#include <bits/stdc++.h> n using namespace std; n const double PI = 3.141592653589793238; n long long powmod(long long a, long long b) { n   long long res = 1; n   a %= 1000000007; n   for (; b; b >>= 1) { n     if (b & 1) res = res * a % 1000000007; n     a = a * a % 1000000007; n   } n   return res; n } n long long fac[1000005]; n void factorial(int mx) { n   fac[0] = 1ll; n   for (int i = 1; i <= mx; i++) { n     fac[i] = fac[i - 1] * (long long)i; n     fac[i] %= 1000000007; n   } n } n int main() { n   factorial(1000005 - 5); n   int n, m, vol, type; n   long long ans, cnt = 1ll; n   scanf( %d , &n); n   scanf( %d , &m); n   vector<vector<int> > vec(m); n   for (int i = 0; i < n; i++) { n     scanf( %d , &vol); n     while (vol--) { n       scanf( %d , &type); n       vec[type - 1].push_back(i); n     } n   } n   for (int i = 0; i < m; i++) sort(vec[i].begin(), vec[i].end()); n   sort(vec.begin(), vec.end()); n   ans = 1ll; n   for (int i = 1; i <= m - 1; i++) { n     if (vec[i] == vec[i - 1]) n       ans *= ++cnt; n     else n       cnt = 1; n     ans %= 1000000007; n   } n   printf( %lld n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int t; n   cin >> t; n   for (int ti = 0; ti < t; ti++) { n     int n, k; n     cin >> n >> k; n     vector<int> arr(n); n     for (int i = 0; i < n; i++) cin >> arr[i]; n     sort(arr.begin(), arr.end()); n     vector<int> weights(k); n     for (int i = 0; i < k; i++) cin >> weights[i]; n     sort(weights.begin(), weights.end()); n     long long total = 0; n     for (int i = n - 1; i >= n - k; i--) { n       total += arr[i]; n     } n     int w = 0; n     for (int i = k - 1; i >= 0; i--) { n       if (weights[i] <= 1) break; n       total += arr[w]; n       w += weights[i] - 1; n     } n     for (int i = 0; i < k; i++) { n       if (weights[i] > 1) break; n       total += arr[n - 1 - i]; n     } n     cout << total << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n long long ago; n using namespace std; n template <class T = long long int> n T nxt() { n   T x; n   cin >> x; n   return x; n } n long long int pw(long long int a, long long int b, long long int mod) { n   if (!b) return 1; n   if (b & 1) return a * pw(a * a % mod, b / 2, mod) % mod; n   return pw(a * a % mod, b / 2, mod) % mod; n } n const long long int N = 1000 + 10; n const long long int INF = 8e18; n const long long int MOD = 1e9 + 7; n long long int n, m, mark[N]; n double dist[N], S[N], Z[N]; n vector<pair<long long int, double>> adj[N]; n priority_queue<pair<double, long long int>, vector<pair<double, long long int>>, n                greater<pair<double, long long int>>> n     pq; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(nullptr); n   cout.tie(nullptr); n   cout << setprecision(15) << fixed; n   cin >> n; n   for (long long int i = 1; i <= n; i++) { n     for (long long int j = 1; j <= n; j++) { n       long long int p = nxt(); n       if (i == j || p == 0) continue; n       adj[j].emplace_back(i, double(p) / 100); n     } n   } n   fill(Z, Z + N, 1.0); n   fill(dist, dist + N, INF); n   dist[n] = 0; n   pq.push({0, n}); n   while (!pq.empty()) { n     long long int u = pq.top().second; n     pq.pop(); n     if (mark[u]) continue; n     mark[u] = 1; n     for (auto [v, p] : adj[u]) { n       if (mark[v]) continue; n       S[v] += Z[v] * p * (dist[u] + 1); n       Z[v] *= (1 - p); n       dist[v] = (S[v] + Z[v]) / (1 - Z[v]); n       pq.push({dist[v], v}); n     } n   } n   cout << dist[1] <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   string s; n   int n; n   cin >> n >> s; n   vector<int> vec; n   for (int i = 0; i < s.length(); ++i) { n     if (s[i] ==  B ) { n       int res = 0; n       while (i < s.length() && s[i] ==  B ) { n         ++res; n         ++i; n       } n       vec.push_back(res); n       continue; n     } n   } n   cout << vec.size() <<   n ; n   for (int i = 0; i < (int)vec.size(); ++i) { n     cout << vec[i] <<    ; n   } n   cout <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n const int MaxN = 1e5; n struct NOOD { n   int flag; n   int l; n   int r; n   int v; n } num[MaxN + 5]; n int ans[MaxN + 5], p[MaxN + 5]; n bool ok[MaxN + 5], flag; n int Max; n int n, m; n int main() { n   scanf( %d%d , &n, &m); n   for (int i = 1; i <= m; i++) { n     scanf( %d%d%d%d , &num[i].flag, &num[i].l, &num[i].r, &num[i].v); n   } n   for (int i = 1; i <= n; i++) ans[i] = -100000000; n   for (int i = m; i >= 1; i--) { n     if (num[i].flag == 1) { n       for (int j = num[i].l; j <= num[i].r; j++) ans[j] -= num[i].v; n     } else { n       for (int j = num[i].l; j <= num[i].r; j++) { n         if (ok[j] && ans[j] < num[i].v) continue; n         ans[j] = num[i].v; n         ok[j] = 1; n       } n     } n   } n   for (int i = 1; i <= n; i++) p[i] = ans[i]; n   for (int i = 1; i <= m; i++) { n     if (num[i].flag == 1) { n       for (int j = num[i].l; j <= num[i].r; j++) p[j] += num[i].v; n     } else { n       Max = -100000000; n       for (int j = num[i].l; j <= num[i].r; j++) Max = max(Max, p[j]); n       if (Max != num[i].v) flag = 1; n     } n   } n   if (flag) n     printf( NO n ); n   else { n     printf( YES n ); n     for (int i = 1; i <= n; i++) printf( %d  , ans[i]); n     printf(  n ); n   } n } n


#include <bits/stdc++.h> n using namespace std; n long long solve() { n   long long n, i, j, k; n   cin >> n >> k; n   long long arr[n]; n   for (i = 0; i < n; i++) cin >> arr[i]; n   sort(arr, arr + n); n   j = 0; n   for (i = 0; i < n && arr[i] < 0; i += k) j += 2 * abs(arr[i]); n   for (i = n - 1; i >= 0 && arr[i] > 0; i -= k) j += 2 * arr[i]; n   j -= max(abs(arr[0]), abs(arr[n - 1])); n   return j; n } n int main() { n   long long t; n   cin >> t; n   while (t--) cout << solve() << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   string a, b, tem; n   scanf( %d , &n); n   a =  <3 ; n   for (int i = 0, j = 0; i < n; ++i) { n     cin >> tem; n     a += tem; n     a +=  <3 ; n   } n   cin >> b; n   int len1 = a.length(), po1 = 0, po2 = 0; n   int len2 = b.length(); n   while (1) { n     if (po1 == len1) { n       printf( yes n ); n       return 0; n     } n     if (po2 == len2) { n       printf( no n ); n       return 0; n     } n     if (a[po1] == b[po2]) po1++; n     po2++; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const double eps = 1e-8; n const int inf = 0x3f3f3f3f; n const int Minf = 0x7f7f7f7f; n const long long INF = 0x3f3f3f3f3f3f3f3fll; n const long long MINF = 0x7f7f7f7f7f7f7f7fll; n const int N = 2e5 + 50; n vector<int> t[N]; n int dfn[N], rig[N]; n int fa[N][20], dep[N]; n void dfs(int now, int f) { n   fa[now][0] = f; n   dfn[now] = ++dfn[0]; n   dep[now] = dep[f] + 1; n   for (int i = (1); i <= 19; ++i) fa[now][i] = fa[fa[now][i - 1]][i - 1]; n   for (int to : t[now]) { n     if (to == f) continue; n     dfs(to, now); n   } n   rig[now] = dfn[0]; n } n struct node { n   int l, r; n   int tag; n   int mxn; n   int cnt; n }; n struct seg { n   node t[N << 2]; n   void updata(int now) { n     t[now].mxn = max(t[(now << 1)].mxn, t[(now << 1 | 1)].mxn); n     t[now].cnt = 0; n     if (t[now].mxn == t[(now << 1)].mxn) t[now].cnt += t[(now << 1)].cnt; n     if (t[now].mxn == t[(now << 1 | 1)].mxn) n       t[now].cnt += t[(now << 1 | 1)].cnt; n   } n   void bt(int now, int l, int r) { n     t[now].l = l; n     t[now].r = r; n     if (l == r) { n       t[now].mxn = 0; n       t[now].cnt = 1; n       return; n     } n     bt((now << 1), l, ((t[now].l + t[now].r) >> 1)); n     bt((now << 1 | 1), ((t[now].l + t[now].r) >> 1) + 1, r); n     updata(now); n   } n   void pushdown(int now) { n     if (!t[now].tag) return; n     t[(now << 1)].mxn += t[now].tag; n     t[(now << 1)].tag += t[now].tag; n     t[(now << 1 | 1)].mxn += t[now].tag; n     t[(now << 1 | 1)].tag += t[now].tag; n     t[now].tag = 0; n   } n   void change(int now, int l, int r, int v) { n     if (l <= t[now].l && t[now].r <= r) { n       t[now].mxn += v; n       t[now].tag += v; n       return; n     } n     pushdown(now); n     if (l <= ((t[now].l + t[now].r) >> 1)) change((now << 1), l, r, v); n     if (r > ((t[now].l + t[now].r) >> 1)) change((now << 1 | 1), l, r, v); n     updata(now); n   } n } sgt; n bool in_tree(int y, int x) { return dfn[y] <= rig[x]; } n int main() { n   int n, q, x, y; n   scanf( %d%d , &n, &q); n   for (int i = (2); i <= n; ++i) n     scanf( %d%d , &x, &y), t[x].push_back(y), t[y].push_back(x); n   dfs(1, 0); n   sgt.bt(1, 1, n); n   map<pair<int, int>, int> vis; n   int link = 0, v = 0; n   for (int i = (1); i <= q; ++i) { n     scanf( %d%d , &x, &y); n     if (dfn[x] > dfn[y]) swap(x, y); n     if (vis[make_pair(x, y)]) n       vis[make_pair(x, y)] = 0, v = -1, --link; n     else n       vis[make_pair(x, y)] = 1, v = 1, ++link; n     if (in_tree(y, x)) { n       int u = y; n       for (int i = (19); i >= 0; --i) n         if (dep[fa[u][i]] > dep[x]) u = fa[u][i]; n       sgt.change(1, 1, n, v); n       sgt.change(1, dfn[u], rig[u], -1 * v); n       sgt.change(1, dfn[y], rig[y], v); n     } else { n       sgt.change(1, dfn[x], rig[x], v); n       sgt.change(1, dfn[y], rig[y], v); n     } n     node ans = sgt.t[1]; n     if (ans.mxn == link) { n       printf( %d n , ans.cnt); n     } else n       puts( 0 ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   double r, x1, y1, x2, y2; n   cin >> r >> x1 >> y1 >> x2 >> y2; n   long long x = n       (long long)sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2)) / 2 / r; n   if (x * 2.0 * r < sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2))) n     cout << x + 1; n   else n     cout << x; n   return 0; n } n


