#include <bits/stdc++.h> n using namespace std; n const int maxn = 2007; n int n, m, a[maxn][maxn]; n long long sr[maxn][maxn], sc[maxn][maxn], sumr[maxn], sumc[maxn], allsumr[maxn], n     allsumc[maxn]; n int main() { n   scanf( %d %d , &n, &m); n   for (int i = 1; i <= n; i++) { n     for (int j = 1; j <= m; j++) { n       scanf( %d , &a[i][j]); n       sumr[i] += a[i][j]; n       sumc[j] += a[i][j]; n     } n   } n   for (int r = 1; r <= n + 1; r++) { n     for (int nr = 1; nr <= n; nr++) { n       int dis; n       if (nr < r) n         dis = abs(r - nr - 1) * 4 + 2; n       else n         dis = abs(r - nr) * 4 + 2; n       sr[r][nr] = dis * dis * sumr[nr]; n       allsumr[r] += sr[r][nr]; n     } n   } n   for (int c = 1; c <= m + 1; c++) { n     for (int nc = 1; nc <= m; nc++) { n       int dis; n       if (nc < c) n         dis = abs(c - nc - 1) * 4 + 2; n       else n         dis = abs(c - nc) * 4 + 2; n       sc[c][nc] = dis * dis * sumc[nc]; n       allsumc[c] += sc[c][nc]; n     } n   } n   long long ans = 1e18 + 10; n   int x, y; n   for (int r = 1; r <= n + 1; r++) { n     for (int c = 1; c <= m + 1; c++) { n       if (allsumr[r] + allsumc[c] < ans) { n         ans = allsumr[r] + allsumc[c]; n         x = r, y = c; n       } n     } n   } n   printf( %lld n%d %d n , ans, x - 1, y - 1); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int v[100001]; n int acum[100001]; n long long cnt = 0, ans[100001]; n struct Q { n   int i, j, p; n   Q(int a, int b, int c) : i(a), j(b), p(c){}; n   Q() {} n   bool operator<(const Q &other) const { n     if (i / 300 == other.i / 300) return j < other.j; n     return i / 300 < other.i / 300; n   } n }; n vector<Q> queries; n int freq[2097151]; n int main() { n   int n, m, k; n   scanf( %d %d %d , &n, &m, &k); n   for (int i = 1; i <= n; i++) { n     scanf( %d , v + i); n     acum[i] = v[i] ^ acum[i - 1]; n   } n   for (int k = 0; k < m; k++) { n     int i, j; n     scanf( %d %d , &i, &j); n     queries.push_back(Q(i - 1, j, k)); n   } n   sort(queries.begin(), queries.end()); n   int l = 0, r = 0; n   freq[0] = 1; n   for (int q = 0; q < m; q++) { n     int i = queries[q].i, j = queries[q].j, p = queries[q].p; n     while (l > i) { n       --l; n       cnt += freq[acum[l] ^ k]; n       freq[acum[l]]++; n     } n     while (r < j) { n       r++; n       cnt += freq[acum[r] ^ k]; n       freq[acum[r]]++; n     } n     while (l < i) { n       freq[acum[l]]--; n       cnt -= freq[acum[l] ^ k]; n       l++; n     } n     while (r > j) { n       freq[acum[r]]--; n       cnt -= freq[acum[r] ^ k]; n       r--; n     } n     ans[p] = cnt; n   } n   for (int i = 0; i < m; i++) printf( %lld n , ans[i]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int people[105]; n int main() { n   int a, b, c; n   memset(people, 0, sizeof(people)); n   int n, m; n   scanf( %d%d , &n, &m); n   for (int i = 0; i < m; i++) { n     scanf( %d%d%d , &a, &b, &c); n     people[a] -= c; n     people[b] += c; n   } n   long long sum = 0; n   for (int i = 1; i <= n; i++) n     if (people[i] > 0) sum += people[i]; n   printf( %I64d n , sum); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n bool check(vector<int>& v, int k, int val, int start) { n   vector<int> dp(v.size()); n   if (v[0] <= val && start == 0) { n     dp[0] = dp[1] = 1; n   } n   if (v[1] <= val) { n     dp[1] = 1; n   } n   for (int i = 2; i < v.size(); ++i) { n     dp[i] = dp[i - 1]; n     if (v[i] <= val) { n       dp[i] = max(dp[i], 1 + dp[i - 2]); n     } n   } n   int endpos = v.size() - 1; n   if ((start == 1 && k % 2) || (start == 0 && k % 2 == 0)) { n     endpos--; n   } n   if (dp[endpos] >= (k + 1 - start) / 2) { n     return true; n   } n   return false; n } n bool checkall(vector<int>& v, int k, int val) { n   return check(v, k, val, 0) || check(v, k, val, 1); n } n void solve(std::istream& in, std::ostream& out) { n   int n, k; n   in >> n >> k; n   vector<int> v(n); n   for (auto& x : v) { n     in >> x; n   } n   int l = 1, r = 1e9 + 1; n   while (r - l > 1) { n     int m = (r + l) / 2; n     if (checkall(v, k, m)) { n       r = m; n     } else { n       l = m; n     } n   } n   if (checkall(v, k, l)) { n     r = l; n   } n   out << r << endl; n } n int main() { solve(cin, cout); } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 1e6 + 5; n const int MOD = 1e9 + 7; n inline int read() { n   int s = 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     s = (s << 1) + (s << 3) + ch -  0 ; n     ch = getchar(); n   } n   return s * f; n } n inline void write(long long x) { n   if (x < 0) x = -x, putchar( - ); n   if (x > 9) write(x / 10); n   putchar(x % 10 +  0 ); n } n int a[maxn]; n int main() { n   int T = read(), n, m, k; n   while (T--) { n     n = read(), k = read(); n     for (int i = 1; i <= n; i++) a[i] = read(); n     sort(a + 1, a + n + 1); n     int ans = 0; n     for (int i = 2; i <= n; i++) { n       if (a[1] + a[i] > k) continue; n       ans += (k - a[i]) / a[1]; n     } n     write(ans); n     putchar(  n ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long MOD = (long long)1e9 + 7; n const int N = 1e5 + 5, inf = 1e9 + 5; n long long add(long long x, long long y) { n   x += y; n   if (x >= MOD) return x - MOD; n   return x; n } n long long sub(long long x, long long y) { n   x -= y; n   if (x < 0) return x + MOD; n   return x; n } n long long mult(long long x, long long y) { return (x * y) % MOD; } n mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count()); n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(nullptr); n   int T; n   cin >> T; n   while (T--) { n     int n, m; n     cin >> n >> m; n     vector<int> row_sum(n), col_sum(m); n     vector<vector<bool>> a(n, vector<bool>(m)); n     for (int i = 0; i < n; i++) { n       for (int j = 0; j < m; j++) { n         char c; n         cin >> c; n         row_sum[i] += c ==  * ; n         col_sum[j] += c ==  * ; n         a[i][j] = c ==  * ; n       } n     } n     int ans = inf; n     for (int i = 0; i < n; i++) { n       for (int j = 0; j < m; j++) { n         ans = min(ans, n - row_sum[i] + m - col_sum[j] - (!a[i][j])); n       } n     } n     cout << ans << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int IINF = 1e9; n const long long LINF = 1e18; n const int K = 1e5 + 5; n const int KL = 1e6 + 5; n int n; n int a[505]; n int mem[505][505]; n bool tok[505]; n int dp(int l, int r) { n   if (l == r) return 1; n   if (l > r) return 0; n   if (mem[l][r] != -1) return mem[l][r]; n   int k; n   int ans = 505, ans2 = 505, ans3 = 505; n   if (a[l] == a[l + 1]) ans2 = min(ans2, 1 + dp(l + 2, r)); n   for (int k = l + 2; k <= r; k++) n     if (a[l] == a[k]) { n       ans = min(ans, dp(l + 1, k - 1) + dp(k + 1, r)); n     } n   ans3 = 1 + dp(l + 1, r); n   return mem[l][r] = min({ans, ans2, ans3}); n } n int main() { n   memset(mem, -1, sizeof mem); n   scanf( %d , &n); n   for (int i = 1; i <= n; i++) scanf( %d , &a[i]); n   printf( %d n , dp(1, n)); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   cin >> n; n   for (int i = 0; i < n * n / 2; i++) { n     cout << i + 1 <<     << n * n - i; n     if (2 * (i + 1) % n == 0) n       cout << endl; n     else n       cout <<    ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 2e5 + 10, mod = 1e9 + 7; n int n, m, q, top, col, cnt, st[N], len[N], size[N], dfn[N], bl[N], son[N], S[N], n     w[N], T[N], low[N], bin[N], dep[N], f[N]; n char sc[N][10]; n vector<int> e[N], v[N]; n int read() { n   int x = 0, f = 1; n   char c = getchar(); n   while (c <  0  || c >  9 ) { n     if (c ==  - ) f = -1; n     c = getchar(); n   } n   while (c >=  0  && c <=  9 ) n     x = (x << 3) + (x << 1) + (c ^ 48), c = getchar(); n   return f * x; n } n void Tarjan(int x) { n   dfn[x] = low[x] = ++cnt; n   st[++top] = x; n   for (int i = 0; i < e[x].size(); i++) { n     int y = e[x][i]; n     if (!dfn[y]) { n       Tarjan(y); n       low[x] = min(low[x], low[y]); n       if (low[y] == dfn[x]) { n         if (st[top] == y) { n           top--; n           v[x].push_back(y); n           v[y].push_back(x); n           continue; n         } n         size[++col] = 1; n         while (1) { n           int z = st[top--]; n           size[col]++; n           bl[z] = col; n           v[z].push_back(col); n           v[col].push_back(z); n           if (z == y) break; n         } n         bl[x] = col; n         v[x].push_back(col); n         v[col].push_back(x); n       } n     } else n       low[x] = min(low[x], dfn[y]); n   } n } n void dfs(int x, int fa) { n   S[x] = 1; n   f[x] = fa; n   dep[x] = dep[fa] + 1; n   w[x] = w[fa]; n   if (size[x] > 2) n     w[x]--; n   else if (size[bl[x]] > 2) n     w[x]++; n   for (int i = 0; i < v[x].size(); i++) { n     int y = v[x][i]; n     if (y == fa) continue; n     dfs(y, x); n     S[x] += S[y]; n     if (S[y] > S[son[x]]) son[x] = y; n   } n } n void dfs2(int x, int fa) { n   T[x] = fa; n   if (!son[x]) return; n   dfs2(son[x], fa); n   for (int i = 0; i < v[x].size(); i++) { n     int y = v[x][i]; n     if (T[y]) continue; n     dfs2(y, y); n   } n } n int lca(int x, int y) { n   while (T[x] ^ T[y]) { n     if (dep[T[x]] > dep[T[y]]) n       x = f[T[x]]; n     else n       y = f[T[y]]; n   } n   return dep[x] > dep[y] ? y : x; n } n int main() { n   n = read(), m = read(); n   col = n; n   for (int i = 1, x, y; i <= m; i++) { n     x = read(), y = read(); n     e[x].push_back(y); n     e[y].push_back(x); n   } n   Tarjan(1); n   bin[0] = 1; n   sc[0][len[0]++] =  1 ; n   for (int i = 1; i <= col; i++) { n     bin[i] = bin[i - 1] * 2 % mod; n     int x = bin[i]; n     while (x) { n       sc[i][len[i]++] =  0  + x % 10; n       x /= 10; n     } n   } n   dfs(1, 0); n   dfs2(1, 1); n   q = read(); n   for (int i = 1, x, y, z; i <= q; i++) { n     x = read(), y = read(); n     z = lca(x, y); n     z = w[x] + w[y] - w[z] - w[f[z]]; n     for (int j = len[z] - 1; j > -1; j--) putchar(sc[z][j]); n     putchar(  n ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long n, k; n   cin >> n >> k; n   vector<long long> div; n   for (long long i = 1; i <= sqrt(n); i++) { n     if (n % i == 0) { n       if (n / i != i) { n         div.push_back(n / i); n       } n       div.push_back(i); n     } n   } n   sort(div.begin(), div.end()); n   if (k > div.size()) { n     cout << -1 << endl; n   } else { n     cout << div[k - 1] << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 107; n const int M = 107 * 26; n const int inf = (int)1e9 + 7; n const long long base = (long long)1e18; n const double pi = acos(-1); n const double ep = 1e-18; n int dp[107][M]; n void solve() { n   dp[0][0] = 1; n   for (int i = 1; i < N; i++) { n     for (int j = 0; j < M; j++) { n       for (int t = 0; t <= min(25, j); t++) n         dp[i][j] = (dp[i][j] + dp[i - 1][j - t]) % inf; n     } n   } n   int test; n   cin >> test; n   while (test-- > 0) { n     string s; n     cin >> s; n     int sum = 0; n     for (int i = 0; i < s.size(); i++) sum += s[i] -  a ; n     cout << dp[s.size()][sum] - 1 << endl; n   } n } n int main() { n   solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   scanf( %d , &n); n   int num; n   while (n--) { n     scanf( %d , &num); n     printf( %d n , num > 2 ? num - 2 : 0); n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, k; n   while (cin >> n >> k) { n     set<int> s; n     int l = 0, r = 0; n     int ans[200005]; n     int a; n     while (n--) { n       cin >> a; n       if (s.count(a)) { n         continue; n       } n       s.insert(a); n       ans[r++] = a; n       if (r > k) { n         s.erase(ans[l++]); n       } n     } n     n = min(r - l, k); n     cout << n <<   n ; n     while (n--) { n       cout << ans[--r] <<    ; n     } n     cout <<   n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const double EPS = 1E-10; n inline int cmp(double x, double y = 0, double tol = EPS) { n   return (x <= y + tol) ? (x + tol < y) ? -1 : 0 : 1; n } n char M[440][440]; n int C[440][440]; n int dy[] = {-1, 0, 1, 0}; n int dx[] = {0, -1, 0, 1}; n int main() { n   int N, K; n   cin >> N >> K; n   N += 200; n   for (int i = 200; i < N; i++) n     for (int j = 200; j < N; j++) cin >> M[i][j]; n   for (int i = 200; i < N; i++) n     for (int j = 200; j < N; j++) n       if (M[i][j] ==  . ) { n         for (int d = 0; d < 4; d++) { n           bool ok = 1; n           int y = i, x = j; n           for (int k = 0; k < K && ok; k++, y += dy[d], x += dx[d]) n             if (M[y][x] !=  . ) ok = 0; n           y = i, x = j; n           if (ok) n             for (int k = 0; k < K; k++) C[y][x]++, y += dy[d], x += dx[d]; n         } n       } n   int rety = 0, retx = 0, ret = 0; n   for (int i = 200; i < N; i++) n     for (int j = 200; j < N; j++) n       if (ret < C[i][j]) ret = C[i][j], rety = i - 200, retx = j - 200; n   printf( %d %d n , rety + 1, retx + 1); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long powi(long long base, int p) { n   long long res = 1; n   while (p--) res *= base; n   return res; n } n long long gcd(long long a, long long b) { return b == 0 ? a : gcd(b, a % b); } n bool is_vowel(char c) { n   c = tolower(c); n   return c ==  a  || c ==  e  || c ==  o  || c ==  i  || c ==  u  || c ==  y ; n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   int n; n   cin >> n; n   map<int, int> dict; n   for (int i = 0, a; i < n; i++) { n     cin >> a; n     dict[a]++; n   } n   bool hh = false; n   int res = 0; n   for (auto p : dict) { n     res += p.second / 4; n     int c = p.second % 4; n     if (c >= 2 && hh) { n       res++; n       hh = false; n     } else if (c >= 2) n       hh = true; n   } n   cout << res; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n map<long, long> cmap; n map<long, long>::iterator it; n bool visit[200005]; n long c[200005]; n vector<long> graph[200005]; n long res = 0, mac, counter; n void DFS(long node); n int main(void) { n   long long n, m, k, i, l, r, j; n   cin >> n >> m >> k; n   memset(visit, false, sizeof(visit)); n   for (i = 1; i <= n; i++) cin >> c[i]; n   for (i = 1; i <= m; i++) { n     cin >> l >> r; n     graph[l].push_back(r); n     graph[r].push_back(l); n   } n   for (i = 1; i <= n; i++) { n     if (!visit[i]) { n       mac = 0; n       counter = 0; n       DFS(i); n       for (it = cmap.begin(); it != cmap.end(); it++) { n         mac = max(mac, it->second); n       } n       res += counter - mac; n       cmap.clear(); n     } n   } n   cout << res; n   return 0; n } n void DFS(long node) { n   visit[node] = true; n   counter++; n   cmap[c[node]]++; n   long j, i; n   for (i = 0; i < graph[node].size(); i++) { n     j = graph[node][i]; n     if (!visit[j]) DFS(j); n   } n } n


