#include <bits/stdc++.h> n using namespace std; n struct point { n   double x, y; n }; n point operator+(point a, point b) { return (point){a.x + b.x, a.y + b.y}; } n point operator*(point a, double b) { return (point){a.x * b, a.y * b}; } n double len(point x) { return sqrt(x.x * x.x + x.y * x.y); } n void print(point x) { printf( %.10lf %.10lf n , x.x, x.y); } n int main() { n   ios::sync_with_stdio(false); n   point p, v; n   cin >> p.x >> p.y >> v.x >> v.y; n   double a, b, c, d; n   cin >> a >> b >> c >> d; n   print(p + v * (b / len(v))); n   point u = v * (-1); n   point l = (point){-v.y, v.x}; n   point r = l * (-1); n   point tmp = p; n   print(tmp = tmp + l * (a / 2 / len(l))); n   print(tmp = tmp + r * ((a - c) / 2 / len(r))); n   print(tmp = tmp + u * (d / len(u))); n   print(tmp = tmp + r * (c / len(r))); n   print(tmp = tmp + v * (d / len(v))); n   print(tmp = tmp + r * ((a - c) / 2 / len(r))); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   long long int n, k; n   cin >> n >> k; n   if (k == 1) { n     long long int ans = n * (n - 1) / 2; n     cout << ans <<     << ans << endl; n   } else { n     long long int ans = (n - k + 1) * (n - k) / 2; n     long long int x = n / k; n     long long int y = n % k; n     long long int ans2 = x * (x - 1) / 2; n     long long int ans3 = (x + 1) * x / 2; n     cout << ans3 * y + ans2 * (k - y) <<     << ans << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int Mod = (int)1e9 + 7; n const int MX = 1073741822; n const long long MXLL = 4e18; n const int Sz = 1110111; n inline void Read_rap() { n   ios_base ::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n } n inline void randomizer3000() { n   unsigned int seed; n   asm( rdtsc  :  =A (seed)); n   srand(seed); n } n void files(string problem) { n   if (fopen((problem +  .in ).c_str(),  r )) { n     freopen((problem +  .in ).c_str(),  r , stdin); n     freopen((problem +  .out ).c_str(),  w , stdout); n   } n } n int n; n long long d[Sz], m[Sz], r[Sz], p[Sz]; n bool used[Sz]; n struct fenwick { n   int n; n   vector<vector<pair<int, int> > > t; n   fenwick(int n) : n(n), t(n + 1) {} n   void add(int x, int y, int id) { n     for (int i = x; i <= n; i |= (i + 1)) t[i].push_back(make_pair(y, id)); n   } n   void preSort() { n     for (int i = 0; i <= n; i++) n       sort(t[i].begin(), t[i].end(), n            [](pair<int, int> a, pair<int, int> b) { return a > b; }); n   } n   void expand(int x, int y, queue<int> &q) { n     for (int i = x; i >= 0; i = (i & (i + 1)) - 1) { n       while ((int)(t[i].size()) && t[i].back().first <= y) { n         int id = t[i].back().second; n         if (!used[id]) { n           used[id] = 1; n           q.push(id); n         } n         t[i].pop_back(); n       } n     } n   } n }; n int Bfs(int st) { n   fenwick T(2 * (n + 1)); n   for (int i = 1; i <= n; i++) T.add(m[i], d[i], i); n   T.preSort(); n   queue<int> q; n   q.push(st); n   int cnt = 0; n   while (!q.empty()) { n     int i = q.front(); n     q.pop(); n     int p = ::p[i]; n     int r = ::r[i]; n     T.expand(p, r, q); n     cnt++; n   } n   return cnt - 1; n } n void compress(long long a[], long long b[]) { n   static vector<long long> c; n   c.clear(); n   for (int i = 0; i <= n; i++) { n     c.push_back(a[i]); n     c.push_back(b[i]); n   } n   sort(c.begin(), c.end()); n   for (int i = 0; i <= n; i++) n     a[i] = lower_bound(c.begin(), c.end(), a[i]) - c.begin(); n   for (int i = 0; i <= n; i++) n     b[i] = lower_bound(c.begin(), c.end(), b[i]) - c.begin(); n } n long long dist(int x, int y) { return x * 1ll * x + y * 1ll * y; } n int main() { n   int sx, sy; n   scanf( %d %d %d %d %d , &sx, &sy, p + 0, r + 0, &n); n   for (int i = 1; i <= n; i++) { n     int x, y; n     scanf( %d %d %d %d %d , &x, &y, m + i, p + i, r + i); n     d[i] = dist(x - sx, y - sy); n   } n   for (int i = 0; i <= n; i++) r[i] *= r[i]; n   compress(r, d); n   compress(p, m); n   cout << Bfs(0); n   return 0; n } n


#include <bits/stdc++.h> n const int N = 41; n long long bin_p(long long a, long long b) { n   if (!b) return 1; n   if (b % 2 == 0) { n     long long cur = bin_p(a, b / 2); n     return cur * cur; n   } n   return a * bin_p(a, b - 1); n } n long long color[N]; n std::vector<long long> g[N]; n long long g1[N], cmpSz = 0; n bool fl = false; n void dfs(int v, int clr = 1) { n   color[v] = clr; n   for (auto e : g[v]) { n     if (color[e] == 0) { n       color[e] = (3 - clr); n       dfs(e, 3 - clr); n     } n     if (color[e] == clr) { n       fl = true; n     } n   } n } n const long long LEFT = 22; n const long long MASK_LEFT = (1LL << LEFT) - 1, n                 MASK_RIGHT = ((1LL << 40) - 1) ^ MASK_LEFT; n long long dp1[1LL << 21], dp2[1LL << 21]; n int main() { n   std::ios::sync_with_stdio(false); n   std::cin.tie(0); n   int n, m; n   std::cin >> n >> m; n   for (int i = 0; i < m; ++i) { n     int a, b; n     std::cin >> a >> b; n     --a; n     --b; n     g[a].push_back(b); n     g[b].push_back(a); n     g1[a] |= (1LL << b); n     g1[b] |= (1LL << a); n   } n   long long isolated = 0; n   for (int i = 0; i < n; ++i) { n     if (!color[i]) { n       dfs(i); n       cmpSz++; n     } n     if (g[i].size() == 0) isolated++; n   } n   long long answ = (1LL << n); n   if (m == 0) { n     answ -= (1LL << n); n   } n   if (!fl) { n     answ += (1LL << cmpSz); n   } n   answ += 2LL * (1LL << isolated); n   answ -= (1LL << cmpSz); n   for (long long mask = 0; mask < (1 << std::max(0LL, (n - LEFT))); ++mask) { n     long long realMask = (mask << LEFT), adMask = 0; n     for (long long j = 0; j < n; ++j) { n       if ((realMask >> j) & 1) { n         adMask |= g1[j]; n       } n     } n     if ((adMask & realMask) == 0) { n       dp2[mask]++; n     } n   } n   for (long long j = 0; j < std::max(0LL, n - LEFT); ++j) { n     for (long long msk = 0; msk < (1LL << std::max(0LL, (n - LEFT))); ++msk) { n       if (((msk >> j) & 1)) { n         dp2[msk] += dp2[msk ^ (1LL << j)]; n       } n     } n   } n   for (long long mask = 0; mask < (1 << std::min((long long)n, LEFT)); ++mask) { n     long long adMask = 0; n     for (long long j = 0; j < n; ++j) { n       if ((mask >> j) & 1) { n         adMask |= g1[j]; n       } n     } n     if ((adMask & mask) == 0) { n       answ -= 2 * dp2[(adMask ^ ((1LL << n) - 1)) >> LEFT]; n     } n   } n   std::cout << answ; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 100010; n int ans = -1, ansd, a[MAXN], b[MAXN], n, m; n inline int Pow(int x, int y, int mod) { n   int s = 1; n   for (; y; y >>= 1, x = 1ll * x * x % mod) n     if (y & 1) s = 1ll * s * x % mod; n   return s; n } n inline bool find(int *a, int n, int x) { n   int t = lower_bound(a + 1, a + n + 1, x) - a; n   return a[t] == x; n } n inline void solve(int *a, int n) { n   if (n == 1) { n     ans = a[1], ansd = 1; n     return; n   } n   int tmp = a[2] - a[1], cnt = 0; n   for (int i = 1; i <= n; i++) n     if (find(a, n, (a[i] + tmp) % m)) cnt++; n   ansd = 1ll * tmp * Pow(n - cnt, m - 2, m) % m; n   int d = (m - ansd) % m; n   for (int i = 1; i <= n; i++) n     if (!find(a, n, (a[i] + d) % m)) { n       if (ans == -1) n         ans = a[i]; n       else { n         ans = -1; n         return; n       } n     } n } n int main() { n   scanf( %d%d , &m, &n); n   for (int i = 1; i <= n; i++) scanf( %d , &a[i]); n   sort(a + 1, a + n + 1); n   if (n == 1 || n == m) return printf( %d 1 n , a[1], 1), 0; n   if (2 * n - 4 < m) n     solve(a, n); n   else { n     int t = 0; n     for (int i = 0; i < m; i++) n       if (!find(a, n, i)) b[++t] = i; n     solve(b, t); n     if (ans != -1) (ans += 1ll * ansd * t % m) %= m; n   } n   if (ans == -1) return printf( -1 n ), 0; n   return printf( %d %d n , ans, ansd), 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long int n, k, i; n   while (scanf( %lld %lld , &n, &k) != EOF) { n     vector<long long int> v; n     for (i = 1; i * i <= n; i++) { n       if (n % i == 0) { n         v.push_back(i); n         if ((n / i) != i) v.push_back(n / i); n       } n     } n     sort(v.begin(), v.end()); n     if (v.size() < k) { n       cout <<  -1  << endl; n     } else { n       printf( %lld n , v[k - 1]); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void solve() { n   int i, j, n, ct = 0; n   cin >> n; n   int a[n]; n   for (i = 0; i < n; i++) cin >> a[i]; n   if (n < 2) { n     cout <<  -1 n ; n     return; n   } n   sort(a, a + n); n   set<int> ans; n   if (n == 2) { n     ans.insert(a[1] + a[1] - a[0]); n     ans.insert(a[0] - (a[1] - a[0])); n     if ((a[1] - a[0]) % 2 == 0) ans.insert((a[0] + a[1]) / 2); n   } else { n     set<int> dif; n     for (i = 1; i < n; i++) dif.insert(a[i] - a[i - 1]); n     if (dif.size() == 1) { n       int d = *dif.begin(); n       ans.insert(a[0] - d); n       ans.insert(a[n - 1] + d); n     } else if (dif.size() == 2) { n       int d1 = *dif.begin(); n       int d2 = *dif.upper_bound(d1); n       if (2 * d1 == d2) { n         for (i = 0; i + 1 < n; i++) { n           if (a[i] + d1 == a[i + 1]) continue; n           ct++; n           ans.insert(a[i] + d1); n         } n       } n     } n   } n   if (ct > 1) ans.clear(); n   cout << ans.size() <<   n ; n   for (auto &x : ans) cout << x <<    ; n } n int main() { n   ios_base::sync_with_stdio(false); n   int test = 1; n   while (test--) solve(); n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long int i, j, k, l, m, n; n   cin >> n; n   string s[n], t; n   for (i = 0; i < n; i++) { n     cin >> s[i]; n   } n   map<string, vector<long long int>> mp; n   set<string> ss; n   for (i = 1; i <= 9; i++) { n     for (j = 0; j < n; j++) { n       for (k = 0; k + i - 1 < 9; k++) { n         t = s[j].substr(k, i); n         ss.insert(t); n       } n       while (ss.empty() != true) { n         mp[*(ss.begin())].push_back(j); n         ss.erase(ss.begin()); n       } n     } n   } n   map<string, vector<long long int>>::iterator it; n   string fir; n   map<long long int, long long int> mpp; n   string ans[n]; n   for (it = mp.begin(); it != mp.end(); it++) { n     fir = it->first; n     if (mp[fir].size() == 1 && mpp[mp[fir][0]] == 0) { n       mpp[mp[fir][0]] = 1; n       ans[mp[fir][0]] = fir; n     } else if (mp[fir].size() == 1 && mpp[mp[fir][0]] == 1) { n       if (fir.length() < ans[mp[fir][0]].length()) { n         ans[mp[fir][0]] = fir; n       } n     } n   } n   for (i = 0; i < n; i++) { n     cout << ans[i] << endl; n     ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n string ToStr(char k) { n   stringstream ss; n   ss << k; n   return ss.str(); n } n bool check(string str) { n   int n = (int)str.length(); n   for (int i = 0; i < n / 2; i++) { n     if (str[i] != str[n - i - 1]) return false; n   } n   return true; n } n int main() { n   string s; n   cin >> s; n   for (int i = 0; i <= (int)s.length(); i++) { n     for (int j = 0; j < 26; j++) { n       string tmp = s; n       tmp.insert(i, ToStr( a  + j)); n       if (check(tmp)) { n         cout << tmp << endl; n         return 0; n       } n     } n   } n   cout <<  NA  << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long maxn = 1000 * 101; n const long long INF = 1e18; n long long n, a[maxn]; n vector<pair<char, long long> > ans1; n vector<pair<long long, long long> > ans; n queue<long long> bra; n int main() { n   std::ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   cin >> n; n   for (int i = 1; i <= n; i++) cin >> a[i]; n   a[0] = 0; n   a[n + 1] = 0; n   for (int i = 1; i <= n + 1; i++) { n     if (a[i] > a[i - 1]) { n       long long dis = a[i] - a[i - 1]; n       for (int j = 0; j < dis; j++) ans1.push_back({ ( , i}); n     } else if (a[i] < a[i - 1]) { n       long long dis = a[i - 1] - a[i]; n       for (int j = 0; j < dis; j++) ans1.push_back({ ) , i - 1}); n     } n   } n   for (int i = 0; i < ans1.size(); i++) { n     if (ans1[i].first ==  ( ) { n       bra.push(ans1[i].second); n     } else { n       long long x2 = bra.front(); n       ans.push_back({x2, ans1[i].second}); n       bra.pop(); n     } n   } n   cout << ans.size() <<   n ; n   for (int i = 0; i < ans.size(); i++) { n     cout << ans[i].first <<     << ans[i].second <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int mod = 998244353, kMaxP = 3000007, kMaxN = 200007, root = 15311432, n           root_1 = 469870224, root_pw = 1 << 23; n long long binpow(long long a, long long b) { n   a %= mod; n   long long res = 1; n   while (b > 0) { n     if (b & 1) res = res * a % mod; n     a = a * a % mod; n     b >>= 1; n   } n   return res; n } n int inverse(int a) { return binpow(a, mod - 2); } n int counts[kMaxP]; n vector<vector<int>> polynomials_of_degree[kMaxN]; n void fft(vector<int>& a, bool invert) { n   int n = a.size(); n   for (int i = 1, j = 0; i < n; i++) { n     int bit = n >> 1; n     for (; j & bit; bit >>= 1) j ^= bit; n     j ^= bit; n     if (i < j) swap(a[i], a[j]); n   } n   for (int len = 2; len <= n; len <<= 1) { n     int wlen = invert ? root_1 : root; n     for (int i = len; i < root_pw; i <<= 1) n       wlen = (int)(1LL * wlen * wlen % mod); n     for (int i = 0; i < n; i += len) { n       int w = 1; n       for (int j = 0; j < len / 2; j++) { n         int u = a[i + j], v = (int)(1LL * a[i + j + len / 2] * w % mod); n         a[i + j] = u + v < mod ? u + v : u + v - mod; n         a[i + j + len / 2] = u - v >= 0 ? u - v : u - v + mod; n         w = (int)(1LL * w * wlen % mod); n       } n     } n   } n   if (invert) { n     int n_1 = inverse(n); n     for (int& x : a) x = (int)(1LL * x * n_1 % mod); n   } n } n vector<int> multiply(vector<int> const& a, vector<int> const& b) { n   vector<int> fa(a.begin(), a.end()), fb(b.begin(), b.end()); n   ; n   int n = 1; n   while (n < (static_cast<int>((a).size())) + (static_cast<int>((b).size()))) n     n <<= 1; n   fa.resize(n); n   fb.resize(n); n   fft(fa, false); n   fft(fb, false); n   for (int i = 0; i < n; i++) fa[i] = int64_t{1} * fa[i] * fb[i] % mod; n   fft(fa, true); n   fa.resize((static_cast<int>((a).size())) + (static_cast<int>((b).size())) - n             1); n   ; n   return fa; n } n int solve() { n   int n; n   cin >> n; n   for (int i = n; i--;) { n     int p; n     cin >> p; n     counts[p]++; n   } n   for (int i = 2; i < kMaxP; i++) { n     if (counts[i] > 0) { n       vector<int> all_ones(counts[i] + 1, 1); n       polynomials_of_degree[counts[i]].emplace_back(all_ones); n       ; n     } n   } n   for (int i = 1; true; i++) { n     while (!polynomials_of_degree[i].empty()) { n       vector<int> a = polynomials_of_degree[i].back(); n       polynomials_of_degree[i].pop_back(); n       vector<int> b; n       for (int j = i; j < n; j++) n         if (!polynomials_of_degree[j].empty()) { n           b = polynomials_of_degree[j].back(); n           polynomials_of_degree[j].pop_back(); n           break; n         } n       if (b.empty()) return a[(n >> 1)]; n       polynomials_of_degree[(static_cast<int>((a).size())) - 1 + n                             (static_cast<int>((b).size())) - 1] n           .emplace_back(multiply(a, b)); n       ; n     } n   } n } n int main() { n   ios_base::sync_with_stdio(0), cin.tie(0); n   int test_cases_num = 1; n   for (int i = 1; i <= test_cases_num; i++) { n     if (false) n       if (test_cases_num > 1) n         if (false) cerr <<  Test #  << i <<  : n ; n     cout << solve() <<   n ; n     if (false) n       if (test_cases_num > 1) n         if (false) cerr <<  End of test #  << i <<  . n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n bool gone[1000000]; n int n; n int ord[1000000], put[1000000]; n int main() { n   scanf( %i , &n); n   for (int(a) = 0; (a) < (n); (a)++) scanf( %i , ord + a); n   for (int(a) = 0; (a) < (n); (a)++) scanf( %i , put + a); n   int i = 0; n   for (int(a) = 0; (a) < (n); (a)++) { n     int res = 0; n     if (gone[put[a]] || i >= n) { n       printf( %i  , res); n       continue; n     } n     while (ord[i] != put[a]) { n       gone[ord[i]] = true; n       i++; n       res++; n     } n     gone[ord[i]] = true; n     i++; n     res++; n     printf( %i  , res); n   } n   printf(  n ); n   return 0; n } n


