#include <bits/stdc++.h> n using namespace std; n void setIO(const string &name =   ) { n   ios::sync_with_stdio(false); n   cin.tie(nullptr); n   cout.tie(nullptr); n   if (name.length()) { n     freopen((name +  .in ).c_str(),  r , stdin); n     freopen((name +  .out ).c_str(),  w , stdout); n   } n } n template <typename T> n void read(vector<T> &a) { n   for (auto &x : a) cin >> x; n } n template <typename T> n void read(vector<T> &a, int n) { n   a.resize(n); n   for (auto &x : a) cin >> x; n } n template <class T, class U> n ostream &operator<<(ostream &out, const pair<T, U> &v) { n   out <<  ( ; n   out << v.first <<  ,  << v.second; n   return out <<  ) ; n } n template <class T> n ostream &operator<<(ostream &out, const vector<T> &v) { n   out <<  [ ; n   for (auto(i) = 0; (i) < ((int)(v).size()); (i)++) { n     if (i) out <<  ,  ; n     out << v[i]; n   } n   return out <<  ] ; n } n template <typename T> n void print(vector<T> &a) { n   for (const auto &x : a) cout << x <<    ; n   cout <<   n ; n } n void MOD(int &x, int m = 1000000007) { n   x %= m; n   if (x < 0) x += m; n } n template <typename T> n void dbg(const char *name, T &&arg1) { n   cout << name <<   :   << arg1 <<   n ; n } n template <typename T, typename... U> n void dbg(const char *names, T &&arg1, U &&...args) { n   const char *comma = strchr(names + 1,  , ); n   cout.write(names, comma - names) <<   :   << arg1 <<   |  ; n   dbg(comma + 1, args...); n } n template <class T> n void read(T &x) { n   cin >> x; n } n template <class T, class... U> n void read(T &t, U &...u) { n   read(t); n   read(u...); n } n int gcd(int a, int b) { return !a ? b : gcd(b % a, a); } n struct SegTree { n   int size = 1; n   vector<int> elements, a; n   vector<bool> lazy; n   void init(int n) { n     while (size < n) size *= 2; n     elements.assign(2 * size, 0); n     lazy.assign(2 * size, false); n   } n   void push(int x, int lx, int rx) { n     if (!lazy[x]) return; n     lazy[x] = false; n     lazy[2 * x + 1] = !lazy[2 * x + 1]; n     lazy[2 * x + 2] = !lazy[2 * x + 2]; n     int mid = (lx + rx) / 2; n     elements[2 * x + 1] = (mid - lx) - elements[2 * x + 1]; n     elements[2 * x + 2] = (rx - mid) - elements[2 * x + 2]; n   } n   void pull(int x) { elements[x] = elements[2 * x + 1] + elements[2 * x + 2]; } n   void build(int x, int lx, int rx) { n     if (rx - lx == 1) { n       if (lx < (int)(a).size()) { n         elements[x] = a[lx]; n       } n       return; n     } n     int mid = (lx + rx) / 2; n     build(2 * x + 1, lx, mid); n     build(2 * x + 2, mid, rx); n     pull(x); n   } n   void build(vector<int> &x) { n     a = x; n     build(0, 0, size); n   } n   void flip(int l, int r, int x, int lx, int rx) { n     if (l >= rx or r <= lx) return; n     if (l <= lx and rx <= r) { n       lazy[x] = !lazy[x]; n       elements[x] = (rx - lx) - elements[x]; n       return; n     } n     push(x, lx, rx); n     int mid = (lx + rx) / 2; n     flip(l, r, 2 * x + 1, lx, mid); n     flip(l, r, 2 * x + 2, mid, rx); n     pull(x); n   } n   void flip(int l, int r) { flip(l, r, 0, 0, size); } n   int get(int l, int r, int x, int lx, int rx) { n     if (l >= rx or r <= lx) return 0; n     if (l <= lx and rx <= r) { n       return elements[x]; n     } n     push(x, lx, rx); n     int mid = (lx + rx) / 2; n     int res = get(l, r, 2 * x + 1, lx, mid); n     res += get(l, r, 2 * x + 2, mid, rx); n     return res; n   } n   int get(int l, int r) { return get(l, r, 0, 0, size); } n }; n int n, q; n vector<int> adj[int(2e5 + 10)], p; n vector<int> tin, tout, ar, st_ar; n int tt = -1; n void dfs(int x, int par = -1) { n   tt++; n   ar.emplace_back(x); n   tin[x] = tt; n   for (auto &(c) : (adj[x])) { n     if (c == par) continue; n     dfs(c, x); n   } n   tout[x] = tt; n } n void euler_tour() { n   tin.resize(n); n   tout.resize(n); n   ar.reserve(n); n   dfs(0); n   st_ar.resize(n); n   for (auto(i) = 0; (i) < (n); (i)++) { n     st_ar[i] = p[ar[i]]; n   } n } n int32_t main() { n   setIO(); n   read(n); n   int x; n   for (auto(i) = 1; (i) < (n); (i)++) { n     cin >> x; n     x--; n     adj[x].emplace_back(i); n     adj[i].emplace_back(x); n   } n   read(p, n); n   euler_tour(); n   SegTree st; n   st.init(n); n   st.build(st_ar); n   cin >> q; n   string op; n   int v; n   while (q--) { n     read(op, v); n     v--; n     if (op ==  get ) { n       cout << st.get(tin[v], tout[v] + 1) <<   n ; n     } else { n       st.flip(tin[v], tout[v] + 1); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   int t; n   cin >> t; n   while (t--) { n     int n, m; n     cin >> n >> m; n     vector<vector<int>> a(n, vector<int>(m)); n     map<int, vector<int>> raws; n     for (int i = 0; i < n; i++) { n       for (int j = 0; j < m; j++) { n         cin >> a[i][j]; n       } n       raws[a[i][0]] = a[i]; n     } n     for (int j = 0; j < m; j++) { n       for (int i = 0; i < n; i++) { n         cin >> a[i][j]; n       } n       if (raws.find(a[0][j]) != raws.end()) { n         for (int i = 0; i < n; i++) { n           for (int x : raws[a[i][j]]) { n             cout << x <<    ; n           } n           cout <<   n ; n         } n       } n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int inf = 1000000007; n const long long linf = 1LL << 62; n const unsigned long long ulinf = 1ULL << 63; n const double eps = 0.000001; n const double pi = 3.14159265358979323846; n template <class T> n T abs(T a) { n   return a >= 0 ? a : -a; n } n template <class T> n T sqr(T a) { n   return a * a; n } n template <class T> n T gcd(T a, T b) { n   return b ? gcd(b, a % b) : a; n } n template <class T> n T mod(T a, T b) { n   return (a % b + b) % b; n } n template <class T> n T addmod(T a, T b, T c) { n   return (T)mod((long long)a + b, (long long)c); n } n template <class T> n T mulmod(T a, T b, T c) { n   return (T)mod((long long)a * b, (long long)c); n } n template <class T> n T powmod(T a, T b, T c) { n   return (T)mod( n       b ? mulmod(mod(sqr((long long)powmod(a, b >> 1, c)), (long long)c), n                  (b & 1LL) ? a : 1LL, (long long)c) n         : 1LL, n       (long long)c); n } n template <class T> n void maxe(T &a, T b) { n   a = max(a, b); n } n template <class T> n void mine(T &a, T b) { n   a = min(a, b); n } n template <class T> n void mode(T &a, T b) { n   a = mod(a, b); n } n template <class T> n void addmode(T &a, T b, T c) { n   a = addmod(a, b, c); n } n template <class T> n void mulmode(T &a, T b, T c) { n   a = mulmod(a, b, c); n } n template <class T> n void powmode(T &a, T b, T c) { n   a = powmod(a, b, c); n } n int iszero(double a) { return abs(a) <= eps; } n template <class T> n void geti(T &a) { n   a = 0; n   int b = 1, c = getchar(); n   if (c ==  - ) n     b = -1; n   else n     a = c - 48; n   while ((c = getchar()) !=     && c !=   n ) a = a * 10 + c - 48; n   a *= b; n } n void fileio_in_out() { n   freopen( .in ,  r , stdin); n   freopen( .out ,  w , stdout); n } n void fileio_txt() { n   freopen( input.txt ,  r , stdin); n   freopen( output.txt ,  w , stdout); n } n const int N = 1111; n int test; n int n, m, k, ans; n char s[N][N]; n void work100() { n   s[0][0] =  > ; n   for (int i = 1; i < 100; i++) s[i][0] =  ^ ; n   for (int i = 0; i < 100; i += 2) { n     for (int j = 1; j < 49; j++) s[i][j] =  > ; n     for (int j = 49; j < 98; j += 2) { n       s[i][j] =  > ; n       s[i][j + 1] =  . ; n     } n     s[i][99] =  v ; n   } n   for (int i = 1; i < 100; i += 2) { n     for (int j = 1; j < 49; j++) s[i][100 - j] =  < ; n     for (int j = 49; j < 98; j += 2) { n       s[i][100 - j] =  < ; n       s[i][99 - j] =  . ; n     } n     s[i][1] =  v ; n   } n   s[99][1] =  . ; n   for (int i = 0; i < 100; i++) printf( %s n , s[i]); n   puts( 1 1 ); n } n int main() { n   scanf( %d%d , &n, &m); n   if (n == 3 || n == 5) { n     if (n == 3) n       printf( >vv n^<. n^.< n1 3 n ); n     else n       printf( >...v nv.<.. n..^.. n>.... n..^.< n1 1 n ); n   } else n     work100(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n using ll = long long; n template <class t, class u> n void chmax(t& first, u second) { n   if (first < second) first = second; n } n template <class t, class u> n void chmin(t& first, u second) { n   if (second < first) first = second; n } n template <class t> n using vc = vector<t>; n template <class t> n using vvc = vc<vc<t>>; n using pi = pair<int, int>; n using vi = vc<int>; n template <class t, class u> n ostream& operator<<(ostream& os, const pair<t, u>& p) { n   return os <<  {  << p.first <<  ,  << p.second <<  } ; n } n template <class t> n ostream& operator<<(ostream& os, const vc<t>& v) { n   os <<  { ; n   for (auto e : v) os << e <<  , ; n   return os <<  } ; n } n using uint = unsigned; n using ull = unsigned long long; n template <int i, class T> n void print_tuple(ostream&, const T&) {} n template <int i, class T, class H, class... Args> n void print_tuple(ostream& os, const T& t) { n   if (i) os <<  , ; n   os << get<i>(t); n   print_tuple<i + 1, T, Args...>(os, t); n } n template <class... Args> n ostream& operator<<(ostream& os, const tuple<Args...>& t) { n   os <<  { ; n   print_tuple<0, tuple<Args...>, Args...>(os, t); n   return os <<  } ; n } n void print(ll x, int suc = 1) { n   cout << x; n   if (suc == 1) cout <<   n ; n   if (suc == 2) cout <<    ; n } n ll read() { n   ll i; n   cin >> i; n   return i; n } n vi readvi(int n, int off = 0) { n   vi v(n); n   for (int i = int(0); i < int(n); i++) v[i] = read() + off; n   return v; n } n template <class T> n void print(const vector<T>& v, int suc = 1) { n   for (int i = int(0); i < int(v.size()); i++) n     print(v[i], i == int(v.size()) - 1 ? suc : 2); n } n string readString() { n   string s; n   cin >> s; n   return s; n } n template <class T> n T sq(const T& t) { n   return t * t; n } n void yes(bool ex = true) { n   cout <<  Yes  << endl; n   if (ex) exit(0); n } n void no(bool ex = true) { n   cout <<  No  << endl; n   if (ex) exit(0); n } n constexpr ll ten(int n) { return n == 0 ? 1 : ten(n - 1) * 10; } n const ll infLL = LLONG_MAX / 3; n const int inf = INT_MAX / 2 - 100; n int topbit(signed t) { return t == 0 ? -1 : 31 - __builtin_clz(t); } n int topbit(ll t) { return t == 0 ? -1 : 63 - __builtin_clzll(t); } n int botbit(signed first) { return first == 0 ? 32 : __builtin_ctz(first); } n int botbit(ll first) { return first == 0 ? 64 : __builtin_ctzll(first); } n int popcount(signed t) { return __builtin_popcount(t); } n int popcount(ll t) { return __builtin_popcountll(t); } n bool ispow2(int i) { return i && (i & -i) == i; } n int mask(int i) { return (int(1) << i) - 1; } n bool inc(int first, int second, int c) { n   return first <= second && second <= c; n } n template <class t> n void mkuni(vc<t>& v) { n   sort(v.begin(), v.end()); n   v.erase(unique(v.begin(), v.end()), v.end()); n } n ll rand_int(ll l, ll r) { n   static random_device rd; n   static mt19937_64 gen(rd()); n   return uniform_int_distribution<ll>(l, r)(gen); n } n template <class t> n int lwb(const vc<t>& v, const t& first) { n   return lower_bound(v.begin(), v.end(), first) - v.begin(); n } n using uint = unsigned; n using ull = unsigned long long; n const uint mod = 998244353; n struct mint { n   uint v; n   mint(ll vv = 0) { s(vv % mod + mod); } n   mint& s(uint vv) { n     v = vv < mod ? vv : vv - mod; n     return *this; n   } n   mint operator-() const { return mint() - *this; } n   mint& operator+=(const mint& rhs) { return s(v + rhs.v); } n   mint& operator-=(const mint& rhs) { return s(v + mod - rhs.v); } n   mint& operator*=(const mint& rhs) { n     v = ull(v) * rhs.v % mod; n     return *this; n   } n   mint& operator/=(const mint& rhs) { return *this *= rhs.inv(); } n   mint operator+(const mint& rhs) const { return mint(*this) += rhs; } n   mint operator-(const mint& rhs) const { return mint(*this) -= rhs; } n   mint operator*(const mint& rhs) const { return mint(*this) *= rhs; } n   mint operator/(const mint& rhs) const { return mint(*this) /= rhs; } n   mint pow(int n) const { n     mint res(1), x(*this); n     while (n) { n       if (n & 1) res *= x; n       x *= x; n       n >>= 1; n     } n     return res; n   } n   mint inv() const { return pow(mod - 2); } n   friend ostream& operator<<(ostream& os, const mint& m) { return os << m.v; } n   bool operator<(const mint& r) const { return v < r.v; } n   bool operator==(const mint& r) const { return v == r.v; } n }; n const int vmax = (1 << 21) + 10; n mint fact[vmax], finv[vmax], invs[vmax]; n void initfact() { n   fact[0] = 1; n   for (int i = int(1); i < int(vmax); i++) { n     fact[i] = fact[i - 1] * i; n   } n   finv[vmax - 1] = fact[vmax - 1].inv(); n   for (int i = vmax - 2; i >= 0; i--) { n     finv[i] = finv[i + 1] * (i + 1); n   } n   for (int i = vmax - 1; i >= 1; i--) { n     invs[i] = finv[i] * fact[i - 1]; n   } n } n mint choose(int n, int k) { return fact[n] * finv[n - k] * finv[k]; } n mint binom(int first, int second) { n   return fact[first + second] * finv[first] * finv[second]; n } n mint catalan(int n) { n   return binom(n, n) - (n - 1 >= 0 ? binom(n - 1, n + 1) : 0); n } n const int nmax = 4010; n const int K = 40; n vi t[nmax]; n mint dp[nmax][K]; n vc<mint> buf; n mint work[K][K]; n void dfs(int v, int p) { n   for (auto c : t[v]) n     if (c != p) dfs(c, v); n   memset(work, 0, sizeof(work)); n   work[0][0] = 1; n   for (int us = int(0); us < int(K - 1); us++) { n     for (auto c : t[v]) n       if (c != p) { n         for (int i = int(0); i < int(K); i++) { n           for (int j = int(0); j < int(K - i - 1); j++) { n             work[us + 1][i + j + 1] += work[us][i] * dp[c][j]; n           } n         } n       } n   } n   buf[0] += 1; n   for (int us = int(0); us < int(K); us++) n     for (int i = int(0); i < int(K); i++) dp[v][i] += work[us][i]; n   for (int us = int(1); us < int(K); us++) { n     for (int i = int(1); i < int(K); i++) n       if (work[us][i].v) { n         for (int j = int(2); j < int(min((K - 1) / us, (K - 1) / i) + 1); j++) { n           work[us * j][i * j] -= work[us][i]; n         } n         for (int j = int(1); j < int(min((K - 1) / us, (K - 1) / i) + 1); j++) { n           buf[i * j] += work[us][i] * i * 2 * invs[us]; n         } n       } n   } n } n signed main() { n   cin.tie(0); n   ios::sync_with_stdio(0); n   cout << fixed << setprecision(20); n   initfact(); n   int n[2]; n   for (int i = int(0); i < int(2); i++) cin >> n[i]; n   int k; n   cin >> k; n   if (k % 2) { n     cout << 0 << endl; n     return 0; n   } n   k /= 2; n   vc<mint> x[2]; n   for (int w = int(0); w < int(2); w++) { n     for (int i = int(0); i < int(n[w]); i++) t[i].clear(); n     memset(dp, 0, sizeof(dp)); n     buf.assign(K, 0); n     for (int _ = int(0); _ < int(n[w] - 1); _++) { n       int first, second; n       cin >> first >> second; n       first--; n       second--; n       t[first].push_back(second); n       t[second].push_back(first); n     } n     dfs(0, -1); n     x[w] = buf; n   } n   void(0); n   void(0); n   mint ans = 0; n   for (int i = int(0); i < int(k + 1); i++) { n     ans += x[0][i] * x[1][k - i] * choose(k * 2, i * 2); n   } n   cout << ans << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   long long n, c = 0, ans; n   cin >> n; n   while (n) { n     if (n % 2) c++; n     n /= 2; n   } n   cout << c << endl; n } n


