#include <bits/stdc++.h> n using namespace std; n const int inf = 100010; n const int N = 100010; n struct e { n   int v; n   e *nxt; n } es[N << 1], *fir[N]; n map<pair<int, int>, int> mp; n struct node { n   int ls, rs; n   int l, r; n   int sum; n   int mid() { return (l + r) >> 1; } n } nodes[N << 2]; n int n, en, val[N]; n int que[N], par[N], dep[N], root[N], seg[N], st[N], ed[N], top[N], sons[N], n     id[N]; n int ln, cnt, tr; n inline void init() { n   en = 0; n   memset(fir, 0, sizeof(fir)); n } n inline void add_e(int u, int v) { n   es[en].v = v; n   es[en].nxt = fir[u]; n   fir[u] = &es[en++]; n } n inline void newNode(int &id, int l, int r) { n   nodes[cnt].ls = nodes[cnt].rs = -1; n   nodes[cnt].l = l; n   nodes[cnt].r = r; n   id = cnt++; n } n void build(int &id, int l, int r) { n   newNode(id, l, r); n   if (l + 1 >= r) { n     nodes[id].sum = 1; n     return; n   } n   int mid = (l + r) >> 1; n   build(nodes[id].ls, l, mid); n   build(nodes[id].rs, mid, r); n   nodes[id].sum = nodes[nodes[id].ls].sum + nodes[nodes[id].rs].sum; n } n void initTree() { n   int l, r, u, v, i; n   e *cur; n   l = r = 0; n   tr = 1; n   que[r++] = tr; n   par[tr] = -1; n   dep[tr] = 0; n   while (l != r) { n     u = que[l++]; n     for (cur = fir[u]; cur; cur = cur->nxt) { n       if ((v = cur->v) != par[u]) { n         que[r++] = v; n         par[v] = u; n         dep[v] = dep[u] + 1; n       } n     } n   } n   for (i = 1; i <= n; i++) { n     sons[i] = 1; n     id[i] = -1; n   } n   for (i = r - 1; i >= 0; i--) { n     u = que[i]; n     if (par[u] >= 0) { n       sons[par[u]] += sons[u]; n     } n   } n   l = r = 0; n   que[r++] = tr; n   ln = cnt = 0; n   while (l != r) { n     u = que[l++]; n     st[ln] = dep[u]; n     top[ln] = u; n     while (u >= 0) { n       id[u] = ln; n       ed[ln] = dep[u]; n       seg[dep[u]] = u; n       int best; n       for (cur = fir[u], best = -1; cur; cur = cur->nxt) { n         if (id[v = cur->v] == -1) { n           if (best == -1 || (best >= 0 && sons[v] > sons[best])) { n             best = v; n           } n         } n       } n       if (best >= 0) { n         for (cur = fir[u]; cur; cur = cur->nxt) { n           if (id[v = cur->v] == -1 && best != v) { n             que[r++] = v; n           } n         } n       } n       u = best; n     } n     root[ln] = -1; n     build(root[ln], st[ln], ed[ln]); n     ln++; n   } n } n int S; n void lqry(int &id, int ql, int qr) { n   if (id == -1) return; n   if (ql == qr) return; n   if (ql <= nodes[id].l && nodes[id].r <= qr) { n     S += nodes[id].sum; n     if (S > inf) S = inf; n     return; n   } n   if (nodes[id].l + 1 == nodes[id].r) { n     S += nodes[id].sum; n     if (S > inf) S = inf; n     return; n   } n   int mid = (nodes[id].l + nodes[id].r) >> 1; n   if (ql < mid) { n     lqry(nodes[id].ls, ql, qr); n   } n   if (qr > mid) { n     lqry(nodes[id].rs, ql, qr); n   } n } n void update(int id, int x, int y, int val) { n   if (id == -1) return; n   if (nodes[id].l + 1 == nodes[id].r) { n     nodes[id].sum = val; n     return; n   } n   int mid = (nodes[id].l + nodes[id].r) >> 1; n   if (mid <= x) n     update(nodes[id].rs, x, y, val); n   else if (mid >= y) n     update(nodes[id].ls, x, y, val); n   nodes[id].sum = nodes[nodes[id].ls].sum + nodes[nodes[id].rs].sum; n   if (nodes[id].sum > inf) nodes[id].sum = inf; n } n void qry(int u, int v) { n   while (id[u] != id[v]) { n     if (id[u] > id[v]) { n       swap(u, v); n     } n     int b = id[v]; n     lqry(root[b], st[b], dep[v]); n     v = par[top[b]]; n     if (mp.find(make_pair(top[b], par[top[b]])) != mp.end()) n       S += val[mp[make_pair(top[b], par[top[b]])]]; n     else n       S += val[mp[make_pair(par[top[b]], top[b])]]; n     if (S > inf) S = inf; n   } n   if (dep[u] > dep[v]) { n     swap(u, v); n   } n   lqry(root[id[u]], dep[u], dep[v]); n } n struct ee { n   int x, y; n } edge[100010]; n int main() { n   int m, x, y, ty; n   scanf( %d , &n); n   init(); n   for (int i = 1; i < n; i++) { n     scanf( %d%d , &x, &y); n     mp[make_pair(x, y)] = i; n     val[i] = 1; n     edge[i].x = x; n     edge[i].y = y; n     add_e(x, y); n     add_e(y, x); n   } n   initTree(); n   scanf( %d , &m); n   for (int i = 0; i < m; i++) { n     scanf( %d , &ty); n     if (ty == 3) { n       scanf( %d%d , &x, &y); n       S = 0; n       qry(x, y); n       if (S >= inf) n         printf( -1 n ); n       else n         printf( %d n , S); n     } else if (ty == 1) { n       scanf( %d , &x); n       int u = edge[x].x; n       int v = edge[x].y; n       val[x] = 1; n       if (dep[u] > dep[v]) { n         swap(u, v); n       } n       if (id[u] == id[v]) update(root[id[u]], dep[u], dep[u], 1); n     } else { n       scanf( %d , &x); n       int u = edge[x].x; n       int v = edge[x].y; n       val[x] = inf; n       if (dep[u] > dep[v]) { n         swap(u, v); n       } n       if (id[u] == id[v]) update(root[id[u]], dep[u], dep[v], inf); n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n int32_t main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   ; n   int n; n   cin >> n; n   bitset<6> a(n); n   string s = a.to_string(); n   swap(s[1], s[5]); n   swap(s[2], s[3]); n   bitset<6> b(s); n   cout << static_cast<int>(b.to_ulong()) <<   n ; n } n


#include <bits/stdc++.h> n #pragma comment(linker,  /STACK:256000000 ) n using namespace std; n int main() { n   int n; n   scanf( %d , &n); n   printf( %d , n); n   for (int i = 1; i < n; i++) printf(  %d , i); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int tux; n   cin >> tux; n   int foo, bar, baz, quz, pur, oo; n   foo = bar = baz = 0; n   quz = 1; n   while (tux) { n     cin >> pur; n     foo += pur; n     bar++; n     if (foo * quz > bar * baz) { n       baz = foo; n       quz = bar; n     } n     tux--; n   } n   cout.precision(5); n   cout << fixed << (baz * 1. / quz); n   return 0; n } n


#include <bits/stdc++.h> n  n using namespace std; n  n typedef long long ll; n typedef long double ld; n typedef pair<int, int> pii; n typedef pair<ll, ll> pll; n typedef vector<int> vi; n typedef vector<ll> vl; n typedef vector<pii> vii; n typedef vector<vi> vvi; n typedef vector<pll> vll; n typedef vector<vl> vvl; n  n #define fori(i, n) for (int i = 0; i < n; i++) n #define ford(i, n) for (int i = n - 1; i >= 0; i--) n #define rep(i, a, b) for (int i = a; i <= b; i++) n #define repd(i, a, b) for (int i = a; i >= b; i--) n #define trav(x, a) for (auto &x : a) n #define all(x) (x).begin(), (x).end() n #define pb push_back n #define eb emplace_back n #define endl   n  n #define sz(a) (int)(a).size() n #define fi first n #define se second n  n clock_t time_p = clock(); n void time_taken() n { n     time_p = clock() - time_p; n     cerr <<  Time Taken :   << (float)(time_p) / CLOCKS_PER_SEC <<   n ; n } n  n const ll mod = 1e9 + 7; n const ll INF = 1e18; n  n int main() n { n     ios_base::sync_with_stdio(false), cin.tie(nullptr); n  n     int n, d; n     cin >> n >> d; n  n     vi a(n); n     fori(i, n) cin >> a[i]; n  n     vector<vector<ld>> dp(n + 1, vector<ld>(10, -1)); n     vvi prv(n + 1, vi(10)); n     vvi taken(n + 1, vi(10)); n     dp[0][1] = 0; n  n     for (int i = 0; i < n; i++) n     { n         for (int j = 0; j < 10; j++) n         { n             if (dp[i][j] == -1) n                 continue; n             int to = (j * a[i]) % 10; n             ld maybe = dp[i][j] + log2l(a[i]); n             if (maybe > dp[i + 1][to]) n             { n                 dp[i + 1][to] = maybe; n                 taken[i + 1][to] = 1; n                 prv[i + 1][to] = j; n             } n  n             to = j; n             maybe = dp[i][j]; n             if (maybe > dp[i + 1][to]) n             { n                 dp[i + 1][to] = maybe; n                 taken[i + 1][to] = 0; n                 prv[i + 1][to] = j; n             } n         } n     } n  n     if (dp[n][d] == -1) n     { n         cout <<  -1  << endl; n         return 0; n     } n  n     int i = n, j = d; n  n     vi ans; n     while (i) n     { n         if (taken[i][j]) n         { n             ans.pb(a[i - 1]); n         } n         j = prv[i][j]; n         i--; n     } n  n     if(sz(ans) == 0){ n         cout << -1 << endl; n         return 0; n     } n  n     cout << sz(ans) << endl; n     for (int x : ans) n     { n         cout << x <<    ; n     } n     cout << endl; n  n     time_taken(); n     return 0; n }


#include <bits/stdc++.h> n using namespace std; n int n, m, sz, timer; n int p[1000005]; n vector<int> v[1000005]; n int tin[1000005], tout[1000005]; n int t[4000005], tt[4000005]; n int depth[1000005]; n int a[1000005]; n pair<int, int> q[1000005]; n bool used[1000005]; n void dfs(int x, int y = 0) { n   used[x] = true; n   tin[x] = ++timer; n   depth[x] = depth[y] + 1; n   for (int i = 0; i < v[x].size(); ++i) { n     int to = v[x][i]; n     dfs(to, x); n   } n   tout[x] = timer; n   return; n } n void update(int x, int y) { n   x += sz - 1; n   t[x] = y; n   x >>= 1; n   while (x) { n     t[x] = max(t[x + x], t[x + x + 1]); n     x >>= 1; n   } n   return; n } n int get(int l, int r, int ll, int rr, int x) { n   if (l > r || ll > r || l > rr || ll > rr) return 0; n   if (l >= ll && r <= rr) return t[x]; n   int mid = (l + r) >> 1; n   return max(get(l, mid, ll, rr, x + x), get(mid + 1, r, ll, rr, x + x + 1)); n } n void update2(int x, int y) { n   x += sz - 1; n   tt[x] = y; n   x >>= 1; n   while (x) { n     tt[x] = max(tt[x + x], tt[x + x + 1]); n     x >>= 1; n   } n   return; n } n int get_pred(int x) { n   if (x == p[x]) return x; n   return p[x] = get_pred(p[x]); n } n void update_val(int x) { n   int val = get(1, sz, tin[x], tout[x], 1) - depth[x] + 1; n   update2(x, val); n   return; n } n void add_vertex(int x) { n   for (int i = 0; i < v[x].size(); ++i) { n     int to = v[x][i]; n     update2(to, 0); n     to = get_pred(to); n     p[to] = x; n   } n   update(tin[x], depth[x]); n   update_val(x); n   return; n } n void del_vertex(int x) { n   update(tin[x], 0); n   x = get_pred(x); n   update_val(x); n   return; n } n int main(int argc, const char* argv[]) { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   cin >> n >> m; n   for (int i = 1; i <= n; ++i) { n     cin >> a[i]; n   } n   q[0] = {1e9, n + 1}; n   for (int i = n; i > 0; --i) { n     while (sz > 0 && q[sz].first <= a[i]) --sz; n     v[q[sz].second].push_back(i); n     q[++sz] = {a[i], i}; n   } n   for (int i = n + 1; i > 0; --i) n     if (!used[i]) dfs(i); n   for (int i = 1; i <= n + 1; ++i) p[i] = i; n   sz = 1; n   while (sz < n + 1) sz += sz; n   for (int i = 1; i <= m; ++i) { n     add_vertex(i); n   } n   cout << tt[1] <<    ; n   for (int i = m + 1; i <= n; ++i) { n     add_vertex(i); n     del_vertex(i - m); n     cout << tt[1] <<    ; n   } n   cout <<   n ; n } n


