#include <bits/stdc++.h> n using namespace std; n vector<bool> visited; n vector<vector<long long> > adj; n vector<vector<long long> > rev; n vector<long long> order; n vector<char> col; n void dfs1(long long x) { n   visited[x] = true; n   for (auto y : adj[x]) { n     if (!visited[y]) dfs1(y); n   } n   order.push_back(x); n } n void dfs2(long long x, char colour) { n   visited[x] = true; n   col[x] = colour; n   for (auto y : rev[x]) { n     if (!visited[y]) { n       dfs2(y, colour); n     } n   } n } n int32_t main(void) { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   long long i, j; n   long long n, k; n   cin >> n >> k; n   adj.resize(n); n   rev.resize(n); n   long long p[n]; n   for (i = 0; i < n; i++) cin >> p[i]; n   for (i = 0; i < n - 1; i++) { n     long long u = p[i + 1] - 1; n     long long v = p[i] - 1; n     adj[v].push_back(u); n     rev[u].push_back(v); n   } n   for (i = 0; i < n; i++) cin >> p[i]; n   for (i = 0; i < n - 1; i++) { n     long long u = p[i + 1] - 1; n     long long v = p[i] - 1; n     adj[v].push_back(u); n     rev[u].push_back(v); n   } n   visited.assign(n, false); n   for (long long i = 0; i < n; i++) { n     if (!visited[i]) dfs1(i); n   } n   visited.assign(n, false); n   reverse(order.begin(), order.end()); n   vector<long long> condensed[n]; n   char colour =  a ; n   col.resize(n); n   for (i = 0; i < n; i++) { n     long long v = order[i]; n     if (!visited[v]) { n       dfs2(v, colour); n       if (colour !=  z ) ++colour; n     } n   } n   set<long long> numCols; n   for (i = 0; i < n; i++) { n     numCols.insert(col[i]); n   } n   if (numCols.size() >= k) n     cout <<  YES  n          <<   n ; n   else { n     cout <<  NO  n          <<   n ; n     return 0; n   } n   for (auto ch : col) cout << ch; n   cout <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n char s[5000001]; n int dp[5000000]; n int main() { n   gets(s); n   unsigned long long a = 0, l = 0, r = 0, t = 1; n   for (int i = 0; s[i]; i++) { n     l = (l * 109) + s[i]; n     r = r + s[i] * t; n     t *= 109; n     if (l == r) dp[i + 1] = dp[(i + 1) / 2] + 1; n     a += dp[i + 1]; n   } n   cout << a << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long MAX = 1000000000; n long long MOD = 1000000007; n pair<long long, long long> p[100010]; n void solve() { n   long long n; n   cin >> n; n   long long d; n   cin >> d; n   ; n   for (long long i = (0); i < (n); i++) { n     long long in1; n     cin >> in1; n     long long in2; n     cin >> in2; n     ; n     p[i] = {in1, in2}; n   } n   sort(p, p + n); n   long long j = 0, sum = 0, ans = 0; n   for (long long i = (0); i < (n); i++) { n     while (p[j].first - p[i].first < d and j < n) { n       sum += p[j].second; n       j++; n     } n     ans = max(ans, sum); n     sum -= p[i].second; n   } n   cout << ans; n } n int main() { n   ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); n   long long test = 1; n   while (test--) { n     solve(); n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   cin >> n; n   int a[100]; n   for (int i = 0; i < n; i++) cin >> a[i]; n   int num[101] = {0}; n   for (int i = 0; i < n; i++) num[a[i]]++; n   int cnt = 0; n   for (int i = 1; i <= 100; i++) cnt += num[i] / 2; n   cout << cnt / 2 << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int n, i, poz, p[250]; n double t, v[250], m[250], x[250], aux, mint, tempv1, tempv2; n bool fcomp(int a, int b) { return x[a] < x[b]; } n int main() { n   scanf( %d , &n); n   scanf( %lf , &t); n   for (i = 1; i <= n; ++i) n     scanf( %lf , &x[i]), scanf( %lf , &v[i]), scanf( %lf , &m[i]), p[i] = i; n   sort(p + 1, p + n + 1, fcomp); n   do { n     mint = 250.0; n     for (i = 1; i < n; ++i) n       if (v[p[i]] > v[p[i + 1]]) { n         aux = -(x[p[i]] - x[p[i + 1]]) / (v[p[i]] - v[p[i + 1]]); n         if (aux < mint) mint = aux, poz = i; n       } n     if (mint >= t) break; n     t -= mint; n     for (i = 1; i <= n; ++i) x[i] += v[i] * mint; n     tempv1 = (m[p[poz]] - m[p[poz + 1]]) * v[p[poz]] + n              2 * m[p[poz + 1]] * v[p[poz + 1]]; n     tempv1 /= m[p[poz]] + m[p[poz + 1]]; n     tempv2 = n         (m[p[poz + 1]] - m[p[poz]]) * v[p[poz + 1]] + 2 * m[p[poz]] * v[p[poz]]; n     tempv2 /= m[p[poz]] + m[p[poz + 1]]; n     v[p[poz]] = tempv1; n     v[p[poz + 1]] = tempv2; n   } while (1); n   for (i = 1; i <= n; ++i) x[i] += v[i] * t, printf( %.9lf n , x[i]); n } n


#include <bits/stdc++.h> n const long long mod = 1e6 + 3; n using namespace std; n int g = 0; n int vis[10]; n int a[100005]; n int main() { n   int n; n   cin >> n; n   set<int> s; n   for (int i = 1; i <= n; i++) scanf( %d , a + i), s.insert(a[i]); n   int p = s.size(); n   if (p <= 3) { n     if (p == 1) { n       printf( YES n ); n     } else if (p == 2) { n       int b[3]; n       g = 0; n       for (set<int>::iterator it = s.begin(); it != s.end(); it++) n         g++, b[g] = (*it); n       for (int i = 1; i <= n; i++) { n         if (a[i] == b[1]) n           vis[1]++; n         else n           vis[2]++; n       } n       printf( YES n ); n     } else if (p == 3) { n       int c[5]; n       g = 0; n       for (set<int>::iterator it = s.begin(); it != s.end(); it++) n         g++, c[g] = (*it); n       sort(c + 1, c + 1 + g); n       for (int i = 1; i <= n; i++) { n         if (a[i] == c[1]) n           vis[1]++; n         else if (a[i] == c[2]) n           vis[2]++; n         else n           vis[3]++; n       } n       if ((c[1] + c[3]) == 2 * c[2]) n         printf( YES n ); n       else n         printf( NO n ); n     } n   } else n     printf( NO n ); n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   long long n; n   cin >> n; n   long long k; n   cin >> k; n   long long arr[n + 1]; n   for (long long i = 1; i <= n; i++) { n     cin >> arr[i]; n   } n   set<long long> s; n   for (long long i = k; i <= n; i++) { n     s.insert(arr[i]); n   } n   if (s.size() != 1) { n     cout << -1 << endl; n   } else { n     long long index = k; n     while (index >= 1 && arr[index] == arr[k]) { n       index--; n     } n     index++; n     cout << index - 1 << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int n, curbest; n int ans[5005], t[5005]; n int cnt[5005]; n int main() { n   cin >> n; n   for (int i = 1; i <= n; i++) cin >> t[i]; n   for (int i = 1; i <= n; i++) { n     curbest = 0; n     memset(cnt, 0, sizeof cnt); n     for (int j = i; j <= n; j++) { n       cnt[t[j]]++; n       if (cnt[t[j]] > cnt[curbest] || n           cnt[t[j]] == cnt[curbest] && t[j] < curbest) { n         curbest = t[j]; n       } n       ans[curbest]++; n     } n   } n   for (int i = 1; i <= n; i++) { n     cout << ans[i]; n     if (i != n) n       cout <<    ; n     else n       cout << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int mark[100005]; n long long msum[100005]; n long long sum[100005]; n int c[100005]; n long long cs; n int main() { n   long long ans = 0; n   int i, n, m, x; n   scanf( %d%d , &n, &m); n   for (i = 1; i <= n; ++i) scanf( %d , &c[i]); n   for (i = 1; i <= m; ++i) { n     scanf( %d , &x); n     mark[x] = 1; n   } n   for (i = n; i > 0; --i) { n     sum[i] = sum[i + 1] + c[i]; n     msum[i] = msum[i + 1] + c[i] * mark[i]; n   } n   for (i = 1; i <= n; ++i) n     if (mark[i]) n       ans += c[i] * sum[i + 1]; n     else { n       ans += c[i] * msum[i + 1]; n       if (!mark[i + 1]) ans += (long long)c[i] * c[i + 1]; n       if (i == 1 && !mark[n]) ans += (long long)c[i] * c[n]; n     } n   printf( %I64d n , ans); n   fclose(stdin); n   fclose(stdout); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 103; n const int mod = 1e9 + 7; n char s[N]; n long long g[N], f[N][N][N]; n long long qpow(long long a, long long n) { n   long long ret = 1; n   while (n) { n     if (n & 1) ret = ret * a % mod; n     a = a * a % mod; n     n >>= 1; n   } n   return ret; n } n int main() { n   int n, m; n   cin >> n >> m; n   scanf( %s , s + 1); n   g[0] = g[1] = 1; n   for (int i = 2; i <= m; i++) (g[i] = g[i - 1] + g[i - 2]) %= (mod - 1); n   for (int i = 0; i <= m; i++) g[i] = qpow(2, g[i]); n   for (int i = 1; i <= n; i++) { n     if (s[i] ==  1 ) n       f[1][i][i] = 1; n     else n       f[0][i][i] = 1; n   } n   for (int i = 2; i <= m; i++) n     for (int l = 1; l <= n; l++) n       for (int r = l; r <= n; r++) { n         if (l == 1) n           (f[i][l][r] += f[i - 2][l][r] * g[i - 1] % mod) %= mod; n         else n           (f[i][l][r] += f[i - 2][l][r]) %= mod; n         if (r == n) n           (f[i][l][r] += f[i - 1][l][r] * g[i - 2] % mod) %= mod; n         else n           (f[i][l][r] += f[i - 1][l][r]) %= mod; n         for (int j = 1; j < r; j++) { n           (f[i][l][r] += f[i - 1][l][j] * f[i - 2][j + 1][r] % mod) %= mod; n         } n       } n   cout << f[m][1][n] << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n = 0, k = 0, i = 0, j = 0; n   char str[100]; n   cin >> n >> k; n   cin >> str; n   if (k <= n - k) { n     for (i = 1; i < k; i++) { n       cout <<  LEFT  << endl; n     } n     for (i = 0; i < strlen(str) - 1; i++) { n       cout <<  PRINT   << str[i] << endl; n       cout <<  RIGHT  << endl; n     } n     cout <<  PRINT   << str[strlen(str) - 1] << endl; n   } n   if (k > n - k) { n     for (i = 0; i < n - k; i++) { n       cout <<  RIGHT  << endl; n     } n     for (i = strlen(str) - 1; i > 0; i--) { n       cout <<  PRINT   << str[i] << endl; n       cout <<  LEFT  << endl; n     } n     cout <<  PRINT   << str[0] << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 1e6; n const int INF = 1e9; n const long long linf = 0x3f3f3f3f3f3f3f3f; n const double PI = acos(-1.0); n long long mod = 1e9 + 7; n int main() { n   int n, a; n   cin >> n >> a; n   cout << 2 <<     << 1 <<     << max(3, min(n, ((n * a + 90) / 180 + 2))) n        << endl; n } n


