#include <bits/stdc++.h> n using namespace std; n template <typename T> n inline T abs(T t) { n   return t < 0 ? -t : t; n } n const long long modn = 1000000007; n inline long long mod(long long x) { return x % modn; } n int main() { n   long long d1, d2, d3; n   cin >> d1 >> d2 >> d3; n   long long ans = min(d1 + d2 + d3, d1 * 2 + d2 * 2); n   ans = min(ans, min(d1 * 2 + d3 * 2, d2 * 2 + d3 * 2)); n   cout << ans <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<pair<int, int> > marks(200002); n vector<pair<int, int> > marksbyy(200002); n vector<vector<int> > partials(2005, vector<int>(2005)); n vector<vector<int> > inpart(2005, vector<int>(2005)); n int n; n void fillpart() { n   sort(marks.begin() + 1, marks.begin() + n + 1); n   sort(marksbyy.begin() + 1, marksbyy.begin() + n + 1); n   for (int i = 1; i <= n; i++) { n     int j = marks[i].second; n     inpart[(i - 1) / 100][(j - 1) / 100]++; n   } n   partials[0][0] = inpart[0][0]; n   for (int i = 1; i <= 2000; i++) { n     partials[0][i] = partials[0][i - 1] + inpart[0][i]; n   } n   for (int i = 1; i <= 2000; i++) { n     partials[i][0] = partials[i - 1][0] + inpart[i][0]; n   } n   for (int i = 1; i <= 2000; i++) { n     for (int j = 1; j <= 2000; j++) { n       partials[i][j] = partials[i - 1][j] + partials[i][j - 1] - n                        partials[i - 1][j - 1] + inpart[i][j]; n     } n   } n } n int query(int x, int y) { n   int ans = 0; n   int biggestpartx = x / 100 - 1; n   int biggestparty = y / 100 - 1; n   if (biggestpartx >= 0 && biggestparty >= 0) { n     ans += partials[biggestpartx][biggestparty]; n     for (int i = (biggestpartx + 1) * 100 + 1; i <= x; i++) { n       if (marks[i].second <= (biggestparty + 1) * 100) { n         ans++; n       } n     } n     for (int i = (biggestparty + 1) * 100 + 1; i <= y; i++) { n       if (marksbyy[i].second <= x) { n         ans++; n       } n     } n   } else { n     for (int i = 1; i <= x; i++) { n       if (marks[i].second <= y) { n         ans++; n       } n     } n   } n   return ans; n } n int main() { n   ios::sync_with_stdio(false); n   cin.tie(NULL); n   int q; n   cin >> n >> q; n   vector<int> arr(n); n   for (int i = 1; i <= n; i++) { n     int x; n     cin >> x; n     x = n + 1 - x; n     marks[i] = make_pair(x, i); n     marksbyy[i] = make_pair(i, x); n   } n   fillpart(); n   for (int i = 0; i < q; i++) { n     int l, d, r, u; n     cin >> l >> d >> r >> u; n     u = n + 1 - u; n     d = n + 1 - d; n     long long int topleft = query(u - 1, l - 1); n     long long int top = query(u - 1, r) - topleft; n     long long int left = query(d, l - 1) - topleft; n     long long int inside = query(d, r) - topleft - top - left; n     long long int topright = query(u - 1, n) - top - topleft; n     long long int bottomleft = query(n, l - 1) - left - topleft; n     long long int bottom = n         query(n, r) - inside - bottomleft - topleft - top - left; n     long long int right = n         query(d, n) - inside - topright - topleft - top - left; n     long long int bottomright = query(n, n) - inside - topright - topleft - n                                 top - left - bottom - bottomleft - right; n     long long int ans = 0; n     ans += topleft * (bottom + right + bottomright + inside); n     ans += topright * (bottom + left + bottomleft + inside); n     ans += bottomleft * (top + right + topright + inside); n     ans += bottomright * (top + left + topleft + inside); n     ans += inside * (topright + topleft + bottomleft + bottomright + top + n                      right + bottom + left + inside - 1); n     ans += top * (bottom + left + right + bottomleft + bottomright + inside); n     ans += right * (top + left + bottom + topleft + bottomleft + inside); n     ans += bottom * (top + left + right + topleft + topright + inside); n     ans += left * (top + bottom + right + topright + bottomright + inside); n     ans /= 2; n     cout << ans <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 1000000; n int a[300005], b[300005]; n class SegmentTree { n  public: n   SegmentTree* left = nullptr; n   SegmentTree* right = nullptr; n   int start, end, val = 0, lazyadd = 0; n   SegmentTree(int s, int e) { n     start = s; n     end = e; n     if (start != end) { n       left = new SegmentTree(start, (start + end) / 2); n       right = new SegmentTree((start + end) / 2 + 1, end); n     } n   } n   void propogate() { n     if (lazyadd) { n       left->val += lazyadd; n       right->val += lazyadd; n       left->lazyadd += lazyadd; n       right->lazyadd += lazyadd; n       lazyadd = 0; n     } n   } n   void update(int l, int r, int v) { n     if (l > end || r < start) { n       return; n     } n     if (l <= start && end <= r) { n       val += v; n       lazyadd += v; n       return; n     } n     propogate(); n     left->update(l, r, v); n     right->update(l, r, v); n     val = min(left->val, right->val); n   } n   int traverse() { n     if (val >= 0) { n       return -1; n     } n     if (start == end) { n       return start; n     } n     propogate(); n     if (right->val < 0) { n       return right->traverse(); n     } n     return left->traverse(); n   } n }; n int main() { n   ios::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(0); n   int n, m, q, input; n   int index, x; n   cin >> n >> m; n   SegmentTree myTree(1, 1000000); n   for (int i = 1; i <= n; i++) { n     cin >> a[i]; n     myTree.update(1, a[i], -1); n   } n   for (int j = 1; j <= m; j++) { n     cin >> b[j]; n     myTree.update(1, b[j], 1); n   } n   cin >> q; n   for (int w = 0; w < q; w++) { n     cin >> input >> index >> x; n     if (input == 1) { n       myTree.update(1, a[index], 1); n       a[index] = x; n       myTree.update(1, a[index], -1); n     } else { n       myTree.update(1, b[index], -1); n       b[index] = x; n       myTree.update(1, b[index], 1); n     } n     cout << myTree.traverse() << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m, x, len, dir, t, y; n bool fin; n pair<int, int> a[210000]; n int loc[210000]; n int binary_right(int x, int t) { n   int l = x, r = n, mid; n   while (l < r) { n     mid = (l + r + 1) / 2; n     if (a[mid].first <= t) n       l = mid; n     else n       r = mid - 1; n   } n   return l; n } n int binary_left(int x, int t) { n   int l = 1, r = x, mid; n   while (l < r) { n     mid = (l + r) / 2; n     if (a[mid].first >= t) n       r = mid; n     else n       l = mid + 1; n   } n   return l; n } n int main() { n   scanf( %d%d , &n, &m); n   for (int i = 1; i <= n; i++) { n     scanf( %d , &a[i].first); n     a[i].second = i; n   } n   sort(a + 1, a + n + 1); n   for (int i = 1; i <= n; i++) loc[a[i].second] = i; n   for (int i = 1; i <= m; i++) { n     scanf( %d%d , &x, &len); n     x = loc[x]; n     dir = 1; n     fin = 0; n     while (!fin) { n       t = a[x].first + dir * len; n       if (dir == 1) { n         y = binary_right(x, t); n         if (binary_left(y, a[y].first - (len - a[y].first + a[x].first)) == x) { n           if (y == x) n             fin = 1; n           else n             len %= 2 * (a[y].first - a[x].first); n         } else { n           dir = -1; n           len -= (a[y].first - a[x].first); n           x = y; n         } n       } else { n         y = binary_left(x, t); n         if (binary_right(y, a[y].first + (len - a[x].first + a[y].first)) == n             x) { n           if (y == x) n             fin = 1; n           else n             len %= 2 * (a[x].first - a[y].first); n         } else { n           dir = 1; n           len -= (a[x].first - a[y].first); n           x = y; n         } n       } n     } n     printf( %d n , a[x].second); n   } n } n


#include <bits/stdc++.h> n using namespace std; n int n, k; n int main() { n   while (scanf( %d%d , &n, &k) != EOF) { n     if (k > n || k > 26 || (n > 1 && k == 1)) n       printf( -1 n ); n     else { n       for (int i = 0; i < n - max(0, (k - 2)); i++) n         if (i % 2 == 0) n           printf( a ); n         else n           printf( b ); n       for (int i = (2); i <= (k - 1); ++i) printf( %c ,  a  + i); n       printf(  n ); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n; n int a[5000006]; n int S; n void go(int lf, int rg) { n   if (lf < 0) return; n   S = max(S, (rg - lf) / 2); n   if (a[lf] == a[rg]) { n     for (int i = lf; i < rg; ++i) a[i] = a[lf]; n   } else { n     for (int i = lf, j = rg; i < j; ++i, --j) { n       a[i] = a[lf]; n       a[j] = a[rg]; n     } n   } n } n void solve() { n   cin >> n; n   for (int i = 0; i < (n); ++i) scanf( %d , a + i); n   int lst = 0; n   S = 0; n   for (int i = 1; i <= (n - 2); ++i) { n     if (a[i] == a[i - 1] || a[i] == a[i + 1]) { n       if (i - lst > 1) go(lst, i); n       lst = i; n     } n   } n   if (n - 1 - lst > 1) go(lst, n - 1); n   cout << S << endl; n   for (int i = 0; i < (n); ++i) printf( %d  , a[i]); n } n int main() { n   solve(); n   return 0; n } n


