#include <bits/stdc++.h> n #pragma GCC optimize(2) n #pragma GCC optimize(3) n #pragma GCC optimize( Ofast ) n #pragma GCC optimize( inline ) n #pragma GCC optimize( -fgcse ) n #pragma GCC optimize( -fgcse-lm ) n #pragma GCC optimize( -fipa-sra ) n #pragma GCC optimize( -ftree-pre ) n #pragma GCC optimize( -ftree-vrp ) n #pragma GCC optimize( -fpeephole2 ) n #pragma GCC optimize( -ffast-math ) n #pragma GCC optimize( -fsched-spec ) n #pragma GCC optimize( unroll-loops ) n #pragma GCC optimize( -falign-jumps ) n #pragma GCC optimize( -falign-loops ) n #pragma GCC optimize( -falign-labels ) n #pragma GCC optimize( -fdevirtualize ) n #pragma GCC optimize( -fcaller-saves ) n #pragma GCC optimize( -fcrossjumping ) n #pragma GCC optimize( -fthread-jumps ) n #pragma GCC optimize( -funroll-loops ) n #pragma GCC optimize( -fwhole-program ) n #pragma GCC optimize( -freorder-blocks ) n #pragma GCC optimize( -fschedule-insns ) n #pragma GCC optimize( inline-functions ) n #pragma GCC optimize( -ftree-tail-merge ) n #pragma GCC optimize( -fschedule-insns2 ) n #pragma GCC optimize( -fstrict-aliasing ) n #pragma GCC optimize( -fstrict-overflow ) n #pragma GCC optimize( -falign-functions ) n #pragma GCC optimize( -fcse-skip-blocks ) n #pragma GCC optimize( -fcse-follow-jumps ) n #pragma GCC optimize( -fsched-interblock ) n #pragma GCC optimize( -fpartial-inlining ) n #pragma GCC optimize( no-stack-protector ) n #pragma GCC optimize( -freorder-functions ) n #pragma GCC optimize( -findirect-inlining ) n #pragma GCC optimize( -frerun-cse-after-loop ) n #pragma GCC optimize( inline-small-functions ) n #pragma GCC optimize( -finline-small-functions ) n #pragma GCC optimize( -ftree-switch-conversion ) n #pragma GCC optimize( -foptimize-sibling-calls ) n #pragma GCC optimize( -fexpensive-optimizations ) n #pragma GCC optimize( -funsafe-loop-optimizations ) n #pragma GCC optimize( inline-functions-called-once ) n #pragma GCC optimize( -fdelete-null-pointer-checks ) n using namespace std; n inline long long read() { n   long long x = 0, y = 0, c = getchar(); n   while (!isdigit(c)) y = c, c = getchar(); n   while (isdigit(c)) x = (x << 1) + (x << 3) + (c ^  0 ), c = getchar(); n   return y ==  -  ? -x : x; n } n inline void print(long long q) { n   if (q < 0) putchar( - ), q = -q; n   if (q >= 10) print(q / 10); n   putchar(q % 10 +  0 ); n } n long long s1 = 19260817, s2 = 23333333, s3 = 998244353, srd; n inline long long rd() { return srd = (srd * s1 + s2 + rand()) % s3; } n void file() { n   freopen( lis.in ,  r , stdin); n   freopen( lis.out ,  w , stdout); n } n int n, m; n set<int> s; n int t[2][800010]; n void ins(int op, int q, int l, int r, int ax, int x) { n   if (l == r) { n     t[op][q] = x; n     return; n   } n   int mid = l + r >> 1; n   if (mid >= ax) n     ins(op, q << 1, l, mid, ax, x); n   else n     ins(op, q << 1 | 1, mid + 1, r, ax, x); n   t[op][q] = max(t[op][q << 1], t[op][q << 1 | 1]); n } n int qy(int op, int q, int l, int r, int al, int ar) { n   if (l >= al && r <= ar) return t[op][q]; n   int mid = l + r >> 1; n   int as = 0; n   if (mid >= al) as = qy(op, q << 1, l, mid, al, ar); n   if (mid < ar) as = max(as, qy(op, q << 1 | 1, mid + 1, r, al, ar)); n   return as; n } n int gx(int op, int q) { n   int tmp = qy(op, 1, 0, (!op ? n : m + 10), 0, q - 1) + 1; n   ins(op, 1, 0, (!op ? n : m + 10), q, tmp); n   return tmp; n } n int gd[200010], wz[200010]; n int main() { n   srand(time(0)); n   rd(); n   int i, j, op, x, y; n   n = read(); n   m = read(); n   for (i = 1; i <= m; ++i) { n     op = read(); n     x = read(); n     if (op == 1) { n       y = read(); n       x = n - x + 1; n       y = i - y + 10; n       gd[x] = y; n       wz[y] = x; n       s.insert(x); n       for (j = y + 1; j <= y + 9; ++j) n         if (wz[j]) ins(0, 1, 0, n, wz[j], 0); n       for (j = y; j <= y + 9; ++j) n         if (wz[j]) { n           int nxt = gx(0, wz[j]); n           ins(1, 1, 0, m + 10, j, nxt); n         } n     } else { n       set<int>::iterator it = s.end(), tp; n       while (x--) { n         --it; n         ins(1, 1, 0, m + 10, gd[*it], 0); n       } n       tp = it; n       ins(0, 1, 0, n, *it, 0); n       wz[gd[*it]] = 0; n       gd[*it] = 0; n       for (++it; it != s.end(); ++it) { n         int nxt = gx(1, gd[*it]); n         ins(0, 1, 0, n, *it, nxt); n       } n       s.erase(tp); n     } n     print(t[0][1]); n     putchar(  n ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long n, m, f[10] = {0}, x, i; n   long long ans, z; n   scanf( %lld , &n); n   scanf( %lld , &m); n   for (i = 0; i < n; i++) { n     scanf( %lld , &x); n     f[x - 1]++; n   } n   ans = (n * (n - 1)) / 2; n   for (i = 0; i < m; i++) { n     z = (f[i] * (f[i] - 1)) / 2; n     ans -= z; n   } n   cout << ans; n } n