#include <bits/stdc++.h> n #pragma GCC optimize( Ofast ) n #pragma GCC target( sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx ) n using namespace std; n bool is_prime(long long n) { n   for (long long i = 2; i * i <= n; ++i) { n     if (n % i == 0) { n       return false; n     } n   } n   return true; n } n vector<long long> fact(long long n) { n   vector<long long> ans; n   for (int i = 2; i * i <= n; i++) { n     if (n % i == 0) { n       ans.push_back(i); n       while (n % i == 0) { n         n /= i; n       } n     } n   } n   if (n > 1) ans.push_back(n); n   return ans; n } n long long getPow(long long a, long long b) { n   long long res = 1ll, tp = a; n   while (b) { n     if (b & 1ll) { n       res *= tp; n     } n     tp *= tp; n     b >>= 1ll; n   } n   return res; n } n long long vec_mult(long long x1, long long y1, long long x2, long long y2, n                    long long x3, long long y3) { n   return abs((x2 - x1) * (y3 - y1) - (y2 - y1) * (x3 - x1)); n } n void ok() { n   cout <<  YES  << endl; n   exit(0); n } n void no() { n   cout <<  NO  << endl; n   exit(0); n } n inline long long nxt() { n   long long x; n   cin >> x; n   return x; n } n const long long N = 3000 + 5, inf = 2e16; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0), cout.tie(0); n   long long n = nxt(), m = nxt(), k = nxt(); n   vector<long long> used(n, 1); n   for (int i = 0; i < m; i++) { n     long long x = nxt(); n     used[x] = 0; n   } n   vector<long long> a(k); n   for (int i = 0; i < k; i++) { n     a[i] = nxt(); n   } n   if (!used[0]) { n     return cout << -1, 0; n   } n   vector<long long> pos; n   for (int i = 0; i < n; i++) { n     if (used[i]) pos.push_back(i); n   } n   auto get = [&](long long l) { n     long long ct = 0; n     long long start = 0; n     long long ind = 0; n     if (!pos.size()) return inf; n     while (true) { n       ct++; n       long long cur = start + l; n       if (cur >= n) { n         start = cur; n         break; n       } n       long long cur_ind = n           (upper_bound(pos.begin(), pos.end(), cur)) - pos.begin(); n       if (cur_ind == pos.size()) n         cur_ind--; n       else if (pos[cur_ind] > cur) { n         cur_ind--; n       } n       if (pos[cur_ind] == start) break; n       if (cur < n) { n         start = pos[cur_ind]; n       } else { n         start = cur; n         break; n       } n     } n     if (start >= n) n       return ct; n     else n       return inf; n   }; n   long long ans = inf; n   for (int i = 0; i < k; i++) { n     long long ct = get(i + 1); n     if (ct == inf) continue; n     ans = min(ans, ct * a[i]); n   } n   if (ans >= inf) n     cout << -1; n   else n     cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   char a[1000000], b[1000000]; n   int n; n   cin >> n >> a; n   int l = strlen(a); n   for (int i = 1; i < n; i++) { n     cin >> b; n     for (int j = 0; j < l; j++) { n       if (a[j] ==  ? ) n         a[j] = b[j]; n       else if (b[j] ==  ? ) n         continue; n       else if (a[j] != b[j]) n         a[j] =  0 ; n     } n   } n   for (int i = 0; i < l; i++) { n     if (a[i] ==  0 ) n       a[i] =  ? ; n     else if (a[i] ==  ? ) n       a[i] =  x ; n   } n   cout << a; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAX = 1000006; n int rnk[MAX], parent[MAX], ans[MAX], arr[MAX]; n vector<int> comp[MAX]; n priority_queue<int> q[MAX]; n int find(int x) { n   if (x == parent[x]) return x; n   return parent[x] = find(parent[x]); n } n void merge(int x, int y) { n   x = find(x); n   y = find(y); n   if (x == y) return; n   if (rnk[x] > rnk[y]) n     parent[y] = x; n   else { n     parent[x] = y; n     if (rnk[x] == rnk[y]) rnk[y]++; n   } n } n int main() { n   int n, m, a, b; n   scanf( %d%d , &n, &m); n   for (int i = 0; i < n; i++) { n     scanf( %d , &arr[i]); n     parent[i] = i; n     rnk[i] = 0; n   } n   for (int i = 0; i < m; i++) { n     scanf( %d%d , &a, &b); n     a--, b--; n     merge(a, b); n   } n   int maxc = 0; n   for (int i = 0; i < n; i++) { n     comp[find(i)].push_back(i); n     maxc = max(maxc, find(i)); n     q[find(i)].push(arr[i]); n   } n   for (int i = 0; i <= maxc; i++) { n     for (int j = 0; j < comp[i].size(); j++) { n       ans[comp[i][j]] = q[i].top(); n       q[i].pop(); n     } n   } n   for (int i = 0; i < n; i++) printf( %d  , ans[i]); n   return 0; n } n


#include <bits/stdc++.h> n bool office[25][25]; n size_t m, n; n bool check(size_t x1, size_t y1, size_t x2, size_t y2) { n   for (size_t x = x1; x <= x2; ++x) { n     for (size_t y = y1; y <= y2; ++y) { n       if (office[x][y]) { n         return false; n       } n     } n   } n   return true; n } n bool check(size_t x, size_t y) { n   for (size_t i = 0; i < m - x; ++i) { n     for (size_t j = 0; j < n - y; ++j) { n       if (check(i, j, i + x, j + y)) { n         return true; n       } n     } n   } n   return false; n } n int main() { n   std::cin >> m >> n; n   std::string row; n   size_t i = 0; n   while (std::cin >> row) { n     for (size_t j = 0; j < row.size(); ++j) { n       office[i][j] = row[j] ==  1 ; n     } n     ++i; n   } n   size_t best = 4; n   for (size_t x = 0; x < m; ++x) { n     for (size_t y = 0; y < n; ++y) { n       size_t p = (x + y + 2) * 2; n       if (p > best && check(x, y)) { n         best = p; n       } n     } n   } n   std::cout << best; n } n


#include <bits/stdc++.h> n using namespace std; n int a[111111], b[111111], c[111111]; n int main() { n   int n; n   cin >> n; n   for (int i = 1; i <= n; ++i) { n     cin >> a[i]; n   } n   for (int i = 1; i <= n; ++i) { n     b[i] = min(a[i], min(b[i - 1] + 1, i)); n   } n   reverse(a + 1, a + n + 1); n   for (int i = 1; i <= n; ++i) { n     c[i] = min(a[i], min(c[i - 1] + 1, i)); n   } n   reverse(c + 1, c + n + 1); n   int ans = 0; n   for (int i = 1; i <= n; ++i) { n     ans = max(ans, min(b[i], c[i])); n   } n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n; n vector<string> s; n string ans(200,    ); n set<string> Set; n vector<string> cand; n int main() { n   cin >> n; n   ans.resize(2 * n - 2); n   for (int i = 0; i < 2 * n - 2; ++i) { n     string a; n     cin >> a; n     Set.insert(a); n     s.push_back(a); n     if (a.size() == n - 1) cand.push_back(a); n   } n   bool prefix = true, suffix = true; n   string P, S; n   for (int i = 1; i < n - 1; ++i) prefix &= Set.count(cand[0].substr(0, i)); n   for (int i = 1; i < n - 1; ++i) n     suffix &= Set.count(cand[1].substr(n - 1 - i)); n   if (suffix && prefix && cand[0].substr(1, n - 1) == cand[1].substr(0, n - 2)) n     P = cand[0], S = cand[1]; n   else n     P = cand[1], S = cand[0]; n   for (int i = 1; i <= n - 1; ++i) { n     string sub = P.substr(0, i); n     for (int j = 0; j < ans.size(); ++j) n       if (ans[j] ==     && s[j] == sub) { n         ans[j] =  P ; n         break; n       } n   } n   for (int i = 0; i < ans.size(); ++i) n     if (ans[i] ==    ) ans[i] =  S ; n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 100000 + 10; n const int M = 1000000007; n const double PI = atan(1) * 4; n const int oo = 2100000000; n int main() { n   int n; n   cin >> n; n   vector<pair<int, int> > v(n), g(n); n   for (int i = 0; i < n; ++i) { n     scanf( %d%d , &v[i].first, &v[i].second); n     int x = v[i].first; n     int w = v[i].second; n     g[i].first = x + w; n     g[i].second = x - w; n   } n   sort((g).begin(), (g).end()); n   int ls = -1e9, ans = 0; n   for (int i = 0; i < n; ++i) { n     if (g[i].second >= ls) { n       ++ans; n       ls = g[i].first; n     } n   } n   cout << 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 n; n     cin >> n; n     for (int i = 0; i < n; i++) cout <<  1  ; n     cout << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n int main() { n   int n; n   scanf( %d , &n); n   char *str = new char[n + 1]; n   scanf( %s , str); n   int size = strlen(str); n   int sum1 = 0, sum2 = 0; n   bool f = true; n   for (int i = 0; i < size / 2; ++i) { n     if ((str[i] ==  4  || str[i] ==  7 ) && n         (str[size - i - 1] ==  4  || str[size - i - 1] ==  7 )) { n       sum1 += str[i] -  0 ; n       sum2 += str[size - i - 1] -  0 ; n     } else { n       f = false; n       break; n     } n   } n   if (sum1 == sum2 && f) n     printf( YES n ); n   else n     printf( NO n ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long nb, ns, nc, pb, ps, pc, ab, as, ac, money; n bool solve(long long k) { n   long long cb = k * ab, cs = k * as, cc = k * ac; n   long long cost = 0; n   cost += max(cb - nb, 0LL) * pb; n   cost += max(cs - ns, 0LL) * ps; n   cost += max(cc - nc, 0LL) * pc; n   return cost <= money; n } n int main() { n   ios_base::sync_with_stdio(false); n   string s; n   cin >> s; n   for (int i = 0; i < s.size(); ++i) n     if (s[i] ==  B ) n       ab++; n     else if (s[i] ==  S ) n       as++; n     else n       ac++; n   cin >> nb >> ns >> nc >> pb >> ps >> pc; n   cin >> money; n   long long lo = 0, hi = 1e14, mid; n   while (lo < hi) { n     mid = (lo + hi + 1) / 2; n     if (solve(mid)) n       lo = mid; n     else n       hi = mid - 1; n   } n   cout << lo <<   n ; n } n


#include<bits/stdc++.h> n using namespace std; n  n #define FOR(i, a, b) for(int i = (a); i < (b); ++i) n #define RFOR(i, b, a) for(int i = (b) - 1; i >= (a); --i) n #define REP(i, N) FOR(i, 0, N) n #define RREP(i, N) RFOR(i, N, 0) n #define FILL(A,value) memset(A,value,sizeof(A)) n #define endl   n  n  n #define ALL(V) V.begin(), V.end() n #define SZ(V) (int)V.size() n #define watch(x) cout << (#x) <<   is   << (x) << endl n #define pb push_back n #define mp make_pair n #define Pi 3.14159265358979 n #define A first n #define B second n  n typedef long long ll; n typedef vector <int> vi; n typedef vector<ll> vl; n typedef pair <int, int> pi; n typedef pair <ll, int> pl; n typedef pair <ll, ll> pll; n typedef pair <double, double> pd; n  n //change these according to prob constraints n const int INF = 0x3f3f3f3f; n const int MAX = 100005; n const int MAX1 = 1005; n const int MAX2 = 105; n const int LEN = 105; n const int BASE = 1000000000; n const double EPS = 1e-7; n const int MOD = 1000000007; n //g++ -std=c++11 your_file.cpp -o your_program n  n ll mypow(ll a, ll b){ n  ll ans=1; n  while(b){ n   if(b&1) n    ans=(ans*a)%MOD; n   b/=2; n   a=(a*a)%MOD; n  } n  return ans; n } n  n int main(void){ n  ios_base::sync_with_stdio(false); n  cin.tie(NULL); n  cout.tie(NULL); n  ifstream in(  ); n  ofstream out(  ); n  clock_t begin = clock(); n  int q; n     cin >> q; n     while (q--){ n         int n; n         cin >> n; n         if (n == 1) cout << 9 << endl; n         else if (n == 2) cout << 98 << endl; n         else if (n == 3) cout << 989 << endl; n         if (n <= 3) continue; n         string s =  98 ; n         int ct = 9; n         REP(i, n - 2){ n             if (ct == 10) ct = 0; n             s += to_string(ct); n             ct++; n         } n         cout << s << endl; n     } n  clock_t end = clock(); n     double esecs = double(end - begin) / CLOCKS_PER_SEC; n     //cout << esecs <<   seconds  << endl; n  return 0;  n }


