#include <bits/stdc++.h> n using namespace std; n const int INF = 1.01e9; n const double PI = acos(-1); n bool eq(double A, double B) { return abs(A - B) < 1e-9; } n bool ls(double A, double B) { return A < B && !eq(A, B); } n bool le(double A, double B) { return A < B || eq(A, B); } n struct pt { n   double x, y, id; n   pt() {} n   pt(double xx, double yy) : x(xx), y(yy) {} n   pt operator+(pt A) { return pt(x + A.x, y + A.y); } n   pt operator-(pt A) { return pt(x - A.x, y - A.y); } n   pt operator*(double k) { return pt(x * k, y * k); } n   pt operator/(double k) { n     assert(abs(k) > 1e-20); n     return pt(x / k, y / k); n   } n   double operator*(pt A) { return x * A.y - y * A.x; } n   double operator%(pt A) { return x * A.x + y * A.y; } n   bool operator==(pt A) { return eq(x, A.x) && eq(y, A.y); } n   bool operator!=(pt A) { return !eq(x, A.x) || !eq(y, A.y); } n   double len() { return sqrt(x * x + y * y); } n   double len2() { return x * x + y * y; } n   pt rotate() { return pt(-y, x); } n   pt norm() { return (*this) / len(); } n   bool operator<(pt A) { return x < A.x || (eq(x, A.x) && ls(y, A.y)); } n   void read() { scanf( %lf%lf , &x, &y); } n   void Epr() { cerr <<  x y:   << x <<     << y << endl; } n }; n struct Line { n   pt O, v; n   Line(pt A, pt B) : O(A), v((B - A).norm()) {} n   pt operator*(Line l) { n     pt u = l.v.rotate(); n     double t = (l.O - O) % u / (v % u); n     return O + v * t; n   } n }; n int main() { n   int n, q; n   scanf( %d%d , &n, &q); n   vector<pt> p(n); n   for (int i = 0; i < n; i++) { n     p[i].read(); n   } n   reverse((p).begin(), p.end()); n   int idMn = 0; n   int idMx = 0; n   for (int i = 0; i < n; i++) { n     if (p[i] < p[idMn]) { n       idMn = i; n     } n     if (p[idMx] < p[i]) { n       idMx = i; n     } n   } n   vector<double> tr(n); n   vector<double> prefArea(n + 1); n   auto getTrap = [&](pt A, pt B) { return (A.x - B.x) * (A.y + B.y) / 2; }; n   for (int i = 0; i < n; i++) { n     tr[i] = getTrap(p[i], p[(i + 1) % n]); n     prefArea[i + 1] = prefArea[i] + tr[i]; n   } n   for (int i = 0; i < n; i++) p[i].id = i; n   vector<pt> part[2]; n   for (int tt = 0; tt < 2; tt++) { n     for (int i = idMn; i != idMx; i = (i + 1) % n) { n       part[tt].push_back(p[i]); n     } n     part[tt].push_back(p[idMx]); n     swap(idMn, idMx); n   } n   auto getFarPoint = [&](pt v) { n     int where = 0; n     int tt; n     if (v.y < 0) { n       tt = 0; n     } else { n       tt = 1; n     } n     int l = 0; n     int r = part[tt].size() - 1; n     while (r - l > 3) { n       int m1 = (l + l + r) / 3; n       int m2 = (l + r + r) / 3; n       if (part[tt][m1] % v > part[tt][m2] % v) { n         r = m2; n       } else { n         l = m1; n       } n     } n     for (int i = l; i <= r; i++) { n       if (part[tt][i] % v > p[where] % v) { n         where = part[tt][i].id; n       } n     } n     return where; n   }; n   auto getSegArea = [&](int l, int r) { n     l %= n; n     r %= n; n     if (l <= r) { n       return prefArea[r] - prefArea[l]; n     } n     return prefArea[n] - prefArea[l] + prefArea[r]; n   }; n   auto getArea = [&](pt O, double ang) { n     pt v(cos(ang), sin(ang)); n     pt u = v.rotate(); n     int p1 = getFarPoint(u); n     int p2 = getFarPoint(u * -1); n     int ret[2]; n     for (int tt = 0; tt < 2; tt++) { n       int l = p2; n       int r = p1; n       assert(l != r); n       if (r < l) { n         r += n; n       } n       while (r - l > 1) { n         if ((p[(l + r) / 2 % n] - O) * v > 0) { n           l = (l + r) / 2; n         } else { n           r = (l + r) / 2; n         } n       } n       ret[tt] = l; n       v = v * -1; n       swap(p1, p2); n     } n     pt G1 = Line(p[ret[0] % n], p[(ret[0] + 1) % n]) * Line(O, O + v); n     pt G2 = Line(p[ret[1] % n], p[(ret[1] + 1) % n]) * Line(O, O + v); n     double res = 0; n     res += getSegArea((ret[0] + 1) % n, ret[1]); n     res += getTrap(p[ret[1] % n], G2); n     res += getTrap(G2, G1); n     res += getTrap(G1, p[(ret[0] + 1) % n]); n     return res; n   }; n   for (int tt = 0; tt < q; tt++) { n     pt O; n     O.read(); n     double l, r; n     double res = getArea(O, 0); n     if (res * 2 < prefArea.back()) { n       l = 0; n       r = PI; n     } else { n       l = PI; n       r = PI * 2; n     } n     for (int it = 0; it < 40; it++) { n       double tmp = getArea(O, (l + r) / 2); n       if (tmp * 2 < prefArea.back()) { n         l = (l + r) / 2; n       } else { n         r = (l + r) / 2; n       } n     } n     double ang = (l + r) / 2; n     if (ang > PI) { n       ang -= PI; n     } n     printf( %.17f n , ang); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, t; n int ult, casas; n string s; n int solve(int k) { n   int u = -1; n   int acc = k; n   for (int i = 0; i < n; i++) n     if (s[i] ==  S ) { n       acc++; n       if (acc >= casas) { n         u = i; n         break; n       } n     } n   if (u == -1) return 2000000000; n   u = max(u, ult); n   acc = k; n   int last = 2000000000; n   int t = 1, need = 0; n   int ans = 2000000000; n   for (int i = 0; i < n; i++) { n     if (s[i] ==  H ) { n       if (acc) { n         acc--; n         if (i == ult && need == 0) ans = min(ans, t); n       } else { n         last = min(last, i); n         need++; n       } n       t++; n     } else if (s[i] ==  S ) { n       acc++; n       if (need && acc >= need) { n         ans = min(ans, t + 2 * (u - i) + (i - last)); n         acc -= need; n         need = 0; n         if (i >= ult) { n           t += (i - last); n           ans = min(ans, t); n           return ans; n         } else n           t += (2 * (i - last)) + 1; n         last = 2000000000; n       } else n         t++; n     } else n       t++; n   } n   return ans; n } n int main() { n   cin >> n >> t; n   cin >> s; n   casas = 0; n   ult = -1; n   for (int i = 0; i < n; i++) n     if (s[i] ==  H ) casas++, ult = max(ult, i); n   int ini = 0, fim = casas; n   if (solve(fim) > t) { n     printf( -1 n ); n     return 0; n   } n   while (ini < fim) { n     int mid = (ini + fim) / 2; n     if (solve(mid) <= t) n       fim = mid; n     else n       ini = mid + 1; n   } n   cout << ini << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const bool ready = []() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout << fixed << setprecision(12); n   return true; n }(); n const double PI = acos(-1); n using ll = long long; n using pii = pair<ll, ll>; n using pdd = pair<double, double>; n using vd = vector<double>; n using vb = vector<bool>; n using vi = vector<ll>; n using vvi = vector<vi>; n using vs = vector<string>; n void solve() { n   ll n; n   cin >> n; n   ; n   ll m; n   cin >> m; n   ; n   vb sx(n + 1); n   vb sy(n + 1); n   for (ll i = 0; i < m; i++) { n     ll x; n     cin >> x; n     ; n     ll y; n     cin >> y; n     ; n     sx[x] = true; n     sy[y] = true; n   } n   ll ans = 0; n   for (ll i = 2; i < n / 2 + 1; i++) { n     if (!sy[n - i + 1]) ans++; n     if (!sy[i]) ans++; n     if (!sx[n - i + 1]) ans++; n     if (!sx[i]) ans++; n   } n   if (n & 1) { n     if ((!sx[n / 2 + 1]) || (!sy[n / 2 + 1])) ans++; n   } n   cout << ans << endl; n } n signed main() { solve(); } n


#include <bits/stdc++.h> n using namespace std; n template <typename T> n T id(T b) { n   return b; n }; n template <class It> n bool all(It f, It l) { n   return std::all_of(f, l, id<bool>); n } n template <class It> n bool any(It f, It l) { n   return std::any_of(f, l, id<bool>); n } n const int MAX_N = 2000, MAX_A = 1000000; n int n, k, s; n int a[MAX_N]; n int main() { n   cin >> k; n   s = a[0] = -1; n   n = 1; n   while (s - n + 1 != k) { n     a[n] = min(k - (s - n), MAX_A); n     s += a[n]; n     n++; n   } n   cout << n << endl; n   for (int i = 0; i < n; i++) cout << a[i] <<    n [i + 1 == n]; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int arr[1007]; n int main() { n   char c; n   int t = 3; n   while (t--) { n     scanf(  %c , &c); n     char temp; n     scanf( %c , &temp); n     char d; n     scanf( %c , &d); n     if (temp ==  > ) { n       arr[c]++; n       arr[d]--; n     } else { n       arr[c]--; n       arr[d]++; n     } n   } n   int flag1 = 0; n   char x, y, z; n   int flag2 = 0; n   int flag3 = 0; n   for (int i =  A ; i <=  C ; i++) { n     if (arr[i] == 2) { n       flag1 = 1; n       x = (char)i; n     } n     if (arr[i] == 0) { n       flag2 = 1; n       y = (char)i; n     } n     if (arr[i] == -2) { n       flag3 = 1; n       z = (char)i; n     } n   } n   if (flag1 && flag2 && flag3) { n     cout << z << y << x << endl; n   } else n     puts( Impossible ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   cin >> n; n   vector<int> q(3 * n + 1); n   for (int i = 0; i < 3 * n; i++) { n     int a; n     cin >> a; n     q[a] = i; n   } n   vector<vector<int>> v(n, vector<int>(3)); n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < 3; j++) { n       cin >> v[i][j]; n     } n   } n   int is; n   cin >> is; n   int team; n   int ind; n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < 3; j++) { n       if (v[i][j] == is) { n         team = i; n         ind = j; n       } n     } n   } n   int m = 1e9; n   for (int i = 0; i < 3; i++) { n     m = min(m, q[v[team][i]]); n   } n   if (q[is] == m) { n     vector<int> ans; n     vector<int> dop; n     int mi = 1e9; n     for (int i = 0; i < 3; i++) { n       if (v[team][i] < mi && i != ind) mi = v[team][i]; n     } n     ans.push_back(mi); n     mi = 0; n     for (int i = 0; i < 3; i++) { n       if (v[team][i] > mi && i != ind) mi = v[team][i]; n     } n     ans.push_back(mi); n     for (int i = 0; i < team; i++) { n       for (int j = 0; j < 3; j++) { n         if (v[i][j] < mi) n           ans.push_back(v[i][j]); n         else n           dop.push_back(v[i][j]); n       } n     } n     sort(ans.begin(), ans.end()); n     for (int i = team + 1; i < n; i++) { n       for (int j = 0; j < 3; j++) { n         dop.push_back(v[i][j]); n       } n     } n     sort(dop.begin(), dop.end()); n     for (int i = 0; i < ans.size(); i++) { n       cout << ans[i] <<    ; n     } n     for (int i = 0; i < dop.size(); i++) { n       cout << dop[i] <<    ; n     } n   } else { n     for (int i = 1; i <= 3 * n; i++) { n       if (i != is) { n         cout << i <<    ; n       } n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n long long gcd(long long a, long long b) { n   if (b == 0) return a; n   return gcd(b, a % b); n } n long long max(long long a, long long b) { return (a > b ? a : b); } n void printa(long long arr[], long long n) { n   for (long long i = 0; i < n; i++) { n     cout << arr[i] <<    ; n   } n   cout <<   n ; n } n void printv(vector<long long> v) { n   for (long long i = 0; i < v.size(); i++) { n     cout << v[i] <<    ; n   } n   cout <<   n ; n } n void printset(set<long long> s) { n   for (auto x : s) { n     cout << x <<    ; n   } n   cout <<   n ; n } n void printmset(multiset<long long> s) { n   for (auto x : s) { n     cout << x <<    ; n   } n   cout <<   n ; n } n void printmgset(multiset<long long, greater<long long>> s) { n   for (auto x : s) { n     cout << x <<    ; n   } n   cout <<   n ; n } n const long long inf = 1e18; n const int32_t M = 1e9 + 7; n const int32_t MM = 998244353; n long long fact(long long n) { return (n == 1 || n == 0) ? 1 : n * fact(n - 1); } n const long long N = 1e5 + 1; n void solve() { n   long long n; n   cin >> n; n   vector<long long> v(n); n   for (long long i = 0; i < (n); ++i) { n     cin >> v[i]; n   } n   long long x, f; n   cin >> x >> f; n   long long c = 0; n   long long idk = 0; n   for (long long i = 0; i < (n); ++i) { n     if (v[i] > x) { n       idk += ((v[i] - x) / (x + f) + ((v[i] - x) % (x + f) > 0)); n     } n   } n   cout << idk * f <<   n ; n } n signed main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   long long t = 1; n   while (t--) { n     solve(); n   } n   cerr <<   nTime elapsed:   << 1000 * clock() / CLOCKS_PER_SEC <<  ms n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e5 + 7; n int n, m, x[N], y[N], num[N], val[N], dp[2][N], cnt; n vector<int> adj[N]; n bool vis[N]; n void DFS(int u) { n   vis[u] = true; n   for (int i = 0; i < (int)adj[u].size(); ++i) { n     int v = adj[u][i]; n     if (vis[v] == false) DFS(v); n   } n   num[u] = cnt; n   val[cnt--] = u; n } n bool Check(int k) { n   for (int i = 1; i <= n; ++i) adj[i].clear(); n   for (int i = 1; i <= k; ++i) adj[x[i]].push_back(y[i]); n   for (int i = 1; i <= n; ++i) num[i] = 0, vis[i] = false; n   cnt = n; n   for (int i = 1; i <= n; ++i) n     if (vis[i] == false) DFS(i); n   memset(dp, 0, sizeof dp); n   for (int u = 1; u <= n; ++u) { n     for (int i = 0; i < (int)adj[val[u]].size(); ++i) { n       int v = adj[val[u]][i]; n       dp[0][v] = max(dp[0][v], dp[0][val[u]] + 1); n     } n   } n   for (int u = n; u >= 1; --u) { n     for (int i = 0; i < (int)adj[val[u]].size(); ++i) { n       int v = adj[val[u]][i]; n       dp[1][val[u]] = max(dp[1][val[u]], dp[1][v] + 1); n     } n   } n   for (int i = 1; i <= n; ++i) n     if (dp[0][i] + 1 + dp[1][i] != n) return false; n   return true; n } n int main() { n   scanf( %d%d , &n, &m); n   for (int i = 1; i <= m; ++i) scanf( %d%d , &x[i], &y[i]); n   int l = 1, r = m, res = -1; n   while (l <= r) { n     int mid = (l + r) >> 1; n     if (Check(mid)) { n       res = mid; n       r = mid - 1; n     } else n       l = mid + 1; n   } n   printf( %d , res); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, m; n   scanf( %d %d , &n, &m); n   vector<vector<int>> x(m + 1, vector<int>(n + 1)); n   for (int i = 1; i <= n; i++) { n     int k; n     scanf( %d , &k); n     for (int j = 1; j <= m; j++) { n       x[j][i] = x[j][i - 1]; n       if (j == k) { n         x[j][i] += 1; n       } n     } n   } n   vector<int> y(m + 1); n   for (int i = 1; i <= m; i++) { n     scanf( %d , &y[i]); n   } n   for (int i = 1; i <= n; i++) { n     for (int j = i; j <= n; j++) { n       for (int k = 1; k <= m; k++) { n         if (x[k][j] - x[k][i - 1] != y[k]) { n           break; n         } n         if (k == m) { n           printf( YES n ); n           return 0; n         } n       } n     } n   } n   printf( NO n ); n } n


