#include <bits/stdc++.h> n using namespace std; n int Q; n long long int N; n int cheats[20] = {0, 0, 1, 2, 3, 4, 6, 7, 8, 10, 12}; n int main() { n   scanf( %d , &Q); n   for (int qi = 0; qi < Q; qi++) { n     scanf( %lld , &N); n     if (N < 10) { n       printf( %d n , cheats[(int)N]); n       continue; n     } n     long long int low = 3, high = N / 2, med, v; n     while (low != high) { n       med = (high + low + 1) / 2; n       v = med * (med - 1) / 2; n       if (v <= (N - med)) n         low = med; n       else n         high = med - 1; n     } n     high = low * (low - 1) / 2; n     if (N - low >= high + 3) n       printf( %lld n , 2 * (N - low - 1)); n     else n       printf( %lld n , high + N - low); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int dx[] = {0, 0, 1, -1, 1, -1, 1, -1}; n int dy[] = {1, -1, 0, 0, -1, 1, 1, -1}; n long long gcd(long long a, long long b) { return !b ? a : gcd(b, a % b); } n long long lcm(long long a, long long b) { return (a / gcd(a, b)) * b; } n void PLAY() { n   cout << fixed << setprecision(10); n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n } n int num_cost[100005]; n int mp[205][100005]; n int freq_cum[100005]; n int freq[100005]; n int main() { n   PLAY(); n   int n; n   cin >> n; n   vector<int> v(n); n   set<int> st; n   for (int i = 0; i < n; i++) { n     cin >> v[i]; n     freq_cum[v[i]]++; n     freq[v[i]]++; n     st.insert(v[i]); n   } n   for (int i = 0; i < n; i++) { n     int d; n     cin >> d; n     num_cost[v[i]] += d; n     mp[d][v[i]]++; n   } n   vector<int> srtd(st.begin(), st.end()); n   sort(srtd.rbegin(), srtd.rend()); n   for (int i = 1; i < 100005; i++) { n     num_cost[i] += num_cost[i - 1]; n     freq_cum[i] += freq_cum[i - 1]; n   } n   for (int i = 1; i <= 200; i++) n     for (int j = 1; j < 100005; j++) mp[i][j] += mp[i][j - 1]; n   int res = INT_MAX; n   for (int i = 0; i < (int)srtd.size(); i++) { n     int num = srtd[i]; n     int cur_cost = num_cost[srtd[0]] - num_cost[srtd[i]]; n     int removed = freq_cum[srtd[0]] - freq_cum[num]; n     int rem = n - removed; n     int remove = max(0, rem - (freq[num] * 2 - 1)); n     for (int cost = 1; cost <= 200 && remove; cost++) { n       int avilable = mp[cost][num - 1] - mp[cost][0]; n       int cnt = min(remove, avilable); n       cur_cost += cnt * cost; n       remove -= cnt; n     } n     res = min(res, cur_cost); n   } n   cout << res << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<int> g[200005]; n int sz[200005]; n void init(int node) { n   sz[node] = 1; n   for (auto it : g[node]) { n     init(it); n     sz[node] = sz[node] + sz[it]; n   } n } n void dfs(int node) { n   for (auto it : g[node]) { n     if (!(sz[it] & 1)) dfs(it); n   } n   cout << node <<   n ; n   for (auto it : g[node]) { n     if (sz[it] & 1) dfs(it); n   } n } n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   int n, i, pi, r; n   cin >> n; n   if (!(n & 1)) { n     cout <<  NO n ; n     return 0; n   } n   for (i = 1; i <= n; i++) { n     cin >> pi; n     if (pi == 0) n       r = i; n     else n       g[pi].push_back(i); n   } n   init(r); n   cout <<  YES n ; n   dfs(r); n   return 0; n } n


#include <bits/stdc++.h> n typedef struct acv { n   int x, y; n   int l; n   int mark; n } node; n int abs(int a) { return a > 0 ? a : -a; } n int main() { n   int m, n; n   while (scanf( %d %d , &n, &m) != EOF) { n     int i, j; n     node nail[505]; n     int rod[505], sign[505] = {0}; n     for (i = 1; i <= n; i++) { n       scanf( %d %d , &nail[i].x, &nail[i].y); n       nail[i].mark = 0; n     } n     for (i = 1; i <= m; i++) scanf( %d , &rod[i]); n     int leng1, leng2; n     for (i = 1; i <= n; i++) { n       if (i == 1) { n         leng1 = nail[1].x - nail[n].x + nail[1].y - nail[n].y; n         leng2 = nail[1].x - nail[2].x + nail[1].y - nail[2].y; n       } else if (i == n) { n         leng1 = nail[n].x - nail[1].x + nail[n].y - nail[1].y; n         leng2 = nail[n].x - nail[n - 1].x + nail[n].y - nail[n - 1].y; n       } else { n         leng1 = nail[i].x - nail[i - 1].x + nail[i].y - nail[i - 1].y; n         leng2 = nail[i].x - nail[i + 1].x + nail[i].y - nail[i + 1].y; n       } n       nail[i].l = abs(leng1) + abs(leng2); n     } n     int temp[505], flag = 1; n     for (i = 1; i <= m; i++) sign[i] = 0; n     for (i = 1; i <= n; i++) { n       int k = 1; n       if (nail[i].mark == 0) { n         k = 0; n         int kr = 1; n         for (j = 1; j <= m; j++) { n           if (nail[i].l == rod[j] && sign[j] == 0) { n             kr = 0; n             sign[j] = 1; n             nail[i].mark = 1; n             temp[i] = j; n             if (i == 1) { n               nail[2].mark = 1; n               nail[n].mark = 1; n             } else if (i == n) { n               nail[1].mark = 1; n               nail[n - 1].mark = 1; n             } else { n               nail[i + 1].mark = 1; n               nail[i - 1].mark = 1; n             } n             break; n           } n         } n         if (kr) temp[i] = -1; n       } n       if (k) temp[i] = -1; n     } n     for (i = 1; i <= n; i++) n       if (nail[i].mark == 0) flag = 0; n     if (flag == 0) { n       for (i = 1; i <= n; i++) { n         nail[i].mark = 0; n         temp[i] = 0; n       } n       for (i = 1; i <= m; i++) sign[i] = 0; n       for (i = 2; i <= n; i++) { n         int k = 1; n         if (nail[i].mark == 0) { n           k = 0; n           int kr = 1; n           for (j = 1; j <= m; j++) { n             if (nail[i].l == rod[j] && sign[j] == 0) { n               kr = 0; n               sign[j] = 1; n               nail[i].mark = 1; n               temp[i] = j; n               if (i == n) { n                 nail[1].mark = 1; n                 nail[n - 1].mark = 1; n               } else { n                 nail[i + 1].mark = 1; n                 nail[i - 1].mark = 1; n               } n               break; n             } n           } n           if (kr) temp[i] = -1; n         } n         if (k) temp[i] = -1; n       } n     } n     for (i = 1; i <= n; i++) n       if (nail[i].mark == 0) flag = 0; n     if (flag == 0) n       printf( NO n ); n     else { n       printf( YES n ); n       for (i = 1; i <= n; i++) { n         printf( %d , temp[i]); n         if (i == n) n           printf(  n ); n         else n           printf(   ); n       } n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n inline long long read() { n   long long s = 0, f = 1; n   char c = getchar(); n   while (c <  0  || c >  9 ) f *= c ==  -  ? -1 : 1, c = getchar(); n   while (c >=  0  && c <=  9 ) s = s * 10 + c -  0 , c = getchar(); n   return s * f; n } n struct edge { n   int from, nxt, to; n   long long val; n }; n struct graph { n   edge e[1000005 << 1]; n   int head[2000005], ind[2000005], tot; n   inline void addedge(int u, int v, long long w) { n     ind[v]++; n     e[++tot] = (edge){u, head[u], v, w}, head[u] = tot; n   } n } G; n class SCC : public graph { n   stack<int> s; n   int pre[2000005], low[2000005], dfn; n   bool vis[2000005]; n   void dfs(int u) { n     pre[u] = low[u] = ++dfn; n     s.push(u); n     for (int i = head[u]; i; i = e[i].nxt) { n       int v = e[i].to; n       if (!pre[v]) n         dfs(v), low[u] = min(low[u], low[v]); n       else if (!scc[v]) n         low[u] = min(low[u], pre[v]); n     } n     if (low[u] == pre[u]) { n       int x; n       cnt++; n       do x = s.top(), s.pop(), scc[x] = cnt; n       while (x != u); n     } n   } n  n  public: n   int cnt, scc[2000005]; n   inline void tarjan(int n) { n     for (int i = 1; i <= n; i++) n       if (!pre[i]) dfs(i); n   } n   inline void init() { n     memset(head, 0, sizeof(head)); n     memset(pre, 0, sizeof(pre)); n     memset(low, 0, sizeof(low)); n     memset(scc, 0, sizeof(scc)); n     tot = dfn = cnt = 0; n     while (!s.empty()) s.pop(); n   } n } G1; n inline long long calc(long long x) { n   long long t = sqrt(2 * x + 0.25) - 0.5; n   return x + t * x - (t + 1) * (t + 2) * t / 6; n } n int n, m, s; n long long val[2000005], dis[2000005]; n bool vis[2000005]; n queue<int> q; n int main() { n   n = read(), m = read(); n   for (register int i = (1); i <= (m); i++) { n     int u = read(), v = read(); n     long long w = read(); n     G1.addedge(u, v, w); n   } n   G1.tarjan(n); n   s = G1.scc[read()]; n   for (register int i = (1); i <= (m); i++) { n     edge e = G1.e[i]; n     int u = G1.scc[e.from], v = G1.scc[e.to]; n     if (u == v) n       val[u] += calc(e.val); n     else n       G.addedge(u, v, e.val); n   } n   int N = G1.cnt; n   memset(dis, -1, sizeof(dis)); n   q.push(s), dis[s] = val[s], vis[s] = 1; n   while (!q.empty()) { n     int u = q.front(); n     q.pop(); n     vis[u] = 0; n     for (int i = G.head[u]; i; i = G.e[i].nxt) { n       edge e = G.e[i]; n       int v = e.to; n       if (dis[v] < dis[u] + e.val + val[v]) { n         dis[v] = dis[u] + e.val + val[v]; n         if (!vis[v]) q.push(v), vis[v] = 1; n       } n     } n   } n   for (register int i = (1); i <= (N); i++) dis[0] = max(dis[0], dis[i]); n   cout << dis[0]; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n double d[100100]; n int s[1010]; n int main() { n   ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); n   int n, m, h, sum(0); n   cin >> n >> m >> h; n   h--; n   for (int i = 0; i < m; i++) { n     cin >> s[i]; n     sum += s[i]; n   } n   if (sum < n) { n     cout << -1 << endl; n     return 0; n   } n   if (s[h] == 1 || n == 1) { n     cout << 0 << endl; n     return 0; n   } n   d[0] = 1; n   for (int j = 0; j < sum - s[h]; j++) { n     for (int t = n - 1; t >= 0; t--) { n       double p1 = (double)(n - t) / (double)(sum - j - 1); n       double p2 = (double)(n - t - 1) / (double)(sum - j - 1); n       if (p2 <= 1) n         d[t] *= (1 - p2); n       else n         d[t] = 0; n       if (p1 <= 1 && t) d[t] += d[t - 1] * p1; n     } n   } n   double ans(0); n   for (int j = max(0, n - s[h]); j < n - 1; j++) { n     ans += d[j]; n   } n   cout << fixed << setprecision(10) << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int gcd(int a, int b) { n   while (a && b) a > b ? a %= b : b %= a; n   return a + b; n } n long long lcm(long long a, long long b) { return (a * b) / gcd(a, b); } n long long fast_exp(long long base, long long exp) { n   long long res = 1; n   while (exp > 0) { n     if (exp % 2 == 1) n       res = ((res % 1000000007) * (base % 1000000007)) % 1000000007; n     base = ((base % 1000000007) * (base % 1000000007)) % 1000000007; n     exp /= 2; n   } n   return res % 1000000007; n } n const int INF = INT_MAX; n const long long INFL = LLONG_MAX; n map<int, int> Z; n const int size = 3e5 + 9; n int ptr[size]; n int main() { n   std::ios::sync_with_stdio(0); n   cin.tie(0); n   int n, q; n   cin >> n >> q; n   int unread = 0; n   vector<pair<int, int> > unr; n   int cnt = 0, i = 0; n   while (q--) { n     cnt++; n     int t, x; n     cin >> t >> x; n     if (t == 1) { n       unread++; n       unr.push_back(pair<int, int>(x, cnt)); n       Z[x]++; n     } else if (t == 2) { n       unread -= Z[x]; n       Z[x] = 0; n       ptr[x] = cnt; n     } else { n       while (i < x) { n         if (unr[i].second >= ptr[unr[i].first]) { n           unread--; n           ptr[unr[i].first] = unr[i].second; n           Z[unr[i].first]--; n         } n         i++; n       } n     } n     cout << unread <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n char a[1000100], ans[3000], s[3000], ans1[3000]; n int tmp, p, i, len1, len12, len2, lens, k, o; n bool flag; n bool smaller() { n   for (int i = 1; i <= len2; i++) n     if (ans1[i] > ans[i]) n       return false; n     else if (ans1[i] < ans[i]) n       return true; n   return false; n } n bool ok1(int i) { n   for (int j = k; j >= 1; j--) { n     if (a[(j - 1) * len2 + i] != s[(j - 1) * p + tmp]) return false; n   } n   return true; n } n bool ok2(int i) { n   if (k * len2 + i > len1) return false; n   if (k * p + tmp > lens) return false; n   return (a[k * len2 + i] == s[k * p + tmp]); n } n int main() { n   gets(a + 1); n   gets(s + 1); n   scanf( %d , &len12); n   len1 = strlen(a + 1); n   lens = strlen(s + 1); n   if (len12 > len1) n     len2 = len1; n   else n     len2 = len12; n   for (i = 1; i <= len2; i++) ans[i] =  2 ; n   flag = false; n   k = len1 / len2; n   for (o = 0; o <= len2; o++) n     if ((lens - o) % k == 0 && o <= (lens - o) / k) { n       for (i = 1; i <= len2; i++) ans1[i] =  0 ; n       p = (lens - o) / k, tmp = p; n       for (i = len2; i >= 1 && tmp > 0; i--) n         if (tmp > o) { n           if (ok1(i) && k * len2 + i > len1) { n             ans1[i] =  1 ; n             tmp--; n           } n         } else if (ok2(i) && ok1(i)) { n           ans1[i] =  1 ; n           tmp--; n         } n       if (tmp == 0) { n         if (smaller()) { n           for (i = 1; i <= len2; i++) ans[i] = ans1[i]; n           flag = true; n         } n       } n     } n   if (flag) { n     for (i = 1; i <= len2; i++) printf( %c , ans[i]); n     for (i = len2 + 1; i <= len12; i++) printf( 0 ); n   } else n     printf( 0 n ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios::sync_with_stdio(false); n   cin.tie(nullptr); n   int N, M; n   cin >> N >> M; n   vector<pair<string, int> > A(N); n   for (int i = 0; i < N; i++) { n     string S; n     cin >> S; n     for (int j = 1; j < S.size(); j += 2) S[j] =  Z  - (S[j] -  A ); n     A[i] = make_pair(S, i + 1); n   } n   sort(A.begin(), A.end()); n   for (int i = 0; i < N; i++) cout << A[i].second << (i + 1 == N ?   n  :    ); n } n


