#include <bits/stdc++.h> n using namespace std; n int a[4]; n int cur[100005]; n void work(int x, int y, int z, int w, int tp) { n   if (x + z < y + w) return; n   if (x + z >= y + w + 2) return; n   int nlen = x + y + z + w; n   for (int pl = 1; pl <= nlen; pl += 2) { n     if (x) n       cur[pl] = 0, x--; n     else n       cur[pl] = 2, z--; n   } n   for (int pl = 2; pl <= nlen; pl += 2) { n     if (y) n       cur[pl] = 1, y--; n     else n       cur[pl] = 3, w--; n   } n   for (int pl = 1; pl < nlen; pl++) n     if (abs(cur[pl] - cur[pl + 1]) != 1) return; n   printf( YES n ); n   for (int i = 1; i <= nlen; i++) { n     if (tp) n       printf( %d  , 3 - cur[i]); n     else n       printf( %d  , cur[i]); n   } n   exit(0); n } n int main() { n   cin >> a[0] >> a[1] >> a[2] >> a[3]; n   work(a[0], a[1], a[2], a[3], 0); n   work(a[3], a[2], a[1], a[0], 1); n   printf( NO n ); n   return 0; n } n


#include <bits/stdc++.h> n namespace ringo { n template <class T> n inline void read(T &x) { n   x = 0; n   char c = getchar(); n   bool f = 0; n   while (!isdigit(c)) f ^= c ==  - , c = getchar(); n   while (isdigit(c)) x = x * 10 + c -  0 , c = getchar(); n   if (f) x = -x; n } n template <class T> n inline void print(T x) { n   if (x < 0) putchar( - ), x = -x; n   if (x > 9) print(x / 10); n   putchar( 0  + x % 10); n } n template <class T> n inline void print(const std::vector<T> &vet) { n   for (auto it : vet) print(it), putchar(   ); n } n template <class T> n inline void print(T x, char c) { n   print(x), putchar(c); n } n template <class T> n inline void print(T a, int l, int r, std::string s =   ) { n   if (s !=   ) std::cout << s <<  :  ; n   for (int i = l; i <= r; i++) print(a[i],    n [i == r]); n } n const int N = 110, M = 1e5 + 10, mod = 1e9 + 7; n bool vis[N]; n std::map<int, int> map; n std::vector<int> node, vet, dp[N][2]; n int n, m, ans, sum, cnt, nod, fa[N], fa_x[N], fac[M], ifac[M], mov[1 << 20]; n struct edge_t { n   int u, v, w; n }; n std::vector<edge_t> e, edge, rest, G[N]; n inline int dec(int a, int b) { n   a -= b; n   return a < 0 ? a + mod : a; n } n inline int inc(int a, int b) { n   a += b; n   return a >= mod ? a - mod : a; n } n inline int mul(int a, int b) { n   return (long long)a * b - (long long)a * b / mod * mod; n } n inline void inc_f(int &a, int b) { n   a += b; n   if (a >= mod) a -= mod; n } n inline int find(int x) { return fa[x] == x ? x : fa[x] = find(fa[x]); } n inline int find_x(int x) { n   return fa_x[x] == x ? x : fa_x[x] = find_x(fa_x[x]); n } n inline int binom(int n, int m) { n   return n < m ? 0 : mul(fac[n], mul(ifac[m], ifac[n - m])); n } n int get_id(int x) { n   auto it = map.find(x); n   return it == map.end() ? map[x] = ++nod : it->second; n } n std::vector<int> inc(const std::vector<int> &a, const std::vector<int> &b) { n   std::vector<int> c(std::max(a.size(), b.size())); n   for (int i = 0; i < a.size(); i++) c[i] = a[i]; n   for (int i = 0; i < b.size(); i++) c[i] = inc(c[i], b[i]); n   return c; n } n std::vector<int> mul(const std::vector<int> &a, const std::vector<int> &b) { n   std::vector<int> c(a.size() + b.size() - 1); n   for (int i = 0; i < a.size(); i++) n     for (int j = 0; j < b.size(); j++) n       c[i + j] = (c[i + j] + (long long)a[i] * b[j]) % mod; n   return c; n } n void init_fac(int n) { n   fac[0] = fac[1] = ifac[0] = ifac[1] = 1; n   for (int i = 2; i <= n; i++) fac[i] = mul(fac[i - 1], i); n   for (int i = 2; i <= n; i++) ifac[i] = mul(mod - mod / i, ifac[mod % i]); n   for (int i = 2; i <= n; i++) ifac[i] = mul(ifac[i - 1], ifac[i]); n } n void dfs(int u, int father) { n   dp[u][0] = std::vector<int>{1}; n   dp[u][1] = std::vector<int>{}; n   for (auto it : G[u]) { n     int v = it.v, w = it.w; n     if (v != father) { n       dfs(v, u); n       if (!vis[u] && !vis[v]) { n         dp[u][1] = inc(mul(dp[u][1], inc(dp[v][0], dp[v][1])), n                        mul(dp[u][0], mul(dp[v][0], std::vector<int>{0, w}))); n       } else { n         dp[u][1] = mul(dp[u][1], inc(dp[v][0], dp[v][1])); n       } n       dp[u][0] = mul(dp[u][0], inc(dp[v][0], dp[v][1])); n     } n   } n } n std::vector<int> solve(int now) { n   node.clear(), edge.clear(); n   for (int i = 1; i <= nod; i++) n     if (find(i) == now) node.push_back(i); n   for (const auto &it : e) n     if (find(it.u) == now) edge.push_back(it); n   if (node.size() == 1) return std::vector<int>{1}; n   std::vector<int> res(edge.size() + 1); n   if (node.size() <= 20) { n     std::map<int, int> map; n     for (int i = 0; i < node.size(); i++) map[node[i]] = i; n     memset(mov, 0, 1 << node.size() << 2); n     mov[0] = 1; n     for (int i = 0, u, v; i < edge.size(); i++) { n       u = map[edge[i].u], v = map[edge[i].v]; n       for (int x = (1 << node.size()) - 1; x >= 0; x--) n         if (mov[x] && (x & (1 << u)) == 0 && (x & (1 << v)) == 0) { n           inc_f(mov[x | (1 << u) | (1 << v)], mul(mov[x], edge[i].w)); n         } n     } n     for (int x = 0; x < (1 << node.size()); x++) n       if (mov[x]) { n         inc_f(res[__builtin_popcount(x) >> 1], mov[x]); n       } n     return res; n   } else { n     rest.clear(); n     for (auto it : node) fa_x[it] = it; n     for (auto it : edge) n       if (find_x(it.u) != find_x(it.v)) { n         G[it.u].push_back({it.u, it.v, it.w}); n         G[it.v].push_back({it.v, it.u, it.w}); n         fa_x[find_x(it.u)] = find_x(it.v); n       } else { n         rest.push_back(it); n       } n     for (int x = 0; x < (1 << rest.size()); x++) { n       sum = 1, cnt = 0; n       memset(vis, 0, nod + 1); n       for (int i = 0; i < rest.size(); i++) n         if ((x >> i) & 1) { n           if (vis[rest[i].u] || vis[rest[i].v]) { n             sum = 0; n             break; n           } n           ++cnt, sum = mul(sum, rest[i].w); n           vis[rest[i].u] = vis[rest[i].v] = 1; n         } n       if (sum) { n         dfs(now, 0); n         std::vector<int> tmp(cnt + 1); n         tmp[cnt] = sum; n         res = inc(res, mul(tmp, inc(dp[now][0], dp[now][1]))); n       } n     } n     return res; n   } n } n void main() { n   read(m), read(n), init_fac(std::max(n, m)); n   for (int x, y, w, i = 1; i <= n; i++) { n     read(x), read(y), read(w); n     if (w != 1) e.push_back({get_id(x), get_id(y + m), w - 1}); n   } n   for (int i = 1; i <= nod; i++) fa[i] = i; n   for (edge_t it : e) fa[find(it.u)] = find(it.v); n   vet.push_back(1); n   for (int i = 1; i <= nod; i++) n     if (find(i) == i) { n       vet = mul(vet, solve(i)); n     } n   for (int i = 0; i < vet.size() && i <= m; i++) { n     ans = inc(ans, mul(vet[i], fac[m - i])); n   } n   print(ans,   n ); n } n }  // namespace ringo n signed main() { return ringo::main(), 0; } n


#include <bits/stdc++.h> n using namespace std; n struct rec { n   int x, p; n } a[101]; n vector<rec> ans; n int n; n bool was[101]; n bool cmp(rec a, rec b) { return a.x > b.x; } n void dfs(int v) { n   was[v] = true; n   for (int i = v + 1; i < n; ++i) { n     if (a[v].x == 0) break; n     if (!was[i]) { n       a[v].x--; n       rec r; n       r.x = a[v].p; n       r.p = a[i].p; n       ans.push_back(r); n       dfs(i); n     } n   } n } n int main() { n   cin >> n; n   for (int i = 0; i < n; ++i) { n     cin >> a[i].x; n     a[i].p = i; n   } n   sort(a + 1, a + n, cmp); n   dfs(0); n   for (int i = 0; i < n; ++i) n     if (!was[i]) { n       cout << -1; n       return 0; n     } n   cout << ans.size() << endl; n   for (int i = 0; i < ans.size(); ++i) { n     cout << ans[i].x + 1 <<     << ans[i].p + 1 << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int a[1000100]; n int main() { n   int n; n   cin >> n; n   for (int i = 0; i < n; i++) { n     int x; n     scanf( %i , &x); n     a[x]++; n   } n   int res = 0; n   for (int i = 0; i < 1000100 - 1; i++) { n     a[i + 1] += a[i] / 2; n     a[i] %= 2; n     res += a[i]; n   } n   printf( %i n , res); n } n


#include <bits/stdc++.h> n using namespace std; n template <class T> n void DBGpair(T a, string s =   ) { n   cerr << s << (s ==    ?    :  :  ) << a.first <<  ,   << a.second <<   n ; n } n template <class T> n void DBGvec(vector<T> a) { n   for (T i : a) cerr << i <<    ; n   cerr <<   n ; n } n int dx[] = {0, 0, 1, -1}; n int dy[] = {1, -1, 0, 0}; n char dir[] = { R ,  L ,  D ,  U }; n const int MOD = 1e9 + 7; n const long long INF = 1e9; n void setIO(string filename) { n   if (fopen((filename +  .in ).c_str(),  r )) { n     freopen((filename +  .in ).c_str(),  r , stdin); n     freopen((filename +  .out ).c_str(),  w , stdout); n   } n } n struct bit { n   vector<long long> tree; n   bit(int n) { tree = vector<long long>(n + 5, 0); } n   long long rsq(int idx) { n     long long sum = 0; n     while (idx > 0) { n       sum += tree[idx]; n       idx -= idx & -idx; n     } n     return sum; n   } n   long long rsq(int a, int b) { n     a++; n     b++; n     return rsq(b) - (a == 1 ? 0 : rsq(a - 1)); n   } n   void upd(int idx, int val) { n     idx++; n     while (idx < tree.size() - 1) { n       tree[idx] += val; n       idx += idx & -idx; n     } n   } n }; n void solve() { n   int n; n   cin >> n; n   vector<long long> a(n); n   for (auto &i : a) cin >> i; n   vector<long long> t(n); n   for (auto &i : t) cin >> i; n   vector<long long> psums(n + 1); n   for (int i = 1; i <= n; i++) psums[i] = psums[i - 1] + t[i - 1]; n   bit tree(n); n   vector<long long> pos(n + 1); n   for (int i = 0; i < n; i++) tree.upd(i, 1); n   for (int i = 0; i < n; i++) { n     int idx = upper_bound((psums).begin(), (psums).end(), a[i] + psums[i]) - n               psums.begin(); n     if (idx == psums.size()) continue; n     idx--; n     pos[idx] += a[i] + psums[i] - psums[idx]; n     tree.upd(idx, -1); n   } n   for (int i = 0; i < n; i++) { n     long long ans = tree.rsq(0, i) * t[i] + pos[i]; n     cout << ans <<    ; n   } n   cout <<   n ; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   int t = 1; n   for (int i = 1; i <= t; i++) solve(); n } n


