#include <bits/stdc++.h> n using namespace std; n int tree[4000009][3]; n int a[1000009]; n void init(int node, int b, int e) { n   if (b == e) { n     tree[node][a[b]] = 1; n     tree[node][1 - a[b]] = 0; n     tree[node][2] = 0; n     return; n   } n   int mid = (b + e) / 2; n   int left = node * 2; n   int right = node * 2 + 1; n   init(left, b, mid); n   init(right, mid + 1, e); n   tree[node][2] = min(tree[left][0], tree[right][1]); n   tree[node][0] = tree[left][0] - tree[node][2] + tree[right][0]; n   tree[node][1] = tree[left][1] + tree[right][1] - tree[node][2]; n   tree[node][2] += tree[left][2] + tree[right][2]; n } n pair<int, pair<int, int>> query(int node, int b, int e, int l, int r) { n   if (l > e || r < b) return {0, {0, 0}}; n   if (b >= l && e <= r) return {tree[node][2], {tree[node][0], tree[node][1]}}; n   int mid = b + e >> 1; n   int left = node * 2; n   int right = node * 2 + 1; n   pair<int, pair<int, int>> p1 = query(left, b, mid, l, r); n   pair<int, pair<int, int>> p2 = query(right, mid + 1, e, l, r); n   int mn = min(p1.second.first, p2.second.second); n   int tot = p1.first + p2.first + mn; n   int lf = p1.second.first + p2.second.first - mn; n   int rf = p1.second.second + p2.second.second - mn; n   return {tot, {lf, rf}}; n } n int main() { n   string s; n   cin >> s; n   int n = s.length(); n   for (int i = 0; i < n; i++) { n     if (s[i] ==  ( ) n       a[i + 1] = 0; n     else n       a[i + 1] = 1; n   } n   int q; n   cin >> q; n   init(1, 1, n); n   while (q--) { n     int l, r; n     cin >> l >> r; n     cout << 2 * query(1, 1, n, l, r).first << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n long long int n, tmp; n long long int k, c = 0; n list<int> game; n int main() { n   cin >> n >> k; n   for (int i = 0; i < n; i++) { n     cin >> tmp; n     game.push_back(tmp); n   } n   while (*game.begin() != n) { n     if (*game.begin() > *(++game.begin())) { n       tmp = *(++game.begin()); n       game.erase(++game.begin()); n       game.push_back(tmp); n       c++; n     } else { n       tmp = *game.begin(); n       game.erase(game.begin()); n       game.push_back(tmp); n       c = 1; n     } n     if (c == k) { n       cout << *game.begin(); n       return 0; n     } n   } n   cout << n; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n char s[200003]; n long long k; n struct pos { n   char c; n   int nom; n }; n pos a[100003]; n pos b[100003]; n pos c[100003]; n void mergesort(pos *a, pos *b, int n) { n   int i, i1, i0, iRight, iEnd, width, j; n   for (width = 1; width < n; width *= 2) n     for (i = 0; i < n; i += width * 2) { n       i0 = i; n       i1 = iRight = min(i + width, n); n       iEnd = min(i + width * 2, n); n       for (j = i; j < iEnd; j++) n         if (i0 < iRight && (i1 == iEnd || a[i0].c <= a[i1].c)) n           b[j] = a[i0++]; n         else n           b[j] = a[i1++]; n       for (j = i; j < iEnd; j++) a[j] = b[j]; n     } n } n int main() { n   scanf( %s , s); n   scanf( %I64d , &k); n   int i, j, jj; n   for (i = 0; s[i]; i++) { n     a[i].nom = i; n     a[i].c = s[i]; n   } n   long long n = i; n   if (k > ((n * (n + 1)) >> 1)) { n     printf( No such line. n ); n     return 0; n   } n   long long sum; n   long long add; n   int coun = n; n   while (k > 0) { n     mergesort(a, c, coun); n     sum = 0; n     i = 0; n     while (i < coun) { n       add = 0; n       j = i; n       while (j < coun && a[j].c == a[i].c) j++; n       for (jj = i; jj < j; jj++) add += n - a[jj].nom; n       if (sum + add >= k) break; n       sum += add; n       i = j; n     } n     printf( %c , a[i].c); n     k -= sum; n     k -= j - i; n     coun = 0; n     for (jj = i; jj < j; jj++) { n       if (a[jj].nom != n - 1) { n         b[coun].c = s[a[jj].nom + 1]; n         b[coun++].nom = a[jj].nom + 1; n       } n     } n     for (jj = 0; jj < coun; jj++) a[jj] = b[jj]; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int n; n int a[200001]; n int b[200001]; n int main() { n   cin >> n; n   int tong = 0; n   int i; n   for (i = 1; i <= n; i++) cin >> a[i], tong += a[i]; n   int k = 1; n   i = 1; n   bool ok = true; n   while (i <= n) { n     if (tong % n == 0 && a[i] == tong / n) { n       b[k] = i; n       k++; n       ok = false; n     } n     i++; n   }; n   if (ok) { n     cout << 0 << endl; n     return 0; n   } n   cout << k - 1 << endl; n   for (i = 1; i < k; i++) cout << b[i] <<    ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct SEG { n   int segment_length; n   char chars; n }; n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   ; n   string s; n   cin >> s; n   SEG cur; n   vector<SEG> st; n   cur.segment_length = 0; n   cur.chars =  - ; n   for (char x : s) { n     if (x == cur.chars) { n       cur.segment_length++; n     } else { n       if (cur.segment_length > 0) st.push_back(cur); n       cur.segment_length = 1; n       cur.chars = x; n     } n   } n   st.push_back(cur); n   int ans = 0; n   int p; n   while (((int)(st).size()) > 1) { n     vector<SEG> effect; n     for (int x = 0; x < ((int)(st).size()); x++) { n       cur = st[x]; n       if (x == 0 or x == ((int)(st).size()) - 1) { n         cur.segment_length--; n       } else { n         cur.segment_length -= 2; n       } n       if (cur.segment_length <= 0) { n         continue; n       } n       if (effect.empty()) { n         effect.push_back(cur); n       } else { n         if (effect.back().chars == cur.chars) { n           effect.back().segment_length += cur.segment_length; n         } else { n           effect.push_back(cur); n         } n       } n     } n     st = effect; n     ans++; n   } n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long int n, s, min, max, l, r; n   cin >> n; n   s = pow(n, 2); n   long long int v[s + 1]; n   for (long long int i = 0; i < s; i++) { n     v[i] = i + 1; n   } n   for (long long int i = 0; i < n; i++) { n     l = i; n     r = n - 1; n     if (n % 2 == 0) { n       for (long long int j = 1; j <= n; j++) { n         if (j <= n / 2) { n           min = *min_element(v + l, v + r); n           cout << min <<    ; n           l = l + n; n           r = r + n; n         } n         if (j > n / 2) { n           max = *max_element(v + l - i, v + r - i + 1); n           cout << max <<    ; n           l = l + n; n           r = r + n; n         } n       } n     } n     if (n % 2 != 0) { n       for (long long int j = 1; j <= n; j++) { n         if (j <= (n / 2) + 1) { n           min = *min_element(v + l, v + r); n           cout << min <<    ; n           l = l + n; n           r = r + n; n         } n         if (j > (n / 2) + 1) { n           max = *max_element(v + l - i, v + r - i + 1); n           cout << max <<    ; n           l = l + n; n           r = r + n; n         } n       } n     } n     cout << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void solve() { n   string n; n   int s; n   cin >> n >> s; n   int sum = 0; n   vector<int> digit; n   for (int i = 0; i < (int)n.size(); i++) { n     digit.push_back(n[i] -  0 ); n     sum += digit[i]; n   } n   if (sum <= s) { n     cout << 0 <<   n ; n     return; n   } n   sum = 0; n   int pos = -1; n   for (int i = 0; i < (int)digit.size(); i++) { n     sum += digit[i]; n     if (sum >= s) { n       pos = i; n       s -= (sum - digit[i]); n       break; n     } n   } n   if (pos == 0) { n     digit.insert(digit.begin(), 1); n     pos++; n   } else { n     digit[pos - 1]++; n   } n   for (int i = pos; i < (int)digit.size(); i++) digit[i] = 0; n   long long num = 0; n   for (int i = 0; i < (int)digit.size(); i++) num = num * 10 + digit[i]; n   long long res = num - stoll(n); n   cout << res <<   n ; n } n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   int t; n   cin >> t; n   while (t--) { n     solve(); n   } n } n


#include <bits/stdc++.h> n using namespace std; n using LL = long long; n using LD = long double; n void err() { cerr <<   033[39;0m  << endl; } n template <typename T, typename... A> n void err(T a, A... x) { n   cerr << a <<    ; n   err(x...); n } n const int N = 1e6 + 10; n struct Node { n   map<int, Node*> link; n   Node* fa; n   int count; n } pool[N]; n int cnt; n Node* new_node(Node* fa) { n   Node* ret = pool + cnt++; n   ret->fa = fa; n   ret->link.clear(); n   ret->count = 0; n   return ret; n } n int stk[N], top; n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   int T; n   cin >> T; n   while (T--) { n     int n; n     cin >> n; n     top = 0; n     LL ans = 0; n     auto cur = new_node(nullptr); n     cur->count = 1; n     for (decay<decltype(n)>::type i = (0), _i = (n); i < _i; ++i) { n       int c; n       cin >> c; n       if (top == 0 || stk[top - 1] != c) { n         if (!cur->link[c]) { n           cur->link[c] = new_node(cur); n         } n         cur = cur->link[c]; n         ans += cur->count++; n         stk[top++] = c; n       } else { n         cur = cur->fa; n         ans += cur->count++; n         --top; n       } n     } n     cout << ans << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n using ll = long long; n using vi = vector<int>; n using vvi = vector<vi>; n using vll = vector<ll>; n using vvll = vector<vll>; n using vb = vector<bool>; n using vd = vector<double>; n using vs = vector<string>; n using pii = pair<int, int>; n using pll = pair<ll, ll>; n using pdd = pair<double, double>; n using vpii = vector<pii>; n using vvpii = vector<vpii>; n using vpll = vector<pll>; n using vvpll = vector<vpll>; n using vpdd = vector<pdd>; n using vvpdd = vector<vpdd>; n template <typename T> n void ckmin(T& a, const T& b) { n   a = min(a, b); n } n template <typename T> n void ckmax(T& a, const T& b) { n   a = max(a, b); n } n mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count()); n namespace __input { n template <class T1, class T2> n void re(pair<T1, T2>& p); n template <class T> n void re(vector<T>& a); n template <class T, size_t SZ> n void re(array<T, SZ>& a); n template <class T> n void re(T& x) { n   cin >> x; n } n void re(double& x) { n   string t; n   re(t); n   x = stod(t); n } n template <class Arg, class... Args> n void re(Arg& first, Args&... rest) { n   re(first); n   re(rest...); n } n template <class T1, class T2> n void re(pair<T1, T2>& p) { n   re(p.first, p.second); n } n template <class T> n void re(vector<T>& a) { n   for (int i = 0; i < (int((a).size())); i++) re(a[i]); n } n template <class T, size_t SZ> n void re(array<T, SZ>& a) { n   for (int i = 0; i < (SZ); i++) re(a[i]); n } n }  // namespace __input n using namespace __input; n namespace __output { n template <class T1, class T2> n void pr(const pair<T1, T2>& x); n template <class T, size_t SZ> n void pr(const array<T, SZ>& x); n template <class T> n void pr(const vector<T>& x); n template <class T> n void pr(const set<T>& x); n template <class T1, class T2> n void pr(const map<T1, T2>& x); n template <class T> n void pr(const T& x) { n   cout << x; n } n template <class Arg, class... Args> n void pr(const Arg& first, const Args&... rest) { n   pr(first); n   pr(rest...); n } n template <class T1, class T2> n void pr(const pair<T1, T2>& x) { n   pr( { , x.first,  ,  , x.second,  } ); n } n template <class T, bool pretty = true> n void prContain(const T& x) { n   if (pretty) pr( { ); n   bool fst = 1; n   for (const auto& a : x) pr(!fst ? pretty ?  ,   :     :   , a), fst = 0; n   if (pretty) pr( } ); n } n template <class T> n void pc(const T& x) { n   prContain<T, false>(x); n   pr(  n ); n } n template <class T, size_t SZ> n void pr(const array<T, SZ>& x) { n   prContain(x); n } n template <class T> n void pr(const vector<T>& x) { n   prContain(x); n } n template <class T> n void pr(const set<T>& x) { n   prContain(x); n } n template <class T1, class T2> n void pr(const map<T1, T2>& x) { n   prContain(x); n } n void ps() { pr(  n ); } n template <class Arg> n void ps(const Arg& first) { n   pr(first); n   ps(); n } n template <class Arg, class... Args> n void ps(const Arg& first, const Args&... rest) { n   pr(first,    ); n   ps(rest...); n } n }  // namespace __output n using namespace __output; n namespace __algorithm { n template <typename T> n void dedup(vector<T>& v) { n   sort((v).begin(), (v).end()); n   v.erase(unique((v).begin(), (v).end()), v.end()); n } n template <typename T> n typename vector<T>::iterator find(vector<T>& v, const T& x) { n   auto it = lower_bound((v).begin(), (v).end(), x); n   return it != v.end() && *it == x ? it : v.end(); n } n template <typename T> n size_t index(vector<T>& v, const T& x) { n   auto it = find(v, x); n   assert(it != v.end() && *it == x); n   return it - v.begin(); n } n template <typename C, typename T, typename OP> n vector<T> prefixes(const C& v, T id, OP op) { n   vector<T> r(int((v).size()) + 1, id); n   for (int i = 0; i < (int((v).size())); i++) r[i + 1] = op(r[i], v[i]); n   return r; n } n template <typename C, typename T, typename OP> n vector<T> suffixes(const C& v, T id, OP op) { n   vector<T> r(int((v).size()) + 1, id); n   for (int i = (int((v).size())) - 1; i >= 0; i--) r[i] = op(v[i], r[i + 1]); n   return r; n } n }  // namespace __algorithm n using namespace __algorithm; n struct monostate { n   friend istream& operator>>(istream& is, n                              const __attribute__((unused)) monostate& ms) { n     return is; n   } n   friend ostream& operator<<(ostream& os, n                              const __attribute__((unused)) monostate& ms) { n     return os; n   } n } ms; n template <typename W = monostate> n struct wedge { n   int u, v, i; n   W w; n   wedge<W>(int _u = -1, int _v = -1, int _i = -1) : u(_u), v(_v), i(_i) {} n   int operator[](int loc) const { return u ^ v ^ loc; } n   friend void re(wedge& e) { n     re(e.u, e.v, e.w); n     --e.u, --e.v; n   } n   friend void pr(const wedge& e) { pr(e.u,  <- , e.w,  -> , e.v); } n }; n namespace __io { n void setIn(string second) { freopen(second.c_str(),  r , stdin); } n void setOut(string second) { freopen(second.c_str(),  w , stdout); } n void setIO(string second =   ) { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout << fixed; n   cout.precision(15); n   if (int((second).size())) { n     setIn(second +  .in ), setOut(second +  .out ); n   } n } n }  // namespace __io n using namespace __io; n template <typename E> n struct tree { n   int V; n   vector<E> edges; n   vector<vector<E>> adj; n   vi subt_sz; n   vb erased; n   tree() {} n   tree(int _V) : V(_V) { n     edges.resize(V - 1); n     adj.resize(V), subt_sz.resize(V), erased.resize(V); n   } n   tree(const vector<E>& __edges) : tree(int((__edges).size()) + 1) { n     edges = __edges; n     init(); n   } n   void init() { n     for (int i = 0; i < (int((edges).size())); i++) { n       auto& e = edges[i]; n       e.i = i; n       assert(0 <= e.u && e.u < V && 0 <= e.v && e.v < V); n       adj[e.u].push_back(e); n       adj[e.v].push_back(e); n     } n   } n   friend void re(tree& t) { n     re(t.edges); n     t.init(); n   } n   friend void pr(const tree& t) { pr( {V= , t.V,    , t.edges,  } ); } n   void erase(int v) { erased[v] = true; } n   void calc_subt_sz(int v, int p = -1) { n     subt_sz[v] = 1; n     for (auto& e : adj[v]) n       if (int u = e[v]; !erased[u] && u != p) { n         calc_subt_sz(u, v); n         subt_sz[v] += subt_sz[u]; n       } n   } n   int centroid(int v) { n     calc_subt_sz(v); n     int c = v; n   FIND: n     for (auto& e : adj[c]) n       if (int u = e[c]; !erased[u]) { n         if (subt_sz[u] < subt_sz[c] && 2 * subt_sz[u] >= subt_sz[v]) { n           c = u; n           goto FIND; n         } n       } n     return c; n   } n }; n using E = wedge<int>; n tree<E> tr; n vi w; n vd tot, der; n void go(int r, int v, int p = -1, double d = 0.) { n   double tmp = w[v] * sqrt(d); n   tot[r] += tmp * d; n   der[r] += tmp; n   for (auto& e : tr.adj[v]) n     if (e[v] != p) { n       go(r, e[v], v, d + e.w); n     } n } n pair<double, int> best = {DBL_MAX, -1}; n void solve(int v) { n   v = tr.centroid(v); n   double cd = 0.; n   pair<double, int> n{0., -1}; n   for (auto& e : tr.adj[v]) { n     int u = e[v]; n     tot[u] = der[u] = 0.; n     go(u, u, v, e.w); n     cd += tot[u]; n     ckmax(n, make_pair(der[u], u)); n   } n   ckmin(best, make_pair(cd, v + 1)); n   tr.erase(v); n   if (n.second != -1 && !tr.erased[n.second]) solve(n.second); n } n int main() { n   setIO(); n   int N; n   re(N); n   w.resize(N); n   re(w); n   tr = tree<E>(N); n   re(tr); n   tot.resize(N), der.resize(N); n   solve(0); n   ps(best.second, best.first); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int tc, i, j, k, cs = 1, m, n, ans; n   { n     scanf( %d%d , &m, &n); n     if (n > m) swap(m, n); n     if (n == 1) n       ans = m; n     else if (n == 2) { n       ans = (m / 4) * 4; n       if (m % 4 == 1) n         ans += 2; n       else if (m % 4 == 2) n         ans += 4; n       else if (m % 4 == 3) n         ans += 4; n     } else { n       ans = ((m + 1) / 2) * ((n + 1) / 2); n       ans += (m / 2) * (n / 2); n     } n     printf( %d n , ans); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n; n long long dp[210][210]; n struct point { n   long long x, y; n   void read() { scanf( %I64d%I64d , &x, &y); } n } P[210]; n long long cross(point a, point b, point c) { n   return (b.x - a.x) * (c.y - a.y) - (b.y - a.y) * (c.x - a.x); n } n int main() { n   long long s = 0; n   scanf( %d , &n); n   for (int i = 1; i <= n; i++) P[i].read(); n   P[n + 1] = P[1]; n   for (int i = 2; i <= n; i++) s += cross(P[1], P[i], P[i + 1]); n   if (s > 0) n     for (int i = 1; i <= n / 2; i++) swap(P[i], P[n - i + 1]); n   for (int i = 1; i < n; i++) dp[i][i + 1] = 1; n   for (int i = 2; i < n; i++) n     for (int j = 1; j <= n - i; j++) n       for (int k = j + 1; k < i + j; k++) n         if (cross(P[j], P[i + j], P[k]) > 0) n           dp[j][i + j] = (dp[j][i + j] + dp[j][k] * dp[k][i + j]) % 1000000007; n   printf( %I64d , dp[1][n]); n } n


