#include <bits/stdc++.h> n using namespace std; n int n, m, a[4], num, sum; n string s[4], str; n bool check(char ch) { n   return ch ==  a  || ch ==  e  || ch ==  i  || ch ==  o  || ch ==  u ; n } n int work() { n   sum = 0; n   for (int i = str.length() - 1; i >= 0; i--) { n     sum += check(str[i]); n     if (sum == m) return i; n   } n   return -1; n } n int main() { n   cin >> n >> m; n   for (int i = 1; i <= n; i++) { n     for (int j = 0; j < 4; j++) { n       cin >> str; n       num = work(); n       if (!~num) { n         puts( NO ); n         return 0; n       } n       s[j] = str.substr(num); n     } n     if (s[0] == s[1] && s[1] == s[2] && s[2] == s[3]) n       a[0] = 1; n     else if (s[0] == s[1] && s[2] == s[3]) n       a[1] = 1; n     else if (s[0] == s[2] && s[1] == s[3]) n       a[2] = 1; n     else if (s[0] == s[3] && s[1] == s[2]) n       a[3] = 1; n     else { n       puts( NO ); n       return 0; n     } n   } n   if (a[0] && !(a[1] || a[2] || a[3])) n     puts( aaaa ); n   else if (a[1] && !(a[2] || a[3])) n     puts( aabb ); n   else if (a[2] && !(a[1] || a[3])) n     puts( abab ); n   else if (a[3] && !(a[1] || a[2])) n     puts( abba ); n   else n     puts( NO ); n   return 0; n } n


#include <bits/stdc++.h> n int main() { n   long int n, a[100001], i, flag; n   char c[100001], c1; n   while (scanf( %ld , &n) == 1) { n     flag = 0; n     long int a1[100001] = {0}; n     c1 = getchar(); n     gets(c); n     for (i = 0; i < n; i++) { n       scanf( %ld , &a[i]); n     } n     i = 0; n     while (1) { n       if (c[i] ==  >  && a1[i] == 0) { n         a1[i] = 1; n         i += a[i]; n         if (i >= n) { n           flag = 1; n           break; n         } n       } else if (c[i] ==  <  && a1[i] == 0) { n         a1[i] = 1; n         i -= a[i]; n         if (i < 0) { n           flag = 1; n           break; n         } n       } else { n         flag = 0; n         break; n       } n     } n     if (flag == 1) { n       printf( FINITE n ); n     } else { n       printf( INFINITE n ); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T> n void chmin(T &x, const T &y) { n   if (x > y) x = y; n } n template <typename T> n void chmax(T &x, const T &y) { n   if (x < y) x = y; n } n inline int read() { n   int x = 0, f = 1; n   char ch = getchar(); n   while (!isdigit(ch)) { n     if (ch ==  - ) f = -1; n     ch = getchar(); n   } n   while (isdigit(ch)) { n     x = x * 10 + ch - 48; n     ch = getchar(); n   } n   return x * f; n } n const int N = 5000 + 10; n const int mod = 1e9 + 7; n int n, dp[N][30], len, a[N], C[N][N], ans; n signed main() { n   n = read(); n   for (int i = 1; i <= n; i++) { n     int ch; n     ch = getchar() -  a  + 1; n     if (ch != a[len]) a[++len] = ch; n   } n   C[0][0] = 1; n   for (int i = 1; i <= n; i++) n     for (int j = 1; j <= i; j++) n       C[i][j] = (C[i - 1][j] + C[i - 1][j - 1]) % mod; n   dp[0][0] = 1; n   for (int i = 1; i <= len; i++) n     for (int j = i; j; j--) { n       int cur = a[i]; n       dp[j][cur] = 0; n       for (int k = 0; k <= 26; k++) n         if (k != cur) dp[j][cur] = (dp[j][cur] + dp[j - 1][k]) % mod; n     } n   for (int i = 1; i <= len; i++) { n     int tmp = 0; n     for (int j = 1; j <= 26; j++) tmp = (tmp + dp[i][j]) % mod; n     (ans += 1LL * tmp * C[n][i] % mod) %= mod; n   } n   cout << ans; n   return 0; n   ; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, m; n   cin >> n >> m; n   int x = 2 * n, y = 3 * m; n   int k = 1; n   while (6 * k <= x && 6 * k <= y) { n     if (x + 2 <= y + 3) n       x += 2; n     else n       y += 3; n     k += 1; n   } n   cout << max(x, y); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n inline int read() { n   int res = 0, w = 1; n   char ch = getchar(); n   while (ch <  0  || ch >  9 ) n     ch ==  -  ? w = -1, ch = getchar() : ch = getchar(); n   while (ch >=  0  && ch <=  9 ) { n     res = res * 10 + ch -  0 ; n     ch = getchar(); n   } n   return w * res; n } n int n, a, b, c, d, e, f; n int main() { n   a = read(); n   b = read(); n   c = read(); n   d = read(); n   e = read(); n   f = read(); n   int flag = 0; n   flag |= (b * d * f) > (a * c * e); n   flag |= (!c && d); n   flag |= (!a && b && c && d); n   flag |= (!e && f && a && b && c && d); n   flag ? printf( Ron n ) : printf( Hermione n ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void fast() { n   ios_base::sync_with_stdio(0); n   cin.tie(NULL), cout.tie(NULL); n } n int main() { n   fast(); n   int n, k; n   cin >> n >> k; n   vector<int> v(n), vc(k + 1); n   for (int i = 0; i < n; i++) { n     cin >> v[i]; n   } n   auto it = unique(v.begin(), v.end()); n   v.resize(distance(v.begin(), it)); n   for (int i = 0; i < v.size(); i++) { n     vc[v[i]]++; n     if (i > 0 && i < v.size() - 1) { n       vc[v[i]] += (v[i - 1] == v[i + 1]); n     } n   } n   int pos = 1; n   for (int i = 1; i <= k; i++) { n     if (vc[i] > vc[pos]) { n       pos = i; n     } n   } n   cout << pos; n   return 0; n } n


#include <bits/stdc++.h> n long long p[16]; n void F() { n   p[1] = 10; n   for (int i = 2; i <= 14; i++) p[i] = p[i - 1] * 10; n } n using namespace std; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   F(); n   long long n, ans = 0; n   cin >> n; n   if (n < 10) { n     cout << n; n     return 0; n   } n   for (int i = 1; i <= 14; i++) { n     if (n / p[i] == 0) { n       long long a = p[i - 1] - 1; n       long long b = n - a; n       ostringstream str1, str2; n       str1 << a; n       str2 << b; n       string sa = str1.str(); n       string sb = str2.str(); n       for (auto x : sa) ans += x -  0 ; n       for (auto x : sb) ans += x -  0 ; n       cout << ans; n       return 0; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n #pragma GCC optimize( Ofast ) n #pragma GCC target( avx,avx2,fma ) n #pragma GCC optimization( unroll-loops ) n using namespace std; n array<long long, 2> parent[1000][1000]; n long long sze[1000][1000]; n array<long long, 2> p(array<long long, 2> x) { n   if (x == parent[x[0]][x[1]]) { n     return x; n   } else { n     return parent[x[0]][x[1]] = p(parent[x[0]][x[1]]); n   } n } n void unite(array<long long, 2> x, array<long long, 2> y) { n   array<long long, 2> a = p(x); n   array<long long, 2> b = p(y); n   if (a != b) { n     if (sze[a[0]][a[1]] < sze[b[0]][b[1]]) { n       swap(a, b); n     } n     parent[b[0]][b[1]] = a; n     sze[a[0]][a[1]] += sze[b[0]][b[1]]; n   } n } n int32_t main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   ; n   long long n; n   cin >> n; n   unordered_map<long long, vector<array<long long, 2>>> m1; n   unordered_map<long long, vector<array<long long, 2>>> m2; n   vector<array<long long, 2>> a(n); n   for (long long i = 0; i < n; ++i) { n     long long x, y; n     cin >> x >> y; n     x--; n     y--; n     a[i] = {x, y}; n     m1[x].push_back({x, y}); n     m2[y].push_back({x, y}); n   } n   for (long long i = 0; i < 1000; ++i) { n     for (long long j = 0; j < 1000; ++j) { n       parent[i][j] = {i, j}; n       sze[i][j] = 1; n     } n   } n   for (auto i : a) { n     for (auto j : m1[i[0]]) { n       if (j == i) { n         continue; n       } n       unite(i, j); n     } n     for (auto j : m2[i[1]]) { n       if (j == i) { n         continue; n       } n       unite(i, j); n     } n   } n   long long ans = 0; n   for (auto i : a) { n     if (i == parent[i[0]][i[1]]) { n       ans++; n     } n   } n   cout << ans - 1 <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(0); n   int r, c, n, k; n   cin >> r >> c >> n >> k; n   int tab[r][c]; n   for (int i = 0; i < r; i++) n     for (int j = 0; j < c; j++) tab[i][j] = 0; n   for (int i = 0; i < n; i++) { n     int x, y; n     cin >> x >> y; n     --x; n     --y; n     tab[x][y] = 1; n   } n   int wyn = 0; n   for (int x1 = 0; x1 < r; x1++) n     for (int y1 = 0; y1 < c; y1++) n       for (int x2 = x1; x2 < r; x2++) n         for (int y2 = y1; y2 < c; y2++) { n           int v = 0; n           for (int i = x1; i <= x2; i++) n             for (int j = y1; j <= y2; j++) v += tab[i][j]; n           if (v >= k) wyn++; n         } n   cout << wyn; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 200005; n int mod = 998244353; n int T; n int a[N], b[N], p[N]; n bool vis[N]; n int main() { n   scanf( %d , &T); n   while (T--) { n     int n, k; n     scanf( %d%d , &n, &k); n     for (int i = 1; i <= n; ++i) { n       scanf( %d , &a[i]); n       p[a[i]] = i; n       vis[i] = 0; n     } n     for (int i = 1; i <= k; ++i) { n       scanf( %d , &b[i]); n     } n     int ans = 1; n     for (int i = k; i >= 1; --i) { n       int t = p[b[i]]; n       int kk = 0; n       if (t > 1 && !vis[t - 1]) kk++; n       if (t < n && !vis[t + 1]) kk++; n       vis[t] = 1; n       ans = (ans * kk) % mod; n     } n     printf( %d n , ans); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int tc; n   scanf( %d , &tc); n   for (int i = 0; i < tc; i++) { n     int N, K; n     scanf( %d %d , &N, &K); n     if (N < K) { n       printf( NO n ); n       continue; n     } n     if (N % 2 == 0) { n       if (2 * K <= N) { n         printf( YES n ); n         for (int j = 0; j < K - 1; j++) { n           if (j != 0) printf(   ); n           printf( %d , 2); n         } n         printf(  %d n , N - (2 * (K - 1))); n       } else if (N == K) { n         printf( YES n ); n         for (int j = 0; j < K; j++) { n           if (j != 0) printf(   ); n           printf( %d , 1); n         } n         printf(  n ); n       } else if (K % 2 != 0) { n         printf( NO n ); n       } else { n         bool flag = false; n         int count = 0; n         while (N > K) { n           count++; n           N -= 3; n           K -= 1; n           if (N == K) { n             flag = true; n             printf( YES n ); n             for (int l = 0; l < count; l++) { n               if (l != 0) printf(   ); n               printf( %d , 3); n             } n             for (int l = 0; l < N; l++) { n               printf(  %d , 1); n             } n             printf(  n ); n             break; n           } n         } n         if (!flag) { n           printf( NO n ); n         } n       } n     } else { n       if (K % 2 != 1) { n         printf( NO n ); n       } else { n         printf( YES n ); n         for (int j = 0; j < K - 1; j++) { n           if (j != 0) printf(   ); n           printf( %d , 1); n         } n         printf(  %d n , N - ((K - 1))); n       } n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 100005; n int n, m, q; n long long ans, in[N], out[N]; n vector<int> f[N]; n int main() { n   scanf( %d%d , &n, &m); n   for (int i = 1; i <= m; ++i) { n     int x, y; n     scanf( %d%d , &x, &y); n     if (x > y) swap(x, y); n     f[x].push_back(y); n     in[y]++, out[x]++; n   } n   for (int i = 1; i <= n; ++i) ans += in[i] * out[i]; n   printf( %lld n , ans); n   scanf( %d , &q); n   while (q--) { n     int x; n     scanf( %d , &x); n     ans -= in[x] * out[x]; n     in[x] += out[x], out[x] = 0; n     for (int i = 0; i < f[x].size(); ++i) { n       int y = f[x][i]; n       ans -= in[y] * out[y]; n       out[y]++, in[y]--; n       ans += in[y] * out[y]; n       f[y].push_back(x); n     } n     f[x].clear(); n     printf( %lld n , ans); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, ai, nb = 0; n   map<int, int> dict; n   map<int, int>::iterator it; n   cin >> n; n   int i = 0; n   while (i < n) { n     cin >> ai; n     dict[ai]++; n     i++; n   } n   for (it = dict.begin(); it != dict.end(); it++) { n     while ((it->second) > 1) { n       it->second = (it->second) - 2; n       nb++; n     } n   } n   cout << (nb / 2); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 2e5 + 7; n int a[N]; n int b[N]; n int main() { n   int n; n   cin >> n; n   a[1] = 0; n   for (int i = 2; i <= n; ++i) { n     int k; n     cin >> k; n     a[i] = a[i - 1] + k; n   } n   int x = 1e6; n   for (int j = 1; j <= n; ++j) { n     x = min(x, a[j]); n   } n   for (int i = 1; i <= n; ++i) { n     if (a[i] - x + 1 >= 1 && a[i] - x + 1 <= n) { n       b[a[i] - x + 1] = 1; n     } n   } n   for (int i = 1; i <= n; ++i) { n     if (b[i] == 0) { n       cout << -1 << endl; n       return 0; n     } n   } n   for (int i = 1; i <= n; ++i) { n     cout << a[i] - x + 1 <<    ; n   } n   cout << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n queue<pair<int, int> > q; n int c[2048][2048]; n int dr[4] = {-1, 0, 1, 0}; n int dc[4] = {0, 1, 0, -1}, nr, nc; n int main() { n   int n, t, x, y; n   scanf( %d%d , &n, &t); n   c[1023][1023] = n; n   q.push(make_pair(1023, 1023)); n   pair<int, int> s; n   int mxr = 1023, mxc = 1023; n   while (!q.empty()) { n     s = q.front(); n     q.pop(); n     if (c[s.first][s.second] < 4) continue; n     for (int k = 0; k < 4; ++k) { n       nr = s.first + dr[k]; n       nc = s.second + dc[k]; n       c[nr][nc] += c[s.first][s.second] / 4; n       if (c[nr][nc] >= 4) q.push(make_pair(nr, nc)); n     } n     c[s.first][s.second] %= 4; n   } n   ++t; n   while (--t) { n     scanf( %d%d , &x, &y); n     x += 1023; n     y += 1023; n     if (x < 0 || x > 2047 || y < 0 || y > 2047) n       printf( 0 n ); n     else n       printf( %d n , c[y][x]); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m; n long long a[15]; n int main() { n   scanf( %d%d , &n, &m); n   for (int i = 1; i <= n; i++) { n     int x; n     scanf( %d , &x); n     a[x]++; n   } n   long long ans = 0; n   for (int i = 1; i <= m; i++) n     for (int j = i + 1; j <= m; j++) ans += a[i] * a[j]; n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 1e5 + 5; n pair<int, int> p[maxn]; n int a[maxn]; n int b[maxn]; n int n, k, cnt; n bool F(int a, int b) { return a > b; } n void solve() { n   scanf( %d %d , &n, &k); n   for (int i = 1; i <= n; i++) scanf( %d , a + i); n   for (int i = 1; i <= n; i++) scanf( %d , b + i); n   sort(a + 1, a + n + 1); n   sort(b + 1, b + n + 1, F); n   int i = 1, t = 1; n   while (true) { n     if (b[t] + a[i++] >= k) cnt++, t++; n     if (t > n || i > n) break; n   } n   printf( 1 %d , cnt); n } n int main() { solve(); } n


