#include <bits/stdc++.h> n using namespace std; n template <typename T> n T mabs(const T &a) { n   return a < 0 ? -a : a; n } n const int mod = 1e9 + 9; n const int SZ = 300300; n struct mat { n   long long A[2][2]; n   mat(int a = 0, int b = 0, int c = 0, int d = 0) { n     A[0][0] = a; n     A[0][1] = b; n     A[1][0] = c; n     A[1][1] = d; n   } n   mat operator*(const mat &other) const { n     mat res; n     for (int i = (0), ei = (2); i < ei; i++) n       for (int j = (0), ej = (2); j < ej; j++) { n         for (int k = (0), ek = (2); k < ek; k++) n           res.A[i][j] += A[i][k] * other.A[k][j]; n         res.A[i][j] %= mod; n       } n     return res; n   } n   mat operator+(const mat &other) const { n     mat res; n     for (int i = (0), ei = (2); i < ei; i++) n       for (int j = (0), ej = (2); j < ej; j++) { n         res.A[i][j] = A[i][j] + other.A[i][j]; n         res.A[i][j] %= mod; n       } n     return res; n   } n   mat operator-(const mat &other) const { n     mat res; n     for (int i = (0), ei = (2); i < ei; i++) n       for (int j = (0), ej = (2); j < ej; j++) { n         res.A[i][j] = A[i][j] - other.A[i][j]; n         res.A[i][j] %= mod; n       } n     return res; n   } n }; n mat Fib(1, 1, 1, 0); n mat EminusArev(-1, -1, -1, 0); n mat Step[SZ]; n long long Sum[4 * SZ]; n mat M[4 * SZ]; n int A[SZ]; n void build(int v, int l, int r) { n   if (l == r) { n     Sum[v] = A[l]; n     return; n   } n   int c = (l + r) / 2; n   build(2 * v, l, c); n   build(2 * v + 1, c + 1, r); n   Sum[v] = (Sum[2 * v] + Sum[2 * v + 1]) % mod; n } n inline mat sumUp(int a, int b) { n   mat res = EminusArev * (Step[0] - Step[b - a + 1]); n   return Step[a] * res; n } n void add(int v, int l, int r, int ql, int qr, int st) { n   Sum[v] = (Sum[v] + sumUp(ql - st, qr - st).A[0][0]) % mod; n   if (l == ql && r == qr) { n     M[v] = M[v] + Step[ql - st]; n     return; n   } n   int c = (l + r) / 2; n   if (qr <= c) n     add(2 * v, l, c, ql, qr, st); n   else if (ql > c) n     add(2 * v + 1, c + 1, r, ql, qr, st); n   else { n     add(2 * v, l, c, ql, c, st); n     add(2 * v + 1, c + 1, r, c + 1, qr, st); n   } n } n long long getSum(int v, int l, int r, int ql, int qr) { n   if (l == ql && r == qr) { n     return Sum[v]; n   } n   int c = (l + r) / 2; n   long long res = 0; n   if (qr <= c) n     res = getSum(2 * v, l, c, ql, qr); n   else if (ql > c) n     res = getSum(2 * v + 1, c + 1, r, ql, qr); n   else n     res = getSum(2 * v, l, c, ql, c) + getSum(2 * v + 1, c + 1, r, c + 1, qr); n   return (res + (M[v] * sumUp(ql - l, qr - l)).A[0][0]) % mod; n } n void run() { n   Step[0] = mat(1, 0, 0, 1); n   for (int i = (1), ei = (SZ); i < ei; i++) Step[i] = Step[i - 1] * Fib; n   int n, m; n   cin >> n >> m; n   for (int i = (0), ei = (n); i < ei; i++) scanf( %d , A + i); n   build(1, 0, n - 1); n   for (int i = (0), ei = (m); i < ei; i++) { n     int t, a, b; n     scanf( %d%d%d , &t, &a, &b); n     --a; n     --b; n     if (t == 1) { n       add(1, 0, n - 1, a, b, a); n     } else { n       int res = getSum(1, 0, n - 1, a, b); n       res = (res + mod) % mod; n       printf( %d n , res); n     } n   } n } n int main() { n   run(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m; n char mat[1501][1501]; n int visited[1501][1501][3]; n int getr(int x) { return (x % n + n) % n; } n int getc(int x) { return (x % m + m) % m; } n int dx[] = {0, -1, 0, 1}; n int dy[] = {-1, 0, 1, 0}; n bool dfs(int lx, int ly) { n   int x = getr(lx), y = getc(ly); n   if (visited[x][y][0] && (lx != visited[x][y][1] || ly != visited[x][y][2])) n     return 1; n   if (mat[x][y] ==  #  || visited[x][y][0]) return 0; n   visited[x][y][0] = 1; n   visited[x][y][1] = lx; n   visited[x][y][2] = ly; n   for (int i = 0; i < 4; ++i) { n     if (dfs(lx + dx[i], ly + dy[i])) return 1; n   } n   return 0; n } n int main() { n   ios::sync_with_stdio(0); n   while (cin >> n >> m) { n     int sx, sy; n     memset(visited, 0, sizeof(visited)); n     for (int i = 0; i < n; ++i) { n       for (int j = 0; j < m; ++j) { n         cin >> mat[i][j]; n         if (mat[i][j] ==  S ) sx = i, sy = j; n       } n     } n     if (dfs(sx, sy)) n       cout <<  Yes n ; n     else n       cout <<  No n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long M = 1e9 + 7; n vector<long long> prime; n bool num[1000005 + 2]; n void sieve() { n   num[0] = true; n   num[1] = true; n   for (long long i = 2; i <= 1000005; i++) { n     if (num[i] == false) { n       prime.push_back(i); n       for (long long j = 2 * i; j <= 1000005; j = j + i) { n         num[j] = true; n       } n     } n   } n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   long long n, m, i, j, k, l, a, b, c, p, q, r, t, ans, sum; n   cin >> t; n   vector<pair<long long, long long> > v; n   while (t--) { n     cin >> n; n     m = n; n     j = 0; n     while (n--) { n       cin >> k >> a; n       b = a + 1; n       c = b + 1; n       for (i = 1; i < k; i++) { n         cin >> a; n         if (a >= c) { n           b = b + (a - c) + 1; n           c = a + 2; n         } else n           c++; n       } n       v.push_back(make_pair(b, k)); n     } n     sort(v.begin(), v.end()); n     b = v[0].first + v[0].second; n     a = v[0].first; n     for (i = 1; i < m; i++) { n       if (v[i].first > b) { n         c = v[i].first - b; n         b = v[i].first + v[i].second; n         a = a + c; n       } else { n         b = b + v[i].second; n       } n     } n     cout << a << endl; n     v.clear(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const double PI = acos(-1.0); n const double eps = 1e-6; n const int inf = 1e9; n const int mod = 1e9 + 7; n const int maxn = 5e5 + 10; n int n; n int a[1010], b[1010]; n int main() { n   ios::sync_with_stdio(false); n   cin >> n; n   for (int i = 1; i <= n; i++) { n     cin >> a[i]; n   } n   for (int i = 1; i <= n; i++) { n     cin >> b[i]; n   } n   int ans = 0; n   for (int i = 1; i <= n; i++) { n     int r1 = 0, r2 = 0; n     for (int j = i; j <= n; j++) { n       r1 |= a[j]; n       r2 |= b[j]; n       ans = max(r1 + r2, ans); n     } n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, a, b, c; n   cin >> n >> a >> b >> c; n   long long res = 0; n   for (int i = 0; i <= a; ++i) { n     for (int j = 0; j <= b; ++j) { n       if (2 * n - i - 2 * j >= 0 && !((2 * n - i - 2 * j) % 4) && n           2 * n - i - 2 * j <= 4 * c) { n         ++res; n       } n     } n   } n   cout << res << endl; n   return 0; n } n


