#include <bits/stdc++.h> n using namespace std; n int alpha[26] = {0}, dig[105] = {0}; n long long int gcd(long long int a, long long int b) { n   if (a == 0) return b; n   return gcd(b % a, a); n } n long long int lcm(long long int a, long long int b) { n   return ((a * b) / gcd(a, b)); n } n bool isPrime(int 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 (int i = 5; i * i <= n; i = i + 6) n     if (n % i == 0 || n % (i + 2) == 0) return false; n   return true; n } n void getArr(int a[], int n) { n   for (int i = 0; i < n; i++) cin >> a[i]; n } n void putArr(int a[], int n) { n   for (int i = 0; i < n; i++) cout << a[i] <<    ; n   cout <<   n ; n } n void initAlpha(string s) { n   int n = s.size(); n   for (int i = 0; i < n; i++) alpha[s[i] -  a ]++; n } n void initDigit(int a[], int n) { n   for (int i = 0; i < n; i++) dig[a[i]]++; n } n bool isPalindrome(string input) { n   if (input == string(input.rbegin(), input.rend())) return true; n   return false; n } n int numLen(int n) { n   int len = 0; n   while (n != 0) { n     n /= 10; n     len++; n   } n   return len; n } n void robot() { n   string s; n   char n[50]; n   cin >> s; n   int l = s.size(), k = 0; n   bool flag = false; n   for (int i = l - 1; i >= 0; i--) { n     if (s[i] ==  0  && !flag) n       continue; n     else { n       n[k++] = s[i]; n       flag = true; n     } n   } n   n[k] =   0 ; n   if (isPalindrome(n)) n     cout <<  YES n ; n   else n     cout <<  NO n ; n } n int main() { robot(); } n


#include <bits/stdc++.h> n using namespace std; n int n, u, v, N; n vector<char> ans(1e5 + 5); n vector<int> sub(1e5 + 5); n vector<set<int> > adj(1e5 + 5); n void dfs(int u, int par) { n   sub[u] = 1; n   for (int v : adj[u]) { n     if (v != par) { n       dfs(v, u); n       sub[u] += sub[v]; n     } n   } n } n int centroid(int u, int par) { n   for (int v : adj[u]) { n     if (v != par && sub[v] > n / 2) { n       return centroid(v, u); n     } n   } n   return u; n } n void build(int u, int prev, int level) { n   dfs(u, -1); n   n = sub[u]; n   int cur = centroid(u, -1); n   ans[cur] = char(level + 65); n   for (int v : adj[cur]) { n     adj[v].erase(cur); n     build(v, cur, level + 1); n   } n } n int main(void) { n   ios ::sync_with_stdio(0); n   cin.tie(0); n   cin >> n; n   N = n; n   for (int i = 1; i <= n - 1; ++i) { n     cin >> u >> v; n     adj[u].insert(v); n     adj[v].insert(u); n   } n   build(1, -1, 0); n   for (int i = 1; i <= N; ++i) { n     cout << ans[i] <<    ; n   } n   cout <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long n, mod = 1e9 + 7ll, cont, maxi = 1000000000000ll, cont2, val[400005], n              ans, bestval = 1000000000000ll; n pair<long long, long long> dp[400005], num, ft[400005]; n pair<int, int> v[400005]; n map<int, int> ind; n vector<int> opz[400005], tmp; n void update(int pos, long long val, long long num) { n   for (int i = pos; i < 400005; i += (i & (-i))) { n     if (ft[i].first > val) { n       ft[i] = {val, num}; n     } else { n       if (ft[i].first == val) ft[i].second += num; n       ft[i].second %= mod; n     } n   } n } n pair<long long, long long> query(int pos) { n   long long sol = maxi, solnum = 0; n   for (int i = pos; i > 0; i -= (i & (-i))) { n     if (ft[i].first < sol) { n       sol = ft[i].first; n       solnum = ft[i].second; n     } else { n       if (ft[i].first == sol) { n         solnum += ft[i].second; n         solnum %= mod; n       } n     } n   } n   return {sol, solnum}; n } n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   cin >> n; n   for (int i = 0; i < n; i++) { n     cin >> v[i].first >> v[i].second; n     tmp.push_back(v[i].first); n     tmp.push_back(v[i].second); n   } n   sort(tmp.begin(), tmp.end()); n   cont = 1; n   for (int i = 0; i < tmp.size(); i++) { n     if (i == 0 || tmp[i] != tmp[i - 1]) { n       ind[tmp[i]] = cont; n       val[cont] = tmp[i]; n       cont++; n     } n   } n   for (int i = 0; i < n; i++) { n     opz[ind[v[i].first]].push_back(v[i].second); n   } n   for (int i = 0; i < 400005; i++) { n     sort(opz[i].begin(), opz[i].end()); n     ft[i] = {maxi, 0}; n   } n   update(1, maxi, 1); n   for (int i = 1; i < cont; i++) { n     cont2 = 0; n     if (opz[i].size() == 0) continue; n     dp[i].first = maxi; n     dp[i].second = 1; n     for (int j = 0; j < opz[i].size(); j++) { n       cont2++; n       if (j == opz[i].size() - 1 || opz[i][j] != opz[i][j + 1]) { n         num = query(ind[opz[i][j]]); n         num.first = num.first - (val[i] - opz[i][j]); n         num.second *= cont2; n         num.second %= mod; n         if (dp[i].first > num.first) { n           dp[i] = num; n         } else { n           if (dp[i].first == num.first) { n             dp[i].second += num.second; n             dp[i].second %= mod; n           } n         } n         cont2 = 0; n       } n     } n     update(i, dp[i].first, dp[i].second); n   } n   sort(v, v + n); n   reverse(v, v + n); n   for (int i = ind[v[0].first]; i > ind[v[0].second]; i--) { n     if (bestval > (dp[i].first - (maxi - val[i]))) { n       bestval = (dp[i].first - (maxi - val[i])); n       ans = dp[i].second; n     } else { n       if (bestval == (dp[i].first - (maxi - val[i]))) ans += dp[i].second; n       ans %= mod; n     } n   } n   cout << max(ans, 1ll) << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n string arr[9]; n int moves1[4] = {2, 2, -2, -2}, moves2[4] = {2, -2, 2, -2}; n int grid[2][9][9]; n bool visited[9][9]; n int incrementer; n void canMeet(int row, int col, int nw) { n   for (int i = 0; i < 4; ++i) { n     if (row + moves1[i] >= 0 && row + moves1[i] < 8 && col + moves2[i] >= 0 && n         col + moves2[i] < 8 && !visited[row + moves1[i]][col + moves2[i]]) { n       grid[incrementer][row + moves1[i]][col + moves2[i]] = nw + 1; n       visited[row + moves1[i]][col + moves2[i]] = true; n       canMeet(row + moves1[i], col + moves2[i], nw + 1); n     } n   } n } n int main() { n   int t; n   cin >> t; n   while (t--) { n     memset(grid, 0, sizeof(grid)); n     for (int i = 0; i < 8; ++i) { n       cin >> arr[i]; n     } n     int x, y, x1, y1; n     bool done = false; n     for (int i = 0; i < 8; ++i) { n       for (int j = 0; j < 8; ++j) { n         if (arr[i][j] ==  K  && !done) { n           x = i; n           y = j; n           done = true; n         } else if (arr[i][j] ==  K  && done) { n           x1 = i; n           y1 = j; n         } n       } n     } n     incrementer = 1; n     memset(visited, false, sizeof(visited)); n     visited[x][y] = true; n     canMeet(x, y, 0); n     incrementer = 1; n     memset(visited, false, sizeof(visited)); n     visited[x1][y1] = true; n     canMeet(x1, y1, 0); n     grid[1][x1][y1] = 2; n     grid[0][x][y] = 2; n     done = false; n     for (int i = 0; i < 8; ++i) { n       for (int j = 0; j < 8; ++j) { n         if ((arr[i][j] ==  .  || arr[i][j] ==  K ) && grid[0][i][j] != 0 && n             grid[1][i][j] != 0 && grid[0][i][j] % 2 == grid[1][i][j] % 2) n           done = true; n       } n     } n     if (done) n       cout <<  YES  << endl; n     else n       cout <<  NO  << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int AR[1000000 + 7]; n int main() { n   int cnt = 0, k; n   string s; n   cin >> k >> s; n   AR[0] = 1; n   int64_t r = 0; n   for (int i = 0; s[i] !=   0 ; i++) { n     if (s[i] ==  1 ) cnt++; n     if (cnt >= k) r += AR[cnt - k]; n     AR[cnt]++; n   } n   printf( %I64d n , r); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long int solve() { n   long long int n; n   cin >> n; n   vector<bool> dp(2 * n + 1, 0); n   vector<long long int> p(2 * n); n   vector<long long int> nums; n   long long int cover = 0; n   vector<long long int> inv(2 * n + 1); n   for (long long int j = 0; j < 2 * n; j++) { n     cin >> p[j]; n     inv[p[j]] = j; n     if (p[j] == 2 * n) { n       cover = j; n     } n   } n   nums.push_back(2 * n - cover); n   for (long long int j = 2 * n - 1; j >= 1; j--) { n     if (inv[j] > cover) { n       continue; n     } else { n       nums.push_back(cover - inv[j]); n       cover = inv[j]; n     } n   } n   sort(nums.begin(), nums.end()); n   dp[nums[0]] = 1; n   dp[0] = 1; n   long long int k = nums.size(); n   for (long long int j = 1; j < k; j++) { n     vector<long long int> f(2 * n + 1, 0); n     for (long long int i = 0; i <= 2 * n; i++) { n       f[i] = dp[i]; n       if (i - nums[j] >= 0) { n         f[i] = dp[i] || dp[i - nums[j]]; n       } n     } n     for (long long int i = 0; i <= 2 * n; i++) { n       dp[i] = f[i]; n     } n   } n   if (!dp[n]) { n     cout <<  NO n ; n   } else { n     cout <<  YES n ; n   } n   return 0; n } n signed main() { n   ios::sync_with_stdio(0); n   cin.tie(NULL); n   cout.tie(NULL); n   long long int t; n   cin >> t; n   while (t--) { n     solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n; n int s[220]; n int d[220]; n int sol[220]; n vector<pair<int, int> > v; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   ; n   cin >> n; n   for (int(i) = (0); (i) < (n); (i)++) cin >> s[i] >> d[i]; n   for (int(i) = (0); (i) < (n); (i)++) { n     int last = 1; n     int ok = 0; n     for (typeof(v.begin()) it = (v.begin()); it != v.end(); it++) { n       if (last <= s[i] && it->first >= s[i] + d[i]) { n         v.insert(it, make_pair((s[i]), (d[i]))); n         ok = 1; n         sol[i] = s[i]; n         break; n       } n       last = it->first + it->second; n     } n     if (v.empty() || n         v[((int)v.size()) - 1].first + v[((int)v.size()) - 1].second <= s[i]) { n       v.push_back(make_pair((s[i]), (d[i]))); n       ok = 1; n       sol[i] = s[i]; n     } n     if (!ok) { n       last = 1; n       for (typeof(v.begin()) it = (v.begin()); it != v.end(); it++) { n         if (it->first >= last + d[i]) { n           v.insert(it, make_pair((last), (d[i]))); n           sol[i] = last; n           ok = 1; n           break; n         } n         last = it->first + it->second; n       } n     } n     if (!ok) { n       v.push_back(make_pair((last), (d[i]))); n       sol[i] = last; n     } n     cout << sol[i] <<     << sol[i] + d[i] - 1 << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, seq[100005]; n char s[2][55]; n string a[100005], b[100005], cur; n void solve() { n   scanf( %d , &n); n   for (int i = 0; i < n; i++) { n     scanf( %s %s , s[0], s[1]); n     a[i] = s[0]; n     b[i] = s[1]; n   } n   for (int i = 0; i < n; i++) { n     scanf( %d , &seq[i]); n     seq[i]--; n   } n   if (a[seq[0]] < b[seq[0]]) n     cur = a[seq[0]]; n   else n     cur = b[seq[0]]; n   for (int i = 1; i < n; i++) { n     if (cur < a[seq[i]] && cur < b[seq[i]]) { n       if (a[seq[i]] < b[seq[i]]) n         cur = a[seq[i]]; n       else n         cur = b[seq[i]]; n     } else if (cur < a[seq[i]]) n       cur = a[seq[i]]; n     else if (cur < b[seq[i]]) n       cur = b[seq[i]]; n     else { n       puts( NO ); n       return; n     } n   } n   puts( YES ); n } n int main(void) { n   solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n set<string> s; n string v; n map<string, bool> m; n void rot(string x) { n   if (m[x] == 1) return; n   m[x] = 1; n   s.erase(x); n   string y = x; n   y[0] = x[1]; n   y[1] = x[2]; n   y[2] = x[3]; n   y[3] = x[0]; n   rot(y); n   y = x; n   y[1] = x[4]; n   y[4] = x[3]; n   y[3] = x[5]; n   y[5] = x[1]; n   rot(y); n   return; n } n int main() { n   cin >> v; n   for (int i = 0; i < 1000; i++) { n     next_permutation(v.begin(), v.end()); n     s.insert(v); n   } n   int ans = 0; n   while (s.size() > 0) { n     ans++; n     rot(*s.begin()); n   } n   cout << ans << endl; n } n


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


