#include <bits/stdc++.h> n using namespace std; n const int N = 50; n struct matrix { n   double m[N + 5][N + 5]; n   friend matrix operator*(const matrix &A, const matrix &B) { n     matrix C; n     for (int i = 0; i < N; i++) n       for (int j = 0; j < N; j++) C.m[i][j] = 0; n     for (int i = 0; i < N; i++) n       for (int k = 0; k < N; k++) n         for (int j = 0; j < N; j++) C.m[i][j] += A.m[i][k] * B.m[k][j]; n     return C; n   } n } M; n matrix mpow(matrix A, int p) { n   matrix R; n   for (int i = 0; i < N; i++) n     for (int j = 0; j < N; j++) R.m[i][j] = (i == j); n   for (; p; p >>= 1, A = A * A) n     if (p & 1) R = R * A; n   return R; n } n int n; n double p; n double t[N + 5][N + 5], t2[N + 5][N + 5]; n double s[N + 5][N + 5], s2[N + 5][N + 5]; n double f[N + 5][N + 5]; n void init() { n   t[1][1] = p, t[1][2] = 1 - p; n   for (int i = 2; i < N; i++) { n     t[i][1] = p, t[i][2] = (1 - p) + p * p, t2[i][2] = 1; n     for (int j = 3; j < N; j++) n       t[i][j] = t[i][j - 1] * t[i - 1][j - 1], n       t2[i][j] = t2[i][j - 1] * t[i - 1][j - 1]; n   } n   for (int i = 1; i < N; i++) n     for (int j = 1; j < N; j++) n       s[i][j] = t[i][j] * (1 - t[i - 1][j]), n       s2[i][j] = t2[i][j] * (1 - t[i - 1][j]); n   s2[1][2] = 1; n   double sum = 0; n   for (int i = 1; i < N; i++) sum += s2[N - 1][i]; n   for (int i = 1; i < N; i++) M.m[1][i] = s2[N - 1][i] / sum; n   sum = 0; n   for (int i = 2; i < N; i++) { n     sum += s[N - 1][i - 1]; n     for (int j = 1; j < i; j++) M.m[i][j] = s[N - 1][j] / sum; n   } n   for (int i = 1; i < N; i++) M.m[i][0] = i; n   M.m[0][0] = 1; n   f[1][1] = 1, f[1][2] = 2; n   for (int i = 2; i < N; i++) { n     sum = 0; n     f[i][1] = 1; n     for (int j = 1; j < N; j++) sum += s2[i - 1][j]; n     for (int j = 1; j < N; j++) f[i][1] += f[i - 1][j] * s2[i - 1][j] / sum; n     sum = 0; n     for (int j = 2; j < N; j++) { n       f[i][j] = j, sum += s[i - 1][j - 1]; n       for (int k = 1; k < j; k++) f[i][j] += f[i - 1][k] * s[i - 1][k] / sum; n     } n   } n   f[N - 1][0] = 1; n } n int main() { n   scanf( %d%lf , &n, &p), p /= 1E9, init(); n   double ans = 0; n   if (n >= N) { n     M = mpow(M, n - N + 1); n     for (int i = 0; i < N; i++) n       for (int j = 0; j < N; j++) ans += M.m[i][j] * f[N - 1][j] * s[N - 1][i]; n   } else n     for (int i = 1; i < N; i++) ans += s[n][i] * f[n][i]; n   printf( %.9f n , ans); n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int t; n   cin >> t; n   while (t--) { n     int a[51], n; n     cin >> n; n     for (int i = 0; i < n; i++) cin >> a[i]; n     sort(a, a + n); n     int res = INT_MAX; n     for (int i = 1; i < n; i++) { n       res = min(res, a[i] - a[i - 1]); n     } n     cout << res << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int q; n long long a, b, n; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   cin >> q; n   while (q--) { n     cin >> a >> b >> n; n     if (a > b) swap(a, b); n     long long ans = 0; n     while (b <= n) { n       a += b; n       swap(a, b); n       ans++; n     } n     cout << ans <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, k; n   cin >> n; n   long long int a[45001] = {0}; n   if (n == 1 || n == 2) n     cout <<  No  << endl; n   else { n     cout <<  Yes  << endl; n     cout <<  1  n          <<     << n << endl; n     cout << n - 1; n     for (int i = 1; i <= n - 1; i++) { n       cout <<     << i; n     } n     cout << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int INF = 1000000007; n const int N = 2020; n struct trie { n   trie *c[26]; n   int cnt; n } * root, *nil; n string s1, s2, s3; n int n, l, r, f[N][N], ans; n trie *new_node() { n   trie *node = new (trie); n   for (int i = 0; i < 26; i++) node->c[i] = nil; n   node->cnt = 0; n   return node; n } n void deleteTree(trie *root) { n   for (int i = 0; i < 26; i++) n     if (root->c[i] != nil) { n       deleteTree(root->c[i]); n       delete (root->c[i]); n     } n } n int main() { n   getline(cin, s1); n   scanf( %d n , &n); n   nil = new (trie); n   root = new_node(); n   for (int i = 1; i <= n; i++) { n     deleteTree(root); n     for (int t = 0; t < 26; t++) root->c[t] = nil; n     cin >> s2 >> l >> r; n     for (int u = 0; u < s2.size(); u++) { n       trie *path = root; n       for (int v = u; v < s2.size(); v++) { n         int q = s2[v] -  a ; n         if (path->c[q] == nil) { n           trie *nw = new_node(); n           path->c[q] = nw; n         } n         path = path->c[q]; n         path->cnt++; n       } n     } n     for (int u = 0; u < s1.size(); u++) { n       trie *path = root; n       for (int v = u; v < s1.size(); v++) { n         int q = s1[v] -  a ; n         if (path->c[q] == nil) { n           if (l == 0) n             for (int t = v; t < s1.size(); t++) f[u][t]++; n           break; n         } n         path = path->c[q]; n         if (path->cnt >= l && path->cnt <= r) f[u][v]++; n       } n     } n   } n   deleteTree(root); n   for (int t = 0; t < 26; t++) root->c[t] = nil; n   for (int i = 0; i < s1.size(); i++) { n     trie *path = root; n     for (int j = i; j < s1.size(); j++) { n       int q = s1[j] -  a ; n       if (path->c[q] == nil) { n         trie *nw = new_node(); n         path->c[q] = nw; n       } n       path = path->c[q]; n       if (f[i][j] == n && path->cnt == 0) ans++; n       path->cnt++; n     } n   } n   cout << ans << endl; n } n


#include <bits/stdc++.h> n using namespace std; n using ll = long long; n using pii = pair<int, int>; n void solve(int tc) { n   int n; n   cin >> n; n   vector<vector<int>> adj(n, vector<int>(n)); n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < n; j++) { n       cin >> adj[i][j]; n     } n   } n   int k; n   cin >> k; n   while (k--) { n     int a, b, c; n     cin >> a >> b >> c; n     a--, b--; n     ll sumDist = 0; n     for (int i = 0; i < n; i++) { n       for (int j = i + 1; j < n; j++) { n         int newDist = min(adj[i][a] + c + adj[b][j], adj[i][b] + c + adj[a][j]); n         if (newDist < adj[i][j]) { n           adj[i][j] = adj[j][i] = newDist; n         } n         sumDist += adj[i][j]; n       } n     } n     cout << sumDist <<    ; n   } n } n signed main() { n   ios::sync_with_stdio(0), cin.tie(0); n   int tcs = 1; n   for (int tc = 1; tc <= tcs; tc++) solve(tc); n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   int n; n   cin >> n; n   vector<int> a(n); n   for (int i = 0; i < n; ++i) cin >> a[i]; n   sort(a.begin(), a.end()); n   int ans = a.back() - a[0]; n   int eachCanRemove = (n - 2) / 2; n   for (int i = 0; i + eachCanRemove + 1 < n; ++i) { n     ans = min(ans, a[i + eachCanRemove + 1] - a[i]); n   } n   cout << ans <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int caseno = 1; n typedef long long LL; n typedef unsigned long long ULL; n template <class T> n inline void SWAP(T &a, T &b) { n   T t = a; n   a = b; n   b = t; n } n inline LL POW(LL base, LL power) { n   LL res = base; n   if (power == 0) return 1; n   for (int I = 0; I < power - 1; I++) res *= base; n   return res; n } n int ToInt(string s) { n   int r = 0; n   istringstream sin(s); n   sin >> r; n   return r; n } n double ToDouble(string s) { n   double r = 0; n   istringstream sin(s); n   sin >> r; n   return r; n } n string ToString(int n) { n   string s; n   stringstream convert; n   convert << n; n   s = convert.str(); n   return s; n } n const int SIZE = 500 + 10; n string S[SIZE]; n int main() { n   std::ios_base::sync_with_stdio(0); n   int T, I, J, K, N, n, m, k, cnt = 0, len, R, C; n   cin >> R >> C; n   for (I = 0; I < R; I++) cin >> S[I]; n   for (I = 0; I < R; I++) { n     for (J = 0; J < C; J++) { n       if (S[I][J] ==  . ) S[I][J] =  D ; n     } n   } n   for (I = 0; I < R; I++) { n     for (J = 0; J < C; J++) { n       if (S[I][J] ==  S  && ((J + 1 < C && S[I][J + 1] ==  W ) || n                              (J - 1 >= 0 && S[I][J - 1] ==  W ) || n                              (I + 1 < R && S[I + 1][J] ==  W ) || n                              (I - 1 >= 0 && S[I - 1][J] ==  W ))) { n         cout <<  No n ; n         return 0; n       } n     } n   } n   cout <<  Yes n ; n   for (I = 0; I < R; I++) { n     for (J = 0; J < C; J++) cout << S[I][J]; n     cout <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n int n, m; n int A[100005], B[100005], pass[100005]; n int l; n int main() { n   while (scanf( %d %d , &n, &m) != EOF) { n     int i, j; n     for (i = 1; i <= n; ++i) scanf( %d , &A[i]); n     int sum = 0; n     for (i = n; i >= 1; --i) { n       if (B[A[i]] == 0) { n         ++sum; n         B[A[i]] = 1; n       } n       pass[i] = sum; n     } n     for (j = 1; j <= m; ++j) { n       scanf( %d , &l); n       printf( %d n , pass[l]); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int lower_case[26] = {0}, upper_case[26] = {0}; n char buf[100010]; n int main() { n   scanf( %s , buf); n   int len = strlen(buf); n   for (int i = 0; i < len; i++) { n     if (buf[i] <=  Z ) n       upper_case[buf[i] -  A ]++; n     else n       lower_case[buf[i] -  a ]++; n   } n   int best = 100010; n   best = min(best, lower_case[ a  -  a ] / 2); n   best = min(best, upper_case[ b  -  a ]); n   best = min(best, lower_case[ b  -  a ]); n   best = min(best, lower_case[ u  -  a ] / 2); n   best = min(best, lower_case[ l  -  a ]); n   best = min(best, lower_case[ s  -  a ]); n   best = min(best, lower_case[ r  -  a ]); n   printf( %d n , best); n } n


#include <bits/stdc++.h> n using namespace std; n long long pwr(long long base, long long p, long long mod = (1000000007LL)) { n   long long ans = 1; n   while (p) { n     if (p & 1) ans = (ans * base) % mod; n     base = (base * base) % mod; n     p /= 2; n   } n   return ans; n } n long long gcd(long long a, long long b) { n   if (b == 0) return a; n   return gcd(b, a % b); n } n const int N = 3002; n int n, q, K[400002], ans[400002]; n vector<int> adj[N], qarr[N][N], stack_; n bool visited[N], instack[N]; n set<int> cycles; n void dfs(int v, int src) { n   visited[v] = true; n   instack[v] = true; n   stack_.push_back(v); n   for (auto idx : qarr[src][v]) { n     if (ans[idx] != 0) continue; n     if ((int)stack_.size() >= K[idx] && cycles.empty()) n       ans[idx] = stack_[K[idx] - 1]; n     else n       ans[idx] = -1; n   } n   for (auto vv : adj[v]) { n     if (!visited[vv]) { n       dfs(vv, src); n     } else if (instack[vv]) { n       cycles.insert(vv); n     } n   } n   instack[v] = false; n   cycles.erase(v); n   stack_.pop_back(); n } n int main() { n   int m; n   scanf( %d%d%d , &n, &m, &q); n   while (m--) { n     int a, b; n     scanf( %d%d , &a, &b); n     adj[a].push_back(b); n   } n   for (int i = 1; i <= n; i++) sort(adj[i].begin(), adj[i].end()); n   for (int i = 1; i <= q; i++) { n     int s, t, k; n     scanf( %d%d%d , &s, &t, &k); n     K[i] = k; n     qarr[s][t].push_back(i); n   } n   for (int i = 1; i <= n; i++) { n     memset(visited, 0, sizeof(visited)); n     memset(instack, 0, sizeof(instack)); n     cycles.clear(); n     stack_.clear(); n     dfs(i, i); n   } n   for (int i = 1; i <= q; i++) { n     if (ans[i] == 0) ans[i] = -1; n     printf( %d n , ans[i]); n   } n   return 0; n } n


