#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   cin >> n; n   vector<int> arr(n); n   for (int i = 0; i < n; ++i) cin >> arr[i]; n   vector<bool> used(n, false); n   int count = 0; n   int marked = 0; n   while (marked < n) { n     int min = 101; n     for (int i = 0; i < n; ++i) { n       if (arr[i] < min && !used[i]) min = arr[i]; n     } n     for (int i = 0; i < n; ++i) n       if (arr[i] % min == 0) { n         if (!used[i]) ++marked; n         used[i] = true; n       } n     ++count; n   } n   cout << count; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, k; n   long long S; n   cin >> n >> k >> S; n   int a[30]; n   for (int i = 0; i < n; i++) cin >> a[i]; n   long long fac[50]; n   fac[0] = 1; n   for (int i = 1; i < 50; i++) { n     fac[i] = i * fac[i - 1]; n     if (fac[i] > 10000000000000000ll) fac[i] = 10000000000000001ll; n   } n   int bit = n / 2; n   bit++; n   map<pair<long long, int>, long long> mp; n   int lim = pow(3ll, (long long)bit); n   for (int i = 0; i < lim; i++) { n     int rem = i; n     int cnt = 0; n     long long sm = 0; n     for (int j = 0; j < bit; j++) { n       if (rem % 3 == 2) { n         cnt++; n         sm += fac[min(49, a[j])]; n       } else if (rem % 3 == 1) n         sm += a[j]; n       rem /= 3; n     } n     if (cnt > k || sm > S) continue; n     pair<long long, int> p = make_pair(sm, cnt); n     mp[p]++; n   } n   long long r = 0; n   int bit2 = n - bit; n   lim = pow(3ll, (long long)bit2); n   for (int i = 0; i < lim; i++) { n     int rem = i; n     int cnt = 0; n     long long sm = 0; n     for (int j = 0; j < bit2; j++) { n       if (rem % 3 == 2) { n         cnt++; n         sm += fac[min(49, a[j + bit])]; n       } else if (rem % 3 == 1) n         sm += a[j + bit]; n       rem /= 3; n     } n     if (sm > S) continue; n     for (int K = cnt; K <= k; K++) { n       pair<long long, int> p = make_pair(S - sm, k - K); n       r += mp[p]; n     } n   } n   cout << r << endl; n } n


#include <bits/stdc++.h> n using namespace std; n const int INF = 0x3fffffff; n int B[100000 + 5]; n int S[100000 + 5]; n int main() { n   int k, n; n   cin >> n >> k; n   for (int i = 0; i < n; i++) { n     char c; n     int a, b; n     cin >> c >> a >> b; n     if (c ==  B ) { n       B[a] += b; n     } else { n       S[a] += b; n     } n   } n   int t = k; n   stack<int> SS, SSS; n   for (int i = 0; i <= 100000 + 4 && t; i++) { n     if (S[i]) SS.push(i), SSS.push(S[i]), t--; n   } n   while (!SS.empty()) { n     int a = SS.top(), b = SSS.top(); n     SS.pop(); n     SSS.pop(); n     printf( S %d %d n , a, b); n   } n   t = k; n   for (int i = 100000 + 4; i >= 0 && t; i--) { n     if (B[i]) printf( B %d %d n , i, B[i]), t--; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int n, f[2][200]; n int main() { n   f[0][0] = f[1][1] = 1; n   int t = 1; n   cin >> n; n   for (int i = 2; i <= n; ++i) { n     t = i & 1; n     for (int j = 1; j <= i; ++j) f[t][j] = (f[t][j] + f[t ^ 1][j - 1]) & 1; n   } n   printf( %d n , n); n   for (int i = 0; i <= n; ++i) printf( %d  , f[t][i]); n   printf(  n%d n , n - 1); n   for (int i = 0; i < n; ++i) printf( %d  , f[t ^ 1][i]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int INF = 0x3f3f3f3f; n int N, M; n int A[5002], B[5002]; n int D[5002], E[5002], F[5002]; n int result; n int gcd(int a, int b) { n   if (b == 0) return a; n   return gcd(b, a % b); n } n int getDif(int x) { n   int resnow = 0; n   for (int i = 1; i <= M; ++i) n     while (x % B[i] == 0) { n       --resnow; n       x /= B[i]; n     } n   for (int i = 2; i * i <= x; ++i) n     while (x % i == 0) { n       ++resnow; n       x /= i; n     } n   if (x != 1) ++resnow; n   return resnow; n } n int main() { n   cin.sync_with_stdio(false); n   cin >> N >> M; n   for (int i = 1; i <= N; ++i) cin >> A[i]; n   for (int i = 1; i <= M; ++i) cin >> B[i]; n   E[0] = 0; n   for (int i = 1; i <= N; ++i) { n     E[i] = gcd(E[i - 1], A[i]); n     F[i] = getDif(E[i]); n   } n   int base = 0; n   for (int i = 1; i <= N; ++i) base += getDif(A[i]); n   result = base; n   for (int i = N; i >= 1; --i) { n     D[i] = F[i] * i; n     for (int j = i + 1; j <= N; ++j) D[i] = min(D[i], D[j] + (F[i] - F[j]) * i); n     result = max(result, base - D[i]); n   } n   cout << result <<   n ; n } n


#include <bits/stdc++.h> n int mn(int a, int b) { return a < b ? a : b; } n int main() { n   int a, b; n   scanf( %d%d , &a, &b); n   int x = mn(a, b); n   int ans = 1; n   for (int i = 2; i <= x; i++) { n     ans *= i; n   } n   printf( %d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int t; n   cin >> t; n   int ara[3]; n   int a, b, c, sum, d, sum1; n   int m; n   for (int wq = 0; wq < t; wq++) { n     cin >> ara[0] >> ara[1] >> ara[2]; n     sort(ara, ara + 3); n     m = ara[2]; n     sum = ara[0] + ara[1] + ara[2]; n     sum1 = ara[0] + ara[1]; n     if (m > sum1) { n       d = sum1; n     } else { n       d = sum / 2; n     } n     cout << d << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m, s, d; n vector<int> a; n map<int, int> p, q; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(nullptr); n   cin >> n >> m >> s >> d; n   a.resize(n); n   for (int i = 0; i < n; ++i) { n     cin >> a[i]; n   } n   sort(a.begin(), a.end()); n   if (a[0] <= s) { n     cout <<  IMPOSSIBLE  << endl; n     return 0; n   } n   p[a[0] - 1] = 0; n   for (int i = 0; i < n; ++i) { n     int next = a[i] + 1; n     if (binary_search(a.begin(), a.end(), next)) { n       continue; n     } n     int prev = next - d; n     auto pi = p.lower_bound(prev); n     if (pi == p.end()) { n       continue; n     } n     q[next] = pi->first; n     if (i < n - 1 && next + s < a[i + 1]) { n       p[a[i + 1] - 1] = next; n     } else { n       if (i == n - 1) { n         p[m] = next; n       } n     } n   } n   if (p.find(m) != p.end()) { n     int last = m; n     vector<pair<int, bool>> res; n     while (last != 0) { n       int prev = p[last]; n       if (prev < last) { n         res.push_back(make_pair(last - prev, true)); n         last = prev; n       } n       if (last > 0) { n         int prev_j = q[last]; n         res.push_back(make_pair(last - prev_j, false)); n         last = prev_j; n       } n     } n     reverse(res.begin(), res.end()); n     for (auto x : res) { n       if (x.second) { n         cout <<  RUN   << x.first <<   n ; n       } else { n         cout <<  JUMP   << x.first <<   n ; n       } n     } n   } else { n     cout <<  IMPOSSIBLE  << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n mt19937 rng(1234); n const long long mod = 1e9 + 7; n long long n, m, k, r, c; n long long ax, ay, bx, by; n long long modpow(long long u, long long v) { n   long long res = 1, t = u; n   while (v) { n     if (v & 1) res = res * t % mod; n     t = t * t % mod, v >>= 1; n   } n   return res; n } n signed main() { n   ios::sync_with_stdio(false); n   cin.tie(0), cout.precision(15); n   cin >> n >> m >> k >> r >> c; n   cin >> ax >> ay >> bx >> by; n   if (ax == bx && ay == by) { n     cout << modpow(k, n * m); n     return 0; n   } n   long long comp = n * m - r * c; n   cout << modpow(k, comp); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 2e5 + 5; n int n, m; n int pre[maxn]; n void init(int n) { n   for (int i = 0; i <= n + 1; i++) pre[i] = i; n } n int finds(int x) { n   if (x == pre[x]) n     return x; n   else n     return pre[x] = finds(pre[x]); n } n int main() { n   while (cin >> n >> m) { n     int ans = 0; n     init(n); n     for (int i = 1, a, b; i <= m; i++) { n       cin >> a >> b; n       int fa = finds(a); n       int fb = finds(b); n       if (fa != fb) { n         pre[fa] = fb; n       } else n         ans++; n     } n     cout << ans << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxN = 30, maxRow = maxN * maxN; n int n, m, a, b, d[maxN], g[maxN][maxN]; n double A[maxRow][maxRow], p[maxN][maxN]; n int main() { n   scanf( %d%d%d%d , &n, &m, &a, &b); n   --a, --b; n   for (int i = 0; i < m; ++i) { n     int x, y; n     scanf( %d%d , &x, &y); n     --x, --y; n     ++d[x], ++d[y], g[x][y] = g[y][x] = 1; n   } n   for (int i = 0; i < n; ++i) { n     scanf( %lf , &p[i][i]); n     for (int j = 0; j < n; ++j) n       if (g[i][j]) p[i][j] = (1.0 - p[i][i]) / d[i]; n   } n   for (int i = 0; i < n; ++i) n     for (int j = 0; j < n; ++j) { n       int now = i * n + j; n       A[now][now] = -1.0; n       for (int x = 0; x < n; ++x) n         for (int y = 0; y < n; ++y) n           if (x != y) { n             int that = x * n + y; n             A[now][that] += p[x][i] * p[y][j]; n           } n     } n   int N = n * n; n   A[a * n + b][N] = -1.0; n   for (int i = 0; i < N; ++i) { n     int r = i; n     for (int j = i + 1; j < N; ++j) n       if (fabs(A[j][i]) > fabs(A[r][i])) r = j; n     if (r != i) n       for (int j = 0; j < N + 1; ++j) swap(A[i][j], A[r][j]); n     for (int k = i + 1; k < N; ++k) { n       double f = A[k][i] / A[i][i]; n       for (int j = i; j <= N; ++j) A[k][j] -= A[i][j] * f; n     } n   } n   for (int i = N - 1; i >= 0; --i) { n     for (int j = i + 1; j < N; ++j) A[i][N] -= A[j][N] * A[i][j]; n     A[i][N] /= A[i][i]; n   } n   for (int i = 0; i < n; ++i) printf( %.7lf n , A[i * n + i][N]); n   return 0; n } n


#include <bits/stdc++.h> n void build_comp(int v, int comp_num, std::vector<int> &cur_comp, n                 std::vector<int> &rev_comps, n                 const std::vector<std::vector<int>> &g) { n   cur_comp.push_back(v); n   rev_comps[v] = comp_num; n   for (int to : g[v]) { n     if (rev_comps[to] == -1) { n       build_comp(to, comp_num, cur_comp, rev_comps, g); n     } n   } n } n void solve() { n   int n; n   std::cin >> n; n   std::vector<std::vector<std::pair<int, int>>> positions(n); n   const auto reader = [n, &positions](int num) { n     for (int i = 0; i < n; ++i) { n       int a; n       std::cin >> a; n       positions[a - 1].emplace_back(num, i); n     } n   }; n   const auto neg = [n](int val) { return (val + n) % (2 * n); }; n   reader(0); n   reader(1); n   for (int i = 0; i < n; ++i) { n     if (positions[i].size() != 2) { n       std::cout << -1 <<   n ; n       return; n     } n   } n   std::vector<std::vector<int>> g(2 * n); n   for (int i = 0; i < n; ++i) { n     auto pa = positions[i][0]; n     auto pb = positions[i][1]; n     if (pa.second == pb.second) { n       continue; n     } n     int a = pa.second; n     int b = pb.second; n     if (pa.first == pb.first) { n       a = neg(a); n     } n     g[a].push_back(b); n     g[b].push_back(a); n     g[neg(a)].push_back(neg(b)); n     g[neg(b)].push_back(neg(a)); n   } n   int comp_num = 0; n   std::vector<std::vector<int>> comps; n   std::vector<int> rev_comps(2 * n, -1); n   for (int i = 0; i < 2 * n; ++i) { n     if (rev_comps[i] == -1) { n       std::vector<int> cur_comp; n       build_comp(i, comp_num++, cur_comp, rev_comps, g); n       comps.emplace_back(std::move(cur_comp)); n     } n   } n   for (int i = 0; i < n; ++i) { n     if (rev_comps[i] == rev_comps[neg(i)]) { n       std::cout << -1 <<   n ; n       return; n     } n   } n   std::set<int> res; n   std::set<size_t> skipping_comps; n   for (size_t i = 0; i < comps.size(); ++i) { n     if (skipping_comps.find(i) != skipping_comps.end()) { n       continue; n     } n     const auto &line = comps[i]; n     int positive = 0; n     for (int v : line) { n       if (v < n) { n         ++positive; n       } n     } n     int negative = line.size() - positive; n     if (positive <= negative) { n       for (int v : line) { n         if (v < n) { n           res.insert(v); n         } n       } n     } n     if (positive == negative) { n       skipping_comps.insert(rev_comps[neg(line[0])]); n     } n   } n   std::cout << res.size() <<   n ; n   for (int v : res) { n     std::cout << v + 1 <<    ; n   } n   std::cout <<   n ; n } n int main() { n   int t; n   std::cin >> t; n   for (; t; --t) { n     solve(); n   } n   return 0; n } n


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