#include <bits/stdc++.h> n int sta[1000001], head, ans[1000001]; n char s[1000001]; n int main() { n   int ans1 = 0, ans2 = 1; n   scanf( %s , s); n   for (int i = 0; s[i]; ++i) { n     if (s[i] ==  ) ) { n       if (!head) continue; n       head--; n       ans[i] = i - sta[head] + 1 + ans[sta[head] - 1]; n       if (ans1 == ans[i]) n         ans2++; n       else if (ans1 < ans[i]) { n         ans1 = ans[i]; n         ans2 = 1; n       } n     } else n       sta[head++] = i; n   } n   printf( %d %d , ans1, ans2); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int t, n; n int a[200004]; n int main() { n   cin >> t; n   while (t--) { n     cin >> n; n     for (int i = 0; i < 2 * n; i++) cin >> a[i]; n     sort(a, a + 2 * n); n     cout << a[n] - a[n - 1] <<   n ; n   } n   cin >> t; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int T, n, a[30005], b[30005]; n long long sum[2]; n int main() { n   scanf( %d , &T); n   while (T--) { n     scanf( %d , &n); n     for (int i = 1; i <= n; i++) scanf( %d , &a[i]); n     a[n + 1] = 0; n     for (int i = 1; i <= n + 1; i++) b[i] = a[i] - a[i - 1]; n     sum[0] = sum[1] = 0; n     for (int i = 2; i <= n; i++) { n       if (b[i] > 0) n         sum[0] += b[i]; n       else n         sum[1] -= b[i]; n     } n     if (b[1] >= sum[1] && -b[n + 1] >= sum[0]) { n       printf( YES n ); n     } else n       printf( NO n ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n bool g[205][205]; n struct coor { n   int x, y; n }; n vector<int> ans; n int a[8][2] = {1, 0, -1, 0, 0, 1, 0, -1, 1, 1, 1, -1, -1, 1, -1, -1}; n coor Max, Min; n int w, h; n void dfs(int x, int y) { n   g[x][y] = 1; n   for (int i = 0; i < 8; i++) { n     coor c; n     c.x = x + a[i][0]; n     c.y = y + a[i][1]; n     if (c.x >= 0 && c.x <= w && c.y >= 0 && c.y <= h) { n       if (c.x >= Max.x && c.y >= Max.y) Max = c; n       if (c.x <= Min.x && c.y <= Min.y) Min = c; n       if (g[c.x][c.y] == 0) dfs(c.x, c.y); n     } n   } n } n void out() { n   int i, j; n   for (i = 0; i <= w; i++) { n     for (j = 0; j <= h; j++) { n       printf( %d , g[i][j]); n     } n     printf(  n ); n   } n   printf(  n ); n } n int main() { n   int i, j, n, x1, x2, y1, y2; n   scanf( %d%d%d , &w, &h, &n); n   w = w + w; n   h = h + h; n   ans.clear(); n   memset(g, 0, sizeof(g)); n   for (i = 0; i <= w; i++) g[i][0] = g[i][h] = 1; n   for (i = 0; i <= h; i++) g[0][i] = g[w][i] = 1; n   for (j = 0; j < n; j++) { n     scanf( %d%d%d%d , &x1, &y1, &x2, &y2); n     x1 += x1; n     x2 += x2; n     y1 += y1; n     y2 += y2; n     if (x1 == x2) n       for (i = y1; i <= y2; i++) g[x1][i] = 1; n     else n       for (i = x1; i <= x2; i++) g[i][y1] = 1; n   } n   for (i = 0; i <= w; i++) n     for (j = 0; j <= h; j++) { n       if (g[i][j] == 0) { n         Max.x = Max.y = -1; n         Min.y = Min.x = 1000; n         dfs(i, j); n         ans.push_back((Max.x - Min.x) * (Max.y - Min.y) / 4); n       } n     } n   sort(ans.begin(), ans.end()); n   for (i = 0; i < ans.size() - 1; i++) printf( %d  , ans[i]); n   printf( %d n , ans[i]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1000000, M = 1000000007, D3 = (M + 1) / 3; n int T, n, m; n int pd[N + 5], pr[N], pn, phi[N + 5]; n int f[N + 5], s0[N + 5], s1[N + 5], s2[N + 5]; n void init() { n   phi[1] = 1; n   for (int i = 2; i <= N; i++) { n     if (!pd[i]) phi[i] = i - 1, pr[++pn] = i; n     for (int j = 1; j <= pn && i * pr[j] <= N; j++) { n       pd[i * pr[j]] = pr[j]; n       if (i % pr[j] == 0) { n         phi[i * pr[j]] = phi[i] * pr[j]; n         break; n       } else n         phi[i * pr[j]] = phi[i] * phi[pr[j]]; n     } n   } n   for (int d = 1; d <= N; d++) n     for (int k = d; k <= N; k += d) f[k] += d * phi[k / d]; n   for (int L = 1; L <= N; L++) { n     f[L] = n         (((long long)L * L - L - L) % M * L % M - n          (long long)(L + 1) * L % M * L % M + n          (long long)L * (L + 1) % M * (L + L + 1) % M * D3 % M + 2LL * f[L]) % n         M; n     if (f[L] < 0) f[L] += M; n   } n   for (int i = 1; i <= N; i++) { n     s0[i] = (s0[i - 1] + (long long)f[i]) % M; n     s1[i] = (s1[i - 1] + (long long)i * f[i]) % M; n     s2[i] = (s2[i - 1] + (long long)i * i % M * f[i]) % M; n   } n } n int main() { n   init(); n   for (scanf( %d , &T); T--;) { n     scanf( %d%d , &n, &m); n     if (n > m) swap(n, m); n     long long ans = (long long)(n + 1) * (m + 1) % M * s0[n] % M; n     ans -= (long long)(n + m + 2) * s1[n] % M; n     ans += s2[n]; n     ans = (ans % M + M) % M; n     printf( %d n , ans); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); n int n, m; n vector<int> tr[1000005]; n vector<int> vr[1000005]; n pair<int, int> pr[1000005]; n int high; n long long team[1000005], ir[1000005]; n int main() { n   scanf( %d %d , &n, &m); n   int x, y; n   for (int i = 0; i < m; i++) { n     scanf( %d %d , &x, &y); n     if (x < y) swap(x, y); n     vr[x].push_back(y); n   } n   for (int i = 1; i < n + 1; i++) { n     scanf( %d , &x); n     for (int j = 0; j < x; j++) { n       scanf( %d , &y); n       tr[i].push_back(y); n       high = max(high, y); n     } n   } n   while (1) { n     int tot = 0; n     for (int i = 1; i < high + 1; i++) team[i] = rng() % 2 + 1; n     for (int i = 1; i < n + 1; i++) { n       ir[i] = tr[i][rng() % tr[i].size()]; n       for (int j = 0; j < vr[i].size(); j++) { n         if (team[ir[vr[i][j]]] != team[ir[i]]) tot++; n       } n     } n     if (tot >= m / 2 + (m % 2)) break; n   } n   for (int i = 1; i < n + 1; i++) printf( %d  , ir[i]); n   printf(  n ); n   for (int i = 1; i < high + 1; i++) printf( %d  , team[i]); n } n


#include <bits/stdc++.h> n using namespace std; n int a[200001]; n int b[200000]; n int c[200001]; n int main() { n   int n, i, j; n   cin >> n; n   map<int, int> m; n   for (i = 0; i < n; i++) { n     cin >> b[i]; n     m[b[i]]++; n     a[b[i]] = 1; n   } n   vector<int> v; n   for (i = 1; i <= n; i++) { n     if (a[i] == 0) { n       v.push_back(i); n     } n   } n   sort(v.begin(), v.end()); n   cout << v.size() << endl; n   int p = 0; n   for (i = 0; i < n; i++) { n     if (c[b[i]] == 1) { n       cout << v[p] <<    ; n       p++; n       c[b[i]] = 1; n       m[b[i]]--; n     } else { n       if (m[b[i]] == 1) { n         cout << b[i] <<    ; n       } else if (m[b[i]] > 1) { n         if (v[p] < b[i]) { n           cout << v[p] <<    ; n           m[b[i]]--; n           p++; n         } else { n           cout << b[i] <<    ; n           c[b[i]] = 1; n           m[b[i]]--; n         } n       } n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n class segtree { n  public: n   int val[(1 << 20) * 2]; n   int le[(1 << 20) * 2]; n   int ri[(1 << 20) * 2]; n   void update(int pos, int x) { n     val[pos + (1 << 20) - 1] = 0; n     le[pos + (1 << 20) - 1] = 1 - x; n     ri[pos + (1 << 20) - 1] = x; n   } n   void up() { n     for (int i = (1 << 20) - 2; i >= 0; i--) { n       int add = min(le[i * 2 + 1], ri[i * 2 + 2]); n       val[i] = val[i * 2 + 1] + val[i * 2 + 2] + add; n       le[i] = le[i * 2 + 1] + le[i * 2 + 2] - add; n       ri[i] = ri[i * 2 + 1] + ri[i * 2 + 2] - add; n     } n   } n   pair<int, pair<int, int> > query(int a, int b, int k, int l, int r) { n     pair<int, pair<int, int> > p = make_pair(0, make_pair(0, 0)); n     if (r < a || b < l) return p; n     if (a <= l && r <= b) { n       return make_pair(val[k], make_pair(le[k], ri[k])); n     } n     pair<int, pair<int, int> > x = query(a, b, k * 2 + 1, l, (l + r) / 2); n     pair<int, pair<int, int> > y = query(a, b, k * 2 + 2, (l + r) / 2 + 1, r); n     int z = min(x.second.first, y.second.second); n     return make_pair(x.first + y.first + z, n                      make_pair(x.second.first + y.second.first - z, n                                x.second.second + y.second.second - z)); n   } n } seg; n char ss[1000005]; n int main() { n   scanf( %s , &ss); n   int n = strlen(ss); n   for (int i = 0; i < n; i++) { n     seg.update(i, (ss[i] ==  ) )); n   } n   seg.up(); n   int q; n   scanf( %d , &q); n   for (int i = 0; i < q; i++) { n     int a, b; n     scanf( %d %d , &a, &b); n     a--; n     b--; n     printf( %d n , seg.query(a, b, 0, 0, (1 << 20) - 1).first * 2); n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int t; n   cin >> t; n   while (t--) { n     int a, b, c, d; n     cin >> a >> b >> c >> d; n     if ((a + b) % 2 == 0) { n       cout <<  Tidak Tidak ; n       if (b || c) n         cout <<   Ya ; n       else n         cout <<   Tidak ; n       if (a || d) n         cout <<   Ya ; n       else n         cout <<   Tidak ; n     } else { n       if (a || d) n         cout <<  Ya ; n       else n         cout <<   Tidak ; n       if (b || c) n         cout <<   Ya ; n       else n         cout <<   Tidak ; n       cout <<   Tidak Tidak ; n     } n     cout <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int M = 1e6 + 10; n int n, ans; n void swap(int &x, int &y) { n   int t = x; n   x = y; n   y = t; n } n int main() { n   cin >> n; n   vector<char> x(n), y(n); n   for (auto &i : x) cin >> i; n   for (auto &i : y) cin >> i; n   for (int i = 0; i < n - 1; ++i) { n     if (x[i] == y[i]) continue; n     if (x[i + 1] == y[i + 1]) { n       ans++; n       continue; n     } n     if (x[i] == y[i + 1] && x[i + 1] == y[i]) n       swap(x[i], x[i + 1]), ans++; n     else n       ans++; n   } n   if (x[n - 1] != y[n - 1]) ans++; n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int const N = 202000; n set<int> s; n set<int>::iterator it; n int edg[N]; n vector<int> g[N]; n int ans[N]; n int cur = 1; n int vis[N]; n void dfs(int node) { n   vis[node] = 1; n   ans[cur]++; n   vector<int> send; n   for (it = s.begin(); it != s.end();) { n     if (!binary_search(g[node].begin(), g[node].end(), *it)) { n       send.push_back(*it); n       s.erase(it++); n     } else n       it++; n   } n   for (int el : send) dfs(el); n } n int main() { n   int n, e; n   scanf( %d%d , &n, &e); n   for (int i = 1; i <= n; ++i) s.insert(i); n   for (int i = 0; i < e; ++i) { n     int temp, temp1; n     scanf( %d%d , &temp, &temp1); n     edg[temp]++; n     edg[temp1]++; n     g[temp].push_back(temp1); n     g[temp1].push_back(temp); n   } n   vector<pair<int, int>> go; n   for (int i = 1; i <= n; i++) { n     go.push_back(make_pair(edg[i], i)); n     sort(g[i].begin(), g[i].end()); n   } n   sort(go.begin(), go.end()); n   for (auto el : go) { n     if (vis[el.second]) continue; n     s.erase(el.second); n     ans[0]++; n     dfs(el.second); n     cur++; n   } n   cout << ans[0] <<   n ; n   sort(ans + 1, ans + ans[0] + 1); n   for (int i = 1; i <= ans[0]; i++) { n     cout << ans[i] <<    ; n   } n   return 0; n } n