#include <bits/stdc++.h> n using namespace std; n using VI = vector<int>; n using VVI = vector<VI>; n using VB = vector<bool>; n using VVB = vector<VB>; n using VD = vector<double>; n using VVD = vector<VD>; n using VS = vector<string>; n using PII = pair<int, int>; n using VPII = vector<PII>; n using VL = vector<long long>; n using VVL = vector<VL>; n int n; n const int MOD = (int)1e9 + 7; n const int MAXN = 300010; n VI A; n int add(int a, int b) { n   a = ((a % MOD) + (b % MOD)); n   return (a >= MOD) ? (a - MOD) : a; n } n int sub(int a, int b) { n   a -= b; n   return (a < 0) ? (a + MOD) : a; n } n int mul(int a, int b) { return ((long long)a * b) % MOD; } n int powTable[MAXN]; n long long sumMaxDistSubsets() { n   sort(A.begin(), A.end()); n   powTable[0] = 1; n   for (int i = 1; i <= n; ++i) powTable[i] = (powTable[i - 1] * 2ll) % MOD; n   int ret = 0; n   for (int i = 1; i <= n; ++i) { n     int times = powTable[i - 1] - powTable[n - i]; n     ret = add(ret, mul(A[i], times)); n   } n   return ret; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cin >> n; n   A.assign(n + 1, 0); n   for (int i = 1; i < (int)n + 1; ++i) cin >> A[i]; n   cout << sumMaxDistSubsets() << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 520; n int n, m, q, s[4][N][N], id[N], s2[N / 2][N][N], Mx; n char c[N][N]; n bool CK(int s[N][N], int x1, int y1, int x2, int y2, int op) { n   int dat = s[x2][y2] - s[x1 - 1][y2] - s[x2][y1 - 1] + s[x1 - 1][y1 - 1]; n   return op ? dat : dat == (x2 - x1 + 1) * (y2 - y1 + 1); n } n bool ck(int x, int y, int len) { n   int x0 = x - len + 1, y0 = y - len + 1, x1 = x + len, y1 = y + len; n   return CK(s[0], x0, y0, x, y, 0) && CK(s[1], x0, y + 1, x, y1, 0) && n          CK(s[2], x + 1, y0, x1, y, 0) && CK(s[3], x + 1, y + 1, x1, y1, 0); n } n int read() { n   int x = 0, c; n   while (!isdigit(c = getchar())) n     ; n   while (isdigit(c)) x = x * 10 + c - 48, c = getchar(); n   return x; n } n int main() { n   id[ R ] = 0; n   id[ G ] = 1; n   id[ Y ] = 2; n   id[ B ] = 3; n   scanf( %d%d%d , &n, &m, &q); n   Mx = min(n / 2, m / 2); n   for (int i = 1; i <= n; i++) scanf( %s , c[i] + 1); n   for (int i = 1; i <= n; i++) n     for (int j = 1; j <= m; j++) n       for (int k = 0; k < 4; k++) n         s[k][i][j] = s[k][i - 1][j] + s[k][i][j - 1] - s[k][i - 1][j - 1] + n                      (id[c[i][j]] == k); n   for (int i = 1; i <= n; i++) n     for (int j = 1; j <= m; j++) { n       int mx = min(min(i, n - i), min(j, m - j)); n       for (int k = 1; k <= Mx; k++) n         s2[k][i][j] = s2[k][i - 1][j] + s2[k][i][j - 1] - s2[k][i - 1][j - 1]; n       for (int k = 1; k <= mx; k++) n         if (ck(i, j, k)) n           s2[k][i][j]++; n         else n           break; n     } n   for (int i = 1, x1, y1, x2, y2; i <= q; i++) { n     x1 = read(), y1 = read(); n     x2 = read(); n     y2 = read(); n     int l = 1, r = min((x2 - x1 + 1) / 2, (y2 - y1 + 1) / 2), ans = 0; n     while (l <= r) { n       int mid = (l + r) >> 1; n       if (CK(s2[mid], x1 + mid - 1, y1 + mid - 1, x2 - mid, y2 - mid, 1)) n         ans = mid, l = mid + 1; n       else n         r = mid - 1; n     } n     printf( %d n , 4 * ans * ans); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long n, i, j, k, r; n   cin >> n >> k >> r; n   long long ar[n], arr[k]; n   for (i = 0; i < n; i++) { n     cin >> ar[i]; n   } n   for (i = 0; i < k; i++) { n     cin >> arr[i]; n   } n   sort(ar, ar + n); n   sort(arr, arr + k); n   long long a = ar[0], d, p; n   long long b = arr[k - 1]; n   d = r / a; n   p = r % a; n   long long sum = b * d + p; n   cout << max(sum, r) << endl; n   return 0; n } n


#include <bits/stdc++.h> n int getmin(int a, int b) { n   if (a == -1) n     return b; n   else if (b == -1) n     return a; n   else n     return a < b ? a : b; n } n int solve(int a, int b) { n   int tot = 0; n   while (a != 1 || b != 1) { n     tot++; n     if (!a || !b) return -1; n     if (a > b) n       a -= b; n     else n       b -= a; n   } n   return tot; n } n int main() { n   int n, ans = -1; n   scanf( %d , &n); n   for (int a = 1; a < n; a++) ans = getmin(ans, solve(a, n)); n   if (ans == -1) ans++; n   printf( %d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n char s[1010], a[1010], p[1020]; n int a1, a2, tot; n void cal() { n   if (a[tot - 2] ==  . ) { n     a2 += ((a[tot - 1] -  0 ) * 10 + (a[tot] -  0 )); n     tot -= 3; n   } n   if (a2 >= 100) a2 -= 100, a1++; n   int sum = 0; n   for (int i = 1; i <= tot; i++) n     if (a[i] !=  . ) sum = sum * 10 + a[i] -  0 ; n   a1 += sum; n } n int main() { n   scanf( %s , s + 1); n   int len = strlen(s + 1); n   for (int i = 1; i <= len; i++) { n     if ((s[i] <  0  || s[i] >  9 ) && s[i] !=  . ) continue; n     if ((s[i - 1] <  0  || s[i - 1] >  9 ) && s[i - 1] !=  . ) { n       cal(); n       memset(a, 0, sizeof(a)); n       tot = 0; n     } n     a[++tot] = s[i]; n     if (i == len) cal(); n   } n   int np = 0; n   if (a1 == 0) n     np = 1, p[1] =  0 ; n   else { n     int tmp = 0; n     while (a1 > 0) { n       p[++np] = a1 % 10 +  0 ; n       tmp++; n       if (tmp == 3) { n         p[++np] =  . ; n         tmp = 0; n       } n       a1 /= 10; n     } n   } n   if (p[np] !=  . ) printf( %c , p[np]); n   for (int i = np - 1; i >= 1; i--) printf( %c , p[i]); n   if (a2) printf( .%02d , a2); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 200010; n int n, q; n struct X { n   int id, val; n } s[maxn]; n struct XX { n   int a, b; n   XX() {} n   XX(int A, int B) { n     a = A; n     b = B; n   } n   bool operator<(const XX &aa) const { return b > aa.b; } n }; n XX fir, sec; n vector<int> v[maxn]; n set<XX> h; n int tmp[maxn], mn[maxn], mx[maxn]; n bool check(int x) { n   int tt = v[fir.a][x]; n   if (s[tt].val < sec.b) return 0; n   return 1; n } n int main() { n   scanf( %d , &n); n   for (int i = 1; i <= 200000; i++) mx[i] = 0, mn[i] = 2000000000; n   for (int i = 1; i <= n; i++) { n     scanf( %d%d , &s[i].id, &s[i].val); n     v[s[i].id].push_back(i); n     mx[s[i].id] = max(mx[s[i].id], s[i].val); n     mn[s[i].id] = min(mn[s[i].id], s[i].val); n   } n   for (int i = 1; i <= 200000; i++) { n     if (mx[i] == 0) continue; n     h.insert(XX(i, mx[i])); n   } n   scanf( %d , &q); n   for (int i = 1; i <= q; i++) { n     int k; n     scanf( %d , &k); n     for (int j = 1; j <= k; j++) scanf( %d , &tmp[j]); n     for (int j = 1; j <= k; j++) { n       if (mx[tmp[j]] == 0) continue; n       h.erase(XX(tmp[j], mx[tmp[j]])); n     } n     if (h.size() == 0) { n       printf( 0 0 n ); n     } else if (h.size() == 1) { n       XX ans = *h.begin(); n       printf( %d %d n , ans.a, mn[ans.a]); n     } else { n       set<XX>::iterator iter; n       int cnt = 0; n       for (iter = h.begin(); iter != h.end(); iter++) { n         cnt++; n         if (cnt == 1) n           fir = *iter; n         else if (cnt == 2) { n           sec = *iter; n           break; n         } n       } n       int L = 0, R = v[fir.a].size() - 1; n       int ans; n       while (L <= R) { n         int mid = (L + R) / 2; n         if (check(mid)) n           ans = mid, R = mid - 1; n         else n           L = mid + 1; n       } n       int tt = v[fir.a][ans]; n       printf( %d %d n , s[tt].id, s[tt].val); n     } n     for (int j = 1; j <= k; j++) { n       if (mx[tmp[j]] == 0) continue; n       h.insert(XX(tmp[j], mx[tmp[j]])); n     } n   } 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   long long n; n   cin >> n; n   vector<long long> a; n   for (long long i = 0; i < n; ++i) { n     long long x; n     cin >> x; n     a.push_back(x); n   } n   sort(a.begin(), a.end()); n   long long c = 0; n   for (long long i = 0; i < a.size() - 1; ++i) { n     c += a[a.size() - 1] - a[i]; n   } n   cout << c << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int c[110]; n int main() { n   int n, i, x, y, j; n   cin >> n; n   for (i = 0; i < n; i++) { n     cin >> x; n     for (j = 0; j < x; j++) { n       cin >> y; n       c[y]++; n       if (c[y] == n) { n         cout << y <<    ; n       } n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void _R(int &x) { scanf( %d , &x); } n void _R(long long &x) { scanf( %lld , &x); } n void _R(double &x) { scanf( %lf , &x); } n void _R(char &x) { scanf(  %c , &x); } n void _R(char *x) { scanf( %s , x); } n void R() {} n template <class T, class... U> n void R(T &head, U &...tail) { n   _R(head); n   R(tail...); n } n void _W(const int &x) { printf( %d , x); } n void _W(const long long &x) { printf( %lld , x); } n void _W(const double &x) { printf( %.16f , x); } n void _W(const char &x) { putchar(x); } n void _W(const char *x) { printf( %s , x); } n template <class T> n void _W(const vector<T> &x) { n   for (auto i = x.begin(); i != x.end(); _W(*i++)) n     if (i != x.cbegin()) putchar(   ); n } n void W() {} n template <class T, class... U> n void W(const T &head, const U &...tail) { n   _W(head); n   putchar(sizeof...(tail) ?     :   n ); n   W(tail...); n } n const int N = 3e5 + 7, M = 2e6; n const long long mod = 1e9 + 7; n inline int read() { n   int ret = 0; n   char ch = getchar(); n   bool f = 1; n   for (; !isdigit(ch); ch = getchar()) f ^= !(ch ^  - ); n   for (; isdigit(ch); ch = getchar()) ret = (ret << 1) + (ret << 3) + ch - 48; n   return f ? ret : -ret; n } n long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; } n long long ksm(long long a, long long b, long long mod) { n   int ans = 1; n   while (b) { n     if (b & 1) ans = (ans * a) % mod; n     a = (a * a) % mod; n     b >>= 1; n   } n   return ans; n } n long long inv2(long long a, long long mod) { return ksm(a, mod - 2, mod); } n void TelmaZzzz() {} n struct node { n   int l, r, val; n } que[N]; n int a[N]; n int arr[N]; n int k, t, m, n; n bool check(int x) { n   int minn = m + 1, maxn = -1; n   for (int i = 1; i <= m + 1; i++) arr[i] = 0; n   for (int i = 1; i <= k; i++) { n     if (que[i].val <= a[n - x + 1]) continue; n     arr[que[i].l]++; n     arr[que[i].r + 1]--; n   } n   int ans = m + 1; n   for (int i = 1; i <= m + 1; i++) { n     arr[i] += arr[i - 1]; n     if (arr[i]) ans += 2; n   } n   if (ans <= t) return true; n   return false; n } n int main() { n   TelmaZzzz(); n   R(n, m, k, t); n   for (int i = 1; i <= n; i++) R(a[i]); n   for (int i = 1; i <= k; i++) R(que[i].l, que[i].r, que[i].val); n   sort(a + 1, a + 1 + n); n   int l = 1, r = n; n   while (l <= r) { n     int mi = (l + r) / 2; n     if (check(mi)) n       l = mi + 1; n     else n       r = mi - 1; n   } n   W(r); n   return 0; n } n


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


