#include <bits/stdc++.h> n using namespace std; n long long n, b, x, q, t, ans, l, r, m, p, now, sum; n bool ok; n string s; n map<int, int> a, pos; n map<char, int> c; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   cin >> q; n   while (q--) { n     cin >> n >> m; n     cin >> s; n     for (int i = 1; i <= m; ++i) { n       cin >> x; n       a[x + 1]++; n     } n     sum = m + 1; n     for (int i = 0; i < s.size(); ++i) { n       sum -= a[i + 1]; n       c[s[i]] += sum; n     } n     for (char j =  a ; j <=  z ; ++j) { n       cout << c[j] <<    ; n     } n     cout <<   n ; n     a.clear(); n     c.clear(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   cin >> n; n   int a[n], b[n]; n   for (int i = 0; i < n; i++) cin >> a[i]; n   b[n - 1] = a[n - 1]; n   for (int j = 0; j < n - 1; j++) b[j] = a[j] + a[j + 1]; n   for (int k = 0; k < n; k++) cout << b[k] <<    ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T> n inline T read(T& x) { n   x = 0; n   int f = 0; n   char ch = getchar(); n   while (ch <  0  || ch >  9 ) f |= (ch ==  - ), ch = getchar(); n   while (ch >=  0  && ch <=  9 ) x = x * 10 + ch -  0 , ch = getchar(); n   return x = f ? -x : x; n } n const int N = 500 + 10; n const long long INF = 2e18; n int i, j, x, n, match[N], pre[N]; n long long a[N], b[N], k[N], cost[N][N], lx[N], ly[N], slack[N]; n bool vy[N]; n void augment(int root) { n   fill(vy + 1, vy + n + 1, false); n   fill(slack + 1, slack + n + 1, INF); n   int py; n   match[py = 0] = root; n   do { n     vy[py] = true; n     int x = match[py], yy; n     long long delta = INF; n     for (int y = 1; y <= n; y++) { n       if (!vy[y]) { n         if (lx[x] + ly[y] - cost[x][y] < slack[y]) { n           slack[y] = lx[x] + ly[y] - cost[x][y]; n           pre[y] = py; n         } n         if (slack[y] < delta) { n           delta = slack[y]; n           yy = y; n         } n       } n     } n     for (int y = 0; y <= n; y++) { n       if (vy[y]) { n         lx[match[y]] -= delta; n         ly[y] += delta; n       } else { n         slack[y] -= delta; n       } n     } n     py = yy; n   } while (~match[py]); n   do { n     int cnt = pre[py]; n     match[py] = match[cnt]; n     py = cnt; n   } while (py); n } n long long KM() { n   for (int i = 1; i <= n; i++) { n     lx[i] = ly[i] = 0; n     match[i] = -1; n     for (int j = 1; j <= n; j++) lx[i] = max(lx[i], cost[i][j]); n   } n   long long ans = 0; n   for (int i = 1; i <= n; i++) augment(i); n   for (int i = 1; i <= n; i++) { n     ans += lx[i]; n     ans += ly[i]; n   } n   return ans; n } n int main() { n   read(n); n   for (i = 1; i <= n; i++) { n     read(a[i]), read(b[i]), read(k[i]); n     for (j = 1; j <= n; j++) { n       cost[i][j] = max(a[i] - b[i] * min(k[i], j - 1LL), 0LL); n     } n   } n   printf( %lld n , KM()); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int INF = (int)1e9 + 10; n const int N = (int)1e6 + 10; n const long double eps = 1e-7; n mt19937 rnd(228); n int main() { n   int n, q; n   cin >> n >> q; n   deque<int> d; n   int mn = INF; n   int mx = -INF; n   for (int i = 0; i < n; ++i) { n     int x; n     cin >> x; n     d.push_back(x); n     mn = min(mn, x); n     mx = max(mx, x); n   } n   vector<pair<int, int>> ans(N); n   for (int iter = 1; iter < N; ++iter) { n     int a = d.front(); n     d.pop_front(); n     int b = d.front(); n     ans[iter] = make_pair(a, b); n     d.pop_front(); n     d.push_front(max(a, b)); n     d.push_back(min(a, b)); n   } n   int st = -1; n   for (int i = N / 2 - 1; i >= 1; --i) { n     if (ans[i] == pair<int, int>({mx, mn})) { n       st = i; n       break; n     } n   } n   for (int i = 1; i <= q; ++i) { n     long long m; n     cin >> m; n     if (m < N) { n       cout << ans[m].first <<     << ans[m].second <<   n ; n       continue; n     } n     m -= st; n     m %= (n - 1); n     cout << ans[st + m].first <<     << ans[st + m].second <<   n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 200005; n int n, k; n pair<int, int> a[N]; n vector<pair<int, int> > Center; n bool inf; n bool Check(pair<int, int> center) { n   int cnt = 0, l = 1, r = n; n   pair<int, int> t; n   while (l <= r && cnt <= k) { n     t = make_pair(center.first - a[l].first, center.second - a[l].second); n     if (t == a[r]) { n       l++; n       r--; n     } else { n       cnt++; n       if (t < a[r]) n         r--; n       else n         l++; n     } n   } n   return (cnt <= k); n } n void Solve() { n   if (k >= n) { n     inf = true; n     return; n   } n   sort(a + 1, a + n + 1); n   pair<int, int> center; n   int res = 0; n   for (int i = 1; i <= k + 1; i++) n     for (int j = n - k; j <= n; j++) n       if (i <= j) { n         center = make_pair(a[i].first + a[j].first, a[i].second + a[j].second); n         if (Check(center)) Center.push_back(center); n       } n } n int main() { n   cin >> n >> k; n   for (int i = 1; i <= n; i++) scanf( %d %d , &a[i].first, &a[i].second); n   Solve(); n   if (inf) { n     cout << -1 << endl; n     return 0; n   } n   int res = 0; n   sort(Center.begin(), Center.end()); n   for (int i = 0; i < Center.size(); i++) n     if (i == 0 || Center[i] != Center[i - 1]) res++; n   cout << res << endl; n   int cnt = 0; n   for (int i = 0; i < Center.size(); i++) n     if (i == 0 || Center[i] != Center[i - 1]) { n       printf( %.3lf %.3lf n , (Center[i].first + 0.0) / 2.0, n              (Center[i].second + 0.0) / 2.0); n     } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n #pragma GCC optimize( Ofast,no-stack-protector ) n #pragma GCC target( sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native ) n #pragma GCC optimize( unroll-loops ) n int on_bit(int N, int pos) { return N = N | (1 << pos); } n int off_bit(int N, int pos) { return N = N & ~(1 << pos); } n bool check_bit(long long int N, int pos) { return (bool)(N & (1 << pos)); } n template <class T> n inline bool read(T &x) { n   int c = getchar(); n   int sgn = 1; n   while (~c && c <  0  || c >  9 ) { n     if (c ==  - ) sgn = -1; n     c = getchar(); n   } n   for (x = 0; ~c &&  0  <= c && c <=  9 ; c = getchar()) x = x * 10 + c -  0 ; n   x *= sgn; n   return ~c; n } n int main() { n   unsigned long long ar[500005]; n   memset((ar), 0, sizeof((ar))); n   for (unsigned long long i = 3, j = 1; i <= 500001; i += 2, j++) { n     ar[i] = ((i * 4) - 4) * j; n   } n   for (int i = 5; i <= 500001; i += 2) { n     ar[i] += ar[i - 2]; n   } n   int ___T; n   cin >> ___T; n   for (int cs = 1; cs <= ___T; cs++) { n     int n; n     cin >> n; n     cout << ar[n] <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long nt[105]; n string s; n vector<long long> V; n void ts() { n   nt[0] = 0; n   nt[1] = 0; n   for (long long i = 2; i <= 100; i++) { n     nt[i] = 1; n   } n   for (long long i = 2; i <= 100; i++) { n     if (nt[i] == 1) { n       for (long long j = i * i; j <= 100; j += i) { n         nt[j] = 0; n       } n       V.push_back(i); n     } n   } n } n signed main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   V.push_back(4); n   V.push_back(9); n   V.push_back(25); n   V.push_back(49); n   ts(); n   long long i = 0; n   long long dem = 0; n   while (i < 20) { n     cout << V[i] << endl; n     cin >> s; n     if (s ==  yes ) dem++; n     if (dem == 2) { n       cout <<  composite ; n       return 0; n     } n     i++; n   } n   cout <<  prime ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T> n void maxE(T& a, const T& b) { n   a = max(a, b); n } n template <typename T> n void minE(T& a, const T& b) { n   a = min(a, b); n } n template <typename T> n ostream& operator<<(ostream& out, const vector<T>& t_) { n   out <<  [ ; n   for (auto i : t_) out << i <<  ,  ; n   out <<  ] ; n   return out; n } n template <typename S, typename T> n ostream& operator<<(ostream& out, const pair<S, T>& rhs) { n   out <<  (  << rhs.first <<  ,  << rhs.second <<  ) ; n   return out; n } n const long long INF = 1000LL * 1000 * 1000 * 1000 * 1000 * 1000LL; n struct Val { n   long long maxPref, maxSuf, sum, maxInf; n }; n Val neutral() { n   Val r; n   r.maxPref = r.maxSuf = r.maxInf = 0; n   r.sum = 0; n   return r; n } n Val single(long long a) { n   Val r; n   r.maxPref = r.maxSuf = r.maxInf = max(0LL, a); n   r.sum = a; n   return r; n } n Val join(const Val& a, const Val& b) { n   Val r; n   r.maxPref = max(a.maxPref, a.sum + b.maxPref); n   r.maxSuf = max(a.maxSuf + b.sum, b.maxSuf); n   r.sum = a.sum + b.sum; n   r.maxInf = max(max(a.maxInf, b.maxInf), a.maxSuf + b.maxPref); n   return r; n } n struct Tree { n   int s; n   vector<Val> t; n   void init(int s_) { n     s = 1; n     while (s <= s_) s *= 2; n     t.assign(2 * s, neutral()); n   } n   Val tget(int a, int b) { n     if (a > b) n       return neutral(); n     else if (a == b) n       return t[a]; n     Val r = neutral(); n     int na = a; n     int nb = b; n     if (a % 2) na++; n     if (nb % 2 == 0) nb--; n     if (a % 2) r = join(r, t[a]); n     r = join(r, tget(na / 2, nb / 2)); n     if (b % 2 == 0) r = join(r, t[b]); n     return r; n   } n   long long get(int a, int b) { return tget(s + a, s + b).maxInf; } n }; n int n; n vector<long long> t; n Tree tree; n int main() { n   int m; n   assert(scanf( %d%d , &n, &m) == 2); n   vector<int> d, h; n   for (int i = 0; i < int(n); ++i) { n     int a; n     assert(scanf( %d , &a) == 1); n     d.push_back(a); n   } n   for (int i = 0; i < int(n); ++i) { n     int a; n     assert(scanf( %d , &a) == 1); n     h.push_back(a); n   } n   for (int j = 0; j < int(2); ++j) n     for (int i = 0; i < int(n); ++i) { n       t.push_back(h[i] * 2LL); n       t.push_back(-4LL * h[i]); n       t.push_back(h[i] * 2LL); n       t.push_back(d[i]); n     } n   tree.init(((int)((t).size()))); n   for (int i = 0; i < int(((int)((t).size()))); ++i) n     tree.t[tree.s + i] = single(t[i]); n   for (int i = tree.s - 1; i >= 1; i--) n     tree.t[i] = join(tree.t[i * 2], tree.t[i * 2 + 1]); n   if (0) cout <<  tree ready   << endl; n   if (0) n     cout <<  SIZE(t)  n          <<     << ((int)((t).size())) <<     << endl; n   for (int i = 0; i < int(m); ++i) { n     int a, b; n     assert(scanf( %d%d , &a, &b) == 2); n     a--; n     b--; n     int x, y; n     if (a <= b) { n       x = b + 1; n       y = a + n - 1; n     } else { n       x = b + 1; n       y = a - 1; n     } n     assert(x < y); n     if (0) n       cout <<  a  n            <<     << a <<     n            <<  b  n            <<     << b <<     n            <<  x  n            <<     << x <<     n            <<  y  n            <<     << y <<     << endl; n     x = 4 * x; n     y = 4 * y; n     int s = ((int)((t).size())); n     assert(x >= 0 && x < s && y >= 0 && y < s); n     cout << tree.get(x, y) << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n string get(int n, int m) { n   string str; n   for (int i = 1; i <= n; i++) str.push_back( 4 ); n   for (int i = 1; i <= m; i++) str.push_back( 7 ); n   return str; n } n int32_t main() { n   ios_base ::sync_with_stdio(0); n   cin.tie(0); n   string str; n   cin >> str; n   int n = str.size(); n   if (n & 1) { n     cout << get((n + 1) / 2, (n + 1) / 2) << endl; n     return 0; n   } n   int cnta = 0, cntb = 0, ans = -1, aa = 0, bb = 0; n   for (int i = 0; i < n; i++) { n     if (cnta + 1 <= n / 2 && str[i] <  7 ) { n       ans = i; n       aa = cnta; n       bb = cntb; n     } n     if (cntb + 1 <= n / 2 && str[i] <  4 ) { n       ans = i; n       aa = cnta; n       bb = cntb; n     } n     cnta += str[i] ==  7 ; n     cntb += str[i] ==  4 ; n     if ((str[i] !=  4  && str[i] !=  7 ) || cnta > n / 2 || cntb > n / 2) break; n   } n   if (cnta + cntb == n && cnta == cntb) { n     cout << str << endl; n     return 0; n   } n   if (ans == -1) { n     cout << get((n + 2) / 2, (n + 2) / 2) << endl; n     return 0; n   } n   string x; n   for (int i = 0; i < ans; i++) { n     x.push_back(str[i]); n   } n   if (bb + 1 <= n / 2 && str[ans] <  4 ) { n     ++bb; n     x.push_back( 4 ); n   } else { n     ++aa; n     x.push_back( 7 ); n   } n   x += get(n / 2 - bb, n / 2 - aa); n   cout << x << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int n; n vector<int> a; n vector<stack<int>> ids; n vector<int> b; n string answer; n int segtree_size; n template <typename T> n struct segtree { n   int segtree_size; n   vector<T> segtree; n   T identity; n   T (*UpdateFn)(T, T); n   void InitSegtree(int main_size, T (*UpdateFunction)(T, T), T fun_identity) { n     segtree_size = 1; n     while (segtree_size < main_size) segtree_size *= 2; n     segtree_size *= 2; n     segtree.resize(segtree_size); n     UpdateFn = UpdateFunction; n     this->identity = fun_identity; n   } n   void UpdateAllParents() { n     for (int i = segtree_size / 2 - 1; i > 0; i--) n       segtree[i] = UpdateFn(segtree[i * 2], segtree[i * 2 + 1]); n   } n   void ChangeValue(int index, T value) { n     segtree[index + segtree_size / 2] = value; n   } n   T GetValue(int index) { return segtree[index + segtree_size / 2]; } n   void Update(int pos, T value) { n     for (segtree[pos += segtree_size / 2] = value; pos > 1; pos /= 2) n       segtree[pos / 2] = UpdateFn(segtree[pos], segtree[pos ^ 1]); n   } n   T Query(int start, int finish) { n     T ans = this->identity; n     for (start += segtree_size / 2, finish += segtree_size / 2; start < finish; n          start /= 2, finish /= 2) { n       if (start % 2 == 1) ans = UpdateFn(ans, segtree[start++]); n       if (finish % 2 == 0) ans = UpdateFn(ans, segtree[finish--]); n     } n     return UpdateFn(ans, segtree[start]); n   } n   void Destroy() { segtree.clear(); } n }; n struct segtree<int> tree; n void In() { n   cin >> n; n   a.resize(n); n   ids.resize(n + 1); n   b.resize(n); n   for (int i = 0; i < n; i++) cin >> a[i]; n   for (int i = 0; i < n; i++) cin >> b[i]; n } n void Solve() { n   tree.InitSegtree( n       n + 1, [](int a, int b) -> int { return max(a, b); }, INT_MIN); n   for (int i = 0; i < n; i++) tree.ChangeValue(i, a[i]); n   tree.UpdateAllParents(); n   answer =  NO ; n   for (int i = 0; i < n; i++) ids[a[i]].push(i); n   for (int i = n - 1; i >= 0; i--) { n     if (ids[b[i]].empty()) return; n     int a_idx = ids[b[i]].top(); n     ids[b[i]].pop(); n     if (tree.Query(a_idx + 1, n) > b[i]) return; n     tree.Update(a_idx, 0); n   } n   answer =  YES ; n } n void Out() { n   cout << answer << endl; n   tree.Destroy(); n   a.clear(); n   ids.clear(); n   b.clear(); n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   int t; n   cin >> t; n   while (t--) { n     In(); n     Solve(); n     Out(); n   } n   return 0; n } n