#include <bits/stdc++.h> n #pragma GCC optimize( Ofast ) n #pragma GCC target( sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx ) n #pragma GCC optimize( unroll-loops ) n using namespace std; n template <typename T1, typename T2> n inline void chkmin(T1 &first, T2 second) { n   if (first > second) first = second; n } n template <typename T1, typename T2> n inline void chkmax(T1 &first, T2 second) { n   if (first < second) first = second; n } n template <typename T, typename U> n inline ostream &operator<<(ostream &_out, const pair<T, U> &_p) { n   _out << _p.first <<     << _p.second; n   return _out; n } n template <typename T, typename U> n inline istream &operator>>(istream &_in, pair<T, U> &_p) { n   _in >> _p.first >> _p.second; n   return _in; n } n template <typename T> n inline ostream &operator<<(ostream &_out, const vector<T> &_v) { n   if (_v.empty()) { n     return _out; n   } n   _out << _v.front(); n   for (auto _it = ++_v.begin(); _it != _v.end(); ++_it) { n     _out <<     << *_it; n   } n   return _out; n } n template <typename T> n inline istream &operator>>(istream &_in, vector<T> &_v) { n   for (auto &_i : _v) { n     _in >> _i; n   } n   return _in; n } n template <typename T> n inline ostream &operator<<(ostream &_out, const set<T> &_s) { n   if (_s.empty()) { n     return _out; n   } n   _out << *_s.begin(); n   for (auto _it = ++_s.begin(); _it != _s.end(); ++_it) { n     _out <<     << *_it; n   } n   return _out; n } n template <typename T> n inline ostream &operator<<(ostream &_out, const multiset<T> &_s) { n   if (_s.empty()) { n     return _out; n   } n   _out << *_s.begin(); n   for (auto _it = ++_s.begin(); _it != _s.end(); ++_it) { n     _out <<     << *_it; n   } n   return _out; n } n template <typename T> n inline ostream &operator<<(ostream &_out, const unordered_set<T> &_s) { n   if (_s.empty()) { n     return _out; n   } n   _out << *_s.begin(); n   for (auto _it = ++_s.begin(); _it != _s.end(); ++_it) { n     _out <<     << *_it; n   } n   return _out; n } n template <typename T> n inline ostream &operator<<(ostream &_out, const unordered_multiset<T> &_s) { n   if (_s.empty()) { n     return _out; n   } n   _out << *_s.begin(); n   for (auto _it = ++_s.begin(); _it != _s.end(); ++_it) { n     _out <<     << *_it; n   } n   return _out; n } n template <typename T, typename U> n inline ostream &operator<<(ostream &_out, const map<T, U> &_m) { n   if (_m.empty()) { n     return _out; n   } n   _out <<  (  << _m.begin()->first <<  :   << _m.begin()->second <<  ) ; n   for (auto _it = ++_m.begin(); _it != _m.end(); ++_it) { n     _out <<  , (  << _it->first <<  :   << _it->second <<  ) ; n   } n   return _out; n } n template <typename T, typename U> n inline ostream &operator<<(ostream &_out, const unordered_map<T, U> &_m) { n   if (_m.empty()) { n     return _out; n   } n   _out <<  (  << _m.begin()->first <<  :   << _m.begin()->second <<  ) ; n   for (auto _it = ++_m.begin(); _it != _m.end(); ++_it) { n     _out <<  , (  << _it->first <<  :   << _it->second <<  ) ; n   } n   return _out; n } n const int N = 1e5 + 5; n int n, m; n int dsu[N]; n vector<pair<int, int>> edge[N]; n vector<pair<int, pair<int, int>>> edges, v; n set<pair<int, int>> used; n int get(int i) { return i == dsu[i] ? i : dsu[i] = get(dsu[i]); } n bool merge(int i, int j) { n   i = get(i); n   j = get(j); n   if (i == j) return false; n   dsu[i] = j; n   return true; n } n const int LOG = 18; n int t; n int tin[N], tout[N]; n pair<int, int> up[LOG][N]; n void dfs(int i, int p = -1) { n   tin[i] = t++; n   for (auto j : edge[i]) { n     if (j.first == p) continue; n     up[0][j.first] = {i, j.second}; n     dfs(j.first, i); n   } n   tout[i] = t++; n } n bool is_parent(int a, int b) { return tin[a] <= tin[b] && tout[b] <= tout[a]; } n int lca(int a, int b) { n   int ans = 0; n   for (int t = 0; t < 2; ++t) { n     if (tin[a] > tin[b]) swap(a, b); n     if (!is_parent(b, a)) { n       for (int t = LOG; t--;) { n         int c = up[t][b].first; n         if (!is_parent(c, a)) { n           chkmax(ans, up[t][b].second); n           b = c; n         } n       } n       chkmax(ans, up[0][b].second); n       b = up[0][b].first; n     } n     assert(is_parent(b, a)); n   } n   return ans; n } n int main() { n   ios::sync_with_stdio(0); n   srand(time(0)); n   cin >> n >> m; n   edges.resize(m); n   for (int i = 0; i < m; ++i) { n     cin >> edges[i].second.first >> edges[i].second.second >> edges[i].first; n   } n   v = edges; n   sort(edges.begin(), edges.end()); n   bool was = false; n   for (int i = 0; i <= n; ++i) dsu[i] = i; n   for (auto i : edges) { n     if (merge(i.second.first, i.second.second)) { n       used.insert(i.second); n       edge[i.second.first].push_back({i.second.second, i.first}); n       edge[i.second.second].push_back({i.second.first, i.first}); n     } n   } n   up[0][1] = {1, 0}; n   dfs(1); n   for (int t = 0; t + 1 < LOG; ++t) { n     for (int i = 1; i <= n; ++i) { n       up[t + 1][i] = {up[t][up[t][i].first].first, n                       max(up[t][i].second, up[t][up[t][i].first].second)}; n     } n   } n   for (auto i : v) { n     if (used.count(i.second)) { n       continue; n     } n     cout << lca(i.second.first, i.second.second) <<   n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int a[30]; n int main() { n   int n, k, i, j, m; n   string s; n   cin >> s; n   cin >> k; n   map<char, int> mp; n   map<char, int>::iterator it; n   n = s.size(); n   for (i = 0; i < n; i++) { n     ++mp[s[i]]; n   } n   multimap<int, char> mp2; n   multimap<int, char>::iterator it2; n   for (it = mp.begin(); it != mp.end(); it++) { n     mp2.insert(pair<int, char>(it->second, it->first)); n   } n   map<char, int> mp3; n   for (it2 = mp2.begin(); it2 != mp2.end(); it2++) { n     if (k - it2->first >= 0) { n       mp3[it2->second] = 1; n       k -= it2->first; n     } n   } n   string s2 =   ; n   for (i = 0; i < n; i++) { n     if (mp3[s[i]] == 0) { n       s2 += s[i]; n     } n   } n   mp3.clear(); n   n = s2.size(); n   for (i = 0; i < n; i++) { n     ++mp3[s2[i]]; n   } n   cout << mp3.size() << endl; n   cout << s2 << endl; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e5 + 7; n int n, m, q[N], a[N]; n int main() { n   scanf( %d , &m); n   for (int i = (0); i < (m); ++i) scanf( %d , &q[i]); n   int mn = *min_element(q, q + m); n   scanf( %d , &n); n   for (int i = (0); i < (n); ++i) scanf( %d , &a[i]); n   sort(a, a + n); n   int ans = 0; n   for (int i = n - 1, c = 0; i >= 0; --i) { n     ans += a[i], ++c; n     if (c >= mn) c = 0, i -= 2; n   } n   printf( %d , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   int t; n   cin >> t; n   while (t--) { n     int n; n     cin >> n; n     if (n <= 30) { n       cout <<  NO n ; n     } else if (n == 36) { n       cout <<  YES n ; n       cout <<  5 6 10 15  n            <<   n ; n     } else if (n == 40) { n       cout <<  YES n ; n       cout <<  9 6 10 15  n            <<   n ; n     } else if (n == 44) { n       cout <<  YES n ; n       cout <<  6 7 10 21  n            <<   n ; n     } else { n       cout <<  YES n ; n       cout <<  6 10 14   << n - 30 <<   n ; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e6; n int d[2 * N], c[2 * N], n, k, l, p, a = N, q, w, u; n string s; n int main() { n   cin >> n >> k >> l >> s; n   q = 0; n   w = N; n   while (q < w) { n     p = (q + w) >> 1; n     for (int i = n - 1; i >= 0; i--) { n       d[i] = d[i + 1] + (s[i] > 96); n       c[i] = c[i + 1]; n       if (d[i + l] + p < d[i]) d[i] = d[i + l] + p, c[i] = c[i + l] + 1; n     } n     if (c[0] > k) n       q = p + 1; n     else n       w = p, u = d[0] - p * k; n   } n   a = min(a, u); n   for (auto &p : s) p = (p + 32) % 125; n   q = 0; n   w = N; n   while (q < w) { n     p = (q + w) >> 1; n     for (int i = n - 1; i >= 0; i--) { n       d[i] = d[i + 1] + (s[i] > 96); n       c[i] = c[i + 1]; n       if (d[i + l] + p < d[i]) d[i] = d[i + l] + p, c[i] = c[i + l] + 1; n     } n     if (c[0] > k) n       q = p + 1; n     else n       w = p, u = d[0] - p * k; n   } n   a = min(a, u); n   printf( %i n , a); n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int t; n   cin >> t; n   while (t--) { n     unsigned long long a, b; n     cin >> a >> b; n     if (max(a, b) % min(a, b)) { n       cout << -1 << endl; n       continue; n     } n     int dem = 0; n     long long p = max(a, b) / min(a, b); n     while (p != 1) { n       if (p % 8 == 0) { n         dem++; n         p /= 8; n       } else if (p % 4 == 0) { n         dem++; n         p /= 4; n       } else if (p % 2 == 0) { n         dem++; n         p /= 2; n       } else { n         dem = -1; n         break; n       } n     } n     cout << dem << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = (int)5e5 + 9; n const int maxm = (int)1e6 + 9; n const long long mod = (long long)1e9 + 7; n const double pi = acos(-1); n const double eps = 1e-15; n vector<pair<int, int> > to[maxn]; n int fa[maxn]; n int tobig[maxn]; n int n, m; n int e[maxn]; n int e2[maxn]; n int f[maxn]; n int fe[maxn]; n void init() { n   scanf( %d%d , &n, &m); n   for (int i = 0; i < m; i++) { n     int a, b; n     cin >> a >> b; n     to[a].emplace_back(i, b); n     to[b].emplace_back(i, a); n     e[i] = a; n     e2[i] = b; n   } n   memset(tobig, -1, sizeof(tobig)); n } n int vis[maxn]; n void dfs(int u, int ft) { n   vis[u] = -1; n   for (int i = 0; i < to[u].size(); i++) { n     int v = to[u][i].second; n     if (ft == v) continue; n     fa[u] = i; n     if (vis[v] == 0) { n       dfs(v, u); n     } else if (vis[v] == -1) { n       int mn = maxn; n       int mx = -1; n       int tmp = v; n       vector<int> tot; n       do { n         int d = to[tmp][fa[tmp]].first; n         tot.push_back(d); n         mn = min(mn, d); n         mx = max(mx, d); n         tmp = to[tmp][fa[tmp]].second; n       } while (tmp != v); n       tobig[mn] = mx; n       int mxp, mnp; n       int len = tot.size(); n       for (int x = 0; x < len; x++) { n         if (mx == tot[x]) mxp = x; n         if (mn == tot[x]) mnp = x; n       } n       int fl = 0; n       for (int x = mxp; x != mnp; x = (x + 1) % len) { n         if (tot[x] < tot[(x + 1) % len]) { n           fl = 1; n           break; n         } n       } n       for (int x = mxp; x != mnp; x = (x + len - 1) % len) { n         if (tot[x] < tot[(x + len - 1) % len]) { n           fl = 1; n           break; n         } n       } n       if (fl) { n         tobig[mn] = -1; n       } n     } n   } n   vis[u] = 1; n } n int bcj[maxn]; n int fd(int x) { return bcj[x] = bcj[x] == x ? x : fd(bcj[x]); } n int main() { n   init(); n   dfs(1, -1); n   for (int i = 1; i <= n; i++) { n     bcj[i] = i; n     f[i] = 1; n   } n   for (int i = m - 1; i >= 0; i--) { n     int fu = fd(e[i]); n     int fv = fd(e2[i]); n     int tt = f[e[i]] + f[e2[i]]; n     if (fu == fv) { n       if (tobig[i] != -1) { n         tt -= fe[tobig[i]]; n       } n     } else { n       bcj[fu] = fv; n     } n     fe[i] = f[e[i]] = f[e2[i]] = tt; n   } n   for (int i = 1; i <= n; i++) { n     cout << f[i] - 1 <<    ; n   } n   cout << endl; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 100100; n int N, M; n vector<int> conn[MAXN]; n bool adj[10][10]; n bool vis[MAXN]; n int deg[10]; n int ar[MAXN]; n int pa[10]; n int pb[10]; n int numed; n void prec() { n   for (int j = 0; j < numed; ++j) { n     int a = 2, b = 1; n     for (int k = 0; k < j; ++k) { n       if (b + 1 >= a) { n         ++a; n         b = 1; n       } else { n         ++b; n       } n     } n     pa[j] = a; n     pb[j] = b; n   } n } n void go6() { n   numed = N * (N - 1) / 2; n   prec(); n   for (int i = 1; i <= N; ++i) { n     for (typeof((conn[i]).begin()) j = (conn[i]).begin(), _j = (conn[i]).end(); n          j != _j; ++j) { n       adj[i][*j] = true; n     } n   } n   bool fnd = false; n   for (int i = 0; i < (1 << numed); ++i) { n     int cnt = 0; n     bool bad = false; n     for (int j = 1; j <= N; ++j) { n       deg[j] = 0; n     } n     for (int j = 0; j < numed; ++j) { n       if ((i >> j) & 1) { n         int a = pa[j]; n         int b = pb[j]; n         if (adj[a][b]) { n           bad = true; n           break; n         } n         ++deg[a]; n         ++deg[b]; n         if (deg[a] > 2 || deg[b] > 2) { n           bad = true; n           break; n         } n         ++cnt; n       } n     } n     if (bad) continue; n     if (cnt != M) continue; n     fnd = true; n     for (int j = 0; j < numed; ++j) { n       if ((i >> j) & 1) { n         int a = pa[j], b = pb[j]; n         printf( %d %d n , a, b); n       } n     } n     break; n   } n   if (!fnd) { n     printf( -1 n ); n   } n } n int cnt; n bool dfs(int x, int prv = -1) { n   if (vis[x]) return true; n   vis[x] = true; n   ++cnt; n   for (typeof((conn[x]).begin()) i = (conn[x]).begin(), _i = (conn[x]).end(); n        i != _i; ++i) { n     if (*i != prv && dfs(*i, x)) { n       return true; n     } n   } n   return false; n } n int main() { n   scanf( %d %d , &N, &M); n   for (int i = 0; i < M; ++i) { n     int a, b; n     scanf( %d %d , &a, &b); n     conn[a].push_back(b); n     conn[b].push_back(a); n   } n   if (N <= 6) { n     go6(); n   } else { n     if (M > N) { n       printf( -1 n ); n       return 0; n     } n     int pos = 0; n     for (int i = 1; i <= N; ++i) { n       if (!vis[i]) { n         cnt = 0; n         if (dfs(i)) { n           int a = i; n           int b = conn[a][0]; n           int cur = pos; n           int end = pos + cnt; n           bool up = true; n           do { n             ar[cur] = a; n             int c = conn[b][0]; n             if (c == a) c = conn[b][1]; n             a = b; n             b = c; n             if (up) { n               if (cur + 2 < end) n                 cur += 2; n               else { n                 if (cur + 1 < end) n                   cur += 1; n                 else n                   cur -= 1; n                 up = false; n               } n             } else { n               cur -= 2; n             } n           } while (a != i); n         } else { n           int a = i; n           int b = -1; n           while (int((conn[a]).size()) == 2) { n             int p = a; n             if (conn[a][0] == b) n               a = conn[a][1]; n             else n               a = conn[a][0]; n             b = p; n           } n           int cur = pos; n           b = -1; n           while (true) { n             ar[cur] = a; n             ++cur; n             if (int((conn[a]).size()) == 0) break; n             if (int((conn[a]).size()) == 1 && b != -1) break; n             int p = a; n             if (conn[a][0] == b) n               a = conn[a][1]; n             else n               a = conn[a][0]; n             b = p; n           } n         } n         pos += cnt; n       } n     } n     for (int i = 0; i < M; ++i) { n       printf( %d %d n , ar[i], ar[(i + 3) % N]); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, q; n int tim[10000]; n int main() { n   while (~scanf( %d%d , &n, &q)) { n     memset(tim, -1, sizeof(tim)); n     while (q--) { n       int ti, ni, di; n       scanf( %d%d%d , &ti, &ni, &di); n       int cnt = 0; n       for (int i = 1; i <= n; i++) { n         if (tim[i] <= ti) tim[i] = -1; n       } n       for (int i = 1; i <= n; i++) { n         if (tim[i] == -1) cnt++; n       } n       if (cnt >= ni) { n         int sum = 0; n         for (int i = 1; i <= n; i++) { n           if (tim[i] == -1) { n             if (ni > 0) { n               tim[i] = ti + di; n               sum += i; n               ni--; n             } n           } n         } n         printf( %d n , sum); n       } else { n         printf( -1 n ); n       } n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 2000500; n int P[N]; n int D[N]; n multiset<int> S[N]; n inline bool recalc(int x) { n   if (x == 1) { n     D[x] = *S[x].rbegin() + 1; n     return false; n   } else { n     int nd; n     if (S[x].size() == 1) n       nd = *S[x].rbegin(); n     else n       nd = max(*S[x].rbegin(), *(++S[x].rbegin()) + 1); n     if (nd != D[x]) { n       S[P[x]].erase(S[P[x]].find(D[x])); n       D[x] = nd; n       S[P[x]].insert(nd); n       return true; n     } else n       return false; n   } n } n int main() { n   int n; n   scanf( %d , &n); n   P[1] = -1; n   D[0] = 1; n   for (int i = 2; i < n + 2; i++) { n     scanf( %d , &P[i]); n     S[P[i]].insert(0); n     for (int x = P[i]; recalc(x); x = P[x]) n       ; n     printf( %d  , D[1]); n   } n   printf(  n ); n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   cin >> n; n   int a[n + 1]; n   for (int i = 1; i <= n; i++) cin >> a[i]; n   sort(a + 1, a + n + 1); n   if (n % 2) n     cout << a[n / 2 + 1] << endl; n   else n     cout << a[n / 2] << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int n, k, ans; n int a[500101], b[500101], fl[500101]; n int q[500101], qn; n void solve() { n   scanf( %d%d , &n, &k); n   for (int i = 1; i <= n; i++) scanf( %d , &a[i]); n   for (int i = 1; i <= k; i++) { n     scanf( %d , &b[i]); n     fl[b[i]] = 1; n   } n   int fl2 = 0; n   for (int i = 2; i <= k; i++) n     if (a[b[i]] <= a[b[i - 1]] || a[b[i]] - a[b[i - 1]] < b[i] - b[i - 1]) { n       fl2 = 1; n       break; n     } n   if (fl2) { n     printf( -1 n ); n     return; n   } n   for (int i = 1; i <= n; i++) a[i] -= i; n   q[0] = -1000000007; n   int last = 0; n   for (int i = 1; i <= n; i++) { n     if (a[i] >= q[qn]) { n       q[++qn] = a[i]; n       if (fl[i]) last = qn; n     } else { n       int p = upper_bound(q + 1, q + 1 + qn, a[i]) - q; n       if (p > last) { n         q[p] = a[i]; n         if (fl[i]) last = qn = p; n       } n     } n   } n   printf( %d n , n - qn); n } n int main() { n   int T = 1; n   while (T--) { n     solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 2e5 + 100; n int p[N]; n vector<int> G[N]; n int mn[N], mx[N], val[N], sum[N], s[N][2], c[N][2], fa[N], mp[N]; n int head; n void upd(int x) { n   sum[x] = sum[c[x][1]] + sum[c[x][0]] + val[x]; n   mn[x] = min(mn[c[x][0]], n               min(sum[c[x][0]] + val[x], sum[c[x][0]] + val[x] + mn[c[x][1]])); n   mx[x] = max(mx[c[x][0]], n               max(sum[c[x][0]] + val[x], sum[c[x][0]] + val[x] + mx[c[x][1]])); n } n void rot(int x, int d) { n   int f = fa[x]; n   c[f][d ^ 1] = c[x][d]; n   fa[c[x][d]] = f; n   if (fa[f]) c[fa[f]][(c[fa[f]][1] == f)] = x; n   fa[x] = fa[f]; n   c[x][d] = f; n   fa[f] = x; n   upd(f); n   upd(x); n } n void splay(int x, int stop) { n   int t, d1, d2; n   while ((t = fa[x]) - stop) { n     if (fa[t] == stop) n       rot(x, (c[fa[x]][0] == x)); n     else if ((d1 = (c[fa[t]][0] == t)) ^ (d2 = (c[fa[x]][0] == x))) n       rot(x, d2), rot(x, d1); n     else n       rot(t, d1), rot(x, d2); n   } n   head = stop ? head : x; n } n int Next(int x) { n   splay(x, 0); n   for (x = c[x][1]; c[x][0]; x = c[x][0]) n     ; n   return x; n } n int Prev(int x) { n   splay(x, 0); n   for (x = c[x][0]; c[x][1]; x = c[x][1]) n     ; n   return x; n } n void ins(int r, int x) { n   if (!r) { n     head = x; n     return; n   } n   int f = Next(r); n   if (!f) { n     fa[x] = r; n     c[r][1] = x; n     upd(r); n   } else { n     splay(f, r); n     fa[x] = f; n     c[f][0] = x; n     upd(f); n     upd(r); n   } n   splay(x, 0); n } n int cut(int l, int r) { n   int tl = Prev(l), tr = Next(r); n   if (!tl && !tr) n     return r; n   else if (!tl || !tr) { n     int d = !tr, f = tr + tl; n     splay(f, 0); n     int u = c[f][d]; n     fa[u] = c[f][d] = 0; n     upd(f); n     return u; n   } else { n     splay(tr, 0); n     splay(tl, tr); n     int u = c[tl][1]; n     fa[u] = c[tl][1] = 0; n     upd(tl); n     upd(tr); n     return u; n   } n } n int lasth(int x, int h) { n   for (h++;;) { n     if (sum[c[x][0]] + val[x] + mx[c[x][1]] >= h && n         sum[c[x][0]] + val[x] + mn[c[x][1]] <= h) { n       h -= sum[c[x][0]] + val[x]; n       x = c[x][1]; n     } else if (sum[c[x][0]] + val[x] == h) n       return (x == s[mp[x]][0] ? mp[x] : p[mp[x]]); n     else n       x = c[x][0]; n   } n } n int dis(int a, int b) { n   if (a == b) return 0; n   int u = s[a][0], v = s[b][0]; n   splay(v, 0); n   splay(u, v); n   if ((c[fa[u]][1] == u) == 1) { n     swap(u, v); n     splay(v, 0); n     splay(u, v); n   } n   int lcah = min(sum[c[u][0]] + val[u], n                  min(sum[u] + val[v], sum[c[u][0]] + val[u] + mn[c[u][1]])); n   return sum[u] + sum[c[u][0]] + val[u] + val[v] - 2 * lcah; n } n void reorder(int u, int h) { n   int x = s[u][0]; n   splay(x, 0); n   int f = lasth(c[x][0], sum[c[x][0]] + val[x] - h - 1); n   p[u] = f; n   int cuted = cut(s[u][0], s[u][1]); n   ins(Prev(s[f][1]), cuted); n } n int T = 0; n void build(int u) { n   s[u][0] = ++T; n   mp[T] = u; n   sum[T] = val[T] = mn[T] = mx[T] = 1; n   ins(head, T); n   for (int v : G[u]) build(v); n   s[u][1] = ++T; n   mp[T] = u; n   sum[T] = val[T] = mn[T] = mx[T] = -1; n   ins(head, T); n } n int main() { n   int n, m; n   scanf( %d%d , &n, &m); n   memset(mx, -63, sizeof mx); n   memset(mn, 63, sizeof mn); n   for (int i = 0; i < n; i++) { n     int sz = 0; n     scanf( %d , &sz); n     for (int j = 0; j < sz; j++) { n       int v; n       scanf( %d , &v); n       v--; n       G[i].push_back(v); n       p[v] = i; n     } n   } n   build(0); n   for (int i = 0; i < m; i++) { n     int c; n     scanf( %d , &c); n     if (c == 1) { n       int u, v; n       scanf( %d%d , &u, &v); n       u--; n       v--; n       printf( %d n , dis(u, v)); n     } else if (c == 2) { n       int u, h; n       scanf( %d%d , &u, &h); n       u--; n       reorder(u, h); n     } else { n       int h; n       scanf( %d , &h); n       printf( %d n , lasth(head, h) + 1); n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 1e5 + 6; n int n, m; n int a[200]; n bool check() { n   if (*max_element(a, a + n) != 0) { n     return false; n   } n   return true; n } n void solve() { n   int k; n   cin >> n >> k; n   for (int i = 0; i < n; i++) { n     cin >> a[i]; n   } n   if (k == 1) { n     if ((count(a, a + n, *max_element(a, a + n))) != n) { n       cout << -1 <<   n ; n       return; n     } else { n       cout << 1 <<   n ; n       return; n     } n   } n   m = 0; n   while (!check()) { n     int tmp = k; n     m++; n     std::vector<int> b(n); n     for (int i = 0; i < n; i++) { n       if (i == 0) { n         tmp--; n       } else { n         if (a[i] != a[i - 1]) { n           if (tmp == 0) n             break; n           else { n             tmp--; n           } n         } n       } n       b.push_back(i); n     } n     for (auto j : b) { n       a[j] = 0; n     } n   } n   cout << m <<   n ; n } n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0), cout.tie(0); n   int T; n   cin >> T; n   while (T--) { n     solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long n, k, mod = 1000000007, dp[2010][2010]; n long long solve(long long num, long long x) { n   long long i; n   if (x == k) return 1; n   if (dp[num][x] != -1) return dp[num][x]; n   long long res = 0; n   for (i = 1; (i * num) <= n; i++) { n     res += solve(num * i, x + 1) % mod; n   } n   return dp[num][x] = (res % mod); n } n int main() { n   cin >> n >> k; n   memset(dp, -1, sizeof dp); n   cout << solve(1, 0); 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(0); n   int n, k, h = 0; n   cin >> n >> k; n   vector<int> v; n   for (int i = 0; i < n; i++) v.push_back(i); n   for (int ki = 1; ki <= k; ki++) { n     int x; n     cin >> x; n     x = x - (x / (n)) * (n); n     int d = (h + x) % n; n     h = d; n     if (d == n - 1) h = 0; n     n--; n     cout << v[d] + 1 <<    ; n     vector<int> tmp; n     for (int i = 0; i < v.size(); i++) n       if (i != d) tmp.push_back(v[i]); n     v = tmp; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n using ll = long long int; n using ull = unsigned long long int; n using ld = long double; n using pii = pair<int, int>; n using pll = pair<ll, ll>; n const int INT_INF = (int)(2e9); n const ll LL_INF = (ll)(2e18); n const int NIL = -1; n static mt19937 _g(time(nullptr)); n inline ll randint(ll a, ll b) { n   ll w = (_g() << 31LL) ^ _g(); n   return a + w % (b - a + 1); n } n inline void fast_io() { n   ios_base::sync_with_stdio(false); n   cin.tie(nullptr); n   cout.tie(nullptr); n }; n template <typename T> n inline T sign(T x) { n   return T(x > 0) - T(x < 0); n } n template <typename T, typename S> n inline ostream& operator<<(ostream& os, const pair<T, S> p) { n   cout <<  [  << p.first <<  ;  << p.second <<  ] ; n   return os; n } n template <typename T> n inline ostream& operator<<(ostream& os, const vector<T>& v) { n   for (auto el : v) cout << el <<    ; n   return os; n } n template <typename T> n inline T fetch() { n   T ret; n   cin >> ret; n   return ret; n } n template <typename T> n inline void goodbye(const T& val) { n   cout << val <<   n ; n   exit(0); n } n const int MAX_N = (int)1e6 + 777; n const int MOD = (int)1e9 + 7; n int add(int x, int y) { return (x + y >= MOD ? x + y - MOD : x + y); } n int mult(int x, int y) { return (x * 1LL * y) % MOD; } n vector<pii> g[MAX_N]; n int n, m, last; n int dist[MAX_N], __Abacaba[MAX_N], head = 0; n int answ[MAX_N], met[MAX_N]; n vector<int> layers[MAX_N]; n int cls[MAX_N]; n pii pr[MAX_N]; n void add_edge(int v, int u, int digit) { g[v].emplace_back(u, digit); } n void build_long_edge(int v, int u, int id) { n   vector<int> digits; n   for (; id > 0; id /= 10) digits.push_back(id % 10); n   reverse(digits.begin(), digits.end()); n   vector<int> path = {v}; n   for (int i = 0; i < ((int)(digits).size()) - 1; ++i) path.push_back(last++); n   path.push_back(u); n   for (int i = 0; i + 1 < ((int)(path).size()); ++i) n     add_edge(path[i], path[i + 1], digits[i]); n } n void run_bfs(int src) { n   for (int v = 0; v < last; ++v) dist[v] = INT_INF; n   dist[src] = 0; n   __Abacaba[head++] = src; n   layers[0].push_back(src); n   for (int j = 0; j < head; ++j) { n     int v = __Abacaba[j]; n     for (auto& ed : g[v]) { n       int u = ed.first; n       if (dist[u] > dist[v] + 1) { n         dist[u] = dist[v] + 1; n         layers[dist[u]].push_back(u); n         __Abacaba[head++] = u; n       } n     } n   } n } n void build_cls() { n   for (int lay = 1; !layers[lay].empty(); ++lay) { n     for (auto& v : layers[lay]) pr[v] = make_pair(INT_INF, INT_INF); n     for (auto& parent : layers[lay - 1]) { n       for (auto& ed : g[parent]) { n         int v = ed.first; n         if (dist[v] == lay) n           pr[v] = min(pr[v], make_pair(cls[parent], ed.second)); n       } n     } n     vector<pii> ord; n     for (auto& v : layers[lay]) ord.push_back(pr[v]); n     sort(ord.begin(), ord.end()); n     for (auto& v : layers[lay]) n       cls[v] = lower_bound(ord.begin(), ord.end(), pr[v]) - ord.begin(); n   } n } n void build_answ(int v, int cur) { n   met[v] = 1; n   answ[v] = cur; n   for (auto& ed : g[v]) { n     int u = ed.first; n     if (dist[u] != dist[v] + 1 || met[u]) continue; n     if (pr[u] == make_pair(cls[v], ed.second)) n       build_answ(u, add(mult(cur, 10), ed.second)); n   } n } n void solve() { n   cin >> n >> m; n   last = n; n   for (int i = 0; i < m; ++i) { n     int v, u; n     cin >> v >> u; n     --v, --u; n     build_long_edge(v, u, i + 1); n     build_long_edge(u, v, i + 1); n   } n   run_bfs(0); n   build_cls(); n   build_answ(0, 0); n   for (int v = 1; v < n; ++v) cout << answ[v] <<   n ; n } n int main() { n   fast_io(); n   solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long int n, m, k, s = 0, sx, sy, num; n char grid[600][600]; n bool vis[600][600]; n long long int dx[4] = {0, 0, -1, 1}; n long long int dy[4] = {-1, 1, 0, 0}; n bool check(long long int x, long long int y) { n   if (x >= 0 && x < n && y >= 0 && y < m && grid[x][y] ==  .  && !vis[x][y]) n     return 1; n   return 0; n } n void dfs(long long int x, long long int y) { n   vis[x][y] = 1; n   grid[x][y] =  M ; n   if (num >= s - k) return; n   for (long long int i = 0; i < 4; i++) { n     long long int nx = x + dx[i]; n     long long int ny = y + dy[i]; n     if (num >= s - k) { n       break; n     } n     if (check(nx, ny)) { n       num++; n       dfs(nx, ny); n     } n   } n } n int main() { n   cin >> n >> m >> k; n   memset(vis, 0, sizeof vis); n   for (long long int i = 0; i < n; i++) { n     for (long long int j = 0; j < m; j++) { n       cin >> grid[i][j]; n       if (grid[i][j] ==  . ) { n         s++; n         sx = i; n         sy = j; n       } n     } n   } n   num = 1; n   dfs(sx, sy); n   for (long long int i = 0; i < n; i++) { n     for (long long int j = 0; j < m; j++) { n       if (grid[i][j] ==  . ) grid[i][j] =  X ; n       if (grid[i][j] ==  M ) grid[i][j] =  . ; n       cout << grid[i][j]; n     } n     cout << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 505; n unordered_set<int> dp[N]; n int main() { n   int n, k; n   scanf( %d%d , &n, &k); n   dp[0].insert(0); n   for (int i = 0; i < n; ++i) { n     int c; n     scanf( %d , &c); n     for (int j = k; j >= c; --j) { n       for (int l : dp[j - c]) dp[j].insert(l); n       for (int l : dp[j - c]) dp[j].insert(l + c); n       if (dp[j].size() > 0) dp[j].insert(j); n     } n   } n   printf( %d n , dp[k].size()); n   vector<int> res; n   for (int l : dp[k]) res.push_back(l); n   sort(res.begin(), res.end()); n   for (int l : res) printf( %d  , l); n } n


