#include <bits/stdc++.h> n using namespace std; n const int mod = 1000000007; n inline int add(int a, int b) { n   if ((a += b) >= mod) a -= mod; n   return a; n } n inline int dec(int a, int b) { n   if ((a -= b) < 0) a += mod; n   return a; n } n inline void _add(int& a, int b) { n   if ((a += b) >= mod) a -= mod; n } n inline void _dec(int& a, int b) { n   if ((a -= b) < 0) a += mod; n } n inline int mult(int a, int b) { n   long long t = 1ll * a * b; n   if (t >= mod) t %= mod; n   return t; n } n const int L = 16; n char s[510]; n int n, nodecnt = 0, f[170][65536], f2[170][65536]; n void fwt_or(int* A, int flag) { n   if (flag == 1) n     for (int i = 1; i < 65536; i <<= 1) n       for (int j = 0; j < 65536; j += (i << 1)) n         for (int k = 0; k < i; k++) _add(A[i + j + k], A[j + k]); n   else n     for (int i = 1; i < 65536; i <<= 1) n       for (int j = 0; j < 65536; j += (i << 1)) n         for (int k = 0; k < i; k++) _dec(A[i + j + k], A[j + k]); n } n void fwt_and(int* A, int flag) { n   if (flag == 1) n     for (int i = 1; i < 65536; i <<= 1) n       for (int j = 0; j < 65536; j += (i << 1)) n         for (int k = 0; k < i; k++) _add(A[j + k], A[i + j + k]); n   else n     for (int i = 1; i < 65536; i <<= 1) n       for (int j = 0; j < 65536; j += (i << 1)) n         for (int k = 0; k < i; k++) _dec(A[j + k], A[i + j + k]); n } n int dfs(int l, int r) { n   int p = ++nodecnt; n   if (l == r) { n     if (s[l] ==  ? ) { n       for (int t = 0; t < 4; t++) { n         int S = 0; n         for (int i = 0; i < 16; i++) n           if ((i >> t) & 1) S |= (1 << i); n         f[p][S]++; n         f[p][65535 ^ S]++; n       } n     } else { n       if (s[l] >=  A  && s[l] <=  D ) { n         int t = s[l] -  A , S = 0; n         for (int i = 0; i < 16; i++) n           if ((i >> t) & 1) S |= (1 << i); n         f[p][S] = 1; n       } else { n         int t = s[l] -  a , S = 0; n         for (int i = 0; i < 16; i++) n           if ((i >> t) & 1) S |= (1 << i); n         f[p][65535 ^ S] = 1; n       } n     } n     memcpy(f2[p], f[p], sizeof(f[p])); n     return p; n   } n   int cnt = 0, pos = 0; n   for (int i = l; i <= r; i++) { n     if (s[i] ==  ( ) n       cnt++; n     else if (s[i] ==  ) ) n       cnt--; n     if (!cnt) { n       pos = i + 1; n       break; n     } n   } n   int lc = dfs(l + 1, pos - 2), rc = dfs(pos + 2, r - 1); n   if (s[pos] !=  | ) { n     fwt_and(f[lc], 1); n     fwt_and(f[rc], 1); n     for (int i = 0; i < 65536; i++) f[p][i] = mult(f[lc][i], f[rc][i]); n     fwt_and(f[p], -1); n   } n   if (s[pos] !=  & ) { n     fwt_or(f2[lc], 1); n     fwt_or(f2[rc], 1); n     for (int i = 0; i < 65536; i++) f2[p][i] = mult(f2[lc][i], f2[rc][i]); n     fwt_or(f2[p], -1); n   } n   for (int i = 0; i < 65536; i++) _add(f[p][i], f2[p][i]); n   memcpy(f2[p], f[p], sizeof(f[p])); n   return p; n } n int m, fs[16]; n int main() { n   scanf( %s , s + 1); n   n = strlen(s + 1); n   dfs(1, n); n   scanf( %d , &m); n   memset(fs, 255, sizeof(fs)); n   for (int i = 1, a, b, c, d, e; i <= m; i++) { n     scanf( %d%d%d%d%d , &a, &b, &c, &d, &e); n     fs[a | (b << 1) | (c << 2) | (d << 3)] = e; n   } n   int ans = 0; n   for (int i = 0; i < 65536; i++) { n     bool s = 1; n     for (int j = 0; j < 16; j++) s &= (fs[j] == -1 || fs[j] == ((i >> j) & 1)); n     if (s) ans = add(ans, f[1][i]); n   } n   printf( %d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long mod = (long long)(1e9 + 7); n const long long inv = (long long)(5e8 + 4); n int main() { n   long long n, m; n   cin >> n >> m; n   long long ans = (n % mod) * (m % mod) % mod; n   long long l = 1, r = 1; n   while (l <= min(n, m)) { n     long long a = n / l; n     r = min(m, n / a); n     long long x1, x2, x3, x4; n     x1 = (n / r) % mod; n     x2 = (l + r) % mod; n     x3 = (x1 * x2) % mod; n     x4 = (r - l + 1) % mod; n     long long temp = ((x3 * x4) % mod) * inv % mod; n     ans = ((ans + mod) - temp) % mod; n     l = r + 1; n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long Mx = 512; n long long D[Mx][Mx]; n int32_t main() { n   long long n, m, mod, a = 0, b = 0; n   cin >> n >> m >> mod; n   vector<long long> C(n); n   for (long long i = 0; i < m; i++) n     for (long long j = 0; j < n; j++) { n       char c; n       cin >> c; n       if (c ==  1 ) C[j]++; n     } n   for (long long i = 0; i < n; i++) n     if (C[i] == 0) n       a++; n     else if (C[i] == 1) n       b++; n   D[0][0] = 1; n   for (long long d = 1; d <= a + b; d++) { n     for (long long i = 0; i <= d; i++) { n       long long j = d - i; n       D[i][j] = n           (((i >= 2) ? ((i * (i - 1) / 2 % mod) * D[i - 2][j + 2] % mod) : 0) + n            ((i >= 1) ? ((i * j % mod) * D[i - 1][j] % mod) : 0) + n            ((j >= 2) ? ((j * (j - 1) / 2 % mod) * D[i][j - 2] % mod) : 0)) % n           mod; n     } n   } n   cout << D[a][b]; n } n


#include <bits/stdc++.h> n int n, a[200005]; n bool check1(int x) { n   for (int i = x + 1; i <= n; i++) n     if (a[i] >= a[i - 1]) return 0; n   return 1; n } n bool check2(int x) { n   for (int i = x + 1; i <= n; i++) n     if (a[i] <= a[i - 1]) return 0; n   return 1; n } n int main() { n   scanf( %d , &n); n   for (int i = 1; i <= n; i++) scanf( %d , &a[i]); n   if (check1(1) || check2(1)) { n     printf( YES ); n     return 0; n   } n   for (int i = 2; i <= n; i++) { n     if (a[i] <= a[i - 1]) { n       if (check1(i)) n         printf( YES ); n       else n         printf( NO ); n       return 0; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, a[105], p[105]; n long long g[105]; n long long d[105][105][105]; n bool ok[105][105]; n char c[105]; n void read(int& x) { n   char c = getchar(); n   x = 0; n   while (!isdigit(c)) c = getchar(); n   while (isdigit(c)) { n     x = x * 10 + c -  0 ; n     c = getchar(); n   } n } n long long dp(int l, int r, int k) { n   if (ok[l][r]) return d[l][r][k] = g[r - l + k + 1]; n   if (d[l][r][k]) return d[l][r][k]; n   for (int i = l; i < r; i++) { n     d[l][r][k] = max(d[l][r][k], dp(l, i, 0) + dp(i + 1, r, k)); n   } n   for (int i = l; i < p[r]; i++) { n     if (c[i] == c[r] && c[i + 1] != c[r]) { n       d[l][r][k] = n           max(d[l][r][k], dp(l, i, k + r - p[r] + 1) + dp(i + 1, p[r] - 1, 0)); n     } n   } n   return d[l][r][k]; n } n int main() { n   read(n); n   scanf( %s , c + 1); n   for (int i = 1; i <= n; i++) { n     read(a[i]); n     g[i] = a[i]; n   } n   for (int i = 1; i <= n; i++) { n     for (int j = i; j >= 1; j--) { n       if (c[j] == c[i]) { n         p[i] = j; n         ok[j][i] = 1; n       } else n         break; n     } n   } n   for (int i = 2; i <= n; i++) n     for (int j = 1; j < i; j++) g[i] = max(g[i], g[j] + a[i - j]); n   printf( %lld n , dp(1, n, 0)); n } n


#include <bits/stdc++.h> n using namespace std; n const long long M = 1e9 + 7; n const long long INF = LLONG_MAX / 2; n const long long N = 2e5 + 1; n struct event { n   long long sa; n   long long ea; n   long long sb; n   long long eb; n }; n signed main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   { n     long long n; n     cin >> n; n     vector<event> a(n); n     for (long long i = 0; i < n; i++) n       cin >> a[i].sa >> a[i].ea >> a[i].sb >> a[i].eb; n     vector<pair<double, double> > p; n     multiset<pair<double, double> > s1; n     multiset<pair<double, double> > s2; n     bool ans = 1; n     for (long long i = 0; i < n; i++) { n       p.push_back({a[i].sa, i}); n       p.push_back({a[i].ea + 0.5, i}); n     } n     sort(p.begin(), p.end()); n     for (auto i : p) { n       pair<double, double> c = {a[i.second].sb, a[i.second].eb}; n       if (s1.count(c)) { n         s1.erase(s1.find(c)); n         s2.erase(s2.find({c.second, c.first})); n       } else { n         if (s1.empty() || (*s1.rbegin()).first <= c.second && n                               (*s2.begin()).first >= c.first) { n           s1.insert(c); n           s2.insert({c.second, c.first}); n         } else { n           ans = 0; n           break; n         } n       } n     } n     s1.clear(); n     s2.clear(); n     p = vector<pair<double, double> >(0); n     for (long long i = 0; i < n; i++) { n       p.push_back({a[i].sb, i}); n       p.push_back({a[i].eb + 0.5, i}); n     } n     sort(p.begin(), p.end()); n     for (auto i : p) { n       pair<double, double> c = {a[i.second].sa, a[i.second].ea}; n       if (s1.count(c)) { n         s1.erase(s1.find(c)); n         s2.erase(s2.find({c.second, c.first})); n       } else { n         if (s1.empty() || (*s1.rbegin()).first <= c.second && n                               (*s2.begin()).first >= c.first) { n           s1.insert(c); n           s2.insert({c.second, c.first}); n         } else { n           ans = 0; n           break; n         } n       } n     } n     if (ans) n       cout <<  YES ; n     else n       cout <<  NO ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long n, arr[300010], lft[300010], rgt[300010], mx = 0, cnt = 0; n stack<long long> s1, s2; n set<long long> st; n int main() { n   cin >> n; n   for (int i = 0; i < n; i++) cin >> arr[i]; n   for (int i = 0; i < n; i++) lft[i] = -1, rgt[i] = n; n   for (int i = 0; i < n; i++) { n     while (!s1.empty() && (arr[i] % arr[s1.top()])) rgt[s1.top()] = i, s1.pop(); n     s1.push(i); n   } n   for (int i = n - 1; i >= 0; i--) { n     while (!s2.empty() && (arr[i] % arr[s2.top()])) lft[s2.top()] = i, s2.pop(); n     s2.push(i); n   } n   for (int i = 0; i < n; i++) mx = max(mx, rgt[i] - lft[i] - 2); n   for (int i = 0; i < n; i++) n     if (rgt[i] - lft[i] - 2 == mx) st.insert(lft[i] + 2); n   cout << st.size() <<     << mx <<   n ; n   for (auto u : st) cout << u <<    ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int l, r; n   scanf( %d%d , &l, &r); n   if (l == r && l % 2) n     printf( %d , l); n   else n     printf( %d , 2); n } n


