#include <bits/stdc++.h> n template <typename A, typename B> n inline char smax(A &a, const B &b) { n   return a < b ? a = b, 1 : 0; n } n template <typename A, typename B> n inline char smin(A &a, const B &b) { n   return b < a ? a = b, 1 : 0; n } n template <typename I> n inline void read(I &x) { n   int f = 0, c; n   while (!isdigit(c = getchar())) c ==  -  ? f = 1 : 0; n   x = c & 15; n   while (isdigit(c = getchar())) x = (x << 1) + (x << 3) + (c & 15); n   f ? x = -x : 0; n } n const int N = 500 + 7; n const int M = (1 << 16) + 7; n const int P = 1e9 + 7; n int n, m, S, nod; n int A, B, C, D, F; n int fa[N], lc[N], rc[N], v[N], dp[N][M]; n char a[N]; n inline void sadd(int &x, const int &y) { n   x += y; n   x >= P ? x -= P : x; n } n inline int smod(const int &x) { return x >= P ? x - P : x; } n inline int fpow(int x, int y) { n   int ans = 1; n   for (; y; y >>= 1, x = (long long)x * x % P) n     if (y & 1) ans = (long long)ans * x % P; n   return ans; n } n inline void fwt_and(int *a, int n, int f) { n   for (int i = 1; i < n; i <<= 1) n     for (int j = 0; j < n; j += i << 1) n       for (int k = 0; k < i; ++k) { n         int x = a[j + k], y = a[i + j + k]; n         if (f > 0) n           a[j + k] = smod(x + y); n         else n           a[j + k] = smod(x + P - y); n       } n } n inline void fwt_or(int *a, int n, int f) { n   for (int i = 1; i < n; i <<= 1) n     for (int j = 0; j < n; j += i << 1) n       for (int k = 0; k < i; ++k) { n         int x = a[j + k], y = a[i + j + k]; n         if (f > 0) n           a[i + j + k] = smod(x + y); n         else n           a[i + j + k] = smod(y + P - x); n       } n } n int tA[M], tB[M]; n inline void mul_and(int *a, int *b, int *c) { n   memcpy(tA, a, sizeof(*a) * S); n   memcpy(tB, b, sizeof(*b) * S); n   fwt_and(tA, S, 1), fwt_and(tB, S, 1); n   for (int i = 0; i < S; ++i) tA[i] = (long long)tA[i] * tB[i] % P; n   fwt_and(tA, S, -1); n   for (int i = 0; i < S; ++i) sadd(c[i], tA[i]); n } n inline void mul_or(int *a, int *b, int *c) { n   memcpy(tA, a, sizeof(*a) * S); n   memcpy(tB, b, sizeof(*b) * S); n   fwt_or(tA, S, 1), fwt_or(tB, S, 1); n   for (int i = 0; i < S; ++i) tA[i] = (long long)tA[i] * tB[i] % P; n   fwt_or(tA, S, -1); n   for (int i = 0; i < S; ++i) sadd(c[i], tA[i]); n } n inline void dfs(int x, int fa = 0) { n   if (!lc[x]) { n     if (v[x] == 1) n       dp[x][A] = 1; n     else if (v[x] == 2) n       dp[x][B] = 1; n     else if (v[x] == 3) n       dp[x][C] = 1; n     else if (v[x] == 4) n       dp[x][D] = 1; n     else if (v[x] == 5) n       dp[x][(S - 1) ^ A] = 1; n     else if (v[x] == 6) n       dp[x][(S - 1) ^ B] = 1; n     else if (v[x] == 7) n       dp[x][(S - 1) ^ C] = 1; n     else if (v[x] == 8) n       dp[x][(S - 1) ^ D] = 1; n     else if (v[x] == 0) n       ++dp[x][A], ++dp[x][(S - 1) ^ A], ++dp[x][B], ++dp[x][(S - 1) ^ B], n           ++dp[x][C], ++dp[x][(S - 1) ^ C], ++dp[x][D], ++dp[x][(S - 1) ^ D]; n   } else { n     dfs(lc[x], x), dfs(rc[x], x); n     if (v[x] == -1 || v[x] == 0) mul_and(dp[lc[x]], dp[rc[x]], dp[x]); n     if (v[x] == -2 || v[x] == 0) mul_or(dp[lc[x]], dp[rc[x]], dp[x]); n   } n } n inline void build() { n   int curs = nod = 1; n   n = strlen(a + 1); n   for (int i = 1; i <= n; ++i) { n     if (a[i] ==  ( ) n       fa[++nod] = curs, curs = nod; n     else if (a[i] ==  ) ) { n       if (lc[fa[curs]]) n         rc[fa[curs]] = curs; n       else n         lc[fa[curs]] = curs; n       curs = fa[curs]; n     } else if (a[i] >=  A  && a[i] <=  D ) n       v[curs] = a[i] -  A  + 1; n     else if (a[i] >=  a  && a[i] <=  d ) n       v[curs] = a[i] -  a  + 5; n     else if (a[i] ==  & ) n       v[curs] = -1; n     else if (a[i] ==  | ) n       v[curs] = -2; n   } n } n inline void work() { n   build(); n   dfs(1); n   printf( %d n , dp[1][F]); n } n inline void init() { n   scanf( %s , a + 1); n   read(m), S = 1 << m; n   int x; n   for (int i = 1; i <= m; ++i) { n     read(x), x && (A |= 1 << (i - 1)); n     read(x), x && (B |= 1 << (i - 1)); n     read(x), x && (C |= 1 << (i - 1)); n     read(x), x && (D |= 1 << (i - 1)); n     read(x), x && (F |= 1 << (i - 1)); n   } n } n int main() { n   init(); n   work(); n   fclose(stdin), fclose(stdout); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e4 + 6; n int n, l, r, h[N], b[N], ans[N], S; n vector<int> X, Y; n int main() { n   scanf( %d%d%d , &n, &l, &r); n   for (int i = 1; i <= n; ++i) scanf( %d , h + i), S += h[i]; n   for (int i = 1; i <= n; ++i) scanf( %d , b + i); n   int _l = S - r, _r = S - l; n   for (int i = 1; i <= n; ++i) (b[i] ? Y : X).push_back(h[i]); n   sort(X.rbegin(), X.rend()), sort(Y.rbegin(), Y.rend()); n   for (int i = 1; i <= 10000; ++i) ans[i] = -1e9; n   for (int s : X) n     for (int i = 10000; i >= s; --i) n       if (ans[i - s] >= 0) ans[i] = max(ans[i], ans[i - s]); n   for (int s : Y) n     for (int i = 10000; i >= s; --i) n       if (ans[i - s] >= 0) n         ans[i] = max(ans[i], ans[i - s] + (_l <= i && i <= _r)); n   printf( %d n , *max_element(ans, ans + 10001)); n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1000100; n int n, p[N]; n int mx1[N], mx2[N], ans[N]; n int main() { n   scanf( %d , &n); n   for (int v, best, i = 2; i <= n + 1; i++) { n     scanf( %d , p + i); n     v = i; n     while (v != 0) { n       best = max(mx1[v], mx2[v] + 1); n       if (best <= ans[v]) break; n       ans[v] = best; n       if (mx2[p[v]] < best) mx2[p[v]] = best; n       if (mx1[p[v]] < mx2[p[v]]) swap(mx1[p[v]], mx2[p[v]]); n       v = p[v]; n     } n     printf( %d  , max(mx1[1], mx2[1])); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MaxD = 1001; n vector<int> temp; n int main() { n   int n, w[MaxD] = {0}, h[MaxD] = {0}, maximum = 0, minimum = MaxD, ilgis; n   unsigned long long ats = 1e18; n   bool ar; n   cin >> n; n   for (int i = 1; i <= n; i++) { n     cin >> w[i] >> h[i]; n     if (max(w[i], h[i]) > maximum) maximum = max(w[i], h[i]); n     if (min(w[i], h[i]) < minimum) minimum = min(w[i], h[i]); n   } n   for (int i = minimum; i <= maximum; i++) { n     ilgis = 0; n     int vietos = n / 2; n     ar = true; n     temp.clear(); n     for (int j = 1; j <= n; j++) { n       if (min(w[j], h[j]) > i) { n         ar = false; n         break; n       } n       if (h[j] > i && w[j] <= i) { n         vietos--; n         ilgis += h[j]; n       } else if (w[j] > i && h[j] <= i) { n         ilgis += w[j]; n       } else { n         ilgis += w[j]; n         if (w[j] > h[j] && w[j] <= i) temp.push_back(h[j] - w[j]); n       } n       if (vietos < 0) { n         ar = false; n         break; n       } n     } n     sort(temp.begin(), temp.end()); n     if (ar) { n       for (int j = 0; j < temp.size() && vietos; j++) { n         ilgis += temp[j]; n         vietos--; n       } n     } n     if (ar && (ilgis * i) < ats) ats = ilgis * i; n   } n   cout << ats; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   unsigned long long int x; n   cin >> x; n   cout << 25; n } n


#include <bits/stdc++.h> n using namespace std; n int n, dp[300005], k = 0, b[300005]; n vector<int> ke[300005]; n void dfs(int u, int par) { n   if (ke[u].size() == 1 && u != 1) { n     dp[u] = 1; n     k++; n     return; n   } n   if (b[u]) n     dp[u] = 1e9; n   else n     dp[u] = 0; n   for (int v : ke[u]) n     if (v != par) { n       dfs(v, u); n       if (b[u]) n         dp[u] = min(dp[u], dp[v]); n       else n         dp[u] += dp[v]; n     } n } n int main() { n   ios::sync_with_stdio(0); n   cin.tie(); n   cout.tie(); n   cin >> n; n   for (int i = 1; i <= n; i++) cin >> b[i]; n   int u; n   for (int i = 2; i <= n; i++) { n     cin >> u; n     ke[u].push_back(i); n     ke[i].push_back(u); n   } n   dfs(1, 0); n   cout << k - dp[1] + 1; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long double PI = 3.14159265359; n const long long MOD = (long long)1e9 + 7; n const long long MAXN = (long long)2e3 + 10; n const long long INF = (long long)2242545357980376863; n const long double EPS = (long double)1e-8; n long long dp[MAXN][2048]; n long long pw[MAXN], cnt[MAXN]; n long long a[MAXN]; n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   pw[0] = 1; n   for (int i = 1; i < MAXN; i++) pw[i] = (pw[i - 1] << 1) % MOD; n   long long n, k; n   cin >> n >> k; n   k = (1ll << k); n   for (int i = 1; i <= n; i++) cin >> a[i]; n   long long c0 = 0; n   for (int i = n; i >= 0; i--) { n     cnt[i] = c0; n     if (a[i] == 0) c0++; n   } n   if (a[1] == 2) { n     dp[1][2] = 1; n   } n   if (a[1] == 4) { n     dp[1][4] = 1; n   } n   if (a[1] == 0) { n     dp[1][2] = dp[1][4] = 1; n   } n   long long ans = 0; n   for (int i = 1; i < n; i++) { n     for (int j = 2; j < 2048; j++) { n       if (dp[i][j] == 0) continue; n       long long nx = a[i + 1]; n       if (nx != 4) { n         long long sm = j + 2; n         if (sm >= k) { n           ans += (pw[cnt[i + 1]] * dp[i][j]); n           ans %= MOD; n         } else { n           dp[i + 1][sm] += dp[i][j]; n           dp[i + 1][sm] %= MOD; n         } n       } n       if (nx != 2) { n         if (j % 4 == 2) { n           dp[i + 1][4] += dp[i][j]; n         } else { n           long long sm = j + 4; n           if (sm >= k) { n             ans += (pw[cnt[i + 1]] * dp[i][j]); n             ans %= MOD; n           } else { n             dp[i + 1][sm] += dp[i][j]; n             dp[i + 1][sm] %= MOD; n           } n         } n       } n     } n   } n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   long long t; n   cin >> t; n   while (t--) { n     long long a, b, c; n     cin >> a >> b >> c; n     long long ans = (a + b + c) / 2; n     cout << ans <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n using ll = long long; n using vi = vector<int>; n using pii = pair<int, int>; n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   int n; n   cin >> n; n   vector<int> A(n); n   vector<int> P(n + 1); n   for (auto &x : A) cin >> x; n   for (int i = 0; i < n; ++i) P[A[i]] = i; n   int cnt = 0; n   vector<vector<int>> sol; n   for (int kk = 0; kk < n; ++kk) { n     if (is_sorted(A.begin(), A.end())) break; n     ++cnt; n     int goal = -1; n     for (int i = 1; i <= n - 1; ++i) { n       if (P[i] > P[i + 1]) { n         goal = i; n         break; n       } n     } n     vector<vector<int>> G; n     G.push_back({}); n     int state = 0; n     for (int i = 0; i < n; ++i) { n       if (state == 0) { n         if (A[i] != goal + 1) { n           G.back().push_back(A[i]); n         } else { n           G.push_back({A[i]}); n           state = 1; n         } n       } else if (state == 1) { n         if (A[i] == goal) { n           G.push_back({A[i]}); n           state = 3; n           G.push_back({}); n         } else if (A[i] == A[i - 1] + 1) n           G.back().push_back(A[i]); n         else { n           G.push_back({A[i]}); n           state = 2; n         } n       } else if (state == 2) { n         G.back().push_back(A[i]); n         if (A[i] == goal) { n           state = 3; n           G.push_back({}); n         } n       } else { n         G.back().push_back(A[i]); n       } n     } n     sol.push_back({}); n     for (auto &x : G) { n       if (!x.empty()) sol.back().push_back(x.size()); n     } n     reverse(G.begin(), G.end()); n     { n       int i = 0; n       for (auto &g : G) n         for (auto x : g) A[i++] = x; n     } n     for (int i = 0; i < n; ++i) P[A[i]] = i; n   } n   assert(is_sorted(A.begin(), A.end())); n   cout << cnt <<   n ; n   for (auto &s : sol) { n     cout << s.size() <<    ; n     for (auto &x : s) cout << x <<    ; n     cout <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long p = 1e9 + 7; n int n, a[2505], mx, s, ch, ans; n long long f[100005]; n long long ksm(long long x, int y) { n   long long sun = 1; n   x %= p; n   while (y) { n     if (y & 1) sun = sun * x % p; n     y >>= 1; n     x = x * x % p; n   } n   return sun; n } n int main() { n   cin >> n; n   for (int i = 1; i <= n; i++) n     scanf( %d , &a[i]), mx = max(a[i], mx), s += a[i]; n   f[1] = ksm(s - 1, 2) * ksm(s, p - 2) % p; n   ch = f[1]; n   for (int i = 2; i <= mx; i++) { n     ch = (ch - ksm(s - i + 1, p - 2) * (s - 1) % p + p) % p; n     f[i] = (f[i - 1] + ch) % p; n   } n   for (int i = 1; i <= n; i++) ans = (ans + f[a[i]]) % p; n   cout << ans; n } n


