#include <bits/stdc++.h> n using namespace std; n char c[100]; n int main() { n   int n; n   cin >> n; n   int i; n   cin.getline(c, 100); n   int open = 0; n   string tt =   ; n   for (i = 0; i < n; i++) { n     cin.getline(c, 100); n     string s =   ; n     int k = strlen(c); n     int j = 0, z; n     while (j < k && (c[j] ==     || c[j] ==   t )) j++; n     z = k - 1; n     while (z >= j && (c[z] ==     || c[z] ==   t )) z--; n     for (; j <= z; j++) { n       s += c[j]; n     } n     if (s ==  try ) { n       open++; n     } n     if (s.substr(0, 5) ==  throw ) { n       open = 0; n       k = s.length(); n       j = 5; n       while (j < k && (s[j] ==  (  || s[j] ==     || s[j] ==   t )) j++; n       z = k - 1; n       while (z >= j && (s[z] ==  )  || s[z] ==     || s[z] ==   t )) z--; n       for (; j <= z; j++) { n         tt += s[j]; n       } n     } n     if (s.substr(0, 5) ==  catch ) { n       if (open == 0 && tt !=   ) { n         j = 5; n         k = s.length(); n         while (j < k && (s[j] ==  (  || s[j] ==    )) j++; n         string ss =   ; n         string out =   ; n         for (;; j++) { n           if (s[j] ==     || s[j] ==  , ) break; n           ss += s[j]; n         } n         while (j < k && s[j] !=    ) j++; n         for (j++; s[j] !=    ; j++) { n           out += s[j]; n         } n         if (ss == tt) { n           printf( %s n , out.c_str()); n           tt =   ; n         } n       } n       open--; n       if (open < 0) open = 0; n     } n   } n   if (tt !=   ) { n     printf( Unhandled Exception n ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void debugv(std::vector<long long> v); n void printv(std::vector<long long> v); n void inputv(std::vector<long long> &v); n void solve() { n   long long n; n   cin >> n; n   string s; n   cin >> s; n   unordered_map<string, long long> mp; n   for (long long i = 0; i < n - 1; i++) { n     string k = s.substr(i, 2); n     mp[k]++; n   } n   long long maxi = INT_MIN; n   string ans =   ; n   for (auto i : mp) { n     if (i.second > maxi) { n       ans = i.first; n       maxi = i.second; n     } n   } n   cout << ans; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   long long t; n   t = 1; n   while (t--) { n     solve(); n     cout <<   n ; n   } n   return 0; n } n void debugv(std::vector<long long> v) { n   long long size = v.size(); n   for (long long i = 0; i < size; i++) { n     cerr << v[i] <<    ; n   } n   cout <<   n ; n } n void printv(std::vector<long long> v) { n   long long size = v.size(); n   for (long long i = 0; i < size; i++) { n     cout << v[i] <<    ; n   } n   cout <<   n ; n } n void inputv(std::vector<long long> &v) { n   for (auto &i : v) { n     cin >> i; n   } n } n


#include <bits/stdc++.h> n using namespace std; n long long n, k, s[30][105], cnt[105]; n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   cin >> n >> k; n   string t; n   cin >> t; n   s[t[0] -  a ][1] = 1; n   for (int i = 1; i < t.size(); i++) { n     int u = t[i] -  a ; n     for (int j = i + 1; j >= 2; j--) { n       s[u][j] = 0; n       for (int k = 0; k < 26; k++) { n         s[u][j] += s[k][j - 1]; n       } n     } n     s[u][1] = 1; n   } n   for (int i = 0; i < 26; i++) { n     for (int j = 1; j <= t.size(); j++) { n       cnt[j] += s[i][j]; n     } n   } n   cnt[0] = 1; n   long long res = 0, id = t.size(); n   while (k > 0) { n     long long u = min(k, cnt[id]); n     k -= u; n     res += (n - id) * u; n     id--; n     if (id < 0) break; n   } n   if (k > 0) return cout << -1, 0; n   cout << res << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int mod = 1e9 + 7; n int power(int p, int k) { n   int a = 1; n   while (k) { n     if (k & 1) a = (a * p) % mod; n     p = (p * p) % mod; n     k >>= 1; n   } n   return a; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   int tc = 1; n   int i, j; n   cin >> tc; n   while (tc--) { n     int n; n     cin >> n; n     if (n == 1 || n == 2) n       cout << 1; n     else if (n == 3) n       cout << 2; n     else if (n % 2 == 0) n       cout << n / 2; n     else if (n % 2 == 1) n       cout << n / 2 + 1; n     cout <<   n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n struct time { n   int s; n   int e; n }; n int main() { n   int n; n   cin >> n; n   int s, e; n   int minc = INT_MAX, maxc = -1; n   for (int i = 0; i < n; i++) { n     cin >> s >> e; n     if (minc > e) { n       minc = e; n     } n     if (maxc < s) { n       maxc = s; n     } n   } n   int m; n   cin >> m; n   int maxp = -1, minp = INT_MAX; n   for (int i = 0; i < m; i++) { n     cin >> s >> e; n     if (minp > e) { n       minp = e; n     } n     if (maxp < s) { n       maxp = s; n     } n   } n   int p = maxp - minc; n   int q = maxc - minp; n   if (p < 0 && q < 0) { n     cout << 0; n   } else { n     cout << max(p, q); n   } n } n


