#include <bits/stdc++.h> n using namespace std; n using namespace std; n bool debug = 1; n int main() { n   int n, m; n   scanf( %d %d , &n, &m); n   int amn[n + 1]; n   vector<vector<int> > v; n   v.resize(n + 1); n   for (int i = 0; i < m; i++) { n     int a, b; n     scanf( %d %d , &a, &b); n     v[a].push_back(b); n     v[b].push_back(a); n     amn[a]++; n     amn[b]++; n   } n   bool fs[n + 1]; n   fill(fs, fs + n + 1, 0); n   if (0) n     for (int i = 1; i <= n; i++) n       if (v[i].size() == 1) fs[i] = 1; n   int k = 0; n   bool is; n   do { n     bool nfs[n + 1]; n     for (int i = 1; i <= n; i++) nfs[i] = fs[i]; n     is = 0; n     k++; n     for (int i = 1; i <= n; i++) { n       if (fs[i]) continue; n       int amnoth = 0; n       for (int j = 0; j < v[i].size(); j++) { n         int oth = v[i][j]; n         if (!fs[oth]) amnoth++; n       } n       if (amnoth == 1) { n         nfs[i] = 1; n         is = 1; n       } n     } n     for (int i = 1; i <= n; i++) fs[i] = nfs[i]; n   } while (is); n   printf( %d n , k - 1); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n using LL = long long; n const int MAXN = 200010; n vector<int> x[MAXN], y[MAXN]; n int v[MAXN], out[MAXN], sg[MAXN]; n int xr[MAXN]; n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   int n, m; n   cin >> n >> m; n   for (int i = 0; i < n; ++i) cin >> v[i]; n   for (int i = 0; i < m; ++i) { n     int a, b; n     cin >> a >> b; n     --a; n     --b; n     x[a].push_back(b); n     y[b].push_back(a); n     ++out[a]; n   } n   queue<int> Q; n   for (int i = 0; i < n; ++i) { n     if (out[i] == 0) Q.push(i); n   } n   int maxsg = 0; n   while (!Q.empty()) { n     int a = Q.front(); n     Q.pop(); n     for (int b : y[a]) { n       --out[b]; n       if (out[b] == 0) { n         Q.push(b); n       } n     } n     set<int> s; n     for (int b : x[a]) s.insert(sg[b]); n     for (int i = 0; i < n; ++i) { n       if (s.find(i) == s.end()) { n         sg[a] = i; n         break; n       } n     } n     maxsg = max(maxsg, sg[a]); n   } n   for (int i = 0; i < n; ++i) { n     xr[sg[i]] ^= v[i]; n   } n   bool win = false; n   for (int i = maxsg; i >= 0; --i) { n     if (xr[i] != 0) { n       win = true; n       int p = -1; n       for (int j = 0; j < n; ++j) { n         if (sg[j] != i) continue; n         if (p == -1) { n           p = j; n           continue; n         } n         if ((v[j] ^ xr[i]) <= v[j]) p = j; n       } n       xr[i] ^= v[p]; n       v[p] = xr[i]; n       xr[i] = 0; n       for (int b : x[p]) { n         if (xr[sg[b]] != 0) { n           v[b] ^= xr[sg[b]]; n           xr[sg[b]] = 0; n         } n       } n       break; n     } n   } n   if (win) { n     cout <<  WIN n ; n     for (int i = 0; i < n; ++i) cout << v[i] <<    ; n     cout <<   n ; n   } else { n     cout <<  LOSE n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int N, i, sum = 0, max; n   cin >> N; n   int A[N]; n   for (i = 0; i <= N - 1; i++) cin >> A[i]; n   max = *max_element(A, A + N); n   for (i = 0; i <= N - 1; i++) sum += (max - A[i]); n   cout << sum << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n mt19937 rnd(339264); n const long long MOD = 998244353; n const double eps = 1e-7; n int main() { n   cout << fixed << setprecision(20); n   int n, w, m; n   cin >> n >> w >> m; n   if (2 * n < m) { n     cout <<  NO ; n     return 0; n   } n   int left = m; n   int j = 0; n   vector<vector<pair<int, int>>> ans(m); n   for (int i = 0; i < m; i++) { n     int need = n; n     if (left < m && left > n) { n       cout <<  NO ; n       return 0; n     } n     while (need >= left) { n       ans[i].push_back({j, left}); n       need -= left; n       left = m; n       j++; n     } n     if (need > 0) { n       ans[i].push_back({j, need}); n       left -= need; n     } n   } n   cout <<  YES  << endl; n   for (int i = 0; i < m; i++) { n     for (auto &p : ans[i]) { n       cout << p.first + 1 <<     << (p.second * w) / ((double)m) <<    ; n     } n     cout << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n inline int read() { n   char ch = getchar(); n   int x = 0, f = 1; n   while (ch <  0  || ch >  9 ) { n     if (ch ==  - ) f = -1; n     ch = getchar(); n   } n   while ( 0  <= ch && ch <=  9 ) { n     x = x * 10 + ch -  0 ; n     ch = getchar(); n   } n   return x * f; n } n struct node { n   int v, w; n   node() {} n   node(int x, int y) : v(x), w(y) {} n   bool operator<(const node& tl) const { return (v - w) < (tl.v - tl.w); } n } z[105], f[105]; n bool cmp(node aa, node bb) { return aa.v < bb.v; } n int dp[30005 * 2] = {0}; n int main() { n   int n, m, tot1, tot2; n   cin >> n >> m; n   tot1 = tot2 = 1; n   for (int i = 1; i < n + 1; ++i) { n     int x, y; n     cin >> x >> y; n     if (y < 0) n       f[tot2++] = node(x, -y); n     else n       z[tot1++] = node(x, y); n   } n   sort(z + 1, z + tot1, cmp); n   int ans = 0; n   for (int i = 1; i < tot1; ++i) { n     if (z[i].v <= m) { n       m += z[i].w; n       ++ans; n     } n   } n   sort(f + 1, f + tot2); n   int sum = 0; n   for (int i = 1; i < tot2; ++i) { n     for (int j = m; j >= max(f[i].v, f[i].w); --j) { n       dp[j] = max(dp[j], dp[j - f[i].w] + 1); n     } n   } n   cout << ans + dp[m] <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int test, n, mx, i; n double l, r, m, eps = 0.000001, a[100005]; n double pref(double tm) { n   double pos = 0, speed = 1; n   for (int i = 0; i < n; i++) { n     if ((a[i] - pos) <= tm * speed) n       tm -= (a[i] - pos) / speed, pos = a[i], speed += 1; n     else { n       pos += speed * tm; n       tm = 0; n       break; n     } n   } n   pos += speed * tm; n   return pos; n } n double suff(double tm) { n   double pos = mx, speed = 1; n   for (int i = n - 1; i >= 0; i--) { n     if ((pos - a[i]) <= tm * speed) n       tm -= (pos - a[i]) / speed, pos = a[i], speed += 1; n     else { n       pos -= speed * tm; n       tm = 0; n       break; n     } n   } n   pos -= speed * tm; n   return pos; n } n int main() { n   scanf( %d , &test); n   while (test--) { n     cin >> n >> mx; n     for (i = 0; i < n; i++) cin >> a[i]; n     l = 0., r = 2000000000.; n     while (r - l > eps) { n       m = (l + r) / 2.; n       if (pref(m) <= suff(m)) n         l = m; n       else n         r = m - eps; n     } n     cout << fixed << setprecision(9) << l <<   n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n long long a, b, s, c, ans, t; n int main() { n   cin >> t; n   while (t--) { n     cin >> s >> a >> b >> c; n     ans = ((s / (a * c)) * (a + b)) + (s % (a * c)) / c; n     cout << ans << endl; n   } n } n


