#include <bits/stdc++.h> n using namespace std; n const double pi = acos(-1.0); n const int inf = (int)1e9; n const long long int infll = (long long int)1e18; n const double inf1 = 1e-9; n const int ss = (int)1e6 + 3; n const int base = inf; n bool pred(const pair<int, int>& i, const pair<int, int>& j) { n   if (i.first == j.first) n     return i.second > j.second; n   else n     return i.first > j.first; n } n bool pred2(const int& i, const int& j) { return i > j; } n int main() { n   int n; n   scanf( %d , &n); n   vector<int> a(n); n   int k = 100001; n   vector<vector<int> > b(k); n   int mx = -1; n   for (int i = 0; i < n; ++i) { n     scanf( %d , &a[i]); n     b[a[i]].push_back(i); n     mx = max(a[i], mx); n   } n   if (mx == 1) { n     cout << b[1].size() << endl; n     for (int i = 0; i < n; ++i) cout << i + 1 <<    ; n     return 0; n   } n   bool t = true; n   for (int j = 2; j <= mx; ++j) { n     if (b[j].size() > b[j - 1].size()) { n       t = false; n       break; n     } n   } n   if (!t) { n     cout <<  -1 ; n     return 0; n   } n   cout << b[1].size() << endl; n   vector<int> c(n); n   for (int i = 1; i <= mx; ++i) { n     for (int j = 0; j < b[i].size(); ++j) c[b[i][j]] = j + 1; n   } n   for (int i = 0; i < n; ++i) cout << c[i] <<    ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int x, y, a, b; n   scanf( %d %d %d %d , &x, &y, &a, &b); n   int minV = max(a, b + 1); n   int maxP = min(y, x - 1); n   vector<pair<int, int> > output; n   int total = 0; n   for (int v = minV; v <= x; v++) { n     for (int p = b; p <= min(v - 1, maxP); p++) { n       output.push_back(pair<int, int>(v, p)); n       ++total; n     } n   } n   printf( %d n , total); n   for (int k = 0; k < output.size(); k++) { n     printf( %d %d n , output[k].first, output[k].second); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long A[1 << 20]; n long long B[1 << 20]; n long long getIP() { n   int a[4]; n   scanf( %d.%d.%d.%d , &a[0], &a[1], &a[2], &a[3]); n   int i; n   long long res = 0; n   for (i = (0); i < (4); ++i) res = (res << 8) | a[i]; n   return res; n } n void printIP(long long res) { n   int a[4]; n   int i; n   for (i = (0); i < (4); ++i) { n     a[i] = int(res & 255); n     res >>= 8; n   } n   printf( %d.%d.%d.%d n , a[3], a[2], a[1], a[0]); n } n int main() { n   int n, k; n   scanf( %d%d , &n, &k); n   int i, j; n   for (i = (0); i < (n); ++i) A[i] = getIP(); n   long long mask = 0; n   for (i = (32) - 1; i >= (1); --i) { n     if (i != 32) mask |= 1LL << i; n     for (j = (0); j < (n); ++j) B[j] = A[j] & mask; n     sort(B, B + n); n     if (k == unique(B, B + n) - B) { n       printIP(mask); n       return 0; n     } n   } n   printf( -1 n ); n   return 0; n }; n


#include <bits/stdc++.h> n #pragma comment(linker,  /stack:200000000 ) n #pragma GCC optimize( Ofast ) n #pragma GCC target( sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native ) n using namespace std; n const int mod = 1e9 + 7; n const int N = 1e6 + 10; n const long long INF = 1e18; n const long double EPS = 1e-12; n int d, s; n bool vis[5105][515]; n int pa[5105][515]; n int parem[5105][515]; n vector<int> ans; n vector<pair<int, vector<int> > > fin; n void go(int sum, int rem, int dig, int len) { n   if (len == 0) { n     reverse(ans.begin(), ans.end()); n     fin.push_back(make_pair(ans.size(), ans)); n     return; n   } n   ans.push_back(dig); n   int dd = pa[sum][rem]; n   int remm = parem[sum][rem]; n   go(sum - dig, remm, dd, len - 1); n } n queue<vector<int> > qu; n void solve(int dii) { n   while (not qu.empty()) qu.pop(); n   vis[dii][dii % d] = 1; n   qu.push({dii, dii % d, dii, 0}); n   while (not qu.empty()) { n     auto vec = qu.front(); n     qu.pop(); n     int sum = vec[0], rem = vec[1], dig = vec[2], len = vec[3]; n     if (sum > s) continue; n     if (sum > 5001) continue; n     if (len > 50001) continue; n     for (int i = 0; i <= 9; i++) { n       if (sum == 0 && i == 0) continue; n       int sumi = sum + i; n       int remi = (rem * 10 + i) % d; n       if (not vis[sumi][remi]) { n         pa[sumi][remi] = dig; n         parem[sumi][remi] = rem; n         vis[sumi][remi] = 1; n         qu.push({sumi, remi, i, len + 1}); n       } n       if (sumi == s && remi == 0) { n         ans.clear(); n         go(sumi, remi, i, len + 1); n         return; n       } n     } n   } n } n int main() { n   scanf( %d%d , &d, &s); n   vis[0][0] = 1; n   solve(0); n   if (not fin.empty()) { n     sort(fin.begin(), fin.end()); n     for (int i = 0; i < fin[0].first; i++) { n       printf( %d , fin[0].second[i]); n     } n     printf(  n ); n     return 0; n   } n   cout <<  -1  << endl; n   return 0; n } n


#include <bits/stdc++.h> n int main() { n   int x, y, xlast, ylast, n, k, i; n   double sum = 0.0f; n   scanf( %d%d , &n, &k); n   scanf( %d%d , &xlast, &ylast); n   for (i = 0; i < n - 1; i++) { n     scanf( %d%d , &x, &y); n     sum += sqrt(pow((double)x - xlast, 2) + pow((double)y - ylast, 2)); n     xlast = x; n     ylast = y; n   } n   printf( %0.9lf , sum / 50 * k); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long md = 1000000007; n long long ans, f[2002][2002]; n int n, m; n inline void solve() { n   scanf( %d%d , &n, &m); n   for (register int i = 1; i <= m; ++i) f[1][i] = 1; n   for (register int i = 2; i <= n; ++i) { n     int t = 0; n     f[i][1] = 1; n     for (register int j = 2; j <= m; ++j) { n       (t += f[i - 1][j]) %= md; n       f[i][j] = (f[i][j - 1] + t) % md; n     } n   } n   for (register int i = 1; i <= n; ++i) n     for (register int j = 2; j <= m; ++j) n       (ans += (m - j + 1) * (f[i][j] - f[i - 1][j] + md) % md * n               f[n - i + 1][j] % md) %= md; n   printf( %lld n , ans); n } n int main() { n   ios_base::sync_with_stdio(false); n   int T = 1; n   for (; T--;) solve(); n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   int n; n   cin >> n; n   string s; n   cin >> s; n   vector<int> vec(n); n   vector<int> kactane(30, 0); n   vector<int> harfmax(30, 0); n   int res = 0; n   for (int i = 0; i < n; i++) { n     int cnt = 0; n     kactane[s[i] -  a ]++; n     for (int u = s[i] -  a  + 1; u <=  z  -  a ; u++) { n       if (kactane[u]) { n         cnt = max(cnt, harfmax[u]); n       } n     } n     harfmax[s[i] -  a ] = max(harfmax[s[i] -  a ], cnt + 1); n     vec[i] = cnt; n     res = max(res, vec[i]); n     if (res > 1) { n       cout <<  NO ; n       return 0; n     } n   } n   cout <<  YES n ; n   for (auto& x : vec) cout << x; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   string str; n   cin >> str; n   if (str.size() % 2 == 1) { n     cout <<  No  << endl; n     return 0; n   } n   stack<char> sta; n   for (int i = 0; i < str.size(); i++) { n     if (!sta.empty() && sta.top() == str[i]) n       sta.pop(); n     else n       sta.push(str[i]); n   } n   puts(sta.empty() ?  Yes  :  No ); n } n


#include <bits/stdc++.h> n using namespace std; n const double eps = 1.0e-11; n const double pi = acos(-1.0); n int main() { n   string s; n   cin >> s; n   int n = s.size(); n   vector<int> points; n   for (long long i = 0; i < (int)(s).size(); i++) { n     if (s[i] ==  . ) { n       points.push_back(i); n     } n   } n   if (points.empty() || points.front() == 0 || points.front() > 8 || n       points.back() == n - 1 || n - 1 - points.back() > 3) { n     cout <<  NO n ; n     return 0; n   } n   vector<bool> sep((int)(s).size(), false); n   for (long long i = 1; i <= (int)(points).size() - 1; i++) { n     if (points[i] - points[i - 1] - 1 < 2 || n         points[i] - points[i - 1] - 1 > 11) { n       cout <<  NO n ; n       return 0; n     } n     int d = points[i] - points[i - 1] - 1; n     if (d > 8) { n       sep[points[i] - 8] = true; n       continue; n     } n     sep[points[i - 1] + 2] = true; n   } n   cout <<  YES n ; n   for (long long i = 0; i < (int)(s).size(); i++) { n     if (sep[i]) { n       cout << endl; n     } n     cout << s[i]; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n using ll = long long; n using pii = pair<int, int>; n template <typename T, typename S> n ostream& operator<<(ostream& os, pair<T, S> a) { n   os <<  (  << a.first <<  ,  << a.second <<  ) ; n   return os; n } n template <typename T> n ostream& operator<<(ostream& os, vector<T> v) { n   for (auto x : v) os << x <<    ; n   return os; n } n void debug() { cerr <<   n ; } n template <typename H, typename... T> n void debug(H a, T... b) { n   cerr << a; n   if (sizeof...(b)) cerr <<  ,  ; n   debug(b...); n } n vector<bool> get_prime(int n) { n   vector<bool> prime(n + 1, true); n   if (n >= 0) prime[0] = false; n   if (n >= 1) prime[1] = false; n   for (int i = 2; i * i <= n; i++) { n     if (prime[i]) { n       for (int j = i + i; j <= n; j += i) prime[j] = false; n     } n   } n   return (prime); n } n int main() { n   cin.tie(nullptr); n   ios::sync_with_stdio(false); n   auto p = get_prime(2000000); n   vector<ll> d; n   for (int i = 0; i < (int)(1000000); ++i) n     if (p[i]) d.push_back(i); n   int t; n   cin >> t; n   while (t--) { n     int n; n     cin >> n; n     ll a = sqrt(n); n     int ans2 = 1; n     ans2 += n         upper_bound(d.begin(), d.end(), n) - lower_bound(d.begin(), d.end(), a); n     if (p[a]) ans2--; n     cout << ans2 <<   n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int MAX_N = 1050; n const int MAX_M = 1050; n int n, m, q; n int matrix[MAX_N][MAX_M]; n int l[MAX_M][MAX_N]; n int r[MAX_M][MAX_N]; n int u[MAX_N][MAX_M]; n int d[MAX_N][MAX_M]; n void modify(int x, int y) { n   matrix[x][y] ^= 1; n   for (int j = 1; j <= m; j++) { n     if (matrix[x][j] == 1) { n       l[j][x] = l[j - 1][x] + 1; n     } else { n       l[j][x] = 0; n     } n   } n   for (int j = m; j >= 1; j--) { n     if (matrix[x][j] == 1) { n       r[j][x] = r[j + 1][x] + 1; n     } else { n       r[j][x] = 0; n     } n   } n   for (int i = 1; i <= n; i++) { n     if (matrix[i][y] == 1) { n       u[i][y] = u[i - 1][y] + 1; n     } else { n       u[i][y] = 0; n     } n   } n   for (int i = n; i >= 1; i--) { n     if (matrix[i][y] == 1) { n       d[i][y] = d[i + 1][y] + 1; n     } else { n       d[i][y] = 0; n     } n   } n } n int query(int f[], int pos, int bound) { n   int l = pos; n   int r = pos; n   int ret = f[pos]; n   int min_height = f[pos]; n   while (l > 1 || r < bound) { n     if (r == bound || f[l - 1] > f[r + 1]) { n       l--; n       min_height = min(min_height, f[l]); n     } else { n       r++; n       min_height = min(min_height, f[r]); n     } n     ret = max(ret, (r - l + 1) * min_height); n   } n   return ret; n } n int main() { n   scanf( %d%d%d , &n, &m, &q); n   for (int i = 1; i <= n; i++) n     for (int j = 1; j <= m; j++) scanf( %d , matrix[i] + j); n   for (int i = 1; i <= n; i++) n     for (int j = 1; j <= m; j++) { n       if (matrix[i][j] == 1) { n         l[j][i] = l[j - 1][i] + 1; n         u[i][j] = u[i - 1][j] + 1; n       } else { n         l[j][i] = u[i][j] = 0; n       } n     } n   for (int i = n; i >= 1; i--) n     for (int j = m; j >= 1; j--) { n       if (matrix[i][j] == 1) { n         r[j][i] = r[j + 1][i] + 1; n         d[i][j] = d[i + 1][j] + 1; n       } else { n         d[i][j] = r[j][i] = 0; n       } n     } n   while (q--) { n     int a, x, y; n     scanf( %d%d%d , &a, &x, &y); n     if (a == 1) { n       modify(x, y); n       continue; n     } n     int ans = 0; n     ans = max(ans, query(l[y], x, n)); n     ans = max(ans, query(r[y], x, n)); n     ans = max(ans, query(u[x], y, m)); n     ans = max(ans, query(d[x], y, m)); n     printf( %d n , ans); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int i, N, minim, maxim; n   long long M; n   scanf( %d %I64d , &N, &M); n   maxim = 0; n   if (2 * M < N) n     minim = N - 2 * M; n   else n     minim = 0; n   if (M == 0) n     maxim = N; n   else n     for (i = 1; i <= N; ++i) n       if (1LL * i * (i - 1) / 2 >= M) { n         maxim = N - i; n         break; n       } n   printf( %d %d n , minim, maxim); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long pred[300005]; n long long suffc[300005]; n long long ans[300005]; n int main() { n   int n, m; n   scanf( %d %d , &n, &m); n   pair<int, int> ara[n]; n   vector<pair<int, int> > vt(n); n   for (int i = 0; i < n; i++) { n     scanf( %d %d , &ara[i].first, &ara[i].second); n     vt[i] = {ara[i].second - ara[i].first, i}; n   } n   sort(vt.begin(), vt.end()); n   pred[0] = ara[vt[0].second].second; n   for (int i = 1; i < n; i++) { n     pred[i] = pred[i - 1] + ara[vt[i].second].second; n   } n   suffc[n - 1] = ara[vt[n - 1].second].first; n   for (int i = n - 2; i >= 0; i--) { n     suffc[i] = suffc[i + 1] + ara[vt[i].second].first; n   } n   for (int i = 0; i < n; i++) { n     int pos = upper_bound(vt.begin(), vt.end(), n                           make_pair(ara[i].second - ara[i].first, n + 1)) - n               vt.begin(); n     ans[i] += 1ll * suffc[pos] + 1ll * (n - pos) * ara[i].second; n     pos--; n     if (pos >= 0) n       ans[i] += 1ll * pred[pos] + 1ll * (pos + 1) * ara[i].first, n           ans[i] -= ara[i].first + ara[i].second; n     if (i == 0) { n     } n   } n   for (int i = 0; i < m; i++) { n     int u, v; n     scanf( %d %d , &u, &v); n     u--, v--; n     ans[u] -= min(ara[u].first + ara[v].second, ara[u].second + ara[v].first); n     ans[v] -= min(ara[u].first + ara[v].second, ara[u].second + ara[v].first); n   } n   for (int i = 0; i < n; i++) printf( %lld  , ans[i]); n   return 0; n } n