#include <bits/stdc++.h> n using namespace std; n void fre() { n   freopen( c://test//input.in ,  r , stdin); n   freopen( c://test//output.out ,  w , stdout); n } n template <class T1, class T2> n inline void gmax(T1 &a, T2 b) { n   if (b > a) a = b; n } n template <class T1, class T2> n inline void gmin(T1 &a, T2 b) { n   if (b < a) a = b; n } n const int N = 0, M = 0, Z = 1e9 + 7, ms63 = 0x3f3f3f3f; n long long n; n int main() { n   while (~scanf( %lld , &n)) { n     for (long long i = 3;; i *= 3) { n       if (n % i != 0) { n         long long ans = n / i + 1; n         printf( %lld n , ans); n         break; n       } n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void swap(int* a, int* b) { n   int t = *a; n   *a = *b; n   *b = t; n } n int partition(int arr[], int arr2[], int arr3[], int arr4[], int arr5[], n               int low, int high) { n   int pivot = arr[high]; n   int i = (low - 1); n   for (int j = low; j <= high - 1; j++) { n     if (arr[j] < pivot) { n       i++; n       swap(&arr[i], &arr[j]); n       swap(&arr2[i], &arr2[j]); n       swap(&arr3[i], &arr3[j]); n       swap(&arr4[i], &arr4[j]); n       swap(&arr5[i], &arr5[j]); n     } n   } n   swap(&arr[i + 1], &arr[high]); n   swap(&arr2[i + 1], &arr2[high]); n   swap(&arr3[i + 1], &arr3[high]); n   swap(&arr4[i + 1], &arr4[high]); n   swap(&arr5[i + 1], &arr5[high]); n   return (i + 1); n } n void quickSort(int arr[], int arr2[], int arr3[], int arr4[], int arr5[], n                int low, int high) { n   if (low < high) { n     int pi = partition(arr, arr2, arr3, arr4, arr5, low, high); n     quickSort(arr, arr2, arr3, arr4, arr5, low, pi - 1); n     quickSort(arr, arr2, arr3, arr4, arr5, pi + 1, high); n   } n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   int n; n   cin >> n; n   int a[n], b[n], c[n], cost[n], d[n]; n   for (int i = 0; i < n; i++) { n     cin >> a[i] >> b[i] >> c[i] >> cost[i]; n     d[i] = i + 1; n   } n   quickSort(cost, a, b, c, d, 0, n - 1); n   int index = -1; n   bool found = true; n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < n; j++) { n       if (a[i] < a[j] and b[i] < b[j] and c[i] < c[j]) { n         found = false; n         break; n       } n     } n     if (found) { n       index = d[i]; n       break; n     } n     found = true; n   } n   cout << index << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<long long> x; n vector<vector<long long> > y; n vector<long long> bfs(long long k) { n   vector<long long> p((long long)y.size() + 1, 0); n   vector<bool> vis((long long)y.size() + 1, false); n   vis[k] = true; n   queue<long long> q; n   q.push(k); n   while (!q.empty()) { n     long long k = q.front(); n     q.pop(); n     for (auto i : y[k]) { n       if (!vis[i]) { n         p[i] = p[k] + 1; n         vis[i] = true; n         q.push(i); n       } n     } n   } n   return p; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   long long n, m, k; n   cin >> n >> m >> k; n   y = vector<vector<long long> >(n + 1); n   for (long long i = 0; i < k; i++) { n     long long k; n     cin >> k; n     x.push_back(k); n   }; n   for (long long j = 0; j < m; j++) { n     long long p, q; n     cin >> p >> q; n     y[p].push_back(q); n     y[q].push_back(p); n   } n   vector<long long> o = bfs(1); n   vector<long long> l = bfs(n); n   vector<pair<long long, long long> > z; n   long long maxi = -1; n   for (auto i : x) { n     z.push_back({o[i] - l[i], l[i]}); n   } n   sort(z.begin(), z.end()); n   vector<long long> suff(k + 1, 0); n   long long ans = -1; n   for (long long j = k - 1; j >= 0; j--) { n     maxi = max(maxi, z[j].second); n     suff[j] = maxi; n   } n   for (long long j = 0; j < k - 1; j++) { n     ans = max(ans, suff[j + 1] + z[j].first + z[j].second + 1); n   } n   cout << min(ans, o[n]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long INF = 1e9; n const long long MAXN = 2e5 + 1; n void faster() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n } n int prs[1000]; n void solve() { n   int n, k; n   cin >> n >> k; n   unsigned long long a[n]; n   set<int> ms; n   int s0 = 0; n   for (int i = 0; i < n; i++) { n     cin >> a[i]; n   } n   int b[65], cnt = 0; n   for (int i = 0; i <= 60; i++) { n     b[i] = 0; n   } n   for (int i = 0; i < n; i++) { n     cnt = 0; n     while (a[i] >= 1) { n       if (a[i] % k == 0) { n         cnt++; n         a[i] /= k; n       } else if ((a[i] - 1) % k == 0 && a[i] != 1) { n         if (b[cnt] == 0) { n           b[cnt] = 1; n         } else { n           cout <<  NO  << endl; n           return; n         } n         a[i] -= 1; n       } else { n         if (a[i] == 1 && b[cnt] == 0) { n           b[cnt] = 1; n           break; n         } else { n           cout <<  NO  << endl; n           return; n         } n       } n     } n   } n   cout <<  YES  << endl; n } n int main() { n   int n = 1; n   cin >> n; n   while (n--) { n     solve(); n   } n } n


#include <bits/stdc++.h> n using namespace std; n template <class T> n struct range_leq { n   int N; n   vector<vector<T> > fen; n   range_leq(int *A, int n) : N(n + 1), fen(N) { n     for (int i = 1; i < N; ++i) { n       int p = 1; n       for (; !(p & i); p <<= 1) n         ; n       for (; p; --p) fen[i].push_back(A[i - p]); n       sort(fen[i].begin(), fen[i].end()); n     } n   } n   inline int prefix_query(int i, T v) { n     int ans = 0; n     for (++i; i; i ^= i & -i) { n       int lo = -1, hi = fen[i].size(); n       while (hi - lo != 1) { n         int md = (lo + hi) / 2; n         (fen[i][md] <= v ? lo : hi) = md; n       } n       ans += hi; n     } n     return ans; n   } n   inline int range_query(int l, int r, int v) { n     return prefix_query(r, v) - prefix_query(l - 1, v); n   } n   inline int box_query(int l, int r, int w, int v) { n     return range_query(l, r, v) - range_query(l, r, w - 1); n   } n }; n const int N = 1e5 + 5; n int n, k, a[N], q, l, r, lk[N]; n map<int, vector<int> > m; n int main() { n   scanf( %d%d , &n, &k); n   for (int i = 0; i < n; ++i) { n     scanf( %d , a + i); n     m[a[i]].push_back(i); n   } n   for (auto p : m) { n     vector<int> &v = p.second; n     for (int i = 0; i < v.size(); ++i) { n       if (i < k) n         lk[v[i]] = -1; n       else n         lk[v[i]] = v[i - k]; n     } n   } n   range_leq<int> R(lk, n); n   int last = 0; n   scanf( %d , &q); n   while (q--) { n     scanf( %d%d , &l, &r); n     l = (l + last) % n; n     r = (r + last) % n; n     if (r < l) swap(l, r); n     last = R.range_query(l, r, l - 1); n     printf( %d n , last); n   } n   return 0; n } n


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