#include <bits/stdc++.h> n char str[260][260]; n int main(void) { n   int t, n, m; n   scanf( %d , &t); n   while (t--) { n     int ans = 0; n     scanf( %d%d , &n, &m); n     getchar(); n     for (int i = 0; i < n; i++) gets(str[i]); n     for (int i = 0; i < n; i++) { n       for (int j = 0; j < m; j++) { n         if (str[i][j] ==  1 ) { n           if (str[i][j + 1] ==  1 ) { n             if (i >= 1 && str[i - 1][j] ==  1 ) continue; n             if (j >= 1 && str[i][j - 1] ==  1 ) continue; n             if (i >= 1 && j >= 1 && str[i - 1][j - 1] ==  1 ) continue; n             for (int k = 1; k + i < n && k + j < m; k++) { n               if (str[i + k][j] ==  0 ) break; n               if (str[i][j + k] ==  0 ) break; n               if (j >= 1 && str[i + k][j - 1] ==  1 ) break; n               if (i >= 1 && str[i - 1][j + k] ==  1 ) break; n               if (str[i + k][j + k] ==  0 ) continue; n               if (j + k + 1 < m && str[i + k][j + k + 1] ==  1 ) continue; n               if (i + k + 1 < n && str[i + k + 1][j + k] ==  1 ) continue; n               if (i + k + 1 < n && j + k + 1 < m && n                   str[i + k + 1][j + k + 1] ==  1 ) n                 continue; n               if (i + k + 1 < n && str[i + k + 1][j] ==  1 ) continue; n               if (j + k + 1 < m && str[i][j + k + 1] ==  1 ) continue; n               if (i + k + 1 < n && j >= 1 && str[i + k + 1][j - 1] ==  1 ) n                 continue; n               if (j + k + 1 < m && i >= 1 && str[i - 1][j + k + 1] ==  1 ) n                 continue; n               int flag = 0; n               for (int p = 1; p < k && flag == 0; p++) { n                 if (str[i + k][j + p] ==  0 ) flag = 1; n                 if (str[i + p][j + k] ==  0 ) flag = 1; n                 if (j + k + 1 < m && str[i + p][j + k + 1] ==  1 ) flag = 1; n                 if (i + k + 1 < n && str[i + k + 1][j + p] ==  1 ) flag = 1; n                 if (str[i + k - 1][j + p] ==  1 ) flag = 1; n                 if (str[i + p][j + k - 1] ==  1 ) flag = 1; n                 if (str[i + 1][j + p] ==  1 ) flag = 1; n                 if (str[i + p][j + 1] ==  1 ) flag = 1; n               } n               if (!flag) { n                 ans++; n                 break; n               } n             } n           } else { n             int flag = 1; n             for (int k = 2; k + i < n; k += 2) { n               if (j + k / 2 >= m || j - k / 2 < 0) break; n               if (str[i + k][j] ==  1 ) { n                 flag = 1; n                 if (i >= 1 && str[i - 1][j] ==  1 ) break; n                 if (j >= 1 && str[i][j - 1] ==  1 ) break; n                 if (j < m && str[i][j + 1] ==  1 ) break; n                 if (i >= 1 && j + 1 < m && str[i - 1][j + 1] ==  1 ) break; n                 if (i >= 1 && j >= 1 && str[i - 1][j - 1] ==  1 ) break; n                 if (str[i + 1][j] ==  1 ) break; n                 if (str[i + k - 1][j] ==  1 ) continue; n                 if (i + k + 1 < n && str[i + k + 1][j] ==  1 ) continue; n                 if (j >= 1 && str[i + k][j - 1] ==  1 ) continue; n                 if (j + 1 < m && str[i + k][j + 1] ==  1 ) continue; n                 if (i + k + 1 < n && j >= 1 && str[i + k + 1][j - 1] ==  1 ) n                   continue; n                 if (i + k + 1 < n && j + 1 < m && str[i + k + 1][j + 1] ==  1 ) n                   continue; n                 if (j - k / 2 - 1 >= 0 && str[i + k / 2][j - k / 2 - 1] ==  1 ) n                   continue; n                 if (str[i + k / 2][j - k / 2 + 1] ==  1 ) continue; n                 if (str[i + k / 2 - 1][j - k / 2] ==  1 ) continue; n                 if (str[i + k / 2 + 1][j - k / 2] ==  1 ) continue; n                 if (j >= 1 && str[i + k / 2 - 1][j - k / 2 - 1] ==  1 ) n                   continue; n                 if (j >= 1 && str[i + k / 2 + 1][j - k / 2 - 1] ==  1 ) n                   continue; n                 if (str[i + k / 2 - 1][j + k / 2] ==  1 ) continue; n                 if (str[i + k / 2 + 1][j + k / 2] ==  1 ) continue; n                 if (str[i + k / 2][j + k / 2 - 1] ==  1 ) continue; n                 if (j + k / 2 + 1 < m && str[i + k / 2][j + k / 2 + 1] ==  1 ) n                   continue; n                 if (j + k / 2 + 1 < m && n                     str[i + k / 2 - 1][j + k / 2 + 1] ==  1 ) n                   continue; n                 if (j + k / 2 + 1 < m && n                     str[i + k / 2 + 1][j + k / 2 + 1] ==  1 ) n                   continue; n                 flag = 0; n                 for (int p = 1; p <= k / 2 && flag == 0; p++) { n                   if (str[i + p][j + p] ==  0  || n                       str[i + p + 1][j + p] ==  1  || n                       str[i + p - 1][j + p] ==  1 ) n                     flag = 2; n                   if (str[i + p][j - p] ==  0  || n                       str[i + p + 1][j - p] ==  1  || n                       str[i + p - 1][j - p] ==  1 ) n                     flag = 2; n                   if (str[i + k - p][j + p] ==  0  || n                       str[i + k - p + 1][j + p] ==  1  || n                       str[i + k - p - 1][j + p] ==  1 ) n                     flag = 1; n                   if (str[i + k - p][j - p] ==  0  || n                       str[i + k - p + 1][j - p] ==  1  || n                       str[i + k - p - 1][j - p] ==  1 ) n                     flag = 1; n                 } n                 if (flag == 2) break; n                 if (flag == 1) continue; n                 for (int p = 1; p < k / 2 && flag == 0; p++) { n                   if (str[i + p - 1][j + p + 1] ==  1 ) flag = 2; n                   if (str[i + p + 1][j + p - 1] ==  1 ) flag = 2; n                   if (str[i + k - p - 1][j - p + 1] ==  1 ) flag = 1; n                   if (str[i + k - p + 1][j - p - 1] ==  1 ) flag = 1; n                   if (str[i + p - 1][j - p - 1] ==  1 ) flag = 2; n                   if (str[i + p + 1][j - p + 1] ==  1 ) flag = 2; n                   if (str[i + k - p - 1][j + p - 1] ==  1 ) flag = 1; n                   if (str[i + k - p + 1][j + p + 1] ==  1 ) flag = 1; n                 } n                 if (flag == 2) break; n                 if (flag == 1) continue; n               } n               if (!flag) { n                 ans++; n                 break; n               } n             } n           } n         } n       } n     } n     printf( %d n , ans); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e5 + 77; n int T, n, L, R, par[N], fa[N][20]; n int dep[N], son[N], tid[N], sz[N]; n pair<int, int> mx[N][3]; n vector<int> G[N]; n void dfs1(int v, int fa) { n   mx[v][0] = {0, v}, mx[v][1] = mx[v][2] = {-1, 0}; n   for (int u : G[v]) n     if (u ^ fa) { n       dfs1(u, v); n       pair<int, int> p = {mx[u][0].first + 1, u}; n       if (p > mx[v][0]) swap(p, mx[v][0]); n       if (p > mx[v][1]) swap(p, mx[v][1]); n       if (p > mx[v][2]) swap(p, mx[v][2]); n     } n }; n int main() { n   scanf( %d , &T); n   while (T--) { n     scanf( %d %d %d , &n, &L, &R); n     for (int i = 1; i <= n; i++) { n       G[i].clear(); n     } n     for (int i = 1, u, v; i < n; i++) { n       scanf( %d %d , &u, &v); n       G[u].push_back(v), G[v].push_back(u); n     } n     dfs1(1, 0); n     function<void(int, int)> dfs2 = [&](int v, int fa) { n       for (int u : G[v]) n         if (u ^ fa) { n           par[u] = max(par[v], n                        mx[v][0].second == u ? mx[v][1].first : mx[v][0].first) + n                    1, n           dfs2(u, v); n         } n     }; n     memset(par, 0, sizeof(par)), dfs2(1, 0); n     auto find = [&](int u, int v) { n       static int dist[N]; n       fill(dist + 1, dist + n + 1, -1); n       queue<int> q; n       q.push(u), dist[u] = 0; n       while (!q.empty()) { n         int x = q.front(); n         q.pop(); n         for (int y : G[x]) n           if (!~dist[y]) { n             q.push(y), dist[y] = dist[x] + 1; n           } n       } n       return dist[v]; n     }; n     int rt = -1, len = find(L, R); n     for (int i = 1; i <= n; i++) { n       int cnt = 0; n       if (mx[i][0].first >= len) cnt++; n       if (mx[i][1].first >= len) cnt++; n       if (mx[i][2].first >= len) cnt++; n       if (par[i] >= len) cnt++; n       if (cnt >= 3) { n         rt = i; n         break; n       } n     } n     if (!~rt) { n       printf( NO n ); n       continue; n     } n     int tim = 0; n     function<void(int)> dfs3 = [&](int v) { n       son[v] = v, tid[v] = ++tim, sz[v] = 1; n       for (int i = 1; i < 20; i++) { n         fa[v][i] = fa[fa[v][i - 1]][i - 1]; n       } n       for (int u : G[v]) n         if (u ^ fa[v][0]) { n           fa[u][0] = v, dep[u] = dep[v] + 1; n           dfs3(u), sz[v] += sz[u]; n           if (dep[son[u]] > dep[son[v]]) son[v] = son[u]; n         } n     }; n     fa[rt][0] = rt, dep[rt] = 0, dfs3(rt); n     bool flag = 0; n     for (int t = 1; t <= n; t++) { n       auto chk = [&](int u, int v) { n         return tid[v] >= tid[u] && tid[v] < tid[u] + sz[u]; n       }; n       if (chk(L, R) || chk(R, L)) { n         flag = 1; n         break; n       } n       auto get = [&](int v, int k) { n         for (int i = 19; ~i; i--) { n           if (k >> i & 1) v = fa[v][i]; n         } n         return v; n       }; n       int num = dep[son[L]] - dep[L]; n       L = son[L], R = get(R, num), swap(L, R); n     } n     printf( %s n , flag ?  YES  :  NO ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T> n void maximize(T &a, T b) { n   if (a < b) a = b; n } n const int maxN = 305; n int n, a[maxN][maxN], f[2][maxN][maxN]; n void solve() { n   for (int i = 1; i <= n; ++i) fill(f[0][i] + 1, f[0][i] + n + 1, INT_MIN); n   f[0][1][1] = a[1][1]; n   for (int d = 2; d < n + n; ++d) { n     for (int i = 1; i <= n; ++i) n       fill(f[(d + 1) & 1][i] + 1, f[(d + 1) & 1][i] + n + 1, INT_MIN); n     for (int i = 1; i <= n; ++i) n       for (int j = 1; j <= n; ++j) n         if (f[d & 1][i][j] > INT_MIN) n           for (int ni = i; ni < i + 2; ++ni) n             if (ni <= n && d - ni + 1 <= n) n               for (int nj = j; nj < j + 2; ++nj) n                 if (nj <= n && d - nj + 1 <= n) { n                   int t = f[d & 1][i][j] + a[ni][d - ni + 1]; n                   if (ni != nj) t += a[nj][d - nj + 1]; n                   maximize(f[(d + 1) & 1][ni][nj], t); n                 } n   } n } n int main() { n   ios_base::sync_with_stdio(false); n   scanf( %d , &n); n   for (int i = 1; i <= n; ++i) n     for (int j = 1; j <= n; ++j) scanf( %d , &a[i][j]); n   solve(); n   printf( %d n , f[0][n][n]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n using ll = long long; n using vi = vector<int>; n using pi = pair<int, int>; n void setIO(string name =   ) { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n } n int main() { n   setIO(); n   int a, b, c, d; n   cin >> a >> b >> c >> d; n   int pv = max(3 * a / 10, a - a / 250 * c); n   int pm = max(3 * b / 10, b - b / 250 * d); n   if (pv == pm) { n     cout <<  Tie ; n   } else if (pv > pm) { n     cout <<  Misha ; n   } else { n     cout <<  Vasya ; n   } n   return 0; n } n


#include <bits/stdc++.h> n double ans, f[10][1000]; n int n, k, a, v[10], p[10], g[10], ki[10], l[10]; n void calc() { n   int b = a; n   for (int i = 1; i <= n; ++i) { n     l[i] = p[i] + g[i]; n     if (l[i] > 100) l[i] = 100; n   } n   f[0][0] = 1; n   for (int i = 1; i <= n; ++i) n     for (int j = 0; j <= (1 << i - 1) - 1; ++j) { n       f[i][j] = f[i - 1][j] * (100 - l[i]) / 100; n       f[i][j + (1 << i - 1)] = f[i - 1][j] * l[i] / 100; n     } n   double res = 0; n   for (int i = 0; i <= (1 << n) - 1; ++i) { n     int tot = 0, b = a; n     for (int j = 1; j <= n; ++j) n       if (i & (1 << j - 1)) n         ++tot; n       else n         b += v[j]; n     if (tot * 2 > n) n       res += f[n][i]; n     else n       res += f[n][i] * a / b; n   } n   if (res > ans) ans = res; n } n void dfs(int now, int left) { n   if (now == n + 1) { n     calc(); n     return; n   } n   if (left == 0) n     dfs(now + 1, 0); n   else { n     for (int i = 0; i <= left; ++i) { n       if (p[now] + i * 10 >= 110) break; n       g[now] = i * 10; n       dfs(now + 1, left - i); n       g[now] = 0; n     } n   } n } n int main() { n   scanf( %d%d%d , &n, &k, &a); n   for (int i = 1; i <= n; ++i) scanf( %d%d , &v[i], &p[i]); n   dfs(1, k); n   printf( %.6lf n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<pair<int, int> > vec; n void update() { n   int x; n   scanf( %d , &x); n   vector<pair<int, int> > a, b; n   a.push_back({1000010, 0}); n   b.push_back({1000010, 0}); n   a.push_back({0, 0}); n   b.push_back({0, 0}); n   int sz = vec.size(); n   for (int i = 0; i + 1 < sz; i++) { n     int p = x - vec[i].first; n     if (p > 0) a.push_back({p, vec[i + 1].second}); n     if (p < 0) b.push_back({-p, vec[i].second}); n   } n   sort(a.begin(), a.end()); n   sort(b.begin(), b.end()); n   vec.clear(); n   int i = 0, j = 0; n   int as = a.size(), bs = b.size(); n   while (i < as || j < bs) { n     if (a[i].first == b[j].first) { n       vec.push_back({a[i].first, a[i].second + b[j].second}); n       i++, j++; n     } else if (a[i].first < b[j].first) { n       vec.push_back({a[i].first, a[i].second + b[j].second}); n       i++; n     } else { n       vec.push_back({b[j].first, a[i].second + b[j].second}); n       j++; n     } n   } n   vec[0].second = 0; n   return; n } n void qry() { n   int s, e, ans = 0; n   scanf( %d %d , &s, &e); n   int sz = vec.size(); n   for (auto cur : vec) { n     if (e > cur.first) { n       if (cur.first > s) { n         ans += cur.second * (cur.first - s); n         s = cur.first; n       } n     } else { n       ans += cur.second * (e - s); n       break; n     } n   } n   printf( %d n , ans); n   return; n } n int main() { n   int n, q; n   scanf( %d %d , &n, &q); n   vec.push_back({0, 0}); n   vec.push_back({n, 1}); n   vec.push_back({1000010, 0}); n   while (q--) { n     int type; n     scanf( %d , &type); n     if (type == 1) n       update(); n     else n       qry(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int in, x = 0; n   int cnt = 0, p = 1, ans = 0; n   cin >> in; n   for (int i = in; i > 0; i /= 10) { n     if (i % 10 == 7) ans += p; n     p *= 2; n     cnt++; n   } n   if (cnt != 0) x = pow(2, cnt) - 2; n   cout << ans + x + 1; n } n


#include <bits/stdc++.h> n using namespace std; n int a[100 + 5]; n int b[100 + 5]; n int main() { n   int m, n; n   int l, r; n   while (scanf( %d%d , &m, &n) != EOF) { n     int sum = 0; n     for (int i = 1; i <= m; i++) cin >> a[i]; n     for (int i = 1; i <= n; i++) { n       cin >> l >> r; n       int sum1 = 0; n       for (int i = l; i <= r; i++) { n         sum1 += a[i]; n       } n       if (sum1 > 0) sum += sum1; n     } n     cout << sum << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int dx[] = {1, -1, 0, 0}; n int dy[] = {0, 0, 1, -1}; n int xx[] = {0, 0, 1, -1, 1, -1, 1, -1}; n int yy[] = {1, -1, 0, 0, 1, -1, -1, 1}; n double PI = 3.1415926535897932384626433832795; n const long long oo = (long long)1e9 + 1; n const double eps = 1e-9; n const long long mod = 1000000009; n string a, b; n bool good(int l, string s) { n   for (int i = 0; i < ((long long)s.size()); i++) { n     if (a[i % l] != s[i]) return false; n   } n   return true; n } n int main() { n   ios_base::sync_with_stdio(0); n   cin >> a >> b; n   int res = 0; n   for (int i = 1; i <= min(((long long)a.size()), ((long long)b.size())); i++) { n     if (((long long)a.size()) % i == 0 && ((long long)b.size()) % i == 0 && n         good(i, a) && good(i, b)) n       res++; n   } n   cout << res << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n string t; n long long cnt1[3000001]; n int main() { n   ios_base::sync_with_stdio(0); n   cin >> t; n   cnt1[0] = 0; n   int n = (int)t.length(); n   for (int i = 0; i <= n - 4; ++i) { n     if (t.substr(i, 5) ==  heavy ) ++cnt1[i + 1]; n     cnt1[i + 1] += cnt1[i]; n   } n   long long ans = 0; n   for (int i = 0; i <= n - 4; ++i) n     if (t.substr(i, 5) ==  metal ) ans += cnt1[i]; n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n class wwj { n  public: n   wwj(int x); n   int klz(); n   int pdly(int k); n  n  private: n   int x; n }; n wwj::wwj(int x) { this->x = x; } n int wwj::klz() { n   int i, sum = 0; n   double SM = 0; n   if (x == 0) return 0; n   SM = (-1 + sqrt(1.0 + 8 * abs(x))) / 2; n   if (int(SM) == SM) n     i = int(SM); n   else n     i = int(SM + 1); n   while (1) { n     if (pdly(i)) n       return i; n     else n       ++i; n   } n } n int wwj::pdly(int k) { n   int s; n   s = k * (1 + k) / 2; n   x = abs(x); n   if (x % 2 == s % 2) return 1; n   return 0; n } n int main() { n   int x; n   cin >> x; n   wwj LR(x); n   cout << LR.klz() << endl; n   ; n } n