#include <bits/stdc++.h> n using namespace std; n int s[50]; n char a[55][55]; n int main() { n   int n, i, j, c = 1, k, found; n   scanf( %d , &n); n   for (i = 0; i < n; i++) { n     scanf( %s , a[i]); n     int l = strlen(a[i]); n     for (j = 0; j < l; j++) { n       s[a[i][j] -  a ]++; n     } n   } n   for (i = 0; i < 26; i++) { n     if (s[i] == 0) { n       printf( %c , (char)(i +  a )); n       c = 1; n       break; n     } else n       c = 2; n   } n   if (c == 2) { n     for (i = 0; i < 26; i++) { n       for (j = 0; j < 26; j++) { n         char str[4]; n         found = 0; n         str[0] = (char)(i +  a ); n         str[1] = (char)(j +  a ); n         for (k = 0; k < n; k++) { n           int l = strlen(a[k]), x = 0; n           while (x < (l - 1)) { n             if (a[k][x] == str[0] && a[k][x + 1] == str[1]) { n               found = 1; n               break; n             } else n               x++; n           } n           if (found == 1) break; n         } n         if (found == 0) { n           printf( %c%c n , str[0], str[1]); n           c = 3; n           break; n         } n       } n       if (c == 3) break; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n const int maxn = 2e5 + 5; n int n, q; n inline int lowbit(int x) { return x & (-x); } n long long bit[maxn]; n void add(int pos, long long v) { n   for (int i = pos; i <= n; i += lowbit(i)) bit[i] += v; n } n long long querybit(int pos) { n   long long ans = 0; n   for (int i = pos; i; i -= lowbit(i)) ans += bit[i]; n   return ans; n } n long long mv[maxn << 2], a[maxn]; n void updata(int rt) { mv[rt] = std::max(mv[rt << 1], mv[rt << 1 | 1]); } n void build(int l, int r, int rt) { n   if (l == r) { n     mv[rt] = a[l]; n     return; n   } n   int mid = (l + r) >> 1; n   build(l, mid, rt << 1); n   build(mid + 1, r, rt << 1 | 1); n   updata(rt); n } n void modify(int l, int r, int rt, int pos, long long v) { n   if (l == r) { n     mv[rt] = v; n     return; n   } n   int mid = (l + r) >> 1; n   if (pos <= mid) n     modify(l, mid, rt << 1, pos, v); n   else n     modify(mid + 1, r, rt << 1 | 1, pos, v); n   updata(rt); n } n int query(int l, int r, int rt, int pos, long long v) { n   if (mv[rt] < v || pos > n) return n + 1; n   if (l == r) return l; n   int mid = (l + r) >> 1; n   if (pos <= l) { n     if (mv[rt << 1] >= v) n       return query(l, mid, rt << 1, pos, v); n     else n       return query(mid + 1, r, rt << 1 | 1, pos, v); n   } else { n     if (pos <= mid) n       return std::min(query(l, mid, rt << 1, pos, v), n                       query(mid + 1, r, rt << 1 | 1, pos, v)); n     else n       return query(mid + 1, r, rt << 1 | 1, pos, v); n   } n } n int search(int pos) { n   if (pos > n) return -1; n   if (querybit(pos - 1) == a[pos]) return pos; n   return search(query(1, n, 1, pos + 1, querybit(pos))); n } n int main() { n   scanf( %d%d , &n, &q); n   for (int i = 1; i <= n; i++) scanf( %lld , &a[i]), add(i, a[i]); n   build(1, n, 1); n   while (q--) { n     int tp; n     long long tv; n     scanf( %d%lld , &tp, &tv); n     add(tp, tv - a[tp]); n     a[tp] = tv; n     modify(1, n, 1, tp, tv); n     printf( %d n , search(1)); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m; n vector<int> v[1005]; n int ma, src; n bool vis[1005]; n int p[1005]; n void dfs(int s, int pa, int d) { n   p[s] = pa; n   vis[s] = 1; n   if (d >= ma) { n     src = s; n     ma = d; n   } n   for (int i = 0; i < v[s].size(); i++) { n     int u = v[s][i]; n     if (u != pa) { n       dfs(u, s, d + 1); n     } n   } n } n int main() { n   int u = 0; n   ios::sync_with_stdio(0); n   cin >> n >> m; n   for (int i = 0; i < m; i++) { n     int x, y; n     cin >> x >> y; n     v[x].push_back(y); n     v[y].push_back(x); n   } n   int trees = 0, ans = 0; n   vector<int> vvv; n   vector<pair<int, int> > vv; n   for (int i = 1; i <= n; i++) { n     if (!vis[i]) { n       ma = 0; n       src = i; n       dfs(i, -1, 0); n       dfs(src, -1, 0); n       int node = src; n       for (int j = 0; j < ma / 2; j++) { n         node = p[node]; n       } n       vv.push_back({ma, node}); n       trees++; n       u = max(u, ma); n       vvv.push_back((ma + 1) / 2); n     } n   } n   sort(vvv.begin(), vvv.end()); n   sort(vv.rbegin(), vv.rend()); n   for (int i = vvv.size() - 1; i >= max(0, (int)(vvv.size() - 2)); i--) { n     ans += vvv[i]; n   } n   int mi = 2; n   if (vvv.size() >= 3) { n     if (vvv[vvv.size() - 3] < vvv[vvv.size() - 1]) { n       mi = 1; n     } n   } n   ans += min(trees - 1, mi); n   ans = max(ans, u); n   cout << ans << endl; n   for (int i = 0; i < vv.size() - 1; i++) { n     cout << vv[0].second <<     << vv[i + 1].second << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, tx, ty, ans, fa[2055]; n bool vis[2055]; n int find(const int i) { return fa[i] == i ? i : fa[i] = find(fa[i]); } n void merge(const int i, const int j) { fa[i] = j; } n int main() { n   scanf( %d , &n); n   for (int i = 1; i <= 2010; ++i) { n     fa[i] = i; n   } n   for (int i = 1; i <= n; ++i) { n     scanf( %d %d , &tx, &ty); n     ty += 1005; n     if (!vis[tx]) { n       ++ans; n       vis[tx] = true; n     } n     if (!vis[ty]) { n       ++ans; n       vis[ty] = true; n     } n     tx = find(tx); n     ty = find(ty); n     if (tx != ty) { n       --ans; n       merge(tx, ty); n     } n   } n   --ans; n   printf( %d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct yy { n   int id, val; n } f[8000]; n int a[8000], p[8000][2], d[8000]; n bool b[8000]; n bool cmp(yy x, yy y) { return x.val > y.val; } n int main() { n   int i, j, n, m, t, x; n   bool tm; n   scanf( %d%d , &n, &m); n   for (i = 1; i <= 2 * n; ++i) { n     scanf( %d , &f[i].val); n     f[i].id = i; n     a[i] = f[i].val; n   } n   sort(f + 1, f + 1 + 2 * n, cmp); n   for (i = 1; i <= m; ++i) { n     scanf( %d%d , &p[i][0], &p[i][1]); n     d[p[i][0]] = p[i][1]; n     d[p[i][1]] = p[i][0]; n   } n   scanf( %d , &t); n   if (t == 1) { n     for (i = 1; i <= m; ++i) { n       if (a[p[i][0]] > a[p[i][1]]) { n         printf( %d n , p[i][0]); n         fflush(stdout); n         b[p[i][0]] = 1; n       } else { n         printf( %d n , p[i][1]); n         fflush(stdout); n         b[p[i][1]] = 1; n       } n       scanf( %d , &x); n       b[x] = 1; n     } n     for (i = 1; i <= 2 * n; ++i) { n       if (!b[f[i].id]) { n         printf( %d n , f[i].id); n         fflush(stdout); n         b[f[i].id] = 1; n         scanf( %d , &x); n         b[x] = 1; n       } n     } n   } else { n     for (i = 1; i <= n; ++i) { n       scanf( %d , &x); n       b[x] = 1; n       if (d[x] && !b[d[x]]) { n         printf( %d n , d[x]); n         fflush(stdout); n         b[d[x]] = 1; n       } else { n         tm = 0; n         for (j = 1; j <= m; ++j) n           if (!b[p[j][0]]) { n             if (a[p[j][0]] > a[p[j][1]]) { n               printf( %d n , p[j][0]); n               fflush(stdout); n               b[p[j][0]] = 1; n               tm = 1; n               break; n             } else { n               printf( %d n , p[j][1]); n               fflush(stdout); n               b[p[j][1]] = 1; n               tm = 1; n               break; n             } n           } n         if (!tm) n           for (j = 1; j <= 2 * n; ++j) n             if (!b[f[j].id]) { n               printf( %d n , f[j].id); n               fflush(stdout); n               b[f[j].id] = 1; n               break; n             } n       } n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n using ll = long long; n using pii = pair<int, int>; n template <typename T> n using V = vector<T>; n const int N = 1e5 + 5; n bool taken[3 * N]; n int main() { n   ios::sync_with_stdio(false); n   cin.tie(nullptr); n   int tc; n   cin >> tc; n   while (tc--) { n     int n, m; n     cin >> n >> m; n     for (int i = 1; i <= 3 * n; ++i) taken[i] = false; n     vector<pii> edges(m); n     vector<int> matching; n     for (auto &e : edges) { n       cin >> e.first >> e.second; n     } n     int id = 0; n     for (auto &e : edges) { n       ++id; n       if (taken[e.first] or taken[e.second]) continue; n       matching.push_back(id); n       taken[e.first] = taken[e.second] = true; n     } n     if ((int)matching.size() >= n) { n       cout <<  Matching n ; n       for (int i = 0; i < n; ++i) { n         cout << matching[i] <<    ; n       } n     } else { n       cout <<  IndSet n ; n       for (int i = 1, cnt = 0; cnt < n; ++i) { n         if (!taken[i]) { n           cout << i <<    ; n           ++cnt; n         } n       } n     } n     cout <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int i, j, temp = 0; n   char x[5][5]; n   for (i = 0; i < 3; i++) gets(x[i]); n   for (i = 0; i < 3; i++) n     for (j = 0; j < 3; j++) n       if (x[i][j] != x[2 - i][2 - j]) temp = 1; n   if (temp == 1) n     printf( NO n ); n   else n     printf( YES n ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long a, b, x, n; n void input() { n   cin >> a >> b >> n >> x; n   return; n } n long long powmod(long long a, long long b) { n   if (b == 1) return a; n   if (b < 1) return 1; n   long long t = powmod(a, b / 2) % 1000000007; n   t = (t * t) % 1000000007; n   if (b % 2 == 1) { n     t = (t * a) % 1000000007; n   } n   return t; n } n void solve() { n   if (a == 1) { n     cout << (b * (n % 1000000007) + x) % 1000000007; n     return; n   } n   long long tmp = powmod(a, n); n   long long ans = ((tmp - 1) * powmod(a - 1, 1000000007 - 2)) % 1000000007; n   tmp = (tmp * x) % 1000000007; n   ans = (ans * b) % 1000000007; n   cout << (tmp + ans) % 1000000007; n   return; n } n void output() { return; } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   { n     input(); n     solve(); n     output(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   cin >> n; n   int sum = 0; n   for (int i = 0; i < n; i++) { n     int in; n     cin >> in; n     if (in < 0) in *= -1; n     sum += in; n   } n   cout << sum << endl; n } n


#include <bits/stdc++.h> n using namespace std; n long long m, n, q; n long long X; n long long f(long long a) { n   long long b = X - 2, c = X, k, ans = 1; n   while (b > 0) { n     long long p = 1, k = a; n     while (p * 2 <= b) { n       p *= 2; n       k = k * k % c; n     } n     ans = ans * k % c; n     b -= p; n   } n   return ans; n } n long long a[1005009], A; n long long p[1000509]; n int main() { n   cin >> n >> m >> A >> q; n   p[0] = 1; n   for (long long i = 1;; i++) { n     p[i] = p[i - 1] * A % q; n     if (p[i] == 1) { n       X = i; n       break; n     } n   } n   a[0] = 1; n   long long x = m, t = 1; n   for (long long i = 1; i <= n - 1; i++) { n     a[i] = (a[i - 1] * x * f(i)) % X; n     x--; n   } n   long long s = 0; n   for (long long i = 0; i <= n - 1; i++) { n     s += a[i]; n     a[i] = s; n   } n   for (long long i = n - 1; i >= 0; i--) cout << p[a[i] % X] <<    ; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 2e5 + 100; n int fen[maxn * 2], d[maxn], n, first[maxn], w[maxn]; n pair<int, int> p[maxn]; n int N = 1; n map<int, int> mp; n vector<int> v; n inline void upd(int first, int val) { n   for (int i = first; i < N; i += i & -i) fen[i] = max(fen[i], val); n } n inline int ask(int first) { n   int res = 0; n   for (int i = first; i > 0; i -= i & -i) res = max(res, fen[i]); n   return res; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin >> n; n   for (int(i) = (0); (i) < (n); ++(i)) cin >> p[i].first >> p[i].second; n   sort(p, p + n); n   for (int(i) = (0); (i) < (n); ++(i)) { n     first[i] = p[i].first, w[i] = p[i].second; n     v.push_back(first[i] + w[i]); n     v.push_back(first[i] - w[i]); n   } n   sort(v.begin(), v.end()); n   v.resize(unique(v.begin(), v.end()) - v.begin()); n   for (auto &(a) : (v)) mp[a] = N++; n   int ans = 1; n   for (int(i) = (0); (i) < (n); ++(i)) { n     d[i] = 1 + ask(mp[first[i] - w[i]]); n     upd(mp[first[i] + w[i]], d[i]); n     ans = max(ans, d[i]); n   } n   cout << ans << endl; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T, typename U> n inline void chkmax(T& x, U y) { n   if (x < y) x = y; n } n template <typename T, typename U> n inline void chkmin(T& x, U y) { n   if (y < x) x = y; n } n int n, p; n int l[100005], r[100005], len[100005]; n int main() { n   cin >> n >> p; n   for (int i = 0; i < n; i++) n     scanf( %d%d , l + i, r + i), len[i] = r[i] - l[i] + 1; n   l[n] = l[0], r[n] = r[0]; n   len[n] = len[0]; n   double ret = 0; n   for (int i = 0; i < n; i++) { n     int x = len[i] - r[i] / p + (l[i] - 1) / p; n     int y = len[i + 1] - r[i + 1] / p + (l[i + 1] - 1) / p; n     ret += 2000.0 * (1 - 1. * x / len[i] * y / len[i + 1]); n   } n   printf( %.10lf n , ret); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 200010, P = 998244353; n int n, k, num, ans, h[maxn], fact[maxn], inv[maxn]; n int qp(int x, int y) { n   int z = 1; n   for (; y; y >>= 1, x = 1LL * x * x % P) { n     if (y & 1) z = 1LL * z * x % P; n   } n   return z; n } n int C(int x, int y) { return 1LL * fact[x] * inv[y] % P * inv[x - y] % P; } n int main() { n   scanf( %d %d , &n, &k); n   for (int i = fact[0] = inv[0] = 1; i <= n; i++) { n     fact[i] = 1LL * i * fact[i - 1] % P; n     inv[i] = qp(fact[i], P - 2); n   } n   for (int i = 0; i < n; i++) { n     scanf( %d , &h[i]); n   } n   for (int i = 0; i < n; i++) { n     if (h[i] ^ h[(i + 1) % n]) num++; n   } n   for (int i = 0; i + i <= num; i++) { n     ans = (ans + 1LL * C(num, i) * C(num - i, i) % P * qp(k - 2, num - i - i)) % n           P; n   } n   ans = 1LL * qp(k, n - num) * ans % P; n   printf( %d n , 1LL * qp(2, P - 2) * (qp(k, n) - ans + P) % P); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <class T> n using min_heap = priority_queue<T, vector<T>, greater<T>>; n void setupIO(const string &PROB =   ) { n   ios::sync_with_stdio(false); n   cin.tie(nullptr); n   if (PROB.length() != 0) { n     ifstream infile(PROB +  .in ); n     if (infile.good()) { n       freopen((PROB +  .in ).c_str(),  r , stdin); n       freopen((PROB +  .out ).c_str(),  w , stdout); n     } n   } n } n int main() { n   setupIO(); n   int n; n   cin >> n; n   int A[n][n]; n   for (int i = 0; i < (n); i++) { n     for (int j = 0; j < (n); j++) { n       char c; n       cin >> c; n       A[i][j] = c ==  1 ; n     } n   } n   int B[n]; n   for (int i = 0; i < (n); i++) cin >> B[i]; n   bool C[n]; n   for (int i = 0; i < n; i++) C[i] = false; n   ; n   while (true) { n     int tgt = -1; n     for (int i = 0; i < (n); i++) { n       if (B[i] == 0) { n         tgt = i; n         break; n       } n     } n     if (tgt == -1) break; n     assert(!C[tgt]); n     for (int i = 0; i < (n); i++) { n       if (A[tgt][i]) B[i]--; n     } n     C[tgt] = true; n   } n   vector<int> ans; n   for (int i = 0; i < (n); i++) n     if (C[i]) ans.push_back(i); n   cout << ans.size() << endl; n   for (int i = 0; i < (ans.size()); i++) { n     if (i != 0) cout <<    ; n     cout << ans[i] + 1; n   } n   cout << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct yo { n   int val; n   int id; n   int next, prev; n }; n vector<yo> a0, a; n int n, n0, x; n bool operator<(yo a, yo b) { return a.val < b.val; } n int main() { n   cin >> n; n   n0 = n; n   if (n <= 2) { n     cout << 0; n     return 0; n   } n   for (int i = 0; i < n; i++) { n     cin >> x; n     yo cur; n     cur.val = x; n     cur.id = i; n     cur.next = i + 1; n     cur.prev = i - 1; n     a0.push_back(cur); n     a.push_back(cur); n   } n   sort(a.begin(), a.end()); n   long long v = 0, ans = 0; n   for (int i = 0; i < a.size() - 2; i++) { n     int cur_id = a[i].id; n     if (a0[cur_id].prev == -1) { n       a0[a0[cur_id].next].prev = -1; n       ans += (n - 2) * (a[i].val - v); n       v += (a[i].val - v); n     } else if (a0[cur_id].next == n0) { n       a0[a0[cur_id].prev].next = n0; n       ans += (n - 2) * (a[i].val - v); n       v += (a[i].val - v); n     } else { n       ans += min(a0[a0[cur_id].prev].val, a0[a0[cur_id].next].val) - v; n       a0[a0[cur_id].next].prev = a0[cur_id].prev; n       a0[a0[cur_id].prev].next = a0[cur_id].next; n     } n     n--; n   } n   cout << ans; n } n


