#include <bits/stdc++.h> n using namespace std; n const double PI = acos(-1); n template <typename T> n T getint() { n   T val = 0; n   char c; n   while ((c = getchar()) && !(c >=  0  && c <=  9 )) n     ; n   do { n     val = (val * 10) + c -  0 ; n   } while ((c = getchar()) && (c >=  0  && c <=  9 )); n   return val; n } n const int maxn = 100001, hgt = 17; n int n, m; n vector<int> adj[maxn]; n int par[maxn], b[maxn], lvl[maxn]; n int dp[maxn][hgt]; n void dfs_lca(int x, int p = -1, int lev = 0) { n   b[x] = 1; n   par[x] = p; n   lvl[x] = lev; n   for (auto i : adj[x]) { n     if (!b[i]) { n       dfs_lca(i, x, lev + 1); n     } n   } n   b[x] = 2; n } n void init() { n   memset(dp, -1, sizeof dp); n   for (int i = 1; i <= n; ++i) { n     dp[i][0] = par[i]; n   } n   for (int j = 1; j < hgt; ++j) { n     for (int i = 1; i <= n; ++i) { n       if (dp[i][j - 1] != -1) { n         dp[i][j] = dp[dp[i][j - 1]][j - 1]; n       } n     } n   } n } n int lca(int p, int q) { n   if (lvl[p] > lvl[q]) swap(p, q); n   for (int i = hgt - 1; i >= 0; i--) { n     if (dp[q][i] != -1 && lvl[p] <= lvl[dp[q][i]]) q = dp[q][i]; n   } n   if (p == q) return p; n   for (int i = hgt - 1; i >= 0; i--) { n     if (dp[q][i] != -1 && dp[q][i] != dp[p][i]) { n       p = dp[p][i]; n       q = dp[q][i]; n     } n   } n   return dp[p][0]; n } n int kth(int p, int k) { n   for (int i = 0; i < hgt; ++i) { n     if (k & (1 << i)) { n       p = dp[p][i]; n     } n   } n   return p; n } n int dist(int a, int b) { n   int l = lca(a, b); n   return lvl[a] + lvl[b] - 2 * lvl[l]; n } n int banned[maxn]; n int b0[maxn], sz[maxn]; n void dfs0(int x) { n   b0[x] = 1; n   sz[x] = 1; n   for (auto i : adj[x]) { n     if (b0[i] || banned[i]) continue; n     dfs0(i); n     sz[x] += sz[i]; n   } n   b0[x] = 2; n } n int dfs1(int x, int p, int nn) { n   int ind = -1; n   for (auto i : adj[x]) { n     if (banned[i] || i == p) continue; n     if (sz[i] > nn / 2) ind = i; n   } n   if (ind != -1) return dfs1(ind, x, nn); n   return x; n } n vector<int> cadj[maxn]; n int cpar[maxn]; n int decompose(int x = 1) { n   memset(b0, 0, sizeof b0); n   dfs0(x); n   int centroid = dfs1(x, -1, sz[x]); n   banned[centroid] = 1; n   for (auto i : adj[centroid]) { n     if (!banned[i]) { n       int child = decompose(i); n       cadj[centroid].push_back(child); n       cpar[child] = centroid; n     } n   } n   return centroid; n } n int main() { n   n = getint<int>(); n   m = getint<int>(); n   for (int i = 1; i < n; ++i) { n     int a, b; n     a = getint<int>(); n     b = getint<int>(); n     adj[a].push_back(b); n     adj[b].push_back(a); n   } n   dfs_lca(1); n   init(); n   int root = decompose(); n   vector<int> ans(n + 1, 1e9); n   for (int i = -1; i < m; ++i) { n     int typ, v; n     if (i >= 0) { n       typ = getint<int>(); n       v = getint<int>(); n     } else { n       typ = 1; n       v = 1; n     } n     if (typ == 1) { n       int akt = v; n       while (akt) { n         ans[akt] = min(ans[akt], dist(akt, v)); n         akt = cpar[akt]; n       } n     } else { n       int res = 1e9; n       int akt = v; n       while (akt) { n         res = min(res, ans[akt] + dist(akt, v)); n         akt = cpar[akt]; n       } n       printf( %d n , res); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n pair<int, int> volc[100000]; n int main() { n   int n, m; n   scanf( %d , &n); n   scanf( %d , &m); n   for (int i = 0; i < m; i++) { n     scanf( %d , &volc[i].first); n     scanf( %d , &volc[i].second); n   } n   sort(volc, volc + m); n   int x = 0; n   vector<pair<int, int> > ints; n   ints.push_back(make_pair(1, 1)); n   int i = 0; n   while (i != m) { n     if (ints.empty()) break; n     if (volc[i].first > x + 1) { n       int f = ints[0].first; n       ints.clear(); n       ints.push_back(make_pair(f, n)); n     } n     x = volc[i].first; n     vector<pair<int, int> > ys; n     int last = 0; n     while (x == volc[i].first) { n       if ((volc[i].second - last - 1) > 0) n         ys.push_back(make_pair(last + 1, volc[i].second - 1)); n       last = volc[i].second; n       i++; n     } n     if ((n + 1 - last - 1) > 0) ys.push_back(make_pair(last + 1, n)); n     vector<pair<int, int> > nints; n     int k = 0; n     for (int j = 0; j < (int)(ints.size()); j++) { n       while (k < (int)(ys.size()) && ys[k].second < ints[j].first) k++; n       while (k < (int)(ys.size()) && ys[k].first <= ints[j].second) { n         nints.push_back( n             make_pair(max(ys[k].first, ints[j].first), ys[k].second)); n         k++; n       } n     } n     ints = nints; n   } n   if (ints.empty()) { n     cout << -1 << endl; n     return 0; n   } n   if (x != n || ints[(int)(ints.size()) - 1].second == n) n     cout << 2 * (n - 1) << endl; n   else n     cout << -1 << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long int mod = 1000000007; n int main() { n   ios::sync_with_stdio(0); n   int n; n   cin >> n; n   long long int a[10000], sum[10000], all[10000], m[10000]; n   memset(all, 0, sizeof(all)); n   for (int i = 2; i < pow(2, n + 1); i++) { n     cin >> a[i]; n   } n   m[1] = sum[1] = 0; n   for (int i = 2; i < pow(2, n + 1); i++) { n     m[i] = sum[i] = sum[i / 2] + a[i]; n   } n   long long int fin[10000]; n   fin[1] = sum[(int)pow(2, n)]; n   for (int i = pow(2, n); i < pow(2, n + 1); i++) { n     fin[1] = max(fin[1], sum[i]); n   } n   for (int i = pow(2, n) - 1; i >= 1; i--) { n     m[i] = max(m[2 * i], m[2 * i + 1]); n   } n   long long int ans = 0; n   for (int i = 1; i < pow(2, n); i++) { n     all[2 * i] = all[2 * i + 1] = all[i]; n     if (m[2 * i] + all[i] < fin[1]) { n       ans += (fin[1] - all[i] - m[2 * i]); n       all[2 * i] = all[i] + fin[1] - all[i] - m[2 * i]; n     } n     if (m[2 * i + 1] + all[i] < fin[1]) { n       ans += (fin[1] - all[i] - m[2 * i + 1]); n       all[2 * i + 1] = all[i] + fin[1] - all[i] - m[2 * i + 1]; n     } n     cerr <<  i  n          <<  :   << i <<   |   n          <<  ans  n          <<  :   << ans << endl; n     ; n   } n   cout << ans << endl; n } n


#include <bits/stdc++.h> n using namespace std; n const long double PI = acos(-1); n vector<complex<double> > fft(const vector<complex<double> >& as) { n   int n = (int)as.size(); n   int k = 0; n   while ((1 << k) < n) k++; n   vector<int> r(n); n   r[0] = 0; n   int h = -1; n   for (int i = 1; i < n; i++) { n     if ((i & (i - 1)) == 0) h++; n     r[i] = r[i ^ (1 << h)]; n     r[i] |= (1 << (k - h - 1)); n   } n   vector<complex<double> > root(n); n   for (int i = 0; i < n; i++) { n     double ang = 2.0 * PI * i / n; n     root[i] = complex<double>(cos(ang), sin(ang)); n   } n   vector<complex<double> > cur(n); n   for (int i = 0; i < n; i++) cur[i] = as[r[i]]; n   for (int len = 1; len < n; len <<= 1) { n     vector<complex<double> > ncur(n); n     int step = n / (len << 1); n     for (int pdest = 0; pdest < n;) { n       for (int i = 0; i < len; i++) { n         complex<double> val = root[i * step] * cur[pdest + len]; n         ncur[pdest] = cur[pdest] + val; n         ncur[pdest + len] = cur[pdest] - val; n         pdest++; n       } n       pdest += len; n     } n     cur.swap(ncur); n   } n   return cur; n } n vector<complex<double> > inv_fft(const vector<complex<double> >& fa) { n   vector<complex<double> > res = fft(fa); n   int nn = res.size(); n   for (int i = 0; i < nn; i++) { n     res[i] /= nn; n   } n   reverse(res.begin() + 1, res.end()); n   return res; n } n vector<complex<double> > multiply(vector<complex<double> >& a, n                                   vector<complex<double> >& b) { n   int len = a.size() + b.size(); n   while (len & (len - 1)) ++len; n   a.resize(len); n   b.resize(len); n   a = fft(a); n   b = fft(b); n   for (int i = 0; i < len; ++i) a[i] *= b[i]; n   a = inv_fft(a); n   return a; n } n void create(string& s, int size, char x, int k, vector<complex<double> >& v) { n   complex<double> zero(0, 0); n   for (int i = 0, b = 1; i < size; i += b, b = 1) { n     if (s[i] == x) { n       b = 0; n       v[i] = 1; n       for (int j = i - 1; j >= 0 && s[j] != x && v[j] == zero && j >= i - k; n            j--) n         v[j] = 1; n       int tmp = i; n       for (++i; i < size && s[i] != x && i <= tmp + k && v[i] == zero; i++) n         v[i] = 1; n     } n   } n } n int main() { n   ios::sync_with_stdio(false), cin.tie(nullptr); n   int k, a = 0, g = 0, c = 0, t = 0, n, m; n   string s, s2; n   cin >> n >> m >> k >> s >> s2; n   for (int i = 0; i < m / 2; i++) { n     swap(s2[i], s2[m - 1 - i]); n   } n   int size = 1; n   while (size < m + n - 1) size *= 2; n   vector<complex<double> > av(n), av2(m); n   create(s, n,  A , k, av); n   create(s2, m,  A , 0, av2); n   av = multiply(av, av2); n   vector<complex<double> > tv(n), tv2(m); n   create(s, n,  T , k, tv); n   create(s2, m,  T , 0, tv2); n   tv = multiply(tv, tv2); n   vector<complex<double> > gv(n), gv2(m); n   create(s, n,  G , k, gv); n   create(s2, m,  G , 0, gv2); n   gv = multiply(gv, gv2); n   vector<complex<double> > cv(n), cv2(m); n   create(s, n,  C , k, cv); n   create(s2, m,  C , 0, cv2); n   cv = multiply(cv, cv2); n   int sum = 0; n   for (int i = 0; i < cv.size(); i++) n     sum += ((int)(cv[i].real() + gv[i].real() + av[i].real() + tv[i].real() + n                   0.3) == m); n   cout << sum; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n char grid[512][512]; n bool used[512][512]; n int n, m; n struct op { n   char type; n   int x, y; n   op() {} n   op(char c, int a, int b) { n     type = c; n     x = a; n     y = b; n   } n }; n vector<op> res; n int dx[4] = {0, -1, 0, 1}; n int dy[4] = {1, 0, -1, 0}; n void go(int x, int y, int level) { n   if (x < 1 || x > n || y > m || y < 1) return; n   if (used[x][y]) return; n   used[x][y] = 1; n   for (int i = 0; i < 4; i++) { n     go(x + dx[i], y + dy[i], level + 1); n   } n   if (level) { n     res.push_back(op( D , x, y)); n     res.push_back(op( R , x, y)); n   } n } n int main() { n   cin >> n >> m; n   for (int i = 1; i <= n; i++) n     for (int j = 1; j <= m; j++) cin >> grid[i][j]; n   for (int i = 1; i <= n; i++) n     for (int j = 1; j <= m; j++) { n       if (grid[i][j] ==  . ) n         res.push_back(op( B , i, j)); n       else n         used[i][j] = 1; n     } n   for (int i = 1; i <= n; i++) n     for (int j = 1; j <= m; j++) { n       if (!used[i][j]) { n         go(i, j, 0); n       } n     } n   int k = res.size(); n   cout << k << endl; n   for (int i = 0; i < k; i++) { n     cout << res[i].type <<     << res[i].x <<     << res[i].y << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long n, a[1005]; n int main() { n   scanf( %I64d , &n); n   for (int i = 1; i <= n; i++) scanf( %I64d , &a[i]); n   sort(a + 1, a + n + 1); n   long long ans = 0; n   for (int i = 2; i <= n; i++) ans += a[i] - a[i - 1] - 1; n   printf( %I64d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   cin >> n; n   int a[n]; n   bool f = false; n   for (int x = 0; x < n; x++) { n     cin >> a[x]; n     if (a[x] == 1) f = true; n   } n   if (f) { n     cout << -1; n   } else n     cout << 1; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxN = 50 + 10; n long long dp[maxN]; n int used[maxN]; n int main() { n   int n; n   long long l; n   cin >> n >> l; n   dp[0] = 1; n   dp[1] = 1; n   for (int i = 2; i < maxN; i++) dp[i] = dp[i - 1] + dp[i - 2]; n   l--; n   for (int i = 0; i < n; i++) { n     int k = n - i; n     if (dp[k - 1] > l) n       cout << i + 1 <<    ; n     else { n       l -= dp[k - 1]; n       cout << i + 2 <<     << i + 1 <<    ; n       i++; n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n void Emsawy() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n } n const double PI = acos(-1.0); n const double eps = 1e-10; n const long long mod = 1e9 + 7, oo = 1e9; n int dx[]{1, -1, 0, 0, 1, -1, 1, -1}; n int dy[]{0, 0, 1, -1, 1, -1, -1, 1}; n vector<vector<pair<int, int> > > adj; n int n, m, k, t; n const int N = 4001; n int L[N], R[N]; n long dp[89][2002][89 * 2 + 10]; n long go(int l, int r, int k) { n   long &ret = dp[k][l][n - r - l + 89]; n   if (ret != -oo) return ret; n   if (l <= (n - r)) { n     ret = -oo; n     if (r - l > k) ret = max(ret, go(l + k, r, k) - L[l] + L[l + k]); n     if (r - l > k + 1) n       ret = max(ret, go(l + k + 1, r, k + 1) - L[l] + L[l + k + 1]); n     return (ret == -oo ? ret = 0 : ret); n   } n   ret = oo; n   if (r - l > k) ret = min(ret, go(l, r - k, k) - (-R[r] + R[r - k])); n   if (r - l > k + 1) n     ret = min(ret, go(l, r - k - 1, k + 1) - (-R[r] + R[r - k - 1])); n   return (ret == oo ? ret = 0 : ret); n } n int main() { n   Emsawy(); n   while (cin >> n) { n     for (int i = 1; i <= n; i++) { n       cin >> L[i]; n       R[i] = L[i]; n       L[i] += L[i - 1]; n     } n     for (int i = n - 1; i >= 1; i--) R[i] += R[i + 1]; n     for (int i = 0; i < 89; i++) { n       for (int j = 0; j < 2002; j++) { n         for (int g = 0; g < 89 * 2 + 10; g++) { n           dp[i][j][g] = -oo; n         } n       } n     } n     n += 1; n     cout << go(0, n, 1) << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int nod, edge; n int a[401][401]; n int dist[401]; n int vis[401]; n int bfs(int node) { n   queue<int> q; n   q.push(node); n   vis[node] = 1; n   while (q.size()) { n     int cur = q.front(); n     q.pop(); n     if (cur == nod) return dist[cur]; n     for (int i = nod; i >= 1; i--) { n       if (!vis[i] and (a[cur][i] + a[i][cur])) { n         dist[i] += dist[cur] + 1, q.push(i); n         vis[i] = 1; n       } n     } n   } n   return 0; n } n int bfs1(int node) { n   memset(dist, 0, sizeof dist); n   memset(vis, 0, sizeof vis); n   queue<int> q; n   q.push(node); n   vis[node] = 1; n   while (q.size()) { n     int cur = q.front(); n     q.pop(); n     if (cur == nod) return dist[cur]; n     for (int i = nod; i >= 1; i--) { n       if (!vis[i] and !(a[cur][i] + a[i][cur])) { n         dist[i] += dist[cur] + 1, q.push(i); n         vis[i] = 1; n       } n     } n   } n   return 0; n } n int main() { n   cin >> nod >> edge; n   int x, y; n   while (edge--) { n     cin >> x >> y; n     a[x][y] = 1; n     a[y][x] = 1; n   } n   int ans = bfs(1); n   int ans2 = bfs1(1); n   if (ans != 0 and ans2 != 0) n     cout << max(ans, ans2); n   else n     cout << -1; n } n