#include <bits/stdc++.h> n using namespace std; n const int mod = 1e9 + 7; n long long power(long long x, long long y, long long m) { n   if (y == 0) return 1; n   long long p = power(x, y / 2, m) % m; n   p = (p * p) % m; n   return (y % 2 == 0) ? p : (x * p) % m; n } n long long gcd(long long a, long long b) { n   if (a == 0) return b; n   return gcd(b % a, a); n } n long long modInverse(long long a, long long m) { n   long long g = gcd(a, m); n   if (g != 1) n     cout <<  Inverse doesn t exist ; n   else { n     return power(a, m - 2, m); n   } n } n long long log_a_b(long long a, long long b) { n   return (a > b - 1) ? 1 + log_a_b(a / b, b) : 0; n } n int t; n vector<vector<long long> > graph; n vector<long long> tin, tout; n vector<bool> vis; n vector<vector<long long> > up; n void dfs(int v, int p, int l) { n   vis[v] = true; n   tin[v] = ++t; n   up[v][0] = p; n   for (int(i) = (1); (i) < (l + 1); (i)++) { n     up[v][i] = up[v][i - 1] < 0 ? -1 : up[up[v][i - 1]][i - 1]; n   } n   for (int x : graph[v]) n     if (!vis[x]) dfs(x, v, l); n   tout[v] = ++t; n } n bool isan(int u, int v) { return tin[u] <= tin[v] && tout[u] >= tout[v]; } n int lca(int u, int v, int l) { n   if (isan(u, v)) return u; n   if (isan(v, u)) return v; n   for (int(i) = (l); (i) >= (0); (i)--) { n     if (up[u][i] == -1) continue; n     if (!isan(up[u][i], v)) u = up[u][i]; n   } n   return up[u][0]; n } n bool sum(long long n, long long i, long long s, long long cnt, n          vector<long long> &a) { n   if (i >= n && cnt > 0) { n     if (s != 0) n       return 0; n     else n       return 1; n   } n   if (i >= n && cnt == 0) return 0; n   bool a1 = sum(n, i + 1, s, cnt, a); n   bool b = sum(n, i + 1, s - a[i], cnt + 1, a); n   bool c = sum(n, i + 1, s + a[i], cnt + 1, a); n   bool d = (a1 | b | c); n   return d; n } n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.precision(15); n   long long T; n   cin >> T; n   while (T--) { n     long long n, q, k, m, u, v, w, x; n     string s, t; n     cin >> n >> m; n     long long ans = 0; n     m++; n     vector<long long> a(31), b(31); n     for (int(i) = (0); (i) < (31); (i)++) { n       a[i] = n % 2; n       b[i] = m % 2; n       n /= 2; n       m /= 2; n     } n     reverse((a).begin(), (a).end()); n     reverse((b).begin(), (b).end()); n     for (int(i) = (0); (i) < (31); (i)++) { n       if (a[i] && !b[i]) break; n       if (!a[i] && b[i]) ans = ans | (1 << (30 - i)); n     } n     cout << ans <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n inline int two(int n) { return 1 << n; } n inline void set_bit(int& n, int b) { n |= two(b); } n inline void unset_bit(int& n, int b) { n &= ~two(b); } n inline int last_bit(int n) { return n & (-n); } n template <class T> n T gcd(T a, T b) { n   return (b != 0 ? gcd<T>(b, a % b) : a); n } n template <class T> n T lcm(T a, T b) { n   return (a / gcd<T>(a, b) * b); n } n int dr[] = {-1, 0, 0, 1}, dc[] = {0, -1, 1, 0}; n const int md = 1000000007; n int dcmp(double a, double b) { n   return fabs(a - b) <= 1e-9 ? 0 : (a > b) ? 1 : -1; n } n int arr[1009], tmp[1009] = {0}; n int main() { n   std::ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   int n, r, sum = 0; n   cin >> n >> r; n   for (int i = 1; i <= n; ++i) { n     cin >> arr[i]; n     sum += arr[i]; n     if (arr[i] == 1) { n       for (int j = i - r + 1; j <= i + r - 1; ++j) { n         if (j >= 1 && j <= n) tmp[j]++; n       } n     } n   } n   int cnt = 0; n   for (int i = 1; i <= n; ++i) { n     if (tmp[i] < 1) return cout << -1, 0; n   } n   for (int i = 1; i <= n; ++i) { n     bool ok = false; n     if (arr[i] == 1) { n       for (int j = i - r + 1; j <= i + r - 1; ++j) { n         if (j >= 1 && j <= n) n           if (tmp[j] == 1) { n             ok = true; n             break; n           } n       } n       if (!ok) { n         cnt++; n         for (int j = i - r + 1; j <= i + r - 1; ++j) { n           if (j >= 1 && j <= n) tmp[j]--; n         } n       } n     } n   } n   cout << sum - cnt; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   cin.tie(0), cout.tie(0); n   ios::sync_with_stdio(false); n   string a, b; n   while (cin >> a >> b) { n     unsigned int len = a.size(); n     int ans = 0; n     bool OK = true; n     map<int, int> dic; n     for (unsigned int i = 0; i < len; ++i) { n       if (a.at(i) != b.at(i)) { n         if (!dic.count(a.at(i)) && !dic.count(b.at(i))) { n           ++ans; n           dic[a.at(i)] = b.at(i); n           dic[b.at(i)] = a.at(i); n         } else if (dic[a.at(i)] != b.at(i) || dic[b.at(i)] != a.at(i)) { n           cout << -1 << endl; n           OK = false; n           break; n         } n       } else { n         if (!dic.count(a.at(i))) { n           dic[a.at(i)] = a.at(i); n         } else if (dic[a.at(i)] != a.at(i)) { n           cout << -1 << endl; n           OK = false; n           break; n         } n       } n     } n     if (OK) { n       cout << ans << endl; n       int vis[1000] = {0}; n       if (ans != 0) { n         for (char c =  a ; c <=  z ; ++c) { n           if (!vis[(int)c] && dic[c] != 0 && dic[c] != c) { n             cout << c <<     << (char)dic[c] << endl; n             vis[dic[c]] = c; n           } n         } n       } n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 1000 * 100 + 15; n int n, mark[maxn], h[maxn], d[maxn], g[2], cnt, c, w; n vector<int> ad[maxn]; n void dfs(int v) { n   mark[v] = 1; n   for (auto u : ad[v]) { n     if (mark[u] == 0) { n       h[u] = h[v] + 1; n       dfs(u); n     } n   } n } n int main() { n   cin >> n; n   for (int i = 0; i < n - 1; i++) { n     int u, v; n     cin >> u >> v; n     ad[u].push_back(v); n     ad[v].push_back(u); n     d[u]++; n     d[v]++; n   } n   dfs(1); n   for (int i = 1; i <= n; i++) { n     cnt = 0; n     if (d[i] <= 1) { n       g[h[i] % 2] = 1; n     } n     for (auto u : ad[i]) { n       if (d[u] == 1) cnt++; n     } n     if (cnt > 1) { n       c++; n       w += cnt; n     } n   } n   if (g[0] && g[1]) n     cout << 3; n   else n     cout << 1; n   cout <<    ; n   cout << n - 1 + c - w; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   unsigned long long n, k; n   cin >> n >> k; n   unsigned long long lo = 0, hi = min(n / 2, n / k + 1), mid; n   while (lo < hi) { n     mid = lo + (hi - lo + 1) / 2; n     if (mid + mid * k <= n / 2) n       lo = mid; n     else n       hi = mid - 1; n   } n   cout << lo <<     << lo * k <<     << n - (lo + lo * k); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<long long> lol1; n vector<long long> lol2; n int main() { n   long long n, m; n   cin >> n >> m; n   for (long long i = 0; i < n; i++) { n     long long x; n     cin >> x; n     lol1.push_back(x); n   } n   for (long long i = 0; i < m; i++) { n     long long x; n     cin >> x; n     lol2.push_back(x); n   } n   long long s1 = lol1[0], s2 = lol2[0]; n   long long x = 1, y = 1; n   long long ans = 0; n   while (true) { n     if (s1 < s2) { n       if (x == n) break; n       s1 += lol1[x]; n       x++; n     } n     if (s2 < s1) { n       if (y == m) break; n       s2 += lol2[y]; n       y++; n     } n     if (s1 == s2) { n       ans++; n       s1 = lol1[x]; n       s2 = lol2[y]; n       x++; n       y++; n       if (x > n && y > m) break; n     } n   } n   cout << ans << endl; n } n


#include <bits/stdc++.h> n using namespace std; n const long long INF = 1 << 28; n const long long LINF = 1ll << 61; n inline long long getnum() { n   register long long r = 0; n   register bool ng = 0; n   register char c; n   c = getchar(); n   while (c !=  -  && (c <  0  || c >  9 )) c = getchar(); n   if (c ==  - ) ng = 1, c = getchar(); n   while (c >=  0  && c <=  9 ) r = r * 10 + c -  0 , c = getchar(); n   if (ng) r = -r; n   return r; n } n template <class T> n inline void putnum(T x) { n   if (x < 0) putchar( - ), x = -x; n   register short a[20] = {}, sz = 0; n   while (x > 0) a[sz++] = x % 10, x /= 10; n   if (sz == 0) putchar( 0 ); n   for (int i = sz - 1; i >= 0; i--) putchar( 0  + a[i]); n } n inline void putsp() { putchar(   ); } n inline void putendl() { putchar(  n ); } n inline char mygetchar() { n   register char c = getchar(); n   while (c ==     || c ==   n ) c = getchar(); n   return c; n } n int n, c; n int a[200111]; n set<pair<int, int> > s; n vector<pair<int, int> > tmps; n bool used[200111]; n int cnt[200111]; n int main() { n   c = getnum(), n = getnum(); n   s.insert(make_pair(-c, 0)); n   for (int i = 1; i <= n; i++) { n     a[i] = getnum(); n   } n   sort(a + 1, a + n + 1); n   for (int i = n; i >= 1; i--) { n     tmps.clear(); n     while (s.size() > 0 && -s.begin()->first > a[i]) { n       int x = s.begin()->second, y = -s.begin()->first + x; n       s.erase(s.begin()); n       tmps.push_back(make_pair(-a[i], x)); n       tmps.push_back(make_pair(-(y - (x + a[i])), x + a[i])); n     } n     for (int j = 0; j < tmps.size(); j++) { n       s.insert(tmps[j]); n     } n   } n   for (auto x : s) { n     used[x.second] = 1; n   } n   int tmp = c; n   for (int i = n; i >= 1; i--) { n     if (tmp >= a[i]) { n       cnt[a[i]]++; n       cnt[tmp + 1]--; n       tmp -= a[i]; n     } n   } n   int sum = 0; n   for (int i = 1; i <= c; i++) { n     sum += cnt[i]; n     if (sum > 0 && !used[c - i]) { n       cout << i << endl; n       return 0; n     } n   } n   puts( Greed is good ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   vector<int> g; n   while (cin >> n) { n     int xi, xf; n     xi = -1; n     xf = -1; n     bool init = false; n     g = vector<int>(n); n     for (int i = 0; i < n; i++) { n       cin >> g[i]; n       if (init == false && g[i] == 1) { n         xi = i; n         xf = i; n         init = true; n       } else if (init == true && g[i] == 1) n         xf = max(xf, i); n     } n     if (xi == -1 || xf == -1) n       cout << 0 << endl; n     else { n       int ceros = 0; n       for (int i = xi; i < xf;) { n         if (g[i] == 0 && g[i + 1] == 0) { n           i += 2; n           ceros += 2; n           int b = i; n           for (int q = b; q <= xf; q++) n             if (g[q] == 0) { n               ceros++; n               b = max(b, q); n             } else n               break; n           i = b + 1; n         } else n           i++; n       } n       cout << (xf - xi + 1) - ceros << endl; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); } n bool Check(int x, int y) { n   if (gcd(x, y) > 1) return false; n   if (gcd(x + 1, y) > 1) return false; n   if (gcd(x + 2, y) > 1) return false; n   return true; n } n vector<int> ans[10010]; n int main() { n   int n, k; n   cin >> n >> k; n   int x = 1; n   for (int i = 0; i < (n); i++) { n     ans[i].push_back(x * k); n     ans[i].push_back((x + 1) * k); n     ans[i].push_back((x + 2) * k); n     int y = x + 4; n     while (!Check(x, y)) y += 2; n     ans[i].push_back(y * k); n     x = y + 2; n   } n   cout << (x - 2) * k << endl; n   for (int i = 0; i < (n); i++) n     cout << ans[i][0] <<     << ans[i][1] <<     << ans[i][2] <<     n          << ans[i][3] << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long INF = 1e18; n const int N = 1e5 + 10, INFI = 1e9 + 10; n int val[12]; n unordered_set<int> s; n int main() { n   cin.tie(NULL); n   cout.tie(NULL); n   ios_base::sync_with_stdio(false); n   int n; n   cin >> n; n   for (int i = 0; i < 12; i++) { n     s.clear(); n     for (int a = 0; a <= i + 1; a++) { n       for (int b = 0; b <= i + 1 - a; b++) { n         for (int c = 0; c <= i + 1 - a - b; c++) { n           for (int d = 0; d <= i + 1 - a - b - c; d++) { n             if (a + b + c + d == i + 1) { n               s.insert((a * 1) + (b * 5) + (c * 10) + (d * 50)); n             } n           } n         } n       } n     } n     val[i] = s.size(); n   } n   if (n <= 12) { n     cout << val[n - 1] << endl; n   } else { n     cout << val[11] + 1LL * 49 * (n - 12) << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int a[200010], n, S, T; n vector<int> A; n int main() { n   scanf( %d , &n); n   for (int i = 0; i < n; i++) scanf( %d , a + i), S += a[i]; n   for (int i = 0; i < n; i++) n     if (S - a[i] == (long long)a[i] * (n - 1)) A.push_back(i + 1); n   printf( %d n , (int)A.size()); n   for (int i = 0; i < (int)A.size(); i++) n     printf( %d%c , A[i], i == (int)A.size() - 1 ?   n  :    ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<bool> primes(100006, false); n void sieve() { n   for (long long i = 3; i <= 100006; i += 2) { n     primes[i] = 1; n   } n   for (long long i = 3; i <= 100006; i += 2) { n     if (primes[i] == 1) { n       for (long long j = i * i; j <= 100006; j += i) { n         primes[j] = 0; n       } n     } n   } n   primes[2] = 1; n   primes[0] = primes[1] = 0; n } n int recpow(int x, int n) { n   if (n == 0) n     return 1; n   else if (n % 2 == 0) n     return recpow((x * x), n / 2); n   else n     return (x * recpow((x * x), (n - 1) / 2)); n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   long long k, a, b; n   cin >> k >> a >> b; n   long long count = 0; n   count = a / k; n   count += b / k; n   if (count == 0) n     cout << -1 << endl; n   else if (a % k > 0 and b / k == 0) n     cout << -1 << endl; n   else if (b % k > 0 and a / k == 0) n     cout << -1 << endl; n   else n     cout << count << endl; n } n


