#include <bits/stdc++.h> n using namespace std; n const int N = 1e6 + 5, M = (1 << 20) + 5; n template <typename T> n void read(T &x) { n   bool neg = false; n   unsigned char c = getchar(); n   for (; (c ^ 48) > 9; c = getchar()) n     if (c ==  - ) neg = true; n   for (x = 0; (c ^ 48) < 10; c = getchar()) x = (x << 3) + (x << 1) + (c ^ 48); n   if (neg) x = -x; n } n struct line { n   int next, to; n } e[N]; n int n, m, hd[N], cnt, k, s[25], t[25], f[N], a[M], pop[M]; n bool iss[N], ist[N]; n void adline(int u, int v) { n   e[cnt].to = v; n   e[cnt].next = hd[u]; n   hd[u] = cnt++; n } n void dfs(int u) { n   if (f[u]) return; n   for (int i = hd[u]; ~i; i = e[i].next) { n     int v = e[i].to; n     dfs(v); n     f[u] |= f[v]; n   } n } n int main() { n   read(n); n   read(m); n   for (int i = 0, i_end = n; i < i_end; ++i) hd[i] = -1, iss[i] = ist[i] = true; n   for (int i = 0, i_end = m; i < i_end; ++i) { n     int u, v; n     read(u); n     read(v); n     adline(--u, --v); n     ist[u] = false; n     iss[v] = false; n   } n   for (int i = 0, i_end = n; i < i_end; ++i) n     if (iss[i]) s[k++] = i; n   k = 0; n   for (int i = 0, i_end = n; i < i_end; ++i) n     if (ist[i]) t[k++] = i; n   for (int i = 0, i_end = k; i < i_end; ++i) f[t[i]] = 1 << i; n   for (int i = 0, i_end = k; i < i_end; ++i) dfs(s[i]), a[1 << i] = f[s[i]]; n   for (int i = 1, i_end = 1 << k; i < i_end; ++i) n     pop[i] = pop[i ^ (i & -i)] + 1; n   for (int i = 1, i_end = (1 << k) - 1; i < i_end; ++i) { n     a[i] = a[i & -i] | a[i ^ (i & -i)]; n     if (pop[a[i]] <= pop[i]) return puts( NO ), 0; n   } n   puts( YES ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   cin >> n; n   map<int, int> A; n   for (int i = 0; i < n; i++) { n     int x; n     cin >> x; n     A[x]++; n   } n   int mx = 0; n   for (auto x : A) { n     if (x.second > mx) mx = x.second; n   } n   cout << mx; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   const string t =  RGB ; n   int q; n   cin >> q; n   for (int i = 0; i < q; ++i) { n     int n, k; n     string s; n     cin >> n >> k >> s; n     int ans = 1e9; n     for (int j = 0; j < n - k + 1; ++j) { n       for (int offset = 0; offset < 3; ++offset) { n         int cur = 0; n         for (int pos = 0; pos < k; ++pos) { n           if (s[j + pos] != t[(pos + offset) % 3]) { n             ++cur; n           } n         } n         ans = min(ans, cur); n       } n     } n     cout << ans << endl; n   } n   return 0; n } n


#include<bits/stdc++.h> n using namespace std; n  n const int N=500; n int n,mod,xn[N]; n long long dp[N*N]; n  n #define id(x,y) *(xn+x)+y n  n int main(){ n  cin>>n>>mod; n  dp[id(1,1)]=1; n  for(int i=2;i<=n;++i) n   xn[i]=xn[i-1]+n; n  xn[n+2]=(xn[n+1]=xn[n]+n)+n; n  for(int i=1;i<n;i++){ n    long long a=(dp[id(i,1)]<<1)%mod; n    dp[id(i+1,2)]+=a; n    dp[id(i+1,1)]+=a; n    dp[id(i+2,1)]+=a;    n    for(int j=2;j<=n;j++){ n   long long a=dp[id(i,j)]*(j-1)%mod,b=(dp[id(i,j)]*j<<1)%mod; n   dp[id(i+1,j+1)]+=dp[id(i,j)]*(j+1)%mod; n   dp[id(i+3,j-1)]+=a; n   dp[id(i+2,j-1)]+=a<<1; n   dp[id(i+1,j)]+=b; n   dp[id(i+2,j)]+=b; n    } n  } n     n  cout<<dp[id(n,1)]%mod<<endl; n }


#include <bits/stdc++.h> n long long gi() { n   long long x = 0, f = 1; n   char ch = getchar(); n   while (!isdigit(ch)) f ^= ch ==  - , ch = getchar(); n   while (isdigit(ch)) x = x * 10 + ch -  0 , ch = getchar(); n   return f ? x : -x; n } n std::mt19937 rnd(time(NULL)); n template <class T> n void cxk(T& a, T b) { n   a = a > b ? a : b; n } n template <class T> n void cnk(T& a, T b) { n   a = a < b ? a : b; n } n int A[2][3], B[8], C[3]; n int main() { n   int qwq = gi(); n   while (qwq--) { n     for (int i = 0; i < 2; ++i) n       for (int j = 2; ~j; --j) A[i][j] = gi(); n     for (int j = 7; j; --j) B[j] = gi(); n     int s = 0; n     for (int i = 1; i <= 7; ++i) n       if (i != (i & -i)) s += B[i]; n     for (int i = 0; i < 3; ++i) { n       int sum = A[0][i] + A[1][i]; n       for (int j = 1; j <= 7; ++j) n         if (j >> i & 1) sum -= B[j]; n       if (sum < 0) { n         puts( -1 ); n         goto ed; n       } n     } n     for (int i = 0; i < 3; ++i) C[i] = B[7 ^ (1 << i)]; n     for (int a = 0, b = s; a <= s; ++a, --b) { n       bool flg = 0; n       for (int i = 0; i < 3; ++i) n         if (std::max(0, a - A[0][i]) + std::max(0, b - A[1][i]) > C[i]) flg = 1; n       if (a < std::max(0, a - A[0][0]) + std::max(0, a - A[0][1]) + n                   std::max(0, a - A[0][2])) n         flg = 1; n       if (b < std::max(0, b - A[1][0]) + std::max(0, b - A[1][1]) + n                   std::max(0, b - A[1][2])) n         flg = 1; n       if (flg) continue; n       int D[8] = {0}; n       D[7] = a; n       for (int i = 0; i < 3; ++i) { n         int p = std::max(0, a - A[0][i]); n         D[7 ^ (1 << i)] = p; n         D[7] -= p; n       } n       for (int i = 0; i < 3; ++i) { n         int p = std::max( n             0, std::min({D[7] - B[7], B[7 ^ (1 << i)] - D[7 ^ (1 << i)], n                          C[i] - D[7 ^ (1 << i)] - std::max(0, b - A[1][i])})); n         D[7 ^ (1 << i)] += p; n         D[7] -= p; n       } n       for (int i = 0; i < 3; ++i) { n         int s = A[0][i]; n         for (int j = 1; j <= 7; ++j) n           if (j >> i & 1) s -= D[j]; n         D[1 << i] = std::min(s, B[1 << i]); n       } n       for (int i = 7; i; --i) printf( %d  , D[i]); n       puts(  ); n       for (int i = 1; i <= 7; ++i) assert(D[i] <= B[i]); n       for (int i = 0; i < 3; ++i) { n         int s1 = 0, s2 = 0; n         for (int j = 1; j <= 7; ++j) n           if (j >> i & 1) s1 += D[j], s2 += B[j] - D[j]; n         assert(s1 <= A[0][i] && s2 <= A[1][i]); n       } n       goto ed; n     } n     puts( -1 ); n   ed:; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long N = 1e5 + 5; n const long long mod = 1e9 + 7; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   long long n; n   cin >> n; n   long long a[n], b[n]; n   for (long long i = 0; i < n; i++) cin >> a[i]; n   for (long long i = 0; i < n; i++) cin >> b[i]; n   sort(a, a + n, greater<long long>()); n   sort(b, b + n, greater<long long>()); n   long long i = 0, j = 0; n   long long v1 = 0, v2 = 0; n   for (long long c = 0; c < 2 * n; c++) { n     long long op1 = (i == n ? 0 : a[i]); n     long long op2 = (j == n ? 0 : b[j]); n     if (op1 >= op2) { n       i++; n       if (c % 2 == 0) v1 += op1; n     } else { n       j++; n       if (c & 1) v2 += op2; n     } n   } n   cout << v1 - v2; n } n