#include <bits/stdc++.h> n using namespace std; n struct point { n   int x, y; n   int det(point p) { return x * p.y - y * p.x; } n }; n point a[10], b[10]; n int GetCross(point p1, point p2, point p) { n   return (p2.x - p1.x) * (p.y - p1.y) - (p.x - p1.x) * (p2.y - p1.y); n } n bool check(point p) { n   return GetCross(b[1], b[2], p) * GetCross(b[3], b[4], p) >= 0 && n          GetCross(b[2], b[3], p) * GetCross(b[4], b[1], p) >= 0; n } n int main() { n   for (int i = 1; i <= 4; i++) { n     scanf( %d%d , &a[i].x, &a[i].y); n   } n   for (int i = 1; i <= 4; i++) { n     scanf( %d%d , &b[i].x, &b[i].y); n   } n   int mix = min(min(min(a[1].x, a[2].x), a[3].x), a[4].x); n   int mxx = max(max(max(a[1].x, a[2].x), a[3].x), a[4].x); n   int miy = min(min(min(a[1].y, a[2].y), a[3].y), a[4].y); n   int mxy = max(max(max(a[1].y, a[2].y), a[3].y), a[4].y); n   for (int i = mix; i <= mxx; i++) { n     for (int j = miy; j <= mxy; j++) { n       point pi; n       pi.x = i; n       pi.y = j; n       if (check(pi)) { n         printf( YES n ); n         return 0; n       } n     } n   } n   printf( NO n ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 100005; n const int sqrtn = 505; n int l = 1, r, sum, cant; n int n, m, p, s[maxn], w[maxn], cnt[maxn], last[maxn], pre[maxn], nxt[maxn], n     qwq[maxn][sqrtn], num[maxn]; n vector<int> v; n struct node { n   int l, r, id; n } a[maxn]; n inline bool cmp(node a, node b) { n   return a.l / p == b.l / p ? ((a.l / p) & 1 ? a.r < b.r : a.r > b.r) n                             : a.l < b.l; n } n inline void add(int x) { n   cnt[w[x]]++; n   sum += cnt[w[x]] == 1; n } n inline void add_right(int x) { n   add(x); n   if (cnt[w[x]] != 1 && pre[x]) { n     qwq[w[x]][pre[x]]++; n     if (qwq[w[x]][pre[x]] == 1) { n       num[w[x]]++; n       if (num[w[x]] == 2) { n         cant++; n       } n     } n   } n } n void add_left(int x) { n   add(x); n   if (cnt[w[x]] != 1 && nxt[x]) { n     qwq[w[x]][nxt[x]]++; n     if (qwq[w[x]][nxt[x]] == 1) { n       num[w[x]]++; n       if (num[w[x]] == 2) { n         cant++; n       } n     } n   } n } n inline void del(int x) { n   cnt[w[x]]--; n   sum -= cnt[w[x]] == 0; n } n inline void del_right(int x) { n   del(x); n   if (qwq[w[x]][pre[x]] && pre[x]) { n     qwq[w[x]][pre[x]]--; n     if (qwq[w[x]][pre[x]] == 0) { n       num[w[x]]--; n       if (num[w[x]] == 1) { n         cant--; n       } n     } n   } n } n inline void del_left(int x) { n   del(x); n   if (qwq[w[x]][nxt[x]] && nxt[x]) { n     qwq[w[x]][nxt[x]]--; n     if (qwq[w[x]][nxt[x]] == 0) { n       num[w[x]]--; n       if (num[w[x]] == 1) { n         cant--; n       } n     } n   } n } n signed main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   cin >> n; n   for (int i = 1; i <= n; i++) { n     cin >> w[i]; n     if (last[w[i]]) { n       pre[i] = i - last[w[i]]; n       nxt[last[w[i]]] = i - last[w[i]]; n       v.push_back(pre[i]); n       v.push_back(nxt[last[w[i]]]); n     } n     last[w[i]] = i; n   } n   sort(v.begin(), v.end()); n   v.erase(unique(v.begin(), v.end()), v.end()); n   for (int i = 1; i <= n; i++) { n   } n   p = sqrt(n); n   cin >> m; n   for (int i = 1; i <= m; i++) { n     cin >> a[i].l >> a[i].r; n     a[i].id = i; n   } n   sort(a + 1, a + m + 1, cmp); n   for (int i = 1; i <= m; i++) { n     while (l > a[i].l) { n       add_left(--l); n     } n     while (r < a[i].r) { n       add_right(++r); n     } n     while (l < a[i].l) { n       del_left(l++); n     } n     while (r > a[i].r) { n       del_right(r--); n     } n     s[a[i].id] = sum + (cant == sum); n   } n   for (int i = 1; i <= m; i++) { n     cout << s[i] <<   n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int a[123][123], b[123][123], f1[54][54][54][54], f2[54][54][54][54]; n int n; n void getans(int i1, int j1, int i2, int j2) { n   int i, j, t; n   printf( ? %d %d %d %d n , i1, j1, i2, j2); n   fflush(stdout); n   scanf( %d , &t); n   if (t == f1[i1][j1][i2][j2]) { n     printf( ! n ), fflush(stdout); n     for (i = 1; i <= n; i++) { n       for (j = 1; j <= n; j++) printf( %d , (1 + a[i][j]) / 2); n       printf(  n ); n     } n   } else { n     printf( ! n ), fflush(stdout); n     for (i = 1; i <= n; i++) { n       for (j = 1; j <= n; j++) printf( %d , (1 + b[i][j]) / 2); n       printf(  n ); n     } n   } n   exit(0); n } n int main() { n   int i, j, i1, i2, j1, j2, t, j3, i3; n   scanf( %d , &n); n   a[1][1] = 1, a[n][n] = -1; n   for (i = 1; i <= n; i++) n     for (j = 2 - (i % 2); j <= n; j += 2) { n       if (!a[i][j]) { n         if (a[i - 1][j - 1]) { n           printf( ? %d %d %d %d n , i - 1, j - 1, i, j); n           fflush(stdout); n           scanf( %d , &t); n           if (!t) n             a[i][j] = -a[i - 1][j - 1]; n           else n             a[i][j] = a[i - 1][j - 1]; n           continue; n         } n         if (i > j) { n           printf( ? %d %d %d %d n , i - 2, j, i, j); n           fflush(stdout); n           scanf( %d , &t); n           if (!t) n             a[i][j] = -a[i - 2][j]; n           else n             a[i][j] = a[i - 2][j]; n           continue; n         } else { n           printf( ? %d %d %d %d n , i, j - 2, i, j); n           fflush(stdout); n           scanf( %d , &t); n           if (!t) n             a[i][j] = -a[i][j - 2]; n           else n             a[i][j] = a[i][j - 2]; n           continue; n         } n       } n     } n   a[1][2] = 1; n   for (i = 1; i <= n; i++) n     for (j = 3 - (i % 2); j <= n; j += 2) { n       if (!a[i][j]) { n         if (a[i - 1][j - 1]) { n           printf( ? %d %d %d %d n , i - 1, j - 1, i, j); n           fflush(stdout); n           scanf( %d , &t); n           if (!t) n             a[i][j] = -a[i - 1][j - 1]; n           else n             a[i][j] = a[i - 1][j - 1]; n           continue; n         } n         if (i > j - 1) { n           printf( ? %d %d %d %d n , i - 2, j, i, j); n           fflush(stdout); n           scanf( %d , &t); n           if (!t) n             a[i][j] = -a[i - 2][j]; n           else n             a[i][j] = a[i - 2][j]; n           continue; n         } else { n           printf( ? %d %d %d %d n , i, j - 2, i, j); n           fflush(stdout); n           scanf( %d , &t); n           if (!t) n             a[i][j] = -a[i][j - 2]; n           else n             a[i][j] = a[i][j - 2]; n           continue; n         } n       } n     } n   printf( ? %d %d %d %d n , 2, 1, 3, 2); n   fflush(stdout); n   scanf( %d , &t); n   if (!t) n     a[2][1] = -a[3][2]; n   else n     a[2][1] = a[3][2]; n   j = 1; n   for (i = 4; i <= n; i += 2) { n     printf( ? %d %d %d %d n , i - 2, j, i, j); n     fflush(stdout); n     scanf( %d , &t); n     if (!t) n       a[i][j] = -a[i - 2][j]; n     else n       a[i][j] = a[i - 2][j]; n     continue; n   } n   for (i = 1; i <= n; i++) n     for (j = 1; j <= n; j++) b[i][j] = (((i + j) & 1) ? -a[i][j] : a[i][j]); n   for (i3 = 0; i3 < n; i3++) n     for (j3 = 0; j3 < n; j3++) { n       for (i2 = i3 + 1; i2 <= n; i2++) n         for (j2 = j3 + 1; j2 <= n; j2++) { n           i1 = i2 - i3, j1 = j2 - j3; n           if (i1 == i2 && j1 == j2) n             f1[i1][j1][i2][j2] = 1, f2[i1][j1][i2][j2] = 1; n           else if (i2 + j2 - i1 - j1 == 1) { n             f1[i1][j1][i2][j2] = (a[i1][j1] == a[i2][j2]); n             f2[i1][j1][i2][j2] = (b[i1][j1] == b[i2][j2]); n           } else { n             if (a[i1][j1] == a[i2][j2] && n                 (f1[i1 + 1][j1][i2 - 1][j2] || f1[i1][j1 + 1][i2][j2 - 1] || n                  f1[i1 + 1][j1][i2][j2 - 1] || f1[i1][j1 + 1][i2 - 1][j2])) n               f1[i1][j1][i2][j2] = 1; n             else n               f1[i1][j1][i2][j2] = 0; n             if (b[i1][j1] == b[i2][j2] && n                 (f2[i1 + 1][j1][i2 - 1][j2] || f2[i1][j1 + 1][i2][j2 - 1] || n                  f2[i1 + 1][j1][i2][j2 - 1] || f2[i1][j1 + 1][i2 - 1][j2])) n               f2[i1][j1][i2][j2] = 1; n             else n               f2[i1][j1][i2][j2] = 0; n             if (f1[i1][j1][i2][j2] != f2[i1][j1][i2][j2]) n               getans(i1, j1, i2, j2); n           } n         } n     } n } n


#include <bits/stdc++.h> n using namespace std; n bool isprime(int n) { n   for (int i = 2; i < n; i++) { n     if (n % i == 0) { n       return false; n     } n   } n   return true; n } n int main() { n   int n; n   cin >> n; n   string s; n   cin >> s; n   int diff = 1; n   int i = 0; n   while (i < n) { n     cout << s[i]; n     i = i + diff; n     diff++; n   } n } n


