#include <bits/stdc++.h> n #pragma GCC optimize( Ofast ) n #pragma GCC target( avx,avx2,fma ) n #pragma GCC optimization( unroll-loops ) n using namespace std; n using namespace chrono; n template <typename Arg1> n void __f(const char* name, Arg1&& arg1) { n   cout << name <<   :   << arg1 <<   n ; n } n template <typename Arg1, typename... Args> n void __f(const char* names, Arg1&& arg1, Args&&... args) { n   const char* comma = strchr(names + 1,  , ); n   cout.write(names, comma - names) <<   :   << arg1 <<   |  ; n   __f(comma + 1, args...); n } n long long int advceil(long long int num, long long int den) { n   return (num % den == 0 ? num / den : num / den + 1); n } n long long int lstbt(long long int val) { n   long long int msk = val & (val - 1); n   return log2(val ^ msk); n } n long long int modmul(long long int a, long long int b) { n   a %= 1000000007; n   b %= 1000000007; n   long long int res = 0; n   while (b > 0) { n     if (b & 1) res = (res + a) % 1000000007; n     a = (a * 2) % 1000000007; n     b /= 2; n   } n   return res; n } n long long int modexpo(long long int a, long long int b) { n   long long int res = 1; n   while (b > 0) { n     if (b & 1) res = modmul(res, a); n     a = modmul(a, a); n     b /= 2; n   } n   return res; n } n long long int gcd(long long int a, long long int b) { n   return a == 0 ? b : gcd(b % a, a); n } n vector<long long int> CALCfactor(long long int n) { n   vector<long long int> factor(n + 2, 0); n   for (long long int i = 4; i <= n; i += 2) factor[i] = 2; n   for (long long int j = 3; j <= n; j += 2) { n     if (factor[j]) continue; n     for (long long int i = 2 * j; i <= n; i += j) factor[i] = j; n   } n   return factor; n } n vector<long long int> CALCprimeNUM(long long int n) { n   vector<long long int> factor = CALCfactor(n); n   vector<long long int> primeNUM; n   primeNUM.reserve(n + 2); n   for (long long int i = 2; i <= n; i++) n     if (!factor[i]) primeNUM.push_back(i); n   return primeNUM; n } n vector<long long int> CALCprimeFACTOR(long long int n) { n   vector<long long int> factor = CALCfactor(n); n   vector<long long int> ans; n   while (factor[n] != 0) { n     ans.push_back(factor[n]); n     n /= factor[n]; n   } n   ans.push_back(n); n   return ans; n } n vector<long long int> unique(vector<long long int> x) { n   sort(x.begin(), x.end()); n   set<long long int> s; n   vector<long long int> ans; n   ans.reserve(x.size()); n   for (auto elem : x) s.insert(elem); n   for (auto elem : s) ans.push_back(elem); n   return ans; n } n vector<long long int> ZALGO(string s, string pat) { n   long long int lens = pat.length(); n   pat += char(36); n   pat += s; n   s = pat; n   vector<long long int> pref(s.length() + 1, 0); n   pref[0] = s.length(); n   long long int spt = -1; n   long long int ept = -1; n   long long int processCnt = 0; n   for (long long int i = 1; i < s.length(); i++) { n     bool isdone = false; n     if (i <= ept and i >= spt) { n       if (pref[i - spt] + i <= ept) pref[i] = pref[i - spt], isdone = true; n       processCnt++; n     } n     if (!isdone) { n       long long int ptr = i; n       long long int lo = 0; n       while (s[lo] == s[ptr]) ptr++, lo++, processCnt++; n       if (ept < ptr - 1) { n         spt = i; n         ept = ptr - 1; n       } n       pref[i] = lo; n     } n   } n   vector<long long int> ans; n   ans.reserve(s.length() - lens); n   for (long long int i = lens + 1; i < s.length(); i++) ans.push_back(pref[i]); n   return ans; n } n void spclSort( n     vector<pair<pair<long long int, long long int>, long long int>>& v) { n   long long int n = v.size(); n   { n     vector<long long int> cnt(n); n     for (auto elem : v) { n       cnt[elem.first.second]++; n     } n     vector<pair<pair<long long int, long long int>, long long int>> a_new(n); n     vector<long long int> pos(cnt.size()); n     pos[0] = 0; n     for (long long int i = 1; i < cnt.size(); i++) { n       pos[i] = cnt[i - 1] + pos[i - 1]; n     } n     for (auto elem : v) { n       a_new[pos[elem.first.second]] = elem; n       pos[elem.first.second]++; n     } n     v = a_new; n   } n   { n     vector<long long int> cnt(n, 0); n     for (auto elem : v) { n       cnt[elem.first.first]++; n     } n     vector<pair<pair<long long int, long long int>, long long int>> a_new(n); n     vector<long long int> pos(cnt.size()); n     pos[0] = 0; n     for (long long int i = 1; i < cnt.size(); i++) { n       pos[i] = cnt[i - 1] + pos[i - 1]; n     } n     for (auto elem : v) { n       a_new[pos[elem.first.first]] = elem; n       pos[elem.first.first]++; n     } n     v = a_new; n   } n } n pair<vector<vector<long long int>>, vector<vector<long long int>>> getOrdering( n     string s) { n   s += char(36); n   vector<vector<long long int>> dp; n   dp.reserve(log2(s.length()) + 3); n   vector<vector<long long int>> dp2; n   dp2.reserve(log2(s.length()) + 3); n   vector<long long int> ordering(s.length()), eqClass(s.length()); n   { n     vector<pair<char, long long int>> temp(s.length()); n     for (long long int i = 0; i < s.length(); i++) temp[i] = {s[i], i}; n     sort(temp.begin(), temp.end()); n     for (long long int i = 0; i < temp.size(); i++) n       ordering[temp[i].second] = i; n     eqClass[temp[0].second] = 0; n     for (long long int i = 1; i < temp.size(); i++) { n       if (temp[i].first != temp[i - 1].first) n         eqClass[temp[i].second] = eqClass[temp[i - 1].second] + 1; n       else n         eqClass[temp[i].second] = eqClass[temp[i - 1].second]; n     } n     dp.push_back(ordering); n     dp2.push_back(eqClass); n   } n   long long int k = 1; n   while ((1 << (k - 1)) < s.length()) { n     vector<pair<pair<long long int, long long int>, long long int>> arr( n         s.length()); n     for (long long int i = 0; i < s.length(); i++) { n       arr[i] = {{eqClass[i], eqClass[(i + (1 << (k - 1))) % s.length()]}, i}; n     } n     spclSort(arr); n     for (long long int i = 0; i < s.length(); i++) ordering[arr[i].second] = i; n     eqClass[arr[0].second] = 0; n     for (long long int i = 1; i < s.size(); i++) { n       if (arr[i].first != arr[i - 1].first) n         eqClass[arr[i].second] = eqClass[arr[i - 1].second] + 1; n       else n         eqClass[arr[i].second] = eqClass[arr[i - 1].second]; n     } n     dp.push_back(ordering); n     dp2.push_back(eqClass); n     k++; n   } n   return {dp, dp2}; n } n vector<long long int> sortOrdering(vector<vector<long long int>> dp) { n   vector<long long int> arr = dp[dp.size() - 1]; n   vector<long long int> ans(arr.size()); n   long long int cnt = 0; n   for (auto elem : arr) { n     ans[elem] = cnt; n     cnt++; n   } n   return ans; n } n vector<long long int> getLps(string pat) { n   long long int i = 1; n   long long int j = 0; n   vector<long long int> lps(pat.length(), 0); n   lps[0] = 0; n   while (i < pat.length()) { n     if (pat[i] == pat[j]) n       lps[i] = j + 1, i++, j++; n     else if (j == 0) n       lps[i] = 0, i++; n     else n       j = lps[j - 1]; n   } n   return lps; n } n pair<vector<long long int>, vector<long long int>> getFact(long long int n) { n   vector<long long int> fact(n + 1, 1), invfact(n + 1, 1); n   for (long long int i = 1; i <= n; i++) n     fact[i] = (i * (fact[i - 1])) % 1000000007; n   for (long long int i = 1; i <= n; i++) n     invfact[i] = (modexpo(i, 1000000007 - 2) * invfact[i - 1]) % 1000000007; n   return {fact, invfact}; n } n void solve() { n   long long int n; n   cin >> n; n   cout << 2 <<   n ; n   for (long long int i = 3; i <= n + 1; i++) { n     cout << (i * (i) * (i - 1) - (i - 2)) <<   n ; n   } n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   auto start1 = high_resolution_clock::now(); n   solve(); n   auto stop1 = high_resolution_clock::now(); n   auto duration = duration_cast<microseconds>(stop1 - start1); n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   scanf( %d , &n); n   char ar[n + 2]; n   char br[n + 2]; n   scanf( %s%s , ar + 1, br + 1); n   vector<int> v[2]; n   for (int i = 1; i <= n; i++) { n     if (ar[i] != br[i]) { n       if (ar[i] ==  a ) n         v[0].push_back(i); n       else n         v[1].push_back(i); n     } n   } n   if ((v[0].size() + v[1].size()) % 2 == 1) n     cout << -1 << endl; n   else { n     int res = v[0].size() + v[1].size(); n     res /= 2; n     if (v[0].size() % 2) res += 1; n     cout << res << endl; n     for (int i = 1; i < v[0].size(); i += 2) { n       cout << v[0][i - 1] <<     << v[0][i] << endl; n     } n     for (int i = 1; i < v[1].size(); i += 2) { n       cout << v[1][i - 1] <<     << v[1][i] << endl; n     } n     if (v[0].size() % 2) { n       cout << v[0][v[0].size() - 1] <<     << v[0][v[0].size() - 1] << endl; n       cout << v[0][v[0].size() - 1] <<     << v[1][v[1].size() - 1] << endl; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int k; n int n; n int planks[1005]; n int g = 0; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   cin >> k; n   for (int t = 0; t < k; t++) { n     cin >> n; n     for (int i = 0; i < n; i++) cin >> planks[i]; n     sort(planks, planks + n, [](int a, int b) { return a > b; }); n     g = 0; n     for (int i = 0; i < n; i++) n       if (planks[i] >= i + 1) g = max(g, i + 1); n     cout << g <<   n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n long long A[200005]; n long long fast_pow(long long a, long long b) { n   long long res = 1; n   while (b > 0) { n     if (b & 1) res = (res * a) % 1000000007; n     a = (a * a) % 1000000007; n     b >>= 1; n   } n   return res; n } n int main() { n   long long div = 1, num = 1; n   int n; n   cin >> n; n   for (int i = 0; i < n; i++) { n     cin >> A[i]; n     num = (num * A[i]) % 1000000007; n   } n   sort(A, A + n); n   int idx = 0; n   bool chk = true; n   while (idx < n) { n     long long p = A[idx]; n     long long cnt = 0; n     while (idx < n && p == A[idx]) { n       idx++; n       cnt++; n     } n     div = (div * (cnt + 1LL)) % (2LL * (1000000007 - 1)); n     chk = (chk && (cnt % 2 == 0)); n   } n   div = (div % ((1000000007 - 1) * 2LL)) / 2; n   long long ans = fast_pow(num, div); n   if (chk) { n     idx = 0; n     while (idx < n) { n       long long p = A[idx]; n       long long cnt = 0; n       while (idx < n && p == A[idx]) { n         idx++; n         cnt++; n       } n       ans = (ans * fast_pow(p, cnt / 2)) % 1000000007; n     } n   } n   cout << ans << endl; n   return 0; n } n


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