#include <bits/stdc++.h> n using namespace std; n const long long int mod = 1e9 + 7; n const long double PI = 3.141592653589793; n const long long int INF = 9223372036854775807ll; n void solve() { n   string p, h; n   cin >> p >> h; n   if (h.size() < p.size()) { n     cout <<  NO n ; n     return; n   } n   vector<int> x(26, 0); n   for (int i = 0; i < p.size(); i++) x[p[i] -  a ]++; n   for (int i = 0; i < h.size() - p.size() + 1; i++) { n     vector<int> v(26, 0); n     for (int j = i; j < i + p.size(); j++) v[h[j] -  a ]++; n     int cnt = 0; n     for (int i = 0; i < 26; i++) n       if (x[i] == v[i]) cnt++; n     if (cnt == 26) { n       cout <<  YES n ; n       return; n     } n   } n   cout <<  NO n ; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   int t = 1; n   cin >> t; n   while (t--) solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n namespace IO { n const int maxn(1 << 21 | 1); n char *iS, *iT, ibuf[maxn], obuf[maxn], *oS = obuf, *oT = obuf + maxn - 1, c, n                                        st[20]; n int f, tp, len; n inline char getc() { n   return iS == iT ? (iT = (iS = ibuf) + fread(ibuf, 1, maxn, stdin), n                      iS == iT ? EOF : *iS++) n                   : *iS++; n } n template <class T> n inline void read(T &x) { n   for (c = getc(), f = 1; c <  0  || c >  9 ; c = getc()) f = c ==  -  ? -1 : 1; n   for (x = 0; c >=  0  && c <=  9 ; c = getc()) x = x * 10 + (c ^ 48); n   x *= f; n } n template <class T, class... Other> n inline void read(T &x, Other &...y) { n   read(x), read(y...); n } n inline void flush() { fwrite(obuf, 1, oS - obuf, stdout), oS = obuf; } n inline void putc(char ch) { n   *oS++ = ch; n   if (oS == oT) flush(); n } n template <class T> n inline void print(T x, char ch =   n ) { n   if (!x) putc( 0 ); n   if (x < 0) putc( - ), x = -x; n   while (x) st[++tp] = x % 10 +  0 , x /= 10; n   while (tp) putc(st[tp--]); n   putc(ch); n } n template <class T, class... Other> n inline void print(T x, Other... y) { n   print(x), print(y...); n } n inline void print_str(char *s) { n   for (len = strlen(s), f = 0; f < len; ++f) putc(s[f]); n } n inline void read_str(char *s, int &l) { n   for (l = 0, c = getc(); (c <  a  || c >  z ) && (c <  A  || c >  Z ); n        c = getc()) n     ; n   for (; (c >=  a  && c <=  z ) || (c >=  A  && c <=  Z ); c = getc()) n     s[l++] = c; n   s[l] =   0 ; n } n }  // namespace IO n using IO ::flush; n using IO ::print; n using IO ::print_str; n using IO ::read; n using IO ::read_str; n template <class T> n inline void cmax(T &x, T y) { n   x = x >= y ? x : y; n } n template <class T, class... Other> n inline void cmax(T &x, T y, Other... z) { n   Cmax(x, y), Cmax(x, z...); n } n template <class T> n inline void cmin(T &x, T y) { n   x = x <= y ? x : y; n } n template <class T, class... Other> n inline void cmin(T &x, T y, Other... z) { n   Cmin(x, y), Cmin(x, z...); n } n inline int gcd(int x, int y) { return y ? gcd(y, x % y) : x; } n const int mod(998244353); n inline void inc(int &x, const int y) { x = x + y >= mod ? x + y - mod : x + y; } n inline void dec(int &x, const int y) { x = x - y < 0 ? x - y + mod : x - y; } n inline int add(const int x, const int y) { n   return x + y >= mod ? x + y - mod : x + y; n } n inline int sub(const int x, const int y) { n   return x - y < 0 ? x - y + mod : x - y; n } n inline int fpow(long long x, int y) { n   long long ret = 1; n   for (; y; y >>= 1, x = x * x % mod) n     if (y & 1) ret = ret * x % mod; n   return ret; n } n const int maxn(1e5 + 5); n int n, seq[maxn], st[18][maxn], lg[maxn], mx; n long long cnt[maxn], sum[maxn], sumv[maxn]; n inline int query_gcd(int l, int r) { n   int len = lg[r - l + 1]; n   return gcd(st[len][l], st[len][r - (1 << len) + 1]); n } n inline long long calc(long long m, long long a, long long b, long long c) { n   if (c < 0) c = -c, a = -a, b = -b; n   if (m <= 0) return 0; n   if (m == 1) return (a + b) / c; n   if (!a) return b / c * m; n   long long ma, mb, ra, rb, nm; n   if (b < 0 || b >= c || a < 0 || a >= c) { n     ma = (a % c + c) % c, mb = (b % c + c) % c; n     ra = (a - ma) / c, rb = (b - mb) / c; n     return calc(m, ma, mb, c) + (ra * m * (m + 1) >> 1) + rb * m; n   } n   nm = (a * m + b) / c; n   return nm * m - calc(nm, c, -b - 1, a); n } n inline long long solve(long long x, long long cx, long long y, long long cy, n                        long long s) { n   if (!cx || !cy) return 0; n   if (cx * x + cy * y <= s) return cx * cy; n   long long s1 = s - cy * y, s2 = s - cx * x, ss = s - cx * x - cy * y; n   return calc(s / x, -x, s, y) - calc(s1 / x, -x, s1, y) - n          calc(s2 / y, -y, s2, x) + calc(ss / x, -x, ss, y); n } n inline long long check(long long mid) { n   int i, j, p; n   long long ret = 0, s, k; n   for (i = j = 1; i <= mx; ++i) { n     if (!cnt[i]) continue; n     k = min(mid / i, cnt[i]); n     if (k) ret += cnt[i] * k - (k * (k - 1) >> 1); n     while (j < mx && sumv[j + 1] - sumv[i - 1] <= mid) ++j; n     if (j >= i) ret += cnt[i] * (sum[j] - sum[i]); n     if (j == mx) continue; n     for (p = max(i, j); p < mx; ++p) { n       s = mid - sumv[p] + sumv[i]; n       if (s < 0) break; n       ret += solve(i, cnt[i], p + 1, cnt[p + 1], s); n     } n   } n   return ret; n } n int main() { n   int i, j, k, p, g; n   long long ql = 1, qr = 0, mid, ans, lim; n   read(n); n   for (i = 1; i <= n; ++i) read(seq[i]), st[0][i] = seq[i], cmax(mx, seq[i]); n   for (i = 2; i <= n; ++i) lg[i] = lg[i >> 1] + 1; n   for (j = 1; j <= lg[n]; ++j) n     for (i = 1; i + (1 << j) - 1 <= n; ++i) n       st[j][i] = gcd(st[j - 1][i], st[j - 1][i + (1 << (j - 1))]); n   for (i = 1; i <= n; ++i) n     for (j = i; j <= n; ++j) { n       g = query_gcd(i, j), p = j; n       for (k = lg[n - j]; ~k; --k) n         if (p + (1 << k) - 1 <= n && st[k][p] % g == 0) p += (1 << k) - 1; n       cnt[g] += p - j + 1, j = p; n     } n   for (i = 1; i <= mx; ++i) n     qr += cnt[i] * i, sum[i] = sum[i - 1] + cnt[i], n                       sumv[i] = sumv[i - 1] + cnt[i] * i; n   lim = (long long)n * (n + 1) >> 1, lim = lim * (lim + 1) >> 1, n   lim = (lim + 1) >> 1; n   while (ql <= qr) { n     mid = (ql + qr) >> 1; n     if (check(mid) >= lim) n       qr = mid - 1, ans = mid; n     else n       ql = mid + 1; n   } n   print(ans); n   return flush(), 0; 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   int flag1; n   flag1 = 0; n   int flag2 = 0; n   for (int i = 0; i < n; i++) { n     if (s[i] ==  I ) { n       flag1++; n     } n     if (s[i] ==  A ) { n       flag2++; n     } n   } n   if (flag1 == 0) { n     cout << flag2 << endl; n   } else if (flag1 == 1) { n     cout << 1 << endl; n   } else { n     cout << 0 << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long mod = 998244353; n const long long INF = mod * mod; n const long double eps = 1e-5; n const long double pi = acos(-1.0); n int n, m; n long long w[1 << 18]; n vector<int> G[1 << 18]; n bool canr[1 << 18]; n long long score[1 << 18]; n int c[1 << 18]; n bool iseda[1 << 18]; n long long dfs(int id, int fr) { n   long long ret = 0; n   for (int j = 0; j < G[id].size(); j++) { n     int to = G[id][j]; n     if (to == fr) continue; n     ret = max(ret, dfs(to, id)); n   } n   return ret + w[id]; n } n long long ad = 0; n bool sear(int id, int fr) { n   for (int j = 0; j < G[id].size(); j++) { n     int to = G[id][j]; n     if (to == fr) continue; n     if (!iseda[to]) { n       ad += w[id]; n       w[id] = 0; n       return true; n     } n     if (sear(to, id)) { n       ad += w[id]; n       w[id] = 0; n       return true; n     } n   } n   return false; n } n void solve() { n   cin >> n >> m; n   for (int i = 0; i < n; i++) { n     cin >> w[i]; n   } n   for (int i = 0; i < m; i++) { n     int a, b; n     cin >> a >> b; n     a--; n     b--; n     G[a].push_back(b); n     G[b].push_back(a); n   } n   int s; n   cin >> s; n   s--; n   queue<int> q; n   q.push(s); n   canr[s] = true; n   while (!q.empty()) { n     int id = q.front(); n     q.pop(); n     for (int j = 0; j < G[id].size(); j++) { n       int to = G[id][j]; n       if (canr[to]) continue; n       canr[to] = true; n       q.push(to); n     } n   } n   int cnt = 0; n   int cnte = 0; n   for (int i = 0; i < n; i++) { n     if (canr[i]) { n       cnt++; n       cnte += G[i].size(); n     } n   } n   cnte /= 2; n   if (cnt == cnte + 1) { n     long long ans = 0; n     for (int j = 0; j < G[s].size(); j++) { n       ans = max(ans, dfs(G[s][j], s)); n     } n     cout << ans + w[s] << endl; n   } else { n     queue<int> q; n     for (int i = 0; i < n; i++) { n       if (canr[i]) { n         c[i] = G[i].size(); n       } n     } n     for (int i = 0; i < n; i++) { n       if (canr[i]) { n         if (c[i] == 1) { n           q.push(i); n           iseda[i] = true; n           score[i] = 0; n         } n       } n     } n     while (!q.empty()) { n       int id = q.front(); n       q.pop(); n       for (int j = 0; j < G[id].size(); j++) { n         int to = G[id][j]; n         if (iseda[to]) continue; n         score[to] = max(score[to], score[id] + w[id]); n         c[to]--; n         if (c[to] == 1) { n           q.push(to); n           iseda[to] = true; n         } n       } n     } n     if (iseda[s]) { n       long long ans = 0; n       long long ma = 0; n       sear(s, -1); n       for (int i = 0; i < n; i++) { n         if (canr[i] && !iseda[i]) { n           ans += w[i]; n           for (int j = 0; j < G[i].size(); j++) { n             int to = G[i][j]; n             if (iseda[to]) { n               ma = max(ma, dfs(to, i)); n             } n           } n         } n       } n       cout << ans + ad + ma << endl; n     } else { n       long long ans = 0; n       long long ma = 0; n       for (int i = 0; i < n; i++) { n         if (canr[i] && !iseda[i]) { n           ans += w[i]; n           for (int j = 0; j < G[i].size(); j++) { n             int to = G[i][j]; n             if (iseda[to]) { n               ma = max(ma, dfs(to, i)); n             } n           } n         } n       } n       cout << ans + ma << endl; n     } n   } n } n signed main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 2e5 + 7; n const int K = 20; n int go[N][K]; n int tin[N], tout[N]; n vector<int> g[N]; n int timer = 0; n int perm[N]; n int where[N]; n pair<int, int> t[4 * N]; n int n; n void dfs(int u, int p = 0) { n   go[u][0] = p; n   tin[u] = timer++; n   for (int i = 1; i < K; ++i) { n     go[u][i] = go[go[u][i - 1]][i - 1]; n   } n   for (auto v : g[u]) { n     if (v != p) { n       dfs(v, u); n     } n   } n   tout[u] = timer++; n } n bool anc(int a, int b) { return tin[a] <= tin[b] && tout[b] <= tout[a]; } n int lca(int a, int b) { n   if (anc(a, b)) return a; n   for (int k = K - 1; k >= 0; --k) { n     if (!anc(go[a][k], b)) { n       a = go[a][k]; n     } n   } n   return go[a][0]; n } n pair<int, int> null = {-1, -1}; n pair<int, int> empt = {-2, -2}; n int d; n bool on_vert(int top, int bot, int v) { return anc(top, v) && anc(v, bot); } n bool check(pair<int, int> p, pair<int, int> a) { n   int lc = lca(p.first, p.second); n   if (!on_vert(lc, p.first, a.first) && !on_vert(lc, p.second, a.first)) n     return false; n   if (!on_vert(lc, p.first, a.second) && !on_vert(lc, p.second, a.second)) n     return false; n   return true; n } n pair<int, int> merge(pair<int, int> a, pair<int, int> b) { n   if (a.first == -1 || b.first == -1) return {-1, -1}; n   if (check({a.first, b.first}, a) && check({a.first, b.first}, b)) n     return {a.first, b.first}; n   if (check({a.first, b.second}, a) && check({a.first, b.second}, b)) n     return {a.first, b.second}; n   if (check({a.second, b.first}, a) && check({a.second, b.first}, b)) n     return {a.second, b.first}; n   if (check({a.second, b.second}, a) && check({a.second, b.second}, b)) n     return {a.second, b.second}; n   if (check({a.first, a.second}, a) && check({a.first, a.second}, b)) n     return {a.first, a.second}; n   if (check({b.first, b.second}, a) && check({b.first, b.second}, b)) n     return {b.first, b.second}; n   return {-1, -1}; n } n void build(int l, int r, int v) { n   if (l == r) { n     t[v] = {where[l], where[l]}; n   } else { n     int m = (l + r) >> 1; n     build(l, m, 2 * v + 1); n     build(m + 1, r, 2 * v + 2); n     t[v] = merge(t[2 * v + 1], t[2 * v + 2]); n   } n } n void modify(int ind, int l, int r, int v) { n   if (l == r) { n     t[v] = {where[ind], where[ind]}; n   } else { n     int m = (l + r) >> 1; n     if (ind <= m) n       modify(ind, l, m, 2 * v + 1); n     else n       modify(ind, m + 1, r, 2 * v + 2); n     t[v] = merge(t[2 * v + 1], t[2 * v + 2]); n   } n } n void update(int a, int b) { n   int pa = perm[a]; n   int pb = perm[b]; n   swap(where[perm[a]], where[perm[b]]); n   swap(perm[a], perm[b]); n   modify(pa, 0, n - 1, 0); n   modify(pb, 0, n - 1, 0); n } n int get(int v, int l, int r, pair<int, int> cur) { n   if (l == r) { n     return l; n   } n   int m = (l + r) >> 1; n   auto mem = merge(t[2 * v + 1], cur); n   if (mem.first != -1) return get(2 * v + 2, m + 1, r, mem); n   return get(2 * v + 1, l, m, cur); n } n int mex() { n   if (t[0].first != -1) return n; n   return get(0, 0, n - 1, {where[0], where[0]}); n } n void init() { n   cin >> n; n   for (int i = 0; i < n; ++i) { n     cin >> perm[i]; n     where[perm[i]] = i; n   } n   for (int i = 1; i < n; ++i) { n     int p; n     cin >> p; n     --p; n     g[p].push_back(i); n   } n   dfs(0); n   build(0, n - 1, 0); n } n void solve() { n   int q; n   cin >> q; n   while (q--) { n     int t; n     cin >> t; n     if (t == 1) { n       int a, b; n       cin >> a >> b; n       --a, --b; n       update(a, b); n     } else { n       cout << mex() <<   n ; n     } n   } n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.setf(ios::fixed); n   cout.precision(20); n   init(); n   solve(); n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   cin.tie(0)->sync_with_stdio(0); n   string s; n   cin >> s; n   string t = s; n   int n = s.size(); n   auto check = [&]() { n     for (int i = 0; i <= 0; i++) { n       int mx0 = 0, mx1 = 0, mmx0 = 0, mmx1 = 0; n       for (int j = i; j < n; j++) { n         if (s[j] ==  1 ) { n           mx1 = max(mx1 + 1, mx0 + 1); n         } else { n           mx0++; n         } n         if (t[j] ==  1 ) { n           mmx1 = max(mmx1 + 1, mmx0 + 1); n         } else { n           mmx0++; n         } n         if (max(mx0, mx1) != max(mmx0, mmx1)) { n           return 0; n         } n       } n     } n     return 1; n   }; n   for (int i = 0; i < n; i++) { n     if (s[i] ==  0 ) continue; n     s[i] =  0 ; n     if (!check()) { n       s[i] =  1 ; n     } n   } n   cout << s <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n const long long MAX1 = 1e6 + 10; n long long T, a, b, c, d, e, f, x, y, z, l, r, mid, num[MAX1], cnt, flag, n     p1[55][55], p2[55][55]; n string s, t; n stack<char> st; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   ; n   cin >> s; n   a = s.size(); n   for (int i = 0; i < a; ++i) { n     if (st.empty()) n       st.push(s[i]); n     else if (st.top() == s[i]) { n       st.pop(); n     } else n       st.push(s[i]); n   } n   if (st.empty()) n     cout <<  Yes ; n   else n     cout <<  No ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   int l; n   cin >> l; n   string s; n   cin >> s; n   if (s.length() % l != 0) { n     string pattern(l,  0 ); n     pattern[0] =  1 ; n     int times = s.length() / l + 1; n     while (times-- > 0) { n       cout << pattern; n     } n   } else { n     string pattern = s.substr(0, l); n     int times = s.length() / l; n     string presumablyAnswer; n     for (int i = 0; i < times; ++i) { n       presumablyAnswer += pattern; n     } n     if (presumablyAnswer > s) { n       cout << presumablyAnswer << endl; n     } else { n       if (count(pattern.begin(), pattern.end(),  9 ) < l) { n         int i = (int)pattern.size() - 1; n         while (pattern[i] ==  9 ) { n           pattern[i] =  0 ; n           --i; n         } n         ++pattern[i]; n         for (int i = 0; i < times; ++i) { n           cout << pattern; n         } n       } else { n         fill(pattern.begin(), pattern.end(),  0 ); n         pattern[0] =  1 ; n         for (int i = 0; i <= times; ++i) { n           cout << pattern; n         } n       } n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int cnt[5005]; n int nearest[5005]; n int main(void) { n   int n, m; n   cin >> n >> m; n   memset(cnt, 0, sizeof(cnt)); n   for (int i = 0; i < m; i++) { n     int a, b; n     cin >> a >> b; n     b = b > a ? b : b + n; n     if (!cnt[a] || b < nearest[a]) nearest[a] = b; n     cnt[a]++; n   } n   for (int i = 1; i <= n; ++i) { n     int time = 0; n     for (int j = 1; j <= n; ++j) { n       int x = (cnt[j] - 1) * n + (nearest[j] + (i > j ? n : 0)); n       time = max(x, time); n     } n     cout << time - i <<    ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int inf = 0x3f3f3f3f, mod = 1e9 + 7; n const int maxn = 2e5 + 100, mxn = 5e5 + 100; n const long double PI = acos(-1.0); n long long gcd(long long a, long long b) { return (a ? gcd(b % a, a) : b); } n void exgcd(long long a, long long b, long long& d, long long& x, long long& y) { n   if (!b) { n     d = a; n     x = 1; n     y = 0; n   } else { n     exgcd(b, a % b, d, y, x); n     y -= x * (a / b); n   } n } n long long power(long long a, long long n) { n   long long p = 1; n   while (n > 0) { n     if (n % 2) { n       p = p * a; n     } n     n >>= 1; n     a *= a; n   } n   return p; n } n long long power(long long a, long long n, long long mod) { n   long long p = 1; n   while (n > 0) { n     if (n % 2) { n       p = p * a; n       p %= mod; n     } n     n >>= 1; n     a *= a; n     a %= mod; n   } n   return p % mod; n } n int n, m; n double a[maxn], b[maxn], p, q; n double f(double x) { n   double minn = 100000.0; n   for (int i = 1; i <= n; i++) minn = min(minn, (1 - b[i] * x) / a[i]); n   return p * minn + q * x; n } n int main() { n   while (cin >> n >> p >> q) { n     double maxx = 0; n     for (int i = 1; i <= n; i++) { n       cin >> a[i] >> b[i]; n       maxx = max(b[i], maxx); n     } n     double l = 0, r = 1 / maxx; n     for (int i = 1; i <= 400; i++) { n       double mid = (l + r) / 2.0; n       double midmid = (mid + r) / 2.0; n       if (f(mid) >= f(midmid)) n         r = midmid; n       else n         l = mid; n     } n     printf( %.10f n , f((l + r) / 2.0)); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<int> final; n void seive(int n) { n   vector<bool> prime(n + 1, true); n   prime[0] = false; n   prime[1] = false; n   for (long long int i = 2; i <= (long long int)sqrt(n); ++i) { n     if (prime[i]) { n       for (int j = i * i; j <= n; j += i) prime[j] = false; n     } n   } n   for (long long int i = 2; i <= n; ++i) { n     if (prime[i]) final.push_back(i); n   } n } n const int mod = 998244353; n const int mx = 2e5 + 1; n vector<int> fact(mx); n vector<int> inv_fact(mx); n long long int expo_pow(long long int a, long long int b) { n   a %= mod; n   long long int res = 1; n   while (b) { n     if (b & 1) res = (res * a) % mod; n     a = (a * a) % mod; n     b >>= 1; n   } n   return res; n } n void precompute() { n   fact[0] = 1; n   for (long long int i = 1; i <= mx; ++i) fact[i] = (fact[i - 1] * i) % mod; n   for (long long int i = 0; i <= mx; ++i) n     inv_fact[i] = expo_pow(fact[i], mod - 2) % mod; n } n long long int compute(long long int n, long long int r) { n   return fact[n] * 1ll * inv_fact[r] % mod * inv_fact[n - r] % mod; n } n struct cmp { n   bool operator()(long long int const& a, long long int const& b) { n     return a > b; n   } n }; n int main() { n   std::ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   long long int t = 1; n   while (t--) { n     long long int n, k, gain, sum = 0; n     cin >> n >> k; n     vector<long long int> cost(n), a(n); n     for (long long int i = 0; i < n; ++i) cin >> a[i]; n     cin >> gain; n     for (long long int i = 0; i < n; ++i) cin >> cost[i]; n     priority_queue<long long int, vector<long long int>, cmp> pq; n     bool cond = true; n     for (long long int i = 0; i < n; ++i) { n       pq.push(cost[i]); n       if (k < a[i]) { n         while (!pq.empty() && k < a[i]) { n           k += gain; n           sum += pq.top(); n           pq.pop(); n         } n         if (k >= a[i]) n           continue; n         else { n           cond = false; n           break; n         } n         cout <<   n ; n       } n     } n     if (cond) n       cout << sum <<   n ; n     else n       cout << -1 <<   n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   long long int n, w, l; n   cin >> n >> l >> w; n   long double p[n + 1]; n   long long int b[n + 1]; n   for (long long int i = 1; i <= n; i++) { n     cin >> p[i]; n     p[i] /= 100; n   } n   for (long long int i = 1; i <= n; i++) { n     cin >> b[i]; n     b[i]++; n   } n   long double a[n + 1][n + 1][n + 1]; n   for (long long int i = 0; i <= n; i++) { n     for (long long int j = 0; j <= n; j++) { n       for (long long int k = 0; k <= n; k++) { n         a[i][j][k] = 0; n       } n     } n   } n   long double x, y; n   long long int z = 0; n   if (w > n) w = n; n   a[0][0][w] = 1; n   for (long long int i = 1; i <= n; i++) { n     for (long long int j = 0; j <= i; j++) { n       for (long long int k = 0; k <= n; k++) { n         z = b[i]; n         z += k; n         if (z > n) z = n; n         if (j > 0) a[j][i - j][z] += ((a[j - 1][i - j][k]) * (p[i])); n         if ((i - j) > 0) n           a[j][i - j][k] += ((a[j][i - j - 1][k]) * (1.0 - p[i])); n       } n     } n   } n   long double ans = 0.0; n   for (long long int i = 0; i <= n; i++) { n     for (long long int j = 0; j <= n; j++) { n       for (long long int k = 0; k <= n; k++) { n         if ((k >= i) && (i >= l) && ((i + j) == n)) { n           ans += a[i][j][k]; n         } n       } n     } n   } n   cout << fixed << setprecision(6) << ans; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   long long n; n   cin >> n; n   multiset<long long> A, B; n   for (long long i = 0; i < n; i++) { n     long long num; n     cin >> num; n     A.insert(-num); n   } n   for (long long i = 0; i < n; i++) { n     long long num; n     cin >> num; n     B.insert(-num); n   } n   long long ansa = 0, ansb = 0; n   while (!A.empty() || !B.empty()) { n     long long v1 = 0, v2 = 0; n     if (A.size() > 0) { n       v1 = -(*A.begin()); n       if (B.size() > 0) { n         v2 = -(*B.begin()); n         if (v2 >= v1) { n           auto it = B.begin(); n           B.erase(it); n         } else { n           auto it = A.begin(); n           A.erase(it); n           ansa += v1; n         } n       } else { n         auto it = A.begin(); n         A.erase(it); n         ansa += v1; n       } n     } else { n       if (B.size() > 0) { n         auto it = B.begin(); n         B.erase(it); n       } n     } n     v1 = 0, v2 = 0; n     if (B.size() > 0) { n       long long v1 = -(*B.begin()); n       if (A.size() > 0) { n         v2 = -(*A.begin()); n         if (v2 >= v1) { n           auto it = A.begin(); n           A.erase(it); n         } else { n           auto it = B.begin(); n           B.erase(it); n           ansb += v1; n         } n       } else { n         auto it = B.begin(); n         B.erase(it); n         ansb += v1; n       } n     } else { n       if (A.size() > 0) { n         auto it = A.begin(); n         A.erase(it); n       } n     } n   } n   cout << ansa - ansb; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m; n int adj[5000005], can[5000005], fuck[5000005]; 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()) x = x * 10 + ch -  0 ; n   return x * f; n } n int main() { n   n = read(), m = read(); n   if (n == 1) { n     puts( 0 ); n     return 0; n   } n   for (int i = 0; i < n; i++) adj[i] = 1 << i; n   while (m--) { n     int x = read() - 1, y = read() - 1; n     adj[x] |= 1 << y, adj[y] |= 1 << x; n   } n   int all = (1 << n) - 1; n   bool flag = 1; n   for (int i = 0; i < n; i++) n     if (adj[i] != all) flag = 0; n   if (flag) { n     puts( 0 ); n     return 0; n   } n   for (int i = 0; i < n; i++) can[1 << i] = 1, fuck[1 << i] = adj[i]; n   for (int i = 1; i < (1 << n); i++) n     if (can[i]) { n       for (int j = 0; j < n; j++) n         if ((~i >> j & 1) && (fuck[i] >> j & 1)) n           can[1 << j | i] = 1, fuck[1 << j | i] = fuck[i] | adj[j]; n     } n   int ans = n, mask = 0; n   for (int i = 1; i < (1 << n); i++) n     if (can[i] && (fuck[i] == all)) { n       int res = __builtin_popcount(i); n       if (ans > res) ans = res, mask = i; n     } n   printf( %d n , ans); n   for (int i = 0; i < n; i++) n     if (mask >> i & 1) printf( %d  , i + 1); n   puts(  ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e5 + 7; n int n; n int dig[N]; n vector<int> ans[N]; n bool vis[N]; n int main() { n   cin >> n; n   vector<int> x; n   for (int i = (0); i < (n); i++) scanf( %d , &dig[i]), x.push_back(dig[i]); n   sort((x).begin(), (x).end()); n   for (int i = (0); i < (n); i++) n     dig[i] = lower_bound((x).begin(), (x).end(), dig[i]) - x.begin(); n   memset(vis, false, sizeof(vis)); n   int ned = 0; n   for (int i = (0); i < (n); i++) n     if (!vis[i]) { n       for (int u = i; !vis[u]; u = dig[u]) vis[u] = true, ans[ned].push_back(u); n       ned++; n     } n   printf( %d n , ned); n   for (int i = (0); i < (ned); i++) { n     printf( %d , ((int)(ans[i]).size())); n     for (int j = (0); j < (((int)(ans[i]).size())); j++) n       printf(  %d , ans[i][j] + 1); n     printf(  n ); n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   string s1, s2; n   cin >> s1 >> s2; n   for (int i = 0; i < s1.size(); i++) { n     if (s1[i] <= 90) s1[i] += 32; n     if (s2[i] <= 90) s2[i] += 32; n     if (s1[i] > s2[i]) { n       cout << 1 << endl; n       return 0; n     } else if (s1[i] < s2[i]) { n       cout << -1 << endl; n       return 0; n     } n   } n   cout << 0 << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   int t; n   cin >> t; n   while (t--) { n     int n, m; n     cin >> n >> m; n     char a[n][m]; n     for (int i = 0; i < n; i++) { n       for (int j = 0; j < m; j++) { n         cin >> a[i][j]; n       } n     } n     int ans = 0; n     for (int j = 0; j < m - 1; j++) { n       if (a[n - 1][j] ==  D ) { n         ans++; n       } n     } n     for (int i = 0; i < n - 1; i++) { n       if (a[i][m - 1] ==  R ) { n         ans++; n       } n     } n     cout << ans <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAX_N = 1000 + 20, MAX_K = 5 + 2; n int a[MAX_K][MAX_N]; n int dp[MAX_N]; n map<int, int> D, ke; n int ans; n int n, k; n int main() { n   ios::sync_with_stdio(false); n   cin >> n >> k; n   for (int i(0); i < int(k); ++i) n     for (int j(0); j < int(n); ++j) { n       cin >> a[i][j]; n       --a[i][j]; n     } n   for (int i(0); i < int(n); ++i) D[a[0][i]] = i; n   for (int i(0); i < int(k); ++i) n     for (int j(0); j < int(n); ++j) a[i][j] = D[a[i][j]]; n   for (int i(0); i < int(n); ++i) { n     ke.clear(); n     dp[i] = 1; n     for (int j(0); j < int(k); ++j) n       for (int l = 0; l < n && a[j][l] != i; ++l) ke[a[j][l]]++; n     for (int j(0); j < int(i); ++j) n       if (ke[j] == k) dp[i] = max(dp[i], dp[j] + 1); n   } n   for (int i(0); i < int(n); ++i) ans = max(ans, dp[i]); n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n using ll = long long; n using ld = long double; n const char nl =   n ; n using vi = vector<int>; n using vll = vector<ll>; n mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count()); n template <typename... Args, template <typename...> typename T> n string to_string(T<Args...> const &); n string to_string(string const &s) { return     + s +    ; } n string to_string(char const &c) { return to_string(string(1, c)); } n string to_string(char const *c) { return to_string(string(c)); } n string to_string(bool const &b) { return (b ?  T  :  F ); } n template <typename... Args> n string to_string(pair<Args...> const &p) { n   return  (  + to_string(p.first) +  ,   + to_string(p.second) +  ) ; n } n template <typename... Args, template <typename...> typename T> n string to_string(T<Args...> const &S) { n   string s =  { ; n   for (auto const &e : S) s +=     + to_string(e); n   s +=   } ; n   return s; n } n template <typename Tail> n void debug_out(Tail t) { n   cerr <<  [   << to_string(t) <<   ]  << endl; n } n template <typename Head, typename... Tail> n void debug_out(Head h, Tail... t) { n   cerr <<     << to_string(h) <<  , ; n   debug_out(t...); n } n template <typename T> n void dbr(T lb, T ub) { n   cerr <<  { ; n   for (auto it = lb; it != ub; it++) cerr <<     << to_string(*it); n   cerr <<   }  << endl; n } n template <typename T, typename Comp = less<T>> n bool smin(T &mem, T const &v, Comp const &cmp = Comp()) { n   return cmp(v, mem) ? mem = v, true : false; n } n template <typename T, typename Comp = less<T>> n bool smax(T &mem, T const &v, Comp const &cmp = Comp()) { n   return cmp(mem, v) ? mem = v, true : false; n } n const string getEnvVar(string const &key) { n   char *val = getenv(key.c_str()); n   return val == NULL ? string(  ) : string(val); n } n vector<string> splitString(const string &str, const string &delim =    ) { n   vector<string> tokens; n   size_t prev = 0, pos = 0; n   do { n     pos = str.find(delim, prev); n     if (pos == string::npos) pos = str.length(); n     string token = str.substr(prev, pos - prev); n     if (!token.empty()) tokens.push_back(token); n     prev = pos + delim.length(); n   } while (pos < str.length() && prev < str.length()); n   return tokens; n } n void header() { n   ios::sync_with_stdio(false); n   cin.tie(nullptr); n } n int main() { n   header(); n   string enc, dec =   ; n   cin >> enc; n   for (int i = 0; i < enc.length(); i++) { n     if (enc[i] ==  . ) { n       dec +=  0 ; n     } else { n       if (enc[i + 1] ==  . ) n         dec +=  1 ; n       else n         dec +=  2 ; n       i++; n     } n   } n   cout << dec << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, x, y, need4demon, puppets; n   cin >> n >> x >> y; n   need4demon = ceil((y / 100.0) * n); n   if (x >= need4demon) n     puppets = 0; n   else n     puppets = need4demon - x; n   cout << puppets << endl; n } n


