#include <bits/stdc++.h> n #define ull unsigned long long n #define ll long long n #define il inline n #define db double n #define ls rt << 1 n #define rs rt << 1 | 1 n #define pb push_back n #define mp make_pair n #define pii pair<int, int> n #define X first n #define Y second n #define pcc pair<char, char> n #define vi vector<int> n #define vl vector<ll> n #define rep(i, x, y) for(int i = x; i <= y; i ++) n #define rrep(i, x, y) for(int i = x; i >= y; i --) n #define rep0(i, n) for(int i = 0; i < (n); i ++) n #define per0(i, n) for(int i = (n) - 1; i >= 0; i --) n #define ept 1e-9 n #define sz(x) (x).size() n #define All(x) (x).begin(), (x).end() n using namespace std; n inline int read() n { n  int x = 0, f = 1; char ch = getchar(); n  while (ch <  0  || ch >  9 ) { if (ch ==  - ) f = -1; ch = getchar(); } n  while (ch >=  0  && ch <=  9 ) { x = x * 10 + ch -  0 ; ch = getchar(); } n  return x * f; n } n inline ll read1() n { n  ll x = 0, f = 1; char ch = getchar(); n  while (ch <  0  || ch >  9 ) { if (ch ==  - ) f = -1; ch = getchar(); } n  while (ch >=  0  && ch <=  9 ) { x = x * 10 + ch -  0 ; ch = getchar(); } n  return x * f; n } n #define int long long n const int N = 50010; n const int mod = 998244353; n int n, m; n int d[N][25]; n void add(int &a, int b) {a += b; if (a >= mod) a -= mod;} n void dec(int &a, int b) {a -= b; if (a < 0) a += mod;} n int pow_mod(int a, int i) n { n  int s = 1; n  while (i) n  { n   if (i & 1) s = 1LL * s * a % mod; n   a = 1LL * a * a % mod; n   i >>= 1; n  } n  return s; n } n signed main() n { n  n = read(); n  m = read(); n  rep0(i, n) n   rep0(j, m) n    d[j][i] = read(); n  rep0(i, m) sort(d[i], d[i] + n); n  int ans = m; n  int jc = 1; n  rep(i, 1, n) jc = 1LL * jc * i % mod; n  jc = pow_mod(jc, mod - 2); n  rep0(i, m) n  { n   int res = 1; n   rrep(j, n - 1, 0) res = 1LL * res * max(0LL, d[i][j] - (j + 1)) % mod; n   dec(ans, 1LL * res * jc % mod); n  } n  printf( %d n , ans); n  return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int count1(int x) { n   int ans = 0; n   while (x) { n     ans += x & 1; n     x >>= 1; n   } n   return ans; n } n int main() { n   int n, p; n   cin >> n >> p; n   for (int x = 0; x * (p + 1) <= n; x++) n     if (count1(n - x * p) <= x) { n       cout << x; n       return 0; n     } n   cout << -1; n } n


#include <bits/stdc++.h> n using namespace std; n using ll = long long; n using ii = pair<int, int>; n using vi = vector<int>; n using vll = vector<ll>; n using vii = vector<ii>; n const ll MOD = 998244353; n const ll INF = 1e18 + 9; n const int MAXN = 1000006; n const int MAXC = 20; n int n, arr[MAXN], num[MAXC]; n ll dist[MAXC][MAXC], mem[1 << MAXC], realDist[1 << MAXC][MAXC]; n ll solve(int p) { n   if (mem[p] != -1) return mem[p]; n   mem[p] = INF; n   for (int i = 0; i < MAXC; i++) { n     if ((p & (1 << i)) == 0) { n       mem[p] = min(mem[p], solve(p | (1 << i)) + realDist[p][i]); n     } n   } n   return mem[p]; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cin >> n; n   for (int i = 0; i < n; i++) { n     cin >> arr[i]; n     arr[i]--; n     for (int j = 0; j < MAXC; j++) { n       dist[arr[i]][j] += num[j]; n     } n     num[arr[i]]++; n   } n   int p = 0; n   for (int i = 0; i < (1 << MAXC); i++) { n     for (int j = 0; j < MAXC; j++) { n       for (int k = 0; k < MAXC; k++) { n         if (i & (1 << k)) realDist[i][j] += dist[k][j]; n       } n     } n   } n   memset(mem, -1, sizeof(mem)); n   ll ans = INF; n   mem[(1 << MAXC) - 1] = 0; n   for (int i = 0; i < MAXC; i++) { n     ans = min(ans, solve(p | (1 << i))); n   } n   cout << ans << endl; n } n


#include <bits/stdc++.h> n using namespace std; n long long a[3000], b[3000]; n map<long long, pair<long long, long long> > mymap; n pair<long long, long long> p1, p2; n int main() { n   std::ios::sync_with_stdio(false); n   long long n, m, sa = 0, sb = 0; n   p1.first = p1.second = p2.first = p2.second = -1; n   mymap.clear(); n   cin >> n; n   for (long long i = (long long)(1); i <= (long long)(n); i++) { n     cin >> a[i]; n     sa += a[i]; n   } n   cin >> m; n   for (long long i = (long long)(1); i <= (long long)(m); i++) { n     cin >> b[i]; n     sb += b[i]; n   } n   long long curdiff = abs(sa - sb); n   for (long long i = (long long)(1); i <= (long long)(n); i++) { n     for (long long j = (long long)(1); j <= (long long)(m); j++) { n       long long tmpsa, tmpsb; n       tmpsa = sa - a[i] + b[j]; n       tmpsb = sb + a[i] - b[j]; n       if (abs(tmpsa - tmpsb) < curdiff) { n         curdiff = abs(tmpsa - tmpsb); n         p1.first = i; n         p1.second = j; n       } n     } n   } n   for (long long i = (long long)(1); i <= (long long)(n - 1); i++) { n     for (long long j = (long long)(i + 1); j <= (long long)(n); j++) { n       long long val = a[i] + a[j]; n       val = val * 2ll; n       mymap[val] = make_pair(i, j); n     } n   } n   map<long long, pair<long long, long long> >::iterator it; n   for (long long i = (long long)(1); i <= (long long)(m - 1); i++) { n     for (long long j = (long long)(i + 1); j <= (long long)(m); j++) { n       long long val = sa - sb + 2 * (b[i] + b[j]); n       it = mymap.lower_bound(val); n       if (it != mymap.begin()) it--; n       if (it == mymap.end()) continue; n       long long temp = val; n       temp = abs(val - it->first); n       if (temp < curdiff) { n         curdiff = temp; n         p1.first = (it->second).first; n         p1.second = i; n         p2.first = (it->second).second; n         p2.second = j; n       } n       it++; n       if (it != mymap.end()) { n         temp = abs(val - it->first); n         if (temp < curdiff) { n           curdiff = temp; n           p1.first = (it->second).first; n           p1.second = i; n           p2.first = (it->second).second; n           p2.second = j; n         } n       } n     } n   } n   cout << curdiff << endl; n   if (p2.first != -1) { n     cout <<  2  << endl; n     cout << p1.first <<     << p1.second << endl; n     cout << p2.first <<     << p2.second << endl; n   } else if (p1.first != -1) { n     cout <<  1  << endl; n     cout << p1.first <<     << p1.second << endl; n   } else { n     cout <<  0  << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int t; n   scanf( %d , &t); n   while (t--) { n     int n; n     scanf( %d , &n); n     vector<int> vt; n     while (n) { n       int lst = -1; n       for (int i = 2;; i++) { n         if (1ll * i * (i - 1) / 2 <= n) n           lst = i; n         else n           break; n       } n       vt.push_back(lst); n       n -= 1ll * lst * (lst - 1) / 2; n     } n     int maxi = vt[0]; n     reverse(vt.begin(), vt.end()); n     printf( 1 ); n     int x = 0, sum = 0; n     for (int i = 1; i <= maxi; i++) { n       printf( 3 ); n       while (x < vt.size() && i == vt[x]) { n         printf( 7 ); n         x++; n       } n     } n     printf(  n ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 2000 + 50; n int n, cnt[maxn], a[maxn]; n vector<int> q; n int main() { n   cin >> n; n   int sign = 0; n   for (int i = 1; i < n; i++) { n     int x, y; n     cin >> x >> y; n     if (x > y) swap(x, y); n     if (y != n) sign = 1; n     cnt[x]++; n     a[i] = x; n   } n   sort(a + 1, a + n); n   for (int i = 1; i < n; i++) { n     if (a[i] < i) { n       sign = 1; n     } n   } n   if (sign) { n     cout <<  NO  << endl; n     return 0; n   } n   cout <<  YES  << endl; n   for (int i = 1; i < n; i++) { n     if (cnt[i]) { n       q.push_back(i); n     } n   } n   int opt = 1; n   for (int x = 0; x < q.size(); x++) { n     int pre = n; n     for (int i = 1; i <= cnt[q[x]] - 1; i++) { n       while (cnt[opt]) opt++; n       cout << pre <<     << opt << endl; n       pre = opt; n       opt++; n     } n     cout << pre <<     << q[x] << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long int mxn = 0; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   long long int t = 1; n   cin >> t; n   while (t--) { n     long long int i, j, k, n, m; n     string s; n     cin >> s; n     n = s.length(); n     string a, b, c; n     i = -1; n     j = n; n     while ((i + 1) < (j - 1) && s[i + 1] == s[j - 1]) { n       a += s[i + 1]; n       i++, j--; n     } n     c = a; n     reverse(c.begin(), c.end()); n     long long int pr = -1, sf = -1; n     for (long long int i1 = i + 1; i1 < j; i1++) { n       long long int st = i + 1, en = i1; n       while (st <= en && s[st] == s[en]) st++, en--; n       if (st > en) pr = i1; n     } n     for (long long int i1 = i + 1; i1 < j; i1++) { n       long long int st = i1, en = j - 1; n       while (st <= en && s[st] == s[en]) st++, en--; n       if (st > en) { n         sf = i1; n         break; n       } n     } n     if (pr != -1) { n       if (pr - i > j - sf) { n         for (long long int i1 = i + 1; i1 <= pr; i1++) b += s[i1]; n       } else { n         for (long long int i1 = sf; i1 < j; i1++) b += s[i1]; n       } n     } n     cout << a << b << c <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 650; n int kd[MAXN], edges[100010][2]; n int idA[MAXN], idB[MAXN]; n int g[MAXN][MAXN]; n int P; n inline int advPow(int a, int b) { n   int ret = 1; n   while (b) { n     if (b & 1) ret = (long long)ret * a % P; n     a = (long long)a * a % P; n     b >>= 1; n   } n   return ret; n } n inline int inverse(int a) { return advPow(a, P - 2); } n int gauss(int N) { n   int ret = 1; n   for (int col = 1; col <= N; col++) { n     int pos = 0; n     for (int row = col; row <= N; row++) n       if (g[row][col]) { n         pos = row; n         break; n       } n     if (pos && pos != col) { n       for (int i = 1; i <= N; i++) swap(g[pos][i], g[col][i]); n       ret *= -1; n     } n     if (pos == 0) return 0; n     int inv = inverse(g[col][col]); n     for (int row = 1; row <= N; row++) { n       if (!g[row][col] || row == col) continue; n       int cur = (long long)g[row][col] * inv % P; n       for (int i = col; i <= N; i++) n         g[row][i] = (g[row][i] - (long long)g[col][i] * cur % P + P) % P; n     } n   } n   for (int i = 1; i <= N; i++) ret = (long long)ret * g[i][i] % P; n   return (ret + P) % P; n } n struct Node { n   int v, nxt; n } d[100010]; n int head[MAXN], etot; n inline void addEdge(int a, int b) { n   etot++; n   d[etot].v = b; n   d[etot].nxt = head[a]; n   head[a] = etot; n } n int rin[MAXN], rout[MAXN]; n int plans[MAXN][MAXN]; n queue<int> que; n int main() { n   int N, M; n   scanf( %d%d%d , &N, &M, &P); n   for (int i = 1; i <= M; i++) { n     int a, b; n     scanf( %d%d , &a, &b); n     if (a == b) continue; n     rin[b]++, rout[a]++; n     addEdge(a, b); n   } n   for (int i = 1; i <= N; i++) n     if (!rin[i]) que.push(i); n   for (int i = 1; i <= N; i++) plans[i][i] = 1; n   int cnt1 = 0, cnt2 = 0; n   for (int i = 1; i <= N; i++) n     if (!rin[i]) idA[++cnt1] = i; n   for (int i = 1; i <= N; i++) n     if (!rout[i]) idB[++cnt2] = i; n   if (cnt1 != cnt2) { n     puts( 0 ); n     return 0; n   } n   while (!que.empty()) { n     int u = que.front(); n     que.pop(); n     for (int e = head[u]; e; e = d[e].nxt) { n       rin[d[e].v]--; n       for (int i = 1; i <= N; i++) (plans[i][d[e].v] += plans[i][u]) %= P; n       if (!rin[d[e].v]) que.push(d[e].v); n     } n   } n   for (int i = 1; i <= cnt1; i++) n     for (int j = 1; j <= cnt2; j++) g[i][j] = plans[idA[i]][idB[j]]; n   printf( %d n , gauss(cnt1)); n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   double r, x, y, x1, y1; n   cin >> r >> x >> y >> x1 >> y1; n   double dist = sqrt(pow(x - x1, 2) + pow(y - y1, 2)); n   double ans = dist / (2 * r); n   cout << ceil(ans) << endl; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAX = 3e5; n int a[MAX] = {}; n int main() { n   int t; n   cin >> t; n   while (t--) { n     char c; n     long long s; n     cin >> c >> s; n     long long a1 = 0; n     for (int i = 0; i < 18; i++) { n       a1 = a1 * 2 + s % 2; n       s /= 10; n     } n     if (c ==  + ) { n       a[a1]++; n     } else if (c ==  - ) { n       a[a1]--; n     } else { n       cout << a[a1] <<   n ; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<int> l[100007]; n int parent[100007]; n int color[100007]; n int k; n int p; n int dfs(int u, int c) { n   int sz = l[u].size(); n   { n     for (int i = 0; i < sz; i++) { n       if (color[u] - color[l[u][i]] >= k && color[l[u][i]] != 0) { n         parent[l[u][i]] = u; n         return u; n       } else if (color[l[u][i]] == 0) { n         color[l[u][i]] = c + 2; n         parent[l[u][i]] = u; n         return dfs(l[u][i], c + 1); n       } n     } n   } n } n int main() { n   int n, m; n   cin >> n >> m >> k; n   int a, b; n   for (int i = 0; i < m; i++) { n     cin >> a >> b; n     l[a].push_back(b); n     l[b].push_back(a); n   } n   color[1] = 1; n   int u = dfs(1, 0); n   int v = parent[u]; n   int cycle = 1; n   while (v != u) { n     v = parent[v]; n     cycle++; n   } n   cout << cycle << endl; n   cout << u <<    ; n   v = parent[u]; n   while (v != u) { n     cout << v <<    ; n     v = parent[v]; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const long long mod = 998244353; n int n, m, k; n int p[40]; n struct query { n   int l, r, x; n }; n struct query q[500005]; n int max0[500005], max1[500005]; n long long dp[500005]; n long long s[500005]; n void solve() { n   cin >> n >> k >> m; n   p[0] = 1; n   for (int i = 1; i < 40; i++) p[i] = (p[i - 1] << 1); n   n++; n   for (int(i) = (1); (i) <= (m); ++(i)) cin >> q[i].l >> q[i].r >> q[i].x; n   m++; n   q[m].l = n; n   q[m].r = n; n   q[m].x = 0; n   long long ans = 1; n   for (int(j) = (0); (j) <= (k - 1); ++(j)) { n     for (int(i) = (0); (i) <= (n); ++(i)) { n       max0[i] = max1[i] = -1; n     } n     for (int(i) = (1); (i) <= (m); ++(i)) { n       if (q[i].x & p[j]) { n         if (max1[q[i].l] < q[i].r) max1[q[i].l] = q[i].r; n       } else { n         if (max0[q[i].r] < q[i].l) max0[q[i].r] = q[i].l; n       } n     } n     for (int i = 0; i <= n; i++) s[i] = 0; n     int L0 = 0; n     int R1 = -1; n     dp[0] = 1; n     s[0] = 1; n     for (int i = 1; i <= n; i++) { n       R1 = max(R1, max1[i]); n       if (i <= R1) { n         dp[i] = 0; n       } else { n         dp[i] = (L0 == 0 ? s[i - 1] : s[i - 1] - s[L0 - 1]); n         dp[i] %= mod; n       } n       s[i] = (s[i - 1] + dp[i]) % mod; n       L0 = max(L0, max0[i]); n     } n     ans = (ans * dp[n]) % mod; n   } n   if (ans < 0) ans += mod; n   cout << (ans) <<   n ; n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   int T = 1; n   for (int t = 1; t <= T; t++) solve(); n } n