#include <bits/stdc++.h> n using namespace std; n vector<pair<char, pair<int, int> > > closest(10, n                                              make_pair( * , make_pair(0, 0))); n int n, X, Y; n int check_dir(int x, int y) { n   if (x == X && y > Y) return 1; n   if (x == X && y < Y) return 5; n   if (Y == y && x > X) return 3; n   if (Y == y && x < X) return 7; n   if (X < x && (x - X) == (y - Y)) return 2; n   if (X > x && (x - X) == (y - Y)) return 6; n   if (y > Y && (X - x) == (y - Y)) return 4; n   if (y < Y && (X - x) == (y - Y)) return 8; n   return 0; n } n int dist(int x, int y, int xx, int yy) { return max(abs(xx - x), abs(yy - y)); } n void get_closest(char z, int x, int y, int dir) { n   if (closest[dir].first ==  *  || n       dist(X, Y, x, y) < n           dist(X, Y, closest[dir].second.first, closest[dir].second.second)) { n     closest[dir].first = z; n     closest[dir].second = make_pair(x, y); n   } n } n int main() { n   scanf( %d%d%d , &n, &X, &Y); n   for (int i = 0; i < n; ++i) { n     char z; n     int x, y; n     cin >> z >> x >> y; n     int dir = check_dir(x, y); n     if (dir) { n       get_closest(z, x, y, dir); n     } n   } n   bool flag = false; n   for (int i = 1; i <= 7; i += 2) { n     if (closest[i].first ==  R  || closest[i].first ==  Q ) flag = true; n   } n   for (int i = 2; i <= 8; i += 2) { n     if (closest[i].first ==  B  || closest[i].first ==  Q ) flag = true; n   } n   if (flag) n     printf( YES ); n   else n     printf( NO ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void c_p_c() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n } n bool isPrime(long long n) { n   if (n <= 1) return false; n   if (n <= 3) return true; n   if (n % 2 == 0 || n % 3 == 0) return false; n   for (long long i = 5; i * i <= n; i += 6) { n     if (n % i == 0 || n % (i + 2) == 0) return false; n   } n   return true; n } n vector<long long> Sieve(long long n) { n   bool prime[n + 1]; n   memset(prime, true, sizeof(prime)); n   for (long long i = 2; i * i <= n; i++) { n     if (prime[i]) { n       for (long long j = i * i; j <= n; j += i) { n         prime[j] = false; n       } n     } n   } n   vector<long long> primes; n   for (long long i = 2; i <= n; i++) { n     if (prime[i]) primes.push_back(i); n   } n   return primes; n } n map<long long, long long> primeFactors(long long n) { n   map<long long, long long> factors; n   while (n % 2 == 0) { n     n /= 2; n     factors[2]++; n   } n   for (long long i = 3; i <= sqrt(n); i += 2) { n     while (n % i == 0) { n       factors[i]++; n       n /= i; n     } n   } n   if (n > 2) factors[n]++; n   return factors; n } n int32_t main() { n   c_p_c(); n   long long t; n   cin >> t; n   while (t--) { n     long long n; n     cin >> n; n     vector<long long> arr(n + 1, 0); n     long long num; n     for (long long i = 0; i < n; i++) { n       cin >> num; n       arr[num]++; n     } n     sort(arr.begin(), arr.end(), greater<long long>()); n     long long ans = arr[0], prev = arr[0]; n     for (long long i = 1; i <= n; i++) { n       if (prev == 0) break; n       if (arr[i] >= prev) { n         ans += prev - 1; n         prev--; n       } else { n         ans += arr[i]; n         prev = arr[i]; n       } n     } n     cout << ans <<   n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e4 + 5; n int n, a[N], m, k; n long long ans; n int main() { n   scanf( %d%d%d , &n, &m, &k); n   for (int i = 1; i <= n; i++) scanf( %d , &a[i]); n   if (n % 2 == 0) { n     puts( 0 ); n     return 0; n   } n   int mn = N * 100; n   for (int i = 1; i <= n; i += 2) mn = min(mn, a[i]); n   long long ans = min((long long)mn, 1ll * (m / ((n + 1) / 2)) * k); n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n string ITS(long long x) { n   string s =   ; n   while (x > 0) { n     s += (char)(x % 10 +  0 ); n     x /= 10; n   } n   string t =   ; n   for (int i = s.size() - 1; i > -1; i--) t += s[i]; n   return t; n } n long long inf = 1e9; n long long mod = 1e9 + 7; n const long long M = 2e3; n int main() { n   std::ios::sync_with_stdio(false); n   long long n, m; n   cin >> n >> m; n   double a = n * pow(1.000000011, m); n   cout << fixed << setprecision(15) << a; n } n


#include <bits/stdc++.h> n using namespace std; n long long n = 0, m = 0, q = 0; n long long ans = 0; n vector<vector<int>> G = vector<std::vector<int>>(100001); n vector<long long> f(112345), fa(100001), dep(100001), val(100001), sz(100001), n     s(100001); n long long F(long long x) { return x == f[x] ? x : (f[x] = F(f[x])); } n void dfs(long long u, long long ff) { n   sz[u] = 1; n   dep[u] = dep[ff] + 1; n   fa[u] = ff; n   for (int i = 0; i < G[u].size(); ++i) { n     long long v = G[u][i]; n     if (v == ff) { n       continue; n     } n     dfs(v, u); n     sz[u] += sz[v]; n     val[u] += sz[v] * sz[v]; n   } n   val[u] += (n - sz[u]) * (n - sz[u]); n   ans -= val[u]; n } n void merge(int x, int y) { n   ans -= s[x] * (s[x] - 1) * (s[x] - 2); n   ans -= s[y] * (s[y] - 1) * (s[y] - 2); n   ans -= s[x] * (s[x] - 1) * (n - s[x]) * 2; n   ans -= s[y] * (s[y] - 1) * (n - s[y]) * 2; n   ans -= (n - s[x]) * (n - s[x]) * s[x]; n   ans -= (n - s[y]) * (n - s[y]) * s[y]; n   ans += val[x] * s[x]; n   ans += val[y] * s[y]; n   val[x] += val[y] - sz[y] * sz[y] - (n - sz[y]) * (n - sz[y]); n   s[x] += s[y]; n   f[y] = x; n   ans += s[x] * (s[x] - 1) * (s[x] - 2); n   ans += s[x] * (s[x] - 1) * (n - s[x]) * 2; n   ans += (n - s[x]) * (n - s[x]) * s[x]; n   ans -= val[x] * s[x]; n } n int main() { n   cin >> n; n   for (int i = 1; i <= n; ++i) { n     G[i] = std::vector<int>(); n     s[i] = 1; n     f[i] = i; n   } n   for (int i = 0; i < n - 1; ++i) { n     int u, v; n     cin >> u >> v; n     G[u].push_back(v); n     G[v].push_back(u); n   } n   ans = 1LL * n * (n - 1) * (n - 1); n   dfs(1, 0); n   cout << ans << endl; n   cin >> q; n   while (q-- > 0) { n     int u, v; n     cin >> u >> v; n     u = F(u); n     v = F(v); n     while (u != v) { n       if (dep[u] < dep[v]) { n         int o = v; n         v = u; n         u = o; n       } n       merge(F(fa[u]), u); n       u = F(u); n     } n     cout << ans << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <class T> n bool uin(T &a, T b) { n   return a > b ? (a = b, true) : false; n } n template <class T> n bool uax(T &a, T b) { n   return a < b ? (a = b, true) : false; n } n const long long maxn = 1000001, mod = 1e9 + 7; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   int n; n   cin >> n; n   long double a[n], b[n]; n   for (long long i = 0; i < (long long)(n); ++i) cin >> a[i]; n   for (long long i = 0; i < (long long)(n); ++i) cin >> b[i]; n   int ex = 0; n   map<long double, int> freq; n   for (long long i = 0; i < (long long)(n); ++i) { n     if (a[i] == 0 && b[i] == 0) ex++; n     if (a[i] == 0) continue; n     freq[b[i] / a[i]]++; n   } n   int ans = 0; n   for (auto it : freq) { n     ans = max(ans, it.second); n   } n   cout << ans + ex <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n #pragma GCC optimize( O3 ) n using namespace std; n const int N = 3e5 + 5, M = 1e8 + 5, OO = 1000000; n int T, n, m; n int u, v; n int deg[N]; n int main() { n   scanf( %d , &n); n   for (int i = 1; i < n; ++i) { n     scanf( %d %d , &u, &v); n     ++deg[u], ++deg[v]; n   } n   for (int i = 1; i <= n; ++i) { n     if (deg[i] == 2) { n       printf( NO n ); n       return 0; n     } n   } n   printf( YES n ); n } n


#include <bits/stdc++.h> n #pragma GCC target( sse ) n const int maxn = 2e5 + 5; n struct IO { n   char c; n   inline char gc() { n     static char buf[maxn], *p1 = buf, *p2 = buf; n     return p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, maxn, stdin), p1 == p2) n                ? EOF n                : *p1++; n   } n   template <typename T> n   inline IO &operator>>(T &_) { n     _ = 0; n     bool f = 1; n     c = gc(); n     while (c <  0  || c >  9 ) { n       if (c ==  - ) f = 0; n       c = gc(); n     } n     while (c >=  0  && c <=  9 ) { n       _ = _ * 10 + c - 48; n       c = gc(); n     } n     if (!f) _ = -_; n     return *this; n   } n   char buf[maxn]; n   int p = 0; n   ~IO() { fwrite(buf, 1, p, stdout); } n   inline void pc(const char &c) { n     buf[p++] = c; n     if (p == maxn) fwrite(buf, 1, maxn, stdout), p = 0; n   } n   template <typename T> n   inline IO &operator<<(T x) { n     if (!x) { n       pc(48); n       pc(  n ); n       return *this; n     } n     static int wt[41], len; n     len = 0; n     if (x < 0) { n       pc( - ); n       x = -x; n     } n     for (; x; x /= 10) { n       wt[++len] = x % 10; n     } n     while (len) { n       pc(wt[len--] + 48); n     } n     pc(  n ); n     return *this; n   } n   inline IO &operator>>(char s[]) { n     c = gc(); n     int len = -1; n     while (c ==     || c ==   n  || c ==   r ) c = gc(); n     while (c !=     && c !=   n  && c !=   r  && c !=   0  && c != EOF) { n       s[++len] = c; n       c = gc(); n     } n     s[++len] =   0 ; n     return *this; n   } n } io; n int n, m, q; n char s1[maxn], s2[maxn]; n unsigned long long b1[64][(maxn >> 6) + 64], b2[64][(maxn >> 6) + 64]; n void build() { n   for (int i = 0; i < 64; i++) { n     for (int j = i; j < n; j += 64) { n       for (int k = 0; k < 64; k++) n         b1[i][j >> 6] += (1ll << k) * (s1[j + k] - 48); n     } n   } n   for (int i = 0; i < 64; i++) { n     for (int j = i; j < m; j += 64) { n       for (int k = 0; k < 64; k++) n         b2[i][j >> 6] += (1ll << k) * (s2[j + k] - 48); n     } n   } n } n int main() { n   io >> s1 >> s2; n   n = strlen(s1), m = strlen(s2); n   build(); n   io >> q; n   while (q--) { n     int p1, p2, len; n     io >> p1 >> p2 >> len; n     int now = 0, a1 = 0, a2 = 0, a3 = 0, a4 = 0, a5 = 0, a6 = 0, a7 = 0, a8 = 0; n     unsigned long long *f1 = b1[p1 & 63] + (p1 >> 6), n                        *f2 = b2[p2 & 63] + (p2 >> 6); n     for (; now + 511 < len; now += 512, f1 += 8, f2 += 8) { n       a1 += __builtin_popcountll(f1[0] ^ f2[0]); n       a2 += __builtin_popcountll(f1[1] ^ f2[1]); n       a3 += __builtin_popcountll(f1[2] ^ f2[2]); n       a4 += __builtin_popcountll(f1[3] ^ f2[3]); n       a5 += __builtin_popcountll(f1[4] ^ f2[4]); n       a6 += __builtin_popcountll(f1[5] ^ f2[5]); n       a7 += __builtin_popcountll(f1[6] ^ f2[6]); n       a8 += __builtin_popcountll(f1[7] ^ f2[7]); n     } n     for (; now < len; now++) a1 += (s1[p1 + now] - 48) ^ (s2[p2 + now] - 48); n     io << a1 + a2 + a3 + a4 + a5 + a6 + a7 + a8; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   long long int t; n   cin >> t; n   while (t--) { n     long long int n, i, j, k, sum = 0, ev = 0, ze = 0; n     string s; n     cin >> s; n     n = s.length(); n     for (i = 0; i < n; i++) { n       sum = sum + s[i] -  0 ; n       if ((s[i] -  0 ) % 2 == 0) ev++; n       if (s[i] -  0  == 0) ze++; n     } n     if (ze == 0) n       cout <<  cyan  << endl; n     else { n       if (ev < 2) n         cout <<  cyan  << endl; n       else { n         if (sum % 3 == 0) n           cout <<  red  << endl; n         else n           cout <<  cyan  << endl; n       } n     } n   } n } n


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


#include <bits/stdc++.h> n using namespace std; n int MOD = 1000000007; n int tree[20000][2]; n int main() { n   ios::sync_with_stdio(0); n   int n, a, b, k; n   cin >> n >> a >> b >> k; n   for (int i = 1; i <= n; i++) { n     if (i != b) tree[i][0]++; n   } n   int h = 1; n   for (int j = 1; j <= k; j++) { n     for (int i = 2; i <= n; i++) { n       tree[i][1 - h] += tree[i - 1][1 - h]; n       tree[i][1 - h] %= MOD; n     } n     for (int i = 1; i <= n; i++) { n       if (i == b) continue; n       int lim = abs(i - b); n       lim--; n       tree[i][h] = n           tree[min(n, i + lim)][1 - h] - tree[max(1, i - lim) - 1][1 - h]; n       tree[i][h] += MOD; n       tree[i][h] %= MOD; n       tree[i][h] -= (tree[i][1 - h] - tree[i - 1][1 - h] + MOD) % MOD; n       tree[i][h] += MOD; n       tree[i][h] %= MOD; n     } n     h ^= 1; n   } n   for (int i = 2; i <= n; i++) { n     tree[i][1 - h] += tree[i - 1][1 - h]; n     tree[i][1 - h] %= MOD; n   } n   cout << (tree[a][1 - h] - tree[a - 1][1 - h] + MOD) % MOD << endl; n   return 0; n } n


