#include <bits/stdc++.h> n using namespace std; n int n, m; n vector<int> ke[1000010]; n bool cx[1000010], visited[1000010]; n void dfs(int u) { n   visited[u] = true; n   for (int v : ke[u]) n     if (!visited[v]) dfs(v); n } n long long c2(int x) { return (1LL * x * (x - 1)) >> 1; } n int main() { n   if (fopen( a.txt ,  r )) { n     freopen( a.txt ,  r , stdin); n   } else n     ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cin >> n >> m; n   int own = 0; n   for (int i = 1; i <= m; i++) { n     int u, v; n     cin >> u >> v; n     cx[u] = true; n     cx[v] = true; n     if (u != v) { n       ke[u].push_back(v); n       ke[v].push_back(u); n     } else n       own++; n   } n   int cnt = 0; n   for (int u = 1; u <= n; u++) n     if (cx[u] && !visited[u]) { n       dfs(u); n       cnt++; n     } n   if (cnt != 1) { n     cout << 0; n     exit(0); n   }; n   long long res = 0; n   for (int i = 1; i <= n; i++) res += c2(int(ke[i].size())); n   res += c2(own); n   res += 1LL * own * (m - own); n   cout << res; n } n


#include <bits/stdc++.h> n using namespace std; n int get_point(int N) { n   int score[10] = {25, 18, 15, 12, 10, 8, 6, 4, 2, 1}; n   if (N > 10) return 0; n   return score[N - 1]; n } n int main() { n   int T; n   map<string, int> n_map; n   map<string, string> n_win; n   cin >> T; n   for (int i = 0; i < (T); ++i) { n     int N; n     cin >> N; n     for (int j = 0; j < (N); ++j) { n       string t_s; n       cin >> t_s; n       n_map[t_s] += get_point(j + 1); n       if (n_win[t_s] ==   ) n         n_win[t_s] =  0000000000000000000000000000000000000000000000000000 ; n       n_win[t_s][j] += 1; n     } n   } n   map<string, int>::iterator it; n   int n_max1 = 0, n_max2 = 0; n   string n_ret1 =   , n_ret2 =   ; n   n_win[  ] =  0000000000000000000000000000000000000000000000000000 ; n   for (it = n_map.begin(); it != n_map.end(); it++) { n     if (n_max1 < (*it).second) { n       n_ret1 = (*it).first; n       n_max1 = (*it).second; n     } n     if (n_max1 == (*it).second && n_win[n_ret1] < n_win[(*it).first]) { n       n_ret1 = (*it).first; n       n_max1 = (*it).second; n     } n     if (n_win[n_ret2][0] < n_win[(*it).first][0]) { n       n_ret2 = (*it).first; n       n_max2 = (*it).second; n     } n     if (n_win[n_ret2][0] == n_win[(*it).first][0] && n_max2 < (*it).second) { n       n_ret2 = (*it).first; n       n_max2 = (*it).second; n     } n     if (n_win[n_ret2][0] == n_win[(*it).first][0] && n_max2 == (*it).second) { n       if (n_win[n_ret2] < n_win[(*it).first]) { n         n_ret2 = (*it).first; n         n_max2 = (*it).second; n       } n     } n   } n   cout << n_ret1 << endl; n   cout << n_ret2 << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 40; n const int F = N / 2 + N % 2; n const int S = N - F; n const long long N_MASKS = (1ll << N); n const long long F_MASKS = (1ll << F); n const long long S_MASKS = (1ll << S); n double liquid; n int n, f, s; n long long adj[N]; n int firstSizes[F_MASKS]; n long long firstAvailables[F_MASKS]; n int secondSizes[S_MASKS]; n long long secondAvailables[S_MASKS]; n int maxPossible[S_MASKS]; n void readData() { n   cin >> n >> liquid; n   f = n / 2 + n % 2; n   s = n - f; n   for (int i = 0; i < n; ++i) { n     for (int j = 0; j < n; ++j) { n       long long a; n       cin >> a; n       adj[i] |= (a << j); n     } n   } n } n void findCliques(int sta, int cur, int fin, long long mask, int size, n                  long long available, int* sizes, long long* availables) { n   if (cur == fin) { n     return; n   } n   findCliques(sta, cur + 1, fin, mask, size, available, sizes, availables); n   if ((1ll << cur) & available) { n     mask |= (1ll << cur); n     ++size; n     available &= adj[cur]; n     sizes[mask >> sta] = size; n     availables[mask >> sta] = available; n     findCliques(sta, cur + 1, fin, mask, size, available, sizes, availables); n   } n } n int findPossibles(long long available) { n   if (maxPossible[available]) { n     return maxPossible[available]; n   } n   if (available == 0) { n     return 0; n   } n   if (secondSizes[available]) { n     return maxPossible[available] = secondSizes[available]; n   } n   for (int i = 0; i < s; ++i) { n     if (available & (1ll << i)) { n       maxPossible[available] = n           max(maxPossible[available], findPossibles(available & ~(1ll << i))); n     } n   } n   return maxPossible[available]; n } n int findMaxClique() { n   findCliques(0, 0, f, 0, 0, (1ll << n) - 1, firstSizes, firstAvailables); n   findCliques(f, f, n, 0, 0, (1ll << n) - 1, secondSizes, secondAvailables); n   int res = findPossibles((1 << s) - 1); n   for (int mask = 0; mask < (1 << f); ++mask) { n     if (firstSizes[mask]) { n       res = max(res, n                 firstSizes[mask] + findPossibles(firstAvailables[mask] >> f)); n     } n   } n   return res; n } n double solve() { n   int x = findMaxClique(); n   return 0.5 * liquid * liquid * (x - 1.) / x; n } n int main() { n   ios_base::sync_with_stdio(false); n   cout << setprecision(20) << fixed; n   readData(); n   cout << solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long dir[4][2] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}}; n long long power(long long a, long long b) { n   long long res = 1; n   while (b) { n     if (b % 2) res = (res * a) % 5; n     b = b >> 1; n     a = (a * a) % 5; n   } n   return res; n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   long long i, n, t; n   cin >> n >> t; n   long long ans = 1e16; n   pair<long long, long long> p; n   for (i = 0; i < t; i++) { n     long long a, b; n     cin >> a >> b; n     if (b - a + 1 < ans) { n       ans = b - a + 1; n     } n   } n   cout << ans <<   n ; n   long long c = 0; n   for (i = 0; i < n; i++) { n     if (i % ans == 0) { n       c = 0; n       cout << c <<    ; n       c++; n     } else { n       cout << c <<    ; n       c++; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 200005; n vector<int> lk[maxn]; n int fa[maxn], dep[maxn], a[maxn], b[maxn], n; n void dfs(int now, int pre) { n   fa[now] = pre; n   dep[now] = dep[pre] + 1; n   for (auto p : lk[now]) n     if (p != pre) dfs(p, now); n } n vector<int> cir; n int rt, lp, lq; n bool find_circle() { n   cir.clear(); n   static bool vis[maxn]; n   int p = 0; n   for (int i = 1; i <= n; i++) n     if (a[i] != b[i] && dep[i] > dep[p]) p = i; n   if (!p) return 1; n   for (int u = p; vis[u] = 1, cir.push_back(u), a[fa[u]] != b[fa[u]]; u = fa[u]) n     ; n   int q = 0; n   for (int i = 1; i <= n; i++) n     if (a[i] != b[i] && !vis[i] && dep[i] > dep[q]) q = i; n   if (q) { n     reverse(cir.begin(), cir.end()); n     for (int u = q; vis[u] = 1, cir.push_back(u), a[fa[u]] != b[fa[u]]; n          u = fa[u]) n       ; n   } else n     q = fa[cir.back()]; n   for (int i = 1; i <= n; i++) n     if (a[i] != b[i] && !vis[i]) return 0; n   int u, v; n   for (u = p, v = q; u != v; u = fa[u]) n     if (dep[u] < dep[v]) swap(u, v); n   rt = u; n   if (cir.size() != dep[p] + dep[q] - 2 * dep[rt]) return 0; n   lp = p, lq = q; n   return 1; n } n int main() { n   scanf( %d , &n); n   for (int i = 1; i <= n; i++) scanf( %d , &a[i]); n   for (int i = 1; i <= n; i++) scanf( %d , &b[i]); n   for (int i = 1, u, v; i < n; i++) { n     scanf( %d%d , &u, &v); n     lk[u].push_back(v), lk[v].push_back(u); n   } n   int pa = find(a + 1, a + n + 1, 0) - a, pb = find(b + 1, b + n + 1, 0) - b; n   dep[0] = -1; n   dfs(pb, 0); n   for (int u = pa; u != pb; u = fa[u]) swap(a[u], a[fa[u]]); n   long long ans = dep[pa]; n   if (!find_circle()) { n     printf( -1 n ); n     return 0; n   } n   if (!cir.size()) { n     printf( 0 %d n , ans); n     return 0; n   } n   int gap = 0, len = cir.size(); n   for (int i = 0; i < len; i++) n     if (b[cir[i]] == a[cir[0]]) gap = i; n   for (int i = 1; i < len; i++) n     if (b[cir[(i + gap) % len]] != a[cir[i]]) { n       printf( -1 n ); n       return 0; n     } n   static bool vis[maxn]; n   for (int i = pa; i; i = fa[i]) vis[i] = 1; n   if (vis[cir[0]] || vis[cir.back()]) { n     if (vis[cir.back()]) reverse(cir.begin(), cir.end()), gap = len - gap; n     int po = cir.size(); n     for (int i = 0; i < cir.size(); i++) n       if (!vis[cir[i]]) { n         po = i; n         break; n       } n     ans = ans - po + n           min(po + gap * 1ll * (len + 1), n               abs((len - gap) * 1ll * (len + 1) - po)); n   } else n     ans = (ans + min(gap, len - gap) * 1ll * (len + 1)); n   for (; !vis[rt]; rt = fa[rt]) ans += 2; n   if (lp > lq) swap(lq, lp); n   printf( %d %d %lld n , lp, lq, ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 5010; n struct Edge { n   int v, u, f, r, c; n   Edge() {} n   Edge(int v, int u, int f, int c, int r) : v(v), u(u), f(f), c(c), r(r) {} n }; n const int inf = 1e9; n const int C = 4000; n int n, m, k, nflow, s, t, parent[N], number[N], a[N], b[N]; n int dist[N], pl[N]; n vector<Edge> g[N]; n queue<pair<int, int> > q; n vector<int> ga, gb; n void addEdge(int a, int b, int u, int c) { n   g[a].push_back(Edge(b, u, 0, c, g[b].size())); n   g[b].push_back(Edge(a, 0, 0, -c, g[a].size() - 1)); n } n int MinCostFlow() { n   int flow = 0; n   int cost = 0; n   while (true) { n     for (int i = 1; i <= nflow; i++) dist[i] = inf; n     dist[s] = 0; n     q.push(make_pair(0, s)); n     while (!q.empty()) { n       int v = (q.front()).second; n       int d = -(q.front()).first; n       q.pop(); n       if (dist[v] != d) continue; n       if (d == inf) break; n       for (int i = 0; i < g[v].size(); i++) n         if (g[v][i].f < g[v][i].u && dist[g[v][i].v] > d + g[v][i].c) { n           dist[g[v][i].v] = d + g[v][i].c; n           q.push(make_pair(-dist[g[v][i].v], g[v][i].v)); n           number[g[v][i].v] = i; n           parent[g[v][i].v] = v; n         } n     } n     if (dist[t] == inf) break; n     int v = t, addflow = 1e9; n     while (v != s) { n       int u = parent[v]; n       addflow = min(addflow, g[u][number[v]].u - g[u][number[v]].f); n       v = u; n     } n     v = t; n     while (v != s) { n       int u = parent[v]; n       cost += addflow * g[u][number[v]].c; n       g[u][number[v]].f += addflow; n       g[v][g[u][number[v]].r].f -= addflow; n       v = u; n     } n     flow += addflow; n   } n   return cost; n } n int main() { n   scanf( %d%d%d , &n, &m, &k); n   for (int i = 1; i <= n; i++) scanf( %d , &a[i]); n   for (int i = 1; i <= n; i++) scanf( %d , &b[i]); n   s = n + 3; n   t = n + 4; n   for (int i = 1; i <= n; i++) { n     addEdge(n + 1, i, 1, -a[i]); n     addEdge(n + 2, i, 1, -b[i]); n   } n   addEdge(s, n + 1, m, 0); n   addEdge(s, n + 2, k, 0); n   for (int i = 1; i <= n; i++) { n     addEdge(i, t, 1, 0); n   } n   nflow = n + 4; n   int ans = -MinCostFlow(); n   cout << ans << endl; n   for (int j = 0; j < g[n + 1].size(); j++) { n     int v = g[n + 1][j].v; n     if (v >= 1 && v <= n && g[n + 1][j].f == 1) { n       ga.push_back(v); n     } n   } n   for (int j = 0; j < g[n + 2].size(); j++) { n     int v = g[n + 2][j].v; n     if (v >= 1 && v <= n && g[n + 2][j].f == 1) { n       gb.push_back(v); n     } n   } n   for (int i = 0; i < ga.size(); i++) cout << ga[i] <<    ; n   cout << endl; n   for (int i = 0; i < gb.size(); i++) cout << gb[i] <<    ; n   cout << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 2e5 + 5; n int n; n double a[N]; n double max_poor(double x) { n   int i; n   double pre, mpre, ret = 0; n   pre = mpre = 0; n   for (i = 0; i < n; i++) { n     pre += a[i] - x; n     ret = max(ret, abs(pre - mpre)); n     mpre = min(mpre, pre); n   } n   pre = mpre = 0; n   for (i = 0; i < n; i++) { n     pre += a[i] - x; n     ret = max(ret, abs(pre - mpre)); n     mpre = max(mpre, pre); n   } n   return ret; n } n int main() { n   cin.tie(0); n   ios::sync_with_stdio(0); n   int i; n   double l = 0.0, h = 0.0, m1, m2; n   cin >> n; n   for (i = 0; i < n; h = max(h, abs(a[i])), i++) cin >> a[i]; n   l = -h; n   for (i = 0; i < 300; i++) { n     m1 = l + (h - l) / 3; n     m2 = h - (h - l) / 3; n     if (max_poor(m1) > max_poor(m2)) n       l = m1; n     else n       h = m2; n   } n   printf( %.17f n , max_poor(l)); n   return 0; n } n