#include <bits/stdc++.h> n using namespace std; n const int M = 105; n bool visit[M]; n string second[M]; n int main() { n   int n, m; n   scanf( %d %d , &n, &m); n   for (int i = 1; i <= n; i++) cin >> second[i]; n   vector<string> v; n   for (int i = 0; i < m; i++) { n     int x; n     scanf( %d , &x); n     visit[x] = 1; n     v.push_back(second[x]); n   } n   for (int i = 1; i < m; i++) { n     if (v[i].size() != v[0].size()) { n       cout <<  No ; n       return 0; n     } n   } n   string pattern =   ; n   for (int i = 0; i < v[0].size(); i++) { n     set<char> c; n     for (int j = 0; j < m; j++) c.insert(v[j][i]); n     if (c.size() == 1) n       pattern += *c.begin(); n     else n       pattern +=  ? ; n   } n   for (int i = 1; i <= n; i++) { n     if (second[i].size() != pattern.size()) continue; n     bool match = 1; n     for (int j = 0; j < second[i].size(); j++) { n       if (pattern[j] != second[i][j] && pattern[j] !=  ? ) { n         match = 0; n         break; n       } n     } n     if (match && !visit[i]) { n       cout <<  No ; n       return 0; n     } n   } n   cout <<  Yes n  << pattern; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n inline long long mmul(long long a, long long b) { return (a * b) % 998244353; } n inline long long madd(long long a, long long b) { return (a + b) % 998244353; } n long long mpow(long long a, long long p) { n   if (p == 0) return 1; n   long long halfPow = mpow(a, p / 2); n   return mmul(halfPow, p % 2 ? mmul(a, halfPow) : halfPow); n } n long long minv(long long a) { return mpow(a, 998244353 - 2); } n long long mdiv(long long a, long long b) { return mmul(a, minv(b)); } n vector<long long> _mfactMem(1, 1); n long long mfact(long long a) { n   if (a >= 998244353) return 0; n   while (a >= _mfactMem.size()) n     _mfactMem.push_back(mmul(_mfactMem.size(), _mfactMem.back())); n   return _mfactMem[a]; n } n long long mcomb(long long n, long long k) { n   if (n == 0 && k == 0) return 1; n   long long ni = n % 998244353, ki = k % 998244353; n   if (ni < ki) return 0; n   long long comb = mmul(mfact(ni), mmul(minv(mfact(ki)), minv(mfact(ni - ki)))); n   return mcomb(n / 998244353, k / 998244353) * comb; n } n long long calc(int na, int nb) { n   long long cnt = 0; n   for (int i = 0; i <= min(na, nb); i++) { n     cnt = madd(cnt, mmul(mfact(i), mmul(mcomb(na, i), mcomb(nb, i)))); n   } n   return cnt; n } n int main() { n   int a, b, c; n   scanf( %d %d %d n , &a, &b, &c); n   int res = (int)mmul(calc(a, b), mmul(calc(b, c), calc(c, a))); n   printf( %d n , res); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long a[1009]; n int cmp(int a, int b) { return a > b; } n int main() { n   long long T; n   scanf( %lld , &T); n   while (T--) { n     long long n, sum = 0; n     scanf( %lld , &n); n     for (int i = 1; i <= n; i++) { n       scanf( %lld , &a[i]); n       sum = sum + a[i]; n     } n     sort(a + 1, a + n + 1, cmp); n     if (a[1] > sum / 2) { n       printf( T n ); n     } else if (a[1] <= sum / 2) { n       if (sum % 2 == 1) { n         printf( T n ); n       } else if (sum % 2 == 0) { n         printf( HL n ); n       } n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n  n using namespace std; n #define FAST      ios_base::sync_with_stdio(false); cin.tie(NULL); n #define prs(n)      cout << fixed << setprecision(n) n #define ll       long long n #define db       double n #define fo(i,m,n)       for(int i = m; i <= n; i++) n #define fob(i,m,n)      for(int i = n; i >= m; i--) n typedef                 pair<int, int> ii; n typedef                 vector<ii> vii; n typedef                 vector<vii> vvii; n const                   int maxi=INT_MAX; n const                   int _x[]={0,0,1,-1}; n const                   int _y[]={1,-1,0,0}; n  n ll n, ans, l[200007], r[200007], dp[200007][2]; n vector <ll> a[200007]; n  n void input() n { n     int u, v; n     cin >> n; n     for (int i = 1; i <= n; i++) n     { n         cin >> l[i] >> r[i], a[i].resize(0); n         dp[i][1] = dp[i][0] = 0; n     } n     for (int i = 1; i < n ; i++) n     { n         cin >> u >> v; n         a[u].push_back(v); n         a[v].push_back(u); n     } n } n  n void dfs(int u, int f) n { n     for (int i:a[u]) n     { n         if (i != f) n         { n             dfs(i,u); n             dp[u][0] += max(dp[i][0] + abs(l[u] - l[i]), dp[i][1] + abs(l[u] - r[i])); n             dp[u][1] += max(dp[i][0] + abs(r[u] - l[i]), dp[i][1] + abs(r[u] - r[i])); n         } n     } n } n  n void solve() n { n     dfs(1,0); n     ans = max(dp[1][0], dp[1][1]); n } n  n int main() n { n     FAST n     //freopen( a.INP ,  r , stdin); n     //freopen( a.OUT ,  w , stdout); n     int t; n     cin >> t; n     while (t--) n     { n         input(); n         solve(); n         cout << ans <<   n ; n     } n     return 0; n } n


#include <bits/stdc++.h> n using namespace std; n char str[1000000]; n int len; n bool yes(char s[3], int ls, int le) { n   while (true) { n   start: n     int mid = le - ls - 1, i; n     if (le != len - 1) { n       if (s[0] != s[1]) return 0; n     } n     int c[3] = {0}, sd[2]; n     c[(s[1] -  0 ) ^ 1] += len - le - 1; n     for (i = ls + 1; i < le; i++) { n       if (str[i] == s[0]) return 0; n     } n     c[(s[0] -  0 ) ^ 1] += mid; n     for (i = 0; i < ls; i++) { n       if (str[i] ==  ? ) n         c[2]++; n       else n         c[str[i] -  0 ]++; n     } n     if (len & 1) n       sd[0] = len / 2 - 1, sd[1] = sd[0] + 1; n     else n       sd[0] = len / 2 - 1, sd[1] = sd[0]; n     if (s[0] != s[1]) { n       if (c[s[1] -  0 ] > sd[s[1] -  0 ]) return 0; n       if (c[s[0] -  0 ] > sd[s[0] -  0 ]) { n         if (str[ls] ==  ? ) { n           for (i = ls - 1; i >= 0; i--) { n             if (str[i] ==  ?  || str[i] == s[0]) break; n           } n           if (i >= 0) { n             ls = i; n             goto start; n           } n         } n         return 0; n       } n     } else { n       if (s[0] ==  0 ) { n         if (c[0] + c[2] < sd[0]) return 0; n       } else { n         if (c[1] + c[2] < sd[1]) return 0; n       } n     } n     return 1; n   } n } n bool ok(char s[3]) { n   int i, ls, le; n   for (le = len - 1; le >= 0; le--) { n     if (str[le] == s[1] || str[le] ==  ? ) break; n   } n   if (le <= 0) return 0; n   for (ls = le - 1; ls >= 0; ls--) { n     if (str[ls] == s[0] || str[ls] ==  ? ) break; n   } n   if (ls < 0) return 0; n   return yes(s, ls, le); n } n void run() { n   len = strlen(str); n   if (ok( 00 )) printf( 00 n ); n   if (ok( 01 )) printf( 01 n ); n   if (ok( 10 )) printf( 10 n ); n   if (ok( 11 )) printf( 11 n ); n } n int main() { n   scanf( %s , str); n   run(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 100100, INFTY = 0x3f3f3f3f; n int A[MAXN], B[MAXN]; n vector<int> p[MAXN]; n int posb[330]; n int resp[330]; n int main() { n   int N, M, S, E; n   posb[0] = 0; n   resp[0] = 0; n   for (int i = 1; i <= 300; i++) { n     posb[i] = MAXN; n     resp[i] = INFTY; n   } n   for (int i = 1; i <= MAXN - 100; i++) p[i].clear(); n   scanf( %d %d %d %d , &N, &M, &S, &E); n   for (int i = 1; i <= N; i++) scanf( %d , &A[i]); n   for (int i = 1; i <= M; i++) { n     scanf( %d , &B[i]); n     p[B[i]].push_back(i); n   } n   for (int i = 1; i <= 10000; i++) { n     int tam = p[i].size(); n     if (tam == 0) continue; n   } n   for (int k = 1; k <= N; k++) { n     int x = A[k]; n     for (int i = 299; i >= 0; i--) { n       if (posb[i] == MAXN) continue; n       int ini = posb[i] + 1; n       int tam = p[x].size(); n       int prox = (lower_bound(&p[x][0], &p[x][tam], ini) - &p[x][0]); n       if (prox == tam) continue; n       posb[i + 1] = min(posb[i + 1], p[x][prox]); n       resp[i + 1] = min(resp[i + 1], p[x][prox] + k); n     } n   } n   int ans = 0; n   for (int i = 0; i <= 300; i++) { n     long long int qq = E, rr = S; n     qq *= i; n     qq += resp[i]; n     if (qq > rr) break; n     ans = i; n   } n   printf( %d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 100005; n long long child[N], w[N], dp[N][65][3], ans[N][2]; n void pre(int n) { n   for (int i = 0; i < n; i++) { n     dp[i][0][0] = w[i]; n     dp[i][0][2] = w[i]; n     dp[i][0][1] = child[i]; n   } n   for (int j = 1; j < 65; j++) { n     for (int i = 0; i < n; i++) { n       dp[i][j][1] = dp[dp[i][j - 1][1]][j - 1][1]; n       dp[i][j][0] = dp[i][j - 1][0] + dp[dp[i][j - 1][1]][j - 1][0]; n       dp[i][j][2] = min(dp[i][j - 1][2], dp[dp[i][j - 1][1]][j - 1][2]); n     } n   } n } n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   int n; n   long long k; n   cin >> n >> k; n   for (int i = 0; i < n; i++) cin >> child[i]; n   for (int i = 0; i < n; i++) cin >> w[i]; n   pre(n); n   int pt; n   for (int i = 0; i < n; i++) { n     pt = i; n     ans[i][1] = 1000000007ULL; n     for (long long j = 0; (1LL << j) <= k; j++) { n       if ((1LL << j) & k) { n         ans[i][0] += dp[pt][j][0]; n         ans[i][1] = min(ans[i][1], dp[pt][j][2]); n         pt = dp[pt][j][1]; n       } n     } n   } n   for (int i = 0; i < n; i++) cout << ans[i][0] <<     << ans[i][1] <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int T; n signed long long S[101010], E[101010]; n int win[101010], lose[101010]; n int canwin(signed long long s, signed long long e) { n   if (s == e) return 0; n   if (s >= e) return 1; n   if (e % 2) { n     return !(s % 2); n   } else { n     if (s * 2 > e) return (e - s) % 2; n     if (4 * s > e) return 1; n     return canwin(s, e / 4); n   } n } n int canlose(signed long long s, signed long long e) { n   if (e < 2 * s) return 1; n   return canwin(s, e / 2); n } n void solve() { n   int i, j, k, r, x, y; n   string s; n   cin >> T; n   for (i = 0; i < (T); i++) { n     cin >> S[i] >> E[i]; n     win[i] = canwin(S[i], E[i]); n     lose[i] = canlose(S[i], E[i]); n   } n   int w = 1, l = 0; n   for (i = 0; i < (T); i++) { n     if (w == l) break; n     if (w) { n       w = lose[i]; n       l = win[i]; n     } else { n       w = !lose[i]; n       l = !win[i]; n     } n   } n   cout << l <<     << w << endl; n } n int main(int argc, char** argv) { n   string s; n   int i; n   if (argc == 1) ios::sync_with_stdio(false), cin.tie(0); n   for (i = 0; i < (argc - 1); i++) s += argv[i + 1], s +=   n ; n   for (i = 0; i < (s.size()); i++) ungetc(s[s.size() - 1 - i], stdin); n   cout.tie(0); n   solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   map<int, bool> hash; n   int p, n; n   bool has = false; n   cin >> p >> n; n   for (int i = 0; i < n; i++) { n     int x; n     cin >> x; n     x %= p; n     if (hash.count(x) && !has) { n       cout << i + 1 << endl; n       has = true; n     } else { n       hash[x] = true; n     } n   } n   if (!has) printf( -1 ); n } n


#include <bits/stdc++.h> n using namespace std; n const double EPS = 1e-10; n const double PI = acos(-1.0); n int dp[51][51][51]; n int dfs(int n, int m, int k) { n   if (dp[n][m][k] >= 0) return dp[n][m][k]; n   if (k == 0 || n * m == k) return dp[n][m][k] = 0; n   dp[n][m][k] = 1e9; n   for (int j = 0; j <= k; ++j) { n     for (int i = 1; i < n; ++i) n       dp[n][m][k] = n           min(dp[n][m][k], dfs(n - i, m, k - j) + dfs(i, m, j) + m * m); n     for (int i = 1; i < m; ++i) n       dp[n][m][k] = n           min(dp[n][m][k], dfs(n, m - i, k - j) + dfs(n, i, j) + n * n); n   } n   return dp[n][m][k]; n } n int main() { n   cin.tie(0); n   ios_base::sync_with_stdio(false); n   fill((int*)dp, (int*)dp + 51 * 51 * 51, -1); n   int T; n   cin >> T; n   while (T--) { n     int n, m, k; n     cin >> n >> m >> k; n     cout << dfs(n, m, k) << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   long long int d; n   cin >> n >> d; n   vector<int> check_days; n   int a[n]; n   long long int prefix_sum[n]; n   cin >> a[0]; n   if (a[0] == 0) check_days.push_back(0); n   prefix_sum[0] = a[0]; n   for (int i = 1; i < n; i++) { n     cin >> a[i]; n     if (a[i] == 0) check_days.push_back(i); n     prefix_sum[i] = prefix_sum[i - 1] + a[i]; n   } n   int days = 0; n   long long int mi = LLONG_MIN; n   long long int max_balance_after[n]; n   for (int i = n - 1; i >= 0; i--) { n     if (prefix_sum[i] > d) { n       cout << -1; n       return 0; n     } n     mi = max(mi, prefix_sum[i]); n     max_balance_after[i] = mi; n   } n   int deposit_val = 0; n   for (int i = 0; i < check_days.size(); i++) { n     int j = check_days[i]; n     if (prefix_sum[j] + deposit_val < 0) { n       int new_bal = 0, val_to_deposit = 0; n       new_bal = max_balance_after[j] + deposit_val; n       val_to_deposit = d - new_bal; n       days++; n       deposit_val += val_to_deposit; n       if (prefix_sum[j] + deposit_val < 0) { n         cout << -1; n         return 0; n       } n     } n   } n   cout << days; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 5000; n map<int, int> cntmap; n long long ans; n int arr[MAXN]; n char s[MAXN]; n int a, ln; n void fillmap(int x) { cntmap[x]++; } n void getans(int x) { n   if (x == 0) { n     if (a == 0) ans += (ln * (ln + 1)) / 2; n   } else if (a % x == 0) n     if (cntmap.count(a / x)) ans += cntmap[a / x]; n } n void loop(void (*fun)(int)) { n   int sm = 0; n   for (int i = 0; i < ln; i++) { n     sm = arr[i]; n     for (int j = i; j < ln; j++) { n       if (j != i) sm += arr[j]; n       fun(sm); n     } n   } n   return; n } n int main() { n   scanf( %d %s , &a, s); n   ans = 0; n   ln = strlen(s); n   for (int i = 0; i < ln; i++) arr[i] = s[i] -  0 ; n   loop(fillmap); n   loop(getans); n   cout << ans << endl; n } n


