#include <bits/stdc++.h> n using namespace std; n signed main() { n   ios_base::sync_with_stdio(0); n   cin.tie(NULL); n   int t; n   cin >> t; n   while (t--) { n     int a0, a1, a2, b0, b1, b2, ex0, ex1, ex2, ever, only0, only1, only2; n     cin >> a0 >> a1 >> a2 >> b0 >> b1 >> b2 >> ever >> ex2 >> ex1 >> only0 >> n         ex0 >> only1 >> only2; n     int s0 = ever + ex1 + ex2 + only0, s1 = ever + ex0 + ex2 + only1, n         s2 = ever + ex0 + ex1 + only2; n     if (s0 > a0 + b0 || s1 > a1 + b1 || s2 > a2 + b2) { n       cout <<  -1 n ; n       continue; n     } n     int anime = ever + ex0 + ex1 + ex2; n     bool ok = false; n     for (int i = 0; i <= anime; i++) { n       int luex0 = i + max(-a0, ex0 - anime), n           ruex0 = ex0 + min(0, b0 + i - anime), n           luex1 = i + max(-a1, ex1 - anime), n           ruex1 = ex1 + min(0, b1 + i - anime), n           luex2 = i + max(-a2, ex2 - anime), n           ruex2 = ex2 + min(0, b2 + i - anime); n       luex0 = max(0, luex0); n       luex1 = max(0, luex1); n       luex2 = max(0, luex2); n       if (luex0 > ruex0 || luex1 > ruex1 || luex2 > ruex2 || n           min(i, ruex0 + ruex1 + ruex2) < n               max(max(0, i - ever), luex0 + luex1 + luex2)) n         continue; n       int uex0 = max(luex0, max(0, i - ever) - ruex1 - ruex2), n           uex1 = max(luex1, max(0, i - ever) - uex0 - ruex2), n           uex2 = max(luex2, max(0, i - ever) - uex0 - uex1); n       int uonly0 = min(only0, a0 - i + uex0), n           uonly1 = min(only1, a1 - i + uex1), n           uonly2 = min(only2, a2 - i + uex2); n       ok = true; n       cout << i - uex0 - uex1 - uex2 <<     << uex2 <<     << uex1 <<     n            << uonly0 <<     << uex0 <<     << uonly1 <<     << uonly2 <<   n ; n       break; n     } n     if (!ok) cout <<  -1 n ; n   } n   return 0; n } n


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


#include <bits/stdc++.h> n using namespace std; n struct point { n   int x, y, z; n   double dist(point& p) { n     return sqrt((double)((p.x - x) * (p.x - x) + (p.y - y) * (p.y - y) + n                          (p.z - z) * (p.z - z))); n   } n }; n int N; n point P[5005]; n double solve() { n   double res = 1e10; n   for (int i = 1; i < N; i++) { n     for (int j = i + 1; j < N; j++) { n       double per = P[i].dist(P[j]) + P[i].dist(P[0]) + P[j].dist(P[0]); n       if (per < res) res = per; n     } n   } n   return res / 2.0; n } n int main() { n   while (cin >> N) { n     for (int i = 0; i < N; i++) { n       cin >> P[i].x >> P[i].y >> P[i].z; n     } n     cout << setprecision(12) << solve() << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n inline vector<int> prefix(string p) { n   vector<int> pi; n   int m = p.size(); n   for (int(i) = (0); (i) < (m); ++(i)) pi.push_back(-1); n   int k = -1; n   for (int(i) = (1); (i) < (m); ++(i)) { n     while (k != -1 && p[k + 1] != p[i]) k = pi[k]; n     if (p[k + 1] == p[i]) k++; n     pi[i] = k; n   } n   return pi; n } n set<int> s; n inline void shift(string p) { n   vector<int> pi = prefix(p); n   int m = p.size(); n   int k = pi[m - 1]; n   while (k != -1) { n     s.insert(m - k); n     k = pi[k]; n   } n } n int main() { n   ios_base::sync_with_stdio(false); n   long long n, m; n   cin >> n >> m; n   string p; n   cin >> p; n   vector<int> pi = prefix(p); n   shift(p); n   vector<int> v; n   for (int(i) = (0); (i) < (m); ++(i)) { n     int a; n     cin >> a; n     v.push_back(a); n   } n   bool ok = true; n   for (int(i) = (1); (i) < (v.size()); ++(i)) { n     long long r = v[i], l = v[i - 1]; n     if (r < (l + p.size()) && s.find((r - l + 1)) == s.end()) { n       cout << 0 << endl; n       ok = false; n       break; n     } n     if ((p.size() + r - 1) > n && ok) { n       cout << 0 << endl; n       ok = false; n       break; n     } n   } n   long long first = n; n   if (ok) { n     v.push_back(2000000000); n     for (int(i) = (0); (i) < (v.size() - 1); ++(i)) n       first -= min((int)p.size(), v[i + 1] - v[i]); n   } n   long long ans = 1; n   if (first < 0 && ok) { n     cout << 0 << endl; n     ok = false; n   } n   for (int(i) = (0); (i) < (first); ++(i)) n     ans = (ans * (long long)26) % 1000000007; n   if (ok) cout << ans << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, oh, wow; n   cin >> n >> oh >> wow; n   vector<int> res; n   res.push_back(1); n   int sum = 1; n   for (int i = 0; i < wow; i++) { n     res.push_back(sum + 1); n     sum += sum + 1; n   } n   if (!wow && oh) res.push_back(1); n   for (int i = 0; i < oh; i++) { n     res.push_back(res.back() + 1); n   } n   while ((int)res.size() < n) res.push_back(1); n   if ((int)res.size() > n) { n     cout <<  -1 n ; n     return 0; n   } n   for (int i = 0; i < n; i++) n     if (res[i] > 50000) { n       cout <<  -1 n ; n       return 0; n     } n   for (int i = 0; i < n; i++) { n     if (i) cout <<    ; n     cout << res[i]; n   } n   cout <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n long long rdtsc() { n   long long tmp; n   asm( rdtsc  :  =A (tmp)); n   return tmp; n } n inline int myrand() { return abs((rand() << 15) ^ rand()); } n inline int rnd(int x) { return myrand() % x; } n const int INF = (int)1e9 + 1; n const long double EPS = 1e-9; n void precalc() {} n int gcd(int a, int b) { n   for (; b; a %= b, swap(a, b)) n     ; n   return a; n } n int getFunc(const pair<int, int> &a, const pair<int, int> &b) { n   return gcd(abs(a.first - b.first), abs(a.second - b.second)); n } n long long inside(int n, pair<int, int> *ps, long long G = -1) { n   if (n < 3) { n     return 0; n   } n   bool needG = (G == -1); n   if (needG) { n     G = 0; n   } n   long long S2 = 0; n   for (int i = 0; i < n; ++i) { n     int in = (i == n - 1 ? 0 : (i + 1)); n     int dx = ps[in].first - ps[i].first; n     S2 += (long long)dx * (ps[in].second + ps[i].second); n     if (needG) { n       G += getFunc(ps[i], ps[in]); n     } n   } n   S2 = abs(S2); n   long long res = S2 + 2 - G; n   assert(!(res & 1)); n   return res / 2; n } n const int maxn = (int)1e5 + 10; n pair<int, int> ps[maxn * 2]; n int n; n bool read() { n   if (scanf( %d , &n) < 1) { n     return 0; n   } n   for (int i = 0; i < n; ++i) { n     scanf( %d%d , &ps[i].first, &ps[i].second); n   } n   return 1; n } n long long cnts[maxn]; n int gcdn[maxn * 2]; n int gcdc[maxn]; n void solve() { n   for (int i = 0; i < n; ++i) { n     cnts[i] = 0; n     ps[n + i] = ps[i]; n   } n   for (int i = 0; i < n; ++i) { n     gcdc[i] = gcdn[i] = gcdn[i + n] = getFunc(ps[i], ps[i + 1]); n   } n   long long whole = inside(n, ps); n   long double up, down; n   long double prob; n   if (n <= 100) { n     up = 1; n     for (int i = 0; i < n; ++i) { n       up *= 2; n     } n     down = up - (long double)(n * (n - 1) / 2 + n + 1); n     up /= 8; n     up -= 1; n   } else { n     prob = 1; n     prob /= 8; n   } n   pair<int, int> tmp[3]; n   long double res = whole; n   for (int k = 3; k < min(60, n); ++k) { n     assert(k <= 150); n     if (n <= 100) { n       prob = up / down; n     } n     for (int i = 0; i < n; ++i) { n       long long &cnt = cnts[i]; n       tmp[0] = ps[i], tmp[1] = ps[i + k - 2], tmp[2] = ps[i + k - 1]; n       int ngcd = getFunc(ps[i], ps[i + k - 1]); n       cnt += inside(3, tmp, (long long)gcdc[i] + gcdn[i + k - 2] + ngcd) + n              ngcd - 1; n       gcdc[i] = ngcd; n       res -= prob * cnt; n     } n     if (n <= 100) { n       up -= 1; n       up /= 2; n     } else { n       prob /= 2; n     } n   } n   printf( %.18f n , (double)res); n } n int main() { n   srand(rdtsc()); n   precalc(); n   while (1) { n     if (!read()) { n       break; n     } n     solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int a[1000010]; n long long cnt[1000010]; n int main() { n   ios::sync_with_stdio(0); n   int n; n   cin >> n; n   long long s = 0; n   for (int i = 0; i < n; ++i) { n     cin >> a[i]; n     s += a[i]; n   } n   if (s % 3 != 0) n     cout <<  0 n ; n   else { n     s /= 3; n     long long ss = 0; n     for (int i = n - 1; i >= 0; --i) { n       ss += a[i]; n       if (ss == s) cnt[i] = 1; n     } n     for (int i = n - 2; i >= 0; --i) cnt[i] += cnt[i + 1]; n     long long ans = 0; n     ss = 0; n     for (int i = 0; i + 2 < n; ++i) { n       ss += a[i]; n       if (ss == s) ans += cnt[i + 2]; n     } n     cout << ans <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main(int argc, char** argv) { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   int x1, y1, x2, y2; n   cin >> x1 >> y1 >> x2 >> y2; n   double slope; n   slope = x2 == x1 ? INT_MAX : (double)(y2 - y1) / (double)(x2 - x1); n   if (slope == 0 || abs(slope) == 1 || abs(slope) == INT_MAX) { n     if (slope == 0) { n       int sideSq = abs(x2 - x1); n       int next_y = y1 + sideSq; n       if (next_y > 1000) { n         next_y = y1 - sideSq; n         if (next_y < -1000) { n           cout << -1 <<   n ; n           return 0; n         } n       } n       cout << x1 <<     << next_y <<     << x2 <<     << next_y <<   n ; n     } else if (slope == INT_MAX) { n       int sideSq = abs(y2 - y1); n       int next_x = x1 + sideSq; n       if (next_x > 1000) { n         next_x = x1 - sideSq; n         if (next_x < -1000) { n           cout << -1 <<   n ; n           return 0; n         } n       } n       cout << next_x <<     << y1 <<     << next_x <<     << y2 <<   n ; n     } else { n       cout << x1 <<     << y2 <<     << x2 <<     << y1 <<   n ; n     } n   } else { n     cout << -1 <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int N, M; n int For[100000], Against[100000]; n int TotalFor = 0, TotalAgainst = 0; n int Vote[100000]; n int Sign[100000]; n int J[100000]; n int R[100000]; n void Solve() { n   int c = 0; n   for (int i = 0; i < N; i++) { n     J[i] = true; n     if (For[i] + TotalAgainst - Against[i] != M) { n       J[i] = false; n     } else { n       J[i] = true; n       c++; n     } n   } n   for (int i = 0; i < N; i++) { n     if (!J[Vote[i]]) { n       if (Sign[i] > 0) n         R[i] = 2; n       else n         R[i] = 1; n     } else if (c == 1) { n       if (Sign[i] > 0) n         R[i] = 1; n       else n         R[i] = 2; n     } n     if (R[i] == 0) n       printf( Not defined n ); n     else if (R[i] == 1) n       printf( Truth n ); n     else n       printf( Lie n ); n   } n } n int main() { n   scanf( %d %d , &N, &M); n   char c; n   for (int i = 0; i < N; i++) { n     scanf(  %c%d , &c, &Vote[i]); n     Vote[i]--; n     if (c ==  + ) { n       For[Vote[i]]++; n       Sign[i] = 1; n       TotalFor++; n     } else { n       Against[Vote[i]]++; n       Sign[i] = -1; n       TotalAgainst++; n     } n   } n   Solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 3e5 + 10; n long long a[maxn]; n int main() { n   long long n, k, x; n   cin >> n >> k; n   for (int i = 1; i <= n; i++) { n     cin >> x; n     a[i] = a[i - 1] + x; n   } n   long long ans = a[n] * k; n   sort(a + 1, a + n); n   for (int i = 1; i < k; i++) ans -= a[i]; n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n  n using namespace std; n  n typedef long long ll; n typedef pair<int, int> pii; n  n const int N = 510; n const int MOD = 998244353; n  n int a[N]; n int b[N]; n int dp[N][N]; n  n void add_self(int& x, int y) { n     if ((x += y) >= MOD) { n         x -= MOD; n     } n } n  n void sub_self(int& x, int y) { n     if ((x -= y) < 0) { n         x += MOD; n     } n } n  n int add(int x, int y) { n     return add_self(x, y), x; n } n  n int sub(int x, int y) { n     return sub_self(x, y), x; n } n  n int mul(int x, int y) { n     return (ll)x * y % MOD; n } n  n int main() { n     int n; n     cin >> n; n  n     for (int i = 0; i < n; i++) { n         char c; n         scanf(  %c , &c); n         if (c ==  + ) { n             b[i] = 1; n             scanf( %d , a + i); n         } n     } n  n     int answer = 0; n  n     for (int k = 0; k < n; k++) { n         if (!b[k]) { n             continue; n         } n  n         memset(dp, 0, sizeof(dp)); n  n         for (int j = 0; j <= n; j++) { n             dp[n][j] = 1; n         } n  n         for (int i = n - 1; ~i; --i) { n             for (int j = 0; j < n; j++) { n                 if (i == k) { n                     dp[i][j] = dp[i + 1][j]; n                 } else { n                     dp[i][j] = dp[i + 1][j]; n                     if (b[i]) { n                         add_self(dp[i][j], dp[i + 1][j + (pii(a[i], i) < pii(a[k], k))]); n                     } else if (i < k || j > 0) { n                         add_self(dp[i][j], dp[i + 1][max(0, j - 1)]); n                     } n                 } n             } n         } n  n         add_self(answer, mul(a[k], dp[0][0])); n     } n  n     cout << answer << endl; n }


