#include <bits/stdc++.h> n const int maxn = 5e5 + 5; n using namespace std; n bool vis[maxn]; n int a[maxn]; n int main() { n   string s; n   cin >> s; n   int k = s.size(); n   char y = s[0]; n   int ans = 1; n   for (int i = 0; i < k; i++) { n     if (i == 0) { n       cout <<  Mike  << endl; n       continue; n     } n     if (s[0] < s[i]) { n       cout <<  Ann  << endl; n       continue; n     } n     if (s[i] > y) { n       cout <<  Ann  << endl; n     } else { n       y = s[i]; n       cout <<  Mike  << endl; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int tt = 100005; n struct edge { n   edge *nxt; n   int des; n   void maker(edge *&a, int b) { n     nxt = a, des = b; n     a = this; n   } n } w[tt], *sta[tt], *tot; n struct treap { n   treap *l, *r; n   int key, sz, ran; n } * seg[tt * 4], buf[2000000], *pos; n int bg[tt], ed[tt], fa[tt][17], h[tt], ft[tt], now, A, B, Y, n; n void dfs(int i) { n   bg[i] = ++now; n   for (edge *e = sta[i]; e; e = e->nxt) { n     h[e->des] = h[i] + 1; n     dfs(e->des); n   } n   ed[i] = now; n } n void lr(treap *&a) { n   treap *b = a->r; n   a->r = b->l; n   b->l = a; n   b->sz = a->sz; n   a->sz = a->l->sz + a->r->sz + 1; n   a = b; n } n void rr(treap *&a) { n   treap *b = a->l; n   a->l = b->r; n   b->r = a; n   b->sz = a->sz; n   a->sz = a->l->sz + a->r->sz + 1; n   a = b; n } n void ins(treap *&a) { n   if (a == buf) { n     a = ++pos; n     a->l = a->r = buf, a->key = Y, a->sz = 1, a->ran = rand() * 32767 + rand(); n   } else { n     a->sz++; n     if (Y > a->key) { n       ins(a->r); n       if (a->r->ran > a->ran) lr(a); n     } else { n       ins(a->l); n       if (a->l->ran > a->ran) rr(a); n     } n   } n } n void ins(int now, int l, int r) { n   if (A <= l && r <= B) n     ins(seg[now]); n   else { n     if (A <= ((l + r) >> 1)) ins(now << 1, l, ((l + r) >> 1)); n     if (B > ((l + r) >> 1)) ins(now * 2 + 1, ((l + r) >> 1) + 1, r); n   } n } n void build(int now, int l, int r) { n   seg[now] = buf; n   if (l != r) n     build(now << 1, l, ((l + r) >> 1)), n         build(now * 2 + 1, ((l + r) >> 1) + 1, r); n } n int lca(int a, int b) { n   if (h[a] > h[b]) swap(a, b); n   for (int i = 16; i >= 0; i--) n     if (h[fa[b][i]] >= h[a]) b = fa[b][i]; n   if (a == b) return a; n   for (int i = 16; i >= 0; i--) n     if (fa[b][i] != fa[a][i]) b = fa[b][i], a = fa[a][i]; n   return fa[a][0]; n } n int qry(treap *a) { n   int ans = 0; n   for (; a != buf;) n     if (Y < a->key) { n       ans += a->r->sz + 1; n       a = a->l; n     } else n       a = a->r; n   return ans; n } n int qry(int now, int l, int r, int a) { n   int ans = qry(seg[now]); n   if (l != r) n     ans += a <= ((l + r) >> 1) ? qry(now << 1, l, ((l + r) >> 1), a) n                                : qry(now * 2 + 1, ((l + r) >> 1) + 1, r, a); n   return ans; n } n int qry(int a) { return h[a] + 1 - qry(1, 1, n, bg[a]); } n int getqry(int a, int k, int t) { n   for (int i = 16; i >= 0; i--) { n     int b = qry(fa[a][i]); n     if (t - b < k) { n       k -= t - b, a = fa[a][i]; n       t = b; n     } n   } n   return a; n } n int main() { n   int i, j, rt, m, t, a, b, c, k, cnt1, cnt2, cnt3; n   scanf( %d , &n); n   for (now = 0, i = 1; i <= n; i++) sta[i] = NULL, ft[i] = 0; n   for (tot = w, i = 1; i <= n; i++) n     if (scanf( %d , &fa[i][0]), !fa[i][0]) n       h[fa[i][0] = rt = i] = 0; n     else n       (tot++)->maker(sta[fa[i][0]], i); n   dfs(rt); n   for (i = 1; i <= 16; i++) n     for (j = 1; j <= n; j++) fa[j][i] = fa[fa[j][i - 1]][i - 1]; n   build(1, 1, n), buf->sz = 0, pos = buf; n   for (scanf( %d , &m), i = 1; i <= m; i++) { n     scanf( %d , &t); n     if (t == 1) { n       scanf( %d , &a); n       A = bg[a], B = ed[a], ft[a] = Y = i; n       ins(1, 1, n); n     } else { n       scanf( %d%d%d%d , &a, &b, &k, &Y); n       c = lca(a, b); n       cnt1 = qry(a), cnt2 = qry(c), cnt3 = qry(b); n       if (cnt1 + cnt3 - 2 * cnt2 + (ft[(c)] <= Y) - (ft[(a)] <= Y) - n               (ft[(b)] <= Y) < n           k) { n         puts( -1 ); n         continue; n       } n       if (cnt1 - cnt2 - (ft[(a)] <= Y) >= k) n         k = getqry(a, k, cnt1 - (ft[(a)] <= Y)); n       else { n         k = getqry(b, n                    cnt1 + cnt3 - 2 * cnt2 + (ft[(c)] <= Y) - (ft[(a)] <= Y) - n                        (ft[(b)] <= Y) - k + 1, n                    cnt3 - (ft[(b)] <= Y)); n       } n       printf( %d n , k); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n; n char s[10000010]; n struct Trie { n   struct nd { n     int ch[2], fa, fail, pos; n   } t[10000010]; n   int used; n   Trie() : used(1) {} n   int id[10000010]; n   int ins(int len, int I) { n     int u = 1; n     for (int i = 1; i <= len; i++) { n       if (!t[u].ch[s[i] -  a ]) n         t[u].ch[s[i] -  a ] = ++used, t[t[u].ch[s[i] -  a ]].fa = u; n       u = t[u].ch[s[i] -  a ]; n     } n     id[u] = I; n     return u; n   } n   void build() { n     queue<int> q; n     for (int j = 0; j < 2; j++) n       if (t[1].ch[j]) { n         t[t[1].ch[j]].fail = 1; n         q.push(t[1].ch[j]); n       } n     while (!q.empty()) { n       int i = q.front(); n       q.pop(); n       if (id[i]) n         t[i].pos = id[i]; n       else n         t[i].pos = t[t[i].fail].pos; n       for (int j = 0; j < 2; j++) { n         if (t[i].ch[j]) n           t[t[i].ch[j]].fail = t[t[i].fail].ch[j], q.push(t[i].ch[j]); n         else n           t[i].ch[j] = t[t[i].fail].ch[j]; n       } n     } n   } n } T; n int r[1000]; n bool e[1000][1000]; n void add(int i) { n   int p = r[i]; n   if (T.t[T.t[p].fail].pos) { n     e[T.t[T.t[p].fail].pos][i] = 1; n   } n   p = T.t[p].fa; n   while (p) { n     if (T.t[p].pos) { n       e[T.t[p].pos][i] = 1; n     } n     p = T.t[p].fa; n   } n } n int cp[1000]; n bool vis[1000]; n vector<int> pth[1000]; n int fnd(int x) { n   for (int i = 1; i <= n; i++) { n     if (!vis[i] && e[x][i]) { n       vis[i] = 1; n       if (!cp[i] || fnd(cp[i])) { n         cp[i] = x; n         return 1; n       } n     } n   } n   return 0; n } n int id[1000]; n int main() { n   scanf( %d , &n); n   for (int i = 1; i <= n; i++) { n     scanf( %s , s + 1); n     int m = strlen(s + 1); n     r[i] = T.ins(m, i); n   } n   T.build(); n   for (int i = 1; i <= n; i++) add(i); n   for (int k = 1; k <= n; k++) { n     for (int i = 1; i <= n; i++) { n       for (int j = 1; j <= n; j++) e[i][j] |= (e[i][k] & e[k][j]); n     } n   } n   for (int i = 1; i <= n; i++) { n     memset(vis, 0, sizeof(vis)); n     fnd(i); n   } n   memset(vis, 0, sizeof(vis)); n   for (int i = 1; i <= n; i++) n     if (cp[i]) vis[cp[i]] = 1; n   int c = 0; n   for (int i = 1; i <= n; i++) { n     if (!vis[i]) { n       c++; n       int x = i; n       pth[c].push_back(x); n       while (cp[x]) { n         x = cp[x]; n         pth[c].push_back(x); n       } n       reverse(pth[c].begin(), pth[c].end()); n     } n   } n   for (int i = 1; i <= c; i++) id[i] = pth[i].size() - 1; n   while (1) { n     memset(vis, 0, sizeof(vis)); n     for (int i = 1; i <= c; i++) { n       int u = pth[i][id[i]]; n       for (int j = 1; j <= n; j++) n         if (e[u][j]) vis[j] = 1; n     } n     bool fl = 1; n     for (int i = 1; i <= c; i++) n       if (vis[pth[i][id[i]]]) { n         fl = 0; n         break; n       } n     if (fl) break; n     for (int i = 1; i <= c; i++) { n       while (vis[pth[i][id[i]]]) id[i]--; n     } n   } n   printf( %d n , c); n   for (int i = 1; i <= c; i++) printf( %d  , pth[i][id[i]]); n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   long long tab[200001], min1, odl = -1, wynik, first; n   cin >> n; n   cin >> tab[0]; n   min1 = 0; n   first = 0; n   for (int i = 1; i < n; i++) { n     cin >> tab[i]; n     if (tab[i] == tab[min1]) { n       if (odl < i - min1) odl = i - min1; n       min1 = i; n     } n     if (tab[i] < tab[min1]) { n       min1 = i; n       first = i; n       odl = -1; n     } n   } n   if (odl == -1) n     cout << n * (tab[first] + 1) - 1; n   else { n     odl--; n     int space = n - (min1 - first + 1); n     if (odl > space) n       cout << n * tab[first] + odl; n     else n       cout << n * tab[first] + space; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 100010; n const int M = 10005; n const int mod = 1e9 + 7; n const int inf = 0x3f3f3f3f; n int n, a, i, j, b[M + 1], s[100100], r = 200000000; n int main() { n   scanf( %d , &n); n   for (i = 1; i <= M; i++) b[i] = 10000; n   for (i = 1; i <= n; i++) { n     scanf( %d , &a); n     s[i] = s[i - 1] + a; n     for (j = 1; j <= M && i - j > 0; j++) n       b[j] = min(b[j], abs(s[i] - s[i - j])); n   } n   for (j = 1; j <= M; j++) r = min(r, j * j + b[j] * b[j]); n   printf( %d n , r); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int a[55], b[55]; n   int n, i, j, sum = 0, sum1 = 0, s = 0, s1 = 0, count = 0, count1 = 0, k = 0; n   scanf( %d , &n); n   for (i = 0; i < n; i++) { n     scanf( %d , &a[i]); n     sum += a[i]; n   } n   for (i = 0; i < n; i++) { n     scanf( %d , &b[i]); n     sum1 += b[i]; n   } n   if (sum < sum1) { n     printf( No n ); n   } else { n     printf( Yes n ); n   } n   return 0; n } n


#include <bits/stdc++.h> n #pragma GCC optimize( Ofast ) n using namespace std; n const long long MX = 4e6 + 5, MOD = 1e9 + 7, inf = 1e18, LG = 21; n int main() { n   int t; n   cin >> t; n   while (t--) { n     long long x0, n, ans = 0; n     cin >> x0 >> n; n     if (n == 0) { n       cout << x0 <<   n ; n       continue; n     } n     if (x0 % 2 == 0) { n       long long k = (((n - 1) / 4) * 4 + 1); n       ans = x0 - k; n       for (long long i = k + 1; i <= n; i++) { n         if (i % 4 < 2) n           ans -= i; n         else n           ans += i; n       } n     } else { n       long long k = (((n - 1) / 4) * 4 + 1); n       ans = x0 + k; n       for (long long i = k + 1; i <= n; i++) { n         if (i % 4 < 2) n           ans += i; n         else n           ans -= i; n       } n     } n     cout << ans <<   n ; n   } n } n