#include <bits/stdc++.h> n using namespace std; n struct node { n   int r, c, p; n   node() {} n   node(int _r, int _c, int _p) { r = _r, c = _c, p = _p; } n }; n const int MAXN = 1005; n char g[MAXN][MAXN]; n int dist[MAXN][MAXN]; n int dx[] = {1, 0, -1, 0}; n int dy[] = {0, 1, 0, -1}; n int to_int(const char& c) { return c -  0 ; } n bool is_digit(const char& c) { n   if ( 1  <= c && c <=  9 ) return true; n   return false; n } n void bfs(const int& r, const int& c, node s) { n   queue<node> q; n   q.push(s); n   memset(dist, -1, sizeof dist); n   dist[s.r][s.c] = 0; n   int xx, yy; n   while (!q.empty()) { n     s = q.front(); n     q.pop(); n     for (int i = 0; i < 4; ++i) { n       xx = dx[i] + s.r; n       yy = dy[i] + s.c; n       if (xx < 0 || xx >= r || yy < 0 || yy >= c || g[xx][yy] ==  T  || n           dist[xx][yy] != -1) n         continue; n       dist[xx][yy] = dist[s.r][s.c] + 1; n       q.emplace(xx, yy, s.p); n     } n   } n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   int r, c, best, ans; n   vector<node> b; n   node s, f; n   while (cin >> r >> c) { n     for (int i = 0; i < r; ++i) { n       for (int j = 0; j < c; ++j) { n         cin >> g[i][j]; n         if (is_digit(g[i][j])) b.emplace_back(i, j, to_int(g[i][j])); n         if (g[i][j] ==  S ) s = node(i, j, 0); n         if (g[i][j] ==  E ) f = node(i, j, 0); n       } n     } n     bfs(r, c, f); n     ans = 0; n     best = dist[s.r][s.c]; n     for (node i : b) n       if (dist[i.r][i.c] <= best && dist[i.r][i.c] != -1) ans += i.p; n     cout << ans <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 100005; n inline int read() { n   int s = 0, w = 1; n   char ch = getchar(); 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 n; n int a[N]; n void solve() { n   n = read(); n   int sum = 0; n   for (int i = 1; i <= n; i++) a[i] = read(), sum += a[i]; n   for (int i = 2; i * i <= sum; i++) { n     if (sum % i == 0) { n       printf( %d n , n); n       for (int i = 1; i <= n; i++) printf( %d  , i); n       puts(  ); n       return; n     } n   } n   printf( %d n , n - 1); n   bool ok = 0; n   for (int i = 1; i <= n; i++) { n     if (!ok && a[i] % 2 == 1) { n       ok = 1; n       continue; n     } n     printf( %d  , i); n   } n   puts(  ); n } n int main() { n   int T = read(); n   while (T--) solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MOD = 1e9 + 7; n const int INF = 1e9 + 7; n const int base = 1e9; n const int MAX = 1e5; n const double EPS = 1e-9; n const double PI = acos(-1.); n int main() { n   char x; n   string s1, s2, res; n   vector<char> v(10000); n   int c1, c2, cnt = 0, i = 0; n   cin >> s1 >> s2; n   if ((s1.length() - 1 + s2.length()) % 2 == 1) { n     cout <<  Impossible ; n     return 0; n   } n   for (int i = 0; i < s1.length(); i++) { n     if (s1.substr(i, 1) ==  | ) { n       c1 = cnt; n       cnt = 0; n     } else { n       cnt++; n     } n   } n   c2 = cnt; n   if (max(c1, c2) > (s1.length() - 1 + s2.length()) / 2) { n     cout <<  Impossible ; n     return 0; n   } n   if (c1 > c2) { n     res.append(s2, 0, (s2.length() - (c1 - c2)) / 2); n     res.append(s1); n     res.append(s2, (s2.length() - (c1 - c2)) / 2, n                (s2.length() + (c1 - c2)) / 2); n   } else { n     res.append(s2, 0, (s2.length() + (c2 - c1)) / 2); n     res.append(s1); n     res.append(s2, (s2.length() + (c2 - c1)) / 2, n                (s2.length() - (c2 - c1)) / 2); n   } n   cout << res; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m; n long long int arr[100050]; n vector<vector<long long int>> v(100050); n int main() { n   cin >> n >> m; n   for (int i = 1; i <= m; i++) cin >> arr[i]; n   for (int i = 1; i <= m; i++) { n     if (i > 1 && arr[i - 1] != arr[i]) v[arr[i]].push_back(arr[i - 1]); n     if (i < m && arr[i] != arr[i + 1]) v[arr[i]].push_back(arr[i + 1]); n   } n   long long int sum = 0, ans; n   for (int i = 1; i < m; i++) sum += abs(arr[i] - arr[i + 1]); n   ans = sum; n   for (int i = 1; i <= n; i++) { n     if (v[i].size() == 0) continue; n     sort(v[i].begin(), v[i].end()); n     long long int y = v[i][v[i].size() / 2]; n     long long int before = 0, after = 0; n     for (int x : v[i]) { n       before += abs(x - i); n       after += abs(y - x); n     } n     ans = min(ans, sum - before + after); n   } n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<int> prices; n int main() { n   long long int n, p, s = 0; n   cin >> n; n   for (long long int a = 0; a < n; a++) { n     cin >> p; n     prices.push_back(p); n     s += p; n   } n   sort(prices.rbegin(), prices.rend()); n   cin >> n; n   for (long long int a = 0; a < n; a++) { n     cin >> p; n     cout << s - prices[p - 1] <<   n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n vector<int> g[212345]; n int color[212345]; n bool vis[212345]; n map<int, int> m; n int r = 0; n int dfs(int i) { n   if (vis[i]) return 0; n   vis[i] = true; n   m[color[i]]++; n   if (m[r] < m[color[i]]) r = color[i]; n   int cont = 0; n   for (int v : g[i]) { n     cont += dfs(v); n   } n   return 1 + cont; n } n int solve(int i) { n   r = -1; n   m[-1] = 0; n   int cont = dfs(i); n   int rr = cont - m[r]; n   m.clear(); n   return rr; n } n int main() { n   int n, m, k; n   cin >> n >> m >> k; n   for (int i = 0; i < n; ++i) cin >> color[i]; n   for (int i = 0; i < m; ++i) { n     int l, r; n     cin >> l >> r; n     l--; n     r--; n     g[l].push_back(r); n     g[r].push_back(l); n   } n   memset(vis, false, sizeof vis); n   int cont = 0; n   for (int i = 0; i < n; ++i) { n     if (vis[i]) continue; n     cont += solve(i); n   } n   cout << cont << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long base = 71; n const long long mod = 1e9 + 9; n int convert(char a) { n   if (a >=  a  && a <=  z ) return (a -  a ) + 1; n   if (a >=  A  && a <=  Z ) return (a -  A ) + 27; n   return (a -  0 ) + 53; n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   string s; n   cin >> s; n   int n = (int)s.length(); n   long long ans = 0; n   vector<long long> V(n); n   long long H = 0; n   long long I = 0; n   long long p = 1; n   V[0] = 1ll; n   for (int i = 0; i < n; ++i) { n     H = (H * base % mod + (long long)convert(s[i])) % mod; n     I = (I + p * (long long)convert(s[i]) % mod) % mod; n     p = (p * base) % mod; n     if (i && H == I) V[i] = V[(i - 1) / 2] + 1ll; n     ans += V[i]; n   } n   cout << ans <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 200005; n int n, q, a[N], c[N], l[N], r[N]; n vector<int> s[N], p[N], b; n set<int> s1, s2, s3, s4, s5, s6, s7; n int main() { n   scanf( %d%d , &n, &q); n   for (int i = 1; i <= n; i++) scanf( %d , &a[i]); n   for (int i = 1; i <= q; i++) { n     scanf( %d%d , &l[i], &r[i]); n     p[l[i]].push_back(i); n   } n   for (int i = n; i; i--) { n     int x; n     while (s1.size() && a[x = *s1.begin()] < a[i]) { n       s6.insert(x); n       c[x]--; n       if (!c[x]) s5.insert(x); n       s1.erase(x); n     } n     while (s2.size() && a[x = *s2.begin()] > a[i]) { n       s7.insert(x); n       c[x]--; n       if (!c[x]) s5.insert(x); n       s2.erase(x); n     } n     while (s3.size() && a[x = *s3.begin()] <= a[i]) s3.erase(x); n     while (s4.size() && a[x = *s4.begin()] >= a[i]) s4.erase(x); n     if (s3.size() && s4.size()) { n       auto it = s5.lower_bound(max(*s3.begin(), *s4.begin())); n       if (it != s5.end() && (!b.size() || *it < b.back())) { n         b = {i, *it}; n         auto it2 = s1.lower_bound(*it); n         it2--; n         b.push_back(*it2); n         it2 = s2.lower_bound(*it); n         it2--; n         b.push_back(*it2); n         sort(b.begin(), b.end()); n       } n     } n     for (int j : p[i]) { n       if (b.size() && b.back() <= r[j]) n         s[j] = b; n       else { n         if (s3.size()) { n           auto it = s6.lower_bound(*s3.begin()); n           if (it != s6.end() && *it <= r[j]) s[j] = {i, *it - 1, *it}; n         } n         if (s4.size()) { n           auto it = s7.lower_bound(*s4.begin()); n           if (it != s7.end() && *it <= r[j]) s[j] = {i, *it - 1, *it}; n         } n       } n     } n     c[i] = 2; n     s1.insert(i), s2.insert(i), s3.insert(i), s4.insert(i); n   } n   for (int i = 1; i <= q; i++) { n     printf( %d n , s[i].size()); n     for (auto j : s[i]) printf( %d  , j); n     puts(  ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int K = 10; n const int NQ = 160000; n int n, k, q; n int v[NQ]; n pair<int, int> lg[K]; n int main() { n   std::ios_base::sync_with_stdio(false); n   std::cin.tie(NULL); n   cin >> n >> k >> q; n   for (int i = 0; i < n; i++) { n     cin >> v[i]; n   } n   for (int i = 0; i < k; i++) { n     lg[i].first = -1; n     lg[i].second = -1; n   } n   for (int i = 0; i < q; i++) { n     int x, y; n     cin >> x >> y; n     y--; n     if (x == 1) { n       if (lg[0].first == -1 || lg[0].first < v[y]) { n         lg[0] = make_pair(v[y], y); n         sort(lg, lg + k); n       } n     } else { n       bool flag = false; n       for (int i = 0; i < k; i++) { n         if (lg[i].second == y) { n           flag = true; n         } n       } n       cout << (flag ?  YES  :  NO ) << endl; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct __timestamper {}; n vector<vector<int>> ga; n vector<vector<int>> gb; n vector<int> comp; n vector<bool> used; n void dfsa(int v) { n   used[v] = true; n   comp.push_back(v); n   for (int u : ga[v]) { n     if (!used[u]) { n       dfsa(u); n     } n   } n } n int main() { n   int n, m, a, b; n   while (scanf( %d%d%d%d , &n, &m, &a, &b) == 4) { n     ga = gb = vector<vector<int>>(n); n     for (int i = 0; i < m; i++) { n       int x, y, c; n       scanf( %d%d%d , &x, &y, &c); n       --x, --y; n       auto& g = (c == a) ? ga : gb; n       g[x].push_back(y); n       g[y].push_back(x); n     } n     vector<pair<int, int>> order; n     for (int i = 0; i < n; i++) { n       for (int j = 0; j < n; j++) { n         order.emplace_back(i, j); n       } n     } n     sort(order.begin(), order.end(), n          [&](const pair<int, int>& x, const pair<int, int>& y) { n            return x.first * a + x.second * b < y.first * a + y.second * b; n          }); n     vector<int> comp_id(n, -1); n     used = vector<bool>(n, false); n     int cc = 0; n     int cc2 = -2; n     for (int i = 0; i < n; i++) { n       if (comp_id[i] == -1) { n         comp.clear(); n         dfsa(i); n         int cid = (comp.size() >= 4 ? cc++ : cc2--); n         for (int x : comp) { n           comp_id[x] = cid; n         } n       } n     } n     vector<vector<vector<pair<int, int>>>> vs( n         n, vector<vector<pair<int, int>>>(n)); n     vector<vector<int>> dp((1 << cc), vector<int>(n, 1 << 30)); n     int init_id = (comp_id[0] < 0) ? 0 : (1 << comp_id[0]); n     dp[init_id][0] = 0; n     vs[0][0].emplace_back(init_id, 0); n     vector<int> ans(n, 1 << 30); n     for (auto& [cnta, cntb] : order) { n       for (auto& [mask, v] : vs[cnta][cntb]) { n         if (dp[mask][v] != cnta * a + cntb * b) continue; n         ans[v] = min(ans[v], dp[mask][v]); n         for (int u : ga[v]) { n           if (dp[mask][u] > dp[mask][v] + a) { n             assert(comp_id[u] == comp_id[v]); n             dp[mask][u] = dp[mask][v] + a; n             vs[cnta + 1][cntb].emplace_back(mask, u); n           } n         } n         for (int u : gb[v]) { n           if (comp_id[u] == comp_id[v]) continue; n           int nmask = mask; n           if (comp_id[u] >= 0) { n             nmask |= (1 << comp_id[u]); n             if (mask == nmask) continue; n           } n           if (dp[nmask][u] > dp[mask][v] + b) { n             dp[nmask][u] = dp[mask][v] + b; n             vs[cnta][cntb + 1].emplace_back(nmask, u); n           } n         } n       } n     } n     for (int i = 0; i < n; i++) { n       printf( %d  , ans[i]); n     } n     printf(  n ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n bool d[400]; n void input() { n   int i, n, nk, nc, x; n   scanf( %d %d , &n, &nk); n   scanf( %d , &nc); n   for ((i) = 0; (i) < (nc); (i)++) scanf( %d , &x), d[x] = true; n   int res = 0, cnt = 0; n   for ((i) = (1); (i) <= (n); (i)++) { n     cnt++; n     if (cnt == nk || d[i]) { n       res++; n       cnt = 0; n     } n   } n   printf( %d , res); n } n int main() { n   input(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct edge { n   int v, next; n }; n bool islucky[100010]; n bool vis[100010]; n int A[100010], C[100010]; n edge e[2 * 100010]; n int beg[100010], f[100010]; n int n, m, cnt, tot, ne; n void dfs(int x) { n   if (x >= 100010) return; n   islucky[x] = true; n   dfs(x * 10 + 4); n   dfs(x * 10 + 7); n } n void pre_cal() { n   memset(islucky, 0, sizeof(islucky)); n   dfs(0); n   islucky[0] = false; n } n void dfs2(int x) { n   vis[x] = true; n   cnt++; n   for (int i = beg[x]; i != -1; i = e[i].next) n     if (!vis[e[i].v]) { n       dfs2(e[i].v); n     } n } n void add(int x, int y) { n   e[ne].v = y; n   e[ne].next = beg[x]; n   beg[x] = ne++; n } n void input() { n   int x, y; n   ne = 0; n   memset(beg, 255, sizeof(beg)); n   for (int i = 0; i < m; i++) { n     scanf( %d%d , &x, &y); n     add(x, y); n     add(y, x); n   } n   memset(vis, 0, sizeof(vis)); n   memset(C, 0, sizeof(C)); n   for (int i = 1; i <= n; i++) n     if (!vis[i]) { n       cnt = 0; n       dfs2(i); n       C[cnt]++; n     } n   tot = 0; n   for (int i = 1; i <= n; i++) n     if (C[i]) { n       A[tot] = i; n       C[tot++] = C[i]; n     } n } n void dp() { n   fill(f, f + n + 1, 0x3fffffff); n   f[0] = 0; n   for (int j = 0; j < tot; j++) { n     int num = C[j]; n     int k = 0; n     for (k = 0; (1 << k) <= num; k++) { n       for (int i = n; i >= (1 << k) * A[j]; i--) n         f[i] = min(f[i], f[i - (1 << k) * A[j]] + (1 << k)); n       num -= (1 << k); n     } n     if (num > 0) n       for (int i = n; i >= num * A[j]; i--) n         f[i] = min(f[i], f[i - num * A[j]] + num); n   } n   int ans = 0x3fffffff; n   for (int i = 1; i <= n; i++) n     if (islucky[i] && f[i] < ans) ans = f[i]; n   if (ans == 0x3fffffff) n     printf( -1 n ); n   else n     printf( %d n , ans - 1); n } n int main() { n   pre_cal(); n   while (scanf( %d%d , &n, &m) != EOF) { n     input(); n     dp(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   long long sum; n   vector<int> v; n   ios::sync_with_stdio(0); n   cin >> n; n   sum = (1LL + n) * n / 2LL; n   if (sum % 2LL == 0LL) { n     sum /= 2LL; n     for (int i = n; i >= 1; i--) n       if (sum >= i) { n         v.push_back(i); n         sum -= i; n       } n     cout <<  0  << endl; n     cout << v.size(); n     for (int i = 0; i < v.size(); i++) cout <<     << v[i]; n     cout << endl; n   } else { n     sum /= 2LL; n     for (int i = n; i >= 1; i--) n       if (sum >= i) { n         v.push_back(i); n         sum -= i; n       } n     cout <<  1  << endl; n     cout << v.size(); n     for (int i = 0; i < v.size(); i++) cout <<     << v[i]; n     cout << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n class Graph { n   long long n; n   vector<long long> f, par, ans, sz, depth, value, size, fst, to, next; n   long long l; n   void swapUtil(long long &, long long &); n   long long getP(long long); n   long long dfsUtil(long long); n   void setDFS(); n   void setsz(); n   void setf(); n   void setAns(long long); n  n  public: n   Graph(); n   Graph(long long); n   void addEdge(long long, long long); n   void setSolve(long long); n   void solve(long long, long long, long long); n   void printAns(); n }; n void Graph ::swapUtil(long long &u, long long &v) { n   long long t = u; n   u = v; n   v = t; n } n long long Graph ::getP(long long u) { n   if (f[u] == u) n     return f[u]; n   else { n     f[u] = getP(f[u]); n     return f[u]; n   } n } n long long Graph ::dfsUtil(long long u) { n   depth[u] = depth[par[u]] + 1; n   for (long long i = fst[u]; i; i = next[i]) n     if (to[i] != par[u]) { n       par[to[i]] = u; n       long long t = dfsUtil(to[i]); n       value[u] += size[u] * t * 2; n       size[u] += t; n     } n   value[u] += size[u] * (n - size[u] - 1) * 2; n   ans[0] += value[u]; n   size[u]++; n   return size[u]; n } n void Graph ::setDFS() { dfsUtil(1); } n void Graph ::setsz() { n   for (long long i = 1; i < n + 1; i++) sz[i] = 1; n } n void Graph ::setf() { n   for (long long i = 1; i < n + 1; i++) f[i] = i; n } n void Graph ::setAns(long long M) { ans.resize(M + 1); } n Graph ::Graph() {} n Graph ::Graph(long long N) { n   n = N - 1; n   f.resize(N); n   par.resize(N); n   ans.resize(N); n   sz.resize(N); n   depth.resize(N); n   value.resize(N); n   size.resize(N); n   fst.resize(N); n   to.resize(2 * N); n   next.resize(2 * N); n   l = 0; n } n void Graph ::addEdge(long long u, long long v) { n   to[++l] = v; n   next[l] = fst[u]; n   fst[u] = l; n   to[++l] = u; n   next[l] = fst[v]; n   fst[v] = l; n } n void Graph ::setSolve(long long m) { n   setAns(m); n   setf(); n   setsz(); n   setDFS(); n } n void Graph ::solve(long long u, long long v, long long i) { n   ans[i] = ans[i - 1]; n   u = getP(u); n   v = getP(v); n   while (u != v) { n     if (depth[u] < depth[v]) swap(u, v); n     long long t = getP(par[u]); n     ans[i] += n         sz[t] * sz[u] * (size[u] - sz[u]) * 2 + sz[t] * sz[u] * (sz[u] - 1); n     ans[i] += n         sz[u] * sz[t] * (n - size[u] - sz[t]) * 2 + sz[u] * sz[t] * (sz[t] - 1); n     ans[i] += sz[t] * (value[u] - (n - size[u]) * (size[u] - sz[u]) * 2); n     ans[i] += sz[u] * (value[t] - size[u] * (n - size[u] - sz[t]) * 2); n     value[t] = value[t] - sz[u] * (n - size[u] - sz[t]) * 2 + value[u] - n                (size[u] - sz[u]) * (n - size[u]) * 2; n     sz[t] += sz[u]; n     f[u] = t; n     u = t; n   } n } n void Graph ::printAns() { n   for (long long i = 0; i < ans.size(); i++) cout << ans[i] << endl; n } n signed main() { n   long long n; n   cin >> n; n   Graph g(n + 1); n   for (long long i = 0; i < n - 1; i++) { n     long long u, v; n     cin >> u >> v; n     g.addEdge(u, v); n   } n   long long m; n   cin >> m; n   g.setSolve(m); n   for (long long i = 0; i < m; i++) { n     long long u, v; n     cin >> u >> v; n     g.solve(u, v, i + 1); n   } n   g.printAns(); n   return 0; n } n