#include <bits/stdc++.h> n const long long MOD = 1000000007; n const int MAX = 1e9 + 7; n const int MIN = -1e9 + 7; n using namespace std; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   long long b, g; n   cin >> b >> g; n   long long n; n   cin >> n; n   if (b >= n && g >= n) { n     cout << n + 1; n   } else if (b + g == n) { n     cout << 1; n   } else if (b >= n || g >= n) { n     cout << min(b, g) + 1; n   } else { n     cout << b + g - n + 1; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = int(5e4) + 100; n const int MOD = int(360); n const int oo = INT_MAX; n const long double PI = acos(-1.0); n const long double EPS = 1e-9; n struct pt { n   long double x, y; n   pt() {} n   pt(long double x, long double y) : x(x), y(y) {} n }; n inline long double x(pt P) { return P.x; } n inline long double y(pt P) { return P.y; } n istream& operator>>(istream& in, pt& p) { n   long double x, y; n   in >> x >> y; n   p = pt(x, y); n   return in; n } n ostream& operator<<(ostream& out, const pt& p) { n   out <<  (  << double(p.x) <<  ,   << double(p.y) <<  ) ; n   return out; n } n pt operator+(const pt& A, const pt& B) { return pt(x(A) + x(B), y(A) + y(B)); } n pt operator-(const pt& A, const pt& B) { return pt(x(A) - x(B), y(A) - y(B)); } n pt operator*(const pt& A, const pt& B) { n   return pt(x(A) * x(B) - y(A) * y(B), x(A) * y(B) + y(A) * x(B)); n } n pt operator*(const pt& A, const long double& B) { n   return pt(x(A) * B, y(A) * B); n } n pt operator*(const long double& B, const pt& A) { n   return pt(x(A) * B, y(A) * B); n } n pt operator/(const pt& A, const long double& B) { n   return pt(x(A) / B, y(A) / B); n } n inline long double norm(pt A) { return x(A) * x(A) + y(A) * y(A); } n inline long double abs(pt A) { return sqrt(norm(A)); } n inline long double cross(pt A, pt B) { return x(A) * y(B) - y(A) * x(B); } n inline long double dot(pt A, pt B) { return x(A) * x(B) + y(A) * y(B); } n inline bool same(long double a, long double b) { n   return a + EPS > b && b + EPS > a; n } n inline bool samePt(pt A, pt B) { return same(x(A), x(B)) && same(y(A), y(B)); } n inline long double dist(pt A, pt B) { return abs(B - A); } n inline pt rotcw(pt p) { return p * pt(0, -1); } n inline bool segContains(pt X, pt A, pt B) { n   if (samePt(X, A) || samePt(X, B)) return 1; n   if (!same(0, cross(A - X, B - X))) return 0; n   return (dot(A - X, B - X) < EPS); n } n pt linesIntersection(pt A, pt B, pt C, pt D) { n   long double x = cross(C, D - C) - cross(A, D - C); n   x /= cross(B - A, D - C); n   return A + x * (B - A); n } n double signedArea(const vector<pt>& P) { n   double result = 0.0; n   for (int i = 0; i < (int)P.size() - 1; i++) result += cross(P[i], P[i + 1]); n   return result / 2.0; n } n vector<pt> reorganize(vector<pt>& P) { n   int n = int(P.size()); n   vector<pt> R(n); n   if (P.size() == 1) { n     R[0] = P[0]; n     return R; n   } n   if (signedArea(P) > EPS) { n     reverse(P.begin(), P.end()); n   } n   int s = 0; n   for (int i = 1; i < n; ++i) { n     if (x(P[s]) > x(P[i]) || (x(P[s]) == x(P[i]) && y(P[s]) > y(P[i]))) { n       s = i; n     } n   } n   R[0] = P[s]; n   for (int i = (s + 1) % n, j = 1; i != s; i = (i + 1) % n, ++j) { n     if (samePt(P[i], P[(i - 1 + n) % n])) { n       j--; n       continue; n     } n     R[j] = P[i]; n   } n   R[n - 1] = R[0]; n   return R; n } n vector<pt> convexPolygonSum(vector<pt>& P, vector<pt>& Q) { n   P = reorganize(P); n   Q = reorganize(Q); n   int n = int(P.size()), m = int(Q.size()); n   vector<pt> R(n + m - 1); n   R[0] = (P[0] + Q[0]); n   int i = 1, j = 1, k = 1; n   for (; i < n && j < m; ++k) { n     if (cross(P[i] - P[i - 1], Q[j] - Q[j - 1]) < -EPS) { n       R[k] = R[k - 1] + (P[i] - P[i - 1]); n       ++i; n     } else { n       R[k] = R[k - 1] + (Q[j] - Q[j - 1]); n       ++j; n     } n   } n   while (i < n) { n     R[k] = R[k - 1] + (P[i] - P[i - 1]); n     ++i; n     ++k; n   } n   while (j < m) { n     R[k] = R[k - 1] + (Q[j] - Q[j - 1]); n     ++j; n     ++k; n   } n   vector<pt> T; n   T.push_back(R[0]); n   for (int i = 1; i + 1 < int(R.size()); ++i) { n     if (same(cross(R[i] - R[i - 1], R[i + 1] - R[i - 1]), 0.0)) continue; n     T.push_back(R[i]); n   } n   T.push_back(T[0]); n   return T; n } n int inConvexPolygon(pt X, long double mnx, long double mxx, vector<pt>& P) { n   if (x(X) < mnx || x(X) > mxx) return -1; n   int lo = 1, hi = int(P.size()) - 1, mi; n   while (lo <= hi) { n     mi = (lo + hi) / 2; n     if (cross(P[mi] - P[0], X - P[0]) < -EPS) { n       lo = mi + 1; n     } else { n       hi = mi - 1; n     } n   } n   lo = hi; n   if (hi == -1) return -1; n   long double c = cross(X - P[lo], X - P[lo + 1]); n   if (same(c, 0.0)) return (segContains(X, P[lo], P[lo + 1]) ? 0 : -1); n   if (c > EPS) return -1; n   return 1; n } n int l, n, m, q; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   while (cin >> l) { n     vector<pt> P1(l + 1); n     for (int i = 0; i < l; ++i) { n       cin >> P1[i]; n     } n     cin >> n; n     vector<pt> P2(n + 1); n     for (int i = 0; i < n; ++i) { n       cin >> P2[i]; n     } n     cin >> m; n     vector<pt> P3(m + 1); n     for (int i = 0; i < m; ++i) { n       cin >> P3[i]; n     } n     P1[l] = P1[0]; n     P2[n] = P2[0]; n     P3[m] = P3[0]; n     vector<pt> P = convexPolygonSum(P2, P3); n     P = convexPolygonSum(P1, P); n     long double mnx = x(P[0]); n     long double mxx = -oo; n     for (int i = 0; i < int(P.size()); ++i) { n       mxx = max(mxx, x(P[i])); n     } n     cin >> q; n     for (int i = 0; i < q; ++i) { n       pt cur; n       cin >> cur; n       cur = 3.0 * cur; n       if (inConvexPolygon(cur, mnx, mxx, P) >= 0) { n         cout <<  YES n ; n       } else { n         cout <<  NO n ; n       } n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, k = 0; n   string home; n   cin >> n; n   cin >> home; n   for (int i = 0; i < n; i++) { n     string now; n     cin >> now; n     now = now.substr(5, 3); n     if (now == home) k++; n   } n   if (k == n - k) n     cout <<  home ; n   else n     cout <<  contest ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T> n using min_heap = priority_queue<T, vector<T>, greater<T>>; n struct RangeIncrement { n   vector<long long> tree, lazy; n   RangeIncrement(int n) : tree(n << 2), lazy(n << 2) {} n   void refresh(int i, int l, int r) { n     if (lazy[i] != 0) { n       tree[i] += lazy[i] * (r - l + 1); n       if (l < r) { n         int lc = i << 1, rc = (i << 1) + 1; n         lazy[lc] += lazy[i]; n         lazy[rc] += lazy[i]; n       } n       lazy[i] = 0; n     } n   } n   void update(int i, int l, int r, int ul, int ur, long long x) { n     refresh(i, l, r); n     if (r < ul || l > ur) { n       return; n     } else if (ul <= l && r <= ur) { n       lazy[i] += x; n       refresh(i, l, r); n     } else { n       int m = l + r >> 1; n       int lc = i << 1, rc = (i << 1) + 1; n       update(lc, l, m, ul, ur, x); n       update(rc, m + 1, r, ul, ur, x); n       tree[i] = tree[lc] + tree[rc]; n     } n   } n   long long query(int i, int l, int r, int ql, int qr) { n     refresh(i, l, r); n     if (r < ql || l > qr) { n       return 0; n     } else if (ql <= l && r <= qr) { n       return tree[i]; n     } else { n       int m = l + r >> 1; n       int lc = i << 1, rc = (i << 1) + 1; n       return query(lc, l, m, ql, qr) + query(rc, m + 1, r, ql, qr); n     } n   } n }; n template <class T> n struct DisjointRanges { n   struct Range { n     int l, r; n     T x; n     Range(int l, const pair<int, T>& p) : l(l), r(p.first), x(p.second) {} n   }; n   map<int, pair<int, T>> s; n   vector<Range> update(int l, int r, T x) { n     vector<Range> bye; n     auto last = s.upper_bound(r); n     if (last != s.begin()) { n       auto it = prev(last); n       if (it->second.first > r) { n         last = s.emplace_hint(last, r + 1, it->second); n         it->second.first = r; n       } n     } n     auto first = s.lower_bound(l); n     if (first != s.begin()) { n       auto it = prev(first); n       if (it->second.first >= l) { n         bye.emplace_back(l, it->second); n         it->second.first = l - 1; n       } n     } n     while (first != last) { n       bye.emplace_back(first->first, first->second); n       first = s.erase(first); n     } n     s.emplace_hint(last, l, make_pair(r, x)); n     return bye; n   } n }; n int main() { n   int n, m; n   scanf( %d %d , &n, &m); n   RangeIncrement ri(n); n   DisjointRanges<int> dr; n   for (int i = 1; i <= n; i++) { n     dr.update(i, i, i); n   } n   while (m--) { n     int o, l, r, x; n     scanf( %d %d %d , &o, &l, &r); n     switch (o) { n       case 1: { n         scanf( %d , &x); n         auto v = dr.update(l, r, x); n         for (auto it : v) { n           ri.update(1, 1, n, it.l, it.r, abs(x - it.x)); n         } n         break; n       } n       case 2: { n         printf( %lld n , ri.query(1, 1, n, l, r)); n         break; n       } n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n int cnt[100100]; n int R[100100]; n int n; n long long power(long long a, long long b) { n   long long res = 1; n   while (b) { n     if (b & 1) res = (res * a) % 1000000007; n     a = (a * a) % 1000000007; n     b >>= 1; n   } n   return res; n } n int main() { n   memset(cnt, 0, sizeof(cnt)); n   scanf( %d , &n); n   int maxA = 0; n   for (int i = 1; i <= n; i++) { n     int tmp; n     scanf( %d , &tmp); n     cnt[tmp]++; n     maxA = max(maxA, tmp); n   } n   for (int i = maxA; i >= 1; i--) cnt[i] += cnt[i + 1]; n   long long ans = 0; n   for (int i = 1; i <= maxA; i++) { n     int tot = 0; n     for (int j = 1; j * j <= i; j++) { n       if (i % j == 0) { n         R[tot++] = j; n         if (j * j != i) { n           R[tot++] = i / j; n         } n       } n     } n     sort(R, R + tot); n     long long tmp = n         (power(tot, cnt[R[tot - 1]]) - n          power(tot - 1, cnt[R[tot - 1]]) % 1000000007 + 1000000007) % n         1000000007; n     for (int j = 0; j < tot - 1; j++) n       tmp = (tmp * power(j + 1, cnt[R[j]] - cnt[R[j + 1]])) % 1000000007; n     ans = (ans + tmp) % 1000000007; n   } n   printf( %I64d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 500100; n const int maxp = 40; n int n, q; n double d[maxn][maxp]; n int p[maxn]; n void upd(int v, int k, double x) { n   while (v && k < maxp) { n     double a = d[v][k]; n     d[v][k] *= x; n     x = (d[v][k] + 1) / (a + 1); n     v = p[v]; n     k++; n   } n } n int main() { n   ios_base::sync_with_stdio(0); n   for (int i = 0; i < maxn; i++) n     for (int j = 0; j < maxp; j++) d[i][j] = 1; n   cin >> q; n   n = 1; n   for (int i = 0; i < q; i++) { n     int qtype, v; n     cin >> qtype >> v; n     if (qtype == 1) { n       p[++n] = v; n       upd(n, 0, 0); n     } else { n       double ans = 0; n       for (int j = 1; j < maxp; j++) ans += (1 - d[v][j]); n       cout << fixed << setprecision(10) << ans <<   n ; n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n using ll = long long; n const ll INF = 1e14 + 7; n using vi = vector<int>; n using pii = pair<int, int>; n using pli = pair<ll, int>; n int ask(int i, int j) { n   cout <<  ?   << i <<     << j << endl; n   int ans; n   cin >> ans; n   return ans; n } n int main() { n   int n; n   cin >> n; n   vi ans(n + 4), curr(n + 4); n   curr[0] = ask(1, n); n   curr[1] = ask(2, n); n   ans[1] = curr[0] - curr[1]; n   for (int i = 2; i <= n - 1; i++) { n     curr[i] = ask(i - 1, i); n     ans[i] = curr[i] - ans[i - 1]; n   } n   ans[n] = curr[0]; n   for (int i = 1; i < n; i++) { n     ans[n] -= ans[i]; n   } n   cout <<  ! ; n   for (int i = 1; i <= n; i++) { n     cout <<     << ans[i]; n   } n   cout << endl; n   fflush(stdout); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int a[200005]; n int b[200005]; n bool f[200005]; n int main() { n   int q, n; n   cin >> q; n   while (q--) { n     cin >> n; n     for (int i = 1; i <= n; i++) { n       cin >> a[i]; n       b[i] = 1; n       f[i] = 0; n     } n     for (int i = 1; i <= n; i++) { n       int j = i; n       if (f[i] == 1) continue; n       f[i] = 1; n       int ctr = 1; n       while (a[j] != i) { n         ctr++; n         f[j] = 1; n         j = a[j]; n       } n       j = i; n       while (a[j] != i) { n         b[j] = ctr; n         j = a[j]; n       } n     } n     for (int i = 1; i <= n; i++) cout << b[i] <<    ; n     cout <<   n ; n   } n } n