#include <bits/stdc++.h> n #pragma GCC optimize( O3 ) n using namespace std; n unsigned seed = chrono::system_clock::now().time_since_epoch().count(); n mt19937 eng(seed); n long long random2() { return (1ll << 31ll) * eng() + eng(); } n struct FT { n   vector<long long> s; n   FT(int n) : s(n) {} n   void update(int pos, long long dif) { n     for (; pos < (int)(s).size(); pos |= pos + 1) s[pos] += dif; n   } n   long long query(int pos) { n     long long res = 0; n     for (; pos > 0; pos &= pos - 1) res += s[pos - 1]; n     return res; n   } n   int lower_bound(long long sum) { n     if (sum <= 0) return -1; n     int pos = 0; n     for (int pw = 1 << 25; pw; pw >>= 1) { n       if (pos + pw <= (int)(s).size() && s[pos + pw - 1] < sum) n         pos += pw, sum -= s[pos - 1]; n     } n     return pos; n   } n }; n long long n, m, k, q, T; n const long long big = 1000000007; n const long long big2 = 1000000009; n const long long mod = 998244353; n const int MAXN = 600001; n const int SQRT = 500; n vector<int> smalls; n vector<long long> A; n long long amod[SQRT] = {0}; n long long bigs = 0; n long long brute(long long i) { n   long long res = 0; n   for (int c1 = 0; c1 < (i + 1); ++c1) { n     for (int c2 = 0; c2 < (i + 1); ++c2) { n       res += A[c1] % A[c2]; n     } n   } n   return res; n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   long long a, b, c, d, e; n   cin >> n; n   FT ft1(MAXN); n   FT ft2(MAXN); n   long long upscore = 0; n   long long ans = 0; n   for (int c1 = 0; c1 < (n); ++c1) { n     cin >> a; n     A.push_back(a); n     if (a < SQRT) { n       ans += amod[a]; n       ans += ft2.query(a) + a * bigs; n       for (auto& y : smalls) { n         ans += a % y; n       } n       smalls.push_back(a); n     } else { n       long long temp1 = upscore; n       long long a2 = a; n       while (1) { n         temp1 -= (long long(c1) - ft1.query(a2)) * a; n         if (a2 > MAXN / 2) break; n         a2 += a; n       } n       ans += temp1; n       ans += ft2.query(a) + a * bigs; n       for (auto& y : smalls) { n         ans += a % y; n       } n       a2 = a; n       while (1) { n         ft2.update(a2 - 1, -a); n         if (a2 > MAXN / 2) break; n         a2 += a; n       } n       bigs++; n     } n     ft1.update(a, 1); n     for (int c2 = 1; c2 < (SQRT); ++c2) { n       amod[c2] += a % c2; n     } n     upscore += a; n     cout << ans <<    ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<pair<long long, long long> > v; n vector<long long> p; n int main() { n   long long n, i, j, x; n   long long m; n   cin >> n; n   cin >> m; n   for (i = 0; i < n; i++) { n     cin >> x; n     p.push_back(x); n   } n   long long z = *max_element(p.begin(), p.end()), sum = 0; n   for (i = 0; i < n; i++) { n     sum += z - p[i]; n   } n   if (sum >= m) { n     cout << z <<     << *max_element(p.begin(), p.end()) + m << endl; n   } else { n     long long k = m - sum; n     long long y = k % n; n     long long q = k / n; n     if (y != 0) n       cout << z + q + 1 <<     << *max_element(p.begin(), p.end()) + m << endl; n     else n       cout << z + q <<     << *max_element(p.begin(), p.end()) + m << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long int a, b, c, d, i, j = 1, k, l = 0, n, m, t, s = 0, p, r, ans, n                                w = 0, y, x, lcm, gcd, q, count; n   string str; n   vector<string> vc; n   vector<string> vc2; n   map<long long int, long long int> mp; n   set<string> stringstream; n   cin >> n >> m; n   for (j = n + 1;; j++) { n     l = 0; n     for (i = 2; i <= sqrt(j); i++) { n       if (j % i == 0) { n         l = 1; n         break; n       } n     } n     if (l == 0) { n       if (j == m) n         cout <<  YES  << endl; n       else n         cout <<  NO  << endl; n       return 0; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 2e5 + 10; n int n; n vector<pair<int, int> > v[maxn]; n int size[maxn]; n long long mi, mx; n void dfs(int first, int fa) { n   size[first] = 1; n   for (pair<int, int> i : v[first]) { n     if (i.first == fa) continue; n     dfs(i.first, first); n     if (size[i.first] & 1) mi += 1LL * i.second; n     size[first] += size[i.first]; n     mx += 1LL * min(n - size[i.first], size[i.first]) * i.second; n   } n } n void solve() { n   scanf( %d , &n); n   n = n * 2; n   for (int i = 1; i <= n; i++) v[i].clear(); n   for (int i = 1, first, second, z; i < n; i++) { n     scanf( %d%d%d , &first, &second, &z); n     v[first].push_back({second, z}); n     v[second].push_back({first, z}); n   } n   mi = 0, mx = 0; n   dfs(1, 0); n   printf( %lld %lld n , mi, mx); n } n int T; n int main() { n   scanf( %d , &T); n   while (T--) solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 3e5 + 7; n const int MOD = 1e9 + 7; n const int LEN = 8, MAGIC = 1e8; n int a[N], b[N]; n int n; n char bignum[N]; n long long m[N]; n int tot; n int f[N], g[N]; n int add(int a, int b) { n   a += b; n   if (a < 0) a += MOD; n   if (a >= MOD) a -= MOD; n   return a; n } n int divide(int d) { n   long long remain = 0; n   for (int i = tot - 1; i >= 0; i--) { n     remain = remain * MAGIC + m[i]; n     m[i] = remain / d; n     remain = remain % d; n   } n   while (tot > 1 && m[tot - 1] == 0) tot--; n   return remain; n } n int main() { n   scanf( %d , &n); n   for (int i = 1; i < n; i++) { n     scanf( %d , &a[i]); n   } n   a[n] = 1e9 + 7; n   for (int i = 1; i <= n; i++) { n     scanf( %d , &b[i]); n   } n   { n     scanf( %s , bignum); n     tot = 0; n     int cur = 0, l = strlen(bignum); n     for (int i = l - 1; i >= 0; i -= LEN) { n       cur = 0; n       for (int j = max(0, i - LEN + 1); j <= i; j++) { n         cur = cur * 10 + bignum[j] -  0 ; n       } n       m[tot++] = cur; n     } n   } n   int carry = 0; n   f[0] = 1; n   for (int i = 1; i <= n; i++) { n     int new_carry = carry + b[i], s = 0; n     fill(g, g + new_carry + 1, 0); n     for (int j = 0; j <= new_carry; j++) { n       s = add(s, f[j]); n       if (j > b[i]) { n         s = add(s, -f[j - b[i] - 1]); n       } n       g[j] = s; n     } n     fill(f, f + carry + 1, 0); n     if (a[i] > 1) { n       int remainder = divide(a[i]); n       for (int ii = remainder, jj = 0; ii <= new_carry; ii += a[i], jj++) { n         f[jj] = g[ii]; n       } n       carry = (new_carry - remainder) / a[i]; n     } else if (a[i] == 1) { n       for (int i = 0; i <= new_carry; i++) f[i] = g[i]; n       carry = new_carry; n     } else { n       assert(false); n     } n   } n   if (tot == 1 && m[0] == 0) { n     printf( %d n , f[0]); n   } else { n     puts( 0 ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAX = 100005; n bool black[MAX]; n vector<int> vec[MAX]; n bool old_black[MAX]; n const int MAX_BLOCK_SIZE = 600; n int t[MAX], v[MAX]; n bool used[MAX]; n vector<pair<pair<int, int>, int>> v2[MAX]; n int push[MAX]; n bool clear[MAX]; n void dfs_1(int pos, int prev = -1, int white = 0, int dist = 0) { n   if (used[pos]) { n     if (prev != -1) v2[prev].push_back(make_pair(make_pair(pos, white), dist)); n     for (int a : vec[pos]) dfs_1(a, pos, 0, 0); n   } else { n     if (!black[pos]) white++; n     for (int a : vec[pos]) dfs_1(a, prev, white, dist + 1); n   } n } n void make_1(int pos) { n   if (!black[pos]) { n     black[pos] = true; n     return; n   } n   push[pos]++; n   for (auto a : v2[pos]) n     if (a.first.second + 1 <= push[pos]) make_1(a.first.first); n } n void make_2(int pos) { n   black[pos] = false; n   push[pos] = 0; n   clear[pos] = true; n   for (auto &a : v2[pos]) { n     a.first.second = a.second; n     make_2(a.first.first); n   } n } n void dfs_2(int pos, int p = 0, bool cl = false) { n   if (used[pos]) { n     p = push[pos]; n     cl |= clear[pos]; n   } else { n     black[pos] = old_black[pos]; n     if (cl) black[pos] = false; n     if (!black[pos] && p) { n       black[pos] = true; n       p--; n     } n   } n   for (int a : vec[pos]) dfs_2(a, p, cl); n } n int main() { n   ios_base::sync_with_stdio(); n   cin.tie(0); n   cout.tie(0); n   int n, q; n   cin >> n >> q; n   for (int i = 2; i <= n; i++) { n     int a; n     cin >> a; n     vec[a].push_back(i); n   } n   for (int i = 1; i <= q; i++) cin >> t[i] >> v[i]; n   int root = 1; n   for (int i = 1; i <= q; i += MAX_BLOCK_SIZE) { n     for (int j = 1; j <= n; j++) { n       used[j] = false; n       v2[j].clear(); n       old_black[j] = black[j]; n       push[j] = 0; n       clear[j] = false; n     } n     for (int j = 0; j < MAX_BLOCK_SIZE && i + j <= q; j++) n       used[v[i + j]] = true; n     dfs_1(root); n     for (int j = 0; j < MAX_BLOCK_SIZE && i + j <= q; j++) { n       int t = ::t[i + j]; n       int v = ::v[i + j]; n       if (t == 1) n         make_1(v); n       else if (t == 2) n         make_2(v); n       else n         cout << (black[v] ?  black  :  white ) << endl; n     } n     dfs_2(root); n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int INF = 0x3f3f3f3f; n const int mod = 1e9 + 7; n const double eps = 1e-6; n int x[8], y[8]; n bool check(int a, int b) { n   return a >= min({x[0], x[1], x[2], x[3]}) && n          a <= max({x[0], x[1], x[2], x[3]}) && n          b >= min({y[0], y[1], y[2], y[3]}) && n          b <= max({y[0], y[1], y[2], y[3]}); n } n int main() { n   for (int i = 0; i < 8; i++) scanf( %d%d , x + i, y + i); n   int tx = (x[4] + x[6]) / 2; n   int ty = (y[4] + y[6]) / 2; n   int d = abs(x[4] - tx) + abs(y[4] - ty); n   for (int xx = -100; xx <= 100; xx++) n     for (int yy = -100; yy <= 100; yy++) n       if (abs(xx - tx) + abs(yy - ty) <= d) n         if (check(xx, yy)) return cout <<  YES , 0; n   cout <<  NO ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const double pi = acos(-1.0), eps = 1e-4; n const int inf = ~0U >> 2; n int mod = 1e9 + 7; n int Pow(int x, long long t) { n   int r = 1; n   for (; t; t >>= 1, x = (long long)x * x % mod) n     if (t & 1) r = (long long)r * x % mod; n   return r; n } n const long long linf = (long long)inf * inf; n const int N = 5, M = 101010; n int xcost[N][M], ycost[N][M]; n int n, m; n long long f[2][1 << N]; n void Update(long long &x, long long d) { n   if (x > d) x = d; n } n int main() { n   scanf( %d%d , &n, &m); n   for (int i = 0; i < m - 1; ++i) n     for (int j = 0; j < n; ++j) scanf( %d , &xcost[j][i]); n   for (int i = 0; i < m; ++i) n     for (int j = 0; j < n; ++j) scanf( %d , &ycost[j][i]); n   int c = 0; n   for (int i = 0; i < 1 << n; ++i) f[c][i] = linf; n   f[c][(1 << n) - 1] = 0; n   for (int i = 1; i < m; ++i) n     for (int j = 0; j < n; ++j) { n       c ^= 1; n       for (int i = 0; i < 1 << n; ++i) f[c][i] = linf; n       for (int mask = 0; mask < 1 << n; ++mask) { n         int newmask = mask & (~(1 << j)); n         long long Cost = 0; n         if (j >= 1) Cost += mask >> (j - 1) & 1 ? 0 : ycost[j - 1][i]; n         if (j == n - 1) Cost += mask & 1 ? 0 : ycost[j][i]; n         Update(f[c][newmask | (1 << j)], f[c ^ 1][mask] + Cost); n         Cost = mask >> j & 1 ? xcost[j][i - 1] : 0; n         if (j >= 1) Cost += mask >> (j - 1) & 1 ? ycost[j - 1][i] : 0; n         if (j == n - 1) Cost += mask & 1 ? ycost[j][i] : 0; n         Update(f[c][newmask], f[c ^ 1][mask] + Cost); n       } n     } n   cout << f[c][0] << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int mod = 1000000007; n int a[1005]; n int main() { n   int x, y, n; n   cin >> x >> y; n   int a[10] = {-x, -y, x - y, x, y, y - x}; n   cin >> n; n   int ans = (n + 2) % 6; n   a[ans] %= mod; n   if (a[ans] < 0) a[ans] += mod; n   cout << a[ans] << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long int M = 1e9 + 7; n long long int cdiv(long long int a, long long int b) { n   return (a % b == 0) ? a / b : a / b + 1; n } n long long int myMod(long long int a, long long int b) { n   long long int r = a % b; n   return r < 0 ? r + b : r; n } n long long int ModPow(long long int a, long long int b, long long int M) { n   if (M == 1) return 0; n   a %= M; n   long long int ans = 1, t = 1; n   while (t > 0 && t <= b) { n     if (t & b) { n       ans *= a; n       ans %= M; n     } n     t <<= 1; n     a *= a; n     a %= M; n   } n   return ans; n } n struct seg { n   long long int l, r, u, d; n }; n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   long long int n; n   cin >> n; n   seg a[n]; n   for (long long int i = 0; i < n; i++) n     cin >> a[i].l >> a[i].d >> a[i].r >> a[i].u; n   long long int mxll[n], mxlr[n], mnrl[n], mnrr[n]; n   mxll[0] = a[0].l, mxlr[n - 1] = a[n - 1].l, mnrl[0] = a[0].r, n   mnrr[n - 1] = a[n - 1].r; n   for (long long int i = 1; i < n; i++) { n     mxll[i] = max(mxll[i - 1], a[i].l); n     mnrl[i] = min(mnrl[i - 1], a[i].r); n   } n   for (long long int i = n - 1 - 1; i >= 0; i--) { n     mxlr[i] = max(mxlr[i + 1], a[i].l); n     mnrr[i] = min(mnrr[i + 1], a[i].r); n   } n   long long int mxdl[n], mxdr[n], mnul[n], mnur[n]; n   mxdl[0] = a[0].d, mxdr[n - 1] = a[n - 1].d, mnul[0] = a[0].u, n   mnur[n - 1] = a[n - 1].u; n   for (long long int i = 1; i < n; i++) { n     mxdl[i] = max(mxdl[i - 1], a[i].d); n     mnul[i] = min(mnul[i - 1], a[i].u); n   } n   for (long long int i = n - 1 - 1; i >= 0; i--) { n     mxdr[i] = max(mxdr[i + 1], a[i].d); n     mnur[i] = min(mnur[i + 1], a[i].u); n   } n   if (mnrr[1] - mxlr[1] >= 0 && mnur[1] - mxdr[1] >= 0) { n     cout << mxlr[1] <<     << mxdr[1] <<   n ; n     return 0; n   } n   for (long long int i = 1; i < n - 1; i++) { n     long long int cix = n         min(mnrl[i - 1], mnrr[i + 1]) - max(mxll[i - 1], mxlr[i + 1]); n     long long int ciy = n         min(mnul[i - 1], mnur[i + 1]) - max(mxdl[i - 1], mxdr[i + 1]); n     if (cix >= 0 && ciy >= 0) { n       cout << max(mxll[i - 1], mxlr[i + 1]) <<     n            << max(mxdl[i - 1], mxdr[i + 1]); n       return 0; n     } n   } n   if (mnrl[n - 2] - mxll[n - 2] >= 0 && mnul[n - 2] - mxdl[n - 2] >= 0) { n     cout << mxll[n - 2] <<     << mxdl[n - 2] <<   n ; n     return 0; n   } n   return 0; n } n