#include <bits/stdc++.h> n int main() { n   int n, k, i; n   scanf( %d%d , &n, &k); n   while (n > k) { n     for (i = 1; i <= k; i++) { n       if (n % 10 > 0) n         n--; n       else n         n = n / 10; n     } n     printf( %d , n); n     n = 0; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int n, l[200000 + 5], r[200000 + 5]; n int p[200000 + 5], q[200000 + 5], sum; n long long add(long long a, long long b) { return (a + b) % 1000000007; } n long long mul(long long a, long long b) { return 1LL * a * b % 1000000007; } n long long dec(long long a, long long b) { n   return (a - b + 1000000007) % 1000000007; n } n int fst_pow(int a, int b) { n   int res = 1; n   while (b) { n     if (b & 1) res = mul(res, a); n     a = mul(a, a); n     b >>= 1; n   } n   return res; n } n int val(int i) { n   int ans = 1; n   ans = dec(ans, p[i]), ans = dec(ans, p[i + 1]); n   int L = max(l[i - 1], max(l[i], l[i + 1])), n       R = min(r[i - 1], min(r[i], r[i + 1])); n   if (L < R) n     ans = add(ans, mul(R - L, fst_pow(mul(mul(r[i] - l[i], r[i - 1] - l[i - 1]), n                                           r[i + 1] - l[i + 1]), n                                       1000000007 - 2))); n   return ans; n } n int main() { n   scanf( %d , &n); n   for (int i = 1; i <= n; i++) scanf( %d , &l[i]); n   for (int i = 1; i <= n; i++) { n     scanf( %d , &r[i]); n     r[i]++; n   } n   int sum = 0; n   for (int i = 1; i <= n; i++) { n     int L = max(l[i], l[i - 1]), R = min(r[i], r[i - 1]); n     if (L < R) n       p[i] = mul(R - L, fst_pow(mul(r[i] - l[i], r[i - 1] - l[i - 1]), n                                 1000000007 - 2)); n     q[i] = dec(1, p[i]); n     sum = add(sum, q[i]); n   } n   int ans = 0; n   for (int i = 1; i <= n; i++) { n     ans = add(ans, q[i]); n     int ps = dec(sum, q[i]); n     if (i > 2) ans = add(ans, val(i - 1)), ps = dec(ps, q[i - 1]); n     if (i < n) ans = add(ans, val(i)), ps = dec(ps, q[i + 1]); n     ans = add(ans, mul(q[i], ps)); n   } n   printf( %d , ans); n } n


#include <bits/stdc++.h> n #pragma comment(linker,  /STACK:100000000 ) n using namespace std; n int matrix[22][22]; n int mas[22]; n int matrix1[22][22]; n int matrix2[22][22]; n int best = 0; n int minv[22]; n bool used[22]; n int u[22]; n int v[22]; n int p[22]; n int way[22]; n int getresult(int n) { n   int res = 0; n   for (int i = 0; i <= n; i++) u[i] = 0; n   for (int i = 0; i <= n; i++) v[i] = 0; n   for (int i = 0; i <= n; i++) p[i] = 0; n   for (int i = 0; i <= n; i++) way[i] = 0; n   int m = n; n   const int INF = 1e9; n   for (int i = 1; i <= n; ++i) { n     p[0] = i; n     int j0 = 0; n     for (int x = 0; x <= m; x++) minv[x] = INF; n     for (int x = 0; x <= m; x++) used[x] = false; n     do { n       used[j0] = true; n       int i0 = p[j0], delta = INF, j1; n       for (int j = 1; j <= m; ++j) n         if (!used[j]) { n           int cur = matrix[i0][j] - u[i0] - v[j]; n           if (cur < minv[j]) minv[j] = cur, way[j] = j0; n           if (minv[j] < delta) delta = minv[j], j1 = j; n         } n       for (int j = 0; j <= m; ++j) n         if (used[j]) n           u[p[j]] += delta, v[j] -= delta; n         else n           minv[j] -= delta; n       j0 = j1; n     } while (p[j0] != 0); n     do { n       int j1 = way[j0]; n       p[j0] = p[j1]; n       j0 = j1; n     } while (j0); n   } n   return -v[0]; n } n int line1max[22]; n int line2max[22]; n int colmax[22]; n int rr[22]; n bool stop = false; n int itt = 0; n int go(int pos, int N, int N2, int cnt1 = 0, int cnt2 = 0) { n   if (stop) return 0; n   if (pos == N) { n     int res = 0; n     int maxres = 0; n     int maxres2 = 0; n     for (int i = 0; i < N; i++) colmax[i] = 0; n     for (int i = 0; i < N; i++) { n       if (mas[i] == 1) { n         for (int j = 0; j < N; j++) n           matrix[i + 1][j + 1] = -matrix1[i][j], n                             colmax[j] = max(colmax[j], matrix1[i][j]); n         maxres += line1max[i]; n       } else { n         for (int j = 0; j < N; j++) n           matrix[i + 1][j + 1] = -matrix2[i][j], n                             colmax[j] = max(colmax[j], matrix2[i][j]); n         maxres += line2max[i]; n       } n     } n     for (int i = 0; i < N; i++) maxres2 += colmax[i]; n     if (maxres > best && maxres2 > best) { n       res = -getresult(N); n       best = max(best, res); n     } n     return res; n   } n   int res = 0; n   if (rr[pos]) { n     if (cnt1 < N2) { n       mas[pos] = 1; n       res = max(res, go(pos + 1, N, N2, cnt1 + 1, cnt2)); n     } n     if (cnt2 < N2) { n       mas[pos] = 2; n       res = max(res, go(pos + 1, N, N2, cnt1, cnt2 + 1)); n     } n   } else { n     if (cnt2 < N2) { n       mas[pos] = 2; n       res = max(res, go(pos + 1, N, N2, cnt1, cnt2 + 1)); n     } n     if (cnt1 < N2) { n       mas[pos] = 1; n       res = max(res, go(pos + 1, N, N2, cnt1 + 1, cnt2)); n     } n   } n   itt++; n   if (itt & 63) { n     if (clock() * 1e-3 > 0.8) { n       stop = true; n       return res; n     } n   } n   return res; n } n int rowperm[22]; n int colperm[22]; n bool visrow[22]; n bool viscol[22]; n int main() { n   int N; n   scanf( %d , &N); n   for (int i = 0; i < N; i++) rowperm[i] = i; n   for (int i = 0; i < N; i++) colperm[i] = i; n   srand(time(0)); n   random_shuffle(rowperm, rowperm + N); n   random_shuffle(colperm, colperm + N); n   for (int i = 0; i < N; i++) rr[i] = rand() % 2; n   for (int i = 0; i < N; i++) n     for (int j = 0; j < N; j++) scanf( %d , &matrix1[rowperm[i]][colperm[j]]); n   for (int i = 0; i < N; i++) { n     for (int j = 0; j < N; j++) { n       line1max[i] = max(line1max[i], matrix1[i][j]); n     } n   } n   for (int i = 0; i < N; i++) n     for (int j = 0; j < N; j++) scanf( %d , &matrix2[rowperm[i]][colperm[j]]); n   for (int i = 0; i < N; i++) { n     for (int j = 0; j < N; j++) { n       line2max[i] = max(line2max[i], matrix2[i][j]); n     } n   } n   int ost1 = N / 2; n   int ost2 = N / 2; n   memset(visrow, true, sizeof(visrow)); n   memset(viscol, true, sizeof(viscol)); n   for (int step = 0; step < N; step++) { n     int maxi1 = 0; n     int r1 = 0, c1 = 0; n     int maxi2 = 0; n     int r2 = 0, c2 = 0; n     for (int i = 0; i < N; i++) { n       for (int j = 0; j < N; j++) { n         if (matrix1[i][j] > maxi1 && visrow[i] && viscol[j]) { n           maxi1 = matrix1[i][j]; n           r1 = i; n           c1 = j; n         } n         if (matrix2[i][j] > maxi2 && visrow[i] && viscol[j]) { n           maxi2 = matrix2[i][j]; n           r2 = i; n           c2 = j; n         } n       } n     } n     if (ost1 > 0 && maxi1 > maxi2) { n       ost1--; n       best += maxi1; n       visrow[r1] = false; n       viscol[c1] = false; n     } else if (ost2 > 0 && maxi2 > maxi1) { n       ost2--; n       best += maxi2; n       visrow[r2] = false; n       viscol[c2] = false; n     } else if (maxi1 == maxi2) { n       if (ost1 > ost2) { n         ost1--; n         best += maxi1; n         visrow[r1] = false; n         viscol[c1] = false; n       } else { n         ost2--; n         best += maxi2; n         visrow[r2] = false; n         viscol[c2] = false; n       } n     } else if (ost1 > 0) { n       ost1--; n       best += maxi1; n       visrow[r1] = false; n       viscol[c1] = false; n     } else { n       ost2--; n       best += maxi2; n       visrow[r2] = false; n       viscol[c2] = false; n     } n   } n   printf( %d n , max(best, go(0, N, N / 2))); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int solve(string s, int x, int y) { n   int t = 0; n   for (auto i : s) { n     if (i -  0  == x) { n       t++; n       swap(x, y); n     } n   } n   if (x != y && t % 2 == 1) --t; n   return t; n } n int main() { n   cin.tie(0); n   cout.tie(0); n   ios_base::sync_with_stdio(0); n   int t; n   cin >> t; n   while (t--) { n     string s; n     cin >> s; n     int ans = 0; n     for (int i = 0; i <= 9; i++) { n       for (int j = 0; j <= 9; j++) { n         ans = max(ans, solve(s, i, j)); n       } n     } n     cout << (int)(s.size()) - ans << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n char a[2010], b[2010], c[2010]; n int aa[256], bb[256]; n int n; n void _reverse(char *a, int l) { n   for (int i = 0; i < l; i++) c[i] = a[i]; n   for (int i = 0; i < l; i++) a[i] = c[l - 1 - i]; n } n void _rotate(char *a, int p) { n   for (int i = 0; i < p; i++) c[n - p + i] = a[i]; n   for (int i = 0; i < n - p; i++) c[i] = a[p + i]; n   for (int i = 0; i < n; i++) a[i] = c[i]; n } n int main() { n   scanf( %d%s%s , &n, a, b); n   for (int i = 0; i < n; i++) { n     aa[a[i]]++; n     bb[b[i]]++; n   } n   for (int i = 0; i < 256; i++) n     if (aa[i] != bb[i]) { n       printf( -1 n ); n       return 0; n     } n   printf( %d n , 3 * n); n   for (int i = 0; i < n; i++) { n     int j; n     _reverse(a, n); n     for (j = 0; j < n; j++) n       if (a[j] == b[n - 1 - i]) break; n     printf( %d %d %d  , n, n - j - 1, 1); n     _reverse(a + j + 1, n - j - 1); n     _rotate(a, j + 1); n     _rotate(a, n - 1); n   } n } n


