#include <bits/stdc++.h> n #pragma comment(linker,  /STACK:36777216 ) n using namespace std; n template <typename T1, typename T2> n inline void chkmin(T1 &a, T2 b) { n   if (a > b) a = b; n } n template <typename T1, typename T2> n inline void chkmax(T1 &a, T2 b) { n   if (a < b) a = b; n } n const string FILENAME =  input ; n const int MAXN = 100228; n int n, m, d; n vector<int> g[MAXN]; n bool who[MAXN * 50]; n int res = 0; n vector<int> rg[MAXN]; n vector<bool> used[102]; n vector<int> ord; n int used1[MAXN * 50]; n int st[MAXN * 50]; n int id[MAXN * 50]; n int nxt[MAXN * 50]; n int lst[MAXN * 50]; n int uks = 0; n int kek[MAXN * 50]; n void add(int a, int b) { n   if (st[a] == 0) { n     uks++; n     st[a] = uks; n     id[uks] = b; n     lst[a] = uks; n     nxt[uks] = -1; n   } else { n     uks++; n     nxt[lst[a]] = uks; n     nxt[uks] = -1; n     lst[a] = uks; n     id[uks] = b; n   } n } n void dfs1(int u, int t) { n   used1[u * d + t] = true; n   for (auto h : g[u]) { n     if (!used1[h * d + (t + 1) % d]) { n       dfs1(h, (t + 1) % d); n     } n   } n   ord.push_back(u * d + t); n } n int used2[MAXN]; n void dfs2(int u, int t, int c) { n   used1[u * d + t] = c; n   if (who[u * d + t]) { n     if (used2[u] != c) { n       kek[c]++; n       used2[u] = c; n     } n   } n   for (auto h : rg[u]) { n     if (!used1[h * d + (t - 1 + d) % d]) { n       dfs2(h, (t - 1 + d) % d, c); n     } n   } n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   cin >> n >> m >> d; n   for (int i = 0; i < m; i++) { n     int u, v; n     cin >> u >> v; n     u--, v--; n     g[u].push_back(v); n     rg[v].push_back(u); n   } n   for (int i = 0; i < n; i++) { n     string s; n     cin >> s; n     for (int j = 0; j < d; j++) { n       if (s[j] ==  1 ) { n         who[i * d + j] = true; n       } n     } n   } n   for (int i = 0; i < n * d; i++) { n     if (!used1[i]) { n       dfs1(i / d, i % d); n     } n   } n   memset(used1, 0, sizeof(used1)); n   int cur = 1; n   reverse((ord).begin(), (ord).end()); n   for (auto x : ord) { n     if (!used1[x]) { n       dfs2(x / d, x % d, cur); n       cur++; n     } n   } n   for (int i = 0; i < n * d; i++) { n     for (auto x : g[i / d]) { n       int f = (i % d + 1) % d; n       if (used1[i] != used1[x * d + f]) { n         add(used1[i], used1[x * d + f]); n       } n     } n   } n   int ff = used1[0]; n   for (int x = cur - 1; x >= 1; x--) { n     int dp = 0; n     int cur = st[x]; n     if (cur) { n       while (cur != -1) { n         chkmax(dp, kek[id[cur]]); n         cur = nxt[cur]; n       } n     } n     kek[x] += dp; n   } n   cout << kek[ff] << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m; n bool f[100009]; n pair<int, int> a[100009]; n vector<int> v[100009]; n int main() { n   scanf( %d , &n); n   for (int i = 1; i <= n; i++) { n     scanf( %d , &a[i].first); n     a[i].second = i; n   } n   sort(a + 1, a + n + 1); n   for (int i = 1; i <= n; i++) n     if (!f[i]) { n       int x = i; n       while (!f[x]) { n         f[x] = 1; n         v[m].push_back(x); n         x = a[x].second; n       } n       m++; n     } n   printf( %d n , m); n   for (int i = 0; i < m; i++) { n     printf( %d  , v[i].size()); n     for (int j = 0; j < v[i].size(); j++) printf( %d  , v[i][j]); n     printf(  n ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void arr(string &s); n int main() { n   int n = 0, k = 0, m = 0; n   bool b = 0; n   cin >> n >> k; n   string s =   ; n   cin >> s; n   arr(s); n   for (int i = 0; i < s.length(); i++) switch (s[i]) { n       case  G : n         m = i; n         i = s.length(); n         break; n     } n   for (int i = m; i < s.length(); i += k) switch (s[i]) { n       case  T : n         b = 1; n         i = s.length(); n         break; n       case  # : n         i = s.length(); n         break; n     } n   switch (b) { n     case 1: n       cout <<  YES n ; n       break; n     default: n       cout <<  NO n ; n   } n } n void arr(string &s) { n   int a = 0, b = 0; n   string v = s; n   for (int i = 0; i < s.length(); i++) { n     switch (s[i]) { n       case  G : n         a = i; n         break; n       case  T : n         b = i; n         break; n     } n   } n   if (a > b) { n     for (int i = 1; i <= s.length(); i++) v[i - 1] = s[s.length() - i]; n     s = v; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 2e5 + 7; n int a[N], b[N], na[N], nb[N], t1[N], t2[N], c[N], tp, q[N], mx[N], x, n, m, i, n     j; n long long ans; n void init(int* a, int* ans, int n) { n   for (memset(ans, 0x3f, (n + 3) << 2), tp = 0, i = 1; i <= n; ++i) { n     j = a[i]; n     while (tp && a[i] < a[q[tp]]) j = max(j, mx[tp--]); n     if (tp) ans[i] = min(ans[i], j); n     q[++tp] = i; n     mx[tp] = j; n   } n   for (tp = 0, i = n; i; --i) { n     j = a[i]; n     while (tp && a[i] <= a[q[tp]]) j = max(j, mx[tp--]); n     if (tp) ans[i] = min(ans[i], j); n     q[++tp] = i; n     mx[tp] = j; n   } n } n void A(int x, int C) { n   while (x < N) c[x] += C, x += x & -x; n } n int Q(int x, int C = 0) { n   while (x > 0) C += c[x], x -= x & -x; n   return C; n } n int main() { n   for (scanf( %d%d%d , &n, &m, &x), i = 1; i <= n; ++i) n     scanf( %d , a + i), t1[i] = i; n   for (i = 1; i <= m; ++i) scanf( %d , b + i), t2[i] = i; n   init(a, na, n); n   init(b, nb, m); n   sort(t1 + 1, t1 + n + 1, [&](int x, int y) { return a[x] < a[y]; }); n   sort(t2 + 1, t2 + m + 1, [&](int x, int y) { return nb[x] < nb[y]; }); n   for (i = 1, j = m; i <= n; ++i) { n     while (j && a[t1[i]] + nb[t2[j]] > x) A(b[t2[j--]], 1); n     ans += Q(x - a[t1[i]]) - Q(x - na[t1[i]]); n   } n   printf( %lld n , ans); n } n


#include <bits/stdc++.h> n using namespace std; n inline int read() { n   int x = 0, c = getchar(), f = 0; n   for (; c >  9  || c <  0 ; f = c ==  - , c = getchar()) n     ; n   for (; c >=  0  && c <=  9 ; x = (x << 1) + (x << 3) + c -  0 , c = getchar()) n     ; n   return f ? -x : x; n } n inline void write(int x) { n   if (x < 0) putchar( - ), x = -x; n   if (x >= 10) write(x / 10); n   putchar(x % 10 + 48); n } n const int N = 100 + 10, oo = 1e9; n int n, m, q, a, b, f[N], ans[N], dis[N][N], st[N], en[N], vis[N]; n bool must[N][N]; n int dfs(int x, int y, int Time) { n   if (x == y) return ans[x]; n   if (vis[x] == Time) return f[x]; n   vis[x] = Time; n   f[x] = 0; n   for (int i = (int)1, _y = n; i <= _y; i++) n     if (dis[x][i] == 1 && dis[i][y] + 1 == dis[x][y]) n       f[x] = max(f[x], dfs(i, y, Time)); n   return f[x] = min(f[x], ans[x]); n } n int main() { n   n = read(), m = read(), a = read(), b = read(); n   for (int i = (int)1, _y = n; i <= _y; i++) n     for (int j = (int)i + 1, _y = n; j <= _y; j++) dis[i][j] = dis[j][i] = oo; n   for (int i = (int)1, _y = m; i <= _y; i++) { n     int u = read(), v = read(); n     dis[u][v] = 1; n   } n   for (int k = (int)1, _y = n; k <= _y; k++) n     for (int i = (int)1, _y = n; i <= _y; i++) n       if (i != k) n         for (int j = (int)1, _y = n; j <= _y; j++) n           if (i != j && j != k && dis[i][j] > dis[i][k] + dis[k][j]) n             dis[i][j] = dis[i][k] + dis[k][j]; n   q = read(); n   for (int i = (int)1, _y = q; i <= _y; i++) { n     st[i] = read(), en[i] = read(); n     if (dis[st[i]][en[i]] == oo) continue; n     memset(vis, 0, sizeof vis); n     for (int j = (int)1, _y = n; j <= _y; j++) n       if (dis[st[i]][j] + dis[j][en[i]] == dis[st[i]][en[i]]) n         vis[dis[st[i]][j]]++; n     for (int j = (int)1, _y = n; j <= _y; j++) n       if (dis[st[i]][j] + dis[j][en[i]] == dis[st[i]][en[i]] && n           vis[dis[st[i]][j]] == 1) n         must[i][j] = 1; n   } n   for (int i = (int)1, _y = n; i <= _y; i++) ans[i] = oo, vis[i] = 0; n   ans[b] = 0; n   for (int Time = (int)1, _y = n; Time <= _y; Time++) { n     memset(vis, 0, sizeof vis); n     for (int i = (int)1, _y = q; i <= _y; i++) n       if (dis[st[i]][en[i]] != oo) n         for (int j = (int)1, _y = n; j <= _y; j++) n           if (must[i][j]) ans[j] = min(ans[j], dfs(j, en[i], i) + 1); n   } n   printf( %d n , ans[a] == oo ? -1 : ans[a]); 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   int TESTS = 1; n   while (TESTS--) { n     long long m = 1, ans = 1; n     long long n; n     cin >> n; n     while (ans < n) { n       m++; n       ans += 2; n     } n     cout << m <<   n ; n     long long x = 1, y = 1; n     cout << 1 <<     << 1 <<   n ; n     x++; n     for (long long i = 1; i < n; i++) { n       cout << x <<     << y <<   n ; n       if (i == n - 1) break; n       i++; n       cout << x <<     << y + 1 <<   n ; n       x++; n       y++; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int INF = 2147483647; n const long long LLINF = 9223372036854775807LL; n const int ST = 100010; n const int ST1 = 1000010; n const long long MOD = 1000000007; n long long ABS(long long a) { n   if (a < 0) n     return a * (-1); n   else n     return a; n } n int mas[30]; n bool ans = true; n void bad() { n   cout <<  NO ; n   exit(0); n } n vector<vector<int> > ppp; n int sz = 0; n void per(vector<int> &t) { n   if ((int)((t).size()) == sz) { n     ppp.push_back(t); n   } else { n     t.push_back(0); n     per(t); n     t.pop_back(); n     t.push_back(1); n     per(t); n     t.pop_back(); n   } n } n void per(vector<int> val, int ed, vector<int> zap) { n   if ((int)((val).size()) == 0) { n     if (ed == 0) { n       cout <<  YES ; n       exit(0); n     } else n       return; n   } n   vector<vector<int> > pp; n   vector<int> em; n   sz = (int)((zap).size()); n   ppp.clear(); n   per(em); n   pp = ppp; n   int tek = val[(int)((val).size()) - 1] - 1; n   val.pop_back(); n   int ost = 0; n   for (int i = 0; i < (int)((pp).size()); i++) { n     vector<int> next; n     next.push_back(tek + 1); n     ost = 0; n     int kk = 0; n     for (int j = 0; j < (int)((pp[i]).size()); j++) { n       if (pp[i][j] == 1) { n         ost += zap[j]; n         kk++; n       } else n         next.push_back(zap[j]); n     } n     if (ost <= tek) { n       int s = tek - ost; n       if (s > ed) continue; n       kk += s; n       if (kk == 1) continue; n       per(val, ed - s, next); n     } n   } n } n int main() { n   int n; n   cin >> n; n   if (n == 1) { n     cout <<  YES ; n     return 0; n   } n   int kk = 0; n   for (int i = 0; i < n; i++) { n     cin >> mas[i]; n     if (mas[i] == 2) bad(); n     if (mas[i] == 1) kk++; n   } n   sort(mas, mas + n); n   int mn = n / 2; n   if (kk < mn) bad(); n   vector<int> t; n   int nol = 0; n   for (int i = 0; i < n; i++) n     if (mas[i] != 1) n       t.push_back(mas[i]); n     else n       nol++; n   vector<int> em; n   reverse(t.begin(), t.end()); n   per(t, nol, em); n   cout <<  NO ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int read() { n   int x = 0, sgn = 1; n   char ch = getchar(); n   for (; !isdigit(ch); ch = getchar()) n     if (ch ==  - ) sgn = -1; n   for (; isdigit(ch); ch = getchar()) x = (x << 1) + (x << 3) + (ch ^ 48); n   return x * sgn; n } n const int N = 1e5 + 10; n int n, b[N << 1], tot, l[N], r[N], deg[N << 1], h[N << 1], cnt = 1, now[N << 1], n                                                            ans[N << 1]; n struct edge { n   int v, w, nxt; n } e[N << 2]; n bool v[N << 2], vis[N << 1]; n void link(int x, int y, int w) { n   e[++cnt] = (edge){y, w, h[x]}; n   h[x] = cnt; n } n void dfs(int x) { n   vis[x] = 1; n   for (int i = now[x], y; i; i = e[now[x]].nxt) { n     now[x] = i; n     if (v[i]) continue; n     y = e[i].v; n     v[i] = v[i ^ 1] = 1; n     ans[e[i].w] = x < y; n     dfs(y); n   } n } n int main() { n   n = read(); n   for (int i = 1; i <= n; i++) n     b[++tot] = l[i] = read(), b[++tot] = r[i] = read() + 1; n   sort(b + 1, b + tot + 1); n   tot = unique(b + 1, b + tot + 1) - b - 1; n   for (int i = 1; i <= n; i++) { n     l[i] = lower_bound(b + 1, b + tot + 1, l[i]) - b; n     r[i] = lower_bound(b + 1, b + tot + 1, r[i]) - b; n     deg[l[i]]++; n     deg[r[i]]++; n     link(l[i], r[i], i); n     link(r[i], l[i], i); n   } n   int lst = 0; n   for (int i = 1; i <= tot; i++) { n     if (!(deg[i] & 1)) continue; n     if (lst) n       link(lst, i, 0), link(i, lst, 0), lst = 0; n     else n       lst = i; n   } n   for (int i = 1; i <= tot; i++) now[i] = h[i]; n   for (int i = 1; i <= tot; i++) n     if (!vis[i]) dfs(i); n   for (int i = 1; i <= n; i++) { n     if (ans[i]) n       printf( 1  ); n     else n       printf( 0  ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n char *student; n int *arr; n struct node { n   int x; n   int y; n   int val; n }; n std::vector<node> v; n std::vector<node> out; n struct edge { n   int v; n   edge *left; n   edge *right; n }; n edge *ed; n bool *flag; n bool comp(node &a, node &b) { n   return (!(a.val < b.val || (a.val == b.val && a.x < b.x))); n } n void make_node(int x, int y) { n   if (student[x] ^ student[y]) { n     node temp; n     temp.x = x; n     temp.y = y; n     temp.val = abs(arr[x] - arr[y]); n     v.push_back(temp); n     push_heap(v.begin(), v.end(), comp); n   } n } n void print() { n   for (std::vector<node>::iterator it = out.begin(); it != out.end(); ++it) { n     node temp = *it; n     cout << temp.x + 1 <<     << temp.y + 1 << endl; n   } n } n void select() { n   while (true) { n     int l = -1, r = -1; n     while (v.size() > 0) { n       node temp = v.front(); n       pop_heap(v.begin(), v.end(), comp); n       v.pop_back(); n       l = temp.x; n       r = temp.y; n       if (flag[l] || flag[r]) { n         l = -1; n         r = -1; n         continue; n       } else { n         flag[l] = true; n         flag[r] = true; n         out.push_back(temp); n         break; n       } n     } n     if (l == -1) break; n     edge *lt = ed[l].left; n     edge *rt = ed[r].right; n     if (lt != NULL) { n       lt->right = rt; n     } n     if (rt != NULL) { n       rt->left = lt; n     } n     if (lt != NULL && rt != NULL) make_node(lt->v, rt->v); n   } n } n int main() { n   std::cout << std::fixed << std::setprecision(10); n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   int n; n   cin >> n; n   student = new char[n]; n   arr = new int[n]; n   flag = new bool[n]; n   string s; n   cin >> s; n   for (int i = 0; i < n; i++) { n     student[i] = s[i]; n     int x; n     cin >> x; n     arr[i] = x; n     flag[i] = false; n   } n   for (int i = 0; i < n - 1; i++) { n     make_node(i, i + 1); n   } n   ed = new edge[n]; n   for (int i = 0; i < n; i++) { n     ed[i].v = i; n     ed[i].left = i > 0 ? &ed[i - 1] : NULL; n     ed[i].right = i < n - 1 ? &ed[i + 1] : NULL; n   } n   select(); n   cout << out.size() << endl; n   print(); n } n