#include <bits/stdc++.h> n using namespace std; n int IN() { n   int x = 0, f = 0, ch; n   for (; (ch = getchar()) <  0  || ch >  9 ;) f = (ch ==  - ); n   for (; ch >=  0  && ch <=  9 ; (ch = getchar())) x = x * 10 + ch -  0 ; n   return f ? -x : x; n } n const double Pi = acos(-1), eps = 1e-11; n int N, M, T, C, ID[2005]; n struct Nod { n   double x, y; n } A[2005], B[2005]; n struct Opt { n   double x; n   int k, t; n } Q[2005 << 3]; n bool Equ(double a, double b) { return abs(a - b) < eps; } n double Dis(const Nod& a, const Nod& b) { n   return sqrt((a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y)); n } n void Upd(double l, double r, int k) { n   if (r - l < 0) return; n   for (; l < 0; l += 2 * Pi) n     ; n   for (; l > 2 * Pi; l -= 2 * Pi) n     ; n   for (; r < 0; r += 2 * Pi) n     ; n   for (; r > 2 * Pi; r -= 2 * Pi) n     ; n   if (l < r) n     Q[++T] = (Opt){l, k, 1}, Q[++T] = (Opt){r, k, -1}; n   else n     Q[++T] = (Opt){0, k, 1}, Q[++T] = (Opt){r, k, -1}, Q[++T] = (Opt){l, k, 1}, n     Q[++T] = (Opt){2 * Pi, k, -1}; n } n bool Cmp(const Opt& a, const Opt& b) { return a.x < b.x; } n bool RightRed(int d, double r) { n   T = 0; n   Upd(0, 2 * Pi, 1); n   for (int i = 1, lim = M; i <= lim; i++) { n     double a = Dis(A[d], B[i]); n     if (r + r <= a) continue; n     double o = atan2(B[i].y - A[d].y, B[i].x - A[d].x), t = acos(a / r / 2); n     Upd(o - t, o + t, 0); n   } n   sort(Q + 1, Q + T + 1, Cmp); n   int s1 = 0, s2 = 0; n   for (int i = 1, j; i <= T; i = j) { n     for (j = i; j <= T && Equ(Q[i].x, Q[j].x); j++) n       if (Q[j].k == 1) n         s1 += Q[j].t; n       else n         s2 += Q[j].t; n     if (s1 && !s2) return 1; n   } n   return 0; n } n bool RightBlue(int d, double r) { n   T = 0; n   for (int i = 1, lim = M; i <= lim; i++) n     if (i != d) { n       double a = Dis(B[d], B[i]); n       if (r + r <= a) continue; n       double o = atan2(B[i].y - B[d].y, B[i].x - B[d].x), t = acos(a / r / 2); n       Upd(o - t, o + t, 0); n     } n   for (int i = 1, lim = N; i <= lim; i++) { n     double a = Dis(B[d], A[i]); n     if (r + r <= a) continue; n     double o = atan2(A[i].y - B[d].y, A[i].x - B[d].x), t = acos(a / r / 2); n     Upd(o - t, o + t, 1); n   } n   sort(Q + 1, Q + T + 1, Cmp); n   int s1 = 0, s2 = 0; n   for (int i = 1, j; i <= T; i = j) { n     for (j = i; j <= T && Equ(Q[i].x, Q[j].x); j++) n       if (Q[j].k == 1) n         s1 += Q[j].t; n       else n         s2 += Q[j].t; n     if (s1 && !s2) return 1; n   } n   return 0; n } n bool check(double r) { n   for (int i = 1, lim = N; i <= lim; i++) n     if (RightRed(i, r)) return 1; n   for (int i = 1, lim = M; i <= lim; i++) n     if (RightBlue(i, r)) return 1; n   return 0; n } n int main(int argc, char* argv[]) { n   N = IN(); n   M = IN(); n   for (int i = 1, lim = N; i <= lim; i++) A[i] = (Nod){1. * IN(), 1. * IN()}; n   for (int i = 1, lim = M; i <= lim; i++) B[i] = (Nod){1. * IN(), 1. * IN()}; n   if (check(1e10)) return puts( -1 ), 0; n   for (int i = 1, lim = N; i <= lim; i++) ID[++C] = i; n   for (int i = 1, lim = M; i <= lim; i++) ID[++C] = N + i; n   random_shuffle(ID + 1, ID + N + 1); n   random_shuffle(ID + N + 1, ID + C + 1); n   double Ans = 0; n   for (int k = 1, lim = C; k <= lim; k++) { n     if (ID[k] <= N) { n       int i = ID[k]; n       if (RightRed(i, Ans)) { n         double l = Ans, r = 1e10; n         for (; l + (1e-6) < r;) { n           double Mid = (l + r) * .5; n           if (RightRed(i, Mid)) n             l = Mid; n           else n             r = Mid; n         } n         Ans = l; n       } n     } else { n       int i = ID[k] - N; n       if (RightBlue(i, Ans)) { n         double l = Ans, r = 1e10; n         for (; l + (1e-6) < r;) { n           double Mid = (l + r) * .5; n           if (RightBlue(i, Mid)) n             l = Mid; n           else n             r = Mid; n         } n         Ans = l; n       } n     } n   } n   printf( %.12lf n , Ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n double EPS = 1e-9; n int INF = 1000000005; n long long INFF = 1000000000000000005ll; n double PI = acos(-1); n int dirx[8] = {-1, 0, 0, 1, -1, -1, 1, 1}; n int diry[8] = {0, 1, -1, 0, -1, 1, -1, 1}; n long long MOD = 1000000007; n template <typename T_vector> n void output_vector(const T_vector &v, bool line_break = false, n                    bool add_one = false, int start = -1, int end = -1) { n   if (start < 0) start = 0; n   if (end < 0) end = int(v.size()); n   for (int i = start; i < end; i++) { n     cout << v[i] + (add_one ? 1 : 0) n          << (line_break    ?   n  n              : i < end - 1 ?     n                            :   n ); n   } n } n void solve() { n   int n; n   cin >> n; n   vector<int> a(n); n   for (auto &(z) : a) cin >> z; n   ; n   int ans = 0; n   for (int(i) = (0); (i) < (n); ++(i)) { n     int j = i; n     while (j + 1 < n && a[j + 1] <= a[j] * 2) j++; n     ans = max(ans, j - i + 1); n     i = j; n   } n   cout << ans << endl; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 100010; n const int D = 10000010; n const int MAXW = N + 1000; n const int mod = 1e9 + 7; n int n, m; n int pw2[MAXW + 10]; n struct edge { n   int to, nx, w; n } ee[N * 2]; n int eq[N], en = 1; n int dn, flag; n int l[D], r[D], hs[D]; n int ans[N], root[N], pred[N]; n bool done[N]; n int sv[N], svt; n bool cmp(int u, int v) { n   if (!l[u] && !r[u]) n     return hs[u] <= hs[v]; n   else n     return hs[r[u]] == hs[r[v]] ? cmp(l[u], l[v]) : cmp(r[u], r[v]); n } n struct compare { n   bool operator()(const pair<int, int>& u, const pair<int, int>& v) { n     if (hs[u.first] == hs[v.first]) n       return u > v; n     else n       return !cmp(u.first, v.first); n   } n }; n priority_queue<pair<int, int>, vector<pair<int, int> >, compare> Q; n void update(int d, int lt, int rt) { n   int md = (lt + rt) >> 1; n   hs[d] = (1LL * hs[r[d]] * pw2[md - lt + 1] + hs[l[d]]) % mod; n } n int add(int d, int lt, int rt) { n   int nd = ++dn, md = (lt + rt) >> 1; n   if (lt == rt) n     hs[nd] = 1; n   else { n     if ((hs[l[d]] + 1) % mod != pw2[md - lt + 1]) { n       l[nd] = add(l[d], lt, md); n       r[nd] = r[d]; n     } else { n       l[nd] = 0; n       r[nd] = add(r[d], md + 1, rt); n     } n     update(nd, lt, rt); n   } n   return nd; n } n int add(int d, int lt, int rt, int w) { n   int nd = ++dn, md = (lt + rt) >> 1; n   if (lt == rt) { n     if (hs[d]) n       return 0; n     else { n       flag = 1; n       return hs[nd] = 1, nd; n     } n   } n   if (w <= md) { n     l[nd] = add(l[d], lt, md, w); n     if (flag) { n       r[nd] = r[d]; n     } else { n       if ((hs[r[d]] + 1) % mod == pw2[rt - md]) n         r[nd] = 0; n       else { n         flag = 1; n         r[nd] = add(r[d], md + 1, rt); n       } n     } n   } else { n     l[nd] = l[d]; n     r[nd] = add(r[d], md + 1, rt, w); n   } n   update(nd, lt, rt); n   return nd; n } n void link(int u, int v, int w) { ee[++en] = (edge){v, eq[u], w}, eq[u] = en; } n int main() { n   scanf( %d %d , &n, &m); n   for (int i = 0; i <= MAXW; ++i) { n     pw2[i] = !i ? 1 : (pw2[i - 1] * 2) % mod; n   } n   for (int i = 1; i <= m; ++i) { n     int u, v, w; n     scanf( %d %d %d , &u, &v, &w); n     link(u, v, w); n     link(v, u, w); n   } n   int s, t; n   scanf( %d %d , &s, &t); n   for (int i = 1; i <= n; ++i) { n     if (i != s) { n       flag = 0; n       root[i] = add(root[i], 0, MAXW, MAXW); n     } n     Q.push(make_pair(root[i], i)); n   } n   while (!Q.empty()) { n     int u = Q.top().second; n     Q.pop(); n     if (done[u]) continue; n     done[u] = 1; n     for (int j = eq[u], v; v = ee[j].to, j; j = ee[j].nx) n       if (!done[v]) { n         flag = 0; n         int nt = add(root[u], 0, MAXW, ee[j].w); n         if (cmp(nt, root[v])) { n           root[v] = nt; n           pred[v] = u; n           ans[v] = (ans[u] + pw2[ee[j].w]) % mod; n           flag = 0; n           int nt = add(root[u], 0, MAXW, ee[j].w); n           Q.push(make_pair(root[v], v)); n         } n       } n   } n   if (s == t || pred[t]) { n     printf( %d n , ans[t]); n     svt = 0; n     for (int o = t; o; o = pred[o]) sv[++svt] = o; n     printf( %d n , svt); n     for (int i = svt; i >= 1; --i) printf( %d  , sv[i]); n     putchar(  n ); n   } else { n     printf( -1 n ); n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e6 + 5; n ; n const int inf = 1e9 + 5; n const int mod = 1e9 + 7; n const int logN = 17; n int x, y, n, m, a[N], b[N], w[N], p[N], score[N]; n vector<int> v[N]; n long long ans[N], upsum[N], downsum[N]; n bool cmp(int x, int y) { return score[x] < score[y]; } n int main() { n   scanf( %d %d , &n, &m); n   for (int i = 1; i <= n; i++) { n     scanf( %d %d , &a[i], &b[i]); n     score[i] = a[i] - b[i]; n     p[i] = i; n   } n   sort(p + 1, p + n + 1, cmp); n   for (int i = 1; i <= n; i++) { n     w[p[i]] = i; n   } n   for (int i = 1; i <= m; i++) { n     scanf( %d %d , &x, &y); n     v[x].push_back(y); n     v[y].push_back(x); n   } n   for (int i = 1; i <= n; i++) upsum[i] = upsum[i - 1] + a[p[i]]; n   for (int i = n; i >= 1; i--) downsum[i] = downsum[i + 1] + b[p[i]]; n   for (int i = 1; i <= n; i++) { n     int up = i - 1; n     int down = n - i; n     long long ups = upsum[i - 1]; n     long long dws = downsum[i + 1]; n     int cur = p[i]; n     for (auto go : v[cur]) { n       if (w[go] > i) { n         down--; n         dws -= b[go]; n       } else { n         up--; n         ups -= a[go]; n       } n     } n     ans[cur] = ups + dws + up * (long long)b[cur] + down * (long long)a[cur]; n   } n   for (int i = 1; i <= n; i++) printf( %lld  , ans[i]); n   cout << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, potezi = 1, bodovi = 0; n   scanf( %d , &n); n   pair<int, int> a[n]; n   for (int i = 0; i < n; i++) { n     scanf( %d %d , &a[i].second, &a[i].first); n   } n   sort(a, a + n); n   for (int i = n - 1; i >= 0; i--) { n     if (!potezi) break; n     potezi += a[i].first - 1; n     bodovi += a[i].second; n   } n   printf( %d n , bodovi); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n string a, b; n int pas = 0; n int main() { n   cin >> a >> b; n   for (int i = 0; i < a.size(); i++) { n     if (a[i] != b[a.size() - 1 - i]) { n       pas++; n     } n   } n   if (pas > 0) { n     cout <<  NO ; n   } else { n     cout <<  YES ; n   } n } n