#include <bits/stdc++.h> n using namespace std; n #define ll long long n const int N = 3e5+10, oo = 1e9+10; n void solve() { n  int n; n  cin >> n; n  vector<ll> a(n+1); n  ll ans = 0; n  vector<ll> answers; n  answers.reserve(n-1); n  for(int i = 1; i <= n; ++i) { n   cin >> a[i]; n   ans += a[i]; n  } n  answers.push_back(ans); n  vector<vector<ll>> edges(n+1); n  vector<ll> deg(n+1); n  for(int i = 0; i < n - 1; ++i) { n   ll u, v; n   cin >> u >> v; n   ++deg[u]; n   ++deg[v]; n  } n  vector<ll> can_take; n  for(int i = 1; i <= n; ++i) { n   for(int x = 0; x < deg[i] - 1; ++x) { n    can_take.push_back(a[i]); n   } n  } n  sort(can_take.begin(), can_take.end()); n  while(answers.size() < n - 1) { n   answers.push_back(can_take.back() + answers.back()); n   can_take.pop_back(); n  } n  assert(answers.size() == n - 1); n  for(ll x : answers) { n   cout << x <<    ; n  } n  cout <<   n ; n } n int main() { n  ios::sync_with_stdio(0); cin.tie(0); n  int T = 1; n  cin >> T; n  while(T--) { n   solve(); n  } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   int t; n   cin >> t; n   while (t--) { n     long long n, one = 0, z = 0; n     string s, s1; n     cin >> n >> s; n     if (n == 1) { n       cout << s << endl; n       continue; n     } n     for (long long i = 0; i < s.length(); i++) { n       if (s[i] ==  1 ) n         one++; n       else { n         z++; n       } n     } n     if (z > one) { n       for (int i = 0; i < n; i++) { n         s1 +=  0 ; n       } n     } else { n       for (int i = 0; i < n; i++) { n         s1 +=  1 ; n       } n     } n     cout << s1 << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n vector<vector<int> > graph; n vector<int> AL; n vector<int> visited; n void dfs(int u) { n   visited[u] = 1; n   for (auto v : graph[u]) { n     if (!visited[v]) { n       dfs(v); n     } n   } n } n int main() { n   ios::sync_with_stdio(false); n   cin.tie(NULL); n   int tc; n   cin >> tc; n   while (tc--) { n     int n; n     cin >> n; n     string a, b; n     cin >> a >> b; n     AL.assign(20, 0); n     graph.assign(20, vector<int>()); n     visited.assign(20, 0); n     for (int i = 0; i < n; i++) { n       if (a[i] != b[i]) { n         AL[a[i] -  a ] |= 1 << (b[i] -  a ); n         graph[a[i] -  a ].push_back(b[i] -  a ); n         graph[b[i] -  a ].push_back(a[i] -  a ); n       } n     } n     int numCC = 0; n     for (int i = 0; i < 20; i++) { n       if (!visited[i]) { n         dfs(i); n         numCC++; n       } n     } n     int ans = 0; n     vector<bool> c(1 << 20); n     c[0] = true; n     for (int mask = 0; mask < (1 << 20); mask++) { n       if (c[mask]) { n         ans = max(ans, __builtin_popcount(mask)); n         for (int i = 0; i < 20; i++) { n           if (((~mask >> i) & 1) && (AL[i] & mask) == 0) { n             c[mask | (1 << i)] = true; n           } n         } n       } n     } n     cout << 2 * 20 - ans - numCC << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n #pragma GCC optimize( unroll-loops,no-stack-protector ) n #pragma GCC target( sse,sse2,ssse3,sse4,popcnt,abm,mmx,avx,tune=native ) n using namespace std; n const int MOD = 1e9 + 7; n const int INF32 = 1 << 30; n const long long INF64 = 1LL << 60; n const long double pi = acos(-1); n long long gcd(long long a, long long b) { return (!b ? a : gcd(b, a % b)); } n long long lcm(long long a, long long b) { return a * b / gcd(a, b); } n long long mul(long long a, long long b) { return a * b % MOD; } n long long modpow(long long b, long long i) { n   long long s = 1; n   while (i) { n     if (i % 2) s = (s * b) % MOD; n     b = (b * b) % MOD; n     i /= 2; n   } n   return s; n } n long long inv(long long a) { return modpow(a, MOD - 2); } n void solve() { n   int xa, ya, xb, yb; n   cin >> xa >> ya >> xb >> yb; n   cout << (xa + ya <= max(xb, yb) || (xa <= xb && ya <= yb) ?  Polycarp  n                                                             :  Vasiliy ); n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = (int)1e6 + 10; n const int mod = (int)1e9 + 7; n const int inf = (1 << 30) - 1; n int n, m, q; n int a[maxn]; n int good[maxn]; n vector<int> g[maxn]; n int p[maxn]; n int dx[] = {-1, 1, 0, 0}; n int dy[] = {0, 0, -1, 1}; n char dir[] = { U ,  D ,  L ,  R }; n int u[maxn]; n int T; n int b[maxn]; n char c[maxn]; n bool dfs(int v) { n   if (u[v] == T) return false; n   u[v] = T; n   for (int to : g[v]) { n     if (p[to] == -1) { n       p[to] = v; n       p[v] = to; n       return true; n     } n   } n   for (int to : g[v]) { n     if (good[p[to]] > 0) { n       p[p[to]] = -1; n       p[to] = v; n       p[v] = to; n       return true; n     } n   } n   for (int to : g[v]) { n     if (dfs(p[to])) { n       p[to] = v; n       p[v] = to; n       return true; n     } n   } n   return false; n } n void solve() { n   cin >> n >> m; n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < m; j++) { n       cin >> a[i * m + j]; n       good[i * m + j] = 0; n       g[i * m + j].clear(); n       p[i * m + j] = -1; n       u[i * m + j] = 0; n     } n   } n   T = 0; n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < m; j++) { n       for (int k = 0; k < 4; k++) { n         int x = i + dx[k]; n         int y = j + dy[k]; n         if (x >= 0 && x < n && y >= 0 && y < m) { n           if (a[i * m + j] > a[x * m + y]) { n             good[i * m + j] = 1; n           } else if (a[i * m + j] == a[x * m + y]) { n             g[i * m + j].emplace_back(x * m + y); n           } n         } n       } n     } n   } n   vector<int> li; n   for (int i = 0; i < n * m; i++) { n     if (good[i] == 0) { n       li.emplace_back(i); n     } n   } n   while (true) { n     int ok = false; n     T++; n     for (int i = 0; i < li.size(); i++) { n       if (p[li[i]] == -1 && dfs(li[i])) { n         ok = true; n       } n     } n     if (!ok) break; n   } n   for (int i = 0; i < n * m; i++) { n     if (good[i]) continue; n     if (p[i] == -1) { n       cout <<  NO n ; n       return; n     } n   } n   cout <<  YES n ; n   for (int i = 0; i < n * m; i++) { n     if (p[i] == -1) { n       for (int k = 0; k < 4; k++) { n         int x = i / m + dx[k]; n         int y = i % m + dy[k]; n         if (x >= 0 && x < n && y >= 0 && y < m) { n           if (a[i] > a[x * m + y]) { n             b[i] = a[i] - a[x * m + y]; n             c[i] = dir[k]; n           } n         } n       } n     } else { n       for (int k = 0; k < 4; k++) { n         int x = i / m + dx[k]; n         int y = i % m + dy[k]; n         if (x >= 0 && x < n && y >= 0 && y < m) { n           if (a[i] == a[x * m + y] && p[i] == x * m + y) { n             if (i < x * m + y) { n               b[i] = 1; n             } else { n               b[i] = a[i] - 1; n             } n             c[i] = dir[k]; n           } n         } n       } n     } n   } n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < m; j++) { n       cout << b[i * m + j] <<    ; n     } n     cout <<   n ; n   } n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < m; j++) { n       cout << c[i * m + j] <<    ; n     } n     cout <<   n ; n   } n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(); n   cout.tie(); n   int t = 1; n   cin >> t; n   for (int i = 1; i <= t; i++) { n     solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int const N = 400010; n int const M = 1001000; n int n, m; n struct edges { n   int u, next; n } e[M]; n int p[N], idx; n void addedge(int u, int v) { e[idx].u = v, e[idx].next = p[u], p[u] = idx++; } n void init() { n   idx = 0; n   memset(p, (0xff), sizeof p); n } n int key[N]; n int sz[N]; n int dep[N]; n void dfs(int u, int d, int pre = -1) { n   sz[u] = 1; n   dep[u] = d; n   for (int i = p[u]; ~i; i = e[i].next) { n     int v = e[i].u; n     if (v != pre) { n       dfs(v, d + 1, u); n       sz[u] += sz[v]; n     } n   } n   for (int i = p[u]; ~i; i = e[i].next) { n     int v = e[i].u; n     if (v != pre) { n       if (sz[v] + sz[v] > n) key[u] = v; n     } n   } n   if (pre != -1 && sz[u] + sz[u] < n) key[u] = pre; n } n int dp[N]; n int dpu[N]; n void dfs1(int u, int pre = -1) { n   for (int i = p[u]; ~i; i = e[i].next) { n     int v = e[i].u; n     if (v != pre) { n       dfs1(v, u); n       dp[u] = max(dp[u], dp[v]); n     } n   } n   if (sz[u] + sz[u] <= n) dp[u] = sz[u]; n } n void dfs2(int u, int pre = -1) { n   int m1 = dpu[u], m2 = 0; n   for (int i = p[u]; ~i; i = e[i].next) { n     int v = e[i].u; n     if (v != pre) { n       dp[u] = max(dp[u], dp[v]); n       if (dp[v] >= m1) { n         m2 = m1; n         m1 = dp[v]; n       } else if (dp[v] >= m2) { n         m2 = dp[v]; n       } n     } n   } n   for (int i = p[u]; ~i; i = e[i].next) { n     int v = e[i].u; n     if (v != pre) { n       int rem = n - sz[v]; n       if (rem + rem <= n) n         dpu[v] = rem; n       else { n         if (dp[v] == m1) n           dpu[v] = max(dpu[v], m2); n         else n           dpu[v] = max(dpu[v], m1); n       } n       dfs2(v, u); n     } n   } n } n int main() { n   scanf( %d , &n); n   init(); n   for (int i = 0; i < (n - 1); ++i) { n     int a, b; n     scanf( %d%d , &a, &b); n     addedge(a, b); n     addedge(b, a); n   } n   dfs(1, 0); n   dfs1(1); n   dfs2(1); n   for (int i = 1; i <= (n); ++i) { n     if (key[i] == 0) n       printf( 1  ); n     else { n       bool ok = 0; n       int u = key[i]; n       if (dep[u] > dep[i]) { n         int rem = sz[u] - dp[u]; n         if (rem + rem <= n) ok = 1; n       } else { n         int t = dpu[i]; n         int rem = n - sz[i]; n         if ((rem - t) * 2 <= n) ok = 1; n       } n       if (ok) n         printf( 1  ); n       else n         printf( 0  ); n     } n   } n   puts(  ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 5010; n struct SA { n   bool _t[N * 2]; n   int _s[N * 2], _sa[N * 2], _c[N * 2], x[N], _p[N], _q[N * 2], hei[N], r[N]; n   int operator[](int i) { return _sa[i]; } n   void build(int *s, int n, int m) { n     memcpy(_s, s, sizeof(int) * n); n     sais(_s, _sa, _p, _q, _t, _c, n, m); n     mkhei(n); n   } n   void mkhei(int n) { n     for (int i = 0; i < int(n); i++) r[_sa[i]] = i; n     hei[0] = 0; n     for (int i = 0; i < int(n); i++) n       if (r[i]) { n         int ans = i > 0 ? max(hei[r[i - 1]] - 1, 0) : 0; n         while (_s[i + ans] == _s[_sa[r[i] - 1] + ans]) ans++; n         hei[r[i]] = ans; n       } n   } n   void sais(int *s, int *sa, int *p, int *q, bool *t, int *c, int n, int z) { n     bool uniq = t[n - 1] = true, neq; n     int nn = 0, nmxz = -1, *nsa = sa + n, *ns = s + n, lst = -1; n     memset((c), 0, z * sizeof(*(c))); n     for (int i = 0; i < int(n); i++) uniq &= ++c[s[i]] < 2; n     for (int i = 0; i < int(z - 1); i++) c[i + 1] += c[i]; n     if (uniq) { n       for (int i = 0; i < int(n); i++) sa[--c[s[i]]] = i; n       return; n     } n     for (int i = n - 2; i >= 0; i--) n       t[i] = (s[i] == s[i + 1] ? t[i + 1] : s[i] < s[i + 1]); n     memset((sa), 0, n * sizeof(*(sa))); n     memcpy(x, c, sizeof(int) * z); n     for (int i = (1); i <= int(n - 1); i++) n       if (t[i] && !t[i - 1]) sa[--x[s[i]]] = p[q[i] = nn++] = i; n     memcpy(x + 1, c, sizeof(int) * (z - 1)); n     for (int i = 0; i < int(n); i++) n       if (sa[i] && !t[sa[i] - 1]) sa[x[s[sa[i] - 1]]++] = sa[i] - 1; n     memcpy(x, c, sizeof(int) * z); n     for (int i = n - 1; i >= 0; i--) n       if (sa[i] && t[sa[i] - 1]) sa[--x[s[sa[i] - 1]]] = sa[i] - 1; n     ; n     for (int i = 0; i < int(n); i++) n       if (sa[i] && t[sa[i]] && !t[sa[i] - 1]) { n         neq = lst < 0 || memcmp(s + sa[i], s + lst, n                                 (p[q[sa[i]] + 1] - sa[i]) * sizeof(int)); n         ns[q[lst = sa[i]]] = nmxz += neq; n       } n     sais(ns, nsa, p + nn, q + n, t + n, c + z, nn, nmxz + 1); n     memset((sa), 0, n * sizeof(*(sa))); n     memcpy(x, c, sizeof(int) * z); n     for (int i = nn - 1; i >= 0; i--) sa[--x[s[p[nsa[i]]]]] = p[nsa[i]]; n     memcpy(x + 1, c, sizeof(int) * (z - 1)); n     for (int i = 0; i < int(n); i++) n       if (sa[i] && !t[sa[i] - 1]) sa[x[s[sa[i] - 1]]++] = sa[i] - 1; n     memcpy(x, c, sizeof(int) * z); n     for (int i = n - 1; i >= 0; i--) n       if (sa[i] && t[sa[i] - 1]) sa[--x[s[sa[i] - 1]]] = sa[i] - 1; n     ; n   } n } sa; n int H[N], SA[N]; n void suffix_array(int *ip, int len) { n   ip[len++] = 0; n   sa.build(ip, len, 128); n   for (int i = 0; i < len; i++) { n     H[i] = sa.hei[i + 1]; n     SA[i] = sa._sa[i + 1]; n   } n } n int n, k, v[N]; n char c[N]; n void init() { n   scanf( %s , c); n   scanf( %d , &k); n   n = strlen(c); n   for (int i = 0; i < n; i++) v[i] = c[i] -  a  + 1; n   v[n] = 0; n   suffix_array(v, n); n } n bool gt[N][N], pp[N][N]; n bool go(int l, int r) { n   if (l > r) return true; n   if (gt[l][r]) return pp[l][r]; n   gt[l][r] = true; n   if (c[l] != c[r]) return false; n   return pp[l][r] = go(l + 2, r - 2); n } n void solve() { n   for (int i = 0; i < n; i++) n     for (int j = i; j < n; j++) go(i, j); n   for (int i = 0; i < n; i++) n     for (int j = H[i]; SA[i] + j < n; j++) { n       if (!pp[SA[i]][SA[i] + j]) continue; n       int tlen = j + 1; n       for (int ii = i; ii < n; ii++) { n         if (ii > i) tlen = min(tlen, H[ii]); n         if (tlen < j + 1) break; n         k--; n         if (k == 0) { n           for (int u = SA[i]; u <= SA[i] + j; u++) putchar(c[u]); n           puts(  ); n           exit(0); n         } n       } n     } n } n int main() { n   init(); n   solve(); 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 t; n   cin >> t; n   for (int i = 0; i < t; i++) { n     long long n, k; n     cin >> n >> k; n     if (n < k * k) { n       cout <<  NO  <<   n ; n     } else if (n % 2 != k % 2) { n       cout <<  NO  <<   n ; n     } else { n       cout <<  YES  <<   n ; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n char save[550][550]; n int n, m; n bool vis[550][550]; n int dir[4][2] = {-1, 0, 0, 1, 1, 0, 0, -1}; n int ans; n bool iscan(int x, int y) { n   if (x < 1 || x > n || y < 1 || y > m) return false; n   return true; n } n struct Rec { n   char k; n   int x, y; n } rec[1100000]; n void dfs(int x, int y) { n   int xx, yy; n   for (int i = 0; i < 4; i++) { n     xx = x + dir[i][0], yy = y + dir[i][1]; n     if (iscan(xx, yy)) { n       if (save[xx][yy] !=  . ) continue; n       if (vis[xx][yy]) continue; n       vis[xx][yy] = true; n       ans++; n       rec[ans].k =  B , rec[ans].x = xx, rec[ans].y = yy; n       dfs(xx, yy); n       ans++; n       rec[ans].k =  D , rec[ans].x = xx, rec[ans].y = yy; n       ans++; n       rec[ans].k =  R , rec[ans].x = xx, rec[ans].y = yy; n     } n   } n } n int main() { n   while (scanf( %d%d , &n, &m) != EOF) { n     for (int i = 1; i <= n; i++) scanf( %s , save[i] + 1); n     memset(vis, false, sizeof(vis)); n     ans = 0; n     for (int i = 1; i <= n; i++) n       for (int j = 1; j <= m; j++) { n         if (save[i][j] ==  .  && !vis[i][j]) { n           vis[i][j] = true; n           ans++; n           rec[ans].k =  B , rec[ans].x = i, rec[ans].y = j; n           dfs(i, j); n         } n       } n     printf( %d n , ans); n     for (int i = 1; i <= ans; i++) n       printf( %c %d %d n , rec[i].k, rec[i].x, rec[i].y); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int k, i, j, t; n   char c; n   string s; n   cin >> k; n   for (i = 1; i < k; i++) { n     for (j = 1; j < k; j++) { n       if (j > 1) { n         cout <<    ; n       } n       t = i * j; n       s =   ; n       while (t > 0) { n         c = t % k +  0 ; n         s = c + s; n         t /= k; n       } n       cout << s; n     } n     cout <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int mxn = 2000; n int x[mxn]; n int main() { n   int n, a, b; n   cin >> n >> a >> b; n   for (int(i) = (0); (i) < (n); (i)++) cin >> x[i]; n   sort(x, x + n); n   cout << x[n - a] - x[n - a - 1]; n } n