#include <bits/stdc++.h> n using namespace std; n template <class T1, class T2> n inline istream& operator>>(istream& fin, pair<T1, T2>& pr) { n   fin >> pr.first >> pr.second; n   return fin; n } n template <class T0, class T1, class T2> n inline istream& operator>>(istream& fin, tuple<T0, T1, T2>& t) { n   fin >> get<0>(t) >> get<1>(t) >> get<2>(t); n   return fin; n } n template <class T> n inline istream& operator>>(istream& fin, vector<T>& a) { n   if (!a.size()) { n     size_t n; n     fin >> n; n     a.resize(n); n   } n   for (auto& u : a) fin >> u; n   return fin; n } n string probB() { n   vector<string> a(2); n   cin >> a; n   const auto n = a[0].length(); n   size_t diff = 0; n   for (size_t i = 0; i < n; ++i) diff += a[0][i] != a[1][i]; n   if (diff & 1) return  impossible ; n   string str; n   size_t k = 0; n   for (size_t i = 0; i < n; ++i) n     if (a[0][i] == a[1][i]) n       str.push_back(a[0][i]); n     else if (2 * k < diff) n       str.push_back(a[0][i]), ++k; n     else n       str.push_back(a[1][i]); n   return str; n } n int main(const int argc, char* argv[]) { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   ; n   cout << probB(); n   return EXIT_SUCCESS; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 2e5 + 5; n int head[maxn], deg[maxn], ans[maxn], tot = 1; n struct edge { n   int v, nex, id; n   bool f; n } e[maxn << 4]; n void add(int u, int v, int id) { n   e[++tot] = {v, head[u], id, 0}, head[u] = tot; n   e[++tot] = {u, head[v], id, 0}, head[v] = tot; n } n struct Q { n   int l, r; n } q[maxn]; n vector<int> a; n void euler(int u) { n   for (int &i = head[u]; i; i = e[i].nex) { n     if (e[i].f) continue; n     e[i].f = e[i ^ 1].f = 1; n     int v = e[i].v; n     ans[e[i].id] = (u < v); n     euler(v); n   } n } n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   int n; n   cin >> n; n   for (int i = 0; i < n; i++) { n     cin >> q[i].l >> q[i].r; n     q[i].r++; n     a.push_back(q[i].l); n     a.push_back(q[i].r); n   } n   sort(a.begin(), a.end()); n   a.erase(unique(a.begin(), a.end()), a.end()); n   for (int i = 0; i < n; i++) { n     q[i].l = lower_bound(a.begin(), a.end(), q[i].l) - a.begin(); n     q[i].r = lower_bound(a.begin(), a.end(), q[i].r) - a.begin(); n     deg[q[i].l]++, deg[q[i].r]++; n     add(q[i].l, q[i].r, i); n   } n   int len = a.size(), last = -1; n   for (int i = 0; i < len; i++) { n     if (deg[i] & 1) { n       if (last == -1) n         last = i; n       else n         add(last, i, n + 3), last = -1; n     } n   } n   for (int i = 0; i < len; i++) n     if (head[i]) euler(i); n   for (int i = 0; i < n; i++) cout << ans[i] <<    ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n string LexicographicallyMaximumSubsequence(string text) { n   string localMax =   ; n   char current = text.at(text.length() - 1); n   localMax += current; n   unsigned int size = text.length() - 2; n   for (int i = size; i >= 0; i--) { n     if (text.at(i) >= current) { n       localMax = text.at(i) + localMax; n       current = text.at(i); n     } n   } n   return localMax; n } n int main() { n   string text; n   cin >> text; n   cout << LexicographicallyMaximumSubsequence(text) << endl; n   return 0; n } n


#include <bits/stdc++.h> n int main() { n   long int n, c = 0, d = 0, e = 0, f = 0, t, i, j; n   scanf( %ld , &n); n   int a[n], b[n]; n   for (i = 1; i <= n; i++) { n     scanf( %d %d , &a[i], &b[i]); n     if (a[i] < 0) n       c++; n     else n       d++; n   } n   for (i = 1; i < n; i++) { n     for (j = i + 1; j <= n; j++) { n       if (a[i] > a[j]) { n         t = a[i]; n         a[i] = a[j]; n         a[j] = t; n         t = b[i]; n         b[i] = b[j]; n         b[j] = t; n       } n     } n   } n   if (c < d) { n     for (i = 1; i <= 2 * c; i++) { n       e += b[i]; n       f += b[i]; n     } n     f += b[2 * c + 1]; n   } else if (c > d) { n     for (i = c - d + 1; i <= c + d; i++) { n       e += b[i]; n       f += b[i]; n     } n     f += b[c - d]; n   } else { n     for (i = 1; i <= c + d; i++) e += b[i]; n   } n   if (e >= f) n     printf( %d , e); n   else n     printf( %d , f); n } n


#include <bits/stdc++.h> n using namespace std; n struct Binode { n   Binode *z, *o; n   void init() { n     z = NULL; n     o = NULL; n   } n } T; n int n; n long long a[100010], o[100010], s[70]; n void add(long long x) { n   for (int i = 0; i < 61; i++) { n     s[i] = x & 1; n     x >>= 1; n   } n   Binode *p = &T; n   for (int i = 60; i >= 0; i--) { n     if (s[i]) { n       if (!(p->o)) { n         p->o = new Binode; n         p->o->init(); n       } n       p = p->o; n     } else { n       if (!(p->z)) { n         p->z = new Binode; n         p->z->init(); n       } n       p = p->z; n     } n   } n } n long long find(long long x) { n   for (int i = 0; i < 61; i++) { n     s[i] = x & 1; n     x >>= 1; n   } n   long long ans = 0; n   Binode *p = &T; n   for (int i = 60; i >= 0; i--) { n     if (s[i]) { n       if (p->z) { n         ans ^= (1LL << i); n         p = p->z; n       } else n         p = p->o; n     } else { n       if (p->o) { n         ans ^= (1LL << i); n         p = p->o; n       } else n         p = p->z; n     } n   } n   return ans; n } n int main() { n   scanf( %d , &n); n   for (int i = 1; i <= n; i++) scanf( %I64d , &a[i]); n   o[0] = 0; n   for (int i = 1; i <= n; i++) o[i] = o[i - 1] ^ a[i]; n   long long x = 0, ans = 0; n   T.init(); n   for (int i = n; i >= 0; i--) { n     add(x); n     ans = max(ans, find(o[i])); n     x = x ^ a[i]; n   } n   printf( %I64d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n; n vector<vector<int>> a; n bool inp(int i, int j) { n   (i < n && j == n) && inp(i + 1, 0); n   (i < n && j < n) && (cin >> a[i][j]); n   (i < n && j < n) && (inp(i, j + 1)); n   return 0; n } n bool floyd(int k, int i, int j) { n   (k < n && i < n && j == n) && floyd(k, i + 1, 0); n   (k < n && i == n) && floyd(k + 1, 0, 0); n   (k < n && i < n && j < n) && floyd(k, i, j + 1); n   (k < n && i < n && j < n) && (a[i][j] = min(a[i][j], a[i][k] + a[k][j])); n   return 0; n } n void go(int &ans, int i) { n   ans = max(ans, *max_element(begin(a[i]), end(a[i]))); n } n int main() { n   cin.tie(nullptr); n   ios_base::sync_with_stdio(0); n   cin >> n; n   a.assign(20, vector<int>(20)); n   inp(0, 0); n   floyd(0, 0, 0); n   int ans = 0; n   go(ans, 0); n   go(ans, 1); n   go(ans, 2); n   go(ans, 3); n   go(ans, 4); n   go(ans, 5); n   go(ans, 6); n   go(ans, 7); n   go(ans, 8); n   go(ans, 9); n   cout << ans <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int T; n long long n, m; n inline long long Fin(long long w) { n   if (!w) return m * (m + 1) / 2; n   long long p = w / m, x = Fin(p), t = w % m; n   return m * ((m * m + 1) * p + m * t) + m * (m + 1) / 2 + n          max(0ll, min(m, (m * m + 1) * p + t * m + m - x + 1)); n } n int main() { n   scanf( %d , &T); n   while (T--) { n     scanf( %lld%lld , &n, &m); n     long long p = (n - 1) / (m * m + 1), x = Fin(p), n               v1 = (p % m + 1 + m * (p / m)), o = (n - 1) % (m * m + 1); n     printf( %lld n , (n == x) n                          ? (v1 * (m + 1)) n                          : (n + p * m + ((o - (n > x)) / m) - v1 + (x > n))); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 1e3 + 5; n bool adj1[MAXN][MAXN], adj2[MAXN][MAXN]; n vector<pair<int, int> > ans1, ans2; n int n; n void comp1(int a, int b) { n   if (b - a == 1) return; n   for (int i = a + 1; i < b; ++i) { n     if (adj1[1][i] == 1) { n       comp1(a, i); n       comp1(i, b); n       return; n     } n   } n   assert(adj1[a][b] == 1); n   for (int i = a + 1; i < b; ++i) { n     if (adj1[i][a] * adj1[i][b] == 1) { n       ans1.push_back(make_pair(a, b)); n       adj1[a][b] = adj1[b][a] = 0; n       adj1[1][i] = adj1[i][1] = 1; n       comp1(a, i); n       comp1(i, b); n       return; n     } n   } n } n void comp2(int a, int b) { n   if (b - a == 1) return; n   for (int i = a + 1; i < b; ++i) { n     if (adj2[1][i] == 1) { n       comp2(a, i); n       comp2(i, b); n       return; n     } n   } n   assert(adj2[a][b] == 1); n   for (int i = a + 1; i < b; ++i) { n     if (adj2[i][a] * adj2[i][b] == 1) { n       ans2.push_back(make_pair(1, i)); n       adj2[a][b] = adj2[b][a] = 0; n       adj2[1][i] = adj2[i][1] = 1; n       comp2(a, i); n       comp2(i, b); n       return; n     } n   } n } n int main() { n   cin >> n; n   for (int i = 0; i < n - 3; ++i) { n     int u, v; n     cin >> u >> v; n     adj1[u][v] = adj1[v][u] = 1; n   } n   for (int i = 0; i < n - 3; ++i) { n     int u, v; n     cin >> u >> v; n     adj2[u][v] = adj2[v][u] = 1; n   } n   for (int i = 0; i < n; ++i) { n     adj1[i + 1][(i + 1) % n + 1] = adj1[(i + 1) % n + 1][i + 1] = n         adj2[i + 1][(i + 1) % n + 1] = adj2[(i + 1) % n + 1][i + 1] = 1; n   } n   comp1(2, n); n   comp2(2, n); n   cout << ans1.size() + ans2.size() << endl; n   for (int i = 0; i < ans1.size(); ++i) n     cout << ans1[i].first <<     << ans1[i].second << endl; n   for (int i = ans2.size() - 1; i >= 0; --i) n     cout << ans2[i].first <<     << ans2[i].second << endl; n } n