#include <bits/stdc++.h> n void reduce(unsigned& nom, unsigned& denom) { n   bool reduced = true; n   while (reduced) { n     reduced = false; n     for (unsigned i = 2; !reduced && i < 1000; ++i) { n       if (nom % i == 0 && denom % i == 0) { n         nom /= i; n         denom /= i; n         reduced = true; n       } n     } n   } n } n void solve() { n   unsigned sum = 0; n   unsigned A; n   std::cin >> A; n   for (unsigned i = 2; i < A; ++i) { n     auto val = A; n     while (val > 0) { n       sum += val % i; n       val /= i; n     } n   } n   auto nom = sum; n   auto denom = A - 2; n   reduce(nom, denom); n   std::cout << nom <<  /  << denom << std::endl; n } n int main(int argc, char* argv[]) { n   { solve(); } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int max_n = 1500; n const int max_m = 1500; n const int inv = numeric_limits<int>::max(); n int n, m; n char G[max_n][max_m]; n int si, sj; n int di[max_n][max_m][4]; n int dj[max_n][max_m][4]; n int asz[max_n][max_m] = {0}; n int vsti[max_n][max_m]; n int vstj[max_n][max_m]; n stack<int> sti, stj; n int ct[max_n][max_m] = {0}; n bool res; n int amod(int p, int d) { n   if (p % d < 0) n     return p % d + d; n   else n     return p % d; n } n void DFS() { n   int vi, vj; n   sti.push(si); n   stj.push(sj); n   while (!sti.empty()) { n     vi = sti.top(); n     vj = stj.top(); n     int rvi = amod(vi, n), rvj = amod(vj, m); n     vsti[rvi][rvj] = vi; n     vstj[rvi][rvj] = vj; n     int i; n     int ni, nj, rni, rnj; n     if (ct[rvi][rvj] == asz[rvi][rvj]) { n       sti.pop(); n       stj.pop(); n     } else { n       i = ct[rvi][rvj]; n       ++ct[rvi][rvj]; n       ni = vi + di[rvi][rvj][i]; n       nj = vj + dj[rvi][rvj][i]; n       rni = amod(ni, n); n       rnj = amod(nj, m); n       if (vsti[rni][rnj] == inv) { n         sti.push(ni); n         stj.push(nj); n       } else if (vsti[rni][rnj] != ni or vstj[rni][rnj] != nj) { n         res = true; n         return; n       } n     } n   } n   return; n } n int main() { n   int ldi[4] = {-1, 1, 0, 0}; n   int ldj[4] = {0, 0, -1, 1}; n   scanf( %d %d , &n, &m); n   char trow[max_m + 10]; n   for (int i = 0; i < n; ++i) { n     scanf( %s , trow); n     for (int j = 0; j < m; ++j) { n       G[i][j] = trow[j]; n       if (G[i][j] ==  S ) { n         G[i][j] =  . ; n         si = i; n         sj = j; n       } n     } n   } n   for (int i = 0; i < n; ++i) { n     for (int j = 0; j < m; ++j) { n       if (G[i][j] ==  . ) { n         for (int k = 0; k < 4; ++k) { n           int xi = amod(i + ldi[k], n), xj = amod(j + ldj[k], m); n           if (G[xi][xj] ==  . ) { n             di[i][j][asz[i][j]] = ldi[k]; n             dj[i][j][asz[i][j]] = ldj[k]; n             ++asz[i][j]; n           } n         } n       } n     } n   } n   for (int i = 0; i < n; ++i) { n     for (int j = 0; j < m; ++j) { n       vsti[i][j] = inv; n       vstj[i][j] = inv; n     } n   } n   res = false; n   DFS(); n   if (res) n     printf( Yes n ); n   else n     printf( No n ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void add_self(int &a, int b) { n   a += b; n   if (a >= (int)1e9 + 7) a -= (int)1e9 + 7; n } n int n, l; n void f(vector<int> &a) { n   int x = a[0]; n   for (int i = 0; i < n - 1; i++) { n     a[i] = a[i + 1] - a[i]; n   } n   a[n - 1] = l - a[n - 1] + x; n } n void f1(vector<int> &a, int k) { n   reverse(begin(a), end(a)); n   reverse(begin(a) + k, end(a)); n   reverse(begin(a), begin(a) + k); n } n void solve() { n   cin >> n >> l; n   vector<int> a(n), b(n); n   for (int i = 0; i < n; i++) cin >> a[i]; n   for (int i = 0; i < n; i++) cin >> b[i]; n   f(a); n   f(b); n   for (int i = 0; i < n; i++) { n     f1(a, 1); n     bool ok = true; n     for (int j = 0; j < n; j++) { n       if (a[j] != b[j]) { n         ok = false; n         break; n       } n     } n     if (ok) { n       cout <<  YES ; n       return; n     } n   } n   cout <<  NO ; n } n int main() { n   ios::sync_with_stdio(false); n   int q = 1; n   for (int i = 1; i <= q; i++) { n     solve(); n   } n } n


#include <bits/stdc++.h> n using namespace std; n long long int minimum(long long int a[], long long int m, long long int n) { n   long long int ar[m]; n   for (long long int i = 0; i < m; i++) ar[i] = a[i]; n   long long int j = 0; n   long long int ans = 0; n   while (n > 0) { n     if (ar[j] > 0) { n       ans += ar[j]; n       ar[j] -= 1; n       n--; n     } else n       j++; n   } n   return ans; n } n long long int maximum(long long int a[], long long int m, long long int n) { n   long long int ar[m]; n   for (long long int i = 0; i < m; i++) ar[i] = a[i]; n   long long int ans = 0; n   long long int j = m - 1; n   while (n > 0) { n     ans += ar[j]; n     ar[j] -= 1; n     n--; n     long long int maxi = ar[j]; n     long long int id = j; n     for (long long int k = m - 1; k >= 0; k--) { n       if (maxi < ar[k]) { n         id = k; n         maxi = ar[k]; n       } n     } n     swap(ar[id], ar[j]); n   } n   return ans; n } n void solve() { n   long long int n, m; n   cin >> n >> m; n   long long int ar[m]; n   for (auto &it : ar) cin >> it; n   ; n   sort(ar, ar + m); n   long long int mini = minimum(ar, m, n); n   long long int maxi = maximum(ar, m, n); n   cout << maxi <<     << mini <<   n ; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   solve(); n } n


#include <bits/stdc++.h> n using namespace std; n const int N = (1 << 20) + 10; n vector<pair<int, int>> g[N]; n int tamp = 0; n bool faz; n namespace hpath_space { n vector<int> path; n vector<int> idit, used_edge; n void dfs(int no) { n   while (1) { n     int &id = idit[no]; n     while (id < (int)(g[no].size()) and used_edge[g[no][id].second]) id++; n     if (id == (int)(g[no].size())) break; n     used_edge[g[no][id].second] = 1; n     int it = g[no][id++].first; n     dfs(it); n   } n   if (faz) path.push_back(no); n   tamp++; n } n bool has_cycle(int n, int m) { n   int nimp = 0; n   for (int i = 0; i < n; i++) n     if ((int)(g[i].size()) & 1) nimp++; n   tamp = 0; n   if (nimp > 0) return 0; n   int inic = 0; n   for (int i = 0; i < n; i++) n     if ((int)(g[i].size())) inic = i; n   path.clear(); n   idit = vector<int>(n); n   used_edge = vector<int>(m); n   dfs(inic); n   if (tamp == m + 1) return 1; n   return 0; n } n };  // namespace hpath_space n int n; n vector<pair<int, int>> pe; n vector<int> ans; n template <class key_type, class value_type> n struct mymap { n   int n; n   vector<key_type> keys; n   vector<value_type> values; n   mymap(vector<key_type> _keys) { n     keys = _keys; n     sort((keys).begin(), (keys).end()); n     keys.resize(unique((keys).begin(), (keys).end()) - keys.begin()); n     n = (int)(keys.size()); n     values = vector<value_type>(n); n   } n   value_type &operator[](key_type key) { n     int id = lower_bound((keys).begin(), (keys).end(), key) - keys.begin(); n     return values[id]; n   } n }; n bool check(int k, bool sob = 1) { n   faz = !sob; n   int mask = (1 << k) - 1; n   for (int i = 0; i < mask + 1; i++) g[i].clear(); n   for (int i = 0; i < n; i++) { n     int ao, bo; n     tie(ao, bo) = pe[i]; n     int a = ao & mask; n     int b = bo & mask; n     g[a].emplace_back(b, i); n     g[b].emplace_back(a, i); n   } n   if (hpath_space::has_cycle(mask + 1, n)) { n     if (sob) return 1; n     vector<pair<int, int>> vmp, vst; n     for (int i = 0; i < n; i++) { n       int ao, bo; n       tie(ao, bo) = pe[i]; n       int a = ao & mask; n       int b = bo & mask; n       vmp.emplace_back(a, b); n       vmp.emplace_back(b, a); n       vst.emplace_back(2 * i, 2 * i + 1); n       vst.emplace_back(2 * i + 1, 2 * i); n     } n     mymap<pair<int, int>, vector<pair<int, int>>> mp(vmp); n     mymap<pair<int, int>, int> mpid(vmp), st(vst); n     for (int i = 0; i < n; i++) { n       int ao, bo; n       tie(ao, bo) = pe[i]; n       int a = ao & mask; n       int b = bo & mask; n       mp[{a, b}].push_back({2 * i, 2 * i + 1}); n       mp[{b, a}].push_back({2 * i + 1, 2 * i}); n     } n     ans.clear(); n     vector<int> &path = hpath_space::path; n     for (int i = 0; i < (int)(path.size()) - 1; i++) { n       int va = path[i], vb = path[i + 1]; n       int &imp = mpid[{va, vb}]; n       int ia, ja; n       auto &v = mp[{va, vb}]; n       while (1) { n         tie(ia, ja) = v[imp]; n         if (st[{ia, ja}]) { n           imp++; n           continue; n         } n         st[{ia, ja}] = 1; n         st[{ja, ia}] = 1; n         break; n       } n       imp++; n       ans.push_back(ia); n       ans.push_back(ja); n       assert(abs(ia - ja) == 1); n     } n     return 1; n   } n   return 0; n } n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cin >> n; n   for (int i = 0; i < n; i++) { n     int a, b; n     cin >> a >> b; n     pe.emplace_back(a, b); n   } n   int lo = 0, hi = 20; n   while (lo < hi) { n     int k = (lo + hi + 1) / 2; n     if (hi - lo + 1 >= 8) k = lo + (hi - lo) / 3; n     if (check(k)) n       lo = k; n     else n       hi = k - 1; n   } n   if (lo) { n     int k = lo; n     assert(check(k, 0)); n     assert((int)(ans.size()) == 2 * n); n     cout << k <<   n ; n     for (int i = 0; i < 2 * n; i++) { n       if (i) cout <<    ; n       cout << ans[i] + 1; n     } n     cout <<   n ; n     return 0; n   } n   cout << 0 <<   n ; n   for (int i = 0; i < 2 * n; i++) { n     if (i) cout <<    ; n     cout << i + 1; n   } n   cout <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n unsigned long long int w, v, qt = 0, n; n inline unsigned long long int f(unsigned long long int x) { n   return w * (1 - x) * (1 - x) + v; n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cin >> n >> v >> w; n   char c; n   while (cin >> c) qt += c ==  1 ; n   unsigned long long int ans = (~0ULL); n   for (int i = 1; i <= qt; i++) { n     unsigned long long int tmp = 0; n     tmp += (i - (n % i)) * f(n / i); n     tmp += (n % i) * f(n / i + 1); n     ans = min(ans, tmp); n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int number_of_players; n   cin >> number_of_players; n   int old_bid, counter; n   for (counter = 0; counter < number_of_players; counter++) { n     int bid; n     cin >> bid; n     while (bid % 2 == 0) bid /= 2; n     while (bid % 3 == 0) bid /= 3; n     if (counter == 0) n       old_bid = bid; n     else if (old_bid != bid) { n       cout <<  No  << endl; n       break; n     } n   } n   if (counter == number_of_players) cout <<  Yes  << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long mod = (long long)1e9 + 7; n long long n, m, k; n const long long moll = 998244353; n long long fact(long long n) { n   if (n == 0) return 1; n   return (n * fact(n - 1)) % moll; n } n long long binpow(long long a, long long b) { n   if (b == 0) return 1; n   if (b % 2 == 0) return (binpow((a * a) % moll, b / 2)) % moll; n   return (a * (binpow((a * a) % moll, b / 2))) % moll; n } n long long C(long long n, long long k) { n   long long x = (fact(n) * binpow(fact(k), moll - 2)) % moll; n   x *= binpow(fact(n - k), moll - 2); n   x %= moll; n   if (x < 0) x += moll; n   return x; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cin >> n >> m >> k; n   long long x = m; n   x *= C(n - 1, k); n   x %= moll; n   x *= binpow(m - 1, k); n   x %= moll; n   if (x < 0) x += moll; n   cout << x << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long n, m, i, j, a[105], b[105]; n int main() { n   cin >> n >> m; n   for (i = 0; i < n; i++) cin >> a[i]; n   for (i = 0; i < m; i++) cin >> b[i]; n   for (i = 0; i < n; i++) { n     for (j = 0; j < m; j++) { n       if (a[i] == b[j]) { n         cout << a[i] <<    ; n         b[j] = -1; n       } n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   int n; n   cin >> n; n   vector<int> tree[n + 1]; n   for (int i = 2; i <= n; i++) { n     int p; n     cin >> p; n     tree[p].push_back(i); n   } n   bool is_spruce = true; n   for (int i = 1; i <= n; i++) { n     if (tree[i].empty()) n       continue; n     else { n       int count = 0; n       for (auto c : tree[i]) { n         if (count == 3) break; n         if (tree[c].size() == 0) count++; n       } n       if (count != 3) { n         is_spruce = false; n         break; n       } n     } n   } n   if (is_spruce) n     cout <<  Yes  << endl; n   else n     cout <<  No  << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   long long int s, sum = 0; n   cin >> n >> s; n   long long int* v = new long long int[n]; n   for (int i = 0; i < n; i++) { n     cin >> v[i]; n     sum += v[i]; n   } n   if (s > sum) n     cout << -1 << endl; n   else { n     sort(v, v + n); n     if (s < (sum - v[0] * n)) n       cout << v[0] << endl; n     else { n       s -= sum - v[0] * n; n       if (s % n == 0) n         cout << v[0] - s / n << endl; n       else n         cout << v[0] - (s / n) - 1 << endl; n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n int N, M; n signed long long L[100020], R[100020], T[100020]; n signed long long Q[100020]; n vector<pair<signed long long, signed long long> > QP; n signed long long PP[200200]; n signed long long AS[200200], BS[200200], ret[200200]; n vector<signed long long> S[200200], E[200200]; n void solve() { n   int i, j, k, l, r, x, y; n   string s; n   cin >> N >> M; n   for (i = 0; i < M; i++) cin >> L[i] >> R[i] >> T[i]; n   for (i = 0; i < N; i++) cin >> Q[i], QP.push_back(make_pair(Q[i], i)); n   sort(QP.begin(), QP.end()); n   map<signed long long, signed long long> UM; n   UM[0] = 0; n   for (i = 0; i < M; i++) UM[L[i]] = UM[R[i]] = 1; n   i = 0; n   for (__typeof(UM.begin()) it = UM.begin(); it != UM.end(); it++) n     it->second = i++, PP[it->second] = it->first; n   for (i = 0; i < M; i++) n     S[UM[L[i]]].push_back(T[i]), E[UM[R[i]]].push_back(T[i]); n   map<signed long long, int> CS; n   for (i = 0; i < UM.size(); i++) { n     for (j = 0; j < E[i].size(); j++) n       if (--CS[E[i][j]] == 0) CS.erase(E[i][j]); n     for (j = 0; j < S[i].size(); j++) CS[S[i][j]]++; n     if (CS.size()) { n       signed long long ct = CS.begin()->first; n       x = lower_bound(QP.begin(), QP.end(), make_pair(ct - PP[i + 1], 0LL)) - n           QP.begin(); n       y = lower_bound(QP.begin(), QP.end(), make_pair(ct - PP[i], 0LL)) - n           QP.begin(); n       AS[x]++; n       AS[y]--; n       BS[x] -= ct - PP[i + 1], BS[y] += ct - PP[i]; n     } n   } n   signed long long aa = 0, bb = 0; n   for (i = 0; i < N; i++) n     aa += AS[i], bb += BS[i], ret[QP[i].second] = aa * QP[i].first + bb; n   for (i = 0; i < N; i++) cout << ret[i] << endl; n } n int main(int argc, char** argv) { n   string s; n   int i; n   if (argc == 1) ios::sync_with_stdio(false); n   for (i = 0; i < argc - 1; i++) s += argv[i + 1], s +=   n ; n   for (i = 0; i < s.size(); i++) ungetc(s[s.size() - 1 - i], stdin); n   solve(); n   return 0; n } n