#include <bits/stdc++.h> n using namespace std; n long long n, a[1231231]; n long long mark[1231231]; n int main() { n   cin >> n; n   for (int i = 1; i <= n; i++) cin >> a[i]; n   mark[1] = 1; n   for (int i = 2; i <= n; i++) { n     mark[i] = mark[i - 1]; n     mark[i] = max(mark[i], a[i] + 1); n   } n   for (int i = n - 1; i >= 1; i--) { n     mark[i] = max(mark[i], mark[i + 1] - 1); n   } n   long long ans = 0; n   for (int i = 1; i <= n; i++) { n     ans += max(0ll, (mark[i] - 1 - a[i])); n   } n   cout << ans << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int a[1000000], n; n int main() { n   scanf( %d , &n); n   for (int i = 1; i <= n; i++) scanf( %d , &a[i]); n   sort(a + 1, a + n + 1); n   int i = 1, j = n / 2 + 1; n   int ans = n; n   while (true) { n     if (i > n / 2) break; n     if (j > n) break; n     if (2 * a[i] <= a[j]) { n       i++; n       j++; n       ans--; n     } else n       j++; n   } n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n inline int read() { n   int sum = 0; n   char c = getchar(); n   bool f = 0; n   while (c <  0  || c >  9 ) { n     if (c ==  - ) f = 1; n     c = getchar(); n   } n   while (c >=  0  && c <=  9 ) { n     sum = sum * 10 + c -  0 ; n     c = getchar(); n   } n   if (f) return -sum; n   return sum; n } n const int N = 200005; n const int orz = 2000000; n const int otz = 1000000005; n int n, K, d, a[N], b[N], c[N]; n namespace Seg { n int lazy[N * 4], t[N * 4], v, rch; n inline void push(int p) { n   if (!lazy[p]) return; n   int x = lazy[p], l = p << 1, r = (l | 1); n   t[l] += x; n   t[r] += x; n   lazy[l] += x; n   lazy[r] += x; n   lazy[p] = 0; n } n inline void ch(int p, int l, int r, int L, int R) { n   if (l == L && r == R) { n     lazy[p] += v; n     t[p] += v; n     return; n   } n   int mid = (l + r) >> 1; n   push(p); n   if (R <= mid) n     ch(p << 1, l, mid, L, R); n   else if (L > mid) n     ch(p << 1 | 1, mid + 1, r, L, R); n   else n     ch(p << 1, l, mid, L, mid), ch(p << 1 | 1, mid + 1, r, mid + 1, R); n   t[p] = min(t[p << 1], t[p << 1 | 1]); n } n inline void ask(int p, int l, int r) { n   if (l == r) { n     rch = l; n     return; n   } n   int mid = (l + r) >> 1; n   push(p); n   if (t[p << 1] < K) n     ask(p << 1, l, mid); n   else n     ask(p << 1 | 1, mid + 1, r); n } n }  // namespace Seg n using namespace Seg; n int s1[N], s2[N], t1, t2; n map<int, int> H; n int main() { n   int i, r, st = 1, ed = 1, ans = 1, ino = 0; n   n = read(); n   K = read(); n   d = read(); n   for (i = 1; i <= n; i++) a[i] = read() + otz; n   if (!d) { n     for (i = 1; i <= n; i = r) { n       r = i; n       while (r <= n && a[r] == a[i]) r++; n       if (r - i > ans) ans = r - i, st = i, ed = r - 1; n     } n     printf( %d %d , st, ed); n     return 0; n   } n   for (i = 1; i <= n; i++) b[i] = a[i] % d, c[i] = a[i] / d; n   a[0] = a[1]; n   b[0] = b[1]; n   v = orz; n   ch(1, 1, n, 1, n); n   for (i = 1; i <= n; i++) { n     if (b[i] != b[i - 1]) v = orz, ch(1, 1, n, ino + 1, i - 1), ino = i - 1; n     if (H[a[i]] > ino) v = orz, ch(1, 1, n, ino + 1, H[a[i]]), ino = H[a[i]]; n     while (t1 && a[i] > a[s1[t1]]) n       v = c[i] - c[s1[t1]], ch(1, 1, n, s1[t1 - 1] + 1, s1[t1]), t1--; n     while (t2 && a[i] < a[s2[t2]]) n       v = c[s2[t2]] - c[i], ch(1, 1, n, s2[t2 - 1] + 1, s2[t2]), t2--; n     H[a[i]] = i; n     s1[++t1] = i; n     s2[++t2] = i; n     v = -1; n     ch(1, 1, n, 1, i); n     v = -orz; n     ch(1, 1, n, i, i); n     ask(1, 1, n); n     if (i - rch + 1 > ans) ans = i - rch + 1, st = rch, ed = i; n   } n   printf( %d %d , st, ed); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n inline int lowbit(int x) { return x & (-x); } n int n, K, a[16], sum[1 << 16], mp[1 << 16], b[16], fac[2001], vl[2001]; n bitset<2001> f[1 << 16]; n vector<int> v[16]; n void construct(int cur, int sum) { n   if (!cur) return; n   if (sum * K <= 2000 && f[cur][sum * K]) { n     for (int j = 0; j < n; j++) n       if ((cur >> j) & 1) b[j]++; n     construct(cur, sum * K); n   } else { n     for (int j = 0; j < n; j++) { n       if (((cur >> j) & 1) && sum >= a[j] && f[cur ^ (1 << j)][sum - a[j]]) { n         construct(cur ^ (1 << j), sum - a[j]); n         return; n       } n     } n   } n } n int main() { n   scanf( %d%d , &n, &K); n   for (int i = 0; i < n; i++) scanf( %d , &a[i]); n   for (int i = 1; i * K <= 2000; i++) fac[i * K] = fac[i] + 1; n   for (int i = 1; i <= 2000; i++) { n     vl[i] = i; n     for (int j = 0; j < fac[i]; j++) vl[i] /= K; n   } n   for (int i = 0; i < n; i++) mp[1 << i] = i; n   for (int i = 1; i < (1 << n); i++) { n     int t = lowbit(i); n     sum[i] = sum[i ^ t] + a[mp[t]]; n   } n   f[0][0] = 1; n   for (int i = 0; i < (1 << n); i++) { n     for (int j = sum[i] / K * K; j >= 0; j -= K) n       f[i][j / K] = f[i][j / K] | f[i][j]; n     for (int j = 0; j < n; j++) n       if (!((i >> j) & 1)) f[i | (1 << j)] |= f[i] << a[j]; n   } n   if (f[(1 << n) - 1][1]) { n     puts( YES ); n     construct((1 << n) - 1, 1); n     int bpos = 0; n     for (int i = 0; i < n; i++) bpos = max(bpos, b[i]), v[b[i]].push_back(a[i]); n     for (int i = 1; i < n; i++) { n       while (v[bpos].empty()) bpos--; n       int x = v[bpos].size(); n       printf( %d %d n , v[bpos][x - 1], v[bpos][x - 2]); n       int cur = v[bpos][x - 1] + v[bpos][x - 2]; n       v[bpos].pop_back(); n       v[bpos].pop_back(); n       v[bpos - fac[cur]].push_back(vl[cur]); n     } n   } else n     puts( NO ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, k; n pair<int, int> d[3] = {{0, 1}, {1, 0}, {1, 1}}; n int b[550][550]; n void rec(int k) { n   if (k == 1) { n     b[0][0] = 1; n     return; n   } n   rec(k /= 2); n   for (int c = 0; c < 3; c++) { n     for (int i = 0; i < k; i++) { n       for (int j = 0; j < k; j++) { n         b[i + d[c].first * k][j + d[c].second * k] = !b[i][j]; n       } n     } n   } n } n int main() { n   cin >> k; n   n = pow(2, k); n   rec(n); n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < n; j++) cout << (b[i][j] ?  +  :  * ); n     cout << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int inf = 0x7FFFFFFF; n template <class T> n T gcd(T a, T b) { n   return b == 0 ? a : gcd(b, a % b); n } n template <typename T> n T lcm(T a, T b) { n   return a / gcd(a, b) * b; n } n template <class T> n T big_mod(T n, T p, T m) { n   if (p == 0) return (T)1; n   T x = big_mod(n, p / 2, m); n   x = (x * x) % m; n   if (p & 1) x = (x * n) % m; n   return x; n } n template <class T> n T my_pow(T n, T p) { n   if (p == 0) return 1; n   T x = my_pow(n, p / 2); n   x = (x * x); n   if (p & 1) x = (x * n); n   return x; n } n template <class T> n inline T Mod(T n, T m) { n   return (n % m + m) % m; n } n template <class T> n T MIN(T a, T b, T c) { n   return min(a, min(b, c)); n } n template <class T> n T MAX(T a, T b, T c) { n   return max(a, max(b, c)); n } n int n, m, segment[10000]; n int main() { n   cin >> n >> m; n   for (int i = 1; i <= m; i++) { n     int x, y; n     cin >> x >> y; n     for (int j = x; j <= y; j++) { n       segment[j]++; n     } n   } n   for (int i = 1; i <= n; i++) { n     if (segment[i] != 1) { n       printf( %d %d n , i, segment[i]); n       return 0; n     } n   } n   printf( OK n ); n   return 0; n } n


