#include <bits/stdc++.h> n #pragma GCC optimize( Ofast ) n using namespace std; n template <typename T, typename TT> n inline ostream &operator<<(ostream &os, const pair<T, TT> &t) { n   return os << t.first <<     << t.second; n } n template <typename T> n inline ostream &operator<<(ostream &os, const vector<T> &t) { n   for (auto i : t) os << i <<    ; n   return os; n } n template <typename T> n inline istream &operator>>(istream &is, vector<T> &v) { n   for (T &t : v) is >> t; n   return is; n } n template <typename T1, typename T2> n inline istream &operator>>(istream &is, pair<T1, T2> &t) { n   is >> t.first >> t.second; n   return is; n } n const long long mod = 1e9 + 7; n void solve() { n   int a, b, c, d; n   cin >> a >> b >> c >> d; n   vector<string> s(50, string(50,  D )); n   for (int i = 0; i < 36; i++) { n     for (int j = 0; j < 50; j++) { n       s[i][j] = char( A  + (i / 12)); n     } n   } n   a--, b--, c--, d--; n   for (int i = 1; i < 11; i += 2) { n     for (int j = 0; j < 50 and b; j += 2, b--) s[i][j] =  B ; n   } n   for (int i = 13; i < 23; i += 2) { n     for (int j = 0; j < 50 and c; j += 2, c--) s[i][j] =  C ; n   } n   for (int i = 25; i < 35; i += 2) { n     for (int j = 0; j < 50 and d; j += 2, d--) s[i][j] =  D ; n   } n   for (int i = 37; i < 50; i += 2) { n     for (int j = 0; j < 50 and a; j += 2, a--) s[i][j] =  A ; n   } n   cout << 50 <<     << 50 <<   n ; n   for (auto x : s) cout << x <<   n ; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(nullptr); n   ; n   int T = 1; n   while (T--) { n     solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   cin >> n; n   for (int i = 0; i < n; i++) { n     int a; n     int b; n     cin >> a; n     cin >> b; n     if (a != b) { n       cout <<  Happy Alex ; n       return 0; n     } n   } n   cout <<  Poor Alex ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int T, n, i, j, k; n   cin >> T; n   while (T--) { n     cin >> n; n     string str[51]; n     for (i = 0; i < n; i++) { n       cin >> str[i]; n     } n     if (n == 1) { n       cout <<  YES  << endl; n       continue; n     } n     for (i = n - 1; i >= 0; i--) { n       for (j = n - 1; j >= 0; j--) { n         if (str[i][j] ==  1 ) { n           if (i == n - 1 || j == n - 1) str[i][j] =  * ; n           k = n - 1; n           int c = 0; n           while (k >= 0) { n             if (str[k][j] ==  * ) { n               c = 1; n             } else if (str[k][j] ==  1  && c == 1) { n               str[k][j] =  * ; n             } else if (str[k][j] ==  0 ) n               c = 0; n             k--; n           } n           k = n - 1; n           c = 0; n           while (k >= 0) { n             if (str[i][k] ==  * ) { n               c = 1; n             } else if (str[i][k] ==  1  && c == 1) { n               str[i][k] =  * ; n             } else if (str[i][k] ==  0 ) n               c = 0; n             k--; n           } n         } n       } n     } n     for (i = n - 1; i >= 0; i--) { n       for (j = n - 1; j >= 0; j--) { n         if (str[i][j] ==  1 ) { n           if (i == n - 1 || j == n - 1) str[i][j] =  * ; n           k = n - 1; n           int c = 0; n           while (k >= 0) { n             if (str[i][k] ==  * ) { n               c = 1; n             } else if (str[i][k] ==  1  && c == 1) { n               str[i][k] =  * ; n             } else if (str[i][k] ==  0 ) n               c = 0; n             k--; n           } n           k = n - 1; n           c = 0; n           while (k >= 0) { n             if (str[k][j] ==  * ) { n               c = 1; n             } else if (str[k][j] ==  1  && c == 1) { n               str[k][j] =  * ; n             } else if (str[k][j] ==  0 ) n               c = 0; n             k--; n           } n         } n       } n     } n     int cnt = 0; n     for (i = 0; i < n; i++) { n       for (j = 0; j < n; j++) { n         if (str[i][j] ==  1 ) { n           cnt++; n         } n       } n     } n     if (cnt == 0) n       cout <<  YES  << endl; n     else n       cout <<  NO  << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n #pragma GCC optimize( Ofast ) n using namespace std; n const long long int mod = 1e9 + 7; n const long long int mod1 = 998244353; n mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); n long long int rand(long long int l, long long int r) { n   uniform_int_distribution<long long int> uid(l, r); n   return uid(rng); n } n const long long int N = 2e5 + 5; n long long int lazy[4 * N], tr[4 * N]; n long long int a[N], b[N], c[N]; n void build(long long int nd, long long int s, long long int e) { n   if (s == e) { n     tr[nd] = b[s]; n     return; n   } n   long long int m = (s + e) >> 1; n   build(nd << 1, s, m); n   build(nd << 1 | 1, m + 1, e); n   tr[nd] = min(tr[nd << 1], tr[nd << 1 | 1]); n } n void push_down(long long int nd, long long int s, long long int e) { n   if (lazy[nd]) { n     tr[nd] += lazy[nd]; n     if (s != e) { n       lazy[nd << 1] += lazy[nd]; n       lazy[nd << 1 | 1] += lazy[nd]; n     } n     lazy[nd] = 0; n   } n } n void update(long long int nd, long long int s, long long int e, long long int l, n             long long int r, long long int x) { n   push_down(nd, s, e); n   if (s > r || e < l) return; n   if (l <= s && e <= r) { n     lazy[nd] += x; n     push_down(nd, s, e); n     return; n   } n   long long int m = (s + e) >> 1; n   update(nd << 1, s, m, l, r, x); n   update(nd << 1 | 1, m + 1, e, l, r, x); n   tr[nd] = min(tr[nd << 1], tr[nd << 1 | 1]); n } n vector<pair<long long int, long long int> > g[N]; n void build1(long long int nd, long long int s, long long int e) { n   if (s == e) { n     tr[nd] = a[s] + c[s]; n     return; n   } n   long long int m = (s + e) >> 1; n   build1(nd << 1, s, m); n   build1(nd << 1 | 1, m + 1, e); n   tr[nd] = min(tr[nd << 1], tr[nd << 1 | 1]); n } n void update1(long long int nd, long long int s, long long int e, n              long long int idx, long long int x) { n   if (s == e) { n     a[s] = x; n     tr[nd] = x + c[s]; n     return; n   } n   long long int m = (s + e) >> 1; n   if (idx <= m) n     update1(nd << 1, s, m, idx, x); n   else n     update1(nd << 1 | 1, m + 1, e, idx, x); n   tr[nd] = min(tr[nd << 1], tr[nd << 1 | 1]); n } n void solve() { n   long long int n, m, q; n   cin >> n >> m >> q; n   for (long long int i = 1; i <= n - 1; i++) { n     cin >> a[i] >> b[i]; n   } n   build(1, 0, n - 1); n   for (long long int i = 1; i <= m; i++) { n     long long int x, y, z; n     cin >> x >> y >> z; n     g[x].push_back({y, z}); n   } n   for (long long int i = 1; i <= n; i++) { n     for (pair<long long int, long long int> x : g[i]) { n       update(1, 0, n - 1, 0, x.first - 1, x.second); n     } n     c[i] = tr[1]; n   } n   build1(1, 1, n); n   cout << tr[1] <<   n ; n   for (long long int i = 1; i <= q; i++) { n     long long int v, x; n     cin >> v >> x; n     update1(1, 1, n, v, x); n     cout << tr[1] <<   n ; n   } n } n signed main() { n   ios ::sync_with_stdio(0), cin.tie(0), cout.tie(0); n   clock_t clk = clock(); n   long long int t; n   t = 1; n   for (long long int i = 1; i <= t; i++) { n     solve(); n   } n   cerr <<   n  n        << setprecision(10) << fixed << (double)(clock() - clk) / CLOCKS_PER_SEC; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct node { n   int a; n   int b; n   int idx; n   int k; n   int actor; n   node() {} n   node(int a, int b, int idx, int k, int actor) n       : a(a), b(b), idx(idx), k(k), actor(actor) {} n }; n const int MAXN = (int)1e5 + 5; n set<node> s; n vector<node> v; n int res[MAXN]; n bool operator<(const node &x, const node &y) { n   return x.b < y.b || (x.b == y.b && x.idx < y.idx); n } n bool comp(const node &x, const node &y) { n   return x.a < y.a || (x.a == y.a && x.actor > y.actor); n } n int main(void) { n   int n, m; n   int a, b; n   int c, d, k; n   scanf(  %d , &n); n   for (int i = 0; i < n; i++) { n     scanf(  %d %d , &a, &b); n     v.push_back(node(a, b, i, 0, 0)); n   } n   scanf(  %d , &m); n   for (int i = 1; i <= m; i++) { n     scanf(  %d %d %d , &c, &d, &k); n     v.push_back(node(c, d, i, k, 1)); n   } n   sort(v.begin(), v.end(), comp); n   int ok = 1; n   for (int i = 0; i < (int)v.size(); i++) { n     a = v[i].a; n     b = v[i].b; n     k = v[i].k; n     int idx = v[i].idx; n     int actor = v[i].actor; n     if (actor) { n       s.insert(v[i]); n     } else { n       v[i].idx = 0; n       set<node>::iterator it = s.lower_bound(v[i]); n       if (it == s.end()) { n         ok = 0; n       } else { n         node aux = *it; n         res[idx] = aux.idx; n         s.erase(it); n         aux.k--; n         if (aux.k > 0) { n           s.insert(aux); n         } n       } n     } n   } n   if (ok) { n     printf( YES n ); n     for (int i = 0; i < n; i++) { n       if (i != 0) { n         printf(   ); n       } n       printf( %d , res[i]); n     } n   } else { n     printf( NO n ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 2005; n const int INF = 0x3f3f3f3f; n int n, s; n int a[maxn << 1], vis[maxn], nxt[maxn << 1][maxn], prv[maxn << 1][maxn], n     lst[maxn]; n int f[maxn][maxn], h[maxn][maxn]; n vector<int> g[maxn][maxn]; n vector<int> vec; n vector<int> pos[maxn]; n inline void disC(int p1, int p2, int &nf, vector<int> &ng) { n   if (p1 > p2) p2 += n; n   nf += p2 - p1; n   ng.push_back(p2 - p1); n   return; n } n inline void disR(int p1, int p2, int &nf, vector<int> &ng) { n   if (p1 < p2) p1 += n; n   nf += p1 - p2; n   ng.push_back(p2 - p1); n   return; n } n inline bool between(int x, int y, int z) { n   x %= n; n   y %= n; n   z %= n; n   if (z <= x) z += n; n   if (y < x) y += n; n   return y > x && y < z; n } n inline void calcCGB(int p1, int p2, int &nf, vector<int> &ng) { n   int resf = 0; n   vector<int> resg; n   resg.clear(); n   int to = prv[p1 + n][a[p2]] % n; n   if (between(p2, to, p1)) { n     disC(p1, p2, resf, resg); n     disC(p2, to, resf, resg); n     disR(to, p2, resf, resg); n   } else n     disC(p1, p2, resf, resg); n   if (resf < nf) nf = resf, ng = resg; n   return; n } n inline void calcCBG(int p1, int p2, int &nf, vector<int> &ng) { n   int resf = 0; n   vector<int> resg; n   resg.clear(); n   int to = nxt[p2][a[p2]] % n; n   if (between(p2, to, p1)) { n     disR(p1, to, resf, resg); n     disC(to, p2, resf, resg); n   } else n     disC(p1, p2, resf, resg); n   if (resf < nf) nf = resf, ng = resg; n   return; n } n inline void calcRGB(int p1, int p2, int &nf, vector<int> &ng) { n   int resf = 0; n   vector<int> resg; n   resg.clear(); n   int to = nxt[p1][a[p2]] % n; n   if (between(p1, to, p2)) { n     disR(p1, to, resf, resg); n     disC(to, p2, resf, resg); n   } else n     disR(p1, p2, resf, resg); n   if (resf < nf) nf = resf, ng = resg; n   return; n } n inline void calcRBG(int p1, int p2, int &nf, vector<int> &ng) { n   int resf = 0; n   vector<int> resg; n   resg.clear(); n   int to = prv[p2 + n][a[p2]] % n; n   if (between(p1, to, p2)) { n     disC(p1, to, resf, resg); n     disR(to, p2, resf, resg); n   } else n     disR(p1, p2, resf, resg); n   if (resf < nf) nf = resf, ng = resg; n } n inline void calc(int p1, int p2, int &nf, vector<int> &ng) { n   nf = INF; n   calcCGB(p1, p2, nf, ng); n   calcCBG(p1, p2, nf, ng); n   calcRGB(p1, p2, nf, ng); n   calcRBG(p1, p2, nf, ng); n   return; n } n inline void print(int x) { n   printf( %c%d n , x < 0 ?  -  :  + , abs(x)); n   return; n } n int x, dlt; n inline void go(int i, int j) { n   if (i > 0) go(i - 1, h[i][j]); n   for (int k = (0); k < (g[i][j].size()); ++k) { n     int d = (g[i][j][k] < 0 ? -1 : 1), len = abs(g[i][j][k]); n     for (int l = (0); l <= (len); ++l) { n       if (a[x] == i && !vis[x]) print(dlt), dlt = 0, vis[x] = true; n       ((x += d) += n) %= n; n       dlt += d; n     } n     ((x -= d) += n) %= n; n     (dlt -= d); n   } n   return; n } n int main() { n   scanf( %d%d , &n, &s); n   --s; n   for (int i = (0); i < (n); ++i) { n     scanf( %d , a + i); n     vec.push_back(a[i]); n   } n   sort(vec.begin(), vec.end()); n   vec.erase(unique(vec.begin(), vec.end()), vec.end()); n   int mx = 0; n   for (int i = (0); i < (n); ++i) { n     a[i] = lower_bound(vec.begin(), vec.end(), a[i]) - vec.begin(), n     mx = max(mx, a[i]); n     pos[a[i]].push_back(i); n   } n   for (int i = (0); i < (n); ++i) a[n + i] = a[i]; n   memset(lst, -1, sizeof(lst)); n   for (int i = (n << 1) - 1; i >= 0; --i) { n     for (int j = (0); j <= (mx); ++j) nxt[i][j] = lst[j]; n     lst[a[i]] = i; n   } n   memset(lst, -1, sizeof(lst)); n   for (int i = (0); i < (n << 1); ++i) { n     for (int j = (0); j <= (mx); ++j) prv[i][j] = lst[j]; n     lst[a[i]] = i; n   } n   memset(f, 0x3f, sizeof(f)); n   for (int i = (0); i < (pos[0].size()); ++i) n     calc(s, pos[0][i], f[0][i], g[0][i]); n   for (int i = (1); i <= (mx); ++i) n     for (int j = (0); j < (pos[i].size()); ++j) n       for (int k = (0); k < (pos[i - 1].size()); ++k) n         if (f[i - 1][k] != INF) { n           int nf = 0; n           vector<int> ng; n           calc(pos[i - 1][k], pos[i][j], nf, ng); n           nf += f[i - 1][k]; n           if (nf < f[i][j]) f[i][j] = nf, g[i][j] = ng, h[i][j] = k; n         } n   int best = -1; n   for (int i = (0); i < (pos[mx].size()); ++i) n     if (!~best || f[mx][i] < f[mx][best]) best = i; n   printf( %d n , f[mx][best]); n   x = s, dlt = 0; n   go(mx, best); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1000005; n int a[N], sum[N], f[N], ans[N]; n int n, r, c; n char ch[N * 6]; n int main() { n   scanf( %d%d%d , &n, &r, &c); n   for (int i = 1; i <= n; i++) { n     scanf( %s , ch + sum[i - 1] + 2); n     a[i] = strlen(ch + sum[i - 1] + 2); n     sum[i] = sum[i - 1] + 1 + a[i]; n   } n   for (int i = 1; i <= n; i++) n     for (f[i] = max(f[i - 1], i); f[i] <= n && sum[f[i]] - sum[i - 1] - 1 <= c; n          f[i]++) n       ; n   f[n + 1] = n + 1; n   for (int i = 1; i <= n; i++) ans[i] = i; n   for (; r; r >>= 1) n     for (int i = 1; i <= n; i++) { n       if (r & 1) ans[i] = f[ans[i]]; n       f[i] = f[f[i]]; n     } n   int pos = 1; n   for (int i = 2; i <= n; i++) n     if (ans[i] - i > ans[pos] - pos) pos = i; n   int last = 0; n   for (int i = pos; i <= ans[pos] - 1; i++) { n     if (last + a[i] > c) { n       last = 0; n       puts(  ); n     } n     if (last) putchar(   ); n     last += a[i] + 1; n     printf( %s , ch + sum[i - 1] + 2); n   } n   return 0; n } n