#include <bits/stdc++.h> n const int MAX = 111; n using namespace std; n int A[MAX]; n int main() { n   int N, a, b; n   scanf( %d%d%d , &N, &a, &b); n   for (int i = 0; i < (a); ++i) { n     int(x); n     scanf( %d , &x); n     A[x] = 1; n   } n   for (int i = 0; i < (b); ++i) { n     int(x); n     scanf( %d , &x); n     if (!A[x]) A[x] = 2; n   } n   for (int i = (1); i <= (N); ++i) printf( %d  , A[i]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long t, T; n   cin >> t; n   for (T = 0; T < t; T++) { n     long long l, i; n     string s, s1, s2; n     cin >> s; n     l = s.size(); n     for (i = 0; i < l / 2; i++) { n       s1.push_back(s[i]); n     } n     for (i = l / 2; s[i] !=   0 ; i++) { n       s2.push_back(s[i]); n     } n     if (s1 == s2) n       cout <<  YES  << endl; n     else n       cout <<  NO  << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int a[150], b[150]; n void put(int x, int y[]) { n   if (x == 1) n     y[1] = 1; n   else if (x == 2) { n     y[1] = 3; n     y[2] = 4; n   } else { n     for (int i = 1; i <= x - 1; i++) y[i] = 2; n     y[x] = x - 2; n   } n } n int main() { n   int n, m; n   scanf( %d%d , &n, &m); n   put(n, a); n   put(m, b); n   for (int i = 1; i <= n; i++) n     for (int j = 1; j <= m; j++) { n       printf( %d , a[i] * b[j]); n       if (j == m) n         printf(  n ); n       else n         printf(   ); n     } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T> n vector<T>& operator--(vector<T>& v) { n   for (auto& i : v) --i; n   return v; n } n template <typename T> n vector<T>& operator++(vector<T>& v) { n   for (auto& i : v) ++i; n   return v; n } n template <typename T> n istream& operator>>(istream& is, vector<T>& v) { n   for (auto& i : v) is >> i; n   return is; n } n template <typename T> n ostream& operator<<(ostream& os, vector<T>& v) { n   for (auto& i : v) os << i <<    ; n   return os; n } n template <typename T, typename U> n istream& operator>>(istream& is, pair<T, U>& p) { n   is >> p.first >> p.second; n   return is; n } n template <typename T, typename U> n ostream& operator<<(ostream& os, pair<T, U>& p) { n   os << p.first <<     << p.second; n   return os; n } n template <typename T, typename U> n pair<T, U> operator-(pair<T, U> a, pair<T, U> b) { n   return make_pair(a.first - b.first, a.second - b.second); n } n template <typename T, typename U> n pair<T, U> operator+(pair<T, U> a, pair<T, U> b) { n   return make_pair(a.first + b.first, a.second + b.second); n } n template <typename T, typename U> n void umin(T& a, U b) { n   if (a > b) a = b; n } n template <typename T, typename U> n void umax(T& a, U b) { n   if (a < b) a = b; n } n int main() { n   int T; n   cin >> T; n   while (T--) { n     long long a, b, p, q; n     cin >> a >> b >> p >> q; n     long long t = min(b - a + 1, (long long)sqrt(b - a) + 5); n     vector<pair<long long, int>> v; n     auto g = [&](long long x) { return p * x * 2 % (q * 2); }; n     auto f = [&](long long x) { return abs(g(x) - q); }; n     42; n     ; n     for (int i = 0; i < t; ++i) { n       v.emplace_back(g(i), i); n     } n     long long ans = a; n     sort(v.begin(), v.end()); n     42; n     ; n     for (long long i = a; i + t <= b + 1; i += t) { n       long long need = (q - g(i) + q * 2) % (q * 2); n       int ind = (lower_bound((v).begin(), (v).end(), (make_pair(need, -1))) - n                  (v).begin()); n       for (int j = ind - 3; j <= ind + 3; ++j) { n         int k = (j + v.size()) % v.size(); n         long long fn = f(i + v[k].second); n         if (fn < f(ans) || (fn == f(ans) && i + v[k].second < ans)) n           ans = i + v[k].second; n       } n       ind = (upper_bound((v).begin(), (v).end(), (make_pair(need, -1))) - n              (v).begin()); n       for (int j = ind - 3; j <= ind + 3; ++j) { n         int k = (j + v.size()) % v.size(); n         long long fn = f(i + v[k].second); n         if (fn < f(ans) || (fn == f(ans) && i + v[k].second < ans)) n           ans = i + v[k].second; n       } n     } n     for (long long i = b; i >= max(b - t - 5, a); --i) { n       long long fn = f(i); n       if (fn < f(ans) || (fn == f(ans) && i < ans)) ans = i; n     } n     cout << ans <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n #pragma GCC optimize( Ofast ) n using namespace std; n vector<vector<int>> adj; n vector<bool> vis; n vector<int> dep; n vector<int> sz; n vector<int> dif; n void dfsd(int v, int c) { n   vis[v] = true; n   c++; n   for (auto it = adj[v].begin(); it != adj[v].end(); ++it) { n     if (vis[*it] != true) { n       dfsd(*it, c); n     } n   } n   dep[v] = c; n } n void dfss(int v) { n   vis[v] = false; n   for (auto it = adj[v].begin(); it != adj[v].end(); ++it) { n     if (vis[*it] != false) { n       dfss(*it); n     } n   } n   for (auto it = adj[v].begin(); it != adj[v].end(); ++it) { n     sz[v] += sz[*it]; n   } n   sz[v]++; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   int n, i, k, p, q, j; n   long long cnt = 0; n   cin >> n >> k; n   adj.resize(n); n   vis.resize(n); n   dep.resize(n); n   sz.resize(n, 0); n   dif.resize(n); n   for (i = 0; i < n - 1; i++) { n     cin >> p >> q; n     adj[p - 1].push_back(q - 1); n     adj[q - 1].push_back(p - 1); n   } n   dfsd(0, 0); n   dfss(0); n   for (i = 0; i < n; i++) { n     dif[i] = sz[i] - dep[i]; n   } n   sort(dif.begin(), dif.end(), greater<int>()); n   for (i = 0; i < n - k; i++) { n     cnt += dif[i]; n   } n   cout << cnt; n } n


#include <bits/stdc++.h> n using namespace std; n long long n, sd, i, j, top, s, ans[100005], pos[100005], a[100005]; n bitset<100005> f[62], g[62]; n void read(long long &p) { n   p = 0; n   char x = getchar(); n   while (x <  0  || x >  9 ) x = getchar(); n   while (x >=  0  && x <=  9 ) { n     p = p * 10 + x -  0 ; n     x = getchar(); n   } n } n int main() { n   read(n); n   for (i = 1; i <= n; ++i) { n     read(a[i]); n     s ^= a[i]; n     for (j = 0; j <= 60; ++j) n       if ((a[i] >> j) & 1) f[j][i] = 1; n   } n   for (sd = 0; sd <= 1; ++sd) n     for (i = 60; i >= 0; --i) n       if (((s >> i) & 1) == sd) { n         top++; n         g[top] = f[i]; n         g[top][n + 1] = (!sd); n         for (j = 1; j <= top - 1; ++j) n           if (g[top][pos[j]]) g[top] ^= g[j]; n         pos[top] = n + 2; n         for (j = 1; j <= n; ++j) n           if (g[top][j]) { n             pos[top] = j; n             break; n           } n         if (pos[top] > n) { n           top--; n           continue; n         } n         for (j = 1; j <= top - 1; ++j) n           if (g[j][pos[top]]) g[j] ^= g[top]; n       } n   for (i = 1; i <= top; ++i) n     if (g[i][n + 1]) ans[pos[i]] = 1; n   for (i = 1; i <= n; ++i) { n     if (ans[i]) n       putchar( 1 ); n     else n       putchar( 2 ); n     if (i == n) n       putchar(  n ); n     else n       putchar(   ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; } n void solve() { n   long long p, q; n   cin >> p >> q; n   long long g = p; n   long long t = q; n   map<long long, long long> f, l; n   for (long long i = 2; i * i <= t; i++) { n     if (t % i == 0) { n       while (g % i == 0) { n         g /= i; n         f[i]++; n       } n       while (t % i == 0) { n         t /= i; n         l[i]++; n       } n     } n   } n   if (t != 1) { n     while (g % t == 0) { n       g /= t; n       f[t]++; n     } n     l[t]++; n     t = 1; n   } n   long long ans = g; n   for (auto x : l) { n     long long u = p; n     for (int i = 0; i < max(0ll, f[x.first] - x.second + 1); i++) u /= x.first; n     ans = max(ans, u); n   } n   cout << ans; n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   int q = 1; n   cin >> q; n   for (; q > 0; q--) { n     solve(); n     cout << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n template <typename Kausi> n void debug(vector<Kausi> v) { n   cerr <<  v = [  ; n   long long n = v.size(); n   for (int i = 0; i < n; i++) { n     cerr << v[i]; n     if (i < n - 1) cerr <<  ,  ; n   } n   cerr <<   ] length =   << v.size() <<   n ; n } n long long stringToLong(string s) { n   stringstream raaz(s); n   long long x = 0; n   raaz >> x; n   return x; n } n string longToString(long long k) { n   stringstream ss; n   ss << k; n   string s; n   ss >> s; n   return s; n } n bool isPowerOfTwo(int n) { n   if (n == 0) return false; n   return (ceil(log2(n)) == floor(log2(n))); n } n void scanArr(int *arr, int n) { n   for (int i = 0; i < n; i++) cin >> arr[i]; n } n void printArr(int *arr, int n) { n   for (int i = 0; i < n; i++) cout << arr[i] <<    ; n   cout <<   n ; n } n template <typename Kausi> n void scanVector(vector<Kausi> &v, int n) { n   for (int i = 0; i < n; i++) { n     Kausi temp; n     cin >> temp; n     v.push_back(temp); n   } n } n template <typename Kausi> n void printVector(vector<Kausi> v) { n   for (auto x : v) cout << x <<    ; n   cout <<   n ; n } n int main() { n   ios::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   int t; n   cin >> t; n   while (t--) { n     int n; n     cin >> n; n     vector<vector<char>> v(n, vector<char>(n,  = )); n     string s; n     cin >> s; n     for (int i = 0; i < n; i++) v[i][i] =  X ; n     bool ans = true; n     for (int i = 0; i < n; i++) { n       if (s[i] ==  2 ) { n         bool flg = true; n         for (int j = 0; j < n; j++) { n           if (j != i) { n             if (v[i][j] ==  =  and v[j][i] ==  =  and s[j] !=  1 ) { n               v[i][j] =  + ; n               v[j][i] =  - ; n               flg = false; n               break; n             } n           } n         } n         if (flg) { n           cout <<  NO  n                <<   n ; n           ans = false; n           break; n         } n       } n     } n     if (ans) { n       cout <<  YES  n            <<   n ; n       for (int i = 0; i < n; i++) { n         for (int j = 0; j < n; j++) cout << v[i][j]; n         cout <<   n ; n       } n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long d, k, a, b, t; n void solve() { n   cin >> d >> k >> a >> b >> t; n   if (d <= k) { n     cout << d * a; n     return; n   } n   long long ans = k * a + d * b - k * b; n   long long mult = t + k * a - k * b; n   if (mult >= 0) { n     cerr << 1 <<   n ; n     cout << ans; n   } else { n     mult = abs(mult); n     long long low = 0; n     long long high = d / k - 1; n     long long n = ans / mult; n     long long ans2 = n         min(d * b, d * a + (long long)ceil(((double)d) / k - 1) * t); n     if (n >= low && n <= high) { n       cout << min(ans2, ans - n * mult); n     } else if (n > high) { n       cout << min(ans2, ans - high * mult); n     } else n       cout << min(ans2, ans); n   } n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T> n void read(T &x) { n   x = 0; n   bool f = 0; n   char c = getchar(); n   for (; !isdigit(c); c = getchar()) n     if (c ==  - ) f = 1; n   for (; isdigit(c); c = getchar()) x = x * 10 + (c ^ 48); n   if (f) x = -x; n } n template <typename F> n inline void write(F x, char ed =   n ) { n   static short st[30]; n   short tp = 0; n   if (x < 0) putchar( - ), x = -x; n   do st[++tp] = x % 10, x /= 10; n   while (x); n   while (tp) putchar( 0  | st[tp--]); n   putchar(ed); n } n template <typename T> n inline void Mx(T &x, T y) { n   x < y && (x = y); n } n template <typename T> n inline void Mn(T &x, T y) { n   x > y && (x = y); n } n const long long inf = 1ll << 60; n const int N = 200050; n long long ans[N][3], q; n set<long long> s; n long long a[N], bit[N], n; n inline bool oper(long long x, long long y, bool op) { n   if (x + y >= inf) return 0; n   long long t = op ? (x ^ y) : (x + y); n   if (s.find(t) != s.end()) return 0; n   s.insert(t); n   a[++n] = t; n   ans[++q][0] = x, ans[q][1] = y, ans[q][2] = op; n   return 1; n } n bool chk(long long x) { n   for (int i = 60; i >= 0; i--) { n     if (x >> i & 1) { n       if (bit[i]) n         x ^= bit[i]; n       else n         return 1; n     } n   } n   return 0; n } n void insert(long long x) { n   for (int i = 60; i >= 0; i--) { n     if (x >> i & 1) { n       if (bit[i]) n         oper(bit[i], x, 1), x ^= bit[i]; n       else { n         bit[i] = x; n         break; n       } n     } n   } n } n int main() { n   srand((long long)new char); n   read(a[1]), s.insert(a[1]), insert(a[1]), n = 1; n   for (int i = 2; i <= 20; i++) n     if (oper(a[n], a[n], 0) && chk(a[n])) insert(a[n]); n   while (s.find(1) == s.end()) { n     int x = rand() % n + 1, y = rand() % n + 1; n     if (oper(a[x], a[y], 0) && chk(a[n])) insert(a[n]); n   } n   write(q); n   for (int i = 1; i <= q; i++) n     write(ans[i][0],    ), putchar(ans[i][2] ?  ^  :  + ), putchar(   ), n         write(ans[i][1]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n using ll = long long; n template <class t, class u> n void chmax(t& first, u second) { n   if (first < second) first = second; n } n template <class t, class u> n void chmin(t& first, u second) { n   if (second < first) first = second; n } n template <class t> n using vc = vector<t>; n template <class t> n using vvc = vc<vc<t>>; n using pi = pair<ll, ll>; n using vi = vc<ll>; n template <class t, class u> n ostream& operator<<(ostream& os, const pair<t, u>& p) { n   return os <<  {  << p.first <<  ,  << p.second <<  } ; n } n template <class t> n ostream& operator<<(ostream& os, const vc<t>& v) { n   os <<  { ; n   for (auto e : v) os << e <<  , ; n   return os <<  } ; n } n using uint = unsigned; n using ull = unsigned long long; n template <class t, size_t n> n ostream& operator<<(ostream& os, const array<t, n>& first) { n   return os << vc<t>(first.begin(), first.end()); n } n template <ll i, class T> n void print_tuple(ostream&, const T&) {} n template <ll i, class T, class H, class... Args> n void print_tuple(ostream& os, const T& t) { n   if (i) os <<  , ; n   os << get<i>(t); n   print_tuple<i + 1, T, Args...>(os, t); n } n template <class... Args> n ostream& operator<<(ostream& os, const tuple<Args...>& t) { n   os <<  { ; n   print_tuple<0, tuple<Args...>, Args...>(os, t); n   return os <<  } ; n } n template <class t> n void print(t x, ll suc = 1) { n   cout << x; n   if (suc == 1) cout <<   n ; n   if (suc == 2) cout <<    ; n } n ll read() { n   ll i; n   cin >> i; n   return i; n } n vi readvi(ll n, ll off = 0) { n   vi v(n); n   for (ll i = ll(0); i < ll(n); i++) v[i] = read() + off; n   return v; n } n pi readpi(ll off = 0) { n   ll first, second; n   cin >> first >> second; n   return pi(first + off, second + off); n } n template <class T> n void print(const vector<T>& v, ll suc = 1) { n   for (ll i = ll(0); i < ll(v.size()); i++) n     print(v[i], i == ll(v.size()) - 1 ? suc : 2); n } n string readString() { n   string s; n   cin >> s; n   return s; n } n template <class T> n T sq(const T& t) { n   return t * t; n } n void yes(bool ex = true) { n   cout <<  Yes  n        <<   n ; n   if (ex) exit(0); n } n void no(bool ex = true) { n   cout <<  No  n        <<   n ; n   if (ex) exit(0); n } n void possible(bool ex = true) { n   cout <<  Possible  n        <<   n ; n   if (ex) exit(0); n } n void impossible(bool ex = true) { n   cout <<  Impossible  n        <<   n ; n   if (ex) exit(0); n } n constexpr ll ten(ll n) { return n == 0 ? 1 : ten(n - 1) * 10; } n const ll infLL = LLONG_MAX / 3; n const ll inf = infLL; n ll topbit(signed t) { return t == 0 ? -1 : 31 - __builtin_clz(t); } n ll topbit(ll t) { return t == 0 ? -1 : 63 - __builtin_clzll(t); } n ll botbit(signed first) { return first == 0 ? 32 : __builtin_ctz(first); } n ll botbit(ll first) { return first == 0 ? 64 : __builtin_ctzll(first); } n ll popcount(signed t) { return __builtin_popcount(t); } n ll popcount(ll t) { return __builtin_popcountll(t); } n bool ispow2(ll i) { return i && (i & -i) == i; } n ll mask(ll i) { return (ll(1) << i) - 1; } n bool inc(ll first, ll second, ll c) { return first <= second && second <= c; } n template <class t> n void mkuni(vc<t>& v) { n   sort(v.begin(), v.end()); n   v.erase(unique(v.begin(), v.end()), v.end()); n } n ll rand_int(ll l, ll r) { n   static mt19937_64 gen(chrono::steady_clock::now().time_since_epoch().count()); n   return uniform_int_distribution<ll>(l, r)(gen); n } n template <class t> n void myshuffle(vc<t>& first) { n   for (ll i = ll(0); i < ll(ll(first.size())); i++) n     swap(first[i], first[rand_int(0, i)]); n } n template <class t> n ll lwb(const vc<t>& v, const t& first) { n   return lower_bound(v.begin(), v.end(), first) - v.begin(); n } n signed main() { n   cin.tie(0); n   ios::sync_with_stdio(0); n   cout << fixed << setprecision(20); n   ll n; n   cin >> n; n   ll k = n / 2; n   vi x(k), y(n - k); n   for (ll i = ll(0); i < ll(k); i++) x[i] = i + 1; n   for (ll i = ll(0); i < ll(n - k); i++) y[i] = k + i + 1; n   vvc<ll> ans; n   if (n % 2) { n     for (ll i = ll(0); i < ll(k); i++) n       for (ll j = ll(i + 1); j < ll(k); j++) n         for (ll _ = ll(0); _ < ll(2); _++) { n           ans.push_back({x[i], x[j], y[i], y[j]}); n         } n     for (ll i = ll(0); i < ll(k); i++) n       for (ll _ = ll(0); _ < ll(2); _++) ans.push_back({x[i], y[k], y[i]}); n   } else { n     for (ll i = ll(0); i < ll(k); i++) { n       for (ll j = ll(i + 2); j < ll(i == 0 ? k - 1 : k); j++) n         for (ll _ = ll(0); _ < ll(2); _++) { n           ans.push_back({x[i], x[j], y[i], y[j]}); n         } n       if (k >= 3) { n         if (i + 1 < k) { n           ll j = i + 1; n           ans.push_back({x[i], x[j], y[i], y[j]}); n         } n         if (i == 0) { n           ll j = k - 1; n           ans.push_back({x[i], x[j], y[i], y[j]}); n         } n       } n       { n         ans.push_back({x[i], x[(i + 1) % k], y[(i + 1) % k]}); n         ans.push_back({x[(i + 1) % k], y[i], y[(i + 1) % k]}); n       } n     } n   } n   print(ll(ans.size())); n   for (auto vs : ans) { n     print(ll(vs.size()), 2); n     print(vs); n   } n   vvc<ll> cnt(n, vi(n)); n   for (auto vs : ans) { n     for (ll i = ll(0); i < ll(ll(vs.size())); i++) { n       ll p = vs[i] - 1, q = vs[(i + 1) % ll(vs.size())] - 1; n       cnt[p][q]++; n       cnt[q][p]++; n     } n   } n   void(0); n   for (ll i = ll(0); i < ll(n); i++) n     for (ll j = ll(0); j < ll(n); j++) n       if (i != j) { n         assert(cnt[i][j] == 2); n       } n } n