#include <bits/stdc++.h> n const long long N = 200005; n long long hi, ls, n, X[N], t, A[N], bk[N]; n using namespace std; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cin >> n >> t; n   for (int i = 0; i < n; ++i) { n     cin >> A[i]; n   } n   for (int i = 0; i < n; ++i) { n     cin >> X[i], --X[i]; n   } n   for (int i = 0; i < n; ++i) { n     if ((X[i] < i || (i < n - 1 && X[i] > X[i + 1])) || n         (X[i] != i && n          (X[X[i]] != X[i] || (X[i] + 1 < n && A[X[i] + 1] - 1 == A[X[i]])))) { n       cout <<  No , std::exit(0); n     } n   } n   for (int i = 0; i < n; ++i) { n     if (X[i] > i) { n       for (int j = max(hi, 1ll + i); j < X[i] + 1; ++j) { n         bk[j] = 1; n       } n       hi = max(hi, X[i]); n     } n   } n   cout <<  Yes n ; n   for (int i = 0; i < n; ++i) n     (i < n - 1 && bk[i + 1] ? hi = A[i + 1] + t : hi = A[i] + t), n         (i > 0 && hi == ls ? cout << hi + 1 <<     : cout << hi <<    ), n         ls = hi; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n; n string a; n void convA() { n   int c = 0, r = 0; n   int i; n   for (i = 1; a[i] !=  C ; i++) { n     r *= 10; n     r += a[i] -  0 ; n   } n   i++; n   for (; i < a.size(); i++) { n     c *= 10; n     c += a[i] -  0 ; n   } n   vector<int> ans; n   while (c != 0) { n     if (c % 26 == 0) { n       ans.push_back(26); n       c--; n     } else n       ans.push_back(c % 26); n     c /= 26; n   } n   for (int i = ans.size() - 1; i >= 0; i--) { n     char ab = ans[i] + ( A  - 1); n     cout << ab; n   } n   cout << r; n   cout << endl; n } n void convB() { n   vector<int> ans; n   int i; n   for (i = 0; a[i] >=  A  && a[i] <=  Z ; i++) { n     if (a[i] ==  Z ) n       ans.push_back(26); n     else n       ans.push_back(a[i] -  A  + 1); n   } n   int c = 0, r; n   for (int j = 0; j < ans.size(); j++) { n     c += ans[j] * pow(26, ans.size() - j - 1); n   } n   cout <<  R ; n   for (; i < a.size(); i++) cout << a[i]; n   cout <<  C  << c << endl; n } n int main() { n   ios::sync_with_stdio(false); n   cin >> n; n   for (int i = 0; i < n; i++) { n     cin >> a; n     bool RC = false, k = false; n     if (a.size() >= 4) { n       if (a[0] ==  R  && !(a[1] >=  A  && a[1] <=  Z )) n         RC = true; n       else n         RC = false; n       for (int i = 2; RC && i < a.size(); i++) { n         if (a[i] >=  A  && a[i] <=  Z  && k) { n           RC = false; n           break; n         } n         if (a[i] ==  C  && !k) k = true; n       } n       if (RC && k) convA(); n     } n     if (!RC || !k) convB(); n   } n } n


#include <bits/stdc++.h> n #pragma GCC optimize( -O3 ) n using namespace std; n const long long N = 1e6 + 5; n void pairsort(long long a[], long long b[], long long n) { n   pair<long long, long long> pairt[n]; n   for (long long i = 0; i < n; i++) { n     pairt[i].first = a[i]; n     pairt[i].second = b[i]; n   } n   sort(pairt, pairt + n); n   for (long long i = 0; i < n; i++) { n     a[i] = pairt[i].first; n     b[i] = pairt[i].second; n   } n } n long long gcd(long long a, long long b) { n   if (b == 0) return a; n   return gcd(b, a % b); n } n long long isPrime(long long n) { n   if (n < 2) return 0; n   if (n < 4) return 1; n   if (n % 2 == 0 or n % 3 == 0) return 0; n   for (long long i = 5; i * i <= n; i += 6) n     if (n % i == 0 or n % (i + 2) == 0) return 0; n   return 1; n } n long long C(long long n, long long r) { n   if (r > n - r) r = n - r; n   long long ans = 1; n   for (long long i = 1; i <= r; i++) { n     ans *= n - r + i; n     ans /= i; n   } n   return ans; n } n long long mod = 1e9 + 7; n long long modexpo(long long x, long long p) { n   long long res = 1; n   x = x % mod; n   while (p) { n     if (p % 2) res = res * x; n     p >>= 1; n     x = x * x % mod; n     res %= mod; n   } n   return res; n } n struct pair_hash { n   template <class T1, class T2> n   std::size_t operator()(std::pair<T1, T2> const &pair) const { n     std::size_t h1 = std::hash<T1>()(pair.first); n     std::size_t h2 = std::hash<T2>()(pair.second); n     return h1 ^ h2; n   } n }; n long long n, x, y, f[N], vis[N], a[N], k; n set<pair<long long, long long>, greater<pair<long long, long long>>> s; n int32_t main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   ; n   cin >> n >> x; n   for (long long i = 1; i <= n; i++) { n     cin >> y, a[i] = y; n     if (x == y) n       f[x]++; n     else { n       s.erase({f[y], y}), f[y]++, s.insert({f[y], y}); n       pair<long long, long long> p = *s.begin(); n       if (p.first >= f[x]) k = 1; n     } n   } n   if (!k) return cout << -1, 0; n   memset(f, 0, sizeof(f)); n   unordered_set<pair<long long, long long>, pair_hash> ss; n   for (long long i = 1; i <= 1e6; i++) n     if (x != i) ss.insert({0, i}); n   for (long long i = 1; i <= n; i++) { n     if (x == a[i]) { n       f[x]++; n       vector<long long> vv; n       for (auto it = ss.begin(); it != ss.end(); it++) n         if ((*it).first < f[x]) n           vv.push_back({(*it).second}), vis[(*it).second] = 1; n       for (long long i = 0; i < vv.size(); i++) ss.erase({f[vv[i]], vv[i]}); n     } else if (!vis[a[i]]) n       ss.erase({f[a[i]], a[i]}), f[a[i]]++, ss.insert({f[a[i]], a[i]}); n   } n   vector<long long> vv; n   for (auto it = ss.begin(); it != ss.end(); it++) n     if (vis[(*it).second]) vv.push_back((*it).second); n   for (long long i = 0; i < vv.size(); i++) ss.erase({f[vv[i]], vv[i]}); n   if (!ss.size()) return cout << -1, 0; n   cout << (*ss.begin()).second; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int m[367], f[367]; n int main() { n   memset(m, 0, sizeof(m)); n   memset(f, 0, sizeof(f)); n   int N; n   cin >> N; n   char ch; n   int a, b; n   for (int i = 0; i < N; ++i) { n     cin >> ch >> a >> b; n     if (ch ==  M ) n       for (int j = a; j <= b; ++j) ++m[j]; n     else n       for (int j = a; j <= b; ++j) ++f[j]; n   } n   int res = 0; n   for (int i = 1; i <= 366; ++i) res = max(res, min(m[i], f[i]) * 2); n   cout << res << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n char t; n int n, m; n char a[11][11]; n bool used[11][11]; n int kol = 0; n int maxleng = 0; n void dfs(int x, int y, bool ch, int leng = 0) { n   if (x < 0 || y < 0 || x >= n || y >= m) return; n   if (a[x][y] == 2) return; n   if (used[x][y]) return; n   used[x][y] = true; n   if (ch) ++leng; n   if (x - 1 >= 0 && y >= 0 && x - 1 < n && y < m && a[x][y] == !a[x - 1][y]) n     dfs(x - 1, y, !ch, leng); n   if (x >= 0 && y - 1 >= 0 && x < n && y - 1 < m && a[x][y] == !a[x][y - 1]) n     dfs(x, y - 1, !ch, leng); n   if (x >= 0 && y + 1 >= 0 && x < n && y + 1 < m && a[x][y] == !a[x][y + 1]) n     dfs(x, y + 1, !ch, leng); n   if (x + 1 >= 0 && y >= 0 && x + 1 < n && y < m && a[x][y] == !a[x + 1][y]) n     dfs(x + 1, y, !ch, leng); n   maxleng = max(maxleng, leng); n } n int main() { n   cin >> n >> m; n   for (int i = 0; i < n; ++i) n     for (int j = 0; j < m; ++j) { n       cin >> t; n       if (t ==  . ) a[i][j] = 2; n       if (t ==  P ) a[i][j] = 0; n       if (t ==  W ) a[i][j] = 1; n       used[i][j] = false; n     } n   for (int i = 0; i < n; ++i) n     for (int j = 0; j < m; ++j) n       if (!used[i][j]) maxleng = 0, dfs(i, j, 0), kol += maxleng; n   cout << kol; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = (int)2e5 + 5; n int n, t; n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   while (cin >> n) { n     int ans = 0; n     set<int> s; n     for (int i = 1; i <= n && cin >> t; ++i) { n       if (t) { n         if (s.count(t)) n           ans++; n         else n           s.insert(t); n       } else n         ans++; n     } n     cout << ans << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 1e6 + 10; n const int inf = 0x3f3f3f3f; n int main() { n   int t; n   t = 1; n   while (t--) { n     long long a, b, c, d; n     cin >> a >> b >> c >> d; n     long long ans = 0; n     for (int i = (a); i < (b + 1); i++) { n       long long l = b - 1, r = c + 2; n       while (l < r - 1) { n         long long mid = l + r >> 1; n         if (mid + i > c) { n           r = mid; n         } else { n           l = mid; n         } n       } n       if (r <= c && i + r > c) { n         if (i + r <= d + 1) { n           long long mx = min(i + c, d + 1); n           ans += (i + r - c + mx - c) * (mx - (i + r) + 1) / 2; n         } n         if (i + c > d + 1) { n           long long x = max(d + 2 - i, r); n           ans += (d + 1 - c) * (c - x + 1); n         } n       } n     } n     cout << ans << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n double alto, ancho, altomesa, anchomesa, ancho1, alto1, x_1, x_2, y_1, y_2; n double abajo() { n   if (altomesa <= y_1) return 0; n   return altomesa - y_1; n } n double arriba() { n   if (altomesa <= alto - y_2) return 0; n   return altomesa - (alto - y_2); n } n double izquierda() { n   if (anchomesa <= x_1) return 0; n   return anchomesa - x_1; n } n double derecha() { n   if (anchomesa <= ancho - x_2) return 0; n   return anchomesa - (ancho - x_2); n } n void solve() { n   cin >> ancho >> alto; n   cin >> x_1 >> y_1 >> x_2 >> y_2; n   cin >> anchomesa >> altomesa; n   ancho1 = x_2 - x_1, alto1 = y_2 - y_1; n   if (ancho1 + anchomesa > ancho && alto1 + altomesa > alto) { n     cout << -1; n     return; n   } n   double ans = 100000000; n   if (ancho1 + anchomesa <= ancho) { n     ans = min(ans, izquierda()); n     ans = min(ans, derecha()); n   } n   if (alto1 + altomesa <= alto) { n     ans = min(ans, arriba()); n     ans = min(ans, abajo()); n   } n   printf( %.6f , ans); n } n int main() { n   int T = 1; n   cin >> T; n   cout <<   n ; n   while (T--) solve(), cout <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void run(); n signed main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   cout << fixed << setprecision(3); n   srand(42); n   run(); n } n template <typename T1, typename T2> n bool mini(T1 &a, const T2 &b) { n   if (a > b) { n     a = b; n     return true; n   } n   return false; n } n template <typename T1, typename T2> n bool maxi(T1 &a, const T2 &b) { n   if (a < b) { n     a = b; n     return true; n   } n   return false; n } n const long long N = 52; n long long rcnt[N]; n long long ccnt[N]; n void run() { n   long long n, m; n   cin >> n >> m; n   set<long long> masks; n   for (long long i = 0; i < n; i++) { n     long long kek = 0; n     for (long long j = 0; j < m; j++) { n       char c; n       cin >> c; n       if (c ==  # ) kek |= (1LL << j); n     } n     masks.insert(kek); n   } n   for (auto it1 = masks.begin(); it1 != masks.end(); it1++) { n     auto it2 = it1; n     it2++; n     for (; it2 != masks.end(); it2++) { n       if ((*it1) & (*(it2))) { n         cout <<  No ; n         return; n       } n     } n   } n   cout <<  Yes ; n } n


#include <bits/stdc++.h> n using namespace std; n long long c[2000010]; n long long sum[2000010]; n int main() { n   int n, x, y; n   while (cin >> n >> x >> y) { n     memset(c, 0, sizeof c); n     memset(sum, 0, sizeof sum); n     for (int i = 1; i <= n; i++) { n       int w; n       scanf( %d , &w); n       c[w]++; n       sum[w] += w; n     } n     for (int i = 0; i <= 2000000; i++) { n       c[i] += c[i - 1]; n       sum[i] += sum[i - 1]; n     } n     long long ans = 1LL * n * x; n     for (int i = 2; i <= 1000000; i++) { n       long long tmp = 0; n       for (int j = i; j <= 1000000 + i && tmp < ans; j += i) { n         int k = max(j - i, j - ((x - 1) / y + 1)); n         long long h = y * ((c[j] - c[k]) * j - (sum[j] - sum[k])); n         h += (c[k] - c[j - i]) * x; n         tmp += h; n       } n       ans = min(ans, tmp); n     } n     cout << ans << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long int T = 1, inf = 10000000000; n bool check(long long int n) { n   long long int flag = 1; n   while (n > 0) { n     if (n % 10 != 4 && n % 10 != 7) { n       flag = 0; n       break; n     } n     n /= 10; n   } n   return flag; n } n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   while (T--) { n     long long int n, sz = 3, ans = 0, blag = 0, u, mn, ev = 0, mx, mini, i, v, n                      q, p, j, k, w, h, l, m, x, y, od = 0, total = 0, cc = 0, n                      pp = 0, flag = 0, klag = 0, sum = 0; n     string s, s3, s1 =  YES , s2 =  NO ; n     bool ok = 0; n     cin >> n; n     for (i = 4; i <= n; i += 1) { n       if (n % i == 0) { n         if (check(i)) { n           cout << s1; n           return 0; n         } n       } n     } n     cout << s2; n     return 0; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int M = 2e5 + 10; n const int inf = 0x3f3f3f3f; n const long long mod = 1e9 + 7; n const int bit = 32; n const double pi = acos(-1.0); n int a[M]; n int dfs(int k, int l, int r) { n   if (l > r) return 0; n   if (k < 0) return 1; n   int mid = r; n   int t = 1 << k; n   for (int i = l; i <= r; i++) { n     if (t & a[i]) { n       mid = i - 1; n       break; n     } n   } n   int x = dfs(k - 1, l, mid), y = dfs(k - 1, mid + 1, r); n   return max(x + min(r - mid, 1), y + min(mid - l + 1, 1)); n } n int main() { n   int n; n   scanf( %d , &n); n   for (int i = 1; i <= n; i++) scanf( %d , &a[i]); n   sort(a + 1, a + 1 + n); n   printf( %d n , n - dfs(30, 1, n)); n   return 0; n } n