#include <bits/stdc++.h> n inline void in(int &MAGNUS) { scanf( %d , &MAGNUS); } n inline void out(int MAGNUS) { printf( %d n , MAGNUS); } n inline void in(int &MAGNUS, int &CLAR) { scanf( %d%d , &MAGNUS, &CLAR); } n inline void out(int MAGNUS, int CLAR) { printf( %d %d n , MAGNUS, CLAR); } n inline void inl(long long &LIV) { scanf( %lld , &LIV); } n inline void inl(long long &LIV, long long &MART) { n   scanf( %lld%lld , &LIV, &MART); n } n inline void outl(long long LIV) { printf( %lld n , LIV); } n inline void outl(long long LIV, long long MART) { n   printf( %lld %lld n , LIV, MART); n } n using namespace std; n int R; n string s, res; n vector<string> kata; n bool ok(int C) { n   int r = 1, c = 0; n   for (int i = 0; i <= (int)kata.size() - 1; i++) { n     if (kata[i].size() > C) return 0; n     if (c + (int)kata[i].size() <= C) { n       c += (int)kata[i].size(); n     } else { n       r++; n       c = 0; n       c += (int)kata[i].size(); n     } n   } n   return r <= R; n } n int main() { n   in(R); n   getchar(); n   getline(cin, s); n   res =   ; n   for (int i = 0; i <= (int)s.size() - 1; i++) { n     if (s[i] ==    ) s[i] =  . ; n     res += (char)s[i]; n     if (!isalpha(s[i])) { n       kata.push_back(res); n       res =   ; n     } n   } n   kata.push_back(res); n   int lo = 1, hi = (int)s.size(), ans = 1 << 30; n   while (lo <= hi) { n     int mid = (lo + hi) / 2; n     if (ok(mid)) { n       hi = mid - 1; n       ans = mid; n     } else n       lo = mid + 1; n   } n   out(ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long int t; n   long long int mn, mx, noob; n   cin >> t; n   while (t--) { n     long long int flag = 0; n     cin >> mn >> mx >> noob; n     if (mn > mx) swap(mx, mn); n     if (noob >= mn) { n       cout << mn << endl; n       flag = 1; n       continue; n     } n     long long int ans = 0; n     ans = noob; n     mn -= noob; n     mx -= noob; n     long long int p = (mx - mn); n     if (p > mn) n       ans += mn; n     else { n       ans += p; n       mx -= p * 2; n       mn -= p; n       ans += (mn + mx) / 3; n     } n     cout << ans << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long DP[100005][25]; n long long arr[100005]; n long long l = 1, r, ans; n long long freq[100005]; n void alter_range(long long l1, long long r1) { n   while (l1 < l) { n     ans += freq[arr[l - 1]]; n     freq[arr[l - 1]]++; n     l--; n   } n   while (r < r1) { n     ans += freq[arr[r + 1]]; n     freq[arr[r + 1]]++; n     r++; n   } n   while (l < l1) { n     ans -= freq[arr[l]] - 1; n     freq[arr[l]]--; n     l++; n   } n   while (r1 < r) { n     ans -= freq[arr[r]] - 1; n     freq[arr[r]]--; n     r--; n   } n } n void make_layer(long long layer, long long l1, long long r1, long long a, n                 long long b) { n   if (l1 > r1) return; n   long long i, j, mid = (l1 + r1) >> 1, &curr = DP[mid][layer], best = mid; n   for (i = a; i <= min(b, mid); i++) { n     alter_range(i, mid); n     if (curr > DP[i - 1][layer - 1] + ans) { n       curr = DP[i - 1][layer - 1] + ans; n       best = i; n     } n   } n   make_layer(layer, l1, mid - 1, a, best); n   make_layer(layer, mid + 1, r1, best, b); n } n int main() { n   long long i, j, n, k; n   scanf( %lld %lld , &n, &k); n   for (i = 1; i <= n; i++) scanf( %lld , &arr[i]); n   for (i = 0; i <= n; i++) n     for (j = 0; j <= k; j++) DP[i][j] = 1e18; n   DP[0][0] = 0; n   for (i = 1; i <= k; i++) { n     make_layer(i, 1, n, 1, n); n   } n   printf( %lld n , DP[n][k]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long N = 1e5; n long long aa[N + 2], bb[N + 2]; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   long long k1, k2, k3; n   cin >> k1 >> k2 >> k3; n   long long t1, t2, t3; n   cin >> t1 >> t2 >> t3; n   long long n; n   cin >> n; n   for (long long i = 1; i <= n; i++) cin >> aa[i], bb[i] = aa[i]; n   multiset<long long> st; n   vector<long long> v; n   for (long long i = 1; i <= n; i++) { n     while (true) { n       if (st.size() == 0) break; n       long long x = *st.begin(); n       if (x < aa[i]) n         st.erase(st.find(x)); n       else n         break; n     } n     if (st.size() < k1) { n       st.insert(aa[i] + t1 - 1); n       v.push_back(aa[i] + t1); n     } else { n       long long x = *st.begin(); n       st.erase(st.find(x)); n       st.insert(x + t1); n       v.push_back(x + t1 + 1); n     } n   } n   for (long long i = 1; i <= n; i++) aa[i] = v[i - 1]; n   v.clear(); n   st.clear(); n   for (long long i = 1; i <= n; i++) { n     while (true) { n       if (st.size() == 0) break; n       long long x = *st.begin(); n       if (x < aa[i]) n         st.erase(st.find(x)); n       else n         break; n     } n     if (st.size() < k2) { n       st.insert(aa[i] + t2 - 1); n       v.push_back(aa[i] + t2); n     } else { n       long long x = *st.begin(); n       st.erase(st.find(x)); n       st.insert(x + t2); n       v.push_back(x + t2 + 1); n     } n   } n   for (long long i = 1; i <= n; i++) aa[i] = v[i - 1]; n   v.clear(); n   st.clear(); n   for (long long i = 1; i <= n; i++) { n     while (true) { n       if (st.size() == 0) break; n       long long x = *st.begin(); n       if (x < aa[i]) n         st.erase(st.find(x)); n       else n         break; n     } n     if (st.size() < k3) { n       st.insert(aa[i] + t3 - 1); n       v.push_back(aa[i] + t3); n     } else { n       long long x = *st.begin(); n       st.erase(st.find(x)); n       st.insert(x + t3); n       v.push_back(x + t3 + 1); n     } n   } n   for (long long i = 1; i <= n; i++) aa[i] = v[i - 1]; n   v.clear(); n   st.clear(); n   long long mx = 0; n   for (long long i = 1; i <= n; i++) mx = max(mx, aa[n] - bb[n]); n   cout << mx << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n using ll = long long; n using ld = long double; n int main() { n   int n; n   scanf( %d , &n); n   int q = n / 3; n   int r = n % 3; n   if (r > 0) { n     printf( %d , 2 * q + 1); n   } else { n     printf( %d , 2 * q); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long T, n; n long long getFirst(long long n) { n   long long cur = 4; n   while (n > cur) { n     n -= cur; n     cur *= 4; n   } n   return cur + n - 1; n } n long long getSecond(long long n) { n   long long cur = 4; n   while (n > cur) { n     n -= cur; n     cur *= 4; n   } n   n--; n   vector<long long> d; n   while (n) { n     d.push_back(n % 4); n     n /= 4; n   } n   long long f[4] = {0, 2, 3, 1}; n   reverse(d.begin(), d.end()); n   for (auto i : d) n = n * 4ll + f[i]; n   return 2ll * cur + n; n } n long long getThird(long long n) { return getFirst(n) ^ getSecond(n); } n int init() { n   scanf( %lld , &n); n   if (n <= 3) return printf( %lld n , n), 0; n   if (n % 3 == 1) return printf( %lld n , getFirst(n / 3)); n   if (n % 3 == 2) return printf( %lld n , getSecond(n / 3)); n   return printf( %lld n , getThird(n / 3 - 1)); n } n int main() { n   scanf( %lld , &T); n   while (T--) init(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long k, a[1200000]; n int n; n int main() { n   cin >> a[0] >> k; n   n = 0; n   while ((a[n] >= k) || (a[n] < 0)) { n     if (a[n] >= k) { n       long long x = a[n] / k; n       a[n] -= k * x; n       a[++n] = -x; n     } else { n       long long x = (-a[n] + k - 1) / k; n       a[n] += k * x; n       a[++n] = x; n     } n   } n   cout << n + 1 << endl; n   for (int i = 0; i <= n; i++) cout << a[i] <<    ; n   cout << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, ans = 0, ans1 = 0, ans2 = 0, tar; n   cin >> n; n   tar = n / 3; n   string s; n   cin >> s; n   for (int i = 0; i < n; i++) { n     int val = s[i] -  0 ; n     if (val == 0) n       ans++; n     else if (val == 1) n       ans1++; n     else n       ans2++; n   } n   for (int i = 0; tar > ans; i++) { n     if (i == n) break; n     if (s[i] ==  1  and tar < ans1) { n       s[i] =  0 ; n       ans++; n       ans1--; n       continue; n     } n     if (s[i] ==  2  and tar < ans2) { n       s[i] =  0 ; n       ans++; n       ans2--; n       continue; n     } n   } n   for (int i = n - 1; (tar < ans1) and ans2 < tar; i--) { n     if (i < 0) break; n     if (s[i] ==  1 ) { n       s[i] =  2 ; n       ans2++; n       ans1--; n     } n   } n   for (int i = n - 1; (tar < ans) and ans2 < tar; i--) { n     if (i < 0) break; n     if (s[i] ==  0 ) { n       s[i] =  2 ; n       ans2++; n       ans--; n     } n   } n   for (int i = 0; (tar < ans2) and ans1 < tar; i++) { n     if (i == n) break; n     if (s[i] ==  2 ) { n       s[i] =  1 ; n       ans1++; n       ans2--; n     } n   } n   for (int i = n - 1; (tar < ans) and ans1 < tar; i--) { n     if (i < 0) break; n     if (s[i] ==  0 ) { n       s[i] =  1 ; n       ans1++; n       ans--; n     } n   } n   cout << s << endl; n } n


