#include <bits/stdc++.h> n using namespace std; n const int MOD = int(1e9) + 7; n int n; n int a[100000], b[100000]; n int who[200005]; n bool used[200005] = {}; n vector<int> g[200005]; n int mul; n int far[200005]; n int tr[200005]; n void dfs(int v) { n   ++mul; n   used[v] = true; n   for (int to : g[v]) { n     dfs(to); n   } n } n int main() { n   scanf( %d , &n); n   for (int i = 0; i < (int)(n); ++i) { n     scanf( %d%d , a + i, b + i); n     --a[i]; n     --b[i]; n     g[b[i]].push_back(a[i]); n   } n   memset(who, -1, sizeof who); n   for (int i = 0; i < (int)(n); ++i) who[a[i]] = i; n   int ans = 1; n   for (int i = 0; i < (int)(2 * n); ++i) n     if (who[i] == -1) { n       mul = 0; n       dfs(i); n       ans = (long long)ans * mul % MOD; n     } n   memset(far, -1, sizeof far); n   for (int i = 0; i < (int)(n); ++i) far[a[i]] = b[i]; n   for (int _ = 0; _ < (int)(25); ++_) n     for (int i = 0; i < (int)(2 * n); ++i) { n       if (far[i] != -1) { n         far[i] = far[far[i]]; n       } n     } n   for (int i = 0; i < (int)(n); ++i) tr[a[i]] = b[i]; n   for (int i = 0; i < (int)(2 * n); ++i) n     if (far[i] != -1 && !used[far[i]]) { n       int c = 0; n       for (int x = far[i]; !used[x]; x = tr[x]) { n         ++c; n         used[x] = true; n       } n       if (c > 1) { n         ans = 2ll * ans % MOD; n       } n     } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 2e5 + 40; n int num[MAXN]; n int a[MAXN]; n int pref[MAXN]; n bool isneg = false; n int n; n int max_k; n int mabs(int x) { n   if (x < 0) { n     return -x; n   } n   return x; n } n void neg() { n   isneg = !isneg; n   for (int i = 0; i < MAXN; ++i) { n     num[i] = -num[i]; n     a[i] = -a[i]; n   } n } n void bin() { n   int carry = 0; n   for (int i = 0; i < MAXN; ++i) { n     if (mabs(num[i] + carry) % 2 == 0) { n       carry = (num[i] + carry) / 2; n       num[i] = 0; n     } else { n       carry = (num[i] + carry - 1) / 2; n       num[i] = 1; n     } n   } n } n void calc_pref() { n   pref[0] = num[0]; n   for (int i = 1; i < MAXN; ++i) { n     pref[i] = pref[i - 1] + num[i]; n   } n } n int cnt_ones(int from, int to) { n   if (from < to) { n     return 0; n   } n   return pref[from] - (to > 0 ? pref[to - 1] : 0); n } n void prepare_num() { n   int carry = 0; n   for (int i = 0; i < MAXN; ++i) { n     if (mabs(num[i] + carry) % 2 == 0) { n       carry = (num[i] + carry) / 2; n     } else { n       carry = (num[i] + carry - 1) / 2; n     } n   } n   if (carry < 0) { n     neg(); n   } n   bin(); n } n void add(int x, int power) { n   int k = x < 0 ? -1 : 1; n   x = k * x; n   while (x != 0) { n     num[power++] += k * (x % 2); n     x /= 2; n   } n } n bool try_for(int index) { n   bool zero_allowed = index != n; n   int a_i = a[index]; n   int k = a_i < 0 ? -1 : 1; n   a_i *= k; n   int carry = 0; n   long long cur_num = 0; n   int power = 0; n   for (int i = 0; i <= 32; ++i) { n     int cur_digit = num[index++] - k * (a_i % 2) + carry; n     a_i /= 2; n     if (mabs(cur_digit) % 2 == 0) { n       carry = cur_digit / 2; n     } else { n       carry = (cur_digit - 1) / 2; n       cur_num += 1ll << power; n     } n     ++power; n     if (cnt_ones(MAXN - 1, index) == 0 && carry == -1) { n       while (cur_num > 0) { n         cur_num += 1ll << (power++); n       } n       break; n     } n   } n   if (cnt_ones(MAXN - 1, index) != 0 || (carry != 0 && carry != -1) || n       cur_num > max_k || cur_num < -max_k || (cur_num == 0 && !zero_allowed)) { n     return false; n   } else { n     return true; n   } n } n int main() { n   scanf( %d%d , &n, &max_k); n   for (int i = 0; i <= n; ++i) { n     scanf( %d , &a[i]); n     add(a[i], i); n   } n   prepare_num(); n   calc_pref(); n   int ans = 0; n   for (int i = 0; i <= n; ++i) { n     if (cnt_ones(i - 1, 0) > 0) { n       break; n     } n     ans += try_for(i); n   } n   printf( %d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long maxn = 2e5 + 8, inf = 1e18 + 9, mod = 1e9 + 7; n char s[maxn]; n long long n, m; n void solve() { n   long long i, j, ans = 1; n   cin >> n >> (s + 1); n   for (i = 2; i <= n; i++) n     if (s[i] != s[i - 1]) ans++; n   cout << min(ans + 2, n) << endl; n } n signed main() { n   ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0); n   long long t = 1; n   while (t--) solve(); n   return 0; n } n


#include <bits/stdc++.h> n int main() { n   int x, y; n   scanf( %d%d , &x, &y); n   int i = 1; n   while (1) { n     if (y < 2 || ((100 * x) + (10 * y)) < 220) { n       if (i & 1 == 1) n         printf( Hanako ); n       else n         printf( Ciel ); n       return 0; n     } n     if (i & 1 == 1) { n       if (x >= 2) { n         x -= 2; n         y -= 2; n       } else { n         y -= ((220 - (100 * x))) / 10; n         x = 0; n       } n     } else { n       if (y >= 22) n         y -= 22; n       else if (y >= 12) { n         x--; n         y -= 12; n       } else { n         x -= 2; n         y -= 2; n       } n     } n     i++; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace ::std; n const long long maxn = 1e5 + 500; n const long long mod = 1e9 + 7; n const long long inf = 1e9 + 500; n long long out[maxn]; n long long in[maxn]; n long long a[maxn]; n long long b[maxn]; n long long dp[2][maxn]; n int main() { n   fill(out, out + maxn, -1); n   fill(in, in + maxn, -1); n   long long n, q, t; n   cin >> n >> q >> t; n   for (long long i = 0; i < n; i++) { n     cin >> a[i]; n   } n   for (long long i = 0; i < q; i++) { n     long long v, u; n     cin >> v >> u; n     v--; n     u--; n     out[u] = v; n     in[v] = u; n   } n   for (long long i = 0; i < n; i++) { n     for (long long j = 0; j < n; j++) { n       if (out[j] == -1) { n         b[j] = a[j]; n       } else { n         if (b[out[j]] != 0) { n           b[j] = b[out[j]] + a[j]; n         } n       } n     } n   } n   for (long long i = 0; i < n; i++) { n     if (b[i] == 0) { n       cout << 0; n       return 0; n     } n     if (in[i] != -1) { n       t -= b[i]; n     } n   } n   if (t < 0) { n     cout << 0; n     return 0; n   } n   dp[1][0] = 1; n   for (long long i = 0; i < n; i++) { n     for (long long j = 0; j < b[i]; j++) { n       dp[i & 1][j] = dp[(i & 1) ^ 1][j]; n     } n     for (long long j = b[i]; j <= t; j++) { n       dp[i & 1][j] = n           ((long long)dp[(i & 1) ^ 1][j] + dp[(i & 1)][j - b[i]]) % mod; n     } n   } n   cout << dp[(n - 1) & 1][t]; 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 n, k; n     cin >> n >> k; n     long long cur = 1, now = 1, ans = 0; n     while (now <= k && cur < n) { n       cur += now; n       now <<= 1; n       ans++; n     } n     if (cur < n) { n       ans += (n - cur + k - 1) / k; n     } n     printf( %lld n , ans); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, a, b; n int check(int p) { n   int res; n   res = a / p + b / p; n   if (a / p == 0 || b / p == 0) return 0; n   if (res >= n) n     return 1; n   else n     return 0; n } n int main() { n   int l, r, mid; n   scanf( %d%d%d , &n, &a, &b); n   r = a + b; n   l = 1; n   while (r - l > 1) { n     mid = (r + l) / 2; n     if (check(mid)) n       l = mid; n     else n       r = mid; n   } n   printf( %d n , l); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct node { n   int u; n   long long int cost; n   node(int _u, long long int _cost) { n     u = _u; n     cost = _cost; n   } n   bool operator<(const node& p) const { n     return (long long int)cost < (long long int)p.cost; n   } n }; n int numNodes, numEdges; n int source; n bool p; n vector<vector<int> > G; n map<pair<int, int>, int> cost; n vector<long long int> dist; n vector<int> par; n void clear() { n   G.clear(); n   cost.clear(); n   dist.clear(); n } n void dijstkra() { n   priority_queue<pair<long long int, int>, vector<pair<long long int, int> >, n                  greater<pair<long long int, int> > > n       q; n   dist[source] = 0LL; n   q.push({0LL, source}); n   while (!q.empty()) { n     int u = q.top().second; n     q.pop(); n     for (auto v : G[u]) { n       if (dist[u] + cost[{u, v}] < dist[v]) { n         dist[v] = dist[u] + cost[{u, v}]; n         par[v] = u; n         q.push({dist[v], v}); n       } n     } n   } n } n void find_path(int s, int e) { n   if ((s == e) || (e == -1)) { n     if (p) cout <<    ; n     p = true; n     cout << s; n   } else { n     find_path(s, par[e]); n     if (p) cout <<    ; n     p = true; n     cout << e; n   } n } n int main() { n   ios_base::sync_with_stdio(false); n   p = false; n   source = 1; n   cin >> numNodes >> numEdges; n   dist.resize(numNodes + 1, LLONG_MAX); n   par.resize(numNodes + 1, -1); n   G.resize(numNodes + 1); n   for (int i = 1; i <= numEdges; i++) { n     int u, v; n     int w; n     cin >> u >> v >> w; n     G[u].push_back(v); n     G[v].push_back(u); n     cost[pair<int, int>(u, v)] = w; n     cost[pair<int, int>(v, u)] = w; n   } n   dijstkra(); n   if (par[numNodes] == -1) { n     cout <<  -1 n ; n     return 0; n   } n   find_path(source, numNodes); n   clear(); n } n