#include <bits/stdc++.h> n using namespace std; n long long parent[1000001]; n long long result[1000005]; n map<int, list<int>> g; n long long getPoints(int p, int t) { n   return max((3 * p) / 10, (p - (p / 250) * t)); n } n struct Score { n   int p, t; n }; n bool chc(Score a, Score b) { n   if (a.p == b.p) { n     return a.t < b.t; n   } else { n     return a.p < b.p; n   } n } n void solve() { n   int n; n   cin >> n; n   int tf = 0, fifty = 0; n   vector<int> a(n); n   for (int i = 0; i < n; i++) { n     cin >> a[i]; n   } n   for (int i = 0; i < n; i++) { n     int x = a[i]; n     if (x == 25) { n       tf++; n     } else if (x == 50) { n       if (tf == 0) { n         cout <<  NO n ; n         return; n       } else { n         tf--; n       } n       fifty++; n     } else if (x == 100) { n       if (fifty > 0 && tf > 0) { n         fifty--; n         tf--; n       } else if (tf > 2) { n         tf = tf - 3; n       } else { n         cout <<  NO n ; n         return; n       } n     } n   } n   cout <<  YES n ; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); 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 const int maxN = 1e5 + 7; n int n, m; n vector<tuple<int, int, int>> res; n vector<pair<int, int>> g[maxN]; n bool blocked[maxN]; n bool solve(int v, int pr = -1) { n   vector<int> unpaired; n   vector<int> adj; n   for (auto& to : g[v]) { n     if (!blocked[to.second]) { n       adj.push_back(to.first); n     } n     blocked[to.second] = true; n   } n   for (auto& to : adj) { n     if (solve(to, v)) { n       unpaired.push_back(to); n     } n   } n   while (unpaired.size() >= 2) { n     int first = unpaired.back(); n     unpaired.pop_back(); n     int second = unpaired.back(); n     unpaired.pop_back(); n     res.push_back(make_tuple(first, v, second)); n   } n   if (unpaired.size() > 0) { n     int vertex = unpaired.back(); n     unpaired.pop_back(); n     res.push_back(make_tuple(pr, v, vertex)); n     return false; n   } n   return true; n } n int main() { n   scanf( %d %d , &n, &m); n   if (m % 2 == 1) { n     printf( No solution n ); n     return 0; n   } n   for (int i = 0; i < m; ++i) { n     int a, b; n     scanf( %d %d , &a, &b); n     g[a].push_back({b, i}); n     g[b].push_back({a, i}); n   } n   solve(1); n   for (auto& x : res) { n     printf( %d %d %d n , get<0>(x), get<1>(x), get<2>(x)); n   } n   return 0; n } n


#include <bits/stdc++.h> n const int mod = 1e9 + 7; n int n, m; n inline long long ksm(register int y, register int p) { n   long long x = y, now = 1; n   while (p) { n     if (p & 1) now = now * x % mod; n     x = x * x % mod, p >>= 1; n   } n   return now; n } n int main() { n   scanf( %d%d , &n, &m); n   printf( %lld , ksm(2, m) % mod * ksm(n + 1, m - 1) % mod * (n + 1 - m) % mod); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main(int argc, char** argv) { n   int n, x, p, min = 0, max = 1000; n   cin >> n >> x; n   p = x; n   for (int i = 0; i < n; i++) { n     int a, b; n     cin >> a >> b; n     if (a < b) { n       if ((p >= a) && (p <= b)) { n         if (a > min) min = a; n         if (b < max) max = b; n       } else if (p < a) { n         if (a <= max) { n           p = a; n           if (a > min) min = a; n           if (b < max) max = b; n         } else { n           cout << -1; n           return 0; n         } n       } else if (p > b) { n         if (b >= min) { n           p = b; n           if (a > min) min = a; n           if (b < max) max = b; n         } else { n           cout << -1; n           return 0; n         } n       } n     } else { n       if ((p >= b) && (p <= a)) { n         if (b > min) min = b; n         if (a < max) max = a; n       } else if (p < b) { n         if (b <= max) { n           p = b; n           if (b > min) min = b; n           if (a < max) max = a; n         } else { n           cout << -1; n           return 0; n         } n       } else if (p > a) { n         if (a >= min) { n           p = a; n           if (b > min) min = b; n           if (a < max) max = a; n         } else { n           cout << -1; n           return 0; n         } n       } n     } n   } n   if ((p - x) >= 0) n     cout << p - x; n   else n     cout << x - p; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<long long int> graph[250]; n long long int n, a[250]; n long long int visit[250]; n long long int sol(long long int pos) { n   memset(visit, 0, sizeof(visit)); n   long long int ans = 0; n   long long int c = 0; n   while (c <= n) { n     long long int f = 0; n     for (long long int i = 1; i <= n; i++) { n       if (a[i] != pos || visit[i]) { n         continue; n       } n       long long int ff = 1; n       for (auto p : graph[i]) { n         if (!visit[p]) { n           ff = 0; n           break; n         } n       } n       if (ff) { n         f = 1; n         ans++; n         c++; n         visit[i] = 1; n       } n     } n     if (c == n) { n       break; n     } n     if (!f) { n       pos++; n       ans++; n       if (pos == 4) { n         pos = 1; n       } n     } n   } n   return ans; n } n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cin >> n; n   for (long long int i = 1; i <= n; i++) { n     cin >> a[i]; n   } n   for (long long int i = 1; i <= n; i++) { n     long long int k; n     cin >> k; n     for (long long int j = 1; j <= k; j++) { n       long long int tl; n       cin >> tl; n       graph[i].push_back(tl); n     } n   } n   long long int ans = 1e18; n   for (long long int i = 1; i <= 3; i++) { n     ans = min(ans, sol(i)); n   } n   cout << ans <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAX = 1000000; n const int MAX_N = 50000; n const int oo = 1e9; n const int LOGN = 20; n const int Z = 230; n int f[MAX + 1], n, m, a[MAX_N], ans[MAX_N]; n struct node { n   int nxt[2], val; n   multiset<int> mst; n   node() { n     memset(nxt, -1, sizeof nxt); n     val = oo; n   } n }; n struct query { n   int l, r, idx; n   query(int _l, int _r, int _idx) { l = _l, r = _r, idx = _idx; } n   bool operator<(const query &rhs) const { return r < rhs.r; } n }; n vector<query> q[Z]; n struct honey { n   vector<node> trie; n   int ty; n   honey(int _ty) { ty = _ty; } n   void insert(int u, int depth, int first, int ax, bool add) { n     if (!trie.size()) trie.push_back(node()); n     if (depth == -1) { n       if (add) n         trie[u].mst.insert(ax); n       else n         trie[u].mst.erase(trie[u].mst.find(ax)); n       if (ty == 0) { n         trie[u].val = trie[u].mst.size() ? *trie[u].mst.begin() : oo; n       } else { n         trie[u].val = trie[u].mst.size() ? *--trie[u].mst.end() : -oo; n       } n       return; n     } n     bool bt = (first >> depth) & 1; n     if (trie[u].nxt[bt] == -1) { n       trie[u].nxt[bt] = trie.size(); n       trie.push_back(node()); n     } n     insert(trie[u].nxt[bt], depth - 1, first, ax, add); n     trie[u].val = (!ty ? oo : -oo); n     if (trie[u].nxt[bt] != -1) trie[u].val = trie[trie[u].nxt[bt]].val; n     if (trie[u].nxt[bt ^ 1] != -1) { n       if (!ty) n         trie[u].val = min(trie[u].val, trie[trie[u].nxt[bt ^ 1]].val); n       else n         trie[u].val = max(trie[u].val, trie[trie[u].nxt[bt ^ 1]].val); n     } n   } n   int search(int u, int depth, int first, int ax) { n     if (depth == -1 || u == -1 || n         (ty == 0 ? trie[u].val > ax : trie[u].val < ax)) n       return 0; n     bool bt = (first >> depth) & 1; n     if (trie[u].nxt[bt ^ 1] != -1 && n         (ty == 0 ? trie[trie[u].nxt[bt ^ 1]].val <= ax n                  : trie[trie[u].nxt[bt ^ 1]].val >= ax)) n       return (1 << depth) + search(trie[u].nxt[bt ^ 1], depth - 1, first, ax); n     return search(trie[u].nxt[bt], depth - 1, first, ax); n   } n   void clear() { trie.clear(); } n }; n honey lv[2] = {honey(0), honey(1)}; n int main() { n   scanf( %d%d , &n, &m); n   for (int i = 0; i < n; ++i) scanf( %d , &a[i]); n   for (int i = 0, l, r; i < m; ++i) { n     scanf( %d%d , &l, &r); n     --l, --r; n     q[l / Z].push_back(query(l, r, i)); n   } n   for (int i = 1; i <= MAX; ++i) f[i] = i ^ f[i - 1]; n   for (int bucket = 0; bucket < Z; ++bucket) { n     if (q[bucket].size() == 0) continue; n     sort(q[bucket].begin(), q[bucket].end()); n     int idx = bucket * Z + Z; n     int ed = min(n - 1, bucket * Z + Z - 1); n     lv[0].clear(); n     lv[1].clear(); n     int mx = 0; n     for (int j = 0; j < q[bucket].size(); ++j) { n       query &sh = q[bucket][j]; n       while (idx <= sh.r) { n         lv[0].insert(0, LOGN, f[a[idx] - 1], a[idx], 1); n         mx = max(mx, lv[0].search(0, LOGN, f[a[idx]], a[idx])); n         lv[1].insert(0, LOGN, f[a[idx]], a[idx], 1); n         mx = max(mx, lv[1].search(0, LOGN, f[a[idx] - 1], a[idx])); n         idx++; n       } n       ans[sh.idx] = mx; n       int lf = sh.l, rf = min(ed, sh.r); n       for (int p = lf; p <= rf; ++p) { n         lv[0].insert(0, LOGN, f[a[p] - 1], a[p], 1); n         lv[1].insert(0, LOGN, f[a[p]], a[p], 1); n       } n       for (int p = lf; p <= rf; ++p) { n         ans[sh.idx] = max(ans[sh.idx], lv[0].search(0, LOGN, f[a[p]], a[p])); n         ans[sh.idx] = n             max(ans[sh.idx], lv[1].search(0, LOGN, f[a[p] - 1], a[p])); n       } n       for (int p = lf; p <= rf; ++p) { n         lv[0].insert(0, LOGN, f[a[p] - 1], a[p], 0); n         lv[1].insert(0, LOGN, f[a[p]], a[p], 0); n       } n     } n   } n   for (int i = 0; i < m; ++i) { n     printf( %d n , ans[i]); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m; n int mat[505][505]; n const int N = 3e5 + 10, C = 26, inf = 1e7; n bool check(int i, int j) { n   if ((i - 1 > 0 || i - 1 == 0) && mat[i - 1][j] == 1) { n     return 1; n   } n   if ((j - 1 > 0 || j - 1 == 0) && mat[i][j - 1] == 1) { n     return 1; n   } n   if (mat[i + 1][j] == 1 && i < n) { n     return 1; n   } n   if (mat[i][j + 1] == 1 && j < m) { n     return 1; n   } n   return 0; n } n void FUSE() { n   cin >> n >> m; n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < m; j++) { n       char t; n       cin >> t; n       if (t ==  S ) n         mat[i][j] = -1; n       else if (t ==  W ) n         mat[i][j] = 1; n       else n         mat[i][j] = 0; n     } n   } n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < m; j++) { n       if (mat[i][j] == -1 && check(i, j)) { n         cout <<  No n ; n         return; n       } n     } n   } n   cout <<  Yes n ; n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < m; j++) { n       if (mat[i][j] == -1) n         cout <<  S ; n       else if (mat[i][j] == 1) n         cout <<  W ; n       else n         cout <<  D ; n     } n     cout <<   n ; n   } n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   int T = 1; n   while (T--) { n     FUSE(); n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int N = (int)1e5 + 100; n int dp[N]; n int b[N]; n int main() { n   int n; n   cin >> n; n   for (int i = 0; i < n; i++) cin >> b[i]; n   int Max = 0; n   vector<int> v; n   for (int i = 0; i < n; i++) { n     while (!v.empty()) { n       int ind = v.back(); n       if (b[ind] < b[i]) { n         dp[i] = max(dp[i], dp[ind] + 1); n         v.pop_back(); n       } else { n         dp[i] = max(dp[i], 1); n         break; n       } n     } n     if (v.empty()) dp[i] = 0; n     Max = max(Max, dp[i]); n     v.push_back(i); n   } n   cout << Max; n   cin >> n; n } n