#include <bits/stdc++.h> n using namespace std; n const long long C = 1000000007; n long long pow1(long long x, long long y, long long z) { n   if (y == 0) { n     return 1; n   } n   if (y % 2 == 0) { n     return pow1(x * x % z, y / 2, z); n   } n   return pow1(x, y - 1, z) * x % z; n } n signed main() { n   ios::sync_with_stdio(false); n   cin.tie(nullptr); n   cout.tie(nullptr); n   long long n, k, t = 0, r = 0; n   cin >> n >> k; n   for (long long q = 0; q < n; q++) { n     long long w; n     cin >> w; n     if (w == 50) { n       t++; n     } else { n       r++; n     } n   } n   long long c[51][51]; n   for (long long q = 0; q <= 50; q++) { n     c[0][q] = 1; n     for (long long q1 = 1; q1 <= q; q1++) { n       c[q1][q] = c[q1 - 1][q] * (q - q1 + 1) % C * pow1(q1, C - 2, C) % C; n     } n   } n   long long d[5 * n + 2][t + 1][r + 1]; n   for (long long q = 0; q < 5 * n + 2; q++) { n     for (long long q1 = 0; q1 <= t; q1++) { n       fill(d[q][q1], d[q][q1] + r + 1, 0); n     } n   } n   d[0][0][0] = 1; n   bool flag = true; n   for (long long q = 0; q <= 5 * n; q++) { n     if (q % 2 == 1) { n       for (long long q1 = 0; q1 <= t; q1++) { n         for (long long q2 = 0; q2 <= r; q2++) { n           d[q][q1][q2] %= C; n           for (long long q3 = 0; q3 <= q1; q3++) { n             for (long long q4 = 0; q4 <= q2; q4++) { n               if ((q1 - q3) * 50 + (q2 - q4) * 100 <= k && q1 - q3 + q2 - q4) { n                 d[q + 1][q3][q4] += n                     d[q][q1][q2] * c[q1 - q3][q1] % C * c[q2 - q4][q2] % C; n               } n             } n           } n         } n       } n     } else { n       for (long long q1 = 0; q1 <= t; q1++) { n         for (long long q2 = 0; q2 <= r; q2++) { n           d[q][q1][q2] %= C; n           for (long long q3 = q1; q3 <= t; q3++) { n             for (long long q4 = q2; q4 <= r; q4++) { n               if ((q3 - q1) * 50 + (q4 - q2) * 100 <= k && q3 - q1 + q4 - q2) { n                 d[q + 1][q3][q4] += d[q][q1][q2] * c[q3 - q1][t - q1] % C * n                                     c[q4 - q2][r - q2] % C; n               } n             } n           } n         } n       } n     } n     if (d[q][t][r] > 0) { n       cout << q <<   n  << d[q][t][r] <<   n ; n       flag = false; n       break; n     } n   } n   if (flag) { n     cout << -1 <<   n  << 0 <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void init_code() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n } n int main() { n   init_code(); n   int t; n   cin >> t; n   while (t--) { n     int n; n     cin >> n; n     vector<int> arr(n + 1, 0); n     map<int, int> mp; n     for (int i = 1; i <= n; ++i) { n       cin >> arr[i]; n       mp[arr[i]] += 1; n     } n     int res = 0, shifts = 0; n     for (int i = 1; i <= n; ++i) { n       if (arr[i] > i + shifts) { n         int diff = arr[i] - i - shifts; n         res += diff; n         shifts += diff; n       } n     } n     cout << res <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long int mod = 1000000007; n void __print(long long int x) { cerr << x; } n void __print(float x) { cerr << x; } n void __print(double x) { cerr << x; } n void __print(long double x) { cerr << x; } n void __print(char x) { cerr <<      << x <<     ; } n void __print(const char *x) { cerr <<     << x <<    ; } n void __print(const string &x) { cerr <<     << x <<    ; } n void __print(bool x) { cerr << (x ?  true  :  false ); } n template <typename T, typename V> n void __print(const pair<T, V> &x) { n   cerr <<  { ; n   __print(x.first); n   cerr <<  , ; n   __print(x.second); n   cerr <<  } ; n } n template <typename T> n void __print(const T &x) { n   long long int f = 0; n   cerr <<  { ; n   for (auto &i : x) cerr << (f++ ?  ,  :   ), __print(i); n   cerr <<  } ; n } n void _print() { cerr <<  ] n ; } n template <typename T, typename... V> n void _print(T t, V... v) { n   __print(t); n   if (sizeof...(v)) cerr <<  ,  ; n   _print(v...); n } n void solve() { n   long long int n; n   cin >> n; n   vector<long long int> arr(n); n   for (long long int i = 0; i < n; i++) cin >> arr[i]; n   long long int l = n, r = -1; n   for (long long int i = 0; i < n; i++) { n     if (arr[i] == 1) { n       l = i; n       break; n     } n   } n   for (long long int i = n - 1; n - 1 < -1 ? i < -1 : i > -1; n        n - 1 < -1 ? i += 1 : i -= 1) { n     if (arr[i] == 1) { n       r = i; n       break; n     } n   } n   long long int ans = 0; n   for (long long int i = l; l < r + 1 ? i < r + 1 : i > r + 1; n        l < r + 1 ? i += 1 : i -= 1) { n     if (arr[i] == 0) { n       ans++; n     } n   } n   cout << ans << endl; n } n int32_t main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   long long int t = 1; n   cin >> t; n   while (t--) { n     solve(); n   } n } n


#include <bits/stdc++.h> n using namespace std; n template <class T> n T sqr(T a) { n   return (a) * (a); n } n template <class T> n T abs(T a) { n   return (a < 0) ? -(a) : (a); n } n const double Pi = acos(-1.0); n const double eps = 1e-10; n const int INF = 1000 * 1000 * 1000; n const double phi = 0.5 + sqrt(1.25); n int main() { n   string Less =  z , More =    , tmp =   ; n   int n; n   scanf( %d n , &n); n   for (int i = 0, _n = (n)-1; i <= _n; ++i) { n     getline(cin, tmp); n     Less = min(Less, tmp); n     More = max(More, tmp); n   } n   int i = 0, m = ((int)((Less).size())); n   bool coincide = 1; n   while (i < m && coincide) n     if (Less.substr(0, i + 1) == More.substr(0, i + 1)) n       i++; n     else n       coincide = 0; n   printf( %d n , i); n   return 0; n } n