#include <cstdio> n #include <cstring> n #include <queue> n #include <cmath> n #include <set> n #include <cstdlib> n #include <iostream> n #include <map> n #include <algorithm> n using namespace std; n typedef long long ll; n typedef unsigned long long ull; n typedef unsigned int uii; n typedef pair<int, ll> pii; n template<typename T> n inline void rd(T& x) n { n  int tmp = 1; char c = getchar(); x = 0; n  while (c >  9  || c <  0 ) { if (c ==  - )tmp = -1; c = getchar(); } n  while (c >=  0  && c <=  9 ) { x = x * 10 + c -  0 ; c = getchar(); } n  x *= tmp; n } n const int N = 2e5 + 10; n const int M = 1e6 + 10; n const int mod = 1e9 + 7; n const int inf = 0x3f3f3f3f; n const double eps = 1e-5; n int sgn(double x) { n  if (fabs(x) < eps) return 0; n  if (x > 0) return 1; n  return -1; n } n int n, m, k; n int head[N], cntE; n struct edge { n  int to, next; n  int w; n }e[M]; n void add(int u, int v, int w = 0) { n  e[cntE].to = v; n  e[cntE].next = head[u]; n  e[cntE].w = w; n  head[u] = cntE++; n } n bool vis[N]; n int stk[N], top; n bool dfs(int x) { n  if (vis[x ^ 1]) return false; n  if (vis[x]) return true; n  vis[x] = true; n  stk[++top] = x; n  for (int i = head[x]; ~i; i = e[i].next) { n   int v = e[i].to; n   if (!dfs(v)) return false; n  } n  return true; n } n bool two_sat() { n  memset(vis, false, sizeof(bool) * (2 * n + 10)); n  for (int i = 0; i < n; ++i) { n   if (vis[i << 1] || vis[i << 1 | 1]) continue; n   top = 0; n   if (!dfs(i << 1)) { n    while (top) vis[stk[top--]] = false; n    if (!dfs(i << 1 | 1)) return false; n   } n  } n  return true; n } n ll a[N], b[N], c[N], maxn[N]; n ll solve() { n  maxn[n] = c[n] - 1 + 2; n  ll ans = 0; n  for (int i = n - 1; i > 0; --i) { n   ans = max(ans, maxn[i + 1] + b[i + 1] - a[i + 1]); n   maxn[i] = c[i] - 1; n   if (b[i+1] == a[i+1]) { n   // maxn[i] = c[i] + 1; n   } n   else { n    maxn[i] = max(maxn[i], maxn[i + 1] + (c[i] - 1 - (b[i + 1] - a[i + 1]))); n   } n   maxn[i] += 2; n  } n  return ans; n } n int main() { n #ifdef _DEBUG n  FILE* _INPUT = freopen( input.txt ,  r , stdin); n  // FILE* _OUTPUT = freopen( output.txt ,  w , stdout); n #endif // !_DEBUG n  int cas = 0, T; n  rd(T); n  while (T--) { n // while (~scanf( %d , &n)) { n   rd(n); n   for (int i = 1; i <= n; ++i) rd(c[i]); n   for (int i = 1; i <= n; ++i) rd(a[i]); n   for (int i = 1; i <= n; ++i) rd(b[i]); n   for (int i = 1; i <= n; ++i) if (a[i] > b[i]) swap(a[i], b[i]); n   printf( %lld n , solve()); n  } n  return 0; n }


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   cin >> n; n   cout << n / 2 * 3; n } n


#include <bits/stdc++.h> n using namespace std; n void printv(vector<long long int> v) { n   long long int sz = v.size(); n   for (long long int i = 0; i < sz; i++) { n     if (i == sz - 1) n       cout << v[i] <<   n ; n     else n       cout << v[i] <<    ; n   } n } n void input(long long int a[], long long int sz) { n   for (long long int i = 0; i < sz; i++) cin >> a[i]; n } n void print(long long int a[], long long int sz) { n   for (long long int i = 0; i < sz; i++) { n     if (i == sz - 1) n       cout << a[i] <<   n ; n     else n       cout << a[i] <<    ; n   } n } n long long int dx1[] = {1, 1, 0, -1, -1, -1, 0, 1}; n long long int dy1[] = {0, 1, 1, 1, 0, -1, -1, -1}; n long long int dx2[] = {1, 0, -1, 0}; n long long int dy2[] = {0, 1, 0, -1}; n long long int mul(long long int a, long long int b, n                   long long int p = 1000000007) { n   return ((a % p) * (b % p)) % p; n } n long long int add(long long int a, long long int b, n                   long long int p = 1000000007) { n   return (a % p + b % p) % p; n } n long long int power(long long int x, long long int y, n                     long long int p = 1000000007) { n   long long int res = 1; n   x = x % p; n   while (y > 0) { n     if (y & 1) res = (res * x) % p; n     y = y >> 1; n     x = (x * x) % p; n   } n   return res; n } n long long int modInverse(long long int n, long long int p = 1000000007) { n   return power(n, p - 2, p); n } n long long int ncrMod(long long int n, long long int r, n                      long long int p = 1000000007) { n   if (r == 0) return 1; n   long long int fac[n + 1]; n   fac[0] = 1; n   for (long long int i = 1; i <= n; i++) fac[i] = fac[i - 1] * i % p; n   return (fac[n] * modInverse(fac[r], p) % p * modInverse(fac[n - r], p) % p) % n          p; n } n bool ispeed2ime(long long int n) { n   for (int i = 2; i * i <= n; i++) { n     if (n % i == 0) { n       return false; n     } n   } n   return true; n } n bool isPowerof2(long long int n) { n   while (n > 1) { n     if (n % 2 != 0) { n       return true; n     } n     n /= 2; n   } n   return false; n } n string reverse(string str, int len, int l, int r) { n   if (l < 0 || r >= len || l > r) return str; n   while (l < r) { n     char c = str[l]; n     str[l] = str[r]; n     str[r] = c; n     l++; n     r--; n   } n   return str; n } n void solve() { n   long long int n; n   cin >> n; n   string s; n   cin >> s; n   string temp =   ; n   long long int maxi1 = 0, maxi2 = 0; n   for (int i = 0; i < n - 1; i++) { n     temp += s[i]; n     temp += s[i + 1]; n     if (temp ==  10 ) { n       maxi1++; n     } else if (temp ==  01 ) { n       maxi2++; n     } n     temp =   ; n   } n   long long int maxi = max(maxi1, maxi2); n   long long int cnt = n / 2 - (maxi); n   cout << cnt <<   n ; n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(NULL); n   cout.tie(NULL); n   long long int t = 1; n   cin >> t; n   while (t--) { n     solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n bool QQQQ; n const int dx[] = {1, -1, 0, 0, 1, 1, -1, -1}; n const int dy[] = {0, 0, -1, 1, 1, -1, 1, -1}; n using namespace std; n int ttt = 1; n int n, k, res = 100256, z, cnt; n string s, t, ans; n void solve() { n   cin >> n >> k >> s; n   ans = s; n   for (int i = 0; i < n; i++) ans[i] =  9 ; n   for (int i = 0; i < 10; i++) { n     t = s; n     z = k; n     cnt = 0; n     for (int j = 0; j < 10; j++) { n       for (int l = 0; l < n; l++) n         if (z && t[l] -  0  == i + j) { n           z--; n           t[l] = i +  0 ; n           cnt += j; n         } n       if (j) n         for (int l = n - 1; l >= 0; l--) n           if (z && t[l] -  0  == i - j) { n             z--; n             t[l] = i +  0 ; n             cnt += j; n           } n     } n     if (cnt < res) res = cnt, ans = t; n     if (cnt == res) ans = min(ans, t); n   } n   cout << res << endl << ans; n } n int main() { n   for (int tt = 1; tt <= (ttt = ttt); tt++) { n     solve(); n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(); n   cin.tie(); n   int t, n, a, b; n   cin >> t; n   while (t--) { n     int a, b; n     cin >> a >> b; n     int c = abs(a - b); n     int ans = 0; n     int tmp = c / 5; n     ans += tmp; n     c -= tmp * 5; n     tmp = c / 2; n     ans += tmp; n     c -= tmp * 2; n     tmp = c / 1; n     ans += tmp; n     c -= tmp * 1; n     cout << ans << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n unsigned long long int dist[1150][1150], visited[1150][1150]; n char ans[1050][1050]; n void solve() { n   long long n, i, j, m; n   cin >> n >> m; n   for (i = 0; i < n; i++) { n     for (j = 0; j < m; j++) cin >> ans[i][j]; n   } n   int startx, starty, endx, endy; n   for (i = 0; i < n; i++) { n     for (j = 0; j < m; j++) { n       if (ans[i][j] ==  E ) { n         endx = i; n         endy = j; n       } n       if (ans[i][j] ==  S ) { n         startx = i; n         starty = j; n       } n     } n   } n   for (i = 0; i < n; i++) n     for (j = 0; j < m; j++) { n       dist[i][j] = INT_MAX; n       visited[i][j] = 0; n     } n   int rowcol[] = {0, 0, 1, -1}; n   int colcol[] = {1, -1, 0, 0}; n   dist[endx][endy] = 0; n   visited[endx][endy] = 1; n   queue<pair<int, int> > q; n   q.push({endx, endy}); n   while (q.empty() == 0) { n     pair<int, int> point = q.front(); n     q.pop(); n     int x = point.first; n     int y = point.second; n     for (i = 0; i < 4; i++) { n       int x1 = x + rowcol[i]; n       int y1 = y + colcol[i]; n       if (x1 >= 0 && y1 >= 0 && x1 < n && y1 < m && ans[x1][y1] !=  T ) { n         if (visited[x1][y1] == 0) { n           dist[x1][y1] = dist[x][y] + 1; n           visited[x1][y1] = 1; n           q.push({x1, y1}); n         } n       } n     } n   } n   unsigned long long int count = 0; n   for (i = 0; i < n; i++) { n     for (j = 0; j < m; j++) { n       if (startx == i && starty == j) { n         continue; n       } n       if (dist[startx][starty] >= dist[i][j] && ans[i][j] !=  0  && n           ans[i][j] !=  T  && ans[i][j] !=  E ) n         count += ans[i][j] -  0 ; n     } n   } n   cout << count << endl; n } n int main() { n   long long n = 1, x1, y1, x2, y2, i; n   while (n--) { n     solve(); n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 2e5 + 5; n const int inf = 0x3f3f3f3f; n long long dp[maxn]; n int n, m; n struct node { n   int lb, lu; n   long long w; n }; n vector<node> ve[maxn]; n int lb, lu; n long long w, cost[maxn]; n struct seg { n   long long ma, lazy; n } tr[maxn << 2]; n void build(int o, int l, int r) { n   tr[o].ma = 0; n   tr[o].lazy = 0; n   if (l == r) { n     return; n   } n   int mid = (l + r) >> 1; n   build(o << 1, l, mid); n   build(o << 1 | 1, mid + 1, r); n } n void pushup(int o) { tr[o].ma = max(tr[o << 1].ma, tr[o << 1 | 1].ma); } n void pushdown(int o) { n   if (tr[o].lazy) { n     tr[o << 1].ma += tr[o].lazy; n     tr[o << 1 | 1].ma += tr[o].lazy; n     tr[o << 1].lazy += tr[o].lazy; n     tr[o << 1 | 1].lazy += tr[o].lazy; n     tr[o].lazy = 0; n   } n } n void update(int o, int l, int r, int ql, int qr, long long val) { n   if (ql <= l && qr >= r) { n     tr[o].ma += val; n     tr[o].lazy += val; n     return; n   } n   pushdown(o); n   int mid = (l + r) >> 1; n   if (ql <= mid) update(o << 1, l, mid, ql, qr, val); n   if (qr > mid) update(o << 1 | 1, mid + 1, r, ql, qr, val); n   pushup(o); n } n long long query(int o, int l, int r, int ql, int qr) { n   if (ql <= l && qr >= r) { n     return tr[o].ma; n   } n   pushdown(o); n   int mid = (l + r) >> 1; n   long long maxx = 0; n   if (ql <= mid) maxx = max(maxx, query(o << 1, l, mid, ql, qr)); n   if (qr > mid) maxx = max(maxx, query(o << 1 | 1, mid + 1, r, ql, qr)); n   return maxx; n } n int main() { n   while (cin >> n >> m) { n     for (int i = 1; i <= n; i++) { n       scanf( %lld , &cost[i]); n     } n     for (int i = 1; i <= m; i++) { n       scanf( %d%d%lld , &lb, &lu, &w); n       ve[lu].push_back({lb, lu, w}); n     } n     build(1, 0, n + 1); n     long long maxx = 0; n     for (int i = 1; i <= n + 1; i++) { n       dp[i] = dp[i - 1]; n       update(1, 0, n + 1, 0, i - 1, -cost[i]); n       for (int j = 0; j < ve[i].size(); j++) { n         lb = ve[i][j].lb, w = ve[i][j].w; n         update(1, 0, n + 1, 0, lb - 1, w); n       } n       dp[i] = max(dp[i], query(1, 0, n + 1, 0, i - 1)); n       update(1, 0, n + 1, i, i, dp[i]); n       maxx = max(maxx, dp[i]); n     } n     printf( %lld n , maxx); n   } n } n