#include <bits/stdc++.h> n using namespace std; n const int N = 2001000, mod = 998244353; n int n, a[N], b[N], f[N], ans, sum, inv[N]; n void Init() { n   f[0] = inv[1] = 1; n   f[1] = n; n   for (int i = 2; i < N; ++i) { n     inv[i] = mod - mod / i * (long long)inv[mod % i] % mod; n     f[i] = (long long)f[i - 1] * (n - 1 + i) % mod * inv[i] % mod; n   } n } n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   cin >> n; n   Init(); n   for (int i = 1; i <= n; ++i) { n     cin >> a[i]; n     ++b[a[i]]; n   } n   for (int i = 0, j = 0; i < n; ++i) { n     j += b[i]; n     if (j >= i + 2) { n       cout << (ans + 1) % mod <<   n ; n       return 0; n     } n     ans = (ans + f[i - j + 1]) % mod; n   } n   memset(b, 0, sizeof b); n   ans = 0; n   for (int i = 1; i <= n; ++i) sum += a[i] / n, b[a[i] % n]++; n   for (int i = 0; i < n; ++i) { n     ans = (ans + f[sum]) % mod; n     sum -= b[i] - 1; n   } n   cout << ans <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int sum[51], ind[51]; n int main() { n   for (int i = 1; i <= 50; ++i) sum[i] = i * (i + 1) / 2; n   for (int i = 1; i <= 50; ++i) ind[i] = i; n   int n, temp, reqind; n   cin >> n; n   for (int i = 1; i <= 50; ++i) { n     if (n <= sum[i]) { n       temp = sum[i]; n       reqind = i; n       break; n     } n   } n   int diff = temp - n; n   if (diff == 0) { n     cout << reqind << endl; n     for (int k = 1; k <= reqind; ++k) cout << ind[k] <<    ; n     cout << endl; n     return 0; n   } n   cout << reqind - 1 << endl; n   for (int i = 1; i <= reqind; ++i) { n     if (i != diff) cout << i <<    ; n   } n   cout << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long n, m; n   cin >> n >> m; n   for (long long i = 1; i <= n; i++) { n     long long s, f, t; n     cin >> s >> f >> t; n     if (s == f) { n       cout << t << endl; n     } else if (s < f) { n       long long t1 = s - 1; n       while (t1 < t) { n         t1 += (m - s) + (m - 1) + (s - 1); n       } n       t1 += (f - s); n       cout << t1 << endl; n     } else if (s > f) { n       long long t1 = (m - 1) + (m - s); n       while (t1 < t) { n         t1 += (m - 1) + (m - s) + (s - 1); n       } n       t1 += (s - f); n       cout << t1 << endl; n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 405; n const int inf = 0x3f3f3f3f; n int n, m; n int a[maxn]; n int f[maxn][maxn][maxn]; n template <typename T> n inline void minimize(T &target, const T &value) { n   if (target > value) { n     target = value; n   } n } n void divide(int a[], int f[], int g[], int l, int r, int pl, int pr) { n   if (l > r) { n     return; n   } n   int m = (l + r) / 2; n   int ptr = l; n   for (int i = pl; i < min(m, pr + 1); ++i) { n     int tmp = max(f[i], a[m] - a[i]); n     if (tmp < g[m]) { n       g[m] = tmp; n       ptr = i; n     } n   } n   divide(a, f, g, l, m - 1, pl, ptr); n   divide(a, f, g, m + 1, r, ptr, pr); n } n void compute(int a[], int n, int f[maxn][maxn]) { n   f[0][0] = 0; n   for (int i = 1; i < n; ++i) { n     divide(a, f[i - 1], f[i], 0, n - 1, 0, n - 1); n   } n } n int main() { n   scanf( %d%d , &n, &m); n   for (int i = 0; i < n; ++i) { n     scanf( %d , a + i); n   } n   memset(f, inf, sizeof(f)); n   compute(a, n, f[0]); n   for (int i = 0; i < n; ++i) { n     compute(a + i, n - i, f[i]); n   } n   long long ans = 0; n   while (m--) { n     int s, f, c, r; n     scanf( %d%d%d%d , &s, &f, &c, &r), s--, f--; n     r++; n     r = min(r, f - s); n     r = max(r, 1); n     ans = max(ans, (long long)c * ::f[s][r][f - s]); n   } n   printf( %lld n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n pair<int, int> pairs[2 * 100000 + 10]; n vector<int> adj[100000 + 10]; n bool done[100000 + 10]; n int result[100000 + 10]; n void check(int N) { n   bool adj[N + 2][N + 2]; n   memset(adj, false, sizeof(adj)); n   for (int i = 1; i <= 2 * N; i++) { n     scanf( %d %d , &pairs[i].first, &pairs[i].second); n     adj[pairs[i].first][pairs[i].second] = true; n     adj[pairs[i].second][pairs[i].first] = true; n   } n   int A[N]; n   for (int i = 0; i < N; i++) A[i] = i + 1; n   do { n     bool found = true; n     for (int i = 0; i < N; i++) n       if (!(adj[A[i]][A[(i + 1) % N]] && adj[A[i]][A[(i + 2) % N]])) n         found = false; n     if (found) { n       for (int i = 0; i < N; i++) printf( %d  , A[i]); n       printf(  n ); n       return; n     } n   } while (next_permutation(A, A + N)); n   printf( -1 n ); n } n bool contains(int a, int b) { n   for (int i = 0; i < adj[a].size(); i++) n     if (adj[a][i] == b) return true; n   return false; n } n int main() { n   int N; n   scanf( %d , &N); n   if (N == 5 || N == 6) { n     check(N); n     return 0; n   } n   for (int i = 1; i <= 2 * N; i++) { n     scanf( %d %d , &pairs[i].first, &pairs[i].second); n     adj[pairs[i].first].push_back(pairs[i].second); n     adj[pairs[i].second].push_back(pairs[i].first); n   } n   int prev = 1; n   done[1] = true; n   result[0] = 1; n   int candidate[4 + 1], counts[4 + 1], num; n   for (int i = 1; i <= N; i++) n     if (adj[i].size() != 4) { n       printf( -1 n ); n       return 0; n     } n   for (int i = 1; i < N; i++) { n     int num = 0; n     for (int j = 0; j < adj[prev].size(); j++) candidate[num++] = adj[prev][j]; n     memset(counts, 0, sizeof(counts)); n     for (int j = 0; j < num; j++) n       for (int k = 0; k < adj[candidate[j]].size(); k++) n         for (int l = 0; l < num; l++) n           if (adj[candidate[j]][k] == candidate[l]) counts[l]++; n     for (int j = 0; j < num; j++) n       if (!done[candidate[j]] && counts[j] >= 2) { n         prev = candidate[j]; n         done[prev] = true; n         result[i] = prev; n         break; n       } n   } n   for (int i = 1; i <= N; i++) n     if (!done[i]) { n       printf( -1 n ); n       return 0; n     } n   bool found = true; n   for (int i = 0; i < N; i++) n     if (!(contains(result[i], result[(i + 1) % N]) && n           contains(result[i], result[(i + 2) % N]))) n       found = false; n   if (!found) { n     printf( -1 n ); n     return 0; n   } n   for (int i = 0; i < N; i++) printf( %d  , result[i]); n   printf(  n ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const double PI = acos(-1); n struct Comp { n   double re, im; n   Comp(double x = 0.0, double y = 0.0) : re(x), im(y) {} n   Comp operator+(const Comp& rhs) const { n     return Comp(re + rhs.re, im + rhs.im); n   } n   Comp operator-(const Comp& rhs) const { n     return Comp(re - rhs.re, im - rhs.im); n   } n   Comp operator*(const Comp& rhs) const { n     return Comp(re * rhs.re - im * rhs.im, re * rhs.im + im * rhs.re); n   } n   Comp operator/(double x) const { return Comp(re / x, im / x); } n }; n int fft_len, fft_lg; n vector<int> r; n void ensure_base(int deg) { n   fft_len = 2, fft_lg = 1; n   while (fft_len <= deg) fft_len <<= 1, fft_lg++; n   r = vector<int>(fft_len, 0); n   for (int i = 0; i < fft_len; ++i) n     r[i] = (r[i >> 1] >> 1) | ((i & 1) << (fft_lg - 1)); n } n void fft(vector<Comp>& A, int dft) { n   for (int i = 0; i < fft_len; ++i) n     if (i < r[i]) swap(A[i], A[r[i]]); n   for (int mid = 1; mid < fft_len; mid <<= 1) { n     Comp wn = Comp(cos(PI / mid), sin(PI / mid) * dft); n     for (int len = mid << 1, s = 0; s < fft_len; s += len) { n       Comp w(1, 0); n       for (int k = 0; k < mid; ++k, w = w * wn) { n         Comp x = A[s + k], y = w * A[s + k + mid]; n         A[s + k] = x + y; n         A[s + k + mid] = x - y; n       } n     } n   } n   if (dft == -1) n     for (int i = 0; i < fft_len; ++i) A[i] = A[i] / fft_len; n } n void fft_pmul(vector<Comp>& A, int degA, vector<Comp>& B, int degB) { n   ensure_base(degA + degB); n   A.resize(fft_len, 0), B.resize(fft_len, 0); n   fft(A, 1); n   fft(B, 1); n   for (int i = 0; i < fft_len; ++i) A[i] = A[i] * B[i]; n   fft(A, -1); n } n int p[26], q[26]; n string s, t; n int main() { n   ios::sync_with_stdio(false); n   for (int i = 0; i < 26; ++i) cin >> q[i], p[q[i] - 1] = i; n   cin >> t >> s; n   int n = s.length(), m = t.length(); n   reverse(t.begin(), t.end()); n   vector<Comp> ps1(n), ps2(n), ps3(n), pt1(m), pt2(m), pt3(m); n   vector<double> p0(n); n   for (int i = 0; i < n; ++i) { n     int v = s[i] -  a ; n     ps3[i].re = -(2 * v + 2 * p[v]); n     ps2[i].re = v * v + 4 * v * p[v] + p[v] * p[v]; n     ps1[i].re = -2 * v * p[v] * (v + p[v]); n     p0[i] = v * v * p[v] * p[v]; n     if (i) p0[i] += p0[i - 1]; n   } n   double t4 = 0; n   for (int i = 0; i < m; ++i) { n     int v = t[i] -  a ; n     pt3[i].re = v * v * v; n     pt2[i].re = v * v; n     pt1[i].re = v; n     t4 += v * v * v * v; n   } n   fft_pmul(ps3, n, pt3, m); n   fft_pmul(ps2, n, pt2, m); n   fft_pmul(ps1, n, pt1, m); n   for (int i = m - 1; i < n; ++i) { n     double res = t4 + ps3[i].re + ps2[i].re + ps1[i].re + p0[i] - n                  (i - m < 0 ? 0 : p0[i - m]); n     putchar( 0  + (fabs(res) < 0.8)); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int a[110], b[110]; n int main() { n   int n, m, i, j; n   cin >> n >> m; n   for (i = 1; i <= m; i++) cin >> a[i]; n   for (i = 1; i <= m; i++) n     for (j = a[i]; b[j] == 0; j++) b[j] = a[i]; n   for (i = 1; i <= n; i++) cout << b[i] <<    ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long a[100100], b[100100]; n int main() { n   ios::sync_with_stdio(0), cin.tie(0), cout.tie(0); n   long long n, m, cl, ce, v, x1, y1, x2, y2, q, mi; n   cin >> n >> m >> cl >> ce >> v; n   for (long long i = 0; i < cl; i++) cin >> a[i]; n   for (long long i = 0; i < ce; i++) cin >> b[i]; n   cin >> q; n   while (q--) { n     mi = 1e18; n     cin >> x1 >> y1 >> x2 >> y2; n     swap(x1, y1); n     swap(x2, y2); n     if (x1 > x2) swap(x1, x2), swap(y1, y2); n     long long t = abs(y1 - y2); n     if (t == 0) { n       cout << abs(x1 - x2) << endl; n       continue; n     } n     long long p = lower_bound(a, a + cl, x1) - a; n     if (p != cl) { n       if (a[p] <= x2) n         mi = min(mi, x2 - x1 + t); n       else n         mi = min(mi, a[p] - x1 + a[p] - x2 + t); n     } n     p = upper_bound(a, a + cl, x1) - a; n     if (p != 0) mi = min(mi, x2 - a[p - 1] + x1 - a[p - 1] + t); n     p = lower_bound(b, b + ce, x1) - b; n     if (p != ce) { n       if (b[p] <= x2) n         mi = min(mi, x2 - x1 + (t % v == 0 ? (t / v) : (t / v + 1))); n       else n         mi = min(mi, n                  b[p] - x2 + b[p] - x1 + (t % v == 0 ? (t / v) : (t / v + 1))); n     } n     p = upper_bound(b, b + ce, x1) - b; n     if (p != 0) n       mi = min(mi, x2 - b[p - 1] + x1 - b[p - 1] + n                        (t % v == 0 ? (t / v) : (t / v + 1))); n     cout << mi << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(NULL); n   cin.tie(NULL); n   string com; n   cin >> com; n   string s[5]; n   for (int i = 0; i < 5; i++) { n     cin >> s[i]; n     if (s[i][0] == com[0] || s[i][1] == com[1]) { n       cout <<  YES ; n       return 0; n     } n   } n   cout <<  NO ; n } n


#include <bits/stdc++.h> n using namespace std; n vector<int> adj[200001]; n bool vis[200001]; n int en = -1; n int a[200005], b[200005]; n void dfs(int x) { n   vis[x] = 1; n   for (auto it = adj[x].begin(); it != adj[x].end(); ++it) { n     if (!vis[*it]) { n       dfs(*it); n     } n   } n   if (en == -1) en = x; n } n int main() { n   int n, m, i, k, l = 0, ans = 0; n   cin >> n; n   for (i = 1; i <= n; i++) { n     cin >> a[i]; n     adj[i].push_back(a[i]); n   } n   int flag = 0; n   for (i = 1; i <= n; i++) { n     cin >> b[i]; n     if (b[i]) flag++; n   } n   if (!flag) n     ans++; n   else if (flag % 2 == 0) n     ans++; n   int temp = -1; n   for (i = 1; i <= n; i++) { n     if (!vis[i]) { n       temp = en; n       en = -1; n       dfs(i); n       adj[en].clear(); n       if (temp > 0) { n         ans++; n         adj[temp].push_back(i); n       } n     } n   } n   if (temp != -1) { n     adj[en].push_back(1); n     ans++; n   } n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, p; n long long fac[250005], ans; n int main() { n   scanf( %d%d , &n, &p); n   fac[0] = 1; n   for (int i = 1; i <= n; i++) fac[i] = fac[i - 1] * i % p; n   for (int i = 1; i <= n; i++) n     ans = (ans + fac[n - i + 1] * (n - i + 1) % p * fac[i] % p) % p; n   printf( %lld , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long n; n   cin >> n; n   long long s[n]; n   for (long long c = 0; c < n; c++) cin >> s[c]; n   long long sum = 0; n   long long nextAllowedNum = 10000000000000 + 33; n   for (long long c = n - 1; c >= 0; c--) { n     if (nextAllowedNum <= 0) break; n     long long takeval = 0; n     takeval = s[c] <= nextAllowedNum ? s[c] : nextAllowedNum; n     nextAllowedNum = takeval - 1; n     sum += takeval; n   } n   cout << sum << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void vin(vector<long long int> &v, long long int t) { n   for (long long int i = 0; i < t; i++) cin >> v[i]; n } n void vout(vector<long long int> &v, long long int t) { n   for (long long int i = 0; i < t; i++) cout << v[i] <<    ; n   cout << endl; n } n void ingraph(vector<vector<long long int>> &graph, long long int m) { n   long long int x, y; n   for (long long int i = 0; i < m; i++) { n     cin >> x >> y; n     x--, y--; n     graph[x].push_back(y); n     graph[y].push_back(x); n   } n } n bool sortbysec(const pair<int, int> &a, const pair<int, int> &b) { n   return (a.second < b.second); n } n long long int gcd(long long int a, long long int b) { n   if (b == 0) return a; n   return gcd(b, a % b); n } n long long int lcm(long long int a, long long int b) { n   return a * b / gcd(a, b); n } n void solve() { n   long long int n, m, x, y; n   cin >> n >> m >> x >> y; n   long long int cost = 0; n   y = min(y, 2 * x); n   for (long long int i = 0; i < n; i++) { n     string s; n     cin >> s; n     for (long long int j = 0; j < m; j++) { n       if (s[j] ==  . ) { n         if (j != m - 1 && s[j + 1] ==  . ) n           cost += y, j++; n         else n           cost += x; n       } n     } n   } n   cout << cost <<   n ; n } n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   long long int t; n   cin >> t; n   while (t--) solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n inline int gi() { n   char c = getchar(); n   int x = 0, f = 1; n   for (; c <  0  || c >  9 ; c = getchar()) n     if (c ==  - ) f = -1; n   for (; c >=  0  && c <=  9 ; c = getchar()) x = (x << 1) + (x << 3) + c -  0 ; n   return x * f; n } n const int N = 5e5 + 5, Mod = 1e9 + 7; n int x[N], y[N], n; n long long ara, sx[N], sy[N], ssum[N], ans; n long long sum[N]; n long long cross(int i, int j) { return 1ll * x[i] * y[j] - 1ll * x[j] * y[i]; } n long long cross(long long x1, long long y1, long long x2, long long y2, n                 bool b = true) { n   if (!b) return x1 * y2 - x2 * y1; n   x1 %= Mod, y1 %= Mod; n   return (x1 * y2 - x2 * y1) % Mod; n } n int main() { n   n = gi(); n   for (int i = 1; i <= n; ++i) x[i] = gi(), y[i] = gi(); n   for (int i = 3; i <= n; ++i) n     ara += cross(x[i] - x[1], y[i] - y[1], x[i - 1] - x[1], y[i - 1] - y[1], 0); n   for (int i = 2; i <= n; ++i) n     sum[i] = sum[i - 1] + cross(i, i - 1), n     ssum[i] = (ssum[i - 1] + sum[i]) % Mod; n   for (int i = 1; i <= n; ++i) n     sx[i] = sx[i - 1] + x[i], sy[i] = sy[i - 1] + y[i]; n   for (int i = 1, j = 0; i <= n; ++i) { n     for (j = max(j, i + 2); j <= n - (i == 1); ++j) n       if (2ull * (sum[j] - sum[i] - cross(j, i)) > ara) break; n     const int m = n - (i == 1); n     int tmp = 0; n     tmp = (ssum[j - 1] - ssum[i + 1] - sum[i] % Mod * (j - i - 2)) % Mod; n     tmp = (tmp - n            cross(sx[j - 1] - sx[i + 1], sy[j - 1] - sy[i + 1], x[i], y[i])) % n           Mod; n     ans = (ans + ara % Mod * (j - i - 2) % Mod - 2 * tmp % Mod + Mod) % Mod; n     if (j > m) continue; n     tmp = (ssum[m] - ssum[j - 1] - sum[i] % Mod * (m - j + 1)) % Mod; n     tmp = (tmp - cross(sx[m] - sx[j - 1], sy[m] - sy[j - 1], x[i], y[i])) % Mod; n     ans = ((ans + tmp * 2 - ara % Mod * (m - j + 1) % Mod) % Mod + Mod) % Mod; n   } n   printf( %lld n , ans); n } n