#include <bits/stdc++.h> n #pragma GCC optimize( Ofast ) n using namespace std; n long long cp[200010]; n long long find(long long x) { return cp[x] = cp[x] == x ? x : find(cp[x]); } n void union_(long long x, long long y) { cp[find(x)] = find(y); } n int32_t main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   for (long long j = 0; j < 200010; ++j) cp[j] = j; n   long long m, n; n   cin >> m >> n; n   vector<long long> A(m); n   vector<long long> B(n); n   for (long long i = 0; i < m; ++i) cin >> A[i]; n   for (long long i = 0; i < n; ++i) cin >> B[i]; n   vector<pair<long long, pair<long long, long long>>> edg; n   for (long long u = 0; u < m; ++u) { n     long long s; n     cin >> s; n     for (long long i = 0; i < s; ++i) { n       long long v; n       cin >> v; n       edg.push_back({B[v - 1] + A[u], {u, v - 1ll}}); n     } n   } n   sort((edg).begin(), (edg).end()); n   reverse((edg).begin(), (edg).end()); n   long long ans = 0; n   for (long long k = 0; k < edg.size(); ++k) { n     long long u = edg[k].second.first, v = edg[k].second.second + m; n     if (find(u) == find(v)) { n       ans += edg[k].first; n     } else { n       union_(u, v); n     } n   } n   cout << ans <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n const int DIM = 1e5 + 5; n int szt[DIM], ans[DIM]; n vector<int> edg[DIM]; n map<int, int> son, par, out; n void dfs1(int x, int sz) { n   szt[x] = 1; n   for (int y : edg[x]) { n     dfs1(y, sz); n     szt[x] += szt[y]; n   } n   out[szt[x]]++; n   ans[x] = sz - 1; n   return; n } n void add(map<int, int> &mmp, int x, int val) { n   mmp[x] += val; n   out[x] -= val; n   if (!mmp[x]) mmp.erase(x); n   if (!out[x]) out.erase(x); n   return; n } n void bns(map<int, int> &mmp, int x, int le, int ri, int mnm, int aux) { n   int val = ri, sol = ri; n   while (le <= ri) { n     int md = (le + ri) >> 1; n     map<int, int>::iterator it = mmp.lower_bound(val - md + aux); n     if (it == mmp.end() || it->first + mnm > md + aux) n       le = md + 1; n     else n       ri = md - 1, sol = min(sol, md); n   } n   ans[x] = min(ans[x], sol); n   return; n } n void dfs3(int x, int val, int pos) { n   add(son, szt[x], val); n   for (int y : edg[x]) n     if (y != pos) dfs3(y, val, pos); n   return; n } n void dfs2(int x, int sz, bool oki) { n   int mxm1(-1), mxm2(-1), pos(-1), mnm(sz - 1); n   add(par, szt[x], 1); n   for (int y : edg[x]) { n     mnm = min(mnm, szt[y]); n     if (mxm1 < szt[y]) n       mxm2 = mxm1, mxm1 = szt[y], pos = y; n     else n       mxm2 = max(mxm2, szt[y]); n   } n   if (szt[x] != sz) mnm = min(mnm, sz - szt[x]); n   for (int y : edg[x]) n     if (y != pos) dfs2(y, sz, false); n   if (pos != -1) dfs2(pos, sz, true); n   if (mxm1 >= sz - szt[x]) { n     mxm2 = max(mxm2, sz - szt[x]); n     if (mnm != sz - 1) bns(son, x, mxm2, mxm1, mnm, 0); n   } n   dfs3(x, 1, pos); n   add(par, szt[x], -1); n   if (mxm1 <= sz - szt[x] && mnm != sz - 1) { n     bns(par, x, mxm1, sz - szt[x], mnm, szt[x]); n     bns(out, x, mxm1, sz - szt[x], mnm, 0); n   } n   if (!oki) dfs3(x, -1, -1); n   return; n } n int main(void) { n   int n, r(-1); n   scanf( %d , &n); n   for (int i = 1; i <= n; ++i) { n     int x, y; n     scanf( %d %d , &x, &y); n     if (x == 0) n       r = y; n     else n       edg[x].push_back(y); n   } n   dfs1(r, n); n   dfs2(r, n, true); n   for (int i = 1; i <= n; ++i) printf( %d n , ans[i]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long n; n   while (cin >> n) { n     string s; n     cin >> s; n     long a[10010] = {0}, x = 0, c = 0, l; n     l = s.size(); n     for (int i = 0; i < l; i++) { n       if (i % 2 == 0) { n         a[s[i]]++; n       } else { n         x = s[i] + 32; n         if (a[x] > 0) { n           a[x]--; n         } else n           c++; n       } n     } n     cout << c <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long maxN = 1e6 + 5; n const long long inf = 1e10; n const long long mod = 1e9 + 7; n long long n; n long long x[maxN]; n long long a, b; n int main() { n   ios_base::sync_with_stdio(0); n   cin >> n; n   for (long long i = 1; i <= n; i++) cin >> x[i]; n   cin >> a >> b; n   sort(x + 1, x + 1 + n); n   n = unique(x + 1, x + 1 + n) - (x + 1); n   long long res = 0; n   while (a > b) { n     long long next = a - 1; n     for (long long i = 1; i <= n; i++) { n       long long tmp = a - a % x[i]; n       if (tmp < b) { n         x[i--] = x[--n]; n       } else if (tmp < next) n         next = tmp; n     } n     a = next; n     res++; n   } n   cout << res; n   return 0; n } n


