#include <bits/stdc++.h> n using namespace std; n inline int read() { n   int x = 0; n   char ch = getchar(); n   while (ch <  0  || ch >  9 ) ch = getchar(); n   while (ch >=  0  && ch <=  9 ) x = x * 10 + ch -  0 , ch = getchar(); n   return x; n } n vector<pair<int, int> > v[1000000 + 5]; n int s[1000000 / 5], num = 0, last[1000000 + 5], vis[1000000 + 5], n                     la[1000000 + 5], lastans = 0, n, q, mx, a[1000000 + 5], n                     rt[1000000 + 5], cnt = 0, inv[1000000 + 5], n                     Ans[1000000 + 5]; n struct Tree { n   int l, r, x; n } T[1000000 * 4 + 5]; n bool b[1000000 + 5]; n int Build(int l, int r) { n   int x = ++cnt; n   T[x].x = 1; n   if (l != r) { n     int mid = l + r >> 1; n     T[x].l = Build(l, mid); n     T[x].r = Build(mid + 1, r); n   } n   return x; n } n void Modify(int x, int l, int r, int lt, int rt, int v) { n   if (l == lt && r == rt) { n     T[x].x = 1LL * T[x].x * v % 1000000007; n     return; n   } n   int mid = lt + rt >> 1; n   if (r <= mid) n     Modify(T[x].l, l, r, lt, mid, v); n   else if (l > mid) n     Modify(T[x].r, l, r, mid + 1, rt, v); n   else n     Modify(T[x].l, l, mid, lt, mid, v), n         Modify(T[x].r, mid + 1, r, mid + 1, rt, v); n } n void Query(int x, int k, int lt, int rt) { n   lastans = 1LL * lastans * T[x].x % 1000000007; n   if (lt == rt) return; n   int mid = lt + rt >> 1; n   if (k <= mid) n     Query(T[x].l, k, lt, mid); n   else n     Query(T[x].r, k, mid + 1, rt); n } n int main() { n   n = read(); n   inv[0] = inv[1] = 1; n   for (int i = 1; i <= n; ++i) mx = max(mx, a[i] = read()); n   q = read(); n   for (register int i = 2; i <= mx; ++i) { n     inv[i] = n         1LL * (1000000007 - 1000000007 / i) * inv[1000000007 % i] % 1000000007; n     if (!b[i]) s[++num] = i, last[i] = i; n     for (int j = 1; s[j] * i <= mx; ++j) { n       b[s[j] * i] = 1; n       last[s[j] * i] = s[j]; n       if (i % s[j] == 0) break; n     } n   } n   rt[0] = Build(1, n); n   for (int i = 1; i <= q; ++i) { n     int l = read(), r = read(); n     v[r].push_back(make_pair(l, i)); n   } n   for (register int i = 1; i <= n; ++i) { n     Modify(rt[i] = rt[i - 1], 1, i, 1, n, a[i]); n     for (int t = a[i]; t > 1; t /= last[t]) n       if (vis[last[t]] != i) { n         vis[last[t]] = i; n         Modify(rt[i], la[last[t]] + 1, i, 1, n, n                1LL * inv[last[t]] * (last[t] - 1) % 1000000007); n         la[last[t]] = i; n       } n     for (int j = 0; j < v[i].size(); ++j) n       lastans = 1, Query(rt[i], v[i][j].first, 1, n), n       Ans[v[i][j].second] = lastans; n   } n   for (int i = 1; i <= q; ++i) printf( %d n , Ans[i]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n using LL = long long; n const LL MOD = 998244353; n string s; n LL cal(int x) { n   if (x < 10 || x > 18) return 0; n   return 9 - (x - 10); n } n struct SegmentTree { n   struct Node { n     int L, R, l, r; n     LL v[2][2]; n   }; n   vector<Node> N; n   int p; n   vector<int> rt; n   int n; n   inline void init(int _n) { n     n = _n; n     N.resize(n * 4); n     p = 1; n     rt.clear(); n     rt.push_back(build(0, n)); n   } n   inline int copy(Node u) { return N[p] = u, p++; } n   int build(int L, int R) { n     int u = copy({L, R}), M = (L + R) >> 1; n     ; n     if (R - L == 1) n       return N[u].v[0][0] = s[L] -  0  + 1, N[u].v[0][1] = 1, N[u].v[1][0] = 1, n              N[u].v[1][1] = 0, u; n     return pull(u, build(L, M), build(M, R)); n   } n   inline int push(int u) { return u; } n   inline int pull(int u, int l, int r) { n     if (!l || !r) return l ? l : r; n     push(N[u].l = l); n     push(N[u].r = r); n     for (int i = 0; i < 2; i++) { n       for (int j = 0; j < 2; j++) { n         N[u].v[i][j] = n             (N[l].v[i][0] * N[r].v[0][j] + n              N[l].v[i][1] * N[r].v[1][j] % MOD * n                  cal((s[N[l].R - 1] -  0 ) * 10 + (s[N[r].L] -  0 ))) % n             MOD; n       } n     } n     return u; n   } n   int m(int L, int R, int u, int v) { n     if (!u || R <= N[u].L || N[u].R <= L) return u; n     push(u); n     if (L <= N[u].L && N[u].R <= R) return N[u].v[0][0] = v + 1, u; n     return pull(u, m(L, R, N[u].l, v), m(L, R, N[u].r, v)); n   } n   int q(int L, int R, int u) { n     if (!u || R <= N[u].L || N[u].R <= L) return 0; n     if (L <= N[u].L && N[u].R <= R) return u; n     push(u); n     return pull(copy(N[u]), q(L, R, N[u].l), q(L, R, N[u].r)); n   } n   inline void modify(int L, int R, int ver, int v) { n     rt[ver] = m(L, R, rt[ver], v); n   } n   inline Node& query(int L, int R, int ver) { n     p = 2 * n + 1; n     return N[q(L, R, rt[ver])]; n   } n } solver; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   int n, q; n   cin >> n >> q; n   cin >> s; n   solver.init(n); n   while (q--) { n     int x, d; n     cin >> x >> d; n     x--; n     s[x] =  0  + d; n     solver.modify(x, x + 1, 0, d); n     auto v = solver.query(0, n, 0).v; n     cout << v[0][0] <<   n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n vector<int> av; n int main() { n   int n; n   scanf( %d , &n); n   for (int i = max(1, n - 100); i <= n; i++) { n     int j = i, ans = i; n     while (j) { n       ans += j % 10; n       j /= 10; n     } n     if (ans == n) av.push_back(i); n   } n   int avs = av.size(); n   printf( %d n , avs); n   for (int i = 0; i < avs; i++) { n     printf( %d n , av[i]); n   } n   return 0; n } n


#include <bits/stdc++.h> n #pragma GCC optimize( O3 ) n #pragma GCC optimize( Ofast ) n using namespace std; n long long int n, res[200005], cnt = 0, ans[200005]; n vector<long long int> g[200005]; n vector<long long int> v[200005]; n map<long long int, long long int> c; n queue<long long int> q; n void dfs1(long long int node, long long int color) { n   c[node] = color; n   for (auto it : g[node]) { n     if (!c[it]) { n       dfs1(it, color % 2 + 1); n     } n   } n } n void dfs2(long long int node, long long int p) { n   for (auto it : g[node]) { n     if (it != p) { n       dfs2(it, node); n     } n   } n   for (auto it : g[node]) { n     if (c[it] != c[node] && res[it] == 0 && res[node] == 0) { n       cnt++; n       res[it] = cnt; n       res[node] = cnt; n       q.push(it); n       q.push(node); n     } n   } n } n int32_t main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   ; n   cin >> n; n   for (long long int i = 1; i < n; i++) { n     long long int x, y; n     cin >> x >> y; n     g[x].push_back(y); n     g[y].push_back(x); n   } n   dfs1(1, 1); n   dfs2(1, 1); n   while (!q.empty()) { n     long long int x = q.front(); n     q.pop(); n     for (auto it : g[x]) { n       if (res[it] == 0) { n         res[it] = res[x]; n         q.push(it); n       } n     } n   } n   for (long long int i = 1; i <= n; i++) { n     v[res[i]].push_back(i); n   } n   long long int mn = 0; n   for (long long int i = 1; i <= cnt; i++) { n     mn += (v[i].size() - 1) * 2; n     for (long long int j = 0; j < v[i].size(); j++) { n       ans[v[i][j]] = v[i][(j + 1) % v[i].size()]; n     } n   } n   cout << mn <<   n ; n   for (long long int i = 1; i <= n; i++) { n     cout << ans[i] <<    ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int block_size = 320; n const long long mod = 1e9 + 7; n const long long inf = 1e9 + 7; n const long double eps = 1e-9; n const double PI = atan(1) * 4; n template <typename T> n inline int sign(const T &a) { n   if (a < 0) return -1; n   if (a > 0) return 1; n   return 0; n } n string to_string(string s) { return     + s +    ; } n string to_string(const char *s) { return to_string((string)s); } n string to_string(bool b) { return (b ?  true  :  false ); } n template <typename A, typename B> n string to_string(pair<A, B> p) { n   return  (  + to_string(p.first) +  ,   + to_string(p.second) +  ) ; n } n template <typename A> n string to_string(A v) { n   bool first = true; n   string res =  { ; n   for (const auto &x : v) { n     if (!first) { n       res +=  ,  ; n     } n     first = false; n     res += to_string(x); n   } n   res +=  } ; n   return res; n } n void debug_out() { cerr << endl; } n template <typename Head, typename... Tail> n void debug_out(Head H, Tail... T) { n   cerr <<     << to_string(H); n   debug_out(T...); n } n template <typename T, typename S> n inline bool upmin(T &a, const S &b) { n   return a > b ? a = b, 1 : 0; n } n template <typename T, typename S> n inline bool upmax(T &a, const S &b) { n   return a < b ? a = b, 1 : 0; n } n template <typename T> n inline void in(T &x) { n   x = 0; n   T f = 1; n   char ch = getchar(); n   while (!isdigit(ch)) { n     if (ch ==  - ) f = -1; n     ch = getchar(); n   } n   while (isdigit(ch)) { n     x = x * 10 + ch -  0 ; n     ch = getchar(); n   } n   x *= f; n } n long long twop(int x) { return 1LL << x; } n template <typename A, typename B> n inline void in(A &x, B &y) { n   in(x); n   in(y); n } n template <typename A, typename B, typename C> n inline void in(A &x, B &y, C &z) { n   in(x); n   in(y); n   in(z); n } n template <typename A, typename B, typename C, typename D> n inline void in(A &x, B &y, C &z, D &d) { n   in(x); n   in(y); n   in(z); n   in(d); n } n long long gcd(long long a, long long b) { return b == 0 ? a : gcd(b, a % b); } n namespace SOLVE { n int n, q; n string s; n int nxt[100010][26]; n int dp[255][255][255]; n vector<int> word[3]; n void solve(int i, int j, int k) { n   dp[i][j][k] = inf; n   if (i and dp[i - 1][j][k] <= n) n     upmin(dp[i][j][k], nxt[dp[i - 1][j][k]][word[0][i]]); n   if (j and dp[i][j - 1][k] <= n) n     upmin(dp[i][j][k], nxt[dp[i][j - 1][k]][word[1][j]]); n   if (k and dp[i][j][k - 1] <= n) n     upmin(dp[i][j][k], nxt[dp[i][j][k - 1]][word[2][k]]); n } n void main() { n   cin >> n >> q >> s; n   for (long long i = 0; i < n; i++) s[i] -=  a ; n   s =     + s; n   for (long long i = 0; i < 26; i++) nxt[n][i] = inf; n   for (long long i = n - 1; i >= 0; i--) { n     for (long long j = 0; j < 26; j++) nxt[i][j] = nxt[i + 1][j]; n     nxt[i][s[i + 1]] = i + 1; n   } n   for (long long i = 0; i < 3; i++) word[i].push_back(0); n   dp[0][0][0] = 0; n   while (q--) { n     string op; n     cin >> op; n     int id; n     cin >> id; n     id--; n     if (op ==  + ) { n       string sss; n       cin >> sss; n       int ch = sss[0] -  a ; n       word[id].push_back(ch); n       if (id == 0) { n         for (long long b = 0; b < ((int)word[1].size()); b++) { n           for (long long c = 0; c < ((int)word[2].size()); c++) { n             solve(((int)word[0].size()) - 1, b, c); n           } n         } n       } n       if (id == 1) { n         for (long long b = 0; b < ((int)word[0].size()); b++) { n           for (long long c = 0; c < ((int)word[2].size()); c++) { n             solve(b, ((int)word[1].size()) - 1, c); n           } n         } n       } n       if (id == 2) { n         for (long long b = 0; b < ((int)word[0].size()); b++) { n           for (long long c = 0; c < ((int)word[1].size()); c++) { n             solve(b, c, ((int)word[2].size()) - 1); n           } n         } n       } n     } else { n       word[id].pop_back(); n     } n     cout << ((dp[((int)word[0].size()) - 1][((int)word[1].size()) - 1] n                 [((int)word[2].size()) - 1] <= n) n                  ?  YES  n                  :  NO ) n          << endl; n   } n } n }  // namespace SOLVE n signed main() { n   int t; n   t = 1; n   while (t--) { n     SOLVE::main(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long int t; n   cin >> t; n   while (t--) { n     long long int n; n     cin >> n; n     long long int a[n]; n     long long int sum = 0; n     for (int i = 0; i < n; i++) { n       cin >> a[i]; n       if (!(i & 1)) sum += a[i]; n     } n     long long int best = 0; n     long long int cur = 0; n     long long int z, z1; n     if (n & 1) n       z = n - 1; n     else n       z = n; n     for (int i = 0; i < z; i += 2) { n       cur = max(0ll, cur + a[i + 1] - a[i]); n       best = max(cur, best); n     } n     cur = 0; n     if (!(n & 1)) n       z1 = n - 1; n     else n       z1 = n; n     for (int i = 1; i < z1; i += 2) { n       cur = max(0ll, cur + a[i] - a[i + 1]); n       best = max(cur, best); n     } n     cout << sum + best << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 35000 + 10; n const int MAXK = 55; n int tree[MAXN << 2], info[MAXN << 2]; n int dp[MAXK][MAXN]; n int nums[MAXN]; n int pre[MAXN], cur[MAXN]; n void build(int node, int start, int end, int i) { n   if (start == end) { n     tree[node] = dp[i][start - 1]; n     info[node] = 0; n     return; n   } n   int mid = (start + end) >> 1; n   build((node << 1), start, mid, i); n   build(((node << 1) | 1), mid + 1, end, i); n   tree[node] = max(tree[(node << 1)], tree[((node << 1) | 1)]); n   info[node] = 0; n } n void update(int node, int start, int end, int i, int j, int val) { n   if (end < i || j < start) return; n   if (i <= start && end <= j) { n     tree[node] += val; n     info[node] += val; n     return; n   } n   int mid = (start + end) >> 1; n   if (info[node] != 0) { n     update((node << 1), start, mid, start, mid, info[node]); n     update(((node << 1) | 1), mid + 1, end, mid + 1, end, info[node]); n     info[node] = 0; n   } n   update((node << 1), start, mid, i, j, val); n   update(((node << 1) | 1), mid + 1, end, i, j, val); n   tree[node] = max(tree[(node << 1)], tree[((node << 1) | 1)]); n } n int query(int node, int start, int end, int i, int j) { n   if (end < i || j < start) return INT_MIN; n   if (i <= start && end <= j) { n     return tree[node]; n   } n   int mid = (start + end) >> 1; n   if (info[node] != 0) { n     update((node << 1), start, mid, start, mid, info[node]); n     update(((node << 1) | 1), mid + 1, end, mid + 1, end, info[node]); n     info[node] = 0; n   } n   return max(query((node << 1), start, mid, i, j), n              query(((node << 1) | 1), mid + 1, end, i, j)); n } n int main() { n   int n, k; n   cin >> n >> k; n   for (int i = 1; i <= n; i++) { n     scanf( %d , &nums[i]); n     pre[i] = cur[nums[i]]; n     cur[nums[i]] = i; n   } n   for (int i = 1; i <= k; i++) { n     build(1, 1, n, i - 1); n     for (int j = i; j <= n; j++) { n       update(1, 1, n, pre[j] + 1, j, 1); n       dp[i][j] = query(1, 1, n, 1, j); n     } n   } n   cout << dp[k][n] << endl; n } n


