#include <bits/stdc++.h> n const long long Inf = 1e18; n const long double eps = 1e-7; n long long LINF = (long long)2e18; n using namespace std; n long long mod = 1e9 + 7; n long long mod5 = 1e9 + 9; n long long mod3 = 998244353; n long long mod4 = 1000003; n long long mod2 = 1e9 + 123; n const int MAXN = 10100000; n const int INF = 1000000000; n vector<vector<int> > a; n int n, m; n vector<pair<int, int> > mx; n int rec(int pos, vector<int> mxs) { n   if (pos == m) { n     int ss = 0; n     for (long long(i) = 0; (i) < n; (i)++) { n       ss += mxs[i]; n     } n     return ss; n   } n   int cur = 0; n   int ss = 0; n   for (long long(i) = 0; (i) < n; (i)++) { n     cur += mxs[i]; n     ss += mxs[i]; n   } n   int ans = cur; n   for (long long(i) = 0; (i) < n; (i)++) { n     cur = ss; n     vector<int> lol = mxs; n     for (long long(j) = 0; (j) < n; (j)++) { n       if (a[(i + j) % n][mx[pos].second] > mxs[j]) { n         cur += a[(i + j) % n][mx[pos].second] - mxs[j]; n         lol[j] = a[(i + j) % n][mx[pos].second]; n       } n     } n     ans = max(ans, rec(pos + 1, lol)); n   } n   return ans; n } n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   cout.setf(ios::fixed); n   cout.precision(20); n   int tt; n   cin >> tt; n   while (tt--) { n     cin >> n >> m; n     a.clear(); n     a.resize(n, vector<int>(m)); n     for (long long(i) = 0; (i) < n; (i)++) { n       for (long long(j) = 0; (j) < m; (j)++) { n         cin >> a[i][j]; n       } n     } n     mx.clear(); n     for (long long(i) = 0; (i) < m; (i)++) { n       int cur = -1; n       for (long long(j) = 0; (j) < n; (j)++) { n         cur = max(cur, a[j][i]); n       } n       mx.push_back({cur, i}); n     } n     sort((mx).rbegin(), (mx).rend()); n     mx.resize(min(4, m)); n     m = min(m, 4); n     vector<int> kek(n, 0); n     cout << rec(0, kek) <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long M = 1000000007; n int main() { n   int n, x, y; n   cin >> n >> x >> y; n   string s; n   cin >> s; n   int ans = 0; n   for (int i = 0; i < x; i++) { n     int pos = n - i - 1; n     char current =  0 ; n     if (i == y) { n       current =  1 ; n     } n     if (current != s[pos]) { n       ans++; n     } n   } n   cout << ans <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e6 + 10, OO = 0x3f3f3f3f; n int dx[] = {1, 0, -1, 0}; n int dy[] = {0, 1, 0, -1}; n int tc, n, m; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   string s; n   cin >> tc; n   while (tc--) { n     cin >> n >> m; n     cin >> s; n     vector<long long> freq(n + 1, 0); n     vector<int> indx(m); n     for (int i = 0; i < int(m); i++) { n       cin >> indx[i]; n       freq[0]++; n       freq[indx[i]]--; n     } n     freq[0]++; n     for (int i = int(1); i <= int(n - 1); i++) freq[i] += freq[i - 1]; n     vector<long long> karakter(26, 0); n     for (int i = 0; i < int(n); i++) karakter[s[i] -  a ] += freq[i]; n     for (int i = 0; i < int(26); i++) cout << karakter[i] <<    ; n     cout <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e5 + 10; n int a[N]; n int sum[11]; n int main() { n   int n; n   cin >> n; n   for (int i = 1; i <= n; ++i) { n     scanf( %d , &a[i]); n     sum[a[i]]++; n   } n   while (1) { n     int i, aa[11]; n     memset(aa, 0, sizeof(aa)); n     for (i = 1; i < 11; ++i) aa[i] = sum[i]; n     sort(aa, aa + 11); n     for (i = 0; i < 11; ++i) n       if (aa[i]) break; n     if (aa[10] - aa[i] > 1) { n       if (aa[i] == 1) { n         if (aa[i + 1] == aa[10]) { n           cout << n; n           break; n         } n       } n     } else if (aa[i] == aa[10]) { n       if (aa[i] == 1 || i == 10) { n         cout << n; n         break; n       } n     } else { n       if (aa[i] == 1) { n         if (aa[i + 1] == aa[10]) { n           cout << n; n           break; n         } n       } n       if (aa[i] == aa[9]) { n         cout << n; n         break; n       } n     } n     sum[a[n]]--; n     n--; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <class T> n bool umin(T& a, T b) { n   if (a > b) { n     a = b; n     return 1; n   } n   return 0; n } n template <class T> n bool umax(T& a, T b) { n   if (a < b) { n     a = b; n     return 1; n   } n   return 0; n } n long long a, b, c, type, ric[300007], par[300007], cost[300007], d[300007], n, n     m, lvl[300007], P[300007][20 * 2]; n vector<pair<long long, long long> > E[300007]; n pair<pair<long long, long long>, long long> way[300007]; n long long atabul(long long x) { n   if (ric[x] == x) return x; n   return ric[x] = atabul(ric[x]); n } n void dfs(long long x, long long y, long long z) { n   lvl[x] = lvl[y] + 1; n   P[x][0] = y; n   cost[x] = z; n   if (z == 1) { n     ric[x] = y; n     atabul(x); n   } n   for (int i = 0; i < int(E[x].size()); i++) n     if (E[x][i].first != y) dfs(E[x][i].first, x, E[x][i].second); n } n void build_table() { n   for (int j = 1; j <= 20; j++) n     for (int i = 1; i <= n; i++) n       if (P[i][j - 1] > 0) P[i][j] = P[P[i][j - 1]][j - 1]; n } n void form_LCA() { n   for (int i = 0; i <= n; i++) n     for (int j = 0; j <= 20; j++) P[i][j] = -1; n   dfs(1, 0, 0); n   build_table(); n } n long long LCA(long long x, long long y) { n   if (lvl[x] < lvl[y]) swap(x, y); n   for (int i = 20; i >= 0; i--) n     if (lvl[x] - (1 << i) >= lvl[y]) x = P[x][i]; n   if (x == y) return x; n   for (int i = 20; i >= 0; i--) n     if (P[x][i] > 0 && P[x][i] != P[y][i]) x = P[x][i], y = P[y][i]; n   return P[x][0]; n } n int main() { n   scanf( %lld%lld , &n, &m); n   ric[n] = n; n   for (int i = 1; i < n; i++) { n     scanf( %lld%lld%lld , &a, &b, &c); n     E[a].push_back(make_pair(b, c)); n     E[b].push_back(make_pair(a, c)); n     ric[i] = i; n     way[i] = {{a, b}, c}; n   } n   form_LCA(); n   while (m--) { n     scanf( %lld , &type); n     if (type == 1) { n       scanf( %lld%lld%lld , &a, &b, &c); n       long long tr = LCA(a, b); n       while (atabul(a) != atabul(tr) && c > 0) { n         if (cost[a] == 1) { n           a = atabul(a); n           continue; n         } n         c /= cost[a]; n         a = P[a][0]; n       } n       while (atabul(b) != atabul(tr) && c > 0) { n         if (cost[b] == 1) { n           b = atabul(b); n           continue; n         } n         c /= cost[b]; n         b = P[b][0]; n       } n       printf( %lld n , c); n     } else { n       scanf( %lld%lld , &a, &b); n       long long qm = way[a].first.first, gr = way[a].first.second; n       if (lvl[qm] < lvl[gr]) swap(qm, gr); n       cost[qm] = b; n       if (b == 1) { n         ric[qm] = gr; n         atabul(qm); n       } n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n inline int in() { n   int32_t x; n   scanf( %d , &x); n   return x; n } n inline string get() { n   char ch[1000010]; n   scanf( %s , ch); n   return ch; n } n const int MAX_LG = 60; n const long long maxn = 1e6 + 100; n const long long base = 29; n const long long mod = 1e9 + 7; n const long long INF = 1e18 + 100; n struct state { n   long long a = 0, b = 0, tp = 0; n   state() {} n   state(long long a, long long b, long long tp) : a(a), b(b), tp(tp) {} n }; n long long cnt[maxn]; n long long di[51][51][2]; n long long C[51][51]; n long long ways[51][51][2]; n queue<state> q; n int32_t main() { n   long long n = in(), k = in(); n   for (long long i = 0; i < n; i++) cnt[in() / 51]++; n   q.push({cnt[0], cnt[1], 0}); n   for (long long i = 0; i <= 50; i++) n     for (long long j = 0; j <= 50; j++) di[i][j][0] = di[i][j][1] = INF; n   for (long long i = 0; i <= 50; i++) { n     for (long long j = 0; j <= i; j++) { n       if (j == 0) n         C[i][j] = 1; n       else n         C[i][j] = (C[i - 1][j] + C[i - 1][j - 1]) % mod; n     } n   } n   di[cnt[0]][cnt[1]][0] = 0; n   ways[cnt[0]][cnt[1]][0] = 1; n   while (q.empty() == false) { n     state now = q.front(); n     q.pop(); n     long long a2 = cnt[0] - now.a; n     long long b2 = cnt[1] - now.b; n     for (long long i = 0; i <= now.a; i++) { n       for (long long j = 0; j <= now.b; j++) { n         if (i + j == 0) continue; n         if (i * 50 + j * 100 > k) continue; n         long long d2 = di[now.a][now.b][now.tp] + 1; n         long long *d3 = &di[a2 + i][b2 + j][now.tp ^ 1]; n         if (*d3 == INF) { n           *d3 = d2; n           q.push({a2 + i, b2 + j, now.tp ^ 1}); n         } n         if (*d3 == d2) { n           ways[a2 + i][b2 + j][now.tp ^ 1] += ways[now.a][now.b][now.tp] * n                                               C[now.a][i] % mod * C[now.b][j] % n                                               mod; n           if (ways[a2 + i][b2 + j][now.tp ^ 1] > mod) n             ways[a2 + i][b2 + j][now.tp ^ 1] -= mod; n         } n       } n     } n   } n   if (ways[cnt[0]][cnt[1]][1] == 0) n     return cout << -1 <<   n  << 0 <<   n , 0; n   else { n     cout << di[cnt[0]][cnt[1]][1] <<   n ; n     cout << ways[cnt[0]][cnt[1]][1] <<   n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n long long modulo(long long base, long long exponent, long long modulus); n long long choose(long long n, long long k); n long long inverse(long long a, long long m); n void build(); n void fileio(); n long long ncr(long long n, long long r); n const int nax = 1e6 + 10; n const int LG = log2(nax) + 1; n vector<long long> fact(nax); n long long dp[1LL << 20]; n int32_t main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   long long n, m; n   cin >> n >> m; n   string s; n   cin >> s; n   long long freq[m][m]; n   memset(freq, 0, sizeof(freq)); n   for (int i = 1; i < n; i++) { n     freq[s[i] -  a ][s[i - 1] -  a ]++; n     freq[s[i - 1] -  a ][s[i] -  a ]++; n   } n   for (int i = 0; i < (1LL << m); i++) dp[i] = 1000000000000000; n   for (int mask = 0; mask < (1LL << m); mask++) { n     long long cost = 0; n     for (int i = 0; i < m; i++) { n       for (int j = i + 1; j < m; j++) { n         if ((mask >> i & 1) ^ (mask >> j & 1)) { n           cost += freq[i][j]; n         } n       } n     } n     for (int i = 0; i < m; i++) { n       if ((mask & (1LL << i)) == 0) continue; n       dp[mask] = min(dp[mask], dp[mask ^ (1LL << i)] + cost); n       if ((mask ^ (1LL << i)) == 0) dp[mask] = cost; n     } n   } n   long long ans = dp[(1LL << m) - 1]; n   cout << ans <<   n ; n   return 0; n } n long long ncr(long long n, long long r) { n   if (r > n || r < 0 || n < 0) return 0; n   long long ans = fact[n]; n   long long temp = (fact[n - r] * fact[r]) % 1000000007; n   ans = (ans * inverse(temp, 1000000007)) % 1000000007; n   return ans; n } n void fileio() { n   freopen( input.txt ,  r , stdin); n   freopen( output.txt ,  w , stdout); n } n void build() { n   fact[0] = 1; n   for (long long i = 1; i < nax; i++) fact[i] = (fact[i - 1] * i) % 1000000007; n } n long long modulo(long long base, long long exponent, long long modulus) { n   if (modulus == 1) return 0; n   long long result = 1; n   base = base % modulus; n   while (exponent > 0) { n     if (exponent % 2 == 1) { n       result = (result * base) % modulus; n     } n     exponent = exponent >> 1; n     base = (base * base) % modulus; n   } n   return result; n } n long long choose(long long n, long long k) { n   if (k == 0) return 1; n   return (n * choose(n - 1, k - 1)) / k; n } n void EE(long long a, long long b, long long &co1, long long &co2) { n   if (a % b == 0) { n     co1 = 0; n     co2 = 1; n     return; n   } n   EE(b, a % b, co1, co2); n   long long temp = co1; n   co1 = co2; n   co2 = temp - co2 * (a / b); n } n long long inverse(long long a, long long m) { n   long long x, y; n   EE(a, m, x, y); n   if (x < 0) x += m; n   return x; n } n