#include <bits/stdc++.h> n const long long mod = 1e9 + 7; n const long long LL_MAX = 9223372036854775807; n using namespace std; n int a[100005]; n int main() { n   int n; n   scanf( %d , &n); n   for (int i = 0; i < 2 * n; i++) scanf( %d , &a[i]); n   int ans = 0; n   for (int i = 0; i < 2 * n; i++) { n     if (a[i] == 0) { n       continue; n     } n     if (a[i] != a[i + 1]) { n       int t = 0, j; n       for (j = i + 1; j < 2 * n && a[j] != a[i]; j++) { n         if (a[j] != 0) t++; n       } n       a[j] = 0; n       ans += t; n     } else n       i++; n   } n   printf( %d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int record[300000]; n int main() { n   map<pair<int, int>, int> m; n   memset(record, 0, sizeof(record)); n   int n, p; n   scanf( %d%d , &n, &p); n   map<pair<int, int>, int>::iterator iter; n   for (int i = 0; i < n; i++) { n     int a, b; n     scanf( %d%d , &a, &b); n     record[a]++; n     record[b]++; n     if (a > b) n       m[make_pair(b, a)]++; n     else n       m[make_pair(a, b)]++; n   } n   long long ans = 0; n   for (iter = m.begin(); iter != m.end(); iter++) { n     int x = (iter->first).first; n     int y = (iter->first).second; n     if (record[x] + record[y] - iter->second < p && record[x] + record[y] >= p) n       ans--; n   } n   sort(record + 1, record + n + 1); n   int ed = n; n   for (int st = 1; st <= n; st++) { n     while (record[st] + record[ed] >= p && ed > st) ed--; n     ans += min(n - ed, n - st); n   } n   printf( %lld n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long fastpow(int a, int b, int MOD) { n   long long x = 1, y = a; n   while (b > 0) { n     if (b % 2 == 1) { n       x = (x * y) % MOD; n     } n     y = (y * y) % MOD; n     b /= 2; n   } n   return x; n } n long long InverseEuler(int n, int MOD) { return fastpow(n, MOD - 2, MOD); } n long long f[3]; n bool init; n long long C(int n, int r, int MOD) { n   if (!init) { n     init = 1; n     f[0] = 1; n     for (int i = 1; i < 300000; i++) f[i] = (f[i - 1] * i) % MOD; n   } n   return (f[n] * n           ((InverseEuler(f[r], MOD) * InverseEuler(f[n - r], MOD)) % MOD)) % n          MOD; n } n int N; n string s[77000]; n unordered_map<int, int> ct[9]; n unordered_map<int, int> loc[9][77000]; n int main() { n   std::ios::sync_with_stdio(false); n   cin >> N; n   for (int i = 0; i < N; i++) { n     cin >> s[i]; n     int M = s[i].length(); n     for (int j = 0; j < M; j++) { n       int tmp = 0; n       for (int k = j; k < M; k++) { n         tmp = tmp * 10 + (s[i][k] -  0 ); n         ct[k - j][tmp]++; n         loc[k - j][i][tmp]++; n       } n     } n   } n   for (int i = 0; i < N; i++) { n     int M = s[i].length(); n     int ln = 10; n     int res; n     for (int j = 0; j < M; j++) { n       int tmp = 0; n       for (int k = j; k < M; k++) { n         if (k - j >= ln) break; n         tmp = tmp * 10 + (s[i][k] -  0 ); n         if (ct[k - j][tmp] == loc[k - j][i][tmp]) { n           res = tmp; n           ln = k - j; n         } n       } n     } n     string res2 =   ; n     while (res) { n       res2 +=  0  + (res % 10); n       res /= 10; n     } n     reverse(res2.begin(), res2.end()); n     while (res2.size() != ln + 1) res2 =  0  + res2; n     cout << res2 << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n using namespace chrono; n const int maxn = 1e6; n void solve() { n   int n, a[21][21], val; n   vector<int> data; n   map<int, int> m; n   cin >> n; n   for (int i = 0; i < n * n; i++) { n     cin >> val; n     data.push_back(val); n     m[val]++; n   } n   if (n == 1) { n     cout <<  YES  n          <<   n ; n     cout << data[0] <<   n ; n     return; n   } n   for (int ii = 0; ii < n; ii++) { n     for (int jj = 0; jj < n; jj++) { n       a[ii][jj] = 0; n     } n   } n   for (int ii = 0; ii < n / 2; ii++) { n     for (int jj = 0; jj < n / 2; jj++) { n       int v_f = 0, v_s = 0; n       for (auto it = m.begin(); it != m.end(); it++) { n         if (it->second >= 4) { n           v_f = it->first; n           break; n         } n       } n       if (v_f == 0) { n         cout <<  NO  n              <<   n ; n         return; n       } n       a[ii][jj] = v_f; n       a[n - ii - 1][jj] = v_f; n       a[ii][n - jj - 1] = v_f; n       a[n - ii - 1][n - jj - 1] = v_f; n       m[v_f] -= 4; n     } n   } n   if ((n & 1) == 0) { n     cout <<  YES  n          <<   n ; n     for (int ii = 0; ii < n; ii++) { n       for (int jj = 0; jj < n; jj++) { n         cout << a[ii][jj] <<    ; n       } n       cout <<   n ; n     } n     cout <<   n ; n     return; n   } n   for (int ii = 0; ii < n / 2; ii++) { n     int f = 0; n     for (auto it = m.begin(); it != m.end(); it++) { n       if (it->second >= 2) { n         f = it->first; n       } n     } n     if (f == 0) { n       cout <<  NO  n            <<   n ; n       return; n     } n     a[ii][n / 2] = f; n     a[n - ii - 1][n / 2] = f; n     m[f] -= 2; n   } n   for (int ii = 0; ii < n / 2; ii++) { n     int f = 0; n     for (auto it = m.begin(); it != m.end(); it++) { n       if (it->second >= 2) f = it->first; n     } n     if (f == 0) { n       cout <<  NO  n            <<   n ; n       return; n     } n     a[n / 2][ii] = f; n     a[n / 2][n - ii - 1] = f; n     m[f] -= 2; n   } n   int f = 0; n   for (auto it = m.begin(); it != m.end(); it++) { n     if (it->second == 1) { n       f = it->first; n     } n   } n   if ((n & 1) == 1) { n     a[n / 2][n / 2] = f; n   } n   cout <<  YES  n        <<   n ; n   for (int ii = 0; ii < n; ii++) { n     for (int jj = 0; jj < n; jj++) { n       cout << a[ii][jj] <<    ; n     } n     cout <<   n ; n   } n   cout <<   n ; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = (int)(2e6 + 123); n const int R = (int)(2e6); n const long long inf = (long long)(1e12 + 7); n long long pn, p[maxn], lp[maxn], b[maxn]; n bool u[maxn]; n void resheto(long long n) { n   for (long long i = 3; i * i <= n; i += 2) n     if (!u[i]) n       for (long long j = i * i; j <= n; j += 2 * i) u[j] = 1; n   p[pn++] = 2; n   for (long long i = 3; i <= n; i += 2) n     if (!u[i]) p[pn++] = i; n   for (long long i = 0; i < pn; ++i) { n     for (long long j = p[i]; j <= n; j += p[i]) lp[j] = p[i]; n   } n } n long long n, val, x, k, t, ans1; n long long res(long long x) { n   long long ans = x; n   for (long long mask = 1; mask < (1 << n); ++mask) { n     long long cnt = 0, a = 1; n     for (long long i = 0; i < n; ++i) { n       if ((mask >> i) & 1) { n         cnt++; n         a *= b[i]; n       } n     } n     if (cnt & 1) n       ans -= x / a; n     else n       ans += x / a; n   } n   return ans - ans1; n } n long long bin(long long l, long long r, long long k) { n   while (r - l > 1) { n     long long m = (l + r) / 2; n     if (res(m) >= k) n       r = m; n     else n       l = m; n   } n   if (res(l) == k) return l; n   return r; n } n int main() { n   ios_base ::sync_with_stdio(NULL); n   resheto(R); n   cin >> t; n   for (long long cs = 1; cs <= t; ++cs) { n     cin >> x >> val >> k; n     n = ans1 = 0; n     while (val != 1) { n       b[n++] = lp[val]; n       long long y = lp[val]; n       while (val % y == 0) val /= y; n     } n     ans1 = res(x); n     cout << bin(x + 1, inf, k) << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MOD = 1e6 + 3; n const int INFI = 1e9 * 2; n const long long LINFI = 1e17; n const double pi = acos(-1.0); n const int N = 111; n const int M = 11; n const int move[8][2] = {1, 0, -1, 0, 0, 1, 0, -1, 1, 1, 1, -1, -1, 1, -1, -1}; n double mabs(double x) { return x > 0 ? x : -x; } n int main() { n   int n; n   while (scanf( %d , &n) != EOF) { n     if (n == 1) n       printf( a na nb nb n ); n     else { n       if (n & 1) { n         n--; n         for (int i = 0; i < n; i++) putchar((i & 2) ?  a  :  b ); n         puts( c ); n         for (int i = 0; i < n; i++) putchar((i & 2) ?  d  :  e ); n         puts( c ); n         putchar( f ); n         for (int i = 0; i < n; i++) putchar((i & 2) ?  g  :  h ); n         printf(  n ); n         putchar( f ); n         for (int i = 0; i < n; i++) putchar((i & 2) ?  i  :  j ); n         printf(  n ); n       } else { n         for (int i = 0; i < n; i++) putchar((i & 2) ?  a  :  b ); n         printf(  n ); n         for (int i = 0; i < n; i++) putchar((i & 2) ?  c  :  d ); n         printf(  n ); n         n -= 2; n         putchar( e ); n         for (int i = 0; i < n; i++) putchar((i & 2) ?  f  :  g ); n         puts( h ); n         putchar( e ); n         for (int i = 0; i < n; i++) putchar((i & 2) ?  i  :  j ); n         puts( h ); n       } n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, h; n   cin >> n >> h; n   vector<int> a(n, 0); n   copy(istream_iterator<int>(cin), istream_iterator<int>(), a.begin()); n   vector<int> a_orig = a; n   sort(a.begin(), a.end()); n   int max1 = a[n - 1] + a[n - 2]; n   int min1 = a[0] + a[1]; n   int best_sofar = max1 - min1; n   vector<int> res(n, 2); n   if (n >= 3) { n     min1 = min(min1 + h, a[1] + a[2]); n     max1 = max(max1, a[0] + a[n - 1] + h); n     if (abs(max1 - min1) < best_sofar) { n       best_sofar = abs(max1 - min1); n       res[distance(a_orig.begin(), min_element(a_orig.begin(), a_orig.end()))] = n           1; n     } n   } n   cout << best_sofar << endl; n   copy(res.begin(), res.end(), ostream_iterator<int>(cout,    )); n   cout << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long inf = (long long)1e18; n const long long mod = (long long)1e9 + 7; n const double eps = (double)1e-9; n const double pi = acos(-1.0); n const int dx[] = {0, 0, 1, 0, -1}; n const int dy[] = {0, 1, 0, -1, 0}; n const int N = 100500; n void solve() { n   int a, b, cnt = 0; n   string second; n   cin >> a >> b >> second; n   vector<int> v; n   cnt = 0; n   for (int i = 0; i < (int)second.size(); ++i) { n     if (second[i] ==  X ) continue; n     int j = i; n     while (j + 1 < (int)second.size() && second[j + 1] == second[j]) j++; n     int len = j - i + 1; n     if (len < b) continue; n     if (b <= len && len < a) { n       cout <<  NO  << endl; n       return; n     } n     if (a <= len && len < 2 * b) n       cnt++; n     else n       v.push_back(len); n     i = j; n   } n   if (v.size() == 0) { n     if (cnt % 2 == 0) n       cout <<  NO  << endl; n     else n       cout <<  YES  << endl; n     return; n   } n   if (v.size() > 1) { n     cout <<  NO  << endl; n     return; n   } n   int x = v[0]; n   for (int i = 0; i + a <= x; ++i) { n     if ((b <= i && i < a) || (b <= x - a - i && x - a - i < a)) continue; n     if (2 * b <= max(i, x - a - i)) continue; n     int ncnt = cnt; n     if (a <= i && i < 2 * b) ncnt++; n     if (a <= x - a - i && x - a - i < 2 * b) ncnt++; n     if (ncnt % 2 == 0) { n       cout <<  YES  << endl; n       return; n     } n   } n   cout <<  NO  << endl; n } n int main() { n   cin.tie(NULL); n   cout.tie(NULL); n   ios_base::sync_with_stdio(false); n   int T; n   cin >> T; n   while (T--) solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int p = 1000000007; n int n, x, y, g[100005], vg[100005], C, P = 1, a1, a2, a3, b, ans; n vector<int> l[100005], r[100005]; n inline int ksm(int a, int b) { n   int res = 1; n   while (b) { n     if (b & 1) res = (long long)res * a % p; n     a = (long long)a * a % p; n     b >>= 1; n   } n   return res; n } n int head[100005], to[200005], nex[200005], cnt; n inline void adde(int f, int t) { n   to[++cnt] = t; n   nex[cnt] = head[f]; n   head[f] = cnt; n } n int fa[100005], dep[100005], sz[100005], son[100005], seg[100005], top[100005]; n void dfs1(int u, int pre) { n   fa[u] = pre; n   dep[u] = dep[pre] + 1; n   sz[u] = 1; n   for (int i = head[u], v; i; i = nex[i]) { n     v = to[i]; n     if (v == pre) continue; n     dfs1(v, u); n     sz[u] += sz[v]; n     if (sz[v] > sz[son[u]]) son[u] = v; n   } n } n void dfs2(int u) { n   if (son[u]) { n     seg[son[u]] = ++cnt; n     top[son[u]] = top[u]; n     dfs2(son[u]); n   } n   for (int i = head[u], v; i; i = nex[i]) { n     v = to[i]; n     if (top[v]) continue; n     seg[v] = ++cnt; n     top[v] = v; n     dfs2(v); n   } n } n int sum[100005], sum1[100005]; n inline int query(int *a, int x) { n   int res = 0; n   for (; x; x -= x & -x) res = (res + a[x]) % p; n   return res; n } n inline void modify(int *a, int x, int y) { n   for (; x <= n + 1; x += x & -x) a[x] = (a[x] + y) % p; n } n inline int Query(int x) { n   int res = ((query(sum1, 1) - query(sum, 1) * 2) % p + p) % p; n   while (x) { n     res = ((res + (long long)query(sum, seg[x]) * (seg[x] + 1) - n             query(sum1, seg[x])) % n                p + n            p) % n           p; n     res = ((res - (long long)query(sum, seg[top[x]] - 1) * seg[top[x]] + n             query(sum1, seg[top[x]] - 1)) % n                p + n            p) % n           p; n     x = fa[top[x]]; n   } n   return res; n } n inline void Modify(int x, int y) { n   while (x) { n     modify(sum, seg[top[x]], y); n     modify(sum, seg[x] + 1, (p - y) % p); n     modify(sum1, seg[top[x]], (long long)y * seg[top[x]] % p); n     modify(sum1, seg[x] + 1, (p - (long long)y * (seg[x] + 1) % p) % p); n     x = fa[top[x]]; n   } n } n int main() { n   scanf( %d , &n); n   for (int i = 1; i <= n; ++i) { n     scanf( %d%d , &x, &y); n     g[i] = y - x + 1; n     vg[i] = ksm(g[i], p - 2); n     l[x].push_back(i); n     r[y].push_back(i); n     P = (long long)P * g[i] % p; n     C = max(C, y); n   } n   for (int i = 1; i < n; ++i) { n     scanf( %d%d , &x, &y); n     adde(x, y); n     adde(y, x); n   } n   dep[0] = -1; n   dfs1(1, 0); n   seg[1] = top[1] = cnt = 1; n   dfs2(1); n   for (int c = 1; c <= C; ++c) { n     for (unsigned int i = 0; i < l[c].size(); ++i) { n       int x = l[c][i]; n       a1 = (a1 + (long long)dep[x] * vg[x]) % p; n       a2 = (a2 + vg[x]) % p; n       a3 = (a3 + (long long)dep[x] * vg[x] % p * vg[x]) % p; n       b = (b + (long long)Query(x) * vg[x]) % p; n       Modify(x, vg[x]); n     } n     ans = ((ans + (long long)a1 * a2 - a3 - 2 * b) % p + p) % p; n     for (unsigned int i = 0; i < r[c].size(); ++i) { n       int x = r[c][i]; n       a1 = ((a1 - (long long)dep[x] * vg[x]) % p + p) % p; n       a2 = (a2 - vg[x] + p) % p; n       a3 = ((a3 - (long long)dep[x] * vg[x] % p * vg[x]) % p + p) % p; n       Modify(x, p - vg[x]); n       b = ((b - (long long)Query(x) * vg[x]) % p + p) % p; n     } n   } n   ans = (long long)ans * P % p; n   printf( %d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void bringToTen(int &k, int &rating, vector<int> &bucket) { n   for (int i = 9; i > 0; i--) { n     int x = 0; n     while (x < 100) { n       int h = min(k / (10 - i), bucket[x + i]); n       k -= h * (10 - i); n       bucket[x + 10] += h; n       rating += h; n       bucket[x + i] -= h; n       x += 10; n     } n   } n } n void addToTen(int &k, int &rating, vector<int> &bucket) { n   int x = 0; n   while (x < 100) { n     int h = min(k / 10, bucket[x]); n     rating += h; n     bucket[x + 10] += h; n     bucket[x] -= h; n     k -= 10 * h; n     x += 10; n   } n } n int main(void) { n   int n, k, rating = 0, h; n   scanf(  %d %d , &n, &k); n   vector<int> a(n); n   for (int(i) = (0); i < (n); i++) scanf(  %d , &a[i]); n   vector<int> num(101, 0); n   for (int(i) = (0); i < (n); i++) { n     num[a[i]]++; n     rating += a[i] / 10; n   } n   bringToTen(k, rating, num); n   addToTen(k, rating, num); n   printf( %d n , rating); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int arr[200000], arr2[200000]; n int main() { n   int n, i; n   long long sum = 0; n   scanf( %d , &n); n   for (i = 0; i < n; i++) { n     scanf( %d , &arr[i]); n     arr2[i] = arr[i] + arr2[i - 1]; n   } n   for (i = 0; i < n; i++) { n     if (arr[i] == 0) { n       sum += arr2[i]; n     } n   } n   cout << sum; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e6 + 6, M = 3, nBits = 2e8 + 5, MM = (1 << 16), MAX = 1111, n           OO = 0x3f3f3f3f, MOD = 1000000007, inf = 1 << 30; n const double PI = acos(-1); n const long long INF = (long long)1e18, MOOD = 1000000007; n long long GCD(long long a, long long b) { return !b ? a : GCD(b, a % b); } n long long LCM(long long x, long long y) { return (x * y / GCD(x, y)); } n long long fact(long long z) { return (z <= 1) ? 1 : z * fact(z - 1); } n void fast() { n   std::ios_base::sync_with_stdio(0); n   cin.tie(NULL); n   cout.tie(NULL); n } n int t, a, b, color[N], n, m, k, x, y, r[5001], c[5001]; n int main() { n   fast(); n   cin >> n >> m >> k; n   for (int i = 1; i <= k; ++i) { n     cin >> a >> b >> color[i]; n     if (a == 1) n       r[b] = i; n     else n       c[b] = i; n   } n   for (int i = 1; i <= n; ++i) { n     for (int j = 1; j <= m; ++j) cout << color[max(r[i], c[j])] <<    ; n     cout << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<pair<long long int, long long int> > P; n int n; n bool compare_y(const pair<long long int, long long int>& p1, n                const pair<long long int, long long int>& p2) { n   if (p1.second != p2.second) return p1.second < p2.second; n   return p1.first < p2.first; n } n long long int d(pair<long long int, long long int> p1, n                 pair<long long int, long long int> p2) { n   return pow(p1.first - p2.first, 2) + pow(p1.second - p2.second, 2); n } n long long int func(int l, int h, n                    vector<pair<long long int, long long int> > Y) { n   if (h - l == 1) return d(P[l], P[h]); n   if (h - l == 2) n     return min(min(d(P[l], P[l + 1]), d(P[l + 1], P[h])), d(P[l], P[h])); n   int mid = (l + h) / 2, sz = Y.size(); n   vector<pair<long long int, long long int> > left, right; n   for (int i = 0; i < sz; ++i) { n     if (Y[i].first <= P[mid].first) n       left.push_back(Y[i]); n     else n       right.push_back(Y[i]); n   } n   long long int m = min(func(l, mid, left), func(mid + 1, h, right)); n   for (int i = 0; i < sz; ++i) n     for (int j = i + 1; j < i + 10 && j < sz; ++j) n       if (d(Y[i], Y[j]) < m) m = d(Y[i], Y[j]); n   return m; n } n int main() { n   cin >> n; n   vector<pair<long long int, long long int> > Q; n   for (int i = 0; i < n; ++i) { n     long long int u; n     cin >> u; n     P.push_back(make_pair(i + 1, u)); n     Q.push_back(make_pair(i + 1, u)); n     if (i) P[i].second += P[i - 1].second; n     if (i) Q[i].second += Q[i - 1].second; n   } n   sort(Q.begin(), Q.end(), compare_y); n   cout << func(0, n - 1, Q) << endl; n } n