#include <bits/stdc++.h> n enum { MAXN = 102 }; n int reverse[MAXN], target[MAXN], init[MAXN]; n int n; n int eq(int *a, int *b) { n   int i; n   for (i = 1; i <= n; i++) n     if (a[i] != b[i]) return 0; n   return 1; n } n void proc(int *a, int *b) { n   int ans[MAXN], i; n   for (i = 1; i <= n; i++) { n     ans[i] = a[b[i]]; n   } n   for (i = 1; i <= n; i++) a[i] = ans[i]; n } n void print(int *a) { n   int i; n   for (i = 1; i <= n; i++) printf( %d  , a[i]); n   puts(  ); n } n int main(void) { n   int k, m1, m2, i; n   scanf( %d %d , &n, &k); n   for (i = 1; i <= n; i++) { n     scanf( %d , init + i); n     reverse[init[i]] = i; n   } n   for (i = 1; i <= n; i++) scanf( %d , target + i); n   int natur[MAXN]; n   for (i = 1; i <= n; i++) natur[i] = i; n   m1 = 0; n   m2 = 0; n   while (m1 < 1000 && !eq(natur, target)) { n     proc(natur, init); n     m1++; n   } n   if (!eq(natur, target)) m1 = -1; n   for (i = 1; i <= n; i++) natur[i] = i; n   while (m2 < 1000 && !eq(natur, target)) { n     proc(natur, reverse); n     m2++; n   } n   if (!eq(natur, target)) m2 = -1; n   char *ans =  NO ; n   if (m1 == 1 && m2 == 1) { n     if (k == 1) ans =  YES ; n   } else if (m1 == 1 || m2 == 1) { n     if (k % 2 == 1) ans =  YES ; n   } n   if (m1 > 1 && (k - m1) >= 0 && (k - m1) % 2 == 0) ans =  YES ; n   if (m2 > 1 && (k - m2) >= 0 && (k - m2) % 2 == 0) ans =  YES ; n   puts(ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 2e5 + 5; n int R[N], czy[N], dp[N], siz[N]; n pair<int, int> E[N], E2[N]; n int M; n pair<int, int> Find(int x) { n   if (x == R[x]) return {x, 0}; n   pair<int, int> a = Find(R[x]); n   a.second ^= czy[x]; n   return a; n } n void Union(int a, int b) { n   if (a == b) return; n   if (siz[a] > siz[b]) swap(a, b); n   siz[b] += siz[a]; n   R[a] = b; n   czy[a] ^= 1 ^ czy[b]; n   czy[b] = 0; n } n void sep(int a, int b) { n   if (a == b) return; n   czy[a] = 0; n   czy[b] = 0; n   if (siz[a] > siz[b]) swap(a, b); n   siz[b] -= siz[a]; n   R[a] = a; n } n void solve(int l, int r, int a, int b) { n   if (l > r) return; n   int m = (l + r) >> 1; n   dp[m] = N; n   for (int i = l; i < m; i++) { n     pair<int, int> A = Find(E[i].first), B = Find(E[i].second); n     E2[i] = {A.first, B.first}; n     if (A.first == B.first && (A.second ^ B.second ^ 1)) { n       dp[m] = i; n       break; n     } n     if (A.first != B.first) { n       czy[A.first] = A.second; n       czy[B.first] = B.second; n     } n     Union(A.first, B.first); n   } n   if (dp[m] < m) { n     for (int i = dp[m] - 1; i >= l; i--) sep(E2[i].first, E2[i].second); n     solve(l, dp[m], a, b); n     for (int i = dp[m] + 1; i <= r; i++) dp[i] = b; n     return; n   } n   for (int i = b; i >= a; i--) { n     if (i < m) { n       dp[m] = m - 1; n       break; n     } n     pair<int, int> A = Find(E[i].first), B = Find(E[i].second); n     E2[i] = {A.first, B.first}; n     if (A.first == B.first && (A.second ^ B.second ^ 1)) { n       dp[m] = i; n       break; n     } n     if (A.first != B.first) { n       czy[A.first] = A.second; n       czy[B.first] = B.second; n     } n     Union(A.first, B.first); n   } n   for (int i = dp[m] + 1; i <= b; i++) sep(E2[i].first, E2[i].second); n   pair<int, int> A = Find(E[m].first), B = Find(E[m].second); n   if (A.first == B.first && (A.second ^ B.second ^ 1)) { n     for (int i = m + 1; i <= r; i++) dp[i] = b; n   } else { n     if (A.first != B.first) { n       czy[A.first] = A.second; n       czy[B.first] = B.second; n     } n     Union(A.first, B.first); n     solve(m + 1, r, dp[m], b); n     sep(A.first, B.first); n   } n   for (int i = m - 1; i >= l; i--) { n     sep(E2[i].first, E2[i].second); n   } n   for (int i = b; i > dp[m]; i--) { n     A = Find(E[i].first), B = Find(E[i].second); n     E2[i] = {A.first, B.first}; n     if (A.first != B.first) { n       czy[A.first] = A.second; n       czy[B.first] = B.second; n     } n     Union(A.first, B.first); n   } n   solve(l, m - 1, a, dp[m]); n   for (int i = dp[m] + 1; i <= b; i++) sep(E2[i].first, E2[i].second); n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   int n, m, q; n   cin >> n >> m >> q; n   M = m; n   for (int i = 1; i <= n + 2; i++) { n     R[i] = i; n     siz[i] = 1; n   } n   for (int i = 1; i <= m; i++) { n     cin >> E[i].first >> E[i].second; n   } n   E[m + 1] = {n + 1, n + 2}; n   solve(1, m, 0, m + 1); n   while (q--) { n     int l, r; n     cin >> l >> r; n     if (r >= dp[l]) n       cout <<  NO n ; n     else n       cout <<  YES n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int n, m; n long long pw[19]; n long long val[19]; n char A[100009]; n string s[100009]; n int main() { n   scanf( %s , A); n   scanf( %d , &m); n   for (int i = 0; i < 10; i++) { n     val[i] = i; n     pw[i] = 10; n   } n   for (int i = 0; i < m; i++) cin >> s[i]; n   for (int j = m - 1; j >= 0; j--) { n     int ln = s[j].length(); n     int nm = s[j][0] -  0 ; n     long long nmpw = 1; n     long long san = 0; n     for (int i = ln - 1; i > 2; i--) { n       san = (san + (nmpw * val[s[j][i] -  0 ])) % 1000000007; n       nmpw = (nmpw * pw[s[j][i] -  0 ]) % 1000000007; n     } n     val[nm] = san; n     pw[nm] = nmpw; n   } n   int ln = strlen(A); n   long long ans = 0; n   long long nmpw = 1; n   for (int i = ln - 1; i >= 0; i--) { n     ans = (ans + (val[A[i] -  0 ] * nmpw)) % 1000000007; n     nmpw = (nmpw * pw[A[i] -  0 ]) % 1000000007; n   } n   printf( %lld n , ans); n } n