#include <bits/stdc++.h> n using namespace std; n template <typename T> n inline void checkmin(T &a, T b) { n   if (a > b) a = b; n } n const int N = 1001; n char buf[N][N], bf[N][N] = {0}; n int n, m, cnt = 0; n short mx[N * N], my[N * N]; n int msz = 0; n bool isab(int a, int b) { n   if (a == n - 1 || b == m - 1) return false; n   if (buf[a + 1][b] ==  #  && buf[a][b + 1] ==  #  && buf[a][b] ==  # ) n     return true; n   return false; n } n bool iscd(int c, int d) { n   if (c == 0 || d == 0) return false; n   if (buf[c - 1][d] ==  #  && buf[c][d - 1] ==  #  && buf[c][d] ==  # ) n     return true; n   return false; n } n void findp(vector<int> &a, vector<int> &b, vector<int> &c, vector<int> &d) { n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < m; j++) { n       if (isab(i, j)) { n         bf[i][j] |= 1; n       } n       if (iscd(i, j)) { n         bf[i][j] |= 2; n       } n       if (buf[i][j] ==  # ) { n         mx[msz] = i; n         my[msz] = j; n         msz++; n         cnt++; n       } n     } n   } n   int maxnn = 3; n   for (int i = 0; i < n; i++) { n     int nn = 0; n     for (int j = 0; j < m; j++) { n       if (bf[i][j] & 1) nn++; n       if (nn > maxnn) bf[i][j] &= (~1); n     } n   } n   for (int j = 0; j < m; j++) { n     int nn = 0; n     for (int i = 0; i < n; i++) { n       if (bf[i][j] & 1) nn++; n       if (nn > maxnn) bf[i][j] &= (~1); n     } n   } n   for (int i = n - 1; i >= 0; i--) { n     int nn = 0; n     for (int j = m - 1; j >= 0; j--) { n       if (bf[i][j] & 2) nn++; n       if (nn > maxnn) bf[i][j] &= (~2); n     } n   } n   for (int j = m - 1; j >= 0; j--) { n     int nn = 0; n     for (int i = n - 1; i >= 0; i--) { n       if (bf[i][j] & 2) nn++; n       if (nn > maxnn) bf[i][j] &= (~2); n     } n   } n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < m; j++) { n       if (bf[i][j] & 1) { n         a.push_back(i); n         b.push_back(j); n       } n       if (bf[i][j] & 2) { n         c.push_back(i); n         d.push_back(j); n       } n     } n   } n   if (n == 1000 && m == 1000 && a.size() > 16) n     cout << a.size() <<     << c.size() << endl; n } n void valid(vector<int> &a, vector<int> &b, vector<int> &c, vector<int> &d, n            vector<int> &aa, vector<int> &bb, vector<int> &cc, vector<int> &dd) { n   int asz = a.size(), csz = c.size(); n   for (int i = 0; i < asz; i++) { n     for (int j = 0; j < csz; j++) { n       if (c[j] - a[i] >= 2 && d[j] - b[i] >= 2) { n         aa.push_back(a[i]); n         bb.push_back(b[i]); n         cc.push_back(c[j]); n         dd.push_back(d[j]); n       } n     } n   } n } n bool ok(int a, int b, int c, int d, int aa, int bb, int cc, int dd) { n   if ((c - a + d - b + cc - aa + dd - bb) * 2 < cnt) return false; n   for (int i = a; i <= c; i++) n     if (buf[i][b] !=  #  || buf[i][d] !=  # ) return false; n   for (int i = aa; i <= cc; i++) n     if (buf[i][bb] !=  #  || buf[i][dd] !=  # ) return false; n   for (int i = b; i <= d; i++) n     if (buf[a][i] !=  #  || buf[c][i] !=  # ) return false; n   for (int i = bb; i <= dd; i++) n     if (buf[aa][i] !=  #  || buf[cc][i] !=  # ) return false; n   for (int mi = 0; mi < msz; mi++) { n     int i = mx[mi], j = my[mi]; n     bf[i][j] = buf[i][j]; n   } n   for (int i = a; i <= c; i++) bf[i][b] =  . , bf[i][d] =  . ; n   for (int i = aa; i <= cc; i++) bf[i][bb] =  . , bf[i][dd] =  . ; n   for (int i = b; i <= d; i++) bf[a][i] =  . , bf[c][i] =  . ; n   for (int i = bb; i <= dd; i++) bf[aa][i] =  . , bf[cc][i] =  . ; n   for (int mi = 0; mi < msz; mi++) { n     int i = mx[mi], j = my[mi]; n     if (bf[i][j] ==  # ) return false; n   } n   printf( YES n ); n   printf( %d %d %d %d n , a + 1, b + 1, c + 1, d + 1); n   printf( %d %d %d %d n , aa + 1, bb + 1, cc + 1, dd + 1); n   return true; n } n bool valid2(vector<int> &a, vector<int> &b, vector<int> &c, vector<int> &d, n             vector<int> &aa, vector<int> &bb, vector<int> &cc, n             vector<int> &dd) { n   aa.clear(), bb.clear(), cc.clear(), dd.clear(); n   int sz = a.size(); n   bool res = false; n   for (int i = 0; i < sz; i++) { n     for (int j = 0; j < sz; j++) { n       if (ok(a[i], b[i], c[i], d[i], a[j], b[j], c[j], d[j])) { n         res = true; n         return res; n       } n     } n   } n   return res; n } n void solve() { n   cin >> n >> m; n   for (int i = 0; i < n; i++) scanf( %s , buf[i]); n   vector<int> a, b, c, d; n   vector<int> aa, bb, cc, dd; n   findp(a, b, c, d); n   bool res = true; n   if (cnt > (n + m) * 4) res = false; n   if (res) { n     valid(a, b, c, d, aa, bb, cc, dd); n     res = valid2(aa, bb, cc, dd, a, b, c, d); n   } n   if (!res) cout <<  NO  << endl; n } n int main() { n   solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T> n bool vmin(T& a, T b) { n   return (a > b) ? (a = b, true) : (false); n } n template <typename T> n bool vmax(T& a, T b) { n   return (a < b) ? (a = b, true) : (false); n } n template <typename T> n T smax(T x) { n   return x; n } n template <typename T, typename... K> n T smax(T a, const K&... args) { n   return max(a, smax(args...)); n } n template <typename T> n T smin(T x) { n   return x; n } n template <typename T, typename... K> n T smin(T a, const K&... args) { n   return min(a, smin(args...)); n } n template <typename A, typename B> n ostream& operator<<(ostream& os, const pair<A, B>& z) { n   os <<  (  << z.first <<  ,   << z.second <<  ) ; n   return os; n } n template <typename T> n ostream& operator<<(ostream& os, const vector<T>& a) { n   bool isfirst = true; n   os <<  { ; n   for (auto z : a) { n     if (!isfirst) { n       os <<  ,  ; n     } n     os << z; n     isfirst = false; n   } n   os <<  } ; n   return os; n } n const int inf = (signed)(~0u >> 2); n const long long llf = (signed long long)(~0ull >> 2); n template <typename T> n int vsize(vector<T>& x) { n   return (signed)x.size(); n } n template <typename T> n int discrete(T* a, int* b, int n) { n   vector<T> v(a + 1, a + n + 1); n   sort(v.begin(), v.end()); n   v.erase(unique(v.begin(), v.end()), v.end()); n   for (int i = 1; i <= n; i++) n     b[i] = lower_bound(v.begin(), v.end(), a[i]) - v.begin() + 1; n   return v.size(); n } n mt19937 rng(time(NULL)); n int randint(int l, int r) { return rng() % (r - l + 1) + l; } n void exgcd(int a, int b, int& x, int& y) { n   if (!b) { n     x = 1, y = 0; n   } else { n     exgcd(b, a % b, y, x); n     y -= (a / b) * x; n   } n } n int inv(int a, int n) { n   int x, y; n   exgcd(a, n, x, y); n   return (x < 0) ? (x + n) : (x); n } n const int Mod = 1e9 + 7; n template <const int Mod = ::Mod> n class Z { n  public: n   int v; n   Z() : v(0) {} n   Z(int x) : v(x) {} n   Z(long long x) : v(x % Mod) {} n   friend Z operator+(const Z& a, const Z& b) { n     int x; n     return Z(((x = a.v + b.v) >= Mod) ? (x - Mod) : (x)); n   } n   friend Z operator-(const Z& a, const Z& b) { n     int x; n     return Z(((x = a.v - b.v) < 0) ? (x + Mod) : (x)); n   } n   friend Z operator*(const Z& a, const Z& b) { return Z(a.v * 1ll * b.v); } n   friend Z operator~(const Z& a) { return inv(a.v, Mod); } n   friend Z operator-(const Z& a) { return Z(0) - a; } n   Z& operator+=(Z b) { return *this = *this + b; } n   Z& operator-=(Z b) { return *this = *this - b; } n   Z& operator*=(Z b) { return *this = *this * b; } n   friend bool operator==(const Z& a, const Z& b) { return a.v == b.v; } n }; n Z<> qpow(Z<> a, int p) { n   Z<> rt = Z<>(1), pa = a; n   for (; p; p >>= 1, pa = pa * pa) { n     if (p & 1) { n       rt = rt * pa; n     } n   } n   return rt; n } n const int N = 1e6 + 5; n int n, m; n char s[N]; n Z<> f[N]; n int tp; n pair<int, int> stk[N]; n int c0[N]; n int tr[N][2]; n bool hav1[N]; n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0), cout.tie(0); n   cin >> (s + 1); n   n = strlen(s + 1); n   int pr = 1, sf = n; n   while (pr <= n && s[pr] ==  0 ) pr++; n   while (sf >= 1 && s[sf] ==  0 ) sf--; n   if (pr > n) { n     cout << n <<   n ; n     return 0; n   } n   for (int i = pr; i <= sf; i++) { n     s[i - pr + 1] = s[i]; n   } n   s[sf + 2 - pr] = 0; n   int m = sf - pr + 1; n   sf = n - sf + 1; n   n = m; n   tr[n + 1][1] = n + 1; n   int p1 = n + 1; n   for (int i = n; ~i; i--) { n     tr[i][1] = p1; n     if (s[i] ==  1 ) { n       p1 = i; n     } n   } n   stk[tp = 0] = pair<int, int>(n + 1, n + 10); n   vector<pair<int, int> > s0; n   for (int i = 1, j; i <= n; i = j) { n     if (s[i] ==  0 ) { n       j = i; n       while (j <= n && s[j] ==  0 ) j++; n       s0.emplace_back(i, j - 1); n     } else { n       j = i + 1; n     } n   } n   reverse(s0.begin(), s0.end()); n   for (auto seg : s0) { n     int l = seg.first; n     int r = seg.second; n     for (int i = l; i < r; i++) { n       tr[i][0] = i + 1; n     } n     int len = r - l + 1; n     while (stk[tp].second <= len) tp--; n     tr[r][0] = min(stk[tp].first + len, n + 1); n     stk[++tp] = pair<int, int>(l, len); n   } n   int p0 = n + 1; n   for (int i = n; ~i; i--) { n     if (s[i] ==  0 ) { n       p0 = i; n     } else { n       tr[i][0] = p0; n     } n   } n   tr[0][0] = n + 1; n   f[0] = 1; n   for (int i = 0; i < n; i++) { n     f[tr[i][0]] += f[i]; n     f[tr[i][1]] += f[i]; n   } n   Z<> ans = 0; n   for (int i = 1; i <= n; i++) { n     if (s[i] ==  1 ) { n       ans += f[i]; n     } n   } n   ans *= Z<>(pr) * sf; n   cout << ans.v <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   string a, b; n   cin >> a >> b; n   reverse(b.begin(), b.end()); n   cout << atoi(a.c_str()) + atoi(b.c_str()) << endl; n } n


#include <bits/stdc++.h> n using namespace std; n const long long mod = 1000000007; n long long powmod(long long a, long long b) { n   long long res = 1; n   a %= mod; n   assert(b >= 0); n   for (; b; b >>= 1) { n     if (b & 1) res = res * a % mod; n     a = a * a % mod; n   } n   return res; n } n const int N = 55; n int n, m, sw; n char s[N][N], t[N]; n vector<pair<int, int> > ret1, ret2; n void gao(vector<pair<int, int> > &ret) { n   for (int j = 0; j < m; j++) { n     int i = 0; n     while (i < n) { n       if (s[i][j] ==  L ) { n         for (int k = 0; i + k < n && j + k < m; k++) { n           if (s[i + 1 + k][j + k] ==  L ) { n             for (int l = k; l >= 0; l--) { n               ret.push_back(make_pair(i + l, j + l)); n               s[i + l][j + l] = s[i + l][j + l + 1] =  U ; n               s[i + l + 1][j + l] = s[i + l + 1][j + l + 1] =  D ; n               if (l > 0) { n                 ret.push_back(make_pair(i + l, j + l - 1)); n                 s[i + l][j + l - 1] = s[i + l + 1][j + l - 1] =  L ; n                 s[i + l][j + l] = s[i + l + 1][j + l] =  R ; n               } n             } n             break; n           } n           if (s[i + 1 + k][j + 1 + k] ==  U ) { n             for (int l = k; l >= 0; l--) { n               ret.push_back(make_pair(i + l + 1, j + l)); n               s[i + l + 1][j + l] = s[i + l + 2][j + l] =  L ; n               s[i + l + 1][j + l + 1] = s[i + l + 2][j + l + 1] =  R ; n               ret.push_back(make_pair(i + l, j + l)); n               s[i + l][j + l] = s[i + l][j + l + 1] =  U ; n               s[i + l + 1][j + l] = s[i + l + 1][j + l + 1] =  D ; n             } n             break; n           } n         } n       } n       i += 2; n     } n   } n } n int main() { n   scanf( %d%d , &n, &m); n   if (n % 2 == 1) { n     sw = 1; n     for (int i = 0; i < n; i++) { n       scanf( %s , t); n       for (int j = 0; j < m; j++) { n         if (t[j] ==  L ) n           s[j][i] =  U ; n         else if (t[j] ==  R ) n           s[j][i] =  D ; n         else if (t[j] ==  U ) n           s[j][i] =  L ; n         else n           s[j][i] =  R ; n       } n     } n     swap(n, m); n   } else { n     for (int i = 0; i < n; i++) scanf( %s , s[i]); n   } n   gao(ret1); n   if (sw == 1) { n     for (int i = 0; i < m; i++) { n       scanf( %s , t); n       for (int j = 0; j < n; j++) { n         if (t[j] ==  L ) n           s[j][i] =  U ; n         else if (t[j] ==  R ) n           s[j][i] =  D ; n         else if (t[j] ==  U ) n           s[j][i] =  L ; n         else n           s[j][i] =  R ; n       } n     } n   } else { n     for (int i = 0; i < n; i++) scanf( %s , s[i]); n   } n   gao(ret2); n   reverse((ret2).begin(), (ret2).end()); n   for (auto p : ret2) ret1.push_back(p); n   if (sw) { n     for (auto &p : ret1) swap(p.first, p.second); n   } n   printf( %d n , ((int)(ret1).size())); n   for (auto p : ret1) printf( %d %d n , p.first + 1, p.second + 1); n } n


