#include <bits/stdc++.h> n using namespace std; n const int maxn = 3e5 + 5; n const int inf = 0x3f3f3f3f; n const int mod = 1e9 + 7; n int T, n, m; n string s; n int vis[26]; n int pos[maxn][26]; n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   cin >> T; n   while (T--) { n     memset(vis, 0, sizeof(vis)); n     cin >> n >> m; n     for (int i = 0; i < 26; ++i) { n       for (int j = 0; j <= n + 5; ++j) { n         pos[j][i] = 0; n       } n     } n     cin >> s; n     for (int i = 0; i < s.size(); ++i) { n       for (int j = 0; j < 26; ++j) { n         pos[i][j] = pos[i - 1 == -1 ? 0 : i - 1][j]; n       } n       pos[i][s[i] -  a ]++; n     } n     for (int i = 0, x; i < m; ++i) { n       cin >> x; n       x--; n       for (int j = 0; j < 26; ++j) { n         vis[j] += pos[x][j]; n       } n     } n     for (int j = 0; j < 26; ++j) { n       vis[j] += pos[n - 1][j]; n     } n     for (int i = 0; i < 26; ++i) { n       cout << vis[i] <<    ; n     } n     cout <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n class BIT { n   vector<int> tree; n  n  public: n   BIT(int n) : tree(n + 1, 0) {} n   BIT(const vector<int>& Arg) { n     tree.resize(Arg.size()); n     for (int i = 0; i < tree.size(); i++) increase(i, Arg[i]); n   } n   void increase(int i, int delta) { n     for (; i < (int)tree.size(); i |= i + 1) tree[i] += delta; n   } n   int getsum(int left, int right) { return sum(right) - sum(left - 1); } n   int sum(int ind) { n     int sum = 0; n     while (ind >= 0) { n       sum += tree[ind]; n       ind &= ind + 1; n       ind--; n     } n     return sum; n   } n }; n int main(int argc, char const* argv[]) { n   int n, k, a, b, q; n   scanf( %d %d , &n, &k); n   scanf( %d %d , &a, &b); n   scanf( %d , &q); n   BIT bita(n + 10); n   BIT bitb(n + 10); n   for (int i = 0; i < q; i += 1) { n     int op; n     scanf( %d , &op); n     if (op == 1) { n       int di, ai; n       scanf( %d %d , &di, &ai); n       bitb.increase(di, min(b, ai)); n       bita.increase(di, min(a, ai)); n       int sb = bitb.getsum(di, di); n       int sa = bita.getsum(di, di); n       if (sb > b) bitb.increase(di, b - sb); n       if (sa > a) bita.increase(di, a - sa); n     } else { n       int p; n       scanf( %d , &p); n       int nxt = p + k; n       int s = 0; n       if (p > 1) s += bitb.getsum(1, p - 1); n       if (nxt <= n) s += bita.getsum(nxt, n); n       printf( %d n , s); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n = 0, cc = 0, cn = 0, ic = 0, in1 = 0; n   int pr; n   cin >> n; n   for (int i = 1; i <= n; i++) { n     cin >> pr; n     if (pr % 2 == 1) { n       cn++; n       in1 = i; n     } else { n       cc++; n       ic = i; n     } n   } n   int answer = cn > cc ? ic : in1; n   cout << answer; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 2e3 + 10, oo = maxn; n int n, m; n char s[maxn], t[maxn]; n int a[maxn]; n int dp[maxn][maxn]; n void myMax(int &a, int b) { n   if (a < b) { n     a = b; n   } n } n int main() { n   int i, j, k, x, y; n   scanf( %s , s + 1); n   n = strlen(s + 1); n   scanf( %s , t + 1); n   m = strlen(t + 1); n   for (i = n; i >= m; i--) { n     for (j = i, k = 0; j; j--) { n       if (s[j] == t[m - k] && ++k == m) { n         break; n       } n     } n     a[i] = k == m ? i - j + 1 : oo; n   } n   for (; i; i--) { n     a[i] = oo; n   } n   dp[0][0] = 0; n   for (i = (1); i <= (n); i++) { n     for (j = (0); j <= (i); j++) { n       dp[i][j] = 0; n       if (j < i) { n         dp[i][j] = dp[i - 1][j]; n       } n       if (j) { n         myMax(dp[i][j], dp[i - 1][j - 1]); n       } n       if (a[i] != oo && (y = j - a[i] + m) >= 0 && (x = i - a[i]) >= y) { n         myMax(dp[i][j], dp[x][y] + 1); n       } n     } n   } n   for (i = (0); i <= (n); i++) { n     printf( %d%c , dp[n][i],    n [i == n]); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long i, j, k, n, t, s, p[100009], d[100009], a[100009]; n int main() { n   cin >> n; n   for (i = 1; i <= n; i++) cin >> p[i]; n   for (i = 1; i <= n; i++) n     if (!d[i]) { n       k = 0; n       j = i; n       while (d[j] == 0) { n         d[j] = 1; n         j = p[j]; n         k++; n       } n       d[i] = 1; n       k = max(1LL, k); n       a[++t] = k; n     } n   sort(a + 1, a + t + 1); n   for (i = 1; i <= t - 2; i++) s += a[i] * 1LL * a[i]; n   k = a[t - 1] + a[t]; n   s += k * 1LL * k; n   cout << s; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   cin >> n; n   int score = 0; n   string str; n   cin >> str; n   for (int j = 0; j < str.size(); ++j) { n     if (str[j] ==  A ) score += 1; n     if (str[j] ==  D ) { n       score -= 1; n     } n   } n   if (score > 0) cout <<  Anton ; n   if (score < 0) cout <<  Danik ; n   if (score == 0) cout <<  Friendship ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int INF = 1000 * 1000 * 1000; n const long long INF64 = 1LL * INF * INF; n const long long mod = INF + 7; n int m[10][10]; n bool check(int k, int x, int y, int w, int h) { n   int ans = 0; n   for (int i = x; i <= h; ++i) { n     for (int j = y; j <= w; ++j) { n       ans += m[i][j]; n     } n   } n   return ans >= k; n } n void solve() { n   int r, c, n, k; n   cin >> r >> c >> n >> k; n   for (int i = 0; i < n; ++i) { n     int x, y; n     cin >> x >> y; n     x--; n     y--; n     m[x][y] = 1; n   } n   int ans = 0; n   for (int i = 0; i < r; ++i) { n     for (int j = 0; j < c; ++j) { n       for (int w = j; w < c; ++w) { n         for (int h = i; h < r; ++h) { n           ans += check(k, i, j, w, h); n         } n       } n     } n   } n   cout << ans << endl; n } n int main() { n   ios_base::sync_with_stdio(false); n   solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long n = 0, m = 0; n   cin >> n; n   vector<int> hit(n, -1); n   map<int, pair<int, int> > circle; n   for (int i = 0; i < n; i++) { n     int c, r; n     cin >> c >> r; n     circle.insert(make_pair(c, make_pair(r, i))); n   } n   int hits = 0; n   cin >> m; n   for (int i = 0; i < m; i++) { n     int x, y; n     cin >> x >> y; n     map<int, pair<int, int> >::iterator it = circle.upper_bound(x); n     for (int j = 0; j < 2; j++) { n       if (it == circle.end()) it--; n       int c = it->first, r = it->second.first, n = it->second.second; n       if ((x - c) * (x - c) + y * y <= r * r) { n         if (hit[n] < 0) { n           hits++; n           hit[n] = i + 1; n         } n       } n       if (it == circle.begin()) { n         break; n       } n       --it; n     } n   } n   cout << hits << endl; n   for (int i = 0; i < hit.size(); i++) { n     cout << hit[i] <<    ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int mod = int(1e9) + 7; n const int maxn = 200010; n int n; n long long a[maxn]; n long long fact[maxn]; n long long ans; n void read() { n   scanf( %d , &n); n   for (int i = 1; i <= n; ++i) scanf( %I64d , &a[i]); n } n long long exp(long long x) { n   long long s = 1, times = mod - 2; n   while (times) { n     if (times & 1) s = s * x % mod; n     x = x * x % mod; n     times >>= 1; n   } n   return s; n } n long long calc(int x, int y) { n   return (fact[x] * exp(fact[y] * fact[x - y] % mod) % mod); n } n void solve() { n   if (n == 1) { n     ans = a[1]; n     return; n   } n   if (n & 1) { n     for (int i = 1; i < n; ++i) a[i] += ((i & 1) ? a[i + 1] : -a[i + 1]); n     --n; n   } n   fact[0] = 1; n   for (int i = 1; i <= n; ++i) fact[i] = fact[i - 1] * i % mod; n   ans = 0; n   if (n % 4 == 0) { n     for (int i = 1; i <= n; ++i) { n       long long tmp = calc(n / 2 - 1, (i - 1) / 2) * a[i] % mod; n       if (i & 1) n         ans = (ans + tmp) % mod; n       else n         ans = (ans - tmp + mod) % mod; n     } n   } else { n     for (int i = 1; i <= n; ++i) n       ans = (ans + calc(n / 2 - 1, (i - 1) / 2) * a[i] % mod) % mod; n   } n } n int main() { n   read(); n   solve(); n   while (ans < 0) ans += mod; n   printf( %I64d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int it[1000100 * 5], a[1000100]; n char s[1000100]; n void incIT(int node, int l, int r, int pos, int k) { n   if (pos < l || pos > r) return; n   if (l == r) { n     it[node] += k; n     return; n   } n   incIT(node * 2, l, (l + r) / 2, pos, k); n   incIT(node * 2 + 1, (l + r) / 2 + 1, r, pos, k); n   it[node] = it[node * 2] + it[node * 2 + 1]; n } n int findAndDec(int node, int l, int r, int pos, int k) { n   if (l == r) { n     it[node] -= k; n     return l; n   } n   int res = 0; n   if (it[node * 2] >= pos) n     res = findAndDec(node * 2, l, (l + r) / 2, pos, k); n   else n     res = findAndDec(node * 2 + 1, (l + r) / 2 + 1, r, pos - it[node * 2], k); n   it[node] = it[node * 2] + it[node * 2 + 1]; n   return res; n } n int main() { n   cin.sync_with_stdio(false); n   int n, m; n   cin >> n >> m; n   for (int i = 0; i < m; ++i) cin >> a[i]; n   int cnt = 0; n   for (int i = 0; i < n; ++i) { n     int x; n     cin >> x; n     if (x == -1) { n       int mm = upper_bound(a, a + m, it[1]) - a; n       for (int j = mm - 1; j >= 0; --j) findAndDec(1, 1, n, a[j], 1); n     } else { n       s[++cnt] =  0  + x; n       incIT(1, 1, n, cnt, 1); n     } n   } n   int len = 0; n   while (it[1] > 0) { n     int pos = findAndDec(1, 1, n, 1, 1); n     s[len++] = s[pos]; n   } n   s[len] =   0 ; n   if (len == 0) n     cout <<  Poor stack!  << endl; n   else n     cout << s << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n char a[1000005]; n char b[1000005]; n int main(void) { n   int n; n   cin >> n >> (a + 1) >> (b + 1); n   int ok = 0; n   for (int i = 1; i <= n; ++i) ok += a[i] != b[i]; n   if (!ok) return cout <<  26 n , 0; n   if (ok == 1) return cout <<  2 n , 0; n   string k =   , l =   ; n   for (int i = 1; i <= n; ++i) k += a[i]; n   for (int i = 1; i <= n; ++i) l += b[i]; n   int cnt = 0, ans = 0; n   while (k[cnt] == l[cnt]) ++cnt; n   k.insert(k.begin() + cnt, l[cnt]); n   while (k[cnt] == l[cnt]) ++cnt; n   l.insert(l.begin() + cnt, k[cnt]); n   ans += (k == l); n   k =   ; n   l =   ; n   for (int i = 1; i <= n; ++i) k += a[i]; n   for (int i = 1; i <= n; ++i) l += b[i]; n   cnt = 0; n   while (k[cnt] == l[cnt]) ++cnt; n   l.insert(l.begin() + cnt, k[cnt]); n   while (k[cnt] == l[cnt]) ++cnt; n   k.insert(k.begin() + cnt, l[cnt]); n   ans += (k == l); n   return cout << ans <<   n , 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   char s[111111]; n   scanf( %s , s); n   int len = strlen(s); n   int i; n   if (len == 1 && s[0] ==  a ) { n     puts( z ); n     return 0; n   } n   i = 0; n   while (s[i] ==  a ) { n     i++; n   } n   if (i == len) { n     s[len - 1] =  z ; n   } n   for (; i < len; i++) { n     if (s[i] ==  a ) break; n     s[i]--; n   } n   printf( %s n , s); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int mod = 1e9 + 7; n inline int add(int x, int y) { n   x += y; n   if (x >= mod) x -= mod; n   return x; n } n inline int sub(int x, int y) { n   x -= y; n   if (x < 0) x += mod; n   return x; n } n inline int mul(int x, int y) { return (x * 1ll * y) % mod; } n inline int powr(int a, long long b) { n   int x = 1 % mod; n   while (b) { n     if (b & 1) x = mul(x, a); n     a = mul(a, a); n     b >>= 1; n   } n   return x; n } n inline int inv(int a) { return powr(a, mod - 2); } n vector<pair<int, int> > A, B; n bool cmp(const pair<int, int>& x, const pair<int, int>& y) { n   return x.first < y.first || (x.first == y.first && x.second > y.second); n } n void go(vector<pair<int, int> >& X) { n   vector<pair<int, int> > ret; n   sort(X.begin(), X.end(), cmp); n   for (int i = 0; i < X.size(); i++) { n     int l1 = X[i].first, r1 = X[i].second; n     while (!ret.empty()) { n       int l2 = ret.back().first, r2 = ret.back().second; n       if (l1 >= l2 && r1 <= r2) n         ret.pop_back(); n       else n         break; n     } n     ret.push_back(X[i]); n   } n   X = ret; n } n const int inv2 = (mod + 1) >> 1; n const int MAXN = 200005; n int dp[2][2][MAXN]; n int dp1[2][2][MAXN]; n int dp2[2][2][MAXN]; n void ADD(int& a, int b) { n   a += b; n   if (a >= mod) a -= mod; n } n vector<int> P[2]; n int main() { n   int k, n, m; n   scanf( %d , &(k)); n   scanf( %d , &(n)); n   scanf( %d , &(m)); n   int mn = 2e9, mx = -1; n   for (int i = 1; i <= n; i++) { n     int a, b; n     scanf( %d , &(a)); n     scanf( %d , &(b)); n     A.push_back({a, b}); n   } n   for (int i = 1; i <= m; i++) { n     int a, b; n     scanf( %d , &(a)); n     scanf( %d , &(b)); n     B.push_back({a, b}); n   } n   go(A); n   go(B); n   P[0] = P[1] = {-1}; n   for (auto it : A) P[0].push_back(it.second); n   for (auto it : B) P[1].push_back(it.second); n   reverse(A.begin(), A.end()); n   reverse(B.begin(), B.end()); n   dp[0][0][0] = dp1[0][0][0] = dp2[0][0][0] = 1; n   int cnt[2] = {0, 0}; n   int f[2] = {powr(2, k), 0}; n   while (!A.empty() || !B.empty()) { n     int L, R, type; n     if (A.empty()) { n       L = B.back().first; n       R = B.back().second; n       type = 1; n       B.pop_back(); n     } else if (B.empty()) { n       L = A.back().first; n       R = A.back().second; n       type = 0; n       A.pop_back(); n     } else { n       int l1 = A.back().first, r1 = A.back().second; n       int l2 = B.back().first, r2 = B.back().second; n       if (l1 < l2 || (l1 == l2 && r1 < r2)) { n         L = l1; n         R = r1; n         type = 0; n         A.pop_back(); n       } else { n         L = l2; n         R = r2; n         type = 1; n         B.pop_back(); n       } n     } n     cnt[type]++; n     for (int prevparity = 0; prevparity < 2; prevparity++) { n       int newparity = prevparity ^ 1; n       int ind = n           lower_bound(P[type].begin(), P[type].end(), L) - P[type].begin(); n       int ind2 = lower_bound(P[type ^ 1].begin(), P[type ^ 1].end(), L) - n                  P[type ^ 1].begin(); n       ADD(dp[type][newparity][cnt[type]], n           mul(powr(2, L - 1), add(dp2[type][prevparity][ind - 1], n                                   dp2[type ^ 1][prevparity][ind2 - 1]))); n       ADD(dp[type][newparity][cnt[type]], n           sub(dp1[type][prevparity][cnt[type] - 1], n               dp1[type][prevparity][ind - 1])); n     } n     for (int parity = 0; parity < 2; parity++) { n       dp2[type][parity][cnt[type]] = n           add(dp2[type][parity][cnt[type] - 1], n               mul(powr(inv2, R), dp[type][parity][cnt[type]])); n       dp1[type][parity][cnt[type]] = n           add(dp1[type][parity][cnt[type] - 1], dp[type][parity][cnt[type]]); n       f[parity] = n           add(f[parity], mul(dp[type][parity][cnt[type]], powr(2, k - R))); n     } n   } n   printf( %d n , sub(f[0], f[1])); n } n