#include <bits/stdc++.h> n using namespace std; n int n, k1, k2, u[1001], q[1001], start, x, y; n vector<int> vec[1001]; n int a, b; n int f(int v, int pr) { n   if (u[v] == 1) { n     return v; n   } n   int yy = 0; n   for (int i = 0; i < vec[v].size(); i++) { n     if (vec[v][i] != pr) { n       yy = max(yy, f(vec[v][i], v)); n     } n   } n   return yy; n } n pair<int, int> get(int v, int pr) { n   pair<int, int> ans = make_pair(0, 0); n   if (v == y) { n     ans.first = 1; n     return ans; n   } n   if (u[v] == 1) { n     ans.second = v; n   } n   pair<int, int> yy = make_pair(0, 0); n   for (int i = 0; i < vec[v].size(); i++) { n     if (vec[v][i] != pr) { n       yy = max(yy, get(vec[v][i], v)); n     } n   } n   if (yy.first != 0) { n     ans.first = 1; n   } n   if (yy.first != 0 && yy.second != 0) { n     ans = yy; n   } n   return ans; n } n void solve() { n   for (int i = 0; i < 1001; i++) { n     vec[i].clear(); n     u[i] = 0; n     q[i] = 0; n   } n   cin >> n; n   for (int i = 1; i < n; i++) { n     scanf( %d%d , &a, &b); n     vec[a].push_back(b); n     vec[b].push_back(a); n   } n   cin >> k1; n   for (int i = 0; i < k1; i++) { n     scanf( %d , &x); n     u[x] = 1; n   } n   cin >> k2; n   for (int i = 0; i < k2; i++) { n     scanf( %d , &x); n     q[x] = 1; n   } n   start = f(1, -1); n   cout <<  A   << start << endl; n   fflush(stdout); n   cin >> y; n   if (q[y] == 1) { n     cout <<  C   << start << endl; n     fflush(stdout); n     return; n   } else { n     cout <<  B   << x << endl; n     fflush(stdout); n     cin >> y; n     if (u[y] == 1) { n       cout <<  C   << y << endl; n       fflush(stdout); n       return; n     } else { n       pair<int, int> ppp = get(1, -1); n       if (ppp.second == 0) { n         cout <<  C -1  << endl; n         fflush(stdout); n         return; n       } n       start = ppp.second; n       cout <<  A   << start << endl; n       fflush(stdout); n       cin >> y; n       if (q[y] == 1) { n         cout <<  C   << start << endl; n         fflush(stdout); n         return; n       } else { n         cout <<  C -1  << endl; n         fflush(stdout); n         return; n       } n     } n   } n } n int main() { n   int test; n   cin >> test; n   while (test--) { n     solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T> n bool chmax(T& a, const T& b) { n   return a < b ? (a = b, true) : false; n } n template <typename T> n bool chmin(T& a, const T& b) { n   return b < a ? (a = b, true) : false; n } n int N; n vector<int> A; n list<int> edges[100001]; n long long dp[100001][2]; n long long dfs(int curr, int prev, int k) { n   vector<long long> v[2]; n   long long acc[2] = {}; n   long long res = 0; n   for (const auto& it : edges[curr]) n     if (it != prev) { n       res += dfs(it, curr, k); n       for (int i = (int)(0); i < (int)(2); i++) { n         v[i].push_back(dp[it][i]); n         acc[i] += dp[it][i]; n       } n     } n   long long x = 0; n   for (int i = (int)(0); i < (int)(v[0].size()); i++) { n     for (int j = (int)(0); j < (int)(2); j++) { n       if (j ^ (A[curr] >> k & 1)) { n         x += (v[j][i] * (acc[0] - v[0][i])) << k; n       } else { n         x += (v[j][i] * (acc[1] - v[1][i])) << k; n       } n     } n   } n   res += x / 2; n   if (A[curr] >> k & 1) { n     dp[curr][0] += acc[1]; n     dp[curr][1] += acc[0]; n     dp[curr][1]++; n   } else { n     dp[curr][0] += acc[0]; n     dp[curr][1] += acc[1]; n     dp[curr][0]++; n   } n   return res; n } n int main() { n   scanf( %d , &N); n   A.assign(N + 1, 0); n   for (int i = (int)(1); i < (int)(N + 1); i++) { n     scanf( %d , &A[i]); n   } n   for (int i = (int)(0); i < (int)(N - 1); i++) { n     int u, v; n     scanf( %d%d , &u, &v); n     edges[u].push_back(v); n     edges[v].push_back(u); n   } n   long long ans = 0; n   for (int i = (int)(0); i < (int)(20); i++) { n     memset(dp, 0, sizeof(dp)); n     ans += dfs(1, -1, i); n     for (int j = (int)(1); j < (int)(N + 1); j++) { n       ans += dp[j][1] << i; n     } n   } n   printf( %lld n , ans); n   return 0; n } n


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


#include <bits/stdc++.h> n using namespace std; n const int Maxn = 100 * 1000 + 5; n int n; n vector<int> arr[Maxn]; n int main() { n   scanf( %d , &n); n   int x, k; n   for (int i = 0; i < n; i++) { n     scanf( %d%d , &x, &k); n     arr[k - 1].push_back(x); n   } n   for (int i = 0; i < Maxn; i++) { n     int num = -1; n     for (int j = 0; j < (int)arr[i].size(); j++) { n       if (arr[i][j] > num + 1) { n         printf( NO n ); n         return 0; n       } n       if (arr[i][j] == num + 1) num++; n     } n   } n   printf( YES n ); n   return 0; n } n


#include<bits/stdc++.h> n using namespace std; n using ll = long long; n int main() { n  ios::sync_with_stdio(false), cin.tie(nullptr); n  int _; n  for (cin >> _; _--;) { n   int n; n   cin >> n; n   int a[n + 1]; n   int cnt = 0; n   for (int i = 1; i <= n; ++i) { n    cin >> a[i]; n    if (a[i] != i)cnt++; n   } n   if (cnt == 0) cout <<  0 n ; n   else if (a[1] == 1 || a[n] == n)cout <<  1 n ; n   else { n    if (a[1] == n and a[n] == 1)cout <<  3 n ; n    else cout <<  2 n ; n   } n  } n  return 0; n }


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 1123; n const int MAXK = 312345; n struct S2DSum { n   long long x[MAXN][MAXN]; n   void integrate(int n, int m) { n     for (int j = 0; j < m; j++) { n       for (int i = 1; i < n; i++) { n         x[i][j] += x[i - 1][j]; n       } n     } n     for (int i = 0; i < n; i++) { n       for (int j = 1; j < m; j++) { n         x[i][j] += x[i][j - 1]; n       } n     } n   } n   void add(int a, int b, int c, int d, long long e) { n     x[a][b] += e; n     x[a][d + 1] -= e; n     x[c + 1][b] -= e; n     x[c + 1][d + 1] += e; n   } n   long long sum(int a, int b, int c, int d) { n     long long ans = x[c][d]; n     if (a) ans -= x[a - 1][d]; n     if (b) ans -= x[c][b - 1]; n     if (a and b) ans += x[a - 1][b - 1]; n     return ans; n   } n }; n int n, m, k; n int pic[MAXN][MAXN]; n S2DSum orig; n S2DSum cost[26]; n S2DSum dc; n int a[MAXK], b[MAXK], c[MAXK], d[MAXK], e[MAXK]; n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   cin >> n >> m >> k; n   for (int i = 0; i < n; i++) { n     string s; n     cin >> s; n     for (int j = 0; j < m; j++) pic[i][j] = s[j] -  a ; n   } n   for (int i = 0; i < k; i++) { n     char ce; n     cin >> a[i] >> b[i] >> c[i] >> d[i] >> ce; n     e[i] = ce -  a ; n     a[i]--; n     b[i]--; n     c[i]--; n     d[i]--; n     orig.add(0, 0, n - 1, m - 1, 1); n     orig.add(a[i], b[i], c[i], d[i], -1); n     for (int j = 0; j < 26; j++) { n       cost[j].add(a[i], b[i], c[i], d[i], abs(e[i] - j)); n     } n   } n   orig.integrate(n, m); n   orig.integrate(n, m); n   for (int cl = 0; cl < 26; cl++) { n     for (int i = 0; i < n; i++) { n       for (int j = 0; j < m; j++) { n         long long hm = orig.sum(i, j, i, j); n         cost[cl].add(i, j, i, j, abs(cl - pic[i][j]) * hm); n       } n     } n     cost[cl].integrate(n, m); n     cost[cl].integrate(n, m); n   } n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < m; j++) { n       dc.add(i, j, i, j, cost[pic[i][j]].sum(i, j, i, j)); n     } n   } n   dc.integrate(n, m); n   dc.integrate(n, m); n   long long ans = numeric_limits<long long>::max(); n   long long ini = dc.sum(0, 0, n - 1, m - 1); n   for (int i = 0; i < k; i++) { n     long long sub = ini; n     sub -= dc.sum(a[i], b[i], c[i], d[i]); n     sub += cost[e[i]].sum(a[i], b[i], c[i], d[i]); n     ans = min(ans, sub); n   } n   cout << ans << endl; n } n


#include <bits/stdc++.h> n using namespace std; n struct node { n   int x, y, msk; n   string s; n   node(int x1, int x2, int x3, string x4) { x = x1, y = x2, msk = x3, s = x4; } n   bool operator<(const node &b) const { n     if (s.size() != b.s.size()) return s.size() > b.s.size(); n     return s > b.s; n   } n }; n int gx[5] = {0, 1, 0, -1, 0}, gy[5] = {0, 0, 1, 0, -1}; n char s[51][51]; n set<int> st[51][51]; n int las[51][51]; n int count(int x) { n   int ans = 0; n   while (x) ans += x & 1, x >>= 1; n   return ans; n } n signed main() { n   int x = 0, y = 0; n   int n, m, k; n   scanf( %d%d%d , &n, &m, &k); n   for (int i = 1; i <= n; i++) cin >> s[i] + 1; n   for (int i = 1; i <= n; i++) n     for (int j = 1; j <= m; j++) n       if (s[i][j] ==  S ) x = i, y = j; n   priority_queue<node> q; n   q.push(node(x, y, 0,   )); n   while (!q.empty()) { n     node u = q.top(); n     q.pop(); n     for (int i = 1; i <= 4; i++) { n       int tox = u.x + gx[i]; n       int toy = u.y + gy[i]; n       if (tox > n || toy > m || tox <= 0 || toy <= 0) continue; n       if (s[tox][toy] ==  T ) { n         cout << u.s; n         return 0; n       } n       int mk = u.msk | (1 << (s[tox][toy] -  a )); n       if (count(mk) > k) continue; n       if (las[tox][toy] && ((las[tox][toy] & mk) == las[tox][toy])) continue; n       las[tox][toy] = mk; n       if (st[tox][toy].count(mk)) continue; n       st[tox][toy].insert(mk); n       string tmp = u.s; n       q.push(node(tox, toy, mk, u.s + s[tox][toy])); n     } n   } n   printf( -1 ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct rest { n   int s, f; n }; n rest lis[1000500]; n bool cmp(rest &a, rest &b) { return a.f < b.f; } n int main() { n   int n; n   cin >> n; n   for (int i = 1; i <= n; i++) { n     scanf( %d %d , &lis[i].s, &lis[i].f); n   } n   sort(lis + 1, lis + n + 1, cmp); n   int now = 0, ans = 0; n   for (int i = 1; i <= n; i++) { n     if (lis[i].s > now) { n       ans++; n       now = lis[i].f; n     } n   } n   cout << ans << endl; n } n