#include <bits/stdc++.h> n int main() { n   long long int n, i, p = 1, k, x = 0, s = 0; n   scanf( %lld , &n); n   k = n; n   while (k != 0) { n     k = k / 10; n     x++; n   } n   for (i = 1; i <= x - 1; i++) { n     p = p * 10; n   } n   while (x != 0) { n     s = s + (n - p + 1) * x; n     n = p - 1; n     p = p / 10; n     x--; n   } n   printf( %lld , s); n } n


#include <bits/stdc++.h> n using namespace std; n deque<int> d[100005]; n int v[100005]; n int p[100005]; n pair<int, int> q[100005]; n int ans = 0; n int prog = 0; n int as[100005]; n unordered_map<int, int> possible[100005]; n bool cmp(int a, int b) { n   if (q[a].first / 300 != q[b].first / 300) n     return q[a].first / 300 < q[b].first / 300; n   return q[a].second < q[b].second; n } n void update(int val, int pos, int u, int c) { n   if (c == -1) { n     prog -= possible[val].size() <= 1; n     if (u == 1) { n       int cnt = d[val].front(); n       d[val].pop_front(); n       if (d[val].empty()) n         --ans; n       else { n         --possible[val][d[val].front() - cnt]; n         if (!possible[val][d[val].front() - cnt]) n           possible[val].erase(d[val].front() - cnt); n       } n     } else { n       int cnt = d[val].back(); n       d[val].pop_back(); n       if (d[val].empty()) n         --ans; n       else { n         --possible[val][cnt - d[val].back()]; n         if (!possible[val][cnt - d[val].back()]) n           possible[val].erase(cnt - d[val].back()); n       } n     } n     if (!d[val].empty()) prog += possible[val].size() <= 1; n   } else { n     if (d[val].empty()) n       ++ans; n     else n       prog -= possible[val].size() <= 1; n     if (u == 1) { n       if (d[val].empty()) { n         d[val].push_front(pos); n       } else { n         int cnt = d[val].front(); n         d[val].push_front(pos); n         ++possible[val][cnt - pos]; n       } n     } else { n       if (d[val].empty()) { n         d[val].push_back(pos); n       } else { n         int cnt = d[val].back(); n         d[val].push_back(pos); n         ++possible[val][pos - cnt]; n       } n     } n     if (possible[val].size() <= 1) ++prog; n   } n } n int main(void) { n   int n, m; n   ios_base ::sync_with_stdio(0); n   cin >> n; n   for (int i = 1; i <= n; ++i) cin >> v[i]; n   cin >> m; n   for (int i = 1; i <= m; ++i) cin >> q[i].first >> q[i].second, p[i] = i; n   sort(p + 1, p + 1 + m, cmp); n   for (int i = q[p[1]].first; i <= q[p[1]].second; ++i) update(v[i], i, 1, 1); n   as[p[1]] = ans + !prog; n   int l = q[p[1]].first, r = q[p[1]].second; n   for (int i = 2; i <= m; ++i) { n     while (l > q[p[i]].first) --l, update(v[l], l, -1, 1); n     while (r < q[p[i]].second) ++r, update(v[r], r, 1, 1); n     while (l < q[p[i]].first) update(v[l], l, -1, -1), ++l; n     while (r > q[p[i]].second) update(v[r], r, 1, -1), --r; n     as[p[i]] = ans + !prog; n   } n   for (int i = 1; i <= m; ++i) cout << as[i] <<   n ; n   return cout <<   n , 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct tc { n   int cnt, count; n   long long p[5001]; n } q[5001]; n struct sc { n   int id; n   long long num; n } anss[200010], s[200010]; n void merge(int l, int mid, int r) { n   int i = l, j = mid + 1; n   int cc = l; n   while (i <= mid && j <= r) { n     if (anss[i].num <= anss[j].num) { n       s[cc].id = anss[i].id; n       s[cc++].num = anss[i++].num; n     } else { n       s[cc].id = anss[j].id; n       s[cc++].num = anss[j++].num; n     } n   } n   while (i <= mid) { n     s[cc].id = anss[i].id; n     s[cc++].num = anss[i++].num; n   } n   while (j <= r) { n     s[cc].id = anss[j].id; n     s[cc++].num = anss[j++].num; n   } n   for (i = l; i < cc; i++) { n     anss[i].id = s[i].id; n     anss[i].num = s[i].num; n   } n   return; n } n void mergesort(int x, int y) { n   if (x < y) { n     int mid = (x + y) / 2; n     mergesort(x, mid); n     mergesort(mid + 1, y); n     merge(x, mid, y); n   } n   return; n } n int main() { n   int i, j; n   long long n, a, x, y, m; n   int ans = -1, k, sum, l = 0; n   cin >> n; n   for (i = 0; i < n; i++) { n     scanf( %d %I64d %I64d %I64d %I64d , &k, &a, &x, &y, &m); n     sum = 0; n     l += k; n     q[i].cnt = 1; n     q[i].count = k; n     q[i].p[1] = a; n     for (j = 2; j <= k; j++) { n       q[i].p[j] = (x * q[i].p[j - 1] + y) % m; n       if (q[i].p[j] < q[i].p[j - 1]) sum++; n     } n     ans = max(ans, sum); n   } n   printf( %d n , ans); n   int st = 0, end = 0; n   if (l <= 2e5) { n     while (end < l) { n       for (i = 0; i < n; i++) { n         for (j = q[i].cnt; j <= q[i].count; j++) { n           if (j != q[i].cnt && q[i].p[j] < q[i].p[j - 1]) { n             q[i].cnt = j; n             break; n           } n           anss[end].id = i; n           anss[end++].num = q[i].p[j]; n         } n         if (j > q[i].count) q[i].cnt = j; n       } n       mergesort(st, end - 1); n       st = end; n     } n     for (i = 0; i < l; i++) printf( %I64d %d n , anss[i].num, anss[i].id + 1); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int32_t main() { n   long long t; n   cin >> t; n   while (t--) { n     long long n; n     cin >> n; n     vector<long long> p(n), m(n); n     vector<vector<long long>> v(n + 1); n     for (long long i = 0; i < n; i++) { n       cin >> m[i] >> p[i]; n       v[m[i]].push_back(i); n     } n     vector<long long> sum(n + 1); n     sum[0] = v[0].size(); n     for (long long i = 1; i < n + 1; i++) { n       sum[i] = sum[i - 1] + v[i].size(); n     } n     long long kol = 0; n     long long ans = 0; n     multiset<long long> st; n     for (long long i = n; i > 0; i--) { n       for (long long j = 0; j < v[i].size(); j++) { n         st.insert(p[v[i][j]]); n       } n       while (kol + sum[i - 1] < i) { n         ans += *st.begin(); n         ++kol; n         st.erase(st.begin()); n       } n     } n     cout << ans <<   n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n bool check(int x, int n, vector<int>& a) { n   vector<int> temp; n   int i = 0, j = x; n   int cnt = 0; n   bool ok = false; n   while (cnt < n) { n     if (ok) n       temp.push_back(a[i]), i++, ok = false; n     else n       temp.push_back(a[j]), j++, ok = true; n     cnt++; n   } n   cnt = 0; n   return cnt == x; n } n void solve() { n   int n; n   cin >> n; n   vector<int> a(n); n   for (int i = 0; i < n; i++) cin >> a[i]; n   sort(a.begin(), a.end()); n   int cnt = 0; n   vector<int> ans(n); n   for (int i = 1; i < n; i += 2) { n     ans[i] = a[cnt++]; n   } n   for (int i = 0; i < n; i += 2) ans[i] = a[cnt++]; n   cnt = 0; n   for (int i = 1; i < n - 1; i++) { n     if (ans[i] < ans[i - 1] && ans[i] < ans[i + 1]) cnt++; n   } n   cout << cnt <<   n ; n   for (int i = 0; i < n; i++) cout << ans[i] <<    ; n } n int main() { n   ios::sync_with_stdio(0), cin.tie(0), cout.tie(0); n   ; n   int t = 1; n   while (t--) { n     solve(); n     cout <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n int main() { n   int tc; n   scanf( %d , &tc); n   while (tc--) { n     int n, i, z = -1, o = -1, s = 0, max = -1; n     scanf( %d , &n); n     char ss[n + 5], at[2]; n     scanf( %s , ss); n     int a[n], b[n], zero[n], one[n]; n     for (i = 0; i < n; i++) { n       at[0] = ss[i]; n       at[1] =   0 ; n       a[i] = atoi(at); n     } n     for (i = 0; i < n; i++) { n       if (!a[i]) { n         if (o >= 0) { n           zero[++z] = one[o--]; n           b[i] = zero[z]; n         } else { n           zero[++z] = ++s; n           b[i] = zero[z]; n         } n       } n       if (a[i]) { n         if (z >= 0) { n           one[++o] = zero[z--]; n           b[i] = one[o]; n         } else { n           one[++o] = ++s; n           b[i] = one[o]; n         } n       } n       if (max < b[i]) max = b[i]; n     } n     printf( %d n , max); n     for (i = 0; i < n; i++) printf( %d  , b[i]); n     printf(  n ); n   } n } n


#include <algorithm> n #include <iostream> n #include <cstdlib> n #include <cassert> n #include <cstring> n #include <iomanip> n #include <cstdio> n #include <limits> n #include <string> n #include <vector> n #include <cmath> n #include <deque> n #include <queue> n #include <stack> n #include <map> n #include <set> n using namespace std; n  n typedef long long LL; n typedef long long ll; n typedef long double LD; n typedef vector<int> vi; n typedef pair<LL,LL> PLL; n typedef pair<LL,int> PLI; n typedef pair<int,int> PII; n typedef pair<int,int> pii; n typedef vector<vector<LL>> VVL; n  n #define rep(i, a, b) for(int i = a; i < (b); ++i) n #define trav(a, x) for(auto& a : x) n #define all(x) begin(x), end(x) n #define sz(x) (int)(x).size() n #define popf pop_front n #define pf push_front n #define popb pop_back n #define mp make_pair n #define pb push_back n #define remove erase n #define fi first n #define se second n  n #define endl   n  n  n const int INF = 1061109567; n const LL LINF = 4557430888798830399LL; n const LL MOD = 1000000007; n const int dx[8] = {0,0,1,-1,1,-1,1,-1}; n const int dy[8] = {1,-1,0,0,1,-1,-1,1}; n const char dch[4] = { R , L , D , U }; n // const string ds[8] = { E , W , S , N , SE , NW , SW , NE }; n inline void fasterios(){ n     ios_base::sync_with_stdio(0); n     cin.tie(0); cout.tie(0); n } n  n  n // Don t forget to read all the inputs before returning!!! n int solve(){ n   int n; cin >> n; n   vector <PII> arr(n); n   rep(i,0,n){ n     cin >> arr[i].fi; n     arr[i].se = i; n   } n   sort(all(arr)); n   vector <int> tmp[2]; n   rep(i,0,n) if(abs(arr[i].se - i) % 2 == 1) tmp[i%2].pb(arr[i].fi); n   sort(all(tmp[0])); n   sort(all(tmp[1])); n   if(sz(tmp[0]) != sz(tmp[1])){ n     cout <<  NO  << endl; n     return 0; n   } n   for(int i = 0;i < sz(tmp[0]);i++){ n     if(tmp[0][i] != tmp[1][i]){ n       cout <<  NO  << endl; n       return 0; n     } n   } n   cout <<  YES  << endl; n   return 0; n } n  n int main(){ n   fasterios(); n   int testCases; cin >> testCases; int tc = 0; n   while(++tc <= testCases){ n     // cout <<  Case #  << tc <<  :  ; n     solve(); n   } n }