#include <bits/stdc++.h> n using namespace std; n const long long MOD = 1e9 + 7; n const int INF = 1e9; n const long long IINF = 1e18; n const int dx[4] = {1, 0, -1, 0}; n const int dy[4] = {0, 1, 0, -1}; n const char dir[4] = { D ,  R ,  U ,  L }; n template <typename T> n istream &operator>>(istream &is, vector<T> &v) { n   for (T &x : v) is >> x; n   return is; n } n template <typename T> n ostream &operator<<(ostream &os, const vector<T> &v) { n   for (int i = 0; i < v.size(); ++i) { n     os << v[i] << (i + 1 == v.size() ?   n  :    ); n   } n   return os; n } n template <typename T, typename U> n ostream &operator<<(ostream &os, const pair<T, U> &p) { n   cout <<  (  << p.first <<  ,  << p.second <<  ) ; n   return os; n } n template <class T> n inline bool chmin(T &a, T b) { n   if (a > b) { n     a = b; n     return true; n   } n   return false; n } n template <class T> n inline bool chmax(T &a, T b) { n   if (a < b) { n     a = b; n     return true; n   } n   return false; n } n void solve(long long n) { n   long long m = n; n   vector<int> convert; n   while (3 <= m) { n     convert.emplace_back(m % 3); n     m /= 3; n   } n   convert.emplace_back(m); n   convert.emplace_back(0); n   int d = convert.size(), most; n   for (most = d - 1; most >= 0; --most) { n     if (convert[most] == 2) break; n   } n   if (most < 0) { n     cout << n <<   n ; n     return; n   } n   for (int i = 0; i < most; ++i) convert[i] = 0; n   for (int i = most; i < d; ++i) { n     if (convert[i] == 2) n       convert[i] = 0, convert[i + 1] += 1; n     else if (convert[i] == 3) n       convert[i] = 0, convert[i + 1] + -1; n   } n   long long po = 1, ans = 0; n   for (int i = 0; i < d; ++i) { n     ans += po * convert[i]; n     po *= 3; n   } n   cout << ans <<   n ; n } n int main() { n   cin.tie(0); n   ios::sync_with_stdio(false); n   int t; n   cin >> t; n   for (; t--;) { n     long long n; n     cin >> n; n     solve(n); n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 1000000 + 10; n int cnt[maxn]; n int main() { n   int n, m; n   cin >> n >> m; n   for (int i = 0; i < m; ++i) { n     int a, b; n     scanf( %d%d , &a, &b); n     --a, --b; n     cnt[a]++; n     cnt[b]++; n   } n   long long tot = 1LL * n * (n - 1) * (n - 2) / 6; n   long long bad = 0; n   for (int i = 0; i < n; ++i) { n     bad += 1LL * cnt[i] * (n - 1 - cnt[i]); n   } n   cout << tot - bad / 2 << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   string a, b; n   getline(cin, a); n   getline(cin, b); n   long long i = 0, j = 0; n   while (a[i] ==  0 ) i++; n   while (b[j] ==  0 ) j++; n   if (a.size() - i > b.size() - j) { n     cout <<  >  << endl; n     return 0; n   } else if (a.size() - i < b.size() - j) { n     cout <<  <  << endl; n     return 0; n   } else { n     while (i < a.size() && j < b.size()) { n       if (a[i] > b[j]) { n         cout <<  >  << endl; n         return 0; n       } else if (a[i] < b[j]) { n         cout <<  <  << endl; n         return 0; n       } n       i++; n       j++; n     } n   } n   cout <<  =  << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int n = 100000, i = 700; n long long a[n], s[n], d[n], h[1000]; n long long *g1, *g2, *g3, *g4; n int *g5, *g6, *g9; n int my, ans = 0, a1[n], h1[6], h2[6]; n long long gcd(long long e, long long r) { n   if (e < 0) e = -e; n   while ((e) && (r)) n     if (e > r) n       e %= r; n     else n       r %= e; n   return e + r; n } n void getxy(long long ae, long long se, long long de, long long ar, long long sr, n            long long dr, long long &f1, long long &f2, long long &f3, n            long long &f4) { n   f1 = dr * se - de * sr; n   f2 = ae * sr - ar * se; n   if (f2 < 0) { n     f1 = -f1; n     f2 = -f2; n   } n   if (f2 == 0) return; n   long long c = gcd(f1, f2); n   f1 /= c; n   f2 /= c; n   if (se) { n     f3 = -ae * f1 - de * f2; n     f4 = f2 * se; n   } else { n     f3 = -ar * f1 - dr * f2; n     f4 = f2 * sr; n   } n   if (f4 < 0) { n     f3 = -f3; n     f4 = -f4; n   } n   c = gcd(f3, f4); n   f3 /= c; n   f4 /= c; n } n int cmp(int e, long long g1r, long long g2r, long long g3r, long long g4r) { n   if (g1[e] < g1r) return 0; n   if (g1[e] > g1r) return 2; n   if (g2[e] < g2r) return 0; n   if (g2[e] > g2r) return 2; n   if (g3[e] < g3r) return 0; n   if (g3[e] > g3r) return 2; n   if (g4[e] < g4r) return 0; n   if (g4[e] > g4r) return 2; n   return 1; n } n void qs(int q, int w) { n   int e = q, r = w; n   long long y = q + (rand() % (w - q + 1)), t1 = g1[y], t2 = g2[y], t3 = g3[y], n             t4 = g4[y]; n   do { n     while (cmp(e, t1, t2, t3, t4) == 0) e++; n     while (cmp(r, t1, t2, t3, t4) == 2) r--; n     if (e <= r) { n       y = g1[e]; n       g1[e] = g1[r]; n       g1[r] = y; n       y = g2[e]; n       g2[e] = g2[r]; n       g2[r] = y; n       y = g3[e]; n       g3[e] = g3[r]; n       g3[r] = y; n       y = g4[e]; n       g4[e] = g4[r]; n       g4[r] = y; n       y = g5[e]; n       g5[e] = g5[r]; n       g5[r] = y; n       y = g6[e]; n       g6[e] = g6[r]; n       g6[r] = y; n       e++; n       r--; n     } n   } while (e <= r); n   if (q < r) qs(q, r); n   if (e < w) qs(e, w); n } n void qs1(int q, int w) { n   int e = q, r = w; n   long long y = q + (rand() % (w - q + 1)), t = g9[y]; n   do { n     while (g9[e] > t) e++; n     while (g9[r] < t) r--; n     if (e <= r) { n       y = g1[e]; n       g1[e] = g1[r]; n       g1[r] = y; n       y = g2[e]; n       g2[e] = g2[r]; n       g2[r] = y; n       y = g3[e]; n       g3[e] = g3[r]; n       g3[r] = y; n       y = g4[e]; n       g4[e] = g4[r]; n       g4[r] = y; n       y = g5[e]; n       g5[e] = g5[r]; n       g5[r] = y; n       y = g6[e]; n       g6[e] = g6[r]; n       g6[r] = y; n       y = g9[e]; n       g9[e] = g9[r]; n       g9[r] = y; n       e++; n       r--; n     } n   } while (e <= r); n   if (q < r) qs1(q, r); n   if (e < w) qs1(e, w); n } n void del(int &q, long long *a, long long *s, long long *d, int *a1, n          long long f1, long long f2, long long f3, long long f4, int v1, int v2, n          int v3) { n   int w, t = 0; n   long long c1, c2, c3, c4; n   for (w = 0; w < q; w++) { n     if ((a[w] == v1) && ((s[w] == v2) && (d[w] == v3))) continue; n     getxy(a[w], s[w], d[w], v1, v2, v3, c1, c2, c3, c4); n     if (((c1 == f1) && (c2 == f2)) && ((c3 == f3) && (c4 == f4))) continue; n     a[t] = a[w]; n     s[t] = s[w]; n     d[t] = d[w]; n     a1[t] = a1[w]; n     t++; n   } n   q = t; n } n bool poss(int q, int r, int t, int y, int *a) { n   int w, e = 0; n   for (w = 0; w < y; w++) n     if (r + w < t) e += a[r + w]; n   if (e >= q) return 1; n   return 0; n } n long long impos = 0, impos1[i], impos2[i], impos3[i], impos4[i]; n bool schim(long long c1, long long c2, long long c3, long long c4) { n   int w; n   for (w = 0; w < impos; w++) n     if (((impos1[w] == c1) && (impos2[w] == c2)) && n         ((impos3[w] == c3) && (impos4[w] == c4))) n       return 0; n   return 1; n } n void addim(int c1, int c2, int c3, int c4) { n   impos1[impos] = c1; n   impos2[impos] = c2; n   impos3[impos] = c3; n   impos4[impos] = c4; n   impos++; n } n bool slv(int q, int y, long long *a, long long *s, long long *d, int *a1, n          bool b) { n   if (q == 0) return 1; n   if (y == 0) return 0; n   int w, e, r, t, u, f5[i], f6[i], f9[i]; n   long long f1[i], f2[i], f3[i], f4[i]; n   u = my * my + 1; n   if (b) u = q; n   t = 0; n   for (w = 0; (w < u) && (w < q); w++) n     for (e = w + 1; (e < u) && (e < q); e++) { n       getxy(a[w], s[w], d[w], a[e], s[e], d[e], f1[t], f2[t], f3[t], f4[t]); n       f5[t] = w; n       f6[t] = e; n       if (f2[t]) t++; n     } n   g1 = f1; n   g2 = f2; n   g3 = f3; n   g4 = f4; n   g5 = f5; n   g6 = f6; n   if (t) qs(0, t - 1); n   if (t == 0) { n     if (q > y) return 0; n     for (w = 0; w < q; w++) { n       h1[ans] = a1[w] + 1; n       h2[ans] = -1; n       ans++; n     } n     return 1; n   } n   r = 0; n   for (w = 0; w < t;) { n     f1[r] = f1[w]; n     f2[r] = f2[w]; n     f3[r] = f3[w]; n     f4[r] = f4[w]; n     f5[r] = f5[w]; n     f6[r] = f6[w]; n     f9[r] = 1; n     for (e = w + 1; (e < t) && (((f1[w] == f1[e]) && (f2[w] == f2[e])) && n                                 ((f3[w] == f3[e]) && (f4[w] == f4[e]))); n          e++) n       f9[r]++; n     f9[r] = h[f9[r]]; n     r++; n     w = e; n   } n   g9 = f9; n   t = r; n   qs1(0, t - 1); n   long long aa[q], ss[q], dd[q]; n   int aa1[q]; n   for (w = 0; w < q; w++) { n     aa[w] = a[w]; n     ss[w] = s[w]; n     dd[w] = d[w]; n     aa1[w] = a1[w]; n   } n   if (g9[0] > my) { n     r = g5[0]; n     h1[ans] = a1[g5[0]] + 1; n     h2[ans] = a1[g6[0]] + 1; n     ans++; n     del(q, aa, ss, dd, aa1, f1[0], f2[0], f3[0], f4[0], a[r], s[r], d[r]); n     y--; n     return slv(q, y, aa, ss, dd, aa1, 0); n   } n   int q1, im0 = impos; n   for (w = 0; w < t; w++) n     if ((poss(q, w, t, y, f9)) && (schim(f1[w], f2[w], f3[w], f4[w]))) { n       r = f5[w]; n       h1[ans] = a1[f5[w]] + 1; n       h2[ans] = a1[f6[w]] + 1; n       ans++; n       for (e = 0; e < q; e++) { n         aa[e] = a[e]; n         ss[e] = s[e]; n         dd[e] = d[e]; n         aa1[e] = a1[e]; n       } n       q1 = q; n       del(q1, aa, ss, dd, aa1, f1[w], f2[w], f3[w], f4[w], a[r], s[r], d[r]); n       if (slv(q1, y - 1, aa, ss, dd, aa1, 1)) return 1; n       ans--; n       addim(f1[w], f2[w], f3[w], f4[w]); n     } n   impos = im0; n   return 0; n } n int main() { n   int q, q1, w, e, r, r1, r2, r3, t, y, u, max1, max2; n   for (w = 1; w <= 26; w++) h[w * (w - 1) / 2] = w; n   cin >> q >> y; n   my = y; n   for (w = 0; w < q; w++) { n     scanf( %I64d%I64d%I64d , &a[w], &s[w], &d[w]); n     a1[w] = w; n   } n   if (!slv(q, y, a, s, d, a1, 0)) n     cout <<  NO ; n   else { n     cout <<  YES n  << ans <<   n ; n     for (w = 0; w < ans; w++) cout << h1[w] <<     << h2[w] <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int t, i, n; n   cin >> t; n   for (i = 1; i <= t; i++) { n     cin >> n; n     cout << n / 2 <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int mat[1001][1001][2]; n int dp1[1001][1001]; n int dp2[1001][1001]; n int dx[2] = {0, 1}; n int dy[2] = {1, 0}; n vector<pair<int, int> > vx; n void findxx1(int i, int j); n void findxx2(int i, int j); n int chk(int x, int y, int n) { n   if (x >= n || y >= n || y < 0 || x < 0) n     return 0; n   else n     return 1; n } n struct parent { n   int x; n   int y; n }; n struct parent par1[1001][1001], par2[1001][1001]; n struct node { n   int x; n   int y; n }; n void bfs2(int n) { n   int i, j, x, y; n   queue<node> q; n   node a; n   a.x = 0; n   a.y = 0; n   q.push(a); n   for (i = 0; i < n; i++) n     for (j = 0; j < n; j++) dp1[i][j] = 99999; n   dp1[0][0] = mat[0][0][0]; n   while (!q.empty()) { n     x = q.front().x; n     y = q.front().y; n     q.pop(); n     for (i = 0; i < 2; i++) { n       int x1, y1; n       x1 = x + dx[i]; n       y1 = y + dy[i]; n       if (chk(x1, y1, n)) { n         if (dp1[x1][y1] > dp1[x][y] + mat[x1][y1][0]) { n           dp1[x1][y1] = dp1[x][y] + mat[x1][y1][0]; n           a.x = x1; n           a.y = y1; n           q.push(a); n           par1[x1][y1].x = x; n           par1[x1][y1].y = y; n         } n       } n     } n   } n } n void bfs5(int n) { n   int i, j, x, y; n   queue<node> q; n   node a; n   a.x = 0; n   a.y = 0; n   q.push(a); n   for (int i = 0; i < n; i++) n     for (j = 0; j < n; j++) dp2[i][j] = 99999; n   dp2[0][0] = mat[0][0][1]; n   while (!q.empty()) { n     x = q.front().x; n     y = q.front().y; n     q.pop(); n     for (i = 0; i < 2; i++) { n       int x1, y1; n       x1 = x + dx[i]; n       y1 = y + dy[i]; n       if (chk(x1, y1, n)) { n         if (dp2[x1][y1] > dp2[x][y] + mat[x1][y1][1]) { n           dp2[x1][y1] = dp2[x][y] + mat[x1][y1][1]; n           a.x = x1; n           a.y = y1; n           q.push(a); n           par2[x1][y1].x = x; n           par2[x1][y1].y = y; n         } n       } n     } n   } n } n void zeroPrint(int x, int y, int n) { n   int i, j, k; n   for (i = 0; i < x; i++) cout <<  D ; n   for (i = 0; i < y; i++) cout <<  R ; n   for (i = x; i < n - 1; i++) cout <<  D ; n   for (i = y; i < n - 1; i++) cout <<  R ; n } n int main() { n   ios_base::sync_with_stdio(0); n   vx.reserve(10000); n   int n, i, j, k, x, xx, yy, zero = 0; n   cin >> n; n   for (i = 0; i < n; i++) { n     for (j = 0; j < n; j++) { n       cin >> x; n       if (x == 0) { n         zero = 1; n         xx = i; n         yy = j; n       } else { n         int tmp = x; n         while (tmp && tmp % 2 == 0) { n           mat[i][j][0]++; n           tmp /= 2; n         } n         while (tmp && tmp % 5 == 0) { n           mat[i][j][1]++; n           tmp /= 5; n         } n       } n     } n   } n   bfs2(n); n   bfs5(n); n   int ret = min(dp1[n - 1][n - 1], dp2[n - 1][n - 1]); n   if (zero && ret > 1) { n     cout << 1 << endl; n     zeroPrint(xx, yy, n); n   } else { n     cout << ret << endl; n     vx.push_back({n - 1, n - 1}); n     if (dp1[n - 1][n - 1] > dp2[n - 1][n - 1]) { n       findxx2(n - 1, n - 1); n     } else n       findxx1(n - 1, n - 1); n     reverse(vx.begin(), vx.end()); n     for (i = 1; i < vx.size(); i++) { n       if (vx[i - 1].first == vx[i].first) n         cout <<  R ; n       else n         cout <<  D ; n     } n     cout << endl; n   } n } n void findxx1(int i, int j) { n   if (i == 0 && j == 0) { n     return; n   } else { n     vx.push_back({par1[i][j].x, par1[i][j].y}); n     findxx1(par1[i][j].x, par1[i][j].y); n   } n } n void findxx2(int i, int j) { n   if (i == 0 && j == 0) { n     return; n   } else { n     vx.push_back({par2[i][j].x, par2[i][j].y}); n     findxx2(par2[i][j].x, par2[i][j].y); n   } n } n


#include <bits/stdc++.h> n using namespace std; n long long N, M, K, ans; n long long a[105][21005]; n long long sum[105][21005]; n long long tree[21005 * 8]; n long long lazy[21005 * 8]; n long long dp[21005 * 8]; n void updLazy(long long x, long long l, long long r) { n   if (!lazy[x]) return; n   tree[x] += lazy[x]; n   lazy[2 * x + 1] += lazy[x]; n   lazy[2 * x + 2] += lazy[x]; n   lazy[x] = 0; n } n void upd(long long x, long long l, long long r, long long lq, long long rq, n          long long v, bool t) { n   if (r < l) return; n   updLazy(x, l, r); n   if (l > rq or r < lq) return; n   if (l >= lq && r <= rq) { n     if (t) tree[x] = lazy[x] = 0; n     lazy[x] += v; n     updLazy(x, l, r); n     return; n   } n   long long mid = (l + r) / 2; n   upd(2 * x + 1, l, mid, lq, rq, v, t); n   upd(2 * x + 2, mid + 1, r, lq, rq, v, t); n   tree[x] = max(tree[2 * x + 1], tree[2 * x + 2]); n } n long long suma2(long long i, long long j) { n   return sum[i][j + K - 1] - sum[i][j - 1] + sum[i + 1][j + K - 1] - n          sum[i + 1][j - 1]; n } n int main() { n   cin >> N >> M >> K; n   for (long long i = 1; i <= N; i++) n     for (long long j = 1; j <= M; j++) { n       cin >> a[i][j]; n       sum[i][j] = sum[i][j - 1] + a[i][j]; n     } n   for (long long i = 1; i <= N; i++) n     for (long long j = M + 1; j <= M; j++) sum[i][j] = sum[i][j - 1]; n   for (long long i = N; i >= 1; i--) { n     for (long long j = 1; j + K - 1 <= M; j++) { n       if (j <= K) n         upd(0, 1, M, j, j, dp[j] - sum[i + 1][K] + sum[i + 1][j - 1], 1); n       else n         upd(0, 1, M, j, j, dp[j], 1); n     } n     for (long long j = 1; j + K - 1 <= M; j++) { n       dp[j] = suma2(i, j) + tree[0]; n       ans = max(ans, dp[j]); n       upd(0, 1, M, max(1LL, j - K + 1), j, a[i + 1][j], 0); n       upd(0, 1, M, j + 1, min(M, j + K), -a[i + 1][j + K], 0); n       ans = max(ans, dp[j]); n     } n   } n   cout << ans <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n #pragma GCC optimize( O2 ) n using namespace std; n int main() { n   int n, co = 1; n   std::vector<pair<long long, long long>> v; n   cin >> n; n   for (int i = 0; i < n; i++) { n     long long a, b; n     cin >> a >> b; n     v.push_back(make_pair(a, b)); n   } n   for (int i = 1; i < n; i++) { n     if (v[i].first - v[i].second > v[i - 1].first) { n       co++; n     } else if (v[i].first + v[i].second < v[i + 1].first) { n       v[i].first = v[i].first + v[i].second; n       co++; n     } else if (i == n - 1) { n       co++; n     } n   } n   cout << co; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long n, m; n vector<long long> a(30005); n bool check(long long v) { n   long long i, val = 0; n   for (i = 0; i < n; i++) { n     long long x = a[i]; n     long long y = (a[i] + v) % m; n     if (y >= x) { n       if (x > val) val = x; n       if (y < val) return false; n     } else { n       if (val > y) val = max(val, x); n     } n   } n   return true; n } n void solve() { n   cin >> n >> m; n   a.resize(n); n   for (auto &x : a) cin >> x; n   long long low = 0, high = m, mid, ans = m; n   while (low <= high) { n     mid = (low + high) / 2; n     long long z = check(mid); n     if (z) { n       ans = mid; n       high = mid - 1; n     } else n       low = mid + 1; n   } n   cout << ans <<   n ; n } n int32_t main() { n   ios_base::sync_with_stdio(0); n   cin.tie(NULL); n   cout.tie(NULL); n   long long t = 1; n   while (t--) { n     solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 200009; n vector<pair<int, int> > grafo[MAXN]; n int upth[MAXN], dis[MAXN]; n int Build(int u, int p, int upd, int gd) { n   upth[u] = upd; n   dis[u] = gd; n   int ct = 0; n   for (int i = 0; i < grafo[u].size(); ++i) { n     int v = grafo[u][i].first; n     if (v != p) { n       int res = Build(v, u, upd + grafo[u][i].second, gd + 1); n       ct += res + grafo[u][i].second; n     } n   } n   return ct; n } n int main() { n   int N; n   scanf( %d n , &N); n   for (int i = 1; i < N; ++i) { n     int u, v; n     scanf( %d%d , &u, &v); n     grafo[u].push_back(pair<int, int>(v, 0)); n     grafo[v].push_back(pair<int, int>(u, 1)); n   } n   int UT = Build(1, 0, 0, 0); n   int mint = MAXN; n   for (int i = 1; i <= N; ++i) mint = min(mint, UT - (2 * upth[i]) + dis[i]); n   printf( %d n , mint); n   int cnt = 0; n   for (int i = 1; i <= N; ++i) n     if ((UT - (2 * upth[i]) + dis[i]) == mint) { n       if (cnt++) printf(   ); n       printf( %d , i); n     } n   printf(  n ); n   return 0; n } n