#include <bits/stdc++.h> n using namespace std; n int n, m; n int cntrow[21]; n int cntcol[6]; n vector<int> sol; n vector<int> allmasks[6]; n bool DP[20][11][11][11][11][11][(1 << 5) + 1]; n bool solve(int row, int c1, int c2, int c3, int c4, int c5, int mask) { n   if (row == m) { n     int tmp = c1 + c2 + c3 + c4 + c5; n     if (tmp == 0) return true; n     return false; n   } n   bool &temp = DP[row][c1][c2][c3][c4][c5][mask]; n   if (temp) return false; n   temp = true; n   for (int i = 0; i < allmasks[cntrow[row]].size(); i++) { n     int tmp = allmasks[cntrow[row]][i]; n     int nc1 = c1; n     int nc2 = c2; n     int nc3 = c3; n     int nc4 = c4; n     int nc5 = c5; n     if ((mask & (1 << 0)) == 0 && (tmp & (1 << 0)) != 0) nc1--; n     if ((mask & (1 << 1)) == 0 && (tmp & (1 << 1)) != 0) nc2--; n     if ((mask & (1 << 2)) == 0 && (tmp & (1 << 2)) != 0) nc3--; n     if ((mask & (1 << 3)) == 0 && (tmp & (1 << 3)) != 0) nc4--; n     if ((mask & (1 << 4)) == 0 && (tmp & (1 << 4)) != 0) nc5--; n     if (nc1 < 0 || nc2 < 0 || nc3 < 0 || nc4 < 0 || nc5 < 0) continue; n     if (solve(row + 1, nc1, nc2, nc3, nc4, nc5, tmp)) { n       sol.push_back(tmp); n       return true; n     } n   } n   return false; n } n int main() { n   ios_base::sync_with_stdio(0); n   cin >> n >> m; n   for (int i = 0; i < n; i++) cin >> cntcol[i]; n   for (int i = 0; i < m; i++) cin >> cntrow[i]; n   for (int i = 0; i < (1 << n); i++) { n     int cnt = 0; n     for (int j = 0; j < n; j++) { n       if (j == 0 || ((i & (1 << (j - 1))) == 0)) n         if ((i & (1 << j)) != 0) cnt++; n     } n     allmasks[cnt].push_back(i); n   } n   solve(0, cntcol[0], cntcol[1], cntcol[2], cntcol[3], cntcol[4], 0); n   reverse(sol.begin(), sol.end()); n   for (int j = 0; j < n; j++) { n     for (int i = 0; i < sol.size(); i++) { n       if (sol[i] & (1 << j)) n         cout <<  * ; n       else n         cout <<  . ; n     } n     cout <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void f(int& r, int& g, int& b) { n   if (r >= 3 && g % 3 == 2 && b % 3 == 2) { n     r += 4; n     g -= 2; n     b -= 2; n   } n } n int main(void) { n   int r, g, b; n   cin >> r >> g >> b; n   f(r, g, b); n   f(g, r, b); n   f(b, r, g); n   cout << r / 3 + g / 3 + b / 3 + min(r % 3, min(g % 3, b % 3)) << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   cout.setf(ios::fixed); n   cout.precision(20); n   int T; n   cin >> T; n   while (T--) { n     int n; n     cin >> n; n     n *= 2; n     vector<int> a(n); n     for (int &x : a) { n       cin >> x; n     } n     map<int, int> left, right; n     map<int, bool> was; n     int red = 0, blue = 0; n     int res = n; n     for (int i = n - 1; i >= n / 2; i--) { n       int toEat = n / 2 - (n - i); n       red += (a[i] == 1); n       blue += (a[i] == 2); n       right[red - blue] = toEat; n       was[red - blue] = true; n       if (i == n / 2 && red == blue) { n         res = min(res, n / 2); n       } n       if (red == blue) { n         res = min(res, n / 2 + toEat); n       } n     } n     red = blue = 0; n     for (int i = 0; i < n / 2; i++) { n       int toEat = n / 2 - i - 1; n       red += (a[i] == 1); n       blue += (a[i] == 2); n       if (red == blue) { n         res = min(res, n / 2 + toEat); n       } n       if (was[blue - red]) { n         res = min(res, toEat + right[blue - red]); n       } n       if (i == n / 2 - 1 && red == blue) { n         res = min(res, n / 2); n       } n     } n     cout << res <<   n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   string str; n   cin >> str; n   int n = str.size(); n   int fl = 1; n   vector<char> v; n   for (int i = 0; i < str.size(); i++) { n     if (str[i] ==  / ) { n       while (str[i] ==  / ) { n         i++; n       } n       i--; n       v.push_back( / ); n     } else { n       v.push_back(str[i]); n     } n   } n   for (int i = 0; i < v.size(); i++) { n     if (v[i] !=  / ) { n       fl = 0; n       break; n     } n   } n   if (fl == 1) { n     cout <<  /  << endl; n   } else { n     int t = v.size(); n     for (int i = 0; i < t; i++) { n       if (i == t - 1 && v[i] ==  / ) { n         continue; n       } else { n         cout << v[i]; n       } n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   int k; n   cin >> k; n   string s; n   cin >> s; n   long long tot = 0; n   int av[11]; n   memset(av, 0, sizeof(av)); n   int ans = 0; n   for (char a : s) tot += a -  0 , av[9 - (a -  0 )]++; n   for (int i = 10; i; i--) { n     if (tot >= k) break; n     if (k - av[i] * i < tot) { n       while (k > tot) { n         k -= i; n         ans++; n       } n       break; n     } else { n       k -= av[i] * i; n       ans += av[i]; n     } n   } n   cout << ans <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, i, j; n   scanf( %d , &n); n   if (n == 1) n     printf( 1 n1 n ); n   else if (n == 2) n     printf( 1 n1 n ); n   else if (n == 3) n     printf( 2 n1 3 n ); n   else if (n == 4) n     printf( 4 n2 4 1 3 n ); n   else { n     printf( %d n , n); n     for (j = 2; j <= n; j += 2) printf( %d  , j); n     for (j = 1; j <= n; j += 2) printf( %d  , j); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int ar[11000111]; n int el[5100]; n int q, x, k, n; n long long t; n int min(int a, int b) { n   if (a < b) { n     return a; n   } n   return b; n } n int main() { n   scanf( %d , &n); n   scanf( %d , &k); n   for (int i = 0; i < n; ++i) { n     scanf( %d , &x); n     ar[x] = 1; n     el[i] = x; n   } n   scanf( %d , &q); n   for (int qq = 0; qq < q; ++qq) { n     scanf( %I64d , &t); n     int ans = 100; n     for (int i = 0; i < n; ++i) { n       for (int j = 0; j < k + 1; ++j) { n         long long temp = t - el[i] * j; n         if (temp > 0) { n           for (int l = 1; l <= k - l + 1; ++l) { n             if (temp % l == 0 && temp / l <= 10111000 && ar[temp / l] == 1) { n               ans = min(ans, l + j); n             } n           } n         } n       } n     } n     if (ans <= k) { n       printf( %d n , ans); n     } else { n       printf( -1 n ); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(0), cin.tie(0), cin.tie(0), cout.tie(0), n       cout.precision(15); n   int t; n   cin >> t; n   while (t--) { n     int n, x; n     set<int> s; n     cin >> n; n     for (int i = int(0); i < int(n); i++) { n       cin >> x; n       s.insert(x); n     } n     cout << s.size() <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int a[10000000], b[10000000], memo[10000000], pos[10000000]; n int min(int l, int r) { n   if (l <= r) { n     return l; n   } n   return r; n } n int main() { n   int n, m, i, j, k, ind, p, l, x; n   fscanf(stdin,  %d %d , &n, &m); n   for (i = 1; i <= n; i++) { n     fscanf(stdin,  %d , &a[i]); n   } n   i = 1; n   ind = 1; n   while (i <= n) { n     j = i; n     k = a[i]; n     memo[ind] = k; n     pos[ind] = i; n     while (j <= n && a[j] == k) { n       b[j] = ind; n       j++; n     } n     i = j; n     ind++; n   } n   for (i = 1; i <= m; i++) { n     fscanf(stdin,  %d %d %d , &p, &l, &x); n     if (b[p] == b[l]) { n       if (x == a[p]) { n         cout <<  -1  << endl; n       } else { n         cout << p << endl; n       } n     } else { n       ind = b[p]; n       if (memo[b[p]] != x) { n         cout << p << endl; n       } else { n         ind = 1; n         while (pos[b[p] + ind] == x) { n           ind++; n         } n         cout << pos[b[p] + ind] << endl; n       } n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n char s[1005]; n long long t, ans = 0; n int x, l; n int main() { n   scanf( %s , s); n   int len = strlen(s); n   for (int i = 0; i < len; i++) { n     if (isalpha(s[i])) { n       if (t) { n         if (l == 2) n           ans += t; n         else n           ans += t * 100; n       } n       t = 0; n       x = 0; n       l = 0; n     } else { n       if (isdigit(s[i])) { n         t = t * 10 + s[i] -  0 ; n         if (x) { n           l++; n         } n       } else { n         x = 1; n         l = 0; n       } n     } n   } n   if (t) { n     if (l == 2) { n       ans += t; n     } else n       ans += t * 100; n   } n   long long temp = ans / 100; n   long long temp1 = ans % 100; n   len = 0; n   string A =   ; n   if (temp == 0) A = A +  0 ; n   while (temp) { n     if (len && len % 3 == 0) { n       A = A +  . ; n     } n     char c =  0  + temp % 10; n     A = A + c; n     temp /= 10; n     len++; n   } n   reverse(A.begin(), A.end()); n   cout << A; n   if (temp1) { n     printf( .%.2I64d , temp1); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<int> g[1000010]; n int n, k; n bool r[1000010]; n int s[1000010]; n int m[1000010]; n int main() { n   scanf( %d %d , &n, &k); n   for (int i = 0; i < k; i++) { n     int x, y; n     scanf( %d %d , &x, &y); n     g[x].push_back(y); n   } n   int v = 0; n   for (int i = 1; i <= n; i++) { n     if (r[i]) continue; n     s[v++] = i; n     r[i] = true; n     for (int j = 0; j < (int)g[i].size(); j++) r[g[i][j]] = true; n   } n   int sz = 0; n   for (int i = 1; i <= n; i++) r[i] = false; n   for (int i = v - 1; i >= 0; i--) { n     if (!r[s[i]]) { n       m[sz++] = s[i]; n       for (int j = 0; j < (int)g[s[i]].size(); j++) r[g[s[i]][j]] = true; n     } n   } n   printf( %d n , sz); n   for (int i = 0; i < sz; i++) printf( %d  , m[i]); n   printf(  n ); n   return 0; n } n


#include <bits/stdc++.h> n #pragma GCC optimize( Ofast ) n using namespace std; n int n, k, a[1010][1010]; n vector<pair<int, int>> ans; n int main() { n   int is = 0; n   ans.clear(); n   memset(a, 0, sizeof(a)); n   cin >> n >> k; n   for (int i = 1; i <= n; ++i) { n     int cnt = 0; n     for (int j = 1; j <= n; ++j) { n       if (i == j) continue; n       if (cnt == k) break; n       if (a[j][i] == 0) { n         a[i][j] = 1; n         ++cnt; n         ans.push_back({i, j}); n       } n     } n     if (cnt != k) { n       cout <<  -1 ; n       return 0; n     } n   } n   if (!is) { n     cout << ans.size() <<   n ; n     for (auto i : ans) { n       cout << i.first <<     << i.second <<   n ; n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n int n, m; n vector<int> adjs[510]; n char type[510]; n bool visited[510]; n int dfs(int v, int adj, char c) { n   visited[v] = true; n   type[v] = c; n   int totV = 1; n   for (int i = 0; i < (adj); i++) { n     int adjV = adjs[v][i]; n     if (!visited[adjV] && adjs[adjV].size() == adj) totV += dfs(adjV, adj, c); n   } n   return totV; n } n int main(int argc, char **argv) { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cin >> n >> m; n   for (int var = 0; var < (n); var++) type[var] = ( x ); n   for (int var = 0; var < (n); var++) visited[var] = (false); n   for (int i = 0; i < (m); i++) { n     int u, v; n     cin >> u >> v; n     u--; n     v--; n     adjs[u].push_back(v); n     adjs[v].push_back(u); n   } n   int adja = -1, adjb = n - 1, adjc = -1; n   int na = 0, nb = 0, nc = 0; n   bool pos = true; n   for (int i = 0; i < (n); i++) { n     int k = adjs[i].size(); n     if (k == adjb) { n       nb++; n       type[i] =  b ; n       visited[i] = true; n     } else if (!visited[i]) { n       if (na == 0) { n         adja = adjs[i].size(); n         na = dfs(i, adja,  a ); n       } else if (nc == 0) { n         adjc = adjs[i].size(); n         nc = dfs(i, adjc,  c ); n       } else n         pos = false; n     } n   } n   type[n] =   0 ; n   if (pos && na + nb + nc == n && (adja == -1 || adja == na + nb - 1) && n       (adjc == -1 || adjc == nb + nc - 1)) { n     cout << ( Yes ) <<   n ; n     cout << (type) <<   n ; n   } else { n     cout << ( No ) <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 2e5 + 9; n const int L = 301; n const int INF = 1e9 + 239; n int n; n int a[N]; n int cnt[N]; n vector<int> pos[N]; n int mn[2 * N]; n void solve(vector<pair<int, int>> v, int &ans) { n   v.push_back({n - 1, 0}); n   int prev = -1; n   int c = 0; n   mn[N] = 0; n   for (auto [pos, val] : v) { n     if (abs(prev) < 2) ans = max(ans, (pos - 1) - mn[c + N] + 1); n     c += val; n     ans = max(ans, pos - mn[c + N] + 1); n     mn[c + N] = min(mn[c + N], pos + 1); n     prev = val; n   } n   c = 0; n   mn[0] = INF; n   for (auto [pos, val] : v) { n     c += val; n     mn[c + N] = INF; n   } n } n signed main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   fill(mn, mn + 2 * N, INF); n   cin >> n; n   for (int i = 0; i < n; i++) { n     cin >> a[i]; n     cnt[a[i]]++; n     pos[a[i]].push_back(i); n   } n   int ans = 0; n   int mx = max_element(cnt, cnt + N) - cnt; n   int pnt = -1; n   int sz = -1; n   vector<pair<int, int>> vplus, vminus; n   auto add = [&](int p) { n     int imx = lower_bound(pos[mx].begin(), pos[mx].end(), p) - pos[mx].begin(); n     int l = max(pnt + 1, imx - (sz + 3)); n     if (pnt + 1 < imx - (sz + 3)) { n       vplus.push_back({pos[mx][pnt + 1], imx - (sz + 3) - (pnt + 1)}); n     } n     for (int i = l; i <= imx + (sz + 3) && i < (int)pos[mx].size(); i++) { n       vplus.push_back({pos[mx][i], 1}); n       pnt = i; n     } n   }; n   auto relax_small = [&](int s) { n     if (pos[s].empty()) { n       return; n     } n     vplus.clear(); n     vminus.clear(); n     sz = (int)pos[s].size(); n     pnt = -1; n     { add(0); } n     for (auto p : pos[s]) { n       add(p); n       vminus.push_back({p, -1}); n     } n     { add(n - 1); } n     vector<pair<int, int>> vall(vplus.size() + vminus.size()); n     merge(vplus.begin(), vplus.end(), vminus.begin(), vminus.end(), n           vall.begin()); n     solve(vall, ans); n   }; n   auto relax_big = [&](int s) { n     if (pos[s].empty()) { n       return; n     } n     vplus.clear(); n     vminus.clear(); n     for (auto p : pos[mx]) { n       vplus.push_back({p, 1}); n     } n     for (auto p : pos[s]) { n       vminus.push_back({p, -1}); n     } n     vector<pair<int, int>> vall(vplus.size() + vminus.size()); n     merge(vplus.begin(), vplus.end(), vminus.begin(), vminus.end(), n           vall.begin()); n     solve(vall, ans); n   }; n   for (int other = 0; other < N; other++) { n     if (other == mx) { n       continue; n     } n     if (pos[other].size() > L) { n       relax_big(other); n     } else { n       relax_small(other); n     } n   } n   cout << ans <<   n ; n } n