#include <bits/stdc++.h> n using namespace std; n const long double eps = 1e-10; n struct Tpoint { n   long double x, y; n   void input() { n     double _x, _y; n     scanf( %lf%lf , &_x, &_y); n     x = _x; n     y = _y; n   } n } C, H, S; n double t1, t2; n long double T1, T2; n long double ret; n long double Fabs(long double x) { return x < 0 ? -x : x; } n long double Fmin(long double x, long double y) { return x < y ? x : y; } n long double Fmax(long double x, long double y) { return x > y ? x : y; } n long double dist2(Tpoint a, Tpoint b) { n   return (a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y); n } n long double dist(Tpoint a, Tpoint b) { return sqrt(Fabs(dist2(a, b))); } n void init() { n   scanf( %lf%lf , &t1, &t2); n   C.input(); n   H.input(); n   S.input(); n } n bool Inside(Tpoint A, long double r, Tpoint p) { n   return dist2(A, p) < r * r + eps; n } n bool inter(Tpoint A, Tpoint B, long double ra, long double rb) { n   return dist(A, B) < ra + rb + eps; n } n void Normalize(long double &x) { n   if (x > 1.0) x = 1.0; n   if (x < -1.0) x = -1.0; n } n bool inter(Tpoint A, Tpoint B, Tpoint C, long double ra, long double rb, n            long double rc) { n   if (dist(A, B) < Fabs(ra - rb) + eps) return 1; n   long double alpha = atan2(B.y - A.y, B.x - A.x); n   long double dis2 = dist2(A, B); n   long double cosine = n       (ra * ra + dis2 - rb * rb) / (2.0 * ra * sqrt(Fabs(dis2))); n   Normalize(cosine); n   long double beta = acos(cosine); n   Tpoint p; n   p.x = A.x + ra * cos(alpha + beta); n   p.y = A.y + ra * sin(alpha + beta); n   if (Inside(C, rc, p)) return 1; n   p.x = A.x + ra * cos(alpha - beta); n   p.y = A.y + ra * sin(alpha - beta); n   if (Inside(C, rc, p)) return 1; n   return 0; n } n bool check(long double cur) { n   long double rc = cur, rh = Fmax(0, T2 - cur), n               rs = Fmax(0, T1 - cur - dist(S, H)); n   if (!inter(C, H, rc, rh) || !inter(C, S, rc, rs) || !inter(H, S, rh, rs)) n     return 0; n   if (inter(C, H, S, rc, rh, rs)) return 1; n   if (inter(C, S, H, rc, rs, rh)) return 1; n   if (inter(H, S, C, rh, rs, rc)) return 1; n   return 0; n } n long double solve() { n   T1 = dist(C, S) + dist(S, H) + t1; n   T2 = dist(C, H) + t2; n   if (T1 - t1 < T2 + eps) return Fmin(T1, T2); n   long double L = 0, r = Fmin(T1, T2); n   for (int it = 0; it < 100; it++) { n     long double mid = (L + r) / 2; n     if (check(mid)) n       L = mid; n     else n       r = mid; n   } n   return L; n } n void print() { printf( %.10lf n , (double)solve()); } n int main() { n   init(); n   print(); n   return 0; n } n


