#include <bits/stdc++.h> n using namespace std; n using ll = long long; n const int inf = 1e9 + 7; n const ll longinf = 1LL << 60; n const ll mod = 1e9 + 7; n template <typename T, typename S> n struct LazySegmentTree { n  private: n   int n; n   vector<T> node; n   vector<S> lazy; n   T E0; n   S E1; n   inline void updatef(S& lazy, S& value) { lazy += value; } n   inline void calculatef(T& node, S& lazy, int len) { node += lazy; } n   inline T queryf(T& x, T& y) { return max(x, y); } n  n  public: n   LazySegmentTree(int sz, T nodeE, S lazyE) : E0(nodeE), E1(lazyE) { n     n = 1; n     while (n < sz) n <<= 1; n     node.resize(2 * n - 1, E0); n     lazy.resize(2 * n - 1, E1); n   } n   LazySegmentTree(vector<T>& v, T E0, S E1) : E0(E0), E1(E1) { n     n = 1; n     int sz = v.size(); n     while (n < sz) n <<= 1; n     node.resize(2 * n - 1, E0); n     lazy.resize(2 * n - 1, E1); n     for (int i = (int)(0); i < (int)(sz); ++i) node[i + n - 1] = v[i]; n     for (int i = n - 2; i >= 0; --i) { n       node[i] = queryf(node[2 * i + 1], node[2 * i + 2]); n     } n   } n   void eval(int k, int l, int r) { n     if (lazy[k] == E1) return; n     calculatef(node[k], lazy[k], r - l); n     if (r - l > 1) { n       updatef(lazy[2 * k + 1], lazy[k]); n       updatef(lazy[2 * k + 2], lazy[k]); n     } n     lazy[k] = E1; n   } n   void update(int a, int b, S x, int k = 0, int l = 0, int r = -1) { n     if (r < 0) r = n; n     eval(k, l, r); n     if (r <= a || b <= l) return; n     if (a <= l && r <= b) { n       updatef(lazy[k], x); n       eval(k, l, r); n     } else { n       update(a, b, x, 2 * k + 1, l, (l + r) / 2); n       update(a, b, x, 2 * k + 2, (l + r) / 2, r); n       node[k] = queryf(node[2 * k + 1], node[2 * k + 2]); n     } n   } n   T query(int a, int b, int k = 0, int l = 0, int r = -1) { n     if (r < 0) r = n; n     eval(k, l, r); n     if (r <= a || b <= l) return E0; n     if (a <= l && r <= b) return node[k]; n     T xl = query(a, b, 2 * k + 1, l, (l + r) / 2); n     T xr = query(a, b, 2 * k + 2, (l + r) / 2, r); n     return queryf(xl, xr); n   } n }; n int main() { n   cin.tie(0); n   ios::sync_with_stdio(false); n   int n, m, p; n   cin >> n >> m >> p; n   vector<pair<int, pair<int, ll>>> v(n + p); n   for (int i = (int)(0); i < (int)(n); ++i) { n     int x, y; n     cin >> x >> y; n     v[i] = {x, {-inf, -y}}; n   } n   vector<pair<int, int>> b(m); n   for (int i = (int)(0); i < (int)(m); ++i) { n     cin >> b[i].first >> b[i].second; n   } n   for (int i = (int)(0); i < (int)(p); ++i) { n     int x, y, z; n     cin >> x >> y >> z; n     v[n + i] = {x, {y, z}}; n   } n   sort(b.begin(), b.end()); n   vector<ll> c(m); n   for (int i = (int)(0); i < (int)(m); ++i) c[i] = -b[i].second; n   LazySegmentTree<ll, ll> sg(c, -longinf, 0); n   sort(v.begin(), v.end()); n   ll ans = -longinf; n   for (auto e : v) { n     if (e.second.second < 0) { n       ans = max(sg.query(0, m) + e.second.second, ans); n     } else { n       int idx = n           lower_bound(b.begin(), b.end(), pair<int, int>{e.second.first, inf}) - n           b.begin(); n       sg.update(idx, m, e.second.second); n     } n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void solve() { n   int n; n   cin >> n; n   vector<pair<int, int> > v(n); n   for (int i = 0; i < n; i++) cin >> v[i].first; n   bool is0 = false, is1 = false; n   for (int i = 0; i < n; i++) { n     cin >> v[i].second; n     if (v[i].second) n       is1 = true; n     else n       is0 = true; n   } n   if (is0 && is1) { n     cout <<  Yes n ; n     return; n   } n   for (int i = 1; i < n; i++) n     if (v[i - 1] > v[i]) { n       cout <<  No n ; n       return; n     } n   cout <<  Yes n ; n } n int main() { n   ios::sync_with_stdio(false); n   int t; n   cin >> t; n   while (t--) solve(); n } n


#include <bits/stdc++.h> n using namespace std; n int n, m; n int mat[1000010]; n int row(int r) { n   int s = r * m, e = s + m; n   bool eq = false, dif = false; n   for (int i = s, idx = 0; i < e; i++, idx++) { n     if (mat[i] == -1) continue; n     int a = mat[i] / 2; n     int b = idx & 1; n     if (a == b) eq = true; n     if (a != b) dif = true; n   } n   if (eq && dif) return 0; n   if (eq || dif) return 1; n   return 2; n } n int col(int c) { n   int s = c, e = n * m + c; n   bool eq = false, dif = false; n   for (int i = s, idx = 0; i < e; i += m, idx++) { n     if (mat[i] == -1) continue; n     int a = min(mat[i], 3 - mat[i]); n     int b = idx & 1; n     if (a == b) eq = true; n     if (a != b) dif = true; n   } n   if (eq && dif) return 0; n   if (eq || dif) return 1; n   return 2; n } n int main() { n   while (cin >> n >> m) { n     char ch; n     for (int i = 0; i < n * m; i++) { n       cin >> ch; n       if (ch ==  . ) n         mat[i] = -1; n       else n         mat[i] = (int)(ch -  1 ); n     } n     int res = 1; n     for (int i = 0; i < n; i++) { n       res *= row(i); n       if (res >= 1000003) res -= 1000003; n     } n     for (int i = 0; i < m; i++) { n       res *= col(i); n       if (res >= 1000003) res -= 1000003; n     } n     cout << res % 1000003 << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 100; n int n, x, t; n long long ans, s[N]; n int main() { n   scanf( %d , &n); n   for (int i = 1; i <= n; ++i) { n     scanf( %d , &x); n     t = 0; n     while (x) { n       t += x % 2; n       x /= 2; n     } n     ++s[t]; n   } n   for (int i = 1; i <= N; ++i) ans += s[i] * (s[i] - 1) / 2; n   cout << ans; n } n


#include <bits/stdc++.h> n using namespace std; n int n, i, j; n vector<string> koutsompolio; n int main() { n   cin >> n; n   string lol; n   bool r, f; n   for (i = 0; i <= n; i++) { n     getline(cin, lol); n     koutsompolio.push_back(lol); n   } n   for (i = 1; i <= n; i++) { n     r = false; n     f = false; n     if ((koutsompolio[i][0] ==  m ) && (koutsompolio[i][1] ==  i ) && n         ((koutsompolio[i][2] ==  a )) && (koutsompolio[i][3] ==  o ) && n         (koutsompolio[i][4] ==  . )) { n       r = true; n     } n     if ((koutsompolio[i][koutsompolio[i].size() - 1] ==  . ) && n         (koutsompolio[i][koutsompolio[i].size() - 2] ==  a ) && n         ((koutsompolio[i][koutsompolio[i].size() - 3] ==  l )) && n         (koutsompolio[i][koutsompolio[i].size() - 4] ==  a ) && n         (koutsompolio[i][koutsompolio[i].size() - 5] ==  l )) { n       f = true; n     } n     if ((f == false) && (r == true)) { n       cout <<  Rainbow s n ; n     } else if ((f == true) && (r == false)) { n       cout <<  Freda s n ; n     } else { n       cout <<  OMG>.< I don t know! n ; n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n void solve() { n   int x{}; n   cin >> x; n   if (x % 3 == 0 || x % 7 == 0) { n     cout <<  YES  << endl; n     return; n   } n   int ms{x / 3}; n   int ml{x / 7}; n   for (int i{0}; i <= ms; ++i) { n     for (int j{0}; j <= ml; ++j) { n       int chunks{3 * i + 7 * j}; n       if (chunks == x) { n         cout <<  YES  << endl; n         return; n       } n     } n   } n   cout <<  NO  << endl; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(nullptr); n   cout.tie(nullptr); n   int t{}; n   cin >> t; n   while (t > 0) { n     solve(); n     t--; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long k; n   cin >> k; n   long long cnt = 0; n   for (double a = 2 + 2. / 3; a < (2 * k) / sqrt(3); a += 2) { n     double b = n         1e-8 + (-a + sqrt((16 * k * k) / 3. - (3 * a * a))) / 2. - 2. / 3; n     if (b > 0) cnt += ((int)b + 2) / 2; n   } n   cout << 1 + 6 * cnt << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int cmpint(const void *a, const void *b) { n   if (((int *)a)[0] == ((int *)b)[0]) return ((int *)a)[1] - ((int *)b)[1]; n   return ((int *)a)[0] - ((int *)b)[0]; n } n int prim[500005] = {0}; n bool isprim[5000005] = {0}; n int prim_len; n void count_prim() { n   int i, j, l; n   prim[0] = 2; n   prim_len = 1; n   for (i = 3; i < 5000000; i += 2) n     if (isprim[i] == 0) { n       prim[prim_len] = i; n       prim_len++; n       l = i * 2; n       if (i < 10000) n         for (j = i * i; j < 5000000; j += l) isprim[j] = 1; n     } n } n int a[100005]; n int main() { n   int i, j, n, m, k, l, o, p; n   scanf( %d , &n); n   count_prim(); n   m = 0; n   for (i = (0); i < (prim_len); i++) { n     k = prim[i]; n     l = 0; n     while (k > 0) { n       l = l * 10 + k % 10; n       k /= 10; n     } n     if (l != prim[i] && isprim[l] == 0 && l % 2 == 1) { n       m++; n       a[m] = prim[i]; n     } n     if (m > n) break; n   } n   printf( %d n , a[n]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   vector<long> A; n   long N, L, R, M, T, Sum, Min = 2147483647; n   cin >> N; n   A.resize(N); n   for (long i = 0; i < N; ++i) cin >> A[i]; n   for (long M = 1; M <= 100; ++M) { n     Sum = 0; n     for (long i = 0; i < N; ++i) n       Sum += min(min(abs(A[i] - M + 1), abs(A[i] - M - 1)), abs(A[i] - M)); n     if (Sum < Min) { n       Min = Sum; n       T = M; n     } n   } n   cout << T <<     << Min; n   return 0; n } n


#include <bits/stdc++.h> n const int mod = 1e6 + 3; n int add(int a, int b) { return (a += b) >= mod ? a -= mod : a; } n int sub(int a, int b) { return add(a, mod - b); } n int mul(int a, int b) { return int(1LL * a * b % mod); } n int pow(int a, int64_t n) { n   int res = 1; n   while (n > 0) { n     if (n & 1) { n       res = mul(res, a); n     } n     a = mul(a, a); n     n >>= 1; n   } n   return res; n } n int inv(int a) { return pow(a, mod - 2); } n int main() { n   const int n = 11; n   std::vector<int> f(n + 1); n   for (int i = 1; i <= n; ++i) { n     std::cout <<  ?   << i << std::endl; n     std::cin >> f[i]; n     if (f[i] == 0) { n       std::cout <<  !   << i << std::endl; n       return 0; n     } n   } n   std::vector<std::vector<int> > a(1 + n, std::vector<int>(1 + n)); n   for (int i = 1; i <= n; ++i) { n     for (int j = 1; j <= n; ++j) { n       a[i][j] = pow(i, j - 1); n     } n   } n   for (int j = 1, i = 1; j <= n; ++j) { n     bool was = false; n     for (int k = i; k <= n; ++k) { n       if (a[k][j] != 0) { n         was = true; n         std::swap(a[i], a[k]); n         std::swap(f[i], f[k]); n         break; n       } n     } n     if (!was) { n       continue; n     } n     for (int k = i + 1; k <= n; ++k) { n       int coeff = mul(sub(0, inv(a[k][j])), a[i][j]); n       for (int t = j; t <= n; ++t) { n         a[k][t] = add(mul(coeff, a[k][t]), a[i][t]); n       } n       assert(a[k][j] == 0); n       f[k] = add(f[i], mul(coeff, f[k])); n     } n     i++; n   } n   std::vector<int> x(1 + n); n   for (int i = n; i >= 1; --i) { n     assert(a[i][i] != 0); n     int res = f[i]; n     for (int j = i + 1; j <= n; ++j) { n       res = sub(res, mul(a[i][j], x[j])); n     } n     x[i] = mul(res, inv(a[i][i])); n   } n   for (int i = 0; i < mod; ++i) { n     int p = 1, s = 0; n     for (int j = 1; j <= n; ++j) { n       s = add(s, mul(x[j], p)); n       p = mul(p, i); n     } n     if (s == 0) { n       std::cout <<  ?   << i << std::endl; n       int v; n       std::cin >> v; n       assert(v == 0); n       std::cout <<  !   << i << std::endl; n       return 0; n     } n   } n   std::cout <<  !   << -1 << std::endl; n   return 0; n } n


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