#include <bits/stdc++.h> n int n, K, F[3005][3005]; n char s[3005][15], n     T[10][10] = { 1110111 ,  0010010 ,  1011101 ,  1011011 ,  0111010 , n                   1101011 ,  1101111 ,  1010010 ,  1111111 ,  1111011 }; n int read() { n   char c = getchar(); n   int ans = 0; n   while (c <  0  || c >  9 ) c = getchar(); n   while (c >=  0  && c <=  9 ) ans = ans * 10 + c -  0 , c = getchar(); n   return ans; n } n void Write(int x) { n   if (x < 10) n     putchar(x ^ 48); n   else n     Write(x / 10), putchar((x % 10) ^ 48); n   return; n } n int Count(int id, int x) { n   int k = 0; n   for (register int i = 0; i < 7; i++) { n     if (s[id][i] ==  1  && T[x][i] ==  0 ) return -1; n     if (s[id][i] ==  0  && T[x][i] ==  1 ) ++k; n   } n   return k; n } n int main() { n   n = read(), K = read(); n   F[n + 1][0] = 1; n   for (register int i = 1; i <= n; i++) scanf( %s , s[i]); n   for (register int i = n; i; i--) n     for (register int j = 0, now; j < 10; j++) { n       if ((now = Count(i, j)) == -1) continue; n       for (register int k = K; k >= now; k--) F[i][k] |= F[i + 1][k - now]; n     } n   if (!F[1][K]) { n     putchar( - ), putchar( 1 ); n     return 0; n   } n   for (register int i = 1; i <= n; i++) n     for (register int j = 9, now; j >= 0; j--) { n       if ((now = Count(i, j)) == -1) continue; n       if (K >= now && F[i + 1][K - now]) { n         putchar( 0  + j), K -= now; n         break; n       } n     } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n using ll = long long; n using ii = pair<int, int>; n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   int q; n   cin >> q; n   for (int cas = 0; cas < q; ++cas) { n     ll a, b, m; n     cin >> a >> b >> m; n     if (a == b) { n       cout << 1 <<     << a <<   n ; n       continue; n     } n     if (b - a <= m) { n       cout << 2 <<     << a <<     << b <<   n ; n       continue; n     } n     bool poss = false; n     for (ll ans = 3; ans <= 50; ++ans) { n       vector<ll> sum(ans + 1, 0); n       sum[1] = a; n       sum[2] = a + 1; n       bool loc = true; n       for (int i = 3; i <= ans; ++i) { n         for (int j = 1; j < i; ++j) { n           sum[i] += sum[j]; n           if (sum[i] > b) { n             loc = false; n             break; n           } n         } n         sum[i]++; n         if (sum[i] > b) loc = false; n         if (!loc) break; n       } n       if (!loc) continue; n       vector<ll> v(ans + 1, 1); n       for (int i = 2; i <= ans; ++i) { n         ll l = 0, r = m; n         for (int j = 0; j < 50; ++j) { n           ll mm = (l + r) / 2; n           if (i == ans) { n             if (sum[ans] + mm > b) n               r = mm; n             else n               l = mm; n           } else { n             if (sum[ans] + mm * (1LL << (ans - (i + 1LL))) > b) n               r = mm; n             else n               l = mm; n           } n         } n         v[i] += l; n         if (i == ans) n           sum[ans] += l; n         else n           sum[ans] += (l * (1LL << (ans - (i + 1LL)))); n       } n       if (sum[ans] > b) break; n       if (sum[ans] == b) { n         cout << ans <<    ; n         ll curr = a; n         vector<ll> res(ans + 1, 0); n         res[1] = a; n         for (int i = 2; i <= ans; ++i) { n           for (int j = 1; j < i; ++j) res[i] += res[j]; n           res[i] += v[i]; n         } n         for (int i = 1; i <= ans; ++i) cout << res[i] <<    ; n         cout <<   n ; n         poss = true; n         break; n       } n     } n     if (!poss) cout << -1 <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long max(long long a, long long b) { n   if (a > b) { n     return a; n   } else { n     return b; n   } n } n long long min(long long a, long long b) { n   if (a < b) { n     return a; n   } else { n     return b; n   } n } n int ma[101][101], yes[101]; n int main() { n   int n; n   cin >> n; n   int r = n / 2; n   if (n % 2 == 1) { n     r++; n   } n   cout << (n / 2) * r << endl; n   for (long long i = 1; i <= n / 2; i++) { n     for (long long j = n / 2 + 1; j <= n; j++) { n       cout << i <<     << j << endl; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int M, N, x, a[400010], mini = 0; n   cin >> M >> N; n   memset(a, 0, sizeof(a)); n   for (int i = 1; i <= M; i++) { n     scanf( %d , &x); n     a[x % N]++; n     if (x % N != mini) n       printf( %d n , a[mini] * N + mini); n     else { n       if (i < N) { n         for (int j = mini; j <= min(N - 1, i); j++) { n           if (a[j] == 0) { n             mini = j; n             break; n           } n           if (a[mini] > a[j]) mini = j; n         } n       } else { n         mini = 0; n         for (int j = 1; j <= min(N - 1, i); j++) { n           if (a[j] == 0) { n             mini = j; n             break; n           } n           if (a[mini] > a[j]) mini = j; n         } n       } n       printf( %d n , a[mini] * N + mini); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long read() { n   long long x = 0, f = 0; n   char c = getchar(); n   while (!isdigit(c)) f |= c ==  - , c = getchar(); n   while (isdigit(c)) x = x * 10 + c -  0 , c = getchar(); n   return f ? -x : x; n } n inline int max(int a, int b, int c) { return max(a, max(b, c)); } n int n, m, a[200010]; n struct Trie { n   int nxt[2], sz; n } T[200010 * 31]; n int rt = 1, top = 1; n void insert(int x) { n   int now = rt; n   for (int i = 31; ~i; --i) { n     if (!T[now].nxt[x >> i & 1]) T[now].nxt[x >> i & 1] = ++top; n     now = T[now].nxt[x >> i & 1]; n   } n   T[now].sz = 1; n } n int ans; n int dfs(int i, int d) { n   if (!i) return 0; n   int ls, rs; n   if (d == -1) return min(2, T[i].sz); n   ls = dfs(T[i].nxt[0], d - 1); n   T[i].sz += T[T[i].nxt[0]].sz; n   rs = dfs(T[i].nxt[1], d - 1); n   T[i].sz += T[T[i].nxt[1]].sz; n   if (ls == 2 && rs == 2) n     ans += min(T[T[i].nxt[0]].sz, T[T[i].nxt[1]].sz) - 1, n         T[i].sz -= min(T[T[i].nxt[0]].sz, T[T[i].nxt[1]].sz) - 1; n   return min(2, ls + rs); n } n int main() { n   cin >> n; n   for (int i = 1; i <= n; ++i) a[i] = read(), insert(a[i]); n   dfs(rt, 31); n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n std::mt19937_64 rng( n     std::chrono::steady_clock::now().time_since_epoch().count()); n long long int myRand(long long int R) { n   long long int val = rng() % R; n   assert(val >= 0); n   return val; n } n const long double PI = atan(1.0) * 4; n const int32_t INF32 = 2e9 + 7; n const int64_t INF64 = 3e18; n const int32_t LOG = 21; n const int32_t MOD = 1e9 + 7; n using namespace std; n void my_debugger(string second, long long int LINE_NUM) { cerr <<   n ; } n template <typename start, typename... end> n void my_debugger(string second, long long int LINE_NUM, start x, end... y) { n   if (second.back() !=  , ) { n     second +=  , ; n     cerr <<  LINE(  << LINE_NUM <<  ):  ; n   } n   long long int i = second.find( , ); n   cerr << second.substr(0, i) <<   =   << x; n   second = second.substr(i + 1); n   if (!second.empty()) cerr <<  ,  ; n   my_debugger(second, LINE_NUM, y...); n } n int32_t main() { n   ios_base::sync_with_stdio(false), cin.tie(nullptr); n   long long int tests = 1; n   for (long long int ii = 0; ii < (long long int)tests; ii++) { n     vector<long long int> a(6); n     for (long long int i = 0; i < (long long int)6; i++) cin >> a[i]; n     long long int n; n     cin >> n; n     vector<long long int> b(n); n     for (long long int i = 0; i < (long long int)n; i++) cin >> b[i]; n     long long int ans = 2e9; n     vector<pair<long long int, long long int> > v; n     for (long long int fret = 0; fret < (long long int)6; fret++) { n       for (long long int i = 0; i < (long long int)n; i++) { n         long long int x = b[i] - a[fret]; n         v.push_back({b[i] - a[fret], i}); n       } n     } n     sort(v.begin(), v.end()); n     long long int siz = v.size(); n     vector<long long int> cnt(n, 0); n     long long int cur = 0; n     long long int tot = 0; n     for (long long int i = 0; i < (long long int)v.size(); i++) { n       while (cur < siz && tot < n) { n         cnt[v[cur].second]++; n         if (cnt[v[cur].second] == 1) tot++; n         cur++; n       } n       if (tot == n) ans = min(ans, v[cur - 1].first - v[i].first); n       cnt[v[i].second]--; n       if (cnt[v[i].second] == 0) tot--; n     } n     cout << ans <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n string a, b; n int r = 0; n int main() { n   getline(cin, a); n   getline(cin, b); n   int k = 0; n   while (a[k] ==  0 ) k++; n   a = a.substr(k, a.size() - k); n   k = 0; n   while (b[k] ==  0 ) k++; n   b = b.substr(k, b.size() - k); n   if (a.size() < b.size()) { n     cout <<  <  << endl; n     return 0; n   } n   if (a.size() > b.size()) { n     cout <<  >  << endl; n     return 0; n   } n   for (int i = 0; i < a.size(); i++) { n     if (a[i] > b[i]) { n       cout <<  >  << endl; n       return 0; n     } n     if (a[i] < b[i]) { n       cout <<  <  << endl; n       return 0; n     } n   } n   cout <<  =  << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   cin >> n; n   char arr[n]; n   int sf = 0, fs = 0; n   for (int i = 0; i < n; i++) { n     cin >> arr[i]; n   } n   for (int j = 0; j < n - 1; j++) { n     if (arr[j] ==  S ) { n       if (arr[j + 1] ==  F ) sf++; n     } else if (arr[j] ==  F ) { n       if (arr[j + 1] ==  S ) fs++; n     } n   } n   if (sf > fs) n     cout <<  YES ; n   else n     cout <<  NO ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 502; n int n, m, l = -1, ok = 1, r; n string s[N]; n int main() { n   cin >> n >> m; n   for (int i = 0; i < n; i++) cin >> s[i]; n   for (int j = 0; j < n; j++) { n     l = r = -1; n     for (int i = 0; i < s[j].length(); i++) { n       if (l == -1 && s[j][i] ==  X ) n         l = r = i; n       else if (l != -1 && s[j][i] ==  X ) { n         r = i; n       } n     } n     if (l != -1) break; n   } n   if (l == -1) { n     cout <<  NO ; n     return 0; n   } n   for (int i = 0; i < n; i++) { n     int cnt = 0; n     for (int j = l; j <= r; j++) n       if (s[i][j] ==  X ) cnt++; n     if (cnt != r - l + 1 && cnt != 0) ok = 0; n   } n   if (ok == 1) n     for (int i = 0; i < n; i++) n       for (int j = 0; j < s[i].length(); j++) n         if ((j < l || j > r) && s[i][j] ==  X ) ok = 0; n   if (ok) n     cout <<  YES ; n   else n     cout <<  NO ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   string str; n   cin >> str; n   string s =  WUB ; n   size_t pos = str.find(s); n   while (pos != string::npos) { n     string token = str.substr(0, pos); n     if (token.length() != 0) cout << token <<    ; n     str.erase(0, pos + 3); n     pos = str.find(s); n   } n   cout << str << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long mod = 1e9 + 7, M = 2e6 + 7, INF = INT_MAX / 10; n long long powe(long long x, long long y) { n   x = x % mod, y = y % (mod - 1); n   long long ans = 1; n   while (y > 0) { n     if (y & 1) { n       ans = (1ll * x * ans) % mod; n     } n     y >>= 1; n     x = (1ll * x * x) % mod; n   } n   return ans; n } n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   ; n   int n, m, p; n   cin >> n >> m >> p; n   int a1[n], a2[m]; n   for (int i = 0; i < n; i++) cin >> a1[i]; n   for (int i = 0; i < m; i++) cin >> a2[i]; n   int p1, p2; n   for (p1 = 0; p1 < n; p1++) { n     if (a1[p1] % p) break; n   } n   for (p2 = 0; p2 < m; p2++) { n     if (a2[p2] % p) break; n   } n   int ans = p1 + p2; n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m, ans = 1e9; n int a[100005]; n inline int read() { n   int x = 0, f = 1; n   char ch = getchar(); n   for (; ch <  0  || ch >  9 ; ch = getchar()) n     if (ch ==  - ) f = -1; n   for (; ch >=  0  && ch <=  9 ; ch = getchar()) x = x * 10 + ch -  0 ; n   return x * f; n } n inline void print(int x) { n   if (x >= 10) print(x / 10); n   putchar(x % 10 +  0 ); n } n int main() { n   n = read(), m = read(); n   for (int i = 1, l, r; i <= m; i++) n     l = read(), r = read(), ans = min(ans, r - l + 1); n   for (int i = 1; i <= ans; i++) a[i] = i - 1; n   for (int i = ans + 1; i <= n; i++) a[i] = a[i - ans]; n   print(ans); n   puts(  ); n   for (int i = 1; i <= n; i++) print(a[i]), putchar(   ); n   puts(  ); n   return 0; n } n


#include <bits/stdc++.h> n const long long mod = (long long)1e9 + 7; n using namespace std; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   long long n, m; n   cin >> n >> m; n   vector<long long> v(m); n   bool valid = true; n   for (long long i = 0; i < m; i++) { n     cin >> v[i]; n     if (v[i] == n or v[i] == 1) valid = 0; n   } n   if (!valid) { n     cout <<  NO  n          <<   n ; n     return 0; n   } n   sort(v.begin(), v.end()); n   for (long long i = 2; i < m; i++) { n     if (v[i] - 1 == v[i - 1] and v[i] - 2 == v[i - 2]) { n       valid = 0; n       break; n     } n   } n   if (valid) n     cout <<  YES  n          <<   n ; n   else n     cout <<  NO  n          <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, k; n   string out, chr; n   cin >> n >> k; n   for (int i = 0; i < k; ++i) chr +=  a  + i; n   for (int i = 0; i < n; ++i) out += chr[i % k]; n   cout << out; n   return 0; n } n