#include <bits/stdc++.h> n using namespace std; n const int INF = 0x3f3f3f3f; n const long long LINF = 0x3f3f3f3f3f3f3f3f; n const double EPS = 1e-9; n const int maxn = (int)1e5 + 9; n int n, q, t[maxn], k[maxn], d[maxn]; n int x[150]; n int ans[maxn]; n void Solve() { n   while (cin >> n >> q) { n     for (int i = (1); i <= (q); i++) cin >> t[i] >> k[i] >> d[i]; n     memset(ans, (-1), sizeof(ans)); n     for (int i = (1); i <= (q); i++) { n       int f = 0, nt = 0; n       for (int j = (1); j <= (n); j++) { n         if (x[j] <= t[i]) { n           nt++; n         } n         if (nt == k[i]) { n           f = 1; n           break; n         } n       } n       if (f) { n         int pp = 0, nt = 0; n         for (int j = (1); j <= (n); j++) { n           if (x[j] <= t[i]) { n             x[j] = t[i] + d[i]; n             nt++; n             pp += j; n           } n           if (nt == k[i]) break; n         } n         ans[i] = pp; n       } n     } n     for (int i = (1); i <= (q); i++) { n       cout << (ans[i]) << endl; n       ; n     } n   } n } n int main() { n   iostream::sync_with_stdio(false), cin.tie(0), cout.tie(0); n   Solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int t; n   cin >> t; n   while (t--) { n     string s; n     cin >> s; n     sort(s.begin(), s.end()); n     if (s[0] == s[s.size() - 1]) n       cout << -1 <<   n ; n     else n       cout << s <<   n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int dfs(const vector<vector<array<int, 3>>>& edges, vector<int>& ans, n         vector<int>& s, vector<char>& used, int v) { n   used[v] = true; n   int sum = 0; n   for (auto& w : edges[v]) { n     if (!used[w[0]]) { n       int d = dfs(edges, ans, s, used, w[0]); n       ans[w[1]] = -d * w[2]; n       sum += d; n     } n   } n   return sum - s[v]; n } n int main() { n   std::ios::sync_with_stdio(false); n   int n, m; n   cin >> n; n   vector<int> s(n); n   for (int i = 0; i < n; ++i) { n     cin >> s[i]; n   } n   cin >> m; n   if (n == 1) { n     cout << (s[0] == 0 ?  Possible  :  Impossible ); n     return 0; n   } n   vector<vector<array<int, 3>>> edges(n); n   vector<int> ans(m); n   for (int i = 0; i < m; ++i) { n     int x, y; n     cin >> x >> y; n     --x; n     --y; n     edges[x].push_back({y, i, 1}); n     edges[y].push_back({x, i, -1}); n   } n   vector<char> used(n); n   if (dfs(edges, ans, s, used, 0) == 0) { n     cout <<  Possible n ; n     for (int i : ans) { n       cout << i << endl; n     } n   } else { n     cout <<  Impossible ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int gcd(int p, int q) { return q == 0 ? p : gcd(q, p % q); } n int main() { n   int n, num[35][35], sumh[35], suml[35]; n   cin >> n; n   memset(sumh, 0, sizeof(sumh)); n   memset(suml, 0, sizeof(suml)); n   for (int i = 1; i <= n; i++) { n     for (int j = 1; j <= n; j++) { n       cin >> num[i][j]; n     } n   } n   for (int i = 1; i <= n; i++) { n     for (int j = 1; j <= n; j++) { n       sumh[i] += num[i][j]; n       suml[i] += num[j][i]; n     } n   } n   int ans = 0; n   for (int i = 1; i <= n; i++) { n     for (int j = 1; j <= n; j++) { n       if (suml[j] > sumh[i]) { n         ans++; n       } n     } n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m, sx, sy, dx, dy; n string s[1022]; n bool vis[1002][1002][5][5]; n bool f(int i, int j, int dir, int steps) { n   if (i < 0 || j < 0 || i == n || j == m || steps > 2 || s[i][j] ==  *  || n       (dir != -1 && vis[i][j][dir][steps])) n     return false; n   if (dir != -1) vis[i][j][dir][steps] = 1; n   if (i == dx && j == dy && steps <= 2) return true; n   if (dir == -1) { n     return f(i + 1, j, 2, steps) || f(i - 1, j, 0, steps) || n            f(i, j + 1, 1, steps) || f(i, j - 1, 3, steps); n   } else { n     if (dir == 0) n       return f(i + 1, j, 2, steps + 1) || f(i - 1, j, 0, steps) || n              f(i, j + 1, 1, steps + 1) || f(i, j - 1, 3, steps + 1); n     if (dir == 1) n       return f(i + 1, j, 2, steps + 1) || f(i - 1, j, 0, steps + 1) || n              f(i, j + 1, 1, steps) || f(i, j - 1, 3, steps + 1); n     if (dir == 2) n       return f(i + 1, j, 2, steps) || f(i - 1, j, 0, steps + 1) || n              f(i, j + 1, 1, steps + 1) || f(i, j - 1, 3, steps + 1); n     if (dir == 3) n       return f(i + 1, j, 2, steps + 1) || f(i - 1, j, 0, steps) || n              f(i, j + 1, 1, steps + 1) || f(i, j - 1, 3, steps); n   } n   return false; n } n int main() { n   cin >> n >> m; n   for (int i = 0; i < n; i++) { n     cin >> s[i]; n     for (int j = 0; j < s[i].size(); j++) { n       if (s[i][j] ==  S ) { n         sx = i; n         sy = j; n       } n       if (s[i][j] ==  T ) { n         dx = i; n         dy = j; n       } n     } n   } n   memset(vis, 0, sizeof(vis)); n   if (f(sx, sy, -1, 0)) n     cout <<  YES n ; n   else n     cout <<  NO n ; n } n


#include <bits/stdc++.h> n using namespace std; n inline int readint() { n   int a; n   cin >> a; n   return a; n } n int toInt(string &s) { n   const static int Mod = 998244353; n   const static long long bas = 29; n   int res = 0; n   for (int i = 0, n = s.length(); i < n; ++i) n     res = (res * bas + s[i] -  a  + 1) % Mod; n   return res; n } n const int MaxN = 100005; n vector<int> v[MaxN]; n map<int, int> *dp[MaxN]; n int siz[MaxN], son[MaxN]; n void build(int x) { n   siz[x] = 1, son[x] = x; n   for (auto y : v[x]) { n     dp[y] = dp[x] + 1, build(y); n     if (siz[y] >= siz[son[x]]) son[x] = y; n     siz[x] += siz[y]; n   } n } n int val[MaxN]; n void calc(int x, bool f) { n   if (f) n     dp[x][0][val[x]]++; n   else { n     dp[x][0][val[x]]--; n     if (dp[x][0][val[x]] == 0) dp[x][0].erase(val[x]); n   } n   for (auto y : v[x]) calc(y, f); n } n vector<pair<int, int> > query[MaxN]; n int ans[MaxN]; n void dfs(int x, bool f) { n   for (auto y : v[x]) n     if (y != son[x]) dfs(y, false); n   if (son[x] != x) dfs(son[x], true); n   for (auto y : v[x]) n     if (y != son[x]) calc(y, true); n   dp[x][0][val[x]]++; n   for (auto t : query[x]) { n     int dep = t.first; n     int id = t.second; n     ans[id] = dp[x][dep].size(); n   } n   if (not f) calc(x, false); n } n map<int, int> cnt[MaxN]; n bool root[MaxN]; n int main() { n   int n = readint(); n   string s; n   int fa; n   for (int i = 1; i <= n; ++i) { n     cin >> s >> fa; n     if (!fa) n       root[i] = true; n     else n       v[fa].push_back(i); n     val[i] = toInt(s); n   } n   for (int i = 1; i <= n; ++i) n     if (root[i]) dp[i] = cnt, build(i); n   int T = readint(); n   pair<int, int> p; n   for (int i = 1, a, b; i <= T; ++i) { n     a = readint(); n     b = readint(); n     if (dp[a] + b >= cnt + MaxN) continue; n     p.first = b; n     p.second = i; n     query[a].push_back(p); n   } n   for (int i = 1; i <= n; ++i) n     if (root[i]) dfs(i, false); n   for (int i = 1; i <= T; ++i) printf( %d n , ans[i]); n   return 0; n } n