#include <bits/stdc++.h> n template <class T> n inline void read(T &x) { n   x = 0; n   int ne = 0; n   char c; n   while (!isdigit(c = getchar())) ne = c ==  - ; n   x = c - 48; n   while (isdigit(c = getchar())) x = (x << 3) + (x << 1) + c - 48; n   x = ne ? -x : x; n   return; n } n const int maxn = 1005; n const int inf = 0x7fffffff; n int g[maxn][maxn]; n int okx[maxn][maxn], oky[maxn][maxn]; n int sum[maxn][maxn]; n bool flag[maxn][maxn]; n char s[maxn]; n int n, m; n struct Star { n   int x, y, size; n   Star(int a, int b, int c) { x = a, y = b, size = c; } n   Star() { ; } n } st[1000005]; n int tot = 0; n int len; n inline bool solve(int i, int j) { n   int p = 1, k = 0; n   len = 0; n   while (p != 0) { n     int x = p + k; n     if (j - x >= 1 && j + x <= m && i + x <= n && i - x >= 1 && n         (sum[i][j] - sum[i][j - 1] - sum[i - x - 1][j] + n              sum[i - x - 1][j - 1] >= n          (x + 1)) && n         (sum[i + x][j] - sum[i + x][j - 1] - sum[i - 1][j] + n              sum[i - 1][j - 1] >= n          (x + 1))) { n       if ((sum[i][j] - sum[i][j - x - 1] - sum[i - 1][j] + n                sum[i - 1][j - x - 1] >= n            (x + 1)) && n           (sum[i][j + x] - sum[i - 1][j + x] - sum[i][j - 1] + n                sum[i - 1][j - 1] >= n            (x + 1))) { n         k += p, p = p << 1; n         x = p + k; n       } else n         p = p / 2; n     } else n       p = p / 2; n     x = p + k; n     while (j - x < 1 || j + x > m || i + x > n || i - x < 1 && p) { n       p = p >> 1; n       x = p + k; n     } n   } n   len = k; n   if (!len) return 0; n   okx[i][j - len]++, okx[i][j + len + 1]--; n   oky[j][i - len]++, oky[j][i + len + 1]--; n   return 1; n } n int main() { n   read(n), read(m); n   for (register int i = 1; i <= n; i++) { n     scanf( %s , s + 1); n     for (register int j = 1; j <= m; j++) { n       if (s[j] ==  * ) n         g[i][j] = 1; n       else n         g[i][j] = 0; n       sum[i][j] = sum[i - 1][j] + sum[i][j - 1] - sum[i - 1][j - 1] + g[i][j]; n     } n   } n   for (register int i = 1; i <= n; i++) { n     for (register int j = 1; j <= m; j++) { n       if (g[i][j]) { n         if (solve(i, j)) { n           st[++tot] = Star(i, j, len); n         } n       } n     } n   } n   for (register int i = 1; i <= n; i++) { n     int tmp = 0; n     for (register int j = 1; j <= m; j++) { n       tmp += okx[i][j]; n       if (tmp) flag[i][j] = 1; n     } n   } n   for (register int i = 1; i <= m; i++) { n     int tmp = 0; n     for (register int j = 1; j <= n; j++) { n       tmp += oky[i][j]; n       if (tmp) flag[j][i] = 1; n       if (g[j][i] && !flag[j][i]) { n         puts( -1 ); n         return 0; n       } n     } n   } n   printf( %d n , tot); n   for (register int i = 1; i <= tot; i++) { n     printf( %d %d %d n , st[i].x, st[i].y, st[i].size); n   } n   return 0; n } n


#include <bits/stdc++.h> n const int oo = 0x7fffffff; n const long long OO = 0x7fffffffffffffff; n using namespace std; n const int NX = 50; n typedef bitset<NX * NX * NX * NX> dp_t; n int n; n array<bitset<NX>, NX> board1, board2, done; n dp_t dp1, dp2; n int ask(int x1, int y1, int x2, int y2) { n   cout <<  ?   << (1 + x1) <<     << (1 + y1) <<     << (1 + x2) <<     n        << (1 + y2) << endl; n   int r; n   cin >> r; n   if (r == -1) exit(0); n   return r; n } n void ask_n_set(int x1, int y1, int x2, int y2, bool forward = true) { n   int r = ask(x1, y1, x2, y2) ^ 1; n   if (forward) { n     board1[x2][y2] = r ^ board1[x1][y1]; n     board2[x2][y2] = r ^ board2[x1][y1]; n     done[x2][y2] = 1; n   } else { n     board1[x1][y1] = r ^ board1[x2][y2]; n     board2[x1][y1] = r ^ board2[x2][y2]; n     done[x1][y1] = 1; n   } n } n void print(const array<bitset<NX>, NX>& B) { n   cout <<  ! n ; n   for (int i = 0; i < n; ++i) { n     for (int j = 0; j < n; ++j) cout << B[i][j]; n     cout <<   n ; n   } n   cout << endl; n } n void solve(const array<bitset<NX>, NX>& B, dp_t& dp) { n   for (int x1 = 0; x1 < n; ++x1) n     for (int y1 = 0; y1 < n; ++y1) n       for (int x2 = 0; x2 < n; ++x2) n         for (int y2 = 0; y2 < n; ++y2) n           if ((abs(x1 - x2) + abs(y1 - y2)) <= 2) n             dp[(y2 + NX * (x2 + NX * (y1 + NX * x1)))] = B[x1][y1] == B[x2][y2]; n   for (int x1 = 0; x1 < n; ++x1) n     for (int y1 = 0; y1 < n; ++y1) n       for (int x2 = x1 - 1; x2 >= 0; --x2) n         for (int y2 = y1 - 1; y2 >= 0; --y2) n           dp[(y2 + NX * (x2 + NX * (y1 + NX * x1)))] = 0; n   for (int len = 3; len < (2 * n); ++len) n     for (int x1 = 0; x1 < n; ++x1) n       for (int y1 = 0; y1 < n; ++y1) n         for (int x2 = x1; x2 < n; ++x2) n           if ((abs(x1 - x2) + abs(y1 - y1)) <= len) { n             int d = len - (x2 - x1); n             int y2 = y1 + d; n             if (y2 >= n) continue; n             if (x1 == x2) { n               dp[(y2 + NX * (x2 + NX * (y1 + NX * x1)))] = n                   B[x1][y1] == B[x2][y2] and n                   dp[(y2 - 1 + NX * (x2 + NX * (y1 + 1 + NX * x1)))]; n             } else if (y1 == y2) { n               dp[(y2 + NX * (x2 + NX * (y1 + NX * x1)))] = n                   B[x1][y1] == B[x2][y2] and n                   dp[(y2 + NX * (x2 - 1 + NX * (y1 + NX * x1 + 1)))]; n             } else { n               dp[(y2 + NX * (x2 + NX * (y1 + NX * x1)))] = n                   B[x1][y1] == B[x2][y2] and n                   (dp[(y2 + NX * (x2 - 1 + NX * (y1 + NX * x1 + 1)))] or n                    dp[(y2 - 1 + NX * (x2 + NX * (y1 + NX * x1 + 1)))] or n                    dp[(y2 + NX * (x2 - 1 + NX * (y1 + 1 + NX * x1)))] or n                    dp[(y2 - 1 + NX * (x2 + NX * (y1 + 1 + NX * x1)))]); n             } n           } n } n int main() { n   cin >> n; n   for (int i = 0; i < n; ++i) n     for (int j = 0; j < n; ++j) done[i][j] = 0; n   board2[0][0] = board1[0][0] = 1; n   board2[n - 1][n - 1] = board1[n - 1][n - 1] = 0; n   done[0][0] = done[n - 1][n - 1] = 1; n   board1[0][1] = 1; n   board2[0][1] = 0; n   done[0][1] = 1; n   ask_n_set(0, 1, 2, 1); n   ask_n_set(1, 0, 2, 1, false); n   for (int i = 0; i < n; ++i) n     for (int j = 0; j < n; ++j) n       if (not done[i][j]) { n         int x, y; n         if (i - 2 >= 0) { n           x = i - 2; n           y = j; n         } else if (j - 2 >= 0) { n           x = i; n           y = j - 2; n         } else { n           x = i - 1; n           y = j - 1; n         } n         ask_n_set(x, y, i, j); n       } n   solve(board1, dp1); n   solve(board2, dp2); n   for (int x1 = 0; x1 < n; ++x1) n     for (int x2 = x1; x2 < n; ++x2) n       for (int y1 = 0; y1 < n; ++y1) n         for (int y2 = y1; y2 < n; ++y2) n           if (2 <= y2 - y1 + x2 - x1) { n             int r1 = dp1[(y2 + NX * (x2 + NX * (y1 + NX * x1)))], n                 r2 = dp2[(y2 + NX * (x2 + NX * (y1 + NX * x1)))]; n             if (r1 == r2) continue; n             int r = ask(x1, y1, x2, y2); n             if (r1 == r) n               print(board1); n             else n               print(board2); n             return 0; n           } n   print(board1); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <class T> n inline void read(T& x) { n   char c; n   bool f = 0; n   while ((c = getchar()) <  0  ||  9  < c) f |= (c ==  - ); n   for (x = (c ^ 48);  0  <= (c = getchar()) && c <=  9 ; n        x = (x << 1) + (x << 3) + (c ^ 48)) n     ; n   if (f) x = -x; n } n template <class T> n inline T read(const T sample) { n   T x = 0; n   char c; n   bool f = 0; n   while ((c = getchar()) <  0  ||  9  < c) f |= (c ==  - ); n   for (x = (c ^ 48);  0  <= (c = getchar()) && c <=  9 ; n        x = (x << 1) + (x << 3) + (c ^ 48)) n     ; n   return f ? -x : x; n } n template <class T> n void fwrit(const T x) { n   if (x < 0) return (void)(putchar( - ), fwrit(-x)); n   if (x > 9) fwrit(x / 10); n   putchar(x % 10 ^ 48); n } n template <class T> n inline T Max(const T x, const T y) { n   return x > y ? x : y; n } n template <class T> n inline T Min(const T x, const T y) { n   return x < y ? x : y; n } n template <class T> n inline T fab(const T x) { n   return x > 0 ? x : -x; n } n inline int gcd(const int a, const int b) { return b ? gcd(b, a % b) : a; } n inline void getInv(int inv[], const int lim, const int MOD) { n   inv[0] = inv[1] = 1; n   for (int i = 2; i <= lim; ++i) n     inv[i] = 1ll * inv[MOD % i] * (MOD - MOD / i) % MOD; n } n inline long long mulMod(const long long a, const long long b, n                         const long long mod) { n   return ((a * b - (long long)((long double)a / mod * b + 1e-8) * mod) % mod + n           mod) % n          mod; n } n const int MAXN = 100000; n const int MAXM = 100000; n const int INF = (1 << 30) - 1; n struct edge { n   int to, nxt, c; n } e[MAXM * 2 + 5]; n int tail[MAXN + 5], ecnt; n inline void add_edge(const int u, const int v, const int c) { n   e[++ecnt] = edge{v, tail[u], c}; n   tail[u] = ecnt; n   e[++ecnt] = edge{u, tail[v], c}; n   tail[v] = ecnt; n } n int n, m; n inline void Init() { n   n = read(1), m = read(1); n   int u, v; n   char ch[5]; n   for (signed i = (1), i_end_ = (m); i <= i_end_; ++i) { n     scanf( %d %d %s , &u, &v, ch); n     add_edge(u, v, ch[0] ==  R ); n   } n } n int rever[MAXN + 5]; n void Clear(const int u) { n   rever[u] = -1; n   for (int i = tail[u]; i; i = e[i].nxt) n     if (rever[e[i].to] != -1) Clear(e[i].to); n } n int Judge(const int u, const int rev, const int goal) { n   int sum = rev, ret; n   rever[u] = rev; n   for (int i = tail[u], v, tore; i; i = e[i].nxt) { n     v = e[i].to; n     tore = goal ^ e[i].c ^ rev; n     if (rever[v] != -1 && rever[v] != tore) return -1; n     if (rever[v] == -1) { n       ret = Judge(v, tore, goal); n       if (ret == -1) return -1; n       sum += ret; n     } n   } n   return sum; n } n int ans = INF, memo[MAXN + 5]; n signed main() { n   Init(); n   int ans1, ans2, f1, f2; n   bool flg; n   for (signed t = (0), t_end_ = (1); t <= t_end_; ++t) { n     flg = false; n     memset(rever, -1, sizeof rever); n     for (signed i = (1), i_end_ = (n); i <= i_end_; ++i) n       if (rever[i] == -1) { n         Clear(i), ans1 = Judge(i, 0, t); n         Clear(i), ans2 = Judge(i, 1, t); n         if (ans1 == -1 && ans2 == -1) { n           flg = true; n           break; n         } n         if (ans2 == -1 || ans1 < ans2) { n           Clear(i); n           Judge(i, 0, t); n         } n       } n     if (!flg) { n       int tot = 0; n       for (signed i = (1), i_end_ = (n); i <= i_end_; ++i) n         if (rever[i] == 1) ++tot; n       if (tot > (n / 2)) { n         tot = n - tot; n         for (signed i = (1), i_end_ = (n); i <= i_end_; ++i) n           rever[i] = !rever[i]; n       } n       if (tot < ans) { n         ans = tot; n         for (signed i = (1), i_end_ = (n); i <= i_end_; ++i) memo[i] = rever[i]; n       } n     } n   } n   if (ans == INF) n     puts( -1 ); n   else { n     fwrit(ans), putchar(  n ); n     for (signed i = (1), i_end_ = (n); i <= i_end_; ++i) n       if (memo[i]) fwrit(i), putchar(   ); n     putchar(  n ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long mod = 1000000007; n clock_t time_p = clock(); n void rtime() { n   time_p = clock() - time_p; n   cerr <<  Time Taken :   << fixed << (float)(time_p) / CLOCKS_PER_SEC <<   n ; n } n bool isPrime(long long n) { n   for (long long i = 2; i * i <= n; ++i) { n     if (n % i == 0) { n       return false; n     } n   } n   return true; n } n long long factorial(long long n) { n   return (n == 1 || n == 0) ? 1 : n * factorial(n - 1); n } n long long power(long long x, long long y) { n   long long res = 1; n   x = x; n   while (y > 0) { n     if (y & 1) res = (res * x) % mod; n     y = y >> 1; n     x = (x * x) % mod; n   } n   return res % mod; n } n long long ncr(long long n, long long r) { n   long long res = 1; n   if (r > n) return 0; n   if (r > n - r) r = n - r; n   for (long long i = 0; i < r; i++) { n     res *= (n - i); n     res /= (i + 1); n   } n   return res; n } n long long gcd(long long a, long long b) { n   if (a == 0) return b; n   return gcd(b % a, a); n } n long long lcm(long long a, long long b) { return (a / gcd(a, b) * b); } n long long max(long long a, long long b) { n   long long ans = a > b ? a : b; n   return ans; n } n long long min(long long a, long long b) { n   long long ans = a < b ? a : b; n   return ans; n } n bool cmp(long long x, long long y) { return x > y; } n signed main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   ; n   long long t; n   cin >> t; n   while (t--) { n     long long n; n     cin >> n; n     if (n == 2) n       cout <<  2 n ; n     else if (n % 2 == 0) n       cout <<  0 n ; n     else n       cout <<  1 n ; n   } n   rtime(); n } n