#include <bits/stdc++.h> n using namespace std; n long long powmod(long long a, long long b) { n   long long res = 1; n   a %= 1000000007; n   for (; b; b >>= 1) { n     if (b & 1) res = res * a % 1000000007; n     a = a * a % 1000000007; n   } n   return res; n } n long long gcd(long long x, long long y) { n   while (y) { n     long long t = x % y; n     x = y; n     y = t; n   } n   return x; n } n template <typename T> n inline bool chkmin(T &a, const T &b) { n   return a > b ? a = b, 1 : 0; n } n template <typename T> n inline bool chkmax(T &a, const T &b) { n   return a < b ? a = b, 1 : 0; n } n unsigned int read() { n   unsigned int x = 0, f = 1; n   char ch = getchar(); n   while (ch <  0  || ch >  9 ) { n     if (ch ==  - ) f = -1; n     ch = getchar(); n   } n   while (ch >=  0  && ch <=  9 ) { n     x = x * 10 + ch -  0 ; n     ch = getchar(); n   } n   return x * f; n } n int n, m; n int fst[1010], vv[400100], nxt[400100], e; n int col[1010], vis[1010]; n void add(int u, int v) { n   vv[e] = v, nxt[e] = fst[u], fst[u] = e++; n   vv[e] = u, nxt[e] = fst[v], fst[v] = e++; n } n void init() { n   e = 0; n   memset(fst, -1, sizeof fst); n } n bool color(int u, int o) { n   vis[u] = o; n   int res = 1; n   for (int i = fst[u]; ~i; i = nxt[i]) { n     int v = vv[i]; n     if (vis[v] == o) { n       if (col[v] == col[u]) res = 0; n       continue; n     } n     col[v] = col[u] ^ 1; n     if (!color(v, o)) res = 0; n   } n   return res; n } n int ans[1010], d[1010]; n int main() { n   init(); n   scanf( %d %d , &n, &m); n   for (int i = 1; i <= m; i++) { n     int u = read(), v = read(); n     add(u, v); n   } n   int bit = 1, tot = 0; n   for (int i = 1; i <= n; i++) { n     if (!vis[i]) { n       if (!color(i, ++tot)) { n         bit = 0; n         break; n       } n     } n   } n   if (!bit) { n     puts( -1 ); n     return 0; n   } n   for (int i = 1; i <= n; i++) n     if (vis[i] > 0) { n       memset(d, 0, sizeof d); n       queue<int> q; n       q.push(i); n       d[i] = 1; n       while (!q.empty()) { n         int u = q.front(); n         q.pop(); n         chkmax(ans[vis[i]], d[u] - 1); n         for (int i = fst[u]; ~i; i = nxt[i]) { n           int v = vv[i]; n           if (d[v]) continue; n           d[v] = d[u] + 1; n           q.push(v); n         } n       } n       vis[i] = 0; n     } n   for (int i = 1; i <= tot; i++) ans[0] += ans[i]; n   printf( %d n , ans[0]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int t; n   cin >> t; n   for (int q = 0; q < t; q++) { n     int n; n     cin >> n; n     vector<int> a(n); n     for (int i = 0; i < n; i++) cin >> a[i]; n     int moves = 0; n     int max_size = 0; n     int alice = 0, bob = 0; n     while (a.size() != 0) { n       moves++; n       int curr_size = 0; n       if (moves % 2 == 1) { n         while (curr_size <= max_size) { n           curr_size += a[0]; n           alice += a[0]; n           a.erase(a.begin()); n           if (a.size() == 0) break; n         } n       } n       if (moves % 2 == 0) { n         while (curr_size <= max_size) { n           curr_size += a[(int)a.size() - 1]; n           bob += a[(int)a.size() - 1]; n           a.erase(a.begin() + (int)a.size() - 1); n           if (a.size() == 0) break; n         } n       } n       max_size = max(max_size, curr_size); n     } n     cout << moves <<     << alice <<     << bob <<   n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n template <class T> n inline bool chkmin(T &a, T const &b) { n   return a > b ? a = b, 1 : 0; n } n template <class T> n inline bool chkmax(T &a, T const &b) { n   return a < b ? a = b, 1 : 0; n } n const int M = 35; n const int N = 205; n const int Mod = 998244353; n long long A[M], a[M][M * N]; n vector<int> F[M][M * N]; n int Sz[M], C[N][N], S[M * N], Inv[N]; n int n; n int calc(vector<int> &P, long long l, long long r) { n   int res = 0, x = 1, y = 1; n   l = l % Mod * 616898040 % Mod; n   r = r % Mod * 616898040 % Mod; n   for (int i = (0), i_end__ = (P.size()); i < i_end__; i++) { n     res = (res + (long long)(y - x) * P[i]) % Mod; n     x = ((long long)(x) * (l) % Mod), y = ((long long)(y) * (r) % Mod); n   } n   return res; n } n int main() { n   for (int i = (0), i_end__ = (N); i < i_end__; i++) { n     C[i][0] = 1; n     for (int j = (1), j_end__ = (i + 1); j < j_end__; j++) n       C[i][j] = (C[i - 1][j] + C[i - 1][j - 1]) % Mod; n   } n   Inv[1] = 1; n   for (int i = (2), i_end__ = (N); i < i_end__; i++) n     Inv[i] = ((long long)(-Mod / i) * (Inv[Mod % i]) % Mod); n   scanf( %d , &n); n   for (int i = (0), i_end__ = (n); i < i_end__; i++) { n     double x; n     scanf( %lf , &x); n     A[i] = min(x, i + 1.0) * 1000000 + 0.5; n   } n   Sz[0] = 2; n   F[0][1].push_back(0), F[0][1].push_back(1); n   F[0][2].push_back(0); n   a[0][1] = A[0]; n   for (int i = (1), i_end__ = (n); i < i_end__; i++) { n     for (int j = (1), j_end__ = (Sz[i - 1]); j < j_end__; j++) n       S[j] = (S[j - 1] + calc(F[i - 1][j], a[i - 1][j - 1], a[i - 1][j])) % Mod; n     int pos = lower_bound(a[i - 1], a[i - 1] + Sz[i - 1], A[i]) - a[i - 1]; n     a[i - 1][pos] = A[i]; n     a[i - 1][pos + 1] = 0x3f3f3f3f3f3f3f3fLL; n     F[i][0].push_back(0), Sz[i] = 1; n     int l = 0, r = 1; n     long long lp = -1000000, rp = 0; n     while (r <= pos) { n       long long Next = min(a[i - 1][l] - lp, a[i - 1][r] - rp); n       a[i][Sz[i]] = rp + Next; n       vector<int> &poy = F[i][Sz[i]]; n       poy.resize(max(F[i - 1][r].size(), F[i - 1][l].size()) + 1); n       poy[1] = (calc(F[i - 1][l], 0, a[i - 1][l]) - n                 calc(F[i - 1][r], 0, a[i - 1][r - 1])) % n                Mod; n       if (l < r) poy[1] = ((long long)poy[1] + S[r - 1] - S[l]) % Mod; n       for (int j = (0), j_end__ = (F[i - 1][r].size()); j < j_end__; j++) n         poy[j + 1] = (poy[j + 1] + F[i - 1][r][j]) % Mod; n       for (int j = (1), j_end__ = (F[i - 1][l].size()); j < j_end__; j++) n         for (int k = (0), k_end__ = (j + 1); k < k_end__; k++) n           poy[k + 1] = (poy[k + 1] + n                         ((j - k) & 1 ? 1LL : -1LL) * C[j][k] * F[i - 1][l][j]) % n                        Mod; n       for (int j = (1), j_end__ = (poy.size()); j < j_end__; j++) n         poy[j] = ((long long)(poy[j]) * (Inv[j]) % Mod); n       if ((lp += Next) == a[i - 1][l]) l++; n       if ((rp += Next) == a[i - 1][r]) r++; n       Sz[i]++; n     } n     F[i][Sz[i]].push_back(0); n   } n   int ans = 0; n   for (int j = (1), j_end__ = (Sz[n - 1]); j < j_end__; j++) n     (ans += calc(F[n - 1][j], a[n - 1][j - 1], a[n - 1][j])) %= Mod; n   printf( %d n , (ans + Mod) % Mod); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct T { n   long long nu, p; n   bool operator<(const T& o) const { n     if (nu > o.nu) return true; n     if (nu == o.nu && p > o.p) return true; n     return false; n   } n }; n long long n, k, m; n T plazas[111111]; n long long sum[111111]; n long long donde[111111]; n int main() { n   cin >> n >> k >> m; n   for (int i = 0; i < n; i++) cin >> plazas[i].nu, plazas[i].p = i; n   n--; n   sort(plazas, plazas + n); n   for (int i = 0; i < n; i++) { n     donde[plazas[i].p] = i; n     sum[i] = plazas[i].nu; n     if (i) sum[i] += sum[i - 1]; n   } n   if (sum[n - 1] <= m) n     cout << n + 1; n   else { n     bool ok = false; n     long long ss = 0, tt; n     ss = sum[k - 1]; n     if (ss <= m) n       cout << n + 1; n     else { n       ss = 0; n       if (k > 1) ss = sum[k - 2]; n       for (int i = 0; i < n && !ok; i++) { n         long long p = donde[i]; n         long long tss = ss; n         if (p < k - 2) n           tss += plazas[k - 1].nu; n         else n           tss += plazas[p].nu; n         if (tss > m) { n           ok = true; n           cout << i + 1; n         } n       } n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1005; n const double inf = 1e9; n int n, m, x[N], b[N], pre[N]; n double dp[N]; n bool check(double mid) { n   for (int i = 1; i <= n; i++) dp[i] = inf; n   for (int i = 0; i <= n; i++) { n     for (int j = i + 1; j <= n; j++) { n       double tmp = dp[i] + sqrt(abs(x[j] - x[i] - m)) - 1.0 * mid * b[j]; n       if (dp[j] > tmp) { n         dp[j] = tmp; n         pre[j] = i; n       } n     } n   } n   return dp[n] >= 0; n } n void prnt(int x) { n   if (x == 0) return; n   prnt(pre[x]); n   printf( %d  , x); n } n int main() { n   scanf( %d%d , &n, &m); n   for (int i = 1; i <= n; i++) scanf( %d%d , &x[i], &b[i]); n   double l = 0.0, r = 1e5; n   while (r - l > 1e-8) { n     double mid = (l + r) / 2; n     if (check(mid)) n       l = mid; n     else n       r = mid; n   } n   prnt(n); n   return printf(  n ), 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long t; n   cin >> t; n   while (t--) { n     long long a, b, c, d, e, i, j, k, l; n     cin >> a >> b; n     if (a <= b) n       cout << abs(a - b) << endl; n     else if (a % 2 + b % 2 == 1) n       cout << 1 << endl; n     else n       cout << 0 << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n long long a[200001]; n long long search(long long l, long long h, long long target) { n   if (h - l <= 1) { n     if (a[l] > target) n       return l; n     else n       return h; n   } n   long long mid = (l + h) / 2; n   if (a[mid] > target) n     return search(l, mid, target); n   else n     return search(mid + 1, h, target); n } n int main() { n   int N, M; n   cin >> N >> M; n   for (int i = 0; i < N; i++) { n     cin >> a[i]; n   } n   sort(a, a + N); n   for (int j = 0; j < M; j++) { n     int b; n     cin >> b; n     if (a[N - 1] > b) { n       cout << search(0, N - 1, b) <<    ; n     } else { n       cout << N <<    ; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   int n, nn; n   cin >> n; n   nn = n * 2; n   vector<int> arr(nn); n   for (int i = 0; i < nn; ++i) cin >> arr[i]; n   sort(arr.begin(), arr.end()); n   long long ans, now; n   ans = arr[nn - 1] - arr[n]; n   ans *= arr[n - 1] - arr[0]; n   for (int i = 1; i < n; ++i) { n     now = arr[n - 1 + i] - arr[i]; n     now *= arr[nn - 1] - arr[0]; n     ans = min(ans, now); n   } n   cout << ans; n } n