#include <bits/stdc++.h> n using namespace std; n const int MX = 1005; n struct Edge { n   int v, nxt; n } edge[MX * MX]; n int head[MX], tot, c[MX], E[MX], V[MX], vis[MX]; n void add(int u, int v) { n   edge[tot].v = v; n   edge[tot].nxt = head[u]; n   head[u] = tot++; n } n void dfs(int u, int &e, int &v) { n   for (int i = head[u]; ~i; i = edge[i].nxt) { n     int vv = edge[i].v; n     e++; n     if (vis[vv]) continue; n     v++; n     vis[vv] = 1; n     dfs(vv, e, v); n   } n } n int main() { n   int n, m, k; n   while (~scanf( %d%d%d , &n, &m, &k)) { n     memset(head, -1, sizeof(head)); n     memset(vis, 0, sizeof(vis)); n     memset(E, 0, sizeof(E)); n     memset(V, 0, sizeof(V)); n     tot = 0; n     for (int i = 0; i < k; i++) scanf( %d , &c[i]); n     for (int i = 0; i < m; i++) { n       int u, v; n       scanf( %d%d , &u, &v); n       add(u, v); n       add(v, u); n     } n     int ans = 0, mx = 0, c1 = n, c2 = m; n     for (int i = 0; i < k; i++) { n       int v = c[i]; n       vis[v] = 1; n       V[v]++; n       dfs(v, E[v], V[v]); n       c1 -= V[v]; n       c2 -= E[v] / 2; n       if (V[mx] < V[v]) mx = v; n       ans += V[v] * (V[v] - 1) / 2 - E[v] / 2; n     } n     ans += (V[mx] + c1) * (V[mx] + c1 - 1) / 2 - c2 - V[mx] * (V[mx] - 1) / 2; n     printf( %d n , ans); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n; n bool check(int num) { n   if (1 == num) return true; n   for (int i = 2; i <= num / i; i++) { n     if (num % i == 0) { n       return false; n     } n   } n   return true; n } n int main() { n   cin >> n; n   if (check(n)) { n     printf( 1 n ); n     printf( %d n , n); n   } else if (check(n - 2)) { n     printf( 2 n ); n     printf( 2 %d n , n - 2); n   } else if (check(n - 4)) { n     printf( 3 n ); n     printf( 2 2 %d n , n - 4); n   } else { n     bool result = false; n     int i, even; n     int prime1, temp, prime2; n     for (int num = 3; num < n / 3 + 10; num++) { n       if (!check(num)) continue; n       even = n - num; n       for (prime1 = 3; prime1 <= even / 2; prime1 += 2) { n         for (temp = 2; temp <= sqrt(prime1); temp++) { n           if (prime1 % temp == 0) break; n         } n         if (temp > sqrt(prime1)) n           prime2 = even - prime1; n         else n           continue; n         for (temp = 2; temp <= sqrt(prime2); temp++) { n           if (prime2 % temp == 0) break; n         } n         if (temp > sqrt(prime2)) { n           cout << 3 << endl << num <<     << prime1 <<     << prime2 << endl; n           result = true; n           break; n         } n       } n       if (!result) n         continue; n       else n         break; n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 100007; n int n, k, a[N]; n bool solve() { n   bool exists = false; n   for (int i = 0; i < n; ++i) { n     cin >> a[i]; n     if (a[i] == k) { n       exists = true; n     } n   } n   if (!exists) { n     return false; n   } n   if (n == 1) return true; n   for (int i = 0; i < n; ++i) { n     int ct = 0; n     for (int j = i; j < i + 3 && j < n; ++j) { n       if (a[j] >= k) { n         ct++; n       } n     } n     if (ct >= 2) { n       return true; n     } n   } n   return false; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   int t; n   cin >> t; n   while (t--) { n     cin >> n >> k; n     if (solve()) { n       cout <<  yes n ; n     } else { n       cout <<  no n ; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const double PI = acos(-1); n void fft(vector<complex<double> >& a, bool invert) { n   int n = (int)a.size(); n   for (int i = 1, j = 0; i < n; ++i) { n     int bit = n >> 1; n     for (; j >= bit; bit >>= 1) j -= bit; n     j += bit; n     if (i < j) swap(a[i], a[j]); n   } n   for (int len = 2; len <= n; len <<= 1) { n     double ang = 2 * PI / len * (invert ? -1 : 1); n     complex<double> wlen(cos(ang), sin(ang)); n     for (int i = 0; i < n; i += len) { n       complex<double> w(1); n       for (int j = 0; j < len / 2; ++j) { n         complex<double> u = a[i + j], v = a[i + j + len / 2] * w; n         a[i + j] = u + v; n         a[i + j + len / 2] = u - v; n         w *= wlen; n       } n     } n   } n   if (invert) n     for (int i = 0; i < n; ++i) a[i] /= n; n } n void multiply(const vector<long long int>& a, const vector<long long int>& b, n               vector<long long int>& res) { n   vector<complex<double> > fa(a.begin(), a.end()), fb(b.begin(), b.end()); n   size_t n = 1; n   while (n < max(a.size(), b.size())) n <<= 1; n   n <<= 1; n   fa.resize(n), fb.resize(n); n   fft(fa, false), fft(fb, false); n   for (size_t i = 0; i < n; ++i) fa[i] *= fb[i]; n   fft(fa, true); n   res.resize(n); n   for (size_t i = 0; i < n; ++i) res[i] = (long long int)(fa[i].real() + 0.5); n } n vector<long long int> now(1, 1); n int main() { n   vector<long long int> res; n   int n, x; n   cin >> n >> x; n   int need = n; n   while (n--) { n     int u; n     cin >> u; n     if (u < x) { n       now.push_back(1); n     } else { n       ++now.back(); n     } n   } n   long long int w0 = 0; n   for (auto e : now) { n     w0 += e * (e - 1) / 2; n   } n   int k = now.size(); n   vector<long long int> nowr(now.rbegin(), now.rend()); n   nowr.back() = 0; n   multiply(now, nowr, res); n   cout << w0 <<    ; n   for (int i = k - 2; i >= 0 && res[i]; i--) { n     cout << res[i] <<    ; n     --need; n   } n   while (need--) { n     cout << 0 <<    ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n string proc(int a, int b) { n   if (a * 2 > b) return  noob ; n   if (a * 5 > b) return  random ; n   if (a * 10 > b) return  average ; n   if (a * 100 > b) return  hardcore ; n   return  pro ; n } n int main() { n   int i, n, x; n   string s; n   map<string, int> score; n   cin >> n; n   while (n--) { n     cin >> s >> x; n     if (score.find(s) == score.end()) score[s] = 0; n     score[s] = max(score[s], x); n   } n   vector<int> list; n   for (typeof(score.begin()) it = score.begin(); it != score.end(); it++) n     list.push_back(it->second); n   sort(list.rbegin(), list.rend()); n   cout << ((int)score.size()) << endl; n   for (typeof(score.begin()) it = score.begin(); it != score.end(); it++) { n     for (i = 0; i < (int)(((int)list.size())); i++) n       if (list[i] == it->second) break; n     cout << it->first <<     << proc(i, ((int)list.size())) << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   string s1, s2; n   getline(cin, s1); n   getline(cin, s2); n   int yek1 = 0, yek2 = 0; n   for (int i = 0; i < s1.size(); i++) { n     if (s1[i] ==  1 ) yek1++; n   } n   for (int i = 0; i < s2.size(); i++) { n     if (s2[i] ==  1 ) yek2++; n   } n   if (yek1 % 2 == 1) yek1++; n   if (yek1 >= yek2) { n     cout <<  YES ; n     return 0; n   } n   cout <<  NO ; n } n