#include <bits/stdc++.h> n using namespace std; n bool IsPrime(int n) { n   if (n == 1) return false; n   for (int d = 2; d * d <= n; d++) { n     if (n % d == 0) return false; n   } n   return true; n } n int main() { n   int n; n   cin >> n; n   int count = 0; n   while (n > 0) { n     int j = 0; n     int k = n; n     int amx = INT_MIN; n     while (k > 0) { n       j = k % 10; n       k = k / 10; n       amx = max(amx, j); n     } n     n -= amx; n     count++; n   } n   cout << count << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MOD = 998244353, N = 1e6 + 5; n long long fact[N], d[N]; n long long f(int x) { n   if (x == 0) return 1; n   if (fact[x] != 0) return fact[x]; n   return fact[x] = (x * f(x - 1)) % MOD; n } n int main() { n   d[1] = 1; n   for (int i = 2; i < N; i++) { n     d[i] = (1ll * i * (f(i - 1) - 1 + d[i - 1])) % MOD; n   } n   int n; n   cin >> n; n   cout << d[n]; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T> n void chkmin(T &a, T b) { n   a = min(a, b); n } n template <typename T> n void chkmax(T &a, T b) { n   a = max(a, b); n } n inline int fread(void) { n   int ret = 0, op = 1; n   char ch = getchar(); n   for (; !isdigit(ch); ch = getchar()) n     if (ch ==  - ) op = -1; n   for (; isdigit(ch); ch = getchar()) ret = (ret << 1) + (ret << 3) -  0  + ch; n   return ret * op; n } n const int MAXN = 2e5 + 10; n const int MAXM = +10; n int n, m, a[MAXN], c[MAXN]; n struct rec { n   int t, r; n   bool operator<(const rec &b) const { return r < b.r; } n } s[MAXN]; n int top; n inline void init(void) { n   n = fread(), m = fread(); n   for (int i = 1; i <= n; ++i) a[i] = fread(), c[i] = a[i]; n } n int lt, rt; n inline void work(void) { n   rec now; n   top = 0; n   for (int i = 1; i <= m; ++i) { n     now.t = fread(), now.r = fread(); n     while (top && s[top] < now) --top; n     s[++top] = now; n   } n   sort(c + 1, c + s[1].r + 1); n   lt = 1; n   rt = s[1].r; n   ++top; n   s[top].r = 0; n   for (int i = 2; i <= top; ++i) { n     if (s[i - 1].t == 1) { n       for (int j = s[i - 1].r; j > s[i].r; --j) a[j] = c[rt--]; n     } else { n       for (int j = s[i - 1].r; j > s[i].r; --j) a[j] = c[lt++]; n     } n   } n   for (int i = 1; i <= n; ++i) printf( %d  , a[i]); n } n int main() { n   init(); n   work(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n bool cmp(const pair<long long int, long long int> &a, n          const pair<long long int, long long int> &b) { n   return a.second < b.second; n } n int main() { n   long long int n, k, ai, bi, total = 0, ans = 0; n   vector<long long int> a; n   vector<pair<long long int, long long int> > ab; n   map<long long int, long long int> jobs; n   scanf( %lld %lld , &n, &k); n   for (int i = 0; i < n; i++) { n     scanf( %lld , &ai), a.push_back(ai), jobs[ai]++; n     if (jobs[ai] == 1) total++; n   } n   for (int i = 0; i < n; i++) n     scanf( %lld , &bi), ab.push_back(make_pair(a[i], bi)); n   sort(ab.begin(), ab.end(), cmp); n   total = k - total; n   for (int i = 0; i < n; i++) { n     if (jobs[ab[i].first] > 1 && total > 0) { n       ans += ab[i].second; n       total--; n       jobs[ab[i].first]--; n     } n   } n   printf( %lld , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void solve() { n   int n; n   scanf( %d n , &n); n   string a, b; n   getline(cin, a); n   getline(cin, b); n   int q(0); n   map<pair<char, char>, int> SET; n   map<char, int> have; n   for (int i(0), _l((int)(n)-1); i <= _l; ++i) { n     if (a[i] != b[i]) { n       ++q; n       SET[make_pair(a[i], b[i])] = i; n       have[a[i]] = i; n     } n   } n   int delta(0); n   pair<int, int> ans(-2, -2); n   for (int i(0), _l((int)(n)-1); i <= _l; ++i) { n     if (a[i] != b[i]) { n       if (have.count(b[i]) && delta == 0) n         delta = 1, ans = make_pair(i, have[b[i]]); n       if (SET.count(make_pair(b[i], a[i]))) n         delta = 2, ans = make_pair(i, SET[make_pair(b[i], a[i])]); n     } n   } n   cout << q - delta << endl; n   cout << ans.first + 1 <<     << ans.second + 1 << endl; n } n int main() { n   solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int a[200005], b[200005]; n int nx[500005]; n int n, m; n void getnext() { n   nx[0] = -1; n   int j = 0, k = -1; n   while (j < m) { n     if (k == -1 || b[j] == b[k]) n       nx[++j] = ++k; n     else n       k = nx[k]; n   } n } n int KMP() { n   int ret = 0; n   int i = 0, j = 0; n   for (i = 0; i < n; i++) { n     while (j > 0 && a[i] != b[j]) j = nx[j]; n     if (a[i] == b[j]) j++; n     if (j == m) { n       ret++; n       j = nx[j]; n     } n   } n   return ret; n } n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   cin >> n >> m; n   for (int i = 0; i < n; i++) { n     cin >> a[i]; n     if (i != 0) a[i - 1] = a[i] - a[i - 1]; n   } n   for (int i = 0; i < m; i++) { n     cin >> b[i]; n     if (i != 0) b[i - 1] = b[i] - b[i - 1]; n   } n   if (m == 1) { n     cout << n << endl; n     return 0; n   } n   n--; n   m--; n   getnext(); n   cout << KMP() << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 1010; n int N; n long long c[4][maxn][maxn]; n void upd(int p, int i, int j, long long x) { n   int jj = j; n   while (i <= N) { n     j = jj; n     while (j <= N) c[p][i][j] ^= x, j += j & (-j); n     i += i & (-i); n   } n } n long long get(int p, int i, int j) { n   int jj = j; n   long long ans = 0; n   while (i) { n     j = jj; n     while (j) ans ^= c[p][i][j], j -= j & (-j); n     i -= i & (-i); n   } n   return ans; n } n void upd(int i, int j, long long x) { n   upd(0, i, j, x); n   upd(1, i, j, x * (i % 2)); n   upd(2, i, j, x * (j % 2)); n   upd(3, i, j, x * (i % 2) * (j % 2)); n } n long long get(int i, int j) { n   long long s0 = get(0, i, j); n   long long s1 = get(1, i, j); n   long long s2 = get(2, i, j); n   long long s3 = get(3, i, j); n   int x = i, y = j; n   x++, y++; n   x %= 2, y %= 2; n   return (x * y * s0) ^ (y * s1) ^ (x * s2) ^ (s3); n } n int main() { n   int n, m, i, j, k; n   int x0, x1, y0, y1; n   scanf( %d%d , &n, &m); n   N = n; n   while (m--) { n     scanf( %d , &i); n     if (i == 1) { n       scanf( %d%d%d%d , &x0, &y0, &x1, &y1); n       printf( %I64d n , get(x1, y1) ^ get(x0 - 1, y1) ^ get(x1, y0 - 1) ^ n                             get(x0 - 1, y0 - 1)); n     } else { n       scanf( %d%d%d%d , &x0, &y0, &x1, &y1); n       long long v; n       scanf( %I64d , &v); n       upd(x0, y0, v); n       upd(x0, y1 + 1, v); n       upd(x1 + 1, y0, v); n       upd(x1 + 1, y1 + 1, v); n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   cin >> n; n   vector<long long int> v(n); n   for (int i = 0; i < n; i++) { n     cin >> v[i]; n   } n   int i; n   for (int i = 0; i < n; i++) { n     while (v[i] % 2 == 0) { n       v[i] /= 2; n     } n     while (v[i] % 3 == 0) { n       v[i] /= 3; n     } n   } n   cout << endl; n   vector<long long int>::iterator it = min_element(v.begin(), v.end()); n   vector<long long int>::iterator it2 = max_element(v.begin(), v.end()); n   if (*it == *it2) n     cout <<  Yes ; n   else n     cout <<  No ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long max(long long a, long long b) { n   if (a < b) return b; n   return a; n } n long long min(long long a, long long b) { n   if (a < b) return a; n   return b; n } n bool okay(vector<long long> &v, long long d, long long k) { n   long long sum = 0; n   priority_queue<long long, vector<long long>, greater<long long>> pq; n   for (int i = 0; i < d; i++) pq.push(0); n   for (auto x : v) { n     long long ind = pq.top(); n     pq.pop(); n     sum += max(0, x - ind); n     pq.push(ind + 1); n   } n   return sum >= k; n } n void solve() { n   long long n, m; n   cin >> n >> m; n   vector<long long> v(n); n   for (int i = 0; i < n; i++) cin >> v[i]; n   sort(v.rbegin(), v.rend()); n   long long low = 1, high = n, ans = -1; n   while (low <= high) { n     long long mid = low + (high - low) / 2; n     if (okay(v, mid, m)) { n       ans = mid; n       high = mid - 1; n     } else n       low = mid + 1; n   } n   cout << ans; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n namespace GR1 { n vector<int> solve(int N, int num_exact_matches, int num_common, n                   const vector<int>& B) { n   vector<queue<int> > pos(N + 2); n   for (int i = 0, _n = (N); i < _n; ++i) { n     int b = B[i]; n     pos[b].push(i); n   } n   int missing = 0; n   set<pair<int, int>, greater<pair<int, int> > > S; n   for (int v = (1), _b = (N + 1); v <= _b; ++v) n     if (!pos[v].empty()) n       S.emplace(int((pos[v]).size()), v); n     else n       missing = v; n   assert(missing > 0); n   vector<int> res(N); n   vector<bool> exact(N); n   for (int t = 0, _n = (num_exact_matches); t < _n; ++t) { n     auto it = S.begin(); n     int v = it->second; n     S.erase(it); n     queue<int>& q = pos[v]; n     int i = q.front(); n     q.pop(); n     res[i] = v; n     exact[i] = true; n     if (!q.empty()) S.emplace(int((q).size()), v); n   } n   int num_mismatches = N - num_common; n   int max_freq = S.begin()->first; n   if (max_freq * 2 > N - num_exact_matches + num_mismatches) return {}; n   vector<int> block; n   for (auto it : S) { n     int v = it.second; n     while (!pos[v].empty()) { n       int j = pos[v].front(); n       pos[v].pop(); n       block.push_back(j); n     } n   } n   for (int t = 0, _n = ((N - num_exact_matches) / 2); t < _n; ++t) n     block.push_back(block[t]); n   for (int k = 0, _n = (N - num_exact_matches); k < _n; ++k) { n     int i = block[k]; n     int j = block[k + (N - num_exact_matches) / 2]; n     res[i] = B[j]; n   }; n   ; n   for (int i = 0, _n = (N); i < _n; ++i) { n     if (exact[i]) continue; n     if (res[i] == B[i]) { n       assert(num_mismatches > 0); n       --num_mismatches; n       res[i] = missing; n     } n   }; n   ; n   for (int i = 0; i < N && num_mismatches > 0; ++i) { n     if (exact[i] || res[i] == missing) continue; n     --num_mismatches; n     res[i] = missing; n   } n   assert(num_mismatches == 0); n   return res; n } n }  // namespace GR1 n namespace GR2 { n vector<int> solve(int N, int num_exact_matches, int num_common, n                   const vector<int>& B) { n   vector<queue<int> > pos(N + 2); n   for (int i = 0, _n = (N); i < _n; ++i) { n     int b = B[i]; n     pos[b].push(i); n   } n   int missing = 0; n   set<pair<int, int>, greater<pair<int, int> > > S; n   for (int v = (1), _b = (N + 1); v <= _b; ++v) n     if (!pos[v].empty()) n       S.emplace(int((pos[v]).size()), v); n     else n       missing = v; n   assert(missing > 0); n   vector<int> res(N); n   for (int t = 0, _n = (num_exact_matches); t < _n; ++t) { n     auto it = S.begin(); n     int v = it->second; n     S.erase(it); n     queue<int>& q = pos[v]; n     int i = q.front(); n     q.pop(); n     res[i] = v; n     if (!q.empty()) S.emplace(int((q).size()), v); n   } n   int num_mismatches = N - num_common; n   num_common -= num_exact_matches; n   while (num_common > 0) { n     assert(!S.empty()); n     auto max_it = S.begin(); n     int v = max_it->second; n     S.erase(max_it); n     assert(!pos[v].empty()); n     int i = pos[v].front(); n     pos[v].pop(); n     if (B[i] == v) { n       if (int((S).size()) > 0) { n         auto max2_it = S.begin(); n         int w = max2_it->second; n         S.erase(max2_it); n         assert(!pos[w].empty()); n         int j = pos[w].front(); n         pos[w].pop(); n         if (!pos[w].empty()) S.emplace(int((pos[w]).size()), w); n         res[i] = w; n         --num_common; n         if (num_common > 0) { n           res[j] = v; n           --num_common; n         } n       } else if (num_mismatches > 0) { n         res[i] = missing; n         --num_mismatches; n       } else n         return {}; n     } else { n       res[i] = v; n       --num_common; n     } n     if (!pos[v].empty()) S.emplace(int((pos[v]).size()), v); n   } n   for (int i = 0, _n = (N); i < _n; ++i) { n     if (res[i] == 0) { n       assert(num_mismatches > 0); n       --num_mismatches; n       res[i] = missing; n     } n   } n   return res; n } n }  // namespace GR2 n int main(int argc, char* argv[]) { n   ios_base::sync_with_stdio(false); n   cin.tie(nullptr); n   int TC; n   cin >> TC; n   for (int tc = (1), _b = (TC); tc <= _b; ++tc) { n     int N, x, y; n     cin >> N >> x >> y; n     vector<int> B(N); n     for (int i = 0, _n = (N); i < _n; ++i) cin >> B[i]; n     vector<int> res = GR1::solve(N, x, y, B); n     if (res.empty()) n       cout <<  NO n ; n     else { n       cout <<  YES n ; n       for (int a : res) cout << a <<    ; n       cout <<   n ; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int N; n bool v[2001]; n vector<int> G[2001]; n int cnt = 0; n int a[2001][2001]; n int Dfn[2001], Low[2001]; n int Dindex = 0; n int Bcnt; n bool instack[2001]; n int Sta[2001]; n int top = -1; n void add(int x, int y) { G[x].push_back(y); } n void read() { n   scanf( %d , &N); n   for (int i = 0; i < N; ++i) n     for (int j = 0; j < N; ++j) { n       scanf( %d , &a[i][j]); n     } n   for (int i = 0; i < N; ++i) n     for (int j = 0; j < N; ++j) n       if (a[i][j] && i != j) add(i, j); n } n void tarjan(int v) { n   Dfn[v] = Low[v] = ++Dindex; n   instack[v] = 1; n   Sta[++top] = v; n   for (int i = 0; i < G[v].size(); ++i) { n     int w = G[v][i]; n     if (!Dfn[w]) { n       tarjan(w); n       Low[v] = min(Low[v], Low[w]); n     } else if (instack[w]) n       Low[v] = min(Low[v], Dfn[w]); n   } n   int j; n   if (Dfn[v] == Low[v]) { n     Bcnt++; n     do { n       j = Sta[top--]; n       instack[j] = 0; n     } while (j != v); n   } n } n int main() { n   read(); n   Bcnt = 0; n   for (int i = 0; i < N; ++i) n     if (!Dfn[i]) tarjan(i); n   if (Bcnt > 1) n     puts( NO ); n   else n     puts( YES ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 4e6 + 10; n int n, m; n int a[N]; n int find(int x) { n   for (int i = 1; i <= n; i++) { n     if (a[i] == x) return i; n   } n } n vector<int> v; n signed main() { n   int x, y, z, T; n   cin >> T; n   while (T--) { n     scanf( %d , &n); n     for (int i = 1; i <= n; i++) scanf( %d , &a[i]); n     bool f = 0; n     for (int i = 1; i <= n; i += 2) n       if (a[i] % 2 == 0) f = 1; n     if (f) { n       puts( -1 ); n       continue; n     } n     v.clear(); n     for (int i = n; i >= 1; i -= 2) { n       if (i == 1) continue; n       int l = find(i), r = find(i - 1); n       if (l == i && r == i - 1) continue; n       if (l == 1 && r == 2) { n         v.push_back(i); n         reverse(a + 1, a + 1 + i); n         continue; n       } n       v.push_back(l); n       reverse(a + 1, a + 1 + l); n       l = 1, r = find(i - 1); n       if (l == 1 && r == 2) { n         v.push_back(i); n         reverse(a + 1, a + 1 + i); n         continue; n       } n       v.push_back(r - 1); n       reverse(a + 1, a + 1 + r - 1); n       v.push_back(r + 1); n       reverse(a + 1, a + 1 + r + 1); n       v.push_back(3); n       reverse(a + 1, a + 1 + 3); n       v.push_back(i); n       reverse(a + 1, a + 1 + i); n     } n     cout << v.size() << endl; n     for (int i : v) cout << i <<    ; n     cout << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, t, c, d[555555], res = 0, ll = -1; n bool try_ans(int k) { n   if (d[n - 1] > k) return false; n   int last = 0, now = 0, res, l2 = -1; n   vector<pair<int, int> > aa; n   for (int i = 0; i < n; i++) { n     if (d[i] > k) { n       l2 = i; n       if (!last) aa.push_back(make_pair(i, now)); n       last = 1; n       now += 2; n     } else if (last == 1) n       last = 2, l2 = i; n     else n       last = 0; n   } n   if (ll > l2) l2 = ll; n   res = now + l2 + 1; n   for (int i = aa.size() - 1; i >= 0; i--) n     if (2 * l2 + 1 - aa[i].first + aa[i].second < res) n       res = 2 * l2 + 1 - aa[i].first + aa[i].second; n   return res <= t; n } n int ans(int max, int min) { n   if (max == min) return min; n   int mid = (max + min) / 2; n   bool a = try_ans(mid); n   if (a) n     return ans(mid, min); n   else n     return ans(max, mid + 1); n } n int main() { n   cin >> n >> t; n   string str; n   cin >> str; n   int now = 0; n   for (int i = 0; i < n; i++) { n     c = str[i]; n     if (c ==  H ) now++, ll = i; n     if (c ==  S ) now--; n     d[i] = now; n     if (now > res) res = now; n   } n   if (ll + 1 > t) n     cout << -1; n   else { n     cout << ans(res, 0); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T> n void read(T& x) { n   x = 0; n   int fl = 1; n   char ch = getchar(); n   while (ch <  0  || ch >  9 ) { n     if (ch ==  - ) fl = -1; n     ch = getchar(); n   } n   while (ch >=  0  && ch <=  9 ) { n     x = (x << 1) + (x << 3) + ch -  0 ; n     ch = getchar(); n   } n   x *= fl; n } n template <typename T, typename... Args> n inline void read(T& t, Args&... args) { n   read(t); n   read(args...); n } n const int N = 3005; n int T, n, a[N], b[N]; n int main() { n   for (read(T); T; T--) { n     read(n); n     for (int i = 1; i <= n; i++) read(a[i]), b[i] = a[i]; n     sort(b + 1, b + n + 1); n     int len = unique(b + 1, b + n + 1) - (b + 1); n     for (int i = 1; i <= n; i++) a[i] = lower_bound(b + 1, b + n + 1, a[i]) - b; n     int now = 1, mx = 0; n     while (now <= n) { n       int len = 0; n       for (int i = 1; i <= n; i++) n         if (a[i] == now) len++, now++; n       mx = max(mx, len); n     } n     printf( %d n , n - mx); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int mod = 1e9 + 7; n const int MN = 200010; n int exp(int x, int n) { n   int ret = 1; n   while (n) { n     if (n & 1) ret = 1LL * ret * x % mod; n     x = 1LL * x * x % mod; n     n >>= 1; n   } n   return ret; n } n int inv(int x) { return exp(x, mod - 2); } n int fact[MN], invf[MN]; n int comb(int n, int k) { n   if (n < k) return 0; n   return 1LL * fact[n] * invf[k] % mod * invf[n - k] % mod; n } n int S(int n, int k) { n   int ret = 0; n   for (int i = 0; i < k; i++) { n     ret += 1LL * comb(k, i) * (i % 2 ? mod - 1 : 1) % mod * exp(k - i, n) % mod; n     ret %= mod; n   } n   ret = 1LL * ret * invf[k] % mod; n   return ret; n } n int N, K; n int W[MN]; n int main() { n   fact[0] = 1; n   for (int i = 1; i < MN; i++) { n     fact[i] = 1LL * fact[i - 1] * i % mod; n   } n   for (int i = 0; i < MN; i++) { n     invf[i] = inv(fact[i]); n   } n   scanf( %d %d , &N, &K); n   int sum = 0; n   for (int i = 0; i < N; i++) { n     scanf( %d , &W[i]); n     sum += W[i]; n     sum %= mod; n   } n   int ans = n       1LL * ((S(N, K) + 1LL * (N - 1) * S(N - 1, K) % mod) % mod) * sum % mod; n   printf( %d , ans); n } n