#include <bits/stdc++.h> n char s[2000000], t[2000000]; n int n, i, YY = 0, YN = 0, NY = 0, NN = 0, Yaro = 0, Andrew = 0; n int main() { n   scanf( %d n , &n); n   for (i = 0; i < 2 * n; i++) scanf( %c , &s[i]); n   scanf(  n ); n   for (i = 0; i < 2 * n; i++) scanf( %c , &t[i]); n   for (i = 0; i < 2 * n; i++) { n     if (s[i] ==  1  && t[i] ==  1 ) { n       YY++; n     } else if (s[i] ==  1  && t[i] ==  0 ) { n       YN++; n     } else if (s[i] ==  0  && t[i] ==  1 ) { n       NY++; n     } else if (s[i] ==  1  && t[i] ==  1 ) { n       NN++; n     } n   } n   for (i = 0; i < n; i++) { n     if (YY > 0) { n       YY--; n       Yaro++; n     } else if (YN > 0) { n       YN--; n       Yaro++; n     } else if (NY > 0) { n       NY--; n     } else { n       NN--; n     } n     if (YY > 0) { n       YY--; n       Andrew++; n     } else if (NY > 0) { n       NY--; n       Andrew++; n     } else if (YN > 0) { n       YN--; n     } else { n       NN--; n     } n   } n   if (Yaro == Andrew) { n     printf( Draw n ); n   } else if (Yaro > Andrew) { n     printf( First n ); n   } else if (Yaro < Andrew) { n     printf( Second n ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n clock_t T; n constexpr auto EPS = 1e-7; n const int mod = (int)1e9 + 7; n const double pi = acos(-1); n int dx[] = {0, 0, 1, -1, 1, 1, -1, -1}; n int dy[] = {1, -1, 0, 0, 1, -1, 1, -1}; n long long gcd(long long a, long long b) { return (a) ? gcd(b % a, a) : b; } n long long lcm(long long a, long long b) { return (a * b) / gcd(a, b); } n void COME_BACK() { n   std::ios_base::sync_with_stdio(0); n   cin.tie(NULL); n   cout.tie(NULL); n } n int main() { n   COME_BACK(); n   int n, x; n   cin >> n >> x; n   if (n == 2 && !x) n     return cout <<  NO  n                 <<   n , n            0; n   if (n == 1) n     return cout <<  YES  n                 <<   n  n                 << x, n            0; n   if (n == 2) n     return cout <<  YES  n                 <<   n  n                 << 0 <<     << x, n            0; n   cout <<  YES  n        <<   n ; n   for (int i = 0; i < n - 3; i++) cout << i <<    , x ^= i; n   cout << ((1 << 18) ^ x) <<     << (1 << 19) <<     << ((1 << 18) ^ (1 << 19)) n        <<   n ; n } n


#include <bits/stdc++.h> n #pragma GCC optimize( Ofast ) n #pragma GCC optimize( unroll-loops ) n #pragma GCC target( avx,avx2,fma ) n using namespace std; n using ll = long long; n using ld = long double; n const ld eps = (ld)1e-11; n const long long INF = 4e18; n long long n, q; n vector<long long> a, b; n vector<array<long long, 4>> qs; n long long sz = 400; n struct block { n   vector<array<long long, 3>> ln, chln; n   vector<ld> dot; n   vector<long long> ans; n   block() { ans.resize(q); } n   inline ld intr(array<long long, 3> ln1, array<long long, 3> ln2) { n     if (ln1[0] == ln2[0]) n       return (ln1[1] > ln2[1] ? -INF : INF); n     else n       return ld(ln2[1] - ln1[1]) / ld(ln1[0] - ln2[0]); n   } n   void build() { n     sort(ln.begin(), ln.end()); n     dot.clear(); n     chln.clear(); n     for (long long i = 0; i < ln.size(); ++i) { n       while (dot.size() && intr(ln[i], chln.back()) - eps < dot.back()) { n         chln.pop_back(); n         dot.pop_back(); n       } n       if (chln.size()) dot.push_back(intr(ln[i], chln.back())); n       chln.push_back(ln[i]); n     } n     long long pos = 0; n     for (long long i = 0; i < q; ++i) { n       while (pos < dot.size() && dot[pos] < qs[i][0]) ++pos; n       ans[i] = chln[pos][2]; n     } n   } n }; n vector<block> d; n void build() { n   for (long long i = 0; i < n; ++i) { n     long long cur = i / sz; n     if (cur >= d.size()) d.push_back({}); n     d[cur].ln.push_back({b[i], a[i], i}); n   } n   for (auto &x : d) x.build(); n } n long long qry(long long l, long long r, long long num) { n   long long ans = -1, l0 = l % sz; n   while (l < r) { n     if (!l0 && l + sz <= r) { n       long long pos = l / sz, cur = d[pos].ans[num]; n       if (ans == -1 || n           a[cur] + qs[num][0] * b[cur] > a[ans] + qs[num][0] * b[ans]) n         ans = cur; n       l += sz; n     } else { n       if (ans == -1 || a[l] + qs[num][0] * b[l] > a[ans] + qs[num][0] * b[ans]) n         ans = l; n       ++l; n       l0 = (l0 + 1) % sz; n     } n   } n   return ans; n } n void solve() { n   cin >> n >> q; n   a.resize(n); n   b = a; n   for (long long i = 0; i < n; ++i) { n     cin >> a[i] >> b[i]; n   } n   qs.resize(q); n   for (long long i = 0; i < q; ++i) { n     cin >> qs[i][1] >> qs[i][2] >> qs[i][0]; n     --qs[i][1]; n     qs[i][3] = i; n   } n   sort(qs.begin(), qs.end()); n   build(); n   vector<long long> ans(q); n   for (long long i = 0; i < q; ++i) { n     ans[qs[i][3]] = qry(qs[i][1], qs[i][2], i); n   } n   for (auto x : ans) cout << x + 1 <<   n ; n } n signed main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   long long test_case = 1; n   for (long long i = 0; i < test_case; ++i) { n     solve(); n     cout <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int mod(int a, int b) { n   if (a > b) n     return a - b; n   else n     return b - a; n } n int main() { n   int n, m, i, j, l = 0; n   cin >> n >> m; n   int a[n]; n   for (i = 0; i < n; i++) cin >> a[i]; n   for (i = 0; i < n; i++) { n     for (j = 0; j < n; j++) { n       if (i != j) { n         if (mod(a[i], a[j]) <= m) l++; n       } n     } n   } n   cout << l; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 41; n int n, m, q, cs; n char a[N][N]; n int s[N][N]; n int dp[N][N][N][N]; n int main() { n   memset(dp, 0, sizeof(int) * N * N * N * N); n   cin >> n >> m >> q; n   for (int i = 0; i < n; ++i) { n     cin.ignore(); n     for (int j = 0; j < m; ++j) { n       cin >> a[i + 1][j + 1]; n     } n   } n   for (int i = 1; i <= n; ++i) { n     s[i][0] = 0; n     for (int j = 1; j <= m; ++j) { n       s[i][j] = (a[i][j] ==  0  ? s[i][j - 1] + 1 : 0); n     } n   } n   for (int i = 1; i <= n; ++i) { n     for (int j = 1; j <= m; ++j) { n       for (int k = i; k <= n; ++k) { n         for (int l = j; l <= m; ++l) { n           dp[i][j][k][l] = n               dp[i][j][k - 1][l] + dp[i][j][k][l - 1] - dp[i][j][k - 1][l - 1]; n           int mn = l - j + 1; n           for (int i1 = k; i1 >= i; --i1) { n             mn = min(mn, s[i1][l]); n             dp[i][j][k][l] += mn; n           } n           if (dp[i][j][k][l] < 0) n             return cout << i <<     << j <<     << k <<     << l, 0; n         } n       } n     } n   } n   for (int i = 0; i < q; ++i) { n     int a, b, c, d; n     cin >> a >> b >> c >> d; n     cout << dp[a][b][c][d] << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long int n; n   cin >> n; n   for (long long int ij = 0; ij < n; ++ij) { n     long long int a, b; n     cin >> a >> b; n     long long int p = llabs(a - b), s = 0, i = 0, d = 0; n     while (s < p || d == 0) { n       ++i; n       s += i; n       if (s >= p) { n         if (s % 2 == p % 2) { n           ++d; n         } n       } n     } n     if (a == b) n       cout << 0 << endl; n     else n       cout << i << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 2e5 + 10; n int n, k, q, l, r; n int b[N], sum[N]; n int main() { n   scanf( %d%d%d , &n, &k, &q); n   for (int i = 1; i <= n; ++i) { n     scanf( %d%d , &l, &r); n     b[l]++; n     b[r + 1]--; n   } n   int tot = 0; n   for (int i = 1; i <= 200000; ++i) { n     tot += b[i]; n     sum[i] = sum[i - 1]; n     if (tot >= k) sum[i]++; n   } n   for (int i = 1; i <= q; ++i) { n     scanf( %d%d , &l, &r); n     printf( %d n , sum[r] - sum[l - 1]); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long n, m, a, b; n   cin >> n >> m >> a >> b; n   long long cost = 0; n   if (m > n) m = n; n   if (m * a <= b) n     cost = n * a; n   else { n     cost = (n / m) * b; n     n = n % m; n     if (n > 0) { n       if ((n)*a <= b) n         cost += n * a; n       else n         cost += b; n     } n   } n   cout << cost; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long inf = 1e9 + 7; n long long mod = 1e9 + 7; n using namespace std; n bool comp(pair<pair<long long, long long>, long long> a, n           pair<pair<long long, long long>, long long> b) { n   if (a.first.first != b.first.first) return a.first.first < b.first.first; n   return a.first.second < b.first.second; n } n bool isOverlap(pair<long long, long long> a, pair<long long, long long> b) { n   if (a.second < b.first or a.first > b.second) return false; n   return true; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   long long T, N, i, j, k, L, l, r, m, n; n   long long K, P; n   long long M; n   cin >> T; n   while (T--) { n     deque<pair<pair<long long, long long>, long long> > v; n     cin >> N; n     vector<long long> ret(N, 0); n     for (i = 0; i < N; i++) { n       cin >> l >> r; n       v.push_back(make_pair(make_pair(l, r), i)); n     } n     sort(v.begin(), v.end(), comp); n     vector<long long> ans; n     while (!v.empty()) { n       pair<long long, long long> inv = v.front().first; n       ans.push_back(v.front().second); n       v.pop_front(); n       while (!v.empty() and isOverlap(inv, v.front().first)) { n         ans.push_back(v.front().second); n         inv.first = n             (((inv.first) < (v.front().first.first)) ? (inv.first) n                                                      : (v.front().first.first)); n         inv.second = (((inv.second) > (v.front().first.second)) n                           ? (inv.second) n                           : (v.front().first.second)); n         v.pop_front(); n       } n       break; n     } n     if (v.empty()) n       cout << -1 << endl; n     else { n       for (i = 0; i < ans.size(); i++) ret[ans[i]] = 1; n       while (!v.empty()) { n         ret[v.front().second] = 2; n         v.pop_front(); n       } n       for (i = 0; i < N; i++) cout << ret[i] <<    ; n       cout << endl; n     } n     ret.clear(); n   } n } n


