#include <bits/stdc++.h> n using namespace std; n const long long MAXN = 1e5 + 10; n long long tree[MAXN * 4][7][11], s[6][MAXN], a[MAXN]; n void Pushup(long long u, long long len) { n   long long z, i, j; n   for (z = 2; z <= 6; z++) n     for (i = 0; i < 2 * (z - 1); i++) n       tree[u][z][i] = n           tree[u * 2][z][i] + tree[u * 2 + 1][z][(i + len) % (2 * (z - 1))]; n   return; n } n void Build(long long u, long long l, long long r) { n   long long z, i; n   if (l == r) { n     for (z = 2; z <= 6; z++) n       for (i = 0; i < (z - 1) * 2; i++) tree[u][z][i] = a[l] * s[z][i]; n     return; n   } n   long long mid = (l + r) >> 1; n   Build(u * 2, l, mid); n   Build(u * 2 + 1, mid + 1, r); n   Pushup(u, mid - l + 1); n } n void Update(long long u, long long l, long long r, long long pos, n             long long val) { n   long long z, i; n   if (l == r) { n     for (z = 2; z <= 6; z++) n       for (i = 0; i < 2 * (z - 1); i++) tree[u][z][i] = val * s[z][i]; n     return; n   } n   long long mid = (l + r) >> 1; n   if (pos <= mid) n     Update(u * 2, l, mid, pos, val); n   else n     Update(u * 2 + 1, mid + 1, r, pos, val); n   Pushup(u, mid - l + 1); n } n long long Query(long long u, long long L, long long R, long long l, long long r, n                 long long z, long long i) { n   if (L == l && R == r) return tree[u][z][i]; n   long long mid = (L + R) >> 1; n   if (r <= mid) return Query(u * 2, L, mid, l, r, z, i); n   if (l > mid) return Query(u * 2 + 1, mid + 1, R, l, r, z, i); n   return Query(u * 2, L, mid, l, mid, z, i) + n          Query(u * 2 + 1, mid + 1, R, mid + 1, r, z, n                (i + mid - l + 1) % (2 * (z - 1))); n } n int main() { n   long long n, m, z, i, j; n   scanf( %lld , &n); n   for (i = 1; i <= n; i++) scanf( %lld , &a[i]); n   for (z = 2; z <= 6; z++) { n     long long mod = 2 * (z - 1); n     for (i = 1; i <= 100000; i++) { n       if (i % mod == 0) n         s[z][i - 1] = 2; n       else if (i % mod <= z) n         s[z][i - 1] = i % mod; n       else n         s[z][i - 1] = (z * 2) - (i % mod); n     } n   } n   Build(1, 1, n); n   scanf( %lld , &m); n   for (i = 1; i <= m; i++) { n     long long op; n     scanf( %lld , &op); n     if (op == 1) { n       long long pos, val; n       scanf( %lld %lld , &pos, &val); n       Update(1, 1, n, pos, val); n     } else { n       long long l, r, z; n       scanf( %lld %lld %lld , &l, &r, &z); n       printf( %lld n , Query(1, 1, n, l, r, z, 0)); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <class T> n void miz(T &a, T b) { n   if (b < a) a = b; n } n template <class T> n void maz(T &a, T b) { n   if (b > a) a = b; n } n template <class T> n void clear(T &x) { n   T t; n   x.swap(t); n } n template <class T> n T gcd(T a, T b) { n   while (b) { n     a %= b; n     swap(a, b); n   } n   return a; n } n template <class T> n T lcm(T a, T b) { n   return a / gcd(a, b) * b; n } n template <class T> n T sqr(T x) { n   return x * x; n } n unsigned HASH(const char *p) { n   unsigned r = 0; n   while (*p) r = r * 257 + 1 + *p++; n   return r; n } n int ROUND(double x) { return x >= 0 ? x + 0.5 : x - 0.5; } n int cnt[1000005]; n int main() { n   int k, c, i, now = 0; n   long long ans = 0; n   scanf( %d  , &k); n   cnt[0] = 1; n   for (i = 1; (c = getchar()) !=   n ; i++) { n     if (c ==  1 ) now++; n     if (now >= k) ans += cnt[now - k]; n     cnt[now]++; n   } n   cout << ans << endl; n   return 0; n } n


/* n 西江月·证明 n 即得易见平凡，仿照上例显然；留作习题答案略，读者自证不难。 n 反之亦然同理，推论自然成立；略去过程Q.E.D.，由上可知证毕。 n  n result of thinking: Pure.  n 主要是非内、外向树的情形容易遗漏. n   n AC: n */ n #include <bits/stdc++.h> n using namespace std; n typedef long long ll; n typedef unsigned long long ull; n typedef long double ldouble; n template<class T> bool chmin(T &x, const T &y) { n   return x > y ? (x = y, true) : false; n } n template<class T> bool chmax(T &x, const T &y) { n   return x < y ? (x = y, true) : false; n } n  n #define maxn 1000005 n const ll mod = 998244353; n const ll inv2 = 499122177; n const ll inv6 = 166374059; n int n; n ll f[maxn], r[maxn], df[maxn]; n  n int main() { n   scanf( %d , &n); n   f[0] = r[0] = 1; n   for (int i = 1; i <= n; i++) { n     f[i] = (f[i - 1] * f[i - 1] % mod * inv2 + 3 * f[i - 1] * inv2 + 1) % mod; n     r[i] = (f[i] + (f[i - 1] * f[i - 1] % mod * f[i - 1] n                     + f[i - 1] * f[i - 1] * 3 n                     + f[i - 1] * 2) % mod * inv6) % mod; n   } n   ll ans = ((r[n] - r[n - 1]) * 2 - 1) % mod; n   df[0] = f[0]; n   for (int i = 1; i <= n; i++) n     df[i] = f[i] - f[i - 1]; n   for (int i = 1; i <= n - 2; i++) { n     (ans += (df[i] - df[i - 1]) * (df[n - 1 - i] - 1)) %= mod; n   } n   printf( %lld n , (ans + mod) % mod); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 201; n set<int> graph[N]; n map<pair<int, int>, int> edges; n pair<int, int> bfs(int node) { n   int dist[N] = {0}; n   int vis[N] = {0}; n   queue<pair<int, int> > q; n   q.push({0, node}); n   while (!q.empty()) { n     int top = q.front().second; n     q.pop(); n     if (vis[top]) continue; n     vis[top] = true; n     for (set<int>::iterator it = graph[top].begin(); it != graph[top].end(); n          it++) { n       int to = *it; n       if (vis[to]) continue; n       dist[to] = dist[top] + 1; n       q.push({dist[to], to}); n     } n   } n   pair<int, int> temp = {node, 0}; n   for (int i = 1; i <= N; i++) { n     if (dist[i] > temp.second) { n       temp.second = dist[i]; n       temp.first = i; n     } n   } n   return temp; n } n int find_dia(int node) { n   pair<int, int> temp = bfs(node); n   temp = bfs(temp.first); n   return temp.second; n } n int main(void) { n   int n; n   cin >> n; n   for (int i = 1; i < n; i++) { n     int u, v; n     cin >> u >> v; n     graph[u].insert(v); n     graph[v].insert(u); n     edges[{u, v}] = 1; n   } n   int ans = 0; n   for (map<pair<int, int>, int>::iterator it = edges.begin(); it != edges.end(); n        it++) { n     int u = (*it).first.first; n     int v = (*it).first.second; n     graph[u].erase(v); n     graph[v].erase(u); n     ans = max(ans, find_dia(v) * find_dia(u)); n     graph[u].insert(v); n     graph[v].insert(u); n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, n1, n2; n   scanf( %d%d%d , &n, &n1, &n2); n   if (n1 > n2) swap(n1, n2); n   vector<double> X; n   double x; n   for (int i = 0; i < n; i++) { n     scanf( %lf , &x); n     X.push_back(x); n   } n   sort(X.begin(), X.end()); n   double sum1 = 0, sum2 = 0; n   for (int i = n - n1 - n2; i < n; i++) { n     if (i < n - n1) n       sum2 += X[i]; n     else n       sum1 += X[i]; n   } n   sum1 /= n1; n   sum2 /= n2; n   printf( %.8lf , sum1 + sum2); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long N = 100100, mod = 1e9 + 7, mod2 = 1e9 + 9, mod3 = 998244353, n                 sq = 450, base = 727, lg = 30; n long long n, m, x, y, z, w, ans, t, l[N], r[N]; n int main() { n   ios::sync_with_stdio(0), cin.tie(0), cout.tie(0); n   cin >> n; n   for (int i = 0; i < n; i++) { n     cin >> l[i] >> r[i]; n     if (i == 0) { n       x = l[i]; n       y = r[i]; n       continue; n     } n     x = min(x, l[i]); n     y = max(y, r[i]); n   } n   for (int i = 0; i < n; i++) { n     if (l[i] == x && r[i] == y) return cout << i + 1, 0; n   } n   cout << -1; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, m; n   cin >> n >> m; n   if (m == 0) { n     cout << n <<   n ; n     return 0; n   } n   string hint[m]; n   getline(cin, hint[0]); n   vector<vector<string> > h(m); n   for (int i = 0; i < m; i++) { n     getline(cin, hint[i]); n     stringstream ss(hint[i]); n     string w; n     while (ss >> w) h[i].push_back(w); n   } n   int i; n   int ans = 0; n   for (i = 0; i < n; i++) { n     bool yes = true; n     for (int j = 0; j < m; j++) { n       if (h[j][2] ==  left ) { n         int k = atoi(h[j][4].c_str()) - 1; n         if (i >= k) { n           yes = false; n           break; n         } n       } else if (h[j][2] ==  right ) { n         int k = atoi(h[j][4].c_str()) - 1; n         if (i <= k) { n           yes = false; n           break; n         } n       } n     } n     if (yes) ans++; n   } n   if (ans == 0) ans = -1; n   cout << ans <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int a[200], n, i, s = 0; n   scanf( %d , &n); n   for (i = 0; i < 2 * n - 1; i++) { n     scanf( %d , &a[i]); n     if (a[i] < 0) n       s -= a[i]; n     else n       s += a[i]; n   } n   if (n % 2 == 0) { n     sort(a, a + 2 * n - 1); n     for (i = 0; i < 2 * n - 1; i++) { n       if (a[i] > 0) break; n     } n     if (i % 2 == 1) { n       if (a[i - 1] + a[i] < -a[i - 1] - a[i] || a[i] < 0) n         s -= fabs(2 * a[i]); n       else n         s += 2 * a[i - 1]; n     } n   } n   cout << s << endl; n } n


#include <bits/stdc++.h> n using namespace std; n char s[300005]; n int n, cnt, p[300005], to[600005], nexti[600005]; n int fa[300005], son[300005], dep[300005], len[300005], top[300005], n     dfsx[300005], st[300005]; n void dfs(int x, int lasti) { n   son[x] = 0; n   len[x] = 1; n   for (int i = p[x]; i != -1; i = nexti[i]) n     if (to[i] != lasti) { n       fa[to[i]] = x; n       dep[to[i]] = dep[x] + 1; n       dfs(to[i], x); n       len[x] += len[to[i]]; n       if (!son[x] || len[to[i]] > len[son[x]]) son[x] = to[i]; n     } n } n void DFS(int x, int lasti) { n   st[x] = ++cnt; n   dfsx[cnt] = x; n   if (son[x]) top[son[x]] = top[x], DFS(son[x], x); n   for (int i = p[x]; ~i; i = nexti[i]) n     if (to[i] != lasti && to[i] != son[x]) top[to[i]] = to[i], DFS(to[i], x); n } n int pw[300005], pre[300005], suf[300005]; n inline int gethash(int l, int r) { n   if (l < r) n     return (pre[r] - 1LL * pre[l - 1] * pw[r - l + 1] % 998244353 + 998244353) % n            998244353; n   else n     return (suf[r] - 1LL * suf[l + 1] * pw[l - r + 1] % 998244353 + 998244353) % n            998244353; n } n struct Path { n   int l, r; n   int len() { return abs(r - l) + 1; } n   Path(int l = 0, int r = 0) : l(l), r(r) {} n } x[305], y[305]; n int getpath(int u, int v, Path *path) { n   static Path l[305], r[305]; n   int totl = 0, totr = 0; n   while (top[u] != top[v]) { n     if (dep[top[u]] > dep[top[v]]) { n       l[++totl] = Path(st[u], st[top[u]]); n       u = fa[top[u]]; n     } else { n       r[++totr] = Path(st[top[v]], st[v]); n       v = fa[top[v]]; n     } n   } n   if (dep[u] > dep[v]) n     l[++totl] = Path(st[u], st[v]); n   else n     r[++totr] = Path(st[u], st[v]); n   int tot = 0; n   for (int i = 1; i <= totl; i++) path[++tot] = l[i]; n   for (int i = totr; i >= 1; i--) path[++tot] = r[i]; n   return tot; n } n inline void getlr(Path pa, int len, int &l, int &r) { n   if (pa.l < pa.r) n     l = pa.l, r = l + len - 1; n   else n     l = pa.l, r = l - len + 1; n } n inline void update(Path *pa, int &x, int len) { n   if (pa[x].len() == len) n     pa[x++] = Path(); n   else if (pa[x].l < pa[x].r) n     pa[x].l += len; n   else n     pa[x].l -= len; n } n bool ck(int cntx, int cnty, int len) { n   if (len == 0) return true; n   int l, r; n   getlr(x[cntx], len, l, r); n   int hx = gethash(l, r); n   getlr(y[cnty], len, l, r); n   int hy = gethash(l, r); n   if (hx == hy) n     return true; n   else n     return false; n } n int query(int a, int b, int c, int d) { n   int xn = getpath(a, b, x), yn = getpath(c, d, y); n   int cntx = 1, cnty = 1, ret = 0; n   while (cntx <= xn && cnty <= yn) { n     int len = min(x[cntx].len(), y[cnty].len()); n     if (ck(cntx, cnty, len)) { n       update(x, cntx, len); n       update(y, cnty, len); n       ret += len; n     } else { n       int ans = 0, L = 0, R = len - 1; n       while (L < R) { n         len = (L + R + 1) >> 1; n         if (ck(cntx, cnty, len)) n           L = len; n         else n           R = len - 1; n       } n       return ret + L; n     } n   } n   return ret; n } n int main() { n   scanf( %d%s , &n, s + 1); n   memset(p, -1, sizeof p); n   for (int i = 1; i < n; i++) { n     int u, v; n     scanf( %d%d , &u, &v); n     cnt++, to[cnt] = v, nexti[cnt] = p[u], p[u] = cnt; n     cnt++, to[cnt] = u, nexti[cnt] = p[v], p[v] = cnt; n   } n   dep[1] = 1; n   dfs(1, -1); n   cnt = 0; n   DFS(1, 1); n   pw[0] = 1; n   for (int i = 1; i <= n; i++) pw[i] = 1LL * pw[i - 1] * 29 % 998244353; n   for (int i = 1; i <= n; i++) n     pre[i] = (1LL * pre[i - 1] * 29 + s[dfsx[i]] -  a  + 1) % 998244353; n   for (int i = n; i >= 1; i--) n     suf[i] = (1LL * suf[i + 1] * 29 + s[dfsx[i]] -  a  + 1) % 998244353; n   int q; n   scanf( %d , &q); n   while (q--) { n     int a, b, c, d; n     scanf( %d%d%d%d , &a, &b, &c, &d); n     printf( %d n , query(a, b, c, d)); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int a[200005], b[200005], c[200005]; n int num = 0; n int main() { n   int n; n   cin >> n; n   for (int i = 1; i <= n; i++) { n     cin >> a[i]; n     b[a[i]]++; n   } n   for (int i = 1; i <= n; i++) { n     if (b[i] == 0) c[++num] = i; n   } n   cout << num << endl; n   sort(c + 1, c + 1 + num); n   int tot = 0; n   int hson[200005]; n   memset(hson, 0, sizeof(hson)); n   for (int i = 1; i <= n; i++) { n     if (b[a[i]] > 1) { n       if (c[tot + 1] > a[i] && hson[a[i]] == 0) { n         hson[a[i]]++; n         continue; n       } n       tot++; n       b[a[i]]--; n       a[i] = c[tot]; n     } n   } n   for (int i = 1; i <= n; i++) cout << a[i] <<    ; n   cout << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long int a, b; n stack<long long int> st; n bool dfs(long long int n) { n   if (n > b) { n     return false; n   } n   if (n == b) { n     return true; n   } n   bool a1, a2; n   a1 = dfs(n * 2); n   if (a1) { n     st.push(n * 2); n     return true; n   } n   a2 = dfs((n * 10) + 1); n   if (a2) { n     st.push((n * 10) + 1); n     return true; n   } n   return false; n } n int main() { n   cin >> a >> b; n   if (dfs(a)) { n     cout <<  YES n ; n     cout << st.size() + 1 <<   n ; n     cout << a <<    ; n     while (!st.empty()) { n       cout << st.top() <<    ; n       st.pop(); n     } n   } else { n     cout <<  NO ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long MOD = 1e9 + 7; n void printV(vector<int> &S) { n   for (int s : S) { n     cout << s; n   } n   cout << endl; n } n void solve() { n   int x; n   cin >> x; n   string T; n   cin >> T; n   vector<int> S; n   for (int i = 0; i < (int)T.size(); i++) { n     S.push_back(T[i] -  0 ); n   } n   int l = 0; n   while (x > (int)S.size()) { n     int end = (int)S.size(); n     for (int rp = 0; rp < S[l] - 1; rp++) { n       for (int i = l + 1; i < end; i++) { n         S.push_back(S[i]); n       } n     } n     l++; n   } n   long long len = (long long)S.size() - l - 1; n   for (int i = l; i < x; i++) { n     len *= S[i]; n     len--; n     len %= MOD; n   } n   cout << (len + x + 1 + MOD) % MOD << endl; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   int t; n   cin >> t; n   for (int i = 0; i < t; i++) solve(); n } n


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