#include <bits/stdc++.h> n int A[10] = {2, 7, 2, 3, 3, 4, 2, 5, 1, 2}; n int main() { n   char a, b; n   scanf( %c%c , &a, &b); n   printf( %d n , A[a -  0 ] * A[b -  0 ]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <class A> n void read(vector<A>& v) { n   for (auto& x : v) cin >> x; n } n template <class c> n struct rge { n   c b, e; n }; n template <class c> n rge<c> range(c i, c j) { n   return rge<c>{i, j}; n } n template <class c> n auto dud(c* x) -> decltype(cout << *x, 0); n template <class c> n char dud(...); n struct debug { n   ~debug() { cout << endl; } n   template <class c> n   typename enable_if<sizeof dud<c>(0) != 1, debug&>::type operator<<(c i) { n     cout << boolalpha << i; n     return *this; n   } n   template <class c> n   typename enable_if<sizeof dud<c>(0) == 1, debug&>::type operator<<(c i) { n     return *this << range(begin(i), end(i)); n   } n   template <class c, class b> n   debug& operator<<(pair<b, c> d) { n     return *this <<  (  << d.first <<  ,   << d.second <<  ) ; n   } n   template <class c> n   debug& operator<<(rge<c> d) { n     *this <<  [ ; n     for (auto it = d.b; it != d.e; ++it) *this <<  ,   + 2 * (it == d.b) << *it; n     return *this <<  ] ; n   } n }; n const long MOD = 1000000007; n int main() { n   ios::sync_with_stdio(false); n   cin.tie(nullptr); n   cout.tie(nullptr); n   ; n   int t; n   cin >> t; n   while (t--) { n     int n; n     cin >> n; n     int o = 0, z = 0; n     for (int _ = 0; _ < n; _++) { n       int v; n       cin >> v; n       if (v == 1) n         o++; n       else n         z++; n     } n     int times, val; n     if (o > z) { n       times = o; n       val = 1; n     } else { n       times = z; n       val = 0; n     } n     if (val == 1 && times % 2 && val == 1) times--; n     cout << times << endl; n     for (int _ = 0; _ < times; _++) cout << val <<    ; n     cout << endl; n   } n } n


#include <bits/stdc++.h> n int min(int a, int b) { return (a < b ? a : b); } n int max(int a, int b) { return (a > b ? a : b); } n int main() { n   char a[10], b[10]; n   int x, y; n   scanf( %s n%s , a, b); n   if (strcmp(a,  sunday ) == 0) n     x = 0; n   else if (strcmp(a,  monday ) == 0) n     x = 1; n   else if (strcmp(a,  tuesday ) == 0) n     x = 2; n   else if (strcmp(a,  wednesday ) == 0) n     x = 3; n   else if (strcmp(a,  thursday ) == 0) n     x = 4; n   else if (strcmp(a,  friday ) == 0) n     x = 5; n   else if (strcmp(a,  saturday ) == 0) n     x = 6; n   if (strcmp(b,  sunday ) == 0) n     y = 0; n   else if (strcmp(b,  monday ) == 0) n     y = 1; n   else if (strcmp(b,  tuesday ) == 0) n     y = 2; n   else if (strcmp(b,  wednesday ) == 0) n     y = 3; n   else if (strcmp(b,  thursday ) == 0) n     y = 4; n   else if (strcmp(b,  friday ) == 0) n     y = 5; n   else if (strcmp(b,  saturday ) == 0) n     y = 6; n   if (x == y) n     printf( YES ); n   else if (y == (x + 2) % 7) n     printf( YES ); n   else if (y == (x + 3) % 7) n     printf( YES ); n   else n     printf( NO ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n string s, c; n vector<string> v, s1, s2; n bool b1(string &d) { n   if (d.size()) { n     if (d[0] ==  0  && d.size() == 1) { n       return true; n     } else if (d[0] ==  0 ) { n       return false; n     } else { n       bool ans = true; n       for (int i = 0; i < d.size(); ++i) { n         if (!( 0  <= d[i] && d[i] <=  9 )) { n           ans = false; n           break; n         } n       } n       return ans; n     } n   } else { n     return false; n   } n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(nullptr); n   cout.precision(11); n   cout.setf(ios::fixed); n   getline(cin, s); n   c =   ; n   for (int i = 0; i < s.size(); ++i) { n     if (s[i] ==  ,  || s[i] ==  ; ) { n       if (b1(c)) { n         s1.push_back(c); n         c =   ; n       } else { n         s2.push_back(c); n         c =   ; n       } n     } else { n       c.append(1, s[i]); n     } n   } n   if (b1(c)) { n     s1.push_back(c); n     c =   ; n   } else { n     s2.push_back(c); n     c =   ; n   } n   if (s1.size()) { n     cout <<     << s1[0]; n     for (int i = 1; i < s1.size(); ++i) { n       cout <<  ,  << s1[i]; n     } n     cout <<     << endl; n   } else { n     cout <<  -  << endl; n   } n   if (s2.size()) { n     cout <<     << s2[0]; n     for (int i = 1; i < s2.size(); ++i) { n       cout <<  ,  << s2[i]; n     } n     cout <<     << endl; n   } else { n     cout <<  -  << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n map<long long, int> m; n int main() { n   int n; n   scanf( %d , &n); n   long long count = 0; n   int flag = 0; n   for (int i = 0; i < n; i++) { n     long long c; n     cin >> c; n     if (c > 0) { n       m[c] += 1; n       if (m[c] == 2) count++; n       if (m[c] > 2) flag = 1; n     } n   } n   if (flag) n     cout <<  -1  << endl; n   else n     cout << count << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int vis[100005]; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   int n; n   cin >> n; n   int dis[n][n]; n   vector<int> arr(n); n   for (int i = 0; i < n; i++) n     for (int j = 0; j < n; j++) cin >> dis[i][j]; n   for (int i = 0; i < n; i++) cin >> arr[i]; n   reverse(arr.begin(), arr.end()); n   vector<long long> ans; n   for (int k = 0; k < n; k++) { n     int x = arr[k] - 1; n     vis[x] = 1; n     for (int i = 0; i < n; i++) { n       for (int j = 0; j < n; j++) { n         dis[i][j] = min(dis[i][j], dis[i][x] + dis[x][j]); n       } n     } n     long long sum = 0; n     for (int i = 0; i < n; i++) { n       if (!vis[i]) continue; n       for (int j = 0; j < n; j++) n         if (vis[j]) sum += dis[i][j]; n     } n     ans.push_back(sum); n   } n   reverse(ans.begin(), ans.end()); n   for (int i = 0; i < ans.size(); i++) cout << ans[i] <<    ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long mod = 1e9 + 7; n int main() { n   long long n = 1, k = 1; n   int t; n   cin >> t; n   for (int i = 1; i <= t; i++) { n     n %= mod; n     k %= mod; n     n *= 27; n     k *= 7; n   } n   while (n < k) n += mod; n   n -= k; n   cout << n % mod << endl; n } n


#include <bits/stdc++.h> n using namespace std; n long long int arr[2005]; n long long int dp[2005][2005]; n long long int n, h; n long long int calc(long long int id, long long int prev) { n   if (id == n + 1) { n     return !prev; n   } n   long long int ans = dp[id][prev]; n   if (ans != -1) { n     return ans; n   } n   long long int val = arr[id] + prev; n   ans = 0; n   if (val == h) { n     ans = calc(id + 1, prev); n     if (prev) { n       ans = (ans + (prev * calc(id + 1, prev - 1)) % 1000000007) % 1000000007; n     } n   } else if (val == h - 1) { n     ans = calc(id + 1, prev + 1); n     ans = (ans + calc(id + 1, prev)) % 1000000007; n     ans = (ans + (prev * calc(id + 1, prev)) % 1000000007) % 1000000007; n   } n   return (dp[id][prev] = ans); n } n int main() { n   memset(dp, -1, sizeof(dp)); n   scanf( %lld , &n); n   scanf( %lld , &h); n   long long int i; n   for (i = 1; i <= n; i++) { n     scanf( %lld , &arr[i]); n   } n   long long int ans = calc(1, 0); n   printf( %lld , ans); n   ; n   printf(  n ); n   return 0; n } n


#include <bits/stdc++.h> n int n, m, H, T, x, y, l, Mark; n int son[200005], ed[200005], next[200005], v[200005], d[200005]; n void work(int x, int y) { n   ++Mark; n   int h = 0, t = 0; n   for (int p = son[x]; p; p = next[p]) n     if (ed[p] != y) v[ed[p]] = Mark, h++; n   for (int p = son[y]; p; p = next[p]) n     if (ed[p] != x) n       if (v[ed[p]] != Mark) n         t++; n       else if (h > H) n         v[ed[p]] = 0, h--, t++; n   if (h >= H && t >= T) { n     printf( YES n%d %d n , x, y); n     for (int p = son[x]; p && H; p = next[p]) n       if (ed[p] != y) n         if (v[ed[p]] == Mark) printf( %d%s , ed[p], --H ?     :   n ); n     for (int p = son[y]; p && T; p = next[p]) n       if (ed[p] != x) n         if (v[ed[p]] != Mark) printf( %d%s , ed[p], --T ?     :   n ); n     exit(0); n   } n } n int main() { n   scanf( %d%d%d%d , &n, &m, &H, &T); n   for (int i = 1; i <= m; i++) n     scanf( %d%d , &x, &y), d[x]++, d[y]++, ed[++l] = y, next[l] = son[x], n                                            son[x] = l, ed[++l] = x, n                                            next[l] = son[y], son[y] = l; n   for (int i = 1; i <= n; i++) n     if (d[i] > H) n       for (int p = son[i]; p; p = next[p]) n         if (d[ed[p]] > T) work(i, ed[p]); n   printf( NO n ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n  n typedef long long LL; n  n const int maxn = 51; n  n LL f[maxn][2][maxn * maxn]; n  n int n, mod; n  n void solve() { n     scanf( %d%d , &n, &mod); n     int M = n * (n - 1) / 2; n  n     memset(f, 0, sizeof f); n     f[0][0][M] = 1 % mod; n     for (int i = 0; i < n; i++) { n         for (int r = 0; r <= M + M; r++) { n             if (f[i][0][r]) { n                 for (int k1 = 0; k1 < n - i; k1++) n                     for (int k2 = k1; k2 < n - i; k2++) { n                         if (k1 == k2) n                             (f[i + 1][0][r] += f[i][0][r]) %= mod; n                         else n                             (f[i + 1][1][r + k1 - k2] += f[i][0][r]) %= mod; n                     } n             } n  n             if (f[i][1][r]) { n                 for (int k1 = 0; k1 < n - i; k1++) n                     for (int k2 = 0; k2 < n - i; k2++) n                         (f[i + 1][1][r + k1 - k2] += f[i][1][r]) %= mod; n             } n         } n     } n     // for (int r = M; r <= 2 * M; r++) { n     //     cout << f[n][1][r] <<    ; n     // } n     // cout << endl; n  n     LL res = 0; n     for (int r = M + 1; r <= M + n * (n - 1) / 2; r++) { n         (res += f[n][1][r]) %= mod; n     } n     printf( %lld n , res); n } n int main() { n     int _ = 1; n     while (_--) { n         solve(); n     } n     return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long int n, m, xx; n   cin >> n >> m >> xx; n   map<char, long long int> mp; n   vector<char> v[n]; n   for (long long int i = 0; i < n; i++) { n     string s; n     cin >> s; n     for (auto j : s) v[i].push_back(j); n     for (long long int j = 0; j < m; j++) mp[s[j]]++; n   } n   if (mp[ S ] > 0) { n     mp[ S ] = 0; n     for (auto i : mp) { n       char c = i.first - 32; n       if (i.first !=  S ) mp[c]++; n     } n   } n   long long int q; n   cin >> q; n   string s; n   cin >> s; n   for (long long int i = 0; i < q; i++) { n     if (mp[s[i]] == 0) { n       cout <<  -1 ; n       return 0; n     } n   } n   vector<pair<long long int, long long int>> shift; n   for (long long int i = 0; i < n; i++) { n     for (long long int j = 0; j < m; j++) { n       if (v[i][j] ==  S ) shift.push_back({i, j}); n     } n   } n   vector<long long int> chk[200]; n   for (long long int i = 0; i < n; i++) { n     for (long long int j = 0; j < m; j++) { n       for (auto m : shift) { n         long long int x = m.first; n         long long int y = m.second; n         long long int ans = ((x - i) * (x - i)) + ((y - j) * (y - j)); n         if (ans <= (xx * xx)) chk[v[i][j] - 32].push_back(ans); n       } n     } n   } n   long long int c = 0; n   for (long long int i = 0; i < q; i++) { n     if (s[i] >=  A  && s[i] <=  Z ) { n       for (auto j : chk[s[i]]) { n         if (j <= (xx * xx)) goto done; n       } n       c++; n     } n   done: n     1; n   } n   cout << c; n } n