#include <bits/stdc++.h> n using namespace std; n const int inf = 1e9; n bool dentro[410000]; n int main() { n   int n, k; n   scanf( %d %d , &n, &k); n   int vazio = 0; n   int v[n]; n   vector<int> proximo(n, inf); n   vector<int> mapa(n, -1); n   for (int i = 0; i < n; i++) { n     scanf( %d , &v[i]); n     if (mapa[v[i]] != -1) { n       proximo[mapa[v[i]]] = i; n     } n     mapa[v[i]] = i; n   } n   int resp = 0; n   priority_queue<pair<int, int> > q; n   int qt = 0; n   for (int i = 0; i < n; i++) { n     if (dentro[v[i]]) { n       q.push(pair<int, int>(proximo[i], v[i])); n       continue; n     } n     if (qt < k) { n       q.push(pair<int, int>(proximo[i], v[i])); n       qt++; n       dentro[v[i]] = true; n       resp++; n       continue; n     } n     pair<int, int> aux = q.top(); n     q.pop(); n     resp++; n     int u = aux.second; n     dentro[u] = false; n     dentro[v[i]] = true; n     q.push(pair<int, int>(proximo[i], v[i])); n   } n   printf( %d n , resp); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int mapping[200001]; n int main(int narg, char **arg) { n   int h, m, n; n   cin >> h >> m >> n; n   int hh = 0, occup_size = 0; n   while (true) { n     mapping[hh] = occup_size; n     hh += m; n     hh %= h; n     occup_size++; n     if (!hh) break; n   } n   int n_occup = h / occup_size; n   map<int, int> occup[n_occup]; n   for (int i = 0; i < occup_size; i++) mapping[i] = mapping[i * n_occup]; n   long long counter = 0; n   map<int, pair<int, int> > positions; n   for (int i = 0; i < n; i++) { n     char op; n     int id, hash_value; n     cin >> op >> id; n     int i_occup, i_start; n     if (op ==  + ) { n       cin >> hash_value; n       i_occup = hash_value % n_occup; n       i_start = mapping[hash_value / n_occup]; n       while (true) { n         map<int, int>::iterator it = occup[i_occup].upper_bound(i_start); n         if (it == occup[i_occup].begin()) { n           occup[i_occup][i_start] = i_start; n           it = occup[i_occup].begin(); n         } else { n           --it; n           if (it->second >= i_start - 1) { n             if (it->second == occup_size - 1) { n               counter += occup_size - i_start; n               i_start = 0; n               continue; n             } n             it->second++; n             counter += it->second - i_start; n             i_start = it->second; n           } else { n             it = occup[i_occup].insert(make_pair(i_start, i_start)).first; n           } n         } n         map<int, int>::iterator it2 = it; n         ++it2; n         if (it2 != occup[i_occup].end()) { n           if (it->second + 1 == it2->first) { n             it->second = it2->second; n             occup[i_occup].erase(it2); n           } n         } n         break; n       } n       positions[id] = make_pair(i_occup, i_start); n     } else { n       pair<int, int> p = positions[id]; n       i_occup = p.first; n       i_start = p.second; n       positions.erase(id); n       map<int, int>::iterator it = occup[i_occup].upper_bound(i_start); n       --it; n       if (it->first == i_start) { n         int last = it->second; n         occup[i_occup].erase(it); n         if (last != i_start) occup[i_occup][i_start + 1] = last; n       } else if (it->second == i_start) n         it->second--; n       else { n         int last = it->second; n         it->second = i_start - 1; n         occup[i_occup][i_start + 1] = last; n       } n     } n   } n   cout << counter << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n bool sortbysec(const pair<int, int> &a, const pair<int, int> &b) { n   return (a.second < b.second); n } n int main() { n   ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0); n   int t; n   cin >> t; n   while (t--) { n     int n, i, j, alp[27] = {0}, flg = 1; n     cin >> n; n     string s[n + 1]; n     for (i = 0; i < n; i++) cin >> s[i]; n     for (i = 0; i < n; i++) { n       for (j = 0; j < s[i].size(); j++) { n         alp[s[i][j] * 1 - 96]++; n       } n     } n     for (i = 1; i < 27; i++) { n       if (alp[i] != 0) { n         if (alp[i] % n != 0) { n           flg = 0; n           break; n         } n       } n     } n     if (flg == 1) n       cout <<  YES n ; n     else n       cout <<  NO n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 10000 + 10; n int a[maxn]; n int main() { n   int i, j, k, m, n; n   cin >> n; n   int sum = 0; n   while (n > 0) { n     if (n % 8 == 1) sum++; n     n /= 8; n   } n   cout << sum << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios::sync_with_stdio(false); n   cout.setf(ios::fixed); n   cout.precision(20); n   int n; n   cin >> n; n   if (n == 5) return 0 * puts( 1 ); n   cout << n % 3 + 1 <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n; n vector<int> edge[200010]; n int dp[200010][2][2]; n int leaf[200010]; n void dfsLeaf(int u) { n   if (edge[u].size() == 0) { n     leaf[u] = 1; n     return; n   } n   for (int i = 0; i < edge[u].size(); ++i) { n     int v = edge[u][i]; n     dfsLeaf(v); n     leaf[u] += leaf[v]; n   } n } n void dfsDp(int u) { n   if (edge[u].size() == 0) { n     dp[u][0][1] = dp[u][1][0] = dp[u][0][0] = dp[u][1][1] = 1; n     return; n   } n   dp[u][0][0] = 0; n   dp[u][0][1] = 0; n   dp[u][1][0] = 1; n   dp[u][1][1] = 1000000000; n   for (int i = 0; i < edge[u].size(); ++i) { n     int v = edge[u][i]; n     dfsDp(v); n     dp[u][0][0] = max(dp[u][0][0], dp[v][1][0] + leaf[u] - leaf[v]); n     dp[u][0][1] += dp[v][1][1]; n     dp[u][1][0] += dp[v][0][0] - 1; n     dp[u][1][1] = min(dp[u][1][1], dp[v][0][1]); n   } n } n int main() { n   int u, v; n   while (cin >> n) { n     for (int i = 1; i < n; ++i) { n       cin >> u >> v; n       edge[u].push_back(v); n     } n     memset(leaf, 0, sizeof(leaf)); n     dfsLeaf(1); n     dfsDp(1); n     cout << dp[1][0][0] <<     << dp[1][0][1] << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   string a, b; n   cin >> a >> b; n   int x = 0, y = 0; n   int n = b.length(); n   while (x < n && a[x] == b[x]) x++; n   while (y < n && a[n - y] == b[n - 1 - y]) y++; n   int res = x + y + 2 - (n + 1); n   res = max(0, min(res, n + 1)); n   cout << res << endl; n   int start = n - y + 1; n   for (int i = 0; i < res; i++) cout << (start + i) <<    ; n   cout << endl; n   return 0; n } n