/// By: CooloTran n #include <bits/stdc++.h> n using namespace std; n using ll = long long; n using ull = unsigned long long; n using str = string; n #define st first n #define nd second n #define pb push_back n #define pf push_front n #define lb lower_bound n #define ub upper_bound n #define er equal_range n #define file  TEST  n ll gcd(ll a, ll b) { n  if (b == 0) return a; n  return gcd(b, a % b); n } n ll lcm(ll a, ll b) { n  return a / gcd(a, b) * b; n } n ll MULMOD(ll a, ll b, ll n) { n  if (b == 0) return 0; n  ll tmp = MULMOD(a, b >> 1, n); n  if (b & 1) n  return (tmp * 2 % n + a) % n; n  return tmp * 2 % n; n } n int n; n ll a[200005], cur = 0; n signed main() { n  ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); n  // freopen(file .INP ,  r , stdin); n  // freopen(file .OUT ,  w , stdout); n  cin >> n; n  for (int i = 1; i <= n; ++i) n  cin >> a[i]; n  priority_queue<int, vector<int>, greater<int> > pq; n  for (int i = 1; i <= n; ++i) { n   if (cur + a[i] >= 0) { n    cur += a[i]; n    pq.push(a[i]); n   } n   else { n    if (pq.empty()) n    continue; n    ll tmp = pq.top(); n    pq.pop(); n    if (tmp < a[i]) { n     pq.push(a[i]); n     cur += a[i] - tmp; n    } n    else pq.push(tmp); n   } n  } n  cout << pq.size() <<   n ; n  return 0; n }


#include <bits/stdc++.h> n int cmp(const void* a, const void* b); n int main() { n   long long t = 1; n   while (t--) { n     long long n, n1, n2; n     scanf( %lld%lld%lld , &n, &n1, &n2); n     long long arr[n]; n     for (long long i = 0; i < n; i++) { n       scanf( %lld , &arr[i]); n     } n     qsort(arr, n, sizeof(arr[0]), cmp); n     double s1 = 0, s2 = 0; n     if (n2 < n1) { n       long long x = n2; n       n2 = n1; n       n1 = x; n     } n     if (1) { n       for (long long i = 0; i < n1; i++) s1 += arr[i]; n       for (long long i = n1; i < n1 + n2; i++) s2 += arr[i]; n     } n     printf( %f n , s1 / n1 + s2 / n2); n   } n   return 0; n } n int cmp(const void* a, const void* b) { return (*(int*)b - *(int*)a); } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long n, k; n   cin >> n >> k; n   long long a[k + 1]; n   for (long long i = 0; i < k; i++) { n     cin >> a[i]; n   } n   long long c4 = n, c2 = 2 * n; n   long long o1 = 0; n   for (long long i = 0; i < k; i++) { n     while (c4 > 0 && a[i] > 3) { n       c4 -= 1; n       a[i] -= 4; n     } n     while (c2 >= 2 && a[i] > 3) { n       c2 -= 2; n       a[i] -= 4; n     } n     if (a[i] >= 3) { n       while (c4 > 0 && a[i] > 0) { n         c4 -= 1; n         a[i] -= 3; n       } n       while (c2 >= 2 && a[i] > 0) { n         c2 -= 2; n         a[i] -= 3; n       } n     } n     if (a[i] >= 2) { n       while (c2 > 0 && a[i] > 0) { n         c2 -= 1; n         a[i] -= 2; n       } n       while (c4 > 0 && a[i] > 0) { n         c4 -= 1; n         o1++; n         a[i] -= 2; n       } n     } n     if (a[i] > 0) { n       while (o1 > 0 && a[i] > 0) { n         o1--; n         a[i]--; n       } n       while (c4 > 0 && a[i] > 0) { n         c4 -= 1; n         a[i] -= 1; n         c2++; n       } n       while (a[i] > 0 && c2 > 0) { n         c2 -= 1; n         a[i] -= 1; n       } n     } n     if (a[i] > 0) { n       cout <<  NO  << endl; n       return 0; n     } n   } n   cout <<  YES  << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int i, num = 0; n   for (i = 1; i <= 5; i++) { n     int temp; n     scanf( %d , &temp); n     num += temp; n   } n   if (num % 5 || num == 0) { n     printf( -1 n ); n   } else { n     printf( %d n , num / 5); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n string s; n int main() { n   ios_base::sync_with_stdio(false); n   cin >> s; n   bool f = false; n   for (int i = 0; i < s.size() / 2; i++) n     if (s[0] != s[i] or s[0] != s[s.size() - i - 1]) { n       f = true; n       break; n     } n   if (!f) { n     cout <<  Impossible  << endl; n     return 0; n   } n   for (int i = 1; i < s.size() - 1; i++) { n     string v = s.substr(i, s.size() - i) + s.substr(0, i); n     string w = v; n     reverse(w.begin(), w.end()); n     if (v == w and v != s) { n       cout << 1 << endl; n       return 0; n     } n   } n   cout << 2 << endl; n   return 0; n } n


#include <bits/stdc++.h> n static const int MaxSize = 1e5; n int tree[4 * MaxSize][2]; n static void build(std::vector<int> &v, int vertex, int tl, int tr) { n   if (tl == tr) n     tree[vertex][0] = v[tl]; n   else { n     int tm = (tl + tr) / 2; n     build(v, 2 * vertex + 1, tl, tm + 0); n     build(v, 2 * vertex + 2, tm + 1, tr); n     tree[vertex][0] = n         std::min(tree[2 * vertex + 1][0], tree[2 * vertex + 2][0]); n   } n } n static void update(int vertex, int tl, int tr, int l, int r, int x) { n   if (l > r) return; n   if (l == tl && tr == r) n     tree[vertex][1] += x; n   else { n     int tm = (tl + tr) / 2; n     update(2 * vertex + 1, tl, tm + 0, l, std::min(r, tm + 0), x); n     update(2 * vertex + 2, tm + 1, tr, std::max(l, tm + 1), r, x); n     tree[vertex][0] = n         std::min(tree[2 * vertex + 1][0] + tree[2 * vertex + 1][1], n                  tree[2 * vertex + 2][0] + tree[2 * vertex + 2][1]); n   } n } n static int get(int vertex, int tl, int tr, int l, int r, int x) { n   if (l == tl && tr == r) return tree[vertex][0] + tree[vertex][1]; n   x += tree[vertex][1]; n   int tm = (tl + tr) / 2; n   return std::min( n       x + get(2 * vertex + 1, tl, tm + 0, l, std::min(r, tm + 0), x), n       x + get(2 * vertex + 2, tm + 1, tr, std::max(l, tm + 1), r, x)); n } n static void build(std::vector<int> &v, int n) { build(v, 0, 0, n - 1); } n static void update(int n, int l, int r, int x) { update(0, 0, n - 1, l, r, x); } n static int get(int n, int l, int r) { return get(0, 0, n - 1, l, r, 0); } n int main(int argc, const char *argv[]) { n   std::ios_base::sync_with_stdio(false); n   std::cin.tie(NULL); n   int n, m; n   std::cin >> n >> m; n   std::vector<int> v(n), tree(4 * n); n   for (int i = 0; i < n; i++) std::cin >> v[i]; n   std::vector<std::pair<int, int>> p(m); n   for (int i = 0; i < m; i++) { n     std::cin >> p[i].first >> p[i].second; n     p[i].first--; n     p[i].second--; n   } n   build(v, n); n   std::vector<bool> used(m); n   std::vector<int> t(4 * n), r; n   int result = 0; n   for (int i = 0; i < n; i++) { n     std::vector<int> intervals; n     for (int j = 0; j < m; j++) { n       if (p[j].first <= i && i <= p[j].second) { n         if (used[j]) { n           used[j] = false; n           update(n, p[j].first, p[j].second, 1); n         } n       } else { n         if (!used[j]) update(n, p[j].first, p[j].second, -1); n         intervals.push_back(j + 1); n         used[j] = true; n       } n     } n     int curr = v[i] - get(n, 0, n - 1); n     if (result < curr) { n       result = curr; n       std::swap(r, intervals); n     } n   } n   std::cout << result << std::endl; n   std::cout << r.size() << std::endl; n   if (r.size() > 0) { n     for (int number : r) std::cout << number <<    ; n     std::cout << std::endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int solveA(pair<pair<int, int>, int> x, pair<pair<int, int>, int> y, n            pair<pair<int, int>, int> z) { n   pair<int, int> a = x.first; n   pair<int, int> b = y.first; n   pair<int, int> c = z.first; n   int n = a.first + b.first + c.first; n   if (n != a.second) return -1; n   if (n != b.second) return -1; n   if (n != c.second) return -1; n   cout << n <<   n ; n   for (int i = 1; i <= a.first; ++i) { n     for (int j = 1; j <= a.second; ++j) { n       cout << (char)( A  + x.second); n     } n     cout <<   n ; n   } n   for (int i = 1; i <= b.first; ++i) { n     for (int j = 1; j <= b.second; ++j) { n       cout << (char)( A  + y.second); n     } n     cout <<   n ; n   } n   for (int i = 1; i <= c.first; ++i) { n     for (int j = 1; j <= c.second; ++j) { n       cout << (char)( A  + z.second); n     } n     cout <<   n ; n   } n   return n; n } n int solveB(pair<pair<int, int>, int> x, pair<pair<int, int>, int> y, n            pair<pair<int, int>, int> z) { n   pair<int, int> a = x.first; n   pair<int, int> b = y.first; n   pair<int, int> c = z.first; n   int n = a.first; n   if (n != b.first + c.first) return -1; n   if (n != b.second + a.second) return -1; n   if (n != c.second + a.second) return -1; n   cout << n <<   n ; n   for (int i = 1; i <= a.second; ++i) { n     for (int j = 1; j <= n; ++j) { n       cout << (char)( A  + x.second); n     } n     cout <<   n ; n   } n   for (int i = 1; i <= b.second; ++i) { n     for (int j = 1; j <= b.first; ++j) { n       cout << (char)( A  + y.second); n     } n     for (int j = 1; j <= c.first; ++j) { n       cout << (char)( A  + z.second); n     } n     cout <<   n ; n   } n   return n; n } n int main(int argc, const char* argv[]) { n   pair<int, int> a[3]; n   cin >> a[0].first >> a[0].second; n   cin >> a[1].first >> a[1].second; n   cin >> a[2].first >> a[2].second; n   pair<pair<int, int>, int> b[3]; n   for (int i = 0; i < 8; ++i) { n     if (i & 1) n       b[0].first = a[0]; n     else n       b[0].first = make_pair(a[0].second, a[0].first); n     if (i >> 1 & 1) n       b[1].first = a[1]; n     else n       b[1].first = make_pair(a[1].second, a[1].first); n     if (i >> 2 & 1) n       b[2].first = a[2]; n     else n       b[2].first = make_pair(a[2].second, a[2].first); n     for (int i = 0; i <= 2; ++i) { n       b[i].second = i; n     } n     sort(b, b + 3); n     do { n       int n; n       n = solveA(b[0], b[1], b[2]); n       if (n != -1) { n         return 0; n       } n       n = solveB(b[0], b[1], b[2]); n       if (n != -1) { n         return 0; n       } n     } while (next_permutation(b, b + 3)); n   } n   cout << -1; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 3005; n const int inf = 5e8; n vector<int> to[N], from[N]; n int d[N][N], b[N][N]; n vector<pair<int, int>> go[N], back[N]; n bool done[N]; n int n; n void bfs(vector<int>* edge, int dist[N][N], vector<pair<int, int>>* ret) { n   for (int i = 0; i < N; i++) { n     for (int j = 0; j < N; j++) { n       dist[i][j] = -inf; n     } n   } n   for (int i = 1; i <= n; i++) { n     memset(done, 0, sizeof(done)); n     queue<int> q; n     done[i] = 1; n     q.push(i); n     dist[i][i] = 0; n     while (!q.empty()) { n       int now = q.front(); n       q.pop(); n       for (auto it : edge[now]) { n         if (!done[it]) { n           done[it] = 1; n           dist[i][it] = dist[i][now] + 1; n           q.push(it); n         } n       } n     } n   } n   for (int i = 1; i <= n; i++) { n     for (int j = 1; j <= n; j++) { n       if (i != j) { n         ret[i].emplace_back(dist[i][j], j); n       } n     } n     sort(ret[i].begin(), ret[i].end()); n     reverse(ret[i].begin(), ret[i].end()); n   } n } n pair<int, int> get(int p, vector<pair<int, int>>* ret, vector<int>& ojo) { n   for (auto it : ret[p]) { n     bool ada = 0; n     for (auto j : ojo) { n       if (it.second == j) { n         ada = 1; n         break; n       } n     } n     if (!ada) return it; n   } n   return {-inf, -inf}; n } n int main() { n   int m; n   scanf( %d %d , &n, &m); n   while (m--) { n     int u, v; n     scanf( %d %d , &u, &v); n     to[u].push_back(v); n     from[v].push_back(u); n   } n   bfs(to, d, go); n   bfs(from, b, back); n   int ans = -inf, ai, aj, ak, al; n   for (int i = 1; i <= n; i++) { n     for (int j = 1; j <= n; j++) { n       assert(d[i][j] == b[j][i]); n       if (i == j) continue; n       vector<int> ojo = {i, j}; n       pair<int, int> lef = get(i, back, ojo); n       pair<int, int> rig = get(j, go, ojo); n       vector<int> tmp; n       if (lef.second == rig.second) { n         tmp = {i, j, lef.second}; n         pair<int, int> l = get(i, back, tmp); n         int cur = d[i][j] + l.first + rig.first; n         if (cur > ans) { n           ans = cur; n           ai = l.second; n           aj = i; n           ak = j; n           al = rig.second; n         } n         pair<int, int> r = get(j, go, tmp); n         cur = d[i][j] + lef.first + r.first; n         if (cur > ans) { n           ans = cur; n           ai = lef.second; n           aj = i; n           ak = j; n           al = r.second; n         } n       } else { n         int cur = d[i][j] + lef.first + rig.first; n         if (cur > ans) { n           ans = cur; n           ai = lef.second; n           aj = i; n           ak = j; n           al = rig.second; n         } n       } n     } n   } n   printf( %d %d %d %d n , ai, aj, ak, al); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int dr[] = {-1, -1, -1, 0, 0, 1, 1, 1}; n int dc[] = {-1, 0, 1, -1, 1, -1, 0, 1}; n struct node { n   int r, c, dist; n   node() {} n   node(int _r, int _c, int _dist) : r(_r), c(_c), dist(_dist) {} n }; n set<pair<int, int> > valid, visited; n int main() { n   int r0, c0, r1, c1, n; n   int i, j, r, a, b; n   scanf( %d%d%d%d%d , &r0, &c0, &r1, &c1, &n); n   for (i = 1; i <= n; ++i) { n     scanf( %d%d%d , &r, &a, &b); n     for (j = a; j <= b; ++j) valid.insert(make_pair(r, j)); n   } n   int ans = -1; n   queue<node> Q; n   Q.push(node(r0, c0, 0)); n   visited.insert(make_pair(r0, c0)); n   while (Q.size() > 0) { n     node cur = Q.front(); n     Q.pop(); n     if (cur.c == c1 && cur.r == r1) { n       ans = cur.dist; n       break; n     } n     for (i = 0; i < 8; ++i) { n       int r2 = cur.r + dr[i]; n       int c2 = cur.c + dc[i]; n       if (valid.find(make_pair(r2, c2)) != valid.end() && n           visited.find(make_pair(r2, c2)) == visited.end()) { n         visited.insert(make_pair(r2, c2)); n         Q.push(node(r2, c2, cur.dist + 1)); n       } n     } n   } n   printf( %d , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int a, b; n   cin >> a >> b; n   vector<int> arr; n   arr.push_back(b); n   while (b > a) { n     if (b % 10 == 1) n       arr.push_back(b /= 10); n     else if (b % 2 == 1) { n       cout <<  NO  << endl; n       return 0; n     } else n       arr.push_back(b /= 2); n   } n   if (arr.back() != a) { n     cout <<  NO  << endl; n     return 0; n   } n   cout <<  YES n  << arr.size() << endl; n   for (auto it = arr.rbegin(); it != arr.rend(); ++it) cout << *it <<    ; n } n