#include <bits/stdc++.h> n int main() { n   int n, x, m, count; n   scanf( %d %d , &n, &m); n   for (x = 1, count = 0; x <= n; x++) { n     if (m % x == 0 && (m / x) <= n) { n       count = count + 1; n     } n   } n   printf( %d , count); n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   int tt; n   cin >> tt; n   while (tt--) { n     int n; n     cin >> n; n     vector<int> a(n); n     for (int i = 0; i < n; ++i) { n       cin >> a[i]; n     } n     int ans = 0; n     sort(a.begin(), a.end()); n     for (int i = 0, j = 0; j < n; j++) { n       if (j - i + 1 >= a[j]) { n         ++ans; n         i = j + 1; n       } n     } n     cout << ans <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main(int argc, char* argv[]) { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   long long i, t, n, com, midc; n   cin >> n; n   map<int, int> m; n   set<int> s; n   vector<int> x(n); n   vector<int> a(3); n   for (i = 0; i < n; i++) { n     cin >> x[i]; n     m[x[i]]++; n     s.insert(x[i]); n   } n   t = s.size(); n   if (t == 1) { n     cout << n << endl; n     for (i = 0; i < n; i++) cout << x[i] <<    ; n     cout << endl; n   } else if (t == 2) { n     i = 0; n     for (set<int>::const_iterator it = s.begin(); it != s.end(); it++) { n       a[i] = *it; n       i++; n     } n     if (abs(a[0] - a[1]) == 1) { n       cout << n << endl; n       for (i = 0; i < n; i++) cout << x[i] <<    ; n       cout << endl; n     } else { n       com = min(m[a[0]], m[a[1]]); n       cout << n - (2 * com) << endl; n       ; n       for (i = 0; i < (2 * com); i++) cout << a[0] + 1 <<    ; n       for (i = 0; i < m[a[0]] - com; i++) cout << a[0] <<    ; n       for (i = 0; i < m[a[1]] - com; i++) cout << a[1] <<    ; n       cout << endl; n     } n   } else { n     i = 0; n     for (set<int>::const_iterator it = s.begin(); it != s.end(); it++) { n       a[i] = *it; n       i++; n     } n     midc = m[a[1]] / 2; n     com = min(m[a[0]], m[a[2]]); n     if (midc > com) { n       cout << n - (midc * 2) << endl; n       for (i = 0; i < midc; i++) { n         cout << a[0] <<    ; n         cout << a[2] <<    ; n       } n       for (i = 0; i < m[a[0]]; i++) cout << a[0] <<    ; n       for (i = 0; i < m[a[2]]; i++) cout << a[2] <<    ; n       if (m[a[1]] % 2) cout << a[1]; n       cout << endl; n     } else { n       cout << n - (2 * com) << endl; n       for (i = 0; i < m[a[1]]; i++) cout << a[1] <<    ; n       for (i = 0; i < (2 * com); i++) cout << a[1] <<    ; n       for (i = 0; i < m[a[0]] - com; i++) cout << a[0] <<    ; n       for (i = 0; i < m[a[2]] - com; i++) cout << a[2] <<    ; n       cout << endl; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MOD = 1e9 + 7; n const long long int INF = 1e12; n int n, k; n int memo[101][101][101]; n int solve(string &str, int i, int N, int _max) { n   _max = max(N, _max); n   int &a = memo[i][N][_max]; n   if (a != -1) return a; n   if (i == n) return a = (_max == k); n   if (str[i] !=  N ) { n     bool temp1 = solve(str, i + 1, 0, _max); n     if (str[i] ==  Y  || temp1) return a = temp1; n   } n   return a = solve(str, i + 1, N + 1, _max); n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   memset(memo, -1, sizeof(memo)); n   cin >> n >> k; n   string str; n   cin >> str; n   cout << (solve(str, 0, 0, 0) ?  YES  :  NO ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n double EPS = 1e-9; n int INF = 1000000000; n long long INFF = 8000000000000000000LL; n double PI = acos(-1); n int dirx[8] = {-1, 0, 0, 1, -1, -1, 1, 1}; n int diry[8] = {0, 1, -1, 0, -1, 1, -1, 1}; n inline string IntToString(int a) { n   char x[100]; n   sprintf(x,  %d , a); n   string s = x; n   return s; n } n inline int StringToInt(string a) { n   char x[100]; n   int res; n   strcpy(x, a.c_str()); n   sscanf(x,  %d , &res); n   return res; n } n inline string GetString(void) { n   char x[1000005]; n   scanf( %s , x); n   string s = x; n   return s; n } n inline string uppercase(string s) { n   int n = (int)s.size(); n   for (int(i) = (0); (i) < (n); ++(i)) n     if (s[i] >=  a  && s[i] <=  z ) s[i] = s[i] -  a  +  A ; n   return s; n } n inline string lowercase(string s) { n   int n = (int)s.size(); n   for (int(i) = (0); (i) < (n); ++(i)) n     if (s[i] >=  A  && s[i] <=  Z ) s[i] = s[i] -  A  +  a ; n   return s; n } n inline void OPEN(string s) { n   freopen((s +  .in ).c_str(),  r , stdin); n   freopen((s +  .out ).c_str(),  w , stdout); n } n int k, n1, n2, n3, t1, t2, t3; n priority_queue<int, vector<int>, greater<int> > satu, dua, tiga; n int t[100005]; n bool can(int T) { n   if ((int)satu.size() == n1 && satu.top() + t1 > T) return false; n   if ((int)dua.size() == n2 && dua.top() + t2 > T + t1) return false; n   if ((int)tiga.size() == n3 && tiga.top() + t3 > T + t1 + t2) return false; n   return true; n } n int main() { n   scanf( %d %d %d %d %d %d %d , &k, &n1, &n2, &n3, &t1, &t2, &t3); n   int bef = 0; n   for (int(i) = (0); (i) < (k); ++(i)) { n     int ambil = bef; n     int L = t[i - 1]; n     int R = INF; n     while (L <= R) { n       int M = (L + R) >> 1; n       if (can(M)) { n         ambil = M; n         R = M - 1; n       } else n         L = M + 1; n     } n     if ((int)satu.size() == n1) { n       satu.pop(); n     } n     satu.push(ambil); n     if ((int)dua.size() == n2) { n       dua.pop(); n     } n     dua.push(ambil + t1); n     if ((int)tiga.size() == n3) { n       tiga.pop(); n     } n     tiga.push(ambil + t1 + t2); n     if (i == k - 1) { n       printf( %d n , ambil + t1 + t2 + t3); n     } n     bef = ambil; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void solve(int x, int y, int n) { n   vector<int> ans(n + 1); n   ans[x] = 1; n   ans[y] = 2; n   for (int i = 1, j = 3; i <= n; i++) { n     if (ans[i] == 0) { n       ans[i] = j++; n     } n   } n   for (int i = 1; i <= n; i++) { n     cout << ans[i] <<    ; n   } n   cout <<   n ; n   ans[y] = 1; n   for (int i = 1; i <= n; i++) { n     cout << ans[i] <<    ; n   } n   cout <<   n ; n   exit(0); n } n signed main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   int n, m; n   cin >> n >> m; n   if (n == 0 || n * (n - 1) / 2 == m) { n     cout <<  NO n ; n   } else { n     cout <<  YES n ; n     set<pair<int, int>> st; n     for (int i = 0; i < m; i++) { n       int x, y; n       cin >> x >> y; n       if (x > y) swap(x, y); n       st.insert({x, y}); n     } n     for (int i = 1; i <= n; i++) { n       for (int j = i + 1; j <= n; j++) { n         if (st.find({i, j}) == st.end()) { n           solve(i, j, n); n         } n       } n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n map<long long, long long> mp, vis; n vector<long double> vv; n vector<pair<long long, long long> > vp; n string s, q; n char c; n int main() { n   long double t = 0, n, m, l, r, mn = 0, mx = 0, ans = 0, nm = 0, cnt = 0, x, y, n               z, d, v, g; n   cin >> l >> d >> v >> g >> r; n   l -= d; n   t += d / v; n   long long p = (long long)t / (long long)(g + r); n   x = t - (p * (g + r)); n   if (x >= g) t += r - (x - g); n   t += l / v; n   cout << setprecision(10) << fixed << t; n } n


#include <bits/stdc++.h> n using namespace std; n long long int max(long long int a, long long int b) { n   if (a > b) n     return a; n   else n     return b; n } n long long int min(long long int a, long long int b) { n   if (a < b) n     return a; n   else n     return b; n } n int main() { n   long long int t, a, b, x, y; n   cin >> t; n   while (t--) { n     long long int k1 = 0, k2 = 0, k3 = 0, k4 = 0, k5 = 0, k6 = 0, k7 = 0, c1, n                   c2, c3, c4; n     cin >> a >> b >> x >> y; n     c1 = max(x, y); n     c2 = min(x, y); n     c3 = max(a, b); n     c4 = min(a, b); n     if (min(a, b) >= max(x, y)) { n       k1 = (a - x - 1) * b; n       k2 = x * b; n       k3 = (b - y - 1) * a; n       k4 = y * a; n       k5 = max(k1, k2); n       k6 = max(k3, k4); n       k7 = max(k5, k6); n     } else { n       k1 = (c3 - c1 - 1) * c4; n       k2 = c1 * c4; n       k3 = (c4 - c2 - 1) * c3; n       k4 = c2 * c3; n       k5 = max(k1, k2); n       k6 = max(k3, k4); n       k7 = max(k5, k6); n     } n     cout << k7; n     if (t != 0) cout << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int a[26][26] = {0}, n, mini = -1; n   cin >> n; n   while (n--) { n     char c[11]; n     scanf( %s , c); n     int k = strlen(c), i = c[0] -  a , j = c[k - 1] -  a ; n     for (int m = 0; m < 26; m++) n       if (a[m][i]) a[m][j] = max(a[m][j], a[m][i] + k); n     a[i][j] = max(a[i][j], k); n     mini = max(mini, a[j][j]); n   } n   cout << mini; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 500; n int dist[maxn][maxn]; n long long ans; n void modify(int u, int v, int w) { n   if (dist[u][v] > w) { n     ans -= (dist[v][u] - w); n     dist[u][v] = w; n   } n   return; n } n int main() { n   int n; n   cin >> n; n   for (int i = 1; i <= n; i++) { n     for (int j = 1; j <= n; j++) { n       cin >> dist[i][j]; n       if (j > i) { n         ans += dist[i][j]; n       } n     } n   } n   int k; n   cin >> k; n   for (int i = 1; i <= k; i++) { n     int u, v, w; n     cin >> u >> v >> w; n     modify(u, v, w); n     if (dist[u][v] != dist[v][u]) { n       dist[v][u] = dist[u][v]; n     } n     for (int p = 1; p <= n; p++) { n       for (int q = 1; q <= n; q++) { n         modify(p, q, dist[p][u] + dist[v][q] + w); n         if (dist[p][q] != dist[q][p]) { n           dist[q][p] = dist[p][u] + dist[v][q] + w; n         } n       } n     } n     cout << ans << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1000; n const int inf = (int)1e9 + 1; n const long long big = (long long)1e18 + 1; n const int P = 239; n const int MOD = (int)1e9 + 7; n const int MOD1 = (int)1e9 + 9; n const double eps = 1e-9; n const double pi = atan2(0, -1); n const int ABC = 26; n int cnt = 0, len, s, f; n vector<int> g[N]; n void solve(int k) { n   if (k == 1) { n     g[0].push_back(1); n     g[1].push_back(0); n     s = 0; n     f = 1; n     len = 1; n     cnt += 2; n   } else if (k % 2 == 0) { n     solve(k / 2); n     g[cnt].push_back(s); n     g[s].push_back(cnt); n     g[cnt + 1].push_back(s); n     g[s].push_back(cnt + 1); n     g[cnt + 2].push_back(cnt); n     g[cnt].push_back(cnt + 2); n     g[cnt + 2].push_back(cnt + 1); n     g[cnt + 1].push_back(cnt + 2); n     s = cnt + 2; n     len += 2; n     cnt += 3; n   } else { n     solve(k - 1); n     for (int i = 0; i < len; i++) { n       if (i == len - 1) { n         g[cnt - 1].push_back(f); n         g[f].push_back(cnt - 1); n       } else { n         g[cnt - 1].push_back(cnt); n         g[cnt].push_back(cnt - 1); n         cnt++; n       } n     } n   } n } n char ans[N][N]; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.precision(20); n   int k; n   cin >> k; n   solve(k); n   int id[cnt]; n   int curr = 2; n   for (int i = 0; i < cnt; i++) n     if (i == s) n       id[i] = 0; n     else if (i == f) n       id[i] = 1; n     else n       id[i] = curr++; n   for (int i = 0; i < cnt; i++) n     for (int j = 0; j < cnt; j++) ans[i][j] =  N ; n   for (int i = 0; i < cnt; i++) n     for (int j : g[i]) ans[id[i]][id[j]] =  Y ; n   cout << cnt <<   n ; n   for (int i = 0; i < cnt; i++) { n     for (int j = 0; j < cnt; j++) cout << ans[i][j]; n     cout <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n inline int Get() { n   char ch; n   while ((ch = getchar()) <  0  || ch >  9 ) n     ; n   int Num = ch -  0 ; n   while ((ch = getchar()) >=  0  && ch <=  9 ) n     Num = (Num << 3) + (Num << 1) + ch -  0 ; n   return Num; n } n const int N = 1e5 + 5; n int n, m, a[N], count[N << 2], sum[N << 2], rev[N << 2]; n std ::vector<int> vec0[N], vec1[N]; n void build(int k, int l, int r) { n   sum[k] = r - l + 1; n   if (l == r) return; n   int mid = l + r >> 1; n   build(k << 1, l, mid), build(k << 1 | 1, mid + 1, r); n } n void down(int k) { n   if (rev[k]) { n     count[k << 1] = sum[k << 1] - count[k << 1], rev[k << 1] ^= 1; n     count[k << 1 | 1] = sum[k << 1 | 1] - count[k << 1 | 1], n                    rev[k << 1 | 1] ^= 1; n     rev[k] = 0; n   } n } n void modify(int k, int l, int r, int x, int y) { n   if (x <= l && r <= y) { n     rev[k] ^= 1, count[k] = sum[k] - count[k]; n     return; n   } n   int mid = l + r >> 1; n   down(k); n   if (x <= mid) modify(k << 1, l, mid, x, y); n   if (y > mid) modify(k << 1 | 1, mid + 1, r, x, y); n   count[k] = count[k << 1] + count[k << 1 | 1]; n } n int query(int k, int l, int r, int x, int y) { n   if (x <= l && r <= y) return count[k]; n   int mid = l + r >> 1; n   down(k); n   if (y <= mid) return query(k << 1, l, mid, x, y); n   if (x > mid) return query(k << 1 | 1, mid + 1, r, x, y); n   return query(k << 1, l, mid, x, y) + query(k << 1 | 1, mid + 1, r, x, y); n } n int main() { n   n = Get(), m = Get(); n   for (int i = 1; i <= n; ++i) a[i] = Get(); n   std ::sort(a + 1, a + n + 1); n   for (int i = 1; i <= m; ++i) { n     int l = std ::lower_bound(a + 1, a + n + 1, Get()) - a; n     int r = std ::upper_bound(a + 1, a + n + 1, Get()) - a - 1; n     if (1 <= l && l <= n && 1 <= r && r <= n && l <= r) n       vec0[l].push_back(r), vec1[r].push_back(l); n   } n   long long ans = (long long)n * (n - 1) * (n - 2) / 6; n   build(1, 1, n); n   for (int i = 1; i <= n; ++i) { n     for (int k = 0, sze = vec0[i].size(); k < sze; ++k) n       modify(1, 1, n, i, vec0[i][k]); n     int res = (i > 1 ? i - 1 - query(1, 1, n, 1, i - 1) : 0) + n               (i < n ? query(1, 1, n, i + 1, n) : 0); n     ans -= (long long)res * (res - 1) / 2; n     for (int k = 0, sze = vec1[i].size(); k < sze; ++k) n       modify(1, 1, n, vec1[i][k], i); n   } n   printf( %I64d n , ans); n } n