#include <bits/stdc++.h> n const int N = 1e5 + 7; n long long n, m, A[N], sum[N], suf[N]; n inline int read() { n   int now = 0; n   register char c = getchar(); n   for (; !isdigit(c); c = getchar()) n     ; n   for (; isdigit(c); now = now * 10 + c -  0 , c = getchar()) n     ; n   return now; n } n int main() { n   n = read(), m = read(); n   for (int i = 1; i <= n; ++i) A[i] = read(); n   for (int i = 1; i <= n; i += 2) n     sum[i + 1] = sum[i] = sum[i - 1] + A[i] - A[i - 1]; n   if (n & 1) { n     for (int i = n - 1; i > 0; i -= 2) n       suf[i - 1] = suf[i] = suf[i + 1] + A[i + 1] - A[i]; n   } else { n     A[n + 1] = m; n     for (int i = n; i > 0; i -= 2) n       suf[i - 1] = suf[i] = suf[i + 1] + A[i + 1] - A[i]; n   } n   long long ans = sum[n]; n   if (!(n & 1)) ans += m - A[n]; n   if (n & 1) A[++n] = m; n   for (long long i = 1; i <= n; ++i) n     if (A[i] - A[i - 1] > 1) { n       if (i & 1) n         ans = n             std::max(ans, sum[i - 1] + A[i] - A[i - 1] - 1 + m - A[i] - suf[i]); n       else n         ans = n             std::max(ans, sum[i - 1] + A[i] - A[i - 1] - 1 + m - A[i] - suf[i]); n     } n   printf( %I64d , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long oo = 1e18; n vector<int> dinh; n int nn; n long long n, m, k, kc[801][801]; n vector<pair<long long, pair<long long, long long>>> e; n void compress() { n   sort(dinh.begin(), dinh.end()); n   dinh.resize(unique(dinh.begin(), dinh.end()) - dinh.begin()); n   nn = dinh.size(); n   for (long long i = 0; i <= nn - 1; i++) n     for (long long j = 0; j <= nn - 1; j++) kc[i][j] = oo; n   for (long long i = 0; i <= nn - 1; i++) kc[i][i] = 0; n   for (long long i = 0; i <= min(m, k) - 1; i++) { n     long long first = n         lower_bound(dinh.begin(), dinh.end(), e[i].second.first) - dinh.begin(); n     long long second = n         lower_bound(dinh.begin(), dinh.end(), e[i].second.second) - n         dinh.begin(); n     kc[first][second] = kc[second][first] = min(kc[first][second], e[i].first); n   } n } n void floyd() { n   for (long long k = 0; k <= nn - 1; k++) n     for (long long first = 0; first <= nn - 1; first++) n       for (long long second = 0; second <= nn - 1; second++) n         kc[first][second] = n             min(kc[first][second], kc[first][k] + kc[k][second]); n } n int main() { n   cin >> n >> m >> k; n   for (long long i = 0; i <= m - 1; i++) { n     long long first, second, w; n     cin >> first >> second >> w; n     e.push_back({w, {first - 1, second - 1}}); n   } n   sort(e.begin(), e.end()); n   for (long long i = 0; i <= min(m, k) - 1; i++) { n     dinh.push_back(e[i].second.first); n     dinh.push_back(e[i].second.second); n   } n   compress(); n   floyd(); n   vector<long long> res; n   for (long long i = 0; i <= nn - 1; i++) n     for (long long j = 0; j <= i - 1; j++) res.push_back(kc[i][j]); n   sort(res.begin(), res.end()); n   cout << res[k - 1]; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 3e5 + 15; n struct Node { n   int l, r; n   bool operator<(const Node& b) const { return r != b.r ? r < b.r : l < b.l; } n }; n unordered_map<int, vector<Node> > mp; n int sum[N]; n int main() { n   int n; n   while (~scanf( %d , &n)) { n     mp.clear(); n     for (int i = 1; i <= n; i++) { n       scanf( %d , &sum[i]); n       sum[i] += sum[i - 1]; n     } n     for (int i = 1; i <= n; i++) { n       for (int j = i; j <= n; j++) { n         mp[sum[j] - sum[i - 1]].emplace_back(Node{i, j}); n       } n     } n     int ans = 0; n     vector<Node> vec, vecAns; n     for (auto ele : mp) { n       sort(ele.second.begin(), ele.second.end()); n       vec.clear(); n       int sum = 0, r = 0; n       for (auto seg : ele.second) { n         if (seg.l > r) { n           vec.emplace_back(Node{seg.l, seg.r}); n           r = seg.r; n           sum++; n         } n       } n       if (ans < sum) { n         ans = sum; n         vecAns = vec; n       } n     } n     printf( %d n , ans); n     for (auto ele : vecAns) { n       printf( %d %d n , ele.l, ele.r); n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   string s; n   cin >> s; n   int sz = s.size(); n   s =  X  + s; n   int k; n   cin >> k; n   int ans = 0; n   for (int i = 1; i <= sz; i++) { n     for (int len = 1; len <= 400; len++) { n       if (i + 2 * len - 1 > sz + k) break; n       bool can = true; n       for (int l = i; l <= i + len - 1; l++) { n         if (l > sz) break; n         if (l + len <= sz) { n           if (s[l] != s[l + len]) { n             can = false; n             break; n           } n         } n       } n       if (can) ans = max(ans, 2 * len); n     } n   } n   cout << ans <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n  n using namespace std; n  n #define ll long long int n  n void solve() n { n     ll p,a,b,c; n     cin >> p >> a >> b >> c; n  n     ll ans = 1e18 + 1; n     if(p%a != 0) ans = min(a - p%a, ans); n     else ans = 0; n     if(p%b != 0) ans = min(b - p%b, ans); n     else ans = 0; n     if(p%c != 0) ans = min(c - p%c, ans); n     else ans = 0; n      n     // if(ans == 1e18 + 1) ans = 0; n  n     cout << ans << endl; n } n  n int main() n { n     int t; n     cin >> t; n  n     while(t--) solve(); n  n     return 0; n }


#include <bits/stdc++.h> n using namespace std; n const int maxn = 300 + 66; n const long long mod = 1e9 + 7; n int N, M, Q; n int a[maxn][maxn]; n int main() { n   scanf( %d , &N); n   for (int i = 1; i <= N; i++) { n     for (int j = 1; j <= N; j++) { n       scanf( %d , &a[i][j]); n     } n   } n   scanf( %d , &Q); n   int u, v, w; n   while (Q--) { n     scanf( %d %d %d , &u, &v, &w); n     if (a[u][v] > w) { n       a[u][v] = min(w, a[u][v]); n       a[v][u] = min(w, a[u][v]); n       for (int j = 1; j <= N; j++) { n         for (int i = 1; i <= N; i++) { n           a[i][j] = a[j][i] = min(a[j][i], a[j][u] + a[u][i]); n         } n       } n       for (int j = 1; j <= N; j++) { n         for (int i = 1; i <= N; i++) { n           a[i][j] = a[j][i] = min(a[j][i], a[j][v] + a[v][i]); n         } n       } n     } n     long long s = 0; n     for (int i = 1; i <= N; i++) { n       for (int j = i + 1; j <= N; j++) { n         s += a[i][j]; n       } n     } n     printf( %lld  , s); n   } n } n


#include <bits/stdc++.h> n using namespace std; n template <class T> n ostream& operator<<(ostream& os, vector<T> V) { n   os <<  [  ; n   for (auto v : V) os << v <<    ; n   os <<  ] ; n   return os; n } n template <class T> n ostream& operator<<(ostream& os, set<T> second) { n   os <<  {  ; n   for (auto s : second) os << s <<    ; n   return os <<  } ; n } n template <class T> n ostream& operator<<(ostream& os, multiset<T> second) { n   os <<  {  ; n   for (auto s : second) os << s <<    ; n   return os <<  } ; n } n template <class L, class R> n ostream& operator<<(ostream& os, pair<L, R> P) { n   return os <<  (  << P.first <<  ,  << P.second <<  ) ; n } n template <class L, class R> n ostream& operator<<(ostream& os, map<L, R> M) { n   os <<  {  ; n   for (auto m : M) os <<  (  << m.first <<  :  << m.second <<  )  ; n   return os <<  } ; n } n template <class L, class R> n ostream& operator<<(ostream& os, unordered_map<L, R> M) { n   os <<  {  ; n   for (auto m : M) os <<  (  << m.first <<  :  << m.second <<  )  ; n   return os <<  } ; n } n const long long N = 200010, M = 1e9 + 7; n long long n, a[N]; n vector<long long> f[N], sol; n void solve() { n   cin >> n; n   for (long long i = 0; i < n; i++) cin >> a[i]; n   for (long long i = 0; i < n; i++) f[a[i]].push_back(i + 1); n   sol = vector<long long>(0); n   long long minus = 0; n   for (long long i = 0; i < n; i++) { n     long long h = i - minus; n     while (h >= 0 && f[h].size() == 0) h -= 3, minus += 3; n     if (h < 0) { n       cout <<  Impossible n ; n       return; n     } n     sol.push_back(f[h].back()); n     f[h].pop_back(); n   } n   cout <<  Possible n ; n   for (long long it : sol) cout << it <<    ; n   cout <<   n ; n } n int main() { n   ios::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(0); n   solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long H; n int N, M, K; n long long A[100002], C[100002]; n long long add[100002]; n bool can[100002]; n long long minget[10002]; n set<pair<long long, int> > Q; n set<pair<int, int>, greater<pair<int, int> > > S; n int main() { n   cin.sync_with_stdio(false); n   cin >> H >> N >> M >> K; n   for (int i = 0; i < K; ++i) minget[i] = 1LL << 62; n   minget[0] = 0; n   for (int i = 1; i <= N; ++i) { n     cin >> C[i] >> A[i]; n     --C[i]; n     if (minget[C[i] % K] <= C[i]) { n       S.insert(make_pair(A[i], -i)); n       can[i] = true; n     } n   } n   for (int i = 1, type; i <= M; ++i) { n     cin >> type; n     if (type == 1) { n       long long val; n       cin >> val; n       add[++add[0]] = val; n       for (int j = 0; j < K; ++j) minget[j] = 1LL << 62; n       minget[0] = 0; n       Q.insert(make_pair(0, 0)); n       while (!Q.empty()) { n         int now = (*Q.begin()).second; n         Q.erase(Q.begin()); n         for (int j = 1; j <= add[0]; ++j) n           if (minget[(now + add[j]) % K] > minget[now] + add[j]) { n             minget[(now + add[j]) % K] = minget[now] + add[j]; n             Q.insert(make_pair(minget[(now + add[j]) % K], (now + add[j]) % K)); n           } n       } n       for (int j = 1; j <= N; ++j) n         if (!can[j] && minget[C[j] % K] <= C[j]) { n           S.insert(make_pair(A[j], -j)); n           can[j] = true; n         } n     } else if (type == 2) { n       int pos, val; n       cin >> pos >> val; n       if (!can[pos]) n         A[pos] -= val; n       else { n         S.erase(S.find(make_pair(A[pos], -pos))); n         A[pos] -= val; n         S.insert(make_pair(A[pos], -pos)); n       } n     } else if (type == 3) { n       if (S.empty()) { n         cout << 0 <<   n ; n         continue; n       } n       cout << (*S.begin()).first <<   n ; n       A[-(*S.begin()).second] = 0; n       S.erase(S.begin()); n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n int gcd(int a, int b) { return b ? gcd(b, a % b) : a; } n int main() { n   int n; n   cin >> n; n   vector<int> m(n), r(n); n   unsigned long long lcm = 1; n   for (int i = 0; i < n; ++i) { n     cin >> m[i]; n     lcm *= m[i] / gcd(m[i], lcm); n   } n   for (int i = 0; i < n; ++i) cin >> r[i]; n   int ans = 0; n   for (int d = 0; d < lcm; ++d) { n     for (int i = 0; i < n; ++i) n       if (d % m[i] == r[i]) { n         ++ans; n         break; n       } n   } n   cout.setf(ios::fixed | ios::showpoint); n   cout.precision(6); n   cout << 1.0 * ans / lcm; n } n