#include <bits/stdc++.h> n inline int readChar(); n template <class T = int> n inline T readInt(); n template <class T> n inline void writeInt(T x, char end = 0); n inline void writeChar(int x); n inline void writeWord(const char* s); n static const int buf_size = 4096; n inline int getChar() { n   static char buf[buf_size]; n   static int len = 0, pos = 0; n   if (pos == len) pos = 0, len = fread(buf, 1, buf_size, stdin); n   if (pos == len) return -1; n   return buf[pos++]; n } n inline int readChar() { n   int c = getChar(); n   while (c <= 32) c = getChar(); n   return c; n } n template <class T> n inline T readInt() { n   int s = 1, c = readChar(); n   T x = 0; n   if (c ==  - ) s = -1, c = getChar(); n   while ( 0  <= c && c <=  9 ) x = x * 10 + c -  0 , c = getChar(); n   return s == 1 ? x : -x; n } n static int write_pos = 0; n static char write_buf[buf_size]; n inline void writeChar(int x) { n   if (write_pos == buf_size) n     fwrite(write_buf, 1, buf_size, stdout), write_pos = 0; n   write_buf[write_pos++] = x; n } n template <class T> n inline void writeInt(T x, char end) { n   if (x < 0) writeChar( - ), x = -x; n   char s[24]; n   int n = 0; n   while (x || !n) s[n++] =  0  + x % 10, x /= 10; n   while (n--) writeChar(s[n]); n   if (end) writeChar(end); n } n inline void writeWord(const char* s) { n   while (*s) writeChar(*s++); n } n struct Flusher { n   ~Flusher() { n     if (write_pos) fwrite(write_buf, 1, write_pos, stdout), write_pos = 0; n   } n } flusher; n using namespace std; n struct static_graph { n   int colors[500000]; n   int firstv[500000]; n   set<int> pool; n   static const int N = 500000; n   static const int M = 900000; n   struct edge { n     int v; n     edge* next; n   } edges[M]; n   int nedges; n   struct node { n     vector<int>* ice; n     int prev_colors; n     bool depth_use; n     edge* firstedge; n     inline void init() { firstedge = NULL; }; n     inline edge* add_edge(edge* e) { n       e->next = firstedge; n       firstedge = e; n       return firstedge; n     }; n   } nodes[N]; n   int nnodes; n   static_graph() { nnodes = 0, nedges = 0; }; n   inline node& new_node() { n     node& r = nodes[nnodes++]; n     return r; n   } n   inline edge* new_edge() { return &(edges[nedges++]); }; n   inline edge* add_edge(int a, int b) { n     edge* e = new_edge(); n     e->v = b; n     return nodes[a].add_edge(e); n   }; n   inline void add_bi_edge(int a, int b) { n     add_edge(a, b); n     add_edge(b, a); n   }; n   void depth_go(int index, int prev) { n     node& cur = nodes[index]; n     if (cur.depth_use++) return; n     for (int x : (*cur.ice)) n       if (colors[x]) pool.erase(colors[x]); n     for (int x : (*cur.ice)) n       if (!colors[x]) { n         colors[x] = (*pool.upper_bound(0)); n         firstv[x] = index; n         pool.erase(colors[x]); n       } n     for (int x : (*cur.ice)) pool.insert(colors[x]); n     for (edge* e = cur.firstedge; e; e = e->next) { n       depth_go(e->v, index); n     }; n   }; n   void depth_run(int first) { n     chrono::time_point<chrono::high_resolution_clock> start, end; n     start = std::chrono::high_resolution_clock::now(); n     for (int i = 0; i < nnodes; i++) { n       node& cur = nodes[i]; n       cur.depth_use = false; n     }; n     depth_go(first, -1); n   }; n   vector<node*> depth_to_wide(int first) {} n   void read_graph(istream* f){}; n }; n static_graph g; n int main() { n   istream& f = cin; n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   chrono::time_point<chrono::high_resolution_clock> start, end; n   start = std::chrono::high_resolution_clock::now(); n   int n, m, c, x = 0; n   n = readInt<int>(); n   m = readInt<int>(); n   for (int i = 0; i < n; i++) { n     c = readInt<int>(); n     static_graph::node& cur = g.new_node(); n     cur.ice = new vector<int>(); n     cur.ice->resize(c); n     cur.prev_colors = 0; n     for (int t = 0; t < c; t++) { n       x = readInt<int>(); n       (*cur.ice)[t] = x - 1; n     }; n     sort((*cur.ice).begin(), (*cur.ice).end()); n     ; n   }; n   for (int i = 0; i < n - 1; i++) { n     int a, b; n     a = readInt<int>(), b = readInt<int>(); n     g.add_bi_edge(a - 1, b - 1); n   }; n   for (int i = 0; i < m; i++) g.colors[i] = 0; n   for (int i = 0; i < m; i++) g.pool.insert(i + 1); n   g.depth_run(0); n   int mx = 1; n   for (int i = 0; i < m; i++) { n     mx = max(mx, g.colors[i]); n     if (!g.colors[i]) g.colors[i] = 1; n   } n   cout << mx << endl; n   for (int i = 0; i < m; i++) cout << g.colors[i] <<    ; n } n


#include <bits/stdc++.h> n using namespace std; n long long pw(long long a, long long b) { n   if (b == 0) return 1; n   if (b % 2 == 1) n     return (a * pw(a * a, b / 2)); n   else n     return (1 * pw(a * a, b / 2)); n } n void solve() { n   long long n, k; n   cin >> n >> k; n   long long a[n], b[k]; n   for (int i = 0; i < n; i++) cin >> a[i]; n   sort(a, a + n); n   int n1 = 0; n   for (int i = 0; i < k; i++) { n     cin >> b[i]; n     if (b[i] == 1) n1++; n   } n   sort(b, b + k); n   long long ans = 0; n   for (int i = 0; i < n1; i++) ans += (a[n - 1 - i] * 2); n   long long st = 0, en = n - 1 - n1; n   for (long long i = k - 1; i >= n1; i--) { n     if (b[i] == 1) { n       ans += (a[en] * 2); n       en--; n     } else { n       ans += a[en]; n       b[i]--; n       en--; n       ans += a[st]; n       st += b[i]; n     } n   } n   cout << ans << endl; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   long long t = 1; n   cin >> t; n   while (t--) { n     solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct node { n   long long lvl1, lvl2, id; n }; n node t[500005]; n bool cmp(struct node x, struct node y) { n   if (x.lvl1 < y.lvl1) n     return true; n   else n     return false; n } n vector<node> sh; n vector<long long> adj[500005]; n long long vis[500005], lvl1[500005], lvl2[500005]; n void bfs1(long long n = 1) { n   queue<long long> q; n   q.push(n); n   vis[n] = 1; n   lvl1[n] = 0; n   while (!q.empty()) { n     long long x = q.front(); n     q.pop(); n     for (auto &cv : adj[x]) { n       if (vis[cv]) continue; n       q.push(cv); n       vis[cv] = 1; n       lvl1[cv] = lvl1[x] + 1; n     } n   } n } n void bfs2(long long n) { n   queue<long long> q; n   q.push(n); n   vis[n] = 1; n   lvl2[n] = 0; n   while (!q.empty()) { n     long long x = q.front(); n     q.pop(); n     for (auto &cv : adj[x]) { n       if (vis[cv]) continue; n       q.push(cv); n       vis[cv] = 1; n       lvl2[cv] = lvl2[x] + 1; n     } n   } n } n node combine(node x, node y) { n   if (x.lvl2 > y.lvl2) n     return x; n   else n     return y; n } n void build(long long n) { n   for (long long i = n - 1; i > 0; --i) n     t[i] = combine(t[i << 1], t[i << 1 | 1]); n } n node query(long long l, long long r, long long n) { n   node res; n   res.lvl2 = res.lvl1 = res.id = 0; n   for (l += n, r += n; l < r; l >>= 1, r >>= 1) { n     if (l & 1) res = (combine(t[l++], res)); n     if (r & 1) res = combine(t[--r], res); n   } n   return res; n } n long long arr[500005]; n int32_t main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   long long i, j, k, l, m, n, x, y; n   cin >> n >> m >> k; n   for (i = 0; i < k; i++) { n     cin >> arr[i]; n   } n   for (i = 0; i < m; i++) { n     cin >> x; n     cin >> y; n     adj[x].push_back(y); n     adj[y].push_back(x); n   } n   bfs1(); n   memset(vis, 0, sizeof vis); n   bfs2(n); n   for (i = 0; i < k; i++) { n     node temp; n     temp.id = arr[i]; n     temp.lvl1 = lvl1[arr[i]]; n     temp.lvl2 = lvl2[arr[i]]; n     sh.push_back(temp); n   } n   sort(sh.begin(), sh.end(), cmp); n   for (i = 0; i < sh.size(); i++) { n     t[i + k].lvl1 = sh[i].lvl1; n     t[i + k].lvl2 = sh[i].lvl2; n     t[i + k].id = sh[i].id; n   } n   build(k); n   long long ans = 0; n   for (i = 0; i + 1 < sh.size(); i++) { n     long long temp; n     temp = sh[i].lvl1; n     temp++; n     temp += query(i + 1, sh.size(), sh.size()).lvl2; n     ans = max(ans, temp); n   } n   cout << min(lvl2[1], ans) << endl; n   return 0; n } n