#include <bits/stdc++.h> n using namespace std; n const int dx[9] = {0, 1, -1, 0, 0, -1, -1, 1, 1}; n const int dy[9] = {0, 0, 0, -1, 1, -1, 1, -1, 1}; n const double pi = acos(-1.0); n const int N = 110; n long long n, m, ans; n void input_data() { cin >> n >> m; } n void get_ans() { n   if (n <= m) { n     ans = n; n     return; n   } n   n -= (m + 1); n   if (n <= 0) { n     ans = m + 1; n     return; n   } n   long long l = 0, r = 2e9 + 2, t = 0; n   ans = m; n   while (l <= r) { n     long long mid = (l + r) >> 1; n     long long temp = (mid + 2) * (mid - 1) / 2; n     if (temp >= n) { n       t = mid; n       r = mid - 1; n     } else n       l = mid + 1; n   } n   ans += t; n } n void o() { cout << ans << endl; } n int main() { n   input_data(); n   get_ans(); n   o(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T> n void solve() { n   int n, x; n   cin >> n >> x; n   vector<int> cs(n); n   for (auto& c : cs) { n     cin >> c; n   } n   int c_sum = accumulate(cs.begin(), cs.end(), 0); n   vector<vector<T>> mass(n + 1, vector<T>(c_sum + 1, 0)); n   mass[0][0] = T(1); n   for (int c : cs) { n     for (int k = n - 1; k >= 0; --k) { n       for (int s = c_sum - c; s >= 0; --s) { n         mass[k + 1][s + c] += T(k + 1) / T(n - k) * mass[k][s]; n       } n     } n   } n   T answer(0); n   for (int k = 1; k <= n; ++k) { n     for (int s = 0; s <= c_sum; ++s) { n       answer += n           min(T(s) / T(k), T(x) / T(2) * (T(1) + T(n) / T(k))) * mass[k][s]; n     } n   } n   printf( %.17f n , (double)answer); n } n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   solve<double>(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 210; 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 long long LCM = 1, limit, X; n int n, a[MAXN]; n bool inCir[MAXN]; n int main() { n   scanf( %d , &n); n   for (int i = 1; i <= n; i++) scanf( %d , &a[i]); n   for (int i = 1; i <= n; i++) { n     int x = i, Len = 0; n     for (int j = 1; j <= n; j++) x = a[x]; n     while (!inCir[x]) inCir[x] = true, x = a[x], Len++; n     if (Len) LCM = lcm(LCM, Len); n   } n   for (int i = 1; i <= n; i++) { n     int x = i, Len = 0; n     while (!inCir[x]) x = a[x], Len++; n     limit = max(limit, (long long)Len); n   } n   X = LCM; n   while (X < limit) X += LCM; n   printf( %I64d n , X); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct SegmentTree { n   int n; n   struct node { n     long long add, mn; n     node() {} n     node(long long v) : mn(v), add(0) {} n   }; n   vector<node> tree; n   void push(int id, int l, int r) { n     if (tree[id].add != 0) { n       int m = (l + r) >> 1; n       apply(id << 1, l, m, tree[id].add); n       apply(id << 1 | 1, m + 1, r, tree[id].add); n       tree[id].add = 0; n     } n   } n   void apply(int id, int l, int r, long long add) { n     tree[id].mn += add; n     tree[id].add += add; n   } n   void pull(int id) { n     tree[id].mn = min(tree[id << 1].mn, tree[id << 1 | 1].mn); n   } n   SegmentTree(int n) : n(n), tree(4 * n) {} n   SegmentTree(const vector<long long> &v) { n     n = (int)v.size(); n     tree.resize(4 * n); n     function<void(int, int, int)> build = [&](int id, int l, int r) { n       if (l == r) { n         tree[id] = v[l]; n         return; n       } n       push(id, l, r); n       int m = (l + r) >> 1; n       build(id << 1, l, m); n       build(id << 1 | 1, m + 1, r); n       pull(id); n     }; n     build(1, 0, n - 1); n   } n   void rangeadd(int id, int l, int r, int ql, int qr, long long add) { n     if (l > qr || r < ql) { n       return; n     } n     if (ql <= l && r <= qr) { n       apply(id, l, r, add); n       return; n     } n     push(id, l, r); n     int m = (l + r) >> 1; n     rangeadd(id << 1, l, m, ql, qr, add); n     rangeadd(id << 1 | 1, m + 1, r, ql, qr, add); n     pull(id); n   } n   void rangeadd(int ql, int qr, long long add) { n     rangeadd(1, 0, n - 1, ql, qr, add); n   } n }; n void solve() { n   int N; n   cin >> N; n   vector<int> P(N), pos(N); n   for (int i = 0; i < N; i++) { n     cin >> P[i]; n     P[i]--; n     pos[P[i]] = i; n   } n   vector<int> A(N); n   for (int i = 0; i < N; i++) { n     cin >> A[i]; n   } n   vector<long long> init(N - 1); n   for (int i = 0; i < N - 1; i++) { n     init[i] = (i == 0 ? 0 : init[i - 1]) + A[i]; n   } n   SegmentTree st(init); n   long long ans = A[0]; n   for (int i = 0; i < N; i++) { n     st.rangeadd(0, pos[i] - 1, A[pos[i]]); n     st.rangeadd(pos[i], N - 2, -A[pos[i]]); n     ans = min(ans, st.tree[1].mn); n   } n   cout << ans <<   n ; n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   int t = 1; n   while (t--) solve(); n   return 0; n } n


#include <bits/stdc++.h> n #pragma GCC optimize( O3 ) n using namespace std; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   int t; n   cin >> t; n   while (t--) { n     int n; n     cin >> n; n     int arr[n]; n     for (int i = 0; i < n; i++) cin >> arr[i]; n     if (n < 6) { n       cout << 0 <<     << 0 <<     << 0 << endl; n       continue; n     } n     int index = (n / 2) - 1; n     while (index >= 0 && arr[index] == arr[index + 1]) index--; n     vector<int> count; n     int sum = 0; n     for (int i = index; i >= 0; i--) { n       int temp = 1; n       int j = i - 1; n       while (j >= 0 && arr[j] == arr[i]) { n         j--; n         temp++; n       } n       count.push_back(temp); n       sum += temp; n       i = j + 1; n     } n     if (count.size() < 3) { n       cout << 0 <<     << 0 <<     << 0 << endl; n       continue; n     } n     sum -= count[count.size() - 1]; n     int g = count[count.size() - 1], s = 0, b = 0; n     bool ans = 0; n     for (int i = 0; i < count.size() - 1; i++) { n       b += count[i]; n       s = sum - b; n       if (g < b && g < s) { n         ans = 1; n         break; n       } n     } n     if (ans == 1) n       cout << g <<     << s <<     << b << endl; n     else n       cout << 0 <<     << 0 <<     << 0 << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long cal(long long x, long long a) { n   long long y = a, ans = 0, cnt = 1; n   while (x / (long long)(pow(a, cnt)) != 0) { n     ans += x / (long long)(pow(a, cnt)); n     cnt++; n   } n   return ans; n } n signed main() { n   long long n, b, mx = LLONG_MAX; n   vector<long long> v, k; n   cin >> n >> b; n   for (long long i = 2; i * i <= b; i++) { n     if (b % i == 0) { n       long long cnt = 0; n       v.push_back(i); n       while (b % i == 0) { n         b = b / i; n         cnt++; n       } n       k.push_back(cnt); n     } n   } n   if (b != 1) v.push_back(b), k.push_back(1); n   for (long long i = 0; i < v.size(); i++) { n     mx = min(cal(n, v[i]) / k[i], mx); n   } n   cout << mx; n } n


#include <bits/stdc++.h> n const double PI = 2.0 * acos(0.0); n const double EPS = 1e-9; n using namespace std; n int dr[] = {-1, 0, 1, 0, 1, 1, -1, -1}; n int dc[] = {0, 1, 0, -1, -1, 1, -1, 1}; n int V, E; n int T; n int N, M, K; n string S; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   cin >> N >> K; n   int ans[5010]; n   int i, j; n   map<int, vector<int>> m; n   for (i = 0; i <= N - 1; i++) cin >> j, m[j].push_back(i); n   int col = 0; n   for (auto x : m) { n     if (x.second.size() > K) { n       cout <<  NO  << endl; n       return 0; n     } else { n       for (j = 0; j <= x.second.size() - 1; j++) { n         ans[x.second[j]] = col % K + 1; n         col++; n       } n     } n   } n   cout <<  YES  << endl; n   for (i = 0; i <= N - 1; i++) cout << ans[i] <<    ; n   cout << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e5 + 50; n const long long MAXN = 1e18 + 10; n const int MAX = 1e5 + 10; n const long long mod = 1e9 + 7; n const int inf = 0x3f3f3f3f; n const long long INF = 0x3f3f3f3f3f3f3f3f; n const double eps = 1e-7; n inline void solve() { n   int n, w; n   scanf( %d , &n); n   scanf( %d , &w); n   int minn = 0, maxn = w; n   bool flag = false; n   for (int i = 0; i < n; ++i) { n     int temp; n     scanf( %d , &temp); n     if (temp < 0) { n       minn += (-temp); n       if (minn > w) flag = true; n       maxn = min(w, maxn - temp); n     } else if (temp > 0) { n       maxn -= temp; n       if (maxn < 0) flag = true; n       minn = max(0, minn - temp); n     } n     if (maxn < minn) flag = true; n   } n   if (flag) n     printf( 0 n ); n   else n     printf( %d n , maxn - minn + 1); n   return; n } n int main() { n   int casen = 1; n   while (casen--) { n     solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   int n; n   cin >> n; n   cin.get(); n   string s; n   getline(cin, s); n   vector<int> pref(n); n   for (int i = 0; i < n; ++i) { n     if (i != 0) pref[i] = pref[i - 1]; n     pref[i] += s[i] ==  (  ? +1 : -1; n   } n   if (pref[n - 1] != 0) { n     cout <<  0 n1 1 n ; n     return 0; n   } n   int min_pos = min_element(pref.begin(), pref.end()) - pref.begin() + 1; n   if (min_pos == n) min_pos = 0; n   rotate(s.begin(), s.begin() + min_pos, s.end()); n   for (int i = 0; i < n; ++i) { n     pref[i] = 0; n     if (i != 0) pref[i] = pref[i - 1]; n     pref[i] += s[i] ==  (  ? +1 : -1; n     assert(pref[i] >= 0); n   } n   vector<int> merged; n   vector<vector<int>> pos(2); n   vector<vector<int>> cnt(n, vector<int>(3)); n   pos[0].emplace_back(-1); n   merged.emplace_back(-1); n   for (int i = 0; i < n; ++i) { n     if (i != 0) cnt[i] = cnt[i - 1]; n     if (pref[i] < 3) { n       cnt[i][pref[i]] += 1; n       if (pref[i] < 2) { n         merged.emplace_back(i); n         pos[pref[i]].emplace_back(i); n       } n     } n   } n   tuple<int, int, int> ans((int)pos[0].size() - 1, 0, 0); n   for (int i = 0; i + 1 < (int)pos[0].size(); ++i) { n     int l = pos[0][i], r = pos[0][i + 1]; n     int cnt_1 = cnt[r][1] - (l >= 0 ? cnt[l][1] : 0); n     ans = max(ans, make_tuple(cnt_1, l + 1, r)); n   } n   for (int i = 0; i + 1 < (int)merged.size(); ++i) { n     int l = merged[i], r = merged[i + 1]; n     if (s[l + 1] == s[r]) continue; n     int cnt_2 = cnt[r][2] - (l >= 0 ? cnt[l][2] : 0); n     ans = max(ans, make_tuple(cnt_2 + (int)pos[0].size() - 1, l + 1, r)); n   } n   int res, l, r; n   tie(res, l, r) = ans; n   (l += min_pos) %= n; n   (r += min_pos) %= n; n   cout << res << endl; n   cout << 1 + l <<     << 1 + r << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios::sync_with_stdio(false); n   ; n   long long i, j, k, l, r, m, n, o, t; n   cin >> n; n   cin >> l >> r; n   char str[100001]; n   cin >> str; n   long long len = strlen(str); n   l--; n   r--; n   if (str[l] == str[r]) { n     cout << 0 << endl; n     return 0; n   } n   cout << 1 << endl; n } n


