#include <bits/stdc++.h> n using namespace std; n const double pi = acos(-1); n inline double cross(double x1, double y1010101010101, double x2, double y2) { n   return x1 * x2 + y1010101010101 * y2; n } n inline double len(double x, double y) { return sqrt(x * x + y * y); } n inline double angle(double x1, double y1010101010101, double x2, double y2) { n   if (x1 == 0 && y1010101010101 == 0) return pi; n   if (x2 == 0 && y2 == 0) return pi; n   return acos(cross(x1, y1010101010101, x2, y2) / len(x1, y1010101010101) / n               len(x2, y2)); n } n int n, x[100005], y[100005], cur = 0; n struct data { n   double tx, ty; n   int fx, fy; n   int cx, cy; n   data operator~() { n     data ans = *this; n     ans.tx = -ans.tx; n     ans.ty = -ans.ty; n     ans.cx = -ans.cx; n     ans.cy = -ans.cy; n     return ans; n   } n } d[100005 << 5]; n inline data add(int x, int y, int cx, int cy) { n   data ans; n   ans.tx = d[x].tx * cx + d[y].tx * cy; n   ans.ty = d[x].ty * cx + d[y].ty * cy; n   ans.cx = cx; n   ans.cy = cy; n   ans.fx = x; n   ans.fy = y; n   return ans; n } n int neg[100005 << 5]; n inline void dump(int x) { n   if (x <= n) return; n   neg[d[x].fx] = neg[x] * d[x].cx; n   neg[d[x].fy] = neg[x] * d[x].cy; n   dump(d[x].fx); n   dump(d[x].fy); n } n inline void dfs(int x, int y, int z, int w) { n   if (z == n + 1) { n     d[++cur] = ~d[x]; n     d[++cur] = ~d[y]; n     vector<int> t; n     t.push_back(x); n     t.push_back(y); n     t.push_back(cur - 1); n     t.push_back(cur); n     for (int i = 0; i < 4; i += 2) n       for (int j = 1; j < 4; j += 2) { n         int u = t[i], v = t[j]; n         if (len(d[u].tx + d[v].tx, d[u].ty + d[v].ty) <= 1500000.0) { n           if (i == 2) n             neg[x] = -1; n           else n             neg[x] = 1; n           if (j == 3) n             neg[y] = -1; n           else n             neg[y] = 1; n           dump(x); n           dump(y); n           return; n         } n       } n   } n   d[++cur] = ~d[x]; n   d[++cur] = ~d[y]; n   d[++cur] = ~d[z]; n   vector<int> t; n   t.push_back(x); n   t.push_back(y); n   t.push_back(z); n   t.push_back(cur - 2); n   t.push_back(cur - 1); n   t.push_back(cur); n   for (int i = 0; i < 6; i++) n     for (int j = 0; j < i; j++) { n       if ((i % 3) == (j % 3)) continue; n       int a = t[i], b = t[j]; n       if (angle(d[a].tx, d[a].ty, d[b].tx, d[b].ty) >= 2.0 * pi / 3) { n         d[++cur] = add(a, b, 1, 1); n         int nxt = 3 - (i % 3) - (j % 3); n         if (nxt == 0) dfs(cur, x, w, w + 1); n         if (nxt == 1) dfs(cur, y, w, w + 1); n         if (nxt == 2) dfs(cur, z, w, w + 1); n         return; n       } n     } n } n int main() { n   scanf( %d , &n); n   if (n == 1) return puts( 1 ), 0; n   for (int i = 1; i <= n; i++) scanf( %d%d , &x[i], &y[i]); n   for (int i = 1; i <= n; i++) { n     d[i].tx = x[i]; n     d[i].ty = y[i]; n     d[i].fx = i; n     d[i].fy = 0; n     d[i].cx = 1; n     d[i].cy = 0; n   } n   cur = n; n   dfs(1, 2, 3, 4); n   for (int i = 1; i <= n; i++) printf( %d  , neg[i]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 1e5 + 10; n const int mod = 1e9 + 7; n const long long inf = 1e17; n char a[maxn], b[maxn], s[maxn]; n int v[200]; n int main() { n   int q; n   scanf( %s%s , a, b); n   int n = strlen(a); n   for (int i = 0; i < n; ++i) v[a[i]] = 1; n   n = strlen(b); n   scanf( %d , &q); n   while (q--) { n     scanf( %s , s); n     int len = strlen(s); n     int o = len - n + 1; n     if (o < 0) { n       puts( NO ); n       continue; n     } n     int i = 0, j = 0, k; n     while (i < n) { n       if (b[i] == s[j]) { n         i++; n         j++; n       } else if (b[i] ==  ?  && v[s[j]]) { n         i++; n         j++; n       } else if (b[i] ==  * ) { n         for (k = 0; k < o && j < len; ++k, ++j) n           if (v[s[j]]) break; n         if (k < o) break; n         i++; n       } else { n         break; n       } n     } n     if (i < n || j < len) n       puts( NO ); n     else n       puts( YES ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct hehe { n   unsigned long long l, r; n   mutable bool z; n   inline bool operator<(hehe y) const { return l < y.l; } n }; n set<hehe> s; n inline set<hehe>::iterator split(unsigned long long pos) { n   hehe qaq; n   qaq.l = pos; n   qaq.r = 0; n   qaq.z = 0; n   set<hehe>::iterator itp = s.lower_bound(qaq); n   if (itp->l == pos && itp != s.end()) return itp; n   itp--; n   unsigned long long l = itp->l, r = itp->r; n   bool val = itp->z; n   s.erase(itp); n   qaq.l = l; n   qaq.r = pos - 1; n   qaq.z = val; n   s.insert(qaq); n   qaq.l = pos; n   qaq.r = r; n   qaq.z = val; n   set<hehe>::iterator ret = s.insert(qaq).first; n   return ret; n } n inline unsigned long long read() { n   unsigned long long s = 0; n   char ch = getchar(); n   while (ch <  0  || ch >  9 ) ch = getchar(); n   while (ch >=  0  && ch <=  9 ) s = s * 10 + ch -  0 , ch = getchar(); n   return s; n } n inline void rev(unsigned long long l, unsigned long long r) { n   set<hehe>::iterator itr, itl; n   itr = split(r + 1); n   itl = split(l); n   for (set<hehe>::iterator it = itl; it != itr; ++it) it->z ^= 1; n } n inline void fz(unsigned long long l, unsigned long long r, n                unsigned long long val) { n   set<hehe>::iterator itl, itr; n   itr = split(r + 1); n   itl = split(l); n   s.erase(itl, itr); n   hehe qwq; n   qwq.l = l; n   qwq.r = r; n   qwq.z = val; n   s.insert(qwq); n } n inline unsigned long long que() { n   for (set<hehe>::iterator it = s.begin(); it != s.end(); ++it) { n     if (it->z == 0) return it->l; n   } n   return 1000000000000000001llu; n } n signed main() { n   unsigned long long n; n   n = read(); n   hehe qwq; n   qwq.l = 1; n   qwq.r = 1000000000000000000llu; n   qwq.z = 0; n   s.insert(qwq); n   while (n--) { n     unsigned long long opt, l, r; n     opt = read(); n     l = read(); n     r = read(); n     if (opt == 1) n       fz(l, r, 1); n     else if (opt == 2) n       fz(l, r, 0); n     else if (opt == 3) n       rev(l, r); n     printf( %lld n , que()); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n multiset<int> st; n multiset<int>::iterator it; n int arr[200000]; n int main() { n   int n, i, a, b, c, ans = 0, temp[3]; n   scanf( %d , &n); n   scanf( %d , &temp[0]); n   scanf( %d , &temp[1]); n   scanf( %d , &temp[2]); n   sort(temp, temp + 3); n   a = temp[0]; n   b = temp[1]; n   c = temp[2]; n   for (i = 0; i < n; i++) { n     scanf( %d , &arr[i]); n     st.insert(-arr[i]); n   } n   for (i = 0; i < n; i++) n     if (arr[i] > a + b + c) { n       printf( -1 n ); n       return 0; n     } n   while (!st.empty()) { n     it = st.lower_bound(-1 * (a + b + c)); n     if (it == st.end() or -1 * (*it) <= (b + c)) break; n     st.erase(it); n     ans++; n   } n   while (!st.empty()) { n     it = st.lower_bound(-1 * (b + c)); n     if (it == st.end() or -1 * (*it) <= (a + c)) break; n     st.erase(it); n     it = st.lower_bound(-1 * (a)); n     if (it != st.end()) st.erase(it); n     ans++; n   } n   while (!st.empty()) { n     it = st.lower_bound(-1 * (a + c)); n     if (it == st.end() or -1 * (*it) <= (max(a + b, c))) break; n     st.erase(it); n     it = st.lower_bound(-1 * (b)); n     if (it != st.end()) st.erase(it); n     ans++; n   } n   if ((a + b) > c) { n     while (!st.empty()) { n       it = st.lower_bound(-1 * (a + b)); n       if (it == st.end() or -1 * (*it) <= c) break; n       st.erase(it); n       it = st.lower_bound(-1 * (c)); n       if (it != st.end()) st.erase(it); n       ans++; n     } n   } n   while (!st.empty()) { n     it = st.lower_bound(-1 * (c)); n     if (it == st.end()) break; n     st.erase(it); n     it = st.lower_bound(-1 * (b)); n     if (it != st.end()) { n       st.erase(it); n       it = st.lower_bound(-1 * (a)); n       if (it != st.end()) st.erase(it); n     } else { n       it = st.lower_bound(-1 * (a + b)); n       if (it != st.end()) st.erase(it); n     } n     ans++; n   } n   printf( %d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n signed main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   int n, m, k; n   cin >> n >> m >> k; n   int best = 1e9; n   for (int i = 0; i < k; i++) { n     int x, y; n     cin >> x >> y; n     best = min(best, x - 1); n     best = min(best, n - x); n     best = min(best, y - 1); n     best = min(best, m - y); n   } n   if (best > 4) n     cout <<  NO n ; n   else n     cout <<  YES n ; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   vector<int> ans1; n   vector<char> ans2; n   int n1, n2; n   cin >> n1; n   vector<int> a1(n1); n   for (int i = 0; i < n1; i++) { n     cin >> a1[i]; n   } n   cin >> n2; n   vector<int> a2(n2); n   for (int i = 0; i < n2; i++) { n     cin >> a2[i]; n   } n   int now1 = 0, now2 = 0; n   for (int i = 0; i < n2; i++) { n     int s = 0; n     while (now2 < n1 && s < a2[i]) { n       s += a1[now2]; n       now2++; n     } n     if (now2 > n1 || s != a2[i] || now1 == now2) { n       cout <<  NO ; n       return 0; n     } n     int temp = now1; n     for (int j = now1; j < now2; j++) { n       if (a1[temp] <= a1[j] && (j - 1 >= now1 && a1[j - 1] < a1[j] || n                                 j + 1 < now2 && a1[j + 1] < a1[j])) { n         temp = j; n       } n     } n     if (temp == now1 && temp + 1 < now2 && a1[temp + 1] == a1[temp]) { n       cout <<  NO ; n       return 0; n     } n     if (temp - 1 >= now1 && a1[temp] > a1[temp - 1]) { n       int him = a1[temp]; n       int j = temp - 1; n       while (j >= now1 && him > a1[j]) { n         him += a1[j]; n         ans1.push_back(i + j - now1 + 2); n         ans2.push_back( L ); n         j--; n       } n       j = temp + 1; n       while (j < now2 && him > a1[j]) { n         him += a1[j]; n         ans1.push_back(i + 1); n         ans2.push_back( R ); n         j++; n       } n     } else { n       if (temp + 1 < now2 && a1[temp] > a1[temp + 1]) { n         int him = a1[temp]; n         int j = temp + 1; n         while (j < now2 && him > a1[j]) { n           him += a1[j]; n           ans1.push_back(i + temp - now1 + 1); n           ans2.push_back( R ); n           j++; n         } n         j = temp - 1; n         while (j >= now1 && him > a1[j]) { n           him += a1[j]; n           ans1.push_back(i + j - now1 + 2); n           ans2.push_back( L ); n           j--; n         } n       } n     } n     now1 = now2; n     now2 = now1; n   } n   if (now2 != a1.size()) { n     cout <<  NO ; n     return 0; n   } n   cout <<  YES  << endl; n   for (int i = 0; i < ans1.size(); i++) { n     cout << ans1[i] <<     << ans2[i] << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long mod = 998244353; n int t; n int n; n long long dp[500010][2]; n long long x; n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   ; n   cin >> t; n   while (t--) { n     cin >> n; n     for (int i = 1; i <= n + 1; i++) { n       dp[i][0] = 0; n       dp[i][1] = 0; n     } n     dp[0][0] = 1; n     long long ans = 0; n     for (int i = 1; i <= n; i++) { n       cin >> x; n       x++; n       dp[x][0] = (dp[x][0] + dp[x][0]) % mod; n       dp[x][0] = (dp[x][0] + dp[x - 1][0]) % mod; n       dp[x][1] = (dp[x][1] + dp[x][1]) % mod; n       dp[x + 2][1] = (dp[x + 2][1] + dp[x + 2][1]) % mod; n       if (x > 1) { n         dp[x][1] = (dp[x][1] + dp[x - 2][0]) % mod; n       } n     } n     for (int i = 1; i <= n + 1; i++) { n       ans = (ans + dp[i][0]) % mod; n       ans = (ans + dp[i][1]) % mod; n     } n     cout << ans <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long t, n; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   cin >> t; n   while (t--) { n     cin >> n; n     long long k = 0, l = 0; n     for (int i = 0; i < (n); i++) { n       long long a; n       cin >> a; n       if (a % 2 != i % 2) n         if (a % 2) n           k++; n         else n           l++; n     } n     if (k != l) n       cout <<  -1 n ; n     else n       cout << k <<   n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1000010; n int tot; n struct node { n   node *ch[26], *fail; n   int len, id; n   node* init(int tlen) { n     memset(ch, 0, sizeof(ch)); n     fail = NULL; n     len = tlen; n     return this; n   } n } * root, spT[N]; n vector<int> g[N]; n void addin(char* c) { n   node* tmp = root; n   while (*c !=   0 ) { n     int t = *c -  a ; n     if (tmp->ch[t] == NULL) tmp->ch[t] = spT[++tot].init(tmp->len + 1); n     tmp = tmp->ch[t]; n     c++; n   } n } n queue<node*> q; n int en, st; n void getfail() { n   q.push(root); n   while (!q.empty()) { n     node* tmp = q.front(); n     q.pop(); n     for (int t = 0; t < 26; t++) n       if (tmp->ch[t] != NULL) { n         if (tmp == root) n           tmp->ch[t]->fail = root; n         else { n           node* p = tmp->fail; n           while (p != root && p->ch[t] == NULL) p = p->fail; n           if (p->ch[t] != NULL) n             tmp->ch[t]->fail = p->ch[t]; n           else n             tmp->ch[t]->fail = root; n         } n         q.push(tmp->ch[t]); n       } n   } n } n char S[N]; n int f[N][2]; n int dfs(int x) { n   f[x][0] = 0; n   f[x][1] = 1; n   for (auto p : g[x]) { n     dfs(p); n     f[x][1] += f[p][0]; n     f[x][0] += max(f[p][0], f[p][1]); n   } n   return max(f[x][0], f[x][1]); n } n bool v[N]; n int main() { n   int T, n; n   for (int i = (1); i <= (N - 1); i++) spT[i].id = i; n   cin >> T; n   while (T--) { n     root = spT[tot = 1].init(0); n     cin >> n; n     for (int i = (1); i <= (n); i++) { n       scanf( %s , S); n       addin(S); n     } n     getfail(); n     for (int i = (1); i <= (tot); i++) g[i].clear(), v[i] = 0; n     for (int i = (1); i <= (tot); i++) { n       node* tmp = &spT[i]; n       if (tmp->fail && tmp->fail != root && tmp->fail->len + 1 == tmp->len) { n         g[tmp->fail->id].push_back(tmp->id); n         v[tmp->id] = 1; n       } n     } n     int ans = 0; n     for (int i = (2); i <= (tot); i++) n       if (!v[i]) ans += dfs(i); n     cout << ans << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios::sync_with_stdio(false); n   string cad; n   cin >> cad; n   map<char, int> M; n   for (int i = 0; i < cad.size(); i++) M[cad[i]]++; n   int xx = M[ x ]; n   int yy = M[ y ]; n   int ans = max(xx, yy) - min(xx, yy); n   if (max(xx, yy) == M[ x ]) n     cout << string(ans,  x ) << endl; n   else n     cout << string(ans,  y ) << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 200 * 1000 + 85; n int n, m; n int a[N]; n vector<int> vec[N]; n pair<pair<int, int>, int> b[N]; n int mapp[N]; n int ans[N]; n int seg[N << 2]; n inline void add(int, int, int = 1, int = 0, int = N); n inline int get(int, int = 1, int = 0, int = N); n int main() { n   ios::sync_with_stdio(0), cin.tie(0), cout.tie(0); n   cin >> n >> m; n   for (int i = 0; i < n; i++) { n     cin >> a[i]; n     mapp[a[i]] = i; n   } n   for (int i = 1; i <= n; i++) { n     int j = 1; n     while (j * i <= n) { n       if (mapp[j * i] >= mapp[i]) vec[mapp[j * i]].push_back(mapp[i]); n       if (mapp[i] > mapp[j * i]) vec[mapp[i]].push_back(mapp[j * i]); n       j++; n     } n   } n   for (int i = 0; i < m; i++) { n     cin >> b[i].first.second >> b[i].first.first; n     b[i].first.second--; n     b[i].first.first--; n     b[i].second = i; n   } n   sort(b, b + m); n   int k = 0; n   for (int t = 0; t < m; t++) { n     while (k <= b[t].first.first) { n       for (int i = 0; i < vec[k].size(); i++) add(0, vec[k][i] + 1); n       k++; n     } n     ans[b[t].second] = get(b[t].first.second); n   } n   for (int i = 0; i < m; i++) cout << ans[i] <<   n ; n } n inline void add(int st, int en, int id, int l, int r) { n   if (en <= l or r <= st) return; n   if (st <= l and r <= en) { n     seg[id]++; n     return; n   } n   int mid = (l + r) >> 1, lc = id << 1, rc = lc | 1; n   add(st, en, lc, l, mid); n   add(st, en, rc, mid, r); n } n inline int get(int idx, int id, int l, int r) { n   if (idx < l or r <= idx) return 0; n   if (l + 1 == r) return seg[id]; n   int mid = (l + r) >> 1, lc = id << 1, rc = lc | 1; n   return get(idx, lc, l, mid) + get(idx, rc, mid, r) + seg[id]; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m, cnt; n vector<int> e[105]; n bool f[105]; n void dfs(int x) { n   f[x] = 1, ++cnt; n   for (int i = 0; i < e[x].size(); ++i) n     if (!f[e[x][i]]) dfs(e[x][i]); n } n void work() { n   int i, x, y; n   scanf( %d%d , &n, &m); n   if (n != m) { n     puts( NO ); n     return; n   } n   for (i = 1; i <= m; ++i) { n     scanf( %d%d , &x, &y); n     e[x].push_back(y); n     e[y].push_back(x); n   } n   dfs(1); n   if (cnt == n) n     puts( FHTAGN! ); n   else n     puts( NO ); n } n int main() { n   work(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long INF = 1e9 + 7; n const int N = 2e2 + 10; n int main() { n   int n, m; n   cin >> n >> m; n   int l = 1, r = INF; n   int ans = 0; n   while (l <= r) { n     int mid = (r + l) / 2; n     int z = mid / 6; n     int x = mid / 2 - z; n     int y = mid / 3 - z; n     if (z >= max(0, n - x) + max(0, m - y)) n       ans = mid, r = mid - 1; n     else n       l = mid + 1; n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m; n int cnta[10], cntb[10]; n int a[15][2], b[15][2]; n int one_common(int x, int y) { n   if (a[x][0] == b[y][0] && a[x][1] != b[y][1]) return a[x][0]; n   if (a[x][1] == b[y][0] && a[x][0] != b[y][1]) return a[x][1]; n   if (a[x][0] == b[y][1] && a[x][1] != b[y][0]) return a[x][0]; n   if (a[x][1] == b[y][1] && a[x][0] != b[y][0]) return a[x][1]; n   return 0; n } n int main() { n   scanf( %d%d , &n, &m); n   int x, y; n   for (int i = 0; i < n; i++) { n     scanf( %d%d , &x, &y); n     cnta[x]++; n     cnta[y]++; n     a[i][0] = x; n     a[i][1] = y; n   } n   for (int i = 0; i < m; i++) { n     scanf( %d%d , &x, &y); n     cntb[x]++; n     cntb[y]++; n     b[i][0] = x; n     b[i][1] = y; n   } n   set<int> num; n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < m; j++) { n       if (one_common(i, j)) num.insert(one_common(i, j)); n     } n   } n   if (num.size() == 1) { n     printf( %d n , *num.begin()); n     return 0; n   } n   for (int i = 0; i < n; i++) { n     set<int> num; n     for (int j = 0; j < m; j++) n       if (one_common(i, j)) num.insert(one_common(i, j)); n     if (num.size() > 1) { n       printf( -1 n ); n       return 0; n     } n   } n   for (int i = 0; i < m; i++) { n     set<int> num; n     for (int j = 0; j < n; j++) n       if (one_common(j, i)) num.insert(one_common(j, i)); n     if (num.size() > 1) { n       printf( -1 n ); n       return 0; n     } n   } n   printf( 0 n ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios::sync_with_stdio(false); n   cin.tie(nullptr); n   cout.tie(nullptr); n   int n; n   cin >> n; n   const int MAX = 1e6; n   vector<bool> t(MAX + 1); n   for (int i = 0; i < n; ++i) { n     int x; n     cin >> x; n     t[x] = true; n   } n   int d = 0; n   vector<int> sol; n   for (int i = 1; i <= MAX / 2; ++i) { n     if (t[i] ^ t[MAX - i + 1]) { n       sol.push_back(t[i] ? MAX - i + 1 : i); n       t[i] = t[MAX - i + 1] = true; n     } else if (t[i]) { n       ++d; n     } n   } n   for (int i = 1; i <= MAX / 2 && d; ++i) { n     if (!t[i]) { n       sol.push_back(MAX - i + 1); n       sol.push_back(i); n       --d; n     } n   } n   cout << sol.size() <<   n ; n   for (int i : sol) { n     cout << i <<    ; n   } n   cout <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int INF = 1000000007; n const long long INF64 = 2000000000000000007; n const int eps = 10e-6; n long long solve(vector<int> one, vector<int> two, vector<int> three) { n   long long min_w = INF64; n   for (auto x : one) { n     long long b = x; n     auto a = lower_bound(two.begin(), two.end(), b); n     auto c = upper_bound(three.begin(), three.end(), b); n     if (a == two.end() || c == three.begin()) continue; n     long long aa = two[a - two.begin()]; n     long long cc = three[c - three.begin() - 1]; n     min_w = min(min_w, (aa - b) * (aa - b) + (b - cc) * (b - cc) + n                            (cc - aa) * (cc - aa)); n   } n   return min_w; n } n int main() { n   int t; n   cin >> t; n   for (int i = 0; i < t; i++) { n     int k, l, m; n     cin >> k >> l >> m; n     vector<int> red(k); n     vector<int> green(l); n     vector<int> blue(m); n     for (int i = 0; i < k; i++) cin >> red[i]; n     for (int i = 0; i < l; i++) cin >> green[i]; n     for (int i = 0; i < m; i++) cin >> blue[i]; n     sort(red.begin(), red.end()); n     sort(blue.begin(), blue.end()); n     sort(green.begin(), green.end()); n     long long ans = INF64; n     ans = min(ans, solve(red, green, blue)); n     ans = min(ans, solve(red, blue, green)); n     ans = min(ans, solve(green, red, blue)); n     ans = min(ans, solve(green, blue, red)); n     ans = min(ans, solve(blue, green, red)); n     ans = min(ans, solve(blue, red, green)); n     cout << ans << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m, a[100001]; n long long s[100001], ans[100001]; n int main() { n   cin >> n; n   for (int i = 1; i <= n; i++) cin >> a[i]; n   sort(a + 1, a + n + 1); n   reverse(a + 1, a + n + 1); n   for (int i = 1; i <= n; i++) s[i] = s[i - 1] + a[i]; n   cin >> m; n   for (int i = 0; i < m; i++) { n     int k; n     cin >> k; n     if (!ans[k]) { n       long long res = 0; n       long long koef = 1, cnt = k, j0 = 2, j1; n       while (j0 <= n) { n         j1 = min((long long)n, j0 + cnt - 1); n         res += (s[j1] - s[j0 - 1]) * koef; n         j0 = j0 + cnt; n         koef++; n         cnt *= k; n       } n       ans[k] = res; n     } n     cout << ans[k] <<    ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   char s[101]; n   cin >> s; n   int k = 1; n   for (int i = 1; s[i] !=   0 ; ++i) { n     if (s[i] == s[i - 1]) n       k++; n     else n       k = 1; n     if (k == 7) break; n   } n   if (k >= 7) n     cout <<  YES ; n   else n     cout <<  NO ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<vector<int> > AdjList; n vector<int> dist; n vector<int> unused; n void update() { n   queue<int> now; n   for (int i = 0; i < unused.size(); i++) { n     dist[unused[i]] = 0; n     now.push(unused[i]); n   } n   while (!now.empty()) { n     int u = now.front(); n     now.pop(); n     for (int i = 0; i < AdjList[u].size(); i++) { n       int v = AdjList[u][i]; n       if (dist[u] + 1 < dist[v]) { n         dist[v] = dist[u] + 1; n         now.push(v); n       } n     } n   } n   unused.clear(); n } n int main() { n   int V, q; n   cin >> V >> q; n   AdjList.assign(V + 1, vector<int>()); n   dist.assign(V + 1, -1); n   for (int x, y, i = 0; i < V - 1; i++) { n     cin >> x >> y; n     AdjList[x].push_back(y); n     AdjList[y].push_back(x); n   } n   dist[1] = 0; n   queue<int> now; n   now.push(1); n   while (!now.empty()) { n     int u = now.front(); n     now.pop(); n     for (int i = 0; i < AdjList[u].size(); i++) { n       int v = AdjList[u][i]; n       if (dist[v] == -1) { n         dist[v] = dist[u] + 1; n         now.push(v); n       } n     } n   } n   for (int t, from, i = 0; i < q; i++) { n     cin >> t >> from; n     if (t == 1) { n       unused.push_back(from); n     } else { n       update(); n       cout << dist[from] << endl; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int x[20005]; n int y[20005]; n int main() { n   int n; n   scanf( %d , &n); n   for (int i = 0; i < n; i++) { n     int xx, yy; n     scanf( %d %d , &xx, &yy); n     x[xx + 10000]++; n     y[yy + 10000]++; n   } n   long long ret = 0, sumx = 0, sumy = 0, tx = 0, ty = 0, dx = 0, dy = 0; n   for (int i = 0; i <= 20000; i++) { n     ret += (long long)x[i] * tx; n     ret += (long long)y[i] * ty; n     dx += sumx; n     dy += sumy; n     sumx += x[i]; n     sumy += y[i]; n     dx += sumx; n     dy += sumy; n     tx += dx; n     ty += dy; n   } n   cout << ret << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n bool maze[3][100001]; n int judge(int r, int c) { n   if (r == 1) { n     if (c == 100000) return maze[2][99999] + maze[2][100000]; n     return maze[2][c - 1] + maze[2][c] + maze[2][c + 1]; n   } else { n     if (c == 1) return maze[1][1] + maze[1][2]; n     if (c == 100000) return maze[1][c - 1] + maze[1][c]; n     return maze[1][c - 1] + maze[1][c] + maze[1][c + 1]; n   } n } n int main() { n   memset(maze, 0, sizeof(maze)); n   int n, q, cont = 0, r, c; n   cin >> n >> q; n   for (int i = 1; i <= q; i++) { n     cin >> r >> c; n     if (maze[r][c] == 0) { n       maze[r][c] = 1; n       cont += judge(r, c); n     } else { n       maze[r][c] = 0; n       cont -= judge(r, c); n     } n     cout << (cont == 0 ?  Yes  :  No ) << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long int ESP = 1e18; n long long int FCB = 1e9 + 7; n long long int dir[][2] = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}}; n const long long int N = 5005; n long long int l[N], r[N]; n vector<pair<long long int, long long int>> vv; n bool fly(long long int x, long long int y) { n   if (l[x] >= r[y]) return false; n   if (r[x] <= l[y]) return false; n   return true; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   ; n   long long int n; n   cin >> n; n   for (long long int i = 1; i < n + 1; i++) cin >> l[i] >> r[i]; n   for (long long int i = 1; i < n + 1; i++) { n     for (long long int j = 1; j < n + 1; j++) { n       if (i != j && fly(i, j)) vv.push_back(make_pair(i, j)); n     } n   } n   map<long long int, long long int> mp; n   long long int zz = vv.size(); n   for (long long int i = 0; i < zz; i++) { n     mp[vv[i].first]++; n     mp[vv[i].second]++; n   } n   vector<long long int> ans; n   for (long long int i = 1; i <= n; i++) { n     if (mp[i] == zz) ans.push_back(i); n   } n   long long int dd = ans.size(); n   cout << dd <<   n ; n   for (long long int i = 0; i < dd; i++) cout << ans[i] <<    ; n   cout <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m, k, a[111111]; n int main() { n   cin >> n >> m >> k; n   if (n % 2 == 0) { n     cout << 0 << endl; n     return 0; n   } n   for (int i = 0; i < n; i++) cin >> a[i]; n   int maxans = (int)2e9; n   for (int i = 0; i < n; i += 2) n     if (a[i] < maxans) maxans = a[i]; n   cout << min((long long)maxans, ((long long)m / ((n + 1) / 2)) * k) << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int a[222], b[222]; n int main() { n   int n, ans = 0; n   cin >> n; n   int p, q, r, s; n   for (int i = 0; i < n; i++) cin >> a[i] >> b[i]; n   for (int i = 0; i < n; i++) { n     p = q = r = s = 0; n     for (int j = 0; j < n; j++) { n       if (j == i) continue; n       if (a[j] > a[i] && b[j] == b[i]) p++; n       if (a[j] < a[i] && b[j] == b[i]) q++; n       if (a[j] == a[i] && b[j] > b[i]) r++; n       if (a[j] == a[i] && b[j] < b[i]) s++; n     } n     if (p && q && r && s) ans++; n   } n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   int n; n   cin >> n; n   int i, temp, temp2, a[n + 1]; n   for (int i = 0; i < n + 1; i++) a[i] = 0; n   for (int i = 0; i < n - 1; i++) { n     cin >> temp >> temp2; n     a[temp]++; n     a[temp2]++; n   } n   int s = 0; n   for (int i = 0; i < n + 1; i++) { n     if (a[i] < 2) s++; n   } n   cout << s - 1 <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int t, num[30], ans; n char s[100005]; n int main() { n   cin >> t; n   while (t--) { n     ans = 0; n     memset(num, 0, sizeof(num)); n     scanf( %s , s + 1); n     int len = strlen(s + 1); n     for (int i = 1; i <= len; i++) { n       num[s[i] -  A  + 1]++; n     } n     if (num[ L  -  A  + 1] != 0 && num[ R  -  A  + 1] != 0 && n         (num[ U  -  A  + 1] == 0 || num[ D  -  A  + 1] == 0)) { n       printf( 2 n ); n       printf( LR n ); n     } else if (num[ U  -  A  + 1] != 0 && num[ D  -  A  + 1] != 0 && n                (num[ L  -  A  + 1] == 0 || num[ R  -  A  + 1] == 0)) { n       printf( 2 n ); n       printf( UD n ); n     } else if (num[ L  -  A  + 1] != 0 && num[ R  -  A  + 1] != 0 && n                num[ U  -  A  + 1] != 0 && num[ D  -  A  + 1] != 0) { n       ans += min(num[ L  -  A  + 1], num[ R  -  A  + 1]) * 2; n       ans += min(num[ U  -  A  + 1], num[ D  -  A  + 1]) * 2; n       printf( %d n , ans); n       for (int i = 1; i <= min(num[ L  -  A  + 1], num[ R  -  A  + 1]); i++) { n         printf( L ); n       } n       for (int i = 1; i <= min(num[ U  -  A  + 1], num[ D  -  A  + 1]); i++) { n         printf( U ); n       } n       for (int i = 1; i <= min(num[ L  -  A  + 1], num[ R  -  A  + 1]); i++) { n         printf( R ); n       } n       for (int i = 1; i <= min(num[ U  -  A  + 1], num[ D  -  A  + 1]); i++) { n         printf( D ); n       } n       printf(  n ); n     } else { n       printf( 0 n n ); n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n using namespace std; n const long long mod = 1e9 + 7; n const int inf = 1e9; n string s; n long long expMod(long long base, long long ex, long long mod) { n   if (ex == 0) return 1; n   long long temp = expMod(base, ex / 2, mod); n   temp = (temp * temp) % mod; n   if (ex % 2) temp = (temp * base) % mod; n   return temp; n } n long long solve(string x) { n   long long n = x.size(); n   if (n == 1) { n     if (x[0] ==  1 ) n       return 1; n     else n       return 0; n   } n   string temp = x.substr(1); n   long long sz = temp.size(); n   long long ret = 0; n   if (x[0] ==  0 ) { n     ret = (1LL * 2 * solve(temp)) % mod; n   } else { n     ret = (expMod(2, 2 * sz, mod) + 2 * solve(temp)) % mod; n   } n   return ret; n } n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cin >> s; n   cout << solve(s) <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n int main() { n   int n; n   scanf( %d , &n); n   int64_t sum1 = 0; n   int64_t sum2 = 0; n   int to_double = 0; n   int zero = 0; n   std::set<std::pair<int, int>> doubled; n   std::set<std::pair<int, int>> pend; n   for (int i = 1; i <= n; i++) { n     int x, y; n     scanf( %d%d , &x, &y); n     if (y > 0) { n       if (x == 1) to_double++; n       sum1 += y; n       pend.insert({y, x}); n     } else { n       y = -y; n       sum1 -= y; n       auto it = pend.find({y, x}); n       if (it != pend.end()) { n         pend.erase(it); n       } else { n         it = doubled.find({y, x}); n         doubled.erase(it); n         if (x == 0) zero--; n         sum2 -= y; n       } n       if (x == 1) { n         to_double--; n         if (doubled.size() > 0) { n           auto it = doubled.begin(); n           std::pair<int, int> now = *it; n           if (now.second == 0) zero--; n           doubled.erase(it); n           sum2 -= now.first; n           pend.insert(now); n         } n       } n     } n     if (doubled.size() < to_double && pend.size() > 0) { n       auto it = --pend.end(); n       std::pair<int, int> max = *it; n       doubled.insert(max); n       if (max.second == 0) zero++; n       sum2 += max.first; n       pend.erase(it); n     } n     if (doubled.size() > 0 && pend.size() > 0) { n       auto it = doubled.begin(); n       auto it2 = --pend.end(); n       doubled.erase(it); n       pend.erase(it2); n       std::pair<int, int> now1 = *it; n       std::pair<int, int> now2 = *it2; n       if (now2.first > now1.first) { n         sum2 += now2.first - now1.first; n         if (now2.second == 0) zero++; n         if (now1.second == 0) zero--; n         doubled.insert(now2); n         pend.insert(now1); n       } else { n         doubled.insert(now1); n         pend.insert(now2); n       } n     } n     if (doubled.size() > 0 && zero == 0 && doubled.size() == to_double) { n       auto it1 = doubled.begin(); n       std::pair<int, int> now = *it1; n       doubled.erase(it1); n       sum2 -= now.first; n       if (pend.size() > 0) { n         auto it2 = --pend.end(); n         std::pair<int, int> now2 = *it2; n         pend.erase(it2); n         doubled.insert(now2); n         zero++; n         sum2 += now2.first; n       } n       pend.insert(now); n     } n     printf( %lld n , sum1 + sum2); n   } n   return 0; n } n


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