#include <bits/stdc++.h> n using namespace std; n const int INF = 0x3f3f3f3f; n const double eps = 1E-8; n const int FFTSZ = 1 << 20; n const double pi = acos(-1.); n struct cp { n   double real, img; n   cp(double _r = 0.0, double _i = 0.0) : real(_r), img(_i) {} n   cp operator+(const cp &c) const { return cp(real + c.real, img + c.img); } n   cp operator-(const cp &c) const { return cp(real - c.real, img - c.img); } n   cp operator*(const cp &c) const { n     return cp(real * c.real - img * c.img, real * c.img + img * c.real); n   } n }; n void fft(cp x[], int n, int op) { n   for (int i = 1, j = 0; i < n; i++) { n     for (int l = n >> 1; (j ^= l) < l; l >>= 1) n       ; n     if (i < j) swap(x[i], x[j]); n   } n   for (int h = 2; h <= n; h <<= 1) { n     cp wn(cos(-op * 2 * pi / h), sin(-op * 2 * pi / h)); n     for (int j = 0; j < n; j += h) { n       cp w(1, 0); n       for (int k = j; k < j + h / 2; k++) { n         cp u = x[k], v = w * x[k + h / 2]; n         x[k] = u + v, x[k + h / 2] = u - v; n         w = w * wn; n       } n     } n   } n   if (op == -1) n     for (int i = 0; i < n; i++) x[i].real /= n, x[i].img /= n; n } n cp x[FFTSZ], y[FFTSZ]; n int convolution(int a[], int lena, int b[], int lenb, int c[]) { n   int len = 1; n   while (len < lena + lenb - 1) len <<= 1; n   for (int i = 0; i < len; i++) { n     if (i < lena) n       x[i] = cp(a[i] * 1.0, 0.0); n     else n       x[i] = cp(0.0, 0.0); n   } n   for (int i = 0; i < len; i++) { n     if (i < lenb) n       y[i] = cp(b[i] * 1.0, 0.0); n     else n       y[i] = cp(0.0, 0.0); n   } n   fft(x, len, 1), fft(y, len, 1); n   for (int i = 0; i < len; i++) x[i] = x[i] * y[i]; n   fft(x, len, -1); n   c[len] = 0; n   for (int i = 0; i < len; i++) c[i] = int(x[i].real + 0.5); n   return lena + lenb - 1; n } n const int N = (1 << 20) + 1; n int a[N], p[N]; n int n, k; n int main() { n   while (~scanf( %d%d , &n, &k)) { n     (memset(a, 0, sizeof(a))), (memset(p, 0, sizeof(p))); n     a[0] = 1; n     int mx = 0; n     for (int i = 1; i <= n; i++) { n       int v; n       scanf( %d , &v); n       p[v] = 1; n       mx = max(mx, v); n     } n     mx++; n     while (k) { n       if (k & 1) convolution(a, mx, p, mx, a); n       mx = convolution(p, mx, p, mx, p); n       k >>= 1; n       for (int i = 0; i < mx; i++) { n         a[i] = !!a[i]; n         p[i] = !!p[i]; n       } n     } n     for (int i = 0; i < mx; i++) n       if (a[i] != 0) printf( %d  , i); n     puts(  ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long int powOfPositive(long long int n) { n   long long int pos = floor(log2(n)); n   return pos; n } n int main() { n   long long int t; n   cin >> t; n   while (t--) { n     long long int n; n     cin >> n; n     vector<long long int> arr(n); n     for (long long int i = 0; i < n; i++) { n       cin >> arr[i]; n     } n     vector<long long int> dp(30, 0); n     long long int ans = 0; n     for (long long int i = 0; i < n; i++) { n       if (arr[i] == 1) { n         dp[0]++; n         continue; n       } n       long long int x = powOfPositive(arr[i]); n       dp[x]++; n     } n     for (long long int i = 0; i < 30; i++) { n       if (dp[i] > 0) { n         ans += ((dp[i]) * (dp[i] - 1)) / 2; n       } n     } n     cout << ans << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n namespace zyt { n const int N = 1e6 + 10, CH = 20; n int f[1 << CH], n; n char s[N]; n bool check(const int a, const int p) { return a & (1 << p); } n int ctoi(const char c) { return c -  a ; } n void get_max(int &a, const int b) { a = max(a, b); } n int work() { n   scanf( %s , s); n   n = strlen(s); n   for (int i = 0; i < n; i++) { n     int tmp = 0; n     for (int j = i; j < n; j++) n       if (check(tmp, ctoi(s[j]))) n         break; n       else n         get_max(f[tmp |= (1 << ctoi(s[j]))], j - i + 1); n   } n   for (int i = 0; i < (1 << CH); i++) n     for (int j = 0; j < CH; j++) n       if (check(i, j)) get_max(f[i], f[i ^ (1 << j)]); n   int ans = 0; n   for (int i = 1; i < (1 << CH); i++) n     get_max(ans, f[i] + f[~i & ((1 << CH) - 1)]); n   printf( %d , ans); n   return 0; n } n }  // namespace zyt n int main() { return zyt::work(); } n