#include <bits/stdc++.h> n using namespace std; n  n const int N = 102; n int t, n, m, r[N], b[N]; n  n int main() { n  ios::sync_with_stdio(0); n  cin.tie(0); n  cin >> t; n  for(int ti = 1; ti <= t; ++ti) { n   cin >> n; n   for(int i = 1; i <= n; ++i) { n    cin >> r[i]; n    r[i] += r[i - 1]; n   } n   cin >> m; n   for(int i = 1; i <= m; ++i) { n    cin >> b[i]; n    b[i] += b[i - 1]; n   } n   int fp = 0; n   for(int i = 0; i <= n; ++i) { n    for(int j = 0; j <= m; ++j) { n     fp = max(fp, r[i] + b[j]); n    } n   } n   cout << fp <<   n ; n  } n  return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxi = 1e6 + 6; n int b, di; n string a, c, t; n pair<int, int> d[maxi], d1[maxi]; n int ob[maxi]; n int main() { n   cin >> b >> di; n   cin >> a; n   cin >> c; n   int n = a.size(); n   int m = c.size(); n   for (int i = 0; i < m; i++) { n     int id = 0; n     for (int j = 0; j < n; j++) n       if (c[i] == a[j]) id++; n     if (!id) { n       printf( %d n , 0); n       return 0; n     } n   } n   for (int i = 0; i < (m + 1); i++) t += a; n   int cur = 0; n   for (int j = 0; j < n; j++) { n     cur = 0; n     for (int i = j; i < (m + 1) * n; i++) { n       if (c[cur] == t[i]) cur++; n       if (cur == m) { n         d[j] = {i / n, i % n}; n         break; n       } n     } n   } n   for (int j = 0; j < n; j++) { n     int posl = -1; n     int cur = 0; n     int tt = 0; n     for (int k = j; k < n; k++) { n       if (a[k] == c[cur]) cur++; n       if (cur == m) { n         posl = k; n         cur = 0; n         tt++; n       } n     } n     d1[j] = {tt, posl}; n   } n   int tot = 0; n   int ans = 0; n   int c1 = 0; n   while (ans < b) { n     if (d[c1].first > 0) { n       tot++; n       ans += d[c1].first; n       c1 = d[c1].second; n       c1++; n       if (c1 == n) { n         c1 -= n; n         ans++; n         if (ans == b) { n           printf( %d n , tot / di); n           return 0; n         } n       } n     } else { n       tot += d1[c1].first; n       c1 = d1[c1].second; n       c1++; n       if (c1 == n) { n         c1 -= n; n         ans++; n         if (ans == b) { n           printf( %d n , tot / di); n           return 0; n         } n       } n     } n   } n   tot--; n   cout << tot / di <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, k; n   cin >> n; n   cin >> k; n   vector<string> s(n); n   set<string> exist; n   for (int i = 0; i < n; i++) { n     cin >> s[i]; n     exist.insert(s[i]); n   } n   long long int count = 0; n   for (int x = 0; x < n - 1; x++) { n     for (int y = x + 1; y < n; y++) { n       string temp =   ; n       for (int i = 0; i < k; i++) { n         if (s[x][i] == s[y][i]) { n           temp += s[x][i]; n         } else if ((s[x][i] ==  S  && s[y][i] ==  T ) || n                    (s[x][i] ==  T  && s[y][i] ==  S )) { n           temp +=  E ; n         } else if ((s[x][i] ==  S  && s[y][i] ==  E ) || n                    (s[x][i] ==  E  && s[y][i] ==  S )) { n           temp +=  T ; n         } else if ((s[x][i] ==  E  && s[y][i] ==  T ) || n                    (s[x][i] ==  T  && s[y][i] ==  E )) { n           temp +=  S ; n         } n       } n       if (exist.find(temp) != exist.end()) { n         count++; n       } n     } n   } n   cout << count / 3 << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n inline long long max(long long a, long long b) { return a > b ? a : b; } n inline long long min(long long a, long long b) { return a > b ? b : a; } n template <typename T> n inline int read(T& res) { n   long long x = 0, f = 1, flag = 0; n   char ch; n   flag = ch = getchar(); n   if (flag == -1) return -1; n   while (ch <  0  || ch >  9 ) { n     if (ch ==  - ) f = -1; n     flag = ch = getchar(); n     if (flag == -1) return -1; n   } n   while (ch >=  0  && ch <=  9  && flag != -1) { n     x = (x << 1) + (x << 3) + (ch ^ 48); n     flag = ch = getchar(); n   } n   res = x * f; n   return flag; n } n template <typename T, typename... Args> n inline int read(T& t, Args&... a) { n   int res; n   res = read(t); n   if (res == -1) return -1; n   res = read(a...); n   return res; n } n const int maxn = 5511, mod = 1e9 + 9; n long long n, m, k; n long long qpow(long long x, long long y) { n   long long ans = 1; n   while (y) { n     if (y & 1) ans = (ans * x) % mod; n     y >>= 1; n     x = (x * x) % mod; n   } n   return ans % mod; n } n long long A1, A2, C1[maxn], C2[maxn], w, b; n inline void solve() { n   long long ans = 0, mx = max(w, b); n   A1 = A2 = 1; n   for (int i = 2; i <= w; i++) A1 = (A1 * i) % mod; n   for (int i = 2; i <= b; i++) A2 = (A2 * i) % mod; n   C1[2] = w - 1; n   C2[2] = b - 1; n   C1[1] = C2[1] = 1; n   for (int i = 3; i <= mx; i++) { n     long long inv = qpow(i - 1, mod - 2); n     C1[i] = (C1[i - 1] * (w - i + 1) % mod * inv) % mod, n     C2[i] = (C2[i - 1] * (b - i + 1) % mod * inv) % mod; n   } n   int lim = max(1, n - w); n   for (int x = min(n - 2, b); x >= lim; x--) { n     ans = (ans + (((((n - x - 1) * A1 % mod) * C1[n - x] % mod) * A2) % mod) * n                      C2[x]) % n           mod; n   } n   cout << ans << endl; n } n int main() { n   ios::sync_with_stdio(0), cin.tie(0), cout.tie(0); n   int t = 1; n   while (cin >> n >> w >> b) solve(); n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, t; n   scanf( %d%d , &n, &t); n   int x, y; n   vector<pair<int, int> > v(n); n   for (int i = 0; i < (int)(n); ++i) { n     scanf( %d%d , &x, &y); n     v[i].first = x; n     v[i].second = y; n   } n   int a[4] = {v[0].first, v[0].second, 7 - v[0].first, 7 - v[0].second}; n   bool f = true; n   for (int i = 0; i < (int)(n); ++i) { n     if (find(a, a + 4, v[i].first) == a + 4 or n         find(a, a + 4, v[i].second) == a + 4) n       f = false; n   } n   cout << (f ?  YES  :  NO ); n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T> n void deb(T v[], long long n) { n   if (false) cerr <<  ARRAY   :  ; n   long long i; n   for (i = 0; i < n; i++) n     if (false) cerr << v[i] <<    ; n   if (false) cerr << endl; n } n template <typename T> n void deb(set<T> s) { n   if (false) cerr <<  SET     :  ; n   for (auto it : s) n     if (false) cerr << it <<    ; n   if (false) cerr << endl; n } n template <typename T> n void deb(vector<T> v) { n   if (false) cerr <<  VECTOR :  ; n   long long i; n   for (i = 0; i < v.size(); i++) n     if (false) cerr << v[i] <<    ; n   if (false) cerr << endl; n } n template <typename T> n void deb(stack<T> s) { n   if (false) cerr <<  STACK   :  ; n   stack<T> st = s; n   while (st.size()) { n     if (false) cerr << st.top() <<    ; n     st.pop(); n   }; n   if (false) cerr << endl; n } n template <typename T> n void deb(queue<T> q) { n   if (false) cerr <<  Queue   :  ; n   queue<T> qq = q; n   while (qq.size()) { n     if (false) cerr << qq.front() <<    ; n     qq.pop(); n   }; n   if (false) cerr << endl; n } n void deb(map<long long, long long> m) { n   if (false) cerr <<  MAP :  ; n   for (auto it : m) n     if (false) cerr << it.first <<   :   << it.second << endl; n } n void deb(unordered_map<long long, long long> m) { n   if (false) cerr <<  MAP :  ; n   for (auto it : m) n     if (false) cerr << it.first <<   :   << it.second << endl; n } n template <typename T> n T abs(T a) { n   return a < 0 ? a * -1 : a; n } n void swap(long long &x, long long &y); n bool isPrime(long long n); n long long power(long long x, long long y, long long p); n long long gcdExtended(long long a, long long b, long long *x, long long *y); n long long modInverse(long long b, long long m); n long long modDivide(long long a, long long b, long long m); n void simpleSieve(long long N, bool[]); n void PrimeDivisorSieve(long long N, long long prime[]); n class nCrModPClass { n  public: n   vector<long long> f, fi; n   long long N; n   long long MOD; n   long long poww(long long a, long long b, long long m) { n     long long ans = 1; n     while (b) { n       if (b & 1) ans = (ans * a) % m; n       b /= 2; n       a = (a * a) % m; n     } n     return ans; n   } n   long long modinvv(long long k) { return poww(k, MOD - 2, MOD); } n   nCrModPClass(long long size, long long mod) { n     MOD = mod; n     N = size + 10; n     f.resize(N); n     fi.resize(N); n     f[0] = f[1] = 1; n     for (long long i = 2; i < N; i++) f[i] = (f[i - 1] * i) % MOD; n     fi[N - 1] = modinvv(f[N - 1]); n     for (long long i = N - 2; i >= 0; i--) fi[i] = (fi[i + 1] * (i + 1)) % MOD; n   } n   long long nCrModPfromClass(long long n, long long r) { n     if (r > n) return 0; n     long long ret = f[n]; n     ret %= MOD; n     ret *= fi[r]; n     ret %= MOD; n     ret *= fi[n - r]; n     ret %= MOD; n     return ret; n   } n }; n long long testCases; n long long minTocs(string a, char c) { n   if (a.length() == 1) { n     return a[0] != c; n   } n   string lef, rig; n   long long dlef, drig; n   dlef = drig = 0; n   for (long long i = 0; i < a.length() / 2; i++) { n     dlef += a[i] != c; n     lef += a[i]; n   } n   for (long long i = a.length() / 2; i < a.length(); i++) { n     drig += a[i] != c; n     rig += a[i]; n   } n   return min(dlef + minTocs(rig, (char)c + 1), n              minTocs(lef, (char)c + 1) + drig); n } n int main() { n   testCases = 1; n   cin >> testCases; n   while (testCases--) { n     long long n; n     cin >> n; n     string s; n     cin >> s; n     cout << minTocs(s,  a ) << endl; n   } n   return 0; n } n void simpleSieve(long long N, bool prime[]) { n   long long i, j; n   for (i = 0; i < N + 1; i++) prime[i] = true; n   for (i = 2; i * i <= N; i++) { n     if (prime[i]) { n       for (j = i * i; j <= N; j += i) { n         prime[j] = false; n       } n     } n   } n } n void PrimeDivisorSieve(long long N, long long prime[]) { n   long long i, j; n   for (i = 0; i < N + 1; i++) prime[i] = i; n   for (i = 2; i * i <= N; i++) { n     if (prime[i] == i) { n       for (j = i; j <= N; j += i) { n         if (prime[j] == j) prime[j] = i; n       } n     } n   } n } n void swap(long long &x, long long &y) { n   x ^= y; n   y ^= x; n   x ^= y; n } n bool isPrime(long long n) { n   if (n <= 1) return false; n   for (long long i = 2; i < n; i++) n     if (n % i == 0) return false; n   return true; n } n long long power(long long x, long long y, long long p) { n   long long res = 1; n   x = x % p; n   while (y > 0) { n     if (y & 1) res = (res * x) % p; n     y = y >> 1; n     x = (x * x) % p; n   } n   return res; n } n long long gcdExtended(long long a, long long b, long long *x, long long *y) { n   if (a == 0) { n     *x = 0, *y = 1; n     return b; n   } n   long long x1, y1; n   long long gcd = gcdExtended(b % a, a, &x1, &y1); n   *x = y1 - (b / a) * x1; n   *y = x1; n   return gcd; n } n long long modInverse(long long b, long long m) { n   long long x, y; n   long long g = gcdExtended(b, m, &x, &y); n   if (g != 1) return -1; n   return (x % m + m) % m; n } n long long modDivide(long long a, long long b, long long m) { n   a = a % m; n   long long inv = modInverse(b, m); n   if (inv == -1) n     return -1; n   else n     return (inv * a) % m; n } n