#include <bits/stdc++.h> n using namespace std; n vector<int> g[100500]; n int c[100500]; n int n; n bool vis[100500]; n bool dfs(int u, int cu) { n   if (vis[u]) return 0; n   vis[u] = 1; n   bool r = 0; n   if (c[u] != cu) return 1; n   for (int v : g[u]) r |= dfs(v, cu); n   return r; n } n int main() { n   scanf( %d , &n); n   for (int i = 0, qwe = n - 1; i < qwe; ++i) { n     int u, v; n     scanf( %d%d , &u, &v); n     u--; n     v--; n     g[u].push_back(v); n     g[v].push_back(u); n   } n   for (int i = 0, qwe = n; i < qwe; ++i) scanf( %d , c + i); n   int u = -1, sz = 0; n   for (int i = 0, qwe = n; i < qwe; ++i) { n     set<int> s; n     for (int v : g[i]) n       if (c[i] != c[v]) s.insert(v); n     if (s.size() > sz) u = i, sz = s.size(); n   } n   if (u == -1) { n     puts( YES n1 ); n     return 0; n   } n   vis[u] = 1; n   for (int i = 0, qwe = g[u].size(); i < qwe; ++i) n     if (dfs(g[u][i], c[g[u][i]])) { n       puts( NO ); n       return 0; n     } n   printf( YES n%d n , u + 1); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m; n int a[105][105]; n int main() { n   scanf( %d %d , &n, &m); n   if (n + m == 3 or n + m == 4) n     printf( -1 n ); n   else { n     int num = 2; n     if (n >= m) { n       for (int i = 1; i <= n; i++) n         for (int j = 1; j <= m; j++) { n           if (num > m * n) num = 1; n           a[i][j] = num; n           num += 2; n         } n     } else n       for (int j = 1; j <= m; j++) n         for (int i = 1; i <= n; i++) { n           if (num > m * n) num = 1; n           a[i][j] = num; n           num += 2; n         } n     for (int i = 1; i <= n; i++) n       for (int j = 1; j <= m; j++) { n         printf( %d , a[i][j]); n         if (j == m) n           printf(  n ); n         else n           printf(   ); n       } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const inline int __GET_INT() { n   int ret; n   scanf( %d , &ret); n   return ret; n } n const int maxn = 2002; n int n, m; n char grid[maxn][maxn]; n int dx[] = {-1, -1, -1, 0, 0, 1, 1, 1}; n int dy[] = {-1, 0, 1, -1, 1, -1, 0, 1}; n int cntStar(int r, int c) { n   return (grid[r][c] ==  * ) + (grid[r + 1][c] ==  * ) + n          (grid[r][c + 1] ==  * ) + (grid[r + 1][c + 1] ==  * ); n } n void dfs(int r, int c) { n   if (r < 0 || c < 0 || (r + 1) >= n || (c + 1) >= m) { n     return; n   } n   if (cntStar(r, c) != 1) { n     return; n   } n   grid[r][c] = grid[r + 1][c] = grid[r][c + 1] = grid[r + 1][c + 1] =  . ; n   for (int i = 0; i < 2; ++i) n     for (int j = 0; j < 2; ++j) { n       for (int k = 0; k < 8; ++k) { n         dfs(r + i + dx[k], c + j + dy[k]); n       } n     } n } n int main() { n   n = __GET_INT(); n   m = __GET_INT(); n   for (int i = 0; i < n; ++i) { n     scanf( %s , grid[i]); n   } n   for (int i = 0; (i + 1) < n; ++i) n     for (int j = 0; (j + 1) < m; ++j) { n       dfs(i, j); n     } n   for (int i = 0; i < n; ++i) { n     for (int j = 0; j < m; ++j) printf( %c , grid[i][j]); n     puts(  ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long a[200005]; n int b[200005]; n int n, k, Q; n long long ans; n int l, r; n int main() { n   scanf( %d , &Q); n   while (Q--) { n     scanf( %d%d , &n, &k); n     for (int i = 1; i <= n; i++) scanf( %lld , &a[i]); n     for (int i = 1; i <= k; i++) scanf( %d , &b[i]); n     sort(a + 1, a + 1 + n); n     sort(b + 1, b + 1 + k); n     int l, r; n     ans = 0; n     l = 1; n     r = n; n     for (int i = 1; i <= k; i++) { n       if (b[i] == 1) n         ans += a[r] + a[r]; n       else n         break; n       r--; n     } n     for (int i = k; i >= 1; i--) { n       if (b[i] == 1) break; n       ans += a[l] + a[r]; n       r--; n       l += b[i] - 1; n     } n     printf( %lld n , ans); n   } n } n


#include <bits/stdc++.h> n using namespace std; n long long max(long long a, long long b) { n   if (a > b) return a; n   return b; n } n long long min(long long a, long long b) { n   if (a < b) return a; n   return b; n } n const int MOD = 1e9 + 7, N = 102, L = 3005, INF = INT_MAX; n pair<long long, long long> a[N]; n long long dp[L][N][2]; n int main() { n   int n, l; n   cin >> n >> l; n   for (int i = 0; i < n; i++) { n     cin >> a[i].first >> a[i].second; n   } n   for (int i = 1; i <= l; i++) { n     for (int j = 0; j < n; j++) { n       if (a[j].first == i) n         dp[i][j][0] = 1; n       else if (a[j].first < i) { n         for (int c = 0; c < n; c++) { n           if (c == j) continue; n           if (a[c].second == a[j].first) n             dp[i][j][0] += dp[i - a[j].first][c][0]; n           if (a[c].first == a[j].first && a[c].first != a[c].second) n             dp[i][j][0] += dp[i - a[j].first][c][1]; n           dp[i][j][0] %= MOD; n         } n       } n       if (a[j].second == i) n         dp[i][j][1] = 1; n       else if (a[j].second < i) { n         for (int c = 0; c < n; c++) { n           if (c == j) continue; n           if (a[c].second == a[j].second) n             dp[i][j][1] += dp[i - a[j].second][c][0]; n           if (a[c].first == a[j].second && a[c].first != a[c].second) n             dp[i][j][1] += dp[i - a[j].second][c][1]; n           dp[i][j][1] %= MOD; n         } n       } n     } n   } n   long long ans = 0; n   for (int i = 0; i < n; i++) { n     ans += dp[l][i][0]; n     if (a[i].first != a[i].second) ans += dp[l][i][1]; n     ans %= MOD; n   } n   cout << ans <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int M = 11, N = 2e5 + 5; n int n, k; n int a[N]; n map<int, int> rem[M]; n long long solve() { n   long long ans = 0; n   for (int i = 0; i < n; ++i) { n     unsigned long long f = 10; n     int ln = ((int)(to_string(a[i])).size()); n     for (int l = 1; l < M; ++l) { n       int r = (k - (int)((f * a[i]) % k)) % k; n       ans += rem[l][r]; n       if (l == ln && r == a[i] % k) ans--; n       f *= 10; n     } n   } n   return ans; n } n int main() { n   ios::sync_with_stdio(0); n   cin.tie(nullptr); n   cout.tie(nullptr); n   cin >> n >> k; n   for (int i = 0; i < n; ++i) { n     cin >> a[i]; n     int l = ((int)(to_string(a[i])).size()); n     rem[l][a[i] % k]++; n   } n   cout << solve() <<   n ; n   return 0; n } n


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


#include <bits/stdc++.h> n using namespace std; n const int maxn = 5e5 + 5; n int main() { n   long long v, w1, w2, v1, v2; n   cin >> v >> w1 >> w2 >> v1 >> v2; n   long long x = max(v1, v2); n   if (x * x >= v) { n     if (v1 < v2) swap(v1, v2), swap(w1, w2); n     long long ans = 0; n     for (int i = 0; i <= v / v1; i++) { n       ans = max(ans, i * w1 + (v - i * v1) / v2 * w2); n     } n     cout << ans << endl; n   } else { n     if (1.0 * w1 / v1 > 1.0 * w2 / v2) { n       swap(v1, v2), swap(w1, w2); n     } n     long long ans = 0; n     for (int i = 0; i < v2; i++) { n       ans = max(ans, i * w1 + (v - i * v1) / v2 * w2); n     } n     cout << ans << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n const int mod = 1000000007; n const int inf = 1000000009; n const long long INF = 1000000000000000009; n const long long big = 1000000000000000; n const long double eps = 0.000000000000000000001; n using namespace std; n const int M = 100000; n int W[M + 5], U[M + 5], cnt[M + 5]; n vector<int> D[M + 5], S; n long long int funkcja(int x) { n   long long int wynik = 0; n   for (auto v : D[x]) wynik += cnt[v] * U[v]; n   return wynik; n } n void aktualizuj(int x, int c) { n   for (auto v : D[x]) cnt[v] += c; n } n int main() { n   ios::sync_with_stdio(false); n   cin.tie(); n   cout.tie(); n   int n; n   cin >> n; n   for (int i = 1; i <= M; i++) { n     for (int j = i; j <= M; j += i) D[j].push_back(i); n     if (i == 1) n       U[i] = 1; n     else if ((i / D[i][1]) % D[i][1] == 0) n       U[i] = 0; n     else n       U[i] = -U[i / D[i][1]]; n   } n   long long int wynik = 0; n   for (int i = 1; i <= n; i++) { n     int a; n     cin >> a; n     wynik = max(wynik, (long long int)a); n     for (auto v : D[a]) W[v] = 1; n   } n   for (int j = M; j >= 1; j--) { n     if (!W[j]) continue; n     while (funkcja(j)) { n       wynik = max(wynik, (long long int)j * S.back()); n       aktualizuj(S.back(), -1); n       S.pop_back(); n     } n     aktualizuj(j, 1); n     S.push_back(j); n   } n   cout << wynik; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int a, b, sum, x; n int main() { n   cin >> a >> b; n   while (a) { n     sum += a; n     x += a % b; n     a /= b; n     a += x / b; n     x %= b; n   } n   cout << sum; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long n, arr[500010], temp, j, i, ans; n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   cin >> n; n   for (long long i = 0; i < n; i++) { n     cin >> arr[i]; n   }; n   sort(arr, arr + n); n   i = 0, j = n / 2; n   ans = n; n   while (i < n / 2) { n     while (j < n) { n       if (arr[i] * 2 <= arr[j]) { n         ans--; n         j++; n         break; n       } else n         j++; n     } n     i++; n   } n   cout << ans <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n int l[210000], r[210000]; n vector<pair<int, int> > v; n int n, m, k; n int mp[1000]; n int lbit[1000]; n int dp[210000][300]; n int msk[210000]; n int main() { n   for (int i = 0; i < 210000; i++) n     for (int j = 0; j < 300; j++) dp[i][j] = -0x3f3f3f3f; n   dp[0][0] = 0; n   for (int i = 0; i < (1 << 9); i++) { n     int cnt = 0; n     for (int j = 0; j < 8; j++) n       if (i & (1 << j)) { n         mp[i]++; n       } else if (lbit[i] == 0) { n         lbit[i] = (1 << j); n       } n   } n   cin >> n >> m >> k; n   for (int i = 1; i <= n; i++) { n     scanf( %d%d , &l[i], &r[i]); n     v.push_back({l[i] - 1, i}); n     v.push_back({r[i], -i}); n   } n   sort(v.begin(), v.end()); n   int tim = 0; n   int posmsk = 0; n   int ans = 0; n   int lst = 0; n   for (auto p : v) { n     tim++; n     if (p.second < 0) { n       for (int j = 0; j < (1 << 8); j++) n         if (j & msk[-p.second]) n           dp[tim][j ^ msk[-p.second]] = n               max(dp[tim][j ^ msk[-p.second]], n                   dp[tim - 1][j] + max(0, p.first - lst) * (mp[j] & 1)); n         else { n           dp[tim][j] = n               max(dp[tim][j], dp[tim - 1][j] + (p.first - lst) * (mp[j] & 1)); n         } n       posmsk ^= msk[-p.second]; n     } else { n       msk[p.second] = lbit[posmsk]; n       posmsk ^= msk[p.second]; n       for (int j = 0; j < (1 << 8); j++) n         if ((j & msk[p.second]) == 0) { n           dp[tim][j ^ msk[p.second]] = n               max(dp[tim][j ^ msk[p.second]], n                   dp[tim - 1][j] + (max(0, p.first - lst)) * (mp[j] & 1)); n           dp[tim][j] = n               max(dp[tim][j], dp[tim - 1][j] + (p.first - lst) * (mp[j] & 1)); n         } n     } n     lst = p.first; n   } n   cout << dp[2 * n][0]; n   return ~~(0 ^ 0 ^ 0); n } n