#include <bits/stdc++.h> n using namespace std; n const int N = 1e5 + 9; n long long a, b, n, dv[N], l, r, sz; n long long bs(long long l, long long r) { n   long long bas = 0; n   long long son = sz + 1; n   while (bas + 1 < son) { n     long long orta = bas + son >> 1; n     if (dv[orta] < l) n       bas = orta; n     else if (dv[orta] > r) n       son = orta; n     else if (dv[orta] <= r and dv[orta] >= l) n       bas = orta; n   } n   return (dv[bas] < l or dv[bas] > r) ? -1 : dv[bas]; n } n int main() { n   cin >> a >> b >> n; n   for (long long i = 1; i * i <= max(a, b); i++) { n     if (a % i == 0 and b % i == 0) { n       dv[++sz] = i; n       if (b % (a / i) == 0 and i * i != max(a, b)) dv[++sz] = a / i; n       if (a % (b / i) == 0) dv[++sz] = b / i; n     } else if (a % (b / i) == 0 and b % i == 0) n       dv[++sz] = b / i; n     else if (b % (a / i) == 0 and a % i == 0) n       dv[++sz] = a / i; n   } n   sort(dv + 1, dv + sz + 1); n   for (long long i = 1; i <= n; i++) { n     cin >> l >> r; n     cout << bs(l, r) << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int t, n, ans, i, j, k; n   cin >> t; n   for (i = 0; i < t; i++) { n     cin >> n; n     int x[2 * n]; n     for (j = 0; j < 2 * n; j++) cin >> x[j]; n     sort(x, x + 2 * n); n     ans = x[n] - x[n - 1]; n     cout << ans << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n const int INF = 1e9 + 7; n int ds[100]; n int hs[100]; n int solve() { n   int N, X; n   scanf( %d %d , &N, &X); n   for (int i = 0; i < N; i++) { n     scanf( %d %d , &ds[i], &hs[i]); n   } n   int last = *std::max_element(ds, ds + N); n   if (last >= X) return 1; n   int dec = 0; n   for (int i = 0; i < N; i++) { n     dec = std::max(dec, ds[i] - hs[i]); n   } n   if (dec == 0) return -1; n   return (X - last + dec - 1) / dec + 1; n } n int main() { n   int T; n   scanf( %d , &T); n   while (T--) { n     printf( %d n , solve()); n   } n } n


#include <bits/stdc++.h> n using namespace std; n struct s_ix { n   long long ix; n   long long val; n }; n bool operator<(s_ix aa, s_ix bb) { return aa.val - aa.ix < bb.val - bb.ix; } n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   long long T; n   cin >> T; n   while (T--) { n     long long n, k, nc[200200], sum, nbw, maxnbw; n     s_ix nb[200200]; n     string s[200200]; n     cin >> n; n     for (int j = 0; j < n; j++) cin >> s[j]; n     maxnbw = 0; n     for (int l = 0; l < 26; l++) { n       nbw = 0; n       for (int j = 0; j < n; j++) { n         nb[j].val = 0; n         nb[j].ix = 0; n         for (int jj = 0; jj < s[j].length(); jj++) { n           if ((int)(s[j][jj] -  a ) == l) n             nb[j].val++; n           else n             nb[j].ix++; n         } n       } n       sort(nb, nb + n); n       reverse(nb, nb + n); n       long long nl = 0; n       nl = nb[0].val - nb[0].ix; n       int j = 0; n       while (nl > 0 and j < n) { n         j++; n         nl += nb[j].val - nb[j].ix; n         nbw++; n       } n       maxnbw = max(maxnbw, nbw); n     } n     cout << maxnbw << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, s1 = 0, s2 = 0, s3 = 0; n   cin >> n; n   int a[n]; n   for (int i = 0; i < n; i++) { n     cin >> a[i]; n     if (i % 3 == 0) n       s1 += a[i]; n     else if (i % 3 == 1) n       s2 += a[i]; n     else n       s3 += a[i]; n   } n   if (s1 >= s2 && s1 >= s3) n     cout <<  chest n ; n   else if (s2 >= s3 && s2 >= s1) n     cout <<  biceps n ; n   else n     cout <<  back n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long strL[200005], patL[200005], n, m, pref[200005], ans, tempL[200005]; n char strC[200005], tempC[200005]; n vector<long long> endings; n string patC =   ; n long long lenP, lenS; n void calcPrefixFunction() { n   for (long long i = 1, k = 0; i < m - 2; i++) { n     while (k > 0 && (patC[k] != patC[i] || patL[k] != patL[i])) k = pref[k - 1]; n     if (patC[k] == patC[i] && patL[i] == patL[k]) n       pref[i] = ++k; n     else n       pref[i] = k; n   } n } n void kmp() { n   for (long long i = 0, k = 0; i < n; i++) { n     while (k > 0 && (strL[i] != patL[k] || strC[i] != patC[k])) k = pref[k - 1]; n     if (strL[i] == patL[k] && strC[i] == patC[k]) k++; n     if (k == m - 2) { n       endings.push_back(i); n       k = pref[k - 1]; n     } n   } n } n long long tempSolve1() { n   long long ret = 0; n   for (long long i = 0; i < n; i++) n     if (tempL[0] <= strL[i] && strC[i] == tempC[0]) n       ret += strL[i] - tempL[0] + 1; n   return ret; n } n long long tempSolve2() { n   long long ret = 0; n   for (long long i = 0; i < n - 1; i++) n     if (tempL[0] <= strL[i] && strC[i] == tempC[0]) n       if (tempL[1] <= strL[i + 1] && strC[i + 1] == tempC[1]) ret++; n   return ret; n } n void solve() { n   if (lenP > lenS) { n     puts( 0 ); n     return; n   } n   if (m == 1) { n     printf( %I64d , tempSolve1()); n     return; n   } n   if (m == 2) { n     printf( %d , tempSolve2()); n     return; n   } n   calcPrefixFunction(); n   kmp(); n   for (long long i = 0; i < endings.size(); i++) { n     long long start, end; n     end = endings[i] + 1; n     start = end - m + 1; n     if (end >= n || start < 0) continue; n     if (strC[start] == tempC[0] && strL[start] >= tempL[0]) n       if (strC[end] == tempC[m - 1] && strL[end] >= tempL[m - 1]) ans++; n   } n   printf( %d , ans); n } n void input() { n   scanf( %d %d , &n, &m); n   for (long long i = 0; i < n; i++) { n     scanf( %d-%c , &strL[i], &strC[i]); n     if (i && strC[i] == strC[i - 1]) { n       strL[i - 1] += strL[i]; n       n--; n       i--; n     } n     lenS += strL[i]; n   } n   for (long long i = 0; i < m; i++) { n     scanf( %d-%c , &tempL[i], &tempC[i]); n     lenP += tempL[i]; n     if (i && tempC[i] == tempC[i - 1]) { n       tempL[i - 1] += tempL[i]; n       if (i - 2 >= 0) patL[i - 2] = tempL[i - 1]; n       i--; n       m--; n     } else if (i && i < m - 1) n       patC += tempC[i], patL[i - 1] = tempL[i]; n   } n } n int main() { n   input(); n   solve(); n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long n, g; n   cin >> n >> g; n   g %= 4 * n; n   long long xx, yy; n   cin >> xx >> yy; n   long long x[n], y[n]; n   for (int(i) = int(0); i < int(n); ++i) cin >> x[i] >> y[i]; n   for (int(i) = int(1); i < int(g + 1); ++i) { n     long long dx = xx - x[(i - 1) % n]; n     long long dy = yy - y[(i - 1) % n]; n     xx -= 2 * dx; n     yy -= 2 * dy; n   } n   cout << xx <<     << yy << endl; n } n


#include <bits/stdc++.h> n using namespace std; n struct dat { n   long long a; n   long long n; n }; n bool comp(struct dat a, struct dat b) { return a.a < b.a; } n int main() { n   ios::sync_with_stdio(false); n   long long a, b, c, i, j, k, l, m, t, x, y, n, z; n   vector<long long> a1; n   string s; n   cin >> n; n   vector<vector<long long> > arr(n, vector<long long>(2)); n   struct dat arr1[n]; n   for (i = 0; i < n; i++) { n     cin >> arr[i][0]; n     arr[i][1] = i; n     arr1[i].a = arr[i][0]; n     arr1[i].n = i; n   } n   sort(arr1, arr1 + n, comp); n   for (i = 0; i < n; i++) { n     arr[arr1[i].n][1] = i; n   } n   vector<vector<long long> > ans; n   int arr3[n]; n   for (i = 0; i < n; i++) { n     arr3[i] = 0; n   } n   for (i = 0; i < n; i++) { n     if (arr3[i] == 1) n       continue; n     else { n       j = i; n       vector<long long> q1; n       while (arr3[j] == 0) { n         arr3[j] = 1; n         q1.push_back(j); n         j = arr[j][1]; n       } n       sort(q1.begin(), q1.end()); n       ans.push_back(q1); n     } n   } n   cout << ans.size() << endl; n   for (i = 0; i < ans.size(); i++) { n     cout << ans[i].size() <<    ; n     for (j = 0; j < ans[i].size(); j++) { n       cout << ans[i][j] + 1 <<    ; n     } n     cout << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long MOD = 998244353; n const long long N = 5e3 + 5; n long long a[N]; n long long pre[N]; n long long dp[N][N]; n long long n, m, k; n long long check = 0; n long long go(long long pos, long long kk) { n   if (kk == k) return 0; n   if (pos == n + 1) return -1e15; n   long long &ans = dp[pos][kk]; n   if (ans != -1) return ans; n   ans = go(pos + 1, kk); n   if (pos + m - 1 <= n) { n     ans = max(ans, pre[pos + m - 1] - pre[pos - 1] + go(pos + m, kk + 1)); n   } n   return ans; n } n int32_t main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cin >> n >> m >> k; n   for (long long i = 1; i <= n; i++) { n     cin >> a[i]; n     pre[i] = pre[i - 1] + a[i]; n   } n   memset(dp, -1, sizeof(dp)); n   cout << go(1, 0) << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int n; n string str; n int main() { n   cin >> n >> str; n   for (int i = 0; i < str.size(); i++) { n     if (n % (i + 1) == 0) { n       for (int j = 0; j <= i / 2; j++) { n         swap(str[j], str[i - j]); n       } n     } n   } n   cout << str; n } n