#include <bits/stdc++.h> n #pragma warning(disable : 4786) n #pragma comment(linker,  /STACK:102400000,102400000 ) n using namespace std; n const double eps = 1e-8; n const double PI = acos(-1.0); n const int MAXN = 200005; n const int mod = 1000000007; n const long long llinf = (long long)(1e18) + 500; n const int inf = 0x3f3f3f3f; n int x[1005]; n double y[1005]; n int main() { n   int i, j, k, m; n   int n, r; n   scanf( %d%d , &n, &r); n   for (i = 1; i <= n; i++) { n     scanf( %d , x + i); n     y[i] = r * 1.0; n   } n   for (i = 2; i <= n; i++) { n     for (j = i - 1; j >= 1; j--) { n       int dx = abs(x[i] - x[j]); n       if (dx > 2 * r) continue; n       y[i] = max(y[i], y[j] + sqrt(4 * r * r * 1.0 - dx * 1.0 * dx)); n     } n   } n   for (i = 1; i <= n; i++) { n     printf( %.10lf%c , y[i], i == n ?   n  :    ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n char str[1000005 + 1]; n int main() { n   int ans = 0, i; n   stack<int> S; n   scanf( %s , str); n   for (i = 0; str[i] !=   0 ; ++i) { n     if (str[i] ==  ( ) S.push(i); n     if (str[i] ==  )  && !S.empty()) { n       S.pop(); n       ans += 2; n     } n   } n   printf( %d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void solve() { n   int y, b, r; n   cin >> y >> b >> r; n   while (1) { n     if (r - 1 <= b && r - 2 <= y) { n       break; n     } n     --r; n   } n   cout << 3 * r - 3; n } n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   string n; n   cin >> n; n   long long even = 0; n   long long odd = 0; n   long long len = n.length(); n   long long aeven = 0; n   long long aodd = 0; n   long long beven = 0; n   long long bodd = 0; n   for (int i = 0; i < len; i++) { n     if (n[i] ==  a ) { n       if (i % 2 == 1) { n         aodd++; n         odd += aodd; n         even += aeven; n       } else { n         aeven++; n         odd += aeven; n         even += aodd; n       } n     } else { n       if (i % 2 == 1) { n         bodd++; n         odd += bodd; n         even += beven; n       } else { n         beven++; n         odd += beven; n         even += bodd; n       } n     } n   } n   cout << even <<     << odd << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int const N = 100000; n long long a[N + 1]; n long long f[N * 3]; n inline int lc(int root) { return root << 1; } n inline int rc(int root) { return (root << 1) | 1; } n void build(int root, int l, int r) { n   f[root] = -1; n   if (l == r) { n     f[root] = a[l]; n     return; n   } n   int mid = (l + r) >> 1; n   build(lc(root), l, mid); n   build(rc(root), mid + 1, r); n   if (f[lc(root)] == f[rc(root)]) { n     f[root] = f[lc(root)]; n   } n } n void update(int root, int l, int r, int fi, int la, long long val) { n   if (fi == l && la == r) { n     f[root] = val; n     return; n   } n   int mid = (l + r) >> 1; n   f[root] = -1; n   if (fi > mid) { n     update(rc(root), mid + 1, r, fi, la, val); n   } else if (la <= mid) { n     update(lc(root), l, mid, fi, la, val); n   } else { n     update(lc(root), l, mid, fi, mid, val); n     update(rc(root), mid + 1, r, mid + 1, la, val); n   } n   if (f[lc(root)] == f[rc(root)]) { n     f[root] = f[lc(root)]; n   } n } n long long query(int root, int l, int r, int fi, int la) { n   if (f[root] != -1) { n     return f[root]; n   } n   int mid = (l + r) >> 1; n   if (fi > mid) { n     return query(rc(root), mid + 1, r, fi, la); n   } else if (la <= mid) { n     return query(lc(root), l, mid, fi, la); n   } else { n     return max(query(lc(root), l, mid, fi, mid), n                query(rc(root), mid + 1, r, mid + 1, la)); n   } n } n void work() { n   int n; n   int m; n   long long w, h; n   long long res; n   scanf( %d , &n); n   for (int i = 1; i <= n; ++i) { n     scanf( %I64d , &a[i]); n   } n   build(1, 1, n); n   scanf( %d , &m); n   while (m--) { n     scanf( %I64d%I64d , &w, &h); n     res = query(1, 1, n, 1, w); n     printf( %I64d n , res); n     update(1, 1, n, 1, w, res + h); n   } n } n int main() { n   work(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n map<long long int, int> mp; n int main() { n   long long int b, q, l, m, t; n   scanf( %lld%lld%lld%lld , &b, &q, &l, &m); n   for (int i = 0; i < m; i++) { n     scanf( %lld , &t); n     mp[t] = 1; n   } n   if (b < -l || b > l) { n     printf( 0 n ); n     return 0; n   } n   if (b == 0) { n     if (mp[0]) n       printf( 0 n ); n     else n       printf( inf n ); n     return 0; n   } else if (q == 0) { n     if (mp[b] && mp[0]) n       printf( 0 n ); n     else if (!mp[b] && mp[0]) n       printf( 1 n ); n     else n       printf( inf n ); n     return 0; n   } else if (q == 1) { n     if (mp[b]) n       printf( 0 n ); n     else n       printf( inf n ); n     return 0; n   } else if (q == -1) { n     if (mp[b] && mp[-b]) n       printf( 0 n ); n     else n       printf( inf n ); n     return 0; n   } else { n     long long int cont = 0; n     while (b <= l && b >= -l) { n       if (!mp[b]) cont++; n       b *= q; n     } n     printf( %lld n , cont); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(0); n   string a, c; n   int b, d, p; n   int pos[110]; n   p = 0; n   cin >> b >> d; n   cin >> a; n   cin >> c; n   int t = 0, la, lc; n   la = a.length(); n   lc = c.length(); n   int k = 0; n   for (int i = 0; i < lc; i++) { n     k = 0; n     for (int j = 0; j < la; j++) n       if (a[j] == c[(i + k) % lc]) k++; n     pos[i] = k; n   } n   int sum = 0; n   int j = 0; n   for (int i = 0; i < b; i++) { n     sum += pos[j]; n     j = (j + pos[j]) % lc; n   } n   sum /= lc; n   p = sum / d; n   cout << p << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m, a, b; n set<pair<int, int> > s; n set<int> sc; n vector<int> ans; n int main() { n   cin >> n >> m; n   for (int i = 1; i <= m; ++i) { n     cin >> a >> b; n     s.insert(make_pair(min(a, b), max(a, b))); n   } n   for (int i = 1; i <= n; ++i) { n     sc.insert(i); n   } n   for (int i = 1; i <= n; ++i) { n     if (sc.find(i) != sc.end()) { n       int cur = 0; n       queue<int> q; n       q.push(i); n       sc.erase(i); n       while (!q.empty()) { n         cur++; n         int v = q.front(); n         q.pop(); n         set<int>::iterator it; n         set<int> c = sc; n         for (it = c.begin(); it != c.end(); it++) { n           int to = *it; n           if (s.find(make_pair(min(v, to), max(v, to))) == s.end()) { n             q.push(to); n             sc.erase(to); n           } n         } n       } n       ans.push_back(cur); n     } n   } n   sort(ans.begin(), ans.end()); n   cout << (int)ans.size() << endl; n   for (int i = 0; i < (int)ans.size(); ++i) { n     cout << ans[i] <<    ; n   } n   cout << endl; n   return 0; n } n


#include <bits/stdc++.h> n #pragma comment(linker,  /STACK:10000000 ) n using namespace std; n template <typename T> n inline T abs(T a) { n   return ((a < 0) ? -a : a); n } n template <typename T> n inline T sqr(T a) { n   return a * a; n } n const int INF = (int)1E9 + 7; n const long double EPS = 1E-9; n const long double PI = 3.1415926535897932384626433832795; n const int NMAX = 200 * 1000; n int a[NMAX], sza; n int t[NMAX * 4], cnt[NMAX * 4]; n long long sum[NMAX * 4][5]; n char buf[300]; n void upd(int pos, int idx, int lf, int rg) { n   if (lf == rg) { n     t[idx] ^= 1; n     memset(sum[idx], 0, sizeof sum[idx]); n     sum[idx][0] = t[idx] * 1LL * a[pos]; n     cnt[idx] = t[idx]; n     return; n   } n   int mid = (lf + rg) >> 1; n   if (pos <= mid) n     upd(pos, 2 * idx + 1, lf, mid); n   else n     upd(pos, 2 * idx + 2, mid + 1, rg); n   cnt[idx] = cnt[2 * idx + 1] + cnt[2 * idx + 2]; n   for (int i = 0; i < int(5); ++i) { n     long long& ans = sum[idx][i]; n     int f = (cnt[2 * idx + 1]) % 5; n     f = (i - f + 5) % 5; n     ans = sum[2 * idx + 1][i] + sum[2 * idx + 2][f]; n   } n } n void change(int val) { n   int pos = lower_bound(a, a + sza, val) - a; n   upd(pos, 0, 0, sza - 1); n } n inline long long getSum() { return sum[0][2]; } n int main() { n   int n; n   scanf( %d , &n); n   gets(buf); n   vector<pair<int, int> > q; n   for (int i = 0; i < int(n); ++i) { n     gets(buf); n     if (buf[0] ==  a ) { n       int val; n       sscanf(buf,  add %d , &val); n       q.push_back(make_pair(1, val)); n       a[sza++] = val; n     } n     if (buf[0] ==  d ) { n       int val; n       sscanf(buf,  del %d , &val); n       q.push_back(make_pair(-1, val)); n       a[sza++] = val; n     } n     if (buf[0] ==  s ) { n       q.push_back(make_pair(-1, -1)); n     } n   } n   sort(a, a + sza); n   sza = unique(a, a + sza) - a; n   for (int i = 0; i < int(n); ++i) { n     if (q[i] == pair<int, int>(-1, -1)) { n       printf( %I64d n , getSum()); n       continue; n     } n     if (q[i].first == 1) { n       change(q[i].second); n     } else { n       change(q[i].second); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int a[100001]; n int getBound(int start, int dir, int size, int k) { n   int cnt = 0; n   bool seen[100001]; n   fill(seen, seen + 100001, false); n   for (int i = start; i <= size && i > 0; i += dir) { n     if (!seen[a[i]]) cnt++, seen[a[i]] = true; n     if (cnt == k) return i; n   } n   return -1; n } n int main() { n   int n, k; n   cin >> n >> k; n   for (int i = 1; i <= n; i++) cin >> a[i]; n   int right = getBound(1, 1, n, k); n   if (right == -1) n     cout << -1 <<     << -1 << endl; n   else { n     int left = getBound(right, -1, n, k); n     cout << left <<     << right << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long MAX = 10e5; n vector<long long> graph[MAX]; n queue<long long> q; n bool visit[MAX]; n long long a[MAX]; n long long s[MAX]; n long long parent[MAX]; n int main() { n   memset(visit, false, sizeof(visit)); n   long long nodes; n   cin >> nodes; n   for (auto i = 2; i <= nodes; ++i) { n     long long tmp; n     cin >> tmp; n     parent[i] = tmp; n     graph[i].push_back(tmp); n     graph[tmp].push_back(i); n   } n   for (auto i = 1; i <= nodes; ++i) { n     cin >> s[i]; n   } n   a[1] = s[1]; n   q.push(1); n   visit[1] = true; n   while (!q.empty()) { n     long long n = q.front(); n     q.pop(); n     if (n != 1) { n       if (s[n] == -1) { n         s[n] = s[parent[n]]; n         if (graph[n].size() > 1) { n           long long mini = 10e9; n           for (auto v : graph[n]) { n             if (v == parent[n]) continue; n             mini = min(mini, s[v]); n           } n           if (mini < s[n]) { n             cout << -1; n             return 0; n           } else { n             a[n] = mini - s[n]; n             s[n] = mini; n           } n         } else { n           a[n] = 0; n           s[n] = s[parent[n]]; n         } n       } else { n         a[n] = s[n] - s[parent[n]]; n       } n     } n     for (auto p : graph[n]) { n       if (visit[p]) continue; n       q.push(p); n       visit[p] = true; n     } n   } n   long long ans = 0; n   for (auto i = 1; i <= nodes; i++) { n     ans += a[i]; n   } n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void solve() { n   string s; n   cin >> s; n   string ans =   ; n   int cnt(0); n   for (int i = 0; i < s.size(); i++) { n     if (s[i] ==  0  && cnt == 0) { n       cnt++; n     } else n       ans += s[i]; n   } n   if (cnt == 0) { n     for (int i = 0; i < ans.size() - 1; i++) cout << ans[i]; n   } else n     cout << ans; n } n int main() { n   long long t; n   t = 1; n   while (t--) solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int dx[] = {1, -1, 0, 0}, dy[] = {0, 0, 1, -1}; n const int N = 500005; n vector<int> arr[N], cnt[N], adj[N]; n int vis[N], st[N], fin[N], dep[N]; n int tim; n char inp[N]; n void dfs(int i, int d) { n   vis[i] = 1; n   dep[i] = d; n   st[i] = ++tim; n   arr[d].push_back(st[i]); n   int no = 0; n   for (int first = 0; first < (int)26; first++) n     no |= (first == inp[i] -  a ) << first; n   cnt[d].push_back(no); n   for (int v : adj[i]) n     if (!vis[v]) dfs(v, d + 1); n   fin[i] = ++tim; n } n signed main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   int n, m; n   cin >> n >> m; n   for (int i = 0; i < (int)n - 1; i++) { n     int first; n     cin >> first; n     adj[first].push_back(i + 2); n     adj[i + 2].push_back(first); n   } n   for (int i = 0; i < (int)n; i++) cin >> inp[i + 1]; n   dfs(1, 0); n   for (int i = 0; i < (int)N; i++) { n     for (int j = 1; j < cnt[i].size(); j++) { n       cnt[i][j] ^= cnt[i][j - 1]; n     } n   } n   for (int wrd = 0; wrd < (int)m; wrd++) { n     int a, b; n     cin >> a >> b; n     b--; n     int no_odds = 0; n     vector<int>& v = arr[b]; n     int l = lower_bound((v).begin(), (v).end(), st[a]) - v.begin(); n     int r = upper_bound((v).begin(), (v).end(), fin[a]) - v.begin() - 1; n     if (l > r) { n       cout <<  Yes n ; n       continue; n     } n     for (int first = 0; first < (int)26; first++) { n       int add = (cnt[b][r] >> first) & 1; n       int sub = (l == 0) ? 0 : ((cnt[b][l - 1] >> first) & 1); n       no_odds += (add - sub + 2) % 2; n     } n     if (no_odds <= 1) n       cout <<  Yes n ; n     else n       cout <<  No n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long int t; n   cin >> t; n   while (t--) { n     long long int a[3], i; n     for (i = 0; i < 3; i++) cin >> a[i]; n     sort(a, a + 3); n     if (a[2] <= a[1] + a[0] + 1) n       cout <<  YES  << endl; n     else n       cout <<  NO  << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n string to_string(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 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> 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 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 <int MOD_> n struct modnum { n   static constexpr int MOD = MOD_; n   static_assert(MOD_ > 0,  MOD must be positive ); n  n  private: n   using ll = long long; n   int v; n   static int minv(int a, int m) { n     a %= m; n     assert(a); n     return a == 1 ? 1 : int(m - ll(minv(m, a)) * ll(m) / a); n   } n  n  public: n   modnum() : v(0) {} n   modnum(ll v_) : v(int(v_ % MOD)) { n     if (v < 0) v += MOD; n   } n   explicit operator int() const { return v; } n   friend std::ostream& operator<<(std::ostream& out, const modnum& n) { n     return out << int(n); n   } n   friend std::istream& operator>>(std::istream& in, modnum& n) { n     ll v_; n     in >> v_; n     n = modnum(v_); n     return in; n   } n   friend bool operator==(const modnum& a, const modnum& b) { n     return a.v == b.v; n   } n   friend bool operator!=(const modnum& a, const modnum& b) { n     return a.v != b.v; n   } n   modnum inv() const { n     modnum res; n     res.v = minv(v, MOD); n     return res; n   } n   friend modnum inv(const modnum& m) { return m.inv(); } n   modnum neg() const { n     modnum res; n     res.v = v ? MOD - v : 0; n     return res; n   } n   friend modnum neg(const modnum& m) { return m.neg(); } n   modnum operator-() const { return neg(); } n   modnum operator+() const { return modnum(*this); } n   modnum& operator++() { n     v++; n     if (v == MOD) v = 0; n     return *this; n   } n   modnum& operator--() { n     if (v == 0) v = MOD; n     v--; n     return *this; n   } n   modnum& operator+=(const modnum& o) { n     v -= MOD - o.v; n     v = (v < 0) ? v + MOD : v; n     return *this; n   } n   modnum& operator-=(const modnum& o) { n     v -= o.v; n     v = (v < 0) ? v + MOD : v; n     return *this; n   } n   modnum& operator*=(const modnum& o) { n     v = int(ll(v) * ll(o.v) % MOD); n     return *this; n   } n   modnum& operator/=(const modnum& o) { return *this *= o.inv(); } n   friend modnum operator++(modnum& a, int) { n     modnum r = a; n     ++a; n     return r; n   } n   friend modnum operator--(modnum& a, int) { n     modnum r = a; n     --a; n     return r; n   } n   friend modnum operator+(const modnum& a, const modnum& b) { n     return modnum(a) += b; n   } n   friend modnum operator-(const modnum& a, const modnum& b) { n     return modnum(a) -= b; n   } n   friend modnum operator*(const modnum& a, const modnum& b) { n     return modnum(a) *= b; n   } n   friend modnum operator/(const modnum& a, const modnum& b) { n     return modnum(a) /= b; n   } n }; n using num = modnum<1000000007>; n const int mod = 1e9 + 7; n num power(num a, int64_t b) { n   if (b == 0) return 1; n   num res = power(a, b / 2); n   if (b & 1) return res * res * a; n   return res * res; n } n bool checkinter(pair<int, int> x, pair<int, int> y) { n   if (x.first > y.first) swap(x, y); n   return x.first <= y.first && y.first <= x.second; n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   if (fopen( input.txt ,  r )) { n     freopen( input.txt ,  r , stdin); n     freopen( output.txt ,  w , stdout); n   } n   int64_t N, M, K, R, C, Ax, Bx, Ay, By; n   cin >> N >> M >> K >> R >> C >> Ax >> Ay >> Bx >> By; n   if (!checkinter(make_pair(Ax, Ax + R - 1), make_pair(Bx, Bx + R - 1)) or n       !checkinter(make_pair(By, By + C - 1), make_pair(Ay, Ay + C - 1))) { n     num res = 0; n     res = power(K, 1ll * N * M - 2ll * R * C); n     res *= power(K, 1ll * R * C); n     cout << res; n     return 0; n   } n   if (Ax > Bx) swap(Ax, Bx), swap(Ay, By); n   if (By < Ay) { n     int diff = abs(By + C - 1 - Ay + 1); n     By = Ay + C - 1 - diff + 1; n   } n   int64_t sharearea = 1ll * ((Ay + C - 1) - By + 1) * ((Ax + R - 1) - Bx + 1); n   int64_t area = 2ll * R * C - sharearea; n   int64_t remainarea = 1ll * N * M - area; n   num res = power(K, remainarea); n   if (Ax == Bx and Ay == By) n     res *= power(K, R * C); n   else n     res *= power(K, 1ll * R * C - sharearea); n   cout << res; n } n


