#include <bits/stdc++.h> n using namespace std; n void run(vector<vector<int>> &sets, int **sum); n pair<vector<vector<int>>, int **> precalc(); n const int MAXB = 5; n const int MOD = 1e9 + 7; n int main() { n   ios::sync_with_stdio(false); n   cout.tie(0); n   auto precalc_vals = precalc(); n   run(precalc_vals.first, precalc_vals.second); n } n template <typename type> n type *farray(int n) { n   return (type *)malloc(n * sizeof(type)); n } n template <typename type> n type *farray(int n, type val) { n   auto res = farray<type>(n); n   fill(res, res + n, val); n   return res; n } n template <typename type> n type **dim2_farray(int n, int m) { n   auto res = farray<type *>(n); n   for (int i = 0; i < n; i++) { n     res[i] = farray<type>(m); n   } n   return res; n } n template <typename type> n type **dim2_farray(int n, int m, type val) { n   auto res = farray<type *>(n); n   for (int i = 0; i < n; i++) { n     res[i] = farray<type>(m, val); n   } n   return res; n } n template <typename type> n void dim2_free(type *arr, int n) { n   for (int i = 0; i < n; i++) free(arr[i]); n   free(arr); n } n bool gauss(bool **matrix, bool *vec, int n, int m) { n   if (m > n) return false; n   for (int k = 0; k < m; k++) { n     int st = k; n     for (; st < n && matrix[st][k] == 0; st++) n       ; n     if (st == n) return false; n     for (int c = 0; c < m; c++) { n       swap(matrix[st][c], matrix[k][c]); n     } n     swap(vec[st], vec[k]); n     for (int r = st + 1; r < n; r++) { n       if (matrix[r][k] == 0) continue; n       for (int c = k; c < m; c++) { n         matrix[r][c] -= matrix[k][c]; n       } n       vec[r] -= vec[k]; n     } n   } n   for (int k = m - 1; k >= 0; k--) { n     for (int r = k - 1; r >= 0; r--) { n       if (matrix[r][k] == 0) continue; n       matrix[r][k] -= 1; n       vec[r] -= vec[k]; n     } n   } n   for (int k = m; k < n; k++) { n     bool val = 0; n     for (int c = 0; c < m; c++) { n       val += matrix[k][c] * vec[c]; n     } n     if (val != vec[k]) return false; n   } n   return true; n } n bool **to_matrix(const vector<int> &a) { n   int len = (int)a.size(); n   auto matrix = dim2_farray<bool>(MAXB, len); n   for (int i = 0; i < len; i++) { n     for (int b = 0; b < MAXB; b++) { n       matrix[b][i] = (a[i] & (1 << b)) > 0; n     } n   } n   return matrix; n } n bool indep(const vector<int> &a) { n   int len = (int)a.size(); n   auto matrix = to_matrix(a); n   auto vec = farray<bool>(MAXB, 0); n   return gauss(matrix, vec, MAXB, len); n } n bool *bin(int a) { n   auto res = farray<bool>(MAXB); n   for (int i = 0; i < MAXB; i++) { n     res[i] = a & (1 << i); n   } n   return res; n } n bool the_same(const vector<int> &a, const vector<int> &b) { n   if (a.size() != b.size()) return false; n   int len = b.size(); n   for (int i = 0; i < len; i++) { n     auto a_matrix = to_matrix(a); n     if (!gauss(a_matrix, bin(b[i]), MAXB, len)) { n       dim2_free(a_matrix, MAXB); n       return false; n     } n     dim2_free(a_matrix, MAXB); n   } n   return true; n } n void precalc_sets(int from, vector<vector<int>> &ans, vector<int> &cur, n                   int k = 0) { n   cur.push_back(0); n   for (int i = from; i < (1 << MAXB); i++) { n     cur.back() = i; n     if (indep(cur)) { n       bool new_one = true; n       for (auto &v : ans) { n         if (the_same(cur, v)) { n           new_one = false; n           break; n         } n       } n       if (new_one) { n         ans.push_back(cur); n         precalc_sets(i + 1, ans, cur, k + 1); n       } n     } n   } n   cur.pop_back(); n } n int set_id(vector<int> &basis, vector<vector<int>> &sets) { n   for (int i = 0; i < (int)sets.size(); i++) { n     if (the_same(basis, sets[i])) { n       return i; n     } n   } n   return -1; n } n int **precalc_sums(vector<vector<int>> &sets) { n   int n = (int)sets.size(); n   auto res = dim2_farray<int>(n, n); n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < n; j++) { n       vector<int> cur = sets[i]; n       cur.insert(cur.end(), sets[j].begin(), sets[j].end()); n       if (indep(cur)) { n         res[i][j] = set_id(cur, sets); n       } else { n         res[i][j] = -1; n       } n     } n   } n   return res; n } n pair<vector<vector<int>>, int **> precalc() { n   vector<vector<int>> sets(1); n   vector<int> cur; n   precalc_sets(0, sets, cur); n   auto sums = precalc_sums(sets); n   return {sets, sums}; n } n struct Graph { n   int *head, *nxt, *eto, *cost, eto_ptr = 0; n   Graph(int n, int m) { n     head = farray<int>(n, -1); n     nxt = farray<int>(m); n     eto = farray<int>(m); n     cost = farray<int>(m); n   } n   void add_edge(int from, int to, int c) { n     eto[eto_ptr++] = to; n     nxt[eto_ptr - 1] = head[from]; n     head[from] = eto_ptr - 1; n     cost[eto_ptr - 1] = c; n   } n }; n void get_basis(int v, Graph &graph, vector<bool> &used, int *path_xor, n                vector<int> &basis, int p = -1) { n   used[v] = true; n   for (int e = graph.head[v]; e != -1; e = graph.nxt[e]) { n     int u = graph.eto[e]; n     if (u == p || u == 1) continue; n     if (!used[u]) { n       path_xor[u] = path_xor[v] ^ graph.cost[e]; n       get_basis(u, graph, used, path_xor, basis, v); n     } else if (u < v) { n       basis.push_back(path_xor[v] ^ path_xor[u] ^ graph.cost[e]); n     } n   } n } n void run(vector<vector<int>> &sets, int **sums) { n   int n, m; n   cin >> n >> m; n   Graph graph(n + 1, m * 2); n   for (int i = 0; i < m; i++) { n     int u, v, c; n     cin >> u >> v >> c; n     graph.add_edge(u, v, c); n     graph.add_edge(v, u, c); n   } n   auto near_one = farray<int>(n + 1, -1); n   for (int e = graph.head[1]; e != -1; e = graph.nxt[e]) { n     int u = graph.eto[e]; n     near_one[u] = graph.cost[e]; n   } n   vector<bool> used(n + 1); n   used[1] = true; n   vector<vector<int>> dims = {{}}; n   auto path_xor = farray<int>(n + 1, 0); n   for (int v = 2; v <= n; v++) { n     if (used[v] || near_one[v] == -1) continue; n     int u = -1, c = -1; n     for (int e2 = graph.head[v]; e2 != -1; e2 = graph.nxt[e2]) { n       u = graph.eto[e2]; n       c = graph.cost[e2]; n       if (near_one[u] != -1) { n         break; n       } n       u = -1; n     } n     vector<int> basis; n     get_basis(v, graph, used, path_xor, basis); n     if (!indep(basis)) continue; n     dims.push_back({set_id(basis, sets)}); n     if (u != -1) { n       dims.back().push_back(dims.back().back()); n       basis.push_back(near_one[v] ^ near_one[u] ^ c); n       if (indep(basis)) { n         dims.back().push_back(set_id(basis, sets)); n       } n     } n   } n   int p = sets.size(); n   int k = dims.size(); n   auto dp = dim2_farray<int>(k, p, 0); n   dp[0][0] = 1; n   for (int i = 1; i < k; i++) { n     for (int d = 0; d < p; d++) { n       dp[i][d] = dp[i - 1][d]; n     } n     for (int d = 0; d < p; d++) { n       for (int a : dims[i]) { n         if (sums[d][a] == -1) continue; n         int new_dim = sums[d][a]; n         dp[i][new_dim] = (dp[i][new_dim] + dp[i - 1][d]) % MOD; n       } n     } n   } n   int ans = 0; n   for (int i = 0; i < p; i++) { n     ans = (ans + dp[k - 1][i]) % MOD; n   } n   cout << ans << endl; n } n


#include <bits/stdc++.h> n using namespace std; n struct T { n   int p, n; n }; n bool operator<(T x, T y) { return x.p < y.p; } n T m[100100]; n int main() { n   int i, n, j; n   for (scanf( %d , &n), i = 0; i < n; scanf( %d%d , &m[i].p, &m[i].n), i++) n     ; n   sort(m, m + n); n   j = m[n - 1].n; n   for (i = 0; i < n - 1; i++) n     if (m[i + 1].p - m[i].p > 40) n       continue; n     else { n       for (; m[i].p < m[i + 1].p; m[i].n = (m[i].n + 3) / 4, m[i].p++) n         ; n       if (m[i].n > m[i + 1].n) m[i + 1].n = m[i].n; n     } n   if (m[n - 1].n == j && j == 1) n     printf( %d n , m[n - 1].p + 1); n   else { n     for (; m[i].n > 1; m[i].n = (m[i].n + 3) / 4, m[i].p++) n       ; n     printf( %d n , m[i].p); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n inline long long read() { n   long long x = 0; n   int ch = getchar(), f = 1; n   while (!isdigit(ch) && (ch !=  - ) && (ch != EOF)) ch = getchar(); n   if (ch ==  - ) { n     f = -1; n     ch = getchar(); n   } n   while (isdigit(ch)) { n     x = (x << 1) + (x << 3) + ch -  0 ; n     ch = getchar(); n   } n   return x * f; n } n const int N = 5e5 + 10, mod = 1e9 + 7; n int n, ans; n pair<int, int> a[N]; n inline int Mod(int x) { return x >= mod ? x - mod : x; } n inline void upd(int &x, int y) { n   x += y, x = (x >= mod ? x - mod : (x < 0 ? x + mod : x)); n } n struct SegmentTree { n   int v[N << 2], lazy[N << 2]; n   inline void push_up(int u) { v[u] = Mod(v[u << 1] + v[u << 1 ^ 1]); } n   inline void push_down(int u, int l, int mid, int r) { n     if (!lazy[u]) return; n     upd(lazy[u << 1], lazy[u]), upd(lazy[u << 1 ^ 1], lazy[u]); n     upd(v[u << 1], 1ll * lazy[u] * (mid - l + 1) % mod), n         upd(v[u << 1 ^ 1], 1ll * lazy[u] * (r - mid) % mod); n     lazy[u] = 0; n   } n   inline void update(int u, int l, int r, int ql, int qr) { n     if (l >= ql && r <= qr) n       return upd(v[u], r - l + 1), upd(lazy[u], 1), void(0); n     int mid = l + r >> 1; n     push_down(u, l, mid, r); n     if (qr <= mid) n       update(u << 1, l, mid, ql, qr); n     else if (ql > mid) n       update(u << 1 ^ 1, mid + 1, r, ql, qr); n     else n       update(u << 1, l, mid, ql, qr), update(u << 1 ^ 1, mid + 1, r, ql, qr); n     push_up(u); n   } n   inline int Query(int u, int l, int r, int ql, int qr) { n     if (l >= ql && r <= qr) return v[u]; n     int mid = l + r >> 1; n     push_down(u, l, mid, r); n     if (qr <= mid) n       return Query(u << 1, l, mid, ql, qr); n     else if (ql > mid) n       return Query(u << 1 ^ 1, mid + 1, r, ql, qr); n     else n       return Mod(Query(u << 1, l, mid, ql, qr) + n                  Query(u << 1 ^ 1, mid + 1, r, ql, qr)); n   } n } t[2]; n int main() { n   n = read(); n   for (register int i = (1); i <= (n); i++) a[i] = make_pair(read(), i); n   sort(a + 1, a + 1 + n); n   for (register int i = (1); i <= (n); i++) { n     int x = t[0].Query(1, 1, n, a[i].second, a[i].second), n         sum = t[0].Query(1, 1, n, 1, a[i].second); n     int y = t[1].Query(1, 1, n, a[i].second, a[i].second), n         Sum = t[1].Query(1, 1, n, a[i].second, n); n     upd(ans, 1ll * a[i].first * n                  (sum - 1ll * x * a[i].second % mod + a[i].second) % mod * n                  (n - a[i].second + 1) % mod); n     upd(ans, 1ll * a[i].first * (Sum - 1ll * y * (n - a[i].second + 1) % mod) % n                  mod * a[i].second % mod); n     t[0].update(1, 1, n, 1, a[i].second), t[1].update(1, 1, n, a[i].second, n); n   } n   printf( %d n , ans); n } n


#include <bits/stdc++.h> n using namespace std; n vector<pair<long long int, long long int> > v1, v2; n int main() { n   long long int p, q, l, r, w, x, y, z, i, j, flag, tot = 0, k; n   scanf( %lld , &p); n   scanf( %lld , &q); n   scanf( %lld , &l); n   scanf( %lld , &r); n   for (i = 0; i < p; i++) { n     scanf( %lld , &x); n     scanf( %lld , &y); n     v1.push_back(make_pair(x, y)); n   } n   for (i = 0; i < q; i++) { n     scanf( %lld , &x); n     scanf( %lld , &y); n     v2.push_back(make_pair(x, y)); n   } n   for (i = l; i <= r; i++) { n     flag = 0; n     for (j = 0; j < v1.size(); j++) { n       for (k = 0; k < v2.size(); k++) { n         if (v1[j].second < v2[k].first + i || v1[j].first > v2[k].second + i) n           ; n         else n           flag = 1; n       } n     } n     if (flag == 1) tot++; n   } n   cout << tot << endl; n   return 0; n } n