#include <bits/stdc++.h> n using namespace std; n int read(int &x) { return scanf( %d , &x); } n int read(int &x, int &y) { return scanf( %d%d , &x, &y); } n int read(int &x, int &y, int &z) { return scanf( %d%d%d , &x, &y, &z); } n int read(long long &x) { return scanf( %lld , &x); } n int read(long long &x, long long &y) { return scanf( %lld%lld , &x, &y); } n int read(double &x) { return scanf( %lf , &x); } n char buff[2000010]; n int read(string &s) { n   int r = scanf( %s , buff); n   s = buff; n   return r; n } n int main() { n   vector<int> PHI(1000002); n   for (int i = 1; i < PHI.size(); ++i) { n     PHI[i] = i; n   } n   for (int i = 2; i < PHI.size(); ++i) { n     if (PHI[i] == i) { n       for (int j = i; j < PHI.size(); j += i) { n         PHI[j] = PHI[j] / i * (i - 1); n       } n     } n   } n   int N, K; n   read(N, K); n   if (K == 1) { n     cout << 3 << endl; n     return 0; n   } n   sort(PHI.begin() + 3, PHI.begin() + N + 1); n   cout << accumulate(PHI.begin() + 3, PHI.begin() + 3 + K, 2LL) << endl; n } n