#include <bits/stdc++.h> n using namespace std; n struct rangeincrease { n   int l, r; n }; n rangeincrease actions[100001]; n rangeincrease inprogress[100001]; n int actionL = 0; n int nums[100001]; n int opened = 0; n int main() { n   int n; n   int i; n   cin >> n; n   for (i = 1; i <= n; i++) { n     cin >> nums[i]; n   } n   for (i = 1; i <= n; i++) { n     while (nums[i] > opened) { n       opened++; n       inprogress[opened].l = i; n     } n     while (nums[i] < opened) { n       inprogress[opened].r = i - 1; n       actionL++; n       actions[actionL].l = inprogress[opened].l; n       actions[actionL].r = inprogress[opened].r; n       opened--; n     } n   } n   while (opened > 0) { n     inprogress[opened].r = n; n     actionL++; n     actions[actionL].l = inprogress[opened].l; n     actions[actionL].r = inprogress[opened].r; n     opened--; n   } n   cout << actionL << endl; n   for (i = 1; i <= actionL; i++) { n     cout << actions[i].l <<     << actions[i].r << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n #pragma GCC optimize( Ofast ) n #pragma GCC target( avx,avx2,fma ) n using namespace std; n bool ispoweroftwo(long long n) { return n & (!(n & (n - 1))); } n long long mod = 1000000007; n long long dx[] = {1, 0, -1, 0}; n long long dy[] = {0, -1, 0, 1}; n bool test = 0; n const long long inf = 1e18; n const long long N = 1e5 + 5; n long long cache[N][4][4]; n long long k, n; n long long color[N][3]; n vector<long long> adj[N]; n long long dp(long long u, long long f = 3, long long f1 = 3, long long p = -1) { n   long long &ans = cache[u][f][f1]; n   if (ans != -1) return ans; n   long long kid1 = -1, kid2 = -1; n   for (auto it : adj[u]) { n     if (it == p) continue; n     if (kid1 == -1) n       kid1 = it; n     else n       kid2 = it; n   } n   if (kid1 == -1) return ans = 0; n   if (kid2 != -1) { n     vector<long long> c1, c2, c3; n     for (long long i = 0; i < 3; ++i) { n       if (i == f) continue; n       c1.emplace_back(color[kid1][i] + dp(kid1, i, f, u)); n       c3.emplace_back(i); n     } n     for (long long i = 0; i < 3; ++i) { n       if (i == f) continue; n       c2.emplace_back(color[kid2][i] + dp(kid2, i, f, u)); n     } n     ans = min(c1[0] + c2[1], c1[1] + c2[0]); n     return ans; n   } n   ans = inf; n   for (long long i = 0; i < 3; ++i) { n     if (i == f or i == f1) continue; n     ans = min(ans, color[kid1][i] + dp(kid1, i, f, u)); n   } n   return ans; n } n vector<long long> col(N); n void path(long long u, long long f, long long f1 = 3, long long p = -1) { n   long long &ans = cache[u][f][f1]; n   long long kid1 = -1, kid2 = -1; n   for (auto it : adj[u]) { n     if (it == p) continue; n     if (kid1 == -1) n       kid1 = it; n     else n       kid2 = it; n   } n   if (kid1 == -1) return; n   if (kid2 != -1) { n     vector<long long> c1, c2, c3; n     for (long long i = 0; i < 3; ++i) { n       if (i == f) continue; n       c1.emplace_back(color[kid1][i] + cache[kid1][i][f]); n       c3.emplace_back(i); n     } n     for (long long i = 0; i < 3; ++i) { n       if (i == f) continue; n       c2.emplace_back(color[kid2][i] + cache[kid2][i][f]); n     } n     if (ans != c1[0] + c2[1]) swap(kid1, kid2); n     col[kid1] = c3[0] + 1; n     col[kid2] = c3[1] + 1; n     path(kid1, c3[0], f, u); n     path(kid2, c3[1], f, u); n     return; n   } n   for (long long i = 0; i < 3; ++i) { n     if (i == f or i == f1) continue; n     if (ans == color[kid1][i] + cache[kid1][i][f]) { n       col[kid1] = i + 1; n       path(kid1, i, f, u); n     } n   } n } n void solve(long long tc = 0) { n   memset(cache, -1, sizeof(cache)); n   cin >> n; n   for (long long i = 0; i < 3; ++i) n     for (long long j = 1; j <= n; ++j) cin >> color[j][i]; n   for (long long i = 1, x, y; i < n; ++i) { n     cin >> x >> y; n     adj[x].emplace_back(y); n     adj[y].emplace_back(x); n   } n   for (long long i = 1; i <= n; ++i) { n     if (adj[i].size() >= 3) { n       cout <<  -1 n ; n       return; n     } n   } n   adj[0].emplace_back(1); n   long long ans = dp(0, 3); n   cout << ans <<   n ; n   path(0, 3); n   for (long long i = 1; i <= n; ++i) cout << col[i] <<    ; n   cout <<   n ; n } n signed main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   long long t = 1; n   if (test) cin >> t; n   for (long long i = 1; i <= t; ++i) { n     solve(i); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long MX = 200005; n long long n, m; n vector<long long> adj[2][MX]; n long long st[MX], fin[MX]; n long long deg[2][MX]; n long long ans, tim; n long long cur = 1; n pair<long long, long long> seg[4 * MX]; n long long lazy[4 * MX]; n pair<long long, long long> merge(pair<long long, long long> x, n                                  pair<long long, long long> y) { n   if (x.first == y.first) return {x.first, x.second + y.second}; n   return min(x, y); n } n void build(long long node, long long l, long long r) { n   if (l == r) { n     seg[node] = {0, 1}; n     return; n   } n   long long mid = (l + r) / 2; n   build(2 * node, l, mid); n   build(2 * node + 1, mid + 1, r); n   seg[node] = merge(seg[2 * node], seg[2 * node + 1]); n } n void propagate(long long node, long long l, long long r) { n   seg[node].first += lazy[node]; n   if (l != r) { n     lazy[2 * node] += lazy[node]; n     lazy[2 * node + 1] += lazy[node]; n   } n   lazy[node] = 0; n } n void update(long long node, long long l, long long r, long long x, long long y, n             long long val) { n   propagate(node, l, r); n   if (l > y or r < x) return; n   if (l >= x and r <= y) { n     lazy[node] += val; n     propagate(node, l, r); n     return; n   } n   long long mid = (l + r) / 2; n   update(2 * node, l, mid, x, y, val); n   update(2 * node + 1, mid + 1, r, x, y, val); n   seg[node] = merge(seg[2 * node], seg[2 * node + 1]); n } n void dfs(long long u) { n   st[u] = tim++; n   for (auto v : adj[1][u]) { n     dfs(v); n   } n   fin[u] = tim; n } n void solve(long long u) { n   update(1, 0, cur - 1, st[u], fin[u] - 1, 1); n   if (!seg[1].first) ans -= seg[1].second; n   for (auto v : adj[0][u]) { n     solve(v); n   } n   update(1, 0, cur - 1, st[u], fin[u] - 1, -1); n } n signed main() { n   cin >> n >> m; n   vector<vector<long long> > index(n, vector<long long>(m, -1)); n   vector<vector<char> > a(n, vector<char>(m)); n   for (long long i = 0; i < n; i++) { n     for (long long j = 0; j < m; j++) { n       cin >> a[i][j]; n       if (a[i][j] ==  L ) { n         index[i][j] = cur; n         index[i][j + 1] = cur; n         cur++; n       } else if (a[i][j] ==  U ) { n         index[i][j] = cur; n         index[i + 1][j] = cur; n         cur++; n       } n     } n   } n   cur--; n   for (long long i = 0; i < n; i++) { n     for (long long j = 0; j < m; j++) { n       if (a[i][j] ==  L  and j + 2 < m) { n         adj[(i + j) % 2][index[i][j + 2]].push_back(index[i][j]); n         deg[(i + j) % 2][index[i][j]]++; n       } n       if (a[i][j] ==  R  and j - 2 >= 0) { n         adj[(i + j) % 2][index[i][j - 2]].push_back(index[i][j]); n         deg[(i + j) % 2][index[i][j]]++; n       } n       if (a[i][j] ==  U  and i + 2 < n) { n         adj[(i + j) % 2][index[i + 2][j]].push_back(index[i][j]); n         deg[(i + j) % 2][index[i][j]]++; n       } n       if (a[i][j] ==  D  and i - 2 >= 0) { n         adj[(i + j) % 2][index[i - 2][j]].push_back(index[i][j]); n         deg[(i + j) % 2][index[i][j]]++; n       } n     } n   } n   ans = (n * m * n * m) / 4; n   tim = 0; n   for (long long i = 1; i < cur + 1; i++) { n     if (!deg[1][i]) dfs(i); n   } n   build(1, 0, cur - 1); n   for (long long i = 1; i < cur + 1; i++) { n     if (!deg[0][i]) solve(i); n   } n   cout << ans <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 205, inf = 1e9; n int n, a[N]; n int tot, lnk[N], fro[N * N * 2], nxt[N * N * 2], son[N * N * 2], c[N * N * 2], n     ori[N * N * 2]; n int S, T, dep[N], cur[N]; n queue<int> q; n int cnt; n bool vis[N]; n vector<int> ans[N]; n bool isprm(int x) { n   if (x == 2 || x == 3 || x == 5 || x == 7) return 1; n   for (int i = 2; i <= sqrt(x); ++i) n     if (x % i == 0) return 0; n   return 1; n } n void add(int x, int y, int z) { n   nxt[++tot] = lnk[x], lnk[x] = tot; n   fro[tot] = x, son[tot] = y, c[tot] = ori[tot] = z; n } n struct Dinic { n   bool bfs(int x = 0) { n     memset(dep, 0, sizeof dep), dep[S] = 1; n     for (; !q.empty(); q.pop()) n       ; n     q.push(S); n     for (; !q.empty();) { n       x = q.front(), q.pop(); n       for (int j = lnk[x]; j; j = nxt[j]) { n         if (!dep[son[j]] && c[j]) { n           dep[son[j]] = dep[x] + 1; n           q.push(son[j]); n         } n       } n     } n     return dep[T]; n   } n   int dfs(int x, int f, int ret = 0) { n     if (x == T) return f; n     for (int &j = cur[x]; j; j = nxt[j]) { n       if (dep[son[j]] == dep[x] + 1 && c[j]) { n         int delta = dfs(son[j], min(f, c[j])); n         c[j] -= delta, c[j ^ 1] += delta; n         ret += delta, f -= delta; n       } n     } n     return ret; n   } n   int answer(int ret = 0) { n     for (; bfs();) { n       for (int i = S; i <= T; ++i) cur[i] = lnk[i]; n       ret += dfs(S, inf); n     } n     return ret; n   } n } network; n void dfs(int x, int s) { n   vis[x] = 1, ans[cnt].push_back(x); n   if (a[x] & 1) { n     for (int j = lnk[x]; j; j = nxt[j]) { n       if (c[j] != 0 || son[j] == S || son[j] == T) continue; n       if (!vis[son[j]]) { n         dfs(son[j], s); n         break; n       } n     } n   } else { n     for (int j = lnk[x]; j; j = nxt[j]) { n       if (c[j ^ 1] != 0 || son[j] == S || son[j] == T) continue; n       if (!vis[son[j]]) { n         dfs(son[j], s); n         break; n       } n     } n   } n } n int main() { n   cin >> n, tot = 1, S = 0, T = n + 1; n   for (int i = 1; i <= n; ++i) { n     scanf( %d , &a[i]); n     if (a[i] & 1) n       add(S, i, 2), add(i, S, 0); n     else n       add(i, T, 2), add(T, i, 0); n   } n   for (int i = 1; i <= n; ++i) { n     for (int j = 1; j <= n; ++j) n       if (isprm(a[i] + a[j]) && a[i] % 2 == 1 && a[j] % 2 == 0) n         add(i, j, 1), add(j, i, 0); n   } n   int fl = network.answer(); n   if (fl != n) return puts( Impossible ), 0; n   for (int i = 1; i <= n; ++i) n     if (a[i] & 1) { n       if (!vis[i]) ++cnt, dfs(i, i); n     } n   printf( %d n , cnt); n   for (int i = 1; i <= cnt; ++i) { n     printf( %d , ans[i].size()); n     for (int j = 0; j < ans[i].size(); ++j) printf(  %d , ans[i][j]); n     puts(  ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int NMax = 1003; n enum directions { EAST, NORTH }; n struct point { n   int x, y; n }; n point a[NMax]; n int n; n int test(int searchDir, point a[], long double x, long double y) { n   if (searchDir == NORTH) { n     int nr = 0; n     for (int i = 1; i <= n; ++i) { n       if (a[i].y >= y && n           (((long double)a[i].x >= x && x <= (long double)a[i + 1].x) || n            ((long double)a[i].x <= x && x >= (long double)a[i + 1].x))) { n         nr++; n       } n     } n     return (nr & 1); n   } else { n     int nr = 0; n     for (int i = 1; i <= n; ++i) { n       if (a[i].x >= x && n           (((long double)a[i].y >= y && y <= (long double)a[i + 1].y) || n            ((long double)a[i].y <= y && y >= (long double)a[i + 1].y))) { n         nr++; n       } n     } n     return (nr & 1); n   } n } n int main() { n   cin >> n; n   for (int i = 1; i <= n + 1; ++i) { n     cin >> a[i].x >> a[i].y; n   } n   int ans = 0; n   for (int i = 2; i <= n + 1; ++i) { n     if (a[i].x == a[i - 1].x) { n       if (a[i].y > a[i - 1].y) { n         ans += test(EAST, a, a[i].x, a[i].y + 0.5); n       } else { n         ans += test(EAST, a, a[i].x, a[i].y - 0.5); n       } n     } else { n       if (a[i].x > a[i - 1].x) { n         ans += test(NORTH, a, a[i].x + 0.5, a[i].y); n       } else { n         ans += test(NORTH, a, a[i].x - 0.5, a[i].y); n       } n     } n   } n   cout << ans <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int mod = 1051131; n const int Inv2 = (mod + 1) >> 1; n inline void setIO(string a) {} n long long Power; n int m, s, n, nn; n int a[(1 << 25) + 23]; n void work(int n, int p, int q) { n   if (n == 1) { n     int &An = a[1]; n     (p += q) %= mod; n     for (long long c = Power; c; c >>= 1, p = 1ll * p * p % mod) n       if (c & 1) An = 1ll * An * p % mod; n     return; n   } n   int t = n >> 1, tt = 1ll * t * p % mod; n   int ttp = (p + q + mod - tt) % mod, tttp = 1; n   for (long long c = Power; c; c >>= 1, ttp = 1ll * ttp * ttp % mod) n     if (c & 1) tttp = 1ll * tttp * ttp % mod; n   int xi = (1ll * t * p % mod - p + q + mod) % mod; n   for (int i = (1); i <= (t); i++) { n     int A = a[i], B = a[i + t]; n     a[i] = (A + B) % mod; n     a[i + t] = (A + mod - B) % mod; n     a[i + t] = 1ll * a[i + t] * tttp % mod; n   } n   work(n >> 1, (p + p) % mod, xi); n   for (int i = (1); i <= (t); i++) { n     int A = a[i], B = a[i + t]; n     a[i] = (A + B) % mod; n     a[i + t] = (A + mod - B) % mod; n     a[i] = 1ll * a[i] * Inv2 % mod; n     a[i + t] = 1ll * a[i + t] * Inv2 % mod; n   } n } n int main() { n   scanf( %d , &m); n   cin >> Power; n   scanf( %d , &s); n   for (int i = (1); i <= (s); i++) scanf( %d , &a[i]); n   n = 1 << m; n   nn = n; n   for (int i = (s + 1); i <= (n); i++) n     a[i] = (101ll * a[i - s] + 10007) % 1051131; n   work(n, 1, 0); n   int ans = 0; n   for (int i = (1); i <= (n); i++) ans ^= a[i]; n   printf( %d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, arr[30010], book[30010]; n bool judge() { n   int ps, gap; n   int t1 = 1, t2; n   while (t1 <= n && book[t1]) t1++; n   t2 = t1 + 1; n   if (t1 > n) return false; n   while (t2 <= n && book[t2]) t2++; n   if (t2 > n) return true; n   gap = arr[t2] - arr[t1]; n   ps = t2; n   for (int i = t2 + 1; i <= n; i++) n     if (!book[i]) { n       if (arr[i] - arr[ps] == gap) n         ps = i; n       else n         return false; n     } n   return true; n } n int main() { n   scanf( %d , &n); n   for (int i = 1; i <= n; i++) scanf( %d , &arr[i]); n   if (n == 2) n     printf( %d n%d , arr[1], arr[2]); n   else { n     int x[3] = {arr[2] - arr[1], arr[3] - arr[2], arr[3] - arr[1]}; n     int flag = 0; n     for (int i = 0; i < 3; i++) { n       int st = 4, tmp = arr[3]; n       memset(book, 0, sizeof(book)); n       if (i == 0) { n         book[2] = book[1] = 1; n         st = 3; n         tmp = arr[2]; n       } else if (i == 1) n         book[2] = book[3] = 1; n       else n         book[1] = book[3] = 1; n       for (int ii = st; ii <= n; ii++) n         if (arr[ii] == tmp + x[i]) { n           book[ii] = 1; n           tmp = arr[ii]; n         } n       if (judge()) { n         flag = 1; n         break; n       } else { n         int p = n; n         while (!book[p]) p--; n         book[p] = 0; n         if (judge()) { n           flag = 1; n           break; n         } n       } n     } n     if (flag) { n       for (int i = 1; i <= n; i++) n         if (book[i]) printf( %d  , arr[i]); n       printf(  n ); n       for (int i = 1; i <= n; i++) n         if (!book[i]) printf( %d  , arr[i]); n     } else n       printf( No solution ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int ekub(int a, int b) { return b == 0 ? a : ekub(b, a % b); } n long long s[51][51]; n int main() { n   long long i, j, n, m, a, b, ans = 1e9; n   cin >> n >> m; n   for (i = 1; i <= n; i++) n     for (j = 1; j <= m; j++) { n       cin >> s[i][j]; n       s[i][j] += s[i - 1][j] + s[i][j - 1] - s[i - 1][j - 1]; n     } n   cin >> a >> b; n   for (i = a; i <= n; i++) n     for (j = b; j <= m; j++) n       ans = min(ans, s[i][j] - s[i - a][j] - s[i][j - b] + s[i - a][j - b]); n   for (i = b; i <= n; i++) n     for (j = a; j <= m; j++) n       ans = min(ans, s[i][j] - s[i - b][j] - s[i][j - a] + s[i - b][j - a]); n   cout << ans; n } n