#include <bits/stdc++.h> n using namespace std; n const long double PI = acos(-1); n pair<long double, long double> sum(pair<long double, long double> a, n                                    pair<long double, long double> b) { n   return {a.first + b.first, a.second + b.second}; n } n long double cross(pair<long double, long double> a, n                   pair<long double, long double> b) { n   return a.first * b.second - b.first * a.second; n } n pair<long double, long double> prod(long double k, n                                     pair<long double, long double> a) { n   return {k * a.first, k * a.second}; n } n pair<long double, long double> com(vector<pair<long double, long double> > &v) { n   auto i = v.begin(), end = v.end(), j = end - 1; n   auto ss = v.begin(); n   pair<long double, long double> res{0, 0}; n   long double A = 0; n   for (; i != end; j = i++) { n     pair<long double, long double> a = *i, b = *j; n     a.first -= ss->first; n     a.second -= ss->second; n     b.first -= ss->first; n     b.second -= ss->second; n     res = sum(res, prod(cross(b, a), sum(a, b))); n     A += cross(b, a); n   } n   return {res.first / A / 3 + ss->first, res.second / A / 3 + ss->second}; n } n long double dist(pair<long double, long double> a, n                  pair<long double, long double> b) { n   long double dx = a.first - b.first; n   long double dy = a.second - b.second; n   return sqrt(dx * dx + dy * dy); n } n int main() { n   cin.sync_with_stdio(0); n   cin.tie(0); n   int n, q; n   cout << setprecision(20); n   cin >> n >> q; n   vector<pair<long double, long double> > poly(n); n   for (int i = 0; i < n; ++i) { n     cin >> poly[i].first >> poly[i].second; n   } n   pair<long double, long double> c = com(poly); n   vector<pair<long double, long double> > v(n); n   for (int i = 0; i < n; ++i) { n     v[i].first = dist(c, poly[i]); n     v[i].second = atan2((poly[i].second - c.second), (poly[i].first - c.first)); n   } n   bool first = 1; n   pair<int, int> cur{0, 1}; n   pair<pair<long double, long double>, pair<long double, long double> > cc{ n       poly[0], poly[1]}; n   pair<long double, long double> center; n   long double theta; n   while (q--) { n     int type; n     cin >> type; n     if (type == 1) { n       int f, t; n       cin >> f >> t; n       --f; n       --t; n       int m; n       pair<long double, long double> top; n       if (f == cur.first) { n         m = cur.second; n         top = cc.second; n       } else { n         m = cur.first; n         top = cc.first; n       } n       center = {top.first, top.second - v[m].first}; n       theta = PI / 2 - v[m].second; n       pair<long double, long double> other = { n           v[t].first * cos(theta + v[t].second) + center.first, n           v[t].first * sin(theta + v[t].second) + center.second}; n       cur = {m, t}; n       cc = {top, other}; n       first = 0; n     } else { n       int t; n       cin >> t; n       --t; n       if (first) { n         cout << poly[t].first <<     << poly[t].second <<   n ; n       } else { n         cout << v[t].first * cos(theta + v[t].second) + center.first <<     n              << v[t].first * sin(theta + v[t].second) + center.second <<   n ; n       } n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n long long n, a, b, c; n long long d; n long long cnt1, cnt2; n long long r; n int main() { n   cin >> n >> a >> b >> c; n   d = b - c; n   if (n >= a) { n     cnt1 = n / a; n     r = n % a; n     if (r >= b) cnt1 += (r - b) / d + 1; n   } n   if (n >= b) { n     r = n - b; n     long long cnt = r / d + 1; n     cnt2 += cnt; n     cnt2 += (n - cnt * d) / a; n   } n   printf( %lld n , max(cnt1, cnt2)); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, i; n int fi = 0, se = 0, th = 0, fo = 0; n int main() { n   cin >> n; n   cout <<  +------------------------+  << endl; n   if (n >= 3) { n     n--; n     th++; n   } n   while (n) { n     if (n) { n       fi++; n       n--; n     } n     if (n) { n       se++; n       n--; n     } n     if (n) { n       fo++; n       n--; n     } n   } n   cout <<  | ; n   for (i = 11; i; i--) { n     if (fi) { n       cout <<  O. ; n       fi--; n     } else { n       cout <<  #. ; n     } n   } n   cout <<  |D|)  << endl; n   cout <<  | ; n   for (i = 11; i; i--) { n     if (se) { n       cout <<  O. ; n       se--; n     } else { n       cout <<  #. ; n     } n   } n   cout <<  |.|  << endl; n   cout <<  | ; n   if (th) { n     cout <<  O ; n   } else { n     cout <<  # ; n   } n   cout <<  .......................|  << endl; n   cout <<  | ; n   for (i = 11; i; i--) { n     if (fo) { n       cout <<  O. ; n       fo--; n     } else { n       cout <<  #. ; n     } n   } n   cout <<  |.|)  << endl; n   cout <<  +------------------------+  << endl; n   return 0; n } n


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


#include <bits/stdc++.h> n using namespace std; n struct sheet { n   long long xt, yt, xb, yb; n }; n sheet a[3]; n long long flag = 0; n void hor() { n   long long yc = a[0].yb; n   for (double x = a[0].xb; x <= a[0].xt; x += .5) { n     if (a[1].xb <= x && x <= a[1].xt && a[1].yb <= yc && yc <= a[1].yt) { n       continue; n     } else if (a[2].xb <= x && x <= a[2].xt && a[2].yb <= yc && yc <= a[2].yt) { n       continue; n     } else { n       flag = 1; n       return; n     } n   } n   yc = a[0].yt; n   for (double x = a[0].xb; x <= a[0].xt; x += .5) { n     if (a[1].xb <= x && x <= a[1].xt && a[1].yb <= yc && yc <= a[1].yt) { n       continue; n     } else if (a[2].xb <= x && x <= a[2].xt && a[2].yb <= yc && yc <= a[2].yt) { n       continue; n     } else { n       flag = 1; n       return; n     } n   } n } n void ver() { n   long long xc = a[0].xb; n   for (double y = a[0].yb; y <= a[0].yt; y += .5) { n     if (a[1].xb <= xc && xc <= a[1].xt && a[1].yb <= y && y <= a[1].yt) { n       continue; n     } else if (a[2].xb <= xc && xc <= a[2].xt && a[2].yb <= y && y <= a[2].yt) { n       continue; n     } else { n       flag = 1; n       return; n     } n   } n   xc = a[0].xt; n   for (double y = a[0].yb; y <= a[0].yt; y++) { n     if (a[1].xb <= xc && xc <= a[1].xt && a[1].yb <= y && y <= a[1].yt) { n       continue; n     } else if (a[2].xb <= xc && xc <= a[2].xt && a[2].yb <= y && y <= a[2].yt) { n       continue; n     } else { n       flag = 1; n       return; n     } n   } n } n int main() { n   for (int i = 0; i < 3; i++) { n     cin >> a[i].xb >> a[i].yb >> a[i].xt >> a[i].yt; n   } n   hor(); n   ver(); n   cout << (flag ?  YES  :  NO ); n } n


#include <bits/stdc++.h> n using namespace std; n void ret(string s) { n   cout << s; n   exit(0); n } n int main() { n   int x1, x2, y1, y2, n; n   cin >> n >> x1 >> y1 >> x2 >> y2; n   if (x1 * x2 == 0 && x1 + x2 == n) n     cout << min(n + y1 + y2, 3 * n - y1 - y2); n   else if (y1 * y2 == 0 && y1 + y2 == n) n     cout << min(n + x1 + x2, 3 * n - x1 - x2); n   else n     cout << abs(x1 - x2) + abs(y1 - y2); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 34; n const int M = 70; n int n; n int arr[N]; n int dp[N][M * M][M]; n int find(int pos, int sum, int cnt) { n   if (pos > n) { n     return (sum * 2 == cnt * (cnt - 1)) ? cnt : M; n   } n   if (dp[pos][sum][cnt] != -1) { n     return dp[pos][sum][cnt]; n   } n   int res = M; n   int cur = sum; n   int tot = cnt; n   for (int i = 1; tot < 65; ++i) { n     ++tot; n     cur += arr[pos]; n     if (cur * 2 >= tot * (tot - 1)) { n       res = min(res, find(pos + 1, cur, tot)); n     } n   } n   return dp[pos][sum][cnt] = res; n } n int deg[M]; n void go(int pos, int sum, int cnt) { n   if (pos > n) { n     return; n   } n   int cur = sum; n   int tot = cnt; n   for (int i = 1; tot < 65; ++i) { n     ++tot; n     cur += arr[pos]; n     deg[tot] = arr[pos]; n     if (cur * 2 >= tot * (tot - 1)) { n       if (find(pos + 1, cur, tot) == find(pos, sum, cnt)) { n         go(pos + 1, cur, tot); n         return; n       } n     } n   } n } n int graph[M][M]; n pair<int, int> out[M]; n int main() { n   cin >> n; n   for (int i = 1; i <= n; ++i) { n     cin >> arr[i]; n   } n   sort(arr + 1, arr + 1 + n); n   memset(dp, -1, sizeof(dp)); n   int m = find(1, 0, 0); n   go(1, 0, 0); n   for (int i = 1; i <= m; ++i) { n     out[i] = make_pair(deg[i], i); n   } n   sort(out + 1, out + 1 + m); n   for (int i = 1; i <= m; ++i) { n     for (int j = i + 1; j <= i + out[i].first; ++j) { n       graph[out[i].second][out[j].second] = 1; n     } n     for (int j = i + out[i].first + 1; j <= m; ++j) { n       graph[out[j].second][out[i].second] = 1; n       --out[j].first; n     } n     out[i].first = 0; n     sort(out + i + 1, out + 1 + m); n   } n   printf( %d n , m); n   for (int i = 1; i <= m; ++i) { n     for (int j = 1; j <= m; ++j) { n       printf( %d , graph[i][j]); n     } n     printf(  n ); n   } n } n