#include <bits/stdc++.h> n using namespace std; n void upd(int& a, int b) { a = max(a, b); } n int main() { n   int n, m; n   cin >> n >> m; n   vector<int> a(n), b(m); n   for (int i = 0; i < (int)n; ++i) cin >> a[i]; n   for (int i = 0; i < (int)m; ++i) cin >> b[i]; n   sort(a.begin(), a.end()); n   sort(b.begin(), b.end()); n   vector<int> to_seg(n); n   int seg = 0; n   vector<int> start, end; n   for (int i = 0; i < (int)a.size(); ++i) { n     if (i == 0 || a[i] != a[i - 1] + 1) { n       seg++; n       if (i != 0) end.push_back(i - 1); n       start.push_back(i); n     } n     to_seg[i] = seg - 1; n   } n   end.push_back(n - 1); n   vector<int> z(start.size()), d(start.size()); n   for (int segment = 0; segment < (int)start.size(); ++segment) { n     int first = -1; n     int cnt = 0; n     for (int i = 0; i < (int)m; ++i) { n       if (b[i] < a[start[segment]]) n         first = i; n       else if (b[i] <= a[end[segment]]) n         cnt++; n     } n     int cnt_orig = cnt; n     upd(d[segment], cnt); n     if (segment) upd(d[segment], cnt + z[segment - 1]); n     if (first != -1) { n       for (int i = first; i >= 0; --i) { n         ++cnt; n         int r = a[start[segment]]; n         int l = b[i]; n         if (start[segment] - (r - l) >= 0) { n           int Q = 0; n           int X = to_seg[start[segment] - (r - l)] - 1; n           if (X >= 0) Q = z[X]; n           upd(d[segment], cnt + Q); n         } n       } n     } n     cnt = 0; n     for (int i = first + cnt_orig + 1; i < m; ++i) { n       ++cnt; n       int l = a[end[segment]]; n       int r = b[i]; n       if (end[segment] + (r - l) < n) { n         upd(z[to_seg[end[segment] + (r - l)]], d[segment] + cnt); n       } n     } n     upd(z[segment], d[segment]); n   } n   cout << *max_element(z.begin(), z.end()) << endl; n } n


#include <bits/stdc++.h> n using namespace std; n long long calc2(long long a, long long b, long long c, long long ab, n                 long long ac, long long bc, long long abc) { n   long long parts[] = {a - ab - ac + abc, n                        b - ab - bc + abc, n                        c - ac - bc + abc, n                        ab - abc, n                        ac - abc, n                        bc - abc, n                        abc}; n   int masks[3] = {1 + 8 + 16 + 64, 2 + 8 + 32 + 64, 4 + 16 + 32 + 64}; n   static int mark[64 * 8] = {0}; n   static int id = 0; n   ++id; n   long long res = 0; n   for (int i = 0; i < 7; ++i) n     if ((masks[0] >> i) & 1) n       for (int j = 0; j < 7; ++j) n         if ((masks[1] >> j) & 1) n           for (int k = 0; k < 7; ++k) n             if ((masks[2] >> k) & 1) { n               int a1 = min(i, min(j, k)); n               int a3 = max(i, max(j, k)); n               int a2 = i + j + k - a1 - a3; n               if (mark[64 * a1 + 8 * a2 + a3] == id) continue; n               mark[64 * a1 + 8 * a2 + a3] = id; n               if (i == j && j == k) { n                 long long x = parts[i]; n                 res += +x * (x - 1) * (x - 2) / 6 + x * (x - 1) + x; n               } else if (i == j || j == k || i == k) { n                 long long x = parts[i]; n                 long long y = parts[j]; n                 if (i == j) n                   y = parts[k]; n                 else if (j == k) n                   swap(x, y); n                 res += x * (x + 1) / 2 * y; n               } else { n                 res += parts[i] * parts[j] * parts[k]; n               } n             } n   return res; n } n int get_common_num(vector<int> &a, vector<int> &b) { n   int ia = 0, ib = 0; n   int res = 0; n   while (ia < a.size() && ib < b.size()) { n     if (a[ia] == b[ib]) { n       ++res; n       ++ia, ++ib; n     } else if (a[ia] < b[ib]) n       ++ia; n     else n       ++ib; n   } n   return res; n } n int get_common_num(vector<int> &a, vector<int> &b, vector<int> &c) { n   int ia = 0, ib = 0, ic = 0; n   int res = 0; n   while (ia < a.size() && ib < b.size() && ic < c.size()) { n     if (a[ia] == b[ib] && b[ib] == c[ic]) { n       ++res; n       ++ia, ++ib, ++ic; n     } else if (a[ia] <= min(b[ib], c[ic])) n       ++ia; n     else if (b[ib] <= min(a[ia], c[ic])) n       ++ib; n     else n       ++ic; n   } n   return res; n } n vector<int> dividers[100111]; n int main() { n   ios::sync_with_stdio(false); n   dividers[1].push_back(1); n   vector<int> smallest_fact(100111 + 1, 0); n   for (int i = 2; i < 100111; ++i) { n     if (!smallest_fact[i]) { n       smallest_fact[i] = i; n       for (long long j = i * (long long)i; j < 100111; j += i) { n         if (!smallest_fact[j]) smallest_fact[j] = i; n       } n     } n     int p = smallest_fact[i]; n     int x = i; n     while (x % p == 0) x /= p; n     int y = i; n     int pp = p; n     dividers[i] = dividers[x]; n     while (y % p == 0) { n       for (int d : dividers[x]) dividers[i].push_back(pp * d); n       pp *= p; n       y /= p; n     } n     sort(dividers[i].begin(), dividers[i].end()); n   } n   int t; n   cin >> t; n   while (t--) { n     int a, b, c; n     cin >> a >> b >> c; n     int x = dividers[a].size(); n     int y = dividers[b].size(); n     int z = dividers[c].size(); n     int xy = get_common_num(dividers[a], dividers[b]); n     int xz = get_common_num(dividers[a], dividers[c]); n     int yz = get_common_num(dividers[b], dividers[c]); n     int xyz = get_common_num(dividers[a], dividers[b], dividers[c]); n     long long ans = calc2(x, y, z, xy, xz, yz, xyz); n     cout << ans <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n map<string, int> my; n int n, m; n string var[5006]; n string bit[5006]; n int op[5006][2]; n int opera[5006]; n int ans[5005][1003]; n int print1[1005], print2[1005]; n int main() { n   scanf( %d , &n); n   scanf( %d , &m); n   for (int i = 1; i <= n; i++) { n     string z; n     cin >> z; n     my[z] = i; n     var[i] = z; n     cin >> z; n     cin >> z; n     if (z[0] ==  ? ) { n       op[i][0] = 0; n       cin >> z; n       if (z[0] ==  A ) n         opera[i] == 1; n       else if (z[0] ==  O ) n         opera[i] = 2; n       else n         opera[i] = 3; n       cin >> z; n       if (z[0] ==  ? ) n         op[i][1] = 0; n       else n         op[i][1] = my[z]; n     } else if (z[0] ==  0  || z[0] ==  1 ) n       bit[i] = z; n     else { n       op[i][0] = my[z]; n       cin >> z; n       if (z[0] ==  A ) n         opera[i] = 1; n       else if (z[0] ==  O ) n         opera[i] = 2; n       else n         opera[i] = 3; n       cin >> z; n       if (z[0] ==  ? ) n         op[i][1] = 0; n       else n         op[i][1] = my[z]; n     } n   } n   for (int i = 0; i < m; i++) { n     int cnt[2]; n     cnt[0] = cnt[1] = 0; n     for (int j = 0; j <= 1; j++) { n       for (int k = 1; k <= n; k++) { n         if (opera[k] == 0) { n           ans[k][i] = bit[k][i] -  0 ; n           cnt[j] += ans[k][i]; n         } else { n           int v1 = op[k][0], v2 = op[k][1]; n           if (v1 == 0) ans[v1][i] = j; n           if (v2 == 0) ans[v2][i] = j; n           if (opera[k] == 1) n             ans[k][i] = ans[v1][i] & ans[v2][i]; n           else if (opera[k] == 2) n             ans[k][i] = ans[v1][i] | ans[v2][i]; n           else n             ans[k][i] = ans[v1][i] ^ ans[v2][i]; n           cnt[j] += ans[k][i]; n         } n       } n     } n     if (cnt[0] > cnt[1]) { n       print1[i] = 0; n       print2[i] = 1; n     } else if (cnt[0] < cnt[1]) { n       print1[i] = 1; n       print2[i] = 0; n     } else { n       print1[i] = 0; n       print2[i] = 0; n     } n   } n   for (int i = 0; i < m; i++) cout << print2[i]; n   cout << endl; n   for (int i = 0; i < m; i++) cout << print1[i]; n } n


#include <bits/stdc++.h> n using namespace std; n const int MOD = 1e9 + 7; n long long power(long long a, long long b) { n   long long ans = 1; n   while (b > 0) { n     if (b & 1) ans *= a; n     a = a * a; n     b >>= 1; n   } n   return ans; n } n long long powm(long long a, long long b) { n   a %= MOD; n   long long ans = 1; n   while (b > 0) { n     if (b & 1) ans = (ans * a) % MOD; n     a = (a * a) % MOD; n     b >>= 1; n   } n   return ans; n } n void IO() {} n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   IO(); n   ; n   long long n, k; n   cin >> n >> k; n   long long A[k]; n   for (long long &x : A) cin >> x; n   long long pos = 0, res = LLONG_MAX; n   for (long long i = 0; i < k; i++) { n     if (n % A[i] < res) { n       pos = i + 1; n       res = n % A[i]; n     } n   } n   cout << pos <<     << n / A[pos - 1] <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n #pragma GCC optimize( Ofast ) n #pragma GCC target( sse,sse2,sse3,ssse3,sse4,avx,avx2 ) n const long long maxn = 64, mod = 1e9 + 7, INF = 1e9, lg = 15 + 1, SQRT = 400; n int n, a[maxn], par[maxn], SZ_T[maxn], FAC[maxn], INV[maxn]; n vector<int> adj1[maxn], adj2[maxn], cmp; n long long dp[(1 << lg)][maxn], ans[maxn]; n bool mark[maxn]; n inline long long power(long long a, long long b) { n   long long res = 1; n   while (b) { n     if (b & 1) { n       res = (res * a) % mod; n     } n     b /= 2; n     a = (a * a) % mod; n   } n   return res; n } n inline void make_graph() { n   sort(a, a + n); n   for (int i = 0; i < n; ++i) { n     for (int j = i + 1; j < n; ++j) { n       if (a[j] % a[i] == 0) { n         adj1[i].push_back(j); n         adj1[j].push_back(i); n       } n     } n   } n } n void dfs(int v) { n   mark[v] = 1; n   cmp.push_back(a[v]); n   for (auto u : adj1[v]) { n     if (!mark[u]) { n       dfs(u); n     } n   } n } n inline void get(int cnt) { n   memset(dp, 0, sizeof dp); n   sort(cmp.begin(), cmp.end()); n   vector<int> s, t; n   for (int i = 0; i < cmp.size(); ++i) { n     bool is = 0; n     for (int j = 0; j < i; ++j) { n       if (cmp[i] % cmp[j] == 0) is = 1; n     } n     if (!is) { n       s.push_back(cmp[i]); n     } else { n       t.push_back(cmp[i]); n     } n   } n   for (int i = 0; i < t.size(); ++i) { n     par[i] = 0; n     for (int j = 0; j < s.size(); ++j) { n       if (t[i] % s[j] == 0) { n         par[i] |= (1 << j); n       } n     } n   } n   SZ_T[cnt] = t.size(); n   if (!t.size()) { n     ans[cnt] = 1; n     return; n   } n   for (int i = 0; i < t.size(); ++i) { n     dp[par[i]][1]++; n   } n   for (int mask = 0; mask < (1 << s.size()); ++mask) { n     for (int i = 0; i < t.size(); ++i) { n       int counter = 0; n       for (int j = 0; j < t.size(); ++j) { n         if ((mask | par[j]) == mask) { n           counter++; n         } else if (mask & par[j]) { n           dp[mask | par[j]][i + 1] = n               (dp[mask][i] + dp[mask | par[j]][i + 1]) % mod; n         } n       } n       if (counter > i) n         dp[mask][i + 1] = n             (dp[mask][i + 1] + (dp[mask][i] * (counter - i)) % mod) % mod; n     } n   } n   ans[cnt] = dp[(1 << s.size()) - 1][t.size()]; n } n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   FAC[0] = INV[0] = 1; n   for (int i = 1; i < maxn; ++i) { n     FAC[i] = (FAC[i - 1] * i) % mod; n     INV[i] = power(FAC[i], mod - 2); n   } n   cin >> n; n   for (int i = 0; i < n; ++i) { n     cin >> a[i]; n   } n   make_graph(); n   int cnt = 0; n   for (int i = 0; i < n; ++i) { n     if (!mark[i]) { n       dfs(i); n       get(cnt); n       cnt++; n       cmp.clear(); n     } n   } n   long long res = 1, SZ = 0; n   for (int i = 0; i < cnt; ++i) { n     res = (res * ans[i]) % mod; n     SZ += max(SZ_T[i] - 1, 0); n   } n   long long h = FAC[SZ]; n   for (int i = 0; i < cnt; ++i) { n     h = (h * INV[max(SZ_T[i] - 1, 0)]) % mod; n   } n   res = (res * h) % mod; n   cout << res << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   long long n, s = 0, c = 0; n   cin >> n; n   for (long long int i = 0; i < n; i++) { n     long long x; n     cin >> x; n     s += x; n     if (x % 2 != 0) c = 1; n   } n   if (s % 2 != 0) { n     cout <<  First ; n   } else { n     if (c) n       cout <<  First ; n     else n       cout <<  Second ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, d, moves = 0; n   int* b; n   cin >> n >> d; n   b = new int[n]; n   cin >> b[0]; n   for (int i = 1; i < n; i++) { n     cin >> b[i]; n     if (b[i] <= b[i - 1]) { n       int time = (b[i - 1] - b[i]) / d + 1; n       b[i] += time * d; n       moves += time; n     } n   } n   delete[] b; n   cout << moves; n } n