#include <bits/stdc++.h> n using namespace std; n const int nax = 2 * (int)1e5 + 10; n const int INF = (int)1e9 + 47; n vector<int> g[nax]; n int n, k; n int lvl[nax], tin[nax], tout[nax]; n int cnt = 0; n int timer = 0; n int ver[nax]; n int seen[nax]; n int par[nax]; n int vr[nax]; n vector<pair<int, int>> vec; n void dfs(int v, int p = -1) { n   if (p != -1) lvl[v] = lvl[p] + 1; n   vec.push_back({lvl[v], v}); n   par[v] = p; n   if (g[v].size() == 1 && p != -1) cnt++; n   if (p != -1) lvl[v] = lvl[p] + 1; n   for (auto to : g[v]) { n     if (to == p) continue; n     dfs(to, v); n   } n } n long long f(int red, int blue) { n   int w = n - red - blue; n   return w * 1ll * (red - blue); n } n int main() { n   ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); n   cin >> n >> k; n   for (int i = 0; i < n - 1; i++) { n     int a, b; n     cin >> a >> b; n     --a, --b; n     g[a].push_back(b); n     g[b].push_back(a); n   } n   dfs(0); n   sort(vec.begin(), vec.end(), greater<pair<int, int>>()); n   vector<int> vv; n   for (int i = 0; i < n; i++) { n     int cur = vec[i].second; n     int cc = 0; n     while (cur != -1 && !seen[cur]) { n       cc++; n       seen[cur] = 1; n       cur = par[cur]; n     } n     vv.push_back(cc); n   } n   sort(vv.begin(), vv.end(), greater<int>()); n   int blue = n; n   long long best_ans = -(long long)1e18; n   for (int i = 0; i < min(k, cnt); i++) { n     blue -= vv[i]; n     int l = 0, r = blue; n     for (int it = 0; it < 40; it++) { n       int mid = (l + r) / 2; n       if (f(i + 1, mid + 1) <= f(i + 1, mid)) n         l = mid; n       else n         r = mid; n     } n     if ((f(i + 1, r) > f(i + 1, r + 1) && r + 1 < blue) || n         (f(i + 1, r - 1) < f(i + 1, r) && r - 1 >= 0)) n       throw 47; n     best_ans = max(best_ans, f(i + 1, r)); n   } n   while (cnt <= k) { n     best_ans = max(best_ans, (n - cnt) * 1ll * cnt); n     cnt++; n   } n   cout << best_ans <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int T; n   cin >> T; n   while (T--) { n     int n; n     cin >> n; n     vector<long long> v; n     for (int i = 1; i <= n; i++) { n       int x; n       cin >> x; n       v.push_back(x); n     } n     sort(v.begin(), v.end()); n     long long ans = v[0] * v[v.size() - 1]; n     int sum = 0, flag = 1; n     for (long long i = 2; i * i <= ans; i++) { n       if (ans % i == 0) { n         if (find(v.begin(), v.end(), i) == v.end() || n             find(v.begin(), v.end(), ans / i) == v.end()) { n           flag = 0; n           break; n         } n         sum += 2; n         if (i * i == ans) sum--; n       } n     } n     if (sum == n && flag == 1) n       cout << ans << endl; n     else n       cout << -1 << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 1e3 + 10; n const int INF = 99999999; n const double eps = 1e-4; n int dp[MAXN]; n int main() { n   int n, m, c0, d0; n   cin >> n >> m >> c0 >> d0; n   memset(dp, 0, sizeof(dp)); n   for (int i = c0; i <= n; i++) dp[i] = i / c0 * d0; n   for (int i = 1; i <= m; i++) { n     int a, b, c, d; n     cin >> a >> b >> c >> d; n     for (int j = 1; j <= a / b; j++) { n       for (int k = n; k >= c; k--) dp[k] = max(dp[k - c] + d, dp[k]); n     } n   } n   cout << dp[n] << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long nod(long long a, long long b) { n   while (a != 0 && b != 0) { n     if (a > b) n       a %= b; n     else n       b %= a; n   } n   return a + b; n } n int main() { n   long long n, a[100]; n   cin >> n; n   for (int i = 0; i < n; i++) cin >> a[i]; n   sort(a, a + n); n   long long p = nod(a[0], a[1]); n   for (int i = 1; i < n; i++) p = nod(p, a[i]); n   long long max = a[n - 1], s; n   s = ((max - p) / p) + 1; n   if ((s - n) % 2 == 0) n     cout <<  Bob ; n   else n     cout <<  Alice ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 2e5 + 5; n const long long M = (long long)1e9 + 7; n int Int() { n   int x; n   scanf( %d , &x); n   return x; n } n long long Long() { n   long long x; n   scanf( %lld , &x); n   return x; n } n void Char(char *ch) { scanf( %s , ch); } n template <typename t> n inline t abs(t a) { n   if (a >= 0) return a; n   return -a; n } n template <typename T> n inline T modpow(T b, T p, T Mod) { n   T res = 1; n   while (p) { n     if (p & 1) { n       res *= b; n       res %= Mod; n     } n     b *= b; n     b %= Mod; n     p >>= 1; n   } n   return res; n } n template <typename T> n inline T ModInv(T b, T Mod) { n   return modpow(b, Mod - 2, Mod); n } n long long pwr(long long b, long long p) { n   long long res = 1LL; n   while (p) { n     if (p & 1) { n       res *= b; n     } n     b *= b; n     p >>= 1LL; n   } n   return res; n } n template <typename T> n inline T gcd(T a, T b) { n   if (b == 0) return a; n   return gcd(b, a % b); n } n template <typename T> n inline void Int(T &n) { n   n = 0; n   int f = 1; n   register int ch = getchar(); n   for (; !isdigit(ch); ch = getchar()) n     if (ch ==  - ) f = -1; n   for (; isdigit(ch); ch = getchar()) n = (n << 3) + (n << 1) + ch -  0 ; n   n = n * f; n } n template <typename T, typename U> n inline void Int(T &a, U &b) { n   Int(a); n   Int(b); n } n template <typename T, typename U, typename W> n inline void Int(T &a, U &b, W &c) { n   Int(a, b); n   Int(c); n } n template <typename T, typename U, typename W, typename X> n inline void Int(T &a, U &b, W &c, X &d) { n   Int(a, b, c); n   Int(d); n } n vector<int> Unique(vector<int> v) { n   sort(v.begin(), v.end()); n   v.erase(unique(v.begin(), v.end()), v.end()); n   return v; n } n vector<string> split(const string &s, char c) { n   vector<string> v; n   stringstream ss(s); n   string x; n   while (getline(ss, x, c)) v.emplace_back(x); n   return move(v); n } n void err(vector<string>::iterator it) {} n template <typename T, typename... Args> n void err(vector<string>::iterator it, T a, Args... args) { n   cout << it->substr((*it)[0] ==    , it->length()) <<   =   << a <<    ; n   err(++it, args...); n } n int digit(long long n) { n   int s = 0; n   while (n) { n     n /= 10; n     s++; n   } n   return s; n } n long long F(long long n) { n   int len = digit(n); n   if (len <= 1) return n; n   long long res = 0, add = 9; n   for (int i = 1; i < len; i++) { n     if (i == 1) { n       res += 9; n     } else { n       res += add; n       add *= 10; n     } n   } n   long long k = pwr(10, len - 1); n   long long x = n / k; n   long long y = n % 10LL; n   long long p = (n % k) / 10LL; n   if (y >= x) { n     res += (p + 1) + ((x - 1) * (k / 10LL)); n   } else { n     res += p + ((x - 1) * (k / 10LL)); n   } n   return res; n } n int main() { n   long long a = Long(), b = Long(); n   printf( %lld n , F(b) - F(a - 1)); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, m; n   std::cin >> n >> m; n   getchar(); n   int a[n][m], nums[m]; n   for (int i = 0; i < m; i++) { n     nums[i] = 0; n   } n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < m; j++) { n       a[i][j] = getchar() -  0 ; n       nums[j] += a[i][j]; n     } n     getchar(); n   } n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < m; j++) { n       if (a[i][j]) { n         if (nums[j] == 1) goto A; n       } n     } n     std::cout <<  YES ; n     return 0; n   A:; n   } n   printf( NO ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct point { n   long double x, y, s; n   point() {} n   point(int xx, int yy, point o) { n     x = (long double)xx, y = (long double)yy; n     s = atan2(y - o.y, x - o.x); n   } n } o, p; n bool operator<(point _1, point _2) { n   if (_1.s == _2.s) { n     return _1.x < _2.x; n   } n   return _1.s < _2.s; n } n point operator-(point a, point b) { return point(a.x - b.x, a.y - b.y, o); } n long double Cro(point a, point b) { return a.x * b.y - a.y * b.x; } n int px[5], py[5], n; n set<point> s; n set<point>::iterator getpre(set<point>::iterator it) { n   if (it == s.begin()) { n     return --s.end(); n   } n   return --it; n } n set<point>::iterator getnxt(set<point>::iterator it) { n   if ((++it) == s.end()) { n     return s.begin(); n   } n   return it; n } n void Insert(point p) { n   s.insert(p); n   set<point>::iterator tmp = s.find(p); n   set<point>::iterator i = getpre(tmp), j = getpre(i); n   while (Cro(p - (*j), (*i) - (*j)) >= 0) { n     s.erase(i); n     i = j, j = getpre(j); n   } n   i = getnxt(tmp), j = getnxt(i); n   while (Cro(p - (*j), (*i) - (*j)) <= 0) { n     s.erase(i); n     i = j, j = getnxt(j); n   } n   return; n } n int main() { n   { n     n = 0; n     char ch; n     int f = 0; n     do { n       ch = getchar(); n       if (ch ==  - ) { n         f = 1; n       } n     } while (ch <  0  || ch >  9 ); n     while ( 0  <= ch && ch <=  9 ) { n       n = (n << 1) + (n << 3) + ch - 48; n       ch = getchar(); n     } n     if (f) n = -n; n   } n   int op, x, y; n   for (int i = 1; i <= 3; i++) { n     { n       op = 0; n       char ch; n       int f = 0; n       do { n         ch = getchar(); n         if (ch ==  - ) { n           f = 1; n         } n       } while (ch <  0  || ch >  9 ); n       while ( 0  <= ch && ch <=  9 ) { n         op = (op << 1) + (op << 3) + ch - 48; n         ch = getchar(); n       } n       if (f) op = -op; n     } n     { n       px[i] = 0; n       char ch; n       int f = 0; n       do { n         ch = getchar(); n         if (ch ==  - ) { n           f = 1; n         } n       } while (ch <  0  || ch >  9 ); n       while ( 0  <= ch && ch <=  9 ) { n         px[i] = (px[i] << 1) + (px[i] << 3) + ch - 48; n         ch = getchar(); n       } n       if (f) px[i] = -px[i]; n     } n     { n       py[i] = 0; n       char ch; n       int f = 0; n       do { n         ch = getchar(); n         if (ch ==  - ) { n           f = 1; n         } n       } while (ch <  0  || ch >  9 ); n       while ( 0  <= ch && ch <=  9 ) { n         py[i] = (py[i] << 1) + (py[i] << 3) + ch - 48; n         ch = getchar(); n       } n       if (f) py[i] = -py[i]; n     } n     o.x += (long double)px[i], o.y += (long double)py[i]; n   } n   o.x /= 3.0, o.y /= 3.0; n   for (int i = 1; i <= 3; i++) { n     s.insert(point(px[i], py[i], o)); n   } n   for (int i = 4; i <= n; i++) { n     { n       op = 0; n       char ch; n       int f = 0; n       do { n         ch = getchar(); n         if (ch ==  - ) { n           f = 1; n         } n       } while (ch <  0  || ch >  9 ); n       while ( 0  <= ch && ch <=  9 ) { n         op = (op << 1) + (op << 3) + ch - 48; n         ch = getchar(); n       } n       if (f) op = -op; n     } n     { n       x = 0; n       char ch; n       int f = 0; n       do { n         ch = getchar(); n         if (ch ==  - ) { n           f = 1; n         } n       } while (ch <  0  || ch >  9 ); n       while ( 0  <= ch && ch <=  9 ) { n         x = (x << 1) + (x << 3) + ch - 48; n         ch = getchar(); n       } n       if (f) x = -x; n     } n     { n       y = 0; n       char ch; n       int f = 0; n       do { n         ch = getchar(); n         if (ch ==  - ) { n           f = 1; n         } n       } while (ch <  0  || ch >  9 ); n       while ( 0  <= ch && ch <=  9 ) { n         y = (y << 1) + (y << 3) + ch - 48; n         ch = getchar(); n       } n       if (f) y = -y; n     } n     p = point(x, y, o); n     set<point>::iterator nxt = s.lower_bound(p); n     if (nxt == s.end()) { n       nxt = s.begin(); n     } n     set<point>::iterator pre = getpre(nxt); n     if (op == 1) { n       if (Cro(p - (*pre), (*nxt) - p) > 0) { n         Insert(p); n       } n     } else { n       if (Cro(p - (*pre), (*nxt) - p) > 0) { n         puts( NO ); n       } else { n         puts( YES ); n       } n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   double n, l, v1, v2, k; n   cin >> n >> l >> v1 >> v2 >> k; n   double m = ceil(n / k); n   double ans = n       l * v2 / (v1 * v2 - v2 * (v1 * v1 - v2 * v2) / (v1 + (2 * m - 1) * v2)); n   cout << fixed << setprecision(10) << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int ar[5000009]; n long long j; n int finder(vector<pair<long long, long long> > v, long long jump) { n   for (int i = 0; i < v.size(); i++) { n     if (v[i].first > jump) { n       return i; n     } n   } n   return v.size(); n } n long long count = 0; n vector<pair<long long, long long> > v[4]; n void dfs1(long long b, long long p, long long jump) { n   if (v[b].size() == 0) { n     ::count = max(::count, p); n     return; n   } else { n     long long low = finder(v[b], jump); n     if (v[b][0].first > jump) { n       ::count = max(::count, p); n       return; n     } else { n       low--; n       long long u = 0, maxi = v[b][0].second; n       for (int k = 1; k <= low; k++) { n         if (maxi < v[b][k].second) { n           maxi = v[b][k].second; n           u = k; n         } n       } n       jump += v[b][u].second; n       v[b].erase(v[b].begin() + u); n       dfs1((b == 1) ? 0 : ((b == 0) ? 1 : ((b == 2) ? 3 : 2)), p + 1, jump); n     } n   } n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(NULL); n   cout.tie(NULL); n   long long n, jump, a, s, d; n   cin >> n >> jump; n   for (int i = 0; i < n; ++i) { n     cin >> a >> s >> d; n     if (a == 0) { n       v[0].push_back(make_pair(s, d)); n       v[2].push_back(make_pair(s, d)); n     } else { n       v[3].push_back(make_pair(s, d)); n       v[1].push_back(make_pair(s, d)); n     } n   } n   sort(v[0].begin(), v[0].end()); n   sort(v[1].begin(), v[1].end()); n   sort(v[2].begin(), v[2].end()); n   sort(v[3].begin(), v[3].end()); n   dfs1(0, 0, jump); n   dfs1(3, 0, jump); n   cout << ::count; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long int power(long long int x, long long int y) { n   long long int res = 1; n   x = x; n   while (y > 0) { n     if (y & 1) res = (res * x); n     y = y >> 1; n     x = (x * x); n   } n   return res; n } n long long int logtwo(long long int n) { n   if (n == 1) return 0; n   return logtwo(n / 2) + 1; n } n vector<long long int> adj[105][105]; n long long int vis[105][105]; n void dfs(int par, long long int col) { n   for (auto child : adj[par][col]) { n     if (vis[child][col]) continue; n     vis[child][col] = vis[par][col]; n     dfs(child, col); n   } n } n signed main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   int t = 1; n   while (t--) { n     long long int N, M; n     cin >> N >> M; n     for (long long int i = 0; i < M; i++) { n       long long int a, b, c; n       cin >> a >> b >> c; n       a--; n       b--; n       adj[a][c].push_back(b); n       adj[b][c].push_back(a); n     } n     for (long long int i = 0; i < 105; i++) { n       long long int c = 1; n       for (long long int j = 0; j < N; j++) { n         if (vis[j][i]) continue; n         vis[j][i] = c++; n         dfs(j, i); n       } n     } n     long long int Q; n     cin >> Q; n     while (Q--) { n       long long int x, y; n       cin >> x >> y; n       long long int ans = 0; n       x--; n       y--; n       for (long long int i = 0; i < 105; i++) { n         if (vis[x][i] && vis[x][i] == vis[y][i]) ans++; n       } n       cout << ans <<   n ; n     } n   } n   cerr <<  Time :   << 1000 * (long double)clock() / (long double)CLOCKS_PER_SEC n        <<  ms n ; n   ; n } n


#include <bits/stdc++.h> n using namespace std; n int st[200011], disc = 0, mod, en[200011]; n vector<int> adj[200011]; n bitset<1003> m[1000011]; n int flag[1000011], a[200011]; n int p[2011], arr[200011]; n vector<int> pr; n bitset<1003> def; n void dfs(int u, int p) { n   st[u] = disc; n   arr[disc] = u; n   disc++; n   for (int i = 0; i < adj[u].size(); i++) { n     int k = adj[u][i]; n     if (k == p) continue; n     dfs(k, u); n   } n   en[u] = disc - 1; n } n void rotate(std::bitset<1003>& b, unsigned m) { n   b = (((b << m) & def) | (b >> (mod - m))); n } n void build(int n, int b, int e) { n   if (b == e) { n     m[n].set(a[arr[b]]); n     return; n   } n   int l = 2 * n, mid = (b + e) / 2; n   build(l, b, mid); n   build(l + 1, mid + 1, e); n   m[n] = m[l] | m[l + 1]; n } n void update(int n, int b, int e, int x, int y, int v) { n   if (flag[n] != 0) { n     rotate(m[n], flag[n]); n     if (b != e) { n       flag[2 * n] = (flag[2 * n] + flag[n]) % mod; n       flag[2 * n + 1] = (flag[2 * n + 1] + flag[n]) % mod; n     } n     flag[n] = 0; n   } n   if (b > y || e < x) return; n   if (b >= x && e <= y) { n     rotate(m[n], v); n     if (b != e) { n       flag[2 * n] = (flag[2 * n] + v) % mod; n       flag[2 * n + 1] = (flag[2 * n + 1] + v) % mod; n     } n     return; n   } n   int l = 2 * n, mid = (b + e) / 2; n   update(l, b, mid, x, y, v); n   update(l + 1, mid + 1, e, x, y, v); n   m[n] = m[l] | m[l + 1]; n } n bitset<1003> query(int n, int b, int e, int x, int y) { n   if (flag[n] != 0) { n     rotate(m[n], flag[n]); n     if (b != e) { n       flag[2 * n] = (flag[2 * n] + flag[n]) % mod; n       flag[2 * n + 1] = (flag[2 * n + 1] + flag[n]) % mod; n     } n     flag[n] = 0; n   } n   if (b > y || e < x) return 0; n   if (b >= x && e <= y) return m[n]; n   int l = 2 * n, mid = (b + e) / 2; n   return (query(l, b, mid, x, y) | query(l + 1, mid + 1, e, x, y)); n } n int main() { n   int n, i, j, k; n   scanf( %d %d , &n, &mod); n   for (i = 2; i < mod; i++) n     if (p[i] == 0) { n       pr.push_back(i); n       for (j = i + i; j < mod; j += i) p[j] = 1; n     } n   for (i = 1; i <= n; i++) scanf( %d , &a[i]), a[i] %= mod; n   for (i = 0; i < n - 1; i++) { n     int u, v; n     scanf( %d %d , &u, &v); n     adj[u].push_back(v); n     adj[v].push_back(u); n   } n   dfs(1, 0); n   build(1, 0, n - 1); n   int q; n   scanf( %d , &q); n   for (i = 0; i < mod; i++) def.set(i); n   while (q--) { n     int t; n     scanf( %d , &t); n     if (t == 1) { n       int x, v; n       scanf( %d %d , &v, &x); n       x %= mod; n       update(1, 0, n - 1, st[v], en[v], x); n     } else { n       int v, ans = 0; n       scanf( %d , &v); n       bitset<1003> b = query(1, 0, n - 1, st[v], en[v]); n       for (int i = 0; i < pr.size(); i++) n         if (b.test(pr[i])) ans++; n       printf( %d n , ans); n     } n   } n   return 0; n } n


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