#pragma GCC optimize ( O3 ) n #pragma GCC target ( sse4 ) n #include <bits/stdc++.h> n using namespace std; n typedef long long ll; n typedef long double ld; n typedef vector<int> vi; n typedef pair<int, int> pi; n #define debug(x) cerr << #x <<  :   << x <<   n  n #define debug2(x, y) debug(x), debug(y) n #define repn(i, a, b) for(int i = (int)(a); i < (int)(b); i++) n #define rep(i, a) for(int i = 0; i < (int)(a); i++) n #define all(v) v.begin(), v.end() n #define mp make_pair n #define pb push_back n #define lb lower_bound n #define ub upper_bound n #define fi first n #define se second n #define sq(x) ((x) * (x)) n const int MOD = 1e9 + 7; n const int mxN = 205; n  n template<class T> T gcd(T a, T b){ return ((b == 0) ? a : gcd(b, a % b)); } n  n vi g[mxN]; n int up[mxN][10]; n ll p[mxN][mxN]; n int st[mxN], en[mxN], d[mxN]; n int cnt = 0; n  n void dfs(int cur, int prev, int di = 0){ n  st[cur] = cnt++; n  d[cur] = di; n  up[cur][0] = prev; n  repn(i, 1, 10) up[cur][i] = up[up[cur][i - 1]][i - 1]; n  for(int x : g[cur]) if(x != prev) dfs(x, cur, di + 1); n  en[cur] = cnt++; n } n  n bool upper(int a, int b){ n  return st[a] <= st[b] && en[a] >= en[b]; n } n  n int lca(int a, int b){ n  if(upper(a, b)) return a; n  if(upper(b, a)) return b; n  for(int i = 9; i >= 0; i--) if(!upper(up[a][i], b)) a = up[a][i]; n  return up[a][0]; n } n  n ll mypow(ll a, ll p){ n  if(p == 0) return 1; n  if(p == 1) return (a % MOD); n  if(p & 1) return ((a % MOD) * (mypow(a, p - 1) % MOD)) % MOD; n  ll x = mypow(a, p / 2) % MOD; n  return (x * x) % MOD; n } n  n ll inv(ll base){ n  return mypow(base, MOD - 2); n } n  n int main(){ n  ios_base::sync_with_stdio(false); n  cin.tie(0); n  //freopen( input.in ,  r , stdin); n  //freopen( output.out ,  w , stdout); n  ll n; n  cin >> n; n  rep(i, n - 1){ n   int x, y; n   cin >> x >> y; n   x--, y--; n   g[x].pb(y); n   g[y].pb(x); n  } n  p[0][0] = 1LL; n  rep(i, mxN - 1){ n   rep(j, i + 1){ n    int k = i - j; n    (p[j + 1][k] += (p[j][k] * inv(2)) % MOD) %= MOD; n    (p[j][k + 1] += (p[j][k] * inv(2)) % MOD) %= MOD; n   } n  } n  rep(k, n) repn(j, 1, n) (p[j][k] += p[j - 1][k]) %= MOD; n  ll ans = 0LL; n  rep(i, n){ n   cnt = 0; n   dfs(i, i); n   rep(j, n) repn(k, j + 1, n){ n    if(upper(k, j)) ans++; n    else if(!upper(j, k)){ n     int c = lca(j, k); n     int a = d[j] - d[c], b = d[k] - d[c]; n     (ans += (p[a - 1][b - 1] * inv(2)) % MOD) %= MOD; n    } n   } n  } n  cout << (ans * inv(n)) % MOD <<   n ; n  return 0; n } n /* n Things to look out for: n  - Integer overflows n  - Make sure that max is large enough (2e9, 4e18) n  - Special cases n Be careful! n */ n


#include <bits/stdc++.h> n #pragma comment(linker,  /STACK:2000000 ) n #pragma comment(linker,  /HEAP:2000000 ) n using namespace std; n void print_width(long long x) { n   std::cout << std::fixed; n   std::cout << std::setprecision(x); n } n long long power(long long x, long long y, long long p = 1000000007) { n   long long res = 1; n   x = x % p; n   while (y > 0) { n     if (y & 1) res = (res * x) % p; n     y = y >> 1; n     x = (x * x) % p; n   } n   return res; n } n void printArr(long long a[], long long n) { n   for (long long i = 0; i < n; i++) cout << a[i] <<    ; n   cout <<   n ; n } n void printVector(std::vector<long long> v) { n   for (long long i = 0; i < v.size(); i++) cout << v[i] <<    ; n   cout <<   n ; n } n void printVectorPair(std::vector<pair<long long, long long>> v) { n   for (long long i = 0; i < v.size(); i++) n     cout << v[i].first <<     << v[i].second <<   n ; n   ; n   cout <<   n ; n } n void initialize(long long arr[], long long n) { n   for (long long i = 0; i <= n; i++) arr[i] = i; n } n long long root(long long arr[], long long i) { n   while (arr[i] != i) { n     arr[i] = arr[arr[i]]; n     i = arr[i]; n   } n   return i; n } n void Union(long long arr[], long long a, long long b) { n   long long root_a = root(arr, a); n   long long root_b = root(arr, b); n   arr[root_a] = root_b; n } n long long gcd(long long a, long long b) { n   if (b == 0) n     return a; n   else n     return gcd(b, a % b); n } n long long power_wm(long long x, long long y) { n   long long res = 1; n   while (y > 0) { n     if (y & 1) res = (res * x); n     y = y >> 1; n     x = (x * x); n   } n   return res; n } n std::vector<long long> vsum(std::vector<long long> a) { n   std::vector<long long> s(a.size()); n   s[0] = a[0]; n   for (long long i = 1; i <= a.size() - 1; i++) { n     s[i] = s[i - 1] + a[i]; n   } n   return s; n } n void time() {} n long long sti(string s) { n   long long ans = 0; n   long long p = 1; n   for (long long i = s.size() - 1; i >= 0; i--) { n     ans = (ans + ((long long)(s[i] -  0 ) * p) % 1000000007) % 1000000007; n     p = (p * 10) % 1000000007; n   } n   return ans; n } n long long bfs(long long x, long long y, vector<vector<long long>> &g) { n   std::vector<long long> vis(100001, 0); n   vis[x] = 1; n   queue<long long> q; n   q.push(x); n   std::vector<long long> level(100001, (long long)1e18); n   level[x] = 0; n   while (!q.empty()) { n     long long p = q.front(); n     q.pop(); n     for (long long i = 0; i < g[p].size(); i++) { n       long long p2 = g[p][i]; n       if (vis[p2] == 0) { n         if (x == p and y == p2) { n           continue; n         } else { n           q.push(p2); n           vis[p2] = 1; n           level[p2] = level[p] + 1; n         } n       } n     } n   } n   return level[y]; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   long long N = 5000001; n   std::vector<long long> lp(N, 0); n   std::vector<long long> pr; n   for (long long i = 2; i <= N - 1; i++) { n     if (lp[i] == 0) { n       lp[i] = i; n       pr.push_back(i); n     } n     for (long long j = 0; n          j < (long long)pr.size() and pr[j] <= lp[i] and i * pr[j] <= N; j++) n       lp[i * pr[j]] = pr[j]; n   } n   lp[1] = 1; n   std::vector<long long> count(N, 0); n   for (long long i = 2; i <= N - 1; i++) { n     long long prm = i; n     count[i]++; n     while (lp[prm] != prm) { n       count[i]++; n       prm = prm / lp[prm]; n     } n   } n   count = vsum(count); n   long long t; n   cin >> t; n   while (t--) { n     long long a, b; n     cin >> a >> b; n     cout << count[a] - count[b] <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n clock_t __stt; n inline void TStart() { __stt = clock(); } n inline void TReport() { n   printf(  nTaken Time : %.3lf sec n , n          (double)(clock() - __stt) / CLOCKS_PER_SEC); n } n template <typename T> n T MIN(T a, T b) { n   return a < b ? a : b; n } n template <typename T> n T MAX(T a, T b) { n   return a > b ? a : b; n } n template <typename T> n T ABS(T a) { n   return a > 0 ? a : (-a); n } n template <typename T> n void UMIN(T &a, T b) { n   if (b < a) a = b; n } n template <typename T> n void UMAX(T &a, T b) { n   if (b > a) a = b; n } n const int bc = 6, mc = 64; n int n, m, subcnt[mc], chcnt[mc], avai[100005]; n char s[100005], tmp[10]; n int main() { n   int i, j, k; n   scanf( %s , s); n   n = strlen(s); n   scanf( %d , &m); n   fill(avai, avai + n, mc - 1); n   while (m--) { n     scanf( %d%s , &j, tmp); n     k = strlen(tmp); n     avai[--j] = 0; n     for (i = 0; i < k; ++i) { n       avai[j] += (1 << (tmp[i] -  a )); n     } n   } n   for (i = 0; i < n; ++i) { n     for (j = 0; j < mc; ++j) { n       if ((avai[i] & j) == avai[i]) { n         ++subcnt[j]; n       } n       if ((1 << (s[i] -  a )) & j) { n         ++chcnt[j]; n       } n     } n   } n   for (i = 0; i < n; ++i) { n     for (j = 0; j < bc; ++j) { n       if (!(avai[i] & (1 << j))) continue; n       s[i] =  a  + j; n       for (k = 0; k < mc; ++k) { n         if ((avai[i] & k) == avai[i]) { n           --subcnt[k]; n         } n         if ((1 << j) & k) { n           --chcnt[k]; n         } n       } n       for (k = 0; k < mc; ++k) { n         if (chcnt[k] < subcnt[k]) break; n       } n       if (k == mc) break; n       for (k = 0; k < mc; ++k) { n         if ((avai[i] & k) == avai[i]) { n           ++subcnt[k]; n         } n         if ((1 << j) & k) { n           ++chcnt[k]; n         } n       } n     } n     if (j == bc) { n       printf( Impossible n ); n       return 0; n     } n   } n   printf( %s n , s); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int lastr, lastc; n int n, m, a, b; n int row1[10], rcnt, ans; n int main() { n   int i, j, k; n   while (scanf( %d%d%d%d , &n, &m, &a, &b) != EOF) { n     if (m == 1) { n       printf( 1 n ); n       continue; n     } n     lastr = (n + m - 1) / m; n     lastc = n % m; n     lastc = lastc == 0 ? m : lastc; n     ans = 0, rcnt = 0; n     row1[++rcnt] = (a + m - 1) / m; n     if ((b + m - 1) / m != (a + m - 1) / m) { n       if ((b + m - 1) / m == (a + m - 1) / m + 1) n         row1[++rcnt] = (b + m - 1) / m; n       else n         row1[++rcnt] = row1[1] + 1, row1[++rcnt] = (b + m - 1) / m; n     } n     int ta = a % m, tb = b % m; n     if (ta == 0) ta = m; n     if (tb == 0) tb = m; n     if (rcnt == 1) n       printf( 1 n ); n     else if (rcnt == 2) { n       if (ta == 1 && n           (row1[2] != lastr && tb == m || row1[2] == lastr && tb == lastc)) n         printf( 1 n ); n       else n         printf( 2 n ); n     } else { n       if (ta == 1 && n           (row1[3] != lastr && tb == m || row1[3] == lastr && tb == lastc)) n         printf( 1 n ); n       else if (ta == 1 || tb == m || ta - 1 == tb || n                (lastr == row1[3] && lastc == tb)) n         printf( 2 n ); n       else n         printf( 3 n ); n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n using ll = long long; n void solve() { n   int n; n   cin >> n; n   string s = string(n,  ( ) + string(n,  ) ); n   cout << s <<   n ; n   for (int i = 0; i + 1 < n; ++i) { n     s[i + 1] =  ) ; n     s[2 * n - 2] =  ( ; n     cout << s <<   n ; n     s[2 * n - 2] =  ) ; n     s[i + 1] =  ( ; n   } n } n int main() { n   ios::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr), n       cout.precision(12); n   int t; n   cin >> t; n   while (t--) solve(); n } n