#include <bits/stdc++.h> n using std::cerr; n using std::endl; n const int N = 2005, INF = 1e9, P = 998244353; n int n, G[N][N]; n char str[N]; n int deg[N]; n inline int binom(int x) { return x * (x - 1) / 2; } n inline bool check() { n   std::sort(deg + 1, deg + n + 1); n   int sum = 0; n   for (int i = 1; i < n; ++i) { n     sum += deg[i]; n     if (sum == binom(i)) return 0; n   } n   return 1; n } n namespace SOL1 { n inline bool Check(int s) { n   for (int i = 1; i <= n; ++i) deg[i] = 0; n   for (int i = 1; i <= n; ++i) n     for (int j = 1; j <= n; ++j) { n       int cnt = ((s >> (i - 1)) & 1) + ((s >> (j - 1)) & 1); n       if (cnt == 1) { n         if (G[i][j]) ++deg[i]; n       } else { n         if (G[j][i]) ++deg[i]; n       } n     } n   return check(); n } n inline void solve() { n   int min = INF, cnt = 0; n   for (int s = 0; s < 1 << n; ++s) { n     if (__builtin_popcount(s) > min) continue; n     if (Check(s)) { n       if (__builtin_popcount(s) < min) min = __builtin_popcount(s), cnt = 0; n       if (__builtin_popcount(s) == min) { n         ++cnt; n       } n     } n   } n   if (min == INF) n     puts( -1 ); n   else { n     for (int i = 1; i <= min; ++i) cnt = 1ll * cnt * i % P; n     printf( %d %d n , min, cnt); n   } n } n }  // namespace SOL1 n namespace SOL2 { n int buc[N]; n inline void solve() { n   for (int i = 1; i <= n; ++i) n     for (int j = 1; j <= n; ++j) n       if (G[j][i]) ++deg[i], ++buc[i]; n   if (check()) { n     std::cout << 0 <<     << 1 << std::endl; n     return; n   } n   int cnt = 0; n   for (int i = 1; i <= n; ++i) { n     for (int j = 1; j <= n; ++j) deg[j] = buc[j]; n     for (int j = 1; j <= n; ++j) { n       if (G[j][i]) --deg[i], ++deg[j]; n       if (G[i][j]) ++deg[i], --deg[j]; n     } n     cnt += check(); n   } n   std::cout << 1 <<     << cnt << std::endl; n } n }  // namespace SOL2 n int main() { n   scanf( %d , &n); n   for (int i = 1; i <= n; ++i) { n     scanf( %s , str + 1); n     for (int j = 1; j <= n; ++j) G[i][j] = str[j] -  0 ; n   } n   if (n <= 6) n     SOL1::solve(); n   else n     SOL2::solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n class TaskD { n  public: n   unsigned long long compute(vector<pair<int, int> >& a, n                              const vector<unsigned long long>& tag) { n     vector<pair<int, int> > events; n     for (int i = 0; i < a.size(); ++i) { n       events.emplace_back(a[i].first, -(i + 1)); n       events.emplace_back(a[i].second, i + 1); n     } n     sort(events.begin(), events.end()); n     unsigned long long sum = 0; n     unsigned long long res = 0; n     for (const auto& e : events) { n       if (e.second < 0) { n         res += sum * tag[-e.second - 1]; n         sum += tag[-e.second - 1]; n       } else { n         sum -= tag[e.second - 1]; n       } n     } n     return res; n   } n   void solveOne(istream& in, ostream& out) { n     auto seed = std::chrono::system_clock::now().time_since_epoch().count(); n     mt19937 rng(seed); n     uniform_int_distribution<unsigned long long> distrib; n     int n; n     in >> n; n     vector<pair<int, int> > a(n); n     vector<pair<int, int> > b(n); n     vector<unsigned long long> tag(n); n     for (int i = 0; i < n; ++i) { n       in >> a[i].first >> a[i].second >> b[i].first >> b[i].second; n       tag[i] = distrib(rng) * 2 + 1; n     } n     auto ha = compute(a, tag); n     auto hb = compute(b, tag); n     if (ha == hb) { n       out <<  YES n ; n     } else { n       out <<  NO n ; n     } n   } n   void solve(std::istream& in, std::ostream& out) { n     int nt = 1; n     for (int it = 0; it < nt; ++it) { n       solveOne(in, out); n     } n   } n }; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   TaskD solver; n   std::istream& in(std::cin); n   std::ostream& out(std::cout); n   solver.solve(in, out); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long n, m, res, t, k, o; n long long a[1000100], b[1000100]; n priority_queue<int, vector<int>, greater<int> > q; n int main() { n   scanf( %lld %lld , &n, &k); n   for (long long i = 1; i <= n; i++) { n     scanf( %lld , &a[i]); n   } n   scanf( %lld , &m); n   for (long long i = 1; i <= n; i++) { n     scanf( %lld , &b[i]); n   } n   res = 0; n   for (int i = 1; i <= n; i++) { n     q.push(b[i]); n     while (k < a[i] && !q.empty()) { n       k = k + m; n       res += q.top(); n       q.pop(); n     } n     if (k < a[i]) { n       res = -1; n       break; n     } n   } n   printf( %lld n , res); n } n


#include <bits/stdc++.h> n using namespace std; n long long mod_v(long long num) { n   if (num >= 0) n     return (num % 1000000007); n   else n     return (num % 1000000007 + 1000000007) % 1000000007; n } n long long bigmod(long long b, long long p, long long m) { n   long long res = 1 % m, x = b % m; n   while (p) { n     if (p & 1) res = (res * x) % m; n     x = (x * x) % m; n     p >>= 1; n   } n   return res; n } n class node { n  public: n   int s, w; n   node(){}; n   node(int s, int w) { n     this->s = s; n     this->w = w; n   }; n }; n bool operator<(node a, node b) { n   if (a.w < b.w) n     return true; n   else n     return false; n } n bool vol[100005] = {0}; n int vis[100005] = {0}; n vector<int> adj[100005]; n int n; n bool solve(int q, int s, int d) { n   memset(vis, 0, sizeof(vis)); n   vis[s] = q; n   priority_queue<node> pq; n   pq.push(node(s, q)); n   while (!pq.empty()) { n     s = pq.top().s; n     pq.pop(); n     if (s == d) return true; n     for (int i = 0, ns; i < adj[s].size(); i++) { n       ns = adj[s][i]; n       if (ns == d) return true; n       if (vol[ns] == 1 && vis[ns] < q) { n         vis[ns] = q; n         pq.push(node(ns, vis[ns])); n       } else if (vis[ns] < vis[s] - 1) { n         vis[ns] = vis[s] - 1; n         pq.push(node(ns, vis[ns])); n       } n     } n   } n   return false; n } n int main() { n   int m, k, u, v, s, d; n   scanf( %d %d %d , &n, &m, &k); n   for (int i = 1; i <= k; i++) { n     scanf( %d , &u); n     vol[u] = 1; n   } n   for (int i = 1; i <= m; i++) { n     scanf( %d %d , &u, &v); n     adj[u].push_back(v); n     adj[v].push_back(u); n   } n   scanf( %d %d , &s, &d); n   int l = 1, r = n, mid; n   while (l < r) { n     mid = (l + r) / 2; n     if (solve(mid, s, d)) n       r = mid; n     else n       l = mid + 1; n   } n   if (l == n) n     printf( -1 n ); n   else n     cout << l << endl; n } n


#include <bits/stdc++.h> n using namespace std; n long long n, a[3000], b[3000], w[500], pos[500][5000], ans; n string second; n int ispal(int x, int y) { n   int t = y; n   while (x != y) { n     if (second[x] != second[y]) return 0; n     if (y - x == 1 || y - x == 2) { n       b[t]++; n       return 1; n     } n     x++, y--; n   } n } n int main() { n   cin >> second; n   for (int i = second.size() - 1; i >= 0; i--) { n     a[i] = a[i + 1] + 1; n     for (int j = 0; j < w[second[i] - 97]; j++) { n       a[i] += ispal(i, pos[second[i] - 97][j]); n     } n     pos[second[i] - 97][w[second[i] - 97]] = i; n     w[second[i] - 97]++; n   } n   for (int i = 0; i < second.size() - 1; i++) { n     b[i]++; n     ans += b[i] * a[i + 1]; n   } n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n map<string, int> Id; n char Name[107][15], Str[107][150]; n int dp[107][107], last[107][107], record[107][107], vis[107]; n void Show(int Step, int k) { n   if (Step - 1) Show(Step - 1, last[Step][k]); n   int pos = 0; n   while (Str[Step][pos] !=  : ) pos++; n   printf( %s%s n , Name[k], Str[Step] + pos); n } n bool Judge(char t) { n   if (t >=  a  && t <=  z ) n     return true; n   else if (t >=  A  && t <=  Z ) n     return true; n   else if (t >=  0  && t <=  9 ) n     return true; n   else n     return false; n } n int main() { n   int t, n, m; n   string tmp; n   scanf( %d , &t); n   while (t--) { n     scanf( %d , &n); n     Id.clear(); n     for (int i = 1; i <= n; i++) { n       scanf( %s , Name[i]); n       Id[string(Name[i])] = i; n     } n     memset(record, 0, sizeof(record)); n     memset(vis, 0, sizeof(vis)); n     scanf( %d , &m); n     getchar(); n     for (int i = 1; i <= m; i++) { n       gets(Str[i]); n       int pos = 0; n       if (Str[i][0] !=  ? ) { n         while (Str[i][pos] !=  : ) pos++; n         int re = Id[string(Str[i], pos)]; n         record[i][re] = 1; n         vis[re] = 1; n         continue; n       } else { n         for (int j = 1; j <= n; j++) record[i][j] = 1; n         while (1) { n           while (!Judge(Str[i][pos]) && Str[i][pos]) pos++; n           if (Str[i][pos] ==   0 ) break; n           int tt = pos; n           while (Judge(Str[i][pos])) pos++; n           int re = Id[string(Str[i], tt, pos - tt)]; n           if (re || vis[re]) record[i][re] = 0; n         } n       } n     } n     memset(dp, 0, sizeof(dp)); n     memset(last, 0, sizeof(last)); n     for (int i = 1; i <= m; i++) { n       if (i == 1) { n         for (int j = 1; j <= n; j++) dp[i][j] = record[i][j]; n       } else { n         for (int j = 1; j <= n; j++) { n           if (!record[i][j]) continue; n           for (int k = 1; k <= n; k++) { n             if (j == k || !dp[i - 1][k]) continue; n             last[i][j] = k; n             dp[i][j] = 1; n           } n         } n       } n     } n     int flag = true; n     for (int i = 1; i <= n; i++) { n       if (dp[m][i]) { n         Show(m, i); n         flag = false; n         break; n       } n     } n     if (flag) printf( Impossible n ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 100000 + 10; n const int INF = 1000000000; n const int MOD = (int)(1e9) + 7; n int main() { n   int n; n   cin >> n; n   int m = (1 << (n + 1)) - 1; n   vector<int> a(m + 10, 0); n   for (int i = 2; i <= m; i++) cin >> a[i]; n   vector<int> s(m + 10, 0), c(m + 10, 0), add(m + 10, 0); n   int l = 0; n   for (int i = 2; i <= m; i++) { n     s[i] = s[i / 2] + a[i]; n     l = max(l, s[i]); n   } n   for (int i = m; i >= 1; i--) { n     if (i + i + 1 > m) n       c[i] = s[i]; n     else n       c[i] = max(c[i + i], c[i + i + 1]); n   } n   int res = 0; n   for (int i = 2; i <= m; i++) { n     int cur = c[i] + add[i / 2]; n     int inc = l - cur; n     add[i] = add[i / 2] + inc; n     res += inc; n   } n   cout << res << endl; n } n