#include <bits/stdc++.h> n using namespace std; n int gcd(int p, int q) { return q == 0 ? p : gcd(q, p % q); } n char s[100100]; n long long res[2][2]; n int main() { n   cin >> s; n   int len = strlen(s); n   memset(res, 0, sizeof(res)); n   long long a = 0, b = 0; n   for (int i = 0; i < len; i++) { n     int temp = i & 1; n     res[s[i] -  a ][temp]++; n     a += res[s[i] -  a ][1 - temp]; n     b += res[s[i] -  a ][temp]; n   } n   cout << a <<     << b << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long int n, m; n   cin >> n >> m; n   long long int a[m]; n   for (int i = 0; i < m; i++) { n     cin >> a[i]; n   } n   sort(a, a + m); n   int i = 0; n   while (i < m - 2) { n     if (a[i] + 1 == a[i + 1] && a[i + 1] + 1 == a[i + 2]) { n       cout <<  NO n ; n       return 0; n     } n     i++; n   } n   if (a[0] == 1 || a[m - 1] == n) { n     cout <<  NO n ; n     return 0; n   } n   cout <<  YES n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long double lim = 1e99; n template <typename T> n inline T read() { n   register T sum = 0; n   register char cc = getchar(); n   int sym = 1; n   while (cc !=  -  && (cc >  9  || cc <  0 )) cc = getchar(); n   if (cc ==  - ) sym = -1, cc = getchar(); n   sum = sum * 10 + cc -  0 ; n   cc = getchar(); n   while (cc >=  0  && cc <=  9 ) sum = sum * 10 + cc -  0 , cc = getchar(); n   return sym * sum; n } n template <typename T> n inline T read(T& a) { n   a = read<T>(); n   return a; n } n template <typename T, typename... Others> n inline void read(T& a, Others&... b) { n   a = read(a); n   read(b...); n } n int n, m, K; n long double ans, fac[100010]; n long double C(int n, int m) { return fac[n] - fac[m] - fac[n - m]; } n int main() { n   read(n, m, K); n   for (int i = 1; i <= m; i++) fac[i] = fac[i - 1] + log(1. * i); n   for (int i = 0; i <= n; i++) n     for (int j = 0; j <= n; j++) { n       int num = (i + j) * n - i * j; n       if (num <= K) { n         long double p = C(n, i) + C(n, j) + C(m - num, K - num) - C(m, K); n         ans = min(ans + exp(p), lim); n       } n     } n   printf( %.15Lf n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int Max_N(1050); n int N; n double P[Max_N][Max_N], F[Max_N], G[Max_N], V[Max_N]; n bool done[Max_N]; n int main() { n   scanf( %d , &N); n   for (int i = 1; i <= N; ++i) { n     V[i] = 1.0; n     for (int j = 1; j <= N; ++j) scanf( %lf , P[i] + j), P[i][j] /= 100.0; n     if (i < N) F[i] = 1E100; n   } n   for (int u;;) { n     u = -1; n     for (int x = 1; x <= N; ++x) n       if (!done[x] && (u == -1 || F[x] < F[u])) u = x; n     if (u == -1) break; n     done[u] = true; n     for (int v = 1; v <= N; ++v) n       if (!done[v] && P[v][u] != 0.0) n         G[v] += V[v] * P[v][u] * F[u], V[v] *= (1.0 - P[v][u]), n             F[v] = 1.0 / (1.0 - V[v]) + G[v] / (1.0 - V[v]); n   } n   printf( %.10lf , F[1]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n constexpr int MAX_N = 200000; n int N, M, S, D; n int X[MAX_N + 10]; n int DP[MAX_N + 1] = {0}; n stack<string> OPS; n void run(int x) { n   if (x == 0) return; n   std::ostringstream os; n   os <<  RUN   << x; n   OPS.push(os.str()); n } n void jump(int x) { n   assert(x > 0); n   std::ostringstream os; n   os <<  JUMP   << x; n   OPS.push(os.str()); n } n bool dfs(int p) { n   assert(p >= 0 && p <= N); n   if (DP[p] != 0) { n     assert(DP[p] == 2); n     return false; n   } n   if (M < X[p]) { n     DP[p] = 2; n     return false; n   } n   if (p == N || M < X[p + 1]) { n     run(M - X[p] - 1); n     DP[p] = 1; n   } else { n     DP[p] = 2; n     int space = X[p + 1] - X[p] - 2; n     if (space < S) { n       return false; n     } n     int i = p + 1; n     while (DP[p] != 1 && i <= N && X[i] + 1 <= X[p + 1] - 1 + D) { n       if (dfs(i)) { n         DP[p] = 1; n         jump(X[i] + 1 - X[p + 1] + 1); n         run(space); n       } n       i++; n     } n   } n   return DP[p] == 1; n } n int main() { n   cin >> N >> M >> S >> D; n   X[0] = -1; n   for (int i = 0; i < N; i++) { n     cin >> X[i + 1]; n   } n   sort(&X[0], &X[N + 1]); n   if (!dfs(0)) { n     printf( IMPOSSIBLE n ); n   } else { n     while (!OPS.empty()) { n       cout << OPS.top() << endl; n       OPS.pop(); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long MOD = 1e9 + 7; n long long mpow(long long x, long long y) { n   if (y == 0) return 1; n   if (y % 2) return x * mpow(x, y - 1) % MOD; n   long long h = mpow(x, y / 2); n   return h * h % MOD; n } n long long arr[111111]; n int main() { n   int k; n   scanf( %d , &k); n   for (int i = 0; i < k; i++) scanf( %lld , arr + i); n   long long p = 2; n   for (int i = 0; i < k; i++) p = mpow(p, arr[i]); n   p = p * mpow(2, MOD - 2) % MOD; n   long long q = p; n   if (all_of(arr, arr + k, [&](long long i) { return i % 2; })) n     p = (p - 1 + MOD) % MOD; n   else n     p = (p + 1) % MOD; n   p = p * mpow(3, MOD - 2) % MOD; n   printf( %lld/%lld , p, q); n } n


#include <bits/stdc++.h> n using namespace std; n long long solve() { n   long long m, n, i, j; n   string a, b; n   cin >> m >> n >> a >> b; n   long long t[m + 1][n + 1]; n   memset(t, 0, sizeof(t)); n   long long sol = 0; n   for (i = 1; i <= m; i++) n     for (j = 1; j <= n; j++) { n       if (a[i - 1] == b[j - 1]) n         t[i][j] = 2 + t[i - 1][j - 1]; n       else n         t[i][j] = max(t[i - 1][j], t[i][j - 1]) - 1; n       t[i][j] = max((long long)0, t[i][j]); n       sol = max(sol, t[i][j]); n     } n   return sol; n } n int32_t main() { n   long long t = 1; n   while (t--) cout << solve() << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int read() { n   int nm = 0, f = 1; n   char c = getchar(); n   for (; !isdigit(c); c = getchar()) n     if (c ==  - ) f = -1; n   for (; isdigit(c); c = getchar()) nm = nm * 10 + c -  0 ; n   return nm * f; n } n long long cup[10001], flow[10001], to[10001], head[10001], nxt[10001], n     deep[10001]; n bool vis[10001]; n int n, m, s, t, cnt = 1; n long long ans = 0; n void push(int vi, int vj, long long v) { n   cnt++, to[cnt] = vj, nxt[cnt] = head[vi], head[vi] = cnt, cup[cnt] = v; n   cnt++, to[cnt] = vi, nxt[cnt] = head[vj], head[vj] = cnt; n } n bool bfs(int be, int ed) { n   memset(deep, 0, sizeof(deep)); n   deep[be] = 1; n   queue<int> q; n   q.push(be); n   while (!q.empty()) { n     int now = q.front(); n     q.pop(); n     for (int i = head[now]; i; i = nxt[i]) { n       int vj = to[i]; n       if (flow[i] >= cup[i] || deep[vj]) continue; n       deep[vj] = deep[now] + 1; n       q.push(vj); n       if (vj == ed) return true; n     } n   } n   return false; n } n long long dfs(int now, int ed, long long f) { n   if (f == 0 || now == ed) return f; n   long long tot = 0, x; n   for (int i = head[now]; i; i = nxt[i]) { n     int vj = to[i]; n     if (deep[vj] != deep[now] + 1) continue; n     if (x = dfs(vj, ed, min(f, cup[i] - flow[i]))) { n       flow[i] += x; n       flow[i ^ 1] -= x; n       tot += x; n       f -= x; n     } n     if (f == 0) break; n   } n   if (tot == 0) deep[now] = -1; n   return tot; n } n void Dinic(int be, int ed) { n   while (bfs(be, ed)) ans -= dfs(be, ed, 1ll << 50); n } n int main() { n   n = read(), m = read(); n   s = n + m + 1, t = s + 1; n   for (int i = 1; i <= n; i++) { n     push(i, t, read()); n   } n   for (int i = 1; i <= m; i++) { n     int vi = read(), vj = read(), v = read(); n     ans += v; n     push(s, n + i, v); n     push(n + i, vi, 1ll << 50); n     push(n + i, vj, 1ll << 50); n   } n   Dinic(s, t); n   cout << ans <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 2036000; n const long long INF = 199999999999LL; n int n, m; n long long a[N], b[N]; n long long first, second, mod; n long long w[N], t[N]; n int nn; n void AddA(int &i) { n   w[nn] = a[i++]; n   t[nn++] = 1; n } n void AddB(int &j) { n   w[nn] = b[j++]; n   t[nn++] = 2; n } n int main() { n   cin >> n; n   cin >> n >> a[0] >> first >> second >> mod; n   for (int i = (1); i < (int)(n); i++) a[i] = (a[i - 1] * first + second) % mod; n   cin >> m >> b[0] >> first >> second >> mod; n   for (int j = (1); j < (int)(m); j++) b[j] = (b[j - 1] * first + second) % mod; n   int i = 0, j = 0; n   nn = 0; n   long long lt = -INF; n   while (i < n || j < m) { n     if (i == n) { n       AddB(j); n     } else if (j == m) { n       AddA(i); n     } else { n       bool ga = a[i] >= lt; n       bool gb = b[j] >= lt; n       if (ga ^ gb) { n         if (ga) { n           AddA(i); n         } else { n           AddB(j); n         } n       } else { n         if (a[i] < b[j]) { n           AddA(i); n         } else { n           AddB(j); n         } n       } n     } n     lt = w[nn - 1]; n   } n   int bad = 0; n   for (int k = (1); k < (int)(nn); k++) bad += (w[k] < w[k - 1]); n   cout << bad << endl; n   if (n + m <= 200000) { n     for (int k = (0); k < (int)(nn); k++) cout << w[k] <<     << t[k] << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAX = 1e5 + 5; n const int inf = 1e9 + 1; n const long long OO = 1e18; n long long p1, p2, c1, c2, w1, w2; n long long solve() { n   long long a2, b1, b3, ans = 0; n   for (int i = 0; i <= min(p1 / w1, c1); i++) { n     long long a2 = min((p1 - i * w1) / w2, c2); n     long long b1 = min(p2 / w1, c1 - i); n     long long b2 = min((p2 - b1 * w1) / w2, c2 - a2); n     ans = max(ans, i + a2 + b1 + b2); n   } n   return ans; n } n int main() { n   int t; n   cin >> t; n   while (t--) { n     cin >> p1 >> p2 >> c1 >> c2 >> w1 >> w2; n     if (w1 > w2) { n       swap(w1, w2); n       swap(c1, c2); n     } n     cout << solve() <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, lam, mx = 0, c = 0; n   cin >> n; n   vector<int> cand(n); n   for (int i = 0; i < n; i++) { n     cin >> cand[i]; n     if (mx < cand[i]) mx = cand[i]; n   } n   for (int i = 0; i < n; i++) n     if (mx == cand[i]) c++; n   int x = lam = cand[0]; n   if (lam == mx && c > 1) return cout <<  1 , 0; n   if (lam == mx && c == 1) return cout <<  0 , 0; n   sort(cand.begin(), cand.end()); n   while (lam <= cand[n - 1]) { n     lam += 1; n     cand[n - 1] -= 1; n     sort(cand.begin(), cand.end()); n   } n   cout << lam - x; n } n


#include <bits/stdc++.h> n using namespace std; n int64_t *arry[3]; n int main(void) { n   int64_t n, p, q, r; n   cin >> n >> p >> q >> r; n   for (int i = 0; i < 3; i++) { n     arry[i] = (int64_t *)malloc(sizeof(int64_t) * n); n   } n   for (int i = 0; i < n; i++) { n     int buf; n     cin >> buf; n     arry[0][i] = buf * p; n     arry[1][i] = buf * q; n     arry[2][i] = buf * r; n   } n   { n     int64_t *pt = (int64_t *)malloc(sizeof(int64_t) * n); n     pt[n - 1] = arry[2][n - 1]; n     for (int i = n - 1; i > 0; i--) { n       pt[i - 1] = max(pt[i], arry[2][i - 1]); n     } n     free(arry[2]); n     arry[2] = pt; n   } n   { n     int64_t *pt = (int64_t *)malloc(sizeof(int64_t) * n); n     for (int i = 0; i < n; i++) { n       pt[i] = arry[1][i] + arry[2][i]; n     } n     free(arry[1]); n     arry[1] = pt; n   } n   { n     int64_t *pt = (int64_t *)malloc(sizeof(int64_t) * n); n     pt[0] = *arry[0]; n     for (int i = 1; i < n; i++) { n       pt[i] = max(pt[i - 1], arry[0][i]); n     } n     free(arry[0]); n     arry[0] = pt; n   } n   int64_t max = arry[0][0] + arry[1][0]; n   for (int i = 1; i < n; i++) { n     int64_t buf = arry[0][i] + arry[1][i]; n     if (buf > max) max = buf; n   } n   cout << max << endl; n   for (int i = 0; i < 3; i++) { n     free(arry[i]); n   } n } n