#include <bits/stdc++.h> n long long minus(long long k) { return (1000000009LL - 1 + k) % 1000000009LL; } n long long multiply(long long k) { return (k * 2) % 1000000009LL; } n long long multiply(long long a, long long b) { return (a * b) % 1000000009LL; } n int main() { n   int n, m; n   scanf( %d %d , &n, &m); n   long long p = 1; n   for (int i = 0; i < m; i++) p = multiply(p); n   if (m < 20 && p < n) { n     printf( 0 n ); n     return 0; n   } n   long long ans = 1; n   for (int i = 0; i < n; i++) { n     p = minus(p); n     ans = multiply(ans, p); n   } n   printf( %I64d n , ans); n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 1e6 + 5; n const long long MOD = 1e9 + 7; n set<char> seen; n void solve() { n   int n; n   cin >> n; n   string a; n   string b; n   cin >> a >> b; n   for (int i = 0; i < n; i++) { n     if (a[i] > b[i]) { n       cout <<  -1 n ; n       return; n     } n   } n   int ans = 0; n   for (char c =  a ; c <=  t ; c++) { n     char smallest =  z ; n     for (int i = 0; i < n; i++) { n       if (a[i] == c) { n         if (a[i] != b[i]) smallest = min(smallest, b[i]); n       } n     } n     if (smallest !=  z ) { n       ans++; n       for (int i = 0; i < n; i++) { n         if (a[i] == c) { n           if (a[i] != b[i]) { n             a[i] = smallest; n           } n         } n       } n     } n   } n   cout << ans <<   n ; n } n int main() { n   cin.sync_with_stdio(0); n   cin.tie(0); n   int TC; n   cin >> TC; n   while (TC--) { n     solve(); n   } n } n


#include <bits/stdc++.h> n #pragma GCC optimize(2) n #pragma G++ optimize(2) n using namespace std; n char nc() { return getchar(); } n int read() { n   int x = 0, y = 1; n   char c = nc(); n   while (!isdigit(c)) { n     if (c ==  - ) y = -1; n     c = nc(); n   } n   while (isdigit(c)) { n     x = (x << 1) + (x << 3) + (c ^ 48); n     c = nc(); n   } n   return x * y; n } n int n, p, q, up, prime[135], vst[135], bin[135]; n unsigned int now = 1, C[135], ANS = 0; n void divide(int x, int op) { n   for (int i = 1; i <= prime[0]; i++) n     while (!(x % prime[i])) bin[i] += op, x /= prime[i]; n   if (op == 1 && x > 1) now *= (unsigned int)x; n } n unsigned int quick_pow(unsigned int x, int y) { n   unsigned int re = 1; n   while (y) { n     if (y & 1) re = re * x; n     x = x * x; n     y >>= 1; n   } n   return re; n } n void pre() { n   for (int i = 2; i <= up; i++) { n     if (!vst[i]) prime[++prime[0]] = i; n     for (int j = 1; j <= prime[0]; j++) { n       if (i * prime[j] > up) break; n       vst[i * prime[j]] = 1; n       if (!(i % prime[j])) break; n     } n   } n   C[0] = 1; n   for (int i = 1; i <= up; i++) { n     divide(n - i + 1, 1); n     divide(i, -1); n     C[i] = now; n     for (int j = 1; j <= prime[0]; j++) C[i] *= quick_pow(prime[j], bin[j]); n   } n } n int main() { n   n = read(); n   p = read(); n   q = read(); n   up = min(n - 1, p); n   pre(); n   for (int i = 1; i <= q; i++) { n     unsigned int sm = 0, ha = 1; n     for (int j = 0; j <= up; j++) sm += C[j] * ha, ha *= (unsigned int)i; n     ANS ^= (sm * (unsigned int)i); n   } n   cout << ANS; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m, k; n string s; n long long ans; n bool flag; n struct A { n   int id, p, s; n }; n bool comp(A a, A b) { n   if (a.p > b.p) return true; n   return false; n } n void solve() { n   cin >> n >> m >> k; n   vector<int> p(n); n   for (auto i = 0; i < n; i++) cin >> p[i]; n   ; n   vector<int> s(n); n   for (auto i = 0; i < n; i++) cin >> s[i]; n   ; n   vector<A> v(n); n   for (auto i = 0; i < n; i++) { n     v[i].id = i + 1; n     v[i].s = s[i]; n     v[i].p = p[i]; n   } n   sort(v.begin(), v.end(), comp); n   unordered_set<int> school; n   unordered_set<int> kid; n   for (auto i = 0; i < n; i++) { n     if (school.find(v[i].s) == school.end()) { n       school.insert(v[i].s); n       kid.insert(v[i].id); n     } n     int mm = school.size(); n     if (mm == m) break; n   } n   vector<int> K(k); n   for (auto i = 0; i < k; i++) cin >> K[i]; n   ; n   int count = 0; n   for (auto i = 0; i < k; i++) { n     if (kid.find(K[i]) == kid.end()) { n       count++; n     } n   } n   cout << count; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   int t = 1; n   while (t--) { n     solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long N = 2e5 + 3, mod = 1e9 + 7, inf = 3e18 + 7; n long long tc, n, m, a, b, c, d, i, j, k; n long long A[N], odd[N], even[N]; n string s[N]; n vector<long long> vec; n int main() { n   ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0); n   cin >> tc; n   while (tc--) { n     cin >> n; n     long long sum = 0, ans = 0, mx; n     for (i = 1; i <= n; i++) cin >> A[i], sum += (i % 2 ? A[i] : 0); n     A[n + 1] = A[n]; n     for (i = 2; i <= n; i += 2) even[i] = even[i - 2] + A[i] - A[i - 1]; n     for (i = 2; i <= n; i += 2) odd[i] = odd[i - 2] + A[i] - A[i + 1]; n     if (n % 2) n--; n     for (i = n, mx = even[n]; i > 0; i -= 2) n       mx = max(mx, even[i]), ans = max(ans, mx - even[i - 2]); n     for (i = n, mx = odd[n]; i > 0; i -= 2) n       mx = max(mx, odd[i]), ans = max(ans, mx - odd[i - 2]); n     cout << ans + sum << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long x, y; n   cin >> x >> y; n   long long n; n   cin >> n; n   n %= 6; n   if (n == 0) n = 6; n   long long data[7]; n   data[1] = x; n   data[2] = y; n   for (int i = 3; i <= 6; i++) { n     data[i] = data[i - 1] - data[i - 2]; n   } n   for (int i = 1; i <= 6; i++) n     if (data[i] < 0) data[i] += 1000000007; n   if (data[n] < 0) data[n] += 1000000007; n   cout << data[n] % 1000000007 <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n long long read() { n   long long res = 0, flag = 1; n   char ch = getchar(); n   while (!isdigit(ch)) { n     if (ch ==  - ) flag = -1; n     ch = getchar(); n   } n   while (isdigit(ch)) res = res * 10 + ch - 48, ch = getchar(); n   return res * flag; n } n void print(long long x) { n   if (x < 0) putchar( - ), x = -x; n   if (x > 9) print(x / 10); n   putchar(x % 10 + 48); n } n const long long N = 5000 + 5; n long long n, k, a[N], c[N]; n std::vector<long long> g[N]; n void solution() { n   n = read(), k = read(); n   long long tot = 0; n   for (long long i = (1); i <= (n); ++i) a[i] = read(), g[a[i]].push_back(i); n   for (long long i = (1); i <= (N - 1); ++i) { n     if (g[i].size() > k) { n       puts( NO ); n       return; n     } n     for (auto v : g[i]) c[v] = ++tot, tot %= k; n   } n   puts( YES ); n   for (long long i = (1); i <= (n); ++i) print(c[i]), putchar(   ); n   putchar(  n ); n } n signed main() { n   42; n   solution(); n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long t; n   cin >> t; n   while (t--) { n     long long n; n     cin >> n; n     vector<long long> v(n), vec; n     for (int i = 0; i < n; i++) cin >> v[i]; n     for (int i = 0; i < n; i++) { n       if (v[i] == -1) continue; n       if (i - 1 >= 0 && v[i - 1] == -1) vec.push_back(v[i]); n       if (i + 1 < n && v[i + 1] == -1) vec.push_back(v[i]); n     } n     sort(vec.begin(), vec.end()); n     long long num, res = -1; n     if (vec.size() == 0) num = 1; n     if (vec.size() == 1) num = vec[0]; n     if (vec.size() >= 2) num = (vec[vec.size() - 1] + vec[0]) / 2; n     for (int i = 0; i < n; i++) { n       if (v[i] == -1) v[i] = num; n     } n     for (int i = 0; i < n - 1; i++) { n       res = max(res, abs(v[i] - v[i + 1])); n     } n     cout << res <<     << num << endl; n   } n   return 0; n } n


// annotshy n  n #include <bits/stdc++.h> n #define Task  CF  n #define pb push_back n #define MOD 1000000007 n #define F first n #define S second n #define int long long n  n using namespace std; n  n const int maxn = 2005; n  n int n, m, w; n int a[maxn][maxn]; n int dx[5] = {1, 0, -1, 0}; n int dy[5] = {0, 1, 0, -1}; n long long dp[maxn][maxn], to1 = 0, to2 = 0; n  n long long BFS(int i, int j) { n   queue<pair<int, int> > Q; n   Q.push({i, j}); n   for(int p = 1; p <= n; ++ p) { n     for(int q = 1; q <= m; ++ q) dp[p][q] = 1e18; n   } n   dp[i][j] = 0; n   while(Q.size()) { n     int u = Q.front().F; n     int v = Q.front().S; n     Q.pop(); n     for(int k = 0; k < 4; ++ k) { n       int x = u + dx[k]; n       int y = v + dy[k]; n       if(x < 1 || y < 1 || x > n || y > m || a[x][y] == -1) continue; n       if(dp[x][y] > dp[u][v] + w) { n         dp[x][y] = dp[u][v] + w; n         Q.push({x, y}); n       } n     } n   } n   long long res = 1e18; n   if(i == 1 && j == 1) to1 = dp[n][m]; n   if(i == n && j == m) to2 = dp[1][1]; n   for(int i = 1; i <= n; ++ i) { n     for(int j = 1; j <= m; ++ j) if(a[i][j] > 0) { n       res = min(res, dp[i][j] + a[i][j]); n     } n   } n   return res; n } n  n void Solve() { n   cin >> n >> m >> w; n   for(int i = 1; i <= n; ++ i) { n     for(int j = 1; j <= m; ++ j) cin >> a[i][j]; n   } n   long long res1 = BFS(1, 1); n   long long res2 = BFS(n, m); n   if(res1 + res2 >= 1e18 && to1 == 1e18 && to2 == 1e18) cout << - 1 <<   n ; n   else cout << min({to1, to2, res1 + res2}) <<   n ; n } n  n signed main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0), cout.tie(0); n   if(fopen(Task .inp ,  r )) { n     freopen(Task .inp ,  r , stdin); n     freopen(Task .out ,  w , stdout); n   } n   int test = 1; n //  cin >> test; n   while(test --) { n     Solve(); n   } n } n


#include <bits/stdc++.h> n using namespace std; n int mx, n, m, sum, hh, la, v[1000001], a[100001], sz[1000010], tot, e[10000001], n     nt[10000001], hd[1000010]; n void build(int x, int y) { n   tot++; n   e[tot] = y; n   nt[tot] = hd[x]; n   hd[x] = tot; n } n int main() { n   int i, j, k; n   mx = 1e6; n   scanf( %d%d , &n, &m); n   la = m; n   m = m * (m + 1) / 2; n   for (i = 1; i <= n; i++) scanf( %d , &a[i]); n   sort(a + 1, a + n + 1); n   for (i = 1; i <= n; i++) n     for (j = 1; j < i; j++) sz[a[i] - a[j]]++; n   for (i = 1; i <= n; i++) n     for (j = 1; j < i; j++) n       if (sz[a[i] - a[j]] <= m) build(a[i] - a[j], a[j]); n   for (i = 1; i <= mx + 1; i++) { n     sum = 0; n     for (j = i; j <= mx + 1; j += i) sum += sz[j]; n     if (sum > m) continue; n     for (j = i; j <= mx + 1; j += i) n       for (k = hd[j]; k; k = nt[k]) n         if (v[e[k]] != i) { n           v[e[k]] = i; n           hh++; n         } n     if (hh <= la) return printf( %d , i), 0; n     hh = 0; n   } n } n


#include <bits/stdc++.h> n using namespace std; n long long n, cv; n long long level[3501]; n long long vis[3501] = {}; n vector<long long> graph[3501]; n vector<long long> cycles[3501]; n vector<long long> z; n void dfs_cycle(long long u, long long p, long long color[], long long mark[], n                long long par[], long long& cyclenumber) { n   if (color[u] == 2) { n     return; n   } n   if (color[u] == 1) { n     cyclenumber++; n     long long cur = p; n     mark[cur] = cyclenumber; n     while (cur != u) { n       cur = par[cur]; n       mark[cur] = cyclenumber; n     } n     return; n   } n   par[u] = p; n   color[u] = 1; n   for (long long v : graph[u]) { n     if (v == par[u]) { n       continue; n     } n     dfs_cycle(v, u, color, mark, par, cyclenumber); n   } n   color[u] = 2; n } n void printCycles(long long edges, long long mark[], long long& cyclenumber) { n   for (long long i = 1; i <= edges; i++) { n     if (mark[i] != 0) cycles[mark[i]].push_back(i); n   } n   for (long long i = 1; i <= cyclenumber; i++) { n     for (long long x : cycles[i]) z.push_back(x); n   } n } n void bfs(long long s) { n   queue<long long> q; n   q.push(s); n   level[s] = 0; n   vis[s] = 1; n   while (!q.empty()) { n     long long p = q.front(); n     q.pop(); n     for (long long i = 0; i < graph[p].size(); i++) { n       if (vis[graph[p][i]] == 0) { n         level[graph[p][i]] = min(level[graph[p][i]], level[p] + 1); n         q.push(graph[p][i]); n         vis[graph[p][i]] = 1; n       } n     } n   } n } n void quickly_quickly_kro() { n   cin >> n; n   for (long long i = 1; i < n + 1; i++) { n     long long a, b; n     cin >> a >> b; n     graph[a].push_back(b); n     graph[b].push_back(a); n   } n   long long color[3501]; n   long long par[3501]; n   long long mark[3501]; n   long long cyclenumber = 0; n   long long edges = n; n   dfs_cycle(1, 0, color, mark, par, cyclenumber); n   printCycles(edges, mark, cyclenumber); n   for (long long i = 1; i < n + 1; i++) level[i] = (long long)2e18; n   for (auto k : z) { n     for (long long i = 1; i < n + 1; i++) { n       vis[i] = 0; n     } n     bfs(k); n   } n   for (long long i = 1; i < n + 1; i++) { n     cout << level[i] <<    ; n   } n } n signed main() { n   std::ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   long long t; n   t = 1; n   while (t--) { n     quickly_quickly_kro(); n     cout <<   n ; n   } n   return 0; n } n