#include <bits/stdc++.h> n using namespace std; n const int MOD = 1e9 + 7; n const int MAXN = 1e5 + 10; n bool bd[MAXN]; n int n; n int a[MAXN]; n vector<pair<int, int>> sol; n pair<int, int> coord(pair<int, int> p) { return {(n - 1 - p.second), p.first}; } n int main(void) { n   scanf( %d , &n); n   for (int i = 0; i < n; ++i) scanf( %d , &a[i]); n   for (int i = 0; i < n; ++i) bd[i] = false; n   int lst = -1; n   queue<int> dva; n   for (int i = n - 1; i >= 0; --i) { n     if (a[i] == 0) continue; n     while (!dva.empty() && bd[dva.front()]) dva.pop(); n     if ((a[i] == 2 && dva.empty()) || (a[i] == 3 && lst == -1)) { n       printf( -1 n ); n       return 0; n     } n     if (a[i] == 1) { n       sol.emplace_back(i, (n - 1) - i); n       lst = i; n       dva.push(i); n       continue; n     } n     if (a[i] == 2) { n       int xx = dva.front(); n       dva.pop(); n       bd[i] = bd[xx] = true; n       sol.emplace_back(i, (n - 1) - xx); n       lst = i; n     } n     if (a[i] == 3) { n       sol.emplace_back(i, (n - 1) - i); n       sol.emplace_back(lst, (n - 1) - i); n       bd[i] = bd[lst] = true; n       lst = i; n     } n   } n   printf( %d n , (int)sol.size()); n   for (auto &p : sol) { n     auto c = coord(p); n     printf( %d %d n , c.first + 1, c.second + 1); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n string moves[] = { MW ,  LW ,  LM }; n int sequence[25]; n void listify(int k, int n) { n   for (int i = 0; i != n; ++i) { n     sequence[i] = k % 3; n     k /= 3; n   } n } n void solve(int n, vector<vector<int> >& V) { n   int n1 = n / 2; n   int n2 = n - n1; n   int k1 = pow(3, n1), k2 = pow(3, n2); n   vector<int> state(3); n   map<pair<int, int>, pair<int, int> > sums; n   for (int k = 0; k != k1; ++k) { n     listify(k, n1); n     state[0] = state[1] = state[2] = 0; n     for (int i = 0; i != n1; ++i) { n       state[(sequence[i] + 1) % 3] += V[i][(sequence[i] + 1) % 3]; n       state[(sequence[i] + 2) % 3] += V[i][(sequence[i] + 2) % 3]; n     } n     pair<int, int> p(state[1] - state[0], state[2] - state[0]); n     if (sums.find(p) == sums.end() || sums[p].first < state[0]) { n       sums[p] = make_pair(state[0], k); n     } n   } n   int max_equal_rel = -1000000000; n   pair<int, int> argmax; n   for (int k = 0; k != k2; ++k) { n     listify(k, n2); n     state[0] = state[1] = state[2] = 0; n     for (int i = 0; i != n2; ++i) { n       state[(sequence[i] + 1) % 3] += V[i + n1][(sequence[i] + 1) % 3]; n       state[(sequence[i] + 2) % 3] += V[i + n1][(sequence[i] + 2) % 3]; n     } n     pair<int, int> p(state[0] - state[1], state[0] - state[2]); n     if (sums.find(p) != sums.end() && n         sums[p].first + state[0] > max_equal_rel) { n       max_equal_rel = sums[p].first + state[0]; n       argmax = make_pair(sums[p].second, k); n     } n   } n   if (max_equal_rel == -1000000000) { n     cout <<  Impossible  << endl; n   } else { n     listify(argmax.first, n1); n     for (int i = 0; i != n1; ++i) { n       cout << moves[sequence[i]] << endl; n     } n     listify(argmax.second, n2); n     for (int i = 0; i != n2; ++i) { n       cout << moves[sequence[i]] << endl; n     } n   } n } n int main() { n   int n; n   cin >> n; n   vector<vector<int> > V(n, vector<int>(3)); n   for (int i = 0; i != n; ++i) { n     cin >> V[i][0] >> V[i][1] >> V[i][2]; n   } n   solve(n, V); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long powmod(long long a, long long b) { n   long long res = 1; n   a %= 1000000007; n   for (; b; b >>= 1) { n     if (b & 1) res = res * a % 1000000007; n     a = a * a % 1000000007; n   } n   return res; n } n template <typename T> n inline bool chkmin(T &a, const T &b) { n   return a > b ? a = b, 1 : 0; n } n template <typename T> n inline bool chkmax(T &a, const T &b) { n   return a < b ? a = b, 1 : 0; n } n int read() { n   int x = 0, f = 1; n   char ch = getchar(); n   while (ch <  0  || ch >  9 ) { n     if (ch ==  - ) f = -1; n     ch = getchar(); n   } n   while (ch >=  0  && ch <=  9 ) { n     x = x * 10 + ch -  0 ; n     ch = getchar(); n   } n   return x * f; n } n const int P = 10000007; n unsigned int p[500100], l[500100], r[500100]; n int m; n char ans[500100], s[500100]; n int n; n bool ok(int i, int x) { n   if (++i < x) return 0; n   return ((r[i] - r[i - x] * p[x]) * p[i - x]) == (l[i] - l[i - x]); n } n bool dfs(int i, int x) { n   if (i == n) { n     puts(ans); n     return 1; n   } n   ans[i] = x ? s[i] :  a ; n   for (; ans[i] <=  z ; ans[i]++) { n     l[i + 1] = l[i] + ans[i] * p[i]; n     r[i + 1] = r[i] * P + ans[i]; n     if (!ok(i, m) && !ok(i, m + 1) && dfs(i + 1, x && (ans[i] == s[i]))) n       return 1; n   } n   return 0; n } n int main() { n   m = read(); n   scanf( %s , s); n   n = strlen(s); n   int i; n   for (i = n - 1; i >= 0 && s[i] ==  z ; i--) s[i] =  a ; n   if (i == -1) { n     puts( Impossible ); n     return 0; n   } n   s[i]++; n   p[0] = 1; n   for (int i = 1; i < 500100; i++) p[i] = p[i - 1] * P; n   if (!dfs(0, 1)) puts( Impossible ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXS = 200005; n const int MAXN = 100005; n const int B = 75; n template <typename T> n inline void read(T &AKNOI) { n   T x = 0, flag = 1; n   char ch = getchar(); n   while (!isdigit(ch)) { n     if (ch ==  - ) flag = -1; n     ch = getchar(); n   } n   while (isdigit(ch)) { n     x = x * 10 + ch -  0 ; n     ch = getchar(); n   } n   AKNOI = flag * x; n } n template <typename T> n inline void cmin(T &x, T y) { n   if (x > y) x = y; n } n template <typename T> n inline void cmax(T &x, T y) { n   if (x < y) x = y; n } n char ss[MAXS], tt[MAXS]; n int lg2[MAXS]; n struct SuffixArray { n   int sa[MAXS], x[MAXS], y[MAXS], cnt[MAXS]; n   int rnk[MAXS], hei[MAXS], st[MAXS][20]; n   inline bool cmp(int i, int j, int k) { n     return (y[i] == y[j] && y[i + k] == y[j + k]); n   } n   void build(int n) { n     int m = 256; n     for (int i = 1; i <= n; ++i) { n       ++cnt[x[i] = ss[i]]; n     } n     for (int i = 1; i <= m; ++i) { n       cnt[i] += cnt[i - 1]; n     } n     for (int i = n; i >= 1; --i) { n       sa[cnt[x[i]]--] = i; n     } n     for (int k = 1; k <= n; k <<= 1) { n       int p = 0; n       for (int i = n - k + 1; i <= n; ++i) { n         y[++p] = i; n       } n       for (int i = 1; i <= n; ++i) { n         if (sa[i] > k) { n           y[++p] = sa[i] - k; n         } n       } n       for (int i = 0; i <= m; ++i) { n         cnt[i] = 0; n       } n       for (int i = 1; i <= n; ++i) { n         ++cnt[x[y[i]]]; n       } n       for (int i = 1; i <= m; ++i) { n         cnt[i] += cnt[i - 1]; n       } n       for (int i = n; i >= 1; --i) { n         sa[cnt[x[y[i]]]--] = y[i]; n       } n       swap(x, y); n       x[sa[1]] = p = 1; n       for (int i = 2; i <= n; ++i) { n         x[sa[i]] = (cmp(sa[i], sa[i - 1], k) ? p : ++p); n       } n       if (p >= n) break; n       m = p; n     } n     for (int i = 1; i <= n; ++i) { n       rnk[sa[i]] = i; n     } n     for (int i = 1, j = 0; i <= n; ++i) { n       if (j) --j; n       while (ss[i + j] == ss[sa[rnk[i] - 1] + j]) ++j; n       hei[rnk[i]] = j; n     } n     lg2[1] = 0; n     for (int i = 2; i <= n; ++i) { n       lg2[i] = lg2[i >> 1] + 1; n     } n     for (int i = n; i >= 1; --i) { n       st[i][0] = hei[i]; n       for (int j = 1; i + (1 << j) - 1 <= n; ++j) { n         st[i][j] = min(st[i][j - 1], st[i + (1 << (j - 1))][j - 1]); n       } n     } n   } n   inline int LCP(int x, int y) { n     if (x == y) return MAXS; n     int l = rnk[x], r = rnk[y]; n     if (l > r) swap(l, r); n     int k = lg2[r - l]; n     return min(st[l + 1][k], st[r - (1 << k) + 1][k]); n   } n   inline int sgn(int x, int y, int len) { n     if (LCP(x, y) >= len) return 0; n     return (rnk[x] < rnk[y]) ? -1 : 1; n   } n } SA; n int n, m, Q, sor[MAXN], rnk[MAXN], ans[MAXN]; n inline bool cmp(int x, int y) { n   static int t; n   bool swp = (x > y); n   if (swp) swap(x, y); n   if (x + m <= y) { n     t = SA.sgn(n + 2, x + 1, m); n     if (t) return (t < 0) ^ swp; n     t = SA.sgn(x + 1, x + m + 1, y - x - m); n     if (t) return (t < 0) ^ swp; n     t = SA.sgn(y - m + 1, n + 2, m); n     if (t) return (t < 0) ^ swp; n   } else { n     t = SA.sgn(n + 2, x + 1, y - x); n     if (t) return (t < 0) ^ swp; n     t = SA.sgn(n + 2 + y - x, n + 2, x + m - y); n     if (t) return (t < 0) ^ swp; n     t = SA.sgn(x + 1, n + 2 + x + m - y, y - x); n     if (t) return (t < 0) ^ swp; n   } n   return swp ^ 1; n } n struct Query { n   int l, r, x, y, id; n } tmp; n vector<Query> q[80]; n struct SparseTable { n   int st[MAXN][20]; n   void build(int n) { n     for (int i = n; i >= 0; --i) { n       for (int j = 1; i + (1 << j) - 1 <= n; ++j) { n         st[i][j] = min(st[i][j - 1], st[i + (1 << (j - 1))][j - 1]); n       } n     } n   } n   int query(int l, int r) { n     int k = lg2[r - l + 1]; n     return min(st[l][k], st[r - (1 << k) + 1][k]); n   } n } ST; n void Work1(Query p, int k) { n   for (int x = p.x, y = p.y; x <= n; x += k, y += k) { n     int l = max(p.l, x), r = min(p.r, y); n     if (l > r) continue; n     cmin(ans[p.id], ST.query(l, r)); n   } n } n void Work2(int k) { n   for (int x = 0; x < k; ++x) { n     int cnt = 0; n     for (int i = x; i <= n; i += k) { n       ST.st[cnt++][0] = rnk[i]; n     } n     ST.build(cnt - 1); n     for (auto p : q[k]) { n       if (x < p.x || x > p.y) continue; n       int l = p.l / k + (p.l % k > x); n       int r = p.r / k - (p.r % k < x); n       if (l > r) continue; n       cmin(ans[p.id], ST.query(l, r)); n     } n   } n } n void init() { n   scanf( %s , ss + 1); n   n = strlen(ss + 1); n   scanf( %s , tt + 1); n   m = strlen(tt + 1); n   ss[n + 1] =  # ; n   for (int i = 1; i <= m; ++i) { n     ss[n + 1 + i] = tt[i]; n   } n   SA.build(n + 1 + m); n   for (int i = 0; i <= n; ++i) { n     sor[i] = i; n   } n   sort(sor, sor + n + 1, cmp); n   for (int i = 0; i <= n; ++i) { n     rnk[sor[i]] = i; n   } n } n void solve() { n   for (int i = 0; i <= n; ++i) { n     ST.st[i][0] = rnk[i]; n   } n   ST.build(n); n   read(Q); n   memset(ans, 0x3f, sizeof(ans)); n   for (int i = 1, k; i <= Q; ++i) { n     read(tmp.l); n     read(tmp.r); n     read(k); n     read(tmp.x); n     read(tmp.y); n     tmp.id = i; n     if (k > B) { n       Work1(tmp, k); n     } else { n       q[k].push_back(tmp); n     } n   } n   for (int i = 1; i <= B; ++i) { n     Work2(i); n   } n   for (int i = 1; i <= Q; ++i) { n     printf( %d  , (ans[i] <= n) ? sor[ans[i]] : -1); n   } n } n int main() { n   init(); n   solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int morh[100001], parent[100001], pos[100001], temp = 0, ans = 0; n int calculate(int i) { n   int posi = i, ans = 0; n   while (posi != 0) { n     if (pos[posi] > 1) n       break; n     else { n       posi = parent[posi]; n       ans++; n     } n   } n   return ans; n } n int main() { n   int n; n   scanf( %d , &n); n   for (int i = 1; i <= n; i++) { n     scanf( %d , &morh[i]); n   } n   for (int i = 1; i <= n; i++) { n     scanf( %d , &parent[i]); n     pos[parent[i]]++; n   } n   int temp2; n   for (int i = 1; i <= n; i++) { n     if (morh[i] == 1) { n       temp2 = calculate(i); n       if (temp2 > ans) { n         ans = temp2; n         temp = i; n       } n     } n   } n   temp2 = ans; n   int give_ans[ans], i = 0; n   for (int j = 0; j < temp2; j++) { n     give_ans[i] = temp; n     i++; n     temp = parent[temp]; n   } n   printf( %d n , ans); n   for (int i = ans - 1; i >= 0; i--) printf( %d  , give_ans[i]); n   printf(  n ); n } n