#include <bits/stdc++.h> n using namespace std; n inline bool EQ(double a, double b) { return fabs(a - b) < 1e-9; } n const int INF = (((1 << 30) - 1) << 1) + 1; n const int nINF = 1 << 31; n string s; n vector<int> v[2], ans; n int n, t; n void print(vector<int>& vec) { n   for (int x : vec) cout << x <<    ; n   cout << endl; n } n signed main() { n   ios::sync_with_stdio(false); n   cin >> t; n   while (t--) { n     v[0].clear(), v[1].clear(), ans.clear(); n     cin >> n >> s; n     bool good = true, two = false; n     int lo = 10; n     for (int i = 0; i < (n); i++) { n       if (two) { n         if (s[i] == lo && s[v[1].back()] == lo) { n           v[1].push_back(i); n         } else if (s[i] <= lo) { n           if (!v[0].empty() && s[i] < s[v[0].back()]) { n             good = false; n             break; n           } else { n             v[0].push_back(i); n           } n         } else { n           if (!v[1].empty() && s[i] < s[v[1].back()]) { n             good = false; n             break; n           } else { n             v[1].push_back(i); n           } n         } n       } else { n         if (!v[0].empty() && s[i] < s[v[0].back()]) { n           two = true; n           vector<int> temp; n           while (!v[0].empty() && s[i] < s[v[0].back()]) { n             temp.push_back(v[0].back()); n             v[0].pop_back(); n           } n           lo = s[temp.back()]; n           while (!temp.empty()) { n             v[1].push_back(temp.back()); n             temp.pop_back(); n           } n         } n         v[0].push_back(i); n       } n     } n     ans.resize(n, 1); n     for (int i = 0; i < (v[1].size()); i++) { n       ans[v[1][i]] = 2; n     } n     if (!good) n       cout <<  -  << endl; n     else { n       for (int x : ans) cout << x; n       cout << endl; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long oo = 0x3f3f3f3f3f3f3f3fll; n const int LGN = 25; n const int MOD = 1e9 + 7; n const int N = 3e5 + 5; n const int hashmod[] = {(int)1e9 + 7, (int)1e9 + 9}; n const int hashlen = sizeof(hashmod) / sizeof(int); n struct Hash { n   static long long mul; n   static long long div[hashlen]; n   static long long power(long long a, long long b, long long m) { n     long long ans = 1; n     while (b > 0) { n       if (b & 1) ans = (ans * a) % m; n       b >>= 1; n       a = (a * a) % m; n     } n     return ans; n   } n   static void init() { n     static bool ok; n     if (ok) return; n     ok = true; n     for (int i = 0; i < hashlen; i++) { n       div[i] = power(mul, hashmod[i] - 2, hashmod[i]); n     } n   } n   int h[hashlen]; n   Hash(char s) { n     memset(h, 0, sizeof h); n     operator+=(s); n   } n   Hash(const string& s =   ) { n     memset(h, 0, sizeof h); n     operator+=(s); n   } n   bool operator<(const Hash& o) const { n     for (int i = 0; i < hashlen; i++) n       if (h[i] != o.h[i]) return h[i] < o.h[i]; n     return false; n   } n   bool operator==(const Hash& o) const { n     for (int i = 0; i < hashlen; i++) n       if (h[i] != o.h[i]) return false; n     return true; n   } n   bool operator!=(const Hash& o) const { n     for (int i = 0; i < hashlen; i++) n       if (h[i] != o.h[i]) return true; n     return false; n   } n   Hash& operator+=(char s) { n     int c = s + 1; n     for (int i = 0; i < hashlen; i++) h[i] = (h[i] * mul + c) % hashmod[i]; n     return *this; n   } n   Hash& operator+=(const string& s) { n     for (char c : s) operator+=(c); n     return *this; n   } n   Hash& operator+=(const Hash& o) { n     for (int i = 0; i < hashlen; i++) h[i] = (h[i] + o.h[i]) % hashmod[i]; n     return *this; n   } n   Hash& operator-=(char s) { n     int c = s + 1; n     for (int i = 0; i < hashlen; i++) { n       h[i] = ((h[i] + hashmod[i] - c) * div[i]) % hashmod[i]; n     } n     return *this; n   } n   Hash& operator-=(const string& s) { n     for (int i = s.size() - 1; 0 <= i; i--) operator-=(s[i]); n     return *this; n   } n   Hash& operator-=(const Hash& o) { n     for (int i = 0; i < hashlen; i++) { n       h[i] = (h[i] + hashmod[i] - o.h[i]) % hashmod[i]; n     } n     return *this; n   } n   Hash& operator*=(long long pwr) { n     for (int i = 0; i < hashlen; i++) { n       h[i] = (h[i] * power(mul, pwr, hashmod[i])) % hashmod[i]; n     } n     return *this; n   } n   Hash& operator/=(long long pwr) { n     for (int i = 0; i < hashlen; i++) { n       h[i] = (h[i] * power(div[i], pwr, hashmod[i])) % hashmod[i]; n     } n     return *this; n   } n   Hash operator+(char s) const { n     Hash ans = *this; n     ans += s; n     return ans; n   } n   Hash operator+(const string& s) const { n     Hash ans = *this; n     ans += s; n     return ans; n   } n   Hash operator+(const Hash& o) const { n     Hash ans = *this; n     ans += o; n     return ans; n   } n   Hash operator-(char s) const { n     Hash ans = *this; n     ans -= s; n     return ans; n   } n   Hash operator-(const string& s) const { n     Hash ans = *this; n     ans -= s; n     return ans; n   } n   Hash operator-(const Hash& o) const { n     Hash ans = *this; n     ans -= o; n     return ans; n   } n   Hash operator*(long long pwr) const { n     Hash ans = *this; n     ans *= pwr; n     return ans; n   } n   Hash operator/(long long pwr) const { n     Hash ans = *this; n     ans /= pwr; n     return ans; n   } n }; n long long Hash::mul = 257; n long long Hash::div[hashlen]; n int n, x[N * 2], y[N * 2]; n char c[N * 2]; n vector<int> T[N]; n int depans[N]; n Hash h[N]; n int sub[N], big[N], dep[N], depcnt[N]; n void dfs1(int u = 1, int p = 0, Hash h = 0, int d = 1) { n   ::h[u] = h; n   sub[u] = 1; n   dep[u] = d; n   depcnt[d]++; n   for (int i : T[u]) n     if (y[i] != p) { n       int v = y[i]; n       dfs1(v, u, h + c[i], d + 1); n       sub[u] += sub[v]; n       if (sub[big[u]] < sub[v]) big[u] = v; n     } n } n set<Hash> s[N]; n void add(set<Hash>& tmp, int u, int p, Hash h) { n   tmp.insert(h); n   for (int i : T[u]) n     if (y[i] != p) add(tmp, y[i], u, h + c[i]); n } n void dfs2(int u = 1, int p = 0, bool keep = false) { n   if (!big[u]) { n     depans[dep[u]]++; n     return; n   } n   for (int i : T[u]) n     if (y[i] != p && y[i] != big[u]) dfs2(y[i], u); n   dfs2(big[u], u, true), s[u].swap(s[big[u]]), s[u].insert(h[big[u]]); n   set<Hash> sml; n   for (int i : T[u]) n     if (y[i] != p && y[i] != big[u]) add(sml, y[i], u, h[big[u]]); n   int ans = s[u].size(); n   for (auto& h : sml) n     if (!s[u].count(h)) ans++; n   depans[dep[u]] += ans; n   for (int i : T[u]) n     if (y[i] != p && y[i] != big[u]) add(s[u], y[i], u, h[y[i]]); n   if (!keep) s[u].clear(); n } n int main() { n   std::ios::sync_with_stdio(false); n   scanf( %d , &(n)); n   int cnt = 0; n   for (long long i = 2, __R = n; i <= __R; i++) { n     int u, v; n     scanf( %d%d , &(u), &(v)); n     char c[2]; n     scanf( %s , c); n     x[++cnt] = u, y[cnt] = v, ::c[cnt] = c[0], T[u].push_back(cnt); n     x[++cnt] = v, y[cnt] = u, ::c[cnt] = c[0], T[v].push_back(cnt); n   } n   dfs1(); n   dfs2(); n   int depsum = 0, ans = oo, p; n   for (long long d = 1, __R = n; d <= __R; d++) { n     depsum += depcnt[d - 1]; n     int alt = depsum + depans[d]; n     if (alt < ans) ans = alt, p = d; n   } n   printf( %d n%d n , ans, p); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long mod = 1000000007; n int n, m; n int popCount(int x) { n   int count = 0; n   while (x) { n     ++count; n     x &= x - 1; n   } n   return count; n } n inline bool Okay(int x) { return (((1 << m) - 1) & x) == ((1 << m) - 1); } n int main() { n   ios::sync_with_stdio(false); n   cin >> n >> m; n   if (n < m) swap(n, m); n   int*** Arr = new int**[n + 1]; n   for (int i = 0; i <= n; ++i) { n     Arr[i] = new int*[1 << m]; n     for (int j = 0; j < (1 << m); ++j) { n       Arr[i][j] = new int[1 << m]; n       for (int k = 0; k < (1 << m); ++k) { n         Arr[i][j][k] = 100000000; n       } n     } n   } n   Arr[0][(1 << m) - 1][0] = 0; n   for (int i = 0; i < n; ++i) { n     for (int j = 0; j < (1 << m); ++j) { n       for (int k = 0; k < (1 << m); ++k) { n         for (int t = 0; t < (1 << m); ++t) { n           if (Okay(j | k | t | (k >> 1) | (k << 1))) n             Arr[i + 1][k][t] = n                 min(Arr[i + 1][k][t], Arr[i][j][k] + popCount(t)); n         } n       } n     } n   } n   int _ans = 100000; n   for (int k = 0; k < (1 << m); ++k) { n     for (int t = 0; t < (1 << m); ++t) { n       if (Okay(k | t | (t << 1) | (t >> 1))) _ans = min(_ans, Arr[n][k][t]); n     } n   } n   cout << n * m - _ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, l, k; n double probs[200]; n int bags[200]; n double dp[201][410][201]; n double solve(int index, int currCapacity, int wonTours) { n   if (index == n) { n     if (wonTours >= l) { n       if (currCapacity >= 0) n         return 1.0; n       else n         return 0.0; n     } else { n       return 0.0; n     } n   } n   double &calculated = dp[index][currCapacity + 201][wonTours]; n   if (calculated + 1.0 > 1e-4) return calculated; n   double ans = 0.0; n   ans = probs[index] * solve(index + 1, min(405, currCapacity + bags[index]), n                              wonTours + 1) + n         (1.0 - probs[index]) * solve(index + 1, currCapacity, wonTours); n   return calculated = ans; n } n int main() { n   cin >> n >> l >> k; n   memset(dp, -1.0, sizeof(dp)); n   for (int i = 0; i <= n - 1; i++) { n     cin >> probs[i]; n     probs[i] /= 100.0; n   } n   for (int i = 0; i <= n - 1; i++) { n     cin >> bags[i]; n   } n   printf( %.10f , solve(0, k, 0)); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int i, n, sum = 0, t; n   vector<int> a; n   cin >> n; n   for (i = 0; i < n; i++) { n     cin >> t; n     sum += t; n     a.push_back(sum); n   } n   cin >> n; n   for (i = 0; i < n; i++) { n     cin >> t; n     cout << lower_bound(a.begin(), a.end(), t) - a.begin() + 1 <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   int n; n   cin >> n; n   if (n != 2) { n     cout << 1 <<   n ; n   } else { n     cout << 2 <<   n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n void DBG() { n   cerr <<  ]  n        <<   n ; n } n template <class H, class... T> n void DBG(H h, T... t) { n   cerr << h; n   if (sizeof...(t)) cerr <<  ,  ; n   DBG(t...); n } n template <class H> n void DBG(vector<H> v) { n   for (int i = 0; i < (int)v.size(); i++) { n     cerr << v[i]; n     if (i != (int)v.size() - 1) cerr <<  ,  ; n   } n   cerr <<  ]  <<   n ; n } n long long randNum(long long a, long long b) { return a + rand() % (b - a + 1); } n long long mPow(long long b, long long p, long long m) { n   b %= m; n   long long result = 1; n   while (p > 0) { n     if (p & 1) result = (result * b) % m; n     b = (b * b) % m; n     p >>= 1; n   } n   return result; n } n long long gcdExtended(long long a, long long b, long long *x, long long *y) { n   if (a == 0) { n     *x = 0, *y = 1; n     return b; n   } n   long long x1, y1; n   long long gcd = gcdExtended(b % a, a, &x1, &y1); n   *x = y1 - (b / a) * x1; n   *y = x1; n   return gcd; n } n long long modInverse(long long b, long long m) { n   long long x, y; n   long long g = gcdExtended(b, m, &x, &y); n   if (g != 1) return -1; n   return (x % m + m) % m; n } n long long modDivide(long long a, long long b, long long m) { n   a = a % m; n   long long inv = modInverse(b, m); n   if (inv == -1) n     return -1; n   else n     return (inv * a) % m; n } n unsigned long long nCrModPFermat(unsigned long long n, int r, int p) { n   if (n < (unsigned long long)r) return 0; n   if (r == 0) return 1; n   unsigned long long fac[n + 1]; n   fac[0] = 1; n   for (unsigned long long i = 1; i <= n; i++) fac[i] = (fac[i - 1] * i) % p; n   return (fac[n] * modInverse(fac[r], p) % p * modInverse(fac[n - r], p) % p) % n          p; n } n bool sortfunc(pair<int, int> a, pair<int, int> b) { return a.first < b.first; } n long long gcd(long long a, long long b) { return (b == 0) ? a : gcd(b, a % b); } n long long lcm(long long a, long long b) { return (a / gcd(a, b)) * b; } n const int dx4[4] = {-1, 0, 1, 0}, dy4[4] = {0, -1, 0, 1}; n const int dx8[8] = {-1, -1, -1, 0, 0, 1, 1, 1}, n           dy8[8] = {0, 1, -1, -1, 1, 0, 1, -1}; n void solve() { n   vector<string> s; n   int n, m; n   cin >> n >> m; n   string temp; n   vector<int> cnt(m, 0); n   for (int i = 0; i < n; i++) { n     cin >> temp; n     for (int j = 0; j < m; j++) { n       if (temp[j] ==  1 ) { n         cnt[j]++; n       } n     } n     s.push_back(temp); n   } n   for (int i = 0; i < n; i++) { n     bool flag = true; n     for (int j = 0; j < m; j++) { n       if (s[i][j] ==  0  and cnt[j] == 0) { n         flag = false; n         break; n       } n       if (s[i][j] ==  1  and cnt[j] - 1 == 0) { n         flag = false; n         break; n       } n     } n     if (flag) { n       cout <<  YES  n            <<   n ; n       return; n     } n   } n   cout <<  NO  n        <<   n ; n } n signed main() { n   ios::sync_with_stdio(false); n   cout.tie(NULL); n   cin.tie(NULL); n   solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MN = 100005, inf = 1000000005, mod = 1000000007; n const long long INF = 1000000000000000005LL; n const long double eps = (long double)1e-10; n long long det(pair<int, int> x, pair<int, int> y) { n   return 1LL * x.first * y.second - 1LL * x.second * y.first; n } n long long det_pts(pair<int, int> x, pair<int, int> y, pair<int, int> z) { n   y.first -= x.first; n   y.second -= x.second; n   z.first -= x.first; n   z.second -= x.second; n   return det(y, z); n } n long double detf(pair<long double, long double> x, n                  pair<long double, long double> y) { n   return x.first * y.second - x.second * y.first; n } n namespace hull { n vector<pair<int, int> > half_oto(vector<pair<int, int> >& P) { n   vector<pair<int, int> > H; n   for (auto p : P) { n     while (H.size() > 1 && det_pts(H[H.size() - 2], H.back(), p) >= 0LL) n       H.pop_back(); n     H.push_back(p); n   } n   return H; n } n vector<pair<int, int> > oto(vector<pair<int, int> > P) { n   vector<pair<int, int> > L, U, ret; n   sort((P).begin(), (P).end()); n   U = half_oto(P); n   reverse((P).begin(), (P).end()); n   L = half_oto(P); n   int su = U.size(), sl = L.size(); n   for (int i = 0; i < su; ++i) ret.push_back(U[i]); n   for (int i = 1; i < sl - 1; ++i) ret.push_back(L[i]); n   return ret; n } n };  // namespace hull n pair<long double, long double> daj(pair<long double, long double> a, n                                    pair<long double, long double> b, n                                    long double alfa) { n   return {a.first * alfa + b.first * (1.0 - alfa), n           a.second * alfa + b.second * (1.0 - alfa)}; n } n long double P, Q; n long double wyn(pair<long double, long double> p) { n   if (min(p.first, p.second) < eps) return (long double)INF; n   return max(P / p.first, Q / p.second); n } n long double tern(pair<int, int> a, pair<int, int> b) { n   pair<long double, long double> af = {(long double)a.first, n                                        (long double)a.second}; n   pair<long double, long double> bf = {(long double)b.first, n                                        (long double)b.second}; n   long double zd = 0.0, zg = 1.0; n   for (int i = 1; i <= 60; ++i) { n     long double one_th = (zg - zd) / 3.0, a1 = zd + one_th, a2 = zg - one_th; n     pair<long double, long double> l = daj(af, bf, a1), r = daj(af, bf, a2); n     if (wyn(l) > wyn(r)) n       zd = a1; n     else n       zg = a2; n   } n   long double alfa = (zd + zg) / 2.0; n   return wyn(daj(af, bf, alfa)); n } n int main() { n   int n, p, q; n   scanf( %d%d%d , &n, &p, &q); n   P = p, Q = q; n   vector<pair<int, int> > pts; n   for (int i = 1; i <= n; ++i) { n     int a, b; n     scanf( %d%d , &a, &b); n     pair<int, int> p = {a, b}; n     pts.push_back(p); n     p = {a, 0}; n     pts.push_back(p); n     p = {0, b}; n     pts.push_back(p); n   } n   pts = hull::oto(pts); n   int s = pts.size(); n   long double ans = (long double)INF; n   for (int i = 1; i < s; ++i) { n     auto cur_wyn = tern(pts[i - 1], pts[i]); n     ans = min(ans, cur_wyn); n   } n   printf( %.9Lf , ans); n } n