#include <bits/stdc++.h> n using namespace std; n const int INF = 2147483647; n const double PI = 3.141592653589793; n const int N = 40005; n map<pair<int, int>, int> mapa; n int n, m, k, t, a, b, c, pocz[N], waste[N], r, i; n int main() { n   scanf( %d %d %d %d , &n, &m, &k, &t); n   for (i = 0; i < n; i++) waste[i] = 0; n   mapa.clear(); n   while (k--) { n     scanf( %d %d , &a, &b); n     a--; n     b--; n     mapa[pair<int, int>(a, b)] = 1; n     waste[a]++; n   } n   pocz[0] = 0; n   for (i = 1; i < n; i++) pocz[i] = (pocz[i - 1] + m - waste[i - 1]) % 3; n   while (t--) { n     scanf( %d %d , &a, &b); n     a--; n     b--; n     if (mapa.count(pair<int, int>(a, b)) > 0) n       printf( Waste n ); n     else { n       c = 0; n       while (mapa.count(pair<int, int>(a, c)) > 0) c++; n       r = pocz[a]; n       while (c != b) { n         c++; n         while (mapa.count(pair<int, int>(a, c)) > 0) c++; n         r = (r + 1) % 3; n       } n       if (r == 0) n         printf( Carrots n ); n       else if (r == 1) n         printf( Kiwis n ); n       else n         printf( Grapes n ); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long md = 1e9 + 7; n const int N = 100005; n long long power(long long x, long long y, long long p) { n   long long res = 1; n   x = x % p; 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 fact[100003] = {1}; n long long term(long long n, long long r) { n   long long y = fact[n]; n   y = (y * power(fact[r], md - 2, md)) % md; n   y = (y * power(fact[n - r], md - 2, md)) % md; n   return y; n } n long long fun(long long x) { n   long long val = 0; n   for (int i = 0; i <= x / 2; i++) { n     val = (val + term(x - i, i)) % md; n   } n   return val; n } n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   for (int i = 1; i <= 100002; i++) { n     fact[i] = (fact[i - 1] * i) % md; n   } n   string s; n   cin >> s; n   long long u = 0, n = 0; n   long long ans = 1; n   for (int i = 0; i < s.size(); i++) { n     if (s[i] ==  u ) { n       u++; n     } else { n       if (u != 0) { n         long long val = fun(u); n         ans = (ans * val) % md; n       } n       u = 0; n     } n     if (s[i] ==  n ) { n       n++; n     } else { n       if (n != 0) { n         long long val = fun(n); n         ans = (ans * val) % md; n       } n       n = 0; n     } n     if (s[i] ==  w  || s[i] ==  m ) { n       cout <<  0  n            <<   n ; n       return 0; n     } n   } n   if (u != 0) { n     long long val = fun(u); n     ans = (ans * val) % md; n   } else if (n != 0) { n     long long val = fun(n); n     ans = (ans * val) % md; n   } n   cout << ans <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n char str[100020]; n int len[3] = {0}; n int num[100020]; n void zhuanhuan(int x) { n   if (x >= 3) { n     len[2]++; n   } else if (x == 2) { n     len[1]++; n   } else { n     len[0]++; n   } n } n int main() { n   int n, kuan = 1; n   scanf( %d , &n); n   scanf( %*c ); n   for (int i = 0; i < n; i = i + 1) scanf( %c , &str[i]); n   scanf( %*c ); n   for (int i = 0; i < n; i = i + 1) num[i] = str[i] -  0 ; n   int kuanlen = 1; n   for (int i = 1; i < n; i++) { n     if (num[i] == num[i - 1]) { n       kuanlen++; n     } else { n       kuan++; n       zhuanhuan(kuanlen); n       kuanlen = 1; n     } n   } n   zhuanhuan(kuanlen); n   if (len[2] > 0) { n     printf( %d n , kuan + 2); n   } else if (len[1] > 1) { n     printf( %d n , kuan + 2); n   } else { n     if (len[1] == 1) n       printf( %d n , kuan + 1); n     else n       printf( %d n , kuan); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int gcd(int a, int b) { n   if (b == 0) { n     return a; n   } n   return gcd(b, a % b); n } n int main() { n   int x, y; n   long long int a, b; n   cin >> x >> y >> a >> b; n   int lcm = (x * y) / (gcd(x, y)); n   int ans = (b / lcm) - ((a - 1) / lcm); n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, m; n   cin >> n >> m; n   string s[n + 1]; n   for (int i = 0; i < n; i++) { n     cin >> s[i]; n   } n   long long ans = 1; n   for (int i = 0; i < m; i++) { n     unordered_set<char> b; n     for (int j = 0; j < n; j++) { n       b.insert(s[j][i]); n     } n     ans = (ans * b.size()) % 1000000007; n   } n   cout << ans; n } n


#include <bits/stdc++.h> n using namespace std; n int t; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   cin >> t; n   while (t--) { n     int a[3]; n     int b[3]; n     cin >> a[0] >> a[1] >> a[2]; n     cin >> b[0] >> b[1] >> b[2]; n     long long sum = 0; n     int aux = 0; n     for (int i = 2; i >= 0; i--) { n       if (i == 2) { n         for (int j : {1, 2, 0}) { n           aux = min(a[i], b[j]); n           if (i > j) n             sum += aux * i * j; n           else if (i < j) n             sum -= aux * i * j; n           a[i] -= aux; n           b[j] -= aux; n         } n       } else if (i == 1) { n         for (int j : {1, 0, 2}) { n           aux = min(a[i], b[j]); n           if (i > j) n             sum += aux * i * j; n           else if (i < j) n             sum -= aux * i * j; n           a[i] -= aux; n           b[j] -= aux; n         } n       } else { n         for (int j : {0, 1, 2}) { n           aux = min(a[i], b[j]); n           if (i > j) n             sum += aux * i * j; n           else if (i < j) n             sum -= aux * i * j; n           a[i] -= aux; n           b[j] -= aux; n         } n       } n     } n     cout << sum <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long pw(long long b, long long p) { n   if (!p) return 1; n   long long sq = pw(b, p / 2); n   sq *= sq; n   if (p % 2) sq *= b; n   return sq; n } n long long gcd(long long a, long long b) { return (b == 0 ? a : gcd(b, a % b)); } n long long sd(long long x) { return x < 10 ? x : x % 10 + sd(x / 10); } n long long lcm(long long a, long long b) { return ((a * b) / gcd(a, b)); } n int n, xs, ys, xd, yd, dp[1010][1010]; n int dx[] = {0, 0, 1, -1}; n int dy[] = {1, -1, 0, 0}; n bool isvalid(int x, int y) { n   return (((y == 0 || y == n) && x <= n && x >= 0) || n           ((x == 0 || x == n) && y <= n && y >= 0)); n } n int dfs(int x, int y) { n   if (x == xd && y == yd) return 0; n   if (!isvalid(x, y)) return 1 << 30; n   int &ret = dp[x][y]; n   if (ret != -1) return ret; n   ret = 1 << 30; n   for (int i = 0; i < 4; ++i) ret = min(ret, dfs(x + dx[i], y + dy[i]) + 1); n   return ret; n } n int main() { n   cin >> n >> xs >> ys >> xd >> yd; n   memset(dp, -1, sizeof dp); n   cout << dfs(xs, ys); n } n


#include <bits/stdc++.h> n using namespace std; n int N, M; n vector<int> edge[100001]; n int val[100001]; n vector<int> ans[2]; n void dfs(int i, int fi) { n   if (val[i] != 0 && fi != val[i]) { n     cout <<  -1 n ; n     exit(0); n   } n   if (val[i] != 0 && fi == val[i]) { n     return; n   } n   val[i] = fi; n   ans[(fi + 1) / 2].push_back(i); n   for (int j = 0; j < edge[i].size(); j++) { n     dfs(edge[i][j], -fi); n   } n } n int main() { n   ios_base::sync_with_stdio(false); n   if (fopen( cf688c.in ,  r )) { n     freopen( cf688c.in ,  r , stdin); n     freopen( cf688c.out ,  w , stdout); n   } n   cin >> N >> M; n   for (int i = 0; i < M; i++) { n     int a, b; n     cin >> a >> b; n     a--; n     b--; n     edge[a].push_back(b); n     edge[b].push_back(a); n   } n   for (int i = 0; i < N; i++) { n     if (val[i] == 0) { n       if (ans[0].size() == 0) { n         dfs(i, -1); n       } else { n         dfs(i, 1); n       } n     } n   } n   if (ans[0].size() == 0 || ans[1].size() == 0) { n     cout <<  -1 n ; n     return 0; n   } n   cout << ans[0].size() <<   n ; n   for (int i = 0; i < ans[0].size(); i++) { n     cout << ans[0][i] + 1 <<    ; n   } n   cout <<   n ; n   cout << ans[1].size() <<   n ; n   for (int i = 0; i < ans[1].size(); i++) { n     cout << ans[1][i] + 1 <<    ; n   } n   cout <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long n, s[1 << 10]; n vector<long> v[1 << 10]; n long spre, slim, found; n long trem, tail; n int main() { n   ios_base::sync_with_stdio(0); n   cin >> n; n   for (int i = 1; i <= n; i++) { n     cin >> s[i]; n     for (int j = 0; j < 666; j++) v[i].push_back(0); n   } n   for (int j = 0; j < 666; j++) v[0].push_back(0); n   for (int i = 1; i <= n; i++) { n     v[i] = v[i - 1]; n     spre = s[i - 1]; n     slim = 0; n     found = 0; n     for (int j = 665; j >= 0; --j) { n       if (found) break; n       spre -= v[i][j]; n       for (int q = v[i][j] + 1; q <= 9 && found == 0; q++) { n         trem = spre + q; n         tail = s[i] - trem; n         if (tail >= 0 && tail <= slim) { n           v[i][j] = q; n           for (int t = 665; t > j; t--) { n             if (tail > 9) { n               v[i][t] = 9; n               tail -= 9; n             } else { n               v[i][t] = tail; n               tail = 0; n             } n           } n           found = 1; n           v[i][j] = q; n         } n       } n       slim += 9; n     } n   } n   for (int i = 1; i <= n; i++) { n     reverse(v[i].begin(), v[i].end()); n     while (v[i].size() > 1 && v[i].back() == 0) v[i].pop_back(); n     reverse(v[i].begin(), v[i].end()); n     for (int j = 0; j < v[i].size(); j++) { n       cout << v[i][j]; n     } n     cout << endl; n   } n   cin.get(); n   cin.get(); n   return 0; n } n


#include <bits/stdc++.h> n int main(void) { n   int a, b, m, n, count = 0; n   scanf( %d %d , &n, &m); n   for (a = 0; a <= m; a++) { n     for (b = 0; b <= n; b++) { n       if (n - b != a * a) continue; n       if (m - a != b * b) continue; n       count++; n       break; n     } n   } n   printf( %d n , count); n   return 0; n } n


#include <bits/stdc++.h> n inline int pidorand() { return ((rand() & 32767) << 15) | (rand() & 32767); } n using namespace std; n inline int nxt() { n   int x; n   scanf( %d , &x); n   return x; n } n using Point = pair<int, int>; n int mydiv(int first, int second) { n   if (first % second == 0) { n     return first / second; n   } else { n     return first / second - (first < 0); n   } n } n Point convert(int first, int second) { n   first += second; n   second = first - 2 * second; n   return {mydiv(first, 2), mydiv(second, 2)}; n } n const int N = 111111; n int up_y[N + N], down_y[N + N]; n int left_y[N + N], right_y[N + N]; n int main() { n   int n = nxt(); n   vector<Point> ppts[2]; n   for (int i = 0; i < n; ++i) { n     int first = nxt(), second = nxt(); n     ppts[(abs(first) + abs(second)) % 2].push_back(convert(first, second)); n   } n   for (int i = 0; i < 2; ++i) { n     sort((ppts[i]).begin(), (ppts[i]).end()); n   } n   long long ans = 0; n   for (auto pts : ppts) { n     for (int i = 0; i < N + N; ++i) { n       left_y[i] = right_y[i] = -N; n     } n     for (auto pt : pts) { n       left_y[pt.first + N] = max(left_y[pt.first + N], pt.second); n       right_y[pt.first + N] = max(right_y[pt.first + N], pt.second); n     } n     for (int i = 1; i < N + N; ++i) { n       left_y[i] = max(left_y[i], left_y[i - 1]); n     } n     for (int i = N + N - 2; i >= 0; --i) { n       right_y[i] = max(right_y[i], right_y[i + 1]); n     } n     for (int i = 0; i < N + N; ++i) { n       up_y[i] = min(left_y[i], right_y[i]); n     } n     for (int i = 0; i < N + N; ++i) { n       left_y[i] = right_y[i] = N; n     } n     for (auto pt : pts) { n       left_y[pt.first + N] = min(left_y[pt.first + N], pt.second); n       right_y[pt.first + N] = min(right_y[pt.first + N], pt.second); n     } n     for (int i = 1; i < N + N; ++i) { n       left_y[i] = min(left_y[i], left_y[i - 1]); n     } n     for (int i = N + N - 2; i >= 0; --i) { n       right_y[i] = min(right_y[i], right_y[i + 1]); n     } n     for (int i = 0; i < N + N; ++i) { n       down_y[i] = max(left_y[i], right_y[i]); n     } n     for (int i = 1; i < N + N; ++i) { n       int l = max(down_y[i - 1], down_y[i]); n       int r = min(up_y[i - 1], up_y[i]); n       if (l >= r) { n         continue; n       } n       ans += r - l; n     } n   } n   cout << ans <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n inline bool EQ(double a, double b) { return fabs(a - b) < 1e-9; } n const int INF = (((1 << 30) - 1) << 1) + 1; n const int nINF = 1 << 31; n int gcd(int a, int b) { return b ? gcd(b, a % b) : a; } n int main() { n   ios::sync_with_stdio(false); n   int n; n   cin >> n; n   double ans = 0; n   pair<int, int> res; n   for (int i = (n - 2); i >= (0); i--) { n     int a = i, b = n - i; n     if (a > b) continue; n     if (gcd(a, b) == 1 && a / (double)b > ans) { n       ans = a / (double)b; n       res = {a, b}; n     } n   } n   cout << res.first <<     << res.second << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n string s; n int n, res, t; n int main() { n   cin >> n >> s; n   for (int i = 0; i < n; ++i) { n     if (s[i] ==  x ) n       ++t; n     else n       t = 0; n     if (t >= 3) ++res; n   } n   cout << res << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   int t; n   cin >> t; n   while (t--) { n     int n; n     cin >> n; n     vector<string> v(n); n     for (auto& i : v) cin >> i; n     bool state = true; n     for (int i = n - 2; i >= 0; i--) { n       for (int j = n - 2; j >= 0; --j) { n         if (v[i][j] ==  1  && v[i + 1][j] ==  0  && v[i][j + 1] ==  0 ) n           state = false; n       } n     } n     if (state) n       cout <<  Yes  <<   n ; n     else n       cout <<  No  <<   n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   char a[26], b[26], c[26], d[26]; n   scanf( %s , a); n   scanf( %s , b); n   int i = 0, m = 0, n = 0, k, q, w, j = 0, flag = 0, t = 0; n   while (a[i] !=   0 ) { n     if (a[i] ==  | ) m = i; n     i++; n   } n   q = m; n   w = i - m - 1; n   n = i; n   while (b[j] !=   0 ) { n     j++; n   } n   i = 0; n   for (i = 0; i < q; i++) c[i] = a[i]; n   for (i = 0; i < w; i++) d[i] = a[q + i + 1]; n   i = 0; n   while (j > 0) { n     if (q < w) { n       c[q++] = b[i++]; n       j--; n     } else if (q >= w) { n       d[w++] = b[i++]; n       j--; n     } n   } n   if (q == w) { n     for (i = 0; i < q; i++) printf( %c , c[i]); n     printf( | ); n     for (i = 0; i < w; i++) printf( %c , d[i]); n   } else { n     printf( Impossible ); n   } n   return 0; n } n