#include <bits/stdc++.h> n using namespace std; n int n; n int m; n int p[100]; n int f() { n   int r = 0; n   for (int i = 0; i < n; i++) { n     int m = p[i]; n     for (int j = i; j < n; j++) { n       if (m > p[j]) m = p[j]; n       r += m; n     } n   } n   return r; n } n void print() { n   for (int i = 0; i < n; i++) { n     cout << p[i] <<    ; n   } n   cout << endl; n } n int main() { n   scanf( %d %d , &n, &m); n   for (int i = 99; i >= 0; i--) { n     p[i] = i + 1; n   } n   int ma = f(); n   iota(p, p + 100, 1); n   int i = 0; n   do { n     if (f() == ma) { n       i++; n       if (i == m) { n         print(); n         break; n       } n     } n   } while (next_permutation(p, p + n)); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n string a, b, c, d; n int pref[123456]; n int main() { n   int n, k; n   cin >> n >> k; n   string s; n   cin >> s; n   s =  .  + s; n   for (int i = 1; i < s.length(); i++) { n     pref[i] = pref[i - 1]; n     if (s[i] ==  0 ) pref[i]++; n   } n   int ans = INT_MAX; n   for (int i = 1; i < s.length(); i++) { n     if (s[i] ==  0 ) { n       int lo = k / 2; n       int hi = s.length() - 1; n       while (hi - lo > 2) { n         int mid = (lo + hi) / 2; n         int lft = max(0, i - mid); n         int cccc = s.length() - 1; n         int rt = min(cccc, i + mid); n         if (pref[rt] - pref[lft - 1] < k + 1) n           lo = mid; n         else n           hi = mid; n       } n       while (lo <= hi) { n         int lft = max(0, i - lo); n         int cccc = s.length() - 1; n         int rt = min(cccc, i + lo); n         if (pref[rt] - pref[lft - 1] < k + 1) lo++; n         if (pref[rt] - pref[lft - 1] >= k + 1) break; n       } n       ans = min(ans, lo); n     } n   } n   cout << ans; 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   int a[100005]; n   for (int i = 0; i < n; i++) cin >> a[i]; n   sort(a, a + n); n   int inter = a[1] - a[0]; n   for (int i = 2; i < n; i++) { n     inter = gcd(inter, a[i] - a[i - 1]); n   } n   int cnt = 0; n   for (int i = 1; i < n; i++) { n     cnt += (a[i] - a[i - 1]) / inter - 1; n   } n   printf( %d n , cnt); n } n


#include <bits/stdc++.h> n using namespace std; n struct Meth { n   int hud, ten; n   void init(int hud, int ten) { this->hud = hud, this->ten = ten; } n   bool operator==(const class Meth &ans) const { n     return hud == ans.hud && ten == ans.ten; n   } n }; n bool cmpC(const struct Meth &ans1, const struct Meth &ans2) { n   return ans1.hud > ans2.hud; n } n bool cmpH(const struct Meth &ans1, const struct Meth &ans2) { n   return ans1.ten > ans2.ten; n } n struct Comb { n   Meth ci, ha; n   int chud, cten; n   void init(Meth a, Meth b) { n     ci.init(a.hud, a.ten), ha.init(b.hud, b.ten); n     chud = ci.hud + ha.hud, cten = ci.ten + ha.ten; n   } n   bool operator<(const class Comb &ans) const { n     return (cmpC(ci, ans.ci) || (ci == ans.ci && cmpH(ha, ans.ha))); n   } n }; n Meth mat[3]; n Comb comb[9]; n int cnt, cnthud, cntten; n void Prepare() { n   for (int i = 0; i < 3; i++) mat[i].init(i, 22 - i * 10); n   cnt = 0; n   for (int i = 0; i < 3; i++) n     for (int j = 0; j < 3; j++) comb[cnt++].init(mat[i], mat[j]); n   sort(comb, comb + cnt); n } n int main() { n   Prepare(); n   while (scanf( %d%d , &cnthud, &cntten) != EOF) { n     for (int i = 0; i < cnt; i++) { n       if (cnthud >= comb[i].chud && cntten >= comb[i].cten) { n         int a = cntten / comb[i].cten; n         if (comb[i].chud != 0) a = min(a, cnthud / comb[i].chud); n         cnthud -= a * comb[i].chud, cntten -= a * comb[i].cten; n       } n     } n     if (cnthud * 100 + cntten * 10 >= 220 && cntten >= 2) { n       printf( Ciel n ); n     } else { n       printf( Hanako n ); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n #pragma GCC optimize( Ofast ) n #pragma GCC target( avx,avx2,fma ) n using namespace std; n const long long MOD = 1000000007; n const long long mod = 1000000007; n const int Nmax = 2000006; n long long gcd(long long a, long long b) { n   if (a == 0) { n     return b; n   } n   return gcd(b % a, a); n } n int lcm(int a, int b) { return (a * b) / gcd(a, b); } n bool isPrime(long long n) { n   for (int i = 2; i <= sqrt(n); i++) n     if (n % i == 0) return 0; n   return 1; n } n void binaryRepresentation(int x) { n   for (int i = 31; i >= 0; i--) { n     if (x & (1 << i)) n       cout <<  1 ; n     else n       cout <<  0 ; n   } n } n long long combination(long long n, long long r) { n   if (r == n || r == 0) { n     return 1LL; n   } n   return combination(n - 1, r - 1) + combination(n - 1, r); n } n bool isPowerOfTwo(long long n) { n   if (n == 0) return false; n   return (ceil(log2(n)) == floor(log2(n))); n } n long long power(long long a, long long b) { n   long long ans = 1; n   while (b) { n     if (b & 1) ans = (ans * a) % MOD; n     b /= 2; n     a = (a * a) % MOD; n   } n   return ans % MOD; n } n long long inverse(long long i) { n   if (i == 1) return 1; n   return (MOD - ((MOD / i) * inverse(MOD % i)) % MOD + MOD) % MOD; n } n bool cmp(const pair<long long, long long> &a, n          const 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 string toString(long long a) { n   ostringstream temp; n   temp << a; n   return temp.str(); n } n int main() { n   long long n, k; n   cin >> n >> k; n   long long ct = 1; n   if (k == 1) { n     cout << n; n     return 0; n   } n   while (ct <= n) { n     ct = ct * 2; n   } n   cout << ct - 1; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, k; n   scanf( %d%d , &n, &k); n   char a[4][n]; n   for (int i = 0; i < 4; i++) n     for (int j = 0; j < n; j++) a[i][j] =  . ; n   if (!k) { n     printf( YES n ); n     for (int i = 0; i < 4; i++) { n       for (int j = 0; j < n; j++) printf( . ); n       printf(  n ); n     } n     return 0; n   } n   if (k == 2 * (n - 2)) { n     printf( YES n ); n     for (int i = 0; i < 4; i++) { n       for (int j = 0; j < n; j++) { n         if (i == 0 || i == 3 || j == 0 || j == n - 1) n           printf( . ); n         else n           printf( # ); n       } n       printf(  n ); n     } n     return 0; n   } n   if (k > 2 * (n - 2)) { n     printf( NO n ); n     return 0; n   } else n     printf( YES n ); n   if (k % 2 == 0) { n     for (int i = 1; i < 3 && k > 0; i++) n       for (int j = 1; j < n / 2 && k > 0; j++) { n         a[i][j] = a[i][n - j - 1] =  # ; n         k -= 2; n       } n   } else { n     for (int i = 1; i < 3 && k > 1; i++) n       for (int j = 1; j < n / 2 && k > 1; j++) { n         a[i][j] = a[i][n - j - 1] =  # ; n         k -= 2; n       } n     a[1][n / 2] =  # ; n   } n   for (int i = 0; i < 4; i++) { n     for (int j = 0; j < n; j++) printf( %c , a[i][j]); n     printf(  n ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const double eps = 1e-7; n int ary[10005]; n int bry[10005]; n vector<pair<int, int> > ans; n int pivot[31], pid[31]; n int pivot2[31], pid2[31]; n bool vis[10005]; n void solve(int n, int a[]) { n   for (int i = 0; i < 31; ++i) pid[i] = -1; n   for (int k = 0; k < n; ++k) { n     int v = a[k]; n     for (int i = 31 - 1; i >= 0; --i) { n       if (pid[i] == -1) continue; n       if (v & (1 << i)) v ^= pivot[i], ans.push_back(make_pair(k, pid[i])); n     } n     if (v == 0) continue; n     for (int i = 31 - 1; i >= 0; --i) { n       if (v & (1 << i)) { n         assert(pid[i] == -1); n         pivot[i] = v, pid[i] = k; n         for (int j = 0; j < 31; ++j) n           if (pid[j] != -1 and i != j and pivot[j] & (1 << i)) n             pivot[j] ^= pivot[i], ans.push_back(make_pair(pid[j], pid[i])); n         break; n       } n     } n   } n } n bool solve3(vector<pair<int, int> > candi); n bool solve2(int n, int a[]) { n   for (int k = 0; k < n; ++k) { n     if (vis[k]) continue; n     int v = a[k]; n     for (int i = 31 - 1; i >= 0; --i) { n       if (pid[i] == -1) continue; n       if (v & (1 << i)) v ^= pivot[i], ans.push_back(make_pair(k, pid[i])); n     } n     if (v != 0) return false; n   } n   vector<pair<int, int> > candi; n   for (int i = 31 - 1; i >= 0; --i) n     if (pid[i] != -1) candi.push_back(make_pair(a[pid[i]], pid[i])); n   return solve3(candi); n } n bool solve3(vector<pair<int, int> > candi) { n   vector<pair<int, int> > buf; n   for (int i = 0; i < 31; ++i) pid2[i] = -1; n   for (const auto &p : candi) { n     int k = p.second; n     int v = p.first; n     for (int i = 31 - 1; i >= 0; --i) { n       if (pid2[i] == -1) continue; n       if (v & (1 << i)) v ^= pivot2[i], buf.push_back(make_pair(k, pid2[i])); n     } n     if (v == 0) continue; n     for (int i = 31 - 1; i >= 0; --i) { n       if (v & (1 << i)) { n         assert(pid2[i] == -1); n         pivot2[i] = v, pid2[i] = k; n         for (int j = 0; j < 31; ++j) n           if (pid2[j] != -1 and i != j and pivot2[j] & (1 << i)) n             pivot2[j] ^= pivot2[i], buf.push_back(make_pair(pid2[j], pid2[i])); n         break; n       } n     } n   } n   for (int i = 31 - 1; i >= 0; --i) { n     int a = pid[i]; n     int b = pid2[i]; n     if (a == -1 and b == -1) continue; n     if (a == -1 and b != -1) n       return false; n     else if (a != -1 and b == -1) n       ans.push_back(make_pair(a, a)); n     else { n       int d = pivot[i] ^ pivot2[i]; n       for (int j = i - 1; j >= 0; --j) n         if (pid[j] != -1 and d & (1 << j)) n           d ^= pivot[j], ans.push_back(make_pair(pid[i], pid[j])); n       if (d != 0) return false; n       if (a == b) continue; n       ans.push_back(make_pair(pid[i], b)); n       ans.push_back(make_pair(b, pid[i])); n       ans.push_back(make_pair(pid[i], b)); n       for (int j = 0; j < 31; ++j) n         if (pid[j] == b) { n           swap(pid[i], pid[j]); n           break; n         } n     } n   } n   reverse((buf).begin(), (buf).end()); n   for (const auto &p : buf) ans.push_back(p); n   return true; n } n int main() { n   int n; n   scanf( %d , &n); n   for (int i = 0; i < n; ++i) scanf( %d , &ary[i]); n   solve(n, ary); n   for (int i = 0; i < 31; ++i) n     if (pid[i] != -1) vis[pid[i]] = true; n   for (int i = 0; i < n; ++i) scanf( %d , &bry[i]); n   bool posi = solve2(n, bry); n   if (not posi) n     printf( -1 ); n   else { n     printf( %d n , ((int)(ans).size())); n     for (const auto &p : ans) printf( %d %d n , p.first + 1, p.second + 1); n   } n } n


#include <bits/stdc++.h> n int n; n struct camel { n   int x, d; n   inline bool operator<(const camel& a) const { return x < a.x; } n } c[105]; n int main() { n   scanf( %d , &n); n   for (int i = 0; i < n; i++) scanf( %d%d , &c[i].x, &c[i].d); n   std::sort(c, c + n); n   for (int i = 0; i < n; i++) n     for (int j = i; j < n; j++) n       if ((c[i].x + c[i].d == c[j].x) && (c[j].x + c[j].d == c[i].x)) { n         puts( YES ); n         return 0; n       } n   puts( NO ); n } n


#include <bits/stdc++.h> n using namespace std; n const double pi = acos(-1.); n const double eps = 1e-6; n int n, d; n int main() { n   scanf( %d%d , &n, &d); n   if (n == 1) n     printf( %d n , d); n   else { n     if (d == 0) { n       puts( No solution ); n     } else { n       printf( %d , 1); n       for (int i = 0; i < n - 2; i++) printf( %d , 0); n       printf( %d n , d - 1); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n; n vector<pair<int, int> > ans; n int a[1000000]; n int b[1000000]; n int main() { n   cin >> n; n   for (int i = 0; i < n; i++) { n     cin >> a[i]; n   } n   for (int i = 0; i < n; i++) { n     cin >> b[i]; n   } n   for (int i = 0; i < n; i++) { n     if (b[i] == a[i]) n       continue; n     else { n       for (int j = i + 1; j < n; j++) { n         if (b[j] == a[i]) { n           for (int k = j; k > i; k--) { n             swap(b[k], b[k - 1]); n             ans.push_back(make_pair(k - 1, k)); n           } n           break; n         } n       } n     } n   } n   cout << ans.size() << endl; n   for (int i = 0; i < ans.size(); i++) { n     cout << ++ans[i].first <<     << ++ans[i].second << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main(int argc, char const *argv[]) { n   int n; n   cin >> n; n   int a[n]; n   int sum = 0, ans = 0; n   for (int i = 0; i < n; i++) { n     cin >> a[i]; n     sum += a[i]; n   } n   for (int i = 0; i < n; i++) n     if (!((sum - a[i]) % 2)) ans++; n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <class c> n struct rge { n   c b, e; n }; n template <class c> n rge<c> range(c i, c j) { n   return rge<c>{i, j}; n } n template <class c> n auto dud(c* x) -> decltype(cerr << *x, 0); n template <class c> n char dud(...); n struct debug { n   ~debug() { cerr << endl; } n   template <class c> n   typename enable_if<sizeof dud<c>(0) != 1, debug&>::type operator<<(c i) { n     cerr << boolalpha << i; n     return *this; n   } n   template <class c> n   typename enable_if<sizeof dud<c>(0) == 1, debug&>::type operator<<(c i) { n     return *this << range(begin(i), end(i)); n   } n   template <class c, class b> n   debug& operator<<(pair<b, c> d) { n     return *this <<  (  << d.first <<  ,   << d.second <<  ) ; n   } n   template <class c> n   debug& operator<<(rge<c> d) { n     *this <<  [ ; n     for (auto it = d.b; it != d.e; ++it) *this <<  ,   + 2 * (it == d.b) << *it; n     return *this <<  ] ; n   } n }; n const long long p = 1e9 + 7; n long long factmod(long long n) { n   long long res = 1; n   while (n > 1) { n     res = (res * ((n / p) % 2 ? p - 1 : 1)) % p; n     for (long long i = 2; i <= n % p; ++i) res = (res * i) % p; n     n /= p; n   } n   return res % p; n } n long long binpow(long long a, long long b, long long m) { n   a %= m; n   long long res = 1; n   while (b > 0) { n     if (b & 1) res = res * a % m; n     a = a * a % m; n     b >>= 1; n   } n   return res; n } n int32_t main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   long long n; n   cin >> n; n   cout << (factmod(n) + p - binpow(2, n - 1, p)) % p << endl; n } n