#include <bits/stdc++.h> n using namespace std; n bool deb = false; n int i, k, n; n const int N = 500011; n int a[N]; n bool f1(long long int x) { n   int i; n   long long int inc = 0, dec = 0; n   for (i = 0; i < n; ++i) { n     if (a[i] > x) n       dec += (a[i] - x); n     else n       inc += (x - a[i]); n   } n   if (dec <= inc && dec <= k) return 1; n   return 0; n } n bool f2(long long int x) { n   int i; n   long long int inc = 0, dec = 0; n   for (i = 0; i < n; ++i) { n     if (a[i] > x) n       dec += (a[i] - x); n     else n       inc += (x - a[i]); n   } n   if (dec >= inc && inc <= k) return 1; n   return 0; n } n long long int min_max(long long int lo, long long int hi) { n   while (lo < hi) { n     long long int mid = lo + (hi - lo) / 2; n     if (f1(mid)) { n       hi = mid; n     } else n       lo = mid + 1; n   } n   return hi; n } n long long int max_min(long long int lo, long long int hi) { n   while (lo < hi) { n     long long int mid = lo + (hi - lo) / 2; n     if (f2(mid)) { n       lo = mid + 1; n     } else n       hi = mid; n   } n   return lo - 1; n } n int main() { n   int i, j; n   scanf( %d , &n), scanf( %d , &k); n   for (i = 0; i < n; ++i) scanf( %d , &a[i]); n   int mx = min_max(1, 1000000007); n   int mn = max_min(1, 1000000007); n   printf( %d n , mx - mn); 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   ; n   int n; n   cin >> n; n   string s; n   map<string, int> mp; n   for (int i = 0; i < n; ++i) { n     cin >> s; n     mp[s]++; n   } n   int ans = 0; n   for (auto ss : mp) { n     ans = max(ans, ss.second); n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n const int mx = 2002; n const int inf = 1000 * 1000 * 1000; n using namespace std; n vector<int> adj[mx]; n vector<long long> w[mx]; n set<pair<long long, int> > s; n long long dist[mx]; n bool mrk[mx]; n void dijkstra() { n   while (s.size()) { n     pair<long long, int> p = *s.begin(); n     s.erase(s.begin()); n     int v = p.second; n     mrk[v] = 1; n     for (int i = 0; i < adj[v].size(); i++) { n       int u = adj[v][i]; n       if (!mrk[u]) { n         pair<long long, int> p1 = make_pair(dist[u], u); n         s.erase(p1); n         dist[u] = min(dist[u], w[v][i] + dist[v]); n         s.insert(make_pair(dist[u], u)); n       } n     } n   } n } n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   int n, cnt = 0; n   long long mn = inf; n   cin >> n; n   for (int i = 0; i < n; i++) { n     for (int j = i + 1; j < n; j++) { n       long long k; n       cin >> k; n       w[i].push_back(k); n       w[j].push_back(k); n       adj[i].push_back(j); n       adj[j].push_back(i); n       cnt++; n       mn = min(mn, k); n     } n   } n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < w[i].size(); j++) { n       w[i][j] -= mn; n     } n   } n   for (int i = 0; i < n; i++) { n     long long ww = inf; n     for (int j = 0; j < adj[i].size(); j++) { n       ww = min(ww, w[i][j]); n     } n     dist[i] = 2 * ww; n     s.insert(make_pair(dist[i], i)); n   } n   dijkstra(); n   for (int i = 0; i < n; i++) { n     cout << dist[i] + (n - 1) * mn << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n void DBG() { cerr <<  ] n ; } n template <typename T, typename... Args> n void DBG(T first, Args... args) { n   cerr << first; n   if (sizeof...(args)) cerr <<  ,  ; n   DBG(args...); n } n void solve() { n   long long int x0, y0, ax, ay, bx, by, sx, sy, t; n   cin >> x0 >> y0 >> ax >> ay >> bx >> by >> sx >> sy >> t; n   vector<long long int> px, py; n   px.push_back(x0); n   py.push_back(y0); n   while (true) { n     long long bound = 0; n     if (px.back() > sx) { n       bound += px.back() - sx; n     } n     if (py.back() > sy) { n       bound += py.back() - sy; n     } n     if (bound > t) { n       px.pop_back(); n       py.pop_back(); n       break; n     } n     px.push_back(px.back() * ax + bx); n     py.push_back(py.back() * ay + by); n   } n   long long int n = px.size(); n   long long int g = 0; n   for (long long int i = 0; i < n; i++) { n     for (long long int j = i; j < n; j++) { n       long long int from_i = abs(sx - px[i]) + abs(sy - py[i]); n       long long int from_j = abs(sx - px[j]) + abs(sy - py[j]); n       long long int cnt = n           min(from_i, from_j) + abs(px[i] - px[j]) + abs(py[i] - py[j]); n       if (cnt <= t) { n         g = max(g, j - i + 1); n       } n     } n   } n   cout << g <<   n ; n } n int32_t main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   long long int t = 1; n   for (long long int tt = 1; tt <= t; tt++) { n     solve(); n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 1000000 + 100; n int n, root, u, Sum; n int num[maxn], sum[maxn]; n vector<int> G[maxn]; n int ans1, ans2; n void dfs1(int x) { n   sum[x] = num[x]; n   int len = G[x].size(); n   for (int i = 0; i < len; ++i) { n     int pos = G[x][i]; n     dfs1(pos); n     if (ans1 != 0) { n       return; n     } n     sum[x] += sum[pos]; n   } n   if (sum[x] == Sum / 3) { n     ans1 = x; n   } n } n void dfs2(int x) { n   num[x] = 0; n   int len = G[x].size(); n   for (int i = 0; i < len; ++i) { n     int pos = G[x][i]; n     dfs2(pos); n   } n } n void dfs3(int x) { n   sum[x] = num[x]; n   int len = G[x].size(); n   for (int i = 0; i < len; ++i) { n     int pos = G[x][i]; n     if (pos != ans1) { n       dfs3(pos); n       if (ans2 != 0) { n         return; n       } n       sum[x] += sum[pos]; n     } n   } n   if (sum[x] == Sum / 3) { n     ans2 = x; n   } n } n int main() { n   ios::sync_with_stdio(false); n   while (scanf( %d , &n) != EOF) { n     for (int i = 1; i <= n; ++i) { n       G[i].clear(); n     } n     Sum = 0; n     ans1 = ans2 = 0; n     for (int i = 1; i <= n; ++i) { n       scanf( %d%d , &u, &num[i]); n       Sum += num[i]; n       if (u != 0) { n         G[u].push_back(i); n       } else { n         root = i; n       } n     } n     if (Sum % 3 != 0) { n       printf( -1 n ); n       continue; n     } n     dfs1(root); n     if (ans1 == 0 || ans1 == root) { n       printf( -1 n ); n       continue; n     } n     dfs2(ans1); n     dfs3(root); n     if (ans2 == 0 || ans2 == root) { n       printf( -1 n ); n     } else { n       printf( %d %d n , ans1, ans2); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int a, n, m, mis, cri; n int main() { n   cin >> a; n   for (int i = 0; i < a; i++) { n     cin >> n >> m; n     if (n > m) { n       mis++; n     } else if (n < m) { n       cri++; n     } n   } n   if (mis > cri) { n     cout <<  Mishka ; n   } else if (cri > mis) { n     cout <<  Chris ; n   } else { n     cout <<  Friendship is magic!^^ ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n using ll = long long; n using ld = long double; n using ii = pair<int, int>; n using vi = vector<int>; n using vb = vector<bool>; n using vvi = vector<vi>; n using vii = vector<ii>; n using vvii = vector<vii>; n const int INF = 2000000000; n const ll LLINF = 9000000000000000000; n struct T { n   ld mat[2][2]; n   ld dx = 0, dy = 0; n   T() { n     for (int i = 0; i < 2; ++i) n       for (int j = 0; j < 2; ++j) mat[i][j] = i == j; n   } n   T(const T &o) { n     for (int i = 0; i < 2; ++i) n       for (int j = 0; j < 2; ++j) mat[i][j] = o.mat[i][j]; n     dx = o.dx; n     dy = o.dy; n   } n }; n T op(T l, T r) { n   T m; n   m.mat[0][0] = l.mat[0][0] * r.mat[0][0] + l.mat[0][1] * r.mat[1][0]; n   m.mat[0][1] = l.mat[0][0] * r.mat[0][1] + l.mat[0][1] * r.mat[1][1]; n   m.mat[1][0] = l.mat[1][0] * r.mat[0][0] + l.mat[1][1] * r.mat[1][0]; n   m.mat[1][1] = l.mat[1][0] * r.mat[0][1] + l.mat[1][1] * r.mat[1][1]; n   m.dx = l.mat[0][0] * r.dx + l.mat[0][1] * r.dy + l.dx; n   m.dy = l.mat[1][0] * r.dx + l.mat[1][1] * r.dy + l.dy; n   return m; n } n T ident; n struct SegmentTree { n   struct Node { n     T val; n     int l, r; n     Node(T _val, int _l, int _r) : val(_val), l(_l), r(_r){}; n   }; n   int n; n   vector<Node> tree; n   SegmentTree(int p, vector<T> &init) : n(1 << p) { n     tree.assign(2 * n, Node(ident, 0, n - 1)); n     for (int j = 1; j < n; ++j) { n       int m = (tree[j].l + tree[j].r) / 2; n       tree[2 * j].l = tree[j].l; n       tree[2 * j].r = m; n       tree[2 * j + 1].l = m + 1; n       tree[2 * j + 1].r = tree[j].r; n     } n     for (int j = 2 * n - 1; j > 0; --j) { n       if (j >= n) n         tree[j].val = init[j - n]; n       else n         tree[j].val = op(tree[2 * j].val, tree[2 * j + 1].val); n     } n   } n   void update(int i, T val) { n     for (tree[i + n].val = val, i = (i + n) / 2; i > 0; i /= 2) n       tree[i].val = op(tree[2 * i].val, tree[2 * i + 1].val); n   } n   T query(int l, int r) { n     T lhs, rhs; n     for (l += n, r += n; l < r; l >>= 1, r >>= 1) { n       if (l & 1) lhs = op(lhs, tree[l++].val); n       if (!(r & 1)) rhs = op(tree[r--].val, rhs); n     } n     return op(l == r ? op(lhs, tree[l].val) : lhs, rhs); n   } n }; n int main() { n   ios::sync_with_stdio(false); n   cin.tie(NULL); n   int n, m; n   cin >> n >> m; n   int p = 1; n   while ((1 << p) < 2 * n) ++p; n   vector<T> init(1 << p, T()); n   for (int i = 0; i < n; ++i) { n     init[2 * i + 1].dx = 1; n   } n   SegmentTree st(p, init); n   vector<ld> curdeg(n, 0.0); n   while (m--) { n     int x, y, z; n     cin >> x >> y >> z; n     --y; n     if (x == 1) { n       T cur = st.query(2 * y + 1, 2 * y + 1); n       cur.dx += z; n       st.update(2 * y + 1, cur); n     } else { n       curdeg[y] -= ld(z) / 180.0 * 3.14159265358979323846; n       T m; n       m.mat[0][0] = m.mat[1][1] = cos(curdeg[y]); n       m.mat[1][0] = sin(curdeg[y]); n       m.mat[0][1] = -m.mat[1][0]; n       st.update(2 * y, m); n     } n     T f = st.query(0, 2 * n - 1); n     printf( %.7lf %.7lf n , double(f.dx), double(f.dy)); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long arr[166666][6]; n multiset<long long> s[6]; n signed main() { n   long long n; n   scanf( %lld , &n); n   for (long long i = 1; i <= n; i++) { n     for (long long j = 1; j <= 4; j++) { n       scanf( %lld , &arr[i][j]); n       s[j].insert(arr[i][j]); n     } n   } n   for (long long i = 1; i <= n; i++) { n     for (long long j = 1; j <= 4; j++) { n       s[j].erase(s[j].find(arr[i][j])); n     } n     if (*s[1].rbegin() <= *s[3].begin() && *s[2].rbegin() <= *s[4].begin()) { n       cout << *s[1].rbegin() <<     << *s[2].rbegin(); n       return 0; n     } n     for (long long j = 1; j <= 4; j++) { n       s[j].insert(arr[i][j]); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   string s; n   long long int k, a[26], i, max = -1, l, sum = 0; n   cin >> s; n   cin >> k; n   l = s.length(); n   for (i = 0; i < 26; i++) { n     cin >> a[i]; n     if (max < a[i]) max = a[i]; n   } n   for (i = 0; i < l; i++) { n     sum = sum + (i + 1) * a[s[i] -  a ]; n   } n   for (i = 0; i < k; i++) { n     sum += (i + l + 1) * max; n   } n   cout << sum; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long Set(long long N, long long pos) { return N = N | (1 << pos); } n long long reset(long long N, long long pos) { return N = N & ~(1 << pos); } n bool check(long long N, long long pos) { return (bool)(N & (1 << pos)); } n void CI(long long &_x) { scanf( %d , &_x); } n void CO(long long &_x) { cout << _x; } n template <typename T> n void getarray(T a[], long long n) { n   for (long long i = 0; i < n; i++) cin >> a[i]; n } n template <typename T> n void prLLIarray(T a[], long long n) { n   for (long long i = 0; i < n - 1; i++) cout << a[i] <<    ; n   cout << a[n - 1] << endl; n } n const double EPS = 1e-9; n const long long INF = 0x7f7f7f7f; n long long dr8[8] = {1, -1, 0, 0, 1, -1, -1, 1}; n long long dc8[8] = {0, 0, -1, 1, 1, 1, -1, -1}; n long long dr4[4] = {0, 0, 1, -1}; n long long dc4[4] = {-1, 1, 0, 0}; n long long kn8r[8] = {1, 2, 2, 1, -1, -2, -2, -1}; n long long kn8c[8] = {2, 1, -1, -2, -2, -1, 1, 2}; n int main() { n   double a, b, m; n   cin >> a >> b >> m; n   double vx, vy, vz; n   cin >> vx >> vy >> vz; n   double tt = m / vy; n   tt = fabs(tt); n   double x = (a / 2.0) + (tt * vx); n   double z = vz * tt; n   while (x < 0) { n     x += 2.0 * a; n   } n   while (x > (2 * a)) x -= (2.0 * a); n   if (x > a) x = (2.0 * a) - x; n   while (z < 0) z += (2.0 * b); n   while (z > (2 * b)) z -= (2.0 * b); n   if (z > b) z = (2.0 * b) - z; n   cout << fixed << setprecision(9) << x <<     << z <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n template <class T> n int len(const T &c) { n   return (int)c.size(); n } n template <class T> n void cmin(T &a, T b) { n   if (b < a) a = b; n } n template <class T> n void cmax(T &a, T b) { n   if (b > a) a = b; n } n int n; n int main() { n   cin >> n; n   if (n == 3) { n     cout << 5; n     return 0; n   } n   int t; n   for (int i = 1;; i += 2) { n     t = i * i; n     t++; n     t /= 2; n     if (t >= n) { n       cout << i << endl; n       return 0; n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 200005; n vector<int> adj[N]; n int sum[N]; n void dfs1(int node) { n   if (adj[node].size() == 0) sum[node] = 1; n   for (int i = 0; i < adj[node].size(); i++) { n     int a = adj[node][i]; n     dfs1(a); n     sum[node] += sum[a]; n   } n } n int mx(int node); n int mn(int node); n int Mx(int node); n int Mn(int node); n int mx(int node) { n   if (adj[node].size() == 0) return 1; n   int ret = 1; n   for (int i = 0; i < adj[node].size(); i++) { n     int a = adj[node][i]; n     ret = max(ret, sum[node] - sum[a] + mn(a)); n   } n   return ret; n } n int mn(int node) { n   if (adj[node].size() == 0) return 1; n   int ret = 0; n   for (int i = 0; i < adj[node].size(); i++) { n     int a = adj[node][i]; n     ret += mx(a) - 1; n   } n   return ret + 1; n } n int Mx(int node) { n   if (adj[node].size() == 0) return 1; n   int ret = 0; n   for (int i = 0; i < adj[node].size(); i++) { n     int a = adj[node][i]; n     ret += Mn(a); n   } n   return ret; n } n int Mn(int node) { n   if (adj[node].size() == 0) return 1; n   int ret = N; n   for (int i = 0; i < adj[node].size(); i++) { n     int a = adj[node][i]; n     ret = min(ret, Mx(a)); n   } n   return ret; n } n int main() { n   int n, a, b; n   scanf( %d , &n); n   for (int i = 0; i < n - 1; i++) { n     scanf( %d%d , &a, &b); n     adj[a].push_back(b); n   } n   dfs1(1); n   cout << mx(1) <<     << Mx(1); n   return 0; n } n