#include <bits/stdc++.h> n using namespace std; n int tmp, n, tr[100005 * 30][2], cnt; n inline void insert(int x) { n   int p = 0, d; n   for (int i = 29; i >= 0; --i) { n     d = (x >> i) & 1; n     if (tr[p][d]) n       p = tr[p][d]; n     else { n       tr[p][d] = ++cnt; n       p = cnt; n       memset(tr[p], 0, sizeof(tr[p])); n     } n   } n } n int dfs(int p, int d) { n   if (!tr[p][0] && !tr[p][1]) return 0; n   if (!tr[p][0]) return dfs(tr[p][1], d - 1); n   if (!tr[p][1]) return dfs(tr[p][0], d - 1); n   return (1 << d) + min(dfs(tr[p][0], d - 1), dfs(tr[p][1], d - 1)); n } n int main() { n   while (~scanf( %d , &n)) { n     cnt = 0; n     memset(tr[0], 0, sizeof(tr[0])); n     for (int i = 1; i <= n; ++i) { n       scanf( %d , &tmp); n       insert(tmp); n     } n     int ans = dfs(0, 29); n     printf( %d n , ans); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void solve() { n   vector<long long> arr(4); n   for (int i = 0; i < 4; i++) cin >> arr[i]; n   int counter = 0; n   for (int i = 0; i < 4; i++) counter += arr[i] % 2; n   if (counter == 0 || counter == 1 || counter == 4) { n     cout <<  Yes n ; n     return; n   } else if (counter == 3) { n     if (arr[0] != 0 && arr[1] != 0 && arr[2] != 0) n       cout <<  Yes n ; n     else n       cout <<  No n ; n   } else n     cout <<  No n ; n   return; n } n int main() { n   int t; n   cin >> t; n   while (t--) solve(); n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 250; n int n, m, B, ans; n int co[N], sx[N], sy[N]; n int mp[N][N], id[N], fl[N]; n bool cmp(int x, int y) { return sy[x] < sy[y]; } n const int ND = 500; n struct edge { n   int to, next, f, v; n } e[N * N]; n int head[ND], cur[ND], tot; n int S, T, dis[ND]; n int q[ND * ND], tp[ND], fr[ND]; n bool vis[ND]; n void add(int x, int y, int v) { n   e[++tot] = (edge){y, head[x], 1, v}; n   head[x] = tot; n   e[++tot] = (edge){x, head[y], 0, v}; n   head[y] = tot; n } n int times = 0; n bool bfs() { n   for (int i = (int)(1); i <= (int)(T); i++) dis[i] = 2333, vis[i] = 0; n   int h = 0, t = 1; n   q[1] = S; n   dis[S] = 1; n   while (h != t) { n     int x = q[++h]; n     vis[x] = 0; n     for (int i = head[x]; i; i = e[i].next) n       if (dis[e[i].to] > dis[x] + e[i].v && e[i].f) { n         dis[e[i].to] = dis[x] + e[i].v; n         fr[e[i].to] = i; n         if (!vis[e[i].to]) { n           vis[e[i].to] = 1; n           q[++t] = e[i].to; n         } n       } n   } n   if (dis[T] == 2333) return 0; n   return 1; n } n int dfs(int x, int flow) { n   if (x == T) return flow; n   int k, rest = flow; n   vis[x] = 1; n   for (int &i = cur[x]; i; i = e[i].next) n     if (e[i].f && !vis[e[i].to] && dis[e[i].to] == dis[x] + e[i].v) { n       k = dfs(e[i].to, min(rest, e[i].f)); n       e[i].f -= k; n       e[i ^ 1].f += k; n       rest -= k; n       if (!rest) break; n     } n   if (rest) dis[x] = -1; n   vis[x] = 0; n   return flow - rest; n } n struct fuckmdndandwzp { n   int i, j, beg, cnt; n } Ans[5000005]; n int ans_top; n const int OUT_SIZE = 100000000; n char out[OUT_SIZE]; n int out_top; n void write(int x) { n   if (!x) { n     out[out_top++] =  0 ; n     return; n   } n   static int a[15], top; n   for (top = 0; x; a[++top] = x % 10, x /= 10) n     ; n   for (; top; out[out_top++] = a[top--] +  0 ) n     ; n } n void writespace(int x) { n   write(x); n   out[out_top++] =    ; n } n void writeln(int x) { n   write(x); n   out[out_top++] =   n ; n } n int edge[N][N]; n bool visx[N], visy[N]; n int main() { n   scanf( %d%d%d , &n, &m, &B); n   for (int i = (int)(1); i <= (int)(m); i++) scanf( %d , &co[i]); n   for (int i = (int)(1); i <= (int)(n); i++) { n     int cnt; n     scanf( %d , &cnt); n     while (cnt--) { n       int x, y; n       scanf( %d%d , &x, &y); n       mp[i][x] = y; n       sx[i] += y; n       sy[x] += y; n     } n   } n   for (int i = (int)(1); i <= (int)(n); i++) ans = max(ans, sx[i]); n   for (int i = (int)(1); i <= (int)(m); i++) id[i] = i; n   sort(id + 1, id + m + 1, cmp); n   int p = m; n   for (; p && B >= co[id[p]]; B -= co[id[p--]]) n     ; n   for (int i = (int)(1); i <= (int)(p); i++) ans = max(ans, sy[id[i]]); n   for (int i = (int)(p + 1); i <= (int)(m); i++) { n     ans = max(ans, (sy[id[i]] + 1) / 2); n     fl[id[i]] = 1; n     int cnt = 0; n     for (int j = (int)(1); j <= (int)(n); j++) cnt += (mp[j][id[i]] & 1); n     cnt = (cnt + 1) / 2; n     for (int j = (int)(1); j <= (int)(n); j++) { n       int v = mp[j][id[i]]; n       mp[j][id[i]] = mp[j][id[i] + m] = v / 2; n       if (v & 1) { n         if ((--cnt) >= 0) n           mp[j][id[i]]++; n         else n           mp[j][id[i] + m]++; n       } n     } n   } n   writeln(ans); n   for (int i = (int)(1); i <= (int)(m); i++) write(fl[i]); n   out[out_top++] =   n ; n   m *= 2; n   for (int i = (int)(1); i <= (int)(n); i++) sx[i] = 0; n   for (int i = (int)(1); i <= (int)(m); i++) sy[i] = 0; n   for (int i = (int)(1); i <= (int)(n); i++) n     for (int j = (int)(1); j <= (int)(m); j++) n       sx[i] += mp[i][j], sy[j] += mp[i][j]; n   int times = 0; n   for (int TT = 0;;) { n     bool flag = 0; n     memset(head, 0, sizeof(head)); n     tot = 1; n     S = n + m + 1; n     T = S + 1; n     for (int i = (int)(1); i <= (int)(n); i++) n       for (int j = (int)(1); j <= (int)(m); j++) n         if (mp[i][j]) { n           edge[i][j] = tot + 1; n           flag = 1; n           add(i, j + n, 0); n         } n     if (!flag) break; n     for (int i = (int)(1); i <= (int)(n); i++) add(S, i, sx[i] != ans); n     for (int i = (int)(1); i <= (int)(m); i++) add(i + n, T, sy[i] != ans); n     for (; bfs(); times++) { n       for (int i = (int)(1); i <= (int)(T); i++) cur[i] = head[i], vis[i] = 0; n       dfs(S, 1e9); n     } n     int mn = 1e9; n     for (int i = (int)(1); i <= (int)(n); i++) visx[i] = 0; n     for (int i = (int)(1); i <= (int)(m); i++) visy[i] = 0; n     for (int i = (int)(1); i <= (int)(n); i++) n       for (int j = (int)(1); j <= (int)(m); j++) n         if (!e[edge[i][j]].f && mp[i][j]) n           mn = min(mn, mp[i][j]), visx[i] = visy[j] = 1; n     for (int i = (int)(1); i <= (int)(n); i++) n       if (!visx[i] && sx[i]) mn = min(mn, ans - sx[i]); n     for (int i = (int)(1); i <= (int)(m); i++) n       if (!visy[i] && sy[i]) mn = min(mn, ans - sy[i]); n     for (int i = (int)(1); i <= (int)(n); i++) n       for (int j = (int)(1); j <= (int)(m); j++) n         if (!e[edge[i][j]].f && mp[i][j]) { n           Ans[++ans_top] = (fuckmdndandwzp){i, j, TT, mn}; n           mp[i][j] -= mn; n           sx[i] -= mn; n           sy[j] -= mn; n         } n     TT += mn; n     ans -= mn; n   } n   writeln(ans_top); n   for (int i = (int)(1); i <= (int)(ans_top); i++) { n     writespace(Ans[i].i); n     writespace(Ans[i].j - (Ans[i].j <= m / 2 ? 0 : m / 2)); n     writespace(Ans[i].beg); n     writeln(Ans[i].cnt); n   } n   fwrite(out, 1, out_top, stdout); n } n


#include <bits/stdc++.h> n using namespace std; n int n, m, t; n pair<int, int> a[300005]; n int b[300005]; n int force(int ll, int rr) { n   int r = 0; n   for (int i = 1; i <= n; i++) n     if (a[i].first >= ll && a[i].second <= rr) r++; n   return r; n } n struct indexed_tree_2d { n   vector<int> T[1000006]; n   void set(int first, int second) { n     for (int i = first; i < 1000006; i += i & -i) T[i].push_back(second); n   } n   void end_update() { n     for (int i = 1; i < 1000006; i++) sort(T[i].begin(), T[i].end()); n   } n   int get(int first, int second) { n     int r = 0; n     for (int i = first; i >= 1; i -= i & -i) n       r += upper_bound(T[i].begin(), T[i].end(), second) - T[i].begin(); n     return r; n   } n   int get(int x1, int y1, int x2, int y2) { n     return get(x2, y2) - get(x1 - 1, y2) - get(x2, y1 - 1) + n            get(x1 - 1, y1 - 1); n   } n } tr; n int f(int ll, int rr) { n   if (ll > rr) return 0; n   return tr.get(ll, ll, rr, rr); n } n int main() { n   scanf( %d%d , &n, &t); n   for (int i = 1; i <= n; i++) { n     scanf( %d%d , &a[i].first, &a[i].second); n   } n   for (int i = 1; i <= n; i++) tr.set(a[i].first, a[i].second); n   tr.end_update(); n   for (int i = 1; i <= t; i++) { n     int answer = n; n     scanf( %d , &m); n     for (int j = 1; j <= m; j++) { n       scanf( %d , &b[j]); n     } n     sort(b + 1, b + m + 1); n     b[m + 1] = 1000006; n     for (int j = 1; j <= m + 1; j++) answer -= f(b[j - 1] + 1, b[j] - 1); n     printf( %d n , answer); n   } n } n


#include <bits/stdc++.h> n using namespace std; n vector<pair<int, pair<int, int> > > v[100005]; n vector<int> ans; n int n; n bool has_cycle(int mid) { n   int indeg[100005]; n   int cnt = 0, x; n   memset(indeg, 0, sizeof(indeg)); n   for (int i = 1; i <= n; ++i) { n     for (auto g : v[i]) { n       if (g.second.first > mid) { n         indeg[g.first]++; n       } n     } n   } n   queue<int> q; n   for (int i = 1; i <= n; ++i) { n     if (indeg[i] == 0) { n       q.push(i); n     } n   } n   while (!q.empty()) { n     x = q.front(); n     cnt++; n     q.pop(); n     for (auto g : v[x]) { n       if (g.second.first > mid) { n         indeg[g.first]--; n         if (indeg[g.first] == 0) q.push(g.first); n       } n     } n   } n   if (cnt == n) n     return false; n   else n     return true; n } n void topological_sort(int res) { n   int indeg[100005]; n   int cnt = 0, x; n   memset(indeg, 0, sizeof(indeg)); n   for (int i = 1; i <= n; ++i) { n     for (auto g : v[i]) { n       if (g.second.first > res) { n         indeg[g.first]++; n       } n     } n   } n   queue<int> q; n   bool vis[100005]; n   memset(vis, false, sizeof(vis)); n   for (int i = 1; i <= n; ++i) { n     if (indeg[i] == 0) q.push(i); n   } n   while (!q.empty()) { n     x = q.front(); n     vis[x] = true; n     q.pop(); n     for (auto g : v[x]) { n       if (g.second.first > res) { n         indeg[g.first]--; n         if (indeg[g.first] == 0) q.push(g.first); n       } else { n         if (vis[g.first] == true) { n           ans.push_back(g.second.second); n         } n       } n     } n   } n } n bool check(int mid) { n   if (has_cycle(mid)) n     return false; n   else n     return true; n } n int binary_search() { n   int lo = 0, hi = 1e9; n   while (hi - lo > 1) { n     int mid = (lo + hi) / 2; n     if (check(mid)) n       hi = mid; n     else n       lo = mid; n   } n   if (check(lo)) return lo; n   return hi; n } n int main() { n   int m, x, y, c, res; n   cin >> n >> m; n   for (int i = 1; i <= m; i++) { n     cin >> x >> y >> c; n     v[x].push_back({y, {c, i}}); n   } n   res = binary_search(); n   topological_sort(res); n   cout << res <<     << (int)ans.size() <<   n ; n   for (auto g : ans) cout << g <<    ; n } n