#include <bits/stdc++.h> n using namespace std; n struct three { n   int f, s, w; n }; n map<pair<int, int>, bool> mp; n int parent[200005], pat[200005]; n three A[200005]; n int time_in[200005], time_out[200005]; n vector<vector<int> > dp(200005); n vector<vector<int> > dp_mx(200005); n vector<vector<pair<int, int> > > g; n vector<vector<pair<int, int> > > tree; n set<pair<int, pair<int, int> > > st; n set<pair<int, pair<int, int> > >::iterator st_it; n void make_set(int); n int find_set(int); n void union_sets(int, int); n bool subtree(int, int); n int lca(int, int); n void DFS(int, int, int); n int get_max(int, int); n int n, m; n int a, b; n int timer; n int lg; n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   cin >> n >> m; n   g.resize(n + 1); n   tree.resize(n + 1); n   for (int k = 1; k <= n; k++) make_set(k); n   for (int k = 1; k <= m; k++) { n     cin >> A[k].f >> A[k].s >> A[k].w; n     g[A[k].f].push_back(make_pair(A[k].s, A[k].w)); n     g[A[k].s].push_back(make_pair(A[k].f, A[k].w)); n     st.insert(make_pair(A[k].w, make_pair(A[k].f, A[k].s))); n   } n   long long ans = 0; n   while (!st.empty()) { n     st_it = st.begin(); n     a = st_it->second.first; n     b = st_it->second.second; n     while (find_set(a) == find_set(b)) { n       st.erase(st_it); n       if (st.empty()) break; n       st_it = st.begin(); n       a = st_it->second.first; n       b = st_it->second.second; n     } n     if (st.empty()) break; n     union_sets(a, b); n     mp[{a, b}] = 1; n     mp[{b, a}] = 1; n     tree[a].push_back(make_pair(b, st_it->first)); n     tree[b].push_back(make_pair(a, st_it->first)); n     ans += st_it->first; n     st.erase(st_it); n   } n   lg = log2(n + 1); n   for (int k = 0; k <= n; k++) { n     dp[k].resize(lg + 1); n     dp_mx[k].resize(lg + 1); n   } n   DFS(1, 0, 0); n   for (int k = 1; k <= m; k++) { n     int pq = lca(A[k].f, A[k].s); n     int q = max(get_max(A[k].f, pq), get_max(A[k].s, pq)); n     long long cnt = ans - q + A[k].w; n     cout << cnt <<   n ; n   } n   return 0; n } n void make_set(int v) { parent[v] = v; } n int find_set(int v) { n   if (v == parent[v]) return v; n   return parent[v] = find_set(parent[v]); n } n void union_sets(int a, int b) { n   a = find_set(a); n   b = find_set(b); n   if (a != b) parent[b] = a; n } n void DFS(int v, int p, int d) { n   pat[v] = p; n   dp[v][0] = p; n   dp_mx[v][0] = d; n   for (int k = 1; k <= lg; k++) { n     dp[v][k] = dp[dp[v][k - 1]][k - 1]; n     if (dp[v][k] != 0) n       dp_mx[v][k] = max(dp_mx[v][k - 1], dp_mx[dp[v][k - 1]][k - 1]); n   } n   time_in[v] = timer++; n   for (int k = 0; k < tree[v].size(); k++) { n     int to = tree[v][k].first; n     if (to == p) continue; n     DFS(to, v, tree[v][k].second); n   } n   time_out[v] = timer++; n } n bool subtree(int a, int b) { n   if (time_in[a] <= time_in[b] && time_out[b] <= time_out[a]) return 1; n   return 0; n } n int lca(int a, int b) { n   if (subtree(a, b)) return a; n   if (subtree(b, a)) return b; n   for (int k = lg; k >= 0; k--) { n     if (dp[a][k] == 0) continue; n     if (subtree(dp[a][k], b)) continue; n     a = dp[a][k]; n   } n   return pat[a]; n } n int get_max(int a, int lca) { n   int counter = -1; n   for (int k = lg; k >= 0; k--) { n     if (dp[a][k] == 0) continue; n     if (subtree(dp[a][k], lca) && dp[a][k] != lca) continue; n     counter = max(counter, dp_mx[a][k]); n     a = dp[a][k]; n   } n   return counter; n } n


#include <bits/stdc++.h> n using namespace std; n const long long mod = 1e9 + 7; n const int maxn = 2e5 + 50; n const long long inf = 1e17; n int t; n void get() { n   fflush(stdout); n   scanf( %d , &t); n   for (int i = 1; i <= t; i++) scanf( %*s ); n } n int main() { n   do { n     puts( next 0 1 ), get(); n     puts( next 0 ), get(); n   } while (t == 3); n   do { n     puts( next 0 1 2 3 4 5 6 7 8 9 ), get(); n   } while (t == 2); n   puts( done ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int max_n = 2e5 + 5; n long long ans[max_n]; n struct no { n   long long p, val; n }; n vector<no> v; n bool operator<(const no &x, const no &y) { return x.p < y.p; } n int main() { n   int n; n   scanf( %d , &n); n   for (int i = 1; i <= n; i++) { n     long long x, y; n     scanf( %lld%lld , &x, &y); n     v.push_back(no{x, 1}); n     v.push_back(no{y + 1, -1}); n   } n   sort(v.begin(), v.end()); n   long long add = v[0].val, now = v[0].p; n   for (int i = 1; i < v.size();) { n     for (int j = i; j < v.size(); j++) { n       if (v[j].p == now) { n         add = add + v[j].val; n         i++; n       } else { n         ans[add] += v[j].p - now; n         add += v[j].val; n         now = v[j].p; n         i = j + 1; n         break; n       } n     } n   } n   for (int i = 1; i <= n; i++) printf( %lld  , ans[i]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T> n void read(T& x) { n   x = 0; n   char ch = getchar(); n   long long f = 1; n   while (!isdigit(ch)) { n     if (ch ==  - ) f *= -1; n     ch = getchar(); n   } n   while (isdigit(ch)) { n     x = x * 10 + ch - 48; n     ch = getchar(); n   } n   x *= f; n } n template <typename T, typename... Args> n void read(T& first, Args&... args) { n   read(first); n   read(args...); n } n template <typename T> n void write(T arg) { n   T x = arg; n   if (x < 0) { n     putchar( - ); n     x = -x; n   } n   if (x > 9) { n     write(x / 10); n   } n   putchar(x % 10 +  0 ); n } n template <typename T, typename... Ts> n void write(T arg, Ts... args) { n   write(arg); n   if (sizeof...(args) != 0) { n     putchar(   ); n     write(args...); n   } n } n using namespace std; n long long gcd(long long a, long long b) { return b == 0 ? a : gcd(b, a % b); } n long long lcm(long long a, long long b) { return a / gcd(a, b) * b; } n const int N = 1e5 + 5; n int n, m; n int a[N], b[N], fa[N << 1]; n int cnt = 0; n long long res = 0; n struct node { n   int u, v, w; n   bool operator<(const node& no) const { return w > no.w; } n } edge[N << 1]; n int findroot(int x) { n   if (fa[x] == x) return x; n   return fa[x] = findroot(fa[x]); n } n bool merge(int x, int y) { n   int fax = findroot(x); n   int fay = findroot(y); n   if (fax != fay) { n     fa[fax] = fay; n     return true; n   } n   return false; n } n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cin >> m >> n; n   int tot = n; n   for (int i = 1; i <= m; i++) cin >> a[i]; n   for (int i = 1; i <= n; i++) cin >> b[i]; n   for (int i = 1; i <= m; i++) { n     ++tot; n     int s; n     cin >> s; n     for (int j = 1; j <= s; j++) { n       int v; n       cin >> v; n       edge[++cnt].u = tot, edge[cnt].v = v, edge[cnt].w = a[i] + b[v]; n       res += edge[cnt].w; n     } n   } n   sort(edge + 1, edge + 1 + cnt); n   for (int i = 1; i <= tot + 5; i++) fa[i] = i; n   for (int i = 1; i <= cnt; i++) { n     int u = edge[i].u, v = edge[i].v, w = edge[i].w; n     if (merge(u, v)) res -= w; n   } n   cout << res << endl; n } n


#include <bits/stdc++.h> n using namespace std; n long long a[100005]; n long long c[100005]; n int main() { n   int n, k; n   scanf( %d %d , &n, &k); n   for (int i = 1; i <= n; i++) { n     scanf( %lld , &a[i]); n     a[i] -= k; n   } n   long long A; n   scanf( %lld , &A); n   priority_queue<long long, vector<long long>, greater<long long> > P; n   long long cnt = 0; n   long long ans = 0; n   long long now = 0; n   for (int i = 1; i <= n; i++) { n     scanf( %lld , &c[i]); n   } n   for (int i = 1; i <= n; i++) { n     P.push(c[i]); n     if (a[i] > cnt) { n       long long t; n       if (a[i] < now) n         t = 0; n       else n         t = ((a[i] - now) % A == 0 ? (a[i] - now) / A : (a[i] - now) / A + 1); n       for (int j = 1; j <= t; j++) { n         if (P.empty()) { n           ans = -1; n           break; n         } n         ans += P.top(); n         now += A; n         P.pop(); n       } n       cnt = a[i]; n       if (ans == -1) break; n     } n   } n   printf( %lld , ans); n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long int t, n, k; n   cin >> t; n   while (t--) { n     cin >> n >> k; n     long long int a[n]; n     set<long long int> s; n     for (long long int i = 0; i < n; i++) { n       cin >> a[i]; n       s.insert(a[i]); n     } n     sort(a, a + n); n     if (k == 1 && s.size() == 1) { n       cout <<  1 n ; n       continue; n     } n     if (k == 1 && s.size() != 1) { n       cout <<  -1 n ; n       continue; n     } n     long long int c = 0; n     for (long long int i = 0; i < n - 1; i++) { n       if (a[i + 1] - a[i] != 0) { n         c++; n       } n     } n     long long int o = 1; n     cout << max(o, ((c + k - 2) / (k - 1))) <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n; n vector<int> ways[400], a[400], num[400]; n vector<int> ans; n struct edge { n   int a, b; n }; n vector<edge> gr; n bool dfs(int v, int f, int pr) { n   if (v == f) return true; n   for (int i = 0; i < a[v].size(); i++) { n     if (a[v][i] != pr) n       if (dfs(a[v][i], f, v)) return true; n   } n   return false; n } n void findway(int v, int f, int j) { n   if (v == f) { n     ways[j].push_back(v); n     return; n   } n   for (int i = 0; i < a[v].size(); i++) { n     if (dfs(a[v][i], f, v)) { n       ways[j].push_back(v); n       findway(a[v][i], f, j); n       return; n     } n   } n } n int main() { n   cin >> n; n   for (int i = 0; i < n - 1; i++) { n     int z, l; n     cin >> z >> l; n     a[l].push_back(z); n     a[z].push_back(l); n     num[l].push_back(i + 1); n     num[z].push_back(i + 1); n   } n   int num = 0; n   int k; n   while (cin >> k) { n     findway(1, k, num); n     num++; n   } n   for (int i = 0; i < ways[0].size(); i++) { n     ans.push_back(ways[0][i]); n   } n   for (int i = 1; i < num; i++) { n     int j = 0; n     while (ways[i - 1][j] == ways[i][j]) j++; n     j--; n     for (int z = ways[i - 1].size() - 2; z >= j; z--) { n       ans.push_back(ways[i - 1][z]); n     } n     for (int z = j + 1; z < ways[i].size(); z++) { n       ans.push_back(ways[i][z]); n     } n   } n   for (int i = ways[num - 1].size() - 2; i >= 0; i--) { n     ans.push_back(ways[num - 1][i]); n   } n   if (ans.size() != 2 * n - 1) n     cout << -1; n   else { n     for (int i = 0; i < ans.size(); i++) { n       cout << ans[i] <<    ; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long inf = (long long)4e18; n int n, q; n long long c[110000]; n long long v[110000]; n long long d[110000]; n int u[110000]; n int main() { n   ios::sync_with_stdio(0); n   cin >> n >> q; n   for (int i = 0; i < n; ++i) cin >> v[i]; n   for (int i = 0; i < n; ++i) cin >> c[i]; n   while (q--) { n     long long a, b; n     cin >> a >> b; n     for (int i = 0; i <= n; ++i) d[i] = -inf; n     long long et = 0; n     pair<long long, int> m1 = make_pair(-inf, -1); n     pair<long long, int> m2 = make_pair(-inf, -1); n     for (int i = 0; i < n; ++i) { n       long long ans = max(v[i] * b, d[c[i]]); n       ans = max(ans, d[c[i]] + v[i] * a); n       if (m1.second != c[i]) n         ans = max(ans, m1.first + v[i] * b); n       else n         ans = max(ans, m2.first + v[i] * b); n       if (ans > m1.first) n         if (m1.second != c[i]) n           m2 = m1, m1 = make_pair(ans, c[i]); n         else n           m1.first = ans; n       else if (ans > m2.first && c[i] != m1.second) n         m2 = make_pair(ans, c[i]); n       d[c[i]] = max(d[c[i]], ans); n       et = max(et, ans); n     } n     cout << et <<   n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int mod = 998244353; n const int inf = 0x3c3c3c3c; n const long long infl = 0x3c3c3c3c3c3c3c3c; n long long dp[5003][5003]; n int A, B, C; n int main() { n   ios::sync_with_stdio(0), cin.tie(0); n   ; n   cin >> A >> B >> C; n   vector<int> v({A, B, C}); n   sort((v).begin(), (v).end()); n   for (int i = 0; i <= 5000; i++) dp[i][0] = 1; n   for (int i = 1; i <= 5000; i++) { n     for (int j = 1; j <= 5000; j++) { n       dp[i][j] = (dp[i - 1][j - 1] * i + dp[i][j - 1]) % mod; n     } n   } n   long long ans = 1; n   ans = (ans * dp[v[2]][v[1]]) % mod; n   ans = (ans * dp[v[2]][v[0]]) % mod; n   ans = (ans * dp[v[1]][v[0]]) % mod; n   cout << ans; n } n


#include <bits/stdc++.h> n using namespace std; n int n; n int main() { n   scanf( %d , &n); n   int block = (int)sqrt(n), total = 0; n   for (int t = 1;; ++t) { n     for (int j = 1, k = max(1, n - t * block + 1); j <= block; ++j, ++k) { n       printf( %d  , k); n       if (++total == n) { n         return 0; n       } n     } n   } n   return 0; n } n


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


#include <bits/stdc++.h> n using namespace std; n long long a, b, c, d, e, f, n, ans, cn1, cn2, cn3, t; n string s, s1, s2; n int main() { n   ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0); n   cin >> a; n   cin >> b; n   cin >> c; n   cin >> d; n   cin >> e; n   cin >> f; n   ans = 0; n   if (e >= f) { n     ans = min(a, d); n     d = d - ans; n     ans = ans * e; n     ans += min(b, min(c, d)) * f; n     cout << ans; n   } else if (e < f) { n     ans = 0; n     ans = min(b, min(c, d)); n     d = d - ans; n     ans = ans * f; n     ans += min(a, d) * e; n     cout << ans << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, m, a, b; n   cin >> n >> m; n   if (m == n) { n     vector<bool> rev(n, false); n     vector<vector<int> > mat(n + 1); n     for (int i = 0; i < m; i++) { n       cin >> a >> b; n       mat[a].push_back(b); n       mat[b].push_back(a); n     } n     int cont = 0; n     for (int i = 1; i <= m; i++) { n       if (!rev[i]) { n         cont++; n         queue<int> COLA; n         COLA.push(i); n         rev[i] = true; n         while (!COLA.empty()) { n           int j = COLA.front(); n           COLA.pop(); n           for (int k = 0; k < mat[j].size(); k++) { n             if (!rev[mat[j][k]]) { n               rev[mat[j][k]] = 1; n               COLA.push(mat[j][k]); n             } n           } n         } n       } n     } n     if (cont == 1) n       cout <<  FHTAGN! n ; n     else n       cout <<  NO n ; n   } else n     cout <<  NO n ; n   return 0; n } n