#include <bits/stdc++.h> n using namespace std; n vector<pair<int, int> > changes; n vector<int> norm(vector<int> x) { n   int i = -1; n   for (int bit = 30; bit >= 0; bit--) { n     for (int pos = i + 1; pos < x.size(); pos++) n       if (x[pos] & (1 << bit)) { n         if (pos != ++i) { n           changes.push_back(pair<int, int>(pos, i)); n           x[pos] ^= x[i]; n           changes.push_back(pair<int, int>(i, pos)); n           x[i] ^= x[pos]; n         } n         for (int pos = 0; pos < x.size(); pos++) n           if (pos != i) { n             if (x[pos] & (1 << bit)) { n               changes.push_back(pair<int, int>(pos, i)); n               x[pos] ^= x[i]; n             } n           } n         break; n       } n   } n   return x; n } n int used[33]; n int main() { n   int n; n   cin >> n; n   vector<int> x(n), y(n); n   for (int i = 0; i < n; i++) scanf( %d , &x[i]); n   for (int i = 0; i < n; i++) scanf( %d , &y[i]); n   vector<pair<int, int> > a, b; n   vector<int> bx = norm(x); n   a = changes; n   changes.clear(); n   vector<int> by = norm(y); n   b = changes; n   reverse(b.begin(), b.end()); n   for (int i = 0; i < n; i++) n     if (by[i]) used[__builtin_clz(by[i])] = 1; n   for (int i = 0; i < n; i++) n     if (bx[i] && used[__builtin_clz(bx[i])]) { n       for (int j = i - 1; j >= 0; j--) { n         if (bx[j] && used[__builtin_clz(bx[j])]) break; n         a.push_back(pair<int, int>(j, j + 1)); n         bx[j + 1] ^= bx[j]; n         a.push_back(pair<int, int>(j + 1, j)); n         bx[j] ^= bx[j + 1]; n         a.push_back(pair<int, int>(j, j + 1)); n         bx[j + 1] ^= bx[j]; n       } n     } n   for (int i = 0; i < n; i++) { n     if (by[i] != bx[i]) { n       if (!by[i]) { n         bx[i] = 0; n         a.push_back(pair<int, int>(i, i)); n       } else { n         for (int j = i + 1; j < n; j++) n           if (bx[j] && (by[i] & (1 << (31 - __builtin_clz(bx[j]))))) { n             bx[i] ^= bx[j]; n             a.push_back(pair<int, int>(i, j)); n           } n         if (by[i] != bx[i]) { n           cout << -1 << endl; n           return 0; n         } n       } n     } n   } n   vector<int> aux = x; n   cout << a.size() + b.size() << endl; n   for (int i = 0; i < a.size(); i++) { n     cout << a[i].first + 1 <<     << a[i].second + 1 << endl; n     aux[a[i].first] ^= aux[a[i].second]; n   } n   for (int i = 0; i < b.size(); i++) { n     cout << b[i].first + 1 <<     << b[i].second + 1 << endl; n     aux[b[i].first] ^= aux[b[i].second]; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 5e5 + 5; n int size[maxn], dep[maxn], val[maxn], dfn[maxn], hson[maxn], ans[maxn], n     rnk[maxn]; n int cnt[(1 << 22) + 4]; n struct EDGE { n   int to, next; n   char c; n } edge[maxn << 1]; n int head[maxn], tot, num; n void addedge(int u, int v, char c) { n   edge[++tot].next = head[u]; n   edge[tot].to = v; n   edge[tot].c = c; n   head[u] = tot; n } n void dfs(int now, int pre) { n   size[now] = 1; n   dep[now] = dep[pre] + 1; n   dfn[now] = ++num; n   rnk[num] = now; n   for (int i = head[now]; i; i = edge[i].next) { n     int to = edge[i].to; n     if (to == pre) continue; n     val[to] = val[now] ^ (1 << (edge[i].c -  a )); n     dfs(to, now); n     size[now] += size[to]; n     if (size[to] > size[hson[now]]) hson[now] = to; n   } n } n void cal(int now) { n   if (cnt[val[now]]) ans[now] = max(ans[now], cnt[val[now]] - dep[now]); n   for (int i = 0; i < 22; ++i) { n     if (cnt[val[now] ^ (1 << i)]) n       ans[now] = max(ans[now], cnt[val[now] ^ (1 << i)] - dep[now]); n   } n   cnt[val[now]] = max(cnt[val[now]], dep[now]); n   for (int i = head[now]; i; i = edge[i].next) { n     int to = edge[i].to; n     if (dep[to] < dep[now] || to == hson[now]) continue; n     for (int j = dfn[to]; j <= dfn[to] + size[to] - 1; ++j) { n       int tmp = rnk[j]; n       if (cnt[val[tmp]]) n         ans[now] = max(ans[now], cnt[val[tmp]] + dep[tmp] - 2 * dep[now]); n       for (int i = 0; i < 22; ++i) { n         if (cnt[val[tmp] ^ (1 << i)]) n           ans[now] = n               max(ans[now], cnt[val[tmp] ^ (1 << i)] + dep[tmp] - 2 * dep[now]); n       } n     } n     for (int j = dfn[to]; j <= dfn[to] + size[to] - 1; ++j) { n       int tmp = rnk[j]; n       cnt[val[tmp]] = max(cnt[val[tmp]], dep[tmp]); n     } n   } n } n void dfs2(int now, bool heave) { n   for (int i = head[now]; i; i = edge[i].next) { n     int to = edge[i].to; n     if (dep[to] < dep[now] || to == hson[now]) continue; n     dfs2(to, 0); n     ans[now] = max(ans[now], ans[to]); n   } n   if (hson[now]) dfs2(hson[now], 1), ans[now] = max(ans[now], ans[hson[now]]); n   cal(now); n   if (!heave) n     for (int i = dfn[now]; i <= dfn[now] + size[now] - 1; ++i) n       cnt[val[rnk[i]]] = 0; n } n int main() { n   int n; n   scanf( %d , &n); n   for (int i = 2; i <= n; ++i) { n     int to; n     char c; n     scanf( %d %c , &to, &c); n     addedge(i, to, c); n     addedge(to, i, c); n   } n   dfs(1, 0); n   dfs2(1, 0); n   for (int i = 1; i <= n; ++i) printf( %d  , ans[i]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   vector<long long int> v1; n   map<long long int, long long int> m1; n   long long int size, count = 0, i, j, s, k; n   cin >> size; n   long long int a[size]; n   for (i = 0; i < size; i++) { n     cin >> a[i]; n   } n   sort(a, a + size); n   for (i = 0; i < size; i++) { n     for (j = i + 1; j < size; j++) { n       k = a[i] + a[j]; n       m1[k]++; n     } n   } n   map<long long int, long long int>::iterator itr; n   for (itr = m1.begin(); itr != m1.end(); itr++) { n     v1.push_back(itr->second); n   } n   sort(v1.begin(), v1.end(), greater<long long int>()); n   cout << v1[0]; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 110000, MAXM = MAXN * 2; n const int inf = 10000000; n struct eglist { n   int other[MAXM], succ[MAXM], last[MAXM], sum; n   void addEdge(int a, int b) { n     other[++sum] = b, succ[sum] = last[a], last[a] = sum; n     other[++sum] = a, succ[sum] = last[b], last[b] = sum; n   } n   void clear(int n) { n     for (int i = 0; i <= n; i++) last[i] = 0; n     sum = 0; n   } n } e; n int fa[MAXN]; n pair<int, int> down1[MAXN], down2[MAXN]; n int up[MAXN], ans = 0; n queue<int> q; n int visit[MAXN] = {}; n int n, m, d; n void dfsDown(int x, int fa) { n   if (visit[x]) { n     down1[x] = make_pair(0, 0); n     down2[x] = make_pair(0, 0); n   } else { n     down1[x].first = -inf, down2[x].first = -inf; n   } n   for (int i = e.last[x]; i; i = e.succ[i]) { n     int y = e.other[i]; n     if (y == fa) continue; n     dfsDown(y, x); n     pair<int, int> tmp = down1[y]; n     tmp.first++; n     tmp.second = y; n     if (tmp > down1[x]) { n       down2[x] = down1[x]; n       down1[x] = tmp; n     } else if (tmp > down2[x]) n       down2[x] = tmp; n   } n } n void dfsUp(int x, int fa) { n   up[x] = visit[x] ? 0 : -inf; n   if (fa == 0) { n     if (down1[x].first <= d) ans++; n   } else { n     up[x] = max(up[x], up[fa] + 1); n     if (down1[fa].second == x) n       up[x] = max(up[x], down2[fa].first + 1); n     else n       up[x] = max(up[x], down1[fa].first + 1); n     if (max(up[x], down1[x].first) <= d) ans++; n   } n   for (int i = e.last[x]; i; i = e.succ[i]) { n     int y = e.other[i]; n     if (y == fa) continue; n     dfsUp(y, x); n   } n } n int main() { n   scanf( %d %d %d , &n, &m, &d); n   for (int i = 1; i <= m; i++) { n     int p; n     scanf( %d , &p); n     visit[p] = 1; n   } n   e.clear(n); n   for (int i = 1; i < n; i++) { n     int a, b; n     scanf( %d %d , &a, &b); n     e.addEdge(a, b); n   } n   dfsDown(1, 0); n   dfsUp(1, 0); n   printf( %d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int mx(int a, int b) { n   if (a > b) return a; n   return b; n } n int main() { n   int n, s; n   scanf( %d %d , &n, &s); n   int max = -1; n   for (int i = 0; i < n; ++i) { n     int x, y; n     scanf( %d %d , &x, &y); n     if (x >= s && !(x == s && y == 0)) continue; n     if (y == 0) y = 100; n     max = mx(max, 100 - y); n   } n   printf( %d , max); n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T1, typename T2> n istream& operator>>(istream& in, pair<T1, T2>& a) { n   in >> a.first >> a.second; n   return in; n } n template <typename T1, typename T2> n ostream& operator<<(ostream& out, pair<T1, T2> a) { n   out << a.first <<     << a.second; n   return out; n } n template <typename T, typename T1> n T amax(T& a, T1 b) { n   if (b > a) a = b; n   return a; n } n template <typename T, typename T1> n T amin(T& a, T1 b) { n   if (b < a) a = b; n   return a; n } n const long long INF = 1e18; n const int32_t M = 1e9 + 7; n const int32_t MM = 998244353; n const long long N = 5e5 + 5; n vector<long long> v[N]; n void solve() { n   long long n; n   cin >> n; n   v[0].clear(); n   for (long long i = 1; i < n + 1; i++) { n     v[i].clear(); n     v[i].push_back(-1); n   } n   for (long long i = 0; i < n; i++) { n     long long x; n     cin >> x; n     v[x].push_back(i); n   } n   if ((long long)((v[0]).size()) < 2) { n     cout <<  0 n ; n     return; n   } n   long long z = (long long)((v[0]).size()); n   long long L = v[0][(z + 1) / 2], R = v[0][z / 2 - 1]; n   vector<pair<long long, long long> > vec; n   for (long long i = 1; i < n + 1; i++) { n     v[i].push_back(n + 2); n     long long l = *prev(lower_bound((v[i]).begin(), (v[i]).end(), L)); n     long long r = *upper_bound((v[i]).begin(), (v[i]).end(), R); n     if (l != -1 || r != n + 2) vec.push_back({l, r}); n   } n   for (long long x : v[0]) { n     if (x < L) vec.push_back({x, -1}); n   } n   sort((vec).begin(), (vec).end(), n        [&](pair<long long, long long> a, pair<long long, long long> b) { n          return a.first > b.first; n        }); n   long long ans = 0; n   multiset<long long> s; n   vector<long long> vis(n + 4); n   for (auto x : vec) { n     if (x.second == -1) { n       if ((long long)((s).size())) { n         long long r = -*s.begin(); n         s.erase(s.begin()); n         vis[r] = 1; n         ans++; n       } n     } else { n       s.insert(-x.second); n     } n   } n   vector<long long> vec2; n   for (auto x : vec) { n     if (x.second != -1 && !vis[x.second]) vec2.push_back(x.second); n   } n   for (long long x : v[0]) { n     if (x > R) vec2.push_back(x), vis[x] = 1; n   } n   s.clear(); n   sort((vec2).begin(), (vec2).end()); n   for (long long x : vec2) { n     if (vis[x]) { n       if ((long long)((s).size())) { n         s.erase(s.begin()); n         ans++; n       } n     } else { n       s.insert(x); n     } n   } n   amin(ans, z / 2); n   cout << ans <<   n ; n } n signed main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   long long t = 1; n   cin >> t; n   while (t--) solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int mod = 1e9 + 7; n const int N = 1e5 + 5; n int x[N], y[N], n, ans = -1, vis[N]; n vector<int> gx[N], gy[N]; n void dfs(int k) { n   if (vis[k]) return; n   vis[k] = 1; n   for (auto it : gx[x[k]]) dfs(it); n   for (auto it : gy[y[k]]) dfs(it); n } n int main() { n   ios_base::sync_with_stdio(0); n   cin >> n; n   for (int i = 1; i <= n; i++) { n     cin >> x[i] >> y[i]; n     gx[x[i]].push_back(i); n     gy[y[i]].push_back(i); n   } n   for (int i = 1; i <= n; i++) { n     if (!vis[i]) { n       ans++; n       dfs(i); n     } n   } n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n string s; n int main() { n   cin >> s; n   int len = s.length(); n   if (s[0] >=  a  && s[0] <=  z ) s[0] -= 32; n   cout << s << endl; n   return 0; n } n