#include <bits/stdc++.h> n using namespace std; n long long iHateGeometry[100001]; n int main() { n   int N; n   scanf( %d , &N); n   long long maxi = -1, sum = 0; n   for (int i = 0; i < N; i++) { n     scanf( %lld , &iHateGeometry[i]); n     sum += iHateGeometry[i]; n     maxi = max(maxi, iHateGeometry[i]); n   } n   printf( %lld n , 2 * maxi - sum + 1); n } n


#include <bits/stdc++.h> n using namespace std; n long long a[3000005], has[3000005], visited[2000005], dist[1000005]; n long long pr[3000010]; n long long addmod(long long n, long long m) { n   return ((n % 1000000007) + (m % 1000000007)) % 1000000007; n } n long long multmod(long long n, long long m) { n   return ((n % 1000000007) * (m % 1000000007)) % 1000000007; n } n long long submod(long long n, long long m) { n   return ((n % 1000000007) - (m % 1000000007) + 1000000007) % 1000000007; n } n void prime() { n   pr[0] = 1; n   pr[1] = 1; n   for (long long i = 2; i <= sqrt(3000010); i++) { n     for (long long j = 2 * i; j <= 3000010; j += i) { n       pr[j] = 1; n     } n   } n } n long long cntDiv(long long n) { n   long long cnt = 0; n   for (long long i = 1; i <= sqrt(n); i++) { n     if (n % i == 0) { n       if (n / i == i) n         cnt++; n       else n         cnt += 2; n     } n   } n   return cnt; n } n long long gcd(long long a, long long b) { n   if (a == 0) return b; n   return gcd(b % a, a); n } n long long has1[3000011], b[1000005], temp1[1000005]; n vector<long long> mat[10001]; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   long long x, y, z, w; n   cin >> x >> y >> z >> w; n   if (x + y == z + w || x + z == y + w || x + w == y + z || x == y + z + w || n       y == x + z + w || z == x + y + w || w == x + y + z) n     cout <<  YES ; n   else n     cout <<  NO ; n   return 0; n } n


#include <bits/stdc++.h> n #pragma GCC optimize( O3 ) n #pragma GCC optimize( Ofast ) n #pragma GCC optimize( unroll-loops ) n using namespace std; n template <class T> n inline bool setmin(T &a, T b) { n   if (a > b) return a = b, 1; n   return 0; n } n template <class T> n inline bool setmax(T &a, T b) { n   if (a < b) return a = b, 1; n   return 0; n } n template <class T> n inline T fast(T a, T b, T mod) { n   long long res = 1; n   while (b) { n     if (b & 1) res = (res * a) % mod; n     a = (a * a) % mod; n     b >>= 1; n   } n   return res; n } n template <class T> n istream &operator>>(istream &os, vector<T> &container) { n   for (auto &u : container) os >> u; n   return os; n } n template <class T> n ostream &operator<<(ostream &os, const vector<T> &container) { n   for (auto &u : container) os << u <<    ; n   return os; n } n template <class T> n inline T gcd(T a, T b) { n   while (b) swap(a %= b, b); n   return a; n } n const long long INF = 1e9 + 7; n const long long mod = 100000007; n const long long BIG_INF = 1e18 + 7; n const long long N = 1e6 + 7; n const long long T = 1 << 20; n const long double inf = 1e18; n long long n, m, k, q; n int tree[T * 2]; n int tree2[T * 2]; n int ros[N]; n int mal[N]; n void add1(int pocz, int kon) { n   pocz += T - 1; n   kon += T + 1; n   while (pocz / 2 != kon / 2) { n     if (pocz % 2 == 0) tree[pocz + 1]++; n     if (kon & 1) tree[kon - 1]++; n     pocz /= 2; n     kon /= 2; n   } n } n int ask1(int poz) { n   poz += T; n   int ret = 0; n   while (poz) { n     ret += tree[poz]; n     poz /= 2; n   } n   return ret; n } n int ask2(int pocz, int kon) { n   pocz += T - 1; n   kon += T + 1; n   int ret = 0; n   while (pocz / 2 != kon / 2) { n     if (pocz % 2 == 0) ret = max(ret, tree2[pocz + 1]); n     if (kon & 1) ret = max(ret, tree2[kon - 1]); n     pocz /= 2; n     kon /= 2; n   } n   return ret; n } n void add2(int poz, int val) { n   poz += T; n   tree2[poz] = val; n   poz /= 2; n   while (poz) { n     tree2[poz] = max(tree2[poz + poz], tree2[poz + poz + 1]); n     poz /= 2; n   } n } n signed main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cin >> n >> m; n   add1(1, m); n   for (int i = 0; i < (n); i++) { n     int a, b; n     cin >> a >> b; n     add1(a, b); n   } n   for (int i = 1; i <= m; i++) { n     ros[i] = ask2(1, ask1(i)) + 1; n     add2(ask1(i), ros[i]); n   } n   for (int i = 0; i < (T * 2); i++) tree2[i] = 0; n   int ans = 0; n   for (int i = m; i >= 1; i--) { n     mal[i] = ask2(1, ask1(i)) + 1; n     add2(ask1(i), mal[i]); n     ans = max(ans, ros[i] + mal[i] - 1); n   } n   cout << ans <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n using ll = long long; n const int mod = 1e9 + 7; n const int sz = 1e5 + 10; n int main() { n   ll k, b, n, t, z, ans, zz; n   while (cin >> k >> b >> n >> t) { n     z = 1; n     ans = n; n     for (int i = 0; i < n && z < t; i++) { n       z = z * k + b; n       ans--; n     } n     if (z > t) ans++; n     cout << ans <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n inline long long read() { n   char ch = getchar(); n   long long res = 0, pd = 1; n   while (ch <  0  || ch >  9 ) { n     if (ch ==  - ) pd = -1; n     ch = getchar(); n   } n   while (ch >=  0  && ch <=  9 ) { n     res = (res << 1) + (res << 3) + ch -  0 ; n     ch = getchar(); n   } n   return res * pd; n } n inline void write(long long x) { n   if (x < 0) putchar( - ), x = -x; n   if (x > 9) write(x / 10); n   putchar(x % 10 |  0 ); n } n inline void wlk(long long x) { n   write(x); n   putchar(32); n } n inline void wln(long long x) { n   write(x); n   putchar(10); n } n const long long maxn = 100003; n long long a[maxn]; n signed main() { n   long long n = read(), flag = 1; n   for (long long i = 1; i <= n; ++i) a[i] = read(); n   for (long long i = 2; i <= n; ++i) n     if (a[i] % a[1] != 0) flag = 0; n   if (!flag) return puts( -1 ), 0; n   wln(2 * n); n   for (long long i = 1; i <= n; ++i) { n     wlk(a[1]), wlk(a[i]); n   } n   return puts(  ), 0; n } n


#include <bits/stdc++.h> n using namespace std; n void solve() { n   long long int n; n   cin >> n; n   string s; n   cin >> s; n   long long int mid = n / 2; n   long long int check = 0; n   for (long long int i = 0; i < n; i++) { n     if (s[i] ==  0 ) { n       check = 1; n       if (i >= mid) { n         cout << 1 <<     << i + 1 <<     << 1 <<     << i << endl; n         return; n       } else { n         cout << i + 2 <<     << n <<     << i + 1 <<     << n << endl; n         return; n       } n     } n   } n   if (check == 0) { n     cout << 1 <<     << n - 1 <<     << 2 <<     << n << endl; n   } n } n int32_t main() { n   long long int t; n   cin >> t; n   while (t--) { n     solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void sol_problem(const long *, long); n int main() { n   long n, t, *matriz; n   cin >> n >> t; n   matriz = new long[n - 1]; n   for (int i = 0; i < n - 1; i++) cin >> matriz[i]; n   sol_problem(matriz, t); n   return 0; n } n void sol_problem(const long *matriz, long t) { n   long ind = 1 + matriz[0]; n   while (ind <= t) { n     if (ind == t) { n       cout <<  YES ; n       break; n     } n     ind += matriz[ind - 1]; n   } n   if (ind > t) cout <<  NO ; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 200005; n long long mod = (1e9 + 7); n int gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); } n long long lcm(long long a, long long b) { return a * (b / gcd(a, b)); } n bool is_vowel(char c) { n   if (c ==  a  || c ==  e  || c ==  i  || c ==  o  || c ==  u ) return 1; n   return 0; n } n double getDistance(double x1, double y1, double x2, double y2) { n   return sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2)); n } n long long extended_euclidean(long long a, long long b, long long &x, n                              long long &y) { n   if (b == 0) { n     x = 1; n     y = 0; n     return a; n   } n   long long g = extended_euclidean(b, a % b, y, x); n   y -= (a / b) * x; n   return g; n } n long long power(long long base, long long p) { n   if (p == 1) return base; n   if (!p) return 1ll; n   long long ret = power(base, p / 2); n   ret *= ret; n   ret %= mod; n   if (p & 1) ret *= base; n   return ret % mod; n } n vector<int> v; n map<int, int> mp; n int n; n int solve(long long sum, long long a, long long b, long long c) { n   v.clear(); n   int mn = n, A = 0, B = 0, C = 0; n   for (int i = (0); i <= (n); ++i) { n     long long cur = sum - a * i; n     int cost = min(i, mp[a]); n     long long mx = c * (n - i); n     int taken2 = n - i; n     int taken1 = 0; n     if (mx > cur) { n       long long delta = mx - cur; n       taken2 -= delta; n       taken1 += delta; n       mx = cur; n     } n     if (mx == cur && taken1 + taken2 == n - i && taken1 >= 0 && taken2 >= 0) { n       cost += min(mp[b], taken1); n       cost += min(mp[c], taken2); n       mn = min(mn, cost); n       if (mn == cost) { n         A = i; n         B = taken1; n         C = taken2; n       } n     } n   } n   while (A--) v.push_back(a); n   while (B--) v.push_back(b); n   while (C--) v.push_back(c); n   return mn; n } n int main() { n   scanf( %d , &n); n   set<long long> st; n   long long sum = 0; n   vector<int> z; n   for (int i = (0); i <= (n - 1); ++i) { n     int a; n     scanf( %d , &a); n     st.insert(a); n     mp[a]++; n     sum += a; n     z.push_back(a); n   } n   vector<long long> x(st.begin(), st.end()); n   long long mn = *(st.begin()); n   long long mx = *(--st.end()); n   int sol = n; n   if (abs(mn - mx) < 2) { n     cout << n << endl; n     for (int i = (0); i <= (n - 1); ++i) { n       cout << z[i] <<    ; n     } n     return 0; n   } n   sol = solve(sum, mn, mn + 1, mn + 2); n   cout << sol << endl; n   for (int i = (0); i <= (n - 1); ++i) cout << v[i] <<    ; n   return 0; n } n