#include <bits/stdc++.h> n using namespace std; n void make_unique(vector<long long> vec) { n   sort((vec).begin(), (vec).end()); n   vec.resize(unique((vec).begin(), (vec).end()) - vec.begin()); n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   int t; n   cin >> t; n   while (t--) { n     long long k, n, m, i, j, flag = 0, count = 0, move = 0; n     string s; n     cin >> n; n     long long A[n]; n     vector<long long> v; n     for (i = 0; i < n; i++) cin >> A[i]; n     long long freq[51] = {0}; n     for (i = 0; i < n; i++) { n       freq[A[i]]++; n     } n     long long maxf = freq[0]; n     long long pos = 0; n     vector<long long> sum(105, 0); n     for (i = 0; i <= n; i++) { n       for (j = i; j <= n; j++) { n         if (i == j) n           sum[i + j] += freq[i] / 2; n         else { n           sum[i + j] += min(freq[i], freq[j]); n         } n       } n     } n     cout << *max_element((sum).begin(), (sum).end()) << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int nmax = 52, inf = int(1e9), mod = int(1e9) + 7; n int n, K; n int dp[2][nmax][nmax], ways[2][nmax][nmax]; n int comb[nmax][nmax]; n int t[2]; n int main() { n   cin >> n >> K; n   int w; n   for (int i = 0; i < (int)n; ++i) { n     cin >> w; n     t[w != 50]++; n   } n   for (int k = 0; k < (int)2; ++k) { n     for (int i = 0; i < (int)nmax; ++i) { n       for (int j = 0; j < (int)nmax; ++j) { n         dp[k][i][j] = inf; n       } n     } n   } n   comb[0][0] = 1; n   for (int i = 1; i < nmax; i++) { n     comb[i][0] = comb[i][i] = 1; n     for (int j = 1; j < i; ++j) { n       comb[i][j] = (comb[i - 1][j] + comb[i - 1][j - 1]) % mod; n     } n   } n   dp[0][0][0] = 0; n   ways[0][0][0] = 1; n   int side = 0; n   for (int moves = 1;; moves++, side ^= 1) { n     int cSide = side ^ 1; n     if (moves == 1) { n       for (int i = 0; i <= t[0]; ++i) { n         for (int j = 0; j <= t[1]; ++j) { n           if ((i > 0 || j > 0) && i * 50 + j * 100 <= K) { n             dp[cSide][i][j] = 1; n             ways[cSide][i][j] = 1ll * comb[t[0]][i] * comb[t[1]][j] % mod; n           } n         } n       } n       continue; n     } n     bool ok = false; n     for (int i = 0; i <= t[0]; ++i) { n       for (int j = 0; j <= t[1]; ++j) { n         if ((!i && !j) || (i == t[0] && j == t[1]) || dp[side][i][j] == inf) n           continue; n         int oi = t[0] - i; n         int oj = t[1] - j; n         for (int k = 0; k <= i; ++k) { n           for (int w = 0; w <= j; ++w) { n             if (k + w > 0 && k * 50 + w * 100 <= K) { n               if (dp[cSide][oi + k][oj + w] > moves) { n                 dp[cSide][oi + k][oj + w] = moves; n                 ways[cSide][oi + k][oj + w] = n                     (1ll * ways[side][i][j] * comb[i][k] % mod) * 1ll * n                     comb[j][w] % mod; n                 ok = true; n               } else if (dp[cSide][oi + k][oj + w] == moves) { n                 ways[cSide][oi + k][oj + w] += n                     (1ll * ways[side][i][j] * comb[i][k] % mod) * 1ll * n                     comb[j][w] % mod; n                 ways[cSide][oi + k][oj + w] %= mod; n               } n             } n           } n         } n       } n     } n     if (!ok) break; n   } n   if (dp[1][t[0]][t[1]] == inf) { n     cout <<  -1 n0 ; n   } else { n     cout << dp[1][t[0]][t[1]] <<   n  << ways[1][t[0]][t[1]]; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long n, x, y; n   map<long long, long long> mx, my; n   map<tuple<long long, long long>, long long> mt; n   scanf( %lld , &n); n   for (int i = 0; i < n; i++) { n     scanf( %lld%lld , &x, &y); n     tuple<long long, long long> a = make_tuple(x, y); n     mx[x]++; n     my[y]++; n     mt[a]++; n   } n   long long ans = 0; n   for (auto a : mx) { n     long long b = a.second; n     ans += b * (b - 1) / 2; n   } n   for (auto a : my) { n     long long b = a.second; n     ans += b * (b - 1) / 2; n   } n   for (auto a : mt) { n     long long b = a.second; n     ans -= b * (b - 1) / 2; n   } n   printf( %lld n , ans); n } n


#include <bits/stdc++.h> n using namespace std; n string str; n void push(stack<pair<bool, int>>& stk, int offset) { n   if (offset == 0) return; n   pair<bool, int> cur; n   if (stk.size() && stk.top().first) n     cur = stk.top(), stk.pop(), stk.push({1, cur.second + offset}); n   else n     stk.push({1, offset}); n } n bool fix_stk(stack<pair<bool, int>>& stk) { n   int offset = 0; n   while (stk.size() && (stk.top().first || stk.top().second + offset < 0)) n     offset += stk.top().second * stk.top().first, stk.pop(); n   if (stk.size() == 0) return false; n   pair<bool, int> cur = {0, stk.top().second + offset}; n   stk.pop(); n   push(stk, offset); n   stk.push(cur); n   return true; n } n bool operation(stack<pair<bool, int>>& stk, int idx) { n   pair<bool, int> cur = {0, -1}; n   if (str[idx] ==  ? ) { n     if (fix_stk(stk) == false) return false; n     cur = stk.top(); n     stk.pop(); n     push(stk, 1); n     stk.push({0, cur.second + 1}); n     if (cur.second - 1 >= 0) stk.push({0, cur.second - 1}); n   } else n     push(stk, (str[idx] ==  ( ) - (str[idx] ==  ) )); n   return true; n } n int solve(int idx) { n   int ret = 0; n   stack<pair<bool, int>> stk; n   stk.push({0, 0}); n   while ((idx < str.size()) && stk.size()) { n     if (operation(stk, idx) == false) return ret; n     if (fix_stk(stk) == false) return ret; n     ret += stk.top().second == 0; n     idx++; n   } n   return ret; n } n int mine() { n   int ret = 0; n   for (int i = 0; i < str.size(); i++) ret += solve(i); n   return ret; n } n int main() { n   cin >> str; n   cout << mine() << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int cp[1000010], n; n int dfs(int first) { n   int second = 0; n   for (int i = 1;; i++) n     if ((1 << i) - 1 >= first) { n       return (1 << i) - 1 ^ first; n     } n } n int main() { n   scanf( %d , &n); n   for (int i = n; i >= 1; i--) n     if (!cp[i]) { n       cp[i] = dfs(i); n       cp[cp[i]] = i; n     } n   long long ans = 0; n   for (int i = n; i >= 0; i--) { n     ans += i ^ cp[i]; n   } n   cout << ans << endl; n   for (int i = 0; i <= n; i++) { n     printf( %d  , cp[i]); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int t; n   cin >> t; n   while (t--) { n     int n, c, b; n     cin >> n >> c >> b; n     int a[n]; n     for (int i = 0; i < n; i++) { n       a[i] = i + 1; n     } n     if (n - 2 < c + b || abs(c - b) > 1) { n       cout <<  -1 ; n     } else { n       if (c == b) { n         for (int i = 1; i <= 2 * c; i += 2) { n           swap(a[i], a[i + 1]); n         } n       } else if (c < b) { n         int j = b; n         for (int i = 0; j > 0; i += 2) { n           swap(a[i], a[i + 1]); n           j--; n         } n       } else if (c > b) { n         int j = c; n         for (int i = n - 1; j > 0; i = i - 2) { n           swap(a[i], a[i - 1]); n           j--; n         } n       } n       for (int i = 0; i < n; i++) { n         cout << a[i] <<    ; n       } n     } n     cout << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n; n int ar[50][50]; n int dp[51][51]; n bool leaf[50]; n vector<int> adj[50]; n vector<int> leafs; n int bad; n vector<int> pos; n int go(int rem, int now) { n   if (rem == 0) { n     return 0; n   } n   if (dp[rem][now] != -1) { n     return dp[rem][now]; n   } n   int low = 0; n   int high = 125000; n   while (low < high) { n     int mid = (low + high + 1) / 2; n     int temp = rem; n     for (int i = 0; i < n; i++) { n       if (!leaf[i] || i == now) { n         continue; n       } n       int cur = 0; n       while (temp > 0 && ar[now][i] + go(rem - cur - 1, i) >= mid) { n         cur++; n         temp--; n       } n     } n     if (temp == 0) { n       low = mid; n     } else { n       high = mid - 1; n     } n   } n   dp[rem][now] = low; n   return low; n } n void dfs(int now, int from) { n   if (leaf[now]) { n     leafs.push_back(now); n   } n   for (int i = 0; i < pos.size(); i++) { n     if (now == pos[i]) { n       bad++; n     } n   } n   for (int i = 0; i < adj[now].size(); i++) { n     int to = adj[now][i]; n     if (to != from) { n       dfs(to, now); n     } n   } n } n int main() { n   std::ios::sync_with_stdio(false); n   cin.tie(0); n   cin >> n; n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < n; j++) { n       if (i == j) { n         ar[i][j] = 0; n       } else { n         ar[i][j] = 25500000; n       } n     } n   } n   int maxw = 0; n   for (int i = 1; i < n; i++) { n     int a, b, w; n     cin >> a >> b >> w; n     a--; n     b--; n     adj[a].push_back(b); n     adj[b].push_back(a); n     ar[a][b] = w; n     ar[b][a] = w; n     maxw = max(maxw, w); n   } n   assert(maxw <= 50); n   for (int k = 0; k < n; k++) { n     for (int i = 0; i < n; i++) { n       for (int j = 0; j < n; j++) { n         ar[i][j] = min(ar[i][j], ar[i][k] + ar[k][j]); n       } n     } n   } n   for (int i = 0; i <= 50; i++) { n     for (int j = 0; j <= 50; j++) { n       dp[i][j] = -1; n     } n   } n   for (int i = 0; i < n; i++) { n     leaf[i] = adj[i].size() == 1; n   } n   int start, m; n   cin >> start >> m; n   start--; n   for (int i = 0; i < m; i++) { n     int x; n     cin >> x; n     x--; n     pos.push_back(x); n   } n   int ans = 125000; n   if (n == 1) { n     assert(false); n     ans = 0; n   } n   for (int i = 0; i < adj[start].size(); i++) { n     leafs.clear(); n     leafs.resize(0); n     bad = 0; n     dfs(adj[start][i], start); n     if (bad == 0) { n       continue; n     } n     int low = 0; n     int high = ans + 2; n     while (low < high) { n       int mid = (low + high + 1) / 2; n       int temp = bad; n       for (int z = 0; z < leafs.size(); z++) { n         int cl = leafs[z]; n         if (cl == start) { n           continue; n         } n         int cur = 0; n         while (temp > 0 && ar[start][cl] + go(m - cur - 1, cl) >= mid) { n           cur++; n           temp--; n         } n       } n       if (temp == 0) { n         low = mid; n       } else { n         high = mid - 1; n       } n     } n     ans = min(ans, low); n   } n   assert(ans != 125000 && ans != 0); n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m, k, a[100001][6], ans[6]; n multiset<int> st[6]; n long long sum() { n   long long ret = 0; n   for (int i = 1; i <= m; i++) ret += *st[i].rbegin(); n   return ret; n } n int main() { n   scanf( %d%d%d , &n, &m, &k); n   for (int i = 1; i <= n; i++) n     for (int t = 1; t <= m; t++) scanf( %d , &a[i][t]); n   long long curr = 0; n   int res = 0; n   for (int i = 1, j = 1; i <= n; i++) { n     for (int t = 1; t <= m; t++) st[t].insert(a[i][t]); n     while (j <= i and sum() > k) { n       for (int t = 1; t <= m; t++) st[t].erase(st[t].find(a[j][t])); n       j++; n     } n     if (i - j + 1 > res) { n       for (int t = 1; t <= m; t++) ans[t] = *st[t].rbegin(); n       res = i - j + 1; n     } n   } n   for (int t = 1; t <= m; t++) printf( %d  , ans[t]); n } n