#include <bits/stdc++.h> n using namespace std; n int Id[2 * 105]; n int L[2 * 105]; n int W[2 * 105]; n long long int dp[2 * 105][3005]; n bool vis[2 * 105][3005]; n int n, l; n long long int solve(int LastTp, int Len) { n   if (Len > l) return 0LL; n   if (Len == l) return 1LL; n   if (vis[LastTp][Len] == 1) return dp[LastTp][Len]; n   vis[LastTp][Len] = 1; n   long long int ret = 0; n   for (int i = 1; i <= n; i++) { n     if (Id[i] == Id[LastTp]) continue; n     if (W[LastTp] == L[i]) ret = (ret + solve(i, Len + L[i])) % 1000000007; n   } n   return dp[LastTp][Len] = ret; n } n int main() { n   int N, x, y; n   scanf( %d %d , &N, &l); n   for (int i = 1; i <= N; i++) { n     scanf( %d %d , &x, &y); n     L[++n] = x; n     W[n] = y; n     Id[n] = i; n     if (x != y) { n       L[++n] = y; n       W[n] = x; n       Id[n] = i; n     } n   } n   long long int Sum = 0; n   for (int i = 1; i <= n; i++) Sum = (Sum + solve(i, L[i])) % 1000000007; n   cout << Sum << endl; n } n


#include <bits/stdc++.h> n using namespace std; n long long n; n int sum = 1; n int main() { n   cin >> n; n   for (int i = 1; i < n; i++) sum += i * 12; n   cout << sum; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main(int argc, const char* argv[]) { n   int n, cnt = 0; n   cin >> n; n   int a[100]; n   memset(a, 0, sizeof(a)); n   for (int i = 0; i < n; i++) { n     int k, x = 0; n     cin >> k; n     while (k != 0) { n       x = x + k % 10; n       k = k / 10; n     } n     a[x]++; n   } n   for (int i = 1; i < 100; i++) { n     while (a[i] > 0) { n       a[i] = a[i] - 1; n       for (int j = i; j < 100; j++) { n         if (a[j] > 0 && (i + j) % 3 == 0) { n           cnt++; n           a[j]--; n           break; n         } n       } n     } n   } n   cout << cnt; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int INF = 1000000000 + 5; n const int N = 2e5 + 5; n const long long oo = 1e18 + 5; n const long long mod = 998244353; n int a[N], b[N]; n int mul(int a, int b) { return 1ll * a * b % mod; } n void solve() { n   int n, k; n   cin >> n >> k; n   vector<int> pos(n + 2); n   vector<int> lv(n + 2); n   for (int i = 1; i <= n; ++i) { n     int x; n     cin >> x; n     a[i] = x; n     pos[x] = i; n     lv[i] = 0; n   } n   for (int i = 1; i <= k; ++i) { n     int x; n     cin >> x; n     b[i] = x; n     lv[pos[x]] = i; n   } n   lv[0] = lv[n + 1] = INF; n   int ans = 1; n   for (int i = 1; i <= n; ++i) { n     if (lv[i]) { n       if (lv[i] > lv[i - 1] && lv[i] > lv[i + 1]) n         ans = mul(ans, 2); n       else { n         if (lv[i] <= lv[i - 1] && lv[i] <= lv[i + 1]) { n           cout << 0 <<   n ; n           return; n         } n       } n     } n   } n   cout << ans <<   n ; n } n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   int t; n   cin >> t; 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 t; n   cin >> t; n   while (t--) { n     vector<string> s(9); n     for (long long int i = 0; i < 9; i++) { n       cin >> s[i]; n     } n     s[0][0] = s[0][1]; n     s[1][3] = s[1][4]; n     s[2][6] = s[2][7]; n     s[3][1] = s[3][2]; n     s[4][4] = s[4][5]; n     s[5][7] = s[5][8]; n     s[6][2] = s[6][1]; n     s[7][5] = s[7][4]; n     s[8][8] = s[8][7]; n     for (long long int i = 0; i < 9; i++) { n       cout << s[i] << endl; n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n struct Node { n   int l, r; n   pair<int, int> v; n   int mid() { return (l + r) >> 1; } n }; n struct HNode { n   int m, p; n   long long x, y, r, d; n   bool operator<(const HNode &cur) const { return d < cur.d; } n }; n Node tree[4 * 250005]; n HNode hook[250005]; n int n; n vector<int> has; n void push_up(int pos) { n   tree[pos].v = min(tree[pos << 1].v, tree[pos << 1 | 1].v); n } n void build(int pos, int l, int r) { n   tree[pos].l = l; n   tree[pos].r = r; n   if (l == r) { n     tree[pos].v = make_pair(hook[l].m, l); n     return; n   } n   int mid = tree[pos].mid(); n   build(pos << 1, l, mid); n   build(pos << 1 | 1, mid + 1, r); n   push_up(pos); n } n void update(int pos, int idx, int mass) { n   if (tree[pos].l == tree[pos].r) { n     tree[pos].v.first = mass; n     return; n   } n   int mid = tree[pos].mid(); n   if (idx <= mid) n     update(pos << 1, idx, mass); n   else n     update(pos << 1 | 1, idx, mass); n   push_up(pos); n } n pair<int, int> query(int pos, int l, int r) { n   if (l <= tree[pos].l && tree[pos].r <= r) return tree[pos].v; n   int mid = tree[pos].mid(); n   pair<int, int> ret = make_pair(0x3fffffff, 0x3fffffff); n   if (l <= mid) ret = min(ret, query(pos << 1, l, r)); n   if (r > mid) ret = min(ret, query(pos << 1 | 1, l, r)); n   return ret; n } n int bifind(long long v) { n   int l = 1, r = n, mid; n   if (hook[r].d <= v) return r; n   if (hook[l].d > v) return -1; n   while (l + 1 < r) { n     mid = (l + r) >> 1; n     if (hook[mid].d <= v) n       l = mid; n     else n       r = mid; n   } n   return l; n } n int main() { n   int i, j, k; n   while (scanf( %I64d%I64d%d%I64d%d , &hook[0].x, &hook[0].y, &hook[0].p, n                &hook[0].r, &n) != EOF) { n     for (i = 1; i <= n; i++) n       scanf( %I64d%I64d%d%d%I64d , &hook[i].x, &hook[i].y, &hook[i].m, n             &hook[i].p, &hook[i].r); n     for (i = 0; i <= n; i++) { n       hook[i].r *= hook[i].r; n       hook[i].d = (hook[0].x - hook[i].x) * (hook[0].x - hook[i].x) + n                   (hook[0].y - hook[i].y) * (hook[0].y - hook[i].y); n     } n     sort(hook + 1, hook + 1 + n); n     has.clear(); n     has.push_back(0); n     build(1, 1, n); n     for (i = 0; i < has.size(); i++) { n       int rs = bifind(hook[has[i]].r); n       while (rs != -1) { n         pair<int, int> cur = query(1, 1, rs); n         if (cur.first <= hook[has[i]].p) { n           update(1, cur.second, 0x3fffffff); n           has.push_back(cur.second); n         } else n           break; n       } n     } n     printf( %d n , has.size() - 1); n   } n   return 0; n } n


#include <bits/stdc++.h> n #pragma GCC optimize( -O3 ) n #pragma GCC optimize( Ofast ) n #pragma GCC optimize( unroll-loops ) n using namespace std; n long long n, m, k, i, ost, x, y, ans, a[200010], f[200010], ost1, sum1; n vector<pair<long long, long long>> v3, v1, v2; n pair<long long, long long> sum; n multiset<long long> st, st1; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   cin >> n >> m >> k; n   for (i = 1; i <= n; i++) cin >> a[i]; n   cin >> x; n   for (i = 0; i < x; i++) { n     cin >> y; n     f[y]++; n   } n   cin >> x; n   for (i = 0; i < x; i++) { n     cin >> y; n     f[y] += 2; n   } n   for (i = 1; i <= n; i++) { n     if (f[i] == 1) v1.push_back(make_pair(a[i], i)); n     if (f[i] == 2) v2.push_back(make_pair(a[i], i)); n     if (f[i] == 3) v3.push_back(make_pair(a[i], i)); n   } n   sort(v1.begin(), v1.end()); n   sort(v2.begin(), v2.end()); n   sort(v3.begin(), v3.end()); n   x = v3.size(); n   x = min(x, k); n   y = k - x; n   if (min((long long)v1.size(), (long long)v2.size()) < y or m < x + y * 2) { n     cout << -1 << endl; n     return 0; n   } n   for (i = 0; i < x; i++) { n     sum.first += v3[i].first; n     a[v3[i].second] = 1e18; n   } n   for (i = 0; i < y; i++) { n     sum.first += v1[i].first; n     a[v1[i].second] = 1e18; n     sum.first += v2[i].first; n     a[v2[i].second] = 1e18; n   } n   sort(a + 1, a + n + 1); n   ost = m - x - 2 * y; n   for (i = 1; i <= n; i++) { n     if (i <= ost) n       st1.insert(a[i]), sum.second += a[i]; n     else n       st.insert(a[i]); n   } n   ans = sum.first + sum.second; n   while (x) { n     if (min((long long)v1.size(), (long long)v2.size()) <= y or y > k or n         x + (y + 1) * 2 - 1 > m) n       break; n     if (ost > 0) ost--; n     x--; n     sum.first -= v3[x].first; n     sum.first += v1[y].first + v2[y].first; n     st.insert(v3[x].first); n     auto it = st.find(v1[y].first); n     if (it != st.end()) n       st.erase(it); n     else { n       it = st1.find(v1[y].first); n       st1.erase(it); n       sum.second -= v1[y].first; n     } n     it = st.find(v2[y].first); n     if (it != st.end()) n       st.erase(it); n     else { n       it = st1.find(v2[y].first); n       st1.erase(it); n       sum.second -= v2[y].first; n     } n     y++; n     ost1 = st1.size(), sum1 = sum.second; n     it = st1.end(); n     while (ost1 > ost) { n       ost1--; n       it--; n       sum1 -= *it; n     } n     for (auto it = st.begin(); it != st.end(); it++) { n       ost1++; n       if (ost1 > ost) break; n       sum1 += *it; n     } n     ans = min(ans, sum.first + sum1); n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<vector<int> > ad; n vector<int> cam(0); n vector<bool> vis; n bool k = false; n void dfs(const int &m) { n   vis[m] = true; n   if (m == ad.size() - 1) { n     k = true; n   } else n     for (int i = 0; i < ad[m].size(); i++) { n       if (!vis[ad[m][i]]) dfs(ad[m][i]); n       if (k) break; n     } n   if (k) { n     cam.push_back(m); n   } n } n int main() { n   int n; n   cin >> n; n   ad.resize(n + 1, vector<int>(0)); n   vis.resize(n + 1, false); n   for (int i = int(2); i < int(n + 1); i += int(1)) { n     int x; n     cin >> x; n     ad[i].push_back(x); n     ad[x].push_back(i); n   } n   dfs(1); n   for (int i = cam.size() - 1; i >= 0; i--) cout << cam[i] <<    ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 2e5 + 10; n int a[N]; n long long sum[N]; n int main() { n   int n; 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++) sum[i] = sum[i - 1] + a[i]; n   long long ans_sum = 0; n   int ans_len = 0, pos = 1, len = 1; n   for (int i = 1; i <= n; i++) { n     int l = 0, r = min(i - 1, n - i); n     while (l < r) { n       int ml = (2 * l + r) / 3, mr = (2 * r + l + 2) / 3; n       long long suml = sum[i] - sum[i - ml - 1] + sum[n] - sum[n - ml]; n       long long sumr = sum[i] - sum[i - mr - 1] + sum[n] - sum[n - mr]; n       if (suml * (2 * mr + 1) < sumr * (2 * ml + 1)) n         l = ml + 1; n       else n         r = mr - 1; n     } n     long long tmp_sum = n         sum[i] - sum[i - l - 1] + sum[n] - sum[n - l] - (l * 2 + 1LL) * a[i]; n     if (tmp_sum * len > ans_sum * (2 * l + 1)) n       ans_sum = tmp_sum, ans_len = l, pos = i, len = 2 * l + 1; n   } n   printf( %d n , ans_len * 2 + 1); n   for (int i = pos - ans_len; i <= pos; i++) printf( %d  , a[i]); n   for (int i = n - ans_len + 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   int N, p; n   scanf( %d%d , &N, &p); n   int i; n   p--; n   string S; n   cin >> S; n   int sz = S.size(), add = 0, flag = 0, first, last; n   for (i = 0; i < sz / 2; i++) { n     char c1 = S[i], c2 = S[sz - 1 - i]; n     if (c1 != c2) { n       if (flag == 0) { n         first = i; n         flag = 1; n       } n       last = i; n     } n     add += min(abs(c1 - c2), abs(26 - abs(c1 - c2))); n   } n   if (p > sz / 2) { n     p = sz - 1 - p; n   } n   if (flag == 0) { n     printf( 0 ); n     return 0; n   } n   if (p != sz / 2) { n     if (last == first) { n       printf( %d , abs(p - last) + add); n       return 0; n     } n     if (last < p) { n       printf( %d , abs(p - first) + add); n       return 0; n     } n     if (first > p) { n       printf( %d , abs(p - last) + add); n       return 0; n     } n     printf( n          %d , n         min(2 * (p - first) + (last - p), 2 * (last - p) + (p - first)) + add); n     return 0; n   } n   if (p == sz / 2) { n     if (sz % 2) { n       printf( %d , p - first + add); n       return 0; n     } else { n       p--; n       printf( %d , p - first + add); n       return 0; n     } n   } n } n


