#include <bits/stdc++.h> n using namespace std; n const int inf = 1e9 + 7; n vector<vector<pair<string, int>>> adl; n vector<int> dist(2000, inf); n map<pair<string, int>, int> nums; n map<string, pair<int, int>> mins; n void bfs(string& poly, int version) { n   vector<string> order; n   order.push_back(poly); n   mins[poly] = {0, version}; n   dist[1] = 0; n   for (int i = 0; i < order.size(); i++) { n     int u = nums[{order[i], mins[order[i]].second}]; n     for (auto j : adl[u]) { n       int v = nums[j], ver = j.second; n       string& s = j.first; n       if (dist[v] > dist[u] + 1) { n         dist[v] = dist[u] + 1; n         order.push_back(s); n         if (mins.find(s) == mins.end()) mins[s] = {dist[v], ver}; n         if (dist[v] < mins[s].first) { n           mins[s] = {dist[v], ver}; n         } else if (dist[v] == mins[s].first) { n           mins[s].second = max(mins[s].second, ver); n         } n       } n     } n   } n } n int main() { n   int n; n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   cin >> n; n   adl.resize(2 * n); n   int m = 1, v = -1; n   string poly; n   for (int i = 0; i < n; i++) { n     int num, ind, vers; n     string s, s1; n     cin >> s >> num; n     if (i == 0) { n       poly = s; n       v = num; n     } n     if (nums[{s, num}] == 0) { n       nums[{s, num}] = m; n       m++; n     } n     ind = nums[{s, num}]; n     int k; n     cin >> k; n     for (int j = 0; j < k; j++) { n       cin >> s1 >> vers; n       if (s == s1) continue; n       adl[ind].push_back({s1, vers}); n     } n   } n   bfs(poly, v); n   cout << (int)mins.size() - 1 <<   n ; n   for (auto p : mins) n     if (p.first != poly) cout << p.first <<     << p.second.second <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n using nagai = long long; n const int N = 100; n int matr[N][N]; n const int K = 12; n int de[N][K]; n const int oo = 0x3f3f3f3f; n mt19937 rnd(chrono::high_resolution_clock::now().time_since_epoch().count()); n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   int n, k; n   cin >> n >> k; n   for (int i = 0; i < n; ++i) n     for (int j = 0; j < n; ++j) cin >> matr[i][j]; n   int ans = oo; n   for (int i = 0; i < 30000; ++i) { n     memset(de, 0x3f, sizeof de); n     vector<int> col(n); n     for (int i = 0; i < n; ++i) col[i] = rnd() % 2; n     de[0][0] = 0; n     for (int l = 0; l < k; ++l) n       for (int a = 0; a < n; ++a) n         for (int b = 0; b < n; ++b) n           if (col[a] != col[b]) n             de[b][l + 1] = min(de[b][l + 1], de[a][l] + matr[a][b]); n     ans = min(ans, de[0][k]); n   } n   cout << ans <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n const int mod = 1000000007, maxn = 200005, maxk = 105; n int n, m; n int a[maxn], fac[maxn], nfac[maxn], inv[maxn], sum[maxn][maxk]; n inline int C(int a, int b) { n   return 1ll * fac[a] * nfac[b] % mod * nfac[a - b] % mod; n } n int main() { n   fac[0] = fac[1] = nfac[0] = nfac[1] = inv[1] = 1; n   for (int i = 2; i <= 200000; i++) n     fac[i] = 1ll * fac[i - 1] * i % mod, n     inv[i] = mod - 1ll * (mod / i) * inv[mod % i] % mod, n     nfac[i] = 1ll * nfac[i - 1] * inv[i] % mod; n   scanf( %d%d , &n, &m); n   for (int i = 1; i <= n; i++) scanf( %d , &a[i]); n   for (int i = 1; i <= m; i++) { n     int l, r, k; n     scanf( %d%d%d , &l, &r, &k); n     for (int j = 0; j <= k; j++) { n       sum[l][j] = (sum[l][j] + C(k, k - j)) % mod; n       sum[r + 1][j] = (sum[r + 1][j] - C(k + r - l + 1, k - j) + mod) % mod; n     } n   } n   for (int i = 1; i <= n; i++) n     for (int j = 100; j >= 0; j--) n       sum[i][j] = (sum[i][j] + (sum[i - 1][j + 1] + sum[i - 1][j]) % mod) % mod; n   for (int i = 1; i <= n; i++) n     printf( %d%c , (a[i] + sum[i][0]) % mod, i == n ?   n  :    ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long N = 500010; n long long n, sx, sy, x, y, res[6]; n int main() { n   scanf( %lld , &n); n   scanf( %lld%lld , &sx, &sy); n   for (int i = 0; i < n; i++) { n     scanf( %lld%lld , &x, &y); n     if (x < sx) res[1]++; n     if (y < sy) res[2]++; n     if (x > sx) res[3]++; n     if (y > sy) res[4]++; n   } n   long long mx = 0; n   for (int i = 1; i <= 4; i++) mx = max(mx, res[i]); n   for (int i = 1; i <= 4; i++) { n     if (mx == res[i]) { n       long long a, b; n       if (i == 1) { n         a = sx - 1, b = sy; n       } else if (i == 2) { n         a = sx, b = sy - 1; n       } else if (i == 3) { n         a = sx + 1, b = sy; n       } else n         a = sx, b = sy + 1; n       printf( %lld n , mx); n       printf( %lld %lld , a, b); n       return 0; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <class T> n bool uin(T &a, T b) { n   return (a > b ? a = b, true : false); n } n template <class T> n bool uax(T &a, T b) { n   return (a < b ? a = b, true : false); n } n int const X = 8; n int const L = 26; n int const N = 41; n long long const INF = 3e18 + 41; n int x, k, n, q; n int c[N]; n int p[N], w[N]; n long long d[(1 << X)][(1 << X)][L]; n long long ans; n bool bit(int m, int i) { return (m & (1 << i)); } n int xorbit(int m, int i) { return (m ^ (1 << i)); } n int qbit(int m) { n   int res = 0; n   while (m) { n     res += (m & 1); n     m /= 2; n   } n   return res; n } n int dx[(1 << X)][(1 << X)]; n unordered_map<int, int> a; n vector<pair<int, long long>> e[(1 << X)][L]; n void build() { n   for (int i = 0; i < (1 << X); ++i) n     for (int j = 0; j < (1 << X); ++j) n       for (int o = 0; o < L; ++o) d[i][j][o] = INF; n   memset(dx, 255, sizeof(dx)); n   for (int i = 0; i < (1 << k); ++i) { n     if (!bit(i, 0)) { n       int m0 = i; n       int m1 = (m0 >> 1); n       uin(d[m0][m1][0], 0ll); n     } else { n       int m0 = i; n       for (int j = 1; j < k + 1; ++j) { n         if (bit(m0, j)) continue; n         int m1 = xorbit(m0, j); n         m1 = xorbit(m1, 0); n         m1 >>= 1; n         if (uin(d[m0][m1][0], (long long)c[j - 1])) dx[m0][m1] = j; n       } n     } n   } n   for (int o = 1; o < L; ++o) { n     for (int t = 0; t < (1 << k); ++t) { n       if (qbit(t) != x) continue; n       for (int i = 0; i < (1 << k); ++i) { n         if (qbit(i) != x) continue; n         for (int j = 0; j < (1 << k); ++j) { n           if (qbit(j) != x) continue; n           uin(d[i][j][o], d[i][t][o - 1] + d[t][j][o - 1]); n         } n       } n     } n   } n   for (int o = 0; o < L; ++o) { n     for (int i = 0; i < (1 << X); ++i) n       for (int j = 0; j < (1 << X); ++j) n         if (d[i][j][o] < INF) { n           if (qbit(i) != x) continue; n           e[i][o].push_back(make_pair(j, d[i][j][o])); n         } n   } n } n long long f[(1 << X)], nf[(1 << X)]; n void solve() { n   int p1 = 0; n   int x0 = 0; n   for (int i = 0; i < (1 << X); ++i) f[i] = INF; n   f[(1 << x) - 1] = 0; n   while (x0 < n - x) { n     while (p1 < q && x0 > p[p1]) ++p1; n     int s; n     for (int i = L - 1; i >= 0; --i) { n       s = i; n       if (x0 + (1 << i) + k - 1 > n - 1) continue; n       if (p1 != q && x0 + (1 << i) + k - 1 >= p[p1]) continue; n       break; n     } n     for (int i = 0; i < (1 << k); ++i) nf[i] = INF; n     for (int i = 0; i < (1 << k); ++i) { n       if (f[i] == INF) continue; n       for (pair<int, long long> p : e[i][s]) { n         long long v = f[i] + p.second; n         if (s == 0 && dx[i][p.first] != -1 && n             a.find(x0 + dx[i][p.first]) != a.end()) { n           v += a[x0 + dx[i][p.first]]; n         } n         uin(nf[p.first], v); n       } n     } n     memcpy(f, nf, sizeof(f)); n     x0 += (1 << s); n   } n   ans = f[(1 << x) - 1]; n } n int main() { n   scanf( %d %d %d %d , &x, &k, &n, &q); n   for (int i = 0; i < k; ++i) scanf( %d , &c[i]); n   for (int i = 0; i < q; ++i) { n     scanf( %d %d , &p[i], &w[i]); n     --p[i]; n     a[p[i]] = w[i]; n   } n   for (int i = 0; i < q; ++i) n     for (int j = i + 1; j < q; ++j) n       if (p[j] < p[i]) { n         swap(p[j], p[i]); n         swap(w[j], w[i]); n       } n   build(); n   solve(); n   printf( %I64d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int const N = 1e5 + 2; n int n, m; n void solve() { n   string n, m; n   cin >> n >> m; n   sort((n).begin(), (n).end()); n   if (n[0] ==  0  && n.size() > 1) swap(n[0], n[1]); n   if (n == m) n     cout <<  OK  << endl; n   else n     cout <<  WRONG_ANSWER  << endl; n } n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   solve(); n } n