#include <bits/stdc++.h> n const int MAXN = (int)1e5 + 5; n const int MODN = (int)1e9 + 7; n int inf = 0x3f3f3f3f; n using namespace std; n char str[MAXN]; n int num[MAXN]; n int a[MAXN]; n struct Com { n   int cost; n   int qua; n } c[MAXN]; n int cmp(Com a, Com b) { return a.cost < b.cost; } n int main() { n   int n, m; n   while (scanf( %d , &n) != EOF) { n     for (int i = 0; i < n; i++) { n       scanf( %d%d , &c[i].cost, &c[i].qua); n     } n     sort(c, c + n, cmp); n     int flag = 0; n     for (int i = 1; i < n; i++) { n       if (c[i].qua < c[i - 1].qua && c[i].cost > c[i - 1].cost) { n         flag = 1; n       } n     } n     if (flag) n       puts( Happy Alex ); n     else n       puts( Poor Alex ); n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 3e5 + 5; n const int maxm = 15; n const long long INF = 1e18; n int n, m, k; n int a[maxn]; n long long dp[maxn][maxm]; n void ReadInput() { n   cin >> n >> m >> k; n   for (int i = 1; i <= n; ++i) cin >> a[i]; n } n void Solve() { n   fill_n(&dp[0][0], sizeof(dp) / sizeof(dp[0][0]), -INF); n   for (int i = 0; i <= n; ++i) dp[i][10] = 0; n   for (int i = 1; i <= n; ++i) n     for (int j = 1; j <= m; ++j) { n       if (j == 1) n         dp[i][j] = max((long long)a[i] - k, dp[i - 1][m] + a[i] - k); n       else n         dp[i][j] = dp[i - 1][j - 1] + a[i]; n     } n   long long ans = 0; n   for (int i = 1; i <= n; ++i) n     for (int j = 1; j <= m; ++j) ans = max(ans, dp[i][j]); n   cout << ans; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(nullptr); n   ReadInput(); n   Solve(); n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 5e5 + 10; n char s[maxn], t[maxn]; n int nxt[maxn]; n void getNxt(int m) { n   int i = 1, j = 0; n   nxt[0] = 0; n   while (i < m) { n     if (t[i] == t[j]) n       nxt[i++] = ++j; n     else if (!j) n       i++; n     else n       j = nxt[j - 1]; n   } n } n int main() { n   scanf( %s%s , s, t); n   int len1 = strlen(s), len2 = strlen(t); n   int len1_0 = 0, len1_1 = 0, len2_0 = 0, len2_1 = 0; n   for (int i = 0; i < len1; i++) { n     int k = s[i] -  0 ; n     if (k == 0) n       len1_0++; n     else n       len1_1++; n   } n   for (int i = 0; i < len2; i++) { n     int k = t[i] -  0 ; n     if (k == 0) n       len2_0++; n     else n       len2_1++; n   } n   getNxt(len2); n   int k = nxt[len2 - 1]; n   if (len1 < len2) { n     for (int i = 0; i < len1; i++) cout << s[i]; n   } else if (len1 == len2) { n     if (len1_0 != len2_0 || len1_1 != len2_1) { n       for (int i = 0; i < len1; i++) cout << s[i]; n     } else { n       for (int i = 0; i < len1; i++) cout << t[i]; n     } n   } else { n     if (len2_0 == 0) { n       for (int i = 1; i <= len1_1; i++) { n         cout <<  1 ; n       } n       for (int i = 1; i <= len1_0; i++) { n         cout <<  0 ; n       } n       return 0; n     } else if (len2_1 == 0) { n       for (int i = 1; i <= len1_1; i++) { n         cout <<  1 ; n       } n       for (int i = 1; i <= len1_0; i++) { n         cout <<  0 ; n       } n       return 0; n     } n     if (len1_0 < len2_0 || len1_1 < len2_1) { n       for (int i = 0; i < len1; i++) cout << s[i]; n     } else { n       if (k == 0) { n         int p = min(len1_0 / len2_0, len1_1 / len2_1); n         for (int j = 1; j <= p; j++) { n           for (int i = 0; i < len2; i++) { n             cout << t[i]; n           } n         } n         len1_0 = len1_0 - len2_0 * p; n         len1_1 = len1_1 - len2_1 * p; n         for (int i = 1; i <= len1_0; i++) { n           cout <<  0 ; n         } n         for (int i = 1; i <= len1_1; i++) { n           cout <<  1 ; n         } n       } else { n         int f0 = 0, f1 = 0; n         for (int i = k; i < len2; i++) { n           int p = t[i] -  0 ; n           if (p == 0) n             f0++; n           else n             f1++; n         } n         for (int i = 0; i < len2; i++) { n           cout << t[i]; n         } n         len1_0 -= len2_0; n         len1_1 -= len2_1; n         int p = min(len1_0 / f0, len1_1 / f1); n         len1_0 = len1_0 - f0 * p; n         len1_1 = len1_1 - f1 * p; n         for (int j = 1; j <= p; j++) { n           for (int i = k; i < len2; i++) { n             cout << t[i]; n           } n         } n         for (int i = 1; i <= len1_0; i++) { n           cout <<  0 ; n         } n         for (int i = 1; i <= len1_1; i++) { n           cout <<  1 ; n         } n       } n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n #pragma GCC optimize(2) n #pragma GCC optimize(3) n inline int read() { n   int x = 0, f = 0; n   char ch = getchar(); n   while (!isdigit(ch)) f |= ch ==  - , ch = getchar(); n   while (isdigit(ch)) x = 10 * x + ch -  0 , ch = getchar(); n   return f ? -x : x; n } n inline long long llread() { n   long long x = 0, f = 0; n   char ch = getchar(); n   while (!isdigit(ch)) f |= ch ==  - , ch = getchar(); n   while (isdigit(ch)) x = 10 * x + ch -  0 , ch = getchar(); n   return f ? -x : x; n } n long long fact[110], c[110][110]; n long long f[110][110][110]; n long long n, m, k, P; n signed main() { n   n = llread(), m = llread(), k = llread(), P = llread(); n   c[0][0] = fact[0] = 1; n   for (long long i = (1); i <= (n); i++) { n     c[i][0] = c[i][i] = 1; n     for (long long j = (1); j <= (i - 1); j++) n       c[i][j] = (c[i - 1][j - 1] + c[i - 1][j]) % P; n   } n   for (long long i = (1); i <= (n); i++) fact[i] = (fact[i - 1] * i) % P; n   for (long long i = (0); i <= (n); i++) f[0][i][0] = 1; n   for (long long i = (1); i <= (n); i++) { n     f[i][1][1] = fact[i]; n     for (long long j = (i + 1); j <= (n); j++) f[i][j][0] = fact[i]; n     for (long long j = (2); j <= (min(i, m)); j++) { n       for (long long p = (0); p <= (min(i - j + 1, k)); p++) { n         f[i][j][p] = (f[i][j][p] + (f[i - 1][j - 1][p - (j == 1)] * 2)) % P; n         for (long long q = (2); q <= (i - 1); q++) { n           for (long long w = (0); w <= (p - (j == 1)); w++) { n             if (f[q - 1][j - 1][w] && f[i - q][j - 1][p - (j == 1) - w]) { n               f[i][j][p] = (f[i][j][p] + f[q - 1][j - 1][w] * n                                              f[i - q][j - 1][p - (j == 1) - w] % n                                              P * c[i - 1][q - 1]) % n                            P; n             } n           } n         } n       } n     } n   } n   printf( %lld n , f[n][m][k]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   string s; n   cin >> s; n   char now =    ; n   int count = 0, sum = 0, n; n   n = s.size(); n   for (int i = 0; i < n; i++) { n     if (now == s[i] and count < 5) n       count++; n     else n       sum++, now = s[i], count = 1; n   } n   cout << sum <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 100005; n int t[N]; n int main() { n   ios_base::sync_with_stdio(0); n   int n; n   cin >> n; n   t[0] = 0; n   t[n + 1] = 0; n   for (int i = 1; i <= n; ++i) { n     cin >> t[i]; n     t[i] = min(t[i - 1] + 1, t[i]); n   } n   for (int i = n; i > 0; --i) { n     t[i] = min(t[i + 1] + 1, t[i]); n   } n   int res = 0; n   for (int i = 1; i <= n; ++i) res = max(res, t[i]); n   cout << res <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n long long gcd(long long a, long long b) { return b == 0 ? a : gcd(b, a % b); } n const int MAXN = 100000; n const int MAXQ = 100000; n int n, nq; n int x[MAXN]; n int qx[MAXQ], qt[MAXQ]; n long long qans[MAXQ]; n pair<int, int> o[MAXQ]; n priority_queue<pair<int, pair<int, int>>> pq; n struct TreapNode { n   int prio, sz, l, r, par; n }; n struct Treap { n   int n, root; n   vector<TreapNode> nodes; n   void init(int _n) { n     n = _n, root = -1; n     nodes = vector<TreapNode>(n); n     for (int i = (0); i < (n); ++i) n       nodes[i].prio = n           rand() % 1000 * 1000000 + rand() % 1000 * 1000 + rand() % 1000, n       nodes[i].sz = 1, nodes[i].l = nodes[i].r = nodes[i].par = -1; n   } n   int get(int offset) { n     int at = root; n     while (true) { n       assert(at != -1 && offset < nodes[at].sz); n       int l = nodes[at].l, r = nodes[at].r; n       if (l != -1) { n         if (offset < nodes[l].sz) { n           at = l; n           continue; n         } n         offset -= nodes[l].sz; n       } n       if (offset == 0) n         return at; n       else n         --offset; n       if (r != -1) { n         if (offset < nodes[r].sz) { n           at = r; n           continue; n         } n         offset -= nodes[r].sz; n       } n       assert(false); n     } n   } n   int find(int at) { n     int ret = 0; n     if (nodes[at].l != -1) ret += nodes[nodes[at].l].sz; n     while (nodes[at].par != -1) { n       int to = nodes[at].par; n       if (at == nodes[to].r) ret += 1 + getsz(nodes[to].l); n       at = to; n     } n     return ret; n   } n   int getsz(int a) { return a == -1 ? 0 : nodes[a].sz; } n   void setpar(int a, int b) { n     if (a != -1) nodes[a].par = b; n   } n   void split(int res, int &l, int &r, int sz) { n     if (res == -1) { n       assert(sz == 0); n       l = r = -1; n     } else if (sz <= getsz(nodes[res].l)) n       split(nodes[res].l, l, nodes[res].l, sz), setpar(nodes[res].l, res), n           r = res; n     else n       split(nodes[res].r, nodes[res].r, r, sz - 1 - getsz(nodes[res].l)), n           setpar(nodes[res].r, res), l = res; n     if (l != -1) nodes[l].par = -1; n     if (r != -1) nodes[r].par = -1; n     if (res != -1) n       nodes[res].sz = 1 + getsz(nodes[res].l) + getsz(nodes[res].r); n   } n   void merge(int &res, int l, int r) { n     if (l == -1) n       res = r; n     else if (r == -1) n       res = l; n     else if (nodes[l].prio > nodes[r].prio) n       merge(nodes[l].r, nodes[l].r, r), setpar(nodes[l].r, l), res = l; n     else n       merge(nodes[r].l, l, nodes[r].l), setpar(nodes[r].l, r), res = r; n     if (res != -1) n       nodes[res].sz = 1 + getsz(nodes[res].l) + getsz(nodes[res].r); n   } n }; n Treap treap; n void initpockets(int sz) { n   treap.init(sz); n   treap.root = 0; n } n int getpocket(int offset) { return treap.get(offset); } n int getoffset(int pocket) { return treap.find(pocket); } n void insertpocket(int offset, int pocket) { n   int l, r; n   treap.split(treap.root, l, r, offset); n   treap.merge(l, l, pocket); n   treap.merge(treap.root, l, r); n } n void solve() { n   for (int i = (0); i < (nq); ++i) o[i] = make_pair(qx[i], i); n   sort(o, o + nq); n   int at = 0; n   while (at < nq && o[at].first < x[0]) { n     int id = o[at++].second; n     qans[id] = qx[id]; n   } n   int cround = 0, coffset = 0, cseen = 1; n   initpockets(n); n   pq = priority_queue<pair<int, pair<int, int>>>(); n   for (int i = (0); i < (n); ++i) { n     int cx = x[i], nx = i == n - 1 ? INT_MAX : x[i + 1]; n     while (at < nq && (nx == INT_MAX || o[at].first < nx)) { n       int id = o[at++].second; n       int qoffset = (coffset + qx[id] - cx) % cseen; n       int qround = n           cround + (qx[id] - cx) / cseen + (qoffset < coffset ? 1 : 0) + qt[id]; n       int qpocket = getpocket(qoffset); n       pq.push(make_pair(-qround, make_pair(id, qpocket))); n     } n     int dx = nx == INT_MAX ? INT_MAX : nx - cx; n     if (dx != INT_MAX && coffset + dx < cseen) { n       cx += dx, coffset += dx; n     } else { n       int rem = cseen - coffset; n       cx += rem, coffset = 0, ++cround; n       if (dx != INT_MAX) dx -= rem; n       int full = dx == INT_MAX ? INT_MAX : dx / cseen; n       while (!pq.empty() && n              (full == INT_MAX || -pq.top().first < cround + full)) { n         int qround = -pq.top().first, id = pq.top().second.first, n             qpocket = pq.top().second.second; n         pq.pop(); n         int qoffset = getoffset(qpocket); n         qans[id] = cx + (long long)(qround - cround) * cseen + qoffset; n       } n       if (full == INT_MAX) break; n       cround += full, cx += full * cseen, dx -= full * cseen, coffset += dx, n           cx += dx, dx = 0; n     } n     insertpocket(coffset, i + 1), ++cseen; n   } n } n void run() { n   scanf( %d%d , &n, &nq); n   for (int i = (0); i < (n); ++i) scanf( %d , &x[i]); n   for (int i = (0); i < (nq); ++i) scanf( %d%d , &qx[i], &qt[i]); n   solve(); n   for (int i = (0); i < (nq); ++i) printf( %lld n , qans[i]); n } n long long bfans[MAXQ]; n void bf() { n   int sz = 0; n   for (int i = (0); i < (n); ++i) sz = max(sz, x[i] + 1); n   for (int i = (0); i < (nq); ++i) sz = max(sz, qx[i] + 1); n   vector<int> cur(sz); n   for (int i = (0); i < (sz); ++i) cur[i] = i; n   int nxt = sz; n   int t = 0; n   int rem = nq; n   while (rem > 0) { n     for (int i = (0); i < (nq); ++i) n       if (qt[i] == t) { n         bfans[i] = cur[qx[i]]; n         --rem; n       } n     int at = 0, idx = 0; n     for (int i = (0); i < (sz); ++i) { n       if (at < n && x[at] == i) { n         ++at; n         continue; n       } n       cur[idx++] = cur[i]; n     } n     while (idx < sz) cur[idx++] = nxt++; n     ++t; n   } n } n void stress() { n   for (int rep = (0); rep < (10000); ++rep) { n     n = 100; n     nq = 50; n     int lim = 5 * n; n     for (int i = (0); i < (n); ++i) x[i] = rand() % (lim - n); n     sort(x, x + n); n     for (int i = (0); i < (n); ++i) x[i] += i; n     for (int i = (0); i < (nq); ++i) { n       qx[i] = rand() % lim; n       qt[i] = rand() % 5; n     } n     solve(); n     bf(); n     bool ok = true; n     for (int i = (0); i < (nq); ++i) n       if (bfans[i] != qans[i]) ok = false; n     if (ok) { n       printf( . ); n       continue; n     } n     printf( err n ); n     printf( have: ); n     for (int i = (0); i < (nq); ++i) printf(  %lld , qans[i]); n     puts(  ); n     printf( want: ); n     for (int i = (0); i < (nq); ++i) printf(  %lld , bfans[i]); n     puts(  ); n     printf( %d %d n , n, nq); n     for (int i = (0); i < (n); ++i) { n       if (i != 0) printf(   ); n       printf( %d , x[i]); n     } n     puts(  ); n     for (int i = (0); i < (nq); ++i) printf( %d %d n , qx[i], qt[i]); n     break; n   } n } n int main() { n   run(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 5050; n int c, d; n char ch[N]; n int Calc(int b, int e) { n   c = 0, d = 0; n   int ret = 0, i; n   for (i = b; i <= e; i++) { n     if (ch[i] ==  ( ) c++; n     if (ch[i] ==  ) ) { n       if (c) n         c--; n       else { n         if (d > 0) n           d--, c++; n         else n           return ret; n       } n     } n     if (ch[i] ==  ? ) { n       if (c) n         c--, d++; n       else n         c++; n     } n     if (c == 0) { n       ret++; n     } n   } n   return ret; n } n int main() { n   int n, i, sol = 0; n   scanf( %s , ch + 1); n   n = strlen(ch + 1); n   for (i = 1; i <= n; i++) sol += Calc(i, n); n   printf( %i n , sol); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   int tt = 1; n   cin >> tt; n   while (tt--) { n     int n; n     cin >> n; n     int A[n]; n     for (int i = 0; i < n; i++) { n       cin >> A[i]; n     } n     int cnt = 0; n     sort(A, A + n); n     int left, right; n     if (n % 2 == 0) { n       left = n / 2 - 1; n       right = n / 2; n     } else { n       left = n / 2; n       right = n / 2 + 1; n     } n     while (cnt < n) { n       cout << A[left] <<    ; n       left--; n       cnt++; n       if (cnt == n) { n         break; n       } n       cout << A[right] <<    ; n       right++; n       cnt++; n     } n     cout <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int INT_MAX_VAL = (int)0x3F3F3F3F; n int INT_MIN_VAL = (int)-0x3F3F3F3F; n long long LONG_MAX_VAL = (long long)0x3F3F3F3F3F3F3F3F; n long long LONG_MIN_VAL = (long long)-0x3F3F3F3F3F3F3F3F; n long long vx, vy, wx, wy; n long long R; n long double t; n bool solve(long double s, long double dx, long double dy) { n   if (s <= t) { n     dx -= vx * s; n     dy -= vy * s; n   } else { n     dx -= wx * (s - t) + vx * t; n     dy -= wy * (s - t) + vy * t; n   } n   return (dx * dx + dy * dy) <= (R * R * s * s); n } n int main() { n   cin.tie(0); n   ios::sync_with_stdio(false); n   long long x1, y1, x2, y2; n   cin >> x1 >> y1 >> x2 >> y2; n   cin >> R >> t; n   cin >> vx >> vy >> wx >> wy; n   long double l = 0; n   long double r = 1.0e9; n   while ((abs(r - l) / max(1.l, l)) > 1e-9) { n     double c = (l + r) / 2; n     if (solve(c, x2 - x1, y2 - y1)) { n       r = c; n     } else { n       l = c; n     } n   } n   cout << setprecision(15) << fixed << l <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, c, p[55], t[55], x = 0, L = 0, R = 0; n   cin >> n >> c; n   for (int i = 0; i < n; i++) { n     cin >> p[i]; n   } n   for (int i = 0; i < n; i++) { n     cin >> t[i]; n   } n   for (int i = 0; i < n; i++) { n     x += t[i]; n     L += max(0, p[i] - c * x); n   } n   x = 0; n   for (int i = n - 1; i > -1; i--) { n     x += t[i]; n     R += max(0, p[i] - c * x); n   } n   if (L > R) { n     cout <<  Limak  << endl; n   } else if (L < R) { n     cout <<  Radewoosh  << endl; n   } else n     cout <<  Tie  << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long a[200005], b[200005]; n bool cmp(int a, int b) { return a > b; } n int main() { n   long long n; n   cin >> n; n   for (int i = 0; i < n; ++i) scanf( %I64d , &a[i]); n   for (int i = 0; i < n; ++i) scanf( %I64d , &b[i]); n   for (long long i = 1; i <= n; ++i) a[i - 1] *= i * (n - i + 1); n   sort(a, a + n); n   sort(b, b + n, cmp); n   long long ans = 0; n   for (int i = 0; i < n; ++i) { n     ans = (ans + (a[i] % 998244353) * b[i] % 998244353) % 998244353; n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, t; n   cin >> n >> t; n   int x = 0; n   while (n--) { n     int z; n     cin >> z; n     t -= (86400 - z); n     ++x; n     if (t <= 0) { n       cout << x << endl; n       return 0; n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n vector<int> vct; n void DFS(int d, int num) { n   if (d == 9) { n     return; n   } else { n     int a = num * 10 + 4; n     vct.push_back(a); n     DFS(d + 1, a); n     int b = num * 10 + 7; n     vct.push_back(b); n     DFS(d + 1, b); n   } n } n long long calc(int a, int b, int l, int r) { n   int x = max(a, l); n   int y = min(b, r); n   if (x > y) { n     return 0; n   } else { n     return y - x + 1; n   } n } n int main() { n   vct.clear(); n   DFS(0, 0); n   vct.push_back(-1); n   vct.push_back(1000000009); n   sort(vct.begin(), vct.end()); n   int ss = vct.size(); n   int p1, p2, v1, v2; n   int k; n   while (scanf( %d %d %d %d %d , &p1, &p2, &v1, &v2, &k) != EOF) { n     long long ans = 0; n     for (int i = 1; i + k < ss; i++) { n       int x = vct[i - 1] + 1; n       int a = vct[i]; n       int b = vct[i + k - 1]; n       int y = vct[i + k] - 1; n       long long tmp = 0; n       tmp += calc(x, a, p1, p2) * calc(b, y, v1, v2); n       tmp += calc(x, a, v1, v2) * calc(b, y, p1, p2); n       if (k == 1 && v1 <= a && a <= p2 && tmp > 0) { n         tmp--; n       } n       ans += tmp; n     } n     double a = 1.0 * ans / (p2 - p1 + 1) / (v2 - v1 + 1); n     printf( %.20f n , a); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n string a = n      What are you doing at the end of the world? Are you busy? Will you save   n      us? ; n string pref =  What are you doing while sending   ; n string suff =   ? Are you busy? Will you send   ; n string last =   ? ; n long long f[100001]; n long long q, n, k, n1, n2, n3, n4; n const long long inf = 1e18 + 10; n char solution(long long n, long long k) { n   if (k > f[n]) return  . ; n   if (n == 0) return a[k - 1]; n   if (k <= n2) return pref[k - 1]; n   k = k - n2; n   if (k <= f[n - 1]) return solution(n - 1, k); n   k = k - f[n - 1]; n   if (k <= n3) return suff[k - 1]; n   k = k - n3; n   if (k <= f[n - 1]) return solution(n - 1, k); n   k = k - f[n - 1]; n   if (k <= n4) return last[k - 1]; n } n int main() { n   n1 = a.length(); n   n2 = pref.length(); n   n3 = suff.length(); n   n4 = last.length(); n   f[0] = n1; n   for (int i = 1; i <= 100000; i++) n     f[i] = min(inf, n2 + f[i - 1] + n3 + f[i - 1] + n4); n   cin >> q; n   while (q > 0) { n     cin >> n >> k; n     q = q - 1; n     cout << solution(n, k); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long MOD = 1e9 + 7; n const long long INF = 1e9 + 8; n const long double pi = 3.14159265359; n template <class T> n T fac(T n) { n   T res = 1, i; n   for (i = 2; i <= n; i++) res *= i; n   return res; n } n template <class T> n T lcm(T a, T b) { n   return (a * b) / __gcd(a, b); n } n template <class T> n T digitsum(T x) { n   T sum = 0; n   while (x) { n     sum += x % 10; n     x /= 10; n   } n   return sum; n } n string tostring(long long a) { n   string ans; n   while (a) { n     ans += char(a % 10 +  0 ); n     a /= 10; n   } n   return ans; n } n void nPermute(string str, long long n) { n   sort((str).begin(), (str).end()); n   long long i = 1; n   do { n     if (i == n) break; n     i++; n   } while (next_permutation(str.begin(), str.end())); n   cout << str << endl; n } n vector<long long> factors(long long n) { n   vector<long long> f; n   for (long long x = 2; x * x <= n; x++) { n     while (n % x == 0) { n       f.push_back(x); n       n /= x; n     } n   } n   if (n > 1) f.push_back(n); n   return f; n } n bool prime(long long n) { n   if (n < 2) return false; n   for (long long x = 2; x * x <= n; x++) n     if (n % x == 0) return false; n   return true; n } n long long ncr(long long n, long long k) { n   long long res = 1; n   if (k > n - k) k = n - k; n   for (long long i = 0; i < k; i++) { n     res *= (n - i); n     res /= (i + 1); n   } n   return res; n } n tuple<long long, long long, long long> Gcd(long long a, long long b) { n   if (b == 0) n     return {1, 0, a}; n   else { n     long long x, y, g; n     tie(x, y, g) = Gcd(b, a % b); n     return {y, x - (a / b) * y, g}; n   } n } n const long long mod = 1e5; n long long binpow(long long a, long long b, long long m) { n   a %= m; n   long long res = 1; n   while (b > 0) { n     if (b & 1) res = res * a % m; n     a = a * a % m; n     b >>= 1; n   } n   return res % m; n } n void solve() { n   long double n, r; n   cin >> n >> r; n   long double ang1 = 1 / (tan(pi / (2 * n))), ang2 = 1 / (tan(pi / n)); n   long double ans = (n * r * r) / (ang1 + ang2); n   cout << fixed << setprecision(50) << ans << endl; n } n signed main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0), cout.tie(0); n   solve(); n   cerr <<  Time elapsed :   << 1.0 * clock() / CLOCKS_PER_SEC <<   sec  n ; n } n