#include <bits/stdc++.h> n using namespace std; n vector<int> vec; n int result[50000]; n int MIN(int i, int j) { n   int ans = 1e9; n   for (int a = i; a <= j; a++) { n     ans = min(ans, vec[a]); n   } n   return ans; n } n int main() { n   int n, m; n   scanf( %d %d , &n, &m); n   for (int i = 0; i < (n); i++) vec.push_back(i + 1); n   int limit = 1; n   for (int i = 0; i < (n); i++) limit *= (i + 1); n   int sum, mx = 0, realsum = 0; n   for (int z = 0; z < (limit); z++) { n     for (int i = 0; i < (n); i++) { n       sum = 0; n       for (int j = i; j < n; j++) sum += MIN(i, j); n       ; n       realsum += sum; n     } n     if (realsum > mx) mx = realsum; n     result[z] = realsum; n     realsum = 0; n     next_permutation(vec.begin(), vec.end()); n   } n   vector<int> sol; n   for (int i = 0; i < (n); i++) sol.push_back(i + 1); n   int cnt = 0; n   for (int z = 0; z < (limit); z++) { n     if (result[z] == mx) { n       cnt++; n       if (cnt == m) { n         printf( %d , sol[0]); n         for (int i = 1; i < n; i++) printf(  %d , sol[i]); n         printf(  n ); n         break; n       } n     } n     next_permutation(sol.begin(), sol.end()); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long mod = 1000000007; n const int N = 1e5 + 9; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   ; n   int x; n   cin >> x; n   long long ans = 0, c = 0; n   int y[x]; n   for (int i = 0; i < x; i++) cin >> y[i]; n   for (int i = x - 1; i >= 0; i--) { n     if (!y[i]) n       c++; n     else n       ans += c; n   } n   cout << ans <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MX = 102, md = 1000000007; n int n, k, i, j, e, c[MX][MX], f[MX][MX * MX]; n long long m, p[MX], q[MX]; n bool ifq; n long long pw(long long a, long long b) { n   if (b == 0) return 1LL; n   if (b & 1) return (a * pw(a, b - 1)) % md; n   long long x = pw(a, b / 2); n   return (x * x) % md; n } n int main() { n   cin >> n >> m >> k; n   for (i = 0; i <= n; i++) { n     c[i][0] = 1; n     for (j = 1; j <= i; j++) c[i][j] = (c[i - 1][j - 1] + c[i - 1][j]) % md; n   } n   for (i = 0; i <= n; i++) { n     p[i] = pw(c[n][i], m / n); n     q[i] = (p[i] * c[n][i]) % md; n   } n   f[0][0] = 1; n   for (i = 0; i < n; i++) { n     ifq = (i < (m % n)); n     for (j = 0; j <= k; j++) n       if (f[i][j]) n         for (e = 0; e <= n && j + e <= k; e++) n           f[i + 1][j + e] = n               (f[i + 1][j + e] + f[i][j] * (ifq ? q[e] : p[e])) % md; n   } n   cout << f[n][k] <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int INF = 0x3f3f3f3f; n const long long INFF = 0x3f3f3f3f3f3f3f3f; n const double pi = acos(-1.0); n const double eps = 1e-9; n const long long mod = 1e9 + 7; n int read() { 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 = x * 10 + ch -  0 ; n     ch = getchar(); n   } n   return x * f; n } n void Out(int aa) { n   if (aa > 9) Out(aa / 10); n   putchar(aa % 10 +  0 ); n } n int x[505], y[505]; n int dp[1010][1010]; n int n, m, k; n bool check(int t) { n   vector<int> X, Y; n   X.push_back(1), X.push_back(n + 1); n   Y.push_back(1), Y.push_back(m + 1); n   for (int i = 1; i <= k; i++) { n     X.push_back(max(x[i] - t, 1)), X.push_back(min(n + 1, x[i] + t + 1)); n     Y.push_back(max(1, y[i] - t)), Y.push_back(min(m + 1, y[i] + t + 1)); n   } n   sort(X.begin(), X.end()); n   X.erase(unique(X.begin(), X.end()), X.end()); n   sort(Y.begin(), Y.end()); n   Y.erase(unique(Y.begin(), Y.end()), Y.end()); n   memset(dp, 0, sizeof(dp)); n   for (int i = 1; i <= k; i++) { n     int l1 = lower_bound(X.begin(), X.end(), max(x[i] - t, 1)) - X.begin(); n     int l2 = n         lower_bound(X.begin(), X.end(), min(x[i] + t + 1, n + 1)) - X.begin(); n     int r1 = lower_bound(Y.begin(), Y.end(), max(y[i] - t, 1)) - Y.begin(); n     int r2 = n         lower_bound(Y.begin(), Y.end(), min(y[i] + t + 1, m + 1)) - Y.begin(); n     dp[l1][r1]++, dp[l2][r2]++; n     dp[l1][r2]--; n     dp[l2][r1]--; n   } n   int l = n + 1, r = 0; n   int u = m + 1, d = 0; n   for (int i = 0; i < X.size() - 1; i++) n     for (int j = 0; j < Y.size() - 1; j++) { n       if (i && j) n         dp[i][j] += -dp[i - 1][j - 1] + dp[i][j - 1] + dp[i - 1][j]; n       else if (i) n         dp[i][j] += dp[i - 1][j]; n       else if (j) n         dp[i][j] += dp[i][j - 1]; n       if (dp[i][j]) continue; n       l = min(l, X[i]); n       r = max(r, X[i + 1] - 1); n       u = min(u, Y[j]); n       d = max(d, Y[j + 1] - 1); n     } n   return max(r - l, d - u) <= 2 * t; n } n int main() { n   n = read(), m = read(), k = read(); n   for (int i = 1; i <= k; i++) x[i] = read(), y[i] = read(); n   int l = 0, r = max(n, m); n   int ans = r; n   while (r >= l) { n     int mid = (l + r) / 2; n     if (check(mid)) n       ans = mid, r = mid - 1; n     else n       l = mid + 1; n   } n   Out(ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int tr[400], ar[400]; n vector<pair<int, int> > vv; n int main() { n   int n; n   cin >> n; n   for (int i = 0; i < n; i++) cin >> ar[i]; n   for (int i = 0; i < n; i++) cin >> tr[i]; n   for (int i = 0; i < n; i++) { n     if (tr[i] == ar[i]) continue; n     int j; n     for (j = i + 1; j < n; j++) n       if (tr[j] == ar[i]) break; n     for (j = j - 1; j >= i; j--) { n       int tmp = tr[j]; n       tr[j] = tr[j + 1]; n       tr[j + 1] = tmp; n       vv.push_back(make_pair(j, j + 1)); n     } n   } n   if (vv.size() >= 1000000) return -1; n   cout << vv.size() << endl; n   for (int i = 0; i < vv.size(); i++) { n     cout << vv[i].first + 1 <<     << vv[i].second + 1 << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long inf = 1e15 + 9; n const long long N = 1e5 + 5; n long long A[N], n, k, B[N]; n map<long long, long long> mm; n void solve() { n   long long i, j, x = 0, y = 1, m; n   cin >> n; n   for (i = 0; i < n; i++) { n     cin >> x >> y; n     mm[x] = max(mm[x], y); n   } n   cin >> m; n   for (i = 0; i < m; i++) { n     cin >> x >> y; n     mm[x] = max(mm[x], y); n   } n   x = 0; n   for (auto i : mm) x += i.second; n   cout << x; n } n signed main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   long long t = 1, i; n   while (t--) { n     solve(); n   } n } n