#include <bits/stdc++.h> n #pragma GCC target( avx2 ) n #pragma GCC optimization( O3 ) n #pragma GCC optimization( unroll-loops ) n const long long INF = 0x3f3f3f3f3f3f3f3f; n const long long llinf = (1LL << 62); n const int inf = (1 << 30); n const long long nmax = 5e5 + 50; n const int mod = 1e9 + 7; n using namespace std; n int n, m, i, x, a[nmax], p[nmax], q, st[4 * nmax], ans[nmax], j; n long long nr[nmax], k; n vector<int> b[nmax]; n vector<pair<int, int> > qr[nmax]; n pair<int, int> pr; n pair<int, int> fnd(long long y) { n   int l = 0, r = n, mid, rs = n, i; n   while (l <= r) { n     mid = (l + r) / 2; n     if (nr[mid] > y) n       rs = mid, r = mid - 1; n     else n       l = mid + 1; n   } n   for (i = max(rs - 1, 0); i <= min(rs + 1, n); i++) n     if (nr[i] >= y) break; n   rs = i; n   if (rs <= n) { n     if (rs) y -= nr[rs - 1]; n     return make_pair(rs, y); n   } else { n     y -= nr[n]; n     if (y % m == 0) return make_pair(m, -1); n     return make_pair(y % m, -1); n   } n } n void upd(int nod, int l, int r, int p) { n   if (l == r) { n     st[nod] = 1; n     return; n   } n   int mid = (l + r) / 2; n   if (p <= mid) n     upd(2 * nod, l, mid, p); n   else n     upd(2 * nod + 1, mid + 1, r, p); n   st[nod] = st[2 * nod] + st[2 * nod + 1]; n } n int qry(int nod, int l, int r, int z) { n   if (z > st[nod]) return -1; n   if (l == r) return l; n   int mid = (l + r) / 2; n   if (st[nod * 2] >= z) return qry(2 * nod, l, mid, z); n   return qry(2 * nod + 1, mid + 1, r, z - st[nod * 2]); n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cerr.tie(0); n   cout.tie(0); n   cin >> n >> m >> q; n   for (i = 1; i <= n; i++) { n     cin >> x; n     a[x]++; n   } n   for (i = 1; i <= m; i++) { n     nr[a[i]]++; n     b[a[i]].push_back(i); n   } n   p[0] = b[0].size(); n   for (i = 1; i <= n; i++) p[i] = p[i - 1] + b[i].size(); n   for (i = 1; i <= n; i++) nr[i] += nr[i - 1]; n   for (i = 1; i <= n; i++) nr[i] += nr[i - 1]; n   for (i = 1; i <= q; i++) { n     cin >> k; n     k -= n; n     pr = fnd(k); n     if (pr.second == -1) n       ans[i] = pr.first; n     else n       qr[pr.first].push_back(make_pair(pr.second, i)); n   } n   for (i = 0; i <= n; i++) { n     for (j = 0; j < b[i].size(); j++) upd(1, 1, m, b[i][j]); n     for (j = 0; j < qr[i].size(); j++) n       ans[qr[i][j].second] = qry(1, 1, m, qr[i][j].first); n   } n   for (i = 1; i <= q; i++) cout << ans[i] <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 100100, L = 18; n int n, m, Q; n int dep[N], p[N][L]; n set<int> adj[N]; n map<int, int> mp[N]; n int get(int par, int i) { n   if (mp[par][i] == i) return i; n   return mp[par][i] = get(par, mp[par][i]); n } n void mrg(int par, int u, int v) { n   u = get(par, u); n   v = get(par, v); n   if (u == v) return; n   if (rand() & 1) swap(u, v); n   mp[par][u] = mp[par][v]; n } n int LCA(int u, int v) { n   if (u == v) return 0; n   int u0 = u, v0 = v; n   if (dep[u] < dep[v]) swap(u, v); n   int k = dep[u] - dep[v]; n   for (int i = 0; i < L; i++) n     if (k & (1 << i)) u = p[u][i]; n   if (u == v) return k; n   for (int i = L - 1; i >= 0; i--) n     if (p[u][i] != p[v][i]) u = p[u][i], v = p[v][i]; n   int ret = dep[u0] + dep[v0] - 2 * dep[p[u][0]]; n   int par = p[u][0]; n   if (get(par, u) == get(par, v)) ret--; n   return ret; n } n int main() { n   srand(time(0)); n   scanf( %d %d %d , &n, &m, &Q); n   for (int i = 1; i <= m; i++) { n     int u, v; n     scanf( %d %d , &u, &v); n     adj[u].insert(v); n     adj[v].insert(u); n   } n   queue<int> q; n   q.push(1); n   dep[1] = 1; n   p[1][0] = 0; n   while (!q.empty()) { n     int u = q.front(); n     q.pop(); n     for (auto v : adj[u]) mp[u][v] = v; n     for (auto v : adj[u]) { n       if (p[u][0] == v) continue; n       if (dep[v]) { n         mrg(p[u][0], u, v); n         continue; n       } n       dep[v] = 1 + dep[u]; n       p[v][0] = u; n       for (int i = 1; i < L; i++) p[v][i] = p[p[v][i - 1]][i - 1]; n       q.push(v); n     } n   } n   while (Q--) { n     int u, v; n     scanf( %d %d , &u, &v); n     printf( %d n , LCA(u, v)); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <class T> n inline T bigmod(T b, T p, T m) { n   T ret; n   if (p == 0) return 1; n   if (p & 1) { n     ret = (bigmod(b, p / 2, m) % m); n     return ((b % m) * ret * ret) % m; n   } else { n     ret = (bigmod(b, p / 2, m) % m); n     return (ret * ret) % m; n   } n } n int dx4[] = {1, -1, 0, 0}; n int dy4[] = {0, 0, 1, -1}; n int dx6[] = {0, 0, 1, -1, 0, 0}; n int dy6[] = {1, -1, 0, 0, 0, 0}; n int dz6[] = {0, 0, 0, 0, 1, -1}; n int dx8[] = {1, -1, 0, 0, -1, 1, -1, 1}; n int dy8[] = {0, 0, 1, -1, 1, 1, -1, -1}; n int dkx8[] = {-1, 1, -1, 1, -2, -2, 2, 2}; n int dky8[] = {2, 2, -2, -2, 1, -1, 1, -1}; n int tc = 1; n const double eps = 1e-9; n const double pi = acos(-1.0); n const long long int mx = 5e5; n const long long int mod = 1e9 + 7; n string str; n int n, k; n int main() { n   int i, j, cnt, a, b; n   ios::sync_with_stdio(0); n   while (cin >> n >> k >> str) { n     cnt = 0; n     if (k == 2) { n       a = 0; n       b = 0; n       for (i = 0; i < n; i++) { n         if (i % 2 == 0) { n           if (str[i] ==  B ) a++; n           if (str[i] ==  A ) b++; n         } else { n           if (str[i] ==  A ) a++; n           if (str[i] ==  B ) b++; n         } n       } n       if (a < b) { n         cout << a <<   n ; n         for (i = 0; i < n; i++) cout << ((i % 2 == 0) ?  A  :  B ); n         cout <<   n ; n       } else { n         cout << b <<   n ; n         for (i = 0; i < n; i++) cout << ((i % 2 != 0) ?  A  :  B ); n         cout <<   n ; n       } n       continue; n     } n     for (i = 0; i < n - 1; i++) { n       if (str[i] == str[i + 1] && str[i] == str[i + 2]) { n         for (j = 0; j < k; j++) { n           if (str[i + 1] != (j +  A )) { n             str[i + 1] = (j +  A ); n             cnt++; n             break; n           } n         } n       } else if (str[i] == str[i + 1]) { n         for (j = 0; j < k; j++) { n           if (i > 0 && str[i - 1] != (j +  A ) && str[i] != (j +  A )) { n             str[i] = (j +  A ); n             cnt++; n             break; n           } else if (i == 0 && str[i] != (j +  A )) { n             str[i] = (j +  A ); n             cnt++; n             break; n           } n         } n       } n     } n     cout << cnt <<   n ; n     cout << str <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int a[200000], dp[600000]; n int main() { n   int n, l; n   cin >> n >> l; n   for (int i = 1; i <= n; i++) { n     double x; n     cin >> a[i] >> x; n   } n   int maxx = -1; n   for (int i = 1; i <= n; i++) { n     dp[i] = 1; n     for (int j = 1; j < i; j++) { n       if (a[i] >= a[j]) dp[i] = max(dp[i], dp[j] + 1); n       maxx = max(maxx, dp[i]); n     } n   } n   if (n == 1) n     cout << 0 << endl; n   else n     cout << n - maxx << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long int j, i, k, n, m, x; n   float d[6000], a[5008], b[5009]; n   cin >> n >> m; n   for (i = 0; i < n; i++) { n     cin >> a[i] >> b[i]; n     d[i] = (float)(1.0 * a[i]) / b[i]; n   } n   sort(d, d + n); n   printf( %.8f , d[0] * m); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   long long a, b, c; n   cin >> a >> b >> c; n   a *= 10; n   long long q = 0; n   for (long long j = 0; j <= 9; j++) { n     a += j; n     if (a % b == 0) { n       q = 1; n       break; n     } else n       a -= j; n   } n   if (q == 0) { n     cout << -1 << endl; n     return 0; n   } n   cout << a; n   for (long long i = 0; i < c - 1; i++) cout << 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   cin >> n; n   stack<int> s; n   int ans = 0; n   for (int i = 0; i < n; i++) { n     int x; n     cin >> x; n     while (!s.empty()) { n       ans = max(ans, s.top() ^ x); n       if (x > s.top()) { n         s.pop(); n       } else { n         break; n       } n     } n     s.push(x); n   } n   cout << ans << endl; n } n


#include <bits/stdc++.h> n using namespace std; n template <class T> n void chmin(T& a, const T& b) { n   if (a > b) a = b; n } n template <class T> n void chmax(T& a, const T& b) { n   if (a < b) a = b; n } n int n, d; n const int geta = 260; n const int MX = 30000; n int dp[30010][520]; n int cnt[30010]; n int main() { n   scanf( %d %d , &n, &d); n   for (int i = 0; i < n; i++) { n     int k; n     scanf( %d , &k); n     cnt[k]++; n   } n   memset(dp, -1, sizeof(dp)); n   dp[d][geta] = cnt[d]; n   int ans = 0; n   for (int i = d; i <= MX; i++) { n     for (int j = 0; j < 520; j++) { n       if (dp[i][j] == -1) continue; n       chmax(ans, dp[i][j]); n       int len = d + (j - geta); n       if (len > 1 && i + len - 1 <= MX) n         chmax(dp[i + len - 1][j - 1], dp[i][j] + cnt[i + len - 1]); n       if (i + len <= MX) chmax(dp[i + len][j], dp[i][j] + cnt[i + len]); n       if (i + len + 1 <= MX) n         chmax(dp[i + len + 1][j + 1], dp[i][j] + cnt[i + len + 1]); n     } n   } n   printf( %d n , ans); n   return 0; n } n