#include <bits/stdc++.h> n using namespace std; n void think() { n   long long n; n   cin >> n; n   vector<long long> a(n); n   for (long long i = 0; i < n; i++) { n     cin >> a[i]; n   } n   long long ans = 0; n   unordered_map<long long, long long> mp; n   mp[a[0]]++; n   for (long long i = 1; i < n; i++) { n     long long x = a[i]; n     a[i] += a[i - 1]; n     if (mp.find(a[i]) != mp.end() || a[i] == 0) { n       ans++; n       mp.clear(); n       a[i] = x; n       mp[a[i]]++; n     } else { n       mp[a[i]]++; n     } n   } n   cout << ans <<   n ; n } n int32_t main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   ; n   long long testcase = 1; n   while (testcase--) { n     think(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int M = 11, L = 11, N = 1002; n const int Mod = 1e9 + 9; n char a[L], to[99]; n int n; n int dp[2][M * L][N], ch[M * L][4], fail[M * L]; n int len[N]; n int ans = 0; n inline void mo(int& x) { x = (x >= Mod ? x - Mod : x); } n struct AC_automaton { n   int cnt; n   inline void init() { n     cnt = 0; n     to[ A ] = 0, to[ C ] = 1, to[ G ] = 2, to[ T ] = 3; n   } n   inline void insert() { n     int l = strlen(a + 1), now = 0; n     for (int i = 1; i <= l; i++) { n       if (!ch[now][to[a[i]]]) ch[now][to[a[i]]] = ++cnt; n       now = ch[now][to[a[i]]]; n     } n     len[now] = max(len[now], l); n   } n   inline void build() { n     queue<int> Q; n     for (int i = 0; i < 4; i++) n       if (ch[0][i]) Q.push(ch[0][i]); n     while (!Q.empty()) { n       int x = Q.front(); n       Q.pop(); n       len[x] = max(len[x], len[fail[x]]); n       for (int i = 0; i < 4; i++) { n         if (ch[x][i]) n           fail[ch[x][i]] = ch[fail[x]][i], Q.push(ch[x][i]); n         else n           ch[x][i] = ch[fail[x]][i]; n       } n     } n   } n   inline void work() { n     dp[0][0][1] = 1; n     for (int i = 1; i <= n; i++) { n       memset(dp[i & 1], 0, sizeof(dp[i & 1])); n       for (int j = 0; j <= cnt; j++) n         for (int k = 0; k <= i; k++) { n           if (dp[!(i & 1)][j][k]) { n             for (int t = 0; t < 4; t++) { n               if (len[ch[j][t]] + k > i) n                 mo(dp[i & 1][ch[j][t]][i + 1] += dp[!(i & 1)][j][k]); n               else n                 mo(dp[i & 1][ch[j][t]][k] += dp[!(i & 1)][j][k]); n             } n           } n         } n     } n     for (int j = 0; j <= cnt; j++) mo(ans += dp[n & 1][j][n + 1]); n   } n } T; n int main() { n   int m; n   scanf( %d%d , &n, &m); n   T.init(); n   for (int i = 1; i <= m; i++) { n     scanf( %s , a + 1); n     T.insert(); n   } n   T.build(); n   T.work(); n   printf( %d , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n inline int read() { n   int x = 0, y = 0; n   char ch = getchar(); n   while (ch <  0  || ch >  9 ) { n     if (ch ==  - ) y = 1; n     ch = getchar(); n   } n   while (ch >=  0  && ch <=  9 ) n     x = (x << 1) + (x << 3) + (ch ^ 48), ch = getchar(); n   return y ? -x : x; n } n template <typename T> n inline T read() { n   T x = 0; n   int y = 0; n   char ch = getchar(); n   while (ch <  0  || ch >  9 ) { n     if (ch ==  - ) y = 1; n     ch = getchar(); n   } n   while (ch >=  0  && ch <=  9 ) n     x = (x << 1) + (x << 3) + (ch ^ 48), ch = getchar(); n   return y ? -x : x; n } n char s[40]; n long long pw[40] = {-0x3f3f3f3f, 1}, f[40][40]; n bool g[40][40]; n signed main() { n   int n = read(); n   scanf( %s , s + 1); n   for (register int i = 2; i <= n; ++i) pw[i] = pw[i - 1] * 10; n   memset(f, ~0x3f, sizeof f); n   f[(n << 1) + 1][0] = 0; n   for (register int i = n << 1; i; --i) n     for (register int j = 0; j <= min((n << 1) - i + 1, n); ++j) { n       f[i][j] = max(f[i + 1][j - 1] + pw[j] * (s[i] -  0 ), n                     f[i + 1][j] + pw[(n << 1) - i + 1 - j] * (s[i] -  0 )); n       g[i][j] = f[i + 1][j - 1] + pw[j] * (s[i] -  0 ) > n                 f[i + 1][j] + pw[(n << 1) - i + 1 - j] * (s[i] -  0 ); n     } n   for (register int i = 1, j = n; i <= n << 1; ++i) n     putchar(g[i][j] ?  H  :  M ), j -= g[i][j]; n   putchar(  n ); n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e5 + 5; n int n, m, q, i, s, j, k, u, dis[N][350], lazy[350], W[N]; n long long sum[N], arr[N], x, Y[350]; n char c; n vector<int> G[N], F[N]; n bool H[N], is[N][350]; n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   cin >> n >> m >> q; n   for (i = 1; i <= n; i++) cin >> arr[i]; n   for (i = 1; i <= m; i++) { n     cin >> s; n     G[i].resize(s); n     if (s >= 350) H[i] = 1, u++, W[i] = u - 1; n     for (j = 0; j < s; j++) { n       cin >> G[i][j]; n       if (H[i]) is[G[i][j]][u - 1] = 1, sum[u - 1] += arr[G[i][j]]; n     } n   } n   for (i = 1; i <= m; i++) { n     for (int j : G[i]) { n       for (int k = 0; k <= 340; k++) n         if (is[j][k]) dis[i][k]++; n     } n   } n   while (q--) { n     cin >> c >> k; n     if (c ==  ? ) { n       if (H[k] == 0) { n         long long r = 0; n         for (int j : G[k]) r += 1ll * arr[j]; n         for (i = 0; i < 340; i++) r += Y[i] * dis[k][i]; n         cout << r <<   n ; n       } else n         cout << sum[W[k]] <<   n ; n     } else { n       cin >> x; n       if (!H[k]) { n         for (int j : G[k]) arr[j] += x; n         for (i = 0; i < 340; i++) sum[i] += x * dis[k][i]; n       } else { n         for (i = 0; i < 340; i++) sum[i] += 1ll * x * dis[k][i]; n         Y[W[k]] += x; n       } n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n long long odd[200005], even[200005]; n int main() { n   long long n, t, x, y; n   cin >> n; n   long long ara[n + 3]; n   long long sum = 0, cnt = 0; n   for (int i = 1; i <= n; i++) { n     cin >> ara[i]; n     sum += ara[i]; n     if (i % 2 == 0) { n       even[i] = even[i - 1] + ara[i]; n       odd[i] = odd[i - 1]; n     } else { n       odd[i] = odd[i - 1] + ara[i]; n       even[i] = even[i - 1]; n     } n   } n   for (int i = 1; i <= n; i++) { n     long long one = odd[i - 1]; n     one += (even[n] - even[i]); n     long long two = sum - (one + ara[i]); n     if (one == two) cnt++; n   } n   cout << cnt << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(0); n   cout.tie(0); n   cin.tie(0); n   string s; n   cin >> s; n   long long dp[s.length() + 1], mod = 1e9 + 7, f = 0; n   dp[0] = 1; n   dp[1] = 1; n   if (s[0] ==  m  || s[0] ==  w ) f = 1; n   for (long long i = 2; i <= s.length(); i++) { n     if (s[i - 1] ==  m  || s[i - 1] ==  w ) n       f = 1; n     else if ((s[i - 1] ==  n  || s[i - 1] ==  u ) && (s[i - 1] == s[i - 2])) n       dp[i] = (dp[i - 2] + dp[i - 1]) % mod; n     else n       dp[i] = dp[i - 1]; n   } n   if (f) n     cout << 0 << endl; n   else n     cout << dp[s.length()] << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int r1, c1, r2, c2; n   cin >> r1 >> c1 >> r2 >> c2; n   if (r1 == r2 || c1 == c2) n     cout << 1; n   else n     cout << 2; n   cout <<    ; n   if ((r1 + c1) % 2 != (r2 + c2) % 2) n     cout << 0; n   else { n     if (r1 + c1 == r2 + c2 || r1 - c1 == r2 - c2) n       cout << 1; n     else { n       cout << 2; n     } n   } n   cout <<    ; n   cout << max(abs(r1 - r2), abs(c1 - c2)) << endl; n   return 0; n } n