#include <bits/stdc++.h> n namespace P_TEMPLATE { n class fastin { n  private: n   int _ch, _f; n  n  public: n   inline fastin& operator>>(char& c) { n     c = getchar(); n     return *this; n   } n   template <typename _Tp> n   inline fastin& operator>>(_Tp& _x) { n     _x = 0; n     while (!isdigit(_ch)) _f |= (_ch == 45), _ch = getchar(); n     while (isdigit(_ch)) n       _x = (_x << 1) + (_x << 3) + (_ch ^ 48), _ch = getchar(); n     _f && (_x = -_x, _f = 0); n     return *this; n   } n   fastin() { _ch = _f = 0; } n } fin; n class fastout { n  private: n   int _num[32], _head; n  n  public: n   inline fastout& operator<<(char c) { n     putchar(c); n     return *this; n   } n   template <typename _Tp> n   inline fastout& operator<<(_Tp _x) { n     _Tp _k; n     if (_x == 0) { n       putchar( 0 ); n       return *this; n     } n     if (_x < 0) putchar( - ), _x = -_x; n     while (_x > 0) n       _k = _x / 10, ++_head, _num[_head] = (_x - (_k << 1) - (_k << 3)) ^ 48, n       _x = _k; n     while (_head > 0) putchar(_num[_head]), --_head; n     return *this; n   } n   fastout() { _head = 0; } n } fout; n inline void P_INIT() {} n }  // namespace P_TEMPLATE n using namespace P_TEMPLATE; n using namespace std; n const int maxn = 1000004, maxm = 104; n const int mod = 1000000007, inf = 1201201201; n int n, k, p; n char s[maxn][maxm]; n int len[maxn]; n int book[28], cnt[28]; n vector<int> e[28]; n queue<int> q; n int topo[28], indx; n inline bool check() { n   for (register int i = 1; i <= 26; ++i) n     if (!cnt[i]) q.push(i), topo[++indx] = i; n   while (!q.empty()) { n     int t = q.front(); n     q.pop(); n     for (register int i = 0; i < e[t].size(); ++i) { n       --cnt[e[t][i]]; n       if (cnt[e[t][i]] == 0) q.push(e[t][i]), topo[++indx] = e[t][i]; n     } n   } n   return indx == 26; n } n signed main() { n   P_INIT(); n   fin >> n >> k; n   for (register int i = 1; i <= n; ++i) { n     fin >> p; n     for (register int j = 1; j <= k; ++j) { n       scanf( %s , s[p * k + j] + 1), len[p * k + j] = strlen(s[p * k + j] + 1); n       for (register int t = 1; t <= len[p * k + j]; ++t) n         book[s[p * k + j][t] -  a  + 1] = 1; n     } n   } n   for (register int i = 1; i < n * k; ++i) { n     bool flag = 0; n     for (register int j = 1; j <= std::min(len[i], len[i + 1]); ++j) n       if (s[i][j] != s[i + 1][j]) { n         e[s[i][j] -  a  + 1].push_back(s[i + 1][j] -  a  + 1); n         ++cnt[s[i + 1][j] -  a  + 1]; n         flag = 1; n         break; n       } n     if (!flag && len[i] > len[i + 1]) { n       puts( IMPOSSIBLE ); n       return 0; n     } n   } n   if (check()) { n     for (register int i = 1; i <= 26; ++i) n       if (book[topo[i]]) fout << char(topo[i] - 1 +  a ); n   } else n     puts( IMPOSSIBLE ); n   return 0; n } n


