#include <bits/stdc++.h> n using namespace std; n int dd[4][2] = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}}; n vector<vector<int>> mat(1000, vector<int>(1000)); n int n, m, si, sj; n void bfs() { n   mat[si][sj] = 1; n   queue<pair<int, int>> q; n   q.push({si, sj}); n   while (!q.empty()) { n     auto p = q.front(); n     q.pop(); n     for (int i = 0; i < 4; i++) { n       int ti = p.first + dd[i][0]; n       int tj = p.second + dd[i][1]; n       if (ti >= 0 && tj >= 0 && ti < n && tj < m && !mat[ti][tj]) { n         mat[ti][tj] = mat[p.first][p.second] + 1; n         q.push({ti, tj}); n       } n     } n   } n } n int main() { n   ios_base::sync_with_stdio(0); n   cin >> n >> m; n   int ei, ej; n   vector<pair<int, pair<int, int>>> v; n   for (int i = 0; i < n; i++) { n     string s; n     cin >> s; n     for (int j = 0; j < m; j++) { n       if (s[j] ==  E ) { n         si = i, sj = j; n         mat[i][j] = 0; n       } else { n         if (s[j] ==  T ) n           mat[i][j] = -1; n         else { n           mat[i][j] = 0; n           if (s[j] ==  S ) n             ei = i, ej = j; n           else n             v.push_back({s[j] -  0 , {i, j}}); n         } n       } n     } n   } n   bfs(); n   int ans = 0; n   int my = mat[ei][ej]; n   for (auto i : v) { n     int cur = mat[i.second.first][i.second.second]; n     if (cur && cur <= my) { n       ans += i.first; n     } n   } n   cout << ans; n } n


#include <bits/stdc++.h> n using namespace std; n #pragma GCC optimize( Ofast ) n #pragma GCC optimize( fast-math ) n #pragma GCC optimize( unroll-loops ) n #pragma GCC target( sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,tune=native ) n mt19937 rnd(51); n const int N = 1010; n vector<int> g[N]; n vector<bool> used(N); n vector<int> d(N); n void ask(vector<int> v) { n   cout <<  ?  n        <<     << v.size() <<    ; n   for (auto to : v) { n     cout << to <<    ; n   } n   cout << endl; n } n void dfs(int v) { n   used[v] = 1; n   for (auto to : g[v]) { n     if (!used[to]) { n       d[to] = d[v] + 1; n       dfs(to); n     } n   } n } n void solve() { n   vector<int> kek; n   int n, need = 0; n   cin >> n; n   for (int i = 1; i <= n; i++) { n     kek.push_back(i); n   } n   for (int i = 0; i < N; i++) { n     g[i].clear(); n     used[i] = 0; n     d[i] = 0; n   } n   for (int i = 0; i < n - 1; i++) { n     int a, b; n     cin >> a >> b; n     g[a].push_back(b); n     g[b].push_back(a); n   } n   ask(kek); n   cin >> need >> need; n   dfs(1); n   int mx = 0; n   for (int i = 1; i <= n; i++) { n     mx = max(mx, d[i]); n   } n   int l = -1, r = mx + 1, j = 0; n   while (r - l > 1) { n     int mid = (r + l) / 2; n     vector<int> lol; n     for (int i = 1; i <= n; i++) { n       if (d[i] >= mid) { n         lol.push_back(i); n       } n     } n     ask(lol); n     int i, mn; n     cin >> i >> mn; n     if (mn == need) { n       l = mid; n       j = i; n     } else { n       r = mid; n     } n   } n   kek.clear(); n   kek.push_back(1); n   ask(kek); n   int d1; n   cin >> d1 >> d1; n   d1 = (d1 - need) / 2; n   int d2 = d1 + (need - (d[j] - d1)); n   kek.clear(); n   for (int i = 1; i <= n; i++) { n     if (d[i] == d2) { n       kek.push_back(i); n     } n   } n   ask(kek); n   int v, lol; n   cin >> v >> lol; n   bool x = 0; n   deque<int> q; n   q.push_back(v); n   while (q.size() > 0) { n     int u = q.front(); n     q.pop_front(); n     if (u == j) x = 1; n     for (auto to : g[u]) { n       if (d[to] > d[u]) { n         q.push_back(to); n       } n     } n   } n   if (x) { n     int last = v; n     vector<int> kek1; n     for (auto to : kek) { n       if (to != v) { n         kek1.push_back(to); n       } n     } n     if (kek1.size() != 0) { n       ask(kek1); n       cin >> v >> lol; n       if (lol != need) { n         v = last; n       } n     } n   } n   cout <<  !  <<     << v <<     << j << endl; n   return; n } n int32_t main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   int t; n   cin >> t; n   while (t--) { n     solve(); n     string s; n     cin >> s; n     if (s ==  Correct ) n       continue; n     else { n       exit(0); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int mod = 1e9 + 7; n long long binpow(long long a, long long b) { n   long long res = 1; n   while (b > 0) { n     if (b & 1) res = (res % mod) * (a % mod), res %= mod; n     a = (a % mod) * (a % mod); n     a %= mod; n     b >>= 1; n   } n   return res; n } n long long divide(long long a, int b) { n   return ((a % mod) * binpow(b, mod - 2) % mod) % mod; n } n long long nCr(long long n, long long k) { n   long long res = 1; n   if (k > n - k) k = n - k; n   for (int i = 0; i < k; ++i) { n     res = ((res % mod) * ((n - i) % mod)) % mod; n     res = divide(res, i + 1); n   } n   return res; n } n int main() { n   ios ::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   long long n, k; n   cin >> n; n   k = n; n   bool can = (n == 1); n   multiset<long long> st; n   for (long long i = 2; i * i <= n; i++) { n     while (n % i == 0) { n       n /= i; n       st.insert(i); n     } n   } n   if (n > 1) st.insert(n); n   if (n == k) can = 1; n   if (can) { n     return cout <<  1 n0 n , 0; n   } n   if (st.size() == 2) { n     cout <<  2 n ; n   } else { n     cout <<  1 n ; n     int cnt = 0; n     long long mul = 1; n     for (auto i : st) { n       mul *= i; n       cnt++; n       if (cnt == 2) break; n     } n     cout << mul; n   } n } n


#include <bits/stdc++.h> n using namespace std; n void FastInputOutput() { n   ios_base ::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n } n int main() { n   int n, temp; n   map<int, int> idx; n   scanf( %d , &n); n   for (int i = 1; i <= n; ++i) { n     scanf( %d , &temp); n     idx[temp] = i; n   } n   int q; n   long long l = 0, r = 0; n   scanf( %d , &q); n   while (q--) { n     scanf( %d , &temp); n     l += idx[temp], r += n - idx[temp] + 1; n   } n   printf( %lld %lld , l, r); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long n, m, q, t, c; n string s; n long long inv[200007]; n long long p[200007]; n long long f[200007]; n long long nq[200007]; n long long mq[200007]; n long long res[200007]; n long long dp[200007]; n vector<long long> que[200007]; n long long fpow(long long a, long long b) { n   if (b == 0) return 1; n   if (b == 1) return a % 1000000007; n   if (b % 2) n     return (a * fpow(a, b - 1)) % 1000000007; n   else { n     long long q = fpow(a, b / 2); n     return (q * q) % 1000000007; n   } n } n long long dziel(long long a, long long b) { n   return (a * fpow(b % 1000000007, 1000000007 - 2)) % 1000000007; n } n long long choose(long long a, long long b) { n   return dziel(f[a], f[b] * f[a - b]); n } n void solve(long long m) { n   memset(dp, 0, sizeof dp); n   dp[m] = 1; n   long long dwumian = 1; n   for (long long i = m + 1; i <= 1e5; ++i) { n     dwumian = (((dwumian * (i - 1)) % 1000000007) * inv[i - m]) % 1000000007; n     dp[i] = (26 * dp[i - 1] + dwumian * p[i - m]) % 1000000007; n   } n } n int32_t main() { n   ios_base::sync_with_stdio(0); n   cin.tie(NULL); n   f[0] = 1; n   p[0] = 1; n   for (long long i = 1; i < 200007; ++i) { n     f[i] = (f[i - 1] * i) % 1000000007; n     p[i] = (25 * p[i - 1]) % 1000000007; n     inv[i] = fpow(i, 1000000007 - 2); n   } n   cin >> q >> s; n   m = s.size(); n   for (long long i = 1; i <= q; ++i) { n     cin >> t; n     if (t == 1) { n       cin >> s; n       m = s.size(); n     } else { n       cin >> n; n       nq[++c] = n; n       mq[c] = m; n       que[m].push_back(c); n     } n   } n   for (long long i = 1; i <= 1e5; ++i) { n     if (!que[i].empty()) solve(i); n     for (auto it : que[i]) res[it] = dp[nq[it]]; n   } n   for (long long i = 1; i <= c; ++i) { n     cout << res[i] <<   n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const long long LINF = 0x3f3f3f3f3f3f3f3f; n const long long NLINF = 0xf7f7f7f7f7f7f7f7; n const int INF = 0x3f3f3f3f, NINF = 0xf7f7f7f7; n const int MOD1 = 1e9 + 7, MOD2 = 1e9 + 9; n const int N = 1e5 + 10; n vector<int> sz; n int t, n, lst; n int main() { n   for (scanf( %d , &t); t--;) { n     scanf( %d , &n), lst = -1; n     printf( 1 ); n     while (n > 0) { n       ; n       int k = 1; n       while (k * (k - 1) / 2 <= n) k++; n       k--; n       if (lst != -1) sz.emplace_back(lst - k); n       lst = k, n -= k * (k - 1) / 2; n     } n     sz.emplace_back(lst); n     ; n     reverse(sz.begin(), sz.end()); n     for (auto &x : sz) { n       for (int i = 0; i < x; i++) printf( 3 ); n       printf( 7 ); n     } n     sz.clear(); n     printf(  n ); n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int s, n, a[10000] = {0}, m = -1, y, i; n   cin >> s >> n; n   int c = n; n   while (n--) { n     cin >> i >> y; n     a[i] += y; n     m = max(m, i); n   } n   for (int i = 0; i < 10000; i++) { n     if (s > i) { n       s += a[i]; n     } n   } n   s > m ? cout <<  YES  : cout <<  NO ; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long n, m, k, x, y, z; n   cin >> n >> m >> k; n   vector<long long> v(n + 1); n   vector<long long> s(n + 2, 0); n   vector<long long> m1(m + 1); n   vector<long long> m2(m + 1); n   vector<long long> m3(m + 1); n   vector<long long> k1(m + 2); n   for (long long i = 1; i <= n; ++i) { n     cin >> x; n     v[i] = x; n   } n   for (long long i = 1; i <= m; ++i) { n     cin >> x >> y >> z; n     m1[i] = x; n     m2[i] = y; n     m3[i] = z; n   } n   for (long long i = 1; i <= k; ++i) { n     cin >> x >> y; n     k1[x] += 1; n     k1[y + 1] += -1; n   } n   long long sum = 0; n   for (long long i = 1; i <= m; ++i) { n     sum += k1[i]; n     s[m1[i]] += sum * m3[i]; n     s[m2[i] + 1] -= sum * m3[i]; n   } n   sum = 0; n   for (long long i = 1; i <= n; ++i) { n     s[i] += sum; n     sum = s[i]; n   } n   for (long long i = 1; i <= n; ++i) { n     s[i] += v[i]; n     cout << s[i] <<    ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 2e5 + 5; n const int inf = 0x3f3f3f3f; n long long dp[maxn]; n int n, m; n struct node { n   int lb, lu; n   long long w; n }; n vector<node> ve[maxn]; n int lb, lu; n long long w, cost[maxn]; n struct seg { n   long long ma, lazy; n } tr[maxn << 2]; n void build(int o, int l, int r) { n   tr[o].ma = 0; n   tr[o].lazy = 0; n   if (l == r) { n     return; n   } n   int mid = (l + r) >> 1; n   build(o << 1, l, mid); n   build(o << 1 | 1, mid + 1, r); n } n void pushup(int o) { tr[o].ma = max(tr[o << 1].ma, tr[o << 1 | 1].ma); } n void pushdown(int o) { n   if (tr[o].lazy) { n     tr[o << 1].ma += tr[o].lazy; n     tr[o << 1 | 1].ma += tr[o].lazy; n     tr[o << 1].lazy += tr[o].lazy; n     tr[o << 1 | 1].lazy += tr[o].lazy; n     tr[o].lazy = 0; n   } n } n void update(int o, int l, int r, int ql, int qr, long long val) { n   if (ql <= l && qr >= r) { n     tr[o].ma += val; n     tr[o].lazy += val; n     return; n   } n   pushdown(o); n   int mid = (l + r) >> 1; n   if (ql <= mid) update(o << 1, l, mid, ql, qr, val); n   if (qr > mid) update(o << 1 | 1, mid + 1, r, ql, qr, val); n   pushup(o); n } n long long query(int o, int l, int r, int ql, int qr) { n   if (ql <= l && qr >= r) { n     return tr[o].ma; n   } n   pushdown(o); n   int mid = (l + r) >> 1; n   long long maxx = 0; n   if (ql <= mid) maxx = max(maxx, query(o << 1, l, mid, ql, qr)); n   if (qr > mid) maxx = max(maxx, query(o << 1 | 1, mid + 1, r, ql, qr)); n   return maxx; n } n int main() { n   while (cin >> n >> m) { n     for (int i = 1; i <= n; i++) { n       scanf( %lld , &cost[i]); n       ve[i].clear(); n     } n     for (int i = 1; i <= m; i++) { n       scanf( %d%d%lld , &lb, &lu, &w); n       ve[lu].push_back({lb, lu, w}); n     } n     build(1, 0, n); n     long long maxx = 0; n     for (int i = 1; i <= n; i++) { n       dp[i] = dp[i - 1]; n       update(1, 0, n, 0, i - 1, -cost[i]); n       for (int j = 0; j < ve[i].size(); j++) { n         lb = ve[i][j].lb, w = ve[i][j].w; n         update(1, 0, n, 0, lb - 1, w); n       } n       dp[i] = max(dp[i], query(1, 0, n, 0, i - 1)); n       update(1, 0, n, i, i, dp[i]); n       maxx = max(maxx, dp[i]); n     } n     printf( %lld n , maxx); n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   cin >> n; n   unsigned long long ans = 1, ans2 = 1; n   for (int i = 0; i < n; i++) { n     ans = (ans * 27); n     ans2 = (ans2 * 7); n     if (ans2 > 1000000007 || ans % 1000000007 > ans2) { n       ans %= 1000000007; n       ans2 %= 1000000007; n     } n   } n   cout << (1000000007 + ans - ans2) % 1000000007 << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int read_number(int i) { n   cout <<  ?   << i <<   n ; n   fflush(stdout); n   int number; n   cin >> number; n   return number; n } n bool intersects(int l1, int l2, int r1, int r2) { n   if (l1 >= l2) swap(l1, l2); n   if (r1 >= r2) swap(r2, r1); n   return max(l1, r1) <= min(l2, r2); n } n bool contains(int l1, int l2, int r1, int r2) { n   if (l1 >= l2) swap(l1, l2); n   if (r1 >= r2) swap(r1, r2); n   return (l1 <= r1 && l2 >= r2) || (r1 <= l1 && l2 <= r2); n } n bool dif(int l1, int l2, int r1, int r2) { return (l2 - l1) * (r2 - r1) <= 0; } n bool solution(int l1, int l2, int r1, int r2) { n   return (dif(l1, l2, r1, r2) && intersects(l1, l2, r1, r2)) || n          contains(l1, l2, r1, r2); n } n int main() { n   int n; n   cin >> n; n   if ((n / 2) % 2 == 1) { n     cout <<  ! -1 n ; n     return 0; n   } n   int l1 = read_number(1); n   int r1 = read_number(n / 2 + 1); n   int l2 = r1; n   int r2 = l1; n   int min_l1 = 1; n   int max_l1 = n / 2 + 1; n   int min_r1 = n / 2 + 1; n   int max_r2 = n + 1; n   if (l1 == r1) { n     cout <<  !   << l1 <<   n ; n     return 0; n   } n   while (true) { n     int med_l1 = (1 + min_l1 + max_l1) / 2; n     int med_r2 = (1 + min_r1 + max_r2) / 2; n     int temp_l1 = read_number(med_l1); n     int temp_r1 = read_number(med_r2); n     if (temp_l1 == temp_r1) { n       cout <<  !   << med_l1 <<   n ; n       return 0; n     } n     if (solution(l1, temp_l1, r1, temp_r1)) { n       l2 = temp_l1; n       r2 = temp_r1; n       max_l1 = med_l1; n       max_r2 = med_r2; n     } else { n       l1 = temp_l1; n       r1 = temp_r1; n       min_l1 = med_l1; n       min_r1 = med_r2; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, t, k; n vector<int> g[200010]; n int par[200010]; n int nodeId; n int leaf; n vector<int> deep[200010]; n int a[200010]; n void createGraph(int x, int level) { n   if (level > t) return; n   for (int i = 1; i <= a[level]; i++) { n     g[x].push_back(++nodeId); n     par[nodeId] = x; n     deep[level].push_back(nodeId); n   } n   leaf += a[level]; n   if (level != t) leaf--; n   createGraph(nodeId, level + 1); n } n void adjust(int dp) { n   queue<int> Q; n   for (int i : deep[dp]) { n     if (g[i].size() == 0) { n       Q.push(i); n     } n   } n   int size = deep[dp + 1].size(); n   for (int i = 0; i < size - 1; i++) { n     if (Q.empty()) break; n     if (leaf <= k) break; n     int node = deep[dp + 1][i]; n     par[node] = Q.front(); n     Q.pop(); n     --leaf; n   } n } n int main(int argc, char const *argv[]) { n   nodeId = 1; n   scanf( %d %d %d , &n, &t, &k); n   for (int i = 1; i <= t; i++) { n     scanf( %d , a + i); n   } n   createGraph(1, 1); n   if (leaf < k) { n     printf( -1 n ); n     exit(0); n   } n   for (int i = 1; i < t; i++) { n     adjust(i); n   } n   if (leaf != k) { n     printf( -1 n ); n     exit(0); n   } n   printf( %d n , n); n   for (int i = 2; i <= n; i++) { n     printf( %d %d n , par[i], i); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   long long m, k; n   cin >> n >> m >> k; n   vector<long long> arr(n); n   long long max1 = -1; n   long long max2 = -2; n   long long temp; n   for (int i = 0; i < n; i++) { n     cin >> arr[i]; n     if (arr[i] > max2) { n       max2 = arr[i]; n     } n     if (max2 > max1) { n       temp = max2; n       max2 = max1; n       max1 = temp; n     } n   } n   long long cycle = (max1 * k) + max2; n   long long period = k + 1; n   long long ans = (m / period) * cycle; n   ans += (m % period) * max1; n   cout << ans; n } n