#include <bits/stdc++.h> n int main() { n   int n, m; n   while (~scanf( %d%d , &n, &m)) { n     int p = 0; n     while (n <= m) { n       n = n * 3; n       m = m * 2; n       p++; n     } n     printf( %d n , p); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct node { n   char word[210]; n   int l, r; n } a[20]; n char str[210]; n int n; n bool check(int l, int r) { n   int i, j, k, temp, len; n   for (i = 1; i <= n; i++) { n     len = strlen(a[i].word); n     temp = 0; n     if (len < (r - l + 1)) n       temp = 0; n     else { n       for (j = 0; j <= len - (r - l + 1); j++) { n         for (k = 0; k <= (r - l); k++) { n           if (str[l + k] != a[i].word[j + k]) break; n         } n         if (k > (r - l)) temp++; n       } n     } n     if (temp < a[i].l || temp > a[i].r) return false; n   } n   return true; n } n int main() { n   int i, l, j, k, t, flag = 0, ans = 0; n   scanf( %s , str); n   l = strlen(str); n   scanf( %d , &n); n   for (i = 1; i <= n; i++) scanf( %s%d%d , a[i].word, &a[i].l, &a[i].r); n   for (i = 0; i < l; i++) { n     for (j = i; j < l; j++) { n       flag = 0; n       for (k = 0; k < i; k++) { n         for (t = 0; t <= (j - i); t++) { n           if (str[i + t] != str[k + t]) break; n         } n         if (t > (j - i)) { n           flag = 1; n           break; n         } n       } n       if (flag == 1) continue; n       if (check(i, j)) ans++; n     } n   } n   printf( %d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long double x, y, z; n long double a[12]; n int ans = 0; n const char s[12][10] = { x^y^z ,  x^z^y ,  (x^y)^z ,  (x^z)^y , n                          y^x^z ,  y^z^x ,  (y^x)^z ,  (y^z)^x , n                          z^x^y ,  z^y^x ,  (z^x)^y ,  (z^y)^x }; n int main() { n   cin >> x >> y >> z; n   a[0] = pow(y, z) * log(x); n   a[1] = pow(z, y) * log(x); n   a[2] = a[3] = y * z * log(x); n   a[4] = pow(x, z) * log(y); n   a[5] = pow(z, x) * log(y); n   a[6] = a[7] = x * z * log(y); n   a[8] = pow(x, y) * log(z); n   a[9] = pow(y, x) * log(z); n   a[10] = a[11] = x * y * log(z); n   for (int i = 1; i <= 11; i++) { n     if (a[i] > a[ans]) ans = i; n   } n   printf( %s n , s[ans]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n namespace io { n const int __SIZE = (1 << 21) + 1; n char ibuf[__SIZE], *iS, *iT, obuf[__SIZE], *oS = obuf, *oT = oS + __SIZE - 1, n                                            __c, qu[55]; n int __f, qr, _eof; n inline void flush() { fwrite(obuf, 1, oS - obuf, stdout), oS = obuf; } n inline void gc(char& x) { n   x = (iS == iT ? (iT = (iS = ibuf) + fread(ibuf, 1, __SIZE, stdin), n                    (iS == iT ? EOF : *iS++)) n                 : *iS++); n } n inline void pc(char x) { n   *oS++ = x; n   if (oS == oT) flush(); n } n inline void pstr(const char* s) { n   int __len = strlen(s); n   for (__f = 0; __f < __len; ++__f) pc(s[__f]); n } n inline void gstr(char* s) { n   for (__c = (iS == iT ? (iT = (iS = ibuf) + fread(ibuf, 1, __SIZE, stdin), n                          (iS == iT ? EOF : *iS++)) n                        : *iS++); n        __c < 32 || __c > 126 || __c ==    ;) n     __c = (iS == iT ? (iT = (iS = ibuf) + fread(ibuf, 1, __SIZE, stdin), n                        (iS == iT ? EOF : *iS++)) n                     : *iS++); n   for (; __c > 31 && __c < 127 && __c !=    ; n        ++s, __c = (iS == iT ? (iT = (iS = ibuf) + fread(ibuf, 1, __SIZE, stdin), n                               (iS == iT ? EOF : *iS++)) n                             : *iS++)) n     *s = __c; n   *s = 0; n } n template <class I> n inline bool gi(I& x) { n   _eof = 0; n   for (__f = 1, n       __c = (iS == iT ? (iT = (iS = ibuf) + fread(ibuf, 1, __SIZE, stdin), n                         (iS == iT ? EOF : *iS++)) n                       : *iS++); n        (__c <  0  || __c >  9 ) && !_eof; n        __c = (iS == iT ? (iT = (iS = ibuf) + fread(ibuf, 1, __SIZE, stdin), n                          (iS == iT ? EOF : *iS++)) n                        : *iS++)) { n     if (__c ==  - ) __f = -1; n     _eof |= __c == EOF; n   } n   for (x = 0; __c <=  9  && __c >=  0  && !_eof; n        __c = (iS == iT ? (iT = (iS = ibuf) + fread(ibuf, 1, __SIZE, stdin), n                          (iS == iT ? EOF : *iS++)) n                        : *iS++)) n     x = x * 10 + (__c & 15), _eof |= __c == EOF; n   x *= __f; n   return !_eof; n } n template <class I> n inline void print(I x) { n   if (!x) pc( 0 ); n   if (x < 0) pc( - ), x = -x; n   while (x) qu[++qr] = x % 10 +  0 , x /= 10; n   while (qr) pc(qu[qr--]); n } n struct Flusher_ { n   ~Flusher_() { flush(); } n } io_flusher_; n }  // namespace io n using io::gc; n using io::gi; n using io::gstr; n using io::pc; n using io::print; n using io::pstr; n using ll = long long; n using pii = pair<int, int>; n const int MOD = 1000000007; n ll c[500005]; n int p2[500005]; n map<ll, vector<pii>> NO; n int bcj[500006], ltk; n void binit() { memset(bcj, -1, sizeof bcj); } n int bget(int i) { return (bcj[i] < 0) ? i : (bcj[i] = bget(bcj[i])); } n void bmerge(int i, int j) { n   int ri = bget(i), rj = bget(j); n   if (ri == rj) { n     return; n   } n   ltk--; n   if (bcj[ri] > bcj[rj]) { n     swap(ri, rj); n   } n   bcj[ri] += bcj[rj]; n   bcj[rj] = ri; n } n signed main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   int n, m, k; n   gi(n), gi(m), gi(k); n   for (int i = (1); i < ((n) + 1); i++) gi(c[i]); n   p2[0] = 1; n   for (int i = (1); i < ((n) + 1); i++) { n     p2[i] = 2 * p2[i - 1]; n     if (p2[i] >= MOD) p2[i] -= MOD; n   } n   while (m--) { n     int u, v; n     gi(u), gi(v); n     auto& ref = NO[c[u] ^ c[v]]; n     ref.push_back({u, v}); n   } n   ll notinno = (1ll << k) - NO.size(); n   ll ans = 1ll * (notinno % MOD) * p2[n] % MOD; n   binit(); n   for (auto& k : NO) { n     ltk = n; n     for (auto& i : k.second) bmerge(i.first, i.second); n     ans += p2[ltk]; n     for (auto& i : k.second) bcj[i.first] = bcj[i.second] = -1; n     if (ans >= MOD) ans -= MOD; n   } n   print(ans); n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 5e3 + 5; n int N, Q; n int dp[MAXN][MAXN]; n int pre[MAXN][MAXN]; n int A[MAXN]; n int f(int l, int r) { n   if (dp[l][r] != -1) { n     return dp[l][r]; n   } n   if (l == r - 1) { n     dp[l][r] = A[l]; n   } else { n     dp[l][r] = f(l, r - 1) ^ f(l + 1, r); n   } n   return dp[l][r]; n } n int pref(int l, int r) { n   if (pre[l][r] != -1) { n     return pre[l][r]; n   } n   pre[l][r] = dp[l][r]; n   if (l != r - 1) { n     pre[l][r] = max(pre[l][r], pref(l + 1, r)); n     pre[l][r] = max(pref(l, r - 1), pre[l][r]); n   } n   return pre[l][r]; n } n int main() { n   cin >> N; n   for (int i = 0; i < N; i++) { n     cin >> A[i]; n   } n   for (int i = 0; i < MAXN; i++) { n     for (int j = 0; j < MAXN; j++) { n       dp[i][j] = -1; n       pre[i][j] = -1; n     } n   } n   for (int i = 0; i < N; i++) { n     for (int j = i + 1; j <= N; j++) { n       f(i, j); n     } n   } n   for (int i = 0; i < N; i++) { n     for (int j = i + 1; j <= N; j++) { n       pref(i, j); n     } n   } n   cin >> Q; n   for (int i = 0; i < Q; i++) { n     int a, b; n     cin >> a >> b; n     cout << pre[a - 1][b] << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int t; n   cin >> t; n   while (t-- > 0) { n     int n, x; n     cin >> n >> x; n     cout << 2 * x << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long int inf = LLONG_MAX; n const long long int mod = 1e9 + 7; n const long long int N = 1e2 + 1; n long long int a[N][N]; n long long int n; n int32_t main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   cin >> n; n   for (long long int i = 0; i < n; ++i) { n     for (long long int j = 0; j < n; ++j) { n       cin >> a[i][j]; n     } n   } n   vector<long long int> good; n   for (long long int i = 0; i < n; ++i) { n     bool g = true; n     for (long long int j = 0; j < n; ++j) { n       if (a[i][j] == 1 or a[i][j] == 3) { n         g = false; n       } n     } n     if (g) good.push_back(i); n   } n   cout << good.size() << endl; n   for (auto &i : good) { n     cout << i + 1 <<    ; n   } n   cout << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n stack<pair<int, int> > stc; n int main() { n   ios::sync_with_stdio(false); n   int n, men, i, cur_max, max; n   cin >> n; n   cin >> men; n   stc.push(make_pair(men, 0)); n   max = 0; n   for (i = 0; i < n - 1; i++) { n     cin >> men; n     if (stc.top().first > men) { n       stc.push(make_pair(men, 1)); n       if (max == 0) max = 1; n       continue; n     } n     cur_max = 0; n     while (true) { n       if (stc.top().first > men) { n         if (stc.top().second > cur_max || stc.top().second == 0) { n           stc.push(make_pair(men, cur_max + 1)); n           if (max < cur_max + 1) max = cur_max + 1; n           break; n         } n         stc.pop(); n       } else { n         if (stc.top().second == 0) { n           stc.push(make_pair(men, 0)); n           break; n         } n         if (stc.top().second > cur_max) cur_max = stc.top().second; n         if (max < cur_max) max = cur_max; n         stc.pop(); n       } n     } n   } n   cout << max << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 3e5 + 2; n int dp[N][21], ar[N], lis[21]; n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   int n, i, j, k, l, q; n   cin >> n >> q; n   for (i = 1; i <= n; i++) { n     cin >> ar[i]; n     for (j = 0; j <= 19; j++) { n       if (((1 << j) & ar[i])) { n         dp[i][j] = i; n         if (!lis[j]) { n           lis[j] = i; n         } else { n           for (k = 0; k <= 19; k++) { n             dp[i][k] = max(dp[i][k], dp[lis[j]][k]); n           } n           lis[j] = i; n         } n       } n     } n   } n   for (i = 1; i <= q; i++) { n     cin >> j >> k; n     for (l = 0; l <= 19; l++) { n       if (((1 << l) & ar[j])) { n         if (dp[k][l] >= j) { n           cout <<  Shi n ; n           break; n         } n       } n       if (l == 19) { n         cout <<  Fou n ; n       } n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n void __print(int x) { cerr << x; } n void __print(long x) { cerr << x; } n void __print(long long x) { cerr << x; } n void __print(unsigned x) { cerr << x; } n void __print(unsigned long x) { cerr << x; } n void __print(unsigned long long x) { cerr << x; } n void __print(float x) { cerr << x; } n void __print(double x) { cerr << x; } n void __print(long double x) { cerr << x; } n void __print(char x) { cerr <<      << x <<     ; } n void __print(const char *x) { cerr <<     << x <<    ; } n void __print(const string &x) { cerr <<     << x <<    ; } n void __print(bool x) { cerr << (x ?  true  :  false ); } n template <typename T, typename V> n void __print(const pair<T, V> &x) { n   cerr <<  { ; n   __print(x.first); n   cerr <<  , ; n   __print(x.second); n   cerr <<  } ; n } n template <typename T> n void __print(const T &x) { n   int f = 0; n   cerr <<  { ; n   for (auto &i : x) cerr << (f++ ?  ,  :   ), __print(i); n   cerr <<  } ; n } n void _print() { cerr <<  ] n ; } n template <typename T, typename... V> n void _print(T t, V... v) { n   __print(t); n   if (sizeof...(v)) cerr <<  ,  ; n   _print(v...); n } n void pre() {} n long long a[200009]; n long long n, p, k; n long long dp[200009]; n void solve() { n   cin >> n >> p >> k; n   for (int i = 0; i < (n); ++i) cin >> a[i + 1], dp[i] = -1; n   sort(a + 1, a + n + 1); n   dp[0] = 0; n   long long ans = 0; n   for (int i = 1; i <= (n); ++i) { n     if (i >= k) n       dp[i] = min(dp[i - 1], dp[i - k]) + a[i]; n     else n       dp[i] = dp[i - 1] + a[i]; n     if (dp[i] <= p) ans = i; n   } n   cout << ans <<   n ; n } n int main() { n   cin.sync_with_stdio(0); n   cin.tie(0); n   cin.exceptions(cin.failbit); n   pre(); n   int t; n   cin >> t; n   for (int i = 0; i < (t); ++i) solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n string generateString(int a) { n   string s =   ; n   for (int i = 0; i < a; i++) { n     char x =  a  + i; n     s = s + x; n   } n   return s; n } n string wrapAround(string s, int b, int c) { n   int x = c - b; n   for (int i = 0; i < x; i++) { n     s += s[i % b]; n   } n   return s; n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   int t; n   cin >> t; n   while (t--) { n     int n; n     cin >> n; n     int A[n]; n     for (int i = 0; i < n; i++) { n       cin >> A[i]; n     } n     vector<vector<int> > v; n     for (int i = 0; i < 26; i++) { n       vector<int> x(n + 1, 0); n       v.push_back(x); n     } n     for (int j = 0; j < n; j++) { n       v[A[j] - 1][j + 1]++; n     } n     for (int i = 0; i < 26; i++) { n       for (int j = 1; j < n + 1; j++) { n         v[i][j] += v[i][j - 1]; n       } n     } n     vector<vector<int> > pos; n     pos.resize(26); n     for (int i = 0; i < n; i++) { n       pos[A[i] - 1].push_back(i); n     } n     int ans = 1; n     for (int i = 0; i < 26; i++) { n       int sz = pos[i].size(); n       int lim = sz / 2; n       if (sz != 0) { n         for (int k = 0; k < lim; k++) { n           int st = pos[i][k]; n           int en = pos[i][sz - k - 1]; n           int temp = 0; n           for (int j = 0; j < 26; j++) { n             int x = v[j][en] - v[j][st + 1]; n             temp = max(x, temp); n           } n           ans = max(ans, 2 * (k + 1) + temp); n         } n       } n     } n     cout << ans <<   n ; n   } n } n