#include <iostream> n #include <map> n #include <set> n #include <queue> n #include <stack> n #include <algorithm> n #include <vector> n #include <string> n #include <iomanip> n #include <cmath> n #include <cstdio> n #include <cstring> n #include <climits> n #define Buff std::ios::sync_with_stdio(false), cin.tie(0), cout.tie(0) n #define ll long long n #define inf LONG_LONG_MAX n #define Inf INT_MAX n #define endl   n  n  n using namespace std; n  n const int Maxn = 1e3 + 10; n const ll Mod = 1e9 + 7; n  n int t, n; n char bd[Maxn][Maxn]; n bool firPos1; n int posX1, posX2, posX3, posX4; n int posY1, posY2, posY3, posY4; n int dis; n  n void work() n { n     firPos1 = false; n     cin >> n; n     for (int i = 1; i <= n; i++) n     for (int j = 1; j <= n; j++) n     { n         cin >> bd[i][j]; n         if (bd[i][j] ==  * ) n         { n             if (firPos1 == false) n             { n                 posX1 = i; n                 posY1 = j; n                 firPos1 = true; n             }else n             { n                 posX2 = i; n                 posY2 = j; n             } n         } n     } n     if (posX2 == posX1) n     { n         dis = 1; n         if (posX1 - dis >= 1) n         { n             posX3 = posX1 - dis; n             posX4 = posX1 - dis; n             posY3 = posY1; n             posY4 = posY2; n         }else n         { n             posX3 = posX1 + dis; n             posX4 = posX1 + dis; n             posY3 = posY1; n             posY4 = posY2; n         } n     }else n     if (posY2 == posY1) n     { n         dis = 1; n         if (posY1 - dis >= 1) n         { n             posY3 = posY1 - dis; n             posY4 = posY1 - dis; n             posX3 = posX1; n             posX4 = posX2; n         }else n         { n             posY3 = posY1 + dis; n             posY4 = posY1 + dis; n             posX3 = posX1; n             posX4 = posX2; n         } n     }else n     { n         int minx = min(posX1,posX2); n         int maxx = max(posX1,posX2); n         int miny = min(posY1,posY2); n         int maxy = max(posY1,posY2); n         posX1 = minx; n         posY1 = miny; n         posX2 = minx; n         posY2 = maxy; n         posX3 = maxx; n         posY3 = miny; n         posX4 = maxx; n         posY4 = maxy; n     } n     bd[posX1][posY1] =  * ; n     bd[posX2][posY2] =  * ; n     bd[posX3][posY3] =  * ; n     bd[posX4][posY4] =  * ; n     for (int i = 1; i <= n; i++) n     { n         for (int j = 1; j <= n; j++) n             cout << bd[i][j]; n         cout << endl; n     } n } n  n int main() n { n     Buff; n     cin >> t; n     while (t--) n         work(); n     return 0; n }


#include <bits/stdc++.h> n int main() { n   int t, i, j, k; n   scanf( %d , &t); n   int ranges[t][2000]; n   int len[t]; n   for (i = 0; i < t; ++i) { n     scanf( %d , &len[i]); n     for (j = 0; j < (2 * len[i]); j += 2) { n       scanf( %d %d , &ranges[i][j], &ranges[i][j + 1]); n     } n   } n   for (i = 0; i < t; ++i) { n     int num[len[i]]; n     for (j = 0; j < len[i]; ++j) { n       num[j] = j + 1; n     } n     int sort[2 * len[i]]; n     int c = 0, diff = 0; n     while (c < 2 * len[i]) { n       for (j = 0; j < (2 * len[i]); j += 2) { n         if (ranges[i][j + 1] - ranges[i][j] == diff) { n           sort[c] = ranges[i][j]; n           sort[c + 1] = ranges[i][j + 1]; n           c += 2; n         } n       } n       diff += 1; n     } n     for (j = 0; j < (2 * len[i]); j += 2) { n       int d; n       diff = sort[j + 1] - sort[j]; n       if (diff == 0) { n         d = sort[j]; n         num[d - 1] = 0; n         printf( %d %d %d n , sort[j], sort[j + 1], d); n       } else { n         c = sort[j + 1]; n         while (c >= sort[j]) { n           if (num[c - 1] != 0) { n             d = c; n             num[c - 1] = 0; n             printf( %d %d %d n , sort[j], sort[j + 1], d); n             break; n           } n           c -= 1; n         } n       } n     } n     printf(  n ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int pp(int n) { n   for (int i = 2; i <= floor(sqrt(n)); i++) { n     if (n % i == 0) return i; n   } n   return 1; n } n void solve() { n   long long int n, k; n   cin >> n >> k; n   if (k > n) n     cout << max(0ll, n - k / 2) << endl; n   else n     cout << max(0ll, (k - 1) / 2) << endl; n } n int main() { n   int t = 1; n   while (t--) solve(); n } n


#include <bits/stdc++.h> n using namespace std; n using ll = long long; n int main() { n   ios::sync_with_stdio(0), cin.tie(0); n   ; n   int n, m; n   cin >> n >> m; n   vector<vector<char>> v(n, vector<char>(m)); n   for (int i = 0; i < n; i++) n     for (int j = 0; j < m; j++) cin >> v[i][j]; n   bool st = 1; n   int mx = 0; n   set<int> se; n   for (int i = 0; i < n; i++) { n     int s = -1, g = -1; n     for (int j = 0; j < m; j++) { n       if (v[i][j] ==  G ) n         g = j; n       else if (v[i][j] ==  S ) n         s = j; n     } n     if (s < g) n       st = 0; n     else n       se.insert(s - g); n   } n   if (st) n     cout << se.size(); n   else n     cout << -1; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   priority_queue<int, vector<int>, greater<int> > a; n   int temp; n   int n, i; n   int sum = 0; n   scanf( %d , &n); n   for (i = 1; i <= n; i++) { n     scanf( %d , &temp); n     a.push(temp); n   } n   temp = -1; n   while (!a.empty()) { n     if (a.top() == temp) { n       printf( -1 n ); n       return 0; n     } else if (a.top() == 0) { n       a.pop(); n     } else { n       temp = a.top(); n       a.pop(); n       if (!a.empty() && a.top() == temp) { n         sum++; n         a.pop(); n       } n     } n   } n   printf( %d n , sum); n   return 0; n } n


#include <bits/stdc++.h> n int main() { n   using namespace std; n   int num, t1, t2, k; n   cin >> num >> t1 >> t2 >> k; n   double ms[1000]; n   double ms2[1000]; n   for (int i = 0; i < 1000; i++) { n     ms[i] = 0; n     ms2[i] = 0; n   } n   for (int i = 0; i < num; i++) { n     int a, b; n     cin >> a >> b; n     double s1, s2; n     s1 = a * t1 * ((100 - k) / 100.0) + t2 * b; n     s2 = b * t1 * ((100 - k) / 100.0) + t2 * a; n     if (s1 >= s2) n       ms[i] = s1; n     else n       ms[i] = s2; n     ms2[i] = ms[i]; n   } n   stable_sort(ms, ms + num); n   int same; n   for (int i = num - 1; i >= 0; i--) { n     if (i < num - 1 && ms[i] == ms[i + 1]) n       same++; n     else n       same = 0; n     int p; n     int pass = same; n     for (int j = 0; j < num; j++) { n       if (ms2[j] == ms[i]) { n         if (pass > 0) { n           pass--; n           continue; n         } n         p = j + 1; n         break; n       } n     } n     double d = ((int)(ms[i] * 100 + 0.5)) / 100.0; n     printf( %d %3.2f n , p, d); n   } n } n


#include <bits/stdc++.h> n using namespace std; n long long int x[1000007], y[1000007], z[1000007], n; n string s, S; n vector<long long int> v; n int main() { n   { n     long long int a = 0, b = 0, c = 0, d, e, f, g, i, j, k, l, m, p, q, r, u, w, n                   t, tc, in, mod, loc, diff, val, sz, lo, hi, mid, n                   mn = 2134567891, mx = 0, sum = 0, ans = 0; n     cin >> s; n     l = s.size(); n     for (i = 0; i < l; i++) n       if (s[i] ==  a ) a++; n     if (a * 2 > l) n       cout << l << endl; n     else n       cout << a * 2 - 1 << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long double PI = 3.141592653589793238462643l; n void solve(); n int main() { n   solve(); n   return 0; n } n long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; } n void solve() { n   long long a, b, x1, y1, x2, y2; n   cin >> a >> b >> x1 >> y1 >> x2 >> y2; n   long long ans1, ans2; n   long long eps11 = 0, eps12 = 0, eps21 = 0, eps22 = 0; n   if (x1 + y1 < 0) eps11 = -2 * a; n   if (x2 + y2 < 0) eps12 = -2 * a; n   if (x1 - y1 < 0) eps21 = -2 * b; n   if (x2 - y2 < 0) eps22 = -2 * b; n   ans1 = (x1 + y1 + eps11) / (2 * a) - (eps12 + x2 + y2) / (2 * a); n   ans2 = (eps21 + x1 - y1) / (2 * b) - (eps22 + x2 - y2) / (2 * b); n   if (ans1 < 0) ans1 = -ans1; n   if (ans2 < 0) ans2 = -ans2; n   cout << max(ans1, ans2); n } n


#include <bits/stdc++.h> n using namespace std; n long long a, b, c, d, L, R, k, l, kk, ll, l1, r1, l2, r2, x, gcd, lcm; n inline long long exgcd(long long a, long long b, long long &x, long long &y) { n   if (!b) { n     return x = 1, y = 0, a; n   } n   long long t = exgcd(b, a % b, y, x); n   return y -= a / b * x, t; n } n int main() { n   scanf( %lld%lld%lld%lld%lld%lld , &a, &c, &b, &d, &L, &R); n   gcd = exgcd(a, b, k, l), lcm = a * b / gcd; n   if ((d - c) % gcd) return puts( 0 ), 0; n   k *= (d - c) / gcd, kk = lcm / a; n   l *= (c - d) / gcd, ll = lcm / b; n   l1 = max(0ll, (L - c) / a + (L - c > 0 ? (!!((L - c) % a)) : 0)); n   r1 = (R - c) / a - (R - c < 0 ? (!!((R - c) % a)) : 0); n   l2 = max(0ll, (L - d) / b + (L - d > 0 ? (!!((L - d) % b)) : 0)); n   r2 = (R - d) / b - (R - d < 0 ? (!!((R - d) % b)) : 0); n   x = max((l1 - k) / kk, (l2 - l) / ll); n   k += x * kk, l += x * ll; n   if (k < l1 || l < l2) k += kk, l += ll; n   if (k > r1 || l > r2) return puts( 0 ), 0; n   printf( %lld n , min((r1 - k) / kk, (r2 - l) / ll) + 1); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long t = 1; n template <class myType> n void print_arr(myType &arr, long long L, string sep) { n   for (long long(i) = (0); (i) < (L); ++(i)) { n     cout << arr[i]; n     if (i < L - 1) { n       cout << sep; n     } n   } n   cout <<   n ; n   return; n } n long long merge(long long arr[], long long temp_arr[], long long left, n                 long long mid, long long right) { n   long long i, j, k; n   i = left; n   j = mid + 1; n   k = left; n   long long inv_count = 0; n   while (i <= mid && j <= right) { n     if (arr[i] <= arr[j]) { n       temp_arr[k] = arr[i]; n       k += 1; n       i += 1; n     } else { n       temp_arr[k] = arr[j]; n       inv_count += (mid - i + 1); n       k += 1; n       j += 1; n     } n   } n   while (i <= mid) { n     temp_arr[k] = arr[i]; n     k += 1; n     i += 1; n   } n   while (j <= right) { n     temp_arr[k] = arr[j]; n     k += 1; n     j += 1; n   } n   for (long long loop_var = left; loop_var <= right; loop_var++) { n     arr[loop_var] = temp_arr[loop_var]; n   } n   return inv_count; n } n long long _mergeSort(long long arr[], long long temp_arr[], long long left, n                      long long right) { n   long long inv_count = 0; n   if (left < right) { n     long long mid; n     mid = (left + right) / 2; n     inv_count += _mergeSort(arr, temp_arr, left, mid); n     inv_count += _mergeSort(arr, temp_arr, mid + 1, right); n     inv_count += merge(arr, temp_arr, left, mid, right); n   } n   return inv_count; n } n long long mergeSort(long long arr[], long long n) { n   long long temp_arr[n]; n   for (long long(i) = (0); (i) < (n); ++(i)) temp_arr[i] = 0; n   return _mergeSort(arr, temp_arr, 0, n - 1); n } n void solve() { n   long long n; n   cin >> n; n   long long a[n]; n   for (long long(i) = (0); (i) < (n); ++(i)) cin >> a[i]; n   long long invs, invs2; n   long long b[n]; n   for (long long(i) = (0); (i) < (n); ++(i)) b[i] = a[i]; n   invs = mergeSort(b, n); n   long long ans = 0; n   long long p; n   for (long long(i) = (0); (i) < (30); ++(i)) { n     p = (1 << i); n     for (long long(j) = (0); (j) < (n); ++(j)) b[j] = a[j] ^ p; n     invs2 = mergeSort(b, n); n     if (invs2 < invs) { n       ans += p; n     } n   } n   for (long long(j) = (0); (j) < (n); ++(j)) b[j] = a[j] ^ ans; n   cout << mergeSort(b, n) <<     << ans <<   n ; n   return; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   while (t--) { n     solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, k; n   cin >> n >> k; n   string s; n   cin >> s; n   bool arr[26] = {false}; n   for (int i = 0; i < k; i++) { n     char c; n     cin >> c; n     arr[c -  a ] = true; n   } n   long long ans = 0; n   long long co = 0; n   for (int i = 0; i < n; i++) { n     int x; n     x = s[i] -  a ; n     if (arr[s[i] -  a ] == true) n       co++; n     else { n       ans += (co * (co + 1)) / 2; n       co = 0; n     } n   } n   ans += (co * (co + 1)) / 2; n   cout << ans; n } n