#include <bits/stdc++.h> n using namespace std; n typedef struct { n   int x; n   int y; n   long long z; n   long long o; n } H; n long long b, g, i, j, k, sz, t, p[200040], w[200040]; n int pv[200040]; n H e[400005]; n int C(H a, H b) { return a.x < b.x; } n void BP(void); n void S(void); n long long dfs(int v); n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   for (cin >> t; t--;) { n     S(); n     BP(); n     k++; n     for (i = 0; i++ < k;) { n       if (w[i] < 2 && !p[e[pv[i]].y]) { n         dfs(pv[i]); n         p[e[pv[i]].y] = 1; n       } n     } n     for (b = g = i = 0; i < sz; i++) { n       if (!e[i].o) { n         if (w[e[i].y] < 2) n           e[i].o = 1; n         else n           e[i].o = k - 1; n       } n       if (e[i].o * 2 < k || e[i].o * 2 == k && e[i].x < e[i].y) { n         g += e[i].o % 2 * e[i].z; n         b += e[i].o * e[i].z; n       } n     } n     cout << g <<     << b <<   n ; n     for (i = 0; i++ < k + 1;) p[i] = pv[i] = w[i] = 0; n     for (i = 0; i < sz + 1; i++) e[i].o = 0; n   } n } n void BP(void) { n   for (int i = 0; ++i < sz;) { n     if (e[i].x > e[i - 1].x) { n       for (int j = e[i - 1].x; j++ < e[i].x;) { n         pv[j] = i; n       } n     } n   } n   for (int j = e[sz - 1].x; j++ < k + 2;) pv[j] = sz; n } n void S(void) { n   cin >> k; n   k = 2 * k - 1; n   for (int i = 0; i < k; i++) { n     cin >> e[i].x >> e[i].y >> e[i].z; n     e[i + k].x = e[i].y; n     e[i + k].y = e[i].x; n     e[i + k].z = e[i].z; n     w[e[i].x]++; n     w[e[i].y]++; n   } n   sz = 2 * k; n   sort(e, e + sz, C); n } n long long dfs(int v) { n   long long s = 0; n   if (e[v].o) return e[v].o; n   for (int i = pv[e[v].y]; i < pv[e[v].y + 1]; i++) { n     if (e[i].y != e[v].x) s += dfs(i); n   } n   return e[v].o = s + 1; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int a, b; n   cin >> a >> b; n   if ((a >= 0 && b < 0) || (a < 0 && b >= 0)) { n     cout <<  -1 ; n     return 0; n   } n   if (a == 0 && b == 0) { n     cout <<  -1 ; n     return 0; n   } n   if (a < 0 && b < 0) { n     cout <<  -1 ; n     return 0; n   } n   if (b == 0 && a > 0) { n     cout <<  -1 ; n     return 0; n   } n   if (a == b) { n     cout << a; n     return 0; n   } n   if (a > 0 && b > 0) { n     if (b > a) { n       cout <<  -1 ; n       return 0; n     } else { n       int k1 = (a - b) / (2 * b); n       double x1 = (double)(a - b) / (2 * k1); n       int k2 = (a + b) / (2 * b); n       double x2 = (double)(a + b) / (2 * k2); n       cout << setprecision(10) << min(x1, x2); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int fr[301][301]; n   int al[301]; n   int i, j, n; n   cin >> n; n   for (i = 1; i <= n; ++i) n     for (j = 1; j <= n; ++j) cin >> fr[i][j]; n   for (i = 1; i <= n; ++i) cin >> al[i]; n   int k, best[301], best_time[301], cur; n   for (j = 1; j <= n; ++j) { n     best[j] = 301; n     best_time[j] = 301; n     for (i = 1; i <= n; ++i) { n       if (j == 1 && i == 1) continue; n       for (k = 1; k <= n; ++k) n         if (al[k] <= i && al[k] != j) { n           cur = al[k]; n           break; n         } n       for (k = 1; k <= n; ++k) { n         if (cur == fr[j][k]) { n           if (k < best[j]) { n             best[j] = k; n             best_time[j] = i; n           } n           break; n         } n       } n     } n   } n   for (i = 1; i <= n; ++i) cout << best_time[i] <<   t ; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   cin >> n; n   int x = 0, xi = INT_MIN, q = 0; n   vector<int> v; n   for (int i = 0; i < n; i++) { n     int y; n     cin >> y; n     q = x; n     v.push_back(y); n     xi = max(x, v[i]); n     if (x != q) x = i; n   } n   long long ans = 0; n   for (int i = 0; i < n; i++) { n     int z = (abs(x - i) + i + x) * 2; n     ans += (z * v[i]); n   } n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n inline int min(int _a, int _b) { return _a < _b ? _a : _b; } n inline int max(int _a, int _b) { return _a > _b ? _a : _b; } n template <class _T> n inline void rd(_T &_a) { n   int _f = 0, _ch = getchar(); n   _a = 0; n   while (_ch <  0  || _ch >  9 ) { n     if (_ch ==  - ) _f = 1; n     _ch = getchar(); n   } n   while (_ch >=  0  && _ch <=  9 ) _a = _a * 10 + _ch -  0 , _ch = getchar(); n   if (_f) _a = -_a; n } n const int inf = 0x3f3f3f3f; n const double eps = 1e-8; n const int mod = 1e9 + 7, N = 15; n int mp[N][N], f[N][N], ct[N], n, m, k, ans; n void dfs(int x, int y) { n   if (y > m) x++, y = 1; n   if (x > n) { n     ans++; n     ans %= mod; n     return; n   } n   int st = f[x - 1][y] | f[x][y - 1], pre, d = -1; n   if (n + m - x - y + 1 > k - __builtin_popcount(st)) return; n   for (int i = 1; i <= k; i++) n     if (!(st >> i - 1 & 1) && (!mp[x][y] || mp[x][y] == i)) { n       f[x][y] = st | (1 << i - 1); n       ct[i]++; n       if (ct[i] == 1) { n         if (~d) n           ans += d; n         else { n           pre = ans; n           dfs(x, y + 1); n           d = ans - pre; n         } n       } else n         dfs(x, y + 1); n       ans %= mod; n       ct[i]--; n     } n } n int main() { n   rd(n); n   rd(m); n   rd(k); n   if (n + m - 1 > k) return puts( 0 ), 0; n   for (int i = 1; i <= n; i++) n     for (int j = 1; j <= m; j++) { n       rd(mp[i][j]); n       if (mp[i][j]) ct[mp[i][j]]++; n     } n   dfs(1, 1); n   printf( %d , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int a, b, n, m; n int A[100010], B[100010], L[100010]; n double dis(int a, int b, int c, int d) { n   return sqrt( n       double((a - c + 0.0) * (a - c + 0.0) + (b - d + 0.0) * (b - d + 0.0))); n } n double getK(int y) { return 1.0 * y * a / (b * 1.0); } n int find(double xx) { n   int x = xx; n   return upper_bound(A, A + n, x) - A; n } n int main() { n   int ll, rr, id; n   double ans = 0, tmp; n   scanf( %d%d%d%d , &n, &m, &a, &b); n   for (int i = 0; i < n; i++) scanf( %d , A + i); n   for (int i = 0; i < m; i++) scanf( %d , B + i); n   for (int i = 0; i < m; i++) scanf( %d , L + i); n   ll = rr = 0; n   ans = dis(0, 0, a, A[0]) + dis(a, A[0], b, B[0]) + L[0]; n   for (int i = 0; i < m; i++) { n     double k = getK(B[i]); n     id = find(k); n     id = max(0, id - 2); n     for (int j = id; j < n && j <= id + 4; j++) { n       tmp = dis(0, 0, a, A[j]) + dis(a, A[j], b, B[i]) + 1.0 * L[i]; n       if (tmp < ans) ans = tmp, ll = j, rr = i; n     } n   } n   printf( %d %d n , ll + 1, rr + 1); n } n


#include <bits/stdc++.h> n using namespace std; n long long int base(long long int n, long long int k) { n   string res =   ; n   long long int temp = 0, i; n   while (n >= k) { n     res += to_string(n % k); n     n /= k; n   } n   res += to_string(n); n   reverse(res.begin(), res.end()); n   for (i = 0; i < res.length(); i++) { n     temp = temp * 10 + (res[i] -  0 ); n   } n   return temp; n } n int main() { n   long long int n, i, j, flag = 0; n   cin >> n; n   flag = 9; n   for (i = 1; i <= n - 1; i++) { n     for (j = 1; j <= n - 1; j++) { n       cout << base(i * j, n) <<    ; n     } n     cout << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXM = 2e5 + 3, MAXN = 2e5 + 3, MAXQ = 2e5 + 3, MAXC = 2e5 + 3, n           ADD = 1 << 18; n int n, m, q; n int edge_from[MAXM << 1], edge_to[MAXM << 1], edge_nex[MAXM << 1]; n int cntm = 1, head[MAXN]; n void link(int from, int to) { n   edge_from[++cntm] = from; n   edge_to[cntm] = to; n   edge_nex[cntm] = head[from]; n   head[from] = cntm; n } n bool isb[MAXM << 1]; n int edge1_to[MAXC << 1], edge1_nex[MAXC << 1]; n int cntm1 = 0, head1[MAXC]; n void link1(int from, int to) { n   edge1_to[++cntm1] = to; n   edge1_nex[cntm1] = head1[from]; n   head1[from] = cntm1; n } n int dfn[MAXN], low[MAXN], cntd = 0; n void tarjan(int now, int fa) { n   low[now] = dfn[now] = ++cntd; n   for (int i = head[now]; i; i = edge_nex[i]) { n     if ((i ^ 1) == fa) continue; n     if (!dfn[edge_to[i]]) { n       tarjan(edge_to[i], i); n       if (low[edge_to[i]] > low[now]) { n         isb[i] = isb[i ^ 1] = 1; n       } else { n         low[now] = low[edge_to[i]]; n       } n     } else { n       low[now] = min(low[now], low[edge_to[i]]); n     } n   } n } n int col[MAXN], cntc = 0, Fa[MAXC][23], Deep[MAXC], cntd1 = 0; n void dfs(int now) { n   col[now] = cntc; n   for (int i = head[now]; i; i = edge_nex[i]) n     if (!col[edge_to[i]] && !isb[i]) dfs(edge_to[i]); n } n void dfs2(int now) { n   for (int i = head1[now]; i; i = edge1_nex[i]) { n     if (!Deep[edge1_to[i]] && edge1_to[i] != Fa[now][0]) { n       Fa[edge1_to[i]][0] = now; n       Deep[edge1_to[i]] = Deep[now] + 1; n       dfs2(edge1_to[i]); n     } n   } n } n long long val1[MAXN]; n long long sum1; n bool Arr[MAXN]; n int ans = 1; n void dfs3(int now) { n   if (!ans) return; n   Arr[now] = 1; n   long long in1 = sum1; n   sum1 += val1[now]; n   for (int i = head1[now]; i; i = edge1_nex[i]) n     if (!Arr[edge1_to[i]] && ans) dfs3(edge1_to[i]); n   in1 = sum1 - in1; n   if ((in1 >> 18) && in1 - ((in1 >> 18) << 18)) ans = 0; n } n inline int lca(register int A, register int B) { n   if (Deep[A] < Deep[B]) swap(A, B); n   for (int i = 17; i >= 0; i--) { n     if (Deep[Fa[A][i]] >= Deep[B]) A = Fa[A][i]; n   } n   if (A == B) return A; n   for (int i = 17; i >= 0; i--) { n     if (Fa[A][i] != Fa[B][i]) { n       A = Fa[A][i]; n       B = Fa[B][i]; n     } n   } n   if (Fa[A][0] == Fa[B][0] && Fa[A][0] != 0) return Fa[A][0]; n   return -1; n } n int main() { n   ios::sync_with_stdio(false); n   cin >> n >> m >> q; n   for (int i = 1, U, V; i <= m; i++) { n     cin >> U >> V; n     link(U, V); n     link(V, U); n   } n   for (int i = 1; i <= n; i++) n     if (!dfn[i]) tarjan(i, 0); n   for (int i = 1; i <= n; i++) n     if (!col[i]) { n       cntc++; n       dfs(i); n     } n   for (int i = 2; i <= cntm; i++) n     if (isb[i]) link1(col[edge_from[i]], col[edge_to[i]]); n   for (int i = 1; i <= cntc; i++) n     if (!Deep[i]) { n       Deep[i] = 1; n       dfs2(i); n     } n   for (int i = 1; i <= 17; i++) n     for (int j = 1; j <= cntc; j++) Fa[j][i] = Fa[Fa[j][i - 1]][i - 1]; n   for (int i = 1, i2, i3, i4; i <= q; i++) { n     cin >> i2 >> i3; n     i2 = col[i2]; n     i3 = col[i3]; n     if (i2 == i3) continue; n     i4 = lca(i2, i3); n     if (i4 == -1) { n       ans = 0; n       break; n     } n     val1[i2] += 1; n     val1[i4] -= 1; n     val1[i3] += ADD; n     val1[i4] -= ADD; n   } n   for (int i = 1; i <= cntc; i++) n     if (!Arr[i]) dfs3(i); n   if (ans) n     cout <<  Yes ; n   else n     cout <<  No ; n } n