#include <bits/stdc++.h> n #pragma GCC optimize( O3 ) n using namespace std; n const int maxN = 1 << 18; n template <class T1, class T2> n inline void remax(T1& a, T2 b) { n   a = max(a, (T1)b); n } n int deg[maxN], fat[maxN]; n vector<int> G[maxN]; n queue<int> Q; n long long w[maxN]; n pair<long long, int> leg[2][maxN]; n void solve() { n   int n, m; n   scanf( %d%d , &n, &m); n   for (int i = (1); i < (int)(n + 1); i++) scanf( %lld , w + i); n   bool tree = m == n - 1; n   while (m--) { n     int a, b; n     scanf( %d%d , &a, &b); n     G[a].push_back(b); n     G[b].push_back(a); n     deg[a]++, deg[b]++; n   } n   int s, prv = 0; n   scanf( %d , &s); n   if (tree) deg[s] = maxN; n   for (int i = (1); i < (int)(n + 1); i++) n     if (deg[i] == 1) Q.push(i); n   while (!Q.empty()) { n     int v = Q.front(); n     Q.pop(); n     for (int u : G[v]) n       if (deg[u] > 1) { n         pair<long long, int> prop = {leg[0][v].first + w[v], v}; n         fat[v] = u; n         if (leg[0][u].first <= prop.first) n           leg[1][u] = leg[0][u], leg[0][u] = prop; n         else if (leg[1][u].first < prop.first) n           leg[1][u] = prop; n         if (--deg[u] == 1) Q.push(u); n       } n   } n   long long res = 0, finish = 0; n   while (deg[s] < 2) { n     res += w[s]; n     remax(finish, leg[0][s].second == prv ? leg[1][s].first : leg[0][s].first); n     prv = s; n     s = fat[s]; n   } n   remax(finish, leg[0][s].second == prv ? leg[1][s].first : leg[0][s].first); n   for (int i = (1); i < (int)(n + 1); i++) n     if (deg[i] > 1) { n       res += w[i]; n       if (i != s) remax(finish, leg[0][i].first); n     } n   res += finish; n   printf( %lld n , res); n } n int main() { n   int t; n   t = 1; n   while (t--) solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, minx = 999999, ans, df1, df2; n   char s[51]; n   scanf( %d , &n); n   getchar(); n   scanf( %s , s); n   for (int i = 0; i < n - 3; i++) { n     int t = 0; n     ans = 0; n     while (t <= 3) { n       int x = i + t; n       if (t == 0) { n         df1 = s[x] -  A ; n         df2 =  Z  - s[x] + 1; n         ans += min(df1, df2); n       } n       if (t == 1) { n         df1 = abs(s[x] -  C ); n         df2 =  Z  - s[x] + 3; n         ans += min(df1, df2); n       } n       if (t == 2) { n         df1 = abs(s[x] -  T ); n         df2 = s[x] -  A  + 7; n         ans += min(df1, df2); n       } n       if (t == 3) { n         df1 = abs(s[x] -  G ); n         df2 =  Z  - s[x] + 7; n         ans += min(df1, df2); n       } n       t++; n     } n     minx = min(minx, ans); n   } n   printf( %d n , minx); n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 100005; n int n, m; n int a[N], q[N]; n int32_t main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cin >> m; n   for (int i = 0; i < m; ++i) cin >> q[i]; n   cin >> n; n   for (int i = 0; i < n; ++i) cin >> a[i]; n   sort(q, q + m); n   sort(a, a + n); n   int r = n - 1, res = 0; n   while (r >= 0) { n     int t = q[0]; n     while (t-- > 0 && r >= 0) { n       res += a[r--]; n     } n     int k = 2; n     while (k-- > 0 && r >= 0) r--; n   } n   cout << res <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 2001; n const long long INF = 1e15; n int DELTA = 2500; n int n; n long long price[MAXN], Time[MAXN]; n long long dp[MAXN][3 * MAXN]; n long long rec(int idx, int remTime) { n   if (remTime >= MAXN) remTime = MAXN - 1; n   if (idx == n) return remTime >= 0 ? 0 : INF; n   long long &ans = dp[idx][remTime + DELTA]; n   if (ans != -1) return ans; n   ans = INF; n   ans = min(ans, rec(idx + 1, remTime - 1)); n   ans = min(ans, price[idx] + rec(idx + 1, remTime + Time[idx])); n   return ans; n } n int main() { n   cin >> n; n   for (int i = 0; i < n; i++) cin >> Time[i] >> price[i]; n   memset(dp, -1, sizeof(dp)); n   cout << rec(0, 0) << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, s; n   cin >> n >> s; n   map<int, long long> sale; n   map<int, long long> buy; n   for (int i = 0; i < n; i++) { n     char c; n     cin >> c; n     if (c ==  B ) { n       int price, num; n       cin >> price >> num; n       buy[price] += num; n     } else { n       int price, num; n       cin >> price >> num; n       sale[price] += num; n     } n   } n   auto itb = sale.begin(); n   stack<pair<long long, long long>> answ; n   for (int i = 0; i < min(int(sale.size()), s); i++) { n     answ.push((*itb)); n     itb++; n   } n   while (!answ.empty()) { n     cout <<  S  n          <<     << answ.top().first <<     << answ.top().second << endl; n     answ.pop(); n   } n   auto its = buy.end(); n   for (int i = 0; i < min(int(buy.size()), s); i++) { n     its--; n     cout <<  B  <<     << (*its).first <<     << (*its).second << endl; n   } n   cin >> n; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   char s[1001]; n   int m[26]; n   while (gets(s)) { n     memset(m, 0, sizeof(m)); n     int len = strlen(s); n     int dan = 0, shuang = 0; n     for (int i = 0; i < len; i++) m[s[i] -  a ]++; n     for (int i = 0; i < 26; i++) n       if (m[i]) { n         if (m[i] % 2 == 0) shuang++; n         if (m[i] % 2) dan++; n       } n     if ((dan == 0) || (dan % 2)) n       printf( First n ); n     else n       printf( Second n ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long maxi = 1e18; n long long prime[] = {0, 2, 3, 5, 7, 11, 13, 17, 19, 23, 29}; n long long dp[15][1005], mi[15][1005], ans; n int n; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   for (int i = 1; i <= 10; i++) { n     mi[i][0] = 1; n     for (int j = 1; j <= 1000; j++) { n       long long temp; n       bool flag = __builtin_mul_overflow(mi[i][j - 1], prime[i], &temp); n       if (!flag) n         mi[i][j] = mi[i][j - 1] * prime[i]; n       else n         mi[i][j] = maxi; n     } n   } n   int n; n   cin >> n; n   for (int i = 1; i <= 10; i++) { n     for (int j = 1; j <= n; j++) dp[i][j] = maxi; n   } n   for (int i = 1; i <= n; i++) dp[1][i] = mi[1][i - 1]; n   for (int i = 2; i <= 10; i++) { n     for (int j = 1; j <= n; j++) { n       for (int k = 1; k <= j; k++) { n         if (j % k == 0) { n           long long temp; n           bool flag = n               __builtin_mul_overflow(dp[i - 1][j / k], mi[i][k - 1], &temp); n           if (!flag) dp[i][j] = min(dp[i][j], dp[i - 1][j / k] * mi[i][k - 1]); n         } n       } n     } n   } n   cout << dp[10][n] <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n; n vector<pair<int, int> > white, black; n int main() { n   cin >> n; n   for (int i = 0; i < n; i++) { n     int c, s; n     cin >> c >> s; n     if (c) { n       black.push_back(make_pair(s, i + 1)); n     } else { n       white.push_back(make_pair(s, i + 1)); n     } n   } n   sort(black.begin(), black.end()); n   sort(white.begin(), white.end()); n   int p_b = 0, p_w = 0; n   while (p_b < black.size() && p_w < white.size()) { n     cout << black[p_b].second <<     << white[p_w].second <<    ; n     if (black[p_b].first < white[p_w].first) { n       cout << black[p_b].first << endl; n       pair<int, int>& p = white[p_w]; n       p.first -= black[p_b].first; n       p_b++; n     } else { n       cout << white[p_w].first << endl; n       pair<int, int>& p = black[p_b]; n       p.first -= white[p_w].first; n       p_w++; n     } n   } n   if (p_b < black.size()) { n     p_b++; n     while (p_b < black.size()) { n       cout << white[0].second <<     << black[p_b++].second <<   0  << endl; n     } n   } n   if (p_w < white.size()) { n     p_w++; n     while (p_w < white.size()) { n       cout << black[0].second <<     << white[p_w++].second <<   0  << endl; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long INF = (long long)1 << 61; n int main() { n   long long n, m; n   cin >> n >> m; n   pair<long long, long long> first, last; n   bool was = false; n   vector<vector<char>> v(n, vector<char>(m)); n   for (int i = 0; i < n; ++i) { n     for (int j = 0; j < m; ++j) { n       cin >> v[i][j]; n       if (v[i][j] ==  B  && !was) { n         first = {i, j}; n         was = true; n       } n       if (v[i][j] ==  B ) { n         last = {i, j}; n       } n     } n   } n   cout << (first.first + last.first) / 2 + 1 <<     n        << (first.second + last.second) / 2 + 1; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int sum[200000 + 5]; n int main() { n   int n; n   scanf( %d , &n); n   for (int i = 0; i < n; ++i) { n     scanf( %d , sum + i); n     sum[i] += i; n   } n   sort(sum, sum + n); n   for (int i = 1; i < n; ++i) { n     if (sum[i] == sum[i - 1]) { n       printf( :( n ); n       return 0; n     } n   } n   for (int i = 0; i < n; ++i) { n     printf( %d  , sum[i] - i); n   } n   printf(  n ); n } n