#include <bits/stdc++.h> n int main() { n   int J, M; n   float QJ, QM; n   int i; n   scanf( %d , &J); n   scanf( %d , &M); n   i = 1; n   while (1) { n     J = J - i; n     if (J < 0) { n       printf( Vladik n ); n       break; n     } n     i++; n     M = M - i; n     if (M < 0) { n       printf( Valera n ); n       break; n     } n     i++; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int a[3001]; n void swap(int i, int j) { n   int temp = a[i]; n   a[i] = a[j]; n   a[j] = temp; n } n int main() { n   int n; n   cin >> n; n   ; n   for (int i = 0; i < n; i++) { n     cin >> a[i]; n   } n   vector<pair<int, int> > x; n   for (int i = 0; i < n; i++) { n     int min = a[i]; n     int sw = i; n     for (int j = i + 1; j < n; j++) { n       if (a[j] < min) { n         min = a[j]; n         sw = j; n       } n     } n     if (sw != i) { n       x.push_back(make_pair(i, sw)); n       swap(i, sw); n     } n   } n   cout << x.size() << endl; n   for (vector<pair<int, int> >::iterator i = x.begin(); i != x.end(); i++) { n     cout << (*i).first <<     << (*i).second << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 3e5 + 5; n const int INF = 1e7; n struct info { n   pair<int, int> x = {-INF, -1}; n }; n struct segtree { n   int n; n   vector<info> t; n   segtree(int n) { n     this->n = n; n     t.resize(n * 4); n   } n   void Merge(info &node, info &l, info &r) { node.x = max(l.x, r.x); } n   void update(int node, int l, int r, int i, int j, int add) { n     if (l > j || r < i) { n       return; n     } n     if (l >= i && r <= j) { n       t[node].x = {add, i}; n       return; n     } n     int mid = (l + r) / 2; n     update(node * 2, l, mid, i, j, add); n     update(node * 2 + 1, mid + 1, r, i, j, add); n     Merge(t[node], t[node * 2], t[node * 2 + 1]); n   } n   void update(int l, int add) { update(1, 0, n - 1, l, l, add); } n   info query(int node, int l, int r, int i, int j) { n     if (l > j || r < i) { n       info x; n       return x; n     } n     if (l >= i && r <= j) { n       return t[node]; n     } n     int mid = (l + r) / 2; n     info x = query(node * 2, l, mid, i, j); n     info y = query(node * 2 + 1, mid + 1, r, i, j); n     info ret; n     Merge(ret, x, y); n     return ret; n   } n   pair<int, int> query(int l, int r) { n     info ret = query(1, 0, n - 1, l, r); n     return ret.x; n   } n }; n int n; n int a[N]; n int b[N]; n int dist[N][2]; n vector<int> adj[N]; n bool vis[N][2]; n int p[N][2]; n int go[N][2]; n int32_t main() { n   ios_base::sync_with_stdio(false); n   cin.tie(nullptr); n   cin >> n; n   segtree seg(n + 1); n   for (int i = 0; i <= n; i++) { n     dist[i][0] = INF; n     dist[i][1] = INF; n     seg.update(i, INF); n   } n   for (int i = 1; i <= n; i++) { n     cin >> a[i]; n   } n   for (int i = 1; i <= n; i++) { n     cin >> b[i]; n   } n   priority_queue<pair<int, pair<int, int> > > pq; n   pq.push({0, {n, 0}}); n   dist[n][0] = 0; n   dist[n][1] = 0; n   seg.update(n, 0); n   while (!pq.empty()) { n     pair<int, pair<int, int> > t = pq.top(); n     pq.pop(); n     int v = t.second.first; n     int state = t.second.second; n     if (vis[v][state]) continue; n     vis[v][state] = 1; n     if (state == 1) { n       int koi = v + b[v]; n       if (dist[koi][0] > dist[v][1]) { n         dist[koi][0] = dist[v][1]; n         go[koi][0] = v; n         pq.push({-dist[koi][0], {koi, 0}}); n       } n       continue; n     } n     int l = v - a[v]; n     int r = v; n     while (true) { n       pair<int, int> x = seg.query(l, r); n       if (x.first <= dist[v][0] + 1) { n         break; n       } n       int koi = x.second; n       dist[koi][1] = dist[v][0] + 1; n       go[koi][1] = v; n       seg.update(koi, dist[koi][1]); n       pq.push({-dist[koi][1], {koi, 1}}); n     } n   } n   if (dist[0][0] >= INF) { n     cout <<  -1 n ; n     return 0; n   } n   cout << dist[0][0] <<   n ; n   pair<int, int> now = {0, 0}; n   vector<int> ans; n   while (1) { n     if (now.second == 1) { n       ans.push_back(now.first); n     } n     if (now.first == n) { n       break; n     } n     now.first = go[now.first][now.second]; n     now.second ^= 1; n   }; n   reverse(ans.begin(), ans.end()); n   for (int i : ans) cout << i <<    ; n   cout <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int Distance(int x1, int y1, int x2, int y2) { n   return (x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2); n } n int main() { n   int n, m, x1, y1, x2, y2, x3, y3, x4, y4, flag, dis1, dis2; n   while (~scanf( %d%d , &n, &m)) { n     flag = 0; n     if (m > n) { n       swap(n, m), flag = 1; n     } n     if (m == 0) { n       x1 = 1, x2 = n; n       x3 = 0; n       x4 = n - 1; n       y1 = y2 = y3 = y4 = 0; n     } else { n       dis1 = Distance(0, 0, n, m) * 2 + Distance(0, 0, n, 0); n       dis2 = Distance(0, 0, n, m - 1) * 2 + Distance(0, 0, n, m); n       if (dis1 > dis2) { n         x1 = n; n         y1 = m; n         x2 = 0; n         y2 = 0; n         x3 = n; n         y3 = 0; n         x4 = 0; n         y4 = m; n       } else { n         x1 = 0; n         y1 = 1; n         x2 = n; n         y2 = m; n         x3 = 0; n         y3 = 0; n         x4 = n; n         y4 = m - 1; n       } n     } n     if (flag) { n       swap(x1, y1); n       swap(x2, y2); n       swap(x3, y3); n       swap(x4, y4); n     } n     printf( %d %d n%d %d n%d %d n%d %d n , x1, y1, x2, y2, x3, y3, x4, y4); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, a[111], f[(1 << (23))], x[33]; n int main() { n   while (cin >> n) { n     for (int i = 0, _a = (n); i < _a; i++) cin >> a[i]; n     if (n == 1) { n       cout << 1 << endl; n       continue; n     } n     int res = n + 1; n     for (int S = 0, _a = ((1 << (n))); S < _a; S++) f[S] = n + 1; n     f[1] = 1; n     for (int S = (1), _b = ((1 << (n)) - 2); S <= _b; S++) { n       int last = -1; n       for (int i = (n - 1), _b = (0); i >= _b; i--) n         if ((S & (1 << (i)))) { n           last = i; n           break; n         } n       int cur = f[S]; n       if (last == n - 1) { n         res = min(res, cur); n         continue; n       } n       int next = last + 1; n       bool ok = false; n       int nx = 0; n       for (int i = 0, _a = (n); i < _a; i++) n         if ((S & (1 << (i)))) x[++nx] = a[i]; n       sort(x + 1, x + nx + 1); n       int j = nx, i = 1; n       while (i <= j) { n         if (x[i] + x[j] == a[next]) { n           ok = true; n           break; n         } else if (x[i] + x[j] > a[next]) { n           --j; n         } else n           ++i; n       } n       if (!ok) continue; n       int S2 = S | (1 << (next)); n       f[S2] = min(f[S2], max(cur, __builtin_popcount(S2))); n       for (int i = 0, _a = (n); i < _a; i++) n         if ((S & (1 << (i)))) { n           S2 = S - (1 << (i)) + (1 << (next)); n           f[S2] = min(f[S2], max(cur, __builtin_popcount(S2))); n         } n     } n     if (res > n) n       cout << -1 << endl; n     else n       cout << res << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long a, b; n   cin >> a >> b; n   long long ans = 0; n   while (true) { n     if (!a || !b) { n       cout << ans; n       return 0; n     } n     if (a == b || (a % b == 0)) { n       cout << (a / b + ans); n       return 0; n     } n     if (a > b) { n       long long tmp = a / b; n       a -= tmp * b; n       ans += tmp; n     } else { n       long long tmp = b / a; n       b -= tmp * a; n       ans += tmp; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <class T> n bool ckmax(T& x, T y) { n   return x < y ? x = y, 1 : 0; n } n template <class T> n bool ckmin(T& x, T y) { n   return x > y ? x = y, 1 : 0; n } n inline int read() { n   int x = 0, f = 1; n   char ch = getchar(); n   while (!isdigit(ch)) { n     if (ch ==  - ) f = 0; n     ch = getchar(); n   } n   while (isdigit(ch)) x = x * 10 + ch -  0 , ch = getchar(); n   return f ? x : -x; n } n inline char rdopt() { n   char ch = getchar(); n   while (!isalpha(ch)) ch = getchar(); n   return ch; n } n const int N = 1 << 20; n int n, m, t1, t2, nq, pre[N]; n long long tr[N], ans[N]; n vector<int> e1[N], e2[N], u2[N]; n vector<pair<int, int> > u1[N], q[N]; n struct dsu { n   int F[N], siz[N]; n   inline void init(const int& n = N - 1) { n     for (int i = 1, iend = n; i <= iend; ++i) F[i] = i, siz[i] = 1; n   } n   inline int anc(int x) { return x == F[x] ? x : F[x] = anc(F[x]); } n } d1, d2; n inline void add(int x, long long d) { n   for (int i = x; i <= m; i += i & -i) tr[i] += d; n } n inline long long ask(int x) { n   long long res = 0; n   for (int i = x; i > 0; i -= i & -i) res += tr[i]; n   return res; n } n inline int query(int tim) { n   int res = 0, lim = ask(tim); n   for (int i = 19; i >= 0; --i) n     if (res + (1 << i) <= m && tr[res + (1 << i)] < lim) n       res += 1 << i, lim -= tr[res]; n   return res; n } n void dfs2(int u) { n   for (int i : u2[u]) add(i, 1); n   for (pair<int, int> i : q[u]) pre[i.second] = query(i.first); n   for (int v : e2[u]) dfs2(v); n   for (int i : u2[u]) add(i, -1); n } n void dfs1(int u) { n   for (pair<int, int> i : u1[u]) add(i.first, i.second); n   for (pair<int, int> i : q[u]) n     ans[i.second] = ask(i.first) - ask(pre[i.second]); n   for (int v : e1[u]) dfs1(v); n   for (pair<int, int> i : u1[u]) add(i.first, -i.second); n } n signed main() { n   n = read(), m = read(); n   d1.init(n), d2.init(n), t1 = t2 = n; n   for (int i = 1; i <= m; ++i) { n     char op = rdopt(); n     if (op ==  U ) { n       int x = read(), y = read(), fx = d1.anc(x), fy = d1.anc(y); n       ++t1, d1.F[fx] = d1.F[fy] = d1.F[t1] = t1, n             d1.siz[t1] = d1.siz[fx] + d1.siz[fy]; n       e1[t1].push_back(fx), e1[t1].push_back(fy); n     } else if (op ==  M ) { n       int x = read(), y = read(), fx = d2.anc(x), fy = d2.anc(y); n       ++t2, d2.F[fx] = d2.F[fy] = d2.F[t2] = t2; n       e2[t2].push_back(fx), e2[t2].push_back(fy); n     } else if (op ==  A ) { n       int x = read(), fx = d1.anc(x); n       u1[fx].push_back(make_pair(i, d1.siz[fx])); n     } else if (op ==  Z ) { n       int x = read(), fx = d2.anc(x); n       u2[fx].push_back(i); n     } else if (op ==  Q ) { n       int x = read(); n       q[x].push_back(make_pair(i, ++nq)); n     } n   } n   for (int i = 1, iend = t2; i <= iend; ++i) n     if (d2.F[i] == i) dfs2(i); n   for (int i = 1, iend = t1; i <= iend; ++i) n     if (d1.F[i] == i) dfs1(i); n   for (int i = 1; i <= nq; ++i) printf( %lld n , ans[i]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e6 + 100; n int n, m, k, a[N]; n bool mark[N]; n int main() { n   cin >> n >> m >> k; n   for (int i = 1; i <= n; i++) cin >> a[i], mark[a[i]] = 1; n   deque<int> que; n   int ans = 0; n   for (int i = 1; i < N; i++) { n     while (que.size() && que.front() <= i - m) que.pop_front(); n     if (mark[i]) que.push_back(i); n     if (que.size() >= k) n       while (que.size() >= k) que.pop_back(), ans++; n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n #pragma GCC optimize(2) n using namespace std; n template <typename T> n void read(T &x) { n   x = 0; n   char op = getchar(); n   int F = 1; n   while (!isdigit(op)) { n     if (op ==  - ) F *= -1; n     op = getchar(); n   } n   while (isdigit(op)) { n     x = (x << 1) + (x << 3) + op -  0 ; n     op = getchar(); n   } n   x *= F; n } n template <typename T, typename... Args> n void read(T &x, Args &...args) { n   read(x); n   read(args...); n } n template <typename T1, typename T2> n void ckmax(T1 &x, T2 y) { n   if (x < y) x = y; n } n template <typename T1, typename T2> n void ckmin(T1 &x, T2 y) { n   if (x > y) x = y; n } n int a[1005]; n int n, m, c; n void work() { n   int rec; n   cin >> rec; n   if (2 * rec <= c) { n     for (int i = 1; i <= n; i++) { n       if (a[i] > rec || !a[i]) { n         a[i] = rec; n         cout << i <<   n ; n         break; n       } n     } n   } else { n     for (int i = n; i >= 1; i--) { n       if (a[i] < rec || !a[i]) { n         a[i] = rec; n         cout << i <<   n ; n         break; n       } n     } n   } n   fflush(stdout); n } n int main() { n   cin >> n >> m >> c; n   while (m--) work(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long mod = 1e9 + 7; n const int maxn = 1e5 + 100; n long long a[maxn]; n map<long long, long long> p[maxn]; n vector<int> G[maxn]; n long long gcd(long long a, long long b) { return (b == 0) ? a : gcd(b, a % b); } n long long ans = 0; n void dfs(int v, int fa) { n   for (auto it : p[fa]) { n     long long t = gcd(a[v], it.first); n     p[v][t] += it.second; n     ans = (ans + t % mod * it.second % mod) % mod; n   } n   p[v][a[v]] += 1; n   ans = (ans + a[v]) % mod; n   for (int i = 0; i < G[v].size(); i++) { n     int to = G[v][i]; n     if (to == fa) continue; n     dfs(to, v); n   } n } n int main() { n   std::ios::sync_with_stdio(false); n   int N; n   cin >> N; n   for (int i = 1; i <= N; i++) cin >> a[i]; n   for (int i = 1; i < N; i++) { n     int x; n     int y; n     cin >> x >> y; n     G[x].push_back(y); n     G[y].push_back(x); n   } n   dfs(1, 0); n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, m; n   cin >> n >> m; n   string str[n]; n   for (int i = 0; i < n; i++) cin >> str[i]; n   int brr[m]; n   for (int i = 0; i < m; i++) cin >> brr[i]; n   int count = 0; n   for (int i = 0; i < m; i++) { n     int arr[5] = {0}; n     for (int j = 0; j < n; j++) { n       arr[str[j][i] -  A ]++; n     } n     int k = *max_element(arr, arr + 5); n     count += k * brr[i]; n   } n   cout << count; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   double n, a, d; n   cin >> n >> a >> d; n   vector<int> t; n   vector<int> v; n   double prev = 0; n   int bit = 1; n   for (int i = 0; i < n; i++) { n     double t1, v1; n     cin >> t1 >> v1; n     double t2 = 0, t3 = 0, tot = 0; n     double dis = 0; n     t2 = (double)(v1) / double(a); n     dis = a * (t2) * (t2); n     dis = (float)dis / (float)2; n     if (dis >= d) { n       double velo = 2 * a * d; n       velo = sqrt(velo); n       t2 = velo / a; n       tot = t2 + (double)t1; n     } else { n       dis = d - dis; n       t3 = (double)dis / (double)v1; n       tot = (double)t2 + t3 + (double)t1; n     } n     if (tot > prev || bit == 1) { n       cout << setprecision(12) << tot << endl; n       prev = tot; n       bit = 0; n     } else { n       cout << setprecision(12) << prev << endl; n       bit = 0; n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n class Mod { n  public: n   int mod; n   Mod(int mod_ = 1e9 + 7) : mod(mod_) {} n   int add(int a, int b) { return (a + b) % mod; } n   int mul(long long a, int b) { return (a * b) % mod; } n   int pow(long long a, int x) { n     int ans = 1; n     while (x) { n       if (x & 1) { n         ans = (ans * a) % mod; n       } n       x >>= 1; n       a = (a * a) % mod; n     } n     return ans; n   } n }; n class IO { n  public: n   static void read(int &val) { scanf( %d , &val); } n   static void read(long long &val) { scanf( %lld , &val); } n   static void read(char *val) { scanf( %s , val); } n   static void read(string &val) { n     const int BUFFER_SIZE = 1e5 + 5; n     static char buffer[BUFFER_SIZE]; n     scanf( %s , buffer); n     val = buffer; n   } n   static void read_n(vector<int> &val, int n) { n     val.resize(n); n     for (int i = 0; i < n; i++) { n       scanf( %d , &val[i]); n     } n   } n   static void write(int val) { printf( %d n , val); } n   static void write(long long val) { printf( %lld n , val); } n   static void write(const char *val) { printf( %s n , val); } n   static void write(const string &val) { printf( %s n , val.c_str()); } n   static void write(const vector<int> &val) { n     for (int i = 0; i < val.size(); i++) { n       if (i > 0) { n         putchar(   ); n       } n       printf( %d , val[i]); n     } n     putchar(  n ); n   } n }; n long long gcd_ex(long long a, long long b, long long &x, long long &y) { n   if (b == 0) { n     x = 1; n     y = 0; n     return a; n   } n   long long ans = gcd_ex(b, a % b, y, x); n   y -= (a / b) * x; n   return ans; n } n class Solver { n   long long x; n   stringstream ans; n   int cnt_ans = 0; n   void print(long long x, long long a) { n     long long cur = 0; n     while (a) { n       if (a & 1) { n         if (cur > 0) { n           ans << cur <<   +   << x << endl; n           cnt_ans++; n         } n         cur += x; n       } n       a >>= 1; n       ans << x <<   +   << x << endl; n       cnt_ans++; n       x <<= 1; n     } n   } n  n  public: n   void read() { IO::read(x); } n   void solve() { n     long long e = 1; n     while ((e << 1) <= x) { n       e <<= 1; n     } n     print(x, e); n     ans << e * x <<   ^   << x << endl; n     cnt_ans++; n     long long y = ((e * x) ^ x); n     long long a, b; n     gcd_ex(x, y, a, b); n     if (a < 0) { n       swap(a, b); n       swap(x, y); n     } n     b = -b; n     if (!(a & 1)) { n       a += y; n       b += x; n     } n     print(x, a); n     print(y, b); n     ans << a * x <<   ^   << b * y << endl; n     cnt_ans++; n     IO::write(cnt_ans); n     IO::write(ans.str()); n   } n }; n void init() { ios::sync_with_stdio(false); } n int main() { n   init(); n   int t = 1; n   while (t--) { n     Solver solver; n     solver.read(); n     solver.solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e5 + 1; n map<int, int> m[N]; n set<pair<int, int> > s[N]; n vector<int> g[N]; n int n, c[N]; n long long ans[N]; n void dfs(int u, int p) { n   m[u][c[u]] = 1; n   s[u].insert(pair<int, int>(1, c[u])); n   ans[u] = c[u]; n   for (int v : g[u]) { n     if (v == p) continue; n     dfs(v, u); n     if (m[u].size() < m[v].size()) { n       m[u].swap(m[v]); n       s[u].swap(s[v]); n       ans[u] = ans[v]; n     } n   } n   for (int v : g[u]) { n     if (v == p) continue; n     for (auto x : m[v]) { n       s[u].erase(pair<int, int>(m[u][x.first], x.first)); n       m[u][x.first] += x.second; n       if (s[u].empty() || m[u][x.first] > (*--s[u].end()).first) n         ans[u] = x.first; n       else if (m[u][x.first] == (*--s[u].end()).first) n         ans[u] += x.first; n       s[u].insert(pair<int, int>(m[u][x.first], x.first)); n     } n   } n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   cin >> n; n   for (int i = 1; i <= n; ++i) cin >> c[i]; n   for (int i = 1; i < n; ++i) { n     int x, y; n     cin >> x >> y; n     g[x].push_back(y); n     g[y].push_back(x); n   } n   dfs(1, 0); n   for (int i = 1; i <= n; ++i) cout << ans[i] <<    ; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 5e5 + 5; n long long INF = 1e18; n int n, s; n vector<int> ans; n long long ansV = 0; n long long dis[N]; n int edge[N], val[N]; n vector<pair<int, int> > adj[N]; n void solve() { n   for (int i = 1; i <= n; i++) dis[i] = INF; n   priority_queue<pair<long long, int>, vector<pair<long long, int> >, n                  greater<pair<long long, int> > > n       pq; n   dis[s] = 0; n   pq.push({dis[s], s}); n   while (!pq.empty()) { n     int u = pq.top().second; n     long long Cost = pq.top().first; n     pq.pop(); n     if (Cost > dis[u]) continue; n     if (u != s) { n       ans.push_back(edge[u]); n       ansV += val[edge[u]]; n     } n     for (auto P : adj[u]) { n       int v = P.first, idx = P.second; n       if (Cost + val[idx] < dis[v]) { n         dis[v] = Cost + val[idx]; n         edge[v] = idx; n         pq.push({dis[v], v}); n       } else if (Cost + val[idx] == dis[v] && val[idx] < val[edge[v]]) n         edge[v] = idx; n     } n   } n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   int m; n   cin >> n >> m; n   for (int i = 0; i < m; i++) { n     int u, v; n     cin >> u >> v >> val[i]; n     adj[u].push_back({v, i}); n     adj[v].push_back({u, i}); n   } n   cin >> s; n   solve(); n   sort(ans.begin(), ans.end()); n   cout << ansV << endl; n   for (auto v : ans) cout << v + 1 <<    ; n   cout << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long double PI = acosl(-1.0); n const double EPS = 1e-12; n void showBit(int no, int N) { n   for (int i = N - 1; i >= 0; i--) n     if ((no >> i) & 1) n       printf( 1 ); n     else n       printf( 0 ); n   printf(  n ); n } n int binToDec(string s) { n   int ln = s.length(); n   int sum = 0; n   for (int i = ln - 1, k = 0; i >= 0; i--, k++) n     sum = sum + (s[i] -  0 ) * (1 << k); n   return sum; n } n void combination(long long com[][1005]) { n   com[0][0] = 1; n   int sz = 1000; n   for (int i = 1; i <= sz; i++) { n     com[i][0] = 1; n     for (int j = 1; j <= i; j++) { n       com[i][j] = com[i - 1][j] + com[i - 1][j - 1]; n       com[i][j] = com[i][j] % 1000000007; n     } n   } n } n int find_par(int cn) {} n int gcd(int u, int v) { n   if (v == 0) return u; n   return gcd(v, u % v); n } n void insert_ind(int cs, int ci, int s, int e) { n   if (s == e) { n     return; n   } n   int mid = (s + e) / 2; n   if (ci <= mid) n     insert_ind(2 * cs, ci, s, mid); n   else n     insert_ind(2 * cs + 1, ci, mid + 1, e); n } n void update_ind(int cs, int ci, int s, int e, int val) { n   if (s == e) { n     return; n   } n   int mid = (s + e) / 2; n   if (ci <= mid) n     update_ind(2 * cs, ci, s, mid, val); n   else n     update_ind(2 * cs + 1, ci, mid + 1, e, val); n } n void update_seg(int cs, int ds, int de, int s, int e, int val) { n   if (ds == s && de == e) { n     return; n   } n   int mid = (s + e) / 2; n   if (ds <= mid) update_seg(2 * cs, ds, min(mid, de), s, mid, val); n   if (de > mid) update_seg(2 * cs + 1, max(mid + 1, ds), de, mid + 1, e, val); n } n void pattern_match(char pat[], const int len, int pi[]) { n   pi[1] = 0; n   for (int i = 2; i <= len; i++) { n     int st = i - 1; n     while (st >= 0 && pat[pi[st] + 1] != pat[i]) st = pi[st]; n     if (st == 0) n       pi[i] = 0; n     else n       pi[i] = pi[st] + 1; n   } n } n void pattern_match(char pat[], int pi[], char tar[], const int len1, int gi[]) { n   if (pat[0] == tar[0]) gi[0] = 1; n   for (int i = 1; i < len1; i++) { n     int st = gi[i - 1]; n     while (pat[st] != tar[i]) { n       if (st == 0) { n         st = -1; n         break; n       } n       st = pi[st - 1]; n     } n     if (st == -1) n       gi[i] = 0; n     else n       gi[i] = st + 1; n   } n } n struct myArray { n   int mat[10][10]; n }; n myArray matrix_mul(myArray mat1, myArray mat2, const int sz) { n   int tmp[sz][sz]; n   memset((tmp), 0, sizeof((tmp))); n   for (int r = 0; r < sz; r++) n     for (int c = 0; c < sz; c++) n       for (int cr = 0; cr < sz; cr++) n         tmp[r][c] += mat1.mat[r][cr] * mat2.mat[c][cr]; n   myArray res; n   for (int r = 0; r < sz; r++) n     for (int c = 0; c < sz; c++) res.mat[r][c] = tmp[r][c]; n   return res; n } n myArray unit_matrix(const int sz) { n   myArray ma; n   memset((ma.mat), 0, sizeof((ma.mat))); n   for (int i = 0; i < sz; i++) ma.mat[i][i] = 1; n   return ma; n } n myArray matrix_expo(int p, myArray mat, const int sz) { n   if (p == 0) { n     return unit_matrix(sz); n   } n   if (p & 1) return matrix_mul(mat, matrix_expo(p - 1, mat, sz), sz); n   myArray tmp = matrix_expo(p >> 1, mat, sz); n   return matrix_mul(tmp, tmp, sz); n } n int X[] = {0, 0, 1, -1}; n int Y[] = {1, -1, 0, 0}; n map<string, int> msi; n void cal_state(string s, int tran[][30]) { n   int ln = s.length(); n   string s1 = s; n   for (int i = 0; i < ln; i++) { n     msi[s1] = ln - i; n     s1 = s1.substr(0, ln - i - 1); n   } n   s1 =   ; n   for (int i = 0; i <= ln; i++) { n     string ts = s1; n     for (int j = 0; j < 26; j++) { n       s1 = ts + (char)( a  + j); n       int tln = i + 1; n       while (msi[s1] == 0) { n         s1 = s1.substr(1); n         tln--; n         if (tln == 0) break; n       } n       if (tln == 0) s1 =   ; n       tran[i][j] = msi[s1]; n     } n     if (i < ln) s1 = s.substr(0, i + 1); n   } n } n string s, b; n int tran[105][30]; n int n, k; n bool col[105]; n bool dp[105][105][105]; n int c[105][105][105], ps[105][105][105]; n int prp[105][105][105]; n void print(int ln, int cs, int rp) { n   if (ln == 0) return; n   print(ln - 1, ps[ln][cs][rp], prp[ln][cs][rp]); n   printf( %c ,  a  + c[ln][cs][rp]); n } n int main() { n   cin >> n >> k; n   cin >> s; n   cal_state(s, tran); n   cin >> b; n   int ln = b.length(); n   int cnt = 0; n   for (int i = (0); i < (ln); ++i) n     if (b[i] ==  1 ) col[i] = true, cnt++; n   dp[0][0][0] = true; n   ln = s.length(); n   for (int i = 0; i < n; i++) n     for (int j = 0; j <= n; j++) n       for (int rp = 0; rp <= cnt; rp++) n         if (dp[i][j][rp]) { n           for (int ne = 0; ne < k; ne++) { n             int ns = tran[j][ne]; n             if (ns == ln) { n               if (col[i - ln + 1] && rp + 1 <= cnt) { n                 dp[i + 1][ns][rp + 1] = true; n                 c[i + 1][ns][rp + 1] = ne; n                 ps[i + 1][ns][rp + 1] = j; n                 prp[i + 1][ns][rp + 1] = rp; n               } n             } else { n               dp[i + 1][ns][rp] = true; n               c[i + 1][ns][rp] = ne; n               ps[i + 1][ns][rp] = j; n               prp[i + 1][ns][rp] = rp; n             } n           } n         } n   for (int i = 0; i <= n; i++) n     if (dp[n][i][cnt] == true) { n       print(n, i, cnt); n       printf(  n ); n       return 0; n     } n   printf( No solution n ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<pair<int, int> > result; n void move(pair<int, int> &from, pair<int, int> &to) { n   from.first -= to.first; n   to.first *= 2; n   result.push_back(make_pair(to.second, from.second)); n } n void removeone(pair<int, int> list[3]) { n   sort(list, list + 3); n   if (list[0].first == 0) { n     swap(list[0], list[2]); n     return; n   } n   int q = list[1].first / list[0].first; n   for (int x = 1; q; x <<= 1) n     if (q & x) { n       move(list[1], list[0]); n       q -= x; n     } else n       move(list[2], list[0]); n   removeone(list); n } n int main(int argc, char **argv) { n   int n; n   vector<pair<int, int> > nums; n   scanf( %d , &n); n   for (int i = 0; i < n; i++) { n     int v; n     scanf( %d , &v); n     if (v) nums.push_back(make_pair(v, i)); n   } n   if (nums.size() < 2) { n     printf( -1 n ); n     return 0; n   } n   result.clear(); n   while (nums.size() > 2) { n     removeone(&nums[nums.size() - 3]); n     nums.pop_back(); n   } n   printf( %d n , (int)result.size()); n   for (int i = 0; i < (int)result.size(); i++) n     printf( %d %d n , result[i].first + 1, result[i].second + 1); n   return 0; n } n