#include <bits/stdc++.h> n int func(int num) { n   if (num > 0) n     return 1; n   else if (num < 0) n     return -1; n   else n     return num; n } n int main() { n   char a[101], b[101]; n   int i; n   scanf( %s , &a); n   scanf( %s , &b); n   for (i = 0; i < strlen(a); i++) n     if (a[i] >=  A  && a[i] <=  Z ) a[i] +=  a  -  A ; n   for (i = 0; i < strlen(b); i++) n     if (b[i] >=  A  && b[i] <=  Z ) b[i] +=  a  -  A ; n   printf( %d n , func(strcmp(a, b))); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int DEBUG = 0; n int main(int argc, char **argv) { n   DEBUG = (argc >= 2) ? atoi(argv[1]) : 0; n   int t; n   cin >> t; n   for (int c = 0; c < t; c++) { n     char tmp; n     int board[8][8]; n     int kx[2]; n     int ky[2]; n     int eger = 0; n     for (int i = 0; i < 8; i++) { n       for (int j = 0; j < 8; j++) { n         cin >> tmp; n         if (tmp ==  . ) { n           board[i][j] = 1; n         } else if (tmp ==  # ) { n           board[i][j] = 0; n         } else { n           kx[eger] = i; n           ky[eger] = j; n           board[i][j] = 1; n           eger++; n         } n       } n     } n     bool good = false; n     if ((kx[0] - kx[1] + 12) % 4 == 0 && (ky[0] - ky[1] + 12) % 4 == 0) { n       int v = (kx[0] + ky[0]) % 4; n       for (int i = 0; i < 8; i++) { n         for (int j = 0; j < 8; j++) { n           if (board[i][j] == 1 && (i + j) % 4 == v) { n             good = true; n           } n         } n       } n     } n     if (good) { n       cout <<  YES  << endl; n     } else { n       cout <<  NO  << endl; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int64_t mod = 1000000007; n int main() { n   int n; n   cin >> n; n   int64_t ways = 1; n   for (int i = 0; i < 3 * n; ++i) { n     ways *= 3; n     ways %= mod; n   } n   int64_t bad = 1; n   for (int i = 0; i < n; ++i) { n     bad *= 7; n     bad %= mod; n   } n   cout << (mod + (ways - bad) % mod) % mod << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long A[100005 * 10]; n long long B[100005 * 10]; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   ; n   long long tst = 1; n   for (long long tt = 1; tt <= tst; tt++) { n     long long n, a, mn = 1e12, m, res = 0; n     cin >> m; n     for (long long i = 1; i <= m; i++) cin >> a, mn = min(mn, a); n     cin >> n; n     for (long long i = 0; i < n; i++) cin >> A[i]; n     ; n     sort(A, A + n, greater<long long>()); n     long long p1 = 0; n     while (p1 < n) { n       long long x = p1 + mn; n       while (p1 < x) res += A[p1], p1++; n       p1 += 2; n     } n     cout << res << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int N; n pair<pair<int, int>, int> V[100005]; n vector<int> r; n bool cmp(pair<pair<int, int>, int> a, pair<pair<int, int>, int> b) { n   if (a.first.first != b.first.first) return a.first.first > b.first.first; n   return a.first.second > b.first.second; n } n int main() { n   cin.sync_with_stdio(false); n   cout.sync_with_stdio(false); n   cin >> N; n   for (int i = 1; i <= N; i++) cin >> V[i].first.first; n   for (int i = 1; i <= N; i++) { n     cin >> V[i].first.second; n     V[i].second = i; n   } n   sort(V + 1, V + 1 + N, cmp); n   r.push_back(V[1].second); n   for (int i = 2; i <= N; i += 2) { n     if (V[i].first.second > V[i + 1].first.second) n       r.push_back(V[i].second); n     else n       r.push_back(V[i + 1].second); n   } n   cout << r.size() <<   n ; n   for (auto it : r) cout << it <<    ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n using LL = long long; n constexpr int maxn = 100; n int a[maxn]; n int main() { n   ios::sync_with_stdio(false); n   cin.tie(nullptr); n   cout.tie(nullptr); n   int t; n   for (cin >> t; t; t -= 1) { n     int n, x, am = 0, ok = 0; n     cin >> n >> x; n     for (int a; n; n -= 1) { n       cin >> a; n       am = max(a, am); n       if (a == x) ok = 1; n     } n     if (ok) n       cout <<  1 n ; n     else n       cout << max(2, (x + am - 1) / am) <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int caseno = 1; n typedef long long LL; n typedef unsigned long long ULL; n typedef long long int64; n typedef unsigned long long uint64; n const int SIZE = 100 + 10; n int ar[SIZE]; n int main() { n   std::ios_base::sync_with_stdio(0); n   int tcases, I, J, K, N, n, m, cnt = 0, len, f = 0, x, D, t; n   cin >> n; n   for (I = 0; I < n; I++) { n     cin >> ar[I]; n     f += ar[I]; n   } n   n++; n   D = 1; n   t = f; n   t += D; n   I = 1; n   x = 1; n   while (1) { n     x++; n     if (I == n) n       I = 1; n     else n       I++; n     if (x == t) break; n   } n   if (I != 1) cnt++; n   D = 2; n   t = f; n   t += D; n   I = 1; n   x = 1; n   while (1) { n     x++; n     if (I == n) n       I = 1; n     else n       I++; n     if (x == t) break; n   } n   if (I != 1) cnt++; n   D = 3; n   t = f; n   t += D; n   I = 1; n   x = 1; n   while (1) { n     x++; n     if (I == n) n       I = 1; n     else n       I++; n     if (x == t) break; n   } n   if (I != 1) cnt++; n   D = 4; n   t = f; n   t += D; n   I = 1; n   x = 1; n   while (1) { n     x++; n     if (I == n) n       I = 1; n     else n       I++; n     if (x == t) break; n   } n   if (I != 1) cnt++; n   D = 5; n   t = f; n   t += D; n   I = 1; n   x = 1; n   while (1) { n     x++; n     if (I == n) n       I = 1; n     else n       I++; n     if (x == t) break; n   } n   if (I != 1) cnt++; n   cout << cnt <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int INF = 2147483647; n const long long LLINF = 9223372036854775807LL; n int digsum(long long x) { n   int ans = 0; n   do { n     ans += x % 10; n     x /= 10; n   } while (x); n   return ans; n } n int main() { n   int a, b, c; n   cin >> a >> b >> c; n   vector<int> v; n   for (int sum = 1; sum <= 100; ++sum) { n     long long curres = b; n     for (int iter = 0; iter < a; ++iter) curres = curres * sum; n     curres += c; n     if (digsum(curres) == sum && curres > 0 && curres < 1000000000) n       v.push_back(curres); n   } n   sort(v.begin(), v.end()); n   printf( %d n , int((v).size())); n   for (int i = 0; i < int((v).size()); ++i) printf( %d  , v[i]); n   return 0; n } n


#include <bits/stdc++.h> n #pragma comment(linker,  /STACK:671088640 ) n using namespace std; n const int MOD = 1000000007; n const int INF = 20000000; n const long double EPS = 1e-7; n const int HASH_POW = 29; n const long double PI = acos(-1.0); n mt19937_64 rnd(1); n double workTime() { return double(clock()) / CLOCKS_PER_SEC; } n void my_return(int code) { exit(code); } n int bin_pow(int a, int n) { n   int ans = 1; n   while (n) { n     if (n & 1) ans = ans * 1ll * a % MOD; n     n >>= 1; n     a = a * 1ll * a % MOD; n   } n   return ans; n } n int f[100010], rev[100010]; n int C(int n, int k) { n   if (k < 0 || k > n) return 0; n   int res = f[n] * 1ll * rev[k] % MOD; n   return res * 1ll * rev[n - k] % MOD; n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   f[0] = 1; n   for (int i = 1; i <= 100000; ++i) f[i] = f[i - 1] * 1ll * i % MOD; n   for (int i = 0; i <= 100000; ++i) rev[i] = bin_pow(f[i], MOD - 2); n   int f, w, h; n   scanf( %d %d %d , &f, &w, &h); n   if (w == 0) { n     printf( 1 n ); n     my_return(0); n   } else if (f == 0) { n     if (w > h) { n       printf( 1 n ); n       my_return(0); n     } else { n       printf( 0 n ); n       my_return(0); n     } n   } else if (w <= h) { n     printf( 0 n ); n     my_return(0); n   } n   int total = 0, good = 0; n   for (int i = 1; i <= w; ++i) { n     if (i > 1) total = (total + C(w - 1, i - 1) * 1ll * C(f - 1, i - 2)) % MOD; n     total = (total + 2ll * C(w - 1, i - 1) * C(f - 1, i - 1)) % MOD; n     total = (total + C(w - 1, i - 1) * 1ll * C(f - 1, i)) % MOD; n   } n   for (int i = 1; i <= w; ++i) { n     if (i * (h + 1) > w) break; n     int w1 = w - i * h; n     if (i > 1) good = (good + C(w1 - 1, i - 1) * 1ll * C(f - 1, i - 2)) % MOD; n     good = (good + 2ll * C(w1 - 1, i - 1) * C(f - 1, i - 1)) % MOD; n     good = (good + C(w1 - 1, i - 1) * 1ll * C(f - 1, i)) % MOD; n   } n   printf( %d n , good * 1ll * bin_pow(total, MOD - 2) % MOD); n   my_return(0); n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int tst; n   cin >> tst; n   while (tst--) { n     int i, j, k, l, m, n; n     cin >> n; n     string s; n     cin >> s; n     set<int> ze, on; n     for (i = 0; i < n; i++) { n       if (s[i] ==  1 ) n         on.insert(i); n       else n         ze.insert(i); n     } n     int ans[n + 10]; n     memset(ans, 0, sizeof ans); n     int st = 1; n     j = 0; n     int ya = 0, kel, pt; n     st = 0; n     for (i = 0; i < n; i++) { n       if (ans[i]) continue; n       if (on.empty() || ze.empty()) break; n       st++; n       if (s[i] ==  0 ) { n         kel = i; n         pt = 0; n         while (1) { n           if (pt == 0) { n             auto x = on.lower_bound(kel); n             ans[kel] = st; n             ze.erase(kel); n             if (x == on.end()) { n               break; n             } else { n               l = *x; n               ans[l] = st; n               on.erase(l); n               pt = 1 - pt; n               kel = l; n             } n           } else { n             auto x = ze.lower_bound(kel); n             ans[kel] = st; n             on.erase(kel); n             if (x == ze.end()) { n               break; n             } else { n               l = *x; n               ans[l] = st; n               ze.erase(l); n               pt = 1 - pt; n               kel = l; n             } n           } n         } n       } else { n         pt = 0; n         kel = i; n         while (1) { n           if (pt == 0) { n             auto x = ze.lower_bound(kel); n             ans[kel] = st; n             on.erase(kel); n             if (x == ze.end()) { n               break; n             } else { n               l = *x; n               ans[l] = st; n               ze.erase(l); n               pt = 1 - pt; n               kel = l; n             } n           } else { n             auto x = on.lower_bound(kel); n             ans[kel] = st; n             ze.erase(kel); n             if (x == on.end()) { n               break; n             } else { n               l = *x; n               ans[l] = st; n               on.erase(l); n               pt = 1 - pt; n               kel = l; n             } n           } n         } n       } n     } n     st++; n     if (on.size()) { n       for (auto x : on) ans[x] = st, st++; n     } else if (ze.size()) { n       for (auto x : ze) ans[x] = st, st++; n     } n     set<int> kar; n     for (i = 0; i < n; i++) kar.insert(ans[i]); n     cout << kar.size() <<   n ; n     for (i = 0; i < n; i++) cout << ans[i] <<    ; n     cout <<   n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n unsigned long long int t, k, x; n int main() { n   ios_base::sync_with_stdio(0); n   cin >> t; n   for (; t > 0; t--) { n     cin >> k >> x; n     cout << (k - 1) * 9 + x << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int a[60000], x, mn = 0x3f3f3f3f; n int main() { n   int n, m; n   cin >> m >> n; n   for (int i = 1; i <= n; i++) { n     cin >> x; n     a[x]++; n   } n   sort(a + 1, a + m + 1); n   cout << a[1]; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 4e4 + 10; n const int inf = 1e9; n int t; n int n; n int b[N], ans[N]; n void solve() { n   cin >> n; n   for (int i = 0; i < n; i++) cin >> b[i]; n   long long tot = 0; n   for (int i = 0; i < n; i++) tot += b[i]; n   if (tot % (n * (n + 1) / 2) != 0) { n     cout <<  NO n ; n     return; n   } n   tot /= n * (n + 1) / 2; n   for (int i = 0; i < n; i++) { n     long long tmp = b[(i - 1 + n) % n] - b[i] + tot; n     if (tmp <= 0 || tmp % n != 0 || tmp / n > inf) { n       cout <<  NO n ; n       return; n     } n     ans[i] = tmp / n; n   } n   cout <<  YES n ; n   for (int i = 0; i < n; i++) cout << ans[i] <<    ; n   cout << endl; n } n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   cin >> t; n   while (t--) { n     solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long Max = 1e5 + 10; n int T; n long long n, m; n long long number[Max]; n int main() { n   cin >> T; n   while (T--) { n     cin >> n >> m; n     long long ans = 1; n     int k = 1; n     for (int i = 1;; i *= 2) { n       number[k] = number[k - 1] + i; n       if (number[k] > n) { n         ans = (ans * (n - number[k - 1] + 1) % m) % m; n         break; n       } else { n         ans = ans * (i + 1) % m; n       } n       k++; n     } n     cout << (ans - 1 + m) % m << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n const double eps = 1e-10; n const float epsf = 1e-6; n using namespace std; n inline long long int __gcd(long long int a, long long int b) { n   if (a == 0 || b == 0) { n     return max(a, b); n   } n   long long int tempa, tempb; n   while (1) { n     if (a % b == 0) n       return b; n     else { n       tempa = a; n       tempb = b; n       a = tempb; n       b = tempa % tempb; n     } n   } n } n inline int compfloat(float& x, float& y) { n   if (fabs(x - y) < epsf) n     return 0; n   else if (x - y > 0) n     return 1; n   return -1; n } n inline int compdouble(double x, double y) { n   if (fabs(x - y) < eps) n     return 0; n   else if (x - y > 0) n     return 1; n   else n     return -1; n } n bool prime(long long int k) { n   for (long long int i = 2; i * i <= k; i++) n     if (k % i == 0) { n       return false; n     } n   return true; n } n void pdash(int n = 1) { n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < 30; j++) { n       cout <<  - ; n     } n     cout <<   n ; n   } n } n long long int power(long long int x, long long int y) { n   long long int result = 1; n   while (y > 0) { n     if (y & 1) { n       result = (result * x); n     } n     y = y >> 1; n     x = (x * x); n   } n   return result; n } n long long int power(long long int x, long long int y, long long int z) { n   long long int result = 1; n   x = x % z; n   while (y > 0) { n     if (y & 1) { n       result = (result * x) % z; n     } n     y = y >> 1; n     x = (x * x) % z; n   } n   return result; n } n long long int modInverse(long long int n, long long int p) { n   return power(n, p - 2, p); n } n long long int nCrF(long long int n, long long int r, long long int p) { n   if (r == 0) return 1; n   long long int f[n + 1]; n   f[0] = 1; n   for (long long int i = 1; i <= n; i++) f[i] = f[i - 1] * i % p; n   return (f[n] * modInverse(f[r], p) % p * modInverse(f[n - r], p) % p) % p; n } n void cordinate_compression(vector<int>& v) { n   vector<int> p = v; n   sort(p.begin(), p.end()); n   p.erase(unique(p.begin(), p.end()), p.end()); n   for (int i = 0; i < (int)((v).size()); i++) n     v[i] = (int)(lower_bound(p.begin(), p.end(), v[i]) - p.begin()); n } n const int MX = 1400009; n vector<int> used(MX, 0); n vector<int> factor[MX]; n vector<int> isprime(MX, 1); n vector<bool> is_used(MX, 0); n inline bool check(int index) { n   for (auto& j : factor[index]) { n     if (is_used[j]) return 0; n   } n   for (auto& j : factor[index]) { n     is_used[j] = 1; n   } n   return 1; n } n void solve() { n   int n; n   cin >> n; n   int a[n]; n   for (int i = 2; i < MX; i++) { n     if (isprime[i]) { n       for (int j = i; j < MX; j += i) { n         factor[j].push_back(i); n         isprime[j] = 0; n       } n     } n   } n   int curr = 2; n   bool bz = 1; n   for (int i = 0; i < n; i++) { n     cin >> a[i]; n     if (bz) { n       curr = a[i]; n       while (!check(curr)) curr++; n       if (curr != a[i]) { n         bz = 0; n       } n       cout << curr <<    ; n       curr = 2; n     } else { n       while (!check(curr)) curr++; n       cout << curr <<    ; n     } n   } n   cout <<   n ; n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   int t = 1; n   while (t--) { n     solve(); n   } n } n