#include <bits/stdc++.h> n template <typename T> n bool Chkmax(T &x, T y) { n   return x < y ? x = y, true : false; n } n template <typename T> n bool Chkmin(T &x, T y) { n   return y < x ? x = y, true : false; n } n const int MAXN = 1e5 + 1e4, MAXK = 5 + 5; n const double eps = 1e-4; n int n; n int k; n std::pair<int, int> ans[MAXK]; n inline int Dcmp(const double &x) { n   if (fabs(x) < eps) return 0; n   return x > 0 ? 1 : -1; n } n struct Point { n   double x, y; n   Point() {} n   Point(double x, double y) : x(x), y(y) {} n }; n struct Line { n   int A, B, C, id; n   Line() {} n   Line(int A, int B, int C, int id) : A(A), B(B), C(C), id(id) {} n   inline bool OnLine(const Point &p) { n     return Dcmp(A * p.x + B * p.y + C) == 0; n   } n } s[MAXN]; n Point GetInter(Line l1, Line l2) { n   if (l2.A == 0) std::swap(l1, l2); n   double x, y; n   x = (double)(-l2.C + (double)l2.B * l1.C / l1.B) / n       (l2.A - (double)l1.A * l2.B / l1.B); n   y = (double)(-l1.C - l1.A * x) / l1.B; n   return Point(x, y); n } n bool Work(int N, int K) { n   if (N <= K) { n     printf( YES n%d n , k - K + N); n     for (int i = 1, _ = N; i <= _; i++) printf( %d %d n , s[i].id, -1); n     return true; n   } n   if (K == 0) return false; n   Point p[MAXK * MAXK]; n   std::pair<int, int> fr[MAXK * MAXK]; n   int tot = 0; n   for (int i = 1, _ = K + 1; i <= _; i++) n     for (int j = i + 1, _ = K + 1; j <= _; j++) { n       ++tot; n       p[tot] = GetInter(s[i], s[j]); n       fr[tot] = std::make_pair(s[i].id, s[j].id); n     } n   for (int i = 1, _ = tot; i <= _; i++) { n     int newN = N; n     for (int j = 1; j <= newN; j++) { n       if (s[j].OnLine(p[i])) { n         std::swap(s[j], s[newN--]); n         j--; n       } n     } n     if (Work(newN, K - 1)) { n       printf( %d %d n , fr[i].first, fr[i].second); n       return true; n     } n   } n   return false; n } n void Init() { n   int A, B, C; n   scanf( %d%d , &n, &k); n   for (int i = 1, _ = n; i <= _; i++) { n     scanf( %d%d%d , &A, &B, &C); n     s[i] = Line(A, B, C, i); n   } n } n int main() { n   if (fopen( D.in ,  r ) != NULL) { n     freopen( D.in ,  r , stdin); n     freopen( D.out ,  w , stdout); n   } n   Init(); n   if (!Work(n, k)) printf( NO n ); n   return 0; n } n


#include <bits/stdc++.h> n int mod = 1000000007; n using namespace std; n using namespace std; n int n, k; n int a[400005]; n int pos[400005]; n map<int, int> m; n int main() { n   ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0); n   ; n   cin >> n >> k; n   for (int i = 1; i <= n; i++) pos[i] = 1e7; n   m[a[1]] = 1e7; n   for (int i = 1; i <= n; i++) { n     cin >> a[i]; n   } n   for (int i = 1; i <= n; i++) { n     pos[m[a[i]]] = i; n     m[a[i]] = i; n   } n   int ans = 0; n   set<int> s; n   set<pair<int, int> > ss; n   for (int i = 1; i <= n; i++) { n     if (!s.count(a[i])) { n       if (s.size() < k) { n         ans++; n         s.insert(a[i]); n         ss.insert(make_pair(pos[i], a[i])); n       } else { n         ans++; n         set<pair<int, int> >::iterator it = ss.end(); n         it--; n         int val = (*it).second; n         ss.erase(it); n         s.erase((val)); n         s.insert(a[i]); n         ss.insert(make_pair(pos[i], a[i])); n       } n     } else { n       ss.erase(ss.begin()); n       ss.insert(make_pair(pos[i], a[i])); n     } n   } n   cout << ans; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n = ({ n     int x; n     scanf( %d , &x); n     x; n   }); n   string s; n   cin >> s; n   string ans =  no ; n   for (int d = (1); d < (30); d += (1)) { n     for (int i = (0); i < (n); i += (1)) { n       int g = 1; n       for (int j = (0); j < (5); j += (1)) { n         if (i + j * d >= n || s[i + j * d] ==  . ) { n           g = 0; n         } n       } n       if (g) { n         ans =  yes ; n         break; n       } n     } n   } n   cout << ans << endl; n } n


#include <bits/stdc++.h> n using namespace std; n using ll = long long; n using ld = long double; n using ull = unsigned long long; n using pii = pair<int, int>; n using C = complex<double>; n const double PI = 3.14159265; n const double eps = 1e-8; n const int N = 1 << 17; n const int M = 1e9 + 7; n const int inf = 1e9 + 7; n const ll INF = 1e18; n ll pw(ll a, ll b) { n   ll t = 1; n   a %= M; n   for (; b; b >>= 1) { n     if (b & 1) { n       t = t * a % M; n     } n     a = a * a % M; n   } n   return t; n } n ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; } n int n; n int num[N]; n int mx[N << 1]; n int seg[N << 1]; n int find() { n   int u = 1, w = 0; n   while (u < N) { n     if (mx[u << 1 | 1] + w > 0) { n       u = u << 1 | 1; n     } else { n       w += seg[u << 1 | 1]; n       u <<= 1; n     } n   } n   return u - N; n } n void add(int p, int v) { n   p += N; n   seg[p] += v; n   mx[p] = seg[p] == 1; n   for (p >>= 1; p; p >>= 1) { n     seg[p] = seg[p << 1] + seg[p << 1 | 1]; n     mx[p] = max(mx[p << 1 | 1], mx[p << 1] + seg[p << 1 | 1]); n   } n } n int main() { n   ios::sync_with_stdio(0), cin.tie(0), cout.tie(0); n   cin >> n; n   int p, t, x; n   for (int i = 0; i < n; i++) { n     cin >> p >> t; n     if (t == 1) { n       cin >> num[p]; n       add(p, 1); n     } else { n       add(p, -1); n     } n     int ps = find(); n     if (mx[1] <= 0) { n       puts( -1 ); n     } else { n       printf( %d n , num[ps]); n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int t; n   cin >> t; n   while (t--) { n     long long x, y; n     cin >> x >> y; n     long long l = x - y; n     l = l < 0 ? -l : l; n     int r = l % 10; n     long long a = l / 10.0; n     if (r) a++; n     cout << a; n     cout <<   n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const long double INF = 1e9; n const long double EPS = 1e-9; n long double dist(long double x1, long double y1, long double x2, n                  long double y2) { n   return sqrt(pow(x1 - x2, 2) + pow(y1 - y2, 2)); n } n long double func(long double X, long double Y, long double L, long double R) { n   return min(dist(L, 0, X, Y), dist(R, 0, X, Y)) + R - L; n } n int main() { n   cout.setf(ios::fixed); n   cout.precision(15); n   int n, k; n   cin >> n >> k; n   k--; n   vector<int> x(n); n   for (int i = 0; i < n; i++) cin >> x[i]; n   int X, Y; n   cin >> X >> Y; n   int xStart; n   if (k < n) xStart = x[k]; n   sort(x.begin(), x.end()); n   if (k == n) { n     cout << func(X, Y, x[0], x[n - 1]); n     return 0; n   } n   k = lower_bound(x.begin(), x.end(), xStart) - x.begin(); n   long double ans = INF; n   for (int i = 0; i < n; i++) { n     long double cur1 = n         min(x[k] - x[0] + x[i] - x[0] + dist(x[i], 0, X, Y), n             abs(x[i] - x[k]) + x[i] - x[0] + dist(x[0], 0, X, Y)); n     if (i + 1 < n) cur1 += func(X, Y, x[i + 1], x[n - 1]); n     long double cur2 = n         min(x[n - 1] - x[k] + x[n - 1] - x[i] + dist(x[i], 0, X, Y), n             abs(x[k] - x[i]) + x[n - 1] - x[i] + dist(x[n - 1], 0, X, Y)); n     if (i - 1 >= 0) cur2 += func(X, Y, x[0], x[i - 1]); n     ans = min(ans, min(cur1, cur2)); n   } n   cout << ans <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int v, e; n vector<int> out[30005]; n vector<int> ans; n bool vis[30005]; n void dfstree(int n) { n   for (auto x : out[n]) { n     if (!vis[x]) { n       vis[x] = true; n       dfstree(x); n     } n   } n   ans.push_back(n); n } n int main() { n   cin >> v >> e; n   for (int i = 0; i < e; i++) { n     int x, y; n     cin >> x >> y; n     out[x].push_back(y); n   } n   for (int i = 1; i < v + 1; i++) { n     if (!vis[i]) { n       vis[i] = true; n       dfstree(i); n     } n   } n   for (auto x : ans) cout << x <<    ; n   cout <<   n ; n   ; n } n


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