#include <bits/stdc++.h> n using namespace std; n const long long N = 2e5 + 7, inf = 1e9; n long long n, m, cnt, ans; n long long a[N], b[N], c[N], tr[N]; n long long lowbit(long long x) { return x & -x; } n long long query(long long x) { n   long long sum = 0; n   for (; x; x -= lowbit(x)) sum += tr[x]; n   return sum; n } n void add(long long x) { n   for (; x <= cnt; x += lowbit(x)) tr[x]++; n } n long long check(long long x) { n   long long sum = 0; n   add(lower_bound(b + 1, b + cnt + 1, 0) - b); n   for (long long i = 1; i <= n; i++) { n     if (a[i] - x < b[1]) n       sum += i; n     else if (a[i] - x > b[cnt]) n       ; n     else { n       long long pos = lower_bound(b + 1, b + cnt + 1, a[i] - x) - b; n       if (b[pos] > a[i] - x) pos--; n       sum += i - query(pos); n     } n     add(c[i]); n   } n   return sum; n } n signed main() { n   scanf( %lld%lld , &n, &m); n   for (long long i = 1; i <= n; i++) scanf( %lld , &a[i]); n   for (long long i = 1; i <= n; i++) a[i] = a[i - 1] + a[i], b[i] = a[i]; n   b[n + 1] = 0; n   sort(b + 1, b + n + 2); n   cnt = unique(b + 1, b + n + 2) - b - 1; n   for (long long i = 1; i <= n; i++) n     c[i] = lower_bound(b + 1, b + cnt + 1, a[i]) - b; n   printf( %lld n , check(m)); n } n


#include <bits/stdc++.h> n using namespace std; n const long long maxn = 1e6 + 10; n long long n, k, A[maxn]; n int32_t main() { n   ios_base::sync_with_stdio(false); n   cin >> n; n   for (long long i = 0; i < n; i++) { n     long long a, b, c, d; n     cin >> a >> b >> c >> d; n     if (a > b * c) { n       cout << -1 << endl; n       continue; n     } n     long long e = (a + d * b - 1) / (d * b); n     cout << e * a - (e * (e - 1ll) / 2ll * d * b) << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n struct Boot { n   int _pos; n   char _char; n }; n struct PairBoots { n   Boot _left; n   Boot _right; n }; n int main() { n   int n; n   string leftBoots; n   string rightBoots; n   vector<vector<Boot>> leftBootsBucket(27); n   vector<vector<Boot>> rightBootsBucket(27); n   vector<PairBoots> ans; n   cin >> n; n   cin.ignore(); n   getline(cin, leftBoots); n   getline(cin, rightBoots); n   for (int i = 0; i < n; i++) { n     Boot leftBoot; n     leftBoot._pos = i; n     leftBoot._char = leftBoots[i]; n     if (leftBoot._char ==  ? ) n       leftBootsBucket[26].push_back(leftBoot); n     else n       leftBootsBucket[leftBoots[i] -  a ].push_back(leftBoot); n     Boot rightBoot; n     rightBoot._pos = i; n     rightBoot._char = rightBoots[i]; n     if (rightBoot._char ==  ? ) n       rightBootsBucket[26].push_back(rightBoot); n     else n       rightBootsBucket[rightBoots[i] -  a ].push_back(rightBoot); n   } n   for (int i = 0; i < 27; i++) { n     for (int j = 0; n          j < leftBootsBucket[i].size() || j < rightBootsBucket[i].size(); j++) { n       PairBoots temp; n       if (j < leftBootsBucket[i].size() && j < rightBootsBucket[i].size()) { n         temp._left = leftBootsBucket[i][j]; n         temp._right = rightBootsBucket[i][j]; n       } else { n         if (j < leftBootsBucket[i].size()) { n           if (rightBootsBucket[26].size() != 0) { n             temp._left = leftBootsBucket[i][j]; n             temp._right = rightBootsBucket[26][rightBootsBucket[26].size() - 1]; n             rightBootsBucket[26].pop_back(); n             goto here; n           } n         } n         if (j < rightBootsBucket[i].size()) { n           if (leftBootsBucket[26].size() != 0) { n             temp._left = leftBootsBucket[26][leftBootsBucket[26].size() - 1]; n             temp._right = rightBootsBucket[i][j]; n             leftBootsBucket[26].pop_back(); n             goto here; n           } n         } n         break; n       } n     here: n       ans.push_back(temp); n     } n   } n   cout << ans.size() <<   n ; n   for (auto i : ans) { n     cout << i._left._pos + 1 <<     << i._right._pos + 1 <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n bool choice_first(std::vector<size_t> a, std::vector<size_t> b) { n   uint64_t sum_a, sum_b; n   sum_a = sum_b = 0; n   for (auto elem : a) { n     sum_a += elem; n   } n   for (auto elem : b) { n     sum_b += elem; n   } n   return sum_a > sum_b || (sum_a == sum_b && a < b); n } n int main() { n   std::ios_base::sync_with_stdio(false); n   std::cin.tie(0); n   std::cout.tie(0); n   size_t n, m; n   std::cin >> n; n   std::vector<std::vector<std::vector<size_t>>> dp( n       n + 1, std::vector<std::vector<size_t>>(n + 1)); n   std::vector<size_t> a(n); n   for (size_t i = 0; i < n; i++) { n     std::cin >> a[i]; n   } n   std::cin >> m; n   for (size_t i = 0; i < n; i++) { n     if (i == 0) { n       dp[1][i] = {a[0]}; n       continue; n     } n     dp[1][i] = std::max(std::vector<size_t>{a[i]}, dp[1][i - 1]); n   } n   for (size_t i = 2; i <= n; i++) { n     for (size_t j = i - 1; j < n; j++) { n       if (j == i - 1) { n         std::vector<size_t> cur = dp[i - 1][j - 1]; n         cur.push_back(a[j]); n         dp[i][j] = cur; n         continue; n       } n       std::vector<size_t> first, second; n       first = dp[i - 1][j - 1]; n       first.push_back(a[j]); n       second = dp[i][j - 1]; n       if (choice_first(first, second)) { n         dp[i][j] = first; n       } else { n         dp[i][j] = second; n       } n     } n   } n   for (size_t i = 0; i < m; i++) { n     size_t k, pos; n     std::cin >> k >> pos; n     std::cout << dp[k][n - 1][pos - 1] <<   n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T> n inline T abs(T zz) { n   return zz < 0 ? -zz : zz; n } n template <typename T> n inline T sqr(T zz) { n   return zz * zz; n } n const int MAGIC = 1000500; n vector<int> used; n vector<int> primes; n bool is_prime(int n) { n   for (int p : primes) { n     if (p * p > n) break; n     if (n % p == 0) return false; n   } n   return true; n } n int main() { n   used = vector<int>(MAGIC, 0); n   for (int i = 2; i < MAGIC; ++i) { n     if (!used[i]) { n       primes.push_back(i); n       for (int j = i; j < MAGIC; j += i) { n         used[j] = true; n       } n     } n   } n   int n; n   cin >> n; n   if (is_prime(n) == 1) { n     cout << 1 << endl; n     return 0; n   } n   if (n % 2 == 0) { n     cout << 2 << endl; n     return 0; n   } n   if (is_prime(n - 2)) { n     cout << 2 << endl; n     return 0; n   } n   cout << 3 << endl; n   return 0; n } n


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


#include <bits/stdc++.h> n using namespace std; n double R, r; n struct Punct { n   double x, y; n }; n Punct A, B; n inline double Dist(Punct A, Punct B) { n   return sqrt((A.x - B.x) * (A.x - B.x) + (A.y - B.y) * (A.y - B.y)); n } n int main() { n   double d; n   cin >> A.x >> A.y >> R; n   cin >> B.x >> B.y >> r; n   d = Dist(A, B); n   if (R >= r) { n     if (d + r <= R) { n       d = R - d - r; n       d /= 2.0; n     } else { n       if (d <= R + r) n         d = 0.0; n       else { n         d = d - R - r; n         d /= 2.0; n       } n     } n   } else { n     if (d + R <= r) { n       d = r - d - R; n       d /= 2.0; n     } else { n       if (d <= R + r) n         d = 0.0; n       else { n         d = d - R - r; n         d /= 2.0; n       } n     } n   } n   printf( %.6lf n , d); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void smain(); n int main() { n   ios_base::sync_with_stdio(0); n   cout.precision(12); n   cout << fixed; n   smain(); n   return 0; n } n int n; n int ans[1000001]; n int f[1000001]; n inline int sum(int r) { n   int result = 0; n   for (; r >= 0; r = (r & (r + 1)) - 1) result += f[r]; n   return result; n } n inline void inc(int i, int delta) { n   for (; i < n; i = (i | (i + 1))) f[i] += delta; n } n void solve(vector<pair<int, pair<int, int> > > &q) { n   vector<int> ts; n   ts.reserve(q.size()); n   for (auto it : q) ts.push_back(it.first); n   sort(ts.begin(), ts.end()); n   ts.resize(unique(ts.begin(), ts.end()) - ts.begin()); n   int m = ts.size(); n   memset(f, 0, sizeof(int) * m); n   for (auto it : q) { n     int tp = it.second.first; n     int t = lower_bound(ts.begin(), ts.end(), it.first) - ts.begin(); n     if (tp == 1) { n       inc(t, 1); n     } else if (tp == 2) { n       inc(t, -1); n     } else { n       ans[it.second.second] = sum(t); n     } n   } n } n void smain() { n   for (; scanf( %d , &n) != EOF;) { n     map<int, vector<pair<int, pair<int, int> > > > q; n     for (int i = 0; i < n; i++) { n       int a, t, x; n       scanf( %d %d %d , &a, &t, &x); n       q[x].emplace_back(t, make_pair(a, i)); n     } n     memset(ans, 255, sizeof(int) * n); n     for (auto it : q) solve(it.second); n     for (int i = 0; i < n; i++) n       if (ans[i] != -1) printf( %d n , ans[i]); n   } n } n


#include <bits/stdc++.h> n using namespace std; n int dx[] = {0, 0, -1, 1}; n int dy[] = {-1, 1, 0, 0}; n const int N = 1e6 + 5; n long long ans, sum, d[N]; n int n, m; n int main() { n   int a, b; n   while (~scanf( %d%d , &n, &m)) { n     memset(d, 0, sizeof(d)); n     for (int i = 0; i < (m); ++i) { n       scanf( %d%d , &a, &b); n       d[a]++, d[b]++; n     } n     if (n < 3) { n       puts( 0 ); n       continue; n     } n     ans = (long long)n * (n - 1) * (n - 2) / 6; n     sum = 0; n     for (int i = (1); i <= (n); ++i) sum += d[i] * (n - 1 - d[i]); n     sum /= 2; n     printf( %lld n , ans - sum); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long n, a[200009], ans = 1, b[200009], sum = 1; n bool vis[200009]; n void dfs(long long x) { n   if (vis[x]) return; n   vis[x] = 1; n   dfs(a[x]); n } n int main() { n   cin >> n; n   for (int i = 1; i <= n; i++) cin >> a[i]; n   for (int i = 1; i <= n; i++) cin >> b[i]; n   dfs(1); n   for (int i = 1; i <= n; i++) { n     if (vis[i]) continue; n     sum++; n     dfs(i); n   } n   bool f = 0, g = 0; n   for (int i = 1; i <= n; i++) { n     ans += b[i]; n   } n   ans %= 2; n   if (sum != 1) ans += sum; n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int dp[2][105]; n int main() { n   int c, w, h, c2; n   cin >> c >> w >> h; n   c2 = (c + 1) >> 1; n   int *src = dp[0], *dst = dp[1]; n   src[0] = 1; n   for (int i = 1; i <= w; i++) src[i] = 0; n   while (c2--) { n     dst[0] = src[0]; n     for (int i = 1; i <= w; i++) { n       dst[i] = src[i - 1] * h % 1000003; n       dst[0] += src[i]; n     } n     dst[0] %= 1000003; n     swap(src, dst); n   } n   int ans = 0, sum = 0; n   if ((c & 1) == 0) { n     for (int i = 0; i <= w; i++) { n       sum += src[i]; n       ans = (ans + (long long)sum * src[w - i]) % 1000003; n     } n   } else { n     for (int i = 0; i <= w; i++) { n       sum += dst[i]; n       ans = (ans + (long long)sum * src[w - i]) % 1000003; n     } n   } n   cout << ans % 1000003 << endl; n   return 0; n } n