#include <bits/stdc++.h> n #pragma GCC optimize( O3 ) n long long x, y, z, a, b, c, d, n, m, mod = 1000000007; n using namespace std; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   ; n   string s; n   bool w = true; n   cin >> n >> m >> s; n   a = s.find( G ); n   b = s.find( T ); n   if (a > b && (a - b) % m == 0) { n     for (int i = a; i > b; i -= m) { n       if (s[i] ==  # ) { n         w = false; n         break; n       } n     } n   } else if (a < b && (b - a) % m == 0) { n     for (int i = a; i < b; i += m) { n       if (s[i] ==  # ) { n         w = false; n         break; n       } n     } n   } n   if (w == false || abs(b - a) % m != 0) n     cout <<  NO ; n   else n     cout <<  YES ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   std::ios::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL); n   string a, b; n   int k; n   cin >> a >> b >> k; n   int n = a.length(), l = 0; n   long long gd = 0, bd = 0; n   int f = 0; n   if (a.length() != b.length()) { n     cout << 0; n     return 0; n   } n   for (int i = 0; i < n; i++) { n     int j; n     for (j = 0; j < n; j++) n       if (a[(i + j) % n] != a[j]) break; n     if (j == n) n       gd++; n     else n       bd++; n     for (j = 0; j < n; j++) n       if (a[(i + j) % n] != b[j]) break; n     if (j == n) f = 1; n   } n   if (f == 0) { n     cout << 0; n     return 0; n   } n   vector<int> dp(2, 0), dp1(2, 0); n   if (a == b) n     dp[0] = 1; n   else n     dp1[0] = 1; n   long long mod = 1e9 + 7; n   for (int i = 1; i <= k; i++) { n     long long x = dp[1 - i % 2] * (gd - 1) + dp1[1 - i % 2] * gd; n     x %= mod; n     dp[i % 2] = x; n     x = dp[1 - i % 2] * bd + dp1[1 - i % 2] * (bd - 1); n     x %= mod; n     dp1[i % 2] = x; n   } n   cout << dp[k % 2] <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 2e5 + 5; n int gcd(int a, int b) { return b ? gcd(b, a % b) : a; } n void slove() { n   int n, m, k; n   cin >> n >> m >> k; n   int ls = 0; n   if (n % 2) { n     n--, ls += m / 2; n   } n   if (m % 2) m--; n   int flag = 1; n   if (ls > k) flag = 0; n   ls += (n * m) / 2; n   if (ls < k) flag = 0; n   if (ls % 2 != k % 2) flag = 0; n   if (flag) n     puts( YES ); n   else n     puts( NO ); n } n int main() { n   int T; n   scanf( %d , &T); n   while (T--) { n     slove(); n   } n   return 0; n } n