#include <bits/stdc++.h> n #pragma GCC optimize( Ofast,no-stack-protector ) n #pragma GCC target( avx ) n int V[100000]; n float x; n int main() { n   int n, m, l, r, res, type; n   scanf( %d%d , &n, &m); n   for (int i = 1; i <= n; i++) scanf( %d , &V[i]); n   while (m--) { n     scanf( %d%d%d%f , &type, &l, &r, &x); n     if (type == 1) n       for (int i = l; i <= r; ++i) V[i] -= V[i] > x ? x : 0; n     else { n       res = 0; n       for (int i = l; i <= r; ++i) V[i] == x ? ++res : 0; n       printf( %d n , res); n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n #define TC 1 n #define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); n #define deb(...)   [  << #__VA_ARGS__  :   << (__VA_ARGS__) <<  ]   n using ll = long long; n  n template <typename T> n vector<T> readArr(int n) { n     vector <T> v(n); n     for (auto &it : v) n         cin >> it; n     return v; n } n  n void init() { n     ; n } n  n const int N = 1e5+ 5; n  n void solve() { n     int n, m; cin >> n >> m; n      n     vector <int> v = readArr<int>(n); n      n     for (int &x: v) n         x = x % m; n      n     vector <int> ct(N); n     for (int x: v) n         ct[x]++; n          n     int res = (ct[0] > 0 ? 1 : 0); n   //  cout << deb(res) <<   n ; n     for (int i=1; i<=m/2; i++) { n         int j = m - i; n         if (i == j) { n             res += (ct[j] > 0 ? 1 : 0); n            // cout << deb(i) deb(res) <<   n ; n             continue; n         } n         int a = ct[i], b = ct[j]; n         if (a > 0 && b > 0) { n             res++; n             if (a <= b) { n                 ct[i] = 0; n                 ct[j] -= (a+1); n                 ct[j] = max(ct[j], 0); n             } else { n                 ct[j] = 0; n                 ct[i] -= (b+1); n                 ct[i] = max(ct[i], 0); n             } n         } n          n          n         res += ct[i]; n         res += ct[j]; n          n        // cout << deb(i) deb(j) deb(res) <<   n ; n     } n     cout << res <<   n ; n } n  n int main() { n     IOS n     init(); n     int t = 1; n     if (TC) n         cin >> t; n  n     while (t--) { n         solve(); n     } n } n


#include <bits/stdc++.h> n template <class T> n T gcd(T a, T b) { n   T r; n   while (b != 0) { n     r = a % b; n     a = b; n     b = r; n   } n   return a; n } n template <class T> n T lcm(T a, T b) { n   return a / gcd(a, b) * b; n } n using namespace std; n const long long MOD = 1e9 + 7; n vector<int> p, q; n long long res[200005]; n void act() { n   long long a, b, c, d, cnt = 0; n   cin >> a >> b >> c >> d; n   for (int i = 1; i <= c; i++) p.push_back(2); n   for (int i = 1; i <= a; i++) p.push_back(0); n   for (int i = 1; i <= d; i++) q.push_back(3); n   for (int i = 1; i <= b; i++) q.push_back(1); n   if (p.size() <= q.size()) swap(p, q); n   for (int i = 0; i < p.size(); i++) { n     res[++cnt] = p[i]; n     if (i < q.size()) res[++cnt] = q[i]; n   } n   for (int i = 1; i < cnt; i++) n     if (abs(res[i] - res[i + 1]) != 1) { n       cout <<  NO  n            <<   n ; n       return; n     } n   cout <<  YES  n        <<   n ; n   for (int i = 1; i <= cnt; i++) cout << res[i] <<    ; n } n int main() { n   int t = 1; n   while (t--) act(); n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e5 + 5; n int n; n int a[N], b[N], c[N]; n map<pair<int, int>, pair<int, int> > mtp; n int main() { n   scanf( %d , &n); n   for (int i = 1; i <= n; i++) { n     scanf( %d %d %d , &a[i], &b[i], &c[i]); n   } n   double ans = 0; n   int idx = -1; n   for (int i = 1; i <= n; i++) { n     double curr = min(min(a[i], b[i]), c[i]); n     curr /= 2.0; n     if (curr > ans) { n       ans = curr; n       idx = i; n     } n   } n   int idx1 = -1, idx2 = -1; n   for (int i = 1; i <= n; ++i) { n     int aa = a[i]; n     int bb = b[i]; n     int cc = c[i]; n     if (mtp.find({min(aa, bb), max(aa, bb)}) != mtp.end()) { n       int key = mtp[{min(aa, bb), max(aa, bb)}].first; n       int backtrack = mtp[{min(aa, bb), max(aa, bb)}].second; n       double curr = min(min(aa, bb), key + cc); n       curr /= 2.0; n       if (curr > ans) { n         ans = curr; n         idx1 = i; n         idx2 = backtrack; n       } n     } n     if (mtp.find({min(bb, cc), max(bb, cc)}) != mtp.end()) { n       int key = mtp[{min(bb, cc), max(bb, cc)}].first; n       int backtrack = mtp[{min(bb, cc), max(bb, cc)}].second; n       double curr = min(min(bb, cc), key + aa); n       curr /= 2.0; n       if (curr > ans) { n         ans = curr; n         idx1 = i; n         idx2 = backtrack; n       } n     } n     if (mtp.find({min(aa, cc), max(aa, cc)}) != mtp.end()) { n       int key = mtp[{min(aa, cc), max(aa, cc)}].first; n       int backtrack = mtp[{min(aa, cc), max(aa, cc)}].second; n       double curr = min(min(aa, cc), key + bb); n       curr /= 2.0; n       if (curr > ans) { n         ans = curr; n         idx1 = i; n         idx2 = backtrack; n       } n     } n     int x = min(aa, bb); n     int y = max(aa, bb); n     if (mtp.find({x, y}) == mtp.end()) { n       mtp[{x, y}] = {cc, i}; n     } else { n       if (cc > mtp[{x, y}].first) { n         mtp[{x, y}] = {cc, i}; n       } n     } n     x = min(cc, bb); n     y = max(cc, bb); n     if (mtp.find({x, y}) == mtp.end()) { n       mtp[{x, y}] = {aa, i}; n     } else { n       if (aa > mtp[{x, y}].first) { n         mtp[{x, y}] = {aa, i}; n       } n     } n     x = min(aa, cc); n     y = max(aa, cc); n     if (mtp.find({x, y}) == mtp.end()) { n       mtp[{x, y}] = {bb, i}; n     } else { n       if (bb > mtp[{x, y}].first) { n         mtp[{x, y}] = {bb, i}; n       } n     } n   } n   if (idx2 == -1) { n     printf( 1 n%d n , idx); n   } else { n     printf( 2 n%d %d n , idx1, idx2); n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long n, a, b, x1, y1, x2, y2, d1, d2, r1 = 10000000000000000, n                                              r2 = 10000000000000000, ans; n   ans = r2; n   r1 *= -1; n   r2 = r1; n   vector<pair<long long, pair<long long, long long> > > v1, v2; n   cin >> n >> x1 >> y1 >> x2 >> y2; n   for (int i = 0; i < n; i++) { n     cin >> a >> b; n     d1 = (y1 - b) * (y1 - b) + (x1 - a) * (x1 - a); n     d2 = (y2 - b) * (y2 - b) + (x2 - a) * (x2 - a); n     v1.push_back(make_pair(d1, make_pair(a, b))); n     v2.push_back(make_pair(d2, make_pair(a, b))); n   } n   v1.push_back(make_pair(0, make_pair(x1, y1))); n   sort(v1.begin(), v1.end()); n   for (int i = 0; i <= n; i++) { n     long long rad1 = v1[i].first, rad2 = 0, itr = 1; n     while (rad1 == v1[i + itr].first) itr++; n     for (int j = i + itr; j <= n; j++) { n       a = v1[j].second.first; n       b = v1[j].second.second; n       d2 = (y2 - b) * (y2 - b) + (x2 - a) * (x2 - a); n       rad2 = max(rad2, d2); n     } n     ans = min(rad1 + rad2, ans); n   } n   cout << ans << endl; n   return 0; n } n


#include<bits/stdc++.h> n #define ll long long n using namespace std; n  n int k; n int dfs(int v, int p, vector<vector<int>> &viz){ n  int t=-1; n  vector < int > pos; n  for(auto u: viz[v]){ n   if(u==p) continue; n   int w=dfs(u,v,viz); n   pos.push_back(w); n   if(t==-1) t=w+1; n   t = min(t,w+1); n  } n  sort(pos.begin(), pos.end()); n  int n = pos.size(); n  if(v==1){ n   if(n>=1) k = max(k, pos[n-1]+1); n   if(n>=2) k = max(k, pos[n-2]+2); n  } n  else{ n   if(n>=2) k = max(k, pos[n-1]+2); n  } n  if(t==-1) t=0; n  return t; n } n  n int main(){ n  ios::sync_with_stdio(false); n    cin.tie(0); n    int t; n    cin >> t; n    while(t--){ n     int n; n     cin >> n; n     k=0; n     vector < vector <int> > viz(n+1); n     for(int i=0; i<n-1; i++){ n      int a, b; n      cin >> a >> b; n      viz[a].push_back(b); n      viz[b].push_back(a); n   } n   dfs(1,0,viz); n   cout << k << endl; n  } n } n


#include <bits/stdc++.h> n using namespace std; n const long double eps = 1e-9; n const long double pi = acos(-1.0); n const long long inf = 1e+9; n #pragma comment(linker,  /STACK:36777216 ) n template <typename T> n istream &operator>>(istream &, vector<T> &); n template <typename T> n ostream &operator<<(ostream &, const vector<T> &); n long long s, x; n int bit(long long x, int pos) { return (x & (1LL << pos)) > 0; } n void solve() { n   cin >> s >> x; n   int l = 45; n   while (bit(s, l) == 0) l--; n   vector<vector<long long> > dp(l + 2, vector<long long>(2, 0)); n   dp[0][0] = 1; n   for (int i = 1; i <= l + 1; i++) { n     int bs = bit(s, i - 1); n     int bx = bit(x, i - 1); n     if (bs == 0) { n       if (bx == 0) { n         dp[i][0] = dp[i - 1][0]; n         dp[i][1] = dp[i - 1][0]; n       } else { n         dp[i][1] += 2 * dp[i - 1][1]; n       } n     } else { n       if (bx == 0) { n         dp[i][0] = dp[i - 1][1]; n         dp[i][1] = dp[i - 1][1]; n       } else { n         dp[i][0] += 2 * dp[i - 1][0]; n       } n     } n   } n   long long ans = dp[l + 1][0]; n   if (x > s) ans = 0; n   if (x == 0) { n     if (s % 2 == 0) n       ans = 1; n     else n       ans = 0; n   } n   if (x == s) ans -= 2; n   cout << ans << endl; n } n int main() { n   solve(); n   return 0; n } n template <typename T> n istream &operator>>(istream &is, vector<T> &v) { n   for (int i = 0; i < v.size(); ++i) is >> v[i]; n   return is; n } n template <typename T> n ostream &operator<<(ostream &os, const vector<T> &v) { n   for (int i = 0; i < v.size(); ++i) os << v[i]; n   return os; n } n