#include <bits/stdc++.h> n using namespace std; n template <class T> n T min(T a, T b, T c) { n   return min(a, min(b, c)); n } n template <class T> n T min(T a, T b, T c, T d) { n   return min(a, min(b, min(c, d))); n } n template <class T> n T max(T a, T b, T c) { n   return max(a, max(b, c)); n } n template <class T> n T max(T a, T b, T c, T d) { n   return max(a, max(b, max(c, d))); n } n bool cmp(const pair<int, int>& a, const pair<int, int>& b) { n   return (a.first > b.first || (a.first == b.first && a.second >= b.second)); n } n long long GCD(long long a, long long b) { return (a % b) ? GCD(b, a % b) : b; } n const string namePro =  tmp ; n int nodes[4 * 1000007]; n int a[1000007]; n void initIT(int root, int left, int right) { n   if (left == right) { n     nodes[root] = a[left]; n     return; n   } n   int mid = (left + right) / 2; n   initIT(2 * root, left, mid); n   initIT(2 * root + 1, mid + 1, right); n   nodes[root] = min(nodes[2 * root], nodes[2 * root + 1]); n   return; n } n int queryIT(int root, int left, int right, int val) { n   int mid = (left + right) / 2; n   if (left == right) return left; n   if (nodes[2 * root + 1] < val) n     return queryIT(2 * root + 1, mid + 1, right, val); n   else n     return queryIT(2 * root, left, mid, val); n } n int main() { n   int n; n   scanf( %d , &n); n   for (int i = (1); i <= (n); ++i) scanf( %d , &a[i]); n   initIT(1, 1, n); n   for (int i = (1); i <= (n); ++i) { n     int k = queryIT(1, 1, n, a[i]); n     if (k <= i) n       printf( -1  ); n     else n       printf( %d  , k - i - 1); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m, s = 0; n int t[501][501]; n int a = -1, b = -1, c = -1, d; n int g = -1; n int main() { n   ios_base::sync_with_stdio(false); n   cin >> n >> m; n   for (int i = 0; i < n - 1; i++) { n     for (int j = 0; j < m; j++) { n       cin >> t[i][j]; n       if (j == 0) { n         s = s ^ t[i][j]; n         a = t[i][j]; n       } n       if (((!(t[i][j] == a)) && (j > 0)) && (b == -1)) { n         b = i; n         c = j + 1; n       } n     } n   } n   for (int j = 0; j < m; j++) { n     cin >> d; n     if (((s ^ d) > 0) && (g == -1)) { n       g = j + 1; n     } n   } n   if (g == -1) { n     if (b == -1) { n       cout <<  NIE  << endl; n     } else { n       cout <<  TAK  << endl; n       for (int i = 0; i < n; i++) { n         if (i == b) { n           cout << c <<    ; n         } else { n           cout <<  1  n                <<    ; n         } n       } n     } n   } else { n     cout <<  TAK  << endl; n     for (int i = 0; i < n - 1; i++) { n       cout <<  1  n            <<    ; n     } n     cout << g <<    ; n   } n   cout << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <class T> n inline void umax(T &a, T b) { n   if (a < b) a = b; n } n const int maxn = 4000 + 5; n const int INF = 1e9 + 7; n const double eps = 1e-8; n string a[10]; n int dp[4][105]; n int n, k; n int valid(int x, int y) { n   if (x >= 0 && x < n && y >= 0 && y < 3) return true; n   return false; n } n int rec(int posy, int posx) { n   if (posy < 0 || posy >= 3) return 0; n   if (posx >= n - 1) return 1; n   if (dp[posy][posx] != -1) return dp[posy][posx]; n   int &ret = dp[posy][posx]; n   ret = 0; n   if (posx >= n - 1) { n     ret = 1; n     return ret; n   } n   if (a[posy][posx + 1] !=  . ) { n     ret = 0; n     return ret; n   } n   for (int i = (-1); i < (2); i++) { n     if (posy + i < 0 || posy + i >= 3) continue; n     if (a[posy + i][posx + 1] !=  . ) continue; n     if (a[posy + i][posx + 2] !=  . ) continue; n     if (a[posy + i][posx + 3] !=  . ) continue; n     ret |= rec(posy + i, posx + 3); n   } n   return ret; n } n inline void solve(void) { n   int t; n   cin >> t; n   while (t--) { n     memset(dp, -1, sizeof dp); n     cin >> n >> k; n     for (int i = (0); i < (3); i++) cin >> a[i]; n     for (int i = (0); i < (3); i++) n       for (int j = (0); j < (7); j++) a[i].push_back( . ); n     int posy, posx; n     for (int i = (0); i < (3); i++) n       for (int j = (0); j < (n); j++) n         if (a[i][j] ==  s ) posx = j, posy = i; n     if (rec(posy, posx)) { n       cout <<  YES n ; n     } else n       cout <<  NO n ; n   } n   return; n } n void init() { n   ios::sync_with_stdio(false); n   cin.tie(nullptr); n } n int main(int argc, const char *argv[]) { n   init(); n   solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n namespace std { n template <typename T, typename U> n struct hash<pair<T, U>> { n   hash<T> t; n   hash<U> u; n   size_t operator()(const pair<T, U>& p) const { n     return t(p.first) ^ (u(p.second) << 7); n   } n }; n }  // namespace std n template <typename T, typename F> n T bsh(T l, T h, const F& f) { n   T r = -1, m; n   while (l <= h) { n     m = (l + h) / 2; n     if (f(m)) { n       l = m + 1; n       r = m; n     } else { n       h = m - 1; n     } n   } n   return r; n } n template <typename T, typename F> n T bsl(T l, T h, const F& f) { n   T r = -1, m; n   while (l <= h) { n     m = (l + h) / 2; n     if (f(m)) { n       h = m - 1; n       r = m; n     } else { n       l = m + 1; n     } n   } n   return r; n } n template <typename T> n T gcd(T a, T b) { n   if (a < b) swap(a, b); n   return b ? gcd(b, a % b) : a; n } n int main(int, char**) { n   ios_base::sync_with_stdio(false); n   int N; n   cin >> N; n   ; n   int S; n   cin >> S; n   ; n   vector<int> A(N); n   for (auto& _cinv : A) cin >> _cinv; n   ; n   int err = 0; n   if (A[S - 1] != 0) { n     ++err; n     A[S - 1] = 0; n   } n   vector<int> Q(N, 0); n   for (int i = 0; i < N; i++) { n     Q[A[i]]++; n   } n   int i = 1; n   for (; i < N && (Q[0] > 1 || Q[i] > 0); ++i) { n     if (Q[i] == 0) { n       --Q[0]; n       ++Q[i]; n       ++err; n     } n   } n   int j = N - 1; n   while (i < j) { n     if (Q[j] == 0) n       --j; n     else if (Q[i] > 0) n       ++i; n     else { n       Q[i]++; n       Q[j]--; n       err++; n     } n   } n   cout << err << endl; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 5000 + 10; n bitset<MAXN> kna; n vector<int> G[MAXN]; n int n, a, b, c, d, p[MAXN], sze[MAXN]; n bool vis[MAXN]; n bool check(int a, int b) { n   memset(vis, 0, sizeof(vis)); n   kna.reset(); n   kna.flip(0); n   for (int u = a; p[u]; u = p[u]) vis[u] = true; n   for (int u = b; p[u]; u = p[u]) vis[u] = true; n   for (int u = a; u; u = p[u]) n     for (auto i : G[u]) n       if (!vis[i]) { n         kna |= kna << sze[i]; n       } n   return kna.test(sze[1] / 2 - 1); n } n int main() { n   scanf( %d%d%d%d%d , &n, &a, &b, &c, &d); n   for (int i = 1; i <= n; i++) G[i].clear(); n   for (int i = 2; i <= n; i++) { n     scanf( %d , &p[i]); n     G[p[i]].push_back(i); n   } n   for (int u = n; u > 1; u--) { n     sze[u] = max(sze[u], 1); n     sze[p[u]] += sze[u]; n   } n   if ((sze[1] % 2 == 0) && check(a, b) && check(c, d)) n     puts( YES n ); n   else n     puts( NO n ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long int k, i, cn = 0, cnt = 0, j; n   cin >> k; n   string s; n   cin >> s; n   map<long long int, long long int> ans; n   long long int a[s.length()], n = s.length(); n   for (i = 0; i < s.length(); i++) { n     if (s[i] ==  1 ) cnt++; n   } n   if (k > cnt) n     cout <<  0  << endl; n   else { n     a[0] = (s[0] -  0 ); n     for (i = 1; i < s.length(); i++) { n       a[i] = a[i - 1] + (s[i] -  0 ); n     } n     i = 0; n     j = 0; n     ans[0] = 1; n     for (i = 0; i < n; i++) { n       if (a[i] >= k) cn += ans[a[i] - k]; n       ans[a[i]]++; n     } n     cout << cn << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long n, k, p, a; n   cin >> n >> k >> p; n   if (n % 2 && k) { n     n--; n     k--; n   } n   if (2 * k > n) { n     long long t = 2 * k - n; n     n -= t; n     k -= t; n   } n   while (p--) { n     cin >> a; n     if ((a > n) || ((a % 2 == 0) && ((n - a) / 2 < k))) n       putchar( X ); n     else n       putchar( . ); n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, i, nu, ul, ka, c1ul, c1ka, c2ul, c2ka; n   cin >> n; n   c1ul = c1ka = c2ul = c2ka = 0; n   for (i = 0; i < n; i++) { n     cin >> nu >> ul >> ka; n     if (nu == 1) { n       c1ul += ul; n       c1ka += ka; n     } else { n       c2ul += ul; n       c2ka += ka; n     } n   } n   if (c1ul >= (c1ul + c1ka) / 2) { n     cout <<  LIVE  << endl; n   } else { n     cout <<  DEAD  << endl; n   } n   if (c2ul >= (c2ul + c2ka) / 2) { n     cout <<  LIVE  << endl; n   } else { n     cout <<  DEAD  << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int shr[10], mor[10], shrc[10], morc[10], n, i; n   cin >> n; n   for (i = 0; i < 10; i++) { n     shr[i] = 0; n     mor[i] = 0; n     shrc[i] = 0; n     morc[i] = 0; n   } n   char sher[n + 1], mr[n + 1]; n   cin >> sher >> mr; n   for (i = 0; i < n; i++) { n     shr[sher[i] -  0 ]++; n     mor[mr[i] -  0 ]++; n     shrc[sher[i] -  0 ]++; n     morc[mr[i] -  0 ]++; n   } n   long sherlockmax = 0, sheri = 0, mori = 0, minmori = 0; n   while (sheri < 10 && mori < 10) { n     while (shr[sheri] == 0 && sheri < 10) sheri++; n     while (mor[mori] == 0 && mori < 10) mori++; n     while (sheri >= mori && mori < 10) { n       mori++; n     } n     if (mori < 10 && sheri < 10 && mori > sheri) { n       sherlockmax += min(shr[sheri], mor[mori]); n       long k = min(shr[sheri], mor[mori]); n       shr[sheri] -= k; n       mor[mori] -= k; n     } n   } n   sheri = 0; n   mori = 0; n   while (sheri < 10 && mori < 10) { n     while (shrc[sheri] == 0 && sheri < 10) sheri++; n     while (morc[mori] == 0 && mori < 10) mori++; n     while (sheri > mori && mori < 10) { n       minmori += morc[mori]; n       mori++; n     } n     if (mori < 10 && sheri < 10 && mori >= sheri) { n       long k = min(shrc[sheri], morc[mori]); n       shrc[sheri] -= k; n       morc[mori] -= k; n     } n   } n   cout << minmori << endl; n   cout << sherlockmax << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n char a; n int n, sum, flag = 1; n int main() { n   cin >> n; n   for (int i = 0; i < n; i++) { n     cin >> a; n     if (a !=  0 ) n       sum++, flag = 1; n     else if (flag == 1 && a ==  0 ) { n       cout << sum; n       sum = 0; n       flag = 0; n     } else if (a ==  0  && flag == 0) n       cout <<  0 ; n   } n   cout << sum; n } n


#include <bits/stdc++.h> n using namespace std; n struct node { n   int a[4], index; n   node() { memset(a, 0, sizeof(a)); } n } no1[100005]; n int n; n int ans = 0, flag = 1, indexa, indexb; n vector<pair<int, int> > ve; n map<pair<int, int>, int> mp; n int l = 0; n set<pair<int, int> > se[1000000]; n void update(int a, int b, int c, int index) { n   set<pair<int, int> >::iterator it; n   int pre = mp[make_pair(a, b)]; n   it = se[pre].end(); n   if (it != se[pre].begin()) { n     it--; n     if (min(min(a, b), it->first + c) > ans) { n       ans = min(min(a, b), it->first + c); n       flag = 2; n       indexa = it->second; n       indexb = index; n     } n   } n } n int main() { n   scanf( %d , &n); n   for (int i = 0; i < n; i++) { n     int a, b, c; n     scanf( %d%d%d , &no1[i].a[0], &no1[i].a[1], &no1[i].a[2]); n     a = no1[i].a[0]; n     b = no1[i].a[1]; n     c = no1[i].a[2]; n     ve.push_back(make_pair(min(a, b), max(a, b))); n     ve.push_back(make_pair(min(a, c), max(a, c))); n     ve.push_back(make_pair(min(c, b), max(c, b))); n     no1[i].index = i + 1; n     if (min(no1[i].a[0], min(no1[i].a[1], no1[i].a[2])) > ans) { n       ans = min(no1[i].a[0], min(no1[i].a[1], no1[i].a[2])); n       indexa = i + 1; n     } n   } n   sort(ve.begin(), ve.end()); n   mp[ve[0]] = l; n   for (int i = 1; i < ve.size(); i++) { n     if (ve[i].first != ve[i - 1].first || ve[i].second != ve[i - 1].second) l++; n     mp[ve[i]] = l; n   } n   for (int i = 0; i < n; i++) { n     int a = no1[i].a[0]; n     int b = no1[i].a[1]; n     int c = no1[i].a[2]; n     update(min(a, b), max(a, b), c, i + 1); n     update(min(a, c), max(a, c), b, i + 1); n     update(min(b, c), max(b, c), a, i + 1); n     se[mp[make_pair(min(a, b), max(a, b))]].insert(make_pair(c, i + 1)); n     se[mp[make_pair(min(c, b), max(c, b))]].insert(make_pair(a, i + 1)); n     se[mp[make_pair(min(a, c), max(a, c))]].insert(make_pair(b, i + 1)); n   } n   printf( %d n , flag); n   if (flag == 1) { n     printf( %d n , indexa); n   } else { n     printf( %d %d n , indexa, indexb); n   } n   return 0; n } n