#include <bits/stdc++.h> n inline int gi() { n   int x = 0, f = 1; n   char ch = getchar(); n   while (!isdigit(ch)) { n     if (ch ==  - ) f = -1; n     ch = getchar(); n   } n   while (isdigit(ch)) x = x * 10 + ch -  0 , ch = getchar(); n   return x * f; n } n std::set<std::pair<int, int> > M; n struct yyb { n   int x, y, i; n   long double k, b; n } s[300010], stk[300010]; n inline bool cmp_k(const yyb& a, const yyb& b) { n   if (a.y != b.y) n     return a.y < b.y; n   else n     return a.x > b.x; n } n inline bool cmp_i(const yyb& a, const yyb& b) { return a.i < b.i; } n int main() { n   int n = gi(); n   for (int i = 1; i <= n; ++i) n     s[i].x = gi(), s[i].y = gi(), s[i].i = i, s[i].k = 1000000000.0 / s[i].y, n     s[i].b = 1000000000.0 / s[i].x; n   std::sort(s + 1, s + n + 1, cmp_k); n   int top = 0; n   stk[++top] = (yyb){0, 0, 0, 1e18, 0}; n   for (int i = 1; i <= n; ++i) { n     if (s[i].y == s[i - 1].y) continue; n     while (top > 1 && stk[top].y != stk[top - 1].y && n            (-(s[i].b - stk[top].b) / (s[i].k - stk[top].k) + 1e-15 < n             -(stk[top].b - stk[top - 1].b) / (stk[top].k - stk[top - 1].k))) { n       --top; n     } n     stk[++top] = s[i]; n   } n   std::sort(s + 1, s + n + 1, cmp_i); n   for (int i = 1; i <= top; ++i) M.insert(std::make_pair(stk[i].x, stk[i].y)); n   for (int i = 1; i <= n; ++i) n     if (M.find(std::make_pair(s[i].x, s[i].y)) != M.end()) printf( %d  , i); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 2e6 + 1; n const int INF = 2e9 + 1; n const int MOD = (1e9 + 7); n void bye(string s =   ) { n   cout << s <<   n ; n   exit(0); n } n signed main() { n   ios::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   int t = 1; n   cin >> t; n   while (t--) { n     int n; n     cin >> n; n     vector<int> arr(2); n     for (int i = 0; i < n; i++) { n       int a; n       cin >> a; n       arr[a % 2]++; n     } n     if (arr[1] % 2 || (arr[1] && arr[0])) { n       cout <<  YES n ; n     } else { n       cout <<  NO n ; n     } n   } n   bye(); n } n


#include <bits/stdc++.h> n using namespace std; n const bool debug = false; n const int maxn = 1002003; n const long long magic = 1000 * 1000 * 1000 + 7; n int n, k; n long long aa[maxn]; n struct RMQ { n   int l, r; n   RMQ *left, *right; n   long long res; n   int resIndex; n }; n RMQ* build(long long* vs, int l, int r) { n   RMQ* res = new RMQ(); n   res->l = l; n   res->r = r; n   if (r - l >= 2) { n     int m = (l + r) / 2; n     res->left = build(vs, l, m); n     res->right = build(vs, m, r); n     if (res->left->res >= res->right->res) { n       res->res = res->left->res; n       res->resIndex = res->left->resIndex; n     } else { n       res->res = res->right->res; n       res->resIndex = res->right->resIndex; n     } n   } else { n     res->left = nullptr; n     res->right = nullptr; n     res->res = vs[l]; n     res->resIndex = l; n   } n   return res; n } n pair<long long, int> rmqGet(RMQ* q, int l, int r) { n   if (q->r <= l || r <= q->l) return make_pair(-100, -1); n   if (l <= q->l && q->r <= r) return make_pair(q->res, q->resIndex); n   auto [res1, res1index] = rmqGet(q->left, l, r); n   auto [res2, res2index] = rmqGet(q->right, l, r); n   if (res1 >= res2) n     return make_pair(res1, res1index); n   else n     return make_pair(res2, res2index); n } n int ls[maxn]; n int rs[maxn]; n RMQ* rmq; n void findLR(int l, int r) { n   if (debug) cout <<  findLR:   << l <<     << r << endl; n   if (r - l < 2) return; n   auto [res, resIndex] = rmqGet(rmq, l + 1, r); n   if (debug) n     cout <<  rmqGet:   << (l + 1) <<     << r <<     << res <<     << resIndex n          << endl; n   ls[resIndex] = l; n   rs[resIndex] = r; n   findLR(l, resIndex); n   findLR(resIndex, r); n } n long long countLen(int len) { n   if (len < k) return 0; n   int rest = len - k; n   long long count = rest / (k - 1); n   long long res = n       (len + 1) * (count + 1) - (count + 1) * (k + k + count * (k - 1)) / 2; n   return res; n } n long long countIndex(int i) { n   int len = rs[i] - (ls[i] + 1); n   long long res = n       countLen(len) - countLen(i - (ls[i] + 1)) - countLen(rs[i] - (i + 1)); n   if (debug) cout <<  countIndex:   << i <<     << res << endl; n   return res; n } n int main() { n   ios::sync_with_stdio(false); n   cin >> n >> k; n   for (int i = 1; i <= n; i++) cin >> aa[i]; n   aa[0] = aa[n + 1] = 1200ll * 1000 * 1000; n   rmq = build(aa, 0, n + 2); n   findLR(0, n + 1); n   if (debug) n     for (int i = 1; i <= n; i++) cout << ls[i] <<     << rs[i] << endl; n   long long res = 0; n   for (int i = 1; i <= n; i++) { n     long long c = countIndex(i) % magic; n     res = (res + aa[i] * c) % magic; n   } n   cout << res << endl; n   if (debug) n     for (int i = 0; i <= 20; i++) n       cout <<  len:   << i <<  :   << countLen(i) << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n string ans =  1001010111001010 ; n int n; n int main() { n   cin >> n; n   cout << ans[n - 1]; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 2e5 + 5; n int n, k, size[maxn]; n bool f[maxn]; n long long dp[maxn]; n vector<int> g[maxn]; n void dfs(int u, int fa) { n   if (f[u]) n     size[u] = 1; n   else n     size[u] = 0; n   for (int i = 0; i < g[u].size(); i++) { n     int v = g[u][i]; n     if (v == fa) continue; n     dfs(v, u); n     size[u] += size[v]; n     dp[u] += dp[v] + size[v]; n   } n } n long long ans = 0; n int solve(int u, int fa) { n   int sum = (f[u] ? 1 : 0); n   for (int i = 0; i < g[u].size(); i++) { n     int v = g[u][i]; n     if (v == fa) continue; n     sum += solve(v, u); n   } n   ans += min(sum, 2 * k - size[u]); n   return min(sum, 2 * k - size[u]); n } n int main() { n   scanf( %d%d , &n, &k); n   int x; n   for (int i = 1; i <= 2 * k; i++) scanf( %d , &x), f[x] = 1; n   int u, v; n   for (int i = 1; i <= n - 1; i++) n     scanf( %d%d , &u, &v), g[u].push_back(v), g[v].push_back(u); n   dfs(1, -1); n   solve(1, -1); n   printf( %lld n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; 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   vector<int> c(n); n   bool d[2]; n   for (int i = 0; i < n; i++) { n     cin >> c[i]; n     d[c[i] % 2] = 1; n   } n   if (d[0] && d[1]) { n     sort(c.begin(), c.end()); n   } n   for (int i : c) { n     cout << i <<    ; n   } n   return 0; n } n


#include <bits/stdc++.h> n #pragma GCC optimize( O3 ) n using namespace std; n const int MAX = 5e5 + 5; n const long long MAX2 = 11; n const long long MOD = 1000000007; n const long long MOD2 = 1000000006; n const long long INF = 2e18; n const int dr[] = {1, 0, -1, 0, 1, 1, -1, -1, 0}; n const int dc[] = {0, 1, 0, -1, 1, -1, 1, -1, 0}; n const double pi = acos(-1); n const double EPS = 1e-9; n const int block = 2000; n int n, id, a, b; n vector<int> v[MAX], ans[MAX]; n void dfs(int nw, int par) { n   for (auto i : v[nw]) { n     if (i == par) continue; n     ans[i].push_back(++id); n   } n   ans[nw].push_back(++id); n   reverse(v[nw].begin(), v[nw].end()); n   for (auto i : v[nw]) { n     if (i == par) continue; n     dfs(i, nw); n   } n   return; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   cin >> n; n   for (int i = 2; i <= n; ++i) n     cin >> a >> b, v[a].push_back(b), v[b].push_back(a); n   ans[1].push_back(++id); n   dfs(1, 0); n   for (int i = 1; i <= n; ++i) cout << ans[i][0] <<     << ans[i][1] <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int mod = 1000000007; n const vector<int> mods = {998244353, 1000000007, 1000000009}; n const long long inf = 3e18; n const double pi = acos(-1.0); n const double eps = 0.0000001; n mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); n template <class... K> n using umap = unordered_map<K...>; n template <class... K> n using uset = unordered_set<K...>; n double dp[200]; n long long lad[200]; n int main() { n   cin.sync_with_stdio(0); n   cin.tie(0); n   cout.precision(13); n   fill(&dp[0], &dp[0] + sizeof(dp) / sizeof(dp[0]), 0); n   for (long long i = (0); i < (10); ++i) n     for (long long j = (0); j < (10); ++j) { n       long long fw = i & 1; n       long long up; n       cin >> up; n       if (!up) continue; n       long long node = 10 * (9 - i) + (fw ? j : 9 - j); n       long long nxfw = (i + up) & 1; n       long long nx = 10 * (9 - i + up) + (nxfw ? j : 9 - j); n       lad[node] = nx; n     } n   dp[99] = 0.0; n   for (long long i = 98; i >= 0; i--) { n     double mv = 0; n     double ex = 0; n     for (long long j = (1); j < (7); ++j) { n       double nx = dp[i + j]; n       if (lad[i + j]) { n         nx = min(nx, dp[lad[i + j]]); n       } n       mv += 1.0 / 6 * nx; n       if (i + j > 99) ex++; n     } n     mv++; n     mv /= (1 - ex / 6.0); n     dp[i] = mv; n   } n   cout << dp[0] <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   cin >> n; n   int ans = 0; n   for (int i = 0; i < n; i++) { n     int val; n     cin >> val; n     if (val >= 0) n       ans += val; n     else n       ans -= val; n   } n   cout << ans << endl; n } n