#include <bits/stdc++.h> n using namespace std; n const double eps = 1e-11; n int vex[4] = {1, 0, -1, 0}; n int vey[4] = {0, 1, 0, -1}; n long long int MOD = 1000000007; n const int MAX = 3000000; n string c[100005]; n char s[MAX]; n int main() { n   int n; n   cin >> n; n   using p = pair<int, pair<int, int>>; n   using po = pair<int, int>; n   vector<p> d; n   for (int i = 0; i < (MAX); i++) s[i] =  * ; n   int last = 0; n   int a, b; n   for (int i = 0; i < (n); i++) { n     cin >> c[i]; n     int t = c[i].size(); n     cin >> a; n     for (int j = 0; j < (a); j++) { n       cin >> b; n       b--; n       d.push_back(p(b, po(b + t, i))); n     } n   } n   sort(d.begin(), d.end()); n   int in = d.size(); n   for (int j = 0; j < (in - 1); j++) { n     auto l = d[j]; n     auto r = d[j + 1]; n     if (l.first == r.first) continue; n     for (int k = max(l.first, last); k < l.second.first; k++) { n       s[k] = c[l.second.second][k - l.first]; n     } n     last = max(l.second.first, last); n   } n   auto l = d[in - 1]; n   for (int k = l.first; k < l.second.first; k++) { n     s[k] = c[l.second.second][k - l.first]; n   } n   string ans =   ; n   int la = 0; n   for (int i = (MAX)-1; i >= 0; i--) { n     if (s[i] !=  * ) { n       la = i; n       break; n     } n   } n   for (int i = 0; i < (la + 1); i++) { n     if (s[i] ==  * ) n       cout <<  a ; n     else n       cout << s[i]; n   } n   cout << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(false); n   int a, b, n; n   vector<int> div; n   cin >> a >> b >> n; n   if (b > a) swap(a, b); n   for (int d = 1; d * d <= b; d++) { n     if (a % d == 0 and b % d == 0) div.push_back(d); n     int d2 = -1; n     if (b % d == 0 and b / d != d) d2 = b / d; n     if (d2 != -1) n       if (a % d2 == 0) div.push_back(d2); n   } n   sort(div.begin(), div.end()); n   while (n--) { n     int l, r; n     cin >> l >> r; n     auto it = upper_bound(div.begin(), div.end(), r); n     it--; n     if (l <= *it and *it <= r) n       cout << *it << endl; n     else n       cout <<  -1 n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n multiset<int> s; n multiset<int>::iterator itr; n int c[5], tot; n int get_ty(int x) { n   if (c[2] >= x) n     return 1; n   else if (c[1] + c[2] >= x) n     return 2; n   else if (c[0] + c[1] + c[2] >= x) n     return 3; n   else n     return 4; n } n int main() { n   int n; n   scanf( %d , &n); n   for (int i = 0; i < 3; i++) scanf( %d , &c[i]); n   for (int i = 0; i < 3; i++) tot += c[i]; n   sort(c, c + 3); n   for (int i = 0; i < n; i++) { n     int t; n     scanf( %d , &t); n     s.insert(t); n   } n   int res = 0; n   bool isok = 0, flag = 0; n   while (!s.empty()) { n     int mm = *(--s.end()); n     if (!flag) { n       int ty = get_ty(mm); n       if (ty == 4) break; n       if (ty == 3) n         s.erase(--s.end()); n       else if (ty == 2) { n         s.erase(--s.end()); n         int loc = 0; n         for (int i = 0; i < 3; i++) n           if (tot - c[i] >= mm) loc = i; n         itr = s.upper_bound(c[loc]); n         if (itr != s.begin()) s.erase(--itr); n       } else if (ty == 1) n         flag = 1; n     } n     if (flag) { n       int loc = 0; n       for (int i = 2; i >= 0; i--) n         if (c[i] >= mm) loc = i; n       s.erase(--s.end()); n       itr = s.upper_bound(c[loc == 0]); n       bool dbk = 1; n       if (itr == s.begin()) n         dbk = 0; n       else { n         int tt = *(--itr); n         s.erase(itr); n         itr = s.upper_bound(c[1 + (loc <= 1)]); n         if (itr == s.begin()) { n           s.insert(tt); n           dbk = 0; n         } n       } n       if (dbk) n         s.erase(--itr); n       else { n         itr = s.upper_bound(tot - c[loc]); n         if (itr != s.begin()) s.erase(--itr); n       } n     } n     isok = 1; n     res++; n   } n   if (!isok) n     printf( -1 n ); n   else n     printf( %d n , res); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long ret[70]; n long long qpow(long long a, long long n) { n   long long ret = 1; n   while (n) { n     if (n & 1) ret = ret * a; n     a = a * a; n     n >>= 1; n   } n   return ret; n } n void solve() { n   ret[0] = 1; n   for (long long i = 1; i < 64; i++) { n     ret[i] = ret[i - 1] + qpow(2, i); n   } n } n vector<long long> res; n void getans() { n   for (long long i = 0; i < 64; i++) { n     long long tmp = qpow(2, i); n     for (long long j = i + 1; j < 64; j++) { n       res.push_back(ret[j] - tmp); n     } n   } n } n int main() { n   solve(); n   getans(); n   long long a, b; n   cin >> a >> b; n   sort(res.begin(), res.end()); n   long long pos1 = upper_bound(res.begin(), res.end(), a) - res.begin(); n   long long pos3 = lower_bound(res.begin(), res.end(), a) - res.begin(); n   long long pos2 = upper_bound(res.begin(), res.end(), b) - res.begin(); n   long long pos4 = lower_bound(res.begin(), res.end(), b) - res.begin(); n   long long ans = pos2 - pos1; n   if (pos3 < pos1) ans++; n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n const long long mod = (long long)(1e9 + 7); n int n; n long long s, ans; n long long f[1000]; n long long ksm(long long x, long long y) { n   long long ans = 1; n   while (y) { n     if (y & 1) ans = ans * x % mod; n     x = x * x % mod; n     y = y / 2; n   } n   return ans; n } n long long Lucas(long long n, long long m) { n   if (n < mod && m < mod) { n     if (n < m) return 0; n     if (n == m) return 1; n     if (m == 0) return 1; n     long long nj = 1, mj = 1; n     long long a = n - m, b = m; n     if (a > b) std::swap(a, b); n     long long i = b + 1; n     while (i <= n) { n       nj = (nj * i) % mod; n       i++; n     } n     i = 2; n     while (i <= a) { n       mj = (mj * i) % mod; n       i++; n     } n     return ksm(mj, mod - 2) * nj % mod; n   } n   return Lucas(n % mod, m % mod) * Lucas(n / mod, m / mod) % mod; n } n long long dle(long long x) { return (((1 & x) ^ 1) << 1) - 1; } n void dfs(int p, int l, long long sum) { n   if (p == n + 1) { n     ans = (ans + dle(l) * Lucas(s - sum + n - 1, n - 1)) % mod; n     return; n   } n   dfs(p + 1, l, sum); n   dfs(p + 1, l + 1, sum + f[p] + 1); n   return; n } n int main() { n   scanf( %d%I64d , &n, &s); n   for (int i = 1; i <= n; i++) scanf( %I64d , &f[i]); n   ans = 0; n   dfs(1, 0, 0); n   printf( %I64d n , (ans % mod + mod) % mod); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, a, b, c; n   cin >> n >> a >> b >> c; n   int x, y, z, p = 0, i; n   i = min(a, min(b, c)); n   i = n / i; n   for (x = i; x >= 0; x--) { n     for (y = 0; y <= i; y++) { n       z = abs((n - (a * x + b * y)) / c); n       if ((a * x + b * y + c * z) == n) { n         p = max(p, x + y + z); n       } n     } n   } n   cout << p << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long same(vector<pair<long long, int> > &x) { n   int n = x.size(); n   return (x[n - 1].first + x[n - 2].first) - (x[0].first + x[1].first); n } n long long diff(vector<pair<long long, int> > &x, long long h) { n   int n = x.size(); n   long long minx = min(x[0].first + x[1].first + h, x[1].first + x[2].first); n   long long maxx = n       max(x[n - 1].first + x[n - 2].first, x[0].first + x[n - 1].first + h); n   return maxx - minx; n } n int main() { n   int n, h; n   cin >> n >> h; n   vector<pair<long long, int> > a(n); n   for (int i = 0; i < (int)(n); i++) { n     long long tmp; n     cin >> tmp; n     a[i].first = tmp; n     a[i].second = i; n   } n   sort(a.begin(), a.end()); n   long long h1 = same(a); n   long long h2 = diff(a, h); n   cout << min(h1, h2) << endl; n   if (h1 < h2) { n     for (int i = 0; i < (int)(n); i++) printf( %d  , 1); n   } else { n     int head = a[0].second; n     for (int i = 0; i < (int)(n); i++) { n       int ret = (i == head) ? 1 : 2; n       printf( %d  , ret); n     } n   } n   puts(  ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void solve() { n   int n; n   cin >> n; n   string a, b; n   cin >> a >> b; n   vector<int> ans; n   int l = 0, r = n - 1; n   for (int i = 0; i < n; i++) { n     if (!(i & 1)) { n       if (a[l] == b[n - i - 1]) ans.push_back(1); n       ans.push_back(max(l, r) - min(l, r) + 1); n     } else { n       if (a[l] != b[n - i - 1]) ans.push_back(1); n       ans.push_back(max(l, r) - min(l, r) + 1); n     } n     swap(l, r); n     if (r > l) n       r--; n     else n       r++; n   } n   cout << ans.size() <<    ; n   for (auto x : ans) cout << x <<    ; n   cout <<   n ; n   return; n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   int t; n   cin >> t; n   while (t--) { n     solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long int min1(long long int a) { n   long long int mx = 0, mi = 10; n   while (a) { n     mx = max(mx, a % 10); n     mi = min(mi, a % 10); n     a /= 10; n   } n   return mi * mx; n } n int main() { n   int t; n   cin >> t; n   while (t--) { n     long long int a, k; n     cin >> a >> k; n     long long int tem = min1(a); n     while (--k && tem != 0) { n       a = a + tem; n       tem = min1(a); n     } n     cout << a << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long linf = LLONG_MAX; n const int MOD = 1000000007; n const int maxn = 1e5 + 10; n const int MAX = 2e4 + 10; n int _size[maxn], vis[maxn]; n vector<int> sub; n vector<pair<int, int> > adj[maxn]; n int n, k, a, b; n bool first(string second) { n   string a = to_string(linf); n   for (int i = 0; second[i]; i++) { n     if (second[i] > a[i]) n       return 0; n     else if (second[i] == a[i]) { n       int j = i; n       while (second[j] == a[j]) ++j; n       if (second[j] > a[j]) return 0; n       return 1; n     } n     return 1; n   } n   return 1; n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   string str; n   cin >> str; n   if (str.size() == 19) { n     if (first(str)) { n       cout <<  long n ; n       return 0; n     } else { n       cout <<  BigInteger n ; n       return 0; n     } n   } else { n     if (str.size() < 19) { n       stringstream sin(str); n       long long t = 0; n       int h = 32767; n       sin >> t; n       if (t <= INT_MAX && t > h) { n         cout <<  int n ; n         return 0; n       } else if (t > INT_MAX) { n         cout <<  long n ; n         return 0; n       } else if (t <= h) { n         int b = 127; n         if (t <= b) { n           cout <<  byte n ; n           return 0; n         } else { n           cout <<  short n ; n           return 0; n         } n       } n     } else { n       cout <<  BigInteger n ; n       return 0; n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n const long long OO = 1e9; n int main() { n   int n; n   scanf( %d , &n); n   vector<int> a(n); n   for (int i = 0; i < n; i++) scanf( %d , &a[i]); n   map<int, int> mp; n   vector<int> ans; n   for (int i = 0; i < n; i++) { n     if (mp[a[i]]) { n       ans.push_back(i + 1); n       mp.clear(); n     } else n       mp[a[i]] = i + 1; n   } n   if (ans.size() == 0) { n     cout << -1 << endl; n     return 0; n   } n   printf( %d n , (int)ans.size()); n   int h = 1; n   for (int i = 0; i < (int)ans.size(); i++) { n     if (i + 1 == ans.size()) ans[i] = n; n     printf( %d %d n , h, ans[i]); n     h = ans[i] + 1; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios::sync_with_stdio(0); n   int n, m; n   cin >> n >> m; n   int res = 2e9; n   for (int i = 0; i < m; i++) { n     int r, l; n     cin >> l >> r; n     res = min(res, r - l + 1); n   } n   vector<int> vec(n, 0); n   cout << res <<   n ; n   for (int i = 0; i < n; i++) { n     cout << i % res; n     if (i != n - 1) cout <<    ; n   } n   return 0; n } n


#include <bits/stdc++.h> n int i, j, n, m; n bool killed[200100], visited[200100]; n int vlist[200100][3]; n int ans[200100]; n inline int myabs(int a) { return a < 0 ? -a : a; } n inline int sign(int a) { return a / myabs(a); } n struct G { n   int ind[200100]; n   int last[200100 * 2], e[200100 * 2], e_num[200100 * 2]; n   int t; n   void addE(int a, int b, int ee) { n     t++; n     last[t] = ind[a]; n     ind[a] = t; n     e[t] = b; n     e_num[t] = ee; n   } n } g; n inline void gen_ans_for_i(int i, int j) { n   if (vlist[i][0] == 1) { n     ans[i] = vlist[i][1] > 0 ? 1 : 0; n   } else { n     if (myabs(vlist[i][1]) == j) { n       ans[i] = vlist[i][1] > 0 ? 1 : 0; n     } else { n       ans[i] = vlist[i][2] > 0 ? 1 : 0; n     } n   } n } n bool dfs(int a, int fae) { n   if (visited[a] || killed[a]) { n     return true; n   } n   visited[a] = 1; n   int i; n   for (i = g.ind[a]; i; i = g.last[i]) { n     if (g.e_num[i] != fae) n       if (dfs(g.e[i], g.e_num[i])) { n         if (!killed[a]) { n           gen_ans_for_i(g.e_num[i], a); n           killed[a] = true; n         } n       } n   } n   return killed[a]; n } n void dfs2(int a, int fae) { n   if (visited[a]) { n     return; n   } n   visited[a] = 1; n   int i; n   for (i = g.ind[a]; i; i = g.last[i]) { n     if (g.e_num[i] != fae) n       if (!killed[g.e[i]]) { n         killed[g.e[i]] = true; n         gen_ans_for_i(g.e_num[i], g.e[i]); n         dfs2(g.e[i], g.e_num[i]); n       } n   } n   return; n } n int main() { n   scanf( %d %d , &n, &m); n   int a, b; n   for (i = 1; i <= n; i++) { n     scanf( %d , &j); n     while (j--) { n       scanf( %d , &a); n       b = myabs(a); n       vlist[b][0]++; n       if (a < 0) { n         vlist[b][vlist[b][0]] = -i; n       } else { n         vlist[b][vlist[b][0]] = i; n       } n     } n   } n   for (i = 1; i <= m; i++) { n     if (vlist[i][0] == 1) { n       killed[myabs(vlist[i][1])] = true; n       if (vlist[i][1] < 0) { n         ans[i] = 0; n       } else { n         ans[i] = 1; n       } n     } else if (vlist[i][0] == 2) { n       a = myabs(vlist[i][1]); n       b = myabs(vlist[i][2]); n       if (sign(vlist[i][1]) * sign(vlist[i][2]) > 0) { n         killed[a] = true; n         killed[b] = true; n         ans[i] = vlist[i][1] > 0 ? 1 : 0; n       } else { n         g.addE(a, b, i); n         g.addE(b, a, i); n       } n     } n   } n   for (i = 1; i <= n; i++) { n     if (!visited[i] && !killed[i]) { n       dfs(i, 0); n     } n   } n   memset(visited, 0, sizeof(visited)); n   for (i = 1; i <= n; i++) { n     if (killed[i]) { n       dfs2(i, 0); n     } n   } n   for (i = 1; i <= n; i++) { n     if (!killed[i]) { n       printf( NO n ); n       return 0; n     } n   } n   printf( YES n ); n   for (i = 1; i <= m; i++) { n     printf( %d , ans[i]); n   } n   printf(  n ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<string> str(1); n vector<int> ans(1); n int solve(const string& s) { n   for (int len = 1; len <= 10; ++len) { n     for (int opt = 0; opt < (1 << len); ++opt) { n       bitset<10> bits(opt); n       string tmpS(len,  0 ); n       for (int i = 0; i < len; ++i) { n         tmpS[i] += bits[i]; n       } n       if (s.find(tmpS) == string::npos) { n         return len - 1; n       } n     } n   } n   return -1; n } n int main() { n   int n; n   scanf( %d , &n); n   for (int i = 0; i < n; ++i) { n     char buff[256]; n     scanf( %s , buff); n     str.push_back(buff); n     ans.push_back(solve(str.back())); n   } n   int m; n   scanf( %d , &m); n   for (int i = 0; i < m; ++i) { n     int a, b; n     scanf( %d%d , &a, &b); n     str.push_back(str[a] + str[b]); n     if (str.back().length() > 1000) { n       str.back() = str.back().substr(0, 500) + n                    str.back().substr(str.back().length() - 500, 500); n     } n     ans.push_back(max({solve(str.back()), ans[a], ans[b]})); n     cout << ans.back() << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int dx[] = {1, 0, -1, 0}; n const int dy[] = {0, 1, 0, -1}; n const int N = 300009; n const int INF = 1e9; n const long long INF64 = 1e18; n const double eps = 1e-9; n const int MOD = 1e9 + 7; n int n, q; n int a[N], f[N]; n int sum(int pos) { n   int res = 0; n   for (int i = pos; i >= 0; i = (i & (i + 1)) - 1) res += f[i]; n   return res; n } n void upd(int pos, int d) { n   for (int i = pos; i < n; i |= i + 1) f[i] += d; n } n int sum(int l, int r) { return sum(r) - sum(l - 1); } n int main() { n   cin >> n >> q; n   int l = 0, r = n - 1; n   bool rev = false; n   for (int i = 0; i < (int)(n); i++) { n     a[i] = 1; n     upd(i, 1); n   } n   for (int i = 0; i < (int)(q); i++) { n     int type, p, nl, nr; n     cin >> type; n     if (type == 1) { n       cin >> p; n       int len = r - l + 1; n       if (p * 2 <= len) { n         if (rev) { n           for (int i = r - p + 1, j = r - p; i <= r; ++i, --j) { n             upd(j, a[i]); n             upd(i, -a[i]); n             a[j] += a[i]; n             a[i] = 0; n           } n           r -= p; n         } else { n           for (int i = l + p - 1, j = l + p; i >= l; --i, ++j) { n             upd(j, a[i]); n             upd(i, -a[i]); n             a[j] += a[i]; n             a[i] = 0; n           } n           l += p; n         } n       } else { n         if (rev) { n           rev = false; n           p = len - p; n           for (int i = l + p - 1, j = l + p; i >= l; --i, ++j) { n             upd(j, a[i]); n             upd(i, -a[i]); n             a[j] += a[i]; n             a[i] = 0; n           } n           l += p; n         } else { n           rev = true; n           p = len - p; n           for (int i = r - p + 1, j = r - p; i <= r; ++i, --j) { n             upd(j, a[i]); n             upd(i, -a[i]); n             a[j] += a[i]; n             a[i] = 0; n           } n           r -= p; n         } n       } n     } else { n       cin >> nl >> nr; n       --nr; n       if (rev) { n         int ee = nr; n         nr = r - nl, nl = r - ee; n         cout << sum(nl, nr) << endl; n       } else { n         nl = l + nl, nr = l + nr; n         int xc = 0; n         cout << sum(nl, nr) << endl; n       } n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MX = 300005; n int n, c[MX]; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cin >> n; n   for (int i = 0; i < n; i++) cin >> c[i]; n   int res = 0, i = n - 1; n   while (c[0] == c[i]) i--; n   res = i; n   i = 0; n   while (c[i] == c[n - 1]) i++; n   res = max(res, n - i - 1); n   cout << res <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int a, b, c, d, sum, mod1, mod, mod2; n   cin >> a >> b >> c; n   sum = (a + b); n   if ((a >= b || a <= b) && (c <= sum)) { n     if (c == a) { n       cout <<  YES  << endl; n     } else if (c == sum) { n       cout <<  YES  << endl; n     } else { n       cout <<  NO  << endl; n     } n   } else if (c > sum) { n     mod = sum % b; n     sum = sum + 1; n     mod1 = sum % b; n     mod2 = c % b; n     if (mod2 == mod1 || mod2 == mod) { n       cout <<  YES  << endl; n     } else { n       cout <<  NO  << endl; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, k, m; n int arr[1005][105][2], arr1[1005], arr2[1005]; n long long dp(int i, int s, int hg) { n   if (s == 0 && hg) return ((i != n) ? ((9 * 1LL * arr2[n - i - 1]) % m) : 1); n   if (i == n) return 0; n   if (arr[i][s][hg] != -1) return arr[i][s][hg]; n   arr[i][s][hg] = 0; n   for (int j = 0; j <= 9; j++) { n     arr[i][s][hg] += n         dp(i + 1, (s + (j * 1LL * arr1[i]) % k) % k, (hg || (j != 0))); n     arr[i][s][hg] %= m; n   } n   return arr[i][s][hg]; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   memset(arr, -1, sizeof(arr)); n   cin >> n >> k >> m; n   arr1[0] = arr2[0] = 1; n   for (int i = 1; i <= 1000; i++) arr1[i] = (arr1[i - 1] * 1LL * 10) % k; n   for (int i = 1; i <= 1000; i++) arr2[i] = (arr2[i - 1] * 1LL * 10) % m; n   cout << dp(0, 0, 0) <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 1E5 + 5; n const int BASE1 = 1E9 + 7; n const int BASE2 = 999999993; n int n, k; n bool mark[MAXN]; n int a[MAXN], nTurn; n int main() { n   ios_base::sync_with_stdio(0); n   cin >> n >> k; n   for (int i = 0; i <= n; ++i) { n     string s; n     cin >> s; n     if (s[0] ==  ? ) n       mark[i] = 1, ++nTurn; n     else { n       stringstream ss; n       ss << s; n       ss >> a[i]; n     } n   } n   if (k == 0) { n     if (!mark[0]) { n       if (a[0] == 0) { n         cout <<  Yes ; n       } else { n         cout <<  No ; n       } n       return 0; n     } else { n       if (nTurn % 2 == n % 2) { n         cout <<  Yes ; n       } else { n         cout <<  No ; n       } n       return 0; n     } n   } else { n     if (nTurn == 0) { n       long long res1 = 0, res2 = 0, cur1 = 1, cur2 = 1; n       for (int i = 0; i <= n; ++i) { n         res1 = (res1 + a[i] * cur1 % BASE1 + BASE1) % BASE1; n         res2 = (res2 + a[i] * cur2 % BASE2 + BASE2) % BASE2; n         cur1 = cur1 * k % BASE1; n         cur2 = cur2 * k % BASE2; n       } n       if (res1 == 0 && res2 == 0) { n         cout <<  Yes ; n       } else n         cout <<  No ; n     } else { n       if (n % 2 == 0) { n         cout <<  No ; n       } else { n         cout <<  Yes ; n       } n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int M = 5005; n int a[M], n, dp[M], ans, mx, s[M], t[M], cnt; n bool vis[M]; n struct no { n   int l, r, x; n } p[M]; n bool cmp(no a, no b) { return a.r != b.r ? a.r < b.r : a.l < b.l; } n int main() { n   ios::sync_with_stdio(false); n   cin >> n; n   for (int i = 1; i <= n; i++) { n     cin >> a[i]; n     if (!p[a[i]].l) p[a[i]].l = i; n     p[a[i]].r = i; n     if (s[a[i]] == 0) s[a[i]] = i; n     t[a[i]] = i; n     mx = max(mx, a[i]); n   } n   for (int i = 1; i <= n; i++) { n     int l = i, r = t[a[i]]; n     memset(vis, 0, sizeof(vis)); n     bool flag = 0; n     int res = 0; n     for (int j = l; j <= r; j++) { n       if (vis[a[j]]) continue; n       if (s[a[j]] < l) { n         flag = 1; n         break; n       } n       r = max(r, t[a[j]]); n       res ^= a[j]; n       vis[a[j]] = 1; n     } n     if (flag) continue; n     p[++cnt] = (no){l, r, res}; n   } n   sort(p + 1, p + cnt + 1, cmp); n   for (int i = 1; i <= cnt; i++) { n     for (int j = 1; j < i; j++) { n       if (p[j].r < p[i].l) { n         dp[i] = max(dp[i], dp[j]); n       } n     } n     dp[i] += p[i].x; n     ans = max(ans, dp[i]); n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   vector<long long> a; n   a.push_back(0); n   a.push_back(1); n   map<long long, long long> m; n   int k, l = 0; n   m[0] = 1; n   m[1] = 1; n   while (1) { n     k = l; n     l = a.size(); n     for (int i = k; i < l; i++) { n       long long z = a[i] * 10 + 0; n       long long y = a[i] * 10 + 1; n       if (m[z] == 0) { n         a.push_back(z); n         m[z]++; n       } n       if (m[y] == 0) { n         a.push_back(y); n         m[y]++; n       } n     } n     if (a[a.size() - 1] > 1e9) break; n   } n   long long n; n   cin >> n; n   vector<long long>::iterator it; n   it = upper_bound(a.begin(), a.end(), n); n   cout << it - a.begin() - 1 << endl; n } n