#include <bits/stdc++.h> n #pragma GCC optimize(2) n #pragma GCC optimize(3) n #pragma GCC optimize( Ofast ) n #pragma GCC optimize( inline ) n #pragma GCC optimize( -fgcse ) n #pragma GCC optimize( -fgcse-lm ) n #pragma GCC optimize( -fipa-sra ) n #pragma GCC optimize( -ftree-pre ) n #pragma GCC optimize( -ftree-vrp ) n #pragma GCC optimize( -fpeephole2 ) n #pragma GCC optimize( -ffast-math ) n #pragma GCC optimize( -fsched-spec ) n #pragma GCC optimize( unroll-loops ) n #pragma GCC optimize( -falign-jumps ) n #pragma GCC optimize( -falign-loops ) n #pragma GCC optimize( -falign-labels ) n #pragma GCC optimize( -fdevirtualize ) n #pragma GCC optimize( -fcaller-saves ) n #pragma GCC optimize( -fcrossjumping ) n #pragma GCC optimize( -fthread-jumps ) n #pragma GCC optimize( -funroll-loops ) n #pragma GCC optimize( -fwhole-program ) n #pragma GCC optimize( -freorder-blocks ) n #pragma GCC optimize( -fschedule-insns ) n #pragma GCC optimize( inline-functions ) n #pragma GCC optimize( -ftree-tail-merge ) n #pragma GCC optimize( -fschedule-insns2 ) n #pragma GCC optimize( -fstrict-aliasing ) n #pragma GCC optimize( -fstrict-overflow ) n #pragma GCC optimize( -falign-functions ) n #pragma GCC optimize( -fcse-skip-blocks ) n #pragma GCC optimize( -fcse-follow-jumps ) n #pragma GCC optimize( -fsched-interblock ) n #pragma GCC optimize( -fpartial-inlining ) n #pragma GCC optimize( no-stack-protector ) n #pragma GCC optimize( -freorder-functions ) n #pragma GCC optimize( -findirect-inlining ) n #pragma GCC optimize( -frerun-cse-after-loop ) n #pragma GCC optimize( inline-small-functions ) n #pragma GCC optimize( -finline-small-functions ) n #pragma GCC optimize( -ftree-switch-conversion ) n #pragma GCC optimize( -foptimize-sibling-calls ) n #pragma GCC optimize( -fexpensive-optimizations ) n #pragma GCC optimize( -funsafe-loop-optimizations ) n #pragma GCC optimize( inline-functions-called-once ) n #pragma GCC optimize( -fdelete-null-pointer-checks ) n using namespace std; n struct node { n   char c; n   long long l; n   bool operator==(const node &b) const { return (c == b.c) && (l == b.l); } n   bool operator<=(const node &b) const { return (c == b.c) && (l <= b.l); } n } a[200010], b[200010]; n long long n, m, pi[200010]; n long long ans; n void mk(node *a, long long &n) { n   long long m = 0; n   for (long long i = 0; i < n; i++) { n     if (m == 0 || a[m - 1].c != a[i].c) n       a[m++] = a[i]; n     else n       a[m - 1].l += a[i].l; n   } n   n = m; n } n signed main() { n   scanf( %lld%lld , &n, &m); n   for (long long i = 0; i < n; i++) scanf( %lld-%c , &a[i].l, &a[i].c); n   for (long long i = 0; i < m; i++) scanf( %lld-%c , &b[i].l, &b[i].c); n   mk(a, n), mk(b, m); n   if (m == 1) { n     for (long long i = 0; i < n; i++) n       if (b[0] <= a[i]) ans += a[i].l - b[0].l + 1; n   } else if (m == 2) { n     for (long long i = 0; i < n - 1; i++) n       if (b[0] <= a[i] && b[1] <= a[i + 1]) ans++; n   } else { n     pi[1] = 0; n     for (long long i = 2; i < m - 1; i++) { n       long long j = pi[i - 1]; n       while (j > 0 && !(b[j + 1] == b[i])) j = pi[j]; n       if (b[j + 1] == b[i]) j++; n       pi[i] = j; n     } n     for (long long i = 1, j = 0; i < n - 1; i++) { n       while (j > 0 && (!(b[j + 1] == a[i]))) j = pi[j]; n       if (b[j + 1] == a[i]) j++; n       if (j == m - 2) { n         if (b[0] <= a[i - j] && b[j + 1] <= a[i + 1]) ans++; n         j = pi[j]; n       } n     } n   } n   printf( %lld , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   cerr.tie(0); n   string s; n   long long p = 0, q = 0; n   cin >> s; n   for (int i = s.size() - 1; i > -1; i--) { n     if (s[i] ==  a ) { n       p += q; n       p = p % 1000000007; n       q = (2 * q) % 1000000007; n     } else { n       q = (q + 1) % 1000000007; n     } n   } n   cout << p; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n char w[1555][1555]; n int nm[1555][1555]; n int n, m; n struct cell { n   int x, y; n }; n void mark(int x, int y, int c) { n   queue<cell> q; n   cell T; n   T.x = x; n   T.y = y; n   q.push(T); n   while (!q.empty()) { n     T = q.front(); n     q.pop(); n     if (T.x < 0 || T.x >= n || T.y < 0 || T.y >= m || w[T.x][T.y] ==  #  || n         nm[T.x][T.y] != 0) n       continue; n     nm[T.x][T.y] = c; n     cell W; n     W.x = T.x; n     W.y = T.y - 1; n     q.push(W); n     W.x = T.x; n     W.y = T.y + 1; n     q.push(W); n     W.x = T.x + 1; n     W.y = T.y; n     q.push(W); n     W.x = T.x - 1; n     W.y = T.y; n     q.push(W); n   } n } n int kn; n int Sn; n const int M = 1555 * 20; n int fir[1555 * 1555], tov[M], nxt[M], dx[M], dy[M]; n int ke; n void add(int x, int y, int Dx, int Dy) { n   nxt[++ke] = fir[x]; n   tov[ke] = y; n   fir[x] = ke; n   dx[ke] = Dx; n   dy[ke] = Dy; n   if (ke & 1) add(y, x, -Dx, -Dy); n } n bool u[1555 * 1555]; n int vx[1555 * 1555], vy[1555 * 1555]; n bool cool = false; n void dfs(int c, int x, int y) { n   if (u[c]) { n     if (vx[c] != x || vy[c] != y) { n       cool = true; n     } n     return; n   } n   u[c] = true; n   vx[c] = x; n   vy[c] = y; n   for (int t = fir[c]; t; t = nxt[t]) { n     dfs(tov[t], x + dx[t], y + dy[t]); n   } n } n int main() { n   scanf( %d%d , &n, &m); n   cin.ignore(); n   for (int i = 0; i < n; ++i) { n     gets(w[i]); n   } n   for (int i = 0; i < n; ++i) n     for (int j = 0; j < m; ++j) { n       if (w[i][j] !=  #  && nm[i][j] == 0) { n         mark(i, j, ++kn); n       } n       if (w[i][j] ==  S ) { n         Sn = nm[i][j]; n       } n     } n   for (int i = 0; i < n; ++i) { n     if (w[i][0] !=  #  && w[i][m - 1] !=  # ) { n       add(nm[i][0], nm[i][m - 1], -1, 0); n     } n   } n   for (int i = 0; i < m; ++i) { n     if (w[0][i] !=  #  && w[n - 1][i] !=  # ) { n       add(nm[0][i], nm[n - 1][i], 0, -1); n     } n   } n   dfs(Sn, 0, 0); n   if (cool) n     puts( Yes ); n   else n     puts( No ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int sz = 1e5 + 50; n map<int, int> prime, mark; n void init() { n   prime[2] = 1; n   int limit = sqrt(sz) + 1; n   for (int i = 3; i <= sz; i += 2) { n     if (!mark[i]) { n       prime[i] = 1; n       if (i <= limit) { n         for (int j = i * i; j <= sz; j += i * 2) mark[j] = 1; n       } n     } n   } n } n int main() { n   init(); n   int n, a; n   scanf( %d , &n); n   int o = 0, t = 0; n   while (n--) { n     scanf( %d , &a); n     a == 1 ? o++ : t++; n   } n   int sum = 0; n   vector<int> v; n   while (t || o) { n     if (o > 0 && prime[sum + 1]) { n       o--; n       sum += 1; n       v.push_back(1); n     } else if (t > 0 && prime[sum + 2]) { n       t--; n       sum += 2; n       v.push_back(2); n     } else { n       if (t) n         sum += 2, t--, v.push_back(2); n       else n         sum++, o--, v.push_back(1); n     } n   } n   for (auto i : v) printf( %d  , i); n   printf(  n ); n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 100 + 7; n const int M = 59; n const int mid = M / 2; n const int mod = 1e9 + 9; n const int inf = 1e6 + 3; n const long long linf = 1ll * inf * inf; n const double pi = acos(-1); n const double eps = 1e-7; n const double ep = 1e-5; n const int maxn = 1e5 + 7; n const double PI = acos(-1); n int C, W, H; n int dp[2][107]; n struct matrix { n   long long a[N][N]; n   int n, m; n   matrix(int _n, int _m) { n     n = _n; n     m = _m; n     memset(a, 0, sizeof(a)); n   } n   matrix(int _n, int _m, bool itype) { n     n = _n; n     m = _m; n     if (itype) { n       for (int i = (1); i < (n + 1); i++) a[i][i] = 1; n     } n   } n   matrix operator*(matrix u) { n     matrix c(n, u.m); n     for (int i = 1; i <= c.n; i++) { n       for (int j = 1; j <= c.m; j++) { n         long long tmp = 0; n         for (int t = 1; t <= m; t++) n           tmp = (tmp + (long long)a[i][t] * u.a[t][j]) % inf; n         c.a[i][j] = tmp; n       } n     } n     return c; n   } n   void print() { n     for (int i = 1; i <= n; ++i) { n       for (int j = 1; j <= m; ++j) printf( %lld  , a[i][j]); n       printf(  n ); n     } n   } n }; n void solve() { n   cin >> C >> W >> H; n   matrix b(W + 1, W + 1); n   for (int i = (2); i < (W + 2); i++) { n     b.a[i - 1][i] = H; n   } n   for (int i = (1); i < (W + 2); i++) b.a[i][1] = 1; n   matrix I(W + 1, W + 1, true); n   while (C > 0) { n     if (C & 1) { n       I = I * b; n     } n     b = b * b; n     C >>= 1; n   } n   matrix res(1, W + 1); n   res.a[1][1] = 1; n   res = res * I; n   int ans = 0; n   for (int i = (1); i < (res.m + 1); i++) ans = (ans + res.a[1][i]) % inf; n   cout << ans; n } n int main() { n   int T = 1; n   for (int i = (1); i < (T + 1); i++) { n     solve(); n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e5 + 5; n int arr[1001]; n int main() { n   int n, m, c; n   cin >> n >> m >> c; n   fill(arr, arr + n + 1, -1); n   int mid = (c + 1) / 2; n   bool done = 0; n   for (int j = 0; !done && j < m; j++) { n     int x; n     cin >> x; n     if (x > mid) { n       for (int i = n; i >= 1; i--) { n         if (arr[i] == -1 || arr[i] < x) { n           arr[i] = x; n           cout << i << endl; n           break; n         } n       } n     } else { n       for (int i = 1; i <= n; i++) { n         if (arr[i] == -1 || arr[i] > x) { n           arr[i] = x; n           cout << i << endl; n           break; n         } n       } n     } n     done = 1; n     for (int i = 1; done && i <= n; i++) { n       if (arr[i] == -1) { n         done = 0; n         break; n       } n       if (i < n && arr[i + 1] == -1) { n         done = 0; n         break; n       } n       if (i < n && arr[i] > arr[i + 1]) { n         done = 0; n         break; n       } n     } n   } n   exit(0); n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   cin >> n; n   unsigned long long a[n], M; n   for (int i = 0; i < n; ++i) { n     cin >> a[i]; n   } n   stack<unsigned long long> s; n   M = *max_element(a, a + n); n   for (int i = 0; i < n; ++i) { n     a[i] = (M - a[i]) % 2; n     if (s.empty() || s.top() != a[i]) n       s.push(a[i]); n     else n       s.pop(); n   } n   bool ans2 = false; n   for (int i = 0; i < n - 1 && !ans2; ++i) { n     ans2 = ans2 || (a[i] == a[i + 1]); n   } n   bool all1 = true, all0 = true; n   while (!s.empty()) { n     all0 = all0 && (s.top() == 0); n     all1 = all1 && (s.top() == 1); n     s.pop(); n   } n   bool ans = all0 || all1; n   cout << (ans ?  YES  :  NO ) << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 500 + 5, inf = 0x3f3f3f3f; n namespace network_flow { n struct edge { n   int to, nxt, val, cost; n } e[N << 5]; n int head[N], cnt = 1, maxflow, vis[N], dis[N], nxt[N]; n int s, t; n void add_edge(int x, int y, int z) { n   e[++cnt].to = y; n   e[cnt].val = z; n   e[cnt].nxt = head[x]; n   head[x] = cnt; n } n void add(int x, int y, int z) { n   add_edge(x, y, z); n   add_edge(y, x, 0); n } n queue<int> q; n bool spfa() { n   memset(dis, 0x3f, sizeof(dis)); n   dis[s] = 0; n   q.push(s); n   while (!q.empty()) { n     int u = q.front(); n     q.pop(); n     vis[u] = 0; n     for (int i = head[u]; i; i = e[i].nxt) n       if (e[i].val && dis[e[i].to] > dis[u] + 1) { n         dis[e[i].to] = dis[u] + 1; n         if (!vis[e[i].to]) vis[e[i].to] = 1, q.push(e[i].to); n       } n   } n   return (dis[t]) ^ inf; n } n bool flag[N]; n int n; n int dfs(int u, int limit) { n   if (u == t) return limit; n   int flow = 0; n   for (int i = head[u]; i && limit; i = e[i].nxt) n     if (e[i].val && dis[e[i].to] == dis[u] + 1) { n       int k = dfs(e[i].to, min(limit, e[i].val)); n       if (k > 0) { n         e[i].val -= k; n         e[i ^ 1].val += k; n         limit -= k; n         flow += k; n       } n     } n   if (!flow) dis[u] = inf; n   return flow; n } n void Dinic() { n   for (int i = 2; i <= cnt; ++i) e[i].val += e[i ^ 1].val, e[i ^ 1].val = 0; n   maxflow = 0; n   while (spfa()) maxflow += dfs(s, inf); n } n };  // namespace network_flow n int ans[N], ccnt, Ans; n namespace mincut_tree { n struct edge { n   int nxt, to, val, vis; n } e[N << 4]; n int head[N], cnt = 1; n void add(int x, int y, int z) { n   e[++cnt].to = y; n   e[cnt].val = z; n   e[cnt].nxt = head[x]; n   head[x] = cnt; n   e[++cnt].to = x; n   e[cnt].val = z; n   e[cnt].nxt = head[y]; n   head[y] = cnt; n } n int x, y, mn, pos; n void dfs(int u, int fath) { n   for (int i = head[u]; i; i = e[i].nxt) n     if (e[i].to != fath && e[i].vis == 0) { n       if (mn > e[i].val) mn = e[i].val, pos = i, x = u, y = e[i].to; n       dfs(e[i].to, u); n     } n } n void solve(int u) { n   mn = inf; n   dfs(u, 0); n   if (mn == inf) return ans[++ccnt] = u, void(0); n   Ans += mn; n   e[pos].vis = e[pos ^ 1].vis = 1; n   int a = x, b = y; n   solve(a); n   solve(b); n } n }  // namespace mincut_tree n int node[N], tmp1[N], tmp2[N]; n void build(int l, int r) { n   if (l == r) return; n   network_flow::s = node[l]; n   network_flow::t = node[l + 1]; n   network_flow::Dinic(); n   int cnt1 = 0, cnt2 = 0; n   mincut_tree::add(network_flow::s, network_flow::t, network_flow::maxflow); n   for (int i = l; i <= r; ++i) n     if (network_flow::dis[node[i]] ^ inf) n       tmp1[++cnt1] = node[i]; n     else n       tmp2[++cnt2] = node[i]; n   for (int i = 1; i <= cnt1; ++i) node[l + i - 1] = tmp1[i]; n   for (int i = 1; i <= cnt2; ++i) node[l + cnt1 + i - 1] = tmp2[i]; n   build(l, l + cnt1 - 1); n   build(l + cnt1, r); n } n int main() { n   int u, v, n, m, val; n   cin >> n >> m; n   for (int i = 1; i <= m; ++i) { n     cin >> u >> v >> val; n     network_flow::add(u, v, val); n     network_flow::add(v, u, val); n   } n   for (int i = 1; i <= n; ++i) node[i] = i; n   build(1, n); n   mincut_tree::solve(1); n   cout << Ans << endl; n   for (int i = 1; i <= ccnt; ++i) cout << ans[i] <<    ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, i, j, a[100005], l, r; n string s; n int main(int argc, char *argv[]) { n   ios_base::sync_with_stdio(0), cin.tie(NULL); n   ; n   cin >> n; n   for (__typeof(i) i = 0; i < n; ++i) { n     cin >> a[i]; n   } n   cin >> s; n   l = -1000000000; n   r = 1000000000; n   int cur = 0; n   int m = 0; n   for (__typeof(i) i = 4; i < n; ++i) { n     if (s[i] -  0  != cur) { n       if (cur) { n         m = INT_MAX; n         for (__typeof(j) j = -4; j <= 0; ++j) { n           m = min(m, a[i + j]); n         } n         r = min(r, m - 1); n         cur = 0; n       } else { n         m = INT_MIN; n         for (__typeof(j) j = -4; j <= 0; ++j) { n           m = max(m, a[i + j]); n         } n         l = max(l, m + 1); n         cur = 1; n       } n     } n   } n   cout << l <<     << r << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int i, j, k, l, x, y, z, m, n, ans; n string s, a; n int ara[10]; n int main() { n   cin >> a >> s; n   for (int i = 0; i < s.size(); i++) { n     int x = s[i] -  0 ; n     ara[x]++; n   } n   for (int i = 0; i < a.size(); i++) { n     int x = a[i] -  0 ; n     for (int j = 9; j > x; j--) { n       if (ara[j]) { n         ara[j]--; n         a[i] =  0  + j; n         break; n       } n     } n   } n   cout << a << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long int n, k; n bool prime[10000001]; n vector<long int> v; n void sieve(long int n) { n   prime[0] = false; n   prime[1] = false; n   long int m = (long int)sqrt(n); n   for (long int i = 2; i <= m; i++) n     if (prime[i]) n       for (long int k = i * i; k <= n; k += i) prime[k] = false; n } n int main() { n   long int i, j, a, b, c; n   scanf( %ld %ld , &n, &k); n   memset(prime, true, sizeof(prime)); n   a = 1000 * 1000; n   sieve(a); n   for (i = 1; i <= a; i++) { n     if (prime[i] == true) { n       v.push_back(i); n     } n   } n   if (k == 0 && n == 1) { n     printf( 1 ); n   } else if (n < 2) { n     printf( -1 ); n   } else { n     if (n % 2 == 1) { n       a = (n - 1) / 2; n     } else { n       a = n / 2; n     } n     if (a > k) { n       printf( -1 ); n     } else if (a == k) { n       i = 1; n       vector<long int>::iterator it; n       for (it = v.begin(); it != v.end() && (i <= a); ++it) { n         if (*it == 2) { n           continue; n         } else { n           printf( %ld %ld  , *it, *it + 1); n           i++; n         } n       } n       if (n % 2 == 1) { n         printf( %ld , *it); n       } n     } else { n       b = k - (a - 1); n       printf( %ld %ld  , b, 2 * b); n       i = 1; n       vector<long int>::iterator it; n       for (it = v.begin(); it != v.end() && (i < a); ++it) { n         if (*it == 2) { n           continue; n         } n         if ((*it == b) || ((*it + 1) == b) || ((*it + 1) == (2 * b))) { n           continue; n         } else { n           printf( %ld %ld  , *it, *it + 1); n           i++; n         } n       } n       if (n % 2 == 1) { n         while ((*it == b) || ((*it + 1) == b) || ((*it + 1) == (2 * b))) { n           ++it; n         } n         printf( %ld , *it); n       } n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n const int maxn = 1e5 + 5, lim = 62, maxm = 65; n const long long inf = 1e18; n template <typename T> n inline T min(T a, T b) { n   return a < b ? a : b; n } n int n; n long long a[maxn], mod, dp[maxm][maxn]; n inline bool Comp(long long a, long long b) { return a > b; } n void radix_sort() { n   std::vector<long long> bit[2]; n   for (int i = 1; i <= n; i++) { n     bit[(a[i] & (1ll << (mod - 1))) > 0ll].push_back(a[i]); n   } n   int cnt = 0; n   for (int i = 0; i < bit[1].size(); i++) a[++cnt] = bit[1][i]; n   for (int i = 0; i < bit[0].size(); i++) a[++cnt] = bit[0][i]; n } n int main() { n   scanf( %d , &n); n   for (int i = 1; i <= n; i++) scanf( %lld , &a[i]); n   std::sort(a + 1, a + n + 1, Comp); n   for (int i = n; i >= 1; i--) a[i] = a[1] - a[i]; n   for (int i = 0; i <= lim; i++) n     for (int j = 0; j <= n; j++) dp[i][j] = inf; n   dp[0][0] = 0; n   for (int i = 0; i < lim; i++) { n     mod = i; n     if (i) radix_sort(); n     long long cnt = 0, tot = 0; n     mod = 1ll << i; n     for (int j = 1; j <= n; j++) cnt += a[j] & mod ? 1 : 0; n     for (int j = 0; j <= n; j++) { n       tot += a[j] & mod ? 1 : 0; n       dp[i + 1][cnt - tot + j] = n           min(dp[i + 1][cnt - tot + j], dp[i][j] + tot + (n - j) - (cnt - tot)); n       dp[i + 1][tot] = min(dp[i + 1][tot], dp[i][j] + j - tot + cnt - tot); n     } n   } n   printf( %lld n , dp[lim][0]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 100000 + 5; n int n; n int s[N], c[N]; n map<int, int> a, b; n int main() { n   int i; n   scanf( %d , &n); n   for (i = 0; i < n; i++) { n     scanf( %d , &s[i]); n     c[i] = s[i]; n   } n   printf( YES n ); n   if (n == 1) { n     printf( 0 n%d n , s[0]); n     return 0; n   } n   sort(c, c + n); n   int m = (n + 2) / 3; n   for (i = 0; i < n; i++) { n     if (i < m) { n       a[c[i]] = 0; n       b[c[i]] = c[i]; n     } else if (i < m + m) { n       a[c[i]] = c[i]; n       b[c[i]] = 0; n     } else { n       a[c[i]] = n - (i + 1); n       b[c[i]] = c[i] - (n - (i + 1)); n     } n   } n   for (i = 0; i < n; i++) { n     printf( %d , a[s[i]]); n     if (i == n - 1) n       printf(  n ); n     else n       printf(   ); n   } n   for (i = 0; i < n; i++) { n     printf( %d , b[s[i]]); n     if (i == n - 1) n       printf(  n ); n     else n       printf(   ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 200000 + 5; n long long a[maxn]; n map<long long, long long> mp, show; n int main() { n   long long n, k, ans = 0; n   scanf( %lld%lld , &n, &k); n   for (int i = 0; i < n; i++) scanf( %lld , a + i); n   for (int i = n - 1; i >= 0; i--) { n     ans += mp[a[i]]; n     if (a[i] % k == 0) mp[a[i] / k] += show[a[i] * k]; n     show[a[i]]++; n   } n   printf( %lld n , ans); n } n