#include <bits/stdc++.h> n template <class T, class F, class C> n auto binary_search(T l, T r, F func, C cmp) -> T; n auto division(int64_t n, int64_t d) -> int64_t { return n / d; } n int main() { n   int64_t n, k, m, d; n   std::cin >> n >> k >> m >> d; n   int64_t answer = 0; n   for (int64_t q_k = 1; q_k <= d; ++q_k) { n     int64_t x_max = binary_search( n         int64_t(1), m + 1, n         [n, q_k, k](int64_t x) { return (division(n, x) - 1) / k + 1 >= q_k; }, n         [](int64_t l, int64_t r) { return l + 1 >= r; }); n     if ((division(n, x_max) - 1) / k + 1 != q_k) { n       continue; n     } n     answer = std::max(answer, x_max * q_k); n   } n   std::cout << answer <<   n ; n   return 0; n } n template <class T, class F, class C> n auto binary_search(T l, T r, F func, C cmp) -> T { n   bool dir = func(l); n   for (auto mid = (l + r) / 2; !cmp(l, r); mid = (l + r) / 2) { n     (func(mid) == dir ? l : r) = mid; n   } n   return l; n } n


#include <algorithm> n #include <cmath> n #include <vector> n #include <iostream> n using namespace std; n  n void solve() { n     int n, totalCost, prev, x, cost; n     totalCost = 0; n     cin >> n; n  n     if (n <= 1) { n         cout << 0 <<   n ; n         return; n     } n  n     cin >> prev; n  n     int m, M; n  n     for (int i = 1; i < n; ++i) { n         cin >> x; n  n         if (x < prev) { n             m = x; n             M = prev; n         } else { n             m = prev; n             M = x; n         } n  n         if (m * 2 < M) { n             cost = int(ceil(log2(float(M) / float(m)))) - 1; n            // cout <<  ->  << m <<     << M <<     << cost <<   n ; n             totalCost += cost; n         } n  n         prev = x; n     } n  n     cout << totalCost <<   n ; n } n  n int main() { n     ios_base::sync_with_stdio(false); n     cin.tie(nullptr); n  n     int T; n     cin >> T; n  n     for (int t = 0; t < T; ++t) { n         solve(); n     } n  n     return 0; n }