#include <bits/stdc++.h> n const int N = 1e6 + 10; n const int mod = 1e9 + 7; n const int INF = 0x3f3f3f3f; n using namespace std; n long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; } n long long lcm(long long a, long long b) { return a / gcd(a, b) * b; } n int n, m; n long long f[N]; n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   f[1] = 2, f[2] = 4; n   cin >> n >> m; n   for (int i = 3; i <= max(n, m); ++i) f[i] = f[i - 1] % mod + f[i - 2] % mod; n   cout << (f[n] + f[m] - 2) % mod <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 200010; n const int px[] = {0, 0, 1, -1}, py[] = {1, -1, 0, 0}; n int n, m, a[1010][1010], tx[N], ty[N], f[N], ch[N][2], rev[N], tr[N * 4], n     cnt[N * 4], tag[N * 4]; n long long ans; n bool isroot(int x) { n   return !x || !f[x] || (ch[f[x]][0] != x && ch[f[x]][1] != x); n } n int which(int x) { return ch[f[x]][1] == x; } n void rotate(int x) { n   int fx = f[x], ffx = f[fx], d = which(x); n   if (!isroot(fx)) ch[ffx][which(fx)] = x; n   f[x] = f[fx]; n   ch[fx][d] = ch[x][d ^ 1]; n   f[ch[x][d ^ 1]] = fx; n   ch[x][d ^ 1] = fx; n   f[fx] = x; n   return; n } n void rever(int x) { n   rev[x] ^= 1; n   swap(ch[x][0], ch[x][1]); n   return; n } n void pushdown(int x) { n   if (rev[x]) { n     if (ch[x][0]) rever(ch[x][0]); n     if (ch[x][1]) rever(ch[x][1]); n     rev[x] = 0; n   } n   return; n } n void push(int x) { n   if (!isroot(x)) push(f[x]); n   pushdown(x); n   return; n } n void splay(int x) { n   push(x); n   while (!isroot(x)) { n     int fx = f[x]; n     if (!isroot(fx)) { n       if (which(x) == which(fx)) n         rotate(fx); n       else n         rotate(x); n     } n     rotate(x); n   } n   return; n } n void access(int x) { n   for (int y = 0; x; y = x, x = f[x]) { n     splay(x); n     ch[x][1] = y; n   } n   return; n } n void makeroot(int x) { n   access(x); n   splay(x); n   rever(x); n   return; n } n void link(int x, int y) { n   makeroot(x); n   f[x] = y; n   return; n } n int getroot(int x) { n   access(x); n   splay(x); n   while (ch[x][0]) x = ch[x][0]; n   return x; n } n void cut(int x, int y) { n   makeroot(x); n   access(y); n   splay(y); n   if (!ch[x][1] && ch[y][0] == x) f[x] = ch[y][0] = 0; n   return; n } n bool check(int x, int y) { n   makeroot(x); n   access(y); n   splay(y); n   while (f[x]) x = f[x]; n   return x == y; n } n void sgtpushdown(int o) { n   if (tag[o]) { n     tr[o << 1] += tag[o]; n     tr[o << 1 | 1] += tag[o]; n     tag[o << 1] += tag[o]; n     tag[o << 1 | 1] += tag[o]; n     tag[o] = 0; n   } n   return; n } n void pushup(int o) { n   if (tr[o << 1] == tr[o << 1 | 1]) { n     tr[o] = tr[o << 1]; n     cnt[o] = cnt[o << 1] + cnt[o << 1 | 1]; n   } else if (tr[o << 1] < tr[o << 1 | 1]) { n     tr[o] = tr[o << 1]; n     cnt[o] = cnt[o << 1]; n   } else { n     tr[o] = tr[o << 1 | 1]; n     cnt[o] = cnt[o << 1 | 1]; n   } n   return; n } n void build(int o, int l, int r) { n   tr[o] = 0; n   cnt[o] = r - l + 1; n   if (l == r) return; n   int mid = (l + r) >> 1; n   build(o << 1, l, mid); n   build(o << 1 | 1, mid + 1, r); n   return; n } n void update(int o, int l, int r, int L, int R, int v) { n   if (L <= l && r <= R) { n     tag[o] += v; n     tr[o] += v; n     return; n   } n   sgtpushdown(o); n   int mid = (l + r) >> 1; n   if (L <= mid) update(o << 1, l, mid, L, R, v); n   if (R > mid) update(o << 1 | 1, mid + 1, r, L, R, v); n   pushup(o); n   return; n } n int query(int o, int l, int r, int L, int R) { n   if (tr[o] > 1) return 0; n   if (L <= l && r <= R) return tr[o] * cnt[o]; n   sgtpushdown(o); n   int mid = (l + r) >> 1, ret = 0; n   if (L <= mid) ret += query(o << 1, l, mid, L, R); n   if (R > mid) ret += query(o << 1 | 1, mid + 1, r, L, R); n   return ret; n } n int main() { n   scanf( %d%d , &n, &m); n   for (int i = 1; i <= n; i++) n     for (int j = 1; j <= m; j++) { n       scanf( %d , a[i] + j); n       tx[a[i][j]] = i; n       ty[a[i][j]] = j; n     } n   int M = n * m; n   build(1, 1, M); n   for (int l = n * m, r = l; l >= 1; l--) { n     int x = tx[l], y = ty[l]; n     for (int i = 0; i <= 3; i++) { n       int nx = x + px[i], ny = y + py[i]; n       if (nx <= n && nx >= 1 && ny <= m && ny >= 1) { n         while (a[nx][ny] >= l && a[nx][ny] <= r && check(a[x][y], a[nx][ny])) { n           for (int j = 0; j <= 3; j++) { n             int mx = tx[r] + px[j], my = ty[r] + py[j]; n             if (mx <= n && mx >= 1 && my <= m && my >= 1) n               cut(a[tx[r]][ty[r]], a[mx][my]); n           } n           r--; n         } n         if (a[nx][ny] >= l && a[nx][ny] <= r) { n           link(a[x][y], a[nx][ny]); n           update(1, 1, M, a[nx][ny], r, -1); n         } n       } n     } n     update(1, 1, M, l, r, 1); n     ans += query(1, 1, M, l, r); n   } n   printf( %lld n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct lhy { n   int x, y, l, r, id; n } b[400040]; n int n, m, mN, ans; n int cnt[400040], a[400040], Ans[400040], c[2 * 400040], C[2 * 400040], n     L[400040], R[400040], x[2 * 400040]; n inline void add(int x, int o) { n   for (; x <= mN; x += x & -x) c[x] = max(c[x], o); n } n inline int ask(int x) { n   int nowans = 0; n   for (; x; x -= x & -x) nowans = max(nowans, c[x]); n   return nowans; n } n inline void Add(int x, int o) { n   for (; x; x -= x & -x) C[x] = max(C[x], o); n } n inline int Ask(int x) { n   int nowans = 0; n   for (; x <= mN; x += x & -x) nowans = max(nowans, C[x]); n   return nowans; n } n inline int cmp(const lhy &a, const lhy &b) { return a.x < b.x; } n int main() { n   scanf( %d%d , &n, &m); n   for (int i = 1; i <= n; i++) scanf( %d , &a[i]), x[++x[0]] = a[i]; n   for (int i = 1; i <= m; i++) n     scanf( %d%d , &b[i].x, &b[i].y), b[i].id = i, x[++x[0]] = b[i].y; n   sort(x + 1, x + 1 + x[0]); n   mN = unique(x + 1, x + 1 + x[0]) - x - 1; n   for (int i = 1; i <= n; i++) a[i] = lower_bound(x + 1, x + 1 + mN, a[i]) - x; n   for (int i = 1; i <= m; i++) n     b[i].y = lower_bound(x + 1, x + 1 + mN, b[i].y) - x; n   sort(b + 1, b + 1 + m, cmp); n   int now = 1; n   for (int i = 1; i <= n; i++) { n     L[i] = ask(a[i] - 1); n     while (b[now].x == i) { n       b[now].l = ask(b[now].y - 1); n       now++; n     } n     add(a[i], L[i] + 1); n   } n   now = m; n   for (int i = n; i; i--) { n     R[i] = Ask(a[i] + 1); n     while (b[now].x == i) { n       b[now].r = Ask(b[now].y + 1); n       now--; n     } n     Add(a[i], R[i] + 1); n   } n   for (int i = 1; i <= n; i++) ans = max(ans, L[i] + R[i] + 1); n   for (int i = 1; i <= n; i++) n     if (L[i] + R[i] + 1 == ans) cnt[L[i]]++; n   for (int i = 1; i <= m; i++) n     if (L[b[i].x] + R[b[i].x] + 1 < ans || cnt[L[b[i].x]] > 1) n       Ans[b[i].id] = max(ans, b[i].l + b[i].r + 1); n     else n       Ans[b[i].id] = max(ans - 1, b[i].l + b[i].r + 1); n   for (int i = 1; i <= m; i++) printf( %d n , Ans[i]); n } n


#include <bits/stdc++.h> n using namespace std; n long long n, a[100069], dv; n int main() { n   long long i, j, z = 1; n   scanf( %lld%lld , &n, &dv); n   if (n > dv) { n     printf( 0 n ); n     return 0; n   } n   for (i = 1; i <= n; i++) { n     scanf( %lld , a + i); n     for (j = 1; j < i; j++) { n       z = z * abs(a[i] - a[j]) % dv; n     } n   } n   printf( %lld n , z); n } n


#include <bits/stdc++.h> n using namespace std; n int f(string s, int i) { n   if (s[i] ==  ( ) return 0; n   if (s[i] ==  [ ) return 1; n   if (s[i] ==  8 ) return 2; n } n int main() { n   int a, b; n   string s1, s2; n   cin >> s1 >> s2; n   a = b = 0; n   for (int i = 0; i < s1.size(); i += 2) { n     if ((f(s1, i) == 0 && f(s2, i) == 2) || (f(s1, i) == 1 && f(s2, i) == 0) || n         (f(s1, i) == 2 && f(s2, i) == 1)) n       a++; n     else if (f(s1, i) != f(s2, i)) n       b++; n   } n   if (a == b) cout <<  TIE ; n   if (a > b) cout <<  TEAM 1 WINS ; n   if (b > a) cout <<  TEAM 2 WINS ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int mod = 1e9 + 7; n const int max_n = 200005; n vector<pair<int, int>> g[max_n]; n int sz[max_n]; n long long dis[max_n]; n void dfs(int x) { n   sz[x] = 1; n   for (auto p : g[x]) { n     int u = p.first; n     if (sz[u]) continue; n     dis[u] = p.second; n     dfs(u); n     sz[x] += sz[u]; n   } n } n void solve() { n   int n; n   cin >> n; n   n *= 2; n   for (int i = 0; i <= n; i++) g[i].clear(); n   for (int i = 0; i <= n; i++) sz[i] = dis[i] = 0; n   for (int i = 1; i < n; i++) { n     int u, v, w; n     cin >> u >> v >> w; n     g[u].push_back({v, w}); n     g[v].push_back({u, w}); n   } n   dfs(1); n   long long ans1 = 0, ans2 = 0; n   for (int i = 2; i <= n; i++) { n     if (sz[i] & 1) ans1 += dis[i]; n     ans2 += min(sz[i], n - sz[i]) * dis[i]; n   } n   cout << ans1 <<     << ans2 << endl; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(nullptr); n   int t; n   cin >> t; n   while (t--) solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long n, i, tmp_ara1, tmp_ara2; n   vector<long long> ara1(2000007, -1), ara2(2000007, -1), ans(200007, -1), n       in_front; n   cin >> n; n   for (i = 1; i <= n; i++) { n     cin >> tmp_ara1 >> tmp_ara2; n     ara1[tmp_ara1] = tmp_ara2; n     ara2[tmp_ara2] = tmp_ara1; n     in_front.push_back(tmp_ara1); n   } n   long long key = 0; n   for (i = 2; i <= n; i += 2) { n     ans[i] = ara1[key]; n     key = ara1[key]; n   } n   long long first; n   for (i = 0; i < n; i++) { n     if (ara2[in_front[i]] == -1) { n       first = in_front[i]; n       break; n     } n   } n   key = first; n   ans[1] = first; n   for (i = 3; i <= n; i += 2) { n     ans[i] = ara1[key]; n     key = ara1[key]; n   } n   for (i = 1; i <= n; i++) { n     cout << ans[i] <<    ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m; n char gird[64][64]; n set<long long int> set_of_cols; n int main(void) { n   int i, j; n   scanf( %d%d , &n, &m); n   for (i = 0; i < n; ++i) scanf( %s , gird[i]); n   for (i = 0; i < n; ++i) { n     long long int id = 0; n     for (j = 0; j < m; ++j) { n       id <<= 1; n       id |= ( #  == gird[i][j]) ? 1 : 0; n     } n     for (auto have : set_of_cols) { n       if (have != id && ((have & id) != 0)) { n         puts( No ); n         return 0; n       } n     } n     set_of_cols.insert(id); n   } n   puts( Yes ); 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   string str; n   cin >> str; n   int t; n   cin >> t; n   int type, pos, l, r; n   char c; n   set<int> s[26]; n   for (int i = 0; i < str.length(); i++) s[str[i] -  a ].insert(i + 1); n   while (t--) { n     cin >> type; n     if (type == 1) { n       cin >> pos >> c; n       s[str[pos - 1] -  a ].erase(pos); n       s[c -  a ].insert(pos); n       str[pos - 1] = c; n     } else { n       cin >> l >> r; n       int count = 0; n       for (int i = 0; i < 26; i++) n         if (s[i].lower_bound(l) != s[i].upper_bound(r)) count++; n       cout << count << endl; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n bool check(char a) { n   if (a ==  a  || a ==  e  || a ==  i  || a ==  o  || a ==  u ) return true; n   if (a ==  1  || a ==  3  || a ==  5  || a ==  7  || a ==  9 ) return true; n   return false; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   string s; n   cin >> s; n   int ans = 0; n   for (int i = 0; i < s.length(); i++) n     if (check(s[i])) ans++; n   cout << ans; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int t; n   string s; n   getline(cin, s); n   long long n = s.size(), i; n   for (i = n - 1; i >= 0; i--) { n     if (isupper(s[i])) { n       s[i] = tolower(s[i]); n     } n     if (s[i] ==  ?  || s[i] ==    ) { n       continue; n     } else if (s[i] ==  a  || s[i] ==  e  || s[i] ==  i  || s[i] ==  o  || n                s[i] ==  u  || s[i] ==  y ) { n       cout <<  YES  << endl; n       break; n     } else { n       cout <<  NO  << endl; n       break; n     } n   } n } n