#include <bits/stdc++.h> n using namespace std; n template <class T> n bool ckmax(T& x, T y) { n   return x < y ? x = y, 1 : 0; n } n template <class T> n bool ckmin(T& x, T y) { n   return x > y ? x = y, 1 : 0; n } n inline long long read() { n   long long x = 0, f = 1; n   char ch = getchar(); n   while (!isdigit(ch)) { n     if (ch ==  - ) f = 0; n     ch = getchar(); n   } n   while (isdigit(ch)) x = x * 10 + ch -  0 , ch = getchar(); n   return f ? x : -x; n } n const long long N = 200005; n long long n, a[N], b[N], now, tot; n bool isrt(long long x) { n   if (x > 1e13) return 0; n   long long l = max(1ll, (long long)(sqrt(x) - 3)), r = sqrt(x) + 3; n   for (long long i = l, iend = r; i <= iend; ++i) n     if (x % i == 0) return 1; n   return 0; n } n signed main() { n   n = read() / 2; n   for (long long i = 1, iend = n; i <= iend; ++i) a[i] = read(); n   for (long long i = 1; i <= n; ++i) { n     bool flg = 0; n     long long res; n     for (long long j = sqrt(a[i]); j >= 1; --j) { n       if (a[i] % j) continue; n       long long X = a[i] / j, Y = j; n       if ((X - Y) % 2) continue; n       res = (X - Y) / 2, res *= res; n       if (res <= now) continue; n       flg = 1; n       break; n     } n     if (now >= res || !flg) return puts( No ), 0; n     b[++tot] = res - now, b[++tot] = a[i]; n     now = a[i] + res; n   } n   now = 0; n   for (long long i = 1, iend = n << 1; i <= iend; ++i) { n     now += b[i]; n     if (!isrt(now)) return puts( No ), 0; n   } n   puts( Yes ); n   for (long long i = 1, iend = n << 1; i <= iend; ++i) printf( %lld  , b[i]); n   puts(  ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e5 + 20; n long long a[N]; n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   long long n, sum = 0, mx = 0; n   cin >> n; n   for (int i = 0; i < n; i++) { n     cin >> a[i]; n     mx = max(mx, a[i]); n     sum += a[i]; n   } n   cout << max((long long)((sum + n - 2) / (n - 1)), mx) <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n inline long long read() { n   long long x = 0, f = 1; n   char ch = getchar(); n   while (ch <  0  || ch >  9 ) { n     if (ch ==  - ) f = -1; n     ch = getchar(); n   } n   while (ch >=  0  && ch <=  9 ) { n     x = (x << 1) + (x << 3) + (ch ^ 48); n     ch = getchar(); n   } n   return x * f; n } n const int N = 100008; n int n; n int x, y; n int nume, head[N]; n struct edge { n   int to, nxt; n } e[N << 1]; n inline void add_edge(int x, int y) { n   e[++nume] = (edge){y, head[x]}; n   head[x] = nume; n } n int ans[N]; n int tmp; n int dp[N]; n inline void dfs(int x, int fa, int goal) { n   int mx1, mx2; n   mx1 = mx2 = 0; n   for (int i = head[x]; i; i = e[i].nxt) { n     int to = e[i].to; n     if (to != fa) { n       dfs(to, x, goal); n       if (dp[to] > mx1) { n         mx2 = mx1; n         mx1 = dp[to]; n       } else if (dp[to] > mx2) { n         mx2 = dp[to]; n       } n     } n   } n   if (mx1 + mx2 + 1 >= goal) { n     dp[x] = 0; n     ++tmp; n   } else { n     dp[x] = mx1 + 1; n   } n } n inline void solve(int l, int r, int ansl, int ansr) { n   if (l > r) return; n   if (ansl == ansr) { n     for (int i = l; i <= r; ++i) { n       ans[i] = ansl; n     } n     return; n   } n   int mid = (l + r) >> 1; n   tmp = 0; n   dfs(1, 0, mid); n   ans[mid] = tmp; n   solve(l, mid - 1, ansl, ans[mid]); n   solve(mid + 1, r, ans[mid], ansr); n } n int main() { n   n = read(); n   for (int i = 1; i < n; ++i) { n     x = read(); n     y = read(); n     add_edge(x, y); n     add_edge(y, x); n   } n   solve(1, n, n, 0); n   for (int i = 1; i <= n; ++i) { n     printf( %d  , ans[i]); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename A, typename B> n string to_string(pair<A, B> p); n string to_string(const string &s) { return     + s +    ; } n string to_string(const char *s) { return to_string((string)s); } n string to_string(bool b) { return (b ?  true  :  false ); } n template <size_t N> n string to_string(bitset<N> v) { n   string res =   ; n   for (size_t i = 0; i < N; i++) res += static_cast<char>( 0  + v[i]); n   return res; n } n template <typename A> n string to_string(A v) { n   bool first = true; n   string res =  { ; n   for (const auto &x : v) { n     if (!first) res +=  ,  ; n     first = false; n     res += to_string(x); n   } n   res +=  } ; n   return res; n } n template <typename A, typename B> n string to_string(pair<A, B> p) { n   return  (  + to_string(p.first) +  ,   + to_string(p.second) +  ) ; n } n void debug_out() { cerr << endl; } n template <typename Head, typename... Tail> n void debug_out(Head H, Tail... T) { n   cerr <<     << to_string(H); n   debug_out(T...); n } n struct SegmentTree { n   int n; n   vector<int> cnt[2]; n   explicit SegmentTree(int n) : n(n) { n     cnt[0].assign(n << 2 | 1, 0); n     cnt[1].assign(n << 2 | 1, 0); n   } n   void Update(int node, int l, int r, int x, int zero, int one) { n     if (x < l || x > r) return; n     if (l == r) { n       cnt[0][node] += zero; n       cnt[1][node] += one; n       return; n     } n     int m = l + r >> 1; n     Update(node << 1, l, m, x, zero, one); n     Update(node << 1 | 1, m + 1, r, x, zero, one); n     cnt[0][node] = cnt[0][node << 1] + cnt[0][node << 1 | 1]; n     cnt[1][node] = cnt[1][node << 1] + cnt[1][node << 1 | 1]; n   } n   int GetMin(int node, int l, int r, int zero, int one) { n     if (l == r) { n       return min(zero + cnt[0][node], one); n     } n     int m = l + r >> 1; n     int res = 0; n     auto cur_zero = zero + cnt[0][node << 1]; n     auto cur_one = one + cnt[1][node << 1 | 1]; n     res = max(res, min(cur_zero, cur_one)); n     if (cur_zero > cur_one) { n       res = max(res, GetMin(node << 1, l, m, zero, cur_one)); n     } else { n       res = max(res, GetMin(node << 1 | 1, m + 1, r, cur_zero, one)); n     } n     return res; n   } n }; n struct Point { n   int x, y, c; n }; n string to_string(Point u) { n   return  {  + to_string(u.x) +  ,   + to_string(u.y) +  ,  + to_string(u.c) + n           } ; n } n int32_t main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   int n; n   cin >> n; n   vector<Point> a(n); n   vector<int> vals; n   for (auto &u : a) { n     cin >> u.x >> u.y >> u.c; n     vals.push_back(u.x); n     vals.push_back(u.y); n     u.c--; n   } n   auto Normalize = [&](vector<int> &a) { n     sort(a.begin(), a.end()); n     a.resize(unique(a.begin(), a.end()) - a.begin()); n   }; n   Normalize(vals); n   auto m = vals.size(); n   for (auto &u : a) { n     u.x = lower_bound(vals.begin(), vals.end(), u.x) - vals.begin(); n     u.y = lower_bound(vals.begin(), vals.end(), u.y) - vals.begin(); n   } n   int res = 0; n   vector<int> id(3); n   iota(id.begin(), id.end(), 0); n   auto Solve = [&](vector<Point> b) { n     sort(b.begin(), b.end(), n          [&](const Point &u, const Point &v) { return u.x < v.x; }); n     int num = n / 3; n     int cur = 0; n     SegmentTree t_x(m); n     SegmentTree t_y(m); n     for (int i = 0; i < m; i++) { n       while (cur < n && b[cur].x <= i) { n         if (b[cur].c == 0) { n           num--; n         } else if (b[cur].c == 1) { n           t_x.Update(1, 0, m - 1, b[cur].x, 1, 0); n           t_y.Update(1, 0, m - 1, b[cur].y, 1, 0); n         } else if (b[cur].c == 2) { n           t_x.Update(1, 0, m - 1, b[cur].x, 0, 1); n           t_y.Update(1, 0, m - 1, b[cur].y, 0, 1); n         } else { n           exit(1); n         } n         cur++; n       } n       42; n       42; n       42; n       res = max(res, min(num, t_x.GetMin(1, 0, m - 1, 0, 0))); n       res = max(res, min(num, t_y.GetMin(1, 0, m - 1, 0, 0))); n     } n   }; n   while (1) { n     for (int x = 0; x < 2; x++) { n       for (int z = 0; z < 2; z++) { n         auto b = a; n         for (auto &p : b) { n           if (z) swap(p.x, p.y); n           if (x) p.x = m - p.x - 1; n           p.c = id[p.c]; n         } n         Solve(b); n       } n     } n     if (!next_permutation(id.begin(), id.end())) break; n   } n   cout << res * 3 <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int const maxn = 1e5 + 5; n int ans[maxn], used[maxn]; n inline int oks(vector<int> a) { n   for (int i = 0; i < (int)a.size(); ++i) n     if (a[i] == i + 1 || (a[i] & (i + 1)) == 0) return 0; n   return 1; n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   int n, cnt = 1, N; n   cin >> n; n   N = n; n   if (n % 2 == 1) { n     cout <<  NO  <<   n ; n   } else { n     cout <<  YES  <<   n ; n     while (n > 0) { n       int x = 1; n       while (x * 2 <= n) x *= 2; n       for (int w = x; w <= n; ++w) { n         ans[w] = x - (w - x) - 1; n         ans[x - (w - x) - 1] = w; n       } n       n -= 2 * (n - x + 1); n     } n     for (int i = 1; i <= N; ++i) cout << ans[i] <<    ; n     cout <<   n ; n   } n   n = N; n   if (n <= 5) { n     cout <<  NO  <<   n ; n     exit(0); n   } n   while (n > 0) { n     int x = 1; n     while (x * 2 <= n) x *= 2; n     if (x == n) { n       cout <<  NO  <<   n ; n       exit(0); n     } n     if (n == 6) { n       ans[1] = 3, ans[2] = 6, ans[3] = 2, ans[4] = 5, ans[5] = 1, ans[6] = 4; n       n = 0; n       break; n     } n     if ((n - x + 1) % 2 == 1) { n       int R = n, cur = (n - x) / 2 + 1; n       for (int i = x; cur > 0; ++i, cur--) { n         if (i == x + 1) continue; n         ans[i] = R; n         ans[R] = i; n         R--; n       } n       ans[x + 1] = x - 1; n       ans[x - 1] = x + 1; n       n = x - 2; n     } else { n       while (n > 7) { n         ans[n] = n - 1; n         ans[n - 1] = n; n         n -= 2; n       } n       ans[1] = 3, ans[2] = 6, ans[3] = 1, ans[4] = 5, ans[5] = 4, ans[6] = 7, n       ans[7] = 2; n       n = 0; n     } n   } n   cout <<  YES  <<   n ; n   for (int i = 1; i <= N; ++i) cout << ans[i] <<    ; n   cout <<   n ; n   return 0; n } n


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