#include <bits/stdc++.h> n const int MAXN = 50 + 10; n bool row[MAXN]; n bool column[MAXN]; n int main() { n   int n, x, y; n   std::cin >> n; n   for (int i = 0; i <= n * n; i++) { n     std::cin >> x >> y; n     if (row[x - 1] == false && column[y - 1] == false) { n       row[x - 1] = true; n       column[y - 1] = true; n       std::cout << i + 1 <<    ; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <class T> n istream& operator>>(istream& is, vector<T>& v) { n   for (T& x : v) is >> x; n   return is; n } n ostream& operator<<(ostream& os, const pair<int, int>& unit) { n   return os <<  y =   << unit.first <<  x +   << unit.second; n } n template <class T> n ostream& operator<<(ostream& os, const vector<T>& v) { n   if (!v.empty()) { n     os << v.front(); n     for (int i = 1; i < v.size(); ++i) os <<     << v[i]; n   } n   return os; n } n const int P = 1000000007; n int norm(int x) { return x >= P ? x - P : x; } n void add(int& x, int y) { n   if ((x += y) >= P) x -= P; n } n pair<long long, long long> operator+(const pair<long long, long long>& lhs, n                                      const pair<long long, long long>& rhs) { n   return pair<long long, long long>(lhs.first + rhs.first, n                                     lhs.second + rhs.second); n } n pair<long long, long long> operator-(const pair<long long, long long>& lhs) { n   return pair<long long, long long>(-lhs.first, -lhs.second); n } n pair<long long, long long> operator-(const pair<long long, long long>& lhs, n                                      const pair<long long, long long>& rhs) { n   return lhs + (-rhs); n } n long long operator^(const pair<long long, long long>& lhs, n                     const pair<long long, long long>& rhs) { n   return lhs.first * rhs.first + lhs.second * rhs.second; n } n long long operator*(const pair<long long, long long>& lhs, n                     const pair<long long, long long>& rhs) { n   return lhs.first * rhs.second - lhs.second * rhs.first; n } n long long dfrac(long long x, long long y) { n   if (x < 0) return (x - y + 1) / y; n   return x / y; n } n int main() { n   ios::sync_with_stdio(false); n   cin.tie(nullptr); n   int n, m, q; n   cin >> n >> m >> q; n   vector<int> u(m), v(m), w(m); n   for (int i = 0; i < m; ++i) { n     cin >> u[i] >> v[i] >> w[i]; n     --u[i]; n     --v[i]; n   } n   vector<int> dp(n, numeric_limits<int>::min()); n   dp[0] = 0; n   int ans = 0; n   for (int i = 0; i < n; ++i) { n     add(ans, *max_element(dp.begin(), dp.end()) % P); n     vector<int> ndp(n, numeric_limits<int>::min()); n     for (int j = 0; j < m; ++j) { n       ndp[u[j]] = max(ndp[u[j]], dp[v[j]] + w[j]); n       ndp[v[j]] = max(ndp[v[j]], dp[u[j]] + w[j]); n     } n     swap(dp, ndp); n   } n   vector<pair<long long, long long> > pts; n   for (int i = 0; i < m; ++i) { n     int val = max(dp[u[i]], dp[v[i]]); n     pts.emplace_back(w[i], val - w[i] * n); n   } n   sort(pts.begin(), pts.end()); n   vector<pair<long long, long long> > stk; n   for (const auto& pt : pts) { n     if (!stk.empty() && stk.back().first == pt.first) stk.pop_back(); n     while (stk.size() > 1 && ((stk[stk.size() - 1] - stk[stk.size() - 2]) * n                               (pt - stk[stk.size() - 1])) >= 0) { n       stk.pop_back(); n     } n     stk.push_back(pt); n   } n   auto contrib = [&](long long l, long long r, n                      const pair<long long, long long>& lne) { n     l = max(l, (long long)n); n     r = min(r, (long long)q); n     if (l <= r) n       ans = (ans + (r - l + 1) * lne.second % P + n              (l + r) * (r - l + 1LL) / 2 % P * lne.first) % n             P; n     if (ans < 0) ans += P; n   }; n   long long cur = numeric_limits<long long>::min(); n   for (int i = 0; i + 1 < stk.size(); ++i) { n     long long nxt = dfrac(stk[i].second - stk[i + 1].second, n                           stk[i + 1].first - stk[i].first); n     contrib(cur + 1, nxt, stk[i]); n     cur = nxt; n   } n   contrib(cur + 1, numeric_limits<long long>::max(), stk.back()); n   cout << ans <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<int> vec[200005]; n queue<int> Q; n int n, m; n int cvp[200005]; n int top[200005], in[200005]; n int a[200005], b[200005], c[200005]; n void oku() { n   int f, s, cost, i; n   scanf( %d %d , &n, &m); n   for (i = 1; i <= m; i++) { n     scanf( %d %d %d , &f, &s, &cost); n     vec[f].push_back(i); n     vec[s].push_back(i); n     a[i] = f; n     b[i] = s; n     c[i] = cost; n     top[f] += cost; n     top[s] += cost; n   } n   return; n } n void coz() { n   int nod, s; n   vector<int>::iterator it; n   memset(cvp, -1, sizeof cvp); n   Q.push(1); n   while (!Q.empty()) { n     nod = Q.front(); n     Q.pop(); n     for (it = vec[nod].begin(); it != vec[nod].end(); it++) n       if (cvp[*it] == -1) { n         if (nod == b[*it]) n           s = a[*it]; n         else n           s = b[*it]; n         cvp[*it] = (nod == b[*it]); n         in[s] += c[*it]; n         if (s != n && top[s] == 2 * in[s]) Q.push(s); n       } n   } n   for (int i = 1; i <= m; i++) printf( %d n , cvp[i]); n   return; n } n int main() { n   oku(); n   coz(); n   return 0; n } n


#include <bits/stdc++.h> n namespace IO { n char buf[1000000], *p1, *p2; n inline char getc() { n   if (p1 == p2) p2 = (p1 = buf) + fread(buf, 1, 1000000, stdin); n   return p1 == p2 ? EOF : *(p1++); n } n template <typename tp> n inline void r(tp &n) { n   n = 0; n   char c = getc(); n   while (!isdigit(c)) c = getc(); n   while (isdigit(c)) n = n * 10 + c - 48, c = getc(); n } n template <typename tp, typename... Args> n inline void r(tp &n, Args &...args) { n   r(n); n   r(args...); n } n template <typename tp> n inline void write(tp n) { n   if (n / 10) write(n / 10); n   putchar(n % 10 + 48); n } n template <typename tp> n inline void w(tp n, char c =   n ) { n   write(n); n   putchar(c); n } n };  // namespace IO n using namespace IO; n const int N = 1e6 + 2333; n using namespace std; n int m; n int dep[N], f[N][25]; n int LCA(int x, int y) { n   if (dep[x] < dep[y]) x ^= y ^= x ^= y; n   for (int i = 20; i >= 0; --i) n     if (dep[f[x][i]] >= dep[y]) x = f[x][i]; n   if (x == y) return x; n   for (int i = 20; i >= 0; --i) n     if (f[x][i] != f[y][i]) x = f[x][i], y = f[y][i]; n   return f[x][0]; n } n int dis(int a, int b) { return dep[a] + dep[b] - dep[LCA(a, b)] * 2; } n int main() { n   r(m); n   int n = 4, u = 2, v = 3, nowdis = 2; n   for (int i = 2; i <= 4; ++i) f[i][0] = 1, dep[i] = 1; n   while (m--) { n     int x; n     r(x); n     dep[n + 1] = dep[x] + 1, f[n + 1][0] = x; n     dep[n + 2] = dep[x] + 1, f[n + 2][0] = x; n     for (int i = 0; f[n + 1][i]; ++i) f[n + 1][i + 1] = f[f[n + 1][i]][i]; n     for (int i = 0; f[n + 2][i]; ++i) f[n + 2][i + 1] = f[f[n + 2][i]][i]; n     n += 2; n     int a = dis(u, n), b = dis(v, n), c = nowdis; n     nowdis = max(max(a, b), c); n     if (nowdis == a) n       v = n; n     else if (nowdis == b) n       u = n; n     w(nowdis); n   } n   return 0; n } n