#include <bits/stdc++.h> n int main() { n   int64_t n, i, j; n   scanf( %I64d , &n); n   j = (n) * (2 * 3 + (n - 1) * 3) + 1; n   printf( %I64d , j); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void fail(string s) { n   cout << s; n   exit(0); n } n void fail(int s) { n   cout << s; n   exit(0); n } n int n; n int x[5005], y[5005]; n int d[5005]; n vector<int> v[5005]; n int len(int a, int b) { return abs(x[a] - x[b]) + abs(y[a] - y[b]); } n int ok = 0; n void dfs(int u, int dep, int mid) { n   d[u] = dep; n   for (int i = (0); i < (n); i++) n     if (len(u, i) > mid) { n       if (!d[i]) n         dfs(i, dep + 1, mid); n       else if (d[i] > 0) { n         if ((d[i] - d[u]) % 2 == 0) ok = 1; n       } n     } n   d[u] = -1; n } n int chk(int num) { n   ok = 0; n   for (int i = (0); i < (n); i++) d[i] = 0; n   int ans = 1; n   for (int i = (0); i < (n); i++) { n     if (!d[i]) { n       ans = ans * 2 % 1000000007; n       dfs(i, 1, num); n       if (ok) return 0; n     } n   } n   return ans; n } n signed main() { n   cin >> n; n   for (int i = (0); i < (n); i++) { n     cin >> x[i] >> y[i]; n   } n   int ans = -1, can = 0; n   int l = 0, r = 1000000, mid; n   while (l < r) { n     int mid = (l + r) / 2; n     int p = chk(mid); n     if (p) n       ans = mid, can = p, r = mid; n     else n       l = mid + 1; n   } n   cout << ans <<   n  << can <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n bool ask(int a, int b, int c, int d) { n   cout <<  ?   << a <<     << b <<     << c <<     << d << endl; n   fflush(stdout); n   string response; n   cin >> response; n   if (response ==  BAD ) { n     exit(0); n   } else if (response ==  YES ) n     return true; n   else n     return false; n } n int main() { n   int n; n   cin >> n; n   int x = 1, y = 1; n   string ans1 =   , ans2 =   ; n   for (int i = 0; i < n - 1; ++i) { n     if (ask(x + 1, y, n, n)) { n       x++; n       ans1.push_back( D ); n     } else { n       y++; n       ans1.push_back( R ); n     } n   } n   x = n, y = n; n   for (int i = 0; i < n - 1; ++i) { n     if (ask(1, 1, x, y - 1)) { n       y--; n       ans2.push_back( R ); n     } else { n       x--; n       ans2.push_back( D ); n     } n   } n   reverse((ans2).begin(), (ans2).end()); n   string finalans = ans1 + ans2; n   cout <<  !   << finalans << endl; n   fflush(stdout); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int a; n   scanf( %d , &a); n   string ans =  NO ; n   for (int i = 0; i < a; i++) { n     string name; n     int bef, aft; n     cin >> name; n     scanf( %d %d , &bef, &aft); n     if (bef >= 2400 && aft > bef) { n       ans =  YES ; n     } n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, t1, p; n int b[200000], h[200000]; n int main() { n   cin >> n; n   for (int i = 0; i < n; i++) { n     cin >> b[i]; n     if (!b[i]) { n       --i; n       --n; n     } n   } n   for (int i = 0; i < n; i++) { n     cin >> h[i]; n     if (h[i] == b[0]) p = i; n     if (!h[i]) --i; n   } n   for (int i = 0; i < n; i++) { n     if (b[i] != h[(p + i) % (n)]) { n       cout <<  NO  << endl; n       exit(0); n     } n   } n   cout <<  YES  << endl; n } n


#include <bits/stdc++.h> n using namespace std; n const double pi = acos(-1.0); n const double eps = 1e-11; n double memo[105][105][105]; n double dp(int R, int S, int P) { n   if (R == 0) return 0; n   if (S + P == 0) return 1; n   double& ret = memo[R][S][P]; n   if (memo[R][S][P] >= 0) return ret; n   ret = 0; n   if (R && S) ret += dp(R, S - 1, P) * R * S; n   if (S && P) ret += dp(R, S, P - 1) * S * P; n   if (R && P) ret += dp(R - 1, S, P) * R * P; n   ret /= (R * S + S * P + R * P); n   return ret; n } n int main() { n   std::ios_base::sync_with_stdio(0); n   int x, y, z; n   for (int x = 0; x < 105; x++) n     for (int y = 0; y < 105; y++) n       for (int i = 0; i < 105; i++) memo[x][y][i] = -1; n   cin >> x >> y >> z; n   cout << fixed; n   cout << setprecision(10); n   cout << dp(x, y, z) <<     << dp(y, z, x) <<     << dp(z, x, y) << endl; n   ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 500000 + 10; n int n; n vector<int> li; n struct node { n   int x, y; n } p[maxn]; n bool cmp1(node a, node b) { return a.x < b.x; } n bool cmp2(node a, node b) { return a.y < b.y; } n long long vis[maxn], ans; n int main() { n   scanf( %d , &n); n   for (int i = 1; i <= n; i++) { n     scanf( %d%d , &p[i].x, &p[i].y); n     li.push_back(p[i].x), li.push_back(p[i].y); n   } n   sort(li.begin(), li.end()); n   li.erase(unique(li.begin(), li.end()), li.end()); n   for (int i = 1; i <= n; i++) { n     p[i].x = lower_bound(li.begin(), li.end(), p[i].x) - li.begin(); n     p[i].y = lower_bound(li.begin(), li.end(), p[i].y) - li.begin(); n   } n   sort(p + 1, p + n + 1, cmp1); n   int st = 1, ed = 1; n   long long num = 0ll; n   for (; ed <= n; ed++) { n     if (p[ed].x == p[st].x) { n       vis[p[ed].y]++, num++; n     } else { n       ans += ((num - 1) * num / 2ll); n       for (int i = st; i < ed; i++) { n         if (vis[p[i].y]) { n           ans -= ((vis[p[i].y] - 1) * vis[p[i].y] / 2ll), vis[p[i].y] = 0; n         } n       } n       st = ed, num = 1ll; n       vis[p[ed].y] = 1; n     } n   } n   if (num) { n     ans += ((num - 1) * num / 2ll); n     for (int i = st; i <= n; i++) { n       if (vis[p[i].y]) { n         ans -= ((vis[p[i].y] - 1) * vis[p[i].y] / 2ll), vis[p[i].y] = 0; n       } n     } n   } n   sort(p + 1, p + n + 1, cmp2); n   st = 1, ed = 1, num = 0ll; n   for (; ed <= n; ed++) { n     if (p[ed].y == p[st].y) n       num++; n     else { n       ans += ((num - 1) * num / 2ll); n       st = ed, num = 1ll; n     } n   } n   if (num) ans += ((num - 1) * num / 2ll); n   printf( %I64d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MOD = 1e9 + 7; n const int maxn = 222222; n int main(int argc, char const *argv[]) { n   long long s, x; n   cin >> s >> x; n   if (s < x || (s - x) % 2) { n     puts( 0 ); n   } else { n     long long c = (s - x) / 2; n     long long ans = 1; n     for (int i = 0; i < 40; ++i) { n       long long b1 = (x >> i) & 1; n       long long b2 = (c >> i) & 1; n       if (b1 && b2) n         ans = 0; n       else if (b1) n         ans *= 2L; n     } n     if (c == 0) ans -= 2; n     cout << (ans) << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n string s; n int n; n int oddb, evenb, oddr, evenr; n int main() { n   cin >> n; n   cin >> s; n   int ans = 0; n   for (int i = 0; i < n; i++) { n     if (i % 2 == 0) { n       if (s[i] ==  b ) n         evenb++; n       else if (s[i] ==  r ) n         evenr++; n     } else if (i % 2 == 1) { n       if (s[i] ==  r ) n         oddr++; n       else if (s[i] ==  b ) n         oddb++; n     } n   } n   ans = min(max(oddr, evenb), max(oddb, evenr)); n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long int s[4]; n int main() { n   long long int x, y; n   while (scanf( %I64d%I64d , &x, &y) == 2) { n     s[0] = y; n     s[1] = y; n     s[2] = y; n     sort(s, s + 3); n     long long int cnt = 0; n     while (1) { n       if (s[0] == x && s[1] == x && s[2] == x) { n         break; n       } n       if (s[1] + s[2] - 1 > x) { n         s[0] = x; n       } else { n         s[0] = s[1] + s[2] - 1; n       } n       cnt++; n       sort(s, s + 3); n     } n     printf( %I64d n , cnt); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 2000100; n const int inf = 1000000007; n int dp[N][2], n, m[2], t, tot = 0, g[N][2], path[N], ans[N]; n struct nd { n   int d, id; n } a[N][2]; n struct element { n   int d, id[2]; n } b[N]; n struct point { n   int x, y; n } po[N]; n inline int read() { n   int x = 0; n   char ch = getchar(); n   while ((ch <  0 ) || (ch >  9 )) ch = getchar(); n   while ((ch >=  0 ) && (ch <=  9 )) { n     x = x * 10 + ch -  0 ; n     ch = getchar(); n   } n   return x; n } n void calc(int i, int j) { n   if (dp[i - 1][j] == -1) { n     g[i][j] = -1; n     return; n   } n   if (!b[i].id[j]) n     g[i][j] = dp[i - 1][j] + b[i].d - b[i - 1].d; n   else if (dp[i - 1][j] + b[i].d - b[i - 1].d <= t) n     g[i][j] = -1; n   else n     g[i][j] = dp[i - 1][j] + b[i].d - b[i - 1].d - t; n } n void upd(int i, int j) { n   dp[i][j] = g[i][j]; n   if (!b[i].id[j]) dp[i][j] = max(dp[i][j], min(g[i][j ^ 1], t)); n } n int main() { n   int i, j, k, nw, x, len[2], p[2], tmp, st; n   scanf( %d%d%d%d , &n, &m[0], &m[1], &t); n   if (m[0] + m[1] == 0) { n     printf( Yes n0 n n0 n n ); n     return 0; n   } n   for (i = 0; i <= 1; i++) { n     len[i] = 0; n     for (j = 1; j <= m[i]; j++) { n       x = read(); n       if (a[len[i]][i].d == x) n         a[len[i]][i].id = 1; n       else n         a[++len[i]][i] = (nd){x, 1}; n       a[++len[i]][i] = (nd){x + 1, 0}; n     } n     a[len[i] + 1][i] = (nd){inf, 0}; n   } n   tot = 0; n   memset(b, 0, sizeof(b)); n   for (p[0] = p[1] = 1; ((p[0] <= len[0]) || (p[1] <= len[1]));) { n     if (a[p[0]][0].d <= a[p[1]][1].d) n       nw = 0; n     else n       nw = 1; n     if (b[tot].d != a[p[nw]][nw].d) b[++tot].d = a[p[nw]][nw].d; n     b[tot].id[nw] = a[p[nw]][nw].id; n     p[nw]++; n   } n   dp[0][0] = dp[0][1] = 0; n   for (i = 1; i <= tot; i++) { n     for (j = 0; j <= 1; j++) calc(i, j); n     for (j = 0; j <= 1; j++) upd(i, j); n   } n   if ((dp[tot][0] == -1) && (dp[tot][1] == -1)) n     printf( No n ); n   else { n     printf( Yes n ); n     if (g[tot][0] != -1) n       path[tot] = 0; n     else n       path[tot] = 1; n     for (i = tot - 1; i; i--) n       if ((g[i][path[i + 1]] != -1) && n           (b[i + 1].d - b[i].d + g[i][path[i + 1]] - n                b[i + 1].id[path[i + 1]] * t == n            g[i + 1][path[i + 1]])) n         path[i] = path[i + 1]; n       else n         path[i] = path[i + 1] ^ 1; n     ans[0] = 0; n     for (i = 1; i <= tot; i++) n       if (path[i - 1] ^ path[i]) ans[++ans[0]] = b[i - 1].d; n     printf( %d n , ans[0]); n     for (i = 1; i <= ans[0]; i++) printf( %d  , ans[i]); n     printf(  n ); n     ans[0] = 0; n     for (i = 1; i <= tot; i = j + 1) { n       j = i; n       while ((j <= tot) && (path[j + 1] == path[i])) j++; n       tmp = 0; n       for (k = i; k <= j; k++) tmp += b[k].id[path[i]]; n       st = b[i - 1].d + t - dp[i - 1][path[i]]; n       for (k = 1; k <= tmp; k++, st += t) n         po[++ans[0]] = (point){st, path[i] + 1}; n     } n     printf( %d n , ans[0]); n     for (i = 1; i <= ans[0]; i++) printf( %d %d n , po[i].x, po[i].y); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n inline void FAST_IO() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n } n const int N = 3e5 + 5; n long long n, k, A, B; n long long a[N]; n inline long long dfs(long long l, long long r) { n   long long now = upper_bound(a, a + k, r) - lower_bound(a, a + k, l); n   if (now == 0) return A; n   if (l == r) return (now == 0 ? A : now * B); n   long long mid = (l + r) >> 1; n   long long sum = dfs(l, mid) + dfs(mid + 1, r); n   sum = now * B * (r - l + 1) > sum ? sum : now * B * (r - l + 1); n   return sum; n } n signed main() { n   FAST_IO(); n   cin >> n >> k >> A >> B; n   for (int i = 0; i < k; i++) { n     cin >> a[i]; n   } n   sort(a, a + k); n   cout << dfs(1ll, 1ll << n) <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(nullptr); n   long long n, m; n   cin >> n >> m; n   vector<set<long long>> v((size_t)n); n   auto add = [&](long long a, long long b) { v[a].insert((b - a + n) % n); }; n   for (long long i = 0; i < m; i++) { n     long long a, b; n     cin >> a >> b; n     a--, b--; n     add(a, b); n     add(b, a); n   } n   bool o = false; n   for (long long j = 2, k = n; j <= k; j++) { n     if (k % j == 0) { n       bool g = true; n       for (long long i = 0; i < n - n / j; i++) { n         if (v[i] != v[i + n / j]) { n           g = false; n           break; n         } n       } n       if (g) { n         o = true; n         break; n       } n     } n     while (k % j == 0) { n       k /= j; n     } n   } n   cout << (o ?  Yes  :  No ) << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXSIZE = 10000020; n int bufpos; n char buf[MAXSIZE]; n void init() { n   buf[fread(buf, 1, MAXSIZE, stdin)] =   0 ; n   bufpos = 0; n } n int readint() { n   int val = 0; n   for (; !isdigit(buf[bufpos]); bufpos++) n     ; n   for (; isdigit(buf[bufpos]); bufpos++) val = val * 10 + buf[bufpos] -  0 ; n   return val; n } n char readchar() { n   for (; isspace(buf[bufpos]); bufpos++) n     ; n   return buf[bufpos++]; n } n int readstr(char* s) { n   int cur = 0; n   for (; isspace(buf[bufpos]); bufpos++) n     ; n   for (; !isspace(buf[bufpos]); bufpos++) s[cur++] = buf[bufpos]; n   s[cur] =   0 ; n   return cur; n } n char s[5003], w[5003]; n int main() { n   init(); n   int n = readstr(s + 1); n   bool flag = true; n   for (int i = 1; i <= n; i++) { n     if (n % 2 == 1 && i * 2 == n + 1) continue; n     if (s[i] != s[1]) { n       flag = false; n       break; n     } n   } n   if (flag) return puts( Impossible ), 0; n   for (int i = 1; i < n; i++) { n     int cur = 0; n     for (int j = i + 1; j <= n; j++) w[++cur] = s[j]; n     for (int j = 1; j <= i; j++) w[++cur] = s[j]; n     if (!strcmp(s + 1, w + 1)) continue; n     bool flag = true; n     for (int j = 1; j <= n; j++) n       if (w[j] != w[n - j + 1]) { n         flag = false; n         break; n       } n     if (flag) return puts( 1 ), 0; n   } n   puts( 2 ); n } n