#include <bits/stdc++.h> n #pragma GCC optimize( -Ofast ,  -funroll-all-loops ) n using namespace std; n const int N = 5e3 + 10; n int n, m, k, deg[N], dp[N][N], res, pre[N][N]; n int head[N], nex[N], to[N], w[N], tot; n inline void add(int a, int b, int c) { n   to[++tot] = b; n   nex[tot] = head[a]; n   w[tot] = c; n   head[a] = tot; n } n void Top() { n   queue<int> q; n   dp[1][1] = 0; n   for (int i = 1; i <= n; i++) n     if (!deg[i]) q.push(i); n   while (q.size()) { n     int u = q.front(); n     q.pop(); n     for (int i = head[u]; i; i = nex[i]) { n       if (--deg[to[i]] == 0) q.push(to[i]); n       for (int j = 1; j <= n; j++) n         if (dp[u][j] + w[i] < dp[to[i]][j + 1]) { n           dp[to[i]][j + 1] = dp[u][j] + w[i]; n           pre[to[i]][j + 1] = u; n         } n     } n   } n } n void dfs(int x, int y) { n   if (x && y) dfs(pre[x][y], y - 1); n   if (x) printf( %d  , x); n } n signed main() { n   cin >> n >> m >> k; n   memset(dp, 0x3f, sizeof dp); n   for (int i = 1, a, b, c; i <= m; i++) n     cin >> a >> b >> c, add(a, b, c), deg[b]++; n   Top(); n   for (int i = 1; i <= n; i++) n     if (dp[n][i] <= k) res = i; n   cout << res <<   n ; n   dfs(n, res); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n string s1[10], s2[10], name[10], score[10]; n map<string, int> mp; n int a[10][10]; n bool better(int x, int y) { n   int i; n   int px = 0, py = 0, mx = 0, sx = 0, sy = 0, my = 0; n   for (i = 0; i < 4; i++) { n     if (i != x) { n       if (a[x][i] > a[i][x]) n         sx += 3; n       else if (a[x][i] == a[i][x]) n         sx++; n       px += a[x][i]; n       mx += a[i][x]; n     } n   } n   for (i = 0; i < 4; i++) { n     if (i != y) { n       if (a[y][i] > a[i][y]) n         sy += 3; n       else if (a[y][i] == a[i][y]) n         sy++; n       my += a[i][y]; n       py += a[y][i]; n     } n   } n   if (sx != sy) return (sx > sy); n   if (px - mx != py - my) return (px - mx > py - my); n   if (px != py) return (px > py); n   return (x < y); n } n bool check(int x, int y) { n   int i, c; n   int b = mp[ BERLAND ]; n   for (i = 0; i < 4; i++) n     if (i != b && a[b][i] == -1) c = i; n   a[b][c] = x; n   a[c][b] = y; n   int rank = 1; n   for (i = 0; i < 4; i++) { n     if (i != b && better(i, b)) rank++; n   } n   a[b][c] = a[c][b] = -1; n   return (rank <= 2); n } n int main() { n   int d, i, j; n   for (i = 0; i < 5; i++) cin >> s1[i] >> s2[i] >> score[i]; n   for (i = 0; i < 5; i++) mp[s1[i]] = mp[s2[i]] = 0; n   i = 0; n   for (__typeof((mp).begin()) itr = (mp).begin(); itr != (mp).end(); itr++) { n     (itr->second) = i; n     name[i] = (itr->first); n     i++; n   } n   for (i = 0; i < 4; i++) n     for (j = 0; j < 4; j++) a[i][j] = -1; n   for (i = 0; i < 5; i++) { n     int x = mp[s1[i]], y = mp[s2[i]]; n     for (j = 0; j < score[i].length(); j++) n       if (score[i][j] ==  : ) score[i][j] =    ; n     istringstream ss(score[i]); n     ss >> a[x][y] >> a[y][x]; n   } n   for (d = 1; d < 100; d++) n     for (i = 0; i < 100; i++) { n       j = i + d; n       if (check(j, i)) { n         cout << j <<  :  << i << endl; n         return 0; n       } n     } n   cout <<  IMPOSSIBLE  << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long a, b, w, x, c; n   cin >> a >> b >> w >> x >> c; n   long long ans = 0; n   if (a < c) ans = ceil((double(a - c) * w + b) / (x - w)); n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n using ll = long long; n uint64_t getSeed() { n   auto ptr = new char; n   delete ptr; n   return (uint64_t)ptr + n          chrono::high_resolution_clock::now().time_since_epoch().count(); n } n mt19937 rnd(getSeed()); n ll rndVal(uint64_t a, uint64_t b) { n   return uniform_int_distribution<uint64_t>(a, b)(rnd); n } n constexpr uint64_t MOD = (1ull << 61) - 1; n inline uint64_t mod(uint64_t x) { n   x++; n   x = (x & MOD) + (x >> 61); n   x = (x & MOD) + (x >> 61); n   return x - 1; n } n uint64_t mulmod(const uint64_t a, const uint64_t b) { n   uint64_t ha = a >> 32, la = uint32_t(a), hb = b >> 32, lb = uint32_t(b); n   uint64_t h = ha * hb, m = ha * lb + la * hb, l = la * lb; n   return mod((l & MOD) + (l >> 61) + (m >> 29) + (m << 35 >> 3) + (h << 3)); n } n struct Hash { n   vector<uint64_t> h; n   vector<uint64_t> p; n   void build(const string& s, const uint64_t base) { n     int n = s.size(); n     h.resize(n + 1); n     p.resize(n + 1); n     h[0] = 1; n     p[0] = 1; n     for (int i = 0; i < n; i++) { n       h[i + 1] = mod(mulmod(h[i], base) + s[i] + 130); n       p[i + 1] = mulmod(p[i], base); n     } n   } n   void build(const string& s) { build(s, rndVal(258, MOD - 1)); } n   uint64_t query(int l, int r) { n     return mod(h[r + 1] + MOD - mulmod(h[l], p[r - l + 1])); n   } n }; n string s, t; n int main() { n   ios::sync_with_stdio(false); n   cin >> s >> t; n   int ns = s.size(), nt = t.size(); n   uint64_t base = rndVal(258, MOD - 1); n   Hash hash; n   hash.build(t, base); n   int rep = 0; n   for (int i = 0; i < nt - 1; i++) { n     if (hash.query(0, i) == hash.query(nt - i - 1, nt - 1)) rep = i + 1; n   } n   int has[2] = {0, 0}; n   for (auto c : s) has[c -  0 ]++; n   int needs[2] = {0, 0}; n   for (auto c : t) needs[c -  0 ]++; n   if (needs[0] > has[0] or needs[1] > has[1]) { n     cout << s << endl; n     return 0; n   } n   cout << t; n   has[0] -= needs[0]; n   has[1] -= needs[1]; n   for (int i = 0; i < rep; i++) needs[t[i] -  0 ]--; n   while (has[0] >= needs[0] and has[1] >= needs[1]) { n     for (int i = rep; i < nt; i++) { n       cout << t[i]; n       has[t[i] -  0 ]--; n     } n   } n   while (has[0]) { n     cout <<  0 ; n     has[0]--; n   } n   while (has[1]) { n     cout <<  1 ; n     has[1]--; n   } n   cout << endl; n } n