#include <bits/stdc++.h> n using namespace std; n const double PI = 2 * acos(0.0); n mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); n mt19937_64 rng_64(chrono::steady_clock::now().time_since_epoch().count()); n const string DIGITS =  0123456789 ; n const string ALPH =  abcdefghijklmnopqrstuvwxyz ; n template <class T0, class T1> n inline ostream &operator<<(ostream &out, pair<T0, T1> &a) { n   return out <<  {  << a.first <<  ,   << a.second <<  } ; n } n template <class T0, class T1, class T2> n inline ostream &operator<<(ostream &out, tuple<T0, T1, T2> &a) { n   return out <<  {  << get<0>(a) <<  ,   << get<1>(a) <<  ,   << get<2>(a) n              <<  } ; n } n template <class T0, class T1, class T2, class T3> n inline ostream &operator<<(ostream &out, tuple<T0, T1, T2, T3> &a) { n   return out <<  {  << get<0>(a) <<  ,   << get<1>(a) <<  ,   << get<2>(a) n              <<  ,   << get<3>(a) <<  } ; n } n template <class T> n inline ostream &operator<<(ostream &out, vector<T> &a) { n   out <<  [ ; n   for (long long i = 0; i < (long long)(a.size()); ++i) n     out << a[i] << vector<string>{ ,  ,  ]   }[i + 1 == a.size()]; n   return out; n } n void smain(); n signed main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   cout << setprecision(10) << fixed; n   smain(); n   return 0; n } n void smain() { n   long long t; n   cin >> t; n   while (t--) { n     long long n, k; n     string s; n     cin >> n >> k >> s; n     vector<long long> p(2); n     map<long long, long long> mp; n     for (long long i = 0; i < s.size(); i++) { n       p[s[i] -  0 ]++; n       long long u = p[0] - p[1]; n       mp[u]++; n     } n     long long v = p[0] - p[1]; n     if (v == 0) { n       if (mp[k] != 0) { n         cout << -1 <<   n ; n       } else { n         cout << mp[k] <<   n ; n       } n       continue; n     } n     long long sum = 0; n     for (auto h : mp) { n       if ((k - h.first) % abs(v) == 0 && (k - h.first) / v >= 0) { n         sum += h.second; n       } n     } n     if (k == 0) sum++; n     cout << sum <<   n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int n, ans, dp[105], a[105]; n int main() { n   scanf( %d , &n); n   for (int i = 1; i <= n; i++) scanf( %d , &a[i]); n   if (n % 2 == 0 || n == 1) { n     printf( -1 ); n     return 0; n   } n   for (int i = n; i >= 2; i--) { n     ans += max(a[i], a[i - 1]); n     a[(i - 1) / 2] -= min(a[(i - 1) / 2], max(a[i], a[i - 1])); n     i--; n   } n   printf( %d , ans + a[1]); n } n


#include <iostream> n #include <vector> n #include <algorithm> n #include <string> n #include <map> n #include <unordered_set> n #include <unordered_map> n #include <cmath> n #include <random> n #include <chrono> n #include <set> n #include <queue> n #include <stack> n #include <deque> n #include <cstring> n const long long big = 998244353; n using namespace std; n long long power(long long a, long long n) { n   if (n == 0) n     return 1; n   if (n % 2 == 1) n     return (power(a, n - 1) * a) % big; n   else { n     long long b = power(a, n / 2); n     return (b * b) % big; n   } n } n int gcd(int a, int b) { n   if (b == 0) n     return a; n   else n     return gcd(b, a % b); n } n int t2[1000000]; n void build_min(int a[], int v, int tl, int tr) { n   if (tl >= tr) n     t2[v] = a[tl]; n   else { n     int tm = (tl + tr) / 2; n     build_min(a, v * 2, tl, tm); n     build_min(a, v * 2 + 1, tm + 1, tr); n     t2[v] = min(t2[v * 2], t2[v * 2 + 1]); n   } n } n  n int sum_min (int v, int tl, int tr, int l, int r) { n   if (l > r) n     return 1000000; n   if (l == tl && r == tr) n     return t2[v]; n   int tm = (tl + tr) / 2; n   return min(sum_min (v*2, tl, tm, l, min(r,tm)) n       , sum_min (v*2+1, tm+1, tr, max(l,tm+1), r)); n } n  n  n int t[1000000]; n void build_max(int a[], int v, int tl, int tr) { n   if (tl >= tr) n     t[v] = a[tl]; n   else { n     int tm = (tl + tr) / 2; n     build_max(a, v * 2, tl, tm); n     build_max(a, v * 2 + 1, tm + 1, tr); n     t[v] = max(t[v * 2], t[v * 2 + 1]); n   } n } n  n void update(int v, int tl, int tr, int l, int r, int add) { n   if (l > r) n     return; n   if (l == tl && tr == r) n     t[v] += add; n   else { n     int tm = (tl + tr) / 2; n     update(v * 2, tl, tm, l, min(r, tm), add); n     update(v * 2 + 1, tm + 1, tr, max(l, tm + 1), r, add); n   } n } n  n int get(int v, int tl, int tr, int pos) { n   if (tl == tr) n     return t[v]; n   int tm = (tl + tr) / 2; n   if (pos <= tm) n     return t[v] + get(v * 2, tl, tm, pos); n   else n     return t[v] + get(v * 2 + 1, tm + 1, tr, pos); n } n  n int sum_max (int v, int tl, int tr, int l, int r) { n   if (l > r) n     return -1000000; n   if (l == tl && r == tr) n     return t[v]; n   int tm = (tl + tr) / 2; n   return max(sum_max (v*2, tl, tm, l, min(r,tm)) n       , sum_max (v*2+1, tm+1, tr, max(l,tm+1), r)); n } n  n class Cmp { n  public: n   bool operator()(vector<long long>& a, vector<long long>& b) { n     return a.size() > b.size(); n   } n }; n  n  n  n  n void solve() { n   long long n, k; n   cin >> n >> k; n   vector<long long> sa(n); n   for (int i = 0; i < n; ++i) { n     cin >> sa[i]; n   } n   vector<long long> tmp(n); n   for (int i = 0; i < n; ++i) { n     tmp[sa[i]] = i; n   } n   long long cur = 0; n   vector<long long> s(n); n   s[sa[0]] = 0; n   for (int i = 1; i < n; ++i) { n     long long j = sa[i - 1]; n     long long l = sa[i]; n     if (l == n - 1 || (j < n - 1 && tmp[j + 1] > tmp[l + 1])) n       cur++; n     s[sa[i]] = cur; n   } n   long long dif = k; n   for (long long i: s) { n //    cout << i <<    ; n     if (k - i - 1 < dif) { n       dif = k - i - 1; n     } n   } n   if (dif < 0) { n     cout << 0; n     return; n   } else if (dif == 0) { n     cout << 1; n     return; n   } else { n     long long ans = 1; n     for (int i = 1; i <= dif; ++i) { n       ans *= (n + i); n       ans %= big; n       ans *= power(i, big - 2); n       ans %= big; n     } n     cout << ans; n   } n  n } n  n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   long long t = 1; n //  cin >> t; n   //cout.precision(20); n   while (t--) { n     solve(); n   } n }


#include <bits/stdc++.h> n  n using namespace std; n  n int main() { n   ios::sync_with_stdio(0); n   cout.tie(0), cin.tie(0); n   int t; n   cin >> t; n   while (t--) { n     int n, m; n     cin >> n >> m; n     vector<string> ar(n); n     long long cnt = 0; n     vector<vector<int>> dp(n + 1, vector<int>(m + 1, 0)); n     for (int i = 0; i < n; i++) { n       cin >> ar[i]; n       for (int j = 0; j < m; j++) { n         dp[i][j] = (ar[i][j] ==  * ); n         cnt += (ar[i][j] ==  * ); n       } n     } n     for (int i = 0; i < n; i++) { n       for (int j = 1; j < m; j++) { n         dp[i][j] += dp[i][j - 1]; n       } n     } n     // 1st Sol. : from base to head but with binary search on answer - O(N^3 . Log(N)). n     // 2nd Sol. : let * in position [i, j] be the head of k-tree and try to go downward - O(N^3). n     for (int i = 0; i < n; i++) { n       for (int j = 0; j < m; j++) { n         if (ar[i][j] ==  * ) { n           int rows = i + 1; n           int cols_left = j - 1; n           int cols_right = j + 1; n           int temp = 0; n           bool ok = true; n           while (rows < n && ok) { n             int cur = dp[rows][cols_right]; n             if (cols_left - 1 >= 0) { n               cur -= dp[rows][cols_left - 1]; n             } n             if (cur == cols_right - cols_left + 1) { n               temp++; n               rows++; n               cols_left--; n               cols_right++; n             } else { n               ok = false; n             } n           } n           cnt += temp; n         } n       } n     } n     cout << cnt <<   n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n struct pt { n   long long x, y; n } a[3005]; n long long n, m, q, lim, las, pre[3005], suf[3005], rc[3005][3005], c, x, y, z, n     v, ans; n long long cmp(pt x, pt y) { return x.y < y.y || (x.y == y.y && x.x < y.x); } n long long cb(long long x) { return (x + 1) * x >> 1; } n int main() { n   scanf( %lld , &n), scanf( %lld , &m), scanf( %lld , &q), scanf( %lld , &lim); n   for (long long i = 1; i <= q; i++) n     scanf( %lld , &a[i].x), scanf( %lld , &a[i].y); n   sort(a + 1, a + q + 1, cmp); n   for (long long i = 1; i <= q; i++) rc[a[i].x][++rc[a[i].x][0]] = i; n   a[q + 1].y = m + 1; n   for (long long i = 1; i <= n; i++) { n     memset(pre, 255, sizeof pre), memset(suf, 255, sizeof suf), las = 0; n     for (long long j = 1; j <= q; j++) n       if (a[j].x >= i) suf[pre[j] = las] = j, las = j; n     suf[pre[q + 1] = las] = q + 1, v = cb(m), x = y = suf[0], c = 1; n     while (suf[x] > -1) { n       while (c + 1 < lim && suf[suf[y]] > -1) c++, y = suf[y]; n       if (c < lim) v -= (a[x].y - a[pre[x]].y) * (a[suf[y]].y - a[x].y); n       x = suf[x], c--; n     } n     x = suf[0]; n     while (x) v -= cb(a[x].y - a[pre[x]].y - 1), x = suf[x]; n     for (long long j = n; j >= i; j--) { n       ans += v; n       if (!v) break; n       if (j > i) n         for (long long u = 1; u <= rc[j][0]; u++) n           if (x = y = z = rc[j][u]) { n             c = 1; n             while (c < lim && pre[pre[x]] > -1) x = pre[x], c++; n             while (pre[x] != z) { n               while (c < lim && suf[suf[y]] > -1) y = suf[y], c++; n               if (c == lim) n                 v -= (a[x].y - a[pre[x]].y) * (a[suf[y]].y - a[y].y); n               if (c < lim) break; n               x = suf[x], c--; n             } n             x = pre[z], y = suf[z], suf[pre[y] = x] = y; n           } n     } n   } n   printf( %lld , ans); n } n