#include <bits/stdc++.h> n int main() { n   int n, w = 0, k = 1, i = 0, t = 0; n   scanf( %d , &n); n   int a[n]; n   for (i = 0; i < n; i++) { n     scanf( %d , &a[i]); n     if (a[i] > t) t = a[i]; n   } n   for (i = 0; i < n; i++) { n     if ((a[i] == t) && (a[i + 1] == t)) n       k++; n     else if ((a[i] == t) && (a[i + 1] != t)) { n       if (k > w) w = k; n       k = 1; n     } n   } n   printf( %d , w); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long int func(long long int n) { n   long long int res = 0; n   while (n != 0) { n     n /= 10; n     res++; n   } n   return res; n } n int main() { n   long long int w, m, k; n   cin >> w >> m >> k; n   long long int n = func(m), tr = w / k, res = 0; n   while (tr > 0) { n     long long int dum = pow(10, n) - m; n     res += fmin(tr / n, dum); n     tr -= dum * n; n     m += dum; n     n++; n   } n   cout << res; n } n


#include <bits/stdc++.h> n #pragma GCC optimize( Ofast ) n #pragma GCC optimization( O3 ) n #pragma GCC optimize( unroll-loops ) n #pragma GCC target( avx,avx2,fma ) n using namespace std; n int main() { n   long double r, x1, y1, x2, y2; n   cin >> r >> x1 >> y1 >> x2 >> y2; n   long double x = sqrt((y1 - y2) * (y1 - y2) + (x1 - x2) * (x1 - x2)); n   if (x - r >= 0) n     cout << fixed << setprecision(10) << x1 <<     << y1 <<     << r; n   else { n     if (x == 0) { n       cout << x1 - r / 2 <<     << y1 <<     << r / 2; n       return 0; n     } n     x1 = x1 + ((x - r) / 2) * (x2 - x1) / x; n     y1 = y1 + ((x - r) / 2) * (y2 - y1) / x; n     cout << fixed << setprecision(10) << x1 <<     << y1 <<     n          << r - (r - x) / 2; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void Rd(int &x) { n   static char c; n   x = 0; n   while ((c = getchar()) < 48) n     ; n   do x = (x << 3) + (x << 1) + (c ^ 48); n   while ((c = getchar()) > 47); n } n int n; n int Next[200004 << 1], V[200004 << 1], W[200004], Head[200004], tot; n void Add_Edge(int u, int v) { Next[++tot] = Head[u], V[Head[u] = tot] = v; } n int Sz[200004], G, Mn, Nowsz; n bool Mark[200004]; n long long Ans, Dis[200004]; n void GetG(int x, int f) { n   int y, mx = 0; n   Sz[x] = 1; n   for (int i = Head[x]; i; i = Next[i]) n     if (!Mark[y = V[i]] && y != f) { n       GetG(y, x); n       Sz[x] += Sz[y]; n       mx = max(mx, Sz[y]); n     } n   mx = max(mx, Nowsz - Sz[x]); n   if (mx < Mn) Mn = mx, G = x; n } n int m; n struct Node { n   int l; n   long long d; n } D[200004]; n long long Calc(int x) { n   long long Tmp[2] = {0, 0}, Cnt[2] = {0, 0}, Res = 0; n   for (int i = (0), i_end_ = (m); i < i_end_; ++i) n     (Tmp[D[i].l & 1] += D[i].d) %= 1000000007, Cnt[D[i].l & 1]++; n   Res -= 2 * Cnt[1] * Tmp[1] + Cnt[1] * Cnt[1] % 1000000007 * W[x]; n   Res += 2 * Cnt[0] * Tmp[0] + Cnt[0] * Cnt[0] % 1000000007 * W[x]; n   return Res % 1000000007; n } n void Gets(int x, int f, int d) { n   D[m++] = (Node){d, Dis[x]}; n   int y; n   Sz[x] = 1; n   for (int i = Head[x]; i; i = Next[i]) n     if (!Mark[y = V[i]] && y != f) { n       Dis[y] = Dis[x] + ((d & 1) ? W[y] : -W[y]); n       Gets(y, x, d + 1); n       Sz[x] += Sz[y]; n     } n } n void Work(int x, int sz) { n   Mn = 200004, Nowsz = sz; n   GetG(x, 0); n   Mark[x = G] = 1; n   m = Dis[x] = 0; n   Gets(x, 0, 0); n   Ans += Calc(x); n   if (sz == 1) return; n   int y; n   for (int i = Head[x]; i; i = Next[i]) n     if (!Mark[y = V[i]]) { n       m = 0; n       Gets(y, x, 1); n       Ans -= Calc(x); n       Work(y, Sz[y]); n     } n } n int main() { n   Rd(n); n   for (int i = (1), i_end_ = (n + 1); i < i_end_; ++i) scanf( %d , &W[i]); n   for (int i = (1), i_end_ = (n); i < i_end_; ++i) { n     int u, v, w; n     Rd(u), Rd(v); n     Add_Edge(u, v); n     Add_Edge(v, u); n   } n   Work(1, n); n   printf( %I64d n , (Ans % 1000000007 + 1000000007) % 1000000007); n   return 0; n } n


#include<bits/stdc++.h> n using namespace std; n typedef long long ll; n const ll MOD =  998244353; n #define rep(i,a,b) for(ll i=a;i<b;++i)  n #define pii pair<int,int> n  n int main() n { n     ios_base::sync_with_stdio(0); n     cin.tie(0); n     int tt=1; n     cin>>tt; n     while(tt--) n     { n         int n; n         cin>>n; n         int a[n]; n         rep(i,0,n) n         cin>>a[i]; n         vector<int> v; n         rep(i,0,n) n         { n             v.push_back(a[i]); n             int p = i+1; n             rep(j,i+1,n) n             { n                 p = j; n                 if(a[j]>a[i]) break; n                 v.push_back(a[j]); n             } n             reverse(v.begin()+i,v.end()); n             i = p-1; n             if(p==n-1 && v.size()==n) i=n; n         } n         for(int i=v.size()-1;i>=0;--i) n         cout<<v[i]<<   ; n         cout<<  n ; n     } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, m, a[120], i, j, res; n   while (scanf( %d %d , &n, &m) == 2) { n     for (i = 1; i <= n; i++) { n       scanf( %d , &a[i]); n     } n     bool flag = true; n     int k = 0; n     while (flag) { n       for (i = 1; i <= n; i++) { n         if (a[i]) { n           if (a[i] <= m) { n             a[i] = 0; n             k++; n             if (k == n) { n               flag = false; n               res = i; n             } n           } else if (a[i] > m) { n             a[i] -= m; n           } n         } n       } n     } n     printf( %d n , res); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long n; n   double r, v, si, fi; n   cin >> n >> r >> v; n   for (int i = 0; i < n; i++) { n     cin >> si >> fi; n     double dist = fi - si; n     double cir = 2 * 3.14159265358979323846 * r; n     double rev = dist / cir; n     rev = floor(rev); n     double time = rev * (cir / v); n     dist -= time * v; n     double left = 0; n     double right = cir / v; n     double x; n     while (right - left > 1e-12) { n       double mid = (left + right) / 2; n       double temp = n           2 * r * sin((mid * 3.14159265358979323846) / (cir / v)) + mid * v; n       if (temp >= dist) { n         right = mid; n       } else n         left = mid; n     } n     time += left; n     printf( %.12f n , time); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 5e5 + 5; n int n, Q, x; n int fa[N], fv[N]; n vector<int> c[N]; n struct nod { n   int i, l, r; n } b; n vector<nod> a[N]; n int pl, pr; n long long px; n long long t[N * 4], lz[N * 4]; n void down(int i) { n   if (lz[i]) { n     t[i + i] += lz[i]; n     lz[i + i] += lz[i]; n     t[i + i + 1] += lz[i]; n     lz[i + i + 1] += lz[i]; n     lz[i] = 0; n   } n } n void add(int i, int x, int y) { n   if (y < pl || x > pr) return; n   if (x >= pl && y <= pr) { n     t[i] += px; n     lz[i] += px; n     return; n   } n   int m = x + y >> 1; n   down(i); n   add(i + i, x, m); n   add(i + i + 1, m + 1, y); n   t[i] = min(t[i + i], t[i + i + 1]); n } n void ft(int i, int x, int y) { n   if (y < pl || x > pr) return; n   if (x >= pl && y <= pr) { n     px = min(px, t[i]); n     return; n   } n   int m = x + y >> 1; n   down(i); n   ft(i + i, x, m); n   ft(i + i + 1, m + 1, y); n } n int p[N], q[N], tp; n long long s[N]; n const long long M = 1e18; n void dg(int x) { n   p[x] = ++tp; n   for (int j = 0, B = c[x].size() - 1; j <= B; j++) { n     int y = c[x][j]; n     s[y] = s[x] + fv[y], dg(y); n   } n   q[x] = tp; n   if (p[x] == q[x]) { n     pl = pr = p[x]; n     px = s[x] - M; n     add(1, 1, n); n   } n } n long long ans[N]; n void dfs(int x) { n   for (int j = 0, B = a[x].size() - 1; j <= B; j++) { n     pl = a[x][j].l, pr = a[x][j].r; n     px = 1e18; n     ft(1, 1, n); n     ans[a[x][j].i] = px; n   } n   for (int j = 0, B = c[x].size() - 1; j <= B; j++) { n     int y = c[x][j]; n     px = -fv[y]; n     pl = p[y]; n     pr = q[y]; n     add(1, 1, n); n     px = fv[y]; n     pl = 1; n     pr = p[y] - 1; n     add(1, 1, n); n     pl = q[y] + 1; n     pr = n; n     add(1, 1, n); n     dfs(y); n     px = -fv[y]; n     pl = q[y] + 1; n     pr = n; n     add(1, 1, n); n     pl = 1; n     pr = p[y] - 1; n     add(1, 1, n); n     px = fv[y]; n     pl = p[y]; n     pr = q[y]; n     add(1, 1, n); n   } n } n int main() { n   scanf( %d %d , &n, &Q); n   for (int i = 1, B = n * 4; i <= B; i++) t[i] = 1e18; n   for (int i = 2, B = n; i <= B; i++) { n     scanf( %d %d , &fa[i], &fv[i]); n     c[fa[i]].push_back(i); n   } n   for (int i = 1, B = Q; i <= B; i++) { n     scanf( %d , &x); n     scanf( %d %d , &b.l, &b.r); n     b.i = i; n     a[x].push_back(b); n   } n   dg(1); n   dfs(1); n   for (int i = 1, B = Q; i <= B; i++) printf( %I64d n , ans[i]); n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int NWarm, NChip; n   cin >> NWarm >> NChip; n   int temp = NWarm * (NWarm + 1) / 2; n   int ans = NChip % temp; n   int i; n   for (i = 1; i <= temp; i++) { n     ans = ans - i; n     if (ans < 0) break; n   } n   int ans1 = ans + i; n   cout << ans1 << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, m; n   cin >> n >> m; n   vector<int> start(m); n   vector<int> ending(m); n   vector<int> time(m); n   vector<int> prize(m); n   int money = 0; n   for (int i = 0; i < m; i++) { n     cin >> start[i] >> ending[i] >> time[i] >> prize[i]; n   } n   int mn = 9000; n   int currprize; n   for (int j = 1; j <= n; j++) { n     mn = 9000; n     currprize = 0; n     for (int i = 0; i < m; i++) { n       if (start[i] <= j && ending[i] >= j && time[i] < mn) { n         mn = time[i]; n         currprize = prize[i]; n       } n     } n     money += currprize; n   } n   cout << money << endl; n } n