#include <bits/stdc++.h> n template <class T> n inline T lowbit(T x) { n   return x & (-x); n } n template <class T> n inline T sqr(T x) { n   return x * x; n } n template <class T> n inline bool scan(T &ret) { n   char c; n   int sgn; n   if (c = getchar(), c == EOF) return 0; n   while (c !=  -  && (c <  0  || c >  9 )) c = getchar(); n   sgn = (c ==  - ) ? -1 : 1; n   ret = (c ==  - ) ? 0 : (c -  0 ); n   while (c = getchar(), c >=  0  && c <=  9 ) ret = ret * 10 + (c -  0 ); n   ret *= sgn; n   return 1; n } n const double pi = 3.14159265358979323846264338327950288L; n using namespace std; n int n, pre; n char a[100005]; n bool vis[100005]; n pair<int, int> q[100005]; n vector<int> ans[100005], num; n long long Pow[100005], fac[100005], rev[100005]; n long long qpow(long long a, long long b) { n   if (b == 0) return 1; n   long long ans = qpow(a, b >> 1); n   ans = ans * ans % 1000000007; n   if (b & 1) ans = ans * a % 1000000007; n   return ans; n } n long long C(int m, int n) { n   if (m < n) return 0; n   return fac[m] * rev[n] % 1000000007 * rev[m - n] % 1000000007; n } n void solve() { n   fac[0] = 1; n   for (int i = 1; i < 100005; i++) fac[i] = fac[i - 1] * i % 1000000007; n   rev[100005 - 1] = qpow(fac[100005 - 1], 1000000007 - 2); n   for (int i = 100005 - 2; i >= 0; i--) n     rev[i] = rev[i + 1] * (i + 1) % 1000000007; n   for (int i = 0; i < 100005; i++) ans[i].clear(); n   num.clear(); n   Pow[0] = 1; n   for (int i = 1; i < 100005; i++) Pow[i] = Pow[i - 1] * 25 % 1000000007; n   for (int i = 1; i <= 100000; i++) n     if (vis[i]) { n       num.push_back(i); n       for (int j = 0; j < 100005; j++) { n         if (j < i) { n           ans[i].push_back(0); n           continue; n         } n         long long tmp = 1LL * ans[i][j - 1] * 26 % 1000000007 + n                         C(j - 1, i - 1) * Pow[j - i] % 1000000007; n         ans[i].push_back(tmp % 1000000007); n       } n     } n   for (int i = 0; i < n; i++) { n     if (q[i].first == 1) { n       pre = q[i].second; n     } else { n       int k = *(lower_bound(num.begin(), num.end(), pre)); n       printf( %d n , ans[k][q[i].second]); n     } n   } n } n int main() { n   memset(vis, 0, sizeof(vis)); n   cin >> n >> a; n   vis[pre = strlen(a)] = 1; n   for (int i = 0; i < n; i++) { n     cin >> q[i].first; n     if (q[i].first == 1) { n       cin >> a; n       int len = strlen(a); n       q[i].second = len; n       vis[len] = 1; n     } else { n       cin >> q[i].second; n     } n   } n   solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() n { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   int t; n   cin>>t; n   while(t--) n   { n     int n; n     cin>>n; n     vector<long long int>a(n); n     for(int i=0;i<n;i++) n       cin>>a[i]; n     sort(a.begin(),a.end()); n     long long int as=0,bs=0,turn=0; n     for(int i=n-1;i>=0;i--) n     { n       if(turn==0) n       { n         if(a[i]%2==0) n         { n           as+=a[i]; n         } n       } n       else  n       { n         if(a[i]&1) n         { n           bs+=a[i]; n         } n       } n       turn=1-turn; n     } n     if(as>bs) n       cout<< Alice <<endl; n     else if(bs>as) n       cout<< Bob <<endl; n     else n       cout<< Tie <<endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 55; n int n, num, h[N], siz[N], e[N << 1], pre[N << 1]; n double g[N], C[N][N], f[N][N]; n inline void add(int x, int y) { e[++num] = y, pre[num] = h[x], h[x] = num; } n void dfs(int u, int fa = 0) { n   memset(f[u], 0, sizeof f[u]), f[u][0] = siz[u] = 1; n   for (int i = h[u]; i; i = pre[i]) n     if (e[i] != fa) { n       dfs(e[i], u), siz[u] += siz[e[i]]; n       memset(g, 0, sizeof g); n       for (int j = 0; j < siz[e[i]]; ++j) g[j] = f[e[i]][j] * (siz[e[i]] - j); n       for (int j = 1; j <= siz[e[i]]; ++j) n         for (int k = 0; k < j; ++k) g[j] += f[e[i]][k] / 2; n       for (int j = siz[u]; j--;) { n         f[u][j] = f[u][j] * g[0] * C[siz[u] - 1 - j][siz[e[i]]]; n         for (int k = 1; k <= siz[e[i]]; ++k) n           f[u][j] += n               f[u][j - k] * g[k] * C[siz[u] - 1 - j][siz[e[i]] - k] * C[j][k]; n       } n     } n } n int main() { n   scanf( %d , &n); n   for (int i = 1, x, y; i < n; ++i) scanf( %d%d , &x, &y), add(x, y), add(y, x); n   C[0][0] = 1; n   for (int i = 1; i <= n; ++i) n     for (int j = 0; j <= i; ++j) n       C[i][j] = (j ? C[i - 1][j - 1] : 0) + C[i - 1][j]; n   double k = 1; n   for (int i = 1; i < n; ++i) k /= i; n   for (int i = 1; i <= n; ++i) dfs(i), printf( %.10lf n , f[i][n - 1] * k); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = (1e6) + 5; n const int M = (60) + 5; n int memo[M][M]; n int n, a[N]; n int grundy(int n, int last) { n   if (n == 0) { n     return 0; n   } n   int &ret = memo[n][last]; n   if (ret != -1) return ret; n   set<int> S; n   for (int i = last + 1; i <= n; i += 1) { n     S.insert(grundy(n - i, i)); n   } n   ret = 0; n   while (S.count(ret)) ret++; n   return ret; n } n int main() { n   scanf( %d , &n); n   for (int i = 0; i < n; ++i) { n     scanf( %d , a + i); n   } n   memset(memo, -1, sizeof memo); n   int g = 0; n   for (int i = 0; i < n; i += 1) { n     g ^= grundy(a[i], 0); n   } n   cout << (g == 0 ?  YES  :  NO ) <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n #pragma GCC optimize( Ofast ) n #pragma GCC target( sse4 ) n using namespace std; n const int MOD = 1e9 + 7; n const int MX = 2e5 + 5; n const long long INF = 1e18; n const long double PI = acos((long double)-1); n const long double EPS = 1e-6; n const int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1}; n const char nl =   n ; n template <typename T> n inline T gcd(T a, T b) { n   return b ? gcd(b, a % b) : a; n } n template <typename T> n inline T lcm(T a, T b) { n   return a / gcd(a, b) * b; n } n template <class T> n void ckmin(T& a, T b) { n   a = min(a, b); n } n template <class T> n void ckmax(T& a, T b) { n   a = max(a, b); n } n int pct(int x) { return __builtin_popcount(x); } n int pct(long long x) { return __builtin_popcountll(x); } n void DBG() { cerr <<  ]  << endl; } n template <class H, class... T> n void DBG(H h, T... t) { n   cerr << h; n   if (sizeof...(t)) cerr <<  ,  ; n   DBG(t...); n } n void setIn(string s) { freopen(s.c_str(),  r , stdin); } n void setOut(string s) { freopen(s.c_str(),  w , stdout); } n void unsyncIO() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n } n void setIO(string s =   ) { n   unsyncIO(); n   if ((int)s.size()) { n     setIn(s +  .in ), setOut(s +  .out ); n   } n } n long long pow(long long b, long long e, long long m = 0LL, n               long long ans = 1LL) { n   while (e) { n     if (e & 1LL) { n       ans *= b; n       if (m) ans %= m; n     } n     b *= b, e >>= 1LL; n     if (m) b %= m; n   } n   return ans; n } n void solve(int tcn) { n   int n, m; n   cin >> n >> m; n   vector<vector<int> > l(n + 1, vector<int>(m + 1)), n       r(n + 1, vector<int>(m + 1)); n   for (int i = (0); i < (n); ++i) { n     int k; n     cin >> k; n     for (int j = (0); j < (k); ++j) { n       int lll, rrr; n       cin >> lll >> rrr; n       for (int x = (lll); x < (rrr + 1); ++x) { n         l[i + 1][x] = lll; n         r[i + 1][x] = rrr; n       } n     } n   } n   vector<vector<long long> > dp(m + 2, vector<long long>(m + 2, 0LL)); n   for (int lll = m; lll >= 1; --lll) { n     for (int rrr = (lll); rrr < (m + 1); ++rrr) { n       for (int x = (lll); x < (rrr + 1); ++x) { n         long long cnt = 0LL; n         for (int k = (1); k < (n + 1); ++k) n           if (l[k][x] >= lll && r[k][x] <= rrr) ++cnt; n         ckmax(dp[lll][rrr], dp[lll][x - 1] + cnt * cnt + dp[x + 1][rrr]); n       } n     } n   } n   cout << dp[1][m] << nl; n } n int main() { n   setIO(); n   int t = 1; n   for (int i = 1; i <= t; ++i) { n     solve(i); n   } n   return 0; n } n