#include <bits/stdc++.h> n using namespace std; n const int mod = 998244353; n const int N = 3005; n int n, c, a[N], dp[N][N], g[N][N], freq[N], mul[N], imul[N], pw[N], cnt[N]; n int dp2[2][N][1 << 12]; n int inverse(int n) { n   if (n == 1) return 1; n   return inverse(mod % n) * 1LL * (mod - mod / n) % mod; n } n inline void add(int &x, int y) { n   x += y; n   if (x >= mod) x -= mod; n } n void solve1() { n   for (int i = 1; i <= n; i++) { n     a[i]--; n   } n   memset(dp2, 0, sizeof(dp2)); n   int flg = 0; n   dp2[0][0][0] = 1; n   for (int i = 1; i <= n; i++) { n     for (int p = 0; p * c <= i; p++) { n       for (int mask = 0; mask < (1 << c); mask++) { n         dp2[flg ^ 1][p][mask] = 0; n       } n     } n     for (int p = 0; p * c <= i; p++) { n       for (int mask = 0; mask + 1 < (1 << c); mask++) { n         int nmask = mask | (1 << a[i]); n         if (nmask + 1 == (1 << c)) { n           add(dp2[flg ^ 1][p + 1][0], dp2[flg][p][mask]); n         } else { n           add(dp2[flg ^ 1][p][nmask], dp2[flg][p][mask]); n         } n         add(dp2[flg ^ 1][p][mask], dp2[flg][p][mask]); n       } n     } n     flg ^= 1; n   } n   memset(cnt, 0, sizeof(cnt)); n   for (int p = 0; p <= n; p++) { n     for (int mask = 0; mask + 1 < (1 << c); mask++) { n       add(cnt[p], dp2[flg][p][mask]); n     } n   } n   add(cnt[0], mod - 1); n   for (int i = 0; i <= n; i++) { n     cout << cnt[i] <<    ; n   } n   cout <<   n ; n } n void solve2() { n   mul[0] = 0; n   pw[0] = 1; n   for (int i = 1; i <= n; i++) { n     mul[i] = mul[i - 1] + mul[i - 1] + 1; n     if (mul[i] >= mod) mul[i] -= mod; n     imul[i] = inverse(mul[i]); n     pw[i] = mul[i] + 1; n   } n   memset(g, 0, sizeof(g)); n   for (int i = 1; i <= n; i++) { n     int cnt = 0; n     long long prd = 1; n     memset(freq, 0, sizeof(freq)); n     for (int j = i; j <= n; j++) { n       if (freq[a[j]] == 0) { n         cnt++; n       } else { n         (prd *= imul[freq[a[j]]]) %= mod; n       } n       freq[a[j]]++; n       (prd *= mul[freq[a[j]]]) %= mod; n       if (cnt == c) { n         g[i][j] = prd * imul[freq[a[j]]] % mod; n       } n     } n   } n   memset(dp, 0, sizeof(dp)); n   for (int i = 1; i <= n; i++) { n     dp[i][1] = g[1][i]; n     for (int j = 1; j <= i; j++) { n       for (int p = 2; p * c <= i; p++) { n         dp[i][p] += dp[j - 1][p - 1] * 1LL * g[j][i] % mod; n         if (dp[i][p] >= mod) dp[i][p] -= mod; n       } n     } n   } n   memset(cnt, 0, sizeof(cnt)); n   for (int i = 1; i <= n; i++) { n     for (int p = 1; p * c <= i; p++) { n       cnt[p] += dp[i][p] * 1LL * pw[n - i] % mod; n       if (cnt[p] >= mod) cnt[p] -= mod; n     } n   } n   cnt[0] = mul[n] + mod - cnt[1]; n   if (cnt[0] >= mod) cnt[0] -= mod; n   for (int p = 1; p <= n; p++) { n     cnt[p] += mod - cnt[p + 1]; n     if (cnt[p] >= mod) cnt[p] -= mod; n   } n   for (int i = 0; i <= n; i++) { n     cout << cnt[i] <<    ; n   } n   cout <<   n ; n } n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   cin >> n >> c; n   for (int i = 1; i <= n; i++) { n     cin >> a[i]; n   } n   if (c <= 10) { n     solve1(); n   } else { n     solve2(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int ans[101000], mark[101000]; n char s[101000]; n int n, top, now, n_; n int main() { n   do s[++n_] = getchar(); n   while (s[n_] !=  = ); n   scanf( %d , &n); n   mark[1] = 0; n   top = 1; n   for (int i = 3; i < n_; i += 4) mark[++top] = (s[i] ==  - ); n   now = 0; n   for (int i = 1; i <= top; i++) ans[i] = 1; n   for (int i = 1; i <= top; i++) n     if (mark[i]) n       now--; n     else n       now++; n   while (now != n) { n     if (now < n) { n       int q = 0; n       for (int i = 1; i <= top; i++) n         if (!mark[i] && ans[i] < n) { n           q = i; n           break; n         } n       if (!q) { n         printf( Impossible ); n         return 0; n       } n       int inc = min(n - ans[q], n - now); n       ans[q] += inc; n       now += inc; n     } else { n       int q = 0; n       for (int i = 1; i <= top; i++) n         if (mark[i] && ans[i] < n) { n           q = i; n           break; n         } n       if (!q) { n         printf( Impossible ); n         return 0; n       } n       int inc = min(n - ans[q], now - n); n       ans[q] += inc; n       now -= inc; n     } n   } n   printf( Possible n ); n   printf( %d , ans[1]); n   for (int i = 2; i <= top; i++) { n     putchar(   ); n     if (mark[i]) n       putchar( - ); n     else n       putchar( + ); n     putchar(   ); n     printf( %d , ans[i]); n   } n   printf(  = %d , n); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int mod = 1e9 + 7; n const int maxn = 4e5 + 5; n long long a[maxn], f[maxn], d[maxn]; n int q[maxn]; n int main() { n   int n, i, j, l, r; n   long long c; n   scanf( %d%lld , &n, &c); n   for (i = 1; i < (n << 1); i++) { n     scanf( %lld , &a[i]); n     a[i] += a[i - 1]; n   } n   l = r = 1; n   j = q[1] = 0; n   for (i = 1; i <= n; i++) { n     while (j < i && a[(i << 1) - 1] - a[j << 1] > c) j++; n     while (l <= r && q[l] < j) l++; n     if (j) f[i] = f[j - 1] + c; n     if (l <= r) f[i] = max(f[i], d[q[l]] + a[(i << 1) - 1]); n     d[i] = f[i] - a[i << 1]; n     while (l <= r && d[i] > d[q[r]]) r--; n     q[++r] = i; n   } n   printf( %lld n , f[n]); n   return 0; n } n


#include <bits/stdc++.h> n template <typename T> n std::istream& operator>>(std::istream& input, std::vector<T>& v) { n   for (T& a : v) input >> a; n   return input; n } n void answer(bool v) { n   constexpr const char* s[2] = { NO ,  YES }; n   std::cout << s[v] <<   n ; n } n void solve(const std::vector<unsigned>& a) { n   const size_t n = a.size(); n   for (size_t i = 1; i < n; ++i) { n     if (a[i] >= a[i - 1]) return answer(true); n   } n   answer(false); n } n void test_case() { n   size_t n; n   std::cin >> n; n   std::vector<unsigned> a(n); n   std::cin >> a; n   solve(a); n } n int main() { n   size_t t; n   std::cin >> t; n   while (t-- > 0) test_case(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void __print(int x) { cerr << x; }; n void __print(long x) { cerr << x; }; n void __print(long long x) { cerr << x; }; n void __print(unsigned x) { cerr << x; }; n void __print(unsigned long x) { cerr << x; }; n void __print(unsigned long long x) { cerr << x; }; n void __print(float x) { cerr << x; }; n void __print(double x) { cerr << x; }; n void __print(long double x) { cerr << x; }; n void __print(char x) { cerr <<      << x <<     ; }; n void __print(const char *x) { cerr <<     << x <<    ; }; n void __print(const string &x) { cerr <<     << x <<    ; }; n void __print(bool x) { cerr << (x ?  true  :  false ); } n long long powmod(long long a, long long b) { n   long long res = 1; n   a %= 1000000007; n   assert(b >= 0); n   for (; b; b >>= 1) { n     if (b & 1) res = res * a % 1000000007; n     a = a * a % 1000000007; n   } n   return res; n } n long long string_to_int(string s) { n   long long ans = 0; n   long long p = 1; n   for (long long i = s.size() - 1; i >= 0; i--) { n     ans += (s[i] -  0 ) * p; n     p *= 10; n   } n   return ans; n } n long long fast_pow(long long a, long long b) { n   if (b == 1) return a; n   if (b == 0) return 1; n   long long x = fast_pow(a, b / 2); n   x *= x; n   if (b & 1) x *= a; n   return x; n } n template <typename T, typename V> n void __print(const pair<T, V> &x) { n   cerr <<  { ; n   __print(x.first); n   cerr <<  , ; n   __print(x.second); n   cerr <<  } ; n } n template <typename T> n void __print(const T &x) { n   long long f = 0; n   cerr <<  { ; n   for (auto &i : x) cerr << (f++ ?  ,  :   ), __print(i); n   cerr <<  } ; n } n void _print() { cerr <<  ] n ; } n template <typename T, typename... V> n void _print(T t, V... v) { n   __print(t); n   if (sizeof...(v)) cerr <<  ,  ; n   _print(v...); n } n void t_c_c() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   long long n, m, k; n   cin >> n >> m >> k; n   long long arr[m]; n   for (long long i = 0; i < m; i++) { n     cin >> arr[i]; n   } n   long long days = 0; n   long long pages = k; n   long long point = 0; n   long long x = 0; n   long long xx = 0; n   for (long long i = 0; i < m;) { n     if (arr[i] - x <= pages) { n       while (arr[i] - xx <= pages) { n         ; n         x++; n         i++; n       } n       xx = x; n       days++; n     } else { n       long long factor = ((arr[i] - x) - pages) / k; n       pages += n           (((arr[i] - x) - pages) % k == 0 ? factor * k : (factor + 1) * k); n     } n   } n   cout << days <<   n ; n } n int32_t main() { n   t_c_c(); n   return 0; n } n


/** n  *    author:  Ujjwal Tripathi n  *    created: 15.04.2021 18:04:04 n **/ n   n      n #include<bits/stdc++.h> n using namespace std; n   n #define ar array<ll, 2> n #define ll long long n #define in insert n #define pb push_back n #define pii pair<ll,ll> n #define vt vector n #define P_Q(x) priority_queue<x> n #define p_q(x) priority_queue<x, vector<x>, greater<x>> n #define For(i, n) for(ll i = 0; i < n; i++) n #define Rev(i, n) for(ll i = n-1; i >= 0; i--) n #define FOR(i, n) for(ll i = 1; i <= n; i++) n #define REV(i, n) for(ll i = n; i >= 1; i--) n #define Rep(i,a,b) for(ll i = a; i <= b; i++) n #define all(x) (x).begin(),(x).end() n #define rall(x) (x).rbegin(), (x).rend() n #define sz(x) (int) (x).size() n   n // Maximum priority queue n template <typename T> n using mxpq = priority_queue<T>; n   n // Minimum priority queue n template <typename T> n using mnpq = priority_queue<T, vt<T>, greater<T>>; n   n   n // x | (x + 1) sets lowest unset bit of x n // x & (x - 1) unsets lowest set bit of x n   n const ll mod = 1e9L + 7; n const ll INF = 1e18L + 5; n const ll mxN = 2e5 + 1; n   n   n ll n; n ll dp[1000004]; n // remember to read no of test_cases n void solve() n { n    cin>>n; n    vt<ll> v(n); n    For(i,n) n    { n     cin>>v[i]; n     if(v[i]%2==0) n     { n         cout<<v[i]<<   ; n     } n    } n   n     For(i,n) n     { n       if(v[i]%2==1) n       { n         cout<<v[i]<<   ; n       } n     } n     cout<<endl; n } n   n int main() n { n   ios::sync_with_stdio(0);  cin.tie(0); n   ll t = 1; n   cin >> t; n   FOR(tt, t) solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int a[200010], b[200010], n, cnt = 0; n int main() { n   ios::sync_with_stdio(0); n   cin >> n; n   for (int i = 2; i <= n; i++) cin >> a[i]; n   for (int tmp = n; tmp; tmp = a[tmp]) b[++cnt] = tmp; n   reverse(b + 1, b + cnt + 1); n   for (int i = 1; i <= cnt; i++) cout << b[i] <<    ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   cin.tie(0); n   cin.sync_with_stdio(0); n   int t; n   long long n; n   cin >> t; n   for (int i = 0; i < t; i++) { n     cin >> n; n     cout << (-1 * n) + 1 <<     << n <<   n ; n   } n } n bool sortbysec(const pair<int, int> &a, const pair<int, int> &b) { n   return (a.second < b.second); n } n bool sort_descending(int a, int b) { return (a > b); } n


#include <bits/stdc++.h> n using namespace std; n const int seed = 131; n const long long LLMAX = 2e18; n const int MOD = 1e9 + 7; n const double eps = 1e-8; n const int MAXN = 1e5 + 10; n const int hmod1 = 0x48E2DCE7; n const int hmod2 = 0x60000005; n inline long long sqr(long long x) { return x * x; } n inline int sqr(int x) { return x * x; } n inline double sqr(double x) { return x * x; } n long long gcd(long long a, long long b) { return b == 0 ? a : gcd(b, a % b); } n long long exgcd(long long a, long long b, long long &x, long long &y) { n   long long d; n   (b == 0 ? (x = 1, y = 0, d = a) n           : (d = exgcd(b, a % b, y, x), y -= a / b * x)); n   return d; n } n long long qpow(long long a, long long n) { n   long long sum = 1; n   while (n) { n     if (n & 1) sum = sum * a % MOD; n     a = a * a % MOD; n     n >>= 1; n   } n   return sum; n } n inline int dcmp(double x) { n   if (fabs(x) < eps) return 0; n   return (x > 0 ? 1 : -1); n } n long long need[MAXN], tim[MAXN], v[MAXN][2], cnt = 0; n struct node { n   long long id, a, b; n   node() {} n   node(long long x, long long y, long long z) { id = x, a = y, b = z; } n   bool operator<(const node &n) const { n     if (a - b == n.a - n.b) return a > n.a; n     return a - b > n.a - n.b; n   } n } in[MAXN]; n long long check(long long n, long long a, long long b) { n   long long sum = 0; n   for (int i = 0; i <= n; i++) tim[i] = need[i]; n   for (int i = 0; i < cnt; i++) { n     if (tim[in[i].id]) { n       long long minn = min(tim[in[i].id], a); n       sum += in[i].a * minn; n       tim[in[i].id] -= minn; n       a -= minn; n     } n     if (tim[in[i].id]) { n       long long minn = min(tim[in[i].id], b); n       sum += in[i].b * minn; n       tim[in[i].id] -= minn; n       b -= minn; n     } n   } n   return sum; n } n int main(void) { n   ios::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   long long n, s, sum = 0, ans = 0; n   cin >> n >> s; n   for (int i = 0; i < n; i++) { n     cin >> need[i] >> v[i][0] >> v[i][1]; n     sum += need[i]; n     in[cnt++] = node(i, v[i][0], v[i][1]); n   } n   long long all = (sum + s - 1) / s, l = 0, r = all; n   need[n] = all * s - sum; n   in[cnt++] = node(n, 0, 0); n   sort(in, in + cnt); n   while (l + 3 < r) { n     long long m1 = (l + r) / 2, m2 = (m1 + r) / 2; n     if (check(n, m1 * s, (all - m1) * s) > check(n, m2 * s, (all - m2) * s)) n       r = m2; n     else n       l = m1; n   } n   for (long long i = l; i <= r; i++) n     ans = max(ans, check(n, i * s, (all - i) * s)); n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long poww(long long a, long long b, long long md) { n   return (!b ? 1 n              : (b & 1 ? a * poww(a * a % md, b / 2, md) % md n                       : poww(a * a % md, b / 2, md) % md)); n } n const long long MAXN = 1e6 + 10; n const long long INF = 8e18; n const long long MOD = 1e9 + 7; n vector<pair<pair<long long, long long>, int>> v; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   int t; n   cin >> t; n   while (t--) { n     v.clear(); n     int n; n     cin >> n; n     n = n * 2 - 1; n     v.resize(n); n     for (int i = 0; i < n; i++) n       cin >> v[i].first.first >> v[i].first.second, v[i].second = i + 1; n     sort((v).begin(), (v).end(), n          greater<pair<pair<long long, long long>, int>>()); n     vector<int> ans = {v.front().second}; n     for (int i = 1; i < n; i += 2) { n       if (v[i].first.second < v[i + 1].first.second) n         ans.push_back(v[i + 1].second); n       else n         ans.push_back(v[i].second); n     } n     sort((ans).begin(), (ans).end()); n     cout <<  YES  <<   n ; n     for (int e : ans) cout << e <<    ; n     cout <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   ; n   long long int t, x, sum = 0, y, z, flag = 0, a, p, l, r, b, c, q, i, j, k, n, n                       m, maxi = LONG_MIN, mini = LONG_MAX, mul = 1; n   string s, s1; n   vector<long long int> v, va, vb; n   map<long long int, long long int> mp, mp1; n   cin >> n; n   for (i = 0; i < n; i++) { n     cin >> x; n     mp[x]++; n     mp1[x]++; n   } n   auto it = mp.begin(); n   auto it1 = mp1.begin(); n   while (it1 != mp1.end()) { n     if (it->first == it1->first) it1++; n     if (it1 == mp1.end()) break; n     x = it->second; n     y = it1->second; n     for (i = 0; i < x && i < y; i++) { n       mp[it->first]--; n       sum++; n       mp1[it1->first]--; n     } n     if (x == i) it++; n     if (y == i) it1++; n   } n   cout << sum << endl; n } n