#include <bits/stdc++.h> n using namespace std; n const long double PI = acos(-1.0); n const long double EPS = 1e-12; n const int MAXN = (int)1e5; n const int INF = (int)2e9; n int main() { n   int n, a[100]; n   scanf( %d , &n); n   for (int i = 0; i < n; ++i) scanf( %d , &a[i]); n   int ans = INF; n   for (int i = 0; i < n; ++i) { n     int sum = 0; n     for (int j = 0; j < a[i]; ++j) { n       int num; n       scanf( %d , &num); n       sum += num * 5 + 15; n     } n     ans = min(ans, sum); n   } n   printf( %d , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 20, Mod = 1e9 + 7; n int pl, pr, vl, vr, k; n long double p, q; n vector<int> v; n void count(int t) { n   int L, R; n   L = min(v[t], pr) - max(v[t - 1] + 1, pl); n   R = min(v[t + k] - 1, vr) - max(v[t + k - 1], vl); n   p += max(L + 1, 0) * 1ll * max(R + 1, 0); n   L = min(v[t], vr) - max(v[t - 1] + 1, vl); n   R = min(v[t + k] - 1, pr) - max(v[t + k - 1], pl); n   p += max(L + 1, 0) * 1ll * max(R + 1, 0); n   if (k == 1) { n     if (vl <= v[t] && v[t] <= vr && pl <= v[t] && v[t] <= pr) p--; n   } n } n void add(int length) { n   for (int i = 0; i < (1 << length); i++) { n     int num = 0; n     for (int j = 0; j < length; j++) { n       num *= 10; n       if (i & (1 << j)) n         num += 4; n       else n         num += 7; n     } n     v.push_back(num); n   } n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cin >> pl >> pr >> vl >> vr >> k; n   q = (pr - pl + 1) * 1ll * (vr - vl + 1); n   for (int i = 1; i <= 9; i++) add(i); n   v.push_back(0); n   v.push_back(Mod); n   v.push_back(Mod); n   sort(v.begin(), v.end()); n   int n = v.size(); n   for (int i = 1; i < n - k - 1; i++) { n     count(i); n   } n   cout << fixed << setprecision(10) << p / q; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 2e5 + 5; n const int M = 5.1e6 + 5; n const int B = 10; n int n, q; n int h[M]; n int go[B + 1][N]; n vector<int> dv[N]; n int best[8][M]; n int main() { n   for (int i = 2; i * i < M; i++) { n     if (!h[i]) { n       for (int j = i; j < M; j += i) h[j] = i; n     } n   } n   scanf( %d %d , &n, &q); n   for (int i = 1; i <= n; i++) { n     int x; n     scanf( %d , &x); n     while (h[x]) { n       int y = h[x], c = 0; n       while (x % y == 0) { n         x /= y; n         c ^= 1; n       } n       if (c) dv[i].push_back(y); n     } n     if (x > 1) dv[i].push_back(x); n     assert(dv[i].size() <= 7); n   } n   for (int need = 0; need <= B; need++) { n     go[need][n + 1] = n + 1; n     memset(best, 0, sizeof(best)); n     for (int i = n; i >= 1; i--) { n       go[need][i] = go[need][i + 1]; n       for (int mask = 0; mask < (1 << dv[i].size()); mask++) { n         int prod = 1, cnt = dv[i].size() - __builtin_popcount(mask); n         for (int j = 0; j < dv[i].size(); j++) n           if (mask & (1 << j)) prod *= dv[i][j]; n         if (cnt <= need and need - cnt <= 7 and best[need - cnt][prod]) { n           go[need][i] = min(go[need][i], best[need - cnt][prod]); n         } n         best[cnt][prod] = i; n       } n     } n   } n   for (int i = 1; i <= q; i++) { n     int l, r; n     scanf( %d %d , &l, &r); n     for (int i = 0; i <= B + 1; i++) { n       if (i > B or go[i][l] <= r) { n         printf( %d n , i); n         break; n       } n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long inf = 0x3f3f3f3f3f3f3f3fll; n inline long long read() { n   long long x = 0, f = 1; n   char c = getchar(); n   while (c <  0  || c >  9 ) { n     if (c ==  - ) f = -1; n     c = getchar(); n   } n   while (c >=  0  && c <=  9 ) { n     x = (x << 1) + (x << 3) + c -  0 ; n     c = getchar(); n   } n   return x * f; n } n int n, x[5005], m, p[5005], c[5005], q[5005], l, r, t[5005]; n long long s[5005], dp[5005][5005]; n int main() { n   n = read(), m = read(); n   for (int i = 1; i <= n; ++i) { n     x[i] = read(); n   } n   sort(x + 1, x + n + 1); n   for (int i = 1; i <= m; ++i) { n     p[i] = read(), c[i] = read(); n     t[i] = i; n   } n   sort(t + 1, t + m + 1, [&](int i, int j) { return p[i] < p[j]; }); n   memset(dp, 0x3f, sizeof(dp)); n   dp[0][0] = 0; n   for (int j = 1; j <= m; ++j) { n     r = 0, l = 1; n     for (int i = 1; i <= n; ++i) { n       s[i] = s[i - 1] + abs(x[i] - p[t[j]]); n     } n     for (int i = 0; i <= n; ++i) { n       while (l <= r && q[l] < i - c[t[j]]) ++l; n       while (l <= r && dp[i][j - 1] - s[i] <= dp[q[r]][j - 1] - s[q[r]]) --r; n       q[++r] = i; n       dp[i][j] = dp[q[l]][j - 1] + s[i] - s[q[l]]; n     } n   } n   if (dp[n][m] == inf) { n     printf( -1 n ); n     return 0; n   } n   printf( %lld n , dp[n][m]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int pr[2 * 100 * 1000 + 10]; n int a[2 * 100 * 1000 + 10]; n int main() { n   int max_n = 0; n   int n; n   cin >> n; n   for (int i = 0; i < n; i++) { n     cin >> a[i]; n     if (a[i] > max_n) max_n = a[i]; n   } n   for (int i = 0; i <= max_n; i++) pr[i] = 1; n   for (int i = 2; i <= max_n; i++) n     if (pr[i]) { n       int k = 2; n       while (i * k <= max_n) { n         pr[i * k] = 0; n         k++; n       } n     } n   long long ans = 1; n   for (long long i = 2; i <= max_n; i++) { n     if (!pr[i]) continue; n     int bad_index = -1; n     long long cur = i; n     while (cur <= max_n) { n       int cnt = 0; n       int new_bad = -1; n       for (int j = 0; j < n; j++) n         if (j != bad_index && a[j] % cur) { n           cnt++; n           new_bad = j; n           if (cnt > 1) break; n         } n       if (cnt > 1) break; n       if (bad_index != -1 && new_bad != -1) break; n       if (bad_index == -1) bad_index = new_bad; n       ans *= i; n       cur *= i; n     } n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n map<double, int> Freq; n int main() { n   double N; n   double W; n   scanf( %lf%lf , &N, &W); n   vector<double> vec; n   for (int i = 0; i < N * 2; i++) { n     double a; n     scanf( %lf , &a); n     vec.push_back(a); n   } n   sort((vec).begin(), (vec).end()); n   double ans = min(vec[0] * 1.0, vec[(int)(N + 1e-9)] / 2.0); n   ans = min(ans, W * 1.0 / (N * 3.0)); n   printf( %.9lf n , 3 * N * ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<int> edge[200002], col; n int vis[200002]; n queue<int> q; n void bfs(int s) { n   vis[s] = 1; n   q.push(s); n   while (!q.empty()) { n     int x = q.front(); n     q.pop(); n     set<int> st; n     for (auto y : edge[x]) { n       if (vis[y]) { n         st.insert(col[y - 1]); n       } n     } n     if (col[x - 1] == -1) { n       int flag = 1; n       while (1) { n         if (st.find(flag) != st.end()) { n           flag++; n         } else { n           col[x - 1] = flag; n           break; n         } n       } n     } n     int start = col[x - 1] - 1, end = col[x - 1] + 1; n     for (auto y : edge[x]) { n       if (!vis[y]) { n         vis[y] = 1; n         q.push(y); n         int flag = 0; n         while (!flag) { n           if (start > 0) { n             if (st.find(start) != st.end()) { n               start--; n             } else { n               col[y - 1] = start; n               st.insert(start); n               start--; n               flag = 1; n             } n           } else { n             if (st.find(end) != st.end()) { n               end++; n             } else { n               col[y - 1] = end; n               st.insert(end); n               end++; n               flag = 1; n             } n           } n         } n       } n     } n   } n } n void solve() { n   int n; n   cin >> n; n   for (int i = 0; i < n - 1; i++) { n     int x, y; n     cin >> x >> y; n     edge[x].push_back(y); n     edge[y].push_back(x); n   } n   col.assign(n, -1); n   for (int i = 1; i <= n; i++) { n     if (edge[i].size() > 1) { n       bfs(i); n       break; n     } n   } n   int mx = 1; n   for (auto x : col) { n     mx = max(x, mx); n   } n   cout << mx <<   n ; n   for (auto x : col) { n     cout << x <<    ; n   } n   cout <<   n ; n } n int main() { n   ios_base ::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   solve(); n } n