#include <bits/stdc++.h> n using namespace std; n const int MAX_N = 300 * 1000 + 10; n long long dist[MAX_N], dist1[MAX_N]; n vector<pair<int, int>> adj[MAX_N]; n set<pair<long long, int>> s; n bool inPath[MAX_N]; n pair<int, int> mx; n int nxt[MAX_N]; n bool dfs_path(int u, int par = -1) { n   for (auto x : adj[u]) { n     int v = x.first; n     if (v != par && dfs_path(v, u)) inPath[u] = true, nxt[u] = v; n   } n   return inPath[u]; n } n long long dfs_dist(int u, int par = -1) { n   if (inPath[u]) n     for (auto x : adj[u]) { n       int v = x.first, w = x.second; n       if (v == nxt[u]) dist1[v] = dist1[u] + w, dist[u] = dfs_dist(v, u) + w; n     } n   for (auto x : adj[u]) { n     int v = x.first, w = x.second; n     if (v != par && !inPath[v]) n       dist1[v] = dist1[u] + w, dist[v] = dist[u] + w, dfs_dist(v, u); n   } n   return dist[u]; n } n bool cmp(pair<int, int> a, pair<int, int> b) { n   if (min(a.first, b.first) < 0) return a.first < 0 ? true : false; n   return dist1[a.first] + dist[a.second] < dist1[b.first] + dist[b.second]; n } n void rem_block(int u, int par = -1) { n   s.erase({dist[u], u}); n   for (auto x : adj[u]) { n     int v = x.first; n     if (v != par) rem_block(v, u); n   } n } n void add_block(int u, int par = -1) { n   s.insert({dist[u], u}); n   for (auto x : adj[u]) { n     int v = x.first; n     if (v != par) add_block(v, u); n   } n } n void dfs(int u, int par = -1) { n   if (inPath[u]) { n     s.erase({dist[u], u}); n     for (auto x : adj[u]) { n       int v = x.first; n       if (v != par && v != nxt[u]) rem_block(v, u), dfs(v, u), add_block(v, u); n     } n     for (auto x : adj[u]) { n       int v = x.first; n       if (v != par && v != nxt[u]) rem_block(v, u); n     } n     for (auto it = s.rbegin(); it != s.rend(); it++) n       if (it->second != nxt[u]) { n         mx = max(make_pair(u, it->second), mx, cmp); n         break; n       } n     if (nxt[u] != u) dfs(nxt[u], u); n   } else if (!s.empty()) { n     mx = max(make_pair(u, (*s.rbegin()).second), mx, cmp); n     for (auto x : adj[u]) { n       int v = x.first; n       if (v != par) dfs(v, u); n     } n   } n } n int main() { n   ios_base ::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL); n   int n, m; n   cin >> n >> m; n   for (int i = 1; i < n; i++) { n     int u, v, w; n     cin >> u >> v >> w; n     u--, v--; n     adj[u].push_back({v, w}); n     adj[v].push_back({u, w}); n   } n   inPath[n - 1] = true, nxt[n - 1] = n - 1, mx = {-1, -1}; n   dfs_path(0), dfs_dist(0), add_block(0), dfs(0); n   for (int i = 0; i < m; i++) { n     int x; n     cin >> x; n     cout << min(dist1[mx.first] + dist[mx.second] + x, dist[0]) << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long i = -1, k, l; n   cin >> k >> l; n   while (l % k == 0) { n     l /= k; n     i++; n   } n   if (i >= 0 && l == 1) n     cout <<  YES  << endl << i << endl; n   else n     cout <<  NO  << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   cin >> n; n   while (n--) { n     int a, b; n     cin >> a >> b; n     int sum = 0; n     int h; n     for (int i = 1; i < a; i++) { n       sum += i; n       if (sum >= b) { n         sum -= i; n         h = i - 1; n         break; n       } n     } n     int s = a - h - 1; n     int sb = a - (b - sum) + 1; n     for (int i = 1; i <= a; i++) { n       if (i == s || i == sb) n         cout <<  b ; n       else n         cout <<  a ; n     } n     cout << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long int c1, c2, x, y, l, r, mid; n int main() { n   ios::sync_with_stdio(false); n   cin >> c1 >> c2 >> x >> y; n   l = c1 + c2; n   r = 2000000009; n   while (l < r) { n     mid = (l + r) / 2; n     if ((mid - (mid / x) >= c1) && (mid - (mid / y) >= c2) && n         (mid - (mid / (x * y)) >= (c1 + c2))) n       r = mid; n     else n       l = mid + 1; n   } n   cout << l << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int a[16] = {15, 14, 12, 13, 8, 9, 10, 11, 0, 1, 2, 3, 4, 5, 6, 7}; n   int n; n   cin >> n; n   cout << a[n]; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n multiset<long long> h[1000005]; n int main() { n   int q; n   scanf( %d , &q); n   while (q--) { n     char op, s[20]; n     long long a; n     scanf(  %c , &op); n     if (op ==  + ) { n       scanf( %lld , &a); n       int idx = 0, k = 0; n       for (long long x = a; x > 0; x /= 10, k++) { n         int d = x % 10; n         idx |= (d & 1) << k; n       } n       h[idx].insert(a); n     } else if (op ==  - ) { n       scanf( %lld , &a); n       int idx = 0, k = 0; n       for (long long x = a; x > 0; x /= 10, k++) { n         int d = x % 10; n         idx |= (d & 1) << k; n       } n       h[idx].erase(h[idx].find(a)); n     } else { n       scanf( %s , s); n       reverse(s, s + strlen(s)); n       int idx = 0; n       for (int k = 0; s[k]; k++) idx |= (s[k] -  0 ) << k; n       printf( %d n , (int)h[idx].size()); n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n int n, pre = -1; n priority_queue<pair<int, int> > q; n const int M = 3000000 * 4, N = 1e9; n int tree[M], lson[M], rson[M], val[M], size, k, root; n void updata(int &root, int l, int r, int x, int v) { n   if (!root) root = ++size; n   if (l == r) { n     val[root] = tree[root] = v; n     return; n   } n   int mid = (l + r) / 2; n   if (x <= mid) n     updata(lson[root], l, mid, x, v); n   else n     updata(rson[root], mid + 1, r, x, v); n   tree[root] = max(tree[lson[root]], tree[rson[root]]) + val[root]; n } n void add(int &root, int l, int r, int x, int y) { n   if (!root || y < l || r < x) return; n   if (x <= l && r <= y) { n     tree[root]++; n     val[root]++; n     return; n   } n   int mid = (l + r) / 2; n   add(lson[root], l, mid, x, y); n   add(rson[root], mid + 1, r, x, y); n   tree[root] = max(tree[lson[root]], tree[rson[root]]) + val[root]; n } n int ask(int &root, int l, int r, int x, int y) { n   if (!root || y < l || r < x) return 0; n   if (x <= l && r <= y) return tree[root]; n   int mid = (l + r) / 2; n   return max(ask(lson[root], l, mid, x, y), ask(rson[root], mid + 1, r, x, y)) + n          val[root]; n } n int main() { n   scanf( %d%d , &n, &k); n   for (int i = 1; i <= n; i++) { n     int l, r; n     scanf( %d%d , &l, &r); n     l = max(0, l); n     q.push(make_pair(-r, -l)); n     q.push(make_pair(-l, 1)); n   } n   while (!q.empty()) { n     int l = -q.top().second, r = -q.top().first; n     q.pop(); n     if (l >= 0) n       add(root, 0, N, l, r); n     else { n       if (r == pre) continue; n       int u = ask(root, 0, N, pre, pre), v = ask(root, 0, N, 0, r - k); n       if (l == -1 || u < v) { n         pre = r; n         updata(root, 0, N, r, v); n         if (r + root <= N) q.push(make_pair(-r - k, 2)); n       } n     } n   } n   cout << tree[1] << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long int p; n   cin >> p; n   for (long long int z = 0; z < p; z++) { n     long long int n; n     cin >> n; n     while (1) { n       break; n     } n     string a; n     cin >> a; n     long long int re = 0, ro = 0, be = 0, bo = 0; n     for (long long int i = 0; i < n; i++) { n       if (i % 2 == 0) { n         if (a[i] % 2 == 0) { n           re++; n         } else n           ro++; n       } else { n         if (a[i] % 2 == 0) { n           be++; n         } else n           bo++; n       } n     } n     while (1) { n       break; n     } n     if (n % 2 == 0) { n       if (be > 0) n         cout <<  2 n ; n       else n         cout <<  1 n ; n       while (1) { n         break; n       } n     } else { n       while (1) { n         break; n       } n       if (ro > 0) n         cout <<  1 n ; n       else n         cout <<  2 n ; n       while (1) { n         break; n       } n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n void solve() { n   int n; n   cin >> n; n   string s; n   cin >> s; n   int l = -1, r = -1; n   for (int i = 0; i < n; i++) { n     int cnt1 = 0, cnt2 = 0; n     if (s[i] ==  a ) n       ++cnt1; n     else n       ++cnt2; n     for (int j = i + 1; j < n; j++) { n       if (s[j] ==  a ) n         ++cnt1; n       else n         ++cnt2; n       if (cnt1 == cnt2) { n         l = i + 1, r = j + 1; n         break; n       } n     } n   } n   cout << l <<     << r <<   n ; n   return; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   int tt; n   cin >> tt; n   while (tt--) { n     solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int N, M; n int tam[200005], ans[200005]; n map<pair<int, int>, bool> ex; n int main() { n   cin.tie(0); n   ios_base::sync_with_stdio(0); n   cin >> N >> M; n   for (int m = 0; m < M; m++) { n     int u, v; n     cin >> u >> v; n     tam[u]++; n     tam[v]++; n     ex[{u, v}] = 1; n     ex[{v, u}] = 1; n   } n   for (int i = 1; i <= N; i++) n     if (tam[i] < N - 1) { n       cout <<  YES n ; n       for (int j = 1; j <= N; j++) n         if (i != j && !ex[{i, j}]) { n           ans[i] = 1; n           ans[j] = 2; n           int id = 3; n           for (int k = 1; k <= N; k++) n             if (!ans[k]) ans[k] = id++; n           for (int i = 1; i <= N; i++) cout << ans[i] <<    n [i == N]; n           for (int i = 1; i <= N; i++) n             cout << (ans[i] == 2 ? 1 : ans[i]) <<    n [i == N]; n           exit(0); n         } n     } n   cout <<  NO n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int a, d[101], b, f, g, h, j, k, l, n, i; n string s, x; n int main() { n   cin >> s >> x; n   a = s.size(); n   b = x.size(); n   if (s == x) { n     cout <<  YES ; n     return 0; n   } n   if (a != b) { n     cout <<  NO ; n     return 0; n   } n   for (i = 0; i < a; i++) { n     if (s[i] ==  1 ) { n       k++; n     } n     if (x[i] ==  1 ) { n       j++; n     } n   } n   if (k > 0 && j > 0) n     cout <<  YES ; n   else n     cout <<  NO ; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 1505; n const int dx[] = {1, -1, 0, 0}; n const int dy[] = {0, 0, 1, -1}; n int n, m; n pair<int, int> start; n char a[MAXN][MAXN]; n bool vis[MAXN][MAXN]; n pair<int, int> sm[MAXN][MAXN]; n bool dfs(int x, int y, pair<int, int> shift) { n   if (vis[x][y]) { n     return !(sm[x][y] == shift); n   } n   vis[x][y] = true; n   sm[x][y] = shift; n   for (int i = 0; i < 4; i++) { n     int nx = x + dx[i]; n     int ny = y + dy[i]; n     pair<int, int> nshift = shift; n     if (nx >= n) { n       nx = 0; n       nshift.first++; n     } n     if (ny >= m) { n       ny = 0; n       nshift.second++; n     } n     if (nx < 0) { n       nx = n - 1; n       nshift.first--; n     } n     if (ny < 0) { n       ny = m - 1; n       nshift.second--; n     } n     if (a[nx][ny] !=  # ) n       if (dfs(nx, ny, nshift)) return true; n   } n   return false; n } n int main() { n   scanf( %d %d%*c , &n, &m); n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < m; j++) { n       scanf( %c , &a[i][j]); n       if (a[i][j] ==  S ) start.first = i, start.second = j; n     } n     scanf(  n ); n   } n   if (dfs(start.first, start.second, pair<int, int>(0, 0))) n     printf( Yes ); n   else n     printf( No ); n   return 0; n } n


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


#include <bits/stdc++.h> n using namespace std; n pair<long long, long long> a[100100]; n vector<long long> v, v2; n int main() { n   int n, i; n   cin >> n; n   long long Answer = 0; n   for (i = 0; i < n; i++) { n     cin >> a[i].first >> a[i].second; n     v.push_back(a[i].first); n     v2.push_back(a[i].second); n   } n   sort(v.rbegin(), v.rend()); n   sort(v2.rbegin(), v2.rend()); n   for (i = 0; i < n; i++) { n     Answer += max(v[i], v2[i]) + 1; n   } n   cout << Answer; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int A[10005], B[10005]; n long long int dp[10005]; n int main() { n   int n, C; n   scanf( %d %d , &n, &C); n   for (int i = 0; i < n; i++) scanf( %d , &A[i]); n   for (int i = 0; i < n; i++) scanf( %d , &B[i]); n   for (int i = n - 1; i >= 0; i--) { n     for (int j = n; j >= 0; j--) { n       dp[j] += (long long int)j * (long long int)C + B[i]; n       if (j > 0) dp[j] = min(dp[j], dp[j - 1] + A[i]); n     } n   } n   long long int ret = dp[0]; n   for (int j = 0; j <= n; j++) ret = min(ret, dp[j]); n   printf( %lld n , ret); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m, k; n char hh[25]; n string s; n map<string, int> ma; n int fr[100005]; n int ax[100005]; n int bx[100005]; n int main() { n   scanf( %d%d%d , &n, &k, &m); n   int i, j, a, b; n   for (i = 1; i <= n; i++) { n     scanf( %s , hh); n     s = hh; n     ma[s] = i; n   } n   for (i = 1; i <= n; i++) scanf( %d , &ax[i]); n   for (i = 1; i <= k; i++) { n     scanf( %d , &a); n     bx[i] = 1e9; n     for (j = 1; j <= a; j++) { n       scanf( %d , &b); n       bx[i] = min(bx[i], ax[b]); n       fr[b] = i; n     } n   } n   long long res = 0; n   for (i = 1; i <= m; i++) { n     scanf( %s , hh); n     s = hh; n     res += bx[fr[ma[s]]]; n   } n   printf( %lld , res); n   return 0; n } n


#include <bits/stdc++.h> n long long MOD = 1e9 + 7; n using namespace std; n int main() { n   long long n, t; n   cin >> n >> t; n   long long a[200010], x[200010], b[200010]; n   for (int i = 0; i < n; i++) cin >> a[i]; n   for (int i = 0; i < n; i++) { n     cin >> x[i]; n     x[i]--; n     if ((i > 0 && x[i] < x[i - 1]) || x[i] < i) { n       cout <<  No ; n       return 0; n     } n   } n   int c[200010] = {}; n   for (int i = 0; i < n; i++) { n     long long temp = x[i]; n     c[x[i]] = 1; n     if (temp == i) { n       if (i > 0) n         b[i] = max(a[i] + t, b[i - 1] + 1); n       else n         b[i] = a[i] + t; n       if (i < n - 1 && b[i] >= a[i + 1] + t) { n         cout <<  No ; n         return 0; n       } n     } else { n       if (i > 0) n         b[i] = max(a[i + 1] + t, b[i - 1] + 1); n       else n         b[i] = a[i + 1] + t; n     } n     if (i < n - 1 && c[i] && b[i] >= a[i + 1] + t) { n       cout <<  No ; n       return 0; n     } n   } n   cout <<  Yes  << endl; n   for (int i = 0; i < n; i++) { n     printf( %lld  , b[i]); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int counts[26]; n int main() { n   string s; n   cin >> s; n   int N = s.size(); n   for (int i = 0; i < N; i++) counts[s[i] -  a ]++; n   for (int i = 0; i < 26; i++) { n     if (counts[i] % 2 == 1) { n       int k = -1; n       for (int j = 25; j >= 0 && k < 0; j--) n         if (counts[j] % 2 == 1) k = j; n       if (k != -1) counts[i]++, counts[k]--; n     } n   } n   int pos = 0; n   int middle = -1; n   for (int i = 0; i < 26; i++) { n     if (counts[i] % 2 == 1) { n       middle = i; n     } n     for (int j = 0; j + 1 < counts[i]; j += 2) { n       s[pos++] =  a  + i; n       s[N - pos] =  a  + i; n     } n   } n   if (middle != -1) s[N / 2] = middle +  a ; n   cout << s << endl; n } n