#include <bits/stdc++.h> n #pragma comment(linker,  /STACK:66777216 ) n using namespace std; n const int N = 100007; n int n, a[N], cnt[N]; n vector<int> D[N]; n long long power(long long a, long long b) { n   if (b == 0) { n     return 1; n   } n   if (b & 1) { n     return (a * power(a, b - 1)) % 1000000007LL; n   } n   long long ret = power(a, b >> 1); n   return (ret * ret) % 1000000007LL; n } n int main() { n   scanf( %d , &n); n   for (int i = 0; i < n; ++i) { n     scanf( %d , &a[i]); n   } n   sort(a, a + n); n   for (int i = 1; i <= a[n - 1]; ++i) { n     for (int j = i; j <= a[n - 1]; j += i) { n       D[j].push_back(i); n     } n   } n   long long ans = 0; n   for (int m = 1; m <= a[n - 1]; ++m) { n     vector<int> &d = D[m]; n     int nd = (int)(d).size(); n     for (int i = 0; i < nd; ++i) { n       cnt[i] = n - (lower_bound(a, a + n, d[i]) - a); n     } n     long long cur = n         (power(nd, cnt[nd - 1]) - power(nd - 1, cnt[nd - 1]) + 1000000007LL) % n         1000000007LL; n     for (int i = 0; i < nd - 1; ++i) { n       cur = (cur * power(i + 1, cnt[i] - cnt[i + 1])) % 1000000007LL; n     } n     ans = (ans + cur) % 1000000007LL; n   } n   printf( %d n , (int)ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void init_code() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n } n int main() { n   init_code(); n   int t; n   cin >> t; n   while (t--) { n     int n; n     cin >> n; n     vector<pair<int, int>> v; n     vector<pair<int, int>> v1; n     for (int i = 0; i < n; i++) { n       int x, y; n       cin >> x >> y; n       v1.push_back({x, y}); n       v.push_back({x, y}); n     } n     sort(v.begin(), v.end()); n     int st = v[0].first; n     int ed = v[0].second; n     int cnt = 1; n     unordered_map<int, int> mp; n     mp[st] = 1; n     for (int i = 1; i < n; i++) { n       if (v[i].first <= ed) { n         mp[v[i].first] = cnt; n         ed = max(ed, v[i].second); n       } else { n         st = v[i].first; n         ed = v[i].second; n         if (cnt == 1) { n           cnt++; n         } n         mp[v[i].first] = cnt; n       } n     } n     if (cnt < 2) { n       cout <<  -1  << endl; n     } else { n       for (int i = 0; i < v1.size(); i++) { n         cout << mp[v1[i].first] <<    ; n       } n       cout << endl; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void solve() {} n void no() { n   cout <<  NO  <<   n ; n   exit(0); n } n const long long maxn = 100050; n long long a[maxn]; n long long psa[maxn]; n signed main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   long long n; n   cin >> n; n   for (long long i = 1; i <= n; i++) { n     cin >> a[i]; n   } n   long long len = n; n   for (long long i = 1; i <= n; i++) { n     if (a[i] != a[1]) { n       len = i - 1; n       break; n     } n   } n   for (long long i = 1; i <= n; i++) { n     psa[i] = psa[i - 1] + a[i]; n   } n   if (n % len) no(); n   for (long long i = len; i <= n; i += len) { n     long long sum = psa[i] - psa[i - len]; n     if (sum and sum != len) { n       no(); n     } n     if (i >= len * 2) { n       if (sum == psa[i - len] - psa[i - 2 * len]) { n         no(); n       } n     } n   } n   cout <<  YES  <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1234567; n int pref[N], a[N], b[N]; n char c[N]; n int solve(vector<pair<int, int> > z, int b) { n   sort(z.begin(), z.end()); n   int cnt = z.size(); n   if (cnt < 2) { n     return 0; n   } n   pref[0] = 0; n   for (int i = 0; i < cnt; i++) { n     pref[i + 1] = max(pref[i], z[i].second); n   } n   int i = 0; n   int res = 0; n   for (int j = cnt - 1; j >= 0; j--) { n     while (i < j && z[i].first + z[j].first <= b) { n       i++; n     } n     i = min(i, j); n     if (i > 0) { n       res = max(res, pref[i] + z[j].second); n     } n   } n   return res; n } n int same(vector<pair<int, int> > &v, int b) { n   if (v.size() < 2) return 0; n   sort(v.begin(), v.end()); n   vector<int> pre(v.size() + 1, 0); n   for (int i = 0; i < v.size(); i++) pre[i + 1] = max(pre[i], v[i].second); n   int i = 0, ans = 0; n   for (int j = v.size() - 1; j >= 0; j--) { n     while (i < j && v[i].first + v[j].first <= b) i++; n     i = min(i, j); n     if (i > 0) { n       ans = max(ans, v[j].second + pre[i]); n     } n   } n   return ans; n } n int main() { n   int n, C, D; n   scanf( %d %d %d , &n, &C, &D); n   for (int i = 0; i < n; i++) { n     scanf( %d %d , a + i, b + i); n     c[i] = getchar(); n     while (c[i] !=  C  && c[i] !=  D ) { n       c[i] = getchar(); n     } n   } n   int ans = 0; n   { n     int x = 0, y = 0; n     for (int i = 0; i < n; i++) { n       if (c[i] ==  C  && b[i] <= C) { n         x = max(x, a[i]); n       } n     } n     for (int i = 0; i < n; i++) { n       if (c[i] ==  D  && b[i] <= D) { n         y = max(y, a[i]); n       } n     } n     if (x > 0 && y > 0) { n       ans = max(ans, x + y); n     } n   } n   { n     vector<pair<int, int> > z; n     for (int i = 0; i < n; i++) { n       if (c[i] ==  C ) { n         z.push_back(make_pair(b[i], a[i])); n       } n     } n     ans = max(ans, same(z, C)); n   } n   { n     vector<pair<int, int> > z; n     for (int i = 0; i < n; i++) { n       if (c[i] ==  D ) { n         z.push_back(make_pair(b[i], a[i])); n       } n     } n     ans = max(ans, same(z, D)); n   } n   printf( %d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n inline int abs(int n) { return n < 0 ? -n : n; } n bool board[20][20]; n int dx[] = {-3, -2, -2, -1, -1, 0, 0, 1, 1, 2, 2, 3}; n int dy[] = {0, 1, -1, 2, -2, 3, -3, 2, -2, 1, -1, 0}; n int dfs(int p, int n, int m) { n   int mx = p, t, cnt = 0; n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < m; j++) { n       if (!board[i][j]) { n         board[i][j] = true; n         for (int k = 0; k < 12; k++) { n           int ic = i + dx[k], jc = j + dy[k]; n           if (ic >= 0 && ic < n && jc >= 0 && jc < m && !board[ic][jc]) { n             board[ic][jc] = true; n             t = dfs(p + 2, n, m); n             mx < t && (mx = t); n             if (mx == ((m * n) | 1) - 1) return mx; n             board[ic][jc] = false; n           } n         } n         board[i][j] = false; n         cnt++; n         if (m * n - cnt <= mx) return mx; n       } n     } n   } n   return mx; n } n int ans[] = {0, 0, 0, 0, 2, 4}; n int main() { n   int m, n; n   scanf( %d%d , &m, &n); n   if (m < 10 && n < 10) { n     printf( %d n , dfs(0, n, m)); n   } else if (m == 1) { n     printf( %d n , n / 6 * 6 + ans[n % 6]); n   } else if (n == 1) { n     printf( %d n , m / 6 * 6 + ans[m % 6]); n   } else { n     printf( %lld n , (((long long)(m)*n) | 1) - 1); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n using db = long double; n using pll = pair<long long, long long>; n const long long sz = 300003; n long long dp[sz][2], n; n long long a[sz]; n long long dfs(long long i, long long par) { n   if (i >= n) return 0; n   long long &res = dp[i][par]; n   if (res == -1) { n     res = 0; n     if (par) { n       res = a[i] + dfs(i + 1, par ^ 1); n     } else { n       res = -a[i] + dfs(i + 1, par ^ 1); n     } n     res = max({0LL, res, dfs(i + 1, par)}); n   } n   return res; n } n int32_t main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   long long t, i, x, j, y, q, z, k; n   int kkr, csk; n   long long tt; n   cin >> tt; n   for (long long _tt = 0; _tt < tt; _tt++) { n     cin >> n >> q; n     for (int i = 0; i < n; i++) cin >> a[i]; n     ; n     while (q-- > 0) { n       cin >> x >> y; n       x--, y--; n     } n     for (i = 0; i < n; i += 1) { n       memset(dp[i], -1, sizeof(dp[i])); n     } n     z = dfs(0, 1); n     42; n     cout << z <<   n ; n     ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   int t; n   cin >> t; n   while (t--) { n     int n; n     cin >> n; n     int arr[n]; n     for (int i = 0; i < n; i++) cin >> arr[i]; n     int counts[n + 1]; n     memset(counts, 0, sizeof(counts)); n     if (n == 1) { n       cout << 0 << endl; n       continue; n     } n     for (int i = 0; i < n; i++) counts[arr[i]]++; n     int dc = 0, rc = INT_MIN; n     for (int i = 0; i <= n; i++) { n       if (counts[i] > 0) dc++; n       rc = max(rc, counts[i]); n     } n     if (rc > 1) dc--; n     if (rc > dc + 1) n       cout << dc + 1 << endl; n     else n       cout << min(dc, rc) << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 105; n const int inf = 10000000; n int n, k; n int l[N], r[N]; n int main() { n   scanf( %d%d , &n, &k); n   int midx = (k + 1) / 2, midy = (k + 1) / 2; n   for (int i = 0; i < n; i++) { n     int m, ans = inf, x, y1, y2; n     scanf( %d , &m); n     for (int row = 1; row <= k; row++) { n       if (l[row] == 0 && r[row] == 0) { n         for (int a = 1; a + m - 1 <= k; a++) { n           int tot = 0, b = a + m - 1; n           for (int col = a; col <= b; col++) { n             tot += abs(row - midx) + abs(col - midy); n           } n           if (tot < ans) ans = tot, x = row, y1 = a, y2 = b; n         } n       } else { n         if (l[row] > m) { n           int tot = 0, a = l[row] - m, b = l[row] - 1; n           for (int col = a; col <= b; col++) { n             tot += abs(row - midx) + abs(col - midy); n           } n           if (tot < ans) ans = tot, x = row, y1 = a, y2 = b; n         } n         if (r[row] + m <= k) { n           int tot = 0, a = r[row] + 1, b = r[row] + m; n           for (int col = a; col <= b; col++) n             tot += abs(row - midx) + abs(col - midy); n           if (tot < ans) ans = tot, x = row, y1 = a, y2 = b; n         } n       } n     } n     if (ans == inf) n       puts( -1 ); n     else { n       printf( %d %d %d n , x, y1, y2); n       if (r[x] == 0 && l[x] == 0) n         l[x] = y1, r[x] = y2; n       else n         l[x] = min(l[x], y1), r[x] = max(r[x], y2); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 500010; n const int M = 16; n const int inf = 0x3f3f3f3f; n const int S = 1 << 15; n map<string, int> mp; n int vs[15], n, m; n char s[N][12], sx[12]; n int dp[S], cnt[S][16]; n int cal(int st) { n   int res = 0; n   while (st) { n     if (st & 1) res++; n     st >>= 1; n   } n   return res; n } n void prepare() { n   for (int i = 0; i < S; ++i) { n     int tmp = i; n     for (int j = 0; j < M; ++j) { n       tmp >>= 1; n       cnt[i][j] = cal(tmp); n     } n   } n } n int stx[N], pre[M]; n int main() { n   prepare(); n   ios::sync_with_stdio(0); n   scanf( %d , &n); n   for (int i = 0; i < n; ++i) { n     scanf( %s , sx); n     mp[sx] = i; n   } n   int ans1 = -1, ans2 = 0, ans3 = 0; n   scanf( %d , &m); n   for (int i = 0; i < m; ++i) { n     int k; n     scanf( %d , &k); n     memset(vs, 0, sizeof vs); n     for (int j = 0; j < k; ++j) { n       scanf( %s , s[j]); n       if (mp.find(s[j]) == mp.end()) continue; n       vs[mp[s[j]]] = 1; n     } n     int f = 0; n     for (int j = 0; j < n && !f; ++j) n       if (!vs[j]) f = 1; n     if (f) continue; n     int v = 0; n     memset(dp, 0x3f, sizeof dp); n     dp[0] = 0; n     int pre = 0; n     for (int j = 0; j < k; ++j) { n       if (mp.find(s[j]) == mp.end()) continue; n       int id = mp[s[j]], vst = 1 << id; n       if (pre & (1 << id)) continue; n       pre = (pre & ((1 << id) - 1) | (1 << id)); n       for (int st = stx[j] & (vst - 1); st < (1 << n); ++st) n         if ((st & vst) == 0) { n           dp[st | vst] = min(dp[st | vst], dp[st] + cnt[st][id]); n         } n     } n     int tmp = n * (n - 1) / 2 - dp[(1 << n) - 1] + 1; n     if (ans1 == -1 || tmp > ans2 || (tmp == ans2 && dp[(1 << n) - 1] < ans3)) { n       ans1 = i; n       ans2 = tmp; n       ans3 = dp[(1 << n) - 1]; n     } n   } n   if (ans1 == -1) n     puts( Brand new problem! ); n   else { n     printf( %d n , ans1 + 1); n     printf( %s ,  [: ); n     for (int i = 0; i < ans2; ++i) putchar( | ); n     puts( :] ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T> n using V = vector<T>; n const int MM = 5e5 + 5; n using T = long long; n bool QUERY; n struct Line { n   mutable T a, b, p; n   T Eval(T first) const { return a * first + b; } n   bool operator<(const Line& o) const { return QUERY ? p < o.p : a < o.a; } n }; n struct ConvexHull : multiset<Line> { n   const T kInf = numeric_limits<T>::max(); n   T div(T a, T b) { return a / b - ((a ^ b) < 0 && a % b); } n   bool isect(iterator first, iterator second) { n     if (second == end()) { n       first->p = kInf; n       return false; n     } n     if (first->a == second->a) { n       first->p = first->b > second->b ? kInf : -kInf; n     } else { n       first->p = div(second->b - first->b, first->a - second->a); n     } n     return first->p >= second->p; n   } n   void InsertLine(T a, T b) { n     auto nx = insert({a, b, 0}), it = nx++, pv = it; n     while (isect(it, nx)) { n       nx = erase(nx); n     } n     if (pv != begin() && isect(--pv, it)) { n       isect(pv, it = erase(it)); n     } n     while ((it = pv) != begin() && (--pv)->p >= it->p) { n       isect(pv, erase(it)); n     } n   } n   T EvalMax(T first) { n     if (empty()) { n       return -2e18; n     } n     QUERY = 1; n     auto it = lower_bound({0, 0, first}); n     QUERY = 0; n     return it->Eval(first); n   } n }; n vector<vector<int>> tree; n long long sub[MM]; n long long n; n long long sq(long long e) { return e * e; } n void dfs1(int cur, int par) { n   sub[cur] = 1; n   for (auto u : tree[cur]) { n     if (u != par) { n       dfs1(u, cur); n       sub[cur] += sub[u]; n     } n   } n } n long long dp1[MM]; n void dfs2(int cur, int par) { n   dp1[cur] = sq(sub[cur]); n   for (int u : tree[cur]) { n     if (u != par) { n       dfs2(u, cur); n       dp1[cur] = min(dp1[cur], sq(sub[cur] - sub[u]) + dp1[u]); n     } n   } n } n vector<long long> ans; n void dfs3(int cur, int par) { n   ConvexHull CH; n   ans[cur] = sq(n); n   for (int u : tree[cur]) { n     if (u != par) { n       dfs3(u, cur); n       long long val = dp1[u] - CH.EvalMax(sub[u]) + sq(sub[u]); n       ans[cur] = min(ans[cur], val); n       ans[cur] = min(ans[cur], dp1[u] + sq(n - sub[u])); n       CH.InsertLine(2 * (n - sub[u]), -(dp1[u] + sq(n - sub[u]))); n     } n   } n   return; n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   cin >> n; n   tree.resize(n); n   ans.resize(n); n   for (int i = 0; i < n - 1; i++) { n     int u, v; n     cin >> u >> v; n     u--; n     v--; n     tree[u].push_back(v); n     tree[v].push_back(u); n   } n   dfs1(0, -1); n   dfs2(0, -1); n   dfs3(0, -1); n   cout << (n * n - n) / 2 + n               (sq(n) - *min_element((ans).begin(), (ans).end())) / 2 n        << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct Item { n   int x[3], y[3]; n }; n bool check(Item t); n inline int Dist(int x1, int y1, int x2, int y2) { n   return (x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2); n } n int main() { n   Item it; n   for (int i = 0; i < 3; i++) cin >> it.x[i] >> it.y[i]; n   if (check(it)) n     cout <<  RIGHT n ; n   else { n     bool f = false; n     for (int i = 0; i < 3; i++) { n       Item tmp = it; n       tmp.x[i]--; n       if (check(tmp)) { n         f = true; n         break; n       } n       tmp.x[i] += 2; n       if (check(tmp)) { n         f = true; n         break; n       } n     } n     for (int i = 0; i < 3; i++) { n       Item tmp = it; n       tmp.y[i]--; n       if (check(tmp)) { n         f = true; n         break; n       } n       tmp.y[i] += 2; n       if (check(tmp)) { n         f = true; n         break; n       } n     } n     if (f == true) n       cout <<  ALMOST n ; n     else n       cout <<  NEITHER n ; n   } n   return 0; n } n bool check(Item t) { n   int d[3]; n   int xx[3], yy[3]; n   d[0] = Dist(t.x[0], t.y[0], t.x[1], t.y[1]); n   d[1] = Dist(t.x[0], t.y[0], t.x[2], t.y[2]); n   d[2] = Dist(t.x[1], t.y[1], t.x[2], t.y[3]); n   if (!d[0] || !d[1] || !d[2]) return false; n   if (d[0] + d[1] == d[2] || d[1] + d[2] == d[0] || d[0] + d[2] == d[1]) n     return false; n   xx[0] = t.x[0] - t.x[1]; n   yy[0] = t.y[0] - t.y[1]; n   xx[1] = t.x[0] - t.x[2]; n   yy[1] = t.y[0] - t.y[2]; n   xx[2] = t.x[1] - t.x[2]; n   yy[2] = t.y[1] - t.y[2]; n   if (xx[0] * xx[1] + yy[0] * yy[1] == 0 || n       xx[0] * xx[2] + yy[0] * yy[2] == 0 || xx[1] * xx[2] + yy[1] * yy[2] == 0) n     return true; n   else n     return false; n } n