#include <bits/stdc++.h> n using namespace std; n double prec = 0.00000001; n void calcHistory(vector<double>& history, int hp2, double p1, int l1, int h1) { n   double h[201]; n   double newh[201]; n   memset(h, 0, sizeof(h)); n   memset(newh, 0, sizeof(newh)); n   h[hp2] = 1.; n   for (int i = 0; i < history.size(); ++i) { n     for (int j = 1; j <= hp2; ++j) n       if (h[j] > 0) { n         double pHit = (1. - p1) / (h1 - l1 + 1.); n         for (int idx = l1; idx <= h1; ++idx) { n           int nIdx = j - idx > 0 ? j - idx : 0; n           newh[nIdx] += pHit * h[j]; n         } n         newh[j] += p1 * h[j]; n       } n     history[i] = newh[0]; n     newh[0] = 0; n     for (int idx = 0; idx <= 200; ++idx) n       if (newh[idx] > prec) n         h[idx] = newh[idx]; n       else n         h[idx] = 0.; n     memset(newh, 0, sizeof(newh)); n   } n } n int main(int argc, char* argv[]) { n   stringstream strStr; n   int hp1; n   int dt1; n   int l1; n   int h1; n   int pp1; n   double p1; n   int hp2; n   int dt2; n   int l2; n   int h2; n   int pp2; n   double p2; n   cin >> hp1; n   cin >> dt1; n   cin >> l1; n   cin >> h1; n   cin >> pp1; n   cin >> hp2; n   cin >> dt2; n   cin >> l2; n   cin >> h2; n   cin >> pp2; n   strStr >> hp1; n   strStr >> dt1; n   strStr >> l1; n   strStr >> h1; n   strStr >> pp1; n   strStr >> hp2; n   strStr >> dt2; n   strStr >> l2; n   strStr >> h2; n   strStr >> pp2; n   p1 = double(pp1) / 100.; n   p2 = double(pp2) / 100.; n   vector<double> history(5000, 0.); n   calcHistory(history, hp2, p1, l1, h1); n   double res = 0; n   double h[201]; n   double newh[201]; n   memset(h, 0, sizeof(h)); n   memset(newh, 0, sizeof(newh)); n   h[hp1] = 1.; n   int k = 0; n   double p = 0.; n   double pp = 0.; n   for (int i = 0; i < history.size(); ++i) { n     double histP1 = history[i]; n     while (dt2 * k < dt1 * i) { n       ++k; n       for (int j = 1; j <= hp1; ++j) n         if (h[j] > 0) { n           double pHit = (1. - p2) / (h2 - l2 + 1.); n           for (int idx = l2; idx <= h2; ++idx) { n             int nIdx = j - idx > 0 ? j - idx : 0; n             newh[nIdx] += pHit * h[j]; n           } n           newh[j] += p2 * h[j]; n         } n       pp += newh[0]; n       for (int idx = 0; idx <= 200; ++idx) n         if (newh[idx] > prec) n           h[idx] = newh[idx]; n         else n           h[idx] = 0.; n       memset(newh, 0, sizeof(newh)); n     } n     res += histP1 * (1. - pp); n     p += histP1; n   } n   cout << showpoint << res; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m; n long long l[200020], r[200020]; n struct node { n   long long x, y, p; n } a[200020]; n long long ans[200020]; n struct bridge { n   long long p, v; n }; n struct Compare { n   bool operator()(const bridge& a, const bridge& b) const { n     if (a.v < b.v || (a.v == b.v && a.p < b.p)) n       return true; n     else n       return false; n   } n }; n set<bridge, Compare> b; n bool cmp(node a, node b) { n   if (a.x > b.x) return true; n   if (a.x < b.x) return false; n   if (a.y > b.y) return true; n   return false; n } n int main() { n   scanf( %d%d , &n, &m); n   for (int i = 1; i <= n; i++) scanf( %lld%lld , &l[i], &r[i]); n   long long j; n   for (int i = 1; i <= m; i++) { n     bridge tmp; n     scanf( %lld , &j); n     tmp.v = j; n     tmp.p = i; n     b.insert(tmp); n   } n   if (m < n - 1) { n     printf( No n ); n     return 0; n   } n   for (int i = 1; i < n; i++) { n     a[i].x = l[i + 1] - r[i]; n     a[i].y = r[i + 1] - l[i]; n     a[i].p = i; n   } n   sort(a + 1, a + n, cmp); n   bool ok = true; n   set<bridge, Compare>::iterator iter; n   bridge tmp; n   for (int i = 1; i < n; i++) { n     tmp.v = a[i].y + 1; n     tmp.p = -1; n     iter = b.lower_bound(tmp); n     if (iter == b.begin()) { n       ok = false; n       break; n     } n     iter--; n     if (iter == b.end() || iter->v < a[i].x) { n       ok = false; n       break; n     } else { n       ans[a[i].p] = iter->p; n       b.erase(iter); n     } n   } n   if (ok) { n     printf( Yes n ); n     for (int i = 1; i < n; i++) printf( %lld  , ans[i]); n   } else n     printf( No n ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int a[111]; n int main() { n   int mx = 1000000000; n   int cnt, n; n   scanf( %d , &n); n   int sum = 0; n   cnt = 0; n   int k = 0; n   for (int i = 1; i < n * 2; ++i) { n     scanf( %d , &a[i]); n     sum += abs(a[i]); n     mx = ((mx) < (abs(a[i])) ? (mx) : (abs(a[i]))); n     if (a[i] < 0) { n       cnt++; n     } n     if (a[i] == 0) k++; n   } n   if (cnt % 2 == 0 || n % 2 == 1 || k != 0) n     printf( %d n , sum); n   else { n     printf( %d n , sum - mx - mx); n   } n } n


#include <bits/stdc++.h> n using namespace std; n const double eps = 1e-8; n char a[1010], b[1010]; n int h = 0; n int main() { n   scanf( %s , a); n   int l = strlen(a); n   int j = 0; n   for (int i = 0; i < l; i++) { n     b[j++] = a[i]; n     if (a[i] ==  > ) { n       b[j] = 0; n       if (b[1] ==  / ) h--; n       for (int k = 0; k < 2 * h; k++) putchar(   ); n       if (b[1] !=  / ) h++; n       puts(b); n       j = 0; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n default_random_engine generator; n uniform_int_distribution<int> distribution(1, INT_MAX); n long long my_rand(long long a = 0, long long b = INT_MAX) { n   return a + distribution(generator) % b; n } n inline int mul(int a, int b) { return int(a * 1ll * b % 998244353); } n inline int norm(int a) { n   if (a >= 998244353) a -= 998244353; n   if (a < 0) a += 998244353; n   return a; n } n inline int binPow(int a, int k) { n   int ans = 1; n   while (k > 0) { n     if (k & 1) ans = mul(ans, a); n     a = mul(a, a); n     k >>= 1; n   } n   return ans; n } n inline int subtract(int a, int b) { return norm(a - b); } n inline int sum(int a, int b) { return int((a + b) % 998244353); } n inline int inv(int a) { return binPow(a, 998244353 - 2); } n long long ceil_int(long long a, long long b) { return a / b + (a % b != 0); } n long long gcd(long long a, long long b) { n   if (a == 0) return b; n   return gcd(b % a, a); n } n long long lcm(long long a, long long b) { return (a * b) / gcd(a, b); } n int main() { n   generator.seed(chrono::system_clock::now().time_since_epoch().count()); n   cin.sync_with_stdio(false); n   cin.tie(0); n   int n; n   cin >> n; n   vector<int> a(n); n   int sum = 0; n   for (int i = 0; i < n; i++) { n     cin >> a[i]; n     sum += a[i]; n   } n   int res = sum; n   sort(a.begin(), a.end()); n   for (int i = 1; i <= 100; i++) { n     int cur = sum; n     for (int j = n - 1; j > 0; j--) { n       if (a[j] % i == 0) { n         cur += ((a[j] / i) - a[j]); n         cur += (a[0] * i - a[0]); n       } n       res = min(res, cur); n       cur = sum; n     } n   } n   cout << res; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1000010; n struct node { n   int x, id; n } a[N], ans[N]; n int anss; n bool cmp(node x, node y) { n   if (x.x != y.x) return x.x > y.x; n   return x.id < y.id; n } n void solve() { n   int k = a[2].x / a[3].x; n   while (k) { n     if (k & 1) { n       a[2].x -= a[3].x; n       a[3].x <<= 1; n       ans[++anss].x = a[3].id; n       ans[anss].id = a[2].id; n     } else { n       a[1].x -= a[3].x; n       a[3].x <<= 1; n       ans[++anss].x = a[3].id; n       ans[anss].id = a[1].id; n     } n     k >>= 1; n   } n } n int n; n int main() { n   scanf( %d , &n); n   for (int i = 1; i <= n; ++i) a[i].id = i, scanf( %d , &a[i].x); n   sort(a + 1, a + n + 1, cmp); n   if (!a[2].x) { n     puts( -1 ); n     return 0; n   } n   while (a[3].x) { n     solve(); n     sort(a + 1, a + n + 1, cmp); n   } n   printf( %d n , anss); n   for (int i = 1; i <= anss; ++i) printf( %d %d n , ans[i].x, ans[i].id); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 59; n const long long mod = 998244353; n int add[maxn]; n long long w[maxn]; n long long dp[52][52][52]; n long long val[52][52][52]; n bool vis[52][52][52]; n long long Pow(long long a, long long b) { n   long long res = 1; n   a %= mod; n   while (b) { n     if (b & 1) res = res * a % mod; n     b >>= 1; n     a = a * a % mod; n   } n   return res; n } n inline long long inv(long long a) { return Pow(a, mod - 2); } n int flag; n long long dfs(int step, long long p, int i, int j, int k, long long x, n               long long y, long long z) { n   if (step == 0) return p * x % mod; n   if (vis[i][j][k]) { n     return val[i][j][k] * inv(dp[i][j][k]) % mod * p % mod; n   } n   dp[i][j][k] = p; n   val[i][j][k] += dfs(step - 1, p * x % mod * inv(x + y + z) % mod, i + 1, j, k, n                       max(0ll, x + flag), y, z); n   val[i][j][k] += dfs(step - 1, p * y % mod * inv(x + y + z) % mod, i, j + 1, k, n                       x, y + 1, z); n   val[i][j][k] += dfs(step - 1, p * z % mod * inv(x + y + z) % mod, i, j, k + 1, n                       x, y, max(0ll, z - 1)); n   val[i][j][k] %= mod; n   vis[i][j][k] = 1; n   return val[i][j][k]; n } n int main() { n   int n, m; n   scanf( %d%d , &n, &m); n   long long ori, s0 = 0, s1 = 0; n   for (int i = 1; i <= n; i++) scanf( %d , add + i); n   for (int i = 1; i <= n; i++) { n     scanf( %lld , w + i); n     if (add[i]) n       s1 += w[i]; n     else n       s0 += w[i]; n   } n   ori = s0 + s1; n   int x, y, z, step; n   for (int i = 1; i <= n; i++) { n     memset(dp, 0, sizeof dp); n     memset(val, 0, sizeof val); n     memset(vis, 0, sizeof vis); n     if (add[i]) n       flag = 1, val[0][0][0] = dfs(m, 1, 0, 0, 0, w[i], s1 - w[i], s0); n     else n       flag = -1, val[0][0][0] = dfs(m, 1, 0, 0, 0, w[i], s1, s0 - w[i]); n     printf( %lld n , val[0][0][0]); n   } n } n


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


#include <bits/stdc++.h> n using namespace std; n using LL = long long int; n template <class TH> n void _dbg(const char *sdbg, TH h) { n   cerr << sdbg <<  =  << h <<   n ; n } n template <class TH, class... TA> n void _dbg(const char *sdbg, TH h, TA... a) { n   while (*sdbg !=  , ) cerr << *sdbg++; n   cerr <<  =  << h <<  , ; n   _dbg(sdbg + 1, a...); n } n template <class T> n ostream &operator<<(ostream &os, vector<T> V) { n   os <<  [ ; n   for (auto vv : V) os << vv <<  , ; n   return os <<  ] ; n } n template <class L, class R> n ostream &operator<<(ostream &os, pair<L, R> P) { n   return os <<  (  << P.st <<  ,  << P.nd <<  ) ; n } n const int MAXN = 3003; n string t, s; n int n, m; n LL dp[MAXN][MAXN]; n LL mod = 998244353; n bool vis[MAXN][MAXN]; n LL dfs(int i, int j) { n   (i, j); n   if (j == i - 1) { n     return 1; n   } n   if (vis[i][j]) { n     return dp[i][j]; n   } n   vis[i][j] = true; n   char last = s[(j - i) + 1]; n   LL actRes = 0; n   if (i > m || last == t[i]) { n     actRes += dfs(i + 1, j); n     actRes %= mod; n   } n   if (j > m || last == t[j]) { n     actRes += dfs(i, j - 1); n     actRes %= mod; n   } n   dp[i][j] = actRes; n   return dp[i][j]; n } n int main() { n   cin >> s >> t; n   n = s.size(); n   m = t.size(); n   s =  #  + s; n   t =  #  + t; n   LL res = dfs(1, n); n   res = 0; n   for (int i = m; i <= n; ++i) { n     res += dp[1][i]; n     res %= mod; n   } n   cout << res << endl; n } n