#include <bits/stdc++.h> n using ul = std::uint32_t; n using ull = std::uint64_t; n using li = std::int32_t; n using ll = std::int64_t; n using vull = std::vector<ull>; n using vul = std::vector<ul>; n const ul sz = 1 << 18; n const ul base = 1e9 + 7; n class scaler { n  public: n   ul v = 0; n   scaler() = default; n   scaler(ul x) : v(x) {} n }; n scaler operator+(scaler a, scaler b) { n   return scaler(a.v + b.v < base ? a.v + b.v : a.v + b.v - base); n } n scaler operator-(scaler a, scaler b) { n   return scaler(a.v < b.v ? a.v + base - b.v : a.v - b.v); n } n scaler operator*(scaler a, scaler b) { return scaler(ull(a.v) * b.v % base); } n template <typename T> n void change(ul pos, const T& v, std::vector<T>& tree) { n   for (tree[pos |= sz] = v, pos >>= 1; pos; pos >>= 1) { n     tree[pos] = tree[pos << 1] + tree[pos << 1 | 1]; n   } n } n template <typename T> n T query(ul l, ul r, const std::vector<T>& tree) { n   T ret = 0; n   for (l = l - 1 | sz, r = r + 1 | sz; l ^ r ^ 1; l >>= 1, r >>= 1) { n     if (~l & 1) { n       ret = ret + tree[l ^ 1]; n     } n     if (r & 1) { n       ret = ret + tree[r ^ 1]; n     } n   } n   return ret; n } n std::vector<scaler> atree(sz << 1, ul(0)); n vull tree(sz << 1, ul(0)); n ul n, q; n vul a(2e5 + 1); n scaler compute(ul l, ul r, ul cut) { n   return scaler(query(l, cut - 1, tree) % base) * scaler(a[cut]) - n          query(l, cut - 1, atree) + query(cut + 1, r, atree) - n          scaler(query(cut + 1, r, tree) % base) * scaler(a[cut]); n } n int main() { n   std::ios::sync_with_stdio(false); n   std::cin.tie(0); n   std::cin >> n >> q; n   for (ul i = 1; i <= n; ++i) { n     std::cin >> a[i]; n     a[i] -= i; n   } n   for (ul i = 1; i <= n; ++i) { n     ul w; n     std::cin >> w; n     change(i, ull(w), tree); n     change(i, scaler(a[i]) * scaler(w), atree); n   } n   for (ul i = 0; i != q; ++i) { n     li x, y; n     std::cin >> x >> y; n     if (x < 0) { n       ul id = -x; n       ul w = y; n       change(id, ull(w), tree); n       change(id, scaler(a[id]) * scaler(w), atree); n     } else { n       ul l = x, r = y; n       ull temp = query(l, r, tree); n       ul lans = l, rans = r + 1; n       while (lans + 1 != rans) { n         ul mid = lans + rans >> 1; n         if ((query(mid, r, tree) << 1) < temp) { n           rans = mid; n         } else { n           lans = mid; n         } n       } n       std::cout << compute(l, r, lans).v << std::endl; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int gcd(int x, int y) { n   while (x > 0 && y > 0) { n     if (x > y) { n       x %= y; n     } else { n       y %= x; n     } n   } n   return x + y; n } n int main() { n   int n, k; n   cin >> n >> k; n   vector<int> a(n + 1); n   vector<bool> used(n + 1); n   for (int i = 1; i <= n; i++) { n     cin >> a[i]; n   } n   int q; n   cin >> q; n   int tasks = n / k; n   int best = -1, worse = 1 << 30; n   int ticketsLeft = k; n   for (int i = 1; i <= q; i++) { n     int curSum = 0; n     for (int j = 1; j <= tasks; j++) { n       int num; n       cin >> num; n       if (!used[num] && j == 1) { n         ticketsLeft--; n       } n       used[num] = true; n       curSum += a[num]; n     } n     best = max(best, curSum); n     worse = min(worse, curSum); n   } n   vector<int> unUsed; n   for (int i = 1; i <= n; i++) { n     if (!used[i]) { n       unUsed.push_back(a[i]); n     } n   } n   if (unUsed.size() >= tasks && ticketsLeft > 0) { n     sort(unUsed.begin(), unUsed.end()); n     int curSum = 0; n     for (int i = 0; i < tasks; i++) { n       curSum += unUsed[i]; n     } n     best = max(best, curSum); n     worse = min(worse, curSum); n     reverse(unUsed.begin(), unUsed.end()); n     curSum = 0; n     for (int i = 0; i < tasks; i++) { n       curSum += unUsed[i]; n     } n     best = max(best, curSum); n     worse = min(worse, curSum); n   } n   printf( %.15lf %.15lf n , double(worse) / double(tasks), n          double(best) / double(tasks)); n } n


#include <bits/stdc++.h> n using namespace std; n long long dp[205][6003], dp1[205][6003]; n int main() { n   long long n, k; n   cin >> n >> k; n   long long tmp; n   for (int i = 0; i <= k; i++) { n     for (int j = 0; j <= 6000; j++) { n       dp1[i][j] = -2e9; n     } n   } n   dp1[0][0] = 0; n   for (int i = 0; i < n; i++) { n     cin >> tmp; n     int cnt2 = 0, cnt5 = 0; n     while (tmp % 2 == 0) { n       tmp /= 2; n       cnt2++; n     } n     while (tmp % 5 == 0) { n       tmp /= 5; n       cnt5++; n     } n     for (int l = 0; l <= k; l++) { n       for (int j = 0; j <= 6000; j++) { n         dp[l][j] = dp1[l][j]; n         if (j - cnt5 >= 0 && l > 0) n           dp[l][j] = max(dp1[l - 1][j - cnt5] + cnt2, dp1[l][j]); n       } n     } n     for (int l = 0; l <= k; l++) { n       for (int j = 0; j <= 6000; j++) { n         dp1[l][j] = dp[l][j]; n       } n     } n   } n   long long max1 = 0; n   for (long long j = 0; j <= 6000; j++) { n     max1 = max(min(dp[k][j], j), max1); n   } n   cout << max1 << endl; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 2e3 + 10; n char s[N], t[N]; n int n; n vector<int> G; n void shift(int x) { n   if (x == 0) return; n   reverse(s, s + n); n   reverse(s + x, s + n); n   G.push_back(x); n } n int main() { n   scanf( %d%s%s , &n, s, t); n   for (int i = 0; i < n; i++) { n     int j = i; n     while (j < n && s[j] != t[n - i - 1]) j++; n     if (j == n) { n       puts( -1 ); n       exit(0); n     } n     shift(n); n     shift(j); n     shift(1); n   } n   printf( %d n , G.size()); n   for (int i = 0; i < (int)G.size(); ++i) printf( %d  , G[i]); n } n