#include <bits/stdc++.h> n using namespace std; n int Int() { n   int x; n   scanf( %d , &x); n   return x; n } n long long Long() { n   long long x; n   scanf( %lld , &x); n   return x; n } n void err(istream_iterator<string> it) { cout << endl; } n template <typename T, typename... Args> n void err(istream_iterator<string> it, T a, Args... args) { n   cerr << *it <<   =   << a <<    ; n   err(++it, args...); n } n const int N = (int)2e5 + 5; n const int maxN = (int)1e6 + 6; n const long long Mod = (long long)1e9 + 7; n vector<int> box; n int len, k, a, b; n bool vis[201][201]; n string s; n void call(int cur, int last) { n   if (cur > k) return; n   if (last == (int)s.size()) { n     if (cur != k) return; n     int cnt = 0; n     for (int i : box) { n       for (int j = cnt; j < i + cnt; ++j) cout << s[j]; n       cout <<   n ; n       cnt += i; n     } n     exit(0); n   } n   if (vis[cur][last]) return; n   vis[cur][last] = 1; n   for (int i = a; i <= b; ++i) { n     if (last + i <= (int)s.size()) { n       box.push_back(i); n       call(cur + 1, i + last); n       box.pop_back(); n     } n   } n } n int main() { n   int test = 1, tc = 0; n   while (test--) { n     cin >> k >> a >> b; n     cin >> s; n     for (int i = a; i <= b; ++i) { n       if (i <= (int)s.size()) { n         box.push_back(i); n         call(1, i); n         box.pop_back(); n       } n     } n     puts( No solution ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n double vp, vd, t, f, c; n int main() { n   cin >> vp >> vd >> t >> f >> c; n   if (vp >= vd) { n     cout <<  0 ; n     return 0; n   } n   int numberOfBojous = 0; n   double timeToHaveTheDragonBack = 0, overtakePosition = 0; n   double princessPosition = vp * t; n   while (princessPosition < c) { n     overtakePosition = vd * princessPosition / (vd - vp); n     if (overtakePosition < c) numberOfBojous++; n     timeToHaveTheDragonBack = (overtakePosition / vd) + f; n     princessPosition = (vp * timeToHaveTheDragonBack) + overtakePosition; n   } n   cout << numberOfBojous; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int INF = 0x3f3f3f3f; n const double EPS = 1e-10; n const int MOD = 1000000007; n const int MAXN = 110000; n const double PI = acos(-1.0); n template <class T> n T abs(T x) { n   return x < 0 ? -x : x; n } n int num, kase; n int flag[22][MAXN]; n long long fun() { n   long long ret = 0; n   for (int index = 0; index < (21); ++index) { n     long long cnt = 0, t = 0; n     for (int i = (1); i <= (num + 1); ++i) { n       if (flag[index][i] == 1) n         cnt++; n       else { n         t += ((cnt * cnt + cnt) >> 1); n         cnt = 0; n       } n     } n     ret += (1 << index) * t; n   } n   return ret; n } n void findlr(int* f, int n, int& l, int& r) { n   int i = n - 1; n   while (f[i] == 1) i--; n   l = i + 1; n   i = n + 1; n   while (f[i] == 1) i++; n   r = i - 1; n } n int main() { n   int t, p; n   while (~scanf( %d%d , &num, &kase)) { n     memset(flag, 0, sizeof(flag)); n     for (int i = (1); i <= (num); ++i) { n       scanf( %d , &t); n       p = 1; n       for (int ind = 0; ind < (21); ++ind) { n         flag[ind][i] = ((t & p) > 0); n         p <<= 1; n       } n     } n     long long ans = fun(); n     int a, b, l, r; n     for (int tt = 0; tt < (kase); ++tt) { n       scanf( %d%d , &a, &b); n       for (int ind = 0; ind < (21); ++ind) { n         p = 1 << ind; n         if (((b & p) > 0) != flag[ind][a]) { n           findlr(flag[ind], a, l, r); n           long long lenl = a - l, lenr = r - a; n           ans += (1 - flag[ind][a] * 2) * (1 << ind) * n                  (lenl * lenr + lenl + lenr + 1); n           flag[ind][a] = !flag[ind][a]; n         } n       } n       cout << ans << endl; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n; n vector<pair<int, int> > vt; n bool cmp(pair<int, int> x, pair<int, int> y) { n   return (x.first < y.first || (x.first == y.first && x.second < y.second)); n } n int main() { n   cin >> n; n   for (int i = 1; i <= n; i++) { n     int u; n     cin >> u; n     vt.push_back(make_pair(u, i)); n   } n   sort(vt.begin(), vt.end(), cmp); n   for (int i = 0; i < (int)vt.size() - 2; i++) n     for (int j = i + 1; j < (int)vt.size() - 1; j++) { n       int k = upper_bound( n                   vt.begin(), vt.end(), n                   pair<int, int>(vt[i].first + vt[j].first - 1, 1000000000)) - n               vt.begin(); n       if (vt[k].first == (vt[i].first + vt[j].first)) { n         cout << vt[k].second <<     << vt[i].second <<     << vt[j].second; n         return 0; n       } n     } n   cout <<  -1 ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 2500 + 10, MAXK = 7; n int k, sum[MAXN][MAXN], l[MAXK], r[MAXK]; n long long Tohka; n vector<string> Rotate(vector<string>& a) { n   vector<string> Houki; n   for (int i = 0; i < (int)a[0].size(); ++i) { n     string str; n     for (int j = 0; j < (int)a.size(); ++j) str += a[j][i]; n     Houki.push_back(str); n   } n   return Houki; n } n int Gets(int xl, int yl, int xr, int yr) { n   return sum[xr][yr] - sum[xl - 1][yr] - sum[xr][yl - 1] + sum[xl - 1][yl - 1]; n } n void solve(vector<string> a) { n   if (a.size() == 0 || a[0].size() == 0) return; n   if (a.size() > a[0].size()) a = Rotate(a); n   int n = (int)a.size(), m = (int)a[0].size(); n   for (int i = 1; i <= n; ++i) n     for (int j = 1; j <= m; ++j) n       sum[i][j] = sum[i - 1][j] + sum[i][j - 1] - sum[i - 1][j - 1] + n                   (a[i - 1][j - 1] ==  1 ); n   int mid = (m + 1) / 2; n   for (int i = 1; i <= n; ++i) { n     for (int j = 0; j <= k; ++j) l[j] = 1, r[j] = m; n     for (int j = i; j <= n; ++j) { n       for (int p = 0; p <= k; ++p) { n         for (; l[p] <= mid && Gets(i, l[p], j, mid) > p; ++l[p]) n           ; n         for (; r[p] > mid && Gets(i, mid + 1, j, r[p]) > p; --r[p]) n           ; n       } n       for (int lp = 0, rp = k; lp <= k; ++lp, --rp) { n         int ls = (lp ? l[lp - 1] : (mid + 1)) - l[lp], n             rs = r[rp] - (rp ? r[rp - 1] : mid - 1); n         Tohka += (long long)ls * rs; n       } n     } n   } n   vector<string> lstr, rstr; n   for (int i = 0; i < n; ++i) { n     string ls, rs; n     for (int j = 0; j < m; ++j) { n       if (j + 1 < mid) ls += a[i][j]; n       if (j + 1 > mid) rs += a[i][j]; n     } n     lstr.push_back(ls), rstr.push_back(rs); n   } n   solve(lstr), solve(rstr); n } n int main() { n   int n, m; n   vector<string> a; n   cin >> n >> m >> k; n   for (int i = 1; i <= n; ++i) { n     string str; n     cin >> str; n     a.push_back(str); n   } n   solve(a); n   cout << Tohka << endl; n   fclose(stdin); n   fclose(stdout); n   return 0; n } n


#include <bits/stdc++.h> n #pragma GCC target( avx2 ) n #pragma GCC optimization( O3 ) n #pragma GCC optimization( unroll-loops ) n using namespace std; n const int INF = 1e9; n int main() { n   string s; n   cin >> s; n   long long n = s.length(); n   vector<long long> v; n   long long ans = 0; n   for (long long i = 0; i < n; i++) { n     if (int(s[i]) < 97) { n       v.push_back(1); n     } else { n       v.push_back(0); n     } n   } n   vector<long long> p = vector<long long>(n, 0); n   p[0] = (v[0] == 0) ? 1 : 0; n   for (long long i = 1; i < n; i++) { n     p[i] = p[i - 1] + ((v[i] == 0) ? 1 : 0); n   } n   long long mn = n - p[n - 1]; n   for (long long i = 0; i < n; i++) { n     mn = min(mn, p[i] + (n - 1 - i - p[n - 1] + p[i])); n   } n   cout << mn << endl; n } n


#include <bits/stdc++.h> n using namespace std; n map<long long, vector<long long>> v; n map<long long, long long> visited, dist, pt; n long long mx = 0, nd = 0, mxd = 0, mxn = 0; n void bfs(long long u) { n   visited[u] = 1; n   queue<long long> q; n   q.push(u); n   dist[u] = 0; n   while (!q.empty()) { n     u = q.front(); n     visited[u] = 1; n     q.pop(); n     visited[u] = 1; n     for (long long i = 0; i < (long long)v[u].size(); i++) { n       long long p = v[u][i]; n       if (!visited[p]) { n         visited[p] = 1; n         dist[p] = dist[u] + 1; n         if (dist[p] > mxd) { n           mxd = dist[p]; n           mxn = p; n         } n         pt[p] = u; n         q.push(p); n       } n     } n   } n } n int main() { n   long long a, b, x, c, i, j, n, t, k, z = 0, node, edge; n   scanf( %lld , &n); n   for (i = 1; i <= n; i++) { n     scanf( %lld %lld , &a, &b); n     v[a].push_back(b); n     v[b].push_back(a); n     c = a; n   } n   bfs(c); n   x = mxn; n   visited.clear(); n   dist.clear(); n   pt.clear(); n   mxd = 0; n   bfs(mxn); n   vector<long long> path; n   path.push_back(mxn); n   long long now = mxn; n   while (now != x) { n     now = pt[now]; n     path.push_back(now); n   } n   for (i = 0; i < (long long)path.size(); i++) cout << path[i] <<    ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 1e5; n static int comp[maxn]; n static int pr[maxn]; n static int n; n void make_set() { n   for (int i = 0; i < n; ++i) { n     pr[i] = i; n     comp[i] = 1; n   } n } n int find_set(int a) { n   if (pr[a] == a) return a; n   return pr[a] = find_set(pr[a]); n } n void union_set(int a, int b) { n   a = find_set(a); n   b = find_set(b); n   pr[a] = b; n   comp[b] += comp[a]; n   comp[a] = 0; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(nullptr); n   cout.tie(nullptr); n   cin >> n; n   int m; n   cin >> m; n   make_set(); n   for (int i = 0; i < m; ++i) { n     int a, b; n     cin >> a >> b; n     a--; n     b--; n     if (find_set(a) != find_set(b)) union_set(a, b); n   } n   int k; n   cin >> k; n   for (int j = 0; j < k; ++j) { n     int a, b; n     cin >> a >> b; n     a--; n     b--; n     if (find_set(a) == find_set(b)) { n       comp[find_set(a)] = 0; n     } n   } n   int mx = 0; n   for (int l = 0; l < n; ++l) { n     mx = max(mx, comp[l]); n   } n   cout << mx; n } n


#include <bits/stdc++.h> n using namespace std; n vector<pair<int, int> > G[100005]; n int dp[100005], Xor[100005]; n int rec(int n) { n   int answer = 1e9; n   for (int i = 0; i < G[n].size(); i++) { n     int st = G[n][i].first; n     int ed = G[n][i].second + st - 1; n     int x = Xor[ed] ^ Xor[st - 1]; n     if (x == 0) answer = min(answer, G[n][i].second); n   } n   return answer; n } n int main() { n   memset(dp, -1, sizeof(dp)); n   for (int i = 1; i <= 100000; i++) { n     for (int j = i, sum = i; sum <= 100000; j++, sum += j) { n       if (j - i + 1 >= 2) G[sum].push_back(make_pair(i, j - i + 1)); n     } n   } n   for (int n = 1; n <= 100000; n++) { n     set<int> ST; n     for (int i = 0; i < G[n].size(); i++) { n       int st = G[n][i].first; n       int ed = G[n][i].second + st - 1; n       int x = Xor[ed] ^ Xor[st - 1]; n       ST.insert(x); n     } n     for (int i = 0;; i++) { n       if (ST.find(i) == ST.end()) { n         dp[n] = i; n         break; n       } n     } n     Xor[n] = Xor[n - 1] ^ dp[n]; n   } n   int n; n   cin >> n; n   if (dp[n] == 0) { n     cout << -1 << endl; n     return 0; n   } n   cout << rec(n) << endl; n } n