#include <bits/stdc++.h> n using namespace std; n int n, m, q; n std::vector<int> val(500005); n std::map<int, pair<int, int> > edges; n std::vector<pair<int, int> > query(500005); n int vis[500005]; n int par[500005]; n set<pair<int, int> > ss[500005]; n int sz[500005]; n std::vector<pair<int, int> > changes[500005]; n int st[500005], en[500005]; n int getpar(int x) { n   if (par[x] == x) n     return x; n   else n     return getpar(par[x]); n } n void merge(int n1, int n2, int idx) { n   int p1 = getpar(n1); n   int p2 = getpar(n2); n   if (p1 == p2) return; n   if (sz[p1] > sz[p2]) swap(p1, p2); n   sz[p2] += sz[p1]; n   sz[p1] = 0; n   for (auto i : ss[p1]) { n     ss[p2].insert(i); n   } n   par[p1] = p2; n   if (idx != -1) { n     st[idx] = p1; n     en[idx] = p2; n     for (auto i : ss[p1]) { n       changes[idx].push_back(i); n     } n   } n   ss[p1].clear(); n } n int getans(int node) { n   int pp = getpar(node); n   pair<int, int> zz = *(ss[pp].rbegin()); n   cout << zz.first <<   n ; n   auto it = ss[pp].rbegin(); n   ss[pp].erase(zz); n   val[zz.second] = 0; n   ss[pp].insert({val[zz.second], zz.second}); n   return zz.first; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   cin >> n >> m >> q; n   for (int i = 1; i < n + 1; i++) { n     cin >> val[i]; n     sz[i] = 1; n     par[i] = i; n     ss[par[i]].insert({val[i], i}); n   } n   for (int i = 1; i < m + 1; i++) { n     int x, y; n     cin >> x >> y; n     edges[i] = {x, y}; n   } n   for (int i = 1; i < q + 1; i++) { n     cin >> query[i].first >> query[i].second; n     if (query[i].first == 2) { n       vis[query[i].second] = 1; n     } n   } n   for (int i = 1; i < m + 1; i++) { n     if (!vis[i]) { n       merge(edges[i].first, edges[i].second, -1); n     } n   } n   for (int i = q; i >= 1; i--) { n     if (query[i].first == 2) { n       merge(edges[query[i].second].first, edges[query[i].second].second, i); n     } n   } n   for (int i = 1; i < q + 1; i++) { n     if (query[i].first == 1) { n       getans(query[i].second); n     } else { n       int st1 = st[i]; n       int en1 = en[i]; n       ss[st1].clear(); n       for (auto j : changes[i]) { n         ss[st1].insert({val[j.second], j.second}); n         ss[en1].erase({val[j.second], j.second}); n       } n       par[st1] = st1; n     } n   } n } n