#include <bits/stdc++.h> n using namespace std; n long long n, l; n string s, s1, s2; n int main() { n   cin >> n; n   getline(cin, s); n   while (n--) { n     getline(cin, s); n     s1 =   ; n     s2 =   ; n     l = s.length(); n     for (int i = 0; i <= 4; i++) s1 += s[i]; n     for (int i = l - 5; i < l; i++) s2 += s[i]; n     if (s1 ==  miao.  && s2 !=  lala. ) n       cout <<  Rainbow s  << endl; n     else if (s1 !=  miao.  && s2 ==  lala. ) n       cout <<  Freda s  << endl; n     else n       cout <<  OMG>.< I don t know!  << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, nmax, nrpar, nrimpar, par, impar, i, sol; n int luat[200005]; n struct el { n   int val, poz; n } a[200005]; n bool rearanjare(el x, el y) { n   if (x.poz < y.poz) n     return 1; n   else n     return 0; n } n bool comp(el x, el y) { n   if (x.val < y.val) n     return 1; n   else n     return 0; n } n int main() { n   cin >> n >> nmax; n   for (i = 1; i <= n; i++) { n     cin >> a[i].val; n     a[i].poz = i; n     if (a[i].val % 2 == 0) n       nrpar++; n     else n       nrimpar++; n     if (a[i].val <= 200000) luat[a[i].val] = 1; n   } n   sort(a + 1, a + n + 1, comp); n   impar = 1; n   par = 2; n   for (i = 1; i <= n; i++) n     if (a[i].val == a[i + 1].val) { n       if (nrpar > nrimpar) { n         sol++; n         if (a[i].val % 2 == 0) { n           nrimpar++; n           nrpar--; n         } n         while (luat[impar]) impar += 2; n         a[i].val = impar; n         luat[impar] = 1; n         if (impar > nmax) sol = -1000000; n       } else if (nrpar < nrimpar) { n         sol++; n         if (a[i].val % 2 == 1) { n           nrimpar--; n           nrpar++; n         } n         while (luat[par]) par += 2; n         a[i].val = par; n         luat[par] = 1; n         if (par > nmax) sol = -1000000; n       } else { n         if (a[i].val % 2 == 1) { n           sol++; n           while (luat[impar]) impar += 2; n           a[i].val = impar; n           luat[impar] = 1; n           if (impar > nmax) sol = -1000000; n         } else { n           sol++; n           while (luat[par]) par += 2; n           a[i].val = par; n           luat[par] = 1; n           if (par > nmax) sol = -1000000; n         } n       } n     } n   i = 0; n   while (nrpar != nrimpar) { n     i++; n     if (a[i].val % 2 == 0 and nrpar > nrimpar) { n       sol++; n       nrimpar++; n       nrpar--; n       while (luat[impar]) impar += 2; n       a[i].val = impar; n       luat[impar] = 1; n       if (impar > nmax) sol = -1000000; n     } else if (a[i].val % 2 == 1 and nrpar < nrimpar) { n       sol++; n       nrimpar--; n       nrpar++; n       while (luat[par]) par += 2; n       a[i].val = par; n       luat[par] = 1; n       if (par > nmax) sol = -1000000; n     } n   } n   sort(a + 1, a + n + 1, rearanjare); n   if (sol < 0) { n     cout << -1; n     return 0; n   } n   cout << sol <<   n ; n   for (i = 1; i <= n; i++) cout << a[i].val <<    ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long t; n   cin >> t; n   while (t--) { n     long long n; n     cin >> n; n     string s, t; n     cin >> s >> t; n     long long w = 0; n     char s1, t1, s2, t2; n     int q = 0; n     for (long long i = 0; i < n; i++) { n       if (s[i] != t[i]) { n         w++; n       } n       if (w == 1 && q == 0) { n         s1 = s[i], t1 = t[i]; n         q++; n       } n       if (w == 2 && q == 1) { n         s2 = s[i], t2 = t[i]; n         q++; n       } n     } n     if (w != 2) { n       cout <<  NO  << endl; n     } else { n       if (t1 == t2 && s1 == s2) n         cout <<  yes  << endl; n       else n         cout <<  no  << endl; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n int main() { n   int a; n   scanf( %d , &a); n   int m[a]; n   int p = 1, i; n   for (i = 0; i < a; i++) { n     scanf( %d , &m[i]); n     if (i > 0) { n       if (m[i] != m[i - 1]) { n         p++; n       } n     } n   } n   printf( %d , p); n } n


#include <bits/stdc++.h> n using namespace std; n namespace variables { n struct triple { n   long long first, second, third; n }; n }  // namespace variables n using namespace variables; n void testt(); n namespace andu2006 { n void debug(set<long long> _var, string _name =   ) { n   cerr <<  Debug #set   << _name <<   = [ ; n   for (auto it : _var) cerr << it <<  , ; n   cerr <<  END] :   n        <<  size =   << _var.size() <<   n n ; n } n void light_debug(set<long long> _var, string _name =   ) { n   cerr <<  Debug #set   << _name <<   = [ ; n   for (auto it : _var) cerr << it <<  , ; n   cerr <<  END] n n ; n } n void debug(map<long long, long long> _var, string _name =   ) { n   cerr <<  Debug #map (size =   << _var.size() <<  ) n ; n   for (auto it : _var) n     cerr << _name <<  [  << it.first <<  ] =   << it.second <<   n ; n } n void light_debug(map<long long, long long> _var, string _name =   ) { n   cerr <<  Debug #map  n ; n   for (auto it : _var) n     cerr << _name <<  [  << it.first <<  ] =   << it.second <<   n ; n } n void debug(deque<long long> _var, string _name =   ) { n   cerr <<  Debug #deque   << _name <<   = [ ; n   for (auto it : _var) cerr << it <<  , ; n   cerr <<  END] :   n        <<  size =   << _var.size() <<   n n ; n } n void light_debug(deque<long long> _var, string _name =   ) { n   cerr <<  Debug #deque   << _name <<   = [ ; n   for (auto it : _var) cerr << it <<  , ; n   cerr <<  END] n n ; n } n void debug(vector<long long> _var, string _name =   ) { n   cerr <<  Debug #vector   << _name <<   = [ ; n   for (auto it : _var) cerr << it <<  , ; n   cerr <<  END] :   n        <<  size =   << _var.size() <<   n n ; n } n void light_debug(vector<long long> _var, string _name =   ) { n   cerr <<  Debug #vector   << _name <<   = [ ; n   for (auto it : _var) cerr << it <<  , ; n   cerr <<  END] n n ; n } n void debug(string _var, string _name =   ) { n   cerr <<  Debug #string   << _name <<   =  ; n   cerr <<     << _var <<    ; n   cerr <<   :   n        <<  size =   << _var.size() <<   n n ; n } n void light_debug(string _var, string _name =   ) { n   cerr <<  Debug #string :   << _name <<   =   <<     << _var <<     <<   n n ; n } n void debug(long long _var, string _name =   ) { n   cerr <<  Debug #int64 :   << _name <<   =   << _var <<   n n ; n } n void debug(unsigned long long _var, string _name =   ) { n   cerr <<  Debug #uint64 :   << _name <<   =   << _var <<   n n ; n } n void debug(unsigned int _var, string _name =   ) { n   cerr <<  Debug #uint32 :   << _name <<   =   << _var <<   n n ; n } n void debug(int _var, string _name =   ) { n   cerr <<  Debug #int32 :   << _name <<   =   << _var <<   n n ; n } n void debug(char _var, string _name =   ) { n   cerr <<  Debug #char :   << _name <<   =    << _var <<    n n ; n } n void debug(bool _var, string _name =   ) { n   cerr <<  Debug #Boolean :   << _name <<   =   << (_var ?  1 True  :  0 False ) n        <<   n n ; n } n string emptyString; n vector<long long> emptyVector; n stack<long long> emptyStack; n queue<long long> emptyQueue; n void multipleTests() { n   long long __test_count; n   cin >> __test_count; n   while (__test_count--) testt(); n } n void done() { exit(0); } n void fastIO() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n } n void rseed() { srand(time(NULL)); } n void mreset(void* _pointer, unsigned int _size) { memset(_pointer, 0, _size); } n template <char compArg> n bool compare(long long a, long long b) { n   if (compArg ==  < ) n     return a < b; n   else if (compArg ==  > ) n     return a > b; n   return false; n } n template <char compArg1, char compArg2> n bool compair(pair<long long, long long> a, pair<long long, long long> b) { n   return compare<compArg1>(a.first, b.first) || n          (a.first == b.first && compare<compArg2>(a.second, b.second)); n } n template <char compArg1, char compArg2> n struct objcompair { n   bool operator()(pair<long long, long long> a, pair<long long, long long> b) { n     return compare<compArg1>(a.first, b.first) || n            (a.first == b.first && compare<compArg2>(a.second, b.second)); n   } n }; n long long trailingZeroes(long long x) { n   if (x == 0) return 0; n   return (x & 1 ? 0 : 1 + trailingZeroes(x / 2)); n } n long long trailingOnes(long long x) { n   if (x == 0) return 0; n   return (x & 1 ? 1 + trailingZeroes(x / 2) : 0); n } n long long trailingPowerOfTwo(long long x) { return 1ll << trailingZeroes(x); } n long long popCount(long long x) { n   if (x == 0) return x; n   return 1 + popCount(x & -x); n } n long long log2(long long x) { n   if (x == 0) return -1; n   return 1 + log2(x / 2); n } n long long getBit(long long x, long long pos) { return (x >> pos) & 1; } n long long cutLastOne(long long x) { return x & -x; } n long long binPow(long long _num, long long _exponent, long long _mod) { n   if (_exponent == 0) return 1; n   long long _half = binPow(_num, _exponent / 2, _mod); n   return _half * _half % _mod * (_exponent & 1 ? _num : 1) % _mod; n } n long long modularInverse(long long _x, long long _mod) { n   return binPow(_x, _mod - 2, _mod); n } n long long comb(long long _n, long long _k, long long _mod) { n   long long _ans = 1; n   for (long long i = _n - _k + 1; i <= _n; i++) _ans = _ans * i % _mod; n   for (long long i = 2; i <= _k; i++) n     _ans = _ans * modularInverse(i, _mod) % _mod; n   return _ans; n } n const long double PI = acos(-1); n const long double rad = 180 / PI; n long double degsin(long long __alpha) { return sin(__alpha / rad); } n long double degcos(long long __alpha) { return cos(__alpha / rad); } n long double degtan(long long __alpha) { return tan(__alpha / rad); } n long double degasin(long long _sine) { return asin(_sine) * rad; } n long double degacos(long long _cosine) { return acos(_cosine) * rad; } n long double degatan(long long _tangent) { return atan(_tangent) * rad; } n long long gcd(long long _num1, long long _num2) { n   if (_num2 == 0) return _num1; n   return gcd(_num2, _num1 % _num2); n } n long long lcm(long long _num1, long long _num2) { n   return _num1 * _num2 / gcd(_num1, _num2); n } n long long divisors(long long _num) { n   if (_num == 0) return 0; n   long long _cnt = 1, _exp; n   while (_num % 2 == 0) { n     _cnt++; n     _num /= 2; n   } n   for (long long _divisor = 3; _divisor * _divisor <= _num; _divisor += 2) { n     _exp = 0; n     while (_num % _divisor == 0) { n       _exp++; n       _num /= _divisor; n     } n     _cnt *= (_exp + 1); n   } n   if (_num > 1) _cnt *= 2; n   return _cnt; n } n long long eulerTotient(long long _num) { n   if (_num == 0) return 0; n   long long _cnt = 1, _divisor = 2; n   while (_num % _divisor == 0) { n     _cnt *= _divisor; n     _num /= _divisor; n   } n   _cnt /= _divisor; n   _cnt *= (_divisor - 1); n   for (long long _divisor = 3; _divisor * _divisor <= _num; _divisor += 2) { n     if (_num % _divisor == 0) { n       while (_num % _divisor == 0) { n         _cnt *= _divisor; n         _num /= _divisor; n       } n       _cnt /= _divisor; n       _cnt *= (_divisor - 1); n     } n   } n   if (_num > 1) _cnt *= _num - 1; n   return _cnt; n } n void sieve(bool* _startpos, bool* _endpos) { n   long long _maxnum = _endpos - _startpos; n   for (long long i = 4; i < _maxnum; i += 2) *(_startpos + i) = 1; n   for (long long i = 3; i * i < _maxnum; i += 2) n     if (*(_startpos + i) == 0) n       for (long long j = i * i; j < _maxnum; j += i) *(_startpos + j) = 1; n } n }  // namespace andu2006 n void testt() {} n set<long long> points; n long long pos[100001], fr[100001], p[5001], sizp; n int main() { n   andu2006::fastIO(); n   long long p1[100], p2[100], n, m; n   for (long long i = 0; i < 100001; i++) pos[i] = -1; n   cin >> n >> m; n   for (long long i = 0; i < n; i++) cin >> p1[i]; n   for (long long i = 0; i < m; i++) { n     cin >> p2[i]; n     pos[p2[i] + 40000] = i; n     fr[p2[i] + 40000]++; n     for (long long j = 0; j < n; j++) points.insert(p1[j] + p2[i]); n   } n   long long ans = 0; n   bool b1[61] = {0}, b2[61] = {0}; n   for (auto it : points) p[sizp++] = it; n   for (long long i = 0; i < sizp; i++) { n     for (long long j = i; j < sizp; j++) { n       for (long long k = 0; k < n; k++) { n         if (pos[p[i] - p1[k] + 40000] != -1) { n           b2[pos[p[i] - p1[k] + 40000]] = 1; n           b1[k] = 1; n         } n         if (pos[p[j] - p1[k] + 40000] != -1) { n           b2[pos[p[j] - p1[k] + 40000]] = 1; n           b1[k] = 1; n         } n       } n       long long cnt = 0; n       for (long long i = 0; i < max(n, m); i++) { n         if (b1[i] == 1) cnt++; n         if (b2[i] == 1) cnt += fr[p2[i] + 40000]; n         b1[i] = b2[i] = 0; n       } n       ans = max(ans, cnt); n     } n   } n   cout << ans; n   andu2006::done(); n } n