#include <bits/stdc++.h> n int main() { n   int i, j, p; n   char a[100010]; n   while (~scanf( %s , a)) { n     p = 0; n     for (i = 0, j = strlen(a) - 1; i < j; i++, j--) { n       if (a[i] != a[j]) { n         p = 1; n         break; n       } n     } n     if (p == 1) n       printf( NO n ); n     else { n       for (i = 0; i < strlen(a); i++) { n         if (a[i] !=  A  && a[i] !=  H  && a[i] !=  I  && a[i] !=  M  && n             a[i] !=  O  && a[i] !=  T  && a[i] !=  V  && a[i] !=  U  && n             a[i] !=  W  && a[i] !=  X  && a[i] !=  Y ) { n           p = 1; n           break; n         } n       } n       if (p == 1) n         printf( NO n ); n       else n         printf( YES n ); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   int n; n   cin >> n; n   string x; n   int ans = 1; n   vector<string> a; n   for (int i = 0; i < n; i++) { n     cin >> x; n     a.push_back(x); n   } n   for (int i = 0; i < n - 1; ++i) { n     if (a[i] != a[i + 1]) ans++; n   } n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<int> a; n int n; n bool check() { n   for (int i = 1; i < n; i++) { n     if (abs(a[i - 1] - a[i]) >= 2) return 1; n   } n   return 0; n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cin >> n; n   a.resize(n); n   for (int i = 0; i < n; i++) { n     cin >> a[i]; n   } n   if (check()) { n     cout <<  NO ; n     return 0; n   } n   int last = -1; n   for (int i = 0; i < n; i++) { n     int mx = -1, mxj = 0; n     for (int j = 0; j < n; j++) { n       if (mx < a[j]) { n         mx = a[j]; n         mxj = j; n       } n     } n     if (last == -1) last = mx; n     if (mx == -1) break; n     if (abs(last - mx) >= 2) { n       cout <<  NO ; n       return 0; n     } n     a[mxj] = -1; n     last = mx; n   } n   cout <<  YES ; n } n


#include <bits/stdc++.h> n const int maxn = 2e5 + 1; n int fa[maxn], flg[maxn], n, m, cnt, ans; n struct Edge { n   int u, v, w; n } e[maxn]; n inline bool cmp(Edge a, Edge b) { return a.w > b.w; } n inline int find(int x) { return (fa[x] == x) ? x : fa[x] = find(fa[x]); } n int main() { n   scanf( %d %d , &n, &m); n   for (int i = 1; i <= m; i++) scanf( %d %d %d , &e[i].u, &e[i].v, &e[i].w); n   for (int i = 1; i <= n; i++) fa[i] = i; n   std::sort(e + 1, e + m + 1, cmp); n   for (int i = 1; i <= m; i++) { n     int fx = find(e[i].u), fy = find(e[i].v); n     if (fx == fy) { n       if (flg[fx]) continue; n       ans += e[i].w; n       flg[fx] = 1; n     } n     if (flg[fx] && flg[fy]) continue; n     if (flg[fx]) n       fa[fy] = fx; n     else if (flg[fy]) n       fa[fx] = fy; n     else n       fa[fx] = fy; n     ans += e[i].w; n   } n   printf( %d , ans); n } n


#include <bits/stdc++.h> n using namespace std; n int read() { n   char c; n   int w = 1; n   while ((c = getchar()) >  9  || c <  0 ) n     if (c ==  - ) w = -1; n   int ans = c -  0 ; n   while ((c = getchar()) >=  0  && c <=  9 ) n     ans = (ans << 1) + (ans << 3) + c -  0 ; n   return ans * w; n } n const int xx = 2.4e5 + 5; n vector<int> v[xx]; n int n, mn[xx << 2], ad[xx << 2], a[xx]; n void add(int x, int y) { mn[x] += y, ad[x] += y; } n void pd(int k) { n   if (!ad[k]) return; n   add(k << 1, ad[k]); n   add(k << 1 | 1, ad[k]); n   ad[k] = 0; n } n int ask(int k, int l, int r) { n   if (l == r) return l; n   pd(k); n   int mid = l + r >> 1; n   if (mn[k] == mn[k << 1 | 1]) return ask(k << 1 | 1, mid + 1, r); n   return ask(k << 1, l, mid); n } n void ch(int k, int l, int r, int x, int y) { n   if (l == r) return mn[k] = y, void(); n   pd(k); n   int mid = l + r >> 1; n   if (x <= mid) n     ch(k << 1, l, mid, x, y); n   else n     ch(k << 1 | 1, mid + 1, r, x, y); n   mn[k] = min(mn[k << 1], mn[k << 1 | 1]); n } n void ch(int k, int l, int r, int x, int y, int z) { n   if (x > y) return; n   if (x <= l && r <= y) return add(k, z); n   pd(k); n   int mid = l + r >> 1; n   if (x <= mid) ch(k << 1, l, mid, x, y, z); n   if (mid < y) ch(k << 1 | 1, mid + 1, r, x, y, z); n   mn[k] = min(mn[k << 1], mn[k << 1 | 1]); n } n struct pr { n   int x, y; n   pr() {} n   pr(int a, int b) : x(a), y(b) {} n }; n pr s1[xx], s2[xx]; n int L[xx], R[xx], s3[xx], vis[xx], t1, t2, t3; n int f[xx][20], dep[xx], LL[xx], RR[xx], lg[xx]; n long long r[xx], l[xx]; n long long sum[xx], val[xx]; n long long rf[xx][20], lf[xx][20], S[xx]; n long long get(long long x) { return x * (x - 1) / 2; } n int id[xx]; n void dfs(int x, int y) { n   if (x <= n) n     LL[x] = x, RR[x] = x; n   else n     LL[x] = 1e9, RR[x] = -1e9; n   val[x] = 1; n   if (!vis[x] && x > n) val[x] += get(v[x].size()) - 1; n   f[x][0] = y; n   dep[x] = dep[y] + 1; n   for (int i = 1; i < 20; i++) f[x][i] = f[f[x][i - 1]][i - 1]; n   for (int i = 0; i < v[x].size(); i++) n     dfs(v[x][i], x), LL[x] = min(LL[x], LL[v[x][i]]), n                      RR[x] = max(RR[x], RR[v[x][i]]), sum[x] += sum[v[x][i]]; n   sum[x] += val[x]; n   for (int i = 0; i < v[x].size(); i++) l[i] = r[i] = 0; n   for (int i = 0; i < v[x].size(); i++) { n     id[v[x][i]] = i; n     if (i) n       l[i] = l[i - 1], l[i] += sum[v[x][i - 1]], S[v[x][i]] = S[v[x][i - 1]]; n     S[v[x][i]] += sum[v[x][i]]; n     if (!vis[x]) l[i] += get(i); n     lf[v[x][i]][0] = l[i]; n     if (!vis[x]) l[i] -= get(i); n   } n   for (int i = v[x].size() - 1; i >= 0; i--) { n     if (i != v[x].size() - 1) r[i] = r[i + 1], r[i] += sum[v[x][i + 1]]; n     if (!vis[x]) r[i] += get(v[x].size() - 1 - i); n     rf[v[x][i]][0] = r[i]; n     if (!vis[x]) r[i] -= get(v[x].size() - 1 - i); n   } n } n pr lca(int x, int y) { n   if (dep[x] < dep[y]) swap(x, y); n   while (dep[x] != dep[y]) x = f[x][lg[dep[x] - dep[y]]]; n   for (int i = lg[x]; i >= 0; i--) n     (f[x][i] ^ f[y][i]) ? (x = f[x][i], y = f[y][i]) : 0; n   return pr(x, y); n } n int main() { n   lg[0] = -1; n   for (int i = 1; i < xx; i++) lg[i] = lg[i - 1] + (i == (i & -i)); n   memset(mn, 0x3f, sizeof(mn)); n   n = read(); n   for (int i = 1; i <= n; i++) a[i] = read(); n   for (int i = 1; i <= n; i++) L[i] = R[i] = a[i]; n   int cst = n; n   for (int i = 1; i <= n; i++) { n     int now = i; n     int last = t3 + 1; n     while (t1 && s1[t1].x < a[i]) n       ch(1, 1, n, s1[t1].y, last - 1, -s1[t1].x), last = s1[t1--].y; n     ch(1, 1, n, last, t3, a[i]); n     s1[++t1] = pr(a[i], last); n     last = t3 + 1; n     while (t2 && s2[t2].x > a[i]) n       ch(1, 1, n, s2[t2].y, last - 1, s2[t2].x), last = s2[t2--].y; n     ch(1, 1, n, last, t3, -a[i]); n     s2[++t2] = pr(a[i], last); n     while (mn[1] == i) { n       int id = ask(1, 1, n), p = s3[id]; n       if (id == t3 && !vis[p] && v[p].size() && n           ((R[*--v[p].end()] == R[p] && R[p] + 1 == L[now]) || n            (L[*--v[p].end()] == L[p] && L[p] - 1 == R[now]))) { n         v[p].push_back(now); n         L[p] = min(L[p], L[now]); n         R[p] = max(R[p], R[now]); n         now = p; n         ch(1, 1, n, t3, 1e9); n         t3--; n       } else { n         p = ++cst; n         L[p] = L[now]; n         R[p] = R[now]; n         for (int i = id; i <= t3; i++) { n           v[p].push_back(s3[i]); n           L[p] = min(L[p], L[s3[i]]); n           R[p] = max(R[p], R[s3[i]]); n           ch(1, 1, n, i, 1e9); n         } n         v[p].push_back(now); n         now = p; n         if (t3 - id + 1 > 2) vis[p] = 1; n         t3 = id - 1; n       } n     } n     s3[++t3] = now; n     while (t1 && s1[t1].y > t3) t1--; n     while (t2 && s2[t2].y > t3) t2--; n     ch(1, 1, n, t3, i); n   } n   dfs(s3[1], 0); n   for (int j = 1; j < 20; j++) n     for (int i = 1; i <= cst; i++) { n       lf[i][j] = lf[i][j - 1] + lf[f[i][j - 1]][j - 1]; n       rf[i][j] = rf[i][j - 1] + rf[f[i][j - 1]][j - 1]; n     } n   int m = read(); n   while (m--) { n     int l, r; n     l = read(); n     r = read(); n     if (l == r) n       cout << 1 <<   n ; n     else { n       pr p = lca(l, r); n       if (LL[p.x] > LL[p.y]) swap(p.x, p.y); n       long long x = l, y = r; n       for (int j = 20; j >= 0; j--) n         if (f[x][j] && f[x][j] != s3[1] && LL[f[x][j]] == l) x = f[x][j]; n       for (int j = 20; j >= 0; j--) n         if (f[y][j] && f[y][j] != s3[1] && RR[f[y][j]] == r) y = f[y][j]; n       long long res = 0, num = 0; n       if (dep[x] <= dep[p.x]) n         res += sum[p.x], num++; n       else { n         res += sum[x]; n         if (!vis[f[x][0]]) res += v[f[x][0]].size() - 1 - id[x]; n         while (dep[x] != dep[p.x]) n           res += rf[x][lg[dep[x] - dep[p.x]]], x = f[x][lg[dep[x] - dep[p.x]]]; n       } n       if (dep[y] <= dep[p.y]) n         res += sum[p.y], num++; n       else { n         res += sum[y]; n         if (!vis[f[y][0]]) res += id[y]; n         while (dep[y] != dep[p.y]) n           res += lf[y][lg[dep[y] - dep[p.y]]], y = f[y][lg[dep[y] - dep[p.y]]]; n       } n       if (!vis[f[p.x][0]]) n         num += id[p.y] - id[p.x] - 1, res += get(num); n       else if (LL[f[p.x][0]] == l && RR[f[p.x][0]] == r) n         res++; n       res += S[p.y] - S[p.x] - sum[p.y]; n       cout << res <<   n ; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, k; n   cin >> n >> k; n   vector<long long> a(n); n   for (int i = (0); i < (n); ++i) cin >> a[i]; n   long long sum = 0; n   long long dynamicrank = 0; n   for (int i = (0); i < (n); ++i) { n     long long score = sum - (n - (i + 1)) * dynamicrank * a[i]; n     if (score < k) { n       cout << (i + 1) <<   n ; n     } else { n       sum += a[i] * dynamicrank; n       dynamicrank++; n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 100005, M = 200005, L = 20; n const int INF = 1 << 30; n struct node { n   int u, v, nxt; n } edge[M]; n int head[N], mcnt; n void add_edge(int u, int v) { n   mcnt++; n   edge[mcnt].u = u; n   edge[mcnt].v = v; n   edge[mcnt].nxt = head[u]; n   head[u] = mcnt; n } n vector<int> G[N]; n int dfn[N], tot; n int deep[N]; n int pa[N][L + 1]; n bool Chosen[N]; n int OnVtree[N], cnt; n int S[N], top; n int n, m, k; n int d[N][2]; n void dfs(int u, int fa) { n   dfn[u] = ++tot; n   deep[u] = deep[fa] + 1; n   for (int i = head[u]; i; i = edge[i].nxt) { n     int v = edge[i].v; n     if (v == fa) continue; n     pa[v][0] = u; n     dfs(v, u); n   } n } n void Init() { n   for (int i = 1; i < n; i++) { n     int u, v; n     scanf( %d%d , &u, &v); n     add_edge(u, v); n     add_edge(v, u); n   } n   dfs(1, 0); n   for (int j = 1; j <= L; j++) n     for (int i = 1; i <= n; i++) pa[i][j] = pa[pa[i][j - 1]][j - 1]; n } n int LCA(int x, int y) { n   if (deep[x] < deep[y]) swap(x, y); n   for (int j = L; j >= 0; j--) n     if (deep[pa[x][j]] >= deep[y]) x = pa[x][j]; n   if (x == y) return x; n   for (int j = L; j >= 0; j--) n     if (pa[x][j] != pa[y][j]) x = pa[x][j], y = pa[y][j]; n   return pa[x][0]; n } n struct mode { n   int u, dfn; n } p[N]; n bool cmp(mode a, mode b) { return a.dfn < b.dfn; } n void add_tree(int u, int v) { n   G[u].push_back(v); n   G[v].push_back(u); n } n void Get_vtree() { n   sort(p + 1, p + k + 1, cmp); n   top = 0; n   S[++top] = p[1].u; n   for (int i = 2; i <= k; i++) { n     int u = p[i].u, v = S[top]; n     int lca = LCA(u, v); n     if (v == lca) n       S[++top] = u; n     else { n       int ntop = top; n       while (ntop > 0 && deep[S[ntop]] > deep[lca]) ntop--; n       for (int j = ntop + 1; j + 1 <= top; j++) { n         add_tree(S[j], S[j + 1]); n       } n       long long pos = S[ntop + 1]; n       if (ntop < top) { n         add_tree(pos, lca); n       } n       top = ntop; n       if (S[top] != lca) { n         OnVtree[++cnt] = lca; n         S[++top] = lca; n       } n       S[++top] = u; n     } n   } n   for (int j = 1; j + 1 <= top; j++) add_tree(S[j], S[j + 1]); n } n void DP(int u, int fa) { n   d[u][0] = 1, d[u][1] = 0; n   if (Chosen[u]) { n     for (int i = 0, sz = G[u].size(); i < sz; i++) { n       int v = G[u][i]; n       if (v == fa) continue; n       DP(v, u); n       d[u][1] += d[v][0]; n       d[u][0] += d[v][0]; n     } n   } else { n     int mx = INF; n     int sum = 0; n     for (int i = 0, sz = G[u].size(); i < sz; i++) { n       int v = G[u][i]; n       if (v == fa) continue; n       DP(v, u); n       d[u][0] += min(d[v][0], d[v][1]); n       sum += d[v][0]; n       d[u][1] += d[v][0]; n       mx = min(mx, d[v][1] - d[v][0]); n     } n     d[u][0] = min(d[u][0], sum); n     d[u][1] += mx; n   } n } n bool check() { n   for (int i = 1; i <= k; i++) { n     int u = OnVtree[i]; n     for (int i = head[u]; i; i = edge[i].nxt) { n       int v = edge[i].v; n       if (Chosen[v]) return true; n     } n   } n   return false; n } n void Solve() { n   scanf( %d , &k); n   cnt = k; n   for (int i = 1; i <= k; i++) { n     int u; n     scanf( %d , &u); n     Chosen[u] = 1; n     OnVtree[i] = u; n     p[i].u = u; n     p[i].dfn = dfn[u]; n   } n   if (check()) { n     for (int i = 1; i <= cnt; i++) { n       int u = OnVtree[i]; n       Chosen[u] = 0; n     } n     puts( -1 ); n     return; n   } n   Get_vtree(); n   DP(S[1], 0); n   int ans = min(d[S[1]][0], d[S[1]][1]); n   printf( %d n , ans); n   for (int i = 1; i <= cnt; i++) { n     int u = OnVtree[i]; n     G[u].clear(); n     Chosen[u] = 0; n   } n } n int main() { n   scanf( %d , &n); n   Init(); n   scanf( %d , &m); n   while (m--) { n     Solve(); n   } n } n