#include <bits/stdc++.h> n const int bufSize = 1e6; n using namespace std; n inline char nc() { n   return getchar(); n   static char buf[bufSize], *p1 = buf, *p2 = buf; n   return p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, bufSize, stdin), p1 == p2) n              ? EOF n              : *p1++; n } n inline void read(char* s) { n   static char c; n   for (; !isalpha(c); c = nc()) n     ; n   for (; isalpha(c); c = nc()) *s++ = c; n   *s =   0 ; n } n template <typename T> n inline T read(T& r) { n   static char c; n   static int flag; n   flag = 1, r = 0; n   for (c = nc(); !isdigit(c); c = nc()) n     if (c ==  - ) flag = -1; n   for (; isdigit(c); c = nc()) r = r * 10 + c - 48; n   return r *= flag; n } n const int maxn = 1e5 + 100; n const int maxm = 22; n int T, n; n char A[maxn], B[maxn]; n int G[maxm][maxm]; n int G2[maxm]; n vector<int> s[maxm]; n int fa[maxm]; n int find(const int& x) { return fa[x] == x ? x : fa[x] = find(fa[x]); } n inline void merge(int x, int y) { n   x = find(x), y = find(y); n   if (x == y) return; n   if (s[x].size() < s[y].size()) swap(x, y); n   fa[y] = x; n   for (vector<int>::iterator it = s[y].begin(); it != s[y].end(); ++it) n     s[x].push_back(*it); n   s[y].clear(); n } n int f[1 << 21], num[1 << 21]; n inline void init() { n   for (int i = 1; i <= 20; ++i) fa[i] = i, s[i].clear(), s[i].push_back(i); n   for (int i = 1; i <= 20; ++i) n     for (int j = 1; j <= 20; ++j) G[i][j] = 0; n } n int main() { n   for (int i = 0; i < (1 << 20); ++i) n     for (int x = i; x; x >>= 1) num[i] += (x & 1); n   read(T); n   while (T--) { n     read(n); n     read(A + 1), read(B + 1); n     init(); n     for (int i = 1; i <= n; ++i) { n       if (A[i] == B[i]) continue; n       G[A[i] -  a  + 1][B[i] -  a  + 1] = 1; n       merge(A[i] -  a  + 1, B[i] -  a  + 1); n     } n     int ans = 0; n     for (int i = 1; i <= 20; ++i) { n       if (find(i) != i) continue; n       for (unsigned int j = 0; j < s[i].size(); ++j) G2[j] = 0; n       for (unsigned int j = 0; j < s[i].size(); ++j) n         for (unsigned int k = 0; k < s[i].size(); ++k) n           if (G[s[i][j]][s[i][k]]) G2[j] |= (1 << k); n       int maxx = (1 << (int)s[i].size()); n       for (int status = 0; status < maxx; ++status) f[status] = 0; n       f[0] = 1; n       int res = 0; n       for (int status = 0; status < maxx; ++status) { n         if (!f[status]) continue; n         res = max(res, num[status]); n         for (unsigned int j = 0; j < s[i].size(); ++j) n           if (!(status & (1 << j)) && !(G2[j] & status)) n             f[status | (1 << j)] = 1; n       } n       ans += 2 * s[i].size() - res - 1; n     } n     printf( %d n , ans); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void solve(long long int test) { n   long long int n; n   cin >> n; n   cout << n << endl; n   for (long long int i = 1; i <= n; i++) cout << i <<    ; n   cout << endl; n } n int main() { n   long long int t = 1, test = 1; n   cin >> t; n   while (t--) { n     solve(test++); n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int INF = 303303; n const int Max = 101101; n int n, S, L; n int a[Max]; n int lg[Max]; n struct sparse { n   int g, h; n }; n sparse s[22][Max]; n void build_table() { n   for (int i = 1; i <= n; i++) { n     s[0][i].g = a[i]; n     s[0][i].h = a[i]; n   } n   for (int i = 1; i <= lg[n]; i++) { n     for (int j = 1; j <= n - (1 << i) + 1; j++) { n       s[i][j] = s[i - 1][j]; n       int G = s[i - 1][j + (1 << (i - 1))].g; n       int H = s[i - 1][j + (1 << (i - 1))].h; n       if (G < s[i][j].g) s[i][j].g = G; n       if (H > s[i][j].h) s[i][j].h = H; n     } n   } n   return; n } n int get_table(int L, int R) { n   int len = R - L + 1; n   int G1 = s[lg[len]][L].g; n   int H1 = s[lg[len]][L].h; n   int G2 = s[lg[len]][R - (1 << lg[len]) + 1].g; n   int H2 = s[lg[len]][R - (1 << lg[len]) + 1].h; n   int G, H; n   if (G1 <= G2) n     G = G1; n   else n     G = G2; n   if (H1 >= H2) n     H = H1; n   else n     H = H2; n   int result = H - G; n   return result; n } n int tree[Max * 11]; n int get(int v, int L, int R, int pos) { n   if (L == R) return tree[v]; n   if (pos <= (L + R) / 2) n     return get(2 * v + 1, L, (L + R) / 2, pos); n   else n     return get(2 * v + 2, (L + R) / 2 + 1, R, pos); n } n int get_min(int v, int L, int R, int l, int r) { n   if (l > R || L > r) return INF; n   if (l <= L && R <= r) return tree[v]; n   int M1 = get_min(2 * v + 1, L, (L + R) / 2, l, r); n   int M2 = get_min(2 * v + 2, (L + R) / 2 + 1, R, l, r); n   if (M1 <= M2) n     return M1; n   else n     return M2; n } n void push(int v, int L, int R, int pos, int num) { n   if (L == R) { n     tree[v] = num; n     return; n   } n   if (pos <= (L + R) / 2) n     push(2 * v + 1, L, (L + R) / 2, pos, num); n   else n     push(2 * v + 2, (L + R) / 2 + 1, R, pos, num); n   tree[v] = tree[2 * v + 1]; n   if (tree[2 * v + 2] < tree[v]) tree[v] = tree[2 * v + 2]; n   return; n } n int main() { n   cin >> n >> S >> L; n   for (int i = 1; i <= n; i++) cin >> a[i]; n   lg[1] = 0; n   int t = 1; n   for (int i = 2; i <= n; i++) { n     lg[i] = lg[i - 1]; n     if (t * 2 == i) { n       t *= 2; n       lg[i]++; n     } n   } n   build_table(); n   for (int i = 0; i < Max * 11; i++) tree[i] = INF; n   for (int i = L; i <= n; i++) { n     int raz = get_table(i - L + 1, i); n     if (raz <= S) { n       int LL = 1; n       int RR = i - L + 1; n       int start; n       while (true) { n         if (LL == RR) { n           start = LL; n           break; n         } n         int MM = (LL + RR) / 2; n         int raz_new = get_table(MM, i); n         if (raz_new <= S) n           RR = MM; n         else n           LL = MM + 1; n       } n       if (start == 1) n         push(0, 1, n, i, 1); n       else { n         int dp = get_min(0, 1, n, start - 1, i - L); n         if (dp != INF) push(0, 1, n, i, dp + 1); n       } n     } n   } n   int ans = get(0, 1, n, n); n   if (ans == INF) n     cout <<  -1  << endl; n   else n     cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n #pragma GCC optimize( O3 ) n using namespace std; n template <class c> n struct rge { n   c b, e; n }; n template <class c> n rge<c> range(c i, c j) { n   return rge<c>{i, j}; n } n template <class c> n auto dud(c* x) -> decltype(cerr << *x, 0); n template <class c> n char dud(...); n struct debug { n   template <class c> n   debug& operator<<(const c&) { n     return *this; n   } n }; n mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); n using ll = long long; n using pii = pair<int, int>; n using pll = pair<ll, ll>; n using vi = vector<int>; n using vll = vector<ll>; n const int kMaxN = 110; n bool can_reach[kMaxN][kMaxN * kMaxN]; n bool is_ordered[kMaxN][kMaxN * kMaxN]; n int num_children[kMaxN][kMaxN * kMaxN]; n void Preproc() { n   can_reach[1][1] = true; n   for (int len = 1; len < kMaxN; ++len) { n     for (int nintvs = 1; nintvs <= len * len; ++nintvs) { n       if (!can_reach[len][nintvs]) { n         continue; n       } n       for (int k = 2; len + k <= kMaxN; ++k) { n         const int new_len = len + k - 1; n         const int new_nintvs = nintvs + k * (k + 1) / 2 - 1; n         can_reach[new_len][new_nintvs] = true; n         is_ordered[new_len][new_nintvs] = true; n         num_children[new_len][new_nintvs] = k; n       } n       for (int k = 4; len + k <= kMaxN; ++k) { n         const int new_len = len + k - 1; n         const int new_nintvs = nintvs + k; n         can_reach[new_len][new_nintvs] = true; n         is_ordered[new_len][new_nintvs] = false; n         num_children[new_len][new_nintvs] = k; n       } n     } n   } n } n void Construct(int len, int nintvs) { n   vector<int> roots(len); n   const int init_len = len; n   iota((roots).begin(), (roots).end(), 0); n   vector<bool> node_ordered(len); n   vector<vi> node_children(len); n   while (len > 1) { n     assert(can_reach[len][nintvs]); n     const int k = num_children[len][nintvs]; n     const int new_root = ((int)(node_children).size()); n     vi children(roots.end() - k, roots.end()); n     roots.erase(roots.end() - k, roots.end()); n     node_children.push_back(children); n     roots.push_back(new_root); n     if (is_ordered[len][nintvs]) { n       node_ordered.push_back(true); n       len -= (k - 1); n       nintvs -= k * (k + 1) / 2 - 1; n     } else { n       node_ordered.push_back(false); n       len -= (k - 1); n       nintvs -= k; n     } n   } n   debug() <<   [  n           <<  node_children  n               :   n           << (node_children) <<  ]  ; n   debug() <<   [  n           <<  node_ordered  n               :   n           << (node_ordered) <<  ]  ; n   assert(len == 1 && nintvs == 1); n   assert(((int)(roots).size()) == 1); n   vi answer; n   function<void(int, bool)> Dfs = [&](int v, bool ordered_inc) { n     if (v < init_len) { n       answer.push_back(v + 1); n     } else if (node_ordered[v]) { n       if (ordered_inc) { n         for (int child : node_children[v]) { n           Dfs(child, !ordered_inc); n         } n       } else { n         const int sz = ((int)(node_children[v]).size()); n         for (int i = sz - 1; i >= 0; --i) { n           Dfs(node_children[v][i], !ordered_inc); n         } n       } n     } else { n       const int sz = ((int)(node_children[v]).size()); n       vi order; n       for (int start : {1, 0}) { n         for (int i = start; i < sz; i += 2) { n           order.push_back(i); n         } n       } n       if (sz % 2 == 1) { n         swap(order[sz - 2], order[sz - 1]); n       } n       for (int i : order) { n         Dfs(node_children[v][i], !ordered_inc); n       } n     } n   }; n   Dfs(roots[0], true); n   for (int v : answer) { n     cout << v <<    ; n   } n   cout <<   n ; n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout << fixed << setprecision(11); n   cerr << fixed << setprecision(6); n   Preproc(); n   int q; n   cin >> q; n   while (q--) { n     int n, k; n     cin >> n >> k; n     if (!can_reach[n][k]) { n       cout <<  NO n ; n     } else { n       cout <<  YES n ; n       Construct(n, k); n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long q1, q2; n   cin >> q1 >> q2; n   long long w1, w2; n   cin >> w1 >> w2; n   long long e1, e2; n   cin >> e1 >> e2; n   long long l1 = (q2 - w2) * (w1 - e1); n   long long l2 = (q1 - w1) * (w2 - e2); n   if (l2 > l1) return cout <<  LEFT , 0; n   if (l1 > l2) return cout <<  RIGHT , 0; n   cout <<  TOWARDS ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long int N; n   cin >> N; n   if (N == 1) { n     cout << 1 << endl; n     return 0; n   } n   cout << ((N + 1) * N / 2) % 2 << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int inline read() { n   int num = 0, neg = 1; n   char c = getchar(); n   while (!isdigit(c)) { n     if (c ==  - ) neg = -1; n     c = getchar(); n   } n   while (isdigit(c)) { n     num = (num << 3) + (num << 1) + c -  0 ; n     c = getchar(); n   } n   return num * neg; n } n const int maxn = 100010, mod = 1e9 + 7; n int n, m, ans = 2, f[maxn]; n int main() { n   n = read(); n   m = read(); n   if (n < m) swap(n, m); n   f[1] = 4; n   for (int i = 2; i <= m; i++) f[i] = (f[i - 1] + f[i - 2]) % mod; n   for (int i = 1; i < m; i++) ans = (ans + f[i]) % mod; n   f[1] = 2; n   for (int i = 2; i <= n; i++) f[i] = (f[i - 1] + f[i - 2]) % mod; n   for (int i = m + 1; i <= n; i++) ans = (ans + f[i - 1]) % mod; n   printf( %d , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m, a[500005], l = 1, r, b[500005]; n queue<int> Q; n bool flag = false; n int main() { n   scanf( %d%d , &n, &m); n   for (int i = 1; i <= n; ++i) a[i] = i & -i; n   for (int i = 1; i <= n; ++i) b[i] = 1; n   r = n; n   while (m--) { n     int opt, x, y; n     scanf( %d , &opt); n     if (opt == 1) { n       scanf( %d , &x); n       if (flag) n         x = r - x + 1; n       else n         x += l; n       if (r - x + 1 > x - l) n         while (l < x) { n           int tmp = 0; n           for (int j = l; j; j -= j & -j) tmp += a[j]; n           for (int j = l - 1; j; j -= j & -j) tmp -= a[j]; n           for (int j = l; j <= n; j += j & -j) a[j] -= tmp; n           b[x + x - l - 1] += b[l]; n           b[l] = 0; n           for (int j = x + x - l - 1; j <= n; j += j & -j) a[j] += tmp; n           l++; n           flag = false; n         } n       else n         while (r >= x) { n           for (int i = x; i <= r; ++i) { n             int tmp = 0; n             for (int j = i; j; j -= j & -j) tmp += a[j]; n             for (int j = i - 1; j; j -= j & -j) tmp -= a[j]; n             for (int j = i; j <= n; j += j & -j) a[j] -= tmp; n             b[i] = 0; n             Q.push(tmp); n           } n           for (int i = x - 1; i >= x - (r - x + 1); --i) { n             int tmp = Q.front(); n             Q.pop(); n             for (int j = i; j <= n; j += j & -j) a[j] += tmp; n             b[i] += tmp; n           } n           r = x - 1; n           flag = true; n         } n     } n     if (opt == 2) { n       scanf( %d%d , &x, &y); n       int xx, yy; n       if (flag) { n         xx = r - y; n         yy = r - x; n         x = xx, y = yy; n       } else n         x = x + l - 1, y = y + l - 1; n       int ans = 0; n       for (int j = y; j; j -= j & -j) ans += a[j]; n       for (int j = x; j; j -= j & -j) ans -= a[j]; n       printf( %d n , ans); n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n int a[500010]; n pair<int, int> b[500010]; n int c[500010]; n int T, n, tot; n int main(void) { n   T = 1; n   while (T--) { n     scanf( %d , &n); n     for (int i = 1; i <= n; ++i) { n       scanf( %d , &a[i]); n       b[i] = {a[i], -i}; n     } n     sort(b + 1, b + 1 + n); n     for (int i = 1; i <= n; ++i) { n       if (-b[i].second >= b[i].first) { n         int x = -(b[i].second + b[i].first); n         int pos = upper_bound(c + 1, c + 1 + tot, x) - c; n         c[pos] = x; n         tot = max(pos, tot); n       } n     } n     printf( %d n , tot); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const double PI = acos(-1); n template <typename T> n void chmin(T &x, const T &y) { n   if (x > y) x = y; n } n template <typename T> n void chmax(T &x, const T &y) { n   if (x < y) x = y; n } n template <typename T> n T gcd(T x, T y) { n   return y ? gcd(y, x % y) : x; n } n template <class T> n inline bool read(T &ret) { n   char c = getchar(); n   int sgn; n   if (c == EOF) return 0; n   while (c !=  -  && (c <  0  || c >  9 )) c = getchar(); n   sgn = (c ==  - ) ? -1 : 1; n   ret = (c ==  - ) ? 0 : (c -  0 ); n   while (static_cast<void>(c = getchar()), c >=  0  && c <=  9 ) n     ret = ret * 10 + (c -  0 ); n   ret *= sgn; n   return 1; n } n template <class T> n inline void write(T x) { n   if (x > 9) out(x / 10); n   putchar(x % 10 +  0 ); n } n const int N = 1e5 + 5; n int x[4], y[4], an[N], n; n void doit(int j) { n   int k = 0; n   for (int i = 0; i < (int)(4); ++i) y[i] = x[i]; n   while (true) { n     if (j == 4 || y[j] == 0) break; n     an[k++] = j; n     --y[j]; n     if (j > 0 && y[j - 1] > 0) n       --j; n     else n       ++j; n   } n   if (k == n) { n     cout <<  YES  << endl << an[0]; n     for (int i = 1; i < (int)(n); ++i) cout <<     << an[i]; n     cout << endl; n     exit(0); n   } n } n void solve(int times) { n   for (int i = 0; i < (int)(4); ++i) cin >> x[i], n += x[i]; n   for (int j = 0; j < (int)(4); ++j) doit(j); n   cout <<  NO  << endl; n } n void Init() {} n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   int times = 1; n   { n     solve(times++); n     Init(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   int n = 0; n   cin >> n; n   int a[n], res[n]; n   for (int i = 0; i < n; i++) { n     cin >> a[i]; n     res[i] = 0; n   } n   for (int i = 0; i < n - 1; i++) { n     if (i) res[i] = res[i - 1]; n     while (a[i]) { n       int t = 0; n       for (t = 0; i + (1 << t) < n; t++) n         ; n       t--; n       a[i]--; n       a[i + (1 << t)]++; n       res[i]++; n     } n   } n   for (int i = 0; i < n - 1; i++) cout << res[i] <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   string str; n   cin >> n >> str; n   int p = 0; n   if (n % 2 == 1) { n     p = 3; n     cout << str.substr(0, 3); n     if (n != 3) cout <<  - ; n   } n   for (int i = p; i < str.length(); i += 2) { n     cout << str.substr(i, 2); n     if (i != str.length() - 2) cout <<  - ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 3030; n struct dsu { n   int par[MAXN]; n   void clear() { n     for (int i = 0; i < MAXN; ++i) par[i] = i; n   } n   int find(int x) { return par[x] == x ? x : par[x] = find(par[x]); } n   bool join(int u, int v) { n     if ((u = find(u)) == (v = find(v))) return false; n     par[v] = u; n     return true; n   } n } dsu; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   int n; n   cin >> n; n   vector<int> a(n); n   for (int i = 0; i < n; ++i) cin >> a[i], a[i]--; n   int m; n   cin >> m; n   vector<pair<int, int>> moves; n   int step = 0; n   while (true) { n     dsu.clear(); n     int cst = 0; n     for (int i = 0; i < n; ++i) cst += dsu.join(i, a[i]); n     vector<int> b(n); n     for (int i = 0; i < n; ++i) b[a[i]] = i; n     if (cst < m) { n       int u = 0; n       int v = 1; n       while (dsu.find(u) == dsu.find(v)) v++; n       moves.emplace_back(u, v); n       swap(a[u], a[v]); n     } else if (cst > m) { n       int u = 0; n       while (a[u] == u) u++; n       int v = u + 1; n       while (dsu.find(v) != dsu.find(u)) v++; n       moves.emplace_back(u, v); n       swap(a[u], a[v]); n     } else n       break; n   } n   cout << moves.size() <<   n ; n   for (auto &p : moves) cout << p.first + 1 <<     << p.second + 1 <<    ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 100100; n const int MAXK = 17; n int X[MAXN]; n long long Y[MAXN]; n int N, M; n int par[MAXN][MAXK]; n int dpth[MAXN]; n int Q[MAXN]; n int ql = 0, qr = 0; n int main() { n   ios_base::sync_with_stdio(0); n   cin >> N; n   for (int i = 1; i <= N; ++i) { n     cin >> X[i] >> Y[i]; n   } n   for (int i = N; i > 0; --i) { n     while (ql + 1 < qr) { n       int a = Q[qr - 1], b = Q[qr - 2]; n       if ((Y[b] - Y[i]) * (X[a] - X[i]) > (Y[a] - Y[i]) * (X[b] - X[i])) { n         --qr; n       } else n         break; n     } n     if (ql < qr) { n       par[i][0] = Q[qr - 1]; n       dpth[i] = dpth[Q[qr - 1]] + 1; n     } else n       par[i][0] = i; n     Q[qr] = i; n     ++qr; n   } n   for (int k = 0; k + 1 < MAXK; ++k) { n     for (int i = 1; i <= N; ++i) { n       par[i][k + 1] = par[par[i][k]][k]; n     } n   } n   cin >> M; n   for (int q = 0; q < M; ++q) { n     int a, b; n     cin >> a >> b; n     if (dpth[a] < dpth[b]) swap(a, b); n     for (int i = MAXK - 1; i >= 0; --i) { n       if (dpth[par[a][i]] >= dpth[b]) { n         a = par[a][i]; n       } n     } n     for (int i = MAXK - 1; i >= 0; --i) { n       if (par[a][i] != par[b][i]) { n         a = par[a][i]; n         b = par[b][i]; n       } n     } n     if (a != b) { n       a = par[a][0]; n       b = par[b][0]; n     } n     if (a != b) printf( what n ); n     printf( %d  , a); n   } n   printf(  n ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n using ll = long long; n mt19937 rng; n ll MAX = 1e18; n const int MN = 1e3 + 100; n int get_rand(int x) { n   return static_cast<int>(static_cast<ll>(rng()) * x / n                           (static_cast<ll>(rng.max()) - rng.min() + 1)); n } n int N; n ll L, E; n array<ll, 2> ans[MN]; n ll get(int f, ll x) { n   printf( ? %d %lld n , f, x); n   fflush(stdout); n   ll r; n   scanf( %lld , &r); n   return r; n } n ll loc(int f, ll v, ll l = 0, ll r = MAX) { n   r++; n   ll m; n   while (r - l > 1) { n     m = l + (r - l >> 1); n     if (get(f, m) > v) n       r = m; n     else n       l = m; n   } n   return l; n } n void solve(vector<int>& v, int l, int r, ll lp, ll rp) { n   if (v.size() == 1) return ans[v[0]] = {lp, rp}, void(); n   random_shuffle(v.begin(), v.end(), get_rand); n   int m = l + (r - l >> 1); n   ll mp; n   function<ll(vector<int>&, int)> kth_element = [&](vector<int>& x, int k) { n     vector<int> a, b, c; n     b.push_back(x.back()); n     ll t = loc(x.back(), m * E, lp, rp); n     for (x.pop_back(); !x.empty(); x.pop_back()) { n       ll q = get(x.back(), t); n       if (m * E < q) n         a.push_back(x.back()); n       else if (q < m * E) n         c.push_back(x.back()); n       else n         b.push_back(x.back()); n     } n     if (k < a.size()) n       t = kth_element(a, k); n     else if (a.size() + b.size() <= k) n       t = kth_element(c, k - a.size() - b.size()); n     for (int i : a) x.push_back(i); n     for (int i : b) x.push_back(i); n     for (int i : c) x.push_back(i); n     return t; n   }; n   mp = kth_element(v, m - l); n   vector<int> L, R; n   for (int i = 0; i < m - l; i++) L.push_back(v[i]); n   for (int i = m - l; i < r - l; i++) R.push_back(v[i]); n   solve(L, l, m, lp, mp); n   solve(R, m, r, mp, rp); n } n int main(void) { n   srand(clock()); n   scanf( %d%lld , &N, &L); n   E = L / N; n   vector<int> a; n   for (int i = 1; i <= N; i++) a.push_back(i); n   solve(a, 0, N, 0, MAX); n   printf( ! n ); n   for (int i = 1; i <= N; i++) printf( %lld %lld n , ans[i][0], ans[i][1]); n   fflush(stdout); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, u, r; n int a[31], b[31], k[31], p[31]; n long long int s = -333; n void calc(vector<int> &v) { n   int f[31]; n   for (int j = 0; j < n; j++) { n     f[j] = a[j]; n   } n   for (int j = 0; j < v.size(); j++) { n     if (v[j]) { n       for (int i = 0; i < n; i++) { n         f[i] ^= b[i]; n       } n     } else { n       int g[31]; n       for (int i = 0; i < n; i++) { n         g[i] = f[p[i] - 1] + r; n       } n       for (int j = 0; j < n; j++) { n         f[j] = g[j]; n       } n     } n   } n   long long int c = 0; n   for (int j = 0; j < n; j++) { n     c += k[j] * f[j]; n   } n   if (s != -333) { n     s = max(c, s); n   } else { n     s = c; n   } n } n void func(vector<int> v) { n   int d = v.size(); n   if (v[d - 1] == 0) { n     for (int i = d; i < u; i++) { n       vector<int> f = v; n       f.push_back(1); n       if ((u - i - 1) % 2 == 0 && (u - i - 1 > 0)) calc(f); n       func(f); n       for (int j = i + 1; j < u; j++) { n         f.push_back(0); n       } n       calc(f); n       v.push_back(0); n       if ((u - i - 1) % 2 == 0 && (u - i - 1 > 0)) calc(v); n     } n   } else { n     if (d < u) { n       v.push_back(0); n       if ((u - d - 1) % 2 == 0 && (u - d - 1 > 0)) calc(v); n     } n     for (int i = d + 1; i < u; i++) { n       vector<int> f = v; n       f.push_back(1); n       if ((u - i - 1) % 2 == 0 && (u - i - 1 > 0)) calc(f); n       func(f); n       for (int j = i + 1; j < u; j++) { n         f.push_back(0); n       } n       calc(f); n       v.push_back(0); n       if ((u - i - 1) % 2 == 0 && (u - i - 1 > 0)) calc(v); n     } n   } n } n int solve() { n   cin >> n >> u >> r; n   for (int j = 0; j < n; j++) { n     cin >> a[j]; n   } n   for (int j = 0; j < n; j++) { n     cin >> b[j]; n   } n   for (int j = 0; j < n; j++) { n     cin >> k[j]; n   } n   for (int j = 0; j < n; j++) { n     cin >> p[j]; n   } n   vector<int> v1, v2; n   v1.push_back(0); n   v2.push_back(1); n   vector<int> v3, v4; n   v3.push_back(1); n   v4.push_back(0); n   for (int j = 0; j < u - 1; j++) { n     v3.push_back(0); n     v4.push_back(0); n   } n   if (u % 2 == 0) { n     s = 0; n     for (int j = 0; j < n; j++) { n       s += a[j] * k[j]; n     } n   } n   calc(v4); n   calc(v3); n   func(v1); n   func(v2); n   cout << s <<   n ; n   return 0; n } n signed main() { n   ios::sync_with_stdio(0); n   cin.tie(NULL); n   cout.tie(NULL); n   int t; n   t = 1; n   while (t--) { n     solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 2010, oo = 1e9; n int n; n char s[maxn][maxn]; n int f[maxn][maxn], fuck[maxn]; n pair<int, int> pre[maxn][maxn]; n vector<pair<int, int> > all, foo; n int main() { n   scanf( %d , &n); n   int m; n   scanf( %d , &m); n   for (int i = 1; i <= n; ++i) scanf( %s , s[i] + 1); n   f[1][1] = (s[1][1] ==  a  ? 0 : 1); n   if (m < f[1][1]) { n     putchar(s[1][1]); n     all.push_back(make_pair(1, 1)); n   } else { n     int lst = 2; n     for (int k = 3; k <= n + n; ++k) { n       for (int i = 1; i <= n; ++i) { n         int j = k - i; n         if (j >= 1 && j <= n) { n           f[i][j] = oo; n           if (i > 1) n             f[i][j] = min(f[i][j], f[i - 1][j] + (s[i][j] ==  a  ? 0 : 1)); n           if (j > 1) n             f[i][j] = min(f[i][j], f[i][j - 1] + (s[i][j] ==  a  ? 0 : 1)); n           if (f[i][j] <= m) lst = k; n         } n       } n     } n     for (int i = 2; i <= lst; ++i) putchar( a ); n     if (lst == n + n) return 0; n     for (int i = 1; i <= n; ++i) { n       int j = lst - i; n       if (j >= 1 && j <= n && f[i][j] <= m) all.push_back(make_pair(i, j)); n     } n   } n   int T = 0; n   for (;;) { n     if (all[0].first == n && all[0].second == n) break; n     char mn =  z  + 1; n     for (auto p : all) { n       int x = p.first, y = p.second; n       if (x + 1 <= n) mn = min(mn, s[x + 1][y]); n       if (y + 1 <= n) mn = min(mn, s[x][y + 1]); n     } n     foo.clear(); n     ++T; n     for (auto p : all) { n       int x = p.first, y = p.second; n       if (x + 1 <= n && s[x + 1][y] == mn) { n         if (fuck[x + 1] != T) n           fuck[x + 1] = T, foo.push_back(make_pair(x + 1, y)), n                    pre[x + 1][y] = p; n       } n       if (y + 1 <= n && s[x][y + 1] == mn) { n         if (fuck[x] != T) n           fuck[x] = T, foo.push_back(make_pair(x, y + 1)), pre[x][y + 1] = p; n       } n     } n     all = foo; n   } n   vector<char> ans; n   for (int x = n, y = n; x && y;) { n     auto t = pre[x][y]; n     if (!t.first || !t.second) break; n     ans.push_back(s[x][y]); n     x = t.first, y = t.second; n   } n   reverse(ans.begin(), ans.end()); n   for (auto c : ans) putchar(c); n   putchar(  n ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n priority_queue<int, vector<int>, greater<int> > pq; n int dp[11][11]; n void ini() { n   int i, j; n   for (int i = 0; i < 11; i++) n     for (j = 0; j <= max(i, 10); j++) { n       if (i == 0) n         dp[i][j] = 0; n       else if (i == j || j == 0) n         dp[i][j] = 1; n       else if (j == 1) n         dp[i][j] = i; n       else n         dp[i][j] = dp[i - 1][j] + dp[i - 1][j - 1]; n     } n } n int main() { n   ini(); n   int b, i, n, t, m, sum, x, y; n   string s1, s2; n   cin >> s1; n   cin >> s2; n   int l = s1.length(); n   sum = 0; n   x = 0; n   y = 0; n   for (int i = 0; i < l; i++) n     if (s1[i] ==  + ) sum++; n   int sum2 = 0; n   for (int i = 0; i < l; i++) { n     if (s2[i] ==  + ) n       sum2++; n     else if (s2[i] ==  ? ) n       x++; n   } n   int anss = 0; n   double answ = 0; n   if (sum - sum2 < 0) n     answ = 0; n   else if (sum == sum2 && x == 0) n     answ = 1; n   else if (sum != sum2 && x == 0) n     answ = 0; n   else { n     anss = dp[x][sum - sum2]; n     answ = (double)anss / pow(2, x); n   } n   printf( %0.11lf , answ); n } n