#include <bits/stdc++.h> n using namespace std; n const int Maxn = 200005; n const int Maxd = 10; n int bits[1 << Maxd]; n int t; n char str[Maxn]; n int slen; n int main() { n   for (int i = 1; i < 1 << Maxd; i++) bits[i] = 1 + bits[i - (i & -i)]; n   scanf( %d , &t); n   while (t--) { n     scanf( %s , str); n     slen = strlen(str); n     int cur = 0; n     for (int i = 0; i < slen; i++) cur ^= 1 << int(str[i] -  0 ); n     bool solved = false; n     for (int i = slen - 1; i >= 0 && !solved; i--) { n       int my = str[i] -  0 ; n       cur ^= 1 << my; n       for (int j = my - 1; j >= 0 && !solved; j--) { n         cur ^= 1 << j; n         if (i + 1 + bits[cur] <= slen) { n           solved = true; n           str[i] = j +  0 ; n           int pnt = i + 1; n           int lft = slen - (i + 1 + bits[cur]); n           while (lft--) str[pnt++] =  9 ; n           for (int l = Maxd - 1; l >= 0; l--) n             if (cur & 1 << l) str[pnt++] =  0  + l; n         } n         cur ^= 1 << j; n       } n     } n     int pnt = 0; n     while (str[pnt] ==  0 ) pnt++; n     for (int i = pnt; i < slen - pnt; i++) printf( %c , str[i]); n     printf(  n ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MOD = 1e9 + 7; n int n; n vector<int> v, lm, rm; n class SegmentTreeMax { n  public: n   int n; n   vector<long long> vect; n   SegmentTreeMax(int _n) : n(_n) { n     vect.resize(2 * n); n     fill(vect.begin(), vect.end(), 0); n   } n   void modify(int p, long long value) { n     for (vect[p += n] = value; p > 1; p >>= 1) { n       int val = rm[vect[p]] > rm[vect[p ^ 1]] ? vect[p] : vect[p ^ 1]; n       vect[p >> 1] = val; n     } n   } n   long long query(int l, int r) { n     long long res = 0; n     for (l += n, r += n; l < r; l >>= 1, r >>= 1) { n       if (l & 1) { n         if (rm[res] < rm[vect[l]]) res = vect[l]; n         l++; n       } n       if (r & 1) { n         if (rm[res] < rm[vect[r - 1]]) res = vect[r - 1]; n         r--; n       } n     } n     return res; n   } n }; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cin >> n; n   v.resize(n + 1); n   lm.resize(n + 1); n   rm.resize(n + 1); n   vector<int> v2; n   for (int i = 1; i <= n; i++) { n     cin >> v[i]; n     v2.push_back(v[i]); n   } n   v2.push_back(0); n   sort(v2.begin(), v2.end()); n   auto it = unique(v2.begin(), v2.end()); n   v2.resize(it - v2.begin()); n   int l = 1, r = 1; n   vector<int> ans; n   SegmentTreeMax stm(n + 1); n   vector<int> visited, ct(n + 1); n   vector<vector<int>> updates(n + 1); n   while (r <= n) { n     int id = lower_bound(v2.begin(), v2.end(), v[r]) - v2.begin(); n     if (!lm[id]) lm[id] = r; n     int val = stm.query(l, lm[id]); n     updates[id].push_back(r); n     rm[id] = r; n     ct[id]++; n     visited.push_back(id); n     if (rm[val] > lm[id] || ct[id] == 4) { n       if (ct[id] == 4) { n         for (int i = 0; i < 4; i++) ans.push_back(v[r]); n       } else { n         ans.push_back(v2[val]); n         ans.push_back(v[r]); n         ans.push_back(v2[val]); n         ans.push_back(v[r]); n       } n       for (int i : visited) { n         lm[i] = 0; n         rm[i] = 0; n         ct[i] = 0; n         updates[i].clear(); n       } n       visited.clear(); n       l = r + 1; n       r = l; n     } else { n       for (int i : updates[id]) { n         stm.modify(i, id); n       } n       r++; n     } n   } n   cout << ans.size() <<   n ; n   for (int i : ans) { n     cout << i <<    ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T> n bool get_int(T &x) { n   char t = getchar(); n   bool neg = false; n   x = 0; n   for (; (t >  9  || t <  0 ) && t !=  -  && t != EOF; t = getchar()) n     ; n   if (t ==  - ) neg = true, t = getchar(); n   if (t == EOF) return false; n   for (; t <=  9  && t >=  0 ; t = getchar()) x = x * 10 + t -  0 ; n   if (neg) x = -x; n   return true; n } n template <typename T> n void print_int(T x) { n   if (x < 0) putchar( - ), x = -x; n   short a[20] = {}, sz = 0; n   while (x > 0) a[sz++] = x % 10, x /= 10; n   if (sz == 0) putchar( 0 ); n   for (int i = sz - 1; i >= 0; i--) putchar( 0  + a[i]); n } n const int inf = 0x3f3f3f3f; n const long long Linf = 1ll << 61; n const double pi = acos(-1.0); n const int mod = 1e9 + 7; n const int maxn = 100111; n int qpow(int x, int y) { n   int ret = 1; n   while (y) { n     if (y & 1) ret = (long long)ret * x % mod; n     x = (long long)x * x % mod; n     y >>= 1; n   } n   return ret; n } n int n, k, p[maxn]; n long long cnt[maxn]; n vector<int> divs[maxn * 2]; n int main() { n   (get_int(n) && get_int(k)); n   k = qpow(k, mod - 2); n   p[0] = 1; n   for (int i = 1; i <= n; i++) p[i] = (long long)p[i - 1] * k % mod; n   for (int i = 1; i < n; i++) n     for (int j = i; j < n * 2; j += i) divs[j].push_back(i); n   for (int s = 1; s < n + n; s++) { n     reverse(divs[s].begin(), divs[s].end()); n     vector<int> ansv; n     for (auto g : divs[s]) { n       int lb = g, rb = (n - 1) / g * g; n       lb = max(lb, s - rb); n       rb = min(rb, s - lb); n       if (lb > rb) n         ansv.push_back(0); n       else n         ansv.push_back((rb - lb) / g + 1); n     } n     for (int i = 0; i < (int)divs[s].size(); i++) { n       for (int j = 0; j < i; j++) n         if (divs[s][j] % divs[s][i] == 0) ansv[i] -= ansv[j]; n       int g = divs[s][i]; n       if (s - g <= n) n         cnt[n - g] += ansv[i]; n       else n         cnt[n + n - s] += ansv[i]; n     } n   } n   int ans = 0; n   for (int i = 1; i <= n; i++) ans = (ans + cnt[i] % mod * p[i]) % mod; n   printf( %d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int N, M; n int A[104][104], B[104][104]; n int main() { n   scanf( %d %d , &N, &M); n   for (int i = 1; i <= N; ++i) n     for (int j = 1; j <= M; ++j) { n       char c; n       scanf(  %c , &c); n       if (c ==  * ) A[i][j] = 1; n     } n   int cnt = 0; n   for (int i = 1; i <= N; ++i) n     for (int j = 1; j <= M; ++j) n       if (A[i][j]) { n         int x; n         for (x = 0;; ++x) { n           if (!A[i - x][j] || !A[i + x][j] || !A[i][j - x] || !A[i][j + x]) n             break; n         } n         --x; n         if (x) { n           ++cnt; n           for (int k = 0; k <= x; ++k) { n             B[i - k][j] = B[i + k][j] = B[i][j - k] = B[i][j + k] = 1; n           } n         } n       } n   for (int i = 1; i <= N; ++i) n     for (int j = 1; j <= M; ++j) n       if (A[i][j] != B[i][j]) { n         puts( -1 ); n         return 0; n       } n   printf( %d n , cnt); n   for (int i = 1; i <= N; ++i) n     for (int j = 1; j <= M; ++j) n       if (A[i][j]) { n         int x; n         for (x = 0;; ++x) { n           if (!A[i - x][j] || !A[i + x][j] || !A[i][j - x] || !A[i][j + x]) n             break; n         } n         --x; n         if (x) printf( %d %d %d n , i, j, x); n       } n   return 0; n } n


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


#include <bits/stdc++.h> n using namespace std; n int main() { n   int a, b; n   scanf( %d%d , &a, &b); n   if (b > a) n     printf( -1 n ); n   else { n     double ans, a1, a2; n     a1 = (a + b) / (2.0 * ((a + b) / (2 * b))); n     a2 = (a - b) / (2.0 * ((a - b) / (2 * b))); n     printf( %.12lf n , min(a1, a2)); n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   cin >> n; n   int arr[201]; n   for (int i = 1; i <= n; ++i) cin >> arr[i]; n   int brr[201] = {0}, count = 0; n   for (int i = 1; i <= n; ++i) { n     if (i == 1 && n >= 2) { n       if (arr[1] == 1 && arr[2] == 2) brr[i] = 1; n     } else { n       if (i == n && n >= 2) { n         if (arr[n] == 1000 && arr[n - 1] == 999) brr[i] = 1; n       } else { n         if (arr[i - 1] == arr[i] - 1 && arr[i + 1] == arr[i] + 1) brr[i] = 1; n       } n     } n   } n   int t = 0; n   for (int i = 1; i <= n;) { n     while (i <= n && brr[i] == 1) { n       t++; n       i++; n     } n     while (i <= n && brr[i] == 0) { n       i++; n     } n     count = max(count, t); n     t = 0; n   } n   cout << count << endl; n } n