#include <bits/stdc++.h> n int main() { n   int q; n   std::cin >> q; n   while (q--) { n     int n; n     std::cin >> n; n     std::vector<int> a(n); n     for (int i = 0; i < n; ++i) { n       std::cin >> a[i]; n     } n     for (int i = 0; i < n; i += 2) { n       std::cout << -a[i + 1] <<     << a[i] <<    ; n     } n     std::cout <<   n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 200; n int mn[maxn << 2], lazy[maxn << 2], gs[maxn << 2]; n void push(int rt) { n   if (lazy[rt] == 1000000000) return; n   lazy[rt << 1] = lazy[rt << 1 | 1] = lazy[rt]; n   mn[rt << 1] = mn[rt << 1 | 1] = lazy[rt]; n   gs[rt << 1] = gs[rt << 1 | 1] = lazy[rt]; n   lazy[rt] = 1000000000; n } n void update(int l, int r, int rt, int ql, int qr, int v) { n   if (ql <= l && qr >= r) n     lazy[rt] = v, mn[rt] = gs[rt] = v; n   else { n     push(rt); n     int mid = l + r >> 1; n     if (ql <= mid) update(l, mid, rt << 1, ql, qr, v); n     if (qr > mid) update(mid + 1, r, rt << 1 | 1, ql, qr, v); n     mn[rt] = min(mn[rt << 1], mn[rt << 1 | 1]); n   } n } n int query(int l, int r, int rt, int ql, int qr) { n   if (ql <= l && qr >= r) return mn[rt]; n   push(rt); n   int mid = l + r >> 1, ret = 1e9; n   if (ql <= mid) ret = query(l, mid, rt << 1, ql, qr); n   if (qr > mid) ret = min(ret, query(mid + 1, r, rt << 1 | 1, ql, qr)); n   return ret; n } n int Pv(int l, int r, int rt, int p) { n   if (l == r) return gs[rt]; n   push(rt); n   int mid = l + r >> 1; n   if (p <= mid) n     return Pv(l, mid, rt << 1, p); n   else n     return Pv(mid + 1, r, rt << 1 | 1, p); n } n int sum[maxn], a[maxn]; n void build(int l, int r, int rt) { n   lazy[rt] = 1000000000; n   if (l == r) n     mn[rt] = gs[rt] = sum[l]; n   else { n     int mid = l + r >> 1; n     build(l, mid, rt << 1); n     build(mid + 1, r, rt << 1 | 1); n     mn[rt] = min(mn[rt << 1], mn[rt << 1 | 1]); n   } n } n int vis[maxn]; n using pii = pair<int, int>; n pii her[maxn]; n vector<int> ans; n int main() { n   int n, m; n   cin >> n >> m; n   for (int i = 1; i <= m; ++i) cin >> her[i].first >> her[i].second; n   for (int i = 1; i <= n; ++i) cin >> a[i]; n   int pos = -1; n   for (int i = 1; i <= n; ++i) { n     sum[0] = 0; n     sum[n + 1] = 0; n     int tp = a[i]; n     for (int i = 1; i <= n; ++i) vis[i] = 0; n     for (int j = 1; j < i; ++j) sum[j] = sum[j - 1] + a[j]; n     for (int j = n; j > i; --j) sum[j] = sum[j + 1] + a[j]; n     sum[i] = 0; n     build(1, n, 1); n     while (true) { n       int cnt = 0; n       for (int j = 1; j <= m; ++j) { n         if (vis[j]) continue; n         auto h = her[j]; n         int S = 0, ps = Pv(1, n, 1, h.first); n         if (h.first <= i - 1) S = query(1, n, 1, h.first, i - 1) - ps; n         if (h.first >= i + 1) S = query(1, n, 1, i + 1, h.first) - ps; n         if (h.second + S >= 0) { n           if (h.first <= i - 1) S = Pv(1, n, 1, i - 1) - ps; n           if (h.first >= i + 1) S = Pv(1, n, 1, i + 1) - ps; n           if (h.second + S + a[i] >= 0) { n             a[i] = 0; n             vis[j] = 1; n             cnt++; n             ans.push_back(j); n             if (h.first <= i - 1) update(1, n, 1, h.first, i - 1, ps); n             if (h.first >= i + 1) update(1, n, 1, i + 1, h.first, ps); n           } n         } n       } n       if (!cnt) break; n     } n     if (ans.size() == m) { n       pos = i; n       break; n     } n     a[i] = tp; n     ans.clear(); n   } n   cout << pos << endl; n   for (auto &u : ans) cout << u <<    ; n   puts(  ); n   return 0; n } n