#include <bits/stdc++.h> n using namespace std; n int s[101][101]; n int main() { n   int n, a, r; n   cin >> n; n   a = 100; n   while (a * (a - 1) * (a - 2) / 6 > n) a--; n   n -= a * (a - 1) * (a - 2) / 6; n   r = a; n   for (int i = 1; i < a; i++) n     for (int j = i + 1; j <= a; j++) s[i][j] = s[j][i] = 1; n   for (int i = a; i >= 2; i--) { n     while (n >= i * (i - 1) / 2) { n       n -= i * (i - 1) / 2; n       r++; n       for (int j = 1; j <= i; j++) { n         s[r][j] = s[j][r] = 1; n       } n     } n   } n   cout << r << endl; n   for (int i = 1; i <= r; i++) { n     for (int j = 1; j <= r; j++) { n       cout << s[i][j]; n     } n     cout << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long power(long long a, long long b, long long mod) { n   if (b == 0) return 1; n   long long val = power(a, b >> 1, mod); n   long long ans = ((val % mod) * (val % mod)) % mod; n   if (b % 2 != 0) ans = ((ans % mod) * (a % mod)) % mod; n   return ans; n } n int m[1002][1002] = {0}; n int m2[1002][1002] = {0}; n int main(void) { n   int h, w; n   cin >> h >> w; n   vector<int> R, C; n   for (int i = 0; i < h; i++) { n     int r; n     cin >> r; n     for (int j = 0; j < r; j++) m[i][j] = 1; n     R.push_back(r); n   } n   for (int i = 0; i < w; i++) { n     int r; n     cin >> r; n     for (int j = 0; j < r; j++) m[j][i] = 1; n     C.push_back(r); n   } n   for (int i = 0; i < h; i++) { n     int c = 0; n     for (int j = 0; j < w; j++) { n       if (m[i][j] == 0) break; n       c++; n     } n     if (c != R[i]) { n       cout << 0 << endl; n       return 0; n     } n   } n   for (int i = 0; i < w; i++) { n     int c = 0; n     for (int j = 0; j < h; j++) { n       if (m[j][i] == 0) break; n       c++; n     } n     if (c != C[i]) { n       cout << 0 << endl; n       return 0; n     } n   } n   long long cnt = 0; n   for (int i = 0; i < h; i++) { n     for (int j = 0; j < w; j++) { n       if (m[i][j] == 0) { n         for (int l = j + 1; l < w; l++) { n           if (m[i][l] != 1) m2[i][l]++; n         } n         break; n       } n     } n   } n   for (int i = 0; i < w; i++) { n     for (int j = 0; j < h; j++) { n       if (m[j][i] == 0) { n         for (int l = j + 1; l < h; l++) { n           if (m[l][i] != 1) m2[l][i]++; n         } n         break; n       } n     } n   } n   for (int i = 0; i < h; i++) { n     for (int j = 0; j < w; j++) n       if (m2[i][j] == 2) cnt++; n   } n   cout << power(2, cnt, 1000000007) << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); n int dx[8] = {1, -1, 0, 0, -1, -1, 1, 1}; n int dy[8] = {0, 0, -1, 1, -1, 1, -1, 1}; n vector<int> adj[300010], val(300010, 1), vis(300010, 0), dis1(300010, 0), n     dis2(300010, 0); n int myrandom(int i) { return std::rand() % i; } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   long long n, t; n   long double p; n   cin >> n >> p >> t; n   long double dp[t + 2][n + 2]; n   memset(dp, 0, sizeof(dp)); n   dp[0][0] = 1; n   for (int i = 0; i <= t; i++) { n     for (int j = 0; j <= n; j++) { n       if (j < n) { n         dp[i + 1][j + 1] += dp[i][j] * p; n         dp[i + 1][j] += dp[i][j] * (1 - p); n       } else { n         dp[i + 1][j] += dp[i][j]; n       } n     } n   } n   long double ans = 0; n   for (int i = 1; i <= n; i++) { n     ans = ans + i * (dp[t][i]); n   } n   cout << fixed << setprecision(20) << ans <<   n ; n } n


#include <bits/stdc++.h> n int main() { n   char a[200]; n   int n; n   bool plat[200]; n   scanf( %d , &n); n   scanf( %s , a); n   for (int i = 0; i < n; i++) { n     if (a[i] ==  * ) n       plat[i] = true; n     else n       plat[i] = false; n   } n   bool ans = false; n   for (int i = 0; i < n; i++) { n     if (!plat[i]) continue; n     for (int m = 1; i + m * 4 < n; m++) { n       if (plat[i + m] && plat[i + 2 * m] && plat[i + 3 * m] && n           plat[i + 4 * m]) { n         ans = true; n         break; n       } n     } n   } n   printf( %s , ans ?  yes n  :  no n ); n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   int n, p; n   cin >> n >> p; n   vector<int> a(n); n   for (auto &i : a) cin >> i; n   int m = *max_element((a).begin(), (a).end()); n   vector<int> freq(m + n + 10); n   for (int i = 0; i < n; i++) { n     freq[a[i]]++; n   } n   for (int i = 1; i < m + n + 10; i++) { n     freq[i] += freq[i - 1]; n   } n   vector<int> ans; n   for (int x = max(1, m - n + 1); x < m; x++) { n     int i = 0; n     while (i < n) { n       int vals = (freq[x + i] - i); n       if (vals % p == 0) { n         break; n       } n       i++; n     } n     if (i == n) ans.push_back(x); n   } n   cout << ans.size() << endl; n   for (auto &x : ans) { n     cout << x <<    ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long mod = 1e9 + 5; n template <class T> n void read(vector<T> &arr) { n   for (auto &x : arr) cin >> x; n } n template <class T> n void read(T &x) { n   cin >> x; n } n template <class T> n void write(vector<T> &arr) { n   for (auto x : arr) cout << x <<    ; n   cout <<   n ; n } n template <class T> n void write(vector<vector<T>> &arr) { n   for (auto &x : arr) { n     for (auto &y : x) cout << y <<    ; n     cout <<   n ; n   } n } n template <class T> n void write(T &x) { n   cout << x; n } n void solve() { n   long long n; n   cin >> n; n   vector<long long> arr(n); n   read(arr); n   long long ans = 0; n   bool isStarted = false; n   for (long long i = 0; i < n; ++i) { n     if (arr[i] == 1) { n       isStarted = true; n     } n     if (arr[i] == 0 && isStarted) ans++; n   } n   for (long long i = n - 1; i > -1; --i) { n     if (arr[i] == 1) break; n     if (arr[i] == 0) ans--; n   } n   cout << ans <<   n ; n } n int32_t main() { n   ios::sync_with_stdio(false); n   cin.tie(nullptr); n   cout.tie(nullptr); n   long long t = 1; n   cin >> t; n   while (t--) { n     solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios::sync_with_stdio(0); n   cin.tie(NULL); n   cout.tie(NULL); n   int t; n   cin >> t; n   while (t--) { n     long long n, m; n     cin >> n >> m; n     vector<string> ar(11); n     for (int i = 0; i < n; i++) { n       cin >> ar[i]; n     } n     string ans; n     bool res = false; n     for (int i = 0; i < m && !res; i++) { n       for (int j = 0; j < 26 && !res; j++) { n         string s = ar[0]; n         s[i] =  a  + j; n         bool te = true; n         for (int k = 0; k < n; k++) { n           long long co = 0; n           for (int l = 0; l < m; l++) { n             if (ar[k][l] != s[l]) { n               co++; n             } n           } n           if (co > 1) { n             te = false; n             break; n           } n         } n         if (te) { n           cout << s << endl; n           res = true; n           break; n         } n       } n     } n     if (!res) { n       cout << -1 << endl; n     } n   } n } n


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


#include <bits/stdc++.h> n using namespace std; n long long max(long long a, long long b) { n   if (a > b) { n     return a; n   } else { n     return b; n   } n } n long long min(long long a, long long b) { n   if (a < b) { n     return a; n   } else { n     return b; n   } n } n struct P { n   double x, y; n   P() {} n   P(double x, double y) : x(x), y(y) {} n   P operator+(const P &a) const { return P(x + a.x, y + a.y); } n   P operator-(const P &a) const { return P(x - a.x, y - a.y); } n   double operator^(const P &a) const { return x * a.x + y * a.y; } n   void in() { scanf( %lf%lf , &x, &y); } n   void out() { printf( REQUIRED %.7f %.7f n , x, y); } n   double dist(P a, P b) { n     return sqrt((a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y)); n   } n   double sqdist(P a, P b) { n     return (a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y); n   } n }; n int m, n; n int ma[50001][101]; n int fin[50001][101]; n int main() { n   scanf( %d%d , &m, &n); n   for (int i = (1); i <= (m); i++) n     for (int j = (1); j <= (n); j++) scanf( %d , &ma[i][j]); n   fin[1][1] = ma[1][1]; n   for (int i = (2); i <= (m); i++) fin[i][1] = fin[i - 1][1] + ma[i][1]; n   for (int j = (2); j <= (n); j++) { n     fin[1][j] = ma[1][j] + fin[1][j - 1]; n     for (int i = (2); i <= (m); i++) n       fin[i][j] = max(fin[i - 1][j] + ma[i][j], fin[i][j - 1] + ma[i][j]); n   } n   for (int i = (1); i <= (m); i++) printf( %d  , fin[i][n]); n   puts(  ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void canhazfast() { n   ios_base::sync_with_stdio(false); n   cin.tie(nullptr); n   cout.tie(nullptr); n } n template <typename T> n T gcd(T a, T b) { n   return b ? gcd(b, a % b) : a; n } n template <typename T> n T extgcd(T a, T b, T &x, T &y) { n   T x0 = 1, y0 = 0, x1 = 0, y1 = 1; n   while (b) { n     T q = a / b; n     a %= b; n     swap(a, b); n     x0 -= q * x1; n     swap(x0, x1); n     y0 -= q * y1; n     swap(y0, y1); n   } n   x = x0; n   y = y0; n   return a; n } n int ctz(unsigned x) { return __builtin_ctz(x); } n int ctzll(unsigned long long x) { return __builtin_ctzll(x); } n int clz(unsigned x) { return __builtin_clz(x); } n int clzll(unsigned long long x) { return __builtin_clzll(x); } n int popcnt(unsigned x) { return __builtin_popcount(x); } n int popcntll(unsigned long long x) { return __builtin_popcountll(x); } n int bsr(unsigned x) { return 31 ^ clz(x); } n int bsrll(unsigned long long x) { return 63 ^ clzll(x); } n map<int, long long> in[100016], out[100016]; n void do_vertex(int u) { n   for (auto i = in[u].begin(), j = out[u].begin(); n        i != in[u].end() && j != out[u].end();) { n     int s, t; n     long long fs, ft; n     tie(s, fs) = *i; n     tie(t, ft) = *j; n     long long z = min(fs, ft); n     if (z > 0) { n       out[s][u] -= z; n       in[t][u] -= z; n       i->second -= z; n       j->second -= z; n       if (s != t) { n         out[s][t] += z; n         in[t][s] += z; n       } n     } n     if (i->second == 0) i = in[u].erase(i); n     if (j->second == 0) j = out[u].erase(j); n   } n } n int main() { n   canhazfast(); n   int n, m; n   vector<pair<pair<int, int>, long long>> ans; n   cin >> n >> m; n   for (int i = 0; i < m; ++i) { n     int u, v, w; n     cin >> u >> v >> w; n     out[u][v] += w; n     in[v][u] += w; n   } n   for (int i = 1; i <= n; ++i) do_vertex(i); n   for (int i = 1; i <= n; ++i) { n     for (const auto &x : out[i]) { n       if (!x.second || x.first == i) continue; n       ans.emplace_back(pair<int, int>(i, x.first), x.second); n     } n   } n   cout << ans.size() <<   n ; n   for (const pair<pair<int, int>, long long> e : ans) { n     cout << e.first.first <<     << e.first.second <<     << e.second <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n #include<string> n #define M 10000000000007 n using namespace std; n typedef long long int ll; n int gcd(int a,int b) n { n     if (a == 0) n         return b; n     return gcd(b % a, a); n } n int modpow(int x,int n) n { n     int result=1; n     while(n>0) n     { n         if(n % 2 ==1) n             result=((result%M) * (x%M))%M; n         x=((x%M)*(x%M))%M; n         n=n/2; n     } n     return result; n } n int lcm(int a,int b) n { n     return (a*b)/(gcd(a,b)); n } n bool isprime(int n) n { n     if(n<2) n         return false; n     for(int i=2; i<=sqrt(n); i++) n         if(n%i==0) n             return false; n     return true; n } n int ctoi(char c) n { n     return c-48; n } n bool visited[1000000] {false}; n void add_edge(vector<int> v[],int x,int y) n { n     v[x].push_back(y); n     v[y].push_back(x); n } n void dfs(vector<int> v[],int e) n { n     visited[e] = true; n     for(int i=0; i<v[e].size(); i++) n     { n         if(!visited[v[e][i]]) n         { n             dfs(v,v[e][i]); n         } n     } n } n int func(int n) n { n     if(isprime(n)) n     { n         return 2*n; n     } n     else n     { n         int idx = 0; n         for(int i=2; i<=n; i++) n         { n             if(n%i==0) n             { n                 idx = i; n                 break; n             } n         } n         return (idx+n); n     } n } n int maxSubArraySum(ll a[], ll size) n { n     int max_so_far = INT_MIN, max_ending_here = 0,l=0; n  n     for (int i = 0; i < size; i++) n     { n         max_ending_here = max_ending_here + a[i]; n         if (max_so_far < max_ending_here) n         { n             max_so_far = max_ending_here; n             l++; n         } n  n  n         if (max_ending_here < 0) n         { n             max_ending_here = 0; n  n         } n  n     } n     if(l==size)max_so_far=INT_MAX; n     return max_so_far; n } n  n int main() n { n  int t; n  cin>>t; n  while(t--) n  { n      ll a1,b1,a2,b2,a3,b3; n   cin>>a1>>b1>>a2>>b2>>a3>>b3; n   ll ans=abs(a1-a2)+abs(b1-b2); n   if(a1==a2 && a1==a3 ) n         { n             if((b3>b1 && b3<b2) || (b3>b2 && b3<b1))ans+=2; n         } n         if(b1==b2 && b1==b3 ) n         { n             if((a3>a1 && a3<a2) || (a3>a2 && a3<a1))ans+=2; n         } n   cout<<ans<<endl; n  n  n  n  } n  return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, cnt = 0, x, i; n   scanf( %d , &n); n   while (n--) { n     scanf( %d , &x); n     if (x == 1) cnt++; n   } n   if (cnt) n     cout << -1 << endl; n   else n     cout << 1 << endl; n   return 0; n } n


#include <bits/stdc++.h> n int main() { n   double k, b, n, t; n   double bacteria = 1; n   double time = 0; n   scanf( %lf %lf %lf %lf , &k, &b, &n, &t); n   while (bacteria <= t) { n     bacteria = (k * bacteria + b); n     if (bacteria > t) { n       break; n     } else if (bacteria == t) { n       time++; n       break; n     } n     time++; n   } n   if (time > n) { n     n = 0; n   } else { n     n -= time; n   } n   printf( %0.0lf n , n); n   return 0; n } n