#include <bits/stdc++.h> n using namespace std; n int getV(char c) { n   if (c >=  0  && c <=  9 ) return c -  0 ; n   if (c >=  A  && c <=  Z ) return 10 + c -  A ; n   if (c >=  a  && c <=  z ) return 36 + c -  a ; n   if (c ==  - ) return 62; n   if (c ==  _ ) return 63; n } n int main() { n   string s; n   cin >> s; n   int N = s.length(); n   long long p3[6 * N + 1]; n   p3[0] = 1; n   for (int i = 1; i < 6 * N + 1; i++) p3[i] = (p3[i - 1] * 3) % 1000000007; n   int cnt = 0; n   for (int i = 0; i < N; i++) { n     int v = getV(s[i]); n     for (int j = 0; j < 6; j++) n       if ((v & (1 << j)) == 0) cnt++; n   } n   cout << p3[cnt]; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 2e5 + 5; n struct edge n { n  int u, v, w; n  bool operator < (const edge &oth) const n  { n   return w < oth.w; n  } n } e[N << 1]; n int n, m, x, mn = INT_MAX; n long long ans, rem; n struct disjoint_sets_union n { n  int fa[N]; n  int Query(int p) n  { n   if(fa[p] == p) return p; n   return fa[p] = Query(fa[p]); n  } n   n } dsu; n queue<int> que; n set<int> unvised, G[N]; n bool vis[N], type[N]; n void FindBlocks() n { n  for(int i = 1; i <= n; i++) if(!vis[i]) n  { n   que.push(i); n   vis[i] = true; n   unvised.erase(i); n   dsu.fa[i] = i; n   while(!que.empty()) n   { n    int u = que.front(); n    que.pop(); n    for(set<int>::iterator it = unvised.begin(); it != unvised.end();) n    { n     int v = *it; n     it++; n     if(G[u].count(v)) continue; n     dsu.fa[v] = u; n     unvised.erase(v); n     vis[v] = true; n     que.push(v); n     rem--; n    } n   } n  } n } n int main() n { n  ios::sync_with_stdio(false); n  cin >> n >> m; n  rem = 1LL * n * (n - 1) / 2 - m; n  for(int i = 1; i <= n; i++) unvised.insert(i); n  for(int i = 1; i <= m; i++) n  { n   cin >> e[i].u >> e[i].v >> e[i].w; n   G[e[i].u].insert(e[i].v); n   G[e[i].v].insert(e[i].u); n   x ^= e[i].w; n  } n  sort(e + 1, e + m + 1); n  FindBlocks();  n  for(int i = 1; i <= m; i++) n  { n   int fau = dsu.Query(e[i].u), fav = dsu.Query(e[i].v); n   if(fau != fav) n   { n    dsu.fa[fau] = fav; n    ans += e[i].w; n    type[i] = 1; // type 1 n   } n  } n  if(rem > 0) return printf( %lld n , ans) && 0; n  for(int i = 1; i <= n; i++) dsu.fa[i] = i; n  for(int i = 1; i <= m; i++) n  { n   int fau = dsu.Query(e[i].u), fav = dsu.Query(e[i].v); n   if(fau == fav) continue; // type 2 n   dsu.fa[fau] = fav; n   if(!type[i]) { mn = e[i].w; break; } // type 3 n  } n  printf( %lld n , ans + min(x, mn)); n  return 0;  n }


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(nullptr); n   int n, k, left = 0, i; n   long long int temp; n   cin >> n >> k; n   vector<int> task(k, 0); n   vector<pair<int, int>> idl(n, make_pair(0, 0)); n   for (i = 0; i < n; i++) { n     cin >> temp; n     task[temp - 1]++; n     idl[i].first = temp; n   } n   for (i = 0; i < k; i++) left += (int)(task[i] == 0); n   for (i = 0; i < n; i++) { n     cin >> temp; n     if (task[idl[i].first - 1] == 1) temp = INT_MAX; n     idl[i].second = temp; n   } n   if (!left) { n     cout << 0 << endl; n     return 0; n   } n   sort(idl.begin(), idl.end(), n        [](const pair<int, int> &left, const pair<int, int> &right) { n          return left.second < right.second; n        }); n   temp = 0; n   for (i = 0; i < n && left; i++) { n     if (task[idl[i].first - 1] > 1) { n       temp += idl[i].second; n       task[idl[i].first - 1]--; n       left--; n     } n   } n   cout << temp << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n bool debug = 0; n int n, m, k; n int dx[4] = {0, 1, 0, -1}, dy[4] = {1, 0, -1, 0}; n string direc =  RDLU ; n long long ln, lk, lm; n void etp(bool f = 0) { n   puts(f ?  Possible  :  Impossible ); n   exit(0); n } n void addmod(int &x, int y, int mod = 1000000007) { n   x += y; n   if (x >= mod) x -= mod; n } n void et() { n   puts( -1 ); n   exit(0); n } n int x[4], y[4]; n pair<int, int> ans[4]; n int ansVal = (1 << 30); n void ckeck(int len, int bx, int by) { n   int d[4]; n   pair<int, int> tar[4]; n   for (int(i) = 0; (i) < (int)(4); (i)++) d[i] = i; n   tar[0] = {bx, by}; n   tar[1] = {bx + len, by}; n   tar[2] = {bx, by + len}; n   tar[3] = {bx + len, by + len}; n   do { n     bool ok = 1; n     int tmp = 0; n     for (int(i) = 0; (i) < (int)(4); (i)++) { n       int id = d[i]; n       if (x[id] != tar[i].first && y[id] != tar[i].second) { n         ok = 0; n         break; n       } else if (x[id] == tar[i].first) { n         tmp = max(tmp, abs(y[id] - tar[i].second)); n       } else n         tmp = max(tmp, abs(x[id] - tar[i].first)); n     } n     if (!ok) continue; n     if (tmp < ansVal) { n       ansVal = tmp; n       for (int(i) = 0; (i) < (int)(4); (i)++) ans[d[i]] = tar[i]; n     } n   } while (next_permutation(d, d + 4)); n } n void fmain() { n   ansVal = (1 << 30); n   for (int(i) = 0; (i) < (int)(4); (i)++) scanf( %d%d , x + i, y + i); n   set<int> ds; n   set<int> px, py; n   for (int(i) = 0; (i) < (int)(4); (i)++) n     for (int j = i + 1; j < 4; j++) { n       if (x[i] != x[j]) ds.insert(abs(x[i] - x[j])); n       if (y[i] != y[j]) ds.insert(abs(y[i] - y[j])); n     } n   for (int d : ds) { n     for (int(i) = 0; (i) < (int)(4); (i)++) { n       for (int j = -d; j <= d; j += d) { n         px.insert(x[i] + j); n         py.insert(y[i] + j); n       } n     } n   } n   for (int d : ds) { n     int id[4]; n     for (int(i) = 0; (i) < (int)(4); (i)++) id[i] = i; n     do { n       int mxX = -(1 << 30), miX = (1 << 30), mxY = -(1 << 30), miY = (1 << 30); n       int tx[4], ty[4]; n       tx[0] = x[id[0]]; n       ty[0] = y[id[0]]; n       tx[1] = x[id[1]] - d; n       ty[1] = y[id[1]]; n       tx[2] = x[id[2]]; n       ty[2] = y[id[2]] - d; n       tx[3] = x[id[3]] - d; n       ty[3] = y[id[3]] - d; n       for (int(i) = 0; (i) < (int)(4); (i)++) { n         mxX = max(mxX, tx[i]); n         miX = min(miX, tx[i]); n         mxY = max(mxY, ty[i]); n         miY = min(miY, ty[i]); n       } n       px.insert((mxX + miX) / 2); n       py.insert((mxY + miY) / 2); n     } while (next_permutation(id, id + 4)); n   } n   for (int d : ds) n     for (int xx : px) n       for (int yy : py) ckeck(d, xx, yy); n   if (ansVal == (1 << 30)) n     puts( -1 ); n   else { n     printf( %d n , ansVal); n     for (int(i) = 0; (i) < (int)(4); (i)++) n       printf( %d %d n , ans[i].first, ans[i].second); n   } n } n int main() { n   int t; n   cin >> t; n   while (t--) fmain(); n   return 0; n } n


#include <bits/stdc++.h> n #pragma GCC optimize( Ofast ) n using namespace std; n void subMain() { n   double a, b; n   cin >> a >> b; n   cout << fixed << (b * b - a * a) / (2 * a); n } n int32_t main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   subMain(); n   cerr <<  Time :   << 1000 * ((double)clock()) / (double)CLOCKS_PER_SEC n        <<  ms n ; n   return 0; n } n