#include <bits/stdc++.h> n using namespace std; n const int M = 1000000007; n const int N = 100005, E = 262144; n int n, m, i, u, v, p, t, vis[55][55], j, k; n vector<int> g[55][55]; n int q[N], l, r; n vector<int> ansr, ansl, w[55][55]; n long long g1[55][55][105], g2[55][55][105], g3[55][55][105]; n long long dp[105][55][2], f[55][55][105], ans[55][105], fe[55][55][105]; n bool flag1 = true; n int main() { n   scanf( %d %d , &n, &m); n   for (i = 1; i <= m; ++i) { n     scanf( %d %d , &u, &v); n     vis[u][v] = 1; n     scanf( %d , &t); n     if (t == 0) flag1 = false; n     while (t--) { n       scanf( %d , &p); n       g[u][v].push_back(p); n     } n   } n   bool flag = true; n   for (i = 1; i <= n; ++i) n     for (j = 1; j <= n; ++j) n       if (i != j) { n         for (k = 0; k + 1 < g[i][j].size(); ++k) n           if (g[i][j][k] == i && g[i][j][k + 1] == j) break; n         if (k + 1 < g[i][j].size()) { n           l = 1, r = 0; n           for (p = k + 2; p < g[i][j].size(); ++p) q[++r] = g[i][j][p]; n           p = j; n           ansl.clear(), ansr.clear(); n           ansr.push_back(j); n           while (l <= r && r <= 2 * n) { n             if (!vis[p][q[l]]) break; n             for (auto it : g[p][q[l]]) q[++r] = it; n             p = q[l]; n             ansr.push_back(q[l]); n             ++l; n           } n           if (l <= r) continue; n           l = 1, r = 0; n           for (p = k - 1; p >= 0; --p) q[++r] = g[i][j][p]; n           p = i; n           ansl.push_back(i); n           while (l <= r && r <= 2 * n) { n             if (!vis[q[l]][p]) break; n             for (int i = g[q[l]][p].size() - 1; i >= 0; --i) n               q[++r] = g[q[l]][p][i]; n             p = q[l]; n             ansl.push_back(q[l]); n             ++l; n           } n           if (l <= r) continue; n           if (ansl.size() + ansr.size() <= 2 * n) { n             ++g1[ansl[ansl.size() - 1]][ansr[ansr.size() - 1]] n                 [ansl.size() + ansr.size() - 1]; n           } n         } n       } n   for (i = 1; i <= n; ++i) n     for (j = 1; j <= n; ++j) n       if (g[i][j].size() && g[i][j][0] == j) { n         l = 1, r = 0; n         for (p = 1; p < g[i][j].size(); ++p) q[++r] = g[i][j][p]; n         p = j; n         ansr.clear(); n         ansr.push_back(j); n         while (l <= r && r <= 2 * n) { n           if (!vis[p][q[l]]) break; n           for (auto it : g[p][q[l]]) q[++r] = it; n           p = q[l]; n           ansr.push_back(q[l]); n           ++l; n         } n         if (l <= r) continue; n         ++g3[i][ansr[ansr.size() - 1]][ansr.size()]; n       } n   for (i = 1; i <= n; ++i) n     for (j = 1; j <= n; ++j) n       if (g[i][j].size() && g[i][j][g[i][j].size() - 1] == i) { n         l = 1, r = 0; n         for (p = g[i][j].size() - 2; p >= 0; --p) q[++r] = g[i][j][p]; n         p = i; n         ansl.clear(); n         ansl.push_back(i); n         while (l <= r && r <= 2 * n) { n           if (!vis[q[l]][p]) break; n           for (int i = g[q[l]][p].size() - 1; i >= 0; --i) n             q[++r] = g[q[l]][p][i]; n           p = q[l]; n           ansl.push_back(q[l]); n           ++l; n         } n         if (l <= r) continue; n         ++g2[ansl[ansl.size() - 1]][j][ansl.size()]; n       } n   for (i = 1; i <= n; ++i) n     for (j = 1; j <= n; ++j) n       for (k = 1; k <= 2 * n; ++k) n         if (g1[i][j][k] || g2[i][j][k] || g3[i][j][k]) w[i][j].push_back(k); n   for (i = 1; i <= n; ++i) { n     memset(dp, 0, sizeof(dp)); n     dp[0][i][0] = 1; n     for (j = 1; j <= n * 2; ++j) n       for (k = 1; k <= n; ++k) n         for (l = 1; l <= n; ++l) n           for (auto m : w[l][k]) { n             dp[j][k][0] = (dp[j][k][0] + dp[j - m][l][0] * g2[l][k][m]) % M; n             dp[j][k][1] = (dp[j][k][1] + dp[j - m][l][0] * g1[l][k][m] + n                            dp[j - m][l][1] * g3[l][k][m]) % n                           M; n           } n     for (j = 1; j <= n; ++j) n       for (k = 1; k <= 2 * n; ++k) f[i][j][k] = dp[k][j][1]; n   } n   for (i = 1; i <= n; ++i) n     for (j = 1; j <= n; ++j) n       for (k = 1; k <= 2 * n; ++k) ans[j][k] = (ans[j][k] + f[i][j][k]) % M; n   for (i = 1; i <= n; ++i) n     for (j = 1; j <= n; ++j) n       for (l = 1; l <= n; ++l) n         if (g[l][i].size() == 0 && vis[l][i]) n           for (k = 1; k < 2 * n; ++k) n             fe[l][j][k + 1] = (fe[l][j][k + 1] + f[i][j][k]) % M; n   for (k = 1; k <= 2 * n; ++k) n     for (i = 1; i <= n; ++i) n       for (j = 1; j <= n; ++j) n         for (l = 1; l <= k; ++l) n           ans[i][k] = (ans[i][k] + ans[j][k - l] * fe[j][i][l]) % M; n   for (k = 1; k <= 2 * n; ++k) { n     long long s = 0; n     for (i = 1; i <= n; ++i) s = (s + ans[i][k]) % M; n     cout << s << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int N = (int)1e6 + 9; n set<pair<int, int> > len, pos; n int n, m; n void add_seg(int L, int R) { n   if (L > R) return; n   int sz = R - L + 1; n   pos.insert(make_pair(L, sz)); n   if (L != 1 && R != n) sz = (sz + 1) / 2; n   len.insert(make_pair(sz, L)); n } n void rem_seg(int L, int R) { n   if (L > R) return; n   int sz = R - L + 1; n   pos.erase(make_pair(L, sz)); n   if (L != 1 && R != n) sz = (sz + 1) / 2; n   len.erase(make_pair(sz, L)); n } n int loc[N]; n void add(int x) { n   auto it = len.end(); n   --it; n   int opt = it->first; n   it = len.lower_bound(make_pair(opt, -1)); n   auto segt = pos.lower_bound(make_pair(it->second, -1)); n   int L = segt->first; n   int R = L + (segt->second) - 1; n   rem_seg(L, R); n   int M = (L + R) / 2; n   if (L == 1) n     M = 1; n   else if (R == n) n     M = R; n   cout << M <<   n ; n   loc[x] = M; n   add_seg(L, M - 1); n   add_seg(M + 1, R); n } n void rem(int x) { n   int pp = loc[x]; n   loc[x] = 0; n   int L = pp, R = pp; n   auto v2 = pos.lower_bound(make_pair(pp + 1, -1)); n   if (v2 != pos.end() && v2->first == pp + 1) { n     R = (v2->first) + (v2->second) - 1; n     rem_seg(v2->first, (v2->first) + (v2->second) - 1); n   } n   v2 = pos.lower_bound(make_pair(pp + 1, -1)); n   if (v2 != pos.begin()) { n     --v2; n     if ((v2->first) + (v2->second) - 1 == pp - 1) { n       L = v2->first; n       rem_seg(v2->first, pp - 1); n     } n   } n   add_seg(L, R); n } n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   ; n   cin >> n >> m; n   add_seg(1, n); n   int typ; n   int id; n   for (int i = 0; i < m; i++) { n     cin >> typ >> id; n     if (typ == 1) n       add(id); n     else n       rem(id); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int inf = -1e9; n struct node { n   int low; n   int high; n   pair<int, int> val; n   node() : val(make_pair(inf, 0)) {} n   node(int _l, int _h) : low(_l), high(_h), val(make_pair(inf, 0)) {} n   node(int _l, int _h, pair<int, int> _v) : low(_l), high(_h), val(_v) {} n }; n int N = 1 << 19; n vector<node> seg(2 * N); n void initSeg(vector<pair<int, int>> vals) { n   int len = vals.size(); n   for (int i = 0; i < len; ++i) { n     seg[N + i].val = vals[i]; n     seg[N + i].low = i; n     seg[N + i].high = i + 1; n   } n   for (int i = len; i < N; ++i) { n     seg[N + i].low = i; n     seg[N + i].high = i + 1; n   } n   for (int i = N - 1; i > 0; --i) { n     seg[i].val = max(seg[2 * i].val, seg[2 * i + 1].val); n     seg[i].low = seg[2 * i].low; n     seg[i].high = seg[2 * i + 1].high; n   } n } n pair<int, int> querySeg(int index, int low, int high) { n   if (low <= seg[index].low && seg[index].high <= high) return seg[index].val; n   if (seg[index].high <= low || high <= seg[index].low) n     return make_pair(inf, 0); n   return max(querySeg(2 * index, low, high), n              querySeg(2 * index + 1, low, high)); n } n pair<int, int> updateSeg(int index, int low, int high, pair<int, int> val) { n   if (low <= seg[index].low && seg[index].high <= high) { n     seg[index].val = val; n     return val; n   } n   if (seg[index].high <= low || high <= seg[index].low) return seg[index].val; n   pair<int, int> left = updateSeg(2 * index, low, high, val); n   pair<int, int> right = updateSeg(2 * index + 1, low, high, val); n   seg[index].val = max(left, right); n   return seg[index].val; n } n vector<int> seq; n vector<int> a; n vector<pair<int, int>> b; n vector<bool> toVisit; n int n; n void dfs(int index) { n   toVisit[index] = false; n   updateSeg(1, index, index + 1, make_pair(inf, 0)); n   if (b[index].first < n && toVisit[b[index].first]) dfs(b[index].first); n   while (true) { n     pair<int, int> p = querySeg(1, 0, a[index]); n     if (p.first > index) n       dfs(p.second); n     else n       break; n   } n   seq.push_back(index); n } n int main() { n   int Test = 1; n   for (int test = 1; test <= Test; ++test) { n     cin >> n; n     a.clear(); n     b.clear(); n     seq.clear(); n     toVisit.clear(); n     for (int i = 0; i < n; ++i) { n       int in; n       cin >> in; n       if (in == -1) in = n + 1; n       a.push_back(in - 1); n     } n     for (int i = 0; i < n; ++i) { n       b.push_back(make_pair(n, i)); n       toVisit.push_back(true); n     } n     for (int i = 0; i < n; ++i) { n       if (a[i] == n) continue; n       b[a[i]].first = i; n     } n     initSeg(b); n     for (int i = 0; i < n; ++i) { n       if (toVisit[i]) dfs(i); n     } n     int ans[n]; n     for (int i = 0; i < n; ++i) { n       ans[seq[i]] = i + 1; n     } n     for (int i = 0; i < n; ++i) { n       cout << ans[i] <<    ; n     } n     cout << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   cin >> n; n   int t[n]; n   for (int i = 0; i < n; i++) cin >> t[i]; n   int left_cnt, left_time, right_cnt, right_time; n   int left_idx = 0, right_idx = n - 1; n   left_time = left_cnt = right_time = right_cnt = 0; n   while (left_idx <= right_idx) { n     left_time += t[left_idx]; n     left_cnt++; n     while (right_idx > left_idx && right_time < left_time) { n       right_time += t[right_idx--]; n       right_cnt++; n     } n     left_idx++; n   } n   cout << left_cnt <<     << right_cnt << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m, k; n int arr[51][51] = {0}, visit[51][51] = {0}; n vector<pair<int, pair<int, int> > > v; n int dfs(int a, int b, int count); n void make_land(int i, int j); n int main() { n   int i, j, sum = 0; n   char ch; n   cin >> n >> m >> k; n   for (i = 0; i < n; i++) { n     for (j = 0; j < m; j++) { n       cin >> ch; n       if (ch ==  * ) arr[i][j]++; n     } n   } n   for (i = 0; i < n; i++) { n     for (j = 0; j < m; j++) { n       if (!visit[i][j] && !arr[i][j]) { n         int value = dfs(i, j, 0); n         if (value != -1) v.push_back(make_pair(value, make_pair(i, j))); n       } n     } n   } n   sort(v.begin(), v.end()); n   int check = v.size(); n   vector<pair<int, pair<int, int> > >::iterator it = v.begin(); n   for (i = 0; i < v.size() - k; i++) { n     sum += it->first; n     int a = (it->second).first; n     int b = (it->second).second; n     make_land(a, b); n     it++; n   } n   cout << sum << endl; n   for (i = 0; i < n; i++) { n     for (j = 0; j < m; j++) { n       if (arr[i][j] == 1) n         cout <<  * ; n       else n         cout <<  . ; n     } n     cout << endl; n   } n } n int dfs(int i, int j, int count) { n   visit[i][j] = 1; n   if (count != -1) count++; n   if (i == n - 1 || j == m - 1 || i == 0 || j == 0) count = -1; n   if (i <= n - 1 && j - 1 >= 0 && !visit[i][j - 1] && !arr[i][j - 1]) n     count = dfs(i, j - 1, count); n   if (i - 1 >= 0 && j <= m - 1 && !visit[i - 1][j] && !arr[i - 1][j]) n     count = dfs(i - 1, j, count); n   if (i <= n - 1 && j + 1 <= m - 1 && !visit[i][j + 1] && !arr[i][j + 1]) n     count = dfs(i, j + 1, count); n   if (i + 1 <= n - 1 && j <= m - 1 && !visit[i + 1][j] && !arr[i + 1][j]) n     count = dfs(i + 1, j, count); n   return count; n } n void make_land(int i, int j) { n   arr[i][j] = 1; n   if (i <= n - 1 && j - 1 >= 0 && !arr[i][j - 1]) { n     make_land(i, j - 1); n   } n   if (i - 1 >= 0 && j <= m - 1 && !arr[i - 1][j]) { n     make_land(i - 1, j); n   } n   if (i <= n - 1 && j + 1 <= m - 1 && !arr[i][j + 1]) { n     make_land(i, j + 1); n   } n   if (i + 1 <= n - 1 && j <= m - 1 && !arr[i + 1][j]) { n     make_land(i + 1, j); n   } n } n


