#include <bits/stdc++.h> n using namespace std; n const int maxn = 2e5 + 5; n int A[maxn][2]; n bool vis[maxn]; n vector<int> v[maxn]; n int main() { n   int T; n   cin >> T; n   while (T--) { n     int n; n     cin >> n; n     for (int i = 1; i <= n; i++) { n       v[i].clear(); n     } n     for (int i = 1; i <= n; i++) { n       scanf( %d , &A[i][0]); n       v[A[i][0]].push_back(i); n     } n     for (int i = 1; i <= n; i++) { n       scanf( %d , &A[i][1]); n       v[A[i][1]].push_back(i); n     } n     int bad = 0; n     for (int i = 1; i <= n; i++) { n       vis[i] = 0; n       if (v[i].size() != 2) { n         bad = 1; n         break; n       } n     } n     if (bad) { n       printf( -1 n ); n       continue; n     } n     vector<int> ans; n     ans.clear(); n     for (int i = 1; i <= n; i++) { n       if (A[i][0] == A[i][1]) { n         vis[i] = 1; n         continue; n       } n       if (vis[i]) { n         continue; n       } n       int now = i; n       vector<int> v1, v2; n       int st = now; n       v1.push_back(now); n       while (!vis[now]) { n         vis[now] = 1; n         int b = A[now][1]; n         int nex; n         if (v[b][0] == now) n           nex = v[b][1]; n         else n           nex = v[b][0]; n         now = nex; n         if (now == st) break; n         if (A[now][0] == b) { n           v1.push_back(now); n         } else { n           v2.push_back(now); n           swap(A[now][0], A[now][1]); n         } n       } n       if (v1.size() < v2.size()) { n         for (auto j : v1) { n           ans.push_back(j); n         } n       } else { n         for (auto j : v2) { n           ans.push_back(j); n         } n       } n     } n     printf( %d n , ans.size()); n     for (auto i : ans) { n       printf( %d  , i); n     } n     puts(  ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int parent[100010]; n int Rank[100010]; n int cnt; n int st[100010]; n int st_cnt = 0; n void rollback(int id) { n   if (id < 0) n     Rank[-id - 1]--; n   else { n     parent[id] = id; n     cnt++; n   } n } n int find(int cur) { n   if (parent[cur] == cur) return cur; n   return find(parent[cur]); n } n void Union(int x, int y, bool revert = false) { n   int xroot = find(x); n   int yroot = find(y); n   if (xroot == yroot) return; n   cnt--; n   if (Rank[xroot] < Rank[yroot]) swap(xroot, yroot); n   if (revert) st[st_cnt++] = yroot; n   parent[yroot] = xroot; n   if (Rank[yroot] == Rank[xroot]) { n     Rank[xroot]++; n     if (revert) st[st_cnt++] = -xroot - 1; n   } n } n void init(int n) { n   st_cnt = 0; n   cnt = n; n   for (int i = 0; i <= n; ++i) { n     Rank[i] = 0; n     parent[i] = i; n   } n } n int x[100010], y[100010], Out[100010]; n vector<int> List[600][600]; n vector<int> befor[100010], after[100010]; n int main() { n   int n, k, m, q, i, j; n   scanf( %d , &n); n   scanf( %d , &k); n   scanf( %d , &m); n   while (m--) { n     int x, y; n     scanf( %d , &x); n     scanf( %d , &y); n     --x; n     --y; n     if (x < y) { n       after[x].push_back(y); n       befor[y].push_back(x); n     } else { n       after[y].push_back(x); n       befor[x].push_back(y); n     } n   } n   init(n); n   scanf( %d , &q); n   for (i = 0; i < q; i++) { n     scanf( %d , &x[i]); n     scanf( %d , &y[i]); n     --x[i]; n     --y[i]; n     if (x[i] >= n) { n       Out[i] = 0; n       continue; n     } n     if (y[i] >= n) y[i] = n - 1; n     int sb = x[i] / 500; n     if (sb * 500 != x[i]) sb++; n     int eb = y[i] / 500; n     if ((eb + 1) * 500 != y[i] + 1) eb--; n     if (eb < sb) { n       for (j = x[i]; j <= y[i]; j++) n         for (auto z : befor[j]) n           if (z >= x[i]) Union(j, z, true); n       Out[i] = cnt; n       while (st_cnt) { n         rollback(st[--st_cnt]); n       } n     } else n       List[sb][eb].push_back(i); n   } n   for (i = 0; i * 500 < n; i++) { n     init(n); n     for (j = i; (j + 1) * 500 <= n; j++) { n       for (int k = j * 500; k < (j + 1) * 500; k++) n         for (auto x : befor[k]) n           if (x >= i * 500) Union(k, x); n       for (int k = 0; k < List[i][j].size(); k++) { n         for (int l = x[List[i][j][k]]; l < i * 500; l++) n           for (auto x : after[l]) n             if (x < (j + 1) * 500) Union(l, x, true); n         for (int l = (j + 1) * 500; l <= y[List[i][j][k]]; l++) n           for (auto z : befor[l]) n             if (z >= x[List[i][j][k]]) Union(l, z, true); n         Out[List[i][j][k]] = cnt; n         while (st_cnt) { n           rollback(st[--st_cnt]); n         } n       } n     } n   } n   for (i = 0; i < q; i++) printf( %d n , Out[i] - (n - (y[i] - x[i] + 1))); n } n