#include<bits/stdc++.h> n using namespace std; n int main(){ n     int t; n     cin>>t; n      n     while(t--){ n     string s; n     cin>>s; n      n     int n = s.length(); n      n     int i=0,j = n-1,k = n-1; n     char z =  a ; n      n     while(k>=0){ n         if(s[i] == z+k) n         i++; n         else if(s[j] == z+k) n         j--; n         else n         break; n          n         k--; n     } n      n     if(k == -1) n     cout<< YES <<endl; n     else n     cout<< NO <<endl; n      n     } n     return 0; n }


#include <bits/stdc++.h> n using namespace std; n const long N = 1000L + 10; n const long long modulo = 1000000007L; n vector<pair<long, long> > interv; n bool memo[N][N]; n bool visited[N]; n void divideByZero(const char *error) { n   printf(error); n   fclose(stdout); n   int a = 234; n   a = a / (a - a); n } n bool canDirectly(long a, long b, long c, long d) { n   return (c < a && a < d) || (c < b && b < d); n } n void addInterval(long x, long y) { n   long n = interv.size(); n   interv.push_back(make_pair(x, y)); n   for (long i = 0; i < n; i++) { n     if (canDirectly(interv[i].first, interv[i].second, x, y)) { n       memo[i][n] = true; n     } n     if (canDirectly(x, y, interv[i].first, interv[i].second)) { n       memo[n][i] = true; n     } n   } n } n bool pathExists(long from, long to) { n   for (long i = 0; i < N; i++) visited[i] = false; n   long n = interv.size(); n   queue<long> q; n   visited[from] = true; n   q.push(from); n   while (!q.empty()) { n     long v = q.front(); n     if (v == to) return true; n     q.pop(); n     for (long nb = 0; nb < n; nb++) n       if (memo[v][nb]) { n         if (visited[nb]) continue; n         visited[nb] = true; n         q.push(nb); n       } n   } n   return false; n } n int main() { n   long n; n   scanf( %ld  , &n); n   for (long i = 0; i < n; i++) n     for (long j = 0; j < n; j++) memo[i][j] = false; n   for (long i = 0; i < n; i++) { n     long mode, x, y; n     scanf( %ld %ld %ld , &mode, &x, &y); n     if (mode == 1) n       addInterval(x, y); n     else { n       if (pathExists(x - 1, y - 1)) n         printf( YES n ); n       else n         printf( NO n ); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n constexpr int Maxn = 2e5 + 7; n constexpr int Maxm = 1e6 + 7; n constexpr int Mod = 1e9 + 7; n int n, k, sum, root; n void add(int bit[], int p, int v) { n   for (; p < Maxn; p += p & -p) bit[p] = (bit[p] + v) % Mod; n } n int query(int bit[], int p) { n   int res = 0; n   for (; p; p -= p & -p) res = (res + bit[p]) % Mod; n   return res; n } n struct Edge { n   int nxt, to, w; n } e[Maxn << 1]; n int head[Maxn], edge_cnt, res; n inline void add_edge(int u, int v, int w) { n   e[++edge_cnt].to = v; n   e[edge_cnt].nxt = head[u]; n   e[edge_cnt].w = w; n   head[u] = edge_cnt; n } n int a[Maxn], siz[Maxn], f[Maxn]; n int del[Maxn]; n void getroot(int u, int fa) { n   siz[u] = 1; n   f[u] = 0; n   for (int i = head[u]; i; i = e[i].nxt) { n     int v = e[i].to; n     if (!del[v] && v != fa) { n       getroot(v, u); n       siz[u] += siz[v]; n       f[u] = std::max(f[u], siz[v]); n     } n   } n   f[u] = std::max(f[u], sum - siz[u]); n   if (f[u] < f[root]) root = u; n } n int val[2][Maxn], num[2][Maxn]; n void DFS(int u, int fa, int rcol, int col, int nxt, int tot, int opt) { n   tot = (tot + a[u]) % Mod; n   if (opt == 0) { n     if (k >= nxt) { n       int cnt = query(num[rcol], k - nxt + 1); n       int cur = query(val[rcol], k - nxt + 1); n       res = (res + 1LL * cnt * tot % Mod) % Mod; n       res = (res + cur) % Mod; n     } n     if (k >= nxt + 1) { n       int cnt = query(num[rcol ^ 1], k - nxt); n       int cur = query(val[rcol ^ 1], k - nxt); n       res = (res + 1LL * cnt * tot % Mod) % Mod; n       res = (res + cur) % Mod; n     } n     if (k >= nxt) res = (res + tot) % Mod; n   } else if (opt == 1) { n     add(num[rcol], nxt + 1, 1); n     add(val[rcol], nxt + 1, tot); n   } else { n     add(num[rcol], nxt + 1, Mod - 1); n     add(val[rcol], nxt + 1, Mod - tot); n   } n   for (int i = head[u]; i; i = e[i].nxt) { n     int v = e[i].to, w = e[i].w; n     if (del[v] || v == fa) continue; n     int nxtstp = nxt; n     if (w != col) ++nxtstp; n     DFS(v, u, rcol, w, nxtstp, tot, opt); n   } n } n void solve(int u) { n   del[u] = 1; n   for (int i = head[u]; i; i = e[i].nxt) { n     int v = e[i].to, w = e[i].w; n     if (!del[v]) { n       DFS(v, u, w, w, 0, a[u], 0); n       DFS(v, u, w, w, 0, 0, 1); n     } n   } n   for (int i = head[u]; i; i = e[i].nxt) { n     int v = e[i].to, w = e[i].w; n     if (!del[v]) DFS(v, u, w, w, 0, 0, -1); n   } n   for (int i = head[u]; i; i = e[i].nxt) { n     int v = e[i].to; n     if (!del[v]) { n       root = 0, sum = siz[v]; n       getroot(v, 0); n       solve(root); n     } n   } n } n int main() { n   scanf( %d%d , &n, &k); n   f[0] = sum = n; n   for (int i = 1; i <= n; ++i) scanf( %d , &a[i]); n   for (int i = 1, u, v, w; i <= n - 1; ++i) { n     scanf( %d%d%d , &u, &v, &w); n     add_edge(u, v, w), add_edge(v, u, w); n   } n   getroot(1, 0); n   solve(root); n   for (int i = 1; i <= n; ++i) res = (res + a[i]) % Mod; n   printf( %d n , res); n } n