#include <bits/stdc++.h> n using namespace std; n struct node { n   int val, id; n   friend bool operator<(node a, node b) { return a.id > b.id; } n }; n int main() { n   int t, m, n; n   struct node p; n   while (~scanf( %d %d , &m, &n)) { n     priority_queue<node> a; n     priority_queue<node> b; n     int id = 0; n     for (int i = 0; i < m; i++) { n       scanf( %d , &t); n       p.id = id++; n       p.val = t; n       a.push(p); n     } n     id = 0; n     ; n     for (int i = 0; i < n; i++) { n       scanf( %d , &t); n       p.id = id++; n       p.val = t; n       b.push(p); n     } n     while (!b.empty() && !a.empty()) { n       if (a.top().val <= b.top().val) { n         a.pop(); n         b.pop(); n       } else if (a.top().val > b.top().val) { n         a.pop(); n       } n     } n     printf( %d n , n - b.size()); n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 2e5; n const int MAXM = 2e5; n int NV, NH; n int V[MAXN]; n int main() { n   ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); n   cin >> NV >> NH; n   for (int i = 0; i < NV; i++) { n     cin >> V[i]; n   } n   priority_queue<int, vector<int>, greater<int>> H; n   for (int i = 0; i < NH; i++) { n     int x1, x2, y; n     cin >> x1 >> x2 >> y; n     if (x1 == 1) { n       H.push(x2); n     } else { n       continue; n     } n   } n   int res = NV + NH; n   sort(V, V + NV); n   V[NV] = int(1e9); n   for (int i = 0; i <= NV; i++) { n     while (!H.empty() && H.top() < V[i]) { n       H.pop(); n     } n     res = min(res, i + int(H.size())); n   } n   cout << res <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long double EPS = 1e-3; n const int INF = (long double)(1e9); n const long long mod = (int)(1e+9 + 7); n const int N = (int)(2e3); n int days_month[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; n long long ekn; n long long inverse; n long long expo(long long b, long long e) { n   long long prod = 1; n   while (e != 0) { n     if (e & 1) prod = (1LL * prod * b) % mod; n     b = (1LL * b * b) % mod; n     e >>= 1; n   } n   return prod % mod; n } n int f(int i) { n   long long prod = (1LL * expo(2, i) * (ekn - 1)) % mod; n   prod = (prod * inverse) % mod; n   return prod; n } n int main() { n   string s; n   cin >> s; n   int k; n   cin >> k; n   int n = int(s.size()); n   ekn = expo(2, 1LL * k * n); n   inverse = expo(expo(2, n) - 1, mod - 2); n   int ans = 0; n   for (int i = 0; i < n; i++) { n     if (s[i] !=  0  && s[i] !=  5 ) continue; n     ans += f(i); n     if (ans >= mod) ans -= mod; n   } n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 2 * (int)1e5 + 100; n int n, k; n char a[N]; n int main() { n   scanf( %d %d , &n, &k); n   scanf( %s , a); n   int tmp = 0, result = 0, ans = 0; n   for (int i = 0; i < 26; i++) { n     char ch =  a  + i; n     tmp = 0; n     result = 0; n     for (int j = 0; j <= n; j++) { n       if (a[j] == ch) { n         tmp++; n       } else { n         result += tmp / k; n         tmp = 0; n       } n     } n     ans = max(ans, result); n   } n   printf( %d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAX_N = 110; n const int INF = 1 << 28; n const double EPS = 1e-9; n const double PI = 3.14159265358979; n int n, d; n int a[MAX_N]; n int x[MAX_N], y[MAX_N]; n int g[MAX_N][MAX_N]; n int main() { n   cin >> n >> d; n   for (int i = (1); i < (n - 1); i++) cin >> a[i]; n   for (int i = (0); i < (n); i++) cin >> x[i] >> y[i]; n   for (int i = (0); i < (n); i++) n     for (int j = (0); j < (n); j++) { n       g[i][j] = d * (abs(x[i] - x[j]) + abs(y[i] - y[j])); n       if (i != j) g[i][j] -= a[j]; n     } n   for (int k = (0); k < (n); k++) n     for (int i = (0); i < (n); i++) n       for (int j = (0); j < (n); j++) g[i][j] = min(g[i][j], g[i][k] + g[k][j]); n   cout << g[0][n - 1] << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n map<long long, long long> Map; n void modify(long long x, long long y) { n   map<long long, long long>::iterator it; n   long long c; n   scanf( %I64d , &c); n   while (x != y) { n     if (x < y) swap(x, y); n     it = Map.find(x); n     if (it != Map.end()) n       it->second += c; n     else n       Map[x] = c; n     x >>= 1; n   } n } n long long query(long long x, long long y) { n   map<long long, long long>::iterator it; n   long long ans = 0; n   while (x != y) { n     if (x < y) swap(x, y); n     it = Map.find(x); n     if (it != Map.end()) ans += it->second; n     x >>= 1; n   } n   return ans; n } n int main(int argc, char* argv[]) { n   int q = 0; n   for (; scanf( %d , &q) != EOF;) { n     Map.clear(); n     while (q--) { n       int od; n       long long x, y, c; n       scanf( %d%I64d%I64d , &od, &x, &y); n       if (od == 1) n         modify(x, y); n       else n         printf( %I64d n , query(x, y)); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n int a[1000005]; n double tt; n int main() { n   int n, i, t; n   double vb, vs, x, y, d; n   while (scanf( %d %lf %lf , &n, &vb, &vs) != EOF) { n     t = 2; n     for (i = 1; i <= n; i++) { n       scanf( %d , &a[i]); n     } n     scanf( %lf %lf , &x, &y); n     d = a[2] / vb + sqrt((x - a[2]) * (x - a[2]) + y * y) / vs; n     for (i = 2; i <= n; i++) { n       tt = a[i] / vb + sqrt((x - a[i]) * (x - a[i]) + y * y) / vs; n       if (tt <= d) { n         d = tt; n         t = i; n       } n     } n     printf( %d n , t); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long mod = 1e9 + 7; n int sumOfDigits(int n) { n   int ans = 0; n   if (n <= 9) return n; n   while (n) { n     ans += n % 10; n     n /= 10; n   } n   return ans; n } n int main() { n   cin.tie(NULL); n   ios_base::sync_with_stdio(false); n   int a; n   cin >> a; n   while (sumOfDigits(a) % 4 != 0) { n     a++; n   } n   cout << a << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long maxn = 4e5 + 10; n const long long mod = 998244353; n const long long inf = 1e18 + 10; n const long long Log = 21; n long long n, m, k; n long long pw(long long a, long long b) { n   if (b == 0) return 1; n   long long x = pw(a, b / 2); n   x = (x * x) % mod; n   if (b & 1) x = (x * a) % mod; n   return x; n } n long long cnt(long long x, long long p) { n   long long X = 0; n   while (x % p == 0) { n     x /= p; n     X++; n   } n   return X; n } n long long D; n vector<long long> pr; n long long fac[maxn], repo[maxn]; n signed main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   fac[0] = 1; n   repo[0] = 1; n   for (long long i = 1; i < 200; i++) { n     fac[i] = (fac[i - 1] * i) % mod; n     repo[i] = pw(fac[i], mod - 2); n   } n   cin >> D; n   for (long long i = 2; i <= sqrt(D); i++) n     if (D % i == 0) { n       while (D % i == 0) D /= i; n       pr.push_back(i); n     } n   if (D > 1) pr.push_back(D); n   long long q; n   cin >> q; n   while (q--) { n     long long u, v; n     cin >> u >> v; n     if (u > v) swap(u, v); n     long long ans = 1, sm2 = 0, sm1 = 0; n     for (auto p : pr) { n       long long x = cnt(u, p); n       long long y = cnt(v, p); n       (ans *= repo[abs(x - y)]) %= mod; n       if (x < y) sm1 += y - x; n       if (x > y) sm2 += x - y; n     } n     (ans *= (fac[sm1] * fac[sm2]) % mod) %= mod; n     cout << ans <<   n ; n   } n } n


#pragma GCC optimize( -Ofast , -funroll-all-loops ) n #pragma GCC optimize(2) n #pragma GCC optimize(3, Ofast , inline ) n #include<bits/stdc++.h> n using namespace std; n  n typedef long long ll; n int _; n int n; n int a[30010]; n  n void solve(){ n     cin >> _; n     while(_--){ n         cin >> n; n         int cnt0 = 0, cnt1 = 0, cnt2 = 0; n         for(int i = 1; i <= n; i++){ n             cin >> a[i]; n             if(a[i] % 3 == 0){ n                 cnt0++; n             } n             else if(a[i] % 3 == 1){ n                 cnt1++; n             } n             else{ n                 cnt2++; n             } n         } n         int need = n/3; n         if(cnt0 == need && cnt1 == need && cnt2 == need){ n             cout <<  0  <<   n ; n         } n         else if(cnt0 > need && cnt1 > need && cnt2 < need){ n             cout << (cnt0-need)*2 + (cnt1-need) <<   n ; n         } n         else if(cnt0 < need && cnt1 > need && cnt2 > need){ n             cout << (cnt1-need)*2+(cnt2-need) <<   n ; n         } n         else if(cnt0 > need && cnt1 < need && cnt2 > need){ n             cout << (cnt0-need) + (cnt2-need)*2 <<   n ; n         } n         else if(cnt0 > need && cnt1 < need && cnt2 < need){ n             cout << (need-cnt1) + (need-cnt2)*2 <<   n ; n         } n         else if(cnt0 < need && cnt1 > need && cnt2 < need){ n             cout << (need-cnt2) + 2*(need-cnt0) <<   n ; n         } n         else if(cnt0 < need && cnt1 < need && cnt2 > need){ n             cout << (need-cnt0) + 2*(need-cnt1) <<   n ; n         } n         else if(cnt0 == need){ n             if(cnt1 > need && cnt2 < need){ n                 cout << (cnt1-need) <<   n ; n             } n             else{ n                 cout << 2*(cnt2-need) <<  n ; n             } n         } n         else if(cnt1 == need){ n             if(cnt0 > need && cnt2 < need){ n                 cout << 2*(cnt0-need) <<   n ; n             } n             else{ n                 cout << (cnt2-need) <<   n ; n             } n         } n         else if(cnt2 == need){ n             if(cnt0 > need && cnt1 < need){ n                 cout << (cnt0 - need) <<   n ; n             } n             else{ n                 cout << 2*(cnt1 - need) <<   n ; n             } n         } n     } n     return ; n } n  n int main(){ n     ios::sync_with_stdio(false); n     cin.tie(0); n     cout.tie(0); n      n     solve(); n     return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 200005; n int n, a[maxn], total; n void solve() { n   vector<int> ans; n   for (int i = 1; i <= n; i++) { n     if (a[i] * n == total) ans.push_back(i); n   } n   cout << (int)ans.size() << endl; n   for (int i = 0; i < ans.size(); i++) { n     if (i) printf(   ); n     cout << ans[i]; n   } n   cout << endl; n } n int main() { n   cin >> n; n   total = 0; n   for (int i = 1; i <= n; i++) { n     cin >> a[i]; n     total += a[i]; n   } n   solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long arr[1000000], arr1[1000000], cnt2[1000000]; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   long long n, k, i, j, x, y, z, ans, a, b, c; n   cin >> n >> k; n   for (i = 1; i <= n; i++) { n     cin >> arr[i]; n   } n   for (i = 1; i <= n; i++) { n     cin >> arr1[i]; n   } n   x = arr[k] + arr1[1]; n   y = 1; n   z = n; n   for (i = 1; i <= k; i++) { n     if (arr[i] + arr1[z] <= x) { n       z--; n     } else if (arr[i] + arr1[z] > x) { n       y++; n     } n   } n   cout << y << endl; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 500010; n struct edge { n   int nxt, t; n } e[N * 3]; n int head[N], edge_cnt; n void add_edge(int x, int y) { n   e[edge_cnt] = (edge){head[x], y}; n   head[x] = edge_cnt++; n } n int dfn[N], low[N], Tar_cnt, stk[N], stk_top, loop_cnt, ID[N], P[N], tot, st[N], n     ed[N]; n void Tarjan(int x, int f) { n   dfn[x] = low[x] = ++Tar_cnt; n   stk[++stk_top] = x; n   int i; n   for (i = head[x]; ~i; i = e[i].nxt) { n     int to = e[i].t; n     if (to == f) { n       continue; n     } n     if (dfn[to] == 0) { n       Tarjan(to, x); n       low[x] = min(low[x], low[to]); n     } else { n       low[x] = min(low[x], dfn[to]); n     } n   } n   if (low[x] == dfn[x]) { n     int y; n     loop_cnt++; n     st[x] = tot + 1; n     do { n       y = stk[stk_top--]; n       ID[y] = loop_cnt; n       P[++tot] = y; n     } while (x != y); n     ed[x] = tot; n   } n } n int D[N], Mx[N][2], dis[N], Fa[N]; n bool vis[N]; n void cmp(int x, int y) { n   if (dis[Mx[x][0]] < dis[y]) { n     Mx[x][1] = Mx[x][0]; n     Mx[x][0] = y; n   } else if (dis[Mx[x][1]] < dis[y]) { n     Mx[x][1] = y; n   } n } n void BFS(int n) { n   int L = 1, R = 1, i; n   D[1] = 1; n   vis[1] = 1; n   while (L <= R) { n     int x = D[L++]; n     for (i = head[x]; ~i; i = e[i].nxt) { n       int to = e[i].t; n       if (vis[to]) { n         continue; n       } n       vis[to] = 1; n       Fa[to] = x; n       D[++R] = to; n     } n   } n   dis[0] = -1; n   for (i = n; i > 1; i--) { n     int x = D[i], y = Fa[x]; n     if (ID[x] != ID[y]) { n       cmp(y, x); n     } n     dis[y] = max(dis[y], dis[x] + 1); n   } n } n struct node { n   int id, w; n } Q[N << 1]; n int G[N], val[N], Ans[N], dp[N]; n void Push(int &L, int &R, node x) { n   while (L <= R && Q[R].w <= x.w) { n     R--; n   } n   Q[++R] = x; n } n void Pop1(int &L, int x) { n   if (Q[L].id < x) { n     L++; n   } n } n void Pop2(int &L, int x) { n   if (Q[L].id > x) { n     L++; n   } n } n void Solve(int S, int f, int Fr) { n   int i, j, d = ed[S] - st[S] + 1, k, L, R; n   dp[S] = Fr; n   if (d > 1) { n     for (i = st[S]; i <= ed[S]; i++) { n       int x = P[i], t = dis[Mx[x][0]] + 1; n       G[i - st[S] + 1] = x; n       Ans[x] = t; n       if (x == S) { n         val[i - st[S] + 1] = max(Fr, t); n       } else { n         val[i - st[S] + 1] = t; n       } n     } n     L = 1, R = 0, k = d / 2; n     for (i = d - k + 1; i <= d; i++) { n       Push(L, R, (node){i - d, val[i] - (i - d)}); n     } n     for (i = 1; i <= d; i++) { n       Pop1(L, i - k); n       dp[G[i]] = max(dp[G[i]], Q[L].w + i); n       Push(L, R, (node){i, val[i] - i}); n     } n     L = 1, R = 0, k = (d - 1) / 2; n     for (i = k; i >= 1; i--) { n       Push(L, R, (node){i + d, val[i] + (i + d)}); n     } n     for (i = d; i >= 1; i--) { n       Pop2(L, i + k); n       dp[G[i]] = max(dp[G[i]], Q[L].w - i); n       Push(L, R, (node){i, val[i] + i}); n     } n   } else { n     Ans[S] = max(Fr, dis[Mx[S][0]] + 1); n   } n   for (i = st[S]; i <= ed[S]; i++) { n     int x = P[i]; n     for (j = head[x]; ~j; j = e[j].nxt) { n       int to = e[j].t; n       if (ID[x] == ID[to] || to == f) { n         continue; n       } n       if (Mx[x][0] == to) { n         Solve(to, x, max(dp[x] + 1, dis[Mx[x][1]] + 2)); n       } else { n         Solve(to, x, max(dp[x] + 1, dis[Mx[x][0]] + 2)); n       } n     } n   } n } n int main() { n   int n, m, i; n   scanf( %d%d , &n, &m); n   memset(head + 1, -1, sizeof(int) * 1 * n); n   for (i = 1; i <= m; i++) { n     int x, y; n     scanf( %d%d , &x, &y); n     add_edge(x, y); n     add_edge(y, x); n   } n   memset(dfn + 1, 0, sizeof(int) * 1 * n); n   Tarjan(1, 0); n   BFS(n); n   Solve(1, 0, 0); n   for (i = 1; i <= n; i++) { n     printf( %d n , max(Ans[i], dp[i])); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long mod = 998244353; n int n, q, a[300500], dp[300500][30], best[300500][30], last[30]; n int f(int x, int bt) { n   if (a[x] & (1 << bt)) return x; n   if (dp[x][bt] != -1) return dp[x][bt]; n   int mn = 1e9; n   for (int i = 0; i <= 27; i++) { n     if (a[x] & (1 << i) && best[x][i] != 1e9) mn = min(mn, f(best[x][i], bt)); n   } n   return dp[x][bt] = mn; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   memset(dp, -1, sizeof dp); n   for (int i = 0; i <= 27; i++) last[i] = 1e9; n   cin >> n >> q; n   for (int i = 0; i < n; i++) cin >> a[i]; n   for (int i = n - 1; i >= 0; i--) { n     for (int j = 0; j <= 27; j++) best[i][j] = 1e9; n     for (int j = 0; j <= 27; j++) { n       best[i][j] = last[j]; n       if ((a[i] & (1 << j))) last[j] = i; n     } n   } n   while (q--) { n     int x, y, ans = 0; n     cin >> x >> y; n     x--, y--; n     for (int i = 0; i <= 27; i++) { n       if ((a[y] & (1 << i)) && f(x, i) <= y) ans = 1; n     } n     if (ans) n       cout <<  Shi  << endl; n     else n       cout <<  Fou  << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int OO = (int)2e9; n const double PI = 2 * acos(0.0); n const double EPS = 1e-9; n int dcmp(double a, double b) { return fabs(a - b) <= EPS ? 0 : a > b ? 1 : 2; } n int DI[] = {-1, 0, 1, 0, 1, -1, -1, 1}; n int DJ[] = {0, 1, 0, -1, 1, -1, 1, -1}; n map<int, long long> m; n int main() { n   int n; n   cin >> n; n   for (int i = 0; i < (int)n; i++) { n     int x; n     cin >> x; n     m[x]++; n   } n   long long ans = 0; n   for (int i = -10; i < 0; i++) n     if (m.count(i) && m.count(abs(i))) ans += (m[i] * m[abs(i)]); n   if (m.count(0)) { n     for (long long t = m[0] - 1; t > 0; t--) ans += t; n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n priority_queue<pair<int, int>, vector<pair<int, int> >, n                greater<pair<int, int> > > n     que; n stringstream ssin; n const long long LINF = 0x7fffffffffffffffll; n const int N = 4e5 + 5, M = 4e5 + 5, mod = 1e9 + 7, INF = 0x3f3f3f3f; n int n, idx, sum; n int e[M], ne[M], h[N], ans[N]; n inline long long read() { n   char c = getchar(); n   long long x = 0, f = 1; n   while (c <  0  || c >  9 ) { n     if (c ==  - ) f = -1; n     c = getchar(); n   } n   while (c >=  0  && c <=  9 ) { n     x = x * 10 + c -  0 ; n     c = getchar(); n   } n   return x * f; n } n void add(int a, int b) { n   e[idx] = b; n   ne[idx] = h[a]; n   h[a] = idx++; n } n void dfs(int u, int fa) { n   for (int i = h[u]; ~i; i = ne[i]) { n     int v = e[i]; n     if (v == fa) continue; n     dfs(v, u); n   } n   if (fa != -1) { n     if (ans[u] == u) { n       swap(ans[u], ans[fa]); n       sum += 2; n     } n   } n } n int main() { n   n = read(); n   memset(h, -1, sizeof h); n   ; n   for (int i = 1; i < n; ++i) { n     ans[i] = i; n     int a, b; n     a = read(); n     b = read(); n     add(a, b); n     add(b, a); n   } n   ans[n] = n; n   dfs(1, -1); n   if (ans[1] == 1) { n     sum += 2; n     for (int i = h[1]; ~i; i = ne[i]) { n       int v = e[i]; n       swap(ans[1], ans[v]); n       break; n     } n   } n   cout << sum <<   n ; n   for (int i = 1; i <= n; ++i) cout << ans[i] <<    ; n   puts(  ); n } n