#include <bits/stdc++.h> n int main() { n   std::ios::sync_with_stdio(false); n   int n; n   long long m, k; n   long long tmp; n   std::cin >> n >> m >> k; n   std::vector<long long> a; n   for (int i = 0; i < n; i++) { n     std::cin >> tmp; n     a.push_back(tmp); n   } n   std::sort(a.begin(), a.end()); n   std::cout << (m / (k + 1)) * (*(a.end() - 2)) + n                    (m - (m / (k + 1))) * (*(a.end() - 1)); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long oo = 1000000000000000000; n long long sum, ans = 0, mx = 0, mn = 1000000000, num, pos; n vector<int> v[100001]; n long long int a[100001], mxx[100001]; n map<int, int> mp[100001]; n void unin(int x, int y) { n   if (mp[x].size() < mp[y].size()) { n     mp[x].swap(mp[y]); n     mxx[x] = mxx[y]; n     a[x] = a[y]; n   } n   for (map<int, int>::iterator it = mp[y].begin(); it != mp[y].end(); it++) { n     mp[x][(it)->first] += (it)->second; n     if (mp[x][it->first] == mxx[x]) { n       a[x] += it->first; n     } else if (mp[x][it->first] > mxx[x]) { n       mxx[x] = mp[x][it->first]; n       a[x] = it->first; n     } n   } n } n void dfs(int u, int pr = -1) { n   for (int i = 0; i < v[u].size(); i++) { n     int to = v[u][i]; n     if (to == pr) continue; n     dfs(to, u); n     unin(u, to); n   } n } n int main() { n   int n; n   cin >> n; n   for (int i = 1; i <= n; i++) { n     cin >> num; n     mp[i][num]++; n     mxx[i] = 1; n     a[i] = num; n   } n   int x, y; n   for (int i = 1; i < n; i++) { n     cin >> x >> y; n     v[x].push_back(y); n     v[y].push_back(x); n   } n   dfs(1); n   for (int i = 1; i <= n; i++) { n     num = 0; n     cout << a[i] <<    ; n     sum = 0; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   vector<int> a(6); n   int s = 0; n   for (int i = 0; i < 6; i++) { n     cin >> a[i]; n     s += a[i]; n   } n   if (s % 2 != 0) { n     cout <<  NO ; n     return 0; n   } n   for (int i = 0; i < 6; i++) n     for (int j = i + 1; j < 6; j++) n       for (int k = j + 1; k < 6; k++) n         if (s / 2 == a[i] + a[j] + a[k]) { n           cout <<  YES ; n           return 0; n         } n   cout <<  NO ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long ans = 0; n   int n, m, k = 0; n   scanf( %d %d , &n, &m); n   vector<int> v(1e7); n   for (int i = 0; i < n; i++) { n     scanf(  %d , &k); n     ans += v[(k ^ m)]; n     v[k]++; n   } n   printf( %I64d , ans); n } n


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


#include <bits/stdc++.h> n int main() { n   int m, s, c, i; n   std::cin >> m >> s; n   c = s; n   if (m == 1 && c == 0) n     std::cout << 0 <<     << 0; n   else { n     if (c == 0 || c > (9 * m)) { n       std::cout << -1 <<     << -1; n     } else { n       for (i = m; i > 0; i--) { n         if (c <= 9 * (i - 1)) { n           if (i == m) { n             std::cout << 1; n             c--; n           } else n             std::cout << 0; n         } else { n           std::cout << (c - 9 * (i - 1)); n           c = 9 * (i - 1); n         } n       } n       std::cout <<    ; n       c = s; n       for (i = m - 1; i >= 0; i--) { n         if (c <= 9) { n           std::cout << c; n           c = 0; n         } else { n           std::cout << 9; n           c = c - 9; n         } n       } n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <size_t N, size_t M> n struct Suffix_Automata { n   int maxlen[N], trans[N][M], link[N], Size, Last, isclone[N]; n   Suffix_Automata() { Size = Last = 1; } n   void Extend(int id) { n     int cur = (++Size), p; n     maxlen[cur] = maxlen[Last] + 1; n     for (p = Last; p && !trans[p][id]; p = link[p]) trans[p][id] = cur; n     if (!p) n       link[cur] = 1; n     else { n       int q = trans[p][id]; n       if (maxlen[q] == maxlen[p] + 1) n         link[cur] = q; n       else { n         int clone = (++Size); n         maxlen[clone] = maxlen[p] + 1; n         memcpy(trans[clone], trans[q], sizeof(trans[q])); n         link[clone] = link[q]; n         for (; p && trans[p][id] == q; p = link[p]) trans[p][id] = clone; n         link[cur] = link[q] = clone; n         isclone[clone] = true; n       } n     } n     isclone[cur] = false; n     Last = cur; n   } n   int cnt[N], in[N]; n   vector<int> G[N]; n   long long calc() { n     long long res = 0; n     for (int i = 1; i <= Size; i++) cnt[i] = !isclone[i]; n     for (int i = 1; i <= Size; i++) { n       G[i].clear(); n       G[i].push_back(link[i]); n       ++in[link[i]]; n     } n     queue<int> Q; n     for (int i = 1; i <= Size; i++) { n       if (!in[i]) { n         Q.push(i); n       } n     } n     while (!Q.empty()) { n       int u = Q.front(); n       Q.pop(); n       for (int v : G[u]) { n         cnt[v] += cnt[u]; n         if (!(--in[v])) { n           Q.push(v); n         } n       } n     } n     for (int i = 1; i <= Size; i++) { n       res += 1LL * cnt[i] * cnt[i] * (maxlen[i] - maxlen[link[i]]); n     } n     return res; n   } n }; n Suffix_Automata<300000, 26> SAM; n char s[300000]; n int main() { n   int T; n   scanf( %d , &T); n   for (int _ = 0; _ < T; _++) { n     memset(SAM.trans, 0, sizeof(SAM.trans)); n     SAM.Last = SAM.Size = 1; n     scanf( %s , s + 1); n     int n = strlen(s + 1); n     for (int i = 1; i <= n; i++) { n       SAM.Extend(s[i] -  a ); n     } n     printf( %lld n , SAM.calc()); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n; n string s, t, a; n void dfs(int i, int c4, int c7, bool d) { n   if (c4 + n - i < n / 2 || c7 + n - i < n / 2) return; n   if (i == n) { n     a = t; n     return; n   } n   if (d) { n     t[i] =  4 ; n     dfs(i + 1, c4 + 1, c7, 1); n     if (a !=   ) return; n     t[i] =  7 ; n     dfs(i + 1, c4, c7 + 1, 1); n   } else { n     if (s[i] <=  4 ) { n       t[i] =  4 ; n       dfs(i + 1, c4 + 1, c7, s[i] <  4 ); n       if (a !=   ) return; n     } n     if (s[i] <=  7 ) { n       t[i] =  7 ; n       dfs(i + 1, c4, c7 + 1, s[i] <  7 ); n     } n   } n } n int main() { n   cin >> s; n restart: n   if (s.size() % 2) s =  0  + s; n   n = s.size(); n   t = string(n,  0 ); n   dfs(0, 0, 0, 0); n   if (a ==   ) { n     s =  00  + s; n     goto restart; n   } else { n     cout << a << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n void read(int &k) { n   k = 0; n   char x = getchar(); n   while (x <  0  || x >  9 ) x = getchar(); n   while (x >=  0  && x <=  9 ) { n     k = k * 10 - 48 + x; n     x = getchar(); n   } n } n int n, a[55], K; n long double dp[55][55][55], ans, fac[55]; n int main() { n   scanf( %d , &n); n   int sum = 0; n   for (int i = 0; i < n; i++) scanf( %d , &a[i]), sum += a[i]; n   scanf( %d , &K); n   if (sum <= K) { n     printf( %.9lf n , double(n)); n     return 0; n   } n   for (int i = 0; i < n; i++) fac[0] = 1; n   for (int i = 1; i <= n; i++) fac[i] = fac[i - 1] * i; n   for (int pos = 0; pos < n; pos++) { n     memset(dp, 0, sizeof dp); n     dp[0][0][0] = 1; n     for (int i = 0; i < n; i++) { n       if (i == pos) { n         memcpy(dp[i + 1], dp[i], sizeof dp[i]); n         continue; n       } n       for (int j = 0; j <= i; j++) n         for (int k = 0; k <= K; k++) { n           if (k + a[i] <= K) dp[i + 1][j + 1][k + a[i]] += dp[i][j][k]; n           dp[i + 1][j][k] += dp[i][j][k]; n         } n     } n     for (int i = 1; i <= n; i++) n       for (int j = max(K - a[pos] + 1, 1); j <= K; j++) n         ans += ((long double)dp[n][i][j]) * fac[i] * fac[n - i - 1] * i; n   } n   ans /= fac[n]; n   printf( %.9lf n , double(ans)); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int ok[101010]; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   int n; n   cin >> n; n   vector<int> ct(n); n   int tl = 0; n   int nn = 0; n   int odd = 0; n   for (int i = 0; i < n; i++) { n     cin >> ct[i]; n     tl += ct[i]; n     if (ct[i] % 2 == 1) odd++; n     if (ct[i] > 0) { n       nn++; n       for (int j = 1; j <= ct[i]; j++) { n         if (ct[i] % j == 0) { n           ok[j]++; n         } n       } n     } n   } n   if (nn < 2) { n     cout << tl << endl; n     for (int i = 0; i < n; i++) { n       for (int j = 0; j < ct[i]; j++) { n         cout << (char)( a  + i); n       } n     } n     cout << endl; n   } else if (odd > 1) { n     cout << 0 << endl; n     for (int i = 0; i < n; i++) { n       for (int j = 0; j < ct[i]; j++) { n         cout << (char)( a  + i); n       } n     } n     cout << endl; n   } else { n     int bv = 1; n     for (int i = 1; i <= tl; i++) { n       if (ok[i] == nn) { n         int x = i; n         int o = 0; n         for (int ii = 0; ii < n; ii++) { n           ct[ii] /= x; n           if (ct[ii] % 2 == 1) o++; n         } n         if (o < 2 || (i % 2 == 0)) { n           bv = max(bv, i); n         } n         for (int ii = 0; ii < n; ii++) { n           ct[ii] *= x; n         } n       } n     } n     cout << bv << endl; n     tl /= bv; n     string r(tl,    ); n     int of = 0; n     for (int ii = 0; ii < n; ii++) { n       ct[ii] /= bv; n       if (ct[ii] % 2 == 1) of = 1; n     } n     if (bv % 2 == 0) { n       int i2 = 0; n       for (int i = 0; i < (int)ct.size(); i++) { n         for (int j = 0; j < ct[i]; j++) { n           r[i2++] = (char)( a  + i); n         } n       } n     } else { n       int i2 = 0; n       if (of) { n         for (int i = 0; i < (int)ct.size(); i++) { n           if (ct[i] % 2 == 1) { n             for (int j = 0; j < ct[i]; j += 2) { n               r[tl / 2 - i2] = (char)( a  + i); n               r[tl / 2 + i2] = (char)( a  + i); n               i2++; n             } n           } n         } n         for (int i = 0; i < (int)ct.size(); i++) { n           if (ct[i] % 2 == 0) { n             for (int j = 0; j < ct[i]; j += 2) { n               r[tl / 2 - i2] = (char)( a  + i); n               r[tl / 2 + i2] = (char)( a  + i); n               i2++; n             } n           } n         } n       } else { n         for (int i = 0; i < (int)ct.size(); i++) { n           if (ct[i] % 2 == 0) { n             for (int j = 0; j < ct[i]; j += 2) { n               r[tl / 2 - i2 - 1] = (char)( a  + i); n               r[tl / 2 + i2] = (char)( a  + i); n               i2++; n             } n           } n         } n       } n     } n     string v =   ; n     for (int i = 0; i < bv; i++) { n       v = v + r; n       reverse(r.begin(), r.end()); n     } n     cout << v << endl; n   } n } n


#include <bits/stdc++.h> n #pragma GCC optimize ( Ofast ,  unroll-loops ) n using namespace std; n #define ll long long n //#define int ll n #define FOR(i,a,b) for (int i = (a); i<(b); i++) n #define REP(i,n) FOR(i,0,n) n #define REP1(i,n) FOR(i,1,n+1) n #define RREP(i,n) for (int i=(n)-1; i>=0; i--) n #define f first n #define s second n #define pb push_back n #define ALL(x) x.begin(),x.end() n #define SZ(x) (int)(x.size()) n #define SQ(x) (x)*(x) n #define pii pair<int, int> n #define pdd pair<double ,double> n #define pcc pair<char, char>  n #define endl   n  n //#define TOAD n #ifdef TOAD n #define bug(x) cerr<<__LINE__<< :  <<#x<<  is  <<x<<endl n #define IOS() n #else n #define bug(...) n #define IOS() ios::sync_with_stdio(0), cin.tie(0), cout.tie(0) n #endif n const ll inf = 1ll<<60; n const int iinf=2147483647; n const ll mod = 1e9+7; n const ll maxn=1e7+5; n const double PI=acos(-1); n ll pw(ll x, ll p){ n     ll ret=1; n     while (p>0){ n         if (p&1){ n             ret*=x; n             ret%=mod; n         } n         x*=x; n         x%=mod; n         p>>=1; n     } n     return ret; n } n  n ll inv(ll a){ n     return pw(a,mod-2);     n } n  n signed main(){ n     IOS(); n     int t; cin>>t; n     while(t--){ n         int n; cin>>n; n         vector<pii> vc(n); n         vector<int> t(n); n         REP(i,n) cin>>vc[i].f>>vc[i].s; n         REP(i,n) cin>>t[i]; n         int cur=vc[0].f, ans=0; n         REP(i,n){ n             cur+=t[i]; n             if (i==n-1) break; n             int ex=(vc[i].s-vc[i].f)/2+((vc[i].s-vc[i].f)%2>0); n             cur+=ex; n             cur=max(cur, vc[i].s); n             cur+=vc[i+1].f-vc[i].s; n  n         } n         cout<<cur<<endl; n     } n }