#include <bits/stdc++.h> n using namespace std; n const double pi = acos(-1.0); n const double eps = 1e-8; n inline int sign(double x) { n   if (x < -eps) return -1; n   return x > eps; n } n struct Tpoint { n   double x, y; n   Tpoint() {} n   Tpoint(double x, double y) : x(x), y(y) {} n   inline double norm() { return sqrt(x * x + y * y); } n   inline void rotate(double ang) { n     double co = cos(ang), si = sin(ang); n     double tx = x, ty = y; n     x = tx * co - ty * si; n     y = tx * si + ty * co; n   } n }; n inline Tpoint operator+(const Tpoint &a, const Tpoint &b) { n   return Tpoint(a.x + b.x, a.y + b.y); n } n inline Tpoint operator-(const Tpoint &a, const Tpoint &b) { n   return Tpoint(a.x - b.x, a.y - b.y); n } n inline Tpoint operator*(const Tpoint &a, const double &b) { n   return Tpoint(a.x * b, a.y * b); n } n inline Tpoint operator/(const Tpoint &a, const double &b) { n   return Tpoint(a.x / b, a.y / b); n } n inline double det(const Tpoint &a, const Tpoint &b) { n   return a.x * b.y - a.y * b.x; n } n inline double dot(const Tpoint &a, const Tpoint &b) { n   return a.x * b.x + a.y * b.y; n } n const int dx[] = {-1, -1, 1, 1}; n const int dy[] = {-1, 1, -1, 1}; n int main() { n   int n, m, x, y; n   scanf( %d%d , &n, &m); n   char sdir[100]; n   scanf( %d%d%s , &x, &y, sdir); n   int dir = 0; n   if (sdir[0] ==  D ) { n     dir |= 2; n   } n   if (sdir[1] ==  R ) { n     dir |= 1; n   } n   int target = 0; n   for (int i = 1; i <= n; ++i) { n     if ((i + 1) % 2 == (x + y) % 2) { n       ++target; n     } n     if ((i + m) % 2 == (x + y) % 2) { n       ++target; n     } n   } n   for (int i = 2; i < m; ++i) { n     if ((i + 1) % 2 == (x + y) % 2) { n       ++target; n     } n     if ((i + n) % 2 == (x + y) % 2) { n       ++target; n     } n   } n   set<pair<int, int> > visit; n   long long total_time = 1; n   for (int t = 0; t < (n + m) * 4; ++t) { n     visit.insert(make_pair(x, y)); n     if ((int)visit.size() == target) { n       printf( %I64d n , total_time); n       return 0; n     } n     int l = 0, r = max(n, m) + 1; n     while (l + 1 < r) { n       int mid = (l + r) / 2; n       int xx = x + dx[dir] * mid; n       int yy = y + dy[dir] * mid; n       if (xx >= 1 && xx <= n && yy >= 1 && yy <= m) { n         l = mid; n       } else { n         r = mid; n       } n     } n     total_time += l; n     x += dx[dir] * l; n     y += dy[dir] * l; n     if (x == 1 || x == n) { n       dir ^= 2; n     } n     if (y == 1 || y == m) { n       dir ^= 1; n     } n     if (x == 1 && y == 1) { n       dir = 3; n     } else if (x == 1 && y == m) { n       dir = 2; n     } else if (x == n && y == 1) { n       dir = 1; n     } else if (x == n && y == m) { n       dir = 0; n     } n   } n   puts( -1 ); n   return 0; n } n


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


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


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


#include <bits/stdc++.h> n using namespace std; n int a[100000]; n int n, k; n int main() { n   scanf( %d%d , &n, &k); n   for (int i = 0; i < n; i++) scanf( %d , &a[i]); n   int l = 0, r = n; n   while (r - l > 1) { n     int m = (l + r) / 2; n     if ((long long)k > (long long)m * (m + 1) / 2) n       l = m; n     else n       r = m; n   } n   printf( %d , a[k - l * (l + 1) / 2 - 1]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m, k, i, j, v, bor[100005][27], a[100005], b[100005]; n string s; n void dfs(int v) { n   int q = 0; n   int u = 0; n   int t = 0; n   for (int i = 1; i < 27; i++) { n     if (bor[v][i] > 0) { n       q = 1; n       dfs(bor[v][i]); n       if (a[bor[v][i]] == 0) u = 1; n       if (b[bor[v][i]] == 0) t = 1; n     } n   } n   if (q == 0) { n     a[v] = 0; n     b[v] = 1; n   } n   if (u == 1) a[v] = 1; n   if (t == 1) b[v] = 1; n } n int main() { n   cin >> n >> m; n   k = 1; n   for (i = 1; i <= n; i++) { n     cin >> s; n     v = 1; n     for (j = 0; j < s.length(); j++) { n       int to = s[j] - 96; n       if (bor[v][to] > 0) n         v = bor[v][to]; n       else { n         k++; n         bor[v][to] = k; n         v = k; n       } n     } n   } n   dfs(1); n   if (a[1] == 1 && b[1] == 1) n     cout <<  First ; n   else if (a[1] == 1 && b[1] == 0 && (m % 2 == 1)) n     cout <<  First ; n   else n     cout <<  Second ; n } n


#include <bits/stdc++.h> n using namespace std; n int ls[2005], rs[2005], fa[2005], sz[2005], z[2005]; n int id, n, m, rt, mn, Sz; n char s[10]; n void getrt(int x, int F) { n   sz[x] = 1; n   int mx = 0; n   if (ls[x] && ls[x] != F && !z[ls[x]]) { n     getrt(ls[x], x); n     sz[x] += sz[ls[x]]; n     if (sz[ls[x]] > mx) mx = sz[ls[x]]; n   } n   if (rs[x] && rs[x] != F && !z[rs[x]]) { n     getrt(rs[x], x); n     sz[x] += sz[rs[x]]; n     if (sz[rs[x]] > mx) mx = sz[rs[x]]; n   } n   if (fa[x] && fa[x] != F && !z[fa[x]]) { n     getrt(fa[x], x); n     sz[x] += sz[fa[x]]; n     if (sz[fa[x]] > mx) mx = sz[fa[x]]; n   } n   if (Sz - sz[x] > mx) mx = Sz - sz[x]; n   if (mx < mn || (mx == mn && x > rt)) mn = mx, rt = x; n } n int getsz(int x, int F) { n   sz[x] = 1; n   if (ls[x] && ls[x] != F && !z[ls[x]]) sz[x] += getsz(ls[x], x); n   if (rs[x] && rs[x] != F && !z[rs[x]]) sz[x] += getsz(rs[x], x); n   if (fa[x] && fa[x] != F && !z[fa[x]]) sz[x] += getsz(fa[x], x); n   return sz[x]; n } n int getleaf(int x) { return x <= n ? x : getleaf(ls[x]); } n void dfs(int x) { n   z[x] = 1; n   if (x <= n) { n     m++; n     if (ls[fa[x]] == x) n       ls[fa[x]] = m; n     else n       rs[fa[x]] = m; n     fa[m] = fa[x]; n     fa[x] = m; n     fa[id] = m; n     ls[m] = x; n     rs[m] = id; n     return; n   } n   int a1 = getleaf(ls[x]); n   int a2 = getleaf(rs[x]); n   printf( %d %d %d n , a1, a2, id); n   fflush(stdout); n   scanf( %s , s); n   char ch = s[0]; n   if (ch ==  X ) { n     if (z[fa[x]] || !fa[x]) { n       m++; n       if (fa[x]) { n         if (ls[fa[x]] == x) n           ls[fa[x]] = m; n         else n           rs[fa[x]] = m; n         fa[m] = fa[x]; n       } n       fa[x] = m; n       fa[id] = m; n       ls[m] = x; n       rs[m] = id; n       return; n     } n     Sz = getsz(fa[x], 0); n     mn = 1e9; n     getrt(fa[x], 0); n     dfs(rt); n   } n   if (ch ==  Y ) { n     if (z[rs[x]]) { n       m++; n       fa[rs[x]] = fa[id] = m; n       ls[m] = rs[x]; n       rs[m] = id; n       rs[x] = m; n       fa[m] = x; n       return; n     } n     Sz = getsz(rs[x], 0); n     mn = 1e9; n     getrt(rs[x], 0); n     dfs(rt); n   } n   if (ch ==  Z ) { n     if (z[ls[x]]) { n       m++; n       fa[ls[x]] = fa[id] = m; n       ls[m] = ls[x]; n       rs[m] = id; n       ls[x] = m; n       fa[m] = x; n       return; n     } n     Sz = getsz(ls[x], 0); n     mn = 1e9; n     getrt(ls[x], 0); n     dfs(rt); n   } n } n int main() { n   scanf( %d , &n); n   m = n; n   m++; n   ls[m] = 1; n   rs[m] = 2; n   fa[1] = fa[2] = m; n   for (id = 3; id <= n; id++) { n     memset(z, 0, sizeof(z)); n     Sz = 2 * id - 3; n     mn = 1e9; n     getrt(1, 0); n     dfs(rt); n   } n   puts( -1 ); n   for (int i = 1; i <= m; i++) printf( %d  , fa[i] ? fa[i] : -1); n } n


#include <bits/stdc++.h> n using namespace std; n unordered_map<int, int> parent[100005]; n unordered_map<int, int> depth[100005]; n unordered_map<int, int> memo[100005]; n int Find(int node, int color) { n   if (parent[node][color] == node) return node; n   parent[node][color] = Find(parent[node][color], color); n   return parent[node][color]; n } n void Union(int a, int b, int color) { n   if (!parent[a].count(color)) { n     parent[a][color] = a; n   } n   if (!parent[b].count(color)) { n     parent[b][color] = b; n   } n   int roota(Find(a, color)); n   int rootb(Find(b, color)); n   if (depth[roota][color] < depth[rootb][color]) swap(roota, rootb); n   if (depth[roota][color] == depth[rootb][color]) depth[roota][color]++; n   parent[rootb][color] = roota; n } n int solve(int a, int b) { n   if (memo[a].count(b)) return memo[a][b]; n   int res(0); n   if (parent[a].size() > parent[b].size()) swap(a, b); n   unordered_map<int, int>::iterator it; n   for (it = parent[a].begin(); it != parent[a].end(); it++) { n     int color(it->first); n     if (!parent[b].count(color)) continue; n     if (Find(a, color) == Find(b, color)) res++; n   } n   memo[a][b] = memo[b][a] = res; n   return res; n } n int main() { n   int n, m, q; n   cin >> n >> m; n   for (int a, b, c, i = 0; i < m; i++) { n     scanf( %d%d%d , &a, &b, &c); n     a--; n     b--; n     Union(a, b, c); n   } n   cin >> q; n   for (int a, b, i = 0; i < q; i++) { n     scanf( %d%d , &a, &b); n     a--; n     b--; n     printf( %d n , solve(a, b)); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, k, z, i, y, x = 1; n   string line; n   cin >> n >> k >> line; n   for (i = 0; i < line.length(); i++) { n     if (line[i] ==  G ) { n       z = i; n     } n     if (line[i] ==  T ) { n       y = i; n     } n   } n   if (y < z) { n     k = -k; n   } n   if ((y - z) % k == 0) { n     for (i = z; i != y; i = i + k) { n       if (line[i] ==  # ) { n         cout <<  NO ; n         x = 0; n         break; n       } n     } n     if (x != 0) { n       cout <<  YES ; n     } n   } else { n     cout <<  NO ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, d, t[110], tot = 0; n   cin >> n >> d; n   for (int i = 0; i < n; i++) cin >> t[i], tot += t[i]; n   if (tot + (n - 1) * 10 > d) n     cout << -1 << endl; n   else { n     cout << (n - 1) * 2 + (d - tot - (n - 1) * 10) / 5 << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long x, y, m, aux, rs = 0; n void neg() { n   rs = (-y) / x; n   rs++; n   y = y + rs * x; n } n void rec() { n   aux = x + y; n   rs++; n   if (aux < m) { n     y = x; n     x = aux; n     rec(); n   } n } n int main() { n   cin >> x >> y >> m; n   aux = max(x, y); n   y = min(x, y); n   x = aux; n   if (x >= m) { n     cout <<  0  << endl; n     return 0; n   } n   if (x < 1) { n     cout <<  -1  << endl; n     return 0; n   } n   if (y < 1 && x > 0) { n     neg(); n   } n   rec(); n   cout << rs << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 300007; n const double eps = 1e-9; n int n, m; n double C; n inline int Read() { n   register int ch, a, flag; n   while (ch = getchar(), (ch <  0  || ch >  9 ) && (ch !=  - )) n     ; n   flag = (ch ==  - ); n   a = flag ? 0 : ch -  0 ; n   while (ch = getchar(), ch >=  0  && ch <=  9 ) n     a = (a << 1) + (a << 3) + ch -  0 ; n   a = flag ? -a : a; n   return a; n } n inline int dcmp(double a) { n   if (fabs(a) <= eps) return 0; n   return a < 0 ? -1 : 1; n } n struct Point { n   double x, y; n   Point(double a = 0, double b = 0) { x = a, y = b; } n   bool operator==(const Point &A) const { n     return dcmp(x - A.x) == 0 && dcmp(y - A.y) == 0; n   } n }; n inline Point operator+(const Point &A, const Point &B) { n   return Point(A.x + B.x, A.y + B.y); n } n inline Point operator-(const Point &A, const Point &B) { n   return Point(A.x - B.x, A.y - B.y); n } n inline Point operator*(const Point &A, const double &d) { n   return Point(A.x * d, A.y * d); n } n inline Point operator/(const Point &A, const double &d) { n   return Point(A.x / d, A.y / d); n } n inline double Dot(const Point &A, const Point &B) { n   return A.x * B.x + A.y * B.y; n } n inline double Cross(const Point &A, const Point &B) { n   return A.x * B.y - A.y * B.x; n } n inline double Length(const Point &A) { return sqrt(A.x * A.x + A.y * A.y); } n struct Segment { n   double len; n   Point v; n   Point p; n   inline Point Trans(double t) { return p + v * t; } n } s[maxn]; n struct Pos { n   int id; n   Point p; n } p[maxn]; n struct Func { n   double a, b, c; n   double be, ed; n   Func() {} n   Func(double s, double t, double q, double w, double e) { n     be = s, ed = t, a = q, b = w, c = e; n   } n }; n vector<Func> f[maxn]; n double ti[maxn << 1]; n int tot; n bool cmp(const double &a, const double &b) { return dcmp(a - b) <= 0; } n void Load(int a, int b) { n   double be = -Length(p[a].p - s[p[a].id].p), ed; n   ti[++tot] = 0; n   double Rem = C / m - be; n   for (int i = p[a].id; i != p[b].id; i = i % n + 1) { n     ed = be + s[i].len; n     ti[++tot] = ed; n     be = ed; n     Rem -= s[i].len; n   } n   ti[++tot] = be + Rem; n } n void Insert(int a, int b, int c) { n   tot = 0; n   Load(a, b), Load(b, c); n   sort(ti + 1, ti + 1 + tot); n   tot = unique(ti + 1, ti + 1 + tot) - (ti + 1); n   Point A = p[a].p, B = p[b].p; n   int ida = p[a].id, idb = p[b].id; n   for (int i = 1; i < tot; i++) { n     double al = A.x - B.x, bt = s[ida].v.x - s[idb].v.x, gm = A.y - B.y, n            om = s[ida].v.y - s[idb].v.y; n     double alpha = bt * bt + om * om, beta = 2 * (al * bt + gm * om), n            gama = al * al + gm * gm; n     f[a].push_back(Func(ti[i], ti[i + 1] - ti[i], alpha, beta, gama)); n     A = A + s[ida].v * (ti[i + 1] - ti[i]); n     B = B + s[idb].v * (ti[i + 1] - ti[i]); n     if (A == s[ida % n + 1].p) ida = ida % n + 1; n     if (B == s[idb % n + 1].p) idb = idb % n + 1; n   } n } n void Initation() { n   double dlt = C / m; n   p[1].p = s[1].p, p[1].id = 1; n   int j = 1; n   double rem = 0; n   for (int i = 2; i <= m; i++) { n     rem += dlt; n     while (dcmp(rem - s[j].len) >= 0) rem -= s[j++].len; n     p[i].id = j; n     p[i].p = s[j].Trans(rem); n   } n   for (int i = 1; i <= m; i++) Insert(i, i % m + 1, (i % m + 1) % m + 1); n } n struct Arr { n   double L, R; n } A[maxn << 1], B[maxn << 1]; n int Ta, Tb; n inline Arr Calc(Func F, double R) { n   double A = F.a, B = F.b, C = F.c - R * R, le, ri; n   if (dcmp(A) == 0) { n     if (dcmp(B) < 0) n       le = 0, ri = -C / B; n     else if (dcmp(B) == 0) { n       if (C > 0) return (Arr){1, -1}; n       le = 0, ri = F.ed; n     } else n       le = -C / B, ri = F.ed; n   } else { n     double dlt = B * B - 4 * A * C; n     if (dcmp(dlt) < 0) return (Arr){1, -1}; n     dlt = sqrt(dlt + eps); n     le = (-B - dlt) / (2 * A), ri = (-B + dlt) / (2 * A); n     if (le > ri) swap(le, ri); n   } n   le = max(le, (double)0); n   ri = min(ri, F.ed); n   if (le > ri) return (Arr){1, -1}; n   return (Arr){le + F.be, ri + F.be}; n } n void Add(Arr *s, int &Top, int id, double R) { n   Top = 0; n   for (int i = 0; i < f[id].size(); i++) { n     Arr t = Calc(f[id][i], R); n     if (t.R < 0) continue; n     if (Top && dcmp(s[Top].R - t.L) >= 0) n       s[Top].R = t.R; n     else n       s[++Top] = t; n   } n } n Arr tmp[maxn << 1]; n int Top; n void Merge() { n   Top = 0; n   int i = 1, j = 1, k = 0; n   double L, R; n   while (i <= Ta && j <= Tb) { n     L = max(A[i].L, B[j].L), R = min(A[i].R, B[j].R); n     if (L < R) tmp[++Top] = (Arr){L, R}; n     if (A[i].R >= B[j].R) n       j++; n     else n       i++; n   } n   memcpy(A, tmp, sizeof(Arr) * (Top + 3)); n   Ta = Top; n } n bool Check(double mid) { n   Add(A, Ta, 1, mid); n   if (!Ta) return 0; n   for (int i = 2; i <= m; i++) { n     Add(B, Tb, i, mid); n     Merge(); n     if (!Ta) return 0; n   } n   return 1; n } n int main() { n   n = Read(), m = Read(); n   for (int i = 1; i <= n; i++) s[i].p.x = Read(), s[i].p.y = Read(); n   for (int i = 1; i < n; i++) { n     s[i].v = s[i + 1].p - s[i].p, s[i].len = Length(s[i].v); n     s[i].v = s[i].v / s[i].len; n     C += s[i].len; n   } n   s[n].v = s[1].p - s[n].p, s[n].len = Length(s[n].v); n   s[n].v = s[n].v / s[n].len; n   C += s[n].len; n   Initation(); n   double le = 0, ri = 6666, mid; n   for (int r = 1; r <= 100; r++) { n     mid = (le + ri) / 2; n     if (!Check(mid)) n       le = mid; n     else n       ri = mid; n   } n   printf( %.10f n , mid); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n; n int l; n int a[1000007]; n int minn[1000007], maxn[1000007], cnt; n int cmp(int x, int y) { return x > y; } n int main() { n   scanf( %d , &n); n   long long ans = 0; n   for (int i = 1; i <= n; i++) { n     scanf( %d , &l); n     int flag = 0; n     for (int j = 1; j <= l; j++) { n       scanf( %d , &a[j]); n       if (j == 1) continue; n       if (a[j] > a[j - 1]) flag = 1; n     } n     if (flag == 0) { n       minn[++cnt] = a[l]; n       maxn[cnt] = a[1]; n     } n   } n   sort(minn + 1, minn + 1 + cnt); n   for (int i = 1; i <= cnt; i++) { n     int pos = lower_bound(minn + 1, minn + 1 + cnt, maxn[i]) - minn - 1; n     ans += cnt - pos; n   } n   printf( %lld , 1ll * n * n - ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int INF = 0x3f3f3f3f; n const long long INFF = 0x3f3f3f3f3f3f3f3fll; n const long long M = 1e9 + 7; n const long long maxn = 1e5 + 7; n const double eps = 0.00000001; n long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; } n template <typename T> n inline T abs(T a) { n   return a > 0 ? a : -a; n } n int n, m; n int i, j, k; n int main() { n   scanf( %d , &n); n   printf( %d , (n - 1) / 2); n } n