#include <bits/stdc++.h> n using namespace std; n void rwFile() { n   freopen( INP.inp ,  r , stdin); n   freopen( OUT.out ,  w , stdout); n } n void solveC_VanyaAndScales() { n   long long w, m; n   cin >> w >> m; n   if (w == 2) { n     cout <<  YES ; n     return; n   } n   int able = 1; n   while (m) { n     long long tmp = m % w; n     m /= w; n     if (tmp == w - 1) { n       m++; n       continue; n     } n     if (tmp == 1 || tmp == 0) continue; n     able = 0; n     break; n   } n   if (able == 1) n     cout <<  YES ; n   else n     cout <<  NO ; n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(nullptr); n   cout.tie(nullptr); n   solveC_VanyaAndScales(); n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, m; n   cin >> n >> m; n   vector<int> a(n); n   vector<int> update(n); n   for (int i = 0; i < n; i++) { n     cin >> a[i]; n     update[i] = 0; n   } n   int accumalated = 0; n   for (int i = 0; i < m; i++) { n     int t; n     cin >> t; n     if (t == 1) { n       int v, x; n       cin >> v >> x; n       a[v - 1] = x; n       update[v - 1] = accumalated; n     } else if (t == 2) { n       int y; n       cin >> y; n       accumalated += y; n     } else { n       int q; n       cin >> q; n       cout << a[q - 1] + accumalated - update[q - 1] << endl; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 5010; n inline int read() { n   int s = 0, w = 1; n   register char ch = getchar(); n   while (ch <  0  || ch >  9 ) { n     if (ch ==  - ) w = -1; n     ch = getchar(); n   } n   while (ch >=  0  && ch <=  9 ) n     s = (s << 3) + (s << 1) + (ch ^ 48), ch = getchar(); n   return s * w; n } n int n, m, K; n int a[N], b[N], c[N], dp[N][N]; n struct Node { n   int x, w; n } g[N]; n inline bool cp(Node x, Node y) { return x.w < y.w; } n signed main() { n   n = read(), m = read(), K = read(); n   for (register int i = 1; i <= n; i++) n     a[i] = read(), b[i] = read(), c[i] = read(), g[i].x = g[i].w = i; n   for (register int i = 1; i <= m; i++) { n     int u, v; n     u = read(), v = read(); n     g[v].w = max(g[v].w, u); n   } n   sort(g + 1, g + 1 + n, cp); n   int now = 1; n   while (!g[now].w) now++; n   memset(dp, -0x3f, sizeof(dp)); n   for (register int i = 0; i <= K; i++) dp[0][i] = 0; n   int qwq = K; n   for (register int i = 1; i <= n; i++) { n     if (qwq < a[i]) { n       puts( -1 ); n       return 0; n     } n     qwq += b[i]; n     for (register int j = a[i]; j <= 5000 - b[i]; j++) n       dp[i][j + b[i]] = max(dp[i][j + b[i]], dp[i - 1][j]); n     while (now <= n && g[now].w == i) { n       for (register int j = 1; j <= 5000; j++) n         dp[i][j - 1] = max(dp[i][j - 1], dp[i][j] + c[g[now].x]); n       now++; n     } n   } n   int res = 0; n   for (register int i = 0; i <= 5000; i++) res = max(res, dp[n][i]); n   printf( %d n , res); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int inf = ~0U >> 1; n const long long linf = (long long)1e18; n const double eps = 1e-6; n template <class T> n string tostr(T a) { n   ostringstream out; n   out << a; n   return out.str(); n } n struct debugger { n   template <typename T> n   debugger &operator,(const T &x) { n     cerr << x << endl; n     return *this; n   } n } dbg; n namespace fastIO { n bool IOerror = 0; n inline char nc() { n   static char buf[100000], *p1 = buf + 100000, *pend = buf + 100000; n   if (p1 == pend) { n     p1 = buf; n     pend = buf + fread(buf, 1, 100000, stdin); n     if (pend == p1) { n       IOerror = 1; n       return -1; n     } n   } n   return *p1++; n } n inline bool blank(char ch) { n   return ch ==     || ch ==   n  || ch ==   r  || ch ==   t ; n } n inline void read(int &x) { n   bool sign = 0; n   char ch = nc(); n   x = 0; n   for (; blank(ch); ch = nc()) n     ; n   if (IOerror) return; n   if (ch ==  - ) sign = 1, ch = nc(); n   for (; ch >=  0  && ch <=  9 ; ch = nc()) x = x * 10 + ch -  0 ; n   if (sign) x = -x; n } n inline void read(long long &x) { n   bool sign = 0; n   char ch = nc(); n   x = 0; n   for (; blank(ch); ch = nc()) n     ; n   if (IOerror) return; n   if (ch ==  - ) sign = 1, ch = nc(); n   for (; ch >=  0  && ch <=  9 ; ch = nc()) x = x * 10 + ch -  0 ; n   if (sign) x = -x; n } n inline void read(bool &x) { n   bool sign = 0; n   char ch = nc(); n   x = 0; n   for (; blank(ch); ch = nc()) n     ; n   if (IOerror) return; n   if (ch ==  - ) sign = 1, ch = nc(); n   for (; ch >=  0  && ch <=  9 ; ch = nc()) x = x * 10 + ch -  0 ; n   if (sign) x = -x; n } n inline void read(double &x) { n   bool sign = 0; n   char ch = nc(); n   x = 0; n   for (; blank(ch); ch = nc()) n     ; n   if (IOerror) return; n   if (ch ==  - ) sign = 1, ch = nc(); n   for (; ch >=  0  && ch <=  9 ; ch = nc()) x = x * 10 + ch -  0 ; n   if (ch ==  . ) { n     double tmp = 1; n     ch = nc(); n     for (; ch >=  0  && ch <=  9 ; ch = nc()) n       tmp /= 10.0, x += tmp * (ch -  0 ); n   } n   if (sign) x = -x; n } n inline void read(char *s) { n   char ch = nc(); n   for (; blank(ch); ch = nc()) n     ; n   if (IOerror) return; n   for (; !blank(ch) && !IOerror; ch = nc()) *s++ = ch; n   *s = 0; n } n inline void read(char &c) { n   for (c = nc(); blank(c); c = nc()) n     ; n   if (IOerror) { n     c = -1; n     return; n   } n } n inline void read1(int &x) { n   char ch; n   int bo = 0; n   x = 0; n   for (ch = getchar(); ch <  0  || ch >  9 ; ch = getchar()) n     if (ch ==  - ) bo = 1; n   for (; ch >=  0  && ch <=  9 ; x = x * 10 + ch -  0 , ch = getchar()) n     ; n   if (bo) x = -x; n } n inline void read1(long long &x) { n   char ch; n   int bo = 0; n   x = 0; n   for (ch = getchar(); ch <  0  || ch >  9 ; ch = getchar()) n     if (ch ==  - ) bo = 1; n   for (; ch >=  0  && ch <=  9 ; x = x * 10 + ch -  0 , ch = getchar()) n     ; n   if (bo) x = -x; n } n inline void read1(double &x) { n   char ch; n   int bo = 0; n   x = 0; n   for (ch = getchar(); ch <  0  || ch >  9 ; ch = getchar()) n     if (ch ==  - ) bo = 1; n   for (; ch >=  0  && ch <=  9 ; x = x * 10 + ch -  0 , ch = getchar()) n     ; n   if (ch ==  . ) { n     double tmp = 1; n     for (ch = getchar(); ch >=  0  && ch <=  9 ; n          tmp /= 10.0, x += tmp * (ch -  0 ), ch = getchar()) n       ; n   } n   if (bo) x = -x; n } n inline void read1(char *s) { n   char ch = getchar(); n   for (; blank(ch); ch = getchar()) n     ; n   for (; !blank(ch); ch = getchar()) *s++ = ch; n   *s = 0; n } n inline void read1(char &c) { n   for (c = getchar(); blank(c); c = getchar()) n     ; n } n inline void read2(int &x) { scanf( %d , &x); } n inline void read2(long long &x) { scanf( %lld , &x); } n inline void read2(double &x) { scanf( %lf , &x); } n inline void read2(char *s) { scanf( %s , s); } n inline void read2(char &c) { scanf(  %c , &c); } n inline void readln2(char *s) { gets(s); } n struct Ostream_fwrite { n   char *buf, *p1, *pend; n   Ostream_fwrite() { n     buf = new char[100000]; n     p1 = buf; n     pend = buf + 100000; n   } n   void out(char ch) { n     if (p1 == pend) { n       fwrite(buf, 1, 100000, stdout); n       p1 = buf; n     } n     *p1++ = ch; n   } n   void print(int x) { n     static char s[15], *s1; n     s1 = s; n     if (!x) *s1++ =  0 ; n     if (x < 0) out( - ), x = -x; n     while (x) *s1++ = x % 10 +  0 , x /= 10; n     while (s1-- != s) out(*s1); n   } n   void println(int x) { n     static char s[15], *s1; n     s1 = s; n     if (!x) *s1++ =  0 ; n     if (x < 0) out( - ), x = -x; n     while (x) *s1++ = x % 10 +  0 , x /= 10; n     while (s1-- != s) out(*s1); n     out(  n ); n   } n   void print(long long x) { n     static char s[25], *s1; n     s1 = s; n     if (!x) *s1++ =  0 ; n     if (x < 0) out( - ), x = -x; n     while (x) *s1++ = x % 10 +  0 , x /= 10; n     while (s1-- != s) out(*s1); n   } n   void println(long long x) { n     static char s[25], *s1; n     s1 = s; n     if (!x) *s1++ =  0 ; n     if (x < 0) out( - ), x = -x; n     while (x) *s1++ = x % 10 +  0 , x /= 10; n     while (s1-- != s) out(*s1); n     out(  n ); n   } n   void print(double x, int y) { n     static long long mul[] = {1, n                               10, n                               100, n                               1000, n                               10000, n                               100000, n                               1000000, n                               10000000, n                               100000000, n                               1000000000, n                               10000000000LL, n                               100000000000LL, n                               1000000000000LL, n                               10000000000000LL, n                               100000000000000LL, n                               1000000000000000LL, n                               10000000000000000LL, n                               100000000000000000LL}; n     if (x < -1e-7) out( - ), x = -x; n     x *= mul[y]; n     long long x1 = (long long)floor(x); n     if (x - floor(x) >= 0.5) ++x1; n     long long x2 = x1 / mul[y]; n     print(x2); n     if (y > 0) { n       out( . ); n       print(x1 - x2 * mul[y]); n     } n   } n   void println(double x, int y) { n     print(x, y); n     out(  n ); n   } n   void print(char *s) { n     while (*s) out(*s++); n   } n   void println(char *s) { n     while (*s) out(*s++); n     out(  n ); n   } n   void flush() { n     if (p1 != buf) { n       fwrite(buf, 1, p1 - buf, stdout); n       p1 = buf; n     } n   } n   ~Ostream_fwrite() { flush(); } n } Ostream; n inline void print(int x) { Ostream.print(x); } n inline void println(int x) { Ostream.println(x); } n inline void print(char x) { Ostream.out(x); } n inline void println(char x) { n   Ostream.out(x); n   Ostream.out(  n ); n } n inline void print(long long x) { Ostream.print(x); } n inline void println(long long x) { Ostream.println(x); } n inline void print(double x, int y) { Ostream.print(x, y); } n inline void println(double x, int y) { Ostream.println(x, y); } n inline void print(char *s) { Ostream.print(s); } n inline void println(char *s) { Ostream.println(s); } n inline void println() { Ostream.out(  n ); } n inline void flush() { Ostream.flush(); } n char Out[100000], *o = Out; n inline void print1(int x) { n   static char buf[15]; n   char *p1 = buf; n   if (!x) *p1++ =  0 ; n   if (x < 0) *o++ =  - , x = -x; n   while (x) *p1++ = x % 10 +  0 , x /= 10; n   while (p1-- != buf) *o++ = *p1; n } n inline void println1(int x) { n   print1(x); n   *o++ =   n ; n } n inline void print1(long long x) { n   static char buf[25]; n   char *p1 = buf; n   if (!x) *p1++ =  0 ; n   if (x < 0) *o++ =  - , x = -x; n   while (x) *p1++ = x % 10 +  0 , x /= 10; n   while (p1-- != buf) *o++ = *p1; n } n inline void println1(long long x) { n   print1(x); n   *o++ =   n ; n } n inline void print1(char c) { *o++ = c; } n inline void println1(char c) { n   *o++ = c; n   *o++ =   n ; n } n inline void print1(char *s) { n   while (*s) *o++ = *s++; n } n inline void println1(char *s) { n   print(s); n   *o++ =   n ; n } n inline void println1() { *o++ =   n ; } n inline void flush1() { n   if (o != Out && *(o - 1) ==   n ) *--o = 0; n   puts(Out); n } n struct puts_write { n   ~puts_write() { flush1(); } n }; n inline void print2(int x) { printf( %d , x); } n inline void println2(int x) { printf( %d n , x); } n inline void print2(char x) { printf( %c , x); } n inline void println2(char x) { printf( %c n , x); } n inline void print2(long long x) { printf( %lld , x); } n inline void println2(long long x) { n   print2(x); n   printf(  n ); n } n inline void println2() { printf(  n ); } n };  // namespace fastIO n using namespace fastIO; n string name =   , in =  .in , out =  .out ; n bool a[2005][2005], visit[2005][2005]; n int cx[700005], cy[700005], c1, n, cnt, cntC, cntS; n double dist[700005], Ox, Oy; n void dfs(int x, int y) { n   if (visit[x][y]) return; n   visit[x][y] = 1; n   ++cnt; n   bool bo = 0; n   Ox += x; n   Oy += y; n   for (int dx = -1; dx <= 1; ++dx) n     for (int dy = -1; dy <= 1; ++dy) { n       int x1 = x + dx, y1 = y + dy; n       if (!a[x1][y1]) n         ++bo; n       else n         dfs(x1, y1); n     } n   if (bo) ++c1, cx[c1] = x, cy[c1] = y; n } n void genMap() { n   static bool tmp[2005][2005]; n   memcpy(tmp, a, sizeof(a)); n   memset(a, 0, sizeof(a)); n   for (int i = 4; i + 3 <= n; ++i) n     for (int j = 4; j + 3 <= n; ++j) { n       int cnt = 0; n       for (int dx = -3; dx <= 3; ++dx) n         for (int dy = -3; dy <= 3; ++dy) cnt += tmp[i + dx][j + dy]; n       if (cnt >= 24) n         a[i][j] = 1; n       else n         a[i][j] = 0; n     } n } n int main() { n   read(n); n   memset(visit, 1, sizeof(visit)); n   for (int i = 1; i <= n; ++i) n     for (int j = 1; j <= n; ++j) read(a[i][j]), visit[i][j] = 0; n   genMap(); n   for (int i = 1; i <= n; ++i) n     for (int j = 1; j <= n; ++j) n       if (!visit[i][j] && a[i][j]) { n         c1 = 0; n         cnt = 0; n         Ox = 0; n         Oy = 0; n         dfs(i, j); n         if (cnt < 130) continue; n         double max_d = 0; n         int c1_pre = c1; n         Ox /= cnt; n         Oy /= cnt; n         for (int k = 1; k <= c1; ++k) n           dist[k] = n               sqrt((Ox - cx[k]) * (Ox - cx[k]) + (Oy - cy[k]) * (Oy - cy[k])); n         for (int k = 1; k <= c1; ++k) max_d = max(max_d, dist[k]); n         for (int k = 1; k <= c1; ++k) dist[k] /= max_d; n         double ave = accumulate(dist + 1, dist + c1 + 1, 0.0) / c1, sum = 0; n         for (int k = 1; k <= c1; ++k) sum += (dist[k] - ave) * (dist[k] - ave); n         sum /= c1; n         sum = sqrt(sum); n         double div = sum / ave; n         if (div > 0.045 && div < 0.055) { n           printf( div=%.5lf cnt=%d n , div, cnt); n           while (1) n             ; n         } n         if (div < 0.060) n           ++cntC; n         else n           ++cntS; n       } n end:; n   printf( %d %d n , cntC, cntS); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAX = 2e5 + 5; n const int MOD = 998244353; n long long p[MAX]; n long long mpow[MAX]; n int main(int argc, char* argv[]) { n   if (argc == 2 or argc == 3) freopen(argv[1],  r , stdin); n   if (argc == 3) freopen(argv[2],  w , stdout); n   int n, m; n   string a, b; n   cin >> n >> m >> a >> b; n   while (n < m) a =  0  + a, n++; n   while (m < n) b =  0  + b, m++; n   mpow[0] = 1; n   for (int i = 1; i < MAX; i++) { n     mpow[i] = (mpow[i - 1] * 2) % MOD; n   } n   for (int i = n - 1; i >= 0; i--) { n     p[i] = p[i + 1] + (a[i] ==  1 ) * mpow[n - i - 1]; n     p[i] %= MOD; n   } n   long long ans = 0; n   for (int i = 0; i < m; i++) { n     if (b[i] ==  1 ) { n       ans = (ans + p[i]) % MOD; n     } n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n bool reverse(long long a, long long b) { return a > b; } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   long long n, i; n   cin >> n; n   if (n % 2 != 0) n     cout <<  -1  n          <<   n ; n   else { n     for (long long i = 1; i <= n; i++) { n       if (i % 2 == 0) n         cout << i - 1 <<    ; n       else n         cout << i + 1 <<    ; n     } n   } 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; n     cin >> n; n     long long int a[n + 1], b[n + 1]; n     for (long long int i = 1; i <= n; i++) cin >> a[i]; n     for (long long int i = 1; i <= n; i++) cin >> b[i]; n     multiset<long long int> s; n     for (long long int i = 1; i <= n; i++) s.insert(b[i]); n     for (long long int i = 1; i <= n; i++) { n       long long int opt1 = *s.begin(); n       if (s.lower_bound(n - a[i]) == s.end()) { n         cout << (a[i] + opt1) % n <<    ; n         s.erase(s.find(opt1)); n         continue; n       } n       long long int opt2 = *s.lower_bound(n - a[i]); n       if ((a[i] + opt1) % n < (a[i] + opt2) % n) { n         cout << (a[i] + opt1) % n <<    ; n         s.erase(s.find(opt1)); n       } else { n         cout << (a[i] + opt2) % n <<    ; n         s.erase(s.find(opt2)); n       } n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long head[200010], cnt; n class littlestar { n  public: n   long long to, nxt; n   void add(long long u, long long v) { n     to = v; n     nxt = head[u]; n     head[u] = cnt; n   } n } star[400010]; n long long n, f[200010], g[200010], h[200010]; n void dfs(long long u, long long fa) { n   for (long long i = head[u]; i; i = star[i].nxt) { n     long long v = star[i].to; n     if (v == fa) continue; n     dfs(v, u); n     f[fa] = f[fa] + f[v] + 1; n     g[u] = g[u] + f[v] + 1; n   } n } n void dp(long long u, long long fa) { n   for (long long i = head[u]; i; i = star[i].nxt) { n     long long v = star[i].to; n     if (v == fa) continue; n     h[v] = g[u] - 1; n     if (fa) h[v] = h[fa]; n     dp(v, u); n   } n } n signed main() { n   cin >> n; n   for (register long long i = 1; i <= n - 1; i++) { n     long long x, y; n     scanf( %lld%lld , &x, &y); n     star[++cnt].add(x, y); n     star[++cnt].add(y, x); n   } n   dfs(1, 0); n   h[1] = f[1]; n   dp(1, 0); n   long long ans = LLONG_MAX; n   for (register long long i = 1; i <= n; i++) ans = min(ans, h[i]); n   cout << ans; n } n


#include <bits/stdc++.h> n using namespace std; n bool comparator(string a, string b) { n   if (a.size() == b.size()) { n     return a > b; n   } n   return a.size() > b.size(); n } n int main() { n   int n, m; n   cin >> n >> m; n   string mat[n]; n   for (int i = 0; i < n; i++) { n     cin >> mat[i]; n   } n   vector<pair<int, int> > enemy; n   vector<int> adj[n * m]; n   int jugador; n   int end; n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < m; j++) { n       int pos = i * m + j; n       if (mat[i][j] ==  T ) continue; n       if (mat[i][j] ==  E ) { n         end = pos; n       } n       if (mat[i][j] -  0  >= 0 && mat[i][j] -  0  <= 9) { n         enemy.emplace_back(pair<int, int>(pos, mat[i][j] -  0 )); n       } n       if (mat[i][j] ==  S ) { n         jugador = pos; n       } n       if (i > 0 && mat[i - 1][j] !=  T ) { n         adj[pos].emplace_back(pos - m); n         adj[pos - m].emplace_back(pos); n       } n       if (i < n - 1 && mat[i + 1][j] !=  T ) { n         adj[pos].emplace_back(pos + m); n         adj[pos + m].emplace_back(pos); n       } n       if (j < m - 1 && mat[i][j + 1] !=  T ) { n         adj[pos].emplace_back(pos + 1); n         adj[pos + 1].emplace_back(pos); n       } n       if (j > 0 && mat[i][j - 1] !=  T ) { n         adj[pos].emplace_back(pos - 1); n         adj[pos - 1].emplace_back(pos); n       } n     } n   } n   queue<int> qu; n   int dist[n * m]; n   for (int i = 0; i < n * m; i++) { n     dist[i] = 1000000000; n   } n   dist[end] = 0; n   qu.push(end); n   while (!qu.empty()) { n     int u = qu.front(); n     qu.pop(); n     for (auto v : adj[u]) { n       if (dist[u] + 1 < dist[v]) { n         dist[v] = dist[u] + 1; n         qu.push(v); n       } n     } n   } n   long long ans = 0; n   for (auto ene : enemy) { n     if (dist[ene.first] <= dist[jugador]) { n       ans += ene.second; n     } n   } n   cout << ans; n } n