#include <bits/stdc++.h> n long long inp() { n   char c = getchar(); n   int neg = 1; n   while (c <  0  || c >  9 ) { n     if (c ==  - ) neg = -1; n     c = getchar(); n   } n   long long sum = 0; n   while (c >=  0  && c <=  9 ) { n     sum = sum * 10 + c -  0 ; n     c = getchar(); n   } n   return sum * neg; n } n long long v[300010], mask[300010]; n int main() { n   int n = inp(); n   long long sum = 0; n   for (int i = 1; i <= n; i++) { n     v[i] = inp(); n     sum += v[i]; n     mask[i] = inp(); n   } n   if (sum < 0) n     for (int i = 1; i <= n; i++) v[i] *= -1; n   long long s = 0; n   for (int i = 0; i <= 61; i++) { n     long long sum = 0; n     for (int j = 1; j <= n; j++) { n       if (mask[j] & (1ll << i)) { n         if (mask[j] <= (1ll << (i + 1))) { n           if (__builtin_popcountll(s & mask[j]) & 1) n             sum += v[j]; n           else n             sum -= v[j]; n         } n       } n     } n     if (sum < 0) s |= (1ll << i); n   } n   std::cout << s << std::endl; n } n


#include <bits/stdc++.h> n using namespace std; n pair<int, int> s[100010]; n int a[100010], b[100010]; n int main() { n   int N, P, i, x; n   scanf( %d n , &N); n   for (i = 0; i < N; i++) { n     scanf( %d , &x); n     s[i] = make_pair(x, i); n   } n   sort(s, s + N); n   P = N / 3; n   for (i = 0; i < P; i++) { n     a[s[i].second] = s[i].first; n     b[s[i].second] = 0; n   } n   P = (2 * N) / 3; n   for (; i < P; i++) { n     a[s[i].second] = 0; n     b[s[i].second] = s[i].first; n   } n   for (; i < N; i++) { n     a[s[i].second] = s[i].first - (N - i - 1); n     b[s[i].second] = N - i - 1; n   } n   printf( YES n ); n   for (i = 0; i < N; i++) printf( %d  , a[i]); n   printf(  n ); n   for (i = 0; i < N; i++) printf( %d  , b[i]); n   printf(  n ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m, t[100010], nowt, pos, ans[100010]; n pair<int, int> a[100010]; n int main() { n   scanf( %d%d , &n, &m); n   for (int i = 1; i <= n; ++i) { n     scanf( %d%d , &t[i], &a[i].first); n     a[i].second = i; n   } n   for (int i = 1, j; i <= n; i = j + 1) { n     j = min(n, i + m - 1); n     sort(a + i, a + j + 1); n     nowt = max(nowt, t[j]); n     pos = 0; n     for (int k = i, l; k <= j; k = l) { n       l = k; n       while (l <= j && a[l].first == a[k].first) ++l; n       nowt += a[k].first - pos; n       for (int h = k; h < l; ++h) ans[a[h].second] = nowt; n       nowt += 1 + (l - k) / 2; n       pos = a[k].first; n     } n     nowt += pos; n   } n   for (int i = 1; i <= n; ++i) printf( %d  , ans[i]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long fastPow(long long a, long long b) { n   long long ret = 1; n   while (b) { n     if (b & 1) ret *= a; n     a *= a; n     b >>= 1; n   } n   return ret; n } n int main() { n   ios::sync_with_stdio(false), cin.tie(0), cout.tie(0); n   long long n, e, o, ce, co, ret = INT_MIN, c; n   cin >> n; n   vector<long long> nums(n + 1); n   for (int i = int(1); i < int(n + 1); i++) cin >> nums[i]; n   e = 0, o = 0, ce = 0, co = 0, c = 0; n   for (int l = int(1); l < int(n); l++) { n     if (!(l % 2)) { n       e = abs(nums[l] - nums[l + 1]); n       ce = max(ce + e, c); n       o = e * -1; n       co = max(co + o, c); n     } else { n       e = abs(nums[l] - nums[l + 1]) * -1; n       ce = max(ce + e, c); n       o = e * -1; n       co = max(co + o, c); n     } n     ret = max(ret, max(ce, co)); n   } n   cout << ret << endl; n   return 0; n } n


#include <bits/stdc++.h> n template <typename T> n void MACRO_VAR_Scan(T& t) { n   std::cin >> t; n } n template <typename First, typename... Rest> n void MACRO_VAR_Scan(First& first, Rest&... rest) { n   std::cin >> first; n   MACRO_VAR_Scan(rest...); n } n template <typename T> n void MACRO_VEC_ROW_Init(int n, T& t) { n   t.resize(n); n } n template <typename First, typename... Rest> n void MACRO_VEC_ROW_Init(int n, First& first, Rest&... rest) { n   first.resize(n); n   MACRO_VEC_ROW_Init(n, rest...); n } n template <typename T> n void MACRO_VEC_ROW_Scan(int p, T& t) { n   std::cin >> t[p]; n } n template <typename First, typename... Rest> n void MACRO_VEC_ROW_Scan(int p, First& first, Rest&... rest) { n   std::cin >> first[p]; n   MACRO_VEC_ROW_Scan(p, rest...); n } n template <class T> n inline T CHMAX(T& a, const T b) { n   return a = (a < b) ? b : a; n } n template <class T> n inline T CHMIN(T& a, const T b) { n   return a = (a > b) ? b : a; n } n void CHECKTIME(std::function<void()> f) { n   auto start = std::chrono::system_clock::now(); n   f(); n   auto end = std::chrono::system_clock::now(); n   auto res = std::chrono::duration_cast<std::chrono::nanoseconds>((end - start)) n                  .count(); n   std::cerr << res / (1.0e9) <<  s n ; n } n template <class T> n using V = std::vector<T>; n template <class T> n using VV = V<V<T>>; n template <typename S, typename T> n std::ostream& operator<<(std::ostream& os, std::pair<S, T> p) { n   os <<  (  << p.first <<  ,   << p.second <<  ) ; n   return os; n } n using ll = long long; n using ull = unsigned long long; n using ld = long double; n using PAIR = std::pair<ll, ll>; n using PAIRLL = std::pair<ll, ll>; n constexpr ll INFINT = (1 << 30) - 1; n constexpr ll INFINT_LIM = (1LL << 31) - 1; n constexpr ll INFLL = 1LL << 60; n constexpr ll INFLL_LIM = (1LL << 62) - 1 + (1LL << 62); n constexpr double EPS = 1e-10; n constexpr ll MOD = 998244353; n constexpr double PI = 3.141592653589793238462643383279; n template <class T, size_t N> n void FILL(T (&a)[N], const T& val) { n   for (auto& x : a) x = val; n } n template <class ARY, size_t N, size_t M, class T> n void FILL(ARY (&a)[N][M], const T& val) { n   for (auto& b : a) FILL(b, val); n } n template <class T> n void FILL(std::vector<T>& a, const T& val) { n   for (auto& x : a) x = val; n } n template <class ARY, class T> n void FILL(std::vector<std::vector<ARY>>& a, const T& val) { n   for (auto& b : a) FILL(b, val); n } n class ModInt { n   static constexpr std::int_fast64_t M = MOD; n   using value_type = std::int_fast64_t; n  n  public: n   value_type x; n   ModInt() : x(0) {} n   ModInt(value_type n) : x(n % M) { n     if (x < 0) x += M; n   } n   const std::int_fast64_t& operator()() const { return x; } n   ModInt operator+() const { return *this; } n   ModInt operator-() const { return ModInt(-x); } n   ModInt& operator++() { return *this += 1; } n   ModInt& operator++(signed tmp) { return *this += 1; } n   ModInt& operator--() { return *this -= 1; } n   ModInt& operator--(signed tmp) { return *this -= 1; } n   ModInt& operator=(const ModInt& r) { n     x = r.x; n     return *this; n   } n   ModInt& operator=(const value_type& r) { return *this = ModInt(r); } n   ModInt& operator+=(const ModInt& r) { n     x += r.x; n     if (x >= M) x -= M; n     return *this; n   } n   ModInt& operator-=(const ModInt& r) { n     x -= r.x; n     if (x < +0) x += M; n     return *this; n   } n   ModInt& operator*=(const ModInt& r) { n     x = x * r.x % M; n     return *this; n   } n   ModInt& operator/=(ModInt r) { return *this *= r ^= M - 2; } n   ModInt& operator^=(value_type p) { n     p %= M - 1; n     if (p < 0) p += M - 1; n     ModInt res(1); n     while (p) { n       if (p & 1) res *= *this; n       *this *= *this; n       p >>= 1; n     } n     return *this = res; n   } n   ModInt& operator+=(const value_type& r) { return *this += ModInt(r); } n   ModInt& operator-=(const value_type& r) { return *this -= ModInt(r); } n   ModInt& operator*=(const value_type& r) { return *this *= ModInt(r); } n   ModInt& operator/=(const value_type& r) { return *this /= ModInt(r); } n   ModInt operator+(const ModInt& r) const { return ModInt(*this) += r; } n   ModInt operator-(const ModInt& r) const { return ModInt(*this) -= r; } n   ModInt operator*(const ModInt& r) const { return ModInt(*this) *= r; } n   ModInt operator/(const ModInt& r) const { return ModInt(*this) /= r; } n   ModInt operator+(const value_type& r) const { return ModInt(*this) += r; } n   ModInt operator-(const value_type& r) const { return ModInt(*this) -= r; } n   ModInt operator*(const value_type& r) const { return ModInt(*this) *= r; } n   ModInt operator/(const value_type& r) const { return ModInt(*this) /= r; } n   ModInt operator^(const value_type& r) const { return ModInt(*this) ^= r; } n }; n std::ostream& operator<<(std::ostream& os, const ModInt& obj) { n   return os << obj.x; n } n std::istream& operator>>(std::istream& is, ModInt& obj) { n   std::int_fast64_t x; n   is >> x; n   obj = ModInt(x); n   return is; n } n struct S { n   ModInt w, s0, s1; n   bool operator<(const S& r) const { n     return std::tie(w(), s0(), s1()) < std::tie(r.w(), r.s0(), r.s1()); n   } n }; n std::map<S, ModInt> dp[2]; n signed main() { n   std::ios::sync_with_stdio(false); n   std::cin.tie(0); n   ; n   ll n, m; n   MACRO_VAR_Scan(n, m); n   ; n   std::vector<ll> a(n); n   for (auto& i : a) std::cin >> i; n   ; n   std::vector<ModInt> w(n); n   for (auto& i : w) std::cin >> i; n   ; n   ModInt bsum0 = 0, bsum1 = 0; n   for (ll i = 0; i < ll(n); ++i) { n     if (a[i]) n       bsum1 += w[i]; n     else n       bsum0 += w[i]; n   } n   for (ll i = 0; i < ll(n); ++i) { n     dp[0].clear(); n     dp[1].clear(); n     dp[1][S{w[i], bsum0, bsum1}] = 1; n     for (ll _ = 0; _ < ll(m); ++_) { n       std::swap(dp[0], dp[1]); n       for (const auto& p : dp[0]) { n         const auto& s = p.first; n         const auto& v = p.second; n         auto sum = s.s0 + s.s1; n         if (a[i]) { n           dp[1][S{s.w + 1, s.s0 - 0, s.s1 + 1}] += v * s.w / sum; n           dp[1][S{s.w + 0, s.s0 - 0, s.s1 + 1}] += v * (s.s1 - s.w) / sum; n           dp[1][S{s.w + 0, s.s0 - 1, s.s1 + 0}] += v * s.s0 / sum; n         } else { n           dp[1][S{s.w - 1, s.s0 - 1, s.s1 + 0}] += v * s.w / sum; n           dp[1][S{s.w + 0, s.s0 - 1, s.s1 + 0}] += v * (s.s0 - s.w) / sum; n           dp[1][S{s.w + 0, s.s0 - 0, s.s1 + 1}] += v * s.s1 / sum; n         } n       } n       dp[0].clear(); n     } n     ModInt ans = 0; n     for (const auto& p : dp[1]) ans += p.second * p.first.w; n     std::cout << (ans); n     std::cout <<   n ; n     ; n   } n   return 0; n } n


