#include<cstdio> n int t,n; n int vx[13][2003]; n int vy[13][2003]; n int fx[13][2003]; n int lx[13][2003]; n int fy[13][2003]; n int ly[13][2003]; n int a[2003][2003]; n int main() n { n  scanf(  %d ,&t); n  while(t--) n  { n   scanf(  %d ,&n); n   for(int i=1;i<=n;++i) n   { n    for(int j=1;j<=n;++j) n    { n     scanf(  %1d ,&a[i][j]); n    } n   } n   for(int d=0;d<10;++d) n   { n    int ans=0; n    for(int j=1;j<=n;++j) n    { n     fx[d][j]=lx[d][j]=0; n     for(int i=1;i<=n;++i) n     { n      if(a[i][j]!=d)continue; n      if(!fx[d][j]) n      { n       fx[d][j]=i; n       break; n      } n     } n     for(int i=n;i;--i) n     { n      if(a[i][j]!=d)continue; n      if(!lx[d][j]) n      { n       lx[d][j]=i; n       break; n      } n     } n     if(fx[d][j])vx[d][j]=((n-fx[d][j]>lx[d][j]-1)?(n-fx[d][j]):(lx[d][j]-1)); n     else vx[d][j]=0; n    } n    for(int i=1;i<=n;++i) n    { n     fy[d][i]=ly[d][i]=0; n     for(int j=1;j<=n;++j) n     { n      if(a[i][j]!=d)continue; n      if(!fy[d][i]) n      { n       fy[d][i]=j; n       break; n      } n     } n     for(int j=n;j;--j) n     { n      if(a[i][j]!=d)continue; n      if(!ly[d][i]) n      { n       ly[d][i]=j; n       break; n      } n     } n     if(fy[d][i])vy[d][i]=((n-fy[d][i]>ly[d][i]-1)?(n-fy[d][i]):(ly[d][i]-1)); n     else vy[d][i]=0; n    } n    for(int i=1;i<=n;++i) n    { n     if(!vx[d][i])continue; n     for(int j=1;j<i;++j) n     { n      if(!vx[d][j])continue; n      if(vx[d][j]*(i-j)>ans)ans=vx[d][j]*(i-j); n      if(vx[d][i]*(i-j)>ans)ans=vx[d][i]*(i-j); n     } n    } n    for(int i=1;i<=n;++i) n    { n     if(!vy[d][i])continue; n     for(int j=1;j<i;++j) n     { n      if(!vy[d][j])continue; n      if(vy[d][j]*(i-j)>ans)ans=vy[d][j]*(i-j); n      if(vy[d][i]*(i-j)>ans)ans=vy[d][i]*(i-j); n     } n    } n    printf( %d  ,ans); n   } n   puts(  ); n  } n  return 0; n }


#include <bits/stdc++.h> n using namespace std; n const long long PR = 1000000009; n int n, m, premin[100009], sufmax[100009]; n char A[100009], B[100009]; n int main() { n   ios::sync_with_stdio(false); n   cin >> A >> B; n   n = strlen(A); n   m = strlen(B); n   for (int i = 0; i < m; i++) premin[i] = n, sufmax[i] = -1; n   int st = 0; n   for (int i = 0; i < m && st < n; i++) { n     while (A[st] != B[i]) { n       st++; n       if (st >= n) break; n     } n     premin[i] = st; n     st++; n   } n   st = n - 1; n   for (int i = m - 1; i >= 0 && st >= 0; i--) { n     while (A[st] != B[i]) { n       st--; n       if (st < 0) break; n     } n     sufmax[i] = st; n     st--; n   } n   int ans = m, i1 = -1, j1 = -1; n   for (int i = 0; i < m; i++) { n     if (premin[i] != n) { n       int mid, st = i + 1, end = m - 1; n       while (st < end) { n         mid = (st + end) / 2; n         if (sufmax[mid] >= premin[i] + 1) n           end = mid; n         else n           st = mid + 1; n       } n       if (sufmax[st] < premin[i] + 1) st = m; n       int now = st - i - 1; n       if (now < ans) { n         i1 = i + 1; n         j1 = st - 1; n         ans = now; n       } n     } n   } n   for (int i = m - 1; i >= 0; i--) { n     if (sufmax[i] == -1) break; n     int now = i - 1; n     if (now < ans) { n       j1 = i - 1; n       i1 = 0; n       ans = now; n     } n   } n   if (i1 == -1 || j1 - i1 + 1 == m) { n     cout <<  -  << endl; n     return 0; n   } n   for (int i = 0; i < i1; i++) cout << B[i]; n   for (int j = j1 + 1; j < m; j++) cout << B[j]; n   cout << endl; n   return 0; n }; n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long n, m; n   cin >> n >> m; n   long long mod = 1; n   int k = 0; n   while (k < n) { n     if (mod > m) { n       break; n     } n     mod *= 2; n     k++; n   } n   cout << m % mod << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int n, k; n string a; n int change(char c) { n   int j = 0, i = 0, ans = 0, cont = 0; n   for (i = 0; i < n; i++) { n     if (a[i] == c) cont++; n     if (cont > k) { n       while (cont > k) n         if (a[j++] == c) cont--; n     } n     ans = max(ans, i - j + 1); n   } n   return ans; n } n int main() { n   ios::sync_with_stdio(0); n   while (cin >> n >> k) { n     a =   ; n     int suma = 0, sumb = 0; n     for (int i = 0; i < n; ++i) { n       char c; n       cin >> c; n       if (c ==  a ) n         suma++; n       else n         sumb++; n       a += c; n     } n     int ans; n     if (suma > sumb) n       ans = change( b ); n     else if (sumb > suma) n       ans = change( a ); n     else n       ans = max(change( a ), change( b )); n     cout << ans << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<pair<int, int> > g[100100]; n int fa[100100][18], typ[100100], n; n int sum[100100][2], dep[100100]; n void dfs(int u) { n   for (int i = 1; i < 18; ++i) fa[u][i] = fa[fa[u][i - 1]][i - 1]; n   for (auto v : g[u]) { n     int A = v.first, tp = v.second; n     sum[A][0] = sum[u][0]; n     sum[A][1] = sum[u][1]; n     ++sum[A][tp]; n     dep[A] = dep[u] + 1; n     dfs(A); n   } n } n int LCA(int u, int v) { n   if (dep[u] > dep[v]) swap(u, v); n   int hu = dep[u], hv = dep[v]; n   int tu = u, tv = v; n   for (int det = hv - hu, i = 0; det; det >>= 1, i++) n     if (det & 1) tv = fa[tv][i]; n   if (tu == tv) return tu; n   for (int i = 17; i >= 0; --i) { n     if (fa[tu][i] == fa[tv][i]) continue; n     tu = fa[tu][i]; n     tv = fa[tv][i]; n   } n   return fa[tu][0]; n } n int tmp[2]; n int main() { n   scanf( %d , &n); n   for (int i = 1; i <= n; ++i) { n     scanf( %d%d , &fa[i][0], &typ[i]); n     if (fa[i][0] == -1) fa[i][0] = typ[i] = 0; n   } n   for (int i = 1; i <= n; ++i) g[fa[i][0]].push_back(pair<int, int>(i, typ[i])); n   dfs(0); n   int q; n   scanf( %d , &q); n   while (q--) { n     int tp, u, v; n     scanf( %d%d%d , &tp, &u, &v); n     int lca = LCA(u, v); n     if (!lca || lca == v) { n       puts( NO ); n       continue; n     } n     if (tp == 1) { n       if (lca != u) { n         puts( NO ); n         continue; n       } n       tmp[0] = sum[v][0] - sum[u][0]; n       tmp[1] = sum[v][1] - sum[u][1]; n       if (tmp[1]) { n         puts( NO ); n         continue; n       } n       puts( YES ); n     } else { n       tmp[0] = sum[u][0] - sum[lca][0]; n       tmp[1] = sum[u][1] - sum[lca][1]; n       if (tmp[1]) { n         puts( NO ); n         continue; n       } n       tmp[0] = sum[v][0] - sum[lca][0]; n       tmp[1] = sum[v][1] - sum[lca][1]; n       if (tmp[0]) { n         puts( NO ); n         continue; n       } n       puts( YES ); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int a[32]; n int n, k; n double dp[32][32][205]; n int main() { n   memset(dp, 0, sizeof(dp)); n   cin >> n >> k; n   for (int i = 0; i < n; ++i) cin >> a[i]; n   for (int i = 0; i < n; ++i) { n     for (int j = i; j < n; ++j) { n       if (a[i] > a[j]) { n         dp[i][j][0] = 1; n       } n     } n   } n   double prob = (double)2 / (n * (n + 1)); n   for (int op = 1; op <= k; ++op) { n     for (int start = 0; start < n; ++start) { n       for (int end = start; end < n; ++end) { n         for (int x = 0; x < n; ++x) { n           for (int y = x; y < n; ++y) { n             int nx = x; n             if (start <= x && x <= end) nx = end - x + start; n             int ny = y; n             if (start <= y && y <= end) ny = start + end - y; n             if (ny >= nx) n               dp[x][y][op] += dp[nx][ny][op - 1] * prob; n             else n               dp[x][y][op] += (1 - dp[ny][nx][op - 1]) * prob; n           } n         } n       } n     } n   } n   double ans = 0; n   for (int i = 0; i < n; ++i) n     for (int j = i + 1; j < n; ++j) ans += dp[i][j][k]; n   cout << fixed << setprecision(9) << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n const int mod = 1e9 + 7; n const int N = 3e5 + 1; n using namespace std; n void err(istream_iterator<string> it) {} n template <typename T, typename... Args> n void err(istream_iterator<string> it, T a, Args... args) { n   cerr << *it <<   =   << a <<    ; n   err(++it, args...); n } n long long arr[N], brr[N]; n long long n, m, k, q, a, b; n long long binpow(long long a, long long n) { n   long long res = 1; n   while (n) { n     if (n & 1) res = res * a; n     a = a * a; n     n /= 2; n   } n   return res; n } n void solve() { n   cin >> n; n   vector<long long> arr(n); n   vector<long long> v; n   for (int i = 0; i < n; i++) cin >> arr[i]; n   long long x = arr[0]; n   arr[0] = 0; n   for (int k = 0; k + 1 < n; k++) { n     x = min(max(0LL, arr[k + 1] - arr[k]), x); n     arr[k + 1] -= x; n   } n   bool ok = true; n   for (int k = 0; k + 1 < n; k++) { n     if (arr[k + 1] - arr[k] < 0) ok = false; n   } n   n = v.size(); n   for (int i = 0; i < n; i++) n     if (v[i] < 0) ok = false; n   if (ok) n     cout <<  yes ; n   else n     cout <<  no ; n   cout <<   n ; n   cerr <<   n ; n } n int main() { n   int t; n   t = 1; n   cin >> t; n   int a = 0; n   while (t--) { n     solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long n, q; n struct interv { n   long long l, r, ubonus, lbonus; n   interv(long long L, long long R, long long UBONUS, long long LBONUS) { n     l = L; n     r = R; n     ubonus = UBONUS; n     lbonus = LBONUS; n   } n   bool operator<(const interv& x) const { return r < x.r; } n }; n set<interv> s; n set<interv>::iterator it; n long long x, y, to; n char dir[3]; n long long ans; n int main() { n   scanf( %lld %lld , &n, &q); n   s.insert(interv(1, n, 0, 0)); n   for (int i = 1; i <= q; i++) { n     scanf( %lld %lld , &x, &y); n     scanf( %s , dir); n     it = s.lower_bound(interv(0, x, 0, 0)); n     if (it == s.end()) { n       printf( 0 n ); n       continue; n     } n     long long b = (*it).r; n     long long a = (*it).l; n     long long ubonus = (*it).ubonus; n     long long lbonus = (*it).lbonus; n     if (x < a) { n       printf( 0 n ); n       continue; n     } n     if (dir[0] ==  U ) { n       ans = (b - a + 1) - (x - a) + ubonus; n       printf( %lld n , ans); n       s.erase(it); n       if (x - 1 >= 1 && a <= x - 1) s.insert(interv(a, x - 1, ans, lbonus)); n       if (x + 1 <= n && x + 1 <= b) s.insert(interv(x + 1, b, ubonus, 0)); n     } else { n       ans = (b - a + 1) - (b - x) + lbonus; n       printf( %lld n , ans); n       s.erase(it); n       if (x - 1 >= 1 && a <= x - 1) s.insert(interv(a, x - 1, 0, lbonus)); n       if (x + 1 <= n && x + 1 <= b) s.insert(interv(x + 1, b, ubonus, ans)); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n inline void solve() { n   long long n; n   cin >> n; n   vector<bool> isp(n + 1, 1); n   vector<long long> maxx(n + 1, -1); n   isp[0] = isp[1] = 0; n   for (long long i = 2; i <= n; i++) { n     if (isp[i]) { n       if (i * 2 <= n) { n         for (long long j = i * 2; j <= n; j += i) { n           isp[j] = 0; n           maxx[j] = max(maxx[j], i); n         } n       } n     } n   } n   long long best = 1, now = n; n   for (long long j = 2; j * j <= n; j++) { n     while (now % j == 0) { n       now /= j; n       best = max(best, j); n     } n   } n   best = max(best, now); n   long long ans = n - best + 1; n   for (long long i = n - best + 1; i < n; i++) { n     ans = min(ans, i - maxx[i] + 1); n   } n   cout << ans; n } n signed main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long n, m, k, y, a, b, c; n long long A[2005]; n bool B[2005]; n int main() { n   cin >> n >> k; n   priority_queue<pair<int, int> > pq; n   for (int i = int(0); i < int(n); i++) { n     cin >> A[i]; n     pq.push({A[i], i}); n   } n   int ans = 0; n   for (int i = int(0); i < int(k); i++) { n     B[pq.top().second] = true; n     ans += pq.top().first; n     pq.pop(); n   } n   cout << ans <<   n ; n   m = -1; n   int t = 0; n   for (int i = int(0); i < int(n); i++) { n     if (B[i] && t + 1 < k) { n       cout << i - m <<    ; n       m = i; n       t++; n     } n   } n   cout << n - 1 - m <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e5 + 5; n int n; n int seq[N]; n vector<int> zli[N]; n int res[N]; n int main() { n   scanf( %d , &n); n   for (int i = 1; i <= n; ++i) { n     scanf( %d , seq + i); n     seq[i] = n - seq[i]; n     zli[seq[i]].push_back(i); n   } n   int rodzaj = 0; n   for (int ile = 1; ile <= n; ++ile) { n     if ((int)(zli[ile]).size() % ile) { n       puts( Impossible ); n       exit(0); n     } n     for (int i = 0; i < (int)(zli[ile]).size(); i += ile) { n       rodzaj++; n       for (int j = i; j < i + ile; ++j) { n         res[zli[ile][j]] = rodzaj; n       } n     } n   } n   puts( Possible ); n   for (int i = 1; i <= n; ++i) { n     printf( %d  , res[i]); n   } n   puts(  ); n } n