#include <bits/stdc++.h> n using namespace std; n void test_case() { n   int n; n   long long k; n   cin >> n >> k; n   int l, r[2] = {-1, -1}; n   long long curr[2] = {k, k}, ans[2] = {0, 0}, monsters; n   for (int i = 0; i < n; i++) { n     int old_r[2] = {r[0], r[1]}, tmp_r; n     long long old_curr[2] = {curr[0], curr[1]}, old_ans[2] = {ans[0], ans[1]}; n     cin >> l >> tmp_r >> monsters; n     ans[0] = -1; n     for (int j = 0; j < 2; j++) { n       if (old_ans[j] == -1) continue; n       if (old_curr[j] + (tmp_r - l) * k < monsters) continue; n       if (ans[0] != -1 && ans[0] < old_ans[j] + monsters) continue; n       ans[0] = old_ans[j] + monsters; n       if (old_curr[j] >= monsters) { n         r[0] = l; n         curr[0] = old_curr[j] - monsters; n       } else { n         int turns = (monsters - old_curr[j] + k - 1) / k; n         r[0] = l + turns; n         curr[0] = k * turns + old_curr[j] - monsters; n       } n     } n     ans[1] = -1; n     for (int j = 0; j < 2; j++) { n       if (old_ans[j] == -1) continue; n       if ((tmp_r - l + 1) * k < monsters) continue; n       if (old_r[j] == l) continue; n       if (ans[1] != -1 && ans[1] < old_ans[j] + old_curr[j] + monsters) n         continue; n       ans[1] = old_ans[j] + old_curr[j] + monsters; n       int turns = (monsters + k - 1) / k; n       r[1] = l + turns - 1; n       curr[1] = k * turns - monsters; n     } n   } n   if (ans[0] == -1) n     cout << ans[1] <<   n ; n   else if (ans[1] == -1) n     cout << ans[0] <<   n ; n   else n     cout << min(ans[0], ans[1]) <<   n ; n } n int main(void) { n   ios::sync_with_stdio(0); n   cin.tie(0); n   int Case = 1; n   for (int i = 0; i < Case; i++) { n     test_case(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int r, c, n, k, x, y; n int flag[101][101]; n int main() { n   cin >> r >> c >> n >> k; n   for (int i = 0; i < n; i++) { n     cin >> x >> y; n     flag[x][y] = 1; n   } n   int cnt = 0; n   for (int i = 1; i <= r; i++) { n     for (int i1 = 1; i1 <= c; i1++) { n       for (int i2 = 1; i2 <= r; i2++) { n         for (int i3 = 1; i3 <= c; i3++) { n           int sum = 0; n           for (int i5 = i; i5 <= i2; i5++) { n             for (int i6 = i1; i6 <= i3; i6++) { n               if (flag[i5][i6] == 1) sum++; n             } n           } n           if (sum >= k) cnt++; n         } n       } n     } n   } n   cout << cnt; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MOD = 1e9 + 7; n const int MAX = 1e5 + 55; n const int INF = 1e9 + 77; n const long long INFINF = 1e18 + 1e17; n const double PI = acos(-1.0); n long long power(long long x, long long y) { n   if (y == 0) return 1; n   long long s = power(x, y / 2); n   s *= s; n   if (y & 1) s *= x; n   return s; n } n int n; n long long e[4000][4000]; n long long dis[4000]; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   cin >> n; n   int node = 1; n   for (int p = 1; p <= ((1 << n + 1) - 1) / 2; p++) { n     int dis1; n     int dis2; n     cin >> dis1; n     cin >> dis2; n     e[node * 2][node] = dis1; n     e[node * 2 + 1][node] = dis2; n     node++; n   } n   long long ans = 0; n   node = ((1 << n + 1) - 1); n   while (node != 1) { n     int node1 = node; n     int node2 = node - 1; n     int par = node / 2; n     int max1 = dis[node1] + e[node1][par]; n     int max2 = dis[node2] + e[node2][par]; n     dis[par] = max(max1, max2); n     ans += (dis[par] - min(max1, max2)); n     node -= 2; n   } n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int Int() { n   int x; n   scanf( %d , &x); n   return x; n } n long long Long() { n   long long x; n   scanf( %lld , &x); n   return x; n } n void err(istream_iterator<string> it) { cout << endl; } n template <typename T, typename... Args> n void err(istream_iterator<string> it, T a, Args... args) { n   cerr << *it <<   =   << a <<    ; n   err(++it, args...); n } n const int N = (int)2e5 + 5; n const int maxN = (int)1e6 + 6; n const long long Mod = (long long)1e9 + 7; n const int inf = (int)2e9; n const long long Inf = (long long)1e18; n int deg[N], vis[N]; n vector<int> g[N]; n int par[N]; n int Find(int s) { return par[s] == s ? s : Find(par[s]); } n int main() { n   int test = 1, tc = 0; n   while (test--) { n     int n = Int(), m = Int(), mx = 0, node = 0; n     for (int i = 1; i <= m; ++i) { n       int x = Int(), y = Int(); n       deg[x]++, deg[y]++; n       if (deg[x] > mx) { n         mx = deg[x], node = x; n       } n       if (deg[y] > mx) { n         mx = deg[y], node = y; n       } n       g[x].push_back(y); n       g[y].push_back(x); n     } n     priority_queue<pair<int, int> > pp; n     pp.push({mx, node}); n     while (!pp.empty()) { n       pair<int, int> c = pp.top(); n       pp.pop(); n       vis[c.second] = 1; n       for (int i : g[c.second]) { n         if (vis[i]) continue; n         printf( %d %d n , i, c.second); n         vis[i] = 1; n         pp.push({deg[i], i}); n       } n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n; n string s, t; n int main() { n   ios::sync_with_stdio(false); n   cin.tie(NULL); n   cin >> n >> t; n   int ph = 0; n   pair<int, int> mh = {0, 0}; n   for (int i = 0; i < n; i++) { n     if (t[i] ==  ( ) n       ph++; n     else n       ph--; n     mh = min(mh, make_pair(ph, i + 1)); n   } n   if (ph != 0) { n     cout <<  0 n1 1 n ; n     return 0; n   } n   string s; n   for (int i = mh.second; i < n; i++) { n     s.push_back(t[i]); n   } n   for (int i = 0; i < mh.second; i++) { n     s.push_back(t[i]); n   } n   int shift = mh.second; n   int h = 0, cnt = 0, one = 0, two = 0, piv = 0, piv2 = -1; n   pair<int, pair<int, int>> opt = {-1, {-1, -1}}; n   pair<int, pair<int, int>> opt2 = {-1, {-1, -1}}; n   for (int i = 0; i < n; i++) { n     if (s[i] ==  ( ) n       h++; n     else n       h--; n     if (h == 0) { n       cnt++; n       opt = max(opt, make_pair(one, make_pair(piv, i))); n       one = 0; n       piv = i + 1; n       piv2 = -1; n     } else if (h == 1) { n       one++; n       if (piv2 != -1) opt2 = max(opt2, make_pair(two, make_pair(piv2, i))); n       two = 0; n       piv2 = i + 1; n     } else if (h == 2) n       two++; n   } n   if (opt.first <= cnt + max(0, opt2.first)) { n     if (opt2.first == -1) { n       cout << cnt <<   n1 1 n ; n       return 0; n     } n     cout << cnt + opt2.first <<   n ; n     cout << (opt2.second.first + shift) % n + 1 <<    ; n     cout << (opt2.second.second + shift) % n + 1 <<   n ; n     return 0; n   } n   cout << opt.first <<   n  n        << (opt.second.first + shift) % n + 1 <<     n        << (opt.second.second + shift) % n + 1 <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n string s; n int main() { n   int t; n   cin >> t; n   while (t--) { n     cin >> s; n     int n = s.length(); n     if (s[n - 2] ==  p  && s[n - 1] ==  o ) { n       cout <<  FILIPINO  << endl; n     } else if (s[n - 1] ==  u  && s[n - 2] ==  s  && n                (s[n - 3] ==  e  || s[n - 3] ==  a ) && n                (s[n - 4] ==  d  || s[n - 4] ==  m )) { n       cout <<  JAPANESE  << endl; n     } else { n       cout <<  KOREAN  << endl; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n #pragma GCC optimize( Ofast ) n using namespace std; n const int INF = 1e9, MOD = INF - 1755647; n int gcd(int a, int b) { return (b ? gcd(b, a % b) : a); } n int power(int a, int b) { n   return (!b ? 1 : power(a, b / 2) * power(a, b / 2) * (b % 2 ? a : 1)); n } n int mod(int a) { return (a % MOD + MOD) % MOD; } n const int N = 2e5 + 20; n struct node { n   int val = 0; n   bool marked = false; n   vector<int> adj; n }; n int a[N]; n node g[N]; n set<int> ans; n void run(int src) { n   g[src].marked = true; n   queue<int> q; n   q.push(src); n   while (!q.empty()) { n     int v = q.front(); n     q.pop(); n     g[v].val++; n     ans.insert(v); n     for (int u : g[v].adj) { n       g[u].val++; n       if (!g[u].marked && g[u].val == a[u]) q.push(u); n     } n   } n } n int32_t main() { n   ios::sync_with_stdio(0), cin.tie(0), cout.tie(0); n   int n, m; n   cin >> n >> m; n   for (int i = 0; i < m; i++) { n     int v, u; n     cin >> v >> u; n     g[v].adj.push_back(u); n     g[u].adj.push_back(v); n   } n   for (int i = 1; i <= n; i++) cin >> a[i]; n   for (int i = 1; i <= n; i++) n     if (g[i].val == a[i]) run(i); n   cout << ans.size() <<   n ; n   for (int v : ans) cout << v <<    ; n   cout <<   n ; n   return 0; n } n


#include<bits/stdc++.h> n using namespace std; n int main() n { n    n  ios_base::sync_with_stdio(false); n  cin.tie(NULL); n  cout.tie(NULL); n  int t; n  cin>>t; n  while(t--) n  { n   int n; n   cin>>n; n   string a; n   cin>>a; n   if(a[0]== 2 &&a[1]== 0 &&a[2]== 2 &&a[3]== 0 ) n   cout<< YES <<  n ; n   else if(a[n-4]== 2 &&a[n-3]== 0 &&a[n-2]== 2 &&a[n-1]== 0 ) n   cout<< YES <<  n ; n   else if(a[0]== 2 &&a[1]== 0 &&a[2]== 2 &&a[n-1]== 0 ) n   cout<< YES <<  n ; n   else if(a[0]== 2 &&a[1]== 0 &&a[n-2]== 2 &&a[n-1]== 0 ) n   cout<< YES <<  n ; n   else if(a[0]== 2 &&a[n-3]== 0 &&a[n-2]== 2 &&a[n-1]== 0 ) n   cout<< YES <<  n ; n   else n   cout<< NO <<  n ; n  } n     return 0; n }


#include <bits/stdc++.h> n using namespace std; n const int maxn = 1000000 + 10; n char b[maxn]; n int a[maxn]; n int ccc[20]; n int main() { n   long long n; n   long long sum = 0; n   cin >> n; n   if (n % 2 == 0) n     sum = n / 2; n   else n     sum = (-n - 1) / 2; n   cout << sum << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int x[3], y[3]; n map<pair<int, int>, bool> cleaned; n int cost(int cx, int cy) { n   int sum = 0; n   for (int i = 0; i < (3); i++) n     sum += ((cx - x[i]) > 0 ? (cx - x[i]) : -(cx - x[i])) + n            ((cy - y[i]) > 0 ? (cy - y[i]) : -(cy - y[i])); n   return sum; n } n void clean(int x1, int y1, int x2, int y2) { n   cleaned[pair<int, int>(x1, y1)] = true; n   if (x1 == x2 && y1 == y2) return; n   if (x1 < x2) n     clean(x1 + 1, y1, x2, y2); n   else if (x1 > x2) n     clean(x1 - 1, y1, x2, y2); n   else if (y1 < y2) n     clean(x1, y1 + 1, x2, y2); n   else n     clean(x1, y1 - 1, x2, y2); n } n int main() { n   ios_base::sync_with_stdio(false); n   int bestX = 0, bestY = 0, bestCost; n   for (int i = 0; i < (3); i++) cin >> x[i] >> y[i]; n   bestCost = cost(0, 0); n   for (int ix = 0; ix < (1001); ix++) n     for (int iy = 0; iy < (1001); iy++) { n       int curCost = cost(ix, iy); n       if (curCost < bestCost) { n         bestCost = curCost; n         bestX = ix; n         bestY = iy; n       } n     } n   for (int i = 0; i < (3); i++) clean(x[i], y[i], bestX, bestY); n   cout << ((cleaned).size()) << endl; n   for (map<pair<int, int>, bool>::iterator it = cleaned.begin(); n        it != cleaned.end(); ++it) { n     cout << it->first.first <<     << it->first.second << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const double eps = 1e-7; n struct segtree { n   struct node { n     double sum; n     double mul, add; n     node() { n       sum = 0; n       mul = 1; n       add = 0; n     } n     void apply_mul(int l, int r, double v) { n       mul *= v; n       add *= v; n       sum *= v; n     } n     void apply_add(int l, int r, double v) { n       add += v; n       sum += v * (r - l + 1); n     } n   }; n   inline void push(int x, int l, int r) { n     int m = (l + r) >> 1; n     int z = x + ((m - l + 1) << 1); n     if (fabs(tree[x].mul - 1) > eps) { n       tree[x + 1].apply_mul(l, m, tree[x].mul); n       tree[z].apply_mul(m + 1, r, tree[x].mul); n       tree[x].mul = 1; n     } n     if (fabs(tree[x].add) > eps) { n       tree[x + 1].apply_add(l, m, tree[x].add); n       tree[z].apply_add(m + 1, r, tree[x].add); n       tree[x].add = 0; n     } n   } n   node unite(const node &a, const node &b) const { n     node res; n     res.sum = a.sum + b.sum; n     return res; n   } n   inline void pull(int x, int z) { tree[x] = unite(tree[x + 1], tree[z]); } n   int n; n   vector<node> tree; n   void build(int x, int l, int r, const vector<double> &v) { n     if (l == r) { n       tree[x].sum = v[l]; n       return; n     } n     int m = (l + r) >> 1; n     int z = x + ((m - l + 1) << 1); n     build(x + 1, l, m, v); n     build(z, m + 1, r, v); n     pull(x, z); n   } n   node get(int x, int l, int r, int ll, int rr) { n     if (ll <= l && r <= rr) { n       return tree[x]; n     } n     int y = (l + r) >> 1; n     int z = x + ((y - l + 1) << 1); n     push(x, l, r); n     node res; n     if (rr <= y) { n       res = get(x + 1, l, y, ll, rr); n     } else { n       if (ll > y) { n         res = get(z, y + 1, r, ll, rr); n       } else { n         res = unite(get(x + 1, l, y, ll, rr), get(z, y + 1, r, ll, rr)); n       } n     } n     pull(x, z); n     return res; n   } n   void modify_mul(int x, int l, int r, int ll, int rr, double v) { n     if (ll <= l && r <= rr) { n       tree[x].apply_mul(l, r, v); n       return; n     } n     int m = (l + r) >> 1; n     int z = x + ((m - l + 1) << 1); n     push(x, l, r); n     if (ll <= m) { n       modify_mul(x + 1, l, m, ll, rr, v); n     } n     if (rr > m) { n       modify_mul(z, m + 1, r, ll, rr, v); n     } n     pull(x, z); n   } n   void modify_add(int x, int l, int r, int ll, int rr, double v) { n     if (ll <= l && r <= rr) { n       tree[x].apply_add(l, r, v); n       return; n     } n     int m = (l + r) >> 1; n     int z = x + ((m - l + 1) << 1); n     push(x, l, r); n     if (ll <= m) { n       modify_add(x + 1, l, m, ll, rr, v); n     } n     if (rr > m) { n       modify_add(z, m + 1, r, ll, rr, v); n     } n     pull(x, z); n   } n   segtree() {} n   template <typename M> n   segtree(const vector<M> &v) { n     n = v.size(); n     assert(n > 0); n     tree.resize(2 * n - 1); n     build(0, 0, n - 1, v); n   } n   node get(int ll, int rr) { n     assert(0 <= ll && ll <= rr && rr <= n - 1); n     return get(0, 0, n - 1, ll, rr); n   } n   void modify_mul(int ll, int rr, double v) { n     assert(0 <= ll && ll <= rr && rr <= n - 1); n     modify_mul(0, 0, n - 1, ll, rr, v); n   } n   void modify_add(int ll, int rr, double v) { n     assert(0 <= ll && ll <= rr && rr <= n - 1); n     modify_add(0, 0, n - 1, ll, rr, v); n   } n }; n int main() { n   int n, q; n   scanf( %d%d , &n, &q); n   vector<double> a(n); n   for (int i = 0; i < n; i++) { n     scanf( %lf , &a[i]); n   } n   segtree st(a); n   while (q--) { n     int tp, l1, r1, l2, r2, l, r; n     scanf( %d , &tp); n     if (tp == 1) { n       scanf( %d%d%d%d , &l1, &r1, &l2, &r2); n       l1--; n       r1--; n       l2--; n       r2--; n       int lena = r1 - l1 + 1; n       int lenb = r2 - l2 + 1; n       double suma = st.get(l1, r1).sum; n       double sumb = st.get(l2, r2).sum; n       st.modify_mul(l1, r1, 1.0 * (lena - 1) / lena); n       st.modify_add(l1, r1, sumb / lena / lenb); n       st.modify_mul(l2, r2, 1.0 * (lenb - 1) / lenb); n       st.modify_add(l2, r2, suma / lena / lenb); n     } else { n       scanf( %d%d , &l, &r); n       l--; n       r--; n       printf( %.8f n , st.get(l, r).sum); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 2e5 + 5; n const int mod = 1e9 + 7; n int n, l[N], t[N]; n long long r; n int main() { n   scanf( %d%lld , &n, &r); n   for (int i = 0; i < n; i++) scanf( %d , l + i); n   for (int i = 0; i < n; i++) { n     scanf( %d , t + i); n     if (t[i] < l[i]) return puts( -1 ); n   } n   vector<long long> out; n   long long cur = 0, time = 0; n   long long ans = 0; n   for (int i = 0; i < n; i++) { n     int x = min((long long)l[i], cur); n     cur -= x; n     l[i] -= x; n     t[i] -= x; n     time += x; n     x = l[i] + l[i]; n     time += min(x, t[i]); n     int d = x - t[i]; n     if (d <= 0) continue; n     int need = (d + r - 1) / r; n     ans += need; n     long long lo = time - d; n     for (int j = 0; j < need; j++) { n       if (out.size() > 100000) break; n       out.push_back(lo); n       lo += r; n     } n     cur = need * r - d; n   } n   printf( %lld n , ans); n   if (ans <= 100000) { n     for (int i = 0; i < ans; i++) printf( %lld  , out[i]); n   } n } n