#include <bits/stdc++.h> n using namespace std; n long long inv(long long a, long long mod) { n   long long b = mod, p = 1, q = 0; n   while (b) { n     long long c = a / b, d = a; n     a = b; n     b = d % b; n     d = p; n     p = q; n     q = d - c * q; n   } n   return (p + mod) % mod; n } n int fs = 505050; n vector<long long> fact(fs), ifact(fs); n long long binom(int a, int b, long long mod) { n   if (a < b) return 0; n   return fact[a] * ifact[b] % mod * ifact[a - b] % mod; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   int n; n   cin >> n; n   long long k; n   cin >> k; n   vector<long long> a(n); n   for (int i = 0; i < n; i++) cin >> a[i]; n   long long mod = 998244353; n   fact[0] = 1; n   for (int i = 1; i < fs; i++) fact[i] = fact[i - 1] * i % mod; n   ifact[fs - 1] = inv(fact[fs - 1], mod); n   for (int i = fs - 1; i > 0; i--) ifact[i - 1] = ifact[i] * i % mod; n   long long res = 0; n   int same = 0; n   for (int i = 0; i < n; i++) { n     if (a[i] == a[(i + n - 1) % n]) same++; n   } n   int x = n - same; n   if (x & 1) { n     long long coef = k - 2; n     for (int i = 1; i <= x; i += 2) { n       res += n           binom(x, i, mod) * coef % mod * binom(x - i, (x - i) / 2, mod) % mod; n       res %= mod; n       coef = coef * (k - 2) % mod * (k - 2) % mod; n     } n   } else { n     long long coef = 1; n     for (int i = 0; i <= x; i += 2) { n       res += n           binom(x, i, mod) * coef % mod * binom(x - i, (x - i) / 2, mod) % mod; n       res %= mod; n       coef = coef * (k - 2) % mod * (k - 2) % mod; n     } n   } n   for (int i = 0; i < same; i++) { n     res = res * k % mod; n   } n   long long ent = 1; n   for (int i = 0; i < n; i++) { n     ent = ent * k % mod; n   } n   res = (ent - res + mod) % mod; n   res = res * ((mod + 1) / 2) % mod; n   cout << res <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAX = 1e5 + 20, MAX2 = 1e7 + 10; n int an[MAX], n, x; n bool primes[MAX2]; n void seive(long long n) { n   for (long long i = 2; i <= n; i++) primes[i] = 1; n   for (long long i = 2; i <= n; i++) { n     if (primes[i] == 1) { n       for (long long j = i * i; j <= n; j += i) primes[j] = 0; n     } n   } n } n set<int> mm; n int main() { n   scanf( %d%d , &n, &x); n   for (int i = 0; i < n; i++) scanf( %d , &an[i]), mm.insert(an[i]); n   int k = 1299721; n   seive(k); n   if (x == 2) n     printf( %d n , 0); n   else if (*mm.begin() == 1) n     printf( %d n , 1); n   else { n     if (x - 1 > 1299721) n       printf( %d n , -1); n     else { n       vector<int> pp; n       for (int i = 2; i <= x - 1; i++) { n         if (primes[i] == 1) pp.push_back(i); n       } n       bool ok = 1; n       for (int i = 0; i < pp.size(); i++) { n         auto ite = mm.find(pp[i]); n         if (ite == mm.end()) ok = 0; n       } n       if (!ok) n         printf( %d n , -1); n       else n         printf( %d n , pp.size()); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <class T> n inline T lcm(T a, T b) { n   return a / gcd(a, b) * b; n } n long long read_ll() { n   char c = getchar(); n   while ((c <  0  || c >  9 ) && c !=  - ) c = getchar(); n   long long ret = 0; n   int neg = 0; n   if (c ==  - ) neg = 1, c = getchar(); n   while (c >=  0  && c <=  9 ) { n     ret = 10 * ret + c - 48; n     c = getchar(); n   } n   return neg ? -ret : ret; n } n int main() { n   long long n, i; n   n = read_ll(); n   long long a[n + 2], mini = INT_MAX, maxi = INT_MIN; n   a[0] = INT_MIN; n   a[n + 1] = INT_MIN; n   for (i = 1; i <= n; i++) { n     a[i] = read_ll(); n     mini = min(a[i], mini); n     maxi = max(a[i], maxi); n   } n   long long leftmin[n + 1], rightmin[n + 1], leftmax[n + 1], rightmax[n + 1]; n   memset(leftmax, 0, sizeof(leftmax)); n   memset(rightmin, 0, sizeof(rightmin)); n   memset(rightmax, 0, sizeof(rightmax)); n   memset(leftmin, 0, sizeof(leftmin)); n   stack<long long> st; n   st.push(0); n   for (i = 1; i <= n; i++) { n     while (a[st.top()] >= a[i]) st.pop(); n     leftmin[i] = st.top(); n     st.push(i); n   } n   while (!st.empty()) st.pop(); n   st.push(n + 1); n   for (i = n; i >= 1; i--) { n     while (a[st.top()] > a[i]) st.pop(); n     rightmin[i] = st.top(); n     st.push(i); n   } n   while (!st.empty()) st.pop(); n   a[0] = INT_MAX, a[n + 1] = INT_MAX; n   st.push(0); n   for (i = 1; i <= n; i++) { n     while (a[st.top()] <= a[i]) st.pop(); n     leftmax[i] = st.top(); n     st.push(i); n   } n   while (!st.empty()) st.pop(); n   st.push(n + 1); n   for (i = n; i >= 1; i--) { n     while (a[st.top()] < a[i]) st.pop(); n     rightmax[i] = st.top(); n     st.push(i); n   } n   long long minsum = mini, maxsum = maxi, res = 0; n   for (i = 1; i <= n; i++) { n     long long cnt = (rightmax[i] - i) * (i - leftmax[i]) - n                     (rightmin[i] - i) * (i - leftmin[i]); n     res += cnt * a[i]; n   } n   cout << res; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 2010; n int n, a[N], p[N], pos[N]; n vector<int> c[26], ans; n string s, t; n void add(int x) { n   if (0 < x) { n     ans.push_back(x); n   } n   vector<int> nw; n   for (int i = n - 1; i >= n - x; --i) { n     nw.push_back(p[i]); n   } n   for (int i = n - 1; i >= x; --i) { n     p[i] = p[i - x]; n   } n   for (int i = 0; i < x; ++i) { n     p[i] = nw[i]; n   } n   for (int i = 0; i < n; ++i) { n     pos[p[i]] = i; n   } n } n int main() { n   cin >> n >> s >> t; n   for (int i = 0; i < n; ++i) { n     c[t[i] -  a ].push_back(i); n   } n   for (int i = 0; i < n; ++i) { n     int ind = s[i] -  a ; n     if (c[ind].size() == 0) { n       puts( -1 ); n       return 0; n     } n     p[i] = c[ind].back(); n     c[ind].pop_back(); n     pos[p[i]] = i; n   } n   for (int i = 0; i < n; ++i) { n     add(n - pos[i] - 1); n     add(1); n     add(n); n   } n   cout << ans.size() <<   n ; n   for (int i : ans) cout << i <<    ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int INF = 1000000000; n int main() { n   int n, m; n   cin >> n >> m; n   vector<int> session(n); n   vector<vector<int> > s2d(m); n   for (int i = 0; i < n; i++) { n     cin >> session[i]; n     if (session[i]) s2d[session[i] - 1].push_back(i); n   } n   vector<int> price(m); n   for (int i = 0; i < m; i++) { n     cin >> price[i]; n   } n   int l = 0, r = n, mid, cnt, doned; n   vector<int> pos(m); n   bool res, ares = false; n   while (r - l > 1) { n     mid = (l + r) / 2; n     for (int i = 0; i < m; i++) { n       pos[i] = (int)(upper_bound(s2d[i].begin(), s2d[i].end(), mid) - n                      s2d[i].begin()) - n                1; n       pos[i] = (pos[i] >= 0 ? s2d[i][pos[i]] : -1); n     } n     cnt = 0, doned = 0; n     res = true; n     for (int i = 0; i <= mid && res; i++) { n       if (session[i] && pos[session[i] - 1] == i) { n         cnt -= price[session[i] - 1]; n         doned++; n       } else n         cnt++; n       if (cnt < 0) res = false; n     } n     if (!res || doned < m) { n       l = mid; n     } else { n       r = mid; n       ares = true; n     } n   } n   if (ares) n     cout << r + 1; n   else n     cout << -1; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n; n int f[500000], g[500000], a[500000]; n map<long long, int> mp; n struct bit1 { n   int s[500000]; n   int num; n   void add(int x, int z) { n     for (int i = x; i <= num; i += (i & -i)) s[i] = max(s[i], z); n   } n   int ask(int x) { n     int tmp = 0; n     for (int i = x; i >= 1; i -= (i & -i)) tmp = max(tmp, s[i]); n     return tmp; n   } n   void clear() { memset(s, 0, sizeof(s)); } n } s; n struct bit2 { n   int s[500000]; n   int num; n   void add(int x, int z) { n     for (int i = x; i >= 1; i -= (i & -i)) s[i] = max(s[i], z); n   } n   int ask(int x) { n     int tmp = 0; n     for (int i = x; i <= num; i += (i & -i)) tmp = max(tmp, s[i]); n     return tmp; n   } n   void clear() { memset(s, 0, sizeof(s)); } n } t; n int main() { n   scanf( %d , &n); n   int mx = 0, lis = 0; n   for (int i = 1; i <= n; i++) scanf( %d , &a[i]), mx = max(mx, a[i]); n   s.clear(); n   s.num = mx + 1; n   for (int i = 1; i <= n; i++) { n     f[i] = s.ask(a[i] - 1) + 1; n     s.add(a[i], f[i]); n     lis = max(lis, f[i]); n   } n   t.clear(); n   t.num = mx + 1; n   for (int i = n; i >= 1; i--) { n     g[i] = t.ask(a[i] + 1) + 1; n     t.add(a[i], g[i]); n   } n   for (int i = 1; i <= n; i++) { n     long long tmp = f[i] * (n + 1) + g[i]; n     if (mp.find(tmp) == mp.end()) n       mp[tmp] = 1; n     else n       mp[tmp]++; n   } n   for (int i = 1; i <= n; i++) { n     long long tmp = f[i] * (n + 1) + g[i]; n     if (f[i] + g[i] - 1 < lis) n       printf( 1 ); n     else if (mp[tmp] > 1) n       printf( 2 ); n     else n       printf( 3 ); n   } n   printf(  n ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 1e5 + 256; n const char nxt =   n ; n inline void in(string Q) { n   Q +=  .in ; n   freopen(Q.c_str(),  r , stdin); n } n inline void out(string Q) { n   Q +=  .out ; n   freopen(Q.c_str(),  w , stdout); n } n inline void open_file(string Q) { n   in(Q); n   out(Q); n } n int n, a[111][111]; n int main() { n   if (0) open_file(  ); n   scanf( %d , &n); n   while (n--) { n     int x, y, X, Y; n     scanf( %d , &x); n     scanf( %d , &y); n     scanf( %d , &X); n     scanf( %d , &Y); n     for (int i = x; i <= X; ++i) { n       for (int j = y; j <= Y; ++j) { n         a[i][j]++; n       } n     } n   } n   int ans = 0; n   for (int i = 1; i <= 100; ++i) { n     for (int j = 1; j <= 100; ++j) { n       ans += a[i][j]; n     } n   } n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n char BUFFER[100000 + 5]; n bool readn(int &n) { return scanf( %d , &n) == 1; } n bool readl(long long &n) { return scanf( %I64d , &n) == 1; } n bool readd(double &n) { return scanf( %lf , &n) == 1; } n bool reads(string &s) { n   s =   ; n   int n = scanf( %s , BUFFER); n   if (n == 1) s = BUFFER; n   return n == 1; n } n bool readln(string &s) { n   char *valid = gets(BUFFER); n   if (valid) s = BUFFER; n   return ((bool)valid); n } n int Print(long long a, long long b) { n   cout << a <<     << b; n   return 0; n } n void update(long long &su, long long n, long long mod) { n   su *= n; n   su %= mod; n } n int doit(long long n, long long mod) { n   long long su = 1; n   int i; n   for (i = 1; i < n; i++) { n     su *= 10; n     su %= mod; n   } n   update(su, 3, mod); n   update(su, 3, mod); n   update(su, 5, mod); n   update(su, 2, mod); n   update(su, 3, mod); n   update(su, 3, mod); n   su++; n   su %= mod; n   long long a = 1, b = 1; n   for (i = 0; i < n; i++) b *= 10; n   if (0 == su) return Print(a, b); n   su = mod - su; n   return Print(a + su, b + su); n } n int main() { n   long long a; n   cin >> a; n   doit(18, a); n   printf(  n ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n signed main() { n   long long n; n   cin >> n; n   vector<long long> a(n), b(n); n   for (long long i = 0; i < n; ++i) cin >> a[i]; n   for (long long i = 0; i < n; ++i) cin >> b[i]; n   if (n == 1) { n     if (a[0] == b[0]) n       cout <<  SMALL n  << 0; n     else n       cout <<  IMPOSSIBLE ; n     return 0; n   } n   if (n == 2) { n     vector<pair<long long, bool>> res; n     long long kol = 0; n     while (true) { n       if (b[0] == 0 || b[1] == 0) { n         cout <<  IMPOSSIBLE ; n         return 0; n       } n       if (a == b) break; n       if (a[0] == b[1] && a[1] == b[0]) { n         res.emplace_back(1, false); n         break; n       } n       if (b[0] == b[1]) { n         cout <<  IMPOSSIBLE ; n         return 0; n       } n       if (b[0] > b[1]) { n         res.emplace_back(1, false); n         swap(b[0], b[1]); n       } n       if (a[0] == b[0] && a[1] < b[1] && a[1] % b[0] == b[1] % b[0]) { n         res.emplace_back((b[1] - a[1]) / b[0], true); n         kol += res.back().first; n         break; n       } n       if (a[1] == b[0] && a[0] < b[1] && a[0] % b[0] == b[1] % b[0]) { n         res.emplace_back((b[1] - a[0]) / b[0], true); n         kol += res.back().first; n         res.emplace_back(1, false); n         break; n       } n       kol += b[1] / b[0]; n       res.emplace_back(b[1] / b[0], true); n       b[1] %= b[0]; n     } n     if (kol > 2e5) { n       cout <<  BIG n ; n       cout << kol; n     } else { n       cout <<  SMALL n ; n       long long flex = 0; n       for (auto i : res) flex += i.first; n       cout << flex <<   n ; n       for (long long i = res.size() - 1; i >= 0; --i) { n         for (long long j = 0; j < res[i].first; ++j) { n           if (res[i].second) n             cout <<  P ; n           else n             cout <<  R ; n         } n       } n     } n     return 0; n   } n   vector<bool> ans; n   long long kol = 0; n   while (true) { n     if (a == b) break; n     reverse(b.begin(), b.end()); n     if (a == b) { n       ans.push_back(false); n       break; n     } n     reverse(b.begin(), b.end()); n     bool vozr = false, ub = false, r = false; n     for (long long i = 1; i < n; ++i) { n       if (b[i] > b[i - 1]) n         vozr = true; n       else if (b[i] < b[i - 1]) n         ub = true; n       else n         r = true; n     } n     if (r || (vozr && ub)) { n       cout <<  IMPOSSIBLE ; n       return 0; n     } n     vector<long long> c(n); n     if (ub) { n       ans.push_back(false); n       reverse(b.begin(), b.end()); n     } n     c[0] = b[0]; n     for (long long i = 1; i < n; ++i) { n       c[i] = b[i] - b[i - 1]; n     } n     ans.push_back(true); n     b = c; n     ++kol; n   } n   if (kol > 2e5) { n     cout <<  BIG n  << kol; n   } else { n     cout <<  SMALL n  << ans.size() <<   n ; n     for (long long i = ans.size() - 1; i >= 0; --i) { n       if (ans[i]) n         cout <<  P ; n       else n         cout <<  R ; n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n int n, p[200009], b[200009], ans, u[200009], sum; n void dfs(int v) { n   if (u[v] == 1) { n     ans++; n     return; n   } n   u[v] = 1; n   dfs(p[v]); n } n int main() { n   cin >> n; n   for (int i = 1; i <= n; i++) cin >> p[i]; n   for (int i = 1; i <= n; i++) cin >> b[i]; n   for (int i = 1; i <= n; i++) { n     if (!u[p[i]]) { n       dfs(p[i]); n     } n   } n   if (ans == 1) ans = 0; n   for (int i = 1; i <= n; i++) sum += b[i]; n   if (sum % 2 == 0) ans++; n   cout << ans; n } n