#include <bits/stdc++.h> n using namespace std; n int adj[205][205]; n int rgraph[205][205]; n int n, m; n int parent[205]; n bool visited[205]; n int a[105]; n int b[105]; n bool bfs(int s, int t) { n   memset(visited, 0, sizeof(visited)); n   queue<int> q; n   q.push(s); n   visited[s] = true; n   parent[s] = -1; n   while (!q.empty()) { n     int u = q.front(); n     q.pop(); n     for (int i = 0; i <= 2 * n + 1; i++) { n       if (!visited[i] && rgraph[u][i] > 0) { n         q.push(i); n         parent[i] = u; n         visited[i] = true; n       } n     } n   } n   return visited[t] == true; n } n int solve(int s, int t) { n   for (int i = 0; i <= 2 * n + 1; i++) { n     for (int j = 0; j <= 2 * n + 1; j++) { n       rgraph[i][j] = adj[i][j]; n     } n   } n   int max_flow = 0; n   while (bfs(0, 2 * n + 1)) { n     int path = 2000000000; n     for (int i = t; i != s; i = parent[i]) { n       int u = parent[i]; n       path = min(path, rgraph[u][i]); n     } n     for (int i = t; i != s; i = parent[i]) { n       int u = parent[i]; n       rgraph[u][i] -= path; n       rgraph[i][u] += path; n     } n     max_flow += path; n   } n   return max_flow; n } n int main() { n   cin >> n >> m; n   int suma = 0, sumb = 0; n   for (int i = 1; i <= n; i++) { n     cin >> a[i]; n     adj[0][i] = a[i]; n     suma += a[i]; n   } n   for (int i = 1; i <= n; i++) { n     cin >> b[i]; n     adj[n + i][2 * n + 1] = b[i]; n     sumb += b[i]; n     adj[i][n + i] = 1000000; n   } n   for (int i = 0; i < m; i++) { n     int c, d; n     cin >> c >> d; n     adj[c][n + d] = 1000000; n     adj[d][n + c] = 1000000; n   } n   int ans = solve(0, 2 * n + 1); n   if (ans == suma && ans == sumb) { n     printf( YES n ); n     for (int i = 1; i <= n; i++) { n       for (int j = 1; j <= n; j++) { n         if (rgraph[i][n + j] != 0) n           printf( %d  , 1000000 - rgraph[i][n + j]); n         else n           printf( %d  , rgraph[i][n + j]); n       } n       printf(  n ); n     } n   } else { n     printf( NO n ); n   } n } n


#include <bits/stdc++.h> n using namespace std; n int32_t main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   long long t; n   t = 1; n   for (long long test = 1; test <= t; test++) { n     long long n; n     cin >> n; n     vector<long long> a; n     for (long long i = 0; i <= n - 1; i++) { n       long long x; n       cin >> x; n       if (x != 0) a.push_back(x); n     } n     long long bits[61] = {0}; n     n = a.size(); n     for (long long i = 0; i <= n - 1; i++) { n       long long temp = a[i]; n       long long j = 0; n       while (temp > 0) { n         if (temp & 1) bits[j]++; n         temp = temp >> 1; n         j++; n       } n     } n     for (long long i = 0; i <= 60; i++) n       if (bits[i] >= 3) { n         cout << 3; n         exit(0); n       } n     long long cyclec = -1; n     vector<vector<long long>> adj(n); n     for (long long i = 0; i < n; i++) n       for (long long j = i + 1; j < n; j++) n         if ((a[i] & a[j]) != 0) { n           adj[i].push_back(j); n           adj[j].push_back(i); n         } n     for (long long i = 0; i <= n - 1; i++) n       if (adj[i].size() != 0) { n         vector<long long> dis(n, -1); n         vector<long long> par(n, -1); n         queue<long long> q; n         q.push(i); n         dis[i] = 0; n         par[i] = -1; n         while (!q.empty()) { n           auto x = q.front(); n           q.pop(); n           for (auto y : adj[x]) n             if (dis[y] == -1) { n               dis[y] = 1 + dis[x]; n               par[y] = x; n               q.push(y); n             } else if (y != par[x]) { n               if (cyclec == -1) n                 cyclec = (dis[y] + dis[x] + 1); n               else n                 cyclec = min(cyclec, dis[y] + dis[x] + 1); n             } n         } n       } n     cout << cyclec; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long N = 200001; n std::vector<long long> g[N]; n long long s[N]; n long long h[N]; n long long a[N] = {0}; n bool visited[N] = {false}; n void dfs1(long long n, long long parent, long long depth) { n   h[n] = depth; n   for (long long v : g[n]) { n     if (v != parent) { n       dfs1(v, n, depth + 1); n     } n   } n } n void dfs2(long long n, long long parent) { n   if (h[n] % 2 == 0) { n     for (long long v : g[n]) { n       if (v != parent) { n         a[n] = s[v] - s[parent]; n       } n     } n     for (long long v : g[n]) { n       if (v != parent) { n         a[n] = min(s[v] - s[parent], a[n]); n       } n     } n     s[n] = a[n] + s[parent]; n   } else { n     a[n] = s[n] - s[parent]; n   } n   for (long long v : g[n]) { n     if (v != parent) { n       dfs2(v, n); n     } n   } n } n int main() { n   long long n; n   cin >> n; n   for (long long i = 2; i <= n; i++) { n     long long parent; n     cin >> parent; n     g[parent].push_back(i); n     g[i].push_back(parent); n   } n   for (long long i = 1; i <= n; i++) { n     cin >> s[i]; n   } n   dfs1(1, -1, 1); n   a[1] = s[1]; n   dfs2(1, n + 1); n   long long sum = 0; n   for (long long i = 1; i <= n; i++) { n     if (a[i] < 0) { n       cout <<  -1  << endl; n       return 0; n     } n     sum += a[i]; n   } n   cout << sum << endl; n } n


#include <bits/stdc++.h> n template <typename C> n int sz(const C &c) { n   return c.size(); n } n using namespace std; n int main() { n   iostream::sync_with_stdio(false); n   int64_t n; n   cin >> n; n   int k; n   cin >> k; n   int64_t maxV = 0; n   int maxI = 1; n   int64_t maxC = 0; n   for (int i = 1; i <= k; ++i) { n     int64_t a; n     cin >> a; n     if (a * (n / a) > maxV) { n       maxV = a * (n / a); n       maxI = i; n       maxC = n / a; n     } n   } n   cout << maxI <<     << maxC <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n long long a[10], rest[10]; n int n, res = 1e9, pre[10], tmp, size[10], flag = 0; n vector<int> ve[10]; n map<long long, long long> mp; n int count(long long x) { n   if (mp[x]) return mp[x]; n   long long tmp = x; n   long long res = 0; n   for (long long i = 2; i * i <= x; i++) { n     if (x % i == 0) { n       while (x % i == 0) { n         x /= i; n         res++; n       } n     } n   } n   if (x > 1) res++; n   return mp[tmp] = res; n } n bool prime(long long x) { n   if (x == 2 || x == 3) return true; n   for (long long i = 2; i * i <= x; i++) n     if (x % i == 0) return false; n   return true; n } n int isprime[10] = {0}; n void cal(int now) { n   tmp++; n   if (size[now] == 0) { n     if (!isprime[now]) tmp += count(a[now]); n     return; n   } n   if (now) { n     if (rest[now]) tmp += count(rest[now]); n   } n   for (int i = 0; i < size[now]; i++) { n     cal(ve[now][i]); n     if (flag) return; n   } n } n void dfs(int pos) { n   if (pos == n + 1) { n     tmp = 0; n     flag = 0; n     for (int i = 0; i <= n; i++) { n       ve[i].clear(); n       size[i] = 0; n     } n     for (int i = 1; i <= n; i++) { n       size[pre[i]]++; n       ve[pre[i]].push_back(i); n     } n     cal(0); n     if (size[0] == 1) { n       tmp -= 1; n     } n     if (flag) tmp = 1e9; n     if (tmp < res) { n       res = tmp; n     } n     return; n   } n   for (int i = 0; i < pos; i++) { n     if (i && rest[i] % a[pos]) continue; n     rest[i] = rest[i] / a[pos]; n     pre[pos] = i; n     dfs(pos + 1); n     rest[i] *= a[pos]; n   } n } n int main() { n   scanf( %d , &n); n   for (int i = 1; i <= n; i++) scanf( %lld , a + i); n   sort(a + 1, a + n + 1, greater<long long>()); n   for (int i = 1; i <= n; i++) { n     rest[i] = a[i]; n     if (prime(a[i])) isprime[i] = 1; n   } n   dfs(1); n   printf( %d n , res); n } n


#include <bits/stdc++.h> n using namespace std; n int n, m, INF = 2e9; n vector<vector<int>> value; n void shift_column(int j, int k) { n   vector<int> res(n); n   for (int i = 0; i < n; i++) res[(i - k + n) % n] = value[i][j]; n   for (int i = 0; i < n; i++) value[i][j] = res[i]; n } n void run() { n   cin >> n >> m; n   value.resize(n, vector<int>(m)); n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < m; j++) { n       cin >> value[i][j]; n     } n   } n   int ans = 0; n   for (int j = 0; j < m; j++) { n     map<int, int> pos; n     vector<int> cnt(n); n     for (int i = 0; i < n; i++) pos[1 + j + i * m] = i; n     for (int i = 0; i < n; i++) { n       if (pos.find(value[i][j]) == pos.end()) continue; n       int shift = (i - pos[value[i][j]] + n) % n; n       cnt[shift]++; n     } n     auto best_shift = n         distance(cnt.begin(), max_element(cnt.begin(), cnt.end())); n     shift_column(j, best_shift); n     int best_add = INF; n     for (int i = 0; i < n; i++) best_add = min(best_add, n - cnt[i] + i); n     ans += best_add; n   } n   cout << ans; n } n signed main() { n   cout.setf(ios::fixed); n   cout.precision(10); n   cin.tie(0); n   cout.tie(0); n   ios::sync_with_stdio(0); n   run(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m, a[300100], b[300100], pos[300100]; n vector<pair<int, int> > ans; n inline void ws(int xx, int yy) { ans.push_back(make_pair(xx, yy)); } n int main() { n   scanf( %d , &n); n   for (int i = 1; i <= n; ++i) { n     scanf( %d , &a[i]); n     pos[a[i]] = i; n   } n   if (a[1] != 1) { n     int p = pos[1]; n     int x = a[1]; n     if (p - 1 >= n / 2) { n       ws(1, p); n       a[1] = 1; n       pos[1] = 1; n       pos[x] = p; n       a[p] = x; n     } else if (n - p >= n / 2) { n       ws(p, n); n       ws(1, n); n       ws(p, n); n       a[1] = 1; n       pos[1] = 1; n       pos[x] = p; n       a[p] = x; n     } n   } n   if (a[n] != n) { n     int p = pos[n]; n     int x = a[1]; n     int z = a[n]; n     if (p - 1 >= n / 2) { n       ws(1, p); n       ws(1, n); n       ws(1, p); n       a[p] = z; n       pos[z] = p; n       pos[n] = n; n       a[n] = n; n     } else if (n - p >= n / 2) { n       ws(p, n); n       a[p] = z; n       pos[z] = p; n       pos[n] = n; n       a[n] = n; n     } n   } n   for (int i = 2; i < n; ++i) { n     if (pos[i] == i) continue; n     int p = pos[i]; n     int x = a[1]; n     int y = a[i]; n     int z = a[n]; n     if (p - 1 >= n / 2 && i - 1 >= n / 2) { n       ws(1, p); n       ws(1, i); n       ws(1, p); n       swap(a[i], a[pos[i]]); n       pos[y] = p; n       pos[i] = i; n     } else if (p - 1 >= n / 2 && n - i >= n / 2) { n       ws(1, p); n       ws(1, n); n       ws(i, n); n       ws(1, n); n       ws(1, p); n       swap(a[i], a[p]); n       pos[y] = p; n       pos[i] = i; n     } else if (n - p >= n / 2 && i - 1 >= n / 2) { n       ws(p, n); n       ws(1, n); n       ws(1, i); n       ws(1, n); n       ws(p, n); n       swap(a[i], a[p]); n       pos[y] = p; n       pos[i] = i; n     } else if (n - p >= n / 2 && n - i >= n / 2) { n       ws(p, n); n       ws(i, n); n       ws(p, n); n       swap(a[i], a[p]); n       pos[y] = p; n       pos[i] = i; n     } n   } n   printf( %d n , ans.size()); n   for (int i = 0; i < ans.size(); ++i) { n     printf( %d %d n , ans[i].first, ans[i].second); n   } n } n