#include <bits/stdc++.h> n int main() { n   int n, m, count, x; n   while (scanf( %d %d , &n, &m) == 2) { n     x = 0; n     count = 0; n     while (n != 0) { n       count++; n       if (count % m == 0) x = 1; n       n = (n - 1) + x; n       x = 0; n     } n     printf( %d n , count); n   } n   return 0; n } n


#include <bits/stdc++.h> n #pragma comment(linker,  /STACK:100000000 ) n #pragma GCC optimize( O3 ) n using namespace std; n const long long N = 1e5 + 10; n const long long mod = 1e9 + 7; n const long long INF = 1e18; n const long long inf = -1e18; n const long long M = 1e6; n int n, x, y, a[5 * N]; n bool us[1000010]; n int used[1000010]; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL), cout.tie(NULL); n   cin >> n >> x >> y; n   for (int i = 1; i <= n; i++) { n     cin >> a[i]; n     used[a[i]]++; n   } n   long long ans = INF; n   for (int i = 2; i <= 1000000; i++) { n     if (us[i] == 0) { n       long long s = used[i]; n       for (int j = i + i; j <= 1000000; j += i) { n         us[j] = 1; n         s += used[j]; n       } n       if ((n - s) * min(x, y) >= ans) continue; n       long long cnt = 0; n       for (int j = 1; j <= n; j++) { n         if (a[j] % i != 0) { n           long long t = a[j] % i; n           cnt += min(1ll * x, 1ll * (i - t) * y); n         } n       } n       ans = min(ans, 1ll * cnt); n     } n   } n   cout << ans; n   cin.get(), cin.get(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void doit(long long int a[], long long int i, long long int sz) { n   long long int x; n   x = i + sz - 1; n   a[i] = x; n   for (i++; i <= x; i++) a[i] = i - 1; n   return; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   long long int n, a, b, x, y, i, z; n   cin >> n >> a >> b; n   long long int arr[n + 1]; n   i = 0; n   while (i * a <= n) { n     x = n - i * a; n     if (x % b == 0) break; n     i++; n   } n   if (i * a > n) { n     cout << -1 <<   n ; n     return 0; n   } n   y = x / b; n   x = i; n   for (i = 0; i < x; i++) doit(arr, i * a + 1, a); n   for (i = 0; i < y; i++) doit(arr, x * a + i * b + 1, b); n   for (i = 1; i <= n; i++) cout << arr[i] <<    ; n   cout <<   n ; n } n