#include <bits/stdc++.h> n using ld = long double; n const ld EPS = 1e-12; n const ld PI = acos((ld)-1); n inline bool equ(const ld& a, const ld& b) { return std::abs(a - b) < EPS; } n struct vec { n  public: n   ld x, y; n   bool operator==(const vec& o) const { return equ(x, o.x) && equ(y, o.y); } n   vec& operator+=(const vec& o) { return x += o.x, y += o.y, *this; } n   vec& operator-=(const vec& o) { return x -= o.x, y -= o.y, *this; } n   ld ang() { return atan2(y, x); } n   friend vec operator+(vec a, const vec& b) { return a += b; } n   friend vec operator-(vec a, const vec& b) { return a -= b; } n   ld operator*(const vec& o) const { return x * o.x + y * o.y; } n   ld mag() const { return hypot(x, y); } n }; n const int MN = 1e5 + 10; n int N, S, P, G, s, p, g, ctr, op; n char t; n vec m, a[MN]; n std::multiset<ld> set, val; n std::map<ld, int> map; n int main() { n   scanf( %d%d%d%d , &S, &P, &G, &N); n   m = vec{(ld)P / (S + P + G), (ld)G / (S + P + G)}; n   for (int i = 0, x = 0, y; i < N; ++i) { n     scanf(  %c , &t); n     if (t ==  A ) { n       scanf( %d%d%d , &s, &p, &g); n       ++x; n       a[x] = {(ld)p / (s + p + g), (ld)g / (s + p + g)}; n       a[x] -= m; n       if (a[x].mag() < EPS) n         ++ctr; n       else { n         { n           ld v = a[x].ang(); n           auto it = map.lower_bound(v - EPS); n           if (it == map.end() || !equ(it->first, v)) n             it = map.insert({v, 0}).first; n           if (!it->second++) { n             v += PI; n             if (v > PI) v -= 2 * PI; n             it = map.lower_bound(v - EPS); n             if (it != map.end() && equ(it->first, v) && it->second) ++op; n           } n         } n         { n           ld v = a[x].ang(); n           auto it = set.insert(v); n           auto nx = std::next(it); n           if (it != set.begin()) { n             auto pr = std::prev(it); n             val.insert(*it - *pr); n             if (nx != set.end()) val.erase(val.lower_bound(*nx - *pr - EPS)); n           } n           if (nx != set.end()) val.insert(*nx - *it); n         } n       } n     } else { n       scanf( %d , &y); n       if (a[y].mag() < EPS) n         --ctr; n       else { n         { n           ld v = a[y].ang(); n           auto it = map.lower_bound(v - EPS); n           assert(it != map.end() && equ(it->first, v)); n           if (!--it->second) { n             v += PI; n             if (v > PI) v -= 2 * PI; n             it = map.lower_bound(v - EPS); n             if (it != map.end() && equ(it->first, v) && it->second) --op; n           } n         } n         { n           ld v = a[y].ang(); n           auto it = set.lower_bound(v - EPS); n           auto nx = std::next(it); n           if (it != set.begin()) { n             auto pr = std::prev(it); n             val.erase(val.lower_bound(*it - *pr - EPS)); n             if (nx != set.end()) val.insert(*nx - *pr); n           } n           if (nx != set.end()) val.erase(val.lower_bound(*nx - *it - EPS)); n           set.erase(it); n         } n       } n     } n     if (ctr) n       printf( 1 n ); n     else if (op) n       printf( 2 n ); n     else if (set.size() >= 3 && *val.rbegin() < PI && n              (2 * PI + *set.begin() - *set.rbegin()) < PI) n       printf( 3 n ); n     else n       printf( 0 n ); n   } n   return 0; n } n


#include <bits/stdc++.h> n #pragma GCC optimize( O3 ) n using namespace std; n const int MAX_N = 3e5 + 10, MAX_LOG_N = 20; n const long long MOD1 = 1e9 + 7, BASE1 = 29, MOD2 = 1e9 + 9, BASE2 = 37; n long long sq(long long a, long long p) { return a * a % p; } n long long qpow(long long a, long long b, long long p) { n   return b ? sq(qpow(a, b / 2, p), p) * (b & 1 ? a : 1) % p : 1; n } n long long inv(long long a, long long p) { return qpow(a, p - 2, p); } n int N, M, logN, log_table[MAX_N], pa[MAX_N], jmp[MAX_N][MAX_LOG_N], n     depth[MAX_N], down_len[MAX_N], chain_content[MAX_N * 2], n     *chain_content_alloc = chain_content, *pos[MAX_N]; n char S[MAX_N]; n long long pow_base1[MAX_N], pow_base1i[MAX_N], pow_base2[MAX_N], n     pow_base2i[MAX_N]; n long long pref_hash1_up[MAX_N], pref_hash1_down[MAX_N], pref_hash2_up[MAX_N], n     pref_hash2_down[MAX_N]; n vector<int> edge[MAX_N], child[MAX_N]; n void construct_dfs1(int x) { n   pref_hash1_down[x] = n       (pref_hash1_down[pa[x]] + (S[x] -  a ) * pow_base1[depth[x]]) % MOD1; n   pref_hash1_up[x] = n       (pref_hash1_up[pa[x]] + (S[x] -  a ) * pow_base1i[depth[x]]) % MOD1; n   pref_hash2_down[x] = n       (pref_hash2_down[pa[x]] + (S[x] -  a ) * pow_base2[depth[x]]) % MOD2; n   pref_hash2_up[x] = n       (pref_hash2_up[pa[x]] + (S[x] -  a ) * pow_base2i[depth[x]]) % MOD2; n   for (int i : edge[x]) { n     if (i == pa[x]) continue; n     child[x].push_back(i); n     pa[i] = x; n     depth[i] = depth[x] + 1; n     construct_dfs1(i); n     down_len[x] = max(down_len[x], down_len[i]); n   } n   down_len[x]++; n   sort(child[x].begin(), child[x].end(), n        [](int a, int b) { return down_len[a] > down_len[b]; }); n } n void construct_dfs2(int x) { n   vector<int> light; n   int y = x; n   while (!child[y].empty()) { n     for (int i = 1; i < child[y].size(); i++) light.push_back(child[y][i]); n     y = child[y][0]; n   } n   chain_content_alloc += 2 * down_len[x]; n   int *t = chain_content_alloc; n   for (int i = 1; i <= 2 * down_len[x]; i++) { n     *--t = y; n     if (i <= down_len[x]) { n       pos[y] = t; n     } n     y = pa[y]; n   } n   for (int i : light) { n     construct_dfs2(i); n   } n } n void init() { n   scanf( %d , &N); n   for (int i = 1; i <= N; i++) { n     log_table[i] = log_table[i - 1]; n     if (1 << log_table[i] + 1 < i) log_table[i]++; n   } n   logN = log_table[N]; n   pow_base1[0] = pow_base1i[0] = pow_base2[0] = pow_base2i[0] = 1; n   pow_base1[1] = BASE1; n   pow_base1i[1] = inv(BASE1, MOD1); n   pow_base2[1] = BASE2; n   pow_base2i[1] = inv(BASE2, MOD2); n   for (long long *arr : {pow_base1, pow_base1i}) { n     for (int i = 2; i <= N; i++) arr[i] = arr[1] * arr[i - 1] % MOD1; n   } n   for (long long *arr : {pow_base2, pow_base2i}) { n     for (int i = 2; i <= N; i++) arr[i] = arr[1] * arr[i - 1] % MOD2; n   } n   scanf( %s , S + 1); n   for (int i = 1; i < N; i++) { n     int a, b; n     scanf( %d%d , &a, &b); n     edge[a].push_back(b); n     edge[b].push_back(a); n   } n   construct_dfs1(1); n   for (int i = 1; i <= N; i++) jmp[i][0] = pa[i]; n   for (int i = 1; i <= logN; i++) { n     for (int j = 1; j <= N; j++) { n       jmp[j][i] = jmp[jmp[j][i - 1]][i - 1]; n     } n   } n   construct_dfs2(1); n } n int kth_ancestor(int x, int k) { n   if (k) { n     int t = log_table[k]; n     x = jmp[x][t]; n     k -= 1 << t; n   } n   return pos[x][-k]; n } n int LCA(int a, int b) { n   if (depth[a] > depth[b]) swap(a, b); n   b = kth_ancestor(b, depth[b] - depth[a]); n   if (a == b) return a; n   for (int i = logN; i >= 0; i--) { n     if (jmp[a][i] != jmp[b][i]) { n       a = jmp[a][i]; n       b = jmp[b][i]; n     } n   } n   return pa[a]; n } n long long hash1_up(int a, int b) { n   return (pref_hash1_up[a] - pref_hash1_up[pa[b]] + MOD1) * n          pow_base1[depth[a]] % MOD1; n } n long long hash1_down(int a, int b) { n   return (pref_hash1_down[a] - pref_hash1_down[pa[b]] + MOD1) * n          pow_base1i[depth[b]] % MOD1; n } n long long hash1(int a, int b, int c, int len) { n   if (len <= depth[a] - depth[b] + 1) { n     return hash1_up(a, kth_ancestor(a, len - 1)); n   } else { n     int dis = depth[a] + depth[c] - 2 * depth[b] + 1; n     long long res = n         hash1_up(a, b) + hash1_down(kth_ancestor(c, dis - len), n                                     kth_ancestor(c, depth[c] - depth[b] - 1)) * n                              pow_base1[depth[a] - depth[b] + 1]; n     return res % MOD1; n   } n } n long long hash2_up(int a, int b) { n   return (pref_hash2_up[a] - pref_hash2_up[pa[b]] + MOD2) * n          pow_base2[depth[a]] % MOD2; n } n long long hash2_down(int a, int b) { n   return (pref_hash2_down[a] - pref_hash2_down[pa[b]] + MOD2) * n          pow_base2i[depth[b]] % MOD2; n } n long long hash2(int a, int b, int c, int len) { n   if (len <= depth[a] - depth[b] + 1) { n     return hash2_up(a, kth_ancestor(a, len - 1)); n   } else { n     int dis = depth[a] + depth[c] - 2 * depth[b] + 1; n     long long res = n         hash2_up(a, b) + hash2_down(kth_ancestor(c, dis - len), n                                     kth_ancestor(c, depth[c] - depth[b] - 1)) * n                              pow_base2[depth[a] - depth[b] + 1]; n     return res % MOD2; n   } n } n void solve() { n   int s1, t1, s2, t2; n   scanf( %d%d%d%d , &s1, &t1, &s2, &t2); n   int c1 = LCA(s1, t1), c2 = LCA(s2, t2); n   int l = 0, r = min(depth[s1] + depth[t1] - 2 * depth[c1] + 1, n                      depth[s2] + depth[t2] - 2 * depth[c2] + 1); n   while (l != r) { n     int m = (l + r + 1) / 2; n     if (hash1(s1, c1, t1, m) == hash1(s2, c2, t2, m) && n         hash2(s1, c1, t1, m) == hash2(s2, c2, t2, m)) { n       l = m; n     } else { n       r = m - 1; n     } n   } n   printf( %d n , l); n } n int main() { n   init(); n   scanf( %d , &M); n   for (int i = 1; i <= M; i++) { n     solve(); n   } n } n