#include <bits/stdc++.h> n using namespace std; n const int maxn = 100005; n int main() { n   ios::sync_with_stdio(false); n   int t; n   cin >> t; n   while (t--) { n     long long x, y, z; n     cin >> x >> y >> z; n     long long sum = x + y + z; n     cout << (sum / 2) << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const double EPS = 1e-9; n const double PI = acos(-1.0); n const int MOD = 1000 * 1000 * 1000 + 7; n const int INF = 2000 * 1000 * 1000; n const int MAXN = 200010; n template <typename T> n inline T sqr(T n) { n   return n * n; n } n int n, k; n vector<int> g[MAXN]; n long long sum[MAXN][7]; n long long dp[MAXN]; n long long size[MAXN][7]; n void dfs(int v, int par = -1) { n   size[v][0] = 1; n   for (int to : g[v]) { n     if (to != par) { n       dfs(to, v); n       for (int mod = 1; mod <= k; mod++) { n         size[v][mod % k] += size[to][mod - 1]; n         sum[v][mod % k] += sum[to][mod - 1]; n       } n       sum[v][0] += size[to][k - 1]; n     } n   } n   for (int m2 = 0; m2 < k; m2++) { n     long long summ2 = 0, sizem2 = 0; n     for (int to : g[v]) { n       if (to != par) { n         summ2 += sum[to][m2]; n         sizem2 += size[to][m2]; n       } n     } n     for (int m1 = 0; m1 < k; m1++) { n       for (int to : g[v]) { n         if (to != par) { n           long long osize = sizem2 - size[to][m2]; n           long long isize = size[to][m1]; n           dp[v] += sum[to][m1] * osize + (summ2 - sum[to][m2]) * isize; n           dp[v] += isize * osize * ((m1 + m2 + 2 + k - 1) / k); n         } n       } n     } n   } n   for (int m = 0; m < k; m++) { n     dp[v] += 2 * (sum[v][m] + size[v][m] * (m != 0)); n   } n   for (int to : g[v]) { n     if (to != par) { n       dp[v] += dp[to]; n     } n   } n } n int main() { n   scanf( %d%d , &n, &k); n   for (int i = 0; i < n - 1; i++) { n     int u, v; n     scanf( %d%d , &u, &v); n     g[u].push_back(v); n     g[v].push_back(u); n   } n   dfs(1); n   printf( %lld n , dp[1] / 2); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 2e5 + 100; n int n, m, k; n long long sum[maxn], dp[maxn]; n int d[maxn]; n pair<int, int> p[maxn]; n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   int cnt = 0; n   while (cnt <= 1e9) { n     ++cnt; n   } n   cin >> n >> m >> k; n   for (int i = 1; i <= n; ++i) { n     cin >> d[i]; n   } n   sort(d + 1, d + 1 + n); n   for (int i = 0; i < m; ++i) { n     cin >> p[i].first >> p[i].second; n   } n   sum[0] = 0; n   for (int i = 1; i <= k; ++i) { n     sum[i] = sum[i - 1] + d[i]; n     dp[i] = 0; n   } n   for (int j = 1; j <= k; ++j) { n     for (int i = 0; i < m; ++i) { n       if (p[i].first <= j) n         dp[j] = n             max(dp[j], dp[j - p[i].first] + sum[j + p[i].second - p[i].first] - n                            sum[j - p[i].first]); n     } n   } n   long long ans = 0; n   for (int i = 1; i <= k; ++i) { n     ans = max(ans, dp[i]); n   } n   cout << sum[k] - ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <class T> n inline void smn(T &a, const T &b) { n   if (b < a) a = b; n } n template <class T> n inline void smx(T &a, const T &b) { n   if (b > a) a = b; n } n template <class T> n inline T rev(const T &a) { n   T _ = a; n   reverse(_.begin(), _.end()); n   return _; n } n const double eps = 1e-9; n const long double leps = 1e-14; n int a, x, y; n int main(int argc, char *argv[]) { n   ios_base::sync_with_stdio(false); n   cin >> a >> x >> y; n   if (y % a == 0) { n     cout << -1 <<   n ; n     return 0; n   } n   if (y < a) { n     if (2 * abs(x) >= a) n       cout << -1 <<   n ; n     else n       cout << 1 <<   n ; n     return 0; n   } n   y -= a; n   int t = y / a; n   int now = 2 + 3 * (t / 2); n   if (t % 2 == 1) now++; n   if (t % 2 == 0) { n     if (2 * abs(x) >= a) n       cout << -1 <<   n ; n     else n       cout << now <<   n ; n     return 0; n   } n   if (abs(x) >= a || x == 0) n     cout << -1 <<   n ; n   else if (x < 0) n     cout << now <<   n ; n   else n     cout << now + 1 <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int ans[30], tans[30], flot[30]; n   memset(flot, 0, sizeof(flot)); n   memset(ans, 0x3f3f3f3f, sizeof(ans)); n   int n, num[55], top = 0, m; n   string s, s1; n   cin >> n; n   cin >> s; n   for (int i = 0; i < n; i++) { n     if (s[i] ==  * ) n       num[top++] = i; n     else { n       flot[s[i] -  a ] = 1; n       ans[s[i] -  a ] = 0; n     } n   } n   cin >> m; n   for (int i = 0; i < m; i++) { n     int tt = 0; n     memset(tans, 0, sizeof(tans)); n     cin >> s1; n     for (int i = 0; i < top; i++) { n       if (flot[s1[num[i]] -  a ] == 1) { n         tt = 1; n         break; n       } n       tans[s1[num[i]] -  a ]++; n     } n     for (int i = 0; i < n; i++) { n       if (s[i] ==  * ) continue; n       if (s[i] != s1[i]) { n         tt = 1; n         break; n       } n     } n     if (tt) continue; n     for (int i = 0; i < 30; i++) ans[i] = min(ans[i], tans[i]); n   } n   int je = 0; n   for (int i = 0; i < 30; i++) n     if (ans[i] != 0) je++; n   cout << je << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, s, e; n int x[5555], a[5555], b[5555], c[5555], d[5555]; n long long dist(int from, int to) { n   return 1LL * abs(x[from] - x[to]) + n          (from < to ? d[from] + a[to] : c[from] + b[to]); n } n int main() { n   ios::sync_with_stdio(0); n   cin >> n >> s >> e; n   for (int i = 1; i <= n; ++i) { n     cin >> x[i]; n   } n   for (int i = 1; i <= n; ++i) { n     cin >> a[i]; n   } n   for (int i = 1; i <= n; ++i) { n     cin >> b[i]; n   } n   for (int i = 1; i <= n; ++i) { n     cin >> c[i]; n   } n   for (int i = 1; i <= n; ++i) { n     cin >> d[i]; n   } n   long long ans = dist(s, e); n   list<int> path; n   path.push_back(s); n   path.push_back(e); n   for (int i = 1; i <= n; ++i) { n     if (i == s || i == e) continue; n     list<int>::iterator best_it = path.begin(); n     long long next_ans = 1e18; n     for (auto it = path.begin(); it != path.end(); ++it) { n       if (*it == e) continue; n       int from = *it; n       auto next_iter = next(it); n       int to = *next_iter; n       long long current_ans = n           ans - dist(from, to) + dist(from, i) + dist(i, to); n       if (current_ans < next_ans) { n         next_ans = current_ans; n         best_it = next_iter; n       } n     } n     path.insert(best_it, i); n     ans = next_ans; n   } n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int ar[100001]; n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   long long t, n, m; n   cin >> t; n   while (t--) { n     cin >> n >> m; n     long long val = n / m; n     int a = m % 10, cnt = 0; n     ar[cnt++] = a; n     int b = 2; n     while ((a * b) % 10 != a) { n       ar[cnt++] = (a * b) % 10; n       b++; n     } n     long long sum = 0; n     for (int i = 0; i < cnt; i++) sum += ar[i]; n     long long ans = 0; n     ans += sum * (val / cnt); n     for (int i = 0; i < (val % cnt); i++) ans += ar[i]; n     cout << ans << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int a[500500], n, len, ans; n int main() { n   scanf( %d , &n); n   ans = 0; n   for (int i = 0; i < n; i++) scanf( %d , &a[i]); n   for (int i = 0; i < n; i++) { n     int k = i; n     len = 0; n     while (a[k + 1] != a[k] && k < n - 1) { n       len++; n       k++; n     } n     if (i != k) { n       if (a[i] == a[k]) n         for (i++; i < k; i++) a[i] = a[k]; n       else { n         int j; n         for (j = 1; j <= (k - i) / 2; j++) a[j + i] = a[i]; n         for (; j + i < k; j++) a[j + i] = a[k]; n         i = k; n       } n     } n     ans = (ans > len ? ans : len); n   } n   if (ans == n) ans--; n   printf( %d n , ans / 2); n   for (int i = 0; i < n; i++) { n     if (i == n - 1) n       cout << a[i] << endl; n     else n       cout << a[i] <<    ; n   } n   printf(  n ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m; n int r1, c1, r2, c2; n char s[505][505]; n bool dfs(int i, int j) { n   if (r2 == i && c2 == j && s[r2][c2] !=  . ) return 1; n   if (s[i][j] !=  . ) return 0; n   s[i][j] =  X ; n   return dfs(i + 1, j) || dfs(i - 1, j) || dfs(i, j + 1) || dfs(i, j - 1); n } n int main() { n   cin >> n >> m; n   for (int i = 1; i <= n; i++) n     for (int j = 1; j <= m; j++) cin >> s[i][j]; n   cin >> r1 >> c1 >> r2 >> c2; n   s[r1][c1] =  . ; n   if (dfs(r1, c1)) n     cout <<  YES  << endl; n   else n     cout <<  NO  << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   string s, x; n   cin >> s; n   int p = 0, q = 0, r = 0, t = 0; n   for (int i = 0; i < s.length() - 1; i++) n     if (s[i] != s[i + 1]) x[t++] = s[i + 1]; n   if (t != 2) n     cout <<  NO ; n   else { n     if (x[0] !=  b  || x[1] !=  c ) n       cout <<  NO ; n     else { n       for (int i = 0; i < s.length(); i++) { n         if (s[i] ==  a ) p++; n         if (s[i] ==  b ) q++; n         if (s[i] ==  c ) r++; n       } n       if (q == 0) n         cout <<  NO ; n       else { n         if (p == r || r == q) n           cout <<  YES ; n         else n           cout <<  NO ; n       } n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int mod = 1e9 + 7; n struct S { n   int l, r, h; n   S() {} n   S(int l, int r, int h) : l(l), r(r), h(h) {} n } Ss[100001]; n struct E { n   int l, r, h, f; n   S s; n   E() {} n   E(int l, int r, int h, S s, int f = 0) : l(l), r(r), h(h), s(s), f(f) {} n   bool operator<(const E& p) const { return l < p.l; } n   void print() const { printf( l=%d, r=%d, f=%d, h=%d n , l, r, f, h); } n } Es[100001]; n int n, T, inf = 1e9; n struct compar { n   bool operator()(const S& a, const S& b) const { return a.h < b.h; } n }; n struct Solution { n   int solve() { n     set<E> sweep; n     Ss[n] = S(-inf, inf, T); n     sweep.insert(E(-inf, inf, 0, S(-inf, inf, 0), 2 * inf)); n     sweep.insert(E(-inf - 1, -inf - 1, 0, S(-inf - 1, -inf - 1, 0))); n     sweep.insert(E(inf + 1, inf + 1, 0, S(inf + 1, inf + 1, 0))); n     sort(Ss, Ss + n, compar()); n     for (int(i) = (0); (i) <= (n); ++(i)) { n       auto& s = Ss[i]; n       auto e = E(s.l, s.r, s.h, s); n       std::set<E>::iterator start = sweep.upper_bound(e), end, it, lt, rt; n       start--, it = lt = rt = start, lt--, rt++; n       while (it->l < e.r) { n         if (!(it->h < lt->h && lt->h < e.h && lt->s.r > max(it->s.l, e.l) || n               it->h < rt->h && rt->h < e.h && rt->s.l < min(it->s.r, e.r))) { n           e.f = max(e.f, min(it->f, overlap(*it, e))); n         } else if (e.h == T) { n         } n         it++, rt++, lt++; n       } n       auto first = *start, last = *lt; n       sweep.erase(start, it); n       if (first.l < e.l) { n         sweep.insert(E(first.l, e.l, first.h, first.s, first.f)); n       } n       if (last.r > e.r) { n         sweep.insert(E(e.r, last.r, last.h, last.s, last.f)); n       } n       sweep.insert(e); n     } n     auto it = sweep.begin(); n     it++; n     return it->f; n   } n   int overlap(const E& a, E& b) { n     return max(0, min(a.r, b.r) - max(a.l, b.l)); n   } n }; n void print(vector<int>& nums) { n   for (auto num : nums) cout << num << endl; n } n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   cin >> n >> T; n   for (int(i) = (0); (i) < (n); ++(i)) cin >> Ss[i].h >> Ss[i].l >> Ss[i].r; n   Solution test; n   cout << test.solve() << endl; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e6 + 7, mod = 1e9 + 7, INF = 0x3f3f3f3f; n int a[N]; n int n; n bool vis[N]; n int num[N]; n int b[N]; n int f(int x) { return x == b[x] ? x : b[x] = f(b[x]); } n void u(int x, int y) { n   x = f(x), y = f(y); n   if (x != y) b[x] = y; n } n int pre[N]; n int main() { n   cin >> n; n   for (int i = 1; i < n; i++) { n     scanf( %d , &a[i]); n     if (pre[a[i]] == 0) pre[a[i]] = i; n     vis[a[i]] = 1; n   } n   priority_queue<int, vector<int>, greater<int> > q; n   for (int i = 1; i <= n; i++) { n     b[i] = i; n     if (!vis[i]) q.push(i); n     num[i] = i; n   } n   vector<pair<int, int> > ans; n   for (int i = n - 1; i >= 1; i--) { n     int x = q.top(); n     q.pop(); n     ans.push_back(pair<int, int>(num[x], a[i])); n     u(num[x], a[i]); n     x = max(x, a[i]); n     num[x] = a[i]; n     if (i == pre[a[i]]) q.push(x); n   } n   for (int i = 2; i <= n; i++) n     if (f(i) != f(1)) { n       cout << -1; n       return 0; n     } n   cout << a[1] <<   n ; n   for (int i = 0; i < ans.size(); i++) n     cout << ans[i].first <<     << ans[i].second <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n class Solution { n  public: n   void solve(std::istream& in, std::ostream& out) { n     string s; n     in >> s; n     int n = s.size(); n     for (size_t i = 0; i < n; i++) { n       int x = (s[i] -  0 ); n       if (x % 8 == 0) { n         out <<  YES  n             <<   n  n             << x; n         return; n       } n     } n     for (size_t i = 0; i < n; i++) { n       for (size_t j = i + 1; j < n; j++) { n         int x = 10 * (s[i] -  0 ) + (s[j] -  0 ); n         if (x % 8 == 0) { n           out <<  YES  n               <<   n  n               << x; n           return; n         } n       } n     } n     for (size_t i = 0; i < n; i++) { n       for (size_t j = i + 1; j < n; j++) { n         for (size_t k = j + 1; k < n; k++) { n           int x = 100 * (s[i] -  0 ) + 10 * (s[j] -  0 ) + (s[k] -  0 ); n           if (x % 8 == 0) { n             out <<  YES  n                 <<   n  n                 << x; n             return; n           } n         } n       } n     } n     out <<  NO ; n   } n }; n void solve(std::istream& in, std::ostream& out) { n   out << std::setprecision(12); n   Solution solution; n   solution.solve(in, out); n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   istream& in = cin; n   ostream& out = cout; n   solve(in, out); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long x, k, col; n vector<long long> d, g[10101]; n void go(long long pos, long long K) { n   if (col == 100000) return; n   if (col == 10000) cerr <<  Ok ; n   if (pos == 0 || K == 0) { n     cout << d[pos] <<    ; n     col++; n     return; n   } n   for (int j = 0; j < g[pos].size(); ++j) go(g[pos][j], K - 1); n } n int main() { n   cin >> x >> k; n   for (long long i = 1; i * i <= x; ++i) n     if (x % i == 0) { n       d.push_back(i); n       if (i != x / i) d.push_back(x / i); n     } n   sort(d.begin(), d.end()); n   for (int i = 0; i < d.size(); ++i) n     for (int j = 0; j <= i; ++j) n       if (d[i] % d[j] == 0) g[i].push_back(j); n   go(d.size() - 1, k); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n using ll = long long; n using vi = vector<int>; n using ii = pair<int, int>; n template <class A, class B> n ostream& operator<<(ostream& os, pair<A, B> p) { n   return os << p.first <<     << p.second; n } n template <class A, class B> n istream& operator>>(istream& is, pair<A, B>& p) { n   return is >> p.first >> p.second; n } n const int oo = 0x3f3f3f3f; n const ll OO = ll(oo) * oo; n const int N = 100005; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   int n; n   cin >> n; n   vi t(n); n   for (int i = 0; i < n; i++) cin >> t[i]; n   sort((t).begin(), (t).end()); n   int ans = 0; n   ll T = 0; n   for (int i = 0; i < n; i++) { n     if (T <= t[i]) { n       ans++; n       T += t[i]; n     } n   } n   cout << ans <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int Max_N = 2e3, Mod = 998244353; n int n, d[Max_N + 5], D[Max_N + 5], opt[Max_N + 5]; n char a[Max_N + 5][Max_N + 5]; n inline bool Chck() { n   sort(D + 1, D + n + 1); n   int sum = 0; n   for (int i = 1; i < n; i++) { n     sum += D[i]; n     if (sum == i * (i - 1) / 2) return false; n   } n   return true; n } n int main() { n   scanf( %d , &n); n   for (int i = 1; i <= n; i++) scanf( %s , a[i] + 1); n   for (int i = 1; i <= n; i++) n     for (int j = 1; j <= n; j++) n       if (a[i][j] ==  1 ) ++d[j]; n   if (n <= 6) { n     int ans = -1, cnt = 0; n     for (int s = 0; s < 1 << n; s++) { n       for (int i = 1; i <= n; i++) D[i] = d[i], opt[i] = 0; n       for (int i = 1; i <= n; i++) n         if ((1 << i - 1) & s) opt[i] = 1; n       for (int i = 1; i <= n; i++) n         for (int j = 1; j <= n; j++) n           if (a[i][j] ==  1  && (opt[i] ^ opt[j])) --D[j], ++D[i]; n       if (Chck()) { n         int sum = 0; n         for (int i = 1; i <= n; i++) n           if ((1 << i - 1) & s) ++sum; n         if (sum < ans || ans == -1) n           ans = sum, cnt = 1; n         else n           cnt += sum == ans; n       } n     } n     if (ans == -1) n       puts( -1 ); n     else { n       for (int i = 1; i <= ans; i++) cnt = 1ll * cnt * i % Mod; n       printf( %d %d n , ans, cnt); n     } n   } else { n     for (int i = 1; i <= n; i++) D[i] = d[i]; n     if (Chck()) { n       puts( 0 1 ); n       return 0; n     } n     int ans = 0; n     for (int i = 1; i <= n; i++) { n       for (int j = 1; j <= n; j++) D[j] = d[j]; n       for (int j = 1; j <= n; j++) n         if (a[i][j] ==  1 ) n           --D[j], ++D[i]; n         else if (i != j) n           ++D[j], --D[i]; n       if (Chck()) ++ans; n     } n     printf( 1 %d n , ans); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, a[200003]; n vector<int> v; n int main() { n   cin >> n; n   for (int i = 2; i <= n; i++) cin >> a[i]; n   v.push_back(n); n   int j = n; n   while (j != 1) { n     v.push_back(a[j]); n     j = a[j]; n   } n   reverse(v.begin(), v.end()); n   for (int i = 0; i < v.size(); i++) cout << v[i] <<    ; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 2e3 + 10; n int tim[maxn]; n int ans[30][30]; n set<int> q; n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   int n; n   cin >> n; n   for (int i = 0; i < n * n; i++) { n     int t; n     cin >> t; n     q.insert(t); n     tim[t]++; n   } n   if (n % 2 == 0) { n     for (auto idx : q) n       if (tim[idx] % 4) { n         cout <<  NO  << endl; n         return 0; n       } n     vector<int> p; n     for (auto idx : q) { n       for (int i = 0; i < tim[idx] / 4; i++) p.push_back(idx); n     } n     int idx = 0; n     for (int i = 0; i < n / 2; i++) n       for (int j = 0; j < n / 2; j++) n         ans[i][j] = ans[i][n - 1 - j] = ans[n - 1 - i][j] = n             ans[n - 1 - i][n - 1 - j] = p[idx++]; n     cout <<  YES  << endl; n     for (int i = 0; i < n; i++, cout << endl) n       for (int j = 0; j < n; j++) cout << ans[i][j] <<    ; n   } else { n     int odd = 0; n     vector<int> two; n     for (auto idx : q) n       if (tim[idx] % 2) odd++; n     if (odd > 1) { n       cout <<  NO  << endl; n       return 0; n     } n     for (auto idx : q) { n       if (tim[idx] % 2) { n         ans[n / 2][n / 2] = idx; n         tim[idx]--; n       } n       if (tim[idx] % 4) { n         two.push_back(idx); n         tim[idx] -= 2; n       } n     } n     if (two.size() > n - 1 || (n - 1 - two.size()) % 2) { n       cout <<  NO  << endl; n       return 0; n     } n     if (two.size() < n - 1) n       for (auto idx : q) { n         if (tim[idx] > 0 && tim[idx] % 4 == 0) { n           int qt = tim[idx]; n           for (int i = 0; i < qt / 2 && two.size() < n - 1; i++) { n             two.push_back(idx); n             tim[idx] -= 2; n           } n           if (two.size() == n - 1) break; n         } n       } n     vector<int> p; n     for (auto idx : q) { n       if (tim[idx] % 4 == 0 && tim[idx] > 0) n         for (int i = 0; i < tim[idx] / 4; i++) p.push_back(idx); n     } n     int idx = 0; n     for (int i = 0; i < n / 2; i++) n       for (int j = 0; j < n / 2; j++) n         ans[i][j] = ans[i][n - 1 - j] = ans[n - 1 - i][j] = n             ans[n - 1 - i][n - 1 - j] = p[idx++]; n     idx = 0; n     for (int i = 0; i < n / 2; i++) { n       ans[n / 2][i] = ans[n / 2][n - 1 - i] = two[idx++]; n       ans[i][n / 2] = ans[n - 1 - i][n / 2] = two[idx++]; n     } n     cout <<  YES  << endl; n     for (int i = 0; i < n; i++, cout << endl) n       for (int j = 0; j < n; j++) cout << ans[i][j] <<    ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 20; n const double eps = 1e-6; n double p[maxn], ans[maxn], dp[1 << maxn], S[1 << maxn]; n int Size[1 << maxn]; n bool eq(const double &a, const double &b) { return abs(a - b) <= eps; } n int main() { n   int n, k, zerCnt = 0; n   bool is0; n   scanf( %d %d , &n, &k); n   for (int i = (0); i < (n); ++i) { n     scanf( %lf , &p[i]); n     dp[i] = ans[i] = 0; n     Size[i] = 0; n     if (eq(p[i], 0)) ++zerCnt; n   } n   k = min(k, n - zerCnt); n   dp[0] = 1; n   for (int i = (1); i < (1 << n); ++i) { n     for (int j = (0); j < (n); ++j) n       if (i & (1 << j)) { n         Size[i] = Size[i - (1 << j)] + 1; n         S[i] = S[i - (1 << j)] + p[j]; n       } n   } n   for (int i = (1); i < (1 << n); ++i) { n     if (S[i] > 0) { n       for (int j = (0); j < (n); ++j) n         if ((i & (1 << j)) && (S[(1 << n) - 1 - i + (1 << j)] > 0)) n           dp[i] += dp[i - (1 << j)] * p[j] / S[(1 << n) - 1 - i + (1 << j)]; n       if (Size[i] == k) n         for (int j = (0); j < (n); ++j) n           if (i & (1 << j)) ans[j] += dp[i]; n     } else n       dp[i] = 0; n   } n   for (int i = (0); i < (n); ++i) printf( %.8f  , ans[i]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<long long int> as; n set<long long int> s; n int main() { n   int n; n   cin >> n; n   for (int i = 0; i < n; i++) { n     long long int a; n     cin >> a; n     a -= i; n     as.push_back(a); n     s.insert(a); n   } n   vector<long long int> unique; n   for (long long int ss : s) { n     unique.push_back(ss); n   } n   long long int dp[2][3010]; n   long long int low[2][3010]; n   for (int i = 0; i < 1; i++) { n     for (int j = 0; j < n; j++) { n       dp[i][j] = 1e15; n     } n   } n   int start = 0; n   for (int i = 0; i < unique.size(); i++) { n     dp[0][i] = abs(unique[i] - as[0]); n   } n   low[0][0] = dp[0][0]; n   for (int j = 1; j < unique.size(); j++) { n     low[0][j] = min(low[0][j - 1], dp[0][j]); n   } n   if (n == 1) { n     cout << 0 << endl; n     return 0; n   } n   for (int i = 1; i < n; i++) { n     for (int j = 0; j < unique.size(); j++) { n       dp[i % 2][j] = low[(i - 1) % 2][j] + abs(unique[j] - as[i]); n     } n     low[i % 2][0] = dp[i % 2][0]; n     for (int j = 1; j < unique.size(); j++) { n       low[i % 2][j] = min(low[i % 2][j - 1], dp[i % 2][j]); n     } n   } n   long long int ans = 1e15; n   for (int i = 0; i < unique.size(); i++) { n     ans = min(dp[(n - 1) % 2][i], ans); n   } n   cout << ans << endl; n } n