#include <bits/stdc++.h> n using namespace std; n const int maxn = 100; n int n, m, ans; n int num[maxn]; n set<int> st[2]; n set<int>::iterator it, itt; n void get_st(int begin, int end, set<int> &st) { n   st.clear(); n   st.insert(0); n   for (int i = 0; i < (1 << (end - begin)); ++i) { n     int tmp = 0; n     for (int j = 0; j < end - begin; ++j) { n       if (((i >> j) & 1) == 1) { n         tmp = (tmp + num[j + begin]) % m; n       } n     } n     st.insert(tmp); n   } n } n int main() { n   ios::sync_with_stdio(false); n   while (scanf( %d%d , &n, &m) != EOF) { n     ans = 0; n     for (int i = 0; i < n; ++i) { n       scanf( %d , &num[i]); n     } n     get_st(0, (n + 1) / 2, st[0]); n     get_st((n + 1) / 2, n, st[1]); n     for (it = st[0].begin(); it != st[0].end(); ++it) { n       for (int i = 1; i <= 2; ++i) { n         itt = st[1].lower_bound(m * i - *it); n         if (itt != st[1].begin()) { n           --itt; n           ans = max(ans, (*it + *itt) % m); n         } n       } n     } n     printf( %d n , ans); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m, a[200005], head[200005]; n struct edge { n   int to, nxt; n   edge(int _t = -1, int _n = -1) { n     to = _t; n     nxt = _n; n   } n } e[400005]; n void addedge(int u, int v) { n   e[m] = edge(v, head[u]); n   head[u] = m++; n   e[m] = edge(u, head[v]); n   head[v] = m++; n } n vector<int> g; n int dp[200005], s; n void dfs1(int fa, int u) { n   if (a[u] == -1) n     dp[u] = 1; n   else n     dp[u] = 0; n   for (int p = head[u]; p != -1; p = e[p].nxt) { n     int v = e[p].to; n     if (v == fa) continue; n     dfs1(u, v); n     dp[u] += dp[v]; n   } n } n void dfs2(int fa, int u) { n   if (s == 0 || dp[u] == 0) return; n   int x; n   for (int p = head[u]; p != -1; p = e[p].nxt) { n     int v = e[p].to; n     x = v; n     if (v == fa || dp[v] == 0) continue; n     g.push_back(v + 1); n     if (a[v] == -1) { n       s--; n       dp[v]--; n     } else { n       s++; n       dp[v]++; n     } n     a[v] = -a[v]; n     dfs2(u, v); n     if (s == 0) return; n     g.push_back(u + 1); n     if (a[u] == -1) { n       s--; n       dp[u]--; n     } else { n       s++; n       dp[u]++; n     } n     a[u] = -a[u]; n   } n   if (a[u] == -1) { n     a[u] = 1; n     if (fa == -1) { n       g.push_back(x + 1); n       g.push_back(1); n       g.push_back(x + 1); n       return; n     } else { n       g.push_back(fa + 1); n       g.push_back(u + 1); n       s--; n       dp[u]--; n       if (a[fa] == -1) { n         s--; n         dp[fa]--; n       } else { n         s++; n         dp[fa]++; n       } n       a[fa] = -a[fa]; n     } n   } n } n int main() { n   scanf( %d , &n); n   for (int i = 0; i < n; ++i) { n     scanf( %d , &a[i]); n     if (a[i] == -1) s++; n   } n   memset(head, -1, sizeof(head)); n   m = 0; n   for (int i = 1; i < n; ++i) { n     int u, v; n     scanf( %d%d , &u, &v); n     addedge(u - 1, v - 1); n   } n   if (s == 0) { n     printf( 1 n ); n     return 0; n   } n   g.clear(); n   dfs1(-1, 0); n   dfs2(-1, 0); n   printf( 1 ); n   for (int i = 0; i < g.size(); ++i) printf(  %d , g[i]); n   printf(  n ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = int(3e5), mod = int(1e9) + 7, block = 316; n int n, m, q; n vector<int> g[N], comp[N], heavy, di[N]; n int root[N], cur, sz[N], d[N], dr[N], d1[N], d2[N]; n bool used[N]; n bool h[N]; n long double ans[N], fans[N]; n vector<pair<int, int> > qu[N]; n void dfs(int v) { n   used[v] = 1; n   comp[cur].push_back(v); n   sz[cur]++; n   root[v] = cur; n   for (int i = 0; i < g[v].size(); i++) { n     int to = g[v][i]; n     if (!used[to]) dfs(to); n   } n } n void dfs1(int v, int p = -1) { n   used[v] = 1; n   for (int i = 0; i < g[v].size(); i++) { n     int to = g[v][i]; n     if (to == p) continue; n     dfs1(to, v); n     d1[v] = max(d1[v], d1[to] + 1); n   } n } n void dfs2(int v, int p = -1) { n   int mx1 = d2[v], mx2 = 0; n   for (int i = 0; i < g[v].size(); i++) { n     int to = g[v][i]; n     if (to == p) continue; n     if (d1[to] + 1 > mx1) { n       mx2 = mx1; n       mx1 = d1[to] + 1; n     } else if (d1[to] + 1 > mx2) { n       mx2 = d1[to] + 1; n     } n   } n   for (int i = 0; i < g[v].size(); i++) { n     int to = g[v][i]; n     if (to == p) continue; n     if (mx1 == d1[to] + 1) { n       d2[to] = mx2 + 1; n     } else n       d2[to] = mx1 + 1; n     dfs2(to, v); n   } n } n int a[N], b[N], an, bn; n long long pref[N]; n void cons(int v) { n   an = 0; n   for (int i = 0; i < di[v].size(); i++) { n     a[++an] = di[v][i]; n   } n   for (int i = 1; i <= an; i++) { n     pref[i] = pref[i - 1] + a[i]; n   } n } n long double get(int v, int curd) { n   bn = 0; n   long long sum = 0; n   for (int i = 0; i < di[v].size(); i++) { n     b[++bn] = di[v][i]; n   } n   int j = an; n   for (int i = 1; i <= bn; i++) { n     while (j > 0 && b[i] + a[j] + 1 > curd) j--; n     sum += 1ll * j * curd + 1ll * (an - j) * b[i] + pref[an] - pref[j] + an - j; n   } n   return (sum + 0.0) / (1ll * an * bn); n } n int main() { n   scanf( %d%d%d , &n, &m, &q); n   for (int i = 1, u, v; i <= m; i++) { n     scanf( %d%d , &u, &v); n     g[u].push_back(v); n     g[v].push_back(u); n   } n   for (int i = 1; i <= n; i++) { n     if (!used[i]) { n       cur = i; n       dfs(i); n     } n   } n   for (int i = 1; i <= n; i++) { n     if (root[i] == i) { n       if (sz[i] > block) { n         heavy.push_back(i); n         h[i] = 1; n       } n     } n   } n   memset(used, 0, sizeof(used)); n   for (int i = 1; i <= n; i++) { n     if (!used[i]) { n       dfs1(i); n       dfs2(i); n       int v = root[i]; n       for (int j = 0; j < comp[v].size(); j++) { n         int u = comp[v][j]; n         dr[u] = max(d1[u], d2[u]); n         di[v].push_back(dr[u]); n         d[v] = max(d[v], dr[u]); n       } n       sort(di[v].begin(), di[v].end()); n     } n   } n   for (int i = 1, u, v; i <= q; i++) { n     scanf( %d%d , &u, &v); n     u = root[u]; n     v = root[v]; n     if (u == v) { n       ans[i] = -1; n     } else { n       if (h[u]) { n         qu[u].push_back(make_pair(v, i)); n       } else if (h[v]) { n         qu[v].push_back(make_pair(u, i)); n       } else { n         int curd = max(d[u], d[v]); n         cons(u); n         ans[i] = get(v, curd); n       } n     } n   } n   for (int i = 1; i <= n; i++) { n     if (int(qu[i].size()) > 0) { n       cons(i); n       for (int j = 1; j <= n; j++) n         if (root[j] == j) { n           fans[j] = get(j, max(d[i], d[j])); n         } n       for (int j = 0; j < qu[i].size(); j++) { n         ans[qu[i][j].second] = fans[qu[i][j].first]; n       } n     } n   } n   for (int i = 1; i <= q; i++) { n     printf( %.12lf n , double(ans[i])); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int a, b, c, d, p; n int main() { n   cin >> a >> b >> c >> d; n   d -= c - 1; n   a -= c; n   b -= c; n   if (d == 2) { n     a--; n     b--; n     p = 3; n   } n   if (d == 1) { n     if (a) { n       a--; n       p = 1; n     } else { n       b--; n       p = 2; n     } n   } n   if (a < 0 || b < 0 || d > 2 || d < 0) n     cout <<  -1 ; n   else { n     if (p & 2) cout <<  7 ; n     for (; a > 0; a--) cout <<  4 ; n     for (; c > 0; c--) cout <<  47 ; n     for (; b > 0; b--) cout <<  7 ; n     if (p & 1) cout <<  4 ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 1e6 + 10; n int n, k; n int ver[maxn], t[maxn], dp[maxn], tin[maxn], n     cnt = 0, tout[maxn], h[maxn], p[maxn][30], important[maxn], ans = 0; n vector<int> e[maxn]; n void dfs(int u, int par = 0) { n   cnt++; n   tin[u] = cnt; n   h[u] = h[par] + 1; n   for (int i = 0; i < e[u].size(); i++) { n     int v = e[u][i]; n     if (v == par) continue; n     p[v][0] = u; n     for (int j = 1; j < 22; j++) p[v][j] = p[p[v][j - 1]][j - 1]; n     dfs(v, u); n   } n   tout[u] = cnt; n   return; n } n int get(int u, int len) { n   for (int i = 22; i >= 0; i--) n     if (len & (1 << i)) u = p[u][i]; n   return u; n } n int lca(int u, int v) { n   if (h[u] > h[v]) swap(u, v); n   int len = h[v] - h[u]; n   v = get(v, len); n   if (u == v) return u; n   for (int i = 22; i >= 0; i--) n     if (p[u][i] != p[v][i]) { n       u = p[u][i]; n       v = p[v][i]; n     } n   return p[u][0]; n } n bool cmp(int u, int v) { return tin[u] < tin[v]; } n bool compare(int u, int v) { return tin[u] < tin[v] && tout[u] >= tout[v]; } n bool buildGraph() { n   for (int i = 1; i < k; i++) ver[i + k] = lca(ver[i], ver[i + 1]); n   sort(ver + 1, ver + 2 * k, cmp); n   int cnter = unique(ver + 1, ver + 2 * k) - ver - 1; n   for (int i = 1; i <= cnter; i++) e[ver[i]].clear(); n   stack<int> s; n   s.push(ver[1]); n   for (int i = 2; i <= cnter; i++) { n     int v = ver[i]; n     while (!compare(s.top(), v)) s.pop(); n     int u = s.top(); n     if (important[u] && important[v] && u == p[v][0]) return 0; n     s.push(v); n     e[u].push_back(v); n   } n   return 1; n } n void dfsSolve(int u) { n   int cnter = 0; n   for (int i = 0; i < e[u].size(); i++) { n     int v = e[u][i]; n     dfsSolve(v); n     cnter += dp[v]; n   } n   if (important[u]) { n     ans += cnter; n     dp[u] = 1; n   } else { n     if (cnter >= 2) n       ans++, dp[u] = 0; n     else if (cnter == 1) n       dp[u] = 1; n     else n       dp[u] = 0; n   } n } n int main() { n   memset(h, 0, sizeof(h)); n   memset(tin, 0, sizeof(tin)); n   memset(tout, 0, sizeof(tout)); n   memset(dp, 0, sizeof(dp)); n   memset(p, 0, sizeof(p)); n   memset(important, 0, sizeof(important)); n   ios_base::sync_with_stdio(false); n   cin >> n; n   for (int i = 1; i < n; i++) { n     int u, v; n     cin >> u >> v; n     e[u].push_back(v); n     e[v].push_back(u); n   } n   dfs(1); n   int q; n   cin >> q; n   while (q--) { n     cin >> k; n     for (int i = 1; i <= k; i++) { n       cin >> ver[i]; n       important[ver[i]] = 1; n       t[i] = ver[i]; n     } n     sort(ver + 1, ver + k + 1, cmp); n     if (!buildGraph()) { n       cout << -1 << endl; n     } else { n       ans = 0; n       dfsSolve(ver[1]); n       cout << ans << endl; n     } n     for (int i = 1; i <= k; i++) important[t[i]] = 0; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n string ar; n string br; n int n = 0, lst = 0, lstmin = 0, lp = 0, lpc = 0; n int gzg = 0, rzg = 0, gzy2 = 0, rzy2 = 0; n int gzf = 0, gzc = 0, rzf = 0, rzc = 0; n int gzcc = 0, rzff = 0; n int main() { n   int f = 0; n   cin >> n; n   ar.reserve(n + 1); n   br.reserve(n + 1); n   cin >> ar; n   for (f = 0; f < n; f++) { n     if (lst == lstmin) lpc++; n     if (ar[f] ==  ( ) n       lst++; n     else n       lst--; n     if (lst < lstmin) { n       lstmin = lst; n       lp = f + 1; n       lpc = 0; n     } n   } n   if (lst != 0) { n     cout <<  0 n1 1 n ; n     return 0; n   } n   rzg = lpc; n   gzcc = lp; n   rzff = lp; n   for (f = lp; f < lp + n * 2 + 2; f++) { n     if (ar[f % n] ==  ( ) n       lst++; n     else n       lst--; n     switch (lst) { n       case 0: n         if (gzy2 <= gzg) { n           gzy2 = gzg; n           gzc = (gzcc + 1) % n + 1; n           gzf = f % n + 1; n         } n         gzg = 0; n         gzcc = f % n; n         break; n       case 1: n         gzg++; n         if (rzy2 <= rzg) { n           rzy2 = rzg; n           rzf = rzff % n + 1; n           rzc = f % n + 1; n         } n         rzff = f % n + 1; n         rzg = lpc; n         break; n       case 2: n         rzg++; n         break; n     } n   } n   if (rzy2 >= gzy2 && rzy2 > lpc) { n     cout << rzy2 <<   n  << rzf <<     << rzc <<   n ; n   } else if (gzy2 > rzy2 && gzy2 > lpc) { n     cout << gzy2 <<   n  << gzf <<     << gzc <<   n ; n   } else { n     cout << lpc <<   n1 1 n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int ToInt(string& s, int len) { n   int r = 0; n   for (int i = 0, max_ = (len); i < max_; ++i) { n     r *= 10; n     r += s[i] -  0 ; n   } n   return r; n } n int GCD(int a, int b) { return b != 0 ? GCD(b, a % b) : a; } n int LCM(int a, int b) { return a * (b / GCD(a, b)); } n long long Pow(long long n, long long e) { n   if (e == 0) return 1; n   if (e == 1) n     return n; n   else if (e & 1) { n     long long t = Pow(n, e / 2); n     return n * t * t; n   } else { n     long long t = Pow(n, e / 2); n     return t * t; n   } n } n int main() { n   string s; n   int N; n   scanf( %d , &N); n   getline(cin, s); n   getline(cin, s); n   int h = 0; n   for (int i = 0, max_ = ((int)(s).size()); i < max_; ++i) n     if (s[i] ==  H ) ++h; n   int min = 10000; n   for (int start = 0, max_ = ((int)(s).size()); start < max_; ++start) { n     int swaps = 0; n     for (int i = 0, max_ = (h); i < max_; ++i) n       if (s[(start + i) % (int)(s).size()] ==  T ) ++swaps; n     min = std::min(min, swaps); n   } n   printf( %d n , min); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int cnt[(100000 + 10)]; n int f[(100000 + 10)]; n int main() { n   int n; n   cin >> n; n   int ans = 0; n   int mx = 0; n   for (int i = 1; i <= n; i++) { n     int v; n     cin >> v; n     cnt[f[v]]--; n     f[v]++; n     cnt[f[v]]++; n     int ok = 0; n     mx = max(mx, f[v]); n     if (cnt[1] == i) { n       ok = 1; n     } else if (cnt[mx] == i) { n       ok = 1; n     } else if (cnt[mx] * mx == i - 1 && cnt[1] == 1) { n       ok = 1; n     } else if (cnt[mx - 1] * (mx - 1) == i - mx && cnt[mx] == 1) { n       ok = 1; n     } n     if (ok) { n       ans = i; n     } n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <class T> n inline void amin(T &x, const T &y) { n   if (y < x) x = y; n } n template <class T> n inline void amax(T &x, const T &y) { n   if (x < y) x = y; n } n template <class Iter> n void rprintf(const char *fmt, Iter begin, Iter end) { n   for (bool sp = 0; begin != end; ++begin) { n     if (sp) n       putchar(   ); n     else n       sp = true; n     printf(fmt, *begin); n   } n   putchar(  n ); n } n int A, B, C; n int N; n void MAIN() { n   scanf( %d%d%d%d , &A, &B, &C, &N); n   int ans = 0; n   for (int i = 0, i_len = (N); i < i_len; ++i) { n     int x; n     scanf( %d , &x); n     if (B < x && x < C) ans++; n   } n   printf( %d n , ans); n } n int main() { n   int TC = 1; n   for (int tc = 0, tc_len = (TC); tc < tc_len; ++tc) MAIN(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct art { n   int t, d, p, id; n } a[101]; n pair<int, int> go[101][2001]; n bool cmp(art xx, art yy) { return xx.d < yy.d; } n int f[101][2001], num[101][2001], ans[1000], tt; n int main() { n   int n; n   cin >> n; n   for (register int i = 1; i <= n; i++) n     cin >> a[i].t >> a[i].d >> a[i].p, a[i].id = i; n   sort(a + 1, a + n + 1, cmp); n   for (register int i = 1; i <= n; i++) n     for (register int k = 1; k <= 2000; k++) { n       if (k <= a[i].d && k - a[i].t > 0) { n         f[i][k] = f[i - 1][k - a[i].t] + a[i].p, n         go[i][k] = make_pair(i - 1, k - a[i].t), num[i][k] = i; n       } n       if (f[i][k - 1] > f[i][k]) { n         f[i][k] = f[i][k - 1]; n         go[i][k] = make_pair(i, k - 1), num[i][k] = -1; n       } n       if (f[i - 1][k] > f[i][k]) { n         f[i][k] = f[i - 1][k]; n         go[i][k] = make_pair(i - 1, k), num[i][k] = -1; n         ; n       } n     } n   cout << f[n][2000] << endl; n   int n1 = n, n2 = 2000; n   while (go[n1][n2].first) { n     if (num[n1][n2] != -1) ans[++tt] = a[num[n1][n2]].id; n     pair<int, int> cur = go[n1][n2]; n     n1 = cur.first; n     n2 = cur.second; n   } n   if (num[n1][n2] != -1 && num[n1][n2] != 0) ans[++tt] = a[num[n1][n2]].id; n   cout << tt << endl; n   for (register int i = tt; i; i--) cout << ans[i] <<    ; n   cout << endl; n   return 0; n } n


#include <bits/stdc++.h> n int main() { n   int i = 0, z, flag = 0, even = 0; n   char str[100002], temp; n   scanf( %s , str); n   int size = strlen(str); n   char x = str[size - 1]; n   for (i = 0; i < size; i++) { n     if (str[i] % 2 == 0) { n       even = 1; n       if (str[i] < x) { n         flag = 1; n         str[size - 1] = str[i]; n         str[i] = x; n         break; n       } else { n         z = i; n       } n     } n   } n   if (i >= size && even == 1) { n     flag = 1; n     str[size - 1] = str[z]; n     str[z] = x; n   } n   if (flag == 1) { n     printf( %s , str); n   } else n     printf( -1 ); n   return 0; n } n