#include <bits/stdc++.h> n using namespace std; n bool is_possible(int m, long long k, long long p[], int n, int x, int a, int y, n                  int b) { n   if (a != b) { n     int first = 0; n     int second = 0; n     int third = 0; n     for (int i = 1; i <= m; i++) { n       if (i % a == 0 && i % b == 0) n         third++; n       else if (i % a == 0) n         first++; n       else if (i % b == 0) n         second++; n     } n     long long ans1 = 0; n     for (int i = 0; i < third; i++) { n       ans1 += ((p[i] * (x + y)) / 100); n     } n     for (int i = third; i <= third + first - 1; i++) { n       ans1 += ((p[i] * x) / 100); n     } n     for (int i = third + first; i <= third + first + second - 1; i++) { n       ans1 += ((p[i] * y) / 100); n     } n     long long ans2 = 0; n     for (int i = 0; i < third; i++) { n       ans2 += ((p[i] * (x + y)) / 100); n     } n     for (int i = third; i <= third + second - 1; i++) { n       ans2 += ((p[i] * y) / 100); n     } n     for (int i = third + second; i <= third + second + first - 1; i++) { n       ans2 += ((p[i] * x) / 100); n     } n     if (ans1 >= k || ans2 >= k) return true; n     return false; n   } else { n     long long ans1 = 0; n     int first = 0; n     for (int i = 1; i <= m; i++) { n       if (i % a == 0) first++; n     } n     for (int i = 0; i < first; i++) { n       ans1 += ((p[i] * (x + y)) / 100); n     } n     if (ans1 < k) return false; n     return true; n   } n } n int main() { n   int t; n   cin >> t; n   while (t--) { n     int n; n     cin >> n; n     long long aa[n]; n     long long p[n]; n     for (int i = 0; i < n; i++) { n       cin >> aa[i]; n       p[i] = aa[i]; n     } n     int x, a, y, b; n     cin >> x >> a >> y >> b; n     long long k; n     cin >> k; n     sort(p, p + n, greater<long long>()); n     int l = 1; n     int h = n; n     int ans = -1; n     while (l <= h) { n       int m = l + (h - l) / 2; n       if (is_possible(m, k, p, n, x, a, y, b)) { n         ans = m; n         h = m - 1; n       } else { n         l = m + 1; n       } n     } n     cout << ans << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n void dbg_out() { cerr <<   b b] n ; } n template <typename Head, typename... Tail> n void dbg_out(Head H, Tail... T) { n   cerr << H <<  ,  ; n   dbg_out(T...); n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(nullptr); n   string s; n   cin >> s; n   int n = (int)s.size(); n   int curr = 0, last = -1; n   vector<int> ans; n   for (int i = 0; i < n; ++i) { n     if (s[i] ==  ) ) n       curr--; n     else if (s[i] ==  ( ) n       curr++; n     else { n       if (curr <= 0) { n         cout << -1; n         return 0; n       } n       last = i; n       curr--; n       ans.push_back(curr); n     } n     if (curr < 0) { n       cout << -1; n       return 0; n     } n   } n   if (last > -1) { n     int x = 0, neg = 0; n     for (int i = n - 1; i > last; --i) { n       if (s[i] ==  ) ) n         x--; n       else if (s[i] ==  ( ) n         x++; n       if (x > 0) { n         cout << -1; n         return 0; n       } n     } n     x = 0; n     for (int i = last + 1; i < n; ++i) { n       if (s[i] ==  ) ) n         x--; n       else if (s[i] ==  ( ) n         x++; n       neg = min(neg, x); n     } n     neg = ans.back() + neg; n     if (x > 0 or neg < 0) { n       cout << -1; n       return 0; n     } n     ans.pop_back(); n     ans.push_back(neg + 1); n   } n   for (int i = 0; i < (int)ans.size(); ++i) { n     if (i + 1 == (int)ans.size()) { n       cout << ans[i] <<   n ; n     } else n       cout << 1 <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct ball { n   float coord; n   int mass; n   float vel; n   short num; n }; n bool sort_num(ball a, ball b) { return a.num < b.num; } n bool sort_coord(ball a, ball b) { return a.coord < b.coord; } n void collide(ball &a, ball &b) { n   float temp = a.vel; n   a.vel = ((a.mass - b.mass) * a.vel + 2 * b.vel * b.mass) / (b.mass + a.mass); n   b.vel = ((b.mass - a.mass) * b.vel + 2 * temp * a.mass) / (b.mass + a.mass); n } n void iter(vector<ball> &balls, int size, float &cur_time, int time) { n   float col_t = 9999.9; n   int col_c = -1; n   float temp; n   for (int i = 0; i < size - 1; i++) { n     temp = (balls[i + 1].coord - balls[i].coord) / n            (balls[i].vel - balls[i + 1].vel); n     if (temp > 0.0000001 && temp < col_t) { n       col_t = temp; n       col_c = i; n     } n   } n   if (cur_time + col_t > time || col_c == -1) { n     col_t = time - cur_time; n     for (int i = 0; i < size; i++) balls[i].coord += balls[i].vel * col_t; n     cur_time += col_t; n     return; n   } n   for (int i = 0; i < size; i++) { n     balls[i].coord += balls[i].vel * col_t; n   } n   for (int i = 0; i < size - 1; i++) { n     if (fabs(balls[i].coord - balls[i + 1].coord) < 0.0000001) { n       collide(balls[i], balls[i + 1]); n       i++; n     } n   } n   cur_time += col_t; n } n int main() { n   int n, t; n   float cur_t = 0.0; n   cin >> n >> t; n   vector<ball> balls(n); n   for (int i = 0; i < n; i++) { n     cin >> balls[i].coord >> balls[i].vel >> balls[i].mass; n     balls[i].num = i; n   } n   sort(balls.begin(), balls.end(), sort_coord); n   while (cur_t < t) { n     iter(balls, n, cur_t, t); n   } n   sort(balls.begin(), balls.end(), sort_num); n   for (int i = 0; i < n; i++) cout << balls[i].coord << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n; n vector<int> v[10005]; n int color[10005]; n int flag[10005]; n void dfs(int src, int p, int ctr) { n   if (src == p) { n     return; n   } n   flag[src] = ctr; n   int i; n   for (i = 0; i < v[src].size(); i++) { n     if (color[v[src][i]] == color[src]) { n       if (v[src][i] != p) dfs(v[src][i], src, ctr); n     } n   } n } n int main(void) { n   scanf( %d , &n); n   int i; n   int x; n   for (i = 2; i <= n; i++) { n     scanf( %d , &x); n     v[i].push_back(x); n     v[x].push_back(i); n   } n   for (i = 1; i <= n; i++) { n     scanf( %d , &x); n     color[i] = x; n   } n   int ctr = 0; n   for (i = 1; i <= n; i++) { n     if (flag[i] == 0) { n       ctr++; n       dfs(i, -1, -ctr); n     } n   } n   cout << ctr << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const double pi = acos(-1.0); n int maxar(int arr[], int n) { n   int res = arr[0]; n   for (int i = 0; i < n; i++) { n     res = max(res, arr[i]); n   } n   return res; n } n long long maxar(long long arr[], long long n) { n   long long res = arr[0]; n   for (long long i = 0; i < n; i++) { n     res = max(res, arr[i]); n   } n   return res; n } n long long minar(long long arr[], long long n) { n   long long res = arr[0]; n   for (long long i = 0; i < n; i++) { n     res = min(res, arr[i]); n   } n   return res; n } n int minar(int arr[], int n) { n   int res = arr[0]; n   for (int i = 0; i < n; i++) { n     res = min(res, arr[i]); n   } n   return res; n } n void inpar(int arr[], int n) { n   for (int i = 0; i < n; i++) { n     cin >> arr[i]; n   } n } n void inpar(long long arr[], long long n) { n   for (long long i = 0; i < n; i++) { n     cin >> arr[i]; n   } n } n template <typename T> n inline T readInt() { n   T n = 0, s = 1; n   char p = getchar(); n   if (p ==  - ) s = -1; n   while ((p <  0  || p >  9 ) && p != EOF && p !=  - ) p = getchar(); n   if (p ==  - ) s = -1, p = getchar(); n   while (p >=  0  && p <=  9 ) { n     n = (n << 3) + (n << 1) + (p -  0 ); n     p = getchar(); n   } n   return n * s; n } n void solve() { n   int n = readInt<int>(); n   int a[n]; n   inpar(a, n); n   int res = 0; n   priority_queue<int> q; n   for (int i = 1; i < n; i++) { n     q.push(a[i]); n   } n   while (q.top() >= a[0]) { n     res++; n     a[0]++; n     int temp = q.top(); n     temp--; n     q.pop(); n     q.push(temp); n   } n   cout << res; n } n int32_t main() { n   int tc; n   tc = 1; n   while (tc--) { n     solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 200000 + 10; n int a[MAXN]; n int sum[MAXN]; n int cnt[100 + 10]; n vector<int> vc; n unordered_map<int, int> mp; n int exist[MAXN]; n int main() { n   int n; n   scanf( %d , &n); n   for (int i = 1; i <= n; ++i) { n     scanf( %d , &a[i]); n     cnt[a[i]]++; n   } n   int D = -1; n   bool flag = false; n   for (int i = 1; i <= 100; ++i) { n     if (cnt[i]) { n       vc.push_back(i); n       if (D == -1 || cnt[D] < cnt[i]) { n         D = i; n         flag = false; n       } else if (D != -1 && cnt[i] == cnt[D] && i != D) { n         flag = true; n       } n     } n   } n   if (flag) { n     printf( %d n , n); n     return 0; n   } n   sum[0] = 0; n   int ans = 0; n   for (int x : vc) { n     if (x == D) continue; n     mp.clear(); n     exist[0] = 0; n     mp[0] = 0; n     for (int i = 1; i <= n; ++i) { n       if (a[i] == D) n         sum[i] = sum[i - 1] + 1; n       else if (a[i] == x) n         sum[i] = sum[i - 1] - 1; n       else n         sum[i] = sum[i - 1] + 0; n       if (mp.count(sum[i]) == 0) mp[sum[i]] = i; n       if (a[i] == D) { n         exist[i] = exist[i - 1] + 1; n       } else { n         exist[i] = exist[i - 1]; n       } n     } n     int L = 0; n     for (int r = 1; r <= n; ++r) { n       if (mp.count(sum[r])) { n         int l = mp[sum[r]] + 1; n         if (l <= r && exist[r] - exist[l - 1] >= 1) { n           L = max(L, r - l + 1); n         } n       } n     } n     ans = max(ans, L); n   } n   printf( %d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   int pc[1 << 10]; n   for (size_t i = 0; i < (1 << 10); i++) { n     int buf = 0; n     for (int j = 0; j < 10; j++) { n       if ((1 << j) & i) buf++; n     } n     pc[i] = buf; n   } n   int n, m; n   cin >> n >> m; n   map<int, pair<unsigned long long, unsigned long long>> ans; n   vector<int> v1(n); n   vector<int> v2(m); n   for (size_t i = 0; i < n; i++) { n     cin >> v1[i]; n     v1[i] *= 2; n   } n   for (size_t i = 0; i < m; i++) { n     cin >> v2[i]; n     v2[i] *= 2; n   } n   for (size_t i = 0; i < n; i++) { n     for (size_t j = 0; j < m; j++) { n       int buf = (v1[i] + v2[j]) / 2; n       ans[buf].first |= (1ULL << i); n       ans[buf].second |= (1ULL << j); n     } n   } n   int ansv = 0; n   for (auto i = ans.begin(); i != ans.end(); i++) { n     for (auto j = i; j != ans.end(); j++) { n       long long a1 = i->second.first; n       long long a2 = j->second.first; n       long long b1 = i->second.second; n       long long b2 = j->second.second; n       long long buf1 = a1 | a2; n       long long buf2 = b1 | b2; n       int buf = 0; n       while (buf1) { n         int buff = buf1 & 1023; n         buf1 >>= 10; n         buf += pc[buff]; n       } n       while (buf2) { n         int buff = buf2 & 1023; n         buf2 >>= 10; n         buf += pc[buff]; n       } n       ansv = max(ansv, buf); n     } n   } n   cout << ansv; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m, a[105]; n int main() { n   int s = 0, t = 0; n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cin >> n >> m; n   for (int i = 0; i < n; i++) cin >> a[i]; n   sort(a, a + n); n   for (int i = 0; i < n; i++) { n     if (s >= m) n       break; n     else { n       s += a[n - i - 1]; n       t++; n     } n   } n   cout << t; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 300 * 1000 + 13; n int n; n long long a[N], b[N]; n void solve() { n   scanf( %d , &n); n   for (int i = 0; i < int(n); ++i) scanf( %lld%lld , &a[i], &b[i]); n   long long ans = 0, mn = 1e18; n   for (int i = 0; i < int(n); ++i) { n     int ni = (i + 1) % n; n     long long val = min(a[ni], b[i]); n     ans += a[ni] - val; n     mn = min(mn, val); n   } n   ans += mn; n   printf( %lld n , ans); n } n int main() { n   int T; n   scanf( %d , &T); n   for (int i = 0; i < int(T); ++i) solve(); n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios::sync_with_stdio(false); n   unsigned long long c = 0; n   short p; n   int n; n   cin >> n; n   for (int i = 0; i < n; i++) { n     cin >> p; n     c += p; n   } n   cout << setprecision(14) << ((double)c / n); n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int i, j, m, n, k, a[1100], b[1100][10], count = 0; n   cin >> n >> m >> k; n   for (i = 0; i <= m; i++) a[i] = 10000000; n   for (i = 1; i <= n; i++) { n     for (j = 0; j < 2; j++) cin >> b[i][j]; n     if (a[b[i][0]] > b[i][1]) a[b[i][0]] = b[i][1]; n   } n   for (i = 1; i <= m; i++) count += a[i]; n   if (count > k) n     cout << k << endl; n   else n     cout << count << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int a, b; n   char c[101][101]; n   cin >> a >> b; n   for (int i = 1; i <= a; i++) { n     for (int j = 1; j <= b; j++) { n       if (i % 2 != 0) { n         c[i][j] =  # ; n       } n     } n   } n   for (int i = 2; i <= a; i += 4) { n     for (int j = 1; j <= b; j++) { n       c[i][j] =  . ; n       c[i][b] =  # ; n     } n   } n   for (int i = 4; i <= a; i += 4) { n     for (int j = 1; j <= b; j++) { n       c[i][j] =  . ; n       c[i][1] =  # ; n     } n   } n   for (int i = 1; i <= a; i++) { n     for (int j = 1; j <= b; j++) { n       cout << c[i][j]; n     } n     cout << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n using ll = long long; n template <class T, class U> n using Pa = pair<T, U>; n template <class T> n using vec = vector<T>; n template <class T> n using vvec = vector<vec<T>>; n bool chmax(int &a, int b) { n   if (a < b) { n     a = b; n     return true; n   } n   return false; n } n vec<string> A = { 1110111 ,  0010010 ,  1011101 ,  1011011 ,  0111010 , n                   1101011 ,  1101111 ,  1010010 ,  1111111 ,  1111011 }; n int dp[2010][2010] = {}; n int pre[2010][2010] = {}; n int main() { n   cin.tie(0); n   ios::sync_with_stdio(false); n   int N, K; n   cin >> N >> K; n   vec<string> S(N); n   for (int i = 0; i < N; i++) cin >> S[i]; n   int M = 7; n   auto calc = [&](string T, int num) { n     string B = A[num]; n     bool ok = true; n     int c = 0; n     for (int i = 0; i < M; i++) { n       if (T[i] ==  1  && B[i] ==  0 ) ok = false; n       if (T[i] ==  0  && B[i] ==  1 ) c++; n     } n     if (!ok) n       return -1; n     else n       return c; n   }; n   for (int i = 0; i <= N; i++) n     for (int k = 0; k <= K; k++) dp[i][k] = -1; n   dp[0][0] = 0; n   for (int i = 0; i < N; i++) n     for (int j = 0; j <= K; j++) n       if (dp[i][j] != -1) { n         for (int n = 0; n < 10; n++) { n           int c = calc(S[N - 1 - i], n); n           if (c == -1) continue; n           if (j + c <= K) { n             if (chmax(dp[i + 1][j + c], n)) { n               pre[i + 1][j + c] = j; n             } n           } n         } n       } n   if (dp[N][K] == -1) { n     cout << -1 <<   n ; n     return 0; n   } n   string ans =   ; n   int now = K; n   for (int i = N - 1; i >= 0; i--) { n     char c =  0  + dp[i + 1][now]; n     ans += c; n     now = pre[i + 1][now]; n   } n   cout << ans <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n inline char gc() { n   static char buf[100000], *p1 = buf, *p2 = buf; n   return p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 100000, stdin), p1 == p2) n              ? EOF n              : *p1++; n } n inline long long read() { n   long long x = 0; n   char ch = getchar(); n   bool positive = 1; n   for (; !isdigit(ch); ch = getchar()) n     if (ch ==  - ) positive = 0; n   for (; isdigit(ch); ch = getchar()) x = x * 10 + ch -  0 ; n   return positive ? x : -x; n } n inline void write(long long a) { n   if (a < 0) { n     a = -a; n     putchar( - ); n   } n   if (a >= 10) write(a / 10); n   putchar( 0  + a % 10); n } n inline void writeln(long long a) { n   write(a); n   puts(  ); n } n inline void wri(long long a) { n   write(a); n   putchar(   ); n } n inline unsigned long long rnd() { n   return ((unsigned long long)rand() << 30 ^ rand()) << 4 | rand() % 4; n } n const int N = 300005, mod = 998244353; n pair<long long, long long> f[N]; n long long dp[N]; n int a[N], n, sum; n long long ksm(long long a, int b) { n   int ans = 1; n   for (; b; b >>= 1) { n     if (b & 1) ans = ans * a % mod; n     a = a * a % mod; n   } n   return ans; n } n pair<long long, long long> operator*(long long k, n                                      pair<long long, long long> a) { n   return make_pair(a.first * k % mod, a.second * k % mod); n } n pair<long long, long long> operator+(pair<long long, long long> a, n                                      pair<long long, long long> b) { n   return make_pair(a.first + b.first, a.second + b.second); n } n void ycl() { n   f[sum - 1] = make_pair(1, 0); n   long long a = ksm(sum, mod - 2), b = ksm(n - 1, mod - 2); n   for (int i = (int)(sum - 1); i >= (int)(1); i--) { n     f[i - 1] = n         -ksm(i, mod - 2) * sum % mod * n         ((sum - i) * a % mod * b % mod * f[i + 1] + n          ((sum - i) * a % mod * (1 - b) % mod - 1) * f[i] + make_pair(0, 1)); n   } n   pair<long long, long long> c = n       b * f[1] + (n - 2) * b % mod * f[0] + make_pair(0, 1); n   long long X = (c.second - f[0].second) % mod * n                 ksm((f[0].first - c.first) % mod, mod - 2) % mod; n   for (int i = (int)(0); i <= (int)(sum); i++) n     dp[i] = (f[i].first * X + f[i].second) % mod; n } n int main() { n   n = read(); n   for (int i = (int)(1); i <= (int)(n); i++) sum += (a[i] = read()); n   ycl(); n   long long ans = 0; n   for (int i = (int)(1); i <= (int)(n); i++) ans += dp[a[i]]; n   ans = ((ans - (n - 1) * dp[0]) % mod + mod) % mod; n   cout << ans * ksm(n, mod - 2) % mod << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long t; n   cin >> t; n   while (t--) { n     long long n; n     cin >> n; n     vector<long long> vis(n + 1, 0); n     vector<long long> res(n); n     vector<long long> v; n     int b; n     cin >> b; n     v.push_back(b); n     int cur = 1; n     int ok = 1; n     vis[v[0]] = 1; n     res[0] = v[0]; n     for (int i = 1, a; i < n; i++) { n       cin >> a; n       v.push_back(a); n       if (!ok) continue; n       if (v[i] > v[i - 1]) { n         vis[v[i]] = 1; n         vis[v[i - 1]] = 1; n         res[i] = v[i]; n         continue; n       } else { n         for (int j = cur; j < v[i]; j++) { n           if (vis[j] == 0) { n             vis[j] = 1; n             res[i] = j; n             cur = j + 1; n             break; n           } n         } n         if (res[i] == 0) { n           ok = 0; n         } n       } n     } n     if (!ok) n       cout << -1 << endl; n     else { n       for (int i = 0; i < res.size(); i++) n         if (res[i] == 0) n           cout << v[i] <<    ; n         else n           cout << res[i] <<    ; n       cout << endl; n     } n   } n } n