#include <bits/stdc++.h> n using namespace std; n template <class T> n inline bool chmax(T &a, T b) { n   if (a < b) { n     a = b; n     return true; n   } n   return false; n } n template <class T> n inline bool chmin(T &a, T b) { n   if (a > b) { n     a = b; n     return true; n   } n   return false; n } n const long long INF = 1e18; n int N, K; n vector<int> vertex; n vector<int> d[200]; n set<pair<int, int>> st; n vector<int> g[30000]; n int dist[200][30000]; n void dfs(int idx, int now, int from) { n   for (auto to : g[now]) { n     if (to == from) continue; n     dist[idx][to] = dist[idx][now] + 1; n     dfs(idx, to, now); n   } n } n set<int> appear; n map<int, vector<pair<int, int>>> children; n struct UnionFind { n   vector<int> par; n   vector<int> rank; n   vector<long long> Size; n   UnionFind(int n = 1) { init(n); } n   void init(int n = 1) { n     par.resize(n + 1); n     rank.resize(n + 1); n     Size.resize(n + 1); n     for (int i = 0; i <= n; ++i) par[i] = i, rank[i] = 0, Size[i] = 1; n   } n   int root(int x) { n     if (par[x] == x) { n       return x; n     } else { n       int r = root(par[x]); n       return par[x] = r; n     } n   } n   bool issame(int x, int y) { return root(x) == root(y); } n   bool merge(int x, int y) { n     x = root(x); n     y = root(y); n     if (x == y) return false; n     if (rank[x] < rank[y]) swap(x, y); n     if (rank[x] == rank[y]) ++rank[x]; n     par[y] = x; n     Size[x] += Size[y]; n     return true; n   } n   long long size(int x) { return Size[root(x)]; } n }; n int match[30000]; n int TL = 2980; n int main() { n   cin.tie(0); n   ios::sync_with_stdio(false); n   auto start = chrono::steady_clock::now(); n   int nowtimer; n   cin >> N >> K; n   vertex.resize(K, -1); n   for (int k = 0; k < K; k++) { n     d[k].resize(N); n     for (int i = 0; i < N; i++) { n       cin >> d[k][i]; n       if (d[k][i] == 0) { n         if (vertex[k] != -1) { n           cout << -1 << endl; n           return 0; n         } else { n           vertex[k] = i; n         } n       } n     } n     if (vertex[k] == -1) { n       cout << -1 << endl; n       return 0; n       ; n     } n     appear.insert(vertex[k]); n   } n   for (int k = 1; k < K; k++) { n     int mini = d[0][vertex[k]]; n     vector<int> v(mini + 1, -1); n     for (int i = 0; i < N; i++) { n       if (d[0][i] + d[k][i] < mini) { n         cout << -1 << endl; n       } n       if (d[0][i] + d[k][i] == mini) { n         if (v[d[0][i]] != -1) { n           cout << -1 << endl; n           return 0; n           ; n         } n         v[d[0][i]] = i; n       } n     } n     for (int i = 0; i <= mini; i++) { n       if (v[i] == -1) { n         cout << -1 << endl; n         return 0; n         ; n       } n     } n     for (int i = 0; i < mini; i++) { n       int a = v[i]; n       int b = v[i + 1]; n       if (a > b) swap(a, b); n       st.insert({a, b}); n     } n   } n   UnionFind uni(N); n   for (auto tmp : st) { n     if (!uni.merge(tmp.first, tmp.second)) { n       cout << -1 << endl; n       return 0; n     } else { n       g[tmp.first].push_back(tmp.second); n       g[tmp.second].push_back(tmp.first); n       appear.insert(tmp.first); n       appear.insert(tmp.second); n     } n   } n   vector<pair<vector<int>, int>> mp; n   for (int i = 0; i < N; i++) { n     if (appear.find(i) == appear.end()) continue; n     vector<int> v(K); n     int mini = 1e9; n     for (int j = 0; j < K; j++) { n       v[j] = d[j][i]; n       chmin(mini, v[j]); n     } n     for (int j = 0; j < K; j++) { n       v[j] -= mini; n     } n     mp.push_back({v, i}); n   } n   mp.push_back({{999999999}, -1}); n   sort(mp.begin(), mp.end()); n   vector<pair<vector<int>, int>> sub; n   for (int i = 0; i < N; i++) { n     if (appear.find(i) != appear.end()) continue; n     vector<int> v(K); n     int mini = 1e9; n     for (int j = 0; j < K; j++) { n       v[j] = d[j][i]; n       chmin(mini, v[j]); n     } n     for (int j = 0; j < K; j++) { n       v[j] -= mini; n     } n     sub.push_back({v, i}); n   } n   sort(sub.begin(), sub.end()); n   int idx = 0; n   if ((nowtimer = chrono::duration_cast<chrono::milliseconds>( n                       chrono::steady_clock::now() - start) n                       .count()) > TL) { n     assert(0); n   } n   for (int i = 0; i < sub.size(); i++) { n     if ((nowtimer = chrono::duration_cast<chrono::milliseconds>( n                         chrono::steady_clock::now() - start) n                         .count()) > TL) { n       return 0; n     } n     while (mp[idx].first < sub[i].first) idx++; n     if (mp[idx].first == sub[i].first) { n       match[sub[i].second] = mp[idx].second; n     } else { n       cout << -1 << endl; n       return 0; n     } n   } n   for (int i = 0; i < N; i++) { n     if (appear.find(i) != appear.end()) continue; n     int p = match[i]; n     int len = d[0][i] - d[0][p]; n     if (len <= 0) { n       cout << -1 << endl; n       return 0; n     } n     children[p].push_back({len, i}); n   } n   for (int i = 0; i < N; i++) { n     if (children[i].empty()) continue; n     auto v = children[i]; n     sort(v.begin(), v.end()); n     if (v[0].first != 1) { n       cout << -1 << endl; n       return 0; n     } n     int Last = i; n     st.insert({i, v[0].second}); n     g[Last].push_back(v[0].second); n     g[v[0].second].push_back(Last); n     for (int i = 1; i < v.size(); i++) { n       if (v[i].first > v[i - 1].first + 1) { n         cout << -1 << endl; n         return 0; n       } else if (v[i].first == v[i - 1].first) { n         st.insert({Last, v[i].second}); n         g[Last].push_back(v[i].second); n         g[v[i].second].push_back(Last); n       } else { n         Last = v[i - 1].second; n         st.insert({Last, v[i].second}); n         g[Last].push_back(v[i].second); n         g[v[i].second].push_back(Last); n       } n     } n   } n   for (int i = 0; i < K; i++) { n     dfs(i, vertex[i], -1); n     for (int j = 0; j < N; j++) { n       if (dist[i][j] != d[i][j]) { n         cout << -1 << endl; n         return 0; n       } n     } n   } n   for (auto e : st) { n     cout << e.first + 1 <<     << e.second + 1 << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n int main() { n   int num; n   scanf( %d , &num); n   int res = 0; n   for (int i = 0; i < num; i++) { n     char shape[30]; n     scanf( %s , shape); n     switch (shape[0]) { n       case  T : n         res += 4; n         break; n       case  C : n         res += 6; n         break; n       case  O : n         res += 8; n         break; n       case  D : n         res += 12; n         break; n       case  I : n         res += 20; n         break; n     } n   } n   printf( %d n , res); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long n, len, vf, va, k; n   cin >> n >> len >> vf >> va >> k; n   long long bl = (n + k - 1) / k; n   double eff = static_cast<double>(va - vf) / (va + vf); n   double koef = (va + (bl + (bl - 1) * eff - 1) * vf); n   double ta = len / koef; n   double ans = ta * (bl + (bl - 1) * eff); n   cout << setprecision(15) << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n #pragma GCC optimize  trapv  n using namespace std; n void solveEachTest(long long int T35TC453N = 1) { n   unsigned long long int a, b; n   cin >> a >> b; n   bool ok = true; n   bitset<64> xandy((a - b) / 2); n   bitset<64> xxory(b); n   unsigned long long int x = (a - b) / 2; n   unsigned long long int y = x + b; n   if (a < b) ok = false; n   for (auto i = (0); (((1) < 0) ? (i) > (64) : (i) < (64)); (i) += (1)) { n     if (xandy[i] == 1 and xxory[i] == 1) { n       ok = false; n       break; n     } n   } n   if (!ok) n     cout <<  -1 ; n   else n     cout << x <<     << y; n   cout <<   n ; n   return; n } n signed main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   long long int T3X0 = 0, T353 = 1; n   solveEachTest(T353 - T3X0); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int k; n string res; n int c[500]; n int main(void) { n   for (int i = 2; i < 449; i++) { n     c[i] = i * (i - 1) / 2; n   } n   scanf( %d , &k); n   if (k == 0) { n     cout <<  a  << endl; n   } n   char cur =  a ; n   while (k > 0) { n     int takeind = upper_bound(c + 2, c + 449, k) - c - 1; n     k -= c[takeind]; n     for (int i = 0; i < takeind; i++) { n       res += cur; n     } n     cur++; n   } n   cout << res << endl; n } n


#include <bits/stdc++.h> n int main() { n   double n2, a, b, c, n1; n   scanf( %lf %lf %lf , &a, &b, &c); n   n1 = ((-b + pow((b * b - 4 * a * c), 0.5)) / (a * 2)); n   n2 = ((-b - pow((b * b - 4 * a * c), 0.5)) / (a * 2)); n   if (n1 >= n2) n     printf( %lf %lf , n1, n2); n   else n     printf( %lf %lf , n2, n1); n   return 0; n } n


#include <bits/stdc++.h> n const int inf = 1e9 + 7; n const int md = 1e9 + 7; n const double eps = -1e5; n using namespace std; n int n, ans, x, y; n bool a[1001][1001]; n bool isin(int x, int y) { return x > 0 && x <= 1000 && y > 0 && y <= 1000; } n int main() { n   cin >> n; n   for (int i = 0; i < n; i++) { n     cin >> x >> y; n     a[x][y] = true; n   } n   for (int x = 1; x <= 1000; x++) n     for (int y = 1; y <= 1000; y++) n       if (a[x][y]) { n         for (int xx = x, yy = y; isin(xx, yy); xx--, yy--) ans += a[xx][yy]; n         ans--; n         for (int xx = x, yy = y; isin(xx, yy); xx++, yy--) ans += a[xx][yy]; n         ans--; n         for (int xx = x, yy = y; isin(xx, yy); xx--, yy++) ans += a[xx][yy]; n         ans--; n         for (int xx = x, yy = y; isin(xx, yy); xx++, yy++) ans += a[xx][yy]; n         ans--; n       } n   cout << ans / 2; n } n