#include <bits/stdc++.h> n #pragma comment(linker,  /stack:200000000 ) n #pragma GCC optimize( Ofast,no-stack-protector ) n #pragma GCC target( avx ) n using namespace std; n template <class T> n int getbit(T s, int i) { n   return (s >> i) & 1; n } n template <class T> n T onbit(T s, int i) { n   return s | (T(1) << i); n } n template <class T> n T offbit(T s, int i) { n   return s & (~(T(1) << i)); n } n template <class T> n int cntbit(T s) { n   return __builtin_popcount(s); n } n template <class T> n T gcd(T a, T b) { n   T r; n   while (b != 0) { n     r = a % b; n     a = b; n     b = r; n   } n   return a; n } n template <class T> n T lcm(T a, T b) { n   return a / gcd(a, b) * b; n } n const int MAXN = 100; n const int MOD = 1e9 + 7; n const long long MAXV = 1e6; n const double eps = 1e-12; n const long long INF = 1e16; n inline string toStr(long long x) { n   string tmp =   ; n   do tmp = char(x % 10 +  0 ) + tmp; n   while (x /= 10); n   return tmp; n } n inline long long toInt(string s) { n   long long res = 0; n   for (auto x : s) res = res * 10 + x -  0 ; n   return res; n } n inline string toBinStr(long long x) { n   string res =   ; n   do res = (x % 2 ?  1  :  0 ) + res; n   while (x >>= 1LL); n   return res; n } n long long rnd(int k) { n   if (!k) return rand() % MAXV + 1; n   long long t = rand() % MAXV + 1; n   return (rand() % t) + (MAXV - t); n } n long long random_gen(int sign) { n   long long x = rnd(rand() % 2); n   long long s = rand() % 2; n   s = !s ? 1 : -1; n   return sign == 1 ? x : sign == -1 ? -x : s * x; n } n inline void debugArray(int a[], int n) { n   cerr <<  array =  ; n   for (int i = 0; i < n; ++i) cerr << a[i] <<    ; n   cerr <<   n ; n } n int n, m; n vector<int> row[MAXN]; n bool color[MAXN]; n string s[MAXN], actual[MAXN]; n int main() { n   cin >> n >> m; n   for (int i = 0, _a = (n); i < _a; ++i) { n     cin >> s[i]; n     for (int j = 0, _a = (m); j < _a; ++j) n       if (s[i][j] ==  # ) row[i].push_back(j); n   } n   for (int i = 0, _a = (n); i < _a; ++i) { n     actual[i] =   ; n     for (int j = 0, _a = (m); j < _a; ++j) actual[i] +=  . ; n   } n   for (int i = 0, _a = (n); i < _a; ++i) n     if (!color[i]) { n       vector<int> need; n       color[i] = true; n       need.push_back(i); n       for (int j = 0, _a = (n); j < _a; ++j) n         if (!color[j]) n           for (int k = 0, _a = (m); k < _a; ++k) n             if (s[i][k] ==  #  && s[j][k] ==  # ) { n               need.push_back(j); n               color[j] = true; n               break; n             } n       for (auto x : need) n         for (auto y : row[i]) actual[x][y] =  # ; n     } n   bool good = true; n   for (int i = 0, _a = (n); i < _a; ++i) n     for (int j = 0, _a = (m); j < _a; ++j) good &= actual[i][j] == s[i][j]; n   puts(good ?  Yes  :  No ); n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 2e5 + 1; n int n, k; n int clo[N]; n string s; n bool block[N]; n int dist(int a, int b) { n   if (a > b) swap(a, b); n   if (a == -1 || b == n) return n; n   return min(b - a, n - (b - a)); n } n char neg(char a) { n   if (a ==  W ) n     return  B ; n   else n     return  W ; n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   cin >> n >> k >> s; n   for (int i = 0; i < n; ++i) { n     if (s[i] == s[(i - 1 + n) % n] || s[i] == s[(i + 1) % n]) { n       block[i] = 1; n     } n   } n   int cur = -1, l = n, r = -1; n   for (int i = 0; i < n; ++i) { n     clo[i] = n; n     if (block[i]) { n       l = min(l, i); n       r = max(r, i); n     } n   } n   for (int i = 0; i < n; ++i) { n     if (block[i]) { n       cur = i; n     } n     clo[i] = min(clo[i], dist(i, cur)); n   } n   cur = -1; n   for (int i = n - 1; i >= 0; --i) { n     if (block[i]) { n       cur = i; n     } n     clo[i] = min(clo[i], dist(i, cur)); n   } n   for (int i = 0; i < n; ++i) { n     clo[i] = min(clo[i], dist(i, l)); n     clo[i] = min(clo[i], dist(i, r)); n   } n   for (int i = 0; i < n; ++i) { n     if (clo[i] <= k) { n       cout << ((clo[i] % 2 == 0) ? s[i] : neg(s[i])); n     } else { n       cout << ((k % 2 == 0) ? s[i] : neg(s[i])); n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n struct Node { n   int v, index; n } a[100005]; n int n, h, big, small, tmp, diff, num; n int ans[100005]; n int cmp(const void *i, const void *j) { n   Node x = *(Node *)i; n   Node y = *(Node *)j; n   if (x.v == y.v) { n     return x.index > y.index ? 1 : -1; n   } else { n     return x.v > y.v ? 1 : -1; n   } n } n int main() { n   while (scanf(  %d %d , &n, &h) != EOF) { n     for (int i = 0; i < n; i++) { n       scanf(  %d , &a[i].v); n       a[i].index = i; n     } n     qsort(a, n, sizeof(a[0]), cmp); n     num = 0, diff = (a[n - 1].v + a[n - 2].v) - (a[0].v + a[1].v); n     for (int i = 1; i < n; i++) { n       small = a[0].v + a[1].v + h; n       if (i >= 2 && small > a[1].v + a[2].v) small = a[1].v + a[2].v; n       if (i != n - 1 && small > a[0].v + a[i + 1].v) n         small = a[0].v + a[i + 1].v; n       if (i == n - 1) n         big = a[i].v + a[0].v + h; n       else n         big = a[n - 1].v + a[i].v + h; n       if (big < a[n - 1].v + a[n - 2].v) big = a[n - 1].v + a[n - 2].v; n       if (diff > big - small) { n         diff = big - small; n         num = i; n       } n     } n     fill(ans, ans + n, 1); n     for (int i = 1; i <= num; i++) ans[a[i].index] = 2; n     printf( %d n , diff); n     for (int i = 0; i < n; i++) printf( %d%c , ans[i], i == n - 1 ?   n  :    ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const double eps = 1e-8; n const double PI = acos(-1.0); n const double e = 2.718281828459; n const int N = 200000; n inline int sgn(double a); n inline long long gcd(long long a, long long b); n inline long long mod_pow(long long x, long long n, long long mod); n vector<int> vv; n vector<int> vis[1005]; n int main() { n   memset(vis, 0, sizeof(vis)); n   int n, m; n   string s; n   string t; n   cin >> n >> m; n   cin >> s; n   cin >> t; n   int maxidx = 0; n   int maxcount = 0; n   for (int i = 0; i <= m - n; i++) { n     int count = 0; n     for (int j = 0; j < n; j++) { n       if (s[j] != t[j + i]) { n         vis[i].push_back(j + 1); n       } else { n         ++count; n       } n     } n     if (maxcount < count) { n       maxcount = count; n       maxidx = i; n     } n   } n   cout << vis[maxidx].size() << endl; n   for (int i = 0; i < vis[maxidx].size(); i++) { n     cout << vis[maxidx][i] <<    ; n   } n   return 0; n } n inline int sgn(double a) { return a < -eps ? -1 : a < eps ? 0 : 1; } n inline long long gcd(long long a, long long b) { n   return a == 0 ? b : gcd(b % a, a); n } n long long mod_pow(long long x, long long n, long long mod) { n   long long res = 1; n   while (n > 0) { n     if (n & 1) res = res * x % mod; n     x = x * x % mod; n     n = n >> 1; n   } n   return res; n } n


#include <bits/stdc++.h> n using namespace std; n const int Maxn = 1e5 + 1e3 + 2; n const int mx = 20; n int n, q, N, ans; n int k[Maxn]; n vector<int> V[Maxn]; n vector<pair<int, int>> g; n int h[Maxn], par[mx][Maxn]; n bool mark[Maxn]; n int st[Maxn], en[Maxn], T; n set<pair<int, int>> ver, lca; n void dfs(int u, int DD = 0) { n   st[u] = T++; n   par[0][u] = DD; n   for (int i = 1; i < mx; i++) par[i][u] = par[i - 1][par[i - 1][u]]; n   for (int i = 0; i < V[u].size(); i++) { n     int v = V[u][i]; n     if (v != DD) h[v] = h[u] + 1, dfs(v, u); n   } n   en[u] = T++; n } n int LCA(int u, int v) { n   if (h[v] < h[u]) swap(u, v); n   for (int i = mx; i--;) n     if (h[par[i][v]] >= h[u]) v = par[i][v]; n   if (v == u) return v; n   for (int i = mx; i--;) n     if (par[i][v] != par[i][u]) v = par[i][v], u = par[i][u]; n   return par[0][v]; n } n bool check() { n   bool res = true; n   for (int i = 0; i < N; i++) mark[k[i]] = true; n   for (int i = 0; i < N; i++) n     if (mark[par[0][k[i]]]) res = false; n   return res; n } n void solve() { n   if (!check()) { n     for (int i = 0; i < N; i++) mark[k[i]] = false; n     ans = -1; n     return; n   } n   ans = 0; n   ver.clear(); n   lca.clear(); n   for (int i = 0; i < N; i++) ver.insert(make_pair(st[k[i]], k[i])); n   set<pair<int, int>>::iterator it = ver.begin(); n   for (it++; it != ver.end(); it++) { n     int v = (*it).second; n     it--; n     int u = (*it).second; n     it++; n     int DD = LCA(u, v); n     lca.insert(make_pair(-h[DD], DD)); n   } n   for (; ver.size() > 1;) { n     ans++; n     pair<int, int> p = (*lca.begin()); n     lca.erase(p); n     int v = p.second; n     it = ver.upper_bound(make_pair(st[v], n)); n     for (; it != ver.end() && ver.size(); it++) { n       int u = (*it).second; n       if (st[u] > en[v]) break; n       if (mark[v]) ans++; n       g.push_back(*it); n     } n     ans -= mark[v]; n     if (!mark[v] && g.size() < 2) { n       g.clear(); n       ans--; n       continue; n     } n     for (int i = 0; i < g.size(); i++) n       ver.erase(g[i]), mark[g[i].second] = false; n     g.clear(); n   } n   for (int i = 0; i < N; i++) mark[k[i]] = false; n } n int main() { n   cin >> n; n   for (int u, v, i = 0; i < n - 1; i++) { n     cin >> u >> v; n     V[u].push_back(v); n     V[v].push_back(u); n   } n   h[1] = 1; n   dfs(1); n   cin >> q; n   for (int j = 0; j < q; j++) { n     cin >> N; n     for (int i = 0; i < N; i++) cin >> k[i]; n     solve(); n     cout << ans << endl; n   } n } n


#include <bits/stdc++.h> n using namespace ::std; n int n, m, k, ans; n int a[105][105]; n int d[3][105]; n int a1[105]; n int main() { n   scanf( %d %d %d , &n, &m, &k); n   for (int i = 1; i <= n; i++) n     for (int j = 1; j <= m; j++) { n       scanf( %d , &a[i][j]); n     } n   ans = 2140000000; n   if (n > 10) { n     for (int i = 1; i <= n; i++) { n       int an = 0; n       for (int j = 1; j <= n; j++) { n         int op = 0; n         for (int k = 1; k <= m; k++) n           if (a[i][k] != a[j][k]) op++; n         an += min(op, m - op); n       } n       ans = min(ans, an); n     } n   } else { n     for (int i = 0; i < (1 << n); i++) { n       for (int j = 0; j < n; j++) { n         if ((i & (1 << j)) != 0) n           a1[j] = 1; n         else n           a1[j] = 0; n       } n       int an = 0; n       for (int j = 1; j <= m; j++) { n         int op = 0; n         for (int k = 1; k <= n; k++) n           if (a[k][j] != a1[k - 1]) op++; n         an += min(op, n - op); n       } n       ans = min(ans, an); n     } n   } n   if (ans > k) n     printf( -1 ); n   else n     printf( %d , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1 << 20; n long long here[N + 100]; n int n, m; n long double ter[N + 100]; n int go(string &s1, string &s2) { n   int val = 0; n   for (int i = int(0); i < int(m); ++i) n     if (s1[i] == s2[i]) val |= (1 << i); n   return val; n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout << fixed << setprecision(14); n   cin >> n; n   vector<string> vec(n); n   for (int i = int(0); i < int(n); ++i) cin >> vec[i]; n   m = int((vec[0]).size()); n   for (int i = int(0); i < int(n); ++i) n     for (int j = int(i + 1); j < int(n); ++j) n       if (i != j) { n         int mask = go(vec[i], vec[j]); n         here[mask] |= (1LL << i) | (1LL << j); n       } n   int lim = (1 << m) - 1; n   for (int mask = int(lim); mask >= int(0); mask--) n     for (int i = int(0); i < int(m); ++i) here[mask] |= here[mask | (1 << i)]; n   ter[lim] = 0; n   for (int i = int(lim - 1); i >= int(0); i--) { n     ter[i] = __builtin_popcountll(here[i]); n     long double val = 0; n     for (int j = int(0); j < int(m); ++j) n       if (1 - ((i >> j) & 1)) val += ter[i | (1 << j)]; n     ter[i] += val / (m - __builtin_popcount(i)); n   } n   cout << ter[0] / n << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 1e6; n int n, k; n int root; n vector<int> G[MAXN + 10]; n int res = 0; n int dfs(int u, int p = -1) { n   if ((int)G[u].size() == 1) return 0; n   vector<int> d; n   for (int v : G[u]) { n     if (v == p) continue; n     d.push_back(dfs(v, u) + 1); n   } n   sort(d.begin(), d.end(), greater<int>()); n   while (d.size() > 1) { n     int b = d.back(); n     d.pop_back(); n     int a = d.back(); n     d.pop_back(); n     if (a + b > k) { n       res += d.size() + 1; n       return b; n     } else { n       d.push_back(a); n     } n   } n   return d[0]; 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   root = 1; n   while ((int)G[root].size() == 1) ++root; n   dfs(root); n   res++; n   printf( %d n , res); n } n