#include <bits/stdc++.h> n const double PI = 3.1415926535897932384626433; n const int KL = 1e5 + 10; n const long long MOD = 1e9 + 7; n using namespace std; n struct point { n   long double x, y; n   void go(long long x1, long long y1) { n     x = x1; n     y = y1; n   } n   void read() { cin >> x >> y; } n   point operator-(point b) { return point{x - b.x, y - b.y}; } n   point operator+(point b) { return point{x + b.x, y + b.y}; } n }; n long double fnd_angle(point p) { n   long double len = sqrt(p.x * p.x + p.y * p.y); n   long double angle = acos(fabs(p.x) / len); n   if (p.y < 0) { n     if (p.x >= 0) n       angle = 2 * PI - angle; n     else n       angle += PI; n   } else if (p.x < 0) n     angle = PI - angle; n   return angle; n } n int n, k; n vector<point> A; n vector<pair<long double, long double>> a, b; n multiset<long double> s; n bool ok(long double mid) { n   if (k <= 0) return 1; n   a.clear(); n   b.clear(); n   for (int i = 0; i < n; i++) { n     long double dis = sqrt(A[i].x * A[i].x + A[i].y * A[i].y); n     if (dis > 2 * mid) continue; n     long double angle = fnd_angle(A[i]); n     long double angle2 = acos(dis / (2.0 * mid)); n     a.push_back({angle - angle2, 2 * angle2}); n     b.push_back({angle + angle2, -2.0 * angle2}); n   } n   sort(a.begin(), a.end()); n   sort(b.begin(), b.end()); n   reverse(b.begin(), b.end()); n   int sz = a.size(); n   for (int i = 0; i < sz; i++) a.push_back({a[i].first + 2 * PI, a[i].second}); n   for (int i = 0; i < sz; i++) b.push_back({b[i].first - 2 * PI, b[i].second}); n   int mx = 0; n   s.clear(); n   for (int i = 0; i < a.size(); i++) { n     while (s.size() > 0 && *s.begin() < a[i].first) s.erase(s.begin()); n     if (s.size() + 1 >= k) return 1; n     s.insert(a[i].first + a[i].second); n   } n   s.clear(); n   for (int i = 0; i < b.size(); i++) { n     while (s.size() > 0 && *(--s.end()) > b[i].first) s.erase(--s.end()); n     if (s.size() + 1 >= k) return 1; n     s.insert(b[i].first + b[i].second); n   } n   return 0; n } n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   cin >> n >> k; n   for (int i = 1; i <= n; i++) { n     point pp; n     pp.read(); n     if (pp.x == 0 && pp.y == 0) { n       k--; n       continue; n     } n     A.push_back(pp); n   } n   n = A.size(); n   long double lo = 0.0, hi = 2e5; n   int cnt = 33; n   while (cnt--) { n     long double mid = (lo + hi) / 2.0; n     if (ok(mid)) n       hi = mid; n     else n       lo = mid; n   } n   cout << fixed << setprecision(8) << lo <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long int MOD = 998244353; n long long int power(long long int x, unsigned long long int y) { n   long long res = 1, m = y + 2; n   x %= m; n   while (y > 0) { n     if (y & 1) res = res * x % m; n     x = x * x % m; n     y >>= 1; n   } n   return res; n } n long long int modInverse(long long int a, long long int m = MOD) { n   return power(a, m - 2); n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   long long int t = 1; n   while (t--) { n     long long int n, i, j; n     cin >> n; n     long long int a[n]; n     for (i = 0; i < n; i++) cin >> a[i]; n     long long int p[n + 1]; n     p[0] = 1; n     for (i = 1; i < n + 1; i++) { n       p[i] = 100 * p[i - 1] % MOD; n     } n     long long int dp[n], x = 1; n     for (i = 0; i < n; i++) { n       dp[i] = x * (100 - a[i]) % MOD; n       x *= a[i]; n       x %= MOD; n       dp[i] *= p[n - i - 1]; n       dp[i] %= MOD; n     } n     long long int dp1[n]; n     for (i = 0; i < n; i++) dp1[i] = dp[i] * (i + 1) % MOD; n     long long int s1 = n; n     for (i = 0; i < n; i++) s1 *= a[i], s1 %= MOD; n     long long int s2 = 0; n     for (i = 0; i < n; i++) s2 += dp1[i], s2 %= MOD; n     long long int s3 = 0; n     for (i = 0; i < n; i++) s3 += dp[i], s3 %= MOD; n     s3 = p[n] - s3; n     while (s3 < 0) s3 += MOD; n     s2 += s1; n     s2 %= MOD; n     cout << s2 * modInverse(s3) % MOD; n   } n   cerr <<  Time Taken:  << (float)clock() / CLOCKS_PER_SEC * 1000 <<  ms  n        <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, m; n   cin >> n >> m; n   vector<int> prime(n + 1, 0); n   vector<int> div(n + 1, 0); n   prime[1] = 1; n   for (long long i = 2; i <= n; i++) { n     if (!prime[i]) { n       for (long long j = i; j <= n; j += i) { n         prime[j] = i; n       } n     } n   } n   for (int i = 0; i < m; i++) { n     int flag; n     int x; n     char c; n     cin >> c >> x; n     if (c ==  + ) { n       if (div[prime[x]] == x) { n         cout <<  Already on  << endl; n       } else { n         flag = 0; n         for (long long i = x; i > 1; i /= prime[i]) { n           if (div[prime[i]]) { n             flag = div[prime[i]]; n           } n         } n         if (flag) { n           cout <<  Conflict with   << flag << endl; n         } else { n           for (long long i = x; i > 1; i /= prime[i]) { n             div[prime[i]] = x; n           } n           if (x == 1) { n             div[1] = 1; n           } n           cout <<  Success  << endl; n         } n       } n     } else { n       if (div[prime[x]] != x) { n         cout <<  Already off  << endl; n       } else { n         for (long long i = x; i > 1; i /= prime[i]) { n           div[prime[i]] = 0; n         } n         if (x == 1) { n           div[1] = 0; n         } n         cout <<  Success  << endl; n       } n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int query(const vector<int> &S, int v, const vector<int> &T) { n   if (S.empty() || T.empty()) return 0; n   cout << S.size() <<   n ; n   for (int i = 0; i < (int)S.size(); i++) n     cout << S[i] + 1 << (i < (int)S.size() - 1 ?     :   n ); n   cout << T.size() <<   n ; n   for (int i = 0; i < (int)T.size(); i++) n     cout << T[i] + 1 << (i < (int)T.size() - 1 ?     :   n ); n   cout << v + 1 <<   n ; n   cout << flush; n   int answer; n   cin >> answer; n   return answer; n } n int query(int s, int v, const vector<int> &T) { n   return query(vector<int>(1, s), v, T); n } n int N, root; n vector<int> parent; n vector<int> subtree_size; n int main() { n   ios::sync_with_stdio(false); n   cin >> N; n   root = 0; n   parent.assign(N, -1); n   subtree_size.assign(N, -1); n   vector<int> non_root; n   for (int i = 0; i < N; i++) n     if (i != root) non_root.push_back(i); n   for (int i = 0; i < N; i++) n     if (i != root) subtree_size[i] = query(root, i, non_root); n   subtree_size[root] = N; n   vector<int> sorted; n   for (int i = 0; i < N; i++) sorted.push_back(i); n   sort(sorted.begin(), sorted.end(), n        [&](int a, int b) { return subtree_size[a] < subtree_size[b]; }); n   for (int i = 0; i < N; i++) { n     int v = sorted[i]; n     vector<int> missing_parent; n     for (int j = 0; j < i; j++) n       if (parent[sorted[j]] < 0) missing_parent.push_back(sorted[j]); n     int remaining = query(root, v, missing_parent); n     int high = (int)missing_parent.size(); n     while (remaining > 0) { n       int low = 0; n       while (low < high) { n         int mid = (low + high + 1) / 2; n         if (query(root, v, n                   vector<int>(missing_parent.begin(), n                               missing_parent.begin() + mid)) == remaining) n           high = mid - 1; n         else n           low = mid; n       } n       int child = missing_parent[low]; n       parent[child] = v; n       remaining--; n     } n   } n   cout <<  ANSWER  <<   n ; n   for (int i = 0; i < N; i++) n     if (parent[i] >= 0) cout << i + 1 <<     << parent[i] + 1 <<   n ; n   cout << flush; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   if (false) { n     freopen( input.txt ,  r , stdin); n     freopen( output.txt ,  w , stdout); n   } n   int n; n   cin >> n; n   vector<int> v(n + 1, -1); n   int xx = 1048576; n   long long sum = 0; n   for (int i = n; i > 0; i--) { n     if (v[i] >= 0) { n       continue; n     } n     while (xx / 2 > i) { n       xx /= 2; n     } n     int a = i ^ (xx - 1); n     v[i] = a; n     v[a] = i; n     sum += (a + i) * 2; n   } n   if (v[0] < 0) { n     v[0] = 0; n   } n   cout << sum << endl; n   for (int i = 0; i <= n; i++) { n     if (i > 0) { n       cout <<    ; n     } n     cout << v[i]; n   } n   cout << endl; n   if (false) { n     fclose(stdin); n     fclose(stdout); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 150007; n const double eps = 1e-8; n int n; n double l, r, mid, sm[N], mxp[N], mnp[N]; n struct note { n   double p, t, c; n   int id; n } a[N], b[N]; n int cmp(note x, note y) { return x.c > y.c; } n int cmp0(note x, note y) { return x.p < y.p; } n int check() { n   double mxv = 0; n   for (int i = 1, j, lb, rb; i <= n; i = j) { n     j = i; n     while (j <= n && a[j].c == a[i].c) ++j; n     lb = i, rb = j - 1; n     for (int k = i; k < j; ++k) { n       mnp[a[k].id] = a[k].p * (1 - mid * sm[rb] / sm[n]); n       mxp[a[k].id] = a[k].p * (1 - mid * (sm[lb - 1] + a[k].t) / sm[n]); n     } n   } n   for (int i = 1, j; i <= n; i = j) { n     j = i; n     while (j <= n && b[j].p == b[i].p) ++j; n     for (int k = i; k < j; ++k) n       if (mxv > mnp[b[k].id]) return 0; n     for (int k = i; k < j; ++k) mxv = max(mxv, mxp[b[k].id]); n   } n   return 1; n } n int main() { n   scanf( %d , &n); n   for (int i = 1; i <= n; ++i) scanf( %lf , &a[i].p), a[i].id = i; n   for (int i = 1; i <= n; ++i) scanf( %lf , &a[i].t), a[i].c = a[i].p / a[i].t; n   memcpy(b, a, sizeof(a)); n   sort(b + 1, b + n + 1, cmp0); n   sort(a + 1, a + n + 1, cmp); n   for (int i = 1; i <= n; ++i) sm[i] = sm[i - 1] + a[i].t; n   l = 0, r = 1; n   while (r - l > eps) { n     mid = (l + r) / 2; n     if (check()) n       l = mid; n     else n       r = mid; n   } n   printf( %.7lf n , l); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n string tostr(int n) { n   stringstream rr; n   rr << n; n   return rr.str(); n } n long long qpow(long long n, long long k) { n   long long ans = 1; n   while (k) { n     if (k & 1) ans = ans * n; n     n = n * n; n     k >>= 1; n   } n   return ans; n } n const int mod = 1e9 + 7; n const int mxn = 1e5 + 9; n const int eps = 1e-9; n int main() { n   ios_base::sync_with_stdio(false), cin.tie(NULL); n   int i, j, k, l, st; n   vector<char> v; n   string s, p; n   cin >> s; n   if (s[0] ==  - ) n     st = 1, cout <<  ( ; n   else n     st = 0; n   cout <<  $ ; n   p = s.substr(st); n   if (p.find( . ) == -1) n     for (i = p.size() - 1; i >= 0; i--) { n       v.push_back(p[i]); n       if ((p.size() - i) % 3 == 0 && i != 0) v.push_back( , ); n     } n   else n     for (i = p.find( . ) - 1; i >= 0; i--) { n       v.push_back(p[i]); n       if ((p.find( . ) - i) % 3 == 0 && i != 0) v.push_back( , ); n     } n   reverse(v.begin(), v.end()); n   for (auto c : v) cout << c; n   cout <<  . ; n   if (p.find( . ) == -1) n     cout <<  00 ; n   else { n     cout << p[p.find( . ) + 1]; n     if (p[p.find( . ) + 2] !=   0 ) n       cout << p[p.find( . ) + 2]; n     else n       cout <<  0 ; n   } n   if (st == 1) cout <<  ) ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 5e3 + 1; n int dp[N][N]; n bool palid[N][N]; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   string s; n   cin >> s; n   int n = s.size(); n   for (int d = 0; d < n; d++) n     for (int i = 0; i < n; i++) { n       int j = i + d; n       if (j >= n) continue; n       palid[i][j] = (s[i] == s[j]); n       if (i + 1 <= j - 1) palid[i][j] &= palid[i + 1][j - 1]; n     } n   for (int d = 0; d < n; d++) n     for (int i = 0; i < n; i++) { n       int j = i + d; n       if (j >= n) continue; n       dp[i][j] = palid[i][j]; n       if (i + 1 <= j) dp[i][j] += dp[i + 1][j]; n       if (i <= j - 1) dp[i][j] += dp[i][j - 1]; n       if (i + 1 <= j - 1) dp[i][j] -= dp[i + 1][j - 1]; n     } n   int q; n   cin >> q; n   for (int i = 0; i < q; i++) { n     int l, r; n     cin >> l >> r; n     l--; n     r--; n     cout << dp[l][r] <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m, flag; n int ar[101][101], ars[101][101], c[101][101], q[101][101]; n int fu() { n   int y = 1; n   for (int i = 0; i < n; i++) n     for (int j = 0; j < n; j++) n       if (ars[i][j] != c[i][j]) y = 0; n   return y; n } n void f() { n   flag = max(flag, fu()); n   for (int i = 0; i < n; i++) n     for (int j = 0; j < n; j++) q[i][j] = c[j][n - i - 1]; n   for (int i = 0; i < n; i++) n     for (int j = 0; j < n; j++) c[i][j] = q[i][j]; n   flag = max(flag, fu()); n   for (int i = 0; i < n; i++) n     for (int j = 0; j < n; j++) q[i][j] = c[j][n - i - 1]; n   for (int i = 0; i < n; i++) n     for (int j = 0; j < n; j++) c[i][j] = q[i][j]; n   flag = max(flag, fu()); n   for (int i = 0; i < n; i++) n     for (int j = 0; j < n; j++) q[i][j] = c[j][n - i - 1]; n   for (int i = 0; i < n; i++) n     for (int j = 0; j < n; j++) c[i][j] = q[i][j]; n   flag = max(flag, fu()); n } n int main() { n   cin >> n; n   for (int i = 0; i < n; i++) n     for (int j = 0; j < n; j++) { n       char a; n       cin >> a; n       if (a ==  X ) n         ar[i][j] = 0; n       else n         ar[i][j] = 1; n       c[i][j] = ar[i][j]; n     } n   for (int i = 0; i < n; i++) n     for (int j = 0; j < n; j++) { n       char a; n       cin >> a; n       if (a ==  X ) n         ars[i][j] = 0; n       else n         ars[i][j] = 1; n     } n   f(); n   for (int i = 0; i < n; i++) n     for (int j = 0; j < n; j++) c[i][j] = ar[n - i - 1][j]; n   f(); n   for (int i = 0; i < n; i++) n     for (int j = 0; j < n; j++) c[i][j] = ar[i][n - j - 1]; n   f(); n   if (flag) { n     cout <<  Yes ; n   } else n     cout <<  No ; n   return 0; n } n


#include <bits/stdc++.h> n int main() { n   int a, b, c, count = 0, i, j; n   scanf( %d%d%d , &a, &b, &c); n   if ((a == c) || (b == c)) { n     count++; n   } else { n     for (i = 0; i <= c; i += a) { n       for (j = 0; j <= c; j += b) { n         if ((i + j) == c) { n           count++; n           break; n         } n       } n     } n   } n   if (count != 0) { n     printf( Yes ); n   } else { n     printf( No ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <const int mod, const int modPhi> n struct Mint { n   int v; n   Mint() { v = 0; } n   Mint(int x) { n     v = x % mod; n     if (v < 0) v += mod; n   } n   Mint(long long x) { n     v = x % mod; n     if (v < 0) v += mod; n   } n   friend bool operator==(const Mint &a, const Mint &b) { return a.v == b.v; } n   friend bool operator!=(const Mint &a, const Mint &b) { return a.v != b.v; } n   friend bool operator<(const Mint &a, const Mint &b) { return a.v < b.v; } n   friend bool operator<=(const Mint &a, const Mint &b) { return a.v <= b.v; } n   friend bool operator>(const Mint &a, const Mint &b) { return a.v > b.v; } n   friend bool operator>=(const Mint &a, const Mint &b) { return a.v >= b.v; } n   Mint &operator+=(const Mint &a) { n     v += a.v; n     if (v >= mod) v -= mod; n     return *this; n   } n   Mint &operator-=(const Mint &a) { n     v -= a.v; n     if (v < 0) v += mod; n     return *this; n   } n   Mint &operator*=(const Mint &a) { n     v = (1LL * v * a.v) % mod; n     return *this; n   } n   Mint operator-() { return Mint(-v); } n   Mint &operator++() { return *this += 1; } n   Mint &operator--() { return *this -= 1; } n   friend Mint operator+(Mint a, const Mint b) { return a += b; } n   friend Mint operator-(Mint a, const Mint b) { return a -= b; } n   friend Mint operator*(Mint a, const Mint b) { return a *= b; } n   friend Mint min(Mint a, Mint b) { return (a < b ? b : a); } n   friend Mint max(Mint a, Mint b) { return (a > b ? a : b); } n   friend Mint pow(Mint a, long long b) { n     Mint res = 1; n     while (b > 0) { n       if (b & 1) { n         res *= a; n       } n       a *= a, b >>= 1; n     } n     return res; n   } n   friend Mint inv(const Mint &a) { return pow(a, modPhi - 1); } n   Mint operator/=(const Mint &a) { n     *this *= inv(a); n     return *this; n   } n   friend Mint operator/(Mint a, const Mint b) { return a /= b; } n   friend istream &operator>>(istream &in, Mint &a) { return in >> a.v; } n   friend ostream &operator<<(ostream &out, Mint a) { return out << a.v; } n }; n const int mod = 1e9 + 7; n using mint = Mint<mod, mod - 1>; n mint k; n mint pp = 1; n int moves = 0; n void move(int &x, int &del, int b) { n   if (x == b) del = -1; n   if (x == 1) del = 1; n   x += del; n } n int move1(int x, int del, int b) { n   if (x == b) del = -1; n   if (x == 1) del = 1; n   x += del; n   return x; n } n void testCase() { n   int n, m, rb, cb, rd, cd; n   mint p; n   cin >> n >> m >> rb >> cb >> rd >> cd; n   cin >> p; n   p /= mint(100); n   int delr = 1, delc = 1; n   if (rb == rd || cb == cd) n     k += mint(pp) * mint(p) * mint(moves), pp *= (1 - p); n   moves++; n   int initr = rb; n   int initc = cb; n   move(rb, delr, n); n   move(cb, delc, m); n   int initr1 = rb; n   int initc1 = cb; n   while (true) { n     if (rb == rd || cb == cd) n       k += mint(pp) * mint(p) * mint(moves), pp *= (1 - p); n     moves++; n     move(rb, delr, n); n     move(cb, delc, m); n     if (rb == initr && cb == initc && move1(rb, delr, n) == initr1 && n         move1(cb, delc, m) == initc1) n       break; n   } n   mint ans = mint(k + moves * pp) / mint(1 - pp); n   cout << ans <<   n ; n   moves = 0; n   pp = 1; n   k = 0; n } n int main() { n   ios::sync_with_stdio(false); n   cin.tie(nullptr); n   int tc; n   cin >> tc; n   while (tc--) { n     testCase(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, m, i, j, a, b, k; n   vector<vector<bool> > M; n   cin >> n; n   m = 5; n   M.resize(m); n   for (i = 0; i < m; i++) M[i].resize(m); n   for (i = 0; i < n; i++) { n     cin >> a >> b; n     a--; n     b--; n     M[a][b] = M[b][a] = true; n   } n   for (i = 0; i < m - 2; i++) n     for (j = i + 1; j < m - 1; j++) n       for (k = j + 1; k < m; k++) n         if (M[i][j] == M[i][k] && M[i][j] == M[j][k]) { n           cout <<  WIN ; n           return 0; n         } n   cout <<  FAIL ; n   return 0; n } n