#include <bits/stdc++.h> n using namespace std; n const long long Llinf = LLONG_MAX; n const int Inf = INT_MAX; n const int Maxn = 400 + 10; n const int Mod = 1e9 + 7; n char* _c = new char[Maxn]; n string GetString() { n   scanf( %s , _c); n   return string(_c); n } n int n; n string s[Maxn]; n string ns[Maxn]; n unordered_map<string, bool> m; n int ans; n int main() { n   scanf( %d , &n); n   for (int i = 0; i < n; i++) s[i] = GetString(); n   for (int i = 0; i < n; i++) n     for (int j = 0; j < s[i].size(); j++) { n       if (s[i][j] ==  u ) s[i][j] =  o , s[i].insert(j + 1,  o ); n       if (s[i][j] ==  h ) { n         int k = j - 1; n         while (k >= 0 && s[i][k] ==  k ) s[i][k] =  . , k--; n       } n     } n   for (int i = 0; i < n; i++) n     for (int j = 0; j < s[i].size(); j++) n       if (s[i][j] !=  . ) ns[i].push_back(s[i][j]); n   for (int i = 0; i < n; i++) n     if (!m[ns[i]]) ans++, m[ns[i]] = true; n   printf( %d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int digit, base, pos, n; n   long long int sum = 0, sum_1 = 0, weight; n   cin >> digit >> base; n   pos = digit - 1; n   for (int i = 0; i < digit; i++, pos--) { n     cin >> n; n     weight = base; n     if (pos == 0) weight = 1; n     for (int j = 1; j < pos; j++) { n       weight *= base; n     } n     sum += n * weight; n   } n   cin >> digit >> base; n   pos = digit - 1; n   for (int i = 0; i < digit; i++, pos--) { n     cin >> n; n     weight = base; n     if (pos == 0) weight = 1; n     for (int j = 1; j < pos; j++) { n       weight *= base; n     } n     sum_1 += n * weight; n   } n   if (sum == sum_1) n     cout <<  =  << endl; n   else if (sum > sum_1) n     cout <<  >  << endl; n   else n     cout <<  <  << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long maxn = 200005; n struct node { n   long long ab; n   long long cab; n } a[maxn], b[maxn]; n struct mon { n   long long x, y, z; n } mo[maxn]; n bool cmpab(struct node ab1, struct node ab2) { return ab1.ab < ab2.ab; } n bool cmpm(struct mon mo1, struct mon mo2) { return mo1.x < mo2.x; } n struct cmp1 { n   bool operator()(const node &a, const node &b) const { return a.ab < b.ab; } n }; n long long trb[4 * maxn + 1] = {0}, trbk[4 * maxn + 1] = {0}; n void build(long long k, long long l, long long r, long long l1, long long r1, n            long long val) { n   if (l1 <= l && r <= r1) { n     trbk[k] += val; n   } else if (!(r < l1 || r1 < l)) { n     long long m = (l + r) / 2; n     trbk[k * 2 + 2] += trbk[k], trbk[k * 2 + 1] += trbk[k]; n     trbk[k] = 0; n     build(k * 2 + 1, l, m, l1, r1, val); n     build(k * 2 + 2, m + 1, r, l1, r1, val); n     trb[k] = n         max(trb[k * 2 + 1] + trbk[k * 2 + 1], trb[k * 2 + 2] + trbk[k * 2 + 2]); n   } else { n     return; n   } n } n long long cal(long long k, long long l, long long r, long long l1, n               long long r1) { n   if (l1 <= l && r <= r1) { n     return trb[k] + trbk[k]; n   } else if (!(r < l1 || r1 < l)) { n     long long m = (l + r) / 2; n     trbk[k * 2 + 2] += trbk[k], trbk[k * 2 + 1] += trbk[k]; n     trbk[k] = 0; n     trb[k] = n         max(trb[k * 2 + 1] + trbk[k * 2 + 1], trb[k * 2 + 2] + trbk[k * 2 + 2]); n     long long ans1 = cal(k * 2 + 1, l, m, l1, r1); n     long long ans2 = cal(k * 2 + 2, m + 1, r, l1, r1); n     return max(ans1, ans2); n   } else { n     return 0; n   } n } n long long bi(long long l, long long r, long long val) { n   long long m = (l + r) / 2; n   if (l == r) { n     return m; n   } else if (b[m].ab <= val) { n     return bi(m + 1, r, val); n   } else { n     return bi(l, m, val); n   } n } n int main(void) { n   long long n, m, p; n   scanf( %lld%lld%lld , &n, &m, &p); n   for (long long i = 0; i < n; i++) { n     scanf( %lld%lld , &a[i].ab, &a[i].cab); n   } n   for (long long i = 0; i < m; i++) { n     scanf( %lld%lld , &b[i].ab, &b[i].cab); n   } n   for (long long i = 0; i < p; i++) { n     scanf( %lld%lld%lld , &mo[i].x, &mo[i].y, &mo[i].z); n   } n   sort(a, a + n, cmpab); n   sort(b, b + m, cmpab); n   sort(mo, mo + p, cmpm); n   long long ma = -a[0].cab - b[0].cab; n   long long monu = 0; n   for (long long i = 0; i < m; i++) { n     build(0, 0, m - 1, i, i, -b[i].cab); n   } n   for (long long i = 0; i < n; i++) { n     while (a[i].ab > mo[monu].x && monu < p) { n       if (mo[monu].y >= b[m - 1].ab) { n         monu++; n         continue; n       } n       long long num = bi(0, m - 1, mo[monu].y); n       build(0, 0, m - 1, num, m - 1, mo[monu].z); n       monu++; n     } n     ma = max(ma, -a[i].cab + cal(0, 0, m - 1, 0, m - 1)); n   } n   printf( %lld , ma); n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long double m, n, c = 0, k = 0, a = 0, b = 0, d; n   cin >> m >> n >> c >> d; n   long double s1, s2, x, y; n   a = (3 * m) / 10; n   b = m - (m / 250) * c; n   x = (3 * n) / 10; n   y = n - (n / 250) * d; n   s1 = max(a, b); n   s2 = max(x, y); n   if (s1 > s2) n     cout <<  Misha  << endl; n   else if (s1 < s2) n     cout <<  Vasya  << endl; n   else n     cout <<  Tie  << endl; n } n


#include <bits/stdc++.h> n using namespace std; n long long res; n const long long mod = 1073741824; n long long d(int a, int b, int c) { n   vector<int> q(102, 0); n   for (int i = 2; i <= a; i++) n     if (a % i == 0) { n       while (a % i == 0) { n         a /= i; n         q[i]++; n       } n     } n   for (int i = 2; i <= b; i++) n     if (b % i == 0) { n       while (b % i == 0) { n         b /= i; n         q[i]++; n       } n     } n   for (int i = 2; i <= c; i++) n     if (c % i == 0) { n       while (c % i == 0) { n         c /= i; n         q[i]++; n       } n     } n   long long res = 1; n   for (int i = 0; i < 102; i++) res = (res * (q[i] + 1)) % mod; n   return res; n } n int main() { n   int a, b, c; n   cin >> a >> b >> c; n   for (int i = 1; i <= a; i++) n     for (int j = 1; j <= b; j++) n       for (int k = 1; k <= c; k++) res = (res % mod + d(i, j, k) % mod) % mod; n   cout << res; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 5010; n const int mod = 998244353; n long long d[N][N]; n long long p[N]; n long long inv[N]; n long long _inv[N]; n long long f[N]; n void init(int n) { n   d[0][0] = 1; n   for (int i = 1; i <= n; i++) { n     for (int j = 1; j <= i; j++) { n       d[i][j] = n           (d[i - 1][j - 1] * (i - j + 1) % mod + d[i - 1][j] * j % mod) % mod; n     } n   } n   p[0] = p[1] = inv[0] = inv[1] = _inv[0] = _inv[1] = 1; n   for (int i = 2; i <= n; i++) { n     p[i] = p[i - 1] * i % mod; n     inv[i] = (mod - mod / i) * inv[mod % i] % mod; n     _inv[i] = _inv[i - 1] * inv[i] % mod; n   } n } n int ans[N]; n int main() { n   int n; n   cin >> n; n   init(n); n   for (int i = 1; i <= n; i++) n     for (int j = 1; j <= n; j++) { n       ans[i] = (ans[i] + d[j][i] * _inv[j] % mod) % mod; n     } n   for (int i = 1; i <= n; i++) ans[i] = ans[i] * p[n] % mod; n   for (int i = 1; i <= n; i++) cout << ans[i] <<    ; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1000010; n vector<pair<long long, long long> > g[N]; n int n, m; n stack<int> st; n bool inStack[N]; n int cmp[N], ind[N], low[N], cnt, cont; n void dfs(int x) { n   ind[x] = low[x] = cnt++; n   st.push(x); n   inStack[x] = true; n   for (int i = 0; i < g[x].size(); ++i) { n     int y = g[x][i].first; n     if (ind[y] == -1) { n       dfs(y); n       low[x] = min(low[x], low[y]); n     } else if (inStack[y]) { n       low[x] = min(low[x], ind[y]); n     } n   } n   if (ind[x] == low[x]) { n     int idx; n     do { n       idx = st.top(); n       st.pop(); n       inStack[idx] = 0; n       cmp[idx] = cont; n     } while (idx != x); n     cont++; n   } n } n inline void initTarjan() { memset(ind, -1, sizeof ind); } n bool componentProcessed[N]; n vector<pair<long long, long long> > compressedGraph[N]; n long long componentCost[N]; n inline long long getCost(long long x) { n   long long lo = 0, hi = 1000000LL; n   while (lo < hi) { n     long long md = (lo + hi + 1) >> 1; n     if ((md * (md - 1)) / 2LL <= x) n       lo = md; n     else n       hi = md - 1; n   } n   return x * lo - lo * (lo + 1) * (lo - 1) / 6LL; n } n long long pd[N]; n long long func(int x) { n   if (pd[x] != -1) return pd[x]; n   pd[x] = componentCost[x]; n   for (int i = 0; i < compressedGraph[x].size(); ++i) { n     int y = compressedGraph[x][i].first; n     long long w = compressedGraph[x][i].second; n     pd[x] = max(pd[x], func(y) + w + componentCost[x]); n   } n   return pd[x]; n } n int main() { n   initTarjan(); n   scanf( %d %d , &n, &m); n   for (int i = 0; i < m; ++i) { n     int a, b, c; n     scanf( %d %d %d , &a, &b, &c); n     g[a].emplace_back(make_pair(b, c)); n   } n   for (int i = 1; i <= n; ++i) { n     if (ind[i] == -1) dfs(i); n   } n   int s; n   scanf( %d , &s); n   for (int x = 1; x <= n; ++x) { n     for (int j = 0; j < g[x].size(); ++j) { n       int y = g[x][j].first; n       if (cmp[x] != cmp[y]) n         compressedGraph[cmp[x]].emplace_back(make_pair(cmp[y], g[x][j].second)); n       else n         componentCost[cmp[x]] += getCost(g[x][j].second); n     } n   } n   memset(pd, -1, sizeof pd); n   printf( %lld n , func(cmp[s])); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   long long ar[51]; n   vector<vector<long long>> price(5); n   long long currentBalance = 0; n   cin >> n; n   for (int i = 0; i < n; i++) { n     cin >> ar[i]; n   } n   for (int i = 0; i < 5; i++) { n     price[i].assign(2, 0); n     cin >> price[i][0]; n   } n   for (int i = 0; i < n; i++) { n     currentBalance += ar[i]; n     for (int j = 4; j >= 0; j--) { n       int k = currentBalance / price[j][0]; n       price[j][1] += k; n       currentBalance -= k * price[j][0]; n     } n   } n   for (int i = 0; i < 5; i++) { n     cout << price[i][1] <<    ; n   } n   cout <<   n  << currentBalance; n } n