#include <bits/stdc++.h> n using namespace std; n long long a, b; n long long nexta, nextb; n long long d, m; n int main() { n   cin >> a >> b; n   nexta = a; n   nextb = b; n   long long cur = 0; n   while (nexta != nextb) n     if (nexta < nextb) { n       d += nexta - cur; n       cur = nexta; n       nexta += a; n     } else { n       m += nextb - cur; n       cur = nextb; n       nextb += b; n     } n   if (a > b) n     d += nexta - cur; n   else n     m += nextb - cur; n   if (d > m) n     cout <<  Dasha n ; n   else if (d < m) n     cout <<  Masha n ; n   else n     cout <<  Equal n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T> n void _R(T &x) { n   cin >> x; n } n void _R(int &x) { scanf( %d , &x); } n void _R(long long &x) { scanf( %  PRId64, &x); } n void _R(double &x) { scanf( %lf , &x); } n void _R(char &x) { scanf(  %c , &x); } n void _R(char *x) { scanf( %s , x); } n void R() {} n template <typename T, typename... X> n void R(T &head, X &...tail) { n   _R(head); n   R(tail...); n } n const int N = 7005; n int stat[2][N], n, t[2], arr[2][N], cnt[2][N]; n char str[3][10] = { Loop ,  Win ,  Lose }; n void dfs(int who, int pos, int st) { n   if (stat[who][pos]) return; n   stat[who][pos] = st; n   ; n   if (st == 1) n     for (int i = 0; i < t[1 - who]; i++) { n       int pre = (pos - arr[1 - who][i] + n) % n; n       if (--cnt[1 - who][pre] == 0) dfs(1 - who, pre, 2); n     } n   else n     for (int i = 0; i < t[1 - who]; i++) { n       int pre = (pos - arr[1 - who][i] + n) % n; n       dfs(1 - who, pre, 1); n     } n } n void print() { n   for (int i = 0; i < 2; i++) n     for (int j = 1; j < n; j++) n       printf( %s%c , str[stat[i][j]],    n [j == n - 1]); n } n int main() { n   R(n); n   for (int i = 0; i < 2; i++) { n     R(t[i]); n     for (int j = 0; j < t[i]; j++) R(arr[i][j]); n   } n   for (int i = 0; i < 2; i++) n     for (int j = 0; j < n; j++) cnt[i][j] = t[i]; n   stat[0][0] = stat[1][0] = 2; n   for (int i = 0; i < 2; i++) n     for (int j = 0; j < t[i]; j++) dfs(i, n - arr[i][j], 1); n   print(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int kk[7]; n int main() { n   string s; n   cin >> s; n   int n = s.length(); n   sort(s.begin(), s.end()); n   for (int i = 0, j; i < n; i = j) { n     j = i + 1; n     while (j < n && s[j] == s[i]) j++; n     kk[j - i]++; n   } n   int ans; n   if (kk[6] == 1 || kk[5] == 1 && kk[1] == 1) n     ans = 1; n   else if (kk[4] == 1 || kk[3] == 2) n     ans = 2; n   else if (kk[3] == 1 && kk[2] == 1 && kk[1] == 1) n     ans = 3; n   else if (kk[3] == 1 && kk[1] == 3) n     ans = 5; n   else if (kk[2] == 3) n     ans = 6; n   else if (kk[2] == 2 && kk[1] == 2) n     ans = 8; n   else if (kk[2] == 1 && kk[1] == 4) n     ans = 15; n   else n     ans = 30; n   cout << ans <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int t; n   cin >> t; n   while (t--) { n     long long int a, b, x, y, n; n     cin >> a >> b >> x >> y >> n; n     cout << min(max(x, a - n) * max(y, b - n + min(n, a - x)), n                 max(y, b - n) * max(x, a - n + min(n, b - y))) n          << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long n; n   cin >> n; n   string s; n   cin >> s; n   if (n % 4 != 0) { n     cout <<  === ; n     return 0; n   } n   vector<long long> v(5, 0); n   for (long long i = 0; i < n; i++) { n     if (s[i] ==  A ) { n       v[0]++; n     } n     if (s[i] ==  C ) { n       v[1]++; n     } n     if (s[i] ==  G ) { n       v[2]++; n     } n     if (s[i] ==  T ) { n       v[3]++; n     } n     if (s[i] ==  ? ) { n       v[4]++; n     } n   } n   long long max1 = max(v[0], max(v[1], v[2])); n   long long maxov = max(max1, v[3]); n   long long reqd = 4 * maxov - v[0] - v[1] - v[2] - v[3]; n   bool check = false; n   if (v[4] >= reqd && (v[4] - reqd) % 4 == 0) { n     check = true; n   } n   if (check == false) { n     cout <<  === ; n   } else { n     long long reqda = maxov - v[0] + ((v[4] - reqd) / 4); n     long long reqdc = maxov - v[1] + ((v[4] - reqd) / 4); n     long long reqdg = maxov - v[2] + ((v[4] - reqd) / 4); n     long long reqdt = maxov - v[3] + ((v[4] - reqd) / 4); n     for (long long i = 0; i < n; i++) { n       if (s[i] !=  ? ) { n         cout << s[i]; n       } else { n         if (reqda > 0) { n           cout <<  A ; n           reqda--; n           continue; n         } n         if (reqdc > 0) { n           cout <<  C ; n           reqdc--; n           continue; n         } n         if (reqdg > 0) { n           cout <<  G ; n           reqdg--; n           continue; n         } n         if (reqdt > 0) { n           cout <<  T ; n           reqdt--; n         } n       } n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int64_t v[200005] = {0}; n int64_t fnc(int64_t h, int n) { n   int64_t mn = v[0]; n   for (int i = 1; i < n; i++) { n     v[i] += v[i - 1]; n     mn = min(mn, v[i]); n   } n   if (v[n - 1] >= 0 && h + mn > 0) return -1; n   int64_t ans = 0; n   if (h + mn <= 0) { n     for (int i = 0; i < n; i++) n       if (h + v[i] <= 0) return i + 1; n   } n   int64_t t = ceil(((-mn - h) * 1.0) / v[n - 1]); n   if (t < 0) t = 0; n   ans += n * t; n   h = h + t * v[n - 1]; n   for (int i = 0; i < n; i++) { n     if (h + v[i] <= 0) { n       ans += i + 1; n       break; n     } n   } n   return ans; n } n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   int m; n   int64_t h; n   cin >> h >> m; n   for (int i = 0; i < m; i++) cin >> v[i]; n   cout << fnc(h, m) << 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(NULL); n   cout.tie(NULL); n   ; n   long long n, k, ans = 1e11; n   string s; n   cin >> n; n   if (n == 1 || n == 2) { n     cout <<  No  << endl; n     return 0; n   } n   cout <<  Yes  << endl; n   set<long long> s1, s2; n   for (long long i = 1; i < n + 1; i++) { n     i % 2 ? s1.insert(i) : s2.insert(i); n   } n   cout << s2.size() <<    ; n   for (auto i : s2) cout << i <<    ; n   cout << endl; n   cout << s1.size() <<    ; n   for (auto i : s1) cout << i <<    ; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e3 + 5; n int n; n double p[N][N], E[N], g[N], f[N]; n bool vis[N]; n int main() { n   scanf( %d , &n); n   for (int i = 1; i <= n; i++) n     for (int j = 1; j <= n; j++) scanf( %lf , &p[i][j]), p[i][j] /= 100.0; n   for (int i = 1; i <= n; i++) g[i] = 1 - p[i][n]; n   vis[n] = 1; n   for (int i = 1; i <= n; i++) { n     double mn = 1e18; n     int id = 0; n     for (int j = 1; j <= n; j++) n       if (!vis[j] && (f[j] + 1) / (1 - g[j]) < mn) { n         mn = (f[j] + 1) / (1 - g[j]); n         id = j; n       } n     vis[id] = 1; n     E[id] = (f[id] + 1) / (1 - g[id]); n     for (int j = 1; j <= n; j++) n       if (!vis[j]) n         f[j] += g[j] * p[j][id] * E[id], g[j] = g[j] * (1 - p[j][id]); n   } n   printf( %.6lf n , E[1]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<int64_t> v[3001]; n int64_t calc(int64_t taken, int64_t n, int64_t m) { n   int64_t req = taken - v[0].size(); n   int64_t t = v[0].size(); n   int64_t ans = 0; n   vector<int64_t> rem; n   for (int64_t i = 1; i < m; i++) { n     if (v[i].size() == 0) continue; n     for (int64_t j = 0; j < v[i].size(); j++) { n       if (j + taken <= v[i].size()) { n         ans += v[i][j]; n         req--; n       } else n         rem.push_back(v[i][j]); n     } n   } n   if (req > 0) { n     sort(rem.begin(), rem.end()); n     int64_t i = 0; n     while (req > 0 and i < rem.size()) { n       ans += rem[i]; n       i++; n       req--; n     } n   } n   return ans; n } n void solve() { n   int64_t n, m, i, ans; n   cin >> n >> m; n   for (int64_t i = 0; i < n; i++) { n     int64_t p, c; n     cin >> p >> c; n     p--; n     v[p].push_back(c); n   } n   for (int64_t i = 0; i < m; i++) { n     sort(v[i].begin(), v[i].end()); n   } n   ans = LLONG_MAX; n   for (int64_t i = 1; i <= n; i++) { n     int64_t curr_cost = calc(i, n, m); n     ans = min(ans, curr_cost); n   } n   cout << ans <<   n ; n } n signed main() { n   std::ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   solve(); n   cerr <<   Execution :   << (1.0 * clock()) / CLOCKS_PER_SEC <<  s  n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int INF = 1e9; n long long n, m, b, ans; n long long a[1005]; n int l, r, mid; n long long dp[1005][1005]; n struct Node { n   long long w, id; n } f[1005]; n bool cmp(Node a, Node b) { n   if (a.w != b.w) return a.w < b.w; n   return a.id > b.id; n } n int main() { n   cin >> n >> m; n   for (int i = 1; i <= n; ++i) cin >> a[i]; n   for (int i = 1; i <= n + 1; ++i) n     for (int j = 0; j <= n + 1; ++j) dp[i][j] = 1e18; n   dp[n + 1][0] = 0; n   for (int i = n; i >= 1; --i) { n     for (int j = 0; j <= n; ++j) n       dp[i][j] = min(dp[i][j], max(0LL, dp[i + 1][j] - a[i])); n     for (int j = 1; j <= n; ++j) dp[i][j] = min(dp[i][j], dp[i + 1][j - 1]); n   } n   for (int i = 0; i <= n; ++i) { n     f[i].w = dp[1][i]; n     f[i].id = i; n   } n   sort(f, f + n + 1, cmp); n   for (int i = 1; i <= m; ++i) { n     cin >> b; n     l = 0; n     r = n; n     ans = -1; n     while (l <= r) { n       mid = (l + r) / 2; n       if (f[mid].w <= b) { n         ans = f[mid].id; n         l = mid + 1; n       } else n         r = mid - 1; n     } n     cout << ans << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n int main() { n   double y1, y2, yw, xb, yb, r, A, B, C; n   scanf( %lf%lf%lf%lf%lf%lf , &y1, &y2, &yw, &xb, &yb, &r); n   yw -= r; n   y1 += r; n   yb = 2 * yw - yb; n   A = y1 - yb; n   B = xb; n   C = -B * y1 + B * y2; n   if ((C / sqrt(A * A + B * B)) <= r) { n     printf( -1 ); n     return 0; n   } n   C = -B * y1; n   printf( %.10lf , (-C - B * yw) / A); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m, a[200010], t[200010], pos[200010], ans[200010]; n struct point { n   int pos, v, f; n   point(int pos, int v, int f) : pos(pos), v(v), f(f) {} n }; n vector<point> v[200010]; n inline void insert(int x) { n   for (register int i = x; i <= n; i += i & -i) t[i]++; n } n inline int find(int x) { n   register int ans = 0; n   for (register int i = x; i; i -= i & -i) ans += t[i]; n   return ans; n } n inline void add(int x, int y, int f, int i) { v[x].push_back(point(i, y, f)); } n inline int read() { n   register int x = 0, ch = getchar(); n   while (!isdigit(ch)) ch = getchar(); n   while (isdigit(ch)) x = x * 10 + ch -  0 , ch = getchar(); n   return x; n } n int main() { n   n = read(), m = read(); n   for (register int i = 1; i <= n; i++) pos[a[i] = read()] = i; n   for (register int i = 1; i <= m; i++) { n     int l = read(), r = read(); n     add(r, r, 1, i), add(l - 1, r, -1, i), add(r, l - 1, -1, i), n         add(l - 1, l - 1, 1, i); n   } n   for (register int i = 1; i <= n; i++) { n     for (register int j = a[i]; j <= n; j += a[i]) insert(pos[j]); n     for (register int j = 0; j < v[i].size(); j++) n       ans[v[i][j].pos] += find(v[i][j].v) * v[i][j].f; n   } n   for (register int i = 1; i <= m; i++) printf( %d n , ans[i]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, k; n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   cin >> n >> k; n   vector<pair<int, int> > v(n), ans; n   for (int i = 0; i < n; ++i) cin >> v[i].first, v[i].second = i; n   while (k--) { n     sort(v.begin(), v.end()); n     if (v.back().first - v[0].first > 1) n       --v.back().first, ++v[0].first, n           ans.push_back({v.back().second, v[0].second}); n   } n   sort(v.begin(), v.end()); n   cout << v.back().first - v[0].first <<     << ans.size() <<   n ; n   for (auto it : ans) cout << it.first + 1 <<     << it.second + 1 <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n #pragma GCC optimize( O3 ) n #pragma GCC target( sse4 ) n using namespace std; n template <class c> n struct rge { n   c b, e; n }; n template <class c> n rge<c> range(c i, c j) { n   return rge<c>{i, j}; n } n template <class c> n auto dud(c* x) -> decltype(cerr << *x, 0); n template <class c> n char dud(...); n struct debug { n   template <class c> n   debug& operator<<(const c&) { n     return *this; n   } n }; n int32_t main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   long long n, x; n   cin >> n >> x; n   x--; n   vector<long long> a(n); n   for (long long i = 0; i < n; i++) cin >> a[i]; n   long long mn = a[x]; n   for (long long i = 0; i < n; i++) { n     if (a[i] < mn) { n       mn = a[i]; n     } n   } n   long long ans = mn * n; n   while (a[x] != mn) { n     a[x]--; n     ans++; n     x--; n     if (x < 0) x = n - 1; n   } n   for (long long i = 0; i < n; i++) cout << (i == x ? ans : a[i] - mn) <<    ; n   cout <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int M = 2e5 + 10; n int n, k, m, kk, x, b[M]; n int main() { n   int T; n   cin >> T; n   while (T--) { n     cin >> n >> k; n     kk = 1; n     m = 0; n     for (int i = 1; i <= n; i++) { n       cin >> x; n       if (x % 2) { n         m++; n         if (kk < k) { n           b[kk] = i; n           kk++; n         } n       } n     } n     if ((m - k + 1) % 2 == 1 && kk == k) { n       cout <<  YES  << endl; n       for (int i = 1; i < kk; i++) cout << b[i] <<    ; n       cout << n << endl; n     } else n       cout <<  NO  << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long dx[] = {1, 0, -1, 0}; n long long dy[] = {0, 1, 0, -1}; n bool myComparison1(const pair<long long, long long> &a, n                    const pair<long long, long long> &b) { n   return a.second < b.second; n } n bool myComparison2(const pair<long long, long long> &a, n                    const pair<long long, long long> &b) { n   if (a.first == b.first) { n     return (a.second < b.second); n   } n   return (a.first < b.first); n } n long long gcd(long long x, long long y) { n   if (y == 0) n     return x; n   else n     return gcd(y, x % y); n } n long long expo(long long n, long long m, long long p) { n   long long r = 1; n   n = n % p; n   while (m > 0) { n     if (m % 2) r = (r * n) % p; n     n = (n * n) % p; n     m = m / 2; n   } n   return r % p; n } n bool isPrime(long long n) { n   if (n <= 1) return false; n   if (n <= 3) return true; n   if (n % 2 == 0 || n % 3 == 0) return false; n   for (long long i = 5; i * i <= n; i = i + 6) n     if (n % i == 0 || n % (i + 2) == 0) return false; n   return true; n } n vector<long long> z_function(string s) { n   long long n = (long long)s.length(); n   vector<long long> z(n); n   for (long long i = 1, l = 0, r = 0; i < n; ++i) { n     if (i <= r) z[i] = min(r - i + 1, z[i - l]); n     while (i + z[i] < n && s[z[i]] == s[i + z[i]]) ++z[i]; n     if (i + z[i] - 1 > r) l = i, r = i + z[i] - 1; n   } n   return z; n } n long long distance(long long x, long long y) { return (sqrt(x * 2 + y * 2)); } n int32_t main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   ; n   long long n; n   cin >> n; n   vector<long long> x, y; n   for (long long i = 0; i < n; i++) { n     long long c, d; n     cin >> c >> d; n     x.push_back(c); n     y.push_back(d); n   } n   long long max = -1; n   long long X = 0, Y = 0; n   for (long long i = 0; i < n; i++) { n     if (x[i] + y[i] > max) { n       X = x[i]; n       Y = y[i]; n       max = X + Y; n     } n   } n   cout << max; 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   while (T--) { n     int a[3]; n     for (int i = 0; i < int(3); i++) cin >> a[i]; n     sort(a, a + 3); n     cout << a[2] - a[0]; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, a[5]; n int c[100]; n int main() { n   ios_base::sync_with_stdio(false); n   cin >> n >> a[0] >> a[1]; n   for (int i = 1; i <= n; i++) cin >> c[i]; n   int m = min(a[0], a[1]); n   int ans = 0; n   for (int i = 1; i <= n / 2 + n % 2; i++) { n     if (c[i] == 2 && c[n - i + 1] == 2) { n       if (i != n / 2 + n % 2) n         ans += 2 * m; n       else if (n % 2 == 0) n         ans += 2 * m; n       else n         ans += m; n     } else if (c[i] == 2) n       ans += a[c[n - i + 1]]; n     else if (c[n - i + 1] == 2) n       ans += a[c[i]]; n     else if (c[i] != c[n - i + 1]) { n       cout << -1; n       return 0; n     } n   } n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 510; n bitset<510> can[maxn][2][64]; n int N, M; n long long ans, best[maxn][2]; n inline long long dfs(int now, int last) { n   if (best[now][last ^ 1] != -1) n     return best[now][last ^ 1]; n   else n     best[now][last ^ 1] = 0; n   for (int i = 59; i >= 0; --i) n     if (can[now][last ^ 1][i].any()) { n       for (int j = 1; j <= N; ++j) n         if (can[now][last ^ 1][i][j]) n           best[now][last ^ 1] = n               max(dfs(j, last ^ 1) + (1LL << i), best[now][last ^ 1]); n       break; n     } n   return best[now][last ^ 1]; n } n int main() { n   scanf( %d %d , &N, &M); n   while (M--) { n     int a, b, c; n     scanf( %d %d %d , &a, &b, &c); n     can[a][c][0][b] = 1; n   } n   for (int i = 1; i <= 62; ++i) n     for (int j = 1; j <= N; ++j) n       for (int k = 0; k < 2; ++k) n         for (int l = 1; l <= N; ++l) { n           if (!can[j][k][i - 1][l]) continue; n           can[j][k][i] |= can[l][k ^ 1][i - 1]; n         } n   for (int i = 1; i <= N; ++i) n     if (can[i][0][60].any()) puts( -1 ), exit(0); n   memset(best, -1, sizeof(best)); n   for (int i = 1; i <= N; ++i) ans = max(ans, dfs(i, 1)); n   if (ans > 1000000000000000000LL) ans = -1; n   printf( %I64d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 1005; n int vis[maxn], p[maxn]; n int plen; n void prime() { n   memset(vis, 0, sizeof(vis)); n   plen = 0; n   for (int i = 2, k = 4; i < maxn; ++i, k += i + i - 1) { n     if (!vis[i]) { n       p[plen++] = i; n       if (k < maxn) { n         for (int j = k; j < maxn; j += i) { n           vis[j] = 1; n         } n       } n     } n   } n } n struct node { n   int id; n   int cnt; n   int color; n } need[maxn]; n bool cmp(const node &a, const node &b) { return a.cnt > b.cnt; } n bool cmp1(const node &a, const node &b) { return a.id < b.id; } n char buf[maxn], out[maxn]; n int cnt[maxn]; n int main(int argc, char *argv[]) { n   prime(); n   while (scanf( %s , buf) != EOF) { n     memset(vis, -1, sizeof(vis)); n     memset(cnt, 0, sizeof(cnt)); n     int len = strlen(buf); n     for (int i = 0; i < len; i++) { n       cnt[buf[i] -  a ]++; n     } n     int colornum = 0; n     for (int i = 0; i < plen && p[i] <= len; i++) { n       int color = -1; n       for (int j = 1; j <= len / p[i]; j++) { n         if (vis[j * p[i]] != -1) { n           color = vis[j]; n           break; n         } n       } n       if (color == -1) { n         color = colornum++; n       } n       for (int j = 1; j <= len / p[i]; j++) { n         vis[j * p[i]] = color; n       } n     } n     for (int i = 0; i < maxn; i++) { n       need[i].id = i; n       need[i].cnt = 0; n     } n     for (int i = 2; i <= len; i++) { n       need[vis[i]].cnt++; n     } n     sort(need, need + colornum, cmp); n     int flag = 0; n     for (int i = 0; i < colornum; i++) { n       int maxcolor = 0, k = -1; n       for (int j = 0; j < 26; j++) { n         if (cnt[j] > maxcolor) { n           maxcolor = cnt[j]; n           k = j; n         } n       } n       if (k == -1 || maxcolor < need[i].cnt) { n         flag = 1; n         break; n       } else { n         need[i].color = k; n         cnt[k] -= need[i].cnt; n       } n     } n     if (!flag) { n       printf( YES n ); n       for (int i = 0; i < 26; i++) { n         if (cnt[i]) { n           out[1] = i +  a ; n           break; n         } n       } n       sort(need, need + colornum, cmp1); n       for (int i = 2; i <= len; i++) { n         out[i] = need[vis[i]].color +  a ; n       } n       out[len + 1] = 0; n       printf( %s n , out + 1); n     } else { n       printf( NO n ); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int readInt() { n   bool minus = false; n   int result = 0; n   char ch; n   ch = getchar(); n   while (true) { n     if (ch ==  - ) break; n     if (ch >=  0  && ch <=  9 ) break; n     ch = getchar(); n   } n   if (ch ==  - ) n     minus = true; n   else n     result = ch -  0 ; n   while (true) { n     ch = getchar(); n     if (ch <  0  || ch >  9 ) break; n     result = result * 10 + (ch -  0 ); n   } n   if (minus) n     return -result; n   else n     return result; n } n template <class T> n T mulmod(T a, T b) { n   if (b == 0) return 0; n   if (b == 1) return a; n   T x = mulmod(a, b >> 1); n   x = (x + x) % 1000000007; n   if (b & 1) x = (x + a) % 1000000007; n   return x; n } n template <class T> n T larger(T a, T b) { n   return (a > b ? a : b); n } n template <class T> n T smaller(T a, T b) { n   return (a < b ? a : b); n } n template <class T> n T gcd(T a, T b) { n   if (b == 0) return a; n   return gcd(b, a % b); n } n template <class T> n T lcm(T a, T b) { n   return (a * b) / gcd(a, b); n } n template <class T> n string converter(T n) { n   stringstream x; n   x << n; n   return x.str(); n } n template <class T> n T pw(T a, T b) { n   if (b == 0) return 1; n   T x = pw(a, b >> 1); n   x = (x * x) % 1000000007; n   if (b & 1) x = (x * a) % 1000000007; n   return x; n } n template <class T1> n void deb(T1 e1) { n   cout << e1 << endl; n } n template <class T1, class T2> n void deb(T1 e1, T2 e2) { n   cout << e1 <<     << e2 << endl; n } n template <class T1, class T2, class T3> n void deb(T1 e1, T2 e2, T3 e3) { n   cout << e1 <<     << e2 <<     << e3 << endl; n } n template <class T1, class T2, class T3, class T4> n void deb(T1 e1, T2 e2, T3 e3, T4 e4) { n   cout << e1 <<     << e2 <<     << e3 <<     << e4 << endl; n } n template <class T1, class T2, class T3, class T4, class T5> n void deb(T1 e1, T2 e2, T3 e3, T4 e4, T5 e5) { n   cout << e1 <<     << e2 <<     << e3 <<     << e4 <<     << e5 << endl; n } n template <class T1, class T2, class T3, class T4, class T5, class T6> n void deb(T1 e1, T2 e2, T3 e3, T4 e4, T5 e5, T6 e6) { n   cout << e1 <<     << e2 <<     << e3 <<     << e4 <<     << e5 <<     << e6 n        << endl; n } n char totalDiff(char a, char b) { n   for (char c =  a ; c <=  z ; c++) n     if (c != a && c != b) return c; n } n void letsgo() { n   int n, t; n   cin >> n >> t; n   string a, b, c; n   cin >> a >> b; n   int i, same = 0; n   for (i = 0; i < n; i++) { n     same += (a[i] == b[i]); n   } n   c =   ; n   if (same >= n - t) { n     int j = n - t; n     for (i = 0; i < n; i++) { n       if (a[i] == b[i] && j) n         c.push_back(a[i]), j--; n       else n         c.push_back(totalDiff(a[i], b[i])); n     } n     cout << c; n     return; n   } n   int j = n - t - same; n   int l = j; n   int k = same; n   if (2 * j > n - same) { n     cout << -1; n     return; n   } n   for (i = 0; i < n; i++) { n     if (a[i] == b[i] && k) { n       c.push_back(a[i]); n       k--; n     } else if (j) { n       c.push_back(a[i]); n       j--; n     } else if (l) { n       c.push_back(b[i]); n       l--; n     } else { n       c.push_back(totalDiff(a[i], b[i])); n     } n   } n   cout << c; n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   ; n   letsgo(); n   int t; n   cin >> t; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   { n     long long int i, j, k, y, n, x, m, a, b, ans = 0, sum = 0, cnt = 0; n     long long int c, mx = 0; n     string s, z, taj =  YES ; n     cin >> s; n     map<char, long long int> mp; n     n = s.size(); n     for (i = 0; i < n; i++) mp[s[i]]++; n     if (!mp[ 4 ] and !mp[ 7 ]) n       cout << -1; n     else if (mp[ 4 ] >= mp[ 7 ]) n       cout << 4; n     else n       cout << 7; n     printf(  n ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int v[11][100010]; n int vmap[11][100010]; n int pointer[11]; n int main() { n   int n, m; n   scanf( %d %d , &n, &m); n   long long ans = 0; n   for (int j = 0; j < m; j++) { n     for (int i = 0; i < n; i++) { n       scanf( %d , &v[j][i]); n       v[j][i]--; n       vmap[j][v[j][i]] = i; n     } n   } n   bool isNew = true; n   long long sz; n   for (int i = 0; i < n; i++) { n     if (isNew) { n       for (int j = 1; j < m; j++) pointer[j] = vmap[j][v[0][i]]; n       sz = 1; n       ans += 1; n       isNew = false; n     } else { n       bool check = true; n       sz++; n       for (int j = 1; j < m && check; j++) { n         pointer[j]++; n         if (pointer[j] >= n || v[j][pointer[j]] != v[0][i]) check = false; n       } n       if (check) n         ans += sz; n       else { n         isNew = true; n         i--; n       } n     } n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 200010; n int n, s; n int a[N]; n int vis[N]; n int main() { n   ios::sync_with_stdio(false); n   cin.tie(NULL); n   cin >> n >> s; n   for (int i = 0; i < n; ++i) cin >> a[i]; n   int cnt = 0; n   if (a[s - 1]) { n     cnt++; n     a[s - 1] = 0; n   } n   sort(a, a + n); n   memset(vis, false, sizeof false); n   vis[0] = true; n   for (int i = 1; i < n; ++i) vis[a[i]] = true; n   int cmax = 0; n   for (int i = 1; i < n; ++i) { n     if (!a[i]) { n       while (cmax < n && vis[cmax]) cmax++; n       if (cmax < n) { n         vis[cmax] = true; n       } n       cnt++; n     } else { n       int pos = n - 1; n       for (int j = i; j <= pos; ++j) { n         if (!vis[a[j] - 1]) { n           while (pos > j && cmax < a[j] - 1) { n             while (cmax < a[j] - 1 && vis[cmax]) cmax++; n             vis[cmax] = true; n             pos--; n             cnt++; n             if (cmax == a[j] - 1) break; n           } n           if (!vis[a[j] - 1]) { n             while (cmax < a[j] - 1 && vis[cmax]) cmax++; n             vis[cmax] = true; n             cnt++; n           } n         } n       } n       break; n     } n   } n   cout << cnt << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   cin >> n; n   int arr[n], i; n   for (i = 0; i < n; i++) cin >> arr[i]; n   int ind = 0, price; n   int max = arr[0]; n   for (i = 1; i < n; i++) { n     if (arr[i] > max) { n       max = arr[i]; n       ind = i; n     } n   } n   sort(arr, arr + n); n   price = arr[n - 2]; n   cout << ind + 1 <<     << price; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T, typename U> n static inline void amin(T &x, U y) { n   if (y < x) x = y; n } n template <typename T, typename U> n static inline void amax(T &x, U y) { n   if (x < y) x = y; n } n template <typename T> n static inline void sd(vector<T> &x) { n   sort((x).begin(), (x).end(), greater<T>()); n } n long long n, m; n const long long N = 3e5 + 5; n vector<long long> g[N]; n vector<long long> dep(N), parent(N), vis(N); n void dfs(long long v, long long par = -1) { n   vis[v] = 1; n   for (long long ch : g[v]) { n     if (ch != par and !vis[ch]) { n       dep[ch] = dep[v] + 1; n       parent[ch] = v; n       dfs(ch, v); n     } n   } n } n void solve() { n   cin >> n >> m; n   for (long long i = 0; i < n + 1; ++i) { n     g[i].clear(); n     vis[i] = 0; n   } n   auto path = [&](long long a, long long b) { n     bool flag = false; n     if (dep[a] < dep[b]) { n       swap(a, b); n       flag = true; n     } n     vector<long long> v[2]; n     while (dep[a] > dep[b]) { n       v[0].push_back(a); n       a = parent[a]; n     } n     while (a != b) { n       v[0].push_back(a); n       v[1].push_back(b); n       a = parent[a]; n       b = parent[b]; n     } n     v[0].push_back(a); n     while (!v[1].empty()) { n       v[0].push_back(v[1].back()); n       v[1].pop_back(); n     } n     if (flag) { n       reverse(v[0].begin(), v[0].end()); n     } n     return v[0]; n   }; n   vector<long long> cnt(n + 1); n   for (long long i = 0; i < m; ++i) { n     long long e, f; n     cin >> e >> f; n     g[e].push_back(f); n     g[f].push_back(e); n   } n   dfs(1); n   vector<vector<long long> > output; n   long long q; n   cin >> q; n   for (long long j = 0; j < q; ++j) { n     long long a, b; n     cin >> a >> b; n     cnt[a]++; n     cnt[b]++; n     output.push_back(path(a, b)); n   } n   long long k = 0; n   for (long long i = 1; i < n + 1; ++i) { n     if ((cnt[i] & 1) == 1) { n       k++; n     } n   } n   if (k == 0) { n     cout <<  YES n ; n     for (auto &i : output) { n       cout << i.size() <<   n ; n       for (long long j : i) { n         cout << j <<    ; n       } n       cout <<   n ; n     } n   } else { n     cout <<  NO n ; n     cout << k / 2 <<   n ; n   } n } n signed main() { n   ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); n   long long testcases = 1; n   while (testcases--) { n     solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long inf = 0x3f3f3f3f3f3f3f3f; n template <typename T> n inline void read(T& X) { n   X = 0; n   int w = 0; n   char ch = 0; n   while (!isdigit(ch)) { n     w |= ch ==  - ; n     ch = getchar(); n   } n   while (isdigit(ch)) X = (X << 3) + (X << 1) + (ch ^ 48), ch = getchar(); n   if (w) X = -X; n } n long long a[20], p[20]; n int main() { n   int t; n   for (int i = 0; i <= 9; i++) p[i] = pow(10, i); n   scanf( %d , &t); n   while (t--) { n     long long k, n; n     long long ans = 0; n     scanf( %lld %lld , &n, &k); n     for (int i = 1; i <= n; i++) { n       scanf( %lld , &a[i]); n       a[i] = p[a[i]]; n     } n     for (int i = 1; i <= n; i++) { n       if (i == n) { n         ans = ans + (k + 1) * a[i]; n         break; n       } n       long long now = (a[i + 1] - a[i]) / a[i]; n       if (now <= k) { n         ans = ans + a[i + 1] - a[i]; n         k = k - now; n       } else { n         ans = ans + a[i] * (k + 1); n         break; n       } n     } n     printf( %lld n , ans); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 5010; n long long n, a[N], dp[2][N], mini[2][N]; n vector<long long> v; n int main() { n   cin >> n; n   for (int i = 0; i < n; i++) { n     long long x; n     cin >> x; n     v.push_back(x); n     a[i] = x; n   } n   sort(v.begin(), v.end()); n   v.resize(unique(v.begin(), v.end()) - v.begin()); n   for (int i = 1; i <= n; i++) { n     for (int j = 1; j <= v.size(); j++) { n       int r = i % 2; n       dp[r][j] = mini[1 - r][j] + abs(a[i - 1] - v[j - 1]); n       if (j == 1) { n         mini[r][j] = dp[r][j]; n       } else n         mini[r][j] = min(dp[r][j], mini[r][j - 1]); n     } n   } n   cout << mini[n % 2][v.size()]; n   cin.get(); n } n


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