#include <bits/stdc++.h> n long long n; n long long check(long long x) { n   for (long long i = 2; i * i <= x; i++) n     if (x % i == 0) return 1; n   return 0; n } n long long exgcd(long long a, long long b, long long &x, long long &y) { n   long long g = a; n   if (b == 0) n     x = 1, y = 0; n   else n     g = exgcd(b, a % b, y, x), y -= (a / b) * x; n   return g; n } n int main() { n   scanf( %lld , &n); n   if (n == 1) return !printf( YES n1 n ); n   if (n == 4) return !printf( YES n1 3 2 4 n ); n   if (check(n)) return !printf( NO n ); n   printf( YES n1 n ); n   for (register long long i = 2; i <= n - 1; ++i) { n     long long x, y; n     exgcd(i - 1, n, x, y); n     printf( %lld n , ((x * i) % n + n) % n); n   } n   printf( %lld n , n); n   return 0; n } n


#include <bits/stdc++.h> n #pragma comment(linker,  /STACK:1000000000 ) n #pragma GCC optimize( Ofast,no-stack-protector ) n using namespace std; n const int maxn = (int)1e5 + 10; n vector<int> ed[maxn]; n int sz[maxn]; n void dfs(int v, int p) { n   sz[v] = 1; n   for (int u : ed[v]) { n     if (u != p) { n       dfs(u, v); n       sz[v] += sz[u]; n     } n   } n } n int h[maxn]; n vector<pair<pair<int, int>, int> > g[maxn]; n long long sum = 0; n set<int> vert[maxn]; n int tt = -1; n int tin[maxn], tout[maxn]; n void build(int v, int p, int pr, int hh) { n   tt++; n   tin[v] = tt; n   vert[pr].insert(v); n   sum += hh; n   h[v] = hh; n   sz[v] = 1; n   vector<int> a; n   for (int u : ed[v]) { n     if (u == p) { n       continue; n     } n     build(u, v, pr, hh + 1); n     sz[v] += sz[u]; n     if (sz[u] & 1) { n       a.push_back(u); n     } n   } n   while ((int)a.size() >= 2) { n     int x = a.back(); n     a.pop_back(); n     int y = a.back(); n     a.pop_back(); n     g[pr].push_back(make_pair(make_pair(x, y), hh)); n   } n   if (!a.empty()) { n     g[pr].push_back(make_pair(make_pair(a.back(), v), hh)); n   } n   tout[v] = tt; n } n bool is_anc(int x, int y) { return tin[x] <= tin[y] && tout[y] <= tout[x]; } n int main() { n   int n; n   long long k; n   cin >> n >> k; n   for (int i = 0; i < n - 1; i++) { n     int x, y; n     scanf( %d %d , &x, &y); n     x--; n     y--; n     ed[x].push_back(y); n     ed[y].push_back(x); n   } n   dfs(0, -1); n   int v = 0; n   while (true) { n     bool ok = true; n     for (int u : ed[v]) { n       if (sz[u] < sz[v] && 2 * sz[u] > n) { n         v = u; n         ok = false; n         break; n       } n     } n     if (ok) { n       break; n     } n   } n   for (int u : ed[v]) { n     build(u, v, u, 1); n     reverse((g[u]).begin(), (g[u]).end()); n   } n   if (k > sum) { n     cout <<  NO n  <<   n ; n     return 0; n   } n   set<pair<int, int> > s; n   for (int u : ed[v]) { n     s.insert(make_pair(sz[u], u)); n   } n   vector<pair<int, int> > res; n   if ((k - sum) & 1) { n     cout <<  NO n  <<   n ; n     return 0; n   } n   while (!s.empty() && sum != k) { n     int u = s.rbegin()->second; n     s.erase(*s.rbegin()); n     if (sz[u] == 1) { n       cout <<  NO n  <<   n ; n       return 0; n     } n     if (sum - 2 * g[u].back().second >= k) { n       sum -= 2 * g[u].back().second; n       sz[u] -= 2; n       s.insert(make_pair(sz[u], u)); n       res.push_back(g[u].back().first); n       vert[u].erase(g[u].back().first.first); n       vert[u].erase(g[u].back().first.second); n       g[u].pop_back(); n       continue; n     } n     int x = g[u].back().first.first; n     int y = -1; n     for (int z : vert[u]) { n       if (sum - 2 * h[z] == k && is_anc(z, x)) { n         y = z; n         break; n       } n     } n     assert(y != -1); n     res.push_back(make_pair(x, y)); n     vert[u].erase(x); n     vert[u].erase(y); n     break; n   } n   cout <<  YES n ; n   s.clear(); n   for (int u : ed[v]) { n     if (vert[u].size()) { n       s.insert(make_pair(vert[u].size(), u)); n     } n   } n   while ((int)s.size() > 1) { n     int x = s.rbegin()->second; n     s.erase(*s.rbegin()); n     int y = s.rbegin()->second; n     s.erase(*s.rbegin()); n     int a = *vert[x].begin(); n     vert[x].erase(a); n     int b = *vert[y].begin(); n     vert[y].erase(b); n     res.push_back(make_pair(a, b)); n     if (vert[x].size()) { n       s.insert(make_pair(vert[x].size(), x)); n     } n     if (vert[y].size()) { n       s.insert(make_pair(vert[y].size(), y)); n     } n   } n   int x = s.begin()->second; n   assert((int)vert[x].size() == 1); n   res.push_back(make_pair(v, *vert[x].begin())); n   for (pair<int, int> f : res) { n     printf( %d %d n , f.first + 1, f.second + 1); n   } n } n