#include <bits/stdc++.h> n #pragma comment(linker,  /STACK:256000000 ) n using namespace std; n int n, k; n int a[200000]; n bool ok() { n   int cur = a[k - 1]; n   for (int i = k; i < n; ++i) { n     if (a[i] != cur) return false; n   } n   return true; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin >> n >> k; n   for (int i = 0; i < (int)(n); i++) cin >> a[i]; n   if (!ok()) n     cout << -1 << endl; n   else { n     int ans = 0; n     int cur = a[k - 1]; n     for (int i = 0; i < (int)(k); i++) { n       if (a[i] != cur) ans = i + 1; n     } n     cout << ans << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long a[1000005] = {1, 1}, b[1000005], num; n long long x2, ans = 100000000; n void init() { n   long long i, j; n   for (i = 2; i <= 1000000; i++) { n     if (a[i]) continue; n     for (j = i * i; j <= 1000000; j += i) a[j] = 1; n   } n   for (i = 1; i <= 1000000; i++) n     if (a[i] == 0) b[++num] = i; n } n int main(void) { n   init(); n   scanf( %lld , &x2); n   long long x = x2, p = 0; n   for (long long i = 1; b[i] * b[i] <= x; i++) n     while (x % b[i] == 0) x /= b[i], p = b[i]; n   if (x != 1) p = x; n   for (long long i = x2 - p + 1; i <= x2; i++) { n     long long tmp = i; n     long long pp = p; n     for (long long j = 1; b[j] * b[j] <= tmp; j++) n       while (tmp % b[j] == 0) tmp /= b[j], pp = b[j]; n     if (tmp != 1) pp = tmp; n     if (pp == i) continue; n     ans = min(ans, i - pp + 1); n   } n   printf( %lld n , ans); n   return 0; n } n


#include <bits/stdc++.h> n namespace { n using namespace std; n template <typename T> n inline bool whitespace(const vector<T> &) { n   return false; n } n template <typename T> n inline bool whitespace(const T &) { n   return true; n } n inline bool whitespace(const char) { return false; } n template <typename S, typename T> n ostream &operator<<(ostream &os, const pair<S, T> &p) { n   os << p.first << (whitespace(p.second) ?     :   ) << p.second <<   n ; n   return os; n } n template <typename S, typename T> n istream &operator>>(istream &is, pair<S, T> &p) { n   is >> p.first >> p.second; n   return is; n } n template <typename T> n istream &operator>>(istream &is, vector<T> &v) { n   for (T &t : v) is >> t; n   return is; n } n template <typename T> n ostream &operator<<(ostream &os, const vector<T> &v) { n   for (const T &t : v) { n     os << t << (whitespace(t) ?     :   ); n   } n   os <<   n ; n   return os; n } n template <typename T> n istream &operator>>(istream &is, deque<T> &v) { n   for (T &t : v) is >> t; n   return is; n } n template <typename T> n ostream &operator<<(ostream &os, const deque<T> &v) { n   for (const T &t : v) { n     os << t << (whitespace(t) ?     :   ); n   } n   os <<   n ; n   return os; n } n template <typename T> n T gcd(T a, T b) { n   while (b) { n     T mod = a % b; n     a = b; n     b = mod; n   } n   return a; n } n template <typename T> n T sum(const vector<T> &v) { n   T s = 0; n   for (T value : v) s += value; n   return s; n } n template <typename T> n vector<T> prefix_sum(const vector<T> &v) { n   vector<T> res(v.size() + 1); n   res[0] = 0; n   for (long long i = 0; i < v.size(); ++i) res[i + 1] = res[i] + v[i]; n   return res; n } n template <typename S, typename T> n S min(S a, T b) { n   return a < (S)b ? a : b; n } n template <typename S, typename T> n S max(S a, T b) { n   return a < (S)b ? b : a; n } n class exit_exception : exception {}; n istream &in = cin; n ostream &out = cout; n template <typename T> n void answer(T ans) { n   out << ans <<   n ; n   throw exit_exception(); n } n struct autoint { n   long long x; n   autoint() { in >> x; } n   operator long long() { return x; } n   autoint(autoint &i) { x = i.x; } n   long long operator--() { return --x; } n   long long operator--(signed) { return x--; } n   long long operator++() { return ++x; } n   long long operator++(signed) { return x++; } n }; n template <typename T> n struct autoread : T { n   template <typename... Args> n   autoread(Args &&...args) : T(args...) { n     in >> *dynamic_cast<T *>(this); n   } n }; n }  // namespace n void solve(); n signed main() { n   cin.tie(nullptr); n   cout.tie(nullptr); n   ios::sync_with_stdio(false); n   long long t = 1; n   cin >> t; n   do { n     try { n       solve(); n     } catch (exit_exception &) { n     } n   } while (--t); n   return 0; n } n void solve() { n   autoint n, k; n   string s; n   cin >> s; n   vector<map<char, long long>> groups((k + 1) / 2); n   for (long long i = 0; i < (k + 1) / 2; ++i) { n     for (long long j = i; j < n; j += k) groups[i][s[j]]++; n     if (i + i < k - 1) n       for (long long j = k - i - 1; j < n; j += k) groups[i][s[j]]++; n   } n   long long ans = 0; n   for (auto m : groups) { n     vector<long long> cnts; n     for (auto p : m) cnts.push_back(p.second); n     stable_sort(cnts.rbegin(), cnts.rend()); n     ans += accumulate(cnts.begin(), cnts.end(), 0ll) - cnts.front(); n   } n   answer(ans); n } n


#include <bits/stdc++.h> n using namespace std; n void getint(int &v) { n   char ch, fu = 0; n   for (ch =  * ; (ch <  0  || ch >  9 ) && ch !=  - ; ch = getchar()) n     ; n   if (ch ==  - ) fu = 1, ch = getchar(); n   for (v = 0; ch >=  0  && ch <=  9 ; ch = getchar()) v = v * 10 + ch -  0 ; n   if (fu) v = -v; n } n const int MO = 1e9 + 7; n long long P[5555], ans, p[5555], T; n int n, k, a[5555], f[5010][5010]; n long long pw(long long x, long long y) { n   long long t = 1; n   for (; y; y >>= 1) { n     if (y & 1) t = t * x % MO; n     x = x * x % MO; n   } n   return t; n } n int main() { n   cin >> n >> k; n   T = 1; n   for (int i = 1; i <= n; ++i) getint(a[i]), T = T * a[i] % MO; n   P[0] = 1; n   for (int i = 1; i <= n; ++i) P[i] = P[i - 1] * (k - i + 1) % MO; n   f[0][0] = 1; n   for (int i = 1; i <= n; ++i) { n     f[i][0] = 1; n     for (int j = 1; j <= i; ++j) n       f[i][j] = (f[i - 1][j] + (long long)f[i - 1][j - 1] * a[i]) % MO; n   } n   long long t = MO - pw(n, MO - 2); n   p[0] = 1; n   for (int i = 1; i <= n; ++i) p[i] = p[i - 1] * t % MO; n   for (int i = 0; i <= n; ++i) { n     if (n - i > k) continue; n     long long x = f[n][i] * P[n - i] % MO * p[n - i] % MO; n     (ans += x) %= MO; n   } n   cout << (T - ans + MO) % MO << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long N = 201; n const long long K = 201; n const long long X = 201; n long long dp[N][X]; n int32_t main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   ; n   long long n, k, x; n   cin >> n >> k >> x; n   vector<long long> picBeauty; n   picBeauty.push_back(-1); n   for (long long i = 1; i <= n; i++) { n     long long x; n     cin >> x; n     picBeauty.push_back(x); n   } n   long long minimumPosts = n / k; n   if (x < minimumPosts) { n     cout << -1 << endl; n     return 0; n   } n   for (long long i = 0; i <= n; i++) { n     for (long long j = 0; j <= x; j++) { n       dp[i][j] = 0; n     } n   } n   for (long long i = 1; i <= k; i++) { n     dp[i][1] = picBeauty[i]; n   } n   long long flag = 0; n   for (long long j = 2; j <= x; j++) { n     for (long long i = 1; i <= n; i++) { n       long long tempFlag = 0; n       long long backAllowed = i - k; n       for (long long y = i - 1; y >= 1 && y >= backAllowed; y--) { n         if (dp[y][j - 1] > 0) n           dp[i][j] = max(dp[i][j], dp[y][j - 1] + picBeauty[i]); n       } n     } n   } n   long long ans = 0; n   for (long long i = n - k + 1; i <= n; i++) { n     ans = max(ans, dp[i][x]); n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<vector<int> > adj(500005); n int visited[500005]; n int counter; n void dfs(int node, int color) { n   counter++; n   visited[node] = color; n   for (int i = 0; i < adj[node].size(); ++i) { n     if (visited[adj[node][i]] == 0) dfs(adj[node][i], color); n   } n   return; n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   int test = 1; n   while (test--) { n     int n, val; n     cin >> n; n     vector<int> arr; n     for (int i = 0; i < n; ++i) { n       cin >> val; n       arr.push_back(val); n     } n     string s; n     cin >> s; n     vector<pair<int, int> > v; n     int flag = 0, start = -1, ender = -1; n     for (int i = 0; i < s.size(); ++i) { n       if (flag == 0 && s[i] ==  0 ) continue; n       if (flag == 0 && s[i] ==  1 ) { n         start = i; n         flag = 1; n         continue; n       } n       if (flag == 1 && s[i] ==  0 ) { n         ender = i; n         v.push_back({start, ender}); n         start = -1; n         ender = -1; n         flag = 0; n         continue; n       } n       if (flag == 1 && s[i] ==  1 ) continue; n     } n     if (flag == 1) v.push_back({start, n - 1}); n     vector<pair<int, int> >::iterator it; n     for (it = v.begin(); it != v.end(); ++it) { n       sort(arr.begin() + it->first, arr.begin() + it->second + 1); n     } n     flag = 0; n     for (int i = 1; i < n; ++i) { n       if (arr[i] <= arr[i - 1]) { n         flag = 1; n         break; n       } n     } n     if (flag == 1) n       cout <<  NO ; n     else n       cout <<  YES ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int a[5]; n bool able[5]; n int main() { n   int T; n   scanf( %d , &T); n   while (T--) { n     for (int i = 1; i <= 4; i++) able[i] = true; n     bool flag = true; n     for (int i = 1; i <= 4; i++) scanf( %d , &a[i]); n     if ((a[1] + a[2]) % 2 == 1) flag = false; n     if (flag) { n       able[1] = able[2] = false; n       if (a[4] == 0 && a[1] == 0) able[4] = false; n       if (a[2] == 0 && a[3] == 0) able[3] = false; n     } else { n       able[3] = able[4] = false; n       if (a[4] == 0 && a[1] == 0) able[1] = false; n       if (a[2] == 0 && a[3] == 0) able[2] = false; n     } n     for (int i = 1; i <= 4; i++) { n       if (able[i]) n         printf( Ya  ); n       else n         printf( Tidak  ); n     } n     puts(  ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int a[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; n int n, m, k; n int q(int x) { return x * (x + 1) / 2; } n int p[200005]; n int b[200005]; n int u[200005]; n int dfs(int v) { n   u[v] = 1; n   if (!u[p[v]]) n     return dfs(p[v]) + 1; n   else n     return 1; n } n int solve() { n   int n; n   cin >> n; n   for (int i = 0; i < n; ++i) { n     scanf( %d , &p[i + 1]); n   } n   int c = 0; n   for (int i = 0; i < n; ++i) { n     scanf( %d , &b[i + 1]); n     c += b[i + 1]; n   } n   if (n == 1) { n     cout << (c + 1) % 2; n     return 0; n   } n   int res = 0; n   int ones = 0; n   for (int i = 1; i <= n; ++i) n     if (!u[i]) { n       int k = dfs(i); n       if (k == 1) n         ++ones; n       else n         ++res; n     } n   if (res == 1 && ones == 0) { n     cout << (c + 1) % 2; n     return 0; n   } n   if (res == 0) n     cout << ones + (c + 1) % 2; n   else if (ones == 0) n     cout << res + (c + 1) % 2; n   else n     cout << res + ones + (c + 1) % 2; n   return 0; n } n int main() { n   int t = 1; n   while (t--) { n     solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int tux, foo, bar, baz, quz = 1; n int main() { n   scanf( %d , &tux); n   for (int i = 1; i <= tux; ++i) { n     int pur; n     scanf( %d , &pur); n     foo += pur, bar++; n     if (foo * quz > baz * bar) baz = foo, quz = bar; n   } n   printf( %.6lf , baz * 1.0 / quz); n } n