#include <bits/stdc++.h> n #pragma GCC optimize(2,  Ofast ,  inline ,  -fgcse ,  -fgcse-lm ,  -fipa-sra ,   n                       -ftree-pre ,  -ftree-vrp ,  -fpeephole2 ,  -ffast-math ,   n                       -fsched-spec ,  unroll-loops ,  -falign-jumps ,            n                       -falign-loops ,  -falign-labels ,  -fdevirtualize ,        n                       -fcaller-saves ,  -fcrossjumping ,  -fthread-jumps ,       n                       -funroll-loops ) n #pragma GCC target(   n      sse,sse2,sse3,ssse3,sse4.1,sse4.2,avx,avx2,popcnt,tune=native ) n using namespace std; n char BB[1 << 20], *SS = BB, *TT = BB; n inline long long read() { n   long long s = 0; n   char ch = n       (SS == TT && (TT = (SS = BB) + fread(BB, 1, 1 << 20, stdin), SS == TT) n            ? EOF n            : *SS++); n   while (!((ch >=  0 ) && (ch <=  9 ))) n     ch = (SS == TT && (TT = (SS = BB) + fread(BB, 1, 1 << 20, stdin), SS == TT) n               ? EOF n               : *SS++); n   while (((ch >=  0 ) && (ch <=  9 ))) n     s = (s << 3) + (s << 1) + (ch ^ 48), n     ch = (SS == TT && (TT = (SS = BB) + fread(BB, 1, 1 << 20, stdin), SS == TT) n               ? EOF n               : *SS++); n   return s; n } n const int mod = 1000000007; n inline long long che(long long a) { return a >= mod ? a - mod : a; } n inline long long ksm(long long a, long long b) { n   if (b < 0) return 0; n   long long res = 1; n   for (; b; b >>= 1ll, a = a * a % mod) n     if (b & 1ll) res = res * a % mod; n   return res; n } n const int maxn = 2000005; n const int mn = 1505; n inline long long inv(long long a) { return ksm(a, mod - 2); } n long long fac[maxn], inc[maxn], dd[maxn]; n long long C(long long x, long long y) { n   return x <= y ? fac[y] * inc[x] % mod * inc[y - x] % mod : 0; n } n long long ss1[2][mn], ss2[2][mn], sum1[mn], sum2[mn], sum3[mn]; n int main() { n   long long n = read(), m = read(), a = read(), b = read(), k = read(); n   long long ans = 1, anss = 0, an = 0; n   ans = a * inv(b) % mod; n   an = inv(1 - ans + mod); n   fac[0] = 1; n   for (long long i = 1; i <= 2 * k; ++i) fac[i] = fac[i - 1] * i % mod; n   inc[2 * k] = inv(fac[2 * k]); n   for (long long i = 2 * k - 1; i >= 0; --i) n     inc[i] = inc[i + 1] * (i + 1) % mod; n   for (long long i = 0; i <= k; ++i) n     dd[i] = ksm(1 - ans + mod, k - i) * C(i, k) % mod * ksm(ans, i) % mod; n   for (int j = 1; j <= m; ++j) ss1[0][j] = 1; n   sum1[0] = dd[0]; n   for (int i = 1; i <= m; ++i) sum1[i] = che(sum1[i - 1] + dd[i]); n   for (int i = 1; i <= n; ++i) { n     for (int j = m; j >= 1; --j) n       sum2[j] = che(sum2[j + 1] + ss1[(i ^ 1) & 1][j] * dd[m - j] % mod); n     for (int j = 1; j <= m; ++j) n       sum3[j] = n           che(sum3[j - 1] + (mod - ss2[(i ^ 1) & 1][j - 1]) * dd[j - 1] % mod); n     for (int j = 1; j <= m; ++j) { n       ss1[i & 1][j] = 0; n       ss1[i & 1][j] = n           che(ss1[i & 1][j] + n               che(mod + mod - ss2[(i ^ 1) & 1][j - 1]) * sum1[m - j] % mod); n       ss1[i & 1][j] = che(ss1[i & 1][j] + sum2[j]); n       ss1[i & 1][j] = ss1[i & 1][j] * dd[j - 1] % mod; n       ss1[i & 1][j] = che(ss1[i & 1][j] + ss1[i & 1][j - 1]); n     } n     for (int l = 1; l < m; ++l) { n       ss2[i & 1][l] = 0; n       ss2[i & 1][l] = che(ss2[i & 1][l] + sum3[l]); n       ss2[i & 1][l] = n           che(ss2[i & 1][l] + (ss1[(i ^ 1) & 1][l]) * sum1[l - 1] % mod); n       ss2[i & 1][l] = ss2[i & 1][l] * dd[m - l] % mod; n       ss2[i & 1][l] = che(ss2[i & 1][l] + ss2[i & 1][l - 1]); n     } n   } n   printf( %lld n , ss1[n & 1][m]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   string s1, s2, s3; n   cin >> s1 >> s2 >> s3; n   int i; n   s1 = s1.append(s2); n   sort(s1.begin(), s1.end()); n   sort(s3.begin(), s3.end()); n   if (s1.length() != s3.length()) { n     cout <<  NO ; n     return 0; n   } else { n     for (i = 0; i < s1.length(); i++) { n       if (s1[i] != s3[i]) { n         cout <<  NO ; n         return 0; n       } n     } n     cout <<  YES ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, m; n   cin >> n >> m; n   long long ans = 0; n   for (int i = (0); i < (n); ++i) { n     long long t, T, x, cost; n     cin >> t >> T >> x >> cost; n     if (t >= T) { n       ans += cost + m * x; n       continue; n     } n     long long aux1 = cost; n     if (m > (T - t)) aux1 += m * x; n     long long aux2 = (long long)ceil((double)(m - (T - t)) / (T - t)) + 1; n     aux2 *= cost; n     ans += min(aux1, aux2); n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 55; n char str[N][N]; n int vis[N]; n int n, m; n long long msk[N]; n int main() { n   scanf( %d %d , &n, &m); n   for (int i = 0; i < int(n); i++) scanf( %s , str[i]); n   for (int i = 0; i < int(n); i++) n     for (int j = 0; j < int(m); j++) n       if (str[i][j] ==  # ) msk[i] |= (1LL << j); n   for (int i = 0; i < int(n); i++) { n     if (vis[i]) continue; n     vector<int> v; n     for (int j = int(i); j <= int(n - 1); j++) { n       if (msk[i] == msk[j]) n         v.push_back(j); n       else if (msk[i] & msk[j]) { n         puts( No ); n         return 0; n       } n     } n     for (int nxt : v) vis[nxt] = true; n   } n   puts( YES ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int i, n; n   cin >> n; n   char a[n]; n   for (i = 0; i <= n - 1; i++) { n     cin >> a[i]; n   } n   if (a[0] ==  S  && a[n - 1] ==  F ) n     cout <<  YES ; n   else n     cout <<  NO ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct ac { n   int x, y, z; n } p[3000]; n int mo[3000]; n vector<pair<int, int> > pr; n int main() { n   int n; n   scanf( %d , &n); n   for (int i = 1; i <= n; ++i) { n     scanf( %d%d%d , &p[i].x, &p[i].y, &p[i].z); n   } n   for (int i = 1; i <= n; ++i) { n     int d = 1e9, f = 0; n     if (mo[i] == 1) continue; n     for (int j = 1; j <= n; ++j) { n       if (mo[j] == 1 || i == j) continue; n       int td = n           abs(p[i].x - p[j].x) + abs(p[j].y - p[i].y) + abs(p[j].z - p[i].z); n       if (td < d) { n         f = j; n         d = td; n       } n     } n     mo[i] = mo[f] = 1; n     cout << i <<     << f << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long K = 1000000007; n long long gcd(long long a, long long b, long long& x, long long& y) { n   if (a == 0) { n     x = 0; n     y = 1; n     return b; n   } n   long long x1, y1; n   long long d = gcd(b % a, a, x1, y1); n   x = y1 - (b / a) * x1; n   y = x1; n   return d; n } n int32_t main() { n   long long n, m; n   cin >> n >> m; n   long long ans = n + 1 - m; n   for (long long i = 0; i < m; i++) { n     ans *= (2 * n + 2); n     ans %= K; n   } n   long long x, y; n   gcd(n + 1, K, x, y); n   x *= ans; n   x %= K; n   if (x < 0) x += K; n   cout << x << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAX_N = 30; n int a[MAX_N], s[MAX_N]; n int main() { n   ios::sync_with_stdio(false); n   int n; n   cin >> n; n   for (int i = 0; i < n; i++) { n     cin >> a[i]; n     s[i] = a[i]; n   } n   sort(s, s + n); n   map<int, int> spos; n   for (int i = 0; i < n; i++) { n     spos[s[i]] = i; n   } n   for (int i = 0; i < n; i++) { n     cout << s[(spos[a[i]] + 1) % n] <<    ; n   } n   cout << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   int t, n, l, r; n   cin >> t; n   while (t--) { n     cin >> n; n     int a[n]; n     for (int i = 0; i < n; i++) { n       cin >> a[i]; n     } n     for (int i = 0; i < n; i++) { n       if (a[i] == 1) { n         l = i; n         break; n       } n     } n     for (int i = n - 1; i >= 0; i--) { n       if (a[i] == 1) { n         r = i; n         break; n       } n     } n     int ans = 0; n     for (int i = l; i < r + 1; i++) { n       if (a[i] == 0) ans++; n     } n     cout << ans << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int SIZE = 640; n int h, w; n int matr[SIZE][SIZE]; n int main() { n   int q; n   scanf( %d , &q); n   for (int tt = 0; tt < q; tt++) { n     scanf( %d%d , &h, &w); n     for (int i = 0; i < h; i++) n       for (int j = 0; j < w; j++) scanf( %d , &matr[i][j]); n     int cut = h / 2; n     int diffZero = 0; n     int diffSwap = 0; n     for (int j = 0; j < w; j++) { n       diffZero += abs(matr[cut - 1][j] - matr[cut][j]); n       diffSwap += abs(matr[0][j] - matr[h - 1][j]); n     } n     ((void)0); n     printf(diffZero < diffSwap ?  NO n  :  YES n ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const unsigned long long P = 239017, MaxN = 2100000, INF = 1000000000; n int a, arr[1000000], sum[1000000], have[1000000]; n char s[1000000]; n int main() { n   scanf( %d%s , &a, s); n   int len = strlen(s); n   for (int i = 0; i < len; ++i) arr[i] = s[i] -  0 ; n   sum[0] = 0; n   for (int i = 1; i <= len; ++i) sum[i] = sum[i - 1] + arr[i - 1]; n   int S = 0; n   for (int i = 1; i <= len; ++i) n     for (int j = 1; j <= i; ++j) ++have[sum[i] - sum[j - 1]], ++S; n   long long ans = 0; n   for (int i = 1; i <= len; ++i) n     for (int j = 1; j <= i; ++j) { n       if ((sum[i] - sum[j - 1]) && a % (sum[i] - sum[j - 1]) == 0) { n         if ((a / (sum[i] - sum[j - 1])) < 1000000) n           ans += have[a / (sum[i] - sum[j - 1])]; n       } n       if (a == 0 && (sum[i] - sum[j - 1]) == 0) ans += S; n     } n   printf( %I64d , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long int mod = 1e9 + 7; n int main() { n   long long int t, n, ans = 0, count0 = 0, count1 = 0, count2 = 0, num0 = 0, n                       num1 = 0, num2 = 0; n   t = 1; n   while (t--) { n     cin >> n; n     vector<long long int> A(n, 0); n     for (int i = 0; i < n; i++) { n       cin >> A[i]; n     } n     sort(A.begin(), A.end()); n     for (int i = n - 1; i >= 0; i--) { n       if (A[i] > 0) { n         count1 += (A[i] - 1); n         num1++; n       } else if (A[i] == 0) { n         count0++; n         num0++; n       } else { n         count2 += (-1 - A[i]); n         num2++; n       } n     } n     ans += count1; n     if (num2 % 2 == 0) { n       ans += count2; n       ans += count0; n     } else { n       if (num0 > 0) { n         ans += count0; n         ans += count2; n       } else { n         ans += count0; n         ans += (count2 + 2); n       } n     } n     cout << ans << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n unsigned long long mod = 1e9 + 7; n unsigned long long ncr(long long int n, long long int k) { n   unsigned long long res = 1; n   if (k > n - k) k = n - k; n   for (int i = 0; i < k; ++i) { n     res *= (n - i); n     res /= (i + 1); n     res = res % mod; n   } n   return res % mod; n } n bool prime(unsigned long long num) { n   bool flag = true; n   for (unsigned long long i = 2; i * i <= num; i++) { n     if (num % i == 0) { n       flag = false; n       break; n     } n   } n   return flag; n } n long long ipow(long long base, long long exp) { n   long long result = 1; n   for (;;) { n     if (exp & 1) { n       result *= base; n       result = result % mod; n     } n     exp >>= 1; n     if (!exp) break; n     base *= base; n     base = base % mod; n   } n   return result % mod; n } n long long abs(long long a, long long b) { n   if ((a - b) > 0) n     return (a - b); n   else n     return (b - a); n } n int main() { n   ios_base ::sync_with_stdio(false); n   cin.tie(NULL); n   int n, k; n   cin >> n >> k; n   int mx[n + 1], mn[n + 1]; n   memset(mn, -1, sizeof mn); n   memset(mx, -1, sizeof mx); n   for (int i = 0; i < k; i++) { n     int k; n     cin >> k; n     if (mn[k] == -1) mn[k] = i; n     mx[k] = i; n   } n   int ans = 0; n   for (int i = 1; i <= n; i++) { n     if (mn[i] == -1 && i != 1 && i != n) n       ans += 3; n     else if (mn[i] == -1 && (i == 1 || i == n)) n       ans += 2; n     else { n       if (i != 1) { n         if (mn[i - 1] == -1) n           ans++; n         else { n           if (mx[i - 1] < mn[i]) ans++; n         } n       } n       if (i != n) { n         if (mn[i + 1] == -1) n           ans++; n         else { n           if (mx[i + 1] < mn[i]) { n             ans++; n           } n         } n       } n     } n   } n   cout << ans; n } n


#include <bits/stdc++.h> n using namespace std; n long long bin_pow(long long x, long long e, long long MOD) { n   long long res = 1; n   while (e) { n     if (e & 1) res = res * x % MOD; n     e >>= 1; n     x = x * x % MOD; n   } n   return res; n } n long long p, a, b, x, ans; n vector<long long> m[1123456]; n long long xd[1123456]; n long long xy[1123456]; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cin >> a >> b >> p >> x; n   for (int i = 1; i < p; i++) { n     xd[i] = bin_pow(i, p - 2, p); n     xy[i] = bin_pow(a, i, p); n   } n   for (int i = 0; i < p - 1; i++) { n     m[bin_pow(a, i, p)].push_back(i); n   } n   for (int i = 1; i < p; i++) { n     vector<long long> g = m[((b * xd[i]) % p) * xd[xy[i]] % p]; n     for (auto k : g) { n       long long y = x - i; n       if (y < 0) continue; n       y /= p; n       long long t = y / (p - 1); n       if (t * (p - 1) + k <= y) n         ans += t + 1; n       else n         ans += t; n     } n   } n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m, q, ans[600005]; n bitset<505> a[505][505], b[505][505]; n char mp[505][505]; n struct ccf { n   int x1, y1, x2, y2, id; n }; n void solve(int l, int r, vector<ccf> qus) { n   if (!qus.size()) return; n   int mid = (l + r) >> 1; n   for (int i = mid; i >= l; i--) { n     for (int j = m; j >= 1; j--) { n       a[i][j].reset(); n       if (i == mid) a[mid][j][j] = (mp[mid][j] ==  . ); n       if (i != mid && mp[i + 1][j] ==  . ) a[i][j] |= a[i + 1][j]; n       if (j != m && mp[i][j + 1] ==  . ) a[i][j] |= a[i][j + 1]; n     } n   } n   for (int i = mid; i <= r; i++) { n     for (int j = 1; j <= m; j++) { n       b[i][j].reset(); n       if (i == mid) b[mid][j][j] = (mp[mid][j] ==  . ); n       if (i != mid && mp[i - 1][j] ==  . ) b[i][j] |= b[i - 1][j]; n       if (j != 1 && mp[i][j - 1] ==  . ) b[i][j] |= b[i][j - 1]; n     } n   } n   vector<ccf> askl, askr; n   int len = qus.size(); n   for (int i = 0; i < len; i++) { n     if (qus[i].x2 < mid) n       askl.push_back(qus[i]); n     else if (qus[i].x1 > mid) n       askr.push_back(qus[i]); n     else n       ans[qus[i].id] = n           (a[qus[i].x1][qus[i].y1] & b[qus[i].x2][qus[i].y2]).any(); n   } n   solve(l, mid - 1, askl); n   solve(mid + 1, r, askr); n } n vector<ccf> eve; n int main() { n   scanf( %d%d , &n, &m); n   for (int i = 1; i <= n; i++) cin >> (mp[i] + 1); n   scanf( %d , &q); n   for (int i = 1; i <= q; i++) { n     ccf qus; n     qus.id = i; n     scanf( %d%d%d%d , &qus.x1, &qus.y1, &qus.x2, &qus.y2); n     eve.push_back(qus); n   } n   solve(1, n, eve); n   for (int i = 1; i <= q; i++) n     if (ans[i]) n       puts( Yes ); n     else n       puts( No ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int t, n, ans, lcp[5009][5009], dp[5009]; n char a[5009]; n int main() { n   scanf( %d , &t); n   while (t--) { n     scanf( %d , &n); n     scanf( %s , a + 1); n     ans = 0; n     for (int i = n; i >= 1; i--) n       for (int j = n; j >= 1; j--) n         lcp[i][j] = (a[i] == a[j]) ? (lcp[i + 1][j + 1] + 1) : 0; n     for (int i = 1; i <= n; i++) { n       dp[i] = n + 1 - i; n       for (int j = 1; j < i; j++) { n         int len = lcp[i][j]; n         if (a[j + len] < a[i + len]) n           dp[i] = max(dp[i], dp[j] + n + 1 - (i + len)); n       } n       ans = max(ans, dp[i]); n     } n     printf( %d n , ans); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int M = 1010; n int n; n vector<pair<int, int> > pt; n vector<int> vx, vy; n vector<int> gx[M], gy[M]; n vector<tuple<int, int, int> > sh, sv; n int pa[M], pb[M], ln, lm; n vector<int> v[M]; n bool used[M], used1[M]; n vector<int> dx[M], dy[M]; n bool dfs(int p) { n   used[p] = true; n   for (int i : v[p]) { n     if (pb[i] == -1 || (!used[pb[i]] && dfs(pb[i]))) { n       pa[p] = i; n       pb[i] = p; n       return true; n     } n   } n   return false; n } n void dfs_set(int p) { n   used[p] = true; n   for (int i : v[p]) { n     used1[i] = true; n     if (pb[i] != -1 && !used[pb[i]]) dfs_set(pb[i]); n   } n } n void kuhn() { n   memset(pa, -1, sizeof(pa)); n   memset(pb, -1, sizeof(pb)); n   for (int i = 0; i < ln; i++) { n     memset(used, 0, sizeof(used)); n     if (dfs(i)) continue; n   } n   memset(used, 0, sizeof(used)); n   memset(used1, 0, sizeof(used1)); n   for (int i = 0; i < ln; i++) n     if (pa[i] == -1) dfs_set(i); n   for (int i = 0; i < ln; i++) used[i] = (!used[i]); n } n int main() { n   ios::sync_with_stdio(0); n   cin >> n; n   for (int i = 0; i < n; i++) { n     int x, y; n     cin >> x >> y; n     vx.push_back(x); n     vy.push_back(y); n     pt.push_back(make_pair(x, y)); n   } n   sort(vx.begin(), vx.end()); n   vx.resize(unique(vx.begin(), vx.end()) - vx.begin()); n   sort(vy.begin(), vy.end()); n   vy.resize(unique(vy.begin(), vy.end()) - vy.begin()); n   for (int i = 0; i < n; i++) { n     pt[i].first = lower_bound(vx.begin(), vx.end(), pt[i].first) - vx.begin(); n     pt[i].second = lower_bound(vy.begin(), vy.end(), pt[i].second) - vy.begin(); n   } n   for (int i = 0; i < n; i++) { n     gx[pt[i].first].push_back(pt[i].second); n     gy[pt[i].second].push_back(pt[i].first); n   } n   for (int i = 0; i < n; i++) sort(gx[i].begin(), gx[i].end()); n   for (int i = 0; i < n; i++) sort(gy[i].begin(), gy[i].end()); n   for (int y = 0; y < n; y++) n     if ((int)gy[y].size() > 1) n       for (int i = 0; i < (int)gy[y].size() - 1; i++) n         sh.push_back(make_tuple(gy[y][i], gy[y][i + 1], y)); n   for (int x = 0; x < n; x++) n     if ((int)gx[x].size() > 1) n       for (int i = 0; i < (int)gx[x].size() - 1; i++) n         sv.push_back(make_tuple(gx[x][i], gx[x][i + 1], x)); n   sort(sh.begin(), sh.end()); n   sort(sv.begin(), sv.end()); n   ln = sh.size(); n   lm = sv.size(); n   for (int i = 0; i < ln; i++) n     for (int j = 0; j < lm; j++) { n       if (!(get<0>(sh[i]) < get<2>(sv[j]) && get<2>(sv[j]) < get<1>(sh[i]))) n         continue; n       if (!(get<0>(sv[j]) < get<2>(sh[i]) && get<2>(sh[i]) < get<1>(sv[j]))) n         continue; n       v[i].push_back(j); n     } n   kuhn(); n   for (int y = 0; y < n; y++) n     if ((int)gy[y].size() >= 1) dy[y].push_back(gy[y][0]); n   for (int x = 0; x < n; x++) n     if ((int)gx[x].size() >= 1) dx[x].push_back(gx[x][0]); n   for (int i = 0; i < ln; i++) n     if (used[i]) { n       dy[get<2>(sh[i])].push_back(get<0>(sh[i])); n       dy[get<2>(sh[i])].push_back(get<1>(sh[i])); n     } n   for (int i = 0; i < lm; i++) n     if (used1[i]) { n       dx[get<2>(sv[i])].push_back(get<0>(sv[i])); n       dx[get<2>(sv[i])].push_back(get<1>(sv[i])); n     } n   for (int y = 0; y < n; y++) n     if ((int)gy[y].size() >= 1) dy[y].push_back(gy[y].back()); n   for (int x = 0; x < n; x++) n     if ((int)gx[x].size() >= 1) dx[x].push_back(gx[x].back()); n   int ans = 0; n   for (int y = 0; y < n; y++) ans += (int)dy[y].size(); n   ans /= 2; n   cout << ans <<   n ; n   for (int y = 0; y < n; y++) n     for (int i = 0; i < (int)dy[y].size(); i += 2) n       cout << vx[dy[y][i]] <<     << vy[y] <<     << vx[dy[y][i + 1]] <<     n            << vy[y] <<   n ; n   ans = 0; n   for (int x = 0; x < n; x++) ans += (int)dx[x].size(); n   ans /= 2; n   cout << ans <<   n ; n   for (int x = 0; x < n; x++) n     for (int i = 0; i < (int)dx[x].size(); i += 2) n       cout << vx[x] <<     << vy[dx[x][i]] <<     << vx[x] <<     n            << vy[dx[x][i + 1]] <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = int(5e5) + 100; n int n, m; n vector<pair<int, int> > out[maxn]; n int fa[maxn]; n long long f[maxn], g[maxn], a[maxn]; n void read() { n   scanf( %d%d , &n, &m); n   for (int i = 1; i <= n; ++i) out[i].clear(); n   for (int i = 1; i < n; ++i) { n     int u, v, d; n     scanf( %d%d%d , &u, &v, &d); n     out[u].push_back(make_pair(v, d)); n     out[v].push_back(make_pair(u, d)); n   } n } n void dfs(int cur) { n   f[cur] = g[cur] = 0; n   for (auto &to : out[cur]) n     if (to.first != fa[cur]) { n       fa[to.first] = cur; n       dfs(to.first); n       f[cur] += f[to.first]; n       g[cur] += f[to.first]; n     } n   int cnt = 0; n   for (auto &to : out[cur]) n     if (to.first != fa[cur]) a[++cnt] = to.second + g[to.first] - f[to.first]; n   sort(a + 1, a + 1 + cnt, greater<long long>()); n   int fin = min(m - 1, cnt); n   for (int i = 1; i <= fin; ++i) n     g[cur] += max(0LL, a[i]), f[cur] += max(a[i], 0LL); n   if (m <= cnt) f[cur] += max(0LL, a[m]); n } n void solve() { n   dfs(1); n   printf( %lld n , f[1]); n } n int main() { n   int casesum; n   scanf( %d , &casesum); n   while (casesum--) { n     read(); n     solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n inline int read() { n   int f = 1, x = 0; 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 * 10 + ch -  0 ; n     ch = getchar(); n   } n   return f * x; n } n int q, n, k, ans, f[200000 + 1], p[200000 + 1], v[3]; n char s[200000 + 1]; n int main() { n   q = read(); n   while (q--) { n     n = read(), k = read(); n     scanf( %s , s + 1); n     for (int i = 1; i <= n; i++) { n       if (s[i] ==  R ) n         f[i] = 2; n       else if (s[i] ==  G ) n         f[i] = 1; n       else n         f[i] = 0; n       p[i] = (f[i] + i) % 3; n     } n     for (int i = 1; i < k; i++) v[p[i]]++; n     ans = 2147483647; n     for (int i = k; i <= n; i++) { n       v[p[i]]++; n       for (int j = 0; j <= 2; j++) ans = std::min(ans, k - v[j]); n       v[p[i - k + 1]]--; n     } n     for (int i = n - k + 2; i <= n; i++) v[p[i]]--; n     printf( %d n , ans); n   } n } n