#include <bits/stdc++.h> n using namespace std; n bool prime[101]; n void SieveOfEratosthenes(int n) { n   memset(prime, true, sizeof(prime)); n   for (int p = 2; p * p <= n; p++) { n     if (prime[p] == true) { n       for (int i = p * p; i <= n; i += p) prime[i] = false; n     } n   } n } n int main() { n   long long n, m; n   cin >> n; n   long long c[n + 1]; n   if (n % 2 != 0) { n     cout << -1; n     return 0; n   } n   for (long long i = 1; i <= n; i++) { n     c[i] = i; n   } n   for (long long i = 1; i <= n; i += 2) { n     swap(c[i], c[i + 1]); n   } n   for (long long i = 1; i <= n; i++) { n     cout << c[i] <<    ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 2000010; n inline int in() { n   int x; n   scanf( %d , &x); n   return x; n } n inline long long lin() { n   long long x; n   scanf( %I64d , &x); n   return x; n } n char s[N]; n int t[4 * N]; n int help[N * 4]; n int limit = (int)1e6 * 2; n int ki[N]; n vector<int> V[N]; n vector<string> Store; n void modify(int x, int b, int e, int l, int r, int v) { n   if (help[x]) return; n   if (b > r || e < l) return; n   if (b >= l && e <= r) { n     help[x] = 1; n     t[x] = v; n     return; n   } n   int m = (b + e) >> 1; n   modify(x + x, b, m, l, r, v); n   modify(x + x + 1, m + 1, e, l, r, v); n   help[x] = help[x + x] & help[x + x + 1]; n } n int get(int x, int b, int e, int l, int r) { n   if (t[x] > 0 && (b != e)) { n     t[x + x] = t[x]; n     t[x + x + 1] = t[x]; n   } n   if (b > r || e < l) return 0; n   if (b >= l && e <= r) { n     return t[x]; n   } n   int m = (b + e) >> 1; n   return max(get(x + x, b, m, l, r), get(x + x + 1, m + 1, e, l, r)); n } n int main() { n   int n = in(), mx = 0; n   string str =  aa ; n   Store.push_back(str); n   for (int i = 1; i <= n; i++) { n     scanf( %s , s); n     str = s; n     Store.push_back(str); n     ki[i] = in(); n     for (int j = 1; j <= ki[i]; j++) { n       int di = in(); n       V[i].push_back(di); n       mx = max(mx, di + (int)str.size() - 1); n       modify(1, 1, limit, di, di + str.size() - 1, i); n     } n   } n   for (int i = 1; i <= mx; i++) { n     int idx = get(1, 1, limit, i, i); n     if (idx == 0) n       printf( a ); n     else { n       int low = 0, high = V[idx].size() - 1, ans = 0; n       while (low <= high) { n         int mid = (low + high) >> 1; n         if (V[idx][mid] <= i) { n           low = mid + 1; n           ans = V[idx][mid]; n         } else { n           high = mid - 1; n         } n       } n       printf( %c , Store[idx][i - ans]); n     } n   } n   puts(  ); n } n


#include <bits/stdc++.h> n using namespace std; n void solve() { n   long long int k; n   cin >> k; n   string s =  codeforces ; n   long long int a[10]; n   long long int cnt = 1; n   for (int i = 0; i < 10; ++i) { n     a[i] = 1; n   } n   int idx = 0; n   while (cnt < k) { n     a[idx]++; n     cnt = 1; n     for (int i = 0; i < 10; ++i) { n       cnt *= a[i]; n     } n     idx = (idx + 1) % 10; n   } n   for (int i = 0; i < 10; ++i) { n     for (int j = 0; j < a[i]; ++j) { n       cout << s[i]; n     } n   } n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   long long int t = 1; n   while (t--) { n     solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n using LL = long long; n constexpr int N = 1e5 + 5; n int main() { n   ios::sync_with_stdio(false); n   cin.tie(nullptr); n   int n, m, x, y; n   cin >> n >> m >> x >> y; n   x--; n   y--; n   string s; n   cin >> s; n   vector<vector<int>> mat(n, vector<int>(m, -1)); n   for (int i = 0; i < s.size(); i++) { n     if (mat[x][y] == -1) mat[x][y] = i; n     if (s[i] ==  U ) x = max(x - 1, 0); n     if (s[i] ==  D ) x = min(x + 1, n - 1); n     if (s[i] ==  R ) y = min(y + 1, m - 1); n     if (s[i] ==  L ) y = max(y - 1, 0); n   } n   vector<int> ans(s.size() + 1); n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < m; j++) { n       if (mat[i][j] == -1) { n         ans[s.size()]++; n       } else { n         ans[mat[i][j]]++; n       } n     } n   } n   for (int x : ans) cout << x <<    ; n } n


#include <bits/stdc++.h> n using namespace std; n const int mx = 2e5 + 10; n const int mxb = 60; n long long int a[mx], b[mx], c[mx]; n int cnt[mxb][2]; n int bat(int idx, long long int v) { n   if (v & (1ll << idx)) return 1; n   return 0; n } n int main() { n   int n; n   scanf( %d , &n); n   long long int sm = 0; n   for (int i = 0; i < n; i++) scanf( %lld , b + i); n   for (int i = 0; i < n; i++) scanf( %lld , c + i), sm += b[i] + c[i]; n   sm /= n + n; n   for (int i = 0; i < n; i++) { n     a[i] = (b[i] + c[i] - sm) / n; n     if (a[i] < 0) { n       puts( -1 ); n       return 0; n     } n     for (int j = 0; j < mxb; j++) cnt[j][bat(j, a[i])]++; n   } n   for (int i = 0; i < n; i++) { n     long long int bi = 0, ci = 0; n     int cb = 0, cc = 0; n     for (int j = 0; j < mxb; j++) { n       int db = cb, dc = cc; n       for (int x = 0; x < 2; x++) { n         db += cnt[j][x] * (bat(j, a[i]) & x); n         dc += cnt[j][x] * (bat(j, a[i]) | x); n       } n       cb = db / 2, cc = dc / 2; n       if (db & 1) bi |= (1ll << j); n       if (dc & 1) ci |= (1ll << j); n     } n     if (bi != b[i] || ci != c[i]) { n       puts( -1 ); n       return 0; n     } n   } n   for (int i = 0; i < n; i++) printf( %lld  , a[i]); n   puts(  ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int a, b, c, d, e, f; n   cin >> a >> b >> c >> d >> e >> f; n   int sum = 0; n   bool flag = false; n   if (f > e) { n     int x = min(b, min(c, d)); n     sum += f * x; n     b -= x; n     c -= x; n     d -= x; n     flag = true; n   } else { n     int y = min(a, d); n     sum += e * y; n     a -= y; n     d -= y; n   } n   if (flag == true) { n     int y = min(a, d); n     sum += e * y; n   } else { n     int x = min(b, min(c, d)); n     sum += f * x; n   } n   cout << sum; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n inline void pisz(int n) { printf( %d n , n); } n const int fx[4][2] = {{0, 1}, {-1, 0}, {0, -1}, {1, 0}}; n const int fxx[9][2] = {{0, 0}, {0, 1},  {0, -1}, {1, 0},  {-1, 0}, n                        {1, 1}, {1, -1}, {-1, 1}, {-1, -1}}; n const int N = 500; n const long long mod = 1000000007; n struct FenwickTreeMax { n   vector<int> bit; n   int n; n   FenwickTreeMax(int n) { n     this->n = n; n     bit.assign(n, 0); n   } n   FenwickTreeMax(vector<int> a) : FenwickTreeMax(a.size()) { n     for (size_t i = 0; i < a.size(); i++) update(i, a[i]); n   } n   int getmax(int r) { n     int ret = 0; n     for (; r >= 0; r = (r & (r + 1)) - 1) ret = (ret < bit[r] ? bit[r] : ret); n     return ret; n   } n   int pos_val(int r) { return bit[r]; } n   void update(int idx, int val) { n     for (; idx < n; idx = idx | (idx + 1)) n       bit[idx] = (bit[idx] < val ? val : bit[idx]); n   } n }; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   long long n; n   cin >> n; n   string s; n   cin >> s; n   vector<long long> pos[26]; n   for (long long i = 0; i < n; i++) { n     pos[s[i] -  a ].push_back(i); n   } n   FenwickTreeMax ft(n); n   vector<long long> output(n, -1); n   long long maxi = 0; n   for (long long i = 25; i >= 0; i--) { n     if (pos[i].size() == 0) continue; n     vector<long long> col; n     for (auto x : pos[i]) { n       output[x] = ft.getmax(x) + 1; n       maxi = (output[x] < maxi ? maxi : output[x]); n     } n     for (auto x : pos[i]) { n       ft.update(x, output[x]); n     } n   } n   cout << maxi <<   n ; n   for (long long i = 0; i < n; i++) { n     cout << output[i] <<    ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n vector<pair<int, int>> vec; n int main() { n   int n; n   cin >> n; n   for (int tt = 0; tt < n; tt++) { n     int x, y; n     cin >> x >> y; n     vec.push_back({x, y}); n   } n   sort(vec.begin(), vec.end()); n   int ans = -1; n   for (int i = 0; i < n; i++) { n     if (ans <= vec[i].second) n       ans = vec[i].second; n     else n       ans = vec[i].first; n   } n   cout << ans << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int T; n string s, ans; n bool exist[30]; n int main() { n   scanf( %d , &T); n   for (int i = 0; i < T; i++) { n     cin >> s; n     ans.clear(); n     for (int j = 0; j < 26; j++) { n       exist[j] = false; n     } n     ans = s[0]; n     exist[s[0] -  a ] = true; n     bool Done = true; n     for (int j = 1; j < s.length(); j++) { n       if (!exist[s[j] -  a ]) { n         if (ans.front() == s[j - 1]) { n           ans = s[j] + ans; n         } else if (ans.back() == s[j - 1]) { n           ans.push_back(s[j]); n         } else { n           Done = false; n           break; n         } n         exist[s[j] -  a ] = true; n       } else { n         int count = 0; n         for (int k = 0; k < ans.length(); k++) { n           if (ans[k] == s[j - 1]) { n             count = k; n             break; n           } n         } n         if (count == 0) { n           if (ans[1] != s[j]) { n             Done = false; n             break; n           } else { n             continue; n           } n         } n         if (count == ans.length() - 1) { n           if (ans[count - 1] != s[j]) { n             Done = false; n             break; n           } else { n             continue; n           } n         } n         if (ans[count - 1] != s[j] && ans[count + 1] != s[j]) { n           Done = false; n           break; n         } n       } n     } n     if (!Done) { n       cout <<  NO  << endl; n     } else { n       cout <<  YES  << endl; n       cout << ans; n       for (int j = 0; j < 26; j++) { n         if (!exist[j]) { n           cout << char( a  + j); n         } n       } n     } n     cout << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n #pragma GCC optimize( Ofast ) n using namespace std; n int n, first, second, a, b, m, d[(1 << 17) + 5], p[(1 << 17) + 5]; n vector<int> g[(1 << 17) + 5]; n bool check(int nod, int par, int del, int hg) { n   int kids = g[nod].size(); n   if (par) kids--; n   if (nod == del) { n     if (hg == 1) n       return 0; n     else if (hg == 2) { n       if (kids != 1) return 0; n       for (int i : g[nod]) { n         if (i != par) return check(i, nod, del, hg - 1); n       } n     } else { n       if (kids != 3) return 0; n       for (int i : g[nod]) { n         if (i != par) kids -= check(i, nod, del, hg - 1); n       } n       if (kids != 2) return 0; n       for (int i : g[nod]) { n         if (i != par) kids -= check(i, nod, del, hg - 2); n       } n       if (kids != 0) return 0; n       return 1; n     } n   } else { n     if (hg == 1) n       return (kids == 0); n     else { n       if (kids != 2) return 0; n       for (int i : g[nod]) { n         if (i != par) kids -= check(i, nod, del, hg - 1); n       } n       return (kids == 0); n     } n   } n } n bool good(int del, int root) { return check(root, 0, del, n); } n int32_t main() { n   ios_base ::sync_with_stdio(0); n   cin.tie(); n   cout.tie(); n   cin >> n; n   m = (1 << n) - 2; n   for (int i = 1; i < m; i++) { n     cin >> first >> second; n     g[first].push_back(second); n     g[second].push_back(first); n   } n   for (int i = 1; i <= m; i++) { n     if (g[i].size() == 2) { n       int gd = 1; n       for (int j : g[i]) { n         if (g[j].size() == 1) gd = 0; n       } n       if (gd) n         b = i; n       else n         a = i; n     } else if (g[i].size() == 4) n       a = i; n   } n   if (a && b && good(a, b)) { n     cout << 1 <<   n  << a; n     return 0; n   } n   int st = 0; n   for (int i = 1; i <= m; i++) { n     if (g[i].size() == 1) st = i; n   } n   queue<int> q; n   d[st] = 1; n   p[st] = 0; n   q.push(st); n   while (q.size()) { n     int nod = q.front(); n     q.pop(); n     for (int i : g[nod]) { n       if (!d[i]) d[i] = d[nod] + 1, p[i] = nod, q.push(i); n     } n   } n   st = 0; n   for (int i = 1; i <= m; i++) { n     if (d[i] == 2 * n - 2) st = i; n   } n   for (int i = 1; i <= n - 2; i++) st = p[st]; n   a = st; n   b = p[st]; n   if (a && b && good(a, a)) { n     cout << 2 <<   n  << min(a, b) <<     << max(a, b); n     return 0; n   } n   cout << 0; n } n