#include <bits/stdc++.h> n using namespace std; n struct ele { n   long long next, prev, delta, y; n }; n long long r, c, k, n, curcnt = 0, ans = 0; n vector<ele> a; n vector<long long> deltaleft, deltaright; n vector<vector<long long> > X, id; n vector<pair<long long, long long> > tmp; n void init() { n   X.clear(); n   X.resize(r + 1); n   id.resize(r + 1); n } n void remove(long long id) { n   long long cur = id; n   deltaleft.clear(); n   deltaright.clear(); n   for (long long i = 0; i <= k; i++) { n     if (cur == -1 || a[cur].delta == -1) break; n     deltaleft.push_back(a[cur].delta); n     cur = a[cur].prev; n   } n   cur = a[id].next; n   for (long long i = 0; i <= k; i++) { n     if (cur == -1 || a[cur].delta == -1) break; n     deltaright.push_back(a[cur].delta); n     cur = a[cur].next; n   } n   curcnt -= deltaright[0] * deltaleft[0]; n   curcnt -= deltaright[0] * (deltaright[0] - 1) / 2 + n             deltaleft[0] * (deltaleft[0] - 1) / 2; n   curcnt += n       (deltaright[0] + deltaleft[0]) * (deltaright[0] + deltaleft[0] - 1) / 2; n   for (long long i = 0; i <= k; i++) { n     if (i < deltaleft.size() && k - i < deltaright.size()) n       curcnt += deltaleft[i] * deltaright[k - i]; n   } n   a[a[id].prev].next = a[id].next; n   a[a[id].next].prev = a[id].prev; n   a[a[id].next].delta += a[id].delta; n } n long long cmp(pair<long long, long long> a, pair<long long, long long> b) { n   return X[a.first][a.second] < X[b.first][b.second]; n } n void solve(long long r1) { n   tmp.clear(); n   for (long long i = r1; i <= r; i++) { n     for (long long j = 0; j < X[i].size(); j++) { n       tmp.push_back({i, j}); n     } n   } n   sort(tmp.begin(), tmp.end(), cmp); n   a.resize(tmp.size() + 2); n   a[0].prev = -1; n   a[0].next = 1; n   a[0].y = 0; n   a[tmp.size() + 1].prev = (long long)tmp.size(); n   a[tmp.size() + 1].next = -1; n   a[tmp.size() + 1].y = c + 1; n   for (long long i = 0; i < tmp.size(); i++) { n     id[tmp[i].first][tmp[i].second] = i + 1; n     a[i + 1].y = X[tmp[i].first][tmp[i].second]; n     a[i + 1].prev = i; n     a[i + 1].next = i + 2; n   } n   a[0].delta = -1; n   for (long long i = 1; i < a.size(); i++) { n     a[i].delta = a[i].y - a[i - 1].y; n   } n   curcnt = 0; n   for (long long i = 1; i < a.size(); i++) { n     curcnt += (a[i].delta - 1) * a[i].delta / 2; n     for (long long j = 1; j <= k; j++) { n       if (i - j <= 0) break; n       curcnt += a[i].delta * a[i - j].delta; n     } n   } n   for (long long i = r; i >= r1; i--) { n     ans += curcnt; n     for (long long j = 0; j < X[i].size(); j++) { n       remove(id[i][j]); n     } n   } n } n signed main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cin >> r >> c >> n >> k; n   k--; n   init(); n   for (long long i = 1; i <= n; i++) { n     long long u, v; n     cin >> u >> v; n     X[u].push_back(v); n   } n   for (long long i = 1; i <= r; i++) { n     id[i].resize(X[i].size()); n   } n   for (long long i = 1; i <= r; i++) { n     solve(i); n   } n   ans = (r + 1) * r / 2 * (c + 1) * c / 2 - ans; n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 300007; n struct tup { n   int u, v, pos; n   friend bool operator<(tup x, tup y) { n     if (x.v < y.v) { n       return 1; n     } n     return 0; n   } n } ans[maxn]; n int rec[maxn], n; n int solve() { n   int cnt = 0; n   rec[cnt++] = n; n   ans[n].v = 2 * n + 1; n   for (int i = n - 1; i >= 0; --i) { n     if (ans[i].u < ans[rec[cnt - 1]].v && ans[i].u < ans[i].v) { n       rec[cnt++] = i; n     } n   } n   return cnt; n } n int main() { n   scanf( %d , &n); n   for (int i = 0; i < n; ++i) { n     scanf( %d%d , &ans[i].u, &ans[i].v); n     ans[i].pos = i; n   } n   sort(ans, ans + n); n   int cnt1 = solve() - 1; n   int tmp[maxn]; n   for (int i = 1; i <= cnt1; ++i) { n     tmp[i] = ans[rec[i]].pos + 1; n   } n   for (int i = 0; i < n; ++i) { n     ans[i].u = -ans[i].u; n     ans[i].v = -ans[i].v; n   } n   sort(ans, ans + n); n   int cnt = solve() - 1; n   if (cnt <= cnt1) { n     printf( %d n , cnt1); n     for (int i = 1; i <= cnt1; ++i) { n       printf( %d  , tmp[i]); n     } n   } else { n     printf( %d n , cnt); n     for (int i = 1; i <= cnt; ++i) { n       printf( %d  , ans[rec[i]].pos + 1); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main(int argc, char const *argv[]) { n   int n, q, counts = 0; n   cin >> n; n   int s[n]; n   for (int i = 0; i < n; i++) cin >> s[i]; n   cin >> q; n   int l[q], r[q]; n   for (int i = 0; i < q; i++) cin >> l[i] >> r[i]; n   int sums[n + 1]; n   sums[0] = 0; n   for (int i = 1; i < n + 1; i++) sums[i] = sums[i - 1] + s[i - 1]; n   for (int i = 0; i < q; i++) n     cout << (sums[r[i]] - sums[l[i] - 1]) / 10 << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e5 + 7; n const int M = 107; n const int inf = 1e9 + 7; n vector<int> g[N], a[M]; n int w[N][M], d[N]; n void bfs(int u) { n   queue<int> q; n   memset(d, inf, sizeof(d)); n   for (int i = 0; i < (int)a[u].size(); ++i) { n     q.push(a[u][i]); n     d[a[u][i]] = 0; n   } n   while (!q.empty()) { n     int v = q.front(); n     q.pop(); n     w[v][u] = d[v]; n     for (int i = 0; i < (int)g[v].size(); ++i) { n       int to = g[v][i]; n       if (d[to] > d[v] + 1) { n         d[to] = d[v] + 1; n         q.push(to); n       } n     } n   } n } n int main() { n   int n, m, k, second; n   scanf( %d %d %d %d , &n, &m, &k, &second); n   for (int i = 1, x; i <= n; ++i) { n     scanf( %d , &x); n     a[x].push_back(i); n   } n   for (int i = 0, u, v; i < m; ++i) { n     scanf( %d %d , &u, &v); n     g[u].push_back(v); n     g[v].push_back(u); n   } n   for (int i = 1; i <= k; ++i) bfs(i); n   for (int i = 1; i <= n; ++i) { n     sort(w[i] + 1, w[i] + k + 1); n     int ans = 0; n     for (int j = 1; j <= second; ++j) ans += w[i][j]; n     printf( %d  , ans); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int t, n, m, x; n   unordered_map<int, int> mp; n   cin >> t; n   int a[t]; n   for (int i = 1; i <= t; i++) { n     cin >> a[i]; n     mp[i] = a[i]; n   } n   for (int i = 1; i <= t; i++) { n     n = mp[i]; n     m = mp[n]; n     x = mp[m]; n     if (i == x) { n       cout <<  YES ; n       return 0; n     } n   } n   cout <<  NO  << endl; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxN = 23; n const int mod = 1000 * 1000 * 1000 + 7; n int dp[2][1 << maxN]; n int a[maxN]; n int res[maxN][maxN]; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   int n; n   cin >> n; n   for (int i = 0; i < n; i++) cin >> a[i]; n   dp[0][1] = 1; n   dp[0][0] = mod; n   memset(res, -1, sizeof res); n   for (int i = 0; i < n; i++) n     for (int j = 0; j < i; j++) n       for (int k = 0; k < i; k++) n         if (a[j] + a[k] == a[i]) res[i][j] = k; n   for (int i = 1; i < n; i++) { n     int u = i & 1; n     for (int mask = 0; mask < (1 << (i + 1)); mask++) dp[u][mask] = mod; n     for (int mask = 0; mask < (1 << i); mask++) { n       for (int j = 0; j < i; j++) { n         if (res[i][j] == -1) continue; n         if (mask >> j & 1) n           if (mask >> res[i][j] & 1) { n             int tmp = mask ^ (1 << i); n             dp[u][tmp] = n                 min(dp[u][tmp], max(__builtin_popcount(tmp), dp[!u][mask])); n             for (int k = 0; k < i; k++) { n               if (mask >> k & 1) { n                 dp[u][tmp ^ (1 << k)] = n                     min(dp[u][tmp ^ (1 << k)], dp[!u][mask]); n               } n             } n             break; n           } n       } n     } n   } n   int ans = mod; n   for (int mask = 0; mask < (1 << n); mask++) n     ans = min(ans, dp[(n - 1) & 1][mask]); n   if (ans == mod) n     cout << -1 << endl; n   else n     cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MN = 51; n char a[MN][MN], b[MN][MN]; n const char L =  L , R =  R , U =  U , D =  D ; n vector<pair<int, int> > sol, sol2; n int n, m; n void show() { n   ; n   for (int i = 0; i < n; ++i) n     ; n   ; n   ; n   for (int i = 0; i < n; ++i) n     ; n   ; n } n bool move(char arr[MN][MN], int i, int j) { n   ; n   if (arr[i][j] == U && arr[i][j + 1] == U && arr[i + 1][j] == D && n       arr[i + 1][j + 1] == D) { n     ; n     arr[i][j] = arr[i + 1][j] = L; n     arr[i][j + 1] = arr[i + 1][j + 1] = R; n   } else if (arr[i][j] == L && arr[i + 1][j] == L && arr[i][j + 1] == R && n              arr[i + 1][j + 1] == R) { n     ; n     arr[i][j] = arr[i][j + 1] = U; n     arr[i + 1][j] = arr[i + 1][j + 1] = D; n   } else { n     ; n     return false; n   } n   sol.emplace_back(i, j); n   return true; n   ; n   show(); n } n void push(char arr[MN][MN], int i, int k, bool ver) { n   char aim = ver ? U : L; n   ; n   show(); n   assert(arr[i][k] == L || arr[i][k] == U); n   if (arr[i][k] != aim) { n     if (!move(arr, i, k)) { n       push(arr, i + ver, k + !ver, !ver); n       move(arr, i, k); n     } n   } n } n int main() { n   scanf( %d%d , &n, &m); n   for (int i = 0; i < n; ++i) scanf( %s , a[i]); n   for (int i = 0; i < n; ++i) scanf( %s , b[i]); n   if (n % 2) { n     ; n     for (int k = 0; k < m; k += 2) n       for (int i = 0; i < n; ++i) push(a, i, k, false); n     swap(sol, sol2); n     ; n     for (int k = 0; k < m; k += 2) n       for (int i = 0; i < n; i++) push(b, i, k, false); n   } else { n     ; n     for (int i = 0; i < n; i += 2) n       for (int k = 0; k < m; k++) push(a, i, k, true); n     swap(sol, sol2); n     ; n     for (int i = 0; i < n; i += 2) n       for (int k = 0; k < m; k++) push(b, i, k, true); n   } n   printf( %d n , (int)(sol.size() + sol2.size())); n   reverse(sol.begin(), sol.end()); n   for (auto x : sol2) printf( %d %d n , x.first + 1, x.second + 1); n   for (auto x : sol) printf( %d %d n , x.first + 1, x.second + 1); 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   int t; n   cin >> t; n   while (t-- > 0) { n     int l, r; n     cin >> l >> r; n     cout << l <<     << 2 * l <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, Mod, p[100010], a[200010], nxt[200010], w[200010], tot, root, n     size[100010], son[100010], cnt; n bool vis[100010]; n long long ans, mul[100010]; n struct record { n   int dep, step; n   record(int D = 0, int S = 0) { n     dep = D; n     step = S; n   } n } pre[100010], suf[100010]; n int comp(record a, record b) { return a.dep < b.dep; } n void add(int x, int y, int val) { n   tot++; n   a[tot] = y; n   nxt[tot] = p[x]; n   w[tot] = val; n   p[x] = tot; n } n void getroot(int u, int fa, int N) { n   size[u] = 1; n   son[u] = 0; n   for (int i = p[u]; i != 0; i = nxt[i]) n     if (a[i] != fa && vis[a[i]] == false) { n       getroot(a[i], u, N); n       size[u] += size[a[i]]; n       son[u] = max(son[u], size[a[i]]); n     } n   son[u] = max(son[u], N - size[u]); n   if (son[u] < son[root]) root = u; n } n void getdeep(int u, int fa, long long pred, int step, long long sufd) { n   pre[++cnt] = record(pred, step); n   suf[cnt] = record(sufd, step); n   for (int i = p[u]; i != 0; i = nxt[i]) n     if (a[i] != fa && vis[a[i]] == false) n       getdeep(a[i], u, (pred * 10 + w[i]) % Mod, step + 1, n               (sufd + w[i] * mul[step] % Mod) % Mod); n } n int findL(int l, int r, int x, record *v) { n   int mid; n   while (l != r) { n     mid = (l + r) >> 1; n     if (v[mid].dep >= x) n       r = mid; n     else n       l = mid + 1; n   } n   if (v[l].dep != x) return 1000000000; n   return l; n } n int findR(int l, int r, int x, record *v) { n   int mid, ans = 0; n   while (l <= r) { n     mid = (l + r) >> 1; n     if (v[mid].dep <= x) { n       ans = max(ans, mid); n       l = mid + 1; n     } else n       r = mid - 1; n   } n   if (v[ans].dep != x) return 0; n   return ans; n } n long long getnum(int l, int r, int now, record *v) { n   int L, R; n   if (l > r) return 0; n   L = findL(l, r, now, v); n   R = findR(l, r, now, v); n   if (L <= R) return R - L + 1; n   return 0; n } n void exgcd(int a, int b, long long &x, long long &y) { n   if (b == 0) { n     x = 1; n     y = 0; n     return; n   } n   long long t; n   exgcd(b, a % b, x, y); n   t = x; n   x = y; n   y = t - (a / b) * y; n } n int getinv(long long A, long long B) { n   long long x, y; n   exgcd(A, Mod, x, y); n   x = (x % Mod + Mod) % Mod; n   x = (x * B) % Mod; n   return x; n } n long long calc(int u, int pre0, int s0, int suf0) { n   long long sum = 0; n   cnt = 0; n   getdeep(u, 0, pre0, s0, suf0); n   sort(pre + 1, pre + cnt + 1, comp); n   sort(suf + 1, suf + cnt + 1, comp); n   for (int i = 1; i <= cnt; i++) { n     int now = getinv(mul[pre[i].step], Mod - pre[i].dep); n     sum += getnum(1, cnt, now, suf); n     if (now == 0) --sum; n   } n   return sum; n } n void work(int u) { n   ans += calc(u, 0, 0, 0); n   vis[u] = true; n   for (int i = p[u]; i != 0; i = nxt[i]) n     if (vis[a[i]] == false) { n       ans -= calc(a[i], w[i] % Mod, 1, w[i] % Mod); n       root = 0; n       getroot(a[i], 0, size[a[i]]); n       work(root); n     } n } n int main() { n   scanf( %d%d , &n, &Mod); n   for (int i = 1; i < n; i++) { n     int x, y, v; n     scanf( %d%d%d , &x, &y, &v); n     ++x; n     ++y; n     add(x, y, v); n     add(y, x, v); n   } n   son[0] = 1000000000; n   root = 0; n   mul[0] = 1; n   for (int i = 1; i <= n; i++) mul[i] = mul[i - 1] * 10 % Mod; n   getroot(1, 0, n); n   work(root); n   printf( %I64d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <class T> n inline T gcd(T a, T b) { n   return b ? gcd(b, a % b) : a; n } n template <class T> n inline T LCM(T a, T b) { n   if (a < 0) return LCM(-a, b); n   if (b < 0) return LCM(a, -b); n   return a * (b / gcd(a, b)); n } n vector<int> arr; n const int N = 50005; n int memo[5001]; n int save[N]; n int visit[N]; n int find(int pos, int &xr) { n   for (int i = 0; i < N; i++) visit[i] = 0; n   vector<int> toAdd; n   int end = pos; n   visit[arr[pos]] = 1; n   for (int i = 0; i < pos; i++) { n     visit[arr[i]] = 2; n   } n   if (visit[arr[pos]] == 2) { n     return -1; n   } n   bool cant = false; n   for (int i = pos; i < arr.size(); i++) { n     if (visit[arr[i]] == 2) { n       cant = true; n     } n     if (visit[arr[i]] == 1) { n       if (cant) return -1; n       for (int j = 0; j < toAdd.size(); j++) { n         visit[toAdd[j]] = 1; n       } n       toAdd.clear(); n       end = i; n     } else { n       toAdd.push_back(arr[i]); n     } n   } n   xr = 0; n   for (int i = 0; i < N; i++) { n     if (visit[i] == 1) { n       xr = xr ^ i; n     } n   } n   return end; n } n int solve(int from) { n   int txr = 0; n   int ret = 0; n   if (from >= arr.size()) return 0; n   if (memo[from] != -1) { n     return memo[from]; n   } n   int end = find(from, txr); n   if (end != -1) { n     ret = max(txr + solve(end + 1), solve(from + 1)); n   } else { n     ret = solve(from + 1); n   } n   memo[from] = ret; n   return ret; n } n int main() { n   int n, i, temp, j; n   for (i = (int)0; i < (int)5001; ++i) { n     memo[i] = -1; n   } n   scanf( %d , &n); n   for (i = (int)0; i < (int)n; ++i) { n     scanf( %d , &temp); n     arr.push_back(temp); n   } n   int xx; n   cout << solve(0); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int a, b, c, d; n void solve() { n   double m = max((3.0) * a / (10.0), (double)(a - a * c / 250)); n   double v = max((3.0) * b / (10.0), (double)(b - b * d / 250)); n   if (m > v) cout <<  Misha  << endl; n   if (m == v) cout <<  Tie  << endl; n   if (m < v) cout <<  Vasya  << endl; n } n int main() { n   while (cin >> a >> b >> c >> d) { n     solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n string op; n bool check(int l, int r) { n   char c; n   cout <<  ?   << l <<     << r << endl; n   cin >> c; n   return c ==  x ; n } n int main() { n   while (cin >> op) { n     if (op ==  end ) { n       return 0; n     } n     int l = 1, r = 2; n     while (1) { n       if (check(l, r)) break; n       l <<= 1, r <<= 1; n     } n     while (l + 1 < r) { n       int mid = (l + r) >> 1; n       if (check(l - 1, mid)) n         r = mid; n       else n         l = mid + 1; n     } n     if (l == 1 && r == 2) { n       if (check(2, 3)) n         cout <<  ! 1  << endl; n       else n         cout <<  ! 2  << endl; n     } else { n       if (l == r) n         cout <<  !   << l << endl; n       else { n         if (check(l, l + 1)) n           cout <<  !   << l + 1 << endl; n         else n           cout <<  !   << l << endl; n       } n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n int n, k, a[1000005]; n long long l; n vector<int> val, cou, dp[1000005]; n void add(int &a, int b) { n   a += b; n   if (a >= 1000000007) a -= 1000000007; n } n void setup() { n   cin >> n >> l >> k; n   for (int i = 0; i <= n - 1; i++) { n     cin >> a[i]; n     val.push_back(a[i]); n   } n   sort(val.begin(), val.end()); n   val.resize(unique(val.begin(), val.end()) - val.begin()); n   cou.resize(val.size()); n   for (int i = 0; i <= n - 1; i++) { n     dp[i].resize(k + 1); n     dp[i][1] = 1; n     a[i] = lower_bound(val.begin(), val.end(), a[i]) - val.begin(); n   } n } n void xuly() { n   for (int j = 2; j <= k; j++) { n     for (int i = 0; i <= int(cou.size()) - 1; i++) cou[i] = 0; n     for (int i = 0; i <= n - 1; i++) add(cou[a[i]], dp[i][j - 1]); n     for (int i = 1; i <= int(cou.size()) - 1; i++) add(cou[i], cou[i - 1]); n     for (int i = 0; i <= n - 1; i++) dp[i][j] = cou[a[i]]; n   } n   int ans = 0; n   for (int i = 0; i <= n - 1; i++) n     for (int j = 1; j <= k; j++) { n       long long m = l / n; n       if (i < l % n) m++; n       m = m - j + 1; n       if (m > 0) ans = (ans + (m % 1000000007) * dp[i][j]) % 1000000007; n     } n   cout << ans; n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(NULL); n   cout.tie(NULL); n   setup(); n   xuly(); n   return 0; n } n