#include <bits/stdc++.h> n #pragma GCC optimize( O2 ) n using namespace std; n const int N = 1e6 + 10; n const long long mod = 1e9 + 7; n const long long mod2 = 998244353; n const long long inf = 8e18; n const int LOG = 22; n long long pw(long long a, long long b, long long M) { n   return (!b ? 1 n              : (b & 1 ? (a * pw(a * a % M, b / 2, M)) % M n                       : pw(a * a % M, b / 2, M))); n } n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   ; n   string s; n   cin >> s; n   int n = (int)s.size(); n   long long ans = 0; n   for (int l = 0; l < n; l++) { n     ans += max(0, n - l - 10); n     if (l + 2 > n) continue; n     for (int r = l + 2; r < min(l + 10, n); r++) { n       bool is_OK = 0; n       for (int x1 = l; x1 <= r; x1++) { n         for (int x2 = x1 + 1; x2 <= r; x2++) { n           if (s[x1] == s[x2] && 2 * x2 - x1 <= r && s[2 * x2 - x1] == s[x1]) { n             is_OK = 1; n           } n         } n       } n       ans += is_OK; n     } n   } n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<int> edge[100005]; n int visited[100005][2]; n vector<int> ans; n int draw = 1; n int visit(int verId, int isPetyaTurn) { n   visited[verId][isPetyaTurn] = 1; n   if (edge[verId].size() == 0) { n     draw = 0; n     if (isPetyaTurn == 0) { n       ans.push_back(verId); n       return 1; n     } n   } n   for (int i = 0; i < edge[verId].size(); ++i) { n     int nextVerId = edge[verId][i]; n     if (visited[nextVerId][1 - isPetyaTurn] == 0 && n         visit(nextVerId, 1 - isPetyaTurn)) { n       ans.push_back(verId); n       return 1; n     } n   } n   return 0; n } n int canDrawVisited[100005]; n int canDraw(int verId, int step) { n   if (canDrawVisited[verId] == 1) { n     return 1; n   } n   if (step == 1000000) { n     return 1; n   } n   canDrawVisited[verId] = 1; n   for (int i = 0; i < edge[verId].size(); ++i) { n     int nextVerId = edge[verId][i]; n     if (canDraw(nextVerId, step + 1)) { n       return 1; n     } n   } n   canDrawVisited[verId] = 0; n   return 0; n } n int main() { n   int n, m; n   scanf( %d%d , &n, &m); n   for (int i = 1; i <= n; ++i) { n     int nEdge; n     scanf( %d , &nEdge); n     while (nEdge--) { n       int tmp; n       scanf( %d , &tmp); n       edge[i].push_back(tmp); n     } n   } n   int begVerId; n   scanf( %d , &begVerId); n   int res = visit(begVerId, 1); n   if (draw) { n     puts( Draw ); n   } else if (res) { n     puts( Win ); n     for (int i = ans.size() - 1; i >= 0; --i) { n       printf( %d%c , ans[i],    n [i == 0]); n     } n   } else { n     int draw = canDraw(begVerId, 0); n     if (draw) { n       puts( Draw ); n     } else { n       puts( Lose ); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int inf = 2000000000; n static inline int Rint() { n   struct X { n     int dig[256]; n     X() { n       for (int i =  0 ; i <=  9 ; ++i) dig[i] = 1; n       dig[ - ] = 1; n     } n   }; n   static X fuck; n   int s = 1, v = 0, c; n   for (; !fuck.dig[c = getchar()];) n     ; n   if (c ==  - ) n     s = 0; n   else if (fuck.dig[c]) n     v = c ^ 48; n   for (; fuck.dig[c = getchar()]; v = v * 10 + (c ^ 48)) n     ; n   return s ? v : -v; n } n template <typename T> n static inline void cmax(T& a, const T& b) { n   if (b > a) a = b; n } n template <typename T> n static inline void cmin(T& a, const T& b) { n   if (b < a) a = b; n } n struct Pt { n   int x, y; n }; n int cmp_x_min(const Pt& a, const Pt& b) { n   if (a.x != b.x) return a.x < b.x; n   return a.y < b.y; n } n Pt data[100005]; n int main() { n   int n = Rint(), m = Rint(); n   for (int i = 0; i < m; ++i) data[i].x = Rint(), data[i].y = Rint(); n   sort(data, data + m, cmp_x_min); n   vector<int> last; n   last.push_back(1); n   last.push_back(1); n   int row = 0; n   for (int now = 0; now < m;) { n     int j = now + 1; n     while (j < m && data[j].x == data[now].x) ++j; n     if (last.size() && data[now].x > row + 1) { n       vector<int> next; n       next.push_back(last.front()), next.push_back(n); n       last.swap(next); n       row = row + 1; n     } n     const int size = ((int)(last).size()); n     vector<int> next; n     for (int k = now, pos = 0; k <= j; ++k) { n       int r = k == j ? n : data[k].y - 1; n       int l = k == now ? 1 : data[k - 1].y + 1; n       if (l > r) continue; n       while (pos < size && last[pos + 1] < l) pos += 2; n       if (pos < size) { n         if (last[pos] > r) continue; n         if (last[pos] <= l) { n           next.push_back(l); n           next.push_back(r); n         } else if (last[pos] <= r) { n           next.push_back(last[pos]); n           next.push_back(r); n         } n       } n     } n     last.swap(next); n     row = data[now].x; n     now = j; n   } n   if (last.size() && data[m - 1].x < n) { n     vector<int> next; n     next.push_back(last.front()), next.push_back(n); n     last.swap(next); n     row = row + 1; n   } n   if (last.size()) { n     const int size = ((int)(last).size()); n     if (last[size - 2] <= n && last[size - 1] >= n) { n       printf( %d n , 2 * n - 2); n       return 0; n     } n   } n   puts( -1 ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int32_t main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   long long a; n   cin >> a; n   bitset<22> b(a); n   long long i, j, k; n   long long c = 0; n   vector<long long> v; n   for (i = 0; i <= 40; i++) { n     long long temp = a; n     temp++; n     if ((a & temp) == 0) { n       break; n     } n     if (i % 2) n       a++; n     else { n       bitset<22> z(a); n       for (j = 21; j >= 0; j--) { n         if (z[j] == 1) break; n       } n       for (; j >= 0; j--) n         if (z[j] == 0) break; n       v.push_back(j + 1); n       a = a ^ (long long)(pow(2, j + 1) - 1); n     } n   } n   cout << i << endl; n   for (auto g : v) cout << g <<    ; n   cout << endl; n } n


#include <bits/stdc++.h> n using namespace std; n const int INF = 1e9 + 7; n vector<int> eum; n int w[100005]; n int Euler(int num) { n   int p = 2, ans = 1; n   while (p * p <= num) { n     int ct = 1; n     while (num % p == 0) { n       ct *= p; n       num /= p; n     } n     if (ct > 1) ct = ct / p * (p - 1); n     ans *= ct; n     p++; n   } n   if (num > 1) ans *= num - 1; n   return ans; n } n long long power(int x, int t, long long m) { n   if (t == 0) return 1; n   long long cc = power(x, t >> 1, m); n   cc = cc * cc % m; n   if (t & 1) cc = cc * x % m; n   return cc; n } n int gcd(int m, int n) { return n == 0 ? m : gcd(n, m % n); } n int solve(int l, int r, int mid) { n   if (l > r) return 1; n   if (eum[mid] == 1) return 0; n   vector<int> cc; n   for (int k = l + 1; k <= r && k <= l + 4; k++) cc.push_back(w[k]); n   cc.push_back(1); n   for (int k = 0; k < (cc.size()); k++) n     if (cc[k] == 1) { n       long long cur = 1LL; n       for (int j = k - 1; j >= 0; j--) { n         if (cc[j] >= 30 || cc[j] >= 4 && cur >= 4) { n           cur = 30LL; n           break; n         } n         cur = power(cc[j], cur, 1LL << 62); n         if (cur > 30LL) { n           cur = 30LL; n           break; n         } n       } n       if (cur < 30LL) n         return power(w[l], cur, eum[mid]) % eum[mid]; n       else n         return power(w[l], solve(l + 1, r, mid + 1) + eum[mid + 1], eum[mid]); n     } n } n int main() { n   int n, m, q, x, l, r; n   scanf( %d%d , &n, &m); n   eum.push_back(m); n   while ((x = eum.back()) > 1) eum.push_back(Euler(x)); n   for (int k = 0; k < (n); k++) scanf( %d , &w[k]); n   scanf( %d , &q); n   for (int k = 0; k < (q); k++) { n     scanf( %d%d , &l, &r); n     l--; n     r--; n     printf( %d n , solve(l, r, 0)); n   } n   return 0; n } n


#include <bits/stdc++.h> n #pragma comment(linker,  /STACK:1000000000 ) n #pragma GCC optimize( Ofast,no-stack-protector ) n using namespace std; n const int maxn = (int)5e5 + 10, maxlog = 19; n vector<pair<int, int> > ed[maxn]; n int par[maxlog][maxn]; n int wup[maxn]; n int h[maxn]; n int pr[maxn]; n int getp(int x) { return (pr[x] == x ? x : pr[x] = getp(pr[x])); } n int x[maxn], y[maxn]; n int inc[maxn], aksldal[maxn]; n void dfs(int v, int p, int we, int hh) { n   h[v] = hh; n   par[0][v] = p; n   wup[v] = we; n   for (int i = 1; i < maxlog; i++) { n     par[i][v] = par[i - 1][par[i - 1][v]]; n   } n   for (auto [u, num] : ed[v]) { n     if (u != p) { n       if (v == 0 || num > we) { n         aksldal[u] = aksldal[v] + 1; n       } else { n         aksldal[u] = 1; n       } n       if (v == 0 || num < we) { n         inc[u] = inc[v] + 1; n       } else { n         inc[u] = 1; n       } n       dfs(u, v, num, hh + 1); n     } n   } n } n int lca(int x, int y) { n   if (h[x] < h[y]) { n     swap(x, y); n   } n   for (int i = 0; i < maxlog; i++) { n     if (((h[x] - h[y]) >> i) & 1) { n       x = par[i][x]; n     } n   } n   if (x == y) { n     return x; n   } n   for (int i = maxlog - 1; i >= 0; i--) { n     if (par[i][x] != par[i][y]) { n       x = par[i][x]; n       y = par[i][y]; n     } n   } n   return par[0][x]; n } n int up(int x, int d) { n   for (int i = 0; i < maxlog; i++) { n     if ((d >> i) & 1) { n       x = par[i][x]; n     } n   } n   return x; n } n bool tr[maxn]; n int f[maxn]; n int val[maxn]; n int main() { n   int n, m; n   cin >> n >> m; n   for (int i = 0; i < n; i++) { n     pr[i] = i; n   } n   for (int i = 0; i < m; i++) { n     scanf( %d %d , &x[i], &y[i]); n     x[i]--; n     y[i]--; n   } n   for (int i = m - 1; i >= 0; i--) { n     int a = x[i]; n     int b = y[i]; n     int pa = getp(a); n     int pb = getp(b); n     if (pa != pb) { n       ed[a].push_back(make_pair(b, i)); n       ed[b].push_back(make_pair(a, i)); n       pr[pa] = pb; n       tr[i] = true; n     } n   } n   dfs(0, 0, -1, 0); n   for (int i = 0; i < n; i++) { n     val[i] = 1; n   } n   for (int i = m - 1; i >= 0; i--) { n     int a = x[i]; n     int b = y[i]; n     if (tr[i]) { n       f[i] = val[a] + val[b]; n       val[a] = val[b] = f[i]; n       continue; n     } else { n       int l = lca(a, b); n       int x = val[a] + val[b]; n       if (b == l) { n         swap(a, b); n       } n       if (a == l) { n         int d = inc[b]; n         if (h[b] - h[a] <= d) { n           d = h[b] - h[a]; n         } n         int m = up(b, d - 1); n         if (aksldal[m] >= h[m] - h[a]) { n           x -= f[wup[m]]; n         } n       } else { n         int da = inc[a]; n         int db = inc[b]; n         if (h[a] - h[l] <= da && h[b] - h[l] <= db) { n           int pa = up(a, h[a] - h[l] - 1); n           int pb = up(b, h[b] - h[l] - 1); n           x -= f[max(wup[pa], wup[pb])]; n         } else { n           if (h[a] - h[l] <= da) { n             swap(a, b); n             swap(da, db); n           } n           if (h[b] - h[l] <= db) { n             int m = up(a, da - 1); n             if (aksldal[m] >= h[m] - h[l]) { n               int pa = up(a, h[a] - h[l] - 1); n               int pb = up(b, h[b] - h[l] - 1); n               if (wup[pb] < wup[pa]) { n                 x -= f[wup[m]]; n               } n             } n           } n         } n       } n       f[i] = x; n       val[a] = val[b] = x; n     } n   } n   for (int i = 0; i < n; i++) { n     printf( %d  , val[i] - 1); n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e5 + 9, M = 998244353; n int dp[2][205][2], n, a[N]; n long long sum[2][205][2]; n int f(int i, int x, int z) { n   if (a[i] > 0 && x != a[i]) return dp[i % 2][x][z] = 0; n   if (i == n - 1) return dp[i % 2][x][z] = z; n   long long ans = 0, l = 0, r = 200; n   i %= 2; n   ans = z * (sum[1 - i][x - 1][1] - sum[1 - i][l][1]) % M + dp[1 - i][x][1] + n         (sum[1 - i][r][0] - sum[1 - i][x][0]) % M + 2 * M; n   return dp[i][x][z] = ans % M; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   long long i, j; n   cin >> n; n   for (i = 0; i < n; i++) cin >> a[i]; n   for (i = n - 1; i >= 0; i--) n     for (j = 1; j < 201; j++) { n       sum[i % 2][j][0] = f(i, j, 0) + sum[i % 2][j - 1][0]; n       sum[i % 2][j][1] = f(i, j, 1) + sum[i % 2][j - 1][1]; n     } n   cout << sum[0][200][0] % M << endl; n   return 0; n } n