#include <bits/stdc++.h> n #pragma GCC optimize( O2 ) n #pragma GCC optimize( unroll-loops ) n using namespace std; n const int MAX = 47 + 5; n const long long MAX2 = 11; n const long long MOD = 1000000007; n const long long MOD2 = 1000005329; n const long long INF = 2e18; n const int dr[] = {1, 0, -1, 0, 1, 1, -1, -1, 0}; n const int dc[] = {0, 1, 0, -1, 1, -1, 1, -1, 0}; n const double pi = acos(-1); n const double EPS = 1e-9; n const int block = 555; n int n, x[MAX], sm, k, tmp; n long long dp[MAX][MAX][MAX], fr[MAX]; n double ans, res; n long long f(int nw, int rem, int cnt) { n   if (rem == 0) return cnt == 0; n   if (cnt < 0 || nw > n) return 0; n   long long &ret = dp[nw][rem][cnt]; n   if (ret != -1) return ret; n   ret = f(nw + 1, rem, cnt); n   if (x[nw] <= rem) ret += f(nw + 1, rem - x[nw], cnt - 1); n   return ret; n } n int main() { n   cout << fixed << setprecision(10); n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   cin >> n; n   for (int i = 1; i <= n; ++i) cin >> x[i], sm += x[i]; n   cin >> k; n   if (k >= sm) return cout << n <<   n , 0; n   for (int i = 1; i <= n; ++i) { n     tmp = x[i]; n     x[i] = 100; n     memset(dp, -1, sizeof dp); n     for (int j = max(1, k - tmp + 1); j <= k; ++j) n       for (int o = 1; o <= n; ++o) fr[o] += f(1, j, o); n     x[i] = tmp; n   } n   for (int i = 1; i <= n; ++i) { n     res = fr[i] * i; n     for (int j = i + 1; j < n; ++j) res /= j, res *= n - j; n     res /= n; n     ans += res; n   } n   cout << ans <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m, x; n int main() { n   scanf( %d%d , &n, &m); n   x = n; n   for (int i = 0; i < m; i++) { n     int l, r; n     scanf( %d%d , &l, &r); n     x = min(x, r - l + 1); n   } n   printf( %d n , x); n   for (int i = 0; i < n; i++) printf( %d  , i % x); n   printf(  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(0); n   cout.tie(0); n   int n, m; n   cin >> n >> m; n   vector<int> g(n), v(m), b(m); n   for (auto &i : g) cin >> i; n   for (auto &i : v) cin >> i; n   for (auto &i : b) cin >> i; n   int res = 0; n   vector<bool> news(n, false); n   vector<int> x(n, 1); n   for (int i = 1; i <= m; ++i) { n     int a = (v[i - 1] + res - 1) % n + 1; n     int rating = b[i - 1] >= n ? n : b[i - 1], ctr = 0, temp = a; n     if (!news[a - 1]) { n       news[a - 1] = 1; n       ctr++; n     } n     while (rating) { n       rating -= x[a - 1]; n       if (rating <= 0) break; n       temp = g[a - 1]; n       if (!news[temp - 1]) { n         news[temp - 1] = 1; n         ctr++; n       } n       x[a - 1] += x[temp - 1]; n       g[a - 1] = g[temp - 1]; n       a = temp; n     } n     res = ctr; n     cout << ctr <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n inline int in() { n   int x, y; n   y = scanf( %d , &x); n   return x; n } n const int N = 2 * 100 * 1000 + 5; n int a[N], ans[N]; n vector<pair<int, int>> v; n int seg[4 * N]; n void add(int p, int s = 0, int e = N, int x = 1) { n   seg[x]++; n   if (e - s == 1) return; n   int mid = (s + e) / 2; n   if (p < mid) n     add(p, s, mid, 2 * x); n   else n     add(p, mid, e, 2 * x + 1); n } n int pth(int p, int s = 0, int e = N, int x = 1) { n   if (e - s == 1) return s; n   int mid = (s + e) / 2; n   if (seg[2 * x] >= p) n     return pth(p, s, mid, 2 * x); n   else n     return pth(p - seg[2 * x], mid, e, 2 * x + 1); n } n int main() { n   int n = in(); n   for (int i = 0; i < n; i++) { n     a[i] = in(); n     v.push_back({-a[i], i}); n   } n   sort(v.begin(), v.end()); n   vector<pair<pair<int, int>, int>> que; n   int m = in(); n   for (int i = 0; i < m; i++) { n     int k = in(), p = in(); n     que.push_back({{k, p}, i}); n   } n   sort(que.begin(), que.end()); n   int r = 0; n   for (auto q : que) { n     int k = q.first.first, p = q.first.second, t = q.second; n     while (r < k) { n       add(v[r++].second); n     } n     ans[t] = a[pth(p)]; n   } n   for (int i = 0; i < m; i++) cout << ans[i] <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAX = 100000 + 10; n const int Alpha = 27; n string str; n int sum[Alpha], need[Alpha]; n int get(char a) { n   if (a ==  9 ) a =  6 ; n   if (a ==  5 ) a =  2 ; n   return a -  0 ; n } n int main() { n   int i; n   cin >> str; n   for ((i) = (0); (i) != ((int)str.size()); ++(i)) ++need[get(str[i])]; n   cin >> str; n   for ((i) = (0); (i) != ((int)str.size()); ++(i)) ++sum[get(str[i])]; n   int ans = 1000000000; n   for ((i) = (0); (i) != (Alpha); ++(i)) n     if (!sum[i] && need[i]) n       ans = 0; n     else if (need[i]) n       ans = min(ans, sum[i] / need[i]); n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n string s; n long long a[101], b[101]; n int main() { n   long long i, j, k, l, n, c, m, x, y, lft, rgt, p, q; n   cin >> n; n   for (i = 0; i < 2 * n; i++) { n     cin >> a[i]; n   } n   sort(a, a + 2 * n); n   c = 0; n   p = 0; n   long long mnm = 1267357834; n   for (i = 0; i < 2 * n; i++) { n     if (i > 0) { n       p = 0; n       for (j = 0; j < i; j++) { n         b[p++] = a[j]; n       } n     } n     for (j = i + 1; j < 2 * n; j++) { n       q = p; n       for (k = i + 1; k < 2 * n; k++) { n         if (k != j) { n           b[q++] = a[k]; n         } n       } n       long long x = 0; n       for (k = 0; k < q; k += 2) { n         x += abs(b[k] - b[k + 1]); n       } n       mnm = min(mnm, x); n     } n   } n   cout << mnm << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n bool arr[1001][1001]; n struct node { n   int ct; n   int lastind; n }; n struct node1 { n   int ct; n   int lastind; n }; n node row[1001]; n node1 col[1001]; n int main() { n   vector<int> a; n   int n, i, x, y, j, temp; n   scanf( %d , &n); n   for (i = 0; i < n - 1; i++) { n     scanf( %d%d , &x, &y); n     arr[x][y] = 1; n   } n   for (i = 1; i <= n; i++) { n     row[i].ct = 0; n     row[i].lastind = 0; n     for (j = 1; j <= n; j++) { n       if (arr[i][j]) { n         row[i].ct++; n         row[i].lastind = j; n       } n     } n   } n   for (i = 1; i <= n; i++) { n     temp = i; n     for (j = i + 1; j <= n; j++) { n       if (row[temp].ct > row[j].ct) { n         temp = j; n       } else if (row[temp].ct == row[j].ct && n                  row[temp].lastind > row[j].lastind) n         temp = j; n     } n     if (temp != i) { n       a.push_back(1); n       a.push_back(i); n       a.push_back(temp); n       swap(row[i], row[temp]); n       for (j = 1; j <= n; j++) { n         swap(arr[i][j], arr[temp][j]); n       } n     } n   } n   for (i = 1; i <= n; i++) { n     col[i].ct = 0; n     col[i].lastind = n; n     for (j = 1; j <= n; j++) { n       if (arr[j][i]) { n         col[i].ct++; n         col[i].lastind = min(col[i].lastind, j); n       } n     } n   } n   for (i = 1; i <= n; i++) { n     temp = i; n     for (j = i + 1; j <= n; j++) { n       if (col[temp].ct < col[j].ct) { n         temp = j; n       } else if (col[temp].ct == col[j].ct && n                  col[temp].lastind > col[j].lastind) n         temp = j; n     } n     if (temp != i) { n       a.push_back(2); n       a.push_back(i); n       a.push_back(temp); n       swap(col[i], col[temp]); n     } n   } n   printf( %d n , a.size() / 3); n   for (i = 0; i < a.size(); i += 3) { n     printf( %d %d %d n , a[i], a[i + 1], a[i + 2]); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n using ll = long long; n const int maxn = 2e5 + 5; n int a[maxn], lst[maxn]; n bool used[maxn]; n int occ[maxn]; n int main() { n   ios::sync_with_stdio(false); n   int n; n   cin >> n; n   int ans = n; n   for (int i = 1; i <= n; i++) { n     cin >> a[i]; n     lst[a[i]] = i; n     if (occ[a[i]] == 0) ans--; n     occ[a[i]]++; n   } n   cout << ans <<   n ; n   int j = 1; n   for (int i = 1; i <= n; i++) { n     if (!used[a[i]] && (a[i] < j || lst[a[i]] == i)) { n       used[a[i]] = true; n       cout << a[i] <<    ; n     } else { n       while ((j != a[i] && occ[j] > 0) || occ[j] == 1 || used[j]) j++; n       used[j] = true; n       cout << j <<    ; n     } n   } n   cout <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 1e6 + 10, maxm = 2e6 + 10; n const long long INF = 0x3f3f3f3f, mod = 1e9 + 7; n int casn, n, m, k; n namespace graph { n vector<int> g[maxn]; n int all, sz[maxn], root, maxt; n bool vis[maxn]; n int dfs_root(int now, int fa) { n   int cnt = 1; n   for (auto to : g[now]) { n     if (to == fa || vis[to]) continue; n     cnt += dfs_root(to, now); n   } n   int tmp = max(cnt - 1, all - cnt); n   if (maxt > tmp) maxt = tmp, root = now; n   return sz[now] = cnt; n } n int ans[maxn]; n void dfs_col(int now, int fa, int c) { n   ans[now] = c; n   for (auto to : g[now]) n     if (to != fa && !vis[to]) dfs_col(to, now, c); n } n void dfs_dv(int now, int d = 0) { n   vis[now] = 1; n   dfs_col(now, now, d); n   for (auto to : g[now]) { n     if (vis[to]) continue; n     maxt = root = n + 1; n     all = sz[to]; n     dfs_root(to, now); n     dfs_dv(root, d + 1); n   } n } n void solve(int n) { n   all = maxt = root = n + 1; n   dfs_root(1, 1); n   dfs_dv(root); n } n }  // namespace graph n using namespace graph; n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   cin >> n; n   for (int i = 2; i <= n; ++i) { n     int a, b; n     cin >> a >> b; n     g[a].push_back(b); n     g[b].push_back(a); n   } n   solve(n); n   for (int i = 1; i <= n; ++i) cout << char(ans[i] +  A ) <<    ; n } n