#include <bits/stdc++.h> n const int MAXN = 2000 + 5; n long long mn = 1e18; n int ans[3][2], k = -1; n int n, m; n long long a[MAXN], b[MAXN]; n inline long long mabs(long long x) { return x < 0 ? -x : x; } n int main() { n   scanf( %d , &n); n   long long s1 = 0, s2 = 0; n   for (int i = 1; i <= n; ++i) scanf( %lld , a + i), s1 += a[i]; n   scanf( %d , &m); n   for (int i = 1; i <= m; ++i) scanf( %lld , b + i), s2 += b[i]; n   mn = mabs(s1 - s2); n   k = 0; n   for (int i = 1; i <= n; ++i) { n     for (int j = 1; j <= m; ++j) { n       if (mn > mabs(s1 - s2 - 2 * a[i] + 2 * b[j])) { n         mn = mabs(s1 - s2 - 2 * a[i] + 2 * b[j]); n         k = 1; n         ans[1][0] = i; n         ans[1][1] = j; n       } n     } n   } n   std::vector<long long> S1, S2; n   long long t1 = -1, t2 = -1; n   for (int i = 1; i <= n; ++i) n     for (int j = i + 1; j <= n; ++j) S1.push_back(2ll * (a[i] + a[j])); n   for (int i = 1; i <= m; ++i) n     for (int j = i + 1; j <= m; ++j) S2.push_back(2ll * (b[i] + b[j])); n   std::sort(S1.begin(), S1.end()); n   std::sort(S2.begin(), S2.end()); n   for (auto x : S1) { n     long long now = s1 - s2 - x; n     auto p = std::upper_bound(S2.begin(), S2.end(), -now); n     if (p != S2.end()) { n       if (mn > mabs(now + *p)) { n         mn = mabs(now + *p); n         t1 = x; n         t2 = *p; n         k = 2; n       } n     } n     if (p != S2.begin()) { n       --p; n       if (mn > mabs(now + *p)) { n         mn = mabs(now + *p); n         t1 = x; n         t2 = *p; n         k = 2; n       } n     } n   } n   if (k == 2) { n     for (int i = 1; i <= n; ++i) n       for (int j = i + 1; j <= n; ++j) n         if (2ll * (a[i] + a[j]) == t1) { n           ans[1][0] = i; n           ans[2][0] = j; n           break; n         } n     for (int i = 1; i <= m; ++i) n       for (int j = i + 1; j <= m; ++j) n         if (2ll * (b[i] + b[j]) == t2) { n           ans[1][1] = i; n           ans[2][1] = j; n           break; n         } n   } n   printf( %lld n%d n , mn, k); n   for (int i = 1; i <= k; ++i) { n     printf( %d %d n , ans[i][0], ans[i][1]); n   } n   auto chk = [&]() { n     for (int i = 1; i <= k; ++i) std::swap(a[ans[i][0]], b[ans[i][1]]); n     long long sm0 = 0, sm1 = 0; n     for (int i = 1; i <= n; ++i) sm0 += a[i]; n     for (int i = 1; i <= m; ++i) sm1 += b[i]; n     assert(mabs(sm0 - sm1) == mn); n   }; n   chk(); 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   string l =  312931303130313130313031 ; n   string input =   ; n   string s =  312831303130313130313031 ; n   string s1 = s + s + s; n   string s2 = l + s + s; n   string s3 = s + l + s; n   string s4 = s + s + l; n   long long int i; n   for (i = 0; i < n; i++) { n     string ch; n     cin >> ch; n     input += ch; n   } n   long long int flag1 = 0, flag2 = 0, flag3 = 0, flag4 = 0; n   for (i = 0; i < s1.length() - 2 * n; i++) { n     if (input.compare(s1.substr(i, n * 2)) == 0) { n       flag1 = 1; n     } n   } n   for (i = 0; i < s2.length() - 2 * n; i++) { n     if (input.compare(s2.substr(i, 2 * n)) == 0) { n       flag2 = 1; n     } n   } n   for (i = 0; i < s3.length() - 2 * n; i++) { n     if (input.compare(s3.substr(i, 2 * n)) == 0) { n       flag3 = 1; n     } n   } n   for (i = 0; i < s4.length() - 2 * n; i++) { n     if (input.compare(s4.substr(i, 2 * n)) == 0) { n       flag4 = 1; n     } n   } n   if (flag1 || flag2 || flag3 || flag4) { n     cout <<  YES  << endl; n   } else { n     cout <<  NO  << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 5005; n const long long inf = 1e15; n long long dp[2][N][2]; n int a[N][2]; n void load() {} n void trungtt() { n   int n, x; n   scanf( %d%d , &n, &x); n   long long ans = 0; n   int l = x, r = x; n   for (int i = 1; i <= n; ++i) { n     int a, b; n     scanf( %d%d , &a, &b); n     if (l > b) { n       ans += l - b; n       r = l; n       l = b; n     } else if (r < a) { n       ans += a - r; n       l = r, r = a; n     } else { n       if (l < a) l = a; n       if (r > b) r = b; n     } n   } n   printf( %lld , ans); n   return; n } n int main() { n   load(); n   trungtt(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T> n inline bool upmin(T &x, T y) { n   return y < x ? x = y, 1 : 0; n } n template <typename T> n inline bool upmax(T &x, T y) { n   return x < y ? x = y, 1 : 0; n } n const long double eps = 1e-11; n const long double pi = acos(-1); n const int oo = 1 << 30; n const long long loo = 1ll << 62; n const int mods = 1e9 + 7; n const int MAXN = 5005; n const int INF = 0x3f3f3f3f; n inline int read() { n   int f = 1, x = 0; n   char c = getchar(); n   while (c <  0  || c >  9 ) { n     if (c ==  - ) f = -1; n     c = getchar(); n   } n   while (c >=  0  && c <=  9 ) { n     x = (x << 3) + (x << 1) + (c ^ 48); n     c = getchar(); n   } n   return x * f; n } n struct anode { n   int u, v; n   long long c; n }; n vector<anode> Ans; n long long a[MAXN]; n int f[MAXN][MAXN], frm[MAXN][MAXN], flag[MAXN], n, k, V; n void add(int u, int v, long long c) { n   long long t = min(c * k, a[u]); n   a[u] -= t, a[v] += t; n   Ans.push_back((anode){u, v, c}); n } n signed main() { n   long long sum = 0; n   n = read(), k = read(), V = read(); n   for (int i = 1; i <= n; i++) a[i] = read(), sum += a[i]; n   if (sum < V) { n     puts( NO ); n     return 0; n   } n   f[0][0] = 1; n   for (int i = 1; i <= n; i++) n     for (int j = 0; j < k; j++) n       if (f[i - 1][j]) n         f[i][j] = 1, frm[i][j] = 0; n       else if (f[i - 1][(j - a[i] % k + k) % k]) n         f[i][j] = 1, frm[i][j] = 1; n   int nw = V % k, id1 = 0, id2 = 0; n   if (!f[n][nw]) { n     puts( NO ); n     return 0; n   } n   puts( YES ); n   for (int i = n; i >= 1; i--) { n     if (frm[i][nw]) n       flag[i] = 1, nw = (nw - a[i] % k + k) % k, id1 = i; n     else n       id2 = i; n   } n   for (int i = 1; i <= n; i++) n     if (a[i] && i != id1 && i != id2) { n       if (flag[i]) n         add(i, id1, (a[i] - 1) / k + 1); n       else n         add(i, id2, (a[i] - 1) / k + 1); n     } n   if (!id1) id1 = (id2 == n ? id2 - 1 : id2 + 1); n   if (!id2) id2 = (id1 == n ? id1 - 1 : id1 + 1); n   if (a[id1] > V) add(id1, id2, (a[id1] - V) / k); n   if (a[id1] < V) add(id2, id1, (V - a[id1]) / k); n   for (auto v : Ans) printf( %lld %d %d n , v.c, v.u, v.v); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, m; n   scanf( %d %d  , &n, &m); n   char A[147][10047]; n   for (int i = 0; i < n; i++) n     for (int j = 0; j < m; j++) scanf( %c  , &A[i][j]); n   int P[147][10047]; n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < m; j++) P[i][j] = -1; n     queue<int> Q; n     for (int j = 0; j < m; j++) n       if (A[i][j] ==  1 ) { n         Q.push(j); n         Q.push(0); n         P[i][j] = 0; n       } n     while (!Q.empty()) { n       int kde = Q.front(); n       Q.pop(); n       int kolko = Q.front(); n       Q.pop(); n       int dx[] = {1, -1}; n       for (int k = 0; k < 2; k++) { n         int kde1 = kde + dx[k]; n         kde1 += m; n         kde1 %= m; n         if (P[i][kde1] == -1) { n           P[i][kde1] = kolko + 1; n           Q.push(kde1); n           Q.push(kolko + 1); n         } n       } n     } n   } n   int vys = -1; n   int V[10047]; n   for (int i = 0; i < m; i++) V[i] = 0; n   for (int i = 0; i < n; i++) n     for (int j = 0; j < m; j++) { n       if (P[i][j] == -1) n         V[j] = -1; n       else if (V[j] != -1) n         V[j] += P[i][j]; n     } n   vys = V[0]; n   for (int i = 0; i < m; i++) vys = min(vys, V[i]); n   printf( %d n , vys); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<int> mat[100010]; n int vis[100010], val[100010]; n int dfs(int p, int pa) { n   if (vis[p]) return 1; n   int ret = 0; n   vis[p] = 1; n   if (p != pa) val[p] += val[pa]; n   for (auto x : mat[p]) ret = max(ret, dfs(x, p)); n   return ret; n } n int in[100010], out[100010], n, dp[302][100002][2]; n int rec(int p, int r, int t) { n   if (p == n + 1) return (r == 0); n   if (r < 0) return 0; n   int &temp = dp[p][r][t]; n   if (temp != -1) return temp; n   temp = 0; n   if (!out[p] || t) temp += rec(p + 1, r, 0); n   if (temp > 1000000007) temp -= 1000000007; n   temp += rec(p, r - val[p], 1); n   if (temp > 1000000007) temp -= 1000000007; n   return temp; n } n int main() { n   ios::sync_with_stdio(0); n   int q, t, u, v; n   cin >> n >> q >> t; n   for (int i = 1; i <= n; i++) cin >> val[i]; n   for (int i = 0; i <= q - 1; i++) { n     cin >> u >> v; n     mat[u].push_back(v); n     in[v]++; n     out[u]++; n   } n   int chao = 0; n   for (int i = 1; i <= n; i++) n     if (!vis[i] && !in[i]) chao = max(chao, dfs(i, i)); n   for (int i = 1; i <= n; i++) n     if (!vis[i]) chao = 1; n   memset(dp, -1, sizeof(dp)); n   if (chao) n     cout << 0 << endl; n   else n     cout << rec(1, t, 0) << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int B[300000]; n pair<int, int> A[300000]; n stack<pair<int, int> > s; n queue<pair<pair<int, int>, int> > q; n int main() { n   int N, l, f, i; n   long long ta = 0, tb = 0; n   scanf( %d , &N); n   for (i = 0; i < N; ++i) n     scanf( %d , &A[i].first), A[i].second = i, ta += A[i].first; n   for (i = 0; i < N; ++i) scanf( %d , &B[i]), tb += B[i]; n   sort(A, A + N); n   sort(B, B + N); n   if (ta != tb) { n     printf( NO n ); n     return 0; n   } n   for (i = 0; i < N; ++i) { n     if (A[i].first < B[i]) n       s.emplace(B[i] - A[i].first, A[i].second); n     else { n       l = A[i].first - B[i]; n       while (l && !s.empty()) { n         q.emplace(make_pair(s.top().second + 1, A[i].second + 1), n                   f = min(l, s.top().first)); n         s.top().first -= f; n         l -= f; n         if (!s.top().first) s.pop(); n       } n       if (l) { n         printf( NO n ); n         return 0; n       } n     } n   } n   printf( YES n ); n   printf( %d n , q.size()); n   while (!q.empty()) n     printf( %d %d %d n , q.front().first.first, q.front().first.second, n            q.front().second), n         q.pop(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n set<int> s[40]; n set<int>::iterator it, q; n int n, m, k, sol, v[40]; n int main() { n   int i, j; n   scanf( %d %d , &n, &m); n   for (i = 1; i <= n; i++) { n     scanf( %d , &v[i]); n     v[i] %= m; n   } n   if (n == 1) { n     printf( %d , v[1]); n     return 0; n   } n   if (n == 2) { n     printf( %d , max((v[1] + v[2]) % m, max(v[1], v[2]))); n     return 0; n   } n   k = n / 2; n   for (i = 1; i <= n; i++) { n     s[i].insert(v[i]); n     if (i != k + 1) { n       for (it = s[i - 1].begin(); it != s[i - 1].end(); it++) { n         j = *it; n         s[i].insert(j); n         j += v[i]; n         j %= m; n         s[i].insert(j); n       } n       s[i - 1].clear(); n     } n   } n   it = s[k].end(); n   it--; n   sol = *it; n   it = s[n].end(); n   it--; n   sol = max(sol, int(*it)); n   if (s[n].begin() != s[n].end()) n     for (it = s[k].begin(); it != s[k].end(); it++) { n       i = *it; n       j = m - i; n       q = s[n].lower_bound(j); n       if (q != s[n].begin()) { n         q--; n         sol = max(sol, (i + int(*q)) % m); n       } n       q = s[n].end(); n       q--; n       sol = max(sol, int((1LL * i + int(*q)) % m)); n     } n   printf( %d , sol); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n char M[10][10]; n int V[10]; n int main() { n   int n, k; n   cin >> n >> k; n   for (int i = 0; i < n; i++) cin >> M[i]; n   for (int i = 0; i < k; i++) V[i] = i; n   long ans(1e10); n   do { n     long mx(0), mn(1e10); n     for (int i = 0; i < n; i++) { n       long num = 0; n       for (int j = 0; j < k; j++) { n         num = num * 10 + (M[i][V[j]] -  0 ); n       } n       mx = max(mx, num); n       mn = min(mn, num); n     } n     ans = min(ans, mx - mn); n   } while (next_permutation(V, V + k)); n   cout << ans << 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   int t = 1; n   cin >> t; n   while (t--) { n     int n, mx = -1; n     cin >> n; n     int arr[n]; n     for (int i = 0; i < n; i++) { n       cin >> arr[i]; n       mx = max(mx, arr[i]); n     } n     int ans = -1; n     for (int i = 1; i < n; i++) n       if (arr[i] == mx && arr[i - 1] != mx) ans = i + 1; n     for (int i = 0; i < n - 1; i++) n       if (arr[i] == mx && arr[i + 1] != mx) ans = i + 1; n     cout << ans << (  n ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int min(int a, int b) { n   if (a < b) return a; n   return b; n } n int Abs(int a) { n   if (a < 0) n     return -a; n   else n     return a; n } n double Abs(double a) { n   if (a < 0) n     return -a; n   else n     return a; n } n unsigned long long sqr(unsigned long long a) { return a * a; } n double sqr(double a) { return a * a; } n int main() { n   int a, b, c; n   cin >> a >> b; n   c = 0; n   while (b) { n     c = c * 10 + b % 10; n     b /= 10; n   } n   cout << a + c << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long n, m; n vector<long long> boxes; n vector<int> counts; n long long answer; n void In() { n   boxes.clear(); n   counts.clear(); n   answer = 0; n   cin >> n >> m; n   boxes.resize(m); n   counts.resize(63); n   for (int i = 0; i < m; i++) cin >> boxes[i]; n } n long long Solve() { n   long long sum = 0; n   for (long long box : boxes) sum += box; n   if (sum < n) return -1; n   for (long long box : boxes) counts[log2(box)]++; n   for (int i = 0; i <= log2(n); i++) n     if ((1 << i) & n) { n       for (int j = 0; j < i; j++) { n         counts[j + 1] += counts[j] / 2; n         counts[j] %= 2; n       } n       if (!counts[i]) n         for (int j = i + 1; j < 63; j++) n           if (counts[j]) { n             while (j != i) { n               counts[j]--; n               j--; n               counts[j] += 2; n               answer++; n             } n             break; n           } n       counts[i]--; n     } n   return answer; n } n void Out() { cout << answer << endl; } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   int t = 1; n   cin >> t; n   while (t--) { n     In(); n     answer = Solve(); n     Out(); n   } n } n