#include <bits/stdc++.h> n using namespace std; n long long mc[1 << 20]; n list<int> th; n struct f { n   long long x, k, m, ms[20]; n } fs[100]; n bool kcomp(f a, f b) { return a.k < b.k; } n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   long long n, m, b; n   cin >> n >> m >> b; n   for (int i = 0; i < n; i++) { n     cin >> fs[i].x >> fs[i].k >> fs[i].m; n     for (int j = 0; j < fs[i].m; j++) { n       cin >> fs[i].ms[j]; n       fs[i].ms[j]--; n     } n   } n   sort(fs, fs + n, kcomp); n   long long bst = -1; n   fill_n(mc, 1 << 20, -1); n   for (int i = 0; i < n; i++) { n     int tbt = 0; n     for (int mi = 0; mi < fs[i].m; mi++) { n       tbt |= 1 << fs[i].ms[mi]; n     } n     int old = mc[tbt]; n     mc[tbt] = mc[tbt] == -1 ? fs[i].x : min(mc[tbt], fs[i].x); n     if (old == mc[tbt]) continue; n     th.push_front(tbt); n     auto it = th.begin(); n     auto pe = th.end(); n     { n       auto it2 = it; n       ++it2; n       for (; it2 != pe; ++it2) { n         int bt = *it | *it2; n         if (mc[bt] == -1) th.push_back(bt); n         mc[bt] = n             mc[bt] == -1 ? mc[*it] + mc[*it2] : min(mc[bt], mc[*it] + mc[*it2]); n       } n     } n     for (int cpos = 0; it != th.end(); ++it, ++cpos) { n       auto it2 = pe; n       auto ce = th.end(); n       for (; it2 != ce; ++it2) { n         int bt = *it | *it2; n         if (mc[bt] == -1) th.push_back(bt); n         mc[bt] = n             mc[bt] == -1 ? mc[*it] + mc[*it2] : min(mc[bt], mc[*it] + mc[*it2]); n       } n     } n     if (mc[(1 << m) - 1] != -1) { n       bst = bst == -1 ? mc[(1 << m) - 1] + b * fs[i].k n                       : min(bst, mc[(1 << m) - 1] + b * fs[i].k); n     } n   } n   cout << bst <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n char s[10]; n int v[10][10]; n int dx[4] = {-2, -2, 2, 2}, dy[4] = {2, -2, 2, -2}; n int sx, sy, ex, ey; n int mark[10][10]; n int ans; n void dfs(int curx, int cury, int curnum) { n   for (int i = 0; i < 4; i++) { n     int tempx = curx + dx[i]; n     int tempy = cury + dy[i]; n     if (tempx >= 1 && tempx <= 8 && tempy >= 1 && tempy <= 8 && n         !mark[tempx][tempy]) { n       mark[tempx][tempy] = curnum + 1; n       dfs(tempx, tempy, curnum + 1); n     } n   } n } n int const nMax = 1; n int main() { n   int t; n   scanf( %d , &t); n   while (t--) { n     sx = -1; n     memset(mark, 0, sizeof(mark)); n     memset(v, 0, sizeof(v)); n     getchar(); n     for (int i = 1; i <= 8; i++) { n       gets(s); n       for (int j = 0; j < 8; j++) { n         if (s[j] ==  # ) n           v[i][j + 1] = -1; n         else if (s[j] ==  . ) { n           v[i][j + 1] = 0; n         } else { n           v[i][j + 1] = 0; n           if (sx != -1) { n             ex = i; n             ey = j + 1; n           } else { n             sx = i; n             sy = j + 1; n           } n         } n       } n     } n     if (abs(sx - ex) % 2 != 0 || abs(sy - ey) % 2 != 0) { n       puts( NO ); n       continue; n     } n     dfs(sx, sy, 0); n     if (mark[ex][ey] % 2 == 0 && mark[ex][ey] != 0) n       puts( YES ); n     else n       puts( NO ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int report[105], folder[105]; n int main() { n   int n; n   cin >> n; n   int k = 1, am = 0; n   for (int i = 1; i <= n; i++) cin >> report[i]; n   for (int i = 1; i <= n; i++) { n     if (report[i] < 0 && am == 2) { n       k++; n       folder[k]++; n       am = 1; n     } else { n       folder[k]++; n       if (report[i] < 0) am++; n     } n   } n   cout << k << endl; n   for (int i = 1; i <= k; i++) cout << folder[i] <<    ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n; n string a; n vector<pair<int, int> > v; n void input() { n   cin >> a; n   n = a.size(); n } n void solve() { n   int i; n   for (i = 0; i < n; i++) { n     if (a[i] !=  a ) { n       v.push_back(make_pair((a[i] -  a ), i)); n     } n   } n   int sz = v.size(); n   if (sz == 0) { n     cout << a <<   n ; n     return; n   } n   if ((sz % 2) != 0) { n     cout <<  :( n ; n     return; n   } n   int half = (sz / 2); n   for (i = 0; i < half; i++) { n     if (v[i].first != v[i + half].first) { n       cout <<  :( n ; n       return; n     } n   } n   int st_id = v[half].second; n   for (i = st_id; i < n; i++) { n     if (a[i] ==  a ) { n       cout <<  :( n ; n       return; n     } n   } n   for (i = 0; i < st_id; i++) { n     cout << a[i]; n   } n   cout <<   n ; n } n int main() { n   ios_base ::sync_with_stdio(false); n   cin.tie(NULL); n   input(); n   solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<int> vec[10]; n vector<vector<int> > vvv(183); n queue<int> qu; n map<string, int> mp; n map<string, int>::iterator iter; n bool ch[10] = {false}; n int main() { n   string s1, s2; n   cin >> s1 >> s2; n   for (int i = 0; i < s1.length(); i++) { n     if (s1[i] >= s2[i]) n       s1[i] = s2[i]; n     else { n       cout << -1 << endl; n       return 0; n     } n   } n   cout << s1 << endl; n   return 0; n } n


#include <bits/stdc++.h> n int freq[1005] = {0}; n int find_int(int a, int b, int c, int d) { n   int i; n   if (b < c) return 0; n   if (a > d) return 0; n   if (a <= c && b >= d) { n     for (i = c; i <= d; i++) freq[i] = 1; n     return d - c + 1; n   } else if (a >= c && b <= d) { n     for (i = a; i <= b; i++) freq[i] = 1; n     return b - a + 1; n   } else if (a >= c && b >= d) { n     for (i = a; i <= d; i++) freq[i] = 1; n     return d - a + 1; n   } else if (a <= c && b <= d) { n     for (i = c; i <= b; i++) freq[i] = 1; n     return b - c + 1; n   } n } n int main() { n   int i, j, k, l, r, p, q, a[55], b[55], c[55], d[55]; n   scanf( %d %d %d %d , &p, &q, &l, &r); n   for (i = 0; i < p; i++) scanf( %d %d , &a[i], &b[i]); n   for (i = 0; i < q; i++) scanf( %d %d , &c[i], &d[i]); n   int count = 0; n   for (i = 0; i < q; i++) { n     for (j = 0; j < p; j++) { n       int l1, r1; n       if (c[i] >= a[j] && c[i] <= b[j]) l1 = 0; n       r1 = b[j] - c[i]; n       if (c[i] < a[j]) { n         l1 = a[j] - d[i]; n         r1 = b[j] - c[i]; n       } n       count = count + find_int(l1, r1, l, r); n     } n   } n   count = 0; n   for (i = l; i <= r; i++) { n     if (freq[i] == 1) count++; n   } n   printf( %d n , count); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int GLL(long long& x) { return scanf( %lld , &x); } n int GI(int& x) { return scanf( %d , &x); } n int m, n; n long long add(long long x, long long y) { n   x += y; n   if (x >= m) x -= m; n   return x; n } n long long sub(long long x, long long y) { n   x -= y; n   if (x < 0) x += m; n   return x; n } n long long mul(long long x, long long y) { return (x * y) % m; } n long long powmod(long long a, long long b) { n   long long res = 1; n   a %= m; n   for (; b; b >>= 1) { n     if (b & 1) res = res * a % m; n     a = a * a % m; n   } n   return res; n } n bool euler_criterion(long long a, long long p) { n   return powmod(a, (p - 1) / 2) == 1; n } n long long tonelli_shanks(long long a, long long p) { n   long long q = p - 1; n   long long s = 0; n   while (q % 2 == 0) { n     s++; n     q /= 2; n   } n   long long z = rand() % p; n   while (euler_criterion(z, p)) { n     z = rand() % p; n   } n   long long c = powmod(z, q); n   long long r = powmod(a, (q + 1) / 2); n   long long t = powmod(a, q); n   long long m = s; n   while (t != 1) { n     long long lo = 1, hi = m - 1; n     long long i = -1; n     while (lo <= hi) { n       long long mid = (lo + hi) / 2; n       if (powmod(t, 1LL << mid) == 1) { n         i = mid; n         hi = mid - 1; n       } else { n         lo = mid + 1; n       } n     } n     long long b = powmod(c, 1LL << (m - i - 1)); n     r = mul(b, r); n     t = mul(t, mul(b, b)); n     c = mul(b, b); n     m = i; n   } n   return r; n } n const int MAXN = 100005; n long long a[MAXN], d[MAXN], sqrd[MAXN], sumd[MAXN], sumsqrd[MAXN]; n long long sumsqr[MAXN]; n unordered_set<long long> sa, stest; n int main() { n   GI(m); n   GI(n); n   for (int i = 1; i <= (int)(n); i++) { n     GLL(a[i]); n     sa.insert(a[i]); n   } n   if (n == 0) { n     printf( %lld 0 n , a[0]); n     return 0; n   } n   memset(d, 0, sizeof d); n   memset(sqrd, 0, sizeof sqrd); n   memset(sumd, 0, sizeof sumd); n   memset(sumsqrd, 0, sizeof sumsqrd); n   sort(a + 1, a + n + 1); n   long long inv2 = powmod(2, m - 2); n   for (int i = 2; i <= n; i++) { n     d[i] = add(d[i - 1], mul(a[i] - a[i - 1], i - 1)); n     sqrd[i] = add(add(sqrd[i - 1], mul(2, mul(a[i] - a[i - 1], d[i - 1]))), n                   mul(mul(a[i] - a[i - 1], a[i] - a[i - 1]), i - 1)); n     sumd[i] = add(sumd[i - 1], d[i]); n     sumsqrd[i] = add(sumsqrd[i - 1], sqrd[i]); n   } n   memset(sumsqr, 0, sizeof sumsqr); n   for (int i = 1; i < MAXN; i++) sumsqr[i] = add(sumsqr[i - 1], mul(i, i)); n   long long c = 0; n   for (int i = 1; i <= (int)(n - 1); i++) c = add(c, sumsqr[i]); n   long long test2 = mul(sumsqrd[n], powmod(c, m - 2)); n   if (test2 == 0 || euler_criterion(test2, m)) { n     long long test = test2 == 0 ? 1 : tonelli_shanks(test2, m); n     int fwd = 0; n     long long curr = add(a[1], test); n     while (curr != a[1] && sa.find(curr) != sa.end()) { n       fwd++; n       curr = add(curr, test); n     } n     int bwd = 0; n     curr = sub(a[1], test); n     while (curr != a[1] && sa.find(curr) != sa.end()) { n       bwd++; n       curr = sub(curr, test); n     } n     if (fwd + bwd + 1 >= n) { n       curr = add(curr, test); n       printf( %lld %lld n , curr, test); n     } else { n       printf( -1 n ); n     } n   } else { n     printf( -1 n ); n     return 0; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n inline void Boost() { n   ios::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n } n const long double eps = 1e-10; n int main() { n   Boost(); n   int n; n   cin >> n; n   vector<long double> A(n), B(n); n   for (auto &x : A) cin >> x; n   for (auto &x : B) cin >> x; n   long double lo = 0; n   long double hi = 1000000000; n   long double best = 0; n   while (hi - lo > eps) { n     long double mid = (lo + hi) / 2.0; n     pair<long double, long double> inter = {A[0] - mid * B[0], n                                             A[0] + mid * B[0]}; n     for (int i = 1; i < n; ++i) { n       pair<long double, long double> temp = {A[i] - mid * B[i], n                                              A[i] + mid * B[i]}; n       inter.first = max(inter.first, temp.first); n       inter.second = min(inter.second, temp.second); n     } n     if (inter.first <= inter.second) { n       best = mid; n       hi = mid; n     } else { n       lo = mid; n     } n   } n   cout << fixed << setprecision(10) << best; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, x; n   long long score, sum = 0; n   cin >> n; n   vector<int> ara(n); n   for (int i = 0; i < n; i++) { n     cin >> ara[i]; n     sum += ara[i]; n   } n   sort(ara.begin(), ara.end()); n   reverse(ara.begin(), ara.end()); n   score = sum; n   for (int i = n - 1; i >= 0; i--) { n     sum -= ara[i]; n     score += (ara[i] + sum); n   } n   score -= ara[0]; n   cout << score << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int x; n   cin >> x; n   x++; n   cout << x / 36 <<     << (((x % 36)) / 3); n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   long long n = 0, m = 0, mn = 0, mx = 0, l = 1, r = 0, k = 0, j = 1, i = 0, n             ans = 0; n   cin >> n; n   vector<int> a(n); n   for (auto &x : a) cin >> x; n   bool got = true; n   while (got) { n     while (i < l) { n       if (a[i] > l) { n         l = a[i]; n       } n       i++; n     } n     l = i + 1; n     m++; n     if (l == n + 1) { n       break; n     } n   } n   cout << m << endl; n } n


#include <bits/stdc++.h> n using namespace std; n set<int> st; n set<int> en; n vector<int> out; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   int n; n   cin >> n; n   for (int i = 0; i < n; i++) { n     int t; n     cin >> t; n     if (st.find(t) == st.end()) n       st.insert(t); n     else if (en.find(t) == en.end()) n       en.insert(t); n   } n   int ma = 0; n   for (set<int>::iterator it = st.begin(); it != st.end(); it++) { n     out.push_back(*it); n     ma = max(ma, (*it)); n   } n   for (set<int>::reverse_iterator it = en.rbegin(); it != en.rend(); it++) n     if ((*it) != ma) out.push_back(*it); n   cout << out.size() << endl; n   for (int i = 0; i < out.size(); i++) cout << out[i] <<    ; n } n