#include <bits/stdc++.h> n using namespace std; n const int INF = 1000000000; n vector<int> hung(const vector<vector<int> > &A) { n   int n = A.size(); n   int m = A[0].size(); n   vector<vector<long long> > a(n + 1); n   a[0] = vector<long long>(m + 1); n   for (int i = 0; i < n; ++i) { n     a[i + 1].push_back(100LL * INF); n     for (int j = 0; j < m; ++j) { n       a[i + 1].push_back(A[i][j]); n     } n   } n   vector<long long> u(n + 1), v(m + 1); n   vector<int> p(m + 1), way(m + 1); n   for (int i = 1; i <= n; ++i) { n     p[0] = i; n     int j0 = 0; n     vector<long long> minv(m + 1, 100LL * INF); n     vector<char> used(m + 1, false); n     do { n       used[j0] = true; n       int i0 = p[j0], j1; n       long long delta = 100LL * INF; n       for (int j = 1; j <= m; ++j) n         if (!used[j]) { n           long long cur = a[i0][j] - u[i0] - v[j]; n           if (cur < minv[j]) minv[j] = cur, way[j] = j0; n           if (minv[j] < delta) delta = minv[j], j1 = j; n         } n       for (int j = 0; j <= m; ++j) n         if (used[j]) n           u[p[j]] += delta, v[j] -= delta; n         else n           minv[j] -= delta; n       j0 = j1; n     } while (p[j0] != 0); n     do { n       int j1 = way[j0]; n       p[j0] = p[j1]; n       j0 = j1; n     } while (j0); n   } n   vector<int> rv(n); n   for (int j = 1; j <= m; ++j) { n     rv[p[j] - 1] = j - 1; n   } n   return rv; n } n struct MinCostFlow { n   struct edge { n     int b, u, c, f; n     size_t back; n   }; n   int n, m; n   vector<vector<edge> > g; n   MinCostFlow(int n) : n(n), m(0), g(n) {} n   void add_edge(int a, int b, int u, int c) { n     ++m; n     edge r1 = {b, u, c, 0, g[b].size()}; n     edge r2 = {a, 0, -c, 0, g[a].size()}; n     g[a].push_back(r1); n     g[b].push_back(r2); n   } n   int flow(int s, int t, int k) { n     int flow = 0, cost = 0; n     while (flow < k) { n       vector<int> id(n, 0); n       vector<int> d(n, INF); n       vector<int> q(n); n       vector<int> p(n); n       vector<size_t> p_edge(n); n       int qh = 0, qt = 0; n       q[qt++] = s; n       d[s] = 0; n       while (qh != qt) { n         int v = q[qh++]; n         id[v] = 2; n         if (qh == n) qh = 0; n         for (size_t i = 0; i < g[v].size(); ++i) { n           edge &r = g[v][i]; n           if (r.f < r.u && d[v] + r.c < d[r.b]) { n             d[r.b] = d[v] + r.c; n             if (id[r.b] == 0) { n               q[qt++] = r.b; n               if (qt == n) qt = 0; n             } else if (id[r.b] == 2) { n               if (--qh == -1) qh = n - 1; n               q[qh] = r.b; n             } n             id[r.b] = 1; n             p[r.b] = v; n             p_edge[r.b] = i; n           } n         } n       } n       if (d[t] == INF) break; n       int addflow = k - flow; n       for (int v = t; v != s; v = p[v]) { n         int pv = p[v]; n         size_t pr = p_edge[v]; n         addflow = min(addflow, g[pv][pr].u - g[pv][pr].f); n       } n       for (int v = t; v != s; v = p[v]) { n         int pv = p[v]; n         size_t pr = p_edge[v], r = g[pv][pr].back; n         g[pv][pr].f += addflow; n         g[v][r].f -= addflow; n         cost += g[pv][pr].c * addflow; n       } n       flow += addflow; n     } n     if (flow < k) { n       return -1; n     } n     return cost; n   } n }; n const int MAXN = 30000; n struct Dinic { n   struct edge { n     int a, b, cap, flow; n   }; n   int n, s, t, d[MAXN], ptr[MAXN], q[MAXN]; n   vector<edge> e; n   vector<int> g[MAXN]; n   Dinic(int n) : n(n) {} n   void add_edge(int a, int b, int cap) { n     edge e1 = {a, b, cap, 0}; n     edge e2 = {b, a, 0, 0}; n     g[a].push_back((int)e.size()); n     e.push_back(e1); n     g[b].push_back((int)e.size()); n     e.push_back(e2); n   } n   bool bfs() { n     int qh = 0, qt = 0; n     q[qt++] = s; n     memset(d, -1, n * sizeof d[0]); n     d[s] = 0; n     while (qh < qt && d[t] == -1) { n       int v = q[qh++]; n       for (size_t i = 0; i < g[v].size(); ++i) { n         int id = g[v][i], to = e[id].b; n         if (d[to] == -1 && e[id].flow < e[id].cap) { n           q[qt++] = to; n           d[to] = d[v] + 1; n         } n       } n     } n     return d[t] != -1; n   } n   int dfs(int v, int flow) { n     if (!flow) return 0; n     if (v == t) return flow; n     for (; ptr[v] < (int)g[v].size(); ++ptr[v]) { n       int id = g[v][ptr[v]], to = e[id].b; n       if (d[to] != d[v] + 1) continue; n       int pushed = dfs(to, min(flow, e[id].cap - e[id].flow)); n       if (pushed) { n         e[id].flow += pushed; n         e[id ^ 1].flow -= pushed; n         return pushed; n       } n     } n     return 0; n   } n   int flow(int S, int T) { n     s = S; n     t = T; n     int flow = 0; n     for (;;) { n       if (!bfs()) break; n       memset(ptr, 0, n * sizeof ptr[0]); n       while (int pushed = dfs(s, INF)) flow += pushed; n     } n     return flow; n   } n }; n struct MaxFlow { n   struct Edge { n     int to, c, ne; n     Edge(int to, int c, int ne) : to(to), c(c), ne(ne) {} n   }; n   int n; n   vector<Edge> ed; n   vector<int> fi; n   MaxFlow(int n) : n(n), ed(), fi(n, -1) {} n   void add_edge(int a, int b, int c) { n     ed.push_back(Edge(b, c, fi[a])); n     fi[a] = (int)ed.size() - 1; n     ed.push_back(Edge(a, c, fi[b])); n     fi[b] = (int)ed.size() - 1; n   } n   vector<char> us; n   int dfs(int v, int tar, int cur, int minv = 1) { n     if (v == tar) return cur; n     if (us[v]) return 0; n     us[v] = 1; n     for (int i = fi[v]; i != -1; i = ed[i].ne) { n       if (ed[i].c < minv) { n         continue; n       } n       int tmp = dfs(ed[i].to, tar, min(cur, ed[i].c), minv); n       if (tmp > 0) { n         ed[i].c -= tmp; n         ed[i ^ 1].c += tmp; n         return tmp; n       } n     } n     return 0; n   } n   int flow(int s, int t) { n     int ans = 0, cur, mv = 1; n     for (int i = 0; i < 2; ++i) { n       do { n         us = vector<char>(n); n         cur = dfs(s, t, INF, mv); n         ans += cur; n       } while (cur); n     } n     return ans; n   } n }; n struct BoundedMinFlow { n   vector<int> a, b; n   vector<int> l, r; n   int n; n   BoundedMinFlow(int n) : n(n) {} n   void add_edge(int f, int t, int minc, int maxc) { n     a.push_back(f); n     b.push_back(t); n     l.push_back(minc); n     r.push_back(maxc); n   } n   bool check(int s, int t, int m) { n     MaxFlow f(n + 2); n     f.add_edge(t, s, m); n     int total = 0; n     for (int i = 0; i < (int)a.size(); ++i) { n       if (l[i] > 0) { n         f.add_edge(n, b[i], l[i]); n         f.add_edge(a[i], n + 1, l[i]); n         total += l[i]; n       } n       if (r[i] > l[i]) { n         f.add_edge(a[i], b[i], r[i] - l[i]); n       } n     } n     return (f.flow(n, n + 1) == total); n   } n   int flow(int s, int t) { n     int le = -1, ri = 10000; n     if (!check(s, t, ri)) { n       return -1; n     } n     while (le + 1 < ri) { n       int mi = (le + ri); n       if (check(s, t, mi)) { n         ri = mi; n       } else { n         le = mi; n       } n     } n     return ri; n   } n }; n int h, w; n string va[16] = { BY ,  BW ,  BR ,  BB ,  RY ,  RW ,  RR ,  WY ,  WW ,  YY }; n char vv[16][2]; n char st[16][16][4][2]; n int cnt[16]; n struct pos { n   int sx, sy, ex, ey; n   int id; n   pos() {} n   pos(int SX, int SY, int EX, int EY, int ID) n       : sx(SX), sy(SY), ex(EX), ey(EY), id(ID) {} n   bool operator<(const pos &p) const { return id < p.id; } n }; n map<vector<int>, vector<pos> > mp; n char t[16][16]; n int lp; n vector<pos> p(28); n vector<int> cur; n bool us[16][16]; n int dif[16][16]; n char ou[32][32]; n int gk[32][32][2]; n void prec() { n   for (int r = 0; r < h; ++r) { n     for (int c = 0; c + 1 < w; ++c) { n       int k = -1; n       for (int i = 0; i < 10; ++i) { n         if ((st[r][c][0][0] == vv[i][0] && st[r][c][0][1] == vv[i][1]) || n             (st[r][c][1][0] == vv[i][0] && st[r][c][1][1] == vv[i][1])) { n           k = i; n           break; n         } n       } n       gk[r][c][0] = k; n     } n   } n   for (int r = 0; r + 1 < h; ++r) { n     for (int c = 0; c < w; ++c) { n       int k = -1; n       for (int i = 0; i < 10; ++i) { n         if ((st[r][c][2][0] == vv[i][0] && st[r][c][2][1] == vv[i][1]) || n             (st[r][c][3][0] == vv[i][0] && st[r][c][3][1] == vv[i][1])) { n           k = i; n           break; n         } n       } n       gk[r][c][1] = k; n     } n   } n } n void go(int r, int c) { n   if (r >= h) { n     if (mp.find(cur) != mp.end()) { n       return; n     } n     mp[cur] = p; n     return; n   } n   if (c >= w) { n     go(r + 1, 0); n     return; n   } n   if (us[r][c]) { n     go(r, c + 1); n     return; n   } n   if (!us[r][c + 1] && c + 1 < w) { n     us[r][c] = us[r][c + 1] = 1; n     int k = gk[r][c][0]; n     cur[k]++; n     p[lp].sx = r; n     p[lp].sy = c; n     p[lp].ex = r; n     p[lp].ey = c + 1; n     p[lp].id = k; n     ++lp; n     go(r, c + 2); n     cur[k]--; n     --lp; n     us[r][c] = us[r][c + 1] = 0; n   } n   if (!us[r + 1][c] && r + 1 < h) { n     us[r][c] = us[r + 1][c] = 1; n     int k = gk[r][c][1]; n     cur[k]++; n     p[lp].sx = r; n     p[lp].sy = c; n     p[lp].ex = r + 1; n     p[lp].ey = c; n     p[lp].id = k; n     ++lp; n     go(r, c + 1); n     cur[k]--; n     --lp; n     us[r][c] = us[r + 1][c] = 0; n   } n } n int main() { n   h = 7; n   w = 8; n   for (int i = 0; i < h; ++i) { n     scanf( %s , t[i]); n   } n   for (int i = 0; i < 10; ++i) { n     scanf( %d , &cnt[i]); n   } n   cur = vector<int>(10, 0); n   memset(us, 0, sizeof(us)); n   for (int i = 0; i < h; ++i) { n     for (int j = 0; j + 1 < w; ++j) { n       st[i][j][0][0] = t[i][j]; n       st[i][j][0][1] = t[i][j + 1]; n       st[i][j][1][0] = t[i][j + 1]; n       st[i][j][1][1] = t[i][j]; n     } n   } n   for (int i = 0; i + 1 < h; ++i) { n     for (int j = 0; j < w; ++j) { n       st[i][j][2][0] = t[i][j]; n       st[i][j][2][1] = t[i + 1][j]; n       st[i][j][3][0] = t[i + 1][j]; n       st[i][j][3][1] = t[i][j]; n     } n   } n   for (int i = 0; i < 10; ++i) { n     vv[i][0] = va[i][0]; n     vv[i][1] = va[i][1]; n   } n   prec(); n   go(0, 0); n   memset(dif, 0, sizeof(dif)); n   for (int i = 0; i < 10; ++i) { n     for (int j = 0; j < 10; ++j) { n       dif[i][j] = min((va[i][0] != va[j][0]) + (va[i][1] != va[j][1]), n                       (va[i][0] != va[j][1]) + (va[i][1] != va[j][0])); n     } n   } n   int bsol = (1 << 20); n   MinCostFlow be(1); n   vector<pos> bp; n   for (map<vector<int>, vector<pos> >::iterator it = mp.begin(); it != mp.end(); n        ++it) { n     MinCostFlow f(22); n     for (int i = 0; i < 10; ++i) { n       f.add_edge(20, i, cnt[i], 0); n       f.add_edge(10 + i, 21, it->first[i], 0); n     } n     for (int i = 0; i < 10; ++i) { n       for (int j = 0; j < 10; ++j) { n         f.add_edge(i, j + 10, INF, dif[i][j]); n       } n     } n     int k = f.flow(20, 21, 28); n     if (k < bsol) { n       bsol = k; n       be = f; n       bp = it->second; n     } n   } n   printf( %d n , 56 - bsol); n   memset(ou,  . , sizeof(ou)); n   for (int i = 0; i < (int)bp.size(); ++i) { n     int nd = bp[i].id + 10; n     for (int j = 0; j < (int)be.g[nd].size(); ++j) { n       MinCostFlow::edge &ed = be.g[nd][j]; n       if (ed.b < 10 && ed.f < 0) { n         ++ed.f; n         int c1 = (va[ed.b][0] != t[bp[i].sx][bp[i].sy]) + n                  (va[ed.b][1] != t[bp[i].ex][bp[i].ey]); n         int c2 = (va[ed.b][1] != t[bp[i].sx][bp[i].sy]) + n                  (va[ed.b][0] != t[bp[i].ex][bp[i].ey]); n         if (c1 <= c2) { n           ou[2 * bp[i].sx][2 * bp[i].sy] = va[ed.b][0]; n           ou[2 * bp[i].ex][2 * bp[i].ey] = va[ed.b][1]; n         } else { n           ou[2 * bp[i].sx][2 * bp[i].sy] = va[ed.b][1]; n           ou[2 * bp[i].ex][2 * bp[i].ey] = va[ed.b][0]; n         } n         if (bp[i].sx == bp[i].ex) { n           ou[2 * bp[i].sx][2 * bp[i].sy + 1] =  - ; n         } else { n           ou[2 * bp[i].sx + 1][2 * bp[i].sy] =  | ; n         } n         break; n       } n     } n   } n   for (int i = 0; i < 2 * h - 1; ++i) { n     ou[i][2 * w - 1] =   0 ; n     printf( %s n , ou[i]); n   } n   return 0; n } n