#include <bits/stdc++.h> n int a[101], b[101]; n int main() { n   int n, i, q, x, y, k, t, s, ls; n   for (scanf( %d , &n), i = 1; i <= n; i++) { n     scanf( %d , &a[i]); n     b[a[i]] = i; n   } n   for (scanf( %d , &q), i = 0; i < q; i++) { n     scanf( %d%d%d , &k, &x, &y); n     if (k == 1) { n       for (s = 0, ls = 101, t = x; t <= y; t++) { n         if (b[t] < ls) s++; n         ls = b[t]; n       } n       printf( %d n , s); n     } else if (k == 2) { n       b[a[x]] = y; n       b[a[y]] = x; n       t = a[x]; n       a[x] = a[y]; n       a[y] = t; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1E6 + 5, K = 1E6 + 5, INF = 1E9 + 7; n int n, k, c, v, ans = 0, dsu[K]; n char s[N]; n vector<int> adj[N]; n struct node { n   int l, r, xo; n   node(int _l = 0, int _r = 0, int _xo = 0) : l(_l), r(_r), xo(_xo) {} n   int get() { return min(l, r); } n   inline void operator+=(node oth) { n     l = min(INF, l + oth.l); n     r = min(INF, r + oth.r); n   } n } val[K]; n pair<int, int> trace(int u) { n   if (dsu[u] < 0) { n     return {u, 0}; n   } else { n     pair<int, int> tmp = trace(dsu[u]); n     dsu[u] = tmp.first; n     val[u].xo ^= tmp.second; n     return {dsu[u], val[u].xo}; n   } n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(nullptr); n   cin >> n >> k >> (s + 1); n   for (int i = 1; i <= k; i++) { n     dsu[i] = -1; n     val[i] = node(1, 0, 0); n     cin >> c; n     while (c--) { n       cin >> v; n       adj[v].push_back(i); n     } n   } n   for (int i = 1; i <= n; i++) { n     int typ = (s[i] -  0 ) ^ 1; n     if (ans != -1) { n       if (adj[i].size() == 1) { n         pair<int, int> u = trace(adj[i][0]); n         ans -= val[u.first].get(); n         val[u.first] += node((u.second == typ) * INF, (u.second != typ) * INF); n         ans += val[u.first].get(); n       } else if (adj[i].size() == 2) { n         pair<int, int> u = trace(adj[i][0]); n         pair<int, int> v = trace(adj[i][1]); n         if (u.first != v.first) { n           ans -= val[u.first].get() + val[v.first].get(); n           if (dsu[u.first] > dsu[v.first]) { n             swap(u, v); n           } n           if (u.second ^ v.second ^ typ) { n             swap(val[v.first].l, val[v.first].r); n             val[v.first].xo = 1; n           } n           dsu[u.first] += dsu[v.first]; n           dsu[v.first] = u.first; n           val[u.first] += val[v.first]; n           ans += val[u.first].get(); n         } n       } n     } n     cout << ans <<   n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int mod = 1000000007; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   long long int q, n, i, j, k, t; n   long long int a[3]; n   cin >> t; n   long long int area[5]; n   while (t--) { n     long long int a, b, x, y; n     cin >> a >> b >> x >> y; n     area[1] = (b - y - 1) * a; n     area[2] = (y)*a; n     area[3] = (a - x - 1) * b; n     area[4] = (x)*b; n     long long int ans = -1; n     for (i = 1; i <= 4; i++) { n       ans = max(ans, area[i]); n     } n     cout << ans <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int rem = 10; n set<char> st; n int main() { n   string s; n   cin >> s; n   long long ans = 1; n   int zeroes = 0; n   for (int i = 0; i < s.size(); i++) { n     if (s[i] >=  0  && s[i] <=  9 ) continue; n     if (s[i] ==  ? ) { n       if (i == 0) n         ans *= 9; n       else n         zeroes += 1; n     } else { n       if (st.count(s[i])) continue; n       st.insert(s[i]); n       if (i == 0) n         ans *= 9; n       else n         ans *= rem; n       rem--; n     } n   } n   cout << ans; n   while (zeroes) { n     cout <<  0 ; n     zeroes--; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = (int)1e6 + 5; n const int MACX = (int)1e9 + 7; n const int dx[] = {1, -1, 0, 0}; n const int dy[] = {0, 0, 1, -1}; n long long n, a[N], b[N], c[N]; n multiset<long long> ms, ms2; n int main() { n   ios_base ::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   ; n   cin >> n; n   for (int i = 1; i <= n; i++) { n     cin >> a[i]; n     ms.insert(a[i]); n   } n   for (int i = 1; i < n; i++) { n     cin >> b[i]; n     ms.erase(ms.find(b[i])); n     ms2.insert(b[i]); n   } n   for (int i = 1; i < n - 1; i++) { n     cin >> c[i]; n     ms2.erase(ms2.find(c[i])); n   } n   cout << *ms.begin() <<     << *ms2.begin(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n bool debug = 0; n int n, m, k; n int dx[4] = {0, 1, 0, -1}, dy[4] = {1, 0, -1, 0}; n string direc =  RDLU ; n long long ln, lk, lm; n void etp(bool f = 0) { n   puts(f ?  YES  :  NO ); n   exit(0); n } n void addmod(int &x, int y, int mod = 1000000007) { n   assert(y >= 0); n   x += y; n   if (x >= mod) x -= mod; n   assert(x >= 0 && x < mod); n } n void et() { n   puts( -1 ); n   exit(0); n } n long long fastPow(long long x, long long y, int mod = 1000000007) { n   long long ans = 1; n   while (y > 0) { n     if (y & 1) ans = (x * ans) % mod; n     x = x * x % mod; n     y >>= 1; n   } n   return ans; n } n long long gcd1(long long x, long long y) { n   long long z = y; n   while (x % y != 0) { n     z = x % y; n     x = y; n     y = z; n   } n   return z; n } n const int B = 165; n int nxt[41005][10], cnt; n bool vis[20][41005]; n unordered_map<bitset<B>, int> id; n bitset<B> rid[41005]; n long long dp[20][10][41005]; n void init() { n   bitset<B> bs; n   bs.reset(); n   bs[0] = 1; n   id[bs] = ++cnt; n   rid[cnt] = bs; n   vis[0][1] = 1; n   for (int(i) = 1; (i) <= (int)(19); (i)++) { n     for (int u = 1; u <= cnt; u++) n       if (vis[i - 1][u]) { n         for (int(j) = 0; (j) < (int)(10); (j)++) { n           if (nxt[u][j] == 0) { n             if (j == 0) n               nxt[u][j] = u; n             else { n               bs.reset(); n               bs |= rid[u] >> j; n               bs |= rid[u] << j; n               for (int k = 1; k < j; k++) n                 if (rid[u][k]) bs[j - k] = 1; n               if (id.count(bs)) n                 nxt[u][j] = id[bs]; n               else { n                 id[bs] = ++cnt; n                 rid[cnt] = bs; n                 nxt[u][j] = cnt; n               } n             } n           } n           vis[i][nxt[u][j]] = 1; n         } n       } n   } n   for (int(i) = 1; (i) <= (int)(cnt); (i)++) { n     int j = 0; n     for (; j < 10; j++) n       if (rid[i][j] == 1) break; n     for (; j < 10; j++) dp[0][j][i] = 1; n   } n   for (int(i) = 1; (i) <= (int)(19); (i)++) { n     for (int(k) = 1; (k) <= (int)(cnt); (k)++) n       if (vis[19 - i][k]) { n         for (int(j) = 0; (j) < (int)(10); (j)++) n           for (int(l) = 0; (l) < (int)(10); (l)++) { n             dp[i][l][k] += dp[i - 1][l][nxt[k][j]]; n           } n       } n   } n } n long long cal(long long x, int k) { n   if (x == 0) return 1; n   vector<int> bs; n   while (x) { n     bs.push_back(x % 10); n     x /= 10; n   } n   long long ans = 0; n   int u = 1; n   for (int i = (int)bs.size() - 1; ~i; i--) { n     for (int j = 0; j < bs[i] + (i == 0); j++) ans += dp[i][k][nxt[u][j]]; n     u = nxt[u][bs[i]]; n   } n   return ans; n } n void fmain(int ID) { n   init(); n   scanf( %d , &n); n   for (int(i) = 1; (i) <= (int)(n); (i)++) { n     long long l, r; n     scanf( %lld%lld%d , &l, &r, &k); n     printf( %lld n , cal(r, k) - cal(l - 1, k)); n   } n } n int main() { n   int t = 1; n   for (int(i) = 1; (i) <= (int)(t); (i)++) { n     fmain(i); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long n, d, b; n long long a[500005], c[500005]; n long long x, xx; n void read(long long &x) { n   x = 0; n   long long k = 1; n   char c = getchar(); n   while ((c !=  - ) && (c <  0  || c >  9 )) c = getchar(); n   if (c ==  - ) k = -1, c = getchar(); n   while (c >=  0  && c <=  9 ) x = x * 10 + c -  0 , c = getchar(); n   x = x * k; n } n void write(long long x) { n   if (x < 0) putchar( - ), x = -x; n   if (x > 9) write(x / 10); n   putchar(x % 10 +  0 ); n } n void init() { freopen( f.in ,  r , stdin); } n long long sum(long long x, long long y) { n   if (x < 1) x = 1; n   if (y > n) y = n; n   return c[y] - c[x - 1]; n } n int main() { n   read(n), read(d), read(b); n   for (long long i = 1; i <= n; i++) { n     read(a[i]); n     c[i] = c[i - 1] + a[i]; n   } n   for (long long i = 1; i <= n / 2; i++) { n     if (sum(1, i + i * d) >= (x + 1) * b) x++; n     if (sum(n - i - i * d + 1, n) >= (xx + 1) * b) xx++; n   } n   write(max(n / 2 - x, n / 2 - xx)); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e5 + 7; n pair<int, int> a[N]; n int n; n int main() { n   ios_base::sync_with_stdio(0); n   cin >> n; n   for (int i = 0; i < n; ++i) cin >> a[i].first >> a[i].second; n   sort(a, a + n); n   int ans = 0; n   int mxb = -1; n   for (int i = 0; i < n; ++i) { n     if (a[i].second < mxb) n       ++ans; n     else n       mxb = a[i].second; n   } n   cout << ans <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<pair<int, int> > pts; n vector<pair<int, int> > rods1; n vector<pair<int, int> > rods2; n vector<int> r1; n vector<int> r2; n vector<int> rr1; n vector<int> rr2; n bool cmp(const pair<int, int> &a, const pair<int, int> &b) { n   return a.first < b.first; n } n inline int m_dist(pair<int, int> a, pair<int, int> b) { n   return abs(a.first - b.first) + abs(a.second - b.second); n } n int main() { n   ios::sync_with_stdio(0); n   int n, m; n   int a, b, r; n   cin >> n >> m; n   for (int i = 0; i < n; i++) { n     cin >> a >> b; n     pts.push_back(make_pair(a, b)); n   } n   for (int i = 0; i < m; i++) { n     cin >> r; n     rods1.push_back({r, i}); n     rods2.push_back({r, i}); n   } n   sort(rods1.begin(), rods1.end()); n   sort(rods2.begin(), rods2.end()); n   for (int i = 0; i < n - 1; i += 2) { n     r1.push_back(m_dist(pts[i], pts[i + 1]) + n                  m_dist(pts[i + 1], pts[(i + 2) % n])); n     r2.push_back(m_dist(pts[(i - 1 + n) % n], pts[i % n]) + n                  m_dist(pts[i % n], pts[(i + 1) % n])); n   } n   bool check1 = true; n   for (auto r : r1) { n     auto fr = n         lower_bound(rods1.begin(), rods1.end(), pair<int, int>(r, 0), cmp); n     if (fr == rods1.end() || r != (*fr).first) { n       check1 = false; n       break; n     } else { n       rr1.push_back((*fr).second); n       rods1.erase(fr); n     } n   } n   bool check2 = true; n   for (auto r : r2) { n     auto fr = n         lower_bound(rods2.begin(), rods2.end(), pair<int, int>(r, 0), cmp); n     if (fr == rods2.end() || r != (*fr).first) { n       check2 = false; n       break; n     } else { n       rr2.push_back((*fr).second); n       rods2.erase(fr); n     } n   } n   if (check1 || check2) { n     cout <<  YES n ; n     if (check1) { n       for (auto e : rr1) { n         cout <<  -1  <<     << e + 1 <<    ; n       } n     } else { n       for (auto e : rr2) { n         cout << e + 1 <<     <<  -1  <<    ; n       } n     } n   } else { n     cout <<  NO n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int t; n   cin >> t; n   while (t--) { n     int a, b, c = 0, e, i, n; n     cin >> n; n     vector<char> v; n     char s[n + 1]; n     cin >> s; n     for (i = 0; i < n; i++) { n       if ((s[i] -  0 ) % 2 != 0) { n         c++; n         v.push_back(s[i]); n         if (c == 2) break; n       } n     } n     if (c < 2) n       cout <<  -1 n ; n     else { n       for (i = 0; i < 2; i++) cout << v[i]; n       cout <<   n ; n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n const double eps = 1e-8; n const int inf = 0x3f3f3f3f; n const int Minf = 0x7f7f7f7f; n const long long INF = 0x3f3f3f3f3f3f3f3fll; n const long long MINF = 0x7f7f7f7f7f7f7f7fll; n const int N = 1e6 + 50; n long long val[N], fa[N]; n int pp[N], ps[N]; n int to[N][2], cnt[N * 2]; n int main() { n   long long n, k; n   scanf( %lld%lld , &n, &k); n   for (int i = (2); i <= n; ++i) n     scanf( %lld%lld , &fa[i], &val[i]), val[i] ^= val[fa[i]]; n   long long ans = 0; n   for (int j = (62); j >= 0; --j) { n     int tot = 0; n     long long num = 0; n     for (int i = (1); i <= n; ++i) { n       int d = (val[i] >> j) & 1; n       if (to[pp[i]][d] == 0) to[pp[i]][d] = ++tot; n       cnt[to[pp[i]][d]]++; n       pp[i] = to[pp[i]][d]; n     } n     for (int i = (1); i <= n; ++i) { n       int d = (val[i] >> j) & 1; n       num += cnt[to[ps[i]][d]]; n     } n     int flag = 0; n     if (num < k) k -= num, ans |= (1ll << j), flag = 1; n     for (int i = (1); i <= n; ++i) { n       int d = (val[i] >> j) & 1; n       ps[i] = to[ps[i]][d ^ flag]; n     } n     memset(to, 0, sizeof to); n     memset(cnt, 0, sizeof cnt); n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long int powr(long long int a, long long int b) { n   long long int res = 1; n   while (b) { n     if (b % 2) res = (a * res) % 1000000007; n     a = (a * a) % 1000000007; n     b /= 2; n   } n   return res; n } n void extentded_eucledian(long long int a, long long int b, long long int &ex, n                          long long int &ey) { n   if (b == 0) { n     ex = 1; n     ey = 0; n     return; n   } n   extentded_eucledian(b, a % b, ex, ey); n   swap(ex, ey); n   ey -= (a / b) * ex; n } n bool cmp(pair<pair<long long int, long long int>, long long int> &a, n          pair<pair<long long int, long long int>, long long int> &b) { n   if (a.first.first != b.first.first) return (a.first.first < b.first.first); n   return (a.first.second < b.first.second); n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   long long int tc = 1; n   cin >> tc; n   while (tc--) { n     long long int n; n     cin >> n; n     vector<pair<pair<long long int, long long int>, long long int>> vec; n     vector<long long int> ans(n); n     for (long long int i = 0; i < n; ++i) { n       long long int x, y; n       cin >> x >> y; n       vec.push_back(make_pair(make_pair(x, y), i)); n     } n     sort(vec.begin(), vec.end(), cmp); n     long long int curr = 1, maxr = vec[0].first.second, wasn1 = 1, wasn2 = 1; n     ans[vec[0].second] = 1; n     stack<long long int> s; n     for (long long int i = 1; i < n; ++i) { n       if (maxr >= vec[i].first.first) n         s.push(vec[i].second); n       else { n         while (!s.empty()) { n           long long int tmp = s.top(); n           s.pop(); n           ans[tmp] = curr; n         } n         curr = 3 - curr; n         s.push(vec[i].second); n       } n       maxr = max(maxr, vec[i].first.second); n     } n     while (!s.empty()) { n       long long int tmp = s.top(); n       s.pop(); n       ans[tmp] = curr; n     } n     for (long long int i : ans) n       if (i == 1) wasn1 = 0; n     for (long long int i : ans) n       if (i == 2) wasn2 = 0; n     if (wasn1 || wasn2) n       cout <<  -1 n ; n     else { n       for (auto lstelem : ans) cout << lstelem <<    ; n       cout <<    n ; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long int n, t; n   cin >> n >> t; n   long int arr[n + 1]; n   bool visited[n + 1]; n   for (long int i = 1; i < n; i++) { n     cin >> arr[i]; n     visited[i] = false; n   } n   long int i = 1; n   while (visited[i] == false && i != t && i != n) { n     visited[i] = true; n     i = i + arr[i]; n   } n   if (i == t) { n     cout <<  YES ; n   } else { n     cout <<  NO ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n double find(int x, int y) { return (sqrt(x * x + y * y)); } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   int r, d; n   int t; n   cin >> r >> d; n   cin >> t; n   int c = 0; n   while (t--) { n     int x, y, q; n     cin >> x >> y >> q; n     double l; n     l = find(x, y); n     int a = r - d; n     int b = a + d; n     if (l - q >= a && l + q <= r) c++; n   } n   cout << c << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 1e5; n const int MAXR = 1; n const int MAXC = 1; n const int INF = (int)1e9; n priority_queue<int, vector<int>, greater<int>> q1; n priority_queue<int, vector<int>, less<int>> q2; n int arr[MAXN]; n vector<pair<int, int>> v(MAXN); n vector<vector<int>> adjl(MAXN); n queue<int> q; n set<pair<int, int>> s; n int n; n int x4[] = {-1, 0, 1, 0}; n int y4[] = {0, 1, 0, -1}; n void createGraph() { n   while (!q.empty()) { n     int vn = q.front(); n     q.pop(); n     pair<int, int> p = v[vn]; n     int deg = p.first; n     int xs = p.second; n     if (deg == 0) continue; n     if (adjl[vn].size() == deg) continue; n     int cxs = 0; n     for (int j = 0; j < adjl[vn].size(); ++j) { n       cxs ^= adjl[vn][j]; n     } n     int ns = xs ^ cxs; n     ; n     adjl[vn].push_back(ns); n     ; n     adjl[ns].push_back(vn); n     if (v[ns].first - adjl[ns].size() == 1) { n       q.push(ns); n     } n   }; n } n void createEdgeSet() { n   for (int i = 0; i < adjl.size(); ++i) { n     for (int j = 0; j < adjl[i].size(); ++j) { n       int src = i; n       int dest = adjl[i][j]; n       pair<int, int> p1 = make_pair(src, dest); n       pair<int, int> p2 = make_pair(dest, src); n       if (s.find(p1) == s.end() && s.find(p2) == s.end()) { n         s.insert(p1); n       } n     } n   }; n } n int main(void) { n   ios::sync_with_stdio(false); n   cin >> n; n   v.resize(n); n   adjl.resize(n); n   int d, sv; n   for (int i = 0; i < n; ++i) { n     cin >> d >> sv; n     v[i] = make_pair(d, sv); n     if (d == 1) { n       q.push(i); n     } n   }; n   createGraph(); n   createEdgeSet(); n   cout << s.size() << endl; n   for (set<pair<int, int>>::iterator it = s.begin(); it != s.end(); it++) { n     pair<int, int> p = *it; n     cout << p.first <<     << p.second << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   int a, b; n   cin >> a >> b; n   if (a == b) { n     cout << (a * 10) <<     << (b * 10) + 1; n   } else if (a + 1 == b) { n     cout << (min(a, b) * 10) + 9 <<     << (max(a, b) * 10); n   } else if (a == 9 && b == 1) { n     cout << a <<     << b * 10; n   } else n     cout << -1; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n #pragma comment(linker,  /STACK:256000000 ) n struct __ { n   __() { n     ios_base::sync_with_stdio(false); n     cin.tie(NULL); n   } n } _; n template <typename I> n inline void in(I l, I r) { n   while (l != r) cin >> *l, ++l; n } n template <typename I> n inline void out(I l, I r, char D =    ) { n   if (l != r) { n     cout << *l; n     while (++l != r) cout << D << *l; n   } n   cout <<   n ; n } n struct debug_t { n   template <typename T> n   debug_t& operator<<(const T& value) { n     return *this; n   } n } debug; n namespace DSU { n vector<int> parent, rank; n void init(int n) { n   parent = vector<int>(n); n   rank = vector<int>(n, 0); n   for (int i = 0; i < (n); ++i) parent[i] = i; n } n int find(int v) { n   if (parent[v] != v) return parent[v] = find(parent[v]); n   return v; n } n int merge(int a, int b) { n   a = find(a); n   b = find(b); n   if (a == b) return a; n   if (rank[a] > rank[b]) swap(a, b); n   parent[a] = b; n   if (rank[a] == rank[b]) ++rank[b]; n   return b; n } n }  // namespace DSU n vector<int> mx_dist; n pair<int, int> find_far(vector<vector<int> >& g, int v, int p = -1) { n   pair<int, int> result = make_pair(0, v); n   for (int i = 0; i < (static_cast<int>((g[v]).size())); ++i) { n     int nxt = g[v][i]; n     if (nxt != p) { n       pair<int, int> nxt_result = find_far(g, nxt, v); n       nxt_result.first += 1; n       result = max(result, nxt_result); n     } n   } n   return result; n } n int get_color_value(vector<vector<int> >& g, int v) { n   int n = static_cast<int>((g).size()); n   pair<int, int> f; n   f = find_far(g, v); n   f = find_far(g, f.second); n   return f.first; n } n void calc_mx_dist(vector<vector<int> >& g) { n   int n = static_cast<int>((g).size()); n   vector<int> used(n, 0); n   for (int i = 0; i < (n); ++i) { n     int color = DSU::find(i); n     if (!used[color]) { n       used[color] = 1; n       mx_dist[color] = get_color_value(g, i); n     } n   } n } n void req(int x, int y) { n   x = DSU::find(x); n   y = DSU::find(y); n   if (x != y) { n     int result = (mx_dist[x] + 1) / 2 + (mx_dist[y] + 1) / 2 + 1; n     int res = DSU::merge(x, y); n     mx_dist[res] = max(result, max(mx_dist[x], mx_dist[y])); n   } n } n int main() { n   int n, m, q; n   while (cin >> n >> m >> q) { n     DSU::init(n); n     mx_dist = vector<int>(n, 0); n     vector<vector<int> > g = vector<vector<int> >(n); n     while (m--) { n       int a, b; n       cin >> a >> b; n       --a; n       --b; n       g[a].push_back(b); n       g[b].push_back(a); n       DSU::merge(a, b); n     } n     calc_mx_dist(g); n     while (q--) { n       int code, a, b; n       cin >> code >> a; n       --a; n       if (code == 1) n         cout << mx_dist[DSU::find(a)] <<   n ; n       else { n         cin >> b; n         --b; n         req(a, b); n       } n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void solve() { n   int n; n   cin >> n; n   long long coins = 0; n   vector<long long> arr(n); n   for (int i = 0; i < n; i++) cin >> arr[i]; n   int i = 0, j = 0; n   while (i < n && j < n) { n     while (i < n && arr[i] <= 0) i++; n     while (j < n && (arr[j] >= 0 || j < i)) j++; n     if (i >= n || j >= n) break; n     long long x = min(abs(arr[i]), abs(arr[j])); n     arr[i] -= x; n     arr[j] += x; n   } n   for (int i = 0; i < n; i++) { n     if (arr[i] > 0) coins += arr[i]; n   } n   cout << coins <<   n ; n } n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   int t; n   cin >> t; n   while (t--) { n     solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T> n T mabs(const T &a) { n   return a < 0 ? -a : a; n } n int n, m, p, bss; n vector<int> a, b; n map<int, int> bCounts; n vector<int> checkPos(int sp) { n   vector<int> res; n   map<int, int> aCounts; n   int wrongCounts = bss; n   for (int i = (0), ei = (m); i < ei; i++) { n     int cp = sp + i * p; n     if (cp >= n) return res; n     int aVal = a[cp]; n     int cac = aCounts[aVal]; n     int nac = cac + 1; n     if (cac == bCounts[aVal]) n       wrongCounts++; n     else if (nac == bCounts[aVal]) n       wrongCounts--; n     aCounts[aVal] = nac; n   } n   int moved = 0; n   while (1) { n     if (wrongCounts == 0) res.push_back(sp + moved * p); n     int curExc = a[sp + moved * p]; n     int curIncIdx = sp + (moved + m) * p; n     if (curIncIdx >= n) break; n     int curInc = a[curIncIdx]; n     { n       int cac = aCounts[curExc]; n       int nac = cac - 1; n       if (cac == bCounts[curExc]) n         wrongCounts++; n       else if (nac == bCounts[curExc]) n         wrongCounts--; n     } n     if (wrongCounts < 0) throw 1; n     aCounts[curExc]--; n     { n       int cac = aCounts[curInc]; n       int nac = cac + 1; n       if (cac == bCounts[curInc]) n         wrongCounts++; n       else if (nac == bCounts[curInc]) n         wrongCounts--; n     } n     if (wrongCounts < 0) throw 1; n     aCounts[curInc]++; n     moved++; n   } n   return res; n } n void run() { n   scanf( %d%d%d , &n, &m, &p); n   for (int i = (0), ei = (n); i < ei; i++) { n     int cur; n     scanf( %d , &cur); n     a.push_back(cur); n   } n   for (int i = (0), ei = (m); i < ei; i++) { n     int cur; n     scanf( %d , &cur); n     b.push_back(cur); n     bCounts[cur]++; n   } n   bss = bCounts.size(); n   vector<int> res; n   for (int i = (0), ei = (p); i < ei; i++) { n     vector<int> cur = checkPos(i); n     for (int j = (0), ej = (cur.size()); j < ej; j++) res.push_back(cur[j]); n   } n   sort((res).begin(), (res).end()); n   printf( %d n , (int)res.size()); n   for (int i = (0), ei = (res.size()); i < ei; i++) { n     printf( %d  , res[i] + 1); n   } n } n int main() { n   run(); n   return 0; n } n