#include <bits/stdc++.h> n int find_min(int pos, int target) { n   int right = -1; n   for (int i = 0; right == -1; i++) n     if ((pos + i) % 26 == target) right = i; n   int left = -1; n   for (int i = 0; left == -1; i++) n     if ((pos - i + 26 * 2) % 26 == target) left = i; n   return std::min(right, left); n } n int main() { n   int n; n   std::cin >> n; n   std::string s; n   std::cin >> s; n   std::string genome =  ACTG ; n   int min_moves = INT_MAX; n   for (int i = 0; i < n - 3; i++) { n     int min_temp = 0; n     for (int j = 0; j < 4; j++) n       min_temp += find_min(s[i + j] -  A , genome[j] -  A ); n     min_moves = std::min(min_moves, min_temp); n   } n   std::cout << min_moves <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const double PI = acos(-1); n const long long MOD = 998244353; n struct node { n   int v; n   node *d, *r; n }; n node a[1050][1050]; n long long n, m, q; n void solve() { n   cin >> n >> m >> q; n   for (int i = (int)(1); i <= (int)(n); i += 1) { n     for (int j = (int)(1); j <= (int)(m); j += 1) { n       cin >> a[i][j].v; n     } n   } n   for (int i = (int)(0); i <= (int)(n); i += 1) { n     for (int j = (int)(0); j <= (int)(m); j += 1) { n       a[i][j].r = &a[i][j + 1]; n       a[i][j].d = &a[i + 1][j]; n     } n   } n   while (q--) { n     long long e, b, c, d, h, w; n     cin >> e >> b >> c >> d >> h >> w; n     node *x, *y, *j, *k; n     x = y = &a[0][0]; n     for (int i = (int)(1); i <= (int)(e - 1); i += 1) x = x->d; n     for (int i = (int)(1); i <= (int)(b - 1); i += 1) x = x->r; n     for (int i = (int)(1); i <= (int)(c - 1); i += 1) y = y->d; n     for (int i = (int)(1); i <= (int)(d - 1); i += 1) y = y->r; n     j = x, k = y; n     for (int i = (int)(1); i <= (int)(h); i += 1) { n       x = x->d, y = y->d; n       swap(x->r, y->r); n     } n     for (int i = (int)(1); i <= (int)(w); i += 1) { n       x = x->r, y = y->r; n       swap(x->d, y->d); n     } n     for (int i = (int)(1); i <= (int)(w); i += 1) { n       j = j->r, k = k->r; n       swap(j->d, k->d); n     } n     for (int i = (int)(1); i <= (int)(h); i += 1) { n       j = j->d, k = k->d; n       swap(j->r, k->r); n     } n   } n   node *x = &a[0][0]; n   for (int i = (int)(1); i <= (int)(n); i += 1) { n     x = x->d; n     node *y = x; n     for (int j = (int)(1); j <= (int)(m); j += 1) { n       y = y->r; n       cout << y->v <<    ; n     } n     cout << endl; n   } n } n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   int t = 1; n   while (t--) { n     solve(); n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 50, maxm = 5e3 + 100; n int n, lim, f[maxn + 3], s[maxn + 3]; n double p[maxn + 3], q[maxn + 3], dp[maxn + 3][maxm + 3]; n bool check(double x) { n   for (int i = n; i; i--) { n     for (int j = lim + 1; j <= lim + 100; j++) { n       dp[i + 1][j] = x; n     } n     for (int j = 0; j <= lim; j++) { n       dp[i][j] = min(x, (dp[i + 1][j + f[i]] + f[i]) * p[i] + n                             (dp[i + 1][j + s[i]] + s[i]) * q[i]); n     } n   } n   return dp[1][0] == x; n } n int main() { n   scanf( %d %d , &n, &lim); n   for (int i = 1, x; i <= n; i++) { n     scanf( %d %d %d , &f[i], &s[i], &x); n     p[i] = x / 100., q[i] = (100 - x) / 100.; n   } n   double l = 0, r = 1e9, mid; n   for (int k = 0; k < 70; k++) { n     mid = (l + r) / 2; n     if (check(mid)) { n       l = mid; n     } else { n       r = mid; n     } n   } n   printf( %.12lf n , (l + r) / 2); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, ans = 0; n   bool flag = false; n   int a[105]; n   cin >> n; n   for (int i = 0; i < n; i++) cin >> a[i]; n   for (int i = 1; i < n; i++) { n     if (a[i] == 2) { n       if (a[i - 1] == 1) { n         if (i >= 2 && a[i - 2] == 3) n           ans += 2; n         else n           ans += 3; n       } else { n         flag = true; n         break; n       } n     } else if (a[i] == 1) { n       if (a[i - 1] == 2) n         ans += 3; n       else if (a[i - 1] == 3) n         ans += 4; n       else { n         flag = true; n         break; n       } n     } else { n       if (a[i - 1] == 1) n         ans += 4; n       else { n         flag = true; n         break; n       } n     } n   } n   if (flag) n     puts( Infinite ); n   else { n     puts( Finite ); n     cout << ans << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int matmul[][6] = {{2, 1, 1, 1, 0, 0}, {1, 2, 1, 1, 0, 0}, n                          {1, 0, 1, 0, 0, 0}, {0, 1, 0, 1, 0, 0}, n                          {1, 1, 1, 1, 1, 0}, {2, 2, 2, 2, 1, 1}}; n long long N, sx, sy, dx, dy, t; n struct matrix { n   int m, n; n   long long d[13][13]; n   matrix() { n     m = 0; n     n = 0; n     memset(d, 0, sizeof d); n   } n   matrix(int m, int n) { n     this->m = m; n     this->n = n; n     memset(d, 0, sizeof d); n   } n   matrix operator*(const matrix &a) const { n     int x = m; n     int y = n; n     int z = a.n; n     matrix res = matrix(x, z); n     for (int i = 0; i < (x); i = i + 1) n       for (int j = 0; j < (z); j = j + 1) n         for (int k = 0; k < (y); k = k + 1) n           res.d[i][j] = (res.d[i][j] + d[i][k] * a.d[k][j]) % N; n     return (res); n   } n   matrix operator^(long long k) const { n     matrix res = matrix(m, n); n     matrix mul = (*this); n     for (int i = 0; i < (m); i = i + 1) res.d[i][i] = 1; n     while (k > 0) { n       if (k % 2 == 1) res = res * mul; n       mul = mul * mul; n       k = k >> 1; n     } n     return (res); n   } n   void print(void) const { n     for (int i = 0; i < (m); i = i + 1) { n       for (int j = 0; j < (n); j = j + 1) cout << d[i][j] <<    ; n       cout <<   n ; n     } n     cout <<   n ; n   } n }; n matrix fst, mul; n void process(void) { n   cin >> N >> sx >> sy >> dx >> dy >> t; n   if (N == 1) { n     cout <<  1 1 ; n     return; n   } n   fst = matrix(1, 6); n   mul = matrix(6, 6); n   for (int i = 0; i < (6); i = i + 1) n     for (int j = 0; j < (6); j = j + 1) mul.d[i][j] = matmul[i][j] % N; n   fst.d[0][0] = sx - 1; n   fst.d[0][1] = sy - 1; n   fst.d[0][2] = (dx % N + N) % N; n   fst.d[0][3] = (dy % N + N) % N; n   fst.d[0][4] = 0; n   fst.d[0][5] = 1; n   fst = fst * (mul ^ t); n   cout << fst.d[0][0] + 1 <<     << fst.d[0][1] + 1; n } n int main(void) { n   process(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios::sync_with_stdio(0); n   int n, m; n   cin >> n >> m; n   int ver[n + 1]; n   ver[n] = 1e9; n   for (int i = 0; i < n; i++) cin >> ver[i]; n   int hor[m]; n   for (int i = 0; i < m; i++) { n     int x1, x2, y; n     cin >> x1 >> x2 >> y; n     if (x1 > 1) n       hor[i] = 0; n     else n       hor[i] = x2; n   } n   if (m == 0) { n     cout << 0 << endl; n     return 0; n   } n   sort(ver, ver + n); n   sort(hor, hor + m); n   int wy = 10000000; n   int poz = m; n   int a = 0; n   for (int i = 0; i < n + 1; i++) { n     while (a < m and hor[a] < ver[i]) { n       poz--; n       a++; n     } n     wy = min(wy, poz + i); n   } n   cout << wy << endl; n   return 0; n } n