#include <bits/stdc++.h> n using namespace std; n const int mod = 1000000007; n char s[105]; n int main() { n   int n, nr = 0; n   gets(s); n   n = strlen(s); n   int x = 0; n   for (int i = 0; i < n; i++) x = (x * 2 + s[i] -  0 ) % mod; n   for (int i = 1; i < n; i++) x = (x * 2) % mod; n   printf( %d n , x); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, m, b, mod; n   cin >> n >> m >> b >> mod; n   int dp[502][502]; n   int bugs[n + 2]; n   for (int i = 0; i < n; i++) cin >> bugs[i]; n   memset(dp, 0, sizeof dp); n   dp[0][0] = 1; n   for (int k = 0; k < n; k++) { n     for (int j = 0; j <= b - bugs[k]; j++) { n       for (int i = 0; i < m; i++) { n         dp[i + 1][j + bugs[k]] += dp[i][j]; n         dp[i + 1][j + bugs[k]] %= mod; n       } n     } n   } n   int ans = 0; n   for (int i = 0; i <= b; i++) { n     ans += dp[m][i]; n     ans %= mod; n   } n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n mt19937_64 rang( n     chrono::high_resolution_clock::now().time_since_epoch().count()); n int rng(int lim) { n   uniform_int_distribution<int> uid(0, lim - 1); n   return uid(rang); n } n const int INF = 0x3f3f3f3f; n const int mod = 1e9 + 7; n void solve() { n   int sum, limit; n   cin >> sum >> limit; n   vector<int> res; n   for (int i = limit; i >= 1; i--) { n     int lowbit = i ^ (i & (i - 1)); n     if (sum >= lowbit) res.push_back(i), sum -= lowbit; n     if (sum == 0) break; n   } n   if (sum != 0) n     cout << -1; n   else { n     cout << res.size() <<   n ; n     for (auto x : res) cout << x <<    ; n   } n } n int main() { n   ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr); n   srand(chrono::high_resolution_clock::now().time_since_epoch().count()); n   int t = 1; n   while (t--) { n     solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = (int)4e6 + 11; n const long long inf = 1e9 + 7; n int n, m, p[1 << 20], can[1 << 20]; n int find_parent(int x) { n   if (x == p[x]) { n     return x; n   } n   return p[x] = find_parent(p[x]); n } n int main() { n   scanf( %d %d , &n, &m); n   for (int i = 0; i < 1 + n; ++i) { n     p[i] = i; n   } n   memset(can, -1, sizeof(can)); n   for (int i = 0; i < m; ++i) { n     int l, r, x; n     scanf( %d %d %d , &l, &r, &x); n     --x; n     l = find_parent(l - 1); n     while (l < r) { n       if (l != x) { n         can[l] = x; n         p[l] = 1 + l; n       } n       l = find_parent(1 + l); n     } n   } n   for (int i = 0; i < n; ++i) { n     printf( %d  , can[i] + 1); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 110, alpha = 26; n int n, m; n int dp[maxn][maxn][alpha]; n bool mark[maxn]; n vector<int> ts; n vector<pair<int, int> > G[maxn]; n void dfs(int a) { n   mark[a] = 1; n   for (pair<int, int> p : G[a]) { n     int b = p.first; n     if (!mark[b]) { n       dfs(b); n     } n   } n   ts.push_back(a); n } n int32_t main() { n   cin >> n >> m; n   for (int i = int(0); i < int(m); i++) { n     int a, b; n     char c; n     cin >> a >> b >> c; n     a--, b--; n     G[a].push_back({b, c -  a }); n   } n   for (int i = int(0); i < int(n); i++) { n     if (!mark[i]) { n       dfs(i); n     } n   } n   for (int size = int(0); size < int(2 * n - 1); size++) { n     for (int i = int(0); i < int(min(size + 1, n)); i++) { n       int j = size - i; n       if (j >= n) continue; n       int a = ts[i], b = ts[j]; n       for (int x = int(0); x < int(alpha); x++) { n         for (pair<int, int> p : G[a]) { n           int ap = p.first, c = p.second; n           if (c >= x) dp[a][b][x] = max(dp[a][b][x], 1 - dp[b][ap][c]); n         } n       } n     } n   } n   for (int i = int(0); i < int(n); i++) { n     for (int j = int(0); j < int(n); j++) { n       cout << (dp[i][j][0] ?  A  :  B ); n     } n     cout << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int a[105]; n int main() { n   int n, i, f; n   cin >> n; n   for (i = 0; i < n; i++) cin >> a[i]; n   sort(a, a + n); n   f = 0; n   for (i = 1; i < n; i++) { n     if (a[i] != a[0]) { n       f = 1; n       break; n     } n   } n   if (f == 1) n     cout << a[i]; n   else n     cout <<  NO ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 110; n string s[maxn]; n int cur[maxn], c[maxn]; n int n, m, ans = 0; n void dfs(int x) { n   if (x == n) { n     int flag = 1; n     for (int i = 1; i <= m; i++) n       if (cur[i] - c[i]) flag = 0; n     ans += flag; n     return; n   } n   for (char v =  0 ; v <=  1 ; v++) { n     int can = 1; n     for (int i = 1; i <= m; i++) { n       cur[i] += (v == s[i][x]); n       if (cur[i] > c[i]) can = 0; n     } n     if (can) dfs(x + 1); n     for (int i = 1; i <= m; i++) cur[i] -= (v == s[i][x]); n   } n } n int main() { n   cin >> n >> m; n   for (int i = 1; i <= m; i++) cin >> s[i] >> c[i]; n   memset(cur, 0, sizeof(cur)); n   dfs(0); n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct Hint { n   int up, cnt; n   bool operator<(Hint o) const { return up < o.up; } n }; n int n, b, q; n Hint h[10013]; n int has[10013][5], take[10013]; n int main() { n   scanf( %d%d%d , &n, &b, &q); n   for (int i = 1; i <= q; i++) { n     scanf( %d%d , &h[i].up, &h[i].cnt); n   } n   h[++q] = {b, n}; n   sort(h, h + q + 1); n   for (int i = 1; i <= q; i++) { n     for (int j = h[i - 1].up + 1; j <= h[i].up; j++) has[i][j % 5] += 1; n     take[i] = h[i].cnt - h[i - 1].cnt; n     if (0 > take[i] || take[i] > h[i].up - h[i - 1].up) n       return printf( unfair n ), 0; n   } n   for (int i = 1; i < 32; i++) { n     int need = n / 5 * __builtin_popcount(i); n     int can = 0; n     for (int k = 1; k <= q; k++) { n       int cur = 0; n       for (int j = 0; j < 5; j++) n         if (i & (1 << j)) { n           cur += has[k][j]; n         } n       can += min(cur, take[k]); n     } n     if (need > can) return printf( unfair n ), 0; n   } n   printf( fair n ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 60; n map<double, int> p; n bitset<N * N> g[N * N + 5]; n int n, m; n double ya[N + 5], yb[N + 5]; n int main() { n   scanf( %d%d , &n, &m); n   for (int i = 1; i <= n; i++) scanf( %lf , &ya[i]); n   for (int i = 1; i <= m; i++) scanf( %lf , &yb[i]); n   int tot = 0; n   for (int i = 1; i <= n; i++) { n     for (int j = 1; j <= m; j++) { n       double now = (ya[i] + yb[j]) / 2; n       if (p.find(now) == p.end()) tot++, p[now] = tot; n       g[p[now]].set(i, 1); n       g[p[now]].set(j + n, 1); n     } n   } n   int ans = -1; n   for (int i = 1; i <= tot; i++) { n     for (int j = i; j <= tot; j++) { n       bitset<N * N> s; n       s = g[i] | g[j]; n       ans = max(ans, (int)s.count()); n     } n   } n   printf( %d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e6 + 100; n int ans; n int main() { n   ios_base::sync_with_stdio(false); n   int n; n   cin >> n; n   int a[1111] = {}, cnt[1111] = {}; n   for (int i = 1; i <= n; i++) { n     cin >> a[i]; n   } n   sort(a + 1, a + n + 1); n   for (int i = 1; i <= n; i++) { n     if (cnt[i] == 0) { n       ans++; n       for (int j = i + 1; j <= n; j++) { n         if (a[j] % a[i] == 0) { n           cnt[j] = 1; n         } n       } n     } n   } n   cout << ans; n   return 0; 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 n, k; n     cin >> n; n     vector<long long> dp(n + 3, 0); n     vector<long long> v; n     long long sum = 0; n     for (int j = 0; j < n; j++) { n       cin >> k; n       if (j % 2 == 0) { n         sum = sum + k; n       } n       v.push_back(k); n     } n     dp[0] = 0; n     long long ans = 0; n     long long z = 0; n     if (n > 1) { n       dp[1] = max(z, v[1] - v[0]); n       ans = max(ans, dp[1]); n     } n     k = 2; n     for (int j = 1; j < n - 1; j++) { n       if (j % 2 == 0) { n         dp[k] = max(z, dp[k - 2] + (v[j + 1] - v[j])); n       } else { n         dp[k] = max(z, dp[k - 2] + (v[j] - v[j + 1])); n       } n       ans = max(ans, dp[k]); n       k++; n     } n     cout << sum + ans << endl; n   } n } n