#include <bits/stdc++.h> n using namespace std; n void input(long long int n, long long int a[]) { n   for (long long int i = 0; i < n; ++i) cin >> a[i]; n } n long long int bin_expo(long long int A, long long int B, long long int M) { n   long long int result = 1ll; n   while (B > 0) { n     if (B % 2 == 1) { n       result = (result * A) % M; n     } n     A = (A * A) % M; n     B = B / 2; n   } n   return result; n } n long long int gcd(long long int a, long long int b) { n   if (b == 0) return a; n   return gcd(b, a % b); n } n long long int lcm(long long int a, long long int b) { n   return (a * b) / gcd(a, b); n } n long long int n, l; n long long int a[101], b[101]; n long long int dp[4001][200][3]; n long long int solve(long long int left, long long int prev, n                     long long int flag) { n   if (left < 0) return 0LL; n   if (left == 0) return 1LL; n   if (dp[left][prev][flag] != -1) return dp[left][prev][flag]; n   long long int res = 0; n   if (prev == -1) { n     for (long long int i = 0; i < n; ++i) { n       res += solve(left - a[i], i, 2); n       if (a[i] != b[i]) res += solve(left - b[i], i, 1); n       res %= 1000000007; n     } n   } else { n     for (long long int i = 0; i < n; ++i) { n       if (i != prev) { n         long long int cur; n         if (flag == 1) cur = a[prev]; n         if (flag == 2) cur = b[prev]; n         if (a[i] == cur) n           res += solve(left - a[i], i, 2); n         else if (b[i] == cur) n           res += solve(left - b[i], i, 1); n         res %= 1000000007; n       } n     } n   } n   return dp[left][prev][flag] = res % 1000000007; n } n int main() { n   memset(dp, -1, sizeof(dp)); n   cin >> n >> l; n   for (long long int i = 0; i < n; ++i) cin >> a[i] >> b[i]; n   cout << solve(l, -1, 0) << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int sum, t, p, pos, a[100005], b[100005], n, f, c, f1; n   cin >> t; n   while (t--) { n     memset(a, 0, sizeof(a)); n     memset(b, 0, sizeof(b)); n     cin >> n; n     f = 1; n     sum = 0; n     p = 0; n     f1 = 1, c = 0; n     for (int i = 0; i < n; i++) cin >> a[i]; n     for (int i = 0; i < n; i++) cin >> b[i]; n     for (int i = 0; i < n; i++) { n       if (f == 1 && a[i] != b[i]) { n         if (p == 0) pos = i, p = 1, c = b[i] - a[i]; n         sum++; n         f = 0; n       } else if (a[i] > b[i]) n         f1 = 0; n       else if (a[i] == b[i]) n         f = 1; n     } n     if (sum >= 2) { n       cout <<  NO  << endl; n     } else if (f1 == 0 || c < 0) n       cout <<  NO  << endl; n     else { n       if (sum == 0) n         cout <<  YES  << endl; n       else { n         for (int i = pos; a[i] != b[i]; i++) { n           if (b[i] - a[i] != c) f1 = 0; n         } n         if (f1 == 0) n           cout <<  NO  << endl; n         else n           cout <<  YES  << endl; n       } n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n int left_[105], right_[105], mark[105]; n int main() { n   int n, j; n   std::ios::sync_with_stdio(0); n   while (~scanf( %d , &n)) { n     int ans = 0; n     memset(left_, 0, sizeof(left_)); n     memset(right_, 0, sizeof(right_)); n     memset(mark, 0, sizeof(mark)); n     for (int i = 1; i <= n; i++) { n       scanf( %d %d , &left_[i], &right_[i]); n       if (left_[i] == 0) mark[++ans] = i; n     } n     for (int i = 1; i < ans; i++) { n       for (j = mark[i]; right_[j] != 0; j = right_[j]) n         ; n       left_[mark[i + 1]] = j; n       right_[j] = mark[i + 1]; n     } n     for (int i = 1; i <= n; i++) printf( %d %d n , left_[i], right_[i]); n   } n } n


#include <bits/stdc++.h> n using namespace std; n int gcd(int f, int s) { n   if (s == 0) n     return f; n   else n     return gcd(s, f % s); n } n int const N = 1000006; n long long const M = 1020; n long double const ep = .000000000000000001; n long long arr[N], arr1[N]; n int main() { n   int n, m; n   scanf( %d , &n); n   long long sum = 0, sum1 = 0; n   for (int i = 0; i < n; i++) { n     scanf( %lld , arr + i); n     sum += arr[i]; n   } n   scanf( %d , &m); n   for (int i = 0; i < m; i++) { n     scanf( %lld , arr1 + i); n     sum1 += arr1[i]; n   } n   if (sum == sum1) { n     printf( 0 n0 ); n     return 0; n   } n   long long best = abs(sum - sum1); n   int ind = -1, ind1 = -1; n   for (int i = 0; i < n; i++) { n     long long temp = sum, temp1 = sum1; n     for (int x = 0; x < m; x++) { n       temp -= arr[i]; n       temp += arr1[x]; n       temp1 -= arr1[x]; n       temp1 += arr[i]; n       if (abs(temp - temp1) < best) { n         ind = i + 1, ind1 = x + 1; n         best = abs(temp - temp1); n       } n       temp = sum, temp1 = sum1; n     } n   } n   int indd = -1, indd1 = -1, indd2 = -1, indd21 = -1; n   vector<pair<long long, pair<int, int> > > v, v1; n   for (int i = 0; i < n; i++) n     for (int x = i + 1; x < n; x++) n       v.push_back(make_pair(arr[i] + arr[x], make_pair(i, x))); n   for (int i = 0; i < m; i++) n     for (int x = i + 1; x < m; x++) n       v1.push_back(make_pair(arr1[i] + arr1[x], make_pair(i, x))); n   sort(v1.begin(), v1.end()); n   long long want = (sum - sum1) / 2; n   long long test = 1e17; n   long long num = -1; n   for (int i = 0; i < v.size(); i++) { n     int left = 0, right = v1.size() - 1, mid; n     while (left <= right) { n       mid = (left + right) / 2; n       long long curr = v[i].first - v1[mid].first; n       if (test > abs(curr - want)) { n         test = abs(curr - want); n         num = curr; n         indd = v[i].second.first + 1, indd1 = v[i].second.second + 1; n         indd2 = v1[mid].second.first + 1, indd21 = v1[mid].second.second + 1; n       } n       if (curr > want) n         left = mid + 1; n       else n         right = mid - 1; n     } n   } n   long long s = sum, s1 = sum1; n   s -= num; n   s1 += num; n   if (ind == -1 && indd == -1) { n     printf( %lld n0 , best); n     return 0; n   } n   if (best > abs(s - s1)) { n     printf( %lld n , abs(s - s1)); n     printf( 2 n%d %d n%d %d , indd, indd2, indd1, indd21); n   } else { n     printf( %lld n , best); n     printf( 1 n%d %d , ind, ind1); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long fact[100005]; n long long power(long long x, long long y, long long p) { n   long long res = 1; n   x = x % p; n   while (y > 0) { n     if (y & 1) res = ((res % p) * (x % p)) % p; n     y = y >> 1; n     x = ((x % p) * (x % p)) % p; n   } n   return res; n } n long long modInverse(long long n, long long p) { return power(n, p - 2, p); } n long long nCrModFermat(long long n, long long r, long long p) { n   if (r == 0 || r == n) return 1; n   return (((fact[n] % p) * (modInverse(fact[r], p) % p)) % p * n           (modInverse(fact[n - r], p) % p)) % n          p; n } n void sieve(vector<bool> &isPrime) { n   long long n = isPrime.size(); n   isPrime[0] = false; n   isPrime[1] = false; n   for (long long i = 2; i * i <= n; i++) { n     if (isPrime[i] == true) { n       for (long long j = i * i; j <= n; j += i) isPrime[j] = false; n     } n   } n } n void addEdge(vector<long long> adj[], long long u, long long v) { n   adj[u].push_back(v); n   adj[v].push_back(u); n } n void BFS(long long s, vector<long long> adj[], vector<bool> &visited) { n   deque<long long> q; n   q.push_back(s); n   visited[s] = true; n   cout << s <<    ; n   while (!q.empty()) { n     long long p = q.front(); n     q.pop_front(); n     for (long long i = 0; i < adj[p].size(); i++) { n       if (visited[adj[p][i]] == false) { n         q.push_back(adj[p][i]); n         visited[adj[p][i]] = true; n         cout << adj[p][i] <<    ; n       } n     } n   } n } n void DFSUtil(long long u, vector<long long> adj[], vector<bool> &visited) { n   visited[u] = true; n   cout << u <<    ; n   for (long long i = 0; i < adj[u].size(); i++) n     if (visited[adj[u][i]] == false) DFSUtil(adj[u][i], adj, visited); n } n void DFS(vector<long long> adj[], long long V) { n   vector<bool> visited(V, false); n   for (long long u = 0; u < V; u++) n     if (visited[u] == false) DFSUtil(u, adj, visited); n } n void ChubbyCoder() { n   long long n; n   cin >> n; n   cout << n <<   n ; n } n signed main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   ; n   long long t; n   t = 1; n   cin >> t; n   while (t--) { n     ChubbyCoder(); n   } n } n


#include <bits/stdc++.h> n using namespace std; n bool seat[100][100] = {0}; n int main() { n   int n, k; n   scanf( %d %d , &n, &k); n   while (n--) { n     int num, x, l, r, min_sum; n     min_sum = 100000; n     scanf( %d , &num); n     for (int i = 1; i <= k; i++) { n       for (int j = 1; j <= k - num + 1; j++) { n         int flag, sum; n         sum = 0; n         flag = 0; n         for (int m = j; m <= j + num - 1; m++) { n           if (seat[i][m]) { n             flag = 1; n             break; n           } n           sum += abs(i - k / 2 - 1) + abs(m - k / 2 - 1); n         } n         if (flag == 0 && sum < min_sum) { n           x = i; n           l = j; n           r = j + num - 1; n           min_sum = sum; n         } n       } n     } n     if (min_sum == 100000) n       printf( -1 n ); n     else { n       printf( %d %d %d n , x, l, r); n       for (int i = l; i <= r; i++) seat[x][i] = 1; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   cin >> n; n   if (n % 2 == 0) { n     if (n >= 8 && n % 4 == 0) n       cout << ((n / 4) - 1) << endl; n     else if (n > 4 && n < 8) n       cout <<  1  << endl; n     else if (n <= 4) n       cout <<  0  << endl; n     else n       cout << (n / 4) << endl; n   } else { n     cout <<  0  << endl; n   } n   return 0; n } n


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


#include <bits/stdc++.h> n using namespace std; n int gcd(int a, int b) { n   if (b == 0) return a; n   return gcd(b, a % b); n } n int main() { n   int a, i, temp; n   long long int sum = 0, n, fra; n   cin >> a; n   for (i = 2; i <= a - 1; i++) { n     temp = a; n     while (temp > 0) { n       sum += temp % i; n       temp = temp / i; n     } n   } n   temp = a - 2; n   fra = gcd(sum, temp); n   while (fra != 1) { n     sum = sum / fra; n     temp = temp / fra; n     fra = gcd(sum, temp); n   } n   cout << sum <<  /  << temp << endl; n } n


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


#include <bits/stdc++.h> n using namespace std; n const double eps = 1e-8; n const int M = 1e5 + 5; n const int N = 1e6 + 5; n const int mod = 1e9 + 7; n const double pi = acos(-1.0); n const int INF = 0x3f3f3f3f; n const long long lINF = 0x3f3f3f3f3f3f3f3fLL; n const int sINF = 0xcfcfcfcf; n char s[M * 2], s1[M]; n int mx, tmp, len; n int main() { n   scanf( %s , s1); n   len = strlen(s1); n   mx = tmp = 1; n   strcpy(s, s1); n   strcat(s + len, s1); n   for (int i = 1; i < len * 2; i++) { n     if (s[i] == s[i - 1]) { n       mx = max(mx, tmp); n       tmp = 1; n     } else { n       tmp++; n       mx = max(mx, tmp); n     } n   } n   mx = min(mx, len); n   printf( %d n , mx); n } n