#include <bits/stdc++.h> n int x, y, u, v, d = 1, a, b; n int main() { n   for (scanf( %d%d , &a, &b); a; b /= 4, d *= 2, --a) { n     if (b % 4 == 0) n       u = y, v = x; n     else if (b % 4 == 1) n       u = x, v = y + d; n     else if (b % 4 == 2) n       u = x + d, v = y + d; n     else n       u = 2 * d - y - 1, v = d - x - 1; n     x = u, y = v; n   } n   printf( %d %d n , x, y); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<int> v[100009]; n map<int, int> mp; n int main() { n   int n, m, x, y, index = 1; n   scanf( %d%d , &n, &m); n   for (int i = 0; i < m; i++) { n     scanf( %d%d , &x, &y); n     if (mp[x] == 0) { n       mp[x] = index; n       v[index++].push_back(y); n     } else n       v[mp[x]].push_back(y); n     if (mp[y] == 0) { n       mp[y] = index; n       v[index++].push_back(x); n     } else n       v[mp[y]].push_back(x); n   } n   int bus_out = 0, bus_in = 0, ring = 0, s, cen_c = 0, cen_out = 0; n   for (int i = 1; i < index; i++) { n     s = v[i].size(); n     if (s == 1) { n       cen_out++; n       bus_out++; n     } else if (s == 2) { n       bus_in++; n       ring++; n     } else { n       cen_c++; n     } n   } n   if (n < m) { n     printf( unknown topology n ); n   } else if (n - 1 == m && bus_out == 2 && bus_in == n - 2) { n     printf( bus topology n ); n   } else if (ring == n && n == m) { n     printf( ring topology n ); n   } else if (n == m + 1 && cen_c == 1 && cen_out == n - 1) { n     printf( star topology n ); n   } else { n     printf( unknown topology n ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int mxn = 2e5 + 5; n long long a[1001][1001]; n int main() { n   int t; n   cin >> t; n   while (t--) { n     int n, m; n     cin >> n >> m; n     for (int i = 1; i <= n; ++i) { n       for (int j = 1; j <= m; ++j) { n         cin >> a[i][j]; n         if ((i + j) & 1 && !(a[i][j] & 1)) n           ++a[i][j]; n         else if (!((i + j) & 1) && a[i][j] & 1) n           ++a[i][j]; n         cout << a[i][j] <<    ; n       } n       cout << endl; n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int INF = 0x3f3f3f3f; n const long long LINF = 0x3f3f3f3f3f3f3f3f; n const int MOD = (int)1e9 + 7; n const int N = (int)1e6 + 7; n struct node { n   long long x, y, a; n } a[N]; n int n; n long long dp[N]; n struct P { n   long long x, y; n   P() {} n   P(long long x, long long y) : x(x), y(y) {} n   P operator-(const P &rhs) const { return P(x - rhs.x, y - rhs.y); } n   bool operator^(const P &rhs) const { n     return (double)x * rhs.y > (double)y * rhs.x; n   } n }; n void Init() { n   for (int i = (1); i < (n + 1); ++i) n     scanf( %I64d%I64d%I64d , &a[i].x, &a[i].y, &a[i].a); n   sort(a + 1, a + n + 1, [&](node a, node b) { return a.x < b.x; }); n } n int Solve() { n   deque<P> Q; n   Q.push_back(P((a[0].x), (dp[0]))); n   for (int i = (1); i < (n + 1); ++i) { n     while ((int)(Q).size() > 1 && n            (Q[1].y - Q[0].y) > (Q[1].x - Q[0].x) * (a[i].y)) n       Q.pop_front(); n     dp[i] = Q[0].y - (a[i].y) * Q[0].x + a[i].x * a[i].y - a[i].a; n     P aa((a[i].x), (dp[i])); n     while ((int)(Q).size() > 1 && !((aa - Q[(int)(Q).size() - 2]) ^ n                                     (Q.back() - Q[(int)(Q).size() - 2]))) n       Q.pop_back(); n     Q.push_back(aa); n   } n   long long ans = 0; n   for (int i = (1); i < (n + 1); ++i) ans = max(ans, dp[i]); n   return printf( %I64d n , ans); n } n int main() { n   while (~scanf( %d , &n)) { n     Init(); n     Solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long LINF = 1001002003004005006ll; n int dx[] = {1, 0, -1, 0}; n int dy[] = {0, 1, 0, -1}; n long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; } n template <class T> n bool chmax(T &a, const T &b) { n   if (a < b) { n     a = b; n     return 1; n   } n   return 0; n } n template <class T> n bool chmin(T &a, const T &b) { n   if (b < a) { n     a = b; n     return 1; n   } n   return 0; n } n signed main() { n   cin.tie(0); n   ios::sync_with_stdio(0); n   long long n; n   cin >> n; n   string s; n   cin >> s; n   if (n == 1) { n     cout << 0 << endl; n     return 0; n   } n   vector<long long> v; n   long long i = 0; n   while (i < n) { n     char tmp = s[i]; n     long long cnt = 0; n     while (i + cnt < n and s[i + cnt] == tmp) { n       cnt++; n     } n     v.push_back(cnt); n     i += cnt; n   } n   long long m = v.size(); n   if (m == 1) { n     long long ans = n * (n + 1) / 2 - n; n     cout << ans << endl; n     return 0; n   } n   long long diff = 0; n   for (long long i = 0; i < m; i++) { n     diff += 2 * v[i]; n   } n   diff -= v[0]; n   diff -= v[m - 1]; n   diff -= m - 1; n   long long ans = n * (n + 1) / 2 - diff - n; n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long mypow3(long long p, long long mod) { n   long long rez = 1, x = 3; n   while (p > 0) { n     if (p % 2 == 1) rez = (rez * x) % mod; n     x = (x * x) % mod; n     p /= 2; n   } n   return rez; n } n int main() { n   long long n, m, ans; n   ios_base::sync_with_stdio(false); n   cin >> n >> m; n   ans = mypow3(n, m) - 1; n   if (ans == -1) ans = m - 1; n   cout << ans <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long a, b, c[101], d[101], mx = -1e9, cnt; n int main() { n   cin >> a >> b; n   for (int i = 1; i <= a; ++i) { n     cin >> c[i]; n   } n   for (int i = 1; i < a; ++i) { n     d[i] = c[i] - c[i + 1] - b; n   } n   for (int i = 1; i <= a; ++i) { n     mx = max(d[i], mx); n   } n   cout << mx; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n using ll = long long; n constexpr int inf = 1e9; n using P = pair<int, int>; n template <typename T> n void printv(const vector<T> &v) { n   int sz = v.size(); n   for (int i = 0; i < sz; i++) { n     cout << v[i] <<    n [i == sz - 1]; n   } n } n using Graph = vector<vector<int>>; n const int MAX_N = 3010; n Graph g(MAX_N); n vector<vector<int>> dist(MAX_N, vector<int>(MAX_N, inf)); n vector<vector<int>> ch(MAX_N, vector<int>(MAX_N)); n int dfs(int r, int cur, int pre) { n   ch[r][cur] = 1; n   for (auto nxt : g[cur]) { n     if (nxt == pre) continue; n     ch[r][cur] += dfs(r, nxt, cur); n   } n   return ch[r][cur]; n } n int main() { n   cin.tie(0); n   ios::sync_with_stdio(false); n   int n; n   cin >> n; n   vector<P> ps; n   for (int i = 0; i < n - 1; i++) { n     int u, v; n     cin >> u >> v; n     u--; n     v--; n     ps.push_back(P(u, v)); n     g[u].push_back(v); n     g[v].push_back(u); n   } n   for (int i = 0; i < n; i++) { n     queue<int> que; n     que.push(i); n     dist[i][i] = 0; n     while (!que.empty()) { n       int v = que.front(); n       que.pop(); n       for (auto nxt : g[v]) { n         if (dist[i][nxt] != inf) continue; n         dist[i][nxt] = dist[i][v] + 1; n         que.push(nxt); n       } n     } n   } n   for (int i = 0; i < n; i++) { n     dfs(i, i, -1); n   } n   vector<vector<P>> cs(n); n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < n; j++) { n       cs[dist[i][j]].push_back(P(i, j)); n     } n   } n   vector<vector<ll>> dp(n, vector<ll>(n)); n   for (int d = 0; d < n - 1; d++) { n     for (auto p : cs[d]) { n       int u = p.first, v = p.second; n       for (auto nxt : g[u]) { n         if (dist[u][nxt] + dist[u][v] != dist[v][nxt]) continue; n         ll x = ch[nxt][v], y = ch[u][nxt]; n         dp[nxt][v] = max(dp[nxt][v], dp[u][v] + x * y); n         dp[v][nxt] = max(dp[v][nxt], dp[u][v] + x * y); n       } n       for (auto nxt : g[v]) { n         if (dist[v][nxt] + dist[u][v] != dist[u][nxt]) continue; n         ll x = ch[nxt][u], y = ch[v][nxt]; n         dp[nxt][u] = max(dp[nxt][u], dp[u][v] + x * y); n         dp[u][nxt] = max(dp[u][nxt], dp[u][v] + x * y); n       } n     } n   } n   ll ret = 0; n   for (int i = 0; i < n; i++) n     for (int j = 0; j < n; j++) ret = max(ret, dp[i][j]); n   cout << ret << endl; n } n