#include <bits/stdc++.h> n using ul = std::uint32_t; n using li = std::int32_t; n using ull = std::uint64_t; n using ll = std::int64_t; n std::vector<ul> v; n std::string s; n int main() { n   std::getline(std::cin, s); n   std::stringstream ss(s); n   while (std::getline(ss, s,  , )) { n     std::stringstream ss(s); n     ul t; n     ss >> t; n     v.push_back(t); n   } n   std::sort(v.begin(), v.end()); n   bool flag = false; n   for (ul i = 0, j = 0; i != v.size(); i = j) { n     for (++j; j != v.size() && v[j - 1] + 1 >= v[j]; ++j) n       ; n     if (flag) { n       std::putchar( , ); n     } n     flag = true; n     if (v[j - 1] == v[i]) { n       std::printf( %u , v[i]); n     } else { n       std::printf( %u-%u , v[i], v[j - 1]); n     } n   } n   std::putchar(  n ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = (int)4e2; n const int INF = (int)1e9; n int n, m, a[N][N], p; n int dyn[N][N], last[N]; n vector<pair<int, int> > q[N * N]; n int len[N][N]; n int main() { n   cin >> n >> m >> p; n   for (int i = 0; i < n; ++i) { n     for (int j = 0; j < m; ++j) { n       cin >> a[i][j]; n       q[a[i][j]].push_back(make_pair(i, j)); n       dyn[i][j] = INF; n     } n   } n   for (int i = 0; i < m; ++i) { n     dyn[0][i] = i; n   } n   for (int lvl = 1; lvl <= p; ++lvl) { n     int x, y; n     for (auto tmp : q[lvl]) { n       tie(x, y) = tmp; n       int ans = INF; n       for (int row = 0; row < n; ++row) { n         if (last[row] == lvl - 1) { n           ans = min(ans, dyn[row][y] + abs(row - x)); n         } n       } n       len[x][y] = ans; n     } n     for (auto tmp : q[lvl]) { n       tie(x, y) = tmp; n       if (last[x] != lvl) { n         last[x] = lvl; n         for (int col = 0; col < m; ++col) { n           dyn[x][col] = len[x][y] + abs(y - col); n         } n       } else { n         for (int col = 0; col < m; ++col) { n           dyn[x][col] = min(dyn[x][col], len[x][y] + abs(y - col)); n         } n       } n     } n   } n   cout << len[q[p][0].first][q[p][0].second] <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long mul(long long a, long long b) { return (a * b) % 1000000007; } n long long add(long long a, long long b) { n   a += b; n   if (a >= 1000000007) a -= 1000000007; n   return a; n } n long long binary_expo(long long base, long long expo) { n   long long res = 1; n   while (expo) { n     if (expo % 2) { n       res = mul(res, base); n     } n     expo /= 2; n     base = mul(base, base); n   } n   return res; n } n long long A[202]; n void solve() { n   long long n; n   cin >> n; n   for (long long i = 1; i <= 2 * n; i++) { n     cin >> A[i]; n   } n   long long ans = 0; n   for (long long i = 1; i <= 2 * n; i += 2) { n     long long index = -1; n     for (long long j = i + 1; j <= 2 * n; j++) { n       if (A[i] == A[j]) { n         index = j; n         break; n       } n     } n     for (long long j = index - 1; j >= i + 1; j--) { n       swap(A[j], A[j + 1]); n       ans++; n     } n   } n   cout << ans <<   n ; n } n signed main() { n   long long t; n   t = 1; n   while (t--) { n     solve(); n   } n } n


#include<bits/stdc++.h> n using namespace std; n  n long long t,n,k,pos; n string s; n bool mode; n  n char best(int pos){ n  char c=s[pos]; n   n  return min({c,char((c- a +1)%k+ a ),char(((c- a -1)%k+k)%k+ a )}); n } n  n int main(){ n  n  ios::sync_with_stdio(false); n  cin.tie(0); n  n  cin>>t; n   n  while(t--){ n   cin>>n>>k>>s; n    n   mode=false; n   pos=0; n    n   while(pos<s.size()){ n    if(mode==false){ n     if((pos+1>=s.size() || best(pos)<= best(pos+1)) && (pos+2>=s.size() || best(pos)<=s[pos+2])){ n      s[pos]=best(pos); n      pos++; n      mode=false; n     }else if(pos+2>=s.size() || best(pos+1)<=s[pos+2]){ n      if(best(pos+1)==s[pos+1]){ n       s[pos]=best(pos); n       swap(s[pos],s[pos+1]); n       pos++; n       mode=true; n      }else{ n       s[pos+1]=best(pos+1); n       swap(s[pos],s[pos+1]); n       mode=true; n       pos++; n      } n     }else{ n      s[pos]=best(pos); n      swap(s[pos+1],s[pos+2]); n      swap(s[pos],s[pos+1]); n      pos+=2; n      mode=true; n     } n    }else{ n     if((pos+1>=s.size() || s[pos]<=s[pos+1]) && (pos+2>=s.size() || s[pos]<=s[pos+2])){ n      pos++; n      mode=false; n     }else if(pos+2>=s.size() || s[pos+1]<=s[pos+2]){ n      swap(s[pos],s[pos+1]); n      pos++; n      mode=true; n     }else{ n      swap(s[pos+1],s[pos+2]); n      swap(s[pos],s[pos+1]); n      pos+=2; n      mode=true; n     } n    } n     n    //cout<<s<<  n ; n    //cout<<pos<<   <<mode<<  ecco  n ; n   } n    n   cout<<s<<  n ; n  } n  n  return 0; n } n  n


#include <bits/stdc++.h> n using namespace std; n const int nMax = 2000005; n int p[nMax]; n int main() { n   int n; n   cin >> n; n   for (int i = 0; i < n; ++i) { n     p[i] = i + 1; n   } n   int s = 0; n   for (int j = 2; j < n + 1; ++j) { n     int curr = p[s]; n     p[n + s] = p[n + s - n % j]; n     for (int i = s; i <= n + s; i += j) { n       int t = p[i]; n       p[i] = curr; n       curr = t; n     } n     ++s; n   } n   for (int i = s; i < n + s; ++i) { n     cout << p[i] <<    ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m, k; n bool debug = false; n int dp[1005][1005]; n char a[1005], b[1005]; n int main() { n   scanf( %s%s , a + 1, b + 1); n   int n = strlen(a + 1); n   int m = strlen(b + 1); n   for (int i = 1; i <= n; i++) dp[i][0] = i; n   for (int i = 1; i <= m; i++) dp[0][i] = i; n   for (int i = 1; i <= n; i++) { n     for (int j = 1; j <= m; j++) { n       dp[i][j] = min(min(dp[i][j - 1], dp[i - 1][j]) + 1, n                      dp[i - 1][j - 1] + (a[i] != b[j])); n     } n   } n   printf( %d n , dp[n][m]); n   while (n != 0 || m != 0) { n     if (m != 0 && dp[n][m] == dp[n][m - 1] + 1) { n       printf( INSERT %d %c n , n + 1, b[m]); n       m--; n     } else if (n != 0 && dp[n][m] == dp[n - 1][m] + 1) { n       printf( DELETE %d n , n); n       n--; n     } else if (a[n--] != b[m--]) { n       printf( REPLACE %d %c n , n + 1, b[m + 1]); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long N = 1e5 + 5; n const long long MOD = 1e9 + 7; n long long n, m, cnt = 0; n long long a[N], comp[N], vis[N]; n vector<long long> g[N], rg[N], v[N]; n vector<long long> todo; n void dfs(long long k) { n   vis[k] = 1; n   for (auto it : g[k]) { n     if (!vis[it]) { n       dfs(it); n     } n   } n   todo.push_back(k); n } n void dfs2(long long k, long long val) { n   comp[k] = val; n   v[val].push_back(a[k]); n   for (long long w : rg[k]) { n     if (comp[w] == -1) { n       dfs2(w, val); n     } n   } n } n void sccAddE(long long from, long long to) { n   g[from].push_back(to); n   rg[to].push_back(from); n } n void scc() { n   for (long long i = 1; i <= n; i++) { n     comp[i] = -1; n   } n   for (long long i = 1; i <= n; i++) { n     if (!vis[i]) dfs(i); n   } n   reverse(todo.begin(), todo.end()); n   for (long long i : todo) { n     if (comp[i] == -1) { n       dfs2(i, i); n       cnt++; n     } n   } n } n int32_t main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   ; n   cin >> n; n   for (long long i = 1; i <= n; i++) cin >> a[i]; n   cin >> m; n   for (long long i = 1; i <= m; i++) { n     long long u, v; n     cin >> u >> v; n     sccAddE(u, v); n   } n   scc(); n   long long ans1 = 0; n   long long ans2 = 1; n   for (long long i = 1; i <= n; i++) { n     if (!v[i].size()) continue; n     sort(v[i].begin(), v[i].end()); n     long long min1 = v[i][0]; n     ans1 += min1; n     cnt = 0; n     for (auto it : v[i]) n       if (it == min1) cnt++; n     ans2 *= cnt; n     ans2 %= MOD; n   } n   cout << ans1 <<     << ans2; n } n