#include <bits/stdc++.h> n using namespace std; n int main(int argc, char const *argv[]) { n   int h1, a1, c1; n   int h2, a2; n   cin >> h1 >> a1 >> c1; n   cin >> h2 >> a2; n   int th1 = h1; n   int th2 = h2; n   int count = 0; n   while (th2 > 0) { n     if (th1 > a2 || th2 - a1 <= 0) { n       count++; n       th2 = th2 - a1; n       th1 = th1 - a2; n     } else { n       count++; n       th1 = th1 + c1; n       th1 = th1 - a2; n     } n   } n   cout << count <<   n ; n   while (h2 > 0) { n     if (h1 > a2 || h2 - a1 <= 0) { n       h2 = h2 - a1; n       h1 = h1 - a2; n       cout <<  STRIKE n ; n     } else { n       h1 = h1 + c1; n       h1 = h1 - a2; n       cout <<  HEAL n ; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int t, i, j, curr, n; n   cin >> curr >> n; n   int arr[curr + 1]; n   int temp; n   arr[0] = 1; n   int count = curr - 1; n   for (i = 1; i <= curr; i++) arr[i] = 0; n   for (i = 0; i < n; i++) { n     cin >> temp; n     if (temp == 1) { n       int a, b; n       cin >> a >> b; n       arr[a] = 1; n       arr[b] = 1; n       count = count - 2; n     } else { n       int a; n       cin >> a; n       arr[a] = 1; n       count = count - 1; n     } n   } n   arr[curr] = 1; n   int min = 0; n   for (i = 1; i < curr; i++) { n     if (arr[i] == 0 && arr[i + 1] == 0) { n       min += 1; n       i++; n       continue; n     } else if (arr[i] == 0) { n       min += 1; n     } n   } n   cout << min <<     << count << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int a[10001]; n int best = 100000000; n int main() { n   int n, m, d; n   scanf( %d %d %d , &n, &m, &d); n   scanf( %d , &a[0]); n   int c = a[0] % d; n   int lll = n * m; n   for (int i = 1; i < lll; i++) { n     scanf( %d , &a[i]); n     if (a[i] % d != c) { n       cout << -1 << endl; n       return 0; n     } n   } n   for (int i = 0; i < lll; i++) { n     int ans = 0; n     int lo = a[i]; n     for (int j = 0; j < lll; j++) { n       ans += abs(a[j] - lo) / d; n     } n     best = min(ans, best); n   } n   cout << best << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, m; n   cin >> n >> m; n   pair<int, int> s[n + 1]; n   for (int a = 0; a < n; a++) { n     int x, y; n     cin >> x >> y; n     s[a] = make_pair(max(x - y, 1), min(x + y, m)); n   } n   sort(s, s + n); n   int dp[m + 1]; n   dp[m] = 0; n   for (int a = m - 1; a >= 0; a--) { n     bool cover = 0; n     int minima = m - a; n     for (int b = 0; b < n; b++) { n       if (s[b].first <= (a + 1) && s[b].second >= (a + 1)) { n         cover = 1; n         break; n       } else if (s[b].first > (a + 1)) { n         minima = min(minima, dp[min(m, s[b].second + s[b].first - (a + 1))] + n                                  s[b].first - (a + 1)); n       } n     } n     if (cover) { n       dp[a] = dp[a + 1]; n     } else n       dp[a] = minima; n   } n   cout << dp[0]; n } n