#include <bits/stdc++.h> n using namespace std; n #pragma comment(linker,  /STACK:64000000 ) n int prec[10] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; n const int mod = 1000000000 + 7; n int n, k; n long long doIt(int m, int bse) { n   int b[10]; n   for (int i = 0; i < 10; i++) b[i] = 0; n   int res = 0; n   while (!b[m]) { n     int can = true; n     for (int i = 0; i < m && can; i++) { n       int cur = i; n       int iter = m + 1; n       while (iter && cur) { n         cur = b[cur]; n         iter--; n       } n       can &= (cur == 0); n     } n     if (can) res++; n     int ind = 0; n     b[ind]++; n     while (b[ind] >= bse) { n       b[ind] = 0; n       ind++; n       b[ind]++; n     } n   } n   return res % mod; n } n int main() { n   cin >> n >> k; n   k = min(k, 8); n   long long res = 1; n   if (k > 1) res = doIt(k, k) % mod; n   for (int i = 0; i < n - k; i++) { n     res = res * 1LL * (n - k); n     res %= mod; n   } n   cout << res << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 100000; n int n, a, b; n int scores[MAXN], answer[MAXN]; n pair<int, int> tmp_scores[MAXN]; n void solve() { n   if (a == b) { n     for (int i = 0; i < a; ++i) printf( 1  ); n     for (int i = 0; i < b; ++i) printf( 2  ); n     printf(  n ); n     return; n   } n   if (b < a) n     for (int i = 0; i < n; ++i) tmp_scores[i] = make_pair(scores[i], i); n   else n     for (int i = 0; i < n; ++i) tmp_scores[i] = make_pair(6 - scores[i], i); n   sort(&tmp_scores[0], &tmp_scores[n]); n   for (int i = 0; i < a; ++i) answer[tmp_scores[i].second] = 1; n   for (int i = a; i < n; ++i) answer[tmp_scores[i].second] = 2; n   for (int i = 0; i < n; ++i) printf( %d  , answer[i]); n   printf(  n ); n } n int main() { n   scanf( %d%d%d , &n, &a, &b); n   for (int i = 0; i < n; ++i) scanf( %d , &scores[i]); n   solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n using ll = long long; n using ld = long double; n vector<vector<vector<ll>>> v; n vector<vector<ll>> mat; n vector<vector<ll>> matMal(vector<vector<ll>> mat1, vector<vector<ll>> mat2) { n   vector<vector<ll>> mat3((int)(mat1).size(), vector<ll>((int)(mat1).size())); n   for (int i = 0; i < (int)(mat1).size(); ++i) { n     for (int j = 0; j < (int)(mat1).size(); ++j) { n       ll sum = 0; n       for (int k = 0; k < (int)(mat1).size(); ++k) { n         sum = sum + (ll)mat1[i][k] * mat2[k][j] % 1000000007; n       } n       mat3[i][j] = sum % 1000000007; n     } n   } n   return mat3; n } n vector<vector<ll>> matMult(vector<vector<ll>> matr, ll x) { n   if (x == 1) return matr; n   if (x % 2 == 1) return matMal(matMult(matr, x - 1), mat); n   vector<vector<ll>> matTemp = matMult(matr, x / 2); n   return matMal(matTemp, matTemp); n } n int main() { n   int n, k, m; n   vector<ll> anzb = {0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4}; n   cin >> n >> k >> m; n   mat.resize((1 << m) * (k + 1), vector<ll>((1 << m) * (k + 1))); n   for (int i = 0; i < 1 << m; ++i) { n     for (int j = 0; j < k + 1; ++j) { n       if (i % 2 == 0) { n         mat[(k + 1) * i + j][(k + 1) * (i / 2) + j] = 1; n         mat[(k + 1) * i + j][(k + 1) * ((i + (1 << m)) / 2) + j] = 1; n       } else if (j != 0) { n         mat[(k + 1) * i + j][(k + 1) * (i / 2) + j - 1] = anzb[i / 2] + 1; n         mat[(k + 1) * i + j][(k + 1) * ((i + (1 << m)) / 2) + j - 1] = n             anzb[(i + (1 << m)) / 2] + 1; n       } n     } n   } n   mat = matMult(mat, n); n   ll sum = 0; n   for (int i = 0; i < 1 << m; ++i) { n     sum = (sum + mat[(k + 1) * i + k][0]) % 1000000007; n   } n   cout << sum << endl; n } n


#include <bits/stdc++.h> n using namespace std; n using pii = pair<int, int>; n using ll = long long; n pii operator+(pii& p1, pii& p2) { n   return {p1.first + p2.first, p1.second + p2.second}; n } n bool isvalidpos(pii& p, int n) { n   return p.first >= 0 && p.first < n && p.second >= 0 && p.second < n; n } n int main() { n   int n; n   cin >> n; n   vector<vector<int>> v1(n, vector<int>(n, -1)); n   vector<vector<int>> v2(n, vector<int>(n, -1)); n   v1[0][0] = 1; n   v1[n - 1][n - 1] = 0; n   v2[0][0] = 1; n   v2[n - 1][n - 1] = 0; n   queue<pii> q; n   q.push({0, 0}); n   pii dyx[] = {{2, 0}, {0, 2}, {1, 1}}; n   while (!q.empty()) { n     pii now = q.front(); n     q.pop(); n     for (int i = 0; i < 3; i++) { n       pii nxt = now + dyx[i]; n       if (!isvalidpos(nxt, n)) continue; n       if (v1[nxt.first][nxt.second] != -1) continue; n       cout <<  ?   << now.first + 1 <<     << now.second + 1 <<     n            << nxt.first + 1 <<     << nxt.second + 1 << endl; n       cout.flush(); n       int res; n       cin >> res; n       if (res == 1) n         v2[nxt.first][nxt.second] = v1[nxt.first][nxt.second] = n             v1[now.first][now.second]; n       else n         v2[nxt.first][nxt.second] = v1[nxt.first][nxt.second] = n             1 - v1[now.first][now.second]; n       q.push(nxt); n     } n   } n   v1[0][1] = 0; n   v2[0][1] = 1; n   q.push({0, 1}); n   while (!q.empty()) { n     pii now = q.front(); n     q.pop(); n     for (int i = 0; i < 3; i++) { n       pii nxt = now + dyx[i]; n       if (!isvalidpos(nxt, n)) continue; n       if (v1[nxt.first][nxt.second] != -1) continue; n       cout <<  ?   << now.first + 1 <<     << now.second + 1 <<     n            << nxt.first + 1 <<     << nxt.second + 1 << endl; n       cout.flush(); n       int res; n       cin >> res; n       if (res == 1) { n         v2[nxt.first][nxt.second] = v2[now.first][now.second]; n         v1[nxt.first][nxt.second] = v1[now.first][now.second]; n       } else { n         v2[nxt.first][nxt.second] = 1 - v2[now.first][now.second]; n         v1[nxt.first][nxt.second] = 1 - v1[now.first][now.second]; n       } n       q.push(nxt); n     } n   } n   for (int i = 1; i < n; i += 2) { n     cout <<  ?   << i + 1 <<     << 1 <<     << i + 2 <<     << 2 << endl; n     cout.flush(); n     int res; n     cin >> res; n     if (res == 1) { n       v2[i][0] = v2[i + 1][1]; n       v1[i][0] = v1[i + 1][1]; n     } else { n       v2[i][0] = 1 - v2[i + 1][1]; n       v1[i][0] = 1 - v1[i + 1][1]; n     } n   } n   bool answer = false; n   pii dyx2[][4] = {{{0, 0}, {0, 1}, {0, 2}, {1, 2}}, n                    {{0, 0}, {0, 1}, {1, 1}, {1, 2}}, n                    {{0, 0}, {1, 0}, {1, 1}, {1, 2}}}; n   bool end = false; n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < n; j++) { n       bool palin1 = false; n       bool palin2 = false; n       pii now = {i, j}; n       for (int k1 = 0; k1 < 3; k1++) { n         bool check = true; n         for (int k2 = 0; k2 < 4; k2++) { n           pii nxt = now + dyx2[k1][k2]; n           if (!isvalidpos(nxt, n)) { n             check = false; n             break; n           } n         } n         if (!check) continue; n         if (v1[now.first + dyx2[k1][0].first] n               [now.second + dyx2[k1][0].second] == n                 v1[now.first + dyx2[k1][3].first] n                   [now.second + dyx2[k1][3].second] && n             v1[now.first + dyx2[k1][1].first] n               [now.second + dyx2[k1][1].second] == n                 v1[now.first + dyx2[k1][2].first] n                   [now.second + dyx2[k1][2].second]) n           palin1 = true; n         if (v2[now.first + dyx2[k1][0].first] n               [now.second + dyx2[k1][0].second] == n                 v2[now.first + dyx2[k1][3].first] n                   [now.second + dyx2[k1][3].second] && n             v2[now.first + dyx2[k1][1].first] n               [now.second + dyx2[k1][1].second] == n                 v2[now.first + dyx2[k1][2].first] n                   [now.second + dyx2[k1][2].second]) n           palin2 = true; n       } n       if (palin1 || palin2) { n         cout <<  ?   << now.first + 1 <<     << now.second + 1 <<     n              << now.first + 2 <<     << now.second + 3 << endl; n         cout.flush(); n         int res; n         cin >> res; n         if (res == 1) { n           if (palin1) n             answer = true; n           else n             answer = false; n         } else { n           if (palin1) n             answer = false; n           else n             answer = true; n         } n         end = true; n         break; n       } n     } n     if (end) break; n   } n   cout <<  !  << endl; n   cout.flush(); n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < n; j++) { n       if (answer) n         cout << v1[i][j]; n       else n         cout << v2[i][j]; n     } n     cout << endl; n     cout.flush(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int a[105]; n int main() { n   int i, n, j = 0, k, l; n   cin >> n; n   for (i = 0; i < n; i++) { n     cin >> a[i]; n   } n   sort(a + 1, a + n); n   while (a[0] <= a[n - 1]) { n     j++; n     a[n - 1]--; n     a[0]++; n     sort(a + 1, a + n); n   } n   cout << j << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   cin.tie(NULL); n   cout.tie(NULL); n   ios_base ::sync_with_stdio(false); n   int n; n   cin >> n; n   vector<pair<long long, long long>> news(n + 1); n   for (int i = 1; i <= n; i++) cin >> news[i].first; n   for (int i = 1; i <= n; i++) cin >> news[i].second; n   long long ans = 0; n   priority_queue<long long> q; n   sort(news.begin() + 1, news.end()); n   long long val = news[1].first, sum = 0; n   news.push_back(make_pair(2e9, 0)); n   for (int i = 1; i <= n + 1; i++) { n     while (val < news[i].first && q.size() > 0) { n       sum -= q.top(); n       q.pop(); n       ans += sum; n       val++; n     } n     val = news[i].first; n     q.push(news[i].second); n     sum += news[i].second; n   } n   cout << ans; n } n


#include <bits/stdc++.h> n #pragma GCC optimize(2) n #pragma GCC optimize(3) n #pragma GCC optimize( Ofast ) n #pragma GCC optimize( inline ) n using namespace std; n const long long p = 998244353, g = 3, gv = 332748118, N = 1e6 + 5; n long long qpow(long long a, int b) { n   long long ret = 1; n   while (b) { n     if (b & 1) ret = ret * a % p; n     a = a * a % p; n     b >>= 1; n   } n   return ret; n } n long long inv(long long a) { return qpow(a, p - 2); } n int rev[N]; n void getrev(int len) { n   for (int i = 0; i < len; i++) { n     rev[i] = rev[i >> 1] >> 1; n     if (i & 1) rev[i] |= (len >> 1); n   } n } n void ntt(vector<long long> &a, int len, int tp) { n   a.resize(len); n   for (int i = 0; i < len; i++) n     if (i < rev[i]) swap(a[i], a[rev[i]]); n   for (int i = 2; i <= len; i <<= 1) { n     long long wn = qpow(tp == 1 ? g : gv, (p - 1) / i); n     int stp = i >> 1; n     for (int j = 0; j < len; j += i) { n       long long w = 1; n       for (int k = j; k < j + stp; k++) { n         long long s1 = a[k], s2 = a[k + stp] * w % p; n         w = w * wn % p; n         a[k] = (s1 + s2) % p, a[k + stp] = (s1 + p - s2) % p; n       } n     } n   } n   if (tp == -1) { n     long long lv = inv(len); n     for (int i = 0; i < len; i++) a[i] = a[i] * lv % p; n   } n } n vector<long long> operator*(vector<long long> a, vector<long long> b) { n   int len = 1, n = a.size() + b.size() - 1; n   while (len < n) len <<= 1; n   getrev(len); n   ntt(a, len, 1), ntt(b, len, 1); n   for (int i = 0; i < len; i++) a[i] = a[i] * b[i] % p; n   ntt(a, len, -1); n   a.resize(n); n   return a; n } n vector<long long> operator+(vector<long long> a, vector<long long> b) { n   if (a.size() < b.size()) swap(a, b); n   for (int i = 0; i < b.size(); i++) a[i] = (a[i] + b[i]) % p; n   return a; n } n vector<long long> operator-(vector<long long> a) { n   for (int i = 0; i < a.size(); i++) a[i] = (p - a[i]) % p; n   return a; n } n vector<long long> operator-(vector<long long> a, vector<long long> b) { n   return a + (-b); n } n vector<long long> operator*(long long b, vector<long long> a) { n   for (int i = 0; i < a.size(); i++) a[i] = a[i] * b % p; n   return a; n } n vector<long long> operator/(vector<long long> a, long long b) { n   int v = inv(b); n   for (int i = 0; i < a.size(); i++) a[i] = a[i] * v % p; n   return a; n } n long long fac[N], finv[N], iv[N]; n int main() { n   int n; n   long long m; n   scanf( %d%lld , &n, &m); n   n++; n   fac[0] = fac[1] = iv[1] = finv[0] = finv[1] = 1; n   for (int i = 2; i <= n; i++) n     fac[i] = fac[i - 1] * i % p, iv[i] = (p - p / i) * iv[p % i] % p, n     finv[i] = finv[i - 1] * iv[i] % p; n   vector<long long> a, b, c; n   a.resize(n), b.resize(n); n   for (int i = 0; i < n; i++) { n     int x; n     scanf( %d , &x); n     a[i] = x * 1ll * fac[i] % p, b[n - i - 1] = finv[i]; n   } n   c = a * b; n   for (int i = 0; i < n; i++) n     a[i] = c[n + i - 1] * 1ll * qpow(iv[i + 1], m % (p - 1)) % p, n     b[n - i - 1] = i & 1 ? p - finv[i] : finv[i]; n   c = a * b; n   for (int i = 0; i < n; i++) printf( %lld  , c[n + i - 1] * 1ll * finv[i] % p); n   printf(  n ); n } n