#include <stdio.h> n  n int main() { n     int t; n     scanf( %d , &t); n     while (t--) { n         int x, i; n         scanf( %d , &x); n         for (i = 1; i * (i + 1) / 2 < x; i++); n         if (i * (i + 1) / 2 - x != 1) { n             printf( %d n , i); n         } else { n             printf( %d n , i + 1); n         } n     } n }


#include <bits/stdc++.h> n template <typename Y> n inline bool updmin(Y &a, Y b) { n   if (a > b) { n     a = b; n     return 1; n   } n   return 0; n } n template <typename Y> n inline bool updmax(Y &a, Y b) { n   if (a < b) { n     a = b; n     return 1; n   } n   return 0; n } n template <typename Y> n inline Y abs(Y a) { n   if (a < 0) a = -a; n   return a; n } n template <typename Y> n inline Y sqr(Y a) { n   return a * a; n } n int read() { n   int w = 1, q = 0, ch =    ; n   for (; ch <  0  || ch >  9 ; ch = getchar()) n     if (ch ==  - ) w = -1; n   for (; ch >=  0  && ch <=  9 ; ch = getchar()) q = q * 10 + ch - 48; n   return q * w; n } n inline void FileIO() { n   freopen( n          n        .in , n        r , stdin); n   freopen( n          n        .out , n        w , stdout); n } n int n, m, h; n const int N = 100020; n std::vector<int> G[N], s; n int tim = 0, cnt = 0; n int low[N], dfn[N], vis[N], bel[N]; n void tarjan(int p) { n   vis[p] = 1; n   s.push_back(p); n   low[p] = dfn[p] = ++tim; n   for (int t : G[p]) { n     if (!dfn[t]) { n       tarjan(t); n       updmin(low[p], low[t]); n     } else if (vis[t]) { n       updmin(low[p], dfn[t]); n     } n   } n   if (low[p] == dfn[p]) { n     ++cnt; n     while (s.back() != p) { n       int cur = s.back(); n       s.pop_back(); n       vis[cur] = 0; n       bel[cur] = cnt; n     } n     bel[p] = cnt; n     vis[p] = 0; n     s.pop_back(); n   } n } n int u[N], size[N], out[N]; n int main() { n   n = read(); n   m = read(); n   h = read(); n   for (int i = 1; i <= n; i++) { n     u[i] = read(); n   } n   for (int i = 1; i <= m; i++) { n     int x = read(), y = read(); n     if ((u[x] + 1) % h == u[y]) { n       G[x].push_back(y); n     } n     if ((u[y] + 1) % h == u[x]) { n       G[y].push_back(x); n     } n   } n   for (int i = 1; i <= n; i++) { n     if (!dfn[i]) { n       tarjan(i); n     } n   } n   for (int i = 1; i <= n; i++) { n     size[bel[i]]++; n     for (int j : G[i]) { n       if (bel[i] != bel[j]) { n         out[bel[i]]++; n       } n     } n   } n   int ans = n + 1, pos = 0; n   for (int i = 1; i <= cnt; i++) { n     if (!out[i] && updmin(ans, size[i])) { n       pos = i; n     } n   } n   printf( %d n , ans); n   for (int i = 1; i <= n; i++) { n     if (bel[i] == pos) { n       printf( %d  , i); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   string s; n   cin >> s; n   int n = s.size(); n   string r; n   int i = 0; n   while (i < n) { n     if (n - i >= 3) { n       if (s.substr(i, 3) ==  WUB ) { n         if (r.size() && r.back() !=    ) r +=    ; n         i += 3; n       } else n         r += s[i++]; n     } else { n       r += s[i++]; n     } n   } n   cout << r << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long ara[12]; n int main() { n   long long n, i, j, k; n   for (i = 0; i < 4; i++) { n     cin >> k; n     ara[i] = k; n   } n   if (ara[0] == 0 && ara[3] == 0 && ara[2] > 0) { n     printf( 0 n ); n   } else { n     if (ara[0] == ara[3]) n       printf( 1 n ); n     else n       printf( 0 n ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n bool isvowel(char c) { n   return c ==  a  || c ==  e  || c ==  i  || c ==  o  || c ==  u ; n } n int main() { n   char arr[110]; n   scanf( %s , arr); n   int len = strlen(arr); n   bool check = true; n   for (int i = 0; i < len; i++) { n     if (isvowel(arr[i])) { n     } else if (arr[i] ==  n ) { n     } else { n       if (i == len - 1 || !isvowel(arr[i + 1])) check = false; n     } n   } n   puts(check ?  YES  :  NO ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long int power(long long int a, long long int x) { n   long long int res = 1; n   while (x > 0) { n     if (x & 1) { n       res *= a; n     } n     a *= a; n     x = x / 2; n   } n   return res; n } n int main() { n   long long int n; n   scanf( %lld , &n); n   long long int ans = 0; n   ans = power(4, n - 2) * 6; n   if (n > 3) { n     ans += (n - 3) * 4 * 9 * power(4, n - 4); n   } n   printf( %lld n , ans); n } n