#include <bits/stdc++.h> n using namespace std; n const int INF = 0x3f3f3f3f; n const double EPS = 1e-9; n const long long MAXN = 5209; n inline long long read() { n   long long x = 0, f = 1; n   char ch = getchar(); n   while (ch < 48 || ch > 57) { n     if (ch ==  - ) f = -1; n     ch = getchar(); n   } n   while (ch >= 48 && ch <= 57) x = x * 10 + ch - 48, ch = getchar(); n   return x * f; n } n long long n, m, a[MAXN], b[MAXN], p[MAXN]; n double f[57][MAXN]; n bool check(double x) { n   for (int i = n - 1; ~i; i--) { n     for (int j = m + 1; j <= MAXN; j++) f[i + 1][j] = x; n     for (int j = 0; j <= m; j++) n       f[i][j] = min((f[i + 1][j + a[i]] + a[i]) * p[i] * 0.01 + n                         (f[i + 1][j + b[i]] + b[i]) * (100 - p[i]) * 0.01, n                     x); n   } n   return f[0][0] < x; n } n signed main() { n   n = read(), m = read(); n   for (int i = 0; i < n; i++) a[i] = read(), b[i] = read(), p[i] = read(); n   double l = 0, r = 1e9; n   int FTL = 0; n   while (l + EPS < r) { n     if (++FTL >= 1000) break; n     double mid = (l + r) * 0.5; n     if (check(mid)) n       r = mid; n     else n       l = mid; n   } n   printf( %.10f , l); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int N, K, i; n   cin >> N >> K; n   if (K == 1 || (N <= 3) || (K > 3) || (N == 4 and K == 2)) n     return cout << -1 << endl, 0; n   if (K == 2) { n     cout << N - 1 << endl; n     for (i = 1; i <= N - 1; i++) cout << i <<     << i + 1 << endl; n   } else { n     cout << 3 + (N - 4) * (2) << endl; n     cout <<  1 2  << endl; n     cout <<  2 3  << endl; n     cout <<  3 4  << endl; n     for (i = 5; i <= N; i++) n       cout << i <<   2  << endl, cout << i <<   4  << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   int w, h; n   cin >> w >> h >> n; n   multiset<int> cuth, cutw, maxh, maxw; n   cuth.insert(0); n   cuth.insert(h); n   cutw.insert(0); n   cutw.insert(w); n   maxh.insert(h); n   maxw.insert(w); n   int len; n   for (int i = 0; i < n; i++) { n     char str[10]; n     cin >> str; n     cin >> len; n     if (str[0] ==  H ) { n       multiset<int>::iterator it, it1, it2; n       it = cuth.lower_bound(len); n       it1 = it; n       it1--; n       int val = *it - *it1; n       it2 = maxh.lower_bound(val); n       maxh.erase(it2); n       cuth.insert(len); n       maxh.insert((len - *it1)); n       maxh.insert((*it - len)); n     } else { n       multiset<int>::iterator it, it1, it2; n       it = cutw.lower_bound(len); n       it1 = it; n       it1--; n       int val = *it - *it1; n       it2 = maxw.lower_bound(val); n       maxw.erase(it2); n       cutw.insert(len); n       maxw.insert((len - *it1)); n       maxw.insert((*it - len)); n     } n     long long int l1 = *(maxw.rbegin()); n     long long int l2 = *(maxh.rbegin()); n     long long int ans = l1 * l2; n     cout << ans << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int a[111][111], x[111], k; n int main() { n   int n, m; n   cin >> n >> m; n   for (int i = 0; i < n; i++) n     for (int j = 0; j < m; j++) cin >> a[i][j]; n   int in = 0; n   for (int i = 0; i < n; i++) n     if (a[i][0] < a[in][0]) in = i; n   for (int i = 0; i < n; i++) x[i] = a[i][0] - a[in][0]; n   for (int i = 0; i < n; i++) n     for (int j = 0; j < m; j++) n       if (a[in][j] + x[i] < a[i][j]) { n         cout <<  NO  << endl; n         return 0; n       } else if (a[in][j] + x[i] > a[i][j]) { n         if (k && (a[in][j] + x[i]) % k != a[i][j]) { n           cout <<  NO  << endl; n           return 0; n         } n         k = a[in][j] + x[i] - a[i][j]; n       } n   if (!k) k = 2e9; n   for (int i = 0; i < n; i++) n     for (int j = 0; j < m; j++) n       if ((a[in][j] + x[i]) % k != a[i][j]) { n         cout <<  NO  << endl; n         return 0; n       } n   cout <<  YES  << endl; n   cout << k << endl; n   for (int i = 0; i < n; i++) cout << x[i] <<    ; n   cout << endl; n   for (int j = 0; j < m; j++) cout << a[in][j] <<    ; n   cout << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n #define mod 1000000007 n #define int long long n #define double long double n #define pb push_back n #define mp make_pair n #define endl   n  n #define all(x) x.begin(),x.end() n #define fill(a,b) memset(a,b,sizeof(a)) n #define sz(x) (int)x.size() n #define ff first n #define ss second n #define lb lower_bound n #define ub upper_bound n #define bs binary_search n #define rep(i, begin, end) for (__typeof(end) i = (begin) - ((begin) > (end)); i != (end) - ((begin) > (end)); i += 1 - 2 * ((begin) > (end))) n #define deb(x) cout<<#x<<   <<x<<endl; n #define T int t; cin>>t; while(t--) n #define fst ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); n  n int firstMissingPositive(int arr[], int n) n { n       n     for (int i = 0; i < n; i++) { n  n         while (arr[i] >= 1 && arr[i] <= n n                && arr[i] != arr[arr[i] - 1]) { n             swap(arr[i], arr[arr[i] - 1]); n         } n     } n  n     for (int i = 0; i < n; i++) { n         if (arr[i] != i + 1) { n             return i + 1; n         } n     } n     n     return n + 1; n } n  n signed main() n { n  n     fst; n  n #ifndef ONLINE_JUDGE n     freopen( input3.txt ,  r , stdin); n     freopen( output3.txt ,  w , stdout); n #endif n  n      n     T{ n         int n,p,q, m,h, k, sum = 0, cnt=0, x, y, z, flag = 0, pos, diff,r=0,l=0,u=0,d=0,cnt1=0,cnt0=0,cnt2=0,ans,mi=INT_MAX,mx=INT_MIN; n         cin>>n; n          n         int a[n]; n         vector<int> v,v1,v2; n         set<int> st; n         map<int,int> mp; n  n         rep(i,0,n) { n             cin>>x; n             if(find(all(st),x) != st.end()) n               v.pb(x); n             st.insert(x); n         } n  n         for(auto i:st) cout<<i<<   ; n         sort(all(v)); n  n         for(auto i:v) cout<<i<<   ; n  n  n         cout<<endl; n  n         v.clear(); n         st.clear(); n  n     } n } n