#include <bits/stdc++.h> n using namespace std; n int arr[1000001], dp[(1 << 22)], N; n int main() { n   scanf( %d , &N); n   memset(dp, -1, sizeof dp); n   for (int i = 0; i < N; i++) { n     scanf( %d , &arr[i]); n     dp[arr[i]] = arr[i]; n   } n   for (int i = 0; i < (1 << 22); i++) { n     for (int j = 0; j < 22; j++) { n       if (i & (1 << j)) { n         dp[i] = max(dp[i], dp[i ^ (1 << j)]); n       } n     } n   } n   for (int i = 0; i < N; i++) { n     int need = dp[((1 << 22) - 1) ^ arr[i]]; n     printf( %d%c , need, i == N - 1 ?   n  :    ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n string convert(string s) { n   int l = s.size(); n   if (l % 2 == 1) return s; n   string s1 = s.substr(0, l / 2), s2 = s.substr(l / 2); n   s1 = convert(s1); n   s2 = convert(s2); n   if (s1 < s2) n     return s1 + s2; n   else n     return s2 + s1; n } n bool equivalent(string s1, string s2) { n   s1 = convert(s1); n   s2 = convert(s2); n   return s1 == s2; n } n int main() { n   string a, b; n   cin >> a >> b; n   if (equivalent(a, b)) n     cout <<  YES  << endl; n   else n     cout <<  NO  << endl; n } n


#include <bits/stdc++.h> n using namespace std; n struct answer { n   int b, c; n   bool operator==(answer other) const { return b == other.b && c == other.c; } n   bool operator!=(answer other) const { return !(*this == other); } n }; n answer eval(string secret, string query) { n   string left_s, left_q; n   answer ans = {0, 0}; n   for (int i = 0; i < (int)(4); i++) { n     if (secret[i] == query[i]) n       ans.b++; n     else { n       left_s.push_back(secret[i]); n       left_q.push_back(query[i]); n     } n   } n   while (!left_s.empty()) { n     auto it = find(left_q.begin(), left_q.end(), left_s.back()); n     if (it != left_q.end()) { n       left_q.erase(it); n       ans.c++; n     } n     left_s.pop_back(); n   } n   return ans; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(nullptr); n   cout.tie(nullptr); n   int n; n   cin >> n; n   vector<string> q(n); n   vector<answer> anss(n); n   for (int i = 0; i < (int)(n); i++) { n     cin >> q[i] >> anss[i].b >> anss[i].c; n   } n   vector<string> answers; n   for (int a = 0; a < 10; a++) { n     for (int b = 0; b < 10; b++) { n       if (a == b) continue; n       for (int c = 0; c < 10; c++) { n         if (a == c || b == c) continue; n         for (int d = 0; d < 10; d++) { n           if (a == d || b == d || c == d) continue; n           string s; n           s.push_back( 0  + a); n           s.push_back( 0  + b); n           s.push_back( 0  + c); n           s.push_back( 0  + d); n           bool ok = true; n           for (int i = 0; i < (int)(n); i++) { n             if (anss[i] != eval(s, q[i])) { n               ok = false; n             } n           } n           if (ok) { n             answers.push_back(s); n           } n         } n       } n     } n   } n   if (answers.empty()) { n     cout <<  Incorrect data  << endl; n   } else if (answers.size() > 1) { n     cout <<  Need more data  << endl; n   } else { n     cout << answers[0] << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 1000006; n long long num[MAXN << 2]; n long long sum[MAXN << 2]; n long long N; n void pushup(int rt) { n   num[rt] = num[rt << 1] + num[rt << 1 | 1]; n   sum[rt] = sum[rt << 1] + sum[rt << 1 | 1]; n } n void update(int P, int C, int l, int r, int rt) { n   if (l == r) { n     num[rt] += C; n     sum[rt] += 1ll * P * C; n     return; n   } n   int m = (l + r) / 2; n   if (P <= m) n     update(P, C, l, m, rt << 1); n   else n     update(P, C, m + 1, r, rt << 1 | 1); n   pushup(rt); n } n long long query(int K, int l, int r, int rt) { n   if (l == r) { n     if (l == MAXN) { n       return 0; n     } n     if (K > 0) { n       return 1ll * K * l; n     } else n       return 0; n   } n   int m = (l + r) / 2; n   if (num[rt << 1] >= K) { n     return query(K, l, m, rt << 1); n   } else { n     return sum[rt << 1] + query(K - num[rt << 1], m + 1, r, rt << 1 | 1); n   } n } n vector<pair<int, int> > C[MAXN]; n vector<pair<int, int> > O[MAXN]; n int main() { n   int K, M; n   scanf( %d%d%d , &N, &K, &M); n   int l, r, c, p; n   for (int i = 0; i < M; i++) { n     scanf( %d%d%d%d , &l, &r, &c, &p); n     C[l].push_back(make_pair(p, c)); n     O[r].push_back(make_pair(p, c)); n   } n   long long ans = 0; n   for (int i = 1; i <= N; i++) { n     for (int j = 0; j < C[i].size(); j++) n       update(C[i][j].first, C[i][j].second, 1, MAXN, 1); n     ans += query(K, 1, MAXN, 1); n     for (int j = 0; j < O[i].size(); j++) n       update(O[i][j].first, -O[i][j].second, 1, MAXN, 1); n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n bool comp(int a, int b) { return (a > b); } n int gcd(long long int a, long long int b) { n   if (b == 0) n     return a; n   else n     return gcd(b, a % b); n } n int main(void) { n   int n, p; n   cin >> n >> p; n   int l[n], r[n], i; n   long long int c, cp, l1, l2; n   double ans = 0; n   for (i = 0; i < n; ++i) cin >> l[i] >> r[i]; n   for (i = 0; i < n; ++i) { n     l1 = r[i] - l[i] + 1; n     l2 = r[(i - 1 + n) % n] - l[(i - 1 + n) % n] + 1; n     c = r[i] / p - (l[i] - 1) / p; n     cp = r[(i - 1 + n) % n] / p - (l[(i - 1 + n) % n] - 1) / p; n     ans += (2000.0 * (c * l2 + cp * l1 - (c * cp))) / (l1 * l2); n   } n   printf( %.10lf , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n using ll = long long int; n using pii = pair<int, int>; n using pll = pair<ll, ll>; n using maxHeap = priority_queue<int>; n using minHeap = priority_queue<int, vector<int>, greater<int>>; n int main() { n   ios::sync_with_stdio(0), cin.tie(0); n   int t, x; n   double d, p; n   cout << fixed << setprecision(10); n   for (cin >> t; t; t--) { n     cin >> x; n     d = x; n     if (x * x - 4 * x < 0) { n       cout <<  N n ; n     } else { n       p = sqrt(d * d - 4 * d); n       cout <<  Y   << (d - p) / 2 <<     << (d + p) / 2 <<   n ; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m; n int const N = 3e5 + 3; n int ans[N]; n int temp[N]; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cin >> n >> m; n   vector<pair<int, pair<int, int>>> edges; n   for (int i = (0); i < (m); ++i) { n     int u, v, w; n     cin >> u >> v >> w; n     --u; n     --v; n     edges.push_back({w, {u, v}}); n   } n   sort(begin(edges), end(edges)); n   fill(ans, ans + n, 0); n   fill(temp, temp + n, 0); n   for (int i = (0); i < (m); ++i) { n     vector<int> cand; n     int cur = edges[i].first; n     while (i < m && edges[i].first == cur) { n       int u = edges[i].second.first; n       int v = edges[i].second.second; n       cand.push_back(v); n       temp[v] = max(temp[v], ans[u] + 1); n       ++i; n     } n     --i; n     for (auto& e : cand) { n       ans[e] = max(temp[e], ans[e]); n       temp[e] = 0; n     } n   } n   cout << *max_element(ans, ans + n) <<   n ; 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 vll = vector<ll>; n struct vec { n   ll x; n   ll y; n }; n void solve() { n   ll t, sx, sy, ex, ey; n   cin >> t >> sx >> sy >> ex >> ey; n   string s; n   cin >> s; n   for (int i = 0, time = 0; i < s.size(); ++i, ++time) { n     vec d = {ex - sx, ey - sy}; n     char quater; n     if (d.x > 0 && d.y > 0) n       quater = 1; n     else if (d.x < 0 && d.y > 0) n       quater = 2; n     else if (d.x < 0 && d.y < 0) n       quater = 3; n     else if (d.x > 0 && d.y < 0) n       quater = 4; n     else if (d.x > 0 && d.y == 0) n       quater = 5; n     else if (d.x == 0 && d.y > 0) n       quater = 6; n     else if (d.x < 0 && d.y == 0) n       quater = 7; n     else n       quater = 8; n     switch (s[i]) { n       case  E : n         if (quater == 1 || quater == 4 || quater == 5) n           sx++; n         else n           continue; n         break; n       case  N : n         if (quater == 1 || quater == 2 || quater == 6) n           sy++; n         else n           continue; n         break; n       case  W : n         if (quater == 2 || quater == 3 || quater == 7) n           sx--; n         else n           continue; n         break; n       case  S : n         if (quater == 3 || quater == 4 || quater == 8) n           sy--; n         else n           continue; n     } n     if (sx == ex && sy == ey) { n       cout << time + 1 <<   n ; n       return; n     } n   } n   cout << -1 <<   n ; n } n int main() { n   ios::sync_with_stdio(false); n   cin.tie(nullptr); n   cout.tie(nullptr); n   int t = 1; n   while (t-- > 0) { n     solve(); n   } n } n


#include <bits/stdc++.h> n using namespace std; n string str; n int main() { n   cin >> str; n   int hun, ten, one; n   if (str.size() == 1) { n     if (str[0] ==  8  || str[0] ==  0 ) { n       cout <<  YES n ; n       cout << str << endl; n     } else n       cout <<  NO n  << endl; n   } else if (str.size() == 2) { n     ten = str[0] -  0 ; n     ten *= 10; n     one = str[1] -  0 ; n     if ((ten + one) % 8 == 0) { n       cout <<  YES n ; n       cout << str << endl; n     } else if ((ten / 10 == 8) || one == 8) { n       cout <<  YES n ; n       cout << 8 << endl; n     } else if (!ten || !one) { n       cout <<  YES n ; n       cout << 0 << endl; n     } else n       cout <<  NO n  << endl; n   } else { n     for (int i = 0; i < str.size(); i++) n       if (str[i] ==  8  || str[i] ==  0 ) { n         cout <<  YES n ; n         cout << str[i] << endl; n         return 0; n       } n     for (int i = 0; i < str.size() - 1; i++) { n       ten = str[i] -  0 ; n       ten *= 10; n       for (int j = i + 1; j < str.size(); j++) { n         one = str[j] -  0 ; n         int temp = ten + one; n         if (temp % 8 == 0) { n           cout <<  YES n ; n           cout << temp << endl; n           return 0; n         } n       } n     } n     for (int i = 0; i < str.size() - 2; i++) { n       hun = str[i] -  0 ; n       hun *= 100; n       for (int j = i + 1; j < str.size() - 1; j++) { n         ten = str[j] -  0 ; n         ten *= 10; n         for (int k = j + 1; k < str.size(); k++) { n           one = str[k] -  0 ; n           int temp = hun + ten + one; n           if (temp % 8 == 0) { n             cout <<  YES n ; n             cout << temp << endl; n             return 0; n           } n         } n       } n     } n     cout <<  NO n  << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n long long n, i, res; n using namespace std; n int main() { n   cin >> n; n   long long a[n]; n   for (i = 0; i < n; i++) cin >> a[i]; n   int l = 0, r = n - 1; n   long long suma = 0, sumb = 0; n   while (l <= r) { n     if (suma < sumb) { n       suma += a[l]; n       l++; n     } else { n       if (suma == sumb) { n         res = max(res, suma); n         suma += a[l]; n         l++; n       } else { n         sumb += a[r]; n         r--; n       } n     } n   } n   if (suma == sumb) res = max(res, suma); n   cout << res; n   return 0; n } n


#include <bits/stdc++.h> n int main() { n   using namespace std; n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   int n, m, k, t; n   cin >> n >> m >> k >> t; n   vector<pair<int, int>> w; n   for (int i = 0, r, c; i < k; i++, w.push_back(make_pair(r, c))) cin >> r >> c; n   sort(w.begin(), w.end()); n   const char* s[] = { Carrots ,  Kiwis ,  Grapes }; n   for (int r, c; t--;) { n     cin >> r >> c; n     auto v = make_pair(r, c); n     auto vi = upper_bound(w.begin(), w.end(), v) - w.begin(); n     int i = (r - 1) * m + c - 1; n     if (vi && w[vi - 1] == v) n       cout <<  Waste  <<   n ; n     else n       cout << s[(i - vi) % 3] <<   n ; n   } n } n