#include <bits/stdc++.h> n using namespace std; n struct ji { n   int nex, to; n } edge[100005 << 1]; n set<pair<int, int> > s[100005]; n set<pair<int, int> >::iterator it; n priority_queue<pair<int, int> > q; n pair<int, int> ans[100005]; n int E, n, r, x, y, z, cnt, head[100005], sz[100005], mx[100005], f[100005], n     d[100005]; n long long m; n void add(int x, int y) { n   edge[E].nex = head[x]; n   edge[E].to = y; n   head[x] = E++; n } n void dfs(int k, int fa) { n   sz[k] = 1; n   mx[k] = 0; n   for (int i = head[k]; i != -1; i = edge[i].nex) n     if (edge[i].to != fa) { n       dfs(edge[i].to, k); n       sz[k] += sz[edge[i].to]; n       mx[k] = max(mx[k], sz[edge[i].to]); n     } n   if (max(n - sz[k], mx[k]) <= n / 2) r = k; n } n void dfs(int k, int fa, int sh) { n   f[k] = fa; n   d[k] = sh; n   m -= sh; n   s[x].insert(make_pair(sh, k)); n   sz[k] = 1; n   for (int i = head[k]; i != -1; i = edge[i].nex) n     if (edge[i].to != fa) { n       dfs(edge[i].to, k, sh + 1); n       sz[k] += sz[edge[i].to]; n     } n } n int main() { n   scanf( %d%lld , &n, &m); n   memset(head, -1, sizeof(head)); n   for (int i = 1; i < n; i++) { n     scanf( %d%d , &x, &y); n     add(x, y); n     add(y, x); n   } n   dfs(1, 0); n   for (int i = head[r]; i != -1; i = edge[i].nex) { n     x = edge[i].to; n     dfs(x, r, 1); n     q.push(make_pair(sz[x], x)); n   } n   if ((m > 0) || (m % 2)) { n     printf( NO ); n     return 0; n   } n   m /= -2; n   while (m) { n     x = q.top().second; n     q.pop(); n     if (sz[x] < 2) { n       printf( NO ); n       return 0; n     } n     sz[x] -= 2; n     if (sz[x]) q.push(make_pair(sz[x], x)); n     if ((*--s[x].end()).first - 1 <= m) { n       y = (*--s[x].end()).second; n       s[x].erase(--s[x].end()); n       m -= d[y] - 1; n       z = f[y]; n       bool flag = 0; n       for (int i = head[z]; i != -1; i = edge[i].nex) n         if ((edge[i].to != f[z]) && (edge[i].to != y) && n             (s[x].find(make_pair(d[edge[i].to], edge[i].to)) != s[x].end())) { n           ans[++cnt] = make_pair(edge[i].to, y); n           s[x].erase(make_pair(d[edge[i].to], edge[i].to)); n           flag = 1; n           break; n         } n       if (!flag) { n         ans[++cnt] = make_pair(z, y); n         s[x].erase(make_pair(d[z], z)); n       } n     } else { n       it = lower_bound(s[x].begin(), s[x].end(), make_pair((int)m, 0)); n       while ((*it).first == m) { n         bool flag = 0; n         y = (*it).second; n         for (int i = head[y]; i != -1; i = edge[i].nex) n           if ((edge[i].to != f[y]) && n               (s[x].find(make_pair(d[edge[i].to], edge[i].to)) != s[x].end())) { n             ans[++cnt] = make_pair(y, edge[i].to); n             s[x].erase(make_pair(d[y], y)); n             s[x].erase(make_pair(d[edge[i].to], edge[i].to)); n             flag = 1; n             break; n           } n         if (flag) break; n         it++; n       } n       m = 0; n     } n   } n   while (!q.empty()) { n     x = q.top().second; n     q.pop(); n     if (q.empty()) { n       ans[++cnt] = make_pair((*s[x].begin()).second, r); n       break; n     } n     y = q.top().second; n     q.pop(); n     if (sz[x] > 1) q.push(make_pair(--sz[x], x)); n     if (sz[y] > 1) q.push(make_pair(--sz[y], y)); n     ans[++cnt] = make_pair((*s[x].begin()).second, (*s[y].begin()).second); n     s[x].erase(s[x].begin()); n     s[y].erase(s[y].begin()); n   } n   printf( YES n ); n   for (int i = 1; i <= cnt; i++) printf( %d %d n , ans[i].first, ans[i].second); n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   long long int n; n   cin >> n; n   vector<long long int> v; n   for (long long int i = 0; i < n; i++) { n     long long int x; n     cin >> x; n     v.push_back(x); n   } n   long long int z; n   long long int count = 1; n   for (long long int i = 0;; i++) { n     if (n == count) { n       z = i; n       break; n     } else n       count *= 4; n   } n   sort(v.begin(), v.end()); n   reverse(v.begin(), v.end()); n   long long int prev = 0; n   long long int end = 1; n   vector<long long int> ans; n   while (prev < n) { n     long long int sum = 0; n     for (long long int j = prev; j < end; j++) sum += v[j]; n     ans.push_back(sum); n     sum = 0; n     prev = end; n     end *= 4; n   } n   long long int fin = 0; n   long long int calc = z + 1; n   for (long long int j = 0; j < ans.size(); j++) { n     fin += ans[j] * calc; n     calc--; n   } n   cout << fin; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long gcd(long long a, long long b) { n   while (b) { n     a %= b; n     swap(a, b); n   } n   return a; n } n long long lcm(const long long &a, const long long &b) { n   return (a / gcd(a, b)) * b; n } n bool check(long long a) { n   int k = 0; n   while (a > 0) { n     if (!(a & 1)) { n       k++; n     } n     a >>= 1; n   } n   return k == 1; n } n int bits(long long a) { n   int k = 0; n   if (a == 0) { n     return 1; n   } n   while (a > 0) { n     k++; n     a >>= 1; n   } n   return k; n } n int main() { n   int h, w; n   cin >> h >> w; n   char c; n   vector<vector<int>> a(h + 2, vector<int>(w + 2, 1)); n   for (int i = 1; i <= h; i++) { n     for (int j = 1; j <= w; j++) { n       cin >> c; n       if (c ==  . ) { n         a[i][j] = 0; n       } else n         a[i][j] = 1; n     } n   } n   vector<vector<pair<int, int>>> v(h + 2, vector<pair<int, int>>(w + 2)); n   for (int i = 1; i <= h; i++) { n     for (int j = 1; j <= w; j++) { n       v[i][j].first = v[i][j - 1].first + (a[i][j - 1] + a[i][j] == 0 ? 1 : 0); n     } n   } n   for (int j = 1; j <= w; j++) { n     for (int i = 1; i <= h; i++) { n       v[i][j].second = n           v[i - 1][j].second + (a[i - 1][j] + a[i][j] == 0 ? 1 : 0); n     } n   } n   int q; n   cin >> q; n   int l1, r1, l2, r2; n   int ans = 0, s1, s2; n   for (int i = 0; i < q; i++) { n     cin >> l1 >> r1 >> l2 >> r2; n     ans = 0; n     s1 = 0; n     s2 = 0; n     for (int j = l1; j <= l2; j++) { n       s1 = v[j][r2].first - v[j][r1].first; n       ans += s1; n     } n     for (int j = r1; j <= r2; j++) { n       s2 = v[l2][j].second - v[l1][j].second; n       ans += s2; n     } n     cout << ans << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios::sync_with_stdio(false); n   int n, m; n   cin >> n >> m; n   vector<int> v[n + 1]; n   for (int i = 0; i < m; i++) { n     int a, b; n     cin >> a >> b; n     v[a].push_back(b); n   } n   int cnt[n + 1]; n   for (int o = 1; o <= n; o++) { n     for (int i = 0; i < (int)v[o].size(); i++) { n       if (o < v[o][i]) n         v[o][i] -= o; n       else n         v[o][i] = n - (o - v[o][i]); n     } n     sort(v[o].begin(), v[o].end()); n     if (!v[o].empty()) n       cnt[o] = ((v[o].size() - 1) * n + v[o][0]); n     else n       cnt[o] = 0; n   } n   for (int o = 1; o <= n; o++) { n     int res = cnt[o]; n     int mv = 0; n     for (int i = o + 1;; i++) { n       mv++; n       if (i == n + 1) i = 1; n       if (i == o) break; n       res = max(res, (cnt[i] != 0) * (cnt[i] + mv)); n     } n     cout << res <<    ; n   } n   cout << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void __print(int x) { cerr << x; } n void __print(long x) { cerr << x; } n void __print(long long x) { cerr << x; } n void __print(unsigned x) { cerr << x; } n void __print(unsigned long x) { cerr << x; } n void __print(unsigned long long x) { cerr << x; } n void __print(float x) { cerr << x; } n void __print(double x) { cerr << x; } n void __print(long double x) { cerr << x; } n void __print(char x) { cerr <<      << x <<     ; } n void __print(const char *x) { cerr <<     << x <<    ; } n void __print(const string &x) { cerr <<     << x <<    ; } n void __print(bool x) { cerr << (x ?  true  :  false ); } n template <typename T, typename V> n void __print(const pair<T, V> &x) { n   cerr <<  { ; n   __print(x.first); n   cerr <<  , ; n   __print(x.second); n   cerr <<  } ; n } n template <typename T> n void __print(const T &x) { n   int f = 0; n   cerr <<  { ; n   for (auto &i : x) cerr << (f++ ?  ,  :   ), __print(i); n   cerr <<  } ; n } n void _print() { cerr <<  ] n ; } n template <typename T, typename... V> n void _print(T t, V... v) { n   __print(t); n   if (sizeof...(v)) cerr <<  ,  ; n   _print(v...); n } n struct SegTree { n   long long tree[4 * 200005]; n   long long lazy[4 * 200005]; n   void push(long long node) { n     tree[2 * node + 1] += lazy[node]; n     tree[2 * node + 2] += lazy[node]; n     lazy[2 * node + 1] += lazy[node]; n     lazy[2 * node + 2] += lazy[node]; n     lazy[node] = 0; n   } n   void build(vector<long long> &a, long long node, long long start, n              long long end) { n     if (start == end) { n       tree[node] = a[start]; n       return; n     } n     long long mid = (start + end) >> 1; n     build(a, 2 * node + 1, start, mid); n     build(a, 2 * node + 2, mid + 1, end); n     tree[node] = min(tree[2 * node + 1], tree[2 * node + 2]); n   } n   void rupd(long long node, long long start, long long end, long long l, n             long long r, long long val) { n     if (r < start || l > end) return; n     if (l <= start && end <= r) { n       tree[node] += val; n       lazy[node] += val; n       return; n     } n     push(node); n     long long mid = (start + end) >> 1; n     rupd(2 * node + 1, start, mid, l, r, val); n     rupd(2 * node + 2, mid + 1, end, l, r, val); n     tree[node] = min(tree[2 * node + 1], tree[2 * node + 2]); n   } n   long long rquery(long long node, long long start, long long end, long long l, n                    long long r) { n     if (r < start || l > end) return 1000000000000005; n     if (l <= start && end <= r) return tree[node]; n     push(node); n     long long mid = (start + end) >> 1; n     long long p1 = rquery(2 * node + 1, start, mid, l, r); n     long long p2 = rquery(2 * node + 2, mid + 1, end, l, r); n     return min(p1, p2); n   } n } st; n void solve() { n   long long n; n   cin >> n; n   vector<long long> p(n); n   vector<long long> rp(n); n   for (long long i = 0; i < n; i++) { n     cin >> p[i]; n     p[i]--; n     rp[p[i]] = i; n   } n   vector<long long> a(n); n   for (long long i = 0; i < n; i++) cin >> a[i]; n   vector<long long> b(n); n   b[0] = a[0]; n   for (long long i = 1; i < n; i++) { n     b[i] = b[i - 1] + a[i]; n   } n   st.build(b, 0, 0, n - 1); n   long long ans = st.rquery(0, 0, n - 1, 0, n - 1); n   for (long long i = 0; i < (n); i++) { n     long long pos = rp[i]; n     st.rupd(0, 0, n - 1, pos, n - 1, -a[pos]); n     if (pos) st.rupd(0, 0, n - 1, 0, pos - 1, a[pos]); n     ans = min(ans, st.rquery(0, 0, n - 1, 0, n - 2)); n   } n   cout << ans <<   n ; n } n signed main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   long long tests; n   tests = 1; n   for (long long i = 0; i < tests; i++) { n     solve(); n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 5e4 + 10; n int b[MAXN], n, a[MAXN]; n int ID(int x) { n   x = x % n; n   if (x < 0) x += n; n   return x; n } n int main(void) { n   scanf( %d , &n); n   for (int i = 0; i < n; ++i) scanf( %d , a + i); n   vector<pair<int, int> > foo; n   for (int i = 0; i < n; ++i) foo.push_back(make_pair(a[i] + a[ID(i + 1)], i)); n   sort(foo.begin(), foo.end()); n   for (int i = 0; i < n; ++i) b[foo[i].second] = i; n   printf( %d , b[0]); n   for (int i = 1; i < n; ++i) printf(  %d , b[i]); n   printf(  n ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   cin >> n; n   int ex[n], e[n]; n   int temp = n; n   int i = 0; n   while (temp--) { n     cin >> ex[i] >> e[i]; n     i++; n   } n   int sum = e[0], m = e[0]; n   for (int j = 1; j < n; j++) { n     sum = sum - ex[j] + e[j]; n     if (sum > m) { n       m = sum; n     } n   } n   cout << m << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 300050; n const int L = 19; n const long long inf = 1e15; n vector<int> E[N]; n int par[N][L], dep[N]; n void DFS(int u, int p) { n   dep[u] = dep[p] + 1; n   par[u][0] = p; n   for (int i = 1; i < L; i++) par[u][i] = par[par[u][i - 1]][i - 1]; n   for (int v : E[u]) n     if (v != p) DFS(v, u); n } n int LCA(int u, int v) { n   if (dep[u] < dep[v]) return LCA(v, u); n   for (int i = L - 1; ~i; i--) n     if (dep[par[u][i]] >= dep[v]) u = par[u][i]; n   for (int i = L - 1; ~i; i--) n     if (par[u][i] != par[v][i]) u = par[u][i], v = par[v][i]; n   return u == v ? v : par[v][0]; n } n vector<pair<int, int> > up[N]; n bool ok[N]; n long long dp[N]; n int hi[N]; n set<pair<long long, int> > st[N]; n pair<long long, int> operator+(pair<long long, int> a, long long b) { n   a.first += b; n   return a; n } n long long st_add[N], ans; n void Solve(int u, int p) { n   int nok = 0; n   long long sum = 0; n   hi[u] = dep[u]; n   for (int v : E[u]) n     if (v != p) n       Solve(v, u), nok += !ok[v], sum += ok[v] ? dp[v] : 0, n           hi[u] = min(hi[u], hi[v]); n   if (nok) { n     ok[u] = 0; n   } else { n     for (int v : E[u]) n       if (v != p) { n         if (st[u].size() < st[v].size()) n           st[u].swap(st[v]), swap(st_add[u], st_add[v]); n         for (auto p : st[v]) st[u].insert(p + (st_add[v] - st_add[u])); n       } n     dp[u] = sum; n     for (auto p : up[u]) n       st[u].insert(make_pair(p.second, dep[p.first]) + (-st_add[u])); n     if (hi[u] == dep[u]) { n       while (st[u].size() && st[u].begin()->second >= dep[u]) n         st[u].erase(st[u].begin()); n       if (st[u].empty()) n         ok[u] = 0; n       else { n         ok[u] = 1; n         dp[u] += st[u].begin()->first + st_add[u]; n         hi[u] = st[u].begin()->second; n         st_add[u] -= st[u].begin()->first + st_add[u]; n         st[u].erase(st[u].begin()); n       } n     } else n       ok[u] = 1; n   } n   if (u == 1) ans = nok == 0 ? sum : -1; n } n int main() { n   int n, m, i, u, v, c; n   scanf( %i %i , &n, &m); n   for (i = 1; i < n; i++) n     scanf( %i %i , &u, &v), E[u].push_back(v), E[v].push_back(u); n   DFS(1, 0); n   for (i = 1; i <= m; i++) n     scanf( %i %i %i , &u, &v, &c), up[u].push_back(make_pair(LCA(u, v), c)); n   Solve(1, 0); n   printf( %lld n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const double eps = 1e-9; n const int MAXB = 10; n const int MAXN = 1025; n vector<int> distGroup[MAXN]; n int hasDist[MAXN]; n int predecessor[MAXN]; n vector<int> output; n set<int> centers; n string result, resultm; n int maxDist[MAXN]; n int minDist[MAXN]; n vector<string> strangeresults; n int main() { n   int N; n   cin >> N; n   for (int i = (0); i < (N); i++) minDist[i] = 1; n   for (int i = (0); i < (N); i++) maxDist[i] = (1 << MAXB) - 1; n   for (int i = (0); i < (N); i++) hasDist[i] = -1; n   for (int i = (0); i < (N); i++) distGroup[i].clear(); n   distGroup[0].push_back(0); n   maxDist[0] = minDist[0] = hasDist[0] = 0; n   int largest = 0; n   for (int p = int(MAXB) - 1; p >= (0); p--) { n     for (int s = (0); s < (2); s++) { n       output = vector<int>(N, 0); n       centers.clear(); n       for (int i = (s << (p + 1)); true; i += (4 << p)) { n         if (i > N || int((distGroup[i]).size()) == 0) break; n         for (auto n : distGroup[i]) { n           assert(output[n] == 0); n           output[n] = (1 << p); n         } n         centers.insert(i); n       } n       if (int((centers).size()) > 0) { n         cout <<  ? ; n         for (auto v : output) cout <<     << min(N - 1, v); n         cout << endl << flush; n         cin >> result; n         cout <<  ? ; n         for (auto v : output) cout <<     << max(0, min(N - 1, v - 1)); n         cout << endl << flush; n         cin >> resultm; n         while (int((result).size()) != N || int((resultm).size()) != N) { n           cerr <<  VERY STRANGE!! got    << result <<    and    << resultm n                <<    as results  << endl n                << flush; n           strangeresults.push_back(result); n           strangeresults.push_back(resultm); n           if (int((strangeresults).size()) == (N - 1) * 2) { n             cout <<  !  << endl; n             for (int i = (0); i < (N - 1); i++) n               cout << strangeresults[i * 2] <<     << strangeresults[i * 2 + 1] n                    << endl; n             return 0; n           } n           cout <<  ? ; n           for (auto v : output) cout <<     << min(N - 1, v); n           cout << endl << flush; n           cin >> result; n           cout <<  ? ; n           for (auto v : output) cout <<     << max(0, min(N - 1, v - 1)); n           cout << endl << flush; n           cin >> resultm; n         } n         for (int i = (0); i < (N); i++) { n           if (minDist[i] == maxDist[i]) continue; n           if (centers.find(minDist[i] - 1) != centers.end()) { n             if (result[i] ==  1 ) { n               if (resultm[i] ==  1 ) { n                 maxDist[i] -= (1 << p); n               } else { n                 minDist[i] = maxDist[i] = minDist[i] - 1 + (1 << p); n               } n             } else n               minDist[i] += (1 << p); n           } n           if (minDist[i] == maxDist[i]) { n             hasDist[i] = minDist[i]; n             largest = max(largest, minDist[i]); n             distGroup[minDist[i]].push_back(i); n           } n         } n       } n     } n   } n   for (int i = (0); i < (N); i++) predecessor[i] = -1; n   for (int i = (0); i < (N); i++) n     assert(minDist[i] == maxDist[i] && maxDist[i] == hasDist[i]); n   for (int a = (0); a < (2); a++) { n     vector<string> results; n     for (int b = (0); b < (MAXB); b++) { n       for (int xo = (0); xo < (2); xo++) { n         cout <<  ? ; n         for (int n = (0); n < (N); n++) { n           if (((hasDist[n] >> 1) ^ a) & 1) n             cout <<     << (((n >> b) ^ xo) & 1); n           else n             cout <<   0 ; n         } n         cout << endl << flush; n         string tmp; n         cin >> tmp; n         results.push_back(tmp); n       } n     } n     for (int d = (0); d < (largest); d++) { n       if (((d >> 1) ^ a ^ 1) & 1) continue; n       for (auto n1 : distGroup[d]) n         for (auto n2 : distGroup[d + 1]) { n           bool ok = true; n           for (int b = (0); b < (MAXB); b++) { n             if (results[b * 2 + (1 ^ ((n1 >> b) & 1))][n2] ==  0 ) ok = false; n           } n           if (ok) { n             if (predecessor[n2] != -1) { n               cerr <<  prev predecessor[  << n2 <<  ]=  << predecessor[n2] n                    << endl; n               cerr << predecessor[n2] << endl; n             } n             assert(predecessor[n2] == -1); n             predecessor[n2] = n1; n           } n         } n     } n   } n   assert(predecessor[0] == -1); n   for (int i = (1); i < (N); i++) assert(predecessor[i] != -1); n   cout <<  !  << endl << flush; n   for (int i = (1); i < (N); i++) n     cout << predecessor[i] + 1 <<     << i + 1 << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long ans = 0; n string s; n int main() { n   ios_base::sync_with_stdio(false); n   cin >> s; n   int id = -1; n   for (int i = 0; i < (int)s.length(); i++) { n     for (int j = 1; j <= 4 && i - 2 * j >= 0; j++) n       if (s[i] == s[i - j] && s[i - j] == s[i - 2 * j]) id = max(id, i - 2 * j); n     ans += id + 1; n   } n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   char ch; n   int state = 0; n   while ((ch = cin.get()) != EOF) switch (state) { n       case 0: n         if (isdigit(ch)) { n           cout << ch; n           state = 1; n         } n         if (ch ==  , ) { n           cout << ch; n           state = 2; n         } n         if (ch ==    ) { n           state = 0; n         } n         if (ch ==  . ) { n           cout << ch; n           state = 3; n         } n         break; n       case 1: n         if (isdigit(ch)) { n           cout << ch; n           state = 1; n         } n         if (ch ==  , ) { n           cout << ch; n           state = 2; n         } n         if (ch ==    ) { n           state = 6; n         } n         if (ch ==  . ) { n           cout <<    ; n           cout << ch; n           state = 3; n         } n         break; n       case 2: n         if (isdigit(ch)) { n           cout <<    ; n           cout << ch; n           state = 1; n         } n         if (ch ==  , ) { n           cout <<    ; n           cout << ch; n           state = 2; n         } n         if (ch ==    ) { n           state = 2; n         } n         if (ch ==  . ) { n           cout <<    ; n           cout << ch; n           state = 3; n         } n         break; n       case 3: n         if (ch ==  . ) { n           cout << ch; n           state = 4; n         } n         break; n       case 4: n         if (ch ==  . ) { n           cout << ch; n           state = 5; n         } n         break; n       case 5: n         if (isdigit(ch)) { n           cout << ch; n           state = 1; n         } n         if (ch ==  , ) { n           cout << ch; n           state = 2; n         } n         if (ch ==    ) { n           state = 5; n         } n         if (ch ==  . ) { n           cout <<    ; n           cout << ch; n           state = 3; n         } n         break; n       case 6: n         if (isdigit(ch)) { n           cout <<    ; n           cout << ch; n           state = 1; n         } n         if (ch ==  , ) { n           cout << ch; n           state = 2; n         } n         if (ch ==    ) { n           state = 6; n         } n         if (ch ==  . ) { n           cout <<    ; n           cout << ch; n           state = 3; n         } n         break; n     } n exit: n   return (0); n } n