#include <bits/stdc++.h> n using namespace std; n const long long MOD = 1000 * 1000 * 1000 + 7; n const long long INF = 2 * 1000 * 1000 + 200; n const long long MAXN = 100 * 1000 + 1; n const long long MLOG = 18; n const long long NMAX = 1 * 1000; n signed main() { n   long long n, m, a[200]; n   a[0] = 0; n   long long x, y; n   cin >> n; n   for (long long i = 1; i <= n; i++) cin >> a[i]; n   cin >> m; n   for (long long i = 0; i < m; i++) { n     cin >> x >> y; n     a[x - 1] += y - 1; n     a[x + 1] += a[x] - y; n     a[x] = 0; n   } n   for (long long i = 1; i <= n; i++) cout << a[i] << endl; n } n


#include <bits/stdc++.h> n using namespace std; n ofstream out( biconex.out ); n ifstream in( biconex.in ); n struct Edge { n   int node; n   bool is_critic; n }; n const int N_MAX = 100000; n vector<Edge> neighbours[1 + N_MAX]; n stack<int> component; n int n, m, h, minim = N_MAX + 1, node_ans, Depth, root_sons, depth[1 + N_MAX], n              height[1 + N_MAX], main_hour[1 + N_MAX], color[1 + N_MAX]; n bool visited[1 + N_MAX], not_in_subgraph[1 + N_MAX], is_critic[1 + N_MAX], n     on_stack[1 + N_MAX]; n void read() { n   cin >> n >> m >> h; n   for (int i = 1; i <= n; i++) cin >> main_hour[i]; n   for (int i = 0; i < m; i++) { n     int x, y; n     cin >> x >> y; n     if (main_hour[x] - main_hour[y] == 1 | n         (main_hour[x] == 0 & main_hour[y] == h - 1)) n       neighbours[y].push_back({x, false}); n     if (main_hour[y] - main_hour[x] == 1 | n         (main_hour[y] == 0 & main_hour[x] == h - 1)) n       neighbours[x].push_back({y, false}); n   } n   for (int i = 1; i <= n; i++) depth[i] = -1; n } n void DFS(int node, int root) { n   depth[node] = height[node] = Depth; n   component.push(node); n   on_stack[node] = true; n   Depth++; n   for (Edge& son : neighbours[node]) { n     if (depth[son.node] == -1) { n       DFS(son.node, root); n       height[node] = min(height[node], height[son.node]); n     } else if (on_stack[son.node]) n       height[node] = min(height[node], depth[son.node]); n   } n   if (height[node] == depth[node]) { n     int son; n     do { n       son = component.top(); n       color[son] = node; n       component.pop(); n       on_stack[son] = false; n     } while (son != node); n   } n } n int DFS2(int node, int cnt, bool possible_ans) { n   cnt++; n   visited[node] = true; n   for (Edge son : neighbours[node]) { n     if (color[node] != color[son.node]) { n       possible_ans = false; n       continue; n     } n     if (visited[son.node]) continue; n     cnt = DFS2(son.node, cnt, possible_ans); n     if (!cnt) possible_ans = false; n   } n   if (!possible_ans) cnt = 0; n   return cnt; n } n void solution() { n   for (int i = 1; i <= n; i++) n     if (depth[i] == -1) DFS(i, i); n   for (int i = 1; i <= n; i++) visited[i] = false; n   for (int i = 1; i <= n; i++) { n     if (visited[i]) continue; n     int cnt = DFS2(i, 0, true); n     if (cnt) n       if (minim > cnt) { n         minim = cnt; n         node_ans = i; n       } n   } n } n void DFS_print(int node) { n   cout << node <<    ; n   visited[node] = true; n   for (Edge son : neighbours[node]) { n     if (!visited[son.node]) DFS_print(son.node); n   } n } n void print() { n   cout << minim <<   n ; n   for (int i = 1; i <= n; i++) visited[i] = false; n   DFS_print(node_ans); n } n int main() { n   read(); n   solution(); n   print(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long N = 2e5 + 9; n long long wei[N], p[N], in[N], ans[N], par[N][22], mx[N][22], h[N]; n pair<long long, long long> edge[N]; n vector<pair<long long, long long> > vec, adj[N]; n priority_queue<pair<long long, long long>, vector<pair<long long, long long> >, n                greater<pair<long long, long long> > > n     hp[N]; n long long FS(long long v) { n   if (p[v] < 0) return v; n   return p[v] = FS(p[v]); n } n void US(long long u, long long v, long long id) { n   long long a = FS(u), b = FS(v); n   if (a == b) return; n   if (p[a] > p[b]) swap(a, b); n   p[a] += p[b]; n   p[b] = a; n   adj[u].push_back({v, id}); n   adj[v].push_back({u, id}); n   in[id] = 1; n } n void DFS1(long long u) { n   for (pair<long long, long long> v : adj[u]) n     if (v.first != par[u][0]) { n       h[v.first] = h[u] + 1; n       par[v.first][0] = u; n       mx[v.first][0] = wei[v.second]; n       for (long long i = 1; i < 22; i++) n         par[v.first][i] = par[par[v.first][i - 1]][i - 1]; n       for (long long i = 1; i < 22; i++) n         mx[v.first][i] = n             max(mx[v.first][i - 1], mx[par[v.first][i - 1]][i - 1]); n       DFS1(v.first); n     } n } n pair<long long, long long> LYFT(long long v, long long dis) { n   long long maxx = 0; n   for (long long i = 21; i >= 0; i--) n     if (h[v] - (1 << i) >= dis) maxx = max(maxx, mx[v][i]), v = par[v][i]; n   return {v, maxx}; n } n long long LCA(long long u, long long v) { n   if (h[u] > h[v]) swap(u, v); n   v = LYFT(v, h[u]).first; n   if (u == v) return v; n   for (long long i = 21; i >= 0; i--) n     if (par[v][i] != par[u][i]) v = par[v][i], u = par[u][i]; n   return par[u][0]; n } n void DFS2(long long u) { n   for (pair<long long, long long> v : adj[u]) n     if (v.first != par[u][0]) { n       DFS2(v.first); n       if (hp[v.first].size()) ans[v.second] = hp[v.first].top().first - 1; n       if (hp[v.first].size() > hp[u].size()) swap(hp[u], hp[v.first]); n       while (hp[v.first].size()) { n         pair<long long, long long> x = hp[v.first].top(); n         hp[v.first].pop(); n         if (x.second < h[u]) hp[u].push(x); n       } n     } n   while (hp[u].size() && hp[u].top().second >= h[u]) hp[u].pop(); n } n int32_t main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   long long n, m; n   cin >> n >> m; n   fill(p, p + N, -1); n   for (long long i = 0; i < m; i++) { n     cin >> edge[i].first >> edge[i].second >> wei[i]; n     vec.push_back({wei[i], i}); n   } n   sort(vec.begin(), vec.end()); n   for (long long i = 0; i < m; i++) n     US(edge[vec[i].second].first, edge[vec[i].second].second, vec[i].second); n   fill(ans, ans + N, -1); n   DFS1(1); n   for (long long i = 0; i < m; i++) n     if (!in[i]) { n       long long u = edge[i].first, v = edge[i].second, x = LCA(u, v); n       ans[i] = max(LYFT(u, h[x]).second, LYFT(v, h[x]).second) - 1; n       hp[u].push({wei[i], h[x]}); n       hp[v].push({wei[i], h[x]}); n     } n   DFS2(1); n   for (long long i = 0; i < m; i++) cout << ans[i] <<    ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n double EPS = 1e-9; n int INF = 1 << 30; n long long INFL = 1LL << 60; n int main() { n   int n; n   cin >> n; n   string w; n   cin >> w; n   int ile = 0; n   for (int i = 1; i < n; i += 2) { n     if (w[i] ==  a  && w[i - 1] ==  a ) { n       w[i] =  b ; n       ile++; n     } n     if (w[i] ==  b  && w[i - 1] ==  b ) { n       w[i] =  a ; n       ile++; n     } n   } n   cout << ile <<   n  << w; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   int n; n   cin >> n; n   string s; n   cin >> s; n   vector<int> stars, pacmans; n   for (int i = 0; i < n; i++) { n     if (s[i] ==  * ) n       stars.push_back(i); n     else if (s[i] ==  P ) n       pacmans.push_back(i); n   } n   stars.push_back(1e9); n   int l = 1, r = 2 * n, ok = 1; n   while (l <= r) { n     int mid = (l + r) / 2; n     int start = stars[0]; n     int curr = 0; n     bool f = true; n     for (int x : pacmans) { n       if (start < x - mid) n         f = false; n       else if (start < x) { n         curr = max(x + mid - (x - start) * 2, x + (mid - x + start) / 2); n       } else { n         curr = x + mid; n       } n       start = *upper_bound(stars.begin(), stars.end(), curr); n     } n     if (!f or start != 1e9) { n       l = mid + 1; n     } else { n       ok = mid; n       r = mid - 1; n     } n   } n   cout << ok << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n bool used[10000001]; n int n, m, second = 0, ds[1000001], ans[1000001], str[1000001], sv[1000001]; n struct o { n   int x, i, j; n }; n o d[1000001]; n vector<vector<int> > a; n bool comp(o a, o b) { n   if (a.x == b.x) return a.i < b.i; n   return a.x < b.x; n } n bool fcomp(o a, o b) { n   if (a.x == b.x) return a.j < b.j; n   return a.x < b.x; n } n int FS(int v) { n   if (ds[v] == v) return v; n   return ds[v] = FS(ds[v]); n } n void US(int a, int b) { n   a = FS(a); n   b = FS(b); n   ds[a] = ds[b]; n   return; n } n vector<pair<int, int> > tt[1000001]; n pair<int, int> dd(int v) { n   int i = (v - 1) / m; n   ++i; n   int j = v % m; n   if (j == 0) j = m; n   return {i, j}; n } n int main() { n   cin >> n >> m; n   a.resize(n + 1); n   for (int k = 1; k <= n; ++k) { n     a[k].resize(m + 1); n   } n   for (int k = 1; k <= n; ++k) n     for (int i = 1; i <= m; ++i) { n       cin >> a[k][i]; n       ds[(k - 1) * m + i] = (k - 1) * m + i; n       o w; n       w.x = a[k][i]; n       w.i = k; n       w.j = i; n       d[++second] = w; n     } n   sort(d + 1, d + second + 1, comp); n   for (int k = 1; k < second; ++k) n     if (d[k].x == d[k + 1].x) { n       if (d[k].i == d[k + 1].i) n         US((d[k].i - 1) * m + d[k].j, (d[k + 1].i - 1) * m + d[k + 1].j); n     } n   sort(d + 1, d + second + 1, fcomp); n   for (int k = 1; k < second; ++k) n     if (d[k].x == d[k + 1].x) { n       if (d[k].j == d[k + 1].j) { n         US((d[k].i - 1) * m + d[k].j, (d[k + 1].i - 1) * m + d[k + 1].j); n       } n     } n   for (int k = 1; k <= n * m; ++k) tt[FS(ds[k])].push_back(dd(k)); n   for (int k = 1; k <= second; ++k) { n     int i = d[k].i; n     int j = d[k].j; n     if (used[(i * m - m) + j]) continue; n     int cm = FS(ds[(i * m - m) + j]); n     int cnt = 0; n     for (int i = 0; i < tt[cm].size(); ++i) { n       int ii = tt[cm][i].first; n       int jj = tt[cm][i].second; n       used[ii * (m)-m + jj] = 1; n       cnt = max(str[ii], cnt); n       cnt = max(sv[jj], cnt); n     } n     for (int i = 0; i < tt[cm].size(); ++i) { n       int ii = tt[cm][i].first; n       int jj = tt[cm][i].second; n       ans[ii * (m)-m + jj] = cnt + 1; n       sv[jj] = max(cnt + 1, sv[jj]); n       str[ii] = max(cnt + 1, str[ii]); n     } n   } n   for (int k = 1; k <= n; ++k) { n     for (int i = 1; i <= m; ++i) cout << ans[(k - 1) * m + i] <<    ; n     cout << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, k; n   cin >> n >> k; n   if (((n * (n - 1)) / 2) <= k) n     cout <<  no solution ; n   else { n     while (n--) cout <<  0   << n << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n #pragma GCC optimize( O3 ) n using namespace std; n int n, m, t, k; n int d[((int)1e5 + 5)]; n int countt[105]; n int l = 1; n int r = 100; n void lbulma() { n   while (countt[l] == 0) l++; n   return; n } n void rbulma() { n   while (countt[r] == 0) r--; n   return; n } n int asal[4] = {2, 3, 5, 7}; n int main() { n   long long int sum = 0; n   cin >> n; n   for (int i = 0; i < n; i++) { n     cin >> d[i]; n     sum += d[i]; n     countt[d[i]]++; n   } n   lbulma(); n   rbulma(); n   long long minn = INT_MAX; n   for (int j = 100; j >= l; --j) { n     if (countt[j]) n       for (int i = 10; i >= 2; --i) { n         if (j % i == 0 && (j / i) + (l * i) < j + l) { n           long long int tmp = sum - ((j + l) - ((j / i) + (l * i))); n           minn = min(minn, tmp); n         } n       } n   } n   cout << min(minn, sum) << endl; n } n