#include <bits/stdc++.h> n #pragma comment(linker,  /STACK:1024000000,1024000000 ) n using namespace std; n const int mxn = 400005, maxnn = 100005; n const int mod = 1e9 + 7; n long long ans; n int n, m, num, cir, r, cnt; n int head[maxnn], que[maxnn]; n struct edge { n   int to, next; n   long long w; n } f[mxn << 2]; n long long p[70], circle[mxn], dis[maxnn], dig[2], pw[105]; n inline void add(int u, int v, long long w) { n   f[++cnt].to = v, f[cnt].w = w, f[cnt].next = head[u], head[u] = cnt; n } n inline void dfs(int u, int fa, long long now) { n   dis[u] = now, que[++num] = u; n   for (int i = head[u]; i; i = f[i].next) { n     int v = f[i].to; n     if (v == fa) continue; n     if (dis[v] == -1) n       dfs(v, u, dis[u] ^ f[i].w); n     else n       circle[++cir] = dis[u] ^ dis[v] ^ f[i].w; n   } n } n inline void init() { n   int i, j; n   r = 0; n   memset(p, 0, sizeof p); n   for (i = 1; i <= cir; i++) { n     long long x = circle[i]; n     for (j = 62; j >= 0; j--) { n       if (!(x >> j)) continue; n       if (!p[j]) { n         p[j] = x; n         break; n       } n       x ^= p[j]; n     } n   } n   for (j = 0; j <= 62; j++) n     if (p[j]) r++; n } n inline void calc() { n   init(); n   int i, j; n   for (j = 0; j <= 62; j++) { n     bool flag = 0; n     dig[0] = dig[1] = 0; n     for (i = 1; i <= num; i++) dig[(dis[que[i]] >> j) & 1]++; n     for (i = 0; i <= 62; i++) n       if ((p[i] >> j) & 1) { n         flag = 1; n         break; n       } n     long long now = n         (dig[0] * (dig[0] - 1) / 2 + dig[1] * (dig[1] - 1) / 2) % mod; n     if (flag) { n       if (r) now = now * pw[r - 1] % mod; n       now = now * pw[j] % mod; n       ans = (ans + now) % mod; n     } n     now = dig[0] * dig[1] % mod; n     if (flag) { n       if (r) now = now * pw[r - 1] % mod; n     } else n       now = now * pw[r] % mod; n     now = now * pw[j] % mod; n     ans = (ans + now) % mod; n   } n } n int main() { n   int i, j, u, v; n   long long w; n   memset(dis, -1, sizeof dis); n   pw[0] = 1; n   for (j = 1; j <= 100; j++) pw[j] = pw[j - 1] * 2 % mod; n   scanf( %d%d , &n, &m); n   for (i = 1; i <= m; i++) { n     scanf( %d%d%lld , &u, &v, &w); n     add(u, v, w), add(v, u, w); n   } n   for (i = 1; i <= n; i++) n     if (dis[i] == -1) { n       num = cir = 0; n       dfs(i, 0, 0); n       calc(); n     } n   printf( %lld , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   long long n; n   cin >> n; n   vector<long long> x(n); n   for (long long i = 0; i < n; i++) cin >> x[i]; n   unordered_map<int, int> m; n   for (auto num : x) { n     m[num]++; n   } n   if (m.size() < 3) { n     cout << 0; n     return 0; n   } n   int ans = INT_MAX; n   for (auto num : m) { n     if (ans > num.second) { n       ans = num.second; n     } n   } n   printf( %d n , ans); n   vector<vector<int>> v(4); n   for (int i = 0; i < n; i++) { n     if (x[i] == 1) v[1].push_back(i + 1); n     if (x[i] == 2) v[2].push_back(i + 1); n     if (x[i] == 3) v[3].push_back(i + 1); n   } n   for (long long i = 0; i < ans; i++) { n     printf( %d %d %d n , v[1][i], v[2][i], v[3][i]); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct pokr { n   double p1; n   double p2; n   double len; n   pokr(double _p1, double _p2, double _l) : p1(_p1), p2(_p2), len(_l) {} n   pokr() { p1 = p2 = len = 0; } n } d[2100][2100]; n int x[2100]; n int c[2100][2100]; n int a[2100]; n int b[2100]; n double p, q; n int h; n pokr get_ans(int l, int r) { n   pokr res; n   if (l > r) return pokr(0, 0, 0); n   if (l == r) return pokr(p, q, 0); n   if (c[l][r]) return d[l][r]; n   pokr temp = get_ans(l + 1, r); n   res.p1 += p / 2; n   res.p2 += temp.p2 * p / 2; n   res.len += p / 2 * temp.len; n   res.len += p / 2 * temp.p1 * min(x[l + 1] - x[l], h); n   int next = b[l]; n   if (next <= r) { n     pokr temp = get_ans(next, r); n     res.p2 += q / 2 * temp.p2; n     res.len += q / 2 * temp.len; n     res.len += q / 2 * (x[next - 1] + h - x[l]); n     res.len += q / 2 * temp.p1 * min(x[next] - x[next - 1] - h, h); n   } else { n     res.p2 += q / 2; n     res.len += q / 2 * (x[r] - x[l]); n   } n   temp = get_ans(l, r - 1); n   res.p2 += q / 2; n   res.p1 += temp.p1 * q / 2; n   res.len += q / 2 * temp.len; n   res.len += q / 2 * temp.p2 * min(x[r] - x[r - 1], h); n   int prev = a[r]; n   if (prev >= l) { n     pokr temp = get_ans(l, prev); n     res.p1 += p / 2 * temp.p1; n     res.len += p / 2 * temp.len; n     res.len += p / 2 * (x[r] - x[prev + 1] + h); n     res.len += p / 2 * temp.p2 * min(x[prev + 1] - x[prev] - h, h); n   } else { n     res.p1 += p / 2; n     res.len += p / 2 * (x[r] - x[l]); n   } n   c[l][r] = 1; n   d[l][r] = res; n   return res; n } n int main() { n   int n; n   cin >> n >> h; n   cin >> p; n   q = 1 - p; n   for (int i = 0; i < n; i++) cin >> x[i]; n   sort(x, x + n); n   a[0] = -1; n   for (int i = 1; i < n; i++) n     if (x[i] - x[i - 1] < h) n       a[i] = a[i - 1]; n     else n       a[i] = i - 1; n   b[n - 1] = n; n   for (int i = n - 2; i >= 0; i--) n     if (x[i + 1] - x[i] < h) n       b[i] = b[i + 1]; n     else n       b[i] = i + 1; n   pokr r = get_ans(0, n - 1); n   printf( %.12f , (r.p1 + r.p2) * h + r.len); n   return 0; n } n


#include <bits/stdc++.h> n #pragma GCC optimize( Ofast,unroll-loops ) n #pragma GCC target( avx,avx2,fma ) n using namespace std; n template <typename T> n using vc = vector<T>; n template <typename T> n using uset = unordered_set<T>; n template <typename A, typename B> n using umap = unordered_map<A, B>; n template <typename T> n using pq = priority_queue<T>; n template <typename T> n using minpq = priority_queue<T, vc<T>, greater<T>>; n template <typename T> n inline void sort(vc<T>& v) { n   sort(v.begin(), v.end()); n } n template <typename T, typename Comp> n inline void sort(vc<T>& v, Comp c) { n   sort(v.begin(), v.end(), c); n } n using ll = long long; n using ull = unsigned long long; n using pii = pair<ll, ll>; n using vi = vc<ll>; n using vii = vc<pii>; n constexpr char el =   n ; n constexpr char sp =    ; n constexpr ll INF = 0x3f3f3f3f; n constexpr ll LLINF = 0x3f3f3f3f3f3f3f3fLL; n ll xh, yh, xs, ys, n; n inline bool evan(ll a, ll b) { return a < 0 && b > 0 || a > 0 && b < 0; } n signed main() { n   ios_base::sync_with_stdio(false); n   cin.tie(nullptr); n   cout << fixed; n   cin >> xh >> yh; n   cin >> xs >> ys; n   cin >> n; n   ll ans = 0; n   while (n--) { n     ll a, b, c; n     cin >> a >> b >> c; n     if (evan(a * xh + b * yh + c, a * xs + b * ys + c)) ans++; n   } n   cout << ans; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 26; n vector<int> kmp; n string txt, pattern; n vector<vector<int> > nxt, dp; n void build() { n   kmp.resize(pattern.size() + 10); n   kmp[0] = 0; n   for (int i = 1, j = 0; i < pattern.size();) { n     if (pattern[i] == pattern[j]) n       kmp[i++] = ++j; n     else if (!j) n       kmp[i++] = 0; n     else n       j = kmp[j - 1]; n   } n   nxt.resize(pattern.size() + 10); n   for (int i = 0; i < pattern.size(); ++i) nxt[i].resize(N + 10); n   for (int j = 0; j < N; ++j) nxt[0][j] = (pattern[0] -  a  == j); n   for (int i = 1; i < pattern.size(); ++i) n     for (int j = 0; j < N; ++j) n       nxt[i][j] = (pattern[i] -  a  == j) ? i + 1 : nxt[kmp[i - 1]][j]; n   dp.resize(txt.size() + 10); n   for (int i = 0; i < dp.size(); ++i) dp[i].resize(pattern.size() + 10); n   for (int i = 0; i < dp.size(); ++i) n     for (int j = 0; j < dp[i].size(); ++j) dp[i][j] = -1; n } n int solve(const int &i, const int &j) { n   if (i == txt.size()) return 0; n   if (dp[i][j] != -1) return dp[i][j]; n   int ans = 0; n   if (txt[i] ==  ? ) { n     for (int k = 0; k < N; ++k) { n       int new_j = nxt[j][k]; n       if (new_j == pattern.size()) n         ans = max(ans, 1 + solve(i + 1, kmp[new_j - 1])); n       else n         ans = max(ans, solve(i + 1, new_j)); n     } n   } else { n     int k = txt[i] -  a ; n     int new_j = nxt[j][k]; n     if (new_j == pattern.size()) n       ans = max(ans, 1 + solve(i + 1, kmp[new_j - 1])); n     else n       ans = max(ans, solve(i + 1, new_j)); n   } n   return dp[i][j] = ans; n } n int main() { n   cin >> txt >> pattern; n   build(); n   printf( %d n , solve(0, 0)); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n map<int, int> mp; n set<int> s; n int main(void) { n   int q; n   scanf( %d , &q); n   while (q--) { n     mp.clear(); n     int x; n     scanf( %d , &x); n     int t = x; n     int lim = sqrt(t); n     for (int i = 2; i <= lim; i++) { n       while (t % i == 0) { n         mp[i]++; n         t /= i; n       } n     } n     if (1 < t) mp[t]++; n     s.clear(); n     for (int i = 2; i <= lim; i++) { n       if (x % i == 0) { n         s.insert(i); n         s.insert(x / i); n       } n     } n     s.insert(x); n     vector<int> rep; n     for (auto e : mp) rep.push_back(e.first); n     if (rep.size() == 2) { n       int mult = 1; n       for (auto e : mp) mult *= e.first; n       vector<int> ds; n       if (mult == x) { n         printf( %d  , mult); n         for (int p : rep) { n           for (int div : s) { n             if (div % p == 0) { n               if (div != mult) { n                 printf( %d  , div); n               } n               ds.push_back(div); n             } n           } n           for (int e : ds) s.erase(e); n           ds.clear(); n         } n         printf(  n1 n ); n       } else { n         printf( %d  , x); n         vector<int> ds; n         for (int p : rep) { n           for (int div : s) { n             if (div % p == 0) { n               if (div != mult) { n                 if (div != x) { n                   printf( %d  , div); n                 } n               } n               ds.push_back(div); n             } n           } n           for (int e : ds) s.erase(e); n           ds.clear(); n           if (p != rep.back()) printf( %d  , mult); n         } n         printf(  n0 n ); n       } n     } else if (rep.size() == 1) { n       for (int div : s) { n         printf( %d  , div); n       } n       printf(  n0 n ); n     } else { n       map<int, int> joint; n       for (int i = 0; i < rep.size(); i++) { n         joint[rep[i] * rep[(i + 1) % rep.size()]] = true; n       } n       vector<int> ds; n       for (int i = 0; i < rep.size(); i++) { n         for (int div : s) { n           if (div % rep[i] == 0) { n             if (joint.count(div) == 0) { n               printf( %d  , div); n             } n             ds.push_back(div); n           } n         } n         printf( %d  , rep[i] * rep[(i + 1) % rep.size()]); n         for (int e : ds) s.erase(e); n         ds.clear(); n       } n       printf(  n0 n ); n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n bool isSquare(long long n) { n   const long long s = (long long)ceil(sqrt(n)); n   return (s * s) == n; n } n long long calcCostToSquare(long long n) { n   if (isSquare(n)) return 0; n   const auto f1 = (long long)ceil(sqrt(n)); n   const auto f2 = (long long)floor(sqrt(n)); n   return min((long long)abs(f1 * f1 - n), (long long)abs(f2 * f2 - n)); n } n long long calcCostToNonSquare(long long n) { n   if (n == 0) return 2; n   if (isSquare(n)) return 1; n   return 0; n } n int main() { n   long long n; n   cin >> n; n   long long numSquare = 0; n   multiset<long long> toMakeSquare; n   multiset<long long> toMakeNonSquare; n   for (long long i = 0; i < n; ++i) { n     long long a; n     cin >> a; n     if (isSquare(a)) ++numSquare; n     toMakeSquare.insert(calcCostToSquare(a)); n     toMakeNonSquare.insert(calcCostToNonSquare(a)); n   } n   n /= 2; n   long long result = 0; n   if (numSquare < n) { n     auto it = toMakeSquare.upper_bound(0); n     while (numSquare < n) { n       result += *it; n       ++it; n       ++numSquare; n     } n   } else { n     auto it = toMakeNonSquare.upper_bound(0); n     while (numSquare > n) { n       result += *it; n       ++it; n       --numSquare; n     } n   } n   cout << result << endl; n } n


