#include  bits/stdc++.h  n   n using int64 = long long; n const int N = 305; n const int Q = 998244353; n   n inline void add(int &x, int y) { n   x += y; n   if (x >= Q) x -= Q; n } n   n int n, dep[N]; n std::vector<int> e[N]; n int f[N][N];  // x , y : max uncovered depth n int g[N][N];  // x , y : max extended length n   n void dfs(int x, int fa, int K) { n   dep[x] = 0; n   int nf[2][N]; n   int ng[2][N]; n   int cur = 0, nxt = 1; n   memset(nf[cur], 0, sizeof(nf[cur])); n   memset(ng[cur], 0, sizeof(ng[cur])); n   ng[cur][0] = 1; n   n   for (auto &y : e[x]) { n     if (y == fa) { n       continue; n     } n     dfs(y, x, K); n     memset(nf[nxt], 0, sizeof(nf[nxt])); n     memset(ng[nxt], 0, sizeof(ng[nxt])); n     for (int i = 0; i <= dep[x]; ++i) { n       if (nf[cur][i]) { n         for (int j = 0; j <= dep[y]; ++j) { n           int depth = std::max(i, j); n           if (depth <= K) { n             add(nf[nxt][depth], (int64)nf[cur][i] * f[y][j] % Q); n           } n         } n         for (int j = 0; j <= K; ++j) { n           if (j - 2 >= i) { n             add(ng[nxt][j], (int64)nf[cur][i] * g[y][j] % Q); n           } else { n             add(nf[nxt][i], (int64)nf[cur][i] * g[y][j] % Q); n           } n         } n       } n     } n     for (int i = 0; i <= K; ++i) { n       if (ng[cur][i] > 0) { n         for (int j = 0; j <= dep[y]; ++j) { n           if (i - 2 >= j) { n             add(ng[nxt][i], (int64)ng[cur][i] * f[y][j] % Q); n           } else { n             add(nf[nxt][j], (int64)ng[cur][i] * f[y][j] % Q); n           } n         } n         for (int j = 0; j <= K; ++j) { n           int depth = std::max(i, j); n           add(ng[nxt][depth], (int64)ng[cur][i] * g[y][j] % Q); n         } n       } n     } n     std::swap(cur, nxt); n     dep[x] = std::max(dep[x], dep[y]); n   } n   n   for (int i = 0; i <= dep[x]; ++i) { n     if (i + 1 < K) { n       add(f[x][i + 1], nf[cur][i]); n     } n     if (i + 1 <= K) { n       add(g[x][K], nf[cur][i]); n     } n   } n   for (int i = 0; i <= K; ++i) { n     if (i > 0) { n       add(g[x][i - 1], ng[cur][i]); n     } else { n       add(f[x][0], ng[cur][i]); n     } n     add(g[x][std::max(i - 1, K)], ng[cur][i]); n   } n   // printf( === %d n , x); n   // for (int i = 0; i <= dep[x]; ++i) { n   //   printf( %d  , f[x][i]); n   // } n   // puts(  ); n   // for (int i = 0; i <= K; ++i) { n   //   printf( %d  , g[x][i]); n   // } n   // puts(  ); n   n   ++dep[x]; n } n   n int main() { n   scanf( %d , &n); n   for (int i = 1; i < n; ++i) { n     int x = i, y = i + 1; n     scanf( %d%d , &x, &y); n     e[x].emplace_back(y); n     e[y].emplace_back(x); n   } n   int res = Q - 1, pow2 = 1, inv = (Q + 1) / 2, inv2 = 1; n   for (int i = 1; i <= n; ++i) { n     add(pow2, pow2); n     inv2 = (int64)inv2 * inv % Q; n   } n   for (int i = 1; i <= n; ++i) { n     memset(f, 0, sizeof(f)); n     memset(g, 0, sizeof(g)); n     dfs(1, 0, i); n     add(res, pow2); n     for (int j = 0; j <= i; ++j) { n       add(res, Q - g[1][j]); n     } n     // std::cout <<  :   << i <<     << res << std::endl; n   } n   printf( %d n , (int)((int64)res * inv2 % Q)); n }


#include <bits/stdc++.h> n using namespace std; n const long long md = 1e9 + 7; n const long long o5 = 716327852; n const int N = 2e5 + 34; n long long f[N], o[N]; n long long ans, n, w, h; n long long dp[N]; n pair<long long, long long> a[N]; n long long c(long long h, long long w) { n   return (((f[h + w] * o[h]) % md) * o[w]) % md; n } n int main() { n   o[0] = f[0] = 1; n   o[100000] = o5; n   for (long long i = 1; i < N; i++) f[i] = (f[i - 1] * i) % md; n   for (long long i = 99999; i > 0; i--) o[i] = (o[i + 1] * (i + 1)) % md; n   cin >> h >> w >> n; n   ans = c(h - 1, w - 1); n   for (int i = 0; i < n; i++) { n     cin >> a[i].first >> a[i].second; n     a[i].first--; n     a[i].second--; n   } n   sort(a, a + n); n   for (int i = 0; i < n; i++) { n     dp[i] = c(a[i].second, a[i].first); n     for (int j = 0; j < i; j++) n       if (a[j].first <= a[i].first && a[j].second <= a[i].second) n         dp[i] = (dp[i] - n                  dp[j] * c(a[i].first - a[j].first, a[i].second - a[j].second) + n                  md * md) % n                 md; n     ans = (ans + md * md - dp[i] * c(w - a[i].second - 1, h - a[i].first - 1)) % n           md; n   } n   cout << ans; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1005; n int k; n long long pa, pb; n long long A, B, C; n long long f[N][N]; n void exgcd(int a, int b, long long &x, long long &y) { n   if (b == 0) { n     x = 1, y = 0; n     return; n   } n   exgcd(b, a % b, y, x); n   y -= a / b * x; n } n long long inv(int x) { n   long long res, y; n   exgcd(x, 1000000007, res, y); n   res = ((res % 1000000007) + 1000000007) % 1000000007; n   return res; n } n int main() { n   std::ios::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   cin >> k >> pa >> pb; n   A = pa * inv(pa + pb) % 1000000007; n   B = pb * inv(pa + pb) % 1000000007; n   C = pa * inv(pb) % 1000000007; n   for (int i = k; i >= 1; i--) n     for (int j = k; j >= 0; j--) { n       if (i + j >= k) { n         f[i][j] = i + C; n         continue; n       } n       f[i][j] = (A * f[i + 1][j] + B * (f[i][i + j] + i)) % 1000000007; n     } n   cout << (f[1][0] + 1000000007) % 1000000007; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<int> adj[202]; n int height[202], best[202][3]; n int N; n int getHeight(int ind, int parent) { n   if (height[ind] != -1) return height[ind]; n   int ret = 1; n   for (int i = 0; i < (int)adj[ind].size(); i++) { n     if (adj[ind][i] == parent) continue; n     ret = max(ret, getHeight(adj[ind][i], ind) + 1); n   } n   return height[ind] = ret; n } n int solve(int ind, int parent, int f) { n   if (best[ind][f] != -1) return best[ind][f]; n   int ret = 0; n   if (!f) { n     for (int i = 0; i < (int)adj[ind].size(); i++) { n       if (adj[ind][i] == parent) continue; n       int a = 0, b = 0; n       for (int j = 0; j < (int)adj[ind].size(); j++) { n         if (adj[ind][j] == parent || j == i) continue; n         int h = getHeight(adj[ind][j], adj[ind][j]); n         if (h > a) n           b = a, a = h; n         else if (h > b) n           b = h; n       } n       ret = max(ret, (a + b) * solve(adj[ind][i], ind, f + 1)); n     } n   } else { n     int a = 0, b = 0; n     for (int j = 0; j < (int)adj[ind].size(); j++) { n       if (adj[ind][j] == parent) continue; n       int h = getHeight(adj[ind][j], adj[ind][j]); n       if (h > a) n         b = a, a = h; n       else if (h > b) n         b = h; n       ret = max(ret, solve(adj[ind][j], ind, f)); n     } n     ret = max(ret, a + b); n   } n   return best[ind][f] = ret; n } n int main() { n   scanf( %d , &N); n   for (int i = 0; i < N - 1; i++) { n     int a, b; n     scanf( %d %d , &a, &b); n     a--, b--; n     adj[a].push_back(b); n     adj[b].push_back(a); n   } n   int ret = 0; n   for (int i = 0; i < N; i++) { n     memset(height, -1, sizeof height); n     memset(best, -1, sizeof best); n     getHeight(i, i); n     ret = max(ret, solve(i, i, 0)); n   } n   printf( %d n , ret); n } n


#include <bits/stdc++.h> n using namespace std; n long long n, ans[100001], curr, deg[100001] = {0}; n vector<pair<int, int>> adj[100001]; n void dfs(long long u, long long p) { n   for (auto v : adj[u]) { n     if (v.first == p) { n       continue; n     } n     if (ans[v.second] == -1) { n       ans[v.second] = curr++; n     } n     dfs(v.first, u); n   } n } n void solve() { n   long long n; n   cin >> n; n   long long m; n   cin >> m; n   if (n > m || n % 2 != m % 2) { n     cout << -1 << endl; n     return; n   } n   if (n == m && n == 0) { n     cout << n << endl; n     return; n   } n   if (n == m) { n     cout << 1 << endl << n << endl; n     return; n   } n   long long x = (m - n) / 2; n   if ((x & n) == 0) { n     cout << 2 << endl << n + x <<     << x << endl; n     return; n   } n   { n     cout << 3 << endl << n <<     << x <<     << x << endl; n     return; n   } n } n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   ; n   solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, m; n   cin >> n >> m; n   long long ans = 0; n   for (int i = (0); i < (n); ++i) { n     long long t, T, x, cost; n     cin >> t >> T >> x >> cost; n     if (t >= T) { n       ans += cost + m * x; n       continue; n     } n     long long aux1 = cost; n     if (m > (T - t)) aux1 += m * x; n     long long aux2 = (long long)ceil((double)(m - (T - t)) / (T - t)) + 1; n     aux2 *= cost; n     ans += min(aux1, aux2); n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<pair<int, int> > ans; n void merge(int l, int r, int lev) { n   if (lev == 0) { n     ans.push_back({l, r}); n     return; n   } n   int mid = (l + r) >> 1; n   merge(l, mid, lev - 1); n   merge(mid + 1, r, lev - 1); n   int j = mid + 1; n   for (int i = l; i <= mid; i++) { n     ans.push_back({i, j}); n     j++; n   } n } n int main(int argc, char** argv) { n   int n; n   scanf( %d , &n); n   if (n == 1) { n     printf( 0 ); n     return 0; n   } n   int t = 1; n   int lev = -1; n   while (t * 2 <= n) t *= 2, lev++; n   merge(1, t, lev); n   merge(n - t + 1, n, lev); n   cout << ans.size() << endl; n   for (int i = 0; i <= ans.size() - 1; i++) { n     printf( %d %d n , ans[i].first, ans[i].second); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MaxK = 53, MaxN = 2000005; n int N, K; n char A[MaxN]; n char B[MaxN]; n int id(char c) { return ( a  <= c && c <=  z ) ? c -  a  : c -  A  + 26; } n namespace KuhnMunkres { n const int MAX_N = 505; n const int INF = 100000000; n const int BASE = 1000; n int n; n int graph[MAX_N][MAX_N]; n bool usedX[MAX_N]; n bool usedY[MAX_N]; n int linkTo[MAX_N]; n int labelX[MAX_N]; n int labelY[MAX_N]; n bool findPath(int i) { n   usedX[i] = true; n   for (int j = 0; j < n; j++) n     if (!usedY[j] && labelX[i] + labelY[j] == graph[i][j] && linkTo[j] == -1) { n       usedY[j] = true; n       linkTo[j] = i; n       return true; n     } n   for (int j = 0; j < n; j++) n     if (!usedY[j] && labelX[i] + labelY[j] == graph[i][j]) { n       usedY[j] = true; n       if (linkTo[j] == -1 || findPath(linkTo[j])) { n         linkTo[j] = i; n         return true; n       } n     } n   return false; n } n int KuhnMunkres() { n   memset(linkTo, -1, sizeof(linkTo)); n   memset(labelX, 0, sizeof(labelX)); n   memset(labelY, 0, sizeof(labelY)); n   for (int i = 0; i < n; i++) n     for (int j = 0; j < n; j++) labelX[i] = max(labelX[i], graph[i][j]); n   for (int k = 0; k < n; k++) n     while (true) { n       memset(usedX, 0, sizeof(usedX)); n       memset(usedY, 0, sizeof(usedY)); n       if (findPath(k)) break; n       int delta = INF; n       for (int i = 0; i < n; i++) n         if (usedX[i]) n           for (int j = 0; j < n; j++) n             if (!usedY[j]) n               delta = min(delta, labelX[i] + labelY[j] - graph[i][j]); n       if (delta == 0 || delta == INF) break; n       for (int i = 0; i < n; i++) n         if (usedX[i]) labelX[i] -= delta; n       for (int i = 0; i < n; i++) n         if (usedY[i]) labelY[i] += delta; n     } n   int maxTotal = 0; n   for (int i = 0; i < n; i++) maxTotal += labelX[i] + labelY[i]; n   return maxTotal; n } n };  // namespace KuhnMunkres n int main() { n   cin.sync_with_stdio(false); n   cin >> N >> K; n   cin >> A; n   cin >> B; n   KuhnMunkres::n = K; n   for (int i = 0; i < N; i++) { n     int u = id(A[i]); n     int v = id(B[i]); n     KuhnMunkres::graph[u][v]++; n   } n   cout << KuhnMunkres::KuhnMunkres() << endl; n   int array[MaxK]; n   for (int i = 0; i < K; i++) { n     array[KuhnMunkres::linkTo[i]] = i; n   } n   for (int i = 0; i < K; i++) { n     int b = array[i]; n     if (b < 26) n       cout << (char)( a  + b); n     else n       cout << (char)( A  + b - 26); n   } n   cout << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int x[500500], y[500500]; n long long minl[500500], maxl[500500], minr[500500], maxr[500500], ll, rr; n int n; n pair<long long, long long> a[500500]; n bool ok(long long M) { n   int j = 0; n   for (int i = 0; i < n; i++) { n     while ((j < n) && (a[j].first - a[i].first <= M)) j++; n     if (max((i > 0) ? maxl[i - 1] : -10000000000LL, n             (j < n) ? maxr[j] : -10000000000LL) - n             min((i > 0) ? minl[i - 1] : 10000000000LL, n                 (j < n) ? minr[j] : 10000000000LL) <= n         M) n       return 1; n   } n   return 0; n } n int main() { n   scanf( %d , &n); n   for (int i = 0; i < n; i++) scanf( %d%d , &x[i], &y[i]); n   for (int i = 0; i < n; i++) a[i] = make_pair(x[i] - y[i], x[i] + y[i]); n   sort(a, a + n); n   for (int i = 0; i < n; i++) { n     minl[i] = min((i > 0) ? minl[i - 1] : 10000000000LL, a[i].second); n     maxl[i] = max((i > 0) ? maxl[i - 1] : -10000000000LL, a[i].second); n   } n   for (int i = n - 1; i >= 0; i--) { n     minr[i] = min((i + 1 < n) ? minr[i + 1] : 10000000000LL, a[i].second); n     maxr[i] = max((i + 1 < n) ? maxr[i + 1] : -10000000000LL, a[i].second); n   } n   ll = 0; n   rr = 3e9; n   for (int it = 0; it < 40; it++) n     if (ok((ll + rr) / 2)) n       rr = (ll + rr) / 2; n     else n       ll = (ll + rr) / 2; n   if (!ok(ll)) ll = rr; n   printf( %.10lf n , ll * 1.0 / 2); n } n