#include <bits/stdc++.h> n using namespace std; n const int A = 1e5 + 11; n const int B = 1e6 + 11; n const int mod = 1e9 + 7; n const int inf = 0x3f3f3f3f; n inline int read() { n   char c = getchar(); n   int x = 0, f = 1; n   for (; !isdigit(c); c = getchar()) n     if (c ==  - ) f = -1; n   for (; isdigit(c); c = getchar()) x = x * 10 + (c ^ 48); n   return x * f; n } n struct edgexx { n   int v, nxt, w; n } edge[B]; n struct edgex { n   int v, nxt; n } edge_[B]; n struct node { n   long long d; n   int id; n   bool operator<(const node &a) const { return d > a.d; } n }; n struct xxs { n   int u, v; n   long long w; n   bool operator<(const xxs &e) const { return w < e.w; } n } e[B]; n int n, cnt, js, k, c, t, head[B], m, q, head2[B], fa[B], u_fa[B]; n long long dis[B], wnd[B]; n int sz[B], son[B], top[B], dep[B]; n priority_queue<node> pq; n void add1(int u, int v, int w) { n   edge[++cnt].nxt = head[u]; n   edge[cnt].v = v; n   edge[cnt].w = w; n   head[u] = cnt; n } n void add2(int u, int v) { n   edge_[++js].nxt = head2[u]; n   edge_[js].v = v; n   head2[u] = js; n } n int find(int x) { n   if (x == u_fa[x]) return x; n   return u_fa[x] = find(u_fa[x]); n } n void dfs1(int u, int f) { n   sz[u] = 1; n   dep[u] = dep[fa[u] = f] + 1; n   for (int i = head2[u]; i; i = edge_[i].nxt) { n     int v = edge_[i].v; n     if (v == f) continue; n     dfs1(v, u); n     sz[u] += sz[v]; n     if (sz[v] > sz[son[u]]) son[u] = v; n   } n } n void dfs2(int u, int topf) { n   top[u] = topf; n   if (son[u]) dfs2(son[u], topf); n   for (int i = head2[u]; i; i = edge_[i].nxt) { n     int v = edge_[i].v; n     if (v == fa[u] || v == son[u]) continue; n     dfs2(v, v); n   } n } n int lca(int u, int v) { n   while (top[u] != top[v]) { n     if (dep[top[u]] < dep[top[v]]) swap(u, v); n     u = fa[top[u]]; n   } n   return dep[u] < dep[v] ? u : v; n } n int main() { n   cin >> n >> m >> k >> q; n   for (int i = 1; i <= m; i++) { n     int x = read(), y = read(), z = read(); n     add1(x, y, z); n     add1(y, x, z); n     e[i] = (xxs){x, y, z}; n   } n   memset(dis, 0x3f, sizeof(dis)); n   for (int i = 1; i <= k; i++) pq.push((node){dis[i] = 0, i}); n   while (!pq.empty()) { n     long long d = pq.top().d; n     int u = pq.top().id; n     pq.pop(); n     if (d != dis[u]) continue; n     for (int i = head[u]; i; i = edge[i].nxt) { n       int v = edge[i].v; n       if (dis[v] > d + edge[i].w) pq.push((node){dis[v] = d + edge[i].w, v}); n     } n   } n   for (int i = 1; i <= m; i++) e[i].w += dis[e[i].u] + dis[e[i].v]; n   sort(e + 1, e + 1 + m); n   for (int i = 1; i <= 2 * n; i++) u_fa[i] = i; n   cnt = n; n   for (int i = 1; i <= m; i++) { n     int u = e[i].u, v = e[i].v; n     long long w = e[i].w; n     u = find(u); n     v = find(v); n     if (u == v) continue; n     u_fa[u] = u_fa[v] = ++cnt; n     wnd[cnt] = w; n     add2(cnt, u); n     add2(cnt, v); n   } n   dfs1(cnt, 0); n   dfs2(cnt, cnt); n   while (q--) { n     int u = read(), v = read(); n     printf( %lld n , wnd[lca(u, v)]); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n map<int, int> mp; n vector<int> object, ans; n map<int, int> counter; n int gcd(int a, int b) { return a == 0 ? b : gcd(b % a, a); } n int main() { n   int n, num; n   scanf( %d , &n); n   for (int i = 0; i < n * n; i++) { n     scanf( %d , &num); n     object.push_back(num); n   } n   sort(object.begin(), object.end()); n   for (int i = object.size() - 1; i >= 0; i--) { n     if (counter[object[i]]) { n       counter[object[i]]--; n       continue; n     } n     for (int j = 0; j < ans.size(); j++) { n       int temp = gcd(ans[j], object[i]); n       counter[temp] += 2; n     } n     ans.push_back(object[i]); n   } n   for (int i = 0; i < n; i++) { n     if (i) printf(   ); n     printf( %d , ans[i]); n   } n   printf(  n ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 100005; n vector<long long> g[100005]; n long long vis[MAXN], s[MAXN], gc[MAXN]; n long long n; n void dfs(long long v) { n   vis[v] = 1; n   s[v] = 1; n   for (auto i : g[v]) { n     if (!vis[i]) { n       dfs(i); n       s[v] += s[i]; n       gc[v] = max(gc[v], s[i]); n     } n   } n   gc[v] = max(gc[v], n - s[v]); n } n int pa[MAXN], sz[MAXN]; n long long find_pa(long long pos) { n   if (pa[pos] != pos) pa[pos] = find_pa(pa[pos]); n   return pa[pos]; n } n void join(int a, int b) { n   int x = find_pa(a), y = find_pa(b); n   if (x != y) { n     if (sz[x] >= sz[y]) n       sz[x] += sz[y], pa[y] = x; n     else n       sz[y] += sz[x], pa[x] = y; n   } n } n void dsu_init(long long n) { n   for (long long i = 0, ggdem = n; i < ggdem; ++i) pa[i] = i, sz[i] = 1; n } n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   long long t; n   cin >> t; n   while (t--) { n     cin >> n; n     for (long long i = 0, ggdem = n - 1; i < ggdem; ++i) { n       long long u, v; n       cin >> u >> v; n       u--; n       v--; n       g[u].push_back(v); n       g[v].push_back(u); n     } n     dfs(0); n     long long mini = n; n     for (long long i = 0, ggdem = n; i < ggdem; ++i) { n       mini = min(mini, gc[i]); n     } n     vector<long long> v; n     for (long long i = 0, ggdem = n; i < ggdem; ++i) { n       if (gc[i] == mini) v.push_back(i); n     } n     assert(((int)v.size()) <= 2); n     sort(v.begin(), v.end()); n     if (((int)v.size()) == 1) { n       for (long long k = 0, ggdem = 2; k < ggdem; ++k) n         cout << 1 <<     << g[0][0] + 1 <<   n ; n     } else { n       assert(mini == n / 2 && n % 2 == 0); n       dsu_init(n); n       for (long long i = 0, ggdem = n; i < ggdem; ++i) { n         for (auto j : g[i]) { n           if (min(i, j) != v[0] || max(i, j) != v[1]) { n             join(i, j); n           } n         } n       } n       long long br = 0; n       for (long long i = 0, ggdem = n; i < ggdem; ++i) { n         if (((int)g[i].size()) == 1 && i != v[0] && n             find_pa(i) != find_pa(v[1])) { n           cout << i + 1 <<     << g[i][0] + 1 <<   n ; n           cout << i + 1 <<     << v[1] + 1 <<   n ; n           br++; n           break; n         } n       } n       assert(br); n     } n     for (long long i = 0, ggdem = n; i < ggdem; ++i) g[i].clear(); n     for (long long i = 0, ggdem = n; i < ggdem; ++i) n       vis[i] = 0, s[i] = 0, gc[i] = 0; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int a[1000005]; n queue<long long int> q; n vector<long long int> v; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   long long int i, j, k, n, m, mid; n   cin >> k; n   string s; n   cin >> s; n   n = s.size(); n   int co = 0; n   long long int sum = 0; n   if (k == 0) { n     for (i = 0; i < n; i++) { n       if (s[i] ==  0 ) { n         co++; n       } else n         co = 0; n       sum += (co); n     } n     cout << sum; n     return 0; n   } n   for (i = 0; i < n; i++) { n     if (s[i] ==  1 ) { n       a[i] = co; n       co = 0; n       v.push_back(i); n     } else n       co++; n   } n   long long int first = 0; n   long long int l = v.size(); n   for (i = k - 1; i < l; i++) { n     long long int temp1 = a[v[first]] + 1; n     long long int temp2; n     if (i + 1 < l) n       temp2 = v[i + 1] - v[i]; n     else n       temp2 = n - v[i]; n     sum += temp1 * temp2; n     first++; n   } n   cout << sum; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n inline int readint() { n   int x = 0; n   bool f = 0; n   char c = getchar(); n   while (!isdigit(c) && c !=  - ) c = getchar(); n   if (c ==  - ) { n     f = 1; n     c = getchar(); n   } n   while (isdigit(c)) { n     x = x * 10 + c -  0 ; n     c = getchar(); n   } n   return f ? -x : x; n } n const int maxn = 4e5 + 5; n int n, m, c[maxn]; n vector<int> g[maxn]; n int fa[maxn]; n void dfs(int u) { n   for (int i = 0; i < (int)g[u].size(); i++) { n     int v = g[u][i]; n     if (v == fa[u]) continue; n     fa[v] = u; n     dfs(v); n   } n } n struct lct { n   int fa[maxn], ch[maxn][2], s[maxn], s2[maxn]; n   long long ss[maxn]; n   void init() { n     for (int i = 1; i <= n; i++) s[i] = 1; n   } n   void pushup(int x) { n     s[x] = s2[x] + 1; n     if (ch[x][0]) s[x] += s[ch[x][0]]; n     if (ch[x][1]) s[x] += s[ch[x][1]]; n   } n   bool isroot(int x) { n     return !fa[x] || (ch[fa[x]][0] != x && ch[fa[x]][1] != x); n   } n   void rotate(int x) { n     int y = fa[x], z = fa[fa[x]]; n     if (!isroot(y)) ch[z][ch[z][1] == y] = x; n     bool d = ch[y][1] == x; n     int k = ch[x][!d]; n     if (k) fa[k] = y; n     ch[y][d] = k; n     ch[x][!d] = y; n     fa[y] = x; n     fa[x] = z; n     pushup(y); n     pushup(x); n   } n   void splay(int x) { n     while (!isroot(x)) { n       int y = fa[x], z = fa[fa[x]]; n       if (!isroot(y)) { n         if ((ch[y][1] == x) ^ (ch[z][1] == y)) n           rotate(x); n         else n           rotate(y); n       } n       rotate(x); n     } n   } n   void access(int x) { n     int y = 0; n     while (x) { n       splay(x); n       if (ch[x][1]) { n         s2[x] += s[ch[x][1]]; n         ss[x] += 1ll * s[ch[x][1]] * s[ch[x][1]]; n       } n       ch[x][1] = y; n       if (y) { n         s2[x] -= s[y]; n         ss[x] -= 1ll * s[y] * s[y]; n       } n       y = x; n       x = fa[x]; n     } n   } n   int findroot(int x) { n     access(x); n     splay(x); n     while (ch[x][0]) x = ch[x][0]; n     splay(x); n     return x; n   } n   void link(int x, int y) { n     access(y); n     splay(y); n     splay(x); n     fa[x] = y; n     s2[y] += s[x]; n     ss[y] += 1ll * s[x] * s[x]; n     pushup(y); n   } n   void cut(int x, int y) { n     access(y); n     splay(y); n     splay(x); n     fa[x] = 0; n     s2[y] -= s[x]; n     ss[y] -= 1ll * s[x] * s[x]; n     pushup(y); n   } n } t; n vector<pair<int, int> > q[maxn]; n bool c2[maxn]; n long long query(int u) { n   int x = t.findroot(u); n   if (c2[x]) n     return 1ll * t.s[t.ch[x][1]] * t.s[t.ch[x][1]]; n   else n     return 1ll * t.s[x] * t.s[x]; n } n long long modify(int u) { n   c2[u] ^= 1; n   if (c2[u]) { n     if (u == 1) { n       t.access(1); n       return t.ss[1] - 1ll * t.s[1] * t.s[1]; n     } else { n       long long res = -query(u); n       t.cut(u, fa[u]); n       t.access(u); n       res += t.ss[u]; n       if (!c2[fa[u]]) res += query(fa[u]); n       return res; n     } n   } else { n     if (u == 1) { n       t.access(1); n       return 1ll * t.s[1] * t.s[1] - t.ss[1]; n     } else { n       t.access(u); n       long long res = -t.ss[u]; n       if (!c2[fa[u]]) res -= query(fa[u]); n       t.link(u, fa[u]); n       res += query(u); n       return res; n     } n   } n } n long long ans[maxn]; n void solve(int c) { n   for (int i = 0; i < (int)q[c].size(); i++) n     ans[q[c][i].first] += modify(q[c][i].second); n   for (int i = (int)q[c].size() - 1; i >= 0; i--) modify(q[c][i].second); n } n int main() { n   n = readint(); n   m = readint(); n   for (int i = 1; i <= n; i++) c[i] = readint(); n   for (int i = 1; i < n; i++) { n     int u, v; n     u = readint(); n     v = readint(); n     g[u].push_back(v); n     g[v].push_back(u); n   } n   dfs(1); n   t.init(); n   for (int i = 2; i <= n; i++) t.link(i, fa[i]); n   for (int i = 1; i <= n; i++) q[c[i]].push_back(pair<int, int>(0, i)); n   for (int i = 1; i <= m; i++) { n     int u, x; n     u = readint(); n     x = readint(); n     q[c[u]].push_back(pair<int, int>(i, u)); n     q[c[u] = x].push_back(pair<int, int>(i, u)); n   } n   for (int i = 1; i <= n; i++) solve(i); n   for (int i = 1; i <= m; i++) ans[i] += ans[i - 1]; n   for (int i = 0; i <= m; i++) printf( %lld n , -ans[i]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long n, m; n long long a[100000], b[100000]; n long long val(long long p) { n   long long ret = 0; n   for (long long i = 0; i < n; ++i) ret += max((long long)0, p - a[i]); n   for (long long i = m - 1; i >= 0; --i) ret += max((long long)0, b[i] - p); n   return ret; n } n int32_t main() { n   cin >> n >> m; n   for (long long i = 0; i < n; ++i) cin >> a[i]; n   for (long long i = 0; i < m; ++i) cin >> b[i]; n   sort(a, a + n), sort(b, b + m); n   long long l = 0, r = 1e9 + 1, m1, m2, v1, v2; n   long long ans = LLONG_MAX; n   while (l <= r) { n     m1 = l + (r - l) / 3; n     m2 = l + 2 * (r - l) / 3; n     v1 = val(m1), v2 = val(m2); n     if (v1 <= v2) { n       ans = min(v1, ans); n       r = m2 - 1; n     } else { n       ans = min(v2, ans); n       l = m1 + 1; n     } n   } n   cout << ans; n } n