#include <bits/stdc++.h> n int input[1001]; n int ans[1001][2]; n int main() { n   int n, i, answer; n   scanf( %d , &n); n   for (i = 0; i < n; i++) scanf( %d , &input[i]); n   ans[0][0] = 1; n   for (i = 1; i < n; i++) { n     if (input[i] >= input[i - 1]) n       ans[i][0] = 1 + ans[i - 1][0]; n     else n       ans[i][0] = 1; n   } n   ans[n - 1][1] = 1; n   for (i = n - 2; i >= 0; i--) { n     if (input[i] >= input[i + 1]) n       ans[i][1] = ans[i + 1][1] + 1; n     else n       ans[i][1] = 1; n   } n   answer = -1; n   for (i = 0; i < n; i++) n     answer = n         (answer > ans[i][0] + ans[i][1] ? (answer) : (ans[i][0] + ans[i][1])); n   printf( %d n , answer - 1); n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, m, count_zero = 0, count_max = 0; n   cin >> n >> m; n   int data[n][m]; n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < m; j++) { n       cin >> data[i][j]; n       count_max += data[i][j]; n       if (data[i][j] == 0) { n         count_zero++; n       } n     } n   } n   for (int i = n - 2; i >= 1; i--) { n     for (int j = m - 2; j >= 1; j--) { n       int h_d = data[i][j + 1] - data[i][j - 1], n           v_d = data[i + 1][j] - data[i - 1][j]; n       if (data[i][j] == 0 && h_d > 1 && v_d > 1) { n         data[i][j] = min(data[i + 1][j] - 1, data[i][j + 1] - 1); n         count_max += data[i][j]; n       } else if (h_d <= 1 || v_d <= 1) { n         cout <<  -1 ; n         return 0; n       } n     } n   } n   for (int i = 0; i < n; i++) { n     for (int j = 1; j < m; j++) { n       if (data[i][j] <= data[i][j - 1]) { n         cout <<  -1 ; n         return 0; n       } n     } n   } n   for (int i = 0; i < m; i++) { n     for (int j = 1; j < n; j++) { n       if (data[j][i] <= data[j - 1][i]) { n         cout <<  -1 ; n         return 0; n       } n     } n   } n   cout << count_max; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 1000; n vector<int> a[maxn]; n bool flag[maxn]; n int ans[maxn]; n int n; n bool judge(int x, int y) { n   int cnt = 0; n   memset(flag, false, sizeof(flag)); n   for (int i = 0; i < (int)a[x].size(); i++) { n     flag[a[x][i]] = true; n   } n   for (int i = 0; i < (int)a[y].size(); i++) { n     if (flag[a[y][i]] == false) return false; n   } n   return true; n } n int main() { n   while (cin >> n) { n     for (int i = 1; i <= n; i++) a[i].clear(); n     for (int i = 1; i <= n; i++) { n       int m; n       cin >> m; n       while (m--) { n         int x; n         cin >> x; n         a[i].push_back(x); n       } n     } n     for (int i = 1; i <= n; i++) ans[i] = 1; n     for (int i = 1; i <= n; i++) { n       for (int j = 1; j <= n; j++) { n         if (i == j) continue; n         if (judge(i, j)) { n           ans[i] = 0; n           break; n         } n       } n     } n     for (int i = 1; i <= n; i++) { n       if (ans[i]) n         puts( YES ); n       else n         puts( NO ); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long int n; n   cin >> n; n   int event, police = 0, ans = 0; n   for (int i = 0; i < n; i++) { n     cin >> event; n     if (event == -1) { n       if (police == 0) n         ans++; n       else n         police--; n     } else n       police += event; n   } n   cout << ans; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 5000 + 10; n int n, a[N], dp[N][N][2]; n int calc(int i, int rem, bool isLess) { n   if (rem == 0) return 0; n   if (i >= n) return 2e9; n   int &ret = dp[i][rem][isLess]; n   if (ret != -1) return ret; n   ret = 2e9; n   if (isLess) { n     if (i != n - 1) { n       int h = a[i + 1]; n       int extra = max(h - a[i] + 1, 0); n       h -= extra; n       ret = min(ret, extra + calc(i + 2, rem - 1, h < a[i + 2])); n       if (i != n - 2) { n         int h = a[i + 1]; n         int extra = max(h - a[i + 2] + 1, 0); n         h -= extra; n         ret = min(ret, extra + calc(i + 2, rem - (h < a[i]), h < a[i + 2])); n       } n       ret = min(ret, calc(i + 1, rem, a[i] < a[i + 1])); n       if (a[i] >= a[i + 1]) n         ret = min(ret, (a[i] - a[i + 1] + 1) + calc(i + 1, rem, true)); n     } else n       ret = min(ret, calc(i + 1, rem - 1, false)); n   } else { n     if (i != n - 1) { n       ret = min(ret, calc(i + 1, rem, a[i] < a[i + 1])); n       if (a[i] >= a[i + 1]) n         ret = min(ret, (a[i] - a[i + 1] + 1) + calc(i + 1, rem, true)); n     } n   } n   return ret; n } n int main() { n   scanf( %d , &n); n   for (int i = 0; i < n; ++i) scanf( %d , a + i); n   memset(dp, -1, sizeof dp); n   int k = (n + 1) / 2; n   for (int i = 1; i <= k; ++i) printf( %d  , calc(0, i, true)); n   puts(  ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long int power(long long int x, long long int y, long long int p) { n   long long int res = 1; n   x = x % p; n   while (y > 0) { n     if (y & 1) res = (res * x) % p; n     y = y >> 1; n     x = (x * x) % p; n   } n   return res; n } n long long int modInverse(long long int a, long long int m) { n   return power(a, m - 2, m); n } n long long int f(long long int y) { n   long double x = -1 + sqrt(1 + 8 * (y + 0.0)); n   long long int ret = x / 2; n   return ret; n } n vector<long long int> v[101]; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   ; n   long long int b; n   cin >> b; n   long long int cnt = 0; n   for (long long int i = 1; i * i <= b; i++) { n     if (b % i == 0) { n       if (b / i == i) n         cnt++; n       else n         cnt += 2; n     } n   } n   cout << cnt <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n int n; n int res[51][51]; n int main() { n   scanf( %d , &n); n   for (int i = 0; i < n * (n - 1) / 2 - 1; i++) { n     int a, b; n     scanf( %d%d , &a, &b); n     res[a][b] = 1; n     res[b][a] = -1; n   } n   int q1, q2; n   for (int i = 1; i < n + 1; i++) n     for (int j = i + 1; j < n + 1; j++) { n       if (res[i][j] == 0) { n         q1 = i; n         q2 = j; n       } n     } n   for (int i = 1; i < n + 1; i++) { n     if (i != q1 && i != q2 && res[q1][i] > res[q2][i]) { n       printf( %d %d , q1, q2); n       return 0; n     } n     if (i != q1 && i != q2 && res[q1][i] < res[q2][i]) { n       printf( %d %d , q2, q1); n       return 0; n     } n   } n   printf( %d %d , q2, q1); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<pair<int, int> > Div[5 * int(1e5 + 100)]; n int a[2 * int(1e5 + 100)]; n bool Have[2 * int(1e5 + 100)]; n int calc[5 * int(1e5 + 100)]; n int root[5 * int(1e5 + 100)]; n int val = 1; n void Prime(int x) { n   int u = x; n   root[x] = 1; n   for (int i = 2; i <= sqrt(x); i++) n     if (x % i == 0) { n       Div[u].push_back(pair<int, int>(i, 1)); n       while (x % i == 0) x /= i; n       root[u] *= i; n     } n   if (x > 1) { n     Div[u].push_back(pair<int, int>(x, 1)); n     root[u] *= x; n   } n } n void Generate(int x, int n, int maxnumber, int number, int pos) { n   if (number > maxnumber) { n     Div[x].push_back(pair<int, int>(val, maxnumber)); n     return; n   } n   for (int j = pos; j <= n - (maxnumber - number); j++) { n     val *= Div[x][j].first; n     Generate(x, n, maxnumber, number + 1, j + 1); n     val /= Div[x][j].first; n   } n } n int main() { n   for (int i = 2; i <= 5e5; i++) { n     Prime(i); n     if (i != root[i]) Div[i].clear(); n   } n   for (int i = 2; i <= 5e5; i++) n     if (i == root[i]) { n       int n = Div[i].size(); n       for (int j = 2; j <= n; j++) Generate(i, n - 1, j, 1, 0); n     } n   int n, q; n   cin >> n >> q; n   for (int i = 0; i < n; i++) scanf( %d , &a[i]); n   int Size = 0; n   long long ans = 0; n   for (int i = 1; i <= q; i++) { n     int x, base; n     scanf( %d , &x); n     x--; n     if (!Have[x]) { n       Have[x] = true; n       base = 1; n       ans += 1ll * Size; n       Size++; n     } else { n       Have[x] = false; n       base = -1; n       Size--; n       ans -= 1ll * Size; n     } n     for (int j = 0; j < Div[root[a[x]]].size(); j++) { n       int base2; n       if (Div[root[a[x]]][j].second % 2 == 1) n         base2 = -1; n       else n         base2 = 1; n       if (base == -1) calc[Div[root[a[x]]][j].first]--; n       ans = ans + (1ll * base * base2 * calc[Div[root[a[x]]][j].first]); n       if (base == 1) calc[Div[root[a[x]]][j].first]++; n     } n     printf( %I64d n , ans); n   } n } n


#include <bits/stdc++.h> n using namespace std; n map<float, int> mp; n float cnt[200005]; n int main() { n   int m; n   cin >> m; n   for (int i = 1; i <= m; i++) { n     char s1, s2, s3, s4; n     int a, b, c; n     cin >> s1 >> a >> s2 >> b >> s3 >> s4 >> c; n     cnt[i] = (((a + b) * 1.0) / c); n     mp[cnt[i]]++; n   } n   for (int i = 1; i <= m; i++) { n     if (i == m) n       cout << mp[cnt[i]] << endl; n     else n       cout << mp[cnt[i]] <<    ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int T, n, a[100005], b[100005], f[22], F[22], u[22], m, dp[2222222], n     cnt[2222222], C; n void ope(int x) { n   dp[x] = 0; n   for (int i = 1; i <= 20; ++i) { n     if ((1 << i) & x) { n       int y = x ^ (1 << i); n       if (dp[y] == -1) ope(y); n       if (!(y & f[i])) dp[x] |= dp[y]; n     } n   } n   if (dp[x]) C = max(C, cnt[x]); n } n void dfs(int x) { n   u[x] = 1; n   m |= (1 << x); n   for (int i = 1; i <= 20; ++i) n     if (!u[i] && (F[x] & (1 << i))) dfs(i); n } n int main() { n   for (int i = 1; i < 2222000; ++i) { n     if (i & 1) n       cnt[i] = cnt[i >> 1] + 1; n     else n       cnt[i] = cnt[i >> 1]; n   } n   scanf( %d , &T); n   while (T--) { n     scanf( %d , &n); n     for (int i = 1; i <= n; ++i) { n       char c; n       scanf(  %c , &c); n       a[i] = c - 96; n     } n     for (int i = 1; i <= n; ++i) { n       char c; n       scanf(  %c , &c); n       b[i] = c - 96; n     } n     memset(f, 0, sizeof(f)); n     memset(F, 0, sizeof(F)); n     for (int i = 1; i <= n; ++i) n       if (a[i] != b[i]) f[a[i]] |= (1 << b[i]), F[b[i]] |= (1 << a[i]); n     for (int i = 1; i <= 20; ++i) F[i] |= f[i]; n     memset(u, 0, sizeof(u)); n     memset(dp, -1, sizeof(dp)); n     dp[0] = 1; n     int ans = 0; n     for (int i = 1; i <= 20; ++i) { n       if (!u[i]) { n         m = 0; n         C = 0; n         dfs(i); n         ope(m); n         ans += cnt[m] * 2 - 1 - C; n       } n     } n     printf( %d n , ans); n   } n   return 0; n } n