#include <bits/stdc++.h> n #pragma optimize( Ofast ) n using namespace std; n using ll = long long; n const int N = 2e5 + 5; n vector<int> g[N]; n bool vis[N]; n int cnt; n void dfs(int s) { n   ++cnt; n   vis[s] = true; n   for (auto it : g[s]) { n     if (!vis[it]) { n       dfs(it); n     } n   } n } n int main() { n   ios_base::sync_with_stdio(0); n   int n; n   cin >> n; n   for (int i = 0; i < n; ++i) { n     int x; n     cin >> x; n     g[i + 1].push_back(x); n     g[x].push_back(i + 1); n   } n   deque<ll> d; n   for (int i = 1; i <= n; ++i) { n     if (!vis[i]) { n       cnt = 0; n       dfs(i); n       d.push_back(cnt); n     } n   } n   sort(d.rbegin(), d.rend()); n   if (d.size() == 1) d.push_back(0); n   ll ans = (d[0] + d[1]) * (d[0] + d[1]); n   for (int i = 2; i < d.size(); ++i) { n     ans += d[i] * d[i]; n   } n   cout << ans <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long i, j, n, t, sum = 0, k, c1 = 1; n int main() { n   cin >> n; n   vector<string> c(n); n   vector<string> s(n); n   for (i = 0; i < n; i++) { n     cin >> c[i] >> s[i]; n   } n   for (i = 1; i < n; i++) { n     k = 0; n     for (j = i - 1; j >= 0; j--) { n       if (c[i] == c[j] && s[i] == s[j]) { n         k++; n         if (k != 0) { n           break; n         } n       } n     } n     if (k == 0) { n       c1++; n     } n   } n   cout << c1; 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 testCases = 1; n   cin >> testCases; n   while (testCases--) { n     int n; n     cin >> n; n     vector<int> a(n); n     for (int i = 0; i < n; i++) { n       cin >> a[i]; n     } n     if (n % 2 == 0) { n       for (int i = 0; i < n; i += 2) { n         cout << a[i + 1] <<     << -a[i] <<    ; n       } n     } else { n       for (int i = 0; i < n - 3; i += 2) { n         cout << a[i + 1] <<     << -a[i] <<    ; n       } n       if (a[n - 3] + a[n - 2] != 0) { n         cout << a[n - 1] <<     << a[n - 1] <<     << -(a[n - 3] + a[n - 2]); n       } else if (a[n - 2] + a[n - 1] != 0) { n         cout << -(a[n - 2] + a[n - 1]) <<     << a[n - 3] <<     << a[n - 3]; n       } else { n         cout << a[n - 2] <<     << -(a[n - 1] + a[n - 3]) <<     << a[n - 2]; n       } n     } n     cout << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long inf = 1e9; n int main() { n   std::ios::sync_with_stdio(false); n   int n; n   cin >> n; n   long long noemer = 0; n   for (long long i = 1; i <= n; i++) { n     noemer += (n + 1 - i) * (n + 1 - i); n   } n   string s1, s2; n   cin >> s1; n   cin >> s2; n   double ans = 0.0; n   long long teller = 0; n   for (char ch =  A ; ch <=  Z ; ch++) { n     long long x, y, z; n     x = 0; n     y = 0; n     z = 0; n     for (int i = n - 1; i >= 0; i--) { n       if (s2[i] == ch) { n         z += n - i; n       } n     } n     x = z; n     if (s1[0] == ch) { n       if (teller >= inf) { n         ans += 1.0 * teller / noemer; n         teller = 0; n       } n       teller += x; n     } n     for (int i = 1; i < n; i++) { n       if (s2[i - 1] == ch) { n         y += i; n         z -= (n + 1 - i); n       } n       long long now = x - y + z; n       if (s1[i] == ch) { n         if (teller >= inf) { n           ans += 1.0 * teller / noemer; n           teller = 0; n         } n         teller += now; n       } n       x = now; n     } n   } n   ans += 1.0 * teller / noemer; n   cout << fixed << setprecision(12) << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int t, a, b, c, arr[5], count = 0; n   cin >> t; n   for (int i = 0; i < t; i++) { n     cin >> arr[0] >> arr[1] >> arr[2]; n     sort(arr, arr + 3); n     c = arr[0]; n     b = arr[1]; n     a = arr[2]; n     if (a > 0) { n       a--; n       count++; n     } n     if (b > 0) { n       b -= 1; n       count++; n     } n     if (c > 0) { n       c--; n       count++; n     } n     if (a > 0 && b > 0) { n       a--; n       b--; n       count++; n     } n     if (a > 0 && c > 0) { n       a--; n       c--; n       count++; n     } n     if (b > 0 && c > 0) { n       c--; n       b--; n       count++; n     } n     if (a > 0 && b > 0 && c > 0) { n       c--; n       a--; n       b--; n       count++; n     } n     cout << count << endl; n     count = 0; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const long long mod = 1000000007; n const double PI = 3.141592653589793238463; n const int N = 1e5 + 100; n vector<int> v[N]; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   ; n   int x; n   cin >> x; n   int r, l; n   for (int i = 1; i < x; i++) { n     cin >> r >> l; n     v[r].push_back(l); n     v[l].push_back(r); n   } n   bool ans = true; n   for (int i = 1; i <= x; i++) { n     if (v[i].size() == 2) { n       ans = false; n       break; n     } n   } n   cout << (ans ?  YES  :  NO ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 1e5 + 10; n struct edge { n   int u, v, st, nt; n } e[MAXN * 2]; n vector<int> lis; n int hd[MAXN], etot, used[MAXN], in[MAXN], u[MAXN], v[MAXN]; n void addedge(int u, int v, int st) { n   e[etot].u = u; n   e[etot].v = v; n   e[etot].st = st; n   e[etot].nt = hd[u]; n   hd[u] = etot++; n } n void dfs(int p) { n   used[p] = 1; n   lis.push_back(p); n   for (int i = hd[p]; ~i; i = e[i].nt) { n     if (used[e[i].v]) continue; n     dfs(e[i].v); n   } n } n queue<int> Q; n int topsort() { n   for (int i = 0; i < (int)lis.size(); i++) n     if (!in[lis[i]]) Q.push(lis[i]); n   int cnt = 0, u; n   for (; !Q.empty();) { n     cnt++; n     u = Q.front(); n     Q.pop(); n     for (int i = hd[u]; ~i; i = e[i].nt) { n       if (!e[i].st) continue; n       in[e[i].v]--; n       if (!in[e[i].v]) Q.push(e[i].v); n     } n   } n   return cnt == (int)lis.size(); n } n int main() { n   int n, m; n   scanf( %d%d , &n, &m); n   etot = 0; n   memset(hd, -1, sizeof(hd)); n   for (int i = 0; i < m; i++) { n     scanf( %d%d , u + i, v + i); n     addedge(u[i], v[i], 1); n     addedge(v[i], u[i], 0); n     in[v[i]]++; n   } n   int ans = 0; n   for (int i = 1; i < n + 1; i++) { n     lis.clear(); n     if (!used[i]) dfs(i); n     if ((int)lis.size() == 0) continue; n     ans += (int)lis.size() - topsort(); n   } n   printf( %d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 2e5 + 9; n int a[N], n; n double ans(double x) { n   double arr[N]; n   for (int i = 0; i < n; i++) arr[i] = a[i] - x; n   double neg = 0; n   double pos = 0; n   double ans = 0; n   for (int i = 0; i < n; i++) { n     if (neg > 0) neg = 0; n     if (pos < 0) pos = 0; n     neg += arr[i]; n     pos += arr[i]; n     ans = max(ans, max(-neg, pos)); n   } n   return ans; n } n int main() { n   cin >> n; n   for (int i = 0; i < n; i++) cin >> a[i]; n   double l = -1e5 - 9, r = 1e5 + 9; n   for (int i = 0; i < 100; i++) { n     double m1 = l + (r - l) / 3, m2 = r - (r - l) / 3; n     if (ans(m1) > ans(m2)) n       l = m1; n     else n       r = m2; n   } n   cout << setprecision(12) << fixed << ans(l) << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(false); n   long long int n, a, b, c, d, e, f, g, h, i, j, x, y, count = 0; n   cin >> n >> x >> y; n   n = n / 2; n   if ((x == n || x == n + 1) && (y == n || y == n + 1)) n     cout <<  NO ; n   else n     cout <<  YES ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct Pt { n   long long int x, y; n   friend ostream& operator<<(ostream& os, const Pt& a) { n     os << a.x <<     << a.y; n     return os; n   } n }; n struct R { n   long long int a, b, c; n }; n int main() { n   ios::sync_with_stdio(false); n   long long int N, K; n   cin >> N >> K; n   vector<long long int> w[4]; n   for (long long int i = 0; i < (long long int)(4); ++i) n     w[i] = vector<long long int>(N); n   vector<Pt> wp[4]; n   for (long long int i = 0; i < (long long int)(4); ++i) wp[i] = vector<Pt>(N); n   for (long long int i = 0; i < (long long int)(4); ++i) n     for (int j = 0; j < N; ++j) wp[i][j] = {i + 1, j + 1}; n   for (long long int i = 0; i < (long long int)(4); ++i) n     for (int j = 0; j < N; ++j) cin >> w[i][j]; n   vector<long long int> a[2]; n   for (long long int i = 0; i < (long long int)(2); ++i) n     a[i] = vector<long long int>(2 * N); n   vector<Pt> p[2]; n   for (long long int i = 0; i < (long long int)(2); ++i) n     p[i] = vector<Pt>(2 * N); n   for (long long int i = 0; i < (long long int)(N); ++i) a[0][i] = w[0][i]; n   for (long long int i = 0; i < (long long int)(N); ++i) n     a[0][N + i] = w[3][N - 1 - i]; n   for (long long int i = 0; i < (long long int)(N); ++i) a[1][i] = w[1][i]; n   for (long long int i = 0; i < (long long int)(N); ++i) n     a[1][N + i] = w[2][N - 1 - i]; n   for (long long int i = 0; i < (long long int)(N); ++i) p[0][i] = wp[0][i]; n   for (long long int i = 0; i < (long long int)(N); ++i) n     p[0][N + i] = wp[3][N - 1 - i]; n   for (long long int i = 0; i < (long long int)(N); ++i) p[1][i] = wp[1][i]; n   for (long long int i = 0; i < (long long int)(N); ++i) n     p[1][N + i] = wp[2][N - 1 - i]; n   N *= 2; n   vector<R> res; n   for (int t = 0; t < 151; ++t) { n     long long int c = 0; n     for (long long int i = 0; i < (long long int)(N); ++i) { n       if (a[1][i] != 0 && a[1][i] == a[0][i]) { n         res.push_back({a[1][i], p[0][i].x, p[0][i].y}); n         a[1][i] = 0; n       } n       if (a[1][i] == 0) c++; n     } n     if (c == N) break; n     if (c == 0 || t == 150) { n       cout << -1 << endl; n       return 0; n     } n     int j = 0; n     for (; j < N; ++j) n       if (a[1][j] == 0) break; n     for (int i = 0; i < N - 1; ++i) { n       long long int w = (j + i) % N; n       long long int q = (j + i + 1) % N; n       if (a[1][q] != 0) { n         res.push_back({a[1][q], p[1][w].x, p[1][w].y}); n         a[1][w] = a[1][q]; n         a[1][q] = 0; n       } n     } n   } n   cout << res.size() << endl; n   for (long long int i = 0; i < (long long int)(res.size()); ++i) { n     cout << res[i].a <<     << res[i].b <<     << res[i].c <<     << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T> n void pr(vector<T> &v) { n   for (int i = 0; i < (int)(v).size(); i++) cout << v[i] <<    ; n   cout <<   n ; n   ; n } n template <typename T> n void pr(vector<vector<T>> &v) { n   for (int i = 0; i < (int)(v).size(); i++) { n     pr(v[i]); n   } n } n template <typename T> n void re(T &x) { n   cin >> x; n } n template <typename T> n void re(vector<T> &a) { n   for (int i = 0; i < (int)(a).size(); i++) re(a[i]); n } n template <class Arg, class... Args> n void re(Arg &first, Args &...rest) { n   re(first); n   re(rest...); n } n template <typename T> n void pr(T x) { n   cout << x <<   n ; n   ; n } n template <class Arg, class... Args> n void pr(const Arg &first, const Args &...rest) { n   cout << first <<    ; n   pr(rest...); n   cout <<   n ; n   ; n } n void ps() { n   cout <<   n ; n   ; n } n template <class T, class... Ts> n void ps(const T &t, const Ts &...ts) { n   cout << t; n   if (sizeof...(ts)) cout <<    ; n   ps(ts...); n } n const long long MOD = 1000000007; n long double PI = 4 * atan(1); n long double eps = 1e-12; n int LPS[200005] = {}; n vector<pair<long long, char>> t; n vector<pair<long long, char>> s; n vector<pair<long long, char>> tcp; n vector<pair<long long, char>> scp; n void computeLPS() { n   int M = scp.size(); n   int i = 1; n   int j = 0; n   LPS[0] = 0; n   while (i < M) { n     if (scp[j] == scp[i]) { n       j++; n       LPS[i] = j; n       i++; n     } else { n       if (j == 0) { n         LPS[i] = 0; n         i++; n       } else { n         j = LPS[j - 1]; n       } n     } n   } n } n vector<int> KMP() { n   int M = (int)scp.size(); n   int N = (int)tcp.size(); n   computeLPS(); n   int i = 0; n   int j = 0; n   vector<int> matches; n   while (i < N) { n     if (scp[j] == tcp[i]) { n       i++; n       j++; n     } n     if (j == M) { n       matches.push_back(i - j); n       j = LPS[j - 1]; n     } else if (i < N && scp[j] != tcp[i]) { n       if (j != 0) { n         j = LPS[j - 1]; n       } else { n         i++; n       } n     } n   } n   return matches; n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   int n, m; n   cin >> n >> m; n   for (int i = 0; i < n; i++) { n     long long k; n     cin >> k; n     char dash; n     cin >> dash; n     char c; n     cin >> c; n     if (t.size() == 0 || c != t.back().second) { n       t.push_back(make_pair(k, c)); n     } else { n       t.back().first += k; n     } n   } n   for (int i = 0; i < m; i++) { n     long long k; n     cin >> k; n     char dash; n     cin >> dash; n     char c; n     cin >> c; n     if (s.size() == 0 || c != s.back().second) { n       s.push_back(make_pair(k, c)); n     } else { n       s.back().first += k; n     } n   } n   long long ans = 0; n   if (s.size() == 1) { n     for (int i = 0; i < t.size(); i++) { n       if (t[i].second == s[0].second) { n         ans += max(0ll, t[i].first - s[0].first + 1ll); n       } n     } n     cout << ans <<   n ; n     ; n     return 0; n   } else if (s.size() == 2) { n     for (int i = 0; i < t.size() - 1; i++) { n       if (t[i].second == s[0].second && t[i + 1].second == s[1].second && n           t[i].first >= s[0].first && t[i + 1].first >= s[1].first) { n         ans += 1; n       } n     } n     cout << ans <<   n ; n     ; n     return 0; n   } n   for (int i = 1; i < t.size() - 1; i++) tcp.push_back(t[i]); n   for (int i = 1; i < s.size() - 1; i++) scp.push_back(s[i]); n   vector<int> matches = KMP(); n   for (int ind : matches) { n     if (t[ind].first >= s[0].first && t[ind].second == s[0].second && n         t[ind + s.size() - 1].first >= s.back().first && n         t[ind + s.size() - 1].second == s.back().second) { n       ans++; n     } n   } n   cout << ans <<   n ; n   ; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int length; n   cin >> length; n   int query; n   cin >> query; n   string result; n   cin >> result; n   for (int i = 0; i < query; ++i) { n     int left; n     int right; n     cin >> left >> right; n     char which; n     char what; n     cin >> which >> what; n     for (int j = left; j <= right; ++j) n       if (result[j - 1] == which) result[j - 1] = what; n   } n   cout << result; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   long long int n, x, y, cn = 0, ans = 0; n   string s; n   cin >> n >> x >> y >> s; n   for (int i = 0; i < n; i++) { n     if (s[i] ==  0 ) n       cn++; n     else { n       if (cn) ans++; n       cn = 0; n     } n   } n   if (cn) ans++; n   if (!ans) n     cout << 0 << endl; n   else n     cout << (ans - 1) * min(x, y) + y << endl; n   return 0; n } n