#include <bits/stdc++.h> n using namespace std; n double esp = 1e-11; n const double PI = acos(-1.0); n const long long int INF = 0x3f3f3f3f; n const long long int MOD = 1e9 + 7; n const int maxn = 200010; n int main() { n   int a, b, c; n   scanf( %d%d%d , &a, &b, &c); n   long double x = (-b + sqrt((long double)b * b - 4 * a * c)) / (2 * a); n   long double y = (-b - sqrt((long double)b * b - 4 * a * c)) / (2 * a); n   if (x < y) swap(x, y); n   printf( %.6f n%.6f n , (double)x, (double)y); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MOD = 1000000007; n const int NMAX = 300010; n const double E = 1e-8; n const double PI = acos(-1); n int n, m, stest; n int d; n set<long long> St; n long long st[NMAX], h[NMAX]; n pair<int, int> Tree[4 * NMAX]; n int dp[NMAX], trace[NMAX]; n pair<int, int> cmp(pair<int, int> a, pair<int, int> b) { n   if (a.first > b.first) n     return a; n   else n     return b; n } n void update(int i, int lo, int hi, int u, int x, int vt) { n   if (lo == hi) { n     Tree[i] = cmp(Tree[i], pair<int, int>(x, vt)); n     return; n   } n   int mid = (lo + hi) >> 1; n   if (u <= mid) n     update(2 * i, lo, mid, u, x, vt); n   else n     update(2 * i + 1, mid + 1, hi, u, x, vt); n   Tree[i] = cmp(Tree[2 * i], Tree[2 * i + 1]); n } n pair<int, int> query(int i, int lo, int hi, int u, int v) { n   if (v < lo || hi < u) return pair<int, int>(0, 0); n   if (u <= lo && hi <= v) return Tree[i]; n   int mid = (lo + hi) >> 1; n   return cmp(query(2 * i, lo, mid, u, v), query(2 * i + 1, mid + 1, hi, u, v)); n } n void truy(int x) { n   if (x == 0) return; n   truy(trace[x]); n   cout << x <<    ; n } n int main() { n   ios::sync_with_stdio(false); n   cin >> n >> d; n   for (int i = 1; i <= n; i++) { n     cin >> h[i]; n     St.insert(h[i]); n     St.insert(h[i] - d); n     St.insert(h[i] + d); n   } n   for (set<long long>::iterator it = St.begin(); it != St.end(); it++) n     st[++m] = *it; n   int ans = 0, vt = 0; n   for (int i = 1; i <= n; i++) { n     int pos = lower_bound(st + 1, st + m + 1, h[i]) - st; n     int u = lower_bound(st + 1, st + m + 1, h[i] - d) - st; n     int v = lower_bound(st + 1, st + m + 1, h[i] + d) - st; n     pair<int, int> res = cmp(query(1, 1, m, 1, u), query(1, 1, m, v, m)); n     dp[i] = res.first + 1; n     trace[i] = res.second; n     update(1, 1, m, pos, dp[i], i); n     if (dp[i] > ans) { n       ans = dp[i]; n       vt = i; n     } n   } n   cout << ans << endl; n   truy(vt); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n using ll = long long; n const int MXN = 3e5 + 5; n const int MOD = 998244353; n const int LOG = 19; n int add(int x, int y) { return (x += y) < MOD ? x : x - MOD; } n int sub(int x, int y) { return (x -= y) >= 00 ? x : x + MOD; } n int mul(int x, int y) { return (ll)x * y % MOD; } n int pwr(int x, int p) { n   if (p == 0) return 1; n   if (p % 2) return mul(x, pwr(x, p - 1)); n   return pwr(mul(x, x), p / 2); n } n int inv(int x) { return pwr(x, MOD - 2); } n int sol = 1; n int ok[MXN]; n int val[MXN]; n int dep[MXN]; n int lca[MXN]; n int nxt[MXN]; n int dp[MXN][2]; n int anc[MXN][LOG]; n vector<int> adj[MXN]; n int getlca(int x, int y) { n   if (dep[x] < dep[y]) swap(x, y); n   int diff = dep[x] - dep[y]; n   for (int i = 0; i < LOG; ++i) { n     if (diff >> i & 1) x = anc[x][i]; n   } n   if (x == y) return x; n   for (int i = LOG - 1; i >= 0; --i) { n     if (anc[x][i] != anc[y][i]) { n       x = anc[x][i]; n       y = anc[y][i]; n     } n   } n   return anc[x][0]; n } n void dfs1(int x, int p) { n   if (val[x]) { n     if (lca[val[x]]) { n       lca[val[x]] = getlca(lca[val[x]], x); n     } else { n       lca[val[x]] = x; n     } n   } n   for (int y : adj[x]) { n     if (y == p) continue; n     anc[y][0] = x; n     dep[y] = dep[x] + 1; n     for (int i = 0; i < LOG - 1; ++i) { n       anc[y][i + 1] = anc[anc[y][i]][i]; n     } n     dfs1(y, x); n   } n } n void dfs2(int x, int p) { n   vector<int> vec; n   for (int y : adj[x]) { n     if (y == p) continue; n     dfs2(y, x); n     if (ok[y]) { n       ok[x] = 1; n       vec.push_back(y); n     } n     if (val[y] && y != lca[val[y]]) { n       if (val[x] && val[x] != val[y]) { n         cout <<  0 n ; n         exit(0); n       } n       val[x] = val[y]; n     } n   } n   if (val[x]) { n     dp[x][0] = ok[x] = 1; n     for (int y : vec) { n       sol = mul(sol, add(mul(dp[y][0], nxt[y] + 1), dp[y][1])); n     } n   } else if (vec.size() > 1) { n     dp[x][1] = 1; n     for (int y : vec) { n       int tmp = add(dp[y][1], mul(dp[y][0], nxt[y] + 1)); n       dp[x][1] = mul(dp[x][1], tmp); n     } n     for (int y : vec) { n       int tmp = add(dp[y][1], mul(dp[y][0], nxt[y] + 1)); n       dp[x][0] = add(dp[x][0], mul(mul(dp[x][1], inv(tmp)), dp[y][0])); n     } n   } else if (vec.size() == 1) { n     dp[x][0] = dp[vec[0]][0]; n     dp[x][1] = dp[vec[0]][1]; n     nxt[x] = nxt[vec[0]] + 1; n   } n } n int main() { n   ios::sync_with_stdio(0), cin.tie(0); n   int n, k; n   cin >> n >> k; n   for (int i = 1; i <= n; ++i) { n     cin >> val[i]; n   } n   for (int i = 2; i <= n; ++i) { n     int x, y; n     cin >> x >> y; n     adj[x].push_back(y); n     adj[y].push_back(x); n   } n   dfs1(1, 0); n   dfs2(1, 0); n   if (dp[1][0]) sol = mul(sol, dp[1][0]); n   cout << sol <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n int l[100000 + 9], r[100000 + 9]; n int n, p; n int main() { n   cin >> n >> p; n   for (int i = 0; i < n; i++) { n     cin >> l[i] >> r[i]; n   } n   double ans = 0; n   for (int i = 0; i < n; i++) { n     int j = (i + 1) % n; n     int div1 = r[i] / p - (l[i] - 1) / p; n     int nondiv1 = r[i] - l[i] + 1 - div1; n     int div2 = r[j] / p - (l[j] - 1) / p; n     int nondiv2 = r[j] - l[j] + 1 - div2; n     double nondivs = 1.0 - ((double)nondiv1 / double(r[i] - l[i] + 1)) * n                                ((double)nondiv2 / double(r[j] - l[j] + 1)); n     ans += nondivs * 2000.0; n   } n   cout << fixed << setprecision(15) << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main(int argc, char *argv[]) { n   ios::sync_with_stdio(false); n   cin.tie(nullptr); n   string s, t; n   cin >> s >> t; n   int ns = s.size() - 1; n   int nt = t.size() - 1; n   bool stop = true; n   while (stop && (ns >= 0 && nt >= 0)) { n     if (s[ns] != t[nt]) { n       stop = false; n     } n     if (stop) { n       ns--; n       nt--; n     } n   } n   int r = ns + nt + 2; n   cout << r; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int inf = 1e9; n const long long inf64 = 1e18; n const int mod = inf + 7; n inline int _sum(int a, int b) { return a + b < mod ? a + b : a + b - mod; } n inline int _mul(int a, int b) { return (1ll * a * b) % mod; } n inline int _sub(int a, int b) { return a >= b ? a - b : a - b + mod; } n inline void _inc(int& x, int y) { n   if ((x += y) >= mod) { n     x -= mod; n   } n } n inline void _dec(int& x, int y) { n   if ((x -= y) < 0) { n     x += mod; n   } n } n inline int _binpow(int x, int pw) { n   int res = 1; n   int tmp = x; n   while (pw > 0) { n     if (pw & 1) { n       res = _mul(res, tmp); n     } n     tmp = _mul(tmp, tmp); n     pw >>= 1; n   } n   return res; n } n inline int _div(int a, int b) { return _mul(a, _binpow(b, mod - 2)); } n void read(string& s) { cin >> s; } n void gen(string& s) { n   mt19937 rnd(chrono::high_resolution_clock::now().time_since_epoch().count()); n   int n = 2 * (rnd() % 3 + 1); n   s.resize(n,  ? ); n   for (int i = 0; i < n; i++) { n     s[i] = rnd() % 5 == 0 ?  a  :  b ; n   } n } n string join_halfs(string s) { n   int n = (int)s.size(); n   string t; n   for (int i = 0; i < n / 2; i++) { n     t.push_back(s[i]); n     t.push_back(s[n - 1 - i]); n   } n   return t; n } n int slow(string s) { n   s = join_halfs(s); n   int n = (int)s.size(); n   s =  #  + s; n   vector<int> dp(n + 1); n   dp[0] = 1; n   cout << s <<   n ; n   for (int i = 1; i <= n; i++) { n     for (int j = i; j >= 1; j--) { n       if ((i - j + 1) % 2) continue; n       string hlp = s.substr(j, i - j + 1); n       string rhlp = hlp; n       reverse(rhlp.begin(), rhlp.end()); n       if (hlp == rhlp) { n         _inc(dp[i], dp[j - 1]); n       } n     } n   } n   return dp[n]; n } n const int N = 2e6 + 5; n struct Node { n   map<int, int> nxt; n   int link = -1; n   int dp = 0; n   int slink = -1; n   int len = 0; n   int diff = 0; n   Node() = default; n }; n Node t[N]; n int sz, max_suff; n int fast(string s) { n   { n     sz = 0; n     t[sz++] = Node(); n     t[sz++] = Node(); n     max_suff = 1; n     t[0].link = 1; n     t[1].link = 0; n     t[0].len = -1; n     t[1].len = 0; n   } n   int n = (int)s.size(); n   s =  #  + join_halfs(s); n   vector<int> dp(n + 1); n   dp[0] = 1; n   max_suff = 1; n   for (int i = 1; i <= n; i++) { n     { n       while (s[i] != s[i - t[max_suff].len - 1]) { n         max_suff = t[max_suff].link; n       } n       if (!t[max_suff].nxt.count(s[i])) { n         t[sz++] = Node(); n         t[sz - 1].len = t[max_suff].len + 2; n         int next_max_suff = t[max_suff].link; n         while (s[i] != s[i - t[next_max_suff].len - 1]) { n           next_max_suff = t[next_max_suff].link; n         } n         t[sz - 1].link = n             t[next_max_suff].nxt.count(s[i]) ? t[next_max_suff].nxt[s[i]] : 1; n         t[sz - 1].diff = t[sz - 1].len - t[t[sz - 1].link].len; n         if (t[sz - 1].diff == t[t[sz - 1].link].diff) { n           t[sz - 1].slink = t[t[sz - 1].link].slink; n         } else { n           t[sz - 1].slink = t[sz - 1].link; n         } n         t[max_suff].nxt[s[i]] = sz - 1; n       } n       max_suff = t[max_suff].nxt[s[i]]; n     } n     for (int x = max_suff; t[x].len > 0; x = t[x].slink) { n       { n         int j = i - t[t[x].slink].len - t[x].diff; n         t[x].dp = dp[j]; n       } n       if (t[x].diff == t[t[x].link].diff) { n         _inc(t[x].dp, t[t[x].link].dp); n       } n       if (i % 2 == 0) { n         _inc(dp[i], t[x].dp); n       } n     } n   } n   return dp[n]; n } n void WORK() { n   string s; n   read(s); n   cout << fast(s) <<   n ; n } n void TEST() { n   while (1) { n     string s; n     gen(s); n     cout <<  s =   << s << endl; n     int sl = slow(s); n     int fs = fast(s); n     if (sl == fs) { n       cout <<  OK (  << fs <<  )  << endl; n     } else { n       cout <<  WA  << endl; n       cout <<  exp =   << sl << endl; n       cout <<  fnd =   << fs << endl; n       break; n     } n   } n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   WORK(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long maxm = 3e5 + 5; n const long long mod = 998244353; n long long fac[maxm], inv[maxm]; n long long l[maxm], r[maxm]; n pair<long long, long long> p[25]; n long long sum[maxm][45]; n long long num[maxm]; n long long n, m; n long long ppow(long long a, long long b, long long mod) { n   long long ans = 1 % mod; n   a %= mod; n   while (b) { n     if (b & 1) ans = ans * a % mod; n     a = a * a % mod; n     b >>= 1; n   } n   return ans; n } n void init() { n   fac[0] = 1; n   for (long long i = 1; i < maxm; i++) fac[i] = fac[i - 1] * i % mod; n   inv[maxm - 1] = ppow(fac[maxm - 1], mod - 2, mod); n   for (long long i = maxm - 2; i >= 0; i--) inv[i] = inv[i + 1] * (i + 1) % mod; n } n long long cal(long long x) { n   long long ans = 0; n   while (x) { n     ans++; n     x &= (x - 1); n   } n   return ans; n } n long long C(long long n, long long m) { n   if (m < 0 || m > n) return 0; n   return fac[n] * inv[m] % mod * inv[n - m] % mod; n } n signed main() { n   ios::sync_with_stdio(0); n   init(); n   cin >> n >> m; n   for (long long i = 1; i <= n; i++) { n     cin >> l[i] >> r[i]; n   } n   for (long long i = 0; i < m; i++) { n     cin >> p[i].first >> p[i].second; n   } n   for (long long i = 1; i <= n; i++) { n     num[l[i]]++; n     num[r[i] + 1]--; n   } n   for (long long i = 1; i <= n; i++) { n     num[i] += num[i - 1]; n   } n   for (long long i = 1; i <= n; i++) { n     for (long long j = 0; j <= m * 2; j++) { n       sum[i][j] = (sum[i - 1][j] + C(num[i] - j, i - j)) % mod; n     } n   } n   long long ans = 0; n   for (long long i = 0; i < (1 << m); i++) { n     set<long long> s; n     for (long long j = 0; j < m; j++) { n       if (i >> j & 1) { n         s.insert(p[j].first); n         s.insert(p[j].second); n       } n     } n     long long tot = s.size(); n     long long L = 1, R = n; n     for (auto x : s) { n       L = max(L, l[x]); n       R = min(R, r[x]); n     } n     if (L > R) continue; n     long long t = (sum[R][tot] - sum[L - 1][tot] + mod) % mod; n     if (cal(i) & 1) { n       ans = (ans - t + mod) % mod; n     } else { n       ans = (ans + t) % mod; n     } n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 5, R = 1e4; n int n, l[N], r[N]; n long long cnt[R + 1]; n double all; n bool used[N]; n long long f(int cur, int c, int mn, int x = N) { n   if (cur == n) { n     if (mn > 0) { n       return 0; n     } n     long long cnt = 1; n     for (int i = 0; i < n; i++) { n       if (!used[i] && i != x) { n         cnt *= max(0, min(c, r[i] + 1) - l[i]); n       } n     } n     return cnt; n   } n   long long cnt = f(cur + 1, c, mn, x); n   if (cur != x && l[cur] <= c && c <= r[cur]) { n     used[cur] = true; n     cnt += f(cur + 1, c, mn - 1, x); n     used[cur] = false; n   } n   return cnt; n } n int main() { n   scanf( %d , &n); n   for (int i = 0; i < n; i++) { n     scanf( %d%d , l + i, r + i); n   } n   for (int cur = 0; cur <= R; cur++) { n     long long sum = 0; n     for (int i = 0; i < n; i++) { n       sum += f(0, cur, 1, i) * max(0, r[i] - max(cur, l[i] - 1)); n     } n     sum += f(0, cur, 2); n     all += sum; n     cnt[cur] = sum; n   } n   double ans = 0; n   for (int i = 0; i <= R; i++) { n     ans += cnt[i] / all * i; n   } n   printf( %.10f , ans); n } n