#include <bits/stdc++.h> n using namespace std; n int a[210000]; n long long dp[210000][2]; n const long long INF = 1LL << 60; n int N; n void init() { n   for (int i = 0; i < 210000; i++) n     for (int j = 0; j < 2; j++) dp[i][j] = INF; n } n long long solve(int x, bool t) { n   if (x <= 0 || x > N) return 0; n   if (x == 1) return -1; n   if (dp[x][t] == -INF) return dp[x][t] = -1; n   if (dp[x][t] != INF) return dp[x][t]; n   dp[x][t] = -INF; n   int i2 = x; n   if (t) n     i2 += a[x]; n   else n     i2 -= a[x]; n   long long r = solve(i2, !t); n   long long res; n   if (r == -1) n     res = -1; n   else n     res = a[x] + r; n   return dp[x][t] = res; n } n int main() { n   cin >> N; n   for (int i = 0; i < N - 1; i++) cin >> a[i + 2]; n   init(); n   for (int i = 1; i < N; i++) { n     long long r = solve(1 + i, 0); n     if (r != -1) r += i; n     cout << r << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int a, b, c, x, y, z; n   scanf( %d%d%d%d%d%d , &a, &b, &c, &x, &y, &z); n   bool can = true; n   int more = 0; n   int less = 0; n   if (a > x) n     more += (a - x) / 2; n   else n     less += x - a; n   if (b > y) n     more += (b - y) / 2; n   else n     less += y - b; n   if (c > z) n     more += (c - z) / 2; n   else n     less += z - c; n   if (more >= less) n     printf( Yes n ); n   else n     printf( No n ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long N, T; n   cin >> N; n   list<long long> L; n   for (int i = 0; i < N; i++) { n     cin >> T; n     L.push_back(T); n   } n   long long K, possible = 1, curr = -1, orig = 0; n   cin >> K; n   vector<pair<long long, long long> > operations; n   long long sum = *L.begin(); n   list<long long>::iterator it = L.begin(); n   for (int i = 0; i < K && possible; i++) { n     cin >> T; n     ++curr; n     list<long long> L2; n     if (it == L.end()) n       possible = 0; n     else { n       sum = *it; n       if (T != sum) { n         if (T < sum) n           possible = 0; n         else { n           sum = 0; n           long long maz = *it; n           while (it != L.end() && T > sum) { n             sum += *it; n             L2.push_back(*it); n             maz = max(maz, *it); n             it++; n           } n           if (T != sum) n             possible = 0; n           else { n             list<long long>::iterator it2 = L2.begin(); n             while (it2 != L2.end() && *it2 != maz) ++it2, ++curr; n             if (it2 == L2.begin()) { n               if (it2 != L2.end()) ++it2; n               ++curr; n               while (it2 != L2.end() && *it2 == maz) ++it2, ++curr; n               if (it2 == L2.end()) n                 possible = 0; n               else { n                 list<long long>::iterator it3 = it2; n                 --it2; n                 --curr; n                 do { n                   operations.push_back(make_pair(curr, 1)); n                   ++it3; n                 } while (it3 != L2.end()); n                 while (it2 != L2.begin()) { n                   --it2; n                   operations.push_back(make_pair(curr, 0)); n                   --curr; n                 } n               } n             } else if (it2 == L2.end()) n               possible = 0; n             else { n               list<long long>::iterator it3 = it2; n               do { n                 --it3; n                 operations.push_back(make_pair(curr, 0)); n                 --curr; n               } while (it3 != L2.begin()); n               ++it2; n               while (it2 != L2.end()) { n                 operations.push_back(make_pair(curr, 1)); n                 ++it2; n               } n             } n           } n         } n       } else n         ++it; n     } n   } n   if (it != L.end() || operations.size() != N - K) possible = 0; n   if (possible) { n     cout <<  YES  << endl; n     for (int i = 0; i < operations.size(); i++) { n       cout << operations[i].first + 1 <<     n            << (operations[i].second == 0 ?  L  :  R ) << endl; n     } n   } else n     cout <<  NO  << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e5 + 4; n vector<int> e[N]; n int n, siz[N]; n long long ans; n int sn = 0, sx = 0; n int en[N], ex[N]; n void dfs(int u, int fa) { n   siz[u] = 1; n   for (auto v : e[u]) { n     if (v == fa) continue; n     dfs(v, u); n     siz[u] += siz[v]; n     en[u] += en[v]; n     ans += 1ll * ex[u] * en[v] * siz[v]; n   } n   ans += 1ll * ex[u] * (sn - en[u]) * (n - siz[u]); n } n int main() { n   scanf( %d , &n); n   for (int i = 1; i < n; i++) { n     int u, v; n     scanf( %d %d , &u, &v); n     e[u].push_back(v); n     e[v].push_back(u); n   } n   for (int i = 1; i <= n; i++) n     scanf( %d %d , &en[i], &ex[i]), sn += en[i], sx += ex[i]; n   dfs(1, 0); n   printf( %.10f n , (double)(ans / (1.0 * sx * sn))); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <class T> n inline T bigmod(T p, T e, T M) { n   long long ret = 1; n   for (; e > 0; e >>= 1) { n     if (e & 1) ret = (ret * p) % M; n     p = (p * p) % M; n   } n   return (T)ret; n } n template <class T> n inline T gcd(T a, T b) { n   if (b == 0) return a; n   return gcd(b, a % b); n } n template <class T> n inline T modinverse(T a, T M) { n   return bigmod(a, M - 2, M); n } n string S, P; n int dp[2001][2002], dp1[2001][2011], len[2001]; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   long long a, b, c, d = 0, e, f = 0, g, h = 0, x, y, z = 0; n   cin >> S >> P; n   a = S.size(), b = P.size(); n   for (int i = (0); i < (b); ++i) dp1[a][i] = 1000000; n   for (int i = a - 1; i >= 0; i--) { n     for (int j = 0; j < b; j++) { n       if (S[i] == P[j]) { n         dp1[i][j] = dp1[i + 1][j + 1]; n       } else n         dp1[i][j] = 1 + dp1[i + 1][j]; n     } n     len[i] = dp1[i][0]; n   } n   for (int i = 1; i <= a; i++) dp[a][i] = -100000; n   for (int i = a - 1; i >= 0; i--) { n     for (int j = 0; j <= a; j++) { n       dp[i][j] = -100000; n       if (j > (a - i)) { n         continue; n       } n       dp[i][j] = max(dp[i][j], dp[i + 1][j]); n       if (j > 0) dp[i][j] = max(dp[i][j], dp[i + 1][j - 1]); n       if (len[i] <= j && (i + len[i] + b) <= a) n         dp[i][j] = max(dp[i][j], 1 + dp[i + len[i] + b][j - len[i]]); n     } n   } n   for (int i = 0; i <= a; i++) cout << dp[0][i] <<    ; n   cout << endl; n } n