#include <bits/stdc++.h> n using namespace std; n bool isSeparator(char ch) { return (ch ==  ,  || ch ==     || ch ==  ; ); } n int main() { n   int i, j; n   bool isNumber; n   string s, ss; n   vector<string> words[2]; n   cin >> s; n   s +=    ; n   ss =   , isNumber = true; n   for (i = 0; i < s.length(); i++) { n     if (isSeparator(s[i])) { n       if (ss.empty() || (isNumber && ss.length() > 1 && ss[0] ==  0 )) n         isNumber = false; n       words[isNumber].push_back(ss); n       ss =   ; n       isNumber = true; n     } else { n       ss += s[i]; n       if (!isdigit(s[i])) isNumber = false; n     } n   } n   for (j = 1; j >= 0; j--) { n     if (words[j].size()) { n       cout <<    ; n       for (i = 0; i < words[j].size(); i++) { n         if (i) cout <<  , ; n         cout << words[j][i]; n       } n       cout <<     << endl; n     } else n       cout <<  -  << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int mN = 222222; n int n; n int a[mN], outa[mN], outb[mN], ge[mN]; n vector<int> ans; n struct Bit { n   int c[mN]; n   void clear() { memset(c, 0, sizeof(c)); } n   int lob(int x) { return x & (-x); } n   void add(int x, int dt) { n     while (x <= n) { n       c[x] += dt; n       x += lob(x); n     } n   } n   int sum(int x) { n     int re = 0; n     while (x > 0) { n       re += c[x]; n       x -= lob(x); n     } n     return re; n   } n } bit; n void calc(int o[]) { n   for (int i = 1; i <= n; i++) { n     cin >> a[i]; n   } n   bit.clear(); n   for (int i = n; i >= 1; i--) { n     o[i] = bit.sum(a[i] + 1); n     bit.add(a[i] + 1, 1); n   } n } n int bins(int key) { n   int l = 1, r = n; n   key++; n   while (l < r) { n     int mid = (l + r) / 2; n     if ((mid)-bit.sum(mid) >= key) n       r = mid; n     else n       l = mid + 1; n   } n   return l - 1; n } n void out(int o[]) { n   bit.clear(); n   for (int i = 1; i <= n; i++) { n     int x = bins(o[i]); n     ans.push_back(x); n     bit.add(x + 1, 1); n   } n } n int main() { n   cin >> n; n   calc(outa); n   calc(outb); n   for (int i = n - 1; i >= 1; i--) { n     ge[i] += outa[i] + outb[i]; n     int mod = (n + 1) - i; n     if (ge[i] >= mod) { n       ge[i - 1] += ge[i] / mod; n       ge[i] %= mod; n     } n   } n   out(ge); n   for (int i = 0, n_ = (ans.size()); i < n_; i++) n     cout << ans[i] << ((i == ans.size() - 1) ?   n  :    ); n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 4e6 + 7, M = 2e6; n const long long mod = 1e9 + 7; n inline int read() { n   int ret = 0; n   char ch = getchar(); n   bool f = 1; n   for (; !isdigit(ch); ch = getchar()) f ^= !(ch ^  - ); n   for (; isdigit(ch); ch = getchar()) ret = (ret << 1) + (ret << 3) + ch - 48; n   return f ? ret : -ret; n } n long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; } n long long ksm(long long a, long long b, long long mod) { n   int ans = 1; n   while (b) { n     if (b & 1) ans = (ans * a) % mod; n     a = (a * a) % mod; n     b >>= 1; n   } n   return ans; n } n long long inv2(long long a, long long mod) { return ksm(a, mod - 2, mod); } n int head[N], NEXT[M], ver[M], tot; n void link(int u, int v) { n   ver[++tot] = v; n   NEXT[tot] = head[u]; n   head[u] = tot; n } n int indeg[N], in[N], out[N], lazy[N], a[N]; n int ant; n struct node { n   int l, r, mid; n   int val; n   int maxn; n } tree[N]; n void dfs(int x) { n   in[x] = ++ant; n   for (int i = head[x]; i; i = NEXT[i]) { n     int y = ver[i]; n     dfs(y); n   } n   out[x] = ant; n } n void build(int x, int l, int r) { n   tree[x].l = l; n   tree[x].r = r; n   lazy[x] = 0; n   if (l == r) { n     tree[x].maxn = 0; n     return; n   } n   int mi = tree[x].mid = l + r >> 1; n   build(x << 1, l, mi); n   build(x << 1 | 1, mi + 1, r); n   tree[x].maxn = max(tree[x << 1].maxn, tree[x << 1 | 1].maxn); n } n void down(int x) { n   if (lazy[x] != 0) { n     lazy[x << 1] += lazy[x]; n     lazy[x << 1 | 1] += lazy[x]; n     tree[x << 1].maxn += lazy[x]; n     tree[x << 1 | 1].maxn += lazy[x]; n     lazy[x] = 0; n   } n } n void change(int x, int l, int r, int num) { n   if (l > tree[x].r || r < tree[x].l) return; n   if (l <= tree[x].l && r >= tree[x].r) { n     tree[x].maxn += num; n     lazy[x] += num; n     return; n   } else { n     down(x); n     if (l <= tree[x].mid) change(x << 1, l, r, num); n     if (r > tree[x].mid) change(x << 1 | 1, l, r, num); n     tree[x].maxn = max(tree[x << 1].maxn, tree[x << 1 | 1].maxn); n   } n } n int ask(int x, int l, int r) { return tree[x].maxn; } n int main() { n   int n, k; n   int ant = 0; n   scanf( %d%d , &n, &k); n   for (int i = 1; i <= n; i++) { n     scanf( %d , &a[i]); n   } n   stack<int> s; n   for (int i = 1; i <= n; i++) { n     if (s.empty()) n       s.push(i); n     else { n       while (!s.empty() && a[s.top()] < a[i]) { n         link(i, s.top()); n         indeg[s.top()]++; n         s.pop(); n       } n       s.push(i); n     } n   } n   for (int i = 1; i <= n; i++) { n     if (indeg[i]) continue; n     link(n + 1, i); n   } n   dfs(n + 1); n   build(1, in[n + 1], out[n + 1]); n   for (int i = 1; i <= n; i++) { n     if (i > k) { n       change(1, in[i - k], out[i - k], -1); n       change(1, in[i], out[i], 1); n       printf(  %d , ask(1, in[n + 1], out[n + 1])); n     } else { n       change(1, in[i], out[i], 1); n       if (i == k) printf( %d , ask(1, in[n + 1], out[n + 1])); n     } n   } n   puts(  ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long n, m, W, i, j, k; n   cin >> n >> m >> W; n   long long w[n], b[n]; n   for (i = 0; i < n; i++) cin >> w[i]; n   for (i = 0; i < n; i++) cin >> b[i]; n   vector<long long> G[n]; n   for (i = 0; i < m; i++) { n     cin >> j >> k; n     j--; n     k--; n     G[j].push_back(k); n     G[k].push_back(j); n   } n   bool u[n]; n   memset(u, 0, sizeof(u)); n   vector<pair<long long, long long> > v; n   vector<vector<long long> > vl; n   for (i = 0; i < n; i++) { n     if (u[i]) continue; n     queue<long long> q; n     vector<long long> l; n     q.push(i); n     pair<long long, long long> p = pair<long long, long long>(0, 0); n     while (!q.empty()) { n       k = q.front(); n       q.pop(); n       if (u[k]) continue; n       u[k] = 1; n       l.push_back(k); n       p.first += b[k]; n       p.second += w[k]; n       for (j = 0; j < G[k].size(); j++) q.push(G[k][j]); n     } n     v.push_back(p); n     vl.push_back(l); n   } n   long long V = v.size(), ans = 0; n   long long dp[V + 1][W + 1]; n   memset(dp, 0, sizeof(dp)); n   for (i = 0; i < V; i++) { n     for (j = 0; j < W; j++) { n       dp[i + 1][j] = max(dp[i + 1][j], dp[i][j]); n       for (k = 0; k < vl[i].size(); k++) { n         if (j + w[vl[i][k]] > W) continue; n         dp[i + 1][j + w[vl[i][k]]] = n             max(dp[i + 1][j + w[vl[i][k]]], dp[i][j] + b[vl[i][k]]); n       } n       if (j + v[i].second > W) continue; n       dp[i + 1][j + v[i].second] = n           max(dp[i + 1][j + v[i].second], dp[i][j] + v[i].first); n     } n   } n   for (i = 0; i <= V; i++) n     for (j = 0; j <= W; j++) ans = max(ans, dp[i][j]); n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n mt19937 rng(chrono::high_resolution_clock::now().time_since_epoch().count()); n long long power(long long b, long long e, long long m) { n   if (e == 0) return 1; n   if (e & 1) return b * power(b * b % m, e / 2, m) % m; n   return power(b * b % m, e / 2, m); n } n long long power(long long b, long long e) { n   if (e == 0) return 1; n   if (e & 1) return b * power(b * b, e / 2); n   return power(b * b, e / 2); n } n template <typename T, typename U> n static inline void amin(T &x, U y) { n   if (y < x) x = y; n } n template <typename T, typename U> n static inline void amax(T &x, U y) { n   if (x < y) x = y; n } n int n; n const int N = 300005; n vector<int> v[N]; n bool vis[N]; n int val[N]; n int dp[N][2], ans = 0; n void dfs(int s) { n   vis[s] = true; n   for (auto j : v[s]) { n     if (!vis[j]) { n       dfs(j); n       dp[s][0] += dp[j][0]; n       dp[s][1] += dp[j][1]; n     } n   } n   if (val[s]) ++dp[s][val[s] - 1]; n } n void dfs2(int s = 1) { n   vis[s] = true; n   for (auto j : v[s]) { n     if (!vis[j]) { n       int k = (dp[j][0] > 0); n       if ((k > 0 && dp[j][1] == 0) || (k == 0 && dp[j][1] >= 0)) { n         int z = ((dp[1][0] - dp[j][0]) > 0); n         if ((z > 0 && dp[1][1] - dp[j][1] == 0) || n             (z == 0 && dp[1][1] - dp[j][1] >= 0)) n           ++ans; n       } n       dfs2(j); n     } n   } n } n int _runtimeTerror_() { n   int i; n   cin >> n; n   for (i = 1; i <= n; ++i) cin >> val[i]; n   for (i = 0; i < n - 1; ++i) { n     int x, y; n     cin >> x >> y; n     v[x].push_back(y); n     v[y].push_back(x); n   } n   dfs(1); n   memset(vis, false, sizeof(vis)); n   dfs2(); n   cout << ans <<   n ; n   return 0; n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   int TESTS = 1; n   while (TESTS--) _runtimeTerror_(); n   cerr <<   n  << 1.0 * clock() / CLOCKS_PER_SEC; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   cin >> n; n   vector<int> arr(n); n   for (int i = 0; i < n; ++i) cin >> arr[i]; n   sort(arr.begin(), arr.end()); n   int i = 0, j = n - 1; n   vector<int> left; n   vector<int> right; n   while (i <= j) { n     right.push_back(arr[j--]); n     left.push_back(arr[i++]); n   } n   if ((arr.size() & 1) == 1) left.erase(left.begin() + left.size() - 1); n   int k = right.size() - 1; n   cout << k <<   n ; n   int p = 0; n   for (int i = 0; i < right.size(); i++) { n     cout << right[i] <<    ; n     if (p < left.size()) cout << left[p++] <<    ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int p, x, ans = 0; n   bool b; n   cin >> p; n   for (int i = 1; i < p; i++) { n     b = 1; n     x = 1; n     for (int j = 1; j < p - 1; j++) { n       x = (x * i) % p; n       if ((x - 1) % p == 0) b = 0; n     } n     if (((x * i) - 1) % p != 0) b = 0; n     if (b) ans++; n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long n, k, i, j, A[200005], s = 1; n   cin >> n; n   for (long long(i) = 0; (i) < (n); (i) = (i + 1)) { n     cin >> A[i]; n     if (A[i] == n) { n       k = i; n     } n   } n   for (j = k; j < n - 1; j++) { n     if (A[j] < A[j + 1]) { n       s = 0; n       break; n     } n   } n   if (s != 0) { n     for (j = k; j > 0; j--) { n       if (A[j] < A[j - 1]) { n         s = 0; n         break; n       } n     } n     s == 0 ? cout <<  NO  : cout <<  YES ; n   } else { n     cout <<  NO ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n; n const int maxN = 2005; n int a[maxN][maxN]; n int b[maxN][maxN]; n vector<pair<int, int>> by[maxN]; n const int dx[3] = {-1, 0, 0}; n const int dy[3] = {0, -1, 1}; n int his_val[maxN]; n signed main() { n   ios_base::sync_with_stdio(false); n   cin.tie(nullptr); n   cin >> n; n   if (n % 2 == 1) { n     cout <<  NONE ; n     return 0; n   } n   for (int j = 0; j < n; j++) { n     a[0][j] = j / 2; n     b[0][j] = 0; n   } n   for (int i = 0; i + 1 < n; i++) { n     for (int j = 0; j < n; j++) { n       int xr = 0; n       int c = 0; n       vector<int> vals; n       vals.emplace_back(a[i][j]); n       for (int z = 0; z < 3; z++) { n         int ni = i + dx[z]; n         int nj = j + dy[z]; n         if (ni < 0 || ni >= n) continue; n         if (nj < 0 || nj >= n) continue; n         vals.emplace_back(a[ni][nj]); n       } n       int cnt = 0; n       int he = -1; n       sort(vals.begin(), vals.end()); n       vals.erase(unique(vals.begin(), vals.end()), vals.end()); n       for (int& t : vals) { n         int pp = 0; n         for (int z = 0; z < 3; z++) { n           int ni = i + dx[z]; n           int nj = j + dy[z]; n           if (ni < 0 || ni >= n) continue; n           if (nj < 0 || nj >= n) continue; n           if (a[ni][nj] == t) pp ^= 1; n           if (a[i][j] == t) pp ^= 1; n         } n         if (pp && t != a[i][j]) { n           cnt++; n           he = t; n         } n       } n       assert(cnt == 0 || cnt == 1); n       if (cnt == 0) n         a[i + 1][j] = a[i][j]; n       else n         a[i + 1][j] = he; n       int pp = 0; n       for (int z = 0; z < 3; z++) { n         int ni = i + dx[z]; n         int nj = j + dy[z]; n         if (ni < 0 || ni >= n) continue; n         if (nj < 0 || nj >= n) continue; n         pp ^= b[ni][nj]; n         pp ^= b[i][j]; n         pp ^= 1; n       } n       b[i + 1][j] = (pp ^ 1) ^ b[i][j]; n     } n   } n   memset(his_val, -1, sizeof his_val); n   bool ok = true; n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < n; j++) { n       char c; n       cin >> c; n       if (c ==  . ) continue; n       int d = 0; n       if (c ==  S ) n         d = 0; n       else n         d = 1; n       d ^= b[i][j]; n       if (his_val[a[i][j]] == -1) his_val[a[i][j]] = d; n       if (his_val[a[i][j]] != d) ok = false; n     } n   } n   if (!ok) { n     cout <<  NONE n ; n     return 0; n   } n   bool hs = false; n   for (int z = 0; z < n / 2; z++) { n     if (his_val[z] == -1) hs = true; n   } n   if (hs) { n     cout <<  MULTIPLE n ; n     return 0; n   } n   cout <<  UNIQUE n ; n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < n; j++) { n       int d = his_val[a[i][j]] ^ b[i][j]; n       if (d == 0) n         cout <<  S ; n       else n         cout <<  G ; n     } n     cout <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n constexpr int N = 1e6 + 10; n constexpr int M = 30; n long long a1 = 2, a2 = 4, n, ans = 2, t = 4, dis[N], dp[N][M], v, dist1, dist2; n int get_lca(int a, int b) { n   if (dis[a] < dis[b]) swap(a, b); n   while (dis[a] != dis[b]) a = dp[a][(int)log2(dis[a] - dis[b])]; n   for (int i = M - 1; i >= 0; i--) { n     if (a == b) n       return a; n     else if (dp[a][i] != dp[b][i]) { n       a = dp[a][i]; n       b = dp[b][i]; n       return get_lca(a, b); n     } n   } n   return dp[a][0]; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   cin >> n; n   dp[2][0] = 1; n   dp[3][0] = 1; n   dp[4][0] = 1; n   dis[2] = dis[3] = dis[4] = 1; n   for (int i = 0; i < n; i++) { n     cin >> v; n     dp[++t][0] = v; n     dp[++t][0] = v; n     dis[t - 1] = dis[v] + 1; n     dis[t] = dis[v] + 1; n     for (int j = 1; j < M; j++) { n       dp[t - 1][j] = dp[dp[t - 1][j - 1]][j - 1]; n       dp[t][j] = dp[dp[t][j - 1]][j - 1]; n     } n     dist1 = dis[t] + dis[a1] - 2 * dis[get_lca(t, a1)]; n     dist2 = dis[t] + dis[a2] - 2 * dis[get_lca(t, a2)]; n     if (dist1 >= dist2 && dist1 > ans) { n       ans = dist1; n       a2 = t; n     } else if (dist2 > dist1 && dist2 > ans) { n       ans = dist2; n       a1 = t; n     } n     cout << ans <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   vector<long long> v, z; n   long long n, i, j, k; n   cin >> n; n   while (n != 0) { n     v.push_back(n % 10); n     n = n / 10; n   } n   long long p = 1; n   for (i = 0; i < v.size(); i++) { n     p = p * v[i]; n   } n   z.push_back(p); n   for (i = 0; i < v.size() - 1; i++) { n     if (v[i] != 9) { n       for (j = i + 1; j < v.size(); j++) { n         if (v[j] > 0) { n           v[j]--; n           break; n         } n       } n       if (j != v.size()) { n         for (k = i; k < j; k++) { n           v[k] = 9; n         } n       } n       p = 1; n       for (k = 0; k < v.size() - 1; k++) { n         p = p * v[k]; n       } n       if (v[v.size() - 1] != 0) p = p * v[v.size() - 1]; n       z.push_back(p); n     } n   } n   long long max = 0; n   for (i = 0; i < z.size(); i++) { n     if (z[i] > max) max = z[i]; n   } n   cout << max; n } n