#include <bits/stdc++.h> n #pragma GCC optimize( Ofast ) n #pragma GCC optimize( no-stack-protector ) n #pragma GCC optimize( unroll-loops ) n #pragma GCC target( sse,sse2,sse3,ssse3,popcnt,abm,mmx,tune=native ) n #pragma GCC optimize( fast-math ) n using namespace std; n inline void ForSpeed() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n } n const int MAXN = int(1e5 + 5); n const int inf = 1e9 + 7; n const int MOD = 998244353; n const long long INF = (1e18 + 7); n long long b, k, a[MAXN]; n int main() { n   ForSpeed(); n   cin >> b >> k; n   for (int i = 0; i < k; i++) { n     cin >> a[i]; n   } n   if (k == 1) { n     if (a[0] % 2 != 0 && b % 2 != 0) { n       cout <<  odd ; n       return 0; n     } n   } n   if (b % 2 == 0 && a[k - 1] % 2 == 0) { n     cout <<  even ; n     return 0; n   } else if (b % 2 == 0 && a[k - 1] % 2 != 0) { n     cout <<  odd ; n     return 0; n   } n   long long ch = 0; n   if (a[0] % 2 == 0) { n     ch = 1; n   } n   for (int i = 1; i < k - 1; i++) { n     if (ch == 0 && a[i] % 2 != 0) { n       ch = 1; n     } else if (ch == 1 && a[i] % 2 == 0) { n       continue; n     } else { n       ch = 0; n     } n   } n   if (ch == 1 && a[k - 1] % 2 == 0) { n     cout <<  even ; n   } else if (ch == 0 && a[k - 1] % 2 != 0) { n     cout <<  even ; n   } else { n     cout <<  odd ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int mod =  1000000007; n const int LIM = 100005; n #define int long long n #define IOS ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0) n #define mem(a,b) memset(a,b,sizeof(a)) n #define all(a) a.begin() , a.end() n #define pii pair<int,int> n #define vi vector<int> n #define endl   n  n #define pb push_back n #define sp <<   << n #define ss second n #define ff first n int power(int x, int n){ n     int res = 1; n     while(n){ n           if(n&1){ n                 res = res * x % mod; n           } n           x = x * x % mod; n           n>>=1; n     } n     return (res%mod); n } n /*bool prime[1000006]; n void sieve(int n) n { n       memset(prime, true, sizeof(prime)); n       int rootn = (int)sqrt(n); n       for (int p = 2; p <= rootn; p++) n             if(prime[p] == true) n                   for(int i = p*p; i <= n; i += p) n                         prime[i] = false; n       prime[1] = 0; n  n }*/ n /* n int fac[300005]; n int ncr(int n,int r) n { n       return fac[n]*((power(fac[n-r], mod-2)*power(fac[r], mod-2))%mod)%mod; n } n */ n bool check(int a[],int n) n { n  for(int i=1;i<n;i++) n  { n   if(a[i]<a[i-1])return false; n  } n  return true; n } n  n int32_t main() n { n   IOS; n   int tt=1,n,x,k,y,z,i,j; n   cin>>tt; n   while(tt--){ n     cin>>n>>x; n   int a[n]; n   for(int i=0;i<n;i++) n   { n    cin>>a[i]; n   } n   int ans=0; n   for(int i=0;i<=n;i++) n   { n    if(check(a,n))break; n    ans++; n    for(int j=0;j<n;j++) n    { n     if(a[j]>x) n     { n      swap(a[j],x); n      break; n     } n    } n   } n   if(check(a,n))cout<<ans<<endl; n   else cout<<-1<<endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n bool debug = 0; n int n, m, k; n int dx[4] = {0, 1, 0, -1}, dy[4] = {1, 0, -1, 0}; n string direc =  RDLU ; n long long ln, lk, lm; n void etp(bool f = 0) { n   puts(f ?  YES  :  NO ); n   exit(0); n } n void addmod(int &x, int y, int mod = 1000000007) { n   assert(y >= 0); n   x += y; n   if (x >= mod) x -= mod; n   assert(x >= 0 && x < mod); n } n void et() { n   puts( -1 ); n   exit(0); n } n long long fastPow(long long x, long long y, int mod = 1000000007) { n   long long ans = 1; n   while (y > 0) { n     if (y & 1) ans = (x * ans) % mod; n     x = x * x % mod; n     y >>= 1; n   } n   return ans; n } n long long gcd1(long long x, long long y) { n   long long z = y; n   while (x % y != 0) { n     z = x % y; n     x = y; n     y = z; n   } n   return z; n } n int h, u[100005], dp[100005]; n vector<int> mp[100005]; n set<pair<int, int> > cs; n void add(int x, int y) { n   if (cs.count({x, y})) return; n   cs.insert({x, y}); n   mp[x].push_back(y); n } n int dfn[100005], sta[100005], low[100005], color, ii, cnt, belong[100005]; n bool ins[100005]; n int sz[100005]; n void tarjan(int x) { n   dfn[x] = low[x] = ++ii; n   sta[++cnt] = x; n   ins[x] = 1; n   for (auto child : mp[x]) { n     if (!dfn[child]) { n       tarjan(child); n       low[x] = min(low[x], low[child]); n     } else if (ins[child]) { n       low[x] = min(low[x], dfn[child]); n     } n   } n   if (dfn[x] == low[x]) { n     ++color; n     int t; n     do { n       t = sta[cnt]; n       cnt--; n       ins[t] = 0; n       belong[t] = color; n       sz[color]++; n     } while (dfn[t] != low[t]); n   } n } n bool del[100005]; n void fmain(int ID) { n   scanf( %d%d%d , &n, &m, &h); n   for (int(i) = 1; (i) <= (int)(n); (i)++) { n     scanf( %d , u + i); n   } n   for (int(i) = 1; (i) <= (int)(m); (i)++) { n     int x, y; n     scanf( %d%d , &x, &y); n     if ((u[x] + 1) % h == u[y]) add(x, y); n     if ((u[y] + 1) % h == u[x]) add(y, x); n   } n   for (int(i) = 1; (i) <= (int)(n); (i)++) n     if (belong[i] == 0) tarjan(i); n   for (auto p : cs) { n     int x = belong[p.first], y = belong[p.second]; n     if (x == y) continue; n     del[x] = 1; n   } n   int ans = -1; n   for (int(i) = 1; (i) <= (int)(color); (i)++) n     if (!del[i]) { n       if (ans == -1 || sz[ans] > sz[i]) ans = i; n     } n   printf( %d n , sz[ans]); n   for (int(i) = 1; (i) <= (int)(n); (i)++) n     if (belong[i] == ans) printf( %d  , i); n } n int main() { n   int t = 1; n   for (int(i) = 1; (i) <= (int)(t); (i)++) { n     fmain(i); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e5 + 10, V = 1e5; n inline int read() { n   register int x = 0, f = 0; n   register char c = getchar(); n   while (c <  0  || c >  9 ) f |= c ==  - , c = getchar(); n   while (c >=  0  && c <=  9 ) x = (x << 3) + (x << 1) + c -  0 , c = getchar(); n   return f ? -x : x; n } n int n, tot, top; n struct point { n   int x, y; n } p[N * 10], s[N * 10]; n point operator-(const point &A, const point &B) { n   return (point){A.x - B.x, A.y - B.y}; n } n inline void add(int x, int y) { n   p[++tot].x = x; n   p[tot].y = y; n } n inline double accross(point A, point B) { n   return (double)A.x * B.y - (double)A.y * B.x; n } n inline double dis(point A, point B) { n   return (double)(A.x - B.x) * (A.x - B.x) + (double)(A.y - B.y) * (A.y - B.y); n } n inline bool cmp1(const point &A, const point &B) { n   return A.x == B.x ? A.y < B.y : A.x < B.x; n } n inline bool cmp2(const point &A, const point &B) { n   return A.x == B.x && A.y == B.y; n } n inline bool cmp3(const point &A, const point &B) { n   double res = accross(A - p[1], B - p[1]); n   if (res > 0) n     return true; n   else if (res == 0 && dis(A, p[1]) < dis(B, p[1])) n     return true; n   return false; n } n void Get_TB() { n   for (int i = 2; i <= tot; ++i) n     if (p[1].y > p[i].y || (p[1].y == p[i].y && p[1].x > p[i].x)) n       swap(p[i], p[1]); n   sort(p + 2, p + tot + 1, cmp3); n   s[1] = p[1]; n   s[2] = p[2]; n   top = 2; n   for (int i = 3; i <= tot; ++i) { n     while (top > 1 && accross(p[i] - s[top - 1], s[top] - s[top - 1]) >= 0) n       top--; n     s[++top] = p[i]; n   } n   return; n } n inline double len(point &A) { n   return sqrt((double)A.x * A.x + (double)A.y * A.y); n } n double calc(point A, point B, point C) { n   point x = A - B, y = C - B, z = C - A; n   double res = len(x) * len(y) * len(z) / (accross(y, x) * 2); n   return res; n } n int main() { n   n = read(); n   for (int i = 1; i <= n; ++i) { n     int x = read(), y = read(), v = read(); n     if (x < v) n       add(0, min(V, y + v - x)), add(0, max(0, y - v + x)); n     else n       add(x - v, y); n     if (y < v) n       add(max(0, x - v + y), 0), add(min(V, x + v - y), 0); n     else n       add(x, y - v); n     if (x + v > V) n       add(V, min(V, y + x + v - V)), add(V, max(0, y - x - v + V)); n     else n       add(x + v, y); n     if (y + v > V) n       add(min(V, x + y + v - V), V), add(max(0, x - y - v + V), V); n     else n       add(x, y + v); n   } n   sort(p + 1, p + 1 + tot, cmp1); n   tot = unique(p + 1, p + 1 + tot, cmp2) - p - 1; n   Get_TB(); n   int id = 0; n   double ans = -1e9, tmp; n   s[0] = s[top]; n   s[top + 1] = s[1]; n   for (int i = 1; i <= top; ++i) { n     point a = s[i - 1], b = s[i], c = s[i + 1]; n     if ((tmp = calc(a, b, c)) > ans) ans = tmp, id = i; n   } n   cout << s[id - 1].x <<     << s[id - 1].y <<   n ; n   cout << s[id].x <<     << s[id].y <<   n ; n   cout << s[id + 1].x <<     << s[id + 1].y <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void init() { freopen( input.txt ,  r , stdin); } n int abs(int x) { n   if (x < 0) { n     return -x; n   } n   return x; n } n int main() { n   int n, m, x1, y1, x2, y2; n   cin >> n >> m >> x1 >> y1 >> x2 >> y2; n   n = abs(x1 - x2) + 1; n   m = abs(y1 - y2) + 1; n   if ((n >= 6) || (m >= 6)) { n     cout <<  Second ; n     return 0; n   } n   if ((n <= 4) && (m <= 4)) { n     cout <<  First ; n     return 0; n   } n   if (((n == 5) || (n == 4)) && ((m == 5) || (m == 4))) { n     cout <<  Second ; n     return 0; n   } n   cout <<  First  << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int gcd(long long a, int b) { n   if (a < b) { n     return gcd(b, a); n   } n   if (b == 0) { n     return a; n   } n   return gcd(b, a % b); n } n class Triplet { n  public: n   long long gcd; n   long long x; n   long long y; n }; n Triplet gcdExtendedEuclid(int a, int b) { n   if (b == 0) { n     Triplet myAns; n     myAns.gcd = a; n     myAns.x = 1; n     myAns.y = 0; n     return myAns; n   } n   Triplet smallAns = gcdExtendedEuclid(b, a % b); n   Triplet myAns; n   myAns.gcd = smallAns.gcd; n   myAns.x = smallAns.y; n   myAns.y = (smallAns.x - ((a / b) * (smallAns.y))); n   return myAns; n } n long long modInverse(int A, int M) { n   Triplet ans = gcdExtendedEuclid(A, M); n   return ans.x; n } n bool check(vector<long long>& v1, long long val, long long h) { n   long long ans = 0; n   for (long long i = 0; i < v1.size(); i++) { n     ans += v1[i] / val; n     if (v1[i] % val != 0) { n       ans += 1; n     } n   } n   if (ans > h) { n     return false; n   } n   return true; n } n void multiply(long long A[2][2], long long M[2][2]) { n   long long firstValue = (A[0][0] * M[0][0] + A[0][1] * M[1][0]) % 1000000007; n   long long secondValue = (A[0][0] * M[0][1] + A[0][1] * M[1][1]) % 1000000007; n   long long thirdValue = (A[1][0] * M[0][0] + A[1][1] * M[1][0]) % 1000000007; n   long long fourthValue = (A[1][0] * M[0][1] + A[1][1] * M[1][1]) % 1000000007; n   A[0][0] = firstValue; n   A[0][1] = secondValue; n   A[1][0] = thirdValue; n   A[1][1] = fourthValue; n   return; n } n void getpow(long long A[2][2], long long n) { n   if (n == 0 || n == 1) { n     return; n   } n   getpow(A, n / 2); n   multiply(A, A); n   if (n % 2 != 0) { n     long long first[2][2] = {{1, 1}, {1, 0}}; n     multiply(A, first); n   } n   return; n } n long long getpow1(long long n, long long m, long long b) { n   if (m == 0) { n     return 1; n   } n   long long ans = getpow1(n, m / 2, b) % b; n   ans = (ans * ans) % b; n   if (m % 2 != 0) { n     ans = ans * n % b; n   } n   return ans % b; n } n long long getfib(long long n) { n   if (n == 0) { n     return 0; n   } n   long long A[2][2] = {{1, 1}, {1, 0}}; n   getpow(A, n); n   long long first[1][2]; n   first[0][0] = 0; n   first[0][1] = 1; n   long long ans = first[0][0] * A[0][0] + first[0][1] * A[1][0]; n   return ans % 1000000007; n } n vector<long long>* sieve() { n   bool isprime[1000001]; n   for (long long i = 0; i < 1000001; i++) { n     isprime[i] = true; n   } n   vector<long long>* primes = new vector<long long>(); n   for (long long i = 2; i < 1000001; i++) { n     if (isprime[i] == true) { n       for (long long j = 2 * i; j < 1000001; j += i) { n         isprime[j] = false; n       } n     } n   } n   primes->push_back(2); n   for (long long i = 3; i < 1000001; i++) { n     if (isprime[i] == true) { n       primes->push_back(i); n     } n   } n   return primes; n } n long long getpow2(long long a, string& b, long long m) { n   long long mod = 1000000007; n   long long num = 1; n   for (long long i = b.size() - 1; i >= 0; i--) { n     if (b[i] == 1) { n       num = (num * a) % m; n     } else if (b[i] == 2) { n       num = ((num * a) % m * a) % m; n     } n     a = ((a * a) % m * a) % m; n   } n   return num % m; n } n long long arr[5000001]; n long long res[1000001]; n long long res1[1000001]; n int main() { n   long long k, n; n   cin >> k >> n; n   long long num = 0; n   long long k1 = k; n   while (k1 < n) { n     k1 *= k; n     num++; n   } n   if (k1 == n) { n     cout <<  YES  <<   n ; n     cout << num; n     return 0; n   } n   cout <<  NO  <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n const long long mod = (long long)1e9 + 7; n const int maxn = (int)1e5 + 1; n long long inf = 1LL << 60; n using namespace std; n inline void read(int &n) { n   int x = 0, f = 1; n   char ch = getchar(); n   while (ch <  0  || ch >  9 ) { n     if (ch ==  - ) f = -1; n     ch = getchar(); n   } n   while (ch >=  0  && ch <=  9 ) { n     x = 10 * x + ch -  0 ; n     ch = getchar(); n   } n   n = x * f; n } n inline void read(long long &n) { n   long long x = 0, f = 1; n   char ch = getchar(); n   while (ch <  0  || ch >  9 ) { n     if (ch ==  - ) f = -1; n     ch = getchar(); n   } n   while (ch >=  0  && ch <=  9 ) { n     x = 10ll * x + ch -  0 ; n     ch = getchar(); n   } n   n = x * f; n } n int a[20]; n int rule[20][20]; n long long dp[1 << 19][19]; n int bitCount(int n) { n   int count = 0; n   while (n) { n     count++; n     n &= (n - 1); n   } n   return count; n } n int main() { n   setlocale(LC_ALL,  Russian ); n   ios_base::sync_with_stdio(0); n   int n, m, k; n   cin >> n >> m >> k; n   long long ans = 0; n   for (int i = 0; i < n; i++) { n     cin >> a[i]; n   } n   for (int i = 0; i < k; i++) { n     int u, v, d; n     cin >> u >> v >> d; n     u--, v--; n     rule[u][v] = d; n   } n   for (int i = 0; i < n; i++) dp[1 << i][i] = a[i]; n   for (int mask = 0; mask < 1 << n; mask++) { n     for (int i = 0; i < n; i++) { n       if (mask & (1 << i)) { n         for (int j = 0; j < n; j++) { n           if (!(mask & (1 << j))) { n             int nmask = mask | (1 << j); n             dp[nmask][j] = max(dp[nmask][j], dp[mask][i] + a[j] + rule[i][j]); n           } n         } n       } n     } n   } n   for (int mask = 0; mask < 1 << n; mask++) { n     for (int i = 0; i < n; i++) { n       if (bitCount(mask) == m) { n         ans = max(ans, dp[mask][i]); n       } n     } n   } n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n #pragma comment(linker,  /STACK:1024000000,1024000000 ) n const double pi = acos(-1.0), eps = 1e-8; n const int maxm = 1100000, inf = 0x3f3f3f3f; n bool f[maxm]; n vector<int> v[maxm]; n int top, st[maxm]; n int a[maxm]; n void dfs(int x, int pre) { n   f[x] = true; n   int i; n   a[x] ^= 1; n   st[top++] = x; n   for (i = 0; i < v[x].size(); i++) { n     if (f[v[x][i]] == false) { n       dfs(v[x][i], x); n       st[top++] = x; n       a[x] ^= 1; n     } n   } n   if (a[x] != 0 && pre != -1) { n     st[top++] = pre; n     a[pre] ^= 1; n     st[top++] = x; n     a[x] ^= 1; n   } n } n int main() { n   int n, m, i, x, y, begin; n   while (scanf( %d%d , &n, &m) == 2) { n     for (i = 1; i <= n; i++) v[i].clear(); n     while (m--) { n       scanf( %d%d , &x, &y); n       v[x].push_back(y); n       v[y].push_back(x); n     } n     for (i = 1; i <= n; i++) { n       scanf( %d , &a[i]); n       f[i] = false; n     } n     top = 0; n     for (i = 1; i <= n; i++) { n       if (a[i] == 1) { n         dfs(i, -1); n         begin = i; n         break; n       } n     } n     if (i == n + 1) { n       printf( 0 n ); n       continue; n     } n     for (i = begin + 1; i <= n; i++) { n       if (a[i] != 0) { n         printf( -1 n ); n         break; n       } n     } n     if (i <= n) continue; n     if (a[begin] == 1) n       begin = 1; n     else n       begin = 0; n     printf( %d n , top - begin); n     for (i = begin; i < top; i++) { n       printf( %d , st[i]); n       if (i == top - 1) n         printf(  n ); n       else n         printf(   ); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAX_N = 11111, MAX_M = 222222; n class Flow_Algorithm { n  public: n   int g[MAX_M], nxt[MAX_M], flow[MAX_M], c[MAX_N], h[MAX_N], nm; n   int d[MAX_N], q[MAX_N]; n   int S, T; n   void addedge(int x, int y, int w) { n     g[nm] = y; n     nxt[nm] = c[x]; n     c[x] = nm; n     flow[nm] = w; n     nm++; n   } n   void add(int x, int y, int w) { n     addedge(x, y, w); n     addedge(y, x, 0); n   } n   bool bfs() { n     memset(d, -1, sizeof d); n     d[q[1] = S] = 0; n     for (int l = 1, r = 1; l <= r; ++l) { n       int i = q[l]; n       for (int k = c[i]; ~k; k = nxt[k]) { n         int j = g[k]; n         if (!~d[j] && flow[k]) d[q[++r] = j] = d[i] + 1; n       } n     } n     return d[T] != -1; n   } n   int dfs(int i, int now) { n     if (!now) return 0; n     if (i == T) return now; n     int w = 0; n     for (int &k = c[i]; ~k; k = nxt[k]) { n       int j = g[k]; n       if (d[j] == d[i] + 1) { n         int res; n         if (flow[k] && ((res = dfs(j, min(now - w, flow[k]))) > 0)) { n           flow[k] -= res; n           flow[k ^ 1] += res; n           w += res; n           if (w == now) return w; n         } n       } n     } n     return w; n   } n   void sets(int _S, int _T) { n     S = _S; n     T = _T; n     nm = 0; n     memset(c, -1, sizeof c); n   } n   int Dinic() { n     memcpy(h, c, sizeof h); n     int ans = 0; n     while (bfs()) { n       ans += dfs(S, 1000000000); n       memcpy(c, h, sizeof c); n     } n     return ans; n   } n } G; n int A[55], B[55], C[55], L[55], R[55]; n int n, m; n int nd[55][222]; n int calc(int i, int x) { return x * x * A[i] + x * B[i] + C[i]; } n int main() { n   scanf( %d%d , &n, &m); n   for (int i = (1); i <= (n); ++i) scanf( %d%d%d , &A[i], &B[i], &C[i]); n   for (int i = (1); i <= (n); ++i) scanf( %d%d , &L[i], &R[i]); n   int tot = 0; n   for (int i = (1); i <= (n); ++i) n     for (int j = (L[i] - 1); j <= (R[i]); ++j) nd[i][j + 100] = ++tot; n   int S = tot + 1, T = tot + 2; n   G.sets(S, T); n   for (int i = (1); i <= (n); ++i) { n     G.add(S, nd[i][L[i] - 1 + 100], 1000000000); n     for (int j = (L[i]); j <= (R[i]); ++j) n       G.add(nd[i][j - 1 + 100], nd[i][j + 100], 20000000 - calc(i, j)); n     G.add(nd[i][R[i] + 100], T, 1000000000); n   } n   for (int i = (1); i <= (m); ++i) { n     int u, v, d; n     scanf( %d%d%d , &u, &v, &d); n     for (int j = (L[u] - 1); j <= (R[u]); ++j) { n       int p = j - d, pt = 0; n       if (p < L[v]) n         pt = nd[v][L[v] - 1 + 100]; n       else n         pt = nd[v][p + 100]; n       G.add(nd[u][j + 100], pt, 1000000000); n     } n   } n   int ans = G.Dinic(); n   ans = n * 20000000 - ans; n   printf( %d n , ans); n } n