#include <bits/stdc++.h> n using namespace std; n const int MN = 200000, INF = 1100000000; n int main() { n   int n; n   scanf( %d , &n); n   static int x1[MN], y1[MN], x2[MN], y2[MN]; n   for (int i = 0; i < n; i++) { n     scanf( %d%d%d%d , x1 + i, y1 + i, x2 + i, y2 + i); n   } n   static int x1l[MN], y1l[MN], x2l[MN], y2l[MN]; n   x1l[0] = y1l[0] = -INF; n   x2l[0] = y2l[0] = INF; n   for (int i = 0; i < n; i++) { n     x1l[i + 1] = max(x1l[i], x1[i]); n     y1l[i + 1] = max(y1l[i], y1[i]); n     x2l[i + 1] = min(x2l[i], x2[i]); n     y2l[i + 1] = min(y2l[i], y2[i]); n   } n   static int x1r[MN], y1r[MN], x2r[MN], y2r[MN]; n   x1r[n] = y1r[n] = -INF; n   x2r[n] = y2r[n] = INF; n   for (int i = n - 1; i >= 0; i--) { n     x1r[i] = max(x1r[i + 1], x1[i]); n     y1r[i] = max(y1r[i + 1], y1[i]); n     x2r[i] = min(x2r[i + 1], x2[i]); n     y2r[i] = min(y2r[i + 1], y2[i]); n   } n   for (int i = 0; i < n; i++) { n     int X1 = max(x1l[i], x1r[i + 1]); n     int Y1 = max(y1l[i], y1r[i + 1]); n     int X2 = min(x2l[i], x2r[i + 1]); n     int Y2 = min(y2l[i], y2r[i + 1]); n     if (X1 <= X2 && Y1 <= Y2) { n       printf( %d %d n , X1, Y1); n       break; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int d[4][2] = {{0, -1}, {1, 0}, {0, 1}, {-1, 0}}; n int ans, k = 0; n int egg[1000000]; n int n, m, vis[1050][1050]{0}, ma[1050][1050]; n void dfs(int x, int y) { n   for (int i = 0; i < 4; i++) { n     int xx = x + d[i][0]; n     int yy = y + d[i][1]; n     if ((ma[x][y] >> i) % 2 == 0 && vis[xx][yy] == 0) { n       vis[xx][yy] = 1; n       dfs(xx, yy); n       ans++; n     } n   } n } n int main() { n   cin >> n >> m; n   for (int i = 1; i <= n; i++) { n     for (int j = 1; j <= m; j++) { n       cin >> ma[i][j]; n     } n   } n   for (int i = 1; i <= n; i++) { n     for (int j = 1; j <= m; j++) { n       if (!vis[i][j]) { n         ans = 1; n         vis[i][j] = 1; n         dfs(i, j); n         egg[++k] = ans; n       } n     } n   } n   sort(egg + 1, egg + k + 1); n   for (int i = k; i >= 1; i--) { n     cout << egg[i] <<    ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n map<int, int> f, num; n int Q; n int find(int x) { n   if (!f.count(x)) return x; n   int fa = find(f[x]); n   num[x] ^= num[f[x]]; n   return f[x] = fa; n } n int main() { n   scanf( %d , &Q); n   int last = 0; n   while (Q--) { n     int opt, l, r, x; n     scanf( %d %d %d , &opt, &l, &r); n     l ^= last, r ^= last; n     if (l > r) swap(l, r); n     l--; n     if (opt & 1) { n       scanf( %d , &x); n       x ^= last; n       int fl = find(l), fr = find(r); n       if (fl != fr) f[fr] = fl, num[fr] = x ^ num[l] ^ num[r]; n     } else { n       int fl = find(l), fr = find(r); n       if (fl != fr) n         last = 1, printf( -1 n ); n       else n         last = num[l] ^ num[r], printf( %d n , last); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int j, i, t, e, n; n   cin >> t; n   while (t--) { n     cin >> n; n     string s; n     cin >> s; n     string a, b; n     a = s; n     b = s; n     bool f = 0; n     a[0] =  1 ; n     b[0] =  1 ; n     i = 1; n     while (s[i] !=  1  && i < n) { n       if (s[i] ==  0 ) { n         a[i] =  0 ; n         b[i] =  0 ; n       } else { n         a[i] =  1 ; n         b[i] =  1 ; n       } n       i++; n     } n     if (s[i] ==  1 ) { n       a[i] =  1 ; n       b[i] =  0 ; n     } n     i++; n     while (i < n) { n       if (s[i] ==  0 ) { n         a[i] =  0 ; n         b[i] =  0 ; n       } else if (s[i] ==  2 ) { n         a[i] =  0 ; n         b[i] =  2 ; n       } else { n         b[i] =  1 ; n         a[i] =  0 ; n       } n       i++; n     } n     cout << a << endl << b << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n long long mod = 1e9 + 7; n void solve() { n   int n; n   cin >> n; n   int ar[n], lock[n]; n   for (int i = 0; i < n; i++) cin >> ar[i]; n   vector<int> temp, res; n   bool flag = false; n   for (int i = 0; i < n - 1; i++) { n     if (ar[i] > ar[i + 1]) { n       flag = true; n     } n   } n   if (flag == false) { n     cout <<  YES  << endl; n     return; n   } n   int minn = *min_element(ar, ar + n); n   for (int i = 0; i < n; i++) { n     if (ar[i] % minn == 0) { n       temp.push_back(ar[i]); n     } n   } n   sort(temp.begin(), temp.end()); n   int j = 0; n   for (int i = 0; i < n; i++) { n     if (ar[i] % minn != 0) { n       res.push_back(ar[i]); n     } else n       res.push_back(temp[j++]); n   } n   for (int i = 0; i < n - 1; i++) { n     if (res[i] > res[i + 1]) { n       cout <<  NO  << endl; n       return; n     } n   } n   cout <<  YES  << endl; n } n int main() { n   int T; n   cin >> T; n   while (T--) { n     solve(); n   } n } n