#include <iostream> n #include <cstdio> n #include <cmath> n #include <ctime> n #include <cstdlib> n #include <cassert> n #include <vector> n #include <list> n #include <stack> n #include <queue> n #include <deque> n #include <map> n #include <set> n #include <bitset> n #include <string> n #include <algorithm> n #include <utility> n #include <complex> n #define rep(x, s, t) for(llint (x) = (s); (x) <= (t); (x)++) n #define reps(x, s) for(llint (x) = 0; (x) < (llint)(s).size(); (x)++) n #define chmin(x, y) (x) = min((x), (y)) n #define chmax(x, y) (x) = max((x), (y)) n #define sz(x) ((ll)(x).size()) n #define ceil(x, y) (((x)+(y)-1) / (y)) n #define all(x) (x).begin(),(x).end() n #define outl(...) dump_func(__VA_ARGS__) n #define inf 1e18 n  n using namespace std; n  n typedef long long llint; n typedef long long ll; n typedef pair<ll, ll> P; n  n struct edge{ n  ll to, cost; n  edge(){} n  edge(ll a, ll b){ to = a, cost = b;} n }; n const ll dx[] = {1, 0, -1, 0}, dy[] = {0, -1, 0, 1}; n  n const ll mod = 1000000007; n //const ll mod = 998244353; n  n struct mint{ n  ll x = 0; n  mint(ll y = 0){x = y; if(x < 0 || x >= mod) x = (x%mod+mod)%mod;} n  mint(const mint &ope) {x = ope.x;} n   n  mint operator-(){return mint(-x);} n  mint operator+(const mint &ope){return mint(x) += ope;} n  mint operator-(const mint &ope){return mint(x) -= ope;} n  mint operator*(const mint &ope){return mint(x) *= ope;} n  mint operator/(const mint &ope){return mint(x) /= ope;} n  mint& operator+=(const mint &ope){ n   x += ope.x; n   if(x >= mod) x -= mod; n   return *this; n  } n  mint& operator-=(const mint &ope){ n   x += mod - ope.x; n   if(x >= mod) x -= mod; n   return *this; n  } n  mint& operator*=(const mint &ope){ n   x *= ope.x, x %= mod; n   return *this; n  } n  mint& operator/=(const mint &ope){ n   ll n = mod-2; mint mul = ope; n   while(n){ n    if(n & 1) *this *= mul; n    mul *= mul; n    n >>= 1; n   } n   return *this; n  } n  mint inverse(){return mint(1) / *this;} n  bool operator ==(const mint &ope){return x == ope.x;} n  bool operator !=(const mint &ope){return x != ope.x;} n }; n mint modpow(mint a, ll n){ n  if(n == 0) return mint(1); n  if(n % 2) return a * modpow(a, n-1); n  else return modpow(a*a, n/2); n } n istream& operator >>(istream &is, mint &ope){ n  ll t; is >> t, ope.x = t; n  return is; n } n ostream& operator <<(ostream &os, mint &ope){return os << ope.x;} n ostream& operator <<(ostream &os, const mint &ope){return os << ope.x;} n  n bool exceed(ll x, ll y, ll m){return x >= m / y + 1;} n void mark(){ cout <<  *  << endl; } n void yes(){ cout <<  Yes  << endl; } n void no(){ cout <<  No  << endl; } n ll gcd(ll a, ll b){if(b == 0) return a; return gcd(b, a%b);} n ll digitnum(ll x, ll b = 10){ll ret = 0; for(; x; x /= b) ret++; return ret;} n ll digitsum(ll x, ll b = 10){ll ret = 0; for(; x; x /= b) ret += x % b; return ret;} n  n template<typename T> n ostream& operator << (ostream& os, vector<T>& vec) { n  for(int i = 0; i < vec.size(); i++) { n   os << vec[i] << (i + 1 == vec.size() ?    :    ); n  } n  return os; n } n template<typename T> n ostream& operator << (ostream& os, deque<T>& deq) { n  for(int i = 0; i < deq.size(); i++) { n   os << deq[i] << (i + 1 == deq.size() ?    :    ); n  } n  return os; n } n template<typename T, typename U> n ostream& operator << (ostream& os, pair<T, U>& pair_var) { n  os <<  (  << pair_var.first <<  ,   << pair_var.second <<  ) ; n  return os; n } n template<typename T, typename U> n ostream& operator << (ostream& os, const pair<T, U>& pair_var) { n  os <<  (  << pair_var.first <<  ,   << pair_var.second <<  ) ; n  return os; n } n template<typename T, typename U> n ostream& operator << (ostream& os, map<T, U>& map_var) { n  for(typename map<T, U>::iterator itr = map_var.begin(); itr != map_var.end(); itr++) { n   os <<  (  << itr->first <<  ,   << itr->second <<  ) ; n   itr++; n   if(itr != map_var.end()) os <<  , ; n   itr--; n  } n  return os; n } n template<typename T> n ostream& operator << (ostream& os, set<T>& set_var) { n  for(typename set<T>::iterator itr = set_var.begin(); itr != set_var.end(); itr++) { n   os << *itr; n   ++itr; n   if(itr != set_var.end()) os <<    ; n   itr--; n  } n  return os; n } n template<typename T> n ostream& operator << (ostream& os, multiset<T>& set_var) { n  for(typename multiset<T>::iterator itr = set_var.begin(); itr != set_var.end(); itr++) { n   os << *itr; n   ++itr; n   if(itr != set_var.end()) os <<    ; n   itr--; n  } n  return os; n } n template<typename T> n void outa(T a[], ll s, ll t) n { n  for(ll i = s; i <= t; i++){ cout << a[i]; if(i < t) cout <<    ;} n  cout << endl; n } n void dump_func() {cout << endl;} n template <class Head, class... Tail> n void dump_func(Head &&head, Tail &&... tail) { n  cout << head; n  if(sizeof...(Tail) > 0) cout <<    ; n  dump_func(std::move(tail)...); n } n  n struct SegTree{ n  typedef ll SEG; n   n  int size; n  vector<SEG> seg; n   n  SegTree(){} n  SegTree(int size){ n   this->size = size; n   seg.resize(1<<(size+1)); n  } n   n  SEG Ident(){  //identity element n   return inf; n  } n  SEG ope(SEG a, SEG b){ //operator n   return min(a, b); n  } n   n  void init() n  { n   for(int i = 0; i < (1<<(size+1)); i++) seg[i] = Ident(); n  } n   n  void update(int i, SEG val) n  { n   i += (1 << size); n   seg[i] = val; n   while(i > 1){ n    i /= 2; n    seg[i] = ope(seg[i*2], seg[i*2+1]); n   } n  } n  n  SEG query(int a, int b, int k, int l, int r) n  { n   if(b < l || r < a) return Ident(); n   if(a <= l && r <= b) return seg[k]; n   SEG lval = query(a, b, k*2, l, (l+r)/2); n   SEG rval = query(a, b, k*2+1, (l+r)/2+1, r); n   return ope(lval, rval); n  } n  SEG query(int a, int b) n  { n   if(a > b) return Ident(); n   return query(a, b, 1, 0, (1<<size)-1); n  } n }; n  n ll n, m; n ll a[200005], b[200005]; n vector<ll> comp; n vector<P> vec[400005], qvec[400005]; n SegTree seg(19); n  n int main(void) n { n  ios::sync_with_stdio(0); n  cin.tie(0); n   n  cin >> n; n  rep(i, 1, n) cin >> a[i]; n  rep(i, 1, n) cin >> b[i]; n   n  rep(i, 1, n) comp.push_back(a[i]), comp.push_back(b[i]); n  sort(all(comp)); n  comp.erase(unique(all(comp)), comp.end()); n  rep(i, 1, n){ n   a[i] = lower_bound(all(comp), a[i]) - comp.begin(); n   b[i] = lower_bound(all(comp), b[i]) - comp.begin(); n  } n  m = sz(comp); n   n  ll ans = 0; n   n  rep(i, 0, m) vec[i].clear(), qvec[i].clear(); //++ n  rep(i, 1, n){ n   ll A = comp[a[i]], B = comp[b[i]]; n   vec[a[i]].push_back(P(b[i], A - B - abs(A-B))); n   qvec[b[i]].push_back(P(a[i], A - B - abs(A-B))); n  } n  seg.init(); n  for(int i = m; i >= 0; i--){ n   for(auto p : vec[i]){ n    ll x = seg.query(p.first, p.first); n    chmin(x, p.second); n    seg.update(p.first, x); n   } n   for(auto p : qvec[i]){ n    chmin(ans, seg.query(0, p.first) + p.second); n   } n  } n   n  rep(i, 0, m) vec[i].clear(), qvec[i].clear(); //+- n  rep(i, 1, n){ n   ll A = comp[a[i]], B = comp[b[i]]; n   vec[a[i]].push_back(P(b[i], - A - B - abs(A-B))); n   qvec[b[i]].push_back(P(a[i], A + B - abs(A-B))); n  } n  seg.init(); n  for(int i = 0; i <= m; i++){ n   for(auto p : vec[i]){ n    ll x = seg.query(p.first, p.first); n    chmin(x, p.second); n    seg.update(p.first, x); n   } n   for(auto p : qvec[i]){ n    chmin(ans, seg.query(0, p.first) + p.second); n   } n  } n   n  rep(i, 0, m) vec[i].clear(), qvec[i].clear(); //-- n  rep(i, 1, n){ n   ll A = comp[a[i]], B = comp[b[i]]; n   vec[a[i]].push_back(P(b[i], - A + B - abs(A-B))); n   qvec[b[i]].push_back(P(a[i], - A + B - abs(A-B))); n  } n  seg.init(); n  for(int i = 0; i <= m; i++){ n   for(auto p : vec[i]){ n    ll x = seg.query(p.first, p.first); n    chmin(x, p.second); n    seg.update(p.first, x); n   } n   for(auto p : qvec[i]){ n    chmin(ans, seg.query(p.first, m) + p.second); n   } n  } n   n  rep(i, 1, n) ans += abs(comp[a[i]]-comp[b[i]]); n  outl(ans); n   n  return 0; n }


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