#include <bits/stdc++.h> n const long double pie = 3.1415926535; n using namespace std; n bool br1, br2, br; n long long n, k, x, y, z, mini, maxi, l, ind, ini, sum, t, len, r, q, imini, w, n     m; n string s, s1, s2; n struct Point { n   long long X, Y; n   static long long getDir(Point a, Point b, Point c) { n     return ((b.X - a.X) * (c.Y - a.Y) - (b.Y - a.Y) * (c.X - a.X)); n   } n }; n void solve() { n   Point A, B, C; n   cin >> A.X >> A.Y >> B.X >> B.Y >> C.X >> C.Y; n   m = A.getDir(A, B, C); n   if (m == 0) n     cout <<  TOWARDS  << endl; n   else if (m > 0) n     cout <<  LEFT  << endl; n   else n     cout <<  RIGHT  << endl; n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(NULL); n   solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long N = 5e5 + 5, M = 65; n long long n, q, d = 60, fa[N], opt, x, s[M]; n double f[N][M]; n void update(long long v) { n   long long top = 0, x = v; n   for (long long i = 0; i <= d; i++) { n     if (!x) break; n     s[top++] = x, x = fa[x]; n   } n   top--; n   for (long long i = top; i >= 2; i--) n     f[s[i]][i] /= 0.5 * (1 + f[s[i - 1]][i - 1]); n   for (long long i = 1; i <= d; i++) f[v][i] = 1; n   for (long long i = 1; i <= top; i++) n     f[s[i]][i] *= 0.5 * (1 + f[s[i - 1]][i - 1]); n } n double query(long long v) { n   double ans = 0; n   for (long long i = 1; i < d; i++) ans += i * (f[v][i + 1] - f[v][i]); n   return ans; n } n signed main() { n   scanf( %lld , &q), n = 1; n   for (long long i = 1; i <= d; i++) f[1][i] = 1; n   while (q--) { n     scanf( %lld%lld , &opt, &x); n     if (opt == 1) n       n++, fa[n] = x, update(n); n     else n       printf( %.10lf n , query(x)); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MaxN = 2e5 + 10; n const int MOD = 1e9 + 7; n const int INF = 1e9; n int main() { n   int L, n, p, t; n   cin >> L >> n >> p >> t; n   if (n == 0) { n     cout << 0 <<   n ; n     exit(0); n   } n   vector<pair<int, int> > seg(n); n   for (int i = 0; i < n; ++i) { n     cin >> seg[i].first >> seg[i].second; n   } n   sort(seg.begin(), seg.end()); n   set<pair<int, int> > st; n   map<int, int> f; n   for (int i = 0; i < n; ++i) { n     st.insert(make_pair(seg[i].first, i)); n   } n   int ans = 0; n   while (!st.empty()) { n     int x = st.begin()->first, where = st.begin()->second; n     ans = max(ans, f[x]); n     st.erase(st.begin()); n     int ex = seg[where].second; n     if (where + 1 < (int)seg.size()) { n       f[seg[where + 1].first] = max(f[seg[where + 1].first], f[x]); n     } n     int can = (ex - x) / p; n     int val = f[x] + can; n     ans = max(ans, f[x] + can); n     if (where + 1 < n && f[seg[where + 1].first] >= val) { n       continue; n     } n     x += (ex - x) / p * p + t; n     if (where + 1 < n && seg[where + 1].first >= x) { n       f[seg[where + 1].first] = max(f[seg[where + 1].first], val); n       continue; n     } n     int pos = lower_bound(seg.begin(), seg.end(), make_pair(x, INF)) - n               seg.begin() - 1; n     if (pos < n && seg[pos].first <= x && x <= seg[pos].second) { n       st.insert(make_pair(x, pos)); n       f[x] = max(f[x], val); n     } else if (pos + 1 != n) { n       f[seg[pos + 1].first] = max(f[seg[pos + 1].first], val); n     } n   } n   cout << ans <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n; n vector<string> cards; n map<vector<string>, bool> used; n void rec(vector<string>& x) { n   if (x.size() == 1) { n     puts( YES ); n     exit(0); n   } n   if (used[x] == true) return; n   used[x] = true; n   int p = x.size() - 1; n   if (x[p][0] == x[p - 1][0] || x[p][1] == x[p - 1][1]) { n     string s = x[p - 1]; n     string c = x[p]; n     x[p - 1] = x[p]; n     x.pop_back(); n     rec(x); n     x[p - 1] = s; n     x.push_back(c); n   } n   if (p > 2 && (x[p][0] == x[p - 3][0] || x[p][1] == x[p - 3][1])) { n     string s = x[p - 3]; n     string c = x[p]; n     x[p - 3] = x[p]; n     x.pop_back(); n     rec(x); n     x[p - 3] = s; n     x.push_back(c); n   } n } n int main() { n   cin >> n; n   for (int i = 0; i < n; i++) { n     string s; n     cin >> s; n     cards.push_back(s); n   } n   rec(cards); n   puts( NO ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n using ll = long long; n using ld = long double; n using pii = pair<int, int>; n const int INF = (int)1e9; n struct Car { n   int x = INF; n   ll time = 0; n   int id = -1; n   Car() = default; n   Car(int xx, ll t, int i) : x(xx), time(t), id(i) {} n }; n Car get_best(const Car& a, const Car& b, int x) { n   int d1 = abs(x - a.x); n   int d2 = abs(x - b.x); n   if (d1 != d2) return d1 < d2 ? a : b; n   if (a.time != b.time) return a.time < b.time ? a : b; n   return a.id < b.id ? a : b; n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(nullptr); n   map<int, set<pair<ll, int>>> waiting; n   int n, k, m; n   cin >> n >> k >> m; n   for (int i = 0; i < k; i++) { n     int x; n     cin >> x; n     waiting[x].insert({0, i}); n   } n   ll curt = 0; n   map<ll, vector<pii>> events; n   for (int i = 0; i < m; i++) { n     ll t, a, b; n     cin >> t >> a >> b; n     curt = max(curt, t); n     while (events.size() && events.begin()->first <= curt) { n       auto e = *events.begin(); n       events.erase(events.begin()); n       for (auto p : e.second) waiting[p.first].insert({e.first, p.second}); n     } n     if (waiting.empty()) { n       assert(events.size()); n       auto e = *events.begin(); n       events.erase(events.begin()); n       for (auto p : e.second) waiting[p.first].insert({e.first, p.second}); n       curt = e.first; n     } n     assert(waiting.size()); n     Car L; n     Car R; n     auto it = waiting.lower_bound(a); n     if (it != waiting.end()) n       L = Car(it->first, it->second.begin()->first, it->second.begin()->second); n     if (it != waiting.begin()) { n       --it; n       R = Car(it->first, it->second.begin()->first, it->second.begin()->second); n     } n     Car best = get_best(L, R, a); n     assert(best.id != -1); n     cout << best.id + 1 <<     << curt - t + abs(a - best.x) <<   n ; n     waiting[best.x].erase({best.time, best.id}); n     if (waiting[best.x].empty()) waiting.erase(best.x); n     events[curt + abs(a - best.x) + abs(a - b)].push_back({b, best.id}); n   } n } n