#include <bits/stdc++.h> n using namespace std; n vector<int> road_wt[2 * 100000 + 5]; n vector<int> rail_wt[2 * 100000 + 5]; n vector<int> road[2 * 100000 + 5]; n vector<int> rail[2 * 100000 + 5]; n int visited[2 * 100000 + 5]; n struct r { n   int city; n   signed long long int cost; n   int type; n   r() {} n   r(int c, signed long long int co, int t) { n     city = c; n     cost = co; n     type = t; n   } n }; n signed long long int cost[100000 + 5]; n bool way(r a, r b) { return a.cost > b.cost; } n struct OrderBy { n   bool operator()(struct r a, struct r b) { n     return a.cost > b.cost or a.cost == b.cost and a.type < b.type; n   } n }; n priority_queue<struct r, std::vector<struct r>, OrderBy> q; n int main() { n   int n, k, m, a, b, s; n   signed long long int w, y; n   cin >> n >> m >> k; n   for (int i = 1; i <= m; ++i) { n     cin >> a >> b >> w; n     road[a].push_back(b); n     road[b].push_back(a); n     road_wt[a].push_back(w); n     road_wt[b].push_back(w); n   } n   for (int i = 1; i <= k; ++i) { n     cin >> s >> y; n     rail[1].push_back(s); n     rail_wt[1].push_back(y); n   } n   q.push(r(1, 0, 0)); n   int ans = 0; n   while (!q.empty()) { n     r temp = q.top(); n     int v = temp.city; n     q.pop(); n     if (visited[v]) continue; n     visited[v] = 1; n     cost[v] = temp.cost; n     if (temp.type == 1) ++ans; n     for (int i = 0; i < road[v].size(); ++i) { n       int u = road[v][i]; n       if (visited[u]) continue; n       q.push(r(u, temp.cost + road_wt[v][i], 2)); n     } n     for (int i = 0; i < rail[v].size(); ++i) { n       int u = rail[v][i]; n       if (visited[u]) continue; n       q.push(r(u, temp.cost + rail_wt[v][i], 1)); n     } n   } n   cout << k - ans; 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 qtn; n   cin >> qtn; n   vector<long long int> resp(qtn + 1); n   resp[qtn] = 1000000000000000010; n   for (int i = 0; i < qtn >> 1; i++) { n     long long int n; n     cin >> n; n     resp[qtn - i - 1] = min(resp[qtn - i], n); n     resp[i] = n - resp[qtn - i - 1]; n     if (i) { n       if (resp[i] < resp[i - 1]) { n         resp[i] = resp[i - 1]; n         resp[qtn - i - 1] = n - resp[i]; n       } n     } n   } n   for (int i = 0; i < qtn; i++) { n     cout << resp[i] <<    ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int a[26], l[26], n, k, x, se, t; n char b; n string s; n int main() { n   cin >> n; n   for (int i = 0; i < n; i++) { n     cin >> b >> s; n     if (x == 1) { n       if (i == n - 1) break; n       if (b !=  . ) se++; n       continue; n     } n     if (b !=  ? ) n       for (int j = 0; j < s.size(); j++) l[s[j] -  a ]++; n     if (b ==  ! ) { n       for (int j = 0; j < s.size(); j++) { n         a[s[j] -  a ]++; n       } n     } n     if (b ==  . ) { n       for (int j = 0; j < s.size(); j++) { n         a[s[j] -  a ]--; n       } n     } n     if (b ==  ?  && i != n - 1) a[s[0] -  a ]--; n     if (b !=  ? ) n       for (int j = 0; j < 26; j++) { n         if (l[j] > 1) a[j] = a[j] - l[j] + 1; n         l[j] = 0; n       } n     k = 0; n     for (int j = 0; j < 26; j++) k = max(a[j], k); n     for (int j = 0; j < 26; j++) n       if (a[j] == k) t++; n     if (t == 1) x = 1; n     t = 0; n   } n   cout << se; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int xs[8] = {1, 1, 1, 0, 0, -1, -1, -1}; n int ys[8] = {-1, 0, 1, -1, 1, -1, 0, 1}; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   int x1, y1, x2, y2; n   cin >> x1 >> y1; n   cin >> x2 >> y2; n   int nx, ny; n   int mV = INT_MAX; n   for (int i = 0; i < 8; i++) { n     nx = xs[i] + x2; n     ny = ys[i] + y2; n     mV = min(abs(nx - x1) + abs(ny - y1), mV); n   } n   cout << 2 * mV + 8 <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long m, h1, a1, x1, y1, h2, a2, x2, y2; n   long long p1, p2, q1, q2; n   while (cin >> m >> h1 >> a1 >> x1 >> y1 >> h2 >> a2 >> x2 >> y2) { n     p1 = p2 = q1 = q2 = 0; n     for (long long i = 1; i <= 2 * m; i++) { n       h1 = (h1 * x1 + y1) % m; n       if (h1 == a1) { n         if (p1 == 0) n           p1 = i; n         else if (q1 == 0) { n           q1 = i - p1; n           break; n         } n       } n     } n     for (long long i = 1; i <= 2 * m; i++) { n       h2 = (h2 * x2 + y2) % m; n       if (h2 == a2) { n         if (p2 == 0) n           p2 = i; n         else if (q2 == 0) { n           q2 = i - p2; n           break; n         } n       } n     } n     if (p1 == 0 || p2 == 0) { n       puts( -1 ); n       continue; n     } n     long long ans1 = p1, ans2 = p2; n     long long i; n     for (i = 1; i <= 2 * m; i++) { n       if (ans1 == ans2) { n         printf( %I64d n , ans1); n         break; n       } n       if (ans1 > ans2) { n         ans2 += q2; n       } n       if (ans1 < ans2) { n         ans1 += q1; n       } n     } n     if (i > 2 * m) { n       puts( -1 ); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main(void) { n   int n; n   cin >> n; n   if (n > 2) n     cout << n - 2 << endl; n   else if (n <= 2) n     cout << n + 2 << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int t, i, flag; n   cin >> t; n   while (t--) { n     string s1, s2; n     cin >> s1 >> s2; n     flag = 0; n     for (i = 0; i < s1.length(); i++) { n       if (find(s2.begin(), s2.end(), s1[i]) == s2.end()) flag++; n     } n     if (flag == s1.length()) n       cout <<  NO  << endl; n     else n       cout <<  YES  << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n string s; n int a[30]; n int main() { n   int n; n   cin >> n; n   cin >> s; n   for (int i = 0; i < n; i++) { n     a[s[i] -  a ]++; n   } n   for (int i = 0; i < 26; i++) { n     int temp = i; n     for (int j = 0; j < a[temp]; j++) { n       cout << char( a  + temp); n     } n   } n   cout << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MOD = 1e9 + 7; n const int MAXN = 1e3 + 5; n const int INF = 0x3f3f3f3f; n const double eps = 1e-9; n short int mp[MAXN][MAXN]; n int d[MAXN][2]; n bool vis[MAXN]; n int n, m, s, t, u, v; n void dijkstra(int s, int idx) { n   memset(vis, false, sizeof(vis)); n   for (int i = 1; i <= n; i++) d[i][idx] = INF; n   d[s][idx] = 0; n   for (int i = 0; i < n; i++) { n     int x = -1, m = INF; n     for (int j = 1; j <= n; j++) n       if (!vis[j] && d[j][idx] < m) m = d[x = j][idx]; n     if (x != -1) { n       vis[x] = true; n       for (int j = 1; j <= n; j++) { n         if (mp[x][j] == 1 && d[j][idx] > d[x][idx] + 1) { n           d[j][idx] = d[x][idx] + 1; n         } n       } n     } n   } n } n int main() { n   scanf( %d %d %d %d , &n, &m, &s, &t); n   for (int i = 0; i < m; i++) { n     scanf( %d %d , &u, &v); n     mp[u][v] = mp[v][u] = 1; n   } n   dijkstra(s, 0); n   dijkstra(t, 1); n   int ans = 0; n   for (int i = 1; i <= n; i++) { n     for (int j = i + 1; j <= n; j++) { n       if (mp[i][j] == 0 && d[i][0] + d[j][1] + 1 >= d[t][0] && n           d[i][1] + d[j][0] + 1 >= d[t][0]) n         ans++; n     } n   } n   printf( %d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, m, a[101] = {0}, s = 0; n   cin >> n; n   for (int i = 0; i < n; i++) { n     cin >> m; n     a[m]++; n     s = max(a[m], s); n   } n   cout << s; n } n