#include <bits/stdc++.h> n using namespace std; n int i, n, k, x, ans, c; n pair<int, int> a[100005]; n int main() { n   cin >> n >> k; n   for (i = 0; i < n; i++) { n     cin >> x; n     a[i].first = 10 - x % 10; n     a[i].second = x; n   } n   sort(a, a + n); n   for (i = 0; i < n; i++) { n     if (a[i].second < 100 && (k - (10 - a[i].second % 10) >= 0)) { n       a[i].second += 10 - a[i].second % 10; n       k -= a[i].first; n     } else { n       break; n     } n     if (i == n - 1) { n       break; n     } n   } n   for (i = 0; i < n; i++) { n     ans += a[i].second / 10; n   } n   cout << min(n * 10, ans + k / 10); n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, m, h; n   cin >> n >> m >> h; n   int f[m]; n   int l[n]; n   int t[n][m]; n   int ans[n][m]; n   for (int i = 0; i < m; i++) cin >> f[i]; n   for (int i = 0; i < n; i++) cin >> l[i]; n   for (int i = 0; i < n; i++) n     for (int j = 0; j < m; j++) { n       cin >> t[i][j]; n       if (t[i][j] == 1) { n         ans[i][j] = 1; n       } n     } n   memset(ans, 0, sizeof(ans)); n   for (int i = 0; i < m; i++) { n     int h1 = f[i]; n     for (int j = 0; j < n; j++) { n       if (t[j][i] == 1 && l[j] >= h1) { n         ans[j][i] = h1; n       } n     } n   } n   for (int i = 0; i < n; i++) { n     int h1 = l[i]; n     for (int j = 0; j < m; j++) { n       if (t[i][j] == 1 && f[j] >= h1) { n         ans[i][j] = h1; n       } n     } n   } n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < m; j++) { n       cout << ans[i][j] <<    ; n     } n     cout << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, i, now, t, mx, gcd, m, a[107]; n   scanf( %d , &n); n   for (i = 0; i ^ n; i++) { n     scanf( %d , a + i); n   } n   sort(a, a + n); n   mx = gcd = a[0]; n   m = 1; n   for (i = 1; i ^ n; i++) { n     if (a[i - 1] != a[i]) { n       mx = mx < a[i] ? a[i] : mx; n       m++; n       now = a[i]; n       while (gcd) { n         t = now; n         now = gcd; n         gcd = t % gcd; n       } n       gcd = now; n     } n   } n   mx /= gcd; n   mx -= m; n   printf(mx & 1 ?  Alice n  :  Bob n ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int a[100009]; n int b[100009]; n int main() { n   int n; n   cin >> n; n   map<int, int> mp; n   for (int i = 0; i < n; i++) { n     cin >> a[i]; n     mp[a[i]]++; n   } n   for (int j = 0; j < n; j++) { n     cin >> b[j]; n     mp[b[j]]++; n   } n   int c = 0; n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < n; j++) { n       if (mp.find(a[i] xor b[j]) != mp.end()) c++; n     } n   } n   if (c & 1) { n     cout <<  Koyomi n ; n   } else { n     cout <<  Karen n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n signed main() { n   int n; n   cin >> n; n   int ar[n]; n   for (int i = 0; i < n; i++) cin >> ar[i]; n   int max_n = pow(2, n); n   bool ans = false; n   for (int i = 0; i < max_n; i++) { n     int temp = i; n     int val = 0; n     for (int j = 0; j < n; j++) { n       if (temp & 1) { n         val = (val + ar[j]) % 360; n       } else { n         val = (val - ar[j] + 360) % 360; n       } n       temp = temp >> 1; n     } n     if (val == 0) { n       ans = true; n       break; n     } n   } n   if (ans) { n     cout <<  YES n ; n   } else { n     cout <<  NO n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n #pragma GCC optimize(2) n using namespace std; n const int INF = 0x3f3f3f3f; n const int GRAPH_SIZE = 20010; n int pin[GRAPH_SIZE], dep[GRAPH_SIZE], s = 0, t = GRAPH_SIZE - 1; n struct EDGE { n   int u, v, c; n }; n vector<EDGE> e; n vector<int> each[GRAPH_SIZE]; n bool bfs() { n   queue<int> Q; n   Q.push(s); n   while (!Q.empty()) { n     int now = Q.front(); n     Q.pop(); n     for (auto it : each[now]) { n       int next = e[it].v; n       if (e[it].c) n         if (dep[next] > dep[now] + 1) { n           dep[next] = dep[now] + 1; n           Q.push(next); n         } n     } n   } n   return dep[t] != INF; n } n int dfs(int now, int flow) { n   if (now == t) { n     return flow; n   } n   for (int& i = pin[now]; i < each[now].size(); i++) { n     int it = each[now][i]; n     if (e[it].c && dep[e[it].v] == dep[now] + 1) { n       int tmp; n       if (tmp = dfs(e[it].v, min(flow, e[it].c))) { n         e[it].c -= tmp; n         e[it ^ 1].c += tmp; n         return tmp; n       } n     } n   } n   return 0; n } n int Dinic() { n   int max_flow = 0; n   for (int i = 0; i < GRAPH_SIZE; ++i) { n     dep[i] = INF; n   } n   dep[s] = 0; n   while (bfs()) { n     for (int i = 0; i < GRAPH_SIZE; ++i) { n       pin[i] = 0; n     } n     int tmp; n     while (tmp = dfs(s, INF)) { n       max_flow += tmp; n     } n     for (int i = 0; i < GRAPH_SIZE; ++i) { n       dep[i] = INF; n     } n     dep[s] = 0; n   } n   return max_flow; n } n void make_edge(int U, int V, int C) { n   EDGE tmp; n   tmp.u = U; n   tmp.v = V; n   tmp.c = C; n   e.push_back(tmp); n   each[U].push_back(e.size() - 1); n   swap(tmp.u, tmp.v); n   tmp.c = 0; n   e.push_back(tmp); n   each[V].push_back(e.size() - 1); n } n void init() { n   e.clear(); n   for (int i = 0; i < GRAPH_SIZE; ++i) { n     each[i].clear(); n   } n } n vector<pair<int, pair<int, int> > > edges; n int n; n int m; n bool check(int x) { n   init(); n   for (int i = 1; i <= n; ++i) make_edge(s, i, 1); n   for (int i = 1; i <= n; ++i) make_edge(i + n, t, 1); n   for (int i = 0; i < x; ++i) { n     make_edge(edges[i].second.first, edges[i].second.second + n, 1); n   } n   if (Dinic() == n) return true; n   return false; n } n int main() { n   ios::sync_with_stdio(false); n   cin >> n >> m; n   for (int i = 1; i <= m; ++i) { n     int u, v, d; n     cin >> u >> v >> d; n     edges.push_back(make_pair(d, make_pair(u, v))); n   } n   sort(edges.begin(), edges.end()); n   if (!check(m)) { n     cout << -1 << endl; n     return 0; n   } n   int l = 1, r = m; n   while (l < r) { n     int mid = (l + r) >> 1; n     if (check(mid)) { n       r = mid; n     } else { n       l = mid + 1; n     } n   } n   cout << edges[l - 1].first << 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(0); n   cout.tie(0); n   ; n   int tt = 1; n   cin >> tt; n   for (int tc = 1; tc <= tt; tc++) { n     int a; n     cin >> a; n     if (360 % (180 - a)) n       cout <<  NO n ; n     else n       cout <<  YES n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long mod = 1000000007; n long long dp[57 + 7][57 + 7]; n long long mdp[57 + 7][57 + 7]; n bool vis[57 + 7][57 + 7]; n int n, K; n long long ncr[57 + 7][57 + 7]; n int L, R; n long long func(int l, int r, int i, int j, int k, int m) { n   long long ret = (ncr[l][i] * ncr[r][j]) % mod; n   ret = (ret * ncr[L - l + i][k]) % mod; n   ret = (ret * ncr[R - r + j][m]) % mod; n   return ret; n } n void back(int l, int r) { n   if (vis[l][r]) return; n   mdp[l][r] = 200000007; n   dp[l][r] = 0; n   vis[l][r] = true; n   if (l == 0 && r == 0) { n     mdp[l][r] = 0; n     dp[l][r] = 1; n     return; n   } n   if (l * 50 + r * 100 <= K) { n     mdp[l][r] = 1; n     dp[l][r] = 1; n     return; n   } n   for (int i = 0; i <= l && i * 50 <= K; i++) { n     for (int j = 0; j <= r; j++) { n       if (i == 0 && j == 0) continue; n       if (i * 50 + j * 100 > K) break; n       for (int k = 0; k <= L - l + i; k++) { n         for (int m = 0; m <= R - r + j; m++) { n           if (k * 50 + m * 100 == 0) continue; n           if (k * 50 + m * 100 > K) break; n           if (l * 50 + r * 100 <= (l - i + k) * 50 + (r - j + m) * 100) n             continue; n           back(l - (i - k), r - (j - m)); n           mdp[l][r] = min(mdp[l][r], mdp[l - i + k][r - j + m] + 2); n         } n       } n     } n   } n   for (int i = 0; i <= l && i * 50 <= K; i++) { n     for (int j = 0; j <= r; j++) { n       if (i == 0 && j == 0) continue; n       if (i * 50 + j * 100 > K) break; n       for (int k = 0; k <= L - l + i; k++) { n         for (int m = 0; m <= R - r + j; m++) { n           if (k * 50 + m * 100 == 0) continue; n           if (k * 50 + m * 100 > K) break; n           if (l * 50 + r * 100 <= (l - i + k) * 50 + (r - j + m) * 100) n             continue; n           if (mdp[l - i + k][r - j + m] + 2 == mdp[l][r]) { n             dp[l][r] += n                 (func(l, r, i, j, k, m) * dp[l - i + k][r - j + m]) % mod; n             dp[l][r] %= mod; n           } n         } n       } n     } n   } n } n int main() { n   ncr[0][0] = 1; n   for (int i = 1; i <= 57; i++) { n     ncr[i][0] = 1; n     ncr[i][i] = 1; n     ncr[i][1] = i; n   } n   for (int i = 2; i <= 57; i++) { n     for (int j = 2; j < i; j++) { n       ncr[i][j] = (ncr[i - 1][j - 1] + ncr[i - 1][j]) % mod; n     } n   } n   scanf( %d%d , &n, &K); n   int l = 0; n   int r = 0; n   for (int i = 1; i <= n; i++) { n     int v; n     scanf( %d , &v); n     if (v == 50) n       l++; n     else n       r++; n   } n   L = l; n   R = r; n   back(l, r); n   if (mdp[l][r] != 200000007) { n     printf( %d n , (int)mdp[l][r]); n     printf( %d n , (int)dp[l][r]); n   } else { n     printf( -1 n0 n ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 410, MOD = 10007; n char s[maxn]; n int n, m, mat_s; n typedef long long Mat[maxn][maxn]; n void print(Mat A) { n   for (int i = 0; i < (mat_s); i++) { n     for (int j = 0; j < (mat_s); j++) { n       cout << A[i][j] <<    ; n     } n     cout << endl; n   } n   cout << endl; n } n void mat_mul(Mat A, Mat B, Mat res) { n   static Mat C; n   memset(C, 0, sizeof(C)); n   for (int i = 0; i < (mat_s); i++) n     for (int k = (i); k <= (mat_s - 1); k++) n       for (int j = (k); j <= (mat_s - 1); j++) { n         C[i][j] += A[i][k] * B[k][j]; n       } n   for (int i = 0; i < (mat_s); i++) n     for (int j = 0; j < (mat_s); j++) { n       C[i][j] %= MOD; n     } n   memcpy(res, C, sizeof(C)); n } n void mat_pow(Mat A, long long k, Mat res) { n   static Mat a, r; n   memcpy(a, A, sizeof(a)); n   memset(r, 0, sizeof(r)); n   for (int i = 0; i < (mat_s); i++) r[i][i] = 1; n   while (k) { n     if (k & 1) { n       mat_mul(r, a, r); n     } n     if (!(k >>= 1)) { n       break; n     } n     mat_mul(a, a, a); n   } n   memcpy(res, r, sizeof(r)); n } n int dp[210][210][210]; n int rec(int l, int r, int n24) { n   int& res = dp[l][r][n24]; n   if (~res) { n     return res; n   } n   if (l == r) { n     return res = !n24; n   } n   if (s[l] == s[r]) { n     if (l + 1 == r) { n       return res = !n24; n     } n     return res = rec(l + 1, r - 1, n24); n   } else { n     if (n24 > 0) { n       return res = (rec(l + 1, r, n24 - 1) + rec(l, r - 1, n24 - 1)) % MOD; n     } else { n       return res = 0; n     } n   } n } n int n24, n25, n26, times; n int mat_at(Mat A, int c24, int c25) { n   int i24 = n24 - c24; n   int i25 = n24 + c25 - 1; n   int i26 = i25 + n25; n   return A[i24][i26]; n } n int main() { n   scanf( %s%d , s, &n); n   m = strlen(s); n   n += m; n   times = (n + 1) >> 1; n   n24 = m - 1; n   n26 = n25 = (m + 1) >> 1; n   mat_s = n24 + n25 + n26; n   static Mat A, B; n   for (int i = 0; i < (mat_s); i++) { n     if (i < n24) { n       A[i][i] = 24; n       A[i][i + 1] = 1; n     } else if (i < n24 + n25) { n       A[i][i] = 25; n       if (i + 1 < n24 + n25) { n         A[i][i + 1] = 1; n       } n       A[i][i + n25] = 1; n     } else { n       A[i][i] = 26; n     } n   } n   mat_pow(A, times - 1, B); n   mat_mul(B, A, A); n   int ans = 0; n   memset(dp, -1, sizeof(dp)); n   for (int c24 = 0; c24 < (m); c24++) { n     int c25 = (m - c24 + 1) >> 1; n     if (times - c24 - c25 < 0) { n       continue; n     } n     int ways = rec(0, m - 1, c24); n     ans = (ans + ways * mat_at(A, c24, c25)) % MOD; n     if ((n & 1) && (!((m - c24) & 1))) { n       ans = (ans + MOD - ways * B[n24 - c24][n24 + c25 - 1] % MOD) % MOD; n     } n   } n   printf( %d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n bool cmp(pair<long long int, long long int> pa1, n          pair<long long int, long long int> pa2) { n   return pa1.second < pa2.second; n } n int main() { n   long long int n, k, i, mn, cnt; n   cin >> n >> k; n   long long int a[5001]; n   for (i = 0; i < n; i++) cin >> a[i]; n   long long int has[5001] = {0}; n   for (i = 0; i < n; i++) { n     has[a[i]]++; n   } n   mn = *max_element(has, has + 5001); n   if (mn <= k && k <= n) { n     cout <<  YES n ; n     pair<long long int, long long int> pa[5001]; n     for (i = 0; i < n; i++) { n       pa[i].first = a[i]; n       pa[i].second = i; n     } n     sort(pa, pa + n); n     cnt = 1; n     for (i = 0; i < n; i++) { n       pa[i].first = cnt; n       cnt++; n       if (cnt == k + 1) cnt = 1; n     } n     sort(pa, pa + n, cmp); n     for (i = 0; i < n; i++) cout << pa[i].first <<    ; n   } else n     cout <<  NO ; n } n


#include <bits/stdc++.h> n using namespace std; n int f[150000]; n int find(int x) { n   if (x == f[x]) n     return f[x]; n   else { n     return f[x] = find(f[x]); n   } n } n int a[150000]; n int num[150000]; n int main() { n   ios::sync_with_stdio(false); n   int n, m; n   while (cin >> n >> m) { n     memset(a, 0, sizeof(a)); n     memset(num, 0, sizeof(num)); n     for (int i = 1; i <= n; i++) f[i] = i; n     for (int i = 0; i < m; i++) { n       int x, y; n       cin >> x >> y; n       a[x]++; n       a[y]++; n       x = find(x), y = find(y); n       if (x != y) f[x] = y; n     } n     for (int i = 1; i <= n; i++) { n       find(i); n     } n     for (int i = 1; i <= n; i++) { n       num[f[i]]++; n     } n     bool flag = true; n     for (int i = 1; i <= n; i++) { n       if (a[i] != num[f[i]] - 1) { n         flag = false; n         break; n       } n     } n     if (flag) n       cout <<  YES  << endl; n     else n       cout <<  NO  << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 2e5 + 5; n struct node { n   int tim, a, b, id; n   bool operator<(const node& aa) const { return tim < aa.tim; } n } a[N]; n deque<node> s[5], rt[5]; n int vis[N]; n vector<int> ad, de; n int main() { n   int n, m, k; n   scanf( %d%d%d , &n, &m, &k); n   for (int i = 1; i <= n; i++) n     scanf( %d%d%d , &a[i].tim, &a[i].a, &a[i].b), a[i].id = i; n   sort(a + 1, a + 1 + n); n   int na = 0, nb = 0, ans = 2e9; n   for (int i = 1; i <= n; i++) { n     int ne = 0; n     if (a[i].a) ne |= 1; n     if (a[i].b) ne |= 2; n     rt[ne].push_back(a[i]); n   } n   int sum = 0; n   int x = (int)rt[1].size(), y = (int)rt[2].size(); n   int mi = min(x, y); n   if ((int)rt[3].size() + min(mi, (m - (int)rt[3].size()) / 2) < k) n     return 0 * printf( -1 n ); n   while (s[3].size() + min(mi, (m - (int)s[3].size()) / 2) < k || n          s[3].size() + rt[1].size() + rt[2].size() + rt[0].size() < m) { n     s[3].push_back(rt[3].front()); n     sum += rt[3].front().tim; n     vis[rt[3].front().id] = 1; n     rt[3].pop_front(); n   } n   int ned = k - s[3].size(); n   for (int i = 1; i <= ned; i++) { n     s[2].push_back(rt[2].front()); n     sum += rt[2].front().tim; n     vis[rt[2].front().id] = 1; n     rt[2].pop_front(); n     s[1].push_back(rt[1].front()); n     sum += rt[1].front().tim; n     vis[rt[1].front().id] = 1; n     rt[1].pop_front(); n   } n   int res = m - s[3].size() - max(0, 2 * (k - (int)s[3].size())); n   for (int i = 1; i <= res; i++) { n     int add = -1; n     if (rt[0].size()) add = 0; n     if (rt[1].size() && (add == -1 || rt[add].front().tim > rt[1].front().tim)) n       add = 1; n     if (rt[2].size() && (add == -1 || rt[add].front().tim > rt[2].front().tim)) n       add = 2; n     s[add].push_back(rt[add].front()); n     sum += rt[add].front().tim; n     vis[rt[add].front().id] = 1; n     rt[add].pop_front(); n   } n   ans = sum; n   for (; rt[3].size() && s[3].size() < m;) { n     sum = ans; n     ad.clear(), de.clear(); n     s[3].push_back(rt[3].front()); n     ad.push_back(rt[3].front().id); n     sum += rt[3].front().tim; n     rt[3].pop_front(); n     int num = 0; n     if (s[2].size()) { n       rt[2].push_front(s[2].back()); n       de.push_back(s[2].back().id); n       sum -= s[2].back().tim; n       s[2].pop_back(); n       num++; n     } n     if (s[1].size()) { n       rt[1].push_front(s[1].back()); n       de.push_back(s[1].back().id); n       sum -= s[1].back().tim; n       s[1].pop_back(); n       num++; n     } n     if (s[0].size()) { n       rt[0].push_front(s[0].back()); n       de.push_back(s[0].back().id); n       sum -= s[0].back().tim; n       s[0].pop_back(); n       num++; n     } n     while (num > 1) { n       int add = -1; n       if (rt[0].size()) add = 0; n       if (rt[1].size() && n           (add == -1 || rt[add].front().tim > rt[1].front().tim)) n         add = 1; n       if (rt[2].size() && n           (add == -1 || rt[add].front().tim > rt[2].front().tim)) n         add = 2; n       s[add].push_back(rt[add].front()); n       sum += rt[add].front().tim; n       ad.push_back(rt[add].front().id); n       rt[add].pop_front(); n       num--; n     } n     if (sum < ans) { n       for (int i : ad) vis[i]++; n       for (int i : de) vis[i]--; n       ans = sum; n     } n   } n   printf( %d n , ans); n   for (int i = 1; i <= n; i++) n     if (vis[i]) printf( %d  , i); n   printf(  n ); n   return 0; n } n