#include <bits/stdc++.h> n const static long long INF = 0x3f3f3f3f; n static inline void setio(void); n long long dp[5002][5002]; n int main(void) { n   setio(); n   memset(dp, 0, sizeof(dp)); n   long long n, m; n   std::cin >> n >> m; n   std::string s; n   for (long long i = 0; i < n; ++i) { n     std::cin >> s; n     for (long long j = 0, k; j < m;) { n       if (s[j] !=  1 ) { n         ++j; n         continue; n       } n       for (k = j + 1; k < m && s[k] ==  1 ; ++k) n         ; n       ++dp[j][k - 1]; n       j = k + 1; n     } n   } n   for (long long j = 0; j < m; ++j) { n     for (long long k = m - 2; k >= j; --k) dp[j][k] += dp[j][k + 1]; n   } n   for (long long j = 1; j < m; ++j) { n     for (long long k = 0; k < m; ++k) dp[j][k] += dp[j - 1][k]; n   } n   long long best = 0; n   for (long long i = 0; i < m; ++i) n     for (long long j = 0; j < m; ++j) n       best = std::max(best, (j - i + 1) * dp[i][j]); n   std::cout << best <<   n ; n   return 0; n } n static inline void setio(void) { n   std::ios::sync_with_stdio(false); n   std::cin.tie(nullptr); n   std::cout.precision(10); n   std::cout << std::fixed; n } n


#include <bits/stdc++.h> n using namespace std; n using ll = long long int; n using ull = unsigned long long int; n using ld = long double; n using pii = pair<int, int>; n using pll = pair<ll, ll>; n const int INT_INF = (int)(2e9); n const ll LL_INF = (ll)(2e18); n const ll mod = 1e9 + 7; n const int NIL = -1; n static mt19937 _g(time(nullptr)); n inline ll randint(ll a, ll b) { n   ll w = (_g() << 31LL) ^ _g(); n   return a + w % (b - a + 1); n } n inline void fast_io() { n   ios_base::sync_with_stdio(false); n   cin.tie(nullptr); n   cout.tie(nullptr); n }; n template <typename T> n inline T sign(T x) { n   return T(x > 0) - T(x < 0); n } n template <typename T, typename S> n inline ostream& operator<<(ostream& os, const pair<T, S> p) { n   cout <<  [  << p.first <<  ;  << p.second <<  ] ; n   return os; n } n template <typename T> n inline ostream& operator<<(ostream& os, const vector<T>& v) { n   for (auto el : v) cout << el <<    ; n   return os; n } n struct edge { n   int v, u; n   ll weight; n   int id; n   edge(int _v, int _u, ll _weight, int _id) n       : v(_v), u(_u), weight(_weight), id(_id) {} n   bool operator<(const edge& other) const { return weight < other.weight; } n }; n const int MAX_N = (int)1e5 + 77; n int n, m; n vector<edge> edges; n namespace dsu { n int p[MAX_N], r[MAX_N]; n inline void clear() { n   for (int i = 0; i < n; ++i) p[i] = i, r[i] = 1; n } n int get(int v) { n   if (p[v] == v) return v; n   return (p[v] = get(p[v])); n } n inline void merge(int v, int u) { n   v = get(v), u = get(u); n   if (v != u) { n     if (r[v] > r[u]) swap(v, u); n     p[v] = u; n   } n } n }  // namespace dsu n namespace mst { n inline ll get(int forced_edge) { n   dsu::clear(); n   ll answ = 0; n   if (forced_edge != NIL) { n     answ += edges[forced_edge].weight; n     dsu::merge(edges[forced_edge].v, edges[forced_edge].u); n   } n   for (auto edge : edges) { n     if (dsu::get(edge.v) != dsu::get(edge.u)) { n       answ += edge.weight; n       dsu::merge(edge.v, edge.u); n     } n   } n   return answ; n } n }  // namespace mst n inline bool check(ll M) { n   int forced = NIL; n   for (int i = 0; i < m; ++i) n     if (edges[i].id == 0) forced = i; n   edges[forced].weight = M; n   sort(edges.begin(), edges.end()); n   for (int i = 0; i < m; ++i) n     if (edges[i].id == 0) forced = i; n   ll mst_cost = mst::get(NIL); n   ll forced_cost = mst::get(forced); n   return mst_cost == forced_cost; n } n void solve() { n   cin >> n >> m; n   for (int i = 0; i < m; ++i) { n     int v, u; n     ll weight; n     cin >> v >> u >> weight; n     edges.emplace_back(v - 1, u - 1, weight, i); n   } n   ll L = 0, R = (ll)1e9 + 77; n   while (R - L > 1) { n     ll M = (L + R) / 2; n     if (check(M)) n       L = M; n     else n       R = M; n   } n   cout << min(L, (ll)1e9) <<   n ; n } n int main() { n   fast_io(); n   solve(); n   return 0; n } n


#include <bits/stdc++.h> n const double eps = (1e-10); n using namespace std; n int dcmp(double a, double b) { n   return fabs(a - b) <= eps ? 0 : (a > b) ? 1 : -1; n } n int getBit(int num, int idx) { return ((num >> idx) & 1) == 1; } n int setBit1(int num, int idx) { return num | (1 << idx); } n int setBit0(int num, int idx) { return num & ~(1 << idx); } n int flipBit(int num, int idx) { return num ^ (1 << idx); } n int countNumBit1(int mask) { n   int ret = 0; n   while (mask) { n     mask &= (mask - 1); n     ++ret; n   } n   return ret; n } n string str; n vector<string> v1; n vector<pair<string, int> > v; n map<string, int> vis; n int n, num, vs[100]; n void put(int vs[]) { n   string strr = str; n   for (int i = 0; i < n; i++) { n     if (!vs[i]) { n       strr[i] = (strr[i] ==  0  ?  1  :  0 ); n     } n   } n   v1.push_back(strr); n } n void fun(int st, int c) { n   if (c == num) { n     put(vs); n     return; n   } n   for (int i = st; i < n; i++) { n     vs[i] = 1; n     fun(i + 1, c + 1); n     vs[i] = 0; n   } n   return; n } n bool check(string s1, string s2, int sam) { n   int cnt = 0; n   for (int i = 0; i < ((int)(s1).size()); i++) { n     if (s1[i] == s2[i]) cnt++; n   } n   if (cnt == sam) return 1; n   return 0; n } n int main() { n   int m; n   scanf( %d %d , &n, &m); n   char stt[39]; n   string st; n   int rig; n   for (int i = 0; i < m; i++) { n     scanf( %s %d , &stt, &rig); n     st = stt; n     vis[st] = 1; n     v.push_back(make_pair(st, rig)); n   } n   str = v[0].first; n   num = v[0].second; n   fun(0ll, 0); n   long long ans = 0; n   for (int i = 0; i < ((int)(v1).size()); i++) { n     st = v1[i]; n     bool ok = 1; n     for (int j = 1; j < m && ok; j++) { n       if (!check(st, v[j].first, v[j].second)) ok = 0; n     } n     if (ok) { n       if (vis.find(v1[i]) == vis.end()) ans++; n     } n   } n   printf( %I64d n , ans); n } n


#include <bits/stdc++.h> n using namespace std; n void fastio() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n } n void one(); n int main() { n   fastio(); n   int t = 1; n   for (int i = 0; i < t; ++i) { n     one(); n   } n   return 0; n } n void one() { n   long long n; n   cin >> n; n   long long sum = n; n   for (long long i = 2; i * i <= n; i++) { n     while (n % i == 0) { n       n /= i; n       sum += n; n     } n   } n   if (n > 1) { n     sum++; n   } n   cout << sum << endl; n } n


#include <bits/stdc++.h> n using namespace std; n const long long INF = 1e9 + 7; n const long long N = 1e6 + 10; n char s[N]; n long long a[N]; n long long n, k; n int main() { n   long long i, len, j; n   long long tmp, flag, len_q = 0; n   scanf( %lld%lld , &n, &k); n   n++; n   for (i = 1; i <= n; i++) { n     scanf( %s , s); n     len = strlen(s); n     if (s[0] ==  ? ) { n       a[i] = INF; n       len_q++; n       continue; n     } n     flag = 0; n     tmp = 0; n     for (j = 0; j < len; j++) { n       if (s[j] ==  - ) n         flag = 1; n       else { n         tmp *= 10; n         tmp += s[j] -  0 ; n       } n     } n     if (flag) tmp = -tmp; n     a[i] = tmp; n   } n   if (k == 0) { n     if (a[1] == INF) { n       if ((n - len_q) % 2) { n         printf( YES n ); n         return 0; n       } else { n         printf( NO n ); n         return 0; n       } n     } else { n       if (a[1] == 0) { n         printf( YES n ); n         return 0; n       } else { n         printf( NO n ); n         return 0; n       } n     } n   } else { n     tmp = 0; n     if (len_q == 0) { n       for (i = n; abs(tmp) < 1e14 && i >= 1; i--) { n         tmp *= k; n         tmp += a[i]; n       } n       if (tmp == 0) n         printf( YES n ); n       else n         printf( NO n ); n     } else { n       if (n % 2 == 0) { n         printf( YES n ); n         return 0; n       } else { n         printf( NO n ); n         return 0; n       } n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <class T> n T CMP_MIN(T a, T b) { n   return a < b; n } n template <class T> n T CMP_MAX(T a, T b) { n   return a > b; n } n template <class T> n T MAX(T a, T b) { n   return a > b ? a : b; n } n template <class T> n T MIN(T a, T b) { n   return a < b ? a : b; n } n template <class T> n T GCD(T a, T b) { n   return b ? GCD(b, a % b) : a; n } n template <class T> n T LCM(T a, T b) { n   return a / GCD(a, b) * b; n } n const int MAXN = 110000; n const int MAXM = 1000006; n const double eps = 1e-10; n const long long MOD = 1000000007; n int n, k, l, r, sa, sk; n int main() { n   while (~scanf( %d %d %d %d %d %d , &n, &k, &l, &r, &sa, &sk)) { n     int p = sk % k, v = sk / k; n     printf( %d , v + (p ? 1 : 0)); n     for (int i = 1; i < k; i++) { n       printf(  %d , v + (i < p ? 1 : 0)); n     } n     if (k < n) { n       p = (sa - sk) % (n - k); n       v = (sa - sk) / (n - k); n       for (int i = 0; i < n - k; i++) { n         printf(  %d , v + (i < p ? 1 : 0)); n       } n     } n     printf(  n ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   int xs[200000], ys[200000]; n   cin >> n; n   for (int i = 0; i < n; i++) { n     cin >> xs[i] >> ys[i]; n   } n   map<int, int> vert, hor; n   for (int i = 0; i < n; i++) { n     int x, y; n     x = xs[i]; n     y = ys[i]; n     int v = x - y + 1; n     int h = y + x - 1; n     int buf; n     buf = vert[v]; n     vert[v] = buf + 1; n     buf = hor[h]; n     hor[h] = buf + 1; n   } n   long long res = 0; n   for (map<int, int>::iterator it = vert.begin(); it != vert.end(); ++it) { n     long long buf = (long long)it->second; n     buf--; n     long long sum = (buf * buf + buf) / 2; n     res += sum; n   } n   for (map<int, int>::iterator it = hor.begin(); it != hor.end(); ++it) { n     long long buf = (long long)it->second; n     buf--; n     long long sum = (buf * buf + buf) / 2; n     res += sum; n   } n   cout << res; n } n


#include <bits/stdc++.h> n int main() { n   long long int n, m, i, c = 0; n   scanf( %lld %lld , &n, &m); n   long long int a[m]; n   for (i = 0; i < m; i++) { n     scanf( %lld , &a[i]); n   } n   for (i = 0; i < m - 1; i++) { n     if (a[i + 1] > a[i]) { n       c = c + a[i + 1] - a[i]; n     } else if (a[i] == a[i + 1]) { n       c = c; n     } else { n       c = c + n + a[i + 1] - a[i]; n     } n   } n   c = a[0] + c - 1; n   printf( %lld , c); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n map<char, char> a; n int main() { n   string s1, s2; n   cin >> s1 >> s2; n   int dl = s1.size(); n   int k = 0; n   for (int i = 0; i < dl; i++) { n     char p1 = s1[i]; n     char p2 = s2[i]; n     map<char, char>::iterator it = a.find(p1); n     if (it == a.end()) { n       a[p1] = p2; n       if (p1 == p2) k++; n     } else if (it->second != p2) { n       cout << -1; n       exit(0); n     } n     it = a.find(p2); n     if (it == a.end()) { n       a[p2] = p1; n       if (p1 == p2) k++; n     } else if (it->second != p1) { n       cout << -1; n       exit(0); n     } n   } n   cout << (a.size() - k) / 2 <<   n ; n   for (map<char, char>::iterator it = a.begin(); it != a.end(); it++) { n     if (it->first >= it->second) continue; n     cout << it->first <<     << it->second <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long n, s; n   long long v[100005], a[100005], b[100005]; n   long long sum = 0; n   vector<pair<pair<long long, long long>, pair<long long, long long> > > vvv; n   scanf( %lld %lld , &n, &s); n   for (int i = 0; i < n; i++) { n     scanf( %lld %lld %lld , v + i, a + i, b + i); n     vvv.push_back( n         make_pair(make_pair(abs(a[i] - b[i]), v[i]), make_pair(a[i], b[i]))); n     sum += v[i]; n   } n   sort(vvv.begin(), vvv.end()); n   long long qtd = ceil((sum) / (double)s); n   long long ans = 0; n   long long qt1 = 0, qt2 = 0, tt = 0; n   for (int i = 0; i < n; i++) { n     if (vvv[i].second.first > vvv[i].second.second) { n       qt1 += vvv[i].first.second; n       ans += vvv[i].second.first * vvv[i].first.second; n     } else if (vvv[i].second.first < vvv[i].second.second) { n       qt2 += vvv[i].first.second; n       ans += vvv[i].second.second * vvv[i].first.second; n     } else { n       tt += vvv[i].first.second; n       ans += vvv[i].second.second * vvv[i].first.second; n     } n   } n   long long ans1 = ans, ans2 = ans; n   long long qtt1 = qt1 + tt, qtt2 = qt2; n   int id = 0; n   while (ceil((qtt1) / (double)s) + ceil((qtt2) / (double)s) > qtd) { n     if (vvv[id].second.first >= vvv[id].second.second) { n       if (vvv[id].first.second >= (qtt1) % s) { n         long long aux = (qtt1) % s; n         qtt1 -= aux; n         ans1 -= vvv[id].second.first * aux; n         qtt2 += aux; n         ans1 += vvv[id].second.second * aux; n         continue; n       } n       qtt1 -= vvv[id].first.second; n       ans1 -= vvv[id].second.first * vvv[id].first.second; n       qtt2 += vvv[id].first.second; n       ans1 += vvv[id].second.second * vvv[id].first.second; n     } n     id++; n   } n   id--; n   qtt1 = qt1; n   qtt2 = qt2 + tt; n   id = 0; n   while (ceil((qtt1) / (double)s) + ceil((qtt2) / (double)s) > qtd) { n     if (vvv[id].second.first <= vvv[id].second.second) { n       if (vvv[id].first.second >= (qtt2) % s) { n         long long aux = (qtt2) % s; n         qtt2 -= aux; n         ans2 -= vvv[id].second.second * aux; n         qtt1 += aux; n         ans2 += vvv[id].second.first * aux; n         continue; n       } n       qtt2 -= vvv[id].first.second; n       ans2 -= vvv[id].second.second * vvv[id].first.second; n       qtt1 += vvv[id].first.second; n       ans2 += vvv[id].second.first * vvv[id].first.second; n     } n     id++; n   } n   id--; n   printf( %lld n , max(ans1, ans2)); n   return 0; n } n