#include <bits/stdc++.h> n int main() { n   char a[100010]; n   scanf( %s , a); n   long long i = 0, x = 1; n   while (a[i] !=   0 ) { n     if (a[i] ==  0 ) { n       a[i] =  2 ; n       x = 0; n       break; n     } n     i++; n   } n   if (x) { n     a[0] =  2 ; n   } n   i = 0; n   while (a[i] !=   0 ) { n     if (a[i] !=  2 ) printf( %c , a[i]); n     i++; n   } n   printf(  n ); n   return (0); n } n


#include <bits/stdc++.h> n using namespace std; n template <class T1, class T2> n ostream& operator<<(ostream& os, const pair<T1, T2>& p) { n   os <<  {  << p.first <<  ,   << p.second <<  } ; n   return os; n } n template <class T> n ostream& operator<<(ostream& os, const vector<T>& p) { n   os <<  [ ; n   int i = 0; n   for (typename vector<T>::const_iterator it = p.begin(); it != p.end(); ++it) { n     os << (i == 0 ?    :  ,  ) << *it; n     i++; n   } n   os <<  ] ; n   return os; n } n template <class T> n ostream& operator<<(ostream& os, const set<T>& p) { n   os <<  [ ; n   int i = 0; n   for (typename set<T>::const_iterator it = p.begin(); it != p.end(); ++it) { n     os << (i == 0 ?    :  ,  ) << *it; n     i++; n   } n   os <<  ] ; n   return os; n } n template <class K, class V> n ostream& operator<<(ostream& os, const map<K, V>& p) { n   os <<  [ ; n   int i = 0; n   for (typename map<K, V>::const_iterator it = p.begin(); it != p.end(); ++it) { n     os << (i == 0 ?    :  ,  ) << *it; n     i++; n   } n   os <<  ] ; n   return os; n } n template <class T> n string toString(const T& o) { n   stringstream ss; n   ss << o; n   string ret; n   getline(ss, ret); n   return ret; n } n template <class T> n vector<T> subv(const vector<T>& v, int from, int to) { n   return vector<T>(v.begin() + min(max(0, from), (int)v.size()), n                    v.begin() + min(max(0, to), (int)v.size())); n } n pair<int, int> operator+(const pair<int, int>& a, const pair<int, int>& b) { n   return make_pair(a.first + b.first, a.second + b.second); n } n pair<int, int> operator*(const int& a, const pair<int, int>& b) { n   return make_pair(a * b.first, a * b.second); n } n int main() { n   int n; n   cin >> n; n   if (n == 1) { n     cout <<  a na nb nb n ; n   } else if (n == 2) { n     cout <<  aa nbb ncc ndd n ; n   } else { n     if (n % 2 == 0) { n       for (int i = 0; i < n / 2; ++i) cout << (i % 2 == 0 ?  aa  :  bb ); n       cout << endl; n       for (int i = 0; i < n / 2; ++i) cout << (i % 2 != 0 ?  aa  :  bb ); n       cout << endl; n       cout <<  e ; n       for (int i = 0; i < n / 2 - 1; ++i) cout << (i % 2 == 0 ?  cc  :  dd ); n       cout <<  e  << endl; n       cout <<  e ; n       for (int i = 0; i < n / 2 - 1; ++i) cout << (i % 2 != 0 ?  cc  :  dd ); n       cout <<  e  << endl; n     } else { n       for (int i = 0; i < n / 2; ++i) cout << (i % 2 == 0 ?  aa  :  bb ); n       cout <<  e  << endl; n       for (int i = 0; i < n / 2; ++i) cout << (i % 2 != 0 ?  aa  :  bb ); n       cout <<  e  << endl; n       cout <<  e ; n       for (int i = 0; i < n / 2; ++i) cout << (i % 2 == 0 ?  cc  :  dd ); n       cout << endl; n       cout <<  e ; n       for (int i = 0; i < n / 2; ++i) cout << (i % 2 != 0 ?  cc  :  dd ); n       cout << endl; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int t = 1; n   while (t--) { n     int n, k; n     cin >> n >> k; n     vector<int> v(n); n     int itr = 1; n     for (int& x : v) { n       cin >> x; n     } n     set<int> bag{0}, tmp; n     map<int, vector<int>> pep; n     for (int i = 0; i < n && bag.size() <= k; i++) { n       tmp = bag; n       for (auto x : tmp) { n         if (bag.find(x + v[i]) == bag.end()) { n           bag.insert(x + v[i]); n           pep[x + v[i]] = pep[x]; n           pep[x + v[i]].push_back(v[i]); n         } n         if (bag.size() > k) break; n       } n     } n     pep.erase(pep.begin()); n     for (auto& it : pep) { n       cout << it.second.size() <<    ; n       for (auto& x : it.second) { n         cout << x <<    ; n       } n       cout <<   n ; n     } n   } n   cerr <<  processor time:   << clock() / (double)CLOCKS_PER_SEC <<  s     ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n char str[1000005]; n int ar[1000005]; n void solve() { n   int n = (int)strlen(str + 1), i, j, ans = 0; n   memset(ar, 0, sizeof(ar)); n   str[0] =  0 ; n   for (i = n; i >= 0; i--) { n     if (str[i] ==  1 ) { n       j = i; n       while (str[j] ==  1 ) --j; n       if (i - j > 1) n         ar[i] = -1, ar[j] = 1, str[j] =  1 ; n       else { n         int k; n         for (k = -~j; k <= i; k++) ar[k] = 1; n       } n       i = -~j; n     } n   } n   for (i = 0; i <= n; i++) n     if (ar[i]) ans++; n   printf( %d n , ans); n   for (i = 0; i <= n && ans; i++) { n     if (ar[i]) { n       putchar(~ar[i] ?  +  :  - ), printf( 2^%d n , n - i); n       --ans; n     } n   } n } n int main(void) { n   while (~scanf( %s , str + 1)) { n     solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   string s, t; n   cin >> s >> t; n   reverse(t.begin(), t.end()); n   for (int i = 0; i < s.size(); i++) { n     if (s[i] != t[i]) { n       cout <<  NO ; n       return 0; n     } n   } n   cout <<  YES ; n } n


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