#include <bits/stdc++.h> n using namespace std; n signed main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   long long t = 1; n   cin >> t; n   while (t--) { n     long long n, x; n     cin >> n >> x; n     long long deg[1005] = {}; n     for (long long i = 1; i < n; i++) { n       long long u, v; n       cin >> u >> v; n       deg[u]++, deg[v]++; n     } n     if (n == 1 || deg[x] == 1) n       cout <<  Ayush n ; n     else { n       if (n % 2) n         cout <<  Ashish n ; n       else n         cout <<  Ayush n ; n     } n   } n   cerr <<  Time elapsed:   << 1.0 * clock() / CLOCKS_PER_SEC <<   sec  n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 1005; n int n, g[21][21], a, t[maxn]; n bool ans = true; n int find(int times) { n   int i = 1; n   bool finded = false; n   for (; i <= 1000; ++i) n     if (t[i] >= times) { n       finded = true; n       break; n     } n   if (!finded) ans = false; n   t[i] -= times; n   return i; n } n int main() { n   cin >> n; n   for (int i = 0; i < n * n; i++) { n     cin >> a; n     t[a]++; n   } n   for (int i = 1; i <= n / 2; i++) n     for (int j = 1; j <= n / 2; j++) n       g[i][j] = g[n - i + 1][j] = g[i][n - j + 1] = g[n - i + 1][n - j + 1] = n           find(4); n   if (ans) { n     if (n & 1) { n       for (int i = 1; i <= n / 2; i++) n         if (i != (n + 1) / 2) { n           g[i][(n + 1) / 2] = g[n - i + 1][(n + 1) / 2] = find(2); n           g[(n + 1) / 2][i] = g[(n + 1) / 2][n - i + 1] = find(2); n         } n       g[(n + 1) / 2][(n + 1) / 2] = find(1); n     } n     if (ans) { n       cout <<  YES  << endl; n       for (int i = 1; i <= n; i++) n         for (int j = 1; j <= n; j++) cout << g[i][j] <<    n [j == n]; n     } n   } n   if (!ans) cout <<  NO  << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, d; n int a[200]; n int x[200], y[200]; n int dis[200]; n vector<pair<int, int> > edge[200]; n int bellman_ford() { n   for (int i = 0; i < n; ++i) dis[i] = 1000000000; n   dis[0] = 0; n   for (int i = 1; i < n; ++i) { n     for (int j = 0; j < n; ++j) { n       for (int k = 0, len = edge[j].size(); k < len; ++k) n         dis[edge[j][k].first] = n             min(dis[edge[j][k].first], dis[j] + edge[j][k].second); n     } n   } n   return max(0, dis[n - 1]); n } n int D(int i, int j) { return abs(x[i] - x[j]) + abs(y[i] - y[j]); } n int main() { n   while (cin >> n >> d) { n     a[0] = 0; n     a[n - 1] = 0; n     for (int i = 1; i < n - 1; ++i) cin >> a[i]; n     for (int i = 0; i < n; ++i) { n       cin >> x[i] >> y[i]; n     } n     for (int i = 0; i < n; ++i) n       for (int j = 0; j < n; ++j) n         if (i != j) { n           edge[i].push_back(make_pair(j, D(i, j) * d - a[i])); n         } n     cout << bellman_ford() << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long maxn = 2e5 + 50; n const long long Mod = 1e9 + 7; n struct node { n   long long to, next, w; n } edge[maxn * 2]; n struct Edge { n   long long u, v; n } t[maxn]; n long long k = 0, head[maxn]; n void add(long long u, long long v) { n   edge[++k].to = v; n   edge[k].next = head[u]; n   head[u] = k; n } n long long T, p[maxn], m, n; n long long ans = 0, sz[maxn], cnt[maxn]; n void dfs(long long x, long long f, long long opt) { n   sz[x] = 1; n   for (long long i = head[x]; i; i = edge[i].next) { n     if (edge[i].to == f) continue; n     dfs(edge[i].to, x, i); n     sz[x] += sz[edge[i].to]; n   } n   if (opt == 0) return; n   cnt[opt] = sz[x] * (n - sz[x]); n   if (cnt[opt] == 0 && opt != 0) cnt[opt] = n - 1; n } n int main() { n   cin >> T; n   while (T--) { n     k = 0; n     memset(head, 0, sizeof(head)); n     memset(cnt, 0, sizeof(cnt)); n     cin >> n; n     for (long long i = 1; i <= n - 1; i++) { n       long long u, v; n       cin >> u >> v; n       add(u, v); n       add(v, u); n     } n     cin >> m; n     for (long long i = 1; i <= m; i++) cin >> p[i]; n     sort(p + 1, p + 1 + m); n     long long cnt1 = n - 1 - m; n     if (cnt1 <= 0) cnt1 = 0; n     dfs(1, 1, 0); n     sort(cnt + 1, cnt + 1 + k); n     while (m > n - 1) { n       p[m - 1] = (p[m - 1] * p[m]) % Mod; n       m--; n     } n     long long ans = 0, opt = 0; n     for (long long i = k / 2 + 1; i <= k; i++) { n       if (cnt1) { n         ans = (ans + cnt[i] * 1) % Mod; n         cnt1--; n       } else { n         ans = (ans + cnt[i] * p[++opt] % Mod) % Mod; n       } n     } n     cout << ans << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<pair<int, int>> L[1000005]; n long long fac[1000005]; n const long long MOD = 1000000007; n int main() { n   int N, M; n   scanf( %d%d , &N, &M); n   for (int i = 0; i < N; ++i) { n     int g; n     scanf( %d , &g); n     map<int, int> cnt; n     for (int j = 0; j < g; ++j) { n       int a; n       scanf( %d , &a); n       ++cnt[a]; n     } n     for (auto x : cnt) { n       L[x.first].push_back({i, x.second}); n     } n   } n   map<vector<pair<int, int>>, int> mp; n   for (int i = 1; i <= M; ++i) ++mp[L[i]]; n   fac[0] = 1; n   for (int i = 1; i <= M; ++i) fac[i] = (fac[i - 1] * i) % MOD; n   long long ans = 1; n   for (auto &x : mp) ans = (ans * fac[x.second]) % MOD; n   printf( %I64d n , ans); n } n


#include <bits/stdc++.h> n using namespace std; n long long get_sum(vector<long long> &prefix, int left, int right) { n   if (left != 0) n     return prefix[right] - prefix[left - 1]; n   else n     return prefix[right]; n } n void To_Add_or_Not_To_Add(vector<long long> &a, long long k) { n   int n = a.size(); n   sort(a.begin(), a.end()); n   vector<long long> prefix = a; n   for (int i = 1; i < n; i++) prefix[i] += prefix[i - 1]; n   long ans = 0; n   int maxFreq = INT_MIN; n   for (auto key : a) { n     int freq = upper_bound(a.begin(), a.end(), key) - n                lower_bound(a.begin(), a.end(), key); n     int low = 0; n     int high = lower_bound(a.begin(), a.end(), key) - a.begin(); n     high--; n     long long stock = k; n     while (low <= high) { n       int mid = (low + high) / 2; n       int len = high - mid + 1; n       auto req = len * key - get_sum(prefix, mid, high); n       if (stock >= req) { n         stock -= req; n         high = mid - 1; n         freq += len; n       } else { n         low = mid + 1; n       } n     } n     if (freq > maxFreq) { n       maxFreq = freq; n       ans = key; n     } n   } n   cout << maxFreq <<     << ans << endl; n } n int main() { n   int n; n   cin >> n; n   long long k; n   cin >> k; n   vector<long long> a(n); n   for (auto &ele : a) cin >> ele; n   To_Add_or_Not_To_Add(a, k); n   return 0; n } n


/*   n */ n #include <bits/stdc++.h> n using namespace std; n  n #define rng(x) x.begin(), x.end() n #define pb push_back n #define ff first n #define ss second n  n typedef double db; n typedef long long ll; n typedef pair<int, int> pi; n typedef pair<ll, ll> pl; n typedef vector<int> vi; n typedef vector<ll> vl; n typedef vector<vi> vvi; n typedef vector<vl> vvl; n typedef vector<bool> vb; n typedef unsigned long long int lld; n  n /*-----------------------------Code begins----------------------------------*/ n ll mod = 1e9 + 7; n const ll INF = 1e9; n  n ll inv(ll v) n { n  if (v<=1) n   return v; n  return inv(mod%v)*(mod-mod/v)%mod; n } n  n void solve(){ n     int n; cin>>n; n     vvl dist(n,vl(n,INF)); n     for(int i=1; i<n; i++){ n         int u,v; cin>>u>>v; n         u--; v--; n         dist[u][v]=1; n         dist[v][u]=1; n     } n     for(int i=0; i<n; i++){ n         dist[i][i]=0; 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++){ n                 dist[i][j] = min(dist[i][j], dist[i][k] + dist[k][j]); n             } n         } n     } n     vvl prob(207,vl(207)); n     for(int i=0; i<=n; i++){ n         for(int j=0; j<=n; j++){ n             if(i==0){ n                 prob[i][j]=0; n             } n             else if(j==0){ n                 prob[i][j]=1; n             } n             else{ n                 prob[i][j] = ((prob[i-1][j] + prob[i][j-1])%mod * inv(2) )%mod; n             } n         } n     } n  n     ll ans=0; n     for(int a=0; a<n; a++){ n         for(int b=a+1; b<n; b++){ n             for(int root=0; root<n; root++){ n                 int root_a = dist[a][root]; n                 int root_b = dist[b][root]; n                 int root_lca = ( root_a + root_b - dist[a][b] )/2 ; n                 int lca_a = root_a - root_lca ; n                 int lca_b = root_b - root_lca ; n                 ans = (ans + prob[lca_a][lca_b])%mod; n             } n         } n     } n     ans = (ans * inv(n) )%mod; n     cout<<ans<<  n ; n } n int main(){ n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   int T = 1; n   //cin >> T; n   while (T--){ n     solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long double EPS = (long double)1e-7; n const long double PI = acos(0) * 2; n bool isZero(const long double& x) { return abs(x) <= EPS; } n int sign(const long double& x) { return isZero(x) ? 0 : (0 < x ? 1 : -1); } n long long gcd(long long a, long long b) { n   for (; b; a %= b, swap(a, b)) { n   } n   return abs(a); n } n pair<long long, long long> operator+(const pair<long long, long long>& a, n                                      const pair<long long, long long>& b) { n   return pair<long long, long long>(a.first + b.first, a.second + b.second); n } n pair<long long, long long> operator-(const pair<long long, long long>& a, n                                      const pair<long long, long long>& b) { n   return pair<long long, long long>(a.first - b.first, a.second - b.second); n } n pair<long long, long long> operator*(const pair<long long, long long>& a, n                                      const long long& b) { n   return pair<long long, long long>(a.first * b, a.second * b); n } n long long operator*(const pair<long long, long long>& a, n                     const pair<long long, long long>& b) { n   return a.first * b.second - b.first * a.second; n } n long long ccw(const pair<long long, long long>& a, n               const pair<long long, long long>& b, n               const pair<long long, long long>& c) { n   return a * b + b * c + c * a; n } n void fg(vector<int> G[], int a, int b) { n   G[a].emplace_back(b); n   G[b].emplace_back(a); n } n void fg(vector<pair<int, int> > G[], int a, int b, int c) { n   G[a].emplace_back(b, c); n   G[b].emplace_back(a, c); n } n const int MAXN = 1000055; n int A[MAXN], B[MAXN]; n bitset<MAXN> C; n int T, N; n void run() { n   cin >> N; n   for (int i = 1; i <= N; i++) { n     cin >> A[i]; n     B[i] = i - A[i]; n   } n   vector<int> V; n   for (int i = 1;;) { n     V.emplace_back(i); n     if (C[i]) break; n     C[i] = true; n     i = B[i]; n   } n   for (int v : V) C[v] = false; n   V.erase(V.begin(), find(((V).begin()), ((V).end()), V.back()) + 1); n   cout << ((int)(V).size()) <<   n ; n   for (int v : V) cout << v <<    ; n   cout <<   n ; n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   for (cin >> T; T--;) run(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int inf(1010101010); n int le_res, aa; n char naam[1000]; n int le_naam; n char naam_and_res[1000]; n char occ[1000]; n int zvals[1000]; n void z_functie(char* s, int n, int* z) { n   int a(0), b(0); n   for (int i(0); i < (n); i++) z[i] = n; n   for (int i = 1; i < n; i++) { n     int k = max(0, min(b - i, z[i - a])); n     while (i + k < n && s[i + k] == s[k]) k++; n     z[i] = k; n     if (i + k > b) a = i, b = i + k; n   } n } n bool chk(char* s) { n   for (int i(0); i < (le_res); i++) naam_and_res[le_naam + 1 + i] = s[i]; n   naam_and_res[le_naam + 1 + le_res] = 0; n   z_functie(naam_and_res, le_naam + 1 + le_res, zvals); n   for (int i(0); i < (le_res - le_naam + 1); i++) { n     int zz(zvals[le_naam + 1 + i]); n     if (zz == le_naam && occ[i] ==  0 ) return false; n     if (zz != le_naam && occ[i] ==  1 ) return false; n   } n   return true; n } n char tpl[1000]; n char ans[1000]; n int main() { n   cin >> le_res >> aa; n   scanf( %s , naam), le_naam = strlen(naam); n   for (int i(0); i < (le_naam); i++) naam_and_res[i] = naam[i]; n   naam_and_res[le_naam] =  $ ; n   scanf( %s , occ); n   for (int i(0); i < (le_res); i++) tpl[i] =  . ; n   for (int i(0); i < (le_res - le_naam + 1); i++) { n     if (occ[i] ==  1 ) { n       for (int j(i); j < (i + le_naam); j++) { n         tpl[j] = naam[j - i]; n       } n     } n   } n   for (int i(0); i < (le_naam); i++) { n     char c( a  + (((naam[i] -  a ) + 1) % aa)); n     for (int i(0); i < (le_res); i++) { n       if (tpl[i] ==  . ) n         ans[i] = c; n       else n         ans[i] = tpl[i]; n     } n     if (chk(ans)) { n       ans[le_res] = 0; n       printf( %s n , ans); n       goto done; n     } n   } n   puts( No solution ); n done:; n } n