#include <bits/stdc++.h> n using namespace std; n template <class T> n inline T gcd(T a, T b) { n   if (a < 0) return gcd(-a, b); n   if (b < 0) return gcd(a, -b); n   return (b == 0) ? a : gcd(b, a % b); n } n const double EPS = 1e-10; n const double PI = acos(-1.0); n const int INF = 1 << 30; n const int mod = 1000000007; n const int maxm = 1 + 5; n const int maxn = 1000000 + 5; n char cat[4][100000]; n char s[4][100000]; n int n, k; n bool bj; n bool fit(char ch) { n   if (ch ==  a  || ch ==  e  || ch ==  i  || ch ==  u  || ch ==  o ) n     return true; n   return false; n } n void fun(int x) { n   int len = strlen(s[x]); n   int kk = 0, start = -1; n   for (int i = len - 1; i >= 0; i--) { n     if (fit(s[x][i])) kk++; n     if (kk == k) { n       start = i; n       break; n     } n   } n   if (kk != k) n     bj = false; n   else n     bj = true; n   for (int i = start; bj && i < len; i++) { n     cat[x][i - start] = s[x][i]; n   } n   if (bj) cat[x][len - start] =   0 ; n } n bool same(int i, int j) { n   if (strcmp(cat[i], cat[j]) == 0) return true; n   return false; n } n int cmp() { n   if (bj == false) return -1; n   if (same(0, 1) && same(1, 2) && same(2, 3)) return 0; n   if (same(0, 1) && same(2, 3)) return 1; n   if (same(0, 2) && same(1, 3)) return 2; n   if (same(0, 3) && same(1, 2)) return 3; n   return -1; n } n int main() { n   int ans = -2; n   scanf( %d %d , &n, &k); n   for (int i = (0), _b(n); i < _b; ++i) { n     for (int j = (0), _b(4); j < _b; ++j) { n       scanf( %s , s[j]); n       fun(j); n     } n     int p = cmp(); n     if (ans == -1) n       continue; n     else if (ans == -2) n       ans = p; n     else if (ans == 0) n       ans = p; n     else { n       if (p == 0) n         continue; n       else if (ans != p) n         ans = -1; n     } n   } n   if (ans == -1) n     cout <<  NO  << endl; n   else if (ans == 0) n     cout <<  aaaa  << endl; n   else if (ans == 1) n     cout <<  aabb  << endl; n   else if (ans == 2) n     cout <<  abab  << endl; n   else if (ans == 3) n     cout <<  abba  << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long int r, x1, y1, x2, y2; n   cin >> r >> x1 >> y1 >> x2 >> y2; n   long long int xd = x2 - x1, yd = y2 - y1; n   double dist = sqrt(xd * xd + yd * yd); n   long long int int_part = (int)dist; n   double frac_part = dist - int_part; n   long long int ans; n   ans = int_part / (2 * r); n   if (int_part % (2 * r) == 0 && frac_part == 0) ans--; n   ans++; n   cout << ans << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int toInt(string s) { n   int r = 0; n   istringstream sin(s); n   sin >> r; n   return r; n } n long long toInt64(string s) { n   long long r = 0; n   istringstream sin(s); n   sin >> r; n   return r; n } n double toDouble(string s) { n   double r = 0; n   istringstream sin(s); n   sin >> r; n   return r; n } n string toString(long long n) { n   string s, s1; n   while (n / 10 > 0) { n     s += (char)((n % 10) + 48); n     n /= 10; n   } n   s += (char)((n % 10) + 48); n   n /= 10; n   s1 = s; n   for (long long i = 0; i < s.length(); i++) s1[(s.length() - 1) - i] = s[i]; n   return s1; n } n bool isUpperCase(char c) { return c >=  A  && c <=  Z ; } n bool isLowerCase(char c) { return c >=  a  && c <=  z ; } n bool isLetter(char c) { return c >=  A  && c <=  Z  || c >=  a  && c <=  z ; } n bool isDigit(char c) { return c >=  0  && c <=  9 ; } n char toLowerCase(char c) { return (isUpperCase(c)) ? (c + 32) : c; } n char toUpperCase(char c) { return (isLowerCase(c)) ? (c - 32) : c; } n int main() { n   ios::sync_with_stdio(0); n   int n; n   cin >> n; n   long long count = 1; n   int a = 12; n   for (long long i = 0; i < n - 1; i++) { n     count += a; n     a += 12; n   } n   cout << count << endl; n } n


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


#include <bits/stdc++.h> n const int N = 100005; n long long a[N], s[N]; n long long max; n int main() { n   int n; n   scanf( %d , &n); n   for (int i = 1; i <= n; i++) { n     scanf( %I64d , &a[i]); n   } n   std::sort(a + 1, a + n + 1); n   for (int i = 1; i <= n; i++) { n     s[i] = s[i - 1] + a[i]; n     max += s[i]; n   } n   int m, k; n   scanf( %d , &m); n   for (int i = 0; i < m; i++) { n     scanf( %d , &k); n     if (i) putchar(   ); n     if (k == 1) n       printf( %I64d n , max - s[n]); n     else { n       long long ans = 0, t = n - 1, d = 1; n       while (t >= 0) { n         ans += s[t]; n         d *= k; n         t -= d; n       } n       printf( %I64d , ans); n     } n   } n   puts(  ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void solve() { n   long long int n, i; n   cin >> n; n   vector<long long int> a(n), mini(n); n   for (i = 0; i < (n); i++) cin >> a[i]; n   mini[n - 1] = a[n - 1]; n   for (i = n - 2; i >= 0; i--) { n     mini[i] = min(mini[i + 1], a[i]); n   } n   for (i = 0; i < n; i++) { n     long long int l = i, r = n - 1; n     long long int ans = -1; n     while (l <= r) { n       long long int m = l + (r - l) / 2; n       if (mini[m] < a[i]) { n         l = m + 1; n         ans = m; n       } else n         r = m - 1; n     } n     if (ans == -1) n       cout << -1 <<    ; n     else n       cout << ans - i - 1 <<    ; n   } n   cout <<   n ; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, d; n int a[105]; n int main() { n   ios_base::sync_with_stdio(false); n   cin >> n >> d; n   for (int i = 0; i < n; i++) cin >> a[i]; n   int dp[n][n]; n   sort(a, a + n); n   for (int i = 0; i < n; i++) { n     for (int j = i; j < n; j++) dp[i][j] = a[j] - a[i]; n   } n   int ans = 0; n   for (int i = 0; i < n; i++) { n     for (int j = i; j < n; j++) { n       if (dp[i][j] <= d) ans = max(ans, j - i + 1); n     } n   } n   cout << n - ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int maps[1010][1010]; n int n; n int dir[8][2] = {{0, 1},  {1, 0}, {0, -1}, {-1, 0}, n                  {-1, 1}, {1, 1}, {1, -1}, {-1, -1}}; n int pre[8][1010][1010]; n double sums[8][1010][1010]; n void partial() { n   for (int i = 0; i < 8; i++) { n     for (int j = 0; j < n; j++) { n       for (int k = 0; k < n; k++) { n         if (j - dir[i][0] < 0 || j - dir[i][0] >= n || k - dir[i][1] < 0 || n             k - dir[i][1] >= n) n           for (int x = j, y = k; x >= 0 && x < n && y >= 0 && y < n; n                x += dir[i][0], y += dir[i][1]) { n             if (maps[x][y] == 0) { n               pre[i][x][y] = 0; n               sums[i][x][y] = 0; n             } else { n               if (x == j && y == k) { n                 pre[i][x][y] = 1; n                 sums[i][x][y] = log(maps[x][y]); n               } else { n                 pre[i][x][y] = pre[i][x - dir[i][0]][y - dir[i][1]] + 1; n                 sums[i][x][y] = n                     sums[i][x - dir[i][0]][y - dir[i][1]] + log(maps[x][y]); n               } n             } n           } n       } n     } n   } n } n double maxCross(int x, int y) { n   int minN = 1e9, minR = 1e9; n   double rot = log(maps[x][y]), norm = log(maps[x][y]); n   for (int i = 0; i < 4; i++) { n     minN = min(minN, pre[i][x][y]); n   } n   for (int i = 4; i < 8; i++) { n     minR = min(minR, pre[i][x][y]); n   } n   minN--; n   minR--; n   for (int i = 0; i < 4; i++) { n     int sx = x + (dir[i][0] * minN); n     int sy = y + (dir[i][1] * minN); n     norm += sums[i][sx][sy] - sums[i][x][y]; n   } n   for (int i = 4; i < 8; i++) { n     int sx = x + (dir[i][0] * minR); n     int sy = y + (dir[i][1] * minR); n     rot += sums[i][sx][sy] - sums[i][x][y]; n   } n   return max(norm, rot); n } n bool isNorm(int x, int y) { n   int minN = 1e9, minR = 1e9; n   double rot = log(maps[x][y]), norm = log(maps[x][y]); n   for (int i = 0; i < 4; i++) { n     minN = min(minN, pre[i][x][y]); n   } n   for (int i = 4; i < 8; i++) { n     minR = min(minR, pre[i][x][y]); n   } n   minN--; n   minR--; n   for (int i = 0; i < 4; i++) { n     int sx = x + (dir[i][0] * minN); n     int sy = y + (dir[i][1] * minN); n     norm += sums[i][sx][sy] - sums[i][x][y]; n   } n   for (int i = 4; i < 8; i++) { n     int sx = x + (dir[i][0] * minR); n     int sy = y + (dir[i][1] * minR); n     rot += sums[i][sx][sy] - sums[i][x][y]; n   } n   return norm > rot; n } n long long calcMax(int x, int y) { n   int minN = 1e9, minR = 1e9; n   long long rot = maps[x][y], norm = maps[x][y]; n   for (int i = 0; i < 4; i++) { n     minN = min(minN, pre[i][x][y]); n   } n   for (int i = 4; i < 8; i++) { n     minR = min(minR, pre[i][x][y]); n   } n   minN--; n   minR--; n   for (int i = 0; i < 4; i++) { n     int sx = x; n     int sy = y; n     for (int j = 0; j < minN; j++) { n       sx += dir[i][0]; n       sy += dir[i][1]; n       norm *= maps[sx][sy]; n       norm %= (long long)1e9 + 7; n     } n   } n   for (int i = 4; i < 8; i++) { n     int sx = x; n     int sy = y; n     for (int j = 0; j < minR; j++) { n       sx += dir[i][0]; n       sy += dir[i][1]; n       rot *= maps[sx][sy]; n       rot %= (long long)1e9 + 7; n     } n   } n   return (isNorm(x, y) ? norm : rot); n } n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   ; n   cin >> n; n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < n; j++) { n       char ch; n       cin >> ch; n       maps[i][j] = ch -  0 ; n     } n   } n   partial(); n   int maxX = 0, maxY = 0; n   double maxVal = -1; n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < n; j++) { n       if (maps[i][j] != 0) { n         double temp = maxCross(i, j); n         if (temp > maxVal) { n           maxVal = temp; n           maxX = i; n           maxY = j; n         } n       } n     } n   } n   cout << calcMax(maxX, maxY) << endl; n } n


#include <bits/stdc++.h> n using namespace std; n using ll = long long; n using pii = pair<int, ll>; n int n, m; n vector<pii> coupons; n bool comp(pii a, pii b) { return a.second > b.second; } n ll getlen(ll x) { n   if (x % 2 == 1) return x * (x - 1) / 2 + 1; n   return x * (x - 1) / 2 + x / 2; n } n int main() { n   cin >> n >> m; n   ll low = 0, high = n; n   while (low < high) { n     ll mid = (low + high + 1) / 2; n     if (getlen(mid) <= n) { n       low = mid; n     } else { n       high = mid - 1; n     } n   } n   for (int i = 0; i < m; i++) { n     ll q, w; n     cin >> q >> w; n     coupons.push_back({q, w}); n   } n   sort(coupons.begin(), coupons.end(), comp); n   ll ans = 0; n   for (int i = 0; i < low && i < m; i++) { n     ans += coupons[i].second; n   } n   cout << ans << endl; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e5 + 5; n const int MOD = 1e9 + 7; n string ans = n      What are you doing at the end of the world? Are you busy? Will you save   n      us? ; n string a =  What are you doing while sending   ; n string b =   ? Are you busy? Will you send   ; n string c =   ? ; n vector<long long int> dp(N, 1e18 + 1); n char get(long long int n, long long int k) { n   if (k >= dp[n]) return  . ; n   if (n == 0) return ans[k]; n   if (k < a.size()) return a[k]; n   if (k < a.size() + dp[n - 1]) return get(n - 1, k - a.size()); n   if (k < a.size() + dp[n - 1] + b.size()) return b[k - a.size() - dp[n - 1]]; n   if (k < a.size() + dp[n - 1] + b.size() + dp[n - 1]) n     return get(n - 1, k - a.size() - dp[n - 1] - b.size()); n   if (k < a.size() + dp[n - 1] + b.size() + dp[n - 1] + c.size()) n     return c[k - a.size() - dp[n - 1] - b.size() - dp[n - 1]]; n   return  . ; n } n void solve() { n   dp[0] = ans.size(); n   for (int i = 1; i < 60; ++i) { n     long long int k = (2 * dp[i - 1]) + a.size() + b.size() + c.size(); n     dp[i] = min(k, (long long int)1e18 + 1); n   } n   int q; n   cin >> q; n   while (q--) { n     long long int n, k; n     cin >> n >> k; n     cout << get(n, k - 1); n   } n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(NULL); n   cout.tie(NULL); n   int t = 1; n   while (t--) solve(); 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   long long int n, m; n   cin >> n >> m; n   if (m > n) n     cout << -1; n   else if (m == n) n     cout << n; n   else { n     long long int x, y; n     for (y = (n / 2); y >= 0; y--) { n       if (n - y >= 0) { n         x = n - 2 * y; n         if ((x + y) % m == 0) { n           cout << x + y; n           break; n         } n       } n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 55; n int n, m; n int sz, inp; n long long ans[N]; n long long sum[N]; n long long suf[N]; n long long pre[N]; n int main() { n   scanf( %d %d , &n, &m); n   for (int i = 1; i <= n; ++i) { n     scanf( %d , &sz); n     ans[i] = pre[i] = -1e9; n     while (sz--) { n       scanf( %d , &inp); n       sum[i] += inp; n       suf[i] = max(0LL, suf[i]) + inp; n       ans[i] = max(ans[i], suf[i]); n       pre[i] = max(pre[i], sum[i]); n     } n   } n   ans[0] = -1e15; n   while (m--) { n     scanf( %d , &inp); n     ans[0] = max(ans[0], max(0LL, ans[n + 1]) + pre[inp]); n     ans[n + 1] = max(suf[inp], sum[inp] + max(0LL, ans[n + 1])); n     ans[0] = max(ans[0], max(ans[inp], ans[n + 1])); n   } n   printf( %lld n , ans[0]); n } n