#include <bits/stdc++.h> n using namespace std; n long long read() { n   long long s = 0, w = 1; n   char c; n   while (c = getchar(), c >  9  || c <  0 ) n     if (c ==  - ) w = -1; n   while (c >=  0  && c <=  9 ) s = s * 10 + c -  0 , c = getchar(); n   return s * w; n } n void write(long long x) { n   if (x < 0) putchar( - ), x = -x; n   if (x >= 10) write(x / 10); n   putchar(x % 10 +  0 ); n } n void writeln(long long x) { n   write(x); n   puts(  ); n } n const long long N = 1e5 + 7, B = 20, inf = 1e9; n long long n, m, x; n long long a[N]; n struct tree { n   long long la[B], ra[B]; n   long long ans; n } tr[N << 2]; n long long chu(tree ls, tree rs, long long l, long long r, long long mid) { n   long long ln = 0, rn = 0; n   long long la[B + 2], ra[B + 2]; n   long long las[B + 2], ras[B + 2]; n   for (long long i = 0; i < B; i++) n     if (ls.ra[i]) ra[++rn] = ls.ra[i]; n   for (long long i = 0; i < B; i++) n     if (rs.la[i] <= n) la[++ln] = rs.la[i]; n   sort(la + 1, la + ln + 1); n   sort(ra + 1, ra + rn + 1); n   ln = unique(la + 1, la + ln + 1) - la - 1; n   rn = unique(ra + 1, ra + rn + 1) - ra - 1; n   if (!ln && !rn) return x == 0 ? (mid - l + 1) * (r - mid) : 0; n   las[0] = 0; n   ras[rn + 1] = 0; n   ra[rn + 1] = mid; n   la[0] = mid + 1; n   if (!ln) { n     for (long long i = rn; i >= 1; i--) ras[i] = ras[i + 1] | a[ra[i]]; n     for (long long i = rn + 1; i >= 1; i--) { n       if (ras[i] >= x) return (ra[i] - l + 1) * (r - mid); n     } n   } else if (!rn) { n     for (long long i = 1; i <= ln; i++) las[i] = las[i - 1] | a[la[i]]; n     for (long long i = 0; i <= ln; i++) { n       if (las[i] >= x) return (mid - l + 1) * (r - la[i] + 1); n     } n   } else { n     long long ans = 0; n     for (long long i = rn; i >= 1; i--) ras[i] = ras[i + 1] | a[ra[i]]; n     for (long long i = 1; i <= ln; i++) las[i] = las[i - 1] | a[la[i]]; n     for (long long i = 1, j = 0; i <= rn; i++) { n       while (j <= ln && (ras[i] | las[j]) < x) j++; n       if (j > ln) break; n       ans += (ra[i] - (i == 1 ? l - 1 : ra[i - 1])) * (r - la[j] + 1); n     } n     if (ras[rn] != mid) { n       long long j = 0; n       while (j <= ln && las[j] < x) j++; n       if (j <= ln) ans += (mid - ra[rn]) * (r - la[j] + 1); n     } n     return ans; n   } n   return 0; n } n tree pushup(tree ls, tree rs, long long l, long long r, long long mid) { n   tree c; n   for (long long i = 0; i < B; i++) { n     c.la[i] = min(ls.la[i], rs.la[i]); n     c.ra[i] = max(ls.ra[i], rs.ra[i]); n   } n   c.ans = ls.ans + rs.ans + chu(ls, rs, l, r, mid); n   return c; n } n void change(long long ts, long long v, long long l) { n   tr[ts].ans = (v >= x); n   for (long long i = 0; i < B; i++) { n     long long k = v >> i & 1; n     if (k) n       tr[ts].la[i] = l, tr[ts].ra[i] = l; n     else n       tr[ts].la[i] = inf, tr[ts].ra[i] = 0; n   } n } n void build(long long ts, long long l, long long r) { n   if (l == r) { n     change(ts, a[l], l); n     return; n   } n   long long mid = (l + r) >> 1; n   build(ts << 1, l, mid); n   build(ts << 1 | 1, mid + 1, r); n   tr[ts] = pushup(tr[ts << 1], tr[ts << 1 | 1], l, r, mid); n } n void update(long long ts, long long l, long long r, long long k, long long v) { n   if (l == r) { n     change(ts, v, l); n     return; n   } n   long long mid = (l + r) >> 1; n   if (k <= mid) n     update(ts << 1, l, mid, k, v); n   else n     update(ts << 1 | 1, mid + 1, r, k, v); n   tr[ts] = pushup(tr[ts << 1], tr[ts << 1 | 1], l, r, mid); n } n tree query(long long ts, long long l, long long r, long long L, long long R) { n   if (l == L && r == R) return tr[ts]; n   long long mid = (l + r) >> 1; n   if (R <= mid) n     return query(ts << 1, l, mid, L, R); n   else if (L > mid) n     return query(ts << 1 | 1, mid + 1, r, L, R); n   else n     return pushup(query(ts << 1, l, mid, L, mid), n                   query(ts << 1 | 1, mid + 1, r, mid + 1, R), L, R, mid); n } n signed main() { n   n = read(), m = read(), x = read(); n   for (long long i = 1; i <= n; i++) a[i] = read(); n   build(1, 1, n); n   for (long long i = 1, opt, l, r; i <= m; i++) { n     opt = read(), l = read(), r = read(); n     if (opt == 1) n       a[l] = r, update(1, 1, n, l, r); n     else { n       writeln(query(1, 1, n, l, r).ans); n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e5 + 5; n int main() { n   int t; n   scanf( %d , &t); n   ; n   while (t--) { n     int n; n     scanf( %d , &n); n     ; n     if (n % 2 == 0) { n       n = n / 2; n       while (n--) { n         cout << 1; n       } n       cout <<   n ; n     } else { n       n = n / 2; n       n--; n       cout << 7; n       while (n--) { n         cout << 1; n       } n       cout <<   n ; n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n int dp[1 << 6][1 << 6][41][41]; n int h, w; n const int dy[] = {-1, 0, 0, 1, 0}; n const int dx[] = {0, -1, 1, 0, 0}; n int dfs(const int up, const int down, const int cx, const int cy) { n   if (cy == h) { n     int cnt = 0; n     for (int i = 0; i < w; i++) n       if (!((up >> i) & 1)) cnt++; n     return cnt; n   } else if (cx == w) n     return dfs(up, down, 0, cy + 1); n   if (dp[up][down][cy][cx] != -1) return dp[up][down][cy][cx]; n   int res = 0; n   for (int i = 0; i < 5; i++) { n     const int mx = cx + dx[i]; n     const int my = cy + dy[i]; n     if (!(my >= 0 && mx >= 0 && mx < w && my < h)) continue; n     int nup = up; n     int ndown = down; n     if (dy[i] == -1) { n       if ((down >> cx) & 1) n         nup |= (1 << cx); n       else n         nup &= ~(1 << cx); n       ndown &= ~(1 << cx); n       int adNum = 0; n       if (cy == 0) adNum = 0; n       res = max(res, dfs(nup, ndown, cx + 1, cy) + adNum); n     } else if (dy[i] == 1) { n       if ((down >> cx) & 1) n         nup |= (1 << cx); n       else n         nup &= ~(1 << cx); n       ndown |= (1 << cx); n       int adNum = !((up >> cx) & 1); n       if (cy == 0) adNum = 0; n       res = max(res, dfs(nup, ndown, cx + 1, cy) + adNum); n     } else if (dx[i] == 1) { n       ndown |= (1 << mx); n       if ((down >> cx) & 1) n         nup |= (1 << cx); n       else n         nup &= ~(1 << cx); n       ndown &= ~(1 << cx); n       int adNum = !((up >> cx) & 1); n       if (cy == 0) adNum = 0; n       res = max(res, dfs(nup, ndown, cx + 1, cy) + adNum); n     } else if (dx[i] == -1) { n       nup |= (1 << mx); n       if ((down >> cx) & 1) n         nup |= (1 << cx); n       else n         nup &= ~(1 << cx); n       ndown &= ~(1 << cx); n       int adNum = !((up >> cx) & 1); n       if (cy == 0) adNum = 0; n       res = max(res, dfs(nup, ndown, cx + 1, cy) + adNum); n     } else { n       ndown &= ~(1 << cx); n       nup |= (1 << cx); n       int adNum = !((up >> cx) & 1); n       if (cy == 0) adNum = 0; n       res = max(res, dfs(nup, ndown, cx + 1, cy) + adNum); n     } n   } n   return dp[up][down][cy][cx] = res; n } n int main() { n   int n, m; n   cin >> n >> m; n   if (n > m) swap(n, m); n   w = n; n   h = m; n   memset(dp, -1, sizeof(dp)); n   int res = dfs(0, 0, 0, 0); n   cout << res << endl; n   return 0; n } n


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