#include <bits/stdc++.h> n #pragma GCC optimize( O3 ,  unroll-loops ) n using namespace std; n template <class T> n inline void sort(T &a) { n   sort((a).begin(), (a).end()); n } n template <class T> n inline void rsort(T &a) { n   sort((a).rbegin(), (a).rend()); n } n template <class T> n inline void reverse(T &a) { n   reverse((a).begin(), (a).end()); n } n template <class T, class U> n inline void checkmin(T &x, U y) { n   if (x > y) x = y; n } n template <class T, class U> n inline void checkmax(T &x, U y) { n   if (x < y) x = y; n } n vector<vector<int>> rev, G; n void dfs(int v, int p = -1) { n   for (auto &i : G[v]) n     if (i != p) { n       rev[i].push_back(v); n       dfs(i, v); n     } n } n void solve() { n   int n; n   cin >> n; n   G = vector<vector<int>>(n); n   for (int i = 1; i < n; ++i) { n     int u, v; n     cin >> u >> v; n     --u, --v; n     G[u].push_back(v); n     G[v].push_back(u); n   } n   int k1, k2; n   cin >> k1; n   vector<int> arr(k1); n   for (auto &i : arr) cin >> i, --i; n   cin >> k2; n   vector<int> brr(k2); n   for (auto &i : brr) cin >> i, --i; n   vector<bool> used(n); n   for (auto &i : arr) used[i] = true; n   cout <<  B   << brr[0] + 1 << endl; n   int y; n   cin >> y; n   --y; n   rev = vector<vector<int>>(n); n   dfs(y); n   y = arr[0]; n   while (int(rev[y].size())) { n     if (!used[rev[y][0]]) break; n     y = rev[y][0]; n   } n   int start = y; n   cout <<  A   << y + 1 << endl; n   cin >> y; n   --y; n   for (auto i : brr) n     if (i == y) { n       cout <<  C   << start + 1 << endl; n       return; n     } n   cout <<  C   << -1 << endl; n } n signed main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   cout << fixed << setprecision(20); n   srand(time(nullptr)); n   int q; n   cin >> q; n   while (q--) solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   size_t n; n   string s, t; n   cin >> n >> s >> t; n   int base = ( z  -  a ) + 1; n   vector<int> l(n + 1), r(n + 1); n   for (int i = 0; i < int(n); ++i) { n     l[i + 1] = s[i] -  a ; n   } n   for (int i = 0; i < int(n); ++i) { n     r[i + 1] = t[i] -  a ; n   } n   for (int i = n; i >= 0; --i) { n     l[i] += r[i]; n     if (i > 0) { n       l[i - 1] += l[i] / 26; n       l[i] %= 26; n     } n   } n   vector<int> res(n + 1); n   for (int i = 0; i <= n; ++i) { n     int rem = l[i] % 2; n     l[i] /= 2; n     if (i < n) { n       l[i + 1] += rem * 26; n     } n   } n   for (int i = 1; i <= n; ++i) { n     cout << char(l[i] +  a ); n   } n   cout << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long n, m, a, b, c; n vector<pair<long, long> > g[220000]; n long cap[220000]; n map<pair<long, long>, long> mapp; n set<pair<long, long> > done; n long ans[220000]; n void solve(long v) { n   for (int i = 0; i < g[v].size(); i++) { n     long q = g[v][i].first; n     long val = g[v][i].second; n     if (done.find(make_pair(v, q)) != done.end()) continue; n     done.insert(make_pair(v, q)); n     done.insert(make_pair(q, v)); n     ans[mapp[make_pair(v, q)]] = 1; n     cap[q] -= val; n     if (cap[q] == 0 && q < n) solve(q); n   } n } n int main() { n   ios_base::sync_with_stdio(0); n   cin >> n >> m; n   for (int i = 1; i <= m; i++) { n     cin >> a >> b >> c; n     mapp[make_pair(b, a)] = i; n     g[a].push_back(make_pair(b, c)); n     g[b].push_back(make_pair(a, c)); n     cap[a] += c; n     cap[b] += c; n   } n   for (int i = 1; i <= n; i++) cap[i] /= 2; n   cap[n] = 0; n   cap[1] = 0; n   solve(1); n   for (int i = 1; i <= m; i++) cout << ans[i] << endl; n   cin.get(); n   cin.get(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int inf = 0x3f3f3f3f, mod = 998244353; n const long long INF = 0x3f3f3f3f3f3f3f3fLL; n const double pi = acosl(-1.), eps = 1e-9; n inline void sum(int &a, int b) { n   a += b; n   if (a >= mod) a -= mod; n } n inline int power(int a, int b, int m = mod, int ans = 1) { n   for (; b; b >>= 1, a = 1LL * a * a % m) n     if (b & 1) ans = 1LL * ans * a % m; n   return ans; n } n const int NN = 10010; n int ch[NN], rch[NN]; n int c(int n, int m) { n   if (n < m) return 0; n   if (!m) return 1; n   return (long long)ch[n] * rch[n - m] % mod * rch[m] % mod; n } n int dp[111][5555]; n int s[111][5555]; n int main() { n   ch[0] = 1; n   for (int i = 1; i < NN; i++) ch[i] = (long long)ch[i - 1] * i % mod; n   rch[NN - 1] = power(ch[NN - 1], mod - 2); n   for (int i = NN - 1; i; i--) { n     rch[i - 1] = (long long)rch[i] * i % mod; n   } n   int N, S, R; n   cin >> N >> S >> R; n   if (N == 1) { n     puts( 1 ); n     return 0; n   } n   int tmp; n   if (R == 0) { n     tmp = c(N + S - 1, N - 1); n   } else { n     dp[0][0] = 1; n     for (int i = 0; i <= S; i++) s[0][i] = 1; n     for (int i = 1; i <= N; i++) { n       for (int j = 0; j <= S; j++) { n         dp[i][j] = s[i - 1][j]; n         if (j >= R) dp[i][j] -= s[i - 1][j - R]; n         if (dp[i][j] < 0) dp[i][j] += mod; n       } n       s[i][0] = 1; n       for (int j = 1; j <= S; j++) { n         s[i][j] = s[i][j - 1] + dp[i][j]; n         if (s[i][j] >= mod) s[i][j] -= mod; n       } n     } n     tmp = c(S + N - 1, N - 1) - dp[N][S]; n     if (tmp < 0) tmp += mod; n   } n   tmp = (long long)tmp * power(N, mod - 2) % mod; n   int tot = 0; n   for (int i = R; i <= S; i++) { n     sum(tot, c(S - i + N - 1 - 1, N - 2)); n   } n   tot = power(tot, mod - 2); n   cout << (long long)tmp * tot % mod << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n pair<pair<int, int>, int> e[100010]; n int parent[100010]; n int Rank[100010]; n int sz[100010]; n vector<int> v[100010]; n bool mark[100010]; n bool mark1[100010]; n int color[100010]; n vector<int> out[2]; n bool success[2]; n int cnt[2]; n int n, m; n int find(int cur) { n   if (parent[cur] == cur) return cur; n   return parent[cur] = find(parent[cur]); n } n void Union(int x, int y) { n   int xroot = find(x); n   int yroot = find(y); n   if (xroot == yroot) return; n   if (Rank[xroot] < Rank[yroot]) n     parent[xroot] = yroot; n   else if (Rank[xroot] > Rank[yroot]) n     parent[yroot] = xroot; n   else { n     Rank[xroot]++; n     parent[yroot] = xroot; n   } n } n void init(int n) { n   int i; n   for (i = 0; i <= n; ++i) { n     Rank[i] = 0; n     parent[i] = i; n     sz[i] = 0; n   } n } n bool dfs(int cur, int c) { n   mark[cur] = 1; n   color[cur] = c; n   cnt[c] += sz[cur]; n   for (int i = 0; i < v[cur].size(); ++i) { n     if (mark[v[cur][i]]) { n       if (color[v[cur][i]] == c) return 0; n       continue; n     } n     if (!dfs(v[cur][i], 1 - c)) return 0; n   } n   return 1; n } n void dfs1(int cur) { n   mark1[cur] = 1; n   color[cur] = 1 - color[cur]; n   for (int i = 0; i < v[cur].size(); ++i) n     if (!mark1[v[cur][i]]) dfs1(v[cur][i]); n } n void solve(int f) { n   int i; n   init(n); n   success[f] = 0; n   for (i = 1; i <= n; ++i) v[i].clear(); n   for (i = 0; i < m; ++i) n     if (e[i].second == f) Union(e[i].first.first, e[i].first.second); n   for (i = 0; i < m; ++i) n     if (e[i].second != f) { n       int x = find(e[i].first.first), y = find(e[i].first.second); n       if (x == y) return; n       v[x].push_back(y); n       v[y].push_back(x); n     } n   for (i = 1; i <= n; ++i) sz[find(i)]++; n   memset(mark, 0, sizeof(mark)); n   memset(mark1, 0, sizeof(mark1)); n   for (i = 1; i <= n; ++i) { n     cnt[0] = cnt[1] = 0; n     if (mark[find(i)]) continue; n     if (!dfs(find(i), 0)) return; n     if (cnt[0] > cnt[1]) dfs1(find(i)); n   } n   success[f] = 1; n   for (i = 1; i <= n; ++i) n     if (color[find(i)] == 0) out[f].push_back(i); n } n int main() { n   int i, x, y, c; n   string s; n   scanf( %d , &n); n   scanf( %d , &m); n   for (i = 0; i < m; ++i) { n     scanf( %d , &x); n     scanf( %d , &y); n     cin >> s; n     if (s[0] ==  R ) n       c = 0; n     else n       c = 1; n     e[i] = make_pair(make_pair(x, y), c); n   } n   solve(0); n   solve(1); n   int minn = 1000000, minpos; n   for (i = 0; i < 2; ++i) n     if (success[i]) n       if ((int)out[i].size() < minn) { n         minn = (int)out[i].size(); n         minpos = i; n       } n   if (minn == 1000000) n     printf( %d n , -1); n   else { n     printf( %d n , minn); n     for (auto it : out[minpos]) printf( %d  , it); n     printf(  n ); n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int INF = 1 << 30; n const long long int MAX = 1e9 + 7; n void array_show(int *array, int array_n, char middle =    ) { n   for (int i = 0; i < array_n; i++) n     printf( %d%c , array[i], (i != array_n - 1 ? middle :   n )); n } n void array_show(long long int *array, int array_n, char middle =    ) { n   for (int i = 0; i < array_n; i++) n     printf( %lld%c , array[i], (i != array_n - 1 ? middle :   n )); n } n void array_show(vector<int> &vec_s, int vec_n = -1, char middle =    ) { n   if (vec_n == -1) vec_n = vec_s.size(); n   for (int i = 0; i < vec_n; i++) n     printf( %d%c , vec_s[i], (i != vec_n - 1 ? middle :   n )); n } n void array_show(vector<long long int> &vec_s, int vec_n = -1, n                 char middle =    ) { n   if (vec_n == -1) vec_n = vec_s.size(); n   for (int i = 0; i < vec_n; i++) n     printf( %lld%c , vec_s[i], (i != vec_n - 1 ? middle :   n )); n } n string st[110000]; n int t[110000]; n int string_to_int(string sa) { n   int s = 0; n   int a; n   for (auto ca : sa) { n     s *= 10; n     a = (int)ca - (int) 0 ; n     if (a < 0 || a >= 10) return -1; n     if (s == 0 && a == 0) return -1; n     s += a; n   } n   return s; n } n bool used[110000]; n vector<string> va[2], vb[2]; n vector<pair<string, string>> vs; n int main() { n   int n, m; n   int i, j, k; n   int a, b, c; n   string sa; n   cin >> n; n   m = 0; n   for (i = 0; i < n; i++) { n     cin >> st[i] >> t[i]; n     if (t[i] == 1) m++; n   } n   b = -1; n   for (i = 0; i < n; i++) { n     a = string_to_int(st[i]); n     if (a <= 0 || a > n) n       vb[t[i]].push_back(st[i]); n     else { n       used[a] = true; n       if (t[i] == 0 && a <= m) va[0].push_back(st[i]); n       if (t[i] == 1 && a > m) va[1].push_back(st[i]); n     } n   } n   for (i = 1; i <= n; i++) { n     if (!used[i]) { n       b = i; n       break; n     } n   } n   if (b == -1) { n     if (va[0].empty()) { n       cout << 0 << endl; n       return 0; n     } n     vs.push_back(make_pair(va[0].back(), to_string(n + 1))); n     a = string_to_int(va[0].back()); n     used[a] = false, b = a; n     va[0].pop_back(); n     vb[0].push_back(to_string(n + 1)); n   } n   while (1) { n     if (b <= m) n       a = 1; n     else n       a = 0; n     if (va[a].empty()) break; n     vs.push_back(make_pair(va[a].back(), to_string(b))); n     c = string_to_int(va[a].back()); n     used[c] = false, used[b] = true; n     va[a].pop_back(), b = c; n   } n   for (i = 1; i <= n; i++) { n     if (used[i]) continue; n     if (i <= m) n       a = 1; n     else n       a = 0; n     if (va[a].empty()) continue; n     vs.push_back(make_pair(va[a].back(), to_string(i))); n     c = string_to_int(va[a].back()); n     used[c] = false, used[i] = true; n     va[a].pop_back(); n   } n   for (i = 1; i <= n; i++) { n     if (used[i]) continue; n     if (i <= m) n       a = 1; n     else n       a = 0; n     if (vb[a].empty()) continue; n     vs.push_back(make_pair(vb[a].back(), to_string(i))); n     vb[a].pop_back(); n   } n   cout << vs.size() << endl; n   for (auto node : vs) { n     cout <<  move   << node.first <<     << node.second << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n long long int dp[2002][2002]; n vector<long long int> col, row; n long long int sol(long int p, long int q, int n, int m, int jk) { n   if (p == 0 || q == 0) { n     return 0; n   } n   vector<long long int>::iterator it; n   if (p <= 2 * n && q <= 2 * m) { n     if (jk == 0) { n       return (p * q - dp[p][q]); n     } n     return dp[p][q]; n   } else if (p <= 2 * n) { n     it = lower_bound(col.begin(), col.end(), q); n     if (*it != q) { n       it--; n     } n     long long int ab = *it; n     q = q % ab; n     ab /= 2; n     ab = ab * p; n     return ab + sol(p, q, n, m, 1 - jk); n   } else { n     it = lower_bound(row.begin(), row.end(), p); n     if (*it != p) { n       it--; n     } n     long long int ab = *it; n     p = p % ab; n     ab /= 2; n     ab = ab * q; n     return ab + sol(p, q, n, m, 1 - jk); n   } n   return 0; n } n int main() { n   int n, m, q; n   cin >> n >> m >> q; n   int arr[2 * n + 2][2 * m + 2]; n   for (int x = 1; x <= n; x++) { n     string s; n     cin >> s; n     for (int y = 1; y <= m; y++) { n       arr[x][y] = s[y - 1] -  0 ; n     } n   } n   for (int x = 1; x <= 2 * n; x++) { n     for (int y = 1; y <= 2 * m; y++) { n       if (x <= n && y <= m) { n       } else if (x > n && y > m) { n         arr[x][y] = arr[x - n][y - m]; n       } else if (x > n) { n         arr[x][y] = 1 - arr[x - n][y]; n       } else { n         arr[x][y] = 1 - arr[x][y - m]; n       } n       dp[x][y] = dp[x][y - 1] + dp[x - 1][y] - dp[x - 1][y - 1] + arr[x][y]; n     } n   } n   int sz = 0; n   col.push_back(m); n   while (col[sz] < INT_MAX) { n     col.push_back(col[sz++] * 2); n   } n   sz = 0; n   row.push_back(n); n   while (row[sz] < INT_MAX) { n     row.push_back(row[sz++] * 2); n   } n   while (q--) { n     int x1, x2, y1, y2; n     cin >> x1 >> y1 >> x2 >> y2; n     long long int a = sol(x2, y2, n, m, 1); n     long long int b = sol(x1 - 1, y2, n, m, 1); n     long long int c = sol(x2, y1 - 1, n, m, 1); n     long long int d = sol(x1 - 1, y1 - 1, n, m, 1); n     cout << a + d - b - c << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n string s; n int n; n int a[26]; n int b[26]; n bool flag[26]; n bool cmp(int x, int y) { return a[x] < a[y]; } n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   cin >> s >> n; n   memset(a, 0, sizeof(a)); n   memset(flag, 1, sizeof(flag)); n   for (int i = 0; i < s.size(); ++i) { n     a[s[i] -  a ]++; n   } n   for (int i = 0; i < 26; ++i) { n     b[i] = i; n   } n   int ans = 26; n   sort(b, b + 26, cmp); n   for (int i = 0; i < 26; ++i) { n     if (a[b[i]] <= n) { n       flag[b[i]] = 0; n       n -= a[b[i]]; n       ans--; n     } n   } n   cout << ans <<   n ; n   for (int i = 0; i < s.size(); ++i) { n     if (flag[s[i] -  a ]) cout << s[i]; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long power(long long a, long long n) { n   if (a == 0) return 0; n   if (a == 1 || n == 0) return 1; n   if (n == 1) return a % 1000000007; n   long long t = power(a, n / 2); n   t = t * t % 1000000007; n   if (n & 1) return t * a % 1000000007; n   return t; n } n int popcount(long long a) { n   int c = 0; n   while (a) { n     c++; n     a -= a & -a; n   } n   return c; n } n void factorize(int a) {} n void update(int tree[], int idx, int val, int maxval) { n   for (; idx <= maxval; idx += idx & -idx) { n     tree[idx] += val; n   } n } n int read(int tree[], int idx) { n   long long sum = 0; n   for (; idx > 0; idx -= idx & -idx) { n     sum += tree[idx]; n   } n   return sum; n } n struct node2 { n   int id, val; n   node2() { n     static int ctx = 1; n     id = ctx++; n   }; n   node2(int a, int b = 0, int c = 0, int d = 0, int e = 0, int f = 0) { n     val = a; n   } n }; n struct comp2 { n   bool operator()(int a, int b) { return b < a; } n }; n bool cmp2(int a, int b) { return b < a; } n struct node { n   int id, val; n   node() { n     static int ctx = 1; n     id = ctx++; n   }; n   node(int a, int b = 0, int c = 0, int d = 0, int e = 0, int f = 0) { n     val = a; n   } n }; n struct comp { n   bool operator()(int a, int b) { return b < a; } n }; n bool cmp(int a, int b) { return b < a; } n int n, m, a[100009], b, c, d, k, h, w, x, y, p, q, t, res, ma, mi, T, n     act = 0, pas = 1, cur, flag, now; n int bit[33][100009]; n long long ans, dp[100009][2]; n char s[1]; n double e, f, z; n vector<int> v[100009], vec; n set<int> sett; n map<int, int> mapp; n void print() {} n void print2() {} n void input() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cin >> n; n   for (int i = 1; i < n + 1; i++) cin >> a[i]; n   for (int i = 1; i < n; i++) { n     cin >> x >> y; n     v[x].push_back(y); n     v[y].push_back(x); n   } n } n void init() { n   for (int pos = 0; pos < 30; pos++) { n     for (int i = 1; i < n + 1; i++) { n       bit[pos][i] = (((1 << pos) & a[i]) != 0); n     } n   } n } n void calc(int id, int par, int a[]) { n   dp[id][a[id]] = 1; n   for (auto u : v[id]) { n     if (u == par) continue; n     calc(u, id, a); n     if (a[id]) { n       dp[id][1] += dp[u][0]; n       dp[id][0] += dp[u][1]; n       ans += T * (dp[id][1] - dp[u][0]) * dp[u][0]; n       ans += T * (dp[id][0] - dp[u][1]) * dp[u][1]; n     } else { n       dp[id][1] += dp[u][1]; n       dp[id][0] += dp[u][0]; n       ans += T * (dp[id][1] - dp[u][1]) * dp[u][0]; n       ans += T * (dp[id][0] - dp[u][0]) * dp[u][1]; n     } n   } n   ans += T * a[id]; n } n void solve() { n   init(); n   int l = 0; n   int r = 29; n   for (int pos = l; pos < r + 1; pos++) { n     T = 1 << pos; n     memset(dp, 0, sizeof(dp)); n     calc(1, 0, bit[pos]); n   } n } n void output() { cout << ans; } n int main() { n   input(); n   solve(); n   output(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long double pi = 3.14159265358979323846; n long long MOD = 1e9 + 7; n const char nl =   n ; n const long long inf = 1e15; n long long nxt() { n   long long x; n   cin >> x; n   return x; n } n long long mul(long long x, long long y) { n   return (1ll * (x % MOD) * (y % MOD)); n } n long long modpow(long long x, long long y) { n   long long z = 1; n   while (y > 0) { n     if (y % 2) z = mul(z, x); n     x = mul(x, x); n     y /= 2; n   } n   return z; n } n long long power(long long x, long long y) { n   long long z = 1; n   while (y > 0) { n     if (y % 2) z = z * x; n     x = x * x; n     y /= 2; n   } n   return z; n } n long long gcd(long long a, long long b) { n   if (a < b) return gcd(b, a); n   if (b == 0) return a; n   return gcd(b, a % b); n } n long long min(long long a, long long b) { n   if (a < b) return a; n   return b; n } n long long max(long long a, long long b) { n   if (a > b) return a; n   return b; n } n long long sq(long long a) { n   long long ans = (1ll * a * a); n   return ans; n } n void solve() { n   long long n, k; n   cin >> n >> k; n   long long t[n]; n   vector<long long> neg; n   for (long long i = 0; i < n; i++) { n     cin >> t[i]; n     if (t[i] < 0) neg.push_back(i); n   } n   if ((long long)(neg.size()) > k) { n     cout << -1 << nl; n     return; n   } n   if ((long long)(neg.size()) == 0) { n     cout << 0 << nl; n     return; n   } n   if (n == 1) { n     if (t[0] < 0) { n       cout << 1 << nl; n     } else n       cout << 0 << nl; n     return; n   } n   vector<long long> dis; n   for (long long i = 0; i < (long long)(neg.size()) - 1; i++) { n     if (neg[i + 1] - neg[i] > 1) { n       dis.push_back(neg[i + 1] - neg[i] - 1); n     } n   } n   sort(((dis).begin()), ((dis).end())); n   long long ans = (long long)(dis.size()) * 2; n   ans++; n   if (t[n - 1] >= 0) ans++; n   long long ewd = k - (long long)(neg.size()); n   for (long long x : dis) { n     if (ewd >= x) ewd -= x, ans -= 2; n   } n   long long ex = n - 1 - neg.back(); n   if (ewd >= ex && ex) ans--; n   cout << ans <<   n ; n } n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0), cout.tie(0); n   long long TC = 1; n   while (TC--) { n     solve(); n   } n   cerr <<  Time :   << 1000 * ((double)clock()) / (double)CLOCKS_PER_SEC n        <<  ms n ; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T> n T In() { n   T x; n   cin >> x; n   return x; n } n int main() { n   ios::sync_with_stdio(false), cin.tie(0); n   cout << ((In<int>() & 1) ?  Ehab  :  Mahmoud ); n   return cout << endl, 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long int mod = 1e9 + 7; n bool isprime(int a) { n   for (int i = 2; i <= sqrt((double)a); i++) { n     if (a % i == 0) return false; n   } n   return true; n } n int main() { n   int n; n   cin >> n; n   if (n <= 7) n     cout << 1 << endl << n; n   else { n     cout << 3 << endl << 3 <<    ; n     n -= 3; n     for (int i = 2; i <= n; i++) { n       if (isprime(i) && isprime(n - i)) { n         cout << i <<     << n - i; n         return 0; n       } n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, m; n   cin >> n >> m; n   string n1 = to_string(m); n   for (int i = 0; i < n1.length() / 2; i++) { n     char s = n1[i]; n     n1[i] = n1[n1.length() - 1 - i]; n     n1[n1.length() - 1 - i] = s; n   } n   cout << n + stoi(n1); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int32_t main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   ; n   long long l, r; n   cin >> l >> r; n   long long ans = 0; n   for (long long i = 1; i <= r; i *= 2) { n     long long cur = i; n     while (cur <= r) { n       if (cur >= l) ans++; n       cur *= 3; n     } n   } n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n string s, h1, m1; n int h[10000], m[10000]; n bool a[10000]; n int main() { n   int n; n   cin >> n; n   cin.ignore(); n   for (__typeof(n) i = (1); i <= (n); i++) { n     getline(cin, s); n     h1 = s.substr(1, 2); n     m1 = s.substr(4, 2); n     if (s[7] ==  a ) n       a[i] = true; n     else n       a[i] = false; n     stringstream sh, sm; n     sh.flush(); n     sm.flush(); n     sh.clear(); n     sm.clear(); n     sh << h1; n     sm << m1; n     sh >> h[i]; n     sm >> m[i]; n     h[i] %= 12; n     if (!a[i]) h[i] += 12; n   } n   int ans = 1; n   int k = 0; n   for (__typeof(n - 1) i = (1); i <= (n - 1); i++) { n     int t1 = h[i] * 10000 + m[i]; n     int t2 = h[i + 1] * 10000 + m[i + 1]; n     if (t1 == t2) n       k++; n     else n       k = 0; n     if (k == 10) { n       k = 0; n       ans++; n       continue; n     } n     if (!(a[i]) && a[i + 1]) { n       ans++; n       continue; n     } n     if (t1 > t2) { n       ans++; n     } n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n typedef struct { n   char name[20]; n   char phone[200][20]; n   int len[200]; n   int avai[200]; n   int cnt, now; n } friends; n friends frd[20]; n int total; n int main() { n   int n; n   scanf( %d , &n); n   for (int i = 0; i < n; i++) { n     char tmp[20]; n     scanf( %s , tmp); n     int pos = -1; n     for (int j = 0; j < total; j++) { n       if (!strcmp(frd[j].name, tmp)) { n         pos = j; n         break; n       } n     } n     if (pos == -1) pos = total++; n     int num; n     scanf( %d , &num); n     for (int j = 0; j < num; j++) { n       char ph_num[20]; n       scanf( %s , ph_num); n       int l = strlen(ph_num), flag = 0; n       for (int k = 0; k < frd[pos].now; k++) { n         if (frd[pos].avai[k]) { n           if (frd[pos].len[k] >= l) { n             if (!strcmp(frd[pos].phone[k] + frd[pos].len[k] - l, ph_num)) { n               flag = 1; n               break; n             } n           } else { n             if (!strcmp(ph_num + l - frd[pos].len[k], frd[pos].phone[k])) { n               frd[pos].avai[k] = 0; n               frd[pos].cnt--; n               break; n             } n           } n         } n       } n       if (!flag) { n         strcpy(frd[pos].name, tmp); n         strcpy(frd[pos].phone[frd[pos].now], ph_num); n         frd[pos].len[frd[pos].now] = l; n         frd[pos].avai[frd[pos].now] = 1; n         frd[pos].cnt++; n         frd[pos].now++; n       } n     } n   } n   printf( %d n , total); n   for (int i = 0; i < total; i++) { n     printf( %s %d , frd[i].name, frd[i].cnt); n     for (int j = 0; j < frd[i].now; j++) { n       if (frd[i].avai[j]) printf(  %s , frd[i].phone[j]); n     } n     printf(  n ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using std::cin; n using std::cout; n using std::endl; n using std::string; n using std::vector; n int zfn[100010]; n int count = 0, nodes_count; n string P, T; n vector<string> str; n vector<vector<int> > connections; n void kmpPreprocess() { n   int i = 0, j = -1; n   zfn[0] = -1; n   while (i < P.size()) { n     while (j >= 0 && P[i] != P[j]) j = zfn[j]; n     i++; n     j++; n     zfn[i] = j; n   } n } n void kmp_DFS(int current, int j) { n   string T = str[current]; n   int i = 0; n   while (i < T.size()) { n     while (j >= 0 && T[i] != P[j]) j = zfn[j]; n     i++; n     j++; n     if (j == P.size()) { n       count++; n       j = zfn[j]; n     } n   } n   for (vector<int>::iterator it = connections.at(current).begin(); n        it != connections.at(current).end(); ++it) { n     kmp_DFS(*it, j); n   } n } n int main() { n   cin.sync_with_stdio(false); n   cin >> nodes_count; n   str.resize(nodes_count + 1); n   for (int i = 0; i < nodes_count; i++) { n     vector<int> v; n     connections.push_back(v); n   } n   int current_parent; n   for (int current_node = 0; current_node < nodes_count - 1; current_node++) { n     T =   ; n     cin >> current_parent >> T; n     connections[current_parent - 1].push_back(current_node + 1); n     str.at(current_node + 1) = T; n   } n   cin >> P; n   kmpPreprocess(); n   T =   ; n   kmp_DFS(0, 0); n   cout << count << endl; n   return 0; n } n


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


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, ans1 = INT_MAX, ans = -198896785; n   cin >> n; n   int c = 0, d = 0; n   for (int i = 0; i < n; i++) { n     int a; n     cin >> a; n     if (a < 0) { n       c++; n       ans = max(a, ans); n     } n     if (a > 0) { n       d++; n       ans1 = min(a, ans1); n     } n   } n   n = (n + 1) / 2; n   if (d >= n) n     cout << ans1 << endl; n   else if (c >= n) n     cout << ans << endl; n   else n     cout << 0 << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 1010; n const int MOD = 1e9 + 7; n int dp[MAXN][MAXN][2]; n int Steps[MAXN]; n int add(int a, int b) { return ((a + b) >= MOD ? a + b - MOD : a + b); } n void PreproccessSteps() { n   Steps[1] = 1; n   for (int i = (2); i < (1010); i++) n     Steps[i] = Steps[__builtin_popcount(i)] + 1; n } n int Ones(int N, string s) { n   int cnt = 0; n   for (int i = (1); i < (N); i++) n     if (s[i] ==  1 ) cnt++; n   return cnt; n } n void FillDP(int N, string s) { n   for (int j = (0); j < (N + 1); j++) dp[0][j][0] = dp[0][j][1] = 1; n   for (int i = (1); i < (N + 1); i++) n     for (int j = (1); j < (N + 1); j++) { n       if (s[j - 1] ==  1 ) n         dp[i][j][0] = add(dp[i - 1][j - 1][0], dp[i][j - 1][1]); n       if (s[j - 1] ==  0 ) dp[i][j][0] = dp[i][j - 1][0]; n       dp[i][j][1] = add(dp[i - 1][j - 1][1], dp[i][j - 1][1]); n     } n } n int main() { n   int N, k, sum = 0; n   string s; n   cin >> s >> k; n   reverse(s.begin(), s.end()); n   PreproccessSteps(); n   N = (int)s.size(); n   FillDP(N, s); n   for (int i = (1); i < (N + 1); i++) n     if (Steps[i] == k) sum = add(sum, dp[i][N][0]); n   if (k == 1) { n     cout << dp[1][N][0] - 1; n     return 0; n   } n   if (!k) sum++; n   cout << sum; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void solve() { n   int x, y, n; n   cin >> x >> y >> n; n   int max = (n / x) * x; n   if (max + y > n) { n     max = max - x + y; n   } else { n     max += y; n   } n   cout << max <<   n ; n } n int main() { n   int t; n   cin >> t; n   while (t--) solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 1e6 + 10; n const long long mod = 1e9 + 7; n void rd(int &x) { n   int f = 1; n   x = 0; 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 ) x = x * 10 + ch -  0 , ch = getchar(); n   x *= f; n } n void pr(int x) { printf( %d n , x); } n int n, a[55][55]; n int go[4][2] = {-1, -1, 1, -1, -1, 1, 1, 1}; n pair<int, int> g[4]; n int main() { n   int odd = 1, even = 2; n   cin >> n; n   if (n == 1) { n     puts( 1 ); n     return 0; n   } n   if (n == 3) { n     printf( 2 1 4 n3 5 7 n6 9 8 n ); n     return 0; n   } n   int k = (n + 1) / 2; n   int l = k, r = k; n   for (int i = (1); i <= k; i++) { n     for (int j = (l); j <= r; j++) a[i][j] = odd, odd += 2; n     l--, r++; n   } n   l = k, r = k; n   for (int i = n; i >= k + 1; i--) { n     for (int j = (l); j <= r; j++) a[i][j] = odd, odd += 2; n     l--, r++; n   } n   for (int i = (1); i <= n; i++) { n     for (int j = (1); j <= n; j++) { n       if (a[i][j] == 0) a[i][j] = even, even += 2; n     } n   } n   for (int i = (1); i <= n; i++) { n     for (int j = (1); j <= n; j++) printf( %d  , a[i][j]); n     puts(  ); n   } n } n


#include <bits/stdc++.h> n using namespace std; n #pragma comment(linker,  /stack:200000000 ) n #pragma GCC optimize( Ofast ) n #pragma GCC target( sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native ) n int main() { n   ios::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   int n; n   cin >> n; n   int arr[n]; n   for (int i = 0; i < (n); ++i) cin >> arr[i]; n   int ans = 1, idx = 1, cnt = 1; n   while (idx < n) { n     ans = (ans > cnt) ? ans : cnt; n     if (arr[idx] <= 2 * arr[idx - 1]) n       ++cnt; n     else n       cnt = 1; n     ++idx; n   } n   ans = (ans > cnt) ? ans : cnt; n   cout << ans <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n #pragma GCC optimize( Ofast,no-stack-protector ) n using namespace std; n string s; n const int maxn = (int)1e5 + 1; n const int maxlog = 19; n struct vt { n   int w[26]; n   vector<int> lnk[26]; n   vector<int> rlnk[26]; n   string s; n   vt() {} n   vt(string s) : s(s) {} n   int get_cnt(int l, int r) { n     int ans = 0; n     for (int i = 0; i < 26; i++) { n       if (lnk[i][r] >= l || i == s[r] -  a ) { n         ans++; n       } n     } n     return ans - 1; n   } n   vector<long long> sum[1][26]; n   vector<pair<int, int> > tab[1][26]; n   void build() { n     memset(w, -1, sizeof w); n     for (int i = 0; i < 26; i++) { n       lnk[i].resize(s.size()); n       rlnk[i].resize(s.size()); n       for (int j = 0; j < 1; j++) { n         sum[j][i].resize(s.size()); n         tab[j][i].resize(s.size()); n       } n     } n     for (int i = 0; i < (int)s.size(); i++) { n       for (int j = 0; j < 26; j++) { n         lnk[j][i] = w[j]; n       } n       w[s[i] -  a ] = i; n     } n     for (int j = 0; j < 26; j++) { n       w[j] = (int)s.size() - 1; n     } n     for (int i = (int)s.size() - 1; i >= 0; i--) { n       for (int j = 0; j < 26; j++) { n         rlnk[j][i] = w[j]; n       } n       w[s[i] -  a ] = i; n     } n     for (int i = 0; i < (int)s.size(); i++) { n       vector<int> g; n       for (int j = 0; j < 26; j++) { n         if (j != s[i] -  a ) { n           g.push_back(lnk[j][i]); n         } else { n           g.push_back(i); n         } n       } n       sort(g.rbegin(), g.rend()); n       int rm = i; n       for (int j = 0; j < (int)g.size(); j++) { n         if (g[j] == -1) { n           tab[0][j][i] = tab[0][j - 1][i]; n           sum[0][j][i] = sum[0][j - 1][i]; n           continue; n         } n         rm = max(rm, rlnk[s[g[j]] -  a ][g[j]]); n         int lm = g[j]; n         int cnt = get_cnt(lm, rm); n         tab[0][j][i] = make_pair(cnt, rm); n         sum[0][j][i] = rm; n       } n     } n   } n   vector<long long> sums[maxlog]; n   vector<int> tot[maxlog]; n   vector<int> tabs[maxlog]; n   void build(int k) { n     for (int i = 0; i < maxlog; i++) { n       sums[i].assign(s.size(), 0); n       tabs[i].assign(s.size(), -1); n       tot[i].assign(s.size(), 0); n     } n     for (int i = 0; i < (int)s.size(); i++) { n       if (tab[0][k][i].first == k) { n         sums[0][i] = tab[0][k][i].second; n         tabs[0][i] = tab[0][k][i].second; n         tot[0][i] = 1; n       } else { n         sums[0][i] = 0; n         tabs[0][i] = i; n         tot[0][i] = 0; n       } n     } n     for (int it = 1; it < maxlog; it++) { n       for (int i = 0; i < (int)s.size(); i++) { n         tabs[it][i] = tabs[it - 1][tabs[it - 1][i]]; n         sums[it][i] = sums[it - 1][i] + sums[it - 1][tabs[it - 1][i]]; n         tot[it][i] = tot[it - 1][i] + tot[it - 1][tabs[it - 1][i]]; n       } n     } n   } n }; n long long stup(int pos) { n   int lm = pos; n   int rm = pos; n   int n = (int)s.size(); n   long long ans = 0; n   while (rm - lm + 1 != n) { n     ans += n - 1 - (rm - lm); n     int mask = 0; n     for (int i = lm; i <= rm; i++) { n       mask |= (1 << (s[i] -  a )); n     } n     int l = lm; n     while (lm > 0 && mask != 0) { n       lm--; n       int c = s[lm] -  a ; n       if ((mask >> c) & 1) { n         mask ^= (1 << c); n       } n     } n     mask = 0; n     for (int i = l; i <= rm; i++) { n       mask |= (1 << (s[i] -  a )); n     } n     while (rm < n - 1 && mask != 0) { n       rm++; n       int c = s[rm] -  a ; n       if ((mask >> c) & 1) { n         mask ^= (1 << c); n       } n     } n   } n   return ans; n } n int main() { n   cin >> s; n   for (int i = 0; i < (int)1e4; i++) { n   } n   vt dpr = vt(s); n   dpr.build(); n   reverse(s.begin(), s.end()); n   vt dpl = vt(s); n   dpl.build(); n   reverse(s.begin(), s.end()); n   long long ans = 0; n   long long pre = 0; n   int n = (int)s.size(); n   vector<pair<int, int> > cur; n   for (int i = 0; i < (int)s.size(); i++) { n     cur.push_back(make_pair(n - 1 - i, i)); n     ans += n - 1; n   } n   for (int j = 0; j < 26; j++) { n     dpr.build(j); n     dpl.build(j); n     for (int i = 0; i < (int)s.size(); i++) { n       int lm = cur[i].first; n       int rm = cur[i].second; n       int cnt = dpr.get_cnt(n - 1 - lm, rm); n       if (cnt == j) { n         for (int it = maxlog - 1; it >= 0; it--) { n           if (dpr.tot[it][rm] == (1 << it) && dpl.tot[it][lm] == (1 << it)) { n             ans -= dpr.sums[it][rm]; n             rm = dpr.tabs[it][rm]; n             ans -= dpl.sums[it][lm]; n             ans += 2ll * (n - 1) * (1 << it); n             lm = dpl.tabs[it][lm]; n           } n         } n         rm = dpr.tab[0][cnt][rm].second; n         lm = dpl.tab[0][cnt][lm].second; n         ans -= rm + lm; n         ans += 2 * (n - 1); n         cur[i].first = lm; n         cur[i].second = rm; n       } n     } n   } n   cout << ans << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long n; n   cin >> n; n   long long a = (n * (n + 1) / 2); n   cout << (6 * a) + 1; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   cin >> n; n   vector<bool> primes(n + 1, 1); n   primes[0] = primes[1] = 0; n   for (int i = 2; i <= n; ++i) { n     if (!primes[i]) continue; n     for (int j = i * i; j <= n; j += i) primes[j] = 0; n   } n   vector<int> quest; n   for (int i = 2; i <= n; ++i) { n     if (!primes[i]) continue; n     int k = i; n     do { n       quest.push_back(k); n       k *= i; n     } while (k <= n); n   } n   cout << quest.size() <<   n ; n   for (int i = 0; i < quest.size(); ++i) cout << quest[i] <<    ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n char mat[110][110]; n int main() { n   int r, c; n   int cnt = 0; n   int status = 0; n   int i, j; n   scanf( %d%d  , &r, &c); n   for (i = 0; i < r; i++) { n     for (j = 0; j < c; j++) { n       scanf( %c , &mat[i][j]); n     } n     getchar(); n   } n   for (i = 0; i < c; i++) { n     if (status == 0 && mat[r - 1][i] ==  B ) { n       status = 1; n       cnt++; n     } n     if (mat[r - 1][i] ==  . ) { n       status = 0; n     } n   } n   printf( %d n , cnt); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long int m, r, t, i; n   long double s = 1.4142135623730950488; n   long long int b, f; n   long double d, ans, last, sum = 0; n   long double x, y, z; n   cin >> m >> r; n   x = 2 * r; n   y = 2 * r + (s * r); n   z = 2 * r + (2 * s * r); n   t = (m % 2 == 0) ? (m / 2) : (m / 2 + 1); n   for (i = 1; i <= t; i++) { n     d = x; n     b = (i - 1); n     f = (m - i); n     if (f > 1) { n       d = d + y + z; n       d = d + (f - 2) * z + ((f - 1) * (f - 2) * (x)) / 2; n     } n     if (f == 1) { n       d = y + d; n     } n     if (b > 1) { n       d = d + y + z; n       d = d + (b - 2) * z + ((b - 1) * (b - 2) * (x)) / 2; n     } n     if (b == 1) { n       d = y + d; n     } n     sum = sum + d; n     if (i == t) { n       last = d; n     } n   } n   sum = (m % 2 == 0) ? (2 * sum) : (2 * (sum - last) + last); n   ans = (sum) / (m * m); n   cout << setprecision(16) << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long t, a, b, cnt, p; n long long ak[101], tot; n int main() { n   cin >> t >> a >> b; n   if (t == a && a == b) { n     if (t == 1) n       cout <<  inf ; n     else n       cout <<  2 ; n   } else if (a == b) n     cout <<  1 ; n   else { n     if (a == 1) { n       cout <<  0 ; n       return 0; n     } n     if (t == 1) { n       p = 1; n       while (p < b) { n         p *= a; n         if (p == b) { n           cout <<  1 ; n           return 0; n         } n       } n     } n     memset(ak, 0, sizeof(ak)); n     tot = 0; n     ak[tot] = b; n     while (ak[tot] / a) { n       ak[tot + 1] = ak[tot] / a; n       ak[tot] %= a; n       tot++; n     } n     cnt = 0; n     p = 1; n     for (int i = 0; i < 70; i++) { n       if (ak[i]) cnt = cnt + ak[i] * p; n       p *= t; n     } n     if (cnt == a) n       cout <<  1 ; n     else n       cout <<  0 ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n; n long long a[200100]; n long long t[800800]; n long long d[800800]; n void build(int v, int tl, int tr) { n   if (tl == tr) { n     t[v] = a[tl]; n   } else { n     int tm = (tl + tr) / 2; n     build(2 * v, tl, tm); n     build(2 * v + 1, tm + 1, tr); n     t[v] = t[2 * v] + t[2 * v + 1]; n   } n } n int ind(int v, int tl, int tr, int pos) { n   if (tl == tr) { n     return v; n   } else { n     int tm = (tl + tr) / 2; n     if (pos <= tm) { n       return ind(2 * v, tl, tm, pos); n     } else { n       return ind(2 * v + 1, tm + 1, tr, pos); n     } n   } n } n void push(int v, int tl, int tr) { n   if (d[v] != 0 && 2 * v + 1 < 800800) { n     d[2 * v] += d[v]; n     d[2 * v + 1] += d[v]; n     int tm = (tl + tr) / 2; n     t[2 * v] += (tm - tl + 1) * d[v]; n     t[2 * v + 1] += (tr - tm) * d[v]; n     d[v] = 0; n   } n } n long long sum(int v, int tl, int tr, int l, int r) { n   if (l <= tl && tr <= r) { n     return t[v]; n   } n   if (l > tr || tl > r) { n     return 0; n   } n   int tm = (tl + tr) / 2; n   push(v, tl, tr); n   return sum(2 * v, tl, tm, l, r) + sum(2 * v + 1, tm + 1, tr, l, r); n } n void update(int v, int tl, int tr, int l, int r, int x) { n   if (l <= tl && tr <= r) { n     d[v] += x; n     t[v] += (tr - tl + 1) * x; n     return; n   } n   if (l > tr || tl > r) { n     return; n   } n   int tm = (tl + tr) / 2; n   push(v, tl, tr); n   update(2 * v, tl, tm, l, r, x); n   update(2 * v + 1, tm + 1, tr, l, r, x); n   t[v] = t[2 * v] + t[2 * v + 1]; n } n void update_new_val(int v, int tl, int tr, int pos, int new_val) { n   if (tl == tr) { n     t[v] = new_val; n   } else { n     int tm = (tl + tr) / 2; n     push(v, tl, tr); n     if (pos <= tm) { n       update_new_val(2 * v, tl, tm, pos, new_val); n     } else { n       update_new_val(2 * v + 1, tm + 1, tr, pos, new_val); n     } n     t[v] = t[2 * v] + t[2 * v + 1]; n   } n } n int main() { n   for (int i = 0; i < 200100; i++) { n     a[i] = 0; n   } n   scanf( %d , &n); n   long long l = 1; n   for (int i = 0; i < 800800; i++) { n     d[i] = 0; n   } n   build(1, 0, 200099); n   for (int i = 0; i < n; i++) { n     int z; n     scanf( %d , &z); n     if (z == 1) { n       int k; n       long long x; n       cin >> k >> x; n       if (x != 0) { n         update(1, 0, 200099, 0, k - 1, x); n       } n     } n     if (z == 2) { n       long long new_val; n       cin >> new_val; n       update_new_val(1, 0, 200099, l, new_val); n       l++; n     } n     if (z == 3) { n       update_new_val(1, 0, 200099, l - 1, 0); n       l--; n     } n     long long s = sum(1, 0, 200099, 0, 200099); n     double q = l * 1.0; n     double sr = (1.0 * s) / q; n     printf( %.10f n , sr); n   } n   return 0; n } n


#include <bits/stdc++.h> n const long long N = 100100; n const long long Q = 10100; n const long long mod = 1e9 + 7; n const long long block = sqrt(N); n using namespace std; n int n; n int a[N]; n int t[4 * N]; n int rl[4 * N]; n int lz[4 * N]; n void push(int x) { n   if (lz[x]) { n     t[x * 2] += lz[x]; n     lz[x * 2] += lz[x]; n     rl[x * 2] += lz[x]; n     t[x * 2 + 1] += lz[x]; n     lz[x * 2 + 1] += lz[x]; n     rl[x * 2 + 1] += lz[x]; n     lz[x] = 0; n   } n } n void up(int x) { n   t[x] = min(t[x * 2], t[x * 2 + 1]); n   rl[x] = min(rl[x * 2], rl[x * 2 + 1]); n } n void build(int x, int l, int r) { n   if (l == r) { n     t[x] = a[l] - l; n     rl[x] = a[l]; n     return; n   } n   int m = (l + r) / 2; n   build(x * 2, l, m); n   build(x * 2 + 1, m + 1, r); n   up(x); n } n void upd(int x, int l, int r, int tl, int tr, int g) { n   if (tl > tr) { n     return; n   } n   if (tl == l && r == tr) { n     t[x] += g; n     lz[x] += g; n     rl[x] += g; n     return; n   } n   push(x); n   int m = (l + r) / 2; n   upd(x * 2, l, m, tl, min(m, tr), g); n   upd(x * 2 + 1, m + 1, r, max(m + 1, tl), tr, g); n   up(x); n } n int get(int x, int l, int r) { n   if (t[x] >= 0) { n     return n + 1; n   } n   if (l == r) { n     return l; n   } n   push(x); n   int m = (l + r) / 2; n   if (t[x * 2] < 0) { n     return get(x * 2, l, m); n   } else { n     return get(x * 2 + 1, m + 1, r); n   } n } n int get_real(int x, int l, int r, int tl, int tr) { n   if (tl > tr) { n     return 1e9; n   } n   if (l == tl && r == tr) { n     return rl[x]; n   } n   push(x); n   int m = (l + r) / 2; n   return min(get_real(x * 2, l, m, tl, min(m, tr)), n              get_real(x * 2 + 1, m + 1, r, max(m + 1, tl), tr)); n } n void solve() { n   cin >> n; n   for (int i = 1; i <= n; i++) { n     cin >> a[i]; n     a[i] += i - 1; n   } n   build(1, 1, n); n   int ans = 1e9; n   for (int i = 1; i <= n; i++) { n     int g = get(1, 1, n); n     if (g <= n) { n       ans = min(ans, max(get_real(1, 1, n, g, n), get_real(1, 1, n, 1, g - 1))); n     } n     upd(1, 1, n, 1, i, 1); n     upd(1, 1, n, i + 1, n, -1); n   } n   if (ans == 1e9) { n     ans = -1; n   } n   cout << ans <<   n ; n } n bool mtest = false; n int main() { n   ios_base::sync_with_stdio(0); n   int TE = 1; n   if (mtest) cin >> TE; n   while (TE--) solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int mod = 1e9 + 7; n int i, j, k, n, m, s, t, ans1, ans2, ans; n int dp[1005][1 << 12]; n struct Tree { n   int head[1005]; n   struct edge { n     int vet, next; n   } E[2005]; n   int son[1005]; n   int num[1005]; n   int a[1005][1005]; n   int n, tot; n   Tree() { n     memset(head, -1, sizeof(head)); n     tot = -1; n   } n   void add(int u, int v) { n     E[++tot] = (edge){v, head[u]}; n     head[u] = tot; n   } n   void read() { n     scanf( %d , &n); n     for (int i = 1; i < n; i++) { n       int x, y; n       scanf( %d%d , &x, &y); n       add(x, y); n       add(y, x); n     } n   } n   void dfs(int u, int dad) { n     son[u] = 0; n     for (int e = head[u]; e != -1; e = E[e].next) n       if (e != dad) { n         son[u] |= 1 << (E[e].vet - 1); n         dfs(E[e].vet, e ^ 1); n       } n   } n   void getsize(int u, int dad) { n     num[u] = 0; n     for (int e = head[u]; e != -1; e = E[e].next) n       if (e != dad) { n         a[u][++num[u]] = E[e].vet; n         getsize(E[e].vet, e ^ 1); n       } n   } n } S, T; n int powmod(const int &x, int y) { n   if (y == 0) return 1; n   int ret = powmod(x, y >> 1); n   if (y & 1) n     ret = (long long)ret * ret % mod * x % mod; n   else n     ret = (long long)ret * ret % mod; n   return ret; n } n int dfs(int u, int num, int opt) { n   if (num == 0) return opt == 0; n   int x = S.a[u][num]; n   if (dp[x][opt] != -1) return dp[x][opt]; n   dp[x][opt] = dfs(u, num - 1, opt); n   for (int i = 0; i < T.n; i++) n     if ((1 << i) & opt) { n       dp[x][opt] += (long long)dfs(u, num - 1, opt ^ (1 << i)) * n                     dfs(x, S.num[x], T.son[i + 1]) % mod; n       if (dp[x][opt] >= mod) dp[x][opt] -= mod; n     } n   return dp[x][opt]; n } n int main() { n   S.read(); n   T.read(); n   S.getsize(1, -1); n   for (int j = 1; j <= T.n; j++) { n     memset(dp, -1, sizeof(dp)); n     T.dfs(j, -1); n     for (int i = 1; i <= S.n; i++) { n       ans1 += dfs(i, S.num[i], T.son[j]); n       if (ans1 >= mod) ans1 -= mod; n     } n   } n   S = T; n   S.getsize(1, -1); n   for (int j = 1; j <= T.n; j++) { n     memset(dp, -1, sizeof(dp)); n     T.dfs(j, -1); n     ans2 += dfs(1, S.num[1], T.son[j]); n     if (ans2 >= mod) ans2 -= mod; n   } n   ans = (long long)ans1 * powmod(ans2, mod - 2) % mod; n   printf( %d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int32_t main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   long long no_of_input; n   long long input; n   cin >> no_of_input; n   vector<long long> vect1; n   for (long long i = 0; i < no_of_input; i++) { n     cin >> input; n     vect1.push_back(input); n   } n   stack<long long> stack1; n   vector<long long> l(no_of_input + 1, -1); n   vector<long long> r(no_of_input + 1, no_of_input); n   vector<long long> ans(no_of_input + 1, n                         *min_element(vect1.begin(), vect1.end())); n   for (long long i = 0; i < no_of_input; i++) { n     while (stack1.size() != 0 && vect1[stack1.top()] >= vect1[i]) { n       stack1.pop(); n     } n     if (stack1.size() != 0) { n       l[i] = stack1.top(); n     } n     stack1.push(i); n   } n   while (stack1.size() != 0) { n     stack1.pop(); n   } n   for (long long i = vect1.size() - 1; i >= 0; i--) { n     while (stack1.size() != 0 && vect1[stack1.top()] >= vect1[i]) { n       stack1.pop(); n     } n     if (stack1.size() != 0) { n       r[i] = stack1.top(); n     } n     stack1.push(i); n   } n   for (long long i = 0; i < vect1.size(); i++) { n     long long idk = r[i] - l[i] - 1; n     ans[idk] = max(ans[idk], vect1[i]); n   } n   long long temp = ans[ans.size() - 1]; n   for (long long i = ans.size() - 1; i >= 1; i--) { n     temp = max(temp, ans[i]); n     ans[i] = max(ans[i], temp); n   } n   for (long long i = 1; i <= no_of_input; i++) { n     cout << ans[i] <<    ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 100100; n int n, a[N], b[N], aa[N], bb[N]; n long long ans; n void go(int i, int x) { n   if (ans == 0) return; n   if (x == 1) { n     if (a[i + 1] == 9) go(i + 1, -1); n     if (ans == 0) return; n     cout << i + 1 <<     << 1 <<   n ; n     --ans; n     ++a[i]; n     ++a[i + 1]; n   } else { n     if (a[i + 1] == 0) go(i + 1, 1); n     if (ans == 0) return; n     cout << i + 1 <<     << -1 <<   n ; n     --ans; n     --a[i]; n     --a[i + 1]; n   } n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cin >> n; n   int sum1 = 0, sum2 = 0; n   for (int i = 0; i < n; i++) { n     char c; n     cin >> c; n     a[i] = c -  0 ; n   } n   for (int i = 0; i < n; i++) { n     char c; n     cin >> c; n     b[i] = c -  0 ; n     aa[i] = a[i]; n     bb[i] = b[i]; n     if (i % 2 == 0) { n       sum1 += a[i]; n       sum2 += b[i]; n     } else { n       sum1 -= a[i]; n       sum2 -= b[i]; n     } n   } n   if (sum1 != sum2) return cout << -1, 0; n   ans = 0; n   for (int i = 0; i + 1 < n; i++) { n     if (aa[i] < bb[i]) { n       ans += bb[i] - aa[i]; n       aa[i + 1] += bb[i] - aa[i]; n     } else { n       ans += aa[i] - bb[i]; n       aa[i + 1] -= aa[i] - bb[i]; n     } n   } n   cout << ans <<   n ; n   ans = min(ans, 100000ll); n   for (int i = 0; i + 1 < n; i++) { n     while (ans > 0 && a[i] < b[i]) { n       go(i, 1); n     } n     while (ans > 0 && a[i] > b[i]) { n       go(i, -1); n     } n   } n } n


#include <bits/stdc++.h> n #pragma comment(linker,  /stack:200000000 ) n #pragma GCC optimize( Ofast ) n #pragma GCC target( sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native ) n using namespace std; n bool replace(std::string& str, const std::string& from, const std::string& to) { n   size_t start_pos = str.find(from); n   if (start_pos == std::string::npos) return false; n   str.replace(start_pos, from.length(), to); n   return true; n } n inline int c(string& b) { n   int w = 0; n   for (auto& c : b) w = w * 10 + c -  0 ; n   return w; n } n struct segtree { n   vector<int> t; n   vector<int> a; n   int o = 0, iv = -2e9, ee; n   int _func(int v, int vv) { return max(v, vv); } n   int func(int v1, int v2) { return _func(t[v1], t[v2]); } n   void build(vector<int>& v, int y = -2e9) { n     int x = 1; n     iv = y; n     while (x < v.size()) x <<= 1; n     x *= 2; n     t.resize(x); n     ee = x; n     o = x >> 1; n     while (v.size() < o) v.push_back(y); n     for (int i = 0; i < o; i++) t[i + o] = v[i]; n     for (int i = o - 1; i > 0; i--) t[i] = func(i * 2, i * 2 + 1); n     a.resize(x); n   } n   void push(int v) { n     if (a[v]) { n       int v1 = v * 2; n       int v2 = v * 2 + 1; n       t[v] += a[v]; n       if (v1 < ee) { n         if (v1 > o) n           t[v1] += a[v]; n         else n           a[v1] += a[v]; n       } n       if (v2 < ee) { n         if (v2 > o) n           t[v2] += a[v]; n         else n           a[v2] += a[v]; n       } n       a[v] = 0; n     } n   } n   void _add(int v, int l, int r, int vl, int vr, int w) { n     if (vl > vr) return; n     if (l > vr || r < vl) return; n     if (vl == vr && l <= vl && vr <= r) { n       t[v] += w; n       return; n     } n     if (l <= vl && vr <= r) { n       a[v] += w; n       return; n     } n     push(v); n     int m = (vl + vr) >> 1; n     _add(v << 1, l, r, vl, m, w); n     _add(1 + (v << 1), l, r, m + 1, vr, w); n     if (vl != vr) n       t[v] = _func(t[v << 1] + a[v << 1], t[v * 2 + 1] + a[v * 2 + 1]); n   } n   void add(int l, int r, int v) { _add(1, l, r, 0, o - 1, v); } n   int q(int v, int l, int r, int vl, int vr) { n     if (vl > vr) return iv; n     if (l > vr || r < vl) return iv; n     push(v); n     if (vl != vr) n       t[v] = _func(t[v * 2] + a[v * 2], t[v * 2 + 1] + a[v * 2 + 1]); n     if (l <= vl && vr <= r) return a[v] + t[v]; n     int m = (vl + vr) / 2; n     return _func(q(v * 2, l, r, vl, m), q(v * 2 + 1, l, r, m + 1, vr)); n   } n   int query(int l, int r) { return q(1, l, r, 0, o - 1); } n }; n int32_t main(int32_t argc, char** argv) { n   ios::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   int n, m, p; n   cin >> n >> m >> p; n   vector<pair<int, int>> a(n), b(m); n   vector<pair<int, pair<int, int>>> y(p); n   for (auto& e : a) cin >> e.first >> e.second; n   for (auto& e : b) cin >> e.first >> e.second; n   for (auto& e : y) cin >> e.first >> e.second.first >> e.second.second; n   sort(a.begin(), a.end()); n   sort(b.begin(), b.end()); n   sort(y.begin(), y.end()); n   vector<int> s(m); n   for (int i = 0; i < m; i++) s[i] = -b[i].second; n   int i1 = 0; n   segtree t; n   t.build(s, -2e9); n   int r = -2e9; n   for (int i = 0; i < n; i++) { n     while (i1 < y.size() && y[i1].first < a[i].first) { n       pair<int, int> a = {y[i1].second.first + 1, -2e9}; n       auto e = upper_bound(b.begin(), b.end(), a); n       int mx = (e - b.begin()); n       if (mx < m) { n         t.add(mx, m - 1, y[i1].second.second); n       } n       i1++; n     } n     r = max(r, t.query(0, m - 1) - a[i].second); n   } n   cout << r; n } n


#include <bits/stdc++.h> n using namespace std; n const int MOD = 1000000007; n const double EXP = 2.7182818284590452; n const double Pi = 3.14159265; n const double EPS = 1e-13; n const int INF = 1000 * 1000 * 1000; n const long long INFL = (long long)INF * (long long)INF; n long long n; n long long res; n void accept() { n   cin >> n; n   for (long long i = 1, j = 1; i <= INF; i *= 10, ++j) { n     res += ((0 < (((n - i + 1) < 9L * i) ? ((n - i + 1)) : (9L * i))) n                 ? ((((n - i + 1) < 9L * i) ? ((n - i + 1)) : (9L * i))) n                 : (0)) * n            j; n   } n   cout << res; n } n int main(void) { n   accept(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int M = 1e9 + 7; n int incr(string &s) { n   int i, n = s.length(); n   for (i = n - 1; i >= 0; --i) { n     if (s[i] !=  9 ) { n       s[i]++; n       return 0; n     } n     s[i] =  0 ; n   } n   for (i = n - 1; i >= 0; --i) { n     s[i] =  0 ; n   } n   s =  1  + s; n   return 0; n } n int incr2(string &s, int t) { n   while (t > 0) { n     for (int i = s.length() - 1; i >= 0; --i) { n       if (s[i] !=  9 ) { n         s[i]++; n         --t; n         if (s[i] ==  5 ) { n           if (i == 0) { n             return 1; n           } n           s.pop_back(); n           break; n         } else { n           return 0; n         } n       } else { n         s[i] =  0 ; n         s.pop_back(); n         if (i == 0) { n           return 1; n         } n       } n     } n   } n   for (int i = s.length() - 1; i >= 0; --i) { n     if (s[i] !=  9 ) { n       s[i]++; n       return 0; n     } n   } n } n int main() { n   int i, n, t; n   cin >> n >> t; n   string s; n   cin >> s; n   int di = s.find( . ); n   string dpart = s.substr(0, di); n   s = s.substr(di + 1, n - di - 1); n   if (s[0] >=  5 ) { n     incr(dpart); n     cout << dpart; n     return 0; n   } n   for (i = 0; i < s.length(); i++) { n     if (s[i] >=  5 ) { n       break; n     } n   } n   if (i == s.length()) { n     cout << dpart <<  .  << s; n     return 0; n   } n   s = s.substr(0, i); n   int c4 = 0; n   for (i = s.length() - 1; i >= 0; --i) { n     if (s[i] ==  4 ) { n       c4++; n     } else { n       break; n     } n   } n   if (c4 == 0) { n     incr2(s, 0); n     cout << dpart <<  .  << s; n     return 0; n   } n   if (c4 == s.length() && t > c4) { n     incr(dpart); n     cout << dpart; n     return (0); n   } n   cout << dpart <<  . ; n   if (t <= c4) { n     cout << s.substr(0, s.length() - t) <<  5 ; n     return 0; n   } n   s[s.length() - c4 - 1]++; n   cout << s.substr(0, s.length() - c4); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n ifstream fin( B.in ); n ofstream fout( B.out ); n long long n, a[100001], v[100001], heap[100001], pending[100001], s1, s2, ok, n     ideal, current, maxv, nr; n string s; n int main() { n   cin >> n; n   for (int i = 1; i <= 2 * n; ++i) { n     cin >> a[i]; n     v[a[i]]++; n   } n   for (int i = 10; i <= 99; ++i) { n     if (v[i] < 2) continue; n     nr = v[i] / 2; n     if (nr != 0) { n       ++s1; n       ++s2; n     } n     int j; n     for (j = 1; j <= 2 * n && nr > 0; ++j) { n       if (a[j] == i) { n         --nr; n         heap[j] = 1; n       } n     } n     nr = v[i] / 2; n     for (j = 1; j <= 2 * n && nr > 0; ++j) { n       if (a[j] == i && heap[j] != 1) { n         --nr; n         heap[j] = 2; n       } n     } n   } n   ok = 0; n   for (int i = 10; i <= 99; ++i) { n     if (v[i] != 1) continue; n     if (!ok) n       s1++; n     else n       s2++; n     for (int j = 1; j <= 2 * n; ++j) { n       if (a[j] == i && !heap[j]) { n         if (!ok) n           heap[j] = 1; n         else n           heap[j] = 2; n       } n     } n     ok = 1 - ok; n   } n   for (int i = 10; i <= 99; ++i) { n     if (v[i] % 2 != 1 || v[i] == 1) continue; n     for (int j = 1; j <= 2 * n; ++j) { n       if (a[j] == i && !heap[j]) { n         if (!ok) n           heap[j] = 1; n         else n           heap[j] = 2; n       } n     } n     ok = 1 - ok; n   } n   cout << s1 * s2 <<   n ; n   ok = 0; n   for (int i = 1; i <= 2 * n; ++i) { n     cout << heap[i]; n     cout <<    ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int i, ans, f; n long long n, l, r, mid, x; n long long check(long long mid) { n   if ((mid - 1) / 2 > n / mid) return n + 1; n   return mid * ((1ll << i) - 1) + mid * (mid - 1) / 2; n } n int main() { n   scanf( %I64d , &n); n   x = (int)(sqrt(n * 2) + 1e-11); n   if (x * (x + 1) == n * 2 && (x + 1) & 1) printf( %I64d n , x + 1), f = 1; n   for (i = 1; (1ll << i) - 1 <= n; ++i) { n     l = 1; n     r = n / ((1ll << i) - 1); n     while (l <= r) { n       mid = (l + r) / 2; n       if (check(mid) <= n) n         l = mid + 1; n       else n         r = mid - 1; n     } n     if ((l - 1) & 1 && check(l - 1) == n) n       printf( %I64d n , (l - 1) * 1 << i), f = 1; n   } n   if (!f) printf( -1 n ); n } n


#include <bits/stdc++.h> n using namespace std; n long long int pwr(long long int a, long long int n, long long int m) { n   long long int p = 1; n   while (n > 0) { n     if (n % 2 == 1) p = (p * a) % m; n     a = (a * a) % m; n     n = n / 2; n   } n   return p; n } n char str[1000000]; n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   long long int n, a, b, k, s = 0, i, j; n   cin >> n >> a >> b >> k; n   long long int f = pwr(b, k, (long long int)(1e9 + 9)); n   long long int g = pwr(a, k, (long long int)(1e9 + 9)); n   long long int r = n       (f * pwr(g, (long long int)(1e9 + 9) - 2, (long long int)(1e9 + 9))) % n       (long long int)(1e9 + 9); n   r = ((pwr(r, (n + 1) / k, (long long int)(1e9 + 9)) - 1 + n         (long long int)(1e9 + 9)) % n        (long long int)(1e9 + 9) * n        pwr(r - 1, (long long int)(1e9 + 9) - 2, (long long int)(1e9 + 9))) % n       (long long int)(1e9 + 9); n   if (r == 0) r = (n + 1) / k; n   cin >> str; n   for (i = 0; i < k; i++) { n     if (str[i] ==  + ) n       s = (s + n            (pwr(a, n - i, (long long int)(1e9 + 9)) * n             pwr(b, i, (long long int)(1e9 + 9))) % n                (long long int)(1e9 + 9) + n            (long long int)(1e9 + 9)) % n           (long long int)(1e9 + 9); n     else n       s = (s - n            (pwr(a, n - i, (long long int)(1e9 + 9)) * n             pwr(b, i, (long long int)(1e9 + 9))) % n                (long long int)(1e9 + 9) + n            (long long int)(1e9 + 9)) % n           (long long int)(1e9 + 9); n   } n   cout << ((s * r) % (long long int)(1e9 + 9) + (long long int)(1e9 + 9)) % n               (long long int)(1e9 + 9) n        << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1000; n int main(int argc, char *argv[]) { n   string s; n   vector<pair<char, int> > count; n   int n; n   cin >> s; n   n = s.size(); n   for (int i = 0; i < n;) { n     int cnt = 0; n     int prev = s[i]; n     while (s[i] == prev) { n       cnt++; n       i++; n     } n     count.push_back(make_pair(prev, cnt)); n   } n   for (int i = 0; i < count.size(); i++) { n     if (count[i].second >= 3) count[i].second = 2; n   } n   for (int i = 0; i < count.size() - 1; i++) { n     if (count[i].second == 2 && count[i + 1].second == 2) n       count[i + 1].second = 1; n   } n   for (int i = 0; i < count.size(); i++) { n     for (int j = 0; j < count[i].second; j++) cout << count[i].first; n   } n } n


#include <bits/stdc++.h> n using namespace std; n long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; } n signed main() { n   ios_base::sync_with_stdio(false); n   cin.tie(nullptr); n   long long n, a, b, p, q; n   cin >> n >> a >> b >> p >> q; n   long long someGcd = gcd(a, b); n   long long lcm = a * b / someGcd; n   long long max = (p > q) ? p : q; n   if (p > q) { n     long long counta = p * (n / a); n     long long countb = q * (n / b) - q * (n / lcm); n     cout << counta + countb; n   } else { n     long long counta = p * (n / a) - p * (n / lcm); n     long long countb = q * (n / b); n     cout << counta + countb; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   string second; n   cin >> second; n   bool check = 0; n   string tmp(1, second[0]); n   for (int i = 1; i < second.size(); i++) { n     if (second[i] ==  d  && second[i + 1] ==  o  && second[i + 2] ==  t  && n         i < second.size() - 3) n       tmp +=  . , i += 2; n     else if (second[i] ==  a  && second[i + 1] ==  t  && !check && n              i < second.size() - 2) n       tmp +=  @ , check = 1, i++; n     else n       tmp += second[i]; n   } n   cout << tmp << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int k; n long long ans[10009]; n bool chk(long long n) { n   int m = 0; n   while (n) { n     m += (n % 10); n     n /= 10; n     if (m > 10) return 0; n   } n   return (m == 10); n } n int main() { n   cin >> k; n   ans[0] = 19; n   for (int i = 1; i <= 10000; i++) { n     ans[i] = ans[i - 1] + 9; n     while (!chk(ans[i])) ans[i] += 9; n   } n   cout << ans[k - 1]; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n string s; n void solve() { n   long long n = s.length(), i, j, k; n   long long piv = 0; n   for (i = 0; i < n; i++) n     if (s[i] ==  ^ ) { n       piv = i; n       break; n     } n   long long l = piv; n   long long r = n - piv - 1; n   long long c1 = 0, c2 = 0; n   for (i = 0; i < l; i++) { n     if (s[i] ==  = ) continue; n     j = (piv - i) * (s[i] -  0 ); n     c1 += j; n   } n   for (i = piv + 1; i < n; i++) { n     if (s[i] ==  = ) continue; n     j = (i - piv) * (s[i] -  0 ); n     c2 += j; n   } n   if (c1 == c2) cout <<  balance n ; n   if (c1 > c2) cout <<  left n ; n   if (c1 < c2) cout <<  right n ; n } n int main(int argc, char const *argv[]) { n   cin >> s; n   solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 200; n int h[maxn]; n long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; } n int main() { n   long long n, m, sx, sy, ex, ey; n   int q; n   scanf( %lld%lld%d , &n, &m, &q); n   long long gc = gcd(n, m); n   long long stp1 = n / gc, stp2 = m / gc; n   long long num1, num2; n   while (q--) { n     scanf( %lld%lld%lld%lld , &sx, &sy, &ex, &ey); n     num1 = num2 = 0; n     if (sx == 1) { n       num1 = sy / stp1; n       if (sy % stp1 != 0) num1++; n     } else { n       num1 = sy / stp2; n       if (sy % stp2 != 0) num1++; n     } n     if (ex == 1) { n       num2 = ey / stp1; n       if (ey % stp1 != 0) num2++; n     } else { n       num2 = ey / stp2; n       if (ey % stp2 != 0) num2++; n     } n     if (num1 == num2) n       printf( YES n ); n     else n       printf( NO n ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int k[10] = {2, 7, 2, 3, 3, 4, 2, 5, 1, 2}; n int main(void) { n   int n; n   scanf( %d , &n); n   int x = n / 10, y = n % 10; n   printf( %d n , k[x] * k[y]); n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T> n void printContainer(T& a, int n = -1) { n   if (n == -1) n = a.size(); n   cout <<  { ; n   int i = 0; n   for (auto it : a) { n     cout << (it) <<  ,  ; n     if (++i == n) break; n   } n   cout <<  } n ; n } n int main() { n   string s[2]; n   cin >> s[0] >> s[1]; n   int typ[2]; n   int score = 0; n   int c = 0; n   for (int p = 0; p < s[0].size(); p += 2) { n     for (int i = 0; i < (int)(2); ++i) { n       int r = 2; n       if (s[i][p] ==  [ ) n         r = 0; n       else if (s[i][p] ==  ( ) n         r = 1; n       typ[i] = r; n     } n     if (typ[0] == typ[1]) continue; n     for (int i = 0; i < (int)(3); ++i) n       if (((typ[0] + 1) % 3) == typ[1]) { n         ++score; n         goto final; n       } n     --score; n   final: n     ++c; n   } n   if (score == 0) { n     cout <<  TIE  << endl; n   } else { n     cout << (score > 0 ?  TEAM 1 WINS  :  TEAM 2 WINS ) << endl; n     ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const double PI = 2 * acos(0.0); n const double eps = 1e-9; n double toRad(int angle) { return PI * angle / 180.0; } n int a, w, h; n double angle; n complex<double> intersect(complex<double> p1, complex<double> p2, n                           complex<double> p3, complex<double> p4) { n   complex<double> ret; n   double u = (p4.real() - p3.real()) * (p1.imag() - p3.imag()) - n              (p4.imag() - p3.imag()) * (p1.real() - p3.real()); n   u /= (p4.imag() - p3.imag()) * (p2.real() - p1.real()) - n        (p4.real() - p3.real()) * (p2.imag() - p1.imag()); n   return complex<double>(p1.real() + u * (p2.real() - p1.real()), n                          p1.imag() + u * (p2.imag() - p1.imag())); n } n complex<double> rotate(complex<double> p, double angle) { n   return complex<double>(p.real() * cos(angle) - p.imag() * sin(angle), n                          p.real() * sin(angle) + p.imag() * cos(angle)); n } n double dist(complex<double> a, complex<double> b) { n   return (hypot((a - b).real(), (a - b).imag())); n } n int main() { n   cin >> w >> h >> a; n   long double ans = 0; n   if (a > 90) a = 180 - a; n   if (a == 0) n     ans = 1.0 * w * h; n   else if (a == 90) n     ans = min(w, h) * min(w, h); n   else { n     angle = toRad(a); n     if (w < h) swap(w, h); n     complex<double> A(-0.5 * w, 0.5 * h); n     complex<double> B(0.5 * w, 0.5 * h); n     complex<double> C(0.5 * w, -0.5 * h); n     complex<double> D(-0.5 * w, -0.5 * h); n     complex<double> E = rotate(A, angle), F = rotate(B, angle), n                     G = rotate(C, angle), H = rotate(D, angle); n     complex<double> N = intersect(A, B, E, F); n     complex<double> M = intersect(A, D, E, F); n     complex<double> O = intersect(A, B, F, G); n     complex<double> P = intersect(B, C, F, G); n     ans = 1.0 * w * h; n     if (G.imag() < B.imag()) { n       long double area = dist(A, M) * dist(A, N) + dist(B, O) * dist(B, P); n       ans -= area; n     } else { n       N = intersect(A, B, H, G); n       M = intersect(C, D, H, G); n       complex<double> K(N.real(), M.imag()); n       ans -= 2.0 * h * dist(C, K); n       ans -= dist(K, M) * dist(N, K); n     } n   } n   cout << fixed << setprecision(10) << ans << endl; 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   ; n   long long int t; n   cin >> t; n   while (t--) { n     long long int n; n     cin >> n; n     float a = 1; n     float b = (float)360 / ((2 * n)); n     b /= 2; n     b = (3.14159265358979323846264338327950288419716939937510582 / 180) * b; n     float ans = 1 / tan(b); n     cout << fixed << setprecision(15) << ans << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename A, typename B, typename C> n struct triple { n   A X; n   B Y; n   C Z; n   triple(A a = 0, B b = 0, C c = 0) : X(a), Y(b), Z(c) {} n }; n template <typename A, typename B, typename C> n triple<A, B, C> make_triple(A a = 0, B b = 0, C c = 0) { n   return triple<A, B, C>(a, b, c); n } n template <typename A, typename B, typename C> n bool operator<(const triple<A, B, C>& a, const triple<A, B, C>& b) { n   if (a.X != b.X) return a.X < b.X; n   if (a.Y != b.Y) return a.Y < b.Y; n   return a.Z < b.Z; n } n template <typename T, typename SS> n ostream& operator<<(ostream& ofs, const pair<T, SS>& p) { n   ofs <<  (   << p.first <<   ,   << p.second <<   ) ; n   return ofs; n } n template <typename T> n void print(T a) { n   for (auto i : a) cout << i <<    ; n   cout <<   n ; n } n template <typename T> n T max(T a, T b, T c) { n   return max(a, max(b, c)); n } n template <typename T> n T min(T a, T b, T c) { n   return min(a, min(b, c)); n } n struct custom_hash { n   static uint64_t splitmix64(uint64_t x) { n     x += 0x9e3779b97f4a7c15; n     x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9; n     x = (x ^ (x >> 27)) * 0x94d049bb133111eb; n     return x ^ (x >> 31); n   } n   size_t operator()(uint64_t x) const { n     static const uint64_t FIXED_RANDOM = n         chrono::steady_clock::now().time_since_epoch().count(); n     return splitmix64(x + FIXED_RANDOM); n   } n }; n signed main() { n   srand(time(NULL)); n   cin.tie(0); n   cout.tie(0); n   ios_base::sync_with_stdio(false); n   long long n; n   cin >> n; n   vector<long long> a(n); n   for (long long i = 0; i < (n); i++) cin >> a[i]; n   long long ans = 0; n   auto solve = [&](long long v) { n     vector<long long> b(a); n     long long pw = 1 << v; n     for (long long i = 0; i < (n); i++) b[i] %= (pw * 2); n     sort(b.begin(), b.end()); n     long long c = 0; n     auto count = [&](long long l, long long r) -> long long { n       return lower_bound(b.begin(), b.end(), r) - n              lower_bound(b.begin(), b.end(), l); n     }; n     for (long long i = 0; i < (n); i++) n       c += (count(pw - b[i], pw * 2 - b[i]) + n             count(pw * 3 - b[i], pw * 4 - b[i])); n     for (long long i = 0; i < (n); i++) n       if ((2 * b[i]) & pw) c--; n     c >>= 1; n     return c & 1; n   }; n   for (long long i = 0; i < (25); i++) n     if (solve(i)) ans += 1LL << i; n   cout << ans <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n long long int gcd(long long int a, long long int b) { n   if (b != 0) n     return gcd(b, a % b); n   else n     return a; n } n long long int power(long long int a, long long int b) { n   if (b == 0) return 1; n   if (b == 1) return a; n   if (b % 2 == 0) { n     long long int t = power(a, b / 2); n     return t * t; n   } else { n     long long int t = power(a, b / 2); n     return a * t * t; n   } n } n long long int powin(long long int a, long long int b) { n   if (b == 0) n     return 1; n   else if (b == 1) n     return a; n   else if (b % 2 == 0) { n     long long int t = powin(a, b / 2); n     return (t * t) % 1000000007; n   } else { n     long long int t = powin(a, b / 2); n     return (((t * t) % 1000000007) * a) % 1000000007; n   } n } n long long int n, k; n long long int dp[509][10 * 509]; n long long int h[20]; n void solve() { n   for (long long int i = 1; i < n + 1; i++) { n     for (long long int j = 1; j < k * n + 1; j++) { n       for (long long int p = 1; p < k + 1; p++) { n         if (j >= p) dp[i][j] = max(dp[i][j], dp[i - 1][j - p] + h[p]); n       } n     } n   } n } n long long int fr[100000 + 10]; n long long int f[100000 + 10]; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cin >> n >> k; n   memset(dp, 0, sizeof dp); n   memset(fr, 0, sizeof fr); n   memset(f, 0, sizeof f); n   for (long long int i = 0; i < n * k; i++) { n     long long int t; n     cin >> t; n     fr[t]++; n   } n   for (long long int i = 0; i < n; i++) { n     int p; n     cin >> p; n     f[p]++; n   } n   h[0] = 0; n   for (long long int i = 1; i < k + 1; i++) { n     cin >> h[i]; n     dp[1][i] = h[i]; n   } n   solve(); n   long long int ans = 0; n   for (long long int i = 1; i < 100000 + 1; i++) { n     if (f[i] > 0) { n       long long int temp = 0; n       for (long long int j = 1; j < f[i] + 1; j++) { n         temp = max(dp[j][fr[i]], temp); n       } n       ans += temp; n     } n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void read(long long &x) { n   x = 0; n   char c = getchar(); n   int f = 1; n   while (!(c >=  0  && c <=  9 )) { n     if (c ==  - ) f = -1; n     c = getchar(); n   } n   while ((c >=  0  && c <=  9 )) { n     x = (x << 1) + (x << 3) + (c ^ 48); n     c = getchar(); n   } n   x *= f; n } n void output(long long x) { n   if (x < 0) { n     x = -x; n     putchar( - ); n   } n   if (x > 9) output(x / 10); n   putchar(x % 10 +  0 ); n } n struct node { n   long long l, r; n   mutable long long val; n   node(long long L) : l(L) {} n   node(long long L, long long R, long long VAL) : l(L), r(R), val(VAL) {} n }; n bool operator<(node x, node y) { return x.l < y.l; } n set<node> s; n set<node>::iterator split(long long pos) { n   set<node>::iterator it = s.lower_bound(node(pos)); n   if (it != s.end() && it->l == pos) return it; n   it--; n   long long l = it->l, r = it->r, val = it->val; n   s.erase(it); n   s.insert(node(l, pos - 1, val)); n   return s.insert(node(pos, r, val)).first; n } n void add(long long l, long long r, long long x) { n   set<node>::iterator it2 = split(r + 1), it1 = split(l); n   for (set<node>::iterator it = it1; it != it2; ++it) { n     it->val += x; n   } n } n long long kth(long long l, long long r, long long x) { n   set<node>::iterator it2 = split(r + 1), it1 = split(l); n   vector<pair<long long, long long> > v; n   v.clear(); n   for (set<node>::iterator it = it1; it != it2; ++it) { n     v.push_back(pair<long long, long long>(it->val, it->r - it->l + 1)); n   } n   sort(v.begin(), v.end()); n   for (long long i = 0; i < v.size(); ++i) { n     x -= v[i].second; n     if (x <= 0) return v[i].first; n   } n } n long long ksm(long long a, long long b, long long c) { n   a %= c; n   long long ans = 1; n   while (b) { n     if (b & 1) ans = (ans * a) % c; n     a = (a * a) % c; n     b >>= 1; n   } n   return ans; n } n long long query(long long l, long long r, long long x, long long y) { n   long long ans = 0; n   set<node>::iterator it2 = split(r + 1), it1 = split(l); n   for (set<node>::iterator it = it1; it != it2; ++it) { n     ans = (ans + (it->r - it->l + 1) * ksm(it->val, x, y) % y) % y; n   } n   return ans; n } n void assign(long long l, long long r, long long k) { n   set<node>::iterator it2 = split(r + 1), it1 = split(l); n   s.erase(it1, it2); n   s.insert(node(l, r, k)); n } n long long n, m, seed, vmax; n long long op, l, r, x, y; n long long rnd() { n   long long ret = seed; n   seed = (seed * 7 + 13) % 1000000007; n   return ret; n } n void SWAP(long long &x, long long &y) { n   x ^= y; n   y ^= x; n   x ^= y; n } n int main() { n   read(n), read(m), read(seed), read(vmax); n   for (long long i = 1; i <= n; ++i) { n     x = (rnd() % vmax) + 1; n     s.insert(node(i, i, x)); n   } n   for (long long i = 1; i <= m; ++i) { n     op = (rnd() % 4) + 1; n     l = (rnd() % n) + 1; n     r = (rnd() % n) + 1; n     if (l > r) SWAP(l, r); n     if (op == 1) { n       x = (rnd() % vmax) + 1; n       add(l, r, x); n     } else if (op == 2) { n       x = (rnd() % vmax) + 1; n       assign(l, r, x); n     } else if (op == 3) { n       x = (rnd() % (r - l + 1)) + 1; n       output(kth(l, r, x)); n       putchar(  n ); n     } else { n       x = (rnd() % vmax) + 1; n       y = (rnd() % vmax) + 1; n       output(query(l, r, x, y)); n       putchar(  n ); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct ln { n   long long m, k; n   ln(long long mm, long long kk) { n     m = mm; n     k = kk; n   } n }; n deque<ln> cvt; n long long dp[100010][2]; n long long sw[100010]; n long long wt[100010]; n long long hilldist[100010]; n long long cs(long long x, int ci) { return x * cvt[ci].m + cvt[ci].k; } n bool ex(long long m, long long k) { n   long long m1 = cvt[cvt.size() - 2].m; n   long long k1 = cvt[cvt.size() - 2].k; n   long long m2 = cvt[cvt.size() - 1].m; n   long long k2 = cvt[cvt.size() - 1].k; n   return (k1 - k2) * (m - m1) >= (k1 - k) * (m2 - m1); n } n int main() { n   int n, m, p; n   int d, h, t; n   scanf( %d%d%d , &n, &m, &p); n   for (int i = 2; i <= n; i++) { n     scanf( %d , &d); n     hilldist[i] = hilldist[i - 1] + d; n   } n   for (int i = 1; i <= m; i++) { n     scanf( %d%d , &h, &t); n     wt[i] = t - hilldist[h]; n   } n   sort(wt + 1, wt + m + 1); n   for (int i = 1; i <= m; i++) sw[i] = sw[i - 1] + wt[i]; n   int nx = 1, nw = 0; n   for (int i = 1; i <= m; i++) { n     dp[i][0] = wt[i] * i - sw[i]; n   } n   int k1; n   for (int k = 2; k <= p; k++) { n     cvt.clear(); n     k1 = k - 1; n     cvt.push_back(ln(-k1, sw[k1] + dp[k1][nw])); n     for (int i = 0; i <= m; i++) dp[i][nx] = 0; n     for (int i = k; i <= m; i++) { n       while (cvt.size() > 1 && cs(wt[i], 0) >= cs(wt[i], 1)) cvt.pop_front(); n       dp[i][nx] = cs(wt[i], 0) + wt[i] * (i)-sw[i]; n       while (cvt.size() > 1 && ex(-i, sw[i] + dp[i][nw])) cvt.pop_back(); n       cvt.push_back(ln(-i, sw[i] + dp[i][nw])); n     } n     swap(nx, nw); n   } n   cout << dp[m][nw]; n } n


#include <bits/stdc++.h> n using namespace std; n int x[100]; n int a[10010000]; n int main() { n   int a, b, n; n   scanf( %d%d%d , &a, &b, &n); n   if (b >= n - 1) n     puts( 2 ); n   else { n     int f = 0; n     for (int i = 1; i < n && i <= a; i++) { n       int x = (long long)i * 1000000000 % n; n       if (x > 0 && x <= n - b - 1) { n         f = 1; n         printf( 1 %09d n , i); n         break; n       } n     } n     if (!f) puts( 2 ); n   } n } n


#include <bits/stdc++.h> n using namespace std; n const double eps = 1e-6; n const int mod = 1e9 + 7; n const int maxn = 2e6 + 100; n const int maxm = 2e6 + 100; n const int inf = 0x3f3f3f3f; n const double pi = acos(-1.0); n int t; n pair<long long, long long> a[maxn]; n long long s; n int n; n int vis[maxn]; n bool cmp2(pair<long long, long long> a, pair<long long, long long> b) { n   return a.second < b.second; n } n vector<long long> mm, rr, lll; n int ck(long long x) { n   int L = 0, R = 0, M = 0; n   lll.clear(); n   rr.clear(); n   mm.clear(); n   for (int i = 1; i <= n; i++) { n     if (a[i].first <= x && a[i].second >= x) { n       mm.push_back(a[i].first); n       M++; n       continue; n     } n     if (a[i].first > x) { n       R++; n       rr.push_back(a[i].first); n     } n     if (a[i].second < x) { n       L++; n       lll.push_back(a[i].first); n     } n   } n   if (L > n / 2) return false; n   long long res = 0; n   sort(mm.begin(), mm.end()); n   if (L < n / 2) { n     int to = M; n     for (int i = 0; i < to; i++) { n       lll.push_back(mm[i]); n       L++; n       M--; n       if (L == n / 2) break; n     } n   } n   if (R < n / 2 + 1) { n     if (R + M != n / 2 + 1) return false; n     res += 1ll * M * x; n   } n   for (int i = 0; i < (int)lll.size(); i++) { n     res += lll[i]; n   } n   for (int i = 0; i < (int)rr.size(); i++) { n     res += max(rr[i], x); n   } n   return res <= s; n } n int main() { n   scanf( %d , &t); n   while (t--) { n     long long mx = 0; n     scanf( %d %lld , &n, &s); n     for (int i = 1; i <= n; i++) { n       scanf( %lld %lld , &a[i].first, &a[i].second); n       mx = max(mx, a[i].second); n     } n     long long ans = 0; n     sort(a + 1, a + 1 + n); n     long long l = a[n / 2 + 1].first; n     sort(a + 1, a + 1 + n, cmp2); n     long long r = a[n / 2 + 1].second; n     while (l <= r) { n       long long mid = l + r >> 1; n       if (ck(mid)) { n         l = mid + 1; n         ans = mid; n       } else n         r = mid - 1; n     } n     printf( %lld n , ans); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long int max(long long int a, long long int b) { n   if (a > b) n     return a; n   else n     return b; n } n void solve() { n   int n; n   cin >> n; n   vector<int> a(2 * n); n   for (int i = 0; i < 2 * n; i++) { n     cin >> a[i]; n   } n   int din = 0; n   for (int i = 0; i < 2 * n; i++) { n     if (a[i] == 1) n       din += 1; n     else n       din -= 1; n   } n   if (din == 0) { n     cout << 0 << endl; n     return; n   } n   map<int, int> m; n   int dr = 0; n   m[0] = n - 1; n   for (int i = n; i < 2 * n; i++) { n     if (a[i] == 1) n       dr += 1; n     else n       dr -= 1; n     if (!m.count(dr)) { n       m[dr] = i; n     } n   } n   int ans = 1e9 + 7; n   int dl = 0; n   if (m.count(din)) { n     ans = min(ans, m[din] - n + 1); n   } n   for (int i = n - 1; i >= 0; i--) { n     if (a[i] == 1) n       dl += 1; n     else n       dl -= 1; n     int q = din - dl; n     if (m.count(q)) { n       ans = min(ans, m[q] - i + 1); n     } n   } n   cout << ans << endl; n } n int main() { n   int t; n   cin >> t; n   while (t--) { n     solve(); n   } n } n


#include <bits/stdc++.h> n using namespace std; n const long double PI = acos(-1.0); n const double EPS = 1E-6; n const int INF = 2 * (1e+9) + 1; n const int MAXN = 3 * 100000001; n const int MOD = (1e+9) + 7; n int n, m; n map<int, int> m1, m2; n map<int, int> u, cnt; n int main() { n   cin >> n; n   for (int i = 0; i < (n); ++i) { n     int t1, t2; n     cin >> t1 >> t2; n     m1[t1] = t2; n     m2[t2] = t1; n     cnt[t1]++; n     cnt[t2]++; n   } n   int cur = 0; n   vector<int> ans(n); n   int inx = 1; n   while (inx < n) { n     cur = m1[cur]; n     ans[inx] = cur; n     u[cur] = 1; n     inx += 2; n     if (!cur) break; n   } n   cur = 0, inx = n - 2; n   while (inx >= 0) { n     cur = m2[cur]; n     if (!cur) break; n     ans[inx] = cur; n     u[cur] = 1; n     inx -= 2; n   } n   if (n % 2 == 0) { n     for (map<int, int>::iterator it = m1.begin(); it != m1.end(); it++) { n       if (!u[it->first] && it->first) { n         ans[0] = it->first; n       } n     } n   } else { n     for (map<int, int>::iterator it = m1.begin(); it != m1.end(); it++) { n       if (cnt[it->first] == 1 && (!u[it->first])) { n         ans[0] = it->first; n         cur = ans[0], inx = 2; n         while (inx < n) { n           cur = m1[cur]; n           ans[inx] = cur; n           u[cur] = 1; n           inx += 2; n           if (!cur) break; n         } n         break; n       } n     } n   } n   for (int i = 0; i < ((int)(ans).size()); ++i) { n     cout << ans[i] <<    ; n   } n   return 0; n } n