#include <bits/stdc++.h> n int cut; n int q[100005] = {0}; n int totalnode, totaledge, i, j, k, l, a, b, t1, t2; n int c(int q) { n   if (q == 2) { n     return cut; n   } else if (q == cut) { n     return 2; n   } else n     return q; n } n int main() { n   for (i = 0; i < 100005; i++) q[i] = i; n   scanf( %d %d %d , &totalnode, &totaledge, &cut); n   q[cut] = 2; n   q[2] = cut; n   if (totaledge >= totalnode - 1 && n       (totalnode - 1) * (totalnode - 2) / 2 + 1 >= totaledge) { n     for (i = 1; i <= totalnode - 1; i++) { n       printf( %d %d n , q[i], q[i + 1]); n       totaledge--; n     } n     for (i = 2; i <= totalnode && totaledge > 0; i++) { n       for (j = i + 2; j <= totalnode && totaledge > 0; j++) { n         printf( %d %d n , q[i], q[j]); n         totaledge--; n       } n     } n   } else { n     printf( -1 ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct Date { n   int y, m, d; n }; n const int monthDays[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; n int countLeapYears(Date d) { n   int years = d.y; n   if (d.m <= 2) years--; n   return years / 4 - years / 100 + years / 400; n } n int getDifference(Date dt1, Date dt2) { n   long int n1 = dt1.y * 365 + dt1.d; n   for (int i = 0; i < dt1.m - 1; i++) n1 += monthDays[i]; n   n1 += countLeapYears(dt1); n   long int n2 = dt2.y * 365 + dt2.d; n   for (int i = 0; i < dt2.m - 1; i++) n2 += monthDays[i]; n   n2 += countLeapYears(dt2); n   return (n2 - n1); n } n int main() { n   int y1, y2, m1, m2, d1, d2; n   char ch1, ch2, ch3, ch4; n   int tmp; n   cin >> y1 >> ch1 >> m1 >> ch2 >> d1; n   cin >> y2 >> ch3 >> m2 >> ch4 >> d2; n   if ((y1 > y2) || (y1 == y2 && m1 > m2) || (y1 == y2 && m1 == m2 && d1 > d2)) { n     tmp = y1; n     y1 = y2; n     y2 = tmp; n     tmp = m1; n     m1 = m2; n     m2 = tmp; n     tmp = d1; n     d1 = d2; n     d2 = tmp; n   } n   Date dt1 = {y1, m1, d1}; n   Date dt2 = {y2, m2, d2}; n   cout << getDifference(dt1, dt2) << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long INF = 1e16; n long long n, a[200005], p[200005], MA = 0, z = 0; n int main() { n   scanf( %lld , &n); n   memset(p, 0, sizeof(p)); n   for (int i = 0; i < n; i++) { n     scanf( %lld , &a[i]); n     p[a[i]]++; n     if (p[a[i]] > MA) { n       MA = p[a[i]]; n       z = a[i]; n     } n   } n   printf( %lld n , n - MA); n   for (int i = 0; i < n - 1; i++) { n     if (a[i] == z) { n       if (a[i + 1] < a[i]) { n         printf( 1 %d %d n , i + 2, i + 1); n         a[i + 1] = z; n       } else if (a[i + 1] > a[i]) { n         printf( 2 %d %d n , i + 2, i + 1); n         a[i + 1] = z; n       } n     } n   } n   for (int i = n - 1; i >= 1; i--) { n     if (a[i] == z) { n       if (a[i - 1] < a[i]) { n         printf( 1 %d %d n , i, i + 1); n         a[i - 1] = z; n       } else if (a[i - 1] > a[i]) { n         printf( 2 %d %d n , i, i + 1); n         a[i - 1] = z; n       } n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n set<pair<pair<long long int, long long int>, long long int> > ev; n vector<long long int> cnt, len; n multiset<pair<long long int, long long int> > flug; n void expand( n     set<pair<pair<long long int, long long int>, long long int> >::iterator it, n     long long int a) { n   pair<pair<long long int, long long int>, long long int> p = *it; n   p.first.first += a; n   cnt[p.second]++, len[p.second] += a; n   auto nt = ev.insert(p).first; n   ev.erase(it, nt); n   auto ft = flug.upper_bound(make_pair(p.first.first, 1e18)); n   if (ft == flug.begin()) return; n   ft--; n   if (ft->first >= -p.first.second) { n     long long int add = ft->second; n     flug.erase(ft); n     expand(nt, add); n   } n } n int main() { n   int n, m; n   scanf( %d%d , &n, &m); n   vector<pair<pair<long long int, long long int>, long long int> > g(n), f(m); n   cnt = len = vector<long long int>(n, 0); n   for (int((i)) = (0); ((i)) < ((n)); ((i))++) { n     int x, sz; n     scanf( %d%d , &x, &sz); n     g[i] = make_pair(make_pair((x), (sz)), (i)); n     len[i] = sz; n   } n   for (int((i)) = (0); ((i)) < ((m)); ((i))++) { n     int x, sz; n     scanf( %d%d , &x, &sz); n     f[i] = make_pair(make_pair((x), (sz)), (i)); n   } n   sort((g).begin(), (g).end()); n   reverse((g).begin(), (g).end()); n   for (int((i)) = (0); ((i)) < ((n)); ((i))++) { n     auto it = n         ev.insert(make_pair(make_pair((g[i].first.first + g[i].first.second), n                                       (-g[i].first.first)), n                             (g[i].second))) n             .first; n     ev.erase(ev.begin(), it); n   } n   for (int((i)) = (0); ((i)) < ((m)); ((i))++) { n     pair<pair<long long int, long long int>, long long int> fp = n         make_pair(make_pair((f[i].first.first), (-1e18)), (0)); n     auto it = ev.lower_bound(fp); n     if (it != ev.end() && -it->first.second <= f[i].first.first) n       expand(it, f[i].first.second); n     else n       flug.insert({f[i].first.first, f[i].first.second}); n   } n   for (int((i)) = (0); ((i)) < ((n)); ((i))++) n     printf( %I64d %I64d n , cnt[i], len[i]); n } n


#include <bits/stdc++.h> n using namespace std; n const long double eps = 1e-9; n const long long MOD = 1e9 + 7; n const int N = 2e6 + 10; n const long double pi = 3.1415926535897932384650288; n int n, m; n string s, t; n bool ok(int i, int j, int d) { n   if (j == m) return 1; n   if (i == n) return 0; n   if (i < 0) return 0; n   if (s[i] != t[j]) return 0; n   if (d) { n     return ok(i - 1, j + 1, d); n   } n   return ok(i + 1, j + 1, d) || ok(i - 1, j + 1, 1); n } n void solve() { n   cin >> s >> t; n   n = s.size(); n   m = t.size(); n   for (int i = 0; i < n; i++) { n     if (ok(i, 0, 0)) { n       puts( Yes ); n       return; n     } n   } n   puts( No ); n } n int main() { n   int t = 1; n   scanf( %d , &t); n   while (t--) { n     solve(); n   } n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T> n inline T read() { n   T x = 0, f = 1; n   char ch = getchar(); n   while (ch <  0  || ch >  9 ) { n     if (ch ==  - ) f = -1; n     ch = getchar(); n   } n   while (ch >=  0  && ch <=  9 ) { n     x = (x << 1) + (x << 3) + (ch ^ 48); n     ch = getchar(); n   } n   return x * f; n } n const int N = 100010, INF = 0x3f3f3f3f; n int n, q, k, key[N]; n struct Edge { n   int to, nxt; n }; n struct Graph { n   int head[N], tot; n   Edge e[N << 1]; n   void addedge(int x, int y) { n     e[++tot] = (Edge){y, head[x]}; n     head[x] = tot; n   } n }; n struct Tree : public Graph { n   int dfn[N], ti, f[N][19], dep[N]; n   void dfs(int x, int fa) { n     dfn[x] = ++ti; n     f[x][0] = fa; n     dep[x] = dep[fa] + 1; n     for (int i = 1; i <= 18; i++) f[x][i] = f[f[x][i - 1]][i - 1]; n     for (int i = head[x]; i; i = e[i].nxt) { n       int y = e[i].to; n       if (y == fa) continue; n       dfs(y, x); n     } n   } n   int lca(int x, int y) { n     if (dep[x] < dep[y]) swap(x, y); n     for (int i = 18; i >= 0; i--) { n       if (dep[f[x][i]] >= dep[y]) x = f[x][i]; n     } n     if (x == y) return x; n     for (int i = 18; i >= 0; i--) { n       if (f[x][i] != f[y][i]) x = f[x][i], y = f[y][i]; n     } n     return f[x][0]; n   } n } t; n struct Virtual_Tree : public Graph { n   int dp[N][2], d[N], st[N], tp; n   void build_vtree(Tree& t, int* key, int cnt) { n     memcpy(d, key, sizeof(int) * (cnt + 1)); n     sort(d + 1, d + cnt + 1, [&](int x, int y) { return t.dfn[x] < t.dfn[y]; }); n     head[1] = 0, tot = 0; n     st[tp = 1] = 1; n     for (int i = 1; i <= cnt; i++) { n       if (d[i] == 1) continue; n       int lc = t.lca(d[i], st[tp]); n       if (lc != st[tp]) { n         while (t.dfn[lc] < t.dfn[st[tp - 1]]) { n           addedge(st[tp - 1], st[tp]); n           tp--; n         } n         if (t.dfn[lc] != t.dfn[st[tp - 1]]) { n           head[lc] = 0, addedge(lc, st[tp]); n           st[tp] = lc; n         } else n           addedge(lc, st[tp]), tp--; n       } n       head[d[i]] = 0, st[++tp] = d[i]; n     } n     for (int i = 1; i < tp; i++) addedge(st[i], st[i + 1]); n   } n   void dfs_dp(int, int); n } vt; n int iskey[N]; n void Virtual_Tree::dfs_dp(int x, int fa) { n   dp[x][0] = (!iskey[x]) * n, dp[x][1] = iskey[x] * n; n   int minx = INF, sum = 1, cnt = 0; n   for (int i = head[x]; i; i = e[i].nxt) { n     int y = e[i].to; n     if (y == fa) continue; n     cnt += iskey[y]; n     dfs_dp(y, x); n     if (iskey[x]) n       dp[x][0] += min(dp[y][1], dp[y][0] + 1); n     else { n       minx = min(minx, dp[y][0] - dp[y][1]); n       sum += min(dp[y][0], dp[y][1]); n       dp[x][1] += dp[y][1]; n     } n   } n   if (!iskey[x] && cnt <= 1) dp[x][0] = dp[x][1] + minx; n   if (!iskey[x]) dp[x][1] = min(dp[x][1], sum); n } n void solve() { n   k = read<int>(); n   for (int i = 1; i <= k; i++) iskey[key[i] = read<int>()] = 1; n   for (int i = 1; i <= k; i++) { n     int x = key[i]; n     if (iskey[t.f[x][0]]) { n       for (int j = 1; j <= k; j++) iskey[key[j]] = 0; n       puts( -1 ); n       return; n     } n   } n   vt.build_vtree(t, key, k); n   vt.dfs_dp(1, 0); n   printf( %d n , min(vt.dp[1][0], vt.dp[1][1])); n   for (int j = 1; j <= k; j++) iskey[key[j]] = 0; n } n int main() { n   n = read<int>(); n   for (int i = 1; i < n; i++) { n     int x = read<int>(), y = read<int>(); n     t.addedge(x, y); n     t.addedge(y, x); n   } n   t.dfs(1, 0); n   q = read<int>(); n   while (q--) solve(); 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 arr[102]; n     int n; n     cin >> n; n     for (int i = 1; i <= n; i++) cin >> arr[i]; n     int myseq[102]; n     for (int i = 1; i <= n; i++) { n       if (i & 1) n         myseq[i] = arr[i + 1]; n       else n         myseq[i] = -arr[i - 1]; n     } n     for (int i = 1; i <= n; i++) cout << myseq[i] <<    ; n     cout << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 2005; n const long long mod = 998244353; n string s, t; n long long p[maxn], n; n long long dp[maxn][maxn]; n long long ksm(long long a, long long b) { n   long long r = 1; n   while (b) { n     if (b & 1) r = (r * a) % mod; n     a = a * a % mod; n     b >>= 1; n   } n   return r; n } n long long dfs(int l, int r) { n   if (dp[l][r] != -1) return dp[l][r]; n   if (l >= r || l > n || r <= 0) return dp[l][r] = 0; n   long long ans = 0; n   if (s[l] !=  ( ) ans = (ans + dfs(l + 1, r)) % mod; n   if (s[r] !=  ) ) ans = (ans + dfs(l, r - 1)) % mod; n   if (s[l] !=  (  && s[r] !=  ) ) ans = (ans - dfs(l + 1, r - 1) + mod) % mod; n   if (s[l] !=  )  && s[r] !=  ( ) n     ans = (ans + dfs(l + 1, r - 1) + ksm(2, p[r - 1] - p[l])) % mod; n   return dp[l][r] = ans; n } n int main() { n   s +=  # ; n   cin >> t; n   s += t; n   n = t.size(); n   memset(dp, -1, sizeof(dp)); n   for (int i = 1; i <= n; i++) p[i] = p[i - 1] + (s[i] ==  ? ); n   dfs(1, n); n   cout << dp[1][n] << endl; n } n


#include <bits/stdc++.h> n const char en =   n ; n using namespace std; n int main() { n   ios::sync_with_stdio(false); n   int n; n   cin >> n; n   vector<long long> V(n + 47, 0); n   long long ma2 = 0; n   long long ma3 = 0; n   vector<long long> X; n   for (int i = 0; i < n; i++) { n     cin >> V[i]; n     long long p2 = 0, p3 = 0; n     long long k = V[i]; n     while (k % 2 == 0) { n       p2++; n       k /= 2; n     } n     while (k % 3 == 0) { n       p3++; n       k /= 3; n     } n     X.push_back(k); n   } n   sort(X.begin(), X.end()); n   if (X[0] == X[n - 1]) n     cout <<  Yes  << en; n   else n     cout <<  No  << en; n } n


#include <bits/stdc++.h> n using namespace std; n const int oo = 1000000000; n const double H = sqrt(3.0) * 0.5; n const double eps = 1e-6; n int main() { n   double r; n   scanf( %lf , &r); n   int i; n   long long ans = 0; n   for (i = 0; i <= r; i++) { n     double x = 1.5 * i + 1.0; n     if (x > r) break; n     double y = sqrt(r * r - x * x); n     int k; n     if (i % 2) n       k = (floor(y / H + eps) + 1) / 2; n     else n       k = floor(y / H + eps) / 2 + 1; n     x = 1.5 * i + 0.5; n     y = sqrt(r * r - x * x); n     if (i % 2) n       k = 2 * min(k, (int)floor(y / H + eps) / 2); n     else n       k = 2 * min(k, ((int)floor(y / H + eps) + 1) / 2) - 1; n     ans += i ? 2 * k : k; n   } n   cout << ans << endl; n } n