#include <bits/stdc++.h> n using namespace std; n const int INF = 1e9; n const int MN = 105; n bool mark[500][500]; n char s[MN]; n int dis[500][500]; n int ask(pair<int, int> now) { n   int res = INF; n   if (mark[now.first][now.second]) res = min(res, dis[now.first][now.second]); n   if (mark[now.first - 1][now.second]) n     res = min(res, dis[now.first - 1][now.second] + 1); n   if (mark[now.first + 1][now.second]) n     res = min(res, dis[now.first + 1][now.second] + 1); n   if (mark[now.first][now.second - 1]) n     res = min(res, dis[now.first][now.second - 1] + 1); n   if (mark[now.first][now.second + 1]) n     res = min(res, dis[now.first][now.second + 1] + 1); n   return res; n } n int main() { n   scanf( %s , s); n   int l = strlen(s); n   pair<int, int> now = make_pair(105, 105); n   mark[105][105] = 1; n   dis[105][105] = 0; n   int len = 0; n   for (int i = 0; i < l; ++i) { n     ++len; n     if (s[i] ==  U ) now.second += 1; n     if (s[i] ==  D ) now.second -= 1; n     if (s[i] ==  L ) now.first -= 1; n     if (s[i] ==  R ) now.first += 1; n     if (ask(now) < len) { n       printf( BUG n ); n       return 0; n     } n     mark[now.first][now.second] = 1; n     dis[now.first][now.second] = len; n   } n   printf( OK n ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 5e3 + 5; n const int inf = 1e9 + 7; n vector<pair<int, int> > adj[MAXN]; n int dp[MAXN][MAXN], tr[MAXN][MAXN]; n int n, m, t; n int solve(int u, int len) { n   if (u == n and !len) return 0; n   if (!len) return inf; n   if (dp[u][len] != -1) return dp[u][len]; n   int ret = inf; n   for (int i = 0; i < ((int)adj[u].size()); i++) { n     int v = adj[u][i].first; n     int c = adj[u][i].second; n     long long x = solve(v, len - 1) + c; n     if (x < ret) ret = x, tr[u][len] = v; n   } n   return dp[u][len] = ret; n } n void process() { n   memset(dp, -1, sizeof(dp)); n   for (int i = 5000; i >= 0; i--) { n     int ans = solve(1, i); n     if (ans <= t) { n       printf( %d n%d  , i + 1, 1); n       int x = 1; n       while (i--) { n         x = tr[x][i + 1]; n         printf( %d  , x); n       } n     } n   } n } n int main(void) { n   scanf( %d%d%d , &n, &m, &t); n   for (int u, v, c, i = 1; i <= m; i++) { n     scanf( %d%d%d , &u, &v, &c); n     adj[u].push_back(make_pair(v, c)); n   } n   process(); n } n


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


#include <bits/stdc++.h> n using namespace std; n int main() { n   unsigned long long N, x, y, input, r_code = 0; n   cin >> N >> x >> y; n   vector<unsigned long long> days; n   for (unsigned long long i = 0; i < N; i++) { n     cin >> input; n     days.push_back(input); n   } n   unsigned long long temp1; n   for (unsigned long long i = 0; i < N; i++) { n     r_code = 0; n     if (i < x) { n       temp1 = 0; n     } else n       temp1 = x; n     for (unsigned long long j = 1; j <= temp1; j++) { n       if (days[i - j] <= days[i]) { n         r_code = 1; n         break; n       } n     } n     if (r_code == 0) { n       unsigned long long temp; n       if (i + y >= N) { n         temp = N - 1; n       } else n         temp = i + y; n       for (unsigned long long j = i + 1; j <= temp; j++) { n         if (days[j] <= days[i]) { n           r_code = 1; n           break; n         } n       } n     } n     if (r_code == 0) { n       cout << (i + 1); n       break; n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n int lim = 1e6; n int inf = 1e8; n void err(istream_iterator<string> it) { cerr << endl; } n template <typename T, typename... Args> n void err(istream_iterator<string> it, T a, Args... args) { n   cerr << *it <<   =   << a <<   t ; n   err(++it, args...); n } n template <typename T1, typename T2> n ostream& operator<<(ostream& c, pair<T1, T2>& v) { n   c <<  (  << v.first <<  ,  << v.second <<  ) ; n   return c; n } n template <template <class...> class TT, class... T> n ostream& operator<<(ostream& out, TT<T...>& c) { n   out <<  {  ; n   for (auto& x : c) out << x <<    ; n   out <<  } ; n   return out; n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   int t; n   cin >> t; n   while (t--) { n     int n; n     cin >> n; n     int ans = 0; n     for (int i = 6; i >= 0; i--) { n       int k = pow(2, i); n       int j = 0; n       vector<int> a, b; n       for (int r = 1; r <= n; r++) { n         if ((r / k) % 2 == 0) { n           a.push_back(r); n         } else { n           b.push_back(r); n         } n       } n       if (a.size() == 0 or b.size() == 0) continue; n       cout << a.size() <<     << b.size() <<    ; n       for (int j = 0; j < a.size(); j++) { n         cout << a[j] <<    ; n       } n       for (int j = 0; j < b.size(); j++) { n         cout << b[j] <<    ; n       } n       cout << endl; n       int tmpans; n       cin >> tmpans; n       ans = max(ans, tmpans); n     } n     cout << -1 <<     << ans << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n void solve() { n   string s; n   cin >> s; n   map<char, long long> mp; n   for (long long i = 0; i < s.size(); i++) mp[s[i]]++; n   long long c = 0; n   for (auto it = mp.begin(); it != mp.end(); it++) { n     if (it->second & 1) c++; n   } n   if (c <= 1) n     cout <<  First ; n   else { n     if (c % 2 == 0) n       cout <<  Second ; n     else n       cout <<  First ; n   } n } n signed main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 300001; n set<pair<long long int, long long int> >* T[N]; n int A[N]; n int S[N]; n char s[N]; n vector<int> adj[N]; n long long int MOD1 = 1000000007LL; n long long int MOD2 = 1000000009LL; n void dfs(int st, int p = -1, long long int hash1 = 0LL, n          long long int hash2 = 0LL) { n   hash1 = (hash1 * 257LL + s[st] -  a  + 1) % MOD1; n   hash2 = (hash2 * 43LL + s[st] -  a  + 1) % MOD2; n   S[st] = 1; n   int mx = -1; n   for (auto c : adj[st]) { n     if (c != p) { n       dfs(c, st, hash1, hash2); n       S[st] += S[c]; n       if (mx == -1 || S[mx] < S[c]) mx = c; n     } n   } n   set<pair<long long int, long long int> >* root; n   if (mx == -1) n     root = new set<pair<long long int, long long int> >; n   else { n     root = T[mx]; n     for (auto c : adj[st]) { n       if (c != p && c != mx) root->insert(T[c]->begin(), T[c]->end()); n     } n   } n   root->insert({hash1, hash2}); n   T[st] = root; n   A[st] += T[st]->size(); n } n int main() { n   int n; n   scanf( %d , &n); n   for (int i = 1; i <= n; i++) { n     scanf( %d , A + i); n   } n   scanf( %s , s + 1); n   for (int i = 1; i <= n - 1; i++) { n     int u, v; n     scanf( %d%d , &u, &v); n     adj[u].push_back(v); n     adj[v].push_back(u); n   } n   dfs(1); n   int mxc = 0, mx = 1; n   for (int i = 1; i <= n; i++) { n     if (mx == A[i]) n       mxc++; n     else if (mx < A[i]) n       mx = A[i], mxc = 1; n   } n   printf( %d n%d n , mx, mxc); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long int solve(string &s, vector<vector<long long int> > &pre, n                     vector<vector<long long int> > &dp, long long int st, n                     long long int en, long long int a, long long int b) { n   if (st > en) { n     return 0; n   } n   if (dp[st][en] != -1) { n     return dp[st][en]; n   } n   long long int val = solve(s, pre, dp, st + 1, en, a, b) + a; n   for (long long int i = st; i < pre[st][0] + st; i++) { n     val = min(val, solve(s, pre, dp, i + 1, en, a, b) + b); n   } n   dp[st][en] = val; n   return val; n } n void getZarr(string str, int Z[]); n long long int get_len(string &text, string &pattern) { n   string concat = pattern +  $  + text; n   int l = concat.length(); n   int Z[l]; n   getZarr(concat, Z); n   long long int ans = 0; n   for (int i = pattern.length() + 1; i < l; ++i) { n     ans = max(ans, (long long int)Z[i]); n   } n   return ans; n } n void getZarr(string str, int Z[]) { n   int n = str.length(); n   int L, R, k; n   L = R = 0; n   for (int i = 1; i < n; ++i) { n     if (i > R) { n       L = R = i; n       while (R < n && str[R - L] == str[R]) R++; n       Z[i] = R - L; n       R--; n     } else { n       k = i - L; n       if (Z[k] < R - i + 1) n         Z[i] = Z[k]; n       else { n         L = i; n         while (R < n && str[R - L] == str[R]) R++; n         Z[i] = R - L; n         R--; n       } n     } n   } n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   long long int n, a, b; n   cin >> n >> a >> b; n   string s; n   cin >> s; n   vector<vector<long long int> > pre(n + 1, vector<long long int>(1, -1)), n       dp(n + 1, vector<long long int>(n + 1, -1)); n   string s1 =   ; n   for (long long int l = 1; l < n; l++) { n     long long int min1 = min(l, n - l); n     s1 += s[l - 1]; n     string s2 =   ; n     for (long long int idx = l; idx < (l + min1); idx++) { n       s2 += s[idx]; n     } n     long long int len = get_len(s1, s2); n     pre[l][0] = len; n   } n   cout << solve(s, pre, dp, 0, n - 1, a, b) << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long MAXN = 1e5 + 1; n const long long INF = 1e9 + 1; n const long long MOD = 1e9; n const long long BITS = 64; n const long double EPS = 1e-12; n void bye() { n   cout <<   n ; n   exit(0); n } n signed main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   int n, k; n   cin >> n >> k; n   string s; n   cin >> s; n   vector<int> cnt(k); n   for (int i = 0; i < (long long)(s).size(); i++) { n     cnt[s[i] -  A ]++; n   } n   cout << *min_element((cnt).begin(), cnt.end()) * k; n   bye(); n } n