#include <bits/stdc++.h> n using namespace std; n const int oo = 0x3f3f3f3f; n const double eps = 1e-9; n void bset(int &b, int i) { b |= (1 << i); } n void bunset(int &b, int i) { b = (b & ~(1 << i)); } n int bget(int b, int i) { return b & (1 << i); } n int bcnt(int b) { return __builtin_popcount(b); } n map<char, int> color2id = {{ R , 0}, { G , 1}, { B , 2}, { Y , 3}, { W , 4}}; n int pos(int col, int val) { return 5 * col + val; } n int best; n int cards; n int stepmask[10]; n int group[12][25], gn[12]; n const int maxN = 100; n int N; n void check(int step, int hints) { n   if (best <= hints) return; n   for (int g = int(gn[step]) - 1; g >= (0); g--) { n     if (bcnt(group[step][g]) > 1) continue; n     group[step][g] = group[step][--gn[step]]; n   } n   if (gn[step] == 0) { n     best = min(best, hints); n     return; n   } n   if (step >= 10) return; n   gn[step + 1] = gn[step]; n   for (int g = (0); g < (gn[step]); g++) { n     group[step + 1][g] = group[step][g]; n   } n   check(step + 1, hints); n   gn[step + 1] = gn[step]; n   for (int g = (0); g < (gn[step]); g++) { n     int a = group[step][g] & stepmask[step]; n     int b = group[step][g] & ~stepmask[step]; n     if (!a || !b) { n       group[step + 1][g] = group[step][g]; n     } else { n       group[step + 1][g] = a; n       group[step + 1][gn[step + 1]++] = b; n     } n   } n   check(step + 1, hints + 1); n } n int main() { n   ios::sync_with_stdio(false); n   for (int c = (0); c < (5); c++) { n     for (int v = (0); v < (5); v++) bset(stepmask[c], pos(c, v)); n     for (int v = (0); v < (5); v++) bset(stepmask[v + 5], pos(c, v)); n   } n   cin >> N; n   for (int i = (0); i < (N); i++) { n     char c; n     int v; n     cin >> c >> v; n     int a = color2id.at(c), b = v - 1; n     bset(cards, pos(a, b)); n   } n   best = 8; n   gn[0] = 1; n   group[0][0] = cards; n   check(0, 0); n   cout << best << endl; n } n


#include <bits/stdc++.h> n int main() { n   int n; n   scanf( %d , &n); n   if (n == 1) { n     printf( -1 ); n     return 0; n   } n   printf( %d %d %d , n, n + 1, n * (n + 1)); n } n


#include <bits/stdc++.h> n using namespace std; n template <class T> n inline void umax(T &a, T b) { n   if (a < b) a = b; n } n const int maxn = 100 + 10; n const int INF = 1e9 + 7; n char a[maxn][maxn]; n int c[maxn], d[maxn]; n inline void solve(void) { n   int n; n   cin >> n; n   for (int i = (0); i < (n); i++) cin >> a[i]; n   for (int i = (0); i < (n); i++) cin >> c[i]; n   vector<int> ans; n   for (int i = (0); i < (n); i++) { n     if (d[i] != c[i]) continue; n     queue<int> v; n     v.push(i); n     while (!v.empty()) { n       int node = v.front(); n       ans.push_back(node); n       v.pop(); n       for (int j = (0); j < (n); j++) { n         if (a[node][j] ==  1 ) { n           d[j]++; n           if (d[j] == c[j]) v.push(j); n         } n       } n     } n   } n   cout << ans.size() << endl; n   for (int i = (0); i < (ans.size()); i++) cout << ans[i] + 1 <<    ; n } n void init() { n   ios::sync_with_stdio(false); n   cin.tie(nullptr); n } n int main(int argc, const char *argv[]) { n   solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, xC = 0, XC = 0, i, f = 0; n   char c; n   char remain; n   cin >> n; n   string o =   ; n   for (i = 0; i < n; i++) { n     cin >> c; n     if (c ==  x ) { n       xC++; n       o = o +  x ; n       if (xC == n / 2) { n         remain =  X ; n         i++; n         break; n       } n     } else { n       XC++; n       o = o +  X ; n       if (XC == n / 2) { n         remain =  x ; n         i++; n         break; n       } n     } n   } n   while (i < n) { n     cin >> c; n     if (c != remain) { n       f++; n     } n     o = o + remain; n     i++; n   } n   cout << f << endl; n   cout << o; n   return 0; n } n


#include <bits/stdc++.h> n template <class T> n bool MX(T &l, const T &r) { n   return l < r ? l = r, 1 : 0; n } n template <class T> n bool MN(T &l, const T &r) { n   return l > r ? l = r, 1 : 0; n } n using namespace std; n const long long MOD = 1e9 + 7; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   int n, a, b, ans = 0, c; n   cin >> n >> a >> b; n   vector<int> s(n / 2); n   for (int i = 0; i < (int)n / 2; ++i) cin >> s[i]; n   reverse(s.begin(), s.end()); n   if (n % 2) { n     cin >> c; n     if (c == 2) ans += min(a, b); n   } n   for (int i = 0; i < (int)n / 2; ++i) { n     cin >> c; n     if (s[i] == 0) { n       if (c == 2) ans += a; n       if (c == 1) { n         cout << -1 << endl; n         return 0; n       } n     } else if (s[i] == 1) { n       if (c == 2) ans += b; n       if (c == 0) { n         cout << -1 << endl; n         return 0; n       } n     } else { n       if (c == 2) ans += 2 * min(a, b); n       if (c == 1) ans += b; n       if (c == 0) ans += a; n     } n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long N = 5 * 1e5 + 100; n long long n, m, nxt[N], MAX[N]; n char s[N], t[N]; n inline void add(long long &a, long long b, long long mod) { n   a = ((a + b >= mod) ? a + b - mod : a + b); n } n inline void del(long long &a, long long b, long long mod) { n   a = ((a - b < 0) ? a - b + mod : a - b); n } n inline void mul(long long &a, long long b, long long mod) { a = a * b % mod; } n inline long long m_pow(long long a, long long b, long long mod) { n   long long ans = 1; n   while (b) { n     if (b & 1) mul(ans, a, mod); n     b >>= 1; n     mul(a, a, mod); n   } n   return ans; n } n inline long long read() { n   long long f = 1, x = 0; n   char s = getchar(); n   while (s <  0  || s >  9 ) { n     if (s ==  - ) f = -1; n     s = getchar(); n   } n   while (s >=  0  && s <=  9 ) { n     x = x * 10 + s -  0 ; n     s = getchar(); n   } n   return x * f; n } n struct Hash { n   long long a, b; n } z[N]; n Hash h[N], x, th[N]; n Hash operator+(Hash a, Hash b) { n   add(a.a, b.a, 998244353); n   add(a.b, b.b, 342914237); n   return a; n } n Hash operator-(Hash a, Hash b) { n   del(a.a, b.a, 998244353); n   del(a.b, b.b, 342914237); n   return a; n } n Hash operator*(Hash a, Hash b) { n   mul(a.a, b.a, 998244353); n   mul(a.b, b.b, 342914237); n   return a; n } n Hash operator*(Hash a, long long b) { n   mul(a.a, b, 998244353); n   mul(a.b, b, 342914237); n   return a; n } n bool operator==(Hash a, Hash b) { return a.a == b.a && a.b == b.b; } n Hash ask(long long l, long long r) { return h[r] - h[l - 1] * z[r - l + 1]; } n void check(long long a, long long b, long long c, long long d) { n   assert(b + 1 == c); n   if (a > b || c > d || d > n || a <= 0) return; n   if (ask(a, b) + ask(c, d) == x) { n     vector<long long> X, Y; n     if (b - a < d - c) swap(a, c), swap(b, d); n     for (long long i = b; i >= a; i--) X.push_back(s[i] -  0 ); n     for (long long i = d; i >= c; i--) Y.push_back(s[i] -  0 ); n     for (long long i = 0; i < (long long)Y.size(); i++) X[i] += Y[i]; n     long long tmp = 0; n     for (long long i = 0; i < (long long)X.size(); i++) { n       X[i] += tmp; n       tmp = X[i] / 10; n       X[i] %= 10; n     } n     if (tmp) X.push_back(tmp); n     if ((long long)X.size() != m) return; n     bool bl = 1; n     for (long long i = 1; i <= m; i++) n       if (X[(long long)X.size() - i] != t[i] -  0 ) bl = 0; n     if (!bl) return; n     if (b > d) swap(a, c), swap(b, d); n     printf( %lld %lld n%lld %lld n , a, b, c, d); n     exit(0); n   } n } n long long find(long long i) { n   if (s[i] != t[1]) return 0; n   long long l = 1, r = m; n   while (l < r) { n     long long mid = l + ((r - l + 1) >> 1); n     if (ask(i, i + mid - 1) == th[mid]) n       l = mid; n     else n       r = mid - 1; n   } n   return l; n } n signed main() { n   scanf( %s%s , s + 1, t + 1); n   n = strlen(s + 1); n   m = strlen(t + 1); n   for (long long i = 1; i <= m; i++) { n     x = x * 10 + (Hash){t[i] -  0 , t[i] -  0 }; n     th[i] = x; n   } n   z[0] = (Hash){1, 1}; n   for (long long i = 1; i <= n; i++) z[i] = z[i - 1] * (Hash){10, 10}; n   for (long long i = 1; i <= n; i++) n     h[i] = h[i - 1] * 10 + (Hash){s[i] -  0 , s[i] -  0 }; n   for (long long i = 1; i <= n; i++) { n     check(i, i + m - 1, i + m, i + m + m - 1); n     check(i, i + m - 1, i + m, i + m + m - 2); n     check(i, i + m - 2, i + m - 1, i + m + m - 2); n     check(i, i + m - 2, i + m - 1, i + m + m - 3); n   } n   for (long long i = 1; i <= n; i++) { n     long long pos = find(i); n     if (pos == m) continue; n     check(i, i + m - 1, i + m, i + 2 * m - pos - 1); n     check(i, i + m - 1, i + m, i + 2 * m - pos - 2); n     check(i - (m - pos), i - 1, i, i + m - 1); n     check(i - (m - pos - 1), i - 1, i, i + m - 1); n   } n   assert(0); n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 1e5 + 10; n char buf[1 << 15], *fs, *ft; n inline char getc() { n   return (ft == fs && n           (ft = (fs = buf) + fread(buf, 1, 1 << 15, stdin), ft == fs)) n              ? 0 n              : *fs++; n } n template <typename T> n inline void read(T &x) { n   x = 0; n   T f = 1, ch = getchar(); n   while (!isdigit(ch) && ch ^  - ) ch = getchar(); n   if (ch ==  - ) f = -1, ch = getchar(); n   while (isdigit(ch)) x = (x << 1) + (x << 3) + (ch ^ 48), ch = getchar(); n   x *= f; n } n template <typename T> n inline void write(T &x) { n   if (!x) { n     putchar( 0 ), putchar(  n ); n     return; n   } n   if (x < 0) putchar( - ), x = -x; n   T num = 0, ch[16]; n   while (x) ch[++num] = x % 10 + 48, x /= 10; n   while (num) putchar(ch[num--]); n   putchar(  n ); n } n int q[maxn], now; n long long f[2][maxn], a[maxn], d[maxn], sum[maxn]; n inline long long getUp(int j, int k) { n   return (f[now ^ 1][j] + sum[j]) - (f[now ^ 1][k] + sum[k]); n } n inline long long getDw(int j, int k) { return j - k; } n int main() { n   int n, m, p; n   read(n); n   read(m); n   read(p); n   for (int i = 2; i <= n; ++i) read(d[i]), d[i] += d[i - 1]; n   for (int i = 1; i <= m; ++i) { n     int h; n     long long t; n     read(h); n     read(t); n     a[i] = t - d[h]; n   } n   sort(a + 1, a + m + 1); n   for (int i = 1; i <= m; ++i) sum[i] = sum[i - 1] + a[i], f[0][i] = 1e18; n   long long ans = 1e18; n   now = 1; n   for (int t = 1; t <= p; ++t) { n     int l = 1, r = 1; n     q[1] = 0; n     for (int i = 1; i <= m; ++i) { n       while (l < r && getUp(q[l + 1], q[l]) <= a[i] * getDw(q[l + 1], q[l])) n         ++l; n       f[now][i] = f[now ^ 1][q[l]] + (long long)a[i] * (i - q[l]) - n                   (sum[i] - sum[q[l]]); n       while (l < r && getUp(i, q[r]) * getDw(q[r], q[r - 1]) <= n                           getUp(q[r], q[r - 1]) * getDw(i, q[r])) n         --r; n       q[++r] = i; n     } n     ans = min(ans, f[now][m]); n     now ^= 1; n   } n   write(ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long int gcd(long long int a, long long int b) { n   if (a == 0) return b; n   return gcd(b % a, a); n } n int main() { n   long long int n, a[100001], sum = 0, m; n   cin >> n; n   for (int i = 0; i < n; ++i) cin >> a[i]; n   sort(a, a + n); n   for (int i = 1; i < n; ++i) { n     sum += i * a[i] - a[i - 1]; n     a[i] += a[i - 1]; n   } n   sum = 2 * sum + a[n - 1]; n   m = gcd(sum, n); n   cout << sum / m <<     << n / m; n } n