#include <bits/stdc++.h> n using namespace std; n int n, k; n pair<long long, long long> a[100007]; n int r[100007]; n int c[100007]; n int used[100007]; n long long ans = -1; n bool cmp1(int x, int y) { return (a[x].first < a[y].first); } n bool cmp2(int x, int y) { return (a[x].second < a[y].second); } n void input() { n   cin >> n >> k; n   int i; n   long long xx1, yy1, xx2, yy2; n   for (i = 1; i <= n; i++) { n     cin >> xx1 >> yy1 >> xx2 >> yy2; n     xx1 *= 2; n     yy1 *= 2; n     xx2 *= 2; n     yy2 *= 2; n     a[i].first = (xx1 + xx2) / 2; n     a[i].second = (yy1 + yy2) / 2; n     r[i] = c[i] = i; n   } n } n void solve() { n   sort(r + 1, r + n + 1, cmp1); n   sort(c + 1, c + n + 1, cmp2); n   int i, j; n   int mask; n   int lim = (2 << (2 * k)); n   vector<int> v; n   for (mask = 0; mask < lim; mask++) { n     int u = mask; n     v.clear(); n     for (j = 0; j < k; j++) { n       int ost = (u % 4); n       u /= 4; n       if (ost == 0) { n         for (i = 1; i <= n; i++) { n           if (used[r[i]] == 1) { n             continue; n           } n           used[r[i]] = 1; n           v.push_back(r[i]); n           break; n         } n       } else if (ost == 1) { n         for (i = n; i >= 1; i--) { n           if (used[r[i]] == 1) { n             continue; n           } n           used[r[i]] = 1; n           v.push_back(r[i]); n           break; n         } n       } else if (ost == 2) { n         for (i = 1; i <= n; i++) { n           if (used[c[i]] == 1) { n             continue; n           } n           used[c[i]] = 1; n           v.push_back(c[i]); n           break; n         } n       } else { n         for (i = n; i >= 1; i--) { n           if (used[c[i]] == 1) { n             continue; n           } n           used[c[i]] = 1; n           v.push_back(c[i]); n           break; n         } n       } n     } n     long long xx1, yy1, xx2, yy2; n     xx1 = xx2 = yy1 = yy2 = 0; n     for (i = 1; i <= n; i++) { n       if (used[r[i]] == 0) { n         xx1 = a[r[i]].first; n         break; n       } n     } n     for (i = n; i >= 1; i--) { n       if (used[r[i]] == 0) { n         xx2 = a[r[i]].first; n         break; n       } n     } n     for (i = 1; i <= n; i++) { n       if (used[c[i]] == 0) { n         yy1 = a[c[i]].second; n         break; n       } n     } n     for (i = n; i >= 1; i--) { n       if (used[c[i]] == 0) { n         yy2 = a[c[i]].second; n         break; n       } n     } n     if (xx1 == xx2) { n       if (k != n) { n         xx2++; n       } n     } n     if (yy1 == yy2) { n       if (k != n) { n         yy2++; n       } n     } n     long long s = (xx2 - xx1 + 1) / 2; n     s *= ((yy2 - yy1 + 1) / 2); n     if (ans == -1 || ans > s) { n       ans = s; n     } n     for (i = 0; i < k; i++) { n       used[v[i]] = 0; n     } n   } n   cout << ans <<   n ; n } n int main() { n   ios::sync_with_stdio(false); n   cin.tie(NULL); n   input(); n   solve(); n   return 0; n } n


#include <bits/stdc++.h> n char s[100005]; n std::vector<int> v; n int main() { n   scanf( %s , s); n   int n = strlen(s); n   bool pc = 0; n   long long now = 0, pre = 0; n   bool first = true; n   for (int i = 0; i < n; i++) { n     if (s[i] <=  9  && s[i] >=  0 ) { n       now = now * 10 + s[i] -  0 ; n     } else { n       if (first) { n         first = false; n         pre = now; n       } else { n         if (pc) n           pre = pre + now; n         else n           pre = pre - now; n       } n       pc = (s[i] ==  + ); n       now = 0; n     } n   } n   if (pc) n     pre = pre + now; n   else n     pre = pre - now; n   if (pre == 0) v.push_back(0); n   while (pre) { n     v.push_back(pre % 10); n     pre /= 10; n   } n   for (int i = v.size() - 1; i >= 0; i--) { n     for (int j = 0; j < v[i] +  0 ; j++) printf( + ); n     printf( . ); n     if (i != 0) n       printf( > ); n     else n       printf(  n ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n map<long long, string> dic; n int main() { n   long long n; n   scanf( %lld , &n); n   for (long long i = 2; (long long)i * i <= n; i++) { n     for (long long j = i * i, p = 2; j <= n; j *= i, p++) { n       string str = to_string(i) +  ^  + to_string(p); n       if (dic.find(j) == dic.end()) n         dic[j] = str; n       else if (dic[j].size() > str.size()) n         dic[j] = str; n     } n   } n   string ans = to_string(n); n   for (auto it = dic.begin(); it != dic.end(); it++) { n     long long v = it->first; n     string str = it->second; n     long long mul = n / v; n     long long add = n % v; n     string all_str; n     if (mul > 1) { n       string mul_str = to_string(mul); n       if (dic.find(mul) != dic.end() && dic[mul].size() < mul_str.size()) n         all_str += dic[mul] +  * ; n       else n         all_str += mul_str +  * ; n     } n     all_str += str; n     if (add > 0) { n       all_str +=  + ; n       string add_str = to_string(add); n       if (dic.find(add) != dic.end() && dic[add].size() < add_str.size()) n         all_str += dic[add]; n       else n         all_str += add_str; n     } n     if (all_str.size() < ans.size()) ans = all_str; n   } n   puts(ans.c_str()); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n list<int> adjList[200002]; n int depth[200002]; n int sz[200002]; n class LCA_binary { n   int logn, timer; n   vector<int> tin, tout; n   vector<vector<int>> up; n  n  public: n   void innerdfs(int v, int p) { n     tin[v] = ++timer; n     up[v][0] = p; n     for (int i = 1; i <= logn; ++i) up[v][i] = up[up[v][i - 1]][i - 1]; n     for (int u : adjList[v]) { n       if (u != p) innerdfs(u, v); n     } n     tout[v] = ++timer; n   } n   bool is_ancestor(int u, int v) { n     return tin[u] <= tin[v] && tout[u] >= tout[v]; n   } n   int lca(int u, int v) { n     if (is_ancestor(u, v)) return u; n     if (is_ancestor(v, u)) return v; n     for (int i = logn; i >= 0; --i) { n       if (!is_ancestor(up[u][i], v)) u = up[u][i]; n     } n     return up[u][0]; n   } n   void preprocess(int n, int root) { n     tin.resize(n + 3); n     tout.resize(n + 3); n     timer = 0; n     logn = ceil(log2(n)); n     up.assign(n + 3, vector<int>(logn + 1)); n     innerdfs(root, root); n   } n }; n set<int> s; n vector<int> univ; n int deep(int i, int p, int d) { n   depth[i] = d++; n   if (s.count(i)) univ.push_back(i); n   sz[i] = 1; n   for (int v : adjList[i]) n     if (v != p) sz[i] += deep(v, i, d); n   return sz[i]; n } n int main() { n   int n, k; n   cin >> n >> k; n   for (int i = 0; i < (k << 1); i++) { n     int t; n     cin >> t; n     s.insert(t); n   } n   for (int i = 1; i < n; i++) { n     int a, b; n     cin >> a >> b; n     adjList[a].push_back(b); n     adjList[b].push_back(a); n   } n   LCA_binary f; n   f.preprocess(n, 1); n   deep(1, 0, 0); n   long long ans = 0LL; n   for (int i = 0; i < k; i++) n     ans += (long long)(depth[univ[i]] + depth[univ[i + k]] - n                        (depth[f.lca(univ[i], univ[i + k])] << 1)); n   cout << ans <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int arr[1000], ans = 0; n int cnt[1000]; n int main() { n   int n; n   scanf( %d , &n); n   priority_queue<int> pq; n   for (int i = 0; i < n; ++i) { n     int x; n     scanf( %d , &x); n     pq.push(-x); n   } n   for (int i = 0; i < 1000; ++i) { n     arr[i] = 0; n     cnt[i] = 0; n   } n   while (!pq.empty()) { n     int u = -pq.top(); n     pq.pop(); n     int p = -1, smallest = 100000; n     for (int i = 0; i < ans; ++i) n       if (arr[i] < smallest && cnt[i] <= u) { n         smallest = cnt[i]; n         p = i; n       } n     if (p == -1) { n       ++cnt[ans]; n       arr[ans++] = u; n     } else { n       ++cnt[p]; n       arr[p] = u; n     } n   } n   printf( %d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MaxN = 100010; n int n, m, k; n map<int, vector<pair<int, int> > > a, b; n int s = 0; n void deal(map<int, vector<pair<int, int> > > &a, int n, int m) { n   if ((m + 1 - a.size()) % 2) s ^= n; n   for (map<int, vector<pair<int, int> > >::iterator it = a.begin(); n        it != a.end(); ++it) { n     sort(it->second.begin(), it->second.end()); n     vector<pair<int, int> > v; n     pair<int, int> p = it->second[0]; n     for (int i = 1; i < it->second.size(); ++i) { n       if (it->second[i].first > p.second) { n         v.push_back(p); n         p = it->second[i]; n       } else n         p.second = max(p.second, it->second[i].second); n     } n     v.push_back(p); n     it->second = v; n     int t = 0; n     for (int i = 0; i < v.size(); ++i) t += v[i].second - v[i].first; n     s ^= n - t; n   } n } n int search(vector<pair<int, int> > &v, int L, int R, int get) { n   int ans = R; n   while (L <= R) { n     int m = (L + R) >> 1; n     int t = m; n     for (int i = 0; i < v.size(); ++i) { n       if (v[i].first >= m) n         t += v[i].second - v[i].first; n       else if (v[i].second >= m) n         t += v[i].second - m; n     } n     if (t >= get) { n       ans = m; n       R = m - 1; n     } else n       L = m + 1; n   } n   return ans; n } n int main() { n   cin >> n >> m >> k; n   for (int i = 1; i <= k; ++i) { n     int x1, y1, x2, y2; n     scanf( %d%d%d%d , &x1, &y1, &x2, &y2); n     if (x1 == x2) { n       if (y1 > y2) swap(y1, y2); n       a[x1].push_back(pair<int, int>(y1, y2)); n     } else if (y1 == y2) { n       if (x1 > x2) swap(x1, x2); n       b[y1].push_back(pair<int, int>(x1, x2)); n     } n   } n   deal(a, m, n); n   deal(b, n, m); n   if (s) { n     puts( FIRST ); n     if (a.size() < n) n       for (int i = 1; i < n; ++i) { n         if (!a.count(i)) { n           a[i]; n           break; n         } n       } n     for (map<int, vector<pair<int, int> > >::iterator it = a.begin(); n          it != a.end(); ++it) { n       vector<pair<int, int> > v = it->second; n       int t = 0; n       for (int i = 0; i < v.size(); ++i) t += v[i].second - v[i].first; n       t = m - t; n       if (!t) continue; n       if ((s ^ t) >= t) continue; n       int p = search(v, 0, m, m - (s ^ t)); n       printf( %d %d %d %d n , it->first, 0, it->first, p); n       return 0; n     } n     if (b.size() < m) n       for (int i = 1; i < m; ++i) { n         if (!b.count(i)) { n           b[i]; n           break; n         } n       } n     for (map<int, vector<pair<int, int> > >::iterator it = b.begin(); n          it != b.end(); ++it) { n       vector<pair<int, int> > v = it->second; n       int t = 0; n       for (int i = 0; i < v.size(); ++i) t += v[i].second - v[i].first; n       t = n - t; n       if (!t) continue; n       if ((s ^ t) >= t) continue; n       int p = search(v, 0, n, n - (s ^ t)); n       printf( %d %d %d %d n , 0, it->first, p, it->first); n       return 0; n     } n   } else { n     puts( SECOND ); n   } n   return 0; n } n