#include <bits/stdc++.h> n using namespace std; n inline long long read() { n   long long a = 0, b = 1; n   char c = getchar(); n   while (!isdigit(c)) { n     if (c ==  - ) b = -1; n     c = getchar(); n   } n   while (isdigit(c)) { n     a = a * 10 + c -  0 ; n     c = getchar(); n   } n   return a * b; n } n const long long N = 3e5 + 50, M = 6e5 + 50; n long long n, m, w[N], tot, h[N], nx[M], ver[M], ed[M], dat[N * 8], lazy[N * 8], n     ans; n void add(long long u, long long v, long long z) { n   ver[++tot] = v; n   ed[tot] = z; n   nx[tot] = h[u]; n   h[u] = tot; n } n void spread(long long p) { n   if (lazy[p]) { n     dat[(p << 1)] += lazy[p]; n     dat[(p << 1 | 1)] += lazy[p]; n     lazy[(p << 1)] += lazy[p]; n     lazy[(p << 1 | 1)] += lazy[p]; n     lazy[p] = 0; n   } n } n void change(long long p, long long l, long long r, long long u, long long v, n             long long z) { n   if (l >= u && r <= v) { n     dat[p] += z; n     lazy[p] += z; n     return; n   } n   spread(p); n   long long mid = (l + r) >> 1; n   if (u <= mid) change((p << 1), l, mid, u, v, z); n   if (v > mid) change((p << 1 | 1), mid + 1, r, u, v, z); n   dat[p] = max(dat[(p << 1)], dat[(p << 1 | 1)]); n } n signed main() { n   n = read(); n   m = read(); n   for (register long long i = 1; i <= n; i++) { n     w[i] = read(); n   } n   for (register long long i = 1, u, v, z; i <= m; i++) { n     u = read(); n     v = read(); n     z = read(); n     add(v, u, z); n   } n   for (register long long i = 1; i <= n; i++) { n     change(1, 1, n, i, i, ans); n     change(1, 1, n, 1, i, -w[i]); n     for (register long long j = h[i]; j; j = nx[j]) { n       change(1, 1, n, 1, ver[j], ed[j]); n     } n     ans = max(ans, dat[1]); n   } n   printf( %lld n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 4010; n int n, m, a[N], rt, tot, L[N], siz[N], R[N], ls[N], rs[N]; n long long val[N], dp[N][N]; n inline void build(int &p, int l, int r) { n   if (l > r) return; n   p = ++tot; n   int pos = l; n   for (int i = l; i <= r; ++i) n     if (a[i] < a[pos]) pos = i; n   val[p] = a[pos]; n   build(ls[p], l, pos - 1); n   build(rs[p], pos + 1, r); n   dp[p][0] = 0; n   dp[p][1] = m * val[p] - val[p]; n   siz[p] = 1; n   for (int i = siz[p]; i >= 0; --i) n     for (int j = 0; j <= siz[ls[p]]; ++j) n       dp[p][i + j] = n           max(dp[p][i + j], dp[p][i] + dp[ls[p]][j] - 2ll * i * j * val[p]); n   siz[p] += siz[ls[p]]; n   for (int i = siz[p]; i >= 0; --i) n     for (int j = 0; j <= siz[rs[p]]; ++j) n       dp[p][i + j] = n           max(dp[p][i + j], dp[p][i] + dp[rs[p]][j] - 2ll * i * j * val[p]); n   siz[p] += siz[rs[p]]; n } n int main() { n   memset(dp, -0x3f, sizeof(dp)); n   scanf( %d%d , &n, &m); n   for (int i = 1; i <= n; ++i) scanf( %d , &a[i]); n   build(rt, 1, n); n   printf( %lld n , dp[1][m]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m, w; n int v[100010]; n long long mark[100010]; n bool test(long long x) { n   memset(mark, 0, sizeof mark); n   long long qnt = 0; n   long long sum = 0; n   for (int i = 0; i < n; ++i) { n     if (qnt > m) return false; n     sum -= mark[i]; n     long long val = v[i] + sum; n     if (val < x) { n       qnt += x - val; n       mark[min(i + w, n)] += x - val; n       sum += x - val; n     } n   } n   return qnt <= m; n } n long long bs(long long lo, long long hi) { n   while (hi - lo > 1) { n     long long mid = (hi + lo) / 2; n     if (test(mid)) { n       lo = mid; n     } else n       hi = mid; n   } n   if (test(hi)) return hi; n   return lo; n } n int main(void) { n   cin >> n >> m >> w; n   for (int i = 0; i < n; ++i) { n     cin >> v[i]; n   } n   cout << bs(0, 1000000000000000LL) <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long a[105][5]; n int n, Ans, b[105]; n bool check(int i) { n   for (int j = 1; j <= n; j++) n     for (int k = j + 1; k <= n; k++) n       if (i != j && i != k) { n         long long tmp = 0; n         for (int l = 1; l <= 5; l++) n           tmp += (a[j][l] - a[i][l]) * (a[k][l] - a[i][l]); n         if (tmp > 0) return 0; n       } n   return 1; n } n int main() { n   scanf( %d , &n); n   if (n > 66) { n     puts( 0 ); n   } else { n     for (int i = 1; i <= n; i++) n       for (int j = 1; j <= 5; j++) scanf( %I64d , &a[i][j]); n     for (int i = 1; i <= n; i++) { n       Ans += b[i] = check(i); n     } n     printf( %d n , Ans); n     for (int i = 1; i <= n; i++) n       if (b[i]) { n         printf( %d n , i); n       } n   } n } n


#include <bits/stdc++.h> n using namespace std; n void printReverseStack(stack<int> &ans) { n   int x = ans.top(); n   ans.pop(); n   if (!ans.empty()) printReverseStack(ans); n   cout << x <<    ; n } n int main() { n   int n, d, l, x; n   stack<int> ans; n   cin >> n >> d >> l; n   ans.push(d); n   while (ans.size() < n) { n     x = ans.top(); n     ans.pop(); n     if (x <= 0) { n       ans.push(1); n       ans.push(1 - x); n     } else { n       ans.push(l); n       ans.push(l - x); n     } n   } n   if (ans.top() <= 0 || ans.top() > l) n     cout << -1; n   else n     printReverseStack(ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <class T> n inline void read(T &x) { n   x = 0; n   char c = getchar(); n   while (!isdigit(c)) c = getchar(); n   while (isdigit(c)) x = x * 10 + (c & 15), c = getchar(); n } n const int P = 998244353; n int n, m; n char c[500001]; n struct matrix { n   int n, m, a[3][3]; n   inline matrix(int n = 0, int m = 0) : n(n), m(m) { memset(a, 0, sizeof(a)); } n } s[500001 << 2]; n inline matrix operator*(const matrix &a, const matrix &b) { n   matrix c(a.n, b.m); n   for (int i = 1; i <= c.n; i++) n     for (int k = 1; k <= a.m; k++) n       for (int j = 1; j <= c.m; j++) n         c.a[i][j] = (c.a[i][j] + (long long)a.a[i][k] * b.a[k][j]) % P; n   return c; n } n inline void build(int p, int l, int r) { n   if (l == r) { n     s[p] = matrix(2, 2), s[p].a[1][2] = (c[l - 1] == 1 ? 9 - c[l] : 0), n     s[p].a[2][1] = 1, s[p].a[2][2] = c[l] + 1; n     return; n   } n   int mid = (l + r) >> 1; n   build(p << 1, l, mid), build(p << 1 | 1, mid + 1, r); n   s[p] = s[p << 1] * s[p << 1 | 1]; n } n inline void add(int p, int l, int r, int x) { n   if (x > r || x < l) return; n   if (l == r) { n     s[p].a[1][2] = (c[l - 1] == 1 ? 9 - c[l] : 0), s[p].a[2][1] = 1, n     s[p].a[2][2] = c[l] + 1; n     return; n   } n   int mid = (l + r) >> 1; n   if (mid >= x) n     add(p << 1, l, mid, x); n   else n     add(p << 1 | 1, mid + 1, r, x); n   s[p] = s[p << 1] * s[p << 1 | 1]; n } n signed main() { n   read(n), read(m), scanf( %s , c + 1); n   for (int i = 1; i <= n; i++) c[i] -=  0 ; n   build(1, 1, n); n   while (m--) { n     int x, y; n     read(x), read(y), c[x] = y, add(1, 1, n, x), add(1, 1, n, x + 1); n     matrix ans(1, 2); n     ans.a[1][1] = ans.a[1][2] = 1; n     ans = ans * s[1]; n     printf( %d n , ans.a[1][2]); n   } n   return 0; n } n


#include <bits/stdc++.h> n const int32_t MAX_N = 1000; n bool isInA[MAX_N + 5], isInB[MAX_N + 5], isVisited[MAX_N + 5]; n std::vector<int32_t> v[MAX_N + 5]; n int32_t Dfs(int32_t nd) { n   isVisited[nd] = true; n   if (isInA[nd]) { n     return nd; n   } n   int32_t ans = INT32_MAX; n   for (auto &x : v[nd]) { n     if (!isVisited[x]) { n       ans = std::min(ans, Dfs(x)); n     } n   } n   return ans; n } n int main() { n   std::ios_base::sync_with_stdio(false); n   std::cin.tie(nullptr); n   int32_t cntTests; n   std::cin >> cntTests; n   for (int32_t cs = 1; cs <= cntTests; cs++) { n     int32_t n; n     std::cin >> n; n     for (int32_t i = 1; i <= n; i++) { n       v[i].clear(); n     } n     for (int32_t i = 0; i < n - 1; i++) { n       int32_t a, b; n       std::cin >> a >> b; n       v[a].push_back(b); n       v[b].push_back(a); n     } n     memset(isInA, 0, sizeof(isInA)); n     memset(isInB, 0, sizeof(isInB)); n     int32_t k1; n     std::cin >> k1; n     std::vector<int32_t> v1(k1); n     for (int32_t i = 0; i < k1; i++) { n       std::cin >> v1[i]; n       isInA[v1[i]] = true; n     } n     int32_t k2; n     std::cin >> k2; n     std::vector<int32_t> v2(k2); n     for (int32_t i = 0; i < k2; i++) { n       std::cin >> v2[i]; n       isInB[v2[i]] = true; n     } n     std::cout <<  B   << v2[0] <<   n  << std::flush; n     int32_t stNode; n     std::cin >> stNode; n     memset(isVisited, 0, sizeof(isVisited)); n     int32_t target = Dfs(stNode); n     std::cout <<  A   << target <<   n  << std::flush; n     int32_t res; n     std::cin >> res; n     if (isInB[res]) { n       std::cout <<  C   << target <<   n  << std::flush; n     } else { n       std::cout <<  C   << -1 <<   n  << std::flush; n     } n   } n } n


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


#include<bits/stdc++.h> n #define ll long long int n #define mk make_pair n #define pb push_back n #define INF (ll)1e18 n #define pii pair<ll,ll> n #define mod 1000000007 //998244353 n #define f(i,a,b) for(ll i=a;i<b;i++) n #define fb(i,a,b) for(ll i=a;i>b;i--) n #define ff first n #define ss second n #define srt(v)  if(!v.empty())sort(v.begin(),v.end()) n #define rev(v)  if(!v.empty())reverse(v.begin(),v.end()) n #define PI 3.141592653589793238 n #define pqr priority_queue<ll,vector<ll>,greater<ll>()> n using namespace std; n ll pow_mod(ll a,ll b) n { n  ll res=1; n  while(b!=0) n  { n   if(b&1) n   { n    res=(res*a)%mod; n   } n   a=(a*a)%mod; n   b/=2; n  } n  return res; n } n  n ll calc(vector<ll> &a){ n     ll sz=a.size(); n     ll ans=0; n      n     for(ll i=1;i<sz;i++){ n         ll j=i; n          n         while(j<sz&&a[j]==a[i])++j; n          n         i=--j; n         ++ans; n     } n      n     return ans; n } n void solve() n { n   ll n; n   cin>>n; n    n   ll a[n]; n    n   for(ll i=0;i<n;i++){ n       cin>>a[i]; n   } n    n   vector<ll>v1,v2; n    n   vector<ll>v[n+1]; n    n   for(ll i=1;i<=n;i++){ n       v[i].pb(INF); n   }  n    n   for(ll i=n-1;i>=0;i--){ n       v[a[i]].pb(i); n   } n    n   n    n   v1.pb(-1); n   v2.pb(-1); n    n    n   for(ll i=0;i<n;i++){ n       v[a[i]].pop_back(); n        n       if(v1.back()==a[i]&&v2.back()==a[i]){ n            v1.pb(a[i]);         n       } n       else if(v1.back()==a[i]){ n           v1.pb(a[i]); n       } n       else if(v2.back()==a[i]){ n           v2.pb(a[i]); n       } n       else if(v1.back()==-1) n       { n           v1.pb(a[i]); n       } n       else if(v2.back()==-1){ n           v2.pb(a[i]); n       } n       else n       {    n            n           ll x=v[v1.back()].back(); n           ll y=v[v2.back()].back(); n            n           if(x<y){ n               v2.pb(a[i]); n           } n           else n           v1.pb(a[i]); n       } n   } n   n    n   ll ans=calc(v1)+calc(v2); n    n   cout<<ans<<endl; n } n int main() n {    ios_base::sync_with_stdio(false); n      cin.tie(NULL); n      cout.tie(NULL); n       n //Start from Here. n  n   ll t; n   t=1; n // cin>>t; n   while(t--) n   solve(); n    n //Good Bye! n     return 0; n }


#include <bits/stdc++.h> n using namespace std; n int k, n, a[101]; n int main() { n   ios::sync_with_stdio(0); n   cin >> n >> k; n   for (int i = (0), _b = (n); i < _b; i++) cin >> a[i]; n   sort(a, a + n); n   cout << a[n - k]; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<int> P; n int m, n; n void bs() { n   int lo = 1, hi = m, ans = 0; n   int cnt = 0; n   while (lo <= hi) { n     int mid = (lo + hi) / 2; n     cout << mid << endl; n     int t; n     cin >> t; n     if (P[cnt % n] == 0) { n       if (t == 0) { n         exit(0); n       } n       if (t == -1) { n         lo = mid + 1; n       } n       if (t == 1) { n         hi = mid - 1; n       } n     } n     if (P[cnt % n] == 1) { n       if (t == 0) { n         exit(0); n       } n       if (t == 1) { n         lo = mid + 1; n       } n       if (t == -1) { n         hi = mid - 1; n       } n     } n     cnt++; n   } n } n int main() { n   cin >> m >> n; n   P = vector<int>(n); n   for (int i = 0; i < n; i++) { n     cout << 1 << endl; n     int t; n     cin >> t; n     if (t == 0 || t == -2) exit(0); n     if (t == 1) n       P[i] = 1; n     else n       P[i] = 0; n   } n   bs(); n   return 0; n } n