#include <bits/stdc++.h> n #pragma comment(linker,  /STACK:16777216 ) n using namespace std; n int a, b, c, d, i, j, n, m, k, ans, T; n char str[200003]; n int p[22], cs[22][22], dp[1 << 22]; n pair<int, int> cur[26]; n int main() { n   scanf( %d%d%d , &n, &k, &T); n   scanf( %s , str); n   for (int _n((k)-1), i(0); i <= _n; i++) scanf( %d , &p[i]); n   for (int _n((k)-1), i(0); i <= _n; i++) n     for (int _n((k)-1), j(0); j <= _n; j++) scanf( %d , &cs[i][j]); n   cur[0] = make_pair(-1, -1); n   int were = 0; n   for (int i = (n - 1), _b = (0); i >= _b; i--) { n     int mask = 0, t = str[i] -  A ; n     were |= 1 << t; n     for (int _n((k)-1), j(0); j <= _n; j++) { n       if (cur[j].first == -1) break; n       dp[mask] += cs[t][cur[j].second]; n       dp[mask | 1 << t] -= cs[t][cur[j].second]; n       dp[mask | 1 << cur[j].second] -= cs[t][cur[j].second]; n       dp[mask | (1 << t) | (1 << cur[j].second)] += cs[t][cur[j].second]; n       mask |= 1 << cur[j].second; n       if (cur[j].second == t) break; n     } n     for (int _n((k)-1), j(0); j <= _n; j++) { n       if (cur[j].first == -1 || cur[j].second == t) { n         bool tt = 0; n         if (cur[j].first == -1) tt = 1; n         cur[j] = make_pair(i, t); n         for (int z = (j - 1), _b = (0); z >= _b; z--) swap(cur[z], cur[z + 1]); n         if (tt) cur[j + 1] = make_pair(-1, -1); n         break; n       } n     } n   } n   ans = 0; n   for (int _n((k)-1), i(0); i <= _n; i++) { n     for (int _n((were)-1), mask(0); mask <= _n; mask++) { n       if ((mask & were) != mask) continue; n       if (mask & 1 << i) dp[mask] += dp[mask ^ 1 << i]; n     } n   } n   for (int _n((were)-1), mask(0); mask <= _n; mask++) { n     if ((mask & were) != mask) continue; n     c = dp[mask]; n     for (int _n((k)-1), i(0); i <= _n; i++) n       if (mask & 1 << i) c += p[i]; n     if (c <= T) ++ans; n   } n   printf( %d n , ans); n } n


#include <bits/stdc++.h> n using namespace std; n int n, m, p = 1000000; n bool checkDias(int a, const vector<unsigned long long int> &v) { n   for (int i = p; i < n + 1; i += 1) { n     unsigned long long int d = i / a; n     unsigned long long int r = i % a; n     if (v[i] >= (unsigned long long int)m + r * (d + 1) * d / 2 + n                     (a - r) * d * (d - 1) / 2) n       return true; n   } n   return false; n } n int main() { n   cin >> n >> m; n   vector<int> vect(n); n   vector<unsigned long long int> sumasparciales(n + 1, 0); n   for (int i = 0; i < n; i += 1) { n     cin >> vect[i]; n   } n   sort(vect.begin(), vect.end(), greater<int>()); n   for (int i = 0; i < n; i += 1) { n     sumasparciales[i + 1] = sumasparciales[i] + vect[i]; n     if (sumasparciales[i + 1] >= m && i + 1 < p) p = i + 1; n   } n   if (sumasparciales[n] < m) { n     cout << -1 << endl; n     return 0; n   } n   int i = 1; n   int j = m; n   int minimo = m + 10; n   while (i <= j) { n     int nepe = (i + j) / 2; n     if (checkDias(nepe, sumasparciales)) { n       j = nepe - 1; n       minimo = min(minimo, nepe); n     } else n       i = nepe + 1; n   } n   cout << minimo << endl; n } n


#include <bits/stdc++.h> n using namespace std; n inline char nc() { n   static char buf[1000000], *p1 = buf, *p2 = buf; n   return p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1000000, stdin), p1 == p2) n              ? EOF n              : *p1++; n } n template <typename T> n inline void read(T& sum) { n   char ch = nc(); n   T tf = 0; n   sum = 0; n   while ((ch <  0  || ch >  9 ) && (ch !=  - )) ch = nc(); n   tf = ((ch ==  - ) && (ch = nc())); n   while (ch >=  0  && ch <=  9 ) sum = sum * 10 + (ch - 48), ch = nc(); n   (tf) && (sum = -sum); n } n template <typename T> n inline void write(T x) { n   if (x == 0) { n     putchar( 0 ); n     return; n   } n   char F[200]; n   T tmp = x > 0 ? x : -x; n   if (x < 0) putchar( - ); n   T cnt = 0; n   while (tmp > 0) { n     F[cnt++] = tmp % 10 +  0 ; n     tmp /= 10; n   } n   while (cnt > 0) putchar(F[--cnt]); n } n template <typename T> n T gcd(T a, T b) { n   if (!a || !b) return max(a, b); n   while (b ^= a ^= b ^= a %= b) n     ; n   return a; n } n template <typename T> n T C(T m, T n) { n   if (n > m) return 0; n   if (n == 0 || m == 1 || m == n) return 1; n   return C(m - 1, n - 1) + C(m - 1, n); n } n template <typename T> n void exgcd(T a, T b, T& d, T& x, T& y) { n   if (!b) { n     d = a; n     x = 1; n     y = 0; n   } else { n     exgcd(b, a % b, d, y, x); n     y -= x * (a / b); n   } n } n template <typename T> n T niyuan(T a, T p) { n   T d, x, y; n   exgcd(a, p, d, x, y); n   return d == 1 ? (x + p) % p : -1; n } n template <typename T> n T extgcd(T a, T b, T& x, T& y) { n   if (b) { n     T r = extgcd(b, a % b, y, x); n     y -= x * (a / b); n     return r; n   } else { n     x = 1; n     y = 0; n     return a; n   } n } n template <typename T> n T ax_bmodM(T a, T b, T M) { n   T x, y, r = extgcd(a, M, x, y); n   if (b % r) n     return -1; n   else n     x = (x + M) % M * b / r; n   return x % (M / r); n } n template <typename T> n inline T dmod(const string& s, T mod) { n   T len = s.size(); n   T ans = 0; n   for (T i = 0; i < len; i++) n     ans = (int)(((long long)ans * 10 + s[i] -  0 ) % mod); n   return ans; n } n template <typename T> n inline T ksc(T a, T b, T mod) { n   T ans = 0; n   while (b > 0) { n     if (b & 1) ans = (ans + a) % mod; n     a = (a + a) % mod; n     b >>= 1; n   } n   return ans; n } n template <typename T> n inline T ksm(T x, T n, T mod) { n   T ans = 1; n   while (n > 0) { n     if (n & 1) ans = ans * x % mod; n     x = x * x % mod; n     n >>= 1; n   } n   return ans; n } n template <typename T> n inline T mod_jia(T a, T b, T mod) { n   return (a % mod + b % mod) % mod; n } n template <typename T> n inline T mod_jian(T a, T b, T mod) { n   return (a % mod + mod - b % mod) % mod; n } n template <typename T> n inline T mod_cheng(T a, T b, T mod) { n   return ((a % mod) * (b % mod)) % mod; n } n template <typename T> n inline T mod_chu(T a, T b, T p) { n   b = ksm(b, p - 2, p); n   return a * b % p; n } n template <typename T> n inline void rmqmin(T f[][20], T n) { n   for (int i = 1; i <= n; i++) cin >> f[i][0]; n   for (int j = 1; j <= 20; j++) n     for (int i = 1; i <= n; i++) n       if (i + (1 << j) - 1 <= n) n         f[i][j] = min(f[i][j - 1], f[i + (1 << (j - 1))][j - 1]); n } n template <typename T> n inline T zymin(T f[][20], T z, T y) { n   int x = int(log(y - z + 1) / log(2)); n   return min(f[z][x], f[y - (1 << x) + 1][x]); n } n template <typename T> n inline void rmqmax(T f[][20], T n) { n   for (int i = 1; i <= n; i++) cin >> f[i][0]; n   for (int j = 1; j <= 20; j++) n     for (int i = 1; i <= n; i++) n       if (i + (1 << j) - 1 <= n) n         f[i][j] = max(f[i][j - 1], f[i + (1 << (j - 1))][j - 1]); n } n template <typename T> n inline T zymax(T f[][20], T z, T y) { n   int x = int(log(y - z + 1) / log(2)); n   return max(f[z][x], f[y - (1 << x) + 1][x]); n } n template <typename T> n inline void stgcd(T f[][20], T n) { n   for (int i = 1; i <= n; i++) cin >> f[i][0], f[i + n][0] = f[i][0]; n   for (int j = 1; j <= 20; j++) { n     for (int i = 1; i + (1 << j) - 1 <= 2 * n; i++) { n       f[i][j] = __gcd(f[i][j - 1], f[i + (1 << (j - 1))][j - 1]); n     } n   } n } n template <typename T> n inline T qgcd(T f[][20], T z, T y) { n   int x = int(log(y - z + 1) / log(2)); n   return __gcd(f[z][x], f[y - (1 << x) + 1][x]); n } n template <typename T> n inline T rotatex(T x, T y, T ang, T ox = 0, T oy = 0) { n   return (x - ox) * cos(ang) - (y - oy) * sin(ang) + ox; n } n template <typename T> n inline T rotatey(T x, T y, T ang, T ox = 0, T oy = 0) { n   return (x - ox) * sin(ang) + (y - oy) * cos(ang) + oy; n } n vector<int> l, y; n char s[200005]; n pair<int, int> p[200005]; n int a[200005], ans[200005]; n bool cmp(int x, int y) { return a[x] < a[y]; } n void solve() { n   l.clear(); n   y.clear(); n   int n; n   cin >> n; n   for (int i = 1; i <= n; ++i) { n     cin >> a[i]; n     p[i] = make_pair(a[i], i); n   } n   cin >> (s + 1); n   for (int i = 1; i <= n; ++i) { n     if (s[i] ==  1 ) n       y.push_back(i); n     else n       l.push_back(i); n   } n   int xx = 0; n   int long long = l.size(); n   int yy = y.size(); n   sort(y.begin(), y.end(), cmp); n   sort(l.begin(), l.end(), cmp); n   for (auto& i : l) ans[i] = ++xx; n   for (auto& i : y) ans[i] = ++xx; n   for (int i = 1; i <= n; ++i) cout << ans[i] <<    ; n   cout <<   n ; n } n int main() { n   ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); n   int _t; n   cin >> _t; n   while (_t--) solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const double EPS = 1e-6; n const long long mod = 1000000007ll; n const long long mod1 = 1000000009ll; n const long long mod2 = 1100000009ll; n const double PI = 3.14159265359; n const long long MX = 1000000000000ll; n int INF = 2147483645; n long long INFINF = (long long)1e18; n using namespace std; n template <class T> n T Max2(T a, T b) { n   return a < b ? b : a; n } n template <class T> n T Min2(T a, T b) { n   return a < b ? a : b; n } n template <class T> n T Max3(T a, T b, T c) { n   return Max2(Max2(a, b), c); n } n template <class T> n T Min3(T a, T b, T c) { n   return Min2(Min2(a, b), c); n } n template <class T> n T Max4(T a, T b, T c, T d) { n   return Max2(Max2(a, b), Max2(c, d)); n } n template <class T> n T Min4(T a, T b, T c, T d) { n   return Min2(Min2(a, b), Max2(c, d)); n } n template <class T> n void swap(T &a, T &b) { n   T c = b; n   b = a; n   a = c; n } n long long bit_count(long long _x) { n   long long _ret = 0; n   while (_x) { n     if (_x % 2 == 1) _ret++; n     _x /= 2; n   } n   return _ret; n } n long long bit(long long _mask, long long _i) { n   return (_mask & (1 << _i)) == 0 ? 0 : 1; n } n long long powermod(long long _a, long long _b, long long _m) { n   long long _r = 1; n   while (_b) { n     if (_b % 2 == 1) _r = (_r * _a) % _m; n     _b /= 2; n     _a = (_a * _a) % _m; n   } n   return _r; n } n long long add(long long a, long long b, long long m) { n   long long x = a + b; n   while (x >= m) x -= m; n   return x; n } n long long sub(long long a, long long b, long long m) { n   long long x = a - b; n   while (x < 0) x += m; n   return x; n } n long long mul(long long a, long long b, long long m) { n   long long x = a * b; n   x %= m; n   return x; n } n int main() { n   ios::sync_with_stdio(false); n   cin.tie(NULL); n   int q, n; n   cin >> q; n   for (int i = 0; i < int(q); i++) { n     cin >> n; n     vector<int> a(n); n     for (int i = 0; i < int(n); i++) { n       cin >> a[i]; n     } n     sort(a.begin(), a.end()); n     int f = 0; n     for (int i = 0; i < int(n - 1); i++) { n       if (a[i + 1] - a[i] == 1) { n         f = 1; n         break; n       } n     } n     if (f) n       cout << 2 << endl; n     else n       cout << 1 << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, x = 0; n   cin >> n; n   while (n > 0) { n     if (n >= 100) { n       x += n / 100; n       n %= 100; n     } else if (n >= 20 && n < 100) { n       x += n / 20; n       n %= 20; n     } else if (n >= 10 && n < 50) { n       x += n / 10; n       n %= 10; n     } else if (n >= 5 && n < 10) { n       x += n / 5; n       n %= 5; n     } else { n       x += n; n       n = 0; n     } n   } n   cout << x; n } n