#include <iostream> n #include <unordered_map> n #include <list> n #include <queue> n #include <string.h> n #include <stack> n #include <set> n #include <unordered_set> n #include <map> n #include <climits> n #include <algorithm> n #include <math.h> n #include <vector> n #include <iomanip> n #include <ctype.h> n using namespace std; n #define ll long long int n const int MOD = 1e9 + 7; n ll facto[200010]; n void fact() n { n     facto[0] = 1; n     for (ll i = 1; i < 200010; i++) n     { n         facto[i] = (i * facto[i - 1]) % MOD; n     } n } n bool is(ll cnt[]) n { n     for (int i = 0; i < 26; i++) n     { n         if (cnt[i] >= 2) n             return false; n     } n     return true; n } n int main() n { n     // fact(); n     ios_base::sync_with_stdio(false); n     cin.tie(NULL); n     ll n; n     cin >> n; n     ll a[n]; n     ll sum = 0; n     for (ll i = 0; i < n; i++) n     { n         cin >> a[i]; n         sum += a[i]; n     } n     if ((sum % 2) != 0) n     { n         cout <<  0  n ; n         return 0; n     } n     bool dp[n + 1][sum / 2 + 1]; n     for (ll i = 0; i <= n; i++) n     { n         for (ll j = 0; j <= sum / 2; j++) n         { n             dp[i][j] = false; n         } n     } n     for (ll i = 0; i <= n; i++) n     { n         dp[i][0] = true; n     } n     for (ll j = 0; j <= sum / 2; j++) n     { n         dp[0][j] = false; n     } n     for (ll i = 1; i <= n; i++) n     { n         for (ll j = 1; j <= sum / 2; j++) n         { n             dp[i][j] = dp[i - 1][j]; n             if (j - a[i - 1] >= 0) n             { n                 dp[i][j] = dp[i][j] || dp[i - 1][j - a[i - 1]]; n             } n         } n     } n     if (dp[n][sum / 2] == false) n     { n         cout <<  0  n ; n         return 0; n     } n     ll mini = LLONG_MAX; n     ll pos = -1; n     for (ll i = 0; i < n; i++) n     { n         if (a[i] % 2) n         { n             cout <<  1  n ; n             cout << i + 1 <<   n ; n             return 0; n         } n     } n     for (ll i = 0; i < n; i++) n     { n         ll steps = 0; n         while (a[i] % 2 == 0) n         { n             a[i] /= 2; n             steps++; n         } n         if (mini > steps) n         { n             mini = steps; n             pos = i; n         } n     } n     cout <<  1  n ; n     cout << pos + 1 <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 2000; n int n, t; n double p, dp[MAXN + 1][MAXN + 1], ans = 0; n int main() { n   cin >> n >> p >> t; n   dp[0][0] = 1; n   for (int i = 0; i < t; i++) n     for (int j = 0; j <= n; j++) { n       if (j < n) { n         dp[i + 1][j + 1] += dp[i][j] * p; n         dp[i + 1][j] += dp[i][j] * (1 - p); n       } else n         dp[i + 1][j] += dp[i][j]; n     } n   for (int i = 1; i <= n; i++) ans += dp[t][i] * i; n   cout << fixed; n   cout << setprecision(9) << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long q_pow(long long n, long long temp) { n   long long ans = 1; n   long long x = temp; n   while (n) { n     if (n & 1) { n       ans *= x; n       ans %= 1000000007; n     } n     x *= x; n     x %= 1000000007; n     n /= 2; n   } n   return ans; n } n int main() { n   string str; n   long long k; n   while (cin >> str >> k) { n     int len = str.size(); n     long long sum = 0; n     for (int i = 0; i < len; i++) { n       if (str[i] ==  0  || str[i] ==  5 ) { n         long long a1 = q_pow(i, 2); n         sum += a1; n         sum %= 1000000007; n       } n     } n     long long a = (q_pow(len * k, 2) - 1 + 1000000007) % 1000000007; n     long long b = (q_pow(len, 2) - 1 + 1000000007) % 1000000007; n     long long c = (a * q_pow(1000000007 - 2, b)) % 1000000007; n     printf( %I64d n , (sum * c) % 1000000007); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct info { n   int id, d; n   bool operator<(const info &rhs) const { return d < rhs.d; } n } a[200007]; n vector<pair<int, int> > res; n priority_queue<info> q; n stack<info> st; n int main() { n   int n, m; n   int k = 0; n   int M = 0; n   scanf( %d%d , &n, &m); n   for (int i = 1, v; i <= n; i++) { n     scanf( %d , &v); n     if (v == 0) continue; n     M = max(M, v); n     a[i] = {i, v}; n     q.push(a[i]); n     k++; n   } n   if (M >= k) { n     puts( No ); n     return 0; n   } n   while (!q.empty()) { n     info x = q.top(); n     q.pop(); n     if (x.d != a[x.id].d) continue; n     while (x.d--) { n       info g; n       if (q.empty()) { n         puts( No ); n         return 0; n       } n       while (!q.empty()) { n         g = q.top(); n         q.pop(); n         if (g.d == a[g.id].d) break; n       } n       g.d--; n       a[g.id].d--; n       res.push_back({g.id, x.id}); n       if (g.d != 0) st.push(g); n     } n     a[x.id].d = 0; n     while (!st.empty()) { n       q.push(st.top()); n       st.pop(); n     } n   } n   puts( Yes ); n   if (res.size() != 0) printf( %d n , (int)res.size()); n   for (auto e : res) printf( %d %d n , e.first, e.second); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n double dist(double x1, double y1, double x2, double y2) { n   return sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2)); n } n int main() { n   double r, x1, y1, x2, y2; n   cin >> r >> x1 >> y1 >> x2 >> y2; n   if (dist(x1, y1, x2, y2) >= r) { n     printf( %.12f %.12f %.12f n , x1, y1, r); n   } else if (x1 == x2 && y1 == y2) { n     double r1 = r / 2.0; n     double x3 = x1 - r1; n     double y3 = y1; n     printf( %.12f %.12f %.12f n , x3, y3, r1); n   } else { n     double r1 = (dist(x1, y1, x2, y2) + r) / 2.0; n     double d1 = dist(x1, y1, x2, y2); n     double d2 = r1 - d1; n     double alpha1 = d1 / (d1 + d2); n     double alpha2 = d2 / (d1 + d2); n     double x3 = (x1 - alpha2 * x2) / alpha1; n     double y3 = (y1 - alpha2 * y2) / alpha1; n     printf( %.12f %.12f %.12f n , x3, y3, r1); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long n; n   cin >> n; n   int m = 0; n   while (n > 9) { n     n -= 9; n     m += 9 + n % 10; n     n /= 10; n   } n   cout << m + n << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   ; n   int t, n, i; n   cin >> n; n   int a[n]; n   for (i = 0; i < n; i++) cin >> a[i]; n   long long int ans = 0; n   long long int pre[n + 1]; n   memset(pre, 0, sizeof(pre)); n   for (i = 1; i < n + 1; i++) pre[i] = pre[i - 1] + a[i - 1]; n   set<long long int> s; n   int start = -1; n   for (i = 0; i < n; i++) { n     if (start < i) { n       start = i; n       s.insert(pre[i]); n     } n     while (start < n && !s.count(pre[start + 1])) { n       start++; n       s.insert(pre[start]); n     } n     s.erase(pre[i]); n     ans += start - i; n   } n   cout << ans << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m; n int dx[] = {0, -1, 0, 1}; n int dy[] = {-1, 0, 1, 0}; n char a[505][505]; n int main() { n   cin >> n >> m; n   for (int i = 1; i <= n; i++) cin >> a[i] + 1; n   bool flag = 1; n   for (int i = 1; i <= n; i++) n     for (int j = 1; j <= m; j++) { n       for (int k = 0; k <= 3; k++) { n         if (a[i + dx[k]][j + dy[k]] ==  S  && a[i][j] ==  W ) flag = 0; n         if (a[i + dx[k]][j + dy[k]] ==  W  && a[i][j] ==  S ) flag = 0; n       } n       if (a[i][j] !=  W  && a[i][j] !=  S ) a[i][j] =  D ; n     } n   if (flag == 0) { n     cout <<  No  << endl; n   } else { n     cout <<  Yes  << endl; n     for (int i = 1; i <= n; i++) { n       for (int j = 1; j <= m; j++) { n         cout << a[i][j]; n       } n       cout << endl; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1000010; n int fa[N], mx[N], n; n vector<int> son[N]; n vector<int> ans; n bool cmp(const int x, const int y) { return mx[x] < mx[y]; } n void dfs(int x) { n   int s = son[x].size(); n   for (int i = 0; i < s; ++i) { n     int& sn = son[x][i]; n     dfs(sn); n     mx[x] = max(mx[x], mx[sn] + 1); n   } n   sort(son[x].begin(), son[x].end(), cmp); n } n void DFS(int x) { n   printf( %d  , x); n   for (int i = 0; i < son[x].size(); ++i) { n     int& sn = son[x][i]; n     DFS(sn); n     if (i == 0) continue; n     for (int j = 0; j <= mx[son[x][i - 1]]; ++j) ans.push_back(sn); n   } n } n int main() { n   scanf( %d , &n); n   for (int i = 1; i < n; ++i) { n     scanf( %d , &fa[i]); n     son[fa[i]].push_back(i); n   } n   dfs(0); n   DFS(0); n   int s = ans.size(); n   printf(  n%d n , s); n   for (int i = 0; i < s; ++i) printf( %d  , ans[i]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n using ll = long long; n using db = long double; n using str = string; n using pi = pair<int, int>; n template <class T> n using V = vector<T>; n template <class T, size_t SZ> n using AR = array<T, SZ>; n using vi = V<int>; n using vb = V<bool>; n using vpi = V<pi>; n const int MOD = 1e9 + 7; n const db PI = acos((db)-1); n mt19937 rng(0); n template <class T> n bool ckmin(T& a, const T& b) { n   return b < a ? a = b, 1 : 0; n } n template <class T> n bool ckmax(T& a, const T& b) { n   return a < b ? a = b, 1 : 0; n } n template <class T, class U> n T fstTrue(T lo, T hi, U first) { n   ++hi; n   assert(lo <= hi); n   while (lo < hi) { n     T mid = lo + (hi - lo) / 2; n     first(mid) ? hi = mid : lo = mid + 1; n   } n   return lo; n } n using T = db; n const T EPS = 1e-9; n using P = pair<T, T>; n using vP = V<P>; n using Line = pair<P, P>; n int sgn(T a) { return (a > EPS) - (a < -EPS); } n T sq(T a) { return a * a; } n T norm(P p) { return sq(p.first) + sq(p.second); } n T abs(P p) { return sqrt(norm(p)); } n T arg(P p) { return atan2(p.second, p.first); } n P conj(P p) { return P(p.first, -p.second); } n P perp(P p) { return P(-p.second, p.first); } n P dir(T ang) { return P(cos(ang), sin(ang)); } n const int mx = 100005; n struct Eff { n   int n, k; n   P hab[mx]; n   bool works(db r) { n     V<pair<db, int>> ch; n     for (int i = 1; i <= n; i++) { n       if (abs(hab[i]) <= 2.0 * r - EPS) { n         db theta_diff = PI / 2.0 - asin(abs(hab[i]) / 2.0 / r); n         db theta = arg(hab[i]); n         assert(theta_diff <= PI / 2.0); n         ch.push_back(make_pair(theta - theta_diff, +1)); n         ch.push_back(make_pair(theta + theta_diff, -1)); n         ch.push_back(make_pair(theta - theta_diff + 2 * PI, +1)); n         ch.push_back(make_pair(theta + theta_diff + 2 * PI, -1)); n       } n     } n     sort(begin(ch), end(ch)); n     int running_sum = 0; n     for (auto u : ch) { n       running_sum += u.second; n       if (0 <= u.first && u.first <= 2 * PI) { n         if (running_sum >= k) return true; n       } n     } n     return false; n   } n   void solve() { n     cin >> n >> k; n     for (int i = 1; i <= n; i++) { n       cin >> hab[i].first >> hab[i].second; n     } n     db lo = EPS; n     db hi = 200005.0; n     for (int i = 0; i < 50; i++) { n       db mid = (lo + hi) / 2; n       if (works(mid)) { n         hi = mid; n       } else { n         lo = mid; n       } n     } n     cout << lo <<   n ; n   } n }; n int main() { n   cin.tie(0)->sync_with_stdio(0); n   cout << fixed << setprecision(10); n   Eff e; n   e.solve(); n } n


#include <bits/stdc++.h> n using namespace std; n template <class T> n ostream &operator<<(ostream &os, vector<T> V) { n   os <<  [  ; n   for (auto v : V) os << v <<    ; n   os <<  ] ; n   return os; n } n template <class T> n ostream &operator<<(ostream &os, set<T> S) { n   os <<  {  ; n   for (auto s : S) os << s <<    ; n   return os <<  } ; n } n template <class L, class R> n ostream &operator<<(ostream &os, pair<L, R> P) { n   return os <<  (  << P.first <<  ,  << P.second <<  ) ; n } n template <class L, class R> n ostream &operator<<(ostream &os, map<L, R> M) { n   os <<  {  ; n   for (auto m : M) os <<  (  << m.first <<  :  << m.second <<  )  ; n   return os <<  } ; n } n long long n, a[200005], c[200005], cc[200005]; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   long long t; n   cin >> t; n   while (t--) { n     cin >> n; n     for (long long i = 1; i < n + 1; i++) cin >> a[i]; n     for (long long i = 1; i < n + 1; i++) { n       long long k = 1; n       if (c[i - 1] == 1) k = 2; n       long long j = i; n       while (j <= n && a[j] == a[i]) c[j] = k, j++; n       i = j - 1; n     } n     if (c[1] == c[n] && a[1] != a[n]) { n       bool ok = false; n       for (long long i = 1; i < n; i++) { n         if (c[i] == c[i + 1]) { n           for (long long j = i + 1; j < n + 1; j++) { n             if (c[j] == 1) n               c[j] = 2; n             else n               c[j] = 1; n           } n           ok = true; n           break; n         } n       } n       if (!ok) c[n] = 3; n     } n     long long mx = 0; n     for (long long i = 1; i < n + 1; i++) mx = max(mx, c[i]); n     cout << mx <<   n ; n     for (long long i = 1; i < n + 1; i++) cout << c[i] <<    ; n     cout <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int h1, h2, n; n int v[111]; n char c[111]; n int a[111]; n int b[111]; n const double eps = 1e-9; n int main() { n   cin >> h1 >> h2 >> n; n   for (int i = (0); i < (n); i++) cin >> v[i] >> c[i] >> a[i] >> b[i]; n   int best = 0; n   for (int i = -200; i <= 200; i++) { n     bool good = 1; n     int sum = 0; n     set<int> used; n     int x1 = 0; n     int y1 = h1; n     int x2 = 100000; n     int y2 = h2 + i * 100; n     if (i % 2) y2 = 100 - h2 + i * 100; n     if (i > 0) { n       for (int j = (0); j < (i); j++) { n         int y = 100 + 100 * j; n         double x = double(x2 - x1) / (y2 - y1) * (y - y1); n         bool found = 0; n         for (int k = (0); k < (n); k++) n           if (a[k] <= x + eps && b[k] >= x - eps) { n             if (j % 2 == 0 && c[k] ==  T  || j % 2 == 1 && c[k] ==  F ) { n               if (used.count(k)) good = 0; n               used.insert(k); n               sum += v[k]; n               found = 1; n               break; n             } n           } n         if (!found) good = 0; n       } n     } else if (i < 0) { n       for (int j = (0); j < (-i); j++) { n         int y = -100 * j; n         double x = double(x2 - x1) / double(y2 - y1) * (y - y1); n         bool found = 0; n         for (int k = (0); k < (n); k++) n           if (a[k] <= x + eps && b[k] >= x - eps) { n             if (j % 2 == 0 && c[k] ==  F  || j % 2 == 1 && c[k] ==  T ) { n               if (used.count(k)) good = 0; n               used.insert(k); n               sum += v[k]; n               found = 1; n               break; n             } n           } n         if (!found) good = 0; n       } n     } n     if (good) best = max(best, sum); n   } n   cout << best << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long int t; n   cin >> t; n   while (t--) { n     long long int n, i, j, b, c, a; n     cin >> a >> b >> c; n     long long int y = a + b + c; n     y--; n     cout << y << endl; n   } n } n