#include <bits/stdc++.h> n const int maxed = 200000 + 10; n struct Node { n   int x, y; n }; n std::vector<Node> ve[maxed]; n std::vector<Node>::iterator it; n std::map<long long, int> id; n std::map<std::pair<int, int>, int> ma; n int n, id_cnt; n int gcd_(int a, int b); n int main() { n   scanf( %d , &n); n   int a, b, c, sum = 0; n   while (n--) { n     scanf( %d%d%d , &a, &b, &c); n     if (a == 1) { n       sum += 1; n       long long w = 1LL * b * b + 1LL * c * c; n       if (!id.count(w)) id[w] = (++id_cnt); n       int wa = id[w]; n       ve[wa].push_back((Node){b, c}); n       int len = ve[wa].size(); n       for (int i = 0; i < len; ++i) { n         int x = ve[wa][i].x + b; n         int y = ve[wa][i].y + c; n         int z = gcd_(x, y); n         if (b == ve[wa][i].x && c == ve[wa][i].y) n           ma[std::pair<int, int>(x / z, y / z)] += 1; n         else n           ma[std::pair<int, int>(x / z, y / z)] += 2; n       } n     } else if (a == 2) { n       sum -= 1; n       long long w = 1LL * b * b + 1LL * c * c; n       int wa = id[w], len = ve[wa].size(); n       for (int i = 0; i < len; ++i) { n         int x = ve[wa][i].x + b; n         int y = ve[wa][i].y + c; n         int z = gcd_(x, y); n         if (ve[wa][i].x == b && ve[wa][i].y == c) n           ma[std::pair<int, int>(x / z, y / z)] -= 1; n         else n           ma[std::pair<int, int>(x / z, y / z)] -= 2; n       } n       for (it = ve[wa].begin(); it != ve[wa].end(); ++it) n         if (it->x == b && it->y == c) { n           ve[wa].erase(it); n           break; n         } n     } else { n       int z = gcd_(b, c), wa = 0; n       if (ma.count(std::pair<int, int>(b / z, c / z))) n         wa = ma[std::pair<int, int>(b / z, c / z)]; n       printf( %d n , sum - wa); n     } n   } n   return 0; n } n int gcd_(int a, int b) { n   if (!b) return a; n   return gcd_(b, a % b); n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long n, k; n   cin >> n >> k; n   if (k == 1) { n     if (n > 1) n       cout << n; n     else n       cout <<  -1 ; n     return 0; n   } n   int i; n   vector<int> res; n   for (i = 2; i * i <= n; i++) { n     while (n % i == 0 && k != 1) { n       n /= i; n       res.push_back(i); n       k--; n     } n     if (k == 1 && n != 1) { n       res.push_back(n); n       for (i = 0; i < res.size(); i++) { n         cout << res[i] <<    ; n       } n       return 0; n     } n   } n   cout <<  -1 ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T, typename U> n inline void smin(T &a, const U &b) { n   if (a > b) a = b; n } n template <typename T, typename U> n inline void smax(T &a, const U &b) { n   if (a < b) a = b; n } n template <class T> n inline void gn(T &first) { n   char c, sg = 0; n   while (c = getchar(), (c >  9  || c <  0 ) && c !=  - ) n     ; n   for ((c ==  -  ? sg = 1, c = getchar() : 0), first = 0; c >=  0  && c <=  9 ; n        c = getchar()) n     first = (first << 1) + (first << 3) + c -  0 ; n   if (sg) first = -first; n } n template <class T1, class T2> n inline void gn(T1 &x1, T2 &x2) { n   gn(x1), gn(x2); n } n template <class T1, class T2, class T3> n inline void gn(T1 &x1, T2 &x2, T3 &x3) { n   gn(x1, x2), gn(x3); n } n template <class T> n inline void print(T first) { n   if (first < 0) { n     putchar( - ); n     return print(-first); n   } n   if (first < 10) { n     putchar( 0  + first); n     return; n   } n   print(first / 10); n   putchar(first % 10 +  0 ); n } n template <class T> n inline void println(T first) { n   print(first); n   putchar(  n ); n } n template <class T> n inline void printsp(T first) { n   print(first); n   putchar(   ); n } n template <class T1, class T2> n inline void print(T1 x1, T2 x2) { n   printsp(x1), println(x2); n } n template <class T1, class T2, class T3> n inline void print(T1 x1, T2 x2, T3 x3) { n   printsp(x1), printsp(x2), println(x3); n } n int power(int a, int b, int m, int ans = 1) { n   for (; b; b >>= 1, a = (long long)a * a % m) n     if (b & 1) ans = (long long)ans * a % m; n   return ans; n } n int a[3010], arr[3010], n, nn; n long long dp[3010][3010]; n int main() { n   gn(n); n   for (int i = 1; i <= n; i++) { n     gn(a[i]); n     a[i] -= i; n     arr[i] = a[i]; n   } n   sort(arr + 1, arr + n + 1); n   nn = unique(arr + 1, arr + n + 1) - arr - 1; n   for (int i = 1; i <= n; i++) { n     dp[i][0] = 0x3f3f3f3f3f3f3f3fLL; n     for (int j = 1; j <= nn; j++) { n       dp[i][j] = abs(a[i] - arr[j]); n       dp[i][j] += dp[i - 1][j]; n       smin(dp[i][j], dp[i][j - 1]); n     } n   } n   print(dp[n][nn]); n } n


#include <bits/stdc++.h> n #pragma comment(linker,  /stack:200000000 ) n #pragma GCC optimize( Ofast ) n #pragma GCC target( sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native ) n using namespace std; n const int block_size = 320; n const long long mod = 1e9 + 7; n const long long inf = 1e9 + 7; n const long double eps = 1e-9; n const double PI = atan(1) * 4; n template <typename T> n inline int sign(const T &a) { n   if (a < 0) return -1; n   if (a > 0) return 1; n   return 0; n } n template <typename T, typename S> n inline bool upmin(T &a, const S &b) { n   return a > b ? a = b, 1 : 0; n } n template <typename T, typename S> n inline bool upmax(T &a, const S &b) { n   return a < b ? a = b, 1 : 0; n } n template <typename T> n inline void in(T &x) { n   x = 0; n   T f = 1; n   char ch = getchar(); n   while (!isdigit(ch)) { n     if (ch ==  - ) f = -1; n     ch = getchar(); n   } n   while (isdigit(ch)) { n     x = x * 10 + ch -  0 ; n     ch = getchar(); n   } n   x *= f; n } n long long twop(int x) { return 1LL << x; } n template <typename A, typename B> n inline void in(A &x, B &y) { n   in(x); n   in(y); n } n template <typename A, typename B, typename C> n inline void in(A &x, B &y, C &z) { n   in(x); n   in(y); n   in(z); n } n template <typename A, typename B, typename C, typename D> n inline void in(A &x, B &y, C &z, D &d) { n   in(x); n   in(y); n   in(z); n   in(d); n } n namespace SOLVE { n long long n; n long long a[100000]; n long long dp[2]; n long long pre[20000][2]; n long long getSum(long long l, long long r, long long v) { n   return pre[r][v] - pre[l - 1][v]; n } n void main() { n   in(n); n   n++; n   for (long long i = 1; i < n; i++) { n     in(a[i]); n     a[i]--; n     pre[i][a[i]] = 1; n   } n   for (long long i = 1; i < n; i++) { n     for (long long j = 0; j < 2; j++) pre[i][j] += pre[i - 1][j]; n   } n   memset(dp, 0xcf, sizeof(dp)); n   for (long long i = 1; i < n; i++) { n     if (a[i] == 0) { n       dp[0]++; n     } else { n       dp[1]++; n       upmax(dp[1], dp[0] + 1); n     } n   } n   long long ans = max(dp[0], dp[1]); n   for (long long i = 1; i < n; i++) { n     memset(dp, 0, sizeof(dp)); n     for (long long j = i; j < n; j++) { n       if (a[j] == 1) { n         dp[1]++; n       } else { n         dp[0]++; n         upmax(dp[0], dp[1] + 1); n       } n       upmax(ans, n             getSum(1, i - 1, 0) + getSum(j + 1, n - 1, 1) + max(dp[0], dp[1])); n     } n   } n   cout << ans << endl; n } n }  // namespace SOLVE n int main() { n   SOLVE::main(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m, i, x, q, w[110000]; n long long ans; n int f(int x) { return x * (x / 2) + x % 2; } n bool cmp(int x, int y) { return x > y; } n int main() { n   scanf( %d%d , &n, &m); n   for (i = 0; i < m; i++) scanf( %d%d , &q, w + i); n   sort(w, w + m, cmp); n   while (f(x + 1) <= n) x++; n   x = min(x, m); n   for (i = 0; i < x; i++) ans += w[i]; n   printf( %I64d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <class L, class R> n ostream& operator<<(ostream& os, pair<L, R> P) { n   return os <<  (  << P.first <<  ,  << P.second <<  ) ; n } n template <class T> n ostream& operator<<(ostream& os, vector<T> V) { n   os <<  [  ; n   for (auto v : V) os << v <<    ; n   return os <<  ] ; n } n template <class T> n ostream& operator<<(ostream& os, set<T> second) { n   os <<  {  ; n   for (auto s : second) os << s <<    ; n   return os <<  } ; n } n template <class T> n ostream& operator<<(ostream& os, multiset<T> second) { n   os <<  {  ; n   for (auto s : second) os << s <<    ; n   return os <<  } ; n } n template <class L, class R> n ostream& operator<<(ostream& os, map<L, R> M) { n   os <<  {  ; n   for (auto m : M) os <<  (  << m.first <<  :  << m.second <<  )  ; n   return os <<  } ; n } n template <typename Arg1> n void __f(const char* name, Arg1&& arg1) { n   cout << name <<   :   << arg1 <<   n ; n } n template <typename Arg1, typename... Args> n void __f(const char* names, Arg1&& arg1, Args&&... args) { n   const char* comma = strchr(names + 1,  , ); n   cout.write(names, comma - names) <<   :   << arg1 <<   | ; n   __f(comma + 1, args...); n } n void solve() { n   int n; n   cin >> n; n   vector<long long> a(n); n   for (int i = 0; i < n; ++i) { n     cin >> a[i]; n     if (i) a[i] += a[i - 1]; n   } n   long long sm = a[n - 1]; n   if (sm == 1) { n     cout <<  -1 n ; n     return; n   } n   vector<long long> divs; n   for (long long d = 2; d * d <= sm; ++d) { n     if (sm % d) continue; n     divs.push_back(d); n     while (sm % d == 0) { n       sm /= d; n     } n   } n   if (sm != 1) divs.push_back(sm); n   long long ans = LLONG_MAX; n   for (long long d : divs) { n     long long ta = 0; n     for (int i = 0; i < n - 1; ++i) { n       ta += min(a[i] % d, d - a[i] % d); n     } n     ans = min(ans, ta); n   } n   cout << ans <<   n ; n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   int T = 1, tc = 1; n   while (T--) { n     solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   double l, x, v1, v2, k; n   cin >> l >> x >> v1 >> v2 >> k; n   double n = ceil(l / k); n   double t2, t1; n   t2 = x / (v2 * (n * ((v1 + v2) / (v2 - v1)) - (n - 1))); n   t1 = t2 * (v1 + v2) / (v2 - v1); n   double odp = t1 * n + t2 * (n - 1); n   cout << setprecision(8) << odp; n } n