#include <bits/stdc++.h> n using namespace std; n int A[26]; n int main() { n   int n; n   cin >> n; n   string s; n   cin >> s; n   int cnt = 0; n   for (int i = 0; i < n; i++) s[i] = tolower(s[i]); n   for (int i = 0; i < n; i++) { n     if (!A[s[i] -  a ]) { n       A[s[i] -  a ] = 1; n       ++cnt; n     } n   } n   if (cnt == 26) n     cout <<  YES ; n   else n     cout <<  NO ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   int n, m, gn1 = 0, gj1 = 0, gn2 = 0, gj2 = 0; n   cin >> n >> m; n   for (int i = 0; i < n; i++) { n     int x; n     cin >> x; n     if (x % 2 == 0) n       gn1++; n     else n       gj1++; n   } n   for (int i = 0; i < m; i++) { n     int x; n     cin >> x; n     if (x % 2 == 0) n       gn2++; n     else n       gj2++; n   } n   cout << min(gj1, gn2) + min(gj2, gn1) <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n ifstream fin( universal.in ); n ofstream fout(  ); n int n, m; n set<int> on; n int main() { n   cin >> n >> m; n   for (int i = 1; i <= n; ++i) { n     int aux; n     cin >> aux; n     for (int j = 1, bb; j <= aux; ++j) { n       cin >> bb; n       on.insert(bb); n     } n     if (on.size() == m) { n       cout <<  YES n ; n       return 0; n     } n   } n   cout <<  NO n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int p, i, j; n   scanf( %d , &p); n   int n = 1 << (p + 1); n   vector<int> a(n); n   for (i = 2; i < n; i++) scanf( %d , &a[i]); n   vector<int> up(n), mx(n); n   for (i = 1; i < n; i++) { n     int sum = 0; n     for (j = i; j > 1; j /= 2) sum += a[j]; n     up[i] = sum; n   } n   int maxVal = *max_element(up.begin(), up.end()); n   for (i = n / 2; i < n; i++) { n     mx[i] += maxVal - up[i]; n   } n   for (i = n - 1; i > 1; i--) { n     int c = min(mx[i], mx[i ^ 1]); n     mx[i] -= c; n     mx[i ^ 1] -= c; n     mx[i / 2] += c; n   } n   printf( %d n , accumulate(mx.begin(), mx.end(), 0)); n } n


#include <bits/stdc++.h> n using namespace std; n const long long mod = 1e9 + 7; n long long n, k; n long long POW(long long a, long long n) { n   if (!n) return 1; n   long long ans = 1; n   while (n) { n     if (n & 1) ans = ans * a % mod; n     n >>= 1; n     a = a * a % mod; n   } n   return ans; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cin >> n >> k; n   if (!n) return cout << 0, 0; n   n %= mod; n   cout << (POW(2, k + 1) * n % mod - POW(2, k) % mod + 1 + mod * mod) % mod; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long check, tn, tm, maxn; n long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; } n long long exgcd(long long a, long long b, long long &x, long long &y) { n   if (b == 0) { n     x = 1, y = 0; n     return a; n   } n   long long d = exgcd(b, a % b, x, y); n   long long z = x; n   x = y; n   y = z - y * (a / b); n   return d; n } n long long solve(long long x, long long y) { n   long long c = x + y, dx, dy; n   long long d = exgcd(tn, tm, dx, dy); n   if (c % d) return maxn + 1; n   long long mod = tm / d; n   dx *= c / d; n   if (mod < 0) mod = -mod; n   dx = (dx % mod + mod) % mod; n   long long ans = tn * dx - x; n   if (ans < 0 || ans > maxn) return maxn + 1; n   return ans; n } n int main() { n   long long n, m, k, x, y; n   cin >> n >> m >> k; n   maxn = n * m / gcd(n, m); n   tn = n << 1, tm = -(m << 1); n   for (int i = 1; i <= k; ++i) { n     cin >> x >> y; n     long long ans = maxn + 1; n     ans = min(ans, solve(x, y)); n     ans = min(ans, solve(x, -y)); n     ans = min(ans, solve(-x, y)); n     ans = min(ans, solve(-x, -y)); n     if (ans == maxn + 1) n       cout << -1 << endl; n     else n       cout << ans << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 2 * 1e5 + 10; n inline long long po(long long a, long long p, long long mod) { n   long long r = 1; n   for (; p; p >>= 1, a = a * a % mod) n     if (p & 1) r = r * a % mod; n   return r; n } n const long long mod1 = 1e9 + 7; n const long long mod2 = 1e9 + 9; n const long long bas = 1e9 + 3; n int n; n int m; n long long a[N]; n long long b[N]; n long long pa1; n long long pa2; n long long sa1; n long long sa2; n long long pb1; n long long pb2; n long long sb1; n long long sb2; n long long mi1[N]; n long long mi2[N]; n long long imi1[N]; n long long imi2[N]; n long long ans[N]; n int tp; n long long inv1; n long long inv2; n int main() { n   scanf( %d%d , &n, &m); n   for (int i = 1; i <= n; i++) scanf( %lld , &a[i]); n   sort(a + 1, a + n + 1); n   for (int i = 1, j = n; i <= n; i++, j--) b[j] = a[i]; n   mi1[0] = 1; n   for (int i = 1; i <= n; i++) (mi1[i] = mi1[i - 1] * bas) %= mod1; n   mi2[0] = 1; n   for (int i = 1; i <= n; i++) (mi2[i] = mi2[i - 1] * bas) %= mod2; n   for (int i = n; i >= 1; i--) n     pa1 = (pa1 * bas + a[i]) % mod1, pa2 = (pa2 * bas + a[i]) % mod2, n     pb1 = (pb1 * bas + b[i]) % mod1, pb2 = (pb2 * bas + b[i]) % mod2; n   inv1 = po(po(bas, n, mod1) + mod1 - 1, mod1 - 2, mod1); n   inv2 = po(po(bas, n, mod2) + mod2 - 1, mod2 - 2, mod2); n   for (int i = n; i >= 0; i--) { n     long long t1 = ((pa1 + pb1) + (sa1 + mod1 - sb1)) % mod1; n     long long x1 = (bas - 1) * t1 % mod1 * inv1 % mod1; n     long long t2 = ((pa2 + pb2) + (sa2 + mod2 - sb2)) % mod2; n     long long x2 = (bas - 1) * t2 % mod2 * inv2 % mod2; n     if (x1 == x2 && x1 < m) ans[++tp] = x1; n     pa1 = (pa1 + mod1 - a[i] * mi1[i - 1] % mod1) % mod1; n     pa2 = (pa2 + mod2 - a[i] * mi2[i - 1] % mod2) % mod2; n     sa1 = (sa1 + a[i] * mi1[i - 1]) % mod1; n     sa2 = (sa2 + a[i] * mi2[i - 1]) % mod2; n     pb1 = (pb1 + mod1 - a[i]) * 750000005 % mod1; n     pb2 = (pb2 + mod2 - a[i]) * 166666668 % mod2; n     sb1 = (sb1 * 750000005 + mi1[n - 1] * (m - a[i])) % mod1; n     sb2 = (sb2 * 166666668 + mi2[n - 1] * (m - a[i])) % mod2; n   } n   printf( %d n , tp); n   sort(ans + 1, ans + tp + 1); n   for (int i = 1; i <= tp; i++) printf( %lld  , ans[i]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long inf = 0x3f3f3f3f3f3f3f3f; n long long dx[] = {0, 1, 1, 1, 0, -1, -1, -1}; n long long dy[] = {1, 1, 0, -1, -1, -1, 0, 1}; n struct BipartiteMatcher { n   vector<vector<long long>> G; n   vector<long long> L, R, Viz; n   BipartiteMatcher(long long n, long long m) n       : G(n), L(n, -1), R(m, -1), Viz(n) {} n   void AddEdge(long long a, long long b) { G[a].push_back(b); } n   bool Match(long long node) { n     if (Viz[node]) return false; n     Viz[node] = true; n     for (auto vec : G[node]) { n       if (R[vec] == -1) { n         L[node] = vec; n         R[vec] = node; n         return true; n       } n     } n     for (auto vec : G[node]) { n       if (Match(R[vec])) { n         L[node] = vec; n         R[vec] = node; n         return true; n       } n     } n     return false; n   } n   long long Solve() { n     long long ok = true; n     while (ok--) { n       fill(Viz.begin(), Viz.end(), 0); n       for (long long i = 0; i < (long long)L.size(); ++i) n         if (L[i] == -1) ok |= Match(i); n     } n     long long ret = 0; n     for (long long i = 0; i < L.size(); ++i) ret += (L[i] != -1); n     return ret; n   } n }; n bool comp(const pair<long long, pair<long long, long long>> &a, n           const pair<long long, pair<long long, long long>> &b) { n   return a.second.second < b.second.second; n } n int32_t main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   ; n   long long t = 1; n   while (t--) { n     long long n, m; n     cin >> n >> m; n     vector<pair<long long, pair<long long, long long>>> roads; n     for (long long i = 0; i < m; ++i) { n       long long u, v, w; n       cin >> u >> v >> w; n       --u; n       --v; n       roads.push_back({u, {v, w}}); n     } n     sort(roads.begin(), roads.end(), comp); n     long long lo = 0, hi = 1e9 + 1; n     while (lo < hi) { n       long long md = (lo + hi) >> 1; n       BipartiteMatcher graph(n, n); n       for (auto it : roads) { n         if (it.second.second <= md) n           graph.AddEdge(it.first, it.second.first); n         else n           break; n       } n       long long cnt = graph.Solve(); n       if (cnt < n) { n         lo = md + 1; n       } else n         hi = md; n     } n     if (hi == (1e9 + 1)) n       cout << -1; n     else n       cout << hi; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n #define fi first n #define se second n #define fill0(a) memset(a,0,sizeof(a)) n #define fill1(a) memset(a,-1,sizeof(a)) n #define fillbig(a) memset(a,63,sizeof(a)) n #define pb push_back n #define ppb pop_back n #define mp make_pair n #define mt make_tuple n #define eprintf(...) fprintf(stderr,__VA_ARGS__) n template<typename T1,typename T2> void chkmin(T1 &x,T2 y){if(x>y) x=y;} n template<typename T1,typename T2> void chkmax(T1 &x,T2 y){if(x<y) x=y;} n typedef pair<int,int> pii; n typedef long long ll; n typedef unsigned int u32; n typedef unsigned long long u64; n typedef long double ld; n namespace fastio{ n  #define FILE_SIZE 1<<23 n  char rbuf[FILE_SIZE],*p1=rbuf,*p2=rbuf,wbuf[FILE_SIZE],*p3=wbuf; n  inline char getc(){return p1==p2&&(p2=(p1=rbuf)+fread(rbuf,1,FILE_SIZE,stdin),p1==p2)?-1:*p1++;} n  inline void putc(char x){(*p3++=x);} n  template<typename T> void read(T &x){ n   x=0;char c=getchar();T neg=0; n   while(!isdigit(c)) neg|=!(c^ - ),c=getchar(); n   while(isdigit(c)) x=(x<<3)+(x<<1)+(c^48),c=getchar(); n   if(neg) x=(~x)+1; n  } n  template<typename T> void recursive_print(T x){return (!x)?void():(recursive_print(x/10),putc(x%10^48),void());} n  template<typename T> void print(T x){(!x)&&(putc( 0 ),0);(x<0)&&(putc( - ),x=~x+1);recursive_print(x);} n  template<typename T> void print(T x,char c){print(x);putc(c);} n  void print_final(){fwrite(wbuf,1,p3-wbuf,stdout);} n } n const int MAXN=2e5; n const int MAXV=1e6; n int n,qu,s,d,a[MAXN+5],f[MAXN+5],pos[MAXV+5]; n set<int> cmp[MAXN+5];int cmp_cnt=0; n int find(int x){return (!f[x])?x:f[x]=find(f[x]);} n bool merge(int x,int y){ n  x=find(x);y=find(y);if(x==y) return 0; n  if(cmp[x].size()<cmp[y].size()) swap(x,y); n  for(int v:cmp[y]) cmp[x].insert(v); n  cmp[y].clear();f[y]=x;cmp_cnt--;return 1; n } n set<int> st; n int hd[MAXN+5],to[MAXN*2+5],nxt[MAXN*2+5],val[MAXN*2+5],ec=0; n void adde(int u,int v,int w){/*printf( %d %d %d n ,u,v,w);*/to[++ec]=v;val[ec]=w;nxt[ec]=hd[u];hd[u]=ec;} n int mx[MAXN+5]; n void dfs(int x,int f){ n  for(int e=hd[x];e;e=nxt[e]){ n   int y=to[e],z=val[e];if(y==f) continue; n   mx[y]=max(mx[x],z);dfs(y,x); n  } n } n int main(){ n  scanf( %d%d%d%d ,&n,&qu,&s,&d);cmp_cnt=n; n  for(int i=1;i<=n;i++){ n   scanf( %d ,&a[i]);st.insert(a[i]); n   pos[a[i]]=i;cmp[i].insert(i); n  } n  while(cmp_cnt>1){ n   vector<pair<int,pii> > ed; n   for(int i=1;i<=n;i++) if(!cmp[i].empty()){ n    for(int x:cmp[i]) st.erase(st.find(a[x])); n    for(int x:cmp[i]){ n     for(int j=-1,v=-d;j<=1;j++,v=j*d) if(j!=0){ n      auto it=st.lower_bound(a[x]+v); n      if(it!=st.end()) ed.pb(mp(abs(abs(a[x]-(*it))-d),mp(x,pos[*it]))); n      if(it!=st.begin()){--it;ed.pb(mp(abs(abs(a[x]-(*it))-d),mp(x,pos[*it])));} n     } n    } n    for(int x:cmp[i]) st.insert(a[x]); n   } sort(ed.begin(),ed.end()); n   for(int i=0;i<ed.size();i++){ n    int w=ed[i].fi,x=ed[i].se.fi,y=ed[i].se.se; n    if(merge(x,y)) adde(x,y,w),adde(y,x,w); n   } n  } dfs(s,0); n  while(qu--){ n   int x,k;scanf( %d%d ,&x,&k); n   printf( %s n ,(mx[x]<=k)? Yes : No ); n  } n  return 0; n }