#include <bits/stdc++.h> n using namespace std; n long double PI = acosl(-1); n bool compare_int(int a, int b) { return (a > b); } n bool compare_string(string a, string b) { return a.size() < b.size(); } n bool compare_pair(const pair<int, int> &a, const pair<int, int> &b) { n   if (a.second == b.second) n     return a.first > b.first; n   else n     return (a.second > b.second); n } n long long int fact(long long int n) { n   if (n == 0 || n == 1) n     return 1; n   else n     return n * fact(n - 1); n } n int32_t main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   string s, st; n   long long int x = 0, temp = 0, ans = 0; n   cin >> s; n   if (s.size() == 1) { n     cout << s << endl; n     return 0; n   } n   for (int i = 1; i < s.size(); i++) st +=  9 ; n   stringstream ss(s); n   ss >> x; n   stringstream sm(st); n   sm >> temp; n   long long int te = x - temp; n   while (te) { n     ans += (te % 10); n     te /= 10; n   } n   cout << ans + (s.size() - 1) * 9 << endl; n } n


#include <bits/stdc++.h> n using namespace std; n long long num(string str) { n   long long out = 0; n   for (int i = 0; i < str.length(); i++) { n     out *= 26; n     out += str.at(i) -  A  + 1; n   } n   return out; n } n string pb26(long long n) { n   string reverse =   ; n   while (n > 0) { n     int d = (n - 1) % 26 + 1; n     char c = d +  A  - 1; n     reverse.push_back(c); n     n -= d; n     n /= 26; n   } n   string out =   ; n   for (int i = reverse.length() - 1; i > -1; i--) out.push_back(reverse.at(i)); n   return out; n } n int main() { n   long long N; n   cin >> N; n   for (int t = 0; t < N; t++) { n     string S; n     cin >> S; n     bool rc = S.at(0) ==  R ; n     if (rc) { n       int i = 0; n       while (i < S.length() && S.at(i) !=  C ) i++; n       rc = i < S.length() && i > 1; n       if (rc) { n         for (int j = 1; j < i && rc; j++) { n           char c = S.at(j); n           if (c <  0  || c >  9 ) rc = false; n         } n         if (rc) { n           long long row = stoll(S.substr(1, i - 1)), n                     col = stoll(S.substr(i + 1)); n           cout << pb26(col) << row <<   n ; n         } n       } n     } n     if (!rc) { n       int i = 0; n       while (true) { n         char c = S.at(i); n         if (c >=  0  && c <=  9 ) break; n         i++; n       } n       string col = S.substr(0, i); n       long long row = stoll(S.substr(i)); n       cout <<  R  << row <<  C  << num(col) <<   n ; n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n bool comp(pair<double, int> &a, pair<double, int> &b) { n   if (a.first != b.first) return a.first > b.first; n   return a.second < b.second; n } n void solve() { n   int n; n   double t1, t2, k; n   cin >> n >> t1 >> t2 >> k; n   vector<pair<double, int>> p(n); n   for (int i = 0; i < (n); i++) { n     double a, b; n     cin >> a >> b; n     p[i].second = i + 1; n     p[i].first = max(a * t1 - (a * t1 * k) / 100 + b * t2, n                      b * t1 - (b * t1 * k) / 100 + a * t2); n   } n   sort(p.begin(), p.end(), comp); n   for (auto x : p) n     cout << x.second <<     << setprecision(2) << fixed << x.first <<   n ; n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   int t = 1; n   while (t--) solve(); n } n


#include <bits/stdc++.h> n using namespace std; n long long n, x; n map<long long, long long> mp; n map<long long, long long> nums; n long long grund(long long x) { n   if (x <= 1) return 0; n   if (mp[x]) return mp[x]; n   for (long long i = 31; i >= 0; i--) { n     if ((x & (1 << i))) { n       set<long long> second; n       second.clear(); n       for (long long j = i; j >= 1; j--) { n         long long y = x % (1 << j); n         for (long long k = j; k <= i; k++) { n           if (x & (1 << k)) y |= (1 << (k - j)); n         } n         second.insert(grund(y)); n       } n       for (long long j = 0; j <= 31; j++) n         if (second.find(j) == second.end()) return mp[x] = j; n     } n   } n } n signed main() { n   ios_base::sync_with_stdio(0); n   cin >> n; n   for (long long i = 1; i <= n; i++) { n     cin >> x; n     long long y = x; n     for (long long j = 2; j <= sqrt(x); j++) { n       if (!(y % j)) { n         long long cnt = 0; n         while (!(y % j)) { n           cnt++; n           y /= j; n         } n         nums[j] |= (1 << cnt); n       } n     } n     if (y > 1) nums[y] |= 2; n   } n   if (!nums.size()) { n     cout <<  Arpa ; n     return 0; n   } n   long long g = 0; n   for (map<long long, long long>::iterator it = nums.begin(); it != nums.end(); n        it++) { n     g ^= grund(it->second); n   } n   if (!g) n     cout <<  Arpa ; n   else n     cout <<  Mojtaba ; n } n


#include <bits/stdc++.h> n using namespace std; n mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); n void c_p_c() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n } n long long int gcd(long long int a, long long int b) { n   if (b == 0) return a; n   return gcd(b, a % b); n } n bool is_pallindrome(string str, long long int left, long long int right) { n   if (left > right) return true; n   return (str[left] == str[right] && is_pallindrome(str, left + 1, right - 1)); n } n long long int convertToInt(string str) { n   long long int sum = 0; n   long long int power = 1; n   for (long long int i = str.length() - 1; i >= 0; i--) { n     long long int val = str[i] -  0 ; n     long long int prod = n         ((val % 1000000007) * (power % 1000000007)) % 1000000007; n     sum = ((sum % 1000000007) + prod % 1000000007) % 1000000007; n     power *= 10; n   } n   return sum; n } n bool compare(pair<long long int, long long int> a, n              pair<long long int, long long int> b) { n   if (a.first != b.first) return a.first < b.first; n   return a.second < b.second; n } n int32_t main() { n   c_p_c(); n   long long int t; n   cin >> t; n   while (t--) { n     long long int a, b; n     cin >> a >> b; n     if (b % 2 == 0) { n       cout << a * (b / 2); n     } else { n       long long int ans = b / 2 * a; n       if (a % 2 == 0) n         ans += a / 2; n       else n         ans += (a / 2) + 1; n       cout << ans; n     } n     cout <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const unsigned long long l = 1000000007; n const int MAX_ = 2000; n int T; n void solve(int t) { n   long long n; n   cin >> n; n   vector<int> p[n]; n   for (int i = 0; i < n; i++) { n     int k; n     cin >> k; n     p[i] = vector<int>(k); n     for (int j = 0; j < k; j++) { n       cin >> p[i][j]; n     } n   } n   set<int> s; n   for (int i = 1; i <= n; i++) { n     s.insert(i); n   } n   int pos = -1; n   for (int i = 0; i < n; i++) { n     bool f = 0; n     for (int j = 0; j < p[i].size(); j++) { n       int x = p[i][j]; n       if (s.find(x) != s.end()) { n         f = 1; n         s.erase(x); n         break; n       } n     } n     if (pos < 0 && f == 0) { n       pos = i + 1; n     } n   } n   if (pos > 0) { n     cout <<  IMPROVE  << endl; n     cout << pos <<     << *s.begin() << endl; n     return; n   } n   cout <<  OPTIMAL  << endl; n } n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   int t, testCases; n   testCases = 1; n   while (testCases--) { n     t = 1; n     cin >> t; n     T = t; n     while (t--) { n       solve(t); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int M = 1e9 + 7; n vector<string> vec; n long long int sd(long long int n) { n   long long int sum = 0; n   while (n != 0) { n     sum += n % 10; n     n = n / 10; n   } n   return sum; n } n long long int nd(long long int n) { n   long long int ans = 0; n   while (n > 0) { n     n = n / 10; n     ans++; n   } n   return ans; n } n long long int power(long long int a, long long int b) { n   long long int ans = 1ll; n   while (b) { n     if (b & 1) ans = (ans * a) % 1000000007; n     a = (a * a) % 1000000007; n     b = b / 2; n   } n   return ans; n } n int main() { n   long long int n, a, b, r, c, d, i, k, j, temp, sum, ct = 0, flag = -1e13, n                                                       start = 0, next = 1; n   string st, ans; n   long long int ar[1005] = {0}; n   vector<long long int> v; n   vector<pair<long long int, long long int> > v2; n   cin >> n >> k; n   for (i = 0; i < n; i++) { n     cin >> temp; n     v.push_back(temp); n   } n   if (k > n) { n     sort(v.begin(), v.end(), greater<long long int>()); n     cout << v[0]; n     return 0; n   } n   for (i = 0; i < n; i++) { n     start = (i + 1) % n; n     ct = 0; n     if (i != 0) n       if (v[start] > v[i - 1]) { n         ct += 1; n       } n     while (ct < k) { n       if (v[start] > v[i]) break; n       ct++; n       start++; n       if (start == n) start = 0; n     } n     if (ct == k) { n       cout << v[i]; n       return 0; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 100000 + 7; n const int M = 100000 + 7; n const int inf = 1000000000 + 7; n const long long linf = 1ll * inf * (inf - 1); n const double pi = acos(-1); n const double eps = 1e-9; n const bool multipleTest = 0; n const int mod[2] = {(rand() % (inf - 1000) + 1000), n                     (rand() % (inf - 1000) + 1000)}; n int lt[2][N]; n struct Hash { n   string s; n   long long val[2][N]; n   int n; n   void init(string& s_) { n     s = s_; n     n = s.size(); n     val[0][0] = val[1][0] = 0; n     for (int i = 1; i <= n; ++i) { n       for (int j = 0; j < 2; ++j) n         val[j][i] = (1ll * val[j][i - 1] * mod[j] + s[i - 1]) % inf; n     } n   } n   long long get(int j, int u, int v) { n     long long res = val[j][v] - val[j][u - 1] * lt[j][v - u + 1]; n     res %= inf; n     if (res < 0) res += inf; n     return res; n   } n   pair<int, int> get(int u, int v) { n     return make_pair(get(0, u, v), get(1, u, v)); n   } n }; n Hash a, b; n int n, m; n string s, t; n int k; n int dp[N][31]; n int getLCP(int u, int v) { n   int lo = 1, hi = min(n - u, m - v), res = 0; n   while (lo <= hi) { n     int g = (lo + hi) >> 1; n     if (a.get(u + 1, u + g) == b.get(v + 1, v + g)) { n       res = g; n       lo = g + 1; n     } else n       hi = g - 1; n   } n   return res; n } n void solve() { n   cin >> n >> s >> m >> t >> k; n   lt[0][0] = lt[1][0] = 1; n   for (int i = 1; i < N; ++i) { n     lt[0][i] = (1ll * mod[0] * lt[0][i - 1]) % inf; n     lt[1][i] = (1ll * mod[1] * lt[1][i - 1]) % inf; n   } n   a.init(s); n   b.init(t); n   dp[0][0] = 0; n   for (int i = 0; i < n; ++i) { n     dp[i][0] = 0; n     int old = -1, oldRes = 0; n     for (int j = 0; j <= k; ++j) { n       if (j) dp[i + 1][j] = max(dp[i + 1][j], dp[i][j]); n       if (j < k) { n         int res = 0; n         if (dp[i][j] == old) { n           res = oldRes; n         } else { n           res = oldRes = getLCP(i, dp[i][j]); n           old = dp[i][j]; n         } n         if (res > 0) n           dp[i + res][j + 1] = max(dp[i + res][j + 1], dp[i][j] + res); n       } n     } n   } n   if (dp[n][k] == m) n     puts( YES ); n   else n     puts( NO ); n } n int main() { n   int Test = 1; n   if (multipleTest) { n     cin >> Test; n   } n   for (int i = 0; i < Test; ++i) { n     solve(); n   } n } n


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