#include <bits/stdc++.h> n using namespace std; n int n, a, b; n int x[105], y[105]; n int ans; n bool judge(int x1, int x2, int y1, int y2) { n   if (x1 + x2 <= a && max(y1, y2) <= b) { n     return 1; n   } n   if (x1 + x2 <= b && max(y1, y2) <= a) { n     return 1; n   } n   if (x1 + y2 <= a && max(y1, x2) <= b) { n     return 1; n   } n   if (x1 + y2 <= b && max(y1, x2) <= a) { n     return 1; n   } n   if (y1 + x2 <= a && max(x1, y2) <= b) { n     return 1; n   } n   if (y1 + x2 <= b && max(x1, y2) <= a) { n     return 1; n   } n   if (y1 + y2 <= a && max(x1, x2) <= b) { n     return 1; n   } n   if (y1 + y2 <= b && max(x1, x2) <= a) { n     return 1; n   } n   return 0; n } n int main() { n   int i, j; n   cin >> n >> a >> b; n   for (i = 1; i <= n; i++) { n     cin >> x[i] >> y[i]; n   } n   for (i = 1; i <= n; i++) { n     for (j = 1; j < i; j++) { n       if (judge(x[i], x[j], y[i], y[j]) == 1) { n         ans = max(ans, x[i] * y[i] + x[j] * y[j]); n       } n     } n   } n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n inline long long read() { n   char i = getchar(); n   long long f = 1, res = 0; n   while (i <  0  || i >  9 ) { n     if (i ==  - ) f = -1; n     i = getchar(); n   } n   while (i >=  0  && i <=  9 ) { n     res = res * 10 + i -  0 ; n     i = getchar(); n   } n   return f * res; n } n const int N = 50005; n int dp[N]; n int n, K; n int a[N], pre[N], pos[N]; n int maxn[N << 2], lazy[N << 2]; n inline void pushup(int rt) { maxn[rt] = max(maxn[rt << 1], maxn[rt << 1 | 1]); } n inline void pushdown(int rt) { n   if (lazy[rt]) { n     lazy[rt << 1] += lazy[rt]; n     lazy[rt << 1 | 1] += lazy[rt]; n     maxn[rt << 1] += lazy[rt]; n     maxn[rt << 1 | 1] += lazy[rt]; n     lazy[rt] = 0; n   } n } n inline void update(int L, int R, int l, int r, int rt) { n   if (L <= l && r <= R) { n     maxn[rt] += 1; n     lazy[rt] += 1; n     return; n   } n   int mid = (l + r) >> 1; n   pushdown(rt); n   if (L <= mid) update(L, R, l, mid, rt << 1); n   if (R > mid) update(L, R, mid + 1, r, rt << 1 | 1); n   pushup(rt); n } n inline int query(int L, int R, int l, int r, int rt) { n   if (L <= l && r <= R) { n     return maxn[rt]; n   } n   int mid = (l + r) >> 1; n   int res = 0; n   pushdown(rt); n   if (L <= mid) res = max(res, query(L, R, l, mid, rt << 1)); n   if (R > mid) res = max(res, query(L, R, mid + 1, r, rt << 1 | 1)); n   return res; n } n inline void build(int l, int r, int rt) { n   lazy[rt] = 0; n   if (l == r) { n     maxn[rt] = dp[l - 1]; n     return; n   } n   int mid = (l + r) >> 1; n   build(l, mid, rt << 1); n   build(mid + 1, r, rt << 1 | 1); n   pushup(rt); n } n int main() { n   n = read(), K = read(); n   for (int i = 1; i <= n; ++i) { n     a[i] = read(); n     pos[i] = pre[a[i]] + 1; n     pre[a[i]] = i; n   } n   memset(pre, -1, sizeof pre); n   for (int i = 1; i <= K; ++i) { n     build(1, n, 1); n     for (int j = 1; j <= n; ++j) { n       update(pos[j], j, 1, n, 1); n       dp[j] = query(1, j, 1, n, 1); n     } n   } n   cout << dp[n] << endl; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T> n bool chkmin(T &first, T second) { n   return second < first ? first = second, 1 : 0; n } n template <typename T> n bool chkmax(T &first, T second) { n   return first < second ? first = second, 1 : 0; n } n template <typename T> n void readint(T &first) { n   first = 0; n   int f = 1; n   char c; n   for (c = getchar(); !isdigit(c); c = getchar()) n     if (c ==  - ) f = -1; n   for (; isdigit(c); c = getchar()) first = first * 10 + (c -  0 ); n   first *= f; n } n const int MOD = 1000000007, INV3 = (MOD + 1) / 3; n inline int dmy(int first) { return first >= MOD ? first - MOD : first; } n inline void inc(int &first, int second) { first = dmy(first + second); } n int qmi(int first, int second) { n   int ans = 1; n   for (; second; second >>= 1, first = 1ll * first * first % MOD) n     if (second & 1) ans = 1ll * ans * first % MOD; n   return ans; n } n const int MAXN = 6005; n int n; n int cnt[4][4]; n int main() { n   readint(n); n   for (int i = 1, first, second; i <= n; ++i) n     readint(first), readint(second), ++cnt[first & 3][second & 3]; n   long long res = 0; n   for (int i = 0; i <= 3; ++i) n     for (int j = 0; j <= 3; ++j) n       res += 1ll * cnt[i][j] * (cnt[i][j] - 1) / 2 * (n - cnt[i][j]) + n              1ll * cnt[i][j] * (cnt[i][j] - 1) * (cnt[i][j] - 2) / 6; n   cout << res << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, head[100000 + 5], sz[100000 + 5], cnt = 1; n double ans[100000 + 5]; n struct edges { n   int to, next; n } e[2 * 100000 + 5]; n inline void insert(int x) { n   int u = x, v; n   scanf( %d , &v); n   e[++cnt] = (edges){u, head[v]}; n   head[v] = cnt; n   e[++cnt] = (edges){v, head[u]}; n   head[u] = cnt; n } n void dfs(int x, int fa) { n   sz[x] = 1; n   for (int i = head[x]; i; i = e[i].next) { n     if (e[i].to == fa) continue; n     dfs(e[i].to, x); n     sz[x] += sz[e[i].to]; n   } n } n void dfs1(int x, int fa) { n   for (int i = head[x]; i; i = e[i].next) { n     if (e[i].to == fa) continue; n     ans[e[i].to] = 1.0 * (sz[x] - 1 - sz[e[i].to]) / 2.0 + 1.0 + ans[x]; n     dfs1(e[i].to, x); n   } n } n int main() { n   scanf( %d , &n); n   for (int i = 2; i <= n; i++) insert(i); n   dfs(1, 0); n   ans[1] = 1; n   dfs1(1, 0); n   for (int i = 1; i <= n; i++) printf( %.7lf n , ans[i]); n   return 0; n } n


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


#include <bits/stdc++.h> n using namespace std; n struct segment { n   int l, r, fu; n   bool operator<(const segment &temp) const { return l < temp.l; } n }; n segment sg[5000]; n int cnt_sg; n const int inf = 1e9; n int xx[400], yy[400]; n int cnt_x, cnt_y; n struct rectangle { n   int lx, ly, rx, ry; n }; n rectangle rec[99]; n int n, m, k, cnt, mc[1111][1111], nc[1111][11111], list[99], cnt_list, ls[99], n     cnt_ls; n long long ans, now; n char str[1111][1111]; n bool vsx[400], vsy[400]; n int main() { n   int i, j, s, p, q, id, dx1, dx2, dy1, dy2, lx, ly, rx, ry, pn; n   scanf( %d%d%d , &n, &m, &k); n   for (i = 0; i < n; i++) scanf( %s , str[i]); n   cnt = 0; n   for (i = 0; i < n; i++) n     for (j = 0; j < m; j++) { n       if (str[i][j] ==  *  && !(i > 0 && str[i - 1][j] ==  * ) && n           !(j > 0 && str[i][j - 1] ==  * )) { n         rec[cnt].lx = i; n         rec[cnt].ly = j; n         for (s = i; s < n; s++) { n           for (p = j; p < m; p++) { n             if (str[s][p] !=  * ) break; n           } n           if (p == j) break; n           rec[cnt].ry = p - 1; n         } n         rec[cnt++].rx = s - 1; n       } n     } n   memset(mc, 0, sizeof(mc)); n   memset(nc, 0, sizeof(nc)); n   for (i = 0; i < k; i++) mc[rec[i].lx][rec[i].ly]++; n   for (i = 0; i < n; i++) n     for (j = 0; j < m; j++) { n       mc[i + 1][j] += mc[i][j]; n       mc[i][j + 1] += mc[i][j]; n       mc[i + 1][j + 1] -= mc[i][j]; n     } n   cnt_x = cnt_y = 0; n   for (i = 0; i < k; i++) { n     xx[cnt_x++] = rec[i].lx; n     xx[cnt_x++] = rec[i].rx; n     if (rec[i].lx - 1 >= 0) xx[cnt_x++] = rec[i].lx - 1; n     if (rec[i].rx + 1 < n) xx[cnt_x++] = rec[i].rx + 1; n     yy[cnt_y++] = rec[i].ly; n     yy[cnt_y++] = rec[i].ry; n     if (rec[i].ly - 1 >= 0) yy[cnt_y++] = rec[i].ly - 1; n     if (rec[i].ry + 1 < m) yy[cnt_y++] = rec[i].ry + 1; n   } n   sort(xx, xx + cnt_x); n   sort(yy, yy + cnt_y); n   int nn = 0; n   for (i = 0; i < cnt_x; i++) { n     if (nn == 0 || xx[nn - 1] < xx[i]) xx[nn++] = xx[i]; n   } n   cnt_x = nn; n   nn = 0; n   for (i = 0; i < cnt_y; i++) { n     if (nn == 0 || yy[nn - 1] < yy[i]) yy[nn++] = yy[i]; n   } n   cnt_y = nn; n   ans = 0; n   for (i = 0; i < cnt_x; i++) { n     if (i == 0) n       dx1 = xx[i] + 1; n     else n       dx1 = xx[i] - xx[i - 1]; n     for (j = i + 1; j <= cnt_x; j++) { n       if (j == cnt_x) n         dx2 = n - xx[j - 1]; n       else n         dx2 = xx[j] - xx[j - 1]; n       cnt_sg = 0; n       for (s = 0; s < k; s++) { n         if (rec[s].rx < xx[i] || rec[s].lx > xx[j - 1]) continue; n         if ((rec[s].rx >= xx[i] && rec[s].lx < xx[i]) || n             (rec[s].rx > xx[j - 1] && rec[s].lx <= xx[j - 1])) n           sg[cnt_sg].fu = 1; n         else n           sg[cnt_sg].fu = 0; n         sg[cnt_sg].l = rec[s].ly; n         sg[cnt_sg++].r = rec[s].ry; n       } n       sort(sg, sg + cnt_sg); n       nn = 0; n       for (s = 0; s < cnt_sg; s++) { n         if (nn == 0 || sg[nn - 1].r < sg[s].l) n           sg[nn++] = sg[s]; n         else if (sg[nn - 1].r < sg[s].r) { n           sg[nn - 1].r = sg[s].r; n           sg[nn - 1].fu = max(sg[nn - 1].fu, sg[s].fu); n         } n       } n       cnt_sg = nn; n       for (s = 1; s <= cnt_sg; s++) { n         if (s < cnt_sg) n           dy2 = sg[s].l - sg[s - 1].r; n         else n           dy2 = m - sg[s - 1].r; n         pn = 0; n         for (p = s - 1; p >= 0; p--) { n           lx = xx[i]; n           ly = sg[p].l; n           rx = xx[j - 1]; n           ry = sg[s - 1].r; n           int rs = mc[rx][ry]; n           if (lx > 0) rs -= mc[lx - 1][ry]; n           if (ly > 0) rs -= mc[rx][ly - 1]; n           if (lx > 0 && ly > 0) rs += mc[lx - 1][ly - 1]; n           if (rs > 3 || sg[p].fu == 1) break; n           if (rs == 0) continue; n           if (p == 0) n             dy1 = sg[p].l + 1; n           else n             dy1 = sg[p].l - sg[p - 1].r; n           for (q = 0; q < k; q++) { n             if (rec[q].lx >= lx && rec[q].rx <= rx && rec[q].ly >= ly && n                 rec[q].ry <= ry) n               continue; n             if (rec[q].lx <= rx && rec[q].rx >= xx[i] && rec[q].ly <= ry && n                 rec[q].ry >= ly) n               break; n           } n           if (q >= k) pn += dy1; n         } n         ans += (long long)pn * (long long)dy2 * (long long)dx1 * (long long)dx2; n       } n     } n   } n   printf( %I64d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   double num, a, b, c, d, e, f, r1, r2; n   double m1, m2, m3; n   while (cin >> a >> b >> c >> d >> e >> f) { n     if (d == 0) { n       cout <<  Hermione  << endl; n       continue; n     } n     if (c == 0) { n       cout <<  Ron  << endl; n       continue; n     } n     if (b == 0) { n       cout <<  Hermione  << endl; n       continue; n     } n     if (a == 0) { n       cout <<  Ron  << endl; n       continue; n     } n     if (f != 0 && e == 0) { n       cout <<  Ron  << endl; n       continue; n     } n     num = 500; n     r1 = 0; n     r2 = 0; n     m1 = b / a; n     m2 = d / c; n     m3 = e / f; n     r1 = num * m1 * m2; n     r2 = num * m3; n     if (r1 > r2) { n       cout <<  Ron  << endl; n     } else { n       cout <<  Hermione  << endl; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n using ll = long long; n using ii = pair<ll, ll>; n using vi = vector<ll>; n using vii = vector<ii>; n double x; n int main() { n   cin >> x; n   for (double a = 1; a <= 10; a++) { n     for (double h = 1; h <= 10; h++) { n       double cr = -h * a / 2; n       double r = -cr / sqrt(a * a / 4 + h * h); n       if (fabs(r - x) < 1e-5) { n         cout << a <<     << h << endl; n         return 0; n       } n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, m; n   scanf( %d , &n); n   scanf( %d , &m); n   int f[n], b[m], a[m]; n   map<int, int> x; n   for (int i = 0; i < n; i++) { n     scanf( %d , &f[i]); n     if (x.find(f[i]) != x.end()) n       x[f[i]] = -1; n     else n       x[f[i]] = i; n   } n   for (int i = 0; i < m; i++) scanf( %d , &b[i]); n   sort(f, f + n); n   bool y = 0, z = 0; n   for (int i = 0; i < m; i++) { n     if (x.find(b[i]) != x.end()) { n       if (x[b[i]] == -1) { n         z = 1; n       } else { n         a[i] = x[b[i]]; n       } n     } else { n       printf( Impossible ); n       y = 1; n       break; n     } n   } n   if (!y) { n     if (z) { n       printf( Ambiguity ); n     } else { n       printf( Possible n ); n       for (int i = 0; i < m; i++) { n         printf( %d , a[i] + 1); n         printf(   ); n       } n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int MXN = 1e5 + 20; n int n, a[MXN], b[MXN], c[MXN]; n vector<int> ans; n void IN() { n   int tmp; n   cin >> n; n   for (int i = 0; i < n; i++) { n     cin >> tmp; n     a[i] = tmp; n   } n   for (int i = 0; i < n - 1; i++) { n     cin >> tmp; n     b[i] = tmp; n   } n   for (int i = 0; i < n - 2; i++) { n     cin >> tmp; n     c[i] = tmp; n   } n } n void SLV() { n   sort(a, a + n); n   sort(b, b + n - 1); n   sort(c, c + n - 2); n   for (int i = 0; i < n; i++) n     if (b[i] != a[i]) { n       ans.push_back(a[i]); n       break; n     } n   for (int i = 0; i < n; i++) n     if (c[i] != b[i]) { n       ans.push_back(b[i]); n       break; n     } n } n void OUT() { n   for (auto i : ans) cout << i << endl; n } n int main() { n   IN(); n   SLV(); n   OUT(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n using matrix = vector<vector<long long>>; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(nullptr); n   int x, l, n, q; n   cin >> x >> l >> n >> q; n   int log_n = 0; n   while ((1 << log_n) <= n) { n     ++log_n; n   } n   vector<int> c(l + 1); n   for (int i = 1; i <= l; ++i) { n     cin >> c[i]; n   } n   map<int, int> stones; n   vector<int> stops = {n - x}; n   for (int i = 0; i < q; ++i) { n     int p, w; n     cin >> p >> w; n     stones[p] = w; n     for (int j = 1; j <= l; ++j) { n       stops.push_back(p - j); n     } n   } n   sort(stops.begin(), stops.end()); n   while (stops.back() > n - x) { n     stops.pop_back(); n   } n   vector<matrix> pow_matrix(log_n); n   pow_matrix[0].assign(1 << l, vector<long long>(1 << l, LLONG_MAX / 2)); n   for (int i = 0; i < (1 << l); ++i) { n     if ((i & 1) == 0) { n       pow_matrix[0][i][i >> 1] = 0; n     } else { n       for (int j = 1; j <= l; ++j) { n         if ((i & (1 << j)) == 0) { n           pow_matrix[0][i][(i ^ (1 << j)) >> 1] = c[j]; n         } n       } n     } n   } n   auto mul = [](const matrix& a, const matrix& b) -> matrix { n     int n = a.size(); n     matrix c(n, vector<long long>(n, LLONG_MAX / 2)); n     for (int i = 0; i < n; ++i) { n       for (int j = 0; j < n; ++j) { n         for (int k = 0; k < n; ++k) { n           c[i][k] = min(c[i][k], a[i][j] + b[j][k]); n         } n       } n     } n     return c; n   }; n   for (int i = 1; i < log_n; ++i) { n     pow_matrix[i] = mul(pow_matrix[i - 1], pow_matrix[i - 1]); n   } n   auto mul_vector = [](const matrix& a, n                        const vector<long long>& b) -> vector<long long> { n     int n = a.size(); n     vector<long long> c(n, LLONG_MAX / 2); n     for (int i = 0; i < n; ++i) { n       for (int j = 0; j < n; ++j) { n         c[j] = min(c[j], a[i][j] + b[i]); n       } n     } n     return c; n   }; n   vector<long long> dp(1 << l, LLONG_MAX / 2); n   dp[(1 << x) - 1] = 0; n   int pos = 1; n   for (auto i : stops) { n     if (i < pos) { n       continue; n     } n     for (int j = log_n - 1; j >= 0; --j) { n       if (pos + (1 << j) <= i) { n         dp = mul_vector(pow_matrix[j], dp); n         pos += 1 << j; n       } n     } n     vector<long long> dp_new(1 << l, LLONG_MAX / 2); n     for (int j = 0; j < (1 << l); ++j) { n       if ((j & 1) == 0) { n         dp_new[j >> 1] = min(dp_new[j >> 1], dp[j]); n       } else { n         for (int k = 1; k <= l; ++k) { n           if ((j & (1 << k)) == 0) { n             long long cost = dp[j] + c[k]; n             if (stones.count(pos + k)) { n               cost += stones[pos + k]; n             } n             int mask = (j ^ (1 << k)) >> 1; n             dp_new[mask] = min(dp_new[mask], cost); n           } n         } n       } n     } n     dp = dp_new; n     ++pos; n   } n   cout << dp[(1 << x) - 1] <<   n ; n } n