#include <bits/stdc++.h> n using namespace std; n template <typename T> n struct fenwick_tree { n   int tree_n = 0; n   T tree_sum = 0; n   vector<T> tree; n   fenwick_tree(int n = -1) { n     if (n >= 0) init(n); n   } n   void init(int n) { n     tree_n = n; n     tree_sum = 0; n     tree.assign(tree_n + 1, 0); n   } n   template <typename T_array> n   void build(const T_array &initial) { n     assert((int)initial.size() == tree_n); n     tree_sum = 0; n     for (int i = 1; i <= tree_n; i++) { n       tree[i] = initial[i - 1]; n       tree_sum += initial[i - 1]; n       for (int k = (i & -i) >> 1; k > 0; k >>= 1) tree[i] += tree[i - k]; n     } n   } n   void update(int index, const T &change) { n     assert(0 <= index && index < tree_n); n     tree_sum += change; n     for (int i = index + 1; i <= tree_n; i += i & -i) tree[i] += change; n   } n   T query(int count) const { n     assert(count <= tree_n); n     T sum = 0; n     for (int i = count; i > 0; i -= i & -i) sum += tree[i]; n     return sum; n   } n   T query_suffix(int start) const { return tree_sum - query(start); } n   T query(int a, int b) const { return query(b) - query(a); } n   T get(int a) const { n     assert(0 <= a && a < tree_n); n     int above = a + 1; n     T sum = tree[above]; n     above -= above & -above; n     while (a != above) { n       sum -= tree[a]; n       a -= a & -a; n     } n     return sum; n   } n   int find_last_prefix(T sum) const { n     if (sum < 0) return -1; n     int prefix = 0; n     for (int k = 31 - __builtin_clz(tree_n); k >= 0; k--) n       if (prefix + (1 << k) <= tree_n && tree[prefix + (1 << k)] <= sum) { n         prefix += 1 << k; n         sum -= tree[prefix]; n       } n     return prefix; n   } n }; n const int MASK = (1 << 30) - 1; n string S; n vector<pair<int, int>> W_stack; n fenwick_tree<int> suffixes; n long long current = 0; n vector<int> fail = {0}; n int kmp_len = 0; n vector<int> link; n int query_stack_value(int index) { n   assert(!W_stack.empty()); n   int low = 0, high = W_stack.size() - 1; n   while (low < high) { n     int mid = (low + high + 1) / 2; n     if (W_stack[mid].second <= index) n       low = mid; n     else n       high = mid - 1; n   } n   return W_stack[low].first; n } n void add_to_stack(int index, int w) { n   while (!W_stack.empty() && w <= W_stack.back().first) { n     int value = W_stack.back().first; n     int prev_index = W_stack.back().second; n     current -= (long long)suffixes.query(prev_index, index) * (value - w); n     index = prev_index; n     W_stack.pop_back(); n   } n   W_stack.emplace_back(w, index); n } n void add_char(char c) { n   S += c; n   if (S.size() > 1) { n     while (kmp_len > 0 && S[kmp_len] != S.back()) kmp_len = fail[kmp_len]; n     if (S[kmp_len] == S.back()) kmp_len++; n   } n   fail.push_back(kmp_len); n   int index = S.size() - 1; n   if (index > 0 && S[index] == S[fail[index]]) n     link.push_back(link[fail[index]]); n   else n     link.push_back(fail[index]); n } n void remove_suffixes(int len, char c) { n   while (len > 0) { n     if (len < (int)S.size() && S[len] != c) { n       int remove = S.size() - len; n       current -= query_stack_value(remove); n       suffixes.update(remove, -1); n     } n     if (len < (int)S.size() && S[len] == c) n       len = link[len]; n     else n       len = fail[len]; n   } n } n int main() { n   int N; n   scanf( %d , &N); n   suffixes.init(N); n   const long long LOW_BASE = 1e12; n   long long ans_high = 0, ans_low = 0; n   long long ans_mask = 0, ans26 = 0; n   for (int i = 0; i < N; i++) { n     char c; n     int w; n     scanf(  %c %d , &c, &w); n     c =  a  + (c -  a  + ans26) % 26; n     w ^= ans_mask; n     remove_suffixes(i, c); n     add_char(c); n     add_to_stack(i, w); n     if (c == S[0]) { n       suffixes.update(i, +1); n       current += w; n     } n     ans_low += current % LOW_BASE; n     ans_high += current / LOW_BASE + ans_low / LOW_BASE; n     ans_low %= LOW_BASE; n     ans_mask = (ans_mask + current) & MASK; n     ans26 = (ans26 + current) % 26; n     if (ans_high != 0) n       printf( %lld%012lld n , ans_high, ans_low); n     else n       printf( %lld n , ans_low); n   } n } n


#include <bits/stdc++.h> n using namespace std; n int dp[105][2005]; n int main() { n   memset(dp, -1, sizeof(dp)); n   int v1, v2, t, d; n   cin >> v1 >> v2 >> t >> d; n   dp[1][v1] = v1; n   for (int i = 1; i < t; i++) n     for (int j = 0; j < 2005; j++) n       if (dp[i][j] != -1) n         for (int k = -d; k <= d; k++) n           if (j + k >= 0) n             dp[i + 1][j + k] = max(dp[i + 1][j + k], dp[i][j] + j + k); n   if (dp[t][v2] == -1) n     cout <<  0 n ; n   else n     cout << dp[t][v2] <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long INF = 9223372036854775807ll; n const long long dx[] = {1, -1, 0, 0}, dy[] = {0, 0, 1, -1}, inf = 2147483647, n                 nmax = 300005; n struct trip { n   long long a, b, c; n }; n long long n, m, s[nmax]; n vector<pair<long long, long long> > neg, poz; n vector<trip> sol; n signed main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   cin >> n >> m; n   for (long long i = 1; i <= m; ++i) { n     long long x, y, w; n     cin >> x >> y >> w; n     s[x] -= w; n     s[y] += w; n   } n   for (long long i = 1; i <= n; ++i) { n     if (s[i] > 0) poz.push_back({i, s[i]}); n     if (s[i] < 0) neg.push_back({i, -s[i]}); n   } n   while (poz.size() && neg.size()) { n     long long x = min(poz.back().second, neg.back().second); n     long long a = neg.back().first, b = poz.back().first; n     sol.push_back({a, b, x}); n     poz.back().second -= x; n     neg.back().second -= x; n     if (poz.back().second == 0) poz.pop_back(); n     if (neg.back().second == 0) neg.pop_back(); n   } n   cout << sol.size() <<   n ; n   for (auto x : sol) cout << x.a <<     << x.b <<     << x.c <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n using ll = long long; n using vll = vector<ll>; n using vii = vector<int>; n using pii = pair<int, int>; n template <typename T = vii> n using vec = vector<T>; n const ll INF = 1e18, MOD = 998244353; n const int MAX = 1e6; n ll ft[MAX + 1]; n void cf() { n   ft[0] = 1; n   for (int i = 1; i <= MAX; i++) { n     ft[i] = (i * ft[i - 1]) % MOD; n   } n } n int main() { n   cin.tie(NULL); n   ios_base::sync_with_stdio(false); n   cf(); n   int n; n   cin >> n; n   unordered_map<int, int> cnt; n   for (int i = 1; i < n; i++) { n     int u, v; n     cin >> u >> v; n     u--, v--; n     cnt[u]++; n     cnt[v]++; n   } n   ll res = 1; n   for (auto p : cnt) { n     (res *= ft[p.second]) %= MOD; n   } n   cout << (n * res) % MOD <<   n ; n } n


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


#include <bits/stdc++.h> n using namespace std; n long long T, n, tempur; n int t[105], l[105], h[105]; n int main() { n   cin >> T; n   while (T--) { n     memset(t, 0, sizeof t); n     memset(h, 0, sizeof h); n     memset(l, 0, sizeof l); n     cin >> n >> tempur; n     int maxn = tempur, minn = tempur; n     for (int i = 1; i <= n; i++) { n       cin >> t[i] >> l[i] >> h[i]; n     } n     int f = 0; n     for (int i = 1; i <= n; i++) { n       maxn += t[i] - t[i - 1]; n       minn -= t[i] - t[i - 1]; n       if ((h[i] >= minn && l[i] <= maxn) || (h[i] <= maxn && l[i] >= minn)) { n         maxn = min(maxn, h[i]); n         minn = max(minn, l[i]); n       } else { n         f = 1; n         break; n       } n     } n     if (!f) n       cout <<  YES  << endl; n     else n       cout <<  NO  << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n signed main() { n   long long N, B; n   cin >> N >> B; n   vector<long long> res(N, -1); n   queue<pair<long long, long long> > Q; n   long long endTime = -1; n   for (long long i = 0; i < N; i++) { n     long long t, d; n     cin >> t >> d; n     while (endTime <= t) { n       if (Q.size()) { n         res[Q.front().second] = endTime = endTime + Q.front().first; n         Q.pop(); n       } else { n         endTime = t; n         break; n       } n     } n     if (Q.size() < B) { n       Q.push({d, i}); n     } n   } n   while (Q.size()) { n     res[Q.front().second] = endTime = endTime + Q.front().first; n     Q.pop(); n   } n   for (long long i = 0; i < N; i++) cout << res[i] <<    ; n   cout << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int N; n int A[100005]; n int psm[100005]; n int DP[100005][20]; n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   cin >> N; n   for (int i = 1; i <= N; i++) { n     cin >> A[i]; n     psm[i] = (psm[i - 1] + A[i]) % 10; n   } n   for (int d = 1; d <= 17; d++) { n     if ((1 << d) > N) break; n     int l = (1 << d); n     for (int i = 1; i <= N; i++) { n       if (i + l - 1 > N) break; n       int m = i + l / 2 - 1; n       int r = i + l - 1; n       DP[i][d] = n           DP[i][d - 1] + DP[m + 1][d - 1] + n           ((psm[m] - psm[i - 1] + 10) % 10 + (psm[r] - psm[m] + 10) % 10 >= 10); n     } n   } n   int Q; n   int l, r; n   cin >> Q; n   while (Q--) { n     cin >> l >> r; n     int ln = (r - l + 1), d = 0; n     while (ln > 1) { n       d++, ln /= 2; n     } n     cout << DP[l][d] <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using ll = long long; n using ld = long double; n using ull = unsigned long long; n using namespace std; n const int N = 2002, NN = 502, mod = 1e9 + 7, mod1 = 1e9 + 9, Base = 317, n           Base1 = 371; n int n, K, pw[NN], pw1[NN], lcp[N], mn[N][11], id[N]; n string s[N]; n struct Hash { n   vector<pair<int, int> > hs; n   Hash() {} n   Hash(const string &s) : hs(((int)(s).size()) + 1, {0, 0}) { n     for (int i = (1); i <= (((int)(s).size())); ++i) n       hs[i] = {(hs[i - 1].first * 1LL * Base + s[i - 1]) % mod, n                (hs[i - 1].second * 1LL * Base1 + s[i - 1]) % mod1}; n   } n   pair<int, int> query(int l, int r) { n     pair<int, int> h = { n         hs[r].first - hs[l - 1].first * 1LL * pw[r - l + 1] % mod, n         hs[r].second - hs[l - 1].second * 1LL * pw1[r - l + 1] % mod1}; n     if (h.first < 0) h.first += mod; n     if (h.second < 0) h.second += mod; n     return h; n   } n } hs[N]; n int getLcp(const int &A, const int &B) { n   int l = 1, r = min(((int)(s[A]).size()), ((int)(s[B]).size())); n   while (l <= r) { n     int m = (l + r) >> 1; n     if (hs[A].query(1, m) == hs[B].query(1, m)) n       l = m + 1; n     else n       r = m - 1; n   } n   return r; n } n bool cmp(const int &A, const int &B) { n   int r = getLcp(A, B); n   if (r == ((int)(s[B]).size())) return 0; n   if (r == ((int)(s[A]).size())) return 1; n   return s[A][r] < s[B][r]; n } n int queryLcp(int l, int r) { n   int k = 31 - __builtin_clz(r - l + 1); n   return min(mn[l][k], mn[r - (1 << k) + 1][k]); n } n vector<int> solve(int l, int r) { n   if (l == r) return vector<int>(2, 0); n   vector<int> dp(r - l + 2, 0); n   int mid = l + 1; n   for (int i = (l + 2); i <= (r); ++i) n     if (lcp[i] < lcp[mid]) mid = i; n   vector<int> dpLeft = solve(l, mid - 1), dpRight = solve(mid, r); n   for (int i = (0); i <= (mid - l); ++i) n     for (int j = (0); j <= (r - mid + 1); ++j) n       dp[i + j] = max(dp[i + j], dpLeft[i] + dpRight[j] + i * j * lcp[mid]); n   return dp; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(nullptr); n   cin >> n >> K; n   for (int i = (1); i <= (n); ++i) cin >> s[i], id[i] = i, hs[i] = Hash(s[i]); n   sort(id + 1, id + 1 + n, cmp); n   for (int i = (2); i <= (n); ++i) lcp[i] = getLcp(id[i - 1], id[i]); n   cout << solve(1, n)[K]; n   return 0; n } n


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