#include <bits/stdc++.h> n const int md = 1e9 + 7; n const long long hs = 199; n using namespace std; n int n, x; n set<int> a; n int main() { n   ios::sync_with_stdio(0), cin.tie(0), cout.tie(0); n   cin >> n; n   for (int i = 1; i <= n; i++) a.insert(i); n   cout <<  1  ; n   while (--n) { n     cin >> x; n     a.erase(x); n     auto it = a.end(); n     it--; n     cout << 1 + *it - n <<   n ; n   } n   cout << 1 <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n int setBite(int r, int pos) { return r | (1 << pos); } n int resetBit(int r, int pos) { return r & ~(1 << pos); } n bool checkBit(int r, int pos) { return (r & (1 << pos)); } n bool leap(long long x) { n   if ((x % 4 == 0 && x % 100 != 0) || x % 400 == 0) return true; n   return false; n } n long long bigmod(long long a, long long b, long long mod) { n   if (b == 0) return 1; n   if (b % 2 == 0) { n     long long ret = bigmod(a, b / 2, mod); n     return ((ret % mod) * (ret % mod)) % mod; n   } else n     return ((a % mod) * (bigmod(a, b - 1, mod) % mod)) % mod; n } n template <class T> n inline T gcd(T a, T b) { n   if (a < 0) return gcd(-a, b); n   if (b < 0) return gcd(a, -b); n   return (b == 0) ? a : gcd(b, a % b); n } n template <class T> n inline T lcm(T a, T b) { n   if (a < 0) return lcm(-a, b); n   if (b < 0) return lcm(a, -b); n   return a * (b / gcd(a, b)); n } n template <class T> n inline T sqr(T x) { n   return x * x; n } n template <class T> n inline T power(T r, T p) { n   T ans = 1; n   for (T i = 0; i < p; i++) { n     ans *= r; n   } n   return ans; n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   int n; n   cin >> n; n   int g[2 * n + 5][2 * n + 5]; n   for (int i = 0; i < 2 * n + 5; i++) { n     for (int j = 0; j < 2 * n + 5; j++) { n       g[i][j] = 0; n     } n   } n   for (int i = 1; i <= n - 1; i++) { n     int a; n     cin >> a; n     g[i][i + 1] = a; n     g[i + 1][i] = a; n   } n   for (int i = n + 1; i <= 2 * n - 1; i++) { n     int a; n     cin >> a; n     g[i][i + 1] = a; n     g[i + 1][i] = a; n   } n   for (int i = 1; i <= n; i++) { n     int a; n     cin >> a; n     g[i][i + n] = a; n     g[i + n][i] = a; n   } n   vector<int> v; n   for (int i = 1; i <= n; i++) { n     int ans = 0; n     for (int j = 1; j <= i - 1; j++) { n       ans += g[j][j + 1]; n     } n     ans += g[i][i + n]; n     for (int j = i + n; j <= 2 * n - 1; j++) { n       ans += g[j][j + 1]; n     } n     v.push_back(ans); n   } n   sort(v.begin(), v.end()); n   cout << v[0] + v[1]; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long int m; n long long int a; n long long int b; n set<int> s; n long long int im(long long int val, int dv) { n   long long int z = 0; n   long long int U = (1LL + val / dv) * (val / dv) / 2LL; n   U *= dv; n   U -= (val / dv) * (dv - (val % dv) - 1); n   return U; n   for (int i = 0; i <= val; i++) { n     z += i / dv; n   } n   return z; n } n long long int cn(int star, int ds) { n   long long int ret = im(m - ds, b) - im(star - ds - 1, b); n   ret += m - star + 1; n   return ret; n } n int main() { n   cin >> m >> a >> b; n   long long int mx = 0; n   long long int cur = 0; n   swap(a, b); n   long long int ans = cn(0, 0); n   s.insert(0); n   while (true) { n     if (cur < a) { n       cur += b * ((a - cur + b - 1) / b); n     } n     mx = max(mx, cur); n     cur -= a; n     cur %= b; n     if (mx > m) break; n     if (s.count(cur)) break; n     s.insert(cur); n     ans += cn(mx, cur); n   } n   printf( %lld n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long n; n   int a = 0; n   cin >> n; n   long long tp = n; n   for (long long i = 2; i <= sqrt(tp); i++) { n     if (tp % i == 0) { n       while (tp % i == 0) tp /= i; n       a = i; n       break; n     } n   } n   if (a == 0) n     cout << n; n   else if (tp != 1) { n     printf( 1 ); n   } else n     printf( %d , a); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n map<string, int> Map; n char str[2200][22]; n int p[2200], q[2200]; n int main() { n   int num, n, k1, k2, cnt; n   char s1[22], s2[22]; n   while (scanf( %d , &n) != EOF) { n     memset(p, 0, sizeof(p)); n     memset(q, 0, sizeof(q)); n     cnt = 1; n     num = 0; n     while (n--) { n       scanf( %s %s , s1, s2); n       if (Map[s2] > 0) continue; n       if ((k1 = Map[s1]) > 0) { n         Map[s1] = 0; n         strcpy(str[cnt++], s2); n         p[q[k1]] = cnt - 1; n         q[cnt - 1] = q[k1]; n         Map[s2] = cnt - 1; n       } else { n         strcpy(str[cnt++], s1); n         strcpy(str[cnt++], s2); n         p[cnt - 2] = cnt - 1; n         q[cnt - 1] = cnt - 2; n         Map[s2] = cnt - 1; n         num++; n       } n     } n     printf( %d n , num); n     int i; n     for (i = 0; i < cnt; i++) n       if (p[i]) printf( %s %s n , str[i], str[p[i]]); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int a[100002]; n int gcd(int a, int b) { n   int c; n   while (a != 0) { n     c = a; n     a = b % a; n     b = c; n   } n   return b; n } n int main() { n   int n; n   int mina = 2000000009; n   cin >> n; n   cin >> a[1]; n   int ucln; n   ucln = a[1]; n   for (long long int i = 2; i <= n; i++) { n     scanf( %d , a + i); n     ucln = gcd(ucln, a[i]); n   } n   int res0 = 1; n   for (long long int i = 1; i <= n; i++) { n     if (a[i] % ucln != 0) { n       res0 = 0; n     } n   } n   if (ucln == 1) { n     res0 = 0; n   } n   if (res0 == 1) { n     cout <<  YES n ; n     cout << 0; n   } else { n     a[n + 1] = 2; n     int res = 0; n     for (int i = 1; i <= n;) { n       if (a[i] % 2 == 0) { n         i++; n       } else { n         if (a[i + 1] % 2 == 1) { n           res += 1; n           i += 2; n         } else { n           res += 2; n           i += 2; n         } n       } n     } n     cout <<  YES n ; n     cout << res; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int maxx = 1e6 + 10; n int up(int a, int b) { n   if (a % b) n     return a / b + 1; n   else n     return a / b; n } n int main() { n   int t; n   scanf( %d , &t); n   while (t--) { n     int n, d; n     scanf( %d%d , &n, &d); n     bool flag = 0; n     for (int x = 0; x < maxx; x++) { n       if ((x + up(d, x + 1)) <= n) { n         flag = 1; n         break; n       } n     } n     if (flag) n       printf( YES n ); n     else n       printf( NO n ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long int mod = 998244353; n long long int expo(long long int base, long long int exponent, n                    long long int mod) { n   long long int ans = 1; n   while (exponent != 0) { n     if (exponent & 1) ans = (1LL * ans * base) % mod; n     base = (1LL * base * base) % mod; n     exponent >>= 1; n   } n   return ans % mod; n } n vector<long long int> g[100005]; n long long int val[100005], dist[100005]; n void dfs(long long int node, long long int par, long long int d) { n   dist[node] = d; n   if (g[node].size() == 1) val[par]++; n   for (auto x : g[node]) { n     if (x != par) dfs(x, node, d + 1); n   } n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   int tests = 1; n   while (tests--) { n     long long int i, j, u, v, n; n     cin >> n; n     for (long long int i = 1; i <= n - 1; i++) { n       cin >> u >> v; n       g[u].push_back(v); n       g[v].push_back(u); n     } n     long long int root = -1; n     for (long long int i = 1; i <= n; i++) { n       if (g[i].size() > 1) { n         root = i; n         break; n       } n     } n     dfs(root, 0, 0); n     long long int mx = n - 1, mn; n     long long int odd = 0, even = 0; n     for (long long int i = 1; i <= n; i++) { n       if (g[i].size() > 1) continue; n       if (dist[i] % 2) n         odd++; n       else n         even++; n     } n     if (odd == 0 || even == 0) n       mn = 1; n     else n       mn = 3; n     for (long long int i = 1; i <= n; i++) { n       mx -= max(0LL, val[i] - 1); n     } n     cout << mn <<     << mx; n   } n   cerr <<   nTime elapsed:   << 1000 * clock() / CLOCKS_PER_SEC <<  ms n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int Maxn = 55; n long long dp[Maxn][10]; n int main() { n   string k; n   cin >> k; n   int n = k.length(); n   for (int i = 0; i < 10; i++) { n     dp[0][i] = 1; n   } n   for (int i = 1; i < n; i++) { n     int t = k[i] -  0 ; n     for (int j = 0; j < 10; j++) { n       int l = (t + j) / 2, r = (t + j + 1) / 2; n       dp[i][l] += dp[i - 1][j]; n       if (l != r) dp[i][r] += dp[i - 1][j]; n     } n   } n   bool flag = false; n   for (int i = 1; i < n; i++) { n     int t = k[i] -  0 , j = k[i - 1] -  0 ; n     if ((t + j + 1) / 2 != t && (t + j) / 2 != t) flag = true; n   } n   long long ans = (long long)-1 + flag; n   for (int i = 0; i <= 9; i++) { n     ans = (long long)ans + (long long)dp[n - 1][i]; n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long M = 1000000007; n const double pi = acos(-1); n const int N = 2e5 + 10; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(NULL); n   cout.tie(NULL); n   ; n   long long n, k, m, d, q = 1, ans = 0; n   cin >> n >> k >> m >> d; n   for (long long i = 1; i < d + 1; i++) { n     long long mx1 = m; n     long long mx2 = n / q; n     long long w = min(mx1, mx2) * i; n     ans = max(ans, w); n     q += k; n     if (q > n) break; n   } n   cout << ans; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 105; n const int M = 1005; n int n, m, x[N], s; n double p[N * M], tmp[N * M]; n int main() { n   scanf( %d%d , &n, &m); n   for (int i = 1; i <= n; ++i) scanf( %d , &x[i]); n   for (int i = 1; i <= n; ++i) s += x[i]; n   if (m == 1) { n     puts( 1.0000000000 ); n     return 0; n   } n   p[0] = 1; n   for (int i = 1; i <= n; ++i) { n     double now = 0; n     for (int j = 1; j < s; ++j) { n       now += p[j - 1]; n       if (x[i] <= j) now -= p[j - x[i]]; n       if (x[i] < j) now += p[j - x[i] - 1]; n       if (m < j) now -= p[j - m - 1]; n       tmp[j] = now / (m - 1); n     } n     for (int j = 0; j <= s; ++j) p[j] = tmp[j]; n     copy(tmp, tmp + s, p); n   } n   double ans = 0; n   for (int i = 1; i <= s; ++i) ans += p[i]; n   ans = ans * (m - 1) + 1; n   printf( %.10f n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 2e5 + 10; n const int M = 1e6 + 10; n int n, m, x, y, a[N], f[M]; n bitset<N> ans, tmp; n int main() { n   scanf( %d%d%d , &n, &x, &y); n   for (int i = 1; i <= n + 1; i++) { n     scanf( %d , &a[i]); n     tmp.set(a[i]); n   } n   for (int i = 1; i <= n + 1; i++) { n     ans |= tmp >> a[i]; n   } n   for (int i = 1; i <= x; i++) n     if (ans[i]) f[2 * (i + y)] = 2 * (i + y); n   for (int i = 1; i < M; i++) n     for (int j = i; j < M; j += i) f[j] = max(f[j], f[i]); n   scanf( %d , &m); n   while (m--) { n     scanf( %d , &x); n     if (f[x]) n       printf( %d  , f[x]); n     else n       printf( -1  ); n   } n } n


#include <bits/stdc++.h> n using namespace std; n int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1}; n long long gcd(long long a, long long b) { return b == 0 ? a : gcd(b, a % b); } n using namespace std; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   long long int n, m; n   cin >> n >> m; n   string s; n   char x, y; n   cin >> s; n   char ch[26]; n   for (long long int i = 0; i < 26; i++) { n     ch[i] = i +  a ; n   } n   for (long long int i = 0; i < m; i++) { n     cin >> x >> y; n     for (long long int j = 0; j < 26; j++) { n       if (x == ch[j]) { n         ch[j] = y; n       } else if (y == ch[j]) { n         ch[j] = x; n       } n     } n   } n   for (long long int i = 0; i < s.size(); i++) { n     cout << ch[s[i] -  a ]; n   } n   return 0; n } n