#include <bits/stdc++.h> n using namespace std; n long long num_to_sig(long long x) { n   vector<int> v; n   while (x > 0) { n     v.push_back((x % 10) % 2); n     x /= 10; n   } n   int n = v.size(); n   long long ret = 0; n   for (int i = n - 1; i >= 0; i--) { n     ret *= 10; n     ret += v[i]; n   } n   return ret; n } n int main() { n   int Q; n   scanf( %d , &Q); n   map<long long, int> cnt; n   while (Q--) { n     char c; n     scanf(  %c , &c); n     long long x; n     if (c ==  + ) { n       scanf( %lld , &x); n       cnt[num_to_sig(x)]++; n     } else if (c ==  - ) { n       scanf( %lld , &x); n       cnt[num_to_sig(x)]--; n     } else { n       scanf( %lld , &x); n       printf( %d n , cnt[num_to_sig(x)]); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<long long> sp; n vector<pair<long long, long long>> bases; n int main() { n   int s, b; n   cin >> s >> b; n   long long num, num2; n   for (int i = 0; i < s; i++) { n     cin >> num; n     sp.push_back(num); n   } n   for (int i = 0; i < b; i++) { n     cin >> num >> num2; n     bases.push_back({num, num2}); n   } n   sort(bases.begin(), bases.end()); n   long long sum[b + 5]; n   for (int i = 0; i < b; i++) { n     sum[i] = bases[i].second; n     if (i > 0) sum[i] += sum[i - 1]; n   } n   for (int i = 0; i < s; i++) { n     long long bas = 0; n     long long start = 0, end = b, mid, idx = -1; n     while (start <= end) { n       mid = (start + end) / 2; n       if (bases[mid].first <= sp[i]) { n         idx = mid; n         start = mid + 1; n       } else { n         end = mid - 1; n       } n     } n     if (idx == b) idx--; n     if (idx == -1) n       cout <<  0  ; n     else n       cout << sum[idx] <<    ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long int mod = 1e9 + 7; n const double pie = 3.14159265358979323846; n const char nl =   n ; n template <typename Type> n istream &operator>>(istream &in, vector<Type> &v) { n   long long int n = v.size(); n   for (int i = 0; i < n; i++) in >> v[i]; n   return in; n } n template <typename Type> n ostream &operator<<(ostream &out, vector<Type> &v) { n   for (auto value : v) out << value <<    ; n   out << endl; n   return out; n } n void solve() { n   long long int n; n   cin >> n; n   vector<long long int> a(4 * n); n   cin >> a; n   sort(a.begin(), a.end()); n   long long int area = a[0] * a.back(); n   long long int c = 1, j = 4 * n - 1; n   for (long long int i = 0; c <= n;) { n     if (a[i] != a[i + 1] or a[j] != a[j - 1] or a[i] * a[j] != area) { n       cout <<  No n ; n       return; n     } n     ++c; n     i += 2; n     j -= 2; n   } n   cout <<  Yes n ; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   long long int t; n   cin >> t; n   while (t--) { n     solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e5 + 5; n int n, a[N], fa[N], head[N], p, deep[N]; n long long Chuan1[N], Chuan2[N], Ans; n queue<int> q; n struct wfh { n   int to, last; n } d[N << 1]; n inline void add(int a, int b) { d[++p] = (wfh){b, head[a]}, head[a] = p; } n inline long long max(long long A, long long B) { return A > B ? A : B; } n inline long long min(long long A, long long B) { return A < B ? A : B; } n void dfs(int u, int fa) { n   for (int i = head[u], v; i; i = d[i].last) { n     v = d[i].to; n     if (v == fa) continue; n     dfs(v, u); n     Chuan1[u] = max(Chuan1[u], Chuan1[v]); n     Chuan2[u] = max(Chuan2[u], Chuan2[v]); n   } n   a[u] += Chuan2[u] - Chuan1[u]; n   if (a[u] >= 0) n     Chuan1[u] += a[u]; n   else n     Chuan2[u] -= a[u]; n } n int main() { n   scanf( %d , &n); n   for (int i = 1, u, v; i < n; i++) scanf( %d%d , &u, &v), add(u, v), add(v, u); n   for (int i = 1; i <= n; i++) scanf( %d , &a[i]); n   dfs(1, -1); n   printf( %lld n , Chuan1[1] + Chuan2[1]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int a[111111]; n int b[111111]; n int main() { n   int n, m, c; n   scanf( %d%d%d , &n, &m, &c); n   for (int i = 0; i < n; i++) scanf( %d , &a[i]); n   for (int i = 0; i < m; i++) scanf( %d , &b[i]); n   for (int i = 1; i < m; i++) { n     b[i] = (b[i] + b[i - 1]) % c; n   } n   for (int i = 0; i < n; i++) { n     int f = max(0, m - (n - i)); n     int t = min(i, m - 1); n     int s = b[t] - (f == 0 ? 0 : b[f - 1]); n     s = (s % c + c) % c; n     a[i] = (a[i] + s) % c; n     cout << a[i] <<    ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long score(long long a, long long b) { n   if (a == b) n     return 0; n   else if ((a == 3 && b == 2) || (a == 2 && b == 1) || (a == 1 && b == 3)) n     return 1; n   else n     return 0; n } n int main(void) { n   ios::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   long long k, a, b, ca, cb, ncl, cl, csa, csb, pa, pb; n   long long d[4][4][4] = {}; n   long long ch[4][4][4]; n   long long sc[100][2] = {}; n   cin >> k >> a >> b; n   for (long long k = 1; k <= 2; k++) n     for (long long i = 1; i <= 3; i++) n       for (long long j = 1; j <= 3; j++) cin >> ch[i][j][k]; n   d[a][b][0] = 1; n   d[a][b][1] = score(a, b); n   d[a][b][2] = score(b, a); n   sc[1][0] = score(a, b); n   sc[1][1] = score(b, a); n   ca = a; n   cb = b; n   while (true) { n     long long na, nb; n     na = ch[ca][cb][1]; n     nb = ch[ca][cb][2]; n     if (d[na][nb][0] == 0) { n       d[na][nb][0] = d[ca][cb][0] + 1; n       d[na][nb][1] = d[ca][cb][1] + score(na, nb); n       d[na][nb][2] = d[ca][cb][2] + score(nb, na); n       sc[d[na][nb][0]][0] = d[na][nb][1]; n       sc[d[na][nb][0]][1] = d[na][nb][2]; n       ca = na; n       cb = nb; n     } else { n       ncl = d[na][nb][0] - 1; n       cl = d[ca][cb][0] - d[na][nb][0] + 1; n       csa = d[ca][cb][1] - d[na][nb][1] + score(na, nb); n       csb = d[ca][cb][2] - d[na][nb][2] + score(nb, na); n       break; n     } n   } n   if (k <= ncl) { n     pa = sc[k][0]; n     pb = sc[k][1]; n   } else { n     long long km = (k - ncl) % cl; n     pa = sc[ncl + km][0] + ((k - ncl) / cl) * csa; n     pb = sc[ncl + km][1] + ((k - ncl) / cl) * csb; n   } n   cout << pa <<     << pb << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n bool sortin(const pair<long long int, long long int> &e, n             const pair<long long int, long long int> &f) { n   return (e.first < f.first); n } n long long int i, j, k, l, m, n, c, p, q, ts, mn = 10e17; n long long int a[200002], b[200002], x[200002]; n int main() { n   { n     ios_base::sync_with_stdio(false); n     cin.tie(0); n     cout.tie(0); n   }; n   cin >> n; n   cin >> m; n   l = n - m; n   string s, t, u, v; n   cin >> s; n   cin >> t; n   v = s; n   for (long long int i = 0; i <= n - 1; i++) u +=  a ; n   for (long long int i = 0; i <= n - 1; i++) { n     if (!l) break; n     if (s[i] == t[i]) n       u[i] = s[i], v[i] =  . , l--; n     else n       u[i] =  a ; n   } n   if (l) { n     for (long long int i = 0; i <= n - 1; i++) { n       if (v[i] ==  . ) continue; n       if (c % 2) n         u[i] = s[i], v[i] =  . ; n       else n         u[i] = t[i], v[i] =  . ; n       c++; n       if (c % 2 == 0) l--; n       if (!l) break; n     } n   } n   if (l) { n     cout << -1; n     return 0; n   } n   for (long long int i = 0; i <= n - 1; i++) { n     if (v[i] ==  . ) { n       if (s[i] != u[i]) p++; n       if (t[i] != u[i]) q++; n       continue; n     } n     if (s[i] ==  a ) { n       u[i] =  b ; n       if (t[i] ==  b ) u[i] =  c ; n     } else if (t[i] ==  a ) { n       u[i] =  b ; n       if (s[i] ==  b ) u[i] =  c ; n     } n     if (s[i] != u[i]) p++; n     if (t[i] != u[i]) q++; n   } n   if (p != m || q != m) { n     cout << -1; n     return 0; n   } n   cout << u; n } n


#include <bits/stdc++.h> n using namespace std; n const long long N = 100000 + 10; n long long n, K, a[N], b[N]; n struct Node { n   long long v, id; n } p[N]; n bool cmp(Node x, Node y) { return x.v > y.v; } n long long get(long long x, long long v) { return -3ll * (x * x + x) + v + 1; } n long long func(long long v, long long y) { n   long long l = 0, r = v, mid, ret = 0; n   while (l <= r) { n     mid = (l + r) >> 1ll; n     if (get(mid, v) >= y) n       ret = mid, l = mid + 1; n     else n       r = mid - 1; n   } n   return ret; n } n long long calc(long long y) { n   long long sum = 0; n   for (long long i = 1; i <= n; ++i) sum += func(a[i], y); n   return sum; n } n long long ans, sum; n void solve() { n   long long l = -4e18, r = 1e18, mid; n   while (l <= r) { n     mid = (l + r) >> 1ll; n     if (calc(mid) <= K) n       ans = mid, r = mid - 1; n     else n       l = mid + 1; n   } n } n signed main() { n   scanf( %lld%lld , &n, &K); n   for (long long i = 1; i <= n; ++i) scanf( %lld , &a[i]); n   solve(); n   sum = 0; n   for (long long i = 1; i <= n; ++i) { n     b[i] = func(a[i], ans); n     sum += b[i]; n     p[i].v = get(b[i], a[i]); n     p[i].id = i; n   } n   sort(p + 1, p + n + 1, cmp); n   for (long long i = 1; i <= n; ++i) n     if (sum < K && a[p[i].id] > b[p[i].id]) sum++, b[p[i].id]++; n   for (long long i = 1; i <= n; ++i) printf( %lld  , b[i]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long arr[6]; n int main() { n   for (int i = 0; i < 6; ++i) { n     cin >> arr[i]; n   } n   long long res = 0; n   for (int i = 0; i < 3; ++i) { n     res += arr[i]; n   } n   res *= res; n   for (int i = 0; i < 6; i += 2) { n     res -= arr[i] * arr[i]; n   } n   cout << res; n } n


#include <bits/stdc++.h> n using namespace std; n signed solve(); n signed main() { n   ios_base::sync_with_stdio(false); n   cin.tie(nullptr); n   cout.tie(nullptr); n   int tests = 1; n   while (tests--) solve(); n   return 0; n } n signed solve() { n   int n, k; n   cin >> n >> k; n   if (n <= 1ll * k * (k - 1)) { n     cout <<  YES n ; n     for (int i = 0; i < n; ++i) { n       cout << (i % k) + 1 <<     << (i / k + i % k + 1) % k + 1 <<   n ; n     } n   } else { n     cout <<  NO n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename A, typename B> n ostream &operator<<(ostream &os, const pair<A, B> &p) { n   return os <<  (  << p.first <<  ,   << p.second <<  ) ; n } n template <typename T_container, typename T = typename enable_if< n                                     !is_same<T_container, string>::value, n                                     typename T_container::value_type>::type> n ostream &operator<<(ostream &os, const T_container &v) { n   os <<  { ; n   string sep; n   for (const T &x : v) os << sep << x, sep =  ,  ; n   return os <<  } ; n } n void dbg_out() { cerr << endl; } n template <typename Head, typename... Tail> n void dbg_out(Head H, Tail... T) { n   cerr <<     << H; n   dbg_out(T...); n } n const int MAX_N = 1e5 + 5; n const long long MOD = 1e9 + 7; n const long long INF = 1e9; n const long double EPS = 1e-9; n void solve() { n   string str; n   cin >> str; n   int len = ((int)str.size()); n   if (len & 1) n     cout <<  NO  << endl; n   else { n     for (int i = 0; i < len / 2; ++i) { n       if (str[i] != str[i + len / 2]) { n         cout <<  NO  << endl; n         return; n       } n     } n     cout <<  YES  << endl; n   } n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   int tc = 1; n   cin >> tc; n   for (int t = 1; t <= tc; t++) { 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[123456]; n   int avg[123456]; n   for (int i = 0; i < n; i++) cin >> a[i]; n   long long count = 0; n   for (int i = 0; i < n; i++) count += a[i]; n   int Max = count / n; n   int rem = count % n; n   long long c = 0; n   for (int i = 0; i < n; i++) avg[i] = Max; n   sort(a, a + n); n   for (int i = n - 1; i >= 0; i--) { n     if (!rem) continue; n     avg[i]++; n     rem--; n   } n   for (int i = 0; i < n; i++) n     if (a[i] > avg[i]) c += a[i] - avg[i]; n   cout << c << endl; n   return 0; n } n


#include <bits/stdc++.h> n using std::min; n const int MAXL = 3000000 + 9, MAXM = 5000 + 9, M = 4194304; n int tr[M + M]; n int ask(int k) { n   int i; n   for (i = 1; i < M;) n     if (k > tr[i + i]) { n       k -= tr[i + i]; n       i += i + 1; n     } else n       i += i; n   return i - M; n } n void change(int u, int v) { n   for (u += M; u; u >>= 1) tr[u] += v; n } n int l[MAXM], r[MAXM]; n int fa[MAXL]; n char s[MAXL], ans[MAXL]; n int main() { n   int k, m, i, j, t, tot; n   int ll, rr, ori; n   char *p; n   scanf( %s%d%d , s + 1, &k, &m); n   for (i = 1; i <= m; ++i) scanf( %d%d , l + i, r + i); n   for (i = 1; i <= k; ++i) change(i, 1); n   tot = k; n   for (i = m; i; --i) { n     if (r[i] + 1 > tot) continue; n     ll = r[i] - l[i] + 1; n     rr = min(tot, r[i] + ll); n     for (j = 1; j <= rr - r[i]; ++j) { n       m = ll >> 1; n       ori = j <= m ? l[i] - 1 + j + j : l[i] - 2 + 2 * (j - m); n       fa[t = ask(r[i] + 1)] = ask(ori); n       change(t, -1); n     } n     tot -= rr - r[i]; n   } n   p = s; n   for (i = 1; i <= k; ++i) ans[i] = fa[i] ? ans[fa[i]] : *++p; n   puts(ans + 1); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n inline int sn() { n   int n = 0; n   int ch = getchar(); n   while (ch < 48) ch = getchar(); n   while (ch > 47) n = (n << 3) + (n << 1) + ch -  0 , ch = getchar(); n   return n; n } n double dp[2005][2005]; n int main() { n   int i, j, n, t; n   double p; n   scanf( %d %lf %d , &n, &p, &t); n   dp[0][0] = 1; n   for (i = 1; i <= t; i++) { n     dp[i][0] = pow(1 - p, i); n     dp[i][n] = dp[i - 1][n] + dp[i - 1][n - 1] * p; n     for (j = 1; j <= n - 1; j++) { n       dp[i][j] = dp[i - 1][j] * (1 - p) + dp[i - 1][j - 1] * p; n     } n   } n   double ans = 0; n   for (i = 1; i <= n; i++) { n     ans = ans + i * dp[t][i]; n   } n   printf( %0.6lf  , ans); n   return 0; n } n