#include <bits/stdc++.h> n #pragma GCC optimize  03  n using namespace std; n const long long int N = 2e5 + 5; n const long long int mod = 1e9 + 7; n const long long int inf = 1e9 + 9; n long long int a[N]; n signed main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   long long int n; n   cin >> n; n   stack<long long int> s; n   long long int ans = 0; n   for (long long int i = 1; i <= n; i++) { n     cin >> a[i]; n     while (!s.empty() && a[s.top()] < a[i]) s.pop(); n     if (!s.empty()) ans = max(ans, a[s.top()] ^ a[i]); n     s.push(i); n   } n   for (long long int i = n; i >= 1; i--) { n     while (!s.empty() && a[s.top()] < a[i]) s.pop(); n     if (!s.empty()) ans = max(ans, a[s.top()] ^ a[i]); n     s.push(i); n   } n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n #pragma GCC optimize( -O2 ) n using namespace std; n void err(istream_iterator<string> it) {} n template <typename T, typename... Args> n void err(istream_iterator<string> it, T a, Args... args) { n   cerr << *it <<   =   << a << endl; n   err(++it, args...); n } n const int LIM = 1e5 + 5, MOD = 1e9 + 7; n int t, n, m, k, x, y; n vector<int> par, rk; n int getpar(int i) { return (par[i] == i) ? i : (par[i] = getpar(par[i])); } n bool join(int i, int j) { n   i = getpar(i), j = getpar(j); n   if (i == j) return 0; n   if (rk[i] > rk[j]) n     par[j] = i; n   else { n     par[i] = j; n     if (rk[i] == rk[j]) rk[j]++; n   } n   return 1; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   cin >> n >> m; n   par.resize(2 * n + 2 * m - 2); n   for (int i = 0; i < par.size(); i++) par[i] = i; n   x = t + m + n + m - 2; n   rk.assign(2 * n + 2 * m - 2, 0); n   for (int i = 0; i < n; i++) { n     join(i + x, i); n     join(i + x + 1 - m, i + m - 1); n   } n   for (int i = 0; i < m; i++) { n     join(x - i, i); n     join(x - i + n - 1, i + n - 1); n   } n   set<int> st; n   for (int i = 0; i < par.size(); i++) { n     st.insert(getpar(i)); n   } n   cout << st.size() <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n using ll = long long; n using ld = long double; n ll x[1 << 18]; n int main() { n   ios::sync_with_stdio(false), cin.tie(0); n   ll n, r; n   cin >> n >> r; n   ll m = 1 << n; n   ll sum = 0; n   for (int i = 0; i < m; i++) cin >> x[i], sum += x[i]; n   cout << fixed << setprecision(7); n   cout << (ld)sum / (ld)m << endl; n   for (int i = 0; i < r; i++) { n     ll a, b; n     cin >> a >> b; n     sum -= x[a]; n     x[a] = b; n     sum += x[a]; n     cout << (ld)sum / (ld)m << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios::sync_with_stdio(false); n   long long n, k; n   cin >> n >> k; n   if (k >= n / 2) n     cout << n * (n - 1) / 2; n   else n     cout << n * (n - 1) / 2 - (n - 2 * k) * (n - 2 * k - 1) / 2; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   long long T; n   cin >> T; n   while (T--) { n     int N; n     string S; n     cin >> N >> S; n     int Who = -1; n     string Ans1 =   , Ans2 =   ; n     for (int i = 0; i < N; i++) { n       if (S[i] ==  2  && !(~Who)) { n         Ans1.push_back( 1 ); n         Ans2.push_back( 1 ); n       } else if (S[i] ==  2  && ~Who) { n         Ans1.push_back( 0 ); n         Ans2.push_back( 2 ); n       } else if (S[i] ==  0 ) { n         Ans1.push_back( 0 ); n         Ans2.push_back( 0 ); n       } else { n         if (~Who) { n           Ans1.push_back( 0 ); n           Ans2.push_back( 1 ); n         } else { n           Ans1.push_back( 1 ); n           Ans2.push_back( 0 ); n           Who = 1; n         } n       } n     } n     cout << Ans1 <<   n  << Ans2 <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int const N = 1000000; n int n, m; n vector<vector<pair<int, int> > > adj; n bool valid; n int ans[N]; n int cnt; n int st[N * 3]; n void readin() { n   int u, v; n   char b[10]; n   scanf( %d%d , &n, &m); n   valid = true; n   adj.clear(); n   adj.resize(n + 1); n   for (int i = 0; i < m; ++i) { n     scanf( %d%d%s , &u, &v, b); n     if (v <= u) { n       valid = false; n     } n     adj[u].push_back(make_pair(v, b[0] ==  L  ? 0 : 1)); n   } n } n inline int lc(int root) { return root << 1; } n inline int rc(int root) { return (root << 1) | 1; } n void init_st(int root, int l, int r) { n   if (l == r) { n     st[root] = l; n     return; n   } n   int mid = (l + r) >> 1; n   init_st(lc(root), l, mid); n   init_st(rc(root), mid + 1, r); n   st[root] = max(st[lc(root)], st[rc(root)]); n } n void update_st(int root, int l, int r, int x, int v) { n   if (l == r) { n     st[root] = v; n     return; n   } n   int mid = (l + r) >> 1; n   if (x <= mid) { n     update_st(lc(root), l, mid, x, v); n   } else { n     update_st(rc(root), mid + 1, r, x, v); n   } n } n int query_st(int root, int l, int r, int x, int y) { n   if (l == r) { n     return st[root]; n   } n   int mid = (l + r) >> 1; n   if (y <= mid) { n     return query_st(lc(root), l, mid, x, y); n   } else if (x > mid) { n     return query_st(rc(root), mid + 1, r, x, y); n   } else { n     return max(query_st(lc(root), l, mid, x, mid), n                query_st(rc(root), mid + 1, r, mid + 1, y)); n   } n } n bool build(int root, int limit) { n   if (root > limit) { n     return true; n   } n   int l_max = 0, r_min = INT_MAX; n   for (vector<pair<int, int> >::const_iterator it = adj[root].begin(); n        it != adj[root].end(); ++it) { n     if (it->second == 0) { n       l_max = max(l_max, query_st(1, 1, n, root + 1, it->first)); n     } else { n       r_min = min(r_min, it->first); n     } n   } n   if (l_max == 0) { n     ans[cnt++] = root; n     return build(root + 1, limit); n   } else if (r_min == INT_MAX) { n     bool ret = build(root + 1, limit); n     ans[cnt++] = root; n     return ret; n   } else { n     if (l_max >= r_min) { n       return false; n     } n     bool ret = true; n     ret = ret && build(root + 1, l_max); n     ans[cnt++] = root; n     ret = ret && build(l_max + 1, limit); n     return ret; n   } n } n void solve() { n   if (!valid) { n     printf( IMPOSSIBLE n ); n     return; n   } n   init_st(1, 1, n); n   for (int i = n; i >= 1; --i) { n     int u = i; n     for (vector<pair<int, int> >::const_iterator it = adj[i].begin(); n          it != adj[i].end(); ++it) { n       u = max(u, it->first); n     } n     update_st(1, 1, n, i, query_st(1, 1, n, i, u)); n   } n   cnt = 0; n   if (build(1, n)) { n     for (int i = 0; i < n; ++i) { n       printf( %d%c , ans[i], i < n - 1 ?     :   n ); n     } n   } else { n     printf( IMPOSSIBLE n ); n   } n } n int main() { n   readin(); n   solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, f = 0; n   cin >> n; n   string a[] = { Sheldon ,  Leonard ,  Penny ,  Rajesh ,  Howard }; n   while (n > 5) { n     n = n / 2 - 2; n   } n   cout << a[n - 1] << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(0); n   int n, k; n   cin >> n >> k; n   string s, st =   ; n   cin >> s; n   vector<int> m(28); n   vector<int> l(28); n   for (int i = 0; i < n; i++) { n     m[((int)s[i]) - 97]++; n   } n   if (n == k) { n     cout <<   n ; n     return 0; n   } n   while (k > 0) { n     for (int i = 0; i < 27; i++) { n       if (k >= m[i]) { n         k -= m[i]; n         l[i] = m[i]; n       } else { n         m[i] = k; n         l[i] = k; n         k = 0; n       } n       if (k == 0) { n         break; n       } n     } n   } n   for (int i = 0; i < n; i++) { n     if (l[((int)s[i]) - 97] > 0) { n       l[((int)s[i]) - 97]--; n       continue; n     } else n       cout << s[i]; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 80, Inf = 0x3f3f3f3f; n int n, f[N][N][N][2]; n int pos[3][N], num[3][N]; n char s[N]; n inline int getid(char ch) { n   if (ch ==  V ) return 0; n   if (ch ==  K ) return 1; n   return 2; n } n inline int move(int i, int j, int k, int id) { n   return max(0, num[0][id] - i) + max(0, num[1][id] - j) + n          max(0, num[2][id] - k) - 1; n } n int main() { n   ios::sync_with_stdio(false); n   cin >> n >> s + 1; n   for (int i = 1; i <= n; ++i) { n     int now = getid(s[i]); n     for (int j = 0; j < 3; ++j) num[j][i] = num[j][i - 1]; n     pos[now][++num[now][i]] = i; n   } n   memset(f, 0x3f, sizeof f); n   f[0][0][0][0] = 0; n   for (int i = 0; i <= num[0][n]; ++i) n     for (int j = 0; j <= num[1][n]; ++j) n       for (int k = 0; k <= num[2][n]; ++k) n         for (int fl = 0; fl < 2; ++fl) { n           if (i < num[0][n]) n             f[i + 1][j][k][1] = n                 min(f[i + 1][j][k][1], n                     f[i][j][k][fl] + move(i, j, k, pos[0][i + 1])); n           if (j < num[1][n] && !fl) n             f[i][j + 1][k][0] = n                 min(f[i][j + 1][k][0], n                     f[i][j][k][fl] + move(i, j, k, pos[1][j + 1])); n           if (k < num[2][n]) n             f[i][j][k + 1][0] = n                 min(f[i][j][k + 1][0], n                     f[i][j][k][fl] + move(i, j, k, pos[2][k + 1])); n         } n   cout << min(f[num[0][n]][num[1][n]][num[2][n]][0], n               f[num[0][n]][num[1][n]][num[2][n]][1]) n        << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long inf = 1e18; n long long fn(string a, string b) { n   long long n = a.size(), m = b.size(), j = 0; n   for (long long i = 0; i < m; i++) { n     if (b[i] == a[j]) { n       j++; n       if (j >= n) { n         return m - n; n       } n     } n   } n   if (j >= n) { n     return m - n; n   } n   return m - j + n - j; n } n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   long long ttt; n   cin >> ttt; n   vector<string> v; n   long long x = 1; n   while (x <= inf) { n     string s =   ; n     long long z = x; n     while (z) { n       s += (char)( 0  + z % 10); n       z /= 10; n     } n     reverse(s.begin(), s.end()); n     v.push_back(s); n     x *= 2; n   } n   while (ttt--) { n     string s; n     cin >> s; n     long long n = s.size(); n     long long ans = inf; n     for (auto x : v) { n       ans = min(ans, fn(x, s)); n     } n     cout << ans << endl; n   } n } n


#include <bits/stdc++.h> n  n using namespace std; n  n #define lln long long int n  n #define fast ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); n  n void solve(){ n  int n, k; n  cin>>n>>k; n  n  vector<int> a(n+1); n  n  for(int i = 1;i<=k;i++) n   a[i] = i; n  n  int x = k - 1; n  n  for(int i = k+1;i<=n;i++){ n   a[i] = x; n   x--; n  } n  n  vector<int> p(n+1); n  vector<int> ans; n  n  for(int i = n;i>=1;i--){ n   if(p[a[i]]==1) n    continue; n   else { n    ans.push_back(a[i]); n    p[a[i]] = 1; n   } n  } n  reverse(ans.begin(),ans.end()); n  n  for(auto i:ans){ n   cout<<i<<   ; n  } n  cout<<endl; n } n  n int main(){ n     fast n  n     int q; n     cin>>q; n  n     while(q--){ n   solve(); n     } n  n  return 0; n }