#include <bits/stdc++.h> n #pragma GCC optimize( Ofast ) n using namespace std; n const long double eps = 1e-7; n const int inf = 1000000010; n const long long INF = 10000000000000010LL; n const int mod = 1000000007; n const int MAXN = 200010, LOG = 20; n int n, m, k, u, v, x, y, t, a, b; n long long A[MAXN]; n int par[MAXN][LOG]; n long long dist[MAXN][LOG]; n int ans[MAXN]; n vector<int> G[MAXN]; n void dfs(int node) { n   for (int i = 1; i < LOG; i++) par[node][i] = par[par[node][i - 1]][i - 1]; n   for (int i = 1; i < LOG; i++) n     dist[node][i] = n         min(1ll * inf, dist[par[node][i - 1]][i - 1] + dist[node][i - 1]); n   for (int v : G[node]) { n     dfs(v); n     ans[node] += ans[v]; n   } n   ans[par[node][0]]++; n   long long v = node, tmp = A[node]; n   for (int i = LOG - 1; i >= 0; i--) n     if (dist[v][i] <= tmp) { n       tmp -= dist[v][i]; n       v = par[v][i]; n     } n   v = par[v][0]; n   ans[v]--; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   cin >> n; n   for (int i = 1; i <= n; i++) cin >> A[i]; n   for (int i = 2; i <= n; i++) { n     cin >> par[i][0] >> dist[i][0]; n     G[par[i][0]].push_back(i); n   } n   par[1][0] = 0; n   dfs(1); n   for (int i = 1; i <= n; i++) cout << ans[i] <<    ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long t, n, a[100011], sum[100011], ans; n map<long long, long long> mp; n signed main() { n   scanf( %lld , &t); n   while (t--) { n     mp.clear(); n     scanf( %lld , &n); n     memset(a, 0, sizeof a); n     memset(sum, 0, sizeof sum); n     ans = 0; n     for (register long long i = 1; i <= n; ++i) { n       a[i] = getchar() -  0 ; n       while (a[i] < 0 || a[i] > 9) a[i] = getchar() -  0 ; n       a[i] = (a[i]) ? a[i] - 1 : -1; n       sum[i] = sum[i - 1] + a[i]; n     } n     mp[0] = 1; n     for (register long long i = 1; i <= n; ++i) { n       ans += mp[sum[i]]; n       ++mp[sum[i]]; n     } n     printf( %lld n , ans); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, k; n   int i, ans, j, t; n   int num[2005]; n   while (scanf( %d%d , &n, &k) != EOF) { n     ans = 0; n     for (i = 0; i < n; i++) scanf( %d , &num[i]); n     for (i = 0; i < n; i++) n       for (j = 0; j < n - i - 1; j++) n         if (num[j] > num[j + 1]) { n           t = num[j]; n           num[j] = num[j + 1]; n           num[j + 1] = t; n         } n     for (i = n - 1; i >= 0; i = i - k) ans += (num[i] - 1) * 2; n     printf( %d n , ans); n   } n } n


#include <bits/stdc++.h> n using namespace std; n const long long INF = 3e18 + 5; n int N; n long long pt[100005][3]; n long long ans[3]; n pair<long long, long long> gSeg[4]; n void input() { n   cin >> N; n   for (int i = 0; i < (N); i++) cin >> pt[i][0] >> pt[i][1] >> pt[i][2]; n } n inline long long mod2(long long x) { return (x % 2 + 2) % 2; } n bool check(long long v) { n   for (int rem = 0; rem < (2); rem++) { n     pair<long long, long long> seg[4]; n     for (int i = 0; i < (4); i++) { n       seg[i].first = gSeg[i].first - v; n       seg[i].second = gSeg[i].second + v; n       if (mod2(seg[i].first) != rem) seg[i].first++; n       if (mod2(seg[i].second) != rem) seg[i].second--; n     } n     int fail = 0; n     for (int i = 0; i < (4); i++) n       if (seg[i].first > seg[i].second) { n         fail = 1; n       } n     if (fail) continue; n     if (seg[0].second < seg[1].first + seg[2].first + seg[3].first) continue; n     if (seg[0].first > seg[1].second + seg[2].second + seg[3].second) continue; n     long long sum = seg[1].first + seg[2].first + seg[3].first; n     for (int i = (1); i <= (3); i++) { n       if (sum >= seg[0].first) break; n       if (sum + seg[i].second - seg[i].first < seg[0].first) { n         sum += seg[i].second - seg[i].first; n         seg[i].first = seg[i].second; n       } else { n         seg[i].first += seg[0].first - sum; n         if (mod2(seg[i].first) != rem) seg[i].first++; n         if (seg[i].first > seg[i].second) { n           seg[i].first--; n           sum--; n         } else { n           break; n         } n       } n     } n     ans[0] = (seg[2].first + seg[3].first) / 2; n     ans[1] = (seg[1].first + seg[3].first) / 2; n     ans[2] = (seg[1].first + seg[2].first) / 2; n     assert(mod2(seg[2].first + seg[3].first) == 0); n     assert(mod2(seg[1].first + seg[3].first) == 0); n     assert(mod2(seg[1].first + seg[2].first) == 0); n     return true; n   } n   return false; n } n void solve() { n   for (int i = 0; i < (4); i++) gSeg[i] = pair<long long, long long>(-INF, INF); n   for (int i = 0; i < (N); i++) { n     long long x = pt[i][0]; n     long long y = pt[i][1]; n     long long z = pt[i][2]; n     gSeg[0].first = max(gSeg[0].first, x + y + z); n     gSeg[0].second = min(gSeg[0].second, x + y + z); n     gSeg[1].first = max(gSeg[1].first, -x + y + z); n     gSeg[1].second = min(gSeg[1].second, -x + y + z); n     gSeg[2].first = max(gSeg[2].first, x - y + z); n     gSeg[2].second = min(gSeg[2].second, x - y + z); n     gSeg[3].first = max(gSeg[3].first, x + y - z); n     gSeg[3].second = min(gSeg[3].second, x + y - z); n   } n   long long l = 0, r = 4e18 + 5; n   while (l < r) { n     long long m = (l + r) / 2; n     if (check(m)) n       r = m; n     else n       l = m + 1; n   } n   assert(check(l)); n   cout << ans[0] <<     << ans[1] <<     << ans[2] <<   n ; n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   ; n   int ts; n   cin >> ts; n   for (int t = 0; t < (ts); t++) { n     input(); n     solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int i, j, k, sum; n string a; n int main() { n   cin >> a; n   int l = a.size(); n   j = 0; n   for (i = 0; i < l; i++) { n     if (a[i] ==  a ) { n       k++; n     } n   } n   if (k > l / 2) { n     j = l; n   } else { n     for (i = l - 1; i > 0; i--) { n       if (k > i / 2) { n         j = i; n         break; n       } n     } n   } n   cout << j << endl; n   return 0; n } n


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


#include <bits/stdc++.h> n using namespace std; n mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count()); n mt19937 rnf(2106); n const int N = 100005; n bool c[(1 << 22)]; n int d[(1 << 22)]; n int p[(1 << 22)]; n void solv0(int n) { n   vector<int> v; n   for (int x = 0; x < n; ++x) { n     for (int y = x + 1; y < n; ++y) { n       for (int z = y + 1; z < n; ++z) { n         if (y - x == z - y) { n           v.push_back(((1 << x) | (1 << y) | (1 << z))); n         } n       } n     } n   } n   for (int x = 0; x < (1 << n); ++x) c[x] = false; n   queue<int> q; n   c[0] = true; n   d[0] = 0; n   q.push(0); n   while (!q.empty()) { n     int x = q.front(); n     q.pop(); n     for (int i = 0; i < ((int)(v).size()); ++i) { n       int h = (x ^ v[i]); n       if (!c[h]) { n         c[h] = true; n         d[h] = d[x] + 1; n         p[h] = v[i]; n         q.push(h); n       } n     } n   } n   return; n   for (int x = 0; x < (1 << n); ++x) { n     if (!c[x]) { n       for (int i = 0; i < n; ++i) { n         if ((x & (1 << i))) n           cout <<  1 ; n         else n           cout <<  0 ; n       } n       cout << endl; n     } n   } n   int maxu = 0; n   for (int x = 0; x < (1 << n); ++x) { n     if (!c[x]) continue; n     maxu = max(maxu, d[x]); n   } n   cout << maxu <<   n ; n   for (int x = 0; x < (1 << n); ++x) { n     if (!c[x]) continue; n     if (maxu == d[x]) { n       for (int i = 0; i < n; ++i) { n         if ((x & (1 << i))) n           cout <<  1 ; n         else n           cout <<  0 ; n       } n       cout << endl; n       cout << endl; n       while (x) { n         for (int i = 0; i < n; ++i) { n           if ((p[x] & (1 << i))) n             cout <<  1 ; n           else n             cout <<  0 ; n         } n         cout << endl; n         x ^= p[x]; n       } n       return; n     } n   } n } n void solv6() { n   int n = 6; n   vector<int> v; n   for (int x = 0; x < n; ++x) { n     for (int y = x + 1; y < n; ++y) { n       for (int z = y + 1; z < n; ++z) { n         if (y - x == z - y) { n           v.push_back(((1 << x) | (1 << y) | (1 << z))); n         } n       } n       if (x - (y - x) < 0) v.push_back(((1 << x) | (1 << y))); n     } n     v.push_back((1 << x)); n   } n   for (int x = 0; x < (1 << n); ++x) c[x] = false; n   queue<int> q; n   c[0] = true; n   d[0] = 0; n   q.push(0); n   while (!q.empty()) { n     int x = q.front(); n     q.pop(); n     for (int i = 0; i < ((int)(v).size()); ++i) { n       int h = (x ^ v[i]); n       if (!c[h]) { n         c[h] = true; n         d[h] = d[x] + 1; n         p[h] = v[i]; n         q.push(h); n       } n     } n   } n   return; n   for (int x = 0; x < (1 << n); ++x) { n     if (!c[x]) { n       for (int i = 0; i < n; ++i) { n         if ((x & (1 << i))) n           cout <<  1 ; n         else n           cout <<  0 ; n       } n       cout << endl; n     } n   } n   int maxu = 0; n   for (int x = 0; x < (1 << n); ++x) { n     if (!c[x]) continue; n     maxu = max(maxu, d[x]); n   } n   cout << maxu <<   n ; n   for (int x = 0; x < (1 << n); ++x) { n     if (!c[x]) continue; n     if (maxu == d[x]) { n       for (int i = 0; i < n; ++i) { n         if ((x & (1 << i))) n           cout <<  1 ; n         else n           cout <<  0 ; n       } n       cout << endl; n       cout << endl; n       while (x) { n         for (int i = 0; i < n; ++i) { n           if ((p[x] & (1 << i))) n             cout <<  1 ; n           else n             cout <<  0 ; n         } n         cout << endl; n         x ^= p[x]; n       } n       return; n     } n   } n } n int n; n int a[N]; n vector<tuple<int, int, int> > ans; n void ubd(int x, int y, int z) { n   assert(x >= 1 && x <= n && y >= 1 && y <= n && z >= 1 && z <= n); n   assert(x < y && y < z && (y - x) == (z - y)); n   ans.push_back(tie(x, y, z)); n   a[x] ^= 1; n   a[y] ^= 1; n   a[z] ^= 1; n } n void solv() { n   cin >> n; n   for (int i = 1; i <= n; ++i) cin >> a[i]; n   solv6(); n   while (n >= 14) { n     int x = 0; n     for (int i = n - 5; i <= n; ++i) { n       x += (a[i] * (1 << (i - (n - 5)))); n     } n     while (x) { n       vector<int> v; n       for (int i = 0; i < 6; ++i) { n         if ((p[x] & (1 << i))) v.push_back(n - 5 + i); n       } n       if (((int)(v).size()) == 3) n         ubd(v[0], v[1], v[2]); n       else if (((int)(v).size()) == 2) n         ubd(v[0] - (v[1] - v[0]), v[0], v[1]); n       else n         ubd(n - 6 - (v[0] - (n - 6)), n - 6, v[0]); n       x ^= p[x]; n     } n     n -= 6; n   } n   solv0(n); n   int x = 0; n   for (int i = 1; i <= n; ++i) x += (a[i] * (1 << (i - 1))); n   if (!c[x]) { n     cout <<  NO n ; n     return; n   } n   while (x) { n     vector<int> v; n     for (int i = 0; i < n; ++i) { n       if ((p[x] & (1 << i))) v.push_back(i + 1); n     } n     ubd(v[0], v[1], v[2]); n     x ^= p[x]; n   } n   for (int i = 1; i <= n; ++i) assert(!a[i]); n   cout <<  YES n ; n   cout << ((int)(ans).size()) <<   n ; n   for (int i = 0; i < ((int)(ans).size()); ++i) n     cout << get<0>(ans[i]) <<     << get<1>(ans[i]) <<     << get<2>(ans[i]) n          <<   n ; n } n int main() { n   ios_base::sync_with_stdio(false), cin.tie(0); n   int tt = 1; n   while (tt--) { n     solv(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void cp() { n   ios ::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n } n long long power(long long x, long long y) { n   long long res = 1; n   while (y > 0) { n     if (y & 1) res = res * x; n     y = y >> 1; n     x = x * x; n   } n   return res; n } n int main() { n   cp(); n   { n     long long a; n     cin >> a; n     ; n     long long b; n     cin >> b; n     ; n     if (a == b) n       cout <<  0 ; n     else { n       long long y = log2(a ^ b) + 1; n       cout << power(2, y) - 1; n     } n   } n   cerr <<  Time :   << 1000 * ((double)clock()) / (double)CLOCKS_PER_SEC n        <<  ms n ; n } n


#include <bits/stdc++.h> n using namespace std; n void __print(int first) { cerr << first; } n void __print(long first) { cerr << first; } n void __print(long long first) { cerr << first; } n void __print(unsigned first) { cerr << first; } n void __print(unsigned long first) { cerr << first; } n void __print(unsigned long long first) { cerr << first; } n void __print(float first) { cerr << first; } n void __print(double first) { cerr << first; } n void __print(long double first) { cerr << first; } n void __print(char first) { cerr <<      << first <<     ; } n void __print(const char *first) { cerr <<     << first <<    ; } n void __print(const string &first) { cerr <<     << first <<    ; } n void __print(bool first) { cerr << (first ?  true  :  false ); } n template <typename T, typename V> n void __print(const pair<T, V> &first) { n   cerr <<  { ; n   __print(first.first); n   cerr <<  , ; n   __print(first.second); n   cerr <<  } ; n } n template <typename T> n void __print(const T &first) { n   int f = 0; n   cerr <<  { ; n   for (auto &i : first) cerr << (f++ ?  ,  :   ), __print(i); n   cerr <<  } ; n } n void _print() { cerr <<  ] n ; } n template <typename T, typename... V> n void _print(T t, V... v) { n   __print(t); n   if (sizeof...(v)) cerr <<  ,  ; n   _print(v...); n } n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   ; n   long long n; n   cin >> n; n   long long a[n + 1]; n   for (long long i = 1; i < n + 1; i++) cin >> a[i]; n   vector<long long> b; n   b.push_back(0), b.push_back(0); n   for (long long i = 1; i < n + 1; i++) b.push_back(a[i]); n   for (long long i = 1; i < n + 1; i++) b.push_back(a[i]); n   ; n   vector<long long> dp(b.size()); n   for (long long i = 2; i < b.size(); i++) dp[i] = dp[i - 2] + b[i]; n   ; n   long long best = 0; n   for (long long i = 2; i < b.size(); i++) { n     if (i + n - 1 >= b.size()) break; n     best = max(best, dp[i + n - 1] - dp[i - 2]); n   } n   cout << best <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n template <class T> n inline T gcd(T a, T b) { n   return b == 0 ? a : gcd(b, a % b); n } n template <class T> n inline T lcm(T a, T b) { n   return (a / gcd(a, b)) * b; n } n int main() { n   int n; n   int d = 210; n   cin >> n; n   if (n < 3) { n     cout << -1 << endl; n     return 0; n   } n   if (n == 3) { n     cout << 210 << endl; n     return 0; n   } n   cout << 1; n   int m = 1; n   for (int i = 0; i < (n - 1); i++) { n     m *= 10; n     m %= 210; n     if (i < n - 4) cout << 0; n   } n   int r = 210 - m; n   if (r < 10) n     cout << 00 << r << endl; n   else if (r < 100) n     cout << 0 << r << endl; n   else n     cout << r << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<int> v; n int main() { n   long long p, k; n   cin >> p >> k; n   while (p) { n     long long y = p % k; n     if (y < 0) y += k; n     v.push_back(y); n     p -= y; n     p /= k; n     p *= -1; n   } n   cout << v.size() << endl; n   for (int i = 0; i < v.size(); i++) { n     cout << v[i] <<    ; n   } n   cout << endl; n } n