#include <bits/stdc++.h> n #pragma GCC optimize( Ofast,no-stack-protector,unroll-loops ) n #pragma GCC optimize( no-stack-protector,fast-math ) n #pragma GCC target( sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native ) n using namespace std; n template <class S, class T> n inline S smax(S &l, T r) { n   return l = max(l, (S)r); n } n template <class S, class T> n inline S smin(S &l, T r) { n   return l = min(l, (S)r); n } n const int N = 1e6 + 10; n int nex[N][26], sz, cnt[N], lvl[N]; n inline void In(string &s) { n   int now = 0; n   for (auto c : s) { n     c -=  a ; n     if (!nex[now][c]) { n       nex[now][c] = ++sz; n       lvl[sz] = lvl[now] + 1; n     } n     now = nex[now][c]; n   } n   cnt[now]++; n } n vector<int> dp[N]; n inline int f(int i) { return i * (i - 1) / 2; } n inline void merge(int v, int u) { n   vector<int> tmp(dp[v].size() + dp[u].size() - 1); n   for (int i = 0; i < (int)dp[v].size(); i++) n     for (int j = 0; j < (int)dp[u].size(); j++) n       tmp[i + j] = max(tmp[i + j], dp[v][i] + dp[u][j] + i * j * lvl[v]); n   dp[v] = tmp; n } n void dfs(int v) { n   for (int i = 0; i <= cnt[v]; i++) dp[v].emplace_back(lvl[v] * f(i)); n   for (int i = 0; i < 26; i++) n     if (nex[v][i]) { n       dfs(nex[v][i]); n       merge(v, nex[v][i]); n     } n } n int32_t main() { n   ios::sync_with_stdio(0), cin.tie(0), cout.tie(0); n   int n, k; n   cin >> n >> k; n   for (int i = 0; i < n; ++i) { n     string s; n     cin >> s; n     In(s); n   } n   dfs(0); n   cout << dp[0][k] << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int dx[] = {-1, 0, 1, 0}; n const int dy[] = {0, 1, 0, -1}; n int inp[100005]; n int main() { n   int n, a, b; n   cin >> n; n   int flag = 0; n   string str; n   for (int i = 0; i < n; i++) { n     cin >> str; n     int a, b; n     cin >> a >> b; n     if (a >= 2400 && b > 2400 && b - a > 0) { n       flag = 1; n     } n   } n   if (flag) n     cout <<  YES ; n   else n     cout <<  NO ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long a, b, c, d; n   cin >> a >> b >> c >> d; n   long long sum = (a + b + c + d) / 3; n   if (sum == a) n     cout << sum - b <<     << sum - c <<     << sum - d; n   else if (sum == b) n     cout << sum - a <<     << sum - c <<     << sum - d; n   else if (sum == c) n     cout << sum - a <<     << sum - b <<     << sum - d; n   else n     cout << sum - a <<     << sum - b <<     << sum - c; n } n


#include<bits/stdc++.h> n using namespace std; n  n typedef long long ll; n typedef vector<int> vi; n typedef pair<int, int> pi; n  n #define rep(i,k,n) for (int i = k; i < n; ++i) n #define repr(i,k,n) for (int i = k; i >= n; --i) n  n int main() n { n  n  ios_base::sync_with_stdio(false); n  cin.tie(NULL); n  n #ifndef ONLINE_JUDGE n  freopen( input.txt ,  r , stdin); n  freopen( output.txt ,  w , stdout); n #endif n  int t; n  cin >> t; n  while (t--) n  { n   int n; cin >> n; n   string r, b; n   cin >> r >> b; n   int count1 = 0, count2 = 0; n   rep(i, 0, n) n   { n    if (r[i] > b[i]) n    { n     count1++; n    } n    else if (b[i] > r[i]) n    { n     count2++; n    } n    else n    { n     count1++; n     count2++; n    } n   } n   if (count1 > count2) n   { n    cout <<  RED  << endl; n   } n   else if (count2 > count1) n   { n    cout <<  BLUE  << endl; n   } n   else cout <<  EQUAL  << endl; n  } n } n  n


#include <bits/stdc++.h> n using namespace std; n long long n, l, r; n long long getMin() { n   long long ret = 0ll; n   for (long long i = 1, p = 1; i <= min(l, n); i++, p *= 2) { n     ret += p; n   } n   ret += max(0ll, (n - l)); n   return ret; n } n long long getMax() { n   long long ret = 0ll, p = 1ll; n   for (long long i = 1; i <= min(r, n); i++, p *= 2) { n     ret += p; n   } n   p /= 2ll; n   ret += max(0ll, (n - r)) * p; n   return ret; n } n int main() { n   scanf( %lld %lld %lld , &n, &l, &r); n   printf( %lld %lld n , getMin(), getMax()); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const double eps = 1e-6; n void __print(int x) { cerr << x; } n void __print(long x) { cerr << x; } n void __print(long long x) { cerr << x; } n void __print(unsigned x) { cerr << x; } n void __print(unsigned long x) { cerr << x; } n void __print(unsigned long long x) { cerr << x; } n void __print(float x) { cerr << x; } n void __print(double x) { cerr << x; } n void __print(long double x) { cerr << x; } n void __print(char x) { cerr <<      << x <<     ; } n void __print(const char *x) { cerr <<     << x <<    ; } n void __print(const string &x) { cerr <<     << x <<    ; } n void __print(bool x) { cerr << (x ?  true  :  false ); } n template <typename T, typename V> n void __print(const pair<T, V> &x) { n   cerr <<  { ; n   __print(x.first); n   cerr <<  , ; n   __print(x.second); n   cerr <<  } ; n } n template <typename T> n void __print(const T &x) { n   int f = 0; n   cerr <<  { ; n   for (auto &i : x) cerr << (f++ ?  ,  :   ), __print(i); n   cerr <<  } ; n } n void _print() { cerr <<  ] n ; } n template <typename T, typename... V> n void _print(T t, V... v) { n   __print(t); n   if (sizeof...(v)) cerr <<  ,  ; n   _print(v...); n } n void solve() { n   long long n, i, j; n   cin >> n; n   long long x[n]; n   for (i = 0; i < n; i++) { n     cin >> x[i]; n   } n   long long m; n   cin >> m; n   long long y[m]; n   for (i = 0; i < m; i++) { n     cin >> y[i]; n   } n   long long k; n   cin >> k; n   long long z[k]; n   for (i = 0; i < k; i++) { n     cin >> z[i]; n   } n   long long a, b; n   sort(x, x + n); n   sort(y, y + m); n   sort(z, z + k); n   cin >> a >> b; n   double r = double(x[n - 1] * sqrt((double(b * y[m - 1])) / n                                     double(a * z[0] + b * y[m - 1]))); n   cout << fixed << setprecision(18) << r <<   n ; n } n signed main() { n   ios::sync_with_stdio(false); n   cin.tie(NULL); n   ; n   long long t = 1; n   while (t--) solve(); n } n


#include <bits/stdc++.h> n using namespace std; n using ll = long long; n const ll N = 2e5 + 10; n const ll inf = 1e18; n const ll MOD = 1e9 + 100; n int n; n int main() { n   int t; n   cin >> t; n   while (t--) { n     cin >> n; n     if (n < 4) n       printf( -1 n ); n     else { n       deque<int> q; n       q = {3, 1, 4, 2}; n       for (int i = 5; i <= n; i++) { n         if (i % 2) { n           q.push_back(i); n         } else n           q.push_front(i); n       } n       for (int i = 0; i < q.size(); i++) printf( %d  , q[i]); n       printf(  n ); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<vector<long long> > g; n long long vis[200005], w[200005]; n long long cycle[200005]; n long long taken[200005]; n long long s; n void dfs(long long x, long long par) { n   vis[x] = 1; n   for (long long i = 0; i < g[x].size(); i++) { n     long long y = g[x][i]; n     if (y == par) continue; n     if (vis[y] == 1) n       cycle[y] = 1, cycle[x] = 1; n     else if (vis[y] == 0) { n       dfs(y, x); n       if (cycle[y] == 1) cycle[x] = 1; n     } n   } n   vis[x] = 2; n } n long long maxi; n long long dfs2(long long x) { n   vis[x] = 1; n   long long ans = 0, res = 0; n   for (long long i = 0; i < g[x].size(); i++) { n     long long y = g[x][i]; n     if (cycle[x] == 1) { n       if (!vis[y]) { n         if (cycle[y]) n           ans += dfs2(y); n         else { n           maxi = max(maxi, dfs2(y)); n         } n       } n     } else { n       if (!vis[y]) { n         res = max(res, dfs2(y)); n       } n     } n   } n   if (x == s - 1) { n     return ans + maxi + w[x]; n   } n   if (cycle[x] == 0) ans += res, maxi = max(maxi, ans + w[x]); n   return w[x] + ans; n } n int32_t main() { n   ios::sync_with_stdio(false); n   cin.tie(0), cout.tie(0); n   long long n, m; n   cin >> n >> m; n   vector<long long> v; n   for (long long i = 0; i < n; i++) g.push_back(v); n   maxi = 0; n   for (long long i = 0; i < n; i++) cin >> w[i]; n   for (long long i = 0; i < m; i++) { n     long long x, y; n     cin >> x >> y; n     x--, y--; n     g[x].push_back(y), g[y].push_back(x); n   } n   cin >> s; n   dfs(s - 1, -1); n   for (long long i = 0; i < n; i++) vis[i] = 0; n   cout << dfs2(s - 1) << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n signed main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   int n; n   cin >> n; n   vector<int> a(n); n   for (int &i : a) cin >> i; n   int ans = -1, now = 0; n   vector<bool> vis(n); n   while (now < n) { n     ans++; n     if (ans % 2) n       for (int i = n - 1; i >= 0; i--) n         if (!vis[i] && a[i] <= now) n           vis[i] = true, now++; n         else n           ; n     else n       for (int i = 0; i < n; i++) n         if (!vis[i] && a[i] <= now) n           vis[i] = true, now++; n         else n           ; n   } n   cout << ans <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, k, el[101]; n set<int> st; n int main() { n   cin >> n >> k; n   for (int i = 1; i < (int)n + 1; ++i) st.insert(i); n   set<int>::iterator it = st.begin(), er; n   for (int i = 0; i < (int)k; ++i) { n     int a; n     cin >> a; n     int ind = ((a) % (int)(st).size()); n     for (; ind--;) { n       if (++it == st.end()) it = st.begin(); n     } n     cout << *it <<    ; n     er = it; n     if (++it == st.end()) it = st.begin(); n     st.erase(er); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long int mod = 1000000007; n const long long int inf = 1e18; n const long long int N = 1e5 + 5; n const long long int M = N; n const long long int LG = 19; n long long int n, m; n long long int tim = 0, grp = 1; n long long int u[N], v[N], storecomp[N]; n long long int level[N]; n bool vis[N], vis2[N], isBridge[M]; n long long int tin[N], tout[N], minAncestor[N]; n queue<long long int> Q[N]; n vector<pair<long long int, long long int> > g[N]; n vector<long long int> tree[N], vertices[N]; n long long int parent[LG][N]; n void dfs(long long int k, long long int par) { n   vis[k] = 1; n   tin[k] = ++tim; n   minAncestor[k] = tin[k]; n   for (auto it : g[k]) { n     if (it.first == par) continue; n     if (vis[it.first]) { n       minAncestor[k] = min(minAncestor[k], tin[it.first]); n       continue; n     } n     dfs(it.first, k); n     minAncestor[k] = min(minAncestor[k], minAncestor[it.first]); n     if (minAncestor[it.first] > tin[k]) isBridge[it.second] = 1; n   } n   tout[k] = tim; n } n void dfs2(long long int k) { n   long long int comp = grp; n   Q[comp].push(k); n   vis2[k] = 1; n   while (!Q[comp].empty()) { n     long long int u = Q[comp].front(); n     Q[comp].pop(); n     vertices[comp].push_back(u); n     storecomp[u] = comp; n     for (auto it : g[u]) { n       long long int v = it.first; n       long long int edgeidx = it.second; n       if (vis2[v]) continue; n       if (isBridge[edgeidx]) { n         grp++; n         tree[comp].push_back(grp); n         tree[grp].push_back(comp); n         dfs2(v); n       } else { n         Q[comp].push(v); n         vis2[v] = 1; n       } n     } n   } n } n void dfs3(long long int v, long long int par, long long int lvl) { n   tin[v] = ++tim; n   parent[0][v] = par; n   level[v] = lvl; n   for (auto &u : tree[v]) { n     if (u == par) continue; n     dfs3(u, v, lvl + 1); n   } n   tout[v] = tim; n } n bool is_ancestor(long long int u, long long int v) { n   return (tin[u] <= tin[v] and tout[u] >= tout[v]); n } n void precompute() { n   for (long long int i = 1; i < LG; i++) { n     for (long long int j = 1; j <= n; j++) { n       if (parent[i - 1][j]) { n         parent[i][j] = parent[i - 1][parent[i - 1][j]]; n       } n     } n   } n } n long long int __lca(long long int u, long long int v) { n   if (level[u] < level[v]) swap(u, v); n   long long int diff = level[u] - level[v]; n   for (long long int i = LG - 1; i >= 0; i--) { n     if ((1 << i) & diff) { n       u = parent[i][u]; n     } n   } n   if (u == v) return u; n   for (long long int i = LG - 1; i >= 0; i--) { n     if (parent[i][u] and parent[i][u] != parent[i][v]) { n       u = parent[i][u]; n       v = parent[i][v]; n     } n   } n   return parent[0][u]; n } n long long int dist(long long int u, long long int v) { n   return level[u] + level[v] - 2 * level[__lca(u, v)]; n } n void HyperBeast() { n   cin >> n >> m; n   for (long long int i = 1; i <= m; i++) { n     cin >> u[i] >> v[i]; n     g[u[i]].push_back({v[i], i}); n     g[v[i]].push_back({u[i], i}); n   } n   dfs(1, 0); n   dfs2(1); n   tim = 0; n   dfs3(1, 1, 1); n   precompute(); n   long long int k; n   cin >> k; n   for (long long int i = 1; i <= k; i++) { n     long long int u, v; n     cin >> u >> v; n     cout << dist(storecomp[u], storecomp[v]) <<   n ; n   } n } n int32_t main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   long long int T = 1; n   while (T--) { n     HyperBeast(); n     cout <<   n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n string s; n const int MaxN = 2e5 + 10; n int endd, vis[MaxN]; n int main() { n   int tt; n   cin >> tt; n   while (tt--) { n     cin >> s; n     stack<int> sta; n     for (int i = 0; i < s.size(); i++) { n       sta.push(s[i]); n       if (sta.top() ==  B  && sta.size() > 1) { n         sta.pop(); n         sta.pop(); n       } n     } n     cout << sta.size() << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n #pragma GCC optimize( Ofast ) n #pragma GCC target( avx,avx2,fma ) n #pragma GCC optimization( unroll-loops ) n using namespace std; n mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); n void naive(int x, int y) { n   if (x == y) { n     cout <<  Yes n ; n     return; n   } n   for (int i = 2; i * i <= x; i++) { n     while (x % i == 0) { n       if (y % (i * i) == 0) { n         y /= i * i; n         x /= i; n       } else if (x % (i * i) == 0 && y % i == 0) { n         x /= i * i; n         y /= i; n       } else { n         cout <<  No n ; n         return; n       } n     } n   } n   if (x == y * y || x * x == y) n     cout <<  Yes n ; n   else n     cout <<  No n ; n } n void fast(long long x, long long y) { n   long long p = x * y; n   long long l = 1, r = 1e6 + 42; n   while (r - l > 1) { n     long long m = ((r + l) >> 1); n     if (m * m * m > p) n       r = m; n     else n       l = m; n   } n   if (l * l * l == p && x % l == 0 && y % l == 0) n     cout <<  Yes n ; n   else if (r * r * r == p && x % r == 0 && y % r == 0) n     cout <<  Yes n ; n   else n     cout <<  No n ; n } n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   int n = 0; n   cin >> n; n   while (n--) { n     long long a, b; n     cin >> a >> b; n     if (a > b) swap(a, b); n     fast(a, b); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int ent; n   long long int a, b; n   cin >> ent; n   while (ent--) { n     int div = 0, pls = 0; n     cin >> a >> b; n     if (a % b == 0) { n       cout << 0 << endl; n       continue; n     } n     div = a / b; n     pls = (div + 1) * b; n     cout << pls - a << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n long long n; n double t, ans, x, y; n map<pair<double, double>, bool> b; n pair<double, double> p[1005]; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   cin >> n >> t; n   for (int i = 1; i <= n; i++) { n     cin >> x >> y; n     p[i].first = x - y / 2; n     p[i].second = x + y / 2; n   } n   sort(p + 1, p + 1 + n); n   p[0].second = -1e18; n   for (int i = 1; i <= n; i++) { n     if (p[i].first - p[i - 1].second >= t && n         b[{p[i].first - t, p[i].first}] == false) { n       b[{p[i].first - t, p[i].first}] = true; n       ans++; n     } n     if (p[i].second + t <= p[i + 1].first) { n       b[{p[i].second, p[i].second + t}] = true; n       ans++; n     } n   } n   ans++; n   cout << ans <<   n ; n   return 0; n } n