#include <bits/stdc++.h> n using namespace std; n namespace input { n const int InputBufferSize = (1 << 24) + 5; n char buffer[InputBufferSize], *s, *eof; n inline void init() { n   assert(stdin != NULL); n   s = buffer; n   eof = s + fread(buffer, 1, InputBufferSize, stdin); n } n inline bool read(int& x) { n   x = 0; n   int flag = 1; n   while (!isdigit(*s) && *s !=  - ) s++; n   if (eof <= s) return false; n   if (*s ==  - ) flag = -1, s++; n   while (isdigit(*s)) x = x * 10 + *s++ -  0 ; n   x *= flag; n   return true; n } n inline bool read(char* str) { n   *str = 0; n   while (isspace(*s)) s++; n   if (eof < s) return false; n   while (!isspace(*s)) *str = 0, *str = *s, str++, s++; n   *str = 0; n   return true; n } n }  // namespace input n using namespace input; n int const p = 1e9 + 7; n int pr[305], cnt, a[400005], tmp[65], id[305], c[305], tree[1600005], n     tag[1600005]; n long long b[400005], tree2[1600005], tag2[1600005]; n bool flag[305]; n long long res(int x) { n   memset(tmp, 0, sizeof(tmp)); n   long long res = 0; n   for (int i = 0; i <= 6; i++) n     while (!(x % pr[i])) x /= pr[i], res |= (1ll << i); n   if (x != 1) res |= (1ll << id[x]); n   return res; n } n int pw(int x, int y) { n   int res = 1; n   while (y) { n     if (y & 1) res = 1ll * res * x % p; n     x = 1ll * x * x % p; n     y >>= 1; n   } n   return res; n } n void build(int l, int r, int x) { n   if (l == r) { n     tree[x] = a[l]; n     tag[x] = 1; n     return; n   } n   int mid = (l + r) >> 1; n   build(l, mid, x * 2), build(mid + 1, r, x * 2 + 1); n   tree[x] = 1ll * tree[x * 2] * tree[x * 2 + 1] % p; n   tag[x] = 1; n } n void build2(int l, int r, int x) { n   if (l == r) { n     tree2[x] = b[l]; n     return; n   } n   int mid = (l + r) >> 1; n   build2(l, mid, x * 2), build2(mid + 1, r, x * 2 + 1); n   tree2[x] = (tree2[x * 2] | tree2[x * 2 + 1]); n } n void pushdown(int l, int r, int x) { n   if (tag[x] == 1) return; n   tree[x] = 1ll * tree[x] * pw(tag[x], r - l + 1) % p; n   if (l != r) n     tag[x * 2] = 1ll * tag[x * 2] * tag[x] % p, n             tag[x * 2 + 1] = 1ll * tag[x * 2 + 1] * tag[x] % p; n   tag[x] = 1; n } n void modify(int l, int r, int x, int a, int b, int c) { n   if (l >= a && r <= b) { n     tag[x] = 1ll * tag[x] * c % p; n     return; n   } n   int mid = (l + r) >> 1; n   if (a <= mid) modify(l, mid, x * 2, a, b, c); n   if (b > mid) modify(mid + 1, r, x * 2 + 1, a, b, c); n   pushdown(l, mid, x * 2), pushdown(mid + 1, r, x * 2 + 1); n   tree[x] = 1ll * tree[x * 2] * tree[x * 2 + 1] % p; n } n void pushdown2(int l, int r, int x) { n   if (!tag2[x]) return; n   tree2[x] |= tag2[x]; n   if (l != r) tag2[x * 2] |= tag2[x], tag2[x * 2 + 1] |= tag2[x]; n   tag2[x] = 0; n } n void modify2(int l, int r, int x, int a, int b, long long c) { n   if (l >= a && r <= b) { n     tag2[x] = (tag2[x] | c); n     return; n   } n   int mid = (l + r) >> 1; n   if (a <= mid) modify2(l, mid, x * 2, a, b, c); n   if (b > mid) modify2(mid + 1, r, x * 2 + 1, a, b, c); n   pushdown2(l, mid, x * 2), pushdown2(mid + 1, r, x * 2 + 1); n   tree2[x] = (tree2[x * 2] | tree2[x * 2 + 1]); n } n int ask(int l, int r, int x, int a, int b) { n   pushdown(l, r, x); n   if (l >= a && r <= b) return tree[x]; n   int mid = (l + r) >> 1, res = 1; n   if (a <= mid) res = ask(l, mid, x * 2, a, b); n   if (b > mid) res = 1ll * res * ask(mid + 1, r, x * 2 + 1, a, b) % p; n   return res; n } n long long ask2(int l, int r, int x, int a, int b) { n   pushdown2(l, r, x); n   if (l >= a && r <= b) return tree2[x]; n   int mid = (l + r) >> 1; n   long long res = 0; n   if (a <= mid) res = ask2(l, mid, x * 2, a, b); n   if (b > mid) res = (res | ask2(mid + 1, r, x * 2 + 1, a, b)); n   return res; n } n char op[15]; n int main() { n   for (int i = 2; i <= 300; i++) { n     if (!flag[i]) n       pr[cnt] = i, id[i] = cnt, c[cnt] = 1ll * (i - 1) * pw(i, p - 2) % p, n       cnt++; n     for (int j = i * 2; j <= 300; j += i) flag[j] = 1; n   } n   init(); n   int n, q, l, r, x; n   long long y; n   read(n), read(q); n   for (int i = 1; i <= n; i++) { n     read(a[i]); n     b[i] = res(a[i]); n   } n   build(1, n, 1), build2(1, n, 1); n   while (q--) { n     read(op); n     if (op[0] ==  M ) { n       read(l), read(r), read(x); n       y = res(x); n       modify(1, n, 1, l, r, x), modify2(1, n, 1, l, r, y); n     } else { n       read(l), read(r); n       x = ask(1, n, 1, l, r), y = ask2(1, n, 1, l, r); n       for (int j = 0; j <= 61; j++) n         if (y & (1ll << j)) x = 1ll * x * c[j] % p; n       printf( %d n , x); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long OO = (long long)1e9 + 7; n using namespace std; n long long inv(long long a, long long b) { n   return 1 < a ? b - inv(b % a, a) * b / a : 1; n } n long long Pow(long long B, long long P) { n   long long R = 1; n   while (P > 0) { n     if (P & 1) R = (R * B); n     P /= 2; n     B = (B * B); n   } n   return R; n } n long long GCD(long long a, long long b) { n   while (b) { n     b ^= a ^= b ^= a %= b; n   } n   return a; n } n long long LCM(long long a, long long b) { return (a / GCD(a, b) * b); } n long long BigMod(long long B, long long P, long long M) { n   long long R = 1; n   while (P > 0) { n     if (P & 1) { n       R = (R * B) % M; n     } n     P /= 2; n     B = (B * B) % M; n   } n   return (long long)R; n } n vector<long long> primes; n void factorization(long long n) { n   for (long long i = 2; i * i <= n; ++i) n     while (n % i == 0) primes.push_back(i), n /= i; n   if (n > 1) primes.push_back(n); n } n int main() { n   ios::sync_with_stdio(0); n   long long n; n   cin >> n; n   factorization(n); n   if (primes.size() == 2) { n     cout << 2; n   } else { n     if (n == 1 || primes.size() == 1) { n       cout <<  1 n0 ; n     } else { n       cout << 1 << endl; n       cout << primes[0] * primes[1]; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n int main() { n   int n, m, i, j, s; n   char a[12][11]; n   scanf( %d%d , &n, &m); n   for (i = 0; i < n; i++) scanf( %s , a[i]); n   s = 0; n   for (i = 0; i < n; i++) n     for (j = 0; j < m; j++) n       if (a[i][j] ==  W ) { n         if (a[i - 1][j] ==  P ) { n           s++; n           a[i - 1][j] =  . ; n           continue; n         } n         if (a[i + 1][j] ==  P ) { n           s++; n           a[i + 1][j] =  . ; n           continue; n         } n         if (a[i][j - 1] ==  P ) { n           s++; n           a[i][j - 1] =  . ; n           continue; n         } n         if (a[i][j + 1] ==  P ) { n           s++; n           a[i][j + 1] =  . ; n           continue; n         } n       } n   printf( %d , s); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<pair<int, pair<int, int> > > V; n const int maxn = 1010; n int n, N; n int a[maxn][maxn], r[maxn], c[maxn]; n void sr(int x, int y) { n   if (x == y) return; n   V.push_back(make_pair(1, pair<int, int>(x, y))); n   for (int i = 1; i <= N; ++i) swap(a[x][i], a[y][i]); n   swap(r[x], r[y]); n } n void sc(int x, int y) { n   if (x == y) return; n   V.push_back(make_pair(2, pair<int, int>(x, y))); n   for (int i = 1; i <= N; ++i) swap(a[i][x], a[i][y]); n   swap(c[x], c[y]); n } n int main() { n   int x, y, i, j; n   scanf( %d , &n), N = n; n   for (i = 1; i < n; ++i) { n     scanf( %d%d , &x, &y); n     a[x][y] = 1, ++r[x], ++c[y]; n   } n   while (n > 1) { n     for (i = 1; i <= n; ++i) { n       for (j = i; j <= n; ++j) n         if (a[i][j]) break; n       if (a[i][j]) break; n     } n     if (!a[i][j]) break; n     sr(i, n), sc(j, 1); n     for (i = 1; i <= n; ++i) n       if (!c[i]) break; n     sc(i, n); n     for (i = 1; i <= n; ++i) c[i] -= a[n][i], r[i] -= a[i][n]; n     --n; n   } n   printf( %d n , V.size()); n   for (typeof(V.end()) i = V.begin(); i != V.end(); ++i) n     printf( %d %d %d n , i->first, (i->second).first, (i->second).second); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int mxn = 100000; n long long n, k; n int a[2][mxn], first[mxn]; n vector<pair<int, int> > v; n void sol() { n   cout << k <<   n ; n   for (pair<int, int> i : v) cout << (i.first + 1) <<     << i.second <<   n ; n   exit(0); n } n void add(int x, int y) { n   if (a[0][x + 1] + y < 0 || a[0][x + 1] + y > 9) add(x + 1, -y); n   if (v.size() == mxn) sol(); n   a[0][x] += y, a[0][x + 1] += y; n   v.push_back({x, y}); n } n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cin >> n; n   for (int i = 0; i < 2; i++) n     for (int j = 0; j < n; j++) { n       char c; n       cin >> c; n       a[i][j] = c -  0 ; n     } n   memcpy(first, a[0], sizeof(first)); n   for (int i = 0; i < n - 1; i++) { n     int x = a[1][i] - first[i]; n     k += abs(x), first[i] += x, first[i + 1] += x; n   } n   if (a[1][n - 1] != first[n - 1]) k = -1, sol(); n   for (int i = 0; i < n - 1; i++) n     while (a[0][i] != a[1][i]) { n       add(i, 2 * (a[0][i] < a[1][i]) - 1); n     } n   sol(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n bool dbg = 0; n int main() { n   int N, K; n   cin >> N >> K; n   long long Count = 0; n   int x, y; n   for (int n = 0; n < N; ++n) { n     scanf( %d%d , &x, &y); n     Count += (long long)(y - x + 1); n   } n   cout << (K - Count % K) % K << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long n, s, s1, m, k, sum = 0, l, r, a, a1, p; n const int N = 1e5 + 1; n int main() { n   int q; n   cin >> q; n   while (q--) { n     cin >> l >> r; n     if (l % 2) { n       if (r % 2) { n         p = (r / 2) - (l / 2) + 1; n         s = (l + r) * p; n         s /= 2; n         a = l + 1; n         a1 = r - 1; n         p--; n         s1 = (a + a1) * p; n         s1 /= 2; n         sum = s1 - s; n         cout << sum << endl; n       } else { n         a1 = r - 1; n         p = (r / 2) - (l / 2); n         s = (l + a1) * p; n         s /= 2; n         a = l + 1; n         s1 = (a + r) * p; n         s1 /= 2; n         sum = s1 - s; n         cout << sum << endl; n       } n     } else { n       if (r % 2) { n         a = l + 1; n         a1 = r; n         p = (r / 2) + 1; n         p = p - (l / 2); n         s = (a + a1) * p; n         s /= 2; n         a1 = r - 1; n         s1 = (l + a1) * p; n         s1 /= 2; n         sum = s1 - s; n         cout << sum << endl; n       } else { n         p = (r / 2) - (l / 2) + 1; n         s1 = (l + r) * p; n         s1 /= 2; n         p--; n         l++; n         r--; n         s = (l + r) * p; n         s /= 2; n         sum = s1 - s; n         cout << sum << endl; n       } n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n struct Swap { n   int i; n   int j; n }; n std::vector<Swap> swaps; n void solve4(int a, int b, int c, int d, bool firstLast) { n   if (firstLast) swaps.push_back(Swap{a, b}); n   swaps.push_back(Swap{a, c}); n   swaps.push_back(Swap{b, d}); n   swaps.push_back(Swap{a, d}); n   swaps.push_back(Swap{b, c}); n   if (firstLast) swaps.push_back(Swap{c, d}); n } n void solve5(int a, int b, int c, int d, int e, bool firstLast) { n   if (firstLast) { n     swaps.push_back(Swap{a, b}); n     swaps.push_back(Swap{a, c}); n     swaps.push_back(Swap{b, c}); n   } n   swaps.push_back(Swap{b, e}); n   swaps.push_back(Swap{a, d}); n   swaps.push_back(Swap{c, e}); n   swaps.push_back(Swap{c, d}); n   swaps.push_back(Swap{a, e}); n   swaps.push_back(Swap{b, d}); n   if (firstLast) swaps.push_back(Swap{d, e}); n } n int main(void) { n   int n; n   scanf( %d , &n); n   if (n == 1) { n     printf( YES n ); n   } else if (n % 4 <= 1) { n     if (n % 4 == 0) { n       int k = n / 4; n       for (int i = 0; i < k; i++) { n         solve4(4 * i + 1, 4 * i + 2, 4 * i + 3, 4 * i + 4, true); n       } n       for (int i = 0; i + 1 < k; i++) { n         for (int j = i + 1; j < k; j++) { n           solve4(4 * i + 1, 4 * i + 2, 4 * j + 1, 4 * j + 2, false); n           solve4(4 * i + 1, 4 * i + 2, 4 * j + 3, 4 * j + 4, false); n           solve4(4 * i + 3, 4 * i + 4, 4 * j + 1, 4 * j + 2, false); n           solve4(4 * i + 3, 4 * i + 4, 4 * j + 3, 4 * j + 4, false); n         } n       } n     } else { n       int k = (n - 5) / 4; n       solve5(1, 2, 3, 4, 5, true); n       for (int i = 0; i < k; i++) { n         solve4(5 + 4 * i + 1, 5 + 4 * i + 2, 5 + 4 * i + 3, 5 + 4 * i + 4, n                true); n       } n       for (int i = 0; i < k; i++) { n         solve5(1, 2, 3, 5 + 4 * i + 1, 5 + 4 * i + 2, false); n         solve5(1, 2, 3, 5 + 4 * i + 3, 5 + 4 * i + 4, false); n         solve4(4, 5, 5 + 4 * i + 1, 5 + 4 * i + 2, false); n         solve4(4, 5, 5 + 4 * i + 3, 5 + 4 * i + 4, false); n       } n       for (int i = 0; i + 1 < k; i++) { n         for (int j = i + 1; j < k; j++) { n           solve4(5 + 4 * i + 1, 5 + 4 * i + 2, 5 + 4 * j + 1, 5 + 4 * j + 2, n                  false); n           solve4(5 + 4 * i + 1, 5 + 4 * i + 2, 5 + 4 * j + 3, 5 + 4 * j + 4, n                  false); n           solve4(5 + 4 * i + 3, 5 + 4 * i + 4, 5 + 4 * j + 1, 5 + 4 * j + 2, n                  false); n           solve4(5 + 4 * i + 3, 5 + 4 * i + 4, 5 + 4 * j + 3, 5 + 4 * j + 4, n                  false); n         } n       } n     } n     printf( YES n ); n     for (Swap swap : swaps) printf( %d %d n , swap.i, swap.j); n     printf(  n ); n   } else { n     printf( NO n ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m, k, res, xx, yy; n int main() { n   scanf( %d%d%d , &n, &m, &k); n   while (k--) { n     scanf( %d%d , &xx, &yy); n     int mn = xx - 1; n     mn = min(mn, n - xx); n     mn = min(mn, yy - 1); n     mn = min(mn, m - yy); n     res = max(res, (int)(mn <= 4)); n   } n   if (res) n     printf( YES ); n   else n     printf( NO ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int N, ap[3009], dp[3009], how[3009]; n char sir[3009]; n bool cons(char c) { n   if (c ==  a  || c ==  e  || c ==  i  || c ==  o  || c ==  u ) return 0; n   return 1; n } n int main() { n   gets(sir + 1), N = strlen(sir + 1); n   dp[0] = 0; n   for (int i = 1; i <= N; i++) { n     char mini = sir[i], maxi = sir[i]; n     int L = cons(sir[i]); n     dp[i] = dp[i - 1] + 1, how[i] = i; n     for (int j = i - 1; j >= 1; j--) { n       if (cons(sir[j]) == 0) n         L = 0; n       else { n         if (L == 0) n           mini = maxi = sir[j], L = 1; n         else n           L++, mini = min(mini, sir[j]), maxi = max(maxi, sir[j]); n       } n       if (L >= 3 && mini < maxi) break; n       if (dp[j - 1] + 1 < dp[i]) dp[i] = dp[j - 1] + 1, how[i] = j; n     } n   } n   int pos = N; n   while (pos > 0) pos = how[pos] - 1, ap[pos] = 1; n   for (int i = 1; i <= N; i++) { n     printf( %c , sir[i]); n     if (ap[i]) printf(   ); n   } n   printf(  n ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long n, x, a[5005], uc, sum, mux = 0; n vector<long long> myvec; n int main() { n   cin >> n; n   for (int i = 1; i <= n; i++) cin >> a[i]; n   for (int i = 1; i <= n; i++) { n     sum = 0; n     for (int j = i; j <= n; j++) { n       sum += a[j]; n       if ((j - i + 1) * 100 < sum) mux = max(mux, (long long)(j - i + 1)); n     } n   } n   cout << mux; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int X; n vector<int> E[100010]; n int all, all2; n int v[800 + 10]; n int dp[800 + 10], dp2[800 + 10]; n void calc(int x) { n   for (int i = 0; i <= 800 - 1; ++i) v[i] = 0; n   for (int i = 0; i < E[x].size(); ++i) v[E[x][i]] = 1; n   for (int i = 0; i <= 800 - 1; ++i) dp2[i] = (1 << 29); n   int cost = 0; n   for (int i = 800 - 1; i >= 0; --i) { n     if (v[i]) cost += 3; n     int tmp = cost + dp[max(0, i - 1)]; n     if (tmp < dp2[i]) dp2[i] = tmp; n     if (i > 0) tmp += i * (i + 1) / 2 + 2; n     if (tmp < dp2[0]) dp2[0] = tmp; n   } n   for (int i = 0; i <= 800 - 1; ++i) dp[i] = dp2[i]; n   for (int i = 0; i <= 800 - 1; ++i) dp[i + 1] = min(dp[i + 1], dp[i]); n } n int main() { n   int K, i, j, ans = 0; n   cin >> X >> K; n   for (int i = 1; i <= K; ++i) { n     int r, c; n     scanf( %d%d , &r, &c); n     int x = c - 1; n     int y = X - r; n     if (y < 800) n       E[x].push_back(y); n     else n       ans += 3; n   } n   for (i = X - 1; i >= 0; i--) calc(i); n   cout << dp[0] + ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int N, K; n void init() { scanf( %d%d , &N, &K); } n void work() { n   if (K == 1 && N > 1) { n     printf( -1 ); n     return; n   } n   if (K == 1 && N == 1) { n     printf( a ); n     return; n   } n   if (N < K) { n     printf( -1 ); n     return; n   } n   if (N == K) { n     for (int i = 1; i <= K; i++) printf( %c ,  a  + i - 1); n     return; n   } n   for (int i = 1; i <= N - K + 2; i++) { n     if (i & 1) n       printf( a ); n     else n       printf( b ); n   } n   char j =  c ; n   for (int i = N - K + 3; i <= N; i++) printf( %c , j++); n } n int main() { n   init(); n   work(); n   printf(  n ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long p = 998244353; n int gcd(int a, int b, int& x, int& y) { n   if (a == 1) { n     x = 1; n     y = 0; n     return 1; n   } n   if (b == 1) { n     x = 0; n     y = 1; n     return 1; n   } n   if (b == 0) { n     x = 1; n     y = 0; n     return a; n   } n   int res = gcd(b, a % b, y, x); n   y = y - x * (a / b); n   return res; n } n long long Denominator(long long a) { n   int x = 0, y = 0; n   if (a == 0) return 1; n   gcd(p, a, x, y); n   return (y + p) % p; n } n int main() { n   long long n, res = 1; n   cin >> n; n   for (long long i = 1; i <= n; ++i) { n     res *= (n + i); n     res = res % p; n     res *= Denominator(i); n     res = res % p; n   } n   long long sum = 0; n   vector<long long> a(2 * n); n   for (int i = 0; i < 2 * n; ++i) { n     cin >> a[i]; n   } n   sort(a.begin(), a.end()); n   for (int i = 0; i < n; ++i) { n     sum += a[2 * n - 1 - i] - a[i]; n   } n   cout << res * (sum % p) % p << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct pp { n   long long nu, we; n } c; n struct Info { n   long long l, r, ta, nu; n   pp a; n } a[1000010]; n long long b[100010], n, ansn, m, l, r, opt, x, sum; n pp mi(pp a, pp b) { n   if (a.nu < b.nu) return b; n   return a; n } n pp ga(long long nu, long long l, long long r) { n   if (a[nu].l == l && a[nu].r == r) return a[nu].a; n   long long mid = (a[nu].l + a[nu].r) / 2; n   pp u; n   u.nu = -1e18; n   if (l <= mid) u = mi(u, ga(nu * 2, l, min(mid, r))); n   if (r > mid) u = mi(u, ga(nu * 2 + 1, max(mid + 1, l), r)); n   return u; n } n void build(long long x, long long l, long long r) { n   a[x].l = l; n   a[x].r = r; n   a[x].ta = 0; n   if (l == r) { n     a[x].nu = b[l]; n     a[x].a.we = l; n     if (l != 0 && l != n + 1) { n       a[x].a.nu = b[l] - max(b[l + 1], b[l - 1]); n     } else n       a[x].a.nu = -1e18; n   } else { n     build(x * 2, l, (l + r) / 2); n     build(x * 2 + 1, (l + r) / 2 + 1, r); n     if (a[x * 2].a.nu >= a[x * 2 + 1].a.nu) n       a[x].a = a[x * 2].a; n     else n       a[x].a = a[x * 2 + 1].a; n   } n } n void ya(long long x) { n   if (a[x].ta != 0 && a[x].l != a[x].r) { n     a[x * 2].ta += a[x].ta; n     a[x * 2 + 1].ta += a[x].ta; n     a[x].ta = 0; n   } n } n long long que(long long nu, long long x) { n   if (a[nu].l == a[nu].r) { n     a[nu].nu += a[nu].ta; n     a[nu].ta = 0; n     return a[nu].nu; n   } else { n     ya(nu); n     if (x <= (a[nu].l + a[nu].r) / 2) return que(nu * 2, x); n     return que(nu * 2 + 1, x); n   } n } n void add(long long nu, long long l, long long r, long long x) { n   if (a[nu].l == l && a[nu].r == r) { n     a[nu].ta += x; n     return; n   } n   long long mid = (a[nu].l + a[nu].r) / 2; n   if (l <= mid) add(nu * 2, l, min(mid, r), x); n   if (r > mid) add(nu * 2 + 1, max(mid + 1, l), r, x); n } n void chang(long long nu, long long x, long long y) { n   if (a[nu].l == a[nu].r) { n     a[nu].a.nu = y; n   } else { n     if (x <= (a[nu].l + a[nu].r) / 2) n       chang(nu * 2, x, y); n     else n       chang(nu * 2 + 1, x, y); n     if (a[nu * 2].a.nu >= a[nu * 2 + 1].a.nu) n       a[nu].a = a[nu * 2].a; n     else n       a[nu].a = a[nu * 2 + 1].a; n   } n } n long long cou(pp a) { n   return ansn - abs(que(1, a.we - 1) - que(1, a.we)) - n          abs(que(1, a.we + 1) - que(1, a.we)) + n          abs(que(1, a.we - 1) - que(1, a.we) - x) + n          abs(que(1, a.we + 1) - que(1, a.we) - x); n } n void chan(long long x) { n   if (x < 1 || x > n) return; n   long long y = que(1, x) - max(que(1, x + 1), que(1, x - 1)); n   chang(1, x, y); n } n int main() { n   scanf( %d , &n); n   for (long long i = 1; i <= n; i++) scanf( %d , &b[i]); n   b[0] = b[1]; n   b[n + 1] = b[n]; n   for (long long i = 1; i < n; i++) ansn += abs(b[i + 1] - b[i]); n   build(1, 0, n + 1); n   scanf( %d , &m); n   while (m--) { n     scanf( %d %d %d %d , &opt, &l, &r, &x); n     if (opt == 1) { n       c = ga(1, l, r); n       if (c.nu >= 0) n         cout << ansn + x * 2 << endl; n       else { n         sum = cou(c); n         if (l <= c.we - 1) sum = max(sum, cou(ga(1, l, c.we - 1))); n         if (c.we + 1 <= r) sum = max(sum, cou(ga(1, c.we + 1, r))); n         cout << sum << endl; n       } n     } else { n       ansn -= abs(que(1, l - 1) - que(1, l)); n       ansn -= abs(que(1, r + 1) - que(1, r)); n       if (l == 1) l--; n       if (r == n) r--; n       add(1, l, r, x); n       if (l == 0) l++; n       if (r == n + 1) r++; n       chan(l); n       chan(l - 1); n       chan(r); n       chan(r + 1); n       ansn += abs(que(1, l - 1) - que(1, l)); n       ansn += abs(que(1, r + 1) - que(1, r)); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n #pragma GCC optimize( Ofast ) n #pragma GCC target( sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,fma ) n #pragma GCC optimize( unroll-loops ) n using namespace std; n long long MOD = 998244353; n double eps = 1e-12; n long long mpow(long long a, long long b) { n   if (a == 0) return 0; n   if (b == 0) return 1; n   long long t1 = mpow(a, b / 2); n   t1 *= t1; n   t1 %= MOD; n   if (b % 2) t1 *= a; n   t1 %= MOD; n   return t1; n } n long long mpow(long long a, long long b, long long p) { n   if (a == 0) return 0; n   if (b == 0) return 1; n   long long t1 = mpow(a, b / 2, p); n   t1 *= t1; n   t1 %= p; n   if (b % 2) t1 *= a; n   t1 %= p; n   return t1; n } n long long modinverse(long long a, long long m) { n   long long m0 = m; n   long long y = 0, x = 1; n   if (m == 1) return 0; n   while (a > 1) { n     long long q = a / m; n     long long t = m; n     m = a % m, a = t; n     t = y; n     y = x - q * y; n     x = t; n   } n   if (x < 0) x += m0; n   return x; n } n mt19937_64 mt(chrono::steady_clock::now().time_since_epoch().count()); n long long range(long long l, long long r) { return l + mt() % (r - l + 1); } n long long rev(long long v) { return mpow(v, MOD - 2); } n void solve() { n   long long n; n   cin >> n; n   long long a[n]; n   long long alpha = 0, sign = 1; n   set<long long> s; n   long long st = -1, en = -1; n   long long ans = 1; n   for (long long i = 0; i < n; i++) { n     cin >> a[i]; n   } n   if (a[0] % 2) { n     st = 1; n     en = a[0] - 1; n     ans++; n   } else { n     st = a[0] / 2; n     en = a[0] / 2; n   } n   for (long long i = 1; i < n; i++) { n     long long alpha1 = a[i] - alpha; n     long long sign1 = -sign; n     if (a[i] % 2) { n       ans++; n       long long val = a[i] - alpha; n       if (sign > 0) { n         auto it = s.lower_bound(val); n         while (it != s.end()) { n           s.erase(it); n           it = s.lower_bound(val); n         } n       } else { n         auto it = s.lower_bound(-val + 1); n         while (it != s.begin()) { n           it--; n           s.erase(it); n           it = s.lower_bound(-val + 1); n         } n       } n       if (st >= a[i] && ((long long)(s).size()) == 0) { n         ans++; n         st = 1, en = a[i] - 1; n       } else if (st >= a[i]) { n         long long val = alpha1 + sign1 * (*(s.begin())); n         s.erase(s.begin()); n         st = val, en = val; n       } else { n         long long t1 = a[i] - st, t2 = a[i] - en; n         st = max((long long)1, t2), en = max((long long)1, t1); n       } n     } else { n       long long mid = sign * (a[i] / 2 - alpha); n       if (s.find(mid) != s.end() || (st <= a[i] / 2 && en >= a[i] / 2)) { n         s.clear(); n         st = a[i] / 2; n         en = a[i] / 2; n       } else { n         ans++; n         long long val = a[i] - alpha; n         if (sign > 0) { n           auto it = s.lower_bound(val); n           while (it != s.end()) { n             s.erase(it); n             it = s.lower_bound(val); n           } n         } else { n           auto it = s.lower_bound(-val + 1); n           while (it != s.begin()) { n             it--; n             s.erase(it); n             it = s.lower_bound(-val + 1); n           } n         } n         if (st >= a[i] && ((long long)(s).size()) == 0) { n           st = a[i] / 2, en = a[i] / 2; n         } else if (st >= a[i]) { n           long long val = alpha1 + sign1 * (*(s.begin())); n           s.erase(s.begin()); n           st = val, en = val; n           s.insert(sign1 * (a[i] / 2 - alpha1)); n         } else { n           long long t1 = a[i] - st, t2 = a[i] - en; n           st = max((long long)1, t2), en = max((long long)1, t1); n           s.insert(mid); n         } n       } n     } n     alpha = a[i] - alpha; n     sign *= -1; n   } n   cout << ans <<   n ; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   long long t = 1; n   cin >> t; n   for (long long i = 0; i < t; i++) { n     solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int t, m, num = 1; n   string com; n   int arg; n   vector<pair<pair<int, int>, int> > d; n   cin >> t >> m; n   for (int _n((t)-1), q(0); q <= _n; q++) { n     sort((d).begin(), (d).end()); n     cin >> com; n     if (com ==  defragment ) { n       if (d.size() > 0) { n         d[0].first.second -= d[0].first.first; n         d[0].first.first = 0; n         for (int _n((d.size() - 1)), i(1); i <= _n; i++) { n           int delta = d[i].first.first - d[i - 1].first.second; n           d[i].first.first -= delta; n           d[i].first.second -= delta; n         } n       } n       continue; n     } n     cin >> arg; n     if (com ==  alloc ) { n       int res = -1; n       if (d.size() > 0) n         if (d[0].first.first >= arg) { n           d.push_back(make_pair(make_pair(0, arg), num++)); n           res = 1; n         } n       if (res == -1) n         for (int _n((d.size() - 1)), i(1); i <= _n; i++) n           if (d[i].first.first - d[i - 1].first.second >= arg) { n             d.push_back(make_pair( n                 make_pair(d[i - 1].first.second, d[i - 1].first.second + arg), n                 num++)); n             res = 1; n             break; n           } n       if (res == -1) n         if (d.size() > 0) n           if (m - d[d.size() - 1].first.second >= arg) { n             d.push_back(make_pair(make_pair(d[d.size() - 1].first.second, n                                             d[d.size() - 1].first.second + arg), n                                   num++)); n             res = 1; n           } n       if (res == -1) n         if (d.size() == 0) n           if (m >= arg) { n             d.push_back(make_pair(make_pair(0, arg), num++)); n             res = 1; n           } n       if (res == -1) n         cout <<  NULL  << endl; n       else n         cout << num - 1 << endl; n       continue; n     } n     if (com ==  erase ) { n       bool res = false; n       for (int i = 0; i < d.size(); i++) n         if (d[i].second == arg) { n           swap(d[i], d[d.size() - 1]); n           d.pop_back(); n           res = true; n           break; n         } n       if (!res) cout <<  ILLEGAL_ERASE_ARGUMENT  << endl; n       continue; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 100050; n struct str { n   int pre[26], suf[26], mx[26], len; n   str() {} n   void Build(char s[], int n) { n     len = n; n     for (int i = 0; i < 26; i++) pre[i] = suf[i] = mx[i] = 0; n     int len = 0; n     for (int i = 1; i <= n; i++) { n       if (i != 1 && s[i] == s[i - 1]) n         len++; n       else n         len = 1; n       mx[s[i] -  a ] = max(mx[s[i] -  a ], len); n     } n     for (int i = 0; i < 26; i++) { n       while (pre[i] < n && s[pre[i] + 1] ==  a  + i) pre[i]++; n       while (suf[i] < n && s[n - suf[i]] ==  a  + i) suf[i]++; n     } n   } n } STR[N]; n str operator*(str x, str y) { n   str ans; n   ans.len = y.len * (x.len + 1) + x.len; n   for (int i = 0; i < 26; i++) { n     if (y.pre[i] == y.len) { n       if (x.pre[i] == x.len) n         ans.pre[i] = ans.len; n       else n         ans.pre[i] = y.len * (x.pre[i] + 1) + x.pre[i]; n       if (x.suf[i] == x.len) n         ans.suf[i] = ans.len; n       else n         ans.suf[i] = y.len * (x.suf[i] + 1) + x.suf[i]; n       ans.mx[i] = y.len * (x.mx[i] + 1) + x.mx[i]; n     } else { n       ans.pre[i] = y.pre[i]; n       ans.suf[i] = y.suf[i]; n       if (x.mx[i] > 0) n         ans.mx[i] = y.pre[i] + y.suf[i] + 1; n       else n         ans.mx[i] = y.mx[i]; n       ans.mx[i] = max(ans.mx[i], y.mx[i]); n     } n   } n   return ans; n } n char s[N]; n int main() { n   int n, len, i; n   scanf( %i , &n); n   for (i = 1; i <= n; i++) { n     scanf( %s , s + 1); n     len = strlen(s + 1); n     STR[i].Build(s, len); n   } n   for (i = 2; i <= n; i++) { n     STR[1] = STR[1] * STR[i]; n   } n   int ans = 0; n   for (i = 0; i < 26; i++) ans = max(ans, STR[1].mx[i]); n   printf( %i n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const double PI = 2.0 * acos(0.0); n const double EPS = 1e-6; n int dp[100005]; n static int topp; n void LIS(int x) { n   if (topp == 0) n     dp[topp++] = x; n   else if (x > dp[topp - 1]) n     dp[topp++] = x; n   else { n     int left = 0, right = topp; n     while (left < right) { n       int mid = (left + right) >> 1; n       if (dp[mid] > x) n         right = mid; n       else n         left = mid + 1; n     } n     while (left < topp - 1 && dp[left] == dp[left + 1]) left++; n     dp[left] = x; n   } n } n int main() { n   int n; n   scanf( %d , &n); n   ::topp = 0; n   for (int i = 0; i < n; i++) { n     int a; n     scanf( %d , &a); n     LIS(a); n   } n   printf( %d n , topp); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<int> x[100100], y[100100]; n vector<int> p[100100], q[100100], v[100100], w[100100]; n int a[100100], b[100100], c[100100]; n map<pair<int, int>, int> mp; n void solve() { n   mp.clear(); n   int n, m; n   scanf( %d%d , &n, &m); n   for (int i = 0; i < m; i++) { n     scanf( %d%d , &a[i], &b[i]); n     x[a[i]].push_back(b[i]); n     y[b[i]].push_back(a[i]); n     if (a[i] + b[i] <= n + 1) { n       int o = a[i] - 1, oo = o + b[i]; n       p[oo].push_back(b[i]); n     } else { n       int oo = n - b[i], o = a[i] - oo; n       q[o].push_back(a[i]); n     } n     if (a[i] <= b[i]) { n       int o = a[i] - 1, oo = b[i] - o; n       v[oo].push_back(b[i]); n     } else { n       int oo = b[i], o = a[i] - oo + 1; n       w[o].push_back(a[i]); n     } n   } n   for (int i = 1; i <= n; i++) { n     if (x[i].size() <= 1) continue; n     sort(x[i].begin(), x[i].end()); n     for (int j = 0; j < x[i].size(); j++) { n       int xx = i, yy = x[i][j]; n       if (j == 0 || j == x[i].size() - 1) { n         mp[make_pair(xx, yy)]++; n       } else { n         mp[make_pair(xx, yy)] += 2; n       } n     } n   } n   for (int i = 1; i <= n; i++) { n     if (y[i].size() <= 1) continue; n     sort(y[i].begin(), y[i].end()); n     for (int j = 0; j < y[i].size(); j++) { n       int xx = y[i][j], yy = i; n       if (j == 0 || j == y[i].size() - 1) { n         mp[make_pair(xx, yy)]++; n       } else { n         mp[make_pair(xx, yy)] += 2; n       } n     } n   } n   for (int i = 1; i <= n; i++) { n     if (p[i].size() <= 1) continue; n     sort(p[i].begin(), p[i].end()); n     for (int j = 0; j < p[i].size(); j++) { n       int yy = p[i][j]; n       int xx = i - yy + 1; n       if (j == 0 || j == p[i].size() - 1) { n         mp[make_pair(xx, yy)]++; n       } else { n         mp[make_pair(xx, yy)] += 2; n       } n     } n   } n   for (int i = 1; i <= n; i++) { n     if (q[i].size() <= 1) continue; n     sort(q[i].begin(), q[i].end()); n     for (int j = 0; j < q[i].size(); j++) { n       int xx = q[i][j]; n       int yy = n - (xx - i); n       if (j == 0 || j == q[i].size() - 1) { n         mp[make_pair(xx, yy)]++; n       } else { n         mp[make_pair(xx, yy)] += 2; n       } n     } n   } n   for (int i = 1; i <= n; i++) { n     if (v[i].size() <= 1) continue; n     sort(v[i].begin(), v[i].end()); n     for (int j = 0; j < v[i].size(); j++) { n       int yy = v[i][j]; n       int xx = yy - i + 1; n       if (j == 0 || j == v[i].size() - 1) { n         mp[make_pair(xx, yy)]++; n       } else { n         mp[make_pair(xx, yy)] += 2; n       } n     } n   } n   for (int i = 1; i <= n; i++) { n     if (w[i].size() <= 1) continue; n     sort(w[i].begin(), w[i].end()); n     for (int j = 0; j < w[i].size(); j++) { n       int xx = w[i][j]; n       int yy = xx - i + 1; n       if (j == 0 || j == w[i].size() - 1) { n         mp[make_pair(xx, yy)]++; n       } else { n         mp[make_pair(xx, yy)] += 2; n       } n     } n   } n   map<pair<int, int>, int>::iterator iter; n   iter = mp.begin(); n   int s = 0; n   while (iter != mp.end()) { n     int o = iter->second; n     pair<int, int> v = iter->first; n     c[o]++; n     s++; n     iter++; n   } n   printf( %d , m - s); n   for (int i = 1; i < 9; i++) printf(  %d , c[i]); n } n int main() { solve(); } n