#include <bits/stdc++.h> n const int N = 500005; n int to[N << 1], edge, Next[N << 1], last[N], n, x, y, d[N], mx1[N], mx2[N]; n int f[N], fmx[N], ans1, mx[N], f0[N], f1[N], sum[N]; n std::vector<int> e[N]; n void upd(int &x, int y) { n   if (y > x) x = y; n } n void add(int x, int y) { n   to[++edge] = y; n   Next[edge] = last[x]; n   last[x] = edge; n } n void dfs1(int x, int y) { n   f[x] = y; n   for (int i = last[x]; i; i = Next[i]) { n     int u = to[i]; n     if (u == y) continue; n     dfs1(u, x); n     if (mx1[u] + 1 > mx1[x]) n       mx2[x] = mx1[x], mx1[x] = mx1[u] + 1; n     else if (mx1[u] + 1 > mx2[x]) n       mx2[x] = mx1[u] + 1; n   } n } n void dfs2(int x, int y) { n   if (y) e[fmx[x]].push_back(x); n   for (int i = last[x]; i; i = Next[i]) { n     int u = to[i]; n     if (u == y) continue; n     if (mx1[x] == mx1[u] + 1) n       fmx[u] = std::max(mx2[x], fmx[x]) + 1; n     else n       fmx[u] = std::max(mx1[x], fmx[x]) + 1; n     e[mx1[u] + 1].push_back(x); n   } n   for (int i = last[x]; i; i = Next[i]) n     if (to[i] != y) dfs2(to[i], x); n } n int main() { n   scanf( %d , &n); n   for (int i = 1; i < n; i++) { n     scanf( %d%d , &x, &y); n     add(x, y), add(y, x); n     d[x]++, d[y]++; n   } n   for (int i = 1; i <= n; i++) upd(ans1, d[i] + 1); n   dfs1(1, 0); n   dfs2(1, 0); n   f0[n] = f1[n] = 1; n   for (int i = n; i >= 1; i--) { n     int lst = 0; n     for (auto u : e[i]) { n       sum[u]++; n       upd(f0[i], sum[u]); n       upd(f1[i - 1], sum[u]); n       if (lst != u) upd(f1[i], sum[u]); n       upd(mx[f[u]], sum[u]); n       upd(f0[i], sum[u] + mx[u] - 2); n       if (f[u]) upd(f0[i], sum[u] + sum[f[u]] - 2); n       lst = u; n     } n   } n   for (int i = n; i >= 1; i--) upd(f0[i], f0[i + 1]), upd(f1[i], f1[i + 1]); n   printf( %d  , ans1); n   for (int i = 2; i <= n; i++) n     if (i & 1) n       printf( %d  , f1[i / 2]); n     else n       printf( %d  , f0[i / 2]); n } n