#include <bits/stdc++.h> n using namespace std; n using i64 = long long int; n using ii = pair<int, int>; n using ii64 = pair<i64, i64>; n int main() { n   int n; n   scanf( %d , &n); n   string s; n   cin >> s; n   int maxPhone = n / 11; n   int eight = 0; n   for (auto& c : s) n     if (c ==  8 ) eight++; n   printf( %d n , min(maxPhone, eight)); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 2e5 + 2; n int num[N], n, res[N], a[N]; n void ReadData() { n   cin >> n; n   for (int i = 0; i < n; ++i) cin >> num[i]; n } n void Update(int x, int y) { n   if (x > y) return; n   ++a[x]; n   --a[y + 1]; n } n void Solve() { n   for (int i = 0; i < n; ++i) { n     int k = 1; n     for (; k * k <= (i - 1); ++k) n       if (num[i] < num[(i - 1) / k]) ++res[k]; n     for (int j = 0; j <= (i - 1) / k; ++j) n       if (num[i] < num[j]) { n         if (0 == j) n           Update(i, n - 1); n         else n           Update(max(k, (i - 1) / (j + 1) + 1), (i - 1) / j); n       } n   } n   for (int i = 1; i < n; ++i) { n     a[i] += a[i - 1]; n     res[i] += a[i]; n   } n } n void Output() { n   for (int i = 1; i < n; ++i) cout << res[i] << endl; n } n int main() { n   ReadData(); n   Solve(); n   Output(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int dp(int a, int b) { n   if ((a == 1 && b == 1) || b == 0) return 0; n   return dp(max(b + 1, a - 2), min(b + 1, a - 2)) + 1; n } n int main() { n   int n, m; n   cin >> n >> m; n   printf( %d , dp(max(n, m), min(n, m))); n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int T, d; n   scanf( %d , &T); n   while (T--) { n     scanf( %d , &d); n     double a = 0, b = 0; n     double x = d * d - 4 * d; n     if (x < 0) { n       printf( N n ); n       continue; n     } else { n       a = (d - sqrt(x)) / 2.0; n       b = d - a; n       if (a < b) swap(a, b); n       printf( Y %.9lf %.9lf n , a, b); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, i = 0; n   string str; n   scanf( %d , &n); n   cin >> str; n   if (n % 2 == 0) { n     i = 2; n     printf( %c%c , str[0], str[1]); n   } else { n     i = 3; n     printf( %c%c%c , str[0], str[1], str[2]); n   } n   for (; i < n; i += 2) { n     printf( -%c%c , str[i], str[i + 1]); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<pair<int, int> > grafo[212345]; n int marc[212345], dp[212345], ans[212345], ansmin = 1e9; n void dfs(int x) { n   marc[x] = 1; n   for (int i = 0; i < grafo[x].size(); i++) { n     int viz = grafo[x][i].first; n     int type = grafo[x][i].second; n     if (marc[viz]) continue; n     dfs(viz); n     dp[x] += dp[viz]; n     if (type) dp[x]++; n   } n } n void dfs2(int x) { n   marc[x] = 0; n   ans[x] = dp[x]; n   ansmin = min(ansmin, ans[x]); n   for (int i = 0; i < grafo[x].size(); i++) { n     int viz = grafo[x][i].first; n     int type = grafo[x][i].second; n     if (!marc[viz]) continue; n     int dpx = dp[x]; n     int dpv = dp[viz]; n     dp[x] -= dp[viz]; n     if (type) dp[x]--; n     dp[viz] += dp[x]; n     if (!type) dp[viz]++; n     dfs2(viz); n     dp[x] = dpx; n     dp[viz] = dpv; n   } n } n int n; n int main() { n   scanf( %d , &n); n   for (int i = 1; i < n; i++) { n     int u, v; n     scanf( %d %d , &u, &v); n     grafo[u].push_back(make_pair(v, 0)); n     grafo[v].push_back(make_pair(u, 1)); n   } n   dfs(1); n   dfs2(1); n   printf( %d n , ansmin); n   for (int i = 1; i <= n; i++) { n     if (ansmin == ans[i]) printf( %d  , i); n   } n   printf(  n ); n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 100010; n int n, m, i, j, k, a[N], u, v, q, x; n vector<int> b[N]; n long long ans; n long long f(int x) { return 1LL * b[x].size() * (a[x] - b[x].size()); } n int main() { n   scanf( %d%d , &n, &m); n   for (i = 1; i <= m; i++) { n     scanf( %d%d , &u, &v); n     if (u > v) swap(u, v); n     b[u].push_back(v); n     a[u]++; n     a[v]++; n   } n   scanf( %d , &q); n   for (i = 1; i <= n; i++) ans += f(i); n   printf( %lld n , ans); n   for (j = 1; j <= q; j++) { n     scanf( %d , &x); n     ans -= f(x); n     for (i = 0; i < b[x].size(); i++) n       ans -= f(b[x][i]), b[b[x][i]].push_back(x), ans += f(b[x][i]); n     b[x].clear(); n     printf( %lld n , ans); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 2e5 + 9; n int n; n int fs[maxn]; n int pj[maxn]; n long long ansji, ansou; n void solve() { n   int i; n   for (i = 1; i <= n; ++i) fs[i] += fs[i - 1]; n   for (i = 1; i <= n; ++i) { n     pj[i] = pj[i - 1] + (fs[i] & 1); n   } n   for (i = 1; i <= n; ++i) { n     if (fs[i] & 1) { n       ansji += i - pj[i - 1]; n       ansou += pj[i - 1]; n     } else { n       ansji += pj[i - 1]; n       ansou += i - pj[i - 1]; n     } n   } n } n void _solve() { n   solve(); n   cout << ansji <<     << ansou << endl; n } n void _prepare() { n   cin >> n; n   for (int i = 1; i <= n; ++i) { n     int x; n     cin >> x; n     fs[i] = x < 0; n   } n } n int _main_prog() { n   _prepare(); n   _solve(); n   return 0; n } n int main() { return _main_prog(); } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long int i, m, n; n   double add, ans, t1, t2; n   scanf( %ld%ld , &m, &n); n   for (i = 1; i <= m; i++) { n     t1 = (double)i / m; n     t2 = (double)(i - 1) / m; n     add = i * (pow(t1, n) - pow(t2, n)); n     ans += add; n   } n   printf( %.7lf , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int a[110], n = 0, res[1010], ch = 0, k = 0; n string s; n int main() { n   cin >> s; n   memset(a, 0, sizeof a); n   memset(res, 0, sizeof res); n   for (int i = 0; i < (int)s.length(); ++i) n     if (s[i] !=  , ) { n       a[n] *= 10; n       a[n] += (int)(s[i] -  0 ); n     } else n       ++n; n   ++n; n   for (int i = 0; i < n; ++i) res[a[i]] = 1; n   for (int i = 0; i < 1010; ++i) { n     if (res[i] == 1) n       ++ch; n     else { n       if (k > 0 && ch) cout <<  , ; n       if (ch && k == 0) ++k; n       if (ch == 1) n         cout << i - 1; n       else if (ch) n         cout << i - ch <<  -  << i - 1; n       ch = 0; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long sz = 10000001; n bool vis[sz]; n int cs[sz], a[sz], n, m; n void run() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n } n int main() { n   run(); n   cin >> n; n   int x; n   for (int i = 0; i < n; i++) { n     cin >> x; n     a[x]++; n   } n   for (int i = 2; i < sz; i++) n     if (!vis[i]) n       for (int j = i; j < sz; j += i) { n         vis[j] = 1; n         if (a[j]) cs[i] += a[j]; n       } n   for (int i = 3; i < sz; i++) cs[i] = cs[i] + cs[i - 1]; n   cin >> m; n   while (m--) { n     long long l, r; n     cin >> l >> r; n     r = min(r, sz - 1); n     l = min(l, sz - 1); n     cout << cs[r] - cs[l - 1] << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 100; n char ch[maxn][maxn]; n int main() { n   int n; n   scanf( %d , &n); n   for (int i = 0; i <= n + 1; i++) { n     for (int j = 0; j <= n + 1; j++) { n       ch[i][j] =  # ; n     } n   } n   for (int i = 1; i <= n; i++) { n     getchar(); n     for (int j = 1; j <= n; j++) { n       scanf( %c , &ch[i][j]); n     } n   } n   for (int i = 1; i <= n; i++) { n     for (int j = 1; j <= n; j++) { n       if (ch[i][j] ==  # ) { n         continue; n       } n       if (ch[i - 1][j] ==  # ) { n         continue; n       } n       if (ch[i][j - 1] ==  # ) { n         continue; n       } n       if (ch[i + 1][j] ==  # ) { n         continue; n       } n       if (ch[i][j + 1] ==  # ) { n         continue; n       } n       ch[i][j] =  # ; n       ch[i][j + 1] =  # ; n       ch[i][j - 1] =  # ; n       ch[i + 1][j] =  # ; n       ch[i - 1][j] =  # ; n     } n   } n   bool ans = true; n   for (int i = 1; i <= n; i++) { n     for (int j = 1; j <= n; j++) { n       if (ch[i][j] ==  . ) { n         ans = false; n       } n     } n   } n   if (ans) { n     printf( YES n ); n   } else { n     printf( NO n ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long int ma = -1, mi = 1000000000000000; n inline long long int gcd(long long int a, long long int b) { n   return b ? gcd(b, a % b) : a; n } n inline long long int lcm(long long int a, long long int b, long long int MOD) { n   return a / gcd(a, b) * b % MOD; n } n long long int power(long long int x, unsigned int y) { n   if (y == 0) n     return 1; n   else if (y % 2 == 0) n     return power(x, y / 2) * power(x, y / 2); n   else n     return x * power(x, y / 2) * power(x, y / 2); n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(NULL); n   cout.tie(NULL); n   long long int n, i, j = 1, k = 0, b; n   cin >> n >> k; n   if (k % 2 == 1) { n     cout << 1 << endl; n     return 0; n   } n   while (true) { n     if (k % 2 == 1) break; n     j++; n     k /= 2; n   } n   cout << j << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long int var; n   scanf( %ld , &var); n   if (var >= 0) n     cout << var; n   else { n     int a, b; n     a = var % 10; n     var /= 10; n     b = var % 10; n     var /= 10; n     if (a > b) n       var = var * 10 + a; n     else n       var = var * 10 + b; n     cout << var; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<pair<int, int> > v; n int n, r, m; n bool cmp(const pair<int, int>& a, const pair<int, int>& b) { n   return a.first * a.first + a.second * a.second > n          b.first * b.first + b.second * b.second; n } n inline int get_dis(const int& first, const int& second) { n   return first * first + second * second; n } n void init() { n   v.clear(); n   for (int i = -r; i <= r; ++i) n     for (int j = -r; j <= r; ++j) n       if (i * i + j * j <= r * r) v.push_back(make_pair(i, j)); n   sort(v.begin(), v.end(), cmp); n   m = min((int)v.size(), 30 - n * 2); n } n int b[120], ans[120], ansdist; n int dist[39][39]; n void dfs(int first, int last, int sum) { n   if (first == n) { n     if (sum > ansdist) { n       for (int i = 0; i < n; ++i) ans[i] = b[i]; n       ansdist = sum; n     } n     return; n   } n   int ndist; n   for (int i = last; i < m; ++i) { n     ndist = 0; n     for (int j = 0; j < first; ++j) ndist += dist[i][b[j]]; n     b[first] = i; n     dfs(first + 1, i, sum + ndist); n   } n } n void solve() { n   for (int i = 0; i < m; ++i) n     for (int j = 0; j < m; ++j) n       dist[i][j] = get_dis(v[i].first - v[j].first, v[i].second - v[j].second); n   ansdist = 0; n   dfs(0, 0, 0); n   printf( %d n , ansdist); n   for (int i = 0; i < n; ++i) n     printf( %d %d n , v[ans[i]].first, v[ans[i]].second); n } n int main() { n   while (scanf( %d%d , &n, &r) != EOF) { n     init(); n     solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long gcd(long long a, long long b) { n   if (a > b) swap(a, b); n   if (a == 0) return b; n   return gcd(b % a, a); n } n long long powerMod(long long x, long long y) { n   long long res = 1; n   x %= 1000000007; n   while (y > 0) { n     if (y & 1) res = (res * x) % 1000000007; n     y = y >> 1; n     x = (x * x) % 1000000007; n   } n   return res; n } n int main() { n   ios::sync_with_stdio(0); n   cin.tie(NULL); n   cout.tie(NULL); n   ; n   long long n, i, j, k, l; n   cin >> n; n   long long p[n + 1], q[n + 1]; n   for (i = 1; i <= n; i++) { n     cin >> p[i]; n     q[p[i]] = i; n   } n   long long m = 0; n   vector<pair<long long, long long> > swaps; n   for (i = 1; i <= n; i++) { n     if (p[i] != i) { n       j = q[i]; n       if (2 * abs(i - j) >= n) { n         m++; n         swaps.push_back({i, j}); n         p[j] = p[i]; n         q[p[i]] = j; n         p[i] = i; n         q[i] = i; n       } else if (i <= n / 2 && j <= n / 2) { n         k = max(i, j) + n / 2; n         m += 3; n         swaps.push_back({i, k}); n         swaps.push_back({j, k}); n         swaps.push_back({i, k}); n         p[j] = p[i]; n         q[p[i]] = j; n         p[i] = i; n         q[i] = i; n       } else if (i > n / 2 && j > n / 2) { n         k = min(i, j) - n / 2; n         m += 3; n         swaps.push_back({i, k}); n         swaps.push_back({j, k}); n         swaps.push_back({i, k}); n         p[j] = p[i]; n         q[p[i]] = j; n         p[i] = i; n         q[i] = i; n       } else { n         m += 5; n         if (i <= n / 2 && j > n / 2) n           k = i + n / 2, l = j - n / 2; n         else n           k = i - n / 2, l = j + n / 2; n         swaps.push_back({j, l}); n         swaps.push_back({l, k}); n         swaps.push_back({k, i}); n         swaps.push_back({k, l}); n         swaps.push_back({l, j}); n         p[j] = p[i]; n         q[p[i]] = j; n         p[i] = i; n         q[i] = i; n       } n     } n   } n   cout << m << endl; n   for (pair<long long, long long> p : swaps) { n     cout << p.first <<     << p.second << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int c = 0; n   int n; n   cin >> n; n   string s; n   for (int i = 0; i < n; i++) { n     cin >> s; n     if (s ==  Cube ) n       c += 6; n     else if (s ==  Tetrahedron ) n       c += 4; n     else if (s ==  Octahedron ) n       c += 8; n     else if (s ==  Icosahedron ) n       c += 20; n     else n       c += 12; n   } n   cout << c << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, a, b, k, h[200010]; n int main() { n   scanf( %d%d%d%d , &n, &a, &b, &k); n   int ans = 0, x; n   for (int i = 1; i <= n; i++) { n     scanf( %d , &x); n     if (x % (a + b) == 0) n       x -= (x / (a + b) - 1) * (a + b); n     else n       x -= (x / (a + b)) * (a + b); n     if (x <= a) n       ans++; n     else { n       if (x % a == 0) n         x = x / a - 1; n       else n         x = x / a; n       h[i] = x; n     } n   } n   sort(h + 1, h + 1 + n); n   for (int i = 1; i <= n; i++) n     if (h[i] > 0 && k >= h[i]) k -= h[i], ans++; n   printf( %d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void err(istream_iterator<string> it) { cerr << endl; } n template <typename T, typename... Args> n void err(istream_iterator<string> it, T a, Args... args) { n   cerr << *it <<   =   << a <<   ,  ; n   err(++it, args...); n } n namespace fastIO { n bool IOerror = 0; n inline char nc() { n   static char buf[100000], *p1 = buf + 100000, *pend = buf + 100000; n   if (p1 == pend) { n     p1 = buf; n     pend = buf + fread(buf, 1, 100000, stdin); n     if (pend == p1) { n       IOerror = 1; n       return -1; n     } n   } n   return *p1++; n } n inline bool blank(char ch) { n   return ch ==     || ch ==   n  || ch ==   r  || ch ==   t ; n } n inline void read(int &x) { n   char ch; n   while (blank(ch = nc())) n     ; n   if (IOerror) return; n   for (x = ch -  0 ; (ch = nc()) >=  0  && ch <=  9 ; x = x * 10 + ch -  0 ) n     ; n } n }  // namespace fastIO n using namespace fastIO; n const double PI = acos(-1.0); n const double eps = 1e-8; n const int maxn = 3e5 + 7; n const int N = 5e5 + 10; n int a[N]; n int main() { n   int n; n   cin >> n; n   int mx = 0; n   for (int i = 1; i <= n; ++i) cin >> a[i], mx = max(mx, a[i]); n   int ans = 1; n   int len = 0; n   for (int i = 1; i <= n; ++i) { n     if (a[i] == mx) n       len++; n     else n       len = 0; n     ans = max(ans, len); n   } n   cout << ans << endl; n } n