#include <bits/stdc++.h> n using namespace std; n int32_t main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   ; n   long long n; n   cin >> n; n   string s, t; n   cin >> s >> t; n   long long sa = 0, sb = 0, ta = 0, tb = 0; n   for (long long i = 0; i < n; i++) { n     if (s[i] ==  a ) n       sa++; n     else n       sb++; n     if (t[i] ==  a ) n       ta++; n     else n       tb++; n   } n   if ((ta + sa) % 2 == 1 || (tb + sb) % 2 == 1) return cout << -1, 0; n   vector<pair<long long, long long> > ans; n   vector<long long> va, vb; n   for (long long i = 0; i < n; i++) { n     if (s[i] != t[i]) { n       if (s[i] ==  a ) n         va.push_back(i); n       else n         vb.push_back(i); n     } n   } n   { n     for (long long i = 0; n          i < (long long)va.size() && (long long)va.size() > i + 1; i += 2) n       ans.push_back(make_pair(va[i], va[i + 1])); n     for (long long i = 0; n          i < (long long)vb.size() && (long long)vb.size() > i + 1; i += 2) n       ans.push_back(make_pair(vb[i], vb[i + 1])); n   } n   if ((long long)va.size() % 2 == 1) { n     ans.push_back( n         make_pair(va[(long long)va.size() - 1], va[(long long)va.size() - 1])); n     ans.push_back( n         make_pair(va[(long long)va.size() - 1], vb[(long long)vb.size() - 1])); n   } n   cout << ans.size() <<   n ; n   for (long long i = 0; i < (long long)ans.size(); i++) n     cout << ans[i].first + 1 <<     << ans[i].second + 1 <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T> n inline T abs(T a) { n   return ((a < 0) ? -a : a); n } n template <typename T> n inline T sqr(T a) { n   return a * a; n } n const int INF = (int)1E9 + 7; n const long double EPS = 1E-9; n const long double PI = 3.1415926535897932384626433832795; n int n, m; n pair<int, int> a[100]; n int solve(int x, int y, int i) { n   int ans = i; n   if (y == m) x++, y = 0; n   if (x == n) return ans; n   ans = max(ans, solve(x, y + 1, i)); n   bool ok = true; n   for (int j = 0; j < int(i); ++j) { n     if (sqr(a[j].first - x) + sqr(a[j].second - y) == 5) { n       ok = false; n       break; n     } n   } n   if (ok) { n     a[i] = make_pair(x, y); n     ans = max(ans, solve(x, y + 1, i + 1)); n   } n   return ans; n } n long long solve(long long n, long long m) { n   if (n < m) swap(n, m); n   if (m > 2) return (n * m) / 2 + ((n * m) & 1); n   if (m == 1) return n; n   int k = (n - 2) / 4, rem = (n - 2) % 4; n   int start = 4 + k * 4; n   if (rem < 3) return start; n   return start + 2; n } n int main() { n   int n, m; n   cin >> n >> m; n   cout << solve(n, m) << endl; n   return 0; n } n