#include <bits/stdc++.h> n using namespace std; n long long a[10][10]; n int d[10], b[10]; n long long kq; n void dequy(int k) { n   int i; n   long long sum; n   if (k == 6) { n     sum = a[d[1]][d[2]] + a[d[2]][d[1]] + a[d[3]][d[4]] + a[d[4]][d[3]]; n     sum += a[d[2]][d[3]] + a[d[3]][d[2]] + a[d[4]][d[5]] + a[d[5]][d[4]]; n     sum += a[d[3]][d[4]] + a[d[4]][d[3]]; n     sum += a[d[4]][d[5]] + a[d[5]][d[4]]; n     kq = max(kq, sum); n     return; n   } n   for (i = 1; i <= 5; i++) n     if (b[i] == 0) { n       b[i] = 1; n       d[k] = i; n       dequy(k + 1); n       b[i] = 0; n     } n } n void solve() { n   int i, j; n   memset(b, 0, sizeof(b)); n   dequy(1); n   cout << kq << endl; n } n void input() { n   int i, j; n   for (i = 1; i <= 5; i++) n     for (j = 1; j <= 5; j++) cin >> a[i][j]; n   solve(); n } n int main() { n   input(); n   fclose(stdin); n   fclose(stdout); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int t; n   cin >> t; n   while (t--) { n     int n, cnt = 0; n     cin >> n; n     vector<int> a(n); n     for (int i = 0; i < n; i++) { n       cin >> a[i]; n     } n     sort(a.rbegin(), a.rend()); n     for (int i = 0; i < n; i++) { n       if (cnt < a[i]) { n         cnt++; n       } else { n         break; n       } n     } n     cout << cnt << endl; n   } n   return 0; n } n