#include <bits/stdc++.h> n using namespace std; n class Solution { n  public: n   int count(vector<int>& pieces, int p) { n     int n = pieces.size(); n     preprocess(n); n     vector<vector<vector<int>>> cur( n         n + 1, vector<vector<int>>(n + 1, vector<int>(n + 1, 0))); n     vector<vector<vector<int>>> nxt( n         n + 1, vector<vector<int>>(n + 1, vector<int>(n + 1, 0))); n     cur[0][0][0] = 1; n     for (int i = 0; i < n; ++i) { n       vector<int> cv; n       if (pieces[i] == -1) { n         cv.push_back(0); n         cv.push_back(1); n       } else { n         cv.push_back(pieces[i]); n       } n       for (auto& vv : nxt) { n         for (auto& v : vv) { n           fill(v.begin(), v.end(), 0); n         } n       } n       for (int j = 0; j <= i; ++j) { n         for (int k = 0; k <= j; ++k) { n           for (int l = 0; l <= i - j; ++l) { n             if (cur[j][k][l] == 0) { n               continue; n             } n             for (auto c : cv) { n               if (c == 0) { n                 int nj = j + 1; n                 nxt[nj][k + 1][l] = n                     add(nxt[nj][k + 1][l], n                         multiply(cur[j][k][l], n                                  multiply(1LL << j, multiply(1LL << (i - j - l), n                                                              csums[l][0])))); n                 nxt[nj][k][l] = n                     add(nxt[nj][k][l], n                         multiply(cur[j][k][l], n                                  multiply(1LL << j, multiply(1LL << (i - j - l), n                                                              csums[l][1])))); n               } else { n                 int nj = j; n                 nxt[nj][k][l + 1] = add( n                     nxt[nj][k][l + 1], n                     multiply(cur[j][k][l], n                              multiply(1LL << (i - j), n                                       multiply(1LL << (j - k), csums[k][0])))); n                 nxt[nj][k][l] = add( n                     nxt[nj][k][l], n                     multiply(cur[j][k][l], n                              multiply(1LL << (i - j), n                                       multiply(1LL << (j - k), csums[k][1])))); n               } n             } n           } n         } n       } n       cur.swap(nxt); n     } n     long long res = 0; n     for (int i = 0; i <= n; ++i) { n       for (int j = 0; j <= i; ++j) { n         for (int k = 0; k <= n - i; ++k) { n           if ((j + k) % 2 == p) { n             res = add(res, cur[i][j][k]); n           } n         } n       } n     } n     return res; n   } n  n  private: n   const static int M = 1e9 + 7; n   vector<int> fv; n   vector<int> ifv; n   vector<vector<int>> cs; n   vector<vector<int>> csums; n   long long add(long long a, long long b) { n     long long res = (a + b) % M; n     return res; n   } n   long long multiply(long long a, long long b) { return (a * b) % M; } n   long long pow(long long x, long long e) { n     long long res = 1; n     while (e > 0) { n       if (e & 1) { n         res = multiply(res, x); n       } n       x = multiply(x, x); n       e >>= 1; n     } n     return res; n   } n   void preprocess(int n) { n     fv.clear(); n     fv.resize(n + 1); n     ifv.clear(); n     ifv.resize(n + 1); n     cs.clear(); n     cs.resize(n + 1); n     csums.clear(); n     csums.resize(n + 1); n     for (auto& v : csums) { n       v.resize(2); n       fill(v.begin(), v.end(), 0); n     } n     fv[0] = 1; n     for (int i = 1; i <= n; ++i) { n       fv[i] = multiply(fv[i - 1], i); n     } n     ifv[0] = 1; n     ifv[n] = pow(fv[n], M - 2); n     for (int i = n - 1; i >= 1; --i) { n       ifv[i] = multiply(ifv[i + 1], i + 1); n     } n     for (int i = 1; i <= n; ++i) { n       cs[i].resize(i + 1); n       for (int j = 0; j <= i; ++j) { n         cs[i][j] = multiply(multiply(fv[i], ifv[j]), ifv[i - j]); n       } n     } n     csums[0][0] = 1; n     for (int i = 1; i <= n; ++i) { n       for (int j = 0; j <= i; ++j) { n         if (j % 2) { n           csums[i][1] = add(csums[i][1], cs[i][j]); n         } else { n           csums[i][0] = add(csums[i][0], cs[i][j]); n         } n       } n     } n   } n }; n int main(int argc, char** argv) { n   ios::sync_with_stdio(false); n   cin.tie(0); n   int n; n   int p; n   cin >> n >> p; n   vector<int> nums(n); n   for (int i = 0; i < n; ++i) { n     cin >> nums[i]; n   } n   Solution sol; n   cout << sol.count(nums, p) << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n using namespace chrono; n struct stopwatch { n   time_point<high_resolution_clock> last_tick; n   bool notock = false; n   void tick() { last_tick = high_resolution_clock::now(); } n   void tock() const { n     duration<double> dur = high_resolution_clock::now() - last_tick; n     cerr <<  Time:   << dur.count() <<   n ; n   } n   stopwatch() { tick(); } n   stopwatch(int) { n     tick(); n     notock = true; n   } n   ~stopwatch() { n     if (!notock) { n       tock(); n     } n   } n }; n template <int m> n struct modint { n   unsigned x; n   modint() : x(0) {} n   modint(long long arg) { n     arg %= m; n     if (arg < 0) { n       x = arg + m; n     } else { n       x = arg; n     } n   } n   modint& operator+=(const modint& other) { n     x += other.x; n     if (x >= m) { n       x -= m; n     } n     return *this; n   } n   modint& operator*=(const modint& other) { n     x = (x * 1ll * other.x) % m; n     return *this; n   } n   modint& operator-=(const modint& other) { n     x += m - other.x; n     if (x >= m) { n       x -= m; n     } n     return *this; n   } n   modint operator+(const modint& other) const { n     modint tmp = *this; n     tmp += other; n     return tmp; n   } n   modint operator-(const modint& other) const { n     modint tmp = *this; n     tmp -= other; n     return tmp; n   } n   modint operator*(const modint& other) const { n     modint tmp = *this; n     tmp *= other; n     return tmp; n   } n   explicit operator int() const { return x; } n   modint& operator++() { n     ++x; n     if (x == m) { n       x = 0; n     } n     return *this; n   } n   modint& operator--() { n     if (x == 0) { n       x = m - 1; n     } else { n       --x; n     } n     return *this; n   } n   modint operator++(int) { n     modint tmp = *this; n     ++*this; n     return tmp; n   } n   modint operator--(int) { n     modint tmp = *this; n     --*this; n     return tmp; n   } n   bool operator==(const modint& other) const { return x == other.x; } n   bool operator!=(const modint& other) const { return x != other.x; } n   template <class T> n   modint operator^(T arg) const { n     if (arg == 0) { n       return 1; n     } n     if (arg == 1) { n       return x; n     } n     auto t = *this ^ (arg >> 1); n     t *= t; n     if (arg & 1) { n       t *= *this; n     } n     return t; n   } n   template <class T> n   modint operator^=(T arg) { n     return *this = *this ^ arg; n   } n }; n const int MOD = 1051131; n const int TOT = 700752; n int m, s, n; n long long t; n valarray<modint<MOD> > a; n valarray<modint<MOD> > em_inv_slow(int m, int n, n                                    const valarray<modint<MOD> >& a) { n   int k = 0; n   valarray<modint<MOD> > b(n); n   for (int i = 0; i < m; i++) { n     int w = 1 << (m - i - 1); n     int c = 1 << i; n     modint<MOD> mult = modint<MOD>(2) ^ (TOT - i - 1); n     for (int j = 0; j < w; j++) { n       modint<MOD> t = a[k] * mult; n       b[slice(j + 0, c, 2 * w)] -= valarray<modint<MOD> >(t, c); n       b[slice(j + w, c, 2 * w)] += valarray<modint<MOD> >(t, c); n       k++; n     } n   } n   modint<MOD> t = (modint<MOD>(2) ^ (TOT - m)); n   b += a[n - 1] * t; n   return b; n } n void em_inv(int m, int n, valarray<modint<MOD> >& a) { n   int k = 0; n   valarray<long long> bll(n); n   for (int i = 0; i < m; i++) { n     const int w = 1 << (m - i - 1); n     long long mult = (int)(modint<MOD>(2) ^ (TOT - i - 1)); n     for (int j0 = 0; j0 < n; j0 += w + w) { n       for (int j = 0; j < w; j++) bll[j + j0] -= (int)a[k + j] * mult; n       for (int j = 0; j < w; j++) bll[j + j0 + w] += (int)a[k + j] * mult; n     } n     k += w; n   } n   modint<MOD> t = (modint<MOD>(2) ^ (TOT - m)) * a[n - 1]; n   for (int i = 0; i < n; i++) a[i] = bll[i]; n   a += t; n } n valarray<modint<MOD> > em_slow(int m, int n, const valarray<modint<MOD> >& a) { n   int k = 0; n   valarray<modint<MOD> > b(n); n   for (int i = 0; i < m; i++) { n     int w = 1 << (m - i - 1); n     int c = 1 << i; n     for (int j = 0; j < w; j++) { n       b[k] -= a[slice(j + 0, c, 2 * w)].sum(); n       b[k] += a[slice(j + w, c, 2 * w)].sum(); n       k++; n     } n   } n   b[k++] += a.sum(); n   return b; n } n valarray<modint<MOD> > em(int m, int n, const valarray<modint<MOD> >& a) { n   int k = 0; n   valarray<modint<MOD> > b(n); n   for (int i = 0; i < m; i++) { n     const int w = 1 << (m - i - 1); n     for (int j0 = 0; j0 < n; j0 += w + w) { n       for (int j = 0; j < w; j++) b[k + j] -= a[j + j0]; n       for (int j = 0; j < w; j++) b[k + j] += a[j + j0 + w]; n     } n     k += w; n   } n   b[k++] += a.sum(); n   return b; n } n valarray<modint<MOD> > eigenvals(int m, int n, long long t) { n   int k = 0; n   valarray<modint<MOD> > b(n); n   for (int i = 0; i < m; i++) { n     int w = 1 << (m - i - 1); n     modint<MOD> x = 1 + (i - 1) * (n / 2); n     b[slice(k, w, 1)] = x ^ t; n     k += w; n   } n   b[k++] = modint<MOD>(1 + (n / 2) * m) ^ t; n   return b; n } n void pr(const valarray<modint<MOD> >& a) { n   for (modint<MOD> x : a) cerr << (int)x <<    ; n   cerr <<   n ; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(nullptr); n   cout.tie(nullptr); n   cerr.tie(nullptr); n   stopwatch sw(0); n   cin >> m >> t >> s; n   n = 1 << m; n   a.resize(n); n   for (int i = 0; i < s; i++) { n     int x; n     cin >> x; n     a[i] = x; n   } n   sw.tock(); n   for (int i = s; i < n; i++) a[i] = a[i - s] * 101 + 10007; n   sw.tock(); n   a = em(m, n, a); n   sw.tock(); n   { n     valarray<modint<MOD> > b = eigenvals(m, n, t); n     sw.tock(); n     for (int i = 0; i < n; i++) a[i] = a[i] * b[i]; n     sw.tock(); n   } n   em_inv(m, n, a); n   sw.tock(); n   int xr = 0; n   for (modint<MOD> x : a) xr ^= (int)x; n   sw.tock(); n   cout << xr <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int T, money; n   cin >> T >> money; n   int n; n   int g; n   vector<int> lista; n   for (int i = 1; i <= T; i++) { n     cin >> n; n     int brk = 0; n     for (int j = 0; j < n; j++) { n       cin >> g; n       if (brk == 0 && money > g) { n         lista.push_back(i); n         brk = 1; n       } n     } n   } n   cout << lista.size() << endl; n   for (int i = 0; i < lista.size(); i++) { n     cout << lista[i] <<    ; n   } n   return 0; n } n


#include <bits/stdc++.h> n #pragma GCC optimize(3,  inline ,  Ofast ) n using namespace std; n int p[1010]; n bool vis[1010]; n void dfs(int pos) { n   if (vis[pos]) n     return (void)(printf( %d  , pos)); n   else n     vis[pos] = 1, dfs(p[pos]); n } n int main() { n   int n; n   scanf( %d , &n); n   for (int i = 1; i <= n; i++) scanf( %d , &p[i]); n   for (int i = 1; i <= n; i++) memset(vis, 0, sizeof vis), dfs(i); n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 100100; n bool vis[maxn]; n long long val[3][maxn]; n long long mii[3]; n vector<int> g[maxn]; n vector<int> a; n int ans[maxn]; n int d[maxn]; n void dfs(int now) { n   a.push_back(now); n   vis[now] = 1; n   for (int i = 0; i < g[now].size(); i++) { n     if (vis[g[now][i]]) continue; n     dfs(g[now][i]); n   } n } n int main() { n   int n; n   scanf( %d , &n); n   for (int j = 0; j < 3; j++) { n     for (int i = 1; i <= n; i++) { n       scanf( %d , &val[j][i]); n     } n   } n   for (int i = 0; i < n - 1; i++) { n     int f, t; n     scanf( %d%d , &f, &t); n     g[f].push_back(t); n     g[t].push_back(f); n     d[f]++; n     d[t]++; n     if (d[f] > 2 || d[t] > 2) { n       printf( -1 n ); n       return 0; n     } n   } n   vector<int> temp; n   temp.push_back(0); n   temp.push_back(1); n   temp.push_back(2); n   for (int i = 1; i <= n; i++) { n     if (d[i] == 1) { n       dfs(i); n       break; n     } n   } n   long long mi = 1ll << 60; n   for (int _ = 0; _ < 6; _++) { n     long long now = 0; n     for (int i = 0; i < n; i++) { n       now += val[temp[i % 3]][a[i]]; n     } n     if (now < mi) { n       mi = now; n       for (int j = 0; j < 3; j++) { n         mii[j] = temp[j]; n       } n     } n     next_permutation(temp.begin(), temp.end()); n   } n   printf( %lld n , mi); n   for (int i = 0; i < n; i++) { n     ans[a[i]] = mii[i % 3]; n   } n   for (int i = 1; i <= n; i++) { n     printf( %d  , ans[i] + 1); n   } n   puts(  ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int dp[510][510]; n long long MOD; n vector<string> v; n long long nc2(long long num) { return (num * (num - 1)) / 2; } n int recur(int ones, int zeros) { n   if (ones == 0 and zeros == 0) return 1; n   if (dp[ones][zeros] != -1) { n     return dp[ones][zeros] % MOD; n   } n   long long ret = 0; n   if (ones > 1) { n     ret = (ret + ((recur(ones - 2, zeros) * nc2(ones)) % MOD)) % MOD; n   } n   if (zeros > 1) { n     ret = (ret + ((recur(ones + 2, zeros - 2) * nc2(zeros)) % MOD)) % MOD; n   } n   if (ones >= 1 && zeros >= 1) { n     ret = n         (ret + ((recur(ones, zeros - 1) * ((ones * zeros) % MOD)) % MOD)) % MOD; n   } n   dp[ones][zeros] = ret % MOD; n   return dp[ones][zeros]; n } n int solve(int n, int m) { n   int ones = 0; n   int zeros = 0; n   for (int i = 0; i < n; i++) { n     int cnt = 0; n     for (int j = 0; j < m; j++) { n       if (v[j][i] ==  1 ) cnt++; n     } n     if (cnt == 1) ones++; n     if (cnt == 0) zeros++; n   } n   memset(dp, -1, sizeof(dp)); n   int res = recur(ones, zeros); n   return res; n } n int main() { n   int n, m; n   cin >> n >> m >> MOD; n   string line =   ; n   for (int i = 0; i < m; i++) { n     cin >> line; n     v.push_back(line); n   } n   int res = solve(n, m); n   cout << res << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 3e5 + 10; n inline int read() { n   char ch = getchar(); n   int x = 0, f = 1; n   while (ch <  0  || ch >  9 ) { n     if (ch ==  - ) f = -1; n     ch = getchar(); n   } n   while (ch >=  0  && ch <=  9 ) { n     x = (x << 1) + (x << 3) -  0  + ch; n     ch = getchar(); n   } n   return x * f; n } n int n, k, size[N]; n long long ans, f[N][5]; n vector<int> g[N]; n void dfs(int u, int fa, int d) { n   size[u] = 1; n   f[u][d % k] = 1; n   for (auto &v : g[u]) { n     if (v == fa) continue; n     dfs(v, u, d + 1); n     size[u] += size[v]; n     ans += 1ll * (n - size[v]) * size[v]; n     for (int i = 0; i < k; i++) { n       for (int j = 0; j < k; j++) { n         int z = ((i + j - 2 * d) % k + k) % k; n         z = (k - z) % k; n         ans += 1ll * z * f[u][i] * f[v][j]; n       } n     } n     for (int i = 0; i < k; i++) f[u][i] += f[v][i]; n   } n } n int main() { n   n = read(), k = read(); n   for (int i = 1; i <= n - 1; i++) { n     int a = read(), b = read(); n     g[a].push_back(b); n     g[b].push_back(a); n   } n   dfs(1, 0, 0); n   printf( %lld n , ans / k); n } n