#include<cstdio> n #include<cstring> n #include<algorithm> n #include<queue> n #include<vector> n #include<cassert> n using namespace std; n int n,m,k,vx[100],vy[100];long long sum[100];bool mp[100][100],used[100],tmp[100],clsa[100],clsb[100];int match[100];vector<pair<int,int> >ansseq,curseq; n bool dfs(int v){for(int i=1;i<=n;++i)if(mp[v][i]&&!used[i]){used[i]=1;if(!match[i]||dfs(match[i])){match[i]=v; return 1;}}return 0;} n int maxmatch(){int cnt=0; for(int i=1;i<=n;++i){memset(used+1,0,n);cnt+=dfs(i);}memset(match+1,0,n<<2);return cnt;} n int main(){scanf( %d %d %d ,&n,&m,&k);for(int i=1,x,y;i<=m;++i){scanf( %d %d ,&x,&y);mp[x][y]=1;}for(int i=1;i<=k;++i)scanf( %d %d ,&vx[i],&vy[i]),sum[i]=sum[i-1]+vx[i]; n  int cnt=maxmatch();int mi=0x3f3f3f3f,mip=0;long long ans=-1,cur=0;for(int i=1;i<=k;++i){if(vy[i]<mi)mi=vy[i],mip=i;if(cur+sum[k]-sum[i]>ans) n   {ans=cur+sum[k]-sum[i];ansseq=curseq;for(int j=1;j<=n;++j)if(!clsa[j])ansseq.emplace_back(make_pair(i,j)); n    for(int j=1;j<=n;++j)if(!clsb[j])ansseq.emplace_back(make_pair(i,-j));} cur+=vx[i]; n   if(i+cnt==n){cur-=mi;bool flag=0;for(int j=1;j<=n;++j){ if(clsa[j])continue;for(int k=1;k<=n;++k)tmp[k]=mp[j][k],mp[j][k]=0; n     if(maxmatch()<cnt){ curseq.emplace_back(make_pair(mip,j));flag=1;clsa[j]=1; break;}else for(int k=1;k<=n;++k)mp[j][k]=tmp[k];} n    for(int j=1;j<=n&&!flag;++j){if(clsb[j])continue;for(int k=1;k<=n;++k)tmp[k]=mp[k][j],mp[k][j]=0; n     if(maxmatch()<cnt){ curseq.emplace_back(make_pair(mip,-j)); flag=1;clsb[j]=1;break; }else for(int k=1;k<=n;++k)mp[k][j]=tmp[k];} n    assert(flag);--cnt;}}if(cur>ans)ans=cur,ansseq=curseq;printf( %d n ,(int)ansseq.size()+k);int t=0; n  for(int i=1;i<=k;++i){while(t<(int)ansseq.size()&&ansseq[t].first==i)printf( %d  ,ansseq[t].second),++t;printf( 0  );}return 0;}


#include <bits/stdc++.h> n using namespace std; n int main() { n   int meow, k = 0; n   string s[4]; n   for (int i = 0; i < 4; i++) cin >> s[i]; n   for (int i = 0; i < 4; i++) { n     int n1 = 0, n2 = 0; n     for (int j = 0; j < 4; j++) { n       if (j == i) continue; n       n1 += (((s[i].size() - 2) * 2) <= (s[j].size() - 2)); n       n2 += ((s[i].size() - 2) >= ((s[j].size() - 2) * 2)); n     } n     if (n1 == 3 || n2 == 3) { n       meow = i; n       k++; n     } n   } n   if (k == 1) n     cout << s[meow][0] << endl; n   else n     cout <<  C  << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long n, k; n   cin >> n; n   k = (20.0 + pow(1.0 + 3 * n, 0.5)) / 3.0; n   while (k--) { n     if (3 * k * k - 2 * k <= n) break; n   } n   n -= 3 * k * k - 2 * k; n   if (n - k <= 0) { n     cout << k - 2 * n <<     << 2 * k << endl; n     return 0; n   } n   n -= k; n   if (n - k <= 0) { n     cout << -k - n <<     << 2 * k - 2 * n << endl; n     return 0; n   } n   n -= k; n   if (n - k <= 0) { n     cout << -2 * k + n <<     << -2 * n << endl; n     return 0; n   } n   n -= k; n   if (n - k <= 0) { n     cout << -k + 2 * n <<     << -2 * k << endl; n     return 0; n   } n   n -= k; n   if (n - k - 1 <= 0) { n     cout << k + n <<     << -2 * k + 2 * n << endl; n     return 0; n   } n   n -= k + 1; n   cout << 2 * k + 1 - n <<     << 2 + 2 * n << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long temp = 0; n   string s; n   cin >> s; n   for (int i = 0; i < s.length(); i++) { n     temp = temp * 16; n     if (s[i] ==  > ) n       temp += 8; n     else if (s[i] ==  < ) n       temp += 9; n     else if (s[i] ==  + ) n       temp += 10; n     else if (s[i] ==  - ) n       temp += 11; n     else if (s[i] ==  . ) n       temp += 12; n     else if (s[i] ==  , ) n       temp += 13; n     else if (s[i] ==  [ ) n       temp += 14; n     else if (s[i] ==  ] ) n       temp += 15; n     temp = temp % 1000003; n   } n   cout << temp << endl; n } n


#pragma GCC optimize( Ofast ) n #pragma GCC target( avx,avx2,fma ) n #include  bits/stdc++.h  n using namespace std; n #define FAST    ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);  n #define all(n)  (n).begin(), (n).end() n #define SZ(n)       ((int)(n).size()) n #define MOD      1000000007 n  n const int N = 200050; n pair<int, int> p[N]; n int n; n bool chk(int m){ n  long long sum = p[m].first; n  for (int i = 0; i < n; i++){ n   if (i == m) continue; n   if (sum < p[i].first){ n    return false; n   } n   sum += p[i].first; n  } n   return true; n } n int main(){ n FAST n    int T; cin >> T; n    while(T--){ n      cin >> n; n         for (int i = 0; i < n; i++){ n          cin >> p[i].first; n          p[i].second = i; n         } n         sort(p, p + n); n         int l = 0, r = n; n      while (l < r){ n              int mid = l + (r - l) / 2; n              if (chk(mid)){ n               r = mid; n              } n              else l = mid + 1; n      } n      vector<int> ans; n         for (int i = r; i < n; i++){ n             ans.push_back(p[i].second + 1); n         } n         sort(all(ans)); n         cout << SZ(ans) <<   n ; n         for (auto w : ans) cout << w <<    ; n         cout <<   n ; n     n    } n  n   return 0; n } n  n


#include <bits/stdc++.h> n using namespace std; n template <typename T> n constexpr string_view gtype() { n   string_view s = __PRETTY_FUNCTION__; n   return string_view(s.data() + 45, s.find( ; , 45) - 45); n } n const int MAXN = 10000; n int arr[MAXN]; n int best[MAXN]; n int cur[MAXN]; n int n; n const long long MOD = 1000000009; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(NULL); n   int n; n   cin >> n; n   vector<pair<int, int> > vv(n); n   for (int i = 0; i < n; i++) { n     int x; n     cin >> x; n     vv[i] = {x, i + 1}; n   } n   vector<int> sim; n   sim.push_back(-1); n   sort(((vv).begin()), ((vv).end())); n   for (int i = 0; i < int((vv).size()) - 1; i++) n     if (vv[i].first == vv[i + 1].first) sim.push_back(i); n   if (int((sim).size()) <= 2) n     cout <<  NO n ; n   else { n     cout <<  YES n ; n     for (int j = 0; j < 3; ++j) { n       for (int i = 0; i < int((vv).size()); i++) { n         if (i == sim[j]) { n           cout << vv[i + 1].second <<     << vv[i].second <<    ; n           i++; n         } else n           cout << vv[i].second <<    ; n       } n       cout << endl; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int less_than(vector<int>& a, vector<int>& b) { n   for (int i = 0; i < 3; ++i) n     if (a[i] >= b[i]) return false; n   return true; n } n int main(int argc, char* argv[]) { n   int n; n   cin >> n; n   vector<vector<int> > v(n, vector<int>(5)); n   for (int i = 0; i < n; ++i) { n     cin >> v[i][0] >> v[i][1] >> v[i][2] >> v[i][3]; n     v[i][4] = i + 1; n   } n   sort(v.begin(), v.end()); n   for (int i = 0; i < n; ++i) n     for (int j = i + 1; j < n; ++j) n       if (less_than(v[i], v[j])) { n         v[i][3] = 10000; n         break; n       } n   int lowest = 0; n   for (int i = 1; i < n; ++i) n     if (v[i][3] < v[lowest][3]) lowest = i; n   cout << v[lowest][4] << endl; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 213700; n vector<int> g[N]; n int t[N]; n int n; n vector<int> divisiors[N]; n vector<int> genDivisiors(int a) { n   vector<int> v; n   for (int i = 1; i * i <= a; i++) { n     if (a % i == 0) { n       v.push_back(i); n       if (i * i != (a)) v.push_back(a / i); n     } n   } n   return v; n } n vector<int> himmler[N]; n bool vis[N]; n vector<int> wywal; n void dfs(int a, int sraka, int& size) { n   vis[a] = true; n   wywal.push_back(a); n   size++; n   for (int i = 0; i < g[a].size(); i++) { n     int d = g[a][i]; n     if (!vis[d] and (t[d] % sraka == 0)) { n       dfs(d, sraka, size); n     } n   } n } n long long dp1[N]; n long long dp2[N]; n vector<pair<int, long long>> sraka; n int main() { n   scanf( %d , &n); n   int maxa = 0; n   for (int i = 1; i <= n; i++) { n     scanf( %d , &t[i]); n     maxa = max(maxa, t[i]); n   } n   for (int i = 0; i < n - 1; i++) { n     int a, b; n     scanf( %d%d , &a, &b); n     g[a].push_back(b); n     g[b].push_back(a); n   } n   for (int i = 1; i <= n; i++) { n     if (divisiors[t[i]].empty()) divisiors[t[i]] = genDivisiors(t[i]); n     for (int j = 0; j < divisiors[t[i]].size(); j++) { n       himmler[divisiors[t[i]][j]].push_back(i); n     } n   } n   for (int i = 1; i <= maxa; i++) { n     for (int j = 0; j < himmler[i].size(); j++) { n       int a = himmler[i][j]; n       if (!vis[a]) { n         int s = 0; n         dfs(a, i, s); n         dp1[i] += ((long long)s * (long long)(s + 1)) / 2; n       } n     } n     for (int i = 0; i < wywal.size(); i++) vis[wywal[i]] = false; n     wywal.clear(); n   } n   for (int i = maxa; i >= 1; i--) { n     if (dp1[i] != 0) { n       for (int j = 2 * i; j <= maxa; j += i) { n         dp1[i] -= dp1[j]; n       } n       if (dp1[i] != 0) sraka.emplace_back(i, dp1[i]); n     } n   } n   sort(sraka.begin(), sraka.end()); n   for (auto it : sraka) { n     printf( %d %lld n , it.first, it.second); n   } n } n


#include <bits/stdc++.h> n using namespace std; n int n, k, d, mod = 1000000007; n long long dp[105][2]; n long long go(int nw, bool gotit) { n   if (nw == 0 && gotit) return 1; n   if (nw <= 0) return 0; n   if (dp[nw][gotit] != -1) return dp[nw][gotit]; n   long long ret = 0; n   for (int i = 1; i <= k; i++) { n     if (nw - i >= 0) { n       ret += go(nw - i, (gotit | i >= d)); n     } else n       break; n     ret %= mod; n   } n   return dp[nw][gotit] = ret; n } n int main() { n   memset(dp, -1, sizeof(dp)); n   cin >> n >> k >> d; n   cout << go(n, 0) << endl; n } n