#include <bits/stdc++.h> n using namespace std; n bool check(int c1, int c2, int c3, int v1, int v2, int v3, int m) { n   return (c1 >= v1 && c1 <= 2 * v1 && c2 >= v2 && c2 <= 2 * v2 && c3 >= v3 && n           c3 <= 2 * v3 && 2 * m >= c3 && 2 * m < c2 && c3 < c2 && c2 < c1 && n           m <= c3); n } n int main() { n   int v1, v2, v3, m; n   cin >> v1 >> v2 >> v3 >> m; n   for (int i = 1; i <= 200; i++) { n     for (int j = i + 1; j <= 200; j++) { n       for (int k = j + 1; k <= 200; k++) { n         if (check(k, j, i, v1, v2, v3, m)) { n           cout << k << endl; n           cout << j << endl; n           cout << i << endl; n           return 0; n         } n       } n     } n   } n   cout << -1 << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename A, typename B, typename C> n struct triple { n   A X; n   B Y; n   C Z; n   triple(A a = 0, B b = 0, C c = 0) : X(a), Y(b), Z(c) {} n }; n template <typename A, typename B, typename C> n triple<A, B, C> make_triple(A a = 0, B b = 0, C c = 0) { n   return triple<A, B, C>(a, b, c); n } n template <typename A, typename B, typename C> n bool operator<(const triple<A, B, C>& a, const triple<A, B, C>& b) { n   if (a.X != b.X) return a.X < b.X; n   if (a.Y != b.Y) return a.Y < b.Y; n   return a.Z < b.Z; n } n template <typename T, typename SS> n ostream& operator<<(ostream& ofs, const pair<T, SS>& p) { n   ofs <<  (   << p.first <<   ,   << p.second <<   ) ; n   return ofs; n } n template <typename T> n void print(T a) { n   for (auto i : a) cout << i <<    ; n   cout <<   n ; n } n template <typename T> n T max(T a, T b, T c) { n   return max(a, max(b, c)); n } n template <typename T> n T min(T a, T b, T c) { n   return min(a, max(b, c)); n } n vector<vector<long long> > g; n vector<long long> h; n vector<long long> p; n void dfs(long long v, long long hh = 0, long long pp = -1) { n   h[v] = hh; n   for (auto to : g[v]) n     if (to != pp) { n       p[to] = v; n       dfs(to, hh + 1, v); n     } n } n signed main() { n   srand(time(NULL)); n   cin.tie(0); n   cout.tie(0); n   ios_base::sync_with_stdio(false); n   long long n; n   cin >> n; n   g.resize(n); n   p.resize(n); n   h.resize(n); n   for (long long _ = 0; _ < (n - 1); _++) { n     long long x, y; n     cin >> x >> y; n     x--, y--; n     g[x].push_back(y); n     g[y].push_back(x); n   } n   dfs(0); n   set<pair<long long, long long> > need; n   for (long long i = 0; i < n; i++) n     if (h[i] > 2) need.insert(make_pair(-h[i], i)); n   long long ans = 0; n   while (need.size()) { n     long long x = need.begin()->second; n     long long y = p[x]; n     if (need.count(make_pair(-h[y], y))) need.erase(make_pair(-h[y], y)); n     for (auto to : g[y]) n       if (need.count(make_pair(-h[to], to))) need.erase(make_pair(-h[to], to)); n     ans++; n   } n   cout << ans <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n bool comp[30000]; n int P[30000]; n int seive(int n, int* P) { n   int p, q, K = 0; n   comp[1] = true; n   char even = 1; n   for (p = 2; p * p <= n; p += 2) { n     if (!comp[p]) { n       for (q = p * p; q <= n; q += p * (2 - even)) comp[q] = true; n     } n     if (p == 2) --p, even = 0; n   } n   for (int i = (1); i <= (n); ++i) n     if (!comp[i]) P[K++] = i; n   return K; n } n int muls[30000]; n int has[30000]; n int main() { n   int K; n   cin >> K; n   int lim = 2 * K * K; n   int least = (K + 1) / 2; n   vector<int> ans; n   ans.push_back(1); n   int max_added = 0; n   int L = seive(5 * K, P); n   for (int l = 0; l < L && ((int)ans.size()) < K; ++l) { n     for (int l2 = 0; l2 < (l); ++l2) { n       has[l2] = 0; n       for (int i = 0; i < (((int)ans.size())); ++i) n         if (!(ans[i] % P[l2])) ++has[l2]; n     } n     int p = P[l]; n     set<int> tmp; n     for (long long q = p; q <= lim; q *= p) { n       for (int i = 0; i < ((int)ans.size()) && (ans[i] % p); ++i) { n         long long new_num = 1ll * q * ans[i]; n         if (new_num <= lim) { n           tmp.insert(new_num); n         } n       } n     } n     set<int>::iterator it = tmp.begin(); n     for (int i = 0; it != tmp.end() && i < least; ++it) { n       int v = *it; n       assert(!(v % p)); n       for (int j = 0; j < (l); ++j) n         if (!(v % P[j])) ++has[j]; n       if (((int)ans.size()) < K) { n         ans.push_back(v); n       } else { n         bool found = false; n         for (int j = (K - 1); j >= (0); --j) { n           int w = ans[j]; n           bool good = true; n           for (int h = (0); h <= (l); ++h) { n             int& pr = P[h]; n             if (!(w % pr)) { n               if (has[h] <= least) { n                 good = false; n                 break; n               } n             } n           } n           if (good) { n             for (int h = (0); h <= (l - 1); ++h) { n               int& pr = P[h]; n               if (!(w % pr)) { n                 has[h]--; n               } n             } n             assert(ans[j] % p); n             ans[j] = v; n             found = true; n             break; n           } n         } n         assert(found); n         assert(ans.size() == K); n       } n       ++i; n     } n     assert(((int)ans.size()) <= K); n   } n   assert(((int)ans.size()) == K); n   sort(ans.begin(), ans.end()); n   ans.erase(unique(ans.begin(), ans.end()), ans.end()); n   assert(((int)ans.size()) == K); n   for (int l = 0; l < (L); ++l) { n     int cnt = 0; n     for (int i = 0; i < ((int)ans.size()); ++i) n       if (!(ans[i] % P[l])) { n         ++cnt; n       } n     assert(cnt == 0 || cnt >= least); n   } n   for (int i = 0; i < (((int)ans.size())); ++i) assert(ans[i] <= lim); n   for (int i = 0; i < (K); ++i) { n     if (i > 0) printf(   ); n     printf( %d , ans[i]); n   } n   printf(  n ); n } n


#include <bits/stdc++.h> n const long long int inf = 9e18; n const long double pi = 2 * acos(0.0); n using namespace std; n long long int power(long long int a, long long int n) { n   if (n == 0) { n     return 1; n   } n   long long int p = power(a, n / 2) % 998244353; n   p = (p * p) % 998244353; n   if (n % 2 == 1) { n     p = (p * a) % 998244353; n   } n   return p; n } n long long int gcd(long long int a, long long int b) { n   if (a == 0) return b; n   return gcd(b % a, a); n } n int isprime(long long int n) { n   for (long long int i = 2; i <= sqrt(n); i++) { n     if (n % i == 0) { n       return 0; n     } n   } n   return 1; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   ; n   long long int t; n   cin >> t; n   while (t--) { n     long long int n; n     cin >> n; n     if (n >= 15) { n       n = n % 14; n       if (n <= 6 && n >= 1) { n         cout <<  YES  n              <<   n ; n       } else { n         cout <<  NO  n              <<   n ; n       } n     } else { n       cout <<  NO  n            <<   n ; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; 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 ) s = s * 10 + ch -  0 , ch = getchar(); n   return s * w; n } n vector<int> v; n signed main() { n   int n = read(), k = read() % 64, x = read(); n   for (int i = 1, x; i <= n; i++) x = read(), v.push_back(x); n   while (k--) { n     sort(v.begin(), v.end()); n     for (int i = 0; i < n; i += 2) v[i] ^= x; n   } n   sort(v.begin(), v.end()); n   printf( %d %d n , v[n - 1], v[0]); n   return 0; n } n


#include <bits/stdc++.h> n #pragma GCC optimize( Ofast ) n using namespace std; n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   string s; n   int temp; n   long long sol = 0; n   cin >> s; n   if ((s[0] -  0 ) % 4 == 0) { n     sol++; n   } n   for (int a = s.length() - 1; a > 0; a--) { n     temp = (s[a] -  0 ) + 10 * (s[a - 1] -  0 ); n     if (temp % 4 == 0) { n       sol += a; n     } n     if ((s[a] -  0 ) % 4 == 0) { n       sol++; n     } n   } n   cout << sol <<   n ; n   cerr <<   nTime elapsed:   << 1000 * clock() / CLOCKS_PER_SEC <<  ms n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int s[200010], univ[200010]; n int n, k, n_univ; n vector<int> edges[200010]; n bool visited[200010]; n int dist[200010]; n int getSum(int node) { n   int sum = univ[node]; n   visited[node] = true; n   for (auto e : edges[node]) { n     if (!visited[e]) sum += getSum(e); n   } n   s[node] = sum; n   return sum; n } n int dfs(int node) { n   visited[node] = true; n   for (auto e : edges[node]) { n     if (visited[e]) continue; n     if (s[e] * 2 > n_univ) { n       return dfs(e); n     } n   } n   return node; n } n long long getAns(int centroid) { n   queue<pair<int, int> > q; n   memset(visited, 0, sizeof(visited)); n   q.push(make_pair(centroid, 0)); n   visited[centroid] = true; n   while (!q.empty()) { n     pair<int, int> node = q.front(); n     q.pop(); n     dist[node.first] = node.second; n     for (int e : edges[node.first]) { n       if (!visited[e]) { n         visited[e] = true; n         q.push(make_pair(e, node.second + 1)); n       } n     } n   } n   long long sum = 0; n   for (int i = 0; i < n; i++) { n     if (univ[i]) sum += dist[i]; n   } n   return sum; n } n int main() { n   cin >> n >> k; n   n_univ = k + k; n   for (int i = 0; i < n_univ; i++) { n     int x; n     cin >> x; n     x--; n     univ[x] = 1; n   } n   for (int i = 0; i < n - 1; i++) { n     int v1, v2; n     cin >> v1 >> v2; n     v1--; n     v2--; n     edges[v1].push_back(v2); n     edges[v2].push_back(v1); n   } n   getSum(0); n   memset(visited, 0, sizeof(visited)); n   cout << getAns(dfs(0)) << endl; n } n


#include <bits/stdc++.h> n int main() { n   char n[101], a[4] =  127 , b[6] =  32767 , c[11] =  2147483647 , n                d[20] =  9223372036854775807 ; n   int i, l, count, mark; n   while (~scanf( %s , n)) { n     l = strlen(n); n     if (l < 3) { n       printf( byte n ); n       continue; n     } n     if (l == 3) { n       mark = 1; n       for (i = 0; i < l; i++) { n         if (n[i] > a[i]) break; n         if (n[i] < a[i]) { n           mark = 0; n           break; n         } n         if (i == l - 1) { n           if (n[i] == a[i]) mark = 0; n         } n       } n       if (mark == 0) n         printf( byte n ); n       else n         printf( short n ); n       continue; n     } n     if (l == 4) { n       printf( short n ); n       continue; n     } n     if (l == 5) { n       mark = 1; n       for (i = 0; i < l; i++) { n         if (n[i] > b[i]) break; n         if (n[i] < b[i]) { n           mark = 0; n           break; n         } n         if (i == l - 1) { n           if (n[i] == b[i]) mark = 0; n         } n       } n       if (mark == 0) n         printf( short n ); n       else n         printf( int n ); n       continue; n     } n     if (l <= 9) { n       printf( int n ); n       continue; n     } n     if (l == 10) { n       mark = 1; n       for (i = 0; i < l; i++) { n         if (n[i] > c[i]) break; n         if (n[i] < c[i]) { n           mark = 0; n           break; n         } n         if (i == l - 1) { n           if (n[i] == c[i]) mark = 0; n         } n       } n       if (mark == 0) n         printf( int n ); n       else n         printf( long n ); n       continue; n     } n     if (l < 19) { n       printf( long n ); n       continue; n     } n     if (l == 19) { n       mark = 1; n       for (i = 0; i < l; i++) { n         if (n[i] > d[i]) break; n         if (n[i] < d[i]) { n           mark = 0; n           break; n         } n         if (i == l - 1) { n           if (n[i] == d[i]) mark = 0; n         } n       } n       if (mark == 0) n         printf( long n ); n       else n         printf( BigInteger n ); n       continue; n     } n     printf( BigInteger n ); n   } n } n


#include <bits/stdc++.h> n using namespace std; n string second[100]; n int x[100], y[100]; n vector<int> a, b; n bool u[100][100]; n bool uu[100][100]; n bool ux[100], uy[100]; n int n, m; n void dfs(int x, int y) { n   ux[x] = uy[y] = true; n   u[x][y] = true; n   for (int i = 0; i < n; i++) n     if (second[i][y] ==  #  && !u[i][y]) dfs(i, y); n   for (int j = 0; j < m; j++) n     if (second[x][j] ==  #  && !u[x][j]) dfs(x, j); n } n int main() { n   cin >> n >> m; n   for (int i = 0; i < n; i++) cin >> second[i]; n   for (int i = 0; i < n; i++) n     for (int j = 0; j < m; j++) n       if (second[i][j] ==  #  && !u[i][j]) { n         for (int i = 0; i < n; i++) ux[i] = false; n         for (int i = 0; i < m; i++) uy[i] = false; n         dfs(i, j); n         a.clear(); n         b.clear(); n         for (int i = 0; i < n; i++) n           if (ux[i]) a.push_back(i); n         for (int j = 0; j < m; j++) n           if (uy[j]) b.push_back(j); n         for (int i = 0; i < n; i++) n           for (int j = 0; j < m; j++) uu[i][j] = false; n         for (int i = 0; i < a.size(); i++) n           for (int j = 0; j < b.size(); j++) { n             uu[a[i]][b[j]] = true; n           } n         for (int i = 0; i < n; i++) n           for (int j = 0; j < m; j++) { n             if (ux[i] && second[i][j] ==  #  && !uu[i][j]) { n               cout <<  No ; n               return 0; n             } n             if (second[i][j] ==  .  && uu[i][j]) { n               cout <<  No ; n               return 0; n             } n           } n       } n   cout <<  Yes ; n   return 0; n } n