#include <bits/stdc++.h> n using namespace std; n mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count()); n const int N = 3e5; n int n, a[N + 1], b[N + 1], c[N + 1]; n int dp[N + 1]; n vector<int> g1[N + 1], g2[N + 1]; n signed main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cin >> n; n   for (int i = 1; i <= n; ++i) { n     cin >> a[i]; n   } n   vector<pair<int, int> > st; n   for (int i = 1; i <= n; ++i) { n     while (!st.empty() && st.back().first < a[i]) { n       st.pop_back(); n     } n     if (!st.empty()) { n       b[i] = st.back().second; n     } n     st.emplace_back(a[i], i); n   } n   st.clear(); n   for (int i = 1; i <= n; ++i) { n     while (!st.empty() && st.back().first > a[i]) { n       st.pop_back(); n     } n     if (!st.empty()) { n       c[i] = st.back().second; n     } n     st.emplace_back(a[i], i); n   } n   st.clear(); n   for (int i = n; i >= 1; --i) { n     while (!st.empty() && st.back().first < a[i]) { n       st.pop_back(); n     } n     if (!st.empty()) { n       g1[st.back().second].emplace_back(i); n     } n     st.emplace_back(a[i], i); n   } n   st.clear(); n   for (int i = n; i >= 1; --i) { n     while (!st.empty() && st.back().first > a[i]) { n       st.pop_back(); n     } n     if (!st.empty()) { n       g2[st.back().second].emplace_back(i); n     } n     st.emplace_back(a[i], i); n   } n   dp[1] = 0; n   for (int i = 2; i <= n; ++i) { n     dp[i] = dp[i - 1] + 1; n     if (b[i]) dp[i] = min(dp[i], dp[b[i]] + 1); n     if (c[i]) dp[i] = min(dp[i], dp[c[i]] + 1); n     for (int x : g1[i]) { n       if (x >= b[i]) { n         dp[i] = min(dp[i], dp[x] + 1); n       } n     } n     for (int x : g2[i]) { n       if (x >= c[i]) { n         dp[i] = min(dp[i], dp[x] + 1); n       } n     } n   } n   cout << dp[n] <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int dx[] = {-1, 0, 0, 1}; n int dy[] = {0, -1, 1, 0}; n int dx1[] = {-1, -1, -1, 0, 0, 0, 1, 1, 1}; n int dy1[] = {-1, 0, 1, -1, 0, 1, -1, 0, 1}; n const double eps = 1e-9; n const int inf = 2000000000; n const long long int infLL = 9000000000000000000; n const long long int MOD = 1e+7; n const double PI = 3.141592653589793238462643383279; n int main() { n   ios_base ::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   int n, m; n   cin >> n >> m; n   int ans = 2 * n - m + 1; n   for (long long int i = 0; i < m; i++) { n     int k; n     cin >> k; n     for (long long int j = 0; j < k; j++) { n       int a; n       cin >> a; n       if (a == j + 1) ans -= 2; n     } n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void __print(int x) { cout << x; } n void __print(long x) { cout << x; } n void __print(long long x) { cout << x; } n void __print(unsigned x) { cout << x; } n void __print(unsigned long x) { cout << x; } n void __print(unsigned long long x) { cout << x; } n void __print(float x) { cout << x; } n void __print(double x) { cout << x; } n void __print(long double x) { cout << x; } n void __print(char x) { cout <<      << x <<     ; } n void __print(const char *x) { cout <<     << x <<    ; } n void __print(const string &x) { cout <<     << x <<    ; } n void __print(bool x) { cout << (x ?  true  :  false ); } n template <typename T, typename V> n void __print(const pair<T, V> &x) { n   __print(x.first); n   cout <<  , ; n   __print(x.second); n } n template <typename T> n void __print(const T &x) { n   int f = 0; n   for (auto &i : x) cout << (f++ ?  ,  :   ), __print(i); n } n void _print() { cout <<  ] n ; } n template <typename T, typename... V> n void _print(T t, V... v) { n   __print(t); n   if (sizeof...(v)) cout <<  ,  ; n   _print(v...); n } n const int N = 2e5 + 7; n const int MOD = 1e9 + 7; n long long fastpow(long long b, long long p) { n   long long temp; n   if (p == 0) return 1; n   if (p == 1) return b; n   temp = fastpow(b, p / 2); n   temp = temp * temp % MOD; n   if (p & 1) { n     temp = temp * b % MOD; n   } n   return temp; n } n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   long long n, k; n   cin >> n >> k; n   string s; n   cin >> s; n   vector<char> v(k); n   for (int i = 0; i < k; i++) cin >> v[i]; n   vector<int> temp(n); n   for (int i = 0; i < n; i++) { n     if (count(v.begin(), v.end(), s[i])) n       temp[i] = 1; n     else n       temp[i] = 0; n   } n   long long ans = 0; n   for (int i = 0; i < n; i++) { n     if (temp[i] == 1) { n       long long j = i; n       while (j < n && temp[j] == 1) j++; n       ans += ((j - i) * (j - i + 1)) / 2; n       i = j; n     } n   } n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int a[101]; n int main() { n   bool f = false; n   int n, counter = 1; n   long long ans = 0; n   cin >> n; n   for (int i = 0; i < n; i++) { n     cin >> a[i]; n     if (a[i] == 0 && f) counter++; n     if (a[i] == 1 && !f) n       f = true, ans = 1; n     else if (a[i] == 1 && f) n       ans *= counter, counter = 1; n   } n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   int tt; n   cin >> tt; n   while (tt--) { n     int n; n     cin >> n; n     vector<int> a(n + 1); n     for (int i = 1; i <= n; i++) { n       cin >> a[i]; n     } n     int lo = 1, hi = n; n     while (a[lo] == lo && lo <= n) lo++; n     while (a[hi] == hi && hi >= 0) hi--; n     if (lo == n + 1 || hi == -1) { n       cout << 0 <<   n ; n       continue; n     } n     int ans = 1; n     for (int i = lo; i <= hi; i++) { n       if (a[i] == i) { n         ans++; n         break; n       } n     } n     cout << ans <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 4e5 + 9; n vector<int> ans[N]; n vector<char> arr; n int k, n, q, a, hash[N][50], ar[N], s; n char c; n string str; n bool flag; n int main() { n   cin >> k; n   cin >> str; n   int n = str.size(); n   for (int i = 1; i <= k; i++) n     for (int j = 0; j < n; j++) arr.push_back(str[j]); n   for (int i = 0; i < arr.size(); i++) ans[arr[i]].push_back(i); n   cin >> q; n   for (int i = 1; i <= q; i++) { n     cin >> a >> c; n     arr[ans[c][a - 1]] =  0 ; n     ans[c].erase(ans[c].begin() + a - 1); n   } n   for (int i = 0; i < arr.size(); i++) n     if (arr[i] !=  0 ) cout << arr[i]; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = (int)1e5 + 7; n const long long INF = (long long)1e18; n int n; n int m; n vector<pair<int, int>> g[N]; n long long dist[N]; n int par[N]; n int t[N]; n int root(int x) { n   if (t[x] == x) { n     return x; n   } else { n     return t[x] = root(t[x]); n   } n } n void unite(int x, int y) { t[root(x)] = root(y); } n struct T { n   int x; n   long long d; n }; n bool operator<(T a, T b) { return a.d > b.d; } n struct edge { n   int x; n   int y; n   long long d; n }; n bool operator<(edge a, edge b) { return a.d < b.d; } n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cin >> n >> m; n   for (int i = 1; i <= m; i++) { n     int x, y, z; n     cin >> x >> y >> z; n     g[x].push_back({y, z}); n     g[y].push_back({x, z}); n   } n   for (int i = 1; i <= n; i++) { n     t[i] = i; n     dist[i] = INF; n   } n   priority_queue<T> pq; n   int cnt; n   cin >> cnt; n   for (int i = 1; i <= cnt; i++) { n     int portal; n     cin >> portal; n     dist[portal] = 0; n     par[portal] = portal; n     pq.push({portal, 0}); n   } n   while (!pq.empty()) { n     int x = pq.top().x; n     long long d = pq.top().d; n     pq.pop(); n     if (d != dist[x]) { n       continue; n     } n     for (auto &it : g[x]) { n       int y = it.first; n       long long dy = dist[x] + it.second; n       if (dy < dist[y]) { n         dist[y] = dy; n         par[y] = par[x]; n         pq.push({y, dy}); n       } n     } n   } n   vector<edge> e; n   for (int i = 1; i <= n; i++) { n     for (auto &it : g[i]) { n       int j = it.first; n       int c = it.second; n       if (par[i] != par[j]) { n         e.push_back({par[i], par[j], c + dist[i] + dist[j]}); n       } n     } n   } n   long long sol = dist[1]; n   sort(e.begin(), e.end()); n   for (auto &it : e) { n     if (root(it.x) != root(it.y)) { n       unite(it.x, it.y); n       sol += it.d; n     } n   } n   cout << sol <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n const long long mod = 1e9 + 9; n using namespace std; n const long long N = 105; n long long vis[N], l[N], who[N], n, m; n void fail() { n   cout <<  -1 n ; n   exit(0); n } n int32_t main() { n   cin >> n >> m; n   for (long long i = 1; i <= m; i++) cin >> l[i]; n   long long cur = l[1]; n   for (long long i = 2; i <= m; i++) { n     long long dif = (l[i] - cur); n     if (dif <= 0) dif += n; n     if (vis[dif] && vis[dif] != cur) fail(); n     who[cur] = dif; n     vis[dif] = cur; n     cur = l[i]; n   } n   long long cnt = 0; n   set<long long> st; n   for (long long i = 1; i <= n; i++) { n     if (!vis[i]) { n       st.insert(i); n     } n     if (!who[i]) cnt++; n   } n   auto it = st.begin(); n   if (cnt > st.size()) fail(); n   for (long long i = 1; i <= n; i++) { n     if (who[i]) n       cout << who[i]; n     else { n       cout << *it; n       it++; n     } n     cout <<    ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 100086; n int z[maxn], n, m, num, order[maxn][2], last[maxn], ans[maxn], len, in[maxn]; n bool flag, ok[maxn]; n char s[2]; n int main() { n   scanf( %d %d , &n, &m); n   for (int i = 1; i <= m; i++) { n     scanf( %s , s); n     scanf( %d , &num); n     if (s[0] ==  - ) { n       if (z[num] == 0) z[num] = -1; n       order[i][0] = -1; n       order[i][1] = num; n     } else { n       if (z[num] == 0) z[num] = 1; n       order[i][0] = 1; n       order[i][1] = num; n     } n   } n   num = 0; n   for (int i = 1; i <= n; i++) { n     if (z[i] == 0) n       ans[++len] = i; n     else { n       if (z[i] == -1) { n         in[i] = 1; n         num++; n       } n       ok[i] = 1; n     } n   } n   for (int i = 1; i <= m; i++) { n     if (order[i][0] == -1) { n       num--; n       in[order[i][1]] = 0; n       if (num) ok[order[i][1]] = 0; n       last[order[i][1]] = i; n       if ((i != m) && (order[i + 1][1] != order[i][1])) ok[order[i][1]] = 0; n     } else { n       in[order[i][1]] = 1; n       if (num) ok[order[i][1]] = 0; n       if (last[order[i][1]] != i - 1) ok[order[i][1]] = 0; n       num++; n       last[order[i][1]] = i; n     } n   } n   for (int i = 1; i <= n; i++) n     if (ok[i]) ans[++len] = i; n   sort(ans + 1, ans + 1 + len); n   printf( %d n , len); n   if (len) { n     for (int i = 1; i < len; i++) printf( %d  , ans[i]); n     printf( %d , ans[len]); n     printf(  n ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<int> g[1002]; n bool vis[1002]; n bool my[1002], chen[1002]; n int dfs(int n) { n   if (my[n] == 1) return n; n   vis[n] = 1; n   for (int i = 0; i < g[n].size(); i++) { n     int v = g[n][i]; n     if (!vis[v]) { n       int ans = dfs(v); n       if (ans != -1) return ans; n     } n   } n   return -1; n } n void make() { n   memset((vis), 0, sizeof(vis)); n   memset((my), 0, sizeof(my)); n   memset((chen), 0, sizeof(chen)); n   for (int i = 0; i < 1002; i++) g[i].clear(); n } n int main() { n   int t, i, j; n   cin >> t; n   while (t--) { n     make(); n     int n, k1, k2; n     cin >> n; n     for (i = 0; i < n - 1; i++) { n       int u, v; n       cin >> u >> v; n       g[u].push_back(v); n       g[v].push_back(u); n     } n     cin >> k1; n     for (i = 0; i < k1; i++) { n       int u; n       cin >> u; n       my[u] = 1; n     } n     cin >> k2; n     for (i = 0; i < k2; i++) { n       int u; n       cin >> u; n       chen[u] = 1; n     } n     int q; n     for (i = 1; i <= n; i++) n       if (chen[i] == 1) break; n     cout <<  B   << i << endl; n     cout.flush(); n     cin >> q; n     if (my[q] == 1) { n       cout <<  C   << q << endl; n       cout.flush(); n       continue; n     } n     int ans = dfs(q); n     cout <<  A   << ans << endl; n     cout.flush(); n     cin >> q; n     if (chen[q] == 1) { n       cout <<  C   << ans << endl; n       cout.flush(); n     } else { n       cout <<  C   << -1 << endl; n       cout.flush(); n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long int sasha_chiz, masha_chiz, coconut_price, sasha_buy_co, n       masha_buy_co, sum_buy_co, given_chiz = 0; n   cin >> sasha_chiz >> masha_chiz >> coconut_price; n   sasha_buy_co = sasha_chiz / coconut_price; n   masha_buy_co = masha_chiz / coconut_price; n   if (sasha_chiz % coconut_price != 0 && masha_chiz % coconut_price != 0) { n     sasha_chiz = sasha_chiz % coconut_price; n     masha_chiz = masha_chiz % coconut_price; n     if (sasha_chiz + masha_chiz >= coconut_price) { n       if (sasha_chiz >= masha_chiz) { n         given_chiz = coconut_price - sasha_chiz; n         sasha_buy_co = sasha_buy_co + 1; n       } else { n         given_chiz = coconut_price - masha_chiz; n         masha_buy_co = masha_buy_co + 1; n       } n     } n   } n   sum_buy_co = sasha_buy_co + masha_buy_co; n   cout << sum_buy_co <<     << given_chiz; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long MOD = 1e9 + 7; n string s; n long long nb, nc, ns, push_back, pc, ps, money, cntb, cntc, cnts, l, r, mid, mb, n     ms, mc; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0), cout.tie(0); n   cin >> s >> nb >> ns >> nc >> push_back >> ps >> pc >> money; n   for (size_t i = 0; i < s.length(); i++) { n     if (s[i] ==  B ) n       cntb++; n     else if (s[i] ==  S ) n       cnts++; n     else n       cntc++; n   } n   l = 0; n   r = 1e14; n   while (l <= r) { n     mid = (l + r) / 2; n     mb = max(0ll, (cntb * mid - nb) * push_back); n     ms = max(0ll, (cnts * mid - ns) * ps); n     mc = max(0ll, (cntc * mid - nc) * pc); n     if (mc + mb + ms <= money) n       l = mid + 1; n     else n       r = mid - 1; n   } n   cout << r; n   return 0; n } n


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