#include <bits/stdc++.h> n using namespace std; n int n, m, sz1, sz2, val; n int d1[200005], d2[200005], d[200005]; n int main() { n   scanf( %d , &n); n   for (int i = 0; i < n; i++) { n     scanf( %d , &val); n     if (val != 0) d1[sz1++] = val; n   } n   for (int i = 0; i < n; i++) { n     scanf( %d , &val); n     if (val != 0) d2[sz2++] = val; n   } n   for (int i = 0; i < sz1; i++) { n     int val = d1[i]; n     int id = (i + 1) % sz1; n     d[val] = d1[id]; n   } n   for (int i = 0; i < sz2; i++) { n     int val = d2[i]; n     int id = (i + 1) % sz2; n     if (d[val] == d2[id]) continue; n     printf( NO n ); n     return 0; n   } n   printf( YES n ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void low(int n) { n   switch (n) { n     default: n       cout <<  zero ; n       break; n     case 1: n       cout <<  one ; n       break; n     case 2: n       cout <<  two ; n       break; n     case 3: n       cout <<  three ; n       break; n     case 4: n       cout <<  four ; n       break; n     case 5: n       cout <<  five ; n       break; n     case 6: n       cout <<  six ; n       break; n     case 7: n       cout <<  seven ; n       break; n     case 8: n       cout <<  eight ; n       break; n     case 9: n       cout <<  nine ; n       break; n     case 10: n       cout <<  ten ; n       break; n     case 11: n       cout <<  eleven ; n       break; n     case 12: n       cout <<  twelve ; n       break; n     case 13: n       cout <<  thirteen ; n       break; n     case 14: n       cout <<  fourteen ; n       break; n     case 15: n       cout <<  fifteen ; n       break; n     case 16: n       cout <<  sixteen ; n       break; n     case 17: n       cout <<  seventeen ; n       break; n     case 18: n       cout <<  eighteen ; n       break; n     case 19: n       cout <<  nineteen ; n       break; n   } n } n void high(int n) { n   switch (n) { n     case 2: n       cout <<  twenty ; n       break; n     case 3: n       cout <<  thirty ; n       break; n     case 4: n       cout <<  forty ; n       break; n     case 5: n       cout <<  fifty ; n       break; n     case 6: n       cout <<  sixty ; n       break; n     case 7: n       cout <<  seventy ; n       break; n     case 8: n       cout <<  eighty ; n       break; n     default: n       cout <<  ninety ; n       break; n   } n } n int main() { n   int n; n   cin >> n; n   if (n < 20) n     low(n); n   else { n     high(n / 10); n     if (n % 10 != 0) { n       cout <<  - ; n       low(n % 10); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void ga(int N, int *A) { n   for (int i(0); i < N; i++) scanf( %d , A + i); n } n char s[222222], r[222222]; n int N, A, B; n int main(void) { n   scanf( %s%s , s, r), N = strlen(s); n   for (int i(0); i < N; i++) n     if (s[i] ^ r[i]) A += s[i] == 52, B += s[i] != 52; n   printf( %d n , max(A, B)); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 2e5 + 5, mod = 1e9 + 7; n int add(int x, int y) { n   x += y; n   return x >= mod ? x - mod : x; n } n int c[maxn << 1], dp[maxn]; n struct node { n   int x, y, op; n } e[maxn]; n bool cmp(node a, node b) { n   if (a.x == b.x) return a.y < b.y; n   return a.x < b.x; n } n void ins(int x, int k) { n   while (x) { n     c[x] = add(c[x], k); n     x -= x & (-x); n   } n } n int query(int x) { n   int res = 0; n   for (; x <= 400000; x += (x & (-x))) { n     res = add(res, c[x]); n   } n   return res; n } n int main() { n   int n, q, x; n   scanf( %d , &n); n   for (int i = 1; i <= n; i++) { n     scanf( %d%d , &e[i].y, &e[i].x); n   } n   scanf( %d , &q); n   while (q--) { n     scanf( %d , &x); n     e[x].op = 1; n   } n   sort(e + 1, e + 1 + n, cmp); n   ins(2 * n, 1); n   for (int i = 1; i <= n; i++) { n     dp[i] = query(e[i].y); n     ins(e[i].y, dp[i]); n   } n   int pv = 1, ans = 0; n   for (int i = n; i > 0; i--) { n     ins(e[i].y, (mod - dp[i]) % mod); n     if (e[i].op == 1 && e[i].y >= pv) { n       ans = add(ans, query(pv)); n       pv = e[i].y; n     } n   } n   printf( %d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n inline bool ig(char c) { return c >=  0  && c <=  9 ; } n inline void read(int &oi) { n   char c; n   int f = 1, res = 0; n   while (c = getchar(), (!ig(c)) && c !=  - ) n     ; n   c ==  -  ? f = -1 : res = (c ^ 48); n   while (c = getchar(), ig(c)) res = res * 10 + (c ^ 48); n   oi = f * res; n } n inline void print(int oi) { n   if (oi < 0) putchar( - ), oi = ~oi + 1; n   if (oi > 9) print(oi / 10); n   putchar(oi % 10 + 48); n } n int n, m, ans; n struct Edge { n   int x, y, z; n   bool operator<(const Edge &b) const { return z > b.z; } n } e[200005]; n int fa[200005]; n bool s[200005]; n inline int gfa(int u) { return u ^ fa[u] ? fa[u] = gfa(fa[u]) : u; } n int main() { n   read(n); n   read(m); n   for (int i = 1; i <= m; ++i) read(e[i].x), read(e[i].y), read(e[i].z); n   sort(e + 1, e + 1 + m); n   for (int i = 1; i <= n; ++i) fa[i] = i, s[i] = true; n   for (int i = 1; i <= m; ++i) { n     int fx = gfa(e[i].x), fy = gfa(e[i].y); n     if ((fx ^ fy) && (s[fx] || s[fy])) n       ans += e[i].z, fa[fx] = fy, s[fy] = s[fy] & s[fx]; n     else if (!(fx ^ fy) && s[fx]) n       s[fx] = false, ans += e[i].z; n   } n   print(ans); n   putchar(  n ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long a[200005]; n unordered_map<long long, long long> f, done; n vector<long long> v; n int main() { n   std::ios_base::sync_with_stdio(0); n   cin.tie(0); n   long long n; n   long long ans = 0; n   cin >> n; n   for (long long i = 1; i <= n; i++) { n     cin >> a[i]; n     f[a[i]]++; n   } n   for (int i = 1; i <= n; i++) { n     if (f[i] == 0) { n       ans++; n       v.push_back(i); n     } n   } n   cout << ans << endl; n   long long ptr = 0; n   for (long long i = 1; i <= n; i++) { n     if (f[a[i]] == 1) { n       if (done[a[i]] == 0) { n         cout << a[i] <<    ; n         done[a[i]]++; n       } else { n         cout << v[ptr] <<    ; n         ptr++; n       } n     } else { n       if (v[ptr] > a[i] && done[a[i]] == 0) { n         cout << a[i] <<    ; n         f[a[i]]--; n         done[a[i]]++; n       } else { n         f[a[i]]--; n         cout << v[ptr] <<    ; n         done[v[ptr]]++; n         ptr++; n       } n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n int N, M, T, tb[100005]; n long long a, b, c, co, x; n map<int, int> mp; n map<int, int>::iterator it; n int chk[100005]; n int main() { n   int i, j, k; n   scanf( %d%I64d , &N, &co); n   for (i = 1; i <= N; i++) { n     scanf( %d , &tb[i]); n     mp[tb[i]]++; n     x += tb[i]; n   } n   if (x < co) { n     printf( -1 n ); n     scanf(   ); n     return 0; n   } else if (x == co) { n     printf(  n ); n     scanf(   ); n     return 0; n   } n   a = N; n   b = 0; n   for (it = mp.begin(); it != mp.end(); it++) { n     k = (*it).first; n     j = (*it).second; n     if (c + a * (k - b) > co) break; n     c += a * (k - b); n     b = k; n     a -= j; n   } n   a = k - b; n   long long num = co - c; n   int idx = 0; n   for (i = 1; i <= N; i++) { n     if (tb[i] >= k) { n       chk[idx] = i; n       idx++; n     } n   } n   b = num / idx; n   num %= idx; n   i = num; n   bool err = true; n   for (int kk = 1; kk <= idx; kk++) { n     if (b != a - 1 || ((b == a - 1 && tb[chk[i]] != k) || err)) n       printf( %d  , chk[i]); n     i++; n     if (i >= idx) { n       i %= idx; n       err = false; n     } n   } n   scanf(   ); n } n


#include <bits/stdc++.h> n using namespace std; n int mida; n void update(vector<int>& T, int p, int x) { n   T[mida + p] = x; n   for (int i = (mida + p) / 2; i > 0; i /= 2) T[i] = T[2 * i] + T[2 * i + 1]; n } n int suma(vector<int>& T, int a, int b, int c = 0, int d = mida - 1, int p = 1) { n   if (a <= c and d <= b) return T[p]; n   if (d < a or c > b) return 0; n   return suma(T, a, b, c, (c + d) / 2, 2 * p) + n          suma(T, a, b, (c + d) / 2 + 1, d, 2 * p + 1); n } n int main() { n   int k; n   string r; n   cin >> k >> r; n   string s; n   for (int i = 0; i < k; ++i) s += r; n   int n = s.size(); n   mida = 1; n   while (mida < n) mida *= 2; n   vector<vector<int> > T( z  -  a  + 1, vector<int>(2 * mida, 0)); n   for (int i = 0; i < n; ++i) update(T[s[i] -  a ], i, 1); n   int q; n   cin >> q; n   while (q--) { n     int p; n     char c; n     cin >> p >> c; n     int ini = 0, fin = n - 1; n     while (ini <= fin) { n       int m = (ini + fin) / 2; n       if (suma(T[c -  a ], 0, m) >= p) n         fin = m - 1; n       else n         ini = m + 1; n     } n     s[fin + 1] = 0; n     update(T[c -  a ], fin + 1, 0); n   } n   for (int i = 0; i < n; ++i) n     if (s[i] > 0) cout << s[i]; n   cout << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int r, c, s, e; n vector<int> adj[1000 * 1000 + 10]; n int d[1000 * 1000 + 10]; n char grid[1000][1000]; n char access(int n) { n   int x = n % c; n   int y = n / c; n   return grid[y][x]; n } n void bfs(int n) { n   queue<int> frontier; n   frontier.push(n); n   d[n] = 0; n   while (!frontier.empty()) { n     int u = frontier.front(); n     frontier.pop(); n     for (int i = 0; i < adj[u].size(); i++) { n       int v = adj[u][i]; n       if (d[v] == -1) { n         d[v] = d[u] + 1; n         frontier.push(v); n       } n     } n   } n } n int main() { n   cin >> r >> c; n   for (int i = 0; i < r; i++) { n     for (int j = 0; j < c; j++) { n       cin >> grid[i][j]; n     } n   } n   for (int i = 0; i < r; i++) { n     for (int j = 0; j < c; j++) { n       int cell = i * c + j; n       if (grid[i][j] ==  T ) continue; n       if (grid[i][j] ==  S ) n         s = cell; n       else if (grid[i][j] ==  E ) n         e = cell; n       if (grid[i - 1][j] !=  T  && i > 0) { n         adj[cell].push_back(cell - c); n         adj[cell - c].push_back(cell); n       } n       if (grid[i + 1][j] !=  T  && i < r - 1) { n         adj[cell].push_back(cell + c); n         adj[cell + c].push_back(cell); n       } n       if (grid[i][j - 1] !=  T  && j > 0) { n         adj[cell].push_back(cell - 1); n         adj[cell - 1].push_back(cell); n       } n       if (grid[i][j + 1] !=  T  && j < c - 1) { n         adj[cell].push_back(cell + 1); n         adj[cell + 1].push_back(cell); n       } n     } n   } n   memset(d, -1, sizeof(d)); n   bfs(e); n   int shortest_path = d[s]; n   int ans = 0; n   for (int i = 0; i < r * c; i++) { n     char in = access(i); n     if (in ==  T  || in ==  E  || in ==  S ) continue; n     if (d[i] <= shortest_path && d[i] != -1) { n       ans += in -  0 ; n     } n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m; n int in[100000 + 5]; n bool doit() { n   if (scanf( %d%d , &n, &m) != 2) return false; n   for (int i = 0; i < n; ++i) in[i] = 0; n   int x, y; n   for (int i = 0; i < m; ++i) { n     scanf( %d%d , &x, &y); n     ++in[--x], ++in[--y]; n   } n   sort(in, in + n); n   if (in[0] == 1 && in[n - 2] == 1 && in[n - 1] == n - 1) n     printf( star topology n ); n   else if (in[2] == 2 && in[n - 1] == 2) { n     if (in[0] == 1 && in[1] == 1) n       printf( bus topology n ); n     else if (in[0] == 2) n       printf( ring topology n ); n     else n       printf( unknown topology n ); n   } else n     printf( unknown topology n ); n   return true; n } n int main() { n   while (1) n     if (!doit()) break; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = (1 << 19) + 5; n template <typename T> n inline void read(T &WOW) { n   T x = 0, flag = 1; n   char ch = getchar(); n   while (!isdigit(ch)) { n     if (ch ==  - ) flag = -1; n     ch = getchar(); n   } n   while (isdigit(ch)) { n     x = x * 10 + ch -  0 ; n     ch = getchar(); n   } n   WOW = flag * x; n } n namespace ModCalculator { n const int MOD = 998244353; n inline void Inc(int &x, int y) { n   x += y; n   if (x >= MOD) x -= MOD; n } n inline void Dec(int &x, int y) { n   x -= y; n   if (x < 0) x += MOD; n } n inline int Add(int x, int y) { n   Inc(x, y); n   return x; n } n inline int Sub(int x, int y) { n   Dec(x, y); n   return x; n } n inline int Mul(int x, int y) { return 1LL * x * y % MOD; } n inline int Ksm(int x, int y) { n   int ret = 1; n   for (; y; y >>= 1) { n     if (y & 1) ret = Mul(ret, x); n     x = Mul(x, x); n   } n   return ret; n } n inline int Inv(int x) { return Ksm(x, MOD - 2); } n }  // namespace ModCalculator n using namespace ModCalculator; n int len, rev[MAXN], w1[MAXN], w2[MAXN], fac[MAXN]; n void NTT_init(int n) { n   for (len = 1; len < n; len <<= 1) n     ; n   fac[0] = 1; n   for (int i = 1; i <= len; ++i) { n     fac[i] = Mul(fac[i - 1], i); n   } n   int Gi = 3, Gv = Inv(Gi); n   for (int i = 1; i < len; i <<= 1) { n     int wk1 = Ksm(Gi, (MOD - 1) / (i << 1)), wnk1 = 1; n     int wk2 = Ksm(Gv, (MOD - 1) / (i << 1)), wnk2 = 1; n     for (int k = 0; k < i; ++k) { n       w1[i + k] = wnk1; n       wnk1 = Mul(wnk1, wk1); n       w2[i + k] = wnk2; n       wnk2 = Mul(wnk2, wk2); n     } n   } n } n void GetRev(int n) { n   int bit = -1; n   for (len = 1; len < n; len <<= 1) ++bit; n   for (int i = 0; i < len; ++i) { n     rev[i] = (rev[i >> 1] >> 1) | ((i & 1) << bit); n   } n } n void NTT(vector<int> &a, int type) { n   a.resize(len); n   int *w = (type == 1 ? w1 : w2); n   for (int i = 0; i < len; ++i) { n     if (i < rev[i]) { n       swap(a[i], a[rev[i]]); n     } n   } n   for (int i = 1; i < len; i <<= 1) { n     for (int j = 0; j < len; j += (i << 1)) { n       for (int k = 0; k < i; ++k) { n         int x = a[j + k], y = Mul(a[j + k + i], w[i + k]); n         a[j + k] = Add(x, y); n         a[j + k + i] = Sub(x, y); n       } n     } n   } n   if (type == 1) return; n   int lenv = Inv(len); n   for (int i = 0; i < len; ++i) { n     a[i] = Mul(a[i], lenv); n   } n } n vector<int> Poly_Mul(vector<int> F, vector<int> G) { n   int deg = F.size() + G.size() - 1; n   if (deg <= 100) { n     vector<int> H(deg, 0); n     int n = F.size(), m = G.size(); n     for (int i = 0; i < n; ++i) { n       for (int j = 0; j < m; ++j) { n         Inc(H[i + j], Mul(F[i], G[j])); n       } n     } n     return H; n   } n   GetRev(deg); n   NTT(F, 1); n   NTT(G, 1); n   for (int i = 0; i < len; ++i) { n     F[i] = Mul(F[i], G[i]); n   } n   NTT(F, -1); n   F.resize(deg); n   return F; n } n int n, deg[MAXN], node[MAXN], tot; n vector<int> Poly_Prod(int l, int r) { n   if (l == r) { n     vector<int> F; n     F.push_back(1); n     F.push_back(node[l]); n     return F; n   } n   int mid = (l + r) >> 1; n   return Poly_Mul(Poly_Prod(l, mid), Poly_Prod(mid + 1, r)); n } n void solve() { n   read(n); n   NTT_init((n + 1) << 1); n   for (int i = 1, u, v; i < n; ++i) { n     read(u); n     read(v); n     ++deg[u]; n     ++deg[v]; n   } n   for (int i = 1; i <= n; ++i) { n     int x = deg[i] - (i > 1); n     if (x) node[++tot] = x; n   } n   vector<int> F = Poly_Prod(1, tot); n   int ans = 0; n   for (int i = 0; i < n && i < F.size(); ++i) { n     int tmp = Mul(F[i], fac[n - i]); n     (i & 1) ? Dec(ans, tmp) : Inc(ans, tmp); n   } n   printf( %d n , ans); n } n int main() { n   solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n constexpr long long MOD = (long long)1e9 + 7; n long long fexp(long long a, long long n) { n   a %= MOD; n   if (n == 0) return 1; n   long long ret = fexp(a * a % MOD, n / 2); n   if (n % 2 == 1) ret = ret * a % MOD; n   return ret; n } n long long blub(long long pa, long long pb) { n   return pa * fexp((pa + pb) % MOD, MOD - 2) % MOD; n } n int main() { n   long long k, pa, pb; n   cin >> k >> pa >> pb; n   long long ppa = blub(pa, pb); n   long long ppb = blub(pb, pa); n   vector<vector<long long>> dp(k + 1, vector<long long>(k, 0)); n   long long res = 0; n   dp[1][0] = 1; n   for (int i = 1; i < k; ++i) { n     for (int j = 0; j < k; ++j) { n       dp[i + 1][j] = (dp[i + 1][j] + dp[i][j] * ppa) % MOD; n       if (j + i < k) n         dp[i][j + i] = (dp[i][j + i] + dp[i][j] * ppb) % MOD; n       else { n         res = (res + (j + i) * dp[i][j] % MOD * ppb) % MOD; n       } n     } n   } n   for (int i = 0; i < k; ++i) { n     res = (res + ppb * dp[k][i] % MOD * n                      (ppa * fexp((1 - ppa) * (1 - ppa), MOD - 2) % MOD + n                       (i + k) * fexp(1 - ppa, MOD - 2) % MOD)) % n           MOD; n   } n   cout << (res % MOD + MOD) % MOD << endl; n } n