#include <bits/stdc++.h> n using namespace std; n const int mod = 1e9 + 7; n long long qpow(long long x, long long y) { n   long long re = 1, base = x % mod; n   while (y) { n     if (y & 1) re = (re * base) % mod; n     base = (base * base) % mod; n     y >>= 1; n   } n   return re; n } n struct node { n   int to, w; n   node(int to, int w) : to(to), w(w) {} n }; n int n, k; n vector<node> e[100050]; n int deep[100050], f[100050], anc[100050][25]; n int up[100050], cnt[2][100050]; n void init() { n   for (int i = 0; i <= n; i++) e[i].clear(); n   memset(f, 0, sizeof(f)), memset(anc, 0, sizeof(anc)), n       memset(deep, 0, sizeof(deep)); n   memset(up, 0, sizeof(up)), memset(cnt, 0, sizeof(cnt)); n   deep[1] = 1; n } n void dfs1(int x, int fa) { n   anc[x][0] = f[x]; n   for (int i = 1; i <= 20; i++) anc[x][i] = anc[anc[x][i - 1]][i - 1]; n   for (int i = 0; i < e[x].size(); i++) { n     int next = e[x][i].to; n     if (next != fa) { n       f[next] = x; n       deep[next] = deep[x] + 1; n       up[next] = -e[x][i].w; n       dfs1(next, x); n     } n   } n } n int lca(int x, int y) { n   if (deep[x] < deep[y]) swap(x, y); n   for (int i = 20; i >= 0; i--) n     if (deep[anc[x][i]] >= deep[y]) x = anc[x][i]; n   if (x == y) return x; n   for (int i = 20; i >= 0; i--) n     if (anc[x][i] != anc[y][i]) x = anc[x][i], y = anc[y][i]; n   return f[x]; n } n void dfs2(int x) { n   for (int i = 0; i < e[x].size(); i++) { n     int next = e[x][i].to; n     if (next == anc[x][0]) continue; n     dfs2(next); n     for (int j = 0; j < 2; j++) cnt[j][x] += cnt[j][next]; n   } n } n int main() { n   scanf( %d , &n); n   init(); n   for (int i = 1; i < n; i++) { n     int u, v, w; n     scanf( %d%d%d , &u, &v, &w); n     if (w == 0) { n       e[u].push_back(node(v, 0)); n       e[v].push_back(node(u, 0)); n     } else { n       e[u].push_back(node(v, 1)); n       e[v].push_back(node(u, -1)); n     } n   } n   dfs1(1, 0); n   scanf( %d , &k); n   int pre = 1, now; n   while (k--) { n     scanf( %d , &now); n     int x = lca(pre, now); n     cnt[0][pre]++; n     cnt[0][x]--; n     cnt[1][now]++; n     cnt[1][x]--; n     pre = now; n   } n   dfs2(1); n   int ans = 0; n   for (int i = 1; i <= n; i++) { n     if (!up[i]) continue; n     if (up[i] == -1) n       ans = (ans + qpow(2, cnt[0][i]) - 1) % mod; n     else n       ans = (ans + qpow(2, cnt[1][i]) - 1) % mod; n   } n   ans = (ans + mod) % mod; n   printf( %d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n map<int, int> p; n int a[100005]; n int a1[100005], a2[100005]; n int n, flag, pl = 1; n int main() { n   scanf( %d , &n); n   for (int i = 1; i <= n; i++) { n     scanf( %d , &a[i]); n     if (p[a[i]] == 0) p[a[i]] = pl++; n   } n   for (int i = 1; i <= n; i++) { n     a1[i] = p[a[i]]; n     a2[a1[i]] = a[i]; n   } n   int l = p.size(); n   for (int i = 1; i <= l; i++) { n     if (a1[a2[i]] != i) flag = 1; n   } n   if (flag) n     printf( -1 n ); n   else { n     printf( %d n , p.size()); n     for (int i = 1; i <= n; i++) { n       if (i != 1) printf(   ); n       printf( %d , a1[i]); n     } n     printf(  n ); n     for (int i = 1; i <= l; i++) { n       if (i != 1) printf(   ); n       printf( %d , a2[i]); n     } n     printf(  n ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int INF = 0x3f3f3f3f; n const double PI = 3.14159265358979323846; n const int MAXN = 1000005; n int n, x[MAXN], y[MAXN], r[MAXN]; n int d[MAXN]; n vector<int> G[MAXN]; n inline double dis(double x1, double y1, double x2, double y2) { n   double d = (x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2); n   return sqrt(d); n } n int judge(int i, int j) { n   if (r[i] + r[j] <= dis(x[i], y[i], x[j], y[j])) n     return 0; n   else if (r[i] > r[j]) n     return 1; n   else if (r[j] > r[i]) n     return 2; n } n double calc(int i) { return PI * r[i] * r[i]; } n double ans = 0; n void dfs(int i, int dis) { n   if (d[i] >= dis) n     return; n   else n     d[i] = dis; n   for (int v : G[i]) { n     dfs(v, dis + 1); n   } n } n int main() { n   scanf( %d , &n); n   for (int i = (1); i < (n + 1); i++) scanf( %d%d%d , x + i, y + i, r + i); n   for (int i = (1); i < (n + 1); i++) { n     for (int j = (i + 1); j < (n + 1); j++) { n       int status = judge(i, j); n       if (status == 1) { n         G[i].push_back(j); n       } else if (status == 2) { n         G[j].push_back(i); n       } n     } n   } n   for (int i = (1); i < (n + 1); i++) { n     if (!d[i]) dfs(i, 1); n   } n   for (int i = (1); i < (n + 1); i++) { n     if (d[i] <= 2) n       ans += calc(i); n     else if (d[i] & 1) n       ans -= calc(i); n     else n       ans += calc(i); n   } n   printf( %.11f n , ans); n } n


#include <bits/stdc++.h> n using namespace std; n using ll = long long; n constexpr ll mod = 1e9 + 7; n template <typename A, typename B> n string to_string(pair<A, B> p); n template <typename A, typename B, typename C> n string to_string(tuple<A, B, C> p); n template <typename A, typename B, typename C, typename D> n string to_string(tuple<A, B, C, D> p); n string to_string(const string& s) { return     + s +    ; } n string to_string(const char c) { return string(1, c); } n string to_string(const char* s) { return to_string((string)s); } n string to_string(bool b) { return (b ?  true  :  false ); } n string to_string(vector<bool> v) { n   bool first = true; n   string res =  { ; n   for (int i = 0; i < static_cast<int>(v.size()); i++) { n     if (!first) { n       res +=  ,  ; n     } n     first = false; n     res += to_string(v[i]); n   } n   res +=  } ; n   return res; n } n template <size_t N> n string to_string(bitset<N> v) { n   string res =   ; n   for (int i = int(N) - 1; i >= 0; --i) { n     res += static_cast<char>( 0  + v[i]); n   } n   return res; n } n template <typename A> n string to_string(A v) { n   bool first = true; n   string res =  { ; n   for (const auto& x : v) { n     if (!first) { n       res +=  ,  ; n     } n     first = false; n     res += to_string(x); n   } n   res +=  } ; n   return res; n } n template <typename A, typename B> n string to_string(pair<A, B> p) { n   return  (  + to_string(p.first) +  ,   + to_string(p.second) +  ) ; n } n template <typename A, typename B, typename C> n string to_string(tuple<A, B, C> p) { n   return  (  + to_string(get<0>(p)) +  ,   + to_string(get<1>(p)) +  ,   + n          to_string(get<2>(p)) +  ) ; n } n template <typename A, typename B, typename C, typename D> n string to_string(tuple<A, B, C, D> p) { n   return  (  + to_string(get<0>(p)) +  ,   + to_string(get<1>(p)) +  ,   + n          to_string(get<2>(p)) +  ,   + to_string(get<3>(p)) +  ) ; n } n void debug_out() { cerr <<   n ; } n template <typename Head, typename... Tail> n void debug_out(Head H, Tail... T) { n   cerr <<     << to_string(H); n   debug_out(T...); n } n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   ll n; n   cin >> n; n   string s; n   cin >> s; n   vector<ll> A(n); n   for (auto& _a : A) cin >> _a; n   vector<int> left, right; n   for (int i = 0; i < n; ++i) { n     if (s[i] ==  L ) { n       left.push_back(A[i]); n     } else { n       right.push_back(A[i]); n     } n   } n   if (0) { n   }; n   if (0) { n   }; n   sort(left.begin(), left.end()); n   sort(right.begin(), right.end()); n   int nl = left.size(), nr = right.size(); n   ll ans = INT_MAX; n   for (int r = 0; r < nr; ++r) { n     auto it = lower_bound(left.begin(), left.end(), right[r]); n     if (it != left.end()) { n       ans = min(ans, ll(*it - right[r]) / 2); n     } n   } n   if (ans == INT_MAX) ans = -1; n   cout << ans <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n template <class T> n typename T::value_type arr_sum(const T& v, int n) { n   typename T::value_type sum = 0; n   for (int i = 0; i < n; ++i) sum += v[i]; n   return sum; n } n struct Sync_stdio { n   Sync_stdio() { n     cin.tie(NULL); n     ios_base::sync_with_stdio(false); n   } n } _sync_stdio; n vector<int> t; n void build(vector<int>& a, int v, int tl, int tr) { n   if (tl == tr) n     t[v] = a[tl]; n   else { n     int tm = (tl + tr) / 2; n     build(a, v * 2 + 1, tl, tm); n     build(a, v * 2 + 2, tm + 1, tr); n     t[v] = t[v * 2 + 1] + t[v * 2 + 2]; n   } n } n int sum(int v, int tl, int tr, int l, int r) { n   if (l > r) return 0; n   if (l == tl && r == tr) return t[v]; n   int tm = (tl + tr) / 2; n   return sum(v * 2 + 1, tl, tm, l, min(r, tm)) + n          sum(v * 2 + 2, tm + 1, tr, max(l, tm + 1), r); n } n void update(int v, int tl, int tr, int pos, int new_val) { n   if (tl == tr) { n     t[v] = new_val; n   } else { n     int tm = (tl + tr) / 2; n     if (pos <= tm) n       update(v * 2 + 1, tl, tm, pos, new_val); n     else n       update(v * 2 + 2, tm + 1, tr, pos, new_val); n     t[v] = t[v * 2 + 1] + t[v * 2 + 2]; n   } n } n void add(int v, int tl, int tr, int pos, int new_val) { n   if (tl == tr) { n     t[v] += new_val; n   } else { n     int tm = (tl + tr) / 2; n     if (pos <= tm) n       add(v * 2 + 1, tl, tm, pos, new_val); n     else n       add(v * 2 + 2, tm + 1, tr, pos, new_val); n     t[v] = t[v * 2 + 1] + t[v * 2 + 2]; n   } n } n int main() { n   int n, q; n   cin >> n >> q; n   t.resize(4 * n); n   vector<int> v(n, 1); n   int flag = 0; n   build(v, 0, 0, n - 1); n   int l = 0, r = v.size(); n   for (int k = 0; k < q; ++k) { n     int tmp; n     cin >> tmp; n     if (tmp == 1) { n       int x; n       cin >> x; n       if ((x <= (r - l) / 2) ^ flag) { n         if (flag) { n           x = r - l - x; n         } n         for (int i = l + x, j = l + x - 1; j >= l; ++i, --j) { n           add(0, 0, n - 1, i, sum(0, 0, n - 1, j, j)); n         } n         l += x; n         flag = 0; n       } else { n         if (flag) { n           x = r - l - x; n         } n         for (int i = l + x, j = l + x - 1; i < r; ++i, --j) { n           add(0, 0, n - 1, j, sum(0, 0, n - 1, i, i)); n         } n         r = l + x; n         flag = 1; n       } n     } else { n       int tl, tr; n       cin >> tl >> tr; n       if (!flag) { n         cout << sum(0, 0, n - 1, l + tl, l + tr - 1) << endl; n       } else { n         cout << sum(0, 0, n - 1, r - tr, r - tl - 1) << endl; n       } n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int inf = 0x3f3f3f3f; n const long long infll = 0x3f3f3f3f3f3f3f3fLL; n inline long long read() { n   long long x = 0, f = 1; n   char ch = getchar(); n   while (ch <  0  || ch >  9 ) { n     if (ch ==  - ) f = -1; n     ch = getchar(); n   } n   while (ch >=  0  && ch <=  9 ) { n     x = x * 10 + ch -  0 ; n     ch = getchar(); n   } n   return x * f; n } n set<pair<long long, int> > s; n pair<long long, long long> a[200005]; n pair<pair<long long, long long>, int> brige[200005]; n set<pair<long long, int> >::iterator it; n int ans[200005]; n int main() { n   int n = read(); n   int m = read(); n   for (int i = 1; i <= n; i++) { n     cin >> a[i].first >> a[i].second; n   } n   for (int i = 1; i < n; i++) { n     brige[i] = make_pair( n         make_pair(a[i].second - a[i + 1].first, a[i].first - a[i + 1].second), n         i); n   } n   sort(brige + 1, brige + n); n   for (int i = 1; i <= m; i++) { n     long long kiss; n     cin >> kiss; n     s.insert(make_pair(kiss, i)); n   } n   for (int i = 1; i < n; i++) { n     long long r = -brige[i].first.second; n     long long l = -brige[i].first.first; n     it = s.lower_bound(make_pair(r, inf)); n     if (it == s.begin()) { n       printf( No n ); n       return 0; n     } n     it--; n     if (it->first < l) { n       printf( No n ); n       return 0; n     } n     ans[brige[i].second] = it->second; n     s.erase(it); n   } n   printf( Yes n ); n   for (int i = 1; i < n; i++) { n     printf( %d  , ans[i]); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n ifstream fin( input.txt ); n ofstream fout( output.txt ); n const long long mod = 1000000000 + 7; n const int maxn = 500 + 10; n const int inf = 1000000000; n int n, m, par[maxn][maxn][2], dist[maxn][maxn][2]; n vector<int> v[maxn], e[2]; n queue<pair<pair<int, int>, int> > q; n bool mark[maxn][maxn][2]; n void pr(int x, int y, int o) { n   if (par[x][y][o] == -1) return; n   if (o == 0) { n     pr(x, par[x][y][o], 1 - o); n     e[o].push_back(y); n   } else { n     pr(par[x][y][o], y, 1 - o); n     e[o].push_back(x); n   } n } n int main() { n   for (int i = 0; i < maxn; i++) n     for (int j = 0; j < maxn; j++) dist[i][j][0] = dist[i][j][1] = inf; n   cin >> n >> m; n   for (int i = 1; i <= m; i++) { n     int x, y; n     cin >> x >> y; n     v[x].push_back(y); n     v[y].push_back(x); n   } n   q.push(make_pair(make_pair(1, n), 0)); n   par[1][n][0] = -1; n   mark[1][n][0] = 1; n   dist[1][n][0] = 0; n   while (!q.empty()) { n     int x = q.front().first.first; n     int y = q.front().first.second; n     int o = q.front().second; n     q.pop(); n     if (o == 0) { n       for (int i = 0; i < v[x].size(); i++) n         if (!mark[v[x][i]][y][1]) { n           q.push(make_pair(make_pair(v[x][i], y), 1)); n           dist[v[x][i]][y][1] = dist[x][y][0] + 1; n           mark[v[x][i]][y][1] = 1; n           par[v[x][i]][y][1] = x; n         } n     } else { n       for (int i = 0; i < v[y].size(); i++) n         if (v[y][i] != x && !mark[x][v[y][i]][0]) { n           q.push(make_pair(make_pair(x, v[y][i]), 0)); n           dist[x][v[y][i]][0] = dist[x][y][1] + 1; n           mark[x][v[y][i]][0] = 1; n           par[x][v[y][i]][0] = y; n         } n     } n   } n   if (dist[n][1][0] != inf) { n     cout << dist[n][1][0] / 2 << endl; n     pr(n, 1, 0); n     cout << 1 <<    ; n     for (int i = 0; i < e[1].size(); i++) cout << e[1][i] <<    ; n     cout << endl << n <<    ; n     for (int i = 0; i < e[0].size(); i++) cout << e[0][i] <<    ; n     cout << endl; n   } else n     cout << -1 << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long MAXN = 1e7 + 1; n char used[MAXN] = {0}; n int lowest_divisor[MAXN]; n int numbers[2][MAXN], numbers2[2][MAXN]; n void sieve() { n   for (int i = 2; i < MAXN; i += 2) { n     used[i] = 1; n     lowest_divisor[i] = 2; n   } n   for (long long i = 3; i < MAXN; i += 2) { n     if (!used[i]) { n       used[i] = 1; n       lowest_divisor[i] = i; n       for (long long j = i; (j * i) < MAXN; j += 2) { n         if (!used[i * j]) { n           used[i * j] = 1; n           lowest_divisor[i * j] = i; n         } n       } n     } n   } n } n void factor(int n, bool fl) { n   while (n > 1) { n     numbers[fl][lowest_divisor[n]]++; n     n /= lowest_divisor[n]; n   } n } n int main() { n   sieve(); n   int n, m; n   scanf( %d%d , &n, &m); n   vector<int> a, b; n   a.resize(n); n   b.resize(m); n   for (int i = 0; i < n; i++) { n     int t; n     scanf( %d , &t); n     factor(t, 0); n     a[i] = t; n   } n   for (int i = 0; i < m; i++) { n     int t; n     scanf( %d , &t); n     factor(t, 1); n     b[i] = t; n   } n   for (int i = 2; i < MAXN; i++) { n     numbers2[0][i] = numbers[0][i] - min(numbers[0][i], numbers[1][i]); n     numbers2[1][i] = numbers[1][i] - min(numbers[0][i], numbers[1][i]); n   } n   cout << n <<     << m << endl; n   for (int i = 0; i < n; i++) { n     int t = a[i]; n     while (t > 1) { n       if (numbers[0][lowest_divisor[t]] > numbers2[0][lowest_divisor[t]]) { n         numbers[0][lowest_divisor[t]]--; n         a[i] /= lowest_divisor[t]; n       } n       t /= lowest_divisor[t]; n     } n   } n   for (int i = 0; i < m; i++) { n     int t = b[i]; n     while (t > 1) { n       if (numbers[1][lowest_divisor[t]] > numbers2[1][lowest_divisor[t]]) { n         numbers[1][lowest_divisor[t]]--; n         b[i] /= lowest_divisor[t]; n       } n       t /= lowest_divisor[t]; n     } n   } n   for (auto it : a) { n     printf( %d  , it); n   } n   cout << endl; n   for (auto it : b) { n     printf( %d  , it); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n using namespace std; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   long long n, otv = 0, otv1 = 0; n   cin >> n; n   vector<long long> v(n); n   for (long long j = 0; j < n; j++) { n     cin >> v[j]; n   } n   for (long long j = 1; j < n; j++) { n     if (v[j] != v[0]) { n       otv = j; n     } n   } n   for (long long j = 0; j < n - 1; j++) { n     if (v[j] != v[n - 1]) { n       otv1 = n - 1 - j; n       cout << max(otv, otv1); n       return 0; n     } n   } n   cout << max(otv, otv1); n } n