#include <bits/stdc++.h> n using namespace std; n int main(void) { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   int n; n   cin >> n; n   string a[n], b[n]; n   map<string, int> m; n   for (int i = 0; i < n; ++i) { n     cin >> a[i]; n     ++m[a[i]]; n   } n   for (int i = 0; i < n; ++i) { n     cin >> b[i]; n     --m[b[i]]; n   } n   int ans = 0; n   vector<string> v = { XXXL ,  XXXS ,  XXL ,  XXS ,  XL ,  XS ,  S ,  L ,  M }; n   ans = (abs(m[ XXXL ]) + abs(m[ XXXS ])) / 2; n   ans += (abs(m[ XXL ]) + abs(m[ XXS ])) / 2; n   ans += (abs(m[ XL ]) + abs(m[ XS ])) / 2; n   ans += (abs(m[ S ]) + abs(m[ L ]) + abs(m[ M ])) / 2; n   cout << ans; n } n


#include <bits/stdc++.h> n using namespace std; n long long n, q, ans[50005], sum[50005], a[50005], p[50005], x[50005], cont, pos, n     us[50005]; n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   cin >> n >> q; n   for (int i = 1; i <= n; i++) cin >> a[i]; n   for (int i = 2; i <= n; i++) cin >> p[i]; n   for (int i = 0; i < q; i++) cin >> x[i]; n   for (int i = 1; i <= n; i++) { n     pos = i; n     cont = 1; n     while (pos != 1) { n       us[pos] = cont; n       cont++; n       pos = p[pos]; n     } n     us[pos] = cont; n     for (int j = 0; j < q; j++) { n       sum[j] += us[x[j]]; n       if (us[x[j]]) { n         ans[j]++; n         j += a[i] - 1; n       } n     } n     pos = i; n     cont = 1; n     while (pos != 1) { n       us[pos] = 0; n       cont++; n       pos = p[pos]; n     } n     us[pos] = 0; n   } n   for (int i = 0; i < q; i++) cout << sum[i] - ans[i] <<     << ans[i] <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n string S, T; n int n, m; n int nxt[200005][30]; n unsigned long long hashS[200005][30], hashT[200005][30], bpw[200005]; n unsigned long long ht; n int base = 10007; n int f[30]; n vector<int> ans; n bool chk(int p) { n   memset(f, 0, sizeof(f)); n   int ps, pt; n   unsigned long long hps, hpt, temp = 0; n   for (int j = 1; j <= 26; j++) { n     if (nxt[p][j] > p + m - 1) continue; n     ps = j; n     pt = T[(nxt[p][j] - p) + 1] -  a  + 1; n     if (f[ps] == pt) n       continue; n     else if (f[ps] && f[ps] != pt) n       return 0; n     else { n       f[ps] = pt; n       f[pt] = ps; n       hps = hashS[p + m - 1][ps] - bpw[m] * hashS[p - 1][ps]; n       hpt = hashT[m][pt]; n       if (hps != hpt || n           hashS[p + m - 1][pt] - bpw[m] * hashS[p - 1][pt] != hashT[m][ps]) { n         return 0; n       } n       temp += hps * pt; n       if (ps != pt) temp += ps * hashT[m][ps]; n     } n   } n   return temp == ht; n } n int main() { n   scanf( %d%d , &n, &m); n   cin >> S >> T; n   S =  %  + S; n   T =  %  + T; n   for (int i = 1; i <= m; i++) { n     ht = ht * base + T[i] -  a  + 1; n   } n   for (int i = 1; i <= 26; i++) { n     nxt[n + 1][i] = n + 1; n   } n   for (int i = n; i >= 1; i--) { n     for (int j = 1; j <= 26; j++) { n       if (S[i] ==  a  + j - 1) n         nxt[i][j] = i; n       else n         nxt[i][j] = nxt[i + 1][j]; n     } n   } n   bpw[0] = 1; n   for (int i = 1; i <= n; i++) { n     bpw[i] = bpw[i - 1] * base; n   } n   for (int i = 1; i <= n; i++) { n     for (int j = 1; j <= 26; j++) { n       hashS[i][j] = hashS[i - 1][j] * base + (S[i] == ( a  + j - 1) ? 1 : 0); n     } n   } n   for (int i = 1; i <= m; i++) { n     for (int j = 1; j <= 26; j++) { n       hashT[i][j] = hashT[i - 1][j] * base + (T[i] == ( a  + j - 1) ? 1 : 0); n     } n   } n   for (int i = 1; i <= n - m + 1; i++) { n     if (chk(i)) ans.push_back(i); n   } n   cout << ans.size() << endl; n   for (int i = 0; i < ans.size(); i++) { n     printf( %d  , ans[i]); n   } n   return 0; n } n


#include <bits/stdc++.h> n int main() { n   int t, n; n   scanf( %d , &t); n   while (t--) { n     scanf( %d , &n); n     if (n == 1) { n       printf( -1 n ); n     } else { n       printf( 2 ); n       for (int i = 1; i < n; i++) { n         printf( 7 ); n       } n       printf(  n ); n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n inline long long read() { n   long long x = 0, f = 1; n   char c = getchar(); n   while ((c <  0  || c >  9 ) && (c !=  - )) c = getchar(); n   if (c ==  - ) f = -1, c = getchar(); n   while (c >=  0  && c <=  9 ) x = x * 10 + c -  0 , c = getchar(); n   return x * f; n } n const int N = 3e4 + 10; n int n; n vector<int> e[N]; n int cnt, p[N], fa[N], vis[N]; n bool flag[N]; n inline void dfs(int u) { n   vis[u] = 1; n   for (auto v : e[u]) { n     if (v == fa[u]) continue; n     fa[v] = u; n     if (!vis[v]) n       dfs(v); n     else { n       for (int now = u; now != v; now = fa[now]) flag[p[++cnt] = now] = 1; n       flag[p[++cnt] = v] = 1; n     } n     if (cnt) break; n   } n } n long long f[N], g[N], siz[N]; n inline void dfs(int u, int fa) { n   siz[u] = 1; n   for (auto v : e[u]) { n     if (v == fa || flag[v]) continue; n     dfs(v, u), f[u] += g[v], siz[u] += siz[v]; n   } n   g[u] = f[u] + siz[u]; n } n long long dp[N], last[N], pre[N], ans; n inline void dfs(int u, int fa, long long sum) { n   ans = max(ans, sum + n + f[u]); n   for (auto v : e[u]) n     if (v != fa) dfs(v, u, sum + n - siz[v] + f[u] - g[v]); n } n inline void solve() { n   long long sum = 0; n   for (register int i = (1); i <= (cnt * 2); i++) n     last[i] = siz[p[i]], pre[i] = pre[i - 1] + last[i], sum += f[p[i]]; n   for (register int i = (2); i <= (cnt - 1); i++) { n     for (register int l = (1); l <= (cnt * 2 - i + 1); l++) { n       int r = l + i - 1; n       long long tmp = pre[r] - pre[l - 1]; n       dp[l] = max(last[l] + tmp, last[l + 1] + tmp); n     } n     for (register int j = (1); j <= (cnt * 2 - i + 1); j++) last[j] = dp[j]; n   } n   ans = 0; n   for (register int i = (1); i <= (cnt); i++) { n     ans = max(ans, n + sum / 2 + dp[i + 1]); n     for (auto v : e[p[i]]) n       if (!flag[v]) n         dfs(v, p[i], n             n - siz[v] + sum / 2 - f[p[i]] + f[p[i]] - g[v] + dp[i + 1]); n   } n   printf( %lld n , ans); n } n int main() { n   n = read(); n   for (register int i = (1); i <= (n); i++) { n     int x = read() + 1, y = read() + 1; n     e[x].push_back(y), e[y].push_back(x); n   } n   dfs(1); n   for (register int i = (1); i <= (cnt); i++) dfs(p[i], 0); n   for (register int i = (1); i <= (cnt); i++) p[cnt + i] = p[i]; n   solve(); n } n


#include <bits/stdc++.h> n using namespace std; n long long inv_euclid(long long a, long long m = 998244353) { n   long long m0 = m; n   long long y = 0, x = 1; n   if (m == 1) return 0; n   while (a > 1) { n     long long q = a / m; n     long long t = m; n     m = a % m, a = t; n     t = y; n     y = x - q * y; n     x = t; n   } n   if (x < 0) x += m0; n   return x; n } n long long power(long long x, long long y, long long p) { n   long long res = 1; n   x = x % p; n   if (x == 0) return 0; n   while (y > 0) { n     if (y & 1) res = (res * x) % p; n     y = y >> 1; n     x = (x * x) % p; n   } n   return res; n } n long long max(long long a, long long b) { n   if (a > b) return a; n   return b; n } n long long min(long long a, long long b) { n   if (a < b) return a; n   return b; n } n long long ssum(long long n) { return (n * (n + 1)) / 2; } 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 lcm(long long a, long long b) { return (a * b) / (gcd(a, b)); } n void solve() { n   long long n; n   cin >> n; n   vector<long long> v(n); n   for (long long i = 0; i < n; i++) cin >> v[i]; n   while (v.size() && v[v.size() - 1] == 0) v.pop_back(); n   reverse(v.begin(), v.end()); n   while (v.size() && v[v.size() - 1] == 0) v.pop_back(); n   long long ans = 0; n   for (auto i : v) ans += (i == 0); n   cout << ans <<   n ; n } n int main() { n   long long tttt; n   cin >> tttt; n   while (tttt--) { n     solve(); n   } n } n