#include <bits/stdc++.h> n using namespace std; n const int N = 1234567; n int n, a, b, T, used, ans; n int lft[N], rgt[N]; n char s[N]; n void fill_array(int v[]) { n   for (int i = 1; i < n; i++) { n     v[i] = v[i - 1] + a + 1 + ((s[i] ==  w ) ? b : 0); n   } n } n void solve(int lft[], int rgt[]) { n   for (int i = 1; i < n; i++) { n     int need = used + lft[i]; n     if (need <= T) { n       ans = max(ans, i + 1); n     } n     need += (i * a); n     if (need > T) { n       continue; n     } n     if (rgt[n - i - 1] + need <= T) { n       ans = n; n       break; n     } n     int pos = upper_bound(rgt, rgt + n, T - need) - rgt; n     ans = max(ans, i + 1 + pos - 1); n   } n } n int main() { n   scanf( %d %d %d %d , &n, &a, &b, &T); n   scanf( %s , s); n   fill_array(lft); n   reverse(s + 1, s + n); n   fill_array(rgt); n   reverse(s + 1, s + n); n   used = 1; n   if (s[0] ==  w ) { n     used += b; n   } n   if (used > T) { n     puts( 0 ); n     return 0; n   } n   ans = 1; n   solve(lft, rgt); n   solve(rgt, lft); n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 2e5 + 1; n long double pi = 3.141592653589793238; n int main() { n   long long t; n   cin >> t; n   while (t--) { n     long long x; n     cin >> x; n     long double ans; n     long double n = 2.0000000000000 * (long double)x; n     long double angle = n         (180.000000000000000 - 360.00000000000000 / n) / 2.00000000000000; n     angle = angle * pi / 180.000000000000; n     long double side = (long double)0.500000000000 / (long double)cosl(angle); n     if (x % 2 != 0) { n       ans = (long double)2.000000000 * side; n     } else { n       side = side * (long double)sinl(angle); n       ans = (long double)2.000000000 * side; n     } n     cout << to_string(ans) <<   n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int MX = 5000 + 9; n int t[MX]; n int n; n int counts[MX] = {0}; n int ans[MX]; n int main() { n   cin >> n; n   for (int i = 0; i < n; i++) { n     cin >> t[i]; n     t[i]--; n   } n   for (int start = 0; start < n; start++) { n     memset(counts, 0, n * sizeof(int)); n     int best = 0; n     for (int finish = start; finish < n; finish++) { n       int who = t[finish]; n       counts[who]++; n       if (counts[who] > counts[best] || n           (counts[who] == counts[best] && who < best)) n         best = who; n       ans[best]++; n     } n   } n   for (int i = 0; i < n; i++) cout << ans[i] <<    ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   vector<int> v; n   long long int n, s, x, countt = 0; n   cin >> n >> s; n   for (int i = 0; i < n; i++) { n     cin >> x; n     v.push_back(x); n   } n   sort(v.begin(), v.end()); n   int mid = n / 2; n   int m = v[mid]; n   if (m <= s) { n     for (int i = mid; i < v.size(); i++) { n       if (v[i] <= s) { n         countt += abs(v[i] - s); n       } else { n         break; n       } n     } n   } else if (s <= m) { n     for (int i = mid; i >= 0; i--) { n       if (v[i] >= s) { n         countt += abs(v[i] - s); n       } else { n         break; n       } n     } n   } n   cout << countt << endl; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 300010, LOG = 64; n int n, arr[N], nxt[N][LOG], mx, sum[N]; n long long p, ans = 0; n vector<int> seg[2][4 * N]; n bool b; n void build(int s, int e, int idx) { n   if (s == e) { n     if (sum[s] % 2 == b) seg[b][idx].push_back(sum[s]); n     return; n   } n   build(s, (s + e) / 2, idx * 2); n   build((s + e) / 2 + 1, e, idx * 2 + 1); n   for (int i = 0; i < (int)seg[b][idx * 2].size(); i++) n     seg[b][idx].push_back(seg[b][idx * 2][i]); n   for (int i = 0; i < (int)seg[b][idx * 2 + 1].size(); i++) n     seg[b][idx].push_back(seg[b][idx * 2 + 1][i]); n   sort(seg[b][idx].begin(), seg[b][idx].end()); n } n int l, r, val; n int get(int s, int e, int idx) { n   if (s > r || e < l) return 0; n   if (s >= l && e <= r) n     return seg[b][idx].end() - n            lower_bound(seg[b][idx].begin(), seg[b][idx].end(), val); n   return get(s, (s + e) / 2, idx * 2) + get((s + e) / 2 + 1, e, idx * 2 + 1); n } n int main() { n   scanf( %d , &n); n   for (int i = 0; i < LOG; i++) nxt[n][i] = n; n   for (int i = 0; i < n; i++) { n     scanf( %lld , &p); n     arr[i] = 0; n     for (int j = 62; j >= 0; j--) { n       if (((p >> j) & 1) == 1) { n         arr[i]++; n       } n     } n     sum[i] = arr[i]; n     if (i) sum[i] += sum[i - 1]; n   } n   b = 0; n   build(0, n - 1, 1); n   b = 1; n   build(0, n - 1, 1); n   for (int i = n - 1; i >= 0; i--) { n     for (int j = 0; j < LOG; j++) { n       if (arr[i] == j) n         nxt[i][j] = i; n       else n         nxt[i][j] = nxt[i + 1][j]; n     } n     mx = n; n     b = (i == 0 ? 0 : sum[i - 1] % 2); n     for (int j = LOG - 1; j >= 0; j--) { n       if (nxt[i][j] >= mx) continue; n       l = nxt[i][j]; n       r = mx - 1; n       val = 2 * j + (i == 0 ? 0 : sum[i - 1]); n       ans += get(0, n - 1, 1); n       mx = nxt[i][j]; n     } n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n bool f(int x, int n, int *pre, int p) { n   for (int j = 0; j < n; ++j) { n     if ((pre[j + x] - j) % p == 0) return false; n   } n   return true; n } n using namespace std; n int main(int argc, char **argv) { n   vector<int> ans; n   int t, n, p, a[2000], pre[5001] = {0}; n   cin >> n >> p; n   for (int i = 0; i < n; ++i) cin >> a[i]; n   sort(a, a + n); n   int mn = a[0], mx = a[n - 1]; n   t = 0; n   for (int i = 1; i <= 5000; ++i) { n     pre[i] = pre[i - 1]; n     while (t < n && a[t] <= i) ++t, ++pre[i]; n   } n   for (int i = mn; i <= 2000; ++i) { n     if (f(i, n, pre, p)) ans.push_back(i); n   } n   cout << ans.size() << endl; n   for (int i = 0; i < ans.size(); ++i) { n     cout << ans[i] << (i == ans.size() - 1 ?   n  :    ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<long long> avengers; n long long n, k, A, B; n long long rec(long long l, long long r) { n   long long i = n       lower_bound(avengers.begin(), avengers.end(), l) - avengers.begin(); n   long long j = n       upper_bound(avengers.begin(), avengers.end(), r) - avengers.begin(); n   j--; n   long long x = j - i + 1; n   long long power_consumed; n   if (x == 0) n     power_consumed = A; n   else { n     power_consumed = B * x * (r - l + 1); n   } n   if (l == r || x == 0) return power_consumed; n   long long mid = (l + r) / 2; n   long long minPower = min(power_consumed, rec(l, mid) + rec(mid + 1, r)); n   return minPower; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   cin >> n >> k >> A >> B; n   int i; n   for (i = 0; i < k; i++) { n     int val; n     cin >> val; n     avengers.push_back(val); n   } n   sort(avengers.begin(), avengers.end()); n   long long x = (long long)1 << n; n   cout << rec(1, x) << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   unsigned long long x, y, z, res1 = 0, res2 = 0; n   cin >> x >> y >> z; n   unsigned long long a = x % z; n   unsigned long long b = y % z; n   res1 = (x + y) / z; n   if (a == 0 || b == 0) n     res2 = 0; n   else { n     if (a > b) { n       if ((z - a) <= b) res2 = z - a; n     } else { n       if ((z - b) <= a) res2 = z - b; n     } n   } n   cout << res1 <<     << res2; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, ai, bi; n vector<vector<int> > adj(200009); n vector<int> vis(200009, 0); n vector<pair<int, int> > se; n void dfs(int v, int vl) { n   if (vis[v]) return; n   vis[v] = vl; n   vl = (vl == 1) ? 2 : 1; n   int len = adj[v].size(); n   for (int i = 0; i < len; i++) dfs(adj[v][i], vl); n } n int main() { n   cin >> n; n   for (int i = 0; i < n; i++) { n     cin >> ai >> bi; n     se.push_back(make_pair(ai, bi)); n     adj[ai].push_back(bi); n     adj[bi].push_back(ai); n     adj[(2 * i)].push_back(2 * i + 1); n     adj[(2 * i + 1)].push_back(2 * i); n   } n   for (int i = 1; i <= 2 * n; i++) n     if (!vis[i]) dfs(i, 1); n   for (int i = 0; i < n; i++) n     cout << vis[se[i].first] <<     << vis[se[i].second] <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m; n string s[20][20]; n string ran =  23456789TJQKA ; n string sui =  CDHS ; n set<string> st; n int j1x = -1, j1y = -1, j2x = -1, j2y = -1; n inline bool ok(int x, int y) { n   set<char> ra, su; n   for (int i = x; i < x + 3; i++) { n     for (int j = y; j < y + 3; j++) { n       ra.insert(s[i][j][0]); n       su.insert(s[i][j][1]); n     } n   } n   return ra.size() == 9 || su.size() == 1; n } n int main() { n   cin >> n >> m; n   for (int i = 0; i < 13; i++) { n     for (int j = 0; j < 4; j++) { n       string ss =     ; n       ss[0] = ran[i]; n       ss[1] = sui[j]; n       st.insert(ss); n     } n   } n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < m; j++) { n       cin >> s[i][j]; n       if (s[i][j] !=  J1  && s[i][j] !=  J2 ) n         st.erase(s[i][j]); n       else if (s[i][j] ==  J1 ) n         j1x = i, j1y = j; n       else n         j2x = i, j2y = j; n     } n   } n   for (set<string>::iterator it1 = st.begin(); it1 != st.end(); it1++) { n     if (j1x != -1) s[j1x][j1y] = (*it1); n     for (set<string>::iterator it2 = st.begin(); it2 != st.end(); it2++) { n       if (it1 == it2) continue; n       if (j2x != -1) s[j2x][j2y] = (*it2); n       vector<pair<int, int> > v; n       for (int i = 0; i <= n - 3; i++) { n         for (int j = 0; j <= m - 3; j++) n           if (ok(i, j)) v.push_back(make_pair(i + 1, j + 1)); n       } n       for (int i = 0; i < v.size(); i++) { n         for (int j = 0; j < i; j++) { n           if (abs(v[i].first - v[j].first) > 2 || n               abs(v[i].second - v[j].second) > 2) { n             cout <<  Solution exists. n ; n             if (j1x == -1 && j2x == -1) cout <<  There are no jokers. n ; n             if (j1x == -1 && j2x != -1) n               cout <<  Replace J2 with   << (*it2) <<  . n ; n             if (j1x != -1 && j2x == -1) n               cout <<  Replace J1 with   << (*it1) <<  . n ; n             if (j1x != -1 && j2x != -1) n               cout <<  Replace J1 with   << (*it1) <<   and J2 with   << (*it2) n                    <<  . n ; n             cout <<  Put the first square to (  << v[i].first <<  ,   n                  << v[i].second <<  ). n ; n             cout <<  Put the second square to (  << v[j].first <<  ,   n                  << v[j].second <<  ). n ; n             exit(0); n           } n         } n       } n     } n   } n   cout <<  No solution. ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n bool col[100010]; n int a, b, n, k; n int main() { n   cin >> n >> k >> a >> b; n   int inv(1); n   if (a < b) { n     swap(a, b); n     inv = 0; n   } n   for (int i(1); i <= k + 1; i++) { n     for (int j(i); j <= n; j += k + 1) { n       if (a) { n         col[j] = 1; n         a--; n       } n     } n   } n   int frc(0); n   for (int i(1); i <= k + 1; i++) frc += col[i]; n   for (int i(k + 2); i <= n + 1; i++) { n     if (frc == k + 1 || frc == 0) { n       cout <<  NO ; n       return 0; n     } n     frc -= col[i - k - 1]; n     frc += col[i]; n   } n   for (int i(1); i <= n; i++) { n     if (inv) n       cout << (col[i] ?  G  :  B ); n     else n       cout << (!col[i] ?  G  :  B ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int cas, b, k; n int main() { n   cas = 1; n   while (cas--) { n     scanf( %d%d , &b, &k); n     int ans = 0; n     b = b % 2; n     for (int i = 1; i <= k; i++) { n       int x; n       scanf( %d , &x); n       if (i == k) b = 1; n       ans = ans + b * x; n       ans %= 2; n     } n     if (ans % 2) n       printf( odd n ); n     else n       printf( even n ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n signed main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   string s; n   cin >> s; n   int64_t N = s.size(); n   vector<int64_t> v(N, 0); n   int64_t l = 0, r = N - 1; n   for (int64_t i = 0; i < N; ++i) { n     if (s[i] ==  l ) { n       v[r] = i + 1; n       --r; n     } else { n       v[l] = i + 1; n       ++l; n     } n   } n   for (int64_t k : v) { n     cout << k <<   n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int dp[60002][10]; n int main() { n   int n, m; n   scanf( %d%d , &n, &m); n   int a; n   for (int j = 0; j < m; j++) { n     cin >> a; n     if (!j) n       dp[0][j] = a; n     else n       dp[0][j] = dp[0][j - 1] + a; n   } n   cout << dp[0][m - 1] <<    ; n   for (int i = 1; i < n; i++) { n     for (int j = 0; j < m; j++) { n       cin >> a; n       if (!j) n         dp[i][j] = dp[i - 1][j] + a; n       else { n         if (dp[i - 1][j] >= dp[i][j - 1]) n           dp[i][j] = dp[i - 1][j] + a; n         else n           dp[i][j] = dp[i][j - 1] + a; n       } n     } n     cout << dp[i][m - 1] <<    ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int32_t main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   long long n; n   cin >> n; n   long long maxi = INT_MIN; n   while (n--) { n     long long x, y; n     cin >> x >> y; n     long long sum = x + y; n     if (sum > maxi) { n       maxi = sum; n     } n   } n   cout << maxi <<   n ; n   return 0; n } n