#include <bits/stdc++.h> n using namespace std; n long long int mod = 1e9 + 7; n const double error = 1e-10; n const double PI = acos(-1); n inline long long int MOD(long long int x, long long int m = mod) { n   long long int y = x % m; n   return (y >= 0) ? y : y + m; n } n const int inf = INT_MAX; n const long long int infl = LLONG_MAX; n const int nmax = 1e5 + 3; n int grundy[nmax], best[nmax]; n vector<int> reach[nmax]; n void dp() { n   for (int i = 1; i < nmax; i++) { n     sort(reach[i].begin(), reach[i].end()); n     int j, k; n     for (k = 0, j = 0; k < nmax; k++) { n       while (j < reach[i].size()) { n         if (reach[i][j] >= k) break; n         j++; n       } n       if (reach[i].size() == j || reach[i][j] > k) break; n     } n     grundy[i] = k; n     reach[i].clear(); n     int sum = 0, xorval = 0; n     for (int j = i; j > 0 && sum < nmax; j--) { n       sum += j; n       if (sum >= nmax) continue; n       xorval ^= grundy[j]; n       reach[sum].push_back(xorval); n       if (xorval == 0) best[sum] = i - j + 1; n     } n   } n } n int main() { n   int n; n   scanf( %d , &n); n   dp(); n   if (grundy[n] == 0) n     printf( -1 ); n   else n     printf( %d , best[n]); n } n


#include <bits/stdc++.h> n using namespace std; n int n, x1, x2; n struct server { n   int c, ind; n   bool b1, b2; n   int r1, r2; n } * *srv; n void qs(server** a, int l, int r) { n   int i = l, j = r; n   int p = a[(l + r) >> 1]->c; n   do { n     while (i < r && a[i]->c < p) i++; n     while (j > l && a[j]->c > p) j--; n     if (i <= j) swap(a[i++], a[j--]); n   } while (i <= j); n   if (i < r) qs(a, i, r); n   if (j > l) qs(a, l, j); n } n int main() { n   scanf( %d %d %d , &n, &x1, &x2); n   srv = new server*[n]; n   for (int i = 0; i < n; i++) { n     srv[i] = new server; n     scanf( %d , &srv[i]->c); n     srv[i]->ind = i; n   } n   qs(srv, 0, n - 1); n   for (int i = n - 1; i >= 0; i--) { n     srv[i]->r1 = ((long long)x1 + (long long)srv[i]->c - (long long)1) / n                  (long long)srv[i]->c; n     srv[i]->r2 = ((long long)x2 + (long long)srv[i]->c - (long long)1) / n                  (long long)srv[i]->c; n     if (i < n - 1 && srv[i + 1]->b1) n       srv[i]->b1 = 1; n     else if (srv[i]->r1 <= n - i) n       srv[i]->b1 = 1; n     else n       srv[i]->b1 = 0; n     if (i < n - 1 && srv[i + 1]->b2) n       srv[i]->b2 = 1; n     else if (srv[i]->r2 <= n - i) n       srv[i]->b2 = 1; n     else n       srv[i]->b2 = 0; n   } n   for (int i = 0; i < n - 1; i++) { n     if (srv[i]->r1 < n - i && srv[i + srv[i]->r1]->b2) { n       for (int j = i + srv[i]->r1; j < n; j++) { n         if (srv[j]->r2 <= n - j) { n           printf( Yes n ); n           printf( %d %d n , srv[i]->r1, srv[j]->r2); n           for (int k = 0; k < srv[i]->r1; k++) n             printf( %d  , srv[i + k]->ind + 1); n           printf(  n ); n           for (int k = 0; k < srv[j]->r2; k++) n             printf( %d  , srv[j + k]->ind + 1); n           printf(  n ); n           return 0; n         } n       } n     } n   } n   for (int i = 0; i < n - 1; i++) { n     if (srv[i]->r2 < n - i && srv[i + srv[i]->r2]->b1) { n       for (int j = i + srv[i]->r2; j < n; j++) { n         if (srv[j]->r1 <= n - j) { n           printf( Yes n ); n           printf( %d %d n , srv[j]->r1, srv[i]->r2); n           for (int k = 0; k < srv[j]->r1; k++) n             printf( %d  , srv[j + k]->ind + 1); n           printf(  n ); n           for (int k = 0; k < srv[i]->r2; k++) n             printf( %d  , srv[i + k]->ind + 1); n           printf(  n ); n           return 0; n         } n       } n     } n   } n   printf( No n ); n   return 0; n } n


#include<bits/stdc++.h> n using namespace std; n  n #define imax(a,b) (((a)>(b))?(a):(b)) n #define imin(a,b) (((a)<(b))?(a):(b)) n #define mp make_pair n  n typedef long long ll; n  n const int N = 3e5 + 10; n  n typedef pair<int, int> intp; n int T, n, dfstime, L[N], R[N], ans, num; n vector<int> sd[2][N]; n set<intp> np; n  n void read(int& x) n { n  x = 0; char ch = getchar(); bool f = 0; n  while (!(ch >=  0  && ch <=  9 )) f |= (ch ==  - ), ch = getchar(); n  while ((ch >=  0  && ch <=  9 )) x = (x << 3) + (x << 1) + ch -  0 , ch = getchar(); n  if (f) x = -x; n } n  n void kdfs(int x) n { n  L[x] = ++dfstime; n  for (vector<int>::iterator iter = sd[1][x].begin();iter != sd[1][x].end();++iter) n   kdfs(*iter); n  R[x] = dfstime; n } n  n int nofa(int fa, int son){return (L[fa] <= L[son] && R[son] <= R[fa]) ? 0 : 1;} n  n void sdfs(int x) n { n  int mark = num; n  set<intp>::iterator now = np.lower_bound({ L[x],0 }); n  if (now != np.end()) num += nofa(x, now->second); n  if (now != np.begin()) n  { n   set<intp>::iterator fnow = now--; n   num += nofa(now->second, x); n   if (fnow != np.end()) num -= nofa(now->second, fnow->second); n  } n  np.insert({ L[x],x }); n  ans = imax(ans, num); n  for (vector<int>::iterator iter = sd[0][x].begin();iter != sd[0][x].end();++iter) n   sdfs(*iter); n  np.erase({ L[x],x }); n  num = mark; n } n  n int main() n { n  read(T); n  while (T--) n  { n   read(n); n   for (int h = 0;h < 2;++h) n   for (int i = 2;i <= n;++i) n   { n    int a; read(a); n    sd[h][a].push_back(i); n   } n   dfstime = 0; kdfs(1); n   ans = 1; num = 1; sdfs(1); n   for (int i = 1;i <= n;++i) sd[0][i].clear(), sd[1][i].clear(); n   printf( %d n , ans); n  } n  return 0; n }