#include <bits/stdc++.h> n using namespace std; n void solve() { n   long long n, m, k, i, j, count = 0, sum = 0, maxi = 0, mini = 1e18, flag = 0; n   cin >> n; n   vector<long long> res; n   vector<char> temp; n   string a, b; n   cin >> a >> b; n   if (a == b) { n     cout << 0 <<   n ; n     return; n   } n   if (n == 1 && a[0] != b[0]) { n     cout << 1 <<     << 1 <<   n ; n     return; n   } n   for (i = 0; i < n / 2; i++) { n     temp.push_back(a[i]); n     if (a[n - i - 1] ==  1 ) n       temp.push_back( 0 ); n     else n       temp.push_back( 1 ); n   } n   if (n % 2) temp.push_back(a[n / 2]); n   i = 0; n   j = n - 1; n   while (j > 0) { n     if (temp[i] == b[j]) { n       res.push_back(0); n       res.push_back(j); n     } else n       res.push_back(j); n     j--; n     i++; n   } n   if (j == 0) { n     if (temp[i] == b[j]) n       ; n     else n       res.push_back(0); n   } n   cout << res.size() <<    ; n   for (i = 0; i < res.size(); i++) { n     cout << res[i] + 1 <<    ; n   } n   cout <<   n ; n } n int32_t main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   long long t = 1; n   cin >> t; n   while (t--) { n     solve(); n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 5005; n int u[maxn], v[maxn], w[maxn], dp[maxn][maxn], parent[maxn][maxn], x[maxn]; n int main() { n   int n, m, t, pos; n   cin >> n >> m >> t; n   for (int i = 0; i < m; i++) { n     cin >> u[i] >> v[i] >> w[i]; n   } n   memset(dp, 0x3f, sizeof(dp)); n   dp[1][1] = 0; n   for (int i = 2; i <= n; i++) { n     for (int j = 0; j < m; j++) { n       if (dp[i - 1][u[j]] + w[j] < dp[i][v[j]]) { n         dp[i][v[j]] = dp[i - 1][u[j]] + w[j]; n         parent[i][v[j]] = u[j]; n       } n     } n     if (dp[i][n] <= t) pos = i; n   } n   int id = n; n   for (int i = pos; i >= 1; i--) { n     x[i] = id; n     id = parent[i][id]; n   } n   cout << pos << endl; n   for (int i = 1; i <= pos; i++) cout << x[i] <<    ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int k, n, m; n const int dx[4] = {-1, 1, 0, 0}; n const int dy[4] = {0, 0, -1, 1}; n int bx, by, s, ansx[1000000], ansy[1000000]; n bool g[510][510]; n char ansc[1000000]; n int dfs(int x, int y) { n   if (x > 0 && y > 0 && x <= n && y <= m && g[x][y] == 0) { n     g[x][y] = 1; n     s++; n     ansc[s] =  B ; n     ansx[s] = x; n     ansy[s] = y; n     int nx, ny; n     for (int i = 0; i < 4; i++) { n       nx = x + dx[i]; n       ny = y + dy[i]; n       dfs(nx, ny); n     } n     if (bx != x || by != y) { n       s++; n       ansc[s] =  D ; n       ansx[s] = x; n       ansy[s] = y; n       s++; n       ansc[s] =  R ; n       ansx[s] = x; n       ansy[s] = y; n     } n   } n } n int main() { n   cin >> n >> m; n   for (int i = 1; i <= n; i++) n     for (int j = 1; j <= m; j++) { n       char c; n       cin >> c; n       if (c ==  # ) g[i][j] = true; n     } n   for (int i = 1; i <= n; i++) n     for (int j = 1; j <= m; j++) n       if (!g[i][j]) bx = i, by = j, dfs(i, j); n   cout << s << endl; n   for (int i = 1; i <= s; i++) printf( %c %d %d n , ansc[i], ansx[i], ansy[i]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MOD = 1000000007; n int n, c1, c2; n long long f[501]; n long long c[501][501]; n long long dp[501][501]; n long long gao(int a, int b) { n   if (a == 0 && b == 0) { n     return 1; n   } n   if (dp[a][b] == -1) { n     long long& cur = dp[a][b] = 0; n     if (a > 0) { n       if (a >= 1) { n         for (int i = 0; i <= b; ++i) { n           cur = (cur + gao(a - 1, b - i) * c[b][i] % MOD * f[i]) % MOD; n         } n       } n       if (a >= 2) { n         for (int i = 0; i <= b; ++i) { n           cur = (cur + n                  gao(a - 2, b - i) * c[b][i] % MOD * (a - 1) % MOD * f[i + 1]) % n                 MOD; n         } n       } n     } else { n       for (int i = 0; i < b; ++i) { n         cur = (cur + gao(a, b - i - 1) * c[b - 1][i] % MOD * f[i]) % MOD; n       } n     } n   } n   return dp[a][b] % MOD; n } n int main() { n   for (int i = f[0] = 1; i <= 500; ++i) { n     f[i] = f[i - 1] * i % MOD; n   } n   for (int i = 0; i <= 500; ++i) { n     c[i][0] = c[i][i] = 1; n     for (int j = 1; j < i; ++j) { n       c[i][j] = (c[i - 1][j - 1] + c[i - 1][j]) % MOD; n     } n   } n   c1 = c2 = 0; n   scanf( %d , &n); n   for (int i = 0; i < n; ++i) { n     int x; n     scanf( %d , &x); n     ++(x == 1 ? c1 : c2); n   } n   memset(dp, -1, sizeof(dp)); n   int res = gao(c1, c2) % MOD; n   printf( %d n , res); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T> n ostream& operator<<(ostream& out, const vector<T>& vec) { n   out <<  [ ; n   for (int x : vec) out << x <<  ,  ; n   out <<  ] ; n   return out; n } n int main() { n   int M; n   long long N, A, D; n   cin >> N >> M >> A >> D; n   vector<long long> T; n   for (int m = 0; m < M; ++m) { n     long long t; n     cin >> t; n     T.push_back(t); n   } n   T.push_back(N * A); n   ++M; n   sort(T.begin(), T.end()); n   long long last = -D - 1; n   long long ans = 0; n   for (int i = 0; i < M;) { n     while (i < M && last >= T[i]) { n       ++i; n     } n     if (i == M) { n       break; n     } n     last = max(last, 0LL); n     long long u = last / A + 1; n     if (u < N && u * A < T[i]) { n       long long v = D / A + 1; n       long long w = ((T[i] - 1) / A - u) / v + 1; n       last = A * (u + v * (w - 1)) + D; n       ans += w; n     } n     if (last < T[i]) { n       last = T[i] + D; n       ++ans; n     } n   } n   cout << ans <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n const long long M = 1000 * 1000 * 1000 + 7; n const int INF = 0x3f3f3f3f; n const int MAX = 200009; n long long n, f1, f2, f3, c; n vector<vector<long long> > operator*(const vector<vector<long long> > &a, n                                      const vector<vector<long long> > &b) { n   vector<vector<long long> > t(3, vector<long long>(3, 0)); n   for (int i = 0; i < 3; i++) n     for (int j = 0; j < 3; j++) n       for (int k = 0; k < 3; k++) n         t[i][j] = (t[i][j] + a[i][k] * b[k][j]) % (M - 1); n   return t; n } n vector<vector<long long> > mat_pow(vector<vector<long long> > a, long long b) { n   vector<vector<long long> > c(3, vector<long long>(3, 0)); n   for (int i = 0; i < 3; i++) c[i][i] = 1; n   while (b) { n     if (b & 1) c = c * a; n     a = a * a; n     b >>= 1; n   } n   return c; n } n long long ll_pow(long long a, long long b) { n   long long res = 1; n   while (b) { n     if (b & 1) res = (res * a) % M; n     a = (a * a) % M; n     b >>= 1; n   } n   return res; n } n int main() { n   while (cin >> n >> f1 >> f2 >> f3 >> c) { n     vector<vector<long long> > a{{0, 1, 0}, {0, 0, 1}, {1, 1, 1}}; n     a = mat_pow(a, n - 3); n     long long ans = ll_pow(f1 * c % M, a[2][0]) * n                     ll_pow(f2 * c % M * c % M, a[2][1]) % M * n                     ll_pow(f3 * c % M * c % M * c % M, a[2][2]) % M * n                     ll_pow(ll_pow(c, n), M - 2) % M; n     cout << ans << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   cin >> n; n   int count = 0; n   int a[(int)(pow(2.0, (double)(n + 1)) - 2)]; n   int x = (int)pow(2.0, (double)(n + 1)) - 2; n   for (int i = 0; i < x; i++) { n     cin >> a[i]; n   } n   int y = (int)(pow(2.0, (double)(n + 1)) - pow(2.0, (double)n)); n   int b[y]; n   for (int i = 0; i < y; i++) b[i] = 0; n   for (int i = 0; i < y; i++) { n     for (int j = 0; j < n; j = j + 1) n       b[i] = b[i] + n              a[(int)((pow(2.0, (double)n) + i) / (pow(2.0, (double)j))) - 2]; n   } n   for (int j = 1; j <= n; j++) n     for (int i = y - 1; i >= 0; i = i - (int)pow(2.0, (double)j)) { n       count = count + abs(b[i] - b[i - (int)pow(2.0, (double)(j - 1))]); n       if (b[i] > b[i - (int)pow(2.0, (double)(j - 1))]) n         b[i - (int)pow(2.0, (double)(j - 1))] = b[i]; n       else n         b[i] = b[i - (int)pow(2.0, (double)(j - 1))]; n     } n   cout << count << endl; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 2000 + 4; n char s[maxn]; n int a[maxn]; n void get(bitset<maxn> &B) { n   scanf( %s , s); n   int m = strlen(s); n   int x = 12; n   for (int i = 0; i < m; i++) a[m - 1 - i] = s[i] -  0 ; n   m--; n   for (int i = 0; i < maxn && x >= 0; i++) { n     int rem = 0; n     for (int j = m; j >= 0; j--) a[j] += rem * 10, rem = (a[j] & 1), a[j] >>= 1; n     B[i] = rem; n     while (m >= 0 && !a[m]) m--; n     x = rand() + m; n   } n } n int pos[maxn]; n bitset<maxn> b[maxn], st[maxn]; n int main() { n   if (fopen( in ,  r )) { n     freopen( in ,  r , stdin); n     freopen( out ,  w , stdout); n   } n   memset(pos, -1, sizeof(pos)); n   int n; n   scanf( %d , &n); n   for (int i = 0; i < n; i++) { n     get(b[i]); n     st[i][i] = 1; n     bool ok = 1; n     for (int j = 0; j < maxn; j++) n       if (b[i][j]) { n         if (pos[j] == -1) { n           ok = 0; n           pos[j] = i; n           break; n         } n         b[i] ^= b[pos[j]]; n         st[i] ^= st[pos[j]]; n       } n     if (!ok) { n       printf( 0 n ); n       continue; n     } n     st[i][i] = 0; n     printf( %d , st[i].count()); n     for (int j = 0; j < maxn; j++) n       if (st[i][j]) printf(  %d , j); n     printf(  n ); n   } n } n