#include <bits/stdc++.h> n using namespace std; n vector<int> kmp(string &str) { n   int n = str.size(); n   vector<int> res(n + 1); n   res[0] = -1; n   int ind = -1; n   for (int i = 0; i < n; i++) { n     while (ind >= 0 && str[i] != str[ind]) ind = res[ind]; n     ind++; n     res[i + 1] = ind; n   } n   return res; n } n int nex(string &str, char ch, int i, vector<int> &kmp1) { n   if (str[i] == ch) return i + 1; n   int res = i; n   while (res >= 0 && str[res] != ch) res = kmp1[res]; n   return res + 1; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   string s, s1, s2; n   cin >> s >> s1 >> s2; n   int n = s.size(), n1 = s1.size(), n2 = s2.size(); n   vector<int> kmp1 = kmp(s1), kmp2 = kmp(s2); n   vector<vector<vector<int>>> dp( n       n + 1, vector<vector<int>>(n1 + 1, vector<int>(n2 + 1, -(1 << 29)))); n   dp[0][0][0] = 0; n   for (int i = 0; i < n; i++) { n     if (s[i] ==  * ) { n       for (char ch =  a ; ch <=  z ; ch++) { n         for (int j = 0; j < n1; j++) { n           for (int k = 0; k < n2; k++) { n             int nj = nex(s1, ch, j, kmp1), nk = nex(s2, ch, k, kmp2); n             dp[i + 1][nj][nk] = max(dp[i + 1][nj][nk], dp[i][j][k]); n           } n         } n       } n     } else { n       char ch = s[i]; n       for (int j = 0; j < n1; j++) { n         for (int k = 0; k < n2; k++) { n           int nj = nex(s1, ch, j, kmp1), nk = nex(s2, ch, k, kmp2); n           dp[i + 1][nj][nk] = max(dp[i + 1][nj][nk], dp[i][j][k]); n         } n       } n     } n     int nj = kmp1[n1], nk = kmp2[n2]; n     for (int j = 0; j < n1; j++) { n       dp[i + 1][j][nk] = max(dp[i + 1][j][nk], dp[i + 1][j][n2] - 1); n     } n     for (int k = 0; k < n2; k++) { n       dp[i + 1][nj][k] = max(dp[i + 1][nj][k], dp[i + 1][n1][k] + 1); n     } n     dp[i + 1][nj][nk] = max(dp[i + 1][nj][nk], dp[i + 1][n1][n2]); n   } n   int res = -(1 << 29); n   for (int j = 0; j < n1; j++) { n     for (int k = 0; k < n2; k++) { n       res = max(res, dp[n][j][k]); n     } n   } n   cout << res <<   n ; n   return 0; n } n


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


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


#include <bits/stdc++.h> n using namespace std; n const int N = 5e3 + 10, mod = 1e9 + 7; n int fact[N], invfact[N]; n inline int add(int x, int y) { n   x += y; n   if (x >= mod) x -= mod; n   return x; n } n inline int sub(int x, int y) { n   x -= y; n   if (x < 0) x += mod; n   return x; n } n inline int mul(int x, int y) { return (((long long)x) * y) % mod; } n inline int powr(int a, long long b) { n   int x = 1 % mod; n   while (b) { n     if (b & 1) x = mul(x, a); n     a = mul(a, a); n     b >>= 1; n   } n   return x; n } n inline int inv(int a) { return powr(a, mod - 2); } n void pre() { n   fact[0] = invfact[0] = 1; n   for (int i = 1; i < N; i++) fact[i] = mul(i, fact[i - 1]); n   invfact[N - 1] = inv(fact[N - 1]); n   for (int i = N - 2; i >= 1; i--) invfact[i] = mul(invfact[i + 1], i + 1); n   assert(invfact[1] == 1); n } n inline int C(int n, int k) { n   if (n < k || k < 0) return 0; n   return mul(fact[n], mul(invfact[k], invfact[n - k])); n } n int n, k; n int C2[N + 10], A[N], E[N]; n inline int getC2(int i) { n   if (i > k) return 0; n   return C2[i]; n } n int F[N][N]; n int main() { n   pre(); n   cin >> n >> k; n   F[0][0] = 1; n   for (int i = 1; i <= n; i++) { n     F[i][0] = 1; n     cin >> A[i]; n     for (int j = 1; j <= i; j++) { n       F[i][j] = add(F[i - 1][j], mul(A[i], F[i - 1][j - 1])); n     } n   } n   C2[0] = 1; n   for (int i = 1; i <= n + 10; i++) n     C2[i] = mul(C2[i - 1], mul(k - i + 1, inv(i))); n   int invn = inv(n); n   int _div = 1; n   for (int i = 0; i <= n - 1; i++) { n     E[i] = mul(getC2(i + 1), _div); n     _div = mul(_div, invn); n     E[i] = mul(E[i], fact[i]); n   } n   int ans = 0; n   for (int i = 0; i <= n - 1; i++) { n     ans = add( n         ans, n         mul(F[n][i], mul(n - i, mul(powr(mod - 1, n - 1 - i), E[n - 1 - i])))); n   } n   ans = mul(ans, invn); n   cout << ans; n } n


#include <bits/stdc++.h> n using namespace std; n template <class T> n inline bool chmax(T &a, T b) { n   if (a < b) { n     a = b; n     return true; n   } n   return false; n } n template <class T> n inline bool chmin(T &a, T b) { n   if (a > b) { n     a = b; n     return true; n   } n   return false; n } n const long long INF = 1e18; n long long N; n long long a[1050]; n vector<string> ans, trueans; n vector<vector<int>> Prev, Next; n void f(int item) { n   int idx1 = 0; n   int idx2 = 1; n   Next.clear(); n   for (auto v : Prev) { n     if (v.size() <= 1) continue; n     idx1 = v[0]; n     idx2 = v[1]; n   } n   ans[idx1][item] =  1 ; n   ans[idx2][item] =  0 ; n   long long one = a[item] - 1; n   for (int i = 0; i <= N; i++) { n     if (i == idx2 or i == idx1) continue; n     if (one) { n       one--; n       ans[i][item] =  1 ; n     } else { n       ans[i][item] =  0 ; n     } n   } n   for (auto v : Prev) { n     vector<int> u[2]; n     for (auto tmp : v) { n       if (ans[tmp][item] ==  0 ) n         u[0].push_back(tmp); n       else n         u[1].push_back(tmp); n     } n     for (int i = 0; i < 2; i++) { n       if (u[i].size()) Next.push_back(u[i]); n     } n   } n   swap(Prev, Next); n } n int main() { n   cin >> N; n   for (int i = 0; i < N; i++) cin >> a[i]; n   ans.resize(N + 1, string(N,  0 )); n   Prev.push_back({}); n   for (int i = 0; i <= N; i++) { n     Prev[0].push_back(i); n   } n   for (int i = 0; i < N; i++) f(i); n   for (auto v : Prev) { n     assert(v.size() == 1); n   } n   for (auto s : ans) { n     if (s != string(N,  0 )) trueans.push_back(s); n   } n   cout << trueans.size() <<   n ; n   for (auto s : trueans) { n     cout << s <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   long long int n; n   cin >> n; n   map<long long int, long long int> mp; n   vector<long long int> v(n); n   vector<long long int> cSum(n, 0); n   for (long long int i = 0; i < n; i++) { n     cin >> v[i]; n     mp[v[i]]++; n   } n   cSum[0] = v[0]; n   for (long long int i = 1; i < n; i++) { n     cSum[i] = cSum[i - 1] + v[i]; n   } n   if (cSum[n - 1] % 2 != 0) { n     cout <<  NO ; n   } else { n     int flag2 = 0; n     set<long long int> st; n     for (long long int i = 0; i < n; i++) { n       mp[v[i]]--; n       long long int sum1 = cSum[i]; n       long long int sum2 = cSum[n - 1] - cSum[i]; n       long long int t = sum1 + sum2; n       long long int each = t / 2; n       if (each == sum1 && each == sum2) { n         flag2 = 1; n         break; n       } n       if (sum1 < sum2) { n         long long int diff = each - sum1; n         if (mp[diff] > 0) { n           flag2 = 1; n           break; n         } n       } n       if (sum1 > sum2) { n         long long int diff = each - sum2; n         if (st.find(diff) != st.end()) { n           flag2 = 1; n           break; n         } n       } n       st.insert(v[i]); n     } n     if (flag2 == 0) { n       cout <<  NO ; n     } else { n       cout <<  YES ; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long n; n pair<string, long long> s[1000005]; n long long m; n bool cmp(pair<string, long long> a, pair<string, long long> b) { n   string s = a.first, t = b.first; n   for (long long i = 0; i < (m); i++) n     if (s[i] != t[i]) { n       if (i & 1) n         return s[i] > t[i]; n       else n         return s[i] < t[i]; n     } n   return a.second < b.second; n } n signed main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   cin >> n >> m; n   for (long long i = 0; i < (n); i++) cin >> s[i].first, s[i].second = i + 1; n   sort(s, s + n, cmp); n   for (long long i = 0; i < (n); i++) cout << s[i].second <<    ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n #pragma GCC optimize( Ofast ) n const long long int maxn = 5e5 + 16, md = 1e9 + 7, inf = 2e18; n long long int tav(long long int n, long long int k) { n   long long int res = 1; n   while (k > 0) { n     if (k & 1) { n       res *= n; n       res %= md; n     } n     n *= n; n     n %= md; n     k >>= 1; n   } n   return res; n } n struct segtree { n   long long int sz = 1; n   vector<long long int> val, laz; n   void init(long long int n) { n     while (sz < n) sz <<= 1; n     val.assign((sz << 1), 0); n     laz.assign((sz << 1), 0); n     return; n   } n   void shift(long long int x, long long int lx, long long int rx) { n     long long int h = laz[x]; n     laz[x] = 0; n     if (h == 0) return; n     val[x] += h; n     if (rx - lx == 1) return; n     long long int ln = (x << 1) + 1, rn = ln + 1; n     laz[ln] += h; n     laz[rn] += h; n     return; n   } n   void add(long long int l, long long int r, long long int k, long long int x, n            long long int lx, long long int rx) { n     shift(x, lx, rx); n     if (rx <= l || lx >= r) return; n     if (rx <= r && lx >= l) { n       laz[x] = k; n       shift(x, lx, rx); n       return; n     } n     long long int m = (rx + lx) >> 1, ln = (x << 1) + 1, rn = ln + 1; n     add(l, r, k, ln, lx, m); n     add(l, r, k, rn, m, rx); n     val[x] = min(val[ln], val[rn]); n     return; n   } n   void add(long long int l, long long int r, long long int k) { n     add(l, r, k, 0, 0, sz); n     return; n   } n   long long int cal(long long int l, long long int r, long long int x, n                     long long int lx, long long int rx) { n     shift(x, lx, rx); n     if (rx <= l || lx >= r) return inf; n     if (rx <= r && lx >= l) return val[x]; n     long long int m = (rx + lx) >> 1, ln = (x << 1) + 1, rn = ln + 1; n     long long int a = cal(l, r, ln, lx, m), b = cal(l, r, rn, m, rx); n     return min(a, b); n   } n   long long int cal(long long int l, long long int r) { n     return cal(l, r, 0, 0, sz); n   } n }; n long long int n, q; n segtree st; n vector<pair<long long int, long long int> > adj[maxn]; n vector<pair<pair<long long int, long long int>, long long int> > v[maxn]; n long long int ft[maxn], tme = 0, ans[maxn], dis[maxn], dep = 0; n void dDFS(long long int r) { n   dis[r] = dep; n   tme++; n   for (auto p : adj[r]) { n     long long int i = p.first, w = p.second; n     dep += w; n     dDFS(i); n     dep -= w; n   } n   if (adj[r].empty()) { n     st.add(r, r + 1, dep); n   } else { n     st.add(r, r + 1, inf); n   } n   ft[r] = tme; n   return; n } n void sDFS(long long int r) { n   for (auto y : v[r]) { n     long long int l = y.first.first, r = y.first.second, i = y.second; n     ans[i] = st.cal(l, r); n   } n   for (auto p : adj[r]) { n     long long int i = p.first, w = p.second; n     st.add(0, n, w); n     st.add(i, ft[i], -2ll * w); n     sDFS(i); n     st.add(0, n, -w); n     st.add(i, ft[i], 2ll * w); n   } n   return; n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   cin >> n >> q; n   for (long long int u = 1; u < n; u++) { n     long long int v, w; n     cin >> v >> w; n     v--; n     adj[v].push_back({u, w}); n   } n   st.init(n); n   for (long long int i = 0; i < q; i++) { n     long long int u, l, r; n     cin >> u >> l >> r; n     u--; n     l--; n     v[u].push_back({{l, r}, i}); n   } n   dDFS(0); n   sDFS(0); n   for (long long int i = 0; i < q; i++) { n     cout << ans[i] <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <class T> n bool umin(T& a, T b) { n   if (a > b) { n     a = b; n     return 1; n   } n   return 0; n } n template <class T> n bool umax(T& a, T b) { n   if (a < b) { n     a = b; n     return 1; n   } n   return 0; n } n char s[1000009], t[1000009]; n int cnt[2]; n int pw[1000009], par[1000009]; n int wp[1000009], rap[1000009]; n int mod(long long x) { n   x %= 1000000007; n   if (x < 0) x += 1000000007; n   return x; n } n pair<int, int> get(int l, int r) { n   return make_pair(mod(par[r] - (par[l - 1] * 1LL * pw[r - l + 1])), n                    mod(rap[r] - (rap[l - 1] * 1LL * wp[r - l + 1]))); n } n int main() { n   scanf( %s , s); n   int n = strlen(s); n   scanf( %s , t + 1); n   int m = strlen(t + 1); n   pw[0] = wp[0] = 1; n   for (int i = 1; i < 1000009; i++) { n     pw[i] = mod(pw[i - 1] * 27LL); n     wp[i] = mod(wp[i - 1] * 31LL); n   } n   for (int i = 1; i <= m; i++) { n     par[i] = mod(par[i - 1] * 27LL + (t[i] -  a  + 1)); n     rap[i] = mod(rap[i - 1] * 31LL + (t[i] -  a  + 1)); n   } n   for (int i = 0; i < n; i++) cnt[s[i] -  0 ]++; n   int ans = 0; n   for (int i = 1; i * cnt[0] < m and i < m; i++) n     if ((m - i * cnt[0]) % cnt[1] == 0) { n       int d = (m - i * cnt[0]) / cnt[1]; n       pair<int, int> a, b; n       a.first = b.first = -1; n       int cur = 1, f = 1; n       for (int j = 0; j < n; j++) { n         if (s[j] ==  0 ) { n           if (a.first == -1) n             a = get(cur, cur + i - 1); n           else n             f &= (a == get(cur, cur + i - 1)); n           cur += i; n         } else { n           if (b.first == -1) n             b = get(cur, cur + d - 1); n           else n             f &= (b == get(cur, cur + d - 1)); n           cur += d; n         } n       } n       if (f and d == i and a == b) f = 0; n       ans += f; n     } n   printf( %d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n long long t, a, b, h, f[110]; n using namespace std; n long long mi(long long n, long long m) { n   long long res = 1; n   for (int i = 1; i <= m; i++) res *= n; n   return res; n } n void solve(long long a, long long b) { n   for (int i = h; i; i--) { n     long long t = mi(a, i); n     f[i] = b / t; n     b %= t; n   } n   f[0] = b; n } n int main() { n   scanf( %lld%lld%lld , &t, &a, &b); n   if (a == b && b == t) { n     if (a == 1) n       puts( inf ); n     else n       puts( 2 ); n   } else if (a == b) n     puts( 1 ); n   else { n     if (t == 1) { n       h = log(b) / log(a); n       for (int i = 1; i <= h; i++) n         if (mi(a, i) == b) { n           puts( 1 ); n           return 0; n         } n     } n     h = log(b) / log(a); n     solve(a, b); n     long long sum = 0; n     for (int i = 0; i <= h; i++) sum += f[i] * mi(t, i); n     if (sum != a) n       puts( 0 ); n     else n       puts( 1 ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   long long n; n   cin >> n; n   vector<pair<long long, long long>> arr(n); n   for (long long i = 0; i < n; i++) { n     long long a; n     cin >> a; n     if (a >= 0) a = (-1) * a - 1; n     arr[i] = {a, i}; n   } n   sort(arr.begin(), arr.end()); n   if (n % 2 == 1) { n     long long f = 0; n     for (long long i = 0; i < n; i++) { n       if (arr[i].first != -1) { n         arr[i].first = (-1) * arr[i].first - 1; n         f++; n         break; n       } n     } n     if (f == 0) { n       arr[0].first = 0; n     } n   } n   vector<long long> ans(n); n   for (long long i = 0; i < n; i++) { n     ans[arr[i].second] = arr[i].first; n   } n   for (auto i : ans) cout << i <<    ; n   cout << endl; n } n


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