#include <bits/stdc++.h> n using namespace std; n const long long MOD = 1e9 + 7; n void solve() { n   int n, ans = 0; n   cin >> n; n   vector<int> a(n); n   for (int i = 0; i < n; i++) { n     cin >> a[i]; n     ans = max(ans, a[i] - i - 1); n   } n   cout << ans <<   n ; n } n int main() { 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 #pragma GCC optimize( -O3 ) n struct vertex { n   bool vis = false; n   vector<pair<long long int, long long int> > adj; n   long long int low = 1000000007; n   long long int dp = 0; n   long long int parent = -1; n }; n long long int tim = 0, n, m; n vector<pair<long long int, long long int> > edges; n bool flag = false; n void dfs(vertex x[], long long int sor) { n   x[sor].vis = true; n   tim++; n   x[sor].dp = tim; n   x[sor].low = tim; n   for (auto y : x[sor].adj) { n     if (!x[y.first].vis) { n       x[y.first].parent = sor; n       dfs(x, y.first); n       x[sor].low = min(x[sor].low, x[y.first].low); n       if (x[y.first].low > x[sor].dp) n         flag = true; n       else n         edges[y.second] = {sor, y.first}; n     } else if (y.first != x[sor].parent) { n       x[sor].low = min(x[sor].low, x[y.first].dp); n       if (x[y.first].dp < x[sor].dp) { n         edges[y.second] = {sor, y.first}; n       } n     } n   } n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   cin >> n >> m; n   vertex x[n]; n   for (long long int a = 0; a < m; a++) { n     long long int u, v; n     cin >> u >> v; n     u--, v--; n     x[u].adj.push_back({v, a}); n     x[v].adj.push_back({u, a}); n     edges.push_back({u, v}); n   } n   dfs(x, 0); n   if (flag) n     cout << 0 << endl; n   else n     for (auto y : edges) cout << y.first + 1 <<     << y.second + 1 << endl; n   return 0; n } n