#include <bits/stdc++.h> n using namespace std; n int gi() { n   int x = 0, w = 1; n   char ch = getchar(); n   while ((ch <  0  || ch >  9 ) && ch !=  - ) ch = getchar(); n   if (ch ==  - ) w = 0, ch = getchar(); n   while (ch >=  0  && ch <=  9 ) n     x = (x << 3) + (x << 1) + ch -  0 , ch = getchar(); n   return w ? x : -x; n } n const int N = 4e5 + 5; n int n, m, col[N], fa[N]; n long long ans[N]; n vector<int> E[N]; n vector<pair<int, int> > vec[N]; n namespace lct { n int fa[N], ch[2][N], sz[N], si[N]; n long long sz2[N], si2[N]; n bool son(int x) { return x == ch[1][fa[x]]; } n bool isrt(int x) { return x != ch[0][fa[x]] && x != ch[1][fa[x]]; } n void up(int x) { n   sz[x] = sz[ch[0][x]] + sz[ch[1][x]] + si[x]; n   sz2[x] = 1ll * sz[ch[0][x]] * sz[ch[0][x]] + n            1ll * sz[ch[1][x]] * sz[ch[1][x]] + si2[x]; n } n void rotate(int x) { n   int y = fa[x], z = fa[y], c = son(x); n   ch[c][y] = ch[c ^ 1][x]; n   if (ch[c][y]) fa[ch[c][y]] = y; n   fa[x] = z; n   if (!isrt(y)) ch[son(y)][z] = x; n   ch[c ^ 1][x] = y; n   fa[y] = x; n   up(y); n } n void splay(int x) { n   for (int y = fa[x]; !isrt(x); rotate(x), y = fa[x]) n     if (!isrt(y)) rotate(son(x) ^ son(y) ? x : y); n   up(x); n } n void access(int x) { n   for (int y = 0; x; y = x, x = fa[x]) { n     splay(x); n     si[x] += sz[ch[1][x]]; n     si2[x] += 1ll * sz[ch[1][x]] * sz[ch[1][x]]; n     ch[1][x] = y; n     si[x] -= sz[y]; n     si2[x] -= 1ll * sz[y] * sz[y]; n     up(x); n   } n } n void link(int x, int y) { n   access(y); n   splay(y); n   splay(x); n   fa[x] = y; n   si[y] += sz[x]; n   si2[y] += 1ll * sz[x] * sz[x]; n   up(y); n } n void cut(int x, int y) { n   access(x); n   access(y); n   splay(y); n   splay(x); n   fa[x] = 0; n   si[y] -= sz[x]; n   si2[y] -= 1ll * sz[x] * sz[x]; n   up(y); n } n int findrt(int x) { n   access(x); n   splay(x); n   while (ch[0][x]) x = ch[0][x]; n   splay(x); n   return x; n } n long long query(int x) { n   splay(x); n   return sz2[x]; n } n void init() { n   for (int i = 1; i <= n + 1; ++i) sz[i] = si[i] = 1; n   for (int i = 1; i <= n; ++i) link(i, ::fa[i]); n } n }  // namespace lct n void dfs(int u) { n   for (int v : E[u]) n     if (v != fa[u]) fa[v] = u, dfs(v); n } n int main() { n   n = gi(); n   m = gi(); n   for (int i = 1; i <= n; ++i) col[i] = gi(); n   for (int i = 1; i < n; ++i) { n     int x = gi(), y = gi(); n     E[x].push_back(y); n     E[y].push_back(x); n   } n   fa[1] = n + 1; n   dfs(1); n   for (int i = 1; i <= n; ++i) vec[col[i]].push_back(make_pair(0, i << 1 | 1)); n   for (int i = 1; i <= m; ++i) { n     int x = gi(), y = gi(); n     vec[col[x]].push_back(make_pair(i, x << 1)); n     col[x] = y; n     vec[col[x]].push_back(make_pair(i, x << 1 | 1)); n   } n   for (int i = 1; i <= n; ++i) vec[col[i]].push_back(make_pair(m + 1, i << 1)); n   lct::init(); n   for (int i = 1; i <= n; ++i) n     for (auto x : vec[i]) { n       int u = x.second >> 1, f = lct::findrt(fa[u]); n       if (x.second & 1) { n         ans[x.first] += lct::query(f); n         lct::cut(u, fa[u]); n         ans[x.first] -= lct::query(u) + lct::query(f); n       } else { n         ans[x.first] += lct::query(u) + lct::query(f); n         lct::link(u, fa[u]); n         ans[x.first] -= lct::query(f); n       } n     } n   for (int i = 1; i <= m; ++i) ans[i] += ans[i - 1]; n   for (int i = 0; i <= m; ++i) printf( %lld n , ans[i]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, k; n   cin >> n >> k; n   vector<pair<int, int> > v; n   while (n--) { n     int x, y; n     cin >> x >> y; n     v.push_back(make_pair(-x, y)); n   } n   sort(v.begin(), v.end()); n   int ind = k - 1; n   int res = 1; n   while (ind - 1 >= 0 && v[ind] == v[ind - 1]) { n     ind--; n     res++; n   } n   ind = k - 1; n   while (ind + 1 < v.size() && v[ind] == v[ind + 1]) { n     ind++; n     res++; n   } n   cout << res << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int T, b, w, tmp; n   scanf( %d , &T); n   while (T--) { n     scanf( %d %d , &b, &w); n     if (b >= w) n       tmp = 2; n     else n       tmp = 3, swap(b, w); n     if (b > 3 * w + 1) n       printf( NO n ); n     else { n       printf( YES n ); n       for (int i = 1; i <= w; ++i) { n         printf( %d t%d n , tmp, 2 * i); n         printf( %d t%d n , tmp, 2 * i - 1); n       } n       b -= w; n       if (b) printf( %d t%d n , tmp, 2 * w + 1), b--; n       int i = 1; n       while (b) { n         printf( %d t%d n , tmp - 1, 2 * i); n         b--; n         if (b) printf( %d t%d n , tmp + 1, 2 * i), b--; n         i++; n       } n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T1, typename T2> n inline bool chmin(T1 &a, T2 b) { n   if (a > b) { n     a = b; n     return 1; n   } n   return 0; n } n template <typename T1, typename T2> n inline bool chmax(T1 &a, T2 b) { n   if (a < b) { n     a = b; n     return 1; n   } n   return 0; n } n template <typename T> n T gcd(T a, T b) { n   if (b == 0) return a; n   return gcd(b, a % b); n } n template <typename T> n T lcm(T a, T b) { n   return a / gcd(a, b) * b; n } n template <typename T> n T pow(T a, int b) { n   return b ? pow(a * a, b / 2) * (b % 2 ? a : 1) : 1; n } n const int mod = 998244353; n long long modpow(long long a, int b) { n   return b ? modpow(a * a % mod, b / 2) * (b % 2 ? a : 1) % mod : 1; n } n void solve(); n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   int T; n   T = 1; n   while (T--) { n     solve(); n   } n } n struct UnionFind { n   vector<double> data; n   int sz; n   UnionFind(int _sz) : data(_sz, -1) { sz = _sz; } n   bool connect(int x, int y) { n     if ((x = root(x)) == (y = root(y))) return false; n     if (data[x] > data[y]) swap(x, y); n     data[x] += data[y]; n     data[y] = x; n     sz--; n     return true; n   } n   bool issame(int x, int y) { return root(x) == root(y); } n   int root(int x) { return data[x] < 0 ? x : data[x] = root(data[x]); } n   int size(int x) { return -data[root(x)]; } n   int size() { return sz; } n }; n void solve() { n   int n; n   cin >> n; n   set<int> s; n   for (int i = 0; i < (n); ++i) { n     int b; n     cin >> b; n     s.insert(b); n   } n   vector<int> a; n   for (auto e : s) a.emplace_back(e); n   sort(a.begin(), a.end()); n   map<long long, int> mp; n   for (int i = 0; i < (n); ++i) mp[a[i]] = i + 1; n   for (int i = 0; i < (33); ++i) { n     UnionFind uni(n); n     for (int j = 0; j < (n - 1); ++j) { n       long long num = a[j] + pow(2LL, i); n       if (mp.count(num)) { n         uni.connect(j, mp[num] - 1); n       } n     } n     vector<vector<int> > v(n); n     for (int j = 0; j < (n); ++j) { n       v[uni.root(j)].emplace_back(a[j]); n     } n     for (int j = 0; j < (n); ++j) { n       if ((int)v[j].size() >= 3) { n         cout << 3 << endl; n         for (int k = 0; k < (3); ++k) cout << v[j][k] <<    ; n         cout << endl; n         return; n       } n     } n   } n   for (int i = 0; i < (33); ++i) { n     UnionFind uni(n); n     for (int j = 0; j < (n - 1); ++j) { n       long long num = a[j] + pow(2LL, i); n       if (mp.count(num)) { n         uni.connect(j, mp[num] - 1); n       } n     } n     vector<vector<int> > v(n); n     for (int j = 0; j < (n); ++j) { n       v[uni.root(j)].emplace_back(a[j]); n     } n     for (int j = 0; j < (n); ++j) { n       if ((int)v[j].size() >= 2) { n         cout << 2 << endl; n         for (int k = 0; k < (2); ++k) cout << v[j][k] <<    ; n         cout << endl; n         return; n       } n     } n   } n   cout << 1 << endl; n   cout << a[0] << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int T; n int main() { n   cin.tie(0); n   ios_base::sync_with_stdio(0); n   cin >> T; n   for (int i = 0; i < T; i++) { n     int n, oneCnt = 0, zeroCnt = 0; n     cin >> n; n     string str; n     for (int j = 0; j < n; j++) { n       char ch; n       cin >> ch; n       str.push_back(ch); n       if (ch ==  1 ) oneCnt++; n     } n     if (oneCnt == 1) { n       cout << 0 <<   n ; n       continue; n     }; n     int len = str.length(); n     int start = 0, end = 0; n     for (int j = 0; j < len; j++) { n       if (str[j] ==  1 ) { n         start = j; n         break; n       } n     } n     for (int j = len - 1; j > -1; j--) { n       if (str[j] ==  1 ) { n         end = j; n         break; n       } n     } n     for (int j = start; j <= end; j++) { n       if (str[j] ==  0 ) zeroCnt++; n     } n     cout << zeroCnt <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long dx[] = {1, -1, 0, 0}; n long long dy[] = {0, 0, 1, -1}; n const int sz = 2e6 + 5, mod = 998244353; n long long t, n, m, k; n const int MAXN = 2e5 + 7; n const long long lim = 1e18; n const long long inf = 1e15; n vector<long long> Prime; n vector<bool> sie(sz, 1); n void sieve() { n   sie[0] = 0; n   sie[1] = 0; n   for (long long i = 2; i < sz; i++) { n     if (sie[i]) { n       for (long long j = i * i; j < sz; j += i) sie[j] = 0; n     } n   } n   for (long long i = 0; i < sz; i++) { n     if (sie[i]) Prime.push_back(i); n   } n   return; n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   sieve(); n   cin >> n; n   vector<long long> v(n); n   for (auto &i : v) cin >> i; n   map<long long, long long> track; n   vector<long long> ans; n   long long in = -1; n   for (int i = 0; i < n; i++) { n     long long a = v[i]; n     bool flag = 0; n     vector<long long> pp; n     for (long long j = 2; j * j <= a; j++) { n       if (a % j == 0) { n         while (a % j == 0) { n           a /= j; n         } n         if (track.find(j) != track.end()) { n           flag = 1; n           break; n         } else n           track[j]; n         pp.push_back(j); n       } n     } n     if (a != 1 && flag == 0) { n       if (track.find(a) != track.end()) { n         flag = 1; n       } else n         track[a]; n     } n     if (flag) { n       for (auto j : pp) track.erase(j); n       for (long long j = v[i] + 1; j < sz; j++) { n         long long a = j; n         bool f = 1; n         vector<long long> arr; n         for (long long k = 2; k * k <= a; k++) { n           if (a % k == 0) { n             while (a % k == 0) { n               a /= k; n             } n             if (track.find(k) != track.end()) { n               f = 0; n               break; n             } else n               track[k]; n             arr.push_back(k); n           } n         } n         if (a != 1 && f) { n           if (track.find(a) != track.end()) { n             f = 0; n           } else n             track[a]; n         } n         if (f) { n           ans.push_back(j); n           break; n         } else { n           for (auto j : arr) track.erase(j); n         } n       } n       in = i + 1; n       break; n     } n     ans.push_back(v[i]); n   } n   if (in == -1) { n     for (auto i : ans) cout << i <<    ; n     return 0; n   } n   int cnt = ans.size(); n   int i = 0; n   while (cnt < n) { n     if (track.find(Prime[i]) == track.end()) { n       cnt++; n       ans.push_back(Prime[i]); n     } n     i++; n   } n   for (auto i : ans) cout << i <<    ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long int i, j, p, d, n, m, total, k, ok, a, b, c, op, maxi, rasp, m1, m2, n     p1, p2, rez, modu = 1000000007, mini, mij, ls, ld; n pair<long long int, long long int> v[300500]; n priority_queue<pair<long long int, long long int> > nxt; n priority_queue<long long int> q; n string s; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cin >> n; n   cin >> a >> b; n   n--; n   for (i = 1; i <= n; i++) { n     cin >> v[i].first >> v[i].second; n   } n   sort(v + 1, v + 1 + n); n   for (i = 1; i <= n; i++) { n     if (v[i].first > a) n       q.push((v[i].second - v[i].first + 1) * -1), rez++; n     else { n       nxt.push(v[i]); n     } n   } n   mini = rez; n   while (a && q.size()) { n     a += q.top(); n     q.pop(); n     if (a < 0) break; n     rez--; n     while (nxt.size() && nxt.top().first > a) { n       q.push((nxt.top().second - nxt.top().first + 1) * -1), rez++; n       nxt.pop(); n     } n     mini = min(rez, mini); n   } n   cout << mini + 1; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long bipow(long long a) { n   if (a == 0) return 1; n   long long aea = bipow(a / 2) % 998244353; n   if (a % 2 == 0) n     return (aea * aea) % 998244353; n   else n     return ((aea * aea) % 998244353 * 2) % 998244353; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   long long n, m; n   cin >> n >> m; n   cout << bipow(n + m) % 998244353 << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct numer { n   int rost; n   int num_of; n   numer() : rost(0), num_of(0) {} n   numer(int _rost, int _num_of) : rost(_rost), num_of(_num_of) {} n }; n int maxim(int a, int b) { return a > b ? a : b; } n int comp(const void *a, const void *b) { n   int *A = (int *)a, *B = (int *)b; n   return *A < *B ? -1 : *A > *B ? 1 : 0; n } n void push(stack<int> &S, numer &chel) { n   for (int i = 0; i < chel.num_of; i++) S.push(chel.rost); n } n int balance(int *Rost, int num, int *&ring, bool else_good = false, n             bool good = false) { n   qsort(Rost, num, sizeof(int), comp); n   vector<numer> people; n   numer temp(Rost[0], 1); n   stack<int> Stack; n   for (int i = 1; i < num; i++) { n     if (Rost[i] == Rost[i - 1]) n       temp.num_of++; n     else { n       people.push_back(temp); n       temp = numer(Rost[i], 1); n     } n   } n   people.push_back(temp); n   int kol = people.size(), min = 0, curr = 0, tmp = 0, tmp1, *p = 0; n   if (else_good == false) { n     for (int i = 1; i <= kol; i++) { n       curr += people[i - 1].num_of; n       if (i == kol) { n         tmp1 = balance(Rost + min, curr - min, p, true, false); n         if (tmp1 > tmp) { n           ring = p; n           tmp = tmp1; n         } n         continue; n       } n       if (people[i].rost - people[i - 1].rost >= 2) { n         tmp1 = balance(Rost + min, curr - min, p, true, false); n         if (tmp1 > tmp) { n           ring = p; n           tmp = tmp1; n         } n         min = curr; n       } n     } n   } n   if (tmp == 0) { n     int new_curr = people[0].num_of, new_min = 0; n     if (good == false) { n       for (int i = 1; i < kol; i++) { n         new_curr += people[i].num_of; n         if (people[i].num_of == 1 || i == kol - 1) { n           tmp1 = balance(Rost + new_min, new_curr - new_min, p, true, true); n           if (tmp1 > tmp) { n             ring = p; n             tmp = tmp1; n           } n           new_min = new_curr - people[i].num_of; n         } n       } n       if (kol == 1) { n         tmp = tmp1 = new_curr; n         ring = new int[tmp]; n         for (int j = 0; j < tmp; j++) ring[j] = people[0].rost; n       } n     } else { n       for (int i = 0; i < kol; i++) { n         Stack.push(people[i].rost); n         people[i].num_of--; n       } n       for (int i = kol - 1; i >= 0; i--) push(Stack, people[i]); n       tmp = Stack.size(); n       ring = new int[tmp]; n       for (int i = 0; i < tmp; i++) { n         ring[i] = Stack.top(); n         Stack.pop(); n       } n     } n   } n   return tmp; n } n int main() { n   int n; n   cin >> n; n   int *A = new int[n], *B = 0; n   for (int i = 0; i < n; i++) cin >> A[i]; n   int k = balance(A, n, B); n   cout << k << endl; n   for (int i = 0; i < k; i++) cout << B[i] <<    ; n   delete[] A; n   delete[] B; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 1e6 + 10; n int tree[maxn * 4], add[maxn * 4]; n void push(int v, int tl, int tr) { n   if (add[v] != 0) { n     tree[v] += add[v]; n     if (tl != tr) { n       int vv = v + v; n       add[vv] += add[v]; n       add[vv + 1] += add[v]; n       tree[v] = max(tree[vv] + add[vv], tree[vv + 1] + add[vv + 1]); n     } n     add[v] = 0; n   } n } n void upd(int v, int tl, int tr, int l, int r, int x) { n   push(v, tl, tr); n   if (tl == l and tr == r) { n     add[v] += x; n     push(v, tl, tr); n     return; n   } n   int tm = tl + tr >> 1, vv = v + v; n   if (r <= tm) n     upd(vv, tl, tm, l, r, x); n   else if (l > tm) n     upd(vv + 1, tm + 1, tr, l, r, x); n   else { n     upd(vv, tl, tm, l, tm, x); n     upd(vv + 1, tm + 1, tr, tm + 1, r, x); n   } n   tree[v] = max(tree[vv] + add[vv], tree[vv + 1] + add[vv + 1]); n } n int get(int v, int tl, int tr) { n   push(v, tl, tr); n   if (tl == tr) return (tree[v] > 0 ? tl : -1); n   int vv = v + v, tm = tl + tr >> 1; n   if (tree[vv + 1] + add[vv + 1] > 0) return get(vv + 1, tm + 1, tr); n   return get(vv, tl, tm); n } n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   int n, m; n   cin >> n >> m; n   int a[n], b[m]; n   for (int i = 0; i < n; i++) { n     cin >> a[i]; n     upd(1, 1, maxn, 1, a[i], 1); n   } n   for (int i = 0; i < m; i++) { n     cin >> b[i]; n     upd(1, 1, maxn, 1, b[i], -1); n   } n   int q; n   cin >> q; n   while (q--) { n     int type, i, x; n     cin >> type >> i >> x; n     if (type == 1) { n       upd(1, 1, maxn, 1, a[i - 1], -1); n       a[i - 1] = x; n       upd(1, 1, maxn, 1, a[i - 1], 1); n     } else { n       upd(1, 1, maxn, 1, b[i - 1], 1); n       b[i - 1] = x; n       upd(1, 1, maxn, 1, b[i - 1], -1); n     } n     cout << get(1, 1, maxn) <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <class T> n void chmax(T& a, const T& b) { n   a = max(a, b); n } n template <class T> n void chmin(T& a, const T& b) { n   a = min(a, b); n } n using ll = long long; n using P = pair<int, int>; n using VI = vector<int>; n using VVI = vector<VI>; n using VL = vector<ll>; n using VVL = vector<VL>; n int dp[6][410][410]; n int d[410]; n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   int tt; n   cin >> tt; n   while (tt--) { n     int n, m; n     cin >> n >> m; n     for (int k = 0; k < int(5); ++k) n       for (int i = 0; i < int(m + 1); ++i) n         for (int j = 0; j < int(m + 1); ++j) dp[k][i][j] = 100; n     int ans = 100; n     for (int _ = 0; _ < int(n); ++_) { n       for (int j = 0; j < int(m); ++j) { n         char c; n         cin >> c; n         d[j + 1] = c -  0 ; n       } n       for (int j = 0; j < int(m); ++j) d[j + 1] += d[j]; n       for (int k = int(5) - 1; k >= 0; --k) n         for (int r = 0; r < int(m + 1); ++r) n           for (int l = int(r - 4 + 1) - 1; l >= 0; --l) { n             if (k == 0 || k == 4) { n               int x = r - 1 - (l + 1) - (d[r - 1] - d[l + 1]); n               if (k == 4) { n                 chmin(ans, x + dp[k][l][r]); n                 dp[k][l][r] += d[r - 1] - d[l + 1] + 1 - (d[l + 1] - d[l]) + 1 - n                                (d[r] - d[r - 1]); n               } else { n                 dp[1][l][r] = x; n               } n             } else { n               int x = d[r - 1] - d[l + 1] + 1 - (d[l + 1] - d[l]) + 1 - n                       (d[r] - d[r - 1]); n               if (k < 3) { n                 dp[k + 1][l][r] = dp[k][l][r] + x; n               } else { n                 chmin(dp[k + 1][l][r], dp[k][l][r] + x); n               } n             } n           } n     } n     cout << ans <<   n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n vector<long long> t1, t2; n long long a, b, n, k, q, day, val, p, t; n void update1(long long i, long long l, long long r, long long qi, n              long long qx) { n   if (l + 1 == r) { n     t1[i] += qx; n     t1[i] = min(t1[i], a); n     return; n   } n   long long m = (r + l) >> 1; n   if (qi < m) { n     update1(2 * i + 1, l, m, qi, qx); n   } else { n     update1(2 * i + 2, m, r, qi, qx); n   } n   t1[i] = t1[2 * i + 1] + t1[2 * i + 2]; n } n void update2(long long i, long long l, long long r, long long qi, n              long long qx) { n   if (l + 1 == r) { n     t2[i] += qx; n     t2[i] = min(t2[i], b); n     return; n   } n   long long m = (r + l) >> 1; n   if (qi < m) { n     update2(2 * i + 1, l, m, qi, qx); n   } else { n     update2(2 * i + 2, m, r, qi, qx); n   } n   t2[i] = t2[2 * i + 1] + t2[2 * i + 2]; n } n long long get1(long long i, long long l, long long r, long long ql, n                long long qr) { n   if (r <= ql || qr <= l) { n     return 0; n   } n   if (ql <= l && r <= qr) { n     return t1[i]; n   } n   long long m = (r + l) / 2; n   return get1(2 * i + 1, l, m, ql, qr) + get1(2 * i + 2, m, r, ql, qr); n } n long long get2(long long i, long long l, long long r, long long ql, n                long long qr) { n   if (r <= ql || qr <= l) { n     return 0; n   } n   if (ql <= l && r <= qr) { n     return t2[i]; n   } n   long long m = (r + l) / 2; n   return get2(2 * i + 1, l, m, ql, qr) + get2(2 * i + 2, m, r, ql, qr); n } n void Not_Stable() { n   cin >> n >> k >> a >> b >> q; n   t1.resize(4 * n); n   t2.resize(4 * n); n   long long x, y, z; n   for (long long i = 0; i < q; i++) { n     cin >> x >> y; n     if (x == 1) { n       cin >> z; n       update1(0, 0, n, y - 1, z); n       update2(0, 0, n, y - 1, z); n     } else { n       cout << get2(0, 0, n, 0, y - 1) + get1(0, 0, n, y - 1 + k, n) << endl; n     } n   } n } n int32_t main() { n   ios_base::sync_with_stdio(false), cin.tie(nullptr); n   ; n   long long g = 1; n   while (g--) Not_Stable(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void read_file(bool outToFile = true) {} n int n, K; n int cnt50, cnt100; n const int MAXN = 51 + 9; n int memo[MAXN][MAXN][2]; n int memoo[MAXN][MAXN][2]; n int vis[MAXN][MAXN][2]; n int C[MAXN][MAXN]; n inline void add(int &x, int v) { n   x += v; n   if (x >= (1000000007)) x -= (1000000007); n } n void build_C() { n   for (int k = 1; k < MAXN; k++) C[0][k] = 0; n   for (int n = 0; n < MAXN; n++) C[n][0] = 1; n   for (int n = 1; n < MAXN; n++) { n     for (int k = 1; k < MAXN; k++) { n       C[n][k] = C[n - 1][k]; n       add(C[n][k], C[n - 1][k - 1]); n     } n   } n } n struct state { n   int cnt50l, cnt100l, cur; n   state(int a, int b, int c) { cnt50l = a, cnt100l = b, cur = c; } n }; n void BFS() { n   memset(vis, 0, sizeof vis); n   memset(memo, -1, sizeof memo); n   vis[0][0][1] = 1; n   queue<state> Q; n   Q.push(state(0, 0, 1)); n   while (!Q.empty()) { n     state s = Q.front(); n     Q.pop(); n     int cnt50l = s.cnt50l, cnt100l = s.cnt100l, cur = s.cur; n     int cur50, cur100; n     if (cur == 0) n       cur50 = cnt50l, cur100 = cnt100l; n     else n       cur50 = cnt50 - cnt50l, cur100 = cnt100 - cnt100l; n     int nret, ncnt; n     int new50, new100; n     for (int i = 0; i <= cur50; i++) { n       for (int j = 0; j <= cur100; j++) { n         if (i + j == 0) continue; n         if (i * 50 + j * 100 > K) break; n         if (cur == 0) { n           new50 = cnt50l - i; n           new100 = cnt100l - j; n         } else { n           new50 = cnt50l + i; n           new100 = cnt100l + j; n         } n         int &old = vis[new50][new100][!cur]; n         if (old == 1) continue; n         old = 1; n         Q.push(state(new50, new100, !cur)); n       } n     } n     if (cnt50l == 0 && cnt100l == 0 && cur == 1) { n       memo[0][0][1] = 0; n       memoo[0][0][1] = 1; n       continue; n     } n     int &ret = memo[cnt50l][cnt100l][cur]; n     int &cnt = memoo[cnt50l][cnt100l][cur]; n     ret = (2000000000), cnt = 0; n     for (int i = 0; i <= cur50; i++) { n       for (int j = 0; j <= cur100; j++) { n         if (i + j == 0) continue; n         if (i * 50 + j * 100 > K) break; n         if (cur == 0) { n           new50 = cnt50l - i; n           new100 = cnt100l - j; n         } else { n           new50 = cnt50l + i; n           new100 = cnt100l + j; n         } n         ncnt = 1LL * C[cur50][i] * C[cur100][j] % (1000000007); n         ncnt = 1LL * ncnt * memoo[new50][new100][!cur] % (1000000007); n         int nret = memo[new50][new100][!cur]; n         if (nret == -1) continue; n         nret++; n         if (ret > nret) n           ret = nret, cnt = ncnt; n         else n           add(cnt, ncnt); n       } n     } n     if (cnt50l == cnt50 && cnt100l == cnt100 && cur == 0) break; n   } n   int ans = memo[cnt50][cnt100][0]; n   int ansc = memoo[cnt50][cnt100][0]; n   if (ans == -1) ans = -1, ansc = 0; n   printf( %d n%d n n , ans, ansc); n } n int main() { n   read_file(); n   build_C(); n   while (scanf( %d%d , &n, &K) != -1) { n     cnt50 = 0, cnt100 = 0; n     for (int i = 0; i < n; i++) { n       int x; n       scanf( %d , &x); n       if (x == 50) n         cnt50++; n       else n         cnt100++; n     } n     BFS(); n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   cin >> n; n   int a[n]; n   for (int i = 1; i <= n; i++) cin >> a[i]; n   int c = 0, t = 0; n   for (int i = 1; i <= n; i++) { n     if (a[i] < 0) { n       c++; n     } else { n       t++; n     } n   } n   if (t == n || n == 1) { n     cout <<  1  << endl << n << endl; n     return 0; n   } n   if (c % 2 == 0) n     cout << c / 2 << endl; n   else n     cout << c / 2 + 1 << endl; n   int f = 0, d = 0; n   for (int i = 1; i < n; i++) { n     if (a[i] < 0) { n       d++; n     } n     if (d == 2 && a[i + 1] < 0) { n       cout << i - f <<    ; n       f = i; n       d = 0; n     } n   } n   cout << n - f << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   string a, b; n   cin >> a; n   b = a; n   long long int x = 0, y = 0, z = 0; n   long long int i; n   for (i = 0; i < a.size(); i++) { n     if (a[i] ==  a ) { n       x++; n     } n     if (a[i] ==  b ) { n       y++; n     } n     if (a[i] ==  c ) { n       z++; n     } n   } n   sort(b.begin(), b.end()); n   if (b == a && (z == x || z == y) && x != 0 && y != 0 && z != 0) { n     cout <<  YES  << endl; n   } else { n     cout <<  NO  << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int a[1001][1001]; n vector<pair<pair<int, int>, char> > start; n int row[1001][1001], col[1001][1001]; n pair<char, int> dir[100001]; n vector<char> ans; n void init() { n   for (int i = 0; i < 1001; i++) { n     for (int j = 0; j < 1001; j++) a[i][j] = -1; n   } n } n int main() { n   int i, j, n, m, x, y, k, len, f; n   string str; n   cin >> n >> m; n   init(); n   for (i = 1; i <= n; i++) { n     cin >> str; n     str =     + str; n     for (j = 1; j <= m; j++) { n       if (str[j] ==  # ) n         a[i][j] = -1; n       else if (str[j] ==  . ) n         a[i][j] = 0; n       else { n         start.push_back(make_pair(make_pair(i, j), str[j])); n         a[i][j] = 0; n       } n     } n   } n   for (i = 1; i <= n; i++) { n     for (j = 1; j <= m; j++) row[i][j] = row[i][j - 1] + a[i][j]; n   } n   for (j = 1; j <= m; j++) { n     for (i = 1; i <= n; i++) col[j][i] = col[j][i - 1] + a[i][j]; n   } n   cin >> k; n   for (i = 1; i <= k; i++) cin >> dir[i].first >> dir[i].second; n   for (i = 0; i < start.size(); i++) { n     x = start[i].first.first; n     y = start[i].first.second; n     f = 0; n     for (j = 1; j <= k; j++) { n       len = dir[j].second; n       if (dir[j].first ==  N ) { n         if (x - len < 1) { n           f = 1; n           break; n         } else if (col[y][x] - col[y][x - len - 1] != 0) { n           f = 1; n           break; n         } else n           x = x - len; n       } else if (dir[j].first ==  S ) { n         if (x + len > n) { n           f = 1; n           break; n         } else if (col[y][x + len] - col[y][x - 1] != 0) { n           f = 1; n           break; n         } else n           x = x + len; n       } else if (dir[j].first ==  E ) { n         if (y + len > m) { n           f = 1; n           break; n         } else if (row[x][y + len] - row[x][y - 1] != 0) { n           f = 1; n           break; n         } else n           y = y + len; n       } else { n         if (y - len < 1) { n           f = 1; n           break; n         } else if (row[x][y] - row[x][y - len - 1] != 0) { n           f = 1; n           break; n         } else n           y = y - len; n       } n     } n     if (!f) ans.push_back(start[i].second); n   } n   if (!ans.size()) { n     cout <<  no solution ; n     return 0; n   } n   sort(ans.begin(), ans.end()); n   for (i = 0; i < ans.size(); i++) cout << ans[i]; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, k; n   scanf( %d%d , &n, &k); n   vector<char> s(n + 1), t(n + 1); n   scanf( %s , &s[0]); n   scanf( %s , &t[0]); n   long long ans = 0; n   long long cnt_a = 0; n   long long cnt_b = 0; n   bool found_diff = false; n   bool big_num = false; n   for (int i = 0; i < n; ++i) { n     if (found_diff && !big_num) { n       cnt_a = cnt_a * 2 + (s[i] ==  a  ? 1 : 0); n       cnt_b = cnt_b * 2 + (t[i] ==  b  ? 1 : 0); n     } n     if (s[i] != t[i]) { n       found_diff = true; n     } n     if (found_diff) { n       ans += min(cnt_a + cnt_b + 2, (long long)k); n       if (cnt_a + cnt_b + 2 >= k) { n         big_num = true; n       } n     } else { n       ++ans; n     } n   } n   printf( %lld n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n; n int a[101]; n int dp[101][1 << 17]; n int M[61]; n vector<int> prime; n bool isprime(int num) { n   int i; n   for (i = 2; i < num; ++i) { n     if (num % i == 0) return 0; n   } n   return 1; n } n void pre() { n   int i, j; n   for (i = 2; i < 60; ++i) { n     if (isprime(i)) prime.push_back(i); n   } n   memset(M, 0, sizeof(M)); n   for (i = 2; i <= 60; ++i) { n     for (j = 0; j < prime.size(); ++j) { n       if (i % prime[j] == 0) M[i] = M[i] + (1 << j); n     } n   } n } n vector<int> ans; n int f(int pos, int mask) { n   int i; n   if (pos == n) n     return 0; n   else if (dp[pos][mask] != -1) n     return dp[pos][mask]; n   else { n     int tmp = INT_MAX; n     for (i = 1; i <= 2 * a[pos]; ++i) { n       if ((mask & M[i]) == 0) n         tmp = min(tmp, abs(a[pos] - i) + f(pos + 1, mask | M[i])); n     } n     return dp[pos][mask] = tmp; n   } n } n void rec(int pos, int mask) { n   int i; n   if (pos == n) n     return; n   else { n     for (i = 1; i <= 2 * a[pos]; ++i) { n       if ((mask & M[i]) != 0) continue; n       if (f(pos, mask) == (abs(a[pos] - i) + f(pos + 1, mask | M[i]))) { n         ans.push_back(i); n         rec(pos + 1, mask | M[i]); n         return; n       } n     } n   } n } n int main() { n   int i; n   pre(); n   cin >> n; n   for (i = 0; i < n; ++i) cin >> a[i]; n   memset(dp, -1, sizeof(dp)); n   rec(0, 0); n   for (i = 0; i < n; ++i) cout << ans[i] <<    ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n bool ask(long long l, long long r) { n   printf( %lld %lld n , l, r); n   fflush(stdout); n   char x[4]; n   scanf(  %s , x); n   return x[0] ==  Y ; n } n long long n, k; n int main() { n   cin >> n >> k; n   long long l = 1, r = n; n   srand(time(0)); n   while (1) { n     if (r - l + 1 <= 50) { n       long long q = l + rand() % (r - l + 1); n       if (ask(q, q)) return 0; n       l = max(1LL, l - k); n       r = min(n, r + k); n     } else { n       long long md = l + ((r - l) >> 1); n       if (ask(l, md)) { n         l = max(1LL, l - k); n         r = min(n, md + k); n       } else { n         l = max(1LL, md + 1 - k); n         r = min(n, r + k); n       } n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n long long gcd(long long a, long long b) { return b == 0 ? a : gcd(b, a % b); } n long long lcm(long long a, long long b) { return a * (b / gcd(a, b)); } n long long power(long long x, long long y, long long p) { n   long long res = 1; n   x = x % p; n   while (y > 0) { n     if (y & 1) res = ((res % p) * (x % p)) % p; n     y = y >> 1; n     x = ((x % p) * (x % p)) % p; n   } n   return res; n } n long long raichu(long long x, long long y) { n   long long res = 1; n   while (y > 0) { n     if (y & 1) res = ((res) * (x)); n     y = y >> 1; n     x = ((x) * (x)); n   } n   return res; n } n bool isprime(long long n) { n   if (n < 2) n     return false; n   else if (n == 2) n     return true; n   else if (n % 2 == 0) n     return false; n   else { n     long long z = sqrt(n); n     for (int i = 0; i < z - 1; i++) n       if (n % (i + 2) == 0) return false; n     return true; n   } n } n int32_t main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   int t = 1, T; n   T = t; n   while (t--) { n     int n; n     cin >> n; n     vector<pair<int, int> > kb(n); n     vector<pair<pair<long double, int>, int> > v; n     vector<pair<long double, int> > minus, plus; n     vector<pair<long long, long long> > lines; n     long long K = 0, B = 0; n     for (int i = 0; i < n; i++) { n       cin >> kb[i].first >> kb[i].second; n       if (kb[i].first == 0 && kb[i].second == 0) n         continue; n       else if (kb[i].first == 0) { n         if (kb[i].second > 0) n           B += kb[i].second; n         else n           continue; n       } else if (kb[i].second == 0) { n         if (kb[i].first > 0) n           v.push_back(make_pair(make_pair(0, 1), i)); n         else n           v.push_back(make_pair(make_pair(0, -1), i)); n       } else { n         if (kb[i].first > 0) n           v.push_back( n               make_pair(make_pair((-1.0 * kb[i].second) / kb[i].first, 1), i)); n         else n           v.push_back( n               make_pair(make_pair((-1.0 * kb[i].second) / kb[i].first, -1), i)); n       } n     } n     n = (int)v.size(); n     int ans = 0; n     sort(v.begin(), v.end()); n     for (int i = 0; i < n; i++) { n       if (v[i].first.second == -1) { n         K += kb[v[i].second].first; n         B += kb[v[i].second].second; n       } n     } n     lines.push_back(make_pair(K, B)); n     for (int i = 0; i < n; i++) { n       long double x = v[i].first.first; n       while (i < n && v[i].first.first == x) { n         if (v[i].first.second == 1) { n           K += kb[v[i].second].first; n           B += kb[v[i].second].second; n         } else { n           K -= kb[v[i].second].first; n           B -= kb[v[i].second].second; n         } n         i++; n       } n       i--; n       lines.push_back(make_pair(K, B)); n     } n     for (int i = 0; i < (int)lines.size() - 1; i++) { n       if (lines[i].first != lines[i + 1].first) ans++; n     } n     cout << ans; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n bool used[15]; n int main() { n   int n, m, a[15], b[15]; n   cin >> n >> m; n   for (int i = 1; i <= n; i++) cin >> a[i]; n   for (int j = 1; j <= m; j++) cin >> b[j]; n   for (int i = 1; i <= n; i++) { n     for (int j = 1; j <= m; j++) { n       if (a[i] == b[j] && used[j] == 0) { n         used[j] = 1; n         cout << a[i] <<    ; n       } n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n int n; n vector<pair<int, long long>> v[63]; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cin >> n; n   long long sum = 0; n   for (int i = 0; i < n; i++) { n     int val; n     long long mask; n     cin >> val >> mask; n     sum += val; n     int j = 62; n     while (!(mask & (1LL << j))) j--; n     v[j].push_back(make_pair(val, mask)); n   } n   if (sum > 0) n     for (int j = 0; j < 63; j++) n       for (auto& p : v[j]) p.first *= -1; n   long long s = 0; n   for (int j = 0; j < 63; j++) { n     sum = 0; n     for (auto& p : v[j]) { n       p.second &= s; n       int b = 0; n       for (int k = 0; k < j; k++) n         if (p.second & (1LL << k)) b++; n       sum += (b % 2 ? -p.first : p.first); n     } n     if (sum < 0) s |= (1LL << j); n   } n   cout << s << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n bool sortbysec(const pair<int, int> &a, const pair<int, int> &b); n long long int gcd(long long int a, long long int b); n int prime(int n); 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, m, x; n     cin >> n >> m; n     vector<vector<long long int> > v(n, vector<long long int>(26, 0)); n     long long int b[26] = {0}; n     string a; n     cin >> a; n     for (int i = 0; i < n; i++) { n       if (i > 0) n         for (int j = 0; j < 26; j++) { n           v[i][j] = v[i - 1][j]; n         } n       v[i][a[i] -  a ]++; n     } n     for (int i = 0; i < m; i++) { n       cin >> x; n       for (int i = 0; i < 26; i++) { n         b[i] += v[x - 1][i]; n       } n     } n     for (int i = 0; i < 26; i++) { n       b[i] += v[n - 1][i]; n     } n     for (int i = 0; i < 26; i++) cout << b[i] <<    ; n     cout << endl; n   } n } n bool sortbysec(const pair<int, int> &a, const pair<int, int> &b) { n   return (a.second < b.second); n } n long long int gcd(long long int a, long long int b) { n   if (b == 0) return a; n   return gcd(b, a % b); n } n int prime(int n) { n   int flag = 1; n   for (int i = 2; i <= sqrt(n); i++) { n     if (n % i == 0) { n       flag = 0; n       break; n     } n   } n   return flag; n } n