#include <bits/stdc++.h> n const int N = 200010; n std::vector<int> e[N]; n int color[N], dep[N]; n bool vis[N]; n int n, k; n int max = 0; n void dfs(int u, int fa) { n   max = std::max(max, dep[u]); n   for (auto v : e[u]) { n     if (vis[v] || v == fa) { n       continue; n     } n     dep[v] = dep[u] + 1; n     dfs(v, u); n   } n } n std::vector<int> stack; n std::vector<int> diameter; n void find_diameter(int u, int fa, int t) { n   stack.push_back(u); n   if (u == t) { n     diameter = stack; n   } n   for (auto v : e[u]) { n     if (v == fa) { n       continue; n     } n     find_diameter(v, u, t); n   } n   stack.pop_back(); n } n void solve(int u, int fa, int add) { n   for (auto v : e[u]) { n     if (vis[v] || v == fa) { n       continue; n     } n     color[v] = (color[u] + add + k) % k; n     solve(v, u, add); n   } n } n int main() { n   scanf( %d%d , &n, &k); n   for (int i = 0, u, v; i < n - 1; ++i) { n     scanf( %d%d , &u, &v); n     e[u].push_back(v); n     e[v].push_back(u); n   } n   if (k == 2) { n     solve(1, 0, 1); n     puts( Yes ); n     for (int i = 1; i <= n; ++i) { n       printf( %d%c , color[i] + 1,    n [i == n]); n     } n     return 0; n   } n   dfs(1, 0); n   int s = std::max_element(dep + 1, dep + n + 1) - dep; n   dep[s] = 0; n   dfs(s, 0); n   int t = std::max_element(dep + 1, dep + n + 1) - dep; n   find_diameter(s, 0, t); n   int sz = diameter.size(); n   for (int i = 0; i < sz; ++i) { n     color[diameter[i]] = i % k; n     vis[diameter[i]] = true; n   } n   for (int i = 0; i < sz; ++i) { n     dep[diameter[i]] = 0; n     max = 0; n     dfs(diameter[i], 0); n     if (max == 0) { n       continue; n     } n     if (max + i + 1 < k) { n       solve(diameter[i], 0, -1); n     } else if (max + sz - i < k) { n       solve(diameter[i], 0, 1); n     } else { n       puts( No ); n       return 0; n     } n   } n   puts( Yes ); n   for (int i = 1; i <= n; ++i) { n     printf( %d%c , color[i] + 1,    n [i == n]); n   } 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   long long n1, m; n   cin >> n1 >> m; n   string s; n   cin >> s; n   long long n; n   n = s.size(); n   long long lps[n]; n   lps[0] = 0; n   long long i, j; n   if (m == 0) { n     long long ans3 = 1; n     for (i = 1; i <= n1; i++) { n       ans3 = (ans3 * 26) % 1000000007; n     } n     cout << ans3 << endl; n     return 0; n   } n   i = 0; n   i++; n   long long len = 0; n   j = 0; n   while (i < n) { n     if (s[i] == s[len]) { n       len++; n       lps[i] = len; n       i++; n     } else { n       if (len != 0) { n         len = lps[len - 1]; n       } else { n         lps[i] = 0; n         i++; n       } n     } n   } n   vector<long long> v; n   for (i = 0; i < m; i++) { n     long long x; n     cin >> x; n     v.push_back(x); n   } n   if (s.size() == 1) { n     long long ans3 = 1; n     for (i = 1; i <= n1 - m; i++) { n       ans3 = (ans3 * 26) % 1000000007; n     } n     cout << ans3 << endl; n     return 0; n   } n   if (s.size() == 1 && m == 1) { n     cout << 1 << endl; n     return 0; n   } n   long long vis[n1]; n   for (i = 0; i < n1; i++) { n     vis[i] = 0; n   } n   long long r; n   r = -1; n   for (i = 0; i < m; i++) { n     long long x; n     x = v[i]; n     x--; n     if (x > r) { n       if (x + n - 1 >= n1) { n         cout << 0 << endl; n         return 0; n       } n       for (j = x; j < x + n; j++) { n         vis[j] = 1; n       } n       r = x + n - 1; n     } else { n       if (r + (n - (r - x + 1)) >= n1) { n         cout << 0 << endl; n         return 0; n       } n       for (j = r + 1; j <= r + (n - (r - x + 1)); j++) { n         vis[j] = 1; n       } n       r = r + (n - (r - x + 1)); n     } n   } n   long long c = 0LL; n   for (i = 0; i < n1; i++) { n     if (vis[i] == 0) c++; n   } n   long long arr[c + 1]; n   arr[0] = 1; n   for (i = 1; i <= c; i++) { n     arr[i] = (arr[i - 1] * 26) % 1000000007; n   } n   map<long long, long long> m1; n   long long k1; n   k1 = lps[n - 1]; n   while (k1 > 0) { n     m1[n - k1] = 1; n     k1 = lps[k1 - 1]; n   } n   if (m == 1) { n     cout << arr[c] << endl; n     return 0; n   } n   r = v[0] - 1; n   for (i = 1; i < m; i++) { n     long long x; n     x = v[i]; n     x--; n     if (r + n - 1 < x) { n       r = x; n     } else { n       long long pos; n       pos = x - r; n       if (m1.find(pos) == m1.end()) { n         cout << 0 << endl; n         return 0; n       } else n         r = x; n     } n   } n   cout << arr[c] << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 100010; n const int INF = 0x7f7f7f7f; n const double pi = 4.0 * atan(1.0); n int cases; n struct Segment_Tree { n   long long lazy[MAXN << 2]; n   long long data[MAXN << 2]; n   long long flag[MAXN << 2]; n   void pushup(int o) { n     data[o] = data[(o << 1)] + data[(o << 1) | 1]; n     if (flag[(o << 1)] != flag[(o << 1) | 1]) { n       flag[o] = 0; n     } else { n       flag[o] = flag[(o << 1)]; n     } n   } n   void setlazy(int o, int m, long long v) { n     lazy[o] += v; n     data[o] += m * v; n   } n   void pushdown(int o, int m) { n     if (lazy[o]) { n       setlazy((o << 1), m - (m >> 1), lazy[o]); n       setlazy((o << 1) | 1, m >> 1, lazy[o]); n       flag[(o << 1)] = flag[(o << 1) | 1] = flag[o]; n       lazy[o] = 0; n     } n   } n   void build_tree(int o, int l, int r) { n     if (l == r) { n       flag[o] = l; n       return; n     } n     int mid = (l + r) >> 1; n     build_tree((o << 1), l, mid); n     build_tree((o << 1) | 1, mid + 1, r); n     pushup(o); n   } n   void update(int o, int l, int r, int x, int y, long long v) { n     if (x <= l && r <= y && flag[o] != 0) { n       lazy[o] += abs(flag[o] - v); n       data[o] += (long long)(r - l + 1) * abs(flag[o] - v); n       flag[o] = v; n       return; n     } n     pushdown(o, r - l + 1); n     int mid = (l + r) >> 1; n     if (x <= mid) update((o << 1), l, mid, x, y, v); n     if (y > mid) update((o << 1) | 1, mid + 1, r, x, y, v); n     pushup(o); n   } n   long long query(int o, int l, int r, int x, int y) { n     if (x <= l && r <= y) { n       return data[o]; n     } n     pushdown(o, r - l + 1); n     int mid = (l + r) >> 1; n     if (y <= mid) return query((o << 1), l, mid, x, y); n     if (x > mid) return query((o << 1) | 1, mid + 1, r, x, y); n     return query((o << 1), l, mid, x, y) + n            query((o << 1) | 1, mid + 1, r, x, y); n   } n } segtree; n int main() { n   int n, m, op, x, y; n   long long z; n   scanf( %d%d , &n, &m); n   segtree.build_tree(1, 1, n); n   for (int i = 0; i < m; i++) { n     scanf( %d , &op); n     if (op == 1) { n       scanf( %d%d%lld , &x, &y, &z); n       segtree.update(1, 1, n, x, y, z); n     } else if (op == 2) { n       scanf( %d%d , &x, &y); n       printf( %lld n , segtree.query(1, 1, n, x, y)); n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n int n, a[100001], c[100001]; n int main() { n   cin >> n; n   for (int i = 0; i < n; i++) cin >> a[i]; n   int s = 0; n   for (int i = n - 1; i >= 0; i--) n     if (s > 0) n       s -= a[i]; n     else { n       c[i] = 1; n       s += a[i]; n     } n   if (s < 0) n     for (int i = 0; i < n; i++) c[i] ^= 1; n   for (int i = 0; i < n; i++) cout << (c[i] ?  +  :  - ); n   cout << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct num2 { n   static const int MB = 1e9 + 9; n   int b; n   num2() {} n   num2(int x) : b(x) {} n   num2 operator+(const num2& x) const { return num2((b + x.b) % MB); } n   num2 operator-(const num2& x) const { return num2((b + MB - x.b) % MB); } n   num2 operator*(int x) const { return num2(((long long)b * x) % MB); } n   num2 operator*(const num2& x) const { n     return num2(((long long)b * x.b) % MB); n   } n   bool operator==(const num2& x) const { return b == x.b; } n }; n const int MODP = 701; n template <class T> n struct StringOps { n   StringOps() {} n   StringOps(const string& s) : s(s) { n     n = s.length(); n     deg.resize(n + 1); n     hashes.resize(n); n     deg[0] = T(1); n     for (int i = 1; i <= n; ++i) { n       deg[i] = deg[i - 1] * T(MODP); n     } n     T curhash = T(0); n     for (int i = 0; i < n; ++i) { n       curhash = curhash * T(MODP) + T(s[i]); n       hashes[i] = curhash; n     } n   } n   char get(int idx) { return s[idx]; } n   T get_hash(int l, int r) { n     T cur(0); n     if (l > 0) { n       cur = hashes[l - 1]; n     } n     return hashes[r] - cur * deg[r - l + 1]; n   } n   int string_lcp(int l, int r, int lim = 1e9) { n     int cleft = 1, cright = min(n - r, n - l); n     cright = min(lim, cright); n     int res = 0; n     while (cleft <= cright) { n       int key = (cleft + cright) / 2; n       if (s[l + key - 1] != s[r + key - 1]) { n         cright = key - 1; n       } else if (get_hash(l, l + key - 1) == get_hash(r, r + key - 1)) { n         res = key; n         cleft = key + 1; n       } else { n         cright = key - 1; n       } n     } n     return res; n   } n   vector<T> hashes; n   vector<T> deg; n   string s; n   int n; n }; n const int maxN = 1000100; n const int MOD = 1e9 + 7; n int len1, len2; n int u, v; n StringOps<num2> sops; n int get_nlength(const pair<int, int>& a) { n   if (a.first == u) { n     if (a.second != len1) { n       return len1 - 1; n     } else { n       return len1; n     } n   } n   if (a.second != len2) { n     return len2 - 1; n   } else { n     return len2; n   } n } n void fill_segs(int& l1, int& r1, int& l2, int& r2, const pair<int, int>& a) { n   if (a.first == u) { n     l2 = r2 = -1; n     if (a.second == 0) { n       l1 = 1, r1 = len1 - 1; n     } else if (a.second >= len1 - 1) { n       l1 = 0, r1 = a.second - 1; n     } else { n       l1 = 0, r1 = a.second - 1; n       l2 = a.second + 1, r2 = len1 - 1; n     } n     return; n   } n   l2 = r2 = -1; n   if (a.second == 0) { n     l1 = 1, r1 = len2 - 1; n   } else if (a.second >= len2 - 1) { n     l1 = 0, r1 = a.second - 1; n   } else { n     l1 = 0, r1 = a.second - 1; n     l2 = a.second + 1, r2 = len2 - 1; n   } n   l1 += len1, r1 += len1; n   if (l2 != -1) { n     l2 += len1, r2 += len1; n   } n } n int cmp2(vector<pair<int, int>>& a, vector<pair<int, int>>& b, int p1 = 0, n          int p2 = 0) { n   if (p1 == a.size() && p2 < b.size()) { n     return -1; n   } n   if (p1 == a.size() && p2 == b.size()) { n     return 0; n   } n   if (p2 == b.size() && p1 < a.size()) { n     return 1; n   } n   if (p1 + 1 == a.size() && p2 + 1 == b.size() && a[0] == b[0]) { n     return 0; n   } n   if (p1 + 2 == a.size() && p2 + 2 == b.size() && a[0] == b[0] && n       a[1] == b[1]) { n     return 0; n   } n   if (sops.get(a[p1].first) != sops.get(b[p2].first)) { n     char c1 = sops.get(a[p1].first); n     char c2 = sops.get(b[p2].first); n     if (c1 < c2) { n       return -1; n     } n     return 1; n   } n   int lcp = sops.string_lcp( n       a[p1].first, b[p2].first, n       min(a[p1].second - a[p1].first + 1, b[p2].second - b[p2].first + 1)); n   lcp = min( n       lcp, min(a[p1].second - a[p1].first + 1, b[p2].second - b[p2].first + 1)); n   int nx = a[p1].first + lcp - 1, ny = b[p2].first + lcp - 1; n   if (nx >= a[p1].second) { n     ++p1; n   } else { n     a[p1].first = nx + 1; n   } n   if (ny >= b[p2].second) { n     ++p2; n   } else { n     b[p2].first = ny + 1; n   } n   return cmp2(a, b, p1, p2); n } n bool cmp(const pair<int, int>& a, const pair<int, int>& b) { n   int nlen1 = get_nlength(a); n   int nlen2 = get_nlength(b); n   if (nlen1 == 0 && nlen2 == 0) { n     if (a.first == u & b.first == v) { n       return true; n     } n     return false; n   } n   if (nlen1 == 0) { n     return true; n   } n   if (nlen2 == 0) { n     return false; n   } n   int l1, r1, l2, r2; n   fill_segs(l1, r1, l2, r2, a); n   int p1, q1, p2, q2; n   fill_segs(p1, q1, p2, q2, b); n   vector<pair<int, int>> na; n   vector<pair<int, int>> nb; n   na.push_back(make_pair(l1, r1)); n   if (l2 != -1) { n     na.push_back(make_pair(l2, r2)); n   } n   nb.push_back(make_pair(p1, q1)); n   if (p2 != -1) { n     nb.push_back(make_pair(p2, q2)); n   } n   int val = cmp2(na, nb); n   if (val == 0) { n     if (a.first == u && b.first == v) { n       return true; n     } n     return false; n   } n   return val == -1; n } n struct Node { n   Node() { left = right = nullptr; } n   int idxleft, idxright; n   Node* left; n   Node* right; n }; n int curorder[maxN]; n int lenorder; n void dfs(Node* root) { n   if (!root) { n     return; n   } n   if (root->left) { n     dfs(root->left); n   } n   for (int i = root->idxleft; i <= root->idxright; ++i) { n     curorder[lenorder] = i; n     ++lenorder; n   } n   if (root->right) { n     dfs(root->right); n   } n } n pair<int, int> order[2][maxN]; n int clen[2]; n void get_order(const string& s, int p) { n   int n = s.length(); n   Node* root = new Node(); n   Node* curnode = root; n   for (int i = 0; i < n;) { n     int cur = i; n     while (cur < n && s[cur] == s[i]) { n       ++cur; n     } n     curnode->idxleft = i, curnode->idxright = cur - 1; n     if (cur == n) { n       break; n     } n     if (s[i] < s[cur]) { n       Node* left = new Node(); n       curnode->left = left; n       curnode = curnode->left; n     } else { n       Node* right = new Node(); n       curnode->right = right; n       curnode = curnode->right; n     } n     i = cur; n   } n   lenorder = 0; n   dfs(root); n   int ps = n; n   for (int i = 0; i < n; ++i) { n     if (curorder[i] == n - 1) { n       ps = i + 1; n       break; n     } n   } n   vector<pair<int, int>> result; n   clen[p] = 0; n   for (int i = 0; i < ps; ++i) { n     order[p][clen[p]] = make_pair(v, curorder[i]); n     ++clen[p]; n   } n   order[p][clen[p]] = make_pair(v, n); n   ++clen[p]; n   for (int i = ps; i < n; ++i) { n     order[p][clen[p]] = make_pair(v, curorder[i]); n     ++clen[p]; n   } n } n int dp[2][maxN]; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(nullptr); n   int n; n   cin >> n; n   vector<string> s(n); n   for (int i = 0; i < n; ++i) { n     cin >> s[i]; n   } n   for (int i = 0; i <= s[0].length(); ++i) { n     dp[u][i] = 1; n   } n   sops = StringOps<num2>(s[0]); n   len1 = 0, len2 = s[0].length(); n   u = 1, v = 0; n   get_order(s[0], 0); n   swap(u, v); n   for (int i = 0; i + 1 < n; ++i) { n     string& s1 = s[i]; n     string& s2 = s[i + 1]; n     string nw = s1 + s2; n     len1 = s1.length(), len2 = s2.length(); n     sops = StringOps<num2>(nw); n     get_order(s[i + 1], v); n     int cadd = 0; n     int head1 = 0, head2 = 0; n     while (head1 < clen[u] || head2 < clen[v]) { n       if (head1 == clen[u]) { n         dp[v][order[v][head2].second] = cadd; n         ++head2; n       } else if (head2 == clen[v]) { n         break; n       } else { n         if (cmp(order[u][head1], order[v][head2])) { n           int nhead1 = head1; n           while (nhead1 < clen[u] && n                  s1[order[u][nhead1].second] == s1[order[u][head1].second] && n                  order[u][nhead1].second - order[u][head1].second == n                      nhead1 - head1) { n             ++nhead1; n           } n           for (int j = head1; j < nhead1; ++j) { n             cadd += dp[u][order[u][j].second]; n             if (cadd >= MOD) { n               cadd -= MOD; n             } n           } n           head1 = nhead1; n         } else { n           int nhead2 = head2; n           while (nhead2 < clen[v] && n                  s2[order[v][nhead2].second] == s2[order[v][head2].second] && n                  order[v][nhead2].second - order[v][head2].second == n                      nhead2 - head2) { n             ++nhead2; n           } n           for (int j = head2; j < nhead2; ++j) { n             dp[v][order[v][j].second] = cadd; n           } n           head2 = nhead2; n         } n       } n     } n     swap(u, v); n   } n   int res = 0; n   int res2 = 0; n   for (int i = 0; i <= s.back().length(); ++i) { n     res += dp[u][i]; n     if (res >= MOD) { n       res -= MOD; n     } n   } n   cout << res << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n namespace Elaina { n template <class T> n inline T Max(const T x, const T y) { n   return x < y ? y : x; n } n template <class T> n inline T Min(const T x, const T y) { n   return x < y ? x : y; n } n template <class T> n inline T fab(const T x) { n   return x < 0 ? -x : x; n } n template <class T> n inline void getMax(T& x, const T y) { n   x = Max(x, y); n } n template <class T> n inline void getMin(T& x, const T y) { n   x = Min(x, y); n } n template <class T> n T gcd(const T x, const T y) { n   return y ? gcd(y, x % y) : x; n } n template <class T> n inline T readin(T x) { n   x = 0; n   int f = 0; n   char c; n   while ((c = getchar()) <  0  ||  9  < c) n     if (c ==  - ) f = 1; n   for (x = (c ^ 48);  0  <= (c = getchar()) && c <=  9 ; n        x = (x << 1) + (x << 3) + (c ^ 48)) n     ; n   return f ? -x : x; n } n template <class T> n void fwrit(const T x) { n   if (x < 0) return putchar( - ), fwrit(-x); n   if (x > 9) fwrit(x / 10); n   putchar(x % 10 ^ 48); n } n }  // namespace Elaina n using namespace Elaina; n const int maxn = 3e5; n const int jzm = 1000000007; n char fuck[maxn + 5]; n int min_len; n char s[maxn + 5]; n int n; n int tot[maxn + 5][4]; n int sy = 0; n int ans[maxn + 5]; n inline void addchar(const char c, const int i) { n   ++n; n   s[n] = c, ++tot[n][i]; n } n inline void init() { n   min_len = (1 << 30); n   for (int id = 1, id_end_ = 3; id <= id_end_; ++id) { n     scanf( %s , fuck + 1); n     int len = strlen(fuck + 1); n     for (int i = 1, i_end_ = len; i <= i_end_; ++i) addchar(fuck[i], id); n     addchar( z  + id, 0); n     min_len = Min(min_len, len); n   } n   --n; n } n int sa[maxn + 5], x[maxn * 2 + 5], y[maxn * 2 + 5], c[maxn + 5], lev; n inline void getsa() { n   lev =  z  + 3; n   for (int i = 1, i_end_ = n; i <= i_end_; ++i) ++c[x[i] = s[i]]; n   for (int i = 2, i_end_ = lev; i <= i_end_; ++i) c[i] += c[i - 1]; n   for (int i = n, i_end_ = 1; i >= i_end_; --i) sa[c[x[i]]--] = i; n   for (int k = 1; k <= n; k <<= 1) { n     int sz = 0; n     for (int i = n - k + 1, i_end_ = n; i <= i_end_; ++i) y[++sz] = i; n     for (int i = 1, i_end_ = n; i <= i_end_; ++i) n       if (sa[i] > k) y[++sz] = sa[i] - k; n     for (int i = 1, i_end_ = lev; i <= i_end_; ++i) c[i] = 0; n     for (int i = 1, i_end_ = n; i <= i_end_; ++i) ++c[x[i]]; n     for (int i = 2, i_end_ = lev; i <= i_end_; ++i) c[i] += c[i - 1]; n     for (int i = n, i_end_ = 1; i >= i_end_; --i) n       sa[c[x[y[i]]]--] = y[i], y[i] = 0; n     swap(x, y); n     x[sa[1]] = 1; n     int level = 1; n     for (int i = 2, i_end_ = n; i <= i_end_; ++i) n       x[sa[i]] = (y[sa[i]] == y[sa[i - 1]] && y[sa[i] + k] == y[sa[i - 1] + k]) n                      ? level n                      : ++level; n     if (level == n) break; n     lev = level; n   } n } n int heit[maxn + 5], rk[maxn + 5]; n inline void getheit() { n   int h = 0; n   for (int i = 1, i_end_ = n; i <= i_end_; ++i) rk[sa[i]] = i; n   for (int i = 1, i_end_ = n; i <= i_end_; ++i) { n     if (rk[i] == 1) continue; n     if (h) --h; n     int j = sa[rk[i] - 1]; n     while (i + h <= n && j + h <= n && s[i + h] == s[j + h]) ++h; n     heit[rk[i]] = h; n   } n } n struct node { n   int x, v; n   inline int operator<(const node rhs) const { return v > rhs.v; } n } v[maxn + 5 + 5]; n int fa[maxn + 5]; n int find(const int x) { return fa[x] == x ? x : fa[x] = find(fa[x]); } n int cock(const int x) { n   return 1ll * tot[x][1] * tot[x][2] % jzm * tot[x][3] % jzm; n } n inline void merge(int x, int y) { n   x = find(x), y = find(y); n   if (x == y) return; n   sy -= cock(x); n   if (sy < 0) sy += jzm; n   sy -= cock(y); n   if (sy < 0) sy += jzm; n   fa[x] = y; n   for (int j = 1, j_end_ = 3; j <= j_end_; ++j) tot[y][j] += tot[x][j]; n   sy += cock(y); n   if (sy >= jzm) sy -= jzm; n } n inline void solve() { n   for (int i = 1, i_end_ = n; i <= i_end_; ++i) v[i] = node{sa[i], heit[i]}; n   for (int i = 1, i_end_ = n; i <= i_end_; ++i) fa[i] = i; n   sort(v + 1, v + n + 1); n   int now = 1; n   for (int i = min_len, i_end_ = 1; i >= i_end_; --i) { n     while (now <= n && v[now].v >= i) { n       int pre = sa[rk[v[now].x] - 1]; n       merge(pre, v[now].x); n       ++now; n     } n     ans[i] = sy; n   } n   for (int i = 1, i_end_ = min_len; i <= i_end_; ++i) printf( %d  , ans[i]); n } n signed main() { n   init(); n   getsa(); n   getheit(); n   solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int inf = 1000000007; n const long double eps = 1e-9; n const long double pi = 3.141592653589793; n const long double e = 2.718281828459045; n int dp[100005], h[100005]; n int n, mmm; n int main() { n   memset(dp, -1, sizeof(dp)); n   memset(h, 0, sizeof(h)); n   mmm = 0; n   scanf( %d , &n); n   dp[0] = 0; n   dp[n + 1] = 0; n   for (int i = 1; i <= n; i++) { n     scanf( %d , &h[i]); n   } n   for (int i = 1; i <= n; i++) { n     dp[i] = min(dp[i - 1] + 1, h[i]); n   } n   for (int i = n; i >= 1; i--) { n     dp[i] = min(dp[i], dp[i + 1] + 1); n     mmm = max(mmm, dp[i]); n   } n   cout << mmm << endl; n   return 0; n } n