#include <iostream> n #include <cstdio> n #include <cstring> n using namespace std; n char s[5010]; n int n; n  n bool ok(int x,int y,int fx,int fy){ n  if(x==fx && y==fy)return 0; n  return 1; n } n  n bool check(int fx,int fy){ n  int x=0,y=0; n  for(int i=1;i<=n;i++){ n   if(s[i]== D ){ n    if(ok(x,y-1,fx,fy))y--; n   } n   if(s[i]== U ){ n    if(ok(x,y+1,fx,fy))y++; n   } n   if(s[i]== L ){ n    if(ok(x-1,y,fx,fy))x--; n   } n   if(s[i]== R ){ n    if(ok(x+1,y,fx,fy))x++; n   } n  } n  if(x==0 && y==0)return 1; n  else return 0; n } n  n void work(){ n  int x=0,y=0; n  for(int i=1;i<=n;i++){ n   if(s[i]== D ){ n    if(check(x,y-1)){ n     printf( %d %d n ,x,y-1); n     return; n    } n    else y--; n   } n   else if(s[i]== U ){ n    if(check(x,y+1)){ n     printf( %d %d n ,x,y+1); n     return; n    } n    else y++; n   } n   else if(s[i]== L ){ n    if(check(x-1,y)){ n     printf( %d %d n ,x-1,y); n     return; n    } n    else x--; n   } n   else if(s[i]== R ){ n    if(check(x+1,y)){ n     printf( %d %d n ,x+1,y); n     return; n    } n    else x++; n   } n  } n  puts( 0 0 ); n  return; n } n  n int main(){ n  int T; n  scanf( %d ,&T); n  while(T--){ n   scanf( %s ,s+1); n   n=strlen(s+1); n   if(check(5001,5001)){ n    printf( 5001 5001 n ); n    continue; n   } n   work(); n  } n  return 0; n }


#include <bits/stdc++.h> n using namespace std; n int read() { n   int X = 0, w = 1; n   char c = getchar(); n   while (c <  0  || c >  9 ) { n     if (c ==  - ) w = -1; n     c = getchar(); n   } n   while (c >=  0  && c <=  9 ) X = X * 10 + c -  0 , c = getchar(); n   return X * w; n } n const int N = 125000 + 10; n int n, m; n char s[N], t[N]; n int bl[N], ans[N]; n int nxt[N]; n void getnext() { n   for (int i = 2, j = 0; i <= m; ++i) { n     while (j && bl[t[j + 1]] != bl[t[i]]) j = nxt[j]; n     if (bl[t[j + 1]] == bl[t[i]]) ++j; n     nxt[i] = j; n   } n } n void calc(int w) { n   getnext(); n   for (int i = 1, j = 0; i <= n; ++i) { n     while (j && bl[t[j + 1]] != bl[s[i]]) j = nxt[j]; n     if (bl[t[j + 1]] == bl[s[i]]) ++j; n     if (j == m) ans[i - m + 1] = min(ans[i - m + 1], w); n   } n } n void dfs(char c, int tot) { n   if (c >  f ) { n     calc(6 - tot); n     return; n   } n   for (int i = 1; i <= tot; ++i) bl[c] = i, dfs(c + 1, tot); n   bl[c] = tot + 1, dfs(c + 1, tot + 1); n } n int main() { n   scanf( %s%s , s + 1, t + 1), n = strlen(s + 1), m = strlen(t + 1); n   memset(ans, 0x3f, sizeof(ans)); n   dfs( a , 0); n   for (int i = 1; i <= n - m + 1; ++i) printf( %d  , ans[i]); n   puts(  ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n using ll = long long; n int main() { n   ios_base::sync_with_stdio(false); n   int N; n   cin >> N; n   ll ans = 0; n   priority_queue<int, vector<int>, greater<int>> q; n   for (int i = 0; i < N; ++i) { n     int v; n     cin >> v; n     q.emplace(v); n     q.emplace(v); n     ans += v - q.top(); n     q.pop(); n   } n   cout << ans <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 2e5 + 10; n int ar[maxn], num[maxn]; n int main() { n   int t; n   cin >> t; n   while (t--) { n     int n, m; n     memset(num, 0, sizeof(num)); n     memset(ar, 0, sizeof(ar)); n     cin >> n >> m; n     string s; n     cin >> s; n     for (int i = 1; i <= m; i++) { n       int x; n       cin >> x; n       x--; n       ar[x]++; n     } n     int len = s.length(); n     for (int i = 0; i < len; i++) { n       num[s[i] - 97]++; n     } n     for (int i = len - 1; i >= 0; i--) { n       ar[i] += ar[i + 1]; n       num[s[i] - 97] += ar[i]; n     } n     for (int i = 0; i < 26; i++) { n       if (i < 25) n         cout << num[i] <<    ; n       else n         cout << num[i] << endl; n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n int getDaysOfMonth(int m) { n   if (m == 1 || m == 3 || m == 5 || m == 7 || m == 8 || m == 10 || m == 12) n     return 31; n   if (m == 2) return 28; n   return 30; n } n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   ; n   int n; n   cin >> n; n   char a[n]; n   for (int i = 0; i < int(n); i++) cin >> a[i]; n   for (int i = 1; i <= n; i++) { n     if (n % i == 0) { n       reverse(a, a + i); n     } n   } n   for (int i = 0; i < int(n); i++) cout << a[i]; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long MOD = 1000000007; n string yes =  YES n ; n string no =  NO n ; n long long gcd(long long a, long long b) { n   if (b == 0) n     return a; n   else n     return gcd(b, a % b); n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   long long n, k; n   cin >> n >> k; n   long long dp[k + 1][n + 1]; n   memset(dp, 0, sizeof(dp)); n   for (long long i = 1; i <= n; i++) dp[1][i] = 1; n   for (long long i = 2; i <= k; i++) { n     for (long long j = 1; j <= n; j++) { n       for (long long t = j; t <= n; t += j) n         dp[i][t] = (dp[i][t] + dp[i - 1][j]) % MOD; n     } n   } n   long long ans = 0; n   for (long long i = 1; i <= n; i++) { n     ans += dp[k][i]; n     ans %= MOD; n   } n   cout << ans <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n using ll = long long; n using ld = long double; n using uint = unsigned int; n using ull = unsigned long long; n template <typename T> n using pair2 = pair<T, T>; n using pii = pair<int, int>; n using pli = pair<ll, int>; n using pll = pair<ll, ll>; n mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count()); n ll myRand(ll B) { return (ull)rng() % B; } n clock_t startTime; n double getCurrentTime() { n   return (double)(clock() - startTime) / CLOCKS_PER_SEC; n } n const ll MOD = 998244353; n ll add(ll x, ll y) { n   x += y; n   if (x >= MOD) return x - MOD; n   return x; n } n ll sub(ll x, ll y) { n   x -= y; n   if (x < 0) return x + MOD; n   return x; n } n ll mult(ll x, ll y) { return (x * y) % MOD; } n ll bin_pow(ll x, ll p) { n   if (p == 0) return 1; n   if (p & 1) return mult(x, bin_pow(x, p - 1)); n   return bin_pow(mult(x, x), p / 2); n } n ll rev(ll x) { return bin_pow(x, MOD - 2); } n const int N = 1505; n const int M = 2 * N; n const int K = N * N / 2; n vector<int> g[M]; n vector<int> a[M]; n vector<int> b[M]; n int par[M]; n int sz[M]; n int cntEd[M]; n int dp[M][N]; n int ed[K][2]; n int n, m; n void unite(int v, int u) { n   v = par[v]; n   u = par[u]; n   if (v == u) { n     cntEd[v]++; n     if (cntEd[v] == sz[v] * (sz[v] - 1) / 2) { n       for (int w : b[v]) { n         g[v].push_back(w); n       } n       b[v].clear(); n       b[v].push_back(v); n     } n     return; n   } n   sz[m] = sz[v] + sz[u]; n   cntEd[m] = cntEd[v] + cntEd[u] + 1; n   for (int x : a[v]) { n     a[m].push_back(x); n     par[x] = m; n   } n   for (int x : a[u]) { n     a[m].push_back(x); n     par[x] = m; n   } n   for (int x : b[v]) b[m].push_back(x); n   for (int x : b[u]) b[m].push_back(x); n   v = m; n   m++; n   if (cntEd[v] == sz[v] * (sz[v] - 1) / 2) { n     for (int w : b[v]) { n       g[v].push_back(w); n     } n     b[v].clear(); n     b[v].push_back(v); n   } n } n void dfs(int v) { n   if (g[v].empty()) { n     dp[v][1] = 1; n     return; n   } n   dp[v][0] = 1; n   int cur = 0; n   for (int u : g[v]) { n     dfs(u); n     int inner[N]; n     for (int i = 0; i <= cur + sz[u]; i++) inner[i] = 0; n     for (int i = 0; i <= cur; i++) n       for (int j = 1; j <= sz[u]; j++) n         inner[i + j] = add(inner[i + j], mult(dp[v][i], dp[u][j])); n     cur += sz[u]; n     for (int i = 0; i <= cur; i++) dp[v][i] = inner[i]; n   } n   assert(cur == sz[v]); n   dp[v][1] = add(dp[v][1], 1); n } n int main() { n   startTime = clock(); n   scanf( %d , &n); n   m = n; n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < n; j++) { n       int x; n       scanf( %d , &x); n       if (i < j) { n         ed[x][0] = i; n         ed[x][1] = j; n       } n     } n   } n   for (int i = 0; i < n; i++) { n     par[i] = i; n     a[i].push_back(i); n     b[i].push_back(i); n     sz[i] = 1; n     cntEd[i] = 0; n   } n   for (int x = 1; x <= n * (n - 1) / 2; x++) { n     int v = ed[x][0], u = ed[x][1]; n     assert(u > 0); n     unite(v, u); n   } n   dfs(m - 1); n   for (int i = 1; i <= n; i++) printf( %d  , dp[m - 1][i]); n   printf(  n ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void doicho(int &t1, int &t2) { n   if (t1 > t2) swap(t1, t2); n } n void sapxep(pair<double, int> result[], int n) { n   for (int i = 0; i < n - 1; i++) { n     for (int j = i + 1; j < n; j++) { n       if (result[i].first < result[j].first) { n         swap(result[i].first, result[j].first); n         swap(result[i].second, result[j].second); n       } else if (result[i].first == result[j].first && n                  result[i].second > result[j].second) { n         swap(result[i].first, result[j].first); n         swap(result[i].second, result[j].second); n       } n     } n   } n } n int main() { n   int n, t1, t2, k; n   cin >> n >> t1 >> t2 >> k; n   int a[n], b[n]; n   for (int i = 0; i < n; i++) cin >> a[i] >> b[i]; n   pair<double, int> result[n]; n   for (int i = 0; i < n; i++) { n     double x, y, z, t; n     x = (a[i] * t1 - t1 * a[i] * ((double)k / 100) + b[i] * t2); n     t = (a[i] * t2 - t1 * b[i] * ((double)k / 100) + b[i] * t1); n     double max2 = x > t ? x : t; n     result[i].first = max2; n     result[i].second = i + 1; n   } n   sapxep(result, n); n   for (int i = 0; i < n; i++) n     printf( %d %.2f n , result[i].second, result[i].first); n } n