#include <bits/stdc++.h> n using namespace std; n const int INF = 0x3f3f3f3f; n const double eps = 1e-8; n const int mod = 1e9 + 7; n const int P = 1e9 + 7; n const int N = 2e7; n const int maxn = 1e6 + 5; n long long t, a, b, x, y, n, minn; n int main() { n   ios::sync_with_stdio(false); n   cin >> t; n   while (t--) { n     cin >> a >> b >> x >> y >> n; n     long long temp1 = max(x, a - n); n     long long temp2 = max(y, b - n); n     if (temp1 >= temp2) { n       swap(a, b); n       swap(x, y); n     } n     minn = min(n, a - x); n     n -= minn; n     a -= minn; n     minn = min(n, b - y); n     b -= minn; n     cout << a * b << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n unsigned long long f(unsigned long long x, int b) { n   for (int i = 0; i < 64; i++) n     if (__builtin_popcountll(x) < b) x |= 1ULL << i; n   return x; n } n unsigned long long brute(unsigned long long l, unsigned long long r) { n   unsigned long long ret = l; n   for (unsigned long long i = l; i <= r; i++) n     if (__builtin_popcountll(i) > __builtin_popcountll(ret)) ret = i; n   return ret; n } n int main() { n   int T; n   scanf( %d , &T); n   unsigned long long a, b; n   while (T--) { n     scanf( %llu%llu , &a, &b); n     unsigned long long ans = a; n     for (int i = __builtin_popcountll(a) + 1; i < 64; i++) { n       unsigned long long v = f(a, i); n       if (v <= b) ans = v; n     } n     printf( %lld n , ans); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int up[200005]; n void init() { memset(up, -1, sizeof(up)); } n int Find(int x) { n   if (up[x] < 0) return x; n   int root = Find(up[x]); n   up[x] = root; n   return root; n } n void Union(int x, int y) { n   x = Find(x); n   y = Find(y); n   if (x == y) return; n   if (up[x] < up[y]) { n     up[x] += up[y]; n     up[y] = x; n   } else { n     up[y] += up[x]; n     up[x] = y; n   } n } n int n; n int a[200005]; n vector<int> can; n set<int> aree; n signed main() { n   init(); n   int x; n   scanf( %d , &n); n   int keep = -1; n   for (int i = 1; i <= n; i++) { n     scanf( %d , &x); n     if (Find(x) != Find(i)) { n       Union(x, i); n       a[i] = x; n     } else { n       if (x == i && keep == -1) { n         keep = 0; n         a[i] = x; n       } else { n         a[i] = -1; n         can.push_back(i); n       } n     } n   } n   if (keep == -1) { n     a[*(can.end() - 1)] = *(can.end() - 1); n     can.pop_back(); n   } n   for (int i = 1; i <= n; i++) { n     aree.insert(Find(i)); n   } n   for (auto i : can) { n     for (set<int>::iterator k = aree.begin(); k != aree.end(); k++) { n       int j = *k; n       int u, v; n       u = Find(j); n       v = Find(i); n       if (Find(j) != Find(i)) { n         a[i] = j; n         Union(i, j); n         aree.erase(v); n         aree.erase(u); n         aree.insert(Find(j)); n         goto lol; n       } n     } n   lol:; n   } n   cout << can.size() - keep <<   n ; n   for (int i = 1; i <= n; i++) { n     cout << a[i] <<    ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   cin >> n; n   vector<int> a(n); n   map<long long, vector<pair<int, int>>> d; n   for (int i = 0; i < n; i++) cin >> a[i]; n   for (int i = 0; i < n; i++) { n     long long sum = 0; n     for (int j = i; j < n; j++) { n       sum += a[j]; n       d[sum].push_back({i, j}); n     } n   } n   pair<int, vector<pair<int, int>>> opt = {-INT_MAX, {}}; n   for (auto it : d) { n     sort(it.second.begin(), it.second.end(), n          [](pair<int, int> p1, pair<int, int> p2) { n            return p1.second < p2.second; n          }); n     pair<int, int> prev = it.second[0]; n     pair<int, vector<pair<int, int>>> cur = {1, {prev}}; n     for (int i = 1; i < it.second.size(); i++) { n       if (it.second[i].first > prev.second) { n         cur.second.push_back(it.second[i]); n         cur.first++; n         prev = it.second[i]; n       } n     } n     opt = max(opt, cur); n   } n   cout << opt.first << endl; n   for (int i = 0; i < opt.first; i++) { n     cout << opt.second[i].first + 1 <<     << opt.second[i].second + 1 << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n template <class T> n void pv(T a, T b) { n   for (T i = a; i != b; ++i) cout << *i <<    ; n   cout << endl; n } n template <class T> n void chmin(T &t, T f) { n   if (t > f) t = f; n } n template <class T> n void chmax(T &t, T f) { n   if (t < f) t = f; n } n int in() { n   int x; n   scanf( %d , &x); n   return x; n } n const long long MO = 1000000007; n const int LIM = 1000005; n long long inv[1000010]; n long long fac[1000010], fnv[1000010]; n void prepare() { n   int i; n   inv[1] = 1; n   for (i = 2; i < LIM; ++i) { n     inv[i] = MO - MO / i * inv[MO % i] % MO; n     assert(i * inv[i] % MO == 1); n   } n   fac[0] = 1; n   fnv[0] = 1; n   for (i = 1; i < LIM; ++i) { n     fac[i] = fac[i - 1] * i % MO; n     fnv[i] = fnv[i - 1] * inv[i] % MO; n   } n   assert(3628800 * fnv[10] % MO == 1); n } n int A, B, N; n bool isGood(int x) { n   for (; x; x /= 10) { n     if (!(x % 10 == A || x % 10 == B)) { n       return 0; n     } n   } n   return 1; n } n int main() { n   int i; n   prepare(); n   for (; ~scanf( %d%d%d , &A, &B, &N);) { n     long long ans = 0; n     for (i = 0; i <= N; ++i) { n       if (isGood(A * i + B * (N - i))) { n         long long tmp = 1; n         (tmp *= fac[N]) %= MO; n         (tmp *= fnv[i]) %= MO; n         (tmp *= fnv[N - i]) %= MO; n         (ans += tmp) %= MO; n       } n     } n     ans = (ans % MO + MO) % MO; n     cout << ans << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long int mxN = 1e5 + 5; n const long long int MOD = 1e9 + 7; n long long int one = 0; n long long int ceil1(long long int a, long long int b) { n   if (a % b == 0) n     return (a / b); n   else n     return ((a / b) + 1); n } n void solve() { n   long long int n; n   cin >> n; n   string s; n   cin >> s; n   long long int x = 0, y = 0; n   for (long long int i = 0; i < n; i++) { n     if (i & 1) { n       if (s[i] !=  r ) x++; n     } else { n       if (s[i] !=  b ) y++; n     } n   } n   long long int ans = INT_MAX; n   ans = min(ans, max(x, y)); n   x = 0, y = 0; n   for (long long int i = 0; i < n; i++) { n     if (i & 1) { n       if (s[i] !=  b ) x++; n     } else { n       if (s[i] !=  r ) y++; n     } n   } n   ans = min(ans, max(x, y)); n   cout << ans <<   n ; n } n signed main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   long long int t = 1; n   while (t--) { n     one++; n     solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 100005; n int n, o; n string st; n long long a[maxn], all; n int cc[100], where[100], zhu[100], g[maxn], wc[maxn]; n int f[66][maxn]; n int main() { n   cin >> n; n   all = 0; n   for (int i = 1; i <= n; i++) { n     cin >> a[i]; n     all ^= a[i]; n   } n   st =   ; n   for (int i = 1; i <= 63; i++) { n     st = char(all % 2 + 48) + st; n     all /= 2; n   } n   o = 0; n   for (int i = 1; i <= 63; i++) n     if (st[i - 1] ==  0 ) ++o, where[o] = i; n   for (int i = 1; i <= 63; i++) n     if (st[i - 1] ==  1 ) ++o, where[o] = i; n   for (int i = 1; i <= n; i++) { n     for (int j = 1; j <= 63; j++) { n       cc[64 - j] = a[i] % 2; n       a[i] /= 2; n     } n     a[i] = 0; n     for (int j = 1; j <= 63; j++) a[i] = (long long)a[i] * 2 + cc[where[j]]; n   } n   memset(f, 0, sizeof(f)); n   memset(g, 0, sizeof(g)); n   memset(zhu, 0, sizeof(zhu)); n   for (int i = 1; i <= 63; i++) { n     for (int j = 1; j <= n; j++) f[i][j] = ((long long)a[j] >> (63 - i)) % 2; n     g[i] = 1; n     for (int j = 1; j < i; j++) n       if ((zhu[j] != -1) && (f[i][zhu[j]] == 1)) { n         for (int k = 1; k <= n; k++) f[i][k] ^= f[j][k]; n         g[i] ^= g[j]; n       } n     zhu[i] = -1; n     for (int j = 1; j <= n; j++) n       if (f[i][j] == 1) { n         zhu[i] = j; n         break; n       } n     if (zhu[i] == -1) g[i] = 0; n   } n   memset(wc, 0, sizeof(wc)); n   for (int i = 63; i >= 1; i--) { n     for (int j = 1; j <= n; j++) n       if (f[i][j] == 1) g[i] ^= wc[j]; n     wc[zhu[i]] = g[i]; n   } n   for (int i = 1; i <= n; i++) cout << wc[i] + 1 <<    ; n   cout << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int N, Q; n vector<vector<int>> V; n map<pair<int, int>, int> M; n int ret[101010]; n template <int um> n class UF { n  public: n   vector<int> par, rank; n   vector<vector<int>> hist; n   UF() { n     par = rank = vector<int>(um, 0); n     for (int i = 0; i < um; i++) par[i] = i; n   } n   void reinit() { n     int i; n     for (i = 0; i < (um); i++) rank[i] = 0, par[i] = i; n   } n   int operator[](int x) { return (par[x] == x) ? (x) : operator[](par[x]); } n   void pop() { n     if (hist.size()) { n       auto a = hist.back(); n       hist.pop_back(); n       par[a[0]] = a[1]; n       rank[a[0]] = a[2]; n       par[a[3]] = a[4]; n       rank[a[3]] = a[5]; n     } n   } n   void operator()(int x, int y) { n     x = operator[](x); n     y = operator[](y); n     hist.push_back({x, par[x], rank[x], y, par[y], rank[y]}); n     if (x == y) return; n     if (rank[x] < rank[y]) { n       par[x] = y; n     } else if (rank[x] > rank[y]) { n       par[y] = x; n     } else { n       rank[x]++; n       par[y] = x; n     } n   } n }; n void dfs(int L, int R, UF<201010>& uf, vector<vector<int>> V) { n   vector<vector<int>> LV, RV; n   int nums = 0; n   int M = (L + R) / 2; n   int con = 0; n   if (V.empty()) return; n   for (auto& v : V) { n     if (v[2] <= L && R <= v[3]) { n       uf(v[0] * 2, v[1] * 2 + 1); n       uf(v[0] * 2 + 1, v[1] * 2); n       if (uf[v[0] * 2] == uf[v[0] * 2 + 1]) con = 1; n       if (uf[v[1] * 2] == uf[v[1] * 2 + 1]) con = 1; n       nums += 2; n     } else { n       if (v[2] < M) LV.push_back({v[0], v[1], max(L, v[2]), min(M, v[3])}); n       if (v[3] > M) RV.push_back({v[0], v[1], max(M, v[2]), min(R, v[3])}); n     } n   } n   int i; n   if (con) { n     for (int i = L; i < R; i++) ret[i] = 1; n   } else { n     dfs(L, M, uf, LV); n     dfs(M, R, uf, RV); n   } n   while (nums--) uf.pop(); n } n void solve() { n   int i, j, k, l, r, x, y; n   string s; n   cin >> N >> Q; n   for (i = 0; i < (Q); i++) { n     cin >> x >> y; n     x--, y--; n     if (x > y) swap(x, y); n     if (M.count({x, y}) == 0) { n       M[{x, y}] = i; n     } else { n       V.push_back({x, y, M[{x, y}], i}); n       M.erase({x, y}); n     } n   } n   for (auto& r : M) V.push_back({r.first.first, r.first.second, r.second, Q}); n   UF<201010> uf; n   dfs(0, Q, uf, V); n   for (i = 0; i < (Q); i++) { n     if (ret[i]) n       cout <<  NO  << endl; n     else n       cout <<  YES  << endl; n   } n } n int main(int argc, char** argv) { n   string s; n   int i; n   if (argc == 1) ios::sync_with_stdio(false), cin.tie(0); n   for (i = 0; i < (argc - 1); i++) s += argv[i + 1], s +=   n ; n   for (i = 0; i < (s.size()); i++) ungetc(s[s.size() - 1 - i], stdin); n   solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   string s, ss; n   getline(cin, s); n   getline(cin, ss); n   int ara[500]; n   memset(ara, 0, sizeof ara); n   for (auto x : s) n     if (isalpha(x)) ara[x]++; n   bool ok = true; n   for (auto x : ss) { n     if (isalpha(x)) { n       if (!ara[x]) { n         ok = false; n         break; n       } else n         ara[x]--; n     } n   } n   if (ok) n     printf( YES n ); n   else n     printf( NO n ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long mod = 1e9 + 7; n const int N = 1e5 + 100; n const int maxn = 1e6; n const int MAXL = sqrt(1e7) + 1; n int n, a[220], f[220][410]; n void solve() { n   scanf( %d , &n); n   for (int i = 1; i <= n; i++) scanf( %d , &a[i]); n   sort(a + 1, a + n + 1); n   for (int i = 1; i <= n; i++) { n     for (int j = 0; j <= 400; j++) f[i][j] = INT_MAX; n   } n   for (int j = 1; j <= 400; j++) { n     f[1][j] = abs(j - a[1]); n     f[1][j] = min(f[1][j], f[1][j - 1]); n   } n   for (int i = 2; i <= n; i++) { n     for (int j = i; j <= 400; j++) { n       f[i][j] = min(f[i][j], f[i - 1][j - 1] + abs(j - a[i])); n       f[i][j] = min(f[i][j], f[i][j - 1]); n     } n   } n   int ans = INT_MAX; n   for (int i = n; i <= 400; i++) ans = min(ans, f[n][i]); n   printf( %d n , ans); n } n int main() { n   int T; n   scanf( %d , &T); n   int tt = 0; n   while (T--) { n     solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int cal(vector<int> q, int n, int m) { n   int sum = 0; n   for (int i = (int)0; i < (int)n; ++i) { n     for (int j = (int)i; j < (int)n; ++j) { n       int mn = INT_MAX; n       for (int k = (int)i; k < (int)j + 1; ++k) n         mn = ((mn) < (q[k]) ? (mn) : (q[k])); n       sum += mn; n     } n   } n   return sum; n } n int main() { n   int i, j, n, t; n   int m; n   scanf( %d , &n); n   scanf( %d , &m); n   vector<int> q; n   for (int i = (int)1; i < (int)n + 1; ++i) q.push_back(i); n   int mx = 0; n   do { n     mx = ((mx) > (cal(q, n, m)) ? (mx) : (cal(q, n, m))); n   } while (next_permutation(q.begin(), q.end())); n   sort(q.begin(), q.end()); n   int cnt = 0; n   do { n     if (cal(q, n, m) == mx) cnt++; n     if (cnt == m) break; n   } while (next_permutation(q.begin(), q.end())); n   for (int i = (int)0; i < (int)n; ++i) printf( %d  , q[i]); n   printf(  n ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 100100; n struct node { n   long long l, r; n   long long color; n   long long sum; n   long long change; n } tree[maxn << 2]; n long long n, m; n int a[maxn]; n void pushup(long long cur) { n   if (tree[cur * 2].color == tree[cur * 2 + 1].color) n     tree[cur].color = tree[cur * 2].color; n   tree[cur].sum = tree[cur * 2].sum + tree[cur * 2 + 1].sum; n } n void build(long long l, long long r, long long cur) { n   tree[cur].l = l; n   tree[cur].r = r; n   tree[cur].sum = 0; n   tree[cur].change = 0; n   tree[cur].color = 0; n   if (l == r) { n     tree[cur].color = l; n     return; n   } n   long long mid = (r + l) >> 1; n   build(l, mid, cur << 1); n   build(mid + 1, r, cur << 1 | 1); n   pushup(cur); n } n void pushdown(long long cur) { n   if (tree[cur].change) { n     tree[cur * 2].sum += n         tree[cur].change * (tree[cur * 2].r - tree[cur * 2].l + 1); n     tree[cur * 2 + 1].sum += n         tree[cur].change * (tree[cur * 2 + 1].r - tree[cur * 2 + 1].l + 1); n     tree[cur * 2].change += tree[cur].change; n     tree[cur * 2 + 1].change += tree[cur].change; n     tree[cur].change = 0; n   } n   if (tree[cur].color) n     tree[cur * 2].color = tree[cur * 2 + 1].color = tree[cur].color; n   tree[cur].color = 0; n } n void update(long long pl, long long pr, long long l, long long r, long long cur, n             long long x) { n   if ((pl <= l && r <= pr && tree[cur].color) || l == r) { n     tree[cur].sum += abs(x - tree[cur].color) * (r - l + 1); n     tree[cur].change += abs(x - tree[cur].color); n     tree[cur].color = x; n     return; n   } n   pushdown(cur); n   long long mid = (r + l) >> 1; n   if (pl <= mid) update(pl, pr, l, mid, cur << 1, x); n   if (pr > mid) update(pl, pr, mid + 1, r, cur << 1 | 1, x); n   pushup(cur); n } n long long query(long long pl, long long pr, long long l, long long r, n                 long long cur) { n   if (pl <= l && r <= pr) { n     return tree[cur].sum; n   } n   pushdown(cur); n   long long mid = (r + l) >> 1; n   long long res = 0; n   if (pl <= mid) res += query(pl, pr, l, mid, cur << 1); n   if (pr > mid) res += query(pl, pr, mid + 1, r, cur << 1 | 1); n   return res; n } n int main() { n   while (~scanf( %lld%lld , &n, &m)) { n     build(1, n, 1); n     long long op, l, r, x; n     while (m--) { n       scanf( %lld%lld%lld , &op, &l, &r); n       if (op == 1) { n         scanf( %lld , &x); n         update(l, r, 1, n, 1, x); n       } else { n         printf( %lld n , query(l, r, 1, n, 1)); n       } n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n = 5000001; n void okms() { n   vector<int> kk(n); n   kk[0] = 0, kk[1] = 0; n   for (int(i) = 2; i < n; ++i) kk[i] = 1; n   vector<bool> ch(n); n   for (int(i) = 0; i < n; ++i) ch[i] = true; n   ch[0] = 0, ch[1] = 0; n   vector<int> trr(n); n   for (int i = 2; i * i <= n - 1; i++) { n     if (ch[i]) { n       trr[i] = i; n       for (int j = i * i; j <= n - 1; j += i) { n         ch[j] = false; n         trr[j] = i; n       } n     } n   } n   for (int(i) = 2; i < n; ++i) { n     if (!ch[i]) { n       kk[i] = kk[trr[i]] + kk[i / trr[i]]; n     } n   } n   vector<int> ans(n); n   for (int(i) = 1; i < n; ++i) ans[i] += ans[i - 1] + kk[i]; n   int t; n   cin >> t; n   while (t--) { n     int a, b; n     cin >> a >> b; n     cout << ans[a] - ans[b] <<   n ; n   } n   return; n } n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   int test; n   test = 1; n   while (test--) { n     okms(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long n, m, q; n long long siz[100005]; n long long par[100005]; n string s; n unordered_map<string, long long> idx; n vector<pair<long long, long long> > e[100005]; n long long ans[100005]; n long long dis[100005]; n pair<long long, pair<long long, long long> > edges[100005]; n long long find_set(long long a) { n   if (a == par[a]) return a; n   return par[a] = find_set(par[a]); n } n void union_set(long long a, long long b) { n   a = find_set(a); n   b = find_set(b); n   if (a != b) { n     if (siz[a] < siz[b]) swap(a, b); n     par[b] = a; n     siz[a] += siz[b]; n   } n } n void dfs(long long u, long long len, long long par) { n   dis[u] = len; n   for (auto i : e[u]) { n     if (i.first != par) dfs(i.first, len + i.second, u); n   } n } n signed main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   cin >> n >> m >> q; n   for (long long i = 0; i < n; i++) { n     cin >> s; n     idx[s] = i; n     par[i] = i; n     siz[i] = 1; n   } n   for (long long i = 0; i < m; i++) { n     long long type; n     string a, b; n     cin >> type >> a >> b; n     long long u = idx[a]; n     long long v = idx[b]; n     edges[i] = {type, {u, v}}; n     long long uRep = find_set(u); n     long long vRep = find_set(v); n     if (uRep == vRep) continue; n     union_set(uRep, vRep); n     if (type == 1) n       e[u].push_back({v, 0}), e[v].push_back({u, 0}); n     else n       e[u].push_back({v, 1}), e[v].push_back({u, 1}); n     ans[i] = 1; n   } n   memset(dis, -1, sizeof(dis)); n   for (long long i = 0; i < n; i++) { n     if (dis[i] == -1) { n       long long iRep = find_set(i); n       dfs(iRep, 0, -1); n     } n   } n   for (long long i = 0; i < m; i++) { n     if (ans[i]) n       cout <<  YES n ; n     else { n       long long u = edges[i].second.first; n       long long v = edges[i].second.second; n       long long type = edges[i].first; n       long long real_type; n       if (dis[u] % 2 == dis[v] % 2) n         real_type = 1; n       else n         real_type = 2; n       if (real_type == type) n         cout <<  YES n ; n       else n         cout <<  NO n ; n     } n   } n   while (q--) { n     string a, b; n     cin >> a >> b; n     long long u = idx[a]; n     long long v = idx[b]; n     long long uRep = find_set(u); n     long long vRep = find_set(v); n     if (uRep != vRep) n       cout << 3 << endl; n     else { n       if (dis[u] % 2 == dis[v] % 2) n         cout << 1 << endl; n       else n         cout << 2 << endl; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, now, p[20000], p_, g[10], ans; n int xx[10]; n int tmp[10000]; n int yoo[2][10000][10], f[1000][1000]; n void dfs(int k) { n   int tmp = 0; n   for (int i = 1; i < k; ++i) tmp = tmp * 10 + xx[i] / g[k + 1] % 10; n   if (k == n) { n     ans += ::tmp[tmp]; n     return; n   } n   for (int v = yoo[0][tmp][n - k + 1]; v <= yoo[1][tmp][n - k + 1]; v++) { n     xx[k] = p[v]; n     dfs(k + 1); n   } n } n int dfss(int k) { n   int tmp = 0; n   for (int i = 1; i < k; ++i) tmp = tmp * 10 + xx[i] / g[k + 1] % 10; n   if (k == n - 1) { n     int ppp = 0; n     for (int i = 1; i < k; ++i) ppp = ppp * 10 + xx[i] / g[k + 2] % 10; n     if (f[tmp][ppp] != -1) { n       ans += f[tmp][ppp]; n       return 0; n     } n     f[tmp][ppp] = 0; n     for (int v = yoo[0][tmp][n - k + 1]; v <= yoo[1][tmp][n - k + 1]; v++) { n       xx[k] = p[v]; n       f[tmp][ppp] += dfss(k + 1); n     } n     return 0; n   } n   if (k == n) { n     ans += ::tmp[tmp]; n     return ::tmp[tmp]; n   } n   for (int v = yoo[0][tmp][n - k + 1]; v <= yoo[1][tmp][n - k + 1]; v++) { n     xx[k] = p[v]; n     dfss(k + 1); n   } n } n int main() { n   int test; n   cin >> test; n   for (int i = 2; i <= 99999; ++i) { n     bool can = 1; n     for (int j = 0; j < p_; ++j) n       if (i % p[j] == 0) { n         can = 0; n         break; n       } n     if (can) p[p_++] = i; n   } n   g[0] = 100000; n   g[1] = 10000; n   g[2] = 1000; n   g[3] = 100; n   g[4] = 10; n   g[5] = 1; n   memset(yoo[1], -1, sizeof yoo[1]); n   memset(yoo[0], 63, sizeof yoo[0]); n   memset(f, -1, sizeof f); n   for (int i = 0; i < p_; ++i) n     for (int j = 0; j < 5; ++j) n       if (4 == j) n         tmp[p[i] / g[j]]++; n       else n         yoo[0][p[i] / g[j + 1]][4 - j] = min(yoo[0][p[i] / g[j + 1]][4 - j], i), n                                     yoo[1][p[i] / g[j + 1]][4 - j] = n                                         max(yoo[1][p[i] / g[j + 1]][4 - j], i); n   for (; test--;) { n     ans = 0; n     cin >> now; n     ans = 0; n     n = 0; n     for (int i = now; i; i /= 10) n++; n     g[n + 1] = 1; n     for (int i = 0; i < n; ++i) g[n - i] = g[n - i + 1] * 10; n     xx[1] = now; n     if (n < 5) n       dfs(2); n     else n       dfss(2); n     cout << ans << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int tc, n, i, j, p, k; n   scanf( %d , &tc); n   while (tc--) { n     scanf( %d , &n); n     vector<int> v[n + 5]; n     for (i = 0; i < n; i++) { n       scanf( %d , &p); n       v[p].push_back(i); n     } n     k = 0; n     for (i = 1; i <= n; i++) { n       int b = 0; n       if (v[i].size() == 2) b = (v[i][1] - v[i][0]) > 1; n       if ((v[i].size() >= 3) || b) { n         k = 1; n         printf( YES ); n         break; n       } n     } n     if (!k) printf( NO ); n     printf(  n ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int a[200005]; n int main() { n   int n; n   scanf( %d , &n); n   int sum = 0; n   for (int i = 1; i <= n; ++i) { n     scanf( %d , &a[i]); n     sum += a[i]; n   } n   vector<int> ans; n   for (int i = 1; i <= n; ++i) { n     if ((sum - a[i]) % (n - 1) == 0 && (sum - a[i]) / (n - 1) == a[i]) { n       ans.push_back(i); n     } n   } n   printf( %d n , (int)ans.size()); n   for (auto p : ans) { n     printf( %d n , p); n   } n } n