#include <bits/stdc++.h> n char c =  G ; n void incrementa(); n int main() { n   int n; n   scanf( %d , &n); n   n = n - 7; n   printf( ROYGBIV ); n   for (int i = 0; i < n; i++) incrementa(); n   return 0; n } n void incrementa() { n   printf( %c , c); n   switch (c) { n     case  G : n       c =  B ; n       break; n     case  B : n       c =  I ; n       break; n     case  I : n       c =  V ; n       break; n     case  V : n       c =  G ; n       break; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   cin >> n; n   string s; n   cin >> s; n   for (int i = 2; i <= n; i++) { n     if (n % i == 0) reverse(s.begin(), s.begin() + i); n   } n   cout << s; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n char grid[55][55]; n int T, N; n int main() { n   cin >> T; n   for (int QQQ = 0; QQQ < T; QQQ++) { n     cin >> N; n     bool works = true; n     for (int i = 0; i < N; i++) cin >> grid[i]; n     for (int r = 0; r < N - 1; r++) n       for (int c = 0; c < N - 1; c++) n         if (grid[r][c] ==  1  && n             (grid[r + 1][c] ==  0  && grid[r][c + 1] ==  0 )) n           works = false; n     if (works) n       cout <<  YES ; n     else n       cout <<  NO ; n     cout << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n using LL = long long; n constexpr int N = 150005; n double x[N], ex[N]; n array<double, 4> seg[4 * N]; n array<double, 4> merge(const array<double, 4> &l, const array<double, 4> &r) { n   array<double, 4> rv; n   rv[0] = l[0] + r[0]; n   rv[1] = max(l[1], l[0] + r[1]); n   rv[2] = max(r[2], r[0] + l[2]); n   rv[3] = max({l[3], r[3], l[2] + r[1]}); n   return rv; n } n void build(int at, int b, int e) { n   if (b == e) { n     seg[at][0] = ex[b]; n     seg[at][1] = seg[at][2] = seg[at][3] = max(0., ex[b]); n     return; n   } n   build(at * 2, b, ((b + e) / 2)); n   build(at * 2 + 1, ((b + e) / 2) + 1, e); n   seg[at] = merge(seg[at * 2], seg[at * 2 + 1]); n } n array<double, 4> query(int at, int b, int e, int l, int r) { n   if (e < l or r < b) return array<double, 4>(); n   if (l <= b and e <= r) { n     return seg[at]; n   } n   auto q1 = query(at * 2, b, ((b + e) / 2), l, r); n   auto q2 = query(at * 2 + 1, ((b + e) / 2) + 1, e, l, r); n   return merge(q1, q2); n } n int main() { n   ios::sync_with_stdio(false); n   cin.tie(nullptr); n   int n, m, c; n   cin >> n >> m >> c; n   for (int i = 1; i <= n; i++) cin >> x[i]; n   for (int i = 1; i < n; i++) { n     double p; n     cin >> p; n     ex[i] = (x[i + 1] - x[i]) * .5 - p * c / 100.0; n   } n   build(1, 1, n - 1); n   double result = 0; n   while (m--) { n     int l, r; n     cin >> l >> r; n     auto res = query(1, 1, n - 1, l, r - 1); n     result += res[3]; n   } n   cout << setprecision(10) << result << endl; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 100005; n vector<int> e[N]; n int n, ans, f[N][2], g[N][2]; n void dfs1(int x, int fa) { n   f[x][0] = 0; n   f[x][1] = 0; n   int d = e[x].size() - (fa != 0); n   for (auto i : e[x]) n     if (i != fa) { n       dfs1(i, x); n       f[x][0] = max(f[x][0], max(f[i][0], f[i][1]) - 1); n       f[x][1] = max(f[x][1], f[i][0]); n     } n   f[x][0] += d; n   f[x][1]++; n } n void upd(pair<int, int> &x, pair<int, int> &y, pair<int, int> z) { n   if (z > x) swap(x, z); n   y = max(y, z); n } n void dfs2(int x, int fa) { n   if (!fa) g[x][0] = 0, g[x][1] = 0; n   pair<int, int> mx1(0, x), mx2(-1, -1), mx3(0, x), mx4(-1, -1); n   upd(mx1, mx2, pair<int, int>(max(g[x][0], g[x][1]) - 1, -1)); n   upd(mx3, mx4, pair<int, int>(g[x][0], -1)); n   for (auto i : e[x]) n     if (i != fa) { n       upd(mx1, mx2, pair<int, int>(max(f[i][0], f[i][1]) - 1, i)); n       upd(mx3, mx4, pair<int, int>(f[i][0], i)); n     } n   int d = e[x].size() - 1; n   ans = max(ans, max(mx1.first + d + 1, mx3.first)); n   for (auto i : e[x]) n     if (i != fa) { n       g[i][0] = (mx1.second == i ? mx2.first : mx1.first) + d; n       g[i][1] = (mx3.second == i ? mx4.first : mx3.first) + 1; n       dfs2(i, x); n     } n } n int main() { n   srand(time(NULL)); n   scanf( %d , &n); n   for (int i = (int)(1); i <= (int)(n - 1); i++) { n     int x, y; n     scanf( %d%d , &x, &y); n     e[x].push_back(y); n     e[y].push_back(x); n   } n   dfs1(1, 0); n   dfs2(1, 0); n   printf( %d n , ans); n } n


#include <bits/stdc++.h> n using namespace std; n long long a[100009]; n int main() { n   long long n, x, y; n   int i; n   while (cin >> n >> x >> y) { n     int flag = 0; n     for (i = 1; i <= n - 1; i++) a[i] = 1, y--; n     a[n] = y; n     if (a[n] <= 0) flag = 1; n     long long sum = 0; n     for (i = 1; i <= n; i++) { n       sum = sum + a[i] * a[i]; n     } n     if (sum < x || flag) n       cout <<  -1  << endl; n     else { n       for (i = 1; i <= n; i++) { n         cout << a[i] << endl; n       } n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); n inline long long int rand(long long int x, long long int y) { n   ++y; n   return (rng() % (y - x)) + x; n } n long long int n, q, fw[300006], st[300006], en[300006], co, dist[300006], zero, n     line[300006], lines, sizee[300006]; n vector<long long int> v[300006]; n struct fen { n   long long int n; n   vector<long long int> fw; n   void init(long long int x) { n     fw.resize(x + 2 + 1); n     n = fw.size() - 1; n   } n   void update(long long int x, long long int y, long long int nv) { n     ++y; n     for (; x <= n; x += x & (-x)) fw[x] += nv; n     for (; y <= n; y += y & (-y)) fw[y] -= nv; n   } n   long long int sum(long long int x) { n     long long int res = 0; n     for (; x; x -= x & (-x)) res += fw[x]; n     return res; n   } n } fen[300006]; n struct fen2 { n   void update(long long int x, long long int y, long long int nv) { n     ++y; n     for (; x <= n; x += x & (-x)) fw[x] += nv; n     for (; y <= n; y += y & (-y)) fw[y] -= nv; n   } n   long long int sum(long long int x) { n     long long int res = 0; n     for (; x; x -= x & (-x)) res += fw[x]; n     return res; n   } n } depth; n void dfs(long long int x, long long int p) { n   st[x] = co++; n   if (p != 1) n     line[x] = line[p], sizee[line[p]]++; n   else if (x != 1) n     line[x] = lines++, sizee[line[x]]++; n   for (auto i : v[x]) { n     if (i == p) continue; n     dist[i] = dist[x] + 1; n     dfs(i, x); n   } n   en[x] = co; n } n int main() { n   cin >> n >> q; n   for (long long int i = 0; i < n - 1; i++) { n     long long int a, b; n     cin >> a >> b; n     v[a].push_back(b); n     v[b].push_back(a); n   } n   dfs(1, 1); n   for (long long int i = 0; i < lines; i++) fen[i].init(sizee[i]); n   for (long long int i = 1; i <= q; i++) { n     long long int op; n     cin >> op; n     if (op == 0) { n       long long int v, x, d; n       cin >> v >> x >> d; n       long long int overflow = d - dist[v]; n       if (overflow >= 0) depth.update(1, overflow + 1, x); n       if (v == 1) continue; n       if (overflow >= 0) n         fen[line[v]].update(overflow + 2, dist[v] + d + 1, x); n       else n         fen[line[v]].update(dist[v] - d + 1, dist[v] + d + 1, x); n     } else { n       long long int v; n       cin >> v; n       cout << depth.sum(dist[v] + 1) + fen[line[v]].sum(dist[v] + 1) <<   n ; n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int t, i; n   double sum, n; n   cin >> t; n   while (t--) { n     cin >> n; n     sum = 360 / (180 - n); n     if ((sum - (int)sum) == 0) 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 Maxn = 300005; n int res, cnt, head[Maxn], choose[Maxn], target[Maxn], las[Maxn], ct, n[Maxn], k, n     val[Maxn], a[20][Maxn], b[Maxn], bel[Maxn], state_cnt; n bool vis1[Maxn], vis2[Maxn], ans[Maxn], done[20], flag; n map<long long, int> Ma; n long long sum[Maxn], tot; n int tmp_state, Solution[Maxn][20], Solution_cnt; n struct edg { n   int nxt, to; n } edge[2 * Maxn]; n void add(int x, int y) { n   if (bel[x] == bel[y]) return; n   if (done[bel[x]] || done[bel[y]]) return; n   edge[++cnt] = (edg){head[x], y}; n   head[x] = cnt; n } n void dfs(int u) { n   vis1[u] = true; n   vis2[u] = true; n   for (int i = head[u]; i; i = edge[i].nxt) { n     int to = edge[i].to; n     if (vis2[to]) { n       res = to; n       tmp_state = 0; n       state_cnt++; n       tmp_state |= (1 << (bel[to] - 1)); n       tmp_state |= (1 << (bel[u] - 1)); n       vis2[u] = false; n       Solution[state_cnt][1] = to, Solution[state_cnt][2] = u; n       Solution_cnt = 2; n       return; n     } n     dfs(to); n     if (flag) return; n     if (tmp_state & (1 << (bel[u] - 1))) { n       if (res == u) n         val[state_cnt] = tmp_state, n         reverse(Solution[state_cnt] + 1, n                 Solution[state_cnt] + Solution_cnt + 1), n         Solution[state_cnt][Solution_cnt + 1] = Solution[state_cnt][1], n         tmp_state = 0; n       else n         state_cnt--, tmp_state = 0; n       flag = true; n       return; n     } else if (tmp_state) { n       tmp_state |= (1 << (bel[u] - 1)); n       Solution[state_cnt][++Solution_cnt] = u; n       vis2[u] = false; n       return; n     } n   } n   vis2[u] = false; n } n void get_ans(int id) { n   choose[bel[Solution[id][1]]] = b[Solution[id][1]]; n   for (int i = 2; Solution[id][i]; i++) { n     choose[bel[Solution[id][i]]] = b[Solution[id][i]]; n     target[bel[Solution[id][i - 1]]] = bel[Solution[id][i]]; n   } n } n int main() { n   scanf( %d , &k); n   for (int i = 1; i <= k; i++) { n     scanf( %d , &n[i]); n     for (int j = 1; j <= n[i]; j++) n       scanf( %d , &a[i][j]), b[++ct] = a[i][j], sum[i] += a[i][j], n                              tot += a[i][j]; n   } n   if (tot % k) { n     printf( NO ); n     return 0; n   } n   tot /= k; n   for (int i = 1; i <= k; i++) n     if (tot == sum[i]) done[i] = true; n   sort(b + 1, b + 1 + ct); n   for (int i = 1; i <= k; i++) n     for (int j = 1; j <= n[i]; j++) n       Ma[a[i][j]] = lower_bound(b + 1, b + 1 + ct, a[i][j]) - b, n       bel[Ma[a[i][j]]] = i; n   for (int i = 1; i <= k; i++) n     for (int j = 1; j <= n[i]; j++) n       if (Ma[tot - sum[i] + a[i][j]]) n         add(Ma[tot - sum[i] + a[i][j]], Ma[a[i][j]]); n   for (int i = 1; i <= ct; i++) n     if (!vis1[i]) flag = false, dfs(i); n   for (int i = 1; i <= k; i++) n     if (sum[i] == tot) val[++state_cnt] = 1 << (i - 1); n   int maxi = (1 << k) - 1; n   ans[0] = true; n   for (int i = 0; i <= maxi; i++) n     if (ans[i]) n       for (int j = 1; j <= state_cnt; j++) n         if (!(val[j] & i)) ans[val[j] | i] = true, las[val[j] | i] = j; n   if (ans[maxi]) { n     printf( YES n ); n     int state_now = maxi; n     while (state_now) { n       get_ans(las[state_now]); n       state_now ^= val[las[state_now]]; n     } n     for (int i = 1; i <= k; i++) n       if (target[i]) n         printf( %d %d n , choose[i], target[i]); n       else n         printf( %d %d n , a[i][1], i); n     return 0; n   } n   printf( NO ); n   return 0; n } n