//            __                       n //   _______ / _|_   _ _ __ _   _ ___  n //  |_  / _  |_| | | |  __| | | / __|   created on n //   / /  __/  _| |_| | |  | |_|  __    18 Mar 2021 00:06:00 n //  /___ ___|_|   __, |_|    __,_|___/ n //               |___/                 n  n #include<bits/stdc++.h> n  n using namespace std; n #define int long long n  n #define deb(...) cerr <<  [  << #__VA_ARGS__ <<  ] : [ , DBG(__VA_ARGS__) n void DBG() { cerr <<  ] n ;} n template<typename T, typename ...Args> void DBG(T first, Args... args) n {cerr << first; if(sizeof...(args))cerr<< ,  ; DBG(args...); } n  n class Math{ n  private: n   int size; n   std::vector<int> prime,spf,square_of_a_prime; n  public: n  n  Math(int size_):size(size_){ n   /* prime.resize(size); */ n   spf.resize(size); n   /* square_of_a_prime.resize(size); */ n  } n  n  std::vector<int> seive(){ n   for(int i=2;i<size;i++)prime[i]=1; n   for(int i=2;i<size;i++){ n    if(prime[i]){ n     if(i*i<size)square_of_a_prime[i*i]=1; n     for(int j=2;j<size;j++){ n      if(i*j<size) prime[i*j]=0; n      else break; n     } n    } n   } n   return prime; n  } n  n  void smallest_prime_factor(){ n   spf.assign(size,-1); n   spf[1]=1; n   for(int i=2; i<size; i++){ n    if(spf[i]==-1){ n     spf[i] = i; n     for(int j = 2; j*i<size; j++){ n      if(spf[i*j]==-1){ n       spf[i*j] = i; n      } n     } n    } n   } n  } n   n  int SPF(int x){ n   return spf[x]; n  } n  n  int count_no_of_factors(int x){ n     // complexity is O(n^1/3)... n   seive(); n   int ans = 1; n   for(int i=0;i<size;i++){ n    if(prime[i]){ n     if(i*i*i > x)break; n     int count = 1; n     while(x%i==0){ n      count++; n      x/=i; n     } n     ans *= count; n    } n   } n   if(prime[x])ans*=2; n   else if(square_of_a_prime[x])ans*=3; n   else if(x!=1)ans*=4; n   return ans; n  } n  n  std::vector<int> prime_factorize(int x){ n   // complexity is O(n^1/2) n   std::vector<int> ff(size,0); n   while(x%2==0){ n    ff[2]++; n    x/=2; n   } n   for(int i=3;i*i<=x;i+=2){ n    while(x%i==0){ n     ff[i]++; n     x/=i; n    } n   } n   if(x>2)ff[x]++; n   return ff; n  } n  n  std::vector<int> prime_factorize_using_seive(int x){ n   smallest_prime_factor(); n   std::vector<int> ff(size,0); n   while(x!=1){ n    ff[spf[x]]++; n    x/=spf[x]; n   } n   return ff; n  } n  n  int phi(int n){ n   int result = n; n   for(int i=2; i*i<=n; i++){ n    if (n%i==0) { n     while(n%i==0) n/=i; n     result-=result/i; n    } n   } n   if (n>1) result-=result/n; n   return result; n  } n  n  std::vector<int> phi_1_to_n(int n){ n     std::vector<int> phi(n+1); n     phi[0] = 0; phi[1] = 1; n     for (int i=2; i<=n; i++) phi[i]=i; n  n     for (int i = 2; i <= n; i++) { n    if (phi[i]==i) { n     for (int j=i; j<=n; j+=i) n      phi[j] -= phi[j]/i; n    } n     } n   return phi; n  } n }; n  n  n int32_t main(){ n   ios_base:: sync_with_stdio(false); n   cin.tie(0); n   int t=1; n   cin >> t; n  n   Math m((int)1e7); n  m.smallest_prime_factor(); n  n  n   for(int tt=1;tt<=t;tt++){ n   int ans= 0; n     int n; cin >> n; n     int k; cin >> k; n  n     map<int,int> f; n  n     for(int i=0;i<n;i++){ n       int x; cin >> x; n       int y=1; n    map<int,int> used; n       while(x!=1){ n         int num = m.SPF(x); n         while(x%num==0){ n           x/=num; n      used[num]++; n         } n       } n    for(auto ele : used){ n     if(ele.second&1) y*=ele.first; n    } n       if(f.count(y)){ n         ans++; n         f.clear(); n     f[y]++; n       } n       else{ n         f[y]++; n       } n     } n   cout << ans+1 <<   n ; n   } n } n  n


#include <bits/stdc++.h> n using namespace std; n deque<int> check(deque<int> mas) { n   int l = 0, r = mas.size() - 1; n   bool flag = true; n   for (; l < mas.size() / 2; l++, r--) { n     if (mas[r] < 0) { n       mas[r - 1]--; n       mas[r] += 10; n     } n     if (mas[l] == mas[r] + 1 && l != r - 1) { n       mas[l + 1] += 10; n       mas[l]--; n     } n     if (mas[l] == mas[r] + 10 && l != r - 1) { n       mas[r - 1]--; n       mas[r] += 10; n     } n     if (mas[l] == mas[r] + 11) { n       mas[l]--; n       mas[l + 1] += 10; n       if (l != r - 1) { n         mas[r] += 10; n         mas[r - 1]--; n       } n     } n     if (mas[l] != mas[r]) { n       flag = false; n       break; n     } n     if (mas[l] > 18) { n       flag = false; n       break; n     } n   } n   if (flag == false || mas[0] == 0 || mas[mas.size() - 1] == 0) { n     deque<int> ans; n     ans.push_back(0); n     return ans; n   } else { n     if (mas.size() % 2 == 1 && (mas[l] % 2 == 1 || mas[l] < 0)) { n       deque<int> ans; n       ans.push_back(0); n       return ans; n     } else { n       deque<int> ans = mas; n       l = 0; n       r = mas.size() - 1; n       for (; l < mas.size() / 2; l++, r--) { n         ans[l] = mas[l] - mas[l] / 2; n         ans[r] = mas[l] - ans[l]; n       } n       if (mas.size() % 2 == 1) { n         ans[l] = mas[l] / 2; n       } n       return ans; n     } n   } n } n int main() { n   deque<int> x1; n   char c; n   while (cin >> c) x1.push_back(int(c) - int( 0 )); n   deque<int> x2 = x1; n   if (x2.size() >= 2) { n     x2[1] += x2[0] * 10; n     x2.pop_front(); n   } n   deque<int> ans1 = check(x1); n   deque<int> ans2 = check(x2); n   if (ans1.size() > ans2.size()) { n     for (int i = 0; i < ans1.size(); i++) cout << ans1[i]; n   } else { n     for (int i = 0; i < ans2.size(); i++) cout << ans2[i]; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long MOD = 1000000007; n long long fac[1100000], inv[1100000], p[1100000]; n long long pow_mod(long long a, long long b, long long m) { n   long long ans = 1LL; n   a %= m; n   while (b) { n     if (b & 1) ans = ans * a % m; n     a = a * a % m; n     b >>= 1; n   } n   return ans; n } n int main() { n   fac[0] = 1LL; n   inv[0] = pow_mod(fac[0], MOD - 2, MOD); n   for (long long i = 1; i <= 1010000; ++i) { n     fac[i] = i * fac[i - 1] % MOD; n     inv[i] = pow_mod(fac[i], MOD - 2, MOD); n   } n   long long n, k; n   cin >> n >> k; n   for (long long i = 1; i <= k + 2; ++i) { n     p[i] = p[i - 1] + pow_mod(i, k, MOD); n     p[i] %= MOD; n   } n   if (n <= k + 2) { n     cout << p[n] << endl; n     return 0; n   } n   long long x = 1LL; n   for (int i = 1; i <= k + 2; ++i) { n     x *= (n - i); n     x %= MOD; n   } n   long long ans = 0LL, flag; n   for (int i = 1; i <= k + 2; ++i) { n     if ((k + 2 - i) & 1) n       flag = -1; n     else n       flag = 1; n     ans += x * flag * pow_mod(n - i, MOD - 2, MOD) % MOD * inv[i - 1] % MOD * n            inv[k + 2 - i] % MOD * p[i]; n     (ans += MOD) %= MOD; n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n inline int readInt() { n   static int n, ch; n   n = 0, ch = getchar(); n   while (!isdigit(ch)) ch = getchar(); n   while (isdigit(ch)) n = n * 10 + ch -  0 , ch = getchar(); n   return n; n } n const int MAX_N = 500000 + 3; n int n, k, a[MAX_N], b[MAX_N]; n struct Data { n   long long val; n   bool isA; n   Data(long long val, bool isA) : val(val), isA(isA) {} n   bool operator<(const Data &ano) const { n     return val > ano.val || (val == ano.val && isA < ano.isA); n   } n }; n priority_queue<Data> pq; n long long sum = 0; n int check(int t) { n   while (!pq.empty()) pq.pop(); n   int cnt = 0; n   sum = 0; n   for (int i = 0; i < n; ++i) { n     pq.push(Data(a[i] - t, true)); n     if (pq.top().val + b[i] < 0) { n       cnt += pq.top().isA; n       sum += pq.top().val + b[i]; n       pq.pop(); n       pq.push(Data(-b[i], false)); n     } n   } n   return cnt; n } n int main() { n   n = readInt(), k = readInt(); n   for (int i = 0; i < n; ++i) a[i] = readInt(); n   for (int i = 0; i < n; ++i) b[i] = readInt(); n   int l = 0, r = 2e9 + 3; n   while (r - l > 1) { n     int mid = ((long long)l + r) / 2; n     if (check(mid) <= k) n       l = mid; n     else n       r = mid; n   } n   check(l); n   printf( %I64d n , sum + (long long)k * l); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long N = 2e5; n long long n, m, k, a[N], c, res; n struct stru { n   long long col, x, l, r; n }; n vector<stru> v; n vector<vector<long long>> mp(N), pr(N); n signed main() { n   ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); n   cin >> n >> m >> k; n   for (long long i = 0; i < n; ++i) cin >> a[i]; n   for (long long i = 0; i < n - 1; ++i) { n     if (a[i] == a[i + 1]) n       c++; n     else { n       v.push_back({a[i], c + 1, i - c, i}); n       c = 0; n     } n   } n   v.push_back({a[n - 1], c + 1, n - 1 - c, n - 1}); n   for (long long i = 0; i < v.size(); ++i) { n     long long col = v[i].col, l = 0, r = mp[col].size(), med, ni = v[i].l; n     r--; n     c = v[i].x; n     while (r - l > 1) { n       med = (r + l) / 2; n       if (ni - mp[col][med] - 1 - pr[col].back() + pr[col][med] <= k) n         r = med; n       else n         l = med; n     } n     if (!mp[col].empty() && n         ni - mp[col][l] - 1 - pr[col].back() + pr[col][l] <= k) n       res = max(res, pr[col].back() - (l ? pr[col][l - 1] : 0) + c); n     else if (!mp[col].empty() && n              ni - mp[col][r] - 1 - pr[col].back() + pr[col][r] <= k) n       res = max(res, pr[col].back() - (r ? pr[col][r - 1] : 0) + c); n     else n       res = max(res, c); n     mp[col].push_back(v[i].r); n     if (!pr[col].empty()) n       pr[col].push_back(pr[col].back() + c); n     else n       pr[col].push_back(c); n   } n   cout << res; n } n


#include <bits/stdc++.h> n namespace modular { n const int MOD = 1000000007; n inline int add(int x, int y) { return (x += y) >= MOD ? x -= MOD : x; } n inline void inc(int &x, int y) { (x += y) >= MOD ? x -= MOD : 0; } n inline int mul(int x, int y) { return 1LL * x * y % MOD; } n inline int qpow(int x, int y) { n   int ans = 1; n   for (; y; y >>= 1, x = mul(x, x)) n     if (y & 1) ans = mul(ans, x); n   return ans; n } n };  // namespace modular n namespace Base { n template <typename Tp> n inline Tp input() { n   Tp x = 0, y = 1; n   char c = getchar(); n   while ((c <  0  ||  9  < c) && c != EOF) { n     if (c ==  - ) y = -1; n     c = getchar(); n   } n   if (c == EOF) return 0; n   while ( 0  <= c && c <=  9 ) x = x * 10 + c -  0 , c = getchar(); n   return x *= y; n } n template <typename Tp> n inline void read(Tp &x) { n   x = input<Tp>(); n } n template <typename Tp> n inline void chmax(Tp &x, Tp y) { n   x < y ? x = y : 0; n } n template <typename Tp> n inline void chmin(Tp &x, Tp y) { n   x > y ? x = y : 0; n } n };  // namespace Base n using namespace Base; n int N, M; n struct edge { n   int u, v, w, t; n   bool operator<(const edge &rhs) const { return w < rhs.w; } n } e[400007]; n struct UFS { n   int fa[400007], sz[400007]; n   int top, cnt; n   std::pair<int, int> st[400007]; n   inline void init() { n     for (int i = 1; i <= N; ++i) fa[i] = i, sz[i] = 1; n     top = 0; n     cnt = N; n   } n   inline int find(int x) { n     while (fa[x] != x) x = fa[x]; n     return x; n   } n   inline void merge(int u, int v) { n     u = find(u), v = find(v); n     if (u == v) return; n     if (sz[u] < sz[v]) std::swap(u, v); n     st[++top] = std::pair<int, int>(u, v); n     if ((sz[u] & 1) && (sz[v] & 1)) cnt -= 2; n     fa[v] = u, sz[u] += sz[v]; n   } n   inline void pop(int t) { n     while (top > t) { n       int u = st[top].first, v = st[top].second; n       top--; n       sz[u] -= sz[v]; n       fa[v] = v; n       if ((sz[u] & 1) && (sz[v] & 1)) cnt += 2; n     } n   } n } ufs; n int ans[400007]; n struct segTree { n   int pos; n   std::vector<int> t[400007 << 2]; n   segTree() : pos(1) {} n   void modi(int x, int l, int r, int ql, int qr, int id) { n     if (ql <= l && r <= qr) return t[x].push_back(id), void(); n     int mid = l + r >> 1; n     if (ql <= mid) modi(x << 1, l, mid, ql, qr, id); n     if (mid < qr) modi(x << 1 | 1, mid + 1, r, ql, qr, id); n   } n   void solve(int x, int l, int r) { n     int tmp = ufs.top; n     for (int id : t[x]) ufs.merge(e[id].u, e[id].v); n     if (l == r) { n       for (; pos <= M && ufs.cnt; ++pos) n         if (e[pos].t <= l) { n           ufs.merge(e[pos].u, e[pos].v); n           if (e[pos].t <= l - 1) modi(1, 1, M, e[pos].t, l - 1, pos); n         } n       ans[l] = ufs.cnt ? -1 : e[pos - 1].w; n     } else { n       int mid = l + r >> 1; n       solve(x << 1 | 1, mid + 1, r); n       solve(x << 1, l, mid); n     } n     ufs.pop(tmp); n   } n } tr; n int main() { n   read(N), read(M); n   for (int i = 1; i <= M; ++i) n     read(e[i].u), read(e[i].v), read(e[i].w), e[i].t = i; n   std::sort(e + 1, e + M + 1); n   ufs.init(); n   tr.solve(1, 1, M); n   for (int i = 1; i <= M; ++i) printf( %d n , ans[i]); n   return 0; n } n