#include <bits/stdc++.h> n using namespace std; n int a[1000]; n bool b[1000][1000]; n int main() { n   int N, M; n   scanf( %d%d , &N, &M); n   for (int i = 0; i < N; i++) scanf( %d , &a[i]); n   for (int i = 0; i < M; i++) { n     int x, y; n     scanf( %d%d , &x, &y); n     --x; n     --y; n     b[x][y] = true; n     b[y][x] = true; n   } n   int mincost = 1000000000; n   for (int x = 0; x < N; x++) n     for (int y = 0; y < N; y++) n       for (int z = 0; z < N; z++) n         if (b[x][y] && b[y][z] && b[x][z]) n           if (a[x] + a[y] + a[z] < mincost) mincost = a[x] + a[y] + a[z]; n   if (mincost == 1000000000) n     printf( -1 ); n   else n     printf( %d n , mincost); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, m; n   cin >> n >> m; n   vector<vector<int> > arr(n + 1, vector<int>(m + 1)); n   for (int k = 1; k <= n; k++) { n     for (int i = 1; i <= m; i++) { n       cin >> arr[k][i]; n       arr[k][i] += arr[k][i - 1]; n     } n   } n   for (int i = 1; i <= m; i++) { n     for (int k = 1; k <= n; k++) { n       arr[k][i] += arr[k - 1][i]; n     } n   } n   int a, b; n   cin >> a >> b; n   int res = 1e9; n   for (int k = a; k <= n; k++) { n     for (int i = b; i <= m; i++) { n       res = min(res, n                 arr[k][i] - arr[k][i - b] - arr[k - a][i] + arr[k - a][i - b]); n     } n   } n   for (int k = b; k <= n; k++) { n     for (int i = a; i <= m; i++) { n       res = min(res, n                 arr[k][i] - arr[k][i - a] - arr[k - b][i] + arr[k - b][i - a]); n     } n   } n   cout << res <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n const int N = 1e5 + 50; n using namespace std; n int n, m, h, tot, mx, maxn, ans; n int a[N], b[N], cnt[N], num[N]; n int read() { n   int s = 0, t = 1; n   char c; n   while (c <  0  || c >  9 ) { n     if (c ==  - ) t = -1; n     c = getchar(); n   } n   while (c >=  0  && c <=  9 ) { n     s = s * 10 + c -  0 ; n     c = getchar(); n   } n   return s * t; n } n long long readl() { n   long long s = 0, t = 1; n   char c; n   while (c <  0  || c >  9 ) { n     if (c ==  - ) t = -1; n     c = getchar(); n   } n   while (c >=  0  && c <=  9 ) { n     s = s * 10 + c -  0 ; n     c = getchar(); n   } n   return s * t; n } n int main() { n   n = read(); n   m = read(); n   for (int i = 1; i <= m; i++) a[i] = read(), b[a[i]]++, mx = max(mx, a[i]); n   for (int i = 1; i <= mx; i++) n     if (b[i] > 0) cnt[++tot] = b[i]; n   for (int i = 1; i <= m; i++) { n     h = 0; n     for (int j = 1; j <= tot; j++) h += cnt[j] / i; n     if (h >= n) n       ans = i; n     else n       break; n   } n   printf( %d , ans); 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   int n, q; n   cin >> n >> q; n   set<int> s; n   multiset<int> d; n   auto Add = [&](int x) { n     auto ret = s.insert(x); n     auto it = ret.first; n     if (next(it) != s.end() && it != s.begin()) { n       d.erase(d.find(*next(it) - *prev(it))); n     } n     if (it != s.begin()) { n       d.insert(*it - *prev(it)); n     } n     if (next(it) != s.end()) { n       d.insert(*next(it) - *it); n     } n   }; n   auto Remove = [&](int x) { n     auto it = s.find(x); n     if (it != s.begin()) { n       d.erase(d.find(*it - *prev(it))); n     } n     if (next(it) != s.end()) { n       d.erase(d.find(*next(it) - *it)); n     } n     if (next(it) != s.end() && it != s.begin()) { n       d.insert(*next(it) - *prev(it)); n     } n     s.erase(it); n   }; n   for (int i = 0; i < n; i++) { n     int foo; n     cin >> foo; n     Add(foo); n   } n   for (int i = 0; i <= q; i++) { n     if (d.empty()) { n       cout << 0 <<   n ; n     } else { n       cout << *prev(s.end()) - *s.begin() - *prev(d.end()) <<   n ; n     } n     if (i == q) { n       break; n     } n     int c, x; n     cin >> c >> x; n     if (c) { n       Add(x); n     } else { n       Remove(x); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n inline long long read() { n   char ch = getchar(); n   long long s = 0, w = 1; n   while (ch <  0  || ch >  9 ) { n     if (ch ==  - ) w = -1; n     ch = getchar(); n   } n   while (ch >=  0  && ch <=  9 ) { n     s = s * 10 + ch -  0 ; n     ch = getchar(); n   } n   return s * w; n } n int d[1000010], t[1000010], prime[1000010], tot, vis[1000010]; n int n, fa[1000010], a[1000010], m; n long long c[1000010]; n inline void prepare(int Maxn) { n   d[1] = 1; n   for (register int i = 2; i <= Maxn; i++) { n     if (!vis[i]) { n       prime[++tot] = i; n       t[i] = d[i] = 2; n     } n     for (register int j = 1; 1LL * prime[j] * i <= Maxn; j++) { n       vis[i * prime[j]] = 1; n       if (i % prime[j]) { n         d[i * prime[j]] = d[i] * 2; n         t[i * prime[j]] = 2; n       } else { n         d[i * prime[j]] = d[i] / t[i] * (t[i] + 1); n         t[i * prime[j]] = t[i] + 1; n         break; n       } n     } n   } n } n inline void add(int x, int v) { n   for (; x <= n; x += x & (-x)) c[x] += v; n } n inline long long query(int x) { n   long long sum = 0; n   for (; x; x -= x & (-x)) sum += c[x]; n   return sum; n } n int find(int x) { return x == fa[x] ? x : fa[x] = find(fa[x]); } n int main() { n   prepare(1000010 - 10); n   n = read(), m = read(); n   for (register int i = 1; i <= n; i++) a[i] = read(), fa[i] = i; n   fa[n + 1] = n + 1; n   for (register int i = 1; i <= n; i++) add(i, a[i]); n   while (m--) { n     int opt = read(), l = read(), r = read(); n     if (opt == 1) { n       for (register int i = l; i <= r; i = find(i + 1)) { n         add(i, d[a[i]] - a[i]); n         a[i] = d[a[i]]; n         if (a[i] == 2 || a[i] == 1) fa[find(i)] = find(i + 1); n       } n     } else { n       printf( %lld n , query(r) - query(l - 1)); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n char str[100050]; n char a[5000], b[5000]; n int len1, len2, len3; n bool pp(int s1, int s2) { n   while (s2 <= len2) { n     if (str[s1] != a[s2]) return false; n     s1++, s2++; n   } n   return true; n } n bool pp2(int s1, int s2) { n   while (s2 <= len3) { n     if (str[s1] != b[s2]) return false; n     s1++, s2++; n   } n   return true; n } n bool rp(int s1, int s2) { n   while (s2 <= len2) { n     if (str[s1] != a[s2]) return false; n     s1--, s2++; n   } n   return true; n } n bool rp2(int s1, int s2) { n   while (s2 <= len3) { n     if (str[s1] != b[s2]) return false; n     s1--, s2++; n   } n   return true; n } n int main() { printf( Even n ); } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long num, tmp, sum, mx, l, r, mid; n   cin >> num; n   mx = sum = 0; n   for (int i = 0; i < num; i++) { n     cin >> tmp; n     sum += tmp; n     mx = max(mx, tmp); n   } n   l = mx; n   r = sum; n   while (l < r) { n     mid = (l + r) / 2; n     if (mid * num - sum >= mid) n       r = mid; n     else n       l = mid + 1; n   } n   cout << l; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 505; n const int maxm = 1e4 + 5; n struct node { n   int pre[maxn]; n   void init() { n     for (int i = 0; i < maxn; ++i) { n       pre[i] = i; n     } n   } n   int Find(int x) { n     if (x != pre[x]) pre[x] = Find(pre[x]); n     return pre[x]; n   } n   void v(int x, int y) { n     int fx = Find(x), fy = Find(y); n     if (fx != fy) pre[fy] = fx; n   } n }; n struct node q[maxm], h[maxm]; n int u[maxm], v[maxm]; n int n, m; n int solve(node a, node b) { n   int ans = 0; n   for (int i = 1; i <= n; ++i) { n     a.v(i, b.Find(i)); n   } n   for (int i = 1; i <= n; ++i) { n     if (a.Find(i) == i) ans++; n   } n   return ans; n } n int main() { n   scanf( %d%d , &n, &m); n   for (int i = 1; i <= m; ++i) scanf( %d%d , &u[i], &v[i]); n   q[0].init(); n   h[m + 1].init(); n   for (int i = 1; i <= m; ++i) { n     q[i] = q[i - 1]; n     q[i].v(u[i], v[i]); n   } n   for (int i = m; i >= 1; --i) { n     h[i] = h[i + 1]; n     h[i].v(u[i], v[i]); n   } n   int tt; n   scanf( %d , &tt); n   while (tt--) { n     int x, y; n     scanf( %d%d , &x, &y); n     printf( %d n , solve(q[x - 1], h[y + 1])); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   cin >> n; n   vector<long long int> v, t; n   v.push_back(0), t.push_back(0); n   long long int sum = 0, tt = 1, x = 0; n   double result = 0; n   long long int ix, a, b; n   for (int i = 0; i < n; ++i) { n     cin >> ix; n     if (ix == 1) { n       cin >> a >> b; n       t[a - 1] += b; n       sum += a * b; n     } else if (ix == 2) { n       cin >> a; n       v.push_back(a), t.push_back(0); n       sum += a; n       ++tt; n     } else { n       t[t.size() - 2] += t.back(); n       sum -= (v.back() + t.back()); n       --tt; n       v.pop_back(), t.pop_back(); n     } n     result = (double)sum / (double)tt; n     cout << fixed << setprecision(6) << result <<   n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const long long N = 1e9 + 5; n long long n, k; n bool valid(long long x) { n   long long maxi = (x)*k - ((x - 1) * ((x + 2) / 2.0)); n   if (maxi >= n) n     return true; n   else n     return false; n } n long long binary() { n   long long lo = 0, mid, hi = k; n   while (hi - lo > 0) { n     mid = (hi + lo) / 2; n     if (valid(mid)) n       hi = mid; n     else n       lo = mid + 1; n   } n   if (hi == lo) n     return lo; n   else n     return -1; n } n int main() { n   scanf( %I64d %I64d , &n, &k); n   long long ans = binary(); n   long long maxi = k; n   maxi += (ans - 1) * k - ((ans - 1) * ((ans + 2) / 2)); n   if (ans & 1 && ans != 1) maxi--; n   if (maxi >= n) n     printf( %I64d , ans); n   else n     printf( -1 ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int cnt[3600]; n int main() { n   int n; n   cin >> n; n   for (int i = 0; i < n; i++) { n     int h, m; n     scanf(  n%d:%d , &h, &m); n     cnt[h * 60 + m]++; n   } n   int ans = 0; n   for (int i = 0; i < 1440; i++) { n     if (cnt[i] == 0) { n       int cur = 1, j = (i + 1) % 1440; n       while (j != i && cnt[j] == 0) { n         j = (j + 1) % 1440; n         cur++; n       } n       ans = max(ans, cur); n     } n   } n   cout << (ans / 60) / 10 << (ans / 60) % 10 <<  :  << (ans % 60) / 10 n        << ans % 10; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long N = 1e6 + 10; n int a[N], b[N]; n int main() { n   int n; n   cin >> n; n   for (int i = 1; i <= n; i++) { n     cin >> a[i]; n   } n   vector<int> ans; n   for (int i = 1; i <= n; i++) { n     b[i] = a[i] - a[i - 1]; n   } n   for (int i = 1; i <= n; i++) { n     int flag = 0; n     for (int j = 1; i + j <= n; j++) { n       if (b[j] != b[i + j]) { n         flag++; n         break; n       } n     } n     if (!flag) { n       ans.push_back(i); n     } n   } n   cout << ans.size() << endl; n   for (auto i : ans) cout << i <<    ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e5 + 6; n bool flag = true; n vector<int> v[N]; n int a[N], p[N], h[N], g[N]; n void dfs(int u, int temp = -1) { n   a[u] = p[u]; n   int sum = 0; n   for (auto x : v[u]) { n     if (x == temp) continue; n     dfs(x, u); n     sum += g[x]; n     a[u] += a[x]; n   } n   if ((a[u] + h[u]) % 2 != 0) flag = false; n   g[u] = (a[u] + h[u]) / 2; n   if (g[u] >= 0 and g[u] <= a[u]) { n   } else n     flag = false; n   if (sum > g[u]) flag = false; n } n int main() { n   long long int t; n   cin >> t; n   while (t--) { n     int n, m; n     cin >> n >> m; n     for (int i = 0; i < n; i++) cin >> p[i]; n     ; n     for (int i = 0; i < n; i++) cin >> h[i]; n     ; n     for (int i = 0; i < n - 1; i++) { n       int a, b; n       cin >> a >> b; n       a--; n       b--; n       v[a].push_back(b); n       v[b].push_back(a); n     } n     dfs(0); n     cout << (flag ?  YES  :  NO ) << endl; n     flag = true; n     for (int i = 0; i < n; i++) v[i].clear(); n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int i = 0, j = 0, a; n   string s, t; n   cin >> s >> t; n   a = t.length(); n   while (a--) { n     if (t[j] == s[i]) { n       i++; n     } n     j++; n   } n   cout << i + 1; n } n