#include <bits/stdc++.h> n using namespace std; n const long long maxn = 1e6 + 5, mod = 1e9 + 7, inf = 1e18; n long long n, q, a[maxn], mx[2ll][maxn]; n pair<long long, long long> dp[2ll][maxn]; n void cal_mx0(long long x) { n   long long l = n - 1ll, sum = a[n]; n   for (long long i = n; i > 0; i--) { n     if (l == 0) { n       mx[0][i] = 1; n       sum -= a[i]; n       continue; n     } n     while ((l > 0) && (sum + a[l] <= x)) { n       sum += a[l]; n       l--; n     } n     mx[0][i] = l + 1ll; n     sum -= a[i]; n   } n } n void cal_mx1(long long x) { n   long long r = 2ll, sum = a[1]; n   for (long long i = 1; i <= n; i++) { n     if (r == n + 1ll) { n       mx[1][i] = n; n       sum -= a[i]; n       continue; n     } n     while ((r <= n) && (sum + a[r] <= x)) { n       sum += a[r]; n       r++; n     } n     mx[1][i] = r - 1ll; n     sum -= a[i]; n   } n } n void cal_dp0() { n   long long sum = 0; n   for (long long i = 1; i <= n; i++) { n     sum += a[i]; n     if (mx[0][i] == 1) { n       dp[0][i] = {1, sum}; n       continue; n     } n     dp[0][i].first = dp[0][mx[0][i] - 1ll].first + 1ll; n     dp[0][i].second = dp[0][mx[0][i] - 1ll].second; n   } n } n void cal_dp1() { n   long long sum = 0; n   for (long long i = n; i > 0; i--) { n     sum += a[i]; n     if (mx[1][i] == n) { n       dp[1][i] = {1, sum}; n       continue; n     } n     dp[1][i].first = dp[1][mx[1][i] + 1ll].first + 1ll; n     dp[1][i].second = dp[1][mx[1][i] + 1ll].second; n   } n } n int main() { n   ios::sync_with_stdio(false), cin.tie(0), cout.tie(0); n   cin >> n >> q; n   long long second = 0; n   for (long long i = 1; i <= n; i++) { n     cin >> a[i]; n     second += a[i]; n   } n   a[n + 1ll] = inf; n   a[0] = inf; n   while (q--) { n     long long x; n     cin >> x; n     if (x >= second) { n       cout << 1 <<   n ; n       continue; n     } n     cal_mx0(x); n     cal_mx1(x); n     cal_dp0(); n     cal_dp1(); n     long long ans = min(dp[0][n].first, dp[1][1].first); n     for (long long i = 1; i < n; i++) { n       long long z = 0; n       if ((dp[0][i].second + dp[1][i + 1ll].second) <= x) z = 1ll; n       ans = min(ans, dp[0][i].first + dp[1ll][i + 1ll].first - z); n     } n     cout << ans <<   n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   cin >> n; n   char a[2 * n - 1][2 * n - 1]; n   int v = 0, k = n; n   for (int i = 0; i < n; i++, k--, v++) { n     for (int j = 0; j < k; j++) printf(    ); n     for (int j = 0; j < v; j++) printf( %d  , j); n     printf( %d , v); n     for (int j = v - 1; j >= 0; j--) printf(  %d , j); n     printf(  n ); n   } n   for (int i = 0; i < n; i++) printf( %d  , i); n   printf( %d , n); n   for (int i = n - 1; i >= 0; i--) printf(  %d , i); n   for (int i = 0, k = 1, v = n - 1; i < n; i++, k++, v--) { n     printf(  n ); n     for (int j = 0; j < k; j++) printf(    ); n     for (int j = 0; j < v; j++) printf( %d  , j); n     printf( %d , v); n     for (int j = v - 1; j >= 0; j--) printf(  %d , j); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(0); n   int n, m, k, ans = 0; n   cin >> n >> m >> k; n   vector<int> a(n + 1), b(n + 1), c(k + 1); n   for (int i = 1; i <= n; i++) cin >> a[i]; n   for (int i = 1; i <= n; i++) cin >> b[i]; n   for (int i = 1; i <= k; i++) cin >> c[i]; n   for (int i = 1; i <= k; i++) { n     int curp = a[c[i]], curid = b[c[i]]; n     for (int j = 1; j <= n; j++) { n       if (b[j] == curid && curp < a[j]) { n         ans++; n         a[c[i]] = 0; n         b[c[i]] = 0; n         break; n       } n     } n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MOD = 1e9 + 7; n const int MAXN = 205; n const int infint = 1e9; n const long long inf = 1e18; n const int K = 26; n long long n, l, a[MAXN], nxt[MAXN][K], used[MAXN]; n string s[MAXN]; n struct Vertex { n   int next[K]; n   bool leaf = false; n   long long val = 0; n   int p = -1; n   char pch; n   int f = -1; n   int go[K]; n   Vertex(int p = -1, char ch =  $ ) : p(p), pch(ch) { n     memset(next, -1, sizeof next); n     memset(go, -1, sizeof go); n   } n }; n vector<Vertex> t(1); n void add_string(string const &s, int value) { n   int v = 0; n   for (int i = 0; i < s.size(); i++) { n     int c = s[i] -  a ; n     if (t[v].next[c] == -1) { n       t[v].next[c] = t.size(); n       Vertex child = Vertex(v, s[i]); n       t.push_back(child); n     } n     v = t[v].next[c]; n   } n   t[v].leaf = 1; n   t[v].val += value; n } n int go(int v, char ch); n int get_link(int v) { n   if (t[v].f == -1) { n     if (v == 0 || t[v].p == 0) n       t[v].f = 0; n     else n       t[v].f = go(get_link(t[v].p), t[v].pch); n   } n   return t[v].f; n } n int go(int v, char ch) { n   int c = ch -  a ; n   if (t[v].go[c] == -1) { n     if (t[v].next[c] != -1) n       t[v].go[c] = t[v].next[c]; n     else n       t[v].go[c] = v == 0 ? 0 : go(get_link(v), ch); n   } n   return t[v].go[c]; n } n void upd(int c) { n   if (used[c]) return; n   used[c] = 1; n   if (c == 0) return; n   upd(t[c].f); n   t[c].val += t[t[c].f].val; n } n struct matrix { n   long long a[MAXN][MAXN], n, m; n   matrix(long long Cn, long long Cm) { n     n = Cn, m = Cm; n     for (long long i = 0; i < n; i++) n       for (long long j = 0; j < m; j++) a[i][j] = -infint; n   } n   long long *operator[](const long long &i) { return a[i]; } n   matrix Identity(const long long &n) { n     matrix c(n, m); n     return c; n   } n   inline matrix operator*(matrix &b) { n     matrix c(n, b.m); n     for (int i = 0; i < n; i++) n       for (int j = 0; j < b.m; j++) n         for (int k = 0; k < m; k++) n           if (a[i][k] >= 0 && b[k][j] >= 0) n             c[i][j] = max(c[i][j], (long long)a[i][k] + (long long)b[k][j]); n     return c; n   } n   inline matrix operator+(matrix b) { n     matrix c(n, m); n     for (int i = 0; i < n; i++) n       for (int j = 0; j < m; j++) c[i][j] = (a[i][j] + b[i][j]) % MOD; n     return c; n   } n   matrix operator^(const long long &c) { n     if (c == 0) return Identity(n); n     if (c == 1) return *this; n     matrix tmp = *this ^ (c >> 1); n     tmp = tmp * tmp; n     if (c & 1) tmp = tmp * *this; n     return tmp; n   } n   void inline print() { n     for (int i = 0; i < n; i++) { n       for (int j = 0; j < m; j++) cout << a[i][j] <<    ; n       cout <<   n ; n     } n   } n }; n matrix cost(MAXN, MAXN); n matrix base(1, MAXN); n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   cin >> n >> l; n   for (int i = 0; i < n; i++) cin >> a[i]; n   for (int i = 0; i < n; i++) cin >> s[i]; n   for (int i = 0; i < n; i++) add_string(s[i], a[i]); n   for (int i = 0; i < t.size(); i++) n     for (int j = 0; j < K; j++) nxt[i][j] = go(i, (char)(j +  a )); n   for (int i = 1; i < t.size(); i++) upd(i); n   cost.n = cost.m = t.size(); n   for (int i = 0; i < t.size(); i++) n     for (int j = 0; j < t.size(); j++) cost[i][j] = -inf; n   for (int i = 0; i < t.size(); i++) n     for (int j = 0; j < K; j++) cost[i][nxt[i][j]] = t[nxt[i][j]].val; n   cost = cost ^ l; n   base.n = 1; n   base.m = t.size(); n   base[0][0] = 0; n   for (int i = 1; i < t.size(); i++) base[0][i] = -inf; n   base = base * cost; n   long long ans = 0; n   for (int i = 0; i < t.size(); i++) ans = max(ans, base[0][i]); n   cout << ans; n } n


#include <bits/stdc++.h> n using namespace std; n int low(int a) { n   if (a == 1) { n     return 0; n   } n   return 1; n } n int main() { n   int t; n   cin >> t; n   string s; n   cin >> s; n   stack<char> st; n   st.push(s[0]); n   int c = 0; n   for (int i = 1; i < t; i++) { n     if (st.top() == s[i]) { n       c++; n     } else { n       st.push(s[i]); n     } n   } n   cout << c; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 2e5; n int from[N], to[N]; n int main() { n   cin.tie(NULL); n   ios_base::sync_with_stdio(0); n   int n, m; n   cin >> n >> m; n   vector<pair<int, int> > mst; n   set<pair<int, int> > freeEdges; n   for (int i = 0; i < (int)(m); ++i) { n     int w, isMST; n     cin >> w >> isMST; n     if (isMST) { n       mst.push_back(make_pair(w, i)); n     } else { n       freeEdges.insert(make_pair(w, i)); n     } n   } n   sort(mst.begin(), mst.end()); n   for (int i = 0; i < (int)(n - 1); ++i) { n     int id = mst[i].second; n     from[id] = i; n     to[id] = i + 1; n   } n   int u = 0, v = 2; n   for (int i = 0; i < (int)(m - n + 1); ++i) { n     int cur = mst[v - 1].first; n     auto it = freeEdges.lower_bound(make_pair(cur, -1)); n     if (freeEdges.empty() || it == freeEdges.end()) { n       cout << -1; n       return 0; n     } n     int id = it->second; n     from[id] = u; n     to[id] = v; n     --u; n     if (u == -1) { n       u = v - 1; n       ++v; n     } n     freeEdges.erase(it); n   } n   for (int i = 0; i < (int)(m); ++i) { n     cout << 1 + from[i] <<     << 1 + to[i] << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int t, n, k; n   scanf( %d , &t); n   while (t-- && scanf( %d %d , &n, &k)) { n     if ((k & 1) == (n & 1) && (long long)(k)*k <= n && n >= k) n       printf( YES n ); n     else n       printf( NO n ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int dp[128][128][15]; n int board[128][128]; n int dy[] = {-1, 1}; n int N, M, K; n int solve(int x, int y, int k) { n   if (dp[x][y][k] != -2) return dp[x][y][k]; n   if (x == 0) { n     if (board[x][y] % K == k) n       return dp[x][y][k] = board[x][y]; n     else n       return dp[x][y][k] = -1; n   } n   int curr = board[x][y]; n   int ans = -1; n   int req = (K + k - (curr % K)) % K; n   for (int i = 0; i < 2; i++) { n     int nx = x - 1, ny = y + dy[i]; n     if (nx < 0 || ny < 0 || nx >= N || ny >= M) continue; n     int ret = solve(nx, ny, req); n     if (ret != -1) { n       ans = max(ans, curr + ret); n     } n   } n   return dp[x][y][k] = ans; n } n void print(int x, int y, int k) { n   int curr = board[x][y]; n   int req = (K + k - (curr % K)) % K; n   for (int i = 0; i < 2; i++) { n     int nx = x - 1, ny = y + dy[i]; n     if (nx < 0 || ny < 0 || nx >= N || ny >= M) continue; n     if (dp[x][y][k] == curr + dp[nx][ny][req]) { n       if (i == 0) n         cout <<  L ; n       else n         cout <<  R ; n       print(nx, ny, req); n       return; n     } n   } n } n int main() { n   cin >> N >> M >> K; n   K += 1; n   for (int i = 0; i < N; i++) { n     string s; n     cin >> s; n     for (int j = 0; j < M; j++) board[i][j] = s[j] -  0 ; n   } n   for (int i = 0; i < N; i++) n     for (int j = 0; j < M; j++) n       for (int k = 0; k < K; k++) dp[i][j][k] = -2; n   int ans = -1; n   int maxI = 0; n   for (int i = 0; i < M; i++) { n     int ret = solve(N - 1, i, 0); n     if (ret != -1 && ret > ans) { n       ans = ret; n       maxI = i; n     } n   } n   if (ans == -1) { n     cout << -1 << endl; n     return 0; n   } n   cout << ans << endl; n   cout << maxI + 1 << endl; n   print(N - 1, maxI, 0); n   cout << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int gr[110][110]; n int main() { n   int n, m, a[110], b[100]; n   cin >> n >> m; n   int nn = 0; n   int mm = 0; n   for (int i = 0; i < n; i++) { n     scanf( %d , &a[i]); n     nn ^= a[i]; n   } n   for (int i = 0; i < m; i++) { n     scanf( %d , &b[i]); n     mm ^= b[i]; n   } n   if (nn != mm) { n     cout <<  NO n ; n     return 0; n   } n   cout <<  YES n ; n   memset(gr, 0, sizeof gr); n   for (int i = 0; i < n; i++) { n     gr[i][m - 1] = a[i]; n   } n   for (int j = 0; j < m; j++) { n     gr[n - 1][j] = b[j]; n   } n   gr[n - 1][m - 1] = mm ^ b[m - 1] ^ a[n - 1]; n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < m; j++) { n       printf( %d  , gr[i][j]); n     } n     printf(  n ); n   } n   return 0; n } n