#include <bits/stdc++.h> n #pragma GCC optimize( Ofast ) n #pragma GCC target( avx2 ) n using namespace std; n const int MX = 1005; n int n, a, b; n vector<int> adj[2 * MX][2]; n int leaf[MX][2]; n int pos[2 * MX][2]; n int siz[2 * MX][2]; n int best[MX][MX][2]; n int ans[MX][2]; n pair<int, int> rang[2 * MX][2]; n void dfs(int v, int p, int d) { n   if (pos[v][d] != -1) rang[v][d] = {pos[v][d], pos[v][d]}; n   siz[v][d] = 1; n   for (int to : adj[v][d]) { n     if (to != p) { n       dfs(to, v, d); n       siz[v][d] += siz[to][d]; n       rang[v][d].first = min(rang[v][d].first, rang[to][d].first); n       rang[v][d].second = max(rang[v][d].second, rang[to][d].second); n     } n   } n } n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   cin >> n >> a; n   for (int i = 0; i < a; i++) pos[i][0] = -1, rang[i][0] = {1e9, -1e9}; n   for (int i = 0; i < a - 1; i++) { n     int p; n     cin >> p; n     adj[p - 1][0].push_back(i + 1); n   } n   for (int i = 0; i < n; i++) { n     cin >> leaf[i][0], leaf[i][0]--; n     pos[leaf[i][0]][0] = i; n   } n   cin >> b; n   for (int i = 0; i < b; i++) pos[i][1] = -1, rang[i][1] = {1e9, -1e9}; n   for (int i = 0; i < b - 1; i++) { n     int p; n     cin >> p; n     adj[p - 1][1].push_back(i + 1); n   } n   for (int i = 0; i < n; i++) { n     cin >> leaf[i][1], leaf[i][1]--; n     pos[leaf[i][1]][1] = i; n   } n   dfs(0, -1, 0); n   dfs(0, -1, 1); n   for (int i = 0; i < a; i++) { n     auto cur = rang[i][0]; n     best[cur.first][cur.second][0] = n         max(best[cur.first][cur.second][0], siz[i][0] - (i == 0)); n   } n   for (int i = 0; i < b; i++) { n     auto cur = rang[i][1]; n     best[cur.first][cur.second][1] = n         max(best[cur.first][cur.second][1], siz[i][1] - (i == 0)); n   } n   for (int c = 0; c < 2; c++) { n     for (int len = 2; len < n + 1; len++) { n       for (int st = 0; st < n; st++) { n         if (st + len - 1 < n) { n           int l = st, r = st + len - 1; n           for (int k = l; k < r + 1; k++) { n             if (k < r) n               best[l][r][c] = n                   max(best[l][r][c], best[l][k][c] + best[k + 1][r][c]); n           } n         } n       } n     } n   } n   for (int i = 0; i < n + 1; i++) ans[i][0] = -1e9, ans[i][1] = -1e9; n   ans[0][0] = 0, ans[0][1] = 0; n   for (int i = 1; i < n + 1; i++) { n     for (int j = 0; j < i; j++) { n       ans[i][0] = max(ans[i][0], ans[j][1] + best[j][i - 1][1]); n       ans[i][1] = max(ans[i][1], ans[j][0] + best[j][i - 1][0]); n     } n   } n   cout << max(ans[n][0], ans[n][1]) <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n ifstream file( input.txt ); n int main() { n   int ta = 0, tb = 0, tc = 0, v = 2; n   string str; n   cin >> str; n   for (int i = 0; i < str.size(); i++) { n     if (str[i] ==  a ) n       ta++; n     else if (str[i] ==  b ) n       tb++; n     else n       tc++; n   } n   for (int i = 0; i < str.size() - 1; i++) n     if (int(str[i]) > str[i + 1]) { n       v--; n       break; n     } n   if (tc != ta && tc != tb) n     v--; n   else if (ta == 0 || tb == 0 || tc == 0) n     v--; n   if (v == 2) { n     cout <<  YES ; n   } else n     cout <<  NO ; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 2e5 + 5; n const int M = 2e3 + 5; n const int INF = 0x7fffffff; n const int mod = 998244353; n const double eps = 1e-8; n const double pi = acos(-1); n int n, m; n char str[N]; n int f[N][26]; n template <class T> n inline void read(T &x) { n   char c; n   x = 1; n   while ((c = getchar()) <  0  || c >  9 ) n     if (c ==  - ) x = -1; n   T res = c -  0 ; n   while ((c = getchar()) >=  0  && c <=  9 ) res = res * 10 + c -  0 ; n   x *= res; n } n int main() { n   scanf( %s , str + 1); n   read(n); n   int len = strlen(str + 1); n   for (int i = 1; i <= len; i++) { n     for (int j = 0; j <= 25; j++) { n       if (str[i] -  a  == j) n         f[i][j] = f[i - 1][j] + 1; n       else n         f[i][j] = f[i - 1][j]; n     } n   } n   while (n--) { n     int l, r; n     read(l); n     read(r); n     int cnt = 0; n     for (int i = 0; i <= 25; i++) { n       if (f[r][i] - f[l - 1][i] > 0) cnt++; n     } n     if ((cnt >= 3) || (l == r) || (str[l] != str[r])) n       cout <<  Yes n ; n     else n       cout <<  No n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n template <class T> n inline bool chmax(T& a, T b) { n   if (a < b) { n     a = b; n     return 1; n   } n   return 0; n } n template <class T> n inline bool chmin(T& a, T b) { n   if (a > b) { n     a = b; n     return 1; n   } n   return 0; n } n const long long MAX = 5100000; n const long long INF = 1LL << 60; n const long long mod = 1000000007LL; n using namespace std; n long long t[200500]; n long long tmp[200500]; n int main() { n   long long n; n   scanf( %lld , &n); n   long long a[3]; n   scanf( %lld %lld %lld , &a[0], &a[1], &a[2]); n   sort(a, a + 3); n   multiset<long long> ms; n   for (long long i = 0; i < n; i++) { n     scanf( %lld , &t[i]); n     ms.emplace(t[i]); n   } n   if (*ms.rbegin() > a[0] + a[1] + a[2]) { n     puts( -1 ); n     return 0; n   } n   long long res = 0; n   while (!ms.empty() && *ms.rbegin() > a[1] + a[2]) { n     res++; n     ms.erase(--ms.end()); n   } n   while (!ms.empty() && *ms.rbegin() > a[0] + a[2]) { n     res++; n     ms.erase(--ms.end()); n     auto itr = ms.upper_bound(a[0]); n     if (itr == ms.begin()) continue; n     itr--; n     ms.erase(itr); n   } n   while (!ms.empty() && *ms.rbegin() > a[2] && *ms.rbegin() > a[0] + a[1]) { n     res++; n     ms.erase(--ms.end()); n     if (ms.empty()) break; n     auto itr = ms.upper_bound(a[1]); n     if (itr == ms.begin()) continue; n     itr--; n     ms.erase(itr); n   } n   while (!ms.empty()) { n     res++; n     if (a[0] + a[1] <= a[2]) { n       ms.erase(--ms.end()); n       if (ms.empty()) break; n       if (*ms.begin() <= a[0]) { n         ms.erase(ms.begin()); n         if (ms.empty()) break; n         auto itr = ms.upper_bound(a[1]); n         if (itr == ms.begin()) continue; n         itr--; n         ms.erase(itr); n       } else if (*ms.begin() <= a[0] + a[1]) { n         auto itr = ms.upper_bound(a[1] + a[0]); n         itr--; n         ms.erase(itr); n       } n     } else { n       if (*ms.rbegin() <= a[2]) { n         auto itr = ms.upper_bound(a[1]); n         if (!(*ms.begin() > a[0] || itr == ms.begin() || itr == ++ms.begin())) { n           ms.erase(--ms.end()); n           if (ms.empty()) break; n           ms.erase(ms.begin()); n           if (ms.empty()) break; n           auto itr = ms.upper_bound(a[1]); n           itr--; n           ms.erase(itr); n           continue; n         } n       } n       ms.erase(--ms.end()); n       auto itr = ms.upper_bound(a[2]); n       if (itr != ms.begin()) { n         itr--; n         ms.erase(itr); n       } n     } n   } n   printf( %lld n , res); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n double us1[210]; n double us2[210]; n int deal(double *us, int s, int e, int p) { n   int flag = 0; n   for (int i = 1; i <= 200; i++) { n     if (us[i] < 1e-4 / 200.) continue; n     double dd = us[i] * (100 - p) / 100.; n     dd /= (e - s + 1); n     us[i] = us[i] / 100. * p; n     flag = 1; n     for (int ss = s; ss <= e; ss++) { n       us[max(i - ss, 0)] += dd; n     } n   } n   return flag; n } n int main() { n   int hp1, t1, s1, e1, p1; n   int hp2, t2, s2, e2, p2; n   cin >> hp1 >> t1 >> s1 >> e1 >> p1; n   cin >> hp2 >> t2 >> s2 >> e2 >> p2; n   if (p1 == 100) { n     cout << 0.0; n     return 0; n   } n   if (p2 == 100) { n     cout << 1.0; n     return 0; n   } n   int nt1 = 0, nt2 = 0; n   for (int i = 0; i <= 200; i++) { n     us1[i] = 0.; n     us2[i] = 0.; n   } n   us1[hp1] = 1.; n   us2[hp2] = 1.; n   double ret = 0; n   while (1) { n     if (nt1 <= nt2) { n       nt1 += t1; n       deal(us2, s1, e1, p1); n       ret += (1. - us1[0]) * us2[0]; n       us2[0] = 0; n     } else if (nt1 > nt2) { n       nt2 += t2; n       if (!deal(us1, s2, e2, p2)) break; n     } n   } n   cout << ret << endl; n   return 0; n } n


#include <bits/stdc++.h> n #pragma GCC optimize( Ofast ) n #pragma GCC optimize( unroll-loops ) n using namespace std; n const int maxn = 200000; n int t[4 * maxn]; n int p[maxn]; n int n; n int mina(int x, int y) { n   if (x == -1) return y; n   if (y == -1) return x; n   return (p[x] < p[y] ? x : y); n } n void build(int x, int cl, int cr) { n   if (cr - cl > 1) { n     build(2 * x, cl, (cl + cr) / 2); n     build(2 * x + 1, (cl + cr) / 2, cr); n     t[x] = mina(t[2 * x], t[2 * x + 1]); n   } else n     t[x] = cl; n } n int get(int x, int cl, int cr, int l, int r) { n   if (cr <= l || r <= cl) return -1; n   if (l <= cl && cr <= r) return t[x]; n   return mina(get(2 * x, cl, (cl + cr) / 2, l, r), n               get(2 * x + 1, (cl + cr) / 2, cr, l, r)); n } n int a[maxn], dpl[maxn], dpr[maxn]; n int mx = 0; n void dfs(int i, int cl, int cr, int cd) { n   dpl[i] = dpr[i] = 1; n   if (i != cl) { n     int u = get(1, 0, maxn, cl, i); n     dfs(u, cl, i, cd + 1); n     dpl[i] += max(dpl[u], dpr[u]); n   } n   if (cr - i > 1) { n     int u = get(1, 0, maxn, i + 1, cr); n     dfs(u, i + 1, cr, cd + 1); n     dpr[i] += max(dpr[u], dpl[u]); n   } n } n pair<int, int> solve(int sd) { n   for (int i = sd; i < n; i++) p[i - sd] = a[i]; n   for (int i = n - sd; i < n; i++) p[i] = a[i - n + sd]; n   build(1, 0, maxn); n   mx = 0; n   int st = get(1, 0, maxn, 0, n); n   dfs(st, 0, n, 1); n   return {max(dpl[st], dpr[st]), dpl[st] - dpr[st]}; n } n signed main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   cin >> n; n   for (int i = 0; i < n; i++) cin >> a[i]; n   int ans = solve(0).first; n   int mi = 0; n   int st = get(1, 0, maxn, 0, n); n   int l = 0, r = st; n   while (r - l > 10) { n     int md = (l + r) / 2; n     if (solve(md).second >= 0) n       l = md; n     else n       r = md; n   } n   for (int i = l; i <= r; i++) { n     int fuck = solve(i).first; n     if (fuck < ans) { n       mi = i; n     } n     ans = min(ans, fuck); n   } n   l = st; n   r = n - 1; n   while (r - l > 10) { n     int md = (l + r) / 2; n     if (solve(md).second >= 0) n       l = md; n     else n       r = md; n   } n   for (int i = l; i <= r; i++) { n     int fuck = solve(i).first; n     if (fuck < ans) { n       mi = i; n     } n     ans = min(ans, fuck); n   } n   cout << ans <<     << mi; n   return 0; n } n