//THINK ALOUD !!  n #include <bits/stdc++.h> n #define debug(x) cout <<   -> [   << #x <<   =   << x <<  ] n   n #define debug2(x, y) cout <<   -> [   << #x <<   =   << x <<   ,   << #y <<   =   << y <<   ] n  n #define debug3(x, y, z) cout <<   -> [   << #x <<   =   << x <<   ,   << #y <<   =   << y <<   ,   << #z <<   =   << z <<   ] n ; n #define debug4(x, y, z, xx) cout <<   -> [   << #x <<   =   << x <<   ,   << #y <<   =   << y <<   ,   << #z <<   =   << z <<   ,   << #xx <<   =   << xx <<   ] n ; n  n using namespace std; n using ll=long long int; n using lld=long double; n #define mp make_pair n #define pb push_back n #define accu accumulate  n  n #define pll pair<ll, ll> n #define pii pair<int, int>  n #define pci pair<char, int>  n #define pic pair<int, char>  n #define pls pair<ll, string> n #define psl pair<string, ll> n #define pis pair<int, string> n #define psi paid<string, int> n // #define x first  n // #define y second n #define fast ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); n  n  n #define rep(i, a, n) for (ll i = a; i < n; i++) n #define per(i, a, n) for (ll i = n-1; i >= a; i--) n #define repp(i, a, n, p) for (ll i = a; i < n; i += p) n #define all(c) (c).begin(), (c).end() n #define lla(c) (c).rbegin(), (c).rend() n #define print_arr(arr, n) for (int i = 0; i < n; i++) std::cout << arr[i] <<    ; std::cout <<   n ; n #define mod 1000000007 n #define primeSize 2000000 n #define Size 1e6 n  n template <typename t> inline void print(const t & v) { n     for (const auto& i : v) { n         std::cout << i <<    ; n     } std::cout <<   n ; n } n  n template <typename t> inline void printt(const t & v) {  n     for (const auto& i : v) {  n         std::cout << i.first <<     << i.second <<   n ; n     } std::cout <<   n ; n } n  n template <typename INT> INT GCD(INT a, INT b) {  n     //     if (b == 0) return a;  n     //     return gcd(b, a%b); n     return b ? gcd(b, a%b) : a; n } n  n  n template <typename INT> INT LCM(INT a, INT b) {  n     return a / gcd(a, b) * b; n } n  n template <typename INT> std::map<INT, int> factorize(INT n) {  n     map<INT, int> mp;  n     for (INT i = 2; i * i <= n; i++) {  n         while (n % i == 0) {  n             n /= i;  n             mp[i] ++; n         } n     } n  n     if (n > 1) mp[n] += 1; n     return mp; n } n  n ll bin_pow(ll a, ll b) { n     if (b == 0) return 1; n     long long res = bin_pow(a, b / 2); n     if (b % 2) return res * res * a; n     else return res * res; n } n  n int totalDigits(ll i) {  n     return i > 0 ? (int) log10 ((double) i) + 1 : 1; n } n  n vector<bool> isPrime(primeSize, true); n vector<int> arePrimes; n void make_seive() {  n     isPrime[0] = false;  n     isPrime[1] = false;  n  n     for (int i = 2; i <= sqrt(primeSize); i++) {  n         if (isPrime[i]) {  n             for (int j = i * 2; j <= primeSize; j += i) {  n                 isPrime[j] = false;  n             } n         } n     } n  n     for (int i = 2; i < primeSize; i++) {  n         if (isPrime[i]) {  n             arePrimes.push_back(i);  n         } n     } n } n  n  n void solve() { n     int n; n     cin >> n; n  n     vector<ll> X; n     vector<ll> Y; n  n     for (int i = 0; i < 2 * n; i++) {  n         int a, b;  n         cin >> a >> b;  n  n         a = abs(a);  n         b = abs(b); n         if (a == 0)  n             X.push_back(b); n         else  n             Y.push_back(a); n     } n  n     sort(all(X)); n     sort(lla(Y)); n  n     lld ans = 0; n     lld ans2 = 0; n     for (int i = 0; i < n; i++)  n         ans += sqrt(X[i]*X[i] + Y[i]*Y[i]); n  n     sort(all(Y)); n     for (int i = 0; i < n; i++)  n         ans2 += sqrt(X[i]*X[i] + Y[i]*Y[i]); n  n      n     lld final_ans = 0; n     final_ans = min(ans, ans2); n     cout << setprecision(20) << final_ans <<   n ; n } n  n int main() {  n     fast;  n     int t = 1; n     //make_seive(); n     cin >> t; n     while (t--)  n         solve();  n } n  n /* n Tips: n -- for queries involving sum using queue or minimum sum of size k, do also consider prefix sum.  n */ n  n /* n    Build and running n    g++ --std=c++17 -Wall -Wextra -Wshadow -fsanitize=undefined -fsanitize=address FILE.cpp && ./a.out < in  n  n    Naming  n    -for types (class/struct name) UpperCamelCase: Point, Info, SegTree  n    -functions and variable lowerCamelCase: someMethod, someVariable  n    -MACROS and constants all capital letters separated by _ : SOME_MACRO, MAX_N, MOD n  n */ n