#include <bits/stdc++.h> n using namespace std; n int const MAXN = 2e5 + 10; n int n, m, T; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cin >> n >> m; n   int t = 0; n   while (m) { n     t = 10 * t + m % 10; n     m /= 10; n   } n   cout << n + t; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long INF = 1e18; n const int32_t M = 1e9 + 7; n const int32_t MM = 998244353; n void solve() { n   long long x; n   cin >> x; n   long long n = 2; n   long long tot = (n * (n - 1)) / 2; n   long long ans = 0; n   while (tot <= x) { n     ans++; n     n *= 2; n     tot += (n * (n - 1)) / 2; n   } n   cout << ans << endl; n } n int32_t main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   long long t; n   cin >> t; n   while (t--) solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1000010, P = 2000010; n int f[P]; n int n, t, a[N]; n int d[N], g[N], r[P]; n vector<int> q[P], v[N]; n void init(int c, int p) { n   vector<int> z; n   int cv = a[c]; n   d[c] = d[p] + 1; n   g[c] = -1; n   if (c == 1) fill(r + 1, r + P, -1); n   for (int i = 0; i < q[cv].size(); i++) { n     int t = q[cv][i]; n     if (g[c] == -1 || d[r[t]] > d[g[c]]) g[c] = r[t]; n     z.push_back(r[t]); n     r[t] = c; n   } n   for (int i = 0; i < v[c].size(); i++) n     if (v[c][i] != p) init(v[c][i], c); n   for (int i = 0; i < q[cv].size(); i++) r[q[cv][i]] = z[i]; n } n int main() { n   scanf( %d %d , &n, &t); n   for (int i = 2; i < P; i++) n     if (!f[i]) n       for (int j = 1; i * j < P; j++) f[i * j] = i; n   for (int i = 2; i < P; i++) { n     int c = i / f[i]; n     if (f[c] != f[i]) q[i].push_back(f[i]); n     for (int j = 0; j < q[c].size(); j++) q[i].push_back(q[c][j]); n   } n   for (int i = 1; i <= n; i++) scanf( %d , &a[i]); n   for (int i = 1; i < n; i++) { n     int x, y; n     scanf( %d %d , &x, &y); n     v[x].push_back(y); n     v[y].push_back(x); n   } n   init(1, 0); n   while (t--) { n     int k; n     scanf( %d , &k); n     if (k == 1) { n       int c; n       scanf( %d , &c); n       printf( %d n , g[c]); n     } else { n       int x, y; n       scanf( %d %d , &x, &y); n       a[x] = y; n       init(1, 0); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long MXN = 2e5 + 10; n const long long MXS = MXN * 4; n const long long INF = 1e18; n struct Typ { n   long long Sum, ValL, ValR, dpL, dpR, Maxdp, sz, Mindis, Val2L, Val2R; n   pair<long long, long long> Valley; n   void init() { n     Mindis = INF; n     Val2L = Val2R = Valley.first = Valley.second = 0; n     Sum = dpL = dpR = Maxdp = ValL = ValR = sz = 0; n   } n }; n long long n, q, k; n long long A[MXN], Lazy[MXS]; n bool Ok; n Typ seg[MXS], M; n Typ Merge(Typ a, Typ b) { n   Typ now; n   now.Mindis = INF; n   now.Sum = a.Sum + b.Sum + abs(a.ValR - b.ValL); n   now.ValL = a.ValL, now.ValR = b.ValR; n   now.dpL = (a.sz > 1 ? a.dpL : (a.ValR >= b.ValL)); n   now.dpR = (b.sz > 1 ? b.dpR : (b.ValL >= a.ValR)); n   long long x = a.dpR + (a.ValR >= b.ValL); n   long long y = b.dpL + (b.ValL >= a.ValR); n   now.Maxdp = max(a.Maxdp, b.Maxdp); n   if (a.sz > 1) now.Maxdp = max(now.Maxdp, x); n   if (b.sz > 1) now.Maxdp = max(now.Maxdp, y); n   if (a.sz > 1 && x == 1) { n     now.Mindis = n         min(now.Mindis, (b.ValL > a.ValR ? b.ValL - a.ValR : a.Val2R - a.ValR)); n   } n   if (a.sz > 1 && x == 0) { n     now.Valley = {a.Val2R - a.ValR, b.ValL - a.ValR}; n   } n   if (b.sz > 1 && y == 1) { n     now.Mindis = n         min(now.Mindis, (a.ValR > b.ValL ? a.ValR - b.ValL : b.Val2L - b.ValL)); n   } n   if (b.sz > 1 && y == 0) { n     now.Valley = {a.ValR - b.ValL, b.Val2L - b.ValL}; n   } n   if (a.Valley.first != 0) { n     now.Valley = a.Valley; n   } n   if (b.Valley.first != 0) { n     now.Valley = b.Valley; n   } n   now.Mindis = min(now.Mindis, a.Mindis); n   now.Mindis = min(now.Mindis, b.Mindis); n   if (a.sz == 1) n     now.Val2L = b.ValL; n   else n     now.Val2L = a.Val2L; n   if (b.sz == 1) n     now.Val2R = a.ValR; n   else n     now.Val2R = b.Val2R; n   now.sz = a.sz + b.sz; n   return now; n } n void Build(long long id = 1, long long s = 1, long long e = n) { n   seg[id].init(); n   seg[id].sz = (e - s + 1); n   if ((e - s + 1) == 1) { n     seg[id].ValL = seg[id].ValR = A[s]; n     return; n   } n   Build((id * 2), s, ((s + e) / 2)), Build((id * 2 + 1), ((s + e) / 2 + 1), e); n   seg[id] = Merge(seg[(id * 2)], seg[(id * 2 + 1)]); n } n void Shift(long long id, long long s, long long e) { n   if (!Lazy[id]) return; n   seg[id].ValL += Lazy[id], seg[id].ValR += Lazy[id]; n   if ((e - s + 1) > 1) { n     seg[id].Val2L += Lazy[id], seg[id].Val2R += Lazy[id]; n     Lazy[(id * 2)] += Lazy[id]; n     Lazy[(id * 2 + 1)] += Lazy[id]; n   } n   Lazy[id] = 0; n } n void Upd(long long l, long long r, long long x, long long id = 1, n          long long s = 1, long long e = n) { n   Shift(id, s, e); n   if (e < l || s > r) return; n   if (l <= s && e <= r) { n     Lazy[id] += x; n     Shift(id, s, e); n     return; n   } n   Upd(l, r, x, (id * 2), s, ((s + e) / 2)), n       Upd(l, r, x, (id * 2 + 1), ((s + e) / 2 + 1), e); n   seg[id] = Merge(seg[(id * 2)], seg[(id * 2 + 1)]); n } n void Calc(long long l, long long r, long long id = 1, long long s = 1, n           long long e = n) { n   Shift(id, s, e); n   if (e < l || s > r) return; n   if (l <= s && e <= r) { n     if (!Ok) n       M = seg[id], Ok = true; n     else { n       M = Merge(M, seg[id]); n     } n     return; n   } n   Calc(l, r, (id * 2), s, ((s + e) / 2)), n       Calc(l, r, (id * 2 + 1), ((s + e) / 2 + 1), e); n   seg[id] = Merge(seg[(id * 2)], seg[(id * 2 + 1)]); 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; i++) cin >> A[i]; n   Build(); n   cin >> q; n   while (q--) { n     long long t, l, r, x, ans; n     cin >> t >> l >> r >> x; n     if (t == 2) n       Upd(l, r, x); n     else { n       Ok = false; n       Calc(max(l - 1, 1ll), min(r + 1, n)); n       if (M.Maxdp == 2) ans = 2 * x + seg[1].Sum; n       if (M.Maxdp == 0) { n         ans = seg[1].Sum; n         ans += (M.Valley.first >= x ? -x : x - M.Valley.first * 2); n         ans += (M.Valley.second >= x ? -x : x - M.Valley.second * 2); n       } n       if (M.Maxdp == 1) { n         ans = x + (M.Mindis >= x ? -x : x - M.Mindis * 2); n         if (M.Valley.first != 0) { n           ans = max(ans, n                     (M.Valley.first >= x ? -x : x - M.Valley.first * 2) + n                         (M.Valley.second >= x ? -x : x - M.Valley.second * 2)); n         } n         ans += seg[1].Sum; n       } n       cout << ans <<   n ; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const double pi = acos(-1.0); n template <typename T, typename U> n inline void amin(T &x, U y) { n   if (y < x) x = y; n } n template <typename T, typename U> n inline void amax(T &x, U y) { n   if (x < y) x = y; n } n using namespace std; n int main() { n   int n, m; n   cin >> n >> m; n   string grid[n]; n   for (int i = 0; i < n; ++i) { n     cin >> grid[i]; n   } n   int left[n][m], right[n][m], top[n][m], bottom[n][m]; n   memset(left, (0), sizeof(left)); n   memset(right, (0), sizeof(right)); n   memset(top, (0), sizeof(top)); n   memset(bottom, (0), sizeof(bottom)); n   for (int i = 1; i < n; ++i) { n     for (int j = 1; j < m; ++j) { n       if (grid[i][j] !=  * ) continue; n       left[i][j] = left[i][j - 1]; n       top[i][j] = top[i - 1][j]; n       if (grid[i][j - 1] ==  * ) left[i][j] = left[i][j - 1] + 1; n       if (grid[i - 1][j] ==  * ) top[i][j] = top[i - 1][j] + 1; n     } n   } n   for (int i = n - 2; i >= 0; --i) { n     for (int j = m - 2; j >= 0; --j) { n       if (grid[i][j] !=  * ) continue; n       if (grid[i][j + 1] ==  * ) right[i][j] = right[i][j + 1] + 1; n       if (grid[i + 1][j] ==  * ) bottom[i][j] = bottom[i + 1][j] + 1; n     } n   } n   bool marked[n][m]; n   memset(marked, (false), sizeof(marked)); n   vector<int> row, col, sz; n   for (int i = 0; i < n; ++i) { n     for (int j = 0; j < m; ++j) { n       if (grid[i][j] !=  * ) continue; n       int cnt1 = min(left[i][j], right[i][j]); n       int cnt2 = min(top[i][j], bottom[i][j]); n       int cnt = min(cnt1, cnt2); n       if (cnt != 0) { n         row.push_back(i + 1); n         col.push_back(j + 1); n         sz.push_back(cnt); n         marked[i][j] = true; n         for (int k = 1; k <= cnt; ++k) { n           marked[i][j - k] = true; n           marked[i][j + k] = true; n           marked[i - k][j] = true; n           marked[i + k][j] = true; n         } n       } n     } n   } n   bool pos = true; n   for (int l = 0; l < n; ++l) { n     for (int j = 0; j < m; ++j) { n       if (grid[l][j] ==  *  && !marked[l][j]) { n         pos = false; n         break; n       } n     } n   } n   if (pos) { n     cout << row.size() <<   n ; n     for (int i = 0; i < row.size(); ++i) { n       cout << row[i] <<     << col[i] <<     << sz[i] <<   n ; n     } n   } else { n     cout <<  -1 n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int mod = 1e9 + 7; n long long powmod(long long x, long long y) { n   long long t; n   for (t = 1; y; y >>= 1, x = x * x % mod) n     if (y & 1) t = t * x % mod; n   return t; n } n long long gcd(long long x, long long y) { return y ? gcd(y, x % y) : x; } n long long lcm(long long x, long long y) { return x * (y / gcd(x, y)); } n void smask(int pos, int &i) { i = i ^ (1 << pos); } n void clmask(int pos, int &i) { i = i & ~(1 << pos); } n bool chmask(int pos, int i) { return i & (1 << pos); } n double cordist(pair<double, double> a, pair<double, double> b) { n   return sqrt(((a.first - b.first) * (a.first - b.first)) + n               ((a.second - b.second) * (a.second - b.second))); n } n long long n, m, q, r; n int primes[11] = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31}; n void solve() { n   cin >> n; n   long long arr[n]; n   long long brr[n]; n   unordered_map<int, int> mp; n   int k = 1; n   for (int i = 0; i < n; i++) { n     cin >> arr[i]; n     for (int j = 0; j < 11; j++) { n       if (arr[i] % primes[j] == 0) { n         if (mp.count(primes[j]) == 0) { n           brr[i] = k; n           mp[primes[j]] = k++; n         } else { n           brr[i] = mp[primes[j]]; n         } n         break; n       } n     } n   } n   cout << k - 1 << endl; n   for (int i = 0; i < n; i++) cout << brr[i] <<    ; n   cout << endl; n } n int main() { n   int k = 1; n   cin >> k; n   while (k--) { n     solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   long long t; n   cin >> t; n   while (t--) { n     long long n, m; n     cin >> n >> m; n     if (m >= 2 * n) { n       cout << n <<     << 2 * n << endl; n     } else { n       cout <<  -1   n            <<  -1  << endl; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const double pi = 3.1415926535897; n int main() { n   string pass; n   string str[10]; n   cin >> pass; n   for (int i = 0; i < 10; i++) { n     cin >> str[i]; n   } n   for (int i = 0; i < 80; i += 10) { n     string tmp = pass.substr(i, 10); n     for (int io = 0; io < 10; io++) { n       if (tmp == str[io]) { n         cout << io; n         break; n       } n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 300005; n int n, m, q; n struct Graph { n   struct Edge { n     int to, next; n   } edge[N << 1]; n   int head[N], cnt; n   Graph() { n     memset(head, 0, sizeof(head)); n     cnt = 1; n     return; n   } n   void add_edge(int u, int v) { n     if (u == 0 || v == 0) return; n     cnt++; n     edge[cnt].to = v; n     edge[cnt].next = head[u]; n     head[u] = cnt; n     return; n   } n }; n Graph G1; n int dfn1[N], low1[N], Index1; n stack<int> s1; n int bel1[N], tot1; n void tarjan1(int u, int pre) { n   dfn1[u] = low1[u] = ++Index1; n   s1.push(u); n   for (int i = G1.head[u]; i; i = G1.edge[i].next) { n     int v = G1.edge[i].to; n     if ((i ^ 1) == pre) continue; n     if (!dfn1[v]) { n       tarjan1(v, i); n       low1[u] = min(low1[u], low1[v]); n     } else n       low1[u] = min(low1[u], dfn1[v]); n   } n   if (dfn1[u] == low1[u]) { n     tot1++; n     while (s1.top() != u) { n       bel1[s1.top()] = tot1; n       s1.pop(); n     } n     bel1[u] = tot1; n     s1.pop(); n     return; n   } n   return; n } n vector<int> G[N]; n int f[N][20], dep[N]; n int dfn[N], Index; n void dfs(int u, int father) { n   dfn[u] = ++Index; n   dep[u] = dep[father] + 1; n   f[u][0] = father; n   for (int i = 1; (1 << i) <= n; i++) f[u][i] = f[f[u][i - 1]][i - 1]; n   for (int v : G[u]) { n     if (v == father) continue; n     dfs(v, u); n   } n   return; n } n int LCA(int u, int v) { n   if (dep[u] < dep[v]) swap(u, v); n   for (int i = log2(n); i >= 0; i--) n     if (dep[f[u][i]] >= dep[v]) u = f[u][i]; n   if (u == v) return u; n   for (int i = log2(n); i >= 0; i--) n     if (f[u][i] != f[v][i]) u = f[u][i], v = f[v][i]; n   return f[u][0]; n } n int s[N << 1], top; n Graph G2; n vector<int> pos; n void ins(int u) { n   int z = LCA(u, s[top]); n   pos.push_back(u); n   if (z == s[top]) n     s[++top] = u; n   else { n     pos.push_back(z); n     while (top >= 2 && dep[s[top - 1]] > dep[z]) { n       if (s[top - 1] != s[top]) n         G2.add_edge(s[top - 1], s[top]), G2.add_edge(s[top], s[top - 1]); n       top--; n     } n     if (top) { n       if (z != s[top]) G2.add_edge(z, s[top]), G2.add_edge(s[top], z); n       top--; n     } n     s[++top] = z; n     s[++top] = u; n   } n   return; n } n bool cmp(int a, int b) { return dfn[a] < dfn[b]; } n void build(vector<int>& k) { n   sort(k.begin(), k.end()); n   k.erase(unique(k.begin(), k.end()), k.end()); n   sort(k.begin(), k.end(), cmp); n   top = 0; n   s[++top] = 0; n   for (int x : k) n     if (x != 0) ins(x); n   while (top >= 2) { n     if (s[top - 1] != s[top]) n       G2.add_edge(s[top - 1], s[top]), G2.add_edge(s[top], s[top - 1]); n     top--; n   } n   sort(pos.begin(), pos.end()); n   pos.erase(unique(pos.begin(), pos.end()), pos.end()); n   return; n } n int dfn2[N], low2[N], Index2; n stack<int> s2; n int bel2[N], tot2; n void tarjan2(int u, int pre) { n   dfn2[u] = low2[u] = ++Index2; n   s2.push(u); n   for (int i = G2.head[u]; i; i = G2.edge[i].next) { n     int v = G2.edge[i].to; n     if ((i ^ 1) == pre) continue; n     if (!dfn2[v]) { n       tarjan2(v, i); n       low2[u] = min(low2[u], low2[v]); n     } else n       low2[u] = min(low2[u], dfn2[v]); n   } n   if (dfn2[u] == low2[u]) { n     tot2++; n     while (s2.top() != u) { n       bel2[s2.top()] = tot2; n       s2.pop(); n     } n     bel2[u] = tot2; n     s2.pop(); n     return; n   } n   return; n } n int x[N]; n long long R; n int rotate(int element) { n   element = (element + R) % n; n   if (element == 0) { n     element = n; n   } n   return element; n } n void clear() { n   for (int u : pos) { n     G2.head[u] = 0; n     dfn2[u] = low2[u] = 0; n     bel2[u] = 0; n   } n   pos.clear(); n   return; n } n int Case = 0; n void solve() { n   Case++; n   int ni, mi; n   scanf( %d%d , &ni, &mi); n   vector<int> k; n   for (int i = 1; i <= ni; i++) { n     scanf( %d , &x[i]); n     x[i] = rotate(x[i]); n     x[i] = bel1[x[i]]; n     k.push_back(x[i]); n   } n   G2.cnt = 1; n   for (int i = 1; i <= mi; i++) { n     int x, y; n     scanf( %d%d , &x, &y); n     x = rotate(x), y = rotate(y); n     x = bel1[x], y = bel1[y]; n     if (x == y) continue; n     G2.add_edge(x, y); n     G2.add_edge(y, x); n     k.push_back(x), k.push_back(y); n   } n   build(k); n   Index2 = tot2 = 0; n   while (!s2.empty()) s2.pop(); n   for (int u : pos) n     if (!dfn2[u]) tarjan2(u, 0); n   for (int i = 2; i <= ni; i++) n     if (bel2[x[i]] != bel2[x[i - 1]]) { n       printf( NO n ); n       clear(); n       return; n     } n   printf( YES n ); n   R += Case; n   clear(); n   return; n } n int main() { n   scanf( %d%d%d , &n, &m, &q); n   for (int i = 1; i <= m; i++) { n     int x, y; n     scanf( %d%d , &x, &y); n     G1.add_edge(x, y); n     G1.add_edge(y, x); n   } n   for (int i = 1; i <= n; i++) n     if (!dfn1[i]) tarjan1(i, 0); n   for (int u = 1; u <= n; u++) n     for (int i = G1.head[u]; i; i = G1.edge[i].next) { n       int v = G1.edge[i].to; n       int x = bel1[u], y = bel1[v]; n       if (x < y) G[x].push_back(y), G[y].push_back(x); n     } n   for (int i = 1; i <= tot1; i++) n     if (!dep[i]) dfs(i, 0); n   while (q--) solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void solve() { n   long long n, k; n   cin >> n >> k; n   vector<long long> a(n); n   for (long long &i : a) cin >> i; n   sort(a.begin(), a.end()); n   a.resize(unique(a.begin(), a.end()) - a.begin()); n   n = a.size(); n   if (k == 1 and n > 1) { n     cout << -1 << endl; n     return; n   } else if (k >= n) { n     cout << 1 << endl; n     return; n   } n   long long ans; n   ans = 1, n -= k, k--; n   ans += (n + k - 1) / k; n   cout << ans << endl; n } n int32_t main() { n   ios_base::sync_with_stdio(false), cin.tie(nullptr), cin.tie(nullptr); n   long long t_c = 1; n   cin >> t_c; n   while (t_c--) solve(); n } n