#include <bits/stdc++.h> n using namespace std; n const long long INF = 1LL << 61; n int n, r; n long long a[30], b[30], k[30], p[30]; n long long dfs(int u, bool op1) { n   long long res = -INF; n   if (u % 2 == 0) res = max(res, inner_product(a, a + n, k, 0LL)); n   if (u == 0) return res; n   if (!op1) { n     for (int i = 0; i < (n); i++) a[i] ^= b[i]; n     res = max(res, dfs(u - 1, true)); n     for (int i = 0; i < (n); i++) a[i] ^= b[i]; n   } n   long long tmp[30]; n   memcpy(tmp, a, sizeof a); n   for (int i = 0; i < (n); i++) a[i] = tmp[p[i]] + r; n   res = max(res, dfs(u - 1, false)); n   memcpy(a, tmp, sizeof a); n   return res; n } n int main() { n   int u; n   scanf( %d%d%d , &n, &u, &r); n   for (int i = 0; i < (n); i++) scanf( %I64d , a + i); n   for (int i = 0; i < (n); i++) scanf( %I64d , b + i); n   for (int i = 0; i < (n); i++) scanf( %I64d , k + i); n   for (int i = 0; i < (n); i++) scanf( %I64d , p + i), p[i]--; n   printf( %I64d n , dfs(u, false)); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n bool flag = true; n const long long int N = 2e5 + 5; n const long long int inf = 998244353; n void solve() { n   long long int i = 0, j = 0, ans = 0, x = 0, y = 0, z = 0; n   long long int n, k = 0, m = 0, mn = LLONG_MAX, mx = LLONG_MIN; n   cin >> n >> k; n   vector<long long int> a(n), v; n   for (auto &it : a) { n     cin >> it; n     mx = max(mx, it); n     mn = min(mn, it); n   } n   if (mx == mn) { n     cout <<  0 ; n     return; n   } n   sort(a.begin(), a.end()); n   for (i = mx; i >= mn; i--) { n     j = upper_bound(a.begin(), a.end(), i) - a.begin(); n     v.push_back(n - j); n   } n   x = 0; n   ans = 1; n   for (i = 0; i < (long long int)v.size(); i++) { n     if (x + v[i] <= k) n       x += v[i]; n     else { n       ans++; n       x = v[i]; n     } n   } n   cout << ans << endl; n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   ; n   long long int T = 1, te = 1; n   while (T--) { n     solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<char> v; n int n; n string s; n bool potR() { n   int freq[1000]; n   freq[ G ] = 0; n   freq[ B ] = 0; n   freq[ R ] = 0; n   for (int i = 0; i < s.size(); ++i) { n     ++freq[s[i]]; n   } n   int cnt1 = freq[ B ]; n   int cnt2 = freq[ G ]; n   if (cnt1 >= 1 && cnt2 >= 1) { n     return true; n   } n   if (cnt1 == 0 && cnt2 == 0) { n     return true; n   } n   if (cnt1 == 0 && cnt2 >= 2) { n     if (freq[ R ] >= 1) { n       return true; n     } else { n       return false; n     } n   } n   if (cnt1 >= 2 && cnt2 == 0) { n     if (freq[ R ] >= 1) { n       return true; n     } else { n       return false; n     } n   } n   return false; n } n bool potG() { n   int freq[1000]; n   freq[ G ] = 0; n   freq[ B ] = 0; n   freq[ R ] = 0; n   for (int i = 0; i < s.size(); ++i) { n     ++freq[s[i]]; n   } n   int cnt1 = freq[ R ]; n   int cnt2 = freq[ B ]; n   if (cnt1 >= 1 && cnt2 >= 1) { n     return true; n   } n   if (cnt1 == 0 && cnt2 == 0) { n     return true; n   } n   if (cnt1 == 0 && cnt2 >= 2) { n     if (freq[ G ] >= 1) { n       return true; n     } else { n       return false; n     } n   } n   if (cnt1 >= 2 && cnt2 == 0) { n     if (freq[ G ] >= 1) { n       return true; n     } else { n       return false; n     } n   } n   return false; n } n bool potB() { n   int freq[1000]; n   freq[ G ] = 0; n   freq[ B ] = 0; n   freq[ R ] = 0; n   for (int i = 0; i < s.size(); ++i) { n     ++freq[s[i]]; n   } n   int cnt1 = freq[ G ]; n   int cnt2 = freq[ R ]; n   if (cnt1 >= 1 && cnt2 >= 1) { n     return true; n   } n   if (cnt1 == 0 && cnt2 == 0) { n     return true; n   } n   if (cnt1 == 0 && cnt2 >= 2) { n     if (freq[ B ] >= 1) { n       return true; n     } else { n       return false; n     } n   } n   if (cnt1 >= 2 && cnt2 == 0) { n     if (freq[ B ] >= 1) { n       return true; n     } else { n       return false; n     } n   } n   return false; n } n int main() { n   ios::sync_with_stdio(false); n   cin >> n; n   cin.get(); n   cin >> s; n   v.push_back( R ); n   v.push_back( G ); n   v.push_back( B ); n   set<char> mySet; n   if (potR()) { n     mySet.insert( R ); n   } n   if (potB()) { n     mySet.insert( B ); n   } n   if (potG()) { n     mySet.insert( G ); n   } n   for (auto i : mySet) { n     cout << i; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long binpow(long long a, long long b, long long mod) { n   long long res = 1; n   while (b != 0) { n     if (1 & b) res = (res * a) % mod; n     b >>= 1; n     a = (a * a) % mod; n   } n   return res; n } n long long mminvprime(long long a, long long b) { return binpow(a, b - 2, b); } n long long mod_add(long long a, long long b, long long m) { n   a = a % m; n   b = b % m; n   return (((a + b) % m) + m) % m; n } n long long mod_mul(long long a, long long b, long long m) { n   a = a % m; n   b = b % m; n   return (((a * b) % m) + m) % m; n } n long long mod_sub(long long a, long long b, long long m) { n   a = a % m; n   b = b % m; n   return (((a - b) % m) + m) % m; n } n long long mod_div(long long a, long long b, long long m) { n   a = a % m; n   b = b % m; n   return (mod_mul(a, mminvprime(b, m), m) + m) % m; n } n long long combination(long long n, long long r, long long m, long long *fact) { n   long long val1 = fact[n]; n   long long val2 = mod_mul(fact[n - r], fact[r], m); n   return mod_mul(val1, binpow(val2, m - 2, m), m); n } n void solve() { n   long long n; n   cin >> n; n   for (long long i = 0; i < n; i++) { n     for (long long j = 0; j < n - i; j++) { n       cout <<  ( ; n     } n     for (long long j = 0; j < n - i; j++) { n       cout <<  ) ; n     } n     for (long long j = 0; j < i; j++) { n       cout <<  () ; n     } n     cout <<   n ; n   } n } n int32_t main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   long long tt = 1; n   cin >> tt; n   while (tt--) { n     solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n signed main() { n   string s; n   cin >> s; n   long long n = s.size(); n   long long sum = 0; n   for (long long i = 0; i < n; i++) { n     sum += s[i] -  0 ; n   } n   string a = s; n   for (long long i = n - 2; i >= 0; i--) { n     if (s[i] !=  0 ) { n       string p = s; n       p[i] = p[i] - 1; n       for (long long j = i + 1; j < n; j++) { n         p[j] =  9 ; n       } n       long long su = 0; n       for (long long i = 0; i < n; i++) { n         su += p[i] -  0 ; n       } n       if (su > sum) { n         sum = su; n         a = p; n       } n     } n   } n   long long f = 0; n   for (long long i = 0; i < a.size(); i++) { n     if (a[i] !=  0 ) { n       f = 1; n       cout << a[i]; n     } n     if (a[i] ==  0  && f == 1) { n       cout << a[i]; n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n long long n, q, pre[1000000], a[1000000], f[100], vis[100], ans, rec[100], n     temp[100]; n int main() { n   cin >> n >> q; n   for (long long i = n; i >= 1; i--) pre[i] = pre[i + 1] + n - i + 1; n   f[0] = 1; n   for (long long i = 1; i <= 20; i++) f[i] = f[i - 1] * i; n   long long num = 0; n   for (long long o = 1; o <= q; o++) { n     long long tag; n     cin >> tag; n     if (tag == 1) { n       long long l, r; n       cin >> l >> r; n       if (n - r + 1 > 20) { n         cout << pre[n - r + 1] - pre[n - l + 2] << endl; n         continue; n       } n       long long t = num; n       for (long long i = min(n, (long long)20); i >= 1; i--) n         if (t >= f[i - 1]) { n           rec[i] = t / f[i - 1] + 1; n           t = t % f[i - 1]; n         } else n           rec[i] = 1; n       memset(vis, 0, sizeof vis); n       long long base = n - 20; n       if (n <= 20) base = 0; n       for (long long i = min(n, (long long)20); i >= 1; i--) { n         long long xx, cnt = 0, now = 1; n         while (cnt < rec[i]) { n           if (!vis[now]) cnt++; n           now++; n         } n         now--; n         temp[i] = now + base; n         vis[now] = 1; n       } n       for (int i = min(n, (long long)20); i >= 1; i--) n         pre[i] = pre[i + 1] + temp[i]; n       cout << pre[n - r + 1] - pre[n - l + 2] << endl; n     } else { n       long long x; n       cin >> x; n       num += x; n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); n const long long INF = 1e18; n const long long N = 1e5 + 100; n long long n; n long long a[N], dp_right[N], dp_left[N], full_right[N], full_left[N]; n signed main() { n   ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); n   cin >> n; n   for (long long i = 1; i <= n; ++i) { n     cin >> a[i]; n   } n   for (long long i = 1; i <= n; i++) { n     if (a[i] != 1) n       full_left[i] = full_left[i - 1] + (a[i] % 2 == 0 ? a[i] : a[i] - 1); n     dp_left[i] = dp_left[i - 1] + (a[i] % 2 == 0 ? a[i] - 1 : a[i]); n     dp_left[i] = max(dp_left[i], full_left[i]); n   } n   for (long long i = n; i >= 1; i--) { n     if (a[i] != 1) n       full_right[i] = full_right[i + 1] + (a[i] % 2 == 0 ? a[i] : a[i] - 1); n     dp_right[i] = dp_right[i + 1] + (a[i] % 2 == 0 ? a[i] - 1 : a[i]); n     dp_right[i] = max(dp_right[i], full_right[i]); n   } n   long long ans = 0; n   for (long long i = 1; i <= n; i++) { n     long long x = max({dp_right[i], dp_left[i], dp_left[i] + full_right[i + 1], n                        full_left[i] + dp_right[i + 1]}); n     ans = max(ans, x); n   } n   cout << ans <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long n, k, l, c, d, p, nl, np; n   cin >> n >> k >> l >> c >> d >> p >> nl >> np; n   cout << min(k * l / nl, min(c * d, p / np)) / n <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n const double Pi = acos(-1.0); n const double Eps = 1e-6; n const int Mod = (int)1e9 + 7; n long long Bigmod(long long b, long long p, long long m) { n   if (p == 0) return 1LL; n   if (p % 2 == 0) { n     long long c = Bigmod(b, p / 2, m); n     return ((c % m) * (c % m)) % m; n   } else n     return ((b % m) * (Bigmod(b, p - 1, m))) % m; n } n long long ModInverse(long long a, long long M) { return Bigmod(a, M - 2, M); } n template <class T> n inline T GCD(T x, T y) { n   if (y == 0) return x; n   return GCD(y, x % y); n } n template <class T> n inline T LCM(T x, T y) { n   return ((x / GCD(x, y)) * y); n } n bool Reverse(long long a, long long b) { return a > b; } n bool Compare(pair<long long, long long> a, pair<long long, long long> b) { n   if (a.first == b.first) return a.second > b.second; n   return a.first < b.first; n } n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   int T; n   cin >> T; n   while (T--) { n     string A, B; n     cin >> A >> B; n     if (A < B) n       cout << A << endl; n     else { n       int Pos[27]; n       for (int i = 0; i < 27; i++) Pos[i] = -1; n       for (int i = 0; i < A.size(); i++) { n         int Position = A[i] -  A  + 1; n         Pos[Position] = max(Pos[Position], i); n       } n       bool PreBreak = false, SolutionFound = false; n       for (int i = 1; i <= 26; i++) { n         if (Pos[i] == -1) continue; n         for (int j = 0; j < Pos[i]; j++) { n           if (A[j] > A[Pos[i]]) { n             string Temp = A; n             swap(Temp[j], Temp[Pos[i]]); n             if (Temp < B) { n               cout << Temp << endl; n               SolutionFound = true; n               PreBreak = true; n               break; n             } n           } n         } n         if (PreBreak) break; n       } n       if (SolutionFound == false) cout <<  ---  << endl; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n bool check(string s); n void multiple(string s, string d); n constexpr long long MAXN = 1e5 + 15; n int n; n string a[MAXN]; n string ans =  1 , s; n int main() { n   cin >> n; n   for (int i = 0; i < n; i++) cin >> a[i]; n   for (int i = 0; i < n; i++) { n     if (a[i] ==  0 ) { n       return cout <<  0 n , false; n     } else if (check(a[i])) { n       ans += a[i].substr(1, a[i].size() - 1); n     } else { n       s = a[i]; n     } n   } n   if (s ==   ) return cout << ans <<   n , false; n   s = s + ans.substr(1, ans.size() - 1); n   cout << s <<   n ; n } n bool check(string s) { n   int cnt = 0; n   for (int i = 0; i < s.size(); i++) { n     if (s[i] ==  1 ) cnt++; n     if (s[i] !=  1  && s[i] !=  0 ) return false; n   } n   return (cnt < 2 ? true : false); n } n