#include <bits/stdc++.h> n #pragma GCC optimize( Ofast ) n #pragma GCC target( avx,avx2,fma ) n #pragma GCC optimization( unroll-loops ) n using namespace std; n const long long MOD = 998244353; n const long long INF = 0x7f7f7f7f7f7f7f7f; n const int INFi = 0x7f7f7f7f; n const long long N = 150001; n vector<long long> adj[N]; n long long vis[N] = {}; n int dx8[] = {0, 1, 1, 1, 0, -1, -1, -1}, dy8[] = {1, 1, 0, -1, -1, -1, 0, 1}; n int dx4[] = {0, 1, 0, -1}, dy4[] = {1, 0, -1, 0}; n int t = 1; n long long n, a[2 * N + 10], fact[2 * N + 10]; n long long mult(long long a, long long b) { return (a % MOD * b % MOD) % MOD; } n long long add(long long a, long long b) { return (a % MOD + b % MOD) % MOD; } n long long subt(long long a, long long b) { n   return (a % MOD - b % MOD + MOD) % MOD; n } n long long fastModInv(long long x) { n   long long res = 1; n   long long y = MOD - 2; n   while (y) { n     if (y & 1) res = mult(res, x); n     y >>= 1; n     x = mult(x, x); n   } n   return res; n } n void MAIN() { n   cin >> n; n   long long ans = 0; n   for (long long i = 1; i <= 2 * n; i++) cin >> a[i], a[i] = 2 * a[i]; n   sort(a + 1, a + 1 + 2 * n); n   long long val = n       (fact[2 * n - 1] * fastModInv(fact[2 * n - 1 - n + 1]) % MOD) * n       fastModInv(fact[n - 1]) % MOD; n   for (long long i = 1; i <= n; i++) ans = subt(ans, val * a[i] % MOD); n   for (long long i = n + 1; i <= 2 * n; i++) ans = add(ans, val * a[i] % MOD); n   cout << ans <<   n ; n } n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   cout << fixed; n   cout << setprecision(10); n   ; n   mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count()); n   ; n   fact[0] = 1; n   for (long long i = 1; i <= 2 * N; i++) fact[i] = fact[i - 1] * i % MOD; n   while (t--) { n     MAIN(); n   } n } n


#include <bits/stdc++.h> n using namespace std; n int gcd(int a, int b) { n   while (b) { n     a %= b; n     swap(a, b); n   } n   return a; n } n int check(vector<int> v) { n   int k = 0; n   for (int i = 0; i < v.size(); ++i) k += (gcd(i + 1, v[i]) > 1); n   return k; n } n int main() { n   int n, k; n   cin >> n >> k; n   if (n == k) n     cout << -1 << endl; n   else { n     vector<int> v(n); n     v[n - k - 1] = 1; n     int p = 2; n     for (int i = 0; i < n; ++i) { n       if (i == n - k - 1) continue; n       v[i] = p++; n     } n     for (int i = 0; i < n; ++i) cout << v[i] <<    ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long n, m, i, j, k, x, y, z, t; n   long long hh, mm, h, d, c, N; n   cin >> hh >> mm; n   cin >> h >> d >> c >> N; n   if (hh >= 20) { n     double cc = 0.8 * c; n     k = (h + N - 1) / N; n     double ans = 1.0 * k * cc; n     cout << fixed << setprecision(10) << ans << endl; n     return 0; n   } n   double ans1, ans2; n   k = (h + N - 1) / N; n   ans1 = 1.0 * k * c; n   x = (60 - mm) + (20 - hh - 1) * 60; n   h += x * d; n   double cc = 0.8 * c; n   k = (h + N - 1) / N; n   ans2 = 1.0 * k * cc; n   cout << fixed << setprecision(10) << min(ans1, ans2) << endl; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e5 + 7; n const long long MOD = 1e9 + 7; n long long f[N << 2], lazy[N << 2], a[N], b[N], c[N]; n void up(int x, int l, int r, long long y) { n   f[x] = (f[x] + y * (r - l + 1)) % MOD; n   lazy[x] = (lazy[x] + y) % MOD; n } n void push_down(int x, int l, int r) { n   int mid = (l + r) / 2; n   up(x << 1, l, mid, lazy[x]); n   up(x << 1 | 1, mid + 1, r, lazy[x]); n   lazy[x] = 0; n } n void update(int x, int l, int r, int left, int right, long long y) { n   if (left > r || right < l) return; n   if (left <= l && right >= r) { n     up(x, l, r, y); n     return; n   } n   push_down(x, l, r); n   int mid = (l + r) / 2; n   update(x << 1, l, mid, left, right, y); n   update(x << 1 | 1, mid + 1, r, left, right, y); n   f[x] = (f[x << 1] + f[x << 1 | 1]) % MOD; n } n long long query(int x, int l, int r, int left, int right) { n   if (left > r || right < l) return 0; n   if (left <= l && right >= r) return f[x]; n   push_down(x, l, r); n   int mid = (l + r) / 2; n   return (query(x << 1, l, mid, left, right) + n           query(x << 1 | 1, mid + 1, r, left, right)) % n          MOD; n } n int sz[N], son[N], l[N], r[N], n; n vector<int> p[N]; n long long res, ans; n void getsize(int x, int fa) { n   sz[x] = 1; n   son[x] = -1; n   for (int v : p[x]) n     if (v != fa) { n       getsize(v, x); n       sz[x] += sz[v]; n       if (son[x] == -1 || sz[v] > sz[son[x]]) son[x] = v; n     } n } n void calc(int x, long long y, int op) { n   if (op) { n     long long tmp = a[r[x]] - a[l[x] - 1] - n                     query(1, 1, 100000, l[x], r[x]) * 2 - y * (r[x] - l[x] + 1); n     tmp = (tmp % MOD + MOD) % MOD; n     res = (res + tmp * y) % MOD; n   } n   update(1, 1, 100000, l[x], r[x], y); n } n void clear(int x, int fa) { n   calc(x, -c[x] + MOD, 0); n   for (int v : p[x]) n     if (v != fa) clear(v, x); n } n void add(int x, int fa) { n   calc(x, c[x], 1); n   for (int v : p[x]) n     if (v != fa) add(v, x); n } n void gao(int x, int fa) { n   for (int v : p[x]) n     if (v != fa && v != son[x]) { n       gao(v, x); n       clear(v, x); n       res = 0; n     } n   if (son[x] != -1) gao(son[x], x); n   for (int v : p[x]) n     if (v != fa && v != son[x]) { n       add(v, x); n     } n   calc(x, c[x], 1); n   ans = (ans + res) % MOD; n } n long long qpow(long long x, long long y) { n   long long z = 1; n   while (y > 0) { n     if (y & 1) z = z * x % MOD; n     x = x * x % MOD; n     y /= 2; n   } n   return z; n } n int main() { n   scanf( %d , &n); n   for (int i = 1; i <= n; i++) { n     scanf( %d%d , &l[i], &r[i]); n     long long t = qpow(r[i] - l[i] + 1, MOD - 2); n     c[i] = t; n     a[l[i]] = (a[l[i]] + t) % MOD; n     a[r[i] + 1] = (a[r[i] + 1] - t + MOD) % MOD; n   } n   for (int i = 1; i < N; i++) b[i] = (b[i - 1] + a[i]) % MOD; n   memset(a, 0, sizeof(a)); n   for (int i = 1; i < N; i++) a[i] = (a[i - 1] + b[i]) % MOD; n   for (int i = 1; i < n; i++) { n     int u, v; n     scanf( %d%d , &u, &v); n     p[u].push_back(v); n     p[v].push_back(u); n   } n   getsize(1, 0); n   gao(1, 0); n   for (int i = 1; i <= n; i++) { n     ans = ans * (r[i] - l[i] + 1) % MOD; n   } n   printf( %lld , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long const N = 2e5 + 20, MOD = 1e9 + 7; n long long n, x, dp[N][2]; n bool vis[N], col[N]; n vector<long long> child[N]; n void dfs(int v = 0) { n   for (auto u : child[v]) dfs(u); n   vector<long long> preChild, sufChild; n   preChild.push_back(1), sufChild.push_back(1); n   for (auto u : child[v]) { n     preChild.push_back((preChild.back() * ((dp[u][0] + dp[u][1]) % MOD)) % MOD); n   } n   for (int i = child[v].size() - 1; i >= 0; i--) { n     int u = child[v][i]; n     sufChild.push_back((sufChild.back() * ((dp[u][0] + dp[u][1]) % MOD)) % MOD); n   } n   if (col[v]) { n     dp[v][0] = 0; n     dp[v][1] = preChild.back(); n     return; n   } n   dp[v][0] = preChild.back(); n   dp[v][1] = 0; n   preChild.push_back(1), sufChild.push_back(1); n   for (int i = 0; i < child[v].size(); i++) { n     int u = child[v][i]; n     dp[v][1] = (dp[v][1] + ((dp[u][1] * preChild[i] % MOD) * n                             (sufChild[sufChild.size() - 3 - i] % MOD) % MOD)) % n                MOD; n   } n   return; n } n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   cin >> n; n   for (int i = 0; i < n - 1; i++) { n     cin >> x; n     child[x].push_back(i + 1); n   } n   for (int i = 0; i < n; i++) cin >> col[i]; n   dfs(); n   return cout << dp[0][1], 0; n } n