#include <bits/stdc++.h> n using namespace std; n map<int, long long> ans, re, now; n int n, h[200005], tot, mp[200005], siz[200005], mz[200005], rem[200005], cnt; n bool vis[200005]; n struct DX { n   int v, p; n } dx[400005]; n int gcd(int a, int b) { return b ? gcd(b, a % b) : a; } n void add(int u, int v) { n   dx[tot].p = h[u]; n   dx[tot].v = v; n   h[u] = tot++; n } n void dfs_G(int u, int fx) { n   siz[u] = 1; n   rem[++cnt] = u; n   mz[u] = 0; n   for (int i = h[u]; ~i; i = dx[i].p) { n     int v = dx[i].v; n     if (v == fx || vis[v]) continue; n     dfs_G(v, u); n     siz[u] += siz[v]; n     mz[u] = max(mz[u], siz[v]); n   } n } n int fd_G(int u) { n   cnt = 0; n   dfs_G(u, u); n   for (int i = 1; i <= cnt; i++) n     if (mz[rem[i]] <= siz[u] / 2 && siz[u] - siz[rem[i]] <= siz[u] / 2) n       return rem[i]; n } n void dfs(int u, int fx, int gc) { n   re[gc = gcd(gc, mp[u])]++; n   for (int i = h[u]; ~i; i = dx[i].p) { n     int v = dx[i].v; n     if (v == fx || vis[v]) continue; n     dfs(v, u, gc); n   } n } n void sol(int u) { n   u = fd_G(u); n   vis[u] = 1; n   now[mp[u]] = 1; n   ans[mp[u]] += 1; n   for (int i = h[u]; ~i; i = dx[i].p) { n     int v = dx[i].v; n     if (vis[v]) continue; n     dfs(v, 0, mp[u]); n     for (map<int, long long>::iterator it = now.begin(); it != now.end(); it++) n       for (map<int, long long>::iterator ti = re.begin(); ti != re.end(); ti++) n         ans[gcd(it->first, ti->first)] += it->second * ti->second; n     for (map<int, long long>::iterator ti = re.begin(); ti != re.end(); ti++) n       now[ti->first] += ti->second; n     re.clear(); n   } n   now.clear(); n   for (int i = h[u]; ~i; i = dx[i].p) { n     int v = dx[i].v; n     if (vis[v]) continue; n     sol(v); n   } n } n int main() { n   memset(h, -1, sizeof(h)); n   scanf( %d , &n); n   for (int i = 1; i <= n; i++) scanf( %d , &mp[i]); n   int a, b; n   for (int i = 1; i < n; i++) { n     scanf( %d%d , &a, &b); n     add(a, b); n     add(b, a); n   } n   sol(1); n   for (map<int, long long>::iterator it = ans.begin(); it != ans.end(); it++) n     printf( %d %lld n , it->first, it->second); n   return 0; n } n


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


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 150000 + 50; n int n, m, h, vec[MAXN], sec[MAXN], ans, need[4 * MAXN], ex[4 * MAXN]; n void plant(int v, int b, int e) { n   need[v] = e - b; n   if (e - b == 1) return; n   int mid = (b + e) / 2; n   plant(v << 1, b, mid); n   plant(v << 1 ^ 1, mid, e); n } n void water(int v, int b, int e, int x, int val) { n   if (x == e) return; n   if (e - b == 1) { n     need[v] -= val; n     ex[v] = max(0, -need[v]); n     return; n   } n   int mid = (b + e) / 2; n   if (x < mid) n     water(v << 1, b, mid, x, val); n   else n     water(v << 1 ^ 1, mid, e, x, val); n   need[v] = max(0, need[v << 1]) + max(0, need[v << 1 ^ 1] - ex[v << 1]); n   ex[v] = ex[v << 1 ^ 1] + max(0, ex[v << 1] - max(0, need[v << 1 ^ 1])); n } n int main() { n   scanf( %d %d %d , &n, &m, &h); n   for (int i = 0; i < m; i++) scanf( %d , &sec[i]); n   sort(sec, sec + m); n   plant(1, 0, m); n   for (int i = 0; i < n; i++) { n     if (i >= m) n       water(1, 0, m, lower_bound(sec, sec + m, h - vec[i - m]) - sec, -1); n     scanf( %d , &vec[i]); n     water(1, 0, m, lower_bound(sec, sec + m, h - vec[i]) - sec, 1); n     ans += bool(need[1] == 0); n   } n   printf( %d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, k; n char s[200]; n char p[200]; n char ans[200]; n bool used[200][30]; n bool brute(int pos) { n   if (pos == n) { n     puts(ans); n     exit(0); n   } n   if (ans[pos] != 0) { n     if (pos >= strlen(s) - 1) { n       if (p[pos - strlen(s) + 1] ==  0 ) { n         bool ok = true; n         for (int j = strlen(s) - 1; j >= 0; j--) { n           if (ans[pos - j] != s[strlen(s) - 1 - j]) { n             ok = false; n             break; n           } n         } n         if (ok) return false; n       } n     } n     return brute(pos + 1); n   } n   memset(used[pos], 0, sizeof used[pos]); n   int cnt = k; n   bool need_check = false; n   if (pos >= strlen(s) - 1) { n     need_check = true; n     for (int i = strlen(s) - 1; i > 0; i--) { n       if (ans[pos - i] != s[strlen(s) - 1 - i]) { n         need_check = false; n         break; n       } n     } n   } n   for (int t = 0; t < k; t++) { n     ans[pos] = (char)( a  + t); n     if (!need_check) { n       if (brute(pos + 1)) return true; n     } else { n       if (t != s[strlen(s) - 1] -  a ) { n         if (brute(pos + 1)) return true; n       } n     } n   } n   ans[pos] = 0; n   return false; n } n int main() { n   scanf( %d %d n , &n, &k); n   gets(s); n   gets(p); n   memset(ans, 0, sizeof ans); n   for (int i = 0; i < strlen(p); i++) { n     if (p[i] ==  1 ) { n       for (int j = 0; j < strlen(s); j++) { n         if (ans[i + j] == 0 || ans[i + j] == s[j]) { n           ans[i + j] = s[j]; n         } else { n           puts( No solution ); n           return 0; n         } n       } n     } n   } n   for (int i = 0; i < strlen(p); i++) { n     if (p[i] ==  0 ) { n       bool ok = true; n       for (int j = 0; j < strlen(s); j++) { n         if (ans[i + j] != s[j]) { n           ok = false; n           break; n         } n       } n       if (ok) { n         puts( No solution ); n         return 0; n       } n     } n   } n   for (int i = 0; i < k; i++) { n     bool ok = true; n     for (int j = 0; j < strlen(s); j++) { n       if (s[j] -  a  == i) { n         ok = false; n         break; n       } n     } n     if (ok) { n       for (int j = 0; j < n; j++) { n         if (ans[j] == 0) ans[j] = (char)( a  + i); n       } n       puts(ans); n       return 0; n     } n   } n   brute(0); n   puts( No solution ); n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, k; n   cin >> n >> k; n   if (k > n) { n     cout <<  -1 ; n     return 0; n   } n   if (n == 1) { n     cout <<  a ; n     return 0; n   } n   if (n > 1 && k == 1) { n     cout <<  -1 ; n     return 0; n   } n   string str; n   char c1 =  a , c2 =  b ; n   for (int i = 0; i < n; i++) { n     str += c1; n     if (i % 2 == 0) n       c1 =  b ; n     else n       c1 =  a ; n   } n   char ch =  a  + k - 1; n   int l = str.size() - 1; n   for (int i = 0; i < k - 2; i++) str[l--] = ch--; n   cout << str; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m, dfsCont, dfs_num[2005], dfs_low[2005], numComp; n vector<vector<int> > lista; n string ans[2005]; n vector<int> pila, comp; n map<string, int> names; n bool vis[2005]; n void tarjan(int u) { n   dfs_low[u] = dfs_num[u] = dfsCont++; n   vis[u] = 1; n   pila.push_back(u); n   for (int i = 0; i < lista[u].size(); i++) { n     int v = lista[u][i]; n     if (dfs_num[v] == -1) tarjan(v); n     if (vis[v]) dfs_low[u] = min(dfs_low[u], dfs_low[v]); n   } n   if (dfs_low[u] == dfs_num[u]) { n     while (true) { n       int v = pila.back(); n       comp.push_back(v); n       pila.pop_back(); n       vis[v] = 0; n       if (u == v) break; n     } n     numComp++; n   } n } n int main() { n   scanf( %d , &n); n   lista.assign(n, vector<int>()); n   for (int i = 0; i < n; i++) n     for (int j = 0; j < n; j++) { n       int x; n       scanf( %d , &x); n       if (x) lista[i].push_back(j); n     } n   pila.clear(); n   memset(dfs_low, 0, sizeof dfs_low); n   memset(dfs_num, -1, sizeof dfs_num); n   memset(vis, 0, sizeof vis); n   dfsCont = 0; n   numComp = 0; n   tarjan(0); n   if (numComp == 1 && comp.size() == n) n     printf( YES n ); n   else n     printf( NO n ); n } n


#include <bits/stdc++.h> n using namespace std; n template <typename X> n void MA(X &a, X b) { n   a = max(a, b); n } n template <typename X> n void MI(X &a, X b) { n   a = min(a, b); n } n int DEBUG = 0, MULTI = 0, GCJ = 0; n struct solver_t; n solver_t *solver; n struct solver_t { n   vector<int> A; n   vector<vector<int> > EDG; n   struct lcu_t { n     int N; n     vector<int> CNT, PAR; n     lcu_t(int N) : N(N) { n       PAR.resize(N); n       CNT.resize(N); n       for (int64_t i = (0); i < (N); ++i) PAR[i] = i; n       for (int64_t i = (0); i < (N); ++i) CNT[i] = 1; n     } n     int parent(int i) { n       if (PAR[i] == i) n         return i; n       else n         return PAR[i] = parent(PAR[i]); n     } n     int64_t get_cnt(int64_t x) { return x * (x - 1) / 2; } n     int64_t join(int a, int b) { n       int pa = parent(a), pb = parent(b); n       if (pa == pb) return 0; n       if (!(CNT[pb] >= CNT[pa])) swap(pa, pb); n       int64_t diff = -get_cnt(CNT[pa]) - get_cnt(CNT[pb]); n       PAR[pa] = pb; n       CNT[pb] += CNT[pa]; n       diff += get_cnt(CNT[pb]); n       return diff; n     } n   }; n   void solve() { n     int N; n     cin >> N; n     A.resize(N + 1); n     for (int64_t i = (1); i <= (N); ++i) cin >> A[i]; n     EDG.resize(N + 1); n     for (int64_t i = (0); i < (N - 1); ++i) { n       int X, Y; n       cin >> X >> Y; n       EDG[X].push_back(Y); n       EDG[Y].push_back(X); n     } n     vector<array<int64_t, 2> > order; n     for (int64_t i = (1); i <= (N); ++i) order.push_back({A[i], i}); n     sort((order).begin(), (order).end()); n     int64_t res[2] = {}; n     for (int64_t mode = (0); mode < (2); ++mode) { n       lcu_t lcu(N + 1); n       int64_t prev_cnt = 0; n       int64_t &sum = res[mode]; n       vector<int> vis(N + 1); n       for (int i = 0; i < (int)order.size();) { n         int64_t diff = 0; n         int j; n         { n           if (DEBUG) n             cerr << 81 <<     n                  <<      n                  <<     n                  <<    << endl; n         }; n         for (j = i; j < (int)order.size(); j++) { n           if (order[i][0] != order[j][0]) break; n           int u = order[j][1]; n           vis[u] = 1; n           { n             if (DEBUG) n               cerr << 86 <<     n                    <<   vis   << u  n                    <<     n                    <<  vis   << u << endl; n           }; n           for (int v : EDG[u]) n             if (vis[v]) { n               diff += lcu.join(u, v); n               { n                 if (DEBUG) n                   cerr << 90 <<     n                        <<  u <<     << v <<     << diff  n                        <<     << u <<     << v <<     << diff << endl; n               }; n             } n         } n         { n           if (DEBUG) n             cerr << 93 <<     n                  <<  order[i][0] <<     << diff  n                  <<     << order[i][0] <<     << diff << endl; n         }; n         sum += order[i][0] * diff; n         prev_cnt += diff; n         i = j; n       } n       reverse((order).begin(), (order).end()); n       { n         if (DEBUG) n           cerr << 99 <<     n                <<  sum  n                <<     << sum << endl; n       }; n       { n         if (DEBUG) n           cerr << 100 <<     n                <<   !!   n                <<     n                <<  !!  << endl; n       }; n     } n     cout << abs(res[0] - res[1]) <<   n ; n   } n   void gen() {} n   void brute() {} n }; n int main(int argc, char **argv) { n   for (int64_t i = (1); i <= (argc - 1); ++i) n     for (int j = 0; argv[i][j]; j++) n       if (argv[i][j] ==  . ) freopen(argv[i],  r , stdin); n   for (int64_t i = (1); i <= (argc - 1); ++i) n     if (argv[i] == string( q )) DEBUG = 1 << 30; n   for (int64_t i = (1); i <= (argc - 1); ++i) n     if (argv[i] == string( gen )) { n       (solver = new solver_t())->gen(); n       exit(0); n     } n   for (int64_t i = (1); i <= (argc - 1); ++i) n     if (argv[i] == string( brute )) { n       (solver = new solver_t())->brute(); n       exit(0); n     } n   ios::sync_with_stdio(false), cin.tie(0); n   cout.setf(ios::fixed), cout.precision(10); n   int t; n   if (MULTI || GCJ) n     cin >> t; n   else n     t = 1; n   for (int64_t i = (1); i <= (t); ++i) { n     if (DEBUG) cerr << 129 <<     << i << endl; n     if (GCJ) cout <<  Case #  << i <<  :  ; n     solver = new solver_t(); n     solver->solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int INF = 1e9; n const long long INFLL = 1e18 + 1; n const int MAX = 100001; n const long long MOD = 1000000007; n const int SZ = 300100; n const double PI = 3.14159265358979323846264338327; n long long inq(long long k, long long q, long long mod) { n   if (q == 0) return 1; n   long long l = inq(k, q / 2, mod); n   if (q % 2 == 0) n     return l * l % mod; n   else n     return l * l % mod * k % mod; n } n long long gcd(long long a, long long b) { n   if (a < b) swap(a, b); n   if (b == 0) return a; n   return gcd(b, a % b); n } n long long cubr(long long a) { n   long long l = -1, r = 1e6 + 2; n   while (l < r - 1) { n     long long mid = (l + r) / 2; n     if (mid * mid * mid > a) n       r = mid; n     else n       l = mid; n   } n   return l; n } n long long max(long long a, long long b) { n   if (a > b) return a; n   return b; n } n long long min(long long a, long long b) { return -1 * max(-a, -b); } n long long possible(long long q) { n   if (q == INF) return -1; n   return q; n } n bool correct(int x, int xx) { n   if (x < 0) return 0; n   if (x >= xx) return 0; n   return 1; n } n long long dsumm(long long x, long long k) { n   long long y = 1; n   long long z = 0; n   for (int i = 0; y < 1e18; i += 1) { n     z += x / y % k; n     y *= k; n   } n   return z; n } n long long dcount(long long x) { n   long long y = 1; n   long long z = 0; n   int c[100]; n   for (int i = 0; i < 10; i += 1) c[i] = 0; n   for (int i = 0; x > 0; i += 1) { n     if (c[x / y % 10] == 0) z++; n     c[x / y % 10] = 1; n     x /= 10; n   } n   return z; n } n long long lg10(long long x) { n   if (10000 <= x && x < 100000) return 5; n   if (x == 0) return 0; n   return lg10(x / 10) + 1; n } n long long g(long long x, long long mod) { n   if (x == 0) return 0; n   return x / mod + g(x / mod, mod); n } n bool is_digit(char c) { return ( 0  <= c && c <=  9 ); } n string s; n int n, p; n int a[50][50]; n int b[3000000]; n int marks[(1 << 17)][20][20]; n int r[3000000]; n int dp[3000000]; n int len[3000000]; n int cnt[50]; n void mark(int mask, int i, int j) { n   if (marks[mask][i][j] == 0) return; n   marks[mask][i][j] = 0; n   for (int k = 0; k < p; k++) { n     if (((1 << k) & mask) == 0 && k != i && k != j) { n       int m2 = (mask + (1 << k)); n       if (marks[m2][i][j] == 1) mark(m2, i, j); n     } n   } n } n signed main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   cin >> n >> p; n   cin >> s; n   for (int(i) = 0; (i) != (n); i += 1) b[i] = s[i] -  a ; n   for (int(i) = 0; (i) != (n); i += 1) cnt[b[i]]++; n   for (int i = 0; i < p; i++) { n     for (int j = 0; j < p; j++) { n       cin >> a[i][j]; n     } n   } n   for (int i = 0; i < (1 << p); i++) { n     r[i] = 1; n     for (int j = 0; j < p; j++) { n       for (int k = 0; k < p; k++) { n         marks[i][j][k] = 1; n       } n     } n   } n   for (int i = 0; i < p; i++) { n     for (int j = 0; j < p; j++) { n       if (a[i][j] == 0) { n         long long calc = 0; n         long long mask = 0; n         for (int k = 0; k < n; k++) { n           if (b[k] == j) { n             if (calc == 1) { n               mark(mask, i, j); n             } n             calc = 0; n             mask = 0; n           } n           if (b[k] == i) { n             calc = 1; n             mask = 0; n           } n           if (b[k] != i && b[k] != j && calc == 1) mask |= (1 << b[k]); n         } n       } n     } n   } n   for (int i = 0; i < (1 << p); i++) { n     r[i] = 1; n     for (int j = 0; j < p; j++) { n       for (int k = 0; k < p; k++) { n         r[i] = min(r[i], marks[i][j][k]); n       } n     } n   } n   dp[0] = 1; n   int ans = n; n   for (int i = 1; i < (1 << p); i++) { n     for (int j = 0; j < p; j++) { n       if (i & (1 << j)) { n         int ii = i - (1 << j); n         if (dp[ii] && r[i]) { n           dp[i] = 1; n           int first = n; n           for (int k = 0; k < p; k++) { n             if ((1 << k) & i) { n               first -= cnt[k]; n             } n           } n           ans = min(first, ans); n         } n       } n     } n   } n   cout << ans; n } n


#include <bits/stdc++.h> n using namespace std; n void solve() { n   int n, m; n   cin >> n >> m; n   vector<string> a(n); n   for (int i = 0; i < n; i++) { n     cin >> a[i]; n   } n   int ans = 1 << 30; n   for (int k = 2; k < 100; k++) { n     int nn = (n + k - 1) / k * k; n     int nm = (m + k - 1) / k * k; n     vector<string> b = a; n     b.resize(nn,  0 ); n     for (int i = 0; i < nn; i++) { n       b[i].resize(nm,  0 ); n     } n     int cur = 0; n     for (int i = 0; i + k <= nn; i += k) { n       for (int j = 0; j + k <= nm; j += k) { n         int ca = 0; n         int cb = 0; n         for (int w = 0; w < k; w++) { n           for (int h = 0; h < k; h++) { n             ca += (b[i + w][j + h] ==  0  ? 0 : 1); n             cb += (b[i + w][j + h] ==  1  ? 0 : 1); n           } n         }; n         cur += min(ca, cb); n       } n     }; n     ans = min(ans, cur); n   } n   cout << ans <<   n ; n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   solve(); n   return 0; n } n


#include<bits/stdc++.h> n #define int long long n using namespace std; n int32_t main() n { n ios::sync_with_stdio(0); n cin.tie(0); n int t; n cin>>t; n while(t--) n     {int n,k; n      cin>>n>>k; n      int ok=1; n      int poss=0; n      for(int i=0;i<n-1;i++) n         {ok=ok*2; n          if(ok>=k){poss=1;break;} n         } n     if(ok<k){cout<< -1 n ;continue;} n     vector<int> v; n     k--; n     for(int i=0;i<n-1;i++) n         {//Should I take i+1 n          int poss=0; n          int z=1; n          for(int j=0;j<n-i-2;j++) n             {z=z*2; n              if(z>k){break;} n             } n         if(z>k) n             {v.push_back(i+1); n             } n         else n             {k-=z; n             } n         } n     v.push_back(n); n     int visited[n]; n     memset(visited,0,sizeof(visited)); n     for(auto x:v) n         { n             visited[x-1]=1; n             cout<<x<<   ; n             for(int i=x-1;i>0;i--) n                 { n                     if(visited[i-1]==1){break;} n                     cout<<i<<   ; n                 } n         } n     cout<<  n ; n     } n } n  n


#include <bits/stdc++.h> n using namespace std; n vector<int> g[100005]; n int ans = 0; n int csize[100005]; n int dfs(int u, int fa) { n   csize[u] = 1; n   for (int i = 0; i < g[u].size(); i++) { n     int v = g[u][i]; n     if (v == fa) continue; n     dfs(v, u); n     csize[u] += csize[v]; n     if (csize[v] % 2 == 0) ans++; n   } n } n int main() { n   int n; n   while (cin >> n) { n     ans = 0; n     for (int i = 0; i <= n; i++) { n       g[i].clear(); n     } n     for (int i = 0; i < n - 1; i++) { n       int x, y; n       cin >> x >> y; n       g[x].push_back(y); n       g[y].push_back(x); n     } n     if (n % 2) n       cout <<  -1 n ; n     else { n       dfs(1, 0); n       cout << ans <<   n ; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m, s[105][105]; n int main() { n   int i, j, x, y; n   scanf( %d , &n); n   for (i = 3; i <= 102; i++) { n     m = (i * (i - 1) * (i - 2)) / 6; n     if (m > n) break; n   } n   i--; n   memset(s, 0, sizeof(s)); n   for (y = 1; y <= i; y++) n     for (x = 1; x <= i; x++) n       if (y != x) s[y][x] = 1; n   m = i * (i - 1) * (i - 2) / 6; n   while (n != m) { n     for (j = i; j >= 2; j--) n       if (m + j * (j - 1) / 2 <= n) break; n     m += j * (j - 1) / 2; n     i++; n     for (x = 1; x <= j; x++) s[x][i] = s[i][x] = 1; n   } n   printf( %d n , i); n   for (y = 1; y <= i; y++) { n     for (x = 1; x <= i; x++) printf( %d , s[y][x]); n     printf(  n ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e6 + 11; n const long long MOD = 1e18; n long long a[N], b[N]; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   long long x, y, l, r; n   cin >> x >> y >> l >> r; n   vector<long long> v; n   long long k = 1, kk = 1; n   for (int i = 0; i <= 60; i++) { n     a[i] = k; n     b[i] = kk; n     if (k != -1 && r / k >= x && k * x <= r) n       k *= x; n     else n       k = -1; n     if (kk != -1 && r / kk >= y && kk * y <= r) n       kk *= y; n     else n       kk = -1; n   } n   for (int i = 0; i <= 60; i++) n     for (int j = 0; j <= 60; j++) n       if (a[i] != -1 && b[j] != -1) { n         long long dd = a[i] + b[j]; n         if (dd >= l && dd <= r) v.push_back(dd); n       } n   v.push_back(l - 1); n   v.push_back(r + 1); n   sort(v.begin(), v.end()); n   long long ans = 0; n   for (int i = 1; i < v.size(); i++) ans = max(ans, v[i] - v[i - 1] - 1); n   cout << ans << endl; n } n


#include <bits/stdc++.h> n int main(void) { n   int b_cost, money, b_number; n   scanf( %d %d %d , &b_cost, &money, &b_number); n   int total_cost; n   int loan = 0; n   total_cost = b_cost; n   for (int i = 2; i <= b_number; i++) { n     total_cost += i * b_cost; n   } n   if (money < total_cost) { n     loan = total_cost - money; n   } n   printf( %d , loan); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 2e5 + 10; n const long long mod = 1e9 + 7; n struct node { n   int s, e, next; n } edge[maxn]; n int head[maxn], len; n void add(int s, int e) { n   edge[len].e = e; n   edge[len].next = head[s]; n   head[s] = len++; n } n void init() { n   memset(head, -1, sizeof(head)); n   len = 0; n } n double dp[maxn]; n void dfs(int x, int fa) { n   dp[x] = 0; n   int ans = 0; n   for (int i = head[x]; i != -1; i = edge[i].next) { n     int y = edge[i].e; n     if (y == fa) continue; n     dfs(y, x); n     dp[x] += dp[y]; n     ans++; n   } n   if (ans == 0) n     dp[x] = 0; n   else n     dp[x] = dp[x] / ans + 1; n } n int main() { n   int n; n   while (scanf( %d , &n) != EOF) { n     init(); n     int x, y; 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, -1); n     printf( %.8f n , dp[1]); n   } n } n


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


#include <bits/stdc++.h> n using namespace std; n long long a[200100], sum[200100], qs[200100]; n bool bad(pair<long long, long long> &a, pair<long long, long long> &b, n          pair<long long, long long> second) { n   return (a.second - second.second) * (b.first - a.first) > n          (a.second - b.second) * (second.first - a.first); n } n long long at(pair<long long, long long> a, long long x) { n   return a.first * x + a.second; n } n int back = 0; n pair<long long, long long> l[200100]; n void add(long long first, long long second) { n   if (back >= 2 and bad(l[back - 2], l[back - 1], {first, second})) back--; n   l[back++] = {first, second}; n } n long long query(long long x, int L = 0, int R = back - 1) { n   if (L + 1 == R) return max(at(l[L], x), at(l[R], x)); n   if (L == R) return at(l[L], x); n   int mid = (L + R) / 2; n   if (at(l[mid], x) < at(l[mid - 1], x)) return query(x, L, mid - 1); n   if (at(l[mid], x) < at(l[mid + 1], x)) return query(x, mid + 1, R); n   return at(l[mid], x); n } n int main() { n   int n; n   scanf( %d , &n); n   for (int i = 1; i <= n; i++) n     scanf( %lld , &a[i]), qs[i] = a[i] + qs[i - 1], n                           sum[i] = a[i] * i + sum[i - 1]; n   long long ans = 0; n   add(0, 0); n   for (int i = 1; i <= n; i++) { n     ans = max(ans, sum[i] + query(qs[i])); n     add(-i, -sum[i] + i * qs[i]); n   } n   printf( %lld , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int m, n, nums[1010][35], P[1010] = {0}, nCr[1010][1010], basis[1010][35], n                           sz = 0, pivots[1010]; n char buf[1010]; n int main() { n   int mask = 0x1F, shift = 5; n   scanf( %d%d , &m, &n); n   for (int i = 0; i < n; i++) { n     scanf( %s , buf); n     for (int j = 0; j < m; j++) { n       nums[i][j >> shift] |= (buf[j] ==  1 ) << (j & mask); n     } n   } n   nCr[0][0] = 1; n   for (int i = 1; i <= m; i++) { n     nCr[i][0] = nCr[i][i] = 1; n     for (int j = 1; j < i; j++) { n       nCr[i][j] = (nCr[i - 1][j - 1] + nCr[i - 1][j]) % 1000000007; n     } n   } n   P[0] = 1; n   for (int i = 1; i <= m; i++) { n     for (int j = 1; j <= i; j++) { n       P[i] = (P[i] + 1LL * nCr[i - 1][j - 1] * P[i - j]) % 1000000007; n     } n   } n   for (int i = 0; i < m >> shift; i++) nums[n][i] = 0xFFFFFFFF; n   nums[n][m >> shift] = (1 << (m & mask)) - 1; n   for (int i = 0; i < m; i++) pivots[i] = -1; n   for (int i = 0; i < m; i++) { n     int idx = 0; n     while (idx <= n && ((nums[idx][i >> shift] >> (i & mask)) & 1) == 0) { n       idx++; n     } n     if (idx == n + 1) continue; n     for (int i = 0; i < 35; i++) { n       basis[sz][i] = nums[idx][i]; n     } n     for (int j = idx; j <= n; j++) { n       if ((nums[j][i >> shift] >> (i & mask)) & 1) { n         for (int i = 0; i < 35; i++) { n           nums[j][i] ^= basis[sz][i]; n         } n       } n     } n     pivots[i] = sz; n     sz++; n   } n   for (int i = 0; i < m; i++) { n     if (pivots[i] != -1) { n       int u = pivots[i]; n       for (int j = 0; j < sz; j++) { n         if (j != u && ((basis[j][i >> shift] >> (i & mask)) & 1)) n           for (int z = 0; z < 35; z++) { n             basis[j][z] ^= basis[u][z]; n           } n       } n     } n   } n   for (int i = 0; i < m; i++) { n     if (pivots[i] == -1) { n       int u = -1, v = -1; n       for (int j = 0; j < sz; j++) { n         if ((basis[j][i >> shift] >> (i & mask)) & 1) { n           v = u; n           u = j; n         } n       } n       if (v != -1) { n         for (int z = 0; z < 35; z++) { n           basis[sz][z] = basis[u][z] & basis[v][z]; n         } n         for (int j = i + 1; j < m; j++) { n           if (pivots[j] != -1 && ((basis[sz][j >> shift] >> (j & mask)) & 1)) { n             int u = pivots[j]; n             for (int k = 0; k < 35; k++) basis[sz][k] ^= basis[u][k]; n           } n         } n         for (int j = 0; j < sz; j++) { n           if (basis[j][i >> shift] >> (i & mask) & 1) { n             for (int k = 0; k < 35; k++) { n               basis[j][k] ^= basis[sz][k]; n             } n           } n         } n         pivots[i] = sz; n         sz++; n       } n     } n   } n   int ans = 1, num_ones = 0; n   for (int i = 0; i < sz; i++) { n     int no = 0; n     for (int j = 0; j < m; j++) { n       no += (basis[i][j >> shift] >> (j & mask)) & 1; n     } n     num_ones += no; n     ans = 1LL * ans * P[no] % 1000000007; n   } n   ans = 1LL * ans * P[m - num_ones] % 1000000007; n   printf( %d n , ans); n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int a, b, t = 0, c = 0; n   cin >> a >> b; n   t += a; n   while (b <= a) { n     c = a % b; n     a /= b; n     t += a; n     a += c; n   } n   cout << t << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int INF = 1000000007; n const long long INFLL = 1000000000000000000LL; n const int MAXN = 200009; n int m, x; n vector<int> weights; n stack<int> order; n void print() { n   if (order.empty()) return; n   int x = order.top(); n   order.pop(); n   print(); n   cout << x <<    ; n } n void solve(int bal, int step) { n   if (step == m) { n     cout <<  YES  << endl; n     print(); n     exit(0); n   } n   if (abs(bal) >= 10) return; n   for (int w : weights) { n     if (step % 2 == 0) { n       if (bal + w > 0 && (order.empty() || w != order.top())) { n         order.push(w); n         solve(bal + w, step + 1); n         order.pop(); n       } n     } else { n       if (bal - w < 0 && (order.empty() || w != order.top())) { n         order.push(w); n         solve(bal - w, step + 1); n         order.pop(); n       } n     } n   } n } n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   for (int i = 1; i <= 10; i++) { n     scanf( %1d , &x); n     if (x == 1) weights.push_back(i); n   } n   scanf( %d , &m); n   solve(0, 0); n   cout <<  NO  << endl; n } n


#include <bits/stdc++.h> n int N, head[51], next[99], from[99], to[99], len[99], E, root, M, q[51], fa[51], n     count_e[99], f[51][99][51], vi[51][99]; n void BFS() { n   int H = 0, T = 1, u; n   q[1] = root; n   while (H < T) n     for (int e = head[u = q[++H]]; e; e = next[e]) n       if (to[e] != fa[u]) fa[q[++T] = to[e]] = u; n } n void F(int n, int e) { n   if (vi[n][e]) return; n   vi[n][e] = 1; n   f[n][e][0] = 1 << 30; n   if (!next[head[to[e]]]) { n     for (int i = 1; i < n; i++) { n       F(n - i, ((e - 1) ^ 1) + 1); n       f[n][e][i] = f[n - i][((e - 1) ^ 1) + 1][n - i] + len[e]; n     } n     f[n][e][n] = 0; n   } else { n     for (int g = head[to[e]]; g; g = next[g]) n       if (to[g] != from[e]) F(n, g); n     for (int i = 1; i <= n; i++) { n       int l = 0, r = (1 << 30) - 1; n       while (l < r) { n         int m = l + r + 1 >> 1; n         int count = 0; n         for (int g = head[to[e]]; g; g = next[g]) n           if (to[g] != from[e]) n             count += std::upper_bound(f[n][g], f[n][g] + n + 1, m - len[g], n                                       [](int x, int y) { return x > y; }) - n                      f[n][g] - 1; n         if (count >= i) n           l = m; n         else n           r = m - 1; n       } n       f[n][e][i] = l; n     } n   } n } n int main() { n   scanf( %d , &N); n   for (int i = 1, u, v, l; i < N; i++) { n     scanf( %d%d%d , &u, &v, &l); n     next[++E] = head[u], from[E] = u, to[E] = v, len[E] = l, head[u] = E; n     next[++E] = head[v], from[E] = v, to[E] = u, len[E] = l, head[v] = E; n   } n   scanf( %d%d , &root, &M); n   BFS(); n   for (int i = 1, x; i <= M; i++) { n     scanf( %d , &x); n     while (fa[x] != root) x = fa[x]; n     for (int e = head[root]; e; e = next[e]) n       if (to[e] == x) count_e[e]++; n   } n   int O = 1 << 30; n   for (int e = head[root]; e; e = next[e]) { n     F(M, e); n     O = std::min(O, f[M][e][count_e[e]] + len[e]); n   } n   printf( %d n , O); n   return 0; n } n


#include <bits/stdc++.h> n const int mxn = 100000 + 10; n using namespace std; n string s; n string soln; n int main() { n   cin >> s; n   int n = s.size(); n   for (int i = 0; i < n; i++) n     if (s[i] ==  . ) n       printf( 0 ); n     else { n       if (s[i + 1] ==  - ) n         printf( 2 ); n       else n         printf( 1 ); n       i++; n     } n   puts(  ); n } n


//CLOCKS_PER_SEC n using namespace std; n #include<bits/stdc++.h> n #define sqr(x) 1ll*(x)*(x) n //#define sort stable_sort n #define ll long long n #define mk make_pair n #define pb push_back n #define in insert n #define mtr(x,y,z) mk(mk(x,y),z) n #define fi first n #define se second n #define lch(x) ((x)<<1) n #define rch(x) (((x)<<1)|1) n #define all(x) (x).begin(),(x).end() n #define titose CLOCKS_PER_SEC n #define fpi(x) freopen(x, r ,stdin); n #define fpo(x) freopen(x, w ,stdout); n #define fprio fpi( in.txt );fpo( out.txt ); n #define fast ios_base::sync_with_stdio(false); n inline void read(int &x){int v=0,f=1;char c=getchar();while (!isdigit(c)&&c!= - ) c=getchar();if (c== - ) f=-1; else v=(c&15);while (isdigit(c=getchar())) v=(v<<1)+(v<<3)+(c&15);x=v*f;} n inline void read(ll &x){ll v=0ll,f=1ll;char c=getchar();while (!isdigit(c)&&c!= - ) c=getchar();if (c== - ) f=-1; else v=(c&15);while (isdigit(c=getchar())) v=(v<<1)+(v<<3)+(c&15);x=v*f;} n inline void readc(char &x){char c;while (((c=getchar())==   )||c==  n );x=c;} n #define pii pair<int,int> n #define pll pair<ll,ll> n #define vi vector<int> n #define vl vector<ll> n #define si set<int> n //#define sl set<ll> n #define mii map<int,int> n #define mll map<ll,ll> n #define msi map<string,int> n #define msl map<string,ll> n #define piii pair<int,pii > n #define piipi pair<pii,int> n #define plll pair<ll,pll > n #define pllpl pair<pll,ll> n #define pqi priority_queue<int> n #define pql priority_queue<ll> n #define npqi priority_queue<int,vector<int>,greater<int> > n #define npql priority_queue<ll,vector<ll>,greater<ll> > n #define forup(i,a,b) for ((i)=(a);(i)<=(b);(i)++) n #define fordo(i,a,b) for ((i)=(a);(i)>=(b);(i)--) n #define rep(i,x) forup ((i),1,(x)) n #define repd(i,x) fordo ((i),(x),1) n #define rep0(i,x) forup ((i),0,((int)(x))-1) n #define rep0d(i,x) fordo ((i),((int)(x))-1,0) n #define itr iterator n #define fe(itcalc,c) for(__typeof((c).begin()) itcalc=(c).begin();itcalc!=(c).end();itcalc++) n #define NO {cout<< NO ;return 0;} n #define YES {cout<< YES ;return 0;} n #define y0 y000000000000000000000000000 n #define y1 y111111111111111111111111111 n #define j0 j000000000000000000000000000 n #define j1 j111111111111111111111111111 n #define cl0(a) memset((a),(0),(sizeof((a)))) n #define clz(a) memset((a),(0x16),(sizeof((a)))) n #define clf(a) memset((a),(-(0x16)),(sizeof((a)))) n #define inf 0x3bbbbbbb n #define lnf 0x2bbbbbbbbbbbbbbbll n //#define sqrt divi n #define p2(i) (1ll<<(i)) n #define readi read n #define readll read n /*************************************************/ n int n,m,i,j,s=0,t=2001,px[1005],py[1005],dis[2005],inq[2005]; n map<pair<int,int>,int> mp; n struct edg n { n  int y;ll z; n }ee[1000005]; n vector<int> e[2005];int tot; n ll sum; n void add(int x,int y,ll z) n { n // cerr<<x<<   <<y<<   <<z<<endl; n  tot++; n  e[x].pb(tot+tot-2);e[y].pb(tot+tot-1); n  ee[tot+tot-2]=(edg){y,z};ee[tot+tot-1]=(edg){x,0}; n } n bool bfs(int s,int t) n { n  queue<int> qx;qx.push(s); n  clz(dis);dis[s]=0; n  while(!qx.empty()){ n   int x=qx.front();qx.pop();inq[x]=0; n   fe(it,e[x])if(ee[*it].z){ n    if(dis[ee[*it].y]>dis[x]+1){ n     dis[ee[*it].y]=dis[x]+1; n     if(!inq[ee[*it].y]){ n      qx.push(ee[*it].y); n      inq[ee[*it].y]=1; n     } n    } n   } n  } n  return dis[t]<dis[t+1]; n } n ll dfs(int x,int tt,ll flw) n { n  if(x==tt||flw==0)return flw; n  ll res=0; n  fe(it,e[x])if(ee[*it].z&&dis[ee[*it].y]==dis[x]+1){ n   ll t=dfs(ee[*it].y,tt,min(ee[*it].z,flw)); n   res+=t;flw-=t;ee[*it].z-=t;ee[(*it)^1].z+=t; n   if(!flw)break; n  } n  if(flw) dis[x]=-1; n  return res; n } n ll dinic(int s,int t) n { n  ll ans=0; n  while(bfs(s,t)){ n   ans+=dfs(s,t,lnf); n  } n  return ans; n } n int main() n { n  read(n); n  rep(i,n){ n   int z;read(py[i]);read(px[i]);read(z); n   sum+=z; n   add(i,i+n,z);mp[mk(px[i],py[i])]=i; n  } n  rep(i,n){ n   int a=abs(px[i]&1),b=abs(py[i]&1); n   if(a==0){ n    if(b==1){ n     add(s,i,lnf); n     if(mp.count(mk(px[i],py[i]-1))){ n      add(i+n,mp[mk(px[i],py[i]-1)],lnf); n     } n     if(mp.count(mk(px[i],py[i]+1))){ n      add(i+n,mp[mk(px[i],py[i]+1)],lnf); n     } n    } n    else{ n     if(mp.count(mk(px[i]-1,py[i]))){ n      add(i+n,mp[mk(px[i]-1,py[i])],lnf); n     } n     if(mp.count(mk(px[i]+1,py[i]))){ n      add(i+n,mp[mk(px[i]+1,py[i])],lnf); n     } n    } n   } n   if(a==1){ n    if(b==0){ n     if(mp.count(mk(px[i],py[i]-1))){ n      add(i+n,mp[mk(px[i],py[i]-1)],lnf); n     } n     if(mp.count(mk(px[i],py[i]+1))){ n      add(i+n,mp[mk(px[i],py[i]+1)],lnf); n     } n    } n    else{ n     add(i+n,t,lnf); n    } n   } n  } n  cout<<sum-dinic(s,t)<<endl; n  return 0; n }


#include <bits/stdc++.h> n #pragma comment(linker,  /STACK:16777216 ) n using namespace std; n const double eps = 1e-9; n const double pi = acos(-1.0); n int main() { n   long long n; n   cin >> n; n   unsigned long long p = 1; n   unsigned long long sq = (unsigned long long)(sqrt(n + 0.0) + eps + 1); n   bool was = false; n   while (p - 1 <= n) { n     unsigned long long lf = 1; n     unsigned long long rg = min(sq, n / p) + 5; n     while (lf < rg - 1) { n       unsigned long long mm = (lf + rg) >> 1; n       unsigned long long m = 2 * mm - 1; n       if ((p - 1) * m + m * (m - 1) / 2 <= n) { n         lf = mm; n       } else { n         rg = mm; n       } n     } n     lf = 2 * lf - 1; n     if ((p - 1) * lf + lf * (lf - 1) / 2 == n) { n       cout << p * lf << endl; n       was = true; n     } n     p = p * 2; n   } n   if (!was) puts( -1 ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long t1, n, zero, one, cnt, cnt1, i, j; n   string s; n   cin >> t1; n   while (t1--) { n     cin >> n; n     cnt = 0, zero = 0, one = 0; n     for (i = 0; i < n; i++) { n       cin >> s; n       for (j = 0; j < s.length(); j++) { n         if (s[j] ==  0 ) n           zero++; n         else n           one++; n       } n       if (s.length() % 2 != 0) cnt++; n     } n     cnt1 = 0; n     if (zero % 2 != 0) cnt1++; n     if (one % 2 != 0) cnt1++; n     if (cnt >= cnt1) n       cout << n << endl; n     else n       cout << n - 1 << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n signed main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   long long n; n   cin >> n; n   long long a[n], b[n]; n   for (long long i = 0; i < n; i++) { n     cin >> a[i] >> b[i]; n   } n   for (long long i = 0; i < n; i++) { n     if (a[i] != b[i]) { n       cout <<  rated ; n       return 0; n     } n   } n   for (long long i = 0; i < n - 1; i++) { n     if (a[i + 1] > a[i]) { n       cout <<  unrated ; n       return 0; n     } n   } n   cout <<  maybe ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long int Mmax(long long int a, long long int b) { n   if (a > b) return a; n   return b; n } n long long int Mmin(long long int a, long long int b) { n   if (a < b) return a; n   return b; n } n long long int nod(long long int a, long long int b) { n   while (a && b) { n     if (a > b) n       a %= b; n     else n       b %= a; n   } n   return Mmax(a, b); n } n long long int nok(long long int a, long long int b) { n   return a * b / nod(a, b); n } n bool IsPrime(long long int x) { n   if (x < 2) return false; n   long long int X = sqrt(x), i; n   for (i = 2; i <= X; i++) n     if (x % i == 0) return false; n   return true; n } n void hanoi(int n, int A, int C, int B) { n   if (n == 1) { n     cout << n <<     << A <<     << C << endl; n   } else { n     hanoi(n - 1, A, B, C); n     cout << n <<     << A <<     << C << endl; n     hanoi(n - 1, B, C, A); n   } n } n string pr2(string a, int d) { n   if (d == 0) return  0 ; n   string b; n   long long int sz = a.size(), i, prenos = 0; n   for (i = 0; i < sz; i++) { n     b[i] = a[i]; n   } n   for (i = sz - 1; i > -1; i--) { n     a[i] = ((b[i] -  0 ) * d + prenos) % 10 +  0 ; n     prenos = ((b[i] -  0 ) * d + prenos) / 10; n   } n   if (prenos) a = char(prenos +  0 ) + a; n   return a; n } n string sum(string a, string b) { n   bool carry = false; n   long long int i, sz1, sz2, maxsz, minsz; n   string c, d; n   sz1 = a.size(); n   sz2 = b.size(); n   maxsz = max(sz1, sz2); n   minsz = min(sz1, sz2); n   while (sz1 < maxsz) { n     sz1++; n     a =  0  + a; n   } n   while (sz2 < maxsz) { n     sz2++; n     b =  0  + b; n   } n   for (i = maxsz - 1; i > -1; i--) { n     d = char((a[i] + b[i] - 96 + carry) % 10 + 48) + d; n     if (a[i] + b[i] - 96 + carry > 9) n       carry = true; n     else n       carry = false; n   } n   if (carry == true) d = char( 1 ) + d; n   return d; n } n string pr(string a, string b) { n   string res =  0 , p, p2; n   int sz = a.size(), x = 0; n   for (sz = a.size(); sz > 0; sz--, x++) { n     int d = a[sz - 1] -  0 ; n     a = a.erase(sz - 1, 1); n     p2 = pr2(b, d); n     p2 += p; n     res = sum(res, p2); n     p +=  0 ; n   } n   return res; n } n bool vesokosna(long long int x) { n   return (x % 4 == 0 && x % 100 || x % 400 == 0); n } n long long int reverse(long long int x) { n   long long int mirror = 0; n   while (x) { n     mirror = mirror * 10 + x % 10; n     x /= 10; n   } n   return mirror; n } n long long int ost(string x, long long int k) { n   long long int num = 0, i, sz = x.size(); n   for (i = 0; i < sz; i++) { n     num = num * 10; n     num += x[i] -  0 ; n     num %= k; n   } n   return num; n } n int n; n int a[131072]; n int b[131072]; n vector<int> v; n int main() { n   cin >> n; n   int i; n   for (i = 0; i < n; i++) cin >> a[i]; n   for (i = 0; i < n; i++) b[i] = a[i]; n   sort(b, b + n); n   for (i = 0; i < n; i++) { n     if (b[i] != a[i]) v.push_back(i); n   } n   if (v.size() > 2) { n     cout <<  NO  << endl; n   } else if (v.size() == 0) { n     cout <<  YES  << endl; n   } else { n     swap(a[v[0]], a[v[1]]); n     for (i = 0; i < n; i++) { n       if (a[i] < a[i - 1]) { n         cout <<  NO  << endl; n         return 0; n       } n     } n     cout <<  YES  << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   cin >> n; n   string s1, s2; n   cin >> s1 >> s2; n   int flag = 0; n   if (n % 2 != 0) flag = 1; n   int mid; n   if (flag) n     mid = (n + 1) / 2; n   else n     mid = n / 2; n   int cnt = 0; n   for (int i = 0; i < n / 2; i++) { n     int l = i, r = n - i - 1; n     int a[30]; n     fill(a, a + 30, 0); n     a[s1[l] -  a ]++; n     a[s1[r] -  a ]++; n     a[s2[l] -  a ]++; n     a[s2[r] -  a ]++; n     int k = 0; n     int b[4]; n     int counts = 0; n     for (int j = 0; j < 30; j++) { n       if (a[j]) k++; n       if (a[j]) { n         b[counts++] = a[j]; n       } n     } n     sort(b, b + k); n     if (k == 4) { n       cnt += 2; n       continue; n     } n     if (k == 3) { n       if (s1[l] == s1[r]) n         cnt += 2; n       else n         cnt++; n     } n     if (k == 2) { n       if (b[1] == 3) { n         cnt++; n       } n     } n   } n   if (flag) n     if (s1[mid - 1] != s2[mid - 1]) cnt++; n   cout << cnt; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, k; n   int a[205]; n   cin >> n >> k; n   for (int i = 0; i < 2 * n + 1; i++) { n     cin >> a[i]; n   } n   for (int i = 2 * n - 1; k != 0; i--) { n     if (a[i] > a[i + 1] + 1 && a[i] > a[i - 1] + 1) { n       a[i]--; n       k--; n     } n   } n   for (int i = 0; i < 2 * n + 1; i++) { n     if (i != 0) cout <<    ; n     cout << a[i]; n   } n   cout << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int inf = 0x3f3f3f3f; n int isprime[1010]; n int mp[256]; n int main() { n   isprime[0] = isprime[1] = 1; n   for (int i = 2; i <= 1000; i++) n     if (!isprime[i]) n       for (int j = i * 2; j <= 1000; j += i) isprime[j] = 1; n   char s[1100], str[1010]; n   scanf( %s , s); n   int l = strlen(s); n   for (int i = 0; i < l; i++) { n     mp[s[i]]++; n     str[i] = 0; n   } n   str[l] = 0; n   int flag = 0; n   for (int i = 1; i <= l; i++) { n     if (isprime[i]) continue; n     int tmp = 0; n     for (int j = 1; j <= l; j++) { n       if (j % i == 0 && str[j]) { n         tmp = str[j]; n         break; n       } n     } n     if (tmp) { n       int k = 1; n       while (k * i <= l) { n         if (str[k * i] == 0) { n           mp[tmp]--; n           if (mp[tmp] < 0) { n             flag = 1; n             break; n           } n         } n         str[k * i] = tmp; n         k++; n       } n     } else { n       int p = max_element(mp, mp + 256) - mp; n       int k = 1; n       while (k * i <= l) { n         if (str[k * i] == 0) { n           mp[p]--; n           if (mp[p] < 0) { n             flag = 1; n             break; n           } n         } n         str[k * i] = p; n         k++; n       } n     } n     if (flag) break; n   } n   if (flag) n     printf( NO n ); n   else { n     printf( YES n ); n     for (int i = 1; i <= l; i++) { n       if (!str[i]) { n         int j =  a ; n         while (mp[j] == 0) { n           j++; n         } n         str[i] = j; n         mp[j]--; n       } n     } n     for (int i = 1; i <= l; i++) printf( %c , str[i]); n     printf(  n ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <class T> n inline void chkmin(T &a, T b) { n   if (a > b) a = b; n } n template <class T> n inline void chkmax(T &a, T b) { n   if (a < b) a = b; n } n inline int read() { n   int s = 0, f = 1; n   char ch = getchar(); n   while (!isdigit(ch) && ch !=  - ) ch = getchar(); n   if (ch ==  - ) ch = getchar(), f = -1; n   while (isdigit(ch)) s = s * 10 + ch -  0 , ch = getchar(); n   return ~f ? s : -s; n } n const int maxn = 1e5 + 20; n int n, q, t; n long long now; n int fa[maxn], to[maxn]; n int a[maxn]; n int f[maxn], vis[maxn]; n inline void init() { n   n = read(); n   q = read(); n   t = read(); n   for (int i = (1), _end_ = (n); i <= _end_; i++) a[i] = read(); n   for (int i = (1), _end_ = (q); i <= _end_; i++) { n     int x = read(), y = read(); n     to[x] = y; n     fa[y] = x; n   } n   for (int i = (1), _end_ = (n); i <= _end_; i++) n     if (!fa[i]) { n       int x = i; n       while (to[x]) { n         if (vis[x]) puts( 0 ), exit(0); n         vis[x] = 1; n         now += a[x]; n         a[to[x]] += a[x]; n         x = to[x]; n       } n       vis[x] = 1; n     } n   for (int i = (1), _end_ = (n); i <= _end_; i++) n     if (!vis[i]) puts( 0 ), exit(0); n } n const int mod = 1e9 + 7; n inline void pls(int &a, int b) { n   a += b; n   a -= a >= mod ? mod : 0; n } n inline void doing() { n   f[0] = 1; n   for (int j = (1), _end_ = (n); j <= _end_; j++) { n     for (int i = a[j]; i <= t - now; i++) pls(f[i], f[i - a[j]]); n   } n   if (now <= t) n     printf( %d n , f[t - now]); n   else n     puts( 0 ); n } n int main() { n   init(); n   doing(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 35000, K = 50; n struct seg_node { n   seg_node *lc, *rc; n   int l, r, f, m; n   seg_node(seg_node *lc, seg_node *rc, int l, int r) n       : lc(lc), rc(rc), l(l), r(r), f(), m() {} n   static seg_node *build(int l, int r) { n     if (l == r - 1) return new seg_node(nullptr, nullptr, l, r); n     int mid = (l + r) / 2; n     return new seg_node(build(l, mid), build(mid, r), l, r); n   } n   void add(int val) { n     f += val; n     m += val; n   } n   void push_down() { n     if (f > 0) { n       lc->add(f); n       rc->add(f); n       f = 0; n     } n   } n   void maintain() { m = max({m, lc->m, rc->m}); } n   void add(int ql, int qr, int val) { n     if (ql == l && qr == r) n       add(val); n     else { n       push_down(); n       int mid = (l + r) / 2; n       if (qr <= mid) n         lc->add(ql, qr, val); n       else if (ql >= mid) n         rc->add(ql, qr, val); n       else { n         lc->add(ql, mid, val); n         rc->add(mid, qr, val); n       } n       maintain(); n     } n   } n   int query(int ql, int qr) { n     if (ql == l && qr == r) n       return m; n     else { n       push_down(); n       int mid = (l + r) / 2; n       if (qr <= mid) n         return lc->query(ql, qr); n       else if (ql >= mid) n         return rc->query(ql, qr); n       else n         return max(lc->query(ql, mid), rc->query(mid, qr)); n     } n   } n } * t[K + 1]; n int a[N], last[N]; n int main() { n   ios::sync_with_stdio(false); n   int n, k; n   cin >> n >> k; n   int cnt = 0; n   t[1] = seg_node::build(0, n); n   memset(last, 0xff, sizeof last); n   for (int i = 0; i < n; i++) { n     cin >> a[i]; n     a[i]--; n     if (last[a[i]] == -1) { n       cnt++; n       last[a[i]] = i; n     } n     t[1]->add(i, i + 1, cnt); n   } n   for (int i = 2; i <= k; i++) { n     t[i] = seg_node::build(0, n); n     memset(last, 0xff, sizeof last); n     for (int j = i - 1; j < n; j++) { n       t[i - 1]->add(last[a[j]] == -1 ? i - 2 : last[a[j]], j, 1); n       t[i]->add(j, j + 1, t[i - 1]->query(i - 2, j)); n       last[a[j]] = j; n     } n   } n   cout << t[k]->query(n - 1, n); n } n


#include <bits/stdc++.h> n using namespace std; n int n; n int a[222222]; n set<int> s; n int main() { n   scanf( %d , &n); n   for (int i = 1; i <= n; i++) { n     s.insert(i); n   } n   for (int i = 1; i <= n; i++) { n     scanf( %d , &a[i]); n     if (a[i] != 0) { n       s.erase(a[i]); n     } n   } n   int idx = n; n   int iidx = 0; n   int iiidx = -1; n   for (auto &x : s) { n     while (a[idx] != 0) { n       idx--; n     } n     a[idx] = x; n     iidx = idx; n     if (iiidx == -1) { n       iiidx = idx; n     } n   } n   for (int i = 1; i <= n; i++) { n     if (a[i] == i) { n       if (i == iidx) { n         a[i] = a[iiidx]; n         a[iiidx] = i; n       } else { n         a[i] = a[iidx]; n         a[iidx] = i; n       } n       break; n     } n   } n   for (int i = 1; i <= n; i++) { n     printf( %d  , a[i]); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   cin >> n; n   long long ans = 0; n   int t = n; n   for (int i = 2; i * i <= t; ++i) { n     while (n % i == 0) { n       ans += n; n       n /= i; n     } n   } n   ans += n == 1 ? 1 : n + 1; n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int freq[101]; n int main() { n   int n, x, max = 0, count = 0; n   cin >> n; n   string s; n   if (n == 1) { n     cin >> s; n     sort(s.begin(), s.end()); n     for (int i = 0; i < s.length(); i++) { n       if (s[i] == s[i + 1]) count++; n     } n     cout << count << endl; n   } else if (n > 1) { n     int arr[101] = {}; n     for (int i = 1; i <= n; i++) { n       cin >> x; n       freq[x]++; n     } n     for (int i = 0; i < 101; i++) { n       if (freq[i] > max) max = freq[i]; n     } n     cout << max; n     return 0; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int AND = 0; n const int OR = 1; n const int NAND = 2; n const int NOR = 3; n const int N = 111; n int n, m, k; n string s; n int a[N][3]; n int b[N][3]; n vector<int> g[N]; n bool st[N]; n int ord[N]; n int ordSz; n int col[N]; n int C; n bool used[N]; n bool oldUsed[N]; n long long G[N]; n int readType() { n   cin >> s; n   if (s ==  and ) return AND; n   if (s ==  or ) return OR; n   if (s ==  nand ) return NAND; n   if (s ==  nor ) return NOR; n   throw; n } n void read() { n   cin >> n >> m >> k; n   for (int i = 0; i < m; i++) { n     a[i][0] = readType(); n     cin >> s; n     int p = 1; n     for (int j = 0; j < n; j++) { n       if (s[j] ==  x ) { n         a[i][p++] = j; n       } n     } n   } n   for (int i = 0; i < k; i++) { n     b[i][0] = readType(); n     cin >> s; n     int p = 1; n     for (int j = 0; j < m; j++) { n       if (s[j] ==  x ) { n         b[i][p++] = j; n       } n     } n   } n } n void addEdge(int v, int u) { n   g[v ^ 1].push_back(u); n   g[u ^ 1].push_back(v); n } n void addGraph(int v, int tp) { n   tp = a[v][0] ^ (tp << 1); n   if (tp == 0) { n     st[2 * a[v][1] + 1] = 1; n     st[2 * a[v][2] + 1] = 1; n   } else if (tp == 1) { n     addEdge(2 * a[v][1] + 1, 2 * a[v][2] + 1); n   } else if (tp == 2) { n     addEdge(2 * a[v][1], 2 * a[v][2]); n   } else if (tp == 3) { n     st[2 * a[v][1]] = 1; n     st[2 * a[v][2]] = 1; n   } n } n void dfsMark(int v) { n   used[v] = 1; n   for (int u : g[v]) { n     if (!used[u]) dfsMark(u); n   } n } n bool checkUsed() { n   for (int i = 0; i < n; i++) n     if (used[2 * i] && used[2 * i + 1]) return false; n   return true; n } n bool checkSAT(long long MASK) { n   for (int i = 0; i < 2 * n; i++) { n     g[i].clear(); n     st[i] = 0; n     used[i] = 0; n   } n   for (int i = 0; i < k; i++) { n     if (((MASK >> i) & 1) == 0) continue; n     for (int j = 1; j < 3; j++) addGraph(b[i][j], (int)(b[i][0] < 2)); n   } n   for (int i = 0; i < 2 * n; i++) n     if (st[i]) dfsMark(i); n   if (!checkUsed()) return false; n   for (int i = 0; i < n; i++) { n     if (used[2 * i] || used[2 * i + 1]) continue; n     for (int j = 0; j < 2 * n; j++) oldUsed[j] = used[j]; n     dfsMark(2 * i); n     if (checkUsed()) continue; n     for (int j = 0; j < 2 * n; j++) used[j] = oldUsed[j]; n     dfsMark(2 * i + 1); n     if (!checkUsed()) return false; n   } n   return checkUsed(); n } n int intVar[10]; n int cntVar; n bool oper(int t, bool v1, bool v2) { n   bool res = 0; n   if (t >= 2) { n     t ^= 2; n     res ^= 1; n   } n   if (t == AND) n     return res ^ (v1 && v2); n   else n     return res ^ (v1 || v2); n } n bool getValConst(int v, int MASK) { n   for (int i = 0; i < cntVar; i++) { n     if (intVar[i] == v) { n       return (MASK >> i) & 1; n     } n   } n   throw; n } n bool getValA1(int v, int MASK) { n   return oper(a[v][0], getValConst(a[v][1], MASK), getValConst(a[v][2], MASK)); n } n bool getValA2(int v, int MASK) { n   return oper(a[v][0] ^ 2, getValConst(a[v][1], MASK), n               getValConst(a[v][2], MASK)); n } n bool getValB1(int v, int MASK) { n   return oper(b[v][0], getValA1(b[v][1], MASK), getValA1(b[v][2], MASK)); n } n bool getValB2(int v, int MASK) { n   return oper(b[v][0] ^ 2, getValA2(b[v][1], MASK), getValA2(b[v][2], MASK)); n } n bool solve(int id1, int id2) { n   cntVar = 0; n   for (int i = 1; i < 3; i++) n     for (int j = 1; j < 3; j++) { n       intVar[cntVar++] = a[b[id1][i]][j]; n       intVar[cntVar++] = a[b[id2][i]][j]; n     } n   sort(intVar, intVar + cntVar); n   cntVar = unique(intVar, intVar + cntVar) - intVar; n   for (int MASK = 0; MASK < (1 << cntVar); MASK++) { n     if (getValB1(id1, MASK) && getValB2(id2, MASK)) return true; n   } n   return false; n } n bool isGoodMask(long long MASK) { return !checkSAT(MASK); } n void eraseFromG(int v, int u) { n   if ((G[v] >> u) & 1) G[v] ^= 1LL << u; n } n int ANS = 0; n void brute(int curAns, long long curMask, long long cand, long long already) { n   if (cand == 0) { n     if (isGoodMask(curMask)) ANS = max(ANS, curAns); n     return; n   } n   bool ok = true; n   for (int i = 0; ok && i < k; i++) { n     if (((already >> i) & 1) == 0) continue; n     if ((G[i] & cand) == cand) ok = false; n   } n   if (!ok) return; n   for (int v = 0; (1LL << v) <= cand; v++) { n     if (((cand >> v) & 1) == 0) continue; n     brute(curAns + 1, curMask | (1LL << v), (cand & G[v]) ^ (1LL << v), n           already & G[v]); n     already |= 1LL << v; n     cand ^= 1LL << v; n   } n } n int main() { n   read(); n   for (int i = 0; i < k; i++) G[i] = (1LL << k) - 1; n   for (int i = 0; i < k; i++) n     for (int j = 0; j < k; j++) { n       if (solve(i, j)) { n         eraseFromG(i, j); n         eraseFromG(j, i); n       } n     } n   long long all = 0; n   for (int i = 0; i < k; i++) { n     if (((G[i] >> i) & 1) == 0) continue; n     all |= 1LL << i; n   } n   brute(0, 0, all, 0); n   if (ANS == 0) n     cout << -1 << endl; n   else n     cout << k - ANS << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int INF = INT_MAX; n const signed long long int LINF = LLONG_MAX; n const double EPS = 1e-9; n const int MOD = 1000000007; n const int MAXN = 100001; n const int ALPH = 26; n const int DALPH = 52; n string s; n vector<int> z; n vector<pair<int, int> > ans; n vector<int> add, sum; n int main() { n   cin >> s; n   z.resize((int)(s).size()); n   int l = 0, r = 0; n   z[0] = (int)(s).size(); n   for (int i = 1; i < (int)(s).size(); ++i) { n     int j = 0; n     if (i <= r) { n       int ind = i - l; n       j = min(z[ind], r - i + 1); n     } n     for (; i + j < (int)(s).size(); ++j) { n       if (s[j] != s[i + j]) { n         break; n       } n     } n     z[i] = j; n     if (i + j - 1 > r) { n       r = i + j - 1; n       l = i; n     } n   } n   add.resize((int)(s).size()); n   for (int i = 1; i < (int)(s).size(); ++i) { n     add[0]++; n     add[z[i]]--; n   } n   sum.resize((int)(s).size()); n   int cnt = 0; n   for (int i = 0; i < (int)(s).size(); ++i) { n     cnt += add[i]; n     sum[i] = cnt + 1; n   } n   for (int i = (int)(s).size() - 1; i >= 0; --i) { n     if (i + z[i] != (int)(s).size()) { n       continue; n     } n     ans.push_back(make_pair((z[i]), (sum[z[i] - 1]))); n   } n   printf( %d n , (int)(ans).size()); n   for (int i = 0; i < (int)(ans).size(); ++i) { n     printf( %d %d n , ans[i].first, ans[i].second); n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int INF = (int)1e9; n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   int n; n   cin >> n; n   vector<int> xx(n); n   vector<int> yy(n); n   vector<int> diff(n); n   for (int i = 0; i < n; i++) { n     string s; n     cin >> s; n     xx[i] = count(s.begin(), s.end(),  B ); n     yy[i] = (int)s.size() - xx[i]; n     diff[i] = yy[i] - xx[i]; n   } n   int maxX = *max_element(xx.begin(), xx.end()); n   int minX = *min_element(xx.begin(), xx.end()); n   int maxY = *max_element(yy.begin(), yy.end()); n   int minY = *min_element(yy.begin(), yy.end()); n   int max_diff = *max_element(diff.begin(), diff.end()); n   int min_diff = *min_element(diff.begin(), diff.end()); n   auto Works = [&](int dist) -> pair<int, int> { n     int x1 = maxX - dist; n     int x2 = minX + dist; n     int y1 = maxY - dist; n     int y2 = minY + dist; n     int d1 = max_diff - dist; n     int d2 = min_diff + dist; n     auto Inside = [&](int x, int y) { n       int d = y - x; n       return (x1 <= x && x <= x2 && y1 <= y && y <= y2 && d1 <= d && d <= d2 && n               x + y >= 1); n     }; n     { n       int x = x2; n       int y = d1 + x; n       y = max(y, y1); n       y = min(y, y2); n       if (Inside(x, y)) { n         return {x, y}; n       } n     } n     { n       int y = y2; n       int x = y - d1; n       x = max(x, x1); n       x = min(x, x2); n       if (Inside(x, y)) { n         return {x, y}; n       } n     } n     return {-1, -1}; n   }; n   int low = 0, high = 5e5; n   while (low < high) { n     int mid = (low + high) / 2; n     if (Works(mid).first != -1) { n       high = mid; n     } else { n       low = mid + 1; n     } n   } n   auto [k0, k1] = Works(low); n   cout << low <<   n ; n   cout << string(k0,  B ) + string(k1,  N ) <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 100100; n const int INF = 1000000000; n int n, m, numV; n int head[MAXN], e_cnt, s, t; n bool volunt[MAXN]; n struct Edg { n   int v, next; n } edg[MAXN * 4]; n inline void add(int a, int b) { n   edg[e_cnt].v = b; n   edg[e_cnt].next = head[a]; n   head[a] = e_cnt++; n } n int dis[MAXN]; n bool inq[MAXN]; n bool spfa(int x) { n   for (int i = 1; i <= n; ++i) dis[i] = INF; n   dis[s] = 0; n   queue<int> q; n   q.push(s); n   while (!q.empty()) { n     int u = q.front(); n     q.pop(); n     inq[u] = 0; n     if (volunt[u]) dis[u] = 0; n     for (int i = head[u]; i != -1; i = edg[i].next) { n       int v = edg[i].v; n       if (dis[v] > dis[u] + 1 && dis[u] + 1 <= x) { n         dis[v] = dis[u] + 1; n         if (!inq[v]) q.push(v); n         inq[v] = 1; n       } n     } n   } n   return dis[t] != INF; n } n int main() { n   while (scanf( %d%d%d , &n, &m, &numV) != EOF) { n     memset(volunt, 0, sizeof(volunt)); n     e_cnt = 0; n     memset(head, -1, sizeof(head)); n     for (int i = 0, j; i < numV; ++i) { n       scanf( %d , &j); n       volunt[j] = 1; n     } n     for (int i = 0; i < m; ++i) { n       int ta, tb; n       scanf( %d%d , &ta, &tb); n       add(ta, tb); n       add(tb, ta); n     } n     scanf( %d%d , &s, &t); n     int rt(m), lt(1), ans(-1); n     while (lt <= rt) { n       int mid = (lt + rt) >> 1; n       if (spfa(mid)) { n         rt = mid - 1; n         ans = mid; n       } else n         lt = mid + 1; n     } n     printf( %d n , ans); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n; n vector<pair<int, int> > edges; n vector<int> conn[201]; n bool v1[201], v2[201]; n int mx = 0; n void setEdge(int a, int b, int v) { n   for (int s = 0; s < conn[a].size(); s++) { n     if (conn[a][s] == b || conn[a][s] == -1) conn[a][s] = (v == 1 ? b : -1); n   } n   for (int s = 0; s < conn[b].size(); s++) { n     if (conn[b][s] == a || conn[b][s] == -1) conn[b][s] = (v == 1 ? a : -1); n   } n } n int getHeight(int i) { n   v2[i] = 1; n   int ret = 0; n   for (int j = 0; j < conn[i].size(); j++) { n     if (conn[i][j] != -1 && !v2[conn[i][j]]) { n       ret = max(ret, 1 + getHeight(conn[i][j])); n     } n   } n   v2[i] = 0; n   return ret; n } n int longestPath(int i) { n   v1[i] = 1; n   int ret = 0; n   int cnt = 0; n   for (int j = 0; j < conn[i].size(); j++) { n     if (conn[i][j] != -1 && !v1[conn[i][j]]) { n       ret = max(ret, longestPath(conn[i][j])); n       cnt++; n     } n   } n   if (cnt == 0) ret = max(ret, getHeight(i)); n   v1[i] = 0; n   return ret; n } n int main() { n   cin >> n; n   memset(v1, 0, sizeof v1); n   memset(v2, 0, sizeof v2); n   int a, b; n   for (int i = 0; i < n - 1; i++) { n     cin >> a >> b; n     edges.push_back(make_pair(a - 1, b - 1)); n     conn[a - 1].push_back(b - 1); n     conn[b - 1].push_back(a - 1); n   } n   for (int i = 0; i < edges.size(); i++) { n     a = edges[i].first; n     b = edges[i].second; n     setEdge(a, b, 0); n     mx = max(mx, longestPath(a) * longestPath(b)); n     setEdge(a, b, 1); n   } n   cout << mx << endl; n } n


#include<bits/stdc++.h> n using namespace std; n   n typedef long long ll; n typedef vector< ll > vl; n typedef vector< vector< ll > > vvl; n #define ff first n #define ss second n #define boost ios_base::sync_with_stdio(false); cin.tie(NULL);   cout.tie(NULL); n #define pb push_back n #define all(c) c.begin(), c.end() n #define tr(c,it) for(auto it=(c).begin(); it!=(c).end(); it++) n #define ref(i,n)    for(i=0;i<n;i++) n #define mp make_pair n #define endl   n  n #define MOD 1000000007 n ll power(ll a, ll b){//a^b n     ll res=1; n     a=a%MOD; n     while(b>0){ n         if(b&1){res=(res*a)%MOD;b--;} n         a=(a*a)%MOD; n         b>>=1; n     } n     return res; n } n ll fermat_inv(ll y){return power(y,MOD-2);} n ll gcd(ll a, ll b){return (b==0)?a:gcd(b,a%b);} n  n int main() n { n     boost n     ll t; n     cin>>t; n     while(t--) n     { n         ll n,x=0,y=0,flag=0,m,i; n         cin>>n; n         string s; n         cin>>s; n         ref(i,n) n         { n             if(s[i]== T ) n                 x++; n             else n             { n                 y++; n             } n             if(y>x) n             { n                 flag=1; n                 break; n             } n         } n         if(flag==1 || x!=2*y) n         { n             cout<< NO <<endl; n             continue; n         } n         x=y=flag=0; n         for(i=n-1;i>=0;i--) n         { n             if(s[i]== T ) n                 x++; n             else n             { n                 y++; n             } n             if(y>x) n             { n                 flag=1; n                 break; n             } n         } n         if(flag) n             cout<< NO <<endl; n         else n             cout<< YES <<endl; n     } n     return 0; n } n  n  n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(); n   cin.tie(); n   cout.tie(); n   int n; n   cin >> n; n   int weight[n]; n   for (int i = 0; i < n; ++i) cin >> weight[i]; n   int p; n   cin >> p; n   vector<pair<pair<int, int>, int> > data; n   for (int i = 0; i < p; ++i) { n     int a, b; n     cin >> a >> b; n     --a; n     data.push_back({{b, a}, i}); n   } n   sort(data.begin(), data.end()); n   vector<pair<int, long long> > answer; n   long long dp[n]; n   int prev = -1; n   for (int i = 0; i < p; ++i) { n     int b = data[i].first.first, a = data[i].first.second; n     if (b >= 500) { n       long long sum = 0; n       for (; a < n; a += b) sum += weight[a]; n       answer.push_back({data[i].second, sum}); n       prev = -1; n       continue; n     } n     if (prev == -1 || b != prev) { n       for (int j = n - 1; j >= 0; --j) { n         if (j + b > n - 1) n           dp[j] = weight[j]; n         else n           dp[j] = weight[j] + dp[j + b]; n       } n     } n     answer.push_back({data[i].second, dp[a]}); n     prev = b; n   } n   sort(answer.begin(), answer.end()); n   for (int i = 0; i < p; ++i) cout << answer[i].second <<   n ; n   return 0; n } n


#define _CRT_SECURE_NO_WARNINGS n #include <iostream> n #include<time.h> n #include <cstdio> n #include <cstring> n #include <cstdlib> n #include <cmath> n #include <algorithm> n #include <queue> n #include <stack> n #include <unordered_map> n #include <unordered_set> n #include <stdio.h> n #include <deque> n #include <map> n #include <set> n #include <string> n #define ll long long n typedef long long LL; n typedef unsigned long long ULL; n //typedef long double ld; n using namespace std; n const int MAXN = 2e6 + 5000; n const long double eps = (1e-12); n const int logn = 21; n const double Pi = acos(-1.0); n const LL pp = 1000000000 + 9; n int lowbit(int x) { return x & (-x); } n #define MP(i,j) make_pair(i,j) n #define M(x) ((((x)%pp)+pp)%pp) n //#define int LL n #define P pair<int,int> n const int INF = 1e9; n  n int toright[505][505]; n int todown[505][505]; n  n int dx[5] = { 0,0,0,-1,1 }; n int dy[5] = { 0,-1,1,0,0 }; n int dp[505][505][25]; n  n int getww(int x1, int y1, int x2, int y2) { n     if (x2 == x1 + 1) { n         return todown[x1][y1]; n     } n     else if (x2 == x1 - 1) { n         return todown[x2][y1]; n     } n     else if (y2 == y1 + 1) { n         return toright[x1][y1]; n     } n     else if (y2 == y1 - 1) { n         return toright[x1][y2]; n     } n     return -1; n } n  n signed main() { n     int n, m, k; n     scanf( %d%d%d , &n, &m, &k); n     for (int i = 1; i <= n; i++) { n         for (int j = 1; j <= m - 1; j++) { n             scanf( %d , &toright[i][j]); n         } n     } n     for (int i = 1; i <= n - 1; i++) { n         for (int j = 1; j <= m; j++) { n             scanf( %d , &todown[i][j]); n         } n     } n     if (k % 2 == 1) { n         for (int i = 1; i <= n; i++) { n             for (int j = 1; j <= m; j++) { n                 printf( -1  ); n             } n             printf(  n ); n         } n     } n     else { n         k /= 2; n  n         for (int kk = 1; kk <= k; kk++) { n             for (int i = 1; i <= n; i++) { n                 for (int j = 1; j <= m; j++) { n                     dp[i][j][kk] = INF; n                 } n             } n         } n  n         for (int kk = 1; kk <= k; kk++) { n             for (int i = 1; i <= n; i++) { n                 for (int j = 1; j <= m; j++) { n                     for (int l = 1; l <= 4; l++) { n                         int x = i + dx[l]; int y = j + dy[l]; n                         if (x<1 || x>n || y<1 || y>m) continue; n                         int w = getww(i, j, x, y); n                         dp[i][j][kk] = min(dp[i][j][kk], w + dp[x][y][kk - 1]); n                     } n                 } n             } n         } n  n         for (int i = 1; i <= n; i++) { n             for (int j = 1; j <= m; j++) { n                 printf( %d  , dp[i][j][k] * 2); n             } n             printf(  n ); n         } n     } n  n  n     return 0; n }


#include <bits/stdc++.h> n using namespace std; n const int maxn = 300000 + 5; n int a[maxn], dp[maxn]; n int n, k; n bool ok_hack(int x) { n   memset(dp, 0, sizeof(dp)); n   int last = 0; n   for (int i = k; i <= n; i++) { n     if (a[i] - a[dp[i - k] + 1] <= x) last = i; n     dp[i] = last; n   } n   return dp[n] == n; n } n int main() { n   scanf( %d%d , &n, &k); n   for (int i = 1; i <= n; i++) scanf( %d , &a[i]); n   sort(a + 1, a + n + 1); n   a[0] = 0; n   int l = 0, r = a[n] - a[1]; n   while (l < r) { n     int mid = r + l >> 1; n     if (ok_hack(mid)) n       r = mid; n     else n       l = mid + 1; n   } n   printf( %d n , l); n   return 0; n } n


#include <bits/stdc++.h> n #pragma GCC optimize( Ofast ) n #pragma GCC optimize( unroll-loops ) n #pragma GCC target( sse,sse2,sse3,ssse3,abm,mmx,tune=native ) n using namespace std; n const int64_t INF = (int64_t)(2e18); n const int maxn = 500 * 1000 + 100; n chrono::time_point<chrono::steady_clock> cl; n double current_time() { n   return (double)(chrono::steady_clock::now() - cl).count() / 1e9; n } n void no() { n   cout <<  0 ; n   exit(0); n } n void inf() { n   cout <<  inf ; n   exit(0); n } n void ans(long long a) { n   cout << a; n   exit(0); n } n int32_t main() { n   cl = chrono::steady_clock::now(); n   ios_base::sync_with_stdio(false); n   cout << fixed << setprecision(10); n   cin.tie(nullptr); n   long long x, y, z; n   cin >> x >> y >> z; n   if (x == y && y != z) no(); n   if (x != y && y == z) ans(1); n   if (x == z && y != z) no(); n   if (x == y && y == z) { n     if (x == 1) inf(); n     if (x != 1) ans(2); n   } n   if (x > y) no(); n   if (y > z) no(); n   bool flag = false; n   if (x == 1ll) { n     flag = true; n     long long zz = z; n     while (z > 1) { n       if (z % y != 0) flag = false; n       z /= y; n     } n     z = zz; n   } n   vector<int> a; n   long long zcp = z; n   while (zcp > 0) { n     a.push_back(zcp % y); n     zcp /= y; n   } n   long long cur = 0; n   long long curx = 1; n   for (auto& it : a) { n     cur += it * curx; n     curx *= x; n   } n   int res = 0; n   if (cur == y) ++res; n   if (flag) ++res; n   cout << bool(res); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n using ll = long long; n using ii = pair<ll, ll>; n int main() { n   char rep[256], a[256], b[256], d[256]; n   while (scanf( %[0-9].%[0-9]e%[0-9] n , a, d, b) == 3) { n     auto len = strlen(d); n     if (b[0] ==  0 ) { n       if (len == 1 and d[0] ==  0 ) n         printf( %s n , a); n       else n         printf( %s.%s n , a, d); n       continue; n     } n     if (a[0] ==  0 ) a[0] = 0; n     while (len > 0 and d[len - 1] ==  0 ) --len; n     d[len] = 0; n     int digits = 0; n     for (auto p = a; *p; ++p) rep[digits++] = *p; n     int n = atoi(b); n     auto p = d; n     for (; *p and n; ++p) { n       rep[digits++] = *p; n       --n; n     } n     rep[digits] = 0; n     if (n == 0) { n       printf( %s , rep); n       if (*p) { n         printf( . ); n         while (*p) { n           printf( %c , *p); n           ++p; n         } n       } n       printf(  n ); n     } else { n       printf( %s , rep); n       while (n--) printf( 0 ); n       printf(  n ); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long M = 1e9 + 7; n int32_t main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   ; n   long long t; n   cin >> t; n   while (t--) { n     long long n; n     cin >> n; n     long long a[n]; n     map<long long, long long> m; n     map<long long, long long> m1; n     map<long long, long long> m2; n     map<long long, long long> m3; n     vector<pair<long long, long long>> v; n     for (long long i = 0; i < n; i++) { n       cin >> a[i]; n       m2[a[i]]++; n     } n     for (auto it = m2.begin(); it != m2.end(); it++) { n       m3[it->second]++; n     } n     for (long long i = 0; i < n - 1; i++) { n       long long x = ++m[a[i]]; n       m1[x]++; n       m1[x - 1]--; n       long long y = m2[a[i]]; n       if (y == 1) { n         m2.erase(m2.find(a[i])); n       } else n         m2[a[i]]--; n       m3[y]--; n       m3[y - 1]++; n       auto it = m.end(); n       it--; n       x = it->first; n       it = m2.end(); n       it--; n       y = it->first; n       if (x == m.size() && y == m2.size() && m1[1] == x && m3[1] == y) { n         v.push_back({x, y}); n       } n     } n     cout << v.size() <<   n ; n     for (long long i = 0; i < (long long)v.size(); i++) { n       cout << v[i].first <<     << v[i].second <<   n ; n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n struct node { n   int x, h, id; n } a[100005]; n int n, i, j, f[100005], ans[100005]; n bool cmp(node x, node y) { n   if (x.x != y.x) n     return x.x < y.x; n   else n     return x.h < y.h; n } n int main() { n   scanf( %d , &n); n   for (i = 1; i <= n; i++) { n     scanf( %d%d , &a[i].x, &a[i].h); n     a[i].id = i; n   } n   sort(a + 1, a + n + 1, cmp); n   for (i = n; i >= 1; i--) { n     f[i] = 1; n     for (j = i + 1; j <= n; j += f[j]) { n       if (a[i].x + a[i].h - 1 >= a[j].x) n         f[i] += f[j]; n       else n         break; n     } n     ans[a[i].id] = f[i]; n   } n   for (i = 1; i <= n; i++) printf( %d  , ans[i]); n   printf(  n ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   map<char, int> ma; n   char a; n   int b; n   cin >> b; n   for (int i = 0; i < b; i++) { n     cin >> a; n     ma[a]++; n   } n   for (auto i : ma) n     for (int j = 0; j < i.second; j++) cout << i.first; n } n


#include <bits/stdc++.h> n using namespace std; n double x; n int N, M, ar[5005], res; n int dn[5005]; n int main() { n   scanf( %d %d , &N, &M); n   for (int i = 0; i < N; i++) { n     scanf( %d %lf , &ar[i], &x); n     ar[i]--; n   } n   for (int i = 0; i < N; i++) { n     for (int j = ar[i]; j >= 0; j--) { n       dn[ar[i]] = max(dn[ar[i]], 1 + dn[j]); n     } n   } n   for (int i = 0; i < M; i++) { n     res = max(res, dn[i]); n   } n   printf( %d n , N - res); n } n


#include <bits/stdc++.h> n const int MN = 1e3 + 10; n const int K = 20; n int N, M, T, id[MN], t[MN], ctr, m[1 << K]; n bool conn[MN][MN]; n char s[10]; n std::vector<int> a[MN], on[K + 1]; n struct Mod { n  public: n   char c; n   int a, b; n   void out() const { printf( %c %d %d n , c, a, b); } n }; n std::vector<Mod> f; n void flip(int a, int b) { n   f.push_back({conn[a][b] ?  -  :  + , a, b}); n   conn[a][b] ^= 1; n } n void dfs(int n) { n   for (int x : a[n]) n     if (!~id[x]) dfs(x); n   id[n] = ctr; n   t[ctr] = n; n   ++ctr; n } n int main() { n   memset(id, -1, sizeof id); n   scanf( %d%d%d , &N, &M, &T); n   for (int i = 0, u, v; i < M; ++i) n     scanf( %d%d , &u, &v), a[u].push_back(v), conn[u][v] = 1; n   ctr = 0; n   for (int i = 1; i <= N; ++i) n     if (!~id[i]) dfs(i); n   for (int i = std::min(N, K) - 1; i; --i) n     for (int j = i - 1; j >= 0; --j) n       if (!conn[t[i]][t[j]]) { n         a[t[i]].push_back(t[j]); n         conn[t[i]][t[j]] = 1; n         f.push_back({ + , t[i], t[j]}); n       } n   for (int i = 0; i < 1 << K; ++i) n     if (__builtin_popcount(i) <= 3) on[__builtin_popcount(i)].push_back(i); n   memset(m, -1, sizeof m); n   for (int i = K; i < N; ++i) { n     f.push_back({ + , t[i], t[i]}); n     int n = t[i], v = 0; n     for (int x : a[n]) n       if (id[x] < K) v |= 1 << id[x]; n     bool ok = 0; n     for (int j = 0; !ok; ++j) { n       assert(j <= 3); n       for (int k : on[j]) n         if (!~m[v ^ k]) { n           m[v ^ k] = n; n           for (int b = 0; b < K; ++b) n             if (k >> b & 1) flip(n, t[b]); n           ok = 1; n           break; n         } n     } n   } n   printf( %u n , f.size()); n   for (int i = 0; i < f.size(); ++i) f[i].out(); n   fflush(stdout); n   for (int i = 0; i < T; ++i) { n     int v = 0; n     bool lose = 0; n     for (int j = 0; j < std::min(N, K); ++j) { n       printf( ? 1 %d n , t[j]); n       fflush(stdout); n       scanf(  %s , s); n       if (s[0] ==  S ) return 0; n       if (s[0] ==  W ) v |= 1 << j; n       if (s[0] ==  L ) { n         lose = 1, v = j; n         break; n       } n     } n     if (lose) n       printf( ! %d n , t[v]); n     else n       printf( ! %d n , m[v]); n     fflush(stdout); n     scanf(  %s , s); n     if (s[0] ==  W ) return 0; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long n; n long long b[500010], a[500010]; n void solve() { n   cin >> n; n   for (int i = 0; i < n; i++) cin >> b[i]; n   long long sum = 0, flag = 1; n   for (int i = 0; i < n; i++) sum += b[i]; n   if (sum % (n * (n + 1) / 2)) flag = 0; n   sum /= n * (n + 1) / 2; n   for (int i = 0; i < n; i++) { n     long long dx = sum - b[(i + 1) % n] + b[i]; n     if (dx % n || dx <= 0) n       flag = 0; n     else n       a[(i + 1) % n] = dx / n; n   } n   if (flag) { n     cout <<  YES  <<   n ; n     for (int i = 0; i < n; i++) cout << a[i] <<    ; n     cout <<   n ; n   } else n     cout <<  NO  <<   n ; n } n int main() { n   std::ios::sync_with_stdio(false); n   std::cin.tie(0); n   std::cout.tie(0); n   int _; n   cin >> _; n   while (_--) solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 3e5; n const int INF = 1e9; n int n; n int ct[MAXN][2]; n vector<int> ok(int d) { n   vector<int> bad(2, -1); n   int loX = 0, hiX = INF; n   int loY = 0, hiY = INF; n   int loD = -INF, hiD = INF; n   for (int i = 0; i < n; i++) { n     int x = ct[i][0], y = ct[i][1]; n     loX = max(loX, x - d); n     hiX = min(hiX, x + d); n     loY = max(loY, y - d); n     hiY = min(hiY, y + d); n     loD = max(loD, y - x - d); n     hiD = min(hiD, y - x + d); n   } n   if (loX > hiX || loY > hiY || loD > hiD) return bad; n   auto check1 = [&](int x) { n     int lo = max(loY, loD + x), hi = min(hiY, hiD + x); n     if (lo > hi) return bad; n     if (x || hi) return vector<int>{x, hi}; n     return bad; n   }; n   auto check2 = [&](int y) { n     int lo = max(loX, y - hiD), hi = min(hiX, y - loD); n     if (lo > hi) return bad; n     if (y || hi) return vector<int>{hi, y}; n     return bad; n   }; n   if (bad != check1(loX)) return check1(loX); n   if (bad != check1(hiX)) return check1(hiX); n   if (bad != check2(loY)) return check2(loY); n   if (bad != check2(hiY)) return check2(hiY); n   return bad; n } n void run_case() { n   cin >> n; n   for (int i = 0; i < n; i++) { n     string s; n     cin >> s; n     for (char &j : s) { n       ct[i][j ==  N ]++; n     } n   } n   int lo = -1; n   int hi = 1e6; n   while (lo + 1 < hi) { n     int mid = (lo + hi) / 2; n     if (ok(mid)[0] != -1) { n       hi = mid; n     } else { n       lo = mid; n     } n   } n   vector<int> res; n   res = ok(hi); n   cout << hi <<   n ; n   string ans = string(res[0],  B ) + string(res[1],  N ); n   cout << ans <<   n ; n } n int main() { n   ios::sync_with_stdio(false), cin.tie(0); n   if (fopen( input.in ,  r )) n     freopen( input.in ,  r , stdin), freopen( output.out ,  w , stdout); n   int t; n   t = 1; n   while (t--) { n     run_case(); n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 205, M = N * N; n using ll = long long; n int n, a[N][N]; n struct Edge { n   int v, nxt; n   ll w, cw; n } e[M << 2]; n int head[M], ptr; n void add_edge(int uu, int vv) { n   e[ptr] = {vv, head[uu], 1, 0}; n   head[uu] = ptr++; n   e[ptr] = {uu, head[vv], 1, 0}; n   head[vv] = ptr++; n } n int ss[M], tt[M]; n bool vis[M]; n bool push(int pos, int fl) { n   if (tt[pos] && fl == 1) return 1; n   if (ss[pos] && fl == -1) return 1; n   vis[pos] = 1; n   for (int i = head[pos]; i != -1; i = e[i].nxt) { n     int v = e[i].v; n     if (fl == 1 && e[i].cw == 1) continue; n     if (fl == -1 && e[i].cw != -1) continue; n     if (vis[v]) continue; n     if (push(v, fl)) { n       ++e[i].cw; n       --e[i ^ 1].cw; n       return 1; n     } n   } n   return 0; n } n inline int id(int x, int y) { return (x - 1) * n + y; } n vector<pair<int, int>> v; n int main() { n   ios::sync_with_stdio(false); n   cin >> n; n   memset(head, -1, sizeof(head)); n   for (int i = 1; i <= n; i++) n     for (int j = 1; j <= n; j++) cin >> a[i][j]; n   for (int i = 1; i <= n; i++) n     for (int j = 1; j <= n; j++) { n       if (a[i][j] == -1) continue; n       if (i + 1 <= n && a[i + 1][j] >= 0) add_edge(id(i, j), id(i + 1, j)); n       if (j + 1 <= n && a[i][j + 1] >= 0) add_edge(id(i, j), id(i, j + 1)); n       if (a[i][j] > 0) { n         tt[id(i, j)] = 1; n         v.push_back({a[i][j], id(i, j)}); n       } n     } n   sort(v.begin(), v.end()); n   int flow = 0; n   ll ans = 0; n   for (int i = 0; i < int(v.size()) - 1; i++) { n     int pos = v[i].second; n     memset(vis, 0, sizeof(vis)); n     while (push(pos, -1)) --flow, memset(vis, 0, sizeof(vis)); n     tt[pos] = 0; n     ss[pos] = 1; n     memset(vis, 0, sizeof(vis)); n     for (int j = 0; j <= i; j++) n       if (!vis[v[j].second]) n         while (push(v[j].second, 1)) ++flow, memset(vis, 0, sizeof(vis)); n     ans += ll(v[i + 1].first - v[i].first) * flow; n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   string A, B; n   int ma = 0; n   cin >> A >> B; n   for (char &ch : A) ma = max(ma, ch -  0 ); n   for (char &ch : B) ma = max(ma, ch -  0 ); n   ma++; n   int a = 0, b = 0; n   for (char &ch : A) { n     a = a * ma + ch -  0 ; n   } n   for (char &ch : B) { n     b = b * ma + ch -  0 ; n   } n   a += b; n   int ans = 0; n   while (a) ans++, a /= ma; n   cout << ans <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m, ar[11][100005]; n int rev[11][100005]; n long long mx(int start) { n   int val = ar[0][start]; n   for (int tmp = 0; tmp < n; ++tmp) { n     for (int row = 1; row < m; ++row) { n       int col = rev[row][val]; n       if (col + tmp >= n) { n         return tmp; n       } n       int curVal = ar[row][col + tmp]; n       int val2 = ar[0][start + tmp]; n       if (curVal != val2) { n         return tmp; n       } n     } n   } n   return n; n } n int main() { n   scanf( %d%d , &n, &m); n   for (int i = 0; i < m; ++i) { n     for (int j = 0; j < n; ++j) { n       scanf( %d , ar[i] + j); n       rev[i][ar[i][j]] = j; n     } n   } n   long long ans = 0; n   for (int i = 0; i < n;) { n     long long mxi = mx(i); n     ans += (mxi * (mxi + 1) / 2ll); n     i += mxi; n   } n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 6e5 + 5, mod = 998244353; n vector<int> num; n long long A[N]; n int a[N]; n long long sumup = 0, sumdown = 0; n long long qpow(long long a, long long b) { n   long long c = 1; n   while (b) { n     if (b & 1) c = (a * c) % mod; n     b >>= 1; n     a = (a * a) % mod; n   } n   return c; n } n long long inv(long long x) { return qpow(x, mod - 2); } n int main() { n   int n; n   cin >> n; n   n <<= 1; n   A[0] = 1; n   for (int i = 1; i <= n; i++) A[i] = (A[i - 1] * i) % mod; n   for (int i = 1; i <= n; i++) cin >> a[i]; n   long long ans = 0; n   sort(a + 1, a + n + 1); n   for (int i = 1; i <= n / 2; i++) ans -= a[i]; n   for (int i = n / 2 + 1; i <= n; i++) ans += a[i]; n   ans = (ans % mod * A[n] % mod * inv(A[n / 2]) % mod * inv(A[n / 2])) % mod; n   cout << ans << endl; n } n


#include <bits/stdc++.h> n using namespace std; n inline void read(int &x) { n   x = 0; n   char c = getchar(); n   int f = 1; n   while (!isdigit(c)) { n     if (c ==  - ) f = -1; n     c = getchar(); n   } n   while (isdigit(c)) { n     x = x * 10 + c -  0 ; n     c = getchar(); n   } n   x *= f; n } n const int N = 6666; n const double eps = 1e-9; n int n, m, a[N], b[N]; n void Get(double &x, int y) { x -= (double)(x / y); } n int main() { n   read(n); n   read(m); n   for (register int i = 1; i <= n; i++) read(a[i]); n   for (register int i = 1; i <= n; i++) read(b[i]); n   double l = 0, r = 1e9, res = 2e9; n   for (register int t = 70; t >= 1; t--) { n     double mid = (l + r) / 2, now = m + mid; n     for (register int i = 1; i <= n; i++) n       Get(now, a[i]), Get(now, b[i < n ? i + 1 : 1]); n     if (now > m - eps) n       res = mid, r = mid; n     else n       l = mid; n   } n   if (res > 1e9 + eps) n     puts( -1 ); n   else n     printf( %.9lf , res); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long INF = 1e18; n int n, m, q; n long long add[510], a[510][510], dp[510][510], gox[510][510], goy[510][510]; n vector<pair<int, int> > vx[510][510], vy[510][510]; n int main() { n   scanf( %d%d%d , &n, &m, &q); n   for (int i = 0; i < q; i++) { n     int x, y, t, d, e; n     scanf( %d%d%d%d%d , &x, &y, &d, &t, &e); n     if (x + y >= t && (x + y - t) % 4 == 0) { n       a[x][y] += e; n       if (x + y + d >= t && (x + y + d - t) % 4 == 2) n         vx[x][y].push_back({x + d, e}); n       else n         vx[x][y].push_back({n, e}); n       if (x + y + d >= t && (x + y + d - t) % 4 == 3) n         vy[x][y].push_back({y + d, e}); n       else n         vy[x][y].push_back({m, e}); n     } n     if (x + y >= t && (x + y - t) % 4 == 1) { n       a[x + d][y - d] += e; n       vx[x + d][y - d].push_back({n, e}); n       if (x + y + d >= t && (x + y + d - t) % 4 == 2) n         vy[x + d][y - d].push_back({y, e}); n       else n         vy[x + d][y - d].push_back({m, e}); n     } n     if (x + y + d >= t && (x + y + d - t) % 4 == 2) { n       a[x + d][y] += e; n       vx[x + d][y].push_back({n, e}); n       vy[x + d][y].push_back({m, e}); n     } n     if (x + y + d >= t && (x + y + d - t) % 4 == 3) { n       a[x][y + d] += e; n       vx[x][y + d].push_back({n, e}); n       vy[x][y + d].push_back({m, e}); n     } n   } n   for (int x = 0; x < n; x++) { n     for (int y = 0; y < m; y++) { n       dp[x][y] = INF; n       { n         long long cur = 0; n         for (auto it : vx[x][y]) { n           cur += it.second; n           add[it.first] += it.second; n         } n         for (int k = x; k <= n; k++) { n           cur -= add[k]; n           gox[y][k] += cur; n           add[k] = 0; n         } n       } n       { n         long long cur = 0; n         for (auto it : vy[x][y]) { n           cur += it.second; n           add[it.first] += it.second; n         } n         for (int k = y; k <= m; k++) { n           cur -= add[k]; n           goy[k][x] += cur; n           add[k] = 0; n         } n       } n     } n   } n   dp[0][0] = a[0][0]; n   for (int x = 0; x < n; x++) { n     for (int y = 0; y < m; y++) { n       dp[x][y] -= a[x][y]; n       for (int k = x + 1; k < n; k++) n         dp[k][y] = min(dp[k][y], dp[x][y] + gox[y][k]); n       for (int k = y + 1; k < m; k++) n         dp[x][k] = min(dp[x][k], dp[x][y] + goy[k][x]); n       { n         long long cur = 0; n         for (auto it : vx[x][y]) { n           cur += it.second; n           add[it.first] += it.second; n         } n         for (int k = x; k <= n; k++) { n           cur -= add[k]; n           gox[y][k] -= cur; n           add[k] = 0; n         } n       } n       { n         long long cur = 0; n         for (auto it : vy[x][y]) { n           cur += it.second; n           add[it.first] += it.second; n         } n         for (int k = y; k <= m; k++) { n           cur -= add[k]; n           goy[k][x] -= cur; n           add[k] = 0; n         } n       } n     } n   } n   printf( %lld , dp[n - 1][m - 1]); n   return 0; n } n


#include <bits/stdc++.h> n int main() { n   int h, w, bh, bw, th, tw; n   long long ar, ba; n   bh = bw = ba = 0; n   scanf( %d %d , &h, &w); n   int tmp; n   for (int i = 30; i >= 0; i--) { n     tmp = (1 << i); n     if (tmp <= w) { n       long long x = (long long)tmp * 8; n       long long y = (long long)tmp * 125; n       if (x % 10 != 0) n         x = x / 10 + 1; n       else n         x = x / 10; n       y /= 100; n       if (y <= h) { n         th = y; n         tw = tmp; n         ar = (long long)th * (long long)tw; n         if (ar > ba || ar == ba && th > bh) { n           bh = th; n           bw = tw; n           ba = (long long)bh * (long long)bw; n         } n       } else if (x <= h) { n         th = h; n         tw = tmp; n         ar = (long long)th * (long long)tw; n         if (ar > ba || ar == ba && th > bh) { n           bh = th; n           bw = tw; n           ba = (long long)bh * (long long)bw; n         } n       } n     } n   } n   for (int i = 30; i >= 0; i--) { n     tmp = (1 << i); n     if (tmp <= h) { n       int x = (int)((double)tmp / 1.25); n       int y = (int)((double)tmp / 0.8); n       if ((double)y * 0.8 < (double)tmp) y++; n       if ((double)x * 1.25 > (double)tmp) x--; n       if (y <= w) { n         th = tmp; n         tw = y; n         ar = (long long)th * (long long)tw; n         if (ar > ba || ar == ba && th > bh) { n           bh = th; n           bw = tw; n           ba = (long long)bh * (long long)bw; n         } n       } else if (x < w) { n         th = tmp; n         tw = w; n         ar = (long long)th * (long long)tw; n         if (ar > ba || ar == ba && th > bh) { n           bh = th; n           bw = tw; n           ba = (long long)bh * (long long)bw; n         } n       } n     } n   } n   if (bw > bh && bw <= h) std::swap(bw, bh); n   printf( %d %d n , bh, bw); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int r1, r2, c1, c2, d1, d2; n int main() { n   scanf( %d%d%d%d%d%d , &r1, &r2, &c1, &c2, &d1, &d2); n   for (int i = 1; i <= 9; ++i) n     for (int j = 1; j <= 9; ++j) n       for (int ii = 1; ii <= 9; ++ii) n         for (int jj = 1; jj <= 9; ++jj) { n           if (i != j && i != ii && i != jj && j != ii && j != jj && ii != jj) n             if (i + j == r1 && jj + ii == r2 && i + ii == c1 && j + jj == c2 && n                 i + jj == d1 && j + ii == d2) { n               printf( %d %d n%d %d , i, j, ii, jj); n               return 0; n             } n         } n   printf( -1 ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n double a[100010], ans1, ans2; n int n, n1, n2; n bool cmp(double a, double b) { return a > b; } n int main() { n   scanf( %d%d%d , &n, &n1, &n2); n   if (n1 > n2) swap(n1, n2); n   for (int i = 1; i <= n; i++) scanf( %lf , &a[i]); n   sort(a + 1, a + n + 1, cmp); n   for (int i = 1; i <= n1; i++) ans1 += a[i]; n   for (int i = n1 + 1; i <= n1 + n2; i++) ans2 += a[i]; n   printf( %.8f n , ans1 / n1 + ans2 / n2); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n map<long long, long long> bismarck, dp; n long long n; n void factorization(long long x) { n   for (long long i = 2; i * i <= x; i++) { n     if (x % i == 0) { n       long long c = 0; n       while (x % i == 0) { n         c++; n         x /= i; n       } n       dp[i] |= 1 << (c - 1); n     } n   } n   if (x > 1) { n     dp[x] |= 1; n   } n } n long long grundy(long long x) { n   if (x == 0) return 0; n   if (bismarck.count(x)) return bismarck[x]; n   unordered_set<long long> s; n   for (long long i = 0; x >> i; i++) { n     s.insert(grundy(x >> i + 1 | (x & (1 << i) - 1))); n   } n   long long mex = 0; n   while (s.count(mex)) mex++; n   return bismarck[x] = mex; n } n int main() { n   long long x, ans = 0; n   cin >> n; n   while (n--) { n     cin >> x; n     factorization(x); n   } n   for (auto t : dp) { n     ans ^= grundy(t.second); n   } n   if (ans) n     cout <<  Mojtaba ; n   else n     cout <<  Arpa ; n } n