#include <bits/stdc++.h> n long long mpow(long long a, long long n, long long mod) { n   long long ret = 1; n   long long b = a; n   while (n) { n     if (n & 1) ret = (ret * b) % mod; n     b = (b * b) % mod; n     n >>= 1; n   } n   return (long long)ret; n } n using namespace std; n int main() { n   cin.sync_with_stdio(0); n   int i, j, n, m, k, s; n   cin >> n >> m >> k >> s; n   int a[4][10]; n   for (j = 0; j < 10; j++) a[0][j] = -1e9, a[2][j] = -1e9; n   for (j = 0; j < 10; j++) a[1][j] = 1e9, a[3][j] = 1e9; n   for (i = 0; i < n; i++) n     for (j = 0; j < m; j++) { n       long long p; n       cin >> p; n       a[0][p] = max(a[0][p], i + j); n       a[1][p] = min(a[1][p], i + j); n       a[2][p] = max(a[2][p], i - j); n       a[3][p] = min(a[3][p], i - j); n     } n   int p; n   cin >> p; n   int ans = -1; n   for (j = 1; j < s; j++) { n     int q; n     cin >> q; n     ans = max(ans, a[0][p] - a[1][q]); n     ans = max(ans, a[0][q] - a[1][p]); n     ans = max(ans, a[2][p] - a[3][q]); n     ans = max(ans, a[2][q] - a[3][p]); n     p = q; n   } n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 4e5 + 400, LOGN = 19; n int n, q, a[N], dpLCA[LOGN][N], lca[N], kraw[N], dist[N]; n vector<pair<int, int> > G[N]; n long long dp[N], dp2[N], tree[N], odl[N], wynik[N], pom[N]; n vector<int> zapytania[N]; n void insert(int v, long long x) { n   while (v <= n) { n     tree[v] += x; n     v += (v & (-v)); n   } n } n long long query(int v) { n   long long wynik = 0; n   while (v > 0) { n     wynik += tree[v]; n     v -= (v & (-v)); n   } n   return wynik; n } n long long pytaj(int a, int b) { n   if (a > b) return 0; n   return query(b) - query(a - 1); n } n void preDFS(int u) { n   dp[u] = a[u]; n   for (auto v : G[u]) { n     if (v.first != dpLCA[0][u]) { n       dpLCA[0][v.first] = u; n       dist[v.first] = dist[u] + 1; n       odl[v.first] = odl[u] + v.second; n       kraw[v.first] = v.second; n       preDFS(v.first); n       pom[v.first] = max(0LL, dp[v.first] - 2 * v.second); n       dp[u] += pom[v.first]; n     } n   } n } n int LCA(int a, int b) { n   if (dist[a] < dist[b]) swap(a, b); n   int d = dist[a] - dist[b]; n   for (int i = 0; i < LOGN; i++) n     if (d & (1 << i)) a = dpLCA[i][a]; n   if (a == b) return a; n   for (int i = LOGN - 1; i >= 0; i--) { n     if (dpLCA[i][a] != dpLCA[i][b]) { n       a = dpLCA[i][a]; n       b = dpLCA[i][b]; n     } n   } n   return dpLCA[0][a]; n } n void preDP(int u) { n   int ojciec = dpLCA[0][u]; n   dp2[u] = dp[u] + (u != 1 ? max(0LL, dp[ojciec] - 2 * kraw[u]) : 0); n   long long blep = dp[u] - (u != 1 ? pom[u] : 0); n   insert(dist[u], blep); n   for (auto ID : zapytania[u]) wynik[ID] += pytaj(dist[lca[ID]], dist[u]); n   for (auto v : G[u]) { n     if (v.first != dpLCA[0][u]) { n       long long pomek = dp[u]; n       dp[u] = dp2[u] - pom[v.first]; n       preDP(v.first); n       dp[u] = pomek; n     } n   } n   insert(dist[u], -blep); n } n int32_t main() { n   scanf( %d%d , &n, &q); n   for (int i = 1; i <= n; i++) scanf( %d , &a[i]); n   for (int i = 1; i < n; i++) { n     int a, b, c; n     scanf( %d%d%d , &a, &b, &c); n     G[a].push_back({b, c}); n     G[b].push_back({a, c}); n   } n   dpLCA[0][1] = 1; n   dist[1] = 1; n   preDFS(1); n   for (int i = 1; i < LOGN; i++) n     for (int j = 1; j <= n; j++) dpLCA[i][j] = dpLCA[i - 1][dpLCA[i - 1][j]]; n   for (int i = 1; i <= q; i++) { n     int a, b; n     scanf( %d%d , &a, &b); n     lca[i] = LCA(a, b); n     wynik[i] = -(odl[a] + odl[b] - 2 * odl[lca[i]]); n     zapytania[a].push_back(i); n     zapytania[b].push_back(i); n   } n   preDP(1); n   for (int i = 1; i <= q; i++) { n     int u = lca[i]; n     int ojciec = dpLCA[0][u]; n     wynik[i] = wynik[i] - dp[u]; n     long long pomek = dp[ojciec]; n     dp[ojciec] = dp2[ojciec] - pom[u]; n     printf( %lld n , n            wynik[i] + n                (u != 1 ? max(0LL, dp[ojciec] - 2 * kraw[u]) + 2 * pom[u] : 0)); n     dp[ojciec] = pomek; n   } n   getchar(); n   getchar(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int dx[8] = {1, 0, -1, 0, -1, -1, 1, 1}; n int dy[8] = {0, 1, 0, -1, -1, 1, -1, 1}; n int main() { n   int tc = 1; n   cin >> tc; n   for (int cs = 1; cs <= tc; cs++) { n     string s; n     cin >> s; n     vector<int> v; n     for (int i = (int)s.size() - 3; i >= 0; i--) { n       if (s[i] ==  o  && s[i + 1] ==  n  && s[i + 2] ==  e ) { n         if (i > 0 && s[i - 1] ==  o ) { n           s[i + 1] =  x ; n           v.push_back(i + 2); n         } else { n           s[i] =  x ; n           v.push_back(i + 1); n         } n       } else if (s[i] ==  t  && s[i + 1] ==  w  && s[i + 2] ==  o ) { n         if (i > 0 && s[i - 1] ==  t ) { n           s[i + 1] =  x ; n           v.push_back(i + 2); n         } else { n           s[i] =  x ; n           v.push_back(i + 1); n         } n       } n     } n     cout << v.size() <<   n ; n     for (int i = 0; i < v.size(); i++) { n       cout << v[i] <<    ; n     } n     cout <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n #pragma GCC optimize( O3 ) n #pragma GCC target( sse4 ) n using namespace std; n const long long MAX = -1000000000000000000; n const long long MIN = 1000000000000000000; n const long long inf = 1000000000; n const long long KOK = 100000; n const long long LOG = 30; n const long long li = 500005; n const long long mod = 1000000007; n int n, m, b[li], a[li], k, flag, t, kuc[li], buy[li]; n int cev; n string s; n vector<int> v; n int main(void) { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   for (int i = 0; i <= li - 5; i++) { n     buy[i] = inf; n     kuc[i] = inf; n   } n   cin >> n >> s; n   int m = s.size(); n   if (m % n != 0) { n     string ss{}; n     string sss{}; n     int at = 0; n     ss +=  1 ; n     for (int i = 1; i < n; i++) ss +=  0 ; n     while (at < m) { n       at += n; n       sss += ss; n     } n     cout << sss << endl; n     return 0; n   } n   int cev1 = 0; n   for (int i = (int)s.size() - 1; i >= 0; i--) { n     if (s[i] < s[i % n]) { n       cev++; n       kuc[i % n] = i; n     } n     if (s[i] > s[i % n]) { n       buy[i % n] = i; n     } n     if (s[i] == s[i % n]) cev1++; n   } n   for (int i = 0; i < n; i++) { n     if (buy[i] < kuc[i]) n       break; n     else if (buy[i] > kuc[i]) { n       string ss{}; n       string sss{}; n       for (int i = 0; i < n; i++) ss += s[i]; n       int at = 0; n       while (at < m) { n         at += n; n         sss += ss; n       } n       cout << sss << endl; n       return 0; n     } n   } n   if (cev + cev1 == m && cev1 != m) { n     string ss{}; n     string sss{}; n     for (int i = 0; i < n; i++) ss += s[i]; n     int at = 0; n     while (at < m) { n       at += n; n       sss += ss; n     } n     cout << sss << endl; n     return 0; n   } n   string ss{}; n   string sss{}; n   for (int i = 0; i < n - 1; i++) ss += s[i]; n   if (s[n - 1] -  0  + 1 >= 10) flag = 1; n   ss += (s[n - 1] -  0  + 1) % 10 +  0 ; n   int yer = n - 2; n   while (flag == 1) { n     flag = 0; n     if (yer < 0) { n       flag = 1; n       break; n     } n     if (ss[yer] -  0  + 1 >= 10) flag = 1; n     ss[yer] = (ss[yer] -  0  + 1) % 10 +  0 ; n     yer--; n   } n   if (flag == 1) { n     string ss{}; n     string sss{}; n     int at = 0; n     ss +=  1 ; n     for (int i = 1; i < n; i++) ss +=  0 ; n     while (at <= (int)s.size()) { n       at += n; n       sss += ss; n     } n     cout << sss << endl; n     return 0; n   } n   int at = 0; n   while (at < m) { n     at += n; n     sss += ss; n   } n   cout << sss << endl; n   return 0; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 100005; n int a[MAXN], b[MAXN]; n int main() { n   int n, k; n   scanf( %d%d , &n, &k); n   for (int i = 0; i < n / k; ++i) { n     scanf( %d , &a[i]); n   } n   for (int i = 0; i < n / k; ++i) { n     scanf( %d , &b[i]); n   } n   long long ans = 1; n   int mx = 10; n   for (int i = 1; i < k; ++i) { n     mx *= 10; n   } n   int mn = mx / 10; n   --mx; n   for (int i = 0; i < n / k; ++i) { n     int cur = mx / a[i]; n     int l = b[i] * mn, r = (b[i] + 1) * mn - 1; n     if (r >= l) { n       cur -= r / a[i]; n       if (l > 0) cur += (l - 1) / a[i]; n     } n     if (b[i] != 0) ++cur; n     ans = (ans * (long long)cur) % 1000000007; n   } n   printf( %I64d , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int inft = 1000000009; n const int MAXN = 1000006; n const int T = 1024 * 1024; n int drz[2][2 * T]; n vector<int> X; n pair<int, int> Q[MAXN]; n char C[MAXN]; n bool used[MAXN]; n int ask(int a, int nr) { n   int ans = -1; n   a += T; n   while (a) { n     ans = max(ans, drz[nr][a]); n     a /= 2; n   } n   return ans; n } n void add(int a, int b, int value, int nr) { n   a += T; n   b += T; n   while (a <= b) { n     if (a % 2 == 1) { n       drz[nr][a] = max(drz[nr][a], value); n       a++; n     } n     if (b % 2 == 0) { n       drz[nr][b] = max(drz[nr][b], value); n       b--; n     } n     a /= 2; n     b /= 2; n   } n } n void solve() { n   int n, m; n   scanf( %d%d , &n, &m); n   X.push_back(0); n   X.push_back(n + 1); n   for (int i = 0; i < (m); ++i) { n     int a, b; n     char c; n     scanf( %d%d %c , &a, &b, &c); n     X.push_back(a); n     X.push_back(b); n     Q[i] = pair<int, int>(a, b); n     C[i] = c; n   } n   sort((X).begin(), (X).end()); n   X.resize(unique((X).begin(), (X).end()) - X.begin()); n   for (int j = 0; j < (2); ++j) n     for (int i = 0; i < (2 * T); ++i) drz[j][i] = -1; n   add(0, T - 1, 0, 0); n   add(0, T - 1, 0, 1); n   for (int i = 0; i < (m); ++i) { n     int aa = lower_bound((X).begin(), (X).end(), Q[i].first) - X.begin(); n     if (used[aa]) { n       printf( 0 n ); n       continue; n     } n     used[aa] = 1; n     if (C[i] ==  L ) { n       int nr = lower_bound((X).begin(), (X).end(), Q[i].second) - X.begin(); n       int ans = ask(nr, 0); n       printf( %d n , Q[i].first - X[ans]); n       add(ans, lower_bound((X).begin(), (X).end(), Q[i].first) - X.begin(), nr, n           1); n     } else { n       int nr = lower_bound((X).begin(), (X).end(), Q[i].first) - X.begin(); n       int ans = ask(nr, 1); n       printf( %d n , Q[i].second - X[ans]); n       add(ans, lower_bound((X).begin(), (X).end(), Q[i].second) - X.begin(), nr, n           0); n     } n   } n } n int main() { n   int t = 1; n   for (int i = 0; i < (t); ++i) solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n bool compare(long long &x, long long &y) { return x >= y; } n long long mode = pow(10, 9) + 7; n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   long long n; n   cin >> n; n   long long a[n + 1]; n   a[0] = 0; n   for (int i = 1; i <= n; i++) { n     cin >> a[i]; n   } n   long long ans = 0; n   for (int i = 1; i <= n; i++) { n     ans = ans + abs(a[i] - a[i - 1]) + 2; n   } n   cout << ans - 1 <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int INF = 0x3f3f3f3f; n const long long int LINF = 0x3f3f3f3f3f3f3f; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   int k; n   cin >> k; n   string second, t; n   int freq[130]; n   while (k--) { n     memset(freq, 0, sizeof(freq)); n     int n; n     vector<pair<int, int> > ans; n     cin >> n; n     cin >> second >> t; n     for (int i = 0; i < n; i++) { n       freq[second[i]]++; n       freq[t[i]]++; n     } n     for (int i = 0; i < 130; i++) { n       if (freq[i] % 2) { n         cout <<  No  n              <<   n ; n         goto fim; n       } n     } n     for (int i = 0; i < n; i++) { n       if (second[i] == t[i]) continue; n       int opa = 0; n       for (int j = i + 1; j < n; j++) n         if (second[j] == second[i]) { n           swap(second[j], t[i]); n           ans.push_back({j, i}); n           opa = 1; n           break; n         } n       if (opa) continue; n       for (int j = i + 1; j < n; j++) { n         if (second[i] == t[j]) { n           swap(second[j], t[j]); n           ans.push_back({j, j}); n           swap(t[i], second[j]); n           ans.push_back({j, i}); n           break; n         } n       } n     } n     cout <<  Yes  n          <<   n ; n     cout << ans.size() <<   n ; n     for (auto a : ans) cout << a.first + 1 <<     << a.second + 1 << endl; n   fim: n     continue; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e5 + 10; n int main() { n   ios::sync_with_stdio(false); n   int n; n   while (cin >> n) { n     int a, b, c, d, sum = 0, i = 0, mx = -1, my = -1, mix = N, miy = N; n     for (i = 0; i < n; i++) { n       cin >> a >> b >> c >> d; n       sum += abs(a - c) * abs(d - b); n       mx = max(mx, max(a, c)); n       my = max(my, max(d, b)); n       mix = min(mix, min(a, c)); n       miy = min(miy, min(d, b)); n     } n     if (((mx - mix) != (my - miy)) || (mx - mix) * (my - miy) != sum) n       cout <<  NO  << endl; n     else n       cout <<  YES  << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long double eps = 1e-12; n pair<long long, long long> save[200005]; n long long cnt[50]; n int main(void) { n   long long n; n   cin >> n; n   if (n & 1) { n     char ch; n     while (cin >> ch) n       ; n     cout <<  no ; n     return 0; n   } n   for (long long i = 1; i <= n; i++) { n     cin >> save[i].first >> save[i].second; n   } n   long long half = n / 2; n   long double x0 = (save[1].first + save[half + 1].first) / 2.0; n   long double y0 = (save[1].second + save[half + 1].second) / 2.0; n   for (int i = 2; i <= half; i++) { n     if (fabs((save[i].first + save[half + i].first) / 2.0 - x0) < eps && n         fabs((save[i].second + save[half + i].second) / 2.0 - y0) < eps) { n       ; n     } else { n       cout <<  no ; n       return 0; n     } n   } n   cout <<  yes ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 51; n const long long MOD = 1e9 + 7; n const double eps = 1e-8; n int n, a[N], b[N]; n vector<pair<int, int> > v; n double dp[N][N][N], thresh; n bool vis[N][N][N]; n double calc(int i, int g, int e) { n   if (i == n) return 0.0; n   double &r = dp[i][g][e]; n   if (vis[i][g][e]) return r; n   vis[i][g][e] = 1; n   int g2 = g, e2 = e; n   if (i && v[i].first != v[i - 1].first) { n     g2 += e2; n     e2 = 0; n   } n   r = calc(i + 1, g2, e2 + 1) + v[i].first - thresh * v[i].second; n   if (g2) r = min(r, calc(i + 1, g2 - 1, e2)); n   return r; n } n bool check(double m) { n   memset(vis, 0, sizeof vis); n   thresh = m; n   double r = calc(0, 0, 0); n   return !(r > eps); n } n int main() { n   scanf( %d , &n); n   for (int i = 0; i < n; i++) scanf( %d , a + i); n   for (int i = 0; i < n; i++) { n     scanf( %d , b + i); n     v.push_back(make_pair(a[i], b[i])); n   } n   sort(v.begin(), v.end()); n   reverse(v.begin(), v.end()); n   double l = 0, r = 5e9 + 1, m, ans = 0.0; n   int it = 80; n   while (l < r && it) { n     m = (l + r) / 2; n     it--; n     if (check(m)) { n       ans = m; n       r = m; n     } else n       l = m; n   } n   printf( %.0lf n , ceil(ans * 1000)); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int a[100004], d[32]; n int main() { n   int n; n   cin >> n; n   for (int i = 0; i < n; i++) { n     cin >> a[i]; n   } n   for (int j = 0; j < n; j++) { n     int x = a[j]; n     for (int i = 0; x > 0; i++) { n       d[i] += x % 2; n       x /= 2; n     } n   } n   int p = 1, e = 0; n   for (int i = 0; i < 32; i++) { n     if (d[i] == 1) { n       e += p; n     } n     p *= 2; n   } n   int c = -1, m = 0; n   for (int i = 0; i < n; i++) { n     int x = a[i] & e; n     if (x > m) { n       m = x; n       c = i; n     } n   } n   if (c != -1) { n     cout << a[c] <<    ; n   } n   for (int j = 0; j < n; j++) { n     if (c != j) cout << a[j] <<    ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m; n const int mxN = 2e5 + 10; n vector<int> arr(mxN); n vector<pair<int, int> > offers(mxN); n bool isPossible(int day) { n   vector<int> lst(n, -1); n   for (int i = 0; i < m; i++) { n     if (offers[i].first <= day) { n       lst[offers[i].second] = max(lst[offers[i].second], offers[i].first); n     } n   } n   vector<vector<int> > off(2 * mxN); n   for (int i = 0; i < n; i++) { n     if (lst[i] != -1) { n       off[lst[i]].push_back(i); n     } n   } n   int curr_money = 0; n   vector<int> need = arr; n   for (int i = 0; i <= day; i++) { n     ++curr_money; n     if (i > mxN || off[i].size() == 0) { n       continue; n     } n     for (auto it : off[i]) { n       if (curr_money >= need[it]) { n         curr_money -= need[it]; n         need[it] = 0; n       } else { n         need[it] -= curr_money; n         curr_money = 0; n         break; n       } n     } n   } n   return accumulate(need.begin(), need.end(), 0) * 2 <= curr_money; n } n long long int binarySearch(int low, int high) { n   int answer; n   while (high >= low) { n     int mid = (low + high) / 2; n     if (isPossible(mid)) { n       answer = mid; n       high = mid - 1; n     } else { n       low = mid + 1; n     } n   } n   return answer; n } n int main() { n   cin >> n >> m; n   for (int i = 0; i < n; i++) { n     cin >> arr[i]; n   } n   for (int i = 0; i < m; i++) { n     cin >> offers[i].first >> offers[i].second; n     offers[i].first--; n     offers[i].second--; n   } n   cout << binarySearch(0, 2 * mxN) + 1 <<   n ; n   return 0; n } n