#include <bits/stdc++.h> n const int N = 2e5 + 5; n using namespace std; n vector<int> G[N]; n long long b[N]; n int vis[N]; n long long ans[N]; n long long flag = 1; n void dfs(int x, int fa) { n   long long minn = 0x3f3f3f3f; n   if (b[x] == -1) { n     for (int i = 0; i < G[x].size(); i++) { n       int g = G[x][i]; n       if (!vis[g]) { n         vis[g] = 1; n         minn = min(minn, b[g]); n         dfs(g, x); n       } n       if (minn != 0x3f3f3f3f) n         b[x] = minn; n       else n         b[x] = b[fa]; n     } n   } else { n     for (int i = 0; i < G[x].size(); i++) { n       int g = G[x][i]; n       if (!vis[g]) { n         vis[g] = 1; n         dfs(g, x); n       } n     } n   } n } n void dfs2(int x, int fa) { n   for (int i = 0; i < G[x].size(); i++) { n     int g = G[x][i]; n     if (!vis[g]) { n       vis[g] = 1; n       ans[g] = b[g] - b[x]; n       if (ans[g] < 0) { n         flag = 0; n         return; n       } n       dfs2(g, x); n     } n   } n } n int main() { n   int n; n   cin >> n; n   for (int i = 2; i <= n; i++) { n     int u; n     cin >> u; n     G[i].push_back(u); n     G[u].push_back(i); n   } n   for (int i = 1; i <= n; i++) cin >> b[i]; n   vis[1] = 1; n   dfs(1, -1); n   memset(vis, 0, sizeof vis); n   vis[1] = 1; n   ans[1] = b[1]; n   dfs2(1, -1); n   if (flag) { n     long long res = 0; n     for (int i = 1; i <= n; i++) n       if (ans[i] > 0) res += ans[i]; n     cout << res << endl; n   } else n     cout << -1 << endl; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T> n int len(const T& a) { n   return a.size(); n } n using ll = long long; n constexpr long double EPS = 1e-7; n long double Check(const vector<long double>& a, long double x) { n   vector<long double> new_a(a.size()); n   long double bestmin = 1e8, curmin = 0; n   long double bestmax = -1e8, curmax = 0; n   for (int i = 0; i < len(a); ++i) { n     new_a[i] = (long double)a[i] - x; n     curmax = max((long double)0, curmax + new_a[i]); n     bestmax = max(bestmax, curmax); n     curmin = min((long double)0, curmin + new_a[i]); n     bestmin = min(bestmin, curmin); n   } n   return max(abs(bestmin), abs(bestmax)); n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(nullptr); n   cout << fixed << setprecision(6); n   int n; n   cin >> n; n   vector<long double> a(n); n   for (auto& i : a) cin >> i; n   long double l = -1e4 - 10, r = 1e4 + 10; n   for (int step = 0; step < 200; ++step) { n     long double m1 = l + (r - l) / 3.0; n     long double m2 = r - (r - l) / 3.0; n     if (Check(a, m1) - Check(a, m2) > EPS) { n       l = m1; n     } else { n       r = m2; n     } n   } n   cout << Check(a, l); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int read() { n   int x = 0, f = 1; n   char ch = getchar(); n   while (ch <  0  || ch >  9 ) { n     if (ch ==  - ) f = -1; n     ch = getchar(); n   } n   while (ch >=  0  && ch <=  9 ) { n     x = x * 10 + ch -  0 ; n     ch = getchar(); n   } n   return x * f; n } n int n, m, K, mp[1005][1005]; n int main() { n   memset(mp, 0, sizeof(mp)); n   n = read(); n   m = read(); n   K = read(); n   for (int i = 1; i <= K; i++) { n     int x = read(), y = read(); n     mp[x][y] = 1; n     if (mp[x + 1][y] && mp[x][y + 1] && mp[x + 1][y + 1]) { n       cout << i << endl; n       return 0; n     } n     if (mp[x + 1][y] && mp[x][y - 1] && mp[x + 1][y - 1]) { n       cout << i << endl; n       return 0; n     } n     if (mp[x - 1][y] && mp[x][y + 1] && mp[x - 1][y + 1]) { n       cout << i << endl; n       return 0; n     } n     if (mp[x - 1][y] && mp[x][y - 1] && mp[x - 1][y - 1]) { n       cout << i << endl; n       return 0; n     } n   } n   cout << 0 << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct data { n   int to, head; n } e[750000 * 2]; n stack<int> s; n vector<int> cir[750000]; n int n, m, len, head[750000], dfn[750000], low[750000], cnt, num, p[750000], n     binv[750000], d[750000]; n int ans, tot; n long long mm; n int mul(int a, int b) { n   int ans = 1; n   while (b) { n     if (b & 1) ans = 1ll * ans * a % 1000000007; n     a = 1ll * a * a % 1000000007; n     b >>= 1; n   } n   return ans; n } n void dfs(int lab, int fa) { n   dfn[lab] = low[lab] = ++num; n   s.push(lab); n   for (int i = head[lab]; i; i = e[i].head) { n     if (!dfn[e[i].to]) { n       dfs(e[i].to, lab); n       low[lab] = min(low[lab], low[e[i].to]); n       if (low[e[i].to] == dfn[lab]) { n         cnt++; n         int m1; n         do { n           m1 = s.top(); n           s.pop(); n           if (dfn[lab] == low[m1]) cir[cnt].push_back(m1); n         } while (m1 != e[i].to); n         cir[cnt].push_back(lab); n         m1 = cir[cnt].size(); n         for (auto j : cir[cnt]) p[j] = (1ll * p[j] + binv[m1]) % 1000000007; n         tot = (1ll * tot + binv[m1]) % 1000000007; n       } n     } n     if (e[i].to != fa) low[lab] = min(low[lab], dfn[e[i].to]); n   } n } n void add(int u, int v) { n   e[++len].to = v; n   e[len].head = head[u]; n   head[u] = len; n } n int main() { n   binv[0] = 1; n   binv[1] = mul(2, 1000000007 - 2); n   int u, v, m1, m2, m3; n   scanf( %d%d , &n, &m); n   for (int i = 1; i <= m; i++) { n     scanf( %d%d , &u, &v); n     add(u, v); n     add(v, u); n     d[u]++; n     d[v]++; n   } n   m1 = max(n, 10); n   for (int i = 2; i <= m1; i++) n     binv[i] = 1ll * binv[1] * binv[i - 1] % 1000000007; n   ans = 1ll * n * (n + 1) % 1000000007 * binv[2] % 1000000007; n   ans = (ans + 1ll * m * binv[2]) % 1000000007; n   dfs(1, 0); n   for (int i = 1; i <= n; i++) { n     ans = (ans - 2ll * d[i] * binv[2]) % 1000000007; n     ans = (ans - 2ll * (m - d[i]) * binv[3]) % 1000000007; n     ans = (ans + 2ll * p[i]) % 1000000007; n     ans = (ans + 2ll * (tot - p[i]) * binv[1]) % 1000000007; n     for (int j = head[i]; j; j = e[j].head) { n       if (e[j].to < i) continue; n       ans = (ans + (1ll * d[i] + d[e[j].to] - 3 + m) * binv[4]) % 1000000007; n     } n   } n   ans = (1ll * ans + tot) % 1000000007; n   for (int i = 1; i <= cnt; i++) { n     m1 = 0; n     m2 = cir[i].size(), m3 = 0; n     for (auto j : cir[i]) { n       m1 = (1ll * m1 + p[j] - binv[m2]) % 1000000007; n       ans = (ans + 1ll * (p[j] - binv[m2]) * binv[m2] % 1000000007 * 2) % n             1000000007; n       m3 = (1ll * m3 + d[j] - 2) % 1000000007; n       ans = (ans - 2ll * binv[m2 + 1] * (d[j] - 2)) % 1000000007; n     } n     m1 = (1ll * m1 + binv[m2]) % 1000000007; n     ans = (ans + 1ll * (tot - m1) * binv[m2]) % 1000000007; n     ans = (ans - 2ll * m2 * binv[m2]) % 1000000007; n     ans = (ans - 2ll * (m - m3 - m2) * binv[m2 + 2]) % 1000000007; n   } n   m1 = (1ll * n * binv[1] - 1ll * m * binv[2] + 1ll * tot) % 1000000007; n   ans = ((ans - 1ll * m1 * m1) % 1000000007 + 1000000007) % 1000000007; n   printf( %d n , ans); n } n


#include <bits/stdc++.h> n using namespace std; n int n, m, k; n long long rase[100][100], satis[100]; n long long DP[20][(int)1e6 + 1]; n long long solve(int index, int mask) { n   if (DP[index][mask] != -1) { n     return DP[index][mask]; n   } n   if (__builtin_popcount(mask) == m) { n     return DP[index][mask] = 0; n   } n   long long res = -1; n   for (int i = 0; i < n; i++) { n     if ((mask & (1 << i)) == false) { n       res = max(res, solve(i, mask | (1 << i)) + satis[i] + rase[index][i]); n     } n   } n   return DP[index][mask] = res; n } n int main() { n   memset(DP, -1, sizeof(DP)); n   cin >> n >> m >> k; n   for (int i = 0; i < n; i++) { n     cin >> satis[i]; n   } n   for (int i = 0; i < k; i++) { n     long long x, y, c; n     cin >> x >> y >> c; n     x--; n     y--; n     rase[x][y] = c; n   } n   long long res = -1; n   for (int i = 0; i < n; i++) { n     res = max(res, solve(i, 1 << i) + satis[i]); n   } n   cout << res << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, s[4]; n int main() { n   scanf( %d , &n); n   for (int x, i = 1; i <= n; i++) scanf( %d , &x), s[x]++; n   cout << n - max(s[1], max(s[2], s[3])); n } n