#include <bits/stdc++.h> n #pragma GCC optimize(2) n using namespace std; n const int N = 100010; n char s[N]; n int n, k; n int main() { n   ios::sync_with_stdio(false); n   cin.tie(); n   cout.tie(0); n   int T = 1; n   cin >> T; n   while (T--) { n     cin >> n >> k; n     cin >> s + 1; n     priority_queue<int, vector<int>, greater<int>> q1, q2, q3; n     priority_queue<int> p1, p2; n     int cnt = 0; n     for (int i = 1; i <= n; i++) { n       if (s[i] ==  L ) n         cnt++; n       else { n         if (!cnt) continue; n         if (i > cnt + 1 && s[i - cnt - 1] ==  W  && i <= n && s[i] ==  W ) n           q1.push(cnt); n         else if (i > cnt + 1 && s[i - cnt - 1] ==  W  || i <= n && s[i] ==  W ) n           q2.push(cnt); n         else n           q3.push(cnt); n         cnt = 0; n       } n     } n     if (cnt) { n       if (n > cnt && s[n - cnt] ==  W ) n         q2.push(cnt); n       else n         q3.push(cnt); n     } n     int res = 0; n     while (k && q1.size()) { n       auto v = q1.top(); n       q1.pop(); n       if (k >= v) n         res += 2 * v + 1, k -= v; n       else n         p1.push(v); n     } n     while (k && q2.size()) { n       auto v = q2.top(); n       q2.pop(); n       if (k >= v) n         res += 2 * v, k -= v; n       else n         p2.push(v); n     } n     while (k && (p1.size() || p2.size())) { n       res += 2 * k; n       k = 0; n     } n     if (k && q3.size()) { n       auto v = q3.top(); n       res = 2 * min(k, v) - 1; n     } n     for (int i = 1; i <= n; i++) { n       if (s[i] ==  W ) { n         if (i > 1 && s[i - 1] ==  W ) n           res += 2; n         else n           res++; n       } n     } n     cout << res <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   cout.precision(30); n   ; n   int c = 0, z, n; n   string a, b, s, t; n   cin >> a >> b; n   int x = a.size(); n   int y = b.size(); n   if (x > y) { n     a.swap(b); n     z = x; n     x = y; n     y = z; n   } n   for (int i = 1; i <= x; i++) { n     if (x % i == 0 && y % i == 0) { n       s = a.substr(0, i); n       t =   ; n       n = (y / i) - (x / i); n       for (int j = 0; j < (x / i); j++) t += s; n       if (a.compare(t) == 0) { n         for (int j = 0; j < n; j++) t += s; n         if (b.compare(t) == 0) c++; n       } n     } n   } n   cout << c; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int mod = 1000000007; n long long c[1000001] = {0}; n long long d[1000001] = {0}; n long long f[1000001] = {0}; n long long p; n int binpow(long long b, int n) { n   long long ans = 1; n   while (n) { n     if (n & 1) ans = ans * 1ll * b % mod; n     b = b * 1ll * b % mod; n     n /= 2; n   } n   return int(ans); n } n int main() { n   int n; n   cin >> n; n   n++; n   long long a = 1; n   for (int i = 1; i <= 2 * n - 1; i++) a = a * i % mod; n   long long b = 1; n   for (int i = 1; i <= n; i++) b = b * i % mod; n   long long c = 1; n   for (int i = 1; i <= n - 1; i++) c = c * i % mod; n   long long d = binpow(b, mod - 2); n   long long e = binpow(c, mod - 2); n   cout << (2ll * a % mod * d % mod * e % mod - 1 + mod) % mod << endl; n } n


#include <bits/stdc++.h> n using namespace std; n const int INF = 2e9 + 10; n const int MOD = 1e9 + 7; n const int N = 1e6 + 10; n int n, k, m; n string x, y, z; n int main() { n   ios_base::sync_with_stdio(0); n   cout.tie(0); n   cin.tie(0); n   cin >> x >> y; n   n = x.length(); n   int ans = 1; n   for (int i = 0; i < n; i++) { n     if (y[i] > x[i]) { n       cout << -1; n       return 0; n     } n     z += min(x[i], y[i]); n   } n   cout << z; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n pair<int, int> add(pair<int, int> p1, pair<int, int> p2, int odd, int even) { n   return make_pair(p1.first + p2.first - odd, p1.second + p2.second - even); n } n pair<int, int> dfs(vector<int>* adj, int* visited, int* store, int u, n                    int parent, int odd, int even, int* l, int* m, n                    vector<int>& v) { n   visited[u] = 1; n   int level = store[u]; n   if (level % 2 == 1) { n     if (l[u] != m[u] ^ odd % 2 == 1) { n       odd++; n       v.push_back(u); n     } n   } else { n     if (l[u] != m[u] ^ even % 2 == 1) { n       even++; n       v.push_back(u); n     } n   } n   pair<int, int> p = make_pair(odd, even); n   int a = p.first, b = p.second; n   for (int i = 0; i < adj[u].size(); i++) { n     if (adj[u][i] != parent && !visited[adj[u][i]]) n       p = add(p, dfs(adj, visited, store, adj[u][i], u, a, b, l, m, v), a, b); n   } n   return p; n } n int main() { n   int n; n   scanf( %d , &n); n   ; n   vector<int>* adj = new vector<int>[n]; n   for (int i = 0; i < n - 1; i++) { n     int a, b; n     scanf( %d , &a); n     ; n     scanf( %d , &b); n     ; n     a--; n     b--; n     adj[a].push_back(b); n     adj[b].push_back(a); n   } n   int l[n], m[n]; n   for (int i = 0; i < n; i++) scanf( %d , &l[i]); n   ; n   for (int i = 0; i < n; i++) scanf( %d , &m[i]); n   ; n   int* visited = (int*)calloc(n, sizeof(int)); n   int* store = (int*)calloc(n, sizeof(int)); n   queue<int> q; n   int even = 0, odd = 0, level = 1; n   q.push(0); n   q.push(-1); n   visited[0] = 1; n   while (!q.empty()) { n     int u = q.front(); n     q.pop(); n     if (u == -1 && !q.empty()) { n       level++; n       q.push(-1); n       continue; n     } else if (u == -1) n       break; n     visited[u] = 1; n     store[u] = level; n     for (int i = 0; i < adj[u].size(); i++) { n       if (!visited[adj[u][i]]) { n         q.push(adj[u][i]); n         visited[adj[u][i]] = 1; n       } n     } n   } n   for (int i = 0; i < n; i++) visited[i] = 0; n   vector<int> v; n   pair<int, int> p = dfs(adj, visited, store, 0, -1, 0, 0, l, m, v); n   cout << p.first + p.second << endl; n   for (int i = 0; i < v.size(); i++) cout << v[i] + 1 << endl; n   return 0; n } n


#include <bits/stdc++.h> n int main() { n   int t, j; n   scanf( %d , &t); n   for (j = 1; j <= t; j++) { n     int n, d = 0, i; n     char a[100000], b[100000], c[100000]; n     scanf( %s %s %s , a, b, c); n     n = strlen(a); n     for (i = 0; i < n; i++) { n       if (a[i] == c[i] || b[i] == c[i]) d++; n       if (a[i] == b[i] && b[i] != c[i]) break; n     } n     if (d == n) n       printf( YES n ); n     else n       printf( NO n ); n   } n } n


#include <bits/stdc++.h> n template <typename T> n void MACRO_VAR_Scan(T& t) { n   std::cin >> t; n } n template <typename First, typename... Rest> n void MACRO_VAR_Scan(First& first, Rest&... rest) { n   std::cin >> first; n   MACRO_VAR_Scan(rest...); n } n template <typename T> n void MACRO_VEC_ROW_Init(int n, T& t) { n   t.resize(n); n } n template <typename First, typename... Rest> n void MACRO_VEC_ROW_Init(int n, First& first, Rest&... rest) { n   first.resize(n); n   MACRO_VEC_ROW_Init(n, rest...); n } n template <typename T> n void MACRO_VEC_ROW_Scan(int p, T& t) { n   std::cin >> t[p]; n } n template <typename First, typename... Rest> n void MACRO_VEC_ROW_Scan(int p, First& first, Rest&... rest) { n   std::cin >> first[p]; n   MACRO_VEC_ROW_Scan(p, rest...); n } n template <class T> n inline T CHMAX(T& a, const T b) { n   return a = (a < b) ? b : a; n } n template <class T> n inline T CHMIN(T& a, const T b) { n   return a = (a > b) ? b : a; n } n template <class T> n using V = std::vector<T>; n template <class T> n using VV = V<V<T>>; n template <typename S, typename T> n std::ostream& operator<<(std::ostream& os, std::pair<S, T> p) { n   os <<  (  << p.first <<  ,   << p.second <<  ) ; n   return os; n } n using ll = long long; n using ull = unsigned long long; n using ld = long double; n using PAIR = std::pair<ll, ll>; n using PAIRLL = std::pair<ll, ll>; n constexpr ll INFINT = (1 << 30) - 1; n constexpr ll INFINT_LIM = (1LL << 31) - 1; n constexpr ll INFLL = 1LL << 60; n constexpr ll INFLL_LIM = (1LL << 62) - 1 + (1LL << 62); n constexpr double EPS = 1e-10; n constexpr ll MOD = 1000000007; n constexpr double PI = 3.141592653589793238462643383279; n template <class T, size_t N> n void FILL(T (&a)[N], const T& val) { n   for (auto& x : a) x = val; n } n template <class ARY, size_t N, size_t M, class T> n void FILL(ARY (&a)[N][M], const T& val) { n   for (auto& b : a) FILL(b, val); n } n template <class T> n void FILL(std::vector<T>& a, const T& val) { n   for (auto& x : a) x = val; n } n template <class ARY, class T> n void FILL(std::vector<std::vector<ARY>>& a, const T& val) { n   for (auto& b : a) FILL(b, val); n } n ll powMod(ll n, ll p, ll mod) { n   ll res = 1; n   while (p) { n     if (p & 1) (res *= n) %= mod; n     (n *= n) %= mod; n     p >>= 1; n   } n   return res; n } n ll invMod(ll n, ll mod) { return powMod(n, MOD - 2, MOD); } n const signed FACT_MAX_N = 1000006; n signed fact[FACT_MAX_N]; n signed factInv[FACT_MAX_N]; n struct INIT_FACT { n   INIT_FACT() { n     fact[0] = 1; n     for (ll i = 1; i < FACT_MAX_N; ++i) n       fact[i] = (long long)i * fact[i - 1] % MOD; n     factInv[FACT_MAX_N - 1] = powMod(fact[FACT_MAX_N - 1], MOD - 2, MOD); n     for (ll i = FACT_MAX_N - 2; i >= 0; --i) n       factInv[i] = (long long)(i + 1) * factInv[i + 1] % MOD; n   } n } init_fact; n ll Permutation(ll n, ll r) { n   if (r == 0) return 1; n   if (n < r) return 0; n   return (long long)fact[n] * factInv[n - r] % MOD; n } n ll Combination(ll n, ll r) { n   if (r == 0) return 1; n   if (n < r) return 0; n   return (long long)fact[n] * factInv[n - r] % MOD * factInv[r] % MOD; n } n ll CombinationWithRepetition(ll n, ll r) { return Combination(n + r - 1, r); } n std::vector<ll> enumerateMobius(ll n) { n   std::vector<ll> mu(n + 1, 0); n   mu[1] = 1; n   for (ll i = 1; i <= n; i++) n     if (mu[i]) n       for (ll j = i << 1; j <= n; j += i) mu[j] -= mu[i]; n   return mu; n } n signed main() { n   std::ios::sync_with_stdio(false); n   std::cin.tie(0); n   ; n   ll m; n   MACRO_VAR_Scan(m); n   ; n   auto mu(enumerateMobius(m)); n   ll ans = 1; n   for (ll i = (1); i < (m + 1); ++i) { n     if (mu[i] == 0) continue; n     ll rs = m / i * invMod(m, MOD) % MOD; n     (ans += -mu[i] * rs * invMod(MOD + 1 - rs, MOD) % MOD) %= MOD; n   } n   if (ans < 0) ans += MOD; n   std::cout << (ans); n   std::cout <<   n ; n   ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <class S, class T> n pair<S, T> operator+(const pair<S, T> &s, const pair<S, T> &t) { n   return pair<S, T>(s.first + t.first, s.second + t.second); n } n template <class S, class T> n pair<S, T> operator-(const pair<S, T> &s, const pair<S, T> &t) { n   return pair<S, T>(s.first - t.first, s.second - t.second); n } n template <class S, class T> n ostream &operator<<(ostream &os, pair<S, T> p) { n   os <<  (  << p.first <<  ,   << p.second <<  ) ; n   return os; n } n template <class T> n bool chmax(T &a, const T &b) { n   if (a < b) { n     a = b; n     return 1; n   } n   return 0; n } n template <class T> n bool chmin(T &a, const T &b) { n   if (a > b) { n     a = b; n     return 1; n   } n   return 0; n } n const long long inf = 1000000001; n const long long INF = 2e18; n const long long MOD = 1000000007; n const double pi = 3.14159265358979323846; n int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1}; n int dx2[8] = {1, 1, 0, -1, -1, -1, 0, 1}, dy2[8] = {0, 1, 1, 1, 0, -1, -1, -1}; n int main() { n   int n; n   cin >> n; n   vector<int> a(n); n   for (long long i = 0; i < (long long)(n); i++) { n     cin >> a[i]; n   } n   set<int> st; n   st.insert(0); n   for (long long i = 0; i < (long long)(n); i++) { n     st.insert(a[i]); n   } n   cout << (int)st.size() - 1 << endl; n } n