#include <bits/stdc++.h> n int n, t1, t2; n int num = 0; n int dfs(int l, int r) { n   int k = (l + r) / 2; n   num++; n   if (t1 <= k && t2 > k) { n     return num; n   } n   if (t1 <= k && t2 <= k) { n     return dfs(l, k); n   } else { n     return dfs(k, r); n   } n } n int FastLog2(int x) { n   float fx; n   unsigned long ix, exp; n   fx = (float)x; n   ix = *(unsigned long*)&fx; n   exp = (ix >> 23) & 0xFF; n   return exp - 127; n } n int main() { n   int a, b; n   scanf( %d%d%d , &n, &a, &b); n   t1 = std::min(a, b); n   t2 = std::max(a, b); n   int ans = FastLog2(n) + 1 - dfs(1, n); n   if (ans == FastLog2(n)) n     puts( Final! ); n   else n     printf( %d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int get(int n, int p) { n   int res = 0; n   while (n >= p) res += n / p, n /= p; n   return res; n } n template <class T> n T power(T N, T P) { n   return (P == 0) ? 1 : N * power(N, P - 1); n } n void VC(vector<long long int>& v) { n   for (int i = 0; i < v.size(); i++) printf( %lld  , v[i]); n   printf(  n ); n } n struct hash_pair { n   template <class T1, class T2> n   size_t operator()(const pair<T1, T2>& p) const { n     auto hash1 = hash<T1>{}(p.first); n     auto hash2 = hash<T2>{}(p.second); n     return hash1 ^ hash2; n   } n }; n template <typename T> n void print(const T& v) { n   cerr << v <<    ; n } n template <typename T1, typename... T2> n void print(const T1& first, const T2&... rest) { n   print(first); n   print(rest...); n } n const int Maxn = 1e5 + 1; n template <typename T, typename T1> n T mod(T x, T1 p) { n   x %= p; n   if (x < 0) x += p; n   return x; n } n int fact[Maxn]; n void factorial() { n   fact[0] = 1; n   for (int i = 1; i < Maxn; i++) { n     fact[i] = 1LL * fact[i - 1] * i % 1000000007; n   } n } n int inv[Maxn], ifact[Maxn]; n template <typename T> n T inverse(T x, T p) { n   x = mod(x, p); n   if (x == 1) return x; n   return mod((1LL * (-p / x) * (inv[p % x] % p)), p); n } n void inverse_fact() { n   ifact[0] = 1; n   for (int i = 1; i < Maxn; i++) { n     inv[i] = inverse(i, 1000000007); n     ifact[i] = (1LL * ifact[i - 1] * inv[i]) % 1000000007; n   } n } n int nCr(int n, int r) { n   int ret = (1LL * ifact[n - r] * ifact[r]) % 1000000007; n   ret = (1LL * ret * fact[n]) % 1000000007; n   return ret; n } n bool cmp(int a, int b) { return a > b; } n int main() { n   int n, m, p, x, a, b; n   scanf( %d , &(n)); n   scanf( %d , &(m)); n   scanf( %d , &(p)); n   int aa[n + 3], bb[m + 4]; n   for (int i = 0; i < n; i++) { n     scanf( %d , &(aa[i])); n   } n   for (int i = 0; i < m; i++) { n     scanf( %d , &(bb[i])); n   } n   for (int i = 0; i < n; i++) { n     if (aa[i] % p) { n       a = i; n     } n   } n   for (int i = 0; i < m; i++) { n     if (bb[i] % p) { n       b = i; n     } n   } n   printf( %d n , a + b); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n class Data { n  public: n   long long lazy, val; n   Data() { n     lazy = -1; n     val = 0; n   } n }; n long long pr = 107; n long long c_pr[100010]; n Data tree[4 * 100010]; n long long ara[100010]; n void updval(int node, int le_ran, int ri_ran, long long lazy) { n   long long poo = c_pr[ri_ran] - c_pr[le_ran - 1]; n   poo %= 1000000007; n   if (poo < 0) { n     poo = (poo + 1000000007) % 1000000007; n   } n   tree[node].val = poo * lazy; n   tree[node].val %= 1000000007; n   tree[node].lazy = lazy; n } n void update(int node, int le_ran, int ri_ran) { n   if (tree[node].lazy != -1) { n     if (le_ran != ri_ran) { n       int mid = (le_ran + ri_ran) / 2; n       updval(node * 2, le_ran, mid, tree[node].lazy); n       updval((node * 2) + 1, mid + 1, ri_ran, tree[node].lazy); n     } n     tree[node].lazy = -1; n   } n } n void update_range(int node, int le_ran, int ri_ran, int le, int ri, n                   long long val) { n   if ((ri_ran < le) || (le_ran > ri)) { n     return; n   } n   if ((le_ran >= le) && (ri_ran <= ri)) { n     updval(node, le_ran, ri_ran, val); n     return; n   } n   update(node, le_ran, ri_ran); n   int mid = (le_ran + ri_ran) / 2; n   update_range(node * 2, le_ran, mid, le, ri, val); n   update_range((node * 2) + 1, mid + 1, ri_ran, le, ri, val); n   tree[node].val = tree[node * 2].val + tree[(node * 2) + 1].val; n   tree[node].val %= 1000000007; n } n long long query(int node, int le_ran, int ri_ran, int le, int ri) { n   if ((ri_ran < le) || (le_ran > ri)) { n     return 0; n   } n   if ((le_ran >= le) && (ri_ran <= ri)) { n     return tree[node].val; n   } n   update(node, le_ran, ri_ran); n   int mid = (le_ran + ri_ran) / 2; n   long long ans = 0; n   ans += query(node * 2, le_ran, mid, le, ri); n   ans %= 1000000007; n   ans += query((node * 2) + 1, mid + 1, ri_ran, le, ri); n   ans %= 1000000007; n   return ans; n } n string str; n int n, q; n long long bm(long long base, long long power) { n   if (power == 0) { n     return 1; n   } n   if (power % 2 == 0) { n     long long ret = bm(base, power / 2); n     return (ret * ret) % 1000000007; n   } else { n     return (base * bm(base, power - 1)) % 1000000007; n   } n } n long long mod_inv(long long num) { return bm(num, 1000000007 - 2); } n long long get_hash(int l, int r) { n   long long yo = query(1, 1, n, l, r); n   yo %= 1000000007; n   if (yo < 0) { n     yo = (yo + 1000000007) % 1000000007; n   } n   yo = yo * mod_inv(bm(pr, l)); n   yo %= 1000000007; n   return yo; n } n int main() { n   std::ios_base::sync_with_stdio(false); n   int m, k; n   cin >> n >> m >> k; n   q = m + k; n   long long tmp_pr = 1; n   for (int i = 1; i <= n; i++) { n     c_pr[i] = c_pr[i - 1] + tmp_pr; n     c_pr[i] %= 1000000007; n     tmp_pr = tmp_pr * pr; n     tmp_pr %= 1000000007; n   } n   cin >> str; n   for (int i = 0; i <= n - 1; i++) { n     update_range(1, 1, n, i + 1, i + 1, str[i] -  0 ); n   } n   for (int i = 1; i <= q; i++) { n     int cmd; n     cin >> cmd; n     int l, r, val; n     cin >> l >> r >> val; n     if (cmd == 1) { n       update_range(1, 1, n, l, r, val); n     } else { n       if (get_hash(l, r - val) == get_hash(l + val, r)) { n         cout <<  YES n ; n       } else { n         cout <<  NO n ; n       } n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, i, mx = 2, j; n   cin >> n; n   int a[n]; n   for (i = 0; i < n; i++) cin >> a[i]; n   if (n == 1) n     cout <<  1 ; n   else if (n == 2) n     cout <<  2 ; n   else { n     for (i = 2; i < n;) { n       int x = 0; n       for (j = i; j < n; j++) { n         if ((a[j] != (a[j - 1] + a[j - 2]))) break; n         x++; n       } n       i = ++j; n       mx = max(x + 2, mx); n     } n     cout << mx; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 2e6 + 1; n const int INF = 1e9 + 9; n const int B = 1e9 + 7; n int n, b[N], d[N]; n long long a[N]; n int main() { n   ios_base ::sync_with_stdio(0); n   cin.tie(0); n   cin >> n; n   int ans = n; n   for (int i = int(1); i <= int(n); ++i) { n     cin >> a[i]; n     while (a[i] % 2 == 0) a[i] /= 2, ++b[i]; n     d[i] = i - 1; n     for (int j = int(1); j <= int(i - 1); ++j) n       if (a[j] % a[i] == 0 && n           (b[j] + (i - j) == b[i] || b[i] <= i - j - 1 || b[i] == 0)) n         d[i] = min(d[i], d[j] + (i - j - 1)); n     ans = min(ans, d[i] + (n - i)); n   } n   cout << ans <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   string s1, s2; n   cin >> s1 >> s2; n   int n = s1.length(), k = 0, s; n   for (int i = 0; i < n; i++) n     if (s1[i] != s2[i]) k++; n   if (k % 2 == 1) n     printf( impossible ); n   else { n     int s = k / 2; n     string ans; n     for (int i = 0; i < n; i++) n       if (s1[i] != s2[i]) n         if (s > 0) { n           ans += s1[i]; n           s--; n         } else n           ans += s2[i]; n       else n         ans += s1[i]; n     cout << ans; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MOD = 1e9 + 7; n const int MOD2 = 1e9 + 9; n const int PR = 727; n const int INF = INT_MAX; n const long long LINF = LLONG_MAX; n const int N = 1e5 + 20; n vector<int> G[N], p; n int n, m, k, f[N], tmp; n bool visited[N]; n int main() { n   ios::sync_with_stdio(false), cin.tie(0), cout.tie(0); n   cin >> n >> m >> k; n   for (int i = 0, v, u; i < m; i++) { n     cin >> v >> u; n     G[v].push_back(u); n     G[u].push_back(v); n   } n   int v = 1; n   while (1) { n     visited[v] = 1; n     f[v] = tmp++; n     p.push_back(v); n     for (int u : G[v]) n       if (!visited[u]) v = u; n     if (visited[v]) break; n   } n   int idx = INF; n   for (int v : G[p.back()]) idx = min(idx, f[v]); n   cout << p.size() - idx << endl; n   for (int i = idx; i < p.size(); i++) cout << p[i] <<    ; n   cout << endl; n } n