#include <bits/stdc++.h> n using namespace std; n constexpr int N = 2e5 + 3; n vector<int> c[N]; n vector<int> e[N]; n int main() { n   ios_base::sync_with_stdio(false); n   int n, m, q; n   cin >> n >> m >> q; n   for (int it = 0; it < q; ++it) { n     int r, C; n     cin >> r >> C; n     c[r].push_back(C); n   } n   for (int i = 1; i <= n; ++i) { n     int esz = c[i].size(); n     for (int j = 1; j < esz; ++j) { n       int prev = c[i][j - 1], now = c[i][j]; n       e[prev].push_back(now); n       e[now].push_back(prev); n     } n   } n   vector<int> vis(m + 1, 0); n   int cc = 0; n   for (int i = 1; i <= m; ++i) { n     if (vis[i] == 0) { n       ++cc; n       function<void(int)> dfs = [&](int i) { n         assert(i >= 1 && i <= m); n         if (vis[i] != 0) { n           assert(vis[i] == cc); n           return; n         } n         vis[i] = cc; n         for (int v : e[i]) { n           dfs(v); n         } n       }; n       dfs(i); n     } n   } n   int cnt_empty_r = 0; n   for (int i = 1; i <= n; ++i) { n     if (c[i].empty()) { n       ++cnt_empty_r; n     } n   } n   cout << cnt_empty_r + cc - 1 <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n static const int INF = 0x3f3f3f3f; n static const long long INFL = 0x3f3f3f3f3f3f3f3fLL; n template <typename T> n T mulmod(T a, T b, T m) { n   long long int q = n       (long long int)(((long double)a * (long double)b) / (long double)m); n   long long int r = a * b - q * m; n   if (r > m) r %= m; n   if (r < 0) r += m; n   return r; n } n template <typename T> n T MOD(T a, T b) { n   return (a < b ? a : a % b); n } n template <typename T> n T expo(T e, T n) { n   T x = 1, p = e; n   while (n) { n     if (n & 1) x = x * p; n     p = p * p; n     n >>= 1; n   } n   return x; n } n template <typename T> n T power(T e, T n, T m) { n   T x = 1, p = e; n   while (n) { n     if (n & 1) x = mulmod(x, p, m); n     p = mulmod(p, p, m); n     n >>= 1; n   } n   return x; n } n template <typename T> n T gcd(T a, T b) { n   if (a % b) return gcd(b, a % b); n   return b; n } n template <typename T> n T lcm(T a, T b) { n   return (a * (b / gcd(a, b))); n } n template <typename T> n T add(T a, T b, T Mod) { n   a += b; n   return (a >= Mod ? a - Mod : a); n } n template <typename T> n T sub(T a, T b, T Mod) { n   a -= b; n   return (a < 0 ? a + Mod : a); n } n const int Mod = 1000000007; n const int N = 100005; n int MX = 1000001; n int a[N]; n struct segmentTree { n   int tree[6 * N]; n   int lazy[6 * N]; n   void modify(int node, int v) { n     tree[node] += v; n     lazy[node] += v; n   } n   void update(int node, int x, int y, int p, int q, int val) { n     if (lazy[node]) { n       modify(node << 1, lazy[node]); n       modify(node << 1 | 1, lazy[node]); n       lazy[node] = 0; n     } n     if (x >= p and y <= q) { n       modify(node, val); n       return; n     } n     int mid = x + y >> 1; n     if (p <= mid) update(node << 1, x, mid, p, q, val); n     if (q > mid) update(node << 1 | 1, mid + 1, y, p, q, val); n     tree[node] = max(tree[node << 1], tree[node << 1 | 1]); n   } n   int query(int node, int x, int y) { n     if (lazy[node]) { n       modify(node << 1, lazy[node]); n       modify(node << 1 | 1, lazy[node]); n       lazy[node] = 0; n     } n     if (tree[node] <= 0) return -1; n     if (x == y) return a[x]; n     int mid = x + y >> 1; n     int ret = query(node << 1 | 1, mid + 1, y); n     if (ret == -1) { n       ret = query(node << 1, x, mid); n     } n     return ret; n   } n } T; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   int m; n   cin >> m; n   for (int i = 0; i <= m - 1; i++) { n     int p, t; n     cin >> p >> t; n     if (t == 0) { n       T.update(1, 1, m, 1, p, -1); n     } else { n       int x; n       cin >> x; n       a[p] = x; n       T.update(1, 1, m, 1, p, 1); n     } n     cout << T.query(1, 1, m) <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long N = 100005; n const long long M = 998244353; n long long pow1(long long a, long long b) { n   int res = 1; n   while (b > 0) { n     if (b & 1) { n       res = (res * a) % M; n     } n     a = (a * a) % M; n     b >>= 1; n   } n   return res % M; n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   long long T; n   T = 1; n   cin >> T; n   while (T--) { n     long long n, m; n     cin >> n >> m; n     long long ans[n][m]; n     for (long long i = 0; i < n; i++) { n       for (long long j = 0; j < m; j++) { n         long long num; n         cin >> num; n         if (num % 2 != (i + j) % 2) n           ans[i][j] = num + 1; n         else n           ans[i][j] = num; n         cout << ans[i][j] <<    ; n       } n       cout <<   n ; n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long int n, k, i, j; n   cin >> n >> k; n   long long int x = 1; n   long long int ans = n - 1; n   for (i = 0; i < k; i++) { n     vector<long long int> v; n     long long int m; n     cin >> m; n     long long int t = -1; n     for (j = 0; j < m; j++) { n       long long int y; n       cin >> y; n       if (y == 1) t = j; n       v.push_back(y); n     } n     if (t != -1) { n       for (j = t + 1; j < m; j++) { n         if (v[j] == v[j - 1] + 1) n           x++; n         else n           break; n       } n     } n     ans += m - 1; n   } n   cout << ans - (2 * (x - 1)); n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int num, hight, temp, width = 0; n   cin >> num >> hight; n   vector<int> v; n   for (int i = 0; i < num; i++) { n     cin >> temp; n     v.push_back(temp); n     if (v[i] > hight) n       width += 2; n     else n       width++; n   } n   cout << width; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const double pi = 2 * acos(0); n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   int q; n   cin >> q; n   while (q--) { n     int n, r; n     cin >> n >> r; n     set<int> s; n     vector<int> v; n     for (long long i = 0; i < n; i++) { n       int a; n       cin >> a; n       s.insert(a); n     } n     for (auto u : s) v.push_back(u); n     int cnt = 0; n     for (int i = v.size() - 1; i >= 0; i--) { n       if (v[i] - r * cnt > 0) cnt++; n     } n     cout << cnt <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 100005; n int n, a[N], b[N], posa[N], posb[N], dif[N], ans, ord[N], t1; n multiset<int> rig, lef; n int abss(const int i) { return i < 0 ? -i : i; } n bool cmp(int i, int j) { return dif[i] < dif[j]; } n int main() { n   scanf( %d , &n); n   for (int i = 1; i <= n; ++i) { n     scanf( %d , &a[i]); n     posa[a[i]] = i; n   } n   for (int i = 1; i <= n; ++i) { n     scanf( %d , &b[i]); n     posb[b[i]] = i; n   } n   ans = n; n   for (int i = 1; i <= n; ++i) { n     ans = min(ans, abss(posa[i] - posb[i])); n     dif[i] = posb[i] - posa[i]; n     if (dif[i] > 0) { n       rig.insert(dif[i]); n     } else { n       lef.insert(-dif[i]); n     } n     ord[i] = i; n   } n   sort(ord + 1, ord + 1 + n, cmp); n   t1 = 1; n   printf( %d n , ans); n   for (int i = 2; i <= n; ++i) { n     lef.erase(lef.find(posa[b[i - 1]] - i + 1)); n     rig.insert(n - posa[b[i - 1]] + i - 1); n     while (rig.size() > 0 && *rig.begin() - i + 1 == 0) { n       rig.erase(rig.find(*rig.begin())); n       lef.insert((int)(-i + 1)); n     } n     ans = min(*lef.begin() + i - 1, n               rig.size() > 0 ? *rig.begin() - i + 1 : n + 1); n     printf( %d n , ans); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int INF = 0x3f3f3f3f; n const int MAX = 100005; n const int MAX1 = 1005; n const int MAX2 = 105; n const int LEN = 105; n const int BASE = 1000000000; n const double EPS = 1e-7; n const int MOD = 1000000007; n long long pow(long long a, long long b) { n   long long ans = 1; n   while (b) { n     if (b & 1) ans = (ans * a) % MOD; n     b /= 2; n     a = (a * a) % MOD; n   } n   return ans; n } n int main(void) { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   ifstream in(  ); n   ofstream out(  ); n   clock_t begin = clock(); n   int q; n   cin >> q; n   while (q--) { n     int n; n     cin >> n; n     int arr[MAX]; n     for (int i = (0); i < (n); ++i) { n       cin >> arr[i]; n     } n     sort(arr, arr + n); n     cout << min(n - 2, arr[n - 2] - 1) <<   n ; n   } n   clock_t end = clock(); n   double esecs = double(end - begin) / CLOCKS_PER_SEC; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T> n inline T S(T a) { n   return a * a; n } n template <class T> n inline string tostring(T a) { n   ostringstream os(  ); n   os << a; n   return os.str(); n } n template <typename T> n inline long long tolong(T a) { n   long long res; n   istringstream os(a); n   os >> res; n   return res; n } n template <typename T> n inline T gcd(T a, T b) { n   if (b == 0) n     return a; n   else n     return gcd(b, a % b); n } n template <typename T> n inline T bigmod(T a, T b, T m) { n   if (b == 0) n     return 1; n   else if (b % 2 == 0) n     return S(bigmod(a, b / 2, m)) % m; n   else n     return (a % m * bigmod(a, b - 1, m)) % m; n } n const int inf = (int)1e9 + 5; n const long long linf = (long long)1e16 + 5; n const long long modd = (long long)1e9 + 7; n const int mod = 10000007; n void pr(int f) { n   if (f == 1) n     cout <<  YES n ; n   else n     cout <<  NO n ; n } n bool isprime(long long n) { n   if (n < 2) return 0; n   if (n == 2) n     return 1; n   else { n     for (int i = 2; i * i <= n; i++) n       if (n % i == 0) return 0; n   } n   return 1; n } n map<int, int> mp; n multiset<string>::iterator it; n deque<char> q; n const int nax = 10e6 + 5; n int t[nax], l[100005], d[100005]; n int main() { n   ios_base::sync_with_stdio(0); n   long long int n, m; n   cin >> n >> m; n   long long int low, high; n   high = ((n - (m - 1)) * (n - m)) / 2; n   long long int quo = n / m; n   long long int rem = n % m; n   low = rem * ((quo + 1) * (quo)) / 2 + (m - rem) * (quo * (quo - 1)) / 2; n   cout << low <<     << high; n } n