#include <bits/stdc++.h> n #pragma GCC optimize(2) n #pragma GCC optimize(3) n #pragma GCC optimize( Ofast ) n using namespace std; n namespace ywy { n char v[300][300]; n long long dk1[300], dk2[300]; n int dp[600]; n typedef struct _p { n   long long h1, h2; n   _p() { n     h1 = 2333; n     h2 = 6663; n   } n   friend bool operator==(const _p &a, const _p &b) { n     return (a.h1 == b.h1 && a.h2 == b.h2); n   } n   friend bool operator!=(const _p &a, const _p &b) { n     return ((a.h1 != b.h1) || (a.h2 != b.h2)); n   } n   inline void insert(char x) { n     h1 += dk1[x -  a  + 1]; n     h1 %= 998244353; n     h2 += dk2[x -  a  + 1]; n     h2 %= 1000000007; n   } n } pair; n pair str[600], real[600]; n int s[300]; n inline int solve(int n) { n   int ptr = 0, mid = 1, ans = 0; n   for (register int i = 2; i <= n; i++) { n     if (mid + ptr < i) n       mid = i, ptr = 0, dp[i] = 0; n     else n       dp[i] = min(mid + ptr - i, dp[mid * 2 - i]); n     while (dp[i] < min(n - i, i - 1) && n            str[i + dp[i] + 1] == str[i - dp[i] - 1]) n       dp[i]++; n     if (i + dp[i] >= mid + ptr) mid = i, ptr = dp[i]; n     if ((i & 1) == 0 && (s[i >> 1] != (s[i >> 1] & -s[i >> 1]))) continue; n     ans += (dp[i] + 1) / 2; n   } n   return (ans); n } n void ywymain() { n   int n, m; n   cin >> n >> m; n   for (register int i = 1; i <= n; i++) { n     for (register int j = 1; j <= m; j++) cin >> v[i][j]; n   } n   dk1[0] = dk2[0] = 1; n   for (register int i = 1; i <= 26; i++) { n     dk1[i] = (dk1[i - 1] * 233) % 998244353; n     dk2[i] = (dk2[i - 1] * 233) % 1000000007; n   } n   int ans = 0; n   for (register int l = 1; l <= m; l++) { n     memset(s, 0, sizeof(s)); n     for (register int i = 1; i <= n * 2 + 1; i++) str[i] = real[i] = _p(); n     for (register int r = l; r <= m; r++) { n       for (register int i = 1; i <= n; i++) { n         s[i] ^= (1 << (v[i][r] - 1)); n         real[i].insert(v[i][r]); n         if (s[i] != (s[i] & -s[i])) { n           str[i * 2].h1 = i + 19260817; n           str[i * 2].h2 = 19260817 - i; n         } else n           str[i * 2] = real[i]; n       } n       ans += solve(n * 2 + 1); n     } n   } n   cout << ans << endl; n } n }  // namespace ywy n int main() { n   ywy::ywymain(); n   return (0); n } n


#include <bits/stdc++.h> n using namespace std; n const double PI = acos(0) * 2; n const double EPS = 1e-8; n const long long MOD = 1e9 + 7; n const int MAXN = 1e5 + 5; n const int oo = 1e9; n const double foo = 1e30; n template <class T> n int getbit(T s, int i) { n   return (s >> i) & 1; n } n template <class T> n T onbit(T s, int i) { n   return s | (T(1) << i); n } n template <class T> n T offbit(T s, int i) { n   return s & (~(T(1) << i)); n } n template <class T> n int cntbit(T s) { n   return __builtin_popcounll(s); n } n template <class T> n T sqr(T x) { n   return x * x; n } n inline void addmod(int& a, int val, int p = MOD) { n   if ((a = (a + val)) >= p) a -= p; n } n inline void submod(int& a, int val, int p = MOD) { n   if ((a = (a - val)) < 0) a += p; n } n inline int mult(int a, int b, int p = MOD) { return (long long)a * b % p; } n int d, n, m, vs[MAXN], cnt[4], has[MAXN][4]; n vector<pair<int, int> > dX, dY; n int main() { n   cin >> d; n   cin >> n >> m; n   for (int i = 0; i < d; i++) { n     int x, y; n     cin >> x >> y; n     dX.push_back(make_pair(x, i)); n     dY.push_back(make_pair(y, i)); n     cin >> x >> y; n     dX.push_back(make_pair(x, i)); n     dY.push_back(make_pair(y, i)); n   } n   for (int i = 0; i < 4; i++) cin >> cnt[i]; n   sort(dX.begin(), dX.end()); n   sort(dY.begin(), dY.end()); n   int pass = 0; n   memset(vs, 0, sizeof(vs)); n   int tot = 0; n   for (int i = 0; i < ((int)(dX).size()); i++) { n     if (i && dX[i].first != dX[i - 1].first) { n       tot += pass; n       pass = 0; n     } n     if (!vs[dX[i].second]) { n       pass++; n       vs[dX[i].second] = 1; n       has[dX[i].second][0] = max(has[dX[i].second][0], tot); n     } else { n       has[dX[i].second][0] = max(has[dX[i].second][0], tot - 1); n     } n   } n   pass = 0; n   memset(vs, 0, sizeof(vs)); n   tot = 0; n   for (int i = ((int)(dX).size()) - 1; i >= 0; i--) { n     if (i != ((int)(dX).size()) - 1 && dX[i].first != dX[i + 1].first) { n       tot += pass; n       pass = 0; n     } n     if (!vs[dX[i].second]) { n       pass++; n       vs[dX[i].second] = 1; n       has[dX[i].second][1] = max(has[dX[i].second][1], tot); n     } else { n       has[dX[i].second][1] = max(has[dX[i].second][1], tot - 1); n     } n   } n   pass = 0; n   memset(vs, 0, sizeof(vs)); n   tot = 0; n   for (int i = 0; i < ((int)(dY).size()); i++) { n     if (i && dY[i].first != dY[i - 1].first) { n       tot += pass; n       pass = 0; n     } n     if (!vs[dY[i].second]) { n       pass++; n       vs[dY[i].second] = 1; n       has[dY[i].second][2] = max(has[dY[i].second][2], tot); n     } else { n       has[dY[i].second][2] = max(has[dY[i].second][2], tot - 1); n     } n   } n   pass = 0; n   memset(vs, 0, sizeof(vs)); n   tot = 0; n   for (int i = ((int)(dY).size()) - 1; i >= 0; i--) { n     if (i != ((int)(dY).size()) - 1 && dY[i].first != dY[i + 1].first) { n       tot += pass; n       pass = 0; n     } n     if (!vs[dY[i].second]) { n       pass++; n       vs[dY[i].second] = 1; n       has[dY[i].second][3] = max(has[dY[i].second][3], tot); n     } else { n       has[dY[i].second][3] = max(has[dY[i].second][3], tot - 1); n     } n   } n   for (int i = 0; i < d; i++) { n     int check = 1; n     for (int j = 0; j < 4; j++) n       if (has[i][j] != cnt[j]) { n         check = 0; n         break; n       } n     if (check) { n       cout << i + 1; n       return 0; n     } n   } n   cout << -1; n   return 0; n } n