#include <bits/stdc++.h> n using namespace std; n const double eps = 1e-9; n const int maxn = 1e5 + 5; n long long h[maxn], l[maxn], r[maxn]; n int main() { n   int n; n   scanf( %d , &n); n   for (int i = 1; i <= n; ++i) scanf( %lld , &h[i]); n   l[1] = r[n] = 1; n   for (int i = 2; i <= n; ++i) l[i] = min(h[i], l[i - 1] + 1); n   for (int i = n - 1; i >= 1; --i) r[i] = min(h[i], r[i + 1] + 1); n   long long ans = 0; n   for (int i = 1; i <= n; ++i) { n     long long now = min(l[i], r[i]); n     if (now > ans) ans = now; n   } n   printf( %lld n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long n, k, tot, i; n   cin >> n >> k; n   tot = n * (n - 1) / 2; n   if (tot <= k) { n     cout <<  no solution ; n   } else { n     for (i = 0; i < n; i++) { n       cout << 0 <<     << i << endl; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 300000; n int parent[N], size[N]; n vector<pair<int, pair<int, int>>> v; n pair<int, int> q[N]; n long long ans[N]; n int Find(int a) { n   if (parent[a] == a) return a; n   return parent[a] = Find(parent[a]); n } n void Union(int a, int b) { parent[Find(b)] = Find(a); } n long long doit(int a, int b) { n   int x = Find(a); n   int y = Find(b); n   long long p = size[x]; n   long long qq = size[y]; n   long long anss = p * qq; n   size[x] += size[y]; n   Union(x, y); n   return anss; n } n int main(int argc, char const *argv[]) { n   for (int i = 0; i < N; i++) parent[i] = i, size[i] = 1; n   int n, m, a, b, c; n   scanf( %d%d , &n, &m); n   for (int i = 0; i < n - 1; ++i) { n     scanf( %d%d%d , &a, &b, &c); n     v.push_back({c, {a, b}}); n   } n   sort(v.begin(), v.end()); n   for (int i = 0; i < m; i++) { n     scanf( %d , &a); n     q[i] = {a, i}; n   } n   sort(q, q + m); n   int at = 0; n   long long sum = 0; n   for (int i = 0; i < m; i++) { n     if (at < v.size()) { n       while (v[at].first <= q[i].first) { n         if (at >= v.size()) break; n         long long kk = doit(v[at].second.first, v[at].second.second); n         sum += kk; n         at++; n         if (at >= v.size()) break; n       } n     } n     ans[q[i].second] = sum; n   } n   for (int i = 0; i < m; i++) { n     printf( %lld  , ans[i]); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main(int argc, char *argv[]) { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   string line; n   while (getline(cin, line)) { n     map<int, vector<string> > answer; n     line +=  , ; n     istringstream iss(line); n     string s; n     stack<int> stk; n     string tmp; n     while (getline(iss, s,  , )) { n       getline(iss, tmp,  , ); n       int children = stoi(tmp); n       answer[stk.size()].push_back(s); n       if (stk.size() && stk.top() > 0) { n         stk.top()--; n       } n       stk.push(children); n       while (stk.size() && stk.top() <= 0) stk.pop(); n     } n     cout << answer.size() << endl; n     for (auto i = 0; i < answer.size(); i++) { n       for (auto &word : answer[i]) cout << word <<    ; n       cout << endl; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, k; n long long sum, s[1000005], ans = -(1LL << 62); n struct el { n   long long val; n   int poz; n   bool operator<(const el &A) const { return val < A.val; } n }; n el a[1000005]; n int main() { n   int i, poz; n   cin.sync_with_stdio(0); n   cin >> n; n   for (i = 1; i <= n; ++i) { n     cin >> a[i].val; n     a[i].poz = i; n   } n   cin >> k; n   sort(a + 1, a + n + 1); n   for (i = 1; i <= n; ++i) s[i] = s[i - 1] + a[i].val; n   for (i = 1, sum = 0; i <= k; ++i) sum += a[i].val * (2 * i - 1 - k); n   ans = sum; n   poz = 1; n   for (i = k + 1; i <= n; ++i) { n     sum = sum - a[i - k].val * (1 - k) + a[i].val * (k - 1) - n           2 * (s[i - 1] - s[i - k]); n     if (sum < ans) { n       ans = sum; n       poz = i - k + 1; n     } n   } n   for (i = poz; i < poz + k; ++i) cout << a[i].poz <<    ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long int a, b, x, y, aa; n   scanf( %I64d %I64d , &x, &y); n   if (y > x) { n     printf( -1 n ); n     return 0; n   } n   a = x / y; n   if ((x % y) && (a % 2)) a++; n   if (!(a % 2)) n     aa = a - 1; n   else n     aa = a; n   double cc = (double)(x - aa * y); n   double c = cc / (double)a; n   double res = y + c; n   printf( %0.11lf n , res); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 100010; n int N, M; n int ar[MAXN]; n char s[MAXN]; n int dn[MAXN][2]; n int used[MAXN][2]; n int f(int k, int flag) { n   if (k == -1) return 0; n   if (used[k][flag]) return dn[k][flag]; n   used[k][flag] = 1; n   if (!flag) return dn[k][flag] = f(k - 1, 0) + ar[k]; n   if (s[k] ==  0 ) return dn[k][flag] = f(k - 1, 1); n   if (s[k] ==  1 ) return dn[k][flag] = max(f(k - 1, 1) + ar[k], f(k - 1, 0)); n } n int main() { n   cin >> N; n   for (int i = 0; i < N; i++) scanf(  %d , &ar[i]); n   scanf(  %s , s); n   cout << f(N - 1, 1) << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n char a[15]; n char rr[15]; n char ans[15]; n int main() { n   int maxx = -99; n   cin >> a; n   int len = strlen(a); n   for (int i = 1; i < 1 << len; i++) { n     int cnt = 0; n     for (int j = 0; j <= len - 1; j++) { n       if (i >> j & 1) rr[++cnt] = a[j]; n     } n     int flag = 0; n     for (int l = 1, r = cnt; l <= r; l++, r--) { n       if (rr[l] != rr[r]) { n         flag = 1; n         break; n       } n     } n     if (flag == 0) { n       if (strcmp(ans + 1, rr + 1) < 0) { n         for (int i = 1; i <= 14; i++) { n           if (i <= cnt) n             ans[i] = rr[i]; n           else n             ans[i] =   0 ; n         } n       } n     } n   } n   cout << ans + 1 << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int dist(int a, int b) { return min(abs(a - b), 360 - abs(a - b)); } n long long d; n int main() { n   scanf( %I64d , &d); n   d = (d % 360 + 360) % 360; n   int ans = 1000; n   for (int i = 0; i < 4; i++) ans = min(ans, dist(d, 90 * i)); n   for (int i = 0; i < 4; i++) { n     if (dist(d, 90 * i) == ans) { n       printf( %d n , i); n       return 0; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 1e5 + 5; n const int MOD = 1e9 + 7; n int a[MAXN]; n class Matrix { n  public: n   void prinf() { n     for (int i = 0; i < 2; i++) { n       for (int j = 0; j < 2; j++) { n         cout << a[i][j]; n       } n       cout << endl; n     } n   } n   long long a[2][2]; n   Matrix() { memset(a, 0, sizeof a); } n   void init() { n     for (int i = 0; i < 2; i++) n       for (int j = 0; j < 2; j++) a[i][j] = (i == j); n   } n   Matrix operator+(const Matrix &B) const { n     Matrix C; n     for (int i = 0; i < 2; i++) n       for (int j = 0; j < 2; j++) C.a[i][j] = (a[i][j] + B.a[i][j]) % MOD; n     return C; n   } n   Matrix operator*(const Matrix &B) const { n     Matrix C; n     for (int i = 0; i < 2; i++) n       for (int k = 0; k < 2; k++) n         for (int j = 0; j < 2; j++) n           C.a[i][j] = (C.a[i][j] + 1LL * a[i][k] * B.a[k][j]) % MOD; n     return C; n   } n   Matrix operator^(const int &t) const { n     Matrix A = (*this), res; n     res.init(); n     int p = t; n     while (p) { n       if (p & 1) res = res * A; n       A = A * A; n       p >>= 1; n     } n     return res; n   } n }; n Matrix sum[MAXN << 2], add[MAXN << 2], f; n void pushdown(int rt) { n   sum[rt << 1] = sum[rt << 1] * add[rt]; n   sum[rt << 1 | 1] = sum[rt << 1 | 1] * add[rt]; n   add[rt << 1] = add[rt << 1] * add[rt]; n   add[rt << 1 | 1] = add[rt << 1 | 1] * add[rt]; n   add[rt].init(); n } n void pushup(int rt) { sum[rt] = sum[rt << 1] + sum[rt << 1 | 1]; } n void build(int l, int r, int rt) { n   sum[rt].init(); n   add[rt].init(); n   if (l == r) { n     sum[rt] = f ^ (a[l] - 1); n     return; n   } n   int m = (l + r) >> 1; n   build(l, m, rt << 1); n   build(m + 1, r, rt << 1 | 1); n   pushup(rt); n } n void update(int L, int R, Matrix v, int l, int r, int rt) { n   if (L <= l && R >= r) { n     sum[rt] = sum[rt] * v; n     add[rt] = add[rt] * v; n     return; n   } n   pushdown(rt); n   int m = (l + r) >> 1; n   if (L <= m) update(L, R, v, l, m, rt << 1); n   if (R > m) update(L, R, v, m + 1, r, rt << 1 | 1); n   pushup(rt); n } n long long query(int L, int R, int l, int r, int rt) { n   if (L <= l && R >= r) { n     return sum[rt].a[0][0]; n   } n   pushdown(rt); n   int m = (l + r) >> 1; n   int cnt = 0; n   if (L <= m) cnt = (cnt + query(L, R, l, m, rt << 1)) % MOD; n   if (R > m) cnt = (cnt + query(L, R, m + 1, r, rt << 1 | 1)) % MOD; n   return cnt; n } n int main() { n   int m, n; n   f.a[0][0] = 1; n   f.a[0][1] = 1; n   f.a[1][0] = 1; n   f.a[1][1] = 0; n   scanf( %d %d , &n, &m); n   for (int i = 1; i <= n; i++) scanf( %d , &a[i]); n   build(1, n, 1); n   int a1, b, c; n   for (int i = 0; i < m; i++) { n     scanf( %d %d %d , &a1, &b, &c); n     if (a1 == 1) { n       int s; n       scanf( %d , &s); n       update(b, c, f ^ s, 1, n, 1); n     } else { n       cout << query(b, c, 1, n, 1) % MOD << endl; n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n vector<int> v[100146]; n int val[100146], a[100146], b[100146]; n long long cnt[1 << 18], sum[1 << 18]; n int leftError[100146], rightError[100146]; n void countDps(int e) { n   if (v[e].size() == 0) n     leftError[e] = rightError[e] = val[e]; n   else { n     countDps(v[e][0]); n     countDps(v[e][1]); n     if (val[v[e][0]] < val[v[e][1]]) { n       leftError[e] = leftError[v[e][0]]; n       rightError[e] = rightError[v[e][1]]; n     } else { n       leftError[e] = leftError[v[e][1]]; n       rightError[e] = rightError[v[e][0]]; n     } n   } n } n void add(long long* tree, int cur, int l, int r, int cl, int cr, long long c) { n   if (r < cl || cr < l) return; n   if (cl <= l && r <= cr) n     tree[cur] += c; n   else { n     int mid = (l + r) / 2; n     add(tree, cur * 2, l, mid, cl, cr, c); n     add(tree, cur * 2 + 1, mid + 1, r, cl, cr, c); n   } n } n void addToSegment(long long* tree, int l, int r, long long c) { n   if (l > r) return; n   add(tree, 1, 0, (1 << 17) - 1, l, r, c); n } n void dfs(int e, int l, int r) { n   if (v[e].size() == 0) return; n   int mid = lower_bound(a + l, a + r, val[e]) - a; n   if (val[v[e][0]] < val[v[e][1]]) { n     dfs(v[e][0], l, mid); n     dfs(v[e][1], mid, r); n     addToSegment(cnt, l, mid - 1, 1); n     addToSegment(sum, l, mid - 1, leftError[v[e][1]]); n     addToSegment(cnt, mid, r - 1, 1); n     addToSegment(sum, mid, r - 1, rightError[v[e][0]]); n   } else { n     dfs(v[e][1], l, mid); n     dfs(v[e][0], mid, r); n     addToSegment(cnt, l, mid - 1, 1); n     addToSegment(sum, l, mid - 1, leftError[v[e][0]]); n     addToSegment(cnt, mid, r - 1, 1); n     addToSegment(sum, mid, r - 1, rightError[v[e][1]]); n   } n } n long long getValue(long long* tree, int el) { n   long long cur = el + (1 << 17); n   long long ans = 0; n   while (cur) { n     ans += tree[cur]; n     cur /= 2; n   } n   return ans; n } n int main() { n   int n; n   scanf( %d , &n); n   int root = -1; n   for (int i = 0; i < n; i++) { n     int p; n     scanf( %d%d , &p, &val[i]); n     if (p != -1) n       v[p - 1].push_back(i); n     else n       root = i; n   } n   int m; n   scanf( %d , &m); n   for (int i = 0; i < m; i++) { n     scanf( %d , &a[i]); n     b[i] = a[i]; n   } n   sort(a, a + m); n   countDps(root); n   memset(sum, 0, sizeof(sum)); n   memset(cnt, 0, sizeof(cnt)); n   dfs(root, 0, m); n   for (int i = 0; i < m; i++) { n     int ind = lower_bound(a, a + m, b[i]) - a; n     double ans = (double)getValue(sum, ind) / getValue(cnt, ind); n     printf( %.9lf n , ans); n   } n } n