#include <bits/stdc++.h> n const int maxi = 2000000000; n const int maxq = 1000000000; n const double eps = 1e-10; n const double pi = 3.1415926535897932; n const double inf = 1e+18; n const int mo = 1000000007; n using namespace std; n int stn, ms[1111][1111], n, k, t, st[1111111], x, y, z, sum; n bool f[11111]; n void rec(int x, int y) { n   sum += ms[x][y]; n   if (x == n) { n     stn++; n     st[stn] = sum; n   } n   if (y) f[y] = true; n   if (x < n) n     for (int i = 0; i <= n; i++) n       if (f[i] == false && (ms[x + 1][i] || i == 0)) rec(x + 1, i); n   sum -= ms[x][y]; n   f[y] = false; n } n int main() { n   cin >> n >> k >> t; n   for (int i = 0; i < k; i++) { n     cin >> x >> y >> z; n     ms[x][y] = z; n   } n   for (int i = 0; i <= n; i++) n     if (ms[1][i] || i == 0) rec(1, i); n   sort(st + 1, st + stn + 1); n   cout << st[t] << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<vector<long long> > dp; n const long long mod = 1e9 + 7; n bool used[1005][1005]; n int n; n long long dfs(int opening, int closing) { n   if (dp[opening][closing] != -1) return dp[opening][closing]; n   if (opening > n or closing >= n) return 0; n   long long ans = 0; n   if (opening < n) { n     ans += dfs(opening + 1, closing); n     if (!used[opening + 1][closing] and !used[opening][closing]) { n       ans++; n       used[opening][closing] = 1; n     } n   } n   if (closing < opening) { n     ans += dfs(opening, closing + 1); n     if (!used[opening][closing + 1] and !used[opening][closing]) { n       ans++; n       used[opening][closing] = 1; n     } n   } n   return dp[opening][closing] = ans % mod; n } n int main() { n   dp = vector<vector<long long> >(1005, vector<long long>(1005, -1)); n   cin >> n; n   cout << dfs(0, 0) << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   cin >> n; n   vector<int> v(n); n   for (int i = 0; i < n; ++i) v[i] = i + 2; n   int n_colors = 1; n   vector<bool> prime(n, true); n   vector<int> colors(n); n   for (int i = 0; v[i] <= sqrt(n + 1); ++i) { n     if (prime[i]) { n       for (int j = v[i] * v[i] - 2; j < n; j += v[i]) { n         prime[j] = false; n       } n     } n   } n   for (int i = 0; i < n; ++i) { n     if (prime[i]) { n       colors[i] = 1; n     } else { n       n_colors = 2; n       colors[i] = 2; n     } n   } n   cout << n_colors << endl; n   for (auto c : colors) cout << c <<    ; n   cout << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n unordered_map<long long int, long long int> mp; n long long int n, m, k, ans; n long long int a[22][22]; n int dx[] = {1, -1, 0, 0}; n int dy[] = {0, 0, 1, -1}; n void dfs1(int ex, int ey, int sx, int sy, long long int cur) { n   if (sx == ex and sy == ey) mp[cur]++; n   if (sx < ex) dfs1(ex, ey, sx + 1, sy, cur ^ (a[sx][sy])); n   if (sy < ey) dfs1(ex, ey, sx, sy + 1, cur ^ (a[sx][sy])); n } n void dfs(int x, int y, long long int cur) { n   if (x == n - 1 and y == m - 1) cur ^= a[x][y], ans += mp[cur ^ k]; n   if (x < n - 1) dfs(x + 1, y, cur ^ (a[x][y])); n   if (y < m - 1) dfs(x, y + 1, cur ^ (a[x][y])); n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cin >> n >> m >> k; n   for (int i = 0; i < n; i++) n     for (int j = 0; j < m; j++) cin >> a[i][j]; n   int mid = (n + m - 2) / 2; n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < m; j++) { n       if (i + j == mid) { n         dfs1(i, j, 0, 0, 0); n         dfs(i, j, 0); n         mp.clear(); n       } n     } n   } n   cout << ans <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n const int inf = 0x3f3f3f3f, Inf = 0x7fffffff; n const long long INF = 0x7fffffffffffffff; n template <typename _Tp> n _Tp gcd(const _Tp &a, const _Tp &b) { n   return (!b) ? a : gcd(b, a % b); n } n template <typename _Tp> n __inline__ __attribute__((always_inline)) _Tp abs(const _Tp &a) { n   return a >= 0 ? a : -a; n } n template <typename _Tp> n __inline__ __attribute__((always_inline)) _Tp max(const _Tp &a, const _Tp &b) { n   return a < b ? b : a; n } n template <typename _Tp> n __inline__ __attribute__((always_inline)) _Tp min(const _Tp &a, const _Tp &b) { n   return a < b ? a : b; n } n template <typename _Tp> n __inline__ __attribute__((always_inline)) void chmax(_Tp &a, const _Tp &b) { n   (a < b) && (a = b); n } n template <typename _Tp> n __inline__ __attribute__((always_inline)) void chmin(_Tp &a, const _Tp &b) { n   (b < a) && (a = b); n } n template <typename _Tp> n __inline__ __attribute__((always_inline)) void read(_Tp &x) { n   char ch(getchar()); n   bool f(false); n   while (ch < 48 || ch > 57) f |= ch == 45, ch = getchar(); n   x = ch & 15, ch = getchar(); n   while (ch >= 48 && ch <= 57) n     x = (((x << 2) + x) << 1) + (ch & 15), ch = getchar(); n   if (f) x = -x; n } n template <typename _Tp, typename... Args> n __inline__ __attribute__((always_inline)) void read(_Tp &t, Args &...args) { n   read(t); n   read(args...); n } n __inline__ __attribute__((always_inline)) int read_str(char *s) { n   char ch(getchar()); n   while (ch ==     || ch ==   r  || ch ==   n ) ch = getchar(); n   char *tar = s; n   *tar = ch, ch = getchar(); n   while (ch !=     && ch !=   r  && ch !=   n  && ch != EOF) n     *(++tar) = ch, ch = getchar(); n   return tar - s + 1; n } n const int N = 200005; n const int mod = 998244353; n template <typename _Tp1, typename _Tp2> n __inline__ __attribute__((always_inline)) void add(_Tp1 &a, _Tp2 b) { n   (a += b) >= mod && (a -= mod); n } n template <typename _Tp1, typename _Tp2> n __inline__ __attribute__((always_inline)) void sub(_Tp1 &a, _Tp2 b) { n   (a -= b) < 0 && (a += mod); n } n long long ksm(long long a, long long b = mod - 2) { n   long long res = 1; n   while (b) { n     if (b & 1) res = res * a % mod; n     a = a * a % mod, b >>= 1; n   } n   return res; n } n long long fac[N], inv[N]; n void init() { n   fac[0] = fac[1] = inv[0] = inv[1] = 1; n   for (int i = 2; i < N; ++i) { n     fac[i] = fac[i - 1] * i % mod; n     inv[i] = inv[mod % i] * (mod - mod / i) % mod; n   } n   for (int i = 2; i < N; ++i) { n     inv[i] = inv[i - 1] * inv[i] % mod; n   } n } n __inline__ __attribute__((always_inline)) long long C(int n, int m) { n   return fac[n] * inv[m] % mod * inv[n - m] % mod; n } n long long S(int n, int m) { n   long long ans = 0; n   for (int i = 0; i <= m; ++i) { n     if ((m - i) & 1) n       sub(ans, C(m, i) * ksm(i, n) % mod); n     else n       add(ans, C(m, i) * ksm(i, n) % mod); n   } n   ans = ans * inv[m] % mod; n   return ans; n } n int main() { n   init(); n   int n; n   long long k; n   read(n, k); n   if (k == 0) n     printf( %lld n , fac[n]); n   else if (k >= n) n     printf( %lld n , 0ll); n   else n     printf( %lld n , 2ll * C(n, n - k) * fac[n - k] % mod * S(n, n - k) % mod); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int a[2000050]; n int b[2000050]; n int c[2000050]; n struct node { n   int x, y; n   node() {} n   node(int xx, int yy) { x = xx, y = yy; } n } q[2000050]; n int main() { n   int n; n   scanf( %d , &n); n   for (int i = 1; i <= n; ++i) scanf( %d , &a[i]); n   for (int i = 1; i <= n; ++i) { n     scanf( %d , &b[i]); n     c[b[i]] = i; n   } n   for (int i = 1; i <= n; ++i) a[i] = c[a[i]]; n   int tail = 0; n   int ans = 0; n   for (int i = n; i > 0; --i) { n     int p = -1; n     for (int j = n; j > 0; --j) n       if (a[j] == i) { n         p = j; n         break; n       } n     if (p == i) continue; n     int now = p + 1; n     while (p != i) { n       while (now < n && a[now] > p) now++; n       q[tail++] = node(p, now); n       swap(a[p], a[now]); n       ans += now - p; n       p = now; n       now++; n     } n   } n   printf( %d n , ans); n   printf( %d n , tail); n   for (int i = 0; i < tail; ++i) { n     printf( %d %d n , q[i].x, q[i].y); n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main(int argc, char const *argv[]) { n   int bags[4]; n   int flag = 0; n   for (int i = 0; i < 4; i++) { n     cin >> bags[i]; n   } n   if (bags[0] + bags[1] == bags[2] + bags[3]) { n     cout <<  YES n ; n   } else if (bags[0] + bags[2] == bags[1] + bags[3]) { n     cout <<  YES n ; n   } else if (bags[0] + bags[3] == bags[1] + bags[2]) { n     cout <<  YES n ; n   } else if (bags[0] + bags[1] + bags[2] == bags[3] || n              bags[0] + bags[2] + bags[3] == bags[1] || n              bags[0] + bags[1] + bags[3] == bags[2] || n              bags[3] + bags[1] + bags[2] == bags[0]) { n     cout <<  YES n ; n   } else { n     cout <<  NO n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main(void) { n   string s, t; n   cin >> s >> t; n   int ind = -1; n   for (int i = 0; i < s.length(); i++) { n     if (s[i] !=  0 ) n       break; n     else n       ind = i; n   } n   s = s.substr(ind + 1); n   ind = -1; n   for (int i = 0; i < t.length(); i++) { n     if (t[i] !=  0 ) n       break; n     else n       ind = i; n   } n   t = t.substr(ind + 1); n   if (s.length() != t.length()) { n     if (s.length() > t.length()) n       cout <<  > n ; n     else n       cout <<  < n ; n     return 0; n   } n   for (int i = 0; i < s.length(); i++) { n     if (s[i] != t[i]) { n       if (s[i] > t[i]) n         cout <<  > n ; n       else n         cout <<  < n ; n       return 0; n     } n   } n   cout <<  = n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, h; n   cin >> n >> h; n   vector<pair<int, int>> zeros; n   int a, b; n   for (int i = 0; i < n; i++) { n     cin >> a >> b; n     zeros.push_back(make_pair(a, b)); n   } n   int sol = 0; n   int pa, pb, ib, c1; n   pa = zeros[0].first; n   pb = zeros[0].second; n   ib = 0; n   c1 = 0; n   while (c1 < h) { n     int d = h - c1; n     if (ib + 1 >= n) { n       pb = pb + d; n       c1 = h; n       break; n     } n     if (d <= zeros[ib + 1].first - pb) { n       pb = pb + d; n       c1 = h; n       break; n     } n     c1 += zeros[ib + 1].first - pb; n     pb = zeros[ib + 1].second; n     ib++; n   } n   sol = pb - pa; n   for (int i = 1; i < n; i++) { n     int ci1 = zeros[i].first - zeros[i - 1].second; n     pa = zeros[i].first; n     if (h < ci1) { n       pb = zeros[i].second; n       ib = i; n       c1 = 0; n       while (c1 < h) { n         int d = h - c1; n         if (ib + 1 >= n) { n           pb = pb + (h - c1); n           c1 = h; n           break; n         } n         if (d <= zeros[ib + 1].first - pb) { n           pb = pb + (h - c1); n           c1 = h; n           break; n         } n         c1 += zeros[ib + 1].first - pb; n         pb = zeros[ib + 1].second; n         ib++; n       } n     } else { n       c1 = h - ci1; n       while (c1 < h) { n         int d = h - c1; n         if (ib + 1 >= n) { n           pb = pb + (h - c1); n           c1 = h; n           break; n         } n         if (d <= zeros[ib + 1].first - pb) { n           pb = pb + (h - c1); n           c1 = h; n           break; n         } n         c1 += zeros[ib + 1].first - pb; n         pb = zeros[ib + 1].second; n         ib++; n       } n     } n     sol = max(sol, pb - pa); n   } n   cout << sol; n } n


#include <bits/stdc++.h> n using namespace std; n int n; n string s; n int main() { n   cin >> n; n   cin >> s; n   for (int i = 0; i < n; ++i) { n     int c1 = 0, c2 = 0; n     for (int j = i - 1; j >= 0; --j) { n       if (s[j] ==  L ) { n         break; n       } n       if (s[j] ==  R ) { n         ++c1; n       } n     } n     for (int j = i; j < n - 1; ++j) { n       if (s[j] ==  R ) { n         break; n       } n       if (s[j] ==  L ) { n         ++c2; n       } n     } n     cout << max(c1, c2) + 1 <<    ; n   } n   cout << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   int n, m; n   cin >> n >> m; n   vector<long long> x(n), y(n); n   for (int i = 0; i < n; i++) { n     cin >> x[i] >> y[i]; n     y[i] -= x[i]; n   } n   vector<vector<int>> g(n); n   for (int i = 0; i < m; i++) { n     int foo, bar; n     cin >> foo >> bar; n     foo--; n     bar--; n     g[foo].push_back(bar); n     g[bar].push_back(foo); n   } n   long long sum_x = accumulate(x.begin(), x.end(), 0LL); n   vector<long long> sorted_y = y; n   sort(sorted_y.begin(), sorted_y.end()); n   vector<long long> pref(n + 1); n   for (int i = 0; i < n; i++) { n     pref[i + 1] = pref[i] + sorted_y[i]; n   } n   for (int i = 0; i < n; i++) { n     long long ans = x[i] * (n - 1) + (sum_x - x[i]); n     int pos = (int)(lower_bound(sorted_y.begin(), sorted_y.end(), y[i]) - n                     sorted_y.begin()); n     ans += pref[pos] + y[i] * (n - pos - 1); n     for (int j : g[i]) { n       long long cur = x[i] + x[j] + min(y[i], y[j]); n       ans -= cur; n     } n     if (i > 0) { n       cout <<    ; n     } n     cout << ans; n   } n   cout <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int m, n; n   scanf( %d%d , &n, &m); n   printf( %d n , m * n / 2); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int boxes; n   cin >> boxes; n   int arr[boxes]; n   for (int i = 0; i < boxes; i++) cin >> arr[i]; n   int shots; n   cin >> shots; n   int x, y; n   for (int i = 0; i < shots; i++) { n     cin >> x >> y; n     if (x == 1) { n       arr[x] += (arr[x - 1] - y); n       arr[0] = 0; n     } else if (x == boxes) { n       arr[boxes - 1] = 0; n       arr[x - 2] += (y - 1); n     } else { n       arr[x] += (arr[x - 1] - y); n       arr[x - 2] += (y - 1); n       arr[x - 1] = 0; n     } n   } n   for (int i = 0; i < boxes; i++) cout << arr[i] << endl; n } n


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


#include <bits/stdc++.h> n using namespace std; n using ll = long long; n struct node { n   char type; n   vector<node*> children; n   ll dp = 0; n   ll r = 0; n }; n node* get(string& s, int& idx) { n   node* v = new node(); n   if (s[idx] ==  * ) { n     v->type =  * ; n     idx++; n     return v; n   } else if (s[idx] ==  ( ) { n     idx++; n     while (true) { n       v->children.push_back(get(s, idx)); n       if (s[idx] ==  P  || s[idx] ==  S ) { n         v->type = s[idx]; n         idx++; n       } else if (s[idx] ==  ) ) { n         idx++; n         break; n       } else n         assert(false); n     } n   } else n     assert(false); n   return v; n } n const ll Z = 2e18; n ll sq(ll r) { n   if (r > Z / r) return Z; n   return min(r * r, Z); n } n void make_dp(node* v) { n   if (v->type ==  * ) { n     v->dp = 1; n   } else { n     for (node* w : v->children) { n       make_dp(w); n     } n     if (v->type ==  S ) { n       v->dp = Z; n       for (node* w : v->children) v->dp = min(v->dp, w->dp); n     } else if (v->type ==  P ) { n       v->dp = 0; n       for (node* w : v->children) v->dp += w->dp; n     } else n       assert(false); n   } n } n ll res = 0; n void make_resistances(node* v, bool good) { n   if (v->type ==  * ) { n     cout <<     << (good ? res : 0); n   } else if (v->type ==  P ) { n     for (node* w : v->children) { n       make_resistances(w, good); n     } n   } else if (v->type ==  S ) { n     for (node* w : v->children) { n       if (w->dp == v->dp) { n         make_resistances(w, good); n         good = false; n       } else { n         make_resistances(w, false); n       } n     } n   } else n     assert(false); n } n void solve() { n   string s; n   getline(cin, s); n   auto g = s.find(   ); n   ll R = stoll(s.substr(0, g)); n   s = s.substr(g + 1); n   s.resize(remove_if(s.begin(), s.end(), [&](char a) { return a ==    ; }) - n            s.begin()); n   int idx = 0; n   node* z = get(s, idx); n   make_dp(z); n   res = R * z->dp; n   cout <<  REVOLTING ; n   make_resistances(z, true); n   cout <<   n ; n } n int main() { n   ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); n   int T; n   string s; n   getline(cin, s); n   T = stoi(s); n   while (T--) solve(); n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 3e5 + 10; n int n, b[maxn]; n pair<int, int> a[maxn]; n int main() { n   scanf( %d , &n); n   for (int i = 1; i <= n; i++) { n     scanf( %d , &a[i].first); n     a[i].second = i; n   } n   sort(a + 1, a + 1 + n); n   reverse(a + 1, a + 1 + n); n   for (int i = 1; i <= n; i++) { n     b[i] = a[i].second * 2 - 1; n     if (i > 1) printf( %d %d n , b[i], b[i - 1]); n   } n   int res, cur = n, des; n   for (int i = 1; i <= n; i++) { n     res = a[i].second * 2; n     des = i + a[i].first - 1; n     if (des >= cur) { n       b[++cur] = res; n       printf( %d %d n , res, b[cur - 1]); n     } else n       printf( %d %d n , res, b[des]); n   } n } n


#include <bits/stdc++.h> n int main() { n   int n, x, y, z, sum1 = 0, sum2 = 0, sum3 = 0; n   scanf( %d , &n); n   while (n--) { n     scanf( %d %d %d , &x, &y, &z); n     sum1 = sum1 + x; n     sum2 = sum2 + y; n     sum3 = sum3 + z; n   } n   if (sum1 == 0 && sum2 == 0 && sum3 == 0) n     printf( YES ); n   else n     printf( NO ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long N = 22; n long long a[N]; n long long dp[N][N][N]; n long long DP(long long pos, long long sum, long long limit) { n   if (!pos) return 1; n   if (!limit && dp[pos][sum][limit] != -1) return dp[pos][sum][limit]; n   long long high = limit ? a[pos] : 9; n   long long ans = 0; n   for (long long i = 0; i <= high; i++) { n     if (sum == 3 && i != 0) continue; n     ans += DP(pos - 1, sum + (i != 0), limit && (i == high)); n   } n   if (!limit) dp[pos][sum][limit] = ans; n   return ans; n } n long long slove(long long n) { n   long long len = 0; n   while (n) { n     a[++len] = n % 10; n     n = n / 10; n   } n   return DP(len, 0, 1); n } n signed main() { n   memset(dp, -1, sizeof(dp)); n   long long T; n   cin >> T; n   while (T--) { n     long long L, R; n     cin >> L >> R; n     cout << slove(R) - slove(L - 1) <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long a[101000]; n long long pre[101000]; n map<long long, long long> Hash; n int main() { n   int n, k; n   scanf( %d%d , &n, &k); n   for (int i = 1; i <= n; i++) scanf( %I64d , &a[i]); n   for (int i = 1; i <= n; i++) pre[i] = pre[i - 1] + a[i]; n   Hash[0] = 1; n   long long count = 0; n   for (int i = 1; i <= n; i++) { n     if (abs(k) > 1) { n       for (long long p = 1; (p > 0 ? p : -p) <= 1e15; p *= k) { n         if (Hash[pre[i] - p]) count += Hash[pre[i] - p]; n       } n     } else { n       if (k == 1 || k == -1) { n         if (Hash[pre[i] - 1]) count += Hash[pre[i] - 1]; n         if (k == -1) { n           if (Hash[pre[i] + 1]) count += Hash[pre[i] + 1]; n         } n       } else { n         if (Hash[pre[i]]) count += Hash[pre[i]]; n       } n     } n     Hash[pre[i]] += 1; n   } n   cout << count << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int mat[4005][4005]; n int acum[4005][4005]; n int dp[4005][805]; n int step(int n, int k, int min_i, int max_i) { n   dp[n][k] = dp[min_i - 1][k - 1] + (acum[n][n] + acum[min_i - 1][min_i - 1] - n                                      acum[n][min_i - 1] - acum[min_i - 1][n]) / n                                         2; n   for (int i = min_i + 1; i <= max_i; i++) { n     if (dp[i - 1][k - 1] + (acum[n][n] + acum[i - 1][i - 1] - acum[n][i - 1] - n                             acum[i - 1][n]) / n                                2 < n         dp[n][k]) { n       dp[n][k] = dp[i - 1][k - 1] + (acum[n][n] + acum[i - 1][i - 1] - n                                      acum[n][i - 1] - acum[i - 1][n]) / n                                         2; n       min_i = i; n     } n   } n   return min_i; n } n void conquer(int min_n, int max_n, int k, int min_i, int max_i) { n   if (min_n > max_n) return; n   max_i = min(max_n, max_i); n   if (min_n == max_n) { n     step(min_n, k, min_i, max_i); n     return; n   } n   if (min_i == max_i) { n     for (int i = min_n; i <= max_n; i++) step(i, k, min_i, max_i); n     return; n   } n   int mid = (min_n + max_n) / 2; n   int i = step(mid, k, min_i, min(max_i, mid)); n   conquer(min_n, mid - 1, k, min_i, i); n   conquer(mid + 1, max_n, k, i, max_i); n } n char buff[100005]; n int main() { n   int n, k; n   scanf( %d %d n , &n, &k); n   for (int i = 1; i <= n; i++) { n     int acum2 = 0; n     gets(buff); n     for (int j = 1; j <= n; j++) { n       acum2 += buff[j * 2 - 2] -  0 ; n       acum[i][j] = acum[i - 1][j] + acum2; n     } n   } n   for (int i = 1; i <= n; i++) { n     dp[i][1] = acum[i][i] / 2; n   } n   for (int j = 2; j <= k; j++) { n     conquer(1, n, j, j - 1, n); n   } n   printf( %d n , dp[n][k]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n set<int> si; n int n, k; n bool solve(set<int> &cands, vector<int> &chosen) { n   if (cands.empty()) { n     if (chosen.empty()) { n       return false; n     } n     for (int i = 0; i < k; ++i) { n       int cnt = 0; n       for (const int num : chosen) { n         cnt += (((num >> i) & 0x1) ? -1 : 1); n       } n       if (cnt < 0) { n         return false; n       } n     } n     return true; n   } n   int val = *cands.begin(); n   bool ok = false; n   cands.erase(val); n   ok |= solve(cands, chosen); n   chosen.push_back(val); n   ok |= solve(cands, chosen); n   chosen.pop_back(); n   cands.insert(val); n   return ok; n } n int main(int argc, char **argv) { n   std::ios::sync_with_stdio(false); n   cin >> n >> k; n   for (int i = 0; i < n; ++i) { n     int val = 0; n     for (int j = 0; j < k; ++j) { n       int t; n       cin >> t; n       val |= (t << j); n     } n     si.insert(val); n   } n   vector<int> chosen; n   cout << (solve(si, chosen) ?  YES n  :  NO n ); n } n


#include <bits/stdc++.h> n using namespace std; n int a[5005], cnt, tmp, nxt, V[5005], j, n, i; n long long dp[2][5005], ans; n struct LS { n   int pos, val; n } ls[5005]; n inline bool cmp(LS aa, LS bb) { return aa.val < bb.val; } n int main() { n   cin >> n; n   for (i = 1; i <= n; ++i) cin >> a[i], ls[i].val = a[i], ls[i].pos = i; n   sort(ls + 1, ls + n + 1, cmp); n   ls[0].val = -1000000001; n   for (i = 1; i <= n; ++i) { n     if (ls[i].val != ls[i - 1].val) ++cnt, V[cnt] = ls[i].val; n     a[ls[i].pos] = cnt; n   } n   for (i = 1; i <= a[1]; ++i) dp[0][i] = V[a[1]] - V[i]; n   for (i = a[1] + 1; i <= cnt; ++i) dp[0][i] = 0; n   for (i = 2; i <= n; tmp ^= 1, ++i) { n     nxt = tmp ^ 1; n     dp[nxt][0] = 1e16; n     for (j = 1; j <= cnt; ++j) n       dp[nxt][j] = min(dp[nxt][j - 1], dp[tmp][j] + abs(V[a[i]] - V[j])); n   } n   for (ans = 1e16, j = 1; j <= cnt; ++j) ans = min(ans, dp[tmp][j]); n   cout << ans; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long int n, d; n   cin >> n >> d; n   int t; n   cin >> t; n   while (t--) { n     long long int x, y; n     cin >> x >> y; n     long long int eq1 = y * (n - d) - ((n - d) * (x - d)); n     long long int eq2 = (y - (n - d)) + (x - (n)); n     long long int eq3 = ((y - n) - (x - (n - d))); n     long long int eq4 = (y - d) + (x); n     if (eq1 >= 0 && eq2 <= 0 && eq3 <= 0 && eq4 >= 0) n       cout <<  YES n ; n     else n       cout <<  NO n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   bool flag = 0, flag2 = 0; n   long long n, m, c = 0; n   cin >> n >> m; n   for (long long i = 0; i < n; i++) { n     for (long long j = 0; j < m; j++) { n       if (i % 2 != 0 && c % 2 == 0 && j != m - 1) { n         cout <<  . ; n         flag = 1; n       } else if (i % 2 != 0 && c % 2 != 0 && j != 0) { n         cout <<  . ; n         flag2 = 1; n       } else n         cout <<  # ; n     } n     cout << endl; n     if (flag || flag2) c++; n     flag = 0, flag2 = 0; n   } n   return 0; n } n


#include<bits/stdc++.h> n using namespace std; n typedef long long int ll; n ll mod=1e9+7; n int main() n { n  int t; n  cin>>t; n  while(t--) n  { n   ll n,k; n   cin>>n>>k; n   ll ans=1; n   for(ll i=0;i<k;i++) n   { n    ans=(ans*n)%mod; n   } n   cout<<ans<<endl; n    n  } n }


#include <bits/stdc++.h> n using namespace std; n int _I() { n   int x; n   scanf( %d , &x); n   return x; n } n const int lmt = 10000000; n bool prime[lmt + 5]; n int rng = 1, v[lmt + 5]; n void sv() { n   v[0] = 2; n   for (int i = 4; i <= lmt; i += 2) prime[i] = 1; n   for (int i = 3; i <= lmt; i += 2) { n     if (!prime[i]) { n       v[rng++] = i; n       for (int j = i * 3; j <= lmt; j += i << 1) prime[j] = 1; n     } n   } n   prime[1] = 1; n } n int rev(int n) { n   int num = 0; n   while (n) { n     num *= 10; n     num += (n % 10); n     n /= 10; n   } n   return num; n } n void solve() { n   int n = _I(); n   int ans[100001]; n   for (int i = 13, j = 1; j <= 11184; i++) { n     if (i != rev(i) && !prime[i] && !prime[rev(i)]) ans[j++] = i; n   } n   cout << ans[n] <<   n ; n } n int main() { n   sv(); n   solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long double radian = 180 / acos(-1); n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   long long x, mini = LLONG_MAX, p; n   cin >> x; n   x %= 360; n   if (x < 0) x = 360 + x; n   for (int i = 0; i < 4; i++) { n     if (min(x, 360 - x) < mini) { n       mini = min(x, 360 - x); n       p = i; n     } n     x = (x + 270) % 360; n   } n   cout << p; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T> n inline T sqr(const T &a) { n   return a * a; n } n template <typename T> n inline int nread(vector<T> &a) { n   int n; n   cin >> n; n   a.clear(); n   a.resize(n); n   for (int i = 0; i < n; i++) cin >> a[i]; n   return n; n } n template <typename T> n inline void nwread(int n, vector<T> &a) { n   a.clear(); n   a.resize(n); n   for (int i = 0; i < n; i++) cin >> a[i]; n } n int main() { n   int n, k; n   cin >> n >> k; n   vector<int> a(n); n   for (int i = 0; i < n; i++) cin >> a[i]; n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < n; j++) { n       if (a[j] != 0) continue; n       cout << j + 1 <<    ; n       a[j] = -1; n       for (int l = 0; l <= j - k; l++) a[l]--; n       break; n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e5 + 100; n long long t; n long long n; n long long a[N]; n long long b[N]; n map<long long, long long> ma; n int main() { n   scanf( %lld , &t); n   while (t--) { n     ma.clear(); n     memset(a, 0, sizeof(a)); n     memset(b, 0, sizeof(b)); n     scanf( %lld , &n); n     for (long long i = 1; i <= n; i++) scanf( %lld , &a[i]); n     for (long long i = 1; i <= n; i++) scanf( %lld , &b[i]); n     if (a[1] != b[1]) { n       printf( NO n ); n       continue; n     } n     ma[a[1]] = 1; n     bool f = false; n     for (long long i = 2; i <= n; i++) { n       if (a[i] == b[i]) { n         ma[a[i]] = 1; n         continue; n       } n       if (f) break; n       long long cha = b[i] - a[i]; n       if (cha < 0) { n         if (ma[-1] > 0) { n         } else { n           f = true; n           break; n         } n       } n       if (cha > 0) { n         if (ma[1] > 0) { n         } else { n           f = true; n           break; n         } n       } n       if (f) break; n       ma[a[i]] = 1; n     } n     if (f) { n       printf( NO n ); n     } else { n       printf( YES n ); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n inline long long read() { n   long long x; n   cin >> x; n   return x; n } n const int MAX_N = 15; n const int MAX_F = (1 << (MAX_N - 2)) + 4; n int n, m, nQ, id[MAX_N]; n bool isEdge[MAX_N][MAX_N], isParent[MAX_N][MAX_N], isNotParent[MAX_N][MAX_N], n     isLCA[MAX_N][MAX_N][MAX_N]; n bool ok1[MAX_F][MAX_N], ok2[MAX_F][MAX_N], ok3[MAX_F][MAX_N][MAX_N], n     ok4[MAX_F][MAX_F], ok5[MAX_F][MAX_N]; n long long f[MAX_F][MAX_N]; n inline bool check_0(int u, int v, int r) { n   if (u == v && r != u) return 0; n   if ((u == 1 || v == 1) && r != 1) return 0; n   return 1; n } n void init(int nMask) { n   memset(ok1, 0, sizeof(ok1)); n   memset(ok2, 0, sizeof(ok2)); n   memset(ok3, 0, sizeof(ok3)); n   memset(ok4, 0, sizeof(ok4)); n   memset(ok5, 0, sizeof(ok5)); n   for (long long x = (0); x <= (nMask); ++x) n     for (long long i = (1); i <= (n); ++i) n       if (x & id[i]) { n         ok1[x][i] = 1; n         for (long long j = (1); j <= (n); ++j) n           if (j != i && (x & id[j])) n             if (isParent[j][i] || isNotParent[i][j]) { n               ok1[x][i] = 0; n               break; n             } n       } n   for (long long x = (0); x <= (nMask); ++x) n     for (long long i = (1); i <= (n); ++i) n       if (!(x & id[i])) { n         ok2[x][i] = 1; n         for (long long j = (1); j <= (n); ++j) n           if (x & id[j]) n             if (isParent[j][i] || isParent[i][j] || isEdge[j][i] || n                 isEdge[i][j]) { n               ok2[x][i] = 0; n               break; n             } n       } n   for (long long x = (0); x <= (nMask); ++x) n     for (long long i = (1); i <= (n); ++i) n       if (x & id[i]) n         for (long long r = (1); r <= (n); ++r) n           if (!(x & id[r])) { n             ok3[x][i][r] = 1; n             for (long long j = (1); j <= (n); ++j) n               if (j != i && (x & id[j])) n                 if (isEdge[r][j]) { n                   ok3[x][i][r] = 0; n                   break; n                 } n           } n   for (long long x = (0); x <= (nMask); ++x) { n     for (long long y = (0); y <= (nMask); ++y) n       if (!(x & y)) { n         ok4[x][y] = 1; n         for (long long i = (1); i <= (n); ++i) n           if (x & id[i]) { n             for (long long j = (1); j <= (n); ++j) n               if (y & id[j]) n                 if (isParent[i][j] || isParent[j][i] || isEdge[i][j] || n                     isEdge[j][i]) { n                   ok4[x][y] = 0; n                   break; n                 } n             if (!ok4[x][y]) break; n           } n       } n   } n   for (long long x = (0); x <= (nMask); ++x) n     for (long long r = (1); r <= (n); ++r) n       if (!(x & id[r])) { n         ok5[x][r] = 1; n         for (long long i = (1); i <= (n); ++i) n           if (ok5[x][r]) n             if (x & id[i]) n               for (long long j = (1); j <= (n); ++j) n                 if (x & id[j]) n                   if (i != j) n                     if (isLCA[r][i][j]) { n                       ok5[x][r] = 0; n                       break; n                     } n       } n } n inline bool check(int mask, int r, int newMask, int newR) { n   int x = mask - newMask; n   return ok1[newMask][newR] && ok2[x][newR] && ok3[newMask][newR][r] && n          ok4[newMask][x] && ok5[newMask][r]; n } n long long solve(int mask, int par) { n   if (mask == 0) return 1; n   if (f[mask][par] > -1) return f[mask][par]; n   long long ans = 0; n   int x = 0; n   do { n     if (x > 0 && __builtin_clz(x) == __builtin_clz(mask)) n       for (long long r = (1); r <= (n); ++r) n         if (x & id[r]) { n           if (!check(mask, par, x, r)) continue; n           long long t = solve(x - id[r], r) * solve(mask - x, par); n           ans += t; n         } n     x = (x + 1 + (~mask)) & mask; n   } while (x); n   return f[mask][par] = ans; n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.precision(9); n   cout.setf(ios::fixed, ios::floatfield); n   memset(isEdge, 0, sizeof(isEdge)); n   memset(isParent, 0, sizeof(isParent)); n   memset(isNotParent, 0, sizeof(isNotParent)); n   memset(isLCA, 0, sizeof(isLCA)); n   cin >> n >> m >> nQ; n   for (long long i = (1); i <= (n); ++i) id[i] = 1 << (i - 1); n   while (m--) { n     int u, v; n     cin >> u >> v; n     isEdge[u][v] = isEdge[v][u] = 1; n   } n   while (nQ--) { n     int u, v, r; n     cin >> u >> v >> r; n     if (!check_0(u, v, r)) { n       cout << 0 <<   n ; n       return 0; n     } n     if (r != u) isNotParent[u][v] = 1; n     if (r != v) isNotParent[v][u] = 1; n     isParent[r][u] = isParent[r][v] = 1; n     if (r != u && r != v) isLCA[r][u][v] = isLCA[r][v][u] = 1; n   } n   for (long long i = (1); i <= (n); ++i) isParent[i][i] = isNotParent[i][i] = 0; n   memset(f, -1, sizeof(f)); n   int mask = 0; n   for (long long i = (2); i <= (n); ++i) mask |= id[i]; n   init(mask); n   cout << solve(mask, 1) <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, k; n char playLog[1111]; n char posLet[3] = { W ,  D ,  L }; n int delta[3] = {1, 0, -1}; n int val[256]; n char dp[1111][2222]; n void clearArray() { n   for (int i = 0; i <= n + 1; i++) { n     for (int j = -k; j <= k; j++) dp[i][j + k] =  X ; n   } n } n void printAnswer() { n   int currentState = 0; n   for (int i = 0; i < n; i++) { n     char thisOne = dp[i][currentState + k]; n     printf( %c , thisOne); n     currentState += val[thisOne]; n   } n   printf(  n ); n } n int main() { n   val[ W ] = 1; n   val[ D ] = 0; n   val[ L ] = -1; n   scanf( %d%d , &n, &k); n   scanf( %s , playLog); n   clearArray(); n   dp[n][k + k] =  P ; n   for (int i = n - 1; i >= 0; i--) { n     for (int j = -(k - 1); j <= (k - 1); j++) { n       for (int q = 0; q < 3; q++) { n         if (playLog[i] !=  ?  && (playLog[i] != posLet[q])) continue; n         int target = j + delta[q]; n         if (dp[i + 1][target + k] ==  X ) continue; n         dp[i][j + k] = posLet[q]; n         break; n       } n     } n   } n   if (dp[0][0 + k] !=  X ) { n     printAnswer(); n     return 0; n   } n   clearArray(); n   dp[n][0] =  P ; n   for (int i = n - 1; i >= 0; i--) { n     for (int j = -(k - 1); j <= (k - 1); j++) { n       for (int q = 0; q < 3; q++) { n         if (playLog[i] !=  ?  && (playLog[i] != posLet[q])) continue; n         int target = j + delta[q]; n         if (dp[i + 1][target + k] ==  X ) continue; n         dp[i][j + k] = posLet[q]; n         break; n       } n     } n   } n   if (dp[0][0 + k] !=  X ) { n     printAnswer(); n     return 0; n   } n   printf( NO n ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int shape[4][4][2] = {{{1, 0}, {2, 0}, {1, 1}, {1, 2}}, n                             {{1, 0}, {2, 0}, {2, -1}, {2, 1}}, n                             {{0, 1}, {0, 2}, {-1, 2}, {1, 2}}, n                             {{0, 1}, {0, 2}, {1, 1}, {2, 1}}}; n int x, y; n struct node { n   char g[9][9]; n   int num; n   int sx, sy; n   node() { n     sx = sy = 0; n     memset(g, 0, sizeof(g)); n     num = 0; n   } n   void printNode() { n     for (int i = 0; i < y; i++) { n       for (int j = 0; j < x; j++) printf( %c , g[i][j] ? g[i][j] :  . ); n       cout <<    << endl; n     } n   } n }; n stack<node> q; n int ans; n node ansNode; n void bfs() { n   while (true) { n   The_End_Where_I_Began: n     if (q.empty()) break; n     node now = q.top(); n     q.pop(); n     if (now.num > ans) { n       ans = now.num; n       ansNode = now; n     } n     now.num++; n     char ch =  A  + now.num - 1; n     bool flag = true; n     for (int i = now.sy; i < y; i++) { n       int j = (now.sy == i ? now.sx : 0); n       for (; j < x; j++) { n         if (!now.g[i][j]) { n           flag = true; n           for (int k = 3; k >= 0; k--) { n             flag = true; n             for (int l = 0; l < 4; l++) { n               int tx = j + shape[k][l][1]; n               int ty = i + shape[k][l][0]; n               if (tx < 0 || tx >= x || ty < 0 || ty >= y || now.g[ty][tx]) { n                 flag = false; n                 break; n               } n             } n             if (flag) { n               node tNode = now; n               tNode.g[i][j] = ch; n               for (int l = 0; l < 4; l++) { n                 int tx = j + shape[k][l][1]; n                 int ty = i + shape[k][l][0]; n                 tNode.g[ty][tx] = ch; n               } n               tNode.sx = j + 1; n               tNode.sy = i; n               q.push(tNode); n             } n           } n           if (flag) goto The_End_Where_I_Began; n         } n       } n     } n   } n } n int main() { n   ans = -1; n   cin >> y >> x; n   while (!q.empty()) q.pop(); n   q.push(node()); n   bfs(); n   cout << ans << endl; n   ansNode.printNode(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n using ll = long long; n using pii = pair<int, int>; n using pll = pair<ll, ll>; n const ll inf = 1; n const ll mod = 1E9; n int main() { n   int n, m; n   scanf( %d %d , &n, &m); n   int colors = 1; n   vector<vector<int> > mset(n, vector<int>()); n   vector<vector<int> > G(n, vector<int>()); n   for (int i = 0; i < n; i++) { n     int lim; n     scanf( %d , &lim); n     for (int j = 0; j < lim; j++) { n       int x; n       scanf( %d , &x); n       mset[i].push_back(x - 1); n     } n     colors = max(colors, lim); n   } n   for (int i = 0; i < n - 1; i++) { n     int u, v; n     scanf( %d %d , &u, &v); n     G[u - 1].push_back(v - 1); n     G[v - 1].push_back(u - 1); n   } n   set<int> ms; n   vector<int> col(m, 0); n   vector<bool> vis(n, false); n   queue<int> Q; n   Q.push(0); n   vis[0] = true; n   while (!Q.empty()) { n     int u = Q.front(); n     Q.pop(); n     for (auto j : mset[u]) { n       if (col[j] != 0) ms.insert(col[j]); n     } n     int now = 1; n     for (auto j : mset[u]) { n       if (col[j] == 0) { n         while (ms.find(now) != ms.end()) now++; n         col[j] = now; n         now++; n       } n     } n     for (auto v : G[u]) { n       if (!vis[v]) { n         vis[v] = true; n         Q.push(v); n       } n     } n     ms.clear(); n   } n   printf( %d n , colors); n   for (int i = 0; i < m; i++) { n     if (col[i] == 0) col[i] = 1; n     printf( %d  , col[i]); n   } n   printf(  n ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   string s; n   cin >> s; n   cout << s; n   reverse(s.begin(), s.end()); n   cout << s; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T> n inline T Max(T a, T b) { n   return a > b ? a : b; n } n template <typename T> n inline T Min(T a, T b) { n   return a < b ? a : b; n } n template <typename T> n inline void checkMax(T &a, T b) { n   if (a < b) a = b; n } n template <typename T> n inline void checkMin(T &a, T b) { n   if (a > b) a = b; n } n template <typename T> n inline T Abs(T a) { n   return a > 0 ? a : -a; n } n int a; n string s; n int num[4005]; n int SumR[4005]; n int Cnt[50000]; n long long ans; n void work() { n   cin >> s; n   int len = s.length(); n   for (int i = 0; i < len; i++) { n     num[i] = s[i] -  0 ; n   } n   for (int i = 0; i < len; i++) { n     if (i == 0) n       SumR[i] = num[i]; n     else n       SumR[i] = SumR[i - 1] + num[i]; n   } n   memset(Cnt, 0, sizeof(Cnt)); n   for (int i = 0; i < len; i++) { n     for (int j = i; j < len; j++) { n       int tmp = SumR[j]; n       if (i != 0) tmp -= SumR[i - 1]; n       Cnt[tmp]++; n     } n   } n   ans = 0; n   for (int i = 0; i < len; i++) { n     for (int j = i; j < len; j++) { n       int tmp = SumR[j]; n       if (i != 0) tmp -= SumR[i - 1]; n       if (tmp != 0) { n         if (a % tmp == 0) { n           long long div = a / tmp; n           if (div <= SumR[len - 1]) { n             ans += Cnt[div]; n           } n         } n       } else { n         if (a == 0) { n           ans += len * (len + 1) / 2; n         } n       } n     } n   } n   cout << ans << endl; n } n int main() { n   while (cin >> a) { n     work(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int t; n   cin >> t; n   while (t--) { n     int n, k; n     cin >> n >> k; n     string s; n     cin >> s; n     sort(s.begin(), s.end()); n     map<char, int> store; n     for (int i = 0; i < n; i++) { n       store[s[i]]++; n     } n     if (store.size() == 1) { n       int temp = n / k; n       if (n % k != 0) temp++; n       for (int i = 0; i < temp; i++) cout << s[0]; n     } else if (store.size() == 2 && store[s[0]] == k) { n       cout << s[0]; n       int temp = n / k; n       if (n % k != 0) temp++; n       for (int i = 0; i < temp - 1; i++) cout << s[k]; n     } else { n       if (s[0] != s[k - 1]) n         cout << s[k - 1]; n       else { n         for (int i = k - 1; i < n; i++) cout << s[i]; n       } n     } n     cout <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n set<int> v; n multiset<int> diff; n void clean() { n   if (v.size() < 2) { n     cout << 0 << endl; n     return; n   } n   cout << *v.rbegin() - *v.begin() - *diff.rbegin() << endl; n } n int main() { n   v.clear(); n   diff.clear(); n   int n, q; n   cin >> n >> q; n   for (int i = 0; i < n; i++) { n     int x; n     cin >> x; n     v.insert(x); n   } n   for (auto it = v.begin(); next(it) != v.end(); it++) n     diff.insert(*next(it) - *it); n   clean(); n   while (q--) { n     int t, x; n     cin >> t >> x; n     if (t == 1) { n       v.insert(x); n       set<int>::iterator it = v.find(x); n       if (it == v.begin()) n         diff.insert(*next(it) - *it); n       else if (next(it) == v.end()) n         diff.insert(*it - *prev(it)); n       else { n         diff.erase(diff.find(*next(it) - *prev(it))); n         diff.insert(*next(it) - *it); n         diff.insert(*it - *prev(it)); n       } n     } else { n       set<int>::iterator it = v.find(x); n       if (v.size() >= 2) { n         if (it == v.begin()) n           diff.erase(diff.find(*next(it) - *it)); n         else if (next(it) == v.end()) n           diff.erase(diff.find(*it - *prev(it))); n         else { n           diff.erase(diff.find(*it - *prev(it))); n           diff.erase(diff.find(*next(it) - *it)); n           diff.insert(*next(it) - *prev(it)); n         } n       } n       v.erase(it); n     } n     clean(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int const oo = 1e9, bound = 1e6, mod = oo + 7; n long long const OO = 1e18; n int n, m, k, aa[20]; n int cc[20][20]; n long long dp[18][1 << 18]; n long long rec(int idx, int msk) { n   long long &ret = dp[idx][msk]; n   if (ret != -1) return ret; n   int cop = msk, e = 0; n   while (cop) { n     if (cop & 1) e++; n     cop >>= 1; n   } n   if (e == m) return ret = 0; n   for (int(i) = 0; (i) < (n); (i)++) { n     if (!((msk >> i) & 1)) n       ret = max(ret, n                 rec(i, (1 << i) | msk) + aa[i] + (msk != 0 ? cc[idx][i] : 0)); n   } n   return ret; n } n int main() { n   memset(dp, -1, sizeof dp); n   scanf( %d , &n), scanf( %d , &m), scanf( %d , &k); n   for (int(i) = 0; (i) < (n); (i)++) scanf( %d , &aa[i]); n   for (int(i) = 0; (i) < (k); (i)++) { n     int a, b, c; n     scanf( %d , &a), scanf( %d , &b), scanf( %d , &c); n     a--, b--; n     cc[a][b] = c; n   } n   long long mx = rec(0, 0); n   printf( %lld n , mx); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long int INF = 98769876987698889LL; n const int MOD = 1e9 + 7; n const int dx8[] = {1, 1, 0, -1, -1, -1, 0, 1}; n const int dy8[] = {0, 1, 1, 1, 0, -1, -1, -1}; n const int dx4[] = {1, -1, 0, 0}; n const int dy4[] = {0, 0, 1, -1}; n const int N = 2e5 + 5; n long long int dp1[N], a[N], f[N], dp2[N], s[N]; n int n; n vector<int> G[N]; n void dfs(int x, int pa) { n   if (G[x].size() == 1 and G[x][0] == pa) { n     s[x] = a[x]; n     return; n   } n   s[x] = a[x]; n   for (auto k : G[x]) { n     if (k == pa) continue; n     dfs(k, x); n     s[x] += s[k]; n   } n } n void dfs2(int x, int pa) { n   if (G[x].size() == 1 and G[x][0] == pa) { n     f[x] = 0; n     return; n   } n   f[x] = 0; n   vector<long long int> v; n   for (auto k : G[x]) { n     if (k == pa) continue; n     dfs2(k, x); n     if (s[k] >= dp1[k]) { n       v.push_back(s[k]); n     } else { n       v.push_back(dp1[k]); n     } n   } n   sort(v.begin(), v.end()); n   dp1[x] = v[v.size() - 1]; n   if (v.size() > 1) { n     f[x] = 1; n     dp2[x] = v[v.size() - 2]; n   } n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   ; n   cin >> n; n   for (int i = 1; i <= n; i++) cin >> a[i]; n   for (int i = 0; i < N; i++) { n     dp1[i] = -INF; n     dp2[i] = -INF; n   } n   for (int i = 2; i <= n; 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 == 1) { n     cout <<  Impossible  << endl; n     return 0; n   } n   dfs(1, 0); n   dfs2(1, 0); n   int fl = 0; n   for (int i = 1; i <= n; i++) { n     if (f[i]) fl = 1; n   } n   if (!fl) { n     cout <<  Impossible  << endl; n     return 0; n   } n   long long int ans = -INF; n   for (int i = 1; i <= n; i++) { n     if (f[i]) { n       ans = max(ans, dp1[i] + dp2[i]); n     } n   } n   cout << ans; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int t; n   cin >> t; n   for (int i = 0; i < t; ++i) { n     int n, m, h; n     cin >> n >> m; n     long long int sum = 0; n     for (int j = 0; j < n; ++j) { n       int x; n       cin >> x; n       if (j != 0) sum = sum + x; n       if (j == 0) h = x; n     } n     if (m - h < sum) n       h = m; n     else n       h = h + sum; n     cout << h <<   n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n long long n, k, p; n string s; n vector<long long> mem; n long long solve(int i) { n   if (mem[i] != -1) return mem[i]; n   if (i == 0) return 0; n   long long tmp = stoll(s.substr(i - 1, 1)); n   long long res = 1ll << 62; n   for (int j = i - 1; tmp < n; --j) { n     if (s[j] !=  0  || j == i - 1) { n       long long tmptmp = solve(j); n       if (tmptmp < ((1ll << 62) - tmp) / n && tmptmp > -1) n         res = min(res, tmp + n * tmptmp); n     } n     if (j - 1 < 0) break; n     tmp = stoll(s.substr(j - 1, i - j + 1)); n   } n   mem[i] = res; n   return res; n } n int main(int argc, char const *argv[]) { n   cin >> n; n   cin >> s; n   k = s.length(); n   if (n < 11) { n     long long res = 0; n     for (int i = 0; i < k; ++i) { n       res *= n; n       res += stoi(s.substr(i, 1)); n     } n     cout << res << endl; n     return 0; n   } n   mem = vector<long long>(k + 1, -1); n   cout << solve(k) << endl; n   return 0; n } n


#include <bits/stdc++.h> n #pragma GCC optimize( Ofast ) n #pragma GCC target( avx,avx2,fma ) n #pragma GCC optimization( unroll-loops ) n using namespace std; n const int MX = 2000006; n char a[MX], b[MX]; n char arb[MX], abr[MX], ba[MX]; n int parb[MX], zabr[MX], zba[MX], pos[MX]; n void compute_pi(int n, char s[], int pi[]) { n   pi[0] = 0; n   for (int i = 1; i < n; i++) { n     int j = pi[i - 1]; n     while (j) { n       if (s[j] == s[i]) { n         pi[i] = j + 1; n         break; n       } n       j = pi[j - 1]; n     } n     if (j == 0) pi[i] = (s[i] == s[0]) ? 1 : 0; n   } n } n void compute_z(int n, char s[], int z[]) { n   int l = 0, r = 0; n   for (int i = 1; i < n; i++) { n     if (i <= r) z[i] = min(z[i - l], r - i + 1); n     for (int j = i + z[i], p = z[i]; j < n; j++, p++) { n       if (s[p] == s[j]) n         z[i]++; n       else n         break; n     } n     if (i + z[i] - 1 > r) l = i, r = i + z[i] - 1; n   } n } n void calc(int n, int m) { n   compute_pi(n + m + 1, arb, parb); n   compute_z(n + m + 1, abr, zabr); n   compute_z(n + m + 1, ba, zba); n   memset(pos, -1, sizeof(pos)); n   for (int i = n + 1, p = 0; i <= n + m; i++, p++) { n     if (parb[i] < 1) continue; n     int j = p - parb[i] + 1; n     if (pos[p] == -1) pos[p] = j; n   } n } n void make(int n, int m) { n   char sep[] =    ; n   sep[0] = 31; n   strcpy(ba, b); n   strcat(ba, sep); n   strcat(ba, a); n   strcpy(arb, a); n   reverse(arb, arb + n); n   strcat(arb, sep); n   strcat(arb, b); n   strcpy(abr, a); n   reverse(b, b + m); n   strcat(abr, sep); n   strcat(abr, b); n } n int main() { n   gets(a), gets(b); n   int n = strlen(a), m = strlen(b); n   make(n, m); n   calc(n, m); n   int x = -1, y = -1; n   for (int i = m + 2; (i <= n + m && n == m); i++) { n     int p = pos[n + m - i]; n     if (p < 0 || p > zba[i]) continue; n     if (zabr[n + 1] >= i - m - 1) x = i - m - 2, y = i - m - 1 + p; n   } n   printf( %d %d n , x, y); n   return 0; n } n


#include <bits/stdc++.h> n #pragma GCC target( avx2 ) n #pragma GCC optimization( O3 ) n #pragma GCC optimization( unroll-loops ) n using namespace std; n inline bool EQ(double a, double b) { return fabs(a - b) < 1e-9; } n inline int msbp(int x) { return 31 - __builtin_clz(x); } n inline int msb(int x) { return 1 << msbp(x); } n const int INF = 0x3f3f3f3f; n struct hash_pair { n   template <class T1, class T2> n   size_t operator()(const pair<T1, T2>& p) const { n     auto hash1 = hash<T1>{}(p.first); n     auto hash2 = hash<T2>{}(p.second); n     return hash1 ^ hash2; n   } n }; n const int N = 1e6 + 10; n int parent[N], rnk[N]; n int find(int x) { return parent[x] = (x == parent[x] ? x : find(parent[x])); } n bool disjoint(int x, int y) { return find(x) != find(y); } n void join(int x, int y) { n   if (!disjoint(x, y)) return; n   int xPar = find(x), yPar = find(y); n   int newPar, newChild; n   if (rnk[xPar] > rnk[yPar]) { n     newPar = xPar, newChild = yPar; n   } else if (rnk[xPar] < rnk[yPar]) { n     newPar = yPar, newChild = xPar; n   } else { n     newPar = xPar, newChild = yPar; n     ++rnk[xPar]; n   } n   parent[newChild] = newPar; n } n int n, m, b[N]; n pair<int, int> a[N]; n vector<pair<int, int>> all_vals; n map<int, vector<int>> s; n vector<int> g[N]; n int d[N], dp[N]; n void process_eq(vector<pair<int, int>>& cur) { n   map<int, int> val_map; n   for (auto [val, id] : cur) { n     if (val_map.count(val)) { n       join(id, val_map[val]); n     } else { n       val_map[val] = id; n     } n   } n } n void process(vector<int>& cur) { n   sort(cur.begin(), cur.end()); n   for (int i = 0; i < (cur.size() - 1); ++i) { n     if (cur[i] != cur[i + 1]) { n       g[cur[i]].push_back(cur[i + 1]); n       ++d[cur[i + 1]]; n     } n   } n } n signed main() { n   ios::sync_with_stdio(false); n   std::iota(parent, parent + N, 0); n   scanf( %d %d , &n, &m); n   for (int i = 0; i < (n); ++i) { n     for (int j = 0; j < (m); ++j) { n       scanf( %d , &a[i * m + j].first); n       s[a[i * m + j].first].push_back(i * m + j); n     } n   } n   vector<pair<int, int>> curm(m), curn(n); n   for (int i = 0; i < (n); ++i) { n     for (int j = 0; j < (m); ++j) curm[j] = {a[i * m + j].first, i * m + j}; n     process_eq(curm); n   } n   for (int j = 0; j < (m); ++j) { n     for (int i = 0; i < (n); ++i) curn[i] = {a[i * m + j].first, i * m + j}; n     process_eq(curn); n   } n   for (auto [val, ids] : s) { n     int cur = 1; n     for (int id : ids) { n       if (a[find(id)].second == 0) { n         all_vals.push_back({val, cur}); n         a[find(id)].second = cur++; n       } n       a[id].second = a[find(id)].second; n     } n   } n   sort(all_vals.begin(), all_vals.end()); n   map<pair<int, int>, int> id_map; n   for (int i = 0; i < (all_vals.size()); ++i) id_map[all_vals[i]] = i; n   for (int i = 0; i < (n); ++i) n     for (int j = 0; j < (m); ++j) b[i * m + j] = id_map[a[i * m + j]]; n   vector<int> ccurm(m), ccurn(n); n   for (int i = 0; i < (n); ++i) { n     for (int j = 0; j < (m); ++j) ccurm[j] = b[i * m + j]; n     process(ccurm); n   } n   for (int j = 0; j < (m); ++j) { n     for (int i = 0; i < (n); ++i) ccurn[i] = b[i * m + j]; n     process(ccurn); n   } n   queue<int> nxt; n   for (int x = 0; x < (all_vals.size()); ++x) { n     if (d[x] == 0) { n       nxt.push(x); n       dp[x] = 1; n     } n   } n   while (!nxt.empty()) { n     int cur = nxt.front(); n     nxt.pop(); n     for (auto v : g[cur]) { n       dp[v] = max(dp[v], dp[cur] + 1); n       --d[v]; n       if (d[v] == 0) { n         nxt.push(v); n       } n     } n   } n   for (int i = 0; i < (n); ++i) { n     for (int j = 0; j < (m); ++j) cout << dp[b[i * m + j]] <<    ; n     cout <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n #pragma comment(linker,  /STACK:64777216 ) n using namespace std; n const long long maxn = 100001; n const long long maxk = 200001; n const long long maxx = 10001; n const long long secret_number = 87468267; n const long long inf = 1e18; n const int iinf = 2147483647 / 2; n const double pi = 3.141592653589793238462643383279502884; n const double eps = 1e-7; n const string something_very_long = n      aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa  n      aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa  n      aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa  n      aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa  n      aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa  n      aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa  n      aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa  n      aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa  n      aaaaaaaa ; n const string govno =  g ; n const int maxp = 101; n int n, p, q; n int pr[maxp]; n int main() { n   cin.sync_with_stdio(0); n   cin >> n >> p >> q; n   string s; n   cin >> s; n   for (int i = 0; i < maxp; ++i) pr[i] = -2; n   pr[0] = 0; n   queue<int> qq; n   qq.push(0); n   while (!qq.empty()) { n     int cur = qq.front(); n     qq.pop(); n     if ((cur + p <= s.length()) && (pr[cur + p] == -2)) n       pr[cur + p] = cur, qq.push(cur + p); n     if ((cur + q <= s.length()) && (pr[cur + q] == -2)) n       pr[cur + q] = cur, qq.push(cur + q); n   } n   stack<string> v; n   int cur = s.length(); n   if (pr[cur] == -2) { n     cout << -1; n     return 0; n   } n   while (true) { n     if (cur != 0) { n       v.push(s.substr(pr[cur], cur - pr[cur])); n       cur = pr[cur]; n     } else n       break; n   } n   cout << v.size() <<   n ; n   while (!v.empty()) { n     string aa = v.top(); n     v.pop(); n     cout << aa <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n using ll = long long; n struct p { n   ll v; n   int i; n } pp[3]; n bool cmp(p x, p y) { return x.v < y.v; } n void dd() { n   sort(pp, pp + 3, cmp); n   ll y = 2 * pp[2].v - pp[0].v - pp[1].v; n   cout << y << endl; n   cout.flush(); n   int x; n   cin >> x; n   if (x == 0) { n     return; n   } else { n     if (pp[0].i == x) { n       cout << pp[2].v - pp[1].v << endl; n       cout.flush(); n       int g; n       cin >> g; n       return; n     } else if (pp[1].i == x) { n       cout << pp[2].v - pp[0].v << endl; n       cout.flush(); n       int g; n       cin >> g; n     } else { n       pp[2].v += y; n       dd(); n     } n   } n } n int main() { n   ll a, b, c; n   cin >> a >> b >> c; n   pp[0].v = a; n   pp[0].i = 1; n   pp[1].v = b; n   pp[1].i = 2; n   pp[2].v = c; n   pp[2].i = 3; n   cout <<  First  << endl; n   cout.flush(); n   dd(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   scanf( %d , &n); n   vector<vector<int>> g(n); n   for (int i = 0; (i) < (int)(n - 1); (i)++) { n     int a, b; n     scanf( %d%d , &a, &b); n     a--, b--; n     g[a].push_back(b); n     g[b].push_back(a); n   } n   int ans = 0; n   for (int s = 0; (s) < (int)(n); (s)++) { n     for (int t = s + 1; t < n; t++) { n       vector<int> tmp, path; n       function<void(int, int)> dfs = [&](int u, int prev) { n         if (u == t) { n           path = tmp; n           return; n         } n         for (auto v : g[u]) n           if (v != prev) { n             tmp.push_back(v); n             dfs(v, u); n             tmp.pop_back(); n           } n       }; n       tmp.push_back(s); n       dfs(s, -1); n       vector<bool> ng(n); n       for (int p : path) ng[p] = true; n       int len = (int)path.size() - 1; n       vector<int> dp1(n), dp2(n); n       function<void(int, int)> dfs2 = [&](int u, int prev) { n         vector<int> pro; n         for (auto v : g[u]) n           if (v != prev) { n             dfs2(v, u); n             dp1[u] = max(dp1[u], dp1[v]); n             dp1[u] = max(dp1[u], dp2[v]); n             if (!ng[v]) pro.push_back(dp2[v]); n           } n         if (pro.size() == 0 || ng[u]) return; n         sort(pro.rbegin(), pro.rend()); n         dp2[u] = pro[0] + 1; n         if (pro.size() > 1) { n           dp1[u] = max(dp1[u], pro[0] + pro[1] + 2); n         } n       }; n       dfs2(s, -1); n       ans = max(ans, len * dp1[s]); n     } n   } n   printf( %d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n const double pi = acos(-1.0); n int main() { n   int n; n   double s, f, r, v, t; n   scanf( %d %lf %lf , &n, &r, &v); n   double p = 2 * pi * r; n   double tp = p / v; n   while (n--) { n     scanf( %lf %lf , &s, &f); n     double x = f - s; n     double cyc = floor(x / p); n     double rem = (x - cyc * p) / r / 2; n     double lo = 0, hi = pi; n     for (int i = 0; i < 60; ++i) { n       double mid = (lo + hi) / 2; n       if (mid + sin(mid) < rem) n         lo = mid; n       else n         hi = mid; n     } n     t = cyc * tp + 2 * lo * r / v; n     printf( %.8lf n , t); n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e5 + 10; n const int K = 18; n long long a[N], tree[N << 2], lazy[N << 2]; n vector<int> v[N]; n int anc[N][K], h[N], sz[N]; n int c[N], pos[N]; n int ct; n void build(int node, int st, int end) { n   if (st == end) { n     tree[node] = c[st]; n     return; n   } n   long long mid = st + end >> 1LL; n   build(node << 1, st, mid); n   build((node << 1) + 1, mid + 1, end); n   tree[node] = tree[node << 1] + tree[(node << 1) + 1]; n } n void update(int node, int st, int end, int l, int r, long long val) { n   if (lazy[node]) { n     tree[node] += (end - st + 1) * lazy[node]; n     if (st != end) { n       lazy[node << 1] += lazy[node]; n       lazy[(node << 1) + 1] += lazy[node]; n     } n     lazy[node] = 0; n   } n   if (st > end || r < st || end < l) return; n   if (l <= st && end <= r) { n     tree[node] += (end - st + 1) * val; n     if (st != end) { n       lazy[node << 1] += val; n       lazy[(node << 1) + 1] += val; n     } n     return; n   } n   long long mid = st + end >> 1LL; n   update(node << 1, st, mid, l, r, val); n   update((node << 1) + 1, mid + 1, end, l, r, val); n   tree[node] = tree[node << 1] + tree[(node << 1) + 1]; n } n long long query(int node, int st, int end, int l, int r) { n   if (st > end || r < st || end < l) return 0; n   if (lazy[node]) { n     tree[node] += (end - st + 1) * lazy[node]; n     if (st != end) { n       lazy[node << 1] += lazy[node]; n       lazy[(node << 1) + 1] += lazy[node]; n     } n     lazy[node] = 0; n   } n   if (l <= st && end <= r) return tree[node]; n   long long mid = st + end >> 1LL; n   return (query(node << 1, st, mid, l, r) + n           query((node << 1) + 1, mid + 1, end, l, r)); n } n void dfs(int s, int P) { n   c[ct + 1] = a[s]; n   pos[s] = ++ct; n   anc[s][0] = P; n   h[s] = h[P] + 1; n   sz[s]++; n   for (int i = 1; i < K; i++) anc[s][i] = anc[anc[s][i - 1]][i - 1]; n   for (auto i : v[s]) n     if (i != P) dfs(i, s), sz[s] += sz[i]; n } n int LCA(int x, int y) { n   int d = h[y] - h[x]; n   if (d < 0) swap(x, y); n   d = abs(d); n   for (int i = K - 1; i >= 0; i--) n     if ((1 << i) & d) y = anc[y][i]; n   if (x == y) return x; n   for (int i = K - 1; i >= 0; i--) n     if (anc[x][i] != anc[y][i]) x = anc[x][i], y = anc[y][i]; n   return anc[x][0]; n } n int lift(int x, int d) { n   for (int i = K - 1; i >= 0; i--) n     if ((1 << i) & d) x = anc[x][i]; n   return x; n } n int main() { n   ios_base ::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   int n, q; n   cin >> n >> q; n   for (int i = 1; i <= n; i++) cin >> a[i]; n   for (int i = 1; i < n; i++) { n     int x, y; n     cin >> x >> y; n     v[x].push_back(y); n     v[y].push_back(x); n   } n   dfs(1, 0); n   build(1, 1, n); n   int t, val, l, r, x, ro = 1, yo, vo, go; n   long long ho; n   for (int i = 0; i < q; i++) { n     cin >> t; n     if (t == 1) n       cin >> ro; n     else if (t == 2) { n       cin >> l >> r >> x; n       yo = LCA(l, r), vo = LCA(l, ro), go = LCA(r, ro); n       if (h[yo] < h[vo]) yo = vo; n       if (h[yo] < h[go]) yo = go; n       if (yo == ro) { n         update(1, 1, n, 1, n, x); n       } else if (LCA(yo, ro) == yo) { n         update(1, 1, n, 1, n, x); n         yo = lift(ro, h[ro] - h[yo] - 1); n         update(1, 1, n, pos[yo], pos[yo] + sz[yo] - 1, -x); n       } else { n         update(1, 1, n, pos[yo], pos[yo] + sz[yo] - 1, x); n       } n     } else { n       cin >> l; n       if (l == ro) { n         cout << query(1, 1, n, 1, n) <<   n ; n       } else if (LCA(ro, l) == l) { n         ho = query(1, 1, n, 1, n); n         yo = lift(ro, h[ro] - h[l] - 1); n         cout << ho - query(1, 1, n, pos[yo], pos[yo] + sz[yo] - 1) <<   n ; n       } else { n         cout << query(1, 1, n, pos[l], pos[l] + sz[l] - 1) <<   n ; n       } n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 1e5 + 50; n const int LOG = 19; n const int INF = 1e9; n int m, p[MAXN], par[12][MAXN], nxt[MAXN], id[MAXN], ss[MAXN], ans, l[MAXN], n     r[MAXN]; n int tt[LOG][MAXN], lc[LOG][MAXN]; n string s[14]; n pair<pair<int, int>, int> vec[MAXN]; n int lcp(int a, int b) { n   int ta = a, tb = b; n   for (int i = LOG - 1; i >= 0 && a < nxt[ta] && b < nxt[tb]; i--) n     if (a + (1 << i) <= nxt[ta] && b + (1 << i) <= nxt[tb] && n         tt[i][a] == tt[i][b]) n       a += 1 << i, b += 1 << i; n   return a - ta; n } n bool ok(int sz, int ind) { n   int tl = ind, tr = ind; n   for (int w = LOG - 1; w >= 0; w--) n     if (tr + (1 << w) < p[m] && lc[w][tr] >= sz) tr += 1 << w; n   tr++; n   for (int w = LOG - 1; w >= 0; w--) n     if (tl - (1 << w) >= 0 && lc[w][tl - (1 << w)] >= sz) tl -= (1 << w); n   for (int i = 1; i < m; i++) n     if (par[i][tr] - par[i][tl] > r[i]) return false; n   return true; n } n bool ok2(int sz, int ind) { n   int tl = ind, tr = ind; n   for (int w = LOG - 1; w >= 0; w--) n     if (tr + (1 << w) < p[m] && lc[w][tr] >= sz) tr += 1 << w; n   tr++; n   for (int w = LOG - 1; w >= 0; w--) n     if (tl - (1 << w) >= 0 && lc[w][tl - (1 << w)] >= sz) tl -= (1 << w); n   for (int i = 1; i < m; i++) n     if (par[i][tr] - par[i][tl] < l[i]) return false; n   return true; n } n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   cin >> s[0] >> m; n   m++; n   for (int i = 1; i < m; i++) n     cin >> s[i] >> l[i] >> r[i], p[i] = p[i - 1] + (int)s[i - 1].size(); n   p[m] = p[m - 1] + (int)s[m - 1].size(); n   for (int i = 0; i < m; i++) n     for (int j = 0; j < (int)s[i].size(); j++) tt[0][p[i] + j] = s[i][j]; n   for (int w = 1; w < LOG; w++) { n     for (int i = 0; i < m; i++) n       for (int j = 0; j < (int)s[i].size(); j++) n         if (j + (1 << (w - 1)) >= (int)s[i].size()) n           vec[p[i] + j] = {{tt[w - 1][p[i] + j], -1}, p[i] + j}; n         else n           vec[p[i] + j] = { n               {tt[w - 1][p[i] + j], tt[w - 1][p[i] + j + (1 << (w - 1))]}, n               p[i] + j}; n     sort(vec, vec + p[m]); n     for (int i = 1; i < p[m]; i++) n       if (vec[i].first == vec[i - 1].first) n         tt[w][vec[i].second] = tt[w][vec[i - 1].second]; n       else n         tt[w][vec[i].second] = i; n   } n   for (int i = 0; i < p[m]; i++) ss[i] = vec[i].second; n   for (int i = 0; i < m; i++) n     for (int j = 0; j < (int)s[i].size(); j++) n       id[p[i] + j] = i, nxt[p[i] + j] = p[i + 1]; n   for (int i = 0; i < p[m]; i++) n     for (int j = 1; j < m; j++) n       par[j][i + 1] = par[j][i] + bool(id[ss[i]] == j); n   for (int i = 0; i < p[m] - 1; i++) lc[0][i] = lcp(ss[i], ss[i + 1]); n   for (int i = 1; i < LOG; i++) n     for (int j = 0; j < p[m] - 1; j++) n       if (j + (1 << (i - 1)) >= p[m] - 1) n         lc[i][j] = lc[i - 1][j]; n       else n         lc[i][j] = min(lc[i - 1][j], lc[i - 1][j + (1 << (i - 1))]); n   int mn = 0; n   for (int i = 0; i < p[m]; i++) { n     if (id[ss[i]]) { n       mn = min(mn, lc[0][i]); n       continue; n     } n     int sz = nxt[ss[i]] - ss[i]; n     int cur = ss[i]; n     for (int w = LOG - 1; w >= 0 && cur < nxt[ss[i]]; w--) n       if (cur + (1 << w) <= nxt[ss[i]] && !ok(cur + (1 << w) - ss[i], i)) n         cur += (1 << w); n     int cur2 = ss[i]; n     for (int w = LOG - 1; w >= 0 && cur2 < nxt[ss[i]]; w--) n       if (cur2 + (1 << w) <= nxt[ss[i]] && ok2(cur2 + (1 << w) - ss[i], i)) n         cur2 += (1 << w); n     cur = max(cur, ss[i] + mn); n     ans += max(0, cur2 - cur); n     mn = lc[0][i]; n   } n   cout << ans <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, arr[110], c1, c0; n int main() { n   scanf( %d , &n); n   for (int i = 0; i < n; ++i) scanf( %d , &arr[i]), c1 += arr[i]; n   int out = 0; n   for (int i = 0; i < n; ++i) { n     c0 += (!arr[i]); n     out = max(out, c0 + c1); n     c1 -= arr[i]; n   } n   cout << out; n } n


#include <bits/stdc++.h> n using namespace std; n struct node { n   int a; n   int b; n   int index; n }; n bool compare(node &n1, node &n2) { n   if (n1.a < n2.a) n     return true; n   else n     return false; n } n bool compare1(node &n1, node &n2) { n   if (n1.b < n2.b) n     return true; n   else n     return false; n } n int main() { n   int n; n   cin >> n; n   vector<node> pq(n); n   for (int i = 0; i < n; i++) { n     cin >> pq[i].a >> pq[i].b; n     pq[i].index = i; n   } n   int num = 0; n   for (int i = 0; i < n; i++) { n     if (pq[i].a < pq[i].b) num++; n   } n   vector<node> track; n   if (num >= n / 2) { n     for (int i = 0; i < n; i++) { n       if (pq[i].a < pq[i].b) track.push_back(pq[i]); n     } n     sort(track.begin(), track.end(), compare); n     cout << track.size() << endl; n     for (int i = track.size() - 1; i >= 0; i--) { n       cout << track[i].index + 1 <<    ; n     } n   } else { n     for (int i = 0; i < n; i++) { n       if (pq[i].a > pq[i].b) track.push_back(pq[i]); n     } n     sort(track.begin(), track.end(), compare1); n     cout << track.size() << endl; n     for (int i = 0; i < track.size(); i++) { n       cout << track[i].index + 1 <<    ; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int hy, ay, dy; n   int hm, am, dm; n   int h, a, d; n   cin >> hy >> ay >> dy; n   cin >> hm >> am >> dm; n   cin >> h >> a >> d; n   int res = 1e9; n   for (int atk = dm + 1; atk <= 200; ++atk) { n     for (int def = dy; def <= 100; ++def) { n       int rnd = (hm + atk - dm - 1) / (atk - dm); n       int h_add = max(0, rnd * max(0, am - def) - hy + 1); n       int a_add = max(0, atk - ay); n       int d_add = max(0, def - dy); n       res = min(res, h_add * h + a_add * a + d_add * d); n     } n   } n   cout << res << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int N, br[20][200200], d[20][200200], lev[200200]; n vector<int> in, adj[200200], myc[200200], ans; n bool dead[200200]; n vector<multiset<int>> m[200200]; n multiset<int> sub, best[200200], outofall; n int go(int l, int i, int p = -1) { n   in.push_back(i); n   for (int j : adj[i]) n     if (j != p && !dead[j]) { n       d[l][i] += go(l, j, i); n     } n   return ++d[l][i]; n } n void dfs(int l, int i, int p) { n   for (int j : adj[i]) n     if (j != p && !dead[j]) { n       br[l][j] = br[l][i]; n       d[l][j] = d[l][i] + 1; n       dfs(l, j, i); n     } n } n void rec(int l, int i) { n   in.clear(), go(l, i); n   int C = -1; n   for (int k : in) { n     int mx = d[l][i] - d[l][k]; n     for (int j : adj[k]) n       if (d[l][j] < d[l][k]) { n         mx = max(mx, d[l][j]); n       } n     if (mx <= d[l][i] / 2) { n       C = k; n       break; n     } n   } n   lev[C] = l; n   for (int k : in) { n     myc[k].push_back(C); n   } n   for (int k : in) { n     d[l][k] = 0; n   } n   for (int j : adj[C]) n     if (!dead[j]) { n       sub.clear(), d[l][j] = 1; n       br[l][j] = (int)m[C].size(); n       dfs(l, j, C), m[C].push_back(sub); n     } n   dead[C] = 1; n   for (int j : adj[C]) n     if (!dead[j]) { n       rec(l + 1, j); n     } n } n int main() { n   scanf( %d , &N); n   for (int i = (2); i < (N + 1); i++) { n     int p; n     scanf( %d , &p); n     adj[i].push_back(p), adj[p].push_back(i); n   } n   rec(0, 1); n   for (int i = (1); i < (N + 1); i++) { n     for (int c : myc[i]) { n       if ((int)best[c].size() >= 2) { n         auto it = --best[c].end(); n         int t = *it + *prev(it); n         if (outofall.find(t) != outofall.end()) { n           outofall.erase(outofall.find(t)); n         } n       } n       if (c == i) { n         best[c].insert(0); n       } else { n         int myb = br[lev[c]][i], myd = d[lev[c]][i]; n         if (m[c][myb].size() && n             best[c].find(*m[c][myb].rbegin()) != best[c].end()) { n           best[c].erase(best[c].find(*m[c][myb].rbegin())); n         } n         m[c][myb].insert(myd); n         while ((int)m[c][myb].size() > 2) m[c][myb].erase(m[c][myb].begin()); n         best[c].insert(*m[c][myb].rbegin()); n         while ((int)best[c].size() > 2) best[c].erase(best[c].begin()); n       } n       if ((int)best[c].size() >= 2) { n         auto it = --best[c].end(); n         outofall.insert(*it + *prev(it)); n         while ((int)outofall.size() > 2) outofall.erase(outofall.begin()); n       } n     } n     if (i >= 2) printf( %d  , *outofall.rbegin()); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n map<pair<long long, long long>, long long> m; n int main() { n   long long t, ai, bi, dr = 1; n   cin >> t; n   vector<pair<long long, long long>> v(t); n   for (int i = 0; i < t; i++) { n     cin >> ai >> bi; n     v[i] = {ai, bi}; n   } n   m[{0, 0}] = 1; n   dr += min(v[0].first, v[0].second); n   m[{v[0].first, v[0].second}] = 1; n   for (int i = 1; i < t; i++) { n     if (v[i].first > v[i - 1].first && v[i].second > v[i - 1].second && n         m[{v[i].first, v[i].second}] == 0 && n         min(v[i].first, v[i].second) >= max(v[i - 1].second, v[i - 1].first)) { n       if ((v[i - 1].first != 0 || v[i - 1].second != 0) && n           v[i - 1].first != v[i - 1].second) { n         dr += min(v[i].first, v[i].second) - n               max(v[i - 1].first, v[i - 1].second) + 1; n       } else { n         dr += n             min(v[i].first, v[i].second) - max(v[i - 1].first, v[i - 1].second); n       } n       m[{v[i].first, v[i].second}] = 1; n     } else if ((v[i].first > v[i].second && v[i].second == v[i - 1].second && n                 v[i - 1].first < v[i - 1].second) || n                (v[i].second > v[i].first && v[i].first == v[i - 1].first && n                 v[i - 1].second < v[i - 1].first) && n                    m[{v[i].first, v[i].second}] == 0) { n       dr++; n       m[{v[i].first, v[i].second}] = 1; n     } else if (v[i].first == v[i].second && m[{v[i].first, v[i].second}] == 0) { n       dr++; n       m[{v[i].first, v[i].second}] = 1; n     } n   } n   cout << dr; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T> n inline T qmin(const T a, const T b) { n   return a < b ? a : b; n } n template <typename T> n inline T qmax(const T a, const T b) { n   return a > b ? a : b; n } n template <typename T> n inline void getmin(T &a, const T b) { n   if (a > b) a = b; n } n template <typename T> n inline void getmax(T &a, const T b) { n   if (a < b) a = b; n } n inline void fileio(string s) { n   freopen((s +  .in ).c_str(),  r , stdin); n   freopen((s +  .out ).c_str(),  w , stdout); n } n const int inf = 1e9 + 7; n const long long linf = 1e17 + 7; n const int N = 2e5 + 7; n int q[N], st, ed; n int n, deg[N], s[N]; n int tot, ans[N][2]; n void dfs(int u) { n   int v = s[u]; n   ++tot, ans[tot][0] = u, ans[tot][1] = v; n   --deg[v], s[v] ^= u; n   --deg[u], s[u] = 0; n   if (deg[v] == 1) dfs(v); n } n int main() { n   scanf( %d , &n); n   for (int i = 0; i < n; ++i) { n     scanf( %d%d , deg + i, s + i); n   } n   for (int i = 0; i < n; ++i) { n     if (deg[i] == 1) dfs(i); n   } n   printf( %d n , tot); n   for (int i = 1; i <= tot; ++i) printf( %d %d n , ans[i][0], ans[i][1]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long mod = 1000000007; n const int MAXN = 1500907; n vector<vector<long long>> adj; n bool visited[MAXN]; n long long n_nodes; n long long n_edges; n void dfs(int node) { n   visited[node] = 1; n   n_nodes++; n   for (auto i : adj[node]) { n     n_edges++; n     if (!visited[i]) { n       dfs(i); n     } n   } n } n int main() { n   ios::sync_with_stdio(false); n   cin.tie(NULL); n   long long n, m, a, b; n   cin >> n >> m; n   adj.resize(n + 5); n   while (m--) { n     cin >> a >> b; n     a--; n     b--; n     adj[a].push_back(b); n     adj[b].push_back(a); n   } n   for (long long i = 1; i <= n; i++) { n     if (!visited[i]) { n       n_nodes = 0; n       n_edges = 0; n       dfs(i); n       if (n_edges != n_nodes * (n_nodes - 1)) { n         cout <<  NO  << endl; n         return 0; n       } n     } n   } n   cout <<  YES  << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n set<long long> num; n void f(int x, int a, int b, long long now) { n   if (x == 0) { n     num.insert(now); n     return; n   } n   if (a) f(x - 1, a - 1, b, now * 10 + 4); n   if (b) f(x - 1, a, b - 1, now * 10 + 7); n } n void init() { n   int i; n   for (i = 1; i <= 5; i++) f(i * 2, i, i, 0LL); n } n int main() { n   long long n; n   init(); n   scanf( %I64d , &n); n   printf( %I64d , *num.upper_bound(n - 1)); n } n


#include <bits/stdc++.h> n using namespace std; n int main(void) { n   int n; n   scanf( %d , &n); n   long a[n]; n   long long s = 0, ss = 0; n   for (int i = 0; i < n; i++) { n     scanf( %ld , &a[i]); n     s += a[i]; n   } n   if (s % 3 != 0) { n     printf( 0 n ); n     return 0; n   } n   s = s / 3; n   vector<int> sum1; n   vector<int> sum2; n   for (int i = 0; i < n - 1; i++) { n     ss += a[i]; n     if (ss == s) sum1.push_back(i); n     if (ss == 2 * s) sum2.push_back(i); n   } n   long long ans = 0; n   for (int i = 0; i < sum1.size(); i++) { n     ans += sum2.end() - lower_bound(sum2.begin(), sum2.end(), sum1[i] + 1); n   } n   printf( %I64d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, m, i, j, a[110], b[110], a1, b1; n   cin >> n >> m; n   for (i = 0; i <= n; i++) cin >> a[i]; n   for (i = 0; i <= m; i++) cin >> b[i]; n   a1 = a[0], b1 = b[0]; n   if (n > m) { n     if (a1 * b1 > 0) n       cout <<  Infinity  << endl; n     else n       cout <<  -Infinity  << endl; n   } else if (n < m) { n     printf( 0/1 n ); n   } else if (n == m) { n     for (i = 1; i <= min(abs(a1), abs(b1)); i++) { n       if (a1 % i == 0 && b1 % i == 0) j = i; n     } n     a1 /= j, b1 /= j; n     if (a1 * b1 > 0) n       printf( %d/%d n , abs(a1), abs(b1)); n     else n       printf( -%d/%d n , abs(a1), abs(b1)); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long int MOD = 1e9 + 7; n const long long int INF = 1e18; n long long int sub(long long int a, long long int b, long long int p = MOD) { n   return ((a % p) - (b % p) + p) % p; n } n long long int mult(long long int a, long long int b, long long int p = MOD) { n   return ((a % p) * (b % p)) % p; n } n long long int add(long long int a, long long int b, long long int p = MOD) { n   return (a % p + b % p) % p; n } n long long int fpow(long long int n, long long int k, long long int p = MOD) { n   long long int r = 1; n   while (k > 0) { n     if (k & 1) r = r * n % p; n     n = n * n % p; n     k = k >> 1; n   } n   return r; n } n long long int inv(long long int a, long long int p = MOD) { n   return fpow(a, p - 2, p); n } n long long int fdiv(long long int a, long long int b, long long int p = MOD) { n   long long int yinv = inv(b); n   long long int ans = (a * yinv) % p; n   return ans; n } n unsigned long long int I1018 = 1000000000000000000; n const unsigned long long int I10e5 = 100000; n const long long int I10e6 = 1e6; n long long int ZERO = 0; n unsigned long long int I10e9 = 1000000000; n long long int ONE = 1; n void printVecpll(std::vector<pair<long long int, long long int> > v) { n   for (auto p : v) { n     cout << (char)p.first <<     << p.second << endl; n   } n } n void printVec(std::vector<long long int> v) { n   for (long long int i = 0; i < v.size(); i++) { n     cout << v[i] <<    ; n   } n   cout << endl; n } n std::vector<long long int> read_vec(long long int n) { n   long long int i; n   std::vector<long long int> v(n); n   for (i = 0; i < n; i++) { n     cin >> v[i]; n   } n   return v; n } n void print_um(unordered_map<long long int, long long int> m) { n   for (pair<long long int, long long int> p : m) { n     cout << p.first <<     << p.second << endl; n   } n } n std::vector<bool> is_prime; n std::vector<long long int> primes; n void sieve(long long int n) { n   is_prime.resize(n + 2, true); n   primes.clear(); n   long long int p; n   for (p = 2; p * p <= n; p++) { n     if (is_prime[p]) { n       long long int i; n       for (i = p * p; i <= n; i += p) { n         is_prime[i] = false; n       } n     } n   } n   is_prime[0] = is_prime[1] = false; n   long long int i; n   for (i = 2; i <= n; i++) { n     if (is_prime[i]) { n       primes.emplace_back(i); n     } n   } n } n class Sol { n  public: n   long long int n; n   vector<std::vector<long long int> > graph; n   map<long long int, long long int> lc; n   std::vector<bool> visited; n   Sol() { n     cin >> n; n     graph.resize(n); n     visited.resize(n, false); n     long long int i; n     for (i = 1; i < n; i++) { n       long long int v; n       cin >> v; n       v--; n       graph[i].emplace_back(v); n       graph[v].emplace_back(i); n     } n     dfs(0, -1); n     std::vector<long long int> ans; n     long long int temp = lc[0]; n     for (i = 0; i < temp; i++) { n       ans.emplace_back(1); n     } n     for (auto p : lc) { n       if (p.first == 0) continue; n       for (i = 0; i < p.second; i++) { n         ans.emplace_back(p.first); n       } n     } n     printVec(ans); n   } n   long long int dfs(long long int src, long long int par) { n     long long int lt = 0; n     if (graph[src].size() == 1 && src != 0) { n       lc[0]++; n       return 1; n     } n     for (auto p : graph[src]) { n       if (p != par) { n         lt += dfs(p, src); n       } n     } n     lc[lt]++; n     return lt; n   } n }; n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   Sol sol; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <int V, class T = long long> n class max_flow { n   static const T INF = numeric_limits<T>::max(); n   struct edge { n     int t, rev; n     T cap, f; n   }; n   vector<edge> adj[V]; n   int dist[V]; n   int ptr[V]; n   bool bfs(int s, int t) { n     memset(dist, -1, sizeof dist); n     dist[s] = 0; n     queue<int> q({s}); n     while (!q.empty() && dist[t] == -1) { n       int n = q.front(); n       q.pop(); n       for (auto& e : adj[n]) { n         if (dist[e.t] == -1 && e.cap != e.f) { n           dist[e.t] = dist[n] + 1; n           q.push(e.t); n         } n       } n     } n     return dist[t] != -1; n   } n   T augment(int n, T amt, int t) { n     if (n == t) return amt; n     for (; ptr[n] < adj[n].size(); ptr[n]++) { n       edge& e = adj[n][ptr[n]]; n       if (dist[e.t] == dist[n] + 1 && e.cap != e.f) { n         T flow = augment(e.t, min(amt, e.cap - e.f), t); n         if (flow != 0) { n           e.f += flow; n           adj[e.t][e.rev].f -= flow; n           return flow; n         } n       } n     } n     return 0; n   } n  n  public: n   void add(int u, int v, T cap = 1, T rcap = 0) { n     adj[u].push_back({v, (int)adj[v].size(), cap, 0}); n     adj[v].push_back({u, (int)adj[u].size() - 1, rcap, 0}); n   } n   T calc(int s, int t) { n     T flow = 0; n     while (bfs(s, t)) { n       memset(ptr, 0, sizeof(ptr)); n       while (T df = augment(s, INF, t)) flow += df; n     } n     return flow; n   } n   void clear() { n     for (int n = 0; n < V; n++) adj[n].clear(); n   } n }; n set<int> xsw; n set<int> ysw; n bool adj[105][105]; n int x1arr[50]; n int x2arr[50]; n int y1arr[50]; n int y2arr[50]; n int xswa[105]; n int yswa[105]; n int main() { n   int n, m; n   cin >> n >> m; n   xsw.insert(0); n   xsw.insert(n); n   ysw.insert(0); n   ysw.insert(n); n   for (int i = 0; i < m; i++) { n     cin >> x1arr[i] >> y1arr[i] >> x2arr[i] >> y2arr[i]; n     xsw.insert(x1arr[i] - 1); n     xsw.insert(x2arr[i]); n     ysw.insert(y1arr[i] - 1); n     ysw.insert(y2arr[i]); n   } n   int idx = 0; n   for (auto x : xsw) { n     xswa[idx++] = x; n   } n   idx = 0; n   for (auto y : ysw) { n     yswa[idx++] = y; n   } n   for (int i = 0; i < m; i++) { n     int xmin = 0; n     for (int j = 0; j < xsw.size(); j++) { n       if (xswa[j] == x1arr[i] - 1) { n         xmin = j; n         break; n       } n     } n     int xmax = 0; n     for (int j = 0; j < xsw.size(); j++) { n       if (xswa[j] == x2arr[i]) { n         xmax = j; n         break; n       } n     } n     int ymin = 0; n     for (int j = 0; j < ysw.size(); j++) { n       if (yswa[j] == y1arr[i] - 1) { n         ymin = j; n         break; n       } n     } n     int ymax = 0; n     for (int j = 0; j < ysw.size(); j++) { n       if (yswa[j] == y2arr[i]) { n         ymax = j; n         break; n       } n     } n     for (int j = xmin; j < xmax; j++) { n       for (int k = ymin; k < ymax; k++) { n         adj[j][k] = true; n       } n     } n   } n   max_flow<500> network; n   for (int i = 0; i < xsw.size() - 1; i++) { n     network.add(0, i + 1, xswa[i + 1] - xswa[i]); n   } n   for (int i = 0; i < xsw.size() - 1; i++) { n     for (int j = 0; j < ysw.size() - 1; j++) { n       if (adj[i][j]) { n         network.add(i + 1, xsw.size() + j, n); n       } n     } n   } n   for (int j = 0; j < ysw.size() - 1; j++) { n     network.add(xsw.size() + j, xsw.size() + ysw.size(), yswa[j + 1] - yswa[j]); n   } n   cout << network.calc(0, xsw.size() + ysw.size()) << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long int n; n   scanf( %lld , &n); n   long long int i; n   for (i = 2; i * i <= n; i++) { n     while (n % (i * i) == 0) n /= i; n   } n   printf( %lld n , n); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m, s, i, j, k; n bool A[301][301]; n char str[301]; n int main() { n   scanf( %d%d%d , &n, &m, &s); n   for (i = 0; i < n; i++) { n     scanf( %s , str); n     for (j = 0; j < m; j++) A[i][j] = str[j] ==  * ; n   } n   for (i = 1; 2 * i + 1 <= min(n, m); i++) { n     for (j = i; j + i < n; j++) { n       for (k = i; k + i < m; k++) n         if (A[j][k] && A[j][k + i] && A[j][k - i] && A[j + i][k] && n             A[j - i][k]) { n           s--; n           if (!s) break; n         } n       if (!s) break; n     } n     if (!s) break; n   } n   if (s) n     printf( -1 n ); n   else { n     j++; n     k++; n     printf( %d %d n%d %d n%d %d n%d %d n%d %d n , j, k, j - i, k, j + i, k, j, n            k - i, j, k + i); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int nm = 100002; n int n, d; n long long x; n int a[nm], b[nm], vt[nm]; n int c[nm]; n int nc; n int getNextX() { n   x = (x * 37 + 10007) % 1000000007; n   return x; n } n void init() { n   int i; n   for (i = 0; i < n; i = i + 1) { n     a[i] = i + 1; n   } n   for (i = 0; i < n; i = i + 1) { n     swap(a[i], a[getNextX() % (i + 1)]); n   } n   for (i = 0; i < n; i = i + 1) { n     if (i < d) n       b[i] = 1; n     else n       b[i] = 0; n   } n   for (i = 0; i < n; i = i + 1) { n     swap(b[i], b[getNextX() % (i + 1)]); n   } n } n int main() { n   scanf( %d%d%I64d , &n, &d, &x); n   init(); n   int i, j, k; n   for (i = 0; i < n; ++i) { n     vt[a[i]] = i; n     if (b[i]) c[++nc] = i; n   } n   for (i = 0; i < n; ++i) { n     for (j = n; j > n - 100 && j > 0; --j) n       if (vt[j] <= i && b[i - vt[j]]) break; n     if (j > n - 100 && j > 0) { n       printf( %d n , j); n       continue; n     } n     k = 0; n     for (j = 1; j <= nc && c[j] <= i; ++j) k = max(k, a[i - c[j]]); n     printf( %d n , k); n   } n } n