#include <bits/stdc++.h> n using namespace std; n const int inft = 1000000009; n const int MAXN = 1000006; n int n, T[MAXN], A[MAXN], B[MAXN]; n int main() { n   scanf( %d , &n); n   for (int i = 0; i < (n); ++i) scanf( %d , &T[i]); n   A[0] = 1; n   for (int i = 1; i < n; ++i) A[i] = min(A[i - 1] + 1, T[i]); n   B[n - 1] = 1; n   for (int i = n - 2; i >= 0; --i) B[i] = min(B[i + 1] + 1, T[i]); n   int ret = 0; n   for (int i = 0; i < (n); ++i) ret = max(ret, min(A[i], B[i])); n   printf( %d n , ret); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <class T> n T mymax(T a, T b) { n   return a > b ? a : b; n } n template <class T> n T mymin(T a, T b) { n   return a < b ? a : b; n } n int main() { n   int n; n   int a[1001]; n   int s[1001] = {0}, sz, ymax = -1; n   int i, j, k; n   cin >> n; n   for (i = 1; i <= n; i++) { n     cin >> a[i]; n     s[i] = a[i] + s[i - 1]; n   } n   sz = s[n] + 1; n   char M[2002][sz]; n   for (j = 2001; j >= 1; j--) { n     for (i = 0; i <= sz - 1; i++) { n       M[j][i] =    ; n     } n   } n   int x = 1, y = 1000; n   for (i = 1; i <= n; i++) { n     j = a[i]; n     int al; n     if (i % 2 == 1) { n       for (al = 1; al <= j; al++) { n         M[y][x] =  / ; n         x++; n         y++; n       } n       y--; n     } else { n       for (al = 1; al <= j; al++) { n         M[y][x] =     ; n         x++; n         y--; n       } n       y++; n     } n   } n   for (j = 2001; j >= 1; j--) { n     int f = 0; n     for (i = 1; i <= s[n]; i++) { n       if (M[j][i] !=    ) f = 1; n     } n     if (f) { n       for (i = 1; i <= s[n]; i++) { n         cout << M[j][i]; n       } n       cout <<   n ; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long K = 1000000000000000000; n map<int, int> M; n int q, mn, w[600005], nxt[600005], Q[600005], anc[600005], r; n char opt[5], s[600005]; n long long ans0, ans, g, ansx, ansy; n int mask((1 << 30) - 1); n int calc(int x) { return w[Q[lower_bound(Q + 1, Q + r + 1, x) - Q]]; } n int main() { n   mn = 1 << 30; n   scanf( %d , &q); n   int j(0); n   for (int i = 1; i <= q; ++i) { n     scanf( %s%d , opt, &w[i]); n     s[i] = (ansy + opt[0] -  a ) % 26 +  a ; n     w[i] ^= (ansx & mask); n     mn = min(mn, w[i]); n     if (i == 1) { n       printf( %lld n , ans = w[i]); n       ansx = ans & mask; n       ansy = w[i] % 26; n       while (r && w[Q[r]] >= w[i]) --r; n       Q[++r] = i; n       continue; n     } n     while (j && s[j + 1] != s[i]) j = nxt[j]; n     if (s[j + 1] == s[i]) ++j; n     nxt[i] = j; n     if (s[i] == s[nxt[i - 1] + 1]) n       anc[i - 1] = anc[nxt[i - 1]]; n     else n       anc[i - 1] = nxt[i - 1]; n     for (int j = (s[i - 1] == s[1] ? i - 1 : nxt[i - 1]); j;) { n       if (s[j + 1] == s[i]) n         j = anc[j]; n       else { n         int o(calc(i - j)); n         g -= o; n         map<int, int>::iterator it = M.lower_bound(o); n         assert(it != M.end()); n         --it->second; n         if (!it->second) M.erase(it); n         j = nxt[j]; n       } n     } n     while (r && w[Q[r]] >= w[i]) --r; n     Q[++r] = i; n     int cnt(s[i] == s[1]); n     map<int, int>::iterator it = M.lower_bound(w[i]); n     while (it != M.end()) { n       g -= (long long)it->first * it->second; n       cnt += it->second; n       M.erase(it); n       it = M.lower_bound(w[i]); n     } n     g += (long long)cnt * w[i]; n     M.insert(make_pair(w[i], cnt)); n     ans += g, ans += mn; n     if (ans >= K) ++ans0, ans -= K; n     ansx = (ansx + g + mn) & mask; n     ansy = (ansy + g + mn) % 26; n     if (!ans0) n       printf( %lld n , ans); n     else { n       cout << ans0 << setw(18) << setfill( 0 ) << ans << endl; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long double chance[100005]; n int main() { n   int n, p; n   while (cin >> n >> p) { n     memset(chance, 0, sizeof chance); n     long double tmp; n     for (int i = 0; i < n; i++) { n       int a, b; n       cin >> a >> b; n       int tem = b / p - (a - 1) / p; n       tmp = (long double)tem / (b - a + 1); n       chance[i] = tmp; n     } n     long double ans = 0; n     for (int i = 1; i < n; i++) { n       ans += chance[i] + chance[i - 1] - chance[i] * chance[i - 1]; n     } n     ans += chance[0] + chance[n - 1] - chance[n - 1] * chance[0]; n     cout << fixed << setprecision(10) << ans * 2000.0 << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int inf = 0x3f3f3f3f; n const int _inf = 0xc0c0c0c0; n const long long INF = 0x3f3f3f3f3f3f3f3f; n const long long _INF = 0xc0c0c0c0c0c0c0c0; n const long long mod = (int)1e9 + 7; n const int N = 4e5 + 100; n int n, m; n vector<int> vc[N]; n int to[N]; n multiset<int> st; n void Ac() { n   for (int i = 1, v; i <= n; ++i) { n     scanf( %d , &v); n     to[v] = i; n   } n   st.insert(n + 1); n   for (int i = 1, u, v; i <= m; ++i) { n     scanf( %d%d , &u, &v); n     u = to[u]; n     v = to[v]; n     if (u > v) swap(u, v); n     vc[u].push_back(v); n     st.insert(v); n   } n   long long ans = 0; n   for (int i = 1; i <= n; ++i) { n     ans += (*st.begin()) - i; n     for (int v : vc[i]) { n       st.erase(st.lower_bound(v)); n     } n   } n   printf( %I64d n , ans); n } n int main() { n   while (~scanf( %d%d , &n, &m)) { n     Ac(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n map<long long, long long>::iterator itr; n void c_p_c() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n } n int32_t main() { n   c_p_c(); n   long long t; n   cin >> t; n   while (t--) { n     long long num, k; n     cin >> num >> k; n     vector<long long> v; n     vector<long long> v1; n     for (long long i = 0; i < num; i++) { n       long long x; n       cin >> x; n       v.push_back(x); n     } n     for (long long i = 0; i < num; i++) { n       long long x; n       cin >> x; n       v1.push_back(x); n     } n     sort(v.begin(), v.end()); n     sort(v1.begin(), v1.end(), greater<long long>()); n     long long sum = 0; n     for (long long i = 0; i < k; i++) { n       if (v1[i] > v[i]) n         sum += v1[i]; n       else n         sum += v[i]; n     } n     for (long long i = k; i < num; i++) { n       sum += v[i]; n     } n     cout << sum << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const long long INF = 1e9 + 7; n const int N = 3e5 + 10; n vector<pair<int, int>> vp[N]; n vector<int> v[N]; n int pos[N]; n int deg[N]; n int vis[N]; n vector<int> ans; n void dfs(int x, int p = 0, int o = 0) { n   if (vis[x]) return; n   v[p].push_back(x); n   vis[x] = 1; n   pos[x] = o; n   for (auto &pp : vp[x]) { n     int y, oo; n     tie(y, oo) = pp; n     dfs(y, x, oo); n   } n } n int solve(int x) { n   int cur = deg[x]; n   for (auto &y : v[x]) { n     cur ^= solve(y); n   } n   if (cur) ans.push_back(pos[x]); n   return cur; n } n int main() { n   int n, m; n   cin >> n >> m; n   int cnt1 = 0, cnt_1 = 0; n   for (int i = 1; i <= n; i++) { n     cin >> deg[i]; n     if (deg[i] == 1) cnt1 ^= 1; n     if (deg[i] == -1) cnt_1++; n   } n   if (cnt1 == 1 && cnt_1 == 0) { n     puts( -1 ); n     return 0; n   } n   for (int i = 1; i <= n; i++) { n     if (deg[i] == -1) { n       deg[i] = cnt1; n       cnt1 = 0; n     } n   } n   for (int i = 1; i <= m; i++) { n     int x, y; n     cin >> x >> y; n     vp[x].push_back({y, i}); n     vp[y].push_back({x, i}); n   } n   dfs(1); n   solve(1); n   printf( %d n , ans.size()); n   for (auto &x : ans) printf( %d n , x); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   string pal, pal2; n   int x = 0; n   bool b1 = false; n   cin >> pal; n   for (int i = 0; i < pal.size(); i++) { n     if (pal[i] ==  W  and pal[i + 1] ==  U  and pal[i + 2] ==  B ) { n       i += 2; n       if (b1 == true) { n         pal2 +=    ; n         b1 = false; n       } n     } else { n       b1 = true; n       pal2 += pal[i]; n     } n   } n   cout << pal2; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n inline int read() { n   int x = 0, f = 1; n   char c = getchar(); n   while (c <  0  || c >  9 ) { n     if (c ==  - ) f = -1; n     c = getchar(); n   } n   while (c >=  0  && c <=  9 ) n     x = (x << 3) + (x << 1) + (c ^  0 ), c = getchar(); n   return x * f; n } n const int N = 2e5 + 5; n int d, n, m; n struct node { n   int x, p; n } a[N]; n inline int operator<(const node x, const node y) { n   if (x.p == y.p) return x.x > y.x; n   return x.p > y.p; n } n inline int cmp(node x, node y) { return x.x < y.x; } n priority_queue<node> q; n long long ans; n int main() { n   d = read(), n = read(), m = read(); n   for (int i = 1; i <= m; i++) a[i] = {read(), read()}; n   sort(a + 1, a + m + 1, cmp); n   int t = n, j = 1; n   while (t < d) { n     while (j <= m && a[j].x <= t) q.push(a[j++]); n     while (!q.empty() && q.top().x + n <= t) q.pop(); n     if (q.empty()) return puts( -1 ), 0; n     int x = q.top().x, p = q.top().p; n     int nxt = min(d, x + n); n     if (j <= m && nxt >= a[j].x) nxt = a[j].x; n     ans += 1ll * p * (nxt - t); n     t = nxt; n   } n   printf( %lld n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long nMod = 1e9 + 7; n inline long long GCD(long long a, long long b) { n   while (b != 0) { n     long long c = a % b; n     a = b; n     b = c; n   } n   return a; n }; n inline long long LCM(long long a, long long b) { return (a / GCD(a, b)) * b; }; n void vietnakid() { n   int TC; n   cin >> TC; n   while (TC--) { n     int n; n     string s; n     cin >> n; n     cin >> s; n     int fm = n, lr = 0; n     for (int i = 0; i < n; i++) { n       if (s[i] ==  > ) { n         fm = min(fm, i); n       } else { n         lr = max(lr, i); n       } n     } n     cout << min(fm, n - lr - 1) <<   n ; n   } n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(); n   vietnakid(); n   cerr <<   nTime elapsed:   << 1000 * clock() / CLOCKS_PER_SEC <<  ms n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct node { n   long long int word, prefix; n   node *child[2]; n   node() { n     word = prefix = 0; n     child[0] = child[1] = NULL; n   } n }; n void insert(node *root, long long int x, long long int bit) { n   root->prefix += 1; n   if (bit < 0) { n     root->word += 1; n     return; n   } n   bool b = (x & (1 << bit)); n   if (root->child[b] == NULL) { n     root->child[b] = new node(); n   } n   insert(root->child[b], x, bit - 1); n } n node *deleteit(node *root, long long int x, long long int bit) { n   if (root == NULL) { n     return NULL; n   } n   root->prefix -= 1; n   if (bit < 0) { n     root->word -= 1; n     if (root->word == 0 && root->prefix == 0) { n       delete root; n       return NULL; n     } n     return root; n   } n   bool b = (x & (1 << bit)); n   root->child[b] = deleteit(root->child[b], x, bit - 1); n   if (root->word == 0 && root->prefix == 0) { n     delete root; n     return NULL; n   } n   return root; n } n long long int query(node *root, long long int x, long long int k, n                     long long int bit, long long int ans) { n   if (bit < 0 || root == NULL) { n     return ans; n   } n   bool a = (x & (1 << bit)), a_complement = (!a); n   bool b = (k & (1 << bit)), b_complement = (!b); n   if (b == true) { n     if (root->child[a] != NULL) { n       ans += root->child[a]->prefix; n     } n     return query(root->child[a_complement], x, k, bit - 1, ans); n   } else { n     return query(root->child[a], x, k, bit - 1, ans); n   } n   return ans; n } n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   node *root = new node; n   long long int q, x, y, t; n   cin >> q; n   for (long long int i = 0; i < q; i++) { n     cin >> t >> x; n     if (t == 1) { n       insert(root, x, 31); n     } else if (t == 2) { n       root = deleteit(root, x, 31); n       if (root == NULL) { n         root = new node; n       } n     } else { n       cin >> y; n       cout << query(root, x, y, 31, 0) <<   n ; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   int a, b, n; n   cin >> a >> b >> n; n   bool exist(false); n   for (int i = 0; i < 10; i++) { n     if ((a * 10 + i) % b == 0) { n       cout << a * 10 + i; n       string s(n - 1,  0 ); n       cout << s << endl; n       exist = true; n       break; n     } n   } n   if (!exist) cout <<  -1  << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n bool vis[100010]; n int pr[100010], e; n void Shai() { n   vis[1] = true; n   for (long long i = 2; i <= 1000ll; i++) { n     if (!vis[i]) { n       pr[++e] = i; n       for (long long j = i * i; j <= 1000ll; j += i) { n         vis[j] = true; n       } n     } n   } n } n int n, cnta[100010]; n int main() { n   Shai(); n   int x, y; n   scanf( %d , &n); n   for (int i = 1; i <= n; ++i) { n     scanf( %d%d , &x, &y); n     int X = x; n     for (int j = 1; j <= e; ++j) { n       if (X % pr[j] == 0) { n         while (X % pr[j] == 0) { n           X /= pr[j]; n           ++cnta[j]; n         } n       } n     } n     bool lose = 0; n     for (int j = 1; j <= e; ++j) { n       int cntb = 0; n       if (y % pr[j] == 0) { n         while (y % pr[j] == 0) { n           y /= pr[j]; n           ++cntb; n         } n       } n       if ((cnta[j] + cntb) % 3 != 0) { n         lose = 1; n         break; n       } else if (min(cnta[j], cntb) * 2 < max(cnta[j], cntb)) { n         lose = 1; n         break; n       } n     } n     if (!(X == 1 && y == 1) && !((long long)min(X, y) * (long long)min(X, y) == n                                  (long long)max(X, y))) { n       lose = 1; n     } n     for (int j = 1; j <= e; ++j) { n       cnta[j] = 0; n     } n     if (!lose) n       puts( Yes ); n     else n       puts( No ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n ifstream in( test.in ); n ofstream out( test.out ); n const int DIM = 2e3 + 1; n const int INF = 1e9 + 7; n int dp[DIM]; n set<int> mst; n deque<int> que; n vector<int> edg[DIM]; n int main() { n   ios::sync_with_stdio(false); n   cin.tie(); n   cout.tie(); n   int k, n; n   cin >> k >> n; n   for (int i = 1; i <= n; i++) { n     int x; n     cin >> x; n     mst.insert(x - k); n   } n   for (int x : mst) { n     for (int i = 0; i < DIM; i++) { n       if (i + x > -1 && i + x < DIM) edg[i].push_back(i + x); n     } n   } n   fill(dp, dp + DIM, INF); n   dp[DIM / 2] = 0; n   que.push_back(DIM / 2); n   int ans = INF; n   for (; que.empty() == false; que.pop_front()) { n     int x = que.front(); n     for (int y : edg[x]) { n       if (dp[y] == INF) n         dp[y] = dp[x] + 1, que.push_back(y); n       else if (y == DIM / 2) n         ans = min(ans, dp[x] + 1); n     } n   } n   cout << (ans == INF ? -1 : ans) << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int mod = 1e9 + 7; n const int N = 1e6 + 4; n bool isPrime(int n) { n   if (n <= 1) return false; n   if (n <= 3) return true; n   if (n % 2 == 0 || n % 3 == 0) return false; n   for (int i = 5; i * i <= n; i = i + 6) n     if (n % i == 0 || n % (i + 2) == 0) return false; n   return true; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   long long test = 1; n   cin >> test; n   for (int qw = 1; qw <= test; qw++) { n     int n; n     cin >> n; n     int a[n]; n     for (int i = 0; i < n; i++) cin >> a[i]; n     int sum = 0; n     for (int i = 0; i < n; i++) sum += a[i]; n     if (!isPrime(sum)) { n       cout << n << endl; n       for (int i = 1; i <= n; i++) cout << i <<    ; n       cout << endl; n     } else { n       int flag = 0; n       for (int i = 0; i < n; i++) { n         if (!isPrime(sum - a[i])) { n           cout << n - 1 << endl; n           flag = 1; n           for (int j = 0; j < n; j++) { n             if (i != j) cout << j + 1 <<    ; n           } n           cout << endl; n         } n         if (flag == 1) break; n       } n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int t; n   t = 1; n   while (t--) { n     int n, m, i, j; n     cin >> n >> m; n     int a[n + 2][m + 2]; n     int ii = -1, jj = -1; n     for (i = 0; i < n; i++) { n       for (j = 0; j < m; j++) { n         cin >> a[i][j]; n         if (a[i][j] > 0) ii = i; n         jj = j; n       } n     } n     if (ii == -1) n       cout << jj << endl; n     else { n       for (i = ii; i >= 0; i--) { n         for (j = jj; j >= 0; j--) { n           int mn = 9000; n           if (a[i][j] == 0) { n             if ((i + 1) < n) mn = min(mn, a[i + 1][j]); n             if ((j + 1) < m) mn = min(mn, a[i][j + 1]); n             a[i][j] = mn - 1; n           } n         } n       } n       int sum = 0, in = 1; n       for (i = 0; i < n; i++) { n         for (j = 0; j < m; j++) { n           sum += a[i][j]; n           if (i < n - 1 && j < m) n             if (a[i + 1][j] <= a[i][j]) in = 0; n           if (j < m - 1 && i < n) n             if (a[i][j + 1] <= a[i][j]) in = 0; n         } n       } n       if (in == 1) n         cout << sum << endl; n       else n         cout << -1 << endl; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long gcd(long long a, long long b) { return b == 0 ? a : gcd(b, a % b); } n long long a, b, x, y; n long long best, ansp, ansq; n int main() { n   while (cin >> a >> b >> x >> y) { n     long long gg = gcd(x, y); n     x /= gg; n     y /= gg; n     best = -1; n     long long maxr = min(a / x, b / y); n     if (maxr <= 0) n       cout << 0 <<     << 0 << endl; n     else n       cout << x * maxr <<     << y * maxr << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int mod = 1e9 + 7; n const int maxn = 2e5 + 10; n int n, k; n long long fac[maxn + 10], afac[maxn + 10]; n long long qpow(long long a, long long b) { n   long long ret = 1LL; n   while (b) { n     if (b & 1) ret = ret * a % mod; n     a = a * a % mod; n     b >>= 1; n   } n   return ret; n } n long long inv(long long x) { return qpow(x, mod - 2); } n void Pre() { n   fac[0] = 1LL; n   for (int i = 1; i <= maxn; i++) fac[i] = fac[i - 1] * (long long)i % mod; n   afac[maxn] = inv(fac[maxn]); n   for (int i = maxn; i >= 1; --i) afac[i - 1] = afac[i] * i % mod; n } n long long C(long long n, long long m) { n   if (m > n) return 0; n   return fac[n] * afac[n - m] % mod * afac[m] % mod; n } n void solve() { n   vector<pair<int, int> > h; n   int l, r; n   for (int i = 1; i <= n; i++) { n     scanf( %d %d , &l, &r); n     h.push_back(make_pair(l - 1, 1)); n     h.push_back(make_pair(r, -1)); n   } n   sort(h.begin(), h.end()); n   long long cnt = 0LL, ans = 0LL; n   int pos = h[0].first; n   for (int i = 0; i < h.size(); i++) { n     long long tmp = C(cnt, k) * (h[i].first - pos) % mod; n     ans = (ans + tmp) % mod; n     cnt += 1LL * h[i].second; n     pos = h[i].first; n   } n   printf( %I64d n , ans); n } n int main() { n   Pre(); n   while (scanf( %d %d , &n, &k) != EOF) { n     solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 100005; n const int INF = 0x3f3f3f3f; n char tmp[20][20]; n char raw[MAXN]; n long long n, m, T, k; n bool check(int l, int r) { n   for (int i = 1; i <= n; i++) { n     if (r - l + 1 < strlen(tmp[i])) n       continue; n     else { n       int cur_len = strlen(tmp[i]); n       bool flg = true; n       for (int j = 0; j < cur_len; j++) { n         if (tmp[i][cur_len - j - 1] == raw[r - j]) n           continue; n         else { n           flg = false; n           continue; n         } n       } n       if (flg) return false; n     } n   } n   return true; n } n int main() { n   scanf( %s , raw); n   int l = 0, r = 0, res_len = 0, res_pos = 0; n   scanf( %I64d  , &n); n   for (int i = 1; i <= n; i++) scanf( %s , tmp[i]); n   while (r < strlen(raw)) { n     while (l <= r) { n       if (check(l, r)) { n         if (r - l + 1 > res_len) { n           res_len = r - l + 1; n           res_pos = l; n         } n         break; n       } n       l++; n     } n     r++; n   } n   cout << res_len <<     << res_pos << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int read() { n   int tot = 0, fh = 1; n   char c = getchar(); n   while (c <  0  || c >  9 ) { n     if (c ==  - ) fh = -1; n     c = getchar(); n   } n   while (c >=  0  && c <=  9 ) { n     tot = tot * 10 + c -  0 ; n     c = getchar(); n   } n   return tot * fh; n } n int n, cnt, a[20000], ha[20000], b[20000], c[20000], ans, f[200][20000]; n int main() { n   n = read(); n   for (int i = 1; i <= n; i++) a[i] = read(); n   sort(a + 1, a + n + 1); n   for (int i = 1; i <= n; i++) ha[a[i]]++; n   for (int i = 1; i <= 100; i++) n     if (ha[i]) { n       b[++cnt] = ha[i]; n       c[cnt] = i; n     } n   if (cnt == 1 || cnt == 2) n     cout << n <<   n ; n   else { n     for (int i = 1; i <= cnt; i++) { n       if (b[i] <= ans) continue; n       for (int j = 1; j <= 10000; j++) n         for (int k = 1; k <= 100; k++) f[k][j] = 0; n       f[0][0] = 1; n       for (int j = 1; j <= n; j++) n         if (a[j] != c[i]) { n           for (int k = j - 1; k >= 0; k--) n             for (int p = 10000 - a[j]; p >= 0; p--) n               if (f[k][p]) f[k + 1][p + a[j]] = 1; n         } n       for (int j = b[i]; j > ans && j >= 1; j--) { n         bool flag = true; n         for (int k = 0; k < j; k++) n           if (f[j - k][j * c[i] - k * c[i]] == 1) flag = false; n         if (flag) ans = j; n       } n     } n     cout << ans <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m; n bool flag[21][21]; n int deg[21], id[21]; n vector<int> G[21]; n inline int cmp(int x, int y) { n   return deg[x] != deg[y] ? deg[x] < deg[y] : x < y; n } n bool vis[21]; n inline void dfs(int np) { n   vis[np] = 1; n   for (int &x : G[np]) { n     if (!vis[x]) { n       dfs(x); n       return; n     } n   } n } n int main() { n   scanf( %d%d , &n, &m); n   for (int i = 1, ti, tj; i <= m; i++) { n     scanf( %d%d , &ti, &tj); n     if (ti != tj) flag[ti][tj] = flag[tj][ti] = 1; n   } n   for (int i = 1; i <= n; i++) { n     id[i] = i; n     for (int j = 1; j <= n; j++) { n       if (flag[i][j]) deg[i]++, G[i].push_back(j); n     } n   } n   for (int i = 1; i <= n; i++) sort(G[i].begin(), G[i].end(), cmp); n   for (int i = 1; i <= n; i++) { n     memset(vis, 0, sizeof(vis)); n     dfs(i); n     int cnt = 0; n     for (int j = 1; j <= n; j++) cnt += vis[j]; n     if (cnt == n) { n       puts( Yes ); n       return 0; n     } n   } n   puts( No ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 300300; n int a[maxn]; n set<int> st; n int MIN(int x, int y) { return x < y ? x : y; } n int MAX(int x, int y) { return x > y ? x : y; } n int main(void) { n   st.clear(); n   int n, i, j, mi, mx, fg = 0, x; n   scanf( %d , &n); n   for (i = 1; i <= n - 1; i++) { n     scanf( %d , &a[i]); n     if (i == 1) { n       mx = mi = a[i]; n     } else { n       a[i] = a[i] + a[i - 1]; n       mx = MAX(mx, a[i]); n       mi = MIN(mi, a[i]); n     } n     if (!st.count(a[i])) { n       st.insert(a[i]); n     } else n       fg = 1; n     if (a[i] == 0) fg = 1; n   } n   if (fg) { n     printf( -1 n ); n     return 0; n   } n   for (i = 1; i <= n; i++) n     if (i + mx >= 1 && i + mx <= n && i + mi >= 1 && i + mi <= n) { n       fg = 1; n       x = i; n       break; n     } n   if (fg == 0) { n     printf( -1 n ); n   } else { n     printf( %d , x); n     for (i = 1; i <= n - 1; i++) { n       printf(  %d , x + a[i]); n     } n     printf(  n ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int INF = (int)1e8; n const int N = 500; n int n; n int ans; n int a[N]; n set<int> p; n int m; n int b[N][2]; n struct Edge { n   int to, cap, flow; n   Edge() : to(), cap(), flow() {} n   Edge(int _to, int _cap) : to(_to), cap(_cap), flow(0) {} n }; n int sz; n Edge ed[2 * N]; n vector<int> g[N]; n int S, T; n void fact(int x) { n   for (int i = 2; i * i <= x; i++) { n     if (x % i != 0) continue; n     p.insert(i); n     while (x % i == 0) x /= i; n   } n   if (x != 1) p.insert(x); n   return; n } n void addEdge(int v, int to, int cap) { n   ed[sz] = Edge(to, cap); n   g[v].push_back(sz++); n   ed[sz] = Edge(v, 0); n   g[to].push_back(sz++); n   return; n } n int q[N]; n int qSz; n int d[N]; n bool bfs(int S, int T, int n) { n   for (int i = 0; i < n; i++) d[i] = INF; n   d[S] = 0; n   qSz = 1; n   q[0] = S; n   for (int k = 0; k < qSz; k++) { n     int v = q[k]; n     for (int i = 0; i < (int)g[v].size(); i++) { n       Edge e = ed[g[v][i]]; n       if (e.flow == e.cap) continue; n       int to = e.to; n       if (d[to] <= d[v] + 1) continue; n       d[to] = d[v] + 1; n       q[qSz++] = to; n     } n   } n   return (d[T] < INF); n } n int idx[N]; n int dfs(int v, int T, int pushFlow) { n   int res = 0; n   if (v == T || pushFlow == 0) return pushFlow; n   for (; idx[v] < (int)g[v].size(); idx[v]++) { n     int id = g[v][idx[v]]; n     Edge e = ed[id]; n     int to = e.to; n     if (d[to] != d[v] + 1) continue; n     int deltaFlow = dfs(to, T, min(pushFlow, e.cap - e.flow)); n     pushFlow -= deltaFlow; n     res += deltaFlow; n     ed[id].flow += deltaFlow; n     ed[id ^ 1].flow -= deltaFlow; n     if (pushFlow == 0) break; n   } n   return res; n } n int flow(int S, int T, int n) { n   int res = 0; n   while (bfs(S, T, n)) { n     for (int i = 0; i < n; i++) idx[i] = 0; n     res += dfs(S, T, INF); n   } n   return res; n } n int solve(int q) { n   for (int i = 0; i < n + 2; i++) g[i].clear(); n   sz = 0; n   for (int i = 0; i < n; i++) { n     int c = 0; n     int t = a[i]; n     while (t % q == 0) { n       t /= q; n       c++; n     } n     if (i % 2 == 0) n       addEdge(S, i, c); n     else n       addEdge(i, T, c); n   } n   for (int i = 0; i < m; i++) addEdge(b[i][0], b[i][1], INF); n   return flow(S, T, n + 2); n } n int main() { n   scanf( %d%d , &n, &m); n   for (int i = 0; i < n; i++) scanf( %d , &a[i]); n   for (int i = 0; i < n; i++) fact(a[i]); n   for (int i = 0; i < m; i++) n     for (int j = 0; j < 2; j++) { n       scanf( %d , &b[i][j]); n       b[i][j]--; n     } n   for (int i = 0; i < m; i++) { n     if (b[i][0] % 2 == 1) swap(b[i][0], b[i][1]); n   } n   S = n; n   T = n + 1; n   for (auto it = p.begin(); it != p.end(); it++) ans += solve(*it); n   printf( %d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int p[200007]; n int c1[200007]; n int c2[200007]; n bool used[200007]; n int main() { n   int n; n   cin >> n; n   for (int i = 0; i < n; ++i) { n     scanf( %d , &p[i]); n   } n   for (int i = 0; i < n; ++i) { n     scanf( %d , &c1[i]); n     c1[i]--; n   } n   for (int i = 0; i < n; ++i) { n     scanf( %d , &c2[i]); n     c2[i]--; n   } n   priority_queue<pair<int, int>> q[3]; n   for (int i = 0; i < n; ++i) { n     if (c1[i] == c2[i]) { n       q[c1[i]].push(make_pair(-p[i], i)); n     } else { n       q[c1[i]].push(make_pair(-p[i], i)); n       q[c2[i]].push(make_pair(-p[i], i)); n     } n   } n   int m, x; n   cin >> m; n   for (int i = 0; i < m; ++i) { n     scanf( %d , &x); n     x--; n     while (!q[x].empty() && used[q[x].top().second]) q[x].pop(); n     if (q[x].empty()) n       printf( -1  ); n     else { n       printf( %d  , -q[x].top().first); n       used[q[x].top().second] = 1; n       q[x].pop(); n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n long long dp[64][64][64][64]; n double fact[64]; n int main() { n   int n, a[64], p, s = 0; n   cin >> n; n   for (int i = 0; i < n; ++i) { n     cin >> a[i]; n     s += a[i]; n   } n   cin >> p; n   if (s <= p) { n     printf( %.9f n , (double)n); n     return 0; n   } n   fact[0] = 1; n   for (int i = 1; i < 64; ++i) { n     fact[i] = fact[i - 1] * i; n   } n   for (int ignore = 0; ignore < n; ++ignore) { n     dp[ignore][0][0][0] = 1; n     for (int i = 0; i < n; ++i) { n       for (int j = 0; j <= i; ++j) { n         for (int k = 0; k <= p; ++k) { n           if (i != ignore && k + a[i] <= p) { n             dp[ignore][i + 1][j + 1][k + a[i]] += dp[ignore][i][j][k]; n           } n           dp[ignore][i + 1][j][k] += dp[ignore][i][j][k]; n         } n       } n     } n   } n   double res = 0; n   for (int cnt = 1; cnt < n; ++cnt) { n     long long pats = 0; n     for (int last = 0; last < n; ++last) { n       for (int w = max(0, p - a[last] + 1); w <= p; ++w) { n         pats += dp[last][n][cnt][w]; n       } n     } n     res += cnt * (double)pats * fact[cnt] * fact[n - cnt - 1] / fact[n]; n   } n   printf( %.9f n , res); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   cin.sync_with_stdio(false); n   long long int n; n   cin >> n; n   vector<long long int> a(n), start(n), end(n); n   for (long long int i = 0; i < n; i++) { n     cin >> a[i]; n     start[i] = i; n     end[i] = i + 1; n   } n   for (long long int i = 0; i < n; i++) { n     while (start[i] > 0 && (a[i] | a[start[i] - 1]) == a[i]) { n       start[i] = start[start[i] - 1]; n     } n   } n   for (long long int i = n - 1; i >= 0; i--) { n     while (end[i] < n && (a[i] | a[end[i]]) == a[i]) { n       end[i] = end[end[i]]; n     } n   } n   map<long long int, long long int> last; n   long long int good = (n * (n - 1)) / 2; n   for (long long int i = 0; i < n; i++) { n     if (last.count(a[i]) && last[a[i]] >= start[i]) { n       good -= (i - last[a[i]]) * (end[i] - i) - 1; n       last[a[i]] = i; n       continue; n     } n     last[a[i]] = i; n     good -= (i - start[i] + 1) * (end[i] - i) - 1; n   } n   cout << good << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int T, a, b, c, d, k; n int main() { n   cin >> T; n   for (; T--;) { n     cin >> a >> b >> c >> d >> k; n     int sol1 = 0; n     int sol2 = 0; n     if (a % c == 0) n       sol1 = a / c; n     else n       sol1 = a / c + 1; n     if (b % d == 0) n       sol2 = b / d; n     else n       sol2 = b / d + 1; n     if (sol1 + sol2 <= k) n       cout << sol1 <<     << sol2 <<   n ; n     else n       cout << -1 <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 1005; n const int mod1 = 998244353; n const int mod2 = 1e9 + 7; n const int b1 = 337; n const int b2 = 421; n const int inf = 2147483647; n struct Pair { n   int x[2] = {}; n   bool operator<(const Pair a) const { n     return x[0] < a.x[0] || x[0] == a.x[0] && x[1] < a.x[1]; n   } n }; n struct BitSet { n   unsigned long long b[16] = {}; n   inline void reset() { memset(b, 0, sizeof(b)); } n   inline void set(int x) { b[x / 64] |= (1ull << (x % 64)); } n   inline void operator^=(const BitSet &a) { n     for (int i = 0; i < 16; ++i) b[i] ^= a.b[i]; n   } n   inline Pair getHashVal() { n     Pair ans; n     for (int i = 0; i < 16; ++i) { n       ans.x[0] = (1ll * ans.x[0] * b1 % mod1 + b[i] % mod1) % mod1; n       ans.x[1] = (1ll * ans.x[1] * b2 % mod2 + b[i] % mod2) % mod2; n     } n     return ans; n   } n }; n int n, s, d; n BitSet bs[31], sta[1 << 20], cur; n int cnt[1 << 20], m; n map<Pair, int> mp; n void init() { n   m = min(s, 20); n   for (int i = 0; i < m; ++i) { n     cnt[1 << i] = 1; n     sta[1 << i] = bs[i]; n   } n   for (int i = 0; i < (1 << m); ++i) { n     if (i != (i & -i)) { n       cnt[i] = cnt[i - (i & -i)] + 1; n       sta[i] = sta[i - (i & -i)]; n       sta[i] ^= sta[i & -i]; n     } n     Pair x = sta[i].getHashVal(); n     if (!mp.count(x) || mp[x] > cnt[i]) { n       mp[x] = cnt[i]; n     } n   } n   for (int i = 0; i < (s - m); ++i) { n     sta[1 << i] = bs[i + m]; n   } n   for (int i = 1; i < (1 << (s - m)); ++i) { n     if (i == (i & -i)) continue; n     sta[i] = sta[i - (i & -i)]; n     sta[i] ^= sta[i & -i]; n   } n } n int getans() { n   BitSet now; n   int ans = inf; n   for (int i = 0; i < (1 << (s - m)); ++i) { n     if (cnt[i] >= ans) continue; n     now = cur, now ^= sta[i]; n     Pair x = now.getHashVal(); n     if (mp.count(x) && mp[x] + cnt[i] < ans) { n       ans = mp[x] + cnt[i]; n     } n   } n   return ans == inf ? -1 : ans; n } n int main() { n   scanf( %d%d%d , &n, &s, &d); n   for (int i = 0; i < s; ++i) { n     int c; n     scanf( %d , &c); n     for (int j = 0; j < c; ++j) { n       int u; n       scanf( %d , &u); n       u--; n       bs[i].set(u); n     } n   } n   init(); n   vector<int> ans; n   for (int i = 0; i < d; ++i) { n     int t; n     scanf( %d , &t); n     cur.reset(); n     while (t--) { n       int u; n       scanf( %d , &u); n       u--; n       cur.set(u); n     } n     ans.push_back(getans()); n   } n   for (int i = 0; i < ans.size(); ++i) printf( %d n , ans[i]); n   puts(  ); n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 400010; n const int P = 10000007; n int m, n; n char str[N]; n char ans[N]; n int l[N], r[N]; n int p[N]; n bool ok(int i, int m) { n   if (++i < m) return 0; n   return (r[i] - r[i - m]) == ((l[i] - l[i - m] * p[m]) * p[i - m]); n } n int dfs(int i, int ismax) { n   if (i == n) { n     puts(ans); n     return 1; n   } n   for (ans[i] = ismax ? str[i] :  a ; ans[i] <=  z ; ans[i]++) { n     l[i + 1] = l[i] * P + ans[i]; n     r[i + 1] = r[i] + ans[i] * p[i]; n     if (!ok(i, m) && !ok(i, m + 1) && dfs(i + 1, ans[i] == str[i] && ismax)) n       return 1; n   } n   return 0; n } n int main() { n   scanf( %d%s , &m, str); n   n = strlen(str); n   int i = n - 1; n   for (; i >= 0 && str[i] ==  z ; i--) str[i] =  a ; n   if (i < 0) { n     puts( Impossible ); n     return 0; n   } n   str[i]++; n   p[0] = 1; n   for (int i = 1; i <= n; i++) p[i] = p[i - 1] * P; n   if (!dfs(0, 1)) puts( Impossible ); n   return 0; n } n


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