#include <bits/stdc++.h> n long long MOD = 1000000007; n using namespace std; n bool sortbysec(const pair<long long, long long> &a, n                const pair<long long, long long> &b) { n   return (a.second < b.second); n } n long long POW(long long a, long long b) { n   if (b == 0) return 1; n   if (b == 1) return a % MOD; n   long long temp = POW(a, b / 2); n   if (b % 2 == 0) n     return (temp * temp) % MOD; n   else n     return (((temp * temp) % MOD) * a) % MOD; n } n vector<char> capl = { A ,  B ,  C ,  D ,  E ,  F ,  G ,  H ,  I , n                       J ,  K ,  L ,  M ,  N ,  O ,  P ,  Q ,  R , n                       S ,  T ,  U ,  V ,  W ,  X ,  Y ,  Z }; n vector<char> sml = { a ,  b ,  c ,  d ,  e ,  f ,  g ,  h ,  i , n                      j ,  k ,  l ,  m ,  n ,  o ,  p ,  q ,  r , n                      s ,  t ,  u ,  v ,  w ,  x ,  y ,  z }; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   ; n   int t; n   cin >> t; n   while (t--) { n     int n; n     cin >> n; n     int arr[n]; n     map<int, int> m; n     for (int i = 1; i < 201; i++) { n       m[i]++; n     } n     for (int i = 0; i < n; i++) { n       cin >> arr[i]; n       m[arr[i]]--; n     } n     int b[2 * n]; n     for (int i = 0; i < n; i++) { n       b[2 * i] = arr[i]; n       for (auto x : m) { n         if (x.first > arr[i] && x.second != 0) { n           b[2 * i + 1] = x.first; n           m[x.first] = 0; n           break; n         } n       } n     } n     int c[2 * n], f = 0; n     copy(b, b + 2 * n, c); n     sort(c, c + 2 * n); n     for (int i = 0; i < 2 * n; i++) { n       if (c[i] != i + 1) { n         f = 1; n         break; n       } n     } n     if (f == 1) { n       cout <<  -1 n ; n     } else { n       for (int i = 0; i < 2 * n; i++) { n         cout << b[i] <<    ; n       } n       cout <<   n ; n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T> n T sqr(T x) { n   return x * x; n } n template <typename T> n T abs(T x) { n   return x < 0 ? -x : x; n } n template <typename T> n T gcd(T a, T b) { n   return b ? gcd(b, a % b) : a; n } n const int MAXN = 1e5 + 10; n int n; n int sum = 0; n pair<int, int> a[200000]; n int col[MAXN]; n int cost[300]; n int ans = 2000000007; n inline bool cmp(pair<int, int> a, pair<int, int> b) { n   if (a.first < b.first) return true; n   if (a.first == b.first) return a.second > b.second; n   return false; n } n int main() { n   ios_base ::sync_with_stdio(false); n   cin >> n; n   for (int i = 1; i <= n; i++) cin >> a[i].first, col[a[i].first]++; n   for (int i = 1; i <= n; i++) cin >> a[i].second, cost[a[i].second]++; n   sort(a + 1, a + n + 1); n   if (n == 1) { n     cout << 0; n     return 0; n   } n   if (n == 2) { n     if (a[1].first != a[2].first) n       cout << min(a[1].second, a[2].second); n     else n       cout << 0; n     return 0; n   } n   a[0].first = 2000000007; n   int s = 0; n   for (int i = n; i; i--) { n     s += a[i].second; n     cost[a[i].second]--; n     if (a[i].first != a[i - 1].first) { n       int c = 0, del = max(0, i - col[a[i].first]); n       for (int j = 1; j <= 200; j++) { n         int r = min(del, cost[j]); n         c += j * r, del -= r; n         if (!del) break; n       } n       ans = min(ans, sum + c); n       sum += s, s = 0; n     } n   } n   cout << ans; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 1e5 + 10; n struct Node { n   int is, id, x; n   bool operator<(const Node &a) const { n     if (x != a.x) n       return x < a.x; n     else n       return is < a.is; n   } n }; n int a[maxn * 4], s[maxn], p[maxn], inc[maxn], b[maxn], treea[maxn * 4], n     treeb[maxn * 4], pb[maxn]; n int ans[maxn]; n map<int, int> ma; n int cnt = 0, tt = 0; n Node que[maxn * 3]; n int getid(int x) { n   x = -x; n   return ma[x]; n } n void add1(int x, int y) { n   for (int i = x; i < 4 * maxn; i += (i & -i)) treea[i] += y; n } n void add2(int x, int y) { n   for (int i = x; i < 4 * maxn; i += (i & -i)) treeb[i] += -y; n } n int quer1(int x) { n   int res = 0; n   for (int i = x; i >= 1; i -= (i & -i)) res += treea[i]; n   return res; n } n int quer2(int x) { n   int res = 0; n   for (int i = x; i >= 1; i -= (i & -i)) res += treeb[i]; n   return res; n } n int main() { n   int n, m; n   scanf( %d %d , &n, &m); n   for (int i = 1; i <= n; i++) scanf( %d , &p[i]); n   for (int i = 1; i <= n; i++) scanf( %d , &s[i]); n   for (int i = 1; i <= n; i++) scanf( %d , &b[i]); n   for (int i = 1; i <= m; i++) scanf( %d , &inc[i]); n   for (int i = 1; i <= m; i++) scanf( %d , &pb[i]); n   for (int i = 1; i <= n; i++) { n     a[++cnt] = -(p[i] + b[i] - 1), a[++cnt] = -(b[i] - p[i]); n     que[++tt] = Node{0, i, p[i]}; n     que[++tt] = Node{2, i, s[i]}; n   } n   for (int i = 1; i <= m; i++) { n     a[++cnt] = -(inc[i] + pb[i]), a[++cnt] = -(pb[i] - inc[i]); n     que[++tt] = Node{1, i, inc[i]}; n   } n   sort(que + 1, que + 1 + tt); n   sort(a + 1, a + cnt + 1); n   int pz = 1; n   for (int i = 1; i <= cnt; i++) n     if (ma[a[i]] == 0) ma[a[i]] = pz++; n   for (int i = 1; i <= tt; i++) { n     Node now = que[i]; n     if (now.is == 0) { n       add1(getid(b[now.id] - p[now.id]), 1); n       add2(getid(p[now.id] + b[now.id] - 1), 1); n     } else if (now.is == 1) { n       ans[now.id] = quer1(getid(pb[now.id] - inc[now.id])) + n                     quer2(getid(inc[now.id] + pb[now.id])); n     } else if (now.is == 2) { n       add1(getid(b[now.id] - p[now.id]), -1); n       add2(getid(p[now.id] + b[now.id] - 1), -1); n     } n   } n   for (int i = 1; i <= m; i++) { n     printf( %d  , ans[i]); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   int p, q, l, r; n   cin >> p >> q >> l >> r; n   vector<pair<int, int> > z_timing, x_timing; n   int start, stop; n   for (int i = 0; i < p; i++) { n     cin >> start >> stop; n     z_timing.push_back(make_pair(start, stop)); n   } n   for (int i = 0; i < q; i++) { n     cin >> start >> stop; n     x_timing.push_back(make_pair(start, stop)); n   } n   int answer = 0; n   for (int i = l; i <= r; i++) { n     for (int j = 0; j < q; j++) { n       bool has_intersected = false; n       start = x_timing[j].first + i; n       stop = x_timing[j].second + i; n       for (int k = 0; k < p; k++) { n         if (start <= z_timing[k].second && stop >= z_timing[k].first) { n           answer++; n           has_intersected = true; n           break; n         } n       } n       if (has_intersected == true) { n         break; n       } n     } n   } n   cout << answer; n   return 0; n } n


// vaziat meshki-ghermeze ! n #include <bits/stdc++.h> n  n #define pb push_back n #define F first n #define S second n #define all(x) x.begin(), x.end() n #define debug(x) cerr << #x <<   :   << x <<   n  n  n using namespace std; n  n typedef long long ll; n typedef long double ld; n typedef string str; n typedef pair<ll, ll> pll; n  n const ll Mod = 1000000007LL; n const int N = 2e5 + 10; n const ll Inf = 2242545357980376863LL; n const ll Log = 30; n  n pll A[N]; n ll dis(ll i, ll j){ n  return (A[i].F - A[j].F)*(A[i].F - A[j].F) + (A[i].S - A[j].S)*(A[i].S - A[j].S); n } n ll mk[N]; n int main(){ n  ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); n  int n; n  cin >> n; n  pll mn = {Inf, Inf}; n  ll idx = -1; n  for(int i = 1; i <= n; i++){ n   cin >> A[i].F >> A[i].S; n   if(A[i] < mn){ n    mn = A[i]; n    idx = i; n   } n  } n  cout << idx <<    ; n  mk[idx] = 1; n  for(int i = 2; i <= n; i++){ n   ll mx = -1; n   ll id = -1; n   for(int j = 1; j <= n; j++){ n    if(mk[j]) continue; n    if(mx < dis(idx, j)){ n     mx = dis(idx, j); n     id = j; n    } n   } n   idx = id; n   mk[id] = 1; n   cout << idx <<    ; n  } n  cout <<   n ; n  return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <class T> n inline void rd(T &x) { n   x = 0; n   char c = getchar(); n   int f = 1; n   while (!isdigit(c)) { n     if (c ==  - ) f = -1; n     c = getchar(); n   } n   while (isdigit(c)) x = x * 10 -  0  + c, c = getchar(); n   x *= f; n } n template <class T> n inline void cmax(T &x, T y) { n   if (x < y) x = y; n } n template <class T> n inline void cmin(T &x, T y) { n   if (y < x) x = y; n } n const int N = 5e5 + 10, M = N << 1; n int n, m; n struct Tree { n   int F[M], fa[M], ch[M][2], ti[M], sz[M], ncnt; n   int dfn[M], lb[M], rb[M], id; n   void init() { n     ncnt = n; n     for (int i = 1; i <= n; ++i) fa[i] = i, sz[i] = 1; n   } n   int find(int x) { return fa[x] == x ? x : fa[x] = find(fa[x]); } n   void un(int x, int y, int t) { n     int fx = find(x), fy = find(y), u = ++ncnt; n     fa[u] = fa[fx] = fa[fy] = u, ti[u] = t, sz[u] = sz[fx] + sz[fy]; n     ch[u][0] = fx, ch[u][1] = fy; n   } n   void dfs(int u) { n     if (!ch[u][0]) { n       lb[u] = rb[u] = dfn[u] = ++id; n     } else { n       lb[u] = id + 1; n       dfs(ch[u][0]), dfs(ch[u][1]); n       F[ch[u][0]] = F[ch[u][1]] = u; n       rb[u] = id; n     } n   } n   void build() { n     for (int i = 1; i <= ncnt; ++i) n       if (find(i) == i) dfs(i); n     for (int i = 1; i <= n; ++i) fa[i] = i; n   } n   int get(int x, int t) { n     while (F[fa[x]] && ti[F[fa[x]]] <= t) fa[x] = F[fa[x]]; n     return fa[x]; n   } n } A, B; n namespace seg { n int cov[N << 2]; n inline void push_down(int c) { n   if (cov[c]) { n     cmax(cov[c << 1], cov[c]); n     cmax(cov[c << 1 | 1], cov[c]); n     cov[c] = 0; n   } n } n void upd(int l, int r, int c, int L, int R, int v) { n   if (L <= l && R >= r) return (void)(cov[c] = v); n   int mid = l + r >> 1; n   push_down(c); n   if (L <= mid) upd(l, mid, c << 1, L, R, v); n   if (R > mid) upd(mid + 1, r, c << 1 | 1, L, R, v); n } n int query(int l, int r, int c, int v) { n   if (l == r) return cov[c]; n   int mid = l + r >> 1; n   push_down(c); n   if (v <= mid) n     return query(l, mid, c << 1, v); n   else n     return query(mid + 1, r, c << 1 | 1, v); n } n }  // namespace seg n namespace Bit { n long long c[N]; n void add(int i, int t) { n   for (; i <= n; i += i & -i) c[i] += t; n } n void add(int l, int r, int t) { add(l, t), add(r + 1, -t); } n long long query(int i) { n   long long ans = 0; n   for (; i; i -= i & -i) ans += c[i]; n   return ans; n } n }  // namespace Bit n struct OPT { n   int ty, t, x; n   OPT(int ty = 0, int t = 0, int x = 0) : ty(ty), t(t), x(x) {} n }; n vector<OPT> Q; n long long ans[N]; n struct Que { n   int p, id, sgn; n   Que(int p = 0, int id = 0, int sgn = 0) : p(p), id(id), sgn(sgn) {} n }; n vector<Que> R[N]; n char ty[20]; n int main() { n   rd(n), rd(m); n   A.init(), B.init(); n   for (int i = 1, x, y; i <= m; ++i) { n     scanf( %s , ty); n     if (ty[0] ==  U ) { n       rd(x), rd(y); n       A.un(x, y, i); n     } else if (ty[0] ==  M ) { n       rd(x), rd(y); n       B.un(x, y, i); n     } else { n       rd(x); n       Q.push_back(OPT(ty[0] ==  A  ? 1 : (ty[0] ==  Z  ? 2 : 3), i, x)); n     } n   } n   A.build(), B.build(); n   int qtot = 0; n   for (int i = 0; i < Q.size(); ++i) n     if (Q[i].ty == 2) { n       int x = B.get(Q[i].x, Q[i].t); n       seg::upd(1, n, 1, B.lb[x], B.rb[x], Q[i].t); n     } else if (Q[i].ty == 3) { n       R[Q[i].t].push_back(Que(A.dfn[Q[i].x], ++qtot, 1)); n       int pre = seg::query(1, n, 1, B.dfn[Q[i].x]); n       if (pre) R[pre].push_back(Que(A.dfn[Q[i].x], qtot, -1)); n     } n   int q = 0; n   for (int i = 1; i <= m; ++i) { n     while (q < Q.size() && Q[q].t <= i) { n       if (Q[q].ty == 1) { n         int anc = A.get(Q[q].x, Q[q].t); n         Bit::add(A.lb[anc], A.rb[anc], A.sz[anc]); n       } n       q++; n     } n     for (int j = 0; j < R[i].size(); ++j) n       ans[R[i][j].id] += R[i][j].sgn * Bit::query(R[i][j].p); n   } n   for (int i = 1; i <= qtot; ++i) printf( %lld n , ans[i]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int SZ = 1e6 + 10; n const int INF = 1e9 + 10; n const int mod = 998244353; n const long double eps = 1e-8; n long long read() { n   long long n = 0; n   char a = getchar(); n   bool flag = 0; n   while (a >  9  || a <  0 ) { n     if (a ==  - ) flag = 1; n     a = getchar(); n   } n   while (a <=  9  && a >=  0 ) { n     n = n * 10 + a -  0 , a = getchar(); n   } n   if (flag) n = -n; n   return n; n } n struct seg { n   int l, r, mx, add; n } tree[SZ * 4]; n void update(int p) { tree[p].mx = max(tree[p << 1].mx, tree[p << 1 | 1].mx); } n void build(int p, int l, int r) { n   tree[p].l = l; n   tree[p].r = r; n   if (l == r) return; n   int mid = l + r >> 1; n   build(p << 1, l, mid); n   build(p << 1 | 1, mid + 1, r); n   update(p); n } n void pushadd(int p, int d) { n   tree[p].add += d; n   tree[p].mx += d; n } n void pushdown(int p) { n   if (tree[p].add) { n     pushadd(p << 1, tree[p].add); n     pushadd(p << 1 | 1, tree[p].add); n     tree[p].add = 0; n   } n } n void change(int p, int l, int r, int d) { n   if (l <= tree[p].l && tree[p].r <= r) { n     pushadd(p, d); n     return; n   } n   pushdown(p); n   int mid = tree[p].l + tree[p].r >> 1; n   if (l <= mid) change(p << 1, l, r, d); n   if (mid < r) change(p << 1 | 1, l, r, d); n   update(p); n } n struct haha { n   int pos, x, y, d; n } l[SZ]; n bool cmp(haha a, haha b) { return a.pos < b.pos; } n int lsh[SZ]; n int main() { n   int n = read(), m = read(), tot = 0; n   for (int i = 1; i <= n; i++) { n     int a = read(), b = read(), x, y; n     x = a + b; n     y = a - b; n     l[++tot] = (haha){x - m, y - m, y + m, 1}; n     l[++tot] = (haha){x + m + 1, y - m, y + m, -1}; n   } n   for (int i = 1; i <= tot; i++) { n     lsh[++lsh[0]] = l[i].x; n     lsh[++lsh[0]] = l[i].y; n   } n   sort(lsh + 1, lsh + 1 + lsh[0]); n   lsh[0] = unique(lsh + 1, lsh + 1 + lsh[0]) - lsh - 1; n   for (int i = 1; i <= tot; i++) { n     l[i].x = lower_bound(lsh + 1, lsh + 1 + lsh[0], l[i].x) - lsh; n     l[i].y = lower_bound(lsh + 1, lsh + 1 + lsh[0], l[i].y) - lsh; n   } n   int ans = 0; n   build(1, 1, lsh[0]); n   sort(l + 1, l + 1 + tot, cmp); n   for (int i = 1, lst = 1; i <= tot; i++) { n     if (i == tot || l[i + 1].pos != l[i].pos) { n       for (int j = lst; j <= i; j++) { n         change(1, l[j].x, l[j].y, l[j].d); n       } n       ans = max(ans, tree[1].mx); n       lst = i + 1; n     } n   } n   cout << ans << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, k; n   cin >> n >> k; n   vector<int> a(n); n   for (int i = 0; i < n; ++i) { n     cin >> a[i]; n   } n   vector<int> poss; n   for (int i = 0; i < n; ++i) { n     int x = a[i]; n     while (x > 0) { n       poss.push_back(x); n       x /= 2; n     } n   } n   int ans = 1e9; n   for (auto res : poss) { n     vector<int> cnt; n     for (int i = 0; i < n; ++i) { n       int x = a[i]; n       int cur = 0; n       while (x > res) { n         x /= 2; n         ++cur; n       } n       if (x == res) { n         cnt.push_back(cur); n       } n     } n     if (int(cnt.size()) < k) continue; n     sort(cnt.begin(), cnt.end()); n     ans = min(ans, accumulate(cnt.begin(), cnt.begin() + k, 0)); n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void solve() { n   int n; n   cin >> n; n   vector<int> v(n); n   for (int i = 0; i < n; i++) cin >> v[i]; n   sort(v.begin(), v.end()); n   int l = v[n - 1], q = v[n - 2]; n   int d = 0; n   for (int i = 0; i < n - 2; i++) { n     if (q > i + 1) d = i + 1; n   } n   cout << d << endl; n } n int main() { n   std::ios_base::sync_with_stdio(false); 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 bool isPrime(long long n) { n   if (n <= 1) return false; n   if (n <= 3) return true; n   if (n % 2 == 0 || n % 3 == 0) return false; n   for (int i = 5; i * i <= n; i = i + 6) n     if (n % i == 0 || n % (i + 2) == 0) return false; n   return true; n } n int main() { n   long long p, y; n   cin >> p >> y; n   int flag = 1; n   for (long long j = y; j > p; j--) { n     flag = 0; n     for (long long k = 2; k <= p && k * k <= j; k++) { n       if (j % k == 0) { n         flag = 1; n         break; n       } n     } n     if (flag == 0) { n       cout << j; n       return 0; n     } n   } n   cout << -1; n } n