#include <bits/stdc++.h> n int mod = 1000000000 + 7; n long long int inf = 1000000000000000000; n using namespace std; n mt19937 unlucko(chrono::steady_clock::now().time_since_epoch().count()); n vector<int> tree[200000], way(200000), dp(200000), ans(200000); n void dfs(int node, int pnode) { n   dp[node] = way[node]; n   for (auto it : tree[node]) { n     if (it == pnode) continue; n     dfs(it, node); n     dp[node] += max(dp[it], 0); n   } n } n void jfs(int node, int pnode, int curr) { n   ans[node] = dp[node] + curr; n   for (auto it : tree[node]) { n     if (it == pnode) continue; n     jfs(it, node, max(ans[node] - max(0, dp[it]), 0)); n   } n } n int main() { n   iostream::sync_with_stdio(false); n   cin.tie(0); n   int n; n   cin >> n; n   for (int i = 0; i < n; i++) { n     cin >> way[i]; n     if (!way[i]) way[i]--; n   } n   for (int i = 0; i < n - 1; i++) { n     int x, y; n     cin >> x >> y; n     --x; n     y--; n     tree[x].push_back(y); n     tree[y].push_back(x); n   } n   dfs(0, 0); n   jfs(0, 0, 0); n   for (int i = 0; i < n; i++) cout << ans[i] <<    ; n } n


#include <bits/stdc++.h> n constexpr int maxn = 2e5 + 5; n constexpr int inf = 1e9; n constexpr int mod = 1e9 + 7; n using namespace std; n using lint = long long; n using pii = pair<int, int>; n int t, n, a[maxn], b[maxn]; n lint ans; n map<int, int> ma, mb; n signed main() { n   ios_base::sync_with_stdio(false); n   cin.tie(nullptr); n   cin >> t; n   while (t--) { n     cin >> n; n     for (int i = 0; i < n; i++) { n       cin >> a[i] >> b[i]; n       ma[a[i]]++; n       mb[b[i]]++; n     } n     ans = 1ll * n * (n - 1) * (n - 2) / 6; n     for (int i = 0; i < n; i++) ans -= 1ll * (ma[a[i]] - 1) * (mb[b[i]] - 1); n     cout << ans <<   n ; n     ma.clear(); n     mb.clear(); n   } n } n