#include <bits/stdc++.h> n const int inf = (1ll << 30) - 1; n const int maxn = (int)1e5 + 10; n using namespace std; n int n; n vector<int> g[100100]; n int a[100100]; n int in[100100]; n int out[100100]; n int dp[100100]; n map<int, int> t[400400]; n int timer = 0; n void dfs(int v) { n   in[v] = timer; n   a[timer] = v; n   timer++; n   dp[v] = 1; n   for (int i = 0; i < g[v].size(); i++) { n     int to = g[v][i]; n     dfs(to); n     dp[v] += dp[to]; n   } n   out[v] = timer - 1; n } n void build(int v, int l, int r) { n   if (l == r) { n     t[v][dp[a[l]]]++; n   } else { n     int mid = (l + r) >> 1; n     build(v * 2, l, mid); n     build(v * 2 + 1, mid + 1, r); n     for (int i = l; i <= r; i++) t[v][dp[a[i]]]++; n   } n } n int getu(int v, int l, int r, int tl, int tr, int x) { n   if (tl > tr) return inf; n   if (l == tl && tr == r) { n     map<int, int>::iterator pos = t[v].lower_bound(x); n     if (pos != t[v].end()) return pos->first; n     return inf; n   } n   int mid = (l + r) >> 1; n   return min(getu(v * 2, l, mid, tl, min(tr, mid), x), n              getu(v * 2 + 1, mid + 1, r, max(tl, mid + 1), tr, x)); n } n int getd(int v, int l, int r, int tl, int tr, int x) { n   if (tl > tr) return -inf; n   if (l == tl && tr == r) { n     map<int, int>::iterator pos = t[v].lower_bound(x + 1); n     if (pos != t[v].begin()) return (--pos)->first; n     return -inf; n   } n   int mid = (l + r) >> 1; n   return max(getd(v * 2, l, mid, tl, min(tr, mid), x), n              getd(v * 2 + 1, mid + 1, r, max(tl, mid + 1), tr, x)); n } n void ADD(int v, int l, int r, int pos, int x) { n   t[v][x]++; n   if (l == r) { n     return; n   } n   int mid = (l + r) >> 1; n   if (pos <= mid) { n     ADD(v * 2, l, mid, pos, x); n   } else n     ADD(v * 2 + 1, mid + 1, r, pos, x); n } n void DEL(int v, int l, int r, int pos, int x) { n   t[v][x]--; n   if (t[v][x] == 0) t[v].erase(x); n   if (l == r) { n     return; n   } n   int mid = (l + r) >> 1; n   if (pos <= mid) { n     DEL(v * 2, l, mid, pos, x); n   } else n     DEL(v * 2 + 1, mid + 1, r, pos, x); n } n int ans[100100]; n set<int> PA; n void calc(int v, int szz) { n   int cv = dp[v]; n   DEL(1, 0, n - 1, in[v], cv); n   PA.insert(dp[v]); n   for (int i = 0; i < g[v].size(); i++) { n     int to = g[v][i]; n     cv = szz + dp[v] - dp[to]; n     calc(to, szz + dp[v] - dp[to]); n   } n   PA.erase(dp[v]); n   vector<pair<int, int> > cur; n   if (szz) cur.push_back(make_pair(szz, 0)); n   for (int i = 0; i < g[v].size(); i++) { n     int to = g[v][i]; n     cur.push_back(make_pair(dp[to], to)); n   } n   sort(cur.begin(), cur.end()); n   int d = cur.size(); n   if (cur.size() <= 1 || cur[d - 2].first == cur.back().first) { n     ans[v] = cur.back().first; n     ADD(1, 0, n - 1, in[v], dp[v]); n     return; n   } n   ans[v] = cur[d - 1].first; n   int sz = (cur[0].first + cur[d - 1].first) / 2 - cur[0].first; n   int xx; n   int V = cur[d - 1].second; n   if (V) n     xx = getu(1, 0, n - 1, in[V], out[V], sz); n   else { n     xx = min(getu(1, 0, n - 1, 0, in[v] - 1, sz), n              getu(1, 0, n - 1, out[v] + 1, n - 1, sz)); n     set<int>::iterator it = PA.lower_bound(sz + dp[v]); n     if (it != PA.end()) xx = min(xx, *it - dp[v]); n   } n   ans[v] = min(ans[v], max(cur[d - 2].first, n                            max(cur[0].first + xx, cur[d - 1].first - xx))); n   if (V) n     xx = getd(1, 0, n - 1, in[V], out[V], sz); n   else { n     xx = max(getd(1, 0, n - 1, 0, in[v] - 1, sz), n              getd(1, 0, n - 1, out[v] + 1, n - 1, sz)); n     set<int>::iterator it = PA.upper_bound(sz + dp[v]); n     if (it != PA.begin()) xx = max(xx, *(--it) - dp[v]); n   } n   ans[v] = min(ans[v], max(cur[d - 2].first, n                            max(cur[0].first + xx, cur[d - 1].first - xx))); n   ADD(1, 0, n - 1, in[v], dp[v]); n } n void solve() { n   scanf( %d , &n); n   int root = 0; n   for (int i = 1, x, y; i <= n; i++) { n     scanf( %d%d , &x, &y); n     if (x) n       g[x].push_back(y); n     else n       root = y; n   } n   if (n == 1) { n     printf( 0 n ); n     return; n   } n   if (n == 2) { n     printf( 1 n1 n ); n     return; n   } n   dfs(root); n   build(1, 0, n - 1); n   calc(root, 0); n   for (int i = 1; i <= n; i++) { n     printf( %d n , ans[i]); n   } n } n int main() { n   int t = 1; n   for (int i = 1; i <= t; i++) { n     solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, k; n   cin >> n >> k; n   bool zero = false; n   int *arr = new int[n]; n   for (int i = 0; i < n; i++) { n     cin >> arr[i]; n   } n   int neg = 0; n   for (int i = 0; i < n; i++) { n     if (arr[i] < 0) neg++; n     if (arr[i] == 0) { n       zero = true; n     } n   } n   for (int i = 0; k > 0 && i < neg;) { n     arr[i] = arr[i] * -1; n     k--; n     i++; n   } n   long long int ans = 0; n   for (int i = 0; i < n; i++) { n     ans += arr[i]; n   } n   int min = INT_MAX; n   for (int i = 0; i < n; i++) { n     if (arr[i] < min) { n       min = arr[i]; n     } n   } n   if (k % 2 == 0) { n     cout << ans; n   } else { n     cout << ans - 2 * min; n   } n   return 0; n } n


#include<bits/stdc++.h> n #define fo(i, n) for(int i=0; i<n; i++) n #define boost ios::sync_with_stdio(false); cin.tie(0) n #define ll long long int n using namespace std; n  n int main() n {   boost; n     int t; cin>>t; n     while(t--){ n         int n, max_freq = 0, i; cin>>n; n         map<int, int> m; n         for(i=0; i<n; i++){ n             int k; cin>>k; n             max_freq = max(max_freq, ++m[k]); n         } n         cout<<max_freq<<  n ; n     } n }


#include <bits/stdc++.h> n using namespace std; n const int N = 500001; n int arr[N]; n vector<pair<int, int> > v[N]; n int ans[N], n, m; n int bit[N]; n map<int, int> last; n void update(int idx, int val) { n   for (; idx <= n; idx += idx & -idx) { n     bit[idx] = min(bit[idx], val); n   } n } n int query(int idx) { n   int res = n + 1; n   for (; idx; idx -= idx & -idx) { n     res = min(res, bit[idx]); n   } n   return res; n } n int main() { n   cin >> n >> m; n   for (int i = 1; i <= n; i++) { n     scanf( %d , &arr[i]); n     bit[i] = n + 1; n   } n   for (int i = 1; i <= m; i++) { n     int x, y; n     scanf( %d %d , &x, &y); n     v[y].push_back({x, i}); n   } n   for (int i = 1; i <= n; i++) { n     if (last.find(arr[i]) != last.end()) n       update(n - last[arr[i]] + 1, i - last[arr[i]]); n     last[arr[i]] = i; n     int x = v[i].size(); n     for (int j = 0; j < x; ++j) { n       ans[v[i][j].second] = query(n - v[i][j].first + 1); n     } n   } n   for (int i = 1; i <= m; i++) { n     printf( %d n , (ans[i] == n + 1) ? -1 : ans[i]); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   cin.tie(NULL); n   cout.tie(NULL); n   ios_base::sync_with_stdio(false); n   long long int n, i, t, k, c, s = 0, x, sum = 0, st1 = 0, st2 = 0, curr = 0; n   cin >> n; n   vector<pair<long long int, long long int> > v; n   vector<long long int> p; n   for (i = 0; i < n; i++) { n     cin >> k >> c; n     v.push_back(make_pair(c, k)); n   } n   cin >> t; n   for (i = 0; i < t; i++) { n     cin >> x; n     s = x; n     p.push_back(s); n   } n   p.push_back(100000000000000); n   sort(v.begin(), v.end()); n   while (st1 < n) { n     if (curr + v[st1].second < p[st2]) { n       sum += (st2 + 1) * (v[st1].first) * (v[st1].second); n       curr += v[st1].second; n       st1++; n     } else { n       long long int rem = p[st2] - curr; n       curr += rem; n       sum += (rem) * (st2 + 1) * (v[st1].first); n       v[st1].second -= rem; n       st2++; n       if (!v[st1].second) { n         st1++; n       } n     } n   } n   cout << sum; n } n