#include <bits/stdc++.h> n const int MX = 2e6 + 7; n const int N = 1e9 + 1; n const int mod = 998244353; n const double pi = acos(-1); n double esp = 1e-13; n using namespace std; n long long qpow(long long a, long long b, long long MOD = mod) { n   for (long long ans = 1;; a = a * a % MOD, b >>= 1) { n     if (b & 1) ans = ans * a % MOD; n     if (!b) return ans; n   } n } n long long inv(long long a, long long MOD = mod) { n   return qpow(a, MOD - 2, MOD); n } n long long exgcd(long long a, long long b, long long &x, long long &y) { n   if (b == 0) { n     x = 1, y = 0; n     return a; n   } n   long long ret = exgcd(b, a % b, y, x); n   y -= a / b * x; n   return ret; n } n long long getInv(int a, int mod) { n   long long x, y; n   long long d = exgcd(a, mod, x, y); n   return d == 1 ? (x % mod + mod) % mod : -1; n } n long long add(long long a, long long b) { n   long long ans = (a + b) % mod; n   return ans >= 0 ? ans : ans + mod; n } n void mul(long long &a, long long b) { a = a % mod * (b % mod) % mod; } n int main() { n   ios::sync_with_stdio(0), cin.tie(0); n   int t; n   cin >> t; n   while (t--) { n     int n; n     cin >> n; n     priority_queue<pair<int, int>> Q; n     for (int i = 1; i <= n; i++) { n       int x; n       cin >> x; n       Q.push(make_pair(x, i)); n     } n     int tmp = 0, res = 0; n     while (!Q.empty()) { n       auto x = Q.top(); n       Q.pop(); n       if (x.second == res) { n         if (Q.empty()) { n           break; n         } else { n           tmp++; n           auto y = Q.top(); n           Q.pop(); n           y.first--; n           if (y.first != 0) Q.push(y); n           res = y.second; n           Q.push(x); n         } n       } else { n         tmp++; n         x.first--; n         if (x.first != 0) Q.push(x); n         res = x.second; n       } n     } n     cout << (tmp % 2 == 0 ?  HL  :  T ) <<   n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int n, m; n int R[101]; n double dp[100100], sum[100100]; n int main() { n   scanf( %d%d , &n, &m); n   if (m == 1) { n     puts( 1 ); n     return 0; n   } n   int score = 0; n   dp[0] = 1; n   double f = 1. / (m - 1); n   for (int i = 0; i < n; ++i) { n     scanf( %d , R + i); n     score += R[i]; n   } n   for (int i = 0; i < n; ++i) { n     int r = R[i]; n     sum[0] = dp[0]; n     for (int j = 1; j <= min(score - 1, (i + 1) * m); ++j) { n       sum[j] = sum[j - 1] + dp[j]; n     } n     for (int j = min(score - 1, (i + 1) * m); j > 0; --j) { n       dp[j] = sum[j - 1]; n       if (j - m - 1 >= 0) dp[j] -= sum[j - m - 1]; n       if (j - r >= 0) dp[j] -= dp[j - r]; n       dp[j] *= f; n     } n     dp[0] = 0; n   } n   double res = 1; n   for (int i = 0; i < score; ++i) { n     res += dp[i] * (m - 1); n   } n   printf( %.15lf n , res); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int M = 998244353; n int pow(int a, int b) { n   if (b == 0) { n     return 1; n   } n   if (b % 2) { n     return ((long long)a * pow(a, b - 1)) % M; n   } else { n     int temp = pow(a, b / 2); n     return ((long long)temp * temp) % M; n   } n } n int inv[3010]; n int w[55]; n int a[55]; n int dp1[55][55][55]; n int dp2[55][55][55]; n int main() { n   ios_base::sync_with_stdio(0), cin.tie(0); n   int n, m; n   cin >> n >> m; n   for (int i = 0; i < 3010; i++) { n     inv[i] = pow(i, M - 2); n   } n   for (int i = 0; i < n; i++) { n     cin >> a[i]; n   } n   int s = 0, s1 = 0, s2 = 0; n   for (int i = 0; i < n; i++) { n     cin >> w[i]; n     s += w[i]; n     if (a[i]) { n       s1 += w[i]; n     } n   } n   s2 = s - s1; n   for (int i = 0; i < n; i++) { n     dp1[i][0][0] = 1; n   } n   for (int l = 0; l < m; l++) { n     for (int i = 0; i < n; i++) { n       for (int j = 0; j < 55; j++) { n         for (int k = 0; k < 55; k++) { n           if (a[i]) { n             dp2[i][j][k] = (long long)dp1[i][j][k] * (s2 - l + j) % M * n                            inv[s + 2 * j - l] % M; n             if (k && j) n               dp2[i][j][k] = (dp2[i][j][k] + (long long)dp1[i][j - 1][k - 1] * n                                                  (w[i] + k - 1) % M * n                                                  inv[s + 2 * j - l - 2] % M) % n                              M; n             if (j) n               dp2[i][j][k] = (dp2[i][j][k] + (long long)dp1[i][j - 1][k] * n                                                  (s1 + j - w[i] - k - 1) % M * n                                                  inv[s + 2 * j - 2 - l] % M) % n                              M; n           } else { n             dp2[i][j][k] = (long long)dp1[i][j][k] * (s2 - l + j - (w[i] - k)) % n                            M * inv[s + 2 * j - l] % M; n             if (k) n               dp2[i][j][k] = (dp2[i][j][k] + (long long)dp1[i][j][k - 1] * n                                                  (w[i] - (k - 1)) % M * n                                                  inv[s + 2 * j - l] % M) % n                              M; n             if (j) n               dp2[i][j][k] = n                   (dp2[i][j][k] + (long long)dp1[i][j - 1][k] * (s1 + j - 1) % n                                       M * inv[s + 2 * j - 2 - l] % M) % n                   M; n           } n         } n       } n     } n     for (int i = 0; i < 55; i++) { n       for (int j = 0; j < 55; j++) { n         for (int k = 0; k < 55; k++) { n           dp1[i][j][k] = dp2[i][j][k]; n         } n       } n     } n   } n   for (int i = 0; i < n; i++) { n     int ans = 0; n     for (int j = 0; j < 55; j++) { n       for (int k = 0; k < 55; k++) { n         if (a[i]) { n           int temp = (long long)(w[i] + k) * dp1[i][j][k] % M; n           ans = (ans + temp) % M; n         } else { n           int temp = (long long)(w[i] - k) * dp1[i][j][k] % M; n           ans = (ans + temp) % M; n         } n       } n     } n     cout << ans << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 200010; n const int MOD = 1000000007; n const int INF = 0x3fffffff; n typedef struct Node { n   long long v; n   int p; n   Node(long long v_, int p_) { n     v = v_; n     p = p_; n   } n   bool operator<(const Node& other) const { n     return v < other.v || (v == other.v && p > other.p); n   } n } Node; n long long a[N]; n int main() { n   ios::sync_with_stdio(false); n   int n, k; n   scanf( %d%d , &n, &k); n   for (int i = 1; i <= n; ++i) { n     scanf( %I64d , &a[i]); n     a[i] += a[i - 1]; n   } n   priority_queue<Node> Q; n   Q.push(Node(a[n] - a[n - k], n + 1 - k)); n   long long ans = 0; n   int ansa, ansb; n   int q = n + 1 - k; n   for (int i = n + 1 - k - k; i > 0; --i) { n     if (ans <= Q.top().v + a[i + k - 1] - a[i - 1]) { n       ans = Q.top().v + a[i + k - 1] - a[i - 1]; n       ansa = i; n       ansb = Q.top().p; n     } n     --q; n     Q.push(Node(a[q + k - 1] - a[q - 1], q)); n   } n   printf( %d %d n , ansa, ansb); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 105; n int n; n int a[N]; n int sum = 0; n char s[N]; n int main() { n   scanf( %d , &n); n   scanf( %s , s + 1); n   for (int i = 1; i <= n; ++i) { n     a[i] = s[i] -  0 ; n     sum += a[i]; n   } n   if (sum == 0) { n     puts( YES ); n     return 0; n   } n   for (int i = 2; i <= sum; ++i) { n     if (sum % i == 0) { n       bool flag = 1; n       int cnt = 0, tot = sum / i, ret = 0; n       for (int j = 1; j <= n; ++j) { n         ret += a[j]; n         if (ret > tot) { n           flag = 0; n           break; n         } else if (ret == tot) { n           ++cnt; n           ret = 0; n         } n       } n       if (cnt != i) flag = 0; n       if (flag) { n         printf( YES ); n         return 0; n       } n     } n   } n   puts( NO ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long n; n   scanf( %lld , &n); n   long long sum[n + 5], ans[n + 5], a; n   cout <<  ? 1 2  << endl; n   cin >> sum[1]; n   cout <<  ? 2 3  << endl; n   cin >> sum[2]; n   cout <<  ? 1 3  << endl; n   cin >> sum[3]; n   ans[1] = (sum[1] + sum[3] - sum[2]) / 2; n   ans[2] = sum[1] - ans[1]; n   ans[3] = sum[3] - ans[1]; n   for (int i = 4; i <= n; i++) { n     cout <<  ?   << i - 1 <<     << i << endl; n     cin >> sum[i]; n     ans[i] = sum[i] - ans[i - 1]; n   } n   cout <<  ! ; n   for (int i = 1; i <= n; i++) { n     cout <<     << ans[i]; n   } n   printf(  n ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n double const eps = 1e-7; n struct Edge { n   int dest, flow, ctpt; n   double cost; n   Edge(int _dest, int _flow, double _cost, int _ctpt) n       : dest(_dest), flow(_flow), cost(_cost), ctpt(_ctpt) {} n }; n vector<Edge> edges[2004]; n int source = 2002, sink = 2003, from[2004]; n double dist[2004], p[2000], u[2000]; n bool inq[2004]; n vector<Edge>::iterator edge_from[2004]; n inline void insert(int from, int to, int flow, double cost) { n   edges[from].push_back(Edge(to, flow, cost, edges[to].size())); n   edges[to].push_back(Edge(from, 0, -cost, edges[from].size() - 1)); n } n inline bool can_augment() { n   static queue<int> q; n   vector<Edge>::iterator it; n   int x; n   memset(dist, 0xf0, sizeof dist); n   dist[source] = 0.0; n   inq[source] = true; n   q.push(source); n   while (!q.empty()) { n     inq[x = q.front()] = false; n     q.pop(); n     for (it = edges[x].begin(); it != edges[x].end(); ++it) n       if (it->flow && dist[it->dest] < dist[x] + it->cost - eps) { n         dist[it->dest] = dist[x] + it->cost; n         from[it->dest] = x; n         edge_from[it->dest] = it; n         if (!inq[it->dest]) { n           inq[it->dest] = true; n           q.push(it->dest); n         } n       } n   } n   return dist[sink] > -1e60; n } n double augment() { n   int i, flow = 0x3f3f3f3f; n   double cost = 0.0; n   for (i = sink; i != source; i = from[i]) flow = min(flow, edge_from[i]->flow); n   for (i = sink; i != source; i = from[i]) { n     cost += edge_from[i]->cost; n     edge_from[i]->flow -= flow; n     edges[i][edge_from[i]->ctpt].flow += flow; n   } n   return flow * cost; n } n int main() { n   int n, a, b, i, A = 2000, B = 2001; n   double ans = 0.0; n   ios_base::sync_with_stdio(false); n   cin >> n >> a >> b; n   for (i = 0; i < n; ++i) cin >> p[i]; n   for (i = 0; i < n; ++i) cin >> u[i]; n   for (i = 0; i < n; ++i) { n     insert(A, i, 1, p[i]); n     insert(B, i, 1, u[i]); n     insert(i, sink, 1, 0.0); n     insert(i, sink, 1, -p[i] * u[i]); n   } n   insert(source, A, a, 0.0); n   insert(source, B, b, 0.0); n   while (can_augment()) ans += augment(); n   cout << fixed << ans <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void solve() { n   vector<int> v = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47}; n   int pos = -1, cnt = 0; n   for (int i = 0; i < v.size(); i++) { n     cout << v[i] << endl; n     string s; n     cin >> s; n     if (s ==  yes ) pos = i, cnt++; n   } n   int f = 0; n   if (cnt > 1) n     f = 1; n   else if (pos != -1 && v[pos] * v[pos] <= 100) { n     cout << v[pos] * v[pos] << endl; n     string s; n     cin >> s; n     if (s ==  yes ) f = 1; n   } n   if (f) n     cout <<  composite  << endl; n   else n     cout <<  prime  << endl; n } n int main() { n   solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 2010; n int a[maxn], b[maxn], tot, n, m; n pair<int, int> ans1, ans21, ans22; n pair<long long, pair<int, int> > two[maxn * maxn]; n int main(void) { n   long long s = 0, s2 = 1e18, s3 = 1e18; n   scanf( %d , &n); n   for (int i = 1; i <= n; i++) scanf( %d , &a[i]), s += a[i]; n   scanf( %d , &m); n   for (int i = 1; i <= m; i++) scanf( %d , &b[i]), s -= b[i]; n   for (int i = 1; i <= n; i++) n     for (int j = 1; j <= m; j++) { n       long long tem = abs(s + 2 * b[j] - 2 * a[i]); n       if (tem < s2) s2 = tem, ans1 = make_pair(i, j); n     } n   for (int i = 1; i <= m; i++) n     for (int j = i + 1; j <= m; j++) n       two[++tot] = make_pair(2ll * b[i] + 2ll * b[j], make_pair(i, j)); n   sort(two + 1, two + 1 + tot); n   for (int i = 1; i <= n; i++) n     for (int j = i + 1; j <= n; j++) { n       long long tmp = 2ll * a[i] + 2ll * a[j] - s; n       int p = n           lower_bound(two + 1, two + 1 + tot, make_pair(tmp, make_pair(0, 0))) - n           two - 1; n       for (int t = max(1, p - 2); t <= min(tot, p + 2); t++) { n         long long tem = abs(s + two[t].first - 2ll * a[i] - 2ll * a[j]); n         if (tem < s3) { n           s3 = tem; n           ans21 = make_pair(i, two[t].second.first); n           ans22 = make_pair(j, two[t].second.second); n         } n       } n     } n   long long ans = min(min(abs(s), abs(s2)), abs(s3)); n   printf( %lld n , ans); n   if (ans == s) n     printf( 0 n ); n   else if (ans == abs(s2)) n     printf( 1 n%d %d n , ans1.first, ans1.second); n   else n     printf( 2 n%d %d n%d %d n , ans21.first, ans21.second, ans22.first, n            ans22.second); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int INF = 0x3f3f3f3f; n const long long LINF = 0x3f3f3f3f3f3f3f3fLL; n const double EPS = 1e-8; n const int MOD = 1000000007; n const int dy[] = {1, 0, -1, 0}, dx[] = {0, -1, 0, 1}; n int main() { n   cin.tie(0); n   ios::sync_with_stdio(false); n   int n; n   cin >> n; n   vector<long long> s(n + 1, 0); n   for (int i = (1); i < (n + 1); ++i) cin >> s[i]; n   for (int i = (1); i < (n + 1); ++i) s[i] += s[i - 1]; n   int q; n   cin >> q; n   while (q--) { n     int l, r; n     cin >> l >> r; n     --l; n     --r; n     cout << (s[r + 1] - s[l]) / 10 <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int a[100], k[100]; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   ; n   int t, n; n   cin >> t; n   for (int i = 0; i < t; ++i) { n     cin >> n; n     n *= 2; n     for (int j = 0; j < n; ++j) cin >> a[j]; n     for (int j = 0; j < n; ++j) { n       if (k[a[j]] == 0) { n         cout << a[j] <<    ; n         ++k[a[j]]; n       } n     } n     cout <<   n ; n     for (int j = 0; j <= 100; ++j) k[j] = 0; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int inf = 2e9; n const long double eps = 0.000001; n const long long mod = 1e9 + 7; n const long double PI = 3.14159265359; n template <class T> n struct MyVector : vector<T> { n   MyVector() : vector<T>() {} n   MyVector(int n) : vector<T>(n) {} n   T &operator[](size_t i) { return vector<T>::at(i); } n }; n string t; n string p; n vector<int> a; n bool check(int m) { n   vector<int> u((int)t.size(), false); n   for (int i = 0; i < m; i++) { n     u[a[i]] = true; n   } n   string ns; n   for (int i = 0; i < (int)t.size(); i++) { n     if (u[i]) continue; n     ns.push_back(t[i]); n   } n   int idx = 0; n   for (int i = 0; i < (int)p.size(); i++) { n     bool fl = false; n     for (; idx < (int)ns.size(); idx++) { n       if (ns[idx] == p[i]) { n         idx++; n         fl = true; n         break; n       } n     } n     if (!fl) return false; n   } n   return true; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(nullptr); n   cout.tie(nullptr); n   cin >> t >> p; n   a.resize((int)t.size()); n   for (int i = 0; i < (int)t.size(); i++) { n     cin >> a[i]; n     a[i]--; n   } n   int l = -1, r = (int)t.size() + 1; n   while (r - l > 1) { n     int m = (l + r) / 2; n     if (check(m)) n       l = m; n     else n       r = m; n   } n   cout << l; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 1e5 + 50; n int b[maxn]; n int cnt; n int main() { n   std::ios::sync_with_stdio(false); n   int t; n   cin >> t; n   while (t--) { n     string a; n     cin >> a; n     cnt = 0; n     bool flag = true; n     for (int i = 0; i < a.size(); i++) { n       int t = a[i] -  a ; n       b[cnt++] = t; n     } n     sort(b, b + cnt); n     for (int i = 1; i < cnt; i++) { n       if (b[i] - b[i - 1] != 1) { n         flag = false; n       } n     } n     if (flag) n       cout <<  YES  << endl; n     else n       cout <<  NO  << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 150 + 10; n int a[maxn][maxn]; n int n; n int main() { n   scanf( %d , &n); n   a[0][0] = 1; n   a[1][1] = 1; n   for (int i = 2; i <= n; i++) { n     for (int j = 0; j <= i; j++) { n       if (j > 0) a[i][j] = a[i - 1][j - 1]; n       if (j <= i - 2) { n         if (a[i][j] + a[i - 2][j] > 1) n           a[i][j] -= a[i - 2][j]; n         else n           a[i][j] += a[i - 2][j]; n       } n     } n   } n   printf( %d n , n); n   for (int i = 0; i <= n; i++) { n     printf( %d  , a[n][i]); n   } n   printf(  n ); n   printf( %d n , n - 1); n   for (int i = 0; i < n; i++) { n     printf( %d  , a[n - 1][i]); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 1e5 + 10; n int b[maxn]; n char a[maxn]; n int num[2 * maxn]; n int main() { n   int n; n   scanf( %d , &n); n   int count0 = 0, count1 = 0; n   memset(num, -1, sizeof(num)); n   int len = 0, maxlen = 0; n   scanf( %s , a + 1); n   for (int i = 1; i <= n; i++) { n     if (a[i] ==  0 ) n       count0++; n     else n       count1++; n     b[i] = count1 - count0; n     if (b[i] == 0) { n       if (maxlen < i) maxlen = i; n       continue; n     } n     if (num[b[i] + maxn] == -1) n       num[b[i] + maxn] = i; n     else { n       len = i - num[b[i] + maxn]; n       if (maxlen < len) maxlen = len; n     } n   } n   printf( %d n , maxlen); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long t, n, m, k, rem, each, x, y, i, j, ans; n   cin >> t; n   while (t--) { n     cin >> n >> m >> x >> y; n     string s[n]; n     long long one = 0, two = 0; n     for (i = 0; i < n; i++) { n       cin >> s[i]; n     } n     for (i = 0; i < n; i++) { n       for (j = 0; j < m; j++) { n         if (s[i][j] ==  . ) one++; n         if (j == 0) continue; n         if (s[i][j] ==  .  && s[i][j - 1] ==  . ) { n           two++; n           s[i][j] =  $ ; n           s[i][j - 1] =  $ ; n         } n       } n     } n     long long p1, p2; n     p1 = x * one; n     p2 = y * two + x * (one - two * 2); n     ans = min(p1, p2); n     cout << ans << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n constexpr long long mod = 1000000007; n pair<long long, long long> dfs(int v, const vector<vector<int>> &children, n                                const vector<int> &black) { n   long long zero = 0; n   long long one = 0; n   (black[v] ? one : zero) = 1; n   for (const auto &child : children[v]) { n     const auto result = dfs(child, children, black); n     long long t_zero = (zero * result.second + zero * result.first) % mod; n     one = n         (one * result.second + zero * result.second + one * result.first) % mod; n     zero = t_zero; n   } n   return {zero, one}; n } n int main() { n   cin.tie(nullptr); n   ios::sync_with_stdio(false); n   int n; n   cin >> n; n   vector<vector<int>> children(n); n   for (int i = 1; i < n; ++i) { n     int p; n     cin >> p; n     children[p].emplace_back(i); n   } n   vector<int> black(n); n   for (auto &e : black) cin >> e; n   const auto ans = dfs(0, children, black); n   cout << ans.second << endl; n   return EXIT_SUCCESS; n } n


#include <bits/stdc++.h> n using namespace std; n const int INF = 1e9 + 7; n int dp[80][80][80][2]; n char c[80]; n vector<int> V, K, a; n int cal(int id, vector<int> vv, int pos) { n   int cnt = 0; n   for (int i = id; i < ((int)vv.size()); i++) n     if (vv[i] < pos) cnt++; n   return cnt; n } n int cc(int i, int j, int k, int pos) { n   return cal(i, V, pos) + cal(j, K, pos) + cal(k, a, pos); n } n void mi(int &a, int b) { n   if (a > b) a = b; n } n int main() { n   int n; n   scanf( %d , &n); n   scanf( %s , c); n   for (int i = 0; i < n; i++) { n     if (c[i] ==  V ) n       V.push_back(i); n     else if (c[i] ==  K ) n       K.push_back(i); n     else n       a.push_back(i); n   } n   for (int i = 0; i <= ((int)V.size()); i++) n     for (int j = 0; j <= ((int)K.size()); j++) n       for (int k = 0; k <= ((int)a.size()); k++) n         for (int l = 0; l < 2; l++) dp[i][j][k][l] = INF; n   dp[0][0][0][0] = 0; n   for (int i = 0; i <= ((int)V.size()); i++) n     for (int j = 0; j <= ((int)K.size()); j++) n       for (int k = 0; k <= ((int)a.size()); k++) n         for (int l = 0; l < 2; l++) { n           if (i + 1 <= ((int)V.size())) n             mi(dp[i + 1][j][k][1], dp[i][j][k][l] + cc(i, j, k, V[i])); n           if (j + 1 <= ((int)K.size()) && l == 0) n             mi(dp[i][j + 1][k][0], dp[i][j][k][l] + cc(i, j, k, K[j])); n           if (k + 1 <= ((int)a.size())) n             mi(dp[i][j][k + 1][0], dp[i][j][k][l] + cc(i, j, k, a[k])); n         } n   printf( %d n , min(dp[((int)V.size())][((int)K.size())][((int)a.size())][0], n                      dp[((int)V.size())][((int)K.size())][((int)a.size())][1])); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long n, m, k; n   cin >> n >> m >> k; n   long long l = 0, h = n * m + 2; n   while (l < h) { n     long long md = (l + h) / 2; n     long long rs = 0; n     for (int i = 1; i <= n; i++) rs += min(m, md / i); n     if (rs < k) n       l = md + 1; n     else n       h = md; n   } n   cout << l << endl; n } n