#include <bits/stdc++.h> n using namespace std; n int t1, n, i, j, a[202], f[202][404]; n int main() { n   scanf( %d , &t1); n   while (t1--) { n     scanf( %d , &n); n     for (i = 1; i <= n; i++) scanf( %d , &a[i]); n     sort(a + 1, a + n + 1); n     memset(f, 44, sizeof(f)); n     for (j = 0; j <= n * 2; j++) f[0][j] = 0; n     for (i = 1; i <= n; i++) n       for (j = 1; j <= n * 2; j++) { n         f[i][j] = min(f[i][j - 1], f[i - 1][j - 1] + abs(a[i] - j)); n       } n     printf( %d n , f[n][n * 2]); n   } n } n


#include <bits/stdc++.h> n int read() { n   int ans = 0, c, f = 1; n   while (!isdigit(c = getchar())) n     if (c ==  - ) f *= -1; n   do ans = ans * 10 + c -  0 ; n   while (isdigit(c = getchar())); n   return ans * f; n } n const int N = 1000050; n int n, a[N]; n long long S; n long long calc(long long p) { n   long long ans = 0, t = 0; n   for (int i = 0; i < n; ++i) { n     t = (t + a[i]) % p; n     ans += std::min(t, p - t); n   } n   return ans; n } n int main() { n   n = read(); n   for (int i = 0; i < n; ++i) S += a[i] = read(); n   if (S == 1) { n     puts( -1 ); n     return 0; n   } n   long long ans = 0x7fffffffffffffffLL; n   for (long long i = 2; i * i <= S; ++i) n     if (S % i == 0) { n       ans = std::min(ans, calc(i)); n       while (S % i == 0) S /= i; n     } n   if (S != 1) ans = std::min(ans, calc(S)); n   printf( %lld n , ans); n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e6; n vector<long long int> adj[N]; n long long int depth[N]; n void dfs(long long int n, long long int p) { n   for (auto x : adj[n]) { n     if (x != p) { n       depth[x] = depth[n] + 1; n       dfs(x, n); n     } n   } n } n void solve() { n   long long int n, a, b, da, db; n   cin >> n >> a >> b >> da >> db; n   for (long long int i = 1; i <= n; ++i) { n     adj[i].clear(); n     depth[i] = 0; n   } n   for (long long int i = 1; i < n; ++i) { n     long long int x, y; n     cin >> x >> y; n     adj[x].push_back(y); n     adj[y].push_back(x); n   } n   long long int maxi = -1, idx = -1; n   dfs(1, -1); n   for (long long int i = 1; i <= n; ++i) { n     if (depth[i] > maxi) { n       idx = i; n       maxi = depth[i]; n     } n   } n   for (long long int i = 1; i <= n; ++i) { n     depth[i] = 0; n   } n   dfs(idx, -1); n   for (long long int i = 1; i <= n; ++i) { n     if (depth[i] > maxi) { n       idx = i; n       maxi = depth[i]; n     } n   } n   for (long long int i = 1; i <= n; ++i) { n     depth[i] = 0; n   } n   if (2 * da >= min(maxi, db)) { n     cout <<  Alice  << endl; n     return; n   } n   dfs(a, -1); n   if (da >= depth[b]) { n     cout <<  Alice  << endl; n   } else { n     cout <<  Bob  << endl; n   } n } n int main() { n   ios_base::sync_with_stdio(0), cin.tie(NULL), cout.tie(NULL); n   int t = 1; n   cin >> t; n   while (t--) { n     solve(); n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 100005; n int sz = 1; n int t[N << 2], clr[N << 2]; n inline void pushclear(int k) { n   if (!clr[k]) return; n   t[k] = 0; n   if (k < sz) clr[k << 1] = clr[k << 1 | 1] = 1; n   clr[k] = 0; n } n inline void clear(int k, int l, int r, int a, int b) { n   pushclear(k); n   if (a > r || b < l) return; n   if (a >= l && b <= r) { n     clr[k] = 1; n     pushclear(k); n     return; n   } n   int mid = (a + b) >> 1; n   clear(k << 1, l, r, a, mid), clear(k << 1 | 1, l, r, mid + 1, b); n   t[k] = t[k << 1] + t[k << 1 | 1]; n } n inline void add(int k, int pos, int a, int b, int x) { n   pushclear(k); n   if (a == b) { n     t[k] += x; n     return; n   } n   int mid = (a + b) >> 1; n   if (pos <= mid) n     add(k << 1, pos, a, mid, x); n   else n     add(k << 1 | 1, pos, mid + 1, b, x); n   t[k] = t[k << 1] + t[k << 1 | 1]; n } n int n, init_x, init_y; n int a[N]; n vector<int> vec; n inline int getpos(int x) { n   return lower_bound(vec.begin(), vec.end(), x) - vec.begin() + 1; n } n bool check(int mid) { n   for (int i = 1; i < (sz << 1); i++) t[i] = clr[i] = 0; n   add(1, getpos(init_x), 1, sz, 1); n   add(1, getpos(init_y), 1, sz, 1); n   for (int i = 1; i <= n; i++) { n     int l = lower_bound(vec.begin(), vec.end(), a[i] - mid) - vec.begin() + 1; n     int r = lower_bound(vec.begin(), vec.end(), a[i] + mid) - vec.begin() + 1; n     if (r > vec.size() || vec[r - 1] > a[i] + mid) r--; n     if (l - 1 >= 1) clear(1, 1, l - 1, 1, sz); n     if (r + 1 <= sz) clear(1, r + 1, sz, 1, sz); n     if (t[1] == 0) return false; n     add(1, getpos(a[i]), 1, sz, 1); n   } n   return true; n } n int main() { n   scanf( %d%d%d , &n, &init_x, &init_y); n   for (int i = 1; i <= n; i++) { n     scanf( %d , &a[i]); n     vec.emplace_back(a[i]); n   } n   vec.emplace_back(init_x); n   vec.emplace_back(init_y); n   sort(vec.begin(), vec.end()); n   vec.resize(unique(vec.begin(), vec.end()) - vec.begin()); n   while (sz < vec.size()) sz <<= 1; n   int l = abs(init_x - init_y), r = 1000000000, mid, ans; n   while (l <= r) { n     mid = (l + r) >> 1; n     if (check(mid)) n       r = mid - 1, ans = mid; n     else n       l = mid + 1; n   } n   printf( %d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   cout <<  Black  << endl; n } n


#include <bits/stdc++.h> n #pragma comment(linker,  /STACK:836777216 ) n using namespace std; n int n, t, m; n int nextId = 0; n int max_online = 0; n int cur_online = 0; n deque<pair<int, int> > id_q; n int cnt[86400]; n vector<int> answer; n int main(void) { n   memset((cnt), (0), sizeof((cnt))); n   scanf( %d%d%d , &n, &m, &t); n   for (int(i) = 0; (i) < (n); (i)++) { n     int hh, mm, ss; n     scanf( %d:%d:%d , &hh, &mm, &ss); n     int time_ = hh * 60 * 60 + mm * 60 + ss; n     cnt[time_]++; n   } n   for (int(i) = 0; (i) < (86400); (i)++) { n     while ((int)(id_q).size() && (id_q.front().second + t == i)) { n       id_q.pop_front(); n       cur_online--; n     } n     if (cnt[i]) { n       for (int(j) = 0; (j) < (cnt[i]); (j)++) { n         if (cur_online < m) { n           id_q.push_back(make_pair(nextId++, i)); n           cur_online++; n           max_online = max(max_online, cur_online); n           answer.push_back(nextId); n         } else { n           id_q.back().second = i; n           answer.push_back(nextId); n         } n       } n     } n   } n   if (max_online != m) { n     printf( No solution n ); n   } else { n     printf( %d n , nextId); n     for (int(i) = 0; (i) < ((int)(answer).size()); (i)++) { n       printf( %d n , answer[i]); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int t; n   cin >> t; n   for (int i = 0; i < t; i++) { n     int a1; n     cin >> a1; n     cout << 1 <<     << (a1 - 1); n     cout << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long N = 1e5 + 10; n long long n, a, cnt, ans, sum, l, r; n string g; n void DNM() { n   cin >> n; n   cin >> g; n   sort(g.begin(), g.end()); n   cout << g << endl; n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(NULL); n   cout.tie(NULL); n   int Qu_l_uQ = 1; n   while (Qu_l_uQ--) DNM(); n } n


#include <bits/stdc++.h> n using namespace std; n long long t[2300]; n int main() { n   int n, k; n   scanf( %d%d , &n, &k); n   for (int i = 0; i < n; i++) scanf( %lld , &t[i]); n   sort(t, t + n); n   long long sum = 0; n   for (int i = 0; i < k; i++) sum += t[i]; n   printf( %lld n , sum); n } n


#include <bits/stdc++.h> n using namespace std; n int M = 1000000007; n map<string, long long int> b1; n int main() { n   ios ::sync_with_stdio(false); n   cin.tie(0); n   long long int n, k; n   cin >> n >> k; n   long long int c = max(n - 2 * k, 0ll); n   long long int a = n * (n - 1) / 2 - c * (c - 1) / 2; n   cout << a << endl; n } n


#include <bits/stdc++.h> n int main() { n   int m, n, a; n   scanf( %d %d , &n, &m); n   if (m % 2 == 0 || n % 2 == 0) n     printf( %d , m * n / 2); n   else if (n >= m) n     printf( %d , (n - 1) * m / 2 + (m - 1) / 2); n   else if (n < m) n     printf( %d , (m - 1) * n / 2 + (n - 1) / 2); n } n


#include <bits/stdc++.h> n using namespace std; n int Sky[105][105][15]; n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   int n, q, c; n   cin >> n >> q >> c; n   c++; n   for (int i = 1; i <= n; i++) { n     int x, y, s; n     cin >> x >> y >> s; n     for (int r = 0; r < c; r++) Sky[x][y][r] += (s + r) % c; n   } n   for (int x = 1; x <= 100; x++) n     for (int y = 1; y <= 100; y++) n       for (int r = 0; r < c; r++) n         Sky[x][y][r] = Sky[x][y][r] + Sky[x - 1][y][r] + Sky[x][y - 1][r] - n                        Sky[x - 1][y - 1][r]; n   for (int i = 1; i <= q; i++) { n     int t, x1, y1, x2, y2; n     cin >> t >> x1 >> y1 >> x2 >> y2; n     t %= c; n     cout << Sky[x2][y2][t] - Sky[x1 - 1][y2][t] - Sky[x2][y1 - 1][t] + n                 Sky[x1 - 1][y1 - 1][t] n          <<   n ; n   } n } n