#include <bits/stdc++.h> n using namespace std; n bool b[4000000]; n int main() { n   int n, cnt, x[2007], y[2007]; n   cin >> n; n   for (int i = 1; i <= n; i++) { n     cin >> x[i]; n     b[x[i]] = 1; n   } n   for (int i = 1; i <= n; i++) { n     cin >> y[i]; n     b[y[i]] = 1; n   } n   cnt = 0; n   for (int i = 1; i <= n; i++) { n     for (int j = 1; j <= n; j++) n       if (b[x[i] ^ y[j]]) cnt++; n   } n   if (cnt % 2) n     puts( Koyomi ); n   else n     puts( Karen ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 5000 + 5, mod = 1000 * 1000 * 1000 + 7; n long long dp[N][N], ps[N]; n int main() { n   long long n, a, b, k; n   cin >> n >> a >> b >> k; n   a--, b--; n   for (int i = 0; i < n; i++) { n     dp[0][i] = 1; n   } n   dp[0][b] = 0; n   for (int i = 0; i < n; i++) { n     ps[i + 1] = ps[i] + dp[0][i]; n     ps[i + 1] %= mod; n   } n   for (int i = 1; i <= k; i++) { n     for (int j = 0; j < n; j++) { n       if (j < b) { n         dp[i][j] = n             (ps[b] - ps[j + 1]) + (ps[j] - ps[max(0 * 1ll, j - (b - j) + 1)]); n         dp[i][j] += 2 * mod; n         dp[i][j] %= mod; n       } else { n         dp[i][j] = (ps[j] - ps[b + 1]) + (ps[min(n, j + (j - b))] - ps[j + 1]); n         dp[i][j] += 2 * mod; n         dp[i][j] %= mod; n       } n     } n     ps[0] = 0; n     for (int j = 0; j < n; j++) { n       ps[j + 1] = ps[j] + dp[i][j]; n       ps[j + 1] += 2 * mod; n       ps[j + 1] %= mod; n     } n   } n   cout << dp[k][a] % mod; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 5e5 + 10; n long long f[maxn], a[maxn]; n int n; n map<long long, long long> pre, sum; n vector<long long> ans; n void fir() { n   int st = 1; n   while (st <= n) { n     sum.clear(); n     pre.clear(); n     stack<long long> s; n     for (int& i = st; i <= n; i++) { n       if (pre[a[i]]) { n         ans.push_back(pre[a[i]]); n         ans.push_back(a[i]); n         ans.push_back(pre[a[i]]); n         ans.push_back(a[i]); n         break; n       } n       while (!s.empty() && n              (sum[a[i]] > 1 || (sum[a[i]] == 1 && s.top() != a[i]))) { n         pre[s.top()] = a[i]; n         sum[s.top()]--; n         s.pop(); n       } n       s.push(a[i]); n       sum[a[i]]++; n     } n     st++; n   } n   printf( %d n , ans.size()); n   for (int i = 0; i < ans.size(); i++) { n     printf( %I64d , ans[i]); n     if (i == ans.size() - 1) { n       printf(  n ); n     } else n       printf(   ); n   } n } n int main() { n   scanf( %d , &n); n   for (int i = 1; i <= n; i++) { n     scanf( %I64d , &a[i]); n   } n   fir(); n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int a, b, c, d; n   cin >> a >> b >> c >> d; n   int p1 = max((3 * a) / 10, a - ((a / 250) * c)); n   int p2 = max((3 * b) / 10, b - ((b / 250) * d)); n   if (p1 == p2) n     cout <<  Tie  << endl; n   else if (p1 > p2) n     cout <<  Misha  << endl; n   else n     cout <<  Vasya  << endl; n } n


#include <bits/stdc++.h> n using namespace std; n template <class T> n inline void umax(T &a, T b) { n   if (a < b) a = b; n } n template <class T> n inline void umin(T &a, T b) { n   if (a > b) a = b; n } n template <class T> n inline T abs(T a) { n   return a > 0 ? a : -a; n } n template <class T> n inline T gcd(T a, T b) { n   return __gcd(a, b); n } n template <class T> n inline T lcm(T a, T b) { n   return a / gcd(a, b) * b; n } n const int inf = 1e9 + 143; n const long long longinf = 1e18 + 143; n inline int read() { n   int x; n   scanf(  %d , &x); n   return x; n } n const int N = 1 << 17; n int n; n int up[N]; n int a[N], d[N], sz[N], par[N]; n int tin[N], tout[N], ptr = 0; n vector<pair<int, int> > g[N]; n bool isanc(int p, int u) { return tin[p] <= tin[u] && tin[u] <= tout[p]; } n int get_lca(int u, int v) { n   if (!u) return v; n   if (!v) return u; n   for (;;) { n     if (isanc(u, v)) return u; n     if (isanc(v, u)) return v; n     int hu = up[u] ? up[u] : par[u]; n     int hv = up[v] ? up[v] : par[v]; n     if (d[hu] >= d[hv]) { n       u = hu; n     } else { n       v = hv; n     } n   } n } n namespace lca_tree { n int tree[N << 1]; n void add(int x) { n   tree[x + N - 1] = x; n   x += N - 1; n   while (x > 1) { n     x >>= 1; n     tree[x] = get_lca(tree[x + x], tree[x + x + 1]); n   } n } n void del(int x) { n   tree[x += N - 1] = 0; n   while (x > 1) { n     x >>= 1; n     tree[x] = get_lca(tree[x + x], tree[x + x + 1]); n   } n } n };  // namespace lca_tree n namespace Magic_Tree { n struct info { n   int minv; n   long long sum, all; n }; n info tree[N << 1]; n int add[N << 1]; n inline void push(int x) { n   if (add[x]) { n     add[x + x] += add[x]; n     add[x + x + 1] += add[x]; n     add[x] = 0; n   } n } n inline info apply(int x) { n   return (info){tree[x].minv + add[x], tree[x].sum, tree[x].all}; n } n inline info merge(const info &a, const info &b) { n   info res; n   res.minv = min(a.minv, b.minv); n   res.all = a.all + b.all; n   res.sum = 0ll; n   if (a.minv == res.minv) res.sum += a.sum; n   if (b.minv == res.minv) res.sum += b.sum; n   return res; n } n inline void relax(int x) { tree[x] = merge(apply(x + x), apply(x + x + 1)); } n void init() { n   for (int i = 1; i <= n; i++) tree[tin[i] + N - 1] = (info){0, a[i], a[i]}; n   for (int i = N - 1; i > 0; i--) tree[i] = merge(tree[i + i], tree[i + i + 1]); n   memset(add, 0, sizeof add); n } n void update(int x, int l, int r, int x1, int x2, int val) { n   if (l > x2 || r < x1) return; n   if (l >= x1 && r <= x2) { n     add[x] += val; n     return; n   } n   push(x); n   int m = (l + r) >> 1; n   update(x + x, l, m, x1, x2, val); n   update(x + x + 1, m + 1, r, x1, x2, val); n   relax(x); n } n info get(int x, int l, int r, int x1, int x2) { n   if (l > x2 || r < x1) return (info){0, 0, 0}; n   if (l >= x1 && r <= x2) return apply(x); n   int m = (l + r) >> 1; n   push(x); n   info L = get(x + x, l, m, x1, x2); n   info R = get(x + x + 1, m + 1, r, x1, x2); n   relax(x); n   return merge(L, R); n } n long long getsum(int l, int r) { n   info t = get(1, 1, N, l, r); n   if (t.minv > 0) return t.all; n   return t.all - t.sum; n } n };  // namespace Magic_Tree n void dfs1(int u, int p) { n   sz[u] = 1; n   for (__typeof((g[u]).begin()) it = (g[u]).begin(); it != (g[u]).end(); it++) { n     int v = it->first; n     int c = it->second; n     if (v != p) { n       d[v] = d[u] + 1; n       a[v] = c; n       par[v] = u; n       dfs1(v, u); n       sz[u] += sz[v]; n     } n   } n } n void dfs2(int u, int p) { n   tin[u] = ++ptr; n   int hv = -1; n   for (__typeof((g[u]).begin()) it = (g[u]).begin(); it != (g[u]).end(); it++) { n     int v = it->first; n     int c = it->second; n     if (v != p && (hv == -1 || sz[hv] < sz[v])) hv = v; n   } n   if (hv != -1) { n     up[hv] = up[u] ? up[u] : u; n     dfs2(hv, u); n   } n   for (__typeof((g[u]).begin()) it = (g[u]).begin(); it != (g[u]).end(); it++) { n     int v = it->first; n     int c = it->second; n     if (v != p && v != hv) { n       up[v] = 0; n       dfs2(v, u); n     } n   } n   tout[u] = ptr; n } n int main() { n   n = read(); n   for (int i = 1; i < n; i++) { n     int u = read(), v = read(), c = read(); n     g[u].push_back(pair<int, int>(v, c)); n     g[v].push_back(pair<int, int>(u, c)); n   } n   par[1] = 0; n   dfs1(1, 0); n   dfs2(1, 0); n   Magic_Tree::init(); n   int q = read(); n   while (q--) { n     char foo; n     scanf(  %c , &foo); n     if (foo ==  ? ) { n       int lca = lca_tree::tree[1]; n       0; n       if (lca == 0) { n         puts( 0 ); n       } else { n         printf( %I64d n , Magic_Tree::getsum(tin[lca] + 1, tout[lca])); n       } n     } else { n       int u = read(); n       if (foo ==  + ) n         lca_tree::add(u); n       else n         lca_tree::del(u); n       0; n       int delta = (foo ==  + ) ? +1 : -1; n       while (u > 0) { n         if (up[u]) { n           Magic_Tree::update(1, 1, N, tin[up[u]] + 1, tin[u], delta); n           u = up[u]; n         } else { n           Magic_Tree::update(1, 1, N, tin[u], tin[u], delta); n           u = par[u]; n         } n       } n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAX = 3005; n int a[MAX], b[MAX], c[MAX], dp[MAX][2]; n int main() { n   int n; n   cin >> n; n   for (int i = 1; i <= n; i++) cin >> a[i]; n   for (int i = 1; i <= n; i++) cin >> b[i]; n   for (int i = 1; i <= n; i++) cin >> c[i]; n   dp[1][0] = a[1]; n   dp[1][1] = b[1]; n   for (int i = 2; i <= n; i++) { n     dp[i][0] = dp[i - 1][0] + b[i]; n     dp[i][1] = dp[i - 1][0] + c[i]; n     dp[i][0] = max(dp[i][0], dp[i - 1][1] + a[i]); n     dp[i][1] = max(dp[i][1], dp[i - 1][1] + b[i]); n   } n   cout << dp[n][0] << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long t; n   cin >> t; n   while (t--) { n     long long n; n     cin >> n; n     long long a[n]; n     for (long long i = 0; i < n; i++) cin >> a[i]; n     sort(a, a + n); n     long long flg = 0; n     for (long long i = 0; i < n - 1; i++) n       if (a[i] == a[i + 1]) flg = 1; n     if (flg) n       cout <<  YES  << endl; n     else n       cout <<  NO  << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int inf = 1012345678; n int main() { n   cin.tie(0); n   ios_base::sync_with_stdio(false); n   int N, Q; n   cin >> N >> Q; n   vector<vector<int> > g(N); n   int limit = 0; n   for (int i = 0; i < N; ++i) { n     int x, rad = 1; n     cin >> x; n     for (int j = 2; j * j <= x; ++j) { n       int cnt = 0; n       while (x % j == 0) x /= j, cnt ^= 1; n       if (cnt == 1) g[i].push_back(j), rad *= j; n     } n     if (x > 1) g[i].push_back(x), rad *= x; n     limit = max(limit, rad); n   } n   vector<vector<pair<int, int> > > qs(N + 1); n   for (int i = 0; i < Q; ++i) { n     int L, R; n     cin >> L >> R; n     --L; n     qs[R].push_back(make_pair(L, i)); n   } n   vector<vector<int> > curopt(limit + 1, vector<int>(8, -inf)); n   vector<int> curbound(15, -inf); n   vector<int> ans(Q); n   for (int i = 0; i < N; ++i) { n     for (int j = 0; j < 1 << g[i].size(); ++j) { n       int rem = 0, mul = 1; n       for (int k = 0; k < g[i].size(); ++k) { n         if ((j >> k) & 1) n           mul *= g[i][k]; n         else n           ++rem; n       } n       for (int k = 0; k <= 7; ++k) { n         curbound[rem + k] = max(curbound[rem + k], curopt[mul][k]); n       } n       curopt[mul][rem] = i; n     } n     for (pair<int, int> j : qs[i + 1]) { n       for (int k = 0; k <= 14; ++k) { n         if (curbound[k] >= j.first) { n           ans[j.second] = k; n           break; n         } n       } n     } n   } n   for (int i = 0; i < Q; ++i) { n     cout << ans[i] <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m; n const int maxn = 1e5 + 3; n int terminal[maxn]; n struct spe { n   int next, to; n }; n spe edge[maxn << 1]; n int head[maxn]; n int visit[maxn][3]; n int cnt = 1, tpoint = -1; n int nxt[maxn][2]; n bool dp[maxn][3]; n int cir = 0; n bool dfs(int s, int val) { n   if (terminal[s] == 1) { n     tpoint = s; n     return val == 1; n   } n   if (visit[s][val] == 2) return dp[s][val]; n   bool temp = false; n   visit[s][val] = 1; n   for (int i = head[s]; i != -1; i = edge[i].next) { n     int t = edge[i].to; n     if (visit[t][!val] == 1) { n       cir = 1; n       continue; n     } n     if (dfs(t, !val) == true) { n       nxt[s][val] = t; n       temp = true; n     } n   } n   visit[s][val] = 2; n   dp[s][val] = temp; n   return temp; n } n int main() { n   memset(dp, 0, sizeof dp); n   memset(visit, 0, sizeof visit); n   memset(head, -1, sizeof head); n   memset(edge, 0, sizeof edge); n   memset(terminal, 0, sizeof terminal); n   scanf( %d %d , &n, &m); n   for (int i = 1; i <= n; i++) n     for (int j = 0; j <= 1; j++) nxt[i][j] = i; n   int counter = 0; n   for (int i = 1; i <= n; i++) { n     int k, item; n     scanf( %d , &k); n     if (k == 0) counter++, terminal[i] = 1; n     while (k--) { n       scanf( %d , &item); n       edge[cnt].to = item; n       edge[cnt].next = head[i]; n       head[i] = cnt++; n     } n   } n   int start; n   scanf( %d , &start); n   bool ans = dfs(start, 0); n   if (ans == true) { n     printf( Win n ); n     int j = 0; n     printf( %d , start); n     while (nxt[start][j] != start) { n       start = nxt[start][j]; n       j = !j; n       printf(  %d , start); n     } n     printf(  n ); n   } else if (cir == 1) n     printf( Draw n ); n   else n     printf( Lose n ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 100; n const int maxm = 100; n const int maxlen = maxn + maxm - 1; n struct point { n   int ind, p; n   bool operator<(const point& b) const { return p < b.p; } n } cell[maxlen]; n char state[maxlen]; n int n, m; n long long k; n int p; n long long dp[maxlen][maxlen]; n long long Cnt() { n   memset(dp, 0, sizeof(dp)); n   if (state[0] ==  ) ) return 0; n   dp[0][1] = 1; n   for (int i = 1; i <= (n - 1) + (m - 1); ++i) { n     for (int j = 0; j <= (n - 1) + (m - 1); ++j) { n       if (state[i] == 0) { n         if (j > 0) dp[i][j] = dp[i - 1][j - 1]; n         dp[i][j] += dp[i - 1][j + 1]; n       } else if (state[i] ==  ( ) { n         if (j > 0) dp[i][j] = dp[i - 1][j - 1]; n       } else n         dp[i][j] = dp[i - 1][j + 1]; n       if (dp[i][j] > k) dp[i][j] = k + 1; n     } n   } n   return dp[(n - 1) + (m - 1)][0]; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin >> n >> m >> k; n   for (int i = 0; i <= (n - 1) + (m - 1); ++i) n     cell[i].ind = i, cell[i].p = n * m + 1; n   for (int i = 0; i < n; ++i) n     for (int j = 0; j < m; ++j) { n       cin >> p; n       cell[i + j].p = min(cell[i + j].p, p); n     } n   sort(cell, cell + n + m - 1); n   for (int i = 0; i <= (n - 1) + (m - 1); ++i) { n     state[cell[i].ind] =  ( ; n     long long num = Cnt(); n     if (num < k) { n       k -= num; n       state[cell[i].ind] =  ) ; n     } n   } n   for (int i = 0; i < n; ++i) { n     for (int j = 0; j < m; ++j) cout << state[i + j]; n     cout << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int x, y, z, a, b; n   cin >> x >> y >> z; n   a = max(x, y); n   b = min(x, y); n   if (b + z < a || z == 0) { n     if (x > y) n       cout <<  + ; n     else if (x < y) n       cout <<  - ; n     else n       cout <<  0 ; n   } else if (b + z >= a) n     cout <<  ? ; n } n