#include <bits/stdc++.h> n using namespace std; n vector<long long> ad[200009]; n long long vis[200009]; n long long cnt, ans; n void dfs(int u) { n   vis[u] = 1; n   cnt++; n   for (auto x : ad[u]) { n     if (!vis[x]) dfs(x); n   } n } n int main() { n   long long i, j, k, l, m, n, u, v; n   cin >> n >> k; n   for (i = 0; i < k; i++) { n     cin >> u >> v; n     ad[u].push_back(v); n     ad[v].push_back(u); n   } n   for (i = 1; i <= n; i++) { n     if (ad[i].size() == 0) continue; n     if (!vis[i]) { n       cnt = 0; n       dfs(i); n       cnt--; n       ans += cnt; n     } n   } n   cout << k - ans <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n const long long N = 3e5; n const long long P = 1e9 + 7; n long long n; n long long col[N], f[N], g[N]; n vector<long long> a[N]; n long long read(void) { n   long long s = 0, w = 0; n   char c = getchar(); n   while (!isdigit(c)) w |= c ==  - , c = getchar(); n   while (isdigit(c)) s = s * 10 + c - 48, c = getchar(); n   return w ? -s : s; n } n void Dfs(long long x, long long fa) { n   col[x] ? f[x] = 1 : g[x] = 1; n   for (long long i = 0; i < a[x].size(); ++i) { n     long long y = a[x][i]; n     if (y == fa) continue; n     Dfs(y, x); n     f[x] = (f[x] * (f[y] + g[y]) + g[x] * f[y]) % P; n     g[x] = (g[x] * (f[y] + g[y])) % P; n   } n   return; n } n signed main(void) { n   n = read(); n   for (long long i = 1; i < n; ++i) a[read()].push_back(i); n   for (long long i = 0; i < n; ++i) col[i] = read(); n   Dfs(0, 0); n   cout << f[0] << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int q; n   cin >> q; n   while (q--) { n     int n; n     cin >> n; n     int a[n], i, j, k, l, m; n     string s, temp; n     for (i = 0; i < n; i++) cin >> a[i]; n     char c =  a , d; n     for (i = 0; i <= 50; i++) { n       s +=  a ; n     } n     cout << s << endl; n     for (i = 0; i < n; i++) { n       k = a[i]; n       temp =   0 ; n       for (j = 0; j < k; j++) { n         temp += s[j]; n         c = s[j]; n       } n       for (j = k; j <= 50; j++) { n         if (s[j] !=  z ) { n           d = s[j] + 1; n         } else { n           d =  a ; n         } n         temp += d; n       } n       s = temp; n       cout << temp << endl; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   int k1, k2, k3; n   cin >> k1 >> k2 >> k3; n   int n = k1 + k2 + k3; n   int maps[n]; n   for (int i = 0; i < n; i++) { n     int inp; n     cin >> inp; n     inp--; n     if (i < k1) n       maps[inp] = 0; n     else if (i < k1 + k2) n       maps[inp] = 1; n     else n       maps[inp] = 2; n   } n   int costsk1[n]; n   int costsk2[n]; n   int costsk3[n]; n   int profits[n]; n   int maxprofits[n]; n   for (int i = 0; i < n; i++) { n     costsk1[i] = (i == 0 ? 0 : costsk1[i - 1]) + (maps[i] == 0 ? 0 : 1); n     costsk2[i] = (i == 0 ? 0 : costsk2[i - 1]) + (maps[i] == 1 ? 0 : 1); n     costsk3[i] = (i == 0 ? 0 : costsk3[i - 1]) + (maps[i] == 2 ? 0 : 1); n     profits[i] = costsk2[i] - costsk1[i]; n     maxprofits[i] = max((i == 0 ? 0 : maxprofits[i - 1]), profits[i]); n   } n   int minn = costsk3[n - 1]; n   for (int i = 0; i < n; i++) { n     minn = min(minn, costsk2[i] - maxprofits[i] + costsk3[n - 1] - costsk3[i]); n   } n   cout << minn << endl; n } n


#include <bits/stdc++.h> n using namespace std; n void yala_bena() { n   cin.tie(0); n   std::ios::sync_with_stdio(0); n } n const int m_r = 999999; n int k = -1; n int bestsum, limit; n vector<long long> ans; n int binary(int number) { n   while (number) { n     k++; n     if (number % 2) return k; n     number /= 2; n   } n } n void sum_it_up(int sum, int index, vector<int> p) { n   if (sum + ans[index] <= bestsum) { n     sum += ans[index]; n     p.push_back(index + 1); n   } n   if (sum == bestsum) { n     cout << p.size() << endl; n     for (int i = 0; i < p.size(); i++) { n       if (i == p.size() - 1) n         cout << p[i] << endl; n       else n         cout << p[i] <<    ; n     } n     exit(0); n   } n   for (int i = index + 1; i < ans.size(); i++) { n     if (sum + ans[i] <= bestsum) sum_it_up(sum, i, p); n   } n } n int main() { n   yala_bena(); n   cin >> bestsum >> limit; n   for (int i = limit; i >= 1; i--) { n     int a = (int)pow(2, binary(i)); n     k = -1; n     if (a <= bestsum) bestsum -= a, ans.push_back(i); n   } n   if (bestsum == 0) { n     cout << ans.size() << endl; n     for (int i = 0; i < ans.size(); i++) n       i == ans.size() - 1 ? cout << ans[i] << endl : cout << ans[i] <<    ; n     return 0; n   } n   cout << -1 << endl; n } n


#include <bits/stdc++.h> n using namespace std; n const int INF = (1u << 31) - 1; n const int MAXN = 100086; n int mice[MAXN], arr[MAXN], chese[MAXN]; n int main() { n   for (int n, m; ~scanf( %d%d%*d%*d , &n, &m);) { n     for (int i = 0; i < n; i++) { n       scanf( %d , &mice[i]); n     } n     for (int i = 0; i < m; i++) { n       scanf( %d , &chese[i]); n     } n     fill(arr, arr + m, INF); n     int ans = 0; n     for (int i = 0; i < n; i++) { n       int left, right = lower_bound(chese, chese + m, mice[i]) - chese; n       if (right == m) { n         left = --right; n       } else if (right == 0 || mice[i] == chese[right]) { n         left = right; n       } else { n         left = right - 1; n         if (abs(mice[i] - chese[left]) < abs(mice[i] - chese[right])) { n           right = left; n         } else if (abs(mice[i] - chese[left]) > abs(mice[i] - chese[right])) { n           left = right; n         } n       } n       int dist = abs(mice[i] - chese[left]); n       if (arr[left] == INF) { n         ans++; n         arr[left] = dist; n       } else if (arr[left] == dist) { n         ans++; n       } else if (arr[left] > dist && left == right) { n         arr[left] = dist; n       } else if (left != right) { n         dist = abs(mice[i] - chese[right]); n         arr[right] = dist; n         ans++; n       } n     } n     printf( %d n , n - ans); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long int a[101][101], f[101], ans[101]; n int main() { n   int n, m; n   cin >> n >> m; n   memset(a, 0, sizeof(a)); n   memset(f, 0, sizeof(f)); n   memset(ans, 0, sizeof(ans)); n   for (int i = 1; i <= m; i++) { n     for (int j = 1; j <= n; j++) { n       cin >> a[i][j]; n     } n   } n   for (int i = 1; i <= m; i++) { n     int t, t1, mv = -1; n     for (int j = 1; j <= n; j++) { n       if (a[i][j] > mv) { n         mv = a[i][j]; n         t = i; n         t1 = j; n       } n     } n     f[t] = t1; n   } n   for (int i = 1; i <= n; i++) { n     for (int j = 1; j <= m; j++) { n       if (i == f[j]) ans[i]++; n     } n   } n   int max(0), out(1); n   ; n   for (int i = 1; i <= n; i++) { n     if (ans[i] > max) { n       max = ans[i]; n       out = i; n     } n   } n   cout << out; n } n


#include <bits/stdc++.h> n using namespace std; n const int mod = 7 + (int)1e9; n long long process(string s) { n   for (int i = 0; i < s.size(); i++) s[i] = (s[i] -  0 ) % 2 +  0 ; n   return stoll(s); n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   int n; n   char c; n   map<long long, int> m; n   cin >> n; n   while (n--) { n     string a; n     cin >> c >> a; n     if (c ==  + ) { n       m[process(a)]++; n     } else if (c ==  - ) { n       m[process(a)]--; n     } else if (c ==  ? ) { n       cout << m[stoll(a)] << endl; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<pair<int, int> > v; n int n, a, b; n int max(int a, int b) { n   if (a > b) return a; n   return b; n } n bool checkNorm(pair<int, int> f, pair<int, int> s) { n   int x1, y1; n   x1 = max(f.first, s.first), y1 = f.second + s.second; n   if (x1 <= a && y1 <= b) return 1; n   y1 = max(f.second, s.second), x1 = f.first + s.first; n   if (x1 <= a && y1 <= b) return 1; n   return 0; n } n pair<int, int> rotate(pair<int, int> p) { n   swap(p.first, p.second); n   return p; n } n int main() { n   ios_base ::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL); n   cin >> n >> a >> b; n   for (int i = 0; i < n; i++) { n     int x, y; n     cin >> x >> y; n     v.push_back(make_pair(x, y)); n   } n   int mx = 0; n   for (int i = 1; i < n; i++) { n     for (int j = 0; j < i; j++) { n       if (checkNorm(v[i], v[j])) { n         mx = max(mx, v[i].first * v[i].second + v[j].first * v[j].second); n       } n       if (checkNorm(v[i], rotate(v[j]))) { n         mx = max(mx, v[i].first * v[i].second + v[j].first * v[j].second); n       } n       if (checkNorm(rotate(v[i]), (v[j]))) { n         mx = max(mx, v[i].first * v[i].second + v[j].first * v[j].second); n       } n       if (checkNorm(rotate(v[i]), rotate(v[j]))) { n         mx = max(mx, v[i].first * v[i].second + v[j].first * v[j].second); n       } n     } n   } n   cout << mx; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   cin >> n; n   int cnt[100005]; n   int cmax[10]; n   memset(cnt, 0, sizeof(cnt)); n   memset(cmax, 0, sizeof(cmax)); n   for (int i = 1; i <= n; i++) { n     int a; n     cin >> a; n     cnt[a]++; n     if (cnt[a] < 10) cmax[cnt[a]]++; n   } n   int t; n   cin >> t; n   while (t--) { n     char ch; n     int x; n     cin >> ch >> x; n     if (ch ==  + ) { n       cnt[x]++; n       if (cnt[x] < 10) cmax[cnt[x]]++; n     } else { n       if (cnt[x] < 10) cmax[cnt[x]]--; n       cnt[x]--; n     } n     if (cmax[8]) n       cout <<  YES  << endl; n     else if (cmax[4] >= 2) n       cout <<  YES  << endl; n     else if (cmax[4]) { n       if (cmax[2] >= 3) n         cout <<  YES  << endl; n       else if (cmax[2] >= 2 && cmax[6]) n         cout <<  YES  << endl; n       else n         cout <<  NO  << endl; n     } else n       cout <<  NO  << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n list<char> lanc; n using It = list<char>::iterator; n bool torolni_kell(It mit) { n   if (*mit ==  - ) return false; n   char akt = *mit, el = *prev(mit), kov = *next(mit); n   return (el !=  -  && el != akt) || (kov !=  -  && kov != akt); n } n int main() { n   ios_base::sync_with_stdio(false); n   string be; n   cin >> be; n   be =  -  + be +  - ; n   lanc.insert(begin(lanc), begin(be), end(be)); n   int valasz = 0; n   vector<It> to_check; n   for (It it = next(lanc.begin()); *it !=  - ; ++it) to_check.push_back(it); n   while (1) { n     vector<It> torlendok; n     for (It akt : to_check) n       if (torolni_kell(akt)) torlendok.push_back(akt); n     to_check = {}; n     for (int i = 0; i < torlendok.size(); i++) { n       if ((i == 0 || prev(torlendok[i]) != torlendok[i - 1]) && n           (to_check.empty() || to_check.back() != prev(torlendok[i]))) n         to_check.push_back(prev(torlendok[i])); n       if ((i + 1 == torlendok.size() || n            next(torlendok[i]) != torlendok[i + 1]) && n           (to_check.empty() || to_check.back() != next(torlendok[i]))) n         to_check.push_back(next(torlendok[i])); n     } n     if (torlendok.empty()) break; n     for (auto akt : torlendok) lanc.erase(akt); n     valasz++; n   } n   cout << valasz; n } n