#include <bits/stdc++.h> n using namespace std; n int n; n vector<vector<int> > G; n vector<vector<int> > ID; n vector<vector<int> > ans; n void addp(int id, int t) { n   while (ans.size() <= t) ans.push_back(vector<int>()); n   ans[t].push_back(id); n } n void solve(int from, int lab, int v) { n   int ctr = 0; n   for (int i = 0; i < G[v].size(); ++i) { n     if (ctr == lab) ++ctr; n     if (G[v][i] == from) continue; n     addp(ID[v][i], ctr); n     solve(v, ctr++, G[v][i]); n   } n } n int main() { n   ios::sync_with_stdio(0); n   cin >> n; n   G = vector<vector<int> >(n, vector<int>()); n   ID = vector<vector<int> >(n, vector<int>()); n   for (int i = 1; i < n; ++i) { n     int a, b; n     cin >> a >> b; n     G[--a].push_back(--b); n     G[b].push_back(a); n     ID[a].push_back(i); n     ID[b].push_back(i); n   } n   solve(-1, -1, 0); n   cout << ans.size() <<   n ; n   for (int i = 0; i < ans.size(); ++i) { n     cout << ans[i].size(); n     for (int j = 0; j < ans[i].size(); ++j) cout <<     << ans[i][j]; n     cout <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<string> split(const string& s, char c) { n   vector<string> v; n   stringstream second(s); n   string x; n   while (getline(second, x, c)) v.emplace_back(x); n   return move(v); n } n template <typename T, typename... Args> n inline string arrStr(T arr, int n) { n   stringstream s; n   s <<  [ ; n   for (int i = 0; i < n - 1; i++) s << arr[i] <<  , ; n   s << arr[n - 1] <<  ] ; n   return s.str(); n } n inline void __evars_begin(int line) { cerr <<  #  << line <<  :  ; } n template <typename T> n inline void __evars_out_var(vector<T> val) { n   cerr << arrStr(val, val.size()); n } n template <typename T> n inline void __evars_out_var(T* val) { n   cerr << arrStr(val, 10); n } n template <typename T> n inline void __evars_out_var(T val) { n   cerr << val; n } n inline void __evars(vector<string>::iterator it) { cerr <<   n ; } n template <typename T, typename... Args> n inline void __evars(vector<string>::iterator it, T a, Args... args) { n   cerr << it->substr((*it)[0] ==    , it->length()) <<  = ; n   __evars_out_var(a); n   cerr <<  ;  ; n   __evars(++it, args...); n } n mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); n int dx[8] = {1, -1, 0, 0, 1, 1, -1, -1}; n int dy[8] = {0, 0, -1, 1, 2, -2, 2, -2}; n int myrandom(int i) { return std::rand() % i; } n const int MAX = 200010; n const unsigned long long inf = 1000000007; n int ans = 0, maxi = 0; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   long long n, m; n   cin >> n >> m; n   long long x, k, y; n   cin >> x >> k >> y; n   vector<long long> v(n), t(m); n   for (__typeof(n) i = (0) - ((0) > (n)); i != (n) - ((0) > (n)); n        i += 1 - 2 * ((0) > (n))) { n     cin >> v[i]; n   } n   for (__typeof(m) j = (0) - ((0) > (m)); j != (m) - ((0) > (m)); n        j += 1 - 2 * ((0) > (m))) { n     cin >> t[j]; n   } n   vector<long long> vis(n, 0); n   int i = 0, j = 0, cnt = 0; n   while (i < n && j < m) { n     if (v[i] == t[j]) { n       vis[i] = 1; n       i++; n       j++; n       cnt++; n     } else { n       i++; n     } n   } n   if (cnt != m) { n     cout << -1 <<   n ; n     return 0; n   } n   i = 0; n   long long ans = 0; n   while (i < n) { n     while (i < n && vis[i] == 1) { n       i++; n     } n     if (i == n) break; n     int l = i; n     vector<long long> temp; n     while (i < n && vis[i] == 0) { n       temp.push_back(v[i]); n       i++; n     } n     sort((temp).begin(), (temp).end()); n     if (y * k > x) { n       if ((int)temp.size() >= k) { n         ans += ((long long)temp.size() % k) * y; n         ans += ((long long)temp.size() / k) * x; n       } else { n         long long left = l - 1 >= 0 ? v[l - 1] : 0; n         long long right = i < n ? v[i] : 0; n         if (temp.back() < max(left, right)) { n           ans += (long long)temp.size() * y; n         } else { n           cout << -1 <<   n ; n           return 0; n         } n       } n     } else { n       long long left = l - 1 >= 0 ? v[l - 1] : 0; n       long long right = i < n ? v[i] : 0; n       if ((long long)temp.size() >= k) { n         if (temp.back() < max(left, right)) { n           ans += (long long)temp.size() * y; n         } else { n           ans += ((long long)temp.size() - k) * y + x; n         } n       } else { n         if (temp.back() < max(left, right)) { n           ans += (long long)temp.size() * y; n         } else { n           cout << -1 <<   n ; n           return 0; n         } n       } n     } n   } n   cout << ans <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n vector<int> s[10]; n int n1, n2; n int ok(int a, int b) { n   int sum = a + b * 2; n   int len = 0; n   for (int i = 0; i < n1 - a; i++) len += s[1][i]; n   for (int i = 0; i < n2 - b; i++) len += s[2][i]; n   return len <= sum; n } n int main() { n   int a, b, n, m, i, j, ans = 99999999; n   ; n   scanf( %d , &n); n   for (i = 0; i < n; i++) { n     scanf( %d%d , &a, &b); n     s[a].push_back(b); n   } n   sort(s[1].begin(), s[1].end()); n   sort(s[2].begin(), s[2].end()); n   n1 = s[1].size(); n   n2 = s[2].size(); n   for (i = 0; i <= n1; i++) n     for (j = 0; j <= n2; j++) { n       if (ok(i, j)) { n         if (i + j * 2 < ans) ans = i + j * 2; n       } n     } n   printf( %d n , ans); n } n


#include <bits/stdc++.h> n using namespace std; n const int inf = (1 << 30) - 1; n const int maxn = 100010; n char s[maxn]; n int calc(char a, char b) { n   int mx = 30; n   mx = min(mx, a > b ? a - b : b - a); n   mx = min(mx, a > b ? b + 26 - a : a + 26 - b); n   return mx; n } n int main() { n   scanf( %s , s); n   int len = strlen(s); n   int ans = 0; n   ans += calc( a , s[0]); n   for (int i = 0; i < len - 1; i++) ans += calc(s[i], s[i + 1]); n   printf( %d , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, m; n   cin >> n >> m; n   int q = 30; n   if (n == 1 || n == 3 || n == 5 || n == 7 || n == 8 || n == 10 || n == 12) q++; n   if (n == 2) q = 28; n   q = q - 8 + m; n   if (q % 7 == 0) n     cout << (q / 7) + 1; n   else n     cout << (q / 7) + 2; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1000010; n int gcd(int a, int b) { return b ? gcd(b, a % b) : a; } n int lcm(int a, int b) { return a * b / gcd(a, b); } n struct data { n   int num; n   int init; n }; n bool byNum(const data &left, const data &right) { return left.num > right.num; } n bool descend(const int &left, const int &right) { return left > right; } n data total[105]; n pair<int, int> pile[105]; n int sisa[105], card[105]; n int main(void) { n   int temp, n, i, j; n   scanf( %d , &n); n   for (i = 0; i <= n - 1; i++) { n     scanf( %d , &card[i]); n     sisa[i] = 0; n     for (j = 0; j <= card[i] / 2 - 1; j++) { n       scanf( %d , &temp); n       pile[i].first += temp; n     } n     if (card[i] % 2) { n       scanf( %d , &temp); n       sisa[i] = temp; n     } n     for (j = 0; j <= card[i] / 2 - 1; j++) { n       scanf( %d , &temp); n       pile[i].second += temp; n     } n     total[i].num = pile[i].first + sisa[i] + pile[i].second; n     total[i].init = i; n   } n   sort(total, total + n, byNum); n   sort(sisa, sisa + n, descend); n   long long a = 0, b = 0; n   for (i = 0; i <= n - 1; i++) { n     a += pile[total[i].init].first; n     b += pile[total[i].init].second; n   } n   for (i = 0; i <= n - 1; i++) { n     if (i % 2) n       b += sisa[i]; n     else n       a += sisa[i]; n     if (!sisa[i]) break; n   } n   cout << a <<     << b << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<int> caps, full; n int main() { n   int w, l; n   cin >> w >> l; n   caps = vector<int>(w + 1, 0); n   caps[w] = 2e9; n   full = vector<int>(w + 1, 0); n   full[0] = 2e9; n   for (int i = 1; i < w; i++) cin >> caps[i]; n   int from = 0; n   int to = 1; n   while (from != w) { n     ; n     if (to == from) n       to++; n     else if (to - from > l) { n       ; n       from++; n     } else if (full[to] == caps[to]) { n       ; n       to++; n     } else if (full[from] == 0) { n       ; n       from++; n     } else { n       int k = min(full[from], caps[to] - full[to]); n       full[from] -= k; n       full[to] += k; n       ; n     } n   } n   cout << full[w] << endl; n } n


#include <bits/stdc++.h> n using namespace std; n signed main() { n   long long T; n   cin >> T; n   while (T--) { n     string s; n     cin >> s; n     long long ans = (long long)s.size(), curr = 0; n     for (long long i = 0; i < s.size(); i++) { n       if (s[i] ==  + ) { n         curr++; n       } else { n         curr--; n       } n       if (curr < 0) { n         ans += (i + 1); n         curr++; n       } n     } n     cout << ans << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int event, ghotona; n   cin >> event; n   int hired = 0, untreated = 0; n   for (int i = 0; i < event; i++) { n     cin >> ghotona; n     hired += ghotona; n     if (hired < 0) { n       untreated++; n       hired = 0; n     } n   } n   cout << untreated << endl; n } n


#include <bits/stdc++.h> n #pragma comment(linker,  /STACK:102400000,102400000 ) n using namespace std; n int bit[1100]; n int tot = 0; n void GetBinary(int n) { n   memset(bit, 0, sizeof bit); n   while (n) { n     bit[tot++] = n & 1; n     n >>= 1; n   } n } n int layer[1100][2]; n void Construct() { n   layer[1][0] = 1; n   layer[2][0] = 3; n   layer[3][0] = 4; n   layer[3][1] = 5; n   for (int i = 4; i <= 60; i += 2) { n     layer[i][0] = layer[i - 2][0] + 3; n     layer[i + 1][0] = layer[i - 1][0] + 3; n     layer[i + 1][1] = layer[i - 1][1] + 3; n   } n } n int g[1100][1100]; n int main() { n   ios::sync_with_stdio(false); n   int n; n   cin >> n; n   GetBinary(n); n   Construct(); n   memset(g, 0, sizeof g); n   int total = tot << 1; n   layer[total + 1][0] = 2; n   for (int i = 2; i <= total; ++i) { n     if (i == 2) { n       g[layer[i - 1][0]][layer[i][0]] = 1; n       continue; n     } n     if (i % 2 == 1) { n       g[layer[i - 1][0]][layer[i][0]] = 1; n       g[layer[i - 1][0]][layer[i][1]] = 1; n     } else { n       g[layer[i - 1][0]][layer[i][0]] = 1; n       g[layer[i - 1][1]][layer[i][0]] = 1; n     } n   } n   g[2][layer[total][0]] = 1; n   int PathLength = total; n   int ind = tot * 3, indd = tot * 3, end = tot * 3; n   for (int i = 0; i < tot - 1; ++i) { n     if (bit[i]) { n       int pre = layer[(i + 1) * 2][0]; n       indd = ind; n       for (int j = 1; j <= PathLength - (i + 1) * 2 - 1; ++j) { n         g[pre][j + indd] = 1; n         pre = j + indd; n         ind++; n       } n       g[pre][end] = 1; n     } n   } n   cout << ind << endl; n   for (int i = 1; i <= ind; ++i) { n     for (int j = 1; j <= ind; ++j) { n       if (i == j) n         cout <<  N ; n       else if (g[i][j] || g[j][i]) n         cout <<  Y ; n       else n         cout <<  N ; n     } n     cout << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void solve() { n   long long int n; n   string s; n   cin >> n >> s; n   for (long long int i = 0; i < n; i++) cout << s[n - 1]; n   cout << endl; n } n int main() { n   int test; n   cin >> test; n   for (int TEST = 1; TEST <= test; TEST++) solve(); n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   cin >> n; n   char arr[n][n]; n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < n; j++) { n       cin >> arr[i][j]; n     } n   } n   pair<int, int> p[n * n]; n   int in = 0; n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < n; j++) { n       if (arr[i][j] ==  C ) { n         p[in] = make_pair(i, j); n         in++; n       } n     } n   } n   int cnt = 0; n   for (int i = 0; i < in; i++) { n     for (int j = i + 1; j < in; j++) { n       if (p[i].first == p[j].first || p[i].second == p[j].second) { n         cnt++; n       } n     } n   } n   cout << cnt << endl; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 103; n int p[MAXN], c[MAXN]; n int main() { n   int t; n   cin >> t; n   while (t--) { n     int n; n     cin >> n; n     bool check = true; n     for (int i = 1; i <= n; i++) { n       cin >> p[i] >> c[i]; n       if (c[i] > p[i] || c[i] < c[i - 1] || p[i] < c[i] || n           p[i] - p[i - 1] < c[i] - c[i - 1]) n         check = false; n     } n     if (check) n       cout <<  YES  n            <<   n ; n     else n       cout <<  NO  n            <<   n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n double const pi = 3.14159265358979; n int const MAXN = 2000000; n long long const MOD = 1000000007; n long long const INF = 1000000000000000000ll; n int a[2000000], d[2000000]; n bool is[2000000]; n int main() { n   int n, res = 0; n   scanf( %d , &n); n   for (int i = 0; i < (int)(n); i++) { n     scanf( %d , &a[i]); n     is[a[i]] = 1; n   } n   for (int i = (int)1; i <= (int)(1000000); i++) { n     d[i] = 1; n   } n   for (int i = 0; i < (int)(n); i++) { n     int k = 2; n     while (a[i] * k <= 1000000) { n       d[a[i] * k] = max(d[a[i] * k], d[a[i]] + 1); n       k++; n     } n     res = max(res, d[a[i]]); n   } n   printf( %d n , res); n   return 0; n } n