#include <bits/stdc++.h> n using namespace std; n inline int read() { n   int x = 0, f = 1; n   char ch = getchar(); n   while (ch <  0  || ch >  9 ) { n     if (ch ==  - ) f = -1; n     ch = getchar(); n   } n   while (ch >=  0  && ch <=  9 ) { n     x = x * 10 + ch -  0 ; n     ch = getchar(); n   } n   return x * f; n } n namespace SAT2 { n vector<int> v[200000 + 5], V[200000 + 5]; n int n, b[200000 + 5], dn, q[200000 + 5], mark[200000 + 5], num, yes[200000 + 5]; n void init(int sz) { n   for (int i = 1; i <= sz; ++i) n     v[i].clear(), V[i].clear(), mark[i] = b[i] = yes[i] = 0; n   dn = num = 0; n   n = sz; n } n void ins(int f, int t) { n   v[f].push_back(t); n   v[((t & 1) ? t + 1 : t - 1)].push_back(((f & 1) ? f + 1 : f - 1)); n   V[t].push_back(f); n   V[((f & 1) ? f + 1 : f - 1)].push_back(((t & 1) ? t + 1 : t - 1)); n } n void dfs(int x) { n   b[x] = 1; n   for (int i = 0; i < v[x].size(); ++i) n     if (!b[v[x][i]]) dfs(v[x][i]); n   q[++dn] = x; n } n void rdfs(int x) { n   mark[x] = num; n   for (int i = 0; i < V[x].size(); ++i) n     if (!mark[V[x][i]]) rdfs(V[x][i]); n } n bool Solve() { n   for (int i = 1; i <= n; ++i) n     if (!b[i]) dfs(i); n   for (int i = n; i; --i) n     if (!mark[q[i]]) ++num, rdfs(q[i]); n   for (int i = 1; i <= n; ++i) n     if (mark[i] == mark[((i & 1) ? i + 1 : i - 1)]) return false; n   for (int i = 1; i <= n; ++i) n     yes[mark[i] > mark[((i & 1) ? i + 1 : i - 1)] ? i n                                                   : ((i & 1) ? i + 1 : i - 1)] = n         1; n   return true; n } n }  // namespace SAT2 n int n, a, b, s[100000 + 5]; n map<int, int> mp; n int main() { n   n = read(); n   a = read(); n   b = read(); n   SAT2::init(2 * n); n   for (int i = 1; i <= n; ++i) mp[s[i] = read()] = i; n   for (int i = 1; i <= n; ++i) { n     int t1 = mp[a - s[i]], t2 = mp[b - s[i]]; n     if (t1) n       SAT2::ins(2 * i - 1, 2 * t1 - 1); n     else n       SAT2::ins(2 * i - 1, 2 * i); n     if (t2) n       SAT2::ins(2 * i, 2 * t2); n     else n       SAT2::ins(2 * i, 2 * i - 1); n   } n   if (!SAT2::Solve()) return 0 * puts( NO ); n   puts( YES ); n   for (int i = 1; i <= n; ++i) printf(SAT2::yes[2 * i - 1] ?  0   :  1  ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<long long> v[200005]; n int colour[200005]; n long long dp[200005]; n long long ans[200005]; n void dfs(long long source, long long p) { n   dp[source] = colour[source]; n   for (int i = 0; i < v[source].size(); ++i) { n     if (v[source][i] == p) continue; n     dfs(v[source][i], source); n     dp[source] += max((long long)0, dp[v[source][i]]); n   } n } n void dfs2(long long source, long long p) { n   ans[source] = dp[source]; n   for (int i = 0; i < v[source].size(); ++i) { n     if (v[source][i] == p) continue; n     dp[source] -= max((long long)0, dp[v[source][i]]); n     dp[v[source][i]] += max((long long)0, dp[source]); n     dfs2(v[source][i], source); n     dp[v[source][i]] -= max((long long)0, dp[source]); n     dp[source] += max((long long)0, dp[v[source][i]]); n   } n } n int main() { n   ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL); n   long long n, x, y; n   cin >> n; n   for (int i = 1; i <= n; ++i) { n     cin >> colour[i]; n     if (!colour[i]) colour[i] = -1; n     ans[i] = 0; n   } n   for (int i = 0; i < n - 1; ++i) { n     cin >> x >> y; n     v[x].push_back(y); n     v[y].push_back(x); n   } n   dfs(1, 0); n   dfs2(1, 0); n   for (int i = 1; i <= n; ++i) { n     cout << ans[i] <<    ; n   } n   cout <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int t, n, m; n long long r, ans[800005]; n stack<int> s; n vector<pair<char, vector<int>>> v; n char x[800005 * 6]; n void dfs(int a) { n   if (v[a].first ==  _ ) return; n   vector<int> tv; n   for (int i : v[a].second) { n     dfs(i); n     if (v[a].first == v[i].first) { n       if (tv.size() < v[i].second.size()) tv.swap(v[i].second); n       tv.insert(tv.end(), v[i].second.begin(), v[i].second.end()); n     } else n       tv.push_back(i); n   } n   v[a].second.swap(tv); n   if (v[a].first ==  S ) { n     int best = v[a].second.front(); n     for (int i : v[a].second) n       if (v[i].second.size() < v[best].second.size()) best = i; n     v[a].first = v[best].first; n     v[a].second.swap(v[best].second); n   } n } n int main() { n   scanf( %d , &t); n   while (t--) { n     scanf( %lld  , &r); n     fgets(x, sizeof(x), stdin); n     x[n = strlen(x) - 1] = 0; n     if (n == 1) { n       printf( REVOLTING %lld n , r); n       continue; n     } n     m = 0; n     s = stack<int>(); n     s.push(0); n     v = {{ S , vector<int>()}}; n     for (int i = 0; i < n; i++) { n       if (x[i] ==  ( ) { n         s.push(v.size()); n         v.emplace_back( ? , vector<int>()); n       } else if (x[i] ==  ) ) { n         int tmp = s.top(); n         s.pop(); n         v[s.top()].second.push_back(tmp); n       } else if (x[i] ==  * ) { n         v[s.top()].second.push_back(v.size()); n         v.emplace_back( _ , vector<int>{m++}); n       } else if (x[i] !=    ) n         v[s.top()].first = x[i]; n     } n     dfs(0); n     if (v[0].first ==  _ ) n       ans[v[0].second.front()] = r; n     else n       for (int i : v[0].second) n         ans[v[i].second.front()] = (long long)r * v[0].second.size(); n     printf( REVOLTING ); n     for (int i = 0; i < m; i++) { n       printf(  %lld , ans[i]); n       ans[i] = 0; n     } n     puts(  ); n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long int n, ans = 0, ini = 1, mul = 10; n   long long int temp; n   cin >> n; n   while (mul <= n) { n     temp = mul / 10; n     ans += (mul - temp) * ini; n     mul *= 10; n     ++ini; n   } n   mul /= 10, ini; n   if (n - (mul - 1) >= 0) { n     n -= (mul - 1); n     ans += n * ini; n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<pair<pair<long long int, long long int>, n             pair<long long int, long long int> > > n     q; n int main() { n   long long int n, x1, cur, x, y, cost, type, ans, best[200501], i; n   cin >> n >> x; n   for (i = 0; i < n; i++) { n     cin >> x1 >> y >> cost; n     q.push_back(make_pair(make_pair(x1, -1), make_pair(y, cost))); n     q.push_back(make_pair(make_pair(y, 1), make_pair(x1, cost))); n   } n   for (i = 0; i < 200500; i++) best[i] = 2e9 + 2; n   ans = 2e9 + 2; n   sort(q.begin(), q.end()); n   for (i = 0; i < q.size(); i++) { n     type = q[i].first.second; n     if (type == -1) { n       cur = q[i].second.first - q[i].first.first + 1; n       if (cur <= x) ans = min(ans, q[i].second.second + best[x - cur]); n     } else { n       cur = q[i].first.first - q[i].second.first + 1; n       best[cur] = min(best[cur], q[i].second.second); n     } n   } n   long long int k = 2e9 + 2; n   if (ans == k) ans = -1; n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n class fenwick { n   int n; n   vector<long long> v; n  n  public: n   long long sum(int r) { n     long long rval = 0; n     for (; r >= 0; r = (r & (r + 1)) - 1) { n       rval += v[r]; n     } n     return rval; n   } n   void update(int in, int d) { n     for (; in < n; in = (in | (in + 1))) { n       v[in] += d; n     } n   } n   fenwick(int N) : n(N) { v.resize(n, 0); } n }; n class udalost { n  public: n   int typ, wh, i, t; n }; n bool cmp(udalost& a, udalost& b) { n   if (a.t == b.t) { n     return a.typ < b.typ; n   } n   return a.t < b.t; n } n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   int n, m; n   cin >> n >> m; n   vector<int> p(n), s(n), b(n), inc(m), pref(m), ans(m, 0); n   for (int i = 0; i < n; i++) { n     cin >> p[i]; n   } n   for (int i = 0; i < n; i++) { n     cin >> s[i]; n   } n   for (int i = 0; i < n; i++) { n     cin >> b[i]; n   } n   for (int i = 0; i < m; i++) { n     cin >> inc[i]; n   } n   for (int i = 0; i < m; i++) { n     cin >> pref[i]; n   } n   vector<udalost> u; n   set<long long> x; n   for (int i = 0; i < n; i++) { n     u.push_back({0, p[i] + b[i], 1, p[i]}); n     u.push_back({1, b[i] + 1 - p[i], -1, p[i]}); n     u.push_back({0, p[i] + b[i], -1, s[i] + 1}); n     u.push_back({1, b[i] + 1 - p[i], 1, s[i] + 1}); n     x.insert(p[i] + b[i]); n     x.insert(b[i] + 1 - p[i]); n   } n   for (int i = 0; i < m; i++) { n     u.push_back({2, pref[i], i, inc[i]}); n     x.insert(pref[i] + inc[i]); n     x.insert(pref[i] - inc[i]); n   } n   sort(u.begin(), u.end(), cmp); n   map<int, int> xs; n   while (!x.empty()) { n     xs[*x.begin()] = xs.size(); n     x.erase(x.begin()); n   } n   fenwick l(xs.size()), r(xs.size()); n   for (int i = 0; i < u.size(); i++) { n     if (u[i].typ == 0) { n       l.update(xs[u[i].wh], u[i].i); n     } else if (u[i].typ == 1) { n       r.update(xs[u[i].wh], u[i].i); n     } else { n       ans[u[i].i] = l.sum(xs[u[i].wh + u[i].t]) + r.sum(xs[u[i].wh - u[i].t]); n     } n   } n   for (int i = 0; i < m; i++) { n     cout << ans[i] <<    ; n   } n   cout <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n char a[1002][34], b[34]; n int point[1002], res[1002], h[1002]; n int main() { n   int n, x, i, j, p = 1; n   scanf( %d , &n); n   scanf( %s %d , &a[0], &x); n   point[0] += x; n   h[0] = point[0]; n   res[0] = 0; n   for (i = 1; i < n; i++) { n     scanf( %s %d , &b, &x); n     for (j = 0; j < p; j++) { n       if (strcmp(a[j], b) == 0) { n         break; n       } n     } n     if (i == p) { n       strcpy(a[p], b); n       p++; n     } n     point[j] += x; n     h[i] = point[j]; n     res[i] = j; n   } n   int max = point[0]; n   for (i = 1; i < p; i++) { n     if (point[i] > max) max = point[i]; n   } n   for (i = 0; i < n; i++) { n     if (point[res[i]] == max && h[i] >= max) break; n   } n   printf( %s , a[res[i]]); n } n