#include <bits/stdc++.h> n using std::cin; n using std::cout; n using std::deque; n using std::endl; n using std::map; n using std::max; n using std::min; n using std::multiset; n using std::pair; n using std::queue; n using std::set; n using std::sort; n using std::stack; n using std::string; n using std::vector; n int main() { n   string a, b, c; n   cin >> a >> b >> c; n   int dataA[26] = { n       0, n   }; n   int dataB[26] = { n       0, n   }; n   int dataC[26] = { n       0, n   }; n   for (int i = 0; i < a.size(); i++) { n     dataA[a[i] -  a ]++; n   } n   for (int i = 0; i < b.size(); i++) { n     dataB[b[i] -  a ]++; n   } n   for (int i = 0; i < c.size(); i++) { n     dataC[c[i] -  a ]++; n   } n   int cntB = 0; n   int cntC = 0; n   for (int cnt = 0; cnt <= 100000; cnt++) { n     int tempA[26]; n     for (int i = 0; i < 26; i++) { n       tempA[i] = dataA[i]; n     } n     int flag = 1; n     for (int i = 0; i < 26; i++) { n       long long t = (long long)tempA[i] - (long long)cnt * dataB[i]; n       tempA[i] -= cnt * dataB[i]; n       if (t < 0) { n         flag = 0; n       } n     } n     if (flag == 0) { n       break; n     } n     int x = 1000000; n     for (int i = 0; i < 26; i++) { n       if (dataC[i] == 0) continue; n       x = max(0, min(x, tempA[i] / dataC[i])); n     } n     if (cnt + x > cntB + cntC) { n       cntB = cnt; n       cntC = x; n     } n   } n   for (int i = 0; i < 26; i++) { n     dataA[i] -= cntB * dataB[i]; n     dataA[i] -= cntC * dataC[i]; n   } n   for (int i = 0; i < cntB; i++) { n     cout << b; n   } n   for (int i = 0; i < cntC; i++) { n     cout << c; n   } n   for (int i = 0; i < 26; i++) { n     for (int j = 0; j < dataA[i]; j++) printf( %c , i +  a ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 100005; n struct EDGE { n   int num, next; n } e[MAXN * 2]; n int en = 0, start[MAXN]; n inline void Add_edge(int p, int q) { n   en++; n   e[en].num = q; n   e[en].next = start[p]; n   start[p] = en; n   en++; n   e[en].num = p; n   e[en].next = start[q]; n   start[q] = en; n } n double stp[MAXN], edp[MAXN]; n int n; n bool u[MAXN]; n int size[MAXN]; n double sst[MAXN], pro[MAXN]; n void dfs(int p) { n   u[p] = true; n   size[p] = 1; n   sst[p] = stp[p]; n   for (int i = start[p]; i != 0; i = e[i].next) n     if (!u[e[i].num]) { n       dfs(e[i].num); n       size[p] += size[e[i].num]; n       sst[p] += sst[e[i].num]; n       pro[p] += size[e[i].num] * sst[e[i].num]; n     } n   pro[p] += (n - size[p]) * (1 - sst[p]); n } n int main() { n   scanf( %d , &n); n   for (int i = 1; i < n; i++) { n     int p, q; n     scanf( %d%d , &p, &q); n     Add_edge(p, q); n   } n   double stps = 0, edps = 0; n   for (int i = 1; i <= n; i++) scanf( %lf%lf , &stp[i], &edp[i]); n   for (int i = 1; i <= n; i++) { n     stps += stp[i]; n     edps += edp[i]; n   } n   for (int i = 1; i <= n; i++) { n     stp[i] /= stps; n     edp[i] /= edps; n   } n   dfs(1); n   double ans = 0; n   for (int i = 1; i <= n; i++) ans += pro[i] * edp[i]; n   printf( %.11lf n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m; n bool graph[101][101]; n bool traversed[101]; n void search(int x) { n   for (int i = 0; i <= m; i++) n     if (graph[x][i]) n       for (int k = 0; k < n; k++) n         if (graph[k][i] && !traversed[k]) { n           traversed[k] = true; n           search(k); n         } n } n int main() { n   cin >> n >> m; n   int size, lang; n   int need = 0; n   int all = 0; n   for (int i = 0; i < n; i++) { n     cin >> size; n     if (size == 0) all++; n     for (int k = 0; k < size; k++) { n       cin >> lang; n       graph[i][lang] = true; n     } n   } n   if (all == n) n     cout << all << endl; n   else { n     for (int i = 0; i < n; i++) { n       if (!traversed[i]) { n         need++; n         traversed[i] = true; n         search(i); n       } n     } n     cout << need - 1 << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n string s1(const pair<string, string> &x) { return x.first.substr(0, 3); } n string s2(const pair<string, string> &x) { n   return x.first.substr(0, 2) + x.second.substr(0, 1); n } n const int MAXN = 1050; n vector<int> g[MAXN], mt; n int used[MAXN]; n bool dfs(const int &v, const int &now) { n   if (used[v] == now) return false; n   used[v] = now; n   for (auto to : g[v]) { n     if (mt[to] == -1 || dfs(mt[to], now)) { n       mt[to] = v; n       return true; n     } n   } n   return false; n } n signed main() { n   ios_base::sync_with_stdio(false); n   cin.tie(nullptr); n   cout.tie(nullptr); n   int n; n   cin >> n; n   vector<pair<string, string>> a(n); n   for (int i = 0; i < n; ++i) { n     cin >> a[i].first >> a[i].second; n   } n   map<string, int> str_idx; n   map<string, bool> banned; n   vector<string> idx_str; n   for (char c1 =  A ; c1 <=  Z ; ++c1) { n     for (char c2 =  A ; c2 <=  Z ; ++c2) { n       for (char c3 =  A ; c3 <=  Z ; ++c3) { n         string name =   ; n         name += c1; n         name += c2; n         name += c3; n         str_idx[name] = idx_str.size(); n         idx_str.emplace_back(name); n       } n     } n   } n   vector<bool> precalced(n); n   for (int i = 0; i < n; ++i) { n     for (int j = i + 1; j < n; ++j) { n       if (s1(a[i]) == s1(a[j])) { n         precalced[i] = precalced[j] = true; n       } n     } n   } n   int cnt = 0; n   for (int i = 0; i < n; ++i) { n     if (precalced[i]) { n       string name = s2(a[i]); n       if (banned[name]) { n         cout <<  NO ; n         return 0; n       } n       ++cnt; n       banned[name] = true; n     } n   } n   for (int i = 0; i < n; ++i) { n     if (!precalced[i]) { n       string name1 = s1(a[i]); n       string name2 = s2(a[i]); n       if (!banned[name1]) { n         g[i].emplace_back(str_idx[name1]); n       } n       if (!banned[name2]) { n         g[i].emplace_back(str_idx[name2]); n       } n     } n   } n   mt.resize(30 * 30 * 30, -1); n   for (int i = 0; i < n; ++i) { n     if (!precalced[i]) dfs(i, i + 1); n   } n   for (int i = 0; i < 30 * 30 * 30; ++i) { n     if (mt[i] != -1) ++cnt; n   } n   if (cnt == n) { n     cout <<  YES n ; n     vector<string> ans(n); n     for (int i = 0; i < n; ++i) { n       if (precalced[i]) ans[i] = s2(a[i]); n     } n     for (int i = 0; i < 30 * 30 * 30; ++i) { n       if (mt[i] != -1) ans[mt[i]] = idx_str[i]; n     } n     for (auto s : ans) cout << s <<   n ; n   } else { n     cout <<  NO n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 5; n int vis[N][N][N]; n int l[N], r[N]; n double get_frac(int x, int y) { return (1.0 * x) / (1.0 * y); } n double dfs(vector<vector<vector<double>>> &dp, int b, int cur, int g, int e, n            int n) { n   if (cur == n) { n     if (g == 0) n       return (e > 1) ? 1.0 : 0.0; n     else if (g == 1) n       return (e > 0) ? 1.0 : 0.0; n     return 0.0; n   } n   if (vis[cur][g][e] == -1) { n     vis[cur][g][e] = 0; n     double p = 0; n     if (g == 0 && r[cur] > b) n       p += get_frac(r[cur] - max(b, l[cur] - 1), r[cur] - l[cur] + 1) * n            dfs(dp, b, cur + 1, g + 1, e, n); n     if (l[cur] <= b && r[cur] >= b) n       p += get_frac(1, r[cur] - l[cur] + 1) * dfs(dp, b, cur + 1, g, e + 1, n); n     if (l[cur] < b) n       p += get_frac(min(r[cur] + 1, b) - l[cur], r[cur] - l[cur] + 1) * n            dfs(dp, b, cur + 1, g, e, n); n     dp[cur][g][e] = p; n   } n   return dp[cur][g][e]; n } n double solve() { n   int n, mn = 1e4 + 5, mx = 0; n   double res = 0, sum = 0; n   cin >> n; n   for (int i = 0; i < n; i++) { n     cin >> l[i] >> r[i]; n     mn = min(mn, l[i]); n     mx = max(mx, r[i]); n   } n   for (int i = mn; i <= mx; i++) { n     memset(vis, -1, sizeof(vis)); n     vector<vector<vector<double>>> dp( n         n, vector<vector<double>>(n, vector<double>(n))); n     res += 1.0 * i * dfs(dp, i, 0, 0, 0, n); n   } n   return res; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   cout << fixed << setprecision(12); n   cout << solve() <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long solve() { n   long long n; n   cin >> n; n   string str1, str2; n   cin >> str1 >> str2; n   long long freq1[26] = {0}; n   long long freq2[26] = {0}; n   for (long long i = 0; i < n; i++) { n     freq1[str1[i] -  a ]++; n     freq2[str2[i] -  a ]++; n   } n   for (long long i = 0; i < 26; i++) { n     if (freq1[i] != freq2[i]) { n       cout <<  NO n ; n       return 0; n     } n   } n   for (long long i = 0; i < 26; i++) { n     if (freq1[i] >= 2) { n       cout <<  YES n ; n       return 0; n     } n   } n   for (long long i = 0; i < n - 2; i++) { n     long long pos = -1; n     for (long long j = i; j < n; j++) { n       if (str1[i] == str2[j]) { n         pos = j; n         break; n       } n     } n     long long cnt = 0; n     while (pos > i) { n       swap(str2[pos], str2[pos - 1]); n       cnt++; n       pos--; n     } n     cnt = cnt % 2; n     if (cnt == 0) n       continue; n     else n       swap(str2[n - 1], str2[n - 2]); n   } n   for (long long i = 0; i < n; i++) { n     if (str1[i] != str2[i]) { n       cout <<  NO n ; n       return 0; n     } n   } n   cout <<  YES n ; n   return 0; n } n int main() { n   ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0); n   ; n   long long t = 1; n   cin >> t; n   while (t-- != 0) { n     long long stat = solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   cin >> n; n   vector<int> v(n); n   for (int i = 0; i < n; ++i) cin >> v[i]; n   for (int i = 0; i < n; ++i) { n     if (v[i] != i + 1) { n       for (int j = n - 1; j >= 0; --j) { n         if (v[j] != j + 1) { n           reverse(begin(v) + i, begin(v) + j + 1); n           for (int k = i; k <= j; ++k) { n             if (v[k] != k + 1) goto __bad; n           } n           cout << i + 1 <<     << j + 1 << endl; n           return 0; n         } n       } n     } n   } n __bad: n   cout <<  0 0 n ; n   return 0; n } n


#include <bits/stdc++.h> n using lint = long long int; n using pii = std::pair<int, int>; n using std::vector; n int ve[200100]; n bool check[200100]; n int main(void) { n   int N, hol = 0; n   scanf( %d , &N); n   for (int i = 1; i <= N; i++) { n     int temp; n     scanf( %d , &temp); n     ve[i] = temp; n   } n   for (int i = 1; i <= N; i++) { n     int temp; n     scanf( %d , &temp); n     hol += temp; n   } n   int cou = 0; n   for (int i = 1; i <= N; i++) { n     if (!check[i]) { n       int ind = i; n       cou++; n       while (!check[ind]) { n         check[ind] = true; n         ind = ve[ind]; n       } n     } n   } n   int ans = 1 - (hol % 2); n   if (cou != 1) { n     ans += cou; n   } n   printf( %d , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int etf(int n) { n   int res = n, i = 2; n   while (n > 1) { n     if (n % i == 0) { n       res -= res / i; n       while (n % i == 0) n /= i; n     } n     ++i; n     if (i * i > n) i = n; n   } n   return res; n } n int main() { n   ios::sync_with_stdio(0); n   int p; n   cin >> p; n   cout << etf(p - 1) <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1010; n int n, w, a[N]; n int main() { n   cin >> n >> w; n   for (int i = 0; i < n; i++) { n     cin >> a[i]; n   } n   int lo = 0, hi = w, n_lo = 0, n_hi = w; n   for (int i = 0; i < n; i++) { n     n_lo += a[i]; n     n_hi += a[i]; n     if (n_lo > w || n_hi < 0) return cout << 0 <<   n , 0; n     if (n_lo < 0) { n       lo += -n_lo; n       n_lo = 0; n     } n     if (n_hi > w) { n       hi -= n_hi - w; n       n_hi = w; n     } n   } n   cout << (hi - lo + 1) <<   n ; n   return 0; n } n