#include <bits/stdc++.h> n using namespace std; n long long ten[20], a, b, l, ans = (long long)1 << 40; n long long lp, rp, mp, mt; n int ls, rs, ms; n int calc(long long x) { n   int y = 0; n   for (; x; x /= 10) n     if (x % 10 == 4 || x % 10 == 7) ++y; n   return y; n } n void askl() { n   lp = a; n   ls = -1; n   int j = -1; n   for (int i = 0; ten[i] <= a; ++i) { n     int y = a % ten[i + 1]; n     if ((y / ten[i] == 4 || y / ten[i] == 7) && a + ten[i] - y % ten[i] <= b) n       j = i, lp = a + ten[i] - y % ten[i], ls = 0; n   } n   for (int i = j - 1; i >= 0; --i) n     if (a % ten[i + 1] / ten[i] < 9) { n       ls = a % ten[i + 1] / ten[i] < 8 ? i + 1 : i; n       break; n       ; n     } n   if (ls >= 0) n     for (; ten[ls] < lp - a; ++ls) n       ; n } n void askr() { n   rp = a + l; n   rs = -1; n   int j = -1; n   for (int i = 0; ten[i] <= b; ++i) { n     int y = b % ten[i + 1]; n     if ((y / ten[i] == 4 || y / ten[i] == 7) && b - y % ten[i] > a) n       j = i, rp = b - y % ten[i], rs = 0; n   } n   for (int i = j - 1; i >= 0; --i) n     if (b % ten[i + 1] / ten[i] > 0) { n       rs = b % ten[i + 1] / ten[i] > 3 ? i + 1 : i; n       break; n     } n   if (rs >= 0) n     for (; ten[rs] < b + 1 - rp; ++rs) n       ; n } n int checkl(long long x) { n   if (!x) return 99; n   int y = 0; n   for (; x % 10 == 0; x /= 10) ++y; n   return x % 10 == 4 || x % 10 == 7 ? y : -1; n } n int checkr(long long x) { n   if (!x) return 99; n   int y = 0; n   for (; x % 10 == 0; x /= 10) ++y; n   return x % 10 == 5 || x % 10 == 8 ? y : -1; n } n void askm() { n   mp = lp + 1; n   ms = -1; n   for (int i = lp + 1, j = 0; i < rp; i += ten[j]) { n     int k = checkl(i); n     if (k >= j) { n       if (k > j || ms < 0) { n         mp = i; n         ms = k; n         mt = 0; n       } else if (!mt) n         mt = i - mp; n       j = k; n     } n   } n } n long long next(long long x, int y, int z) { n   for (int i = 0;; ++i) { n     int dx = x / ten[i] % 10, w = calc(x); n     if (w > y) x += ten[i], w = calc(x); n     if (w + i + 1 == y && dx < 7) { n       ++w; n       if (dx < 4) n         x += (4 - dx) * ten[i]; n       else n         x += (7 - dx) * ten[i]; n     } n     if (w <= y && w + i >= y) { n       for (int j = 0; j < y - w; ++j) x += 4 * ten[j]; n       break; n     } n     x = (x / ten[i + 1] + 1) * ten[i + 1]; n   } n   if (z) { n     int w = x; n     for (; w % 10 == 9; w /= 10) n       ; n     if (w % 10 == 3 || w % 10 == 4 || w % 10 == 6 || w % 10 == 7) n       x = next(x / 10 + 1, y, 0) * 10; n   } n   return x; n } n void init() { n   scanf( %I64d%I64d , &a, &l); n   b = a + l - 1; n   ten[0] = 1; n   for (int i = 1; i <= 12; ++i) ten[i] = ten[i - 1] * 10; n   askl(); n   askr(); n   askm(); n } n void make(int x) { n   for (int i = 0; i <= 9; ++i) { n     long long y = ms < 0 ? i : (i * 10 + x) * ten[ms]; n     long long ml = (y - mp + lp + ten[ms + 2]) % ten[ms + 2]; n     long long mr = (y + rp - mp) % ten[ms + 2]; n     int z = ml > y || mr < y, w = 0; n     if (mp < rp) { n       if (i >= 4 && i <= 7) { n         if (z) continue; n         if (i > 4 && i < 7 && (ml < 5 * ten[ms + 1] || mr > 7 * ten[ms + 1])) n           continue; n         if (i == 4 && (ml < 4 * ten[ms + 1] || mr > 5 * ten[ms + 1])) continue; n         if (i == 7 && (ml < 7 * ten[ms + 1] || mr > 8 * ten[ms + 1])) continue; n       } n       if (i >= 0 && i <= 3) { n         if (ml > y && ml < 8 * ten[ms + 1]) continue; n         if (mr > 4 * ten[ms + 1] || mr < y) continue; n       } n       if (i >= 8 && i <= 9) { n         if (ml > y || ml < 8 * ten[ms + 1]) continue; n         if (mr > 4 * ten[ms + 1] && mr < y) continue; n       } n     } n     if (~ls) { n       int tl = checkr(ml); n       if (tl < ls) continue; n       if (tl == 99) w = -1; n     } n     if (~rs) { n       int tl = checkl(mr); n       if (tl < rs) continue; n       if (tl == 99) w = 1; n     } n     if (w) n       for (int j = ms + 2; ten[j] <= b * 100; ++j) n         for (int k = 0; k < 2; ++k) { n           int flag = 0; n           long long tp; n           if (w < 0 && j >= ls) { n             flag = 1; n             tp = ((k ? 5 : 8) * ten[j] - lp + a + ten[j + 1]) % ten[j + 1]; n           } n           if (w > 0 && j >= rs) { n             flag = 1; n             tp = ((k ? 4 : 7) * ten[j] - rp + a + ten[j + 1]) % ten[j + 1]; n           } n           if (flag) { n             long long ts = a / ten[j + 1] + (a % ten[j + 1] >= tp); n             int tn = calc(a) - calc(tp); n             if (tn >= 0) ans = min(ans, next(ts, tn, 0) * ten[j + 1] + tp); n           } n         } n     else { n       long long tp = (y - mp + a + ten[ms + 2]) % ten[ms + 2]; n       long long ts = a / ten[ms + 2] + (a % ten[ms + 2] >= tp); n       int tn = calc(a) - calc(tp); n       if (tn >= 0) ans = min(ans, next(ts, tn, z) * ten[ms + 2] + tp); n     } n   } n } n void work() { n   if (!mt) n     if (ms < 0) n       make(0); n     else { n       if (mp - lp <= 3 * ten[ms] && rp - mp <= 7 * ten[ms]) make(7); n       if (rp - mp <= 3 * ten[ms] && mp - lp <= 7 * ten[ms]) make(4); n     } n   else n     make(mt / ten[ms] == 3 ? 4 : 7); n   printf( %I64d n , ans); n } n int main() { n   init(); n   work(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   int t; n   cin >> t; n   while (t--) { n     bool f = false; n     set<int> v1; n     int n, ans = -1; n     cin >> n; n     int a[n]; n     for (long long i = 0; i < n; i++) { n       cin >> a[i]; n       v1.insert(a[i]); n     } n     for (int i = 1; i <= 1024; i++) { n       long long count = 0; n       for (int j = 0; j < n; j++) { n         int m = a[j] ^ i; n         v1.insert(a[j] ^ i); n         if (v1.size() > n) { n           count++; n           v1.erase(v1.find(m)); n           break; n         } n       } n       if (count == 0) { n         ans = i; n         break; n       } n     } n     cout << ans << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 2e5 + 10; n int cnt[32][N + 10]; n int main() { n   for (int i = 1; i <= N; i++) { n     int n = i, t = 0; n     while (n > 0) { n       cnt[t][i] = cnt[t][i - 1] + n % 2; n       t++; n       n /= 2; n     } n   } n   int t; n   cin >> t; n   while (t--) { n     int l, r; n     cin >> l >> r; n     int k[32] = {}; n     for (int i = 0; i < 32; i++) { n       k[i] = cnt[i][r] - cnt[i][l - 1]; n     } n     int res = 0; n     for (int i = 0; i < 32; i++) { n       res = max(res, k[i]); n     } n     cout << (r - l + 1) - res <<   n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int n, q; n int sz, nxt[(1 << 20)][2], son[(1 << 20)], val[(1 << 20)]; n bool vis[(1 << 20)]; n void insert(int v, int add) { n   int x = 0; n   for (int i = 18; i >= 0; i--) { n     int c = (v >> i) & 1; n     if (!nxt[x][c]) nxt[x][c] = sz++; n     x = nxt[x][c]; n     son[x] += add; n   } n   val[x] = v; n } n int query(int v) { n   int x = 0; n   for (int i = 18; i >= 0; i--) { n     int c = (v >> i) & 1; n     if (son[nxt[x][c]] == (1 << i)) n       x = nxt[x][c ^ 1]; n     else n       x = nxt[x][c]; n   } n   return val[x]; n } n int main() { n   scanf( %d %d , &n, &q); n   sz++; n   for (int i = 0; i < (1 << 19); i++) insert(i, 0); n   for (int i = 1; i <= n; i++) { n     int x; n     scanf( %d , &x); n     if (vis[x]) continue; n     insert(x, 1); n     vis[x] = 1; n   } n   int cur = 0, x; n   while (q--) { n     scanf( %d , &x); n     cur ^= x; n     printf( %d n , query(cur) ^ cur); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long MOD = 1000000007; n const int MAX = 100005; n bool check(int x, int y) { n   int cnt1[11] = {0}; n   int cnt2[11] = {0}; n   while (x > 0) { n     cnt1[x % 10]++; n     x /= 10; n   } n   while (y > 0) { n     cnt2[y % 10]++; n     y /= 10; n   } n   for (int i = 0; i < 10; i++) { n     if (cnt1[i] > 0 and cnt2[i] > 0) return true; n   } n   return false; n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   int x; n   cin >> x; n   vector<int> v; n   for (int i = 1; i * i <= x; i++) { n     if (x % i == 0) { n       if (i * i == x) n         v.push_back(i); n       else n         v.push_back(i), v.push_back(x / i); n     } n   } n   int ans = 0, sz = v.size(); n   for (int i = 0; i < sz; i++) { n     if (check(x, v[i])) ans++; n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 3e6 + 7; n long long int a[N], b[N], c[N], tree[N], lazy[N], cnt, sum, s2, mx; n vector<long long int> v, u; n vector<pair<long long int, long long int>> vp; n map<long long int, long long int> mp; n bool vis[111111]; n pair<long long int, long long int> p; n set<pair<long long int, long long int>> ss; n queue<long long int> q; n int main() { n   ios_base ::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   long long int n, m = 0, d, i = 0, s1 = 0, s2 = 0, q = 5, x = 0, y, k, j; n   string s; n   cin >> n; n   if (n % 2 == 0) n     m = n / 2 - 1; n   else n     m = n / 2; n   double mx = 0.0; n   for (i = m; i > 0; i--) { n     map<long long int, long long int> mm; n     cnt = 0; n     for (j = 2; j <= i; j++) { n       if (i % j == 0) mm[j]++; n     } n     for (j = 2; j <= (n - i); j++) { n       if ((n - i) % j == 0 && mm[j] != 0) { n         cnt = 1; n         break; n       } n     } n     if (cnt == 0) return cout << i <<     << n - i, 0; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int n; n int A[1000]; n int main() { n   cin >> n; n   for (int i = 0; i < n; i++) cin >> A[i]; n   int G = 0, C = 0; n   for (int i = 0; i < n; i++) { n     if (A[i] == 0) { n       G = min(C, G) + 1; n       C = G; n     } else if (A[i] == 1) { n       int temp = G; n       G = min(G, C) + 1; n       C = min(temp, C + 1); n     } else if (A[i] == 2) { n       int temp = C; n       C = min(G, C) + 1; n       G = min(temp, G + 1); n     } else { n       int temp = G; n       G = min(C, G + 1); n       C = min(temp, C + 1); n     } n   } n   cout << min(G, C) << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   cin >> n; n   vector<int> angles(n); n   for (int i = 0; i < n; i++) { n     int angle; n     cin >> angle; n     angles[i] = angle; n   } n   int difference = 360; n   for (int i = 0; i < n; i++) { n     int current_angle = angles[i]; n     for (int j = 1; j < n; j++) { n       int curr_diff = max(current_angle, 360 - current_angle) - n                       min(current_angle, 360 - current_angle); n       difference = min(difference, curr_diff); n       current_angle += angles[(i + j) % n]; n     } n     int curr_diff = max(current_angle, 360 - current_angle) - n                     min(current_angle, 360 - current_angle); n     difference = min(difference, curr_diff); n   } n   cout << difference << 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   cin.tie(NULL); n   long long t; n   cin >> t; n   while (t--) { n     long long n; n     cin >> n; n     vector<pair<long long, long long> > v(n); n     for (long long i = 0; i < n; i++) { n       cin >> v[i].first >> v[i].second; n     } n     sort(v.begin(), v.end()); n     long long a, b; n     a = v[n - 1].first; n     for (long long i = 0; i < n; i++) swap(v[i].first, v[i].second); n     sort(v.begin(), v.end()); n     b = v[0].first; n     if (a - b <= 0) n       cout << 0 << endl; n     else n       cout << a - b << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 100100; n int P, n, m, s[maxn]; n long long K; n struct que { n   long long x; n   int prey, y; n   bool operator<(const que &a) const { return x < a.x; } n } q[maxn]; n struct Matrix { n   int a[2][2]; n   Matrix() { memset(a, 0, sizeof a); } n   Matrix(int a_, int b_) { n     a[0][0] = a_, a[0][1] = 1; n     a[1][0] = b_, a[1][1] = 0; n   } n   void init(int x) { n     memset(a, 0, sizeof a); n     a[0][0] = a[1][1] = x; n   } n   Matrix operator*(const Matrix &b) { n     Matrix c; n     c.init(0); n     for (int i = 0; i < 2; i++) n       for (int j = 0; j < 2; j++) n         for (int k = 0; k < 2; k++) n           c.a[i][j] = (c.a[i][j] + 1ll * a[i][k] * b.a[k][j] % P) % P; n     return c; n   } n } tr[maxn * 4]; n int le, ri; n void build(int rt, int l, int r) { n   if (l == r) { n     tr[rt] = Matrix(s[l], s[l - 1]); n     return; n   } n   int mid = (l + r) / 2; n   build(rt * 2, l, mid); n   build(rt * 2 + 1, mid + 1, r); n   tr[rt] = tr[rt * 2] * tr[rt * 2 + 1]; n } n Matrix query(int rt, int l, int r) { n   if (le <= l && ri >= r) return tr[rt]; n   int mid = (l + r) / 2; n   if (ri <= mid) return query(rt * 2, l, mid); n   if (le > mid) return query(rt * 2 + 1, mid + 1, r); n   return query(rt * 2, l, mid) * query(rt * 2 + 1, mid + 1, r); n } n Matrix qk_pow(Matrix a, long long b) { n   Matrix p; n   p.init(1); n   for (; b; b /= 2, a = a * a) n     if (b & 1) p = p * a; n   return p; n } n int get(int x) { return x ? x : n; } n Matrix query(long long l, long long r) { n   long long rr = l + n - get((l - 1) % n); n   if (rr > r) { n     le = get((l - 1) % n), ri = get((r - 1) % n); n     return query(1, 1, n); n   } else { n     Matrix c; n     c.init(0); n     le = get((l - 1) % n), ri = n; n     c = query(1, 1, n); n     c = c * qk_pow(tr[1], (r - rr) / n); n     if ((r - rr) % n) le = 1, ri = (r - rr) % n, c = c * query(1, 1, n); n     return c; n   } n } n int main() { n   scanf( %lld%d%d , &K, &P, &n); n   if (K == 0 || K == 1) { n     printf( %lld , K % P); n     return 0; n   } n   for (int i = 0; i < n; i++) scanf( %d , &s[i]); n   s[n] = s[0]; n   build(1, 1, n); n   scanf( %d , &m); n   for (int i = 1; i <= m; i++) scanf( %lld%d , &q[i].x, &q[i].y); n   sort(q + 1, q + m + 1); n   while (q[m].x > K) m--; n   if (q[m].x != K) q[++m].x = K, q[m].y = s[m % n]; n   Matrix ans; n   ans.a[0][0] = 1; n   for (int i = 1; i <= m; i++) n     q[i].prey = q[i - 1].x + 1 == q[i].x ? q[i - 1].y : s[(q[i].x - 1) % n]; n   q[0].x = 1, q[0].y = s[1], q[0].prey = s[0]; n   for (int i = 1; i <= m; i++) { n     ans = ans * Matrix(q[i - 1].y, q[i - 1].prey); n     if (q[i].x == q[i - 1].x + 1) continue; n     ans = ans * Matrix(s[(q[i - 1].x + 1) % n], q[i - 1].y); n     if (q[i].x == q[i - 1].x + 2) continue; n     ans = ans * query(q[i - 1].x + 3, q[i].x); n   } n   printf( %d n , ans.a[0][0]); n   fclose(stdin); n   fclose(stdout); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long n, MOD, f[(long long)4e6 + 5], sum[(long long)4e6 + 5], n     d[(long long)4e6 + 5]; n signed main() { n   cin >> n >> MOD; n   long long Sum = 0; n   for (long long i = 1; i <= n; i++) { n     if (i == 1) n       f[1] = 1; n     else n       f[i] = (Sum + d[i] + sum[i - 1]) % MOD; n     sum[i] = (sum[i - 1] + f[i]) % MOD; n     for (long long j = 1; j * i <= n; j++) { n       d[j * i] = (d[j * i] + f[i]) % MOD; n       if (j * i + j <= n) d[j * i + j] = (d[j * i + j] - f[i] + MOD) % MOD; n     } n     Sum = (Sum + d[i]) % MOD; n   } n   cout << f[n] % MOD; n   return 0; n } n