#include <bits/stdc++.h> n using namespace std; n #pragma GCC optimize( O3 ) n #pragma GCC target( avx ) n #pragma GCC optimize( Ofast ) n #pragma GCC optimize( unroll-loops ) n #pragma GCC target( sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native ) n long long int dx[] = {0, 1, 0, -1, -1, 1, -1, 1}; n long long int dy[] = {-1, 0, 1, 0, -1, 1, 1, -1}; n const long long int MAXN = 2e6 + 5; n const long long int N = 1e5 + 5; n long long int n, m; n long long int binpow(long long int n, long long int p) { n   long long int res = 1; n   while (p > 0) { n     if (p & 1) res = res * n; n     n = n * n; n     p >>= 1; n   } n   return res; n } n long long int power(long long int n, long long int p) { n   long long int res = 1; n   n %= 1000000007; n   while (p > 0) { n     if (p & 1) res = (res * n) % 1000000007; n     n = (n * n) % 1000000007; n     p >>= 1; n   } n   return res; n } n long long int modInverse(long long int n, long long int p) { n   return power(n, p - 2); n } n long long int fact[MAXN], inv[MAXN], invfact[MAXN]; n void initFactorials() { n   fact[0] = 1; n   for (long long int i = 1; i < MAXN; i++) { n     fact[i] = (fact[i - 1] * i * 1LL) % 1000000007; n   } n   inv[1] = 1; n   for (long long int i = 2; i < MAXN; i++) n     inv[i] = inv[1000000007 % i] * (1000000007 - 1000000007 / i) % 1000000007; n   invfact[0] = 1; n   for (long long int i = 1; i < MAXN; i++) n     invfact[i] = (invfact[i - 1] * inv[i]) % 1000000007; n } n long long int nCrMod(long long int n, long long int r) { n   if (n < r) return 0; n   if (r == 0) return 1; n   long long int num = fact[n], den = (fact[r] * fact[n - r]) % 1000000007; n   long long int inv = modInverse(den, 1000000007); n   return (num * inv) % 1000000007; n } n string s; n vector<long long int> res; n long long int mx = 1000111000111000111LL; n bool isVowel(char c) { n   return (c ==  a  || c ==  e  || c ==  i  || c ==  o  || c ==  u ); n } n void go(long long int pos, long long int cnt, vector<long long int> temp) { n   if (pos >= n) { n     if (temp.size() < mx) { n       mx = temp.size(); n       res = temp; n     } n     return; n   } n   if (isVowel(s[pos])) { n     return go(pos + 1, 0, temp); n   } n   if (cnt == 2) { n     temp.push_back(pos); n     go(pos, 0, temp); n   } else if (cnt == 1) { n     temp.push_back(pos); n     go(pos, 0, temp); n     temp.pop_back(); n     go(pos + 1, cnt + 1, temp); n   } else { n     go(pos + 1, cnt + 1, temp); n   } n } n void solve() { n   cin >> s; n   n = s.size(); n   string temp; n   long long int constant = 0, consecutive = 0; n   temp = s[0]; n   if (!isVowel(s[0])) constant = 1, consecutive = 1; n   for (long long int i = 1; i < n; i++) { n     if (isVowel(s[i])) { n       consecutive = 0; n       constant = 0; n       temp += s[i]; n       continue; n     } n     if (consecutive >= 3) { n       if (!isVowel(s[i]) && s[i] != s[i - 1]) { n         cout << temp <<    ; n         temp = s[i]; n         consecutive = 1; n         constant = 1; n         continue; n       } n     } n     if (!isVowel(s[i]) && s[i] == s[i - 1]) { n       consecutive++; n     } else { n       consecutive = 1; n     } n     if (!isVowel(s[i])) { n       constant++; n     } n     if (consecutive >= 3) { n       temp += s[i]; n       constant = 0; n       continue; n     } n     if (constant == 3) { n       cout << temp <<    ; n       temp = s[i]; n       constant = 1; n     } else { n       temp += s[i]; n     } n   } n   cout << temp <<   n ; n } n signed main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   long long int t = 1; n   while (t--) { n     solve(); n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int MAX = 500000; n int a[MAX], last[MAX], prv[MAX], inSet[MAX], current[MAX]; n int n, k; n int main() { n   ios_base::sync_with_stdio(false); n   cin >> n >> k; n   memset(last, -1, sizeof(last)); n   memset(prv, -1, sizeof(prv)); n   for (int i = 0; i < n; i++) { n     cin >> a[i]; n     prv[i] = last[a[i]]; n     last[a[i]] = i; n   } n   set<pair<int, int> > s; n   int ans = 0; n   for (int i = n - 1; i >= 0; i--) { n     int cur = a[i]; n     if (inSet[cur]) { n       s.erase(s.find(make_pair(current[cur], cur))); n       s.insert(make_pair(prv[i], cur)); n       current[cur] = prv[i]; n       continue; n     } n     ans++; n     if (s.size() < k) { n       inSet[cur] = true; n       s.insert(make_pair(prv[i], cur)); n       current[cur] = prv[i]; n     } else { n       pair<int, int> fr = (*s.begin()); n       inSet[cur] = true; n       inSet[fr.second] = false; n       s.erase(s.begin()); n       s.insert(make_pair(prv[i], cur)); n       current[cur] = prv[i]; n     } n   } n   cout << ans <<   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(NULL); n   cout.tie(NULL); n   int n, k; n   cin >> n >> k; n   vector<int> v(n + 1, 0); n   for (int i = 1; i <= n; i++) { n     cin >> v[i]; n   } n   sort(v.begin() + 1, v.end()); n   v[0] = v[1]; n   auto get = [&](int x) { n     vector<int> p; n     p.emplace_back(0); n     for (int i = 1; i <= n; i++) { n       int left = lower_bound(v.begin(), v.end(), v[i] - x) - v.begin() - 1; n       int right = i - k; n       auto it = lower_bound(p.begin(), p.end(), left); n       if (it != p.end() and *it <= right) { n         p.emplace_back(i); n       } n     } n     return (find(p.begin(), p.end(), n) != p.end()); n   }; n   int lo = 0, hi = (v[n] - v[1]), ans = 1e9; n   while (lo <= hi) { n     int md = (lo + hi) / 2; n     if (get(md)) n       ans = md, hi = md - 1; n     else n       lo = md + 1; n   } n   cout << ans <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n void solve() { n   long long int n, k; n   cin >> n >> k; n   if (k > n) n     cout << -1 <<   n ; n   else if (n != 1 && k == 1) n     cout << -1 <<   n ; n   else if (k == n) { n     char x =  a ; n     for (long long int i = 1; i <= (long long int)n; i++) { n       cout << x; n       x++; n     } n   } else if (k < n) { n     long long int t = n - (k - 2); n     for (long long int i = 1; i <= (long long int)t; i++) { n       if (i % 2 != 0) n         cout <<  a ; n       else n         cout <<  b ; n     } n     long long int q = n - t; n     char y =  c ; n     if (q > 0) { n       for (long long int i = 1; i <= (long long int)q; i++) { n         cout << y; n         y++; n       } n     } n   } n } n int main() { n   std::ios::sync_with_stdio(false); n   int t = 1; n   while (t--) { n     solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(0); n   int n, s, k, a[55], dp[55][2505]; n   cin >> n >> s >> k; n   for (int i = 0; i < n; i++) cin >> a[i]; n   string s1; n   cin >> s1; n   priority_queue<pair<int, pair<int, int> > > q; n   s--; n   for (int i = 0; i < 55; i++) n     for (int j = 0; j < 2505; j++) dp[i][j] = 1e7 + 7; n   for (int i = 0; i < n; i++) { n     dp[i][a[i]] = abs(i - s); n     q.push(make_pair(abs(i - s), make_pair(i, a[i]))); n   } n   while (!q.empty()) { n     pair<int, pair<int, int> > p = q.top(); n     q.pop(); n     int dd = p.first; n     int at = p.second.first; n     int choc = p.second.second; n     for (int i = 0; i < n; i++) { n       if (a[i] > a[at] && s1[i] != s1[at]) { n         int dis = abs(i - at); n         if (dp[at][choc] + dis < dp[i][choc + a[i]]) { n           dp[i][choc + a[i]] = dp[at][choc] + dis; n           q.push(make_pair(-dp[i][choc + a[i]], make_pair(i, choc + a[i]))); n         } n       } n     } n   } n   int ans = 1e7 + 7; n   for (int i = 0; i < n; i++) n     for (int j = k; j <= 2505; j++) ans = min(ans, dp[i][j]); n   if (ans > 1e6) n     cout <<  -1 ; n   else n     cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int x, cnt = 0; n   cin >> x; n   int arr1[x], arr2[x]; n   for (int i = 0; i < x; i++) { n     cin >> arr1[i] >> arr2[i]; n   } n   for (int i = 0; i < x; i++) { n     for (int j = 0; j < x; j++) { n       if (i == j) { n         continue; n       } n       if (arr1[i] == arr2[j]) { n         cnt++; n       } n     } n   } n   cout << cnt; n } n


#include <bits/stdc++.h> n using namespace std; n void IO() { n   mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n } n int main() { n   IO(); n   int t; n   cin >> t; n   while (t--) { n     long long int n; n     cin >> n; n     long long int sum = 0; n     vector<long long int> arr(n + 1); n     for (__typeof(n + 1) i = (1) - ((1) > (n + 1)); n          i != (n + 1) - ((1) > (n + 1)); i += 1 - 2 * ((1) > (n + 1))) { n       cin >> arr[i]; n       sum += arr[i]; n     } n     long long int rem = sum % n; n     if (rem) n       cout << -1 <<   n ; n     else { n       long long int want = sum / n; n       vector<pair<pair<long long int, long long int>, long long int> > ans; n       for (__typeof(n + 1) i = (2) - ((2) > (n + 1)); n            i != (n + 1) - ((2) > (n + 1)); i += 1 - 2 * ((2) > (n + 1))) { n         long long int rem = arr[i] % i; n         arr[1] += arr[i]; n         if (rem) { n           long long int more = i - rem; n           ans.push_back(make_pair(make_pair(1, i), more)); n         } n         ans.push_back(make_pair(make_pair(i, 1), (arr[i] + i - 1) / i)); n       } n       for (__typeof(n + 1) i = (2) - ((2) > (n + 1)); n            i != (n + 1) - ((2) > (n + 1)); i += 1 - 2 * ((2) > (n + 1))) { n         ans.push_back(make_pair(make_pair(1, i), want)); n       } n       long long int a = (int)(ans.size()); n       cout << a <<   n ; n       for (__typeof(a) i = (0) - ((0) > (a)); i != (a) - ((0) > (a)); n            i += 1 - 2 * ((0) > (a))) { n         cout << ans[i].first.first <<     << ans[i].first.second <<     n              << ans[i].second <<   n ; n       } n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main(int argc, char** argv) { n   long long r, x1, y1, x2, y2; n   while (cin >> r >> x1 >> y1 >> x2 >> y2) { n     double dis = sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2) + 0.0); n     cout << ceil(dis / 2 / r) << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n using LL = long long; n const int N = 400005; n const int mod = 998244353; n int n, m; n int fact[N], infact[N]; n int qmi(int a, int k, int p) { n   int res = 1; n   while (k) { n     if (k & 1) res = (LL)res * a % p; n     a = (LL)a * a % p; n     k >>= 1; n   } n   return res; n } n int C(int a, int b) { n   return (LL)fact[a] * infact[a - b] % mod * infact[b] % mod; n } n int main() { n   scanf( %d%d , &n, &m); n   fact[0] = infact[0] = 1; n   for (int i = 1; i < N; ++i) { n     fact[i] = (LL)fact[i - 1] * i % mod; n     infact[i] = (LL)infact[i - 1] * qmi(i, mod - 2, mod) % mod; n   } n   if (n == 2) { n     printf( %d n , 0); n     return 0; n   } n   int res = (LL)C(m, n - 1) * (n - 2) % mod; n   int p = qmi(2, n - 3, mod) % mod; n   printf( %d n , (LL)res * p % mod); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   const int maxn = 1005; n   char a[2 * maxn], b[maxn]; n   scanf( %s , a); n   scanf( %s , b); n   int lenA = strlen(a); n   int lenB = strlen(b); n   int cntA = 0, cntB = 0; n   int i; n   for (i = 0; i < lenA; i++) n     if (a[i] ==  1 ) cntA++; n   for (i = 0; i < lenB; i++) n     if (b[i] ==  1 ) cntB++; n   if (cntA % 2 == 0) { n     if (cntA < cntB) { n       puts( NO ); n     } else { n       puts( YES ); n     } n   } else { n     if (cntB - cntA > 1) { n       puts( NO ); n     } else { n       puts( YES ); n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int H = 1000000; n template <typename Key, typename Val> n struct Hash { n   char *used; n   Key *key; n   Val *val; n   Hash() { n     used = new char[H]; n     key = new Key[H]; n     val = new Val[H]; n     memset(used, 0, sizeof(used)); n   } n   int hash_func(const Key &k) { return k % H; } n   pair<bool, int> find(const Key &k) { n     int h = hash_func(k); n     for (;; h++, h = (h == H) ? 0 : h) { n       if (not used[h]) return make_pair(false, h); n       if (key[h] == k) return make_pair(true, h); n     } n   } n   void insert(const Key &k, const Val &v) { n     int h = find(k).second; n     used[h] = 1; n     key[h] = k; n     val[h] = v; n   } n   Val &get(const Key &k) { n     pair<bool, int> f = find(k); n     if (!f.first) assert(false); n     return val[f.second]; n   } n }; n int mod; n long long find(const long long &n, long long l, long long r, long long u, n                long long v, long long &num) { n   num = 0; n   if (l > r or u > v) return 0; n   if (v < 1 or n < u) return 0; n   if (r < 1 or n < l) return 0; n   if (n == 1) { n     if (l <= 1 and 1 <= r and u <= 1 and 1 <= v) { n       num = 1; n       return 1; n     } else { n       num = 0; n       return 0; n     } n   } n   l = max(1ll, l); n   r = min(n, r); n   u = max(1ll, u); n   v = min(n, v); n   if (l == 1 and r == n) { n     num = v - u + 1; n     long long c1 = v + u, c2 = (v - u + 1); n     if (c1 % 2 == 0) n       c1 /= 2; n     else n       c2 /= 2; n     c1 %= mod; n     c2 %= mod; n     return c1 * c2 % mod; n     ; n   } n   num = 0; n   const long long n1 = (n + 1) / 2, n2 = n / 2; n   long long ans = 0; n   if (l <= n1) { n     long long u1 = u, v1 = v, nn; n     if (u1 % 2 == 0) u1++; n     if (v1 % 2 == 0) v1--; n     ans += find(n1, l, min(r, n1), (u1 + 1) / 2, (v1 + 1) / 2, nn) * 2; n     num += nn; n     ans -= nn; n   } n   if (n1 + 1 <= r) { n     long long u1 = u, v1 = v, nn; n     if (u1 % 2 == 1) u1++; n     if (v1 % 2 == 1) v1--; n     ans += find(n2, max(l, n1 + 1) - n1, r - n1, u1 / 2, v1 / 2, nn) * 2; n     num += nn; n   } n   ans = (ans % mod + mod) % mod; n   return ans; n } n long long n; n int m; n long long l, r, u, v; n int main() { n   scanf( %I64d%d%d , &n, &m, &mod); n   for (int i = 0; i < (int)(m); ++i) { n     scanf( %I64d%I64d%I64d%I64d , &l, &r, &u, &v); n     long long n1; n     printf( %lld n , find(n, l, r, u, v, n1)); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n bool sortmd(pair<long long, long long> &a, pair<long long, long long> &b) { n   if (a.first != b.first) n     return a.first < b.first; n   else n     return a.second < b.second; n } n int main() { n   ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0); n   srand(time(NULL)); n   long long t, i, j, a, b; n   cin >> t; n   while (t--) { n     cin >> a >> b; n     if (a == b) { n       cout << 0 << endl; n     } else if (a < b) { n       j = b - a; n       if (j % 2 == 0) n         cout << 2 << endl; n       else n         cout << 1 << endl; n     } else { n       j = a - b; n       if (j % 2 == 0) n         cout << 1 << endl; n       else n         cout << 2 << endl; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n const int MAX = (int)1e5 + 5; n int a[MAX], b[MAX], n, m, c; n int main() { n   scanf( %d%d%d , &n, &m, &c); n   for (int i = 1; i <= n; i++) scanf( %d , a + i); n   for (int i = 1; i <= m; i++) { n     scanf( %d , b + i); n     b[i] = (b[i] + b[i - 1]) % c; n   } n   for (int i = 1; i <= n; i++) { n     a[i] = (i <= n - m + 1 n                 ? (a[i] + (i >= m ? b[m] : b[i])) % c n                 : (a[i] + c + b[(i >= m) ? m : i] - b[m - 1 - (n - i)]) % c); n     printf( %d  , a[i]); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n inline int read() { n   char c = getchar(); n   int x = 0; n   bool f = 0; n   for (; !isdigit(c); c = getchar()) f ^= !(c ^ 45); n   for (; isdigit(c); c = getchar()) x = (x << 1) + (x << 3) + (c ^ 48); n   if (f) x = -x; n   return x; n } n int mod; n struct modint { n   int x; n   modint(int o = 0) { x = o; } n   modint &operator=(int o) { return x = o, *this; } n   modint &operator+=(modint o) { n     return x = x + o.x >= mod ? x + o.x - mod : x + o.x, *this; n   } n   modint &operator-=(modint o) { n     return x = x - o.x < 0 ? x - o.x + mod : x - o.x, *this; n   } n   modint &operator*=(modint o) { return x = 1ll * x * o.x % mod, *this; } n   modint &operator^=(int b) { n     modint a = *this, c = 1; n     for (; b; b >>= 1, a *= a) n       if (b & 1) c *= a; n     return x = c.x, *this; n   } n   modint &operator/=(modint o) { return *this *= o ^= mod - 2; } n   modint &operator+=(int o) { n     return x = x + o >= mod ? x + o - mod : x + o, *this; n   } n   modint &operator-=(int o) { n     return x = x - o < 0 ? x - o + mod : x - o, *this; n   } n   modint &operator*=(int o) { return x = 1ll * x * o % mod, *this; } n   modint &operator/=(int o) { return *this *= ((modint(o)) ^= mod - 2); } n   template <class I> n   friend modint operator+(modint a, I b) { n     return a += b; n   } n   template <class I> n   friend modint operator-(modint a, I b) { n     return a -= b; n   } n   template <class I> n   friend modint operator*(modint a, I b) { n     return a *= b; n   } n   template <class I> n   friend modint operator/(modint a, I b) { n     return a /= b; n   } n   friend modint operator^(modint a, int b) { return a ^= b; } n   friend bool operator==(modint a, int b) { return a.x == b; } n   friend bool operator!=(modint a, int b) { return a.x != b; } n   bool operator!() { return !x; } n   modint operator-() { return x ? mod - x : 0; } n }; n inline modint qpow(modint a, int b) { return a ^ b; } n int n, k; n map<int, int> mp; n modint iv[100005], sum[100005]; n void div(int l, int r, int k) { n   if (k <= 1 || l == r) return mp[r - l + 1]++, void(); n   int mid = l + r >> 1; n   div(l, mid, k - 1), div(mid + 1, r, k - 1); n } n modint getprob(int x, int y) { n   modint res = modint(x) * y; n   for (register int i = (1); i <= (x); ++i) res -= 2 * (sum[i + y] - sum[i]); n   return res; n } n signed main() { n   n = read(), k = read(), mod = read(); n   iv[1] = 1; n   for (register int i = (2); i <= (max(4, n)); ++i) n     iv[i] = iv[mod % i] * (mod - mod / i); n   for (register int i = (1); i <= (n); ++i) sum[i] = sum[i - 1] + iv[i]; n   div(1, n, k); n   modint res = 0; n   for (auto it1 : mp) n     for (auto it2 : mp) { n       if (it1.first == it2.first) { n         int cnt = it1.second, len = it1.first; n         res += iv[2] * len * (len - 1) * cnt; n         res += modint(cnt) * (cnt - 1) * iv[2] * getprob(len, len); n       } else if (it1.first < it2.first) { n         int l1 = it1.first, l2 = it2.first, c1 = it1.second, c2 = it2.second; n         res += getprob(l1, l2) * c1 * c2; n       } n     } n   res *= iv[2]; n   cout << res.x; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long MOD = 1e9 + 7; n int n, l, m; n const int MAXN = 1e6 + 5; n long long cini[MAXN], cfin[MAXN]; n vector<vector<long long>> owo; n vector<vector<long long>> mul(vector<vector<long long>> A, n                               vector<vector<long long>> B) { n   int n = A.size(); n   vector<vector<long long>> res(n, vector<long long>(n)); n   for (int k = 0; k < n; k++) n     for (int i = 0; i < n; i++) n       for (int j = 0; j < n; j++) n         res[i][j] = (res[i][j] + A[i][k] * B[k][j]) % MOD; n   return res; n } n vector<vector<long long>> mexp(vector<vector<long long>> M, long long e) { n   int n = M.size(); n   vector<vector<long long>> res(n, vector<long long>(n)); n   for (int i = 0; i < n; i++) res[i][i] = 1; n   for (long long k = 1; k <= e; k *= 2) { n     if (k & e) res = mul(res, M); n     M = mul(M, M); n   } n   return res; n } n int cost[MAXN]; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cin >> n >> l >> m; n   for (int i = 0; i < n; i++) { n     int x; n     cin >> x; n     cini[x % m]++; n   } n   owo.assign(m, vector<long long>(m)); n   for (int i = 0; i < n; i++) { n     int x; n     cin >> x; n     cost[i] = x; n     for (int i = 0; i < m; i++) { n       owo[i][(x + i) % m]++; n     } n   } n   for (int i = 0; i < n; i++) { n     int x; n     cin >> x; n     cfin[(x + cost[i]) % m]++; n   } n   vector<vector<long long>> res = mexp(owo, l - 2); n   long long ans = 0; n   for (int a = 0; a < m; a++) { n     for (int b = 0; b < m; b++) { n       int rem = (a + b) % m; n       if (rem > 0) rem = m - rem; n       long long te = ((long long)cini[a] * cfin[b]) % MOD; n       ans = (ans + te * res[0][rem]) % MOD; n     } n   } n   cout << ans << endl; n } n