#include<bits/stdc++.h> n using namespace std; n template<typename AA>inline void CMax(AA &u,AA v){if(v>u)u=v;} n template<typename AA>inline void CMin(AA &u,AA v){if(v<u)u=v;} n #define Put(a) cout<<(a)<<endl n #define For(a,b,c) for(int a=(b);a<=(c);++a) n #define Fov(a,b,c) for(int a=(b);a>=(c);--a) n #define Foe(a,b,c) for(int a=(b);a<(c);++a) n #define Clear(a,b) memset(a,b,sizeof(a)) n const double eps=1E-8;const int Inf=0x3f3f3f3f,oo=0x7fffffff,Mod=998244353; n typedef long long lon;const lon Lnf=0x3f3f3f3f3f3f3f3f; n template<typename AA>void read(AA &x) n {int s=0;char c;while(c=getchar(),(c< 0 ||c> 9 )&&c!= - );c== - ?(s=1,x=0):(x=c- 0 ); n while(c=getchar(),c>= 0 &&c<= 9 ) x=x*10+c- 0 ;if(s) x=-x;} n template<typename AA, typename...Args>void read(AA &t,Args &...args) n {read(t);read(args...);}template<typename AA>void print(AA k){int num=0; n static char stk[23];if(k<0){putchar( - );k=-k;}while(k>0)stk[++num]=k%10+48,k/=10; n if(!num)putchar( 0 );else while(num)putchar(stk[num--]);putchar(  n );} n inline int Log2(int x) {if(!x)return 0;else return 31-__builtin_clz(x);} n char getc(){char ch;do{ch=getchar();}while(ch==   ||ch==  n );return ch;} n lon Pow(lon d,lon m){lon f=1;while(m){if(m&1)f=f*d%Mod;d=d*d%Mod;m>>=1;}return f;} n //#include<ext/rope> using namespace __gnu_cxx; n //#include<bits/extc++.h>#include<ext/pb_ds/assoc_container.hpp> n //using namespace __gnu_pbds; n //#pragma GCC optimize(3) n int N,M,S,Q,K,T=1; n  n int u,v; n char s[300030]; n  n int n; n int nd[300030]; n int ansp;char ansc; n  n void init() { n  int cnt[26]={}; n  For(a,1,n) { n   if(cnt[s[a]- a ]==0) nd[a]=nd[a-1]+K-1; n   else nd[a]=nd[a-1]-1; n  n   cnt[s[a]- a ]=(cnt[s[a]- a ]+1)%K; n  n   if(a<n&&s[a+1]< z ) { n    Fov(b,25,0) { n     if( a +b==s[a+1]) break; n  n     if(cnt[b]==0) { n      nd[a+1]=nd[a]+K-1; n     } n     else { n      nd[a+1]=nd[a]-1; n     } n  n     if(n-a-1>=nd[a+1]&&(n-a-1-nd[a+1])%K==0) ansp=a+1,ansc= a +b; n  n    } n   } n  n  } n } n  n  n signed main() n { n  //freopen( 1.in , r ,stdin); n  n  read(T); n  while(T--) { n   read(N,K); n   scanf( %s ,s+1); n=strlen(s+1); n   ansp=1;ansc=s[1]+1; n   if(N%K) {puts( -1 );continue;} n  n   init(); n    n   //printf( %d %c n ,ansp,ansc); n  n   if(nd[n]==0) {printf( %s n ,s+1);continue;} n   if(ansp==1&&ansc> z ) {puts( -1 );continue;} n  n    n  n   int cnt[26]={}; n   s[ansp]=ansc; n   For(a,1,ansp) { n    cnt[s[a]- a ]=(cnt[s[a]- a ]+1)%K; n   } n   For(a,0,25) if(cnt[a]!=0) cnt[a]=K-cnt[a]; n  n   int now=n; n   Fov(a,25,0) { n    while(cnt[a]--) s[now--]= a +a; n   } n   while(now>ansp) s[now--]= a ; n  n   printf( %s n ,s+1); n  } n  n  n  return 0; n }