#include <bits/stdc++.h> n using ll = long long int; n using namespace std; n int main() { n   int n; n   cin >> n; n   string s; n   cin >> s; n   int cnt = 0; n   int negative = -1; n   int ans = 0; n   for (int i = 0; i < n; ++i) { n     if (s[i] ==  ) ) n       --cnt; n     else if (s[i] ==  ( ) n       ++cnt; n     if (cnt < 0 && negative == -1) negative = i; n     if (cnt == 0 && negative != -1) { n       ans += i - negative + 1; n       negative = -1; n     } n   } n   if (cnt != 0) { n     cout << -1 << endl; n   } else { n     cout << ans << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const long long N = 5056, M = 1e9 + 7; n long long n, a[N], pr[N], ans[N * 2], h[N * 2]; n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cin >> n; n   pr[0] = 1; n   for (int i = 1; i <= n - 1; ++i) { n     cin >> a[i]; n     pr[i] = a[i] * pr[i - 1] % M; n     for (int j = 1; j <= i; ++j) ans[j] += pr[i]; n   } n   for (int i = n - 1; i >= 1; --i) { n     for (int j = n + n - 2; j >= 2; --j) n       h[j] = (a[i + 1] * a[i + 1] % M) * h[j - 2] % M; n     long long p = 1; n     for (int j = 2; i + j - 1 <= n; ++j) { n       (h[j] += p * (j > 2 ? 2 : 1)) %= M; n       p = (p * a[i + j - 1]) % M; n     } n     for (int j = 2; j <= 2 * n - 2; ++j) n       ans[j] += a[i] * (a[i] - 1) / 2 % M * pr[i - 1] % M * h[j] % M; n   } n   for (int k = 1; k <= n + n - 2; ++k) n     cout << ans[k] % M <<    n [k == n + n - 2]; n } n


#include <bits/stdc++.h> n using namespace std; n int transition; n int dp[10][2][1 << 9]; n int solve(int idx, int tight, int mask, string &s) { n   if (idx == s.size()) { n     return __builtin_popcount(mask) == s.size(); n   } n   if (dp[idx][tight][mask] != -1) return dp[idx][tight][mask]; n   int &ans = dp[idx][tight][mask]; n   ans = 0; n   if (idx == transition) { n     for (int i = 0; i <= s[idx] -  0 ; i++) { n       ans += solve(idx + 1, s[idx] -  0  == i, mask | (1 << i), s); n     } n   } else { n     if (tight) { n       for (int i = 0; i <= s[idx] -  0 ; i++) { n         ans += solve(idx + 1, s[idx] -  0  == i, mask | (1 << i), s); n       } n     } else { n       for (int i = 0; i < 7; i++) { n         ans += solve(idx + 1, 0, mask | (1 << i), s); n       } n     } n   } n   return ans; n } n string base7(int n) { n   string ans; n   if (n == 0) { n     ans.push_back( 0 ); n     return ans; n   } n   while (n) { n     ans.push_back( 0  + n % 7); n     n /= 7; n   } n   reverse(ans.begin(), ans.end()); n   return ans; n } n int main() { n   int n, m; n   cin >> n >> m; n   m--; n   n--; n   memset(dp, -1, sizeof(dp)); n   string hours = base7(n); n   string minutes = base7(m); n   string s = hours + minutes; n   transition = hours.size(); n   if (s.size() > 7) n     cout << 0 <<   n ; n   else { n     cout << solve(0, 1, 0, s) <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int fx[4][2] = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}}; n const int fxx[8][2] = {{0, 1}, {0, -1}, {1, 0},  {-1, 0}, n                        {1, 1}, {1, -1}, {-1, 1}, {-1, -1}}; n int gcD(int a, int b) { n   while (a && b) a > b ? a %= b : b %= a; n   return a + b; n } n const int MAXN = 2e5 + 10; n int a[MAXN]; n int N; n int main() { n   scanf( %d , &N); n   int g = 0; n   for (int i = 0; i < N; i++) { n     scanf( %d , &a[i]); n     g = gcD(a[i], g); n   } n   for (int i = 0; i < N; i++) { n     a[i] /= g; n     while ((a[i] & 1) == 0) a[i] = a[i] >> 1; n     while (a[i] % 3 == 0) a[i] /= 3; n     if (a[i] != 1) { n       printf( No n ); n       return 0; n     } n   } n   printf( Yes n ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 310000; n int n, m, cs[N], cd[N], nr, rez[N], aint[4 * N], full[4 * N]; n vector<int> v[N]; n vector<pair<int, int> > x[N]; n bool ver[N]; n void df(int nod) { n   ver[nod] = 1; n   cs[nod] = ++nr; n   for (vector<int>::iterator it = v[nod].begin(); it != v[nod].end(); ++it) n     if (!ver[*it]) df(*it); n   cd[nod] = nr; n } n void update(int nod, int pozx, int pozy, int poz1, int poz2, int val) { n   if (pozx >= poz1 && poz2 >= pozy) { n     full[nod] += val; n     if (full[nod]) n       aint[nod] = pozy - pozx + 1; n     else n       aint[nod] = aint[2 * nod] + aint[2 * nod + 1]; n     return; n   } n   int mid = (pozx + pozy) / 2; n   if (mid >= poz1) update(2 * nod, pozx, mid, poz1, poz2, val); n   if (mid < poz2) update(2 * nod + 1, mid + 1, pozy, poz1, poz2, val); n   if (full[nod]) n     aint[nod] = pozy - pozx + 1; n   else n     aint[nod] = aint[2 * nod + 1] + aint[2 * nod]; n } n void df2(int nod) { n   vector<pair<int, int> >::iterator i; n   vector<int>::iterator it; n   ver[nod] = 1; n   for (i = x[nod].begin(); i != x[nod].end(); ++i) { n     update(1, 1, n, i->first, i->second, 1); n   } n   rez[nod] = max(0, aint[1] - 1); n   for (it = v[nod].begin(); it != v[nod].end(); ++it) n     if (!ver[*it]) df2(*it); n   for (i = x[nod].begin(); i != x[nod].end(); ++i) n     update(1, 1, n, i->first, i->second, -1); n } n int main() { n   int i, a, b; n   cin >> n >> m; n   for (i = 1; i != n; ++i) { n     cin >> a >> b; n     v[a].push_back(b); n     v[b].push_back(a); n   } n   df(1); n   for (i = 1; i <= m; ++i) { n     cin >> a >> b; n     x[a].push_back(pair<int, int>(cs[a], cd[a])); n     x[b].push_back(pair<int, int>(cs[b], cd[b])); n     x[a].push_back(pair<int, int>(cs[b], cd[b])); n     x[b].push_back(pair<int, int>(cs[a], cd[a])); n   } n   for (i = 1; i <= n; ++i) ver[i] = 0; n   df2(1); n   for (i = 1; i <= n; ++i) cout << rez[i] <<    ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long int func1(long long int i) { n   return i * (i + 1) * (2 * i + 1) / 6 - (i + i + 2) * i * (i + 1) / 2 + n          (i + i + 1 + i * i) * i; n } n long long int func2(long long int i) { return i + i * i - i * (i + 1) / 2; } n int main() { n   long long int inp; n   cin >> inp; n   set<pair<long long int, long long int> > s; n   for (long long int i = 1; func1(i) <= inp; i++) n     if ((inp - func1(i)) % func2(i) == 0) { n       s.insert(make_pair(i, i + (inp - func1(i)) / func2(i))); n       s.insert(make_pair(i + (inp - func1(i)) / func2(i), i)); n     } n   cout << s.size() <<   n ; n   for (auto i = s.begin(); i != s.end(); i++) n     cout << (*i).first <<     << (*i).second <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n double dp[200]; n int next1[200]; n int cnt(int i, int j) { n   if ((11 - i) % 2 == 1) { n     return (10 - i) * 10 + j; n   } else { n     return (10 - i) * 10 + (11 - j); n   } n } n int main() { n   for (int i = (1); i < (11); i++) { n     for (int j = (1); j < (11); j++) { n       int x; n       cin >> x; n       int now = cnt(i, j); n       int net = cnt(i - x, j); n       next1[now] = net; n     } n   } n   for (int i = (0); i < (6); i++) dp[99 - i] = 6; n   for (int i = 93; i >= 1; i--) { n     dp[i] = 1; n     for (int j = (1); j < (7); j++) { n       dp[i] += min(dp[i + j], dp[next1[i + j]]) / 6; n     } n   } n   printf( %.10lf , dp[1]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int f[1005][1005]; n int main() { n   int k; n   scanf( %d , &k); n   int w[39], cnt = 0; n   while (k) { n     w[cnt++] = k % 2; n     k /= 2; n   } n   f[1][3] = f[3][1] = 1; n   int now = 3; n   for (int i = 1; i < cnt; i++) { n     f[now][now + 1] = f[now + 1][now] = 1; n     f[now][now + 2] = f[now + 2][now] = 1; n     f[now + 1][now + 3] = f[now + 3][now + 1] = 1; n     f[now + 2][now + 3] = f[now + 3][now + 2] = 1; n     now += 3; n   } n   int tmp = now + 1; n   f[now][2] = f[2][now] = 1; n   for (int i = 0; i < (cnt - 2) * 2; i++) { n     f[tmp][tmp + 1] = f[tmp + 1][tmp] = 1; n     tmp++; n   } n   f[tmp][now] = f[now][tmp] = 1; n   int pos = now + 1; n   now = tmp; n   int t = 1; n   for (int i = 0; i < cnt; i++) { n     if (w[i] == 1) { n       f[t * 3][pos] = f[pos][t * 3] = 1; n     } n     t++; n     pos += 2; n   } n   printf( %d n , now); n   for (int i = 1; i <= now; i++) { n     for (int j = 1; j <= now; j++) n       if (f[i][j]) n         printf( Y ); n       else n         printf( N ); n     puts(  ); n   } n   return 0; n } n