#include <bits/stdc++.h> n using namespace std; n int n, m; n int w; n int q; n vector<int> adj[5000 + 1]; n vector<pair<int, int>> store[5000 + 1]; n bool vis[5000 + 1]; n int dfs(int g, int r, int a) { n   for (int i = 1; i <= n; i++) vis[i] = false; n   queue<pair<int, int>> q; n   priority_queue<pair<int, int>> pq; n   q.push({g, 0}); n   long long sz = 0; n   long long cost = 0; n   while (!q.empty()) { n     auto loc = q.front(); n     q.pop(); n     if (vis[loc.first]) continue; n     vis[loc.first] = true; n     for (auto p : store[loc.first]) { n       sz += p.second; n       cost += 1LL * p.first * p.second; n       pq.push({p.first, p.second}); n     } n     while (!pq.empty()) { n       if (sz <= r) break; n       auto p = pq.top(); n       pq.pop(); n       sz -= p.second; n       cost -= 1LL * p.first * p.second; n       if (sz < r) { n         pq.push({p.first, r - sz}); n         cost += 1LL * p.first * (r - sz); n         sz = r; n       } n     } n     if (sz == r && cost <= a) { n       return loc.second; n     } n     for (int e : adj[loc.first]) { n       if (!vis[e]) { n         q.push({e, loc.second + 1}); n       } n     } n   } n   return -1; n } n int main() { n   cin.tie(0)->sync_with_stdio(0); n   cin >> n >> m; n   for (int i = 0; i < m; i++) { n     int a, b; n     cin >> a >> b; n     adj[a].push_back(b); n     adj[b].push_back(a); n   } n   cin >> w; n   for (int i = 0; i < w; i++) { n     int c, k, p; n     cin >> c >> k >> p; n     store[c].push_back({p, k}); n   } n   cin >> q; n   for (int i = 0; i < q; i++) { n     int g, r, a; n     cin >> g >> r >> a; n     int ans = dfs(g, r, a); n     cout << ans <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n string to_string(string s) { return     + s +    ; } n string to_string(const char* s) { return to_string((string)s); } n string to_string(bool b) { return (b ?  true  :  false ); } n template <typename A, typename B> n string to_string(pair<A, B> p) { n   return  (  + to_string(p.first) +  ,   + to_string(p.second) +  ) ; n } n template <typename A> n string to_string(A v) { n   bool first = true; n   string res =  { ; n   for (const auto& x : v) { n     if (!first) { n       res +=  ,  ; n     } n     first = false; n     res += to_string(x); n   } n   res +=  } ; n   return res; n } n void debug_out() { cerr << endl; } n template <typename Head, typename... Tail> n void debug_out(Head H, Tail... T) { n   cerr <<     << to_string(H); n   debug_out(T...); n } n void test_case() { n   int n, l, r; n   cin >> n >> l >> r; n   vector<int> mn(n + 1); n   int rem = l - 1; n   for (int i = rem + 1; i <= n; ++i) { n     mn[i] = 1; n   } n   for (int i = rem, nxt = 2; i >= 1; --i, nxt *= 2) { n     mn[i] = nxt; n   } n   vector<int> mx(n + 1); n   for (int i = 1, unq = 0, nxt = 1; i <= n; ++i) { n     mx[i] = nxt; n     ++unq; n     if (unq < r) { n       nxt *= 2; n     } n   } n   cout << accumulate(mn.begin(), mn.end(), 0) <<     n        << accumulate(mx.begin(), mx.end(), 0) <<   n ; n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   test_case(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1000005; n using ll = long long; n int n, m, p, a[N]; n struct TNode { n   int l, r; n   vector<ll> f; n   ll sum; n } t[N << 2]; n inline int lc(int pos) { return pos << 1; } n inline int rc(int pos) { return pos << 1 | 1; } n void pushup(int pos) { n   t[pos].sum = t[lc(pos)].sum + t[rc(pos)].sum; n   int ptr = 0; n   for (int i = 1; i <= t[pos].r - t[pos].l + 1; i++) n     t[pos].f[i] = (LLONG_MAX >> 2); n   for (int i = 0; i <= t[lc(pos)].r - t[lc(pos)].l + 1; i++) { n     if (ptr > t[rc(pos)].r - t[rc(pos)].l + 1) --ptr; n     while (ptr <= t[rc(pos)].r - t[rc(pos)].l + 1) { n       ll ret = t[rc(pos)].f[ptr] - (t[lc(pos)].sum - i * 1ll * p); n       ll val = t[lc(pos)].f[i + 1] - 1 + (t[lc(pos)].sum - i * 1ll * p); n       if (val < t[rc(pos)].f[ptr]) { n         if (ptr > 0) --ptr; n         break; n       } n       t[pos].f[i + ptr] = min(t[pos].f[i + ptr], max(t[lc(pos)].f[i], ret)); n       ++ptr; n     } n   } n } n void build(int pos, int l, int r) { n   t[pos].l = l; n   t[pos].r = r; n   t[pos].f.resize(r - l + 1 + 1); n   t[pos].f[0] = LLONG_MIN >> 2; n   t[pos].f.push_back(LLONG_MAX >> 2); n   if (l == r) { n     t[pos].sum = a[l]; n     t[pos].f[1] = p - a[l]; n     return; n   } n   int mid = (l + r) >> 1; n   build(lc(pos), l, mid); n   build(rc(pos), mid + 1, r); n   pushup(pos); n } n ll query(int pos, int l, int r, ll cur = 0) { n   if (t[pos].l == l && t[pos].r == r) { n     assert(cur < LLONG_MAX / 4); n     ll pp = upper_bound(t[pos].f.begin(), t[pos].f.end(), cur) - n             t[pos].f.begin() - 1; n     return cur + t[pos].sum - p * pp; n   } n   int mid = (t[pos].l + t[pos].r) >> 1; n   if (r <= mid) n     return query(lc(pos), l, r, cur); n   else if (l > mid) n     return query(rc(pos), l, r, cur); n   else n     return query(rc(pos), mid + 1, r, query(lc(pos), l, mid, cur)); n } n int main() { n   ios::sync_with_stdio(false); n   cin >> n >> m >> p; n   for (int i = 1; i <= n; i++) cin >> a[i]; n   build(1, 1, n); n   while (m--) { n     int l, r; n     cin >> l >> r; n     cout << query(1, l, r) << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long wph; n long long mul(long long a, long long b) { n   long long c = (a * b - (long long)((long double)a / wph * b) * wph); n   return c < 0 ? c + wph : c; n } n void fwt(long long *a, int n, int f) { n   for (int k = 2, mid = 1; k <= n; k <<= 1, mid <<= 1) n     for (int i = 0; i < n; i += k) n       for (int j = 0; j < mid; j++) { n         long long x = a[i + j], y = a[i + mid + j]; n         a[i + j] = (x + y) % wph, a[i + mid + j] = (x - y + wph) % wph; n       } n   if (f) n     for (int i = 0; i < n; i++) a[i] /= n; n } n long long c[2000010], b[2000010], f[2000010]; n long long ksm(long long bs, long long mi) { n   long long ans = 1; n   while (mi) { n     if (mi & 1) ans = mul(ans, bs); n     bs = mul(bs, bs); n     mi >>= 1; n   } n   return ans; n } n long long count(long long x) { n   long long cnt = 0; n   while (x) x &= (x - 1), cnt++; n   return cnt; n } n int main() { n   long long t; n   int m, n; n   scanf( %d%I64d%I64d , &m, &t, &wph); n   n = 1 << m; n   wph *= n; n   for (int i = 0; i < n; i++) scanf( %I64d , &f[i]); n   for (int i = 0; i <= m; i++) scanf( %I64d , &b[i]); n   for (int i = 0; i < n; i++) c[i] = b[count(i)]; n   fwt(c, n, 0); n   fwt(f, n, 0); n   for (int i = 0; i < n; i++) c[i] = ksm(c[i], t); n   for (int i = 0; i < n; i++) c[i] = mul(c[i], f[i]); n   fwt(c, n, 1); n   wph /= n; n   for (int i = 0; i < n; i++) printf( %I64d n , c[i] % wph); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct Point { n   double x, y; n }; n struct Line { n   double A, B, C; n }; n Line Build(Point A, Point B) { n   Line res; n   res.A = A.y - B.y; n   res.B = B.x - A.x; n   res.C = A.x * B.y - B.x * A.y; n   return res; n } n Line Per(Line l, Point A) { n   Line res; n   res.A = -l.B; n   res.B = l.A; n   res.C = -(res.A * A.x + res.B * A.y); n   return res; n } n Line Par(Line l, Point A) { n   Line res; n   res.A = l.A; n   res.B = l.B; n   res.C = -(l.A * A.x + l.B * A.y); n   return res; n } n Point Inter(Line a, Line b) { n   Point res; n   res.x = (a.B * b.C - b.B * a.C) / (a.A * b.B - b.A * a.B); n   res.y = (a.A * b.C - b.A * a.C) / (b.A * a.B - b.B * a.A); n   return res; n } n Point Middle(Point A, Point B) { n   Point res; n   res.x = (A.x + B.x) / 2.0; n   res.y = (A.y + B.y) / 2.0; n   return res; n } n double Dist(Point A, Point B) { n   double res = sqrt((A.x - B.x) * (A.x - B.x) + (A.y - B.y) * (A.y - B.y)); n   return res; n } n double Dist2(Point A, Point B) { n   double res = (A.x - B.x) * (A.x - B.x) + (A.y - B.y) * (A.y - B.y); n   return res; n } n Point Mir(Point A, Point B) { n   Point res; n   res.x = B.x * 2 - A.x; n   res.y = B.y * 2 - A.y; n   return res; n } n double Cp2(Point A, Point B) { n   double res = A.x * B.y - A.y * B.x; n   return res; n } n double Cp3(Point O, Point A, Point B) { n   double res = (A.x - O.x) * (B.y - O.y) - (B.x - O.x) * (A.y - O.y); n   return res; n } n bool Zero(double x) { return (fabs(x) < 1e-9 ? true : false); } n Line Midper(Point A, Point B) { n   Line res = Per(Build(A, B), Middle(A, B)); n   return res; n } n bool Check(Point ans[]) { n   double tmp; n   for (int i = 0; i < 4; i++) { n     if (Zero(Cp3(ans[i], ans[(i + 1) % 4], ans[(i + 2) % 4]))) return false; n     if (i == 0) n       tmp = Cp3(ans[i], ans[(i + 1) % 4], ans[(i + 2) % 4]); n     else if (tmp * Cp3(ans[i], ans[(i + 1) % 4], ans[(i + 2) % 4]) < 0) n       return false; n   } n   return true; n } n bool Mak(Point A, Point B, Point C) { n   if (Cp3(A, B, C) > 1e-9) swap(B, C); n   Line l1 = Midper(B, A); n   Line l2 = Midper(B, C); n   Point E = Inter(l1, l2); n   Point F = Mir(E, B); n   Point ans[4]; n   ans[0] = Inter(Par(l2, F), l1); n   ans[1] = Mir(ans[0], B); n   ans[2] = Mir(ans[1], C); n   ans[3] = Mir(ans[0], A); n   if (Check(ans) == false) return false; n   puts( YES ); n   for (int i = 3; i < 7; i++) { n     printf( %.9f %.9f , ans[i & 3].x, ans[i & 3].y); n     putchar(i < 6 ? 32 : 10); n   } n   return true; n } n int main() { n   int t; n   scanf( %d , &t); n   while (t--) { n     Point a, b, c; n     scanf( %lf%lf%lf%lf%lf%lf , &a.x, &a.y, &b.x, &b.y, &c.x, &c.y); n     if (!Zero(Cp3(a, b, c)) && (Mak(a, b, c) || Mak(b, c, a) || Mak(c, a, b))) n       continue; n     puts( NO n ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long int n, m; n pair<long long int, bool> All[120]; n long long int ans = 0, na; n void GetAll() { n   cin >> n >> m; n   for (int i = 0; i < n; i++) { n     cin >> All[i].first; n     All[i].second = false; n   } n   int buf; n   for (int i = 0; i < m; i++) { n     cin >> buf; n     All[buf - 1].second = true; n   } n } n void Run() { n   sort(All, All + n); n   for (int i = 0; i < n; i++) { n     if (!All[i].second) { n       na += All[i].first; n     } n   } n   for (int i = n - 1; i >= 0; i--) { n     if (!All[i].second) { n       continue; n     } n     if (All[i].first > na) { n       na += All[i].first; n     } else { n       na += na; n     } n   } n   cout << na; n } n int main() { n   GetAll(); n   Run(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; 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 bool prime(long long int n) { n   for (long long int i = 2; i * i <= n; i++) { n     if (n % i == 0) return false; n   } n   return true; n } n long long binexpm(long long a, long long b, long long m) { n   a %= m; n   long long res = 1; n   while (b > 0) { n     if (b & 1) res = res * a % m; n     a = a * a % m; n     b >>= 1; n   } n   return res; n } n long long binexp(long long a, long long b) { n   long long res = 1; n   while (b > 0) { n     if (b & 1) res = res * a; n     a = a * a; n     b >>= 1; n   } n   return res; n } n long long int n, p; n vector<long long int> adj[10001]; n map<pair<long long int, long long int>, long long int> mp; n bool vis[10001] = {false}; n map<pair<long long int, long long int>, long long int> res; n long long int x, y = -1; n long long int dm = INT_MAX; n bool xx = true; n long long int in[10001] = {0}; n long long int out[10001] = {0}; n bool dfs(long long int node) { n   if (vis[node]) xx = false; n   vis[node] = true; n   for (auto child : adj[node]) { n     dm = min(dm, mp[{node, child}]); n     if (adj[child].size() == 0) y = child; n     xx &= dfs(child); n   } n   return xx; n } n int32_t main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   cin >> n >> p; n   for (long long int i = 0; i < p; i++) { n     long long int a, b, d; n     cin >> a >> b >> d; n     out[a]++; n     in[b]++; n     adj[a].push_back(b); n     mp[make_pair(a, b)] = d; n   } n   long long int ans = 0; n   for (long long int i = 1; i <= n; i++) { n     bool flag; n     dm = INT_MAX; n     if (out[i] == 1 && in[i] == 0) { n       x = i; n       flag = dfs(i); n       if (flag && y != -1) { n         ans++; n         res[make_pair(x, y)] = dm; n       } n     } n   } n   cout << ans <<   n ; n   if (ans != 0) { n     for (auto it : res) { n       cout << it.first.first <<     << it.first.second <<     << it.second n            <<   n ; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int h, i, j, x, l, r, pa[200200], pb[200200]; n bool ans; n int main() { n   scanf( %d , &h); n   for (j = 0; j <= h; j++) { n     scanf( %d , &x); n     for (i = 1; i <= x; i++) { n       pa[r + i] = l; n       pb[r + i] = min(l + i - 1, r); n       if (pa[r + i] != pb[r + i]) ans = true; n     } n     l = r + 1; n     r = r + x; n   } n   if (ans) { n     puts( ambiguous ); n     for (i = 1; i <= r; i++) printf( %d  , pa[i]); n     puts(  ); n     for (i = 1; i <= r; i++) printf( %d  , pb[i]); n     puts(  ); n   } else n     puts( perfect ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, m; n   cin >> n >> m; n   long long ans = 0; n   for (int i = (0); i < (n); ++i) { n     long long t, T, x, cost; n     cin >> t >> T >> x >> cost; n     if (t >= T) { n       ans += cost + m * x; n       continue; n     } n     long long aux1 = cost; n     if (m > (T - t)) aux1 += m * x; n     long long aux2 = (long long)ceil((double)(m - (T - t)) / (T - t)) + 1; n     aux2 *= cost; n     ans += min(aux1, aux2); n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m, q; n int matrix[100][100]; n int ans[10000]; n void fill() { n   for (int i = 0; i < n; ++i) { n     for (int j = 0; j < m; ++j) { n       matrix[i][j] = i * m + j; n     } n   } n } n void solve_1(int i) { n   int temp; n   temp = matrix[i][m - 1]; n   matrix[i][m - 1] = matrix[i][0]; n   for (int j = m - 2; j >= 0; --j) { n     int new_temp = matrix[i][j]; n     matrix[i][j] = temp; n     temp = new_temp; n   } n } n void solve_2(int j) { n   int temp; n   temp = matrix[n - 1][j]; n   matrix[n - 1][j] = matrix[0][j]; n   for (int i = n - 2; i >= 0; --i) { n     int new_temp = matrix[i][j]; n     matrix[i][j] = temp; n     temp = new_temp; n   } n } n int main(int argc, char *argv[]) { n   memset(ans, 0, sizeof(ans)); n   scanf( %d%d%d , &n, &m, &q); n   fill(); n   while (q--) { n     int t; n     scanf( %d , &t); n     if (t == 1) { n       int n_i; n       scanf( %d , &n_i); n       n_i--; n       solve_1(n_i); n     } else if (t == 2) { n       int m_i; n       scanf( %d , &m_i); n       m_i--; n       solve_2(m_i); n     } else { n       int r, c, x; n       scanf( %d%d%d , &r, &c, &x); n       r--; n       c--; n       ans[matrix[r][c]] = x; n     } n   } n   for (int i = 0; i < n; ++i) { n     for (int j = 0; j < m; ++j) { n       printf( %d  , ans[i * m + j]); n     } n     puts(  ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int d, s; n int E[2510050][10]; n int Get(int x, int y) { return x * d + y; } n int dis[2710500]; n char ok[2700050] = {}; n vector<int> ans; n int ST, EN; n int dfs(int x) { n   if (x == EN) return 1; n   ok[x] = 1; n   for (int u = 0; u < 10; u++) n     if (x != 0 || u != 0) { n       int v = E[x][u]; n       if (dis[v] == dis[x] + 1 && ok[v] == 0) { n         ans.push_back(u); n         if (dfs(v)) return 1; n         ans.pop_back(); n       } n     } n   return 0; n } n int main() { n   scanf( %d%d , &d, &s); n   for (int i = 0; i <= s; i++) n     for (int j = 0; j < d; j++) { n       int x = Get(i, j); n       for (int e = 0; e < 10; e++) { n         if (i == 0 && j == 0 && e == 0) continue; n         int ni = i + e; n         int nj = (10 * j + e) % d; n         if (ni <= s) { n           int xx = Get(ni, nj); n           E[x][e] = xx; n         } n       } n     } n   ST = Get(0, 0), EN = Get(s, 0); n   vector<int> v; n   v.push_back(ST); n   memset(dis, -1, sizeof dis); n   dis[ST] = 0; n   for (int i = 0; i < (int)v.size(); i++) { n     int t = v[i]; n     for (int u = 0; u < 10; u++) n       if (t != 0 || u != 0) { n         int w = E[t][u]; n         if (dis[w] == -1) { n           dis[w] = dis[t] + 1; n           v.push_back(w); n         } n       } n   } n   if (dis[EN] == -1) n     puts( -1 ); n   else { n     dfs(ST); n     for (int e : ans) printf( %d , e); n     puts(  ); n   } n } n


#include <bits/stdc++.h> n using namespace std; n int n, i, tmp, res, a[10000]; n bool kt; n int main() { n   scanf( %d , &n); n   for (i = 1; i <= n; i++) scanf( %d , &a[i]); n   kt = true; n   for (i = 3; i <= n; i++) n     if (a[i] - a[i - 1] != a[i - 1] - a[i - 2]) kt = false; n   if (kt == false) { n     printf( %d , a[n]); n     return 0; n   } n   tmp = a[1] - a[2]; n   res = a[n] - tmp; n   printf( %d , res); n } n