#include <bits/stdc++.h> n double eps = 1e-6; n using namespace std; n int main() { n   ios_base::sync_with_stdio(false); n   int n; n   long long p, a[100010], b[100010]; n   long long sum = 0; n   cin >> n >> p; n   for (int i = 0; i < n; ++i) { n     cin >> a[i] >> b[i]; n     sum += a[i]; n   } n   if (p >= sum) { n     cout << -1 << endl; n     return 0; n   } n   double lo = 0; n   double hi = 1e17; n   double mx = 0; n   for (int var = 0; var < 100; ++var) { n     mx = lo + (hi - lo) / 2; n     double need = 0; n     for (int i = 0; i < n; ++i) { n       need += max(mx * a[i] - b[i], 0 * 1.0); n     } n     if (p * mx >= need) { n       lo = mx + eps; n     } else { n       hi = mx - eps; n     } n   } n   printf( %.9f n , lo); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long n, m, f[1050000], a[1050000], b[1050000], mp[21][100005], ans; n void fwt(long long a[], long long n, long long f) { n   for (long long i = 1; i < 1 << n; i <<= 1) { n     for (long long t = 0; t < 1 << n; t++) { n       if (i & t) { n         long long x = a[t - i], y = a[t]; n         a[t - i] = x + y; n         a[t] = x - y; n       } n     } n   } n   if (f == -1) { n     for (long long i = 0; i < 1 << n; i++) a[i] = a[i] / (1 << n); n   } n } n long long get(long long x) { n   long long num = 0; n   while (x) { n     if (x % 2 == 1) num++; n     x /= 2; n   } n   return num; n } n int main() { n   scanf( %lld%lld , &n, &m); n   ans = n * m; n   f[1] = 1; n   for (long long i = 2; i <= n; i++) { n     f[i] = f[i - 1] * 2; n   } n   for (long long i = 1; i <= n; i++) { n     for (long long t = 1; t <= m; t++) { n       char s; n       cin >> s; n       if (s ==  1 ) n         mp[i][t] = 1; n       else n         mp[i][t] = 0; n     } n   } n   for (long long i = 1; i <= m; i++) { n     long long x = 0; n     for (long long t = 1; t <= n; t++) { n       if (mp[t][i] == 1) x += f[t]; n     } n     a[x]++; n   } n   for (long long i = 0; i < 1 << n; i++) { n     long long x = get(i); n     b[i] = min(x, n - x); n   } n   fwt(a, n, 1); n   fwt(b, n, 1); n   for (long long i = 0; i < 1 << n; i++) { n     a[i] = a[i] * b[i]; n   } n   fwt(a, n, -1); n   for (long long i = 0; i < 1 << n; i++) { n     ans = min(ans, a[i]); n   } n   printf( %lld , ans); n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios::sync_with_stdio(false); n   cin.tie(NULL); n   int n, sum = 0; n   while (cin >> n) { n     for (int i = 0; i < (n); ++i) { n       int x; n       cin >> x; n       sum += x; n     } n     if (sum % n) n       cout << n - 1 <<   n ; n     else n       cout << n <<   n ; n   } n   return 0; n } n


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


#include <bits/stdc++.h> n using namespace std; n long long int mod = 1e9 + 7; n long long int mxm = 1e18; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   long long int t; n   cin >> t; n   while (t--) { n     long long int n; n     cin >> n; n     long long int a[n], i; n     set<long long int> x; n     for (i = 0; i < n; i++) { n       cin >> a[i]; n       x.insert(i + 1); n     } n     sort(a, a + n); n     for (i = 0; i < n; i++) { n       if (x.count(a[i])) { n         x.erase(a[i]); n         a[i] = 0; n       } n     } n     long long int ans = 0; n     sort(a, a + n); n     for (i = 0; i < n; i++) { n       if (a[i] > 0) { n         if (2 * (*x.begin()) < a[i]) { n           x.erase(x.begin()); n           ans++; n         } else { n           ans = -1; n           break; n         } n       } n     } n     cout << (ans) << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, d; n   while (cin >> n >> d) { n     int A[n], ans = 0; n     for (int i = 0; i < n; i++) cin >> A[i]; n     for (int i = 1; i < n; i++) { n       if (A[i] <= A[i - 1]) { n         int rest = A[i - 1] - A[i], cnt = (rest / d) + 1; n         A[i] += cnt * d; n         ans += cnt; n       } n     } n     cout << ans << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int inf = (int)1e9; n const long long INF = (long long)5e18; n const int MOD = 998244353; n int add(int x, int y) { n   x += y; n   return x >= MOD ? x - MOD : x; n } n int sub(int x, int y) { n   x -= y; n   return x < 0 ? x + MOD : x; n } n int qpow(int x, int y) { n   int ret = 1; n   while (y) { n     if (y & 1) ret = (long long)(ret) * (x) % MOD; n     x = (long long)(x) * (x) % MOD; n     y >>= 1; n   } n   return ret; n } n void checkmin(int &x, int y) { n   if (x > y) x = y; n } n void checkmax(int &x, int y) { n   if (x < y) x = y; n } n void checkmin(long long &x, long long y) { n   if (x > y) x = y; n } n void checkmax(long long &x, long long y) { n   if (x < y) x = y; n } n inline int read() { n   int x = 0, f = 1; n   char c = getchar(); n   while (c >  9  || c <  0 ) { n     if (c ==  - ) f = -1; n     c = getchar(); n   } n   while (c >=  0  && c <=  9 ) n     x = (x << 1) + (x << 3) + (c ^ 48), c = getchar(); n   return x * f; n } n const int N = 5005; n int n, a[N], dp[N]; n int fac[N], ifac[N], lim[N]; n void init() { n   scanf( %d , &n); n   for (int i = 1; i <= n; i++) scanf( %d , &a[i]); n   sort(a + 1, a + n + 1); n   fac[0] = 1; n   for (int i = 1; i <= n; i++) fac[i] = (long long)(fac[i - 1]) * (i) % MOD; n   ifac[n] = qpow(fac[n], MOD - 2); n   for (int i = n - 1; i >= 0; i--) n     ifac[i] = (long long)(ifac[i + 1]) * (i + 1) % MOD; n   for (int i = 1; i <= n; i++) { n     for (int j = i - 1; j >= 1; j--) { n       if (a[j] * 2 <= a[i]) { n         lim[i] = j; n         break; n       } n     } n   } n } n void solve() { n   dp[0] = 1; n   if (a[n] < a[n - 1] * 2) { n     puts( 0 ); n     return; n   } n   for (int i = 1; i <= n; i++) { n     for (int j = lim[i]; j >= 0; j--) { n       int delta = lim[i] - lim[j] - 1, rest = n - lim[j] - 2; n       if (j == 0) delta++, rest++; n       dp[i] = n           add(dp[i], (long long)(dp[j]) * n                          ((long long)(fac[rest]) * (ifac[rest - delta]) % MOD) % n                          MOD); n     } n   } n   cout << dp[n] << endl; n } n int main() { n   init(); n   solve(); n   return 0; n } n


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


#include <bits/stdc++.h> n using namespace std; n long long gcd(long long a, long long b) { return b == 0 ? a : gcd(b, a % b); } n long long fpow(long long b, long long p) { n   if (b == 0) return 0; n   long long ans = 1; n   while (p) { n     if (p & 1) ans = ans * b % (998244353); n     b = b * b % (998244353); n     p /= 2; n   } n   return ans; n } n struct BIT { n   long long n; n   long long a[150050]; n   void init(long long _n) { n     n = _n; n     memset(a, 0, sizeof(a)); n   } n   void add(long long i, long long x) { n     while (i <= n) { n       a[i] += x; n       if (a[i] >= (998244353)) a[i] -= (998244353); n       i += (i & (-i)); n     } n   } n   long long ask(long long l, long long r) { n     long long ans = 0; n     while (r > 0) { n       ans += a[r]; n       r -= (r & (-r)); n     } n     l--; n     while (l > 0) { n       ans -= a[l]; n       l -= (l & (-l)); n     } n     return ans % (998244353); n   } n } bit; n long long N, N1; n long long addall = 0; n vector<long long> adj[150010]; n long long sz[150010]; n long long p[150010]; n long long nxt[150010]; n long long id[150010]; n long long head[150010]; n long long add[150010]; n long long info(long long n, long long par) { n   sz[n] = 1; n   p[n] = par; n   pair<long long, long long> mx(0, 0); n   for (auto &i : adj[n]) n     if (i != par) { n       mx = max(mx, make_pair(info(i, n), i)); n       sz[n] += sz[i]; n     } n   nxt[n] = mx.second; n   return sz[n]; n } n void hld(long long n, long long h) { n   static long long nowid = 1; n   id[n] = nowid; n   nowid++; n   head[n] = h; n   if (nxt[n] != 0) hld(nxt[n], h); n   for (auto &i : adj[n]) n     if (i != p[n] && i != nxt[n]) { n       hld(i, i); n     } n } n long long ask(long long n) { n   long long ans = addall + add[n]; n   while (n != 0) { n     ans += bit.ask(id[head[n]], id[n]); n     n = head[n]; n     ans = (ans + add[p[n]] * (N - sz[n]) % (998244353) * N1) % (998244353); n     n = p[n]; n   } n   return ans; n } n signed main() { n   { n     ; n     ; n     ; n     ios::sync_with_stdio(false); n     cin.tie(0); n     ; n     ; n     ; n   } n   cin >> N; n   long long q; n   cin >> q; n   N1 = fpow(N, (998244353) - 2); n   for (long long i = 1; i <= N - 1; i++) { n     long long a, b; n     cin >> a >> b; n     adj[a].emplace_back(b); n     adj[b].emplace_back(a); n   } n   bit.init(N); n   info(1, 0); n   hld(1, 1); n   while (q--) { n     long long op; n     cin >> op; n     if (op == 1) { n       long long v, x; n       cin >> v >> x; n       addall = (addall + x * sz[v] % (998244353) * N1) % (998244353); n       add[v] = (add[v] + x) % (998244353); n       bit.add(id[v], -x * sz[v] % (998244353) * N1 % (998244353)); n       if (nxt[v] != 0) { n         bit.add(id[nxt[v]], n                 x * (N - sz[nxt[v]]) % (998244353) * N1 % (998244353)); n       } n     } else { n       long long v; n       cin >> v; n       long long ans = addall + add[v]; n       while (v != 0) { n         ans += bit.ask(id[head[v]], id[v]); n         v = head[v]; n         ans = (ans + add[p[v]] * (N - sz[v]) % (998244353) * N1) % (998244353); n         v = p[v]; n       } n       cout << (ans + (998244353)) % (998244353) <<   n ; n     } n   } n   return 0; n } n