#include <bits/stdc++.h> n const int mod = 1000000007, hmod = 10000019; n int n, a[1000010], p[1000010], f[1000010]; n long long sz[1000010]; n int g[1000010]; n long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; } n int main() { n   scanf( %d , &n); n   for (int i = 1; i <= n; i++) scanf( %d , a + i), sz[i] = a[i]; n   for (int i = 2; i <= n; i++) scanf( %d , p + i); n   for (int i = n; i > 1; i--) sz[p[i]] += sz[i]; n   for (int i = 1; i <= n; i++) { n     long long j = sz[1] / gcd(sz[1], sz[i]); n     if (j <= n) g[j]++; n   } n   for (int i = n; i; i--) n     for (int j = i * 2; j <= n; j += i) g[j] += g[i]; n   for (int i = n; i >= 1; i--) n     if (g[i] >= i) { n       f[i] = 1; n       for (int j = i * 2; j <= n; j += i) (f[i] += f[j]) %= mod; n     } n   printf( %d n , f[1]); n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T> n inline void read(T &val) { n   val = 0; n   char c = getchar(); n   int f = 1; n   while (!isdigit(c)) { n     if (c ==  - ) f = -1; n     c = getchar(); n   } n   while (isdigit(c)) { n     val = (val << 3) + (val << 1) + (c ^ 48); n     c = getchar(); n   } n   val *= f; n } n template <typename T> n void write_(T val) { n   if (!val) return; n   write_(val / 10); n   putchar(val % 10 + 48); n } n template <typename T> n inline void write(T val) { n   if (val == 0) n     putchar(48); n   else if (val < 0) { n     putchar( - ); n     write_(-val); n   } else n     write_(val); n } n const int N = 4e5 + 5; n struct segment { n   long long l, r; n } s[N]; n int n; n long long ans0, ans1, tmp[N << 1], d_[N << 2], d[N << 2]; n int main() { n   ios::sync_with_stdio(false); n   int T; n   cin >> T; n   while (T--) { n     ans0 = 0; n     ans1 = INT_MIN; n     cin >> n; n     for (register int i = 1; i <= n; i++) { n       cin >> s[i].l >> s[i].r; n       tmp[(i << 1) - 1] = s[i].l; n       tmp[i << 1] = s[i].r; n     } n     sort(tmp + 1, tmp + (n << 1) + 1); n     int tot = unique(tmp + 1, tmp + (n << 1) + 1) - tmp - 1; n     for (register int i = 1; i <= n; i++) { n       s[i].l = lower_bound(tmp + 1, tmp + tot + 1, s[i].l) - tmp; n       s[i].r = lower_bound(tmp + 1, tmp + tot + 1, s[i].r) - tmp; n       s[i].l <<= 1LL; n       s[i].r <<= 1LL; n       d[s[i].l]++; n       d[s[i].r + 1]--; n     } n     tot <<= 1LL; n     for (register long long i = 1; i <= tot + 5; i++) d[i] += d[i - 1]; n     for (register long long i = 0; i <= tot + 5; i++) ans0 += d[i] && !d[i + 1]; n     for (register long long i = 0; i <= tot + 4; i++) n       if (d[i] == 1 && d[i + 1] != 1) d_[i]++; n     for (register long long i = 1; i <= tot + 5; i++) n       if (d[i] == 1 && d[i - 1] != 1) d_[i]++; n     for (register long long i = 1; i <= tot + 5; i++) d_[i] += d_[i - 1]; n     for (register int i = 1; i <= n; i++) { n       long long t = d_[s[i].r] - d_[s[i].l - 1]; n       t /= 2; n       t -= (d[s[i].r] == 1) + (d[s[i].l] == 1); n       ans1 = max(ans1, t); n     } n     cout << ans0 + ans1 << endl; n     for (register long long i = 0; i <= tot + 10; i++) d[i] = d_[i] = 0; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n stack<int> a[27]; n stack<int> b[27]; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   ; n   int n; n   cin >> n; n   string f, s; n   cin >> f >> s; n   for (int i = 0; i < f.size(); ++i) { n     if (f[i] ==  ? ) { n       a[26].push(i + 1); n     } else { n       a[(int)(f[i] -  a )].push(i + 1); n     } n   } n   for (int i = 0; i < s.size(); ++i) { n     if (s[i] ==  ? ) { n       b[26].push(i + 1); n     } else { n       b[(int)(s[i] -  a )].push(i + 1); n     } n   } n   int ans = 0; n   vector<pair<int, int> > anss; n   for (int i = 0; i < 27; ++i) { n     while ((a[i].size() != 0 && b[i].size() != 0) || n            (a[i].size() != 0 && b[26].size() != 0) || n            (a[26].size() != 0 && b[i].size() != 0)) { n       if (a[i].size() != 0 && b[i].size() != 0) { n         int c1, c2; n         c1 = a[i].top(); n         c2 = b[i].top(); n         ans++; n         a[i].pop(); n         b[i].pop(); n         anss.push_back({c1, c2}); n       } else if (a[i].size() != 0 && b[i].size() == 0) { n         if (b[26].size() != 0) { n           int c1, c2; n           c1 = a[i].top(); n           c2 = b[26].top(); n           ans++; n           a[i].pop(); n           b[26].pop(); n           anss.push_back({c1, c2}); n         } n       } else if (a[i].size() == 0 && b[i].size() != 0) { n         if (a[26].size() != 0) { n           int c1, c2; n           c1 = a[26].top(); n           c2 = b[i].top(); n           ans++; n           a[26].pop(); n           b[i].pop(); n           anss.push_back({c1, c2}); n         } n       } n     } n   } n   cout << ans << endl; n   for (int i = 0; i < anss.size(); ++i) { n     cout << anss[i].first <<     << anss[i].second << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n inline int read() { n   int x = 0, f = 1; n   char c = getchar(); n   while (!isdigit(c)) { n     if (c ==  - ) f = -1; n     c = getchar(); n   } n   while (isdigit(c)) { n     x = (x << 1) + (x << 3) + (c ^ 48); n     c = getchar(); n   } n   return f == 1 ? x : -x; n } n const int N = 3e5 + 4; n long long ans = 0; n int n, a[N], mx[N], mn[N], cnt[N << 1]; n inline void solve(int l, int r) { n   if (l == r) return ++ans, void(); n   int mid = l + r >> 1; n   mx[mid] = mn[mid] = a[mid]; n   mx[mid + 1] = mn[mid + 1] = a[mid + 1]; n   for (int i = mid + 2; i <= r; i++) { n     mx[i] = (mx[i - 1] > a[i] ? mx[i - 1] : a[i]); n     mn[i] = (mn[i - 1] > a[i] ? a[i] : mn[i - 1]); n   } n   for (int i = mid - 1; i >= l; i--) { n     mx[i] = (mx[i + 1] > a[i] ? mx[i + 1] : a[i]); n     mn[i] = (mn[i + 1] > a[i] ? a[i] : mn[i + 1]); n   } n   int ll = mid, rr = mid; n   for (int i = mid + 1, x; i <= r; i++) { n     x = i - mx[i] + mn[i]; n     if (x >= l && x <= mid && mx[x] < mx[i] && mn[x] > mn[i]) ans++; n     while (ll >= l && mx[ll] < mx[i]) { n       cnt[N + mn[ll] - ll]++; n       ll--; n     } n     while (rr > ll && mn[rr] > mn[i]) { n       cnt[N + mn[rr] - rr]--; n       rr--; n     } n     ans += cnt[N + mx[i] - i]; n   } n   while (rr != ll) { n     cnt[N + mn[rr] - rr]--; n     rr--; n   } n   ll = rr = mid + 1; n   for (int i = mid, x; i >= l; i--) { n     x = mx[i] - mn[i] + i; n     if (x > mid && x <= r && mx[x] < mx[i] && mn[x] > mn[i]) ans++; n     while (rr <= r && mx[rr] < mx[i]) { n       cnt[mn[rr] + rr]++; n       rr++; n     } n     while (ll < rr && mn[ll] > mn[i]) { n       cnt[mn[ll] + ll]--; n       ll++; n     } n     ans += cnt[mx[i] + i]; n   } n   while (ll != rr) { n     cnt[mn[ll] + ll]--; n     ll++; n   } n   solve(l, mid); n   solve(mid + 1, r); n } n int main() { n   n = read(); n   for (int i = 1, u, v; i <= n; i++) { n     u = read(); n     v = read(); n     a[u] = v; n   } n   solve(1, n); n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n int main() { n   int n; n   scanf( %d , &n); n   long long ans = (6 * (1LL * n * (n + 1)) / 2); n   printf( %I64d n , ans + 1); n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, num, sum = 0; n   cin >> n; n   for (int i = 0; i < n; i++) { n     cin >> num; n     sum += num; n   } n   num = 0; n   for (int i = sum + 1; i < sum + 6; i++) { n     if (i % (n + 1) != 1) num++; n   } n   cout << num; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int t, n, m, cnt, xmin, xmax, ymin, ymax, head, tail, g[250 + 2][250 + 2], n     f[250 + 2][250 + 2], qx[250 * 250], qy[250 * 250]; n bool checkType1() { n   if (tail != 4 * (xmax - xmin)) return false; n   for (int i = xmin; i <= xmax; ++i) { n     if (f[i][ymin] != cnt) return false; n     if (f[i][ymax] != cnt) return false; n   } n   for (int j = ymin; j <= ymax; ++j) { n     if (f[xmin][j] != cnt) return false; n     if (f[xmax][j] != cnt) return false; n   } n   return true; n } n bool checkType2() { n   if ((xmin + xmax) % 2 || (ymin + ymax) % 2) return false; n   int xmid = (xmax + xmin) / 2, ymid = (ymax + ymin) / 2, n       rang = (xmax - xmin) / 2; n   if (tail != 4 * rang) return false; n   for (int x = 0; x <= rang; ++x) { n     int y = rang - x; n     if (f[xmid - x][ymid - y] != cnt) return false; n     if (f[xmid - x][ymid + y] != cnt) return false; n     if (f[xmid + x][ymid - y] != cnt) return false; n     if (f[xmid + x][ymid + y] != cnt) return false; n   } n   return true; n } n int main() { n   for (scanf( %d , &t); t; --t) { n     scanf( %d%d , &n, &m); n     memset(g, 0, sizeof(g)); n     memset(f, 0, sizeof(f)); n     for (int i = 1; i <= n; ++i) { n       char b[250]; n       scanf( %s , b); n       for (int j = 1; j <= m; ++j) g[i][j] = b[j - 1] -  0 ; n     } n     int dx[8] = {-1, -1, -1, 0, 0, 1, 1, 1}, n         dy[8] = {-1, 0, 1, -1, 1, -1, 0, 1}, ans = cnt = 0; n     for (int i = 1; i <= n; ++i) n       for (int j = 1; j <= m; ++j) n         if (g[i][j] && !f[i][j]) { n           f[i][j] = ++cnt; n           head = tail = 0; n           xmin = xmax = i; n           ymin = ymax = j; n           qx[tail] = i; n           qy[tail] = j; n           tail++; n           while (head != tail) { n             for (int k = 0; k != 8; ++k) { n               int xx = qx[head] + dx[k], yy = qy[head] + dy[k]; n               if (g[xx][yy] && !f[xx][yy]) { n                 f[xx][yy] = cnt; n                 if (xx < xmin) xmin = xx; n                 if (xx > xmax) xmax = xx; n                 if (yy < ymin) ymin = yy; n                 if (yy > ymax) ymax = yy; n                 qx[tail] = xx; n                 qy[tail] = yy; n                 tail++; n               } n             } n             head++; n           } n           if (tail <= 1 || xmax - xmin != ymax - ymin) continue; n           if (checkType1() || checkType2()) ans++; n         } n     printf( %d n , ans); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n set<pair<long long, long long> > st; n map<long long, long long> tp; n long long ans[100013]; n int main() { n   long long n, k; n   cin >> n >> k; n   vector<pair<long long, long long> > v(n); n   for (int i = 0; i < n; i++) { n     cin >> v[i].first; n     v[i].first = -v[i].first; n     tp[v[i].first] = i; n   } n   for (int i = 0; i < n; i++) { n     cin >> v[i].second; n     v[i].second = -v[i].second; n     st.insert({v[i].second, v[i].first}); n   } n   sort(v.begin(), v.end()); n   for (int i = 0; i < n; i++) { n     long long an = -v[i].second, j = 0; n     for (auto it = st.begin(); it != st.end() && j < k; it++, j++) { n       if (it->second == v[i].first) { n         j--; n         continue; n       } else n         an += (-(it->first)); n     } n     st.erase(st.find({v[i].second, v[i].first})); n     ans[tp[v[i].first]] = an; n   } n   for (int i = 0; i < n; i++) { n     cout << ans[i] <<    ; n   } n   return 0; n } n