#include <bits/stdc++.h> n #pragma GCC optimize( O3 ) n using namespace std; n long long n; n bool valid(long long x) { n   long long a = 0, b = 0; n   long long r = n; n   while (r) { n     a += min(r, x); n     r -= min(r, x); n     b += r / 10; n     r -= r / 10; n   } n   return a >= b; n } n signed main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   long long seed; n   asm( rdtsc  :  =A (seed)); n   srand(seed); n   cin >> n; n   long long lo = 0, hi = n; n   while (lo + 1 < hi) { n     long long mid = (lo + hi) / 2; n     if (valid(mid)) n       hi = mid; n     else n       lo = mid; n   } n   cout << lo + 1 <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename A, typename B> n string to_string(pair<A, B> p); n template <typename A, typename B, typename C> n string to_string(tuple<A, B, C> p); n template <typename A, typename B, typename C, typename D> n string to_string(tuple<A, B, C, D> p); n string to_string(const string& s) { return     + s +    ; } n string to_string(const char* s) { return to_string((string)s); } n string to_string(bool b) { return (b ?  true  :  false ); } n string to_string(vector<bool> v) { n   bool first = true; n   string res =  { ; n   for (int i = 0; i < static_cast<int>(v.size()); i++) { n     if (!first) { n       res +=  ,  ; n     } n     first = false; n     res += to_string(v[i]); n   } n   res +=  } ; n   return res; n } n template <size_t N> n string to_string(bitset<N> v) { n   string res =   ; n   for (size_t i = 0; i < N; i++) { n     res += static_cast<char>( 0  + v[i]); n   } n   return res; n } n template <typename A> n string to_string(A v) { n   bool first = true; n   string res =  { ; n   for (const auto& x : v) { n     if (!first) { n       res +=  ,  ; n     } n     first = false; n     res += to_string(x); n   } n   res +=  } ; n   return res; n } n template <typename A, typename B> n string to_string(pair<A, B> p) { n   return  (  + to_string(p.first) +  ,   + to_string(p.second) +  ) ; n } n template <typename A, typename B, typename C> n string to_string(tuple<A, B, C> p) { n   return  (  + to_string(get<0>(p)) +  ,   + to_string(get<1>(p)) +  ,   + n          to_string(get<2>(p)) +  ) ; n } n template <typename A, typename B, typename C, typename D> n string to_string(tuple<A, B, C, D> p) { n   return  (  + to_string(get<0>(p)) +  ,   + to_string(get<1>(p)) +  ,   + n          to_string(get<2>(p)) +  ,   + to_string(get<3>(p)) +  ) ; n } n void debug_out() { cerr << endl; } n template <typename Head, typename... Tail> n void debug_out(Head H, Tail... T) { n   cerr <<     << to_string(H); n   debug_out(T...); n } n template <typename T> n T inverse(T a, T m) { n   T u = 0, v = 1; n   while (a != 0) { n     T t = m / a; n     m -= t * a; n     swap(a, m); n     u -= t * v; n     swap(u, v); n   } n   assert(m == 1); n   return u; n } n template <typename T> n class Modular { n  public: n   using Type = typename decay<decltype(T::value)>::type; n   constexpr Modular() : value() {} n   template <typename U> n   Modular(const U& x) { n     value = normalize(x); n   } n   template <typename U> n   static Type normalize(const U& x) { n     Type v; n     if (-mod() <= x && x < mod()) n       v = static_cast<Type>(x); n     else n       v = static_cast<Type>(x % mod()); n     if (v < 0) v += mod(); n     return v; n   } n   const Type& operator()() const { return value; } n   template <typename U> n   explicit operator U() const { n     return static_cast<U>(value); n   } n   constexpr static Type mod() { return T::value; } n   Modular& operator+=(const Modular& other) { n     if ((value += other.value) >= mod()) value -= mod(); n     return *this; n   } n   Modular& operator-=(const Modular& other) { n     if ((value -= other.value) < 0) value += mod(); n     return *this; n   } n   template <typename U> n   Modular& operator+=(const U& other) { n     return *this += Modular(other); n   } n   template <typename U> n   Modular& operator-=(const U& other) { n     return *this -= Modular(other); n   } n   Modular& operator++() { return *this += 1; } n   Modular& operator--() { return *this -= 1; } n   Modular operator++(int) { n     Modular result(*this); n     *this += 1; n     return result; n   } n   Modular operator--(int) { n     Modular result(*this); n     *this -= 1; n     return result; n   } n   Modular operator-() const { return Modular(-value); } n   template <typename U = T> n   typename enable_if<is_same<typename Modular<U>::Type, int>::value, n                      Modular>::type& n   operator*=(const Modular& rhs) { n     value = normalize(static_cast<int64_t>(value) * n                       static_cast<int64_t>(rhs.value)); n     return *this; n   } n   template <typename U = T> n   typename enable_if<is_same<typename Modular<U>::Type, int64_t>::value, n                      Modular>::type& n   operator*=(const Modular& rhs) { n     int64_t q = static_cast<int64_t>(static_cast<long double>(value) * n                                      rhs.value / mod()); n     value = normalize(value * rhs.value - q * mod()); n     return *this; n   } n   template <typename U = T> n   typename enable_if<!is_integral<typename Modular<U>::Type>::value, n                      Modular>::type& n   operator*=(const Modular& rhs) { n     value = normalize(value * rhs.value); n     return *this; n   } n   Modular& operator/=(const Modular& other) { n     return *this *= Modular(inverse(other.value, mod())); n   } n   template <typename U> n   friend const Modular<U>& abs(const Modular<U>& v) { n     return v; n   } n   template <typename U> n   friend bool operator==(const Modular<U>& lhs, const Modular<U>& rhs); n   template <typename U> n   friend bool operator<(const Modular<U>& lhs, const Modular<U>& rhs); n   template <typename U> n   friend std::istream& operator>>(std::istream& stream, Modular<U>& number); n  n  private: n   Type value; n }; n template <typename T> n bool operator==(const Modular<T>& lhs, const Modular<T>& rhs) { n   return lhs.value == rhs.value; n } n template <typename T, typename U> n bool operator==(const Modular<T>& lhs, U rhs) { n   return lhs == Modular<T>(rhs); n } n template <typename T, typename U> n bool operator==(U lhs, const Modular<T>& rhs) { n   return Modular<T>(lhs) == rhs; n } n template <typename T> n bool operator!=(const Modular<T>& lhs, const Modular<T>& rhs) { n   return !(lhs == rhs); n } n template <typename T, typename U> n bool operator!=(const Modular<T>& lhs, U rhs) { n   return !(lhs == rhs); n } n template <typename T, typename U> n bool operator!=(U lhs, const Modular<T>& rhs) { n   return !(lhs == rhs); n } n template <typename T> n bool operator<(const Modular<T>& lhs, const Modular<T>& rhs) { n   return lhs.value < rhs.value; n } n template <typename T> n Modular<T> operator+(const Modular<T>& lhs, const Modular<T>& rhs) { n   return Modular<T>(lhs) += rhs; n } n template <typename T, typename U> n Modular<T> operator+(const Modular<T>& lhs, U rhs) { n   return Modular<T>(lhs) += rhs; n } n template <typename T, typename U> n Modular<T> operator+(U lhs, const Modular<T>& rhs) { n   return Modular<T>(lhs) += rhs; n } n template <typename T> n Modular<T> operator-(const Modular<T>& lhs, const Modular<T>& rhs) { n   return Modular<T>(lhs) -= rhs; n } n template <typename T, typename U> n Modular<T> operator-(const Modular<T>& lhs, U rhs) { n   return Modular<T>(lhs) -= rhs; n } n template <typename T, typename U> n Modular<T> operator-(U lhs, const Modular<T>& rhs) { n   return Modular<T>(lhs) -= rhs; n } n template <typename T> n Modular<T> operator*(const Modular<T>& lhs, const Modular<T>& rhs) { n   return Modular<T>(lhs) *= rhs; n } n template <typename T, typename U> n Modular<T> operator*(const Modular<T>& lhs, U rhs) { n   return Modular<T>(lhs) *= rhs; n } n template <typename T, typename U> n Modular<T> operator*(U lhs, const Modular<T>& rhs) { n   return Modular<T>(lhs) *= rhs; n } n template <typename T> n Modular<T> operator/(const Modular<T>& lhs, const Modular<T>& rhs) { n   return Modular<T>(lhs) /= rhs; n } n template <typename T, typename U> n Modular<T> operator/(const Modular<T>& lhs, U rhs) { n   return Modular<T>(lhs) /= rhs; n } n template <typename T, typename U> n Modular<T> operator/(U lhs, const Modular<T>& rhs) { n   return Modular<T>(lhs) /= rhs; n } n template <typename T, typename U> n Modular<T> power(const Modular<T>& a, const U& b) { n   assert(b >= 0); n   Modular<T> x = a, res = 1; n   U p = b; n   while (p > 0) { n     if (p & 1) res *= x; n     x *= x; n     p >>= 1; n   } n   return res; n } n template <typename T> n bool IsZero(const Modular<T>& number) { n   return number() == 0; n } n template <typename T> n string to_string(const Modular<T>& number) { n   return to_string(number()); n } n template <typename T> n std::ostream& operator<<(std::ostream& stream, const Modular<T>& number) { n   return stream << number(); n } n template <typename T> n std::istream& operator>>(std::istream& stream, Modular<T>& number) { n   typename common_type<typename Modular<T>::Type, int64_t>::type x; n   stream >> x; n   number.value = Modular<T>::normalize(x); n   return stream; n } n constexpr int md = 998244353; n using Mint = Modular<std::integral_constant<decay<decltype(md)>::type, md>>; n vector<Mint> fact(1, 1); n vector<Mint> inv_fact(1, 1); n Mint C(int n, int k) { n   if (k < 0 || k > n) { n     return 0; n   } n   while ((int)fact.size() < n + 1) { n     fact.push_back(fact.back() * (int)fact.size()); n     inv_fact.push_back(1 / fact.back()); n   } n   return fact[n] * inv_fact[k] * inv_fact[n - k]; n } n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   int n, m; n   cin >> n >> m; n   C(n + m, 0); n   auto Prob = [&](int i, int p) { n     Mint ret = fact[i] * inv_fact[p] * m * inv_fact[i + m] * fact[m - 1 + p]; n     return ret; n   }; n   vector<Mint> dp(n + 1); n   dp[0] = 0; n   Mint c1 = 0; n   Mint ci = 0; n   for (int i = 0; i <= n; i++) { n     Mint q = Mint(1) / (i + m + 1); n     if (i > 0) { n       dp[i] = c1 + ci * i; n       dp[i] *= m * fact[i] * inv_fact[i + m] * q; n       Mint rest = Prob(i, i); n       dp[i] = (dp[i] + rest * (1 + q * (n - i))) / (1 - rest); n     } n     int p = i; n     c1 += (dp[p] * (m + 1) + Mint(1 - p) * (n + m + 1)) * inv_fact[p] * n           fact[m - 1 + p]; n     ci += (dp[p] + (n + m + 1)) * inv_fact[p] * fact[m - 1 + p]; n   } n   cout << dp[n] <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long N = 1e5 + 7; n int T, n, t, a[N]; n map<long long, long long> mp; n int main() { n   scanf( %d , &T); n   while (T--) { n     mp.clear(); n     scanf( %d%d , &n, &t); n     for (int i = 1; i <= n; ++i) { n       scanf( %d , &a[i]); n     } n     if (t % 2 == 1) { n       for (int i = 1; i <= n; ++i) { n         mp[a[i]] = 1; n         mp[t - a[i]] = 0; n       } n       for (int i = 1; i <= n; ++i) { n         printf( %d  , mp[a[i]]); n       } n       printf(  n ); n     } else { n       for (int i = 1; i <= n; ++i) { n         mp[a[i]] = 1; n         mp[t - a[i]] = 0; n       } n       int flag = 0; n       for (int i = 1; i <= n; ++i) { n         if (a[i] * 2 == t) { n           printf( %d  , flag); n           flag = 1 - flag; n         } else { n           printf( %d  , mp[a[i]]); n         } n       } n       printf(  n ); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 1000010; n int dp[maxn][2]; n int n, cnt = 1, head[maxn], to[maxn], nxt[maxn], val[maxn]; n inline void add(int x, int y, int z) { n   nxt[++cnt] = head[x]; n   head[x] = cnt; n   to[cnt] = y; n   val[cnt] = z; n } n void dfs(int x, int fa) { n   for (int i = head[x]; i; i = nxt[i]) { n     if (to[i] == fa) continue; n     dfs(to[i], x); n     dp[x][0] += dp[to[i]][0] + val[i]; n   } n } n void dfs1(int x, int fa) { n   for (int i = head[x]; i; i = nxt[i]) { n     if (to[i] == fa) continue; n     dp[to[i]][1] = dp[x][0] - dp[to[i]][0] - val[i] + dp[x][1] + val[i ^ 1]; n     dfs1(to[i], x); n   } n } n int st[maxn], top; n int main() { n   cin >> n; n   for (int i = 1, x, y; i < n; ++i) { n     cin >> x >> y; n     add(x, y, 0); n     add(y, x, 1); n   } n   dfs(1, 1); n   dfs1(1, 1); n   int tot = 1e9; n   for (int i = 1; i <= n; ++i) { n     tot = min(tot, dp[i][0] + dp[i][1]); n   } n   for (int i = 1; i <= n; ++i) { n     if (dp[i][0] + dp[i][1] == tot) { n       st[++top] = i; n     } n   } n   cout << tot << endl; n   for (int i = 1; i <= top; ++i) { n     cout << st[i] <<    ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int T; n   cin >> T; n   while (T--) { n     int a, b, c; n     scanf( %d%d%d , &a, &b, &c); n     int temp = 0; n     if (a == b && b == c) { n       printf( 0 n ); n     } else { n       if (b > a) { n         temp = b; n         b = a; n         a = temp; n       } n       if (c > a) { n         temp = c; n         c = a; n         a = temp; n       } n       if (b >= c) { n         printf( %d n , max(2 * (a - c) - 4, 0)); n       } else { n         printf( %d n , max(2 * (a - b) - 4, 0)); n       } n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n int fireball1, fireball2, health[100], attack[100], attack1[100]; n int n, sum = 2e+8; n void dfs(int x, int y) { n   if (y >= sum) return; n   if (x == n) { n     sum = y; n     for (int i = 1; i <= n; i++) attack[i] = attack1[i]; n     return; n   } n   for (int i = 0; i <= 100; i++) { n     if (y + i > sum) break; n     if (health[x - 1] - fireball2 * i >= 0) continue; n     if (x == n - 1) n       if (health[n] - fireball2 * i >= 0) continue; n     attack1[x] = i; n     health[x - 1] -= fireball2 * i; n     health[x] -= fireball1 * i; n     health[x + 1] -= fireball2 * i; n     dfs(x + 1, y + i); n     health[x - 1] += fireball2 * i; n     health[x] += fireball1 * i; n     health[x + 1] += fireball2 * i; n     if (health[x] - fireball1 * i < 0 && health[x + 1] - fireball2 * i < 0) n       break; n   } n } n int main() { n   scanf( %d%d%d , &n, &fireball1, &fireball2); n   for (int i = 1; i <= n; i++) scanf( %d , &health[i]); n   dfs(2, 0); n   printf( %d n , sum); n   for (int i = 2; i < n; i++) n     for (int j = 1; j <= attack[i]; j++) printf( %d  , i); n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 300000 + 3000; n int n, m; n int a[maxn], l[maxn], r[maxn], pos[maxn]; n struct Segtree { n   int l, r; n   int val; n } node[maxn << 3]; n inline int read() { n   int x; n   scanf( %d , &x); n   return x; n } n inline void build(int p, int l, int r) { n   node[p].l = l, node[p].r = r; n   node[p].val = 0; n   if (l == r) return; n   int mid = (l + r) >> 1; n   build(p << 1, l, mid); n   build(p << 1 | 1, mid + 1, r); n } n inline void update(int p) { n   node[p].val = node[p << 1].val + node[p << 1 | 1].val; n } n inline void change(int p, int vl, int vr, int c) { n   if (node[p].l == vl && node[p].r == vr) { n     node[p].val += c; n     return; n   } n   int mid = (node[p].l + node[p].r) >> 1; n   if (vr <= mid) n     change(p << 1, vl, vr, c); n   else n     change(p << 1 | 1, vl, vr, c); n   update(p); n } n inline int query(int p, int vl, int vr) { n   if (node[p].l == vl && node[p].r == vr) return node[p].val; n   int mid = (node[p].l + node[p].r) >> 1; n   if (vr <= mid) n     return query(p << 1, vl, vr); n   else if (vl > mid) n     return query(p << 1 | 1, vl, vr); n   else n     return query(p << 1, vl, mid) + query(p << 1 | 1, mid + 1, vr); n } n int main() { n   n = read(), m = read(); n   build(1, 1, n + m); n   for (int i = 1; i <= n; ++i) n     change(1, m + i, m + i, 1), pos[i] = m + i, l[i] = r[i] = i; n   for (int i = 1; i <= m; ++i) { n     scanf( %d , &a[i]); n     l[a[i]] = min(1, l[a[i]]); n     r[a[i]] = max(r[a[i]], query(1, 1, pos[a[i]])); n     change(1, pos[a[i]], pos[a[i]], -1); n     pos[a[i]] = m - i + 1; n     change(1, pos[a[i]], pos[a[i]], 1); n   } n   for (int i = 1; i <= n; ++i) { n     r[i] = max(r[i], query(1, 1, pos[i])); n     printf( %d %d n , l[i], r[i]); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n inline int read() { n   char c = getchar(); n   int x = 0; n   bool f = 0; n   for (; !isdigit(c); c = getchar()) f ^= !(c ^ 45); n   for (; isdigit(c); c = getchar()) x = (x << 1) + (x << 3) + (c ^ 48); n   if (f) x = -x; n   return x; n } n int n, m, k, s; n vector<int> e[200005]; n vector<int> dfn, res[200005]; n bool vis[200005]; n void dfs(int u) { n   vis[u] = 1; n   dfn.push_back(u); n   for (auto v : e[u]) n     if (!vis[v]) dfs(v), dfn.push_back(u); n } n signed main() { n   n = read(), m = read(), k = read(), s = ceil(2.0 * n / k); n   for (register int i = (1); i <= (m); ++i) { n     int u = read(), v = read(); n     e[u].push_back(v), e[v].push_back(u); n   } n   dfs(1); n   for (register int i = (0); i <= ((int)dfn.size() - 1); ++i) n     res[i / s + 1].push_back(dfn[i]); n   for (register int i = (1); i <= (k); ++i) { n     if (!res[i].size()) res[i].push_back(1); n     cout << res[i].size() <<    ; n     for (auto x : res[i]) cout << x <<    ; n     puts(  ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct sb { n   long long x; n   long long y; n   bool operator<(const sb &wakaka) const { return x < wakaka.x; } n } test; n set<sb> node; n int main() { n   long long x; n   cin >> x; n   long long len = 2 * pow((double)x, (double)1 / (double)3); n   long long cnt = 0; n   long long n = 0; n   while (1) { n     n++; n     if (n > len) break; n     long long en = x * 6; n     long long i2 = (n - 1) * n * (2 * n - 1); n     en = en - i2; n     long long he = (n - 1) * n * 3; n     en = en + he * n; n     long long la = 6 * n * n - he; n     if (en % la == 0) { n       long long ans = en / la; n       test.x = n; n       test.y = ans; n       if (ans < n) break; n       node.insert(test); n       test.y = n; n       test.x = ans; n       node.insert(test); n       cnt = cnt + 2; n     } n   } n   cout << node.size() << endl; n   for (set<sb>::iterator it = node.begin(); it != node.end(); it++) { n     test = *it; n     cout << test.x <<     << test.y << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n using namespace std; n int n, a, b, ans; n int main() { n   cin >> n; n   while (n--) { n     cin >> a >> b; n     a++, b--; n     while (b /= 4) a++; n     ans = max(ans, a); n   } n   cout << ans; n } n