#include <bits/stdc++.h> n #pragma GCC optimize( O3 ) n using namespace std; n const double EPS = 1e-8; n const int mod = 1e9 + 7; n const int N = 3e6 + 10; n const long long INF = 1e18; n long long power(long long x, long long y) { n   long long t = 1; n   while (y > 0) { n     if (y % 2) n       y -= 1, t = t * x % mod; n     else n       y /= 2, x = x * x % mod; n   } n   return t; n } n long long dis[N]; n vector<pair<long long, long long> > G[N]; n int n, s, q; n void make(int C, int s, int e) { n   if (s == e) { n     G[C + n].push_back(make_pair(s, 0)); n     G[s].push_back(make_pair(C + n + 6 * n, 0)); n     return; n   } n   int m = (s + e) >> 1; n   make(C * 2, s, m); n   make(C * 2 + 1, m + 1, e); n   G[C + n].push_back(make_pair(C * 2 + n, 0)); n   G[C + n].push_back(make_pair(C * 2 + 1 + n, 0)); n   G[C * 2 + n + 6 * n].push_back(make_pair(C + n + 6 * n, 0)); n   G[C * 2 + 1 + n + 6 * n].push_back(make_pair(C + n + 6 * n, 0)); n } n void upd1(int C, int S, int E, int s, int e, int v, int w) { n   if (s > E || e < S) return; n   if (s <= S && E <= e) { n     G[v].push_back(make_pair(C + n, w)); n     return; n   } n   int m = (S + E) >> 1; n   upd1(C * 2, S, m, s, e, v, w); n   upd1(C * 2 + 1, m + 1, E, s, e, v, w); n } n void upd2(int C, int S, int E, int s, int e, int v, int w) { n   if (s > E || e < S) return; n   if (s <= S && E <= e) { n     G[C + n + 6 * n].push_back(make_pair(v, w)); n     return; n   } n   int m = (S + E) >> 1; n   upd2(C * 2, S, m, s, e, v, w); n   upd2(C * 2 + 1, m + 1, E, s, e, v, w); n } n priority_queue<pair<long long, long long> > P; n int main() { n   scanf( %d%d%d , &n, &q, &s); n   s--; n   make(1, 0, n - 1); n   for (int i = 0; i < q; ++i) { n     int c; n     scanf( %d , &c); n     if (c == 1) { n       int v, u, w; n       scanf( %d%d%d , &v, &u, &w); n       v--; n       u--; n       G[v].push_back(make_pair(u, w)); n     } else if (c == 2) { n       int v, l, r, w; n       scanf( %d%d%d%d , &v, &l, &r, &w); n       v--; n       l--; n       r--; n       upd1(1, 0, n - 1, l, r, v, w); n     } else { n       int v, l, r, w; n       scanf( %d%d%d%d , &v, &l, &r, &w); n       v--; n       l--; n       r--; n       upd2(1, 0, n - 1, l, r, v, w); n     } n   } n   for (int i = 0; i < N; ++i) dis[i] = INF; n   dis[s] = 0; n   P.push(make_pair(0, s)); n   while (!P.empty()) { n     pair<long long, long long> nx = P.top(); n     P.pop(); n     nx.first = -nx.first; n     if (dis[nx.second] < nx.first) continue; n     for (int i = 0; i < G[nx.second].size(); ++i) { n       long long cst = nx.first + G[nx.second][i].second; n       int v = G[nx.second][i].first; n       if (dis[v] > cst) { n         dis[v] = cst; n         P.push(make_pair(-cst, v)); n       } n     } n   } n   for (int i = 0; i < n; ++i) { n     if (dis[i] >= INF) dis[i] = -1; n     printf( %lld%c , dis[i], (i + 1 == n) ?   n  :    ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n inline long long read() { n   char ch = getchar(); n   long long p = 1, data = 0; n   while (ch <  0  || ch >  9 ) { n     if (ch ==  - ) p = -1; n     ch = getchar(); n   } n   while (ch >=  0  && ch <=  9 ) { n     data = data * 10 + (ch ^ 48); n     ch = getchar(); n   } n   return p * data; n } n inline long long qpow(long long a, long long b) { n   long long r = 1; n   while (b) { n     if (b & 1) r = a * r; n     a = a * a; n     b >>= 1; n   } n   return r; n } n inline long long gcd(long long a, long long b) { n   while (b ^= a ^= b ^= a %= b) n     ; n   return a; n } n inline long long lcm(long long a, long long b) { return a * b / gcd(a, b); } n void exgcd(long long a, long long b, long long &x, long long &y) { n   if (!b) n     x = 1, y = 0; n   else { n     exgcd(b, a % b, y, x); n     y -= x * (a / b); n   } n } n const int mod = 1e9 + 7, maxn = 1e5 + 5; n char s[maxn], ans1[maxn], ans2[maxn]; n int num[10]; n int main() { n   scanf( %s , s + 1); n   int n = strlen(s + 1); n   for (int i = 1; i <= n; i++) num[s[i] -  0 ]++; n   int m = n; n   int mx = 0, t1 = 0, t2 = 0; n   for (int i = 1; i <= 5; i++) { n     int t = 10 - i, now = 0; n     if (!num[i] || !num[t]) continue; n     int tn[10][2]; n     for (int j = 0; j <= 9; j++) tn[j][1] = tn[j][0] = num[j]; n     tn[i][1]--, tn[t][0]--; n     for (int j = 0; j <= 9; j++) now += min(tn[j][1], tn[9 - j][0]); n     if (now >= mx) { n       t1 = i; n       t2 = t; n       mx = now; n     } n   } n   int tn[10][2]; n   for (int j = 0; j <= 9; j++) tn[j][1] = tn[j][0] = num[j]; n   int pre = 0; n   if (t1 || t2) { n     pre = 1; n     ans1[m] = t1 +  0 ; n     ans2[m] = t2 +  0 ; n     tn[t1][0]--; n     tn[t2][1]--; n     m--; n   } n   int flag = 0; n   for (int i = m; i >= 1 + flag; i--) { n     int OK = 0, f1 = 0, f2 = 0; n     if (mx) { n       mx--; n       for (int j = 0; j <= 9; j++) n         if (min(tn[j][0], tn[9 - j][1])) { n           tn[j][0]--; n           tn[9 - j][1]--; n           f1 = j; n           f2 = 9 - j; n           ans1[i] = j +  0 ; n           ans2[i] = 9 - j +  0 ; n           OK = 1; n           break; n         } n       pre = 1; n     } else { n       if (pre) { n         int vis[10][2]; n         for (int j = 0; j <= 9; j++) n           if (tn[j][0] && tn[9 - j][1]) vis[j][0] = vis[9 - j][1] = 1; n         int ok = 0; n         for (int j = 0; j <= 9; j++) n           if (tn[j][0] && !vis[j][0]) { n             f1 = j; n             ans1[i] = j +  0 ; n             ok++; n             break; n           } n         for (int j = 0; j <= 9; j++) n           if (tn[j][1] && !vis[j][1]) { n             f2 = j; n             ans2[i] = j +  0 ; n             ok++; n             break; n           } n         if (ok != 2) { n           ok = 0; n           for (int j = 0; j <= 9; j++) n             if (tn[j][0]) { n               f1 = j; n               ans1[i] = j +  0 ; n               ok++; n               break; n             } n           for (int j = 0; j <= 9; j++) n             if (tn[j][1]) { n               f2 = j; n               ans2[i] = j +  0 ; n               ok++; n               break; n             } n         } n         tn[f1][0]--; n         tn[f2][1]--; n       } else { n         int ok = 0; n         for (int j = 0; j <= 9; j++) { n           if (j == 0 && tn[j][0] && tn[j][1]) { n             f1 = 0, f2 = 0, ok = 1, OK = 1; n             tn[j][0]--, tn[j][1]--; n             ans1[i] =  0 , ans2[i] =  0 ; n             break; n           } else if (j != 0 && tn[j][0] && tn[10 - j][1]) { n             f1 = j, f2 = 10 - j, ok = 1, OK = 1; n             tn[j][0]--, tn[10 - j][1]--; n             ans1[i] = f1 +  0 , ans2[i] = f2 +  0 ; n             break; n           } n         } n         if (!ok) { n           for (int j = 0; j <= 9; j++) n             if (tn[j][0]) { n               tn[j][0]--; n               ans1[i] = j +  0 ; n               f1 = j; n               ok++; n               break; n             } n           for (int j = 0; j <= 9; j++) n             if (tn[j][1]) { n               tn[j][1]--; n               ans2[i] = j +  0 ; n               f2 = j; n               ok++; n               break; n             } n         } n         if (ok == 2) OK = 1; n       } n       pre = (f1 + f2 + 1 >= 10 ? 1 : 0); n     } n     if (f1 == 0 && f2 == 0) { n       flag++; n       i++; n     } n   } n   printf( %s , ans1 + flag + 1); n   for (int i = 1; i <= flag; i++) printf( 0 ); n   puts(  ); n   printf( %s , ans2 + flag + 1); n   for (int i = 1; i <= flag; i++) printf( 0 ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int mx = 1e6; 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     long long n, h; n     cin >> h >> n; n     long long arr[n]; n     for (int i = 0; i < n; i++) cin >> arr[i]; n     long long ans = 0, cp = h; n     for (int i = 1; i < n; i++) { n       if (cp == 0) break; n       if (cp == (arr[i] + 1)) { n         if ((i + 1) < n && cp == (arr[i + 1] + 2)) { n           cp = arr[i + 1]; n           ++i; n         } else { n           if ((i + 1) < n) { n             cp = arr[i + 1] + 1; n             ++ans; n           } else if (i == n - 1) { n             if (cp == 2) n               continue; n             else n               ++ans; n           } n         } n       } else { n         cp = arr[i] + 1; n         --i; n       } n     } n     cout << ans << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 220000; n const int MAXM = 2 * MAXN; n const int MAXS = 26; n struct SAM { n   struct Node { n     int nxt[MAXS], par; n     int len, siz; n   } st[MAXM]; n   int m, root, lst; n   inline int newNode(int l) { n     int x = ++m; n     memset(st[x].nxt, 0, sizeof st[x].nxt); n     st[x].par = 0; n     st[x].len = l; n     st[x].siz = 0; n     return x; n   } n   void extend(char c, bool k) { n     int x = c -  a ; n     int p = lst, np = newNode(st[p].len + 1); n     lst = np; n     st[np].siz = k; n     while (p && !st[p].nxt[x]) st[p].nxt[x] = np, p = st[p].par; n     if (!p) { n       st[np].par = root; n       return; n     } n     int q = st[p].nxt[x]; n     if (st[q].len == st[p].len + 1) { n       st[np].par = q; n       return; n     } n     int nq = newNode(st[p].len + 1); n     memcpy(st[nq].nxt, st[q].nxt, sizeof st[q].nxt); n     st[nq].par = st[q].par; n     st[np].par = st[q].par = nq; n     while (p && st[p].nxt[x] == q) st[p].nxt[x] = nq, p = st[p].par; n   } n   void build(int n, char* s, char* t) { n     static int c[MAXN], d[MAXM]; n     m = 0; n     lst = root = newNode(0); n     for (int i = 0; i < n; i++) extend(s[i], t[i] ^  1 ); n     memset(c, 0, sizeof c); n     for (int i = 1; i <= m; i++) c[st[i].len]++; n     for (int i = 1; i <= n; i++) c[i] += c[i - 1]; n     for (int i = 1; i <= m; i++) d[c[st[i].len]--] = i; n     for (int i = m; i >= 2; i--) st[st[d[i]].par].siz += st[d[i]].siz; n   } n   long long query() { n     long long r = 0; n     for (int i = 1; i <= m; i++) r = max(r, 1ll * st[i].len * st[i].siz); n     return r; n   } n } sam; n char s[MAXN], t[MAXN]; n int main() { n   int n; n   scanf( %d , &n); n   scanf( %s%s , s, t); n   sam.build(n, s, t); n   printf( %lld n , sam.query()); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long maxn = 2e6 + 100; n const long long inf = 0x3f3f3f3f; n const long long iinf = 1 << 30; n const long long linf = 2e18; n const long long mod = 998244353; n const double eps = 1e-7; n template <class T = long long> n T chmin(T &a, T b) { n   return a = min(a, b); n } n template <class T = long long> n T chmax(T &a, T b) { n   return a = max(a, b); n } n template <class T = long long> n inline void red(T &x) { n   x -= mod, x += x >> 31 & mod; n } n template <class T = long long> n T read() { n   T f = 1, a = 0; n   char ch = getchar(); n   while (!isdigit(ch)) { n     if (ch ==  - ) f = -1; n     ch = getchar(); n   } n   while (isdigit(ch)) { n     a = (a << 3) + (a << 1) + ch -  0 ; n     ch = getchar(); n   } n   return a * f; n } n long long n, sqrt_n = 300; n long long ans, val; n long long a[maxn], cnt[maxn], sum[maxn], t[maxn], cnt1[maxn], cnt2[maxn]; n vector<long long> pos; n void modify(long long x, long long pos) { n   cnt2[cnt1[x]]--; n   cnt1[x] += pos; n   cnt2[cnt1[x]]++; n } n signed main() { n   n = read(); n   for (long long i = (1); i <= (n); ++i) a[i] = read(), cnt[a[i]]++; n   long long maxx = 0, tot = 0, val = 0; n   for (long long i = (1); i <= (n); ++i) { n     if (cnt[i] == maxx) tot++; n     if (cnt[i] > maxx) tot = 1, maxx = cnt[i], val = i; n   } n   if (tot > 1) { n     printf( %lld n , n); n     return 0; n   } n   for (long long i = (1); i <= (n); ++i) n     if (cnt[i] > sqrt_n && i != val) pos.push_back(i); n   for (long long i : pos) { n     for (long long j = (1); j <= (n); ++j) n       sum[j] = sum[j - 1] + (a[j] == i ? -1 : (a[j] == val ? 1 : 0)); n     for (long long j = (-n); j <= (n); ++j) t[j + n] = linf; n     t[n] = 0; n     for (long long j = (1); j <= (n); ++j) { n       if (t[sum[j] + n] != linf) n         chmax(ans, j - t[sum[j] + n]); n       else n         t[sum[j] + n] = j; n     } n   } n   for (long long i = (1); i <= (sqrt_n); ++i) { n     for (long long j = (1); j <= (n); ++j) cnt1[j] = cnt2[j] = 0; n     long long l = 1, r = 0; n     while (r < n) { n       modify(a[++r], 1); n       while (cnt1[a[r]] > i) modify(a[l++], -1); n       if (cnt2[i] >= 2) chmax(ans, r - l + 1); n     } n   } n   printf( %lld n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, a[100005], l = -1000000000, r = 1000000000; n string b; n int main() { n   cin >> n; n   for (int i = 0; i < n; ++i) cin >> a[i]; n   cin >> b; n   for (int i = 4; i < n; ++i) { n     if (b[i] ==  0  && b[i - 1] ==  1 ) { n       int minim = a[i]; n       for (int j = i - 4; j < i; ++j) minim = min(minim, a[j]); n       r = min(r, minim - 1); n     } else if (b[i] ==  1  && b[i - 1] ==  0 ) { n       int maxim = a[i]; n       for (int j = i - 4; j < i; ++j) maxim = max(maxim, a[j]); n       l = max(l, maxim + 1); n     } n   } n   cout << l <<     << r <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<vector<int>> adj; n bool vis[200000 + 9]; n int cntnode; n map<int, int> colorcnt; n int c[200000 + 9]; n int mx; n void dfs(int node) { n   if (vis[node]) { n     return; n   } n   cntnode++; n   colorcnt[c[node]]++; n   mx = max(mx, colorcnt[c[node]]); n   vis[node] = 1; n   for (int i = 0; i < (int)adj[node].size(); i++) { n     int ch = adj[node][i]; n     dfs(ch); n   } n } n int main() { n   int n, m, k; n   cin >> n >> m >> k; n   adj.resize(n); n   for (int i = 0; i < n; i++) { n     cin >> c[i]; n   } n   for (int i = 0; i < m; i++) { n     int x, y; n     cin >> x >> y; n     adj[x - 1].push_back(y - 1); n     adj[y - 1].push_back(x - 1); n   } n   int sum = 0; n   for (int i = 0; i < n; i++) { n     cntnode = 0; n     colorcnt.clear(); n     mx = 0; n     if (!vis[i]) { n       dfs(i); n     } n     sum += (cntnode - mx); n   } n   cout << sum; n   return 0; n } n


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