#include <bits/stdc++.h> n using namespace std; n char inp[1005], ans[1005]; n int n, p; n bool possible(int pos, int ok) { n   if (pos == n) return ok; n   int start = ok ? 0 : inp[pos] -  a ; n   int i; n   for (i = start; i < p; i++) { n     ans[pos] = i +  a ; n     if (pos > 0 && ans[pos - 1] == ans[pos]) continue; n     if (pos > 1 && ans[pos - 2] == ans[pos]) continue; n     if (possible(pos + 1, ok | (i > start))) return true; n   } n   return false; n } n int main() { n   scanf( %d%d , &n, &p); n   scanf( %s , inp); n   bool ok = 0; n   int i; n   if (possible(0, 0)) { n     ans[n] =   0 ; n     printf( %s n , ans); n   } else n     printf( NO n ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long lca(long long a, long long b) { n   while (a != b) { n     if (a < b) n       b /= 2; n     else n       a /= 2; n   } n   return a; n } n long long inf = 1000LL * 1000LL * 1000LL * 1000LL * 1000LL * 1000LL; n int main() { n   ios_base::sync_with_stdio(0); n   int t; n   cin >> t; n   map<long long, long long> M; n   while (t-- > 0) { n     int x; n     cin >> x; n     long long u, v, w; n     cin >> u >> v; n     if (x == 1) { n       cin >> w; n       long long y = lca(u, v); n       while (u != y) { n         M[u] += w; n         u /= 2; n       } n       while (v != y) { n         M[v] += w; n         v /= 2; n       } n     } else { n       long long y = lca(u, v); n       long long ans = 0; n       while (u != y) { n         ans += M[u]; n         u /= 2; n       } n       while (v != y) { n         ans += M[v]; n         v /= 2; n       } n       cout << ans <<   n ; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n; n int a[100]; n int main() { n   int x = 1; n   scanf( %d , &n); n   for (int i = 1; i <= n; ++i) { n     scanf( %d , &a[i]); n     if (a[i] < a[x]) x = i; n   } n   if (n == 1 || (n == 2 && a[1] == a[2])) { n     puts( -1 ); n   } else { n     printf( 1 n%d n , x); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n char comp[200005]; n vector<pair<int, int> > edge[200005]; n vector<int> father[200005]; n vector<string> ans; n int dis[200005], n, k; n queue<pair<int, int> > q; n void dfs(int u) { n   if (ans.size() >= k) return; n   if (u == n + 1) { n     ans.push_back(comp + 1); n     return; n   } n   for (int i = 0; i < father[u].size(); i++) { n     comp[father[u][i]] =  1 ; n     dfs(u + 1); n     comp[father[u][i]] =  0 ; n   } n } n int main() { n   char a[10]; n   int m; n   cin >> n >> m >> k; n   for (int i = 1; i <= m; i++) { n     int u, v; n     scanf( %d %d , &u, &v); n     edge[u].push_back(make_pair(v, i)); n     edge[v].push_back(make_pair(u, i)); n   } n   memset(dis, -1, sizeof(dis)); n   for (int i = 1; i <= m; i++) comp[i] =  0 ; n   q.push(make_pair(1, 0)); n   dis[1] = 0; n   while (!q.empty()) { n     pair<int, int> pa = q.front(); n     q.pop(); n     int u = pa.first, d = pa.second; n     for (int i = 0; i < edge[u].size(); i++) { n       int v = edge[u][i].first; n       if (dis[v] == -1) { n         dis[v] = d + 1; n         q.push(make_pair(v, d + 1)); n       } n     } n   } n   for (int i = 2; i <= n; i++) { n     for (int j = 0; j < edge[i].size(); j++) { n       if (dis[i] == dis[edge[i][j].first] + 1) n         father[i].push_back(edge[i][j].second); n     } n   } n   dfs(2); n   if (ans.size() >= k) { n     cout << k << endl; n     for (int i = 0; i < k; i++) cout << ans[i] << endl; n   } else { n     cout << ans.size() << endl; n     for (int i = 0; i < ans.size(); i++) cout << ans[i] << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, m; n   cin >> n >> m; n   for (int i = 0; i < n; i++) { n     int s, f, t; n     cin >> s >> f >> t; n     int x = t % (2 * (m - 1)); n     int y = t - x; n     int ans = 0; n     if (s == f) { n       ans = t; n     } else if (s < f) { n       if (x <= s - 1) { n         ans = y + f - 1; n       } else { n         ans = y + 2 * (m - 1) + f - 1; n       } n     } else { n       if (x <= (m - 1) + (m - s)) { n         ans = y + (m - 1) + m - f; n       } else { n         ans = y + 3 * (m - 1) + m - f; n       } n     } n     cout << ans << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 1e5 + 5; n int n, m, t; n vector<int> e[maxn]; n vector<int> ans; n stack<int> s; n bool vis[maxn]; n int dep[maxn]; n int N; n bool v[maxn]; n int dfs(int x, int fr) { n   vis[x] = true; n   s.push(x); n   for (auto y : e[x]) { n     if (y == fr) continue; n     if (!vis[y]) { n       dep[y] = dep[x] + 1; n       dfs(y, x); n       dep[y] = 0; n     } else if (dep[y]) { n       if (dep[x] - dep[y] + 1 >= N) { n         printf( 2 n ); n         printf( %d n , dep[x] - dep[y] + 1); n         while (s.top() != y) { n           printf( %d  , s.top()); n           s.pop(); n         } n         printf( %d n , s.top()); n         exit(0); n       } n     } n   } n   if (!v[x]) { n     for (auto y : e[x]) { n       v[y] = true; n     } n     ans.push_back(x); n   } n   s.pop(); n   return 0; n } n int read() { n   int x; n   scanf( %d , &x); n   return x; n } n int main() { n   int i, j; n   int x, y; n   n = read(); n   m = read(); n   N = sqrt(n); n   while (N * N < n) N++; n   for (i = 1; i <= m; i++) { n     x = read(); n     y = read(); n     e[x].push_back(y); n     e[y].push_back(x); n   } n   for (i = 1; i <= n; i++) { n     if (!vis[i]) { n       dep[i] = 1; n       dfs(i, -1); n       dep[i] = 0; n     } n   } n   printf( 1 n ); n   for (i = 1; i <= n; i++) { n     if (v[i]) continue; n     N--; n     printf( %d  , i); n     if (!N) break; n   } n   printf(  n ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, a[110], mx; n const int N = 30; n map<int, int> mem; n int Penu(int now) { n   if (!now || mem.find(now) != mem.end()) return mem[now]; n   register int ans = 0; n   register set<int> mex; n   for (register int i = 1; i < 31; i++) n     if (now >> i) mex.insert(Penu(now & ((1 << i) - 1) | (now >> i))); n   while (mex.count(ans)) ans++; n   return mem[now] = ans; n } n int ans; n int main() { n   ios::sync_with_stdio(false); n   cin.tie(nullptr); n   cout.tie(nullptr); n   cin >> n; n   mem[0] = 0; n   map<int, int> ma; n   for (register int i = 1, j; i <= n; i++) { n     cin >> j; n     for (register int r = 2, c = 0; r * r <= j; r++, c = 0) { n       while (!(j % r)) { n         c++; n         j /= r; n       } n       if (c) ma[r] |= (1 << c); n     } n     if (j > 1) ma[j] |= 2; n   } n   for (auto u : ma) ans ^= Penu(u.second); n   cout << (!ans ?  Arpa n  :  Mojtaba n ); n   return 0; n } n


#include<bits/stdc++.h> n  n #define ll long long int n #define ld long double n #define pb push_back n #define fi first n #define se second n #define all(x) x.begin(),x.end() n #define mem(x,y) memset(x,y,sizeof(x)) n #define pii pair<int,int> n #define pll pair<ll,ll> n #define INF 1e9+10 n #define INFL 1e18 n #define mod 1000000007 n //#define mod 998244353 n #define fast ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); n using namespace std; n  n  n  n  n ll power(ll x,ll n){ll res =1;while(n>0){if(n%2==1){res=res*x;}x=x*x;n=n/2;}return res;} n ll powm(ll a,ll b) {ll res=1;a%=mod; assert(b>=0); for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;} n  n //cout<< fixed << setprecision(10) n //__builtin_popcountll(); n //|_2.4_|=2   n int vis[500005]; n vector<int>g[500005]; n  n int lev[500005]; n int ans[500005]; n  n void bfs(int s) n { n  queue<int>q; n  q.push(s); n  vis[s]=1; n  lev[s]=0; n   n  while(!q.empty()) n  { n   int x=q.front(); n   q.pop(); n   for(int i=0;i<g[x].size();i++) n   { n     n    if(vis[g[x][i]]==0) n    { n     lev[g[x][i]]=lev[x]+1; n     vis[g[x][i]]=1; n     q.push(g[x][i]); n    } n     n   } n    n  } n } n void dfs(int s) n { n  vis[s]=1; n  for(auto x:g[s]) n  { n   if(lev[s]>=lev[x]) n    continue; n    n   dfs(x); n  n   ans[s]=min(ans[s],ans[x]); n  } n } n  n  n int main(){ n   n  fast; n  int T=1; n  cin>>T; n  while(T--) n  { n   int n,i,j,m; n   cin>>n>>m; n   vector<pii>v; n   for(i=0;i<m;i++) n   { n    int r,c; n    cin>>r>>c; n    v.pb({c,r}); n   } n   sort(all(v)); n   int l=0,r; n   if(m%2==1) n   { n    cout<< NO n ; n    continue; n   } n   for(i=0;i+1<m;i+=2) n   { n    if(v[i].fi==v[i+1].fi) n    { n     continue; n  n    } n    else n    { n     if(i+2<m&&v[i+1].fi==v[i+2].fi) n     { n      l=1; n      break; n     } n     if((v[i].se==v[i+1].se&&(v[i+1].fi-v[i].fi-1)%2==0)||(v[i].se!=v[i+1].se&&(v[i+1].fi-v[i].fi-1)%2==1)) n     { n      continue; n  n  n     } n     else n     { n      l=1; n      break; n     } n  n    } n   } n   if(l) n   { n    cout<< NO n ; n   } n   else n   { n    cout<< YES n ; n   } n  n  n    n  n    n  } n } n   n  n  n   n   n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   int n; n   string s, t; n   cin >> n >> s >> t; n   string ss = s, tt = t; n   sort(ss.begin(), ss.end()); n   sort(tt.begin(), tt.end()); n   if (ss != tt) n     cout << -1 <<   n ; n   else { n     vector<int> ans; n     for (int j = (int)(n - 1); j >= int(0); j--) { n       char target = t[j]; n       ; n       int pos = n - j - 1; n       ; n       while (target != s[pos]) pos++; n       ; n       ; n       ans.push_back(n); n       reverse(s.begin(), s.end()); n       ; n       ans.push_back(pos); n       string temp(s.begin() + n - pos, s.end()); n       reverse(temp.begin(), temp.end()); n       s.erase(s.begin() + n - pos, s.end()); n       s.insert(s.begin(), temp.begin(), temp.end()); n       ; n       ans.push_back(1); n       char c = s[n - 1]; n       s.erase(s.begin() + n - 1); n       s.insert(s.begin(), c); n       ; n     } n     cout << int((ans).size()) <<   n ; n     for (int i = 0; i < int(int((ans).size())); i++) { n       if (i) cout <<    ; n       cout << ans[i]; n     } n     cout <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long int MOD = 1000000000 + 7; n long long int arr[200005]; n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   long long int t; n   cin >> t; n   while (t--) { n     long long int n, sum = 0; n     cin >> n; n     for (long long int i = 0; i < n; i++) cin >> arr[i]; n     long long int i = 0; n     vector<long long int> vec; n     while (1) { n       long long int maxp = INT_MIN; n       long long int maxn = INT_MIN; n       for (; i < n; i++) { n         if (arr[i] < 0) n           break; n         else { n           maxp = max(maxp, arr[i]); n         } n       } n       if (maxp != INT_MIN) vec.push_back(maxp); n       if (i == n) break; n       for (; i < n; i++) { n         if (arr[i] >= 0) n           break; n         else { n           maxn = max(maxn, arr[i]); n         } n       } n       if (maxn != INT_MIN) vec.push_back(maxn); n       if (i == n) break; n     } n     for (long long int i = 0; i < vec.size(); i++) sum += vec[i]; n     cout << sum << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long maxn = 2e5 + 5; n int n, k, a[maxn], ans[maxn], pos[maxn]; n class Node { n  public: n   int l, r; n }; n Node lis[maxn]; n int main() { n   scanf( %d%d , &n, &k); n   for (int(i) = 0; (i) < (n); (i)++) { n     cin >> a[i + 1]; n     pos[a[i + 1]] = i + 1; n     lis[i + 1].l = i; n     lis[i + 1].r = i + 2; n   } n   int team = 1; n   for (int i = n; i >= 1; i--) { n     int p = pos[i]; n     if (ans[p] != 0) continue; n     ans[p] = team; n     for (int(j) = 0; (j) < (k); (j)++) { n       int el = lis[p].r; n       if (el != n + 1) { n         ans[el] = team; n         lis[lis[el].l].r = lis[el].r; n         lis[lis[el].r].l = lis[el].l; n       } else n         break; n     } n     for (int(j) = 0; (j) < (k); (j)++) { n       int el = lis[p].l; n       if (el != 0) { n         ans[el] = team; n         lis[lis[el].l].r = lis[el].r; n         lis[lis[el].r].l = lis[el].l; n       } else n         break; n     } n     lis[lis[p].l].r = lis[p].r; n     lis[lis[p].r].l = lis[p].l; n     if (team == 1) n       team = 2; n     else n       team = 1; n   } n   for (int(i) = 0; (i) < (n); (i)++) cout << ans[i + 1]; n   cout << endl; n } n