#include <bits/stdc++.h> n #pragma GCC optimize( Ofast ) n #pragma GCC target( avx2 ) n #pragma GCC optimization( O3 ) n #pragma GCC optimization( unroll-loops ) n using namespace std; n const int N = 30 + 7; n const int mod = 1e9 + 7; n const int B = 317; n int n; n int u, r; n int a[N][N]; n int b[N]; n int c[N]; n int p[N]; n long long foo(int x, bool f) { n   long long s = -1e18; n   if (x % 2 == 0) { n     s = 0; n     for (int i = 1; i <= n; i++) s += a[x][i] * 1LL * c[i]; n   } n   if (x == 0) return s; n   if (f) { n     for (int i = 1; i <= n; i++) { n       a[x - 1][i] = a[x][i] ^ b[i]; n     } n     s = max(s, foo(x - 1, false)); n   } n   for (int i = 1; i <= n; i++) { n     a[x - 1][i] = a[x][p[i]] + r; n   } n   s = max(s, foo(x - 1, true)); n   return s; n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   cin >> n >> u >> r; n   for (int i = 1; i <= n; i++) cin >> a[u][i]; n   for (int i = 1; i <= n; i++) cin >> b[i]; n   for (int i = 1; i <= n; i++) cin >> c[i]; n   for (int i = 1; i <= n; i++) cin >> p[i]; n   cout << foo(u, true); n } n


#include <bits/stdc++.h> n using namespace std; n long long ans; n int n, l, w, x, pn; n vector<int> pos, neg; n int cal(long long a, int b) { n   if (b == 0) return (a > 0 ? INT_MAX : -INT_MAX); n   if (a < 0) { n     if (a % b != 0) return (a / b - 1); n   } n   return a / b; n } n int main() { n   ios::sync_with_stdio(false); n   scanf( %d%d%d , &n, &l, &w); n   for (int i = 1; i <= n; i++) { n     scanf( %d%d , &x, &pn); n     if (pn == -1) n       neg.push_back(x); n     else n       pos.push_back(x); n   } n   sort(neg.begin(), neg.end()); n   sort(pos.begin(), pos.end()); n   for (int i = 0; i < neg.size(); i++) { n     int v = neg[i]; n     auto now = lower_bound(pos.begin(), pos.end(), -v - l); n     int max1 = cal((long long)(v + l) * (w + 1) - 1, w - 1); n     int max2 = cal((long long)(v + l) * (w - 1) - 1, w + 1); n     ans += (upper_bound(pos.begin(), now, max1) - pos.begin()) + n            (upper_bound(now, pos.end(), min(max2, v)) - now); n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n map<pair<int, int>, int> mp; n set<int> s; n int m; n pair<int, int> p[100001]; n long long ans; n const long long md = 1e9 + 9; n int up[3][2] = {-1, 1, 0, 1, 1, 1}; n int dwn[3][2] = {-1, -1, 0, -1, 1, -1}; n int cnt(int ind) { n   int res = 0; n   int x = p[ind].first, y = p[ind].second; n   for (int i = 0; i < 3; i++) { n     int xx = x + dwn[i][0], yy = y + dwn[i][1]; n     if (mp.find(make_pair(xx, yy)) != mp.end()) res++; n   } n   return res; n } n bool chk(int ind) { n   int x = p[ind].first, y = p[ind].second; n   for (int i = 0; i < 3; i++) { n     int xx = x + up[i][0], yy = y + up[i][1]; n     if (mp.find(make_pair(xx, yy)) != mp.end()) { n       int d = mp[make_pair(xx, yy)]; n       if (cnt(d) <= 1) { n         return false; n       } n     } n   } n   return true; n } n void work(int ind) { n   ans = (ans * (long long)m % md + ind) % md; n   int x = p[ind].first, y = p[ind].second; n   mp.erase(make_pair(x, y)); n   for (int i = 0; i < 3; i++) { n     int xx = x + dwn[i][0], yy = y + dwn[i][1]; n     if (mp.find(make_pair(xx, yy)) != mp.end()) { n       int d = mp[make_pair(xx, yy)]; n       if (chk(d)) { n         s.insert(d); n       } n     } n   } n } n void mx() { n   set<int>::iterator it; n   int x; n   while (!s.empty()) { n     it = s.end(); n     it--; n     if (chk(*it)) { n       x = *it; n       s.erase(it); n       break; n     } n     s.erase(it); n   } n   work(x); n } n void mn() { n   set<int>::iterator it; n   int x; n   while (!s.empty()) { n     it = s.begin(); n     if (chk(*it)) { n       x = *it; n       s.erase(it); n       break; n     } n     s.erase(it); n   } n   work(x); n } n int main() { n   scanf( %d , &m); n   for (int i = 0; i < m; i++) { n     scanf( %d%d , &p[i].first, &p[i].second); n     mp[make_pair(p[i].first, p[i].second)] = i; n     s.insert(i); n   } n   for (int i = 0; i < m; i++) { n     if (i & 1) n       mn(); n     else n       mx(); n   } n   printf( %lld , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long maxn = 50050; n const long long bign = 55000; n long long n, m; n long long tmp[bign], judge[bign]; n long long ans, head[maxn], sz[maxn], maxp[maxn]; n long long dis[bign], size; n long long q[bign], targetnum; n long long rt, tot, vis[maxn]; n struct edge { n   long long next, to, dis; n } e[maxn * 2]; n inline void addedge(long long next, long long to, long long dis) { n   e[++size].to = to; n   e[size].dis = dis; n   e[size].next = head[next]; n   head[next] = size; n } n void getzx(long long t, long long fat) { n   long long i, j; n   maxp[t] = sz[t] = 1; n   for (i = head[t]; i; i = e[i].next) { n     j = e[i].to; n     if (j == fat || vis[j]) continue; n     getzx(j, t); n     sz[t] += sz[j]; n     maxp[t] = max(maxp[t], sz[j]); n   } n   maxp[t] = max(maxp[t], tot - sz[t]); n   if (maxp[t] < maxp[rt]) rt = t; n } n inline void getdis(long long t, long long fat) { n   long long i, j, k; n   tmp[++tmp[0]] = dis[t]; n   for (i = head[t]; i; i = e[i].next) { n     j = e[i].to; n     k = e[i].dis; n     if (j == fat || vis[j]) continue; n     dis[j] = dis[t] + k; n     getdis(j, t); n   } n } n inline void calc(long long t) { n   long long i, j, k, l, p = 0; n   for (i = head[t]; i; i = e[i].next) { n     j = e[i].to; n     k = e[i].dis; n     if (vis[j]) continue; n     dis[j] = k; n     tmp[0] = 0; n     getdis(j, t); n     for (k = tmp[0]; k; k--) n       if (targetnum >= tmp[k]) ans += judge[targetnum - tmp[k]]; n     for (k = tmp[0]; k; k--) { n       q[++p] = tmp[k]; n       judge[tmp[k]]++; n     } n   } n   for (k = p; k; k--) judge[k] = 0; n } n inline void solve(long long t) { n   long long i, j; n   vis[t] = judge[0] = 1; n   calc(t); n   for (i = head[t]; i; i = e[i].next) { n     j = e[i].to; n     if (vis[j]) continue; n     tot = sz[j]; n     maxp[rt = 0] = 0x3f3f3f3f; n     getzx(j, 0); n     solve(rt); n   } n } n signed main() { n   ios::sync_with_stdio(false); n   register long long i, j; n   cin >> n >> targetnum; n   long long t1, t2; n   for (i = 1; i < n; i++) { n     cin >> t1 >> t2; n     addedge(t1, t2, 1); n     addedge(t2, t1, 1); n   } n   maxp[rt = 0] = 0x3f3f3f3f; n   tot = n; n   getzx(1, 0); n   solve(rt); n   cout << ans << endl; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 12, mod = 1e9 + 7; n long long dp[maxn][2][maxn]; n int num[maxn]; n long long dfs(int pos, bool bnd, int ned) { n   if (ned < 0) return 0; n   if (pos < 0) return ned == 0; n   long long& ndp = dp[pos][bnd][ned]; n   if (~ndp) return ndp; n   ndp = 0; n   int bound = bnd ? num[pos] : 9; n   for (int i = 0; i <= bound; i++) n     ndp += dfs(pos - 1, bnd && i == bound, ned - (i == 4 || i == 7)); n   return ndp; n } n long long cnt[maxn]; n int init(long long x) { n   int len = 0; n   while (x) { n     num[len++] = x % 10; n     x /= 10; n   } n   memset(dp, -1, sizeof(dp)); n   for (int i = len; i >= 0; i--) { n     cnt[i] = dfs(len - 1, true, i); n   } n   cnt[0]--; n   return len; n } n long long ans; n void dfs(int st, long long v, int len, int adder) { n   if (st == 7) { n     for (int i = adder + 1; i <= len; i++) { n       (ans += v * cnt[i] % mod) %= mod; n     } n     return; n   } n   if (adder >= len) return; n   for (int i = 0; i <= len; i++) { n     if (cnt[i] == 0) continue; n     long long mid = v * cnt[i] % mod; n     cnt[i]--; n     dfs(st + 1, mid, len, adder + i); n     cnt[i]++; n   } n } n int main() { n   long long n; n   scanf( %I64d , &n); n   int len = init(n); n   ans = 0; n   dfs(1, 1, len, 0); n   printf( %I64d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int arr[100][100]; n bool dp[100][100][1000]; n bool W[100][100][1000]; n int main() { n   int N; n   int M; n   int K; n   scanf( %d %d %d , &N, &M, &K); n   K++; n   char ch[101]; n   for (int n = 0; n < N; n++) { n     scanf( %s , ch); n     for (int m = 0; m < M; m++) { n       arr[n][m] = ch[m] -  0 ; n     } n   } n   memset(dp, false, sizeof(dp)); n   for (int m = 0; m < M; m++) { n     dp[N - 1][m][arr[N - 1][m]] = true; n   } n   for (int n = N - 2; n >= 0; n--) { n     for (int m = 0; m < M; m++) { n       int y[] = {n + 1, n + 1}; n       int x[] = {m - 1, m + 1}; n       for (int pos = 0; pos < 2; pos++) { n         int Y = y[pos]; n         int X = x[pos]; n         if (Y >= 0 && Y < N && X >= 0 && X < M) { n           for (int k = 0; k < 1000; k++) { n             if (dp[Y][X][k]) { n               dp[n][m][k + arr[n][m]] = true; n               W[n][m][k + arr[n][m]] = pos; n             } n           } n         } n       } n     } n   } n   int sol = -1; n   int pos = -1; n   for (int k = 0; k < 1000; k += K) { n     for (int m = 0; m < M; m++) { n       if (dp[0][m][k]) { n         sol = k; n         pos = m; n       } n     } n   } n   printf( %d n , sol); n   if (sol != -1) { n     int help = sol; n     stack<int> st; n     for (int n = 0; n < N - 1; n++) { n       st.push(W[n][pos][help]); n       if (W[n][pos][help] == 0) { n         help -= arr[n][pos]; n         pos--; n       } else { n         help -= arr[n][pos]; n         pos++; n       } n     } n     printf( %d n , pos + 1); n     while (!st.empty()) { n       if (st.top() == 0) { n         printf( R ); n       } else { n         printf( L ); n       } n       st.pop(); n     } n     printf(  n ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long len; n string a, b; n const long long mo = 1000000007; n long long good[200001], bad[200001]; n int x, y, n; n int main() { n   cin >> a >> b >> n; n   if (a.length() != b.length()) { n     cout << 0; n     return 0; n   } n   len = a.length(); n   bool f; n   for (int i = 0; i < len; i++) { n     f = false; n     for (int j = 0; j < len; j++) { n       if (b[j] != b[(j + i) % len]) { n         f = true; n         break; n       } n     } n     if (f == false) { n       x++; n     } else { n       y++; n     } n   } n   if (a == b) { n     good[0] = 1; n     bad[0] = 0; n   } else { n     good[0] = 0; n     bad[0] = 1; n   } n   f = false; n   b = b + b; n   bool p; n   for (int i = 0; i < len; i++) { n     p = false; n     for (int j = 0; j < len; j++) { n       if (b[j + i] != a[j]) { n         p = true; n         break; n       } n     } n     if (p == false) { n       f = true; n       break; n     } n   } n   if (f == false) { n     cout << 0; n     return 0; n   } n   for (int i = 1; i <= n; i++) { n     good[i] = n         ((good[i - 1] % mo) * ((x - 1) % mo) + (bad[i - 1] * x) % mo) % mo; n     bad[i] = n         ((good[i - 1] % mo) * (y % mo) + (bad[i - 1] % mo) * ((y - 1) % mo)) % n         mo; n   } n   for (int i = 1; i <= n; i++) { n   } n   cout << good[n] % mo; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 1e3 + 5; n unordered_map<bitset<maxn>, int> ma, flag; n bitset<maxn> s[35]; n int t1, t2; n void dfs1(bitset<maxn> z, int cost, int pos) { n   if (pos > t1 + t2) { n     if (flag[z] == 0) n       ma[z] = cost; n     else n       ma[z] = min(ma[z], cost); n     flag[z] = 1; n     return; n   } n   dfs1(z ^ s[pos], cost + 1, pos + 1); n   dfs1(z, cost, pos + 1); n } n int dfs2(bitset<maxn> z, int cost, int pos) { n   int res = 1e9; n   if (flag[z] != 0) res = cost + ma[z]; n   if (pos <= t1) { n     res = min(res, dfs2(z ^ s[pos], cost + 1, pos + 1)); n     res = min(res, dfs2(z, cost, pos + 1)); n   } n   return res; n } n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   int n, m, d; n   cin >> n >> m >> d; n   t1 = m / 3; n   t2 = m - t1; n   for (int i = 1; i <= m; i++) { n     int cnt; n     cin >> cnt; n     for (int j = 1; j <= cnt; j++) { n       int x; n       cin >> x; n       s[i][x] = 1; n     } n   } n   bitset<maxn> tmp; n   dfs1(tmp, 0, t1 + 1); n   while (d--) { n     int cnt; n     cin >> cnt; n     bitset<maxn> q; n     for (int i = 1; i <= cnt; i++) { n       int x; n       cin >> x; n       q[x] = 1; n     } n     int ans = dfs2(q, 0, 1); n     if (ans == 1e9) n       cout << -1 <<   n ; n     else n       cout << ans <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n constexpr int maxn = 3e3 + 4; n struct Node { n   int la, nx; n } nodes[maxn]; n int X[maxn], Y[maxn]; n std::vector<int> yp[maxn]; n int R, C, N, K; n int ordX[maxn], ord[maxn]; n int ans[maxn], xans = 0; n inline void update(int x) { n   int c = x; n   for (int k = 1; k < K && c; ++k) c = nodes[c].nx; n   xans -= ans[x]; n   if (!nodes[x].la) n     ans[x] = X[c] * (R + 1 - X[x]); n   else n     ans[x] = X[c] * (X[nodes[x].la] - X[x]); n   xans += ans[x]; n } n int main() { n   cin >> R >> C >> N >> K; n   for (int i = 1; i <= N; ++i) { n     cin >> X[i] >> Y[i]; n     yp[Y[i]].push_back(i); n   } n   iota(ordX + 1, ordX + N + 1, 1); n   sort(ordX + 1, ordX + N + 1, [&](int a, int b) { return X[a] > X[b]; }); n   long long rans = 0; n   for (int ups = 1; ups <= C; ++ups) { n     xans = 0; n     memset(ans, 0, sizeof(ans)); n     int cn = 0; n     for (int i = 1; i <= N; ++i) { n       if (Y[ordX[i]] >= ups) ord[++cn] = ordX[i]; n     } n     memset(nodes, 0, sizeof(nodes)); n     for (int i = 1; i <= cn; ++i) { n       nodes[ord[i]].la = ord[i - 1]; n       nodes[ord[i - 1]].nx = ord[i]; n     } n     for (int i = 1; i <= cn; ++i) update(ord[i]); n     rans += xans; n     for (int i = C; i > ups; --i) { n       for (int j : yp[i]) { n         nodes[nodes[j].la].nx = nodes[j].nx; n         nodes[nodes[j].nx].la = nodes[j].la; n       } n       for (int j : yp[i]) { n         for (int k = 1, c = nodes[j].la; k < K && c; ++k, c = nodes[c].la) n           update(c); n         if (nodes[j].nx) update(nodes[j].nx); n         xans -= ans[j]; n       } n       rans += xans; n     } n   } n   cout << rans << endl; n   return 0; n } n