#include<bits/stdc++.h> /* n #include <ext/pb_ds/assoc_container.hpp> // Common file n #include <ext/pb_ds/tree_policy.hpp> // Including */ n using namespace std; n //using namespace __gnu_pbds; n  n //typedefs n  n typedef long long ll; n typedef vector<int> vi; n typedef vector<ll> vl; n typedef vector<vi> vvi; n typedef vector<vl> vvl; n typedef pair<int,int> pii; n typedef pair<double, double> pdd; n typedef pair<ll, ll> pll; n typedef vector<pii> vii; n typedef vector<pll> vll; n typedef vector<int>::iterator vit; n typedef set<int>::iterator sit; n  n /* n template<typename T> n using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>; n template<typename F, typename S> n using ordered_map = tree<F, S, less<F>, rb_tree_tag, tree_order_statistics_node_update>; n */ n  n //#Defines n #define rep(i,a,b) for(i=a;i<=b;i++) n #define repR(i,a,b) for(i=a;i>=b;i--) n //#define pb push_back n #define pb emplace_back n #define F first n #define S second n #define mp make_pair n #define all(c)  c.begin(),c.end() n #define endl   n  n #define pf printf n #define sf scanf n //#define left __left n //#define right __right n //#define tree __tree n #define MOD 1000000007 n //#define harmonic(n) 0.57721566490153286l+log(n) n  n #define RESET(a,b) memset(a,b,sizeof(a)) n #define gcd(a,b) __gcd(a,b) n #define lcm(a,b) (a*(b/gcd(a,b))) n #define sqr(a) ((a) * (a)) n  n #define optimize() ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); n #define fraction() cout.unsetf(ios::floatfield); cout.precision(10); cout.setf(ios::fixed,ios::floatfield); n  n const double PI = acos(-1); n const double eps = 1e-9; n const int inf = 2000000000; n const ll infLL = 9000000000000000000; n  n  n //Bit Operations n inline bool checkBit(ll n, int i) { return n&(1LL<<i); } n inline ll setBit(ll n, int i) { return n|(1LL<<i);; } n inline ll resetBit(ll n, int i) { return n&(~(1LL<<i)); } n  n int fx[] = {0, 0, +1, -1}; n int fy[] = {+1, -1, 0, 0}; n //int dx[] = {+1, 0, -1, 0, +1, +1, -1, -1}; n //int dy[] = {0, +1, 0, -1, +1, -1, +1, -1}; n  n  n //Inline functions n  n inline bool EQ(double a, double b) { return fabs(a-b) < 1e-9; } n inline bool isLeapYear(ll year) { return (year%400==0) || (year%4==0 && year%100!=0); } n inline void normal(ll &a) { a %= MOD; (a < 0) && (a += MOD); } n inline ll modMul(ll a, ll b) { a %= MOD, b %= MOD; normal(a), normal(b); return (a*b)%MOD; } n inline ll modAdd(ll a, ll b) { a %= MOD, b %= MOD; normal(a), normal(b); return (a+b)%MOD; } n inline ll modSub(ll a, ll b) { a %= MOD, b %= MOD; normal(a), normal(b); a -= b; normal(a); return a; } n inline ll modPow(ll b, ll p) { ll r = 1; while(p) { if(p&1) r = modMul(r, b); b = modMul(b, b); p >>= 1; } return r; } n inline ll modInverse(ll a) { return modPow(a, MOD-2); } n inline ll modDiv(ll a, ll b) { return modMul(a, modInverse(b)); } n inline bool isInside(pii p,ll n,ll m) { return (p.first>=0&&p.first<n&&p.second>=0&&p.second<m); } n inline bool isInside(pii p,ll n)  { return (p.first>=0&&p.first<n&&p.second>=0&&p.second<n); } n inline bool isSquare(ll x)     { ll s = sqrt(x); return (s*s==x); } n inline bool isFib(ll x)     { return isSquare(5*x*x+4)|| isSquare(5*x*x-4); } n inline bool isPowerOfTwo(ll x)   { return ((1LL<<(ll)log2(x))==x); } n  n  n struct func n { n  //this is a sample overloading function for sorting stl n  bool operator()(pii const &a, pii const &b)  n  { n   if(a.F==b.F) n    return (a.S<b.S); n   return (a.F<b.F); n  } n }; n  n //Prime Number Generator n  n /* n #define M 100000000 n int marked[M/64 + 2]; n  n #define on(x) (marked[x/64] & (1<<((x%64)/2))) n #define mark(x)  marked[x/64] |= (1<<((x%64)/2)) n  n vl prime; n  n bool isPrime(int num) { n   return num > 1 && (num == 2 || ((num & 1) && !on(num)));  n } n  n void sieve(ll n) { n  for (ll i = 3; i * i < n; i += 2) { n   if (!on(i)) { n    for (ll j = i * i; j <= n; j += i + i) { n     mark(j); n    } n   } n  } n  prime.pb(2); n  for(ll i = 3; i <= n; i += 2) { n   if(!on(i)) prime.pb(i);  n  } n } n  n */ n  n // n //debug n #ifdef tajir n template < typename F, typename S > n ostream& operator << ( ostream& os, const pair< F, S > & p ) { n  return os <<  (  << p.first <<  ,   << p.second <<  ) ; n } n  n template < typename T > n ostream &operator << ( ostream & os, const vector< T > &v ) { n  os <<  { ; n  for(auto it = v.begin(); it != v.end(); ++it) { n   if( it != v.begin() ) os <<  ,  ; n    os << *it; n  } n  return os <<  } ; n } n  n template < typename T > n ostream &operator << ( ostream & os, const set< T > &v ) { n  os <<  [ ; n  for(auto it = v.begin(); it != v.end(); ++it) { n   if( it != v.begin() ) os <<  ,  ; n    os << *it; n  } n  return os <<  ] ; n } n  n template < typename T > n ostream &operator << ( ostream & os, const multiset< T > &v ) { n  os <<  [ ; n  for(auto it = v.begin(); it != v.end(); ++it) { n   if( it != v.begin() ) os <<  ,  ; n   os << *it; n  } n  return os <<  ] ; n } n  n template < typename F, typename S > n ostream &operator << ( ostream & os, const map< F, S > &v ) { n  os <<  [ ; n  for(auto it = v.begin(); it != v.end(); ++it) { n   if( it != v.begin() ) os <<  ,  ; n   os << it -> first <<   =   << it -> second ; n   } n  return os <<  ] ; n } n  n #define dbg(args...) do {cerr << #args <<   :  ; faltu(args); } while(0) n  n clock_t tStart = clock(); n #define timeStamp dbg( Execution Time:  , (double)(clock() - tStart)/CLOCKS_PER_SEC) n  n void faltu () { n  cerr << endl; n } n  n template <typename T> n void faltu( T a[], int n ) { n  for(int i = 0; i < n; ++i) cerr << a[i] <<    ; n  cerr << endl; n } n  n template <typename T, typename ... hello> n void faltu( T arg, const hello &... rest) { n  cerr << arg <<    ; n  faltu(rest...); n } n #else n #define dbg(args...) n #endif // tajir n  n bool valid(string s,int n) { n  for(int i = 0; i < (n-1); ++i) { n   if(s[i] ==  1  && s[i+1] ==  1 ) n    return false; n  } n  return true; n } n  n int main() n { n  #ifdef tajir n   freopen( input.txt ,  r , stdin); n  #else n   // online submission n  #endif n  optimize(); n  int n; n  string s; n  cin >> n >> s; n  if(!valid(s,n)) { n   cout <<  No  << endl; n   return 0; n  } n  for(int i = 0; i < n; ++i) { n   if(s[i] ==  0 ) { n    s[i] =  1 ; n    if(valid(s,n)) { n     cout <<  No  << endl; n     return 0; n    } n    s[i] =  0 ; n   } n  } n  cout <<  Yes  << endl; n  return 0; n } n  n  n  n  n  n  n //? n