#include <bits/stdc++.h> n using namespace std; n const int oo = 0x3f3f3f3f; n const double eps = 1e-9; n int N, C; n vector<int> c[6]; n vector<pair<int, int> > cl; n int seq[100008]; n bool seen[100008]; n struct Action { n   int len; n   int b[5]; n   int c[5]; n }; n vector<Action> act; n void cycle2action(int c, int l, Action &a, int p) { n   a.c[p + l - 1] = a.b[p] = c; n   for (int i = (1); i < (l); ++i) { n     a.c[p + i - 1] = a.b[p + i] = seq[a.b[p + i - 1]]; n   } n } n int main() { n   cin >> N; n   for (int i = (1); i < (N + 1); ++i) cin >> seq[i]; n   fill_n(seen, N + 1, false); n   C = 0; n   for (int i = (1); i < (N + 1); ++i) n     if (!seen[i]) { n       seen[i] = true; n       if (seq[i] == i) continue; n       C++; n       int len = 1; n       for (int k = seq[i]; k != i; k = seq[k]) { n         seen[k] = true; n         len++; n       } n       if (len <= 5) { n         c[len].push_back(i); n         continue; n       } n       cl.push_back(pair<int, int>(i, len)); n     } n   while (C > 0) { n     while (!cl.empty()) { n       int l = cl.back().second; n       int k = cl.back().first; n       cl.pop_back(); n       Action a; n       a.len = 5; n       cycle2action(k, 5, a, 0); n       l -= 4; n       seq[a.c[4]] = seq[a.b[4]]; n       if (l > 5) n         cl.push_back(pair<int, int>(a.c[4], l)); n       else n         c[l].push_back(a.c[4]); n       act.push_back(a); n     } n     for (int l = (4); l < (6); ++l) { n       while (!c[l].empty()) { n         Action a; n         a.len = l; n         cycle2action(c[l].back(), l, a, 0); n         c[l].pop_back(); n         act.push_back(a); n         C--; n         continue; n       } n     } n     while (int((c[2]).size()) && int((c[3]).size())) { n       Action a; n       a.len = 5; n       cycle2action(c[2].back(), 2, a, 0); n       cycle2action(c[3].back(), 3, a, 2); n       c[2].pop_back(); n       c[3].pop_back(); n       act.push_back(a); n       C -= 2; n       continue; n     } n     while (int((c[2]).size()) >= 2) { n       Action a; n       a.len = 4; n       cycle2action(c[2].back(), 2, a, 0); n       c[2].pop_back(); n       cycle2action(c[2].back(), 2, a, 2); n       c[2].pop_back(); n       act.push_back(a); n       C -= 2; n     } n     if (int((c[3]).size()) >= 2) { n       Action a; n       a.len = 5; n       cycle2action(c[3].back(), 3, a, 0); n       c[3].pop_back(); n       int k = c[3].back(); n       c[3].pop_back(); n       int l = seq[k]; n       int m = seq[l]; n       a.b[3] = a.c[4] = k; n       a.b[4] = a.c[3] = m; n       swap(seq[k], seq[m]); n       c[2].push_back(l); n       act.push_back(a); n       C--; n       continue; n     } n     if (int((c[3]).size()) == 1) { n       Action a; n       a.len = 3; n       cycle2action(c[3].back(), 3, a, 0); n       c[3].pop_back(); n       act.push_back(a); n       C--; n       continue; n     } n     if (int((c[2]).size()) == 1) { n       Action a; n       a.len = 2; n       cycle2action(c[2].back(), 2, a, 0); n       c[2].pop_back(); n       act.push_back(a); n       C--; n       continue; n     } n   } n   cout << int((act).size()) << endl; n   for (__typeof__((act).begin()) a = (act).begin(); a != (act).end(); ++a) { n     cout << a->len << endl; n     for (int i = (0); i < (a->len); ++i) cout << (i ?     :   ) << a->b[i]; n     cout << endl; n     for (int i = (0); i < (a->len); ++i) cout << (i ?     :   ) << a->c[i]; n     cout << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n string ko(int k, int y) { n   string g =   ; n   while (y > 0) { n     g = char(y % k + 48) + g; n     y = y / k; n   } n   return g; n } n int main() { n   int k; n   cin >> k; n   int i, j; n   for (i = 1; i <= k - 1; i++) { n     for (j = 1; j <= k - 1; j++) { n       cout << ko(k, i * j) <<    ; n     } n     cout << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int a, b, c, d, e, f; n   cin >> a >> b >> c >> d >> e >> f; n   if (a == d || b == e || c == f) n     cout <<  YES ; n   else n     cout <<  NO ; n   return 0; 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 long long oo = 1LL << 60; n const int INF = 0x3f3f3f3f; n const int MOD = 1e9 + 7; n const int N = 100005; n struct segTree { n   int num; n   long long sum[5]; n } node[N << 2]; n struct Query { n   int type, num; n } q[N]; n void pushup(int rt) { n   node[rt].num = (node[(rt << 1)].num + node[(rt << 1 | 1)].num) % 5; n   int leftSonNum = node[(rt << 1)].num; n   for (int i = 0; i < 5; ++i) n     node[rt].sum[i] = node[(rt << 1)].sum[i] + n                       node[(rt << 1 | 1)].sum[(i - leftSonNum + 5) % 5]; n } n void update(int l, int r, int rt, int p, int v) { n   if (l == r) { n     node[rt].num = (v > 0); n     node[rt].sum[1] = (v > 0 ? v : 0); n     return; n   } n   if (p <= ((l + r) >> 1)) n     update(l, ((l + r) >> 1), (rt << 1), p, v); n   else n     update(((l + r) >> 1) + 1, r, (rt << 1 | 1), p, v); n   pushup(rt); n } n vector<int> arr; n int pos[N]; n int main() { n   int n; n   char s[5]; n   scanf( %d , &n); n   for (int i = 0; i < n; ++i) { n     scanf( %s , s); n     if (s[0] ==  a ) { n       scanf( %d , &q[i].num); n       q[i].type = 1; n       arr.push_back(q[i].num); n     } else if (s[0] ==  d ) { n       scanf( %d , &q[i].num); n       q[i].type = 2; n     } else n       q[i].type = 0; n   } n   sort((arr).begin(), (arr).end()); n   arr.erase(unique((arr).begin(), (arr).end()), arr.end()); n   for (int i = 0; i < n; ++i) n     if (q[i].type) n       pos[i] = n           lower_bound((arr).begin(), (arr).end(), q[i].num) - arr.begin() + 1; n   int siz = (int(arr.size())); n   for (int i = 0; i < n; ++i) { n     if (q[i].type == 1) n       update(1, siz, 1, pos[i], q[i].num); n     else if (q[i].type == 2) n       update(1, siz, 1, pos[i], -q[i].num); n     else n       printf( %I64d n , node[1].sum[3]); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n; n int a[100100], b[100100]; n int id[100100], p[100100]; n int main() { n   int T = 1; n   while (T--) { n     scanf( %d , &n); n     for (int i = 1; i <= n; i++) { n       scanf( %d , &a[i]); n       id[a[i]] = i; n     } n     for (int i = 1; i <= n; i++) { n       scanf( %d , &b[i]); n     } n     for (int i = 1; i <= n; i++) { n       int x = b[i]; n       int op = id[x]; n       p[i] = op; n     } n     for (int i = 1; i <= n; i++) printf( %d  , p[i]); n   } n } n