#include <bits/stdc++.h> n using namespace std; n const int N = int(1e6) + 50, P = int(1e9) + 7; n long long fac_NESC[N], rev[N], pow_NESC[N], revfac[N], w; n int k; n inline void inc(long long &x, long long y) { x = (x + y) % P; } n inline long long M(long long x, long long y) { return x * y % P; } n inline long long S(long long x) { return x * x % P; } n inline long long fpow(long long x, long long y) { n   long long r(1); n   for (; y; y >>= 1, x = M(x, x)) n     if (y & 1) r = M(r, x); n   return r; n } n int main() { n   ios::sync_with_stdio(false); n   cin >> k >> w; n   rev[1] = 1; n   for (int i = 2; i <= k; ++i) rev[i] = ((-rev[P % i] * (P / i)) % P + P) % P; n   fac_NESC[0] = 1; n   for (int i = 1; i <= k; ++i) fac_NESC[i] = M(fac_NESC[i - 1], i); n   pow_NESC[0] = fpow(k, w); n   for (int i = 1; i <= k + 20; ++i) pow_NESC[i] = M(pow_NESC[i - 1], rev[k]); n   revfac[0] = 1; n   for (int i = 1; i <= k; ++i) revfac[i] = M(revfac[i - 1], rev[i]); n   long long res(0); n   for (int t = 1; t <= k; ++t) { n     if (t <= w - 1) n       inc(res, M(M(M(t, S(fac_NESC[k])), pow_NESC[t + 1]), S(revfac[k - t]))); n     if (t > w - 1) n       inc(res, n           M(M(M(fac_NESC[k], w), fac_NESC[k - t + w - 1]), S(revfac[k - t]))); n     if (t + 1 <= w - 1) n       inc(res, M(M(M(t, S(fac_NESC[k])), pow_NESC[t + 2]), n                  M(revfac[k - t], revfac[k - t - 1]))); n     if (t + 1 > w - 1) n       inc(res, M(M(M(fac_NESC[k], w - 1), fac_NESC[k - t + w - 2]), n                  M(revfac[k - t], revfac[k - t - 1]))); n   } n   cout << res << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void read(long long &x) { n   x = 0; n   char c = getchar(); n   long long f = 1; n   while (c <  0  || c >  9 ) { n     if (c ==  - ) f = -f; n     c = getchar(); n   } n   while (c >=  0  && c <=  9 ) { n     x = (x << 3) + (x << 1) + c -  0 ; n     c = getchar(); n   } n   x *= f; n   return; n } n void print(long long x) { n   if (x < 10) { n     putchar(x +  0 ); n     return; n   } n   print(x / 10); n   putchar(x % 10 +  0 ); n } n void write(long long x) { n   if (x < 0) x = -x, putchar( - ); n   print(x); n } n long long maxt(long long a, long long b) { return a < b ? b : a; } n long long mint(long long a, long long b) { return a < b ? a : b; } n struct node { n   long long v, id; n   node(long long V, long long Id) { v = V, id = Id; } n }; n stack<long long> q, r; n vector<node> R[1000005], G[1000005]; n vector<long long> E[1000005]; n long long n, m, dfn[1000005], low[1000005], cnt, w[1000005], s[1000005], n     bel[1000005], f[1000005], e[1000005], dep[1000005], col; n void tarjan(long long u, long long fa) { n   q.push(u); n   dfn[u] = low[u] = ++cnt; n   for (int i = 0; i < R[u].size(); i++) { n     long long v = R[u][i].v, id = R[u][i].id; n     if (v == fa) continue; n     r.push(id); n     if (!dfn[v]) { n       tarjan(v, u); n       low[u] = mint(low[u], low[v]); n       if (low[v] > dfn[u]) { n         ++col; n         while (r.top() != id) { n           if (w[r.top()]) s[col] = 1; n           r.pop(); n         } n         r.pop(); n         while (q.top() != v) { n           bel[q.top()] = col; n           E[col].push_back(q.top()); n           q.pop(); n         } n         bel[v] = col; n         E[col].push_back(v); n         q.pop(); n       } n     } else n       low[u] = mint(low[u], dfn[v]); n   } n   if (fa == -1) { n     ++col; n     while (!r.empty()) { n       if (w[r.top()]) s[col] = 1; n       r.pop(); n     } n     while (!q.empty()) { n       bel[q.top()] = col; n       E[col].push_back(q.top()); n       q.pop(); n     } n   } n } n void dfs(long long u, long long fa) { n   f[u] = fa; n   dep[u] = dep[fa] + 1; n   for (int i = 0; i < G[u].size(); i++) { n     long long v = G[u][i].v, id = G[u][i].id; n     if (v == fa) continue; n     e[v] = id; n     dfs(v, u); n   } n } n int main() { n   read(n), read(m); n   for (int i = 1; i <= m; i++) { n     long long u, v; n     read(u), read(v), read(w[i]); n     R[u].push_back(node(v, i)); n     R[v].push_back(node(u, i)); n   } n   for (int i = 1; i <= n; i++) n     if (!dfn[i]) tarjan(i, -1); n   for (int i = 1; i <= col; i++) n     for (int j = 0; j < E[i].size(); j++) { n       long long u = E[i][j]; n       for (int k = 0; k < R[u].size(); k++) { n         long long v = R[u][k].v, id = R[u][k].id; n         if (bel[u] != bel[v]) G[bel[u]].push_back(node(bel[v], id)); n       } n     } n   dfs(1, 1); n   long long a, b; n   bool fl = 0; n   read(a), read(b); n   a = bel[a], b = bel[b]; n   if (dep[a] < dep[b]) swap(a, b); n   while (dep[a] > dep[b]) { n     if (w[e[a]] || s[a]) fl = 1; n     a = f[a]; n   } n   while (a != b) { n     if (w[e[a]] || w[e[b]] || s[a] || s[b]) fl = 1; n     a = f[a]; n     b = f[b]; n   } n   if (s[a]) fl = 1; n   if (fl) n     puts( YES ); n   else n     puts( NO ); n } n


#include <bits/stdc++.h> n using namespace std; n int read() { n   int x = 0; n   bool flg = false; n   char ch = getchar(); n   for (; !isdigit(ch); ch = getchar()) n     if (ch ==  - ) flg = true; n   for (; isdigit(ch); ch = getchar()) x = (x << 3) + (x << 1) + (ch ^ 48); n   return flg ? -x : x; n } n int n; n struct P { n   long long x, y; n } p[100010]; n P operator-(const P &x, const P &y) { return P{x.x - y.x, x.y - y.y}; } n long long cross(const P &x, const P &y) { return x.x * y.y - x.y * y.x; } n long long dot(const P &x, const P &y) { return x.x * y.x + x.y * y.y; } n long long dot(const P &x) { return x.x * x.x + x.y * x.y; } n bool solve() { n   n = read(); n   for (int i(1), _i(n); i <= _i; i++) { n     int x = read(), y = read(); n     p[i] = P{x, y}; n   } n   p[n + 1] = p[1]; n   if (n & 1) return 0; n   int m = n >> 1; n   for (int i(1), _i(m); i <= _i; i++) { n     if (cross(p[i + m + 1] - p[i + m], p[i + 1] - p[i])) return 0; n     if (dot(p[i + m + 1] - p[i + m], p[i + 1] - p[i]) >= 0) return 0; n     if (dot(p[i + m + 1] - p[i + m]) != dot(p[i + 1] - p[i])) return 0; n   } n   return 1; n } n int main() { n   for (int T = 1; T--;) puts(solve() ?  yes  :  no ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e5 + 5; n string s; n int pre[N], nxt0[N], now = 100002; n int ans[N]; n int n; n void init() { n   vector<int> v; n   for (int i = n - 1; i >= 1; i--) { n     int p = s[i] -  0 ; n     int pos = upper_bound(v.begin(), v.end(), -p) - v.begin(); n     if (pos == v.size()) { n       v.push_back(-p); n     } else { n       v[pos] = -p; n     } n     ans[i] = v.size(); n   } n } n int main() { n   ios::sync_with_stdio(false); n   cin >> s; n   s =  *  + s; n   n = s.length(); n   init(); n   for (int i = 1; i < n; i++) pre[i] = pre[i - 1] + (s[i] ==  1 ); n   for (int i = n - 1; i >= 1; i--) { n     nxt0[i] = now; n     if (s[i] ==  0 ) now = i; n   } n   for (int i = 1; i <= n; i++) { n     if (s[i] ==  0 ) continue; n     if (s[i - 1] ==  1 ) continue; n     int zuo1 = pre[i - 1], you1 = pre[n - 1] - pre[i], n         you0 = (n - 1 - i) - you1; n     int youmx = ans[nxt0[i]]; n     if (youmx <= you1) { n       s[i] =  0 ; n     } n   } n   for (int i = 1; i < n; i++) cout << s[i]; n } n


#include <bits/stdc++.h> n using namespace std; n using ll = long long; n const int mod = 1e9 + 7; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   int t; n   cin >> t; n   while (t--) { n     int n; n     cin >> n; n     vector<int> a(n); n     for (int &elem : a) cin >> elem; n     vector<vector<int>> dp(n, vector<int>(2, -1)); n     function<int(int, int)> solve = [&](int idx, int turn) -> int { n       if (idx >= n) { n         return 0; n       } n       if (dp[idx][turn] != -1) return dp[idx][turn]; n       int ans = INT_MAX; n       if (turn == 1) { n         if (a[idx] == 1) { n           ans = min(ans, 1 + solve(idx + 1, 0)); n           if (idx + 1 < n && a[idx + 1] == 1) { n             ans = min(ans, 2 + solve(idx + 2, 0)); n           } else { n             ans = min(ans, 1 + solve(idx + 2, 0)); n           } n         } else { n           ans = min(ans, solve(idx + 1, 0)); n           if (idx + 1 < n) n             ans = min(ans, (a[idx + 1] == 1) + solve(idx + 2, 0)); n         } n       } else { n         ans = min(ans, solve(idx + 1, 1)); n         ans = min(ans, solve(idx + 2, 1)); n       } n       return dp[idx][turn] = ans; n     }; n     cout << solve(0, 1) << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   int n, cnt11 = 0, cnt10 = 0, cnt01 = 0; n   string s, t; n   cin >> n >> s >> t; n   for (int i = 0; i < 2 * n; ++i) { n     if (s[i] ==  1  && t[i] ==  1 ) n       ++cnt11; n     else if (s[i] ==  1 ) n       ++cnt10; n     else if (t[i] ==  1 ) n       ++cnt01; n   } n   if (cnt11 & 1) { n     if (cnt10 >= cnt01) n       puts( First ); n     else if (cnt10 + 1 == cnt01 || cnt10 + 2 == cnt01) n       puts( Draw ); n     else n       puts( Second ); n   } else { n     if (cnt10 == cnt01 || cnt10 + 1 == cnt01) n       puts( Draw ); n     else if (cnt01 < cnt10) n       puts( First ); n     else n       puts( Second ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m, k; n int n1, n2, n3; n int c[510]; n int sc[510]; n int pai[100010]; n int type[100010]; n int peso[100010]; n int dp[510][510]; n vector<pair<pair<int, int>, int> > edges; n int find(int i) { return pai[i] = (pai[i] == i) ? i : find(pai[i]); } n void join(int i, int j) { n   i = find(i); n   j = find(j); n   if (i == j) return; n   if (peso[i] < peso[j]) swap(i, j); n   pai[j] = i; n   if (peso[i] == peso[j]) peso[i]++; n } n void init() { n   for (int g = 1; g <= n; g++) { n     pai[g] = g; n     peso[g] = 0; n   } n } n int main() { n   scanf( %d %d %d , &n, &m, &k); n   init(); n   for (int g = 1; g <= k; g++) { n     scanf( %d , &c[g]); n     sc[g] = sc[g - 1] + c[g]; n   } n   for (int g = 0; g < m; g++) { n     scanf( %d %d %d , &n1, &n2, &n3); n     edges.push_back(make_pair(make_pair(n1, n2), n3)); n     if (n3 == 0) join(n1, n2); n   } n   for (int g = 1; g <= k; g++) n     for (int h = sc[g - 1] + 1; h <= sc[g]; h++) type[h] = g; n   bool error = false; n   for (int g = 1; g < n; g++) n     if (type[g] == type[g + 1] && find(g) != find(g + 1)) error = true; n   if (error) { n     printf( No n ); n     return 0; n   } n   printf( Yes n ); n   for (int g = 1; g <= k; g++) n     for (int h = 1; h <= k; h++) dp[g][h] = 1000000010; n   for (int g = 1; g <= k; g++) dp[g][g] = 0; n   for (int g = 0; g < m; g++) { n     int i = edges[g].first.first; n     int j = edges[g].first.second; n     int w = edges[g].second; n     i = type[i]; n     j = type[j]; n     if (i != j) dp[i][j] = dp[j][i] = min(dp[i][j], w); n   } n   for (int p = 1; p <= k; p++) n     for (int q = 1; q <= k; q++) n       for (int r = 1; r <= k; r++) n         dp[q][r] = min(dp[q][r], dp[q][p] + dp[p][r]); n   for (int q = 1; q <= k; q++) { n     for (int p = 1; p <= k; p++) n       printf( %d  , (dp[p][q] == 1000000010) ? -1 : dp[p][q]); n     printf(  n ); n   } n } n


#include <bits/stdc++.h> n using namespace std; n int sum, i, j, a[5][5], n, b[20]; n int check() { n   int res = 0; n   int i, j, nowsum; n   for (i = 0; i < n; i++) n     for (j = 0; j < n; j++) { n       a[i][j] = b[i * n + j]; n     } n   for (i = 0; i < n; i++) { n     nowsum = 0; n     for (j = 0; j < n; j++) nowsum += a[i][j]; n     res += abs(sum - nowsum); n   } n   for (j = 0; j < n; j++) { n     nowsum = 0; n     for (i = 0; i < n; i++) nowsum += a[i][j]; n     res += abs(sum - nowsum); n   } n   nowsum = 0; n   for (i = 0; i < n; i++) nowsum += a[i][i]; n   res += abs(nowsum - sum); n   nowsum = 0; n   for (i = 0; i < n; i++) nowsum += a[i][n - i - 1]; n   res += abs(nowsum - sum); n   return res; n } n int main() { n   srand((unsigned)time(NULL)); n   cin >> n; n   for (i = 0; i < n * n; i++) { n     cin >> b[i]; n     sum += b[i]; n   } n   sum /= n; n   int q, p, i, j; n   q = check(); n   while (q) { n     random_shuffle(b, b + n * n); n     q = check(); n     for (int iter = 0; iter < 1000; iter++) { n       i = (rand() * 257) % (n * n); n       j = (rand() * 37) % (n * n); n       while (j == i) { n         j = (rand() * 37) % (n * n); n       } n       if (i > j) swap(i, j); n       swap(b[i], b[j]); n       p = check(); n       if (p < q) { n         q = p; n       } else { n         swap(b[i], b[j]); n       } n     } n     q = check(); n   } n   cout << sum << endl; n   for (i = 0; i < n; i++) { n     for (j = 0; j < n; j++) cout << a[i][j] <<    ; n     cout << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = int(3002), mod = int(1e9) + 7; n char second[N][N]; n int n, m; n long long dp1[N][N], dp2[N][N], a, b, c, d; n bool used[N][N]; n inline long long dp(int x, int y) { n   if (x <= 0 || y <= 0) return 0; n   if (second[x][y] ==  # ) return 0; n   if (x == 1 && y == 2) return 1; n   if (used[x][y]) return dp1[x][y]; n   used[x][y] = 1; n   return dp1[x][y] = (dp(x - 1, y) + dp(x, y - 1)) % mod; n } n inline long long rec(int x, int y) { n   if (x <= 0 || y <= 0) return 0; n   if (second[x][y] ==  # ) return 0; n   if (x == 2 && y == 1) return 1; n   if (used[x][y]) return dp2[x][y]; n   used[x][y] = 1; n   return dp2[x][y] = (rec(x - 1, y) + rec(x, y - 1)) % mod; n } n int main() { n   scanf( %d%d n , &n, &m); n   for (int i = 1; i <= n; i++) { n     scanf( %s n , second[i] + 1); n   } n   a = rec(n, m - 1), c = rec(n - 1, m); n   memset(used, 0, sizeof(used)); n   b = dp(n - 1, m), d = dp(n, m - 1); n   a = a * b % mod; n   c = c * d % mod; n   a = (a - c) % mod; n   if (a < 0) a += mod; n   printf( %d , a); n   return 0; n } n