#include <bits/stdc++.h> n using namespace std; n int main() { n   int a, b; n   cin >> a >> b; n   if (a > b) { n     if (a == 9 && b == 1) n       cout << 9 <<     << 10; n     else n       cout << -1; n     return 0; n   } n   if ((b - a) > 1) { n     cout << -1; n     return 0; n   } n   if (b == a) { n     cout << a << 98 <<     << b << 99; n     return 0; n   } n   cout << a <<     << b; n   return 0; n } n


#include <bits/stdc++.h> n #pragma GCC optimize(3) n #pragma GCC optimize( inline ) n using namespace std; n const int N = 100050, INF = 0x3f3f3f3f; n int n, col, sum, n1, n2; n struct node { n   int color, val, id; n   friend bool operator<(node x, node y) { return x.val < y.val; } n } white[N], black[N]; n int min(int x, int y) { return (x > y) ? y : x; } n signed main() { n   scanf( %d , &n); n   for (register int i = 1; i <= n; i++) { n     scanf( %d%d , &col, &sum); n     if (col) n       white[++n1] = node{col, sum, i}; n     else n       black[++n2] = node{col, sum, i}; n   } n   sort(white + 1, white + 1 + n1); n   sort(black + 1, black + 1 + n2); n   for (register int i = 1, j = 1; i <= n1 && j <= n2;) { n     int now = min(white[i].val, black[j].val); n     cout << white[i].id <<     << black[j].id <<     << now << endl; n     white[i].val -= now; n     black[j].val -= now; n     if (white[i].val) n       j++; n     else if (black[j].val) n       i++; n     else if (i < n1) n       i++; n     else n       j++; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long N = 5e3 + 5; n long long read() { n   long long s = 0; n   char c = getchar(), lc =  + ; n   while (c <  0  ||  9  < c) lc = c, c = getchar(); n   while ( 0  <= c && c <=  9 ) s = s * 10 + c -  0 , c = getchar(); n   return lc ==  -  ? -s : s; n } n void write(long long x) { n   if (x < 0) { n     putchar( - ); n     x = -x; n   } n   if (x < 10) n     putchar(x +  0 ); n   else { n     write(x / 10); n     putchar(x % 10 +  0 ); n   } n } n void print(long long x, char c =   n ) { n   write(x); n   putchar(c); n } n long long dp[N][N], a[N], q[N]; n signed main(signed Recall, char *_902_[]) { n   (void)Recall, (void)_902_; n   long long n = read(), k = read(), m = read(), ans = -1; n   for (long long i = 1; i <= n; i++) a[i] = read(); n   for (long long i = 1; i <= n; i++) dp[i][0] = dp[0][i] = -1; n   for (long long j = 1; j <= m; j++) n     for (long long i = 1, h = 0, t = q[0] = 0; i <= n; i++) { n       while (h <= t && i - q[h] > k) h++; n       if (h <= t && dp[q[h]][j - 1] >= 0) n         dp[i][j] = dp[q[h]][j - 1] + a[i]; n       else n         dp[i][j] = -1; n       while (h <= t && dp[q[t]][j - 1] <= dp[i][j - 1]) t--; n       q[++t] = i; n     } n   for (long long i = max(n - k + 1, 1LL); i <= n; i++) ans = max(ans, dp[i][m]); n   print(ans); n   return 0; n } n


/****************************************** By Lord Sanskar Bhargava******************************/   n  n  n #include<bits/stdc++.h> n using namespace std; n  n typedef long long ll; n typedef long double ld; n typedef complex<ld> cd; n   n typedef pair<int, int> pi; n typedef pair<ll,ll> pl; n typedef pair<ld,ld> pd; n   n typedef vector<int> vi; n typedef vector<ld> vd; n typedef vector<ll> vl; n typedef vector<pi> vpi; n typedef vector<pl> vpl; n typedef vector<cd> vcd; n  n int main() n { n     ios::sync_with_stdio(0);cin.tie(0); n     int tt; n     cin>>tt; n     while(tt--) n     { n       ll n;cin>>n; n     ll k=n%2020; n       if(n<2020) n       { n         cout<< NO <<endl; n       } n       else if(k*2021<=n) n       { n         cout<< YES <<endl; n       } n       else n       { n         cout<< NO <<endl; n       } n  n     } n return 0; n }


#include <bits/stdc++.h> n using namespace std; n int dp[1003][1003][12][3]; n string s, t; n int solve(int i, int j, int k, int cont) { n   if (i < 0 || j < 0) return 0; n   if (dp[i][j][k][cont] != -1) return dp[i][j][k][cont]; n   int ret = -9; n   if (s[i] == t[j]) { n     if (cont) { n       ret = max(ret, 1 + solve(i - 1, j - 1, k, 1)); n     } n     if (k > 0) ret = max(ret, 1 + solve(i - 1, j - 1, k - 1, 1)); n   } n   ret = max(ret, solve(i - 1, j, k, 0)); n   ret = max(ret, solve(i, j - 1, k, 0)); n   dp[i][j][k][cont] = ret; n   return ret; n } n int main() { n   int n, m, k; n   cin >> n >> m >> k; n   cin >> s >> t; n   for (int i = 0; i < 1003; ++i) { n     for (int j = 0; j < 1003; ++j) { n       for (int k = 0; k < 12; ++k) { n         for (int l = 0; l < 3; ++l) { n           dp[i][j][k][l] = -1; n         } n       } n     } n   } n   int ans = solve(n - 1, m - 1, k, 0); n   cout << ans <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n const long long mod = 1e9 + 7; n const int N = 510; n int m; n long long n, ans; n int cnt, head[N]; n struct edge { n   int to, next; n } e[1010]; n void ade(int u, int v) { n   e[++cnt] = (edge){v, head[u]}; n   head[u] = cnt; n } n long long getsize(long long num) { n   if (num > n) return 0; n   long long tl = num, tr = num; n   int m1 = 0, m2 = 0; n   while (tl * 2 <= n) tl = tl * 2, m1++; n   while (tr * 2 + 1 <= n) tr = tr * 2 + 1, m2++; n   if (tl <= tr) n     return (1 << (m2 + 1)) - 1; n   else n     return (1 << (m2 + 1)) - 1 + n - tl + 1; n } n map<int, int> vis; n int tot; n long long val[N]; n void build(int x) { n   val[vis[x]] = 1; n   int to = x << 1; n   if (to <= n && vis[to]) { n     build(to); n     ade(vis[x], vis[to]); n     ade(vis[to], vis[x]); n   } else n     val[vis[x]] += getsize(to); n   to = x << 1 | 1; n   if (to <= n && vis[to]) { n     build(to); n     ade(vis[x], vis[to]); n     ade(vis[to], vis[x]); n   } else n     val[vis[x]] += getsize(to); n } n int bk[N]; n void dfs(int now, long long w) { n   bk[now] = 1; n   for (int i = head[now]; i; i = e[i].next) { n     int to = e[i].to; n     if (bk[to]) continue; n     dfs(to, w); n     ans = (ans + w * val[to] % mod) % mod; n   } n   bk[now] = 0; n } n int main() { n   cin >> n >> m; n   if (!m) { n     ans = n * n % mod; n     ans = (ans % mod + mod) % mod; n     printf( %lld n , ans); n   } else { n     for (int i = 1; i <= m; i++) { n       int a, b; n       cin >> a >> b; n       if (!vis[a]) vis[a] = ++tot; n       if (!vis[b]) vis[b] = ++tot; n       ade(vis[a], vis[b]); n       ade(vis[b], vis[a]); n       while (a) { n         a >>= 1; n         if (!vis[a]) vis[a] = ++tot; n       } n       while (b) { n         b >>= 1; n         if (!vis[b]) vis[b] = ++tot; n       } n     } n     build(1); n     for (int i = 1; i <= tot; i++) dfs(i, val[i]); n     for (int i = 1; i <= tot; i++) ans = (ans + val[i] * val[i] % mod) % mod; n     printf( %lld n , ans); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAX_N = 105, MAX_R = 60005; n int n, r, a, b; n vector<pair<int, int> > inc, d; n int dp[MAX_R][MAX_N]; n int main() { n   ios::sync_with_stdio(false); n   cin >> n >> r; n   for (int i = 0; i < n; i++) { n     cin >> a >> b; n     if (b >= 0) { n       inc.push_back({a, b}); n     } else { n       d.push_back({max(a, -b) + b, b}); n     } n   } n   sort(inc.begin(), inc.end()); n   sort(d.begin(), d.end()); n   int proj = 0; n   for (auto p : inc) { n     if (r >= p.first) { n       proj++; n       r += p.second; n     } n   } n   int len = d.size(); n   for (int i = 0; i <= len; i++) { n     if (i == 0) { n       for (int j = 0; j <= r; j++) { n         dp[j][i] = proj; n       } n       continue; n     } n     int a = d[i - 1].first - d[i - 1].second; n     int b = d[i - 1].second; n     for (int j = 0; j <= r; j++) { n       if (j >= a) { n         dp[j][i] = max(dp[j][i - 1], 1 + dp[j + b][i - 1]); n       } else { n         dp[j][i] = dp[j][i - 1]; n       } n     } n   } n   cout << dp[r][len] << endl; n } n