#include <bits/stdc++.h> n using namespace std; n namespace io { n const int SI = 1 << 21 | 1; n char IB[SI], *IS, *IT, OB[SI], *OS = OB, *OT = OS + SI - 1, c, ch[100]; n int f, t; n inline void flush() { fwrite(OB, 1, OS - OB, stdout), OS = OB; } n inline void pc(char x) { n   *OS++ = x; n   if (OS == OT) flush(); n } n template <class I> n inline void rd(I &x) { n   for (f = 1, c = (IS == IT ? (IT = (IS = IB) + fread(IB, 1, SI, stdin), n                               IS == IT ? EOF : *IS++) n                             : *IS++); n        c <  0  || c >  9 ; n        c = (IS == IT ? (IT = (IS = IB) + fread(IB, 1, SI, stdin), n                        IS == IT ? EOF : *IS++) n                      : *IS++)) n     if (c ==  - ) f = -1; n   for (x = 0; c >=  0  && c <=  9 ; x = (x << 3) + (x << 1) + (c & 15), n       c = (IS == IT ? (IT = (IS = IB) + fread(IB, 1, SI, stdin), n                       IS == IT ? EOF : *IS++) n                     : *IS++)) n     ; n   x *= f; n } n inline void rds(char *s, int &x) { n   for (c = (IS == IT ? (IT = (IS = IB) + fread(IB, 1, SI, stdin), n                        IS == IT ? EOF : *IS++) n                      : *IS++); n        c < 33 || c > 126; n        c = (IS == IT ? (IT = (IS = IB) + fread(IB, 1, SI, stdin), n                        IS == IT ? EOF : *IS++) n                      : *IS++)) n     ; n   for (x = 0; c >= 33 && c <= 126; n        s[++x] = c, c = (IS == IT ? (IT = (IS = IB) + fread(IB, 1, SI, stdin), n                                    IS == IT ? EOF : *IS++) n                                  : *IS++)) n     ; n   s[x + 1] =   0 ; n } n template <class I> n inline void print(I x, char k =   n ) { n   if (!x) pc( 0 ); n   if (x < 0) pc( - ), x = -x; n   while (x) ch[++t] = x % 10 +  0 , x /= 10; n   while (t) pc(ch[t--]); n   pc(k); n } n inline void prints(string s) { n   int x = s.length(); n   while (t < x) pc(s[t++]); n   pc(  n ), t = 0; n } n struct Flush { n   ~Flush() { flush(); } n } flusher; n }  // namespace io n using io::print; n using io::prints; n using io::rd; n using io::rds; n const int N = 3e5 + 7; n int n, a[N], sx[N], tx, sn[N], tn; n map<int, int> p; n struct T { n   int l, r, x, c, z; n } t[N << 2]; n long long ans; n void build(int p, int l, int r) { n   t[p].l = l, t[p].r = r, t[p].c = r - l + 1; n   if (l == r) return; n   build((p << 1), l, ((t[p].l + t[p].r) >> 1)), n       build(((p << 1) | 1), ((t[p].l + t[p].r) >> 1) + 1, r); n } n inline void add(int p, int x) { t[p].x += x, t[p].z += x; } n void upd(int p, int l, int r, int x) { n   if (t[p].l >= l && t[p].r <= r) return add(p, x); n   if (t[p].z) add((p << 1), t[p].z), add(((p << 1) | 1), t[p].z), t[p].z = 0; n   if (l <= ((t[p].l + t[p].r) >> 1)) upd((p << 1), l, r, x); n   if (r > ((t[p].l + t[p].r) >> 1)) upd(((p << 1) | 1), l, r, x); n   t[p].x = min(t[(p << 1)].x, t[((p << 1) | 1)].x); n   t[p].c = (t[(p << 1)].x == t[p].x ? t[(p << 1)].c : 0) + n            (t[((p << 1) | 1)].x == t[p].x ? t[((p << 1) | 1)].c : 0); n } n int main() { n   rd(n); n   for (int i = 1, x, y; i <= n; i++) rd(x), rd(y), a[x] = y; n   build(1, 1, n); n   for (int i = 1; i <= n; i++) { n     while (tx && a[sx[tx]] < a[i]) n       upd(1, sx[tx - 1] + 1, sx[tx], -a[sx[tx]]), --tx; n     while (tn && a[sn[tn]] > a[i]) n       upd(1, sn[tn - 1] + 1, sn[tn], a[sn[tn]]), --tn; n     upd(1, p[a[i]] + 1, i, -1), p[a[i]] = sx[++tx] = sn[++tn] = i; n     upd(1, sx[tx - 1] + 1, i, a[i]), upd(1, sn[tn - 1] + 1, i, -a[i]); n     ans += t[1].c; n   } n   print(ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, k, m; n int nt, kt, mt; n int su[100004]; n long long sum = 0; n double res; n int main() { n   scanf( %d%d%d , &n, &k, &m); n   nt = n; n   kt = k; n   mt = m; n   for (int i = 0; i < n; i++) { n     scanf( %d , su + i); n     sum += su[i]; n   } n   res = 1.0 * sum / n; n   sort(su, su + n); n   long long ts = 0; n   for (int i = 1; i <= n; i++) { n     ts += su[n - i]; n     long long rm = m - n + i; n     long long te = i; n     long long tk = k; n     if (rm < 0) { n       continue; n     } n     res = max(res, (ts + min(rm, te * tk)) * 1.0 / te); n   } n   printf( %lf n , res); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 2e5 + 100; n const int MOD = 1e9 + 7; n const int oo = 1e9 + 100; n const long double EPS = 1e-9; n const string DIRS =  UDLR ; n int n, x, y; n char str[MAXN]; n int acc[256][MAXN]; n int get(char ch, int fr, int to) { n   if (to < fr) return 0; n   return acc[ch][to] - acc[ch][fr - 1]; n } n int main() { n   while (scanf( %d %s %d %d , &n, str + 1, &x, &y) == 4) { n     for (auto& e : DIRS) { n       acc[e][0] = 0; n       for (int i = 1; i <= n; ++i) { n         acc[e][i] = acc[e][i - 1] + (str[i] == e); n       } n     } n     int init_x = acc[ R ][n] - acc[ L ][n]; n     int init_y = acc[ U ][n] - acc[ D ][n]; n     if (init_x == x && init_y == y) { n       puts( 0 ); n       continue; n     } n     if (abs(x) + abs(y) > n || (n - abs(x) - abs(y)) % 2 != 0) { n       puts( -1 ); n       continue; n     } n     int lo = 1, hi = n; n     vector<int> aux(256); n     while (lo <= hi) { n       int mi = (lo + hi) >> 1; n       bool ok = false; n       for (int i = 1; i + mi - 1 <= n; ++i) { n         for (auto& e : DIRS) aux[e] = get(e, 1, i - 1) + get(e, i + mi, n); n         int xx = aux[ R ] - aux[ L ]; n         int yy = aux[ U ] - aux[ D ]; n         int rem = mi - (abs(x - xx) + abs(y - yy)); n         if (rem >= 0) ok = true; n       } n       if (ok) n         hi = mi - 1; n       else n         lo = mi + 1; n     } n     printf( %d n , lo); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int a[200010], b[200010]; n bool used[200010]; n int main() { n   int n, k = 0, i, x; n   scanf( %d , &n); n   for (i = 1; i <= n; i++) scanf( %d , &a[i]); n   for (i = 1; i <= n; i++) scanf( %d , &b[i]); n   int ans = 1 - accumulate(b + 1, b + n + 1, 0) % 2; n   for (i = 1; i <= n; i++) n     if (!used[i]) { n       used[i] = true; n       x = a[i]; n       while (!used[x]) { n         used[x] = true; n         x = a[x]; n       } n       k++; n     } n   if (k > 1) ans += k; n   printf( %d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   long long n, m, i, cnt = 0, sum = 0, ans = 0, x, j; n   string s; n   vector<string> v; n   cin >> n >> m; n   int a[m]; n   for (i = 0; i < n; i++) { n     cin >> s; n     v.push_back(s); n   } n   for (i = 0; i < m; i++) cin >> a[i]; n   for (i = 0; i < m; i++) { n     int arr[5] = {0}; n     for (j = 0; j < n; j++) { n       x = v[j][i] -  A ; n       arr[x]++; n     } n     sort(arr, arr + 5); n     ans += arr[4] * a[i]; n   } n   cout << ans; n } n


#include <bits/stdc++.h> n using namespace std; n void chekarre(int* arr, int n) { n   cout <<  [ ; n   for (int i = 0; i < n; i++) cout << arr[i] <<    ; n   cout <<  ]  << endl; n } n bool comp(int i, int j) { return (i > j); } n int a[2000007]; n int main() { n   int n; n   long long sum = 0; n   cin >> n; n   for (int i = 0; i < n; i++) { n     cin >> a[i]; n   } n   sort(a, a + n); n   reverse(a, a + n); n   while (n) { n     for (int j = 0; j < n; j++) { n       sum += a[j]; n     } n     n = n / 4; n   } n   cout << sum << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, a, b; n int ans, nn; n char x[100010]; n int change(int t) { n   int rtn = 0, bit = 1; n   while (t) { n     rtn += (t % 10) * bit; n     bit *= ans; n     t /= 10; n   } n   return rtn; n } n int main() { n   scanf( %d%d , &a, &b); n   int aa = a, bb = b; n   while (aa) { n     ans = max(ans, aa % 10); n     aa /= 10; n   } n   while (bb) { n     ans = max(ans, bb % 10); n     bb /= 10; n   } n   ans += 1; n   a = change(a); n   b = change(b); n   a += b; n   while (a) { n     x[nn++] = a % ans +  0 ; n     a /= ans; n   } n   printf( %d , nn); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long OO = 1e8; n const double EPS = (1e-7); n int dcmp(double x, double y) { return fabs(x - y) <= EPS ? 0 : x < y ? -1 : 1; } n const int N = 100; n bool adjMatrixBool[N][N]; n int adjMatrix[N][N]; n vector<int> adjMatrixAll[N][N]; n map<pair<int, int>, int> adjMatrixMap; n vector<vector<int> > adjList1; n vector<vector<pair<int, int> > > adjList2; n vector<bool> visited; n bool valid(int i, int j) { return 1; } n char arr[303][303], x, b; n bool prime(long long n) { n   long long i; n   for (i = 2; i <= sqrt(n); i++) { n     if (n % i == 0) return false; n   } n   return true; n } n long long gcd(long long a, long long b) { n   if (b == 0) return a; n   return gcd(b, a % b); n } n long long lcm(long long a, long long b) { return (a / gcd(a, b) * b); } n int main() { n   ios_base::sync_with_stdio(false); n   long long n; n   cin >> n; n   string s; n   cin >> s; n   long long arr[n]; n   for (int i = 0; i < n; i++) { n     cin >> arr[i]; n   } n   long long ans = INT_MAX; n   for (int i = 0; i < n - 1; i++) { n     if (s[i] ==  R  and s[i + 1] ==  L ) n       ans = min(ans, abs((arr[i + 1] - arr[i]) / 2)); n   } n   if (ans == INT_MAX) n     cout <<  -1  << endl; n   else n     cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const double PI = acos(-1.0); n const long long INF = 1000 * 1000 * 1000 + 7; n const long long LINF = INF * (long long)INF; n const int MOD = 1000 * 1000 * 1000 + 7; n const int MAX = 10000 + 47; n long long F[MAX]; n vector<int> R; n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   int s, k; n   cin >> s >> k; n   int p = 0; n   F[0] = 1; n   for (int i = (1); i < (MAX); i++) { n     for (int j = (max(0, i - k)); j < (i); j++) { n       F[i] += F[j]; n     } n     if (F[i] > s) { n       p = i; n       break; n     } n   } n   for (int i = (p)-1; i >= (0); i--) { n     if (s >= F[i]) { n       s -= F[i]; n       R.push_back(F[i]); n     } n   } n   if ((int)R.size() == 1) R.push_back(0); n   cout << (int)R.size() << endl; n   for (int i = (0); i < ((int)R.size()); i++) { n     cout << R[i] <<    ; n   } n   cout << endl; n } n


#include <bits/stdc++.h> n using namespace std; n const double eps = 1e-9; n const double PI = acos(-1.0); n const long long int mod = 1e9 + 7; n const int MAXN = 1e6 + 5; n void cp() { n   long long int n, i, k; n   cin >> n >> k; n   long long int a[n], sum = 0; n   for (int i = 0; i < n; i++) cin >> a[i]; n   sort(a, a + n); n   for (i = 1; i < n; i++) { n     if (a[i] < k) { n       sum += (k - a[i]) / a[0]; n     } n   } n   cout << sum; n   cout <<   n ; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   ; n   int t; n   t = 1; n   cin >> t; n   while (t--) { n     cp(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int INF = INT_MAX; n const long long INFL = LLONG_MAX; n int dx[] = {1, -1, 0, 0}; n int dy[] = {0, 0, 1, -1}; n int main() { n   ios_base::sync_with_stdio(0); n   cout.precision(15); n   cout << fixed; n   cout.tie(0); n   cin.tie(0); n   int n, k; n   cin >> n >> k; n   int p = 2 * n + 1; n   int a[p + 2]; n   for (int i = 1; i <= p; i++) cin >> a[i]; n   a[0] = -INF; n   a[p + 1] = +INF; n   for (int i = 1; i <= p && k > 0; i++) n     if (i % 2 == 0 && a[i] > a[i - 1] && a[i] > a[i + 1] && n         a[i] - a[i - 1] > 1 && a[i] - a[i + 1] > 1) { n       k--; n       a[i]--; n     } n   for (int i = 1; i <= p; i++) cout << a[i] <<    ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long a[300005]; n long long c[300005] = {0}; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   ; n   int n; n   cin >> n; n   int i; n   for (i = 1; i <= n - 1; i++) cin >> a[i]; n   long long m = 0; n   long long r = 0; n   for (i = 1; i <= n - 1; i++) { n     r += a[i]; n     m = min(m, r); n   } n   m = abs(m) + 1; n   long long y = m; n   if (m > n || c[m]) { n     cout << -1; n     return 0; n   } n   c[m] = 1; n   for (i = 1; i <= n - 1; i++) { n     m += a[i]; n     if (m > n || c[m]) { n       cout << -1; n       return 0; n     } n     c[m] = 1; n   } n   cout << y <<    ; n   for (i = 1; i <= n - 1; i++) { n     y += a[i]; n     cout << y <<    ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int myrand(int m) { return (rand() * (1 << 15) + rand()) % m; } n int main() { n   srand(time(0)); n   int n, ind, x; n   cin >> n >> ind >> x; n   --ind; n   vector<int> left(n); n   vector<bool> vis(n, 0); n   for (int i = 0; i < n; ++i) left[i] = i; n   vis[ind] = 1; n   random_shuffle(left.begin(), left.end()); n   int best = -1; n   for (int i = 0; i < min(1000, n); ++i) { n     int v, ii = left[i]; n     cout <<  ?   << ii + 1 <<   n ; n     fflush(stdout); n     cin >> v >> ii; n     if (v < x && v > best) { n       ind = left[i]; n       best = max(best, v); n     } n   } n   if (ind < 0) { n     cout <<  ! -1 n ; n     fflush(stdout); n     return 0; n   } n   int itt, LIM = 999; n   for (itt = 0; itt < LIM; ++itt) { n     cout <<  ?   << ind + 1 <<   n ; n     fflush(stdout); n     int v; n     cin >> v >> ind; n     --ind; n     if (v >= x) { n       best = v; n       break; n     } n     if (ind < 0) break; n   } n   if (itt == LIM) { n     int j = 1; n     while (1) { n       j = j + 1; n     } n   } n   cout <<  !   << ((best < x) ? -1 : best) <<   n ; n   fflush(stdout); n } n