#include <bits/stdc++.h> n #pragma GCC target( avx2 ) n #pragma GCC optimization( O3 ) n #pragma GCC optimization( unroll-loops ) n using namespace std; n unsigned long long min(unsigned long long x, unsigned long long y) { n   if (x < y) return x; n   return y; n } n unsigned long long max(unsigned long long x, unsigned long long y) { n   if (x < y) return y; n   return x; n } n long long min(long long x, long long y) { n   if (x < y) return x; n   return y; n } n long long max(long long x, long long y) { n   if (x < y) return y; n   return x; n } n double min(double x, double y) { n   if (x < y) return x; n   return y; n } n double max(double x, double y) { n   if (x < y) return y; n   return x; n } n unsigned long long gcd(unsigned long long x, unsigned long long y) { n   if (!x) return y; n   if (!y) return x; n   if (x > y) swap(x, y); n   return gcd(x, y % x); n } n unsigned long long inv(unsigned long long a, unsigned long long c) { n   if (a == 1) { n     return 1; n   } n   return ((c - (c / a)) * inv(c % a, c)) % c; n } n void printv(vector<int> &a) { n   int n = a.size(); n   for (int i = 0; i < n; ++i) { n     cout << a[i] <<    ; n   } n   cout <<   END n ; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   int T; n   cin >> T; n   for (int t = 1; t <= T; ++t) { n     int n; n     cin >> n; n     int a[n + 1]; n     for (int i = 1; i <= n; ++i) { n       cin >> a[i]; n     } n     bool sorted = true; n     for (int i = 1; i < n; ++i) { n       if (a[i] > a[i + 1]) sorted = false; n     } n     int ans = 0; n     while (!sorted) { n       ans++; n       if (ans & 1) { n         for (int i = 1; i <= n - 2; i += 2) { n           if (a[i] > a[i + 1]) { n             swap(a[i], a[i + 1]); n           } n         } n       } else { n         for (int i = 2; i <= n - 1; i += 2) { n           if (a[i] > a[i + 1]) { n             swap(a[i], a[i + 1]); n           } n         } n       } n       sorted = true; n       for (int i = 1; i < n; ++i) { n         if (a[i] > a[i + 1]) sorted = false; n       } n     } n     cout << ans <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, m; n   cin >> n >> m; n   char a[n][m]; n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < m; j++) { n       cin >> a[i][j]; n     } n   } n   int str1 = n; n   int str2 = -1; n   int st1 = m; n   int st2 = -1; n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < m; j++) { n       if (a[i][j] ==  B ) { n         if (str1 > i) { n           str1 = i; n         } n         if (str2 < i) { n           str2 = i; n         } n         if (st1 > j) { n           st1 = j; n         } n         if (st2 < j) { n           st2 = j; n         } n       } n     } n   } n   if (str2 == -1) { n     cout << 1; n   } else { n     if (str2 - str1 + 1 > m || st2 - st1 + 1 > n) { n       cout << -1; n     } else { n       int b = 0; n       for (int i = 0; i < n; i++) { n         for (int j = 0; j < m; j++) { n           if (a[i][j] ==  B ) { n             b++; n           } n         } n       } n       int ma = max(str2 - str1 + 1, st2 - st1 + 1); n       cout << ma * ma - b; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   vector<long long> no; n   long long int n, m, s = 0, a[100000], l = 1, i; n   cin >> n >> m; n   for (i = 1; i <= n; i++) cin >> a[i]; n   sort(a + 1, a + 1 + n); n   for (i = 1; i <= m; i++) { n     if (a[l] == i) n       l++; n     else { n       s += i; n       if (s <= m) n         no.push_back(i); n       else n         break; n     } n   } n   cout << no.size() << endl; n   for (int j = 0; j < no.size(); j++) cout << no[j] <<    ; 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 t; n   cin >> t; n   while (t--) { n     long long n, k; n     cin >> n >> k; n     long long total = n * k; n     std::vector<long long> v(total); n     for (long long i = 0; i < total; i++) { n       cin >> v[i]; n     } n     long long med; n     if (n % 2 == 0) { n       med = n / 2; n     } else { n       med = n / 2 + 1; n     } n     sort(v.begin(), v.end()); n     long long sum = 0; n     if (n == 1) { n       for (int i = 0; i < total; i++) { n         sum += v[i]; n       } n     } else { n       for (long long i = total - n + med - 1; i >= ((med - 1) * k) - 1; n            i -= (n - med + 1)) { n         sum += v[i]; n       } n     } n     cout << sum << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long int n; n   cin >> n; n   long long int a[n]; n   long long int sum = 0; n   for (long int i = 0; i < n; i++) scanf( %lld , &a[i]), sum += a[i]; n   long long int ans[n]; n   long long int l = 0, r = 1e12; n   long long int mid = 0; n   while (l <= r) { n     mid = (l + r) / 2; n     long long int temp1 = mid * n; n     long long int temp2 = temp1 + (n * (n - 1)) / 2; n     if (temp1 <= sum && temp2 >= sum) { n       r = mid - 1; n     } n     if (temp2 < sum) n       l = mid + 1; n     else n       r = mid - 1; n   } n   mid = l; n   long long int aa = 0; n   for (long int i = 0; i < n; i++) { n     long long int temp1 = mid * (n - i); n     long long int temp2 = temp1 + ((n - i) * (n - i - 1)) / 2; n     if (temp1 <= sum - aa && temp2 >= sum - aa) n       ans[i] = mid; n     else n       ans[i] = ++mid; n     aa += ans[i]; n   } n   for (long int i = 0; i < n; i++) cout << ans[i] <<    ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 2005; n int n, k; n int a[maxn], b[maxn]; n map<int, bool> MAP; n int main() { n   int ans = 0; n   scanf( %d%d , &k, &n); n   for (int i = 0; i < k; i++) { n     scanf( %d , &a[i]); n   } n   for (int i = 0; i < n; i++) { n     scanf( %d , &b[i]); n   } n   vector<int> p; n   if (k < n) { n     cout << 0 << endl; n     return 0; n   } n   for (int i = 0; i < k; i++) { n     MAP.clear(); n     for (int i = 1; i < n; i++) { n       MAP[b[i]] = true; n     } n     bool ok = true; n     int t = b[0]; n     for (int j = i; j > 0; j--) { n       t -= a[j]; n       MAP.find(t)->second = false; n     } n     int m = t - a[0]; n     t = b[0]; n     for (int j = i + 1; j < k; j++) { n       t += a[j]; n       MAP.find(t)->second = false; n     } n     for (map<int, bool>::iterator it = MAP.begin(); it != MAP.end(); it++) { n       if (it->second) { n         ok = false; n         break; n       } n     } n     if (ok) { n       p.push_back(m); n     } n   } n   sort(p.begin(), p.end()); n   for (int i = 0; i < p.size(); i++) { n     if (i == p.size() - 1 || p[i] != p[i + 1]) ans++; n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n; n int a[15]; n void f(int fg, int x) { n   if (fg == 1) { n     for (int i = 0; i <= 10; i++) { n       if (((x >> i) & 1) != 0) { n         a[i] = 1; n       } n     } n   } else if (fg == 2) { n     for (int i = 0; i <= 10; i++) { n       if (((x >> i) & 1) == 0) { n         a[i] = 0; n       } n     } n   } else { n     for (int i = 0; i <= 10; i++) { n       if (((x >> i) & 1) != 0) { n         if (a[i] == 0 || a[i] == 1) n           a[i] ^= 1; n         else n           a[i] = -a[i]; n       } n     } n   } n } n int main() { n   while (~scanf( %d , &n)) { n     for (int i = 0; i <= 10; i++) a[i] = 5; n     for (int i = 0; i < n; i++) { n       char ch; n       int tp; n       getchar(); n       scanf( %c , &ch); n       scanf( %d , &tp); n       if (ch ==  | ) n         f(1, tp); n       else if (ch ==  & ) n         f(2, tp); n       else n         f(3, tp); n     } n     int ansor = 0; n     int ansand = 1023; n     int ansxor = 0; n     for (int i = 0; i < 10; i++) { n       if (a[i] == 1) { n         ansor += (1 << i); n       } else if (a[i] == 0) { n         ansand -= (1 << i); n       } else if (a[i] == -5) { n         ansxor += (1 << i); n       } n     } n     printf( 3 n ); n     printf( | %d n , ansor); n     printf( & %d n , ansand); n     printf( ^ %d n , ansxor); n   } n } n


#include <bits/stdc++.h> n using namespace std; n const long long limita = 998244353; n long long n, v[200], st, dr, mij, mul, ans, maxDegree; n long long GCD(long long x, long long y) { n   if (y != 0) n     return GCD(y, x % y); n   else n     return x; n } n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   cin >> n; n   for (int i = 1; i <= n; i++) { n     cin >> v[i]; n   } n   for (int i = 1; i <= n; i++) { n     ans = 180 / GCD(180LL, v[i]); n     mul = 0; n     st = 1; n     dr = limita / ans; n     while (st <= dr) { n       mij = (st + dr) / 2; n       maxDegree = (mij * ans - 2) * 180 / (mij * ans); n       if (maxDegree >= v[i]) { n         dr = mij - 1; n         mul = mij; n       } else n         st = mij + 1; n     } n     if (mul == 0) n       cout << -1 <<   n ; n     else n       cout << mul * ans <<   n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int n, arr[200000 + 5], x[200000 + 5], ada[35]; n int main() { n   scanf( %d , &n); n   for (int i = 0; i < n; i++) { n     scanf( %d , &arr[i]); n     if (i > 0) n       x[i] = x[i - 1] ^ arr[i]; n     else n       x[i] = arr[i]; n   } n   if (x[n - 1] == 0) n     printf( -1 n ); n   else { n     for (int i = 0; i < n; i++) { n       for (int j = 30; j >= 0; j--) { n         if (x[i] & (1 << j)) { n           if (!ada[j]) { n             ada[j] = x[i]; n             break; n           } else n             x[i] ^= ada[j]; n         } n       } n     } n     int res = 0; n     for (int i = 0; i <= 30; i++) { n       if (ada[i]) { n         res++; n       } n     } n     printf( %d n , res); n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long int l, r, x, y, k; n   cin >> l >> r >> x >> y >> k; n   for (long long int i = x; i <= y; ++i) { n     if (k * i >= l && k * i <= r) { n       cout <<  YES  << endl; n       exit(0); n     } n   } n   cout <<  NO  << endl; n } n


#include <bits/stdc++.h> n using namespace std; n void solutionC(); n int main() { n   solutionC(); n   return EXIT_SUCCESS; n } n int n, a[500002], b[500002]; n bool check() { n   for (int i = 1; i < n - 1; ++i) { n     int s = a[i] + a[i - 1] + a[i + 1]; n     if (s == 2 && a[i] != 1) return false; n     if (s == 1 && a[i] != 0) return false; n   } n   return true; n } n void change() { n   b[0] = a[0]; n   b[n - 1] = a[n - 1]; n   for (int i = 1; i < n - 1; ++i) { n     int s = a[i] + a[i - 1] + a[i + 1]; n     if (s == 2 && a[i] != 1) { n       b[i] = 1; n       continue; n     } n     if (s == 1 && a[i] != 0) { n       b[i] = 0; n       continue; n     } n     b[i] = a[i]; n   } n   for (int i = 0; i < n; ++i) a[i] = b[i]; n } n void out() { n   for (int i = 0; i < n; ++i) { n     if (i) cout <<    ; n     cout << a[i]; n   } n   cout << endl; n } n void solve() { n   int l = 1; n   vector<pair<int, int>> e; n   for (int i = 1; i < n; ++i) { n     if (a[i] != a[i - 1]) { n       ++l; n     } else { n       if (l >= 3) e.emplace_back(i - l, i - 1); n       l = 1; n     } n   } n   if (l >= 3) e.emplace_back(n - l, n - 1); n   int ans = 0; n   for (auto it : e) { n     int d = it.second - it.first; n     if (d % 2 == 0) { n       ans = max(ans, d / 2); n       assert(a[it.first] == a[it.second]); n       for (int i = it.first; i <= it.second; ++i) a[i] = a[it.first]; n     } else { n       assert(a[it.first] != a[it.second]); n       ans = max(ans, (d - 1) / 2); n       for (int i = it.first; i <= it.second; ++i) n         if (i - it.first < it.second - i) n           a[i] = a[it.first]; n         else n           a[i] = a[it.second]; n     } n   } n   cout << ans << endl; n   out(); n } n void bf() { n   for (int l = 3; l < 24; ++l) { n     n = l; n     int mx = 0; n     cout <<  n:   << l << endl; n     for (int bm = 0; bm < (1 << l); ++bm) { n       for (int i = 0; i < n; ++i) a[i] = (bm & (1 << i)) > 0; n       int cnt = 0; n       while (!check()) { n         cnt++; n         change(); n         if (cnt > n * n * 2) { n           cout <<  fnd:  ; n           out(); n           break; n         } n       } n       mx = max(mx, cnt); n     } n     cout <<  mx:   << mx << endl; n   } n } n void solutionC() { n   cin >> n; n   for (int i = 0; i < n; ++i) cin >> a[i]; n   solve(); n } n


#include <bits/stdc++.h> n using namespace std; n struct node { n   int hx, hy; n   int tx, ty; n   int dep; n   long dir; n   int zt[15]; n }; n queue<node> q; n int n, m, len; n int dx[4] = {-1, 0, 1, 0}, dy[4] = {0, 1, 0, -1}; n bool v[15][15][262144]; n char c[30][30]; n bool init(int a, int b) { n   if (a < 0 || a >= n || b < 0 || b >= m) return 0; n   return 1; n } n int update(int d, int nd) { n   d = d | (nd << ((len - 1) * 2)); n   d >>= 2; n   return d; n } n void out(int d) { n   for (int i = 1; i < len; i++) { n     int t = d % 4; n     if (t == 0) cout <<  w ; n     if (t == 1) cout <<  d ; n     if (t == 2) cout <<  s ; n     if (t == 3) cout <<  a ; n     d /= 4; n   } n } n void zut(int x) { n   for (int i = 1; i <= m; i++) { n     cout << x % 2; n     x /= 2; n   } n } n int ax, ay; n node fst; n int main() { n   cin >> n >> m; n   int fhx, fhy, ftx, fty; n   for (int i = 0; i < n; i++) n     for (int j = 0; j < m; j++) { n       cin >> c[i][j]; n       if (c[i][j] >=  1  && c[i][j] <=  9 ) len = max(len, c[i][j] -  0 ); n       if (c[i][j] ==  1 ) { n         fhx = i; n         fhy = j; n       } n       if (c[i][j] == len +  0 ) { n         ftx = i; n         fty = j; n       } n       if (c[i][j] ==  @ ) { n         ax = i; n         ay = j; n       } n     } n   int fdir = 0; n   for (int i = 0; i < n; i++) n     for (int j = 0; j < m; j++) n       if (c[i][j] >=  1  && c[i][j] <=  9 ) { n         for (int k = 0; k < 4; k++) { n           int nx = i + dx[k], ny = j + dy[k]; n           if (init(nx, ny) && c[nx][ny] == c[i][j] - 1) { n             int nk = k; n             fdir = fdir | (nk << ((len - (c[i][j] -  0 )) * 2)); n             break; n           } n         } n         fst.zt[i] |= (1 << j); n       } n   fst.dir = fdir, fst.hx = fhx, fst.hy = fhy, fst.tx = ftx, fst.ty = fty, n   fst.dep = 0; n   q.push(fst); n   while (!q.empty()) { n     node f = q.front(); n     q.pop(); n     if (f.hx == ax && f.hy == ay) { n       cout << f.dep; n       return 0; n     } else { n       for (int i = 0; i < 4; i++) { n         int nx = f.hx + dx[i], ny = f.hy + dy[i]; n         if (!init(nx, ny)) continue; n         if (c[nx][ny] ==  # ) continue; n         if (f.zt[nx] & (1 << ny) && !(nx == f.tx && ny == f.ty)) continue; n         node nf = f; n         nf.hx = nx; n         nf.hy = ny; n         nf.tx = dx[f.dir & (3)] + f.tx; n         nf.ty = dy[f.dir & (3)] + f.ty; n         nf.zt[f.tx] ^= (1 << f.ty); n         nf.zt[nx] |= (1 << ny); n         nf.dir = update(f.dir, i); n         nf.dep++; n         if (v[nx][ny][nf.dir]) continue; n         v[nx][ny][nf.dir] = 1; n         q.push(nf); n       } n     } n   } n   cout << -1; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n using ll = long long; n using vb = vector<bool>; n using vvb = vector<vb>; n using vi = vector<int>; n using vvi = vector<vi>; n using vl = vector<ll>; n using vvl = vector<vl>; n using vc = vector<char>; n using vvc = vector<vc>; n using vs = vector<string>; n const ll mod = 1e9 + 7, inf = 1e18; n void setIO() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   ; n } n int main() { n   setIO(); n   int t; n   cin >> t; n   while (t--) { n     ll n; n     cin >> n; n     ll m = 1; n     for (int i = 3; i <= 2 * n; i++) m = m * i % 1000000007; n     cout << m << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   cin >> n; n   int ara[n]; n   int temp = n; n   int k = 0; n   while (n--) { n     cin >> ara[k++]; n   } n   sort(ara, ara + temp); n   queue<int> Q; n   for (int i = 0; i < temp; i++) { n     Q.push(ara[i]); n   } n   int i; n   for (i = 1; i < 1000000; i++) { n     while (!Q.empty() && Q.front() < i) { n       Q.pop(); n     } n     if (Q.empty()) break; n     Q.pop(); n   } n   cout << i << endl; n   return 0; n } n