#include <bits/stdc++.h> n int main(void) { n   int n, i; n   scanf( %d , &n); n   for (i = 1; i <= n; i++) { n     if ((i % 2) != 0) { n       printf( I hate ); n       if (i == n) { n         printf(  it ); n         break; n       } n     } else { n       printf( I love ); n       if (i == n) { n         printf(  it ); n         break; n       } n     } n     printf(  that  ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n bool IO_FILE = 0; n ifstream ifs( input.txt ); n ofstream ofs( output.txt ); n int main() { n   if (IO_FILE) { n     cin.rdbuf(ifs.rdbuf()); n     cout.rdbuf(ofs.rdbuf()); n   } n   int n, m; n   cin >> n >> m; n   char *s = new char[m]; n   bool res = 1; n   char prev =  E ; n   for (int i = 0; i < n; i++) { n     cin >> s; n     char p = s[0]; n     res = 1; n     if (prev !=  E  && p == prev) { n       res = 0; n       break; n     } n     for (int j = 1; j < m; j++) n       if (!(res = s[j] == p)) break; n     if (!res) break; n     prev = p; n   } n   cout << (res ?  YES  :  NO ); n } n


#include <bits/stdc++.h> n using namespace std; n vector<int> d[2], f; n vector<vector<bool> > nab; n int n, len = -1; n const int INF = 1 << 29; n int calc(vector<bool> &a, vector<bool> &b) { n   vector<bool> c(a.size() + b.size() + 1); n   for (int i = 0; i < b.size(); ++i) c[i] = b[i]; n   for (int i = b.size(); i < c.size(); ++i) c[i + 1] = a[i - (int)b.size()]; n   vector<int> p(c.size()); n   p[0] = 0; n   for (int i = 1; i < p.size(); ++i) { n     int j = p[i - 1]; n     while (j > 0 && (c[i] != c[j] || i == b.size() || j == b.size())) n       j = p[j - 1]; n     if (c[i] == c[j] && !(i == b.size() || j == b.size())) j++; n     p[i] = j; n   } n   return p[c.size() - 1]; n } n vector<vector<int> > p; n inline int suf(vector<bool> &a, int &l) { n   int ans = 0; n   for (int i = len - l; i < len; ++i) ans |= ((int)a[i] << (i + l - len)); n   return ans; n } n inline int pref(vector<bool> &a, int &l) { n   int ans = 0; n   for (int i = 0; i < l; ++i) ans |= ((int)a[i] << i); n   return ans; n } n int main() { n   cin.sync_with_stdio(0); n   cin >> n; n   nab.resize(n); n   d[0].resize(n + 1); n   d[1].resize(n + 1); n   string str; n   for (int i = 0; i < n; ++i) { n     cin >> str; n     if (len == -1) len = str.size(); n     nab[i].resize(len); n     for (int j = 0; j < len; ++j) nab[i][j] = (str[j] ==  0  ? 0 : 1); n   } n   p.resize(len + 1, vector<int>(1 << len, -1)); n   d[0][0] = d[1][0] = 0; n   p[0][0] = 1; n   vector<int> f(n); n   f[0] = len; n   for (int i = 1; i < n; ++i) f[i] = f[i - 1] + len - calc(nab[i - 1], nab[i]); n   int suffix[len + 1], prefix[len + 1]; n   d[1][1] = d[0][1] = len; n   int bestp, bestd, now; n   for (int i = 2; i <= n; ++i) { n     d[0][i] = min(d[0][i - 1], d[1][i - 1]) + f[i - 1] - f[i - 2]; n     d[1][i] = INF; n     for (int j = 1; j <= len; ++j) { n       suffix[j] = suf(nab[i - 2], j); n       prefix[j] = pref(nab[i - 1], j); n     } n     bestp = p[0][0]; n     d[1][i] = d[1][bestp] + f[i - 2] - f[bestp - 1] + len; n     for (int j = 1; j <= len; ++j) { n       int &pp = p[j][prefix[j]]; n       if (pp != -1) { n         now = d[1][pp] + f[i - 2] - f[pp - 1] + len - j; n         if (now < d[1][i]) { n           d[1][i] = now; n           bestp = pp; n         } n       } n     } n     if (d[1][i] < d[1][p[0][0]] + f[i - 1] - f[p[0][0] - 1]) p[0][0] = i; n     for (int j = 1; j <= len; ++j) { n       int &ps = p[j][suffix[j]]; n       if (ps == -1 || (d[1][i] < d[1][ps] + f[i - 1] - f[ps - 1])) ps = i; n     } n   } n   cout << min(d[1][n], d[0][n]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n inline void read(int &x) { n   x = 0; n   int 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   x *= f; n } n inline void judge() { n   freopen( in.txt ,  r , stdin); n   freopen( out.txt ,  w , stdout); n } n const int maxn = 200005; n int n, Q, s[maxn], m[maxn], t[maxn], l[maxn], r[maxn], R[maxn]; n set<pair<pair<int, int>, int> > res; n namespace seg { n long long suma[maxn << 5], sumb[maxn << 5]; n int ls[maxn << 5], rs[maxn << 5], tot; n inline void update(int x) { n   suma[x] = suma[ls[x]] + suma[rs[x]]; n   sumb[x] = sumb[ls[x]] + sumb[rs[x]]; n } n inline void add(int x, int &y, int place, int va, int vb, int l, int r) { n   y = ++tot; n   suma[y] = suma[x]; n   sumb[y] = sumb[x]; n   ls[y] = ls[x]; n   rs[y] = rs[x]; n   suma[y] += va; n   sumb[y] += vb; n   if (l == r) { n     return; n   } n   int md = (l + r) >> 1; n   if (place <= md) n     add(ls[x], ls[y], place, va, vb, l, md); n   else n     add(rs[x], rs[y], place, va, vb, md + 1, r); n } n inline long long queryA(int x, int l, int r, int lq, int rq) { n   if (!x) return 0; n   if (lq <= l && r <= rq) return suma[x]; n   long long res = 0; n   int md = (l + r) >> 1; n   if (lq <= md) res += queryA(ls[x], l, md, lq, rq); n   if (rq > md) res += queryA(rs[x], md + 1, r, lq, rq); n   return res; n } n inline long long queryB(int x, int l, int r, int lq, int rq) { n   if (!x) return 0; n   if (lq <= l && r <= rq) return sumb[x]; n   long long res = 0; n   int md = (l + r) >> 1; n   if (lq <= md) res += queryB(ls[x], l, md, lq, rq); n   if (rq > md) res += queryB(rs[x], md + 1, r, lq, rq); n   return res; n } n }  // namespace seg n int qtot = 0; n int root[maxn]; n inline long long query(int l, int r, int tme) { n   tme = min(tme, 99999); n   return seg::queryA(root[r], 0, 100001, 0, tme) - n          seg::queryA(root[l - 1], 0, 100001, 0, tme) + n          1ll * n              (seg::queryB(root[r], 0, 100001, tme + 1, 100001) - n               seg::queryB(root[l - 1], 0, 100001, tme + 1, 100001)) * n              tme; n } n int main() { n   read(n); n   for (int i = (1); i <= (n); i++) read(s[i]), read(m[i]), read(R[i]); n   for (int i = (1); i <= (n); i++) res.insert(make_pair(make_pair(i, i), -1)); n   read(Q); n   for (int i = (1); i <= (Q); i++) read(t[i]), read(l[i]), read(r[i]); n   for (int i = (1); i <= (n); i++) { n     if (R[i] == 0) { n       seg::add(root[i - 1], root[i], 100001, 0, R[i], 0, 100001); n       continue; n     } n     int t = floor((long double)m[i] / R[i] + 0.99999999); n     assert(1ll * t * R[i] >= m[i]); n     assert(1ll * (t - 1) * R[i] < m[i]); n     seg::add(root[i - 1], root[i], t, m[i], R[i], 0, 100001); n   } n   for (int i = (1); i <= (Q); i++) { n     long long ans = 0; n     set<pair<pair<int, int>, int> >::iterator it = n         res.upper_bound(make_pair(make_pair(l[i], n + 1), 1)); n     vector<set<pair<pair<int, int>, int> >::iterator> rubi; n     rubi.clear(); n     it--; n     if (it->first.first < l[i]) { n       res.insert(make_pair(make_pair(it->first.first, l[i] - 1), it->second)); n     } n     int tmp1 = it->first.second, tmp2 = it->second; n     res.erase(it); n     res.insert(make_pair(make_pair(l[i], tmp1), tmp2)); n     it = res.upper_bound(make_pair(make_pair(l[i], n + 1), 1)); n     it--; n     for (;;) { n       if (it->second == -1) { n         assert(it->first.first == it->first.second); n         ans += min( n             (long long)m[it->first.first], n             (long long)s[it->first.first] + 1ll * R[it->first.first] * t[i]); n       } else { n         ans += query(it->first.first, min(it->first.second, r[i]), n                      t[i] - it->second); n         long long res = 0; n       } n       rubi.push_back(it); n       if (it->first.second > r[i]) { n         res.insert( n             make_pair(make_pair(r[i] + 1, it->first.second), it->second)); n         break; n       } n       if (it->first.second == r[i]) break; n       it++; n     } n     for (set<pair<pair<int, int>, int> >::iterator x : rubi) res.erase(x); n     res.insert(make_pair(make_pair(l[i], r[i]), t[i])); n     cout << ans << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n  n void solve(){ n     int n; n     cin >> n; n     vector<int64_t> X; n     vector<int64_t> Y; n     int x,y; n     for (int i = 0; i < n; i++){ n         cin >> x >> y; n         X.push_back(x); n         Y.push_back(y); n     } n     sort(X.begin(),X.end());         n     sort(Y.begin(),Y.end());         n     int64_t res; n     if (n%2 == 0){ n         res = (X.at(n/2)-X.at(n/2-1)+1)*(Y.at(n/2)-Y.at(n/2-1)+1); n     } else { n         res = 1; n     } n     cout << res << endl; n     return; n } n  n int main() { n     int t; n     cin >> t; n     while (t){ n         solve(); n         t--; n     } n     return 0; n }


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, x0; n   cin >> n >> x0; n   int x = x0; n   int a[n][2]; n   for (int i = 0; i < n; i++) cin >> a[i][0] >> a[i][1]; n   int c = 0, d = 0; n   for (int i = 0; i < n; i++) { n     if (min(a[i][0], a[i][1]) > x) { n       x = min(a[i][0], a[i][1]); n       c++; n     } else if (max(a[i][0], a[i][1]) < x) { n       d++; n       x = max(a[i][0], a[i][1]); n     } n     if (c > 0 && d > 0) break; n   } n   if (c > 0 && d > 0) n     cout <<  -1 n ; n   else { n     c = 0; n     for (int i = 0; i < n; i++) { n       if (x >= min(a[i][0], a[i][1]) && x <= max(a[i][0], a[i][1])) c++; n     } n     if (c == n) n       cout << abs(x - x0) << endl; n     else n       cout <<  -1 n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n void input() { n   freopen( in.txt ,  r , stdin); n   freopen( out.txt ,  w , stdout); n } n const int N = 1e6 + 10; n vector<int> G[N]; n int in[N], n, cnt[3], vis[N]; n void dfs(int x, int flag) { n   cnt[flag]++; n   vis[x] = 1; n   for (int i = 0; i < G[x].size(); i++) { n     int u = G[x][i]; n     if (vis[u]) continue; n     dfs(u, (flag + 1) % 2); n   } n } n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   cin >> n; n   for (int i = 1; i <= n - 1; i++) { n     int u, v; n     cin >> u >> v; n     G[u].push_back(v); n     G[v].push_back(u); n   } n   dfs(1, 1); n   cout << min(cnt[0], cnt[1]) - 1 <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n #pragma warning(disable : 4996) n int main() { n   int n, k; n   scanf( %d %d , &n, &k); n   int cnt = 0, a[100]; n   for (int i = 0; i < n; i++) { n     scanf( %d , &a[i]); n   } n   for (int i = 0; i < n; i++) { n     if (a[i] >= a[k - 1] && a[i] > 0) cnt++; n   } n   printf( %d , cnt); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n = 0; n vector<int> v; n int flag[2010]; n int countt = 0; n int k = 0; n int maxt = 0; n int main() { n   cin >> n; n   cin >> k; n   for (int i = 0; i < n; i++) { n     int kari = 0; n     cin >> kari; n     v.push_back(kari); n     if (i == 0) { n       maxt = kari; n     } else { n       maxt = max(maxt, kari); n     } n   } n   for (int i = 0; i < n; i++) { n     for (int j = i + 1; j < n; j++) { n       vector<int> u; n       vector<int> d; n       u.clear(); n       d.clear(); n       int sum = 0; n       for (int jj = 0; jj < n; jj++) { n         if (jj < i || j < jj) { n           d.push_back(v[jj]); n         } else { n           sum += v[jj]; n           u.push_back(v[jj]); n         } n       } n       sort(u.begin(), u.end()); n       sort(d.begin(), d.end()); n       maxt = max(maxt, sum); n       { n         int ii = 0; n         int jj = d.size() - 1; n         for (; ii < u.size() && jj >= 0 && ii < k; ii++, jj--) { n           sum -= u[ii]; n           sum += d[jj]; n           maxt = max(maxt, sum); n         } n       } n     } n   } n   cout << maxt << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int64_t k = 0; n inline bool equals(string& s1, int64_t i1, int64_t j1, string& s2, int64_t i2, n                    int64_t j2) { n   if (j1 != j2) { n     return false; n   } n   auto x = j1; n   for (int64_t i = 0; i < x; i++) { n     if (s1[i1 + i] != s2[i2 + i]) { n       return false; n     } n   } n   return true; n } n bool equivilant(string& s1, int64_t i1, int64_t j1, string& s2, int64_t i2, n                 int64_t j2) { n   if (equals(s1, i1, j1, s2, i2, j2)) { n     return true; n   } n   if (j1 != j2 || j1 % 2 == 1) { n     return false; n   } n   auto x = j1 / 2; n   if (equivilant(s1, i1, x, s2, i2, x) && n       equivilant(s1, i1 + x, x, s2, i2 + x, x)) { n     return true; n   } n   if (equivilant(s1, i1, x, s2, i2 + x, x) && n       equivilant(s1, i1 + x, x, s2, i2, x)) { n     return true; n   } n   return false; n } n int main() { n   ios::sync_with_stdio(false); n   string s1; n   cin >> s1; n   string s2; n   cin >> s2; n   if (equivilant(s1, 0, s1.size(), s2, 0, s2.size())) { n     cout <<  YES ; n   } else { n     cout <<  NO ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct QUERY { n   int query, time, number; n }; n void update(vector<map<int, int> > &tree, int v, int tl, int tr, int &pos, n             int query, int value) { n   if (pos < tl || pos > tr) return; n   if (query == 1) n     tree[v][value]++; n   else if (query == 2) n     tree[v][value]--; n   if (tr != tl) { n     update(tree, 2 * v, tl, (tr + tl) / 2, pos, query, value); n     update(tree, 2 * v + 1, (tr + tl) / 2 + 1, tr, pos, query, value); n   } n } n int sum(vector<map<int, int> > &tree, int v, int tl, int tr, int i, int j, n         int value) { n   if (i > tr || j < tl) return 0; n   if (i <= tl && j >= tr) return tree[v][value]; n   int sum1 = sum(tree, 2 * v, tl, (tr + tl) / 2, i, j, value); n   int sum2 = sum(tree, 2 * v + 1, (tr + tl) / 2 + 1, tr, i, j, value); n   return sum1 + sum2; n } n int main() { n   int n; n   cin >> n; n   vector<QUERY> a(n + 1); n   vector<int> num(n + 1); n   vector<int> t(n + 1); n   for (int i = 1; i <= n; ++i) { n     cin >> a[i].query >> a[i].time >> a[i].number; n     num[i] = a[i].number; n     t[i] = a[i].time; n   } n   sort(num.begin(), num.end()); n   num.erase(unique(num.begin(), num.end()), num.end()); n   sort(t.begin(), t.end()); n   t.erase(unique(t.begin(), t.end()), t.end()); n   map<int, int> help; n   for (unsigned int i = 1; i < num.size(); ++i) { n     help.insert(make_pair(num[i], i)); n   } n   for (int i = 1; i <= n; ++i) { n     a[i].number = help[a[i].number]; n   } n   help.clear(); n   for (unsigned int i = 1; i < t.size(); ++i) { n     help.insert(make_pair(t[i], i)); n   } n   for (int i = 1; i <= n; ++i) { n     a[i].time = help[a[i].time]; n   } n   int len = t.size() - 1; n   int tree_size = 4 * len; n   vector<map<int, int> > tree(tree_size); n   for (int i = 1; i <= n; ++i) { n     if (a[i].query != 3) n       update(tree, 1, 1, len, a[i].time, a[i].query, a[i].number); n     else n       cout << sum(tree, 1, 1, len, 1, a[i].time, a[i].number) << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 100000 + 10; n int n; n int a[N]; n long long query(int l, int r) { n   vector<pair<long long, long long> > stack; n   long long ret = 0; n   long long cur = 0; n   for (int i = l + 1; i <= r; ++i) { n     long long diff = abs(a[i] - a[i - 1]); n     long long tot = 1; n     for (; stack.size();) { n       if (stack.back().first <= diff) { n         tot += stack.back().second; n         cur -= stack.back().first * stack.back().second; n         stack.pop_back(); n       } else { n         break; n       } n     } n     stack.push_back(make_pair(diff, tot)); n     cur += diff * tot; n     ret += cur; n   } n   return ret; n } n void solve() { n   int q; n   cin >> n >> q; n   for (int i = 0; i < n; ++i) { n     scanf( %d , a + i); n   } n   for (; q--;) { n     int l, r; n     scanf( %d%d , &l, &r); n     --l, --r; n     cout << query(l, r) << endl; n   } n } n int main() { n   solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long s, k; n long long a[5000]; n long long ans[5000]; n int t; n int dfs(int n, int m) { n   if (n == 0) { n     if (t == 1) { n       cout << 2 << endl; n       cout << 0 <<     << ans[0] << endl; n     } else { n       cout << t << endl; n       for (int i = t - 1; i >= 0; i--) cout << ans[i] <<    ; n       cout << endl; n     } n     exit(0); n   } n   for (int i = m; i >= 0; i--) { n     if (n >= a[i]) { n       ans[t++] = a[i]; n       dfs(n - a[i], i - 1); n       t--; n     } n   } n } n int main() { n   cin >> s >> k; n   int m = 0; n   a[0] = 1; n   for (int i = 1; a[i - 1] < s; i++) { n     for (int j = i - 1; j >= 0 && j >= i - k; j--) a[i] += a[j]; n     m = i; n   } n   if (a[m] > s) m--; n   dfs(s, m); n } n


#include <bits/stdc++.h> n int cmp(const void *a, const void *b) { return (*(int *)a - *(int *)b); } n long long int mod; n void f(long long int p) { n   long long ans = 1, n = 3; n   while (p) { n     if (p & 1) ans = (ans * n) % mod; n     n = (n * n) % mod; n     p = p >> 1; n   } n   ans--; n   while (ans < 0) ans += mod; n   printf( %lld , ans); n } n int main() { n   long long int n; n   scanf( %lld%lld , &n, &mod); n   f(n); n   return 0; n } n


#include <bits/stdc++.h> n #pragma comment(linker,  /STACK:128777216 ) n const int null = 0; n using namespace std; n template <class T> n int size(const T &a) { n   return int(a.size()); n } n template <class T> n T abs(const T &a) { n   return (a < 0 ? -a : a); n } n template <class T> n T sqr(const T &a) { n   return a * a; n } n const double pi = acos(-1.0); n const int max_n = 2048; n char s[max_n][max_n]; n struct Res { n   int r, c; n   int d; n   int moves; n   Res() { moves = -1; } n } dp[max_n][max_n]; n bool was[max_n][max_n]; n Res &calc(int r, int c, int d = -1) { n   Res &res = dp[r][c]; n   if (res.moves >= 0) { n     return res; n   } n   if (was[r][c]) { n     cout << -1 << endl; n     exit(0); n   } n   was[r][c] = true; n   switch (s[r][c]) { n     case  # : n       res.moves = 0; n       res.d = -1; n       res.r = r; n       res.c = c; n       break; n     case  > : n       res = calc(r, c + 1, 1); n       res.moves++; n       if (res.moves == 1) { n         res.d = 1; n       } n       break; n     case  < : n       res = calc(r, c - 1, 2); n       res.moves++; n       if (res.moves == 1) { n         res.d = 2; n       } n       break; n     case  ^ : n       res = calc(r - 1, c, 3); n       res.moves++; n       if (res.moves == 1) { n         res.d = 3; n       } n       break; n     case  v : n       res = calc(r + 1, c, 4); n       res.moves++; n       if (res.moves == 1) { n         res.d = 4; n       } n       break; n   } n   return res; n } n int main() { n   int n, m; n   scanf( %d %d , &n, &m); n   for (int i = 0; i < n; i++) { n     scanf( %s , s[i]); n   } n   int res = 0; n   Res best; n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < m; j++) { n       Res cur = calc(i, j); n       if (cur.moves > best.moves) { n         best = cur; n       } n     } n   } n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < m; j++) { n       Res cur = dp[i][j]; n       if (cur.moves + best.moves > res && n           (cur.d != best.d || cur.r != best.r || cur.c != best.c || n            cur.moves != best.moves)) { n         res = cur.moves + best.moves; n       } n     } n   } n   cout << res << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int T, x; n char str[100005], ans[100005]; n char usd[100005]; n int main() { n   scanf( %d , &T); n   while (T--) { n     scanf( %s , str + 1); n     scanf( %d , &x); n     int n = strlen(str + 1); n     for (int i = 1; i <= n; ++i) { n       if (str[i] ==  0 ) { n         if (i + x <= n) ans[i + x] =  0 ; n         if (i - x >= 1) ans[i - x] =  0 ; n       } n     } n     for (int i = 1; i <= n; ++i) n       if (!ans[i]) ans[i] =  1 ; n     for (int i = 1; i <= n; ++i) { n       if (i - x >= 1 && ans[i - x] ==  1 ) n         usd[i] =  1 ; n       else if (i + x <= n && ans[i + x] ==  1 ) n         usd[i] =  1 ; n       else n         usd[i] =  0 ; n     } n     bool flag = true; n     for (int i = 1; i <= n; ++i) n       if (usd[i] != str[i]) { n         flag = false; n         break; n       } n     if (!flag) n       printf( -1 n ); n     else n       printf( %s n , ans + 1); n     for (int i = 1; i <= n; ++i) ans[i] = 0; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long MOD = 998244353; n signed main() { n   cin.tie(0)->sync_with_stdio(0); n   long long n, m; n   cin >> n >> m; n   vector<long long> pw(n + 1); n   pw[0] = 1; n   for (long long i = 1; i <= n; i++) pw[i] = 2 * pw[i - 1] % MOD; n   long long _ = n; n   vector<long long> basis; n   for (long long i = 0; i < n; i++) { n     long long a; n     cin >> a; n     for (auto x : basis) a = min(a, a ^ x); n     if (a) { n       long long id = 0; n       while (id < (long long)basis.size() && basis[id] > a) ++id; n       basis.insert(basis.begin() + id, a); n     } n   } n   n = basis.size(); n   vector<long long> ans(2 * m + 1, 0); n   if (n <= 20) { n     for (long long mask = 0; mask < (1ll << n); mask++) { n       long long val = 0; n       for (long long i = 0; i < n; i++) n         if (mask & (1ll << i)) val ^= basis[i]; n       ans[__builtin_popcountll(val)]++; n     } n   } else { n     vector<bool> good(m); n     for (auto x : basis) n       for (long long i = m - 1; i >= 0; i--) n         if (x & (1ll << i)) { n           good[i] = 1; n           break; n         } n     for (long long i = 0; i < n; i++) n       for (long long j = i + 1; j < n; j++) n         basis[i] = min(basis[i], basis[i] ^ basis[j]); n     vector<long long> stuff(n, 0); n     for (long long i = 0; i < n; i++) { n       long long cnt = 0; n       for (long long j = 0; j < m; j++) n         if (!good[j]) { n           if (basis[i] & (1ll << j)) stuff[i] += (1ll << cnt); n           cnt++; n         } n     } n     vector<vector<vector<long long>>> dp( n         n, vector<vector<long long>>(n + 1, n                                      vector<long long>((1ll << (m - n)), 0))); n     dp[0][1][stuff[0]] = dp[0][0][0] = 1; n     for (long long i = 1; i < n; i++) n       for (long long j = 0; j <= n; j++) n         for (long long k = 0; k < (1ll << (m - n)); k++) { n           dp[i][j][k] = dp[i - 1][j][k]; n           if (j) dp[i][j][k] += dp[i - 1][j - 1][k ^ stuff[i]]; n         } n     for (long long i = 0; i <= n; i++) n       for (long long j = 0; j < (1ll << (m - n)); j++) n         ans[i + __builtin_popcount(j)] += dp[n - 1][i][j]; n   } n   for (long long i = 0; i <= m; i++) n     cout << (ans[i] % MOD) * pw[_ - n] % MOD <<    ; n   cout <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int t; n   scanf( %d , &t); n   while (t--) { n     int N; n     scanf( %d , &N); n     vector<int> a(N + 1); n     vector<int> b(N + 1); n     int sum = 0; n     for (int i = 1; i <= N; ++i) { n       scanf( %d , &a[i]); n       sum += a[i]; n       b[i] = a[i]; n     } n     if (sum % N != 0) { n       printf( -1 n ); n       continue; n     } n     vector<vector<int>> ans; n     for (int i = 2; i <= N; ++i) { n       if (a[i] % i == 0) { n         b[i] -= a[i]; n         b[1] += a[i]; n         ans.push_back({i, 1, a[i] / i}); n       } else { n         int m = i - a[i] % i; n         b[1] -= m; n         b[i] += m; n         ans.push_back({1, i, m}); n         ans.push_back({i, 1, (a[i] + m) / i}); n         b[i] -= (a[i] + m); n         b[1] += (a[i] + m); n       } n     } n     for (int i = 2; i <= N; ++i) { n       b[1] -= sum / N; n       b[i] += sum / N; n       ans.push_back({1, i, sum / N}); n     } n     printf( %d n , (int)ans.size()); n     for (vector<int>& v : ans) { n       printf( %d %d %d n , v[0], v[1], v[2]); n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n int a[200000]; n int b[200000]; n int c[200001]; n int t[200001]; n vector<int> x; n vector<int> ans; n int add(int r, int cr, int d) { n   if (c[r] == t[r]) cr--; n   t[r] += d; n   if (c[r] == t[r]) cr++; n   return cr; n } n int main() { n   int n, m, p, i, j; n   scanf( %d %d %d , &n, &m, &p); n   for (i = 0; i < n; i++) scanf( %d , &a[i]); n   for (i = 0; i < m; i++) { n     scanf( %d , &b[i]); n     x.push_back(b[i]); n   } n   x.push_back(1000000001); n   sort(x.begin(), x.end()); n   x.erase(unique(x.begin(), x.end()), x.end()); n   int k = x.size(); n   for (i = 0; i < n; i++) { n     int pos = lower_bound(x.begin(), x.end(), a[i]) - x.begin(); n     if (a[i] == x[pos]) n       a[i] = pos; n     else n       a[i] = k - 1; n   } n   for (i = 0; i < k; i++) { n     c[i] = 0; n     t[i] = 0; n   } n   for (i = 0; i < m; i++) { n     int pos = lower_bound(x.begin(), x.end(), b[i]) - x.begin(); n     c[pos]++; n   } n   int cr = 1; n   long long d = (long long)p * (m - 1); n   for (i = 0; i < p; i++) { n     if (i + d >= n) continue; n     for (j = 0; j < m; j++) cr = add(a[i + p * j], cr, 1); n     if (cr == k) ans.push_back(i); n     j = i + p * m; n     while (j < n) { n       cr = add(a[j], cr, 1); n       cr = add(a[j - p * m], cr, -1); n       if (cr == k) ans.push_back(j - p * (m - 1)); n       j += p; n     } n     int l = j - p; n     for (j = 0; j < m; j++) cr = add(a[l - p * j], cr, -1); n   } n   sort(ans.begin(), ans.end()); n   printf( %d n , ans.size()); n   for (i = 0; i < ans.size(); i++) printf( %d  , ans[i] + 1); n   puts(  ); n   return 0; n } n


#include <bits/stdc++.h> n class Counter { n  public: n   Counter(std::vector<size_t> perm1, std::vector<size_t> perm2) n       : size_(perm1.size()) { n     std::vector<size_t> inv2(size_, 0); n     for (size_t i = 0; i < size_; ++i) { n       inv2[perm2[i]] = i; n     } n     auto& points = perm1; n     for (size_t x = 0; x < size_; ++x) { n       points[x] = inv2[perm1[x]]; n     } n     size_t nextPowOfTwo = 1; n     while (nextPowOfTwo < size_) nextPowOfTwo <<= 1; n     segTree_.resize(2 * nextPowOfTwo - 1); n     build(points, 0, 0, size_ - 1); n   } n   int count(size_t minX, size_t maxX, size_t minY, size_t maxY) { n     return countAux(0, 0, size_ - 1, minX, maxX, minY, maxY); n   } n  n  private: n   void build(const std::vector<size_t>& points, int vertex, int from, int to) { n     if (from == to) { n       segTree_[vertex].push_back(points[from]); n     } else { n       int middle = from + (to + 1 - from) / 2; n       build(points, 2 * vertex + 1, from, middle - 1); n       build(points, 2 * vertex + 2, middle, to); n       const auto& left = segTree_[2 * vertex + 1]; n       const auto& right = segTree_[2 * vertex + 2]; n       segTree_[vertex].reserve(left.size() + right.size()); n       std::merge(left.begin(), left.end(), right.begin(), right.end(), n                  std::back_inserter(segTree_[vertex])); n     } n   } n   int countAux(int vertex, int from, int to, int minX, int maxX, int minY, n                int maxY) { n     if (maxX < minX) { n       return 0; n     } n     if (from == minX && to == maxX) { n       const auto& range = segTree_[vertex]; n       auto it1 = std::lower_bound(range.begin(), range.end(), minY); n       auto it2 = std::lower_bound(range.begin(), range.end(), maxY + 1); n       return it2 - it1; n     } n     int middle = from + (to + 1 - from) / 2; n     int countLeft = countAux(2 * vertex + 1, from, middle - 1, minX, n                              std::min(maxX, middle - 1), minY, maxY); n     int countRight = countAux(2 * vertex + 2, middle, to, n                               std::max(minX, middle), maxX, minY, maxY); n     return countLeft + countRight; n   } n  n  private: n   size_t size_; n   std::vector<std::vector<int>> segTree_; n }; n int main() { n   size_t size; n   ::scanf( %lu , &size); n   auto readPerm = [](size_t size) { n     std::vector<size_t> perm; n     perm.reserve(size); n     for (size_t i = 0; i < size; ++i) { n       size_t elem; n       ::scanf( %lu , &elem); n       --elem; n       perm.push_back(elem); n     } n     return perm; n   }; n   auto perm1 = readPerm(size); n   auto perm2 = readPerm(size); n   Counter counter(std::move(perm1), std::move(perm2)); n   size_t nQueries; n   ::scanf( %lu , &nQueries); n   size_t x = 0; n   auto f = [&](size_t z) { return (z + x - 1) % size; }; n   for (size_t i = 0; i < nQueries; ++i) { n     size_t a, b, c, d; n     ::scanf( %lu %lu %lu %lu , &a, &b, &c, &d); n     size_t count = counter.count(std::min(f(a), f(b)), std::max(f(a), f(b)), n                                  std::min(f(c), f(d)), std::max(f(c), f(d))); n     std::cout << count <<   n ; n     x = count + 1; n   } n } n


#include <bits/stdc++.h> n const long long N = 3000006; n using namespace std; n const long long MOD = 1000000007LL; n template <typename T> n T gcd(T a, T b) { n   if (a == 0) return b; n   return gcd(b % a, a); n } n template <typename T> n T power(T x, T y, long long m = MOD) { n   T ans = 1; n   while (y > 0) { n     if (y & 1LL) ans = (ans * x) % m; n     y >>= 1LL; n     x = (x * x) % m; n   } n   return ans % m; n } n long long prime[N]; n long long a[N], f[N]; n int32_t main() { n   memset(prime, 1, sizeof(prime)); n   prime[1] = 0; n   for (long long i = 2; i < N; i++) { n     if (!prime[i]) continue; n     for (long long j = i * i; j < N; j += i) prime[j] = 0; n   } n   long long n; n   cin >> n; n   for (long long(i) = (0); i < (n); i++) { n     cin >> a[i]; n     ++f[a[i]]; n   } n   long long maxi = 0; n   if (n == 1) { n     cout <<  1  <<   n ; n     cout << a[0]; n     return 0; n   } n   for (long long(i) = (0); i < (n); i++) { n     if (a[i] == 1) continue; n     if (prime[a[i] + 1] && f[1]) { n       cout << f[1] + 1 <<   n ; n       for (long long(i) = (0); i < (f[1]); i++) cout <<  1  ; n       cout << a[i]; n       return 0; n     } n   } n   if (f[1] >= 2) { n     cout << f[1] <<   n ; n     for (long long(i) = (0); i < (f[1]); i++) cout <<  1  ; n     return 0; n   } n   for (long long(i) = (0); i < (n - 1); i++) { n     for (long long(j) = (i + 1); j < (n); j++) { n       if (prime[a[i] + a[j]]) { n         cout <<  2  <<   n ; n         cout << a[i] <<     << a[j]; n         return 0; n       } n     } n   } n   cout <<  1  <<   n ; n   cout << a[0]; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 2e5 + 9; n char s[MAXN]; n map<char, char> mp; n int main(int argc, char const *argv[]) { n   int n, m; n   scanf( %d%d , &n, &m); n   scanf( %s , s); n   int present = 0; n   for (int i = 0; i < n; i++) { n     present |= (1 << (s[i] -  a )); n   } n   for (char c =  a ; c <=  z ; c++) n     if (present & (1 << (c -  a ))) { n       mp[c] = c; n     } n   char one, two; n   for (int i = 0; i < m; i++) { n     scanf(  %c %c , &one, &two); n     if (one == two) { n       continue; n     } n     for (auto &it : mp) { n       if (it.second == one) n         it.second = two; n       else if (it.second == two) n         it.second = one; n     } n   } n   for (int i = 0; i < n; i++) n     if (mp.count(s[i])) { n       s[i] = mp[s[i]]; n     } n   puts(s); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MX = (int)1e6 + 17; n const int MOD = (int)1e9 + 7; n const long long oo = (long long)1e18 + 7; n const int INF = (int)999999999; n const int N = (int)1e5 + 17; n const int di[4] = {-1, 0, 1, 0}; n const int dj[4] = {0, 1, 0, -1}; n inline long long IN() { n   long long x = 0, ch = getchar(), f = 1; n   while (!isdigit(ch) && (ch !=  - ) && (ch != EOF)) ch = getchar(); n   if (ch ==  - ) { n     f = -1; n     ch = getchar(); n   } n   while (isdigit(ch)) { n     x = (x << 1) + (x << 3) + ch -  0 ; n     ch = getchar(); n   } n   return x * f; n } n inline void OUT(long long x) { n   if (x < 0) putchar( - ), x = -x; n   if (x >= 10) n     OUT(x / 10), putchar(x % 10 +  0 ); n   else n     putchar(x +  0 ); n } n double d, h, v, e, ans; n int main() { n   cin >> d >> h >> v >> e; n   ans = e - ((4 * v) / n              (3.14159265358979323846264338327950288419716939937510 * d * d)); n   h = -h; n   if (ans > -1e4 && ans < 0) n     cout <<  YES  << endl << fixed << setprecision(10) << (h / ans); n   else n     cout <<  NO ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long n, k, x = -1, m, j = 0; n   cin >> n; n   cin >> k; n   if (k == 2) { n     x = 1; n   } n   if (k == 1) { n     x = 0; n   } n   if (k == 0) { n     j++; n   } n   for (int i = 1; i < n; i++) { n     cin >> k; n     if (k == 0) { n       j++; n       x = -1; n     } else { n       if (x == -1 and k == 3) { n       } else { n         if (k == 3) { n           x = ((x - 1) % 2 + 2) % 2; n         } else { n           if (k - 1 == x) { n             j++; n             x = -1; n           } else { n             x = k - 1; n           } n         } n       } n     } n   } n   cout << j; n   return 0; n } n


#include <bits/stdc++.h> n #pragma GCC optimize( Ofast ) n #pragma GCC target(   n      avx,avx2,fma,sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native ) n using namespace std; n long long MOD = 1e9 + 7; n int main() { n   ios::sync_with_stdio(0), cin.tie(0), cout.tie(0), n       cout << fixed << setprecision(20); n   long long t; n   cin >> t; n   while (t--) { n     long long n, p; n     cin >> n >> p; n     vector<pair<long long, long long> > ans; n     for (int i = 0; i < 2 * n + p; i++) { n       ans.push_back({i % n, (i + i / n + 1) % n}); n     } n     for (auto i : ans) { n       cout << i.first + 1 <<     << i.second + 1 <<   n ; n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n struct scroll { n   int pow; n   int dmg; n   int loc; n   bool operator<(const scroll& a) const { n     if (dmg > a.dmg) n       return true; n     else if (dmg == a.dmg && pow < a.pow) n       return true; n     else n       return false; n   } n }; n struct used_scrollr { n   int num; n   int time; n   bool operator<(const used_scrollr& a) const { return time < a.time; } n }; n int main() { n   int N, max, reg; n   scanf( %d%d%d , &N, &max, &reg); n   scroll ary[N]; n   bool used_scroll[N]; n   int used_time[N]; n   for (int i = 0; i < N; ++i) { n     int x, y; n     scanf( %d%d , &x, &y); n     ary[i] = (scroll){x, y, i + 1}; n     used_scroll[i] = false; n   } n   sort(ary, ary + N); n   int time = 0; n   int used_scrolls = 0; n   int current_hp = max; n   int drain = 0; n   while (true) { n     current_hp -= drain; n     if (current_hp + reg < max) n       current_hp += reg; n     else n       current_hp = max; n     if (current_hp <= 0) { n       time++; n       break; n     } n     bool used_a_scroll = false; n     for (int i = 0; i < N; ++i) { n       if (used_scroll[i] == false && n           (((current_hp * 100) / max) + n                ((current_hp * 100) % max == 0 ? 0 : 1) <= n            ary[i].pow)) { n         used_time[i] = time; n         drain += ary[i].dmg; n         used_scroll[i] = true; n         used_scrolls++; n         used_a_scroll = true; n         break; n       } n     } n     if ((reg - drain) >= 0 && used_a_scroll == false) break; n     time++; n   } n   if (current_hp <= 0) { n     printf( YES n ); n     printf( %d %d n , time - 1, used_scrolls); n     used_scrollr ary2[used_scrolls]; n     int cnt = 0; n     for (int i = 0; i < N; ++i) n       if (used_scroll[i]) n         ary2[cnt++] = (used_scrollr){ary[i].loc, used_time[i]}; n     sort(ary2, ary2 + used_scrolls); n     for (int i = 0; i < used_scrolls; ++i) n       printf( %d %d n , ary2[i].time, ary2[i].num); n   } else { n     printf( NO n ); n   } n } n


#include <bits/stdc++.h> n using namespace std; n const long long MAXN = 100000; n const long long oops = 1LL << 60; n class Ball { n  public: n   long long value, color; n } arr[MAXN + 5]; n long long colorBest[MAXN + 5]; n long long n; n void addToTopTwo(vector<pair<long long, int> >& toptwo, long long best, n                  long long color) { n   if (toptwo.size() != 2) n     toptwo.push_back(make_pair(best, color)); n   else { n     long long c, c2; n     bool khalas = 0; n     for (c = 0; c < 2; c++) n       if (toptwo[c].second == color) { n         toptwo[c].first = max(toptwo[c].first, best); n         khalas = 1; n       } n     if (!khalas) { n       if (toptwo[0].first > toptwo[1].first) { n         if (toptwo[1].first < best) toptwo[1] = make_pair(best, color); n       } else { n         if (toptwo[0].first < best) toptwo[0] = make_pair(best, color); n       } n     } n   } n   return; n } n int main() { n   long long c, c2; n   long long qs; n   scanf( %I64d%I64d , &n, &qs); n   for (c = 0; c < n; c++) scanf( %I64d , &arr[c].value); n   for (c = 0; c < n; c++) scanf( %I64d , &arr[c].color); n   for (long long loops = 0; loops < qs; loops++) { n     for (c = 0; c <= n; c++) { n       colorBest[c] = -oops; n     } n     long long a, b; n     scanf( %I64d%I64d , &a, &b); n     long long ret = -oops; n     vector<pair<long long, int> > toptwo; n     for (c = 0; c < n; c++) { n       long long best = b * (long long)arr[c].value; n       if (colorBest[arr[c].color] != -oops) n         best = max(best, colorBest[arr[c].color] + a * (long long)arr[c].value); n       for (c2 = 0; c2 < toptwo.size(); c2++) n         if (toptwo[c2].second != arr[c].color) n           best = max(best, toptwo[c2].first + b * (long long)arr[c].value); n       colorBest[arr[c].color] = max(colorBest[arr[c].color], best); n       addToTopTwo(toptwo, best, arr[c].color); n       ret = max(ret, best); n     } n     printf( %I64d n , max(ret, 0LL)); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void adskiy_razgon() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n } n const int mxN = 1e+9 + 7; n const long long INF = 1e+18 + 7; n long long nod(long long a, long long b) { n   if (b > a) { n     swap(a, b); n   } n   while (b > 0) { n     a %= b; n     swap(a, b); n   } n   return a; n } n long long nok(long long a, long long b) { return a * b / nod(a, b); } n void sp(long long a, double b) { cout << fixed << setprecision(a) << b; } n long long binpow(long long a, long long n) { n   if (n == 0) { n     return 1; n   } n   if (n % 2 == 1) { n     return binpow(a, n - 1) * a; n   } else { n     long long b = binpow(a, n / 2); n     return b * b; n   } n } n void solve() { n   int n, m, t; n   cin >> n >> m >> t; n   cout << ((n - m) * t + m - 1) / m; n } n int main() { n   adskiy_razgon(); n   long long t = 1; n   for (int i = 1; i <= t; ++i) { n     solve(); n   } n   return 0; n } n