#include <bits/stdc++.h> n using namespace std; n const int nmax = 110; n int x[nmax]; n char g[nmax][nmax]; n int pos[nmax * nmax][2]; n int main() { n   int a, b, c, d; n   scanf( %d%d%d%d , &a, &b, &c, &d); n   int q; n   scanf( %d , &q); n   for (int i = 0; i < q; ++i) { n     scanf( %d , &x[i]); n   } n   bool swapped = false; n   if (b < d) { n     swap(b, d); n     swap(a, c); n     swapped = true; n   } n   int n = b; n   int m = a + c; n   memset(g,  . , sizeof(g)); n   int k = 0; n   for (int j = 0; j < m; ++j) { n     if (((j ^ a) & 1) == 0) { n       for (int i = 0; i < (j < a ? b : d); ++i) { n         pos[k][0] = i; n         pos[k][1] = j; n         ++k; n       } n     } else { n       for (int i = (j < a ? b : d) - 1; i >= 0; --i) { n         pos[k][0] = i; n         pos[k][1] = j; n         ++k; n       } n     } n   } n   int cnt = 0; n   for (int i = 0; i < q; ++i) { n     for (int j = 0; j < x[i]; ++j) { n       g[pos[cnt][0]][pos[cnt][1]] =  a  + (char)i; n       ++cnt; n     } n   } n   if (swapped) { n     for (int i = 0; i < n; ++i) { n       reverse(g[i], g[i] + m); n     } n   } n   puts( YES ); n   for (int i = 0; i < n; ++i) { n     g[i][m] = 0; n     puts(g[i]); n   } n   return 0; n } n


#include <bits/stdc++.h> n const int INF = 0x3f3f3f3f; n const long long int INFLL = 0x3f3f3f3f3f3f3f3fll; n long long int getint() { n   long long int ret = 0; n   bool f = 0; n   char c; n   while ((c = getchar()) <  0  || c >  9 ) n     if (c ==  - ) f ^= 1; n   while (c >=  0  && c <=  9 ) { n     ret = ret * 10 + c -  0 ; n     c = getchar(); n   } n   return f ? -ret : ret; n } n using namespace std; n const int MAXN = 100; n long long int f[MAXN + 10][2]; n char s[MAXN + 10]; n int main() { n   scanf( %s , s + 1); n   int n = strlen(s + 1); n   f[1][0] = 1; n   for (int i = 2; i <= n; ++i) { n     if (s[i - 1] ==  A ) f[i][0] += f[i - 1][0]; n     if (s[i - 1] ==  B  && s[i] ==  A ) f[i][1] += f[i - 1][0]; n     if (s[i] ==  B ) f[i][1] += f[i - 1][1]; n     f[i][0] += f[i - 1][1]; n   } n   long long int ans = f[n][1]; n   if (s[n] ==  A ) ans += f[n][0]; n   memset(f, 0, sizeof f); n   f[1][1] = 1; n   for (int i = 2; i <= n; ++i) { n     if (s[i - 1] ==  A ) f[i][0] += f[i - 1][0]; n     if (s[i - 1] ==  B  && s[i] ==  A ) f[i][1] += f[i - 1][0]; n     if (s[i] ==  B ) f[i][1] += f[i - 1][1]; n     f[i][0] += f[i - 1][1]; n   } n   if (s[1] ==  B ) n     ans += f[n][1]; n   else if (s[n] ==  B ) n     ans += f[n][0]; n   printf( %I64d n , ans); n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 109; n const int INF = 1e9; n char zbs[5] = { R ,  G ,  B ,  Y ,  W }; n pair<int, int> a[N]; n int main() { n   int n, ans = 11; n   cin >> n; n   for (int i = 0; i < int(n); ++i) { n     char t1, t2; n     cin >> t1 >> t2; n     a[i].second = int(t2 -  1 ); n     for (int j = 0; j < int(5); ++j) n       if (zbs[j] == t1) a[i].first = j; n   } n   for (int mask1 = 0; mask1 < int(1 << 5); ++mask1) { n     for (int mask2 = 0; mask2 < int(1 << 5); ++mask2) { n       int m[5][5], na = 0, ok = 1; n       memset(m, 0, sizeof(m)); n       for (int i = 0; i < int(5); ++i) { n         if (mask1 & (1 << i)) ++na; n         if (mask2 & (1 << i)) ++na; n       } n       for (int i = 0; i < int(n); ++i) m[a[i].first][a[i].second] = 1; n       for (int i1 = 0; i1 < int(5); ++i1) n         for (int j1 = 0; j1 < int(5); ++j1) n           for (int i2 = 0; i2 < int(5); ++i2) n             for (int j2 = 0; j2 < int(5); ++j2) { n               bool ok1 = 0; n               if (m[i1][j1] * m[i2][j2] == 0) continue; n               if (i1 == i2 && j1 == j2) continue; n               if (i1 != i2 && ((mask1 & (1 << i1)) || (mask1 & (1 << i2)))) n                 ok1 = 1; n               if (j1 != j2 && ((mask2 & (1 << j1)) || (mask2 & (1 << j2)))) n                 ok1 = 1; n               if (ok) ok = ok1; n             } n       if (ok) ans = min(na, ans); n     } n   } n   cout << ans; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m; n vector<int> g[100001]; n bool is_centroid[100001]; n int centroid_parent[100001]; n int mn[100001]; n int sz[100001]; n int level[100001]; n int centroid_root; n map<pair<int, int>, int> dist; n void dfs(int nd, int p, int c) { n   sz[nd] = 1; n   if (p == c) { n     level[nd] = 0; n   } else { n     level[nd] = level[p] + 1; n   } n   dist[make_pair(nd, c)] = level[nd] + 1; n   for (int i = 0; i < g[nd].size(); i++) { n     int z = g[nd][i]; n     if (z != p && !is_centroid[z]) { n       dfs(z, nd, c); n       sz[nd] += sz[z]; n     } n   } n } n int pd; n int find_centroid(int u, int ts) { n   for (int i = 0; i < g[u].size(); i++) { n     int z = g[u][i]; n     if (is_centroid[z] || sz[z] > sz[u]) continue; n     if (sz[z] > ts / 2) return find_centroid(z, ts); n   } n   return u; n } n int get_centroid(int nd, int p) { n   dfs(nd, p, p); n   int u = find_centroid(nd, sz[nd]); n   is_centroid[u] = true; n   centroid_parent[u] = p; n   for (int i = 0; i < g[u].size(); i++) { n     int z = g[u][i]; n     if (!is_centroid[z]) { n       get_centroid(z, u); n     } n   } n   return u; n } n void centroid_decompose() { n   fill(is_centroid, is_centroid + 100001, false); n   centroid_root = get_centroid(1, -1); n } n void debug() { n   for (int i = 1; i <= n; i++) { n     cout << i <<     << centroid_parent[i] <<     << mn[i] << endl; n   } n } n void redify(int u) { n   int x = u; n   while (u != -1) { n     mn[u] = min(dist[make_pair(x, u)], mn[u]); n     u = centroid_parent[u]; n   } n } n int solve(int u) { n   int res = INT_MAX / 3; n   int x = u; n   while (u != -1) { n     res = min(res, dist[make_pair(x, u)] + mn[u]); n     u = centroid_parent[u]; n   } n   return res; n } n int main() { n   int u, v; n   scanf( %d %d , &n, &m); n   for (int i = 0; i < n - 1; i++) { n     scanf( %d %d , &u, &v); n     g[u].push_back(v); n     g[v].push_back(u); n   } n   fill(mn, mn + 100001, INT_MAX / 3); n   centroid_decompose(); n   redify(1); n   while (m--) { n     scanf( %d %d , &u, &v); n     if (u == 1) { n       redify(v); n     } else { n       printf( %d n , solve(v)); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 100010; n vector<int> v[5 * maxn]; n int t[5 * maxn]; n int comp[5 * maxn]; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   int n = 0, m = 0, x = 0, y = 0; n   cin >> n >> m; n   for (int i = 0; i < m; ++i) { n     cin >> x >> y; n     --x; n     --y; n     v[x].push_back(y); n     v[y].push_back(x); n   } n   vector<int> aux; n   for (int i = 0; i < n; ++i) { n     cin >> t[i]; n     comp[i] = 1; n     if (t[i] == comp[i]) aux.push_back(i); n   } n   bool flag = false; n   for (int i = 0; i < (int)aux.size(); ++i) { n     int node = aux[i]; n     if (comp[node] != t[node]) flag = true; n     for (auto j : v[node]) { n       if (comp[j] == t[node]) { n         ++comp[j]; n         if (comp[j] == t[j]) aux.push_back(j); n       } n     } n   } n   if ((int)aux.size() < n) flag = true; n   if (flag) n     cout <<  -1 ; n   else { n     for (auto i : aux) { n       cout << (i + 1) <<    ; n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 3005, MAXM = 55; n const double inf = 1e9 + 5; n vector<int> graph[MAXN]; n double c[MAXN][MAXN]; n int f[MAXN][MAXN]; n priority_queue<pair<double, int> > q; n double dist[MAXN]; n int last[MAXN]; n int n, Q; n int upperBound[MAXM], lowerBound[MAXM]; n int arrayNode[MAXM]; n int dispenserNode[MAXM][MAXM]; n int startNode[MAXM], endNode[MAXM]; n int S = 3001, T = 3002; n void addEdge(int u, int v, int flow, double cost) { n   graph[u].push_back(v); n   graph[v].push_back(u); n   f[u][v] = flow; n   c[u][v] = cost; n   c[v][u] = cost; n } n double Dijkstra(int s, int t) { n   for (int i = 0; i < MAXN; i++) { n     dist[i] = inf; n   } n   dist[s] = 0; n   int u; n   q.push({0, s}); n   while (q.empty() == false) { n     u = q.top().second; n     q.pop(); n     for (int i = 0; i < graph[u].size(); i++) { n       if (f[u][graph[u][i]] != 0 && n           dist[graph[u][i]] > dist[u] + c[u][graph[u][i]]) { n         dist[graph[u][i]] = dist[u] + c[u][graph[u][i]]; n         last[graph[u][i]] = u; n         q.push({-dist[graph[u][i]], graph[u][i]}); n       } n     } n   } n   return dist[t]; n } n pair<int, double> minCostMaxFlow(int s, int t) { n   int maxFlow = 0; n   double minCost = 0; n   while (true) { n     double cost = Dijkstra(s, t); n     if (cost == inf) break; n     int u = t; n     int flow = inf; n     while (u != s) { n       flow = min(flow, f[last[u]][u]); n       u = last[u]; n     } n     u = t; n     while (u != s) { n       f[last[u]][u] -= flow; n       f[u][last[u]] += flow; n       u = last[u]; n     } n     maxFlow += flow; n     minCost += cost * flow; n   } n   return {maxFlow, minCost}; n } n int main() { n   ios::sync_with_stdio(false); n   cin.tie(NULL); n   cin >> n >> Q; n   for (int i = 1; i <= n; i++) { n     lowerBound[i] = 1; n     upperBound[i] = n; n   } n   while (Q--) { n     int type; n     int l, r, v; n     cin >> type; n     cin >> l >> r >> v; n     if (type == 1) { n       for (int i = l; i <= r; i++) { n         lowerBound[i] = max(lowerBound[i], v); n       } n     } else { n       for (int i = l; i <= r; i++) { n         upperBound[i] = min(upperBound[i], v); n       } n     } n   } n   for (int i = 1; i <= n; i++) { n     if (lowerBound[i] > upperBound[i]) { n       cout <<  -1  <<   n ; n       return 0; n     } n   } n   int node = 1; n   for (int i = 1; i <= n; i++) { n     startNode[i] = node; n     node++; n   } n   for (int i = 1; i <= n; i++) { n     endNode[i] = node; n     node++; n   } n   for (int i = 1; i <= n; i++) { n     arrayNode[i] = node; n     node++; n   } n   for (int i = 1; i <= n; i++) { n     for (int j = 1; j <= n; j++) { n       dispenserNode[i][j] = node; n       node++; n     } n   } n   for (int i = 1; i <= n; i++) { n     addEdge(S, startNode[i], n, 0); n   } n   for (int i = 1; i <= n; i++) { n     int value = 1, sum = 0; n     for (int j = 1; j <= n; j++) { n       addEdge(startNode[i], dispenserNode[i][j], 1, j * j - sum); n       addEdge(dispenserNode[i][j], endNode[i], 1, 0); n       sum += j * j - sum; n     } n   } n   for (int i = 1; i <= n; i++) { n     for (int j = 1; j <= n; j++) { n       if (i >= lowerBound[j] && i <= upperBound[j]) { n         addEdge(endNode[i], arrayNode[j], 1, 0); n       } n     } n   } n   for (int i = 1; i <= n; i++) { n     addEdge(arrayNode[i], T, 1, 0); n   } n   cout << minCostMaxFlow(S, T).second <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n int dp[513]; n int n, m; n int main() { n   scanf( %d %d , &n, &m); n   for (int i = 0; i < n; ++i) { n     scanf( %d , &dp[i + 1]); n   } n   double best = 0; n   for (int i = 0; i < m; ++i) { n     int x, y, z; n     scanf( %d %d %d , &x, &y, &z); n     best = max(best, ((double)dp[x] + dp[y]) / z); n   } n   printf( %.9lf , best); n   return 0; n } n