#include <bits/stdc++.h> n using namespace std; n const long long int MOD = 1000000007; n const long double PI = 2 * acos(0.0); n const vector<long long int> dx = {1, -1, 0, 0}; n const vector<long long int> dy = {0, 0, 1, -1}; n vector<long long int> ga(long long int n, bool oneIndexed = false) { n   vector<long long int> a; n   if (oneIndexed) a.push_back(0ll); n   for (long long int i = 0; i < n; i++) { n     long long int p; n     cin >> p; n     a.push_back(p); n   } n   return move(a); n } n vector<unsigned long long int> gau(unsigned long long int n, n                                    bool oneIndexed = false) { n   vector<unsigned long long int> a; n   if (oneIndexed) a.push_back(0llu); n   for (unsigned long long int i = 0; i < n; i++) { n     unsigned long long int p; n     cin >> p; n     a.push_back(p); n   } n   return move(a); n } n vector<string> gas(unsigned long long int n, bool oneIndexed = false) { n   vector<string> a; n   if (oneIndexed) a.push_back(  ); n   for (unsigned long long int i = 0; i < n; i++) { n     string p; n     cin >> p; n     a.push_back(p); n   } n   return move(a); n } n template <typename T, typename A> n void pa(vector<T, A> const &a, long long int begin = 0, n         long long int end = -1) { n   if (end == -1) end = (long long int)a.size() - 1; n   for (long long int i = begin; i <= end; i++) { n     cout << a[i] <<    ; n   } n   cout <<   n ; n } n template <typename T, typename A> n void papair(vector<T, A> const &a, long long int begin = 0, n             long long int end = -1) { n   if (end == -1) end = (long long int)a.size() - 1; n   for (long long int i = begin; i <= end; i++) { n     cout << a[i].first <<     << a[i].second <<   n ; n   } n   cout <<   n ; n } n void yes() { cout <<  YES n ; } n void no() { cout <<  NO n ; } n void yesno(int f) { n   if (f) n     yes(); n   else n     no(); n } n void solve() { n   vector<long long int> infreq, outfreq; n   long long int n, q; n   cin >> n >> q; n   vector<long long int> a = ga(n, 1); n   infreq = vector<long long int>(n + 1, 0); n   outfreq = vector<long long int>(n + 1, 0); n   while (q--) { n     long long int l, r; n     cin >> l >> r; n     infreq[l] += 1; n     outfreq[r] += 1; n   } n   vector<long long int> freq(n + 1, 0); n   for (long long int i = 1; i <= n; i++) { n     freq[i] += freq[i - 1]; n     freq[i] += infreq[i]; n     freq[i] -= outfreq[i - 1]; n   } n   vector<pair<long long int, long long int> > data; n   for (long long int i = 1; i <= n; i++) data.push_back({freq[i], i}); n   sort(data.begin(), data.end()); n   sort(a.begin(), a.end()); n   long long int s = 0; n   while (!data.empty()) { n     s += data.back().first * a.back(); n     a.pop_back(); n     data.pop_back(); n   } n   cout << s; n } n int main(void) { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAX = 1e5 + 1; n int plot[MAX], n, k; n void input() { n   cin >> n >> k; n   for (int i = 1; i <= n; i++) scanf( %d , &plot[i]); n } n int main() { n   input(); n   int res = 0, k1 = 0, k2 = 0; n   for (int i = 1; i <= n; i++) { n     if (plot[i] != k1) k2++; n     if (plot[i] == k1) res = max(res, k2), k2 = 1; n     k1 = plot[i]; n   } n   res = max(res, k2); n   cout << res; n } n


#include <bits/stdc++.h> n using namespace std; n const int Maxn = 100000; n struct Node { n   int t; n   int id; n }; n bool cmp(Node lhs, Node rhs) { n   return lhs.t == rhs.t ? lhs.id < rhs.id : lhs.t < rhs.t; n } n int N, P; n Node A[Maxn + 5]; n queue<int> q1; n priority_queue<int, vector<int>, greater<int> > q2; n long long ans[Maxn + 5]; n int main() { n   scanf( %d %d , &N, &P); n   for (int i = 1; i <= N; i++) { n     scanf( %d , &A[i].t); n     A[i].id = i; n   } n   sort(A + 1, A + N + 1, cmp); n   long long now = 0; n   int last = N + 1; n   for (int i = 1; i <= N;) { n     if (!q1.empty()) { n       int id = q1.front(); n       q1.pop(); n       ans[id] = now + P; n       now += P, last = id; n     } else if (A[i].t <= now) { n       if (q1.empty() && A[i].id < last) n         q1.push(A[i].id); n       else if (!q1.empty() && q1.back() > A[i].id && A[i].id < last) n         q1.push(A[i].id); n       else n         q2.push(A[i].id); n       i++; n     } else { n       if (!q2.empty()) { n         int id = q2.top(); n         q2.pop(); n         ans[id] = now + P; n         now += P, last = id; n       } else n         now = A[i].t, last = N + 1; n     } n   } n   while (!q1.empty()) { n     int id = q1.front(); n     q1.pop(), ans[id] = now + P; n     now += P; n   } n   while (!q2.empty()) { n     int id = q2.top(); n     q2.pop(), ans[id] = now + P; n     now += P; n   } n   for (int i = 1; i <= N; i++) printf( %lld  , ans[i]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long ans[100100]; n int c[100100]; n int v[100100]; n int main() { n   int n, q; n   scanf( %d%d , &n, &q); n   for (int i = (0); i < (n); ++i) scanf( %d , &v[i]); n   for (int i = (0); i < (n); ++i) { n     scanf( %d , &c[i]); n     c[i]--; n   } n   for (int i = (0); i < (q); ++i) { n     int a, b; n     scanf( %d%d , &a, &b); n     if (n == 1) { n       long long res = ((0 < v[0] * 1ll * b) ? (v[0] * 1ll * b) : (0)); n       cout << res << endl; n     } else { n       for (int i = (0); i < (n); ++i) ans[i] = -1000000000000000000ll; n       long long max1 = -1000000000000000000ll; n       long long max2 = max1; n       int v1 = 0, v2 = 1; n       for (int i = (0); i < (n); ++i) { n         long long k1 = ans[c[i]]; n         k1 += v[i] * 1ll * a; n         long long V = max2; n         if (v1 != c[i]) V = max1; n         long long k2 = V; n         k2 += v[i] * 1ll * b; n         long long k3 = v[i] * 1ll * b; n         k1 = ((k1 < k2) ? (k2) : (k1)); n         k1 = ((k1 < k3) ? (k3) : (k1)); n         if (k1 > max1) { n           if (v1 != c[i]) { n             max2 = max1; n             v2 = v1; n             max1 = k1; n             v1 = c[i]; n           } else { n             max1 = k1; n           } n         } else if (k1 > max2) { n           if (c[i] != v1) { n             max2 = k1; n             v2 = c[i]; n           } n         } n         if (ans[c[i]] < k1) ans[c[i]] = k1; n       } n       long long res = 0; n       for (int i = (0); i < (n); ++i) res = ((res < ans[i]) ? (ans[i]) : (res)); n       cout << res << endl; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T> n inline T read() { n   register T sum = 0; n   register char cc = getchar(); n   int sym = 1; n   while (cc !=  -  && (cc >  9  || cc <  0 )) cc = getchar(); n   if (cc ==  - ) sym = -1, cc = getchar(); n   sum = sum * 10 + cc -  0 ; n   cc = getchar(); n   while (cc >=  0  && cc <=  9 ) sum = sum * 10 + cc -  0 , cc = getchar(); n   return sym * sum; n } n template <typename T> n inline T read(T& a) { n   a = read<T>(); n   return a; n } n template <typename T, typename... Others> n inline void read(T& a, Others&... b) { n   a = read(a); n   read(b...); n } n int n, m, tp1, tp2, cnt, a[200010], L[200010], M[200010], R[200010], n     pre[200010], nxt[200010]; n int sta1[200010], sta2[200010], pop1[200010], pop2[200010], buck[200010], n     back[200010]; n int suf1[200010], suf2[200010], st1[20][200010], st2[20][200010]; n set<int> s; n int upper(int x) { n   int l = 1, r = tp1; n   while (l <= r) { n     int mid = (l + r) >> 1; n     if (a[sta1[mid]] > x) n       l = mid + 1; n     else n       r = mid - 1; n   } n   if (!r) return n + 1; n   return sta1[r]; n } n int lower(int x) { n   int l = 1, r = tp2; n   while (l <= r) { n     int mid = (l + r) >> 1; n     if (a[sta2[mid]] < x) n       l = mid + 1; n     else n       r = mid - 1; n   } n   if (!r) return n + 1; n   return sta2[r]; n } n int query1(int l, int r) { n   int k = log2(r - l + 1); n   int ls = st1[k][l], rs = st1[k][r - (1 << k) + 1]; n   if (back[ls] <= back[rs]) return ls; n   return rs; n } n int query2(int l, int r) { n   int k = log2(r - l + 1); n   int ls = st2[k][l], rs = st2[k][r - (1 << k) + 1]; n   if (R[ls] <= R[rs]) return ls; n   return rs; n } n int main() { n   read(n, m); n   for (int i = 1; i <= n; i++) read(a[i]); n   for (int i = 1; i <= n; i++) { n     if (a[i - 1] != a[i]) n       pre[i] = i - 1; n     else n       pre[i] = pre[i - 1]; n   } n   for (int i = n; i >= 1; i--) { n     if (a[i + 1] != a[i]) n       nxt[i] = i + 1; n     else n       nxt[i] = nxt[i + 1]; n   } n   R[0] = n + 1; n   for (int i = 1; i <= n; i++) { n     if (!pre[i] || nxt[i] == n + 1) continue; n     if (1ll * (a[i] - a[pre[i]]) * (a[i] - a[nxt[i]]) > 0) { n       cnt += 1; n       L[cnt] = pre[i]; n       M[cnt] = i; n       R[cnt] = nxt[i]; n       if (nxt[i] < R[st2[0][pre[i]]]) st2[0][pre[i]] = cnt; n     } n   } n   s.insert(n + 1); n   for (int i = n; i >= 1; i--) { n     while (tp1 && a[sta1[tp1]] < a[i]) { n       buck[sta1[tp1]] -= 1; n       pop1[sta1[tp1]] = i; n       if (!buck[sta1[tp1]]) s.insert(sta1[tp1]); n       tp1 -= 1; n     } n     sta1[++tp1] = i; n     buck[i] += 1; n     while (tp2 && a[sta2[tp2]] > a[i]) { n       buck[sta2[tp2]] -= 1; n       pop2[sta2[tp2]] = i; n       if (!buck[sta2[tp2]]) s.insert(sta2[tp2]); n       tp2 -= 1; n     } n     sta2[++tp2] = i; n     buck[i] += 1; n     suf1[i] = upper(a[i]); n     suf2[i] = lower(a[i]); n     back[i] = *s.lower_bound(max(suf1[i], suf2[i])); n   } n   for (int i = 1; i <= n; i++) st1[0][i] = i; n   for (int i = 1; (1 << i) <= n; i++) n     for (int j = 1; j + (1 << i) - 1 <= n; j++) { n       int ls = st1[i - 1][j], rs = st1[i - 1][j + (1 << (i - 1))]; n       if (back[ls] <= back[rs]) n         st1[i][j] = ls; n       else n         st1[i][j] = rs; n       ls = st2[i - 1][j], rs = st2[i - 1][j + (1 << (i - 1))]; n       if (R[ls] <= R[rs]) n         st2[i][j] = ls; n       else n         st2[i][j] = rs; n     } n   for (int i = 1; i <= m; i++) { n     int l, r; n     read(l, r); n     int mn = query1(l, r); n     if (back[mn] <= r) { n       int x = pop1[back[mn]], y = pop2[back[mn]]; n       if (a[mn] < a[back[mn]]) n         y = suf2[mn]; n       else n         x = suf1[mn]; n       printf( 4 n%d %d %d %d n , mn, min(x, y), max(x, y), back[mn]); n     } else { n       int k = query2(l, r); n       if (R[k] <= r) n         printf( 3 n%d %d %d n , L[k], M[k], R[k]); n       else n         printf( 0 n n ); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); } n int main() { n   int n; n   cin >> n; n   map<int, long long> cnt; n   map<int, long long> last; n   for (int i = 0; i < n; i++) { n     int x; n     cin >> x; n     map<int, long long> next; n     last[x]++; n     for (map<int, long long>::iterator j = last.begin(); j != last.end(); j++) { n       int g = gcd(j->first, x); n       next[g] += j->second; n       cnt[g] += j->second; n     } n     last = next; n   } n   int q; n   cin >> q; n   while (q--) { n     int x; n     cin >> x; n     cout << cnt[x] <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n const int maxn = 301, maxk = 10, maxs = 1 << 9, INF = 0x3f3f3f3f; n int n, a[maxn], c[maxn], tot, fact[maxk], f[maxs], ans = INF; n int main() { n   scanf( %d , &n); n   for (int i = 1; i <= n; ++i) scanf( %d , a + i); n   for (int i = 1; i <= n; ++i) scanf( %d , c + i); n   for (int i = 1, j, k; i <= n; ++i) { n     tot = 0; n     for (j = 2, k = a[i]; j * j <= k; ++j) n       if (k % j == 0) { n         fact[tot++] = j; n         while (k % j == 0) k /= j; n       } n     if (k > 1) fact[tot++] = k; n     memset(f, 0x3f, sizeof f); n     f[(1 << tot) - 1] = c[i]; n     for (j = 1; j <= n; ++j) { n       int mask = 0; n       for (k = 0; k < tot; ++k) n         if (a[j] % fact[k] == 0) mask |= 1 << k; n       for (int s = (1 << tot) - 1; s >= 0; --s) n         if (f[s & mask] > f[s] + c[j]) f[s & mask] = f[s] + c[j]; n     } n     if (ans > f[0]) ans = f[0]; n   } n   if (ans == INF) n     puts( -1 ); n   else n     printf( %d n , ans); n   return 0; n } n