#include <bits/stdc++.h> n using namespace std; n void solve() { n   string s; n   cin >> s; n   unordered_map<char, vector<long long int> > um; n   long long int i; n   for (i = 0; i < s.length(); i++) { n     um[s[i]].push_back(i); n   } n   long long int last = -1; n   bool flag; n   string ans =   ; n   for (i = 25; i >= 0; i--) { n     char ch =  a  + i; n     auto f = um.find(ch); n     if (f != um.end()) { n       for (auto it : um[ch]) { n         if (it > last) { n           ans += string(1, s[it]); n           flag = true; n         } n       } n       if (flag == true) last = um[ch][um[ch].size() - 1]; n     } n     flag = false; n   } n   cout << ans << endl; n } n int main() { n   solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n using pii = pair<int, int>; n using ll = long long; n const int N = 3e3 + 3; n int a[N][N]; n pii sum[N]; n int main() { n   ios_base::sync_with_stdio(false); n   int n, m; n   cin >> n >> m; n   for (int i = 0; i < m; i++) { n     for (int j = 0; j < n; j++) cin >> a[i][j]; n   } n   vector<int> res; n   int mn = 1e9; n   for (int i = 0; i < n - 1; i++) { n     vector<pii> ve; n     int sum = 0; n     for (int j = 0; j < m; j++) { n       sum += a[j][i] - a[j][n - 1]; n       ve.push_back({a[j][i] - a[j][n - 1], j}); n     } n     sort(ve.begin(), ve.end()); n     vector<int> cur; n     for (int i = 0; i < m; i++) { n       if (sum >= 0) break; n       sum -= ve[i].first; n       cur.push_back(ve[i].second); n     } n     if (cur.size() < mn) { n       mn = cur.size(); n       res = cur; n     } n   } n   cout << res.size() <<   n ; n   for (auto x : res) cout << ++x <<    ; n   cout <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1000005; n long long A[N], B[N], n, xx, mx; n bool check(int n, int m) { n   int x = n - xx, y = mx - x; n   if (x < 0 || x >= n || y < 0 || y >= m) return 0; n   memset(B, 0, sizeof(B)); n   for (int i = 0; i < n; ++i) n     for (int j = 0; j < m; ++j) ++B[abs(y - j) + abs(i - x)]; n   if (!memcmp(A, B, sizeof(A))) { n     printf( %d %d n%d %d n , n, m, x + 1, y + 1); n     return 1; n   } n   return 0; n } n int main() { n   cin >> n; n   for (int i = 1; i <= n; ++i) { n     scanf( %lld , &xx); n     A[xx]++; n     if (xx > mx) mx = xx; n   } n   if (A[0] != 1) { n     printf( -1 ); n     return 0; n   } n   for (xx = 1; xx <= mx; ++xx) n     if (A[xx] != xx * 4) { n       if (A[xx] > xx * 4) { n         printf( -1 ); n         return 0; n       } else n         break; n     } n   for (int i = 1; i * i <= n; ++i) { n     if (n % i) continue; n     if (check(i, n / i)) return 0; n     if (check(n / i, i)) return 0; n   } n   printf( -1 n ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n char A[1005][1005]; n int P[1000005], MinX[1000005], MinY[1000005], MaxX[1000005], MaxY[1000005], n     M[1005][1005]; n int xr[] = {-1, 1, 0, 0}; n int xc[] = {0, 0, -1, 1}; n int n, m; n void dfs(int x, int y, int i) { n   if ((x < 0) || (x >= n) || (y < 0) || (y >= m) || (A[x][y] ==  . ) || n       (M[x][y])) n     return; n   M[x][y] = 1; n   MinX[i] = min(MinX[i], x); n   MinY[i] = min(MinY[i], y); n   MaxX[i] = max(MaxX[i], x); n   MaxY[i] = max(MaxY[i], y); n   for (int j = 0; j < 4; ++j) { n     dfs(x + xr[j], y + xc[j], i); n   } n } n bool check_convex_island(int len) { n   set<int> X, Y; n   for (int i = 0; i < n; ++i) X.insert(i); n   for (int i = 0; i < m; ++i) Y.insert(i); n   int x, i, j, ans = 1; n   for (x = 0; x < len; ++x) { n     int minx = MinX[x]; n     int miny = MinY[x]; n     int maxx = MaxX[x]; n     int maxy = MaxY[x]; n     for (i = minx; i <= maxx; ++i) { n       if (!X.count(i)) return false; n       X.erase(i); n       for (j = miny; A[i][j] ==  . ; ++j) n         ; n       for (; (j <= maxy) && (A[i][j] ==  # ); ++j) n         ; n       for (; (j <= maxy) && (A[i][j] ==  . ); ++j) n         ; n       ans &= (j > maxy); n     } n     for (j = miny; j <= maxy; ++j) { n       if (!Y.count(j)) return false; n       Y.erase(j); n       for (i = minx; A[i][j] ==  . ; ++i) n         ; n       for (; (i <= maxx) && (A[i][j] ==  # ); ++i) n         ; n       for (; (i <= maxx) && (A[i][j] ==  . ); ++i) n         ; n       ans &= (i > maxx); n     } n   } n   return ans; n } n int main() { n   ios_base::sync_with_stdio(false); n   cout.tie(0); n   cin.tie(0); n   int i, j, k, x, y, z, a, b; n   for (i = 0; i <= 1000000; ++i) MinX[i] = MinY[i] = 1000000007; n   cin >> n >> m; n   for (i = 0; i < n; ++i) cin >> A[i]; n   int ans = 1, len = 0; n   set<int> X, Y; n   for (i = 0; i < n; ++i) X.insert(i); n   for (i = 0; i < m; ++i) Y.insert(i); n   for (i = 0; i < n; ++i) { n     for (j = 0; j < m; ++j) { n       if (A[i][j] ==  . ) continue; n       if (X.find(i) != X.end()) X.erase(i); n       if (Y.find(j) != Y.end()) Y.erase(j); n       if (!M[i][j]) dfs(i, j, len++); n     } n   } n   ans &= ((X.size() == 0 && Y.size() == 0) || (X.size() && Y.size())); n   ans &= check_convex_island(len); n   if (ans) n     cout << len << endl; n   else n     cout << -1 << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int _5 = 100005, _6 = 1000006; n int n, a[1001], b[1001]; n vector<int> v; n int main() { n   scanf( %d , &n); n   for (int i = 0, b; i < n; ++i) cin >> a[i]; n   for (int i = n - 1; i >= 0; --i) { n     if (!b[a[i]]) { n       b[a[i]] = 1; n       v.push_back(a[i]); n     } n   } n   cout << v.size() << endl; n   for (int i = v.size() - 1; i >= 0; --i) { n     cout << v[i] <<    ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int oo = numeric_limits<int>::max(); n const int n_oo = numeric_limits<int>::min(); n const int ALF = 26; n struct SuffixAutomaton { n   int toInt(char c) { return c -  a ; } n   char toChar(int j) { return char(j +  a ); } n   struct Nodo { n     vector<int> hijos; n     int r; n     int l; n     int sl; n     Nodo(int L = 0) : hijos(ALF, -1), l(L), sl(0), r(1) {} n     int& operator[](int idx) { return hijos[idx]; } n   }; n   vector<int> orden; n   vector<long long int> seen; n   int n, ult; n   vector<Nodo> t; n   void extender(char c) { n     int act = n++; n     t.push_back(Nodo(t[ult].l + 1)); n     int p = ult; n     while (~p && t[p][toInt(c)] == -1) { n       t[p][toInt(c)] = act; n       p = t[p].sl; n     } n     if (~p) { n       int q = t[p][toInt(c)]; n       if (t[q].l == t[p].l + 1) { n         t[act].sl = q; n       } else { n         int clon = n++; n         t.push_back(t[q]); n         t[clon].l = t[p].l + 1; n         t[clon].r = 0; n         t[q].sl = t[act].sl = clon; n         while (~p && t[p][toInt(c)] == q) { n           t[p][toInt(c)] = clon; n           p = t[p].sl; n         } n       } n     } n     ult = act; n   } n   SuffixAutomaton(string s =   ) : n(1), ult(0), t(1) { n     t[0].sl = -1; n     for (int i = 0; i < s.size(); i++) extender(s[i]); n   } n   void rTopoSort() { n     int m = t[ult].l; n     vector<vector<int> > bks(m + 1); n     for (int i = 0; i < n; i++) bks[t[i].l].push_back(i); n     for (int i = m; i >= 0; i--) n       for (int j = 0; j < bks[i].size(); j++) orden.push_back(bks[i][j]); n   } n   void cuentaRepeticiones() { n     rTopoSort(); n     for (int i = 0; i < n - 1; i++) { n       int u = orden[i]; n       int v = t[u].sl; n       t[v].r += t[u].r; n     } n     t[0].r = 1; n   } n   long long int dfs(int u) { n     if (seen[u]) return seen[u]; n     long long int& ret = seen[u]; n     ret = t[u].r; n     for (int i = 0; i < ALF; i++) { n       if (t[u][i] == -1) continue; n       ret += dfs(t[u][i]); n     } n     return ret; n   } n   void cuentaCaminos() { n     seen = vector<long long int>(n); n     dfs(0); n   } n   string kth(long long int k) { n     int u = 0; n     k++; n     string ans; n     if (k > seen[0]) return  No such line. ; n     while (true) { n       k -= t[u].r; n       if (k <= 0) return ans; n       for (int i = 0; i < ALF; i++) { n         if (t[u][i] == -1) continue; n         if (k > seen[t[u][i]]) n           k -= seen[t[u][i]]; n         else { n           ans.push_back(toChar(i)), u = t[u][i]; n           break; n         } n       } n     } n     return ans; n   } n   void imprime() { n     for (int i = 0; i < n; i++) { n       cout << i <<   n ; n       cout <<  suffix link:   << t[i].sl <<   n ; n       cout <<  length:   << t[i].l <<   n ; n       cout <<  reps:  << t[i].r <<   n ; n       for (int j = 0; j < ALF; j++) { n         if (t[i][j] == -1) continue; n         cout << toChar(j) <<  :   << t[i][j] <<    ; n       } n       cout <<   n ; n       cout <<   n ; n     } n   } n }; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   string s; n   cin >> s; n   SuffixAutomaton SA(s); n   SA.cuentaRepeticiones(); n   SA.cuentaCaminos(); n   int k; n   cin >> k; n   cout << SA.kth(k) <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n; n vector<int> x; n int ax[3]; n int main() { n   ax[0] = ax[1] = ax[2] = 0; n   cin >> n; n   for (int i = 0; i < n; ++i) { n     string s; n     cin >> s; n     int cc = 0; n     for (int i = 0; i < s.size(); ++i) cc += s[i] -  0 ; n     x.push_back(cc % 3); n     ax[cc % 3]++; n   } n   int res = ax[0] / 2 + min(ax[1], ax[2]); n   cout << res; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long MOD = 998244353; n const long long N = 1e5 + 5; n vector<long long> vp; n long long n; n long long a[N]; n long long binsearch(long long lo, long long hi, long long check) { n   long long ans = -1; n   while (hi >= lo) { n     long long mid = (lo + hi) / 2; n     if (a[mid] * check == n) { n       return mid; n     } else if (a[mid] * check < n) { n       lo = mid + 1; n     } else { n       hi = mid - 1; n     } n   } n   return ans; n } n int32_t main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cin >> n; n   for (long long i = 1; i * i <= n; i++) { n     if (n % i == 0) { n       vp.push_back(n / i); n       if (n / i != i) vp.push_back(i); n     } n   } n   sort(vp.begin(), vp.end()); n   for (long long i = 1; i <= vp.size(); i++) { n     a[i] = vp[i - 1]; n   } n   long long mx = 0; n   long long mn = 1e18; n   for (long long i = 0; i < vp.size(); i++) { n     for (long long j = 0; j < vp.size(); j++) { n       long long k = binsearch(1, vp.size(), vp[i] * vp[j]); n       if (k != -1) { n         k--; n         if (vp[i] * vp[j] * vp[k] == n) { n           mn = min(mn, (vp[i] + 1) * (vp[j] + 2) * (vp[k] + 2) - n); n           mn = min(mn, (vp[i] + 2) * (vp[j] + 2) * (vp[k] + 1) - n); n           mn = min(mn, (vp[i] + 2) * (vp[j] + 1) * (vp[k] + 2) - n); n           mx = max(mx, (vp[i] + 1) * (vp[j] + 2) * (vp[k] + 2) - n); n           mx = max(mx, (vp[i] + 2) * (vp[j] + 2) * (vp[k] + 1) - n); n           mx = max(mx, (vp[i] + 2) * (vp[j] + 1) * (vp[k] + 2) - n); n         } n       } n     } n   } n   cout << mn <<     << mx <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n const long long int N = 100005; n bool powertwo(long long int n) { return (n and !(n & (n - 1))); } n int32_t main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   long long int t; n   cin >> t; n   while (t--) { n     long long int n; n     cin >> n; n     long long int arr[n]; n     map<long long int, long long int> mp; n     for (long long int i = 0; i < n; i++) { n       cin >> arr[i]; n       mp[arr[i]]++; n     } n     long long int g = 0, s = 0, b = 0; n     vector<long long int> v; n     for (auto it = mp.rbegin(); it != mp.rend(); it++) v.push_back(it->second); n     g = v[0]; n     long long int i = 1; n     while (s <= g and i < v.size()) { n       s += v[i]; n       i++; n     } n     while (b <= g and i < v.size()) { n       b += v[i]; n       i++; n     } n     while ((g + s + b + v[i]) <= n / 2 and i < v.size()) { n       b += v[i]; n       i++; n     } n     if ((g + s + b) > n / 2 or s < g or b < g) n       cout <<  0 0 0 n ; n     else if (g == 0 or s == 0 or b == 0) n       cout <<  0 0 0 n ; n     else n       cout << g <<     << s <<     << b <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct Point { n   double x, y; n   int num; n   Point() {} n   Point(double _x, double _y, int _num) : x(_x), y(_y), num(_num) {} n   void read() { scanf( %lf%lf , &x, &y); } n   double operator*(const Point &a) const { return x * a.y - a.x * y; } n   Point operator-(const Point &a) const { return Point(x - a.x, y - a.y, num); } n   bool operator<(const Point &a) const { n     return y < a.y || y == a.y && x < a.x; n   } n   bool operator==(const Point &a) const { return x == a.x && y == a.y; } n } a[2000], s; n int sum = 0, cnt = 0, n, ans[2000], sz[2000]; n int des[4000], nxt[4000], fst[2000]; n int c[2000], nxtc[2000], fstc[2000]; n bool vis[2000]; n void add(int x, int y) { des[++sum] = y, nxt[sum] = fst[x], fst[x] = sum; } n void add2(int x, int y) { c[++cnt] = y, nxtc[cnt] = fstc[x], fstc[x] = cnt; } n void tree(int x) { n   vis[x] = 1; n   sz[x] = 1; n   for (int i = fst[x]; i; i = nxt[i]) n     if (!vis[des[i]]) tree(des[i]), add2(x, des[i]), sz[x] += sz[des[i]]; n } n double cross(Point a, Point b, Point c) { return (a - c) * (b - c); } n bool cmp(Point i, Point j) { n   if (i == s) return 1; n   if (j == s) return 0; n   double y = cross(i, j, s); n   if (y < 0) return 0; n   return 1; n } n void solve(vector<Point> b, int fa) { n   s = b[0]; n   for (int i = 1; i < b.size(); ++i) n     if (b[i] < s) s = b[i]; n   sort(b.begin(), b.end(), cmp); n   int l = 1; n   ans[s.num] = fa; n   for (int i = fstc[fa]; i; i = nxtc[i]) { n     int k = c[i]; n     vector<Point> m; n     for (int j = 1; j <= sz[k]; ++j) m.push_back(b[l]), l++; n     solve(m, k); n   } n } n int main() { n   scanf( %d , &n); n   for (int i = 1; i < n; ++i) { n     int x, y; n     scanf( %d%d , &x, &y); n     add(x, y), add(y, x); n   } n   memset(vis, 0, sizeof vis); n   tree(1); n   vector<Point> b; n   for (int i = 1; i <= n; ++i) a[i].read(), a[i].num = i, b.push_back(a[i]); n   solve(b, 1); n   for (int i = 1; i <= n; ++i) printf( %d  , ans[i]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   double a, b, c, d, e, f; n   cin >> a >> b; n   for (int i = 0; i < a; i++) { n     cin >> c >> d; n     e = c / d; n     if (i > 0) n       f = min(e, f); n     else if (i == 0) n       f = e; n   } n   printf( %.08f n , f * b); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, p[105][105], ans = 0; n   cin >> n; n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < 2; j++) { n       cin >> p[i][j]; n     } n   } n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < 2; j++) { n       if (p[i][j + 1] - p[i][j] >= 2) ans++; n     } n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const double PI = acos(-1.0); n const int INF = 1000 * 1000 * 1000 + 7; n const long long LINF = INF * (long long)INF; n const int L = 22; n int G[L]; n int dp[1 << L]; n int P[1 << L]; n int M[1 << L]; n int n; n void print(int mask) { n   for (int i = (0); i < (n); i++) { n     if (mask & (1 << i)) { n       cout <<  1  ; n       continue; n     } n     cout <<  0  ; n   } n   cout << endl; n } n bool isClik(int mask) { n   for (int i = (0); i < (n); i++) { n     if ((1 << i) & mask) { n       mask ^= 1 << i; n       if ((mask & G[i]) != mask) return false; n       mask ^= 1 << i; n     } n   } n   return true; n } n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   int m; n   cin >> n >> m; n   for (int i = (0); i < (m); i++) { n     int a, b; n     cin >> a >> b; n     a--; n     b--; n     G[a] |= 1 << b; n     G[b] |= 1 << a; n   } n   for (int mask = (0); mask < (1 << n); mask++) { n     dp[mask] = INF; n   } n   for (int mask = (0); mask < (1 << n); mask++) { n     if (isClik(mask)) { n       dp[mask] = 0; n     } n   } n   for (int mask = (0); mask < (1 << n); mask++) { n     for (int k = (0); k < (n); k++) { n       if (!(mask & (1 << k))) continue; n       if (dp[mask | G[k]] > dp[mask] + 1) { n         dp[mask | G[k]] = dp[mask] + 1; n         P[mask | G[k]] = k; n         M[mask | G[k]] = mask; n       } n       dp[mask | G[k]] = min(dp[mask | G[k]], dp[mask] + 1); n     } n   } n   int ans = dp[(1 << n) - 1]; n   cout << ans << endl; n   vector<int> res; n   int mask = (1 << n) - 1; n   for (int i = (0); i < (ans); i++) { n     res.push_back(P[mask]); n     mask = M[mask]; n   } n   reverse(res.begin(), res.end()); n   for (int i = (0); i < ((int)res.size()); i++) { n     cout << res[i] + 1 <<    ; n   } n   cout << endl; n } n