#include <bits/stdc++.h> n int n, m, i, j; n char s[110], p =  - ; n bool q = true; n int main() { n   scanf( %d%d , &n, &m); n   gets(s); n   for (i = 0; i < n; i++) { n     gets(s); n     if (s[0] <  0  || s[0] >  9  || s[0] == p) n       q = false; n     else n       p = s[0]; n     for (j = 1; j < m; j++) n       if (s[j] != p) q = false; n   } n   if (q) n     puts( YES ); n   else n     puts( NO ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   char str[10000]; n   cin >> str; n   if (str[0] >=  a  && str[0] <=  z ) str[0] = str[0] - 32; n   cout << str; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 10007; n int n, m, d, p[maxn], k[maxn], c[maxn], ans[maxn], vis[maxn], match[maxn]; n vector<int> adj[maxn]; n bool dfs(int u) { n   for (int i = 0; i < adj[u].size(); i++) { n     int v = adj[u][i]; n     if (!vis[v]) { n       vis[v] = 1; n       if (match[v] == -1 || dfs(match[v])) { n         match[v] = u; n         return true; n       } n     } n   } n   return false; n } n int main() { n   cin >> n >> m; n   for (int i = 1; i <= n; i++) cin >> p[i]; n   for (int i = 1; i <= n; i++) { n     cin >> c[i]; n   } n   cin >> d; n   for (int i = 1; i <= d; i++) { n     cin >> k[i]; n     vis[k[i]] = 1; n   } n   for (int i = 1; i <= n; i++) { n     if (!vis[i]) { n       adj[p[i]].push_back(c[i] + 5000); n       adj[c[i] + 5000].push_back(p[i]); n     } n   } n   memset(match, -1, sizeof(match)); n   memset(vis, 0, sizeof(vis)); n   while (dfs(ans[d])) { n     ans[d]++; n     memset(vis, 0, sizeof(vis)); n   } n   for (int i = d - 1; i; i--) { n     adj[p[k[i + 1]]].push_back(c[k[i + 1]] + 5000); n     adj[c[k[i + 1]] + 5000].push_back(p[k[i + 1]]); n     ans[i] = ans[i + 1]; n     memset(vis, 0, sizeof(vis)); n     while (dfs(ans[i])) { n       ans[i]++; n       memset(vis, 0, sizeof(vis)); n     } n   } n   for (int i = 1; i <= d; i++) printf( %d n , ans[i]); n } n


#include <bits/stdc++.h> n using namespace std; n const int mod = 998244353; n map<int, int> mmp; n long long qpow(long long a, long long b, long long m) { n   long long ans = 1, aa = a; n   while (b) { n     if (b & 1) ans = ans * aa % m; n     aa = aa * aa % m; n     b >>= 1; n   } n   return ans; n } n int a[200010]; n int ccc[200010]; n int main() { n   int n, i, x = 1; n   cin >> n; n   for (i = 1; i <= n; i++) { n     cin >> a[i]; n     mmp[a[i]] = i; n   } n   int pos = 1; n   ccc[0] = mmp[a[1]]; n   int num = 0; n   while (pos <= n) { n     while (pos <= n) { n       ccc[num] = max(mmp[a[pos]], ccc[num]); n       if (pos == ccc[num]) break; n       pos++; n     } n     if (pos == n) break; n     pos++; n     num++; n   } n   cout << qpow(2, num, mod) << endl; n } n


#include <bits/stdc++.h> n using namespace std; n using ll = long long; n using vi = vector<int>; n using vvi = vector<vi>; n using vpii = vector<pair<int, int>>; n using vc = vector<char>; n using vvc = vector<vc>; n long long binpow(long long a, long long n) { n   long long res = 1; n   while (n) { n     if (n & 1) res *= a; n     a *= a; n     n >>= 1; n   } n   return res; n } n bool prime(long long n) { n   for (long long i = 2; i <= sqrt(n); i++) { n     if (n % i == 0) return false; n   } n   return true; n } n int gcd(int a, int b) { n   while (b) { n     a %= b; n     swap(a, b); n   } n   return a; n } n long long lcm(int a, int b) { return a / gcd(a, b) * b; } n template <class T1, class T2> n void Cin_vector(vector<T1>& a, T2 n) { n   for (auto& val : a) { n     std::cin >> val; n   } n } n template <class T1, class T2> n void Cout_vector(vector<T1>& a, T2 n) { n   for (auto& val : a) { n     std::cout << val <<    ; n   } n } n template <class T1, class T2, class T3> n void Cin_matrix(vector<vector<T1>>& a, T2 n, T3 m) { n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < m; j++) { n       std::cin >> a[i][j]; n     } n   } n } n template <class T1, class T2, class T3> n void Cout_matrix(vector<vector<T1>>& a, T2 n, T3 m) { n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < m; j++) { n       std::cout << a[i][j] <<    ; n     } n     std::cout <<   n ; n   } n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   vector<string> otvet = { F ,  M ,  S }; n   vector<string> strs; n   map<string, int> counts; n   for (int i = 0; i < 3; i++) { n     string s; n     std::cin >> s; n     strs.push_back(s); n     counts[s]++; n   } n   int maxs = 0; n   string temp; n   for (auto& [strings, cnt] : counts) { n     if (cnt > maxs) { n       maxs = cnt; n       temp = strings; n     } n   } n   if (maxs == 1 || maxs == 3) { n     std::cout <<  ? ; n   } else { n     string c; n     for (auto& [strings, cnt] : counts) { n       if (strings != temp) { n         c = strings; n       } n     } n     if (temp ==  rock  && c ==  paper  || temp ==  paper  && c ==  scissors  || n         temp ==  scissors  && c ==  rock ) { n       for (int i = 0; i < 3; i++) { n         if (c == strs[i]) { n           std::cout << otvet[i]; n           break; n         } n       } n     } else { n       std::cout <<  ? ; n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int mod = 1e9 + 7; n const int inf = 0x7f7f7f7f; n const int maxn = 1e3 + 5; n int n, u, v, root, cnt[2], S; n vector<int> vec[maxn]; n int sz[maxn], msz[maxn]; n void dfs1(int x, int par) { n   for (int i = 0; i <= (int)vec[x].size() - 1; i++) { n     if (vec[x][i] == par) continue; n     dfs1(vec[x][i], x); n     sz[x] += sz[vec[x][i]]; n     msz[x] = max(msz[x], sz[vec[x][i]]); n   } n   sz[x]++; n   msz[x] = max(msz[x], n - sz[x]); n } n int par[maxn]; n int find(int x) { n   if (x == par[x]) return x; n   return par[x] = find(par[x]); n } n void dfs2(int x, int par, int f, int used) { n   for (int i = 0; i <= (int)vec[x].size() - 1; i++) { n     if (par == vec[x][i]) continue; n     int now = ++cnt[f] - used; n     dfs2(vec[x][i], x, f, now + used); n     if (f) now *= (S + 1); n     printf( %d %d %d n , x, vec[x][i], now); n   } n } n int main() { n   cin >> n; n   if (n == 1) return 0; n   for (int i = 1; i <= n - 1; i++) { n     scanf( %d%d , &u, &v); n     vec[u].push_back(v); n     vec[v].push_back(u); n   } n   dfs1(1, -1); n   root = 1; n   for (int i = 1; i <= n; i++) { n     if (msz[i] < msz[root]) root = i; n   } n   memset(sz, 0, sizeof(sz)); n   dfs1(root, -1); n   priority_queue<pair<int, int>, vector<pair<int, int> >, n                  greater<pair<int, int> > > n       que; n   for (int i = 0; i <= (int)vec[root].size() - 1; i++) { n     que.push(pair<int, int>(sz[vec[root][i]], vec[root][i])); n   } n   for (int i = 1; i <= n; i++) par[i] = i; n   while (que.size() >= 3) { n     pair<int, int> p1 = que.top(); n     que.pop(); n     pair<int, int> p2 = que.top(); n     que.pop(); n     par[p2.second] = find(p1.second); n     que.push(pair<int, int>(p1.first + p2.first, par[p1.second])); n   } n   for (int i = 1; i <= n; i++) find(i); n   int pp = par[vec[root][0]]; n   for (int i = 0; i <= (int)vec[root].size() - 1; i++) { n     if (par[vec[root][i]] == pp) S += sz[vec[root][i]]; n   } n   for (int i = 0; i <= (int)vec[root].size() - 1; i++) { n     if (par[vec[root][i]] == pp) { n       printf( %d %d %d n , root, vec[root][i], ++cnt[0]); n       dfs2(vec[root][i], root, 0, cnt[0]); n     } else { n       printf( %d %d %d n , root, vec[root][i], ++cnt[1] * (S + 1)); n       dfs2(vec[root][i], root, 1, cnt[1]); n     } n   } n } n


#include <bits/stdc++.h> n  n using namespace std; n  n #define _ ios_base::sync_with_stdio(0);cin.tie(0); n #define endl   n  n  n typedef long long ll; n  n const int INF = 0x3f3f3f3f; n const ll LINF = 0x3f3f3f3f3f3f3f3fll; n  n int main(){ _ n  n  int n; cin >> n; n  vector<int> a(n); n  for(int& i : a) cin >> i; n   n  auto oa = a; n  sort(oa.begin(), oa.end()); n  auto it = unique(oa.begin(), oa.end()); n  oa.resize(distance(oa.begin(), it)); n  map<int,int> mp; n  int m = oa.size(); n  for(int i = 0; i < m; i++) mp[oa[i]] = i; n  for(auto& i : a) i = mp[i]; n  n  vector<vector<int>> ct(m); n  for(int i = 0; i < n; i++) ct[a[i]].push_back(i); n  n  vector<vector<int>> g(n); n  for(int i = 0; i < n-1; i++) { n   int u, v; cin >> u >> v; u--, v--; n   g[u].push_back(v); n   g[v].push_back(u); n  } n  n  int time = 0; n  vector<int> in(n,-1), out(n); n  function<void(int)> tour = [&] (int v) { n   in[v] = time; n   for(auto u : g[v]) if(in[u] == -1) tour(u); n   out[v] = time++; n  }; n  n  tour(0); n  n  auto very_bad = [&] () { n   cout << 0 << endl; n   exit(0); n  }; n  n  vector<int> sum(n+1, 0); n  auto update = [&] (int l, int r, int x) { n   sum[l] += x, sum[r + 1] -= x; n  }; n  n  for(int i = 0; i < m; i++) if(ct[i].size() > 1) { n   vector<pair<int,int>> aux; n   for(auto u : ct[i]) aux.push_back({out[u], u}); n   sort(aux.begin(), aux.end()); n  n   set<int> cur; n   int anc = -1, nanc = 0; n   for(auto [outt, u] : aux) { n    int l = in[u], r = out[u]; n    if(nanc and r > out[anc]) very_bad(); n     n    auto itl = cur.lower_bound(l); n    auto itr = cur.upper_bound(r); n     n    if(itl != itr) { n     anc = u, nanc++; n     if(itl != cur.begin() or itr != cur.end()) very_bad(); n    } n    if(nanc > 2) very_bad(); n     n    cur.insert(r); n   } n  n   if(nanc == 0) { n    for(auto u : ct[i]) update(in[u], out[u], 1); n   } n   else { n    bool found = false; n    int good_child = -1; n    for(auto u : g[anc]) if(out[u] < out[anc]) { n     int l = in[u], r = out[u]; n     auto itl = cur.lower_bound(l); n     auto itr = cur.upper_bound(r); n  n     if(itl != itr) { n      if(found) very_bad(); n      found = true; n      good_child = u; n     } n    } n  n    update(0, n-1, 1); n    update(in[good_child], out[good_child], -1); n    for(auto u : ct[i]) if(u != anc) { n     update(in[u], out[u], 1); n    } n   } n  } n  n  int ans = 0; n  for(int i = 0; i < n; i++) { n   if(i) sum[i] += sum[i-1]; n   if(!sum[i]) { n    ans++; n   } n  } n  n  cout << ans << endl; n  n  return 0; n }