#include <bits/stdc++.h> n using namespace std; n const int N = 1005; n long long a[N][N], dp[N][N][4]; n int n, m; n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   cin >> n >> m; n   for (int i = 1; i <= n; i++) n     for (int j = 1; j <= m; j++) cin >> a[i][j]; n   for (int i = 1; i <= n; i++) n     for (int j = 1; j <= m; j++) n       dp[i][j][0] = max(dp[i - 1][j][0], dp[i][j - 1][0]) + a[i][j]; n   for (int i = n; i >= 1; i--) n     for (int j = m; j >= 1; j--) n       dp[i][j][1] = max(dp[i + 1][j][1], dp[i][j + 1][1]) + a[i][j]; n   for (int i = n; i >= 1; i--) n     for (int j = 1; j <= m; j++) n       dp[i][j][2] = max(dp[i + 1][j][2], dp[i][j - 1][2]) + a[i][j]; n   for (int i = 1; i <= n; i++) n     for (int j = m; j >= 1; j--) n       dp[i][j][3] = max(dp[i - 1][j][3], dp[i][j + 1][3]) + a[i][j]; n   long long ans = 0; n   for (int i = 2; i < n; i++) { n     for (int j = 2; j < m; j++) { n       long long sum = 0; n       sum = n           dp[i][j - 1][0] + dp[i + 1][j][2] + dp[i][j + 1][1] + dp[i - 1][j][3]; n       sum = max(sum, dp[i - 1][j][0] + dp[i][j - 1][2] + dp[i + 1][j][1] + n                          dp[i][j + 1][3]); n       ans = max(ans, sum); n     } n   } n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long mm = 2; n long long n, m0[mm][mm], m1[mm][mm] = {{0, 1}, {2, 1}}, mz[mm][mm], tmp[mm][mm], n                          pwk, dv = 1e9 + 7; n void cop(long long x[mm][mm], long long y[mm][mm]) { n   long long i, j; n   for (i = 0; i < mm; i++) { n     for (j = 0; j < mm; j++) { n       y[i][j] = x[i][j]; n     } n   } n } n void mul(long long x[mm][mm], long long y[mm][mm], long long z[mm][mm]) { n   long long i, j, r; n   for (i = 0; i < mm; i++) { n     for (j = 0; j < mm; j++) { n       tmp[i][j] = 0; n       for (r = 0; r < mm; r++) { n         tmp[i][j] = (tmp[i][j] + x[i][r] * y[r][j]) % dv; n       } n     } n   } n   cop(tmp, z); n } n long long pw(long long x, long long y) { n   if (!y) { n     return 1; n   } n   pwk = pw(x, y / 2); n   pwk = pwk * pwk % dv; n   if (y % 2) { n     pwk = pwk * x % dv; n   } n   return pwk; n } n void mpw(long long x) { n   if (!x) { n     cop(m0, mz); n     return; n   } n   mpw(x / 2); n   mul(mz, mz, mz); n   if (x % 2) { n     mul(mz, m1, mz); n   } n } n int main() { n   long long i, k, dn = 2; n   for (i = 0; i < mm; i++) { n     m0[i][i] = 1; n   } n   scanf( %lld , &n); n   for (i = 0; i < n; i++) { n     scanf( %lld , &k); n     mpw(k); n     cop(mz, m1); n     dn = pw(dn, k) % dv; n   } n   printf( %lld/%lld n , mz[0][0] * pw(2, dv - 2) % dv, dn * pw(2, dv - 2) % dv); n } n