#include <bits/stdc++.h> n int a[500]; n int temp1[500]; n int temp2[500]; n int sum[500]; n void merge(int l, int mid, int r) { n   int i, j, k; n   for (i = l; i <= mid; i++) temp1[i - l] = a[i]; n   temp1[i - l] = 10000000; n   for (j = mid + 1; j <= r; j++) temp2[j - mid - 1] = a[j]; n   temp2[j - mid - 1] = 1000000; n   i = 0; n   j = 0; n   k = l; n   while (k <= r) { n     if (temp1[i] < temp2[j]) { n       a[k] = temp1[i]; n       i++; n     } else { n       a[k] = temp2[j]; n       j++; n     } n     k++; n   } n } n void sorti(int l, int r) { n   if (l >= r) return; n   int mid = (l + r) / 2; n   sorti(l, mid); n   sorti(mid + 1, r); n   merge(l, mid, r); n } n int main() { n   int n; n   scanf( %d , &n); n   int i, j, k; n   for (i = 1; i <= n; i++) scanf( %d , &a[i]); n   int left = 0, right = 0; n   for (i = 1; i <= n; i++) { n     sum[i] = sum[i - 1] + a[i]; n   } n   int val, val1; n   int ans = 360; n   for (i = 1; i <= n; i++) { n     for (j = i; j <= n; j++) { n       val1 = (sum[j] - sum[i - 1]); n       val = 360 - val1; n       val = val > val1 ? (val - val1) : (val1 - val); n       ans = ans > val ? val : ans; n     } n   } n   printf( %d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int mxn = 2e5 + 5; n int main() { n   ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); n   ; n   int tc; n   cin >> tc; n   while (tc--) { n     string s; n     cin >> s; n     int a = 0, b = 0, c = 0; n     for (int i = 0; i < s.size(); i++) { n       if (s[i] ==  A ) n         a++; n       else if (s[i] ==  B ) n         b++; n       else n         c++; n     } n     if (a + c == b) n       cout <<  YES n ; n     else n       cout <<  NO n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 2e5 + 4 ; n int a[N] , prefMin[N] , prefMax[N] , suffixMin[N] , suffixMax[N]; n int main() n {   ios_base::sync_with_stdio(0); n     cin.tie(0); n     int t ; n     cin >> t ; n     while(t--){ n          int n , m , q  , sum =0 , s1 = 0 , s2 =0; n          cin >> n >> m ; n          string s; n          cin >> s; n          s =  #  + s ; n          for(int i = 1 ; i<=n ;++i){ n             if(s[i] ==  + ) n                 sum++ ; n             else n                 sum--; n             a[i] = sum; n             prefMin[i] = min(prefMin[i-1] ,sum); n             prefMax[i] = max(prefMax[i-1] , sum); n          } n          suffixMin[n+1] = suffixMax[n+1] = 0 ; n          sum = 0; n          for(int i = n ; i>=1; --i){ n             if(s[i] ==  + ) n                 s1++ , s2++; n             else n                s1-- , s2--; n             s1 = min(s1 , 0); n             s2 = max(s2 ,0); n             suffixMin[i] = s1; n             suffixMax[i] = s2; n          } n          while(m--){ n             int l , r ; n             cin >> l >> r; n             int maxi = max(prefMax[l-1] , suffixMax[r+1] + a[l-1]) ; n    int mini = min(prefMin[l-1] , suffixMin[r+1] + a[l-1]) ; n    cout<<maxi+abs(mini)+1<<   n  ; n          } n  n  n     } n     return 0; n } n


#include <bits/stdc++.h> n using namespace std; n inline int read() { n   int x = 0, f = 1; n   char ch = getchar(); n   for (; ch <  0  || ch >  9 ; ch = getchar()) n     if (ch ==  - ) f = -1; n   for (; ch >=  0  && ch <=  9 ; ch = getchar()) n     x = (x << 1) + (x << 3) + ch -  0 ; n   return x * f; n } n const int N = 2e5 + 5; n struct BIT { n   int tree[N]; n   void add(int x) { n     for (; x < N; x += x & -x) ++tree[x]; n   } n   int query(int x) { n     int res = 0; n     for (; x; x -= x & -x) res += tree[x]; n     return res; n   } n } bit; n struct SegmentTree { n   int size[N << 2]; n   void build(int cur, int l, int r) { n     if (l == r) { n       size[cur] = 1; n       return; n     } n     build((cur << 1), l, (l + r >> 1)); n     build((cur << 1 | 1), (l + r >> 1) + 1, r); n     size[cur] = size[(cur << 1)] + size[(cur << 1 | 1)]; n   } n   int kth(int cur, int l, int r, int k) { n     if (l == r) { n       size[cur] = 0; n       return l; n     } n     int res = 0; n     if (k <= size[(cur << 1)]) n       res = kth((cur << 1), l, (l + r >> 1), k); n     else n       res = kth((cur << 1 | 1), (l + r >> 1) + 1, r, k - size[(cur << 1)]); n     size[cur] = size[(cur << 1)] + size[(cur << 1 | 1)]; n     return res; n   } n } sgt; n int p[N]; n int main() { n   int n = read(); n   for (int i = 1; i <= n; ++i) { n     int x = read(); n     p[i] += x - bit.query(x); n     bit.add(x + 1); n   } n   memset(bit.tree, 0, sizeof(bit.tree)); n   for (int i = 1; i <= n; ++i) { n     int x = read(); n     p[i] += x - bit.query(x); n     bit.add(x + 1); n   } n   for (int i = n; i >= 1; --i) { n     p[i - 1] += p[i] / (n - i + 1); n     p[i] %= (n - i + 1); n   } n   sgt.build(1, 1, n); n   for (int i = 1; i <= n; ++i) printf( %d  , sgt.kth(1, 1, n, p[i] + 1) - 1); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long a, b, c; n   long long x0, y0, x, y; n   cin >> a >> b >> c >> x0 >> y0 >> x >> y; n   c += a * x0; n   c += b * y0; n   c = -c; n   x -= x0; n   y -= y0; n   if (x < 0) a = -a, x = -x; n   if (y < 0) b = -b, y = -y; n   if (c < 0) a = -a, b = -b, c = -c; n   if (!x || !y || a * b >= 0) n     printf( %.12f n , 1.0 * (x + y)); n   else { n     double u = min(1.0 * x, max(0., 1.0 * c / a)), n            v = max(0., min(1.0 * x, 1.0 * (c - b * y) / a)), n            uu = 1.0 * (c - a * u) / b, vv = 1.0 * (c - a * v) / b; n     printf( %.12f n , u + uu + sqrt((v - u) * (v - u) + (vv - uu) * (vv - uu)) + n                           x + y - v - vv); n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   cin >> n; n   int a[n]; n   for (int i = 0; i < n; i++) cin >> a[i]; n   reverse(a, a + n); n   stack<pair<int, int>> st; n   for (int i = 0; i < n; i++) { n     pair<int, int> p = make_pair(a[i], 0); n     while (!st.empty()) { n       pair<int, int> q = st.top(); n       if (q.first < p.first) { n         st.pop(); n         p.second = max(p.second + 1, q.second); n       } else n         break; n     } n     st.push(p); n   } n   int pas = 0; n   while (!st.empty()) { n     pas = max(pas, st.top().second); n     st.pop(); n   } n   cout << pas; n   return 0; n } n


#include <bits/stdc++.h> n #pragma comment(linker,  /STACK:16777216 ) n using namespace std; n struct edge { n  public: n   int flow, cap, cost; n   int u, v; n   edge(int _u, int _v, int _flow, int _cost, int _cap) { n     u = _u; n     v = _v; n     flow = _flow; n     cost = _cost; n     cap = _cap; n   } n   edge() {} n } E[10000]; n vector<int> V[10000]; n int say[100], src, dest; n int phi[10000]; n int B[10000], par[10000], D[10000]; n bool dijkstra() { n   memset(B, 0, sizeof(B)); n   for (int i = 1; i <= dest; i++) D[i] = 1000000000; n   D[0] = 0; n   par[0] = -1; n   D[dest + 1] = 1000000000; n   for (int it = 0; it <= dest; it++) { n     int v = dest + 1; n     for (int i = 0; i <= dest; i++) n       if (D[i] < D[v] && (!B[i])) v = i; n     B[v] = 1; n     if (v == dest + 1) break; n     for (int j = 0; j < V[v].size(); j++) { n       int idx = V[v][j]; n       if (E[idx].u == v) { n         if (E[idx].flow < E[idx].cap) { n           int nv = E[idx].v; n           assert(D[v] + (phi[v] - phi[nv] + E[idx].cost) >= 0); n           if ((!B[nv]) && D[nv] > D[v] + (phi[v] - phi[nv] + E[idx].cost)) { n             D[nv] = D[v] + (phi[v] - phi[nv] + E[idx].cost); n             par[nv] = idx; n           } n         } n       } else { n         if (E[idx].flow > 0) { n           int nv = E[idx].u; n           assert(D[v] + (phi[v] - phi[nv] - E[idx].cost) >= 0); n           if ((!B[nv]) && D[nv] > D[v] + (phi[v] - phi[nv] - E[idx].cost)) { n             D[nv] = D[v] + (phi[v] - phi[nv] - E[idx].cost); n             par[nv] = idx; n           } n         } n       } n     } n   } n   for (int i = 0; i <= dest; i++) n     if (B[i]) phi[i] += D[i]; n   return B[dest]; n } n void augment() { n   int cur = dest, aug = 1000000000; n   while (par[cur] >= 0) { n     int idx = par[cur]; n     if (E[idx].v == cur) { n       aug = min(aug, E[idx].cap - E[idx].flow); n       cur = E[idx].u; n     } else { n       aug = min(aug, E[idx].flow); n       cur = E[idx].v; n     } n   } n   cur = dest; n   while (par[cur] >= 0) { n     int idx = par[cur]; n     if (E[idx].v == cur) { n       E[idx].flow += aug; n       cur = E[idx].u; n     } else { n       E[idx].flow -= aug; n       cur = E[idx].v; n     } n   } n } n int main() { n   string t, s; n   int n, a; n   cin >> t >> n; n   src = 0; n   dest = n + n * 26 + 27; n   int cv = n + 1, ce = 0; n   for (int i = 1; i <= n; i++) { n     cin >> s >> a; n     memset(say, 0, sizeof(say)); n     int len = s.length(); n     for (int j = 0; j < len; j++) say[s[j] -  a ]++; n     for (int j = 0; j < 26; j++) { n       E[ce] = edge(i, cv, 0, 0, say[j]); n       V[i].push_back(ce); n       V[cv].push_back(ce); n       ce++; n       E[ce] = edge(cv, n + n * 26 + j + 1, 0, 0, 1000000000); n       V[cv].push_back(ce); n       V[n + n * 26 + j + 1].push_back(ce); n       ce++; n       cv++; n     } n     E[ce] = edge(0, i, 0, i, a); n     V[0].push_back(ce); n     V[i].push_back(ce); n     ce++; n   } n   memset(say, 0, sizeof(say)); n   int len = t.length(); n   for (int i = 0; i < len; i++) say[t[i] -  a ]++; n   for (int i = 1; i <= 26; i++) { n     E[ce] = edge(n + n * 26 + i, dest, 0, 0, say[i - 1]); n     V[n + n * 26 + i].push_back(ce); n     V[dest].push_back(ce); n     ce++; n   } n   while (dijkstra()) { n     augment(); n   } n   int can = 1; n   for (int i = 0; i < V[dest].size(); i++) { n     int idx = V[dest][i]; n     if (E[idx].flow != E[idx].cap) can = 0; n   } n   if (!can) { n     cout << -1 << endl; n     return 0; n   } n   int ans = 0; n   for (int i = 0; i < V[0].size(); i++) { n     int idx = V[0][i]; n     ans += E[idx].cost * E[idx].flow; n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <class T1, class T2> n istream &operator>>(istream &in, pair<T1, T2> &P) { n   in >> P.first >> P.second; n   return in; n } n template <class T1, class T2> n ostream &operator<<(ostream &out, const pair<T1, T2> &P) { n   out <<  (  << P.first <<  ,   << P.second <<  ) ; n   return out; n } n template <class T> n istream &operator>>(istream &in, vector<T> &arr) { n   for (auto &x : arr) in >> x; n   return in; n } n template <class T> n ostream &operator<<(ostream &out, const vector<T> &arr) { n   for (auto &x : arr) out << x <<    ; n   cout <<   n ; n   return out; n } n template <class T> n istream &operator>>(istream &in, deque<T> &arr) { n   for (auto &x : arr) in >> x; n   return in; n } n template <class T> n ostream &operator<<(ostream &out, const deque<T> &arr) { n   for (auto &x : arr) out << x <<    ; n   cout <<   n ; n   return out; n } n long long n, m, q, k, v[1000050], l, pref[1000050]; n pair<long long, long long> TT, w[1000050]; n string second; n long long dp[1000050]; n long long prox[1000050]; n long long resolve(long long cost) { n   memset(dp, 0, sizeof dp); n   for (long long i = n; i >= 1; i--) { n     long long A = n         dp[min(n + 1, i + l)] + (pref[min(n, i + l - 1)] - pref[i - 1]) - cost; n     long long B = dp[i + 1]; n     if (A > B) n       prox[i] = 0; n     else n       prox[i] = 1; n     dp[i] = max(A, B); n   } n   long long j = 1, usados = 0; n   while (j <= n) { n     if (!prox[j]) n       usados++, j = j + l; n     else n       j = j + 1; n   } n   return usados; n } n long long solve(long long a, long long b) { n   long long ini = 0, fim = 200000000000000000, mid, best = -1; n   while (fim >= ini) { n     mid = (ini + fim) / 2; n     long long us = resolve(mid); n     if (us <= k) n       best = mid, fim = mid - 1; n     else n       ini = mid + 1; n   } n   if (best == -1) return 0; n   long long us = resolve(best); n   return dp[1] + k * best; n } n int32_t main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   cin >> n >> k >> l; n   cin >> second; n   for (long long i = 1; i <= n; i++) { n     if ( a  <= second[i - 1] and second[i - 1] <=  z ) n       v[i] = 0; n     else n       v[i] = 1; n   } n   for (long long i = 1; i <= n; i++) pref[i] = pref[i - 1] + v[i]; n   long long A = pref[n] - solve(1, k); n   for (long long i = 1; i <= n; i++) { n     v[i] ^= 1; n     pref[i] = pref[i - 1] + v[i]; n   } n   long long B = pref[n] - solve(1, k); n   cout << min(A, B) <<   n ; n } n