/*████████████████████████████████████████████████████████████████████████████████████ n ██████████████████████████████████████████████████████████████████████████████████████ n ███████████████████████████▓▓▓▓▓▓▓▓▓╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬▓▓▓╬╬╬╬╬╬▓███████████████████████ n ███████████████████████████▓███████▓▓╬╬╬╬╬╬╬╬╬╬╬╬▓███▓▓▓▓█▓╬╬╬▓███████████████████████ n ███████████████████████████████▓█████▓▓╬╬╬╬╬╬╬╬▓███▓╬╬╬╬╬╬╬▓╬╬▓███████████████████████ n ████████████████████████████▓▓▓▓╬╬▓█████╬╬╬╬╬╬███▓╬╬╬╬╬╬╬╬╬╬╬╬╬███████████████████████ n ███████████████████████████▓▓▓▓╬╬╬╬╬╬▓██╬╬╬╬╬╬▓▓╬╬╬╬╬╬╬╬╬╬╬╬╬╬▓███████████████████████ n ████████████████████████████▓▓▓╬╬╬╬╬╬╬▓█▓╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬▓███████████████████████ n ███████████████████████████▓█▓███████▓▓███▓╬╬╬╬╬╬▓███████▓╬╬╬╬▓███████████████████████ n ████████████████████████████████████████▓█▓╬╬╬╬╬▓▓▓▓▓▓▓▓╬╬╬╬╬╬╬███████████████████████ n ███████████████████████████▓▓▓▓▓▓▓╬╬▓▓▓▓▓█▓╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬▓███████████████████████ n ████████████████████████████▓▓▓╬╬╬╬▓▓▓▓▓▓█▓╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬▓███████████████████████ n ███████████████████████████▓█▓▓▓▓▓▓▓▓▓▓▓▓▓▓╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬▓███████████████████████ n █████████████████████████████▓▓▓▓▓▓▓▓█▓▓▓█▓╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬▓███████████████████████ n █████████████████████████████▓▓▓▓▓▓▓██▓▓▓█▓╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬████████████████████████ n █████████████████████████████▓▓▓▓▓████▓▓▓█▓╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬████████████████████████ n ████████████████████████████▓█▓▓▓▓██▓▓▓▓██╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬████████████████████████ n ████████████████████████████▓▓███▓▓▓▓▓▓▓██▓╬╬╬╬╬╬╬╬╬╬╬╬█▓╬▓╬╬▓████████████████████████ n █████████████████████████████▓███▓▓▓▓▓▓▓▓████▓▓╬╬╬╬╬╬╬█▓╬╬╬╬╬▓████████████████████████ n █████████████████████████████▓▓█▓███▓▓▓████╬▓█▓▓╬╬╬▓▓█▓╬╬╬╬╬╬█████████████████████████ n ██████████████████████████████▓██▓███████▓╬╬╬▓▓╬▓▓██▓╬╬╬╬╬╬╬▓█████████████████████████ n ███████████████████████████████▓██▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓╬╬╬╬╬╬╬╬╬╬╬██████████████████████████ n ███████████████████████████████▓▓██▓▓▓▓▓╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬▓██████████████████████████ n ████████████████████████████████▓▓▓█████▓▓╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬▓███████████████████████████ n █████████████████████████████████▓▓▓█▓▓▓▓▓███▓╬╬╬╬╬╬╬╬╬╬╬▓████████████████████████████ n ██████████████████████████████████▓▓▓█▓▓▓╬▓██╬╬╬╬╬╬╬╬╬╬╬▓█████████████████████████████ n ███████████████████████████████████▓▓█▓▓▓▓███▓╬╬╬╬╬╬╬╬╬▓██████████████████████████████ n ██████████████████████████████████████▓▓▓███▓▓╬╬╬╬╬╬╬╬████████████████████████████████ n ███████████████████████████████████████▓▓▓██▓▓╬╬╬╬╬╬▓█████████████████████████████████ n ██████████████████████████████████████████████████████████████████████████████████████ n  */ n /* n   ______  __    __ __       __ ________ _______       __    __  ______   ______   ______   ______  __    __ n  /       |    |         /                      |    |   /       /       /       /       |    |    n |  ▓▓▓▓▓▓ ▓▓  | ▓▓ ▓▓   /  ▓▓ ▓▓▓▓▓▓▓▓ ▓▓▓▓▓▓▓    | ▓▓  | ▓▓  ▓▓▓▓▓▓  ▓▓▓▓▓▓  ▓▓▓▓▓▓  ▓▓▓▓▓▓ ▓▓ | ▓▓ n | ▓▓__| ▓▓ ▓▓__| ▓▓ ▓▓▓ /  ▓▓▓ ▓▓__   | ▓▓  | ▓▓    | ▓▓__| ▓▓ ▓▓__| ▓▓ ▓▓___ ▓▓ ▓▓___ ▓▓ ▓▓__| ▓▓ ▓▓▓ | ▓▓ n | ▓▓    ▓▓ ▓▓    ▓▓ ▓▓▓▓  ▓▓▓▓ ▓▓    | ▓▓  | ▓▓    | ▓▓    ▓▓ ▓▓    ▓▓ ▓▓      ▓▓     | ▓▓    ▓▓ ▓▓▓▓ ▓▓ n | ▓▓▓▓▓▓▓▓ ▓▓▓▓▓▓▓▓ ▓▓ ▓▓ ▓▓ ▓▓ ▓▓▓▓▓  | ▓▓  | ▓▓    | ▓▓▓▓▓▓▓▓ ▓▓▓▓▓▓▓▓_ ▓▓▓▓▓▓ _ ▓▓▓▓▓▓ ▓▓▓▓▓▓▓▓ ▓▓ ▓▓ ▓▓ n | ▓▓  | ▓▓ ▓▓  | ▓▓ ▓▓  ▓▓▓| ▓▓ ▓▓_____| ▓▓__/ ▓▓    | ▓▓  | ▓▓ ▓▓  | ▓▓   __| ▓▓   __| ▓▓ ▓▓  | ▓▓ ▓▓  ▓▓▓▓ n | ▓▓  | ▓▓ ▓▓  | ▓▓ ▓▓   ▓ | ▓▓ ▓▓      ▓▓    ▓▓    | ▓▓  | ▓▓ ▓▓  | ▓▓ ▓▓    ▓▓ ▓▓    ▓▓ ▓▓  | ▓▓ ▓▓   ▓▓▓ n   ▓▓    ▓▓ ▓▓    ▓▓ ▓▓       ▓▓ ▓▓▓▓▓▓▓▓ ▓▓▓▓▓▓▓       ▓▓    ▓▓ ▓▓    ▓▓  ▓▓▓▓▓▓   ▓▓▓▓▓▓  ▓▓    ▓▓ ▓▓    ▓▓ n  n  */ n  n  n /* n  __    __                         __ __                          __                    ______   ______  _______   ______       __ n |    |                         |                            |                    /       /       |        /           |    n | ▓▓  | ▓▓ ______   ______   ____| ▓▓ ▓▓_______   ______       _| ▓▓_    ______      |  ▓▓▓▓▓▓  ▓▓▓▓▓▓ ▓▓▓▓▓▓▓  ▓▓▓▓▓▓      ▓▓ _______  ______ n | ▓▓__| ▓▓/       |       /      ▓▓          /           |   ▓▓   /           | ▓▓__| ▓▓ ▓▓    ▓▓ ▓▓__/ ▓▓ ▓▓    ▓▓    |   /        |        n | ▓▓    ▓▓  ▓▓▓▓▓▓  ▓▓▓▓▓▓  ▓▓▓▓▓▓▓ ▓▓ ▓▓▓▓▓▓▓  ▓▓▓▓▓▓      ▓▓▓▓▓▓ |  ▓▓▓▓▓▓    | ▓▓    ▓▓ ▓▓     | ▓▓    ▓▓ ▓▓          | ▓▓  ▓▓▓▓▓▓▓  ▓▓▓▓▓▓  n | ▓▓▓▓▓▓▓▓ ▓▓    ▓▓/      ▓▓ ▓▓  | ▓▓ ▓▓ ▓▓  | ▓▓ ▓▓  | ▓▓      | ▓▓ __| ▓▓  | ▓▓    | ▓▓▓▓▓▓▓▓ ▓▓   __| ▓▓▓▓▓▓▓| ▓▓   __     | ▓▓ ▓▓     /      ▓▓ n | ▓▓  | ▓▓ ▓▓▓▓▓▓▓▓  ▓▓▓▓▓▓▓ ▓▓__| ▓▓ ▓▓ ▓▓  | ▓▓ ▓▓__| ▓▓      | ▓▓|   ▓▓__/ ▓▓    | ▓▓  | ▓▓ ▓▓__/   ▓▓     | ▓▓__/      | ▓▓_ ▓▓▓▓▓▓  ▓▓▓▓▓▓▓ n | ▓▓  | ▓▓ ▓▓       ▓▓    ▓▓ ▓▓    ▓▓ ▓▓ ▓▓  | ▓▓ ▓▓    ▓▓        ▓▓  ▓▓ ▓▓    ▓▓    | ▓▓  | ▓▓ ▓▓    ▓▓ ▓▓       ▓▓    ▓▓    | ▓▓       ▓▓ ▓▓    ▓▓ n   ▓▓    ▓▓  ▓▓▓▓▓▓▓  ▓▓▓▓▓▓▓  ▓▓▓▓▓▓▓ ▓▓ ▓▓    ▓▓_ ▓▓▓▓▓▓▓         ▓▓▓▓   ▓▓▓▓▓▓       ▓▓    ▓▓  ▓▓▓▓▓▓  ▓▓        ▓▓▓▓▓▓       ▓▓ ▓▓▓▓▓▓▓   ▓▓▓▓▓▓▓ n                                                 |   __| ▓▓ n                                                   ▓▓    ▓▓ n                                                    ▓▓▓▓▓▓ n */ n  n #include<bits/stdc++.h> n #define ll                  long long n #define ld                  long double n #define f(i,n)              for(ll i=0;i<(n);i++) n #define f1(i,n)             for(ll i=1;i<=n;i++) n #define el                 n  n #define sq(a)               (a)*(a) n #define pb                  push_back n #define asort(a,n)            sort(a,a+n) n #define dsort(a,n)            sort(a,a+n,greater<>()) n #define vasort(v)           sort(v.begin(), v.end()); n #define vdsort(v)           sort(v.begin(), v.end(),greater<>()); n #define cina(arr)            f(i,n) cin >> arr[i]; n #define YES                 cout <<  YES n  n #define Yes                 cout <<  Yes <<el n #define yes                 cout <<  yes <<el n #define NO                  cout <<  NO n  n #define No                  cout <<  No <<el n #define no                  cout <<  no <<el n #define covid19             ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0); n #define TC                  int t; cin >> t; while(t--) n  n const double pi=acos(-1); n using namespace std; n  n  n int main() { n #ifndef ONLINE_JUDGE n     clock_t start = clock(); n #endif n  n     covid19; n     TC { n         ll n; n         cin >> n; n         for (ll i = 0; i < 32; i++) { n             ll x = pow(2, i); n             if (x > n) { n                 cout << x / 2 - 1; n                 break; n             } n         } n         cout << el; n     } n #ifndef ONLINE_JUDGE n     clock_t end = clock(); n     double time_taken = double(end - start) / double(CLOCKS_PER_SEC); n     cout <<  Time taken:   << time_taken; n     cout <<   sec  n ; n #endif n } n  n  n /*  NOTESSS n decimal digit root :: n  d(n)=1+((n−1)mod9) n  */


#include <bits/stdc++.h> n using namespace std; n const double EPS = 1e-9; n const int MOD = 1e9 + 7; n int dx[8] = {0, 0, 1, -1, -1, 1, -1, 1}, dy[8] = {1, -1, 0, 0, -1, -1, 1, 1}; n void INPUT() {} n void OUTPUT() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n } n int main() { n   INPUT(); n   OUTPUT(); n   string s; n   cin >> s; n   string ans =   , s1 =   , s0 =   ; n   for (int i = ((int)((s).size())) - 1; i >= 0; i--) { n     if (s[i] ==  0 ) { n       ans +=  0 ; n       s0 +=  0 ; n     } else if (s[i] ==  2 ) { n       ans +=  2 ; n       s0 =   ; n     } else { n       s1 +=  1 ; n     } n   } n   cout << s0 + s1; n   int i = ((int)((ans).size())) - 1; n   while (ans[i] ==  0 ) { n     i--; n   } n   while (i > -1) { n     cout << ans[i]; n     i--; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void solve(); n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   solve(); n   return 0; n } n void solve() { n   long long a, b; n   cin >> a >> b; n   long long ct = 0; n   while (a > 0 && b > 0) { n     if (a > b) { n       ct += a / b; n       a = a % b; n     } else { n       ct += b / a; n       b = b % a; n     } n   } n   cout << ct; n } n


#include <bits/stdc++.h> n using namespace std; n struct query { n   long long l, r, x; n }; n int n, a[109]; n long long m; n int main() { n   cin >> n >> m; n   vector<query> v; n   vector<long long> cp = {1LL << 60}; n   for (int i = 0; i < n; i++) { n     cin >> a[i]; n     int pr = sqrt(a[i]); n     for (int j = 1; j < pr; j++) { n       v.push_back(query{j, j + 1, (a[i] + j - 1) / j}); n       cp.push_back(j); n     } n     cp.push_back(pr); n     int r = (a[i] + pr - 1) / pr, ptr = pr; n     for (int j = r; j > 1; j--) { n       v.push_back(query{ptr, (a[i] + j - 2) / (j - 1), j}); n       ptr = v.back().r; n       cp.push_back(ptr); n     } n     v.push_back(query{ptr, 1LL << (59 + 1), 1}); n   } n   sort(cp.begin(), cp.end()); n   cp.erase(unique(cp.begin(), cp.end()), cp.end()); n   vector<long long> sum(cp.size()); n   for (query i : v) { n     int pl = lower_bound(cp.begin(), cp.end(), i.l) - cp.begin(); n     int pr = lower_bound(cp.begin(), cp.end(), i.r) - cp.begin(); n     sum[pl] += i.x; n     sum[pr] -= i.x; n   } n   for (int i = 1; i < cp.size(); i++) sum[i] += sum[i - 1]; n   long long ret = 0; n   for (int i = 0; i < cp.size() - 1; i++) { n     long long r = min(cp[i + 1] - 1, (m + sum[0]) / sum[i]); n     if (cp[i] <= r) ret = max(ret, r); n   } n   cout << ret << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long int mod = 1000000007; n const long long int inf = 0x3f3f3f3f3f3f3f3fLL; n const double eps = 1e-9; n const double PI = acos(-1); n long long int i, j, k; n void solve(void); n inline bool equal(double a, double b) { return fabs(a - b) < eps; } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout << fixed << setprecision(10); n   int t = 1; n   while (t--) solve(); n   return 0; n } n void solve() { n   int n, a, b, T; n   cin >> n >> a >> b >> T; n   char c[n]; n   for (i = (0); i <= (n - 1); ++i) cin >> c[i]; n   int lo = 1, hi = n, mid, ans = 0, left[n], right[n]; n   left[0] = right[0] = 1 + (c[0] ==  w  ? b : 0); n   for (i = (1); i <= (n - 1); ++i) n     left[i] = left[i - 1] + a + (c[n - i] ==  w  ? b : 0) + 1; n   for (i = (1); i <= (n - 1); ++i) n     right[i] = right[i - 1] + a + (c[i] ==  w  ? b : 0) + 1; n   while (lo <= hi) { n     mid = (lo + hi) >> 1; n     bool pos = 0; n     for (i = (mid - 1); i >= (0); --i) { n       long long int time1 = right[i] + left[mid - i - 1] - left[0] + i * a; n       if (time1 <= T) pos = 1; n       long long int time2 = left[i] + right[mid - i - 1] - right[0] + i * a; n       if (time2 <= T) pos = 1; n     } n     if (pos) { n       ans = ((ans) > (mid) ? (ans) : (mid)); n       lo = mid + 1; n     } else n       hi = mid - 1; n   } n   cout << ans <<   n ; n } n