#include<bits/stdc++.h> n using namespace std; n typedef long long ll; n ll n,m,pos,qwq,delta,a[1009],b[1009],p[1009][1009]; n inline ll read(){ n  ll s = 0,w = 1; n  char ch = getchar(); n  while (ch >  9  || ch <  0 ){ if (ch ==  - ) w = -1; ch = getchar();} n  while (ch <=  9  && ch >=  0 ) s = (s << 1) + (s << 3) + (ch ^ 48),ch = getchar(); n  return s * w; n } n int main(){ n  m = read(),n = read(); n  for (ll i = 1;i <= n;i += 1) for (ll j = 1;j <= m;j += 1) p[i][j] = read(),a[i] += p[i][j],b[i] += p[i][j] * p[i][j]; n  for (ll i = 2;i < n;i += 1) if (a[i] - a[i - 1] != (a[n] - a[1]) / (n - 1)){pos = i,delta = a[i] - a[i - 1] - (a[n] - a[1]) / (n - 1); break;} n  if (pos <= 3) qwq = b[n - 2] + b[n] - 2 * b[n - 1]; n  else qwq = b[1] + b[3] - 2 * b[2]; n  for (ll i = 1;i <= m;i += 1) if (b[pos - 1] + b[pos + 1] - 2 * (b[pos] - p[pos][i] * p[pos][i] + (p[pos][i] - delta) * (p[pos][i] - delta)) == qwq){printf( %lld %lld ,pos - 1,p[pos][i] - delta); return 0;} n  return 0; n }


#include <bits/stdc++.h> n using namespace std; n void read(long long &x) { n   char ch = getchar(); n   x = 0; n   while (!isdigit(ch)) ch = getchar(); n   while (isdigit(ch)) x = x * 10 + ch - 48, ch = getchar(); n } n const long long N = 2e5 + 10; n long long n, mx, rt, tmp, sum, f[N], fr[N], tot, st[N], fl[N], p[N]; n long long cnt, h[N], to[N << 1], nxt[N << 1], len[N << 1], d[N], vis[N]; n void add(long long u, long long v, long long l) { n   to[++cnt] = v, len[cnt] = l, nxt[cnt] = h[u], h[u] = cnt; n   to[++cnt] = u, len[cnt] = l, nxt[cnt] = h[v], h[v] = cnt; n } n void dfs(long long u, long long la) { n   if (d[u] > d[mx]) mx = u; n   fr[u] = la; n   for (long long i = h[u], v; i; i = nxt[i]) { n     if ((v = to[i]) == la) continue; n     fl[v] = len[i], d[v] = d[u] + len[i], dfs(v, u); n   } n } n void get_dis(long long u, long long deep) { n   f[u] = deep; n   vis[u] = 1; n   for (long long i = h[u]; i; i = nxt[i]) n     if (!vis[to[i]]) get_dis(to[i], deep + len[i]); n } n long long ff[N], dp[N]; n void get_fa(long long u, long long deep) { n   dp[u] = deep; n   for (long long i = h[u], v; i; i = nxt[i]) n     if ((v = to[i]) != ff[u]) ff[v] = u, get_fa(v, deep + 1); n } n bool cmp(long long x, long long y) { n   return f[x] == f[y] ? dp[x] < dp[y] : f[x] < f[y]; n } n long long fa[N], sz[N]; n long long find(long long x) { return fa[x] == x ? x : (fa[x] = find(fa[x])); } n void work(long long l) { n   long long res = 0; n   for (long long i = 1; i <= n; ++i) fa[i] = i, sz[i] = 1; n   for (long long i = n, j = n; i >= 1; --i) { n     while (f[p[j]] > f[p[i]] + l) --sz[find(p[j--])]; n     res = max(res, sz[p[i]]); n     fa[p[i]] = find(ff[p[i]]); n     sz[find(ff[p[i]])] += sz[p[i]]; n   } n   printf( %lld n , res); n } n signed main() { n   read(n); n   for (long long i = 1, u, v, l; i < n; ++i) n     read(u), read(v), read(l), add(u, v, l); n   dfs(1, 0); n   d[mx] = fl[mx] = 0, dfs(mx, 0); n   while (mx) sum += fl[mx], st[++tot] = mx, vis[mx] = 1, mx = fr[mx]; n   long long now = 0, mn = 1e15; n   for (long long i = 1; i <= tot; ++i) { n     get_dis(st[i], max(now, sum - now)); n     if (max(now, sum - now) < mn) mn = max(now, sum - now), rt = st[i]; n     now += fl[st[i]]; n   } n   get_fa(rt, 0); n   for (long long i = 1; i <= n; ++i) p[i] = i; n   sort(p + 1, p + n + 1, cmp); n   long long m, x; n   read(m); n   while (m--) read(x), work(x); n   return 0; n } n