#include <bits/stdc++.h> n using namespace std; n inline bool isLow(char ch) { return (ch >=  a  && ch <=  z ); } n inline bool isUpp(char ch) { return (ch >=  A  && ch <=  Z ); } n inline bool isDig(char ch) { return (ch >=  0  && ch <=  9 ); } n inline bool are_equal(double a, double b) { n   return fabs(a - b) < numeric_limits<double>::epsilon(); n } n inline bool less_than(double a, double b) { n   return (b - a) > ((fabs(a) < fabs(b) ? fabs(b) : fabs(a)) * n                     numeric_limits<double>::epsilon()); n } n inline bool greater_than(double a, double b) { n   return (a - b) > ((fabs(a) < fabs(b) ? fabs(b) : fabs(a)) * n                     numeric_limits<double>::epsilon()); n } n template <typename T, typename... args> n inline string arrStr(T arr, int n) { n   stringstream s; n   s <<  [  ; n   for (int i = 0; i < n - 1; i++) s << arr[i] <<  ,  ; n   s << arr[n - 1] <<   ] ; n   return s.str(); n } n inline void __evars_begin(int line) { cout <<  #  << line <<  :  ; } n template <typename T> n inline void __evars_out_var(vector<T> val) { n   cout << arrStr(val, val.size()); n } n template <typename T> n inline void __evars_out_var(T *val) { n   cout << arrStr(val, 10); n } n template <typename T> n inline void __evars_out_var(T val) { n   cout << val; n } n inline void __evars(vector<string>::iterator it) { cout << endl; } n template <typename T, typename... Args> n inline void __evars(vector<string>::iterator it, T a, Args... args) { n   cout << it->substr((*it)[0] ==    , it->length()) <<   =  ; n   __evars_out_var(a); n   cout <<  ;  ; n   __evars(++it, args...); n } n void in(int &n, int a[]) { n   cin >> n; n   for (__typeof(n - 1) i = (0); i <= (n - 1); i++) cin >> a[i]; n } n void in(int &n, vector<int> &a) { n   cin >> n; n   a.resize(n); n   for (__typeof(n - 1) i = (0); i <= (n - 1); i++) cin >> a[i]; n } n void out(int n, int a[]) { n   for (__typeof(n - 1) i = (0); i <= (n - 1); i++) n     cout << a[i] <<    n [i + 1 == n]; n } n void out(vector<int> &a) { n   for (__typeof(a.size() - 1) i = (0); i <= (a.size() - 1); i++) n     cout << a[i] <<    n [i + 1 == a.size()]; n } n template <typename T> n T power(T val, T p, T mod) { n   val %= mod; n   T result = 1; n   while (p > 0) { n     if (p & 0x1) result = (result * val) % mod; n     val = (val * val) % mod; n     p >>= 1; n   } n   return result; n } n vector<string> split(const string &s, char c) { n   vector<string> v; n   stringstream ss(s); n   string x; n   while (getline(ss, x, c)) v.emplace_back(x); n   return move(v); n } n template <typename T> n T gcd(T a, T b) { n   return b == 0 ? a : gcd(b, a % b); n } n template <typename T> n T lcm(T a, T b) { n   return (a * b) / gcd(a, b); n } n template <typename T> n string toString(T x) { n   stringstream ss; n   ss << x; n   return ss.str(); n } n template <typename T> n T stringToT(string s, T a) { n   T p; n   istringstream ss(s); n   ss >> p; n   return p; n } n template <typename T> n T SOD(T n) { n   __typeof(n) sum = 0, i = 1; n   for (; i * i <= n; i++) n     sum += (n % i) ? 0 : ((((i) * (i)) == n) ? i : i + n / i); n   return sum; n } n template <typename T, typename S> n ostream &operator<<(ostream &os, const pair<T, S> &v) { n   os <<  (  << v.first <<  ,   << v.second <<  ) ; n   return os; n } n template <typename T, typename S> n ostream &operator<<(ostream &os, const map<T, S> &v) { n   for (auto it : v) os << it.first <<   :   << it.second <<   n ; n   return os; n } n template <typename T> n ostream &operator<<(ostream &os, const set<T> &v) { n   os <<  [ ; n   for (auto it : v) { n     os << it; n     if (it != *v.rbegin()) os <<  ,  ; n   } n   os <<  ] ; n   return os; n } n template <typename T> n ostream &operator<<(ostream &os, const vector<T> &v) { n   os <<  [ ; n   for (int i = 0; i < v.size(); i++) { n     os << v[i]; n     if (i != (int)v.size() - 1) os <<  ,  ; n   } n   os <<  ] ; n   return os; n } n const int fx[] = {+1, -1, +0, +0}; n const int fy[] = {+0, +0, +1, -1}; n const int fx8[] = {+0, +0, +1, -1, -1, +1, -1, +1}; n const int fy8[] = {-1, +1, +0, +0, +1, +1, -1, -1}; n const int kx[] = {-2, -2, -1, -1, 1, 1, 2, 2}; n const int ky[] = {-1, 1, -2, 2, -2, 2, -1, 1}; n int Set(int N, int pos) { return N = N | (1 << pos); } n int reset(int N, int pos) { return N = N & ~(1 << pos); } n bool check(int N, int pos) { return (bool)(N & (1 << pos)); } n const int mod = 1e9 + 7; n const int mx = 2 * 100001; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   long long x, y; n   int cases, tc; n   cin >> tc; n   for (cases = 1; cases <= tc; cases++) { n     cin >> x >> y; n     long long d = x - y; n     if (d == 1) n       cout <<  NO n ; n     else n       cout <<  YES n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n #pragma GCC optimize( O3 ) n using namespace std; n template <class T> n using V = vector<T>; n template <typename T> n using min_priority_queue = priority_queue<T, vector<T>, greater<T> >; n long long GCD(long long a, long long b) { return b ? GCD(b, a % b) : a; } n long long LCM(long long a, long long b) { return a / GCD(a, b) * b; } n long long int pom(long long int a, long long int n, int m) { n   long long int x = 1; n   for (a %= m; n; n /= 2) n& 1 ? x = x * a % m : 0, a = a * a % m; n   return x; n } n int dx[4] = {-1, 0, 1, 0}; n int dy[4] = {0, -1, 0, 1}; n int ddx[8] = {-1, 0, 1, 0, 1, 1, -1, -1}; n int ddy[8] = {0, -1, 0, 1, 1, -1, 1, -1}; n long long int cmp1(pair<pair<long long int, long long int>, long long int> a, n                    pair<pair<long long int, long long int>, long long int> b) { n   return a.first.second > b.first.second; n } n long long int cmp2(pair<long long int, long long int> a, n                    pair<long long int, long long int> b) { n   if (a.second != b.second) n     return a.second < b.second; n   else n     return a.first > b.first; n } n int main(int argc, char* argv[]) { n   cin.tie(0); n   ios::sync_with_stdio(false); n   long long int T; n   cin >> T; n   while (T--) { n     long long int n; n     cin >> n; n     V<long long int> a(n + 1); n     for (long long int i = 0; i < n; i++) { n       cin >> a[i]; n     } n     long long int ans = 0; n     bool abc = 0; n     long long int cnt = 0; n     for (long long int i = 1; i < n; i++) { n       if (a[i - 1] == 1 && a[i] == 0) { n         abc = 1; n         cnt = 1; n       } else if (abc == 1 && a[i] == 0) { n         cnt++; n       } n       if (abc == 1 && (a[i] == 0 && a[i + 1] == 1 && i < n - 1)) { n         ans += cnt; n         cnt = 0; n         abc = 0; n       } n     } n     cout << ans << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int INF = 0x3f3f3f3f; n const double eps = 1e-6; n const double PI = acos(-1); n const double R = 0.57721566490153286060651209; n const int maxn = 1005; n struct Edge { n   int from, to, cap, flow, cost; n   Edge(int u, int v, int c, int f, int cc) n       : from(u), to(v), cap(c), flow(f), cost(cc) {} n }; n struct MCMF { n   int n, m; n   vector<Edge> edges; n   vector<int> G[maxn]; n   int inq[maxn]; n   int d[maxn]; n   int p[maxn]; n   int a[maxn]; n   void init(int n) { n     this->n = n; n     for (int i = 0; i <= n; ++i) G[i].clear(); n     edges.clear(); n   } n   void add(int from, int to, int cap, int cost) { n     edges.emplace_back(Edge(from, to, cap, 0, cost)); n     edges.emplace_back(Edge(to, from, 0, 0, -cost)); n     m = int(edges.size()); n     G[from].emplace_back(m - 2); n     G[to].emplace_back(m - 1); n   } n   bool spfa(int s, int t, int &flow, int &cost) { n     for (int i = 1; i <= n; ++i) d[i] = INF; n     memset(inq, 0, sizeof(inq)); n     d[s] = 0; n     inq[s] = 1; n     p[s] = 0; n     queue<int> q; n     a[s] = INF; n     q.push(s); n     while (!q.empty()) { n       int u = q.front(); n       q.pop(); n       inq[u] = 0; n       for (int i = 0; i < int(G[u].size()); ++i) { n         Edge &e = edges[G[u][i]]; n         if (e.cap > e.flow && d[e.to] > d[u] + e.cost) { n           d[e.to] = d[u] + e.cost; n           p[e.to] = G[u][i]; n           a[e.to] = min(a[u], e.cap - e.flow); n           if (!inq[e.to]) { n             q.push(e.to); n             inq[e.to] = 1; n           } n         } n       } n     } n     if (d[t] == INF) return false; n     flow += a[t]; n     cost += d[t] * a[t]; n     for (int u = t; u != s; u = edges[p[u]].from) { n       edges[p[u]].flow += a[t]; n       edges[p[u] ^ 1].flow -= a[t]; n     } n     return true; n   } n   int MincostMaxflow(int s, int t, int &cost) { n     int flow = 0; n     cost = 0; n     while (spfa(s, t, flow, cost)) n       ; n     return flow; n   } n } mcmf; n int n, m, mincost; n void solve() { n   string S; n   cin >> S; n   cin >> n; n   map<int, int> num, mp[n + 1]; n   vector<int> a(n + 1); n   for (int i = 0; i < S.length(); i++) { n     num[S[i] -  a  + 1]++; n   } n   for (int i = 1; i <= n; i++) { n     string T; n     cin >> T >> a[i]; n     for (int j = 0; j < T.length(); j++) { n       mp[i][T[j] -  a  + 1]++; n     } n   } n   int s = 0, t = n + 26 + 1; n   mcmf.init(t); n   for (int i = 1; i <= 26; i++) { n     if (!num[i]) continue; n     mcmf.add(s, i, num[i], 0); n     for (int j = 1; j <= n; j++) { n       if (!mp[j][i]) continue; n       mcmf.add(i, j + 26, mp[j][i], j); n     } n   } n   for (int i = 1; i <= n; i++) { n     mcmf.add(i + 26, t, a[i], 0); n   } n   int maxflow = mcmf.MincostMaxflow(s, t, mincost); n   if (maxflow == S.length()) n     cout << mincost <<   n ; n   else n     cout << -1 <<   n ; n } n signed main() { n   ios_base::sync_with_stdio(false); n   solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int OO = 0x3f3f3f3f, NegOO = -1 * OO, N = 1e6 + 5, mod = 1e9 + 7; n long long gcd(long long x, long long y) { n   if (y == 0) return x; n   return gcd(y, x % y); n } n int main() { n   int t; n   cin >> t; n   while (t--) { n     int n, x, y, d; n     cin >> n >> x >> y >> d; n     if (x == y) { n       cout << 0 <<   n ; n       continue; n     } else if (x < y) { n       int mx = 1e9 + 5; n       int ans1 = mx, ans2 = mx, ans3 = mx; n       if ((y - x) % d == 0) ans1 = (y - x) / d; n       if ((y - 1) % d == 0) n         ans2 = (x - 1) / d + ((x - 1) % d != 0) + (y - 1) / d; n       if ((n - y) % d == 0) n         ans3 = (n - x) / d + ((n - x) % d != 0) + (n - y) / d; n       int ans = min(min(ans1, ans2), ans3); n       cout << ((ans != mx) ? ans : -1) <<   n ; n     } else { n       int mx = 1e9 + 5; n       int ans1 = mx, ans2 = mx, ans3 = mx; n       if ((x - y) % d == 0) ans1 = (x - y) / d; n       if ((y - 1) % d == 0) n         ans2 = (x - 1) / d + ((x - 1) % d != 0) + (y - 1) / d; n       if ((n - y) % d == 0) n         ans3 = (n - x) / d + ((n - x) % d != 0) + (n - y) / d; n       int ans = min(min(ans1, ans2), ans3); n       cout << ((ans != mx) ? ans : -1) <<   n ; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<long long> g[200000]; n long long c[200000]; n long long cnt; n void dfs(long long v, long long p = -1) { n   cnt++; n   long long cnt1 = cnt; n   for (long long to : g[v]) { n     if (to != p) { n       dfs(to, v); n     } n   } n   c[v] = cnt - cnt1 + 1; n } n signed main() { n   long long t; n   cin >> t; n   for (long long q = 0; q < t; q++) { n     long long k; n     cin >> k; n     k *= 2; n     vector<pair<long long, long long>> edge; n     long long t[k]; n     for (long long i = 0; i < k - 1; i++) { n       long long a, b; n       cin >> a >> b >> t[i]; n       a--; n       b--; n       g[a].push_back(b); n       g[b].push_back(a); n       edge.push_back({a, b}); n     } n     dfs(0); n     long long mnsum = 0, mxsum = 0; n     for (long long i = 0; i < k - 1; i++) { n       long long c1 = min(c[edge[i].first], c[edge[i].second]); n       mnsum += c1 % 2 * t[i]; n       mxsum += min(c1, k - c1) * t[i]; n     } n     cout << mnsum <<     << mxsum <<   n ; n     for (long long i = 0; i < k; i++) { n       g[i].clear(); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 410; n const int inf = 0x7fffffff; n int a[maxn]; n int n; n int main() { n   int ans = inf; n   cin >> n; n   for (int i = 1; i <= n; i++) cin >> a[i]; n   for (int i = 2; i <= n; i++) n     for (int j = i; j <= n; j++) { n       int x = a[i - 1] - 1, y = 0; n       for (int k = i; k <= j; k++) { n         int t = x / a[k]; n         y += t; n         x -= t * a[k]; n       } n       x = a[i - 1] - 1 - x + a[j]; n       y++; n       if (x < ans) { n         int o = x; n         for (int k = 1; k <= n; k++) { n           int t = x / a[k]; n           y -= t; n           x -= t * a[k]; n         } n         if (y < 0) ans = o; n       } n     } n   if (ans == inf) n     puts( -1 ); n   else n     printf( %d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int prime[15] = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47}; n int main() { n   int n; n   cin >> n; n   vector<int> res; n   int limit = 2 * n * n; n   for (int i = 0; i < 15; i++) { n     res.clear(); n     res.push_back(1); n     for (int j = 0; j <= i; j++) { n       int p = prime[j]; n       int sz = res.size(); n       for (int k = 0; k < sz; k++) { n         int x = res[k]; n         while (x * p <= limit) { n           x *= p; n           res.push_back(x); n         } n       } n     } n     if (res.size() >= n) { n       break; n     } n   } n   sort(res.begin(), res.end(), greater<int>()); n   for (int i = 0; i < n; i++) { n     cout << res[i] <<    ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const long long nax = 1e5 + 7; n const long long mod = 1e9 + 7; n const long long inf = numeric_limits<long long>::max(); n long long n, m; n bool check(vector<long long> &vec) { n   vector<long long> freq(n + 1, 0); n   for (long long i = 0; i < n; i++) { n     if (vec[i] > n) return false; n     freq[vec[i]]++; n     if (freq[vec[i]] > 1) { n       return false; n     } n   } n   for (long long i = 0; i < n; i++) { n     if (freq[i + 1] != 1) return false; n   } n   return true; n } n void test() { n   cin >> n; n   vector<long long> vec; n   vec.push_back(0); n   long long first = 0; n   for (long long i = 1; i < n; i++) { n     long long t; n     cin >> t; n     vec.push_back(vec.back() + t); n     first = min(first, vec.back()); n   } n   first = 1 - first; n   for (long long &x : vec) x += first; n   if (check(vec)) n     for (long long x : vec) cout << x <<    ; n   else n     cout << -1; n } n signed main() { n   ios_base ::sync_with_stdio(false); n   cin.tie(NULL); n   long long T = 1; n   while (T--) { n     test(); n     cout <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   int test; n   cin >> test; n   while (test--) { n     int n; n     cin >> n; n     vector<vector<int>> v(n, vector<int>(n, 0)); n     for (int i = 0; i < n; i++) { n       v[i][i] = 1; n       v[(i + 1) % n][i] = 1; n     } n     for (int i = 0; i < n; i++) { n       for (int j = 0; j < n; j++) cout << v[i][j] <<    ; n       cout <<   n ; n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n const long long int inf = (long long int)1e18 + 19; n const long long int mod = (long long int)1e9 + 7; n void cazz() { n   long long int t = 1, i; n   long double j, n; n   cin >> t; n   while (t--) { n     cin >> n; n     i = 3, j = -1; n     while (j < n) { n       j = (((long double)180 * (i - 2)) / i); n       i++; n     } n     if (j == n) { n       cout <<  YES n ; n       continue; n     } n     cout <<  NO n ; n   } n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   ; n   cazz(); n   return 0; n } n