#include <bits/stdc++.h> n using namespace std; n enum STATE { IN, OUT, BOUNDRY }; n const double PI = acos(-1.0); n void fast_in_out() { n   std::ios_base::sync_with_stdio(NULL); n   cin.tie(NULL); n   cout.tie(NULL); n } n int dx[] = {0, 0, -1, 1, -1, 1, -1, 1, 0}; n int dy[] = {-1, 1, 0, 0, -1, -1, 1, 1, 0}; n int lx[] = {2, 2, -1, 1, -2, -2, -1, 1}; n int ly[] = {-1, 1, 2, 2, 1, -1, -2, -2}; n const int N = 19, M = 200 + 10, mod = 998244353; n const double eps = 1e-9; n long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; } n int n; n double ar[N][N]; n double dp[N][(1 << N)]; n int main() { n   fast_in_out(); n   cout << fixed << setprecision(9); n   cin >> n; n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < n; j++) cin >> ar[i][j]; n     dp[i][1 << i] = 1; n   } n   for (int mask = 3; mask < (1 << n); mask++) { n     int cnt = __builtin_popcount(mask); n     if (cnt == 1) continue; n     cnt = cnt * (cnt - 1) / 2; n     double lose[N]{0}; n     for (int i = 0; i < n; i++) { n       for (int j = i + 1; j < n && (mask & (1 << i)); j++) { n         if (mask & (1 << j)) { n           lose[j] += ar[i][j] / cnt; n           lose[i] += ar[j][i] / cnt; n         } n       } n     } n     for (int i = 0; i < n; i++) { n       for (int j = i + 1; j < n && (mask & (1 << i)); j++) { n         if (mask & (1 << j)) { n           dp[j][mask] += dp[j][mask - (1 << i)] * lose[i]; n           dp[i][mask] += dp[i][mask - (1 << j)] * lose[j]; n         } n       } n     } n   } n   for (int i = 0; i < n; i++) cout << dp[i][(1 << n) - 1] <<    ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct __timestamper {}; n vector<vector<int>> g; n vector<vector<int>> id; n vector<int> used; n vector<int> ts; n void dfs(int v, int x) { n   if (used[v]) return; n   used[v] = 1; n   for (int i = 0; i < (int)g[v].size(); i++) { n     if (id[v][i] < x) { n       dfs(g[v][i], x); n     } n   } n   ts.push_back(v); n } n bool check(int x) { n   ts.clear(); n   used = vector<int>(g.size()); n   for (int i = 0; i < (int)g.size(); i++) { n     dfs(i, x); n   } n   reverse(ts.begin(), ts.end()); n   for (int i = 0; i + 1 < (int)ts.size(); i++) { n     int v = ts[i]; n     bool ok = false; n     for (int j = 0; j < (int)g[v].size(); j++) n       if (g[v][j] == ts[i + 1] && id[v][j] < x) { n         ok = true; n       } n     if (!ok) return false; n   } n   return true; n } n int main() { n   int n, m; n   scanf( %d%d , &n, &m); n   g = vector<vector<int>>(n); n   id = vector<vector<int>>(n); n   for (int i = 0; i < m; i++) { n     int a, b; n     scanf( %d%d , &a, &b); n     --a, --b; n     g[a].push_back(b); n     id[a].push_back(i); n   } n   if (!check(m)) { n     printf( -1 n ); n     return 0; n   } n   int l = 0; n   int r = m; n   while (r - l > 1) { n     int mid = (l + r) / 2; n     if (check(mid)) n       r = mid; n     else n       l = mid; n   } n   printf( %d n , r); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 2e5 + 5; n char str1[maxn], str2[maxn]; n int a[maxn], len1, len2; n bool book[maxn]; n bool judge(int x) { n   for (int i = 1; i <= x; i++) book[a[i] - 1] = 1; n   int j = 0, cnt = 0; n   for (int i = 0; i < len2; i++) { n     for (; j < len1; j++) { n       if (str2[i] == str1[j] && !book[j]) { n         j++; n         cnt++; n         break; n       } n     } n   } n   return cnt == len2; n } n int main(void) { n   while (~scanf(  %s %s , str1, str2)) { n     len1 = strlen(str1); n     len2 = strlen(str2); n     for (int i = 1; i <= len1; i++) scanf( %d , &a[i]); n     int l = 0, r = len1, ans; n     while (l <= r) { n       memset(book, 0, sizeof(book)); n       int mid = (l + r) / 2; n       if (judge(mid)) n         l = mid + 1, ans = mid; n       else n         r = mid - 1; n     } n     printf( %d n , ans); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int q; n   scanf( %d , &q); n   while (q--) { n     int l, r, d; n     scanf( %d %d %d , &l, &r, &d); n     if (d < l) n       printf( %d n , d); n     else if (d > r) n       printf( %d n , d); n     else { n       int res = r - d; n       res = res % d; n       printf( %d n , r + d - res); n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n map<int, int> mp; n int main() { n   int n; n   scanf( %d , &n); n   for (int i = 0; i < n; i++) { n     int a; n     scanf( %d , &a); n     mp[a]++; n   } n   int res = n; n   while (!mp.empty()) { n     for (auto itr = mp.begin(); itr != mp.end();) { n       if (--itr->second == 0) n         mp.erase(itr++); n       else n         itr++; n     } n     res--; n   } n   printf( %d n , res); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 200001; n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   int n, k; n   cin >> n >> k; n   string s; n   cin >> s; n   int ans = 2e9; n   vector<int> can; n   vector<string> cand; n   for (int i = 0; i < 10; i++) { n     priority_queue<int> pq; n     int t = 0; n     for (int j = 0; j < s.length(); j++) { n       int alpha = s[j] -  0 ; n       int cost = abs(alpha - i); n       pq.push(-cost); n     } n     for (int j = 0; j < k; j++) { n       int cst = -pq.top(); n       pq.pop(); n       t += cst; n     } n     if (ans > t) { n       can.clear(); n       can.push_back(i); n       ans = t; n     } else if (ans == t) { n       can.push_back(i); n     } n   } n   for (int d = 0; d < can.size(); d++) { n     string temp = s; n     int num = can[d]; n     int need = k; n     vector<pair<int, int> > big; n     vector<pair<int, int> > small; n     vector<pair<int, int> > ord; n     int cnt[10]; n     memset(cnt, 0, sizeof(cnt)); n     priority_queue<int> pq; n     for (int j = 0; j < s.size(); j++) { n       int cst = abs(num - (s[j] -  0 )); n       if (cst) pq.push(-cst); n       if (s[j] -  0  == num) need--; n       if (s[j] -  0  > num) big.push_back({s[j] -  0 , j}); n       if (s[j] -  0  < num) small.push_back({s[j] -  0 , j}); n     } n     reverse(small.begin(), small.end()); n     for (int i = 0; i < big.size(); i++) { n       int cost = abs(big[i].first - num); n       ord.push_back({cost, big[i].second}); n     } n     for (int i = 0; i < small.size(); i++) { n       int cost = abs(small[i].first - num); n       ord.push_back({cost, small[i].second}); n     } n     if (need <= 0) { n       cout << 0 << endl << s; n       exit(0); n     } n     for (int i = 0; i < need; i++) { n       int cst = -pq.top(); n       pq.pop(); n       cnt[cst]++; n     } n     for (int i = 0; i < ord.size(); i++) { n       int cst = ord[i].first; n       int idx = ord[i].second; n       if (cnt[cst]) { n         cnt[cst]--; n         temp[idx] = (char)(num +  0 ); n       } n     } n     cand.push_back(temp); n   } n   cout << ans <<   n ; n   sort(cand.begin(), cand.end()); n   cout << cand[0]; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<int> aa; n char s[100005]; n int n; n void doit() { n   scanf( %s , s + 1); n   n = strlen(s + 1); n   int last = 0, ans = 0; n   for (int i = 1; i <= n; i++) n     if (s[i] ==  1 ) { n       if (last > 0) ans += i - last - 1; n       last = i; n     } n   printf( %d n , ans); n } n int main() { n   int cas; n   scanf( %d , &cas); n   while (cas--) doit(); n } n