#include <bits/stdc++.h> n int mut(int x) { n   if (x < 0) n     return -x; n   else n     return x; n } n int d[1000001][2]; n int d1[1000001][2]; n int main(void) { n   int ax; n   int ay; n   int bx; n   int by; n   int cx; n   int cy; n   int c = 0; n   int min = 99999; n   scanf( %d%d%d%d%d%d , &ax, &ay, &bx, &by, &cx, &cy); n   int x = ax; n   int y = ay; n   d[c][0] = ax; n   d[c][1] = ay; n   c++; n   if (bx >= ax && by >= ay) { n     for (int i = y; i <= by; i++) { n       for (int j = x; j <= bx; j++) { n         if (min > mut(i - cy) + mut(j - cx)) { n           min = mut(i - cy) + mut(j - cx); n           d1[min][0] = j; n           d1[min][1] = i; n         } n       } n     } n   } else if (bx <= ax && by <= ay) { n     for (int i = y; i >= by; i--) { n       for (int j = x; j >= bx; j--) { n         if (min > mut(i - cy) + mut(j - cx)) { n           min = mut(i - cy) + mut(j - cx); n           d1[min][0] = j; n           d1[min][1] = i; n         } n       } n     } n   } else if (bx >= ax && by <= ay) { n     for (int i = y; i >= by; i--) { n       for (int j = x; j <= bx; j++) { n         if (min > mut(i - cy) + mut(j - cx)) { n           min = mut(i - cy) + mut(j - cx); n           d1[min][0] = j; n           d1[min][1] = i; n         } n       } n     } n   } else if (bx <= ax && by >= ay) { n     for (int i = y; i <= by; i++) { n       for (int j = x; j >= bx; j--) { n         if (min > mut(i - cy) + mut(j - cx)) { n           min = mut(i - cy) + mut(j - cx); n           d1[min][0] = j; n           d1[min][1] = i; n         } n       } n     } n   } n   int x1 = d1[min][0]; n   int y1 = d1[min][1]; n   if (ax < x1) { n     while (x < x1) { n       x++; n       d[c][0] = x; n       d[c][1] = y; n       c++; n     } n   } else if (ax > x1) { n     while (x > x1) { n       x--; n       d[c][0] = x; n       d[c][1] = y; n       c++; n     } n   } n   if (ay > y1) { n     while (y > y1) { n       y--; n       d[c][0] = x; n       d[c][1] = y; n       c++; n     } n   } else if (ay < y1) { n     while (y < y1) { n       y++; n       d[c][0] = x; n       d[c][1] = y; n       c++; n     } n   } n   x = x1; n   y = y1; n   if (x1 < bx) { n     while (x < bx) { n       x++; n       d[c][0] = x; n       d[c][1] = y; n       c++; n     } n   } else if (x1 > bx) { n     while (x > bx) { n       x--; n       d[c][0] = x; n       d[c][1] = y; n       c++; n     } n   } n   if (y1 > by) { n     while (y > by) { n       y--; n       d[c][0] = x; n       d[c][1] = y; n       c++; n     } n   } else if (y1 < by) { n     while (y < by) { n       y++; n       d[c][0] = x; n       d[c][1] = y; n       c++; n     } n   } n   x = x1; n   y = y1; n   if (x1 < cx) { n     while (x1 < cx) { n       x1++; n       d[c][0] = x1; n       d[c][1] = y1; n       c++; n     } n   } else if (x1 > cx) { n     while (x1 > cx) { n       x1--; n       d[c][0] = x1; n       d[c][1] = y1; n       c++; n     } n   } n   if (y1 < cy) { n     while (y1 < cy) { n       y1++; n       d[c][0] = x1; n       d[c][1] = y1; n       c++; n     } n   } else if (y1 > cy) { n     while (y1 > cy) { n       y1--; n       d[c][0] = x1; n       d[c][1] = y1; n       c++; n     } n   } n   printf( %d n , c); n   for (int i = 0; i < c; i++) { n     printf( %d %d n , d[i][0], d[i][1]); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int q; n int h, w, k; n int a[600][600]; n int dist[17][17]; n int dp[17][65539]; n int from[17][65539]; n int pos[17]; n int getdown(int x, int y) { n   int sum = 0; n   for (int i = x - 4; i <= x; i++) { n     for (int j = y - 2; j <= y + 2; j++) { n       sum += a[i][j]; n     } n   } n   return sum; n } n int getup(int x, int y) { n   int sum = 0; n   for (int i = x; i <= x + 4; i++) { n     for (int j = y - 2; j <= y + 2; j++) { n       sum += a[i][j]; n     } n   } n   return sum; n } n int main() { n   cin >> q; n   for (int ii = 0; ii < q; ii++) { n     scanf( %d %d %d , &h, &w, &k); n     for (int i = 0; i < h; i++) { n       for (int j = 0; j < w; j++) { n         scanf( %d , &a[i][j]); n       } n     } n     for (int i = 0; i < k; i++) { n       for (int j = 0; j < k; j++) { n         if (i != j) { n           dist[i][j] = 0; n           for (int kk = 5; kk < w - 5; kk++) { n             dist[i][j] += abs(getdown((i + 1) * (h / k) - 1, kk) - n                               getup(j * (h / k), kk)); n           } n         } n       } n     } n     for (int mask = 0; mask < (1 << k); mask++) { n       for (int who = 0; who < k; who++) { n         dp[who][mask] = 1000000000; n       } n     } n     for (int mask = 0; mask < (1 << k); mask++) { n       for (int who = 0; who < k; who++) { n         if (!((1 << who) & mask)) { n           continue; n         } n         if (mask == (1 << who)) { n           dp[who][mask] = 0; n         } n         if (dp[who][mask] == 1000000000) { n           continue; n         } n         for (int next = 0; next < k; next++) { n           if (!((1 << next) & mask)) { n             int NewMask = mask | (1 << next); n             if (dp[next][NewMask] > dp[who][mask] + dist[who][next]) { n               dp[next][NewMask] = dp[who][mask] + dist[who][next]; n               from[next][NewMask] = who; n             } n           } n         } n       } n     } n     int ans = 1000000000; n     int i, j; n     for (int who = 0; who < k; who++) { n       ans = min(ans, dp[who][(1 << k) - 1]); n       if (ans == dp[who][(1 << k) - 1]) { n         i = who; n         j = (1 << k) - 1; n       } n     } n     vector<int> anss; n     while (j != 0) { n       int ni = from[i][j]; n       anss.push_back(i + 1); n       j ^= (1 << i); n       i = ni; n     } n     reverse(anss.begin(), anss.end()); n     for (int i = 0; i < (int)anss.size(); i++) { n       pos[anss[i]] = i + 1; n     } n     for (int i = 1; i <= k; i++) { n       printf( %d  , pos[i]); n     } n     printf(  n ); n   } n   return 0; n } n


#include <bits/stdc++.h> n #define fast ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL) n #define int long long int n #define double long double n #define endl   n  n const int mod = 1000000007; n using namespace std; n  n int modExpo(int x , int n) n { n  int ans = 1; n  while (n > 0) { n   if ( n & 1 == 1 ) ans = ( ans * x ) % mod; n   x = ( x * x ) % mod; n = n / 2; n  } n  return ans; n } n  n  n  n  n  n  n  n  n  n  n void solve() n { n  vector<int> v(4); n  for(int i=0;i<4;i++) n   cin>>v[i]; n  n  int x = max(v[0] , v[1]); n  int y = max(v[2] , v[3]); n  n  if(x < v[2] && x < v[3]) n  { n   cout<< NO n ; n   return; n  }  n  n  if(y < v[0] && y < v[1]) n  { n   cout<< NO n ; n   return; n  }  n  n  cout<< YES n ; n  // cout<<x<<y; n } n  n  n  n  n  n  n  n  n  n  n int32_t main() n { n #ifndef ONLINE_JUDGE n  freopen( input.txt ,  r , stdin); n  freopen( output.txt ,  w , stdout); n #endif n  n  fast; n  int tt; n  cin >> tt; n  while (tt--) n  { n   solve(); n  } n  return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 2 * 100 * 1000 + 10; n string s, t; n int d[N]; n int LastSeen[N]; n void input() { n   cin >> s >> t; n   return; n } n void PrePros() { n   int keep = (int)(t.size()) - 1, pnt = (int)(s.size()) - 1; n   while (keep >= 0) { n     if (s[pnt] == t[keep]) { n       LastSeen[keep] = pnt; n       keep--; n     } n     pnt--; n   } n   LastSeen[t.size()] = s.size(); n   t.push_back( * ); n   return; n } n void solve() { n   PrePros(); n   int lastfound = 0; n   for (int i = 0; i < s.size(); i++) { n     d[i] = LastSeen[lastfound] - i; n     if (s[i] == t[lastfound]) lastfound++; n   } n   return; n } n void output() { n   int ans = 0; n   for (int i = 0; i < s.size(); i++) ans = max(ans, d[i]); n   cout << ans << endl; n   return; n } n int main() { n   input(); n   solve(); n   output(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void fastio() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n } n void input() {} n int main() { n   fastio(); n   input(); n   long long int n; n   cin >> n; n   long long int sum = -1; n   map<long long int, long long int> make_pair; n   for (long long int i = 0; i < n; i++) { n     long long int x; n     cin >> x; n     make_pair[x - i] += x; n     sum = max(sum, make_pair[x - i]); n   } n   cout << sum <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n double a, b, c, d; n int main() { n   char st =  e ; n   cin >> a >> b >> c >> d; n   if (a / b == b / c && b / c == c / d) st =  g ; n   if (a - b == b - c && b - c == c - d) st =  a ; n   if (st ==  e ) n     cout << 42 << endl; n   else if (st ==  g ) { n     int d1 = d, b1 = b, a1 = a; n     if (d * b / a == d1 * b1 / a1) n       cout << d * b / a << endl; n     else n       cout << 42 << endl; n   } else n     cout << d + b - a << endl; n } n


#include <bits/stdc++.h> n using namespace std; n using ll = long long; n using db = double; n using pii = pair<int, int>; n using vi = vector<int>; n const int N = 1e5 + 100; n int n, d[N], f[N][2], ans; n vi G[N]; n void dfs(int x, int fz) { n   f[x][0] = 0, f[x][1] = 1; n   for (int y : G[x]) n     if (y != fz) { n       dfs(y, x); n       ans = max({ans, f[x][0] + f[y][0], f[x][0] + f[y][1], f[x][1] + f[y][0]}); n       f[x][0] = max({f[x][0], f[y][0] + d[x] - 2, f[y][1] + d[x] - 2}); n       f[x][1] = max({f[x][1], f[y][0] + 1}); n     } n   ans = max(ans, f[x][1]); n } n signed main() { n   scanf( %d , &n); n   for (int i = 1; i < n; i++) { n     int x, y; n     scanf( %d %d , &x, &y); n     G[x].push_back(y), G[y].push_back(x); n     d[x]++, d[y]++; n   } n   dfs(1, 0); n   printf( %d n , ans); n   fprintf(stderr,  time=%.4f n , (db)clock() / CLOCKS_PER_SEC); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int32_t main() { n   long long r, g, b; n   cin >> r >> g >> b; n   long long ans = 0; n   long long tr, tg, tb; n   for (long long i = 0; i <= min({r, g, b, 2LL}); i++) { n     long long t = i; n     tr = r; n     tg = g; n     tb = b; n     tr -= i; n     tg -= i; n     tb -= i; n     t += (tr / 3 + tg / 3 + tb / 3); n     ans = max(ans, t); n   } n   cout << ans; n } n