/** n  * Author: lukasdeimos n */ n  n #include<bits/stdc++.h> n  n #define endl   n  n  n #define ff first n #define ss second n  n using ll = long long; n using namespace std; n  n void solve() { n  int n,k; n  cin >> n >> k; n  n  cout << (n-k+k/2) << endl; n  for(int i = k+1; i <= n; i++) cout << i <<    ; n  for(int i = (k+1)/2; i < k; i++) cout << i <<    ; n  cout << endl; n  n } n  n int main() { n  ios::sync_with_stdio(false); n  cin.tie(0); n  cout.tie(0); n   n  ll tt; n  tt = 1; n   cin >> tt; n  for(auto test_case = 1; test_case <= tt; test_case++) { n  // cout <<  Case   << test_case <<  :  ; n   solve(); n  } n   n  n  return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long nCr(int n, int r) { n   if (r > n - r) r = n - r; n   long long ans = 1; n   int i; n   for (i = 1; i <= r; i++) { n     ans *= n - r + i; n     ans /= i; n   } n   return ans; n } n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   ; n   long long a, b, c, d, e, g, h, n, t, f, k, l, m, n       f1 = 0, flag = 0, flag1 = 0, co = 0, co1 = 0, co2 = 0, arr1[100001] = {0}, n       arr[100001] = {0}, sum = 0, sum1 = 0, ma = 0, mi = 1000000000000000001; n   long long co3 = 0, co4 = 0, co5 = 0, co6 = 0, co7 = 0, co8 = 0; n   double pi = 2 * acos(0.0); n   string str, str1, str2, str3; n   char ch; n   vector<long long> v; n   vector<long long> v1; n   vector<string> vs; n   vector<string> vs1; n   map<int, int> mp; n   set<long long> s; n   set<long long> s1; n   set<long long>::iterator it; n   queue<long long> q; n   int x[] = {1, 1, 1, 0, 0, -1, -1, -1}; n   int y[] = {0, 1, -1, 1, -1, 1, -1, 0}; n   cin >> a >> b; n   for (int i = 0; i < a; i++) { n     cin >> c; n     mp[c] = 1; n   } n   for (int i = 1;; i++) { n     if (mp[i] == 1) n       continue; n     else { n       b -= i; n       if (b < 0) break; n       v.push_back(i); n     } n   } n   cout << v.size() << endl; n   for (int i = 0; i < v.size(); i++) cout << v[i] <<    ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 200005; n int n, MOD; n pair<int, int> s[MAXN]; n int main() { n   scanf( %d , &n); n   int m = 0; n   for (int i = 1; i <= n; i++) { n     int a, b; n     scanf( %d , &a); n     s[++m] = make_pair(a, i); n   } n   for (int i = 1; i <= n; i++) { n     int a, b; n     scanf( %d , &a); n     s[++m] = make_pair(a, i); n   } n   sort(s + 1, s + m + 1); n   scanf( %d , &MOD); n   long long res = 1; n   for (int i = 1, j = 1; i <= m; i = j) { n     while (j <= m && s[i].first == s[j].first) j++; n     int l = j - i; n     int t = 0; n     for (int k = i + 1; k < j; k++) n       if (s[k] == s[k - 1]) t++; n     for (int k = 1; k <= l; k++) { n       int tk = k; n       while (t > 0 && tk % 2 == 0) tk /= 2, t--; n       res = (res * tk) % MOD; n     } n   } n   printf( %I64d n , res); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   cin >> n; n   string s; n   cin >> s; n   for (int i = 0; i < n; i++) { n     if (s[i] ==  * ) { n       for (int j = 1; j < n; j++) { n         int t = i; n         int k = 0; n         while (k < 6 && t < n && s[t] ==  * ) { n           t = t + j; n           k++; n         } n         if (k == 5) { n           cout <<  yes ; n           return 0; n         } n       } n     } n   } n   cout <<  no ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int aaa = 0; n int n, pown = 1, a[250005], indx, indy, p[250005], lvl[250005], timer, n        in[250005], out[250005], pw[4 * 250005], st, en, lc, oo, c[6], ans, q, n        typ; n vector<int> v[250005], gg; n pair<int, pair<int, int> > tt[2 * 250005], l[2 * 250005][19], r[2 * 250005][19], n     t[4 * 250005]; n inline void dfs(int x, int par) { n   gg.push_back(x); n   p[x] = par; n   timer++; n   in[x] = timer; n   for (int i = 0; i < v[x].size(); i++) { n     if (v[x][i] == par) continue; n     lvl[v[x][i]] = lvl[x] + 1; n     dfs(v[x][i], x); n   } n   gg.push_back(x); n   timer++; n   out[x] = timer; n } n inline int lca(int x, int y) { n   if (lvl[x] > lvl[y]) swap(x, y); n   if (in[x] <= in[y] && out[x] >= out[y]) return x; n   int t1 = in[x]; n   if (t1 > in[y]) t1 = in[y]; n   int t2 = out[x]; n   if (t2 < out[y]) t2 = out[y]; n   int sz = pw[t2 - t1]; n   pair<int, pair<int, int> > ret = l[t1][sz]; n   if (ret > r[t2][sz]) ret = r[t2][sz]; n   return p[gg[ret.second.first - 1]]; n } n pair<int, pair<int, int> > doit() { n   for (st = 1; st <= 4; st++) { n     for (en = st + 1; en <= 4; en++) { n       lc = lca(c[st], c[en]); n       oo = 1; n       for (int i = 1; i <= 4; i++) { n         if (!((in[c[i]] <= in[c[st]] && out[c[st]] <= out[c[i]]) || n               (in[c[i]] <= in[c[en]] && out[c[en]] <= out[c[i]]))) { n           oo = 0; n           break; n         } n         if (!(in[lc] <= in[c[i]] && out[c[i]] <= out[lc])) { n           oo = 0; n           break; n         } n       } n       if (oo) { n         break; n       } n     } n     if (oo) break; n   } n   if (oo) n     return make_pair(1, make_pair(c[st], c[en])); n   else n     return make_pair(0, make_pair(0, 0)); n } n void upd(int x) { n   if (!x) return; n   t[x].first = 0; n   if (t[2 * x].first && t[2 * x + 1].first) { n     c[1] = t[2 * x].second.first; n     c[2] = t[2 * x].second.second; n     c[3] = t[2 * x + 1].second.first; n     c[4] = t[2 * x + 1].second.second; n     t[x] = doit(); n   } n   upd(x / 2); n } n void cnt(int x, int L, int R) { n   if (t[x].first) { n     if (!c[1]) { n       c[1] = t[x].second.first; n       c[2] = t[x].second.second; n       ans += R - L + 1; n       return; n     } n     c[3] = t[x].second.first; n     c[4] = t[x].second.second; n     pair<int, pair<int, int> > ret = doit(); n     if (ret.first) { n       c[1] = ret.second.first; n       c[2] = ret.second.second; n       ans += R - L + 1; n       return; n     } n   } n   if (!t[2 * x].first) { n     if (L == R) return; n     cnt(2 * x, L, (L + R) / 2); n   } else { n     if (!c[1]) { n       c[1] = t[2 * x].second.first; n       c[2] = t[2 * x].second.second; n       ans += (L + R) / 2 - L + 1; n       if (L == R) return; n       cnt(2 * x + 1, (L + R) / 2 + 1, R); n     } else { n       c[3] = t[2 * x].second.first; n       c[4] = t[2 * x].second.second; n       pair<int, pair<int, int> > ret = doit(); n       if (ret.first) { n         c[1] = ret.second.first; n         c[2] = ret.second.second; n         ans += (L + R) / 2 - L + 1; n         if (L == R) return; n         cnt(2 * x + 1, (L + R) / 2 + 1, R); n       } else { n         if (L == R) return; n         cnt(2 * x, L, (L + R) / 2); n       } n     } n   } n } n int main() { n   ios::sync_with_stdio(false); n   cin >> n; n   while (pown <= n) { n     pown *= 2; n   } n   for (int i = 1; i <= n; i++) { n     cin >> a[i]; n     a[i]++; n   } n   for (int i = 2; i <= n; i++) { n     int x; n     cin >> x; n     v[x].push_back(i); n   } n   cin >> q; n   dfs(1, 1); n   for (int i = 0; i < gg.size(); i++) { n     tt[i + 1] = make_pair(lvl[gg[i]], make_pair(i + 1, i + 1)); n   } n   for (int i = 2 * n; i >= 1; i--) { n     l[i][0] = tt[i]; n     if (tt[i + 1].first < tt[i].first && i != 2 * n) l[i][0] = tt[i + 1]; n     if (i != 2 * n) l[i][0].second.second = i + 1; n     r[l[i][0].second.second][0] = l[i][0]; n     for (int j = 1; j <= 18; j++) { n       l[i][j] = l[i][j - 1]; n       if (l[i][j].first > l[l[i][j - 1].second.second][j - 1].first) { n         l[i][j].first = l[l[i][j - 1].second.second][j - 1].first; n         l[i][j].second.first = l[l[i][j - 1].second.second][j - 1].second.first; n       } n       l[i][j].second.second = l[l[i][j - 1].second.second][j - 1].second.second; n       r[l[i][j].second.second][j] = l[i][j]; n     } n   } n   pw[1] = 0; n   int pp = 2, txx = 1; n   for (int i = 1; i < 19; i++) { n     for (int j = pp; j < 2 * pp; j++) { n       pw[j] = txx; n     } n     pp *= 2; n     txx++; n   } n   for (int i = 1; i <= n; i++) { n     t[pown + a[i] - 1].first = 1; n     t[pown + a[i] - 1].second.first = i; n     t[pown + a[i] - 1].second.second = i; n     upd((pown + a[i] - 1) / 2); n   } n   while (q--) { n     cin >> typ; n     if (typ == 1) { n       cin >> indx >> indy; n       t[pown + a[indx] - 1].first = 1; n       t[pown + a[indx] - 1].second.first = indy; n       t[pown + a[indx] - 1].second.second = indy; n       upd((pown + a[indx] - 1) / 2); n       t[pown + a[indy] - 1].first = 1; n       t[pown + a[indy] - 1].second.first = indx; n       t[pown + a[indy] - 1].second.second = indx; n       upd((pown + a[indy] - 1) / 2); n       swap(a[indx], a[indy]); n     } else { n       ans = 0; n       for (int i = 1; i <= 4; i++) c[i] = 0; n       cnt(1, 1, pown); n       cout << ans << endl; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int gcd(int a, int b) { return (b == 0 ? a : gcd(b, a % b)); } n struct key { n   string name; n   int t, p, g; n }; n int main() { n   int n, s, mt = 0, mg = 0, mp = 0; n   string x; n   cin >> n; n   vector<key> vec(n); n   for (int i = 0; i < n; i++) { n     cin >> s >> vec[i].name; n     vec[i].t = vec[i].p = vec[i].g = 0; n     for (int j = 0; j < s; j++) { n       cin >> x; n       if (x[0] == x[1] && x[1] == x[3] && x[3] == x[4] && x[4] == x[6] && n           x[6] == x[7]) n         vec[i].t++; n       else if (x[1] < x[0] && x[3] < x[1] && x[4] < x[3] && x[6] < x[4] && n                x[7] < x[6]) n         vec[i].p++; n       else n         vec[i].g++; n     } n     mt = max(mt, vec[i].t); n     mg = max(mg, vec[i].g); n     mp = max(mp, vec[i].p); n   } n   int f = 0; n   cout <<  If you want to call a taxi, you should call: ; n   for (int i = 0; i < n; i++) n     if (vec[i].t == mt) { n       if (f) cout <<  , ; n       cout <<     << vec[i].name; n       f = 1; n     } n   f = 0; n   cout <<  .  << endl <<  If you want to order a pizza, you should call: ; n   for (int i = 0; i < n; i++) n     if (vec[i].p == mp) { n       if (f) cout <<  , ; n       cout <<     << vec[i].name; n       f = 1; n     } n   f = 0; n   cout <<  .  << endl n        <<  If you want to go to a cafe with a wonderful girl, you should call: ; n   for (int i = 0; i < n; i++) n     if (vec[i].g == mg) { n       if (f) cout <<  , ; n       cout <<     << vec[i].name; n       f = 1; n     } n   cout <<  . ; n } n


#include <bits/stdc++.h> n using namespace std; n vector<int> a, Q; n vector<vector<int>> Q2; n int n; n void help(int x) { n   int p1, p2, p3, s1, s2, s3, s4; n   vector<int> A, B, C, D; n   p3 = find(a.begin(), a.end(), x) - a.begin(); n   p1 = find(a.begin(), a.end(), x + 1) - a.begin() - 1; n   p2 = p3; n   while (a[p2] != x + 1) --p2; n   while (a[p2 + 1] == a[p2] + 1) ++p2; n   for (int i = 1; i <= p1; ++i) A.push_back(a[i]); n   for (int i = p1 + 1; i <= p2; ++i) B.push_back(a[i]); n   for (int i = p2 + 1; i <= p3; ++i) C.push_back(a[i]); n   for (int i = p3 + 1; i <= n; ++i) D.push_back(a[i]); n   int sz = (!A.empty()) + (!B.empty()) + (!C.empty()) + (!D.empty()); n   if (sz) { n     Q.push_back(sz); n     vector<int> temp; n     if (!A.empty()) temp.push_back(A.size()); n     if (!B.empty()) temp.push_back(B.size()); n     if (!C.empty()) temp.push_back(C.size()); n     if (!D.empty()) temp.push_back(D.size()); n     Q2.push_back(temp); n   } n   vector<int> b{0}; n   for (auto it : D) b.push_back(it); n   for (auto it : C) b.push_back(it); n   for (auto it : B) b.push_back(it); n   for (auto it : A) b.push_back(it); n   a = b; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   cin >> n; n   a = vector<int>(n + 1); n   for (int i = 1; i <= n; ++i) cin >> a[i]; n   while (1) { n     int no = 0; n     for (int j = 1; j < n; ++j) { n       if (find(a.begin(), a.end(), j + 1) < find(a.begin(), a.end(), j)) { n         no = j; n         break; n       } n     } n     if (!no) break; n     help(no); n   } n   cout << Q.size() <<   n ; n   for (int i = 0; i < Q.size(); ++i, cout <<   n ) { n     cout << Q[i] <<    ; n     for (auto it : Q2[i]) cout << it <<    ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int dx[] = {-2, -1, 1, 2, -2, -1, 1, 2}; n int dy[] = {-1, -2, -2, -1, 1, 2, 2, 1}; n template <class T> n inline T biton(T n, T pos) { n   return n | ((T)1 << pos); n } n template <class T> n inline T bitoff(T n, T pos) { n   return n & ~((T)1 << pos); n } n template <class T> n inline T ison(T n, T pos) { n   return (bool)(n & ((T)1 << pos)); n } n template <class T> n inline T gcd(T a, T b) { n   while (b) { n     a = a % b; n     swap(a, b); n   } n   return a; n } n template <class T> n inline T bigmod(T p, T e, T m) { n   T ret = 1; n   for (; e > 0; e >>= 1) { n     if (e & 1) ret = (ret * p) % m; n     p = (p * p) % m; n   } n   return (T)ret; n }; n long long getDigit(long long i) { n   int res = 0; n   while (i) { n     i = i / 10ll; n     res++; n   } n   return res; n } n void solve() { n   long long n; n   cin >> n; n   long long sum = 0; n   long long i = 1; n   long long tdigit = 0; n   long long last = 0; n   while (true) { n     long long digit = getDigit(i); n     tdigit += digit; n     sum += tdigit; n     if (sum >= n) { n       break; n     } n     last = sum; n     i++; n   } n   long long st = 1; n   while (true) { n     long long temp = st; n     string str = to_string(temp); n     for (int i = 0; i < str.size(); i++) { n       last++; n       if (last == n) { n         cout << str[i] << endl; n         ; n         return; n       } n     } n     st++; n   } n } n int main() { n   ios_base::sync_with_stdio(false); n   cout.setf(ios::fixed); n   cout.precision(10); n   cout.tie(nullptr); n   cin.tie(nullptr); n   int t = 1; n   cin >> t; n   while (t--) { n     solve(); n   } n   return 0; n } n