#include <bits/stdc++.h> n using namespace std; n const long long MOD = 1000000007; n const int INF = 1034567890; n const long long LL_INF = 1234567890123456789ll; n const double PI = acos(-1); n const long double ERR = 1E-10; 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     int cnt = 1; n     int place{}; n     int ph; n     for (auto i{0}; i < 9; i++) { n       if (n >= cnt) { n         place = cnt; n       } n       cnt *= 3; n     } n     place *= 3; n     ph = place; n     int res = 0; n     if (n > place / 2) { n       res = place; n     } else { n       place /= 3; n       res += place; n       n -= place; n       while (n && place) { n         place /= 3; n         if (n > place / 2) { n           n -= place; n           res += place; n         } n       } n     } n     cout << res << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long int n, sum = 0; n   vector<int> ans; n   cin >> n; n   cout << (n * (n + 1) / 2) % 2 <<   n ; n   sum = n * (n + 1) / 4; n   while (sum) { n     if (n <= sum) { n       sum -= n; n       ans.emplace_back(n); n     } n     n--; n   } n   cout << ans.size() <<    ; n   for (int i = 0; i < ans.size(); i++) cout << ans[i] <<    ; n   cout <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   cin >> n; n   vector<long long> s(n); n   for (int(i) = (int)(0); (i) < (int)(n); ++(i)) cin >> s[i]; n   long long a, b, c, d, e; n   cin >> a >> b >> c >> d >> e; n   long long r = 0; n   vector<long long> t(5, 0); n   for (int(i) = (int)(0); (i) < (int)(n); ++(i)) { n     r += s[i]; n     if (r >= e) t[4] += r / e, r -= (r / e) * e; n     if (r >= d) t[3] += r / d, r -= (r / d) * d; n     if (r >= c) t[2] += r / c, r -= (r / c) * c; n     if (r >= b) t[1] += r / b, r -= (r / b) * b; n     if (r >= a) t[0] += r / a, r -= (r / a) * a; n   } n   for (int(i) = (int)(0); (i) < (int)(5); ++(i)) cout << t[i] <<    ; n   cout << endl; n   cout << r << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 2000 + 5; n vector<int> g[maxn]; n bool used[maxn][maxn]; n int v[maxn], stk[maxn]; n void dfs(int z, int m, int x, bool eq) { n   stk[z] = x; n   if (z == m) { n     if (eq) return; n     for (int i = 0; i < m + 1; ++i) printf( %d  , stk[i] + 1); n     puts(  ); n     exit(0); n   } n   for (int i = 0; i < (int)g[x].size(); ++i) { n     if (!used[x][g[x][i]] && !used[g[x][i]][x]) { n       if (!eq || g[x][i] >= v[z + 1]) { n         used[x][g[x][i]] = true; n         dfs(z + 1, m, g[x][i], g[x][i] > v[z + 1] ? false : eq); n         used[x][g[x][i]] = false; n       } n     } n   } n } n int main() { n   int n, m; n   scanf( %d%d , &n, &m); n   for (int i = 0; i < m + 1; ++i) { n     scanf( %d , &v[i]); n     --v[i]; n   } n   for (int i = 0; i < m; ++i) { n     g[v[i]].push_back(v[i + 1]); n     g[v[i + 1]].push_back(v[i]); n   } n   for (int i = 0; i < n; ++i) { n     sort(g[i].begin(), g[i].end()); n   } n   dfs(0, m, v[0], true); n   puts( No solution ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long MAGIC = 777777777; n struct point { n   long long x, y, z; n   point() {} n   point(long long val) { x = y = z = val; } n }; n vector<point> arr; n struct equation { n   pair<long long, long long> S; n   pair<long long, long long> a; n   pair<long long, long long> b; n   pair<long long, long long> c; n   equation() {} n   equation(long long f, long long s) { n     S.first = a.first = b.first = c.first = f; n     S.second = b.second = c.second = a.second = s; n   } n }; n equation calcCur(long long mid) { n   equation cur = equation(-9e18, 9e18); n   for (int i = 0; i < arr.size(); i++) { n     equation item; n     item.S.first = arr[i].x + arr[i].y + arr[i].z - mid; n     item.S.second = arr[i].x + arr[i].y + arr[i].z + mid; n     item.a.first = -arr[i].x + arr[i].y + arr[i].z - mid; n     item.a.second = -arr[i].x + arr[i].y + arr[i].z + mid; n     item.b.first = arr[i].x - arr[i].y + arr[i].z - mid; n     item.b.second = arr[i].x - arr[i].y + arr[i].z + mid; n     item.c.first = arr[i].x + arr[i].y - arr[i].z - mid; n     item.c.second = arr[i].x + arr[i].y - arr[i].z + mid; n     cur.S.first = max(cur.S.first, item.S.first); n     cur.S.second = min(cur.S.second, item.S.second); n     cur.a.first = max(cur.a.first, item.a.first); n     cur.a.second = min(cur.a.second, item.a.second); n     cur.b.first = max(cur.b.first, item.b.first); n     cur.b.second = min(cur.b.second, item.b.second); n     cur.c.first = max(cur.c.first, item.c.first); n     cur.c.second = min(cur.c.second, item.c.second); n   } n   return cur; n } n point getSolution(equation cur) { n   if (cur.S.first > cur.S.second || cur.a.first > cur.a.second || n       cur.b.first > cur.b.second || cur.c.first > cur.c.second || n       cur.a.second + cur.b.second + cur.c.second < cur.S.first || n       cur.a.first + cur.b.first + cur.c.first > cur.S.second) { n     return point(MAGIC); n   } n   point result; n   result.x = cur.a.first; n   result.y = cur.b.first; n   result.z = cur.c.first; n   long long delta = max(0ll, cur.S.first - result.x - result.y - result.z); n   result.x += min(cur.a.second - cur.a.first, delta); n   delta -= min(cur.a.second - cur.a.first, delta); n   result.y += min(cur.b.second - cur.b.first, delta); n   delta -= min(cur.b.second - cur.b.first, delta); n   result.z += min(cur.c.second - cur.c.first, delta); n   delta -= min(cur.c.second - cur.c.first, delta); n   assert(delta == 0); n   return result; n } n long long div2(long long x) { return (x - (x & 1)) / 2; } n point can(long long mid) { n   equation cur = calcCur(mid); n   for (int r = 0; r <= 1; r++) { n     equation final; n     final.S.first = div2(cur.S.first - 3 * r + 1); n     final.a.first = div2(cur.a.first - r + 1); n     final.b.first = div2(cur.b.first - r + 1); n     final.c.first = div2(cur.c.first - r + 1); n     final.S.second = div2(cur.S.second - 3 * r); n     final.a.second = div2(cur.a.second - r); n     final.b.second = div2(cur.b.second - r); n     final.c.second = div2(cur.c.second - r); n     point result = getSolution(final); n     if (result.x != MAGIC) { n       point answer; n       answer.x = result.y + result.z + r; n       answer.y = result.x + result.z + r; n       answer.z = result.x + result.y + r; n       return answer; n     } n   } n   return point(MAGIC); n } n void solve() { n   int n; n   cin >> n; n   arr.clear(); n   arr.resize(n); n   for (int i = 0; i < n; i++) { n     scanf( %I64d%I64d%I64d , &arr[i].x, &arr[i].y, &arr[i].z); n   } n   long long lf = -1; n   long long rg = 3e18 + 20; n   while (rg - lf > 1) { n     long long middle = lf + (rg - lf) / 2; n     if (can(middle).x != MAGIC) { n       rg = middle; n     } else { n       lf = middle; n     } n   } n   point answer; n   for (long long i = lf; i <= rg; i++) { n     if (can(i).x != MAGIC) { n       answer = can(i); n       cout << answer.x <<     << answer.y <<     << answer.z << endl; n       return; n     } n   } n   int s = -4; n   cout << arr[s].x << endl; n } n int main() { n   int t; n   cin >> t; n   for (int i = 0; i < t; i++) { n     solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   cin >> n; n   map<int, vector<int> > m; n   int e; n   for (int i = 2; i <= n; i++) { n     cin >> e; n     m[e].push_back(i); n   } n   vector<bool> leaf(n + 1, true); n   leaf[1] = false; n   for (int i = 1; i <= n; i++) { n     if (m.count(i) > 0) { n       int c = 0; n       for (auto it = m[i].begin(); it != m[i].end(); it++) { n         if (m.count(*it) != 1) c++; n       } n       if (c < 3) { n         cout <<  No n ; n         return 0; n       } n     } n   } n   cout <<  Yes n ; n } n


#include <bits/stdc++.h> n using namespace std; n double dp[2005][2005]; n int main() { n   ios::sync_with_stdio(0); n a: n   int n, t; n   double p; n   cin >> n >> p >> t; n   dp[0][0] = 1.0; n   for (int i = 1; i <= t; i++) dp[i][0] = dp[i - 1][0] * (1.0 - p); n   for (int j = 1; j <= n; j++) dp[0][j] = 0.0; n   for (int i = 1; i <= t; i++) { n     for (int j = 1; j <= n; j++) { n       if (j == n) n         dp[i][j] = dp[i - 1][j - 1] * p + dp[i - 1][j]; n       else n         dp[i][j] = dp[i - 1][j - 1] * p + dp[i - 1][j] * (1.0 - p); n     } n   } n   double res = 0.0; n   for (int i = 0; i <= n; i++) res += (dp[t][i] * ((double)i)); n   printf( %.10f n , (double)res); n } n