#include <bits/stdc++.h> n using namespace std; n template <class T> n bool chmax(T& a, const T& b) { n   if (a < b) { n     a = b; n     return 1; n   } n   return 0; n } n template <class T> n bool chmin(T& a, const T& b) { n   if (b < a) { n     a = b; n     return 1; n   } n   return 0; n } n const long long int MOD = 1e9 + 7, INF = 1e18; n long long int dx[8] = {0, -1, 1, 0, 1, -1, 1, -1}, n               dy[8] = {1, 0, 0, -1, -1, -1, 1, 1}; n struct edge { n   long long int cost; n   long long int u, v; n }; n long long int N, M; n long long int mp[200][200]; n long long int delta[200][200]; n int main() { n   cin >> N >> M; n   for (long long int i = (0), i_end_ = (N); i < i_end_; i++) { n     for (long long int j = (0), j_end_ = (M); j < j_end_; j++) { n       cin >> mp[i][j]; n     } n   } n   for (long long int i = (0), i_end_ = (N); i < i_end_; i++) { n     for (long long int j = (0), j_end_ = (M); j < j_end_; j++) { n       delta[i][j] = mp[i][j]; n     } n   } n   bool flag = true; n   vector<pair<long long int, long long int> > pos; n   vector<long long int> _row_ans; n   vector<long long int> _column_ans; n   _row_ans.push_back(0); n   if (mp[0][0] == 0) n     _column_ans.push_back(0); n   else n     _column_ans.push_back(1); n   for (long long int i = (0), i_end_ = (M - 1); i < i_end_; i++) { n     _column_ans.push_back(mp[0][i] ^ mp[0][i + 1] ^ _column_ans.back()); n   } n   for (long long int i = (0), i_end_ = (N - 1); i < i_end_; i++) { n     _row_ans.push_back(mp[i][0] ^ mp[i + 1][0] ^ _row_ans.back()); n     for (long long int j = (0), j_end_ = (M - 1); j < j_end_; j++) { n       if ((mp[0][j] ^ mp[0][j + 1]) != (mp[i + 1][j] ^ mp[i + 1][j + 1])) { n         pos.push_back({i + 1, j + 1}); n         flag = false; n       } n     } n   } n   if (flag) { n     cout <<  YES  <<   n ; n     for (long long int i = (0), i_end_ = (N); i < i_end_; i++) n       cout << _row_ans[i]; n     cout <<   n ; n     for (long long int i = (0), i_end_ = (M); i < i_end_; i++) n       cout << _column_ans[i]; n     cout <<   n ; n     return 0; n   } n   if (((long long int)(pos).size()) != 1) { n     bool _flag = true; n     for (long long int i = (0), i_end_ = (((long long int)(pos).size())); n          i < i_end_; i++) { n       if (pos[i].first == 0) _flag = false; n       if (pos[0].second != pos[i].second) _flag = false; n     } n     for (long long int i = (0), i_end_ = (((long long int)(pos).size()) - 1); n          i < i_end_; i++) { n       if (pos[i].first + 1 != pos[i + 1].first) _flag = false; n     } n     if (((long long int)(pos).size()) != N - 1) _flag = false; n     if (_flag) { n       long long int r = pos[0].second; n       pos.clear(); n       pos.push_back({0, r}); n     } else { n       cout <<  NO  <<   n ; n       return 0; n     } n   } n   for (long long int i = (0), i_end_ = (N); i < i_end_; i++) { n     for (long long int j = (0), j_end_ = (M); j < j_end_; j++) { n       if (i * M + j >= pos[0].first * M + pos[0].second) mp[i][j] ^= 1; n     } n   } n   _row_ans.clear(); n   _column_ans.clear(); n   _row_ans.push_back(0); n   if (mp[0][0] == 0) n     _column_ans.push_back(0); n   else n     _column_ans.push_back(1); n   for (long long int i = (0), i_end_ = (M - 1); i < i_end_; i++) { n     _column_ans.push_back(mp[0][i] ^ mp[0][i + 1] ^ _column_ans.back()); n   } n   for (long long int i = (0), i_end_ = (N - 1); i < i_end_; i++) { n     _row_ans.push_back(mp[i][0] ^ mp[i + 1][0] ^ _row_ans.back()); n   } n   cout <<  YES  <<   n ; n   for (long long int i = (0), i_end_ = (N); i < i_end_; i++) n     cout << _row_ans[i]; n   cout <<   n ; n   for (long long int i = (0), i_end_ = (M); i < i_end_; i++) n     cout << _column_ans[i]; n   cout <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long MOD = (long long)(1e9 + 7); n const int inf = (int)INFINITY; n const long long INF = (long long)INFINITY; n const int MAX = (int)(2e5 + 5); n const int maxn = 2e5 + 5; n int n, m, k, p[maxn], path[maxn]; n vector<int> in[maxn], out[maxn]; n int maxa, mina; n set<pair<long long, long long> > st; n void dijkstra() { n   while (st.size()) { n     pair<long long, long long> x = *st.begin(); n     st.erase(x); n     int i = x.second; n     for (auto A : in[i]) { n       if (path[i] + 1 < path[A]) { n         st.erase(pair<long long, long long>(path[A], A)); n         path[A] = path[i] + 1; n         st.insert(pair<long long, long long>(path[A], A)); n       } n     } n   } n } n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   ; n   cin >> n >> m; n   for (long long i = 0; i < (m); ++i) { n     int u, v; n     cin >> u >> v; n     out[u].push_back(v); n     in[v].push_back(u); n   } n   cin >> k; n   for (int i = 1; i <= k; ++i) cin >> p[i]; n   memset(path, 63, sizeof path); n   path[p[k]] = 0; n   st.insert(pair<long long, long long>(0, p[k])); n   dijkstra(); n   for (int i = 2; i <= k; ++i) { n     if (path[p[i]] != path[p[i - 1]] - 1) { n       ++mina; n       ++maxa; n     } else { n       int e = 0; n       for (auto A : out[p[i - 1]]) n         if (A != p[i] && path[A] == path[p[i]]) e = 1; n       maxa += e; n     } n   } n   cout << mina <<     << maxa; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n char s1[100100], s2[100100]; n long long ini; n long long mod(long long a) { n   return ((a % 1000000007LL) + 1000000007LL) % 1000000007LL; n } n long long func(char* s, int t, bool som) { n   long long r = 0; n   long long ul = 0; n   long long soma = 0, ls = ini; n   long long qt = 0LL; n   bool tem = false; n   long long sum, lst, val, bas, qtd; n   sum = lst = ini; n   val = 0; n   bas = 3LL; n   qtd = 1LL; n   for (int i = t - 1, j = 0; i >= 0; i--, j++) { n     ul = (ul * 10 + s[j] -  0 ) % 1000000007LL; n     long long bas2 = (bas * bas) % 1000000007LL; n     if (s[i] ==  7 ) { n       if (qt > 0) n         r = (r + ((qt - 1) * bas2) % 1000000007LL + n              (bas * (2 * soma - ls - ini)) % 1000000007LL + n              (lst * (ini + bas)) % 1000000007LL) % n             1000000007LL; n       r = (r + val) % 1000000007LL; n       soma = (soma + sum + qt * bas) % 1000000007LL; n       qt = (qt + qtd) % 1000000007LL; n       if (tem) ls = (ls + bas) % 1000000007LL; n       tem = true; n     } else if (!tem) n       ls = (ls + bas) % 1000000007LL; n     val = (2 * val + (lst * (bas + ini)) % 1000000007LL + n            (bas2 * (qtd - 1)) % 1000000007LL + n            (bas * (2 * sum - lst - ini)) % 1000000007LL) % n           1000000007LL; n     sum = (2 * sum + qtd * bas) % 1000000007LL; n     lst = (lst + bas) % 1000000007LL; n     bas = (bas * 10) % 1000000007LL; n     qtd = (qtd * 2) % 1000000007LL; n   } n   if (tem) return (r + ul * ls) % 1000000007LL; n   return r; n } n int main() { n   int tam; n   scanf( %s %s , s1, s2); n   tam = strlen(s1); n   ini = 0; n   for (int i = 0; i < tam; i++) ini = (ini * 10 + 4LL) % 1000000007LL; n   cout << mod(func(s2, tam, true) - func(s1, tam, false)) << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long double eps = -1e12; n inline bool eq(const long double &x, const long double &y) { n   return abs(x - y) < eps || abs(x - y) < eps * abs(x); n } n int main() { n   long double x, y, z; n   cin >> x >> y >> z; n   string expression[12] = { x^y^z ,  x^z^y ,  (x^y)^z ,  (x^z)^y , n                             y^x^z ,  y^z^x ,  (y^x)^z ,  (y^z)^x , n                             z^x^y ,  z^y^x ,  (z^x)^y ,  (z^y)^x }; n   vector<long double> ans(12, 0); n   long double res = -1000; n   long double logx = log(x); n   long double logy = log(y); n   long double logz = log(z); n   ans[0] = pow(y, z) * logx; n   ans[1] = pow(z, y) * logx; n   ans[2] = logx * y * z; n   ans[3] = ans[2]; n   ans[4] = pow(x, z) * logy; n   ans[5] = pow(z, x) * logy; n   ans[6] = logy * x * z; n   ans[7] = ans[6]; n   ans[8] = pow(x, y) * logz; n   ans[9] = pow(y, x) * logz; n   ans[10] = logz * y * x; n   ans[11] = ans[10]; n   int index = 0; n   for (int i = 0; i < 12; ++i) { n     if (ans[i] > res && !eq(ans[i], res)) { n       res = ans[i]; n       index = i; n     } n   } n   cout << expression[index] << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct Point { n   double X, Y, Z; n   void Get() { scanf( %lf%lf%lf , &X, &Y, &Z); } n }; n Point operator-(Point A, Point B) { n   return (Point){A.X - B.X, A.Y - B.Y, A.Z - B.Z}; n } n double operator*(Point A, Point B) { return A.X * B.X + A.Y * B.Y + A.Z * B.Z; } n const double inf = 1e9; n const double eps = 1e-6; n double Solve(Point A, Point V, double R) { n   double a = V * V, b = A * V * 2, c = A * A - R * R; n   double Delta = b * b - 4 * a * c; n   if (Delta < -eps) return inf; n   Delta = sqrt(fabs(Delta)); n   double T0 = (-b + Delta) / (2 * a), T1 = (-b - Delta) / (2 * a); n   return (T0 < 0) ? inf : ((T1 < 0) ? T0 : T1); n } n int main() { n   Point O, V; n   O.Get(); n   V.Get(); n   double R, Ans = inf; n   scanf( %lf , &R); n   int N; n   scanf( %d , &N); n   while (N--) { n     Point _O; n     _O.Get(); n     double r; n     scanf( %lf , &r); n     Ans = min(Ans, Solve(O - _O, V, R + r)); n     int M; n     scanf( %d , &M); n     while (M--) { n       Point Delta; n       Delta.Get(); n       Ans = min(Ans, Solve(O - _O - Delta, V, R)); n     } n   } n   if (Ans == inf) n     printf( -1 n ); n   else n     printf( %0.10lf n , Ans); n   return 0; n } n


#include <bits/stdc++.h> n struct Candidate { n   std::vector<int> d; n   Candidate(std::vector<int> d_) { n     d = d_; n     std::sort(d.begin(), d.end()); n     std::reverse(d.begin(), d.end()); n   } n   inline bool operator<(const Candidate &rhs) { n     if (d[0] < 0 || rhs.d[0] < 0) return d[0] < rhs.d[0]; n     for (int i = 0; i < static_cast<int>(d.size()); ++i) n       if (d[i] != rhs.d[i]) return d[i] < rhs.d[i]; n     return false; n   } n }; n int get_enemy_pos(std::vector<int> locations, bool mate) { n   std::cout << locations[0] + 1 <<     << locations[1] + 1 <<     n             << locations[2] + 1 << std::endl; n   if (!mate) { n     int pos; n     std::cin >> pos; n     return pos - 1; n   } else { n     return -1; n   } n } n std::vector<int> get_dist(std::vector<std::vector<int> > &adj, int src) { n   auto d = std::vector<int>(adj.size(), -1); n   auto q = std::queue<int>(); n   q.push(src); n   d[src] = 0; n   while (!q.empty()) { n     int at = q.front(); n     q.pop(); n     for (auto &next : adj[at]) { n       if (d[next] < 0) { n         d[next] = d[at] + 1; n         q.push(next); n       } n     } n   } n   return d; n } n int main() { n   int nodes, edges; n   std::cin >> nodes >> edges; n   auto adj = std::vector<std::vector<int> >(nodes, std::vector<int>()); n   for (int edge = 0; edge < edges; ++edge) { n     int src, dst; n     std::cin >> src >> dst; n     adj[src - 1].push_back(dst - 1); n     adj[dst - 1].push_back(src - 1); n   } n   srand(time(NULL)); n   auto dists = std::vector<std::vector<int> >(static_cast<int>(adj.size()), n                                               std::vector<int>()); n   int best_dist = nodes + 1; n   auto best_locs = std::vector<int>(3, -1); n   for (int iter = 0; iter < 5000; ++iter) { n     auto locs = std::vector<int>(3); n     for (int i = 0; i < static_cast<int>(locs.size()); ++i) { n       locs[i] = -1; n       bool ok = true; n       do { n         ok = true; n         locs[i] = rand() % nodes; n         for (int j = 0; j < i; ++j) { n           if (locs[i] == locs[j]) ok = false; n         } n       } while (!ok); n       if (dists[locs[i]].size() == 0) { n         dists[locs[i]] = get_dist(adj, locs[i]); n       } n     } n     int cand_dist = 1; n     for (int i = 0; i < nodes; ++i) { n       cand_dist = std::max( n           cand_dist, std::min(dists[locs[0]][i], n                               std::min(dists[locs[1]][i], dists[locs[2]][i]))); n     } n     if (cand_dist < best_dist) { n       best_dist = cand_dist; n       best_locs = locs; n     } n   } n   auto locs = best_locs; n   auto enemy_pos = get_enemy_pos(locs, false); n   while (enemy_pos >= 0) { n     if (enemy_pos == locs[0] || enemy_pos == locs[1] || enemy_pos == locs[2]) { n       break; n     } n     auto targets = std::vector<int>(); n     targets.push_back(enemy_pos); n     for (int repeats = 0; repeats < 0; ++repeats) { n       auto ntargets = std::vector<int>(); n       for (auto &t : targets) n         for (auto &x : adj[t]) { n           ntargets.push_back(x); n         } n       for (auto &nt : ntargets) targets.push_back(nt); n       std::sort(targets.begin(), targets.end()); n       targets.resize(std::unique(targets.begin(), targets.end()) - n                      targets.begin()); n     } n     for (auto &x : targets) { n       if (dists[x].size() == 0) { n         dists[x] = get_dist(adj, x); n       } n     } n     auto d = std::vector<int>(static_cast<int>(locs.size())); n     auto best = n         Candidate(std::vector<int>(static_cast<int>(locs.size()), nodes + 1)); n     static int best_locs[] = {-1, -1, -1}; n     static int by[3]; n     static int choice[3]; n     for (int i = 0; i < 3; ++i) { n       std::random_shuffle(adj[locs[i]].begin(), adj[locs[i]].end()); n     } n     for (choice[0] = 0; choice[0] <= adj[locs[0]].size(); ++choice[0]) n       for (choice[1] = 0; choice[1] <= adj[locs[1]].size(); ++choice[1]) n         for (choice[2] = 0; choice[2] <= adj[locs[2]].size(); ++choice[2]) { n           bool mate = false; n           for (int i = 0; i < 3; ++i) { n             by[i] = (choice[i] == 0 ? locs[i] : adj[locs[i]][choice[i] - 1]); n             if (by[i] == enemy_pos) mate = true; n           } n           if (by[0] == by[1] || by[1] == by[2] || by[0] == by[2]) { n             continue; n           } n           for (int j = 0; j < 3; ++j) d[j] = dists[targets[0]][by[j]]; n           auto cand = Candidate(d); n           if (mate) { n             cand.d[0] = -1; n           } n           if (cand < best) { n             best = cand; n             for (int i = 0; i < 3; ++i) { n               best_locs[i] = by[i]; n             } n           } n         } n     locs = std::vector<int>(best_locs, best_locs + 3); n     enemy_pos = get_enemy_pos(locs, best.d[0] < 0); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n; n pair<pair<int, int>, int> p[200005]; n int t[200005], ans[2000005], a[200005]; n void san() { n   for (int i = 1; i <= n; ++i) t[i] = p[i].first.first; n   sort(t + 1, t + 1 + n); n   for (int i = 1; i <= n; ++i) n     p[i].first.first = lower_bound(t + 1, t + 1 + n, p[i].first.first) - t; n } n bool cmp(pair<pair<int, int>, int> a, pair<pair<int, int>, int> b) { n   return a.first.second < b.first.second; n } n void add(int x) { n   while (x <= n) { n     a[x]++; n     x += x & (-x); n   } n } n int getSum(int x) { n   int res = 0; n   while (x >= 1) { n     res += a[x]; n     x -= x & (-x); n   } n   return res; n } n int main() { n   scanf( %d , &n); n   for (int i = 1; i <= n; ++i) { n     scanf( %d%d , &p[i].first.first, &p[i].first.second); n     p[i].second = i; n   } n   san(); n   sort(p + 1, p + 1 + n, cmp); n   for (int i = 1; i <= n; ++i) { n     ans[p[i].second] = i - 1 - getSum(p[i].first.first); n     add(p[i].first.first); n   } n   for (int i = 1; i <= n; ++i) printf( %d n , ans[i]); n   return 0; n } n