#include <bits/stdc++.h> n using namespace std; n struct vertex { n   long long inside, edge; n   int size = 1; n   vector<int> child; n }; n int ans = 0; n multiset<long long> st; n vertex g[100009]; n void dfs(int v, bool flag, long long deep) { n   if (flag) { n     for (int i = 0; i < g[v].child.size(); i++) { n       dfs(g[v].child[i], 1, 0); n       g[v].size += g[g[v].child[i]].size; n     } n     return; n   } n   if (deep - g[v].inside > *st.begin()) flag = 1; n   st.insert(deep); n   for (int i = 0; i < g[v].child.size(); i++) { n     dfs(g[v].child[i], flag, deep + g[g[v].child[i]].edge); n     g[v].size += g[g[v].child[i]].size; n   } n   if (flag) ans += g[v].size; n   flag = 0; n   st.erase(deep); n } n int main() { n   ios_base::sync_with_stdio(0); n   int n; n   st.insert(100000000000000000); n   cin >> n; n   for (int i = 1; i <= n; i++) cin >> g[i].inside; n   for (int i = 2, c; i <= n; i++) { n     cin >> c >> g[i].edge; n     g[c].child.push_back(i); n   } n   dfs(1, 0, 0); n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long MOD = 1000000007; n const double pi = 3.141593; n int main() { n   ios::sync_with_stdio(false); n   int n, t = 0, ans = 0; n   map<int, int> m; n   cin >> n; n   for (int i = 0; i < 2 * n; i++) { n     int x; n     cin >> x; n     m[x]++; n     t++; n     if (m[x] == 2) t -= 2; n     ans = max(ans, t); n   } n   cout << ans; n } n


#include <bits/stdc++.h> n using namespace std; n template <class c> n struct rge { n   c b, e; n }; n template <class c> n rge<c> range(c i, c j) { n   return rge<c>{i, j}; n } n template <class c> n auto dud(c* x) -> decltype(cerr << *x, 0); n template <class c> n char dud(...); n struct debug { n   template <class c> n   debug& operator<<(const c&) { n     return *this; n   } n }; n int main() { n   int n, m; n   cin >> n >> m; n   vector<vector<pair<int, int>>> mat(m, vector<pair<int, int>>(3)); n   set<int> white, red, blue; n   for (int i = 0; i < m; i++) { n     int x, y, z; n     cin >> x >> y >> z; n     mat[i][0].first = x; n     mat[i][1].first = y; n     mat[i][2].first = z; n     bool xWhite = (white.find(x) != white.end()); n     bool xRed = (red.find(x) != red.end()); n     bool xBlue = (blue.find(x) != blue.end()); n     bool yWhite = (white.find(y) != white.end()); n     bool yRed = (red.find(y) != red.end()); n     bool yBlue = (blue.find(y) != blue.end()); n     bool zWhite = (white.find(z) != white.end()); n     bool zRed = (red.find(z) != red.end()); n     bool zBlue = (blue.find(z) != blue.end()); n     if (xWhite) { n       white.insert(x); n       red.insert(y); n       blue.insert(z); n     } else if (xRed) { n       red.insert(x); n       white.insert(y); n       blue.insert(z); n     } else if (xBlue) { n       blue.insert(x); n       white.insert(y); n       red.insert(z); n     } else if (yWhite) { n       white.insert(y); n       red.insert(x); n       blue.insert(z); n     } else if (yRed) { n       red.insert(y); n       white.insert(x); n       blue.insert(z); n     } else if (yBlue) { n       blue.insert(y); n       white.insert(x); n       red.insert(z); n     } else if (zWhite) { n       white.insert(z); n       red.insert(x); n       blue.insert(y); n     } else if (zRed) { n       red.insert(z); n       blue.insert(x); n       white.insert(y); n     } else if (zBlue) { n       blue.insert(z); n       red.insert(x); n       white.insert(y); n     } else { n       debug() <<   [  n               <<  x  n                   :   n               << (x) n               <<  ]   n                    [  n               <<  y  n                   :   n               << (y) n               <<  ]   n                    [  n               <<  z  n                   :   n               << (z) <<  ]  ; n       white.insert(x); n       red.insert(y); n       blue.insert(z); n     } n   } n   for (int i = 1; i <= n; i++) { n     if (white.find(i) != white.end()) { n       cout <<  1  ; n     } else if (red.find(i) != red.end()) { n       cout <<  2  ; n     } else { n       cout <<  3  ; n     } n   } n   cout <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n using ll = long long; n template<typename A, typename B = A> using pr = pair<A, B>; n #define vec vector n #define ar array n #define len(a) ((int)((a).size())) n #define all(a) (a).begin(), (a).end() n  n int q; n vec<int> used; n int timer; n  n inline void merge(vec<pr<int>> &a, vec<pr<int>> &b, vec<pr<int>> &res) { n     timer++; n     int j = 0; n     auto insert = [&](pr<int> p) { n         if (used[p.first] == timer) n             return; n         used[p.first] = timer; n         res.push_back(p); n     }; n     for (int i = 0; i < len(a); i++) { n         while (len(res) <= q && j < len(b) && b[j].second >= a[i].second) { n             insert(b[j]); n             j++; n         } n         if (len(res) <= q) n             insert(a[i]); n         else n             break; n     } n     while (len(res) <= q && j < len(b)) { n         insert(b[j]); n         j++; n     } n } n  n struct my_stack { n     vec<pr<vec<pr<int>>>> st; n  n     my_stack() {} n  n     inline void pop_back() { n         assert(len(st)); n         st.pop_back(); n     } n  n     inline void push_back(vec<pr<int>> &a) { n         if (!len(st)) { n             st.emplace_back(a, a); n             return; n         } n         st.emplace_back(vec<pr<int>>{}, a); n         merge(st.end()[-2].first, a, st.back().first); n     } n  n     inline void rev() { n         assert(len(st)); n         reverse(all(st)); n         st[0].first = st[0].second; n         for (int i = 1; i < len(st); i++) { n             st[i].first.clear(); n             merge(st[i].second, st[i - 1].first, st[i].first); n         } n     } n  n     inline int get_min() { n         assert(len(st)); n         if (len(st.back().first) <= q) n             return -1; n         return st.back().first.back().second; n     } n }; n  n struct my_queue { n     my_stack front, back; n  n     my_queue() {} n  n     inline void pop() { n         if (!len(front.st)) { n             swap(front, back); n             front.rev(); n         } n         front.pop_back(); n     } n  n     inline void push(vec<pr<int>> &a) { n         back.push_back(a); n     } n  n     inline int get_min() { n         assert(len(front.st) + len(back.st)); n         if (!len(front.st)) n             return back.get_min(); n         if (!len(back.st)) n             return front.get_min(); n         vec<pr<int>> cur; n         merge(front.st.back().first, back.st.back().first, cur); n         if (len(cur) <= q) n             return -1; n         return cur.back().second; n     } n }; n  n int main() { n     ios::sync_with_stdio(false), cin.tie(nullptr); n  n     int n; n     cin >> n >> q; n     vec<vec<int>> a(n, vec<int>(n)); n     for (auto &xs : a) n         for (auto &x : xs) n             cin >> x; n  n     vec<vec<pr<int>>> left(n); n     used.resize(n * n + 1); n     vec<int> ans(n); n     for (int j = 0; j < n; j++) { n         my_queue que; n         int prev = 0; n         for (int i = 0; i < n; i++) { n             vec<pr<int>> nleft{{a[i][j], j}}; n             for (auto [x, pos] : left[i]) n                 if (x != a[i][j] && len(nleft) <= q) n                     nleft.emplace_back(x, pos); n             left[i] = nleft; n  n             que.push(left[i]); n             while (que.get_min() >= j - (i - prev)) { n                 que.pop(); n                 prev++; n             } n             // cerr << i <<     << j <<  :   << i - prev << endl; n             ans[i - prev]++; n         } n     } n  n     for (int i = n - 2; i >= 0; i--) n         ans[i] += ans[i + 1]; n     for (auto x : ans) n         cout << x <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n void solve() { n   int n; n   string s, t, ss; n   cin >> n >> s; n   ss = s; n   t =  abacaba ; n   int cnt = 0; n   for (int i = 0; i < n - 6; i++) { n     int j, k = 0; n     for (j = i; j < i + 7; j++) { n       if (s[j] != t[k]) break; n       k++; n     } n     if (j == i + 7) cnt++; n   } n   if (cnt == 1) { n     cout <<  Yes  n          <<   n ; n     for (int i = 0; i < n; i++) { n       if (s[i] ==  ? ) s[i] =  z ; n     } n     cout << s <<   n ; n   } else if (cnt > 1) { n     cout <<  No  n          <<   n ; n     return; n   } else { n     cnt = 0; n     for (int i = 0; i < n - 6; i++) { n       cnt = 0; n       int j, k = 0; n       for (j = i; j < i + 7; j++) { n         if (s[j] != t[k] && s[j] !=  ? ) break; n         s[j] = t[k]; n         k++; n       } n       if (j == i + 7) { n         for (int x = 0; x < n - 6; x++) { n           int y, z = 0; n           for (y = x; y < x + 7; y++) { n             if (s[y] != t[z]) break; n             z++; n           } n           if (y == x + 7) cnt++; n         } n         if (cnt == 1) { n           cout <<  Yes  n                <<   n ; n           for (int i = 0; i < n; i++) { n             if (s[i] ==  ? ) s[i] =  z ; n           } n           cout << s <<   n ; n           return; n         } else { n           for (int i = 0; i < n; i++) s[i] = ss[i]; n         } n       } else { n         for (int i = 0; i < n; i++) s[i] = ss[i]; n       } n     } n     cout <<  No  n          <<   n ; n   } n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   long long tt; n   cin >> tt; n   while (tt--) solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   int n; n   cin >> n; n   vector<int> id(n + 1); n   for (int i = 0; i < n; i++) { n     int x; n     cin >> x; n     id[x] = i; n   } n   vector<int> dp(n + 1, 1); n   for (int i = 2; i <= n; i++) { n     if (id[i] > id[i - 1]) { n       dp[i] += dp[i - 1]; n     } n   } n   cout << n - *max_element(dp.begin(), dp.end()); n } n


#include<bits/stdc++.h> n #define fo(i,a,b) for(int i=a;i<=b;i++) n #define fd(i,a,b) for(int i=a;i>=b;i--) n using namespace std; n  n typedef long long LL; n typedef pair<int,int> pr; n  n const int maxn=505; n  n int n,m; n char mp[maxn][maxn]; n  n int T; n int main() n { n  scanf( %d ,&T); n  while (T--) n  { n   scanf( %d %d ,&n,&m); n   fo(i,1,n) scanf( %s ,mp[i]+1); n    n   fo(i,1,n) if (i%3==1) n    fo(j,1,m) mp[i][j]= X ; n   fo(i,1,n) if (i%3==1 && i+3<=n) n   { n    bool pd=0; n    fo(j,1,m) if (mp[i+1][j]== X ) n    { n     mp[i+2][j]= X ; n     pd=1; n     break; n    } n    if (pd) continue; n    fo(j,1,m) if (mp[i+2][j]== X ) n    { n     mp[i+1][j]= X ; n     pd=1; n     break; n    } n    if (pd) continue; n    mp[i+1][1]=mp[i+2][1]= X ; n   } n   if (n%3==0) n   { n    fo(j,1,m) if (mp[n][j]== X ) mp[n-1][j]= X ; n   } n    n   fo(i,1,n) printf( %s n ,mp[i]+1); n  } n }