#include <bits/stdc++.h> n using namespace std; n int get(string str) { n   int cnt = 0; n   for (int i = 0; i < str.size() - 1; i++) { n     if (str[i] ==  V  && str[i + 1] ==  K ) cnt++; n   } n   return cnt; n } n int main() { n   string str; n   cin >> str; n   int res = get(str); n   for (int i = 0; i < str.size(); i++) { n     string cp = str; n     cp[i] = (cp[i] ==  K  ?  V  :  K ); n     res = max<int>(res, get(cp)); n   } n   cout << res << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <class T> n inline bool fs(T &x) { n   int c = getchar(); n   int sgn = 1; n   while (~c && c <  0  || c >  9 ) { n     if (c ==  - ) sgn = -1; n     c = getchar(); n   } n   for (x = 0; ~c &&  0  <= c && c <=  9 ; c = getchar()) x = x * 10 + c -  0 ; n   x *= sgn; n   return ~c; n } n int ara[600][600]; n void boshao(int n) { n   int ln = n * 2; n   for (int i = 1; i <= n; i++) n     for (int j = n + 1; j <= ln; j++) ara[i][j] = ara[i][j - n]; n   for (int j = 1; j <= n; j++) n     for (int i = n + 1; i <= ln; i++) ara[i][j] = ara[i - n][j]; n   for (int j = n + 1; j <= ln; j++) n     for (int i = n + 1; i <= ln; i++) ara[i][j] = ara[i - n][j - n] ^ 1; n } n int main() { n   ara[1][1] = 1; n   ara[1][2] = 1; n   ara[2][1] = 1; n   ara[2][2] = 0; n   int n; n   fs(n); n   int x = 2; n   for (int i = 2; i <= 9; i++) { n     boshao(x); n     x *= 2; n   } n   for (int i = 1; i <= (1 << n); i++) { n     for (int j = 1; j <= (1 << n); j++) cout << ((ara[i][j]) ?  *  :  + ); n     cout << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int c1, c2, c3, c4; n   int n, m, x; n   int b = 0, t = 0, tot = 0; n   scanf( %d %d %d %d , &c1, &c2, &c3, &c4); n   scanf( %d %d , &n, &m); n   for (int i = 1; i <= n; i++) { n     scanf( %d , &x); n     if (x * c1 > c2) n       b += c2; n     else n       b += x * c1; n   } n   if (b > c3) b = c3; n   for (int i = 1; i <= m; i++) { n     scanf( %d , &x); n     if (x * c1 > c2) n       t += c2; n     else n       t += x * c1; n   } n   if (t > c3) t = c3; n   tot = b + t; n   if (tot > c4) tot = c4; n   printf( %d n , tot); n } n