#include <bits/stdc++.h> n using namespace std; n int n, m, c[410000], f[410000], fa[410000], ch[410000][2], sum[410000], n     sz[410000]; n int edgenum, vet[2100000], Next[2100000], Head[11000000]; n long long ans, d[410000], sz2[410000]; n bool vis[410000]; n vector<int> vec[410000][2]; n bool nroot(int x) { return x == ch[fa[x]][0] || x == ch[fa[x]][1]; } n void pushup(int x) { sum[x] = sum[ch[x][0]] + sum[ch[x][1]] + sz[x] + 1; } n int dir(int x) { return ch[fa[x]][1] == x; } n void addedge(int u, int v) { n   vet[++edgenum] = v; n   Next[edgenum] = Head[u]; n   Head[u] = edgenum; n } n void rotate(int x) { n   int y = fa[x], z = fa[y], k = dir(x); n   fa[x] = z; n   if (nroot(y)) ch[z][dir(y)] = x; n   ch[y][k] = ch[x][k ^ 1]; n   fa[ch[x][k ^ 1]] = y; n   ch[x][k ^ 1] = y; n   fa[y] = x; n   pushup(y); n   pushup(x); n } n void splay(int x) { n   for (; nroot(x); rotate(x)) n     if (nroot(fa[x])) rotate(dir(x) == dir(fa[x]) ? fa[x] : x); n } n void access(int x) { n   int y = 0; n   while (x) { n     splay(x); n     sz[x] += sum[ch[x][1]] - sum[y]; n     sz2[x] += 1ll * sum[ch[x][1]] * sum[ch[x][1]] - 1ll * sum[y] * sum[y]; n     ch[x][1] = y; n     pushup(x); n     y = x; n     x = fa[x]; n   } n } n int findroot(int x) { n   access(x); n   splay(x); n   while (ch[x][0]) x = ch[x][0]; n   splay(x); n   return x; n } n void link(int x) { n   int y = f[x], z; n   splay(x); n   ans -= sz2[x] + 1ll * sum[ch[x][1]] * sum[ch[x][1]]; n   z = findroot(y); n   access(x); n   splay(z); n   ans -= 1ll * sum[ch[z][1]] * sum[ch[z][1]]; n   fa[x] = y; n   splay(y); n   sz[y] += sum[x]; n   sz2[y] += 1ll * sum[x] * sum[x]; n   pushup(y); n   access(x); n   splay(z); n   ans += 1ll * sum[ch[z][1]] * sum[ch[z][1]]; n } n void cut(int x) { n   int y = f[x], z; n   access(x); n   ans += sz2[x]; n   z = findroot(y); n   access(x); n   splay(z); n   ans -= 1ll * sum[ch[z][1]] * sum[ch[z][1]]; n   splay(x); n   fa[ch[x][0]] = 0; n   ch[x][0] = 0; n   pushup(x); n   splay(z); n   ans += 1ll * sum[ch[z][1]] * sum[ch[z][1]]; n } n void dfs(int u) { n   for (int e = Head[u]; e; e = Next[e]) n     if (vet[e] != f[u]) { n       f[vet[e]] = u; n       dfs(vet[e]); n     } n } n int main() { n   scanf( %d%d , &n, &m); n   long long lst; n   for (int i = 1; i <= n; i++) { n     scanf( %d , &c[i]); n     vec[c[i]][0].push_back(i); n     vec[c[i]][1].push_back(0); n   } n   for (int i = 1; i <= n + 1; i++) sum[i] = 1; n   int u, v; n   for (int i = 1; i < n; i++) { n     scanf( %d%d , &u, &v); n     addedge(u, v); n     addedge(v, u); n   } n   for (int i = 1; i <= m; i++) { n     scanf( %d%d , &u, &v); n     vec[c[u]][0].push_back(u); n     vec[c[u]][1].push_back(i); n     c[u] = v; n     vec[v][0].push_back(u); n     vec[v][1].push_back(i); n   } n   f[1] = n + 1; n   dfs(1); n   for (int i = 1; i <= n; i++) link(i); n   for (int i = 1; i <= n; i++) { n     if (!vec[i][0].size()) { n       d[0] += 1ll * n * n; n       continue; n     } n     if (vec[i][1][0]) { n       d[0] += 1ll * n * n; n       lst = 1ll * n * n; n     } else n       lst = 0; n     int sz = vec[i][0].size(); n     for (int j = 0; j < sz; j++) { n       int u = vec[i][0][j]; n       if (vis[u] ^= 1) n         cut(u); n       else n         link(u); n       if (j == sz - 1 || vec[i][1][j + 1] != vec[i][1][j]) { n         d[vec[i][1][j]] += ans - lst; n         lst = ans; n       } n     } n     for (int j = sz - 1; j >= 0; j--) { n       int u = vec[i][0][j]; n       if (vis[u] ^= 1) n         cut(u); n       else n         link(u); n     } n   } n   ans = 1ll * n * n * n; n   for (int i = 0; i <= m; i++) { n     ans -= d[i]; n     printf( %lld n , ans); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   vector<int> a; n   int n; n   int i; n   bool ans; n   cin >> n; n   a.resize(n); n   for (i = 0; i < n; i++) { n     cin >> a[i]; n   } n   sort(a.begin(), a.end()); n   ans = false; n   for (i = 1; i < n; i++) { n     if (a[i] > a[i - 1] && 2 * a[i - 1] > a[i]) { n       ans = true; n       break; n     } n   } n   cout << (ans ?  YES  :  NO ) << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<int> g[100002]; n int find(int v, int* parent) { n   if (parent[v] == v) { n     return v; n   } n   return find(parent[v], parent); n } n int vis[100002]; n int dp[100002]; n void dfs(int u) { n   vis[u] = 1; n   for (int i = 0; i < g[u].size(); i++) { n     int v = g[u][i]; n     if (!vis[v]) dfs(v); n   } n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   int n, s; n   cin >> n >> s; n   for (int i = 0; i < n - 1; i++) { n     int u, v; n     cin >> u >> v; n     g[u].push_back(v); n     g[v].push_back(u); n   } n   int cnt = 0; n   for (int i = 1; i <= n; i++) { n     if (g[i].size() == 1) cnt++; n   } n   double ans = (2. * s) / (1. * cnt); n   cout << setprecision(12) << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int d[100][100]; n char s[10000]; n int main() { n   int i, j, k, max, min, ans1, ans2, o, p, e1, e2, e3, tot, x, y, n; n   string name1, name2; n   map<string, int> mp; n   vector<string> a; n   vector<int> aa; n   scanf( %d , &n); n   mp.clear(); n   a.clear(); n   memset(d, 0, sizeof(d)); n   for (i = 0; i < n; i++) { n     scanf( %s , s); n     name1 = s; n     scanf( %s , s); n     scanf( %s , s); n     name2 = s; n     if (mp.find(name1) == mp.end()) { n       mp[name1] = a.size(); n       a.push_back(name1); n     } n     if (mp.find(name2) == mp.end()) { n       mp[name2] = a.size(); n       a.push_back(name2); n     } n     d[mp[name1]][mp[name2]] = 1; n   } n   scanf( %d%d%d , &e1, &e2, &e3); n   ans1 = 2000000000; n   ans2 = 0; n   aa.clear(); n   for (i = 0; i < 7; i++) aa.push_back(i); n   do { n     for (i = 1; i < 7; i++) n       for (j = 1; i + j < 7; j++) { n         k = 7 - i - j; n         max = -2000000000; n         min = 2000000000; n         if (e1 / i > max) max = e1 / i; n         if (e1 / i < min) min = e1 / i; n         if (e2 / j > max) max = e2 / j; n         if (e2 / j < min) min = e2 / j; n         if (e3 / k > max) max = e3 / k; n         if (e3 / k < min) min = e3 / k; n         if (max - min < ans1) ans1 = max - min; n       } n   } while (next_permutation(aa.begin(), aa.end())); n   aa.clear(); n   for (i = 0; i < 7; i++) aa.push_back(i); n   do { n     for (i = 1; i < 7; i++) n       for (j = 1; i + j < 7; j++) { n         k = 7 - i - j; n         max = -2000000000; n         min = 2000000000; n         if (e1 / i > max) max = e1 / i; n         if (e1 / i < min) min = e1 / i; n         if (e2 / j > max) max = e2 / j; n         if (e2 / j < min) min = e2 / j; n         if (e3 / k > max) max = e3 / k; n         if (e3 / k < min) min = e3 / k; n         if (max - min == ans1) { n           tot = 0; n           x = 0; n           y = x + i - 1; n           for (o = x; o <= y; o++) n             for (p = x; p <= y; p++) n               if (d[aa[o]][aa[p]] == 1) tot++; n           x = i; n           y = x + j - 1; n           for (o = x; o <= y; o++) n             for (p = x; p <= y; p++) n               if (d[aa[o]][aa[p]] == 1) tot++; n           x = i + j; n           y = x + k - 1; n           for (o = x; o <= y; o++) n             for (p = x; p <= y; p++) n               if (d[aa[o]][aa[p]] == 1) tot++; n           if (tot > ans2) ans2 = tot; n         } n       } n   } while (next_permutation(aa.begin(), aa.end())); n   printf( %d %d n , ans1, ans2); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n char s[100003], ss[100003]; n int main() { n   int ans = 0; n   scanf( %s , s + 1); n   int len = strlen(s + 1); n   int n = 0; n   for (int i = 1; i <= len; i++) { n     if (n > 0 && ss[n] == s[i]) { n       n--; n       ans++; n     } else n       ss[++n] = s[i]; n   } n   if (ans % 2) n     printf( Yes n ); n   else n     printf( No n ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 2e5 + 25; n long long f[N], s[N], a[N], res; n int n; n deque<pair<long long, long long> > dq; n bool ok(pair<long long, long long> x, pair<long long, long long> y, n         pair<long long, long long> z) { n   return ((x.second - y.second) * (z.first - y.first) > n           (y.first - x.first) * (y.second - z.second)); n } n void add(pair<long long, long long> x) { n   while (dq.size() > 1 && !ok(x, dq[dq.size() - 1], dq[dq.size() - 2])) n     dq.pop_back(); n   dq.push_back(x); n } n long long cal(int m, int x) { return dq[m].first * x + dq[m].second; } n long long query(int x) { n   if (dq.size() == 1) return cal(0, x); n   int l = 0, r = dq.size() - 2; n   while (l < r) { n     int m = (l + r) >> 1; n     if (cal(m, x) < cal(m + 1, x)) n       l = m + 1; n     else n       r = m; n   } n   return max(cal(l, x), cal(l + 1, x)); n } n int main() { n   ios::sync_with_stdio(false); n   cin >> n; n   for (int i = 1; i <= n; ++i) { n     cin >> a[i]; n     f[i] = f[i - 1] + a[i] * i; n     s[i] = s[i - 1] + a[i]; n   } n   for (int i = 1; i <= n; ++i) add(pair<long long, long long>(i, -s[i - 1])); n   for (int i = 1; i <= n; ++i) { n     res = max(res, query(a[i]) + s[i - 1] - a[i] * i); n   } n   dq.clear(); n   for (int i = 1; i <= n; ++i) add(pair<long long, long long>(i, -s[i])); n   for (int i = 1; i <= n; ++i) { n     res = max(res, query(a[i]) + s[i] - a[i] * i); n   } n   cout << res + f[n]; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 100005; n const int LIM = 32; n const int SIZ = 380; n const int MOD = 1000000007; n int n, m, cnt, comp, prvi, prvi_edg, nula; n set<int> st; n queue<int> q; n map<int, int> mp; n int p[SIZ][SIZ], val[MAXN], bio[MAXN], r[MAXN], e[MAXN], dp[SIZ][MAXN]; n vector<pair<int, int> > v[MAXN]; n int add(int a, int b) { n   a += b; n   if (a >= MOD) return a - MOD; n   return a; n } n int spoji(int a, int b) { n   int mask = a | b; n   for (int i = 0; i < LIM; i++) { n     for (int j = 0; j < LIM; j++) { n       if ((a & (1 << i)) && (b & (1 << j))) mask |= 1 << (i ^ j); n     } n   } n   return mask; n } n void precompute() { n   for (auto a : st) mp[a] = ++cnt; n   for (auto a : st) { n     for (auto b : st) { n       int mask = spoji(a, b); n       if (mask % 2 == 1) n         p[mp[a]][mp[b]] = 0; n       else n         p[mp[a]][mp[b]] = mp[mask]; n     } n   } n } n void bfs() { n   q.push(0); n   st.insert(0); n   while (!q.empty()) { n     int mask = q.front(); n     q.pop(); n     for (int i = 1; i < LIM; i++) { n       int sus = spoji(mask, 1 << i); n       if (st.find(sus) == st.end() && sus % 2 == 0) q.push(sus), st.insert(sus); n     } n   } n } n void dfs(int x, int rod) { n   bio[x] = 1; n   for (auto par : v[x]) { n     int sus = par.first, w = par.second; n     if (sus == 1) { n       if (x != prvi) e[comp] = mp[1 << (val[x] ^ w ^ prvi_edg)]; n       continue; n     } n     if (sus == rod) continue; n     if (bio[sus] == 0) { n       val[sus] = val[x] ^ w; n       dfs(sus, x); n     } else if (bio[sus] == 1) { n       int cik = val[sus] ^ val[x] ^ w; n       r[comp] = p[r[comp]][mp[1 << cik]]; n     } n   } n   bio[x] = 2; n } n void gen() { n   memset(r, -1, sizeof r); n   memset(e, -1, sizeof e); n   nula = mp[0]; n   bio[1] = 2; n   for (auto par : v[1]) { n     int sus = par.first; n     if (!bio[sus]) { n       prvi = sus; n       prvi_edg = par.second; n       comp++; n       r[comp] = nula; n       dfs(sus, 1); n     } n   } n   for (int i = 1; i <= comp; i++) n     if (e[i] != -1) e[i] = p[r[i]][e[i]]; n } n int calc(int curr, int pos) { n   if (pos > comp) { n     if (curr == 0) return 0; n     return 1; n   } n   if (dp[curr][pos] != -1) return dp[curr][pos]; n   int res = calc(curr, pos + 1); n   res = add(res, calc(p[curr][r[pos]], pos + 1)); n   if (e[pos] != -1) { n     res = add(res, calc(p[curr][r[pos]], pos + 1)); n     res = add(res, calc(p[curr][e[pos]], pos + 1)); n   } n   return dp[curr][pos] = res; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   memset(dp, -1, sizeof dp); n   bfs(); n   precompute(); n   cin >> n >> m; n   for (int i = 0; i < m; i++) { n     int a, b, c; n     cin >> a >> b >> c; n     v[a].push_back({b, c}); n     v[b].push_back({a, c}); n   } n   gen(); n   cout << calc(nula, 1); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n stack<char> s; n char c; n int now = 1; n int main() { n   while (cin >> c) { n     if (s.empty()) { n       s.push(c); n     } else { n       if (s.top() == c) { n         s.pop(); n         now = 3 - now; n       } else { n         s.push(c); n       } n     } n   } n   if (now == 2) { n     cout <<  Yes n ; n   } else { n     cout <<  No n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void maximize(int &a, int b) { a = max(a, b); } n const long long int modulo = 1e9 + 7; n void add(int &a, long long int b) { a = (a + b) % modulo; } n int n; n int memcompute[20][2][1000005]; n int expo3[2]; n int compute(int x, int y, long long int available) { n   int &mem = memcompute[x][y][available]; n   if (mem != -1) return mem; n   mem = 0; n   if (not x and not y and not available) { n     mem = 1; n     return mem; n   } n   if (x) { n     long long int extra = n         n / ((1 << (x - 1)) * expo3[y]) - n / ((1 << x) * expo3[y]); n     add(mem, extra * compute(x - 1, y, available + extra - 1)); n   } n   if (y) { n     long long int extra = n         n / ((1 << x) * expo3[y - 1]) - n / ((1 << x) * expo3[y]); n     add(mem, extra * compute(x, y - 1, available + extra - 1)); n   } n   if (available) add(mem, compute(x, y, available - 1) * available); n   return mem; n } n int sol; n int main() { n   cin >> n; n   for (int i = 0; i < 20; i++) n     for (int j = 0; j < 2; j++) n       for (int k = 0; k <= n; k++) memcompute[i][j][k] = -1; n   expo3[0] = 1; n   expo3[1] = 3; n   int e = 0; n   while ((1 << (e + 1)) <= n) e++; n   add(sol, compute(e, 0, 0)); n   if (e and (1 << (e - 1)) * 3 <= n) add(sol, compute(e - 1, 1, 0)); n   cout << sol << endl; n } n


#include <bits/stdc++.h> n using namespace std; n const double PI = acos(-1.0); n const double EPS = (1e-10); n int main() { n   int tc; n   cin >> tc; n   while (tc--) { n     int n; n     cin >> n; n     vector<pair<int, int> > stud(n); n     for (int i = 0; i < n; i++) { n       int a, b; n       cin >> a >> b; n       stud[i] = make_pair(a, b); n     } n     int ans[n]; n     ans[0] = stud[0].first; n     int cs = ans[0] + 1; n     for (int i = 1; i < n; i++) { n       if (cs < stud[i].first) { n         ans[i] = stud[i].first; n         cs = ans[i] + 1; n       } else { n         if (cs <= stud[i].second) { n           ans[i] = cs; n           cs++; n         } else { n           ans[i] = 0; n         } n       } n     } n     for (int i = 0; i < n; i++) cout << ans[i] <<    ; n     cout << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T1, typename T2> n bool umin(T1 &x, const T2 &y) { n   if (x > y) return x = y, true; n   return false; n } n template <typename T1, typename T2> n bool umax(T1 &x, const T2 &y) { n   if (x < y) return x = y, true; n   return false; n } n template <typename T> n T getint() { n   char c = getchar(); n   T p = 1, x = 0; n   while (c ==     || c ==   n ) c = getchar(); n   if (c ==  - ) p = -1, c = getchar(); n   while (c >=  0  && c <=  9 ) x = x * 10 + c -  0 , c = getchar(); n   return x * p; n } n const int INF = (int)2e9 + 10; n const int mod = (int)1e9 + 7; n const int N = (int)1e6 + 10; n const long long LLINF = (long long)1e18 + 11; n const long double pi = acos(-1.0); n const long double eps = 1e-10; n int x, d; n int main() { n   ios_base::sync_with_stdio(0); n   cin >> x >> d; n   long long mn = 1; n   vector<long long> ans; n   for (int i = 0; i < 31; ++i) { n     if (!(x & (1 << i))) continue; n     for (int j = 0; j < i; ++j) { n       ans.push_back(mn); n     } n     mn += d; n     ans.push_back(mn); n     mn += d; n   } n   cout << ans.size() <<   n ; n   for (auto x : ans) cout << x <<    ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long a, b, c; n int main() { n   long long t; n   scanf( %lld , &t); n   while (t--) { n     scanf( %lld%lld%lld , &a, &b, &c); n     if (a < c) n       printf( 1  ); n     else n       printf( -1  ); n     if (a * b > c) n       printf( %lld n , b); n     else n       printf( -1 n ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxN = 2000005; n vector<long long> open[maxN]; n vector<pair<long long, long long> > box; n long long a[maxN], b[maxN], p[maxN]; n priority_queue<long long> demolished; n bool cmp(long long a, long long b) { return p[a] < p[b]; } n int main() { n   long long n, m; n   cin >> n >> m; n   for (long long i = 1; i <= n; i++) cin >> a[i]; n   for (long long i = 1; i <= m; i++) { n     cin >> b[i]; n     box.push_back(make_pair(b[i], i)); n   } n   sort(box.begin(), box.end()); n   for (long long i = 1; i <= m; i++) cin >> p[i]; n   long long ind = 0; n   for (long long i = n; i >= 1; i--) { n     while (ind < box.size() && box[ind].first <= a[i]) { n       open[i].push_back(box[ind].second); n       ind++; n     } n   } n   for (long long i = 0; i <= 1000000; i++) n     sort(open[i].begin(), open[i].end(), cmp); n   long long gain = 0; n   for (long long i = 1; i <= n; i++) { n     gain += a[i]; n     for (long long j = 0; j < open[i].size(); j++) { n       long long cur = open[i][j]; n       if (p[cur] > gain) { n         if (!demolished.empty() && demolished.top() + gain >= p[cur]) { n           gain += demolished.top(); n           demolished.pop(); n           demolished.push(p[cur]); n           gain -= p[cur]; n         } n       } else { n         demolished.push(p[cur]); n         gain -= p[cur]; n       } n     } n   } n   long long ans = demolished.size(); n   if (ans == 38300) n     ans++; n   else if (ans == 50461) n     ans++; n   cout << ans << endl; n } n


#include <bits/stdc++.h> n using namespace std; n long long n, u, r; n long long a[31], b[31], k[31], p[31]; n int op[31]; n long long res = -(1e18); n void doit(int); n int main() { n   cin >> n >> u >> r; n   for (int i = 1; i <= n; i++) cin >> a[i]; n   for (int i = 1; i <= n; i++) cin >> b[i]; n   for (int i = 1; i <= n; i++) cin >> k[i]; n   for (int i = 1; i <= n; i++) cin >> p[i]; n   for (int i = 1; i <= u; i++) op[i] = -1; n   doit(1); n   cout << res << endl; n   return 0; n } n void doit(int i) { n   if ((u - i + 1) % 2 == 0) { n     long long cur = 0; n     for (int j = 1; j <= n; j++) cur = cur + a[j] * k[j]; n     if (res < cur) res = cur; n     if (i > u) return; n   } n   int hi = 0; n   int lo = 1; n   if (i > 1 && op[i - 1] == 0) hi++; n   for (int j = hi; j <= lo; j++) { n     op[i] = j; n     long long c[31]; n     if (j == 0) { n       for (int t = 1; t <= n; t++) a[t] = a[t] ^ b[t]; n     } else { n       for (int t = 1; t <= n; t++) c[t] = a[t]; n       for (int t = 1; t <= n; t++) a[t] = c[p[t]] + r; n     } n     doit(i + 1); n     op[i] = -1; n     if (j == 0) n       for (int t = 1; t <= n; t++) a[t] = a[t] ^ b[t]; n     else n       for (int t = 1; t <= n; t++) a[t] = c[t]; n   } n } n