#include <bits/stdc++.h> n inline long long MAX2(long long a, long long int b) { n   return (a) > (b) ? (a) : (b); n } n inline long long MAX3(long long a, long long b, long long c) { n   return (a) > (b) ? ((a) > (c) ? (a) : (c)) : ((b) > (c) ? (b) : (c)); n } n inline long long MIN2(long long a, long long b) { n   return (a) < (b) ? (a) : (b); n } n inline long long MIN3(long long a, long long b, long long c) { n   return (a) < (b) ? ((a) < (c) ? (a) : (c)) : ((b) < (c) ? (b) : (c)); n } n using namespace std; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   int q; n   cin >> q; n   while (q--) { n     int b, w; n     cin >> b >> w; n     vector<pair<int, int> > co; n     if (b > w) { n       if (b > 3 * w + 1) { n         cout <<  NO n ; n         continue; n       } n       int x = 1e5 + 1, y = 1e5 + 1; n       int i = 0; n       while (i < w - 1) { n         co.push_back(pair<int, int>(x, y)); n         y++; n         co.push_back(pair<int, int>(x, y)); n         y++; n         i++; n       } n       co.push_back(pair<int, int>(x, y)); n       y++; n       int si = co.size(); n       int req = b - w + 1; n       i = 0; n       while (i < si && req > 0) { n         co.push_back(pair<int, int>(co[i].first - 1, co[i].second)); n         req--; n         if (req == 0) break; n         co.push_back(pair<int, int>(co[i].first + 1, co[i].second)); n         req--; n         i += 2; n       } n       if (req > 0) { n         co.push_back(pair<int, int>(co[0].first, co[0].second - 1)); n         req--; n       } n       if (req > 0) { n         co.push_back( n             pair<int, int>(co[2 * (w - 1)].first, co[2 * (w - 1)].second + 1)); n         req--; n       } n     } else { n       if (w > 3 * b + 1) { n         cout <<  NO n ; n         continue; n       } n       int x = 1e5, y = 1e5 + 1; n       int i = 0; n       while (i < b - 1) { n         co.push_back(pair<int, int>(x, y)); n         y++; n         co.push_back(pair<int, int>(x, y)); n         y++; n         i++; n       } n       co.push_back(pair<int, int>(x, y)); n       y++; n       int si = co.size(); n       int req = w - b + 1; n       i = 0; n       while (i < si && req > 0) { n         co.push_back(pair<int, int>(co[i].first - 1, co[i].second)); n         req--; n         if (req == 0) break; n         co.push_back(pair<int, int>(co[i].first + 1, co[i].second)); n         req--; n         i += 2; n       } n       if (req > 0) { n         co.push_back(pair<int, int>(co[0].first, co[0].second - 1)); n         req--; n       } n       if (req > 0) { n         co.push_back( n             pair<int, int>(co[2 * (b - 1)].first, co[2 * (b - 1)].second + 1)); n         req--; n       } n     } n     cout <<  YES n ; n     int i, si = co.size(); n     for (i = 0; i < si; i++) cout << co[i].first <<     << co[i].second <<   n ; n   } n   cerr <<   n  n        <<  Time elapsed :   << clock() * 1000.0 / CLOCKS_PER_SEC <<   ms  n        <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n const int mod = 1000000007; n typedef struct Cell { n   int i; n   int j; n   int t; n } cell; n int n, k; n bool cmp(pair<int, int> a, pair<int, int> b) { n   if (a.first < b.first) n     return true; n   else if (a.first == b.first && a.second < b.second) n     return true; n   return false; n } n bool check(int i, int j, pair<int, pair<int, int>> times[], int t) { n   bool cond = false; n   for (int tr = 0; tr < k; tr++) { n     if (i == times[tr].first && j >= times[tr].second.first - 2 * t && n         j <= times[tr].second.second - 2 * t) { n       cond = true; n       break; n     } n   } n   return cond; n } n int vis[3][100][101]; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   ; n   int cases; n   cin >> cases; n   for (int T = 0; T < cases; T++) { n     cin >> n >> k; n     string arr[3]; n     for (int i = 0; i < 3; i++) { n       cin >> arr[i]; n     } n     int hi = -1, hj = -1; n     for (int i = 0; i < 3; i++) { n       for (int j = 0; j < n; j++) { n         if (arr[i][j] ==  s ) { n           hi = i; n           hj = j; n           break; n         } n       } n       if (hi != -1) { n         break; n       } n     } n     pair<int, pair<int, int>> times[k]; n     int ind = 0; n     for (int i = 0; i < 3; i++) { n       for (int j = 0; j < n; j++) { n         for (int t = 0; t < 101; t++) { n           vis[i][j][t] = 0; n         } n       } n     } n     for (int i = 0; i < 3; i++) n       for (int j = 0; j < n; j++) { n         if (arr[i][j] >=  A  && arr[i][j] <=  Z ) { n           int s = j; n           int k = j; n           while (k < n && arr[i][k] == arr[i][j]) k++; n           int e = k - 1; n           j = e; n           times[ind].second.first = s; n           times[ind].second.second = e; n           times[ind].first = i; n           ind++; n         } n       } n     queue<cell> q; n     cell node; n     node.i = hi; n     node.j = hj; n     node.t = 0; n     q.push(node); n     bool cond = false; n     while (q.size() > 0) { n       auto p = q.front(); n       q.pop(); n       int i = p.i; n       int j = p.j; n       int t = p.t; n       if (j == n - 1) { n         cond = true; n         break; n       } else { n         if (check(i, j, times, t)) continue; n       } n       if (j + 1 < n && !check(i, j + 1, times, t)) { n         cell node; n         node.i = i; n         node.j = j + 1; n         node.t = t + 1; n         if (vis[i][j + 1][t + 1] == 0) { n           q.push(node); n           vis[i][j + 1][t + 1] = 1; n         } n         if ((i + 1 < 3) && (!check(i + 1, j + 1, times, t))) { n           cell node; n           node.i = i + 1; n           node.j = j + 1; n           node.t = t + 1; n           if ((vis[i + 1][j + 1][t + 1] == 0)) { n             vis[i + 1][j + 1][t + 1] = 1; n             q.push(node); n           } n         } n         if (i - 1 >= 0 && !check(i - 1, j + 1, times, t)) { n           cell node; n           node.i = i - 1; n           node.j = j + 1; n           node.t = t + 1; n           if (vis[i - 1][j + 1][t + 1] == 0) { n             vis[i - 1][j + 1][t + 1] = 1; n             q.push(node); n           } n         } n       } n     } n     if (cond) n       cout <<  YES  << endl; n     else n       cout <<  NO  << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 200005; n inline int read() { n   register int x = 0, w = 1, ch = getchar(); n   for (; ch <  0  || ch >  9 ; ch = getchar()) n     if (ch ==  - ) w = -1; n   for (; ch >=  0  && ch <=  9 ; ch = getchar()) x = x * 10 + ch - 48; n   return w * x; n } n int n, m, k, tot1, tot2, tot3, cnt; n long long s[maxn]; n struct node { n   int opt, id, x; n   long long v, w; n } m1[maxn], m2[maxn], m3[maxn], ans[maxn]; n inline bool cmp1(node a, node b) { n   return (a.x != b.x) ? (a.x < b.x) : (a.v < b.v); n } n inline bool cmp2(node a, node b) { n   return (a.x != b.x) ? (a.x < b.x) : (a.v > b.v); n } n inline bool cmp3(node a, node b) { return a.opt < b.opt; } n inline bool cmp4(node a, node b) { return a.v * b.w > a.w * b.v; } n int main() { n   n = read(), m = read(), k = read(); n   for (int i = 1; i <= n; i++) s[i] = read(); n   for (int i = 1; i <= m; i++) { n     int a = read(), b = read(), c = read(); n     if (a == 1 && c > s[b]) n       m1[++tot1] = (node){1, i, b, c, 1}; n     else if (a == 2) n       m2[++tot2] = (node){2, i, b, c, 1}; n     else if (a == 3) n       m3[++tot3] = (node){3, i, b, c, 1}; n   } n   sort(m1 + 1, m1 + tot1 + 1, cmp1); n   int tmp = 0; n   for (int i = 1; i <= tot1; i++) n     if (m1[i].x != m1[i + 1].x) m1[++tmp] = m1[i]; n   tot1 = tmp; n   for (int i = 1; i <= tot1; i++) n     m1[i].v = m1[i].v - s[m1[i].x], m2[++tot2] = m1[i]; n   sort(m2 + 1, m2 + tot2 + 1, cmp2); n   long long sum = 0; n   for (int i = 1; i <= tot2; i++) { n     if (m2[i].x != m2[i - 1].x) sum = s[m2[i].x]; n     m2[i].w = sum, sum += m2[i].v; n   } n   for (int i = 1; i <= tot3; i++) m3[i].v--; n   for (int i = 1; i <= tot2; i++) m3[++tot3] = m2[i]; n   sort(m3 + 1, m3 + tot3 + 1, cmp4); n   tmp = min(tot3, k); n   for (int i = 1; i <= tmp; i++) ans[++cnt] = m3[i]; n   printf( %d n , cnt); n   sort(ans + 1, ans + cnt + 1, cmp3); n   for (int i = 1; i <= cnt; i++) printf( %d  , ans[i].id); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, k; n struct kni { n   int p, c, id; n   long long ans; n } a[100005]; n long long s[100005]; n inline char nc() { n   static char buf[10001], *p1 = buf, *p2 = buf; n   return p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 10000, stdin), p1 == p2) n              ? EOF n              : *p1++; n } n inline int read() { n   register int s = 0, w = 1; n   register char c = nc(); n   while (c <  0  || c >  9 ) { n     if (c ==  - ) w = -1; n     c = nc(); n   } n   while (c >=  0  && c <=  9 ) s = (s << 1) + (s << 3) + c - 48, c = nc(); n   return s * w; n } n inline void print(long long x) { n   if (x < 0) putchar( - ), x = -x; n   if (x > 9) print(x / 10); n   putchar(x % 10 + 48); n } n inline bool cmp(const kni &a, const kni &b) { return a.p < b.p; } n inline bool cmp2(const kni &a, const kni &b) { return a.id < b.id; } n int main() { n   n = read(); n   k = read(); n   for (int i = 1; i <= n; ++i) a[i].p = read(), a[i].id = i; n   for (int i = 1; i <= n; ++i) a[i].c = read(); n   sort(a + 1, a + n + 1, cmp); n   priority_queue<long long, vector<long long>, greater<long long> > q; n   for (int i = 1; i <= n; ++i) { n     if (i <= k) n       q.push(a[i].c), s[i] = s[i - 1] + a[i].c; n     else if (k) { n       if (q.top() < a[i].c) { n         s[i] = s[i - 1] + a[i].c - q.top(); n         q.pop(); n         q.push(a[i].c); n       } else n         s[i] = s[i - 1]; n     } n     a[i].ans = a[i].c + s[i - 1]; n   } n   sort(a + 1, a + n + 1, cmp2); n   for (int i = 1; i <= n; ++i) print(a[i].ans), putchar(   ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n namespace TYC { n const int N = 17, MX = (1 << 13) + 100, Q = 110; n int n, m, q, edge[N][N], a[Q], b[Q], lca[Q], bin[N], from[N], to[N]; n long long dp[N][MX]; n bool intree(int tree, int node) { return tree & bin[node]; } n long long dfs(int root, int tree) { n   long long &ans = dp[root][tree]; n   if (~ans) return ans; n   ans = 0; n   int sub = tree ^ bin[root]; n   int x = 1; n   for (; x <= n; x++) n     if (intree(sub, x)) break; n   for (int Subtree = sub; Subtree; Subtree = (Subtree - 1) & sub) { n     if (!intree(Subtree, x)) continue; n     bool flag = false; n     for (int i = 1; i <= m; i++) n       if (from[i] != root && to[i] != root && n           intree(Subtree, from[i]) != intree(Subtree, to[i])) { n         flag = true; n         break; n       } n     if (flag) continue; n     for (int i = 1; i <= q; i++) { n       if (lca[i] == root && intree(Subtree, a[i]) && intree(Subtree, b[i])) { n         flag = true; n         break; n       } n       if (intree(Subtree, lca[i]) && n           (!intree(Subtree, a[i]) || !intree(Subtree, b[i]))) { n         flag = true; n         break; n       } n     } n     if (flag) continue; n     int num = 0, son; n     for (int i = 1; i <= n; i++) n       if (intree(Subtree, i) && edge[root][i]) num++, son = i; n     if (num > 1) continue; n     if (!num) { n       for (int i = 1; i <= n; i++) n         if (intree(Subtree, i)) n           ans += dfs(i, Subtree) * dfs(root, tree ^ Subtree); n     } else n       ans += dfs(son, Subtree) * dfs(root, tree ^ Subtree); n   } n   return ans; n } n void work() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   cin >> n >> m >> q; n   int u, v; n   for (int i = 1; i <= m; i++) { n     cin >> u >> v; n     edge[u][v] = edge[v][u] = 1; n     from[i] = u, to[i] = v; n   } n   for (int i = 1; i <= q; i++) cin >> a[i] >> b[i] >> lca[i]; n   bin[1] = 1; n   for (int i = 2; i <= n; i++) bin[i] = bin[i - 1] << 1; n   memset(dp, -1, sizeof(dp)); n   for (int i = 1; i <= n; i++) dp[i][bin[i]] = 1; n   cout << dfs(1, (1 << n) - 1); n } n }  // namespace TYC n int main() { n   TYC::work(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n using ll = long long; n using vi = vector<int>; n using vvi = vector<vi>; n using vll = vector<ll>; n using vvll = vector<vll>; n using vb = vector<bool>; n using vd = vector<double>; n using vs = vector<string>; n using pii = pair<int, int>; n using pll = pair<ll, ll>; n using pdd = pair<double, double>; n using vpii = vector<pii>; n using vvpii = vector<vpii>; n using vpll = vector<pll>; n using vvpll = vector<vpll>; n using vpdd = vector<pdd>; n using vvpdd = vector<vpdd>; n template <typename T> n void ckmin(T& a, const T& b) { n   a = min(a, b); n } n template <typename T> n void ckmax(T& a, const T& b) { n   a = max(a, b); n } n mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count()); n namespace __input { n template <class T1, class T2> n void re(pair<T1, T2>& p); n template <class T> n void re(vector<T>& a); n template <class T, size_t SZ> n void re(array<T, SZ>& a); n template <class T> n void re(T& x) { n   cin >> x; n } n void re(double& x) { n   string t; n   re(t); n   x = stod(t); n } n template <class Arg, class... Args> n void re(Arg& first, Args&... rest) { n   re(first); n   re(rest...); n } n template <class T1, class T2> n void re(pair<T1, T2>& p) { n   re(p.first, p.second); n } n template <class T> n void re(vector<T>& a) { n   for (int i = 0; i < (int((a).size())); i++) re(a[i]); n } n template <class T, size_t SZ> n void re(array<T, SZ>& a) { n   for (int i = 0; i < (SZ); i++) re(a[i]); n } n }  // namespace __input n using namespace __input; n namespace __output { n template <class T1, class T2> n void pr(const pair<T1, T2>& x); n template <class T, size_t SZ> n void pr(const array<T, SZ>& x); n template <class T> n void pr(const vector<T>& x); n template <class T> n void pr(const deque<T>& x); n template <class T> n void pr(const set<T>& x); n template <class T1, class T2> n void pr(const map<T1, T2>& x); n template <class T> n void pr(const T& x) { n   cout << x; n } n template <class Arg, class... Args> n void pr(const Arg& first, const Args&... rest) { n   pr(first); n   pr(rest...); n } n template <class T1, class T2> n void pr(const pair<T1, T2>& x) { n   pr( { , x.first,  ,  , x.second,  } ); n } n template <class T, bool pretty = true> n void prContain(const T& x) { n   if (pretty) pr( { ); n   bool fst = 1; n   for (const auto& a : x) pr(!fst ? pretty ?  ,   :     :   , a), fst = 0; n   if (pretty) pr( } ); n } n template <class T> n void pc(const T& x) { n   prContain<T, false>(x); n   pr(  n ); n } n template <class T, size_t SZ> n void pr(const array<T, SZ>& x) { n   prContain(x); n } n template <class T> n void pr(const vector<T>& x) { n   prContain(x); n } n template <class T> n void pr(const deque<T>& x) { n   prContain(x); n } n template <class T> n void pr(const set<T>& x) { n   prContain(x); n } n template <class T1, class T2> n void pr(const map<T1, T2>& x) { n   prContain(x); n } n void ps() { pr(  n ); } n template <class Arg> n void ps(const Arg& first) { n   pr(first); n   ps(); n } n template <class Arg, class... Args> n void ps(const Arg& first, const Args&... rest) { n   pr(first,    ); n   ps(rest...); n } n }  // namespace __output n using namespace __output; n namespace __algorithm { n template <typename T> n void dedup(vector<T>& v) { n   sort((v).begin(), (v).end()); n   v.erase(unique((v).begin(), (v).end()), v.end()); n } n template <typename T> n typename vector<T>::iterator find(vector<T>& v, const T& x) { n   auto it = lower_bound((v).begin(), (v).end(), x); n   return it != v.end() && *it == x ? it : v.end(); n } n template <typename T> n size_t index(vector<T>& v, const T& x) { n   auto it = find(v, x); n   assert(it != v.end() && *it == x); n   return it - v.begin(); n } n template <typename C, typename T, typename OP> n vector<T> prefixes(const C& v, T id, OP op) { n   vector<T> r(int((v).size()) + 1, id); n   for (int i = 0; i < (int((v).size())); i++) r[i + 1] = op(r[i], v[i]); n   return r; n } n template <typename C, typename T, typename OP> n vector<T> suffixes(const C& v, T id, OP op) { n   vector<T> r(int((v).size()) + 1, id); n   for (int i = (int((v).size())) - 1; i >= 0; i--) r[i] = op(v[i], r[i + 1]); n   return r; n } n }  // namespace __algorithm n using namespace __algorithm; n struct monostate { n   friend istream& operator>>(istream& is, n                              const __attribute__((unused)) monostate& ms) { n     return is; n   } n   friend ostream& operator<<(ostream& os, n                              const __attribute__((unused)) monostate& ms) { n     return os; n   } n } ms; n template <typename W = monostate> n struct wedge { n   int u, v, i; n   W w; n   wedge<W>(int _u = -1, int _v = -1, int _i = -1) : u(_u), v(_v), i(_i) {} n   int operator[](int loc) const { return u ^ v ^ loc; } n   friend void re(wedge& e) { n     re(e.u, e.v, e.w); n     --e.u, --e.v; n   } n   friend void pr(const wedge& e) { pr(e.u,  <- , e.w,  -> , e.v); } n }; n namespace __io { n void setIn(string second) { freopen(second.c_str(),  r , stdin); } n void setOut(string second) { freopen(second.c_str(),  w , stdout); } n void setIO(string second =   ) { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.precision(15); n   if (int((second).size())) { n     setIn(second +  .in ), setOut(second +  .out ); n   } n } n }  // namespace __io n using namespace __io; n struct uf_monostate { n   uf_monostate(__attribute__((unused)) int id) {} n   void merge(__attribute__((unused)) uf_monostate& o, n              __attribute__((unused)) const monostate& e) {} n }; n template <typename T = uf_monostate, typename E = monostate> n struct union_find { n   struct node { n     int par, rnk, size; n     T state; n     node(int id = 0) : par(id), rnk(0), size(1), state(id) {} n     void merge(node& o, E& e) { n       if (rnk == o.rnk) rnk++; n       if (size < o.size) swap(state, o.state); n       size += o.size; n       state.merge(o.state, e); n     } n   }; n   int cc; n   vector<node> uf; n   union_find(int N = 0) : uf(N), cc(N) { n     for (int i = 0; i < N; i++) uf[i] = node(i); n   } n   int rep(int i) { n     if (i != uf[i].par) uf[i].par = rep(uf[i].par); n     return uf[i].par; n   } n   bool unio(int a, int b, E& e = ms) { n     a = rep(a), b = rep(b); n     if (a == b) return false; n     if (uf[a].rnk < uf[b].rnk) swap(a, b); n     uf[a].merge(uf[b], e); n     uf[b].par = a; n     cc--; n     return true; n   } n   T& state(int i) { return uf[rep(i)].state; } n }; n int main() { n   setIO(); n   int N, S; n   re(N, S); n   vi a(N); n   re(a); n   vi ti(N); n   vi st = a, did(N); n   sort((st).begin(), (st).end()); n   vvi occ(N); n   for (int i = 0; i < (N); i++) n     if (a[i] != st[i]) { n       int w = index(st, a[i]); n       while (a[w + did[w]] == st[w + did[w]]) did[w]++; n       ti[i] = w + did[w]++; n       occ[w].push_back(i); n     } else n       ti[i] = i; n   vb vis(N); n   union_find<> uf(N); n   for (int i = 0; i < (N); i++) n     if (!vis[i]) { n       vis[i] = true; n       for (int t = ti[i]; t != i; t = ti[t]) { n         uf.unio(i, t); n         vis[t] = true; n       } n     } n   for (int i = 0; i < (N); i++) n     for (int j = 0; j < (int((occ[i]).size()) - 1); j++) { n       if (uf.unio(occ[i][j], occ[i][j + 1])) { n         swap(ti[occ[i][j]], ti[occ[i][j + 1]]); n       } n     } n   int wr = 0; n   for (int i = 0; i < (N); i++) n     if (a[i] != st[i]) wr++; n   if (wr > S) { n     ps(-1); n     return 0; n   } n   if (a == st) { n     ps(0); n     return 0; n   } n   vvi cyc; n   for (int i = 0; i < (N); i++) n     if (i == uf.rep(i) && i != ti[i]) { n       cyc.push_back({i + 1}); n       for (int t = ti[i]; t != i; t = ti[t]) cyc.back().push_back(t + 1); n     } n   int merge = min(S - wr, int((cyc).size())); n   if (merge > 2) { n     vi loop, fix; n     for (int c = (int((cyc).size()) - merge); c < (int((cyc).size())); c++) { n       loop.insert(loop.end(), (cyc[c]).begin(), (cyc[c]).end()); n       fix.push_back(cyc[c].front()); n     } n     reverse((fix).begin(), (fix).end()); n     cyc.erase(cyc.end() - merge, cyc.end()); n     cyc.push_back(loop); n     cyc.push_back(fix); n   } n   ps(int((cyc).size())); n   for (auto& c : cyc) ps(int((c).size())), pc(c); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = (int)1e5 + 100; n long long a[N]; n int ansL[N], ansR[N]; n int st[N]; n long long diff[N]; n long long solve(int x, int y) { n   long long ans = 0; n   int r = 0; n   for (int i = x; i <= y - 1; i++) { n     while (r > 0 && diff[st[r - 1]] < diff[i]) r--; n     ansL[i] = (r == 0 ? x : st[r - 1] + 1); n     st[r++] = i; n   } n   r = 0; n   for (int i = y - 1; i >= x; i--) { n     while (r > 0 && diff[st[r - 1]] <= diff[i]) r--; n     ansR[i] = (r == 0 ? y : st[r - 1]); n     st[r++] = i; n   } n   for (int i = x; i < y; i++) { n     ans += 1LL * (i - ansL[i] + 1) * (ansR[i] - i) * diff[i]; n   } n   return ans; n } n int main() { n   int n, q; n   scanf( %d%d , &n, &q); n   for (int i = 0; i < n; i++) scanf( %lld , &a[i]); n   for (int i = 0; i < n - 1; i++) diff[i] = llabs(a[i + 1] - a[i]); n   for (int i = 0; i < q; i++) { n     int l, r; n     scanf( %d%d , &l, &r); n     printf( %lld n , solve(l - 1, r - 1)); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n double r1, x, A, B, p1, p2, ans, t, y[5002], z[5002], r2; n int n, m, k; n int main() { n   cin >> n; n   for (int i = 1; i <= n; i++) { n     cin >> x; n     r1 = (((r1) > (x)) ? (r1) : (x)); n   } n   cin >> m; n   for (int i = 1; i <= m; i++) cin >> y[i]; n   cin >> k; n   for (int i = 1; i <= k; i++) cin >> z[i]; n   cin >> A >> B; n   for (int i = 1; i <= m; i++) n     for (int j = 1; j <= k; j++) { n       p1 = y[i]; n       p2 = z[j]; n       t = (p2 * A) / (p1 * B); n       r2 = sqrt((r1 * r1) / (t + 1)); n       ans = (((ans) > (r2)) ? (ans) : (r2)); n     } n   printf( %.9f n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int64_t M = 1e2 + 5; n int64_t n, t[M], w[M], dp[M][20005], totalW; n int64_t solve(int64_t idx, int64_t wt) { n   if (idx > n) { n     if (wt >= 0) return 0; n     return 1e9; n   } n   if (dp[idx][1001 + wt] != -1) return dp[idx][1001 + wt]; n   int64_t ans = t[idx] + solve(idx + 1, wt + t[idx]); n   ans = min(ans, solve(idx + 1, wt - w[idx])); n   return dp[idx][1001 + wt] = ans; n } n int32_t main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   cin >> n; n   for (int64_t i = 1; i <= n; i++) cin >> t[i] >> w[i]; n   memset(dp, -1, sizeof(dp)); n   cout << solve(1, 0); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   cin >> n; n   pair<int, pair<int, int> > k[n]; n   for (int i = 0; i < n; i++) { n     cin >> k[i].second.first >> k[i].second.second; n     k[i].first = abs(k[i].second.first) + abs(k[i].second.second); n   } n   sort(k, k + n); n   int j = 0; n   j += 2 * n; n   for (int i = 0; i < n; i++) { n     if (k[i].second.first != 0) { n       j += 2; n     } n     if (k[i].second.second != 0) { n       j += 2; n     } n   } n   cout << j << endl; n   for (int i = 0; i < n; i++) { n     if (k[i].second.first > 0) { n       cout <<  1   << k[i].second.first <<   R  << endl; n     } else if (k[i].second.first < 0) { n       cout <<  1   << -k[i].second.first <<   L  << endl; n     } n     if (k[i].second.second > 0) { n       cout <<  1   << k[i].second.second <<   U  << endl; n     } else if (k[i].second.second < 0) { n       cout <<  1   << -k[i].second.second <<   D  << endl; n     } n     cout << 2 << endl; n     if (k[i].second.first > 0) { n       cout <<  1   << k[i].second.first <<   L  << endl; n     } else if (k[i].second.first < 0) { n       cout <<  1   << -k[i].second.first <<   R  << endl; n     } n     if (k[i].second.second > 0) { n       cout <<  1   << k[i].second.second <<   D  << endl; n     } else if (k[i].second.second < 0) { n       cout <<  1   << -k[i].second.second <<   U  << endl; n     } n     cout << 3 << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAX = 200000; n int n; n string s1, s2; n int main() { n   cin >> n >> s1 >> s2; n   int K = 7; n   long long H = 0, HH = 0, D = 1; n   n--; n   for (int(i) = (1); i < (n + 1); ++(i)) { n     int p = 0, pp = 0; n     if (s1[n - i] ==  N ) p = 1; n     if (s1[n - i] ==  S ) p = 4; n     if (s1[n - i] ==  E ) p = 2; n     if (s1[n - i] ==  W ) p = 3; n     H = H * K + p; n     if (s2[n - i] ==  N ) pp = 4; n     if (s2[n - i] ==  S ) pp = 1; n     if (s2[n - i] ==  E ) pp = 3; n     if (s2[n - i] ==  W ) pp = 2; n     HH += D * (pp); n     D *= 7; n     if (H == HH) { n       cout <<  NO ; n       return 0; n     } n   } n   cout <<  YES ; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long int n; n   cin >> n; n   long long int sum1 = (n * (n + 1)) / 2; n   if (sum1 % 2 == 0) { n     cout <<  0  << endl; n   } else { n     cout <<  1  << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n long long gcd(long long a, long long b) { n   if (b == 0) return a; n   return gcd(b, a % b); n } n long long a[505], ar[505], num[505], b[505]; n bool used[505]; n signed main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   long long t; n   cin >> t; n   while (t--) { n     long long n; n     cin >> n; n     vector<long long> ans; n     memset(num, 0, sizeof(num)); n     memset(used, 0, sizeof(used)); n     bool swi = false; n     long long swa1, swa2; n     for (long long i = 0; i < n; i++) cin >> ar[i]; n     for (long long i = 0; i < n; i++) { n       if (num[ar[i]]) { n         swi = true; n         swa1 = num[ar[i]] - 1, swa2 = i; n         break; n       } n       num[ar[i]] = i + 1; n     } n     for (long long i = 0; i < n; i++) { n       long long p, mi = 501; n       for (long long j = 0; j < n; j++) n         if (!used[j] && ar[j] < mi) mi = ar[j], p = j; n       a[p] = i + 1; n       b[p] = i + 1; n       used[p] = true; n     } n     long long N = n; n     while (n > 3) { n       long long p, ma = 0; n       for (long long i = 0; i < n; i++) n         if (a[i] > ma) ma = a[i], p = i; n       while (p < n - 3) { n         ans.push_back(p + 1); n         long long tem = a[p + 2]; n         a[p + 2] = a[p + 1]; n         a[p + 1] = a[p]; n         a[p] = tem; n         p++; n       } n       while (p < n - 1) { n         ans.push_back(n - 2); n         long long tem = a[n - 1]; n         a[n - 1] = a[n - 2]; n         a[n - 2] = a[n - 3]; n         a[n - 3] = tem; n         p++; n       } n       n--; n     } n     bool tag = false; n     for (long long i = 0; i < 3; i++) { n       if (a[0] <= a[1] && a[1] <= a[2]) { n         tag = true; n         break; n       } n       ans.push_back(1); n       long long tem = a[2]; n       a[2] = a[1]; n       a[1] = a[0]; n       a[0] = tem; n     } n     if (tag) { n       cout << ((long long)ans.size()) <<   n ; n       for (long long i : ans) cout << i <<    ; n       cout <<   n ; n       continue; n     } n     if (!swi) { n       cout <<  -1  <<   n ; n       continue; n     } n     ans.clear(); n     n = N; n     swap(b[swa1], b[swa2]); n     for (long long i = 0; i < n; i++) a[i] = b[i]; n     while (n > 3) { n       long long p, ma = 0; n       for (long long i = 0; i < n; i++) n         if (a[i] > ma) ma = a[i], p = i; n       while (p < n - 3) { n         ans.push_back(p + 1); n         long long tem = a[p + 2]; n         a[p + 2] = a[p + 1]; n         a[p + 1] = a[p]; n         a[p] = tem; n         p++; n       } n       while (p < n - 1) { n         ans.push_back(n - 2); n         long long tem = a[n - 1]; n         a[n - 1] = a[n - 2]; n         a[n - 2] = a[n - 3]; n         a[n - 3] = tem; n         p++; n       } n       n--; n     } n     for (long long i = 0; i < 3; i++) { n       if (a[0] <= a[1] && a[1] <= a[2]) { n         tag = true; n         break; n       } n       ans.push_back(1); n       long long tem = a[2]; n       a[2] = a[1]; n       a[1] = a[0]; n       a[0] = tem; n     } n     if (tag) { n       cout << ((long long)ans.size()) <<   n ; n       for (long long i : ans) cout << i <<    ; n       cout <<   n ; n     } else n       cout <<  -1  <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n #pragma GCC optimize( O3 ) n #pragma GCC optimize( Ofast ) n using namespace std; n long long n, c[2005], dp[2005], ans[2005], pos = 0; n vector<long long> v[2005]; n void dfs1(long long nod) { n   for (auto it : v[nod]) { n     dfs1(it); n     dp[nod] += dp[it] + 1; n   } n } n void dfs2(long long node) { n   for (auto it : v[node]) { n     dfs2(it); n   } n   if (c[node] == dp[node]) { n     ans[node] = ++pos; n   } else { n     ans[node] = pos - (dp[node] - c[node]) + 1; n     pos++; n     for (long long i = 1; i <= n; i++) { n       if (ans[i] >= ans[node] && i != node) { n         ans[i]++; n       } n     } n   } n } n int32_t main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   ; n   cin >> n; n   long long root = -1; n   for (long long i = 1; i <= n; i++) { n     long long x; n     cin >> x >> c[i]; n     if (x) { n       v[x].push_back(i); n     } else n       root = i; n   } n   dfs1(root); n   for (long long i = 1; i <= n; i++) { n     if (dp[i] < c[i]) { n       cout <<  NO  <<   n ; n       return 0; n     } n   } n   dfs2(root); n   cout <<  YES  <<   n ; n   for (long long i = 1; i <= n; i++) { n     cout << ans[i] <<    ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 100005; n vector<int> level[MAXN]; n vector<pair<int, int>> adj[MAXN]; n vector<pair<int, pair<int, int>>> edges; n int dp[MAXN], d[MAXN], prv[MAXN]; n bool OnPath[MAXN]; n int main() { n   int n, m, a, b, c, cnt = 0; n   scanf( %d%d , &n, &m); n   for (int i = 0; i < m; i++) { n     scanf( %d%d%d , &a, &b, &c); n     adj[a].push_back(make_pair(b, c)); n     adj[b].push_back(make_pair(a, c)); n     edges.push_back({a, {b, c}}); n     cnt += c; n   } n   memset(d, -1, sizeof(d)); n   memset(dp, -1, sizeof(dp)); n   memset(prv, -1, sizeof(prv)); n   queue<int> q; n   d[1] = dp[1] = 0; n   q.push(1); n   while (!q.empty()) { n     int cur = q.front(); n     q.pop(); n     for (auto p : adj[cur]) { n       if (d[p.first] == -1) { n         d[p.first] = d[cur] + 1; n         q.push(p.first); n       } n     } n   } n   for (int i = 1; i <= n; i++) level[d[i]].push_back(i); n   for (int i = 1; i <= d[n]; i++) { n     for (int node : level[i]) { n       for (auto p : adj[node]) { n         int child = p.first; n         if (d[child] == i - 1 && dp[child] + p.second > dp[node]) { n           dp[node] = p.second + dp[child]; n           prv[node] = child; n         } n       } n     } n   } n   int cur = n; n   while (cur != -1) { n     OnPath[cur] = 1; n     cur = prv[cur]; n   } n   int k = d[n] - dp[n] + cnt - dp[n]; n   printf( %d n , k); n   for (int i = 0; i < m; i++) { n     a = edges[i].first, b = edges[i].second.first, c = edges[i].second.second; n     if (OnPath[a] && OnPath[b] && !c) printf( %d %d 1 n , a, b); n     if ((!OnPath[a] || !OnPath[b]) && c) printf( %d %d 0 n , a, b); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long n, k, t, t1 = 0; n   cin >> n >> k; n   cin >> t1; n   for (int i = 2; i <= n; i++) { n     cin >> t; n     t1 += t + 1; n   } n   if (t1 == k) n     cout <<  YES ; n   else n     cout <<  NO ; n } n


#include <bits/stdc++.h> n using namespace std; n const int inf = 2147483647; n template <typename T> n inline T read() { n   T sum = 0, fl = 1; n   char ch = getchar(); n   for (; !isdigit(ch); ch = getchar()) n     if (ch ==  - ) fl = -1; n   for (; isdigit(ch); ch = getchar()) sum = (sum << 3) + (sum << 1) + ch -  0 ; n   return sum * fl; n } n const int maxn = 65; n int ans = 0; n long long v[200005]; n vector<long long> vec[maxn]; n map<long long, int> M; n void Solve() { n   int n = read<int>(); n   for (int i = 0; i <= 62; ++i) M[1ll << i] = i; n   for (int i = 1; i <= n; ++i) { n     v[i] = read<long long>(); n     vec[M[v[i] & -v[i]]].push_back(v[i]); n   } n   for (int i = 0; i <= 62; ++i) n     if (vec[ans].size() < vec[i].size()) ans = i; n   printf( %d n , n - vec[ans].size()); n   for (int i = 1; i <= n; ++i) n     if (M[v[i] & -v[i]] != ans) printf( %I64d n , v[i]); n } n int main() { n   Solve(); n   return 0; n } n