#include <bits/stdc++.h> n using namespace std; n int arr[1100]; n int main() { n   int n, a, b, c, t, ans = 0, sum = 0; n   cin >> n >> a >> b >> c >> t; n   for (int i = 0; i < n; i++) { n     cin >> arr[i]; n     sum += t - arr[i]; n   } n   ans = n * a; n   if (b >= c) { n     cout << ans << endl; n   } else { n     cout << ans + sum * (c - b) << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 4005; n int f[maxn][maxn], prea[maxn][26], preb[maxn][26]; n char a[maxn], b[maxn]; n int n, m, ti, td, tr, te; n int main() { n   int i, j, x, y; n   scanf( %d%d%d%d , &ti, &td, &tr, &te); n   scanf( %s%s , a + 1, b + 1); n   n = strlen(a + 1); n   m = strlen(b + 1); n   for (i = 1; i <= n; ++i) { n     memcpy(prea[i], prea[i - 1], sizeof prea[0]); n     prea[i][a[i] -  a ] = i; n     f[i][0] = i * td; n   } n   for (i = 1; i <= m; ++i) { n     memcpy(preb[i], preb[i - 1], sizeof preb[0]); n     preb[i][b[i] -  a ] = i; n     f[0][i] = i * ti; n   } n   for (i = 1; i <= n; ++i) n     for (j = 1; j <= m; ++j) { n       f[i][j] = min(f[i - 1][j] + td, f[i][j - 1] + ti); n       f[i][j] = min(f[i][j], f[i - 1][j - 1] + (a[i] != b[j] ? tr : 0)); n       x = prea[i - 1][b[j] -  a ]; n       y = preb[j - 1][a[i] -  a ]; n       if (x && y) n         f[i][j] = min(f[i][j], f[x - 1][y - 1] + te + (i - x - 1) * td + n                                    (j - y - 1) * ti); n     } n   printf( %d n , f[n][m]); n   return 0; n } n


#include <bits/stdc++.h> n void read(long long &t) { n   char c; n   while ((c = getchar()) <  0  || c >  9 ) n     ; n   t = c -  0 ; n   while ((c = getchar()) >=  0  && c <=  9 ) (t *= 10) += c -  0 ; n } n static const long long N = 100010; n long long a[N], cnt[45]; n bool is_power(long long t) { return !(t - (t & -t)); } n long long get_cnt(long long t) { n   long long ret = -1; n   while (t) t >>= 1, ++ret; n   return ret; n } n signed main() { n   long long n; n   read(n); n   for (long long i = 0; i < n; ++i) read(a[i]); n   for (long long i = 0; i < n; ++i) n     if (is_power(a[i])) ++cnt[get_cnt(a[i])]; n   long long mx = cnt[0], mn = 0; n   for (long long i = n - 1; i >= 0; --i) n     if (!is_power(a[i])) { n       long long c = get_cnt(a[i]); n       for (long long j = 44; j >= c; --j) n         if (cnt[j] > 0) { n           for (long long k = j; ~k; --k) --cnt[k]; n           goto find; n         } n       printf( -1 n ); n       return 0; n     find: n       ++mn; n     } n   while (cnt[0] > 0) n     for (long long i = 1; i < 45; ++i) n       if (cnt[i] <= 0) { n         for (long long j = 0; j < i; ++j) --cnt[j]; n         long long mx = 0, p = -1; n         for (long long j = i - 1; j >= 0; --j) n           if (cnt[j] > mx) mx = cnt[p = j]; n         if (mx) --cnt[p]; n         ++mn; n         break; n       } n   for (long long i = 0; i < 45; ++i) n     if (cnt[i]) { n       printf( -1 n ); n       return 0; n     } n   for (long long i = mn; i <= mx; ++i) printf( %lld  , i); n   printf(  n ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 2e5 + 5; n const int Mod = 1000000007; n const int INF = 0x3f3f3f3f; n const long long LL_INF = 0x3f3f3f3f3f3f3f3f; n const double e = exp(1); n const double PI = acos(-1); n const double ERR = 1e-10; n int head[maxn], cnt, n, tot, mstnum; n int dfn[maxn], father[maxn], rank_[maxn], sz[maxn]; n pair<int, int> range[maxn]; n long long c[maxn]; n bool mark[maxn]; n set<int> se; n unordered_map<long long, int> mp; n struct node { n   int v, nxt; n } Edge[2 * maxn]; n struct edge { n   int u, v, id; n   long long w; n   edge() {} n   edge(int _id, int _u, int _v, long long _w) : id(_id), u(_u), v(_v), w(_w) {} n } mst[2 * maxn]; n bool operator<(edge a, edge b) { return a.w < b.w; } n vector<edge> ve[maxn]; n void init() { n   for (int i = 0; i <= n; i++) { n     head[i] = -1; n     father[i] = i; n     rank_[i] = 0; n   } n   cnt = 0; n   tot = 0; n   mstnum = 0; n } n void addedge(int u, int v) { n   Edge[cnt].v = v; n   Edge[cnt].nxt = head[u]; n   head[u] = cnt++; n } n void dfs(int id, int fa) { n   sz[id] = 1; n   range[id].first = 0x3f3f3f3f; n   range[id].second = -1; n   for (int i = head[id]; i != -1; i = Edge[i].nxt) { n     int v = Edge[i].v; n     if (v == fa) continue; n     dfs(v, id); n     range[id].first = min(range[id].first, range[v].first); n     range[id].second = max(range[id].second, range[v].second); n     sz[id] += sz[v]; n   } n   if (sz[id] == 1) { n     dfn[id] = ++tot; n     range[id].first = range[id].second = tot; n   } n } n int find(int id) { n   return father[id] == id ? id : father[id] = find(father[id]); n } n void merge(int x, int y) { n   x = find(x); n   y = find(y); n   if (x != y) { n     if (rank_[x] > rank_[y]) n       father[y] = x; n     else { n       father[x] = y; n       if (rank_[x] == rank_[y]) rank_[y]++; n     } n   } n } n int main() { n   int u, v; n   scanf( %d , &n); n   init(); n   for (int i = 1; i <= n; i++) { n     scanf( %lld , &c[i]); n   } n   for (int i = 1; i < n; i++) { n     scanf( %d%d , &u, &v); n     addedge(u, v); n     addedge(v, u); n   } n   dfs(1, -1); n   long long ans = 0; n   int num = 0; n   for (int i = 1; i <= n; i++) { n     mst[i] = edge(i, range[i].first - 1, range[i].second, c[i]); n   } n   sort(mst + 1, mst + 1 + n); n   for (int i = 1; i <= n; i++) { n     if (mp.find(mst[i].w) == mp.end()) { n       mp[mst[i].w] = ++num; n     } n     ve[mp[mst[i].w]].push_back(mst[i]); n   } n   for (int i = 1; i <= num; i++) { n     for (auto ed : ve[i]) { n       int x = find(ed.u); n       int y = find(ed.v); n       int id = ed.id; n       if (x != y) { n         se.insert(id); n       } n     } n     for (auto ed : ve[i]) { n       int x = find(ed.u); n       int y = find(ed.v); n       if (x != y) { n         ans += ed.w; n         merge(x, y); n       } n     } n   } n   printf( %lld %d n , ans, (int)se.size()); n   for (auto x : se) { n     printf( %d  , x); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n char __buf[1 << 20], *__p1, *__p2; n int read() { n   int s = 0, w = 1; n   char ch = getchar(); n   while (ch <  0  || ch >  9 ) { n     if (ch ==  - ) w = -1; n     ch = getchar(); n   } n   while (ch >=  0  && ch <=  9 ) n     s = (s << 3) + (s << 1) + (ch ^ 48), ch = getchar(); n   return s * w; n } n const int maxn = 710; n struct Wint : vector<long long> { n   const static long long BIT = 1e8; n   Wint(long long n = 0) { n     push_back(n); n     check(); n   } n   Wint& operator=(const char* num) { n     int Len = strlen(num) - 1; n     clear(); n     for (int i = Len; i >= 0; i -= 9) { n       push_back(0); n       long long w = 1; n       for (int j = i; j > i - 9 && j >= 0; --j) n         back() += (num[j] ^ 48) * w, w *= 10; n     } n     return *this; n   } n   Wint& check() { n     while (!empty() && !back()) pop_back(); n     if (empty()) return *this; n     for (int i = 1; i < size(); ++i) n       (*this)[i] += (*this)[i - 1] / BIT, (*this)[i - 1] %= BIT; n     while (back() >= BIT) { n       push_back(back() / BIT); n       (*this)[size() - 2] %= BIT; n     } n     return *this; n   } n }; n bool operator<(Wint a, Wint b) { n   if (a.size() != b.size()) return a.size() < b.size(); n   for (int i = a.size() - 1; i >= 0; --i) n     if (a[i] != b[i]) return a[i] < b[i]; n   return 0; n } n bool operator>(Wint a, Wint b) { return b < a; } n bool operator<=(Wint a, Wint b) { return !(a > b); } n bool operator>=(Wint a, Wint b) { return !(a < b); } n bool operator!=(Wint a, Wint b) { return a < b || b < a; } n bool operator==(Wint a, Wint b) { return !(a < b) && !(b < a); } n Wint& operator+=(Wint& a, Wint b) { n   if (a.size() < b.size()) a.resize(b.size()); n   for (int i = 0; i < b.size(); ++i) a[i] += b[i]; n   return a.check(); n } n Wint operator+(Wint a, Wint b) { return a += b; } n Wint& operator-=(Wint& a, Wint b) { n   for (int i = 0; i < b.size(); a[i] -= b[i], ++i) n     if (a[i] < b[i]) { n       int j = i + 1; n       while (!a[j]) ++j; n       while (j > i) --a[j], a[--j] += Wint::BIT; n     } n   return a.check(); n } n Wint operator-(Wint a, Wint b) { return a -= b; } n Wint operator*(Wint a, Wint b) { n   Wint n; n   n.assign(a.size() + b.size() - 1, 0); n   for (int i = 0; i < a.size(); ++i) n     for (int j = 0; j < b.size(); ++j) n[i + j] += a[i] * b[j]; n   return n.check(); n } n Wint& operator*=(Wint& a, Wint b) { return a = a * b; } n Wint operator/(Wint a, int b) { n   Wint n; n   bool wp = 0; n   long long t = 0; n   for (int i = a.size() - 1; i >= 0; --i) { n     t = t * Wint::BIT + a[i]; n     if (wp || t / b) wp = 1, n.push_back(t / b); n     t %= b; n   } n   reverse(n.begin(), n.end()); n   return n; n } n Wint& operator/=(Wint& a, int b) { return a = a / b; } n void writeX(Wint n) { n   if (n.empty()) return putchar( 0 ), void(); n   int Len = n.size() - 1; n   printf( %lld , n[Len]); n   for (int i = Len - 1; i >= 0; --i) printf( %08lld , n[i]); n } n Wint Win(long long num) { n   Wint t = num; n   return t; n } n vector<int> G[maxn]; n Wint f[maxn][maxn], t[maxn]; n int n, sz[maxn]; n void add(int x, int y) { G[x].push_back(y), G[y].push_back(x); } n template <class T> n void Max(T& x, T y) { n   x = max(x, y); n } n void dfs(int x, int pa) { n   f[x][1] = Win(1), sz[x] = 1; n   for (int y : G[x]) n     if (y ^ pa) { n       dfs(y, x); n       for (int i = 1; i <= sz[x]; i++) { n         for (int j = 1; j <= sz[y]; j++) n           Max(t[i], f[x][i] * f[y][j] * Win(j)), n               Max(t[i + j], f[x][i] * f[y][j]); n       } n       for (int i = 1; i <= sz[x] + sz[y]; i++) f[x][i] = t[i], t[i] = Win(0); n       sz[x] += sz[y]; n     } n } n signed main() { n   n = read(); n   for (int i = 1; i < n; i++) add(read(), read()); n   dfs(1, 0); n   Wint ans = Win(0); n   for (int i = 1; i <= n; i++) ans = max(ans, f[1][i] * Win(i)); n   writeX(ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int N, Q, T; n bool matr[300][300]; n int amounts[300]; n vector<int> chains[300]; n bool was[300]; n int sorted[300]; n int s = 0; n long long mas[2][100010]; n void srt(int v) { n   was[v] = true; n   for (int i = 0; i < N; i++) n     if (!was[i] && matr[v][i]) srt(i); n   sorted[s] = v; n   s++; n } n bool z = false; n void f(int v, int c) { n   was[v] = true; n   for (int i = 0; i < N; i++) n     if (matr[v][i]) { n       if (was[i]) n         z = true; n       else n         f(i, c); n     } n   chains[c].push_back(v); n } n int main() { n   scanf( %d%d%d , &N, &Q, &T); n   for (int i = 0; i < N; i++) scanf( %d , &amounts[i]); n   for (int i = 0; i < 300; i++) n     for (int j = 0; j < 300; j++) matr[i][j] = false; n   for (int i = 0; i < Q; i++) { n     int a, b; n     scanf( %d%d , &a, &b); n     a--; n     b--; n     matr[a][b] = true; n   } n   for (int i = 0; i < 300; i++) was[i] = false; n   for (int i = 0; i < N; i++) n     if (!was[i]) srt(i); n   reverse(sorted, sorted + N); n   for (int i = 0; i < 300; i++) was[i] = false; n   int cCnt = 0; n   for (int i = 0; i < N; i++) n     if (!was[sorted[i]]) { n       f(sorted[i], cCnt); n       cCnt++; n     } n   for (int i = 0; i < cCnt; i++) { n     for (int j = chains[i].size() - 1; j >= 0; j--) { n       chains[i][j] = amounts[chains[i][j]]; n       if (j + 1 < chains[i].size()) chains[i][j] += chains[i][j + 1]; n     } n   } n   if (z) { n     printf( %d n , 0); n     return 0; n   } n   memset(mas, 0, sizeof(long long) * 100010 * 2); n   mas[0][0] = 1; n   int index = 0; n   for (int i = 0; i < cCnt; i++) { n     for (int j = 0; j < chains[i].size(); j++) { n       int amount = chains[i][j]; n       for (int k = 0; k < 100010; k++) mas[(index + 1) % 2][k] = 0; n       for (int k = 0; k < 100010; k++) { n         if (k - amount >= 0) n           mas[index % 2][k] = n               (mas[index % 2][k] + mas[index % 2][k - amount]) % 1000000007; n         if (j == 0) n           mas[(index + 1) % 2][k] = mas[index % 2][k]; n         else if (k - amount >= 0) n           mas[(index + 1) % 2][k] = mas[index % 2][k - amount]; n       } n       index++; n     } n   } n   cout << mas[index % 2][T] <<   n ; n   return 0; n } n