#include <bits/stdc++.h> n using namespace std; n bool done1[33][2][2], done2[33][2][2], done3[33][2][2]; n long long memo1[33][2][2], memo2[33][2][2], memo3[33][2][2]; n long long lim; n int n; n long long dp1(int, int, int); n long long dp2(int, int, int); n long long dp3(int, int, int); n long long dp1(int i, int s1, int s2) { n   if (done1[i][s1][s2]) return memo1[i][s1][s2]; n   done1[i][s1][s2] = true; n   long long &ret = memo1[i][s1][s2] = 0; n   int hd = (((lim) >> (n - 1 - i)) & 1), rr = (((lim) >> (i)) & 1); n   if (i * 2 + 1 == n) { n     if (!s1) n       ret = 1; n     else if (s2) n       ret = hd != 0; n     else { n       ret = 1; n     } n   } else if (i * 2 == n) { n     if (s1 && s2) n       ret = 0; n     else n       ret++; n   } else { n     ret += dp2(i + 1, s1 && hd == 0, 0 > rr || (0 == rr && s2)); n     ret += dp3(i + 1, s1 && hd == 0, 1 > rr || (1 == rr && s2)); n   } n   return ret; n } n long long dp2(int i, int s1, int s2) { n   if (done2[i][s1][s2]) return memo2[i][s1][s2]; n   done2[i][s1][s2] = true; n   long long &ret = memo2[i][s1][s2] = 0; n   int hd = (((lim) >> (n - 1 - i)) & 1), rr = (((lim) >> (i)) & 1); n   if (i * 2 + 1 == n) { n     if (!s1) n       ret = 2; n     else if (s2) n       ret = hd != 0; n     else { n       ret = hd + 1; n     } n   } else if (i * 2 == n) { n     if (s1 && s2) n       ret = 0; n     else n       ret++; n   } else { n     for (int x = 0; x <= (s1 ? hd : 1); x++) n       ret += dp2(i + 1, s1 && hd == x, x > rr || (x == rr && s2)); n     int ns1 = s1 && hd == 0, ns2 = 1 > rr || (1 == rr && s2); n     if (!ns1) n       ret += 1LL << (n - 2 * (i + 1)); n     else n       ret += ((lim >> (i + 1)) & ((1LL << (n - 2 * (i + 1))) - 1)) + 1; n     if (ns1 && ns2) ret--; n   } n   return ret; n } n long long dp3(int i, int s1, int s2) { n   if (done3[i][s1][s2]) return memo3[i][s1][s2]; n   done3[i][s1][s2] = true; n   long long &ret = memo3[i][s1][s2] = 0; n   int hd = (((lim) >> (n - 1 - i)) & 1), rr = (((lim) >> (i)) & 1); n   if (i * 2 + 1 == n) { n     if (!s1) n       ret = 1; n     else if (s2) n       ret = hd != 0; n     else { n       ret = 1; n     } n   } else if (i * 2 == n) { n     if (s1 && s2) n       ret = 0; n     else n       ret++; n   } else { n     ret += dp3(i + 1, s1 && hd == 0, 1 > rr || (1 == rr && s2)); n     if (!s1 || hd == 1) n       ret += dp3(i + 1, s1 && hd == 1, 0 > rr || (0 == rr && s2)); n     int ns1 = s1 && hd == 0, ns2 = 0 > rr || (0 == rr && s2); n     if (!ns1) n       ret += 1LL << (n - 2 * (i + 1)); n     else n       ret += ((lim >> (i + 1)) & ((1LL << (n - 2 * (i + 1))) - 1)) + 1; n     if (ns1 && ns2) ret--; n   } n   return ret; n } n void output(long long x, int n) { n   for (int i = 0; i < n; i++) cout << (((x) >> (n - i - 1)) & 1); n   cout <<   n ; n } n long long work(long long LIM) { n   lim = LIM; n   memset(done1, 0, sizeof(done1)); n   memset(done2, 0, sizeof(done2)); n   memset(done3, 0, sizeof(done3)); n   return dp1(0, 1, 0); n } n int main() { n   long long rank; n   cin >> n >> rank; n   rank++; n   long long lb = 0, rb = (1LL << n) - 1; n   for (long long mid = (lb + rb) / 2; lb < rb - 1; mid = (lb + rb) / 2) { n     if (work(mid) >= rank) n       rb = mid; n     else n       lb = mid; n   } n   if (work(rb) == rank) n     output(rb, n); n   else n     printf( -1 n ); n } n


#include <bits/stdc++.h> n using namespace std; n int32_t main() { n   long long n; n   cin >> n; n   map<string, long long> count; n   for (long long i = 0; i < n; i++) { n     string s; n     cin >> s; n     count[s]++; n   } n   string s1 =  a , s2 =  a ; n   for (map<string, long long>::iterator it = count.begin(); it != count.end(); n        ++it) { n     if (s1 ==  a ) n       s1 = (*it).first; n     else n       s2 = (*it).first; n   } n   if (count[s1] > count[s2]) n     cout << s1 <<   n ; n   else n     cout << s2 <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n int to(int x, int y) { return (x * 60) + (y); } n long long int gcd(long long int a, long long int b) { n   return b == 0 ? a : gcd(b, a % b); n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   int n; n   cin >> n; n   int arr[n + 1]; n   for (int i = 1; i <= n; i++) cin >> arr[i]; n   int mx = 0, ans = 0; n   for (int i = 1; i <= n; i++) { n     if (arr[i] == i and mx <= i) { n       ans++; n       continue; n     } n     if (arr[i] == i) { n       continue; n     } n     mx = max(arr[i], mx); n   } n   cout << ans; n   cout << endl; n   return 0; n } n


#include <bits/stdc++.h> n int N; n char s[507][507]; n int rX[] = {-1, 0, 1, 0}; n int rY[] = {0, -1, 0, 1}; n int spoj[507][507]; n int rozm[1000007]; n int akt[1000007]; n int terRozm; n int wyn; n void dfs(int y, int x, int num) { n   spoj[y][x] = num; n   terRozm++; n   for (int i = 0; i < 4; i++) { n     int nx = x + rX[i], ny = y + rY[i]; n     if (nx >= 1 && nx <= N && ny >= 1 && ny <= N) { n       if (s[ny][nx] ==  .  && spoj[ny][nx] == 0) dfs(ny, nx, num); n     } n   } n } n void dolicz(int y1, int x1, int y2, int x2, bool kk = false) { n   for (int y = y1; y <= y2; y++) { n     if (y < 1 || y > N) continue; n     for (int x = x1; x <= x2; x++) { n       if (x < 1 || x > N) continue; n       if (s[y][x] ==  . ) { n         int sp = spoj[y][x]; n         akt[sp]++; n         if (akt[sp] == 1) wyn += rozm[sp]; n       } else if (kk) { n         wyn++; n       } n     } n   } n } n void odejmij(int y1, int x1, int y2, int x2, bool kk = false) { n   for (int y = y1; y <= y2; y++) { n     if (y < 1 || y > N) continue; n     for (int x = x1; x <= x2; x++) { n       if (x < 1 || x > N) continue; n       if (s[y][x] ==  . ) { n         int sp = spoj[y][x]; n         akt[sp]--; n         if (akt[sp] == 0) wyn -= rozm[sp]; n       } else if (kk) { n         wyn--; n       } n     } n   } n } n int main() { n   int K; n   scanf( %d%d , &N, &K); n   for (int i = 1; i <= N; i++) scanf( %s , s[i] + 1); n   int ileSpoj = 0; n   for (int i = 1; i <= N; i++) n     for (int w = 1; w <= N; w++) n       if (s[i][w] ==  .  && spoj[i][w] == 0) { n         terRozm = 0; n         dfs(i, w, ++ileSpoj); n         rozm[ileSpoj] = terRozm; n       } n   int maxi = 0; n   for (int i = 1; i <= N - K + 1; i++) { n     dolicz(i, 1, i + K - 1, K, true); n     dolicz(i - 1, 1, i - 1, K); n     dolicz(i + K, 1, i + K, K); n     dolicz(i, 0, i + K - 1, 0); n     dolicz(i, K + 1, i + K - 1, K + 1); n     maxi = std::max(wyn, maxi); n     for (int x = 2; x <= N + 2; x++) { n       odejmij(i, x - 2, i + K - 1, x - 2); n       odejmij(i, x - 1, i + K - 1, x - 1, true); n       dolicz(i, x - 1, i + K - 1, x - 1); n       odejmij(i, x + K - 1, i + K - 1, x + K - 1); n       dolicz(i, x + K - 1, i + K - 1, x + K - 1, true); n       dolicz(i, x + K, i + K - 1, x + K); n       odejmij(i - 1, x - 1, i - 1, x - 1); n       dolicz(i - 1, x + K - 1, i - 1, x + K - 1); n       odejmij(i + K, x - 1, i + K, x - 1); n       dolicz(i + K, x + K - 1, i + K, x + K - 1); n       maxi = std::max(wyn, maxi); n     } n   } n   printf( %d , maxi); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m, x, c, a[200001], tin[200001], tout[200001], sz[200001], seg[524288]; n string s; n bool lazy[524288]; n vector<int> G[200001]; n void dfs(int i) { n   tin[i] = ++c; n   ++sz[i]; n   for (int j : G[i]) { n     dfs(j); n     sz[i] += sz[j]; n   } n   tout[i] = c; n } n void upd(int l, int r, int s = 1, int e = n, int i = 1) { n   if (lazy[i]) { n     seg[i] = (e - s + 1) - seg[i]; n     if (s != e) { n       lazy[2 * i] ^= 1; n       lazy[2 * i + 1] ^= 1; n     } n     lazy[i] = 0; n   } n   if (s > r || e < l) return; n   if (s >= l && e <= r) { n     seg[i] = (e - s + 1) - seg[i]; n     if (s != e) { n       lazy[2 * i] ^= 1; n       lazy[2 * i + 1] ^= 1; n     } n     return; n   } n   int mid = (s + e) / 2; n   upd(l, r, s, mid, 2 * i); n   upd(l, r, mid + 1, e, 2 * i + 1); n   seg[i] = seg[2 * i] + seg[2 * i + 1]; n } n int que(int l, int r, int s = 1, int e = n, int i = 1) { n   if (lazy[i]) { n     seg[i] = (e - s + 1) - seg[i]; n     if (s != e) { n       lazy[2 * i] ^= 1; n       lazy[2 * i + 1] ^= 1; n     } n     lazy[i] = 0; n   } n   if (s > r || e < l) return 0; n   if (s >= l && e <= r) return seg[i]; n   int mid = (s + e) / 2; n   return que(l, r, s, mid, 2 * i) + que(l, r, mid + 1, e, 2 * i + 1); n } n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cin >> n; n   for (int i = 2; i <= n; ++i) { n     cin >> x; n     G[x].push_back(i); n   } n   for (int i = 1; i <= n; ++i) cin >> a[i]; n   dfs(1); n   for (int i = 1; i <= n; ++i) n     if (a[i]) upd(tin[i], tin[i]); n   cin >> m; n   while (m--) { n     cin >> s >> x; n     if (s ==  pow ) n       upd(tin[x], tout[x]); n     else n       cout << que(tin[x], tout[x]) <<   n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int INF = 1e8 + 1; n vector<vector<int> > adj; n vector<bool> visited; n int contadorNodos = 0; n void DFS(int nodo) { n   contadorNodos++; n   for (auto it : adj[nodo]) { n     DFS(it); n   } n } n void letras(string &s) { n   for (int i = 0; i < s.size(); i++) { n     s[i] = tolower(s[i]); n   } n } n int main() { n   int n; n   cin >> n; n   map<string, int> m; n   adj.resize(205); n   visited.resize(205, false); n   int j = 1; n   for (int i = 0; i < n; i++) { n     string a, b, c; n     cin >> a >> b >> c; n     letras(a); n     letras(c); n     if (m[a] == 0) { n       m[a] = j; n       j++; n     } n     if (m[c] == 0) { n       m[c] = j; n       j++; n     } n     adj[m[a]].push_back(m[c]); n   } n   int res = 0; n   for (int i = 1; i < j; i++) { n     contadorNodos = 0; n     DFS(i); n     res = max(res, contadorNodos); n   } n   cout << res << endl; n } n


#include <bits/stdc++.h> n using namespace std; n const bool online_judge = true; n const long long inf = 1LL << 60; n long long toInt(string s) { n   long long res; n   stringstream ss; n   ss << s; n   ss >> res; n   return res; n } n string toString(long long n) { n   stringstream ss; n   ss << n; n   return ss.str(); n } n long long w, h; n vector<string> f; n typedef struct { n   long long x; n   long long y; n   long long dp; n   long long cp; n } state; n state memo[55][55][5][5]; n long long dx[4] = {0, 1, 0, -1}; n long long dy[4] = {-1, 0, 1, 0}; n long long ex[4] = {-1, 0, 1, 0}; n long long ey[4] = {0, -1, 0, 1}; n state solve(long long y, long long x, long long dp, long long cp) { n   if (memo[y][x][dp][cp].x != -1) return memo[y][x][dp][cp]; n   state ans; n   long long nx = x + dx[dp]; n   long long ny = y + dy[dp]; n   long long mx = x + ex[(dp + (cp ? 2 : 0)) % 4]; n   long long my = y + ey[(dp + (cp ? 2 : 0)) % 4]; n   if (f[ny][nx] == f[y][x]) { n     ans = solve(ny, nx, dp, cp); n   } else if (f[my][mx] == f[y][x]) { n     ans = solve(my, mx, dp, cp); n   } else { n     char c = f[ny][nx]; n     if (c ==  0 ) { n       if (cp == 0) { n         ans.y = y; n         ans.x = x; n         ans.dp = dp; n         ans.cp = 1; n       } else { n         ans.y = y; n         ans.x = x; n         ans.dp = (dp + 1) % 4; n         ans.cp = 0; n       } n     } else { n       ans.x = nx; n       ans.y = ny; n       ans.dp = dp; n       ans.cp = cp; n     } n   } n   memo[y][x][dp][cp] = ans; n   return ans; n } n void run() { n   long long m, n; n   cin >> m >> n; n   for (long long _ = 0; _ < m; _++) { n     string tmp; n     cin >> tmp; n     f.push_back(tmp); n   } n   w = f[0].size(); n   h = m; n   for (long long i = 0; i < m; i++) { n     f[i] =  0  + f[i] +  0 ; n   } n   f.insert(f.begin(), string(w + 2,  0 )); n   f.push_back(string(w + 2,  0 )); n   for (long long i = 1; i <= h; i++) { n     for (long long j = 1; j <= w; j++) { n       for (long long dp = 0; dp <= 3; dp++) { n         for (long long cp = 0; cp <= 1; cp++) { n           memo[i][j][dp][cp].x = -1; n         } n       } n     } n   } n   for (long long i = 1; i <= h; i++) { n     for (long long j = 1; j <= w; j++) { n       for (long long dp = 0; dp <= 3; dp++) { n         for (long long cp = 0; cp <= 1; cp++) { n           solve(i, j, dp, cp); n         } n       } n     } n   } n   long long x = 1, y = 1, dp = 1, cp = 0; n   while (n--) { n     state next = memo[y][x][dp][cp]; n     x = next.x; n     y = next.y; n     dp = next.dp; n     cp = next.cp; n   } n   cout << f[y][x] << endl; n } n int main(int argc, char *argv[]) { n   run(); n   return 0; n } n