#include <bits/stdc++.h> n #pragma comment(linker,  /STACK:536870912 ) n #pragma warning(disable : 4996) n using namespace std; n const int N = 1e5 + 100; n map<int, vector<pair<int, int>>> mp; n pair<pair<int, int>, int> a[N]; n pair<pair<int, int>, pair<int, int>> d[N]; n int t[N]; n int main() { n   int n; n   scanf( %d , &n); n   for (int i = 0; i < n; i++) { n     scanf( %d%d , &a[i].first.first, &a[i].first.second); n     a[i].second = i; n   } n   sort(a, a + n); n   int m; n   scanf( %d , &m); n   for (int i = 0; i < m; i++) { n     scanf( %d%d%d , &d[i].first.first, &d[i].first.second, &d[i].second.first); n     d[i].second.second = i; n   } n   sort(d, d + m); n   int j = 0; n   for (int i = 0; i < n; i++) { n     while (j < m && d[j].first.first <= a[i].first.first) { n       mp[d[j].first.second].push_back({d[j].second.second, d[j].second.first}); n       j++; n     } n     auto it = mp.lower_bound(a[i].first.second); n     if (it == mp.end()) { n       printf( NO ); n       return 0; n     } n     it->second.back().second--; n     t[a[i].second] = it->second.back().first + 1; n     if (it->second.back().second == 0) { n       it->second.pop_back(); n       if (it->second.empty()) mp.erase(it); n     } n   } n   printf( YES n ); n   for (int i = 0; i < n; i++) printf( %d  , t[i]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int m = -2000000000, n = 2000000000; n int main() { n   char useless[100]; n   int t; n   cin >> t; n   getchar(); n   while (t--) { n     char a, c; n     int k; n     char o; n     bool flag = true; n     scanf( %c%c , &a, &c); n     if (c ==  = ) flag = false; n     scanf( %d , &k); n     scanf( %c%c , &o, &o); n     gets(useless); n     if (o ==  Y ) { n       if (a ==  > ) n         m = max(m, k + flag); n       else n         n = min(n, k - flag); n     } else { n       flag = !flag; n       if (a ==  < ) n         m = max(m, k + flag); n       else n         n = min(n, k - flag); n     } n   } n   if (m > n) n     cout <<  Impossible ; n   else n     cout << m; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void solve() { n   int N, M; n   string K; n   while (cin >> N >> M >> K) { n     K = K.substr(2); n     stringstream ss(K); n     int k; n     ss >> k; n     map<string, int> inp; n     for (int i = 0; i < (int)(N); i++) { n       string name; n       int level; n       cin >> name >> level; n       level = (level * k) / 100; n       if (level >= 100) { n         inp.insert(make_pair(name, level)); n       } n     } n     for (int i = 0; i < (int)(M); i++) { n       string name; n       cin >> name; n       if (inp.count(name) == 0) inp[name] = 0; n     } n     cout << inp.size() << endl; n     for (map<string, int>::iterator it = inp.begin(); it != inp.end(); it++) { n       cout << it->first <<     << it->second << endl; n     } n   } n } n int main() { n   solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int compare(const void *a, const void *b) { n   int *p = (int *)a; n   int *q = (int *)b; n   if (p[0] == q[0]) return p[1] - q[1]; n   return p[0] - q[0]; n } n int main() { n   int n, m, c = 0, t, i, x; n   scanf( %d%d , &n, &m); n   ; n   t = 0; n   for (i = 0; i < n; i++) { n     scanf( %d , &x); n     ; n     if (x > t) { n       c++; n       t = m - x; n     } else n       t -= x; n   } n   printf( %d n , c); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const double PI = 3.14159265; n int main() { n   ios::sync_with_stdio(false); n   int t; n   cin >> t; n   while (t--) { n     int n; n     cin >> n; n     cout << fixed << setprecision(7) << (cos(PI / (4 * n)) / sin(PI / (2 * n))) n          << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 100010; n int t, n, cnt = 0, ans = 0, tot = 0, num[maxn] = {}, zz[maxn] = {}, n           b[maxn] = {}; n vector<int> k[maxn]; n int powmod(int a, int b) { n   int ans = 1; n   for (; b; b >>= 1, a = a * a) n     if (b & 1) ans = ans * a; n   return ans; n } n int gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); } n void div(int n) { n   int m = n; n   for (int i = 2; i * i <= n; i++) { n     if (m % i == 0) { n       zz[++cnt] = i, num[cnt] = 0; n       while (m % i == 0) m /= i, num[cnt]++; n     } n   } n   if (m > 1) zz[++cnt] = m, num[cnt] = 1; n } n void dfs(int x, int y, int z) { n   if (x > cnt) { n     if (y != 1) k[z].push_back(y); n     return; n   } n   dfs(x + 1, y, z); n   if (z == 0) z = x; n   for (int i = 1; i <= num[x]; i++) dfs(x + 1, y * powmod(zz[x], i), z); n } n void calc() { n   for (int i = 1; i < tot; i++) n     if (gcd(b[i], b[i + 1]) == 1) ans++; n   if (gcd(b[tot], b[1]) == 1) ans++; n } n int main() { n   scanf( %d , &t); n   while (t--) { n     ans = tot = cnt = 0; n     scanf( %d , &n); n     div(n); n     for (int i = 1; i <= cnt; i++) k[i].clear(); n     dfs(1, 1, 0); n     for (int i = 1; i <= cnt; i++) { n       int x = -1, y = -1; n       for (int j = 0; j < k[i].size(); j++) { n         if (k[i][j] % zz[((i + cnt - 2) % cnt + cnt) % cnt + 1] == 0) { n           x = k[i][j], k[i].erase(k[i].begin() + j); n           break; n         } n       } n       for (int j = 0; j < k[i].size(); j++) { n         if (k[i][j] % zz[i % cnt + 1] == 0) { n           y = k[i][j], k[i].erase(k[i].begin() + j); n           break; n         } n       } n       if (x != -1) b[++tot] = x; n       for (int j = 0; j < k[i].size(); j++) b[++tot] = k[i][j]; n       if (y != -1) b[++tot] = y; n     } n     for (int i = 1; i <= tot; i++) printf( %d  , b[i]); n     calc(); n     printf(  n%d n , ans); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n inline long long read() { n   long long x = 0, f = 1; n   char ch = getchar(); n   while (!isdigit(ch)) { n     if (ch ==  - ) f = -1; n     ch = getchar(); n   } n   while (isdigit(ch)) { n     x = x * 10 + ch - 48; n     ch = getchar(); n   } n   return x * f; n } n long long n, k; n long long st[200001]; n struct aa { n   long long u, v; n   long long next; n } edge[200001 * 4]; n long long head[200001]; n long long cnt = 0; n void add_edge(long long u, long long v) { n   cnt++; n   edge[cnt].u = u; n   edge[cnt].v = v; n   edge[cnt].next = head[u]; n   head[u] = cnt; n } n long long root = 0; n long long f[200001][29]; n long long son[200001]; n long long size[200001]; n long long depth[200001]; n long long ed[200001]; n long long tot = 0; n long long id[200001]; n long long top[200001]; n void dfs1(long long u, long long fa) { n   f[u][0] = fa; n   for (long long i = 1; i <= 21; i++) { n     f[u][i] = f[f[u][i - 1]][i - 1]; n   } n   depth[u] = depth[fa] + 1; n   size[u] = 1; n   for (long long i = head[u]; i; i = edge[i].next) { n     long long v = edge[i].v; n     if (v != fa) { n       dfs1(v, u); n       if (size[son[u]] < size[v]) { n         son[u] = v; n       } n       size[u] += size[v]; n     } n   } n } n void dfs2(long long u, long long tp) { n   tot++; n   id[u] = tot; n   ed[tot] = st[u]; n   top[u] = tp; n   if (son[u]) { n     dfs2(son[u], tp); n     for (long long i = head[u]; i; i = edge[i].next) { n       long long v = edge[i].v; n       if (v != son[u] && v != f[u][0]) { n         dfs2(v, v); n       } n     } n   } n } n struct qw { n   long long l, r, val; n   long long add; n } a[1600001]; n void up(long long i) { a[i].val = a[i * 2].val + a[i * 2 + 1].val; } n void down(long long i) { n   long long x = a[i].add; n   a[i * 2].add += x; n   a[i * 2 + 1].add += x; n   a[i * 2].val += (a[i * 2].r - a[i * 2].l + 1) * x; n   a[i * 2 + 1].val += (a[i * 2 + 1].r - a[i * 2 + 1].l + 1) * x; n   a[i].add = 0; n } n void build(long long i, long long l, long long r) { n   a[i].l = l; n   a[i].r = r; n   if (l == r) { n     a[i].val = ed[a[i].l]; n     return; n   } n   long long mid = (l + r) / 2; n   build(i * 2, l, mid); n   build(i * 2 + 1, mid + 1, r); n   up(i); n } n void update(long long i, long long l, long long r, long long k) { n   if (a[i].l == l && a[i].r == r) { n     a[i].add += k; n     a[i].val += (a[i].r - a[i].l + 1) * k; n     return; n   } n   down(i); n   long long mid = (a[i].l + a[i].r) / 2; n   if (mid >= r) { n     update(i * 2, l, r, k); n   } else { n     if (mid < l) { n       update(i * 2 + 1, l, r, k); n     } else { n       update(i * 2, l, mid, k); n       update(i * 2 + 1, mid + 1, r, k); n     } n   } n   up(i); n } n long long query(long long i, long long l, long long r) { n   if (a[i].l == l && a[i].r == r) { n     return a[i].val; n   } n   down(i); n   long long mid = (a[i].l + a[i].r) / 2; n   if (mid >= r) { n     return query(i * 2, l, r); n   } else { n     if (mid < l) { n       return query(i * 2 + 1, l, r); n     } else { n       return (query(i * 2, l, mid) + query(i * 2 + 1, mid + 1, r)); n     } n   } n } n long long find(long long u, long long to) { n   for (long long i = 21; i >= 0; i--) { n     if (depth[f[u][i]] > depth[to]) { n       u = f[u][i]; n     } n   } n   return u; n } n long long lca(long long x, long long y) { n   if (depth[x] < depth[y]) { n     swap(x, y); n   } n   while (depth[x] > depth[y]) { n     x = f[x][(long long)log2(depth[x] - depth[y])]; n   } n   if (x == y) { n     return x; n   } n   for (long long i = 21; i >= 0; i--) { n     if (f[x][i] != f[y][i]) { n       x = f[x][i]; n       y = f[y][i]; n     } n   } n   return f[x][0]; n } n long long lcaq(long long x, long long y) { n   long long v1 = lca(x, y); n   long long v2 = lca(x, root); n   long long v3 = lca(y, root); n   if (depth[v1] < depth[v2]) { n     v1 = v2; n   } n   if (depth[v1] < depth[v3]) { n     v1 = v3; n   } n   return v1; n } n signed main() { n   cin >> n >> k; n   for (long long i = 1; i <= n; i++) { n     cin >> st[i]; n   } n   for (long long i = 1; i <= n - 1; i++) { n     long long u, v; n     cin >> u >> v; n     add_edge(u, v); n     add_edge(v, u); n   } n   root = 1; n   dfs1(1, 0); n   dfs2(1, 1); n   build(1, 1, n); n   for (long long i = 1; i <= k; i++) { n     long long opt; n     opt = read(); n     if (opt == 1) { n       long long v; n       cin >> v; n       root = v; n     } n     if (opt == 2) { n       long long a, b, k; n       cin >> a >> b >> k; n       long long x = lcaq(a, b); n       if (x == root) { n         update(1, 1, n, k); n       } else { n         if (id[root] >= id[x] && id[root] <= id[x] + size[x] - 1) { n           long long at = find(root, x); n           update(1, 1, n, k); n           update(1, id[at], id[at] + size[at] - 1, -k); n         } else { n           update(1, id[x], id[x] + size[x] - 1, k); n         } n       } n     } n     if (opt == 3) { n       long long x; n       cin >> x; n       if (x == root) { n         cout << query(1, 1, n) << endl; n         ; n       } else { n         if (id[root] >= id[x] && id[root] <= id[x] + size[x] - 1) { n           long long at = find(root, x); n           long long val = n               query(1, 1, n) - query(1, id[at], id[at] + size[at] - 1); n           cout << val << endl; n         } else { n           cout << query(1, id[x], id[x] + size[x] - 1) << endl; n           ; n         } n       } n     } n   } n   return 0; n } n