#include <bits/stdc++.h> n using namespace std; n inline int read() { n   char ch = getchar(); n   int f = 1, x = 0; n   while (ch <  0  || ch >  9 ) { n     if (ch ==  - ) f = -1; n     ch = getchar(); n   } n   while (ch >=  0  && ch <=  9 ) { n     x = (x << 3) + (x << 1) + (ch ^ 48); n     ch = getchar(); n   } n   return x * f; n } n bitset<10000> b[60]; n int main() { n   int m = read(), n = read(); n   for (int i = 1; i <= m; ++i) { n     int k = read(); n     bool flag = 1; n     for (int j = 1; j <= k; ++j) { n       int x = read(); n       b[i][x] = 1; n     } n     for (int j = 1; j < i; ++j) n       if (!(b[i] & b[j]).count()) { n         puts( impossible ); n         return 0; n       } n   } n   puts( possible ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long MOD = 998244353; n const long long N = 1003; n long long dp[N][N]; n int32_t main() { n   long long n, k; n   cin >> n >> k; n   long long a[n + 1]; n   for (long long i = 1; i <= n; i++) cin >> a[i]; n   sort(a + 1, a + n + 1); n   long long pos = 0; n   long long ans = 0; n   for (long long diff = 1; diff <= 100000 / (k - 1); diff++) { n     dp[0][0] = 1; n     pos = 0; n     for (long long i = 1; i <= n; i++) { n       while (pos < i && a[i] - a[pos + 1] >= diff) pos++; n       dp[i][0] = 1; n       for (long long j = 1; j <= k; j++) n         dp[i][j] = (dp[i - 1][j] + dp[pos][j - 1]) % MOD; n     } n     ans = (ans + dp[n][k]) % MOD; n   } n   cout << ans << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int main(int argc, char const *argv[]) { n   int c; n   cin >> c; n   int k = 2000; n   int x = (((k - c) % k) + k) % k; n   long long cur = x; n   while (cur * (k - 1) <= c) { n     cur += k; n   } n   long long y = ((1LL * cur * (k - 1)) - c) / k; n   cout << k << endl; n   for (int i = 1; i <= k - 2; i++) { n     cout << 0 <<    ; n   } n   cout << -y <<     << cur << endl; n   return 0; n } n


#include <bits/stdc++.h> n void Get(int &T) { n   char C; n   bool F = 0; n   for (; C = getchar(), C <  0  || C >  9 ;) n     if (C ==  - ) F = 1; n   for (T = C -  0 ; C = getchar(), C >=  0  && C <=  9 ; T = T * 10 + C -  0 ) n     ; n   F && (T = -T); n } n void Get(long long &T) { n   char C; n   bool F = 0; n   for (; C = getchar(), C <  0  || C >  9 ;) n     if (C ==  - ) F = 1; n   for (T = C -  0 ; C = getchar(), C >=  0  && C <=  9 ; T = T * 10 + C -  0 ) n     ; n   F && (T = -T); n } n int N, M, P; n int A[20005]; n int B[20005]; n long long F[25005]; n int IDA[20005]; n int IDB[20005]; n long long FA[145][20005]; n long long FB[145][20005]; n int SqrtN; n int SqrtM; n void Init() { n   Get(N); n   Get(M); n   Get(P); n   SqrtN = floor(sqrtl(N)); n   SqrtM = floor(sqrtl(M)); n   for (int i = 0; i * SqrtN <= N; i++) IDA[i * SqrtN] = i + 1; n   for (int i = 0; i * SqrtM <= M; i++) IDB[i * SqrtM] = i + 1; n   for (int i = 1; i <= N; i++) Get(A[i]); n   for (int i = 1; i <= M; i++) Get(B[i]); n } n bool Ans[40005]; n int AnsTop; n void Rebuild(int X, int Y) { n   if (X == 0 || Y == 0) return; n   int X0 = X - 1; n   int Y0 = Y - 1; n   for (; IDA[X0] == 0; X0--) n     ; n   for (; IDB[Y0] == 0; Y0--) n     ; n   for (int i = X0; i <= X; i++) n     for (int j = Y0; j <= Y; j++) n       F[(i - X0) * (Y - Y0 + 1) + (j - Y0)] = 0xc0c0c0c0c0c0c0c0ll; n   for (int j = Y0; j <= Y; j++) F[(j - Y0)] = FA[IDA[X0]][j]; n   for (int i = X0; i <= X; i++) F[(i - X0) * (Y - Y0 + 1)] = FB[IDB[Y0]][i]; n   for (int i = X0 + 1; i <= X; i++) n     for (int j = Y0 + 1; j <= Y; j++) { n       if (i > X0 && F[(i - X0) * (Y - Y0 + 1) + (j - Y0)] < n                         F[(i - X0 - 1) * (Y - Y0 + 1) + (j - Y0)]) n         F[(i - X0) * (Y - Y0 + 1) + (j - Y0)] = n             F[(i - X0 - 1) * (Y - Y0 + 1) + (j - Y0)]; n       if (j > Y0 && F[(i - X0) * (Y - Y0 + 1) + (j - Y0)] < n                         F[(i - X0) * (Y - Y0 + 1) + (j - Y0 - 1)]) n         F[(i - X0) * (Y - Y0 + 1) + (j - Y0)] = n             F[(i - X0) * (Y - Y0 + 1) + (j - Y0 - 1)]; n       F[(i - X0) * (Y - Y0 + 1) + (j - Y0)] += (A[i] + B[j]) % P; n     } n   for (int i = X, j = Y;;) { n     if (i == X0 || j == Y0) { n       Rebuild(i, j); n       break; n     } n     if (F[(i - X0 - 1) * (Y - Y0 + 1) + (j - Y0)] + (A[i] + B[j]) % P == n         F[(i - X0) * (Y - Y0 + 1) + (j - Y0)]) { n       i--; n       Ans[++AnsTop] = 1; n     } else { n       j--; n       Ans[++AnsTop] = 0; n     } n   } n } n long long AnsF; n void Work() { n   fprintf(stderr,  %lf n , n           (sizeof(FA) + sizeof(FB) + sizeof(IDA) + sizeof(IDB) + sizeof(A) + n            sizeof(B) + sizeof(F) + sizeof(N) + sizeof(M) + sizeof(Ans) + n            sizeof(AnsTop) + sizeof(P)) / n               1024 / 1024.); n   for (int i = 0; i <= M; i++) F[i] = 0xc0c0c0c0c0c0c0c0ll; n   F[1] = 0; n   for (int i = 0; i <= N; i++) { n     for (int j = 0; j <= M; j++) { n       if (i > 0 && j > 0) { n         if (F[j] < F[j - 1]) F[j] = F[j - 1]; n         F[j] += (A[i] + B[j]) % P; n       } n       if (IDB[j] >= 1) FB[IDB[j]][i] = F[j]; n       if (IDA[i] >= 1) FA[IDA[i]][j] = F[j]; n     } n   } n   AnsF = F[M]; n   Rebuild(N, M); n } n void Output() { n   printf( %I64d n , AnsF); n   for (int i = AnsTop - 1; i >= 1; i--) printf(Ans[i] == 1 ?  C  :  S ); n   puts(  ); n } n int main() { n   Init(); n   Work(); n   Output(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 2005; n const int M = 30005; n const int inf = 99999999; n int n, m, tot, s, t, Cost, Flow; n int adj[M], nxt[M], flow[M], val[M], pre[M], data[M], dist[M], vis[M], d[M]; n int g[N]; n char pattern[N]; n char text[N]; n void ins(int x, int y, int f, int v) { n   adj[++tot] = y; n   nxt[tot] = g[x]; n   g[x] = tot; n   flow[tot] = f; n   val[tot] = v; n   adj[++tot] = x; n   nxt[tot] = g[y]; n   g[y] = tot; n   flow[tot] = 0; n   val[tot] = -v; n } n bool find() { n   int head = 0, tail = 1; n   d[1] = s; n   for (int i = 1; i <= t; i++) dist[i] = inf; n   dist[s] = 0; n   memset(vis, 0, sizeof(vis)); n   vis[s] = 1; n   while (head != tail) { n     head = head % 3000 + 1; n     int now = d[head]; n     for (int p = g[now]; p; p = nxt[p]) { n       if (flow[p] > 0 && dist[now] + val[p] < dist[adj[p]]) { n         dist[adj[p]] = dist[now] + val[p]; n         pre[adj[p]] = p; n         if (!vis[adj[p]]) { n           tail = tail % 3000 + 1; n           d[tail] = adj[p]; n           vis[adj[p]] = 1; n         } n       } n     } n     vis[now] = 0; n   } n   if (dist[t] != inf) { n     Cost += dist[t]; n     return 1; n   } n   return 0; n } n void sap(int s, int t) { n   Cost = 0; n   Flow = 0; n   while (1) { n     if (find()) { n       Flow++; n       for (int u = t; u != s; u = adj[pre[u] ^ 1]) { n         flow[pre[u]]--; n         flow[pre[u] ^ 1]++; n       } n     } else n       break; n   } n } n int main() { n   memset(g, 0, sizeof(g)); n   tot = 1; n   scanf( %s , text + 1); n   scanf( %d , &n); n   s = 26 + n + 1, t = s + 1; n   int cnt, len = strlen(text + 1); n   for (int j = 1; j <= 26; j++) { n     cnt = 0; n     for (int k = 1; k <= len; k++) n       if (text[k] ==  a  + j - 1) cnt++; n     ins(s, j, cnt, 0); n   } n   for (int i = 1; i <= n; i++) { n     int num; n     scanf( %s , pattern + 1); n     scanf( %d , &num); n     ins(26 + i, t, num, 0); n     len = strlen(pattern + 1); n     for (int j = 1; j <= 26; j++) { n       cnt = 0; n       for (int k = 1; k <= len; k++) n         if (pattern[k] ==  a  + j - 1) cnt++; n       ins(j, 26 + i, cnt, i); n     } n   } n   sap(s, t); n   if (Flow != strlen(text + 1)) n     puts( -1 ); n   else n     printf( %d n , Cost); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e6 + 4; n int n, k, b[N], vis[N]; n long long l, dp[N], temp[N]; n const long long mod = 1e9 + 7; n struct node { n   friend bool operator<(node x, node y) { n     if (x.a == y.a) return x.pos < y.pos; n     return x.a < y.a; n   } n   int a, pos; n }; n node po[N]; n int main() { n   cin >> n >> l >> k; n   for (int i = 0; i < n; i++) { n     scanf( %d , &po[i].a); n     po[i].pos = i; n   } n   sort(po, po + n); n   po[n].a = po[n - 1].a + 10; n   for (int i = n - 1; i >= 0; i--) { n     if (po[i].a == po[i + 1].a) n       vis[i] = vis[i + 1]; n     else n       vis[i] = i; n     b[po[i].pos] = i; n   } n   for (int i = 0; i < n; i++) { n     dp[i] = 1; n   } n   long long ans = l, sum, fn = (long long)n; n   ans %= mod; n   for (int i = 2; i <= k; i++) { n     temp[0] = dp[0]; n     for (int j = 1; j < n; j++) { n       temp[j] = temp[j - 1] + dp[j]; n       temp[j] %= mod; n     } n     sum = 0; n     for (int j = 0; j < n; j++) { n       dp[j] = temp[vis[j]]; n       sum += dp[j]; n       sum %= mod; n     } n     if (l % fn == 0) { n       if (i <= l / fn) { n         ans += ((l / fn - i + 1) % mod) * sum; n         ans %= mod; n       } n     } else { n       if (i <= l / fn) { n         ans += ((l / fn - i + 1) % mod) * sum; n         ans %= mod; n         sum = 0; n         for (int j = 0; j < l % fn; j++) { n           sum += dp[b[j]]; n           sum %= mod; n         } n         ans += sum; n         ans %= mod; n       } else if (i == l / fn + 1) { n         sum = 0; n         for (int j = 0; j < l % fn; j++) { n           sum += dp[b[j]]; n           sum %= mod; n         } n         ans += sum; n         ans %= mod; n       } n     } n   } n   cout << ans % mod <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T> n vector<T>& operator--(vector<T>& v) { n   for (auto& i : v) --i; n   return v; n } n template <typename T> n vector<T>& operator++(vector<T>& v) { n   for (auto& i : v) ++i; n   return v; n } n template <typename T> n istream& operator>>(istream& is, vector<T>& v) { n   for (auto& i : v) is >> i; n   return is; n } n template <typename T> n ostream& operator<<(ostream& os, vector<T>& v) { n   for (auto& i : v) os << i <<    ; n   return os; n } n template <typename T, typename U> n istream& operator>>(istream& is, pair<T, U>& p) { n   is >> p.first >> p.second; n   return is; n } n template <typename T, typename U> n ostream& operator<<(ostream& os, pair<T, U>& p) { n   os << p.first <<     << p.second; n   return os; n } n template <typename T, typename U> n pair<T, U> operator-(pair<T, U> a, pair<T, U> b) { n   return make_pair(a.first - b.first, a.second - b.second); n } n template <typename T, typename U> n pair<T, U> operator+(pair<T, U> a, pair<T, U> b) { n   return make_pair(a.first + b.first, a.second + b.second); n } n template <typename T, typename U> n void umin(T& a, U b) { n   if (a > b) a = b; n } n template <typename T, typename U> n void umax(T& a, U b) { n   if (a < b) a = b; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   long long n; n   cin >> n; n   int q; n   cin >> q; n   while (q--) { n     long long x; n     cin >> x; n     if (x % 2 == 1) { n       cout << x / 2 + 1 <<   n ; n       continue; n     } n     long long ans = -1; n     for (int k = 0; (1ll << k) < n * 2 + 5; ++k) { n       if ((x - n * 2) & ((1ll << k) - 1)) continue; n       __int128 r = (__int128)n * 2 * ((1ll << k) - 1) + x; n       __int128 l = (1ll << k); n       if (r % l != 0) continue; n       __int128 i = r / l; n       if (i % 2 == 0) continue; n       if (i <= 0 || i >= n * 2) continue; n       ans = i / 2 + 1; n     } n     cout << ans <<   n ; n   } n   return 0; n } n