#define y1 askjdkasldjlkasd n #include <bits/stdc++.h> n #undef y1 n  n using namespace std; n  n #define pb push_back n #define mp make_pair n #define fi(a, b) for(int i=a; i<=b; i++) n #define fj(a, b) for(int j=a; j<=b; j++) n #define fo(a, b) for(int o=a; o<=b; o++) n #define fdi(a, b) for(int i=a; i>=b; i--) n #define fdj(a, b) for(int j=a; j>=b; j--) n #define fdo(a, b) for(int o=a; o>=b; o--) n #define sz(x) (int)x.size() n  n typedef long long ll; n typedef long double ld; n typedef vector<int> vi; n typedef pair<int, int> pii; n typedef vector<pii> vpii; n typedef pair<ll, ll> pll; n typedef vector<pll> vpll; n typedef vector<ll> vll; n  n #ifdef LOCAL n #define err(...) fprintf(stderr, __VA_ARGS__) n #else n #define err(...) while (0) n #endif n  n double START_TIME; n  n void exit() { n #ifdef LOCAL  n  cerr <<  TIME:   << setprecision(5) << fixed << (clock() - START_TIME) / CLOCKS_PER_SEC << endl; n #endif  n  exit(0); n } n  n template<typename A, typename B> n ostream& operator<<(ostream& os, pair<A, B> p) { n  os <<  (  << p.first <<  ,   << p.second <<  ) ; n  return os; n } n  n template<typename T> n ostream& operator<<(ostream& os, vector<T> v) { n  fi(0, sz(v) - 1) { n   os << v[i] <<    ; n  } n  return os; n } n  n template<typename T> n ostream& operator<<(ostream& os, set<T> t) { n  for (auto z : t) { n   os << z <<    ; n  } n  return os; n } n  n template<typename T1, typename T2> n ostream& operator<<(ostream& os, map<T1, T2> t) { n  cerr << endl; n  for (auto z : t) { n   os <<   t  << z.first <<   ->   << z.second << endl; n  } n  return os; n } n  n #ifdef LOCAL n #define dbg(x) {cerr << __LINE__ <<   t  << #x <<  :   << x << endl;} n #define dbg0(x, n) {cerr << __LINE__ <<   t  << #x <<  :  ; for (int ABC = 0; ABC < n; ABC++) cerr << x[ABC] <<    ; cerr << endl;} n #else n #define dbg(x) while(0){} n #define dbg0(x, n) while(0){} n #endif n  n #ifdef LOCAL n #define ass(x) if (!(x)) { cerr << __LINE__ <<   tassertion failed:   << #x << endl, abort(); } n #else n #define ass(x) assert(x) n #endif n  n /////////////////////////////////////////////////// n  n const int MAX = 1e5 + 41; n  n int n; n int a[MAX]; n int l[MAX]; n int r[MAX]; n int q[MAX]; n  n vpii vd, vs; n  n bool bad() { n  vi cnt(n + 1, 0); n  fi(1, n) cnt[a[i]]++; n  int mx = *max_element(cnt.begin(), cnt.end()); n  return (mx > (n + 1) / 2); n } n  n void clear() { n  fi(1, n) l[i] = r[i] = -1; n  vd.clear(); n  vs.clear(); n } n  n void combine(int x, int y) { n  r[x] = y; n  l[y] = x; n } n  n void rot(int x) {  n  while (x != -1) { n   int y = r[x]; n   swap(l[x], r[x]); n   x = y; n  }  n } n  n void findchains() {  n  int l = 1; n  fi(2, n) { n   if (a[i] == a[i - 1]) { n    int x = l; n    int y = i - 1; n    if (a[x] == a[y]) { n     vs.pb(mp(x, y)); n    } else { n     vd.pb(mp(x, y)); n    } n    l = i; n   } else { n    combine(i - 1, i); n   } n  } n  if (a[n] == a[l]) { n   vs.pb(mp(l, n)); n  } else { n   vd.pb(mp(l, n)); n  } n } n  n void combinediff() { n  fi(1, sz(vd) - 1) { n   int x = a[vd[i - 1].second]; n   int y = a[vd[i].first];   n   if (x == y) { n    rot(vd[i].first); n    swap(vd[i].first, vd[i].second);    n   } n //  dbg(vd); n //  combine(vd[i - 1].second, vd[i].first); n  }  n } n  n vi getposesforvalue(int x, int val) { n  vi res; n  while (true) { n   int y = r[x]; n   if (y == -1) break; n   if (a[x] != val && a[y] != val) { n    res.pb(x); n   } n   x = y; n  } n  return res; n } n  n void combinesame() { n /* n  dbg(vs); n  dbg(vd); n  dbg0(l, n + 1); n  dbg0(r, n + 1); n */ n  set<pii> t; n  vi cnt(n + 1, 0); n  for (auto z : vs) { n   cnt[a[z.first]]++; n  } n  vector<vpii> v(n + 1, vpii(0, mp(0, 0))); n  for (auto z : vs) v[a[z.first]].pb(z); n  fi(1, n) if (cnt[i] > 0) t.insert(mp(cnt[i], i)); n // dbg(t); n // dbg(cnt);  n  while (sz(t) > 1 && (*t.rbegin()).first > 1) {   n  n   pii v1 = (*t.rbegin()); n   t.erase(t.find(v1)); n   pii v2 = (*t.rbegin()); n   t.erase(t.find(v2)); n   cnt[v1.second] -= 1; n   cnt[v2.second] -= 1; n    n   pii A = v[v1.second].back(); n   v[v1.second].pop_back(); n   pii B = v[v2.second].back(); n   v[v2.second].pop_back(); n   pii C = v[v1.second].back(); n   v[v1.second].pop_back(); n   combine(A.second, B.first); n   combine(B.second, C.first); n //  err( COMBINE %d %d ; %d %d ; %d %d n , A.first, A.second, B.first, B.second, C.first, C.second); n   v[v1.second].pb(mp(A.first, C.second)); n   t.insert(mp(cnt[v1.second], v1.second));   n   if (cnt[v2.second] > 0) { n    t.insert(mp(cnt[v2.second], v2.second)); n   } n //  dbg(cnt); n //  dbg(t); n  }   n  n   n  n  if (sz(t) > 0) { n   if (sz(t) > 1) { n //   err( V0 n ); n     int lst = -1; n     int l = -1;     n     for (auto z : t) {      n      if (lst == -1) { n       lst = v[z.second].back().second; n       l = v[z.second].back().first; n      } else { n       combine(lst, v[z.second].back().first); n       lst = v[z.second].back().second; n      }      n     } n     if (sz(vd) > 0) { n      fi(1, sz(vd) - 1) { n       combine(vd[i - 1].second, vd[i].first); n      } n      if (a[vd.back().second] == a[l]) { n       rot(l);       n       swap(l, lst); n      } n      combine(vd.back().second, l); n     }    n   } else { n //   err( V1 n ); n    vpii all, vsame = v[(*t.begin()).second];    n    fi(0, sz(vd) - 1) {     n //    dbg(all); n     if (sz(vsame) > 0) { n      if (sz(all) == 0 || a[all.back().second] != a[vsame.back().first]) { n       all.pb(vsame.back()); n       vsame.pop_back(); n      } n     } n //    err( BEFORE ROT: n ); n //     dbg0(l, n + 1); n //    dbg0(r, n + 1); n     if (sz(all) > 0 && a[all.back().second] == a[vd[i].first]) { n      rot(vd[i].first); n      swap(vd[i].first, vd[i].second);      n     }     n     all.pb(vd[i]); n //    err( AFTER ROT: n ); n //    dbg0(l, n + 1); n //    dbg0(r, n + 1); n //    dbg(all); n    }             n    if (sz(all) > 0 && sz(vsame) > 0 && a[all.back().second] != a[vsame.back().first]) { n     all.pb(vsame.back()); n     vsame.pop_back(); n    } n    fi(1, sz(all) - 1) { n     combine(all[i - 1].second, all[i].first); n    }          n     n //   dbg(all); n    if (sz(vsame) > 0) { n          n     err( V2 n );     n     if (sz(all) > 0) { n      vsame.pb(mp(all[0].first, all.back().second));      n     }     n //    dbg(vsame); n     vector<vi> vp(sz(vsame), vi(0, 0)); n     fi(0, sz(vsame) - 1) { n      vp[i] = getposesforvalue(vsame[i].first, a[vsame[0].first]); n //     err( i = %d size = %d n , i, sz(vp[i])); n     } n //    dbg(vsame); n //    dbg(vp); n     vi tocomb;     n     fi(0, sz(vsame) - 1) { n      if (sz(tocomb) > 0 && sz(vp[tocomb.back()]) > 0) { n       int l1 = vp[tocomb.back()].back(); n       vp[tocomb.back()].pop_back(); n       int r1 = vsame[i].first; n       int l2 = vsame[i].second; n       int r2 = r[l1]; n       combine(l1, r1);       n       combine(l2, r2); n       vp[tocomb.back()].insert(vp[tocomb.back()].begin(), vp[i].begin(), vp[i].end());       n      } else { n       while (sz(vp[i]) > 0 && sz(tocomb) > 0) { n        int l1 = vp[i].back(); n        vp[i].pop_back(); n        int r1 = vsame[tocomb.back()].first; n        int l2 = vsame[tocomb.back()].second; n        int r2 = r[l1]; n //       err( TO COMB %d %d; %d %d n , l1, r1, l2, r2); n //       dbg0(l, n + 1); n //       dbg0(r, n + 1); n        tocomb.pop_back(); n        combine(l1, r1); n        combine(l2, r2);        n       } n       tocomb.pb(i);       n      } n     }     n    } n   } n  } else { n //  err( VA n ); n //  dbg(vd); n   fi(1, sz(vd) - 1) { n     combine(vd[i - 1].second, vd[i].first); n    } n  } n } n  n void printans() { n  int st = -1; n  fi(1, n) if (l[i] == -1) st = i; n  if (st == -1) { n   err( st = %d n , st); n   assert(false); n  } n /* n  err( ans: n ); n  dbg0(l, n + 1);  n  dbg0(r, n + 1); n */ n  int ans = 0; n  vi res; n  while (st != -1) { n //  err( %d  , st); n   res.pb(st); n   st = r[st]; n  } n // err(  n ); n  n  if (sz(res) < n) { n   err( size = %d n , sz(res)); n   assert(false); n  } n  fi(1, sz(res) -1 ) { n   ans += (abs(res[i - 1] - res[i]) > 1 ? 1 : 0); n   if (a[res[i]] == a[res[i - 1]]) { n    err( same neigh! n ); n    assert(false); n   } n  } n  //dbg(res); n  printf( %d n , ans); n } n  n void solve () { n  if (n == 1) { n   printf( 0 n ); n   return; n  } n  if (bad()) { n   printf( -1 n ); n   return; n  } n  clear(); n  findchains();  n  combinediff();  n  combinesame(); n /* n  dbg(  ); n  dbg(vd); n  dbg(vs); n  dbg0(l, n + 1); n  dbg0(r, n + 1); n */ n  printans(); n } n  n int main() { n #ifdef LOCAL n  freopen( input.txt ,  r , stdin); n  freopen( output.txt ,  w , stdout); n  START_TIME = (double)clock(); n #endif n   n  int t; n  scanf( %d , &t); n  while (t--) { n   scanf( %d , &n);   n   fi(1, n) scanf( %d , &a[i]); n   solve(); n  } n  n  exit(); n  return 0; n } n  n  n  n


#include <bits/stdc++.h> n using namespace std; n int main() { n   string temp; n   cin >> temp; n   string a[4]; n   a[0] = temp[0]; n   a[1] = temp[1]; n   cin >> temp; n   a[3] = temp[0]; n   a[2] = temp[1]; n   cin >> temp; n   string b[4]; n   b[0] = temp[0]; n   b[1] = temp[1]; n   cin >> temp; n   b[3] = temp[0]; n   b[2] = temp[1]; n   string A =   ; n   string B =   ; n   for (int i = 0; i < 4; i++) { n     if (a[i] !=  X ) { n       A += a[i]; n     } n     if (b[i] !=  X ) { n       B += b[i]; n     } n   } n   A = A + A; n   string ans =  NO ; n   for (int i = 0; i < 3; i++) { n     if (A.substr(i, 3) == B) { n       ans =  YES ; n     } n   } n   cout << ans; n   return 0; n } n