#include <bits/stdc++.h> n using namespace std; n int q[111], qr[111], s[111]; n int d[222][111]; n int dp[111][222]; n bool ss[222]; n int n, k; n bool rec(int kk, int i) { n   if (dp[kk][k + i] != -1) return dp[kk][k + i]; n   if (kk == 0) return ss[k + i]; n   bool r; n   if (ss[k + i]) n     r = false; n   else { n     r = rec(kk - 1, i - 1) || rec(kk - 1, i + 1); n   } n   return dp[kk][k + i] = r; n } n int main() { n   cin >> n >> k; n   for (int(i) = 0; (i) < (int)(n); ++(i)) cin >> q[i], q[i]--; n   for (int(i) = 0; (i) < (int)(n); ++(i)) cin >> s[i], s[i]--; n   for (int(i) = 0; (i) < (int)(n); ++(i)) qr[q[i]] = i; n   for (int(i) = 0; (i) < (int)(n); ++(i)) d[k + 0][i] = i; n   for (int(i) = (int)(1); (i) <= (int)(k); ++(i)) n     for (int(j) = 0; (j) < (int)(n); ++(j)) d[k + i][j] = d[k + i - 1][q[j]]; n   for (int(i) = (int)(1); (i) <= (int)(k); ++(i)) n     for (int(j) = 0; (j) < (int)(n); ++(j)) d[k - i][j] = d[k + -i + 1][qr[j]]; n   for (int(i) = (int)(-k); (i) <= (int)(k); ++(i)) { n     ss[k + i] = vector<int>(d[k + i], d[k + i] + n) == vector<int>(s, s + n); n   } n   memset(dp, -1, sizeof(dp)); n   bool r = rec(k, 0); n   cout << (r ?  YES  :  NO ) << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long t, a, b; n int main() { n   scanf( %I64d%I64d%I64d , &t, &a, &b); n   if (t > a && a != b || b < a) n     printf( 0 n ); n   else if (t == 1) { n     if (a == b) { n       if (a == 1) n         printf( inf n ); n       else n         printf( 1 n ); n     } else if (a == 1) n       printf( 0 n ); n     else { n       long long temp = b; n       while (temp % a == 0) temp /= a; n       if (temp == 1) { n         printf( 1 n ); n         return 0; n       } n       int pt = 0; n       int cur = 1; n       while (b > 0) { n         long long digit = b % a; n         pt += cur * digit; n         b /= a; n         cur *= t; n       } n       if (pt == a) n         printf( 1 n ); n       else n         printf( 0 n ); n     } n   } else { n     if (a == b) { n       if (t == a) n         printf( 2 n ); n       else n         printf( 1 n ); n     } else { n       long long pt = 0; n       long long cur = 1; n       while (b > 0) { n         long long digit = b % a; n         pt += cur * digit; n         b /= a; n         cur *= t; n       } n       if (pt == a) n         printf( 1 n ); n       else n         printf( 0 n ); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long mod = 1000000007LL; n long long large = 2000000000000000000LL; n vector<int> dp; n vector<vector<int> > adj; n int f(int u) { n   if (dp[u] != -1) return dp[u]; n   vector<int> ch; n   for (int j = 0; j < (int)adj[u].size(); j++) { n     int v = adj[u][j]; n     ch.push_back(f(v)); n   } n   sort(ch.begin(), ch.end()); n   int re = 0; n   for (int i = 0; i < (int)ch.size(); i++) { n     if (ch[i] == re) n       re++; n     else if (ch[i] > re) n       break; n   } n   dp[u] = re; n   return dp[u]; n } n int main() { n   int n, m; n   cin >> n >> m; n   vector<int> h(n, 0); n   for (int i = 0; i < n; i++) scanf( %d , &h[i]); n   adj.assign(n, vector<int>()); n   for (int i = 0; i < m; i++) { n     int x, y; n     scanf( %d%d , &x, &y); n     x--; n     y--; n     adj[x].push_back(y); n   } n   dp.assign(n, -1); n   int ans = 0; n   int sz = 0; n   for (int i = 0; i < n; i++) sz = max(sz, f(i) + 1); n   vector<int> tp(sz, 0); n   for (int i = 0; i < n; i++) tp[f(i)] ^= h[i]; n   for (int i = 0; i < sz; i++) ans |= tp[i]; n   if (ans == 0) { n     cout <<  LOSE  << endl; n     return 0; n   } n   int p = -1; n   for (int i = 0; i < sz; i++) { n     if (tp[i]) p = i; n   } n   for (int i = 0; i < n; i++) n     if (f(i) == p && (h[i] ^ tp[p]) < h[i]) { n       h[i] ^= tp[p]; n       tp[p] = 0; n       for (int j = 0; j < (int)adj[i].size(); j++) { n         int v = adj[i][j]; n         h[v] ^= tp[f(v)]; n         tp[f(v)] = 0; n       } n       break; n     } n   cout <<  WIN  << endl; n   for (int i = 0; i < n; i++) cout << h[i] <<    ; n   cout << endl; n   return 0; n } n


#include <bits/stdc++.h> n #pragma GCC optimize( O3 ) n using namespace std; n using ll = long long; n using ld = long double; n using pi = pair<int, int>; n using pl = pair<ll, ll>; n using vi = vector<int>; n using vl = vector<ll>; n using vpi = vector<pi>; n using si = set<int>; n using sl = set<ll>; n using qi = queue<int>; n using ql = queue<ll>; n template <class T> n bool uin(T& a, T b) { n   return a > b ? (a = b, true) : false; n } n template <class T> n bool uax(T& a, T b) { n   return a < b ? (a = b, true) : false; n } n template <class T> n bool uin(T& u, T& v, T a, T b) { n   return v - u > b - a ? (u = a, v = b, true) : false; n } n template <class T> n bool uax(T& u, T& v, T a, T b) { n   return v - u < b - a ? (u = a, v = b, true) : false; n } n namespace input { n template <class T> n void re(complex<T>& x); n template <class T1, class T2> n void re(pair<T1, T2>& p); n template <class T> n void re(vector<T>& a); n template <class T, size_t SZ> n void re(array<T, SZ>& a); n template <class T> n void re(T& x) { n   cin >> x; n } n void re(double& x) { n   string t; n   re(t); n   x = stod(t); n } n void re(ld& x) { n   string t; n   re(t); n   x = stold(t); n } n template <class T, class... Ts> n void re(T& t, Ts&... ts) { n   re(t); n   re(ts...); n } n template <class T> n void re(complex<T>& x) { n   T a, b; n   re(a, b); n   x = cd(a, b); n } n template <class T1, class T2> n void re(pair<T1, T2>& p) { n   re(p.f, p.s); n } n template <class T> n void re(vector<T>& a) { n   for (int i = 0; i < (((int)(a).size())); ++i) re(a[i]); n } n template <class T, size_t SZ> n void re(array<T, SZ>& a) { n   for (int i = 0; i < (SZ); ++i) re(a[i]); n } n }  // namespace input n using namespace input; n namespace output { n void pr(int x) { cout << x; } n void pr(long x) { cout << x; } n void pr(ll x) { cout << x; } n void pr(unsigned x) { cout << x; } n void pr(unsigned long x) { cout << x; } n void pr(unsigned long long x) { cout << x; } n void pr(float x) { cout << x; } n void pr(double x) { cout << x; } n void pr(ld x) { cout << x; } n void pr(char x) { cout << x; } n void pr(const char* x) { cout << x; } n void pr(const string& x) { cout << x; } n void pr(bool x) { pr(x ?  true  :  false ); } n template <class T> n void pr(const complex<T>& x) { n   cout << x; n } n template <class T1, class T2> n void pr(const pair<T1, T2>& x); n template <class T> n void pr(const T& x); n template <class T, class... Ts> n void pr(const T& t, const Ts&... ts) { n   pr(t); n   pr(ts...); n } n template <class T1, class T2> n void pr(const pair<T1, T2>& x) { n   pr( { , x.f,  ,  , x.s,  } ); n } n template <class T> n void pr(const T& x) { n   pr( { ); n   bool fst = 1; n   for (const auto& a : x) pr(!fst ?  ,   :   , a), fst = 0; n   pr( } ); n } n void ps() { pr(  n ); } n template <class T, class... Ts> n void ps(const T& t, const Ts&... ts) { n   pr(t); n   if (sizeof...(ts)) pr(   ); n   ps(ts...); n } n void pc() { pr( ] n ); } n template <class T, class... Ts> n void pc(const T& t, const Ts&... ts) { n   pr(t); n   if (sizeof...(ts)) pr( ,  ); n   pc(ts...); n } n }  // namespace output n using namespace output; n inline int bs(bool (*valid)(int), int l, int r, bool order) { n   l--, r++; n   if (!order) swap(l, r); n   while (abs(r - l) > 1) { n     int mid = (l + r) >> 1; n     if (valid(mid)) n       r = mid; n     else n       l = mid; n   } n   valid(r); n   return r; n } n struct dsu { n   vector<int> p; n   dsu(int n) { p.resize(n + 1); } n   inline int get(int x) { return p[x] ? p[x] = get(p[x]) : x; } n   inline bool mrg(int x, int y) { n     return get(x) == get(y) ? false : p[get(x)] = get(y); n   } n }; n const int M = 1e9 + 7; n const ll lnf = 1e18 + 3; n const int inf = 1e9 + 3; n const int nax = 2e5 + 5; n typedef decay<decltype(M)>::type T; n struct mi { n   T val; n   explicit operator T() const { return val; } n   mi() { val = 0; } n   mi(const ll& v) { n     val = (-M <= v && v <= M) ? v : v % M; n     if (val < 0) val += M; n   } n   friend void pr(const mi& a) { pr(a.val); } n   friend void re(mi& a) { n     ll x; n     re(x); n     a = mi(x); n   } n   friend bool operator==(const mi& a, const mi& b) { return a.val == b.val; } n   friend bool operator!=(const mi& a, const mi& b) { return !(a == b); } n   friend bool operator<(const mi& a, const mi& b) { return a.val < b.val; } n   mi operator-() const { return mi(-val); } n   mi& operator+=(const mi& m) { n     if ((val += m.val) >= M) val -= M; n     return *this; n   } n   mi& operator-=(const mi& m) { n     if ((val -= m.val) < 0) val += M; n     return *this; n   } n   mi& operator*=(const mi& m) { n     val = (ll)val * m.val % M; n     return *this; n   } n   friend mi pow(mi a, ll p) { n     mi ans = 1; n     assert(p >= 0); n     for (; p; p /= 2, a *= a) n       if (p & 1) ans *= a; n     return ans; n   } n   friend mi inv(const mi& a) { n     assert(a != 0); n     return pow(a, M - 2); n   } n   mi& operator/=(const mi& m) { return (*this) *= inv(m); } n   friend mi operator+(mi a, const mi& b) { return a += b; } n   friend mi operator-(mi a, const mi& b) { return a -= b; } n   friend mi operator*(mi a, const mi& b) { return a *= b; } n   friend mi operator/(mi a, const mi& b) { return a /= b; } n }; n vi invs, fac, ifac; n void genFac(int SZ) { n   invs.resize(SZ), fac.resize(SZ), ifac.resize(SZ); n   invs[1] = fac[0] = ifac[0] = 1; n   for (int i = (2); i <= (SZ - 1); ++i) n     invs[i] = M - (ll)M / i * invs[M % i] % M; n   for (int i = (1); i <= (SZ - 1); ++i) { n     fac[i] = (ll)fac[i - 1] * i % M; n     ifac[i] = (ll)ifac[i - 1] * invs[i] % M; n   } n } n ll comb(int a, int b) { n   if (a < b || b < 0) return 0; n   return (ll)fac[a] * ifac[b] % M * ifac[a - b] % M; n } n int main() { n   ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL); n   int test_case; n   cin >> test_case; n   while (test_case--) { n     int n; n     re(n); n     vi a(n, 0); n     re(a); n     int q = 0, p = n - 1; n     while (a[q] >= q) q++; n     while (a[p] >= n - 1 - p) p--; n     ps(q > p + 1 ?  YES  :  NO ); n   } n } n


#include <bits/stdc++.h> n using namespace std; n const long long INF = 1e18; n const double PI = acos(-1); n long long mod = 1e9 + 7; n struct segment { n   long long l, r; n   bool operator<(const segment& s) const { n     if (r != s.r) return r > s.r; n     return l < s.l; n   } n }; n const long long MAXN = 2e5 + 7; n vector<segment> vs(MAXN); n vector<long long> sum(MAXN, 0), sub(MAXN, 0), ans(MAXN, 0); n vector<vector<long long>> vv(MAXN); n int32_t main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   long long n, k, l, r; n   cin >> n >> k; n   long long m = 1; n   for (long long i = 0; i < n; i++) { n     cin >> vs[i].l >> vs[i].r; n     m = max(m, vs[i].r); n     sum[vs[i].l]++; n     sum[vs[i].r + 1]--; n     vv[vs[i].l].push_back(i + 1); n     vv[vs[i].r + 1].push_back(-i - 1); n   } n   for (long long i = 1; i <= m; i++) sum[i] += sum[i - 1]; n   set<pair<segment, long long>> s; n   long long cntSub = 0; n   long long cnt = 0; n   for (long long i = 1; i <= m; i++) { n     for (long long& idx : vv[i]) { n       if (idx > 0) { n         s.insert({vs[idx - 1], idx}); n       } else { n         idx *= -1; n         s.erase({vs[idx - 1], idx}); n       } n     } n     cntSub += sub[i]; n     while (sum[i] - cntSub > k) { n       auto it = *s.begin(); n       cntSub++; n       s.erase(it); n       sub[it.first.r + 1]--; n       ans[it.second] = 1; n       cnt++; n     } n   } n   cout << cnt <<   n ; n   for (long long i = 1; i <= n; i++) n     if (ans[i]) cout << i <<    ; n   cout <<   n ; n   return 0; n } n