#include <bits/stdc++.h> n bool vis[1001][1001], ret[1001][1001]; n const int dx[] = {1, 1, 0, -1, -1, -1, 0, 1}; n const int dy[] = {0, 1, 1, 1, 0, -1, -1, -1}; n int main() { n   int n, m; n   scanf( %d %d , &n, &m); n   for (int i = 1; i <= n; ++i) { n     scanf(  n ); n     for (int j = 1; j <= m; ++j) { n       char ch = getchar(); n       if (ch ==  # ) vis[i][j] = 1; n     } n   } n   for (int i = 2; i < n; ++i) n     for (int j = 2; j < m; ++j) { n       bool ok = true; n       for (int k = 0; k < 8; ++k) n         if (!vis[i + dx[k]][j + dy[k]]) { n           ok = false; n           break; n         } n       if (ok) n         for (int k = 0; k < 8; ++k) ret[i + dx[k]][j + dy[k]] = true; n     } n   for (int i = 1; i <= n; ++i) n     for (int j = 1; j <= m; ++j) n       if (vis[i][j] != ret[i][j]) return printf( NO ), 0; n   printf( YES ); n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 255; n double f[N][N * N]; n int x, k, p; n int main() { n   scanf( %d%d%d , &x, &k, &p); n   for (int i = 0; i <= k; i++) { n     int xx = x + i, cnt = 0; n     while (xx % 2 == 0) { n       xx /= 2; n       cnt++; n     } n     f[0][i] = cnt; n   } n   for (int i = 1; i <= k; i++) { n     for (int j = 0; j <= k; j++) { n       f[i][j << 1] += (f[i - 1][j] + 1) * p / 100.0; n       f[i][j] += f[i - 1][j + 1] * (100 - p) / 100.0; n     } n   } n   printf( %.10lf n , f[k][0]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n using ll = long long; n using ld = long double; n using ull = unsigned long long; n template <typename T1, typename T2> n inline void chkmin(T1 &x, const T2 &y) { n   if (x > y) x = y; n } n template <typename T1, typename T2> n inline void chkmax(T1 &x, const T2 &y) { n   if (x < y) x = y; n } n const int INF = 1e9 + 228 + 1337; n bool check(string s, string t) { n   sort(s.begin(), s.end()); n   sort(t.begin(), t.end()); n   return s == t; n } n void solve() { n   int n; n   vector<string> a(2); n   cin >> n >> a[0] >> a[1]; n   if (!check(a[0], a[1])) { n     cout << -1 <<   n ; n     return; n   } n   vector<vector<int>> dp(n + 1, vector<int>(n + 1, INF)); n   dp[0][0] = 0; n   vector<vector<vector<int>>> cnt( n       2, vector<vector<int>>(n + 1, vector<int>(26, 0))); n   reverse(a[0].begin(), a[0].end()); n   reverse(a[1].begin(), a[1].end()); n   for (int it = 0; it < 2; it++) { n     for (int i = 1; i <= n; i++) { n       for (int alpha = 0; alpha < 26; alpha++) { n         cnt[it][i][alpha] = cnt[it][i - 1][alpha]; n       } n       cnt[it][i][a[it][i - 1] -  a ]++; n     } n   } n   for (int i = 0; i <= n; i++) { n     for (int j = 0; j <= i; j++) { n       if (i + 1 <= n && a[0][i] == a[1][j]) { n         assert(j + 1 <= n); n         chkmin(dp[i + 1][j + 1], dp[i][j]); n       } n       char x = a[1][j] -  a ; n       if (j + 1 <= n && cnt[0][i][x] > cnt[1][j][x]) { n         chkmin(dp[i][j + 1], dp[i][j]); n       } n       if (i + 1 <= n) { n         chkmin(dp[i + 1][j], dp[i][j] + 1); n       } n     } n   } n   int ans = -1; n   if (dp[n][n] != INF) { n     ans = dp[n][n]; n   } n   cout << ans <<   n ; n } n signed main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   int t; n   cin >> t; n   while (t--) { n     solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int INF = 0x3f3f3f3f; n const int maxn = 5000; n int n, fir[maxn + 10], tmpN; n int nedge, to[2 * maxn + 10], nex[2 * maxn + 10], dis[2 * maxn + 10]; n bool no[2 * maxn + 10]; n long long ans, dp[maxn + 10], num[maxn + 10], allDis[maxn + 10]; n int p[2]; n inline void add_edge(int x, int y, int w) { n   to[nedge] = y; n   dis[nedge] = w; n   nex[nedge] = fir[x]; n   fir[x] = nedge++; n } n void dfs(int x, int fa) { n   num[x] = 0; n   dp[x] = 0; n   for (int i = fir[x]; ~i; i = nex[i]) { n     int y = to[i]; n     if (y == fa) continue; n     int w = dis[i]; n     dfs(y, x); n     num[x] += num[y]; n     dp[x] += dp[y] + (long long)num[y] * w; n   } n   num[x]++; n } n void update(int x, int k) { n   if (p[k] < 0 || allDis[x] < allDis[p[k]]) p[k] = x; n } n void dfs2(int x, int fa, int k) { n   for (int i = fir[x]; ~i; i = nex[i]) { n     int y = to[i]; n     if (y == fa) continue; n     int w = dis[i]; n     allDis[y] = n         allDis[x] - (long long)w * num[y] + (long long)(tmpN - num[y]) * w; n     update(y, k); n     dfs2(y, x, k); n   } n } n void solve() { n   ans = -1; n   int N[2]; n   for (int i = 0; i < 2 * (n - 1); i += 2) { n     memset(p, -1, sizeof p); n     int x = to[i], y = to[i ^ 1], w = dis[i]; n     dfs(x, y); n     tmpN = N[0] = num[x]; n     allDis[x] = dp[x]; n     update(x, 0); n     dfs2(x, y, 0); n     dfs(y, x); n     tmpN = N[1] = num[y]; n     allDis[y] = dp[y]; n     update(y, 1); n     dfs2(y, x, 1); n     long long ret = n         N[1] * (allDis[p[0]] + (long long)N[0] * w) + N[0] * allDis[p[1]]; n     long long ret2 = 0; n     for (int(j) = 1; (j) <= (n); (j)++) ret2 += allDis[j]; n     ret2 /= 2; n     ret += ret2; n     if (ans < 0 || ret < ans) ans = ret; n   } n   cout << ans << endl; n } n int main() { n   std::ios::sync_with_stdio(false); n   int x, y, w; n   while (cin >> n) { n     memset(fir, -1, (n + 1) * sizeof fir[0]); n     nedge = 0; n     for (int(i) = 0; (i) < (n - 1); (i)++) { n       cin >> x >> y >> w; n       add_edge(x, y, w); n       add_edge(y, x, w); n     } n     solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, sum = 0, cnt = 0; n   cin >> n; n   int a[n + 1][n + 1]; n   vector<int> v; n   vector<int> v1; n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < n; j++) { n       cin >> a[i][j]; n     } n   } n   int no = 0; n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < n; j++) { n       if (a[i][j] == 1 || a[i][j] == 3) { n         no = 1; n       } n     } n     if (no != 1) { n       v.push_back(i + 1); n     } n     no = 0; n   } n   if (v.size() == 0) { n     cout <<  0  << endl; n     return 0; n   } n   cout << v.size() << endl; n   for (int i = 0; i < v.size(); i++) { n     cout << v[i] <<    ; n   } n   cout << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n double d(long long int x, long long int y, long long int x1, long long int y1) { n   return sqrt((x - x1) * (x - x1) + (y - y1) * (y - y1)); n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   pair<long long int, long long int> adil; n   pair<long long int, long long int> bera; n   pair<long long int, long long int> bin; n   cin >> adil.first >> adil.second >> bera.first >> bera.second >> bin.first >> n       bin.second; n   double sum = 0; n   long long int n; n   cin >> n; n   pair<int, int> MinAdil, Min1Adil, MinBera, Min1Bera; n   double a, a1, b, b1; n   a = a1 = b = b1 = 1e10; n   for (int i = 0; i < n; i++) { n     long long int x, y; n     cin >> x >> y; n     double dist = d(x, y, bin.first, bin.second); n     sum += dist * 2; n     double krati = d(adil.first, adil.second, x, y) - dist; n     double t = dist; n     dist = krati; n     if (dist <= a) { n       Min1Adil.first = MinAdil.first; n       Min1Adil.second = MinAdil.second; n       MinAdil.first = x; n       MinAdil.second = y; n       a1 = a; n       a = dist; n     } else { n       if (dist < a1) { n         Min1Adil.first = x; n         Min1Adil.second = y; n         a1 = dist; n       } n     } n     dist = d(bera.first, bera.second, x, y) - t; n     if (dist <= b) { n       Min1Bera.first = MinBera.first; n       Min1Bera.second = MinBera.second; n       MinBera.first = x; n       MinBera.second = y; n       b1 = b; n       b = dist; n     } else { n       if (dist < b1) { n         Min1Bera.first = x; n         Min1Bera.second = y; n         b1 = dist; n       } n     } n   } n   double ans = n       min(sum - d(bin.first, bin.second, MinAdil.first, MinAdil.second) + n               d(MinAdil.first, MinAdil.second, adil.first, adil.second), n           sum - d(bin.first, bin.second, MinBera.first, MinBera.second) + n               d(MinBera.first, MinBera.second, bera.first, bera.second)); n   if (n > 1) { n     if (MinAdil.first == MinBera.first && MinAdil.second == MinBera.second) { n       ans = min( n           ans, sum - d(bin.first, bin.second, MinAdil.first, MinAdil.second) - n                    d(bin.first, bin.second, Min1Bera.first, Min1Bera.second) + n                    d(MinAdil.first, MinAdil.second, adil.first, adil.second) + n                    d(Min1Bera.first, Min1Bera.second, bera.first, bera.second)); n       ans = min( n           ans, sum - d(bin.first, bin.second, Min1Adil.first, Min1Adil.second) - n                    d(bin.first, bin.second, MinBera.first, MinBera.second) + n                    d(Min1Adil.first, Min1Adil.second, adil.first, adil.second) + n                    d(MinBera.first, MinBera.second, bera.first, bera.second)); n     } else { n       ans = min(ans, n                 sum - d(bin.first, bin.second, MinAdil.first, MinAdil.second) - n                     d(bin.first, bin.second, MinBera.first, MinBera.second) + n                     d(MinAdil.first, MinAdil.second, adil.first, adil.second) + n                     d(MinBera.first, MinBera.second, bera.first, bera.second)); n     } n   } n   cout << setprecision(10) << fixed << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, m, k; n   cin >> n >> m >> k; n   int arr[n]; n   for (int i = 0; i < n; i++) { n     cin >> arr[i]; n   } n   int suma = 0, ans = 0, count = 0; n   for (int i = n - 1; i >= 0; i--) { n     if (suma + arr[i] <= k) n       suma = suma + arr[i]; n     else { n       suma = arr[i]; n       count += 1; n     } n     if (count >= m) break; n     ans += 1; n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <class T> n inline void rd(T &x) { n   x = 0; n   char c = getchar(); n   int f = 1; n   while (!isdigit(c)) { n     if (c ==  - ) f = -1; n     c = getchar(); n   } n   while (isdigit(c)) x = x * 10 -  0  + c, c = getchar(); n   x *= f; n } n template <class T> n inline void cmax(T &x, T y) { n   if (x < y) x = y; n } n const int N = (1 << 18) + 10; n int f[N][2][2]; n int vis[N]; n void solve(int l, int r, int c) { n   if (l + 1 == r) { n     int x = vis[l], y = vis[r]; n     cmax(f[c][x][y], x | y); n     cmax(f[c][y][x], x | y); n     return; n   } n   int mid = l + r >> 1; n   solve(l, mid, c << 1), solve(mid + 1, r, c << 1 | 1); n   for (int lx = 0; lx < 2; ++lx) n     for (int ly = 0; ly < 2; ++ly) n       if (f[c << 1][lx][ly] >= 0) n         for (int rx = 0; rx < 2; ++rx) n           for (int ry = 0; ry < 2; ++ry) n             if (f[c << 1 | 1][rx][ry] >= 0) { n               for (int S = 0; S < 8; ++S) { n                 int a = (S & 1 ? lx : rx); n                 int b = (S & 2 ? ly : ry); n                 int d = (S & 4 ? (lx ^ rx ^ a) : b); n                 cmax(f[c][a][d], f[c << 1][lx][ly] + f[c << 1 | 1][rx][ry] + n                                      (lx | rx) + (ly | ry) + n                                      ((lx ^ rx ^ a) | b)); n               } n             } n } n int main() { n   int n, k; n   rd(n), rd(k); n   for (int i = 1, x; i <= k; ++i) rd(x), vis[x - 1] = 1; n   memset(f, -0x3f, sizeof(f)); n   solve(0, (1 << n) - 1, 1); n   int ans = -0x3f3f3f3f; n   for (int x = 0; x < 2; ++x) n     for (int y = 0; y < 2; ++y) cmax(ans, f[1][x][y] + (x | y)); n   printf( %d , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int BUF = 200005; n const long long INF = 1LL << 61; n int n; n long long toAdd; n long long arrive[BUF]; n int maxOrder[BUF]; n void read() { n   cin >> n >> toAdd; n   for (int i = 0; i < n; ++i) { n     cin >> arrive[i]; n   } n   for (int i = 0; i < n; ++i) { n     cin >> maxOrder[i]; n   } n } n void work() { n   for (int i = 0; i + 1 < n; ++i) { n     if (maxOrder[i] != maxOrder[i + 1] && n         (maxOrder[i] != i + 1 || maxOrder[i] > maxOrder[i + 1])) { n       cout <<  No  << endl; n       return; n     } n   } n   if (maxOrder[n - 1] != n) { n     cout <<  No  << endl; n     return; n   } n   vector<long long> ans; n   for (int idx = 0; idx < n;) { n     int nexIdx = idx; n     while (nexIdx < n && maxOrder[nexIdx] == maxOrder[idx]) { n       ++nexIdx; n     } n     long long start = n         nexIdx < n ? arrive[nexIdx] + toAdd - (nexIdx - idx) : INF; n     for (int i = 0; i < nexIdx - idx; ++i) { n       if (i == 0 && start + nexIdx - idx - 1 < arrive[idx] + toAdd) { n         cout <<  No  << endl; n         return; n       } else if (i > 0 && start + i < arrive[idx + i] + toAdd + 1) { n         cout <<  No  << endl; n         return; n       } n       ans.push_back(start + i); n     } n     idx = nexIdx; n   } n   for (int i = 0; i < n; ++i) { n     if (i + 1 < n && maxOrder[i] != maxOrder[i + 1] && n         arrive[i + 1] + toAdd <= ans[i]) { n       cout <<  No  << endl; n       return; n     } n     if (arrive[i] + toAdd > ans[i]) { n       cout <<  No  << endl; n       return; n     } n   } n   cout <<  Yes  << endl; n   for (int i = 0; i < n; ++i) { n     if (i) cout <<    ; n     cout << ans[i]; n   } n   cout << endl; n } n int main() { n   read(); n   work(); n   return 0; n } n


#include <bits/stdc++.h> n const long long int M = 1e9 + 7; n using namespace std; n bool visit[100000]; n vector<int> G[100000]; n void dfs1(int v, stack<int> &s) { n   visit[v] = true; n   for (int i = 0; i < G[v].size(); i++) { n     int c = G[v][i]; n     if (!visit[c]) dfs1(c, s); n   } n   s.push(v); n } n void dfs(int n) { n   stack<int> s; n   for (int i = 1; i <= n; i++) n     if (!visit[i]) dfs1(i, s); n   while (!s.empty()) { n     cout << s.top() <<    ; n     s.pop(); n   } n } n int main() { n   ios::sync_with_stdio(false); n   int n, m, i, a, b; n   cin >> n >> m; n   for (i = 0; i < m; i++) { n     cin >> a >> b; n     G[b].push_back(a); n   } n   dfs(n); n   return 0; n } n


#include <bits/stdc++.h> n const long long mn = 1000000009; n long long x, n, m; n long long minu(long long val) { n   if (val == 0) return mn - 1; n   return val - 1; n } n long long getmod(long long val) { n   long long ret = 1; n   for (long long i = 0; i < val; i++) { n     ret *= 2; n     ret %= mn; n   } n   return minu(ret); n } n int main() { n   scanf( %d %d , &n, &m); n   x = getmod(m); n   long long ans = 1; n   while (n--) { n     ans *= x; n     ans %= mn; n     x = minu(x); n   } n   printf( %d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct Grass { n   int sizee; n   char dir; n } a[100005]; n int n; n bool flag[100005]; n int main() { n   cin >> n; n   for (int i = 1; i <= n; i++) { n     cin >> a[i].dir; n   } n   for (int i = 1; i <= n; i++) { n     cin >> a[i].sizee; n   } n   int nowx = 1; n   flag[nowx] = 1; n   while (true) { n     if (a[nowx].dir ==  > ) { n       nowx += a[nowx].sizee; n     } else { n       nowx -= a[nowx].sizee; n     } n     if (nowx > n || nowx < 1) { n       cout <<  FINITE ; n       break; n     } n     if (flag[nowx] == 1) { n       cout <<  INFINITE ; n       break; n     } n     flag[nowx] = 1; n   } n   return 0; n } n


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