#include <bits/stdc++.h> n #pragma comment(linker,  /stack:247474112 ) n #pragma GCC optimize( Ofast ) n using namespace std; n string ans[2] = { Bob ,  Alice }; n void solve() { n   long long n, k; n   cin >> n >> k; n   if (k % 3 != 0) { n     if (n % 3) n       cout << ans[1]; n     else n       cout << ans[0]; n     return; n   } n   long long per_cycle = (k / 3 - 1) * 3 + 4; n   n %= per_cycle; n   if (n == 0) n = per_cycle; n   if (n <= (k / 3 - 1) * 3) { n     if (n % 3) n       cout << ans[1]; n     else n       cout << ans[0]; n   } else { n     n -= (k / 3 - 1) * 3; n     if (n != 4) n       cout << ans[1]; n     else n       cout << ans[0]; n   } n } n int32_t main() { n   ios::sync_with_stdio(0), cin.tie(0), cout.tie(0); n   long long t; n   cin >> t; n   while (t--) { n     solve(); n     cout <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 3000 + 50; n const int maxm = 3000 + 50; n const int maxt = 3000000 + 100; n const int maxk = 10 + 3; n const long long unit = 1LL; n const int INF = 0x3f3f3f3f; n const long long Inf = 0x3f3f3f3f3f3f3f3fLL; n const double eps = 1e-3; n const double inf = 1e15; n const double pi = acos(-1.0); n const long long mod = 1000000007LL; n inline long long read() { n   long long x = 0; n   bool t = false; n   char ch = getchar(); n   while ((ch <  0  || ch >  9 ) && ch !=  - ) ch = getchar(); n   if (ch ==  - ) t = true, ch = getchar(); n   while (ch <=  9  && ch >=  0 ) x = x * 10 + ch - 48, ch = getchar(); n   return t ? -x : x; n } n inline long long add(long long x, long long y) { n   return x + y >= mod ? x + y - mod : x + y; n } n inline long long sub(long long x, long long y) { n   return x < y ? x - y + mod : x - y; n } n inline long long mul(long long x, long long y) { n   return x * y - x * y / mod * mod; n } n struct EasyMath { n   inline long long Quick_Pow(long long a, long long b) { n     long long sum = 1; n     for (; b; b >>= 1, a = (long long)a * a % mod) n       if (b & 1) sum = sum * a % mod; n     return sum; n   } n   inline long long inv(long long x) { return Quick_Pow(x, mod - 2); } n } em; n long long n, d; n long long f[maxn][maxn], g[maxn], s[maxn][maxn], C[maxn][maxn]; n long long inv[maxn], fac[maxn], ifac[maxn]; n void GetInv(int n) { n   inv[1] = fac[0] = fac[1] = ifac[0] = ifac[1] = 1; n   for (register int i = (2); i <= (n); ++i) { n     inv[i] = ((mod - mod / i)) * inv[mod % i] % mod; n     fac[i] = fac[i - 1] * i % mod; n     ifac[i] = ifac[i - 1] * inv[i] % mod; n   } n } n struct Edge { n   int to, next; n } edge[maxm]; n int tot, head[maxn]; n void init() { n   tot = 1; n   memset(head, 0, sizeof(head)); n } n void AddEdge(int u, int v) { n   edge[tot] = (Edge){v, head[u]}; n   head[u] = tot++; n } n void dfs(int u) { n   for (int i = head[u]; i; i = edge[i].next) { n     int v = edge[i].to; n     dfs(v); n     for (int j = 1; j <= n; ++j) (f[u][j] *= s[v][j]) %= mod; n   } n   for (int j = 1; j <= n; ++j) s[u][j] = (s[u][j - 1] + f[u][j]) % mod; n } n inline long long CC(long long n, long long m) { n   long long ans = 1; n   for (long long i = 0; i < m; ++i) n     (ans *= ((n - i) * inv[i + 1]) % mod) %= mod; n   return ans; n } n int main() { n   init(); n   n = read(), d = read(); n   GetInv(n + 10); n   C[0][0] = 1; n   for (register int i = (1); i <= (n); ++i) { n     C[i][0] = C[i][i] = 1; n     for (register int j = (1); j <= (n); ++j) { n       f[i][j] = 1; n     } n     for (register int j = (1); j <= (i - 1); ++j) { n       C[i][j] = (C[i - 1][j] + C[i - 1][j - 1]) % mod; n     } n   } n   int x; n   for (register int i = (2); i <= (n); ++i) { n     x = read(); n     AddEdge(x, i); n   } n   dfs(1); n   for (register int j = (1); j <= (n); ++j) { n     g[j] = f[1][j]; n     for (register int i = (1); i <= (j - 1); ++i) { n       g[j] = ((g[j] - C[j - 1][j - i] * g[i] % mod) % mod + mod) % mod; n     } n   } n   long long ans = 0; n   for (register int i = (1); i <= (min(n, d)); ++i) { n     (ans += CC(d, i) * g[i] % mod) %= mod; n   } n   printf( %lld n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   cin >> n; n   int a[n]; n   for (int i = 0; i < n; i++) { n     cin >> a[i]; n   } n   sort(a, a + n); n   for (int i = 0; i < n - 2; i++) { n     if (a[i] == a[i + 1] && a[i] == a[i + 2]) { n       cout <<  NO ; n       return 0; n     } n   } n   cout <<  YES  << endl; n   vector<int> v1, v2; n   v1.push_back(a[0]); n   for (int i = 1; i < n; i++) { n     if (a[i] != a[i - 1]) n       v1.push_back(a[i]); n     else n       v2.push_back(a[i]); n   } n   int p = v1.size(); n   int r = v2.size(); n   cout << p << endl; n   for (int i = 0; i < p; i++) { n     cout << v1[i] <<    ; n   } n   cout << endl; n   cout << r << endl; n   for (int i = r - 1; i >= 0; i--) { n     cout << v2[i] <<    ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int n; n int maxt[5210][5210]; n int a[5210][5210]; n char str[5210]; n int getn(char ch) { n   if (ch >=  0  && ch <=  9 ) { n     return ch -  0 ; n   } else { n     return 10 + ch -  A ; n   } n } n int main() { n   scanf( %d , &n); n   memset(maxt, 0, sizeof(maxt)); n   for (int i = 1; i <= n; i++) { n     scanf( %s , str + 1); n     int In = 0, len = strlen(str + 1); n     for (int j = len; j >= 1; j--) { n       int N = getn(str[j]); n       int Jn = 0; n       while (N) { n         a[i][n - In - Jn] = N % 2; n         N /= 2; n         Jn++; n       } n       for (; Jn < 4; Jn++) { n         if (n - In - Jn == 0) { n           break; n         } n         a[i][n - In - Jn] = 0; n       } n       In += Jn; n     } n     for (int j = n - In; j >= 1; j--) { n       a[i][j] = 0; n     } n   } n   for (int i = 1; i <= n; i++) { n     for (int j = 1; j <= n; j++) { n       maxt[i][j] = n           a[i][j] + maxt[i - 1][j] + maxt[i][j - 1] - maxt[i - 1][j - 1]; n     } n   } n   for (int I = n; I >= 1; I--) { n     if (n % I == 0) { n       int flag = 1; n       for (int i = 1; i <= (n / I); i++) { n         for (int j = 1; j <= (n / I); j++) { n           int cnt = maxt[i * I][j * I] - maxt[(i - 1) * I][j * I] - n                     maxt[i * I][(j - 1) * I] + maxt[(i - 1) * I][(j - 1) * I]; n           if (cnt != I * I * a[i * I][j * I]) { n             flag = 0; n             break; n           } n         } n         if (!flag) { n           break; n         } n       } n       if (flag) { n         printf( %d n , I); n         break; n       } n     } n   } n   return 0; n } n