#include <bits/stdc++.h> n using namespace std; n inline long long rd() { n   long long x = 0; n   int ch = getchar(), f = 1; n   while (!isdigit(ch) && (ch !=  - ) && (ch != EOF)) ch = getchar(); n   if (ch ==  - ) { n     f = -1; n     ch = getchar(); n   } n   while (isdigit(ch)) { n     x = (x << 1) + (x << 3) + ch -  0 ; n     ch = getchar(); n   } n   return x * f; n } n inline void rt(long long x) { n   if (x < 0) putchar( - ), x = -x; n   if (x >= 10) n     rt(x / 10), putchar(x % 10 +  0 ); n   else n     putchar(x +  0 ); n } n const int maxn = 5005, S = 100005; n int n, m, a[maxn], id[S], dsc[maxn]; n short int f[maxn][maxn], g[maxn][maxn], h[maxn][7], ans; n void upd(short int &x, short int y) { n   if (y > x) x = y; n } n int main() { n   n = rd(); n   for (int i = int(1); i <= (int)(n); i++) a[i] = dsc[i] = rd(); n   sort(dsc + 1, dsc + n + 1); n   m = unique(dsc + 1, dsc + n + 1) - dsc - 1; n   for (int i = int(1); i <= (int)(m); i++) id[dsc[i]] = i; n   for (int i = int(1); i <= (int)(n); i++) { n     int x = id[a[i]]; n     for (int j = int(0); j <= (int)(i - 1); j++) { n       if (x && dsc[x - 1] + 1 == a[i]) upd(f[j][i], g[j][x - 1] + 1); n       if (x < m && dsc[x + 1] - 1 == a[i]) upd(f[j][i], g[j][x + 1] + 1); n       upd(f[j][i], h[j][a[i] % 7] + 1); n       if (j) n         upd(h[i][a[j] % 7], f[j][i]), upd(g[i][id[a[j]]], f[j][i]); n       else { n         for (int k = int(0); k <= (int)(6); k++) upd(h[i][k], f[j][i]); n         for (int k = int(1); k <= (int)(m); k++) upd(g[i][k], f[j][i]); n       } n       upd(g[j][id[a[i]]], f[j][i]), upd(h[j][a[i] % 7], f[j][i]); n       upd(ans, f[j][i]); n     } n   } n   rt(ans), putchar(  n ); n } n


#include <bits/stdc++.h> n using namespace std; n char s[300005]; n int ans[300005]; n int main() { n   int n, m; n   while (cin >> n >> m) { n     int cnt = 0; n     for (int i = 1; i <= n; i++) cin >> s[i]; n     for (int i = 1; i < n; i++) n       if (s[i] == s[i + 1] && s[i] ==  . ) cnt++; n     for (int i = 0; i < m; i++) { n       int x; n       char y; n       cin >> x >> y; n       if (s[x] ==  .  && s[x - 1] ==  . ) cnt--; n       if (s[x] ==  .  && s[x + 1] ==  . ) cnt--; n       s[x] = y; n       if (s[x] ==  .  && s[x - 1] ==  . ) cnt++; n       if (s[x] ==  .  && s[x + 1] ==  . ) cnt++; n       ans[i] = cnt; n     } n     for (int i = 0; i < m; i++) cout << ans[i] << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int a[1005], b[1005]; n int main() { n   int n; n   string s, ss; n   cin >> n >> s >> ss; n   for (int i = 0; i < n; i++) { n     a[i] = s[i] -  0 ; n     b[i] = ss[i] -  0 ; n   } n   sort(a, a + n); n   sort(b, b + n); n   int sum1 = 0, sum2 = 0; n   for (int i = 0, j = 0; i < n && j < n;) { n     while (j < n) { n       if (a[i] <= b[j]) { n         i++; n         j++; n         sum1++; n         break; n       } else { n         j++; n       } n     } n   } n   int sum = n - sum1; n   cout << sum << endl; n   for (int i = 0, j = 0; i < n && j < n;) { n     while (j < n) { n       if (a[i] < b[j]) { n         i++; n         j++; n         sum2++; n         break; n       } else { n         j++; n       } n     } n   } n   cout << sum2; n } n


#include<bits/stdc++.h> n #define endl   n  n #define bug puts( aaa ); n #define Case int t;scanf( %d ,&t);while(t--) n #define ios ios::sync_with_stdio(0),cin.tie(0); n using namespace std; n typedef long long ll; n typedef unsigned long long ull; n const double eps = 1e-5; n const int inf = 0x3f3f3f3f; n const int mod = 1e9+7; n const int N = 1e6+10; n  n int n,x,mi[N<<2],a[N]; n unordered_map<int,int>mp; n  n struct node { n  int x,y; n }e[N]; n  n  n void pushup(int rt) { n  mi[rt] = mp[e[mi[rt<<1]].y] > mp[e[mi[rt<<1|1]].y] ? mi[rt<<1|1] : mi[rt<<1]; n } n  n void build(int l,int r,int rt) { n  if(l==r) { n   mi[rt] = 0; n   return; n  } n  int mid = l+r>>1; n  build(l,mid,rt<<1); n  build(mid+1,r,rt<<1|1); n  pushup(rt); n } n  n void update(int pos, int val, int l, int r, int rt) { n  if(l==r) { n   mi[rt] = mp[e[val].y] < mp[e[mi[rt]].y] ? val : mi[rt]; n   return; n  } n  int mid = l+r>>1; n  if(pos <= mid) update(pos,val,l,mid,rt<<1); n  else update(pos,val,mid+1,r,rt<<1|1); n  pushup(rt); n } n  n int query(int L,int R,int l,int r,int rt) { n  if(L<=l && r<=R) { n   return mi[rt]; n  } n  int mid = l+r>>1; n  int lson = 0,rson = 0; n  if(L <= mid) lson = query(L,R,l,mid,rt<<1); n  if(R > mid) rson = query(L,R,mid+1,r,rt<<1|1); n  return mp[e[lson].y] > mp[e[rson].y] ?rson:lson; n } n  n void run() { n  mp.clear(); n  scanf( %d ,&n); n  int x,y,range = 0,idx=0; n  e[0].x = e[0].y = inf; n  mp[inf] = inf; n  for (int i=1;i<=n;i++) { n   scanf( %d %d ,&e[i].x,&e[i].y); n   a[++idx] = e[i].x; n   a[++idx] = e[i].y; n  n   /** n    * 如果不交换,只符合hj<hi && wj<wi的情况 n    * 首先按把w和h大的那个，放在x位置 n    * 那么我们去找[1,x-1]区间，里面的x肯定满足条件 n    * 接着在这个区间中去找符合条件的y, n    */ n   if(e[i].x > e[i].y) swap(e[i].x,e[i].y); n  } n  n  /** n   * 离散化 n   */ n  sort(a+1,a+idx+1); n  for (int i=1;i<=idx;i++) { n   if(mp[a[i]])continue; n   mp[a[i]] = ++range; n  } n  n  n  n  build(1,range,1); n  for (int i=1;i<=n;i++) { n   update(mp[e[i].x],i,1,range,1); n  } n  for (int i=1;i<=n;i++) { n   if(mp[e[i].x] == 1) { n    printf( -1  ); n    continue; n   } n   int res = query(1,mp[e[i].x]-1,1,range,1); n   if (e[res].y >= e[i].y) { n    printf( -1  ); n   } else { n    printf( %d  ,res); n   } n  } n  printf(  n ); n } n  n  n int main() { n  // freopen( input.txt , r ,stdin); n  // freopen( output.txt , w ,stdout); n  Case n  run(); n  return 0; n }


#include <bits/stdc++.h> n using namespace std; n const int64_t N = 1e5 + 1; n int64_t crt_node = 0, maxi = 0; n vector<int64_t> previous; n vector<vector<int64_t>> adj; n void dfs(int64_t node, int64_t pere, int64_t len) { n   previous[node] = pere; n   if (len > maxi) { n     crt_node = node; n     maxi = len; n   } n   for (int64_t i : adj[node]) { n     if (i != pere) dfs(i, node, len + 1); n   } n } n bool ok = true; n vector<int64_t> level, path; n void hedgehog(int64_t node, int64_t pere) { n   int64_t k = 0; n   for (int64_t i : adj[node]) { n     if (i != pere) { n       hedgehog(i, node); n       if (k == 0) n         k = level[i]; n       else if (k != level[i]) n         ok = false; n     } n   } n   if (adj[node].size() == 1) n     level[node] = 1; n   else if (adj[node].size() < 3 or n            (adj[node].size() == 3 and path[path.size() / 2] != node)) n     ok = false; n   else n     level[node] = k + 1; n } n int32_t main() { n   ios::sync_with_stdio(false); n   cin.tie(nullptr); n   int64_t n, k; n   cin >> n >> k; n   if (n == 1) { n     cout <<  No ; n     return 0; n   } n   adj.resize(n + 1); n   for (int64_t i = 1; i < n; ++i) { n     int64_t u, v; n     cin >> u >> v; n     adj[u].emplace_back(v); n     adj[v].emplace_back(u); n   } n   previous.resize(n + 1); n   dfs(1, 0, 0); n   int64_t a = crt_node; n   maxi = 0; n   fill(previous.begin(), previous.end(), 0ll); n   dfs(crt_node, 0, 0); n   int64_t b = crt_node; n   while (b) { n     path.emplace_back(b); n     b = previous[b]; n   } n   level.resize(n + 1); n   hedgehog(path[path.size() / 2], 0); n   if (level[path[path.size() / 2]] != k + 1) ok = false; n   cout << (ok ?  Yes  :  No ); n   return 0; n } n