#include <bits/stdc++.h> n using namespace std; n int a[4], b[4]; n int get(int *a) { n   int ans; n   cout <<  ?   << a[0] <<     << a[1] <<     << a[2] <<     << a[3] << endl; n   cin >> ans; n   return ans; n } n int get1(int a, int b, int c, int d) { n   int ans; n   cout <<  ?   << a <<     << b <<     << c <<     << d << endl; n   cin >> ans; n   return ans; n } n void solve(int *a) { n   int l = a[0], r = a[2], mid; n   while (l < r) { n     mid = (l + r) / 2; n     a[2] = mid; n     if (get(a)) { n       r = mid; n     } else { n       l = mid + 1; n     } n   } n   a[2] = r; n   l = a[0], r = a[2]; n   while (l < r) { n     mid = (l + r) / 2; n     a[0] = mid + 1; n     if (get(a)) { n       l = mid + 1; n     } else { n       r = mid; n     } n   } n   a[0] = l; n   l = a[1], r = a[3]; n   while (l < r) { n     mid = (l + r) / 2; n     a[3] = mid; n     if (get(a)) { n       r = mid; n     } else { n       l = mid + 1; n     } n   } n   a[3] = r; n   l = a[1], r = a[3]; n   while (l < r) { n     mid = (l + r) / 2; n     a[1] = mid + 1; n     if (get(a)) { n       l = mid + 1; n     } else { n       r = mid; n     } n   } n   a[1] = r; n } n int main() { n   ios_base::sync_with_stdio(false); n   int n; n   cin >> n; n   b[0] = b[1] = a[0] = a[1] = 1; n   a[2] = a[3] = b[2] = b[3] = n; n   solve(a); n   if (a[2] != n && get1(a[2] + 1, 1, n, n)) { n     b[0] = a[2] + 1; n   } else if (a[3] != n && get1(1, a[3] + 1, n, n)) { n     b[1] = a[3] + 1; n   } else if (a[0] != 1 && get1(1, 1, a[0] - 1, n)) { n     b[2] = a[0] - 1; n   } else if (a[1] != 1 && get1(1, 1, n, a[1] - 1)) { n     b[3] = a[1] - 1; n   } n   solve(b); n   cout <<  !  ; n   for (int i = 0; i < 4; i++) { n     cout << a[i] <<    ; n   } n   for (int i = 0; i < 4; i++) { n     cout << b[i] <<    ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, x, a, ans, r[2001], t[2001][2001]; n bool f[2001][2001]; n int main() { n   scanf( %d , &n); n   for (int _ = 1; _ <= n; _++) n     while (scanf( %d: , &x)) { n       r[_]++; n       for (int i = 1; i < x; i++) scanf( %d, , &a), t[_][a] = x; n       scanf( %d , &a), t[_][a] = x; n       if (getchar() !=  - ) break; n     } n   for (int i = 1; i <= n; i++) n     for (int j = i + 1; j <= n; j++) n       if (t[i][j] + t[j][i] == n) f[i][j] = f[j][i] = true, ans++; n   if (ans != (n - 1)) return printf( -1 n ), 0; n   for (int i = 1; i <= n; i++) { n     a = 0; n     for (int j = 1; j <= n; j++) n       if (f[i][j]) a++; n     if (a != r[i]) return printf( -1 n ), 0; n   } n   printf( %d n , ans); n   for (int i = 1; i <= n; i++) n     for (int j = i + 1; j <= n; j++) n       if (f[i][j]) printf( %d %d n , i, j); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxN = 5e5 + 10, mod = 1e9 + 7; n int n, q, cnt; n int nxt[maxN + 1], res[maxN + 1], ans[maxN + 1]; n int pw[maxN + 1], id[maxN + 1], tp[maxN + 1]; n int L[maxN + 1], R[maxN + 1], S[maxN + 1]; n int stk[maxN + 1], top; n int sum[maxN + 1], mn[maxN + 1][21], lg[maxN + 1]; n char s[maxN + 1]; n vector<int> pm[maxN + 1], pa[maxN + 1]; n vector<int> son[maxN + 1], pre[maxN + 1], p0[maxN + 1]; n inline int read() { n   int num = 0, f = 1; n   char ch = getchar(); n   while (!isdigit(ch)) { n     if (ch ==  - ) f = -1; n     ch = getchar(); n   } n   while (isdigit(ch)) num = (num << 3) + (num << 1) + (ch ^ 48), ch = getchar(); n   return num * f; n } n inline void update(int &x, int y) { x = x + y >= mod ? x + y - mod : x + y; } n inline int mpow(int a, int x) { n   int ans = 1; n   while (x) { n     if (x & 1) ans = 1ll * ans * a % mod; n     a = 1ll * a * a % mod; n     x >>= 1; n   } n   return ans; n } n inline int get_mn(int l, int r) { n   int k = lg[r - l + 1]; n   return min(mn[l][k], mn[r - (1 << k) + 1][k]); n } n inline bool check(int l, int r) { n   if (sum[r] != sum[l - 1]) return false; n   if (s[l] ==  *  || s[l] ==  + ) return false; n   if (s[r] ==  *  || s[r] ==  + ) return false; n   return get_mn(l, r) >= sum[r]; n } n inline int get_Mul(int l, int r) { n   int id = sum[r]; n   int x = lower_bound(pm[id].begin(), pm[id].end(), l) - pm[id].begin(); n   if (x == pm[id].size() || pm[id][x] > r) return -1; n   return pm[id][x]; n } n inline int get_Add(int l, int r) { n   int id = sum[r]; n   int x = lower_bound(pa[id].begin(), pa[id].end(), l) - pa[id].begin(); n   if (x == pa[id].size() || pa[id][x] > r) return -1; n   return pa[id][x]; n } n inline int build(int l, int r) { n   int node = ++cnt; n   L[node] = l, R[node] = r; n   while (s[l] ==  (  && nxt[l] == r) id[l++] = node, r--; n   int mid = get_Add(l, r); n   if (mid != -1) { n     tp[node] = 0; n     int x = l; n     while (mid != -1) { n       id[mid] = node; n       son[node].push_back(build(x, mid - 1)); n       x = mid + 1; n       mid = get_Add(x, r); n     } n     son[node].push_back(build(x, r)); n     int sum = 0; n     for (int i = 0; i < son[node].size(); i++) { n       update(sum, res[son[node][i]]); n       pre[node].push_back(sum); n     } n     return res[node] = sum, node; n   } n   mid = get_Mul(l, r); n   if (mid != -1) { n     tp[node] = 1; n     int x = l; n     while (mid != -1) { n       id[mid] = node; n       son[node].push_back(build(x, mid - 1)); n       x = mid + 1; n       mid = get_Mul(x, r); n     } n     son[node].push_back(build(x, r)); n     int sum = 1, tot = 0; n     for (int i = 0; i < son[node].size(); i++) { n       if (!res[son[node][i]]) n         tot++; n       else n         sum = 1ll * sum * res[son[node][i]] % mod; n       pre[node].push_back(sum); n       p0[node].push_back(tot); n     } n     return res[node] = tot ? 0 : sum, node; n   } n   tp[node] = 2; n   for (int i = l; i <= r; i++) n     res[node] = (10ll * res[node] % mod + s[i] -  0 ) % mod; n   return node; n } n inline int get(int l, int r) { n   int ans = S[r]; n   update(ans, mod - 1ll * S[l - 1] * pw[r - l + 1] % mod); n   return ans; n } n inline int findl(int node, int x) { n   int l = 0, r = son[node].size() - 1, ans = -1; n   while (l <= r) { n     int mid = (l + r) >> 1; n     if (L[son[node][mid]] < x) n       ans = mid, l = mid + 1; n     else n       r = mid - 1; n   } n   return ans; n } n inline int findr(int node, int x) { n   int l = 0, r = son[node].size() - 1, ans = son[node].size(); n   while (l <= r) { n     int mid = (l + r) >> 1; n     if (R[son[node][mid]] > x) n       ans = mid, r = mid - 1; n     else n       l = mid + 1; n   } n   return ans; n } n inline int query3(int node, int l, int r) { n   l = max(l, L[node]), r = min(r, R[node]); n   if (l > r) return 0; n   return get(l, r); n } n inline int query2(int node, int l, int r) { n   if (tp[node] == 2) return query3(node, l, r); n   l = max(l, L[node]), r = min(r, R[node]); n   if (l > r) return 0; n   int x = findl(node, l), y = findr(node, r); n   int ans = (y ? pre[node][y - 1] : 1), tot = (y ? p0[node][y - 1] : 0); n   if (x != -1) n     ans = 1ll * ans * mpow(pre[node][x], mod - 2) % mod, tot -= p0[node][x]; n   if (x != -1 && R[son[node][x]] >= l) n     ans = 1ll * ans * query3(son[node][x], l, r) % mod; n   if (y != son[node].size() && L[son[node][y]] <= r) n     ans = 1ll * ans * query3(son[node][y], l, r) % mod; n   return tot ? 0 : ans; n } n inline int query1(int node, int l, int r) { n   int x = findl(node, l), y = findr(node, r); n   int ans = (y ? pre[node][y - 1] : 0); n   if (x != -1) update(ans, mod - pre[node][x]); n   if (x != -1 && R[son[node][x]] >= l) update(ans, query2(son[node][x], l, r)); n   if (y != son[node].size() && L[son[node][y]] <= r) n     update(ans, query2(son[node][y], l, r)); n   return ans; n } n inline int solve(int l, int r) { n   if (s[l] ==  (  && nxt[l] == r) return res[id[l]]; n   int mid = get_Add(l, r); n   if (mid != -1) return query1(id[mid], l, r); n   mid = get_Mul(l, r); n   if (mid != -1) return query2(id[mid], l, r); n   return get(l, r); n } n int main() { n   scanf( %s , s + 1); n   n = strlen(s + 1); n   for (int i = 1; i <= n; i++) { n     sum[i] = sum[i - 1]; n     if (s[i] ==  ( ) sum[i]++, stk[++top] = i; n     if (s[i] ==  ) ) sum[i]--, nxt[stk[top--]] = i; n   } n   for (int i = 2; i <= n; i++) lg[i] = lg[i >> 1] + 1; n   for (int i = 1; i <= n; i++) mn[i][0] = sum[i]; n   for (int j = 1; (1 << j) <= n; j++) n     for (int i = 1; i + (1 << j) - 1 <= n; i++) n       mn[i][j] = min(mn[i][j - 1], mn[i + (1 << j - 1)][j - 1]); n   for (int i = 1; i <= n; i++) { n     if (s[i] ==  + ) pa[sum[i]].push_back(i); n     if (s[i] ==  * ) pm[sum[i]].push_back(i); n   } n   pw[0] = 1; n   for (int i = 1; i <= n; i++) pw[i] = 10ll * pw[i - 1] % mod; n   for (int i = 1; i <= n; i++) { n     S[i] = 10ll * S[i - 1] % mod; n     if (isdigit(s[i])) update(S[i], s[i] -  0 ); n   } n   build(1, n); n   q = read(); n   while (q--) { n     int l = read(), r = read(); n     if (!check(l, r)) { n       puts( -1 ); n       continue; n     } n     printf( %d n , solve(l, r)); 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(nullptr); n   string s; n   cin >> s; n   int count = 0; n   while (s.size() != 1) { n     int sum = 0; n     for (int i = 0; i < s.size(); i++) sum += s[i] - 48; n     string res =   ; n     while (sum) { n       int digit = sum % 10; n       sum /= 10; n       res += (char)(digit + 48); n     } n     s = res; n     count++; n   } n   cout << count; n } n


#include <bits/stdc++.h> n #pragma GCC optimize(3,  Ofast ,  inline ) n #pragma GCC target( avx,avx2 ) n using namespace std; n template <class t> n inline t read(t &x) { n   char c = getchar(); n   bool f = 0; n   x = 0; n   while (!isdigit(c)) f |= c ==  - , c = getchar(); n   while (isdigit(c)) x = (x << 1) + (x << 3) + (c ^ 48), c = getchar(); n   if (f) x = -x; n   return x; n } n template <class t, class... A> n inline void read(t &x, A &...a) { n   read(x); n   read(a...); n } n template <class t> n inline void write(t x) { n   if (x < 0) n     putchar( - ), write(-x); n   else { n     if (x > 9) write(x / 10); n     putchar( 0  + x % 10); n   } n } n long long A, B, C, S, ansA, ansB, ansC, ansS, pw[505][505], V, n, v[10005]; n void dfs2(long long x, long long b, long long a) { n   if (1.0 * b * b > V / a) return; n   if (x > n) { n     B = max(B, b); n     return; n   } n   for (long long i = v[x]; ~i; i--) dfs2(x + 1, b * pw[x][i], a); n } n void dfs1(long long x, long long a) { n   if (1.0 * a * a * a > V) return; n   if (x > n) { n     long long bc = V / a, lim = 4 * a * sqrt(bc) + 2 * bc; n     if (lim >= ansS) return; n     B = 0; n     A = a; n     dfs2(1, 1, A); n     C = V / A / B; n     S = (A * B + B * C + A * C) << 1; n     if (S < ansS) { n       ansS = S; n       ansA = A; n       ansB = B; n       ansC = C; n     } n     return; n   } n   for (long long i = v[x]; ~i; i--) { n     v[x] -= i; n     dfs1(x + 1, a * pw[x][i]); n     v[x] += i; n   } n } n void doit() { n   read(n); n   V = 1; n   for (long long i = 1, x, y; i <= n; i++) { n     read(x, y); n     v[i] = y; n     pw[i][0] = 1; n     for (long long j = 1; j <= y; j++) pw[i][j] = pw[i][j - 1] * x; n     V *= pw[i][y]; n   } n   ansS = 1ll << 62; n   dfs1(1, 1); n   write(ansS); n   putchar(   ); n   write(ansA); n   putchar(   ); n   write(ansB); n   putchar(   ); n   write(ansC); n   puts(  ); n } n signed main() { n   long long t; n   read(t); n   while (t--) doit(); n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   int t; n   cin >> t; n   while (t--) { n     string n; n     int s; n     cin >> n >> s; n     reverse(n.begin(), n.end()); n     char temp[] =  0 ; n     n.insert(n.size(), temp); n     int sum = 0; n     for (int i = 0; i < n.size(); i++) sum += n[i] - 48; n     char ans[25]; n     for (int i = 0; i < 25; i++) ans[i] = 0; n     ans[0] =  0 ; n     int i = 0; n     while (sum > s) { n       int dig = (n[i] - 48); n       int car = 0; n       sum -= dig; n       n[i] =  0 ; n       if (dig > 0) { n         ans[i] = 10 - dig + 48; n         car = 1; n       } else n         ans[i] =  0 ; n       for (int j = i + 1; car; j++) { n         int t = (n[j] - 48 + car); n         if (t == 10) n           sum -= n[j] - 48; n         else n           sum++; n         n[j] = (t % 10) + 48; n         car = t / 10; n       } n       i++; n     } n     int size = 0; n     while (ans[++size]) n       ; n     reverse(ans, ans + size); n     cout << ans <<   n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n long long sqr(long long x) { return x * x; } n int mysqrt(long long x) { n   int l = 0, r = 1e9 + 1; n   while (r - l > 1) { n     int m = (l + r) / 2; n     if (m * (long long)m <= x) n       l = m; n     else n       r = m; n   } n   return l; n } n mt19937 rnd(1227); n mt19937_64 rndll(12365); n long long AR = 19, BR = 13, CR = 23, XR = 228, YR = 322, MODR = 1e9 + 993; n long long myrand() { n   long long ZR = (XR * AR + YR * BR + CR) % MODR; n   XR = YR; n   YR = ZR; n   return ZR; n } n const int Mod = 1e9 + 9; n int bpow(int x, int y) { n   if (y == 0) return 1; n   if (y == 1) return x; n   int ret = bpow(x, y >> 1); n   ret = (ret * (long long)ret) % Mod; n   if (y & 1) ret = (ret * (long long)x) % Mod; n   return ret; n } n int bdiv(int x, int y) { return (x * (long long)bpow(y, Mod - 2)) % Mod; } n void setmin(int &x, int y) { x = min(x, y); } n void setmax(int &x, int y) { x = max(x, y); } n void setmin(long long &x, long long y) { x = min(x, y); } n void setmax(long long &x, long long y) { x = max(x, y); } n int gcd(int a, int b) { return a ? gcd(b % a, a) : b; } n const long long llinf = 2e18 + 100; n const double eps = 1e-9; n const int maxn = 2e5 + 100, maxw = 1e7 + 100, inf = 2e9 + 100, sq = 300, n           mod = 1e9 + 9, LG = 17; n int n; n vector<int> e[maxn]; n int a[maxn]; n vector<int> ans; n bool vis[maxn]; n void go(int v, int par) { n   ans.push_back(v); n   vis[v] = 1; n   for (int i : e[v]) n     if (i != par && !vis[i]) go(i, v); n } n void dfs(int v, int par) { n   for (int i : e[v]) n     if (i != par) { n       dfs(i, v); n       if (vis[i]) a[v] ^= 1; n     } n   if (a[v] == 0) go(v, par); n } n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   cin >> n; n   for (int i = 0; i < n; i++) { n     int v = i, u; n     cin >> u; n     u--; n     if (u >= 0) { n       e[v].push_back(u); n       e[u].push_back(v); n       a[v] ^= 1; n       a[u] ^= 1; n     } n   } n   dfs(0, -1); n   if (ans.size() < n) n     cout <<  NO ; n   else { n     cout <<  YES n ; n     for (int i : ans) cout << i + 1 <<   n ; n   } n } n