#include <bits/stdc++.h> n using namespace std; n const double EPS = 1e-8; n const int mod = 1e9 + 7; n const int N = 1e6 + 10; n const long long INF = 1e18; n long long power(long long x, long long y) { n   long long t = 1; n   while (y > 0) { n     if (y % 2) n       y -= 1, t = t * x % mod; n     else n       y /= 2, x = x * x % mod; n   } n   return t; n } n long long val[N]; n vector<pair<long long, long long> > G[N]; n int vk[N]; n set<pair<long long, long long> > S[N]; n int ans[N]; n int tl = 0; n void merge(int &p, int nw, long long cs) { n   if (S[nw].size() == 0) return; n   set<pair<long long, long long> >::iterator it = S[nw].end(); n   vector<pair<long long, long long> > z; n   while (1) { n     it--; n     if ((*it).first > cs) { n       z.push_back(*it); n     } else n       break; n     if (it == S[nw].begin()) break; n   } n   for (int i = 0; i < z.size(); ++i) S[nw].erase(z[i]); n   if (p == -1) { n     p = nw; n     return; n   } n   if (S[p].size() < S[nw].size()) swap(p, nw); n   it = S[nw].begin(); n   while (it != S[nw].end()) { n     S[p].insert(*it); n     it++; n   } n } n int dfs1(int v, long long c) { n   int my = -1; n   for (int i = 0; i < G[v].size(); ++i) { n     int x = dfs1(G[v][i].first, G[v][i].second + c); n     merge(my, x, c); n   } n   if (my == -1) my = tl++; n   ans[v] = S[my].size(); n   S[my].insert(make_pair(c - val[v], v)); n   return my; n } n int main() { n   int n; n   scanf( %d , &n); n   for (int i = 0; i < n; ++i) scanf( %lld , &val[i]); n   for (int i = 0; i < n - 1; ++i) { n     long long x, y; n     scanf( %lld%lld , &x, &y); n     x--; n     G[x].push_back(make_pair(i + 1, y)); n   } n   dfs1(0, 0); n   for (int i = 0; i < n; ++i) printf( %d  , ans[i]); n   printf(  n ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int INF = 1e9; n int n, m; n int k[200010]; n int rem[200010]; n int idx[200010]; n vector<int> special[200010]; n vector<int> days[200010]; n bool pos(const long long M) { n   long long coins = 0; n   memset(idx, 0, sizeof(idx)); n   for (int i = 0; i < n; i++) rem[i] = k[i]; n   for (int i = 1; i <= min((long long)200000, M); i++) { n     coins++; n     for (int j = 0; j < int(special[i].size()) && coins; j++) { n       int item = special[i][j]; n       auto it = upper_bound(days[item].begin(), days[item].end(), i); n       if (it == days[item].end() || *it > M) { n         long long temp = rem[item]; n         rem[item] -= min((long long)rem[item], coins); n         coins -= min(temp, coins); n       } n     } n   } n   if (M > 200000) coins += M - 200000; n   for (int i = 0; i < n && coins >= 0; i++) { n     if (rem[i]) coins -= 2 * rem[i]; n   } n   return coins >= 0; n } n int main() { n   scanf( %d %d , &n, &m); n   long long sum = 0; n   for (int i = 0; i < n; i++) scanf( %d , &k[i]), sum += k[i]; n   long long L = sum, M, R = 2 * sum, res; n   int d, t; n   while (m--) { n     scanf( %d %d , &d, &t); n     special[d].push_back(t - 1), days[t - 1].push_back(d); n   } n   for (int i = 0; i < n; i++) sort(days[i].begin(), days[i].end()); n   while (L <= R) { n     M = (L + R) / 2; n     if (pos(M)) n       res = M, R = M - 1; n     else n       L = M + 1; n   } n   printf( %I64d n , res); n   return 0; n } n


#include <bits/stdc++.h> n #pragma comment(linker,  /STACK:200000000 ) n using namespace std; n const int INF = (int)1E8; n const long long INF64 = (long long)1E18; n const double EPS = 1E-9; n const double PI = acos((double)0) * 2; n const int MAXN = 160; n int n, cost[MAXN]; n string s; n bool read() { n   if (!(cin >> n)) return false; n   for (int i = 0; i < int(n); ++i) { n     cin >> cost[i + 1]; n     if (cost[i + 1] == -1) cost[i + 1] = -INF; n   } n   cin >> s; n   return true; n } n int d[MAXN][MAXN][MAXN]; n bool u[MAXN][MAXN][MAXN]; n int get_d(int l, int r, int len) { n   if (l > r) return cost[len]; n   if (l == r) return max(cost[len] + cost[1], cost[len + 1]); n   int& my = d[l][r][len]; n   bool& myu = u[l][r][len]; n   if (myu) return my; n   myu = true; n   my = -INF; n   if (len != 0) my = max(my, cost[len] + get_d(l, r, 0)); n   for (int i = int(l); i < int(r); ++i) n     my = max(my, get_d(l, i, 0) + get_d(i + 1, r, len)); n   for (int i = int(l + 1); i < int(r + 1); ++i) n     my = max(my, get_d(i, r, 0) + get_d(l, i - 1, len)); n   for (int i = int(l); i < int(r + 1); ++i) n     my = max(my, get_d(l, i - 1, 0) + cost[len + 1] + get_d(i + 1, r, 0)); n   if (s[l] == s[r]) my = max(my, get_d(l + 1, r - 1, len + 2)); n   return my; n } n int d2[MAXN]; n void solve() { n   memset(u, 0, sizeof u); n   int x = get_d(0, n - 1, 0); n   for (int i = 0; i < int(n); ++i) { n     d2[i] = i ? d2[i - 1] : 0; n     for (int j = 0; j < int(i + 1); ++j) n       d2[i] = max(d2[i], get_d(j, i, 0) + (j ? d2[j - 1] : 0)); n   } n   cout << d2[n - 1] << endl; n } n int main() { n   if (!read()) throw; n   solve(); n } n


#include <bits/stdc++.h> n using namespace std; n int gcd(int a, int b) { n   while (b) { n     int tmp = a % b; n     a = b; n     b = tmp; n   } n   return a; n } n int main() { n   int a, b; n   cin >> a >> b; n   int k = gcd(a, b); n   if (abs(a / k - b / k) <= 1) n     cout <<  Equal n ; n   else if (a < b) n     cout <<  Dasha n ; n   else n     cout <<  Masha n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long const M = 1000000007; n double const pi = acos(-1); n long long const inf = 2e18; n long long const N = 200001; n long long comb(long long n) { n   if (n <= 0) return 0; n   return (n * (n - 1)) / 2; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   long long i, j, t, n, d, ans = 0, curr, prev = 0; n   cin >> n >> d; n   vector<long long> a(n); n   for (i = 0; i < n; i++) cin >> a[i]; n   for (curr = 2; curr < n;) { n     while (curr < n && a[curr] - a[prev] <= d) { n       ans += comb(curr - prev); n       curr++; n     } n     if (a[curr] - a[prev] > d) prev++; n   } n   cout << ans <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m; n long long int a[1004][1004]; n long long int b[1005]; n long long int c[1005]; n long long int ci, cj; n int main() { n   long long int i, j; n   scanf( %d %d , &n, &m); n   for (i = 0; i < n; i++) n     for (j = 0; j < m; j++) scanf( %I64d , &a[i][j]); n   for (i = 0; i < n; i++) n     for (j = 0; j < m; j++) b[i] += a[i][j]; n   for (i = 0; i < n; i++) n     for (j = 0; j < m; j++) c[j] += a[i][j]; n   long long int mm = -1, tt; n   long long int res = 0; n   long long int mi; n   for (i = 0; i <= n; i++) { n     tt = 0; n     for (j = 0; j < n; j++) { n       tt += b[j] * (4 * i - 4 * j - 2) * (4 * i - 4 * j - 2); n     } n     if (mm < 0 || mm > tt) { n       mm = tt; n       mi = i; n     } n   } n   ci = mi; n   res += mm; n   mm = -1; n   for (i = 0; i <= m; i++) { n     tt = 0; n     for (j = 0; j < m; j++) { n       tt += c[j] * (4 * i - 4 * j - 2) * (4 * i - 4 * j - 2); n     } n     if (mm < 0 || mm > tt) { n       mm = tt; n       mi = i; n     } n   } n   cj = mi; n   res += mm; n   printf( %I64d n , res); n   printf( %I64d %I64d n , ci, cj); n } n


#include <bits/stdc++.h> n using namespace std; n const int INF = 1e9 + 7; n const int mod = 1e9 + 7; n long long int powmod(long long int a, long long int b) { n   long long int res = 1; n   a %= mod; n   assert(b >= 0); n   for (; b; b >>= 1) { n     if (b & 1) res = res * a % mod; n     a = a * a % mod; n   } n   return res; n } n int nxt() { n   int x; n   scanf( %d , &x); n   return x; n } n vector<string> type; n vector<vector<int> > edge; n vector<bool> ans; n void dfs(int u) { n   if (type[u] ==  IN ) { n     ans[u] = edge[u][0]; n     return; n   } n   if (type[u] ==  NOT ) { n     dfs(edge[u][0]); n     ans[u] = !ans[edge[u][0]]; n     return; n   } n   dfs(edge[u][0]); n   dfs(edge[u][1]); n   if (type[u] ==  AND ) { n     ans[u] = ans[edge[u][0]] & ans[edge[u][1]]; n   } n   if (type[u] ==  OR ) { n     ans[u] = ans[edge[u][0]] | ans[edge[u][1]]; n   } n   if (type[u] ==  XOR ) { n     ans[u] = ans[edge[u][0]] ^ ans[edge[u][1]]; n   } n } n void dfs2(int u, vector<int>& a) { n   if (type[u] ==  IN ) { n     a.push_back(u); n     return; n   } n   if (type[u] ==  NOT ) { n     dfs2(edge[u][0], a); n     return; n   } n   bool l = ans[edge[u][0]]; n   bool r = ans[edge[u][1]]; n   int x = edge[u][0]; n   int y = edge[u][1]; n   if (type[u] ==  AND ) { n     if (!l && r) { n       dfs2(x, a); n     } n     if (l && !r) { n       dfs2(y, a); n     } n     if (l && r) { n       dfs2(x, a); n       dfs2(y, a); n     } n   } n   if (type[u] ==  OR ) { n     if (!r && !l) { n       dfs2(x, a); n       dfs2(y, a); n     } n     if (!l && r) { n       dfs2(y, a); n     } n     if (l && !r) { n       dfs2(x, a); n     } n   } n   if (type[u] ==  XOR ) { n     dfs2(x, a); n     dfs2(y, a); n   } n } n int main() { n   int n = nxt(); n   type.resize(n); n   edge.resize(n); n   ans.resize(n); n   vector<int> q; n   for (int i = 0; i < n; i++) { n     cin >> type[i]; n     if (type[i] ==  IN ) { n       edge[i].push_back(nxt()); n       q.push_back(i); n     } else if (type[i] ==  NOT ) { n       edge[i].push_back(nxt() - 1); n     } else { n       edge[i].push_back(nxt() - 1); n       edge[i].push_back(nxt() - 1); n     } n   } n   dfs(0); n   vector<int> req; n   dfs2(0, req); n   set<int> s; n   for (int x : req) s.insert(x); n   for (int x : q) { n     if (s.count(x)) n       printf( %d , ans[0] ^ 1 ? 1 : 0); n     else n       printf( %d , ans[0] ? 1 : 0); n   } 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 tpl = tuple<int, int, int>; n using pcc = pair<char, char>; n const ll inf = 2e18; n const ll mod = 1e9 + 7; n int dr[] = {-1, 0, 1, 0}; n int dc[] = {0, 1, 0, -1}; n int kr[] = {-1, 1, -2, -2, -1, 1, 2, 2}; n int kc[] = {-2, -2, -1, 1, 2, 2, -1, 1}; n int dgr[] = {-1, -1, 1, 1}; n int dgc[] = {1, -1, -1, 1}; n const int lim = 10000005; n bool isPrime[lim]; n ll prime[lim]; n ll pal[lim]; n bool ok(string str) { n   for (int i = str.size() - 1, j = 0; i > j; i--, j++) n     if (str[i] != str[j]) return false; n   return true; n } n void sieve() { n   memset(isPrime, true, sizeof isPrime); n   for (int i = 4; i < lim; i += 2) isPrime[i] = false; n   for (ll i = 3; i <= lim; i += 2) { n     if (isPrime[i]) { n       for (ll j = i * i; j < lim; j += i) { n         isPrime[j] = false; n       } n     } n   } n   pal[1] = 1; n   pal[2] = 2; n   prime[2] = 1; n   ll cnt = 1, cnt2 = 2; n   string str, rev; n   for (ll i = 3; i < lim; i++) { n     if (isPrime[i]) cnt++; n     prime[i] = cnt; n     str = to_string(i); n     if (ok(str)) cnt2++; n     pal[i] = cnt2; n   } n } n int solve(int p, int q) { n   int ans = 0; n   for (int i = 1; i <= 10000000; i++) { n     if ((q * prime[i]) <= (p * pal[i])) ans = i; n   } n   return ans; n } n int main() { n   int ans = 0, p, q; n   sieve(); n   cin >> p >> q; n   cout << solve(p, q) << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int w, h; n   cin >> w >> h; n   int u1, d1, u2, d2; n   cin >> u1 >> d1 >> u2 >> d2; n   for (int i = h; i >= 0; i--) { n     w = w + i; n     if (i == d1 || i == d2) { n       if (i == d1) { n         w = w - u1; n         if (w < 0) { n           w = 0; n         } n       } else { n         w = w - u2; n         if (w < 0) { n           w = 0; n         } n       } n     } n   } n   cout << w << endl; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 2e5 + 5; n const int N = 2 * maxn; n int n, m, a[maxn]; n int v[N]; n void jl(int x) { n   for (int i = x; i < N; i += i & -i) { n     v[i]++; n   } n } n long long s(int x) { n   long long sum = 0; n   for (int i = x; i > 0; i -= i & -i) { n     sum += v[i]; n   } n   return sum; n } n long long cal(int k) { n   memset(v, 0, sizeof(v)); n   jl(n + 1); n   int dp = 0; n   long long sum = 0; n   for (int i = 0; i < n; i++) { n     if (a[i] <= k) n       dp -= 1; n     else n       dp += 1; n     sum += s(dp + n); n     jl(dp + n + 1); n   } n   return sum; n } n int main() { n   scanf( %d%d , &n, &m); n   for (int i = 0; i < n; i++) scanf( %d , &a[i]); n   cout << cal(m - 1) - cal(m) << endl; n   return 0; n } n


#include <bits/stdc++.h> n #pragma GCC optimize( O3 ) n #pragma GCC target( sse4 ) n using namespace std; n const int maxn = 1001000; n const int mod = 1000000007; n int a[maxn], f[maxn], r[maxn]; n int main() { n   int n; n   scanf( %d , &n); n   for (int e = 1; e <= n; e++) scanf( %d , a + e); n   auto mul = [](int x, int y) { return 1ll * x * y % mod; }; n   f[0] = 1; n   for (int e = 1; e <= n; e++) f[e] = mul(e, f[e - 1]); n   r[n + 1] = 1; n   for (int e = n; e >= 1; e--) r[e] = mul(e, r[e + 1]); n   sort(a + 1, a + 1 + n); n   int small = 0; n   int i = 1, j = 1; n   int ans = 0; n   while (i <= n) { n     while (j <= n && a[i] == a[j]) j++; n     if (j == n + 1) break; n     ans += mul(j - i, mul(a[i], mul(f[n - small - 1], r[n - small + 1]))); n     small += j - i; n     if (ans >= mod) ans -= mod; n     i = j; n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long double pi = 3.1415926535897932384626433832795; n long long int inp() { n   long long int n = 0, s = 1; n   char c; n   for (c = getchar(); c < 48 || c > 58; c = getchar()) n     if (c ==  - ) s = -1; n   for (; c > 47 && c < 59; c = getchar()) n = n * 10 + c - 48; n   return n * s; n } n long long int gcd(long long int a, long long int b) { n   if (b == 0) n     return a; n   else n     gcd(b, a % b); n } n long long int square(long long int n) { return n * n; } n long long int bigmod(long long int b, long long int p, long long int m) { n   if (p == 0) n     return 1; n   else if (p % 2 == 0) n     return square(bigmod(b, p / 2, m)) % m; n   else n     return ((b % m) * bigmod(b, p - 1, m)) % m; n } n long long mulmod(long long a, long long b, long long c) { n   long long x = 0, y = a % c; n   while (b > 0) { n     if (b % 2 == 1) { n       x = (x + y) % c; n     } n     y = (y * 2) % c; n     b /= 2; n   } n   return x % c; n } n long long int vis[55][55], n, m; n char ch[55][55]; n long long int fun(long long int i, long long int j, char z, char d) { n   long long int p = 0; n   vis[i][j] = 1; n   if ((j + 1) < m && vis[i][j + 1] == 0 && ch[i][j + 1] == z && d !=  l ) { n     p = fun(i, j + 1, z,  r ); n   } else if ((j + 1) < m && vis[i][j + 1] == 1 && ch[i][j + 1] == z && n              d !=  l ) { n     return 1; n   } n   if ((i + 1) < n && vis[i + 1][j] == 0 && ch[i + 1][j] == z && d !=  u ) { n     p = fun(i + 1, j, z,  d ); n   } else if ((i + 1) < n && vis[i + 1][j] == 1 && ch[i + 1][j] == z && n              d !=  u ) { n     return 1; n   } n   if ((j - 1) >= 0 && vis[i][j - 1] == 0 && ch[i][j - 1] == z && d !=  r ) { n     p = fun(i, j - 1, z,  l ); n   } else if ((j - 1) >= 0 && vis[i][j - 1] == 1 && ch[i][j - 1] == z && n              d !=  r ) { n     return 1; n   } n   if ((i - 1) >= 0 && vis[i - 1][j] == 0 && ch[i - 1][j] == z && d !=  d ) { n     p = fun(i - 1, j, z,  u ); n   } else if ((i - 1) >= 0 && vis[i - 1][j] == 1 && ch[i - 1][j] == z && n              d !=  d ) { n     return 1; n   } n   return p; n } n int main() { n   long long int t, ans = 0, i, j, k; n   cin >> n >> m; n   for (i = 0; i < n; i++) { n     for (j = 0; j < m; j++) cin >> ch[i][j]; n   } n   long long int f = 0; n   for (k = 0; k < 25; k++) { n     f = 0; n     for (i = 0; i < n; i++) { n       f = 0; n       for (j = 0; j < m; j++) { n         if (ch[i][j] == (char)( A  + k) && vis[i][j] != 1 && n             fun(i, j, (char)( A  + k),  a )) { n           cout <<  Yes n ; n           f = 1; n           break; n         } n       } n       if (f == 1) break; n     } n     if (f == 1) { n       break; n     } n   } n   if (f == 0) cout <<  No n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1000100; n const int M = 20; n vector<int> g[N]; n vector<int> gr[N]; n vector<int> ord; n int sp[N][M]; n int tin[N]; n int tout[N]; n int timer; n void pre_dfs(int v, int p = -1) { n   sp[v][0] = (p == -1 ? v : p); n   for (int i = 1; i < M; i++) { n     sp[v][i] = sp[sp[v][i - 1]][i - 1]; n   } n   tin[v] = timer++; n   for (int to : g[v]) { n     pre_dfs(to, v); n   } n   tout[v] = timer++; n } n inline bool anc(int u, int v) { n   return (tin[u] <= tin[v] && tout[u] >= tout[v]); n } n int get_par(int v, int k) { n   for (int i = 0; i < M; i++) { n     if ((k >> i) & 1) { n       v = sp[v][i]; n     } n   } n   return v; n } n bool mark[N]; n void dfs1(int v) { n   mark[v] = 1; n   for (int to : g[v]) { n     if (!mark[to]) { n       dfs1(to); n     } n   } n   ord.push_back(v); n } n bool good[N]; n int comp[N]; n int val[N]; n int cur; n void dfs2(int v) { n   comp[v] = cur; n   val[cur] += good[v]; n   for (int to : gr[v]) { n     if (comp[to] == -1) { n       dfs2(to); n     } n   } n } n int dp[N]; n int dfs_end(int v) { n   if (dp[v] != -1) return dp[v]; n   dp[v] = val[v]; n   for (int to : g[v]) { n     dp[v] = max(dp[v], dfs_end(to) + val[v]); n   } n   return dp[v]; n } n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   int n, k; n   cin >> n >> k; n   vector<pair<int, int>> ed; n   for (int i = 1; i < n; i++) { n     int p; n     cin >> p; n     --p; n     g[p].push_back(i); n     gr[i].push_back(p); n     ed.emplace_back(p, i); n   } n   pre_dfs(0); n   for (int i = 0; i < n; i++) { n     good[i] = g[i].empty(); n     if (!good[i]) continue; n     int up = get_par(i, k); n     g[i].push_back(up); n     gr[up].push_back(i); n   } n   for (int i = 0; i < n; i++) { n     if (!mark[i]) { n       dfs1(i); n     } n   } n   memset(comp, -1, sizeof(comp)); n   for (int i = n - 1; i >= 0; i--) { n     int v = ord[i]; n     if (comp[v] == -1) { n       dfs2(v); n       ++cur; n     } n   } n   for (int i = 0; i < n; i++) { n     g[i].clear(); n     gr[i].clear(); n   } n   for (auto u : ed) { n     int x = comp[u.first]; n     int y = comp[u.second]; n     if (x != y) { n       g[x].push_back(y); n     } n   } n   memset(dp, -1, sizeof(dp)); n   cout << dfs_end(comp[0]) << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, m, s, i, l = 0, k = 0, j = 0, t = 0, p = 0, max = 0; n   cin >> n; n   int a[n]; n   for (i = 0; i < n; i++) { n     k++; n     cin >> a[i]; n     if (a[i] == 1) { n       l++; n     } n     if (a[i] == 2) { n       j++; n     } n     if (a[i] == 3) { n       t++; n     } n     if (l > j) { n       max = l; n     } n     if (j > max) { n       max = j; n     } else if (t > max) { n       max = t; n     } n   } n   cout << (n - max) << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int a, b, n; n long long mod = 1000000007; n vector<long long> factorial(1000005); n bool excellent(long long z) { n   while (z != 0) { n     if (z % 10 != a && z % 10 != b) return false; n     z /= 10; n   } n   return true; n } n long long pow(long long a, long long p) { n   if (p == 0) return 1; n   if (p % 2 == 1) return (a * pow(a, p - 1)) % mod; n   long long v = pow(a, p >> 1); n   return (v * v) % mod; n } n int calc(int n, int m) { n   return (factorial[n] * (pow(factorial[m], mod - 2) % mod) % mod * n           (pow(factorial[n - m], mod - 2) % mod) % mod); n } n int main() { n   cin >> a >> b >> n; n   factorial[1] = 1; n   for (int i = 2; i <= n; i++) factorial[i] = (factorial[i - 1] * i) % mod; n   long long count = 0; n   for (int i = 0; i <= n; i++) { n     if (excellent(a * i + b * (n - i))) { n       if (n - i != 0 && i != 0) { n         count += calc(n, i); n         count = count % mod; n       } else n         count++; n     } n   } n   cout << count << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int inf = 1 << 30; n struct line { n   int a, b; n   line() {} n   line(int a, int b) : a(a), b(b) {} n   bool operator<(const line& o) const { return a < o.a; } n   friend line operator+(const line& first, const line& second) { n     if (second < first) return second + first; n     if (first.b >= second.a && first.b < second.b) n       return line(first.a, second.b); n     return first; n   } n   void print() { printf( (%d, %d)  , a, b); } n }; n struct node { n   vector<line> box; n   node() {} n   node(line first) { box = {first}; } n   int size() { return (int)(box).size(); } n   friend node operator+(const node& X, const node& Y) { n     const vector<line>& first = X.box; n     const vector<line>& second = Y.box; n     int l = 0, r = 0; n     node T; n     vector<line>& t = T.box; n     while (l < first.size() && r < second.size()) { n       if (first[l] < second[r]) n         t.push_back(first[l++]); n       else n         t.push_back(second[r++]); n     } n     while (l < first.size()) t.push_back(first[l++]); n     while (r < second.size()) t.push_back(second[r++]); n     vector<line> Stack; n     Stack.push_back(t.back()); n     for (int i = ((int)(t).size() - 2); i >= (0); --i) { n       line& c = t[i]; n       while (!Stack.empty() && Stack.back().a <= c.b) { n         c.b = max(c.b, Stack.back().b); n         Stack.pop_back(); n       } n       Stack.push_back(c); n     } n     Stack.clear(); n     return T; n   } n   line search(int a) { n     int l = 0, r = size(); n     while (l < r) { n       int m = (l + r) >> 1; n       if (box[m].a < a) n         l = m + 1; n       else n         r = m; n     } n     if (l != size()) return box[l]; n     return line(inf, inf); n   } n   void print() { n     printf( {  ); n     for (line& e : box) { n       e.print(); n       cout <<    ; n     } n     printf( } n ); n   } n }; n const int N = 100005; n line rope[N]; n node seg[N << 2]; n void build(int l, int r, int now) { n   if (l == r) { n     seg[now] = node(rope[l]); n     return; n   } n   int m = (l + r) >> 1; n   int L = (now << 1), R = (now << 1 | 1); n   build(l, m, L); n   build(m + 1, r, R); n   seg[now] = seg[L] + seg[R]; n } n line get(int ll, int rr, int l, int r, int now) { n   if (ll <= l && r <= rr) { n     return seg[now].search(ll); n   } n   if (rr < l || r < ll) return line(inf, inf); n   int m = (l + r) >> 1; n   return get(ll, rr, l, m, now << 1) + get(ll, rr, m + 1, r, now << 1 | 1); n } n void solve() { n   int n, m; n   cin >> n >> m; n   for (int i = (1); i <= (m); ++i) { n     int a, b; n     cin >> a >> b; n     rope[b] = {a, b}; n   } n   build(1, n, 1); n   int q; n   cin >> q; n   while (q--) { n     int a, b; n     cin >> a >> b; n     line ans = get(a, b, 1, n, 1); n     if (ans.a != a) n       printf( %d n , a); n     else n       printf( %d n , ans.b); n   } n } n int main() { n   solve(); 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 const int N = 4000090, M = 1000000007; n int t[N]; n void add(int x, int l, int r, int p, int v) { n   if (p > r or p < l) return; n   if (l == r) { n     t[x] += v; n     return; n   } n   int m = (l + r) / 2; n   add(2 * x, l, m, p, v); n   add(2 * x + 1, m + 1, r, p, v); n   t[x] = t[2 * x] + t[2 * x + 1]; n   return; n } n int kth(int x, int l, int r, int k) { n   if (l == r) return l; n   int m = (l + r) / 2; n   if (t[2 * x] >= k) n     return kth(2 * x, l, m, k); n   else n     return kth(2 * x + 1, m + 1, r, k - t[2 * x]); n } n int main() { n   int n, q, x; n   scanf( %d %d , &n, &q); n   for (int i = 0; i < n; ++i) scanf( %d , &x), add(1, 1, n, x, 1); n   for (int Q = 0; Q < q; ++Q) { n     scanf( %d , &x); n     if (x > 0) n       add(1, 1, n, x, 1); n     else n       add(1, 1, n, kth(1, 1, n, -x), -1); n   } n   if (!t[1]) n     puts( 0 ); n   else n     printf( %d , kth(1, 1, n, 1)); n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   string s, t; n   cin >> s >> t; n   int n = s.size(); n   char c; n   vector<pair<char, char>> a; n   for (int i = 0; i < n; i++) n     if (s[i] != t[i]) { n       c = t[i]; n       for (int j = 0; j < n; j++) n         if (t[j] == c && s[j] == s[i] || t[j] == s[i] && s[j] == c) { n           if (t[j] == c) n             t[j] = s[i]; n           else n             t[j] = c; n         } else if (t[j] == c || s[j] == s[i] || t[j] == s[i] || s[j] == c) { n           cout << -1; n           return 0; n         } n       a.push_back(make_pair(c, s[i])); n     } n   cout << a.size() << endl; n   for (int i = 0; i < a.size(); i++) n     cout << a[i].first <<     << a[i].second << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n double ans = 1; n int p, d, h; n int main() { n   cin >> p >> d >> h; n   int top = 0; n   int kh; n   for (int i = 1; i <= d; i++) { n     int x; n     cin >> x; n     top += x; n     if (i == h) { n       kh = x; n     } n   } n   if (top < p) { n     cout << -1; n     return 0; n   } else { n     double t = 1; n     int temp = top - 1; n     for (int i = p - 1; i > 0; i--) { n       t *= ((double)temp / (double)i); n       temp--; n     } n     double ist = 1; n     int temp2 = top - kh; n     for (int i = p - 1; i > 0; i--) { n       ist *= ((double)temp2 / (double)i); n       temp2--; n     } n     double aist = t - ist; n     ans = aist / t; n     cout << ans; n   } n   return 0; n } n


#include <bits/stdc++.h> n #pragma GCC optimize( Ofast ) n using namespace std; n clock_t clk = clock(); n int i, j; n void solve(void); n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   ; n   int t = 1; n   while (t--) solve(); n   return 0; n } n int n, m, a, b; n bool cnt; n bool vis[1501][1501]; n char maze[1501][1501]; n set<pair<int, int> > s[1501][1501]; n int dfs(int x, int y, int ex, int ey, int cntx, int cnty) { n   auto it = s[x][y].find({cntx, cnty}); n   if (vis[x][y] == 1 && it != s[x][y].end()) n     return 0; n   else if (vis[x][y] == 1) n     return -1; n   vis[x][y] = 1; n   s[x][y].insert({cntx, cnty}); n   int val = 0, valx = cntx, valy = cnty; n   if (maze[x][((y - 1) % m + m) % m] !=  #  && ((y - 1) % m + m) % m != ey) { n     valx = cntx, valy = cnty; n     if (y - 1 == -1) --valy; n     val = dfs(x, ((y - 1) % m + m) % m, x, y, valx, valy); n   } n   if (val == -1) return val; n   if (maze[x][((y + 1) % m + m) % m] !=  #  && ((y + 1) % m + m) % m != ey) { n     valx = cntx, valy = cnty; n     if (y + 1 == m) ++valy; n     val = dfs(x, ((y + 1) % m + m) % m, x, y, valx, valy); n   } n   if (val == -1) return val; n   if (maze[((x - 1) % n + n) % n][y] !=  #  && ((x - 1) % n + n) % n != ex) { n     valx = cntx, valy = cnty; n     if (x - 1 == -1) --valx; n     val = dfs(((x - 1) % n + n) % n, y, x, y, valx, valy); n   } n   if (val == -1) return val; n   if (maze[((x + 1) % n + n) % n][y] !=  #  && ((x + 1) % n + n) % n != ex) { n     valx = cntx, valy = cnty; n     if (x + 1 == n) ++valx; n     val = dfs(((x + 1) % n + n) % n, y, x, y, valx, valy); n   } n   if (val == -1) return val; n   return 0; n } n void solve() { n   cin >> n >> m; n   for (i = 0; i <= n - 1; ++i) n     for (j = 0; j <= m - 1; ++j) { n       cin >> maze[i][j]; n       if (maze[i][j] ==  S ) a = i, b = j; n     } n   int val = dfs(a, b, -1, -1, 0, 0); n   if (val == -1) n     cout <<  Yes ; n   else n     cout <<  No ; n   cout <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n const double Pi = acos(-1.0); n inline int cmp(double x, double y = 0, double tol = 1e-7) { n   return (x <= y + tol) ? (x + tol < y) ? -1 : 0 : 1; n } n struct point { n   double x, y; n   point(double x = 0, double y = 0) : x(x), y(y) {} n   point operator+(point q) { return point(x + q.x, y + q.y); } n   point operator-(point q) { return point(x - q.x, y - q.y); } n   point operator*(double t) { return point(x * t, y * t); } n   point operator/(double t) { return point(x / t, y / t); } n   double operator*(point q) { return x * q.x + y * q.y; } n   double operator%(point q) { return x * q.y - y * q.x; } n   double polar() { return ((y > -1e-7) ? atan2(y, x) : 2 * Pi + atan2(y, x)); } n   double mod() { return sqrt(x * x + y * y); } n   double mod2() { return (x * x + y * y); } n   point rotate(double t) { n     return point(x * cos(t) - y * sin(t), x * sin(t) + y * cos(t)); n   } n   point rotate(double sin, double cos) { n     return point(x * cos - y * sin, x * sin + y * cos); n   } n   int cmp(point q) const { n     if (int t = ::cmp(x, q.x)) return t; n     return ::cmp(y, q.y); n   } n   bool operator==(point q) const { return cmp(q) == 0; } n   bool operator!=(point q) const { return cmp(q) != 0; } n   bool operator<(point q) const { return cmp(q) < 0; } n   static point pivot; n }; n point point::pivot; n double abs(point p) { return hypot(p.x, p.y); } n double arg(point p) { return atan2(p.y, p.x); } n inline int ccw(point p, point q, point r) { return cmp((p - r) % (q - r)); } n point proj(point v, point u) { return u * ((u * v) / (u * u)); } n inline double angle(point p, point q, point r) { n   point u = p - q, v = r - q; n   return atan2(u % v, u * v); n } n bool between(point p, point q, point r) { n   return ccw(p, q, r) == 0 && cmp((p - q) * (r - q)) <= 0; n } n bool seg_intersect(point p, point q, point r, point s) { n   point A = q - p, B = s - r, C = r - p, D = s - q; n   int a = cmp(A % C) + 2 * cmp(A % D); n   int b = cmp(B % C) + 2 * cmp(B % D); n   if (a == 3 || a == -3 || b == 3 || b == -3) return false; n   if (a || b || p == r || p == s || q == r || q == s) return true; n   int t = (p < r) + (p < s) + (q < r) + (q < s); n   return t != 0 && t != 4; n } n double seg_distance(point p, point q, point r) { n   point A = r - q, B = r - p, C = q - p; n   double a = A * A, b = B * B, c = C * C; n   if (cmp(b, a + c) >= 0) n     return sqrt(a); n   else if (cmp(a, b + c) >= 0) n     return sqrt(b); n   else n     return fabs(A % B) / sqrt(c); n } n int in_poly(point p, vector<point>& T) { n   double a = 0; n   int N = T.size(); n   for (int i = 0; i < N; i++) { n     if (between(T[i], p, T[(i + 1) % N])) return -1; n     a += angle(T[i], p, T[(i + 1) % N]); n   } n   return cmp(a) != 0; n } n bool radial_lt(point p, point q) { n   point P = p - point::pivot, Q = q - point::pivot; n   double R = P % Q; n   if (cmp(R)) return R > 0; n   return cmp(P * P, Q * Q) < 0; n } n vector<point> convex_hull(vector<point>& T) { n   int j = 0, k, n = T.size(); n   vector<point> U(n); n   point::pivot = *min_element((T).begin(), (T).end()); n   sort((T).begin(), (T).end(), radial_lt); n   for (k = n - 2; k >= 0 && ccw(T[0], T[n - 1], T[k]) == 0; k--) n     ; n   reverse((k + 1) + (T).begin(), (T).end()); n   for (int i = 0; i < n; i++) { n     while (j > 1 && ccw(U[j - 1], U[j - 2], T[i]) >= 0) j--; n     U[j++] = T[i]; n   } n   U.erase(j + (U).begin(), (U).end()); n   return U; n } n point line_intersect(point p, point q, point r, point s) { n   point a = q - p, b = s - r, c = point(p % q, r % s); n   return point(point(a.x, b.x) % c, point(a.y, b.y) % c) / (a % b); n } n double poly_area(vector<point>& T) { n   double s = 0; n   int n = T.size(); n   for (int i = 0; i < n; i++) s += T[i] % T[(i + 1) % n]; n   return s / 2; n } n struct segment { n   point a, b; n   segment(point a = point(0, 0), point b = point(0, 0)) : a(a), b(b) {} n   void input() { n     cin >> a.x >> a.y >> b.x >> b.y; n     if (b < a) swap(a, b); n   } n   void output() { n     printf( a=(%.1f  %.1f)  b=(%.1f  %.1f) n , a.x, a.y, b.x, b.y); n   } n   bool operator<(const segment& q) const { n     return a < q.a || (a == q.a && b < q.b); n   } n }; n bool solve(segment s[4]) { n   if (s[0].a != s[1].a) return false; n   if (s[0].b != s[2].a) return false; n   if (s[1].b != s[3].a) return false; n   if (s[2].b != s[3].b) return false; n   if (cmp(s[0].a.x, s[0].b.x) != 0) return false; n   if (cmp(s[1].a.y, s[1].b.y) != 0) return false; n   if (cmp(s[2].a.y, s[2].b.y) != 0) return false; n   if (cmp(s[3].a.x, s[3].b.x) != 0) return false; n   if (s[0].a == s[0].b) return false; n   if (s[1].a == s[1].b) return false; n   return true; n } n int main() { n   segment s[4]; n   for (int i = 0; i < 4; i++) { n     s[i].input(); n   } n   sort(s, s + 4); n   printf( %s n , (solve(s) ?  YES  :  NO )); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   int n, m; n   cin >> n >> m; n   vector<int> arr(n); n   for (auto& x : arr) cin >> x; n   vector<int> p(m); n   for (int i = 0; i < m; i++) p[i] = i; n   vector<vector<int>> mp(m); n   int ans = 0; n   for (int i = 0; i < n; i++) { n     if (i > 0 && arr[i - 1] != arr[i]) ans++; n     mp[arr[i] - 1].push_back(i); n   } n   cout << ans << endl; n   for (int i = 0; i < m - 1; i++) { n     int u, v; n     cin >> u >> v; n     u--, v--; n     while (p[u] != u) { n       u = p[u]; n     } n     while (p[v] != v) { n       v = p[v]; n     } n     if (mp[v].size() > mp[u].size()) swap(u, v); n     for (int x : mp[v]) { n       if (x + 1 < n && arr[x + 1] == u + 1) ans--; n       if (x > 0 && arr[x - 1] == u + 1) ans--; n     } n     for (int x : mp[v]) { n       arr[x] = u + 1; n       mp[u].push_back(x); n     } n     mp[v].clear(); n     p[v] = u; n     cout << ans << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n int main() { n   int n; n   scanf( %d , &n); n   if (n % 2 == 0) n     printf(  Mahmoud ); n   else n     printf(  Ehab ); n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long a; n   cin >> a; n   map<string, long long> arr; n   for (long long i = 0; i < a; i++) { n     string s1; n     cin >> s1; n     arr[s1] = -i; n     arr.insert(pair<string, long long>(s1, -i)); n   } n   vector<pair<long long, string>> ar1; n   for (auto it = arr.begin(); it != arr.end(); it++) { n     ar1.push_back(make_pair(it->second, it->first)); n   } n   sort(ar1.begin(), ar1.end()); n   for (auto it = ar1.begin(); it != ar1.end(); it++) { n     cout << it->second << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n bool check[100]; n int main() { n   int n; n   string s; n   cin >> n; n   cin >> s; n   int ans = 0; n   for (int x = 0; x < s.size(); ++x) { n     if (!check[s[x] -  a ]) { n       ans++; n       check[s[x] -  a ] = 1; n     } n   } n   cout << ((ans >= n) ?  YES  :  NO ) << endl; n   if (ans >= n) { n     for (int x = 0; x < s.size(); ++x) { n       if (n) { n         if (check[s[x] -  a ]) { n           if (x) cout << endl; n           cout << s[x]; n           check[s[x] -  a ] = 0; n           n--; n         } else { n           cout << s[x]; n         } n       } else n         cout << s[x]; n     } n     cout << endl; n   } n } n


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


#include <bits/stdc++.h> n using namespace std; n const int N = (int)2e5 + 5; n const int K = 30; n int p[N]; n char s[N]; n int get(int l, int r, int s) { n   l = max(l, s * K); n   if (l > r) return 0; n   return r / s - l / s; n } n int main() { n   scanf( %s , s); n   int n = strlen(s); n   for (int i = 0; i < n; ++i) p[i + 1] = p[i] + (int)(s[i] ==  1 ); n   long long ans = 0; n   map<int, int> cnt; n   for (int k = 1; k <= K; ++k) { n     cnt.clear(); n     for (int i = 0; i <= n; ++i) ++cnt[i - k * p[i]]; n     for (auto pr : cnt) ans += pr.second * (long long)(pr.second - 1) / 2; n   } n   vector<int> one; n   for (int i = 0; i < n; ++i) n     if (s[i] ==  1 ) one.push_back(i); n   one.push_back(n); n   reverse((one).begin(), (one).end()); n   for (int i = 0; i < n; ++i) { n     int sz = (int)(one).size(); n     for (int j = 0; j < min(n / K + 1, sz - 1); ++j) n       ans += get(one[sz - j - 1] - i, one[sz - (j + 1) - 1] - i, j + 1); n     if (s[i] ==  1 ) one.pop_back(); n   } n   printf( %lld n , ans); n } n


#include <bits/stdc++.h> n using namespace std; n int main(void) { n   int n, k, link; n   cin >> n >> k; n   vector<int> ans(n + 1); n   for (int i = 1; i <= n; i++) { n     cin >> link; n     ans[i] = min(n, i + k) - max(1, i - k) + 1; n     if (link != 0) { n       ans[i] += ans[link] - max(0, (min(n, link + k) - max(1, i - k) + 1)); n     } n     cout << ans[i] <<    ; n   } n   cout << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   int TESTS = 1; n   while (TESTS--) { n     long long int n, x = 1, ans = 0, w = 0, q; n     string s; n     cin >> n; n     long long int p = pow(2, 32) - 1; n     stack<long long int> stackstack; n     for (int i = 0; i < n; i++) { n       cin >> s; n       if (s ==  for ) cin >> q; n       if (s ==  add  && stackstack.empty() == 1) { n         if (ans + 1 > p) { n           cout <<  OVERFLOW!!! ; n           return 0; n         } else n           ans += 1; n       } else if (s ==  add ) { n         if (stackstack.top() == p + 1) { n           cout <<  OVERFLOW!!! ; n           return 0; n         } n         if (stackstack.top() + ans > p) { n           cout <<  OVERFLOW!!! ; n           return 0; n         } n         ans += stackstack.top(); n       } else if (s ==  end ) { n         stackstack.pop(); n       } else if (s ==  for ) { n         if (stackstack.empty()) n           x = 1; n         else n           x = stackstack.top(); n         if (x * q > p) n           stackstack.push(p + 1); n         else n           stackstack.push(x * q); n       } n     } n     cout << ans; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct pawn { n   long long int r; n   long long int c; n }; n struct pawn b[100], w[100]; n int main() { n   ios::sync_with_stdio(false); n   char a; n   long long int l = 0, h = 0; n   for (int i = 1; i <= 8; i++) { n     for (int j = 1; j <= 8; j++) { n       cin >> a; n       if (a ==  B ) { n         b[l].r = i; n         b[l].c = j; n         l++; n       } else if (a ==  W ) { n         w[h].r = i; n         w[h].c = j; n         h++; n       } n     } n   } n   long long int w_r, w_c; n   for (int i = 0; i < h; i++) { n     int f = 0; n     for (int j = 0; j < l; j++) { n       if (b[j].r < w[i].r && b[j].c == w[i].c) { n         f = 1; n         break; n       } n     } n     if (f == 0) { n       w_r = w[i].r; n       w_c = w[i].c; n       break; n     } n   } n   long long int b_r, b_c; n   for (int i = l - 1; i >= 0; i--) { n     int f = 0; n     for (int j = h - 1; j >= 0; j--) { n       if (w[j].r > b[i].r && w[j].c == b[i].c) { n         f = 1; n         break; n       } n     } n     if (f == 0) { n       b_r = b[i].r; n       b_c = b[i].c; n       break; n     } n   } n   long long int diff_w = w_r - 1; n   long long int diff_b = 8 - b_r; n   if (diff_w == diff_b) n     cout <<  A  << endl; n   else if (diff_w < diff_b) n     cout <<  A  << endl; n   else n     cout <<  B  << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void MAIN() { n   long long N; n   cin >> N; n   string S = to_string(N); n   reverse(S.begin(), S.end()); n   int sz = (int)S.size(); n   long long ans = -2; n   for (int i = 0; i < (1 << sz); i++) { n     int i_c = i; n     if ((i_c >> (sz - 1) & 1) || (i_c >> (sz - 2) & 1)) { n       continue; n     } n     string T = S; n     long long now = 1; n     for (int j = 0; j < sz; j++) { n       if (i_c >> j & 1) { n         int X = 10 + (int)(T[j] -  0 ); n         now *= 19 - X; n         if (T[j + 2] ==  0 ) { n           if (i_c >> (j + 2) & 1) { n             T[j + 2] =  9 ; n             i_c ^= (1 << (j + 2)); n           } else { n             now = 0; n             break; n           } n         } else { n           T[j + 2]--; n         } n       } else { n         now *= (int)(T[j] -  0 ) + 1; n         if (T[j] ==  9  && j >= 2 && (i >> (j - 2) & 1)) { n           T[j + 2]--; n         } n       } n     } n     ans += now; n   } n   cout << ans <<   n ; n } n int main() { n   ios::sync_with_stdio(false); n   cin.tie(nullptr); n   int T; n   cin >> T; n   while (T--) { n     MAIN(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n inline long long ispossible(vector<long long>& b, vector<long long>& p, int k, n                             long long a) { n   int n = b.size(); n   if (k > p.size()) return -1; n   long long c = 0, s = 0, extra = 0; n   for (int i = n - k; i < n; i++) { n     c += p[i - n + k]; n     extra += max(p[i - n + k] - b[i], 0ll); n   } n   if (extra > a) return -1; n   long long e = c - a; n   return e > 0 ? e : 0; n } n int main() { n   int n, m; n   long long a; n   cin >> n >> m >> a; n   vector<long long> b(n, 0), p(m, 0); n   for (int i = 0; i < n; i++) cin >> b[i]; n   for (int i = 0; i < m; i++) cin >> p[i]; n   sort(b.begin(), b.end()); n   sort(p.begin(), p.end()); n   int l, r; n   long long tcost, cost = 0; n   l = 0, r = n; n   while (l < r) { n     m = (l + r) >> 1; n     tcost = ispossible(b, p, m, a); n     if (tcost >= 0) { n       l = m + 1; n       cost = tcost; n     } else n       r = m; n   } n   if (l <= r) { n     tcost = ispossible(b, p, r, a); n     if (tcost >= 0) { n       l = r + 1; n       cost = tcost; n     } n   } n   cout << l - 1 <<     << cost << endl; n   return 0; n } n


#include <bits/stdc++.h> n inline int dist(int a, int b, int d) { n   assert(a % d && b % d); n   if (a >= 0) n     a /= d; n   else n     a = a / d - 1; n   if (b >= 0) n     b /= d; n   else n     b = b / d - 1; n   return abs(a - b); n } n int main() { n   int a, b, x1, y1, x2, y2; n   scanf( %d%d%d%d%d%d , &a, &b, &x1, &y1, &x2, &y2); n   printf( %d n , n          ((dist(x1 + y1, x2 + y2, 2 * a)) > (dist(x1 - y1, x2 - y2, 2 * b)) n               ? (dist(x1 + y1, x2 + y2, 2 * a)) n               : (dist(x1 - y1, x2 - y2, 2 * b)))); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n using ll = long long; n using pll = pair<ll, ll>; n using dd = long double; n namespace { n static bool constexpr dbg = 0; n ll constexpr N = -1; n ll n, a, b, c; n void init() { cin >> n >> a >> b >> c; } n void solve() { n   if (n == 1) n     cout << 0; n   else if (n == 2) n     cout << min(a, b); n   else n     cout << (min(a, b) + (n - 2) * min({a, b, c})); n } n }  // namespace n int main() { n   if (!dbg) { n     std::ios::sync_with_stdio(0); n     cin.tie(0); n     cout.tie(0); n   } n   cout << fixed << setprecision(20); n   init(); n   solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long P[200020]; n long long n, m; n long long Mod = 998244353; n long long Power(long long a, long long b) { n   if (b < 1) return 1; n   long long Tmp = Power(a, b / 2); n   Tmp = Tmp * Tmp % Mod; n   if (b & 1) Tmp = Tmp * a % Mod; n   return Tmp; n } n long long inv(long long a) { return Power(a, Mod - 2); } n long long C(long long a, long long b) { n   if (b < 0 || a < 0 || a - b < 0) return 0; n   return P[a] * inv(P[a - b] * P[b] % Mod) % Mod; n } n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0), cout.tie(0); n   cin >> n >> m; n   P[0] = 1; n   for (int i = 1; i < 200020; i++) P[i] = i * P[i - 1] % Mod; n   long long Ans = C(m, n - 1) * (n - 2) % Mod * Power(2, n - 3) % Mod; n   cout << Ans <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int a[100100], id[100100], t, Ans, ans[100100], n, i, vis[100100], j, k; n int flag; n vector<int> vec[100100]; n bool cmp(int u, int v) { return a[u] < a[v]; } n int main() { n   scanf( %d , &n); n   for (i = 1; i <= n; i++) { n     scanf( %d , &a[i]); n     id[i] = i; n   } n   sort(id + 1, id + 1 + n, cmp); n   vec[1].push_back(1); n   if (n > 1) vec[1].push_back(n); n   vis[1] = vis[n] = vis[0] = vis[n + 1] = 1; n   ; n   j = 1; n   while (a[id[j]] == 1) { n     if (vis[id[j]] == 0) { n       vec[1].push_back(id[j]); n       vis[id[j]] = 1; n     } n     j++; n   } n   for (i = 2; i <= n; i++) { n     flag = 0; n     while (a[id[j]] == i) { n       if (vis[id[j]] == 0) { n         vec[i].push_back(id[j]); n         vis[id[j]] = 1; n         flag = 1; n       } n       j++; n     } n     for (k = 0; k < vec[i - 1].size(); k++) { n       t = vec[i - 1][k]; n       if (vis[t - 1] == 0) { n         vis[t - 1] = 1; n         vec[i].push_back(t - 1); n         flag = 1; n       } n       if (vis[t + 1] == 0) { n         vis[t + 1] = 1; n         vec[i].push_back(t + 1); n         flag = 1; n       } n     } n     if (!flag) break; n   } n   printf( %d n , i - 1); n } n


#include <bits/stdc++.h> n const int N = 3e5 + 10; n const int mod = 1e9 + 7; n int n; n std::vector<int> edge[N]; n std::pair<int, int> v[N]; n int q; n std::pair<int, int> query[N]; n int queue[N]; n std::pair<int, int> range[N]; n int father[N]; n int degree[N]; n int fastPower(int x, int y) { n   int ret = 1; n   for (; y; y >>= 1) { n     if (y & 1) { n       ret = (long long)ret * x % mod; n     } n     x = (long long)x * x % mod; n   } n   return ret; n } n int inv(int x) { return fastPower(x, mod - 2); } n int lowbit(int x) { return x & (-x); } n struct FenwickTree { n   int e[N]; n   void build(int size) { n     for (int i = 1; i <= size; i++) { n       e[i] = 1; n     } n   } n   void change(int x, int value) { n     for (int i = x; i <= n; i += lowbit(i)) { n       e[i] = (long long)e[i] * value % mod; n     } n   } n   int getValue(int x) { n     int ret = 1; n     for (int i = x; i >= 1; i -= lowbit(i)) { n       ret = (long long)ret * e[i] % mod; n     } n     return ret; n   } n }; n FenwickTree ft; n struct SegmentTree { n   struct Node { n     int sum; n     int tag; n   }; n   Node tree[N << 2]; n   void update(int root, int left, int right) { n     tree[root].sum = (long long)tree[root].sum * tree[root].tag % mod; n     if (left != right) { n       tree[root << 1].tag = n           (long long)tree[root << 1].tag * tree[root].tag % mod; n       tree[root << 1 | 1].tag = n           (long long)tree[root << 1 | 1].tag * tree[root].tag % mod; n     } n     tree[root].tag = 1; n   } n   void change(int root, int left, int right, int pos, int value) { n     update(root, left, right); n     if (left == pos && right == pos) { n       tree[root].sum = value; n       return; n     } n     int mid = (left + right) >> 1; n     if (pos <= mid) { n       change(root << 1, left, mid, pos, value); n     } else { n       change(root << 1 | 1, mid + 1, right, pos, value); n     } n     update(root << 1, left, mid); n     update(root << 1 | 1, mid + 1, right); n     tree[root].sum = tree[root << 1].sum + tree[root << 1 | 1].sum; n   } n   void change2(int root, int left, int right, int ql, int qr, int value) { n     update(root, left, right); n     if (left == ql && right == qr) { n       tree[root].tag = value; n       update(root, left, right); n       return; n     } n     int mid = (left + right) >> 1; n     if (qr <= mid) { n       change2(root << 1, left, mid, ql, qr, value); n     } else if (ql > mid) { n       change2(root << 1 | 1, mid + 1, right, ql, qr, value); n     } else { n       change2(root << 1, left, mid, ql, mid, value); n       change2(root << 1 | 1, mid + 1, right, mid + 1, qr, value); n     } n     update(root << 1, left, mid); n     update(root << 1 | 1, mid + 1, right); n     tree[root].sum = tree[root << 1].sum + tree[root << 1 | 1].sum; n   } n   int getSum(int root, int left, int right, int ql, int qr) { n     update(root, left, right); n     if (left == ql && right == qr) { n       return tree[root].sum; n     } n     int mid = (left + right) >> 1; n     if (qr <= mid) { n       return getSum(root << 1, left, mid, ql, qr); n     } else if (ql > mid) { n       return getSum(root << 1 | 1, mid + 1, right, ql, qr); n     } else { n       return (getSum(root << 1, left, mid, ql, mid) + n               getSum(root << 1 | 1, mid + 1, right, mid + 1, qr)) % n              mod; n     } n   } n }; n SegmentTree st; n void init() { n   std::cin >> v[1].second >> q; n   n = 1; n   for (int i = 1; i <= q; i++) { n     scanf( %d%d , &query[i].first, &query[i].second); n     if (query[i].first == 1) { n       father[++n] = query[i].second; n       scanf( %d , &v[n].second); n       edge[father[n]].push_back(n); n     } n   } n } n void dfs(int u) { n   static int tot = 0; n   queue[++tot] = u; n   range[u].first = tot; n   for (int i = 0; i < (int)edge[u].size(); i++) { n     dfs(edge[u][i]); n   } n   range[u].second = tot; n } n void work() { n   dfs(1); n   std::fill(degree + 1, degree + n + 1, 1); n   ft.build(n); n   int tmp = 1; n   v[1].first = v[1].second; n   st.change(1, 1, n, range[1].first, v[1].first); n   for (int i = 1; i <= q; i++) { n     if (query[i].first == 1) { n       int fa = query[i].second; n       tmp++; n       v[tmp].first = v[tmp].second; n       degree[fa]++; n       int cc = (long long)degree[fa] * inv(degree[fa] - 1) % mod; n       ft.change(range[fa].first, cc); n       ft.change(range[fa].second + 1, inv(cc)); n       st.change2(1, 1, n, range[fa].first, range[fa].second, cc); n       st.change(1, 1, n, range[tmp].first, n                 (long long)v[tmp].first * ft.getValue(range[tmp].first) % mod); n     } else { n       int u = query[i].second; n       int answer = st.getSum(1, 1, n, range[u].first, range[u].second); n       if (u != 1) { n         answer = n             (long long)answer * inv(ft.getValue(range[father[u]].first)) % mod; n       } n       printf( %d n , answer); n     } n   } n } n int main() { n   init(); n   work(); n   return 0; n } n


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


#include <bits/stdc++.h> n using namespace std; n int dp[5005][5005]; n string s; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cin >> s; n   memset(dp, -1, sizeof dp); n   int ans = 0; n   for (int i = 0; i < s.size(); i++) { n     int can_rev = 0; n     int cur = 0; n     for (int j = i; j < s.size(); j++) { n       if (cur == 0) ans++; n       if (s[j] ==  ( ) { n         cur++; n       } else if (s[j] ==  ) ) { n         cur--; n         if (cur < 0) { n           if (can_rev == 0) break; n           can_rev--; n           cur += 2; n         } n       } else { n         if (cur > 0) { n           cur--; n           can_rev++; n         } else { n           cur++; n         } n       } n     } n     if (cur == 0) ans++; n     ans--; n   } n   cout << ans <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0), cout.tie(0); n   ; n   long long int t, i; n   cin >> t; n   while (t--) { n     long long int n, x, tt = 0; n     vector<pair<long long int, long long int>> v, v1; n     multimap<long long int, long long int> m, u; n     multiset<long long int> s; n     cin >> n; n     for (long long int i = 0; i < n; i++) { n       cin >> x; n       v.push_back({x, i}); n       m.insert({x, i}); n     } n     long long int c = 0, a, b, min = INT_MAX, j, k; n     if (n < 2) { n       cout <<  -1  <<   n ; n       continue; n     } else { n       sort((v).begin(), (v).end()); n       for (long long int j = 0; j < v.size() - 1; j++) { n         if (v[j].first != v[j + 1].first) n           continue; n         else { n           tt = 1; n           for (long long int k = j + 1; k < v.size(); k++) { n             if (v[j].first == v[k].first) n               c = v[k].second - v[j].second; n             else n               break; n             if (c == 1) { n               min = c; n               break; n             } else if (c < min) n               min = c; n           } n         } n       } n       if (tt == 0) n         cout <<  -1  <<   n ; n       else n         cout << min + 1 <<   n ; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 1e6 + 10; n const long long inf = 1e15; n const int mod = 1e6; n void faster() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n } n const long long N = 3e5 + 10; n int n, m; n string s; n pair<long long, long long> p[N]; n long long prefix[N]; n int main() { n   faster(); n   int n; n   cin >> n; n   prefix[0] = 1; n   for (int i = 0; i < n; i++) { n     cin >> p[i].first >> p[i].second; n   } n   sort(p, p + n); n   long long mini = p[0].first; n   long long maxi = prefix[0]; n   for (int i = 1; i < n; i++) { n     if (p[i].first - p[i].second >= mini) { n       auto q = lower_bound(p, p + i, make_pair(p[i].first - p[i].second, 0ll)); n       q--; n       int ind = q - p; n       prefix[i] = prefix[ind] + 1; n     } else n       prefix[i] = 1; n     maxi = max(maxi, prefix[i]); n   } n   cout << n - maxi << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   std::ios_base::sync_with_stdio(false); n   long long n; n   cin >> n; n   long long nmax = 9999999999; n   while ((nmax / 2) >= n) nmax /= 10; n   int inc = nmax + 1; n   long long ans = 0; n   if (nmax == 0) { n     ans = (n * (n - 1)) / 2; n     cout << ans << endl; n     return 0; n   } n   while (n >= (nmax / 2)) { n     cerr <<  ans  n          <<   is   << ans << endl; n     ; n     if (n >= nmax) n       ans += (nmax / 2); n     else n       ans += n - (nmax / 2); n     nmax += inc; n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n char a[99]; n char b[99]; n int n; n int c, d; n int get(char p, char q, char r, char s) { n   if (p == r) return 0; n   if (p ==  ( ) { n     if (r ==  [ ) return -1; n     return 1; n   } else if (p ==  [ ) { n     if (r ==  ( ) return 1; n     return -1; n   } else { n     if (r ==  ( ) return -1; n     return 1; n   } n } n int main() { n   int i, j; n   scanf( %s %s , a, b); n   n = strlen(a); n   for (i = 0; i < n; i += 2) { n     j = get(a[i], a[i + 1], b[i], b[i + 1]); n     if (j == 1) n       c++; n     else if (j == -1) n       c--; n   } n   if (c == 0) n     printf( TIE n ); n   else if (c > 0) n     printf( TEAM 1 WINS n ); n   else n     printf( TEAM 2 WINS n ); n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   int x = 0; n   int n; n   cin >> n; n   for (int i = 0; i < n; i++) { n     string statement; n     cin >> statement; n     if (statement.find( + ) != std::string::npos) { n       x += 1; n     } else { n       x -= 1; n     } n   } n   cout << x << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long n, h, i, j, t, m, l, r, q; n   long long p = 0, k, w, z, x, y, d; n   string s =   , st =   , s1 =   , s2 =   ; n   q = 1; n   for (l = 0; l < q; l++) { n     int x1, y1, x2, y2; n     std::cin >> x1 >> y1 >> x2 >> y2 >> x >> y; n     p = x2 - x1; n     h = y2 - y1; n     z = 0; n     if (p % x == 0 && h % y == 0) { n       if (p / x == h / y) n         z = 1; n       else { n         p = p / x; n         h = h / y; n         d = abs(p - h); n         if (d % 2 == 0) z = 1; n       } n     } n     if (z == 0) n       std::cout <<  NO  << std::endl; n     else n       std::cout <<  YES  << std::endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int INF = (int)1E9; n const long long LINF = (long long)1E18; n int INP, AM; n char BUF[(1 << 10) + 1], *inp = BUF; n template <typename T> n T gcd(T a, T b) { n   return (b == 0) ? abs(a) : gcd(b, a % b); n } n template <typename T> n inline T lcm(T a, T b) { n   return a / gcd(a, b) * b; n } n template <typename T> n inline T sqr(T x) { n   return x * x; n } n const char DEBUG_PARAM[] =  __LOCAL_TESTING ; n const char IN[] =  input.txt ; n const char OUT[] =  output.txt ; n int ntest = 0, test; n inline void init(); n inline void run(); n inline void stop() { ntest = test - 1; } n int main(int argc, char* argv[]) { n   if (argc > 1 && strcmp(argv[1], DEBUG_PARAM) == 0) { n     freopen(IN,  r , stdin); n   } n   init(); n   if (ntest == 0) { n     puts( ntest = ? ); n     return 0; n   } n   for (test = 1; test <= ntest; test++) { n     run(); n   } n   return 0; n } n const int dx[] = {-1, 0, 0, 1}; n const int dy[] = {0, -1, 1, 0}; n const long double EPS = 1E-9; n const long long MODULO = 1000000007LL; n inline void init() { ntest = 1; } n const int maxn = 100000 + 5; n pair<int, int> p[maxn]; n int n; n vector<int> X, Y; n int sumX, sumY; n void solve() { n   int limit = ceil((double)n / 2); n   for (int i = (n); i >= (1); i--) { n     if (((int)(X).size()) == limit) { n       Y.push_back(p[i].second); n       sumY += p[i].first; n     } else if (((int)(Y).size()) == limit) { n       X.push_back(p[i].second); n       sumX += p[i].first; n     } else { n       if (sumX < sumY) { n         X.push_back(p[i].second); n         sumX += p[i].first; n       } else { n         Y.push_back(p[i].second); n         sumY += p[i].first; n       } n     } n   } n   cout << ((int)(X).size()) << endl; n   for (int i = 0; i < (((int)(X).size())); i++) { n     cout << X[i]; n     if (i != ((int)(X).size()) - 1) cout <<    ; n   } n   cout << endl; n   cout << ((int)(Y).size()) << endl; n   for (int i = 0; i < (((int)(Y).size())); i++) { n     cout << Y[i]; n     if (i != ((int)(Y).size()) - 1) cout <<    ; n   } n } n inline void run() { n   cin >> n; n   for (int i = (1); i <= (n); i++) { n     cin >> p[i].first; n     p[i].second = i; n   } n   sort(p + 1, p + 1 + n); n   solve(); n } n


#include <bits/stdc++.h> n using namespace std; n using ll = long long; n using ull = unsigned long long; n template <typename... Args> n void writeln(Args... args) { n   ((cout << args <<    ), ...); n   cout << endl; n } n template <typename... Args> n void _db(Args... args) { n   ((cerr << args <<   ;;  ), ...); n   cout << endl; n } n int main() { n   ios::sync_with_stdio(false); n   int n; n   cin >> n; n   vector<pair<ll, ll>> v(n); n   for (int i = 0; i < n; ++i) { n     cin >> v[i].first >> v[i].second; n   } n   sort(v.begin(), v.end(), [](const pair<ll, ll>& f, const pair<ll, ll>& s) { n     return abs(f.first) + abs(f.second) < abs(s.first) + abs(s.second); n   }); n   int count = n * 2; n   for (auto [x, y] : v) { n     if (x != 0) { n       count += 2; n     } n     if (y != 0) { n       count += 2; n     } n   } n   cout << count << endl; n   for (auto [x, y] : v) { n     if (x != 0) { n       char dir = x > 0 ?  R  :  L ; n       cout << 1 <<     << abs(x) <<     << dir <<   n ; n     } n     if (y != 0) { n       char dir = y > 0 ?  U  :  D ; n       cout << 1 <<     << abs(y) <<     << dir <<   n ; n     } n     cout << 2 <<   n ; n     if (x != 0) { n       char dir = x > 0 ?  L  :  R ; n       cout << 1 <<     << abs(x) <<     << dir <<   n ; n     } n     if (y != 0) { n       char dir = y > 0 ?  D  :  U ; n       cout << 1 <<     << abs(y) <<     << dir <<   n ; n     } n     cout << 3 <<   n ; n   } n   flush(cout); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long MAXN = 1e6 + 5; n const long long MAXM = 4e7 + 5; n const int MOD = 998244353; n long long n, m, p; n struct node1 { n   long long x, c; n } a[MAXN], b[MAXN]; n struct node2 { n   long long x, y, z; n } c[MAXN]; n struct node { n   long long l, r, val, mark; n } t[MAXN << 2]; n void build(long long rt, long long l, long long r) { n   t[rt].l = l, t[rt].r = r; n   t[rt].mark = 0; n   if (l == r) { n     t[rt].val = -b[l].c; n     return; n   } n   long long mid = (t[rt].l + t[rt].r) >> 1; n   build(rt << 1, l, mid); n   build(rt << 1 | 1, mid + 1, r); n   t[rt].val = max(t[rt << 1].val, t[rt << 1 | 1].val); n } n void spread(long long rt) { n   t[rt << 1].val += t[rt].mark; n   t[rt << 1 | 1].val += t[rt].mark; n   t[rt << 1].mark += t[rt].mark; n   t[rt << 1 | 1].mark += t[rt].mark; n   t[rt].mark = 0; n } n void update(long long rt, long long l, long long r, long long val) { n   if (l > r) return; n   if (t[rt].l >= l && t[rt].r <= r) { n     t[rt].val += val; n     t[rt].mark += val; n     return; n   } n   spread(rt); n   long long mid = (t[rt].l + t[rt].r) >> 1; n   if (l <= mid) update(rt << 1, l, r, val); n   if (r > mid) update(rt << 1 | 1, l, r, val); n   t[rt].val = max(t[rt << 1].val, t[rt << 1 | 1].val); n } n bool cmp(node1 a, node1 b) { return a.x < b.x; } n bool cmp1(node2 a, node2 b) { return a.x < b.x; } n long long q[MAXN]; n int main() { n   while (~scanf( %I64d%I64d%I64d , &n, &m, &p)) { n     for (long long i = 1; i <= n; i++) scanf( %I64d%I64d , &a[i].x, &a[i].c); n     for (long long i = 1; i <= m; i++) scanf( %I64d%I64d , &b[i].x, &b[i].c); n     sort(a + 1, a + n + 1, cmp); n     sort(b + 1, b + m + 1, cmp); n     for (long long i = 1; i <= m; i++) q[i] = b[i].x; n     build(1, 1, m); n     for (long long i = 1; i <= p; i++) { n       scanf( %I64d%I64d%I64d , &c[i].x, &c[i].y, &c[i].z); n     } n     sort(c + 1, c + p + 1, cmp1); n     long long pos = 1; n     long long ans = -1ll << 62; n     for (long long i = 1; i <= n; i++) { n       while (pos <= p && c[pos].x < a[i].x) { n         long long pp = upper_bound(q + 1, q + m + 1, c[pos].y) - q; n         update(1, pp, m, c[pos].z); n         pos++; n       } n       ans = max(ans, (long long)t[1].val - a[i].c); n     } n     printf( %I64d n , ans); n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int r, c, x, y; n   string s; n   cin >> r >> c >> x >> y; n   cin >> s; n   vector<int> visited(r * c + 1, 0); n   cout <<  1  ; n   int t, n = s.size(); n   visited[c * (x - 1) + y] = 1; n   for (int i = 0; i < n - 1; i++) { n     if (s[i] ==  U  && (x - 1 >= 1 && x - 1 <= r)) { n       x -= 1; n       t = c * (x - 1) + y; n       if (visited[t]) n         cout <<  0  ; n       else { n         visited[t] = 1; n         cout <<  1  ; n       } n     } else if (s[i] ==  U ) n       cout <<  0  ; n     else if (s[i] ==  D  && (x + 1 >= 1 && x + 1 <= r)) { n       x += 1; n       t = c * (x - 1) + y; n       if (visited[t]) n         cout <<  0  ; n       else { n         visited[t] = 1; n         cout <<  1  ; n       } n     } else if (s[i] ==  D ) n       cout <<  0  ; n     else if (s[i] ==  L  && (y - 1 >= 1 && y - 1 <= c)) { n       y -= 1; n       t = c * (x - 1) + y; n       if (visited[t]) n         cout <<  0  ; n       else { n         visited[t] = 1; n         cout <<  1  ; n       } n     } else if (s[i] ==  L ) n       cout <<  0  ; n     else if (s[i] ==  R  && (y + 1 >= 1 && y + 1 <= c)) { n       y += 1; n       t = c * (x - 1) + y; n       if (visited[t]) n         cout <<  0  ; n       else { n         visited[t] = 1; n         cout <<  1  ; n       } n     } else n       cout <<  0  ; n   } n   t = 0; n   for (int i = 1; i <= r * c; i++) { n     if (visited[i] == 1) t++; n   } n   cout << r * c - t; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long x, xxxxx = 0; n string s, word; n long long indx = 0, lmt; n string v[100009], out =   ; n bool f_int = false; n bool ok() { n   if (indx > lmt) return xxxxx = 1, false; n   bool o1, o2; n   o1 = o2 = false; n   if (v[indx] ==  int  and indx != 1) { n     out +=  int ; n     f_int = true; n     return true; n   } n   ++indx; n   out +=  pair< ; n   o1 = ok(); n   ++indx; n   out +=  , ; n   o2 = ok(); n   if (!o2) xxxxx = 1, out +=  XXX ; n   out +=  > ; n   return true; n } n int main() { n   cin >> x; n   getchar(); n   getline(cin, s); n   stringstream ss(s); n   while (ss >> word) { n     v[++indx] = word; n   } n   if (indx == 1) { n     if (v[1] ==  int ) n       cout <<  int ; n     else n       cout <<  Error occurred ; n     return 0; n   } n   lmt = indx; n   indx = 1; n   ok(); n   if (xxxxx || !f_int || indx != lmt) n     cout <<  Error occurred ; n   else n     cout << out; n } n


#include <bits/stdc++.h> n using namespace std; n const int inf = 5000; n const int MAXN = 200, MAXE = 2 * MAXN; n int src, snk, nNode, nEdge; n int Q[MAXN], fin[MAXN], pro[MAXN], dist[MAXN]; n int flow[MAXE], cap[MAXE], Next[MAXE], to[MAXE]; n inline void init(int _src, int _snk, int _n) { n   src = _src, snk = _snk, nNode = _n, nEdge = 0; n   memset(fin, -1, sizeof(fin)); n } n inline void add(int u, int v, int c) { n   to[nEdge] = v, cap[nEdge] = c, flow[nEdge] = 0, Next[nEdge] = fin[u], n   fin[u] = nEdge++; n   to[nEdge] = u, cap[nEdge] = 0, flow[nEdge] = 0, Next[nEdge] = fin[v], n   fin[v] = nEdge++; n } n bool bfs() { n   int st, en; n   memset(dist, -1, sizeof(dist)); n   dist[src] = st = en = 0; n   Q[en++] = src; n   while (st < en) { n     int u = Q[st++]; n     for (int i = fin[u]; i >= 0; i = Next[i]) { n       int v = to[i]; n       if (flow[i] < cap[i] && dist[v] == -1) { n         dist[v] = dist[u] + 1; n         Q[en++] = v; n       } n     } n   } n   return (dist[snk] != -1); n } n int dfs(int u, int fl) { n   if (u == snk) return fl; n   for (int &e = pro[u]; e >= 0; e = Next[e]) { n     int v = to[e]; n     if (flow[e] < cap[e] && dist[v] == dist[u] + 1) { n       int df = dfs(v, min(cap[e] - flow[e], fl)); n       if (df > 0) { n         flow[e] += df; n         flow[e ^ 1] -= df; n         return df; n       } n     } n   } n   return 0; n } n long long dinitz() { n   long long ret = 0; n   while (bfs()) { n     for (int i = 0; i <= nNode; i++) pro[i] = fin[i]; n     while (true) { n       int df = dfs(src, inf); n       if (df) n         ret += (long long)df; n       else n         break; n     } n   } n   return ret; n } n int arr[MAXN], x[MAXN], y[MAXN]; n int main() { n   int n, m; n   scanf( %d %d , &n, &m); n   for (int i = 1; i <= n; i++) scanf( %d , &arr[i]); n   for (int i = 1; i <= m; i++) { n     scanf( %d %d , &x[i], &y[i]); n     if (x[i] % 2 == 0) swap(x[i], y[i]); n   } n   set<int> prime; n   for (int i = 1; i <= n; i++) { n     int nw = arr[i]; n     for (int p = 2; p * p <= nw; p++) { n       int cnt = 0; n       while (nw % p == 0) { n         ++cnt, nw /= p; n       } n       if (cnt) prime.insert(p); n     } n     if (nw > 1) prime.insert(nw); n   } n   long long tot = 0; n   for (set<int>::iterator it = prime.begin(); it != prime.end(); it++) { n     int p = *it; n     int s = 0, t = n + 1; n     init(s, t, n + 2); n     for (int i = 1; i <= n; i++) { n       int nw = arr[i], cnt = 0; n       while (nw % p == 0) ++cnt, nw /= p; n       if (cnt) { n         if (i % 2 == 1) n           add(s, i, cnt); n         else n           add(i, t, cnt); n       } n     } n     for (int i = 1; i <= m; i++) { n       add(x[i], y[i], inf); n     } n     tot += dinitz(); n   } n   printf( %lld n , tot); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n #pragma comment(linker,  /STACK:36777216 ) n const int Nmax = 100100; n const int Drection[4][2] = {{-1, 0}, {0, 1}, {1, 0}, {0, -1}}; n int h, q; n long long minV[55], maxV[55]; n long long leftDown(long long L, int hg) { n   if (hg == h) return L; n   return leftDown(L << 1ll, hg + 1); n } n long long rightDown(long long R, int hg) { n   if (hg == h) return R; n   return rightDown((R << 1ll) + 1, hg + 1); n } n inline void end(long long x) { n   if (x == 0) n     cout <<  Data not sufficient!  << endl; n   else if (x == -1) n     cout <<  Game cheated!  << endl; n   else n     cout << x << endl; n   exit(0); n } n inline void add(long long x) { n   static long long extra = -1; n   if (x <= 0) end(extra); n   if (extra != -1) end(0); n   extra = x; n } n int main() { n   ios_base::sync_with_stdio(0); n   cin >> h >> q; n   minV[1] = maxV[1] = 1; n   for (int i = 2; i <= h; i++) { n     minV[i] = minV[i - 1] * 2ll; n     maxV[i] = maxV[i - 1] * 2ll + 1; n   } n   vector<pair<long long, long long> > list; n   for (int i = 1; i <= q; i++) { n     long long lv, L, R, ans; n     cin >> lv >> L >> R >> ans; n     if (ans == 1) { n       if (minV[lv] <= L - 1) n         list.push_back(make_pair(leftDown(minV[lv], lv), rightDown(L - 1, lv))); n       if (R + 1 <= maxV[lv]) n         list.push_back(make_pair(leftDown(R + 1, lv), rightDown(maxV[lv], lv))); n     } else n       list.push_back(make_pair(leftDown(L, lv), rightDown(R, lv))); n   } n   sort(list.begin(), list.end()); n   long long L = leftDown(1, 1); n   long long R = rightDown(1, 1); n   for (int i = 0; i < list.size(); i++) { n     if (list[i].first > L) { n       add(L++); n       i--; n       continue; n     } n     L = max(L, list[i].second + 1); n   } n   while (L <= R) add(L++); n   add(-10); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int mod = 998244353, N = 5e5; n int qmod1(const int &x) { return x >= mod ? x - mod : x; } n int qmod2(const int &x) { return x + (x >> 31 & mod); } n int qpower(int a, int b) { n   int ans = 1; n   for (; b; b >>= 1, a = 1ll * a * a % mod) n     if (b & 1) ans = 1ll * ans * a % mod; n   return ans; n } n struct Z { n   int x; n   Z(int v = 0) { x = qmod2(v % mod); } n   int qmod1(const int &x) const { return x >= mod ? x - mod : x; } n   int qmod2(const int &x) const { return x + (x >> 31 & mod); } n   int qpower(int a, int b) const { n     int ans = 1; n     for (; b; b >>= 1, a = 1ll * a * a % mod) n       if (b & 1) ans = 1ll * ans * a % mod; n     return ans; n   } n   Z operator+(const Z &b) const { return (Z){qmod1(x + b.x)}; } n   Z &operator+=(const Z &b) { n     x += b.x; n     if (x >= mod) x -= mod; n     return *this; n   } n   Z operator-(const Z &b) const { return (Z){qmod2(x - b.x)}; } n   Z &operator-=(const Z &b) { n     x -= b.x; n     x += x >> 31 & mod; n     return *this; n   } n   Z operator*(const Z &b) const { return (Z){(int)(1ll * x * b.x % mod)}; } n   Z &operator*=(const Z &b) { n     x = 1ll * x * b.x % mod; n     return *this; n   } n   int inv() const { n     assert(x != 0); n     return qpower(x, mod - 2); n   } n   Z operator/(const Z &b) const { return (Z){(int)(1ll * x * b.inv() % mod)}; } n   Z &operator/=(const Z &b) { n     x = 1ll * x * b.inv() % mod; n     return *this; n   } n   Z operator-() const { return (Z){qmod2(-x)}; } n }; n bool zero(const Z &a) { return !a.x; } n void print(const Z &a) { printf( %d , a.x); } n Z inv[N], fac[N], ifac[N]; n void prework(int n) { n   fac[0] = 1; n   for (int i = 1; i <= n; i++) fac[i] = fac[i - 1] * Z(i); n   inv[1] = 1; n   for (int i = 2; i <= n; i++) inv[i] = Z(mod - mod / i) * inv[mod % i]; n   ifac[0] = 1; n   for (int i = 1; i <= n; i++) ifac[i] = ifac[i - 1] * inv[i]; n } n template <typename T> n void simplify(T &a) { n   while (a.size() && zero(a[-1])) a.pop_back(); n } n template <typename T> n bool zero(const T &a) { n   int n = a.size() - 1; n   while (n >= 0 && zero(a[n])) --n; n   return n < 0; n } n int limn = 0; n void setn(const int &n) { limn = n; } n template <class T> n struct Poly { n   vector<T> a; n   Poly() { a.resize(0); } n   Poly(const T &u, const T &v) { n     a.resize(2); n     a[0] = u, a[1] = v; n   } n   Poly(const int &u, const int &v) { n     a.resize(2); n     a[0] = u, a[1] = v; n   } n   Poly(const int &x) { n     a.resize(1); n     a[0] = T(x); n   } n   Poly(const T &x) { n     a.resize(1); n     a[0] = x; n   } n   int size() const { return a.size(); } n   void resize(const int &x) { a.resize(x); } n   void clear() { vector<T>().swap(a); } n   void pop_back() { a.pop_back(); } n   T operator[](const int &x) const { n     assert(x < (int)a.size()); n     if (x >= 0) return a[x]; n     assert(-x <= (int)a.size()); n     return a[a.size() + x]; n   } n   T &operator[](const int &x) { n     assert(x < (int)a.size()); n     if (x >= 0) return a[x]; n     assert(-x <= (int)a.size()); n     return a[a.size() + x]; n   } n   Poly<T> operator*(const Poly<T> &b) const { n     Poly<T> c; n     if (!a.size() || !b.size()) return c; n     int n = a.size(), m = b.size(); n     c.resize(n + m - 1); n     for (int i = 0; i < n; i++) n       for (int j = 0; j < m; j++) c[i + j] += a[i] * b[j]; n     simplify(c); n     return c; n   } n   Poly<T> operator*(const T &b) const { n     Poly<T> c; n     int n = a.size(); n     c.resize(n); n     for (int i = 0; i < (int)a.size(); i++) c[i] = a[i] * b; n     return c; n   } n   Poly<T> operator+(const Poly &b) const { n     Poly c; n     int n = a.size(), m = b.size(); n     c.resize(max(n, m)); n     for (int i = 0; i < n && i < m; i++) c[i] = a[i] + b[i]; n     if (n < m) n       copy(b.a.begin() + n, b.a.end(), c.a.begin() + n); n     else n       copy(a.begin() + m, a.end(), c.a.begin() + m); n     simplify(c); n     return c; n   } n   Poly<T> operator-() const { n     Poly c; n     int n = a.size(); n     c.resize(n); n     for (int i = 0; i < n; i++) c[i] = -a[i]; n     return c; n   } n   Poly<T> operator-(const Poly<T> &b) const { n     Poly c; n     int n = a.size(), m = b.size(); n     c.resize(max(n, m)); n     for (int i = 0; i < n && i < m; i++) c[i] = a[i] - b[i]; n     if (n < m) n       for (int i = n; i < m; i++) c[i] = -b[i]; n     else n       copy(a.begin() + m, a.end(), c.a.begin() + m); n     simplify(c); n     return c; n   } n   Poly<T> operator/(const Poly<T> &b) const { n     assert(!zero(b)); n     Poly c; n     int n = a.size(), m = b.size(); n     if (n < m) return c; n     Poly<T> tmp = *this; n     c.resize(n - m + 1); n     for (int i = n - 1; i >= m - 1; i--) { n       T t = tmp[i] / b[m - 1]; n       c[i - m + 1] = t; n       for (int j = 0; j < m; j++) tmp[i - j] -= b[m - 1 - j] * t; n     } n     return c; n   } n   Poly<T> operator%(const Poly<T> &b) const { n     assert(!zero(b)); n     Poly<T> c; n     int n = a.size(), m = b.size(); n     c = *this; n     for (int i = n - 1; i >= m - 1; i--) { n       T t = c[i] / b[m - 1]; n       for (int j = 0; j < m; j++) c[i - j] -= b[m - 1 - j] * t; n     } n     simplify(c); n     return c; n   } n   Poly<T> &operator+=(const Poly<T> &b) { n     *this = *this + b; n     return *this; n   } n   Poly<T> &operator-=(const Poly<T> &b) { n     *this = *this - b; n     return *this; n   } n   Poly<T> &operator*=(const Poly<T> &b) { n     *this = *this * b; n     return *this; n   } n   Poly<T> &operator*=(const T &b) { n     *this = *this * b; n     return *this; n   } n   Poly<T> &operator/=(const Poly<T> &b) { n     *this = *this / b; n     return *this; n   } n   Poly<T> &operator%=(const Poly<T> &b) { n     *this = *this % b; n     return *this; n   } n   template <typename S> n   S operator()(const S &b) const { n     S ans; n     Poly<T> a = *this; n     int n = limn ? limn : a.size(); n     a.resize(n); n     for (int i = n - 1; i >= 0; i--) { n       ans = ans * b + S(a[i]); n       if (limn) ans.resize(limn); n     } n     return ans; n   } n }; n void print(const Poly<Z> &a) { n   int n = a.size(); n   if (!n) { n     printf( 0 ); n     return; n   } n   for (int i = n - 1; i >= 1; i--) print(a[i]), printf( x^%d +  , i); n   print(a[0]); n } n template <typename T> n Poly<T> monic(const Poly<T> &a) { n   Poly<T> ans = a; n   simplify(ans); n   if (zero(ans)) return ans; n   T t = ans[-1].inv(); n   for (int i = 0; i < ans.size(); i++) ans[i] = a[i] * t; n   return ans; n } n template <typename T> n Poly<T> gcd(const Poly<T> &a, const Poly<T> &b) { n   if (zero(b)) return monic(a); n   return gcd(b, a % b); n } n template <typename T> n Poly<T> lcm(const Poly<T> &a, const Poly<T> &b) { n   if (zero(a) || zero(b)) return Poly<T>(); n   return a * b / gcd(a, b); n } n template <typename T> n pair<Poly<T>, Poly<T> > exgcd(const Poly<T> &a, const Poly<T> &b, n                               const Poly<T> &c = Poly<T>(1)) { n   if (zero(b)) { n     assert(zero(c % a)); n     return make_pair(c / a, 0); n   } n   pair<Poly<T>, Poly<T> > t = exgcd(b, a % b, c); n   return make_pair(t.second, t.first - a / b * t.second); n } n Poly<Z> Der(const Poly<Z> &a) { n   Poly<Z> ans; n   int n = a.size(); n   if (n <= 1) return ans; n   ans.resize(n - 1); n   for (int i = 0; i < n - 1; i++) ans[i] = a[i + 1] * (Z(i + 1)); n   return ans; n } n Poly<Z> Ln(const Poly<Z> &A) { n   Poly<Z> a = A; n   int n = limn ? limn : a.size(); n   a.resize(n); n   Poly<Z> ans; n   ans.resize(n); n   ans[0] = 0; n   assert(a[0].x == 1); n   for (int i = 1; i < n; i++) { n     ans[i] = Z(i) * a[i]; n     for (int j = 0; j < i; j++) ans[i] -= ans[j] * a[i - j]; n   } n   for (int i = 1; i < n; i++) ans[i] *= inv[i]; n   return ans; n } n Poly<Z> Exp(const Poly<Z> &A) { n   Poly<Z> a = A; n   int n = limn ? limn : a.size(); n   a.resize(n); n   Poly<Z> ans; n   ans.resize(n); n   ans[0] = 1; n   assert(a[0].x == 0); n   for (int i = 1; i < n; i++) { n     for (int j = 0; j < i; j++) ans[i] += ans[j] * a[i - j] * Z(i - j); n     ans[i] *= inv[i]; n   } n   return ans; n } n Poly<Z> Inv(const Poly<Z> &A) { n   Poly<Z> a = A; n   int n = limn ? limn : a.size(); n   a.resize(n); n   Poly<Z> ans; n   ans.resize(n); n   Z iv = a[0].inv(); n   ans[0] = iv; n   for (int i = 1; i < n; i++) { n     for (int j = 0; j < i; j++) ans[i] -= ans[j] * a[i - j]; n     ans[i] *= iv; n   } n   return ans; n } n Poly<Z> Pow(const Poly<Z> &A, const int &K) { n   Poly<Z> a = A; n   int n = limn ? limn : a.size(); n   a.resize(n); n   Poly<Z> tmp = a; n   simplify(tmp); n   if (zero(tmp)) return tmp; n   int pos = 0; n   for (int i = 0; i < tmp.size(); i++) n     if (!zero(tmp[i])) { n       pos = i; n       break; n     } n   copy(tmp.a.begin() + pos, tmp.a.end(), tmp.a.begin()); n   tmp.resize(tmp.size() - pos); n   Z iv = tmp[0].inv(); n   for (int i = 0; i < tmp.size(); i++) tmp[i] * iv; n   Poly<Z> ans = tmp; n   ans.resize(n); n   ans = Ln(ans); n   for (int i = 1; i < n; i++) ans[i] *= K; n   ans = Exp(ans); n   if (pos * K >= n) return Poly<Z>(); n   ans.resize(ans.size() + pos * K); n   copy(ans.a.begin(), ans.a.end(), ans.a.begin() + pos * K); n   ans.resize(n); n   iv = iv.inv(); n   iv = qpower(iv.x, K); n   for (int i = 0; i < n; i++) ans[i] *= iv; n   return ans; n } n Poly<Z> Sqrt(const Poly<Z> &a) { return Pow(a, (mod + 1) >> 1); } n Poly<Z> mul(const Poly<Z> &A, const Poly<Z> &B) { n   Poly<Z> a = A, b = B; n   int n; n   if (limn) n     n = limn, a.resize(n), b.resize(n); n   else n     n = a.size() + b.size() - 1, a.resize(n), b.resize(n); n   Poly<Z> ans; n   ans.resize(n); n   for (int i = 0; i < n; i++) n     for (int j = 0; j + i < n; j++) ans[i + j] += a[i] * b[j]; n   return ans; n } n struct FRAC { n   Poly<Z> u, v; n   FRAC(const Z &x) { n     u = x; n     v = Z(1); n   } n   FRAC(const int &x) { n     u = Z(x); n     v = Z(1); n   } n   FRAC() { u = Z(0), v = Z(1); } n   FRAC(const Poly<Z> &_u) { n     u = _u; n     v = Z(1); n   } n   FRAC(const Poly<Z> &_u, const Poly<Z> &_v) { n     u = _u, v = _v; n     simplify(u), simplify(v); n     if (zero(u)) { n       v = 1; n       return; n     } n     Poly<Z> d = gcd(u, v); n     u /= d, v /= d; n     Poly<Z> c = v[-1]; n     u /= c, v /= c; n   } n   FRAC operator+(const FRAC &a) const { n     return (FRAC){u * a.v + v * a.u, v * a.v}; n   } n   FRAC operator-(const FRAC &a) const { n     return (FRAC){u * a.v - v * a.u, v * a.v}; n   } n   FRAC operator*(const FRAC &a) const { return (FRAC){u * a.u, v * a.v}; } n   FRAC operator/(const FRAC &a) const { return (FRAC){u * a.v, v * a.u}; } n   FRAC operator-() const { return (FRAC){-u, v}; } n   FRAC operator()(const FRAC &a) const { n     FRAC fz, fm; n     for (int i = (int)u.size() - 1; i >= 0; i--) fz = fz * a + (FRAC){u[i]}; n     for (int i = (int)v.size() - 1; i >= 0; i--) fm = fm * a + (FRAC){v[i]}; n     return fz / fm; n   } n   FRAC inv() { return (FRAC){v, u}; } n }; n bool zero(const FRAC &a) { return zero(a.u); } n FRAC &operator+=(FRAC &a, const FRAC &b) { n   a = a + b; n   return a; n } n FRAC &operator-=(FRAC &a, const FRAC &b) { n   a = a - b; n   return a; n } n FRAC &operator*=(FRAC &a, const FRAC &b) { n   a = a * b; n   return a; n } n FRAC &operator/=(FRAC &a, const FRAC &b) { n   a = a / b; n   return a; n } n FRAC Der(const FRAC &a) { n   return (FRAC){Der(a.u) * a.v - a.u * Der(a.v), a.v * a.v}; n } n void print(const FRAC &a) { n   putchar( ( ), print(a.u), printf( ) / ( ); n   print(a.v); n   putchar( ) ); n } n struct Basis { n   vector<Poly<FRAC> > a; n   vector<Poly<FRAC> > c; n   int n; n   Basis(int _n) { n     n = _n; n     a.resize(n); n     for (int i = 0; i < n; i++) a[i].resize(n); n     c.resize(n); n     for (int i = 0; i < n; i++) c[i].resize(n); n   } n   pair<bool, Poly<FRAC> > ins(Poly<FRAC> x) { n     Poly<FRAC> tc; n     tc.resize(n); n     for (int i = 0; i < n; i++) { n       if (zero(x[i])) continue; n       if (zero(a[i])) { n         a[i] = x; n         c[i] = tc; n         c[i][i] = 1; n         return make_pair(1, 0); n       } n       FRAC t = -x[i] / a[i][i]; n       tc[i] = t; n       x += a[i] * t; n       x.resize(n); n     } n     for (int j = 0; j < n; j++) tc[j] = -tc[j]; n     Poly<FRAC> ans; n     ans.resize(n); n     setn(n); n     for (int j = 0; j < n; j++) ans += c[j] * tc[j]; n     setn(0); n     simplify(ans); n     return make_pair(0, ans); n   } n }; n template <typename T> n int mindeg(const Poly<T> &a) { n   int n = a.size() - 1; n   for (int i = 0; i < n; i++) n     if (!zero(a[i])) return i; n   return -1; n } n struct ODE { n   Poly<FRAC> a; n   int size() const { return a.size(); } n   int length() const { n     Poly<Z> t = 1; n     for (int i = 0; i < (int)a.size(); i++) t = lcm(t, a[i].v); n     Poly<FRAC> tmpa = a; n     for (int i = 0; i < (int)tmpa.size(); i++) tmpa[i] *= (FRAC){t}; n     int mxpos = tmpa.size() - mindeg(t), mipos = tmpa.size() - (t.size() - 1); n     for (int i = 0; i < (int)tmpa.size(); i++) n       if (!zero(tmpa[i])) n         mxpos = max(mxpos, i - mindeg(tmpa[i].u)), n         mipos = min(mipos, i - (int)(tmpa[i].u.size() - 1)); n     return mxpos - mipos; n   } n   Poly<Z> geta(const Poly<Z> &a0, const int &n) const { n     int m = a0.size(); n     Poly<Z> t = 1; n     for (int i = 0; i < (int)a.size(); i++) t = lcm(t, a[i].v); n     Poly<FRAC> tmpa = a; n     for (int i = 0; i < (int)tmpa.size(); i++) tmpa[i] *= (FRAC){t}; n     int mxpos = tmpa.size() - mindeg(t), mipos = tmpa.size() - (t.size() - 1); n     for (int i = 0; i < (int)tmpa.size(); i++) n       if (!zero(a[i])) n         mxpos = max(mxpos, i - mindeg(tmpa[i].u)), n         mipos = min(mipos, i - (int)(tmpa[i].u.size() - 1)); n     assert(m >= mxpos - mipos); n     vector<vector<Z> > pw; n     pw.resize(n + 1); n     for (int i = 0; i <= n; i++) { n       pw[i].resize(tmpa.size() + 1); n       pw[i][0] = 1; n       for (int j = 1; j < (int)pw[i].size(); j++) n         pw[i][j] = pw[i][j - 1] * (Z){i - j + 1}; n     } n     Poly<Z> ans = a0; n     ans.resize(n + 1); n     for (int i = m; i <= n; i++) { n       Z u = 0, v = 0; n       for (int j = 0; j < t.size(); j++) { n         if (zero(t[j])) continue; n         int d = mxpos - (tmpa.size() - j); n         if (!d) n           u += t[j] * pw[i][tmpa.size()]; n         else n           v -= pw[i - d][tmpa.size()] * t[j] * ans[i - d]; n       } n       for (int j = 0; j < (int)tmpa.size(); j++) n         for (int k = 0; k < tmpa[j].u.size(); k++) { n           if (zero(tmpa[j].u[k])) continue; n           int d = mxpos - (j - k); n           if (!d) n             u -= tmpa[j].u[k] * pw[i][j]; n           else n             v += ans[i - d] * pw[i - d][j] * tmpa[j].u[k]; n         } n       assert(!zero(u) || zero(v)); n       if (!zero(u)) n         ans[i] = v / u; n       else n         ans[i] = 1; n     } n     return ans; n   } n }; n bool zero(const ODE &a) { return zero(a.a); } n void print(const ODE &a) { n   if (!a.size()) { n     puts( F=0 ); n     return; n   } n   int n = a.size(); n   printf( F(%d) = , n); n   putchar( ( ), print(a.a[0]); n   printf( )F ); n   for (int i = 1; i < n; i++) n     printf(  +  ), printf( ( ), print(a.a[i]), printf( )F(%d) , i); n   puts(  ); n } n ODE operator+(const ODE &A, const ODE &B) { n   if (zero(A)) return B; n   if (zero(B)) return A; n   Poly<FRAC> a = A.a, b = B.a; n   int n = a.size(), m = b.size(); n   ODE ans; n   Poly<FRAC> tmp; n   tmp.resize(n + m); n   tmp[0] = 1, tmp[n] = 1; n   Basis bs(n + m); n   for (;;) { n     pair<bool, Poly<FRAC> > t = bs.ins(tmp); n     if (!t.first) { n       ans.a = t.second; n       break; n     } n     FRAC u = tmp[n - 1]; n     tmp[n - 1] = Der(tmp[n - 1]); n     for (int i = n - 2; i >= 0; i--) tmp[i + 1] += tmp[i], tmp[i] = Der(tmp[i]); n     for (int i = 0; i < n; i++) tmp[i] += u * a[i]; n     u = tmp[n + m - 1]; n     tmp[n + m - 1] = Der(tmp[n + m - 1]); n     for (int i = n + m - 2; i >= n; i--) n       tmp[i + 1] += tmp[i], tmp[i] = Der(tmp[i]); n     for (int i = n; i < n + m; i++) tmp[i] += u * b[i - n]; n   } n   return ans; n } n ODE operator*(const ODE &A, const ODE &B) { n   if (zero(A)) return B; n   if (zero(B)) return A; n   Poly<FRAC> a = A.a, b = B.a; n   int n = a.size(), m = b.size(); n   ODE ans; n   Poly<FRAC> tmp; n   tmp.resize(n * m); n   tmp[0] = 1; n   Basis bs(n * m); n   for (int T = 0;; ++T) { n     pair<bool, Poly<FRAC> > t = bs.ins(tmp); n     if (!t.first) { n       ans.a = t.second; n       break; n     } n     Poly<FRAC> ttmp = tmp; n     tmp.clear(); n     tmp.resize(n * m); n     for (int i = 0; i < n; i++) n       for (int j = 0; j < m; j++) { n         tmp[i * m + j] += Der(ttmp[i * m + j]); n         if (i != n - 1) n           tmp[(i + 1) * m + j] += ttmp[i * m + j]; n         else { n           for (int k = 0; k < n; k++) tmp[k * m + j] += ttmp[i * m + j] * a[k]; n         } n         if (j != m - 1) n           tmp[i * m + j + 1] += ttmp[i * m + j]; n         else { n           for (int k = 0; k < m; k++) tmp[i * m + k] += ttmp[i * m + j] * b[k]; n         } n       } n   } n   return ans; n } n ODE composite(const ODE &A, const FRAC &b) { n   if (zero(A)) return A; n   ODE ans; n   Poly<FRAC> tmp; n   Poly<FRAC> a = A.a; n   int n = a.size(); n   FRAC db = Der(b); n   tmp.resize(n); n   tmp[0] = 1; n   Basis bs(n); n   for (;;) { n     pair<bool, Poly<FRAC> > t = bs.ins(tmp); n     if (!t.first) { n       ans.a = t.second; n       break; n     } n     FRAC u = tmp[n - 1] * db; n     tmp[n - 1] = Der(tmp[n - 1]); n     for (int i = n - 2; i >= 0; i--) n       tmp[i + 1] += tmp[i] * db, tmp[i] = Der(tmp[i]); n     if (!zero(u)) n       for (int i = 0; i < n; i++) tmp[i] += a[i](b) * u; n   } n   return ans; n } n Poly<FRAC> trans_vector(const vector<Poly<FRAC> > &a, const int &n, n                         const int &m) { n   Poly<FRAC> ans; n   ans.resize(n * m); n   for (int i = 0; i < n; i++) n     for (int j = 0; j < m; j++) { n       ans[i * m + j] = a[i][j]; n     } n   return ans; n } n Poly<FRAC> composite(const FRAC &a, const Poly<FRAC> &b) { n   Poly<FRAC> u, v; n   u.resize(a.u.size()), v.resize(a.v.size()); n   for (int i = 0; i < a.u.size(); i++) u[i] = a.u[i]; n   for (int i = 0; i < a.v.size(); i++) v[i] = a.v[i]; n   return exgcd(v % b, b, u % b).first % b; n } n void print(const Poly<FRAC> &a) { n   int n = a.size(); n   putchar( ( ), print(a[0]), putchar( ) ); n   for (int i = 1; i < n; i++) printf(  + ( ), print(a[i]), printf( )u^%d , i); n   puts(  ); n } n ODE composite(const ODE &A, const Poly<FRAC> &B) { n   if (!A.size()) return A; n   Poly<FRAC> b = monic(B); n   assert(b.size() > 1); n   Poly<FRAC> a = A.a; n   ODE ans; n   vector<Poly<FRAC> > tmp; n   assert(b.size() > 1); n   int n = a.size(), m = b.size(); n   Poly<FRAC> du; n   { n     Poly<FRAC> u = b; n     for (int i = 0; i < m; i++) u[i] = Der(u[i]); n     simplify(u); n     Poly<FRAC> v = b; n     for (int i = 0; i < m - 1; i++) v[i] = v[i + 1] * FRAC(Poly<Z>(i + 1)); n     v.pop_back(); n     du = exgcd(v, b, -u).first % b; n   } n   --m; n   tmp.resize(n); n   tmp[0] = Poly<FRAC>(1); n   for (int i = 0; i < n; i++) tmp[i].resize(m); n   Basis bs(n * m); n   int T = 0; n   for (;;) { n     pair<bool, Poly<FRAC> > t = bs.ins(trans_vector(tmp, n, m)); n     if (!t.first) { n       ans.a = t.second; n       break; n     } n     vector<Poly<FRAC> > ttmp = tmp; n     tmp.clear(); n     tmp.resize(n); n     for (int i = 0; i < n; i++) tmp[i].resize(m); n     for (int i = 0; i < n; i++) n       for (int j = 0; j < m; j++) tmp[i][j] += Der(ttmp[i][j]); n     for (int i = 0; i < n - 1; i++) { n       Poly<FRAC> t = du * ttmp[i] % b; n       tmp[i + 1] += t; n     } n     for (int i = 0; i < n; i++) n       tmp[i] += composite(a[i], b) * du * ttmp[n - 1] % b; n     for (int i = 0; i < n; i++) { n       Poly<FRAC> t = ttmp[i]; n       for (int j = 0; j < t.size() - 1; j++) t[j] = FRAC(j + 1) * t[j + 1]; n       t.pop_back(); n       tmp[i] += t * du % b; n     } n     for (int i = 0; i < n; i++) tmp[i].resize(m); n   } n   return ans; n } n Poly<Z> calc0(const int &n, const int &len) { n   setn(len); n   Poly<Z> X(0, 1); n   Poly<Z> t; n   t.resize(3); n   t[0] = 1, t[1] = 6, t[2] = 1; n   t = Sqrt(t); n   Poly<Z> r = (Poly<Z>(1, 1) + t) * Z((mod + 1) >> 1); n   return mul(Pow(r, n), Inv(t)); n } n ODE getODE(const int &n) { n   Poly<Z> X(0, 1); n   ODE ans; n   ODE t; n   t.a = Poly<FRAC>(FRAC(n, X)); n   Poly<FRAC> tf; n   tf.resize(3); n   tf[0] = -X, tf[1] = Poly<Z>(-1, -1), tf[2] = 1; n   ans = ans * composite(t, tf); n   t.a = Poly<FRAC>(FRAC((mod - 1) >> 1, X)); n   FRAC tx; n   tx.u.resize(3); n   tx.u[0] = 1, tx.u[1] = 6, tx.u[2] = 1; n   ans = ans * composite(t, tx); n   return ans; n } n int main() { n   int n, K; n   scanf( %d%d , &n, &K); n   int tK = K; n   K = min(K, n); n   prework(20); n   ODE h = getODE(n + 1); n   Poly<Z> ans = h.geta(calc0(n + 1, h.length()), K); n   for (int i = 1; i <= K; i++) print(ans[i]), putchar(   ); n   for (int i = K + 1; i <= tK; i++) putchar( 0 ), putchar(   ); n   puts(  ); n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1005; n const int mod = 1e9 + 7; n int n, m; n int c[N][N], fac[N], f[N][N][2][2], dp[N], g[N]; n void Init() { n   scanf( %d%d , &n, &m); n   c[0][0] = 1; n   for (int i = 1; i <= n; i++) { n     c[i][0] = 1; n     for (int j = 1; j <= i; j++) { n       c[i][j] = c[i - 1][j] + c[i - 1][j - 1]; n       if (c[i][j] >= mod) c[i][j] -= mod; n     } n   } n   fac[0] = 1; n   for (int i = 1; i <= n; i++) fac[i] = 1LL * fac[i - 1] * i % mod; n } n inline void Add(int &x, int y) { n   x += y; n   if (x >= mod) x -= mod; n } n int main() { n   Init(); n   f[1][0][1][0] = 1; n   for (int i = 1; i <= n; i++) { n     for (int j = 0; j <= i; j++) { n       for (int a = 0; a < 2; a++) { n         for (int b = 0; b < 2; b++) { n           int t = f[i][j][a][b]; n           Add(f[i + 1][j][b][0], t); n           Add(f[i + 1][j + 1][b][1], t); n           if (a == 0) Add(f[i + 1][j + 1][b][0], t); n         } n       } n     } n   } n   for (int i = 0; i <= n; i++) { n     for (int j = 0; j < 2; j++) { n       Add(dp[i], f[n + 1][i][j][0]); n     } n   } n   for (int i = n; i >= 0; i--) { n     g[i] = 1LL * dp[i] * fac[n - i] % mod; n     for (int j = i + 1; j <= n; j++) { n       g[i] -= 1LL * c[j][i] * g[j] % mod; n       if (g[i] < 0) g[i] += mod; n     } n   } n   printf( %d n , g[m]); n   return 0; n } n


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


#include <bits/stdc++.h> n using ll = long long; n const int MV = 1e6; n const int p10[] = {1, 10, 100, 1000, 10000, 100000}; n int K, Q, F[6]; n ll dp[MV]; n ll val(int x) { n   ll f = 0; n   for (int i = 0; x; ++i, x /= 10) { n     int d = x % 10; n     if (d % 3 == 0) f += (ll)F[i] * d / 3; n   } n   return f; n } n struct info { n  public: n   int k; n   ll v; n }; n std::deque<info> q; n int main() { n   scanf( %d , &K); n   for (int i = 0; i < 6; ++i) scanf( %d , F + i); n   --K; n   K *= 3; n   for (int i = 0; i < MV; ++i) dp[i] = val(i); n   for (int i = 0; i < 6; ++i) { n     for (int j = 0; j < 3 * p10[i]; ++j) { n       int mv = 1 + (MV - j - 1) / (3 * p10[i]); n       std::function<ll&(int)> get = [&](int x) -> ll& { n         assert(j + x * 3 * p10[i] < MV); n         return dp[j + x * 3 * p10[i]]; n       }; n       for (int k = 0; k < mv; ++k) { n         for (; !q.empty() && k - q.front().k > K;) q.pop_front(); n         for (; n              !q.empty() && get(k) >= q.back().v + (ll)F[i] * (k - q.back().k);) n           q.pop_back(); n         q.push_back({k, get(k)}); n         get(k) = q.front().v + (ll)F[i] * (k - q.front().k); n       } n       q.clear(); n     } n   } n   scanf( %d , &Q); n   for (int i = 0; i < Q; ++i) { n     int N; n     scanf( %d , &N); n     printf( %lld n , dp[N]); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long c[2000], d[2000]; n long long u[2000], b[2000]; n int main() { n   long long n, m, d; n   long long lpl = 0; n   cin >> n >> m >> d; n   for (int i = 0; i < m; i++) { n     cin >> c[i]; n     lpl += c[i]; n   } n   if (lpl == n) { n     cout <<  YES  << endl; n     for (int i = 0; i < m; i++) { n       for (int j = 0; j < c[i]; j++) { n         cout << i + 1 <<    ; n       } n     } n     cout << endl; n     return 0; n   } n   long long st = 0; n   long long flag = 0; n   for (int i = 0; i < m; i++) { n     long long op = c[i]; n     op = n - (lpl - op) - 1; n     long long to = st + d; n     long long ss = op - c[i] + 1; n     if (to > ss) { n       u[i] = ss; n       st = ss + c[i] - 1; n     } n     if (to <= ss) { n       u[i] = to; n       st = to + c[i] - 1; n     } n     lpl -= c[i]; n   } n   if (st + d > n) { n     cout <<  YES  << endl; n     for (int i = 0; i < m; i++) { n       for (int j = u[i]; j < u[i] + c[i]; j++) { n         b[j] = i + 1; n       } n     } n     for (int i = 1; i <= n; i++) { n       cout << b[i] <<    ; n     } n     cout << endl; n   } else { n     cout <<  NO  << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n const int _ = 1e4 + 5; n const long long INF = 1ll << 60; n inline long long min(long long a, long long b) { return a < b ? a : b; } n int n, c, s[_], t[_]; n long long dp[2][_]; n int main() { n   scanf( %d %d , &n, &c); n   for (int i = 1; i <= n; ++i) scanf( %d , s + i); n   for (int i = 1; i <= n; ++i) scanf( %d , t + i); n   for (int i = 1; i <= n; ++i) { n     memset(dp[i & 1], 0x3f, sizeof dp[i & 1]); n     dp[i & 1][0] = dp[(i - 1) & 1][0] + s[i]; n     for (int j = 1; j <= i; ++j) n       dp[i & 1][j] = min(dp[(i - 1) & 1][j] + 1ll * c * j + s[i], n                          dp[(i - 1) & 1][j - 1] + t[i]); n   } n   long long ans = INF; n   for (int i = 0; i <= n; ++i) ans = min(ans, dp[n & 1][i]); n   printf( %lld n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL); n   srand(time(NULL)); n   ; n   int T = 1; n   cin >> T; n   while (T--) { n     int n, r = 0, c = 0; n     string s; n     cin >> n >> s; n     for (int i = 0; i < int(n); i++) r += s[i] ==  R ; n     if (r > 0 && r < n) { n       int j = 0; n       while (s[(j - 1 + n) % n] ==  R  || s[j] ==  L ) j++; n       s = s.substr(j, n - j) + s.substr(0, j); n       char d =  R ; n       for (int i = 0; i < int(n); i++) { n         int j = i; n         while (j < n && s[j] == d) j++; n         c += (j - i) / 3; n         d = (d ==  R ) ?  L  :  R ; n         i = j - 1; n       } n       cout << c << endl; n     } else n       cout << (n + 2) / 3 << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   cin >> n; n   string first, secound; n   string arr1[100], arr2[100], arr3[100], arr4[100]; n   int counter1 = 0, counter2 = 0, counter3 = 0, counter4 = 0; n   for (int i = 0; i < n; i++) { n     cin >> first >> secound; n     if (secound ==  rat ) n       arr1[counter1++] = first; n     else if ((secound ==  woman ) || (secound ==  child )) { n       arr2[counter2++] = first; n     } else if (secound ==  man ) { n       arr3[counter3++] = first; n     } else { n       arr4[counter4++] = first; n     } n   } n   for (int k = 0; k < counter1; k++) cout << arr1[k] << endl; n   for (int i = 0; i < counter2; i++) { n     cout << arr2[i] << endl; n   } n   for (int j = 0; j < counter3; j++) { n     cout << arr3[j] << endl; n   } n   for (int m = 0; m < counter4; m++) cout << arr4[m] << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 300000; n long long abss(long long a) { n   if (a < 0) { n     a = -a; n   } n   return a; n } n long long n; n long long xx; n long long aa[N + 5] = {0}, su[N + 5]; n long long mm[105] = {0}; n int main() { n   mm[0] = 1; n   scanf( %lld , &n); n   long long ans = 0; n   su[0] = 0; n   for (int i = 1; i <= n; ++i) { n     scanf( %lld , &xx); n     while (xx) { n       if (xx & 1) { n         ++aa[i]; n       } n       xx >>= 1; n     } n     su[i] = su[i - 1] + aa[i]; n   } n   long long l[2] = {1, 0}; n   aa[0] = 0; n   for (int i = 1; i <= n; ++i) { n     int bitt = su[i] & 1; n     ans += l[bitt]; n     long long mx = aa[i]; n     long long rem = 0; n     for (int j = i - 1; j >= 0 && j >= i - 63; --j) { n       if ((su[i] - su[j]) % 2 == 0 && rem < mx) { n         --ans; n       } n       if (mx < aa[j]) { n         rem += mx; n         mx = aa[j]; n       } else { n         rem += aa[j]; n         ; n       } n     } n     ++l[bitt]; n     ; n   } n   printf( %lld , ans); n   ; n   return 0; n } n


#include <bits/stdc++.h> n int u[400000]; n int v[400000]; n int s[400000]; n int n; n int parent[200000]; n int f[200000]; n int t[200001]; n bool b[200000]; n int tt[200000]; n bool func(int x, int y) { return u[x] < u[y]; } n int main() { n   scanf( %d , &n); n   for (int i = 0; i < n - 1; i++) { n     scanf( %d%d , &u[i], &v[i]); n     u[i]--; n     v[i]--; n     u[n + i - 1] = v[i]; n     v[n + i - 1] = u[i]; n     t[i] = -1; n   } n   for (int i = 0; i < 2 * n - 2; i++) { n     s[i] = i; n   } n   t[n - 1] = -1; n   std::sort(s, s + 2 * n - 2, func); n   for (int i = 0; i < 2 * n - 2; i++) { n     if (t[u[s[i]]] == -1) t[u[s[i]]] = i; n   } n   t[n] = 2 * n - 2; n   f[0] = 0; n   b[0] = true; n   int tail = 0; n   for (int i = 0; i < n; i++) { n     for (int j = t[f[i]]; j < t[f[i] + 1]; j++) { n       if (b[v[s[j]]]) { n         continue; n       } n       tail++; n       f[tail] = v[s[j]]; n       b[f[tail]] = true; n       parent[f[tail]] = f[i]; n     } n   } n   long long cnt = 0; n   for (int i = n - 1; i > 0; i--) { n     tt[parent[f[i]]] += tt[f[i]] + 1; n     cnt = cnt + (long long)(tt[f[i]] + 1) * (n - tt[f[i]] - 1); n   } n   for (int i = 0; i < n; i++) b[i] = false; n   int pa, pb; n   pa = 0; n   pb = 1; n   for (int i = 1; i < n; i++) { n     b[f[i]] = !b[parent[f[i]]]; n     if (b[f[i]]) n       pa++; n     else n       pb++; n   } n   cnt = cnt + (long long)pa * pb; n   cnt = cnt / 2; n   printf( %I64d , cnt); n   return 0; n } n