#include <bits/stdc++.h> n using namespace std; n using int64 = long long; n using ld = double; n const int MAXN = 1e5 + 1; n vector<int> G[MAXN], comp[MAXN], suf_freq[MAXN]; n vector<int64> suf_sum[MAXN]; n int comp_cnt, comp_ind[MAXN], p[MAXN], dp1[MAXN], dp2[MAXN], dp[MAXN], n     diameter[MAXN]; n void max_self(int &a, int b) { a = max(a, b); } n void dfs1(int u) { n   comp_ind[u] = comp_cnt; n   comp[comp_cnt].emplace_back(u); n   for (int v : G[u]) n     if (!comp_ind[v]) { n       p[v] = u; n       dfs1(v); n       if (dp1[u] <= dp1[v] + 1) { n         dp2[u] = dp1[u]; n         dp1[u] = dp1[v] + 1; n       } else n         max_self(dp2[u], dp1[v] + 1); n     } n } n void dfs2(int u, int dist) { n   dp[u] = max(dp1[u], dist); n   max_self(diameter[comp_cnt], dp[u]); n   for (int v : G[u]) n     if (v != p[u]) { n       if (dp1[u] == dp1[v] + 1) n         dfs2(v, max(dp2[u], dist) + 1); n       else n         dfs2(v, max(dp1[u], dist) + 1); n     } n } n ld solve(int x, int y) { n   if (x == y) return -1; n   if (comp[x].size() > comp[y].size()) swap(x, y); n   int D = max(diameter[x], diameter[y]); n   ld sum = 0; n   for (int d = 0; d <= diameter[x]; ++d) { n     int64 freq = suf_freq[x][d]; n     if (d < diameter[x]) freq -= suf_freq[x][d + 1]; n     if (freq == 0) continue; n     int suf = max(0, min(D - d - 1, diameter[y])); n     sum += freq * suf_sum[y][suf]; n     sum += freq * suf_freq[y][suf] * (d + 1); n     int rest = comp[y].size() - suf_freq[y][suf]; n     sum += freq * rest * D; n   } n   return sum / ((ld)comp[x].size() * comp[y].size()); n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(nullptr); n   cout.tie(nullptr); n   int N, M, Q; n   cin >> N >> M >> Q; n   for (int i = 0; i < M; ++i) { n     int u, v; n     cin >> u >> v; n     G[u].emplace_back(v); n     G[v].emplace_back(u); n   } n   for (int u = 1; u <= N; ++u) n     if (!comp_ind[u]) { n       ++comp_cnt; n       dfs1(u); n       dfs2(u, 0); n       suf_sum[comp_cnt] = vector<int64>(diameter[comp_cnt] + 1); n       suf_freq[comp_cnt] = vector<int>(diameter[comp_cnt] + 1); n       for (int v : comp[comp_cnt]) { n         suf_sum[comp_cnt][dp[v]] += dp[v]; n         ++suf_freq[comp_cnt][dp[v]]; n       } n       for (int d = diameter[comp_cnt] - 1; d >= 0; --d) { n         suf_sum[comp_cnt][d] += suf_sum[comp_cnt][d + 1]; n         suf_freq[comp_cnt][d] += suf_freq[comp_cnt][d + 1]; n       } n     } n   map<pair<int, int>, ld> sol; n   cout << fixed << setprecision(8); n   for (int q = 0; q < Q; ++q) { n     int u, v; n     cin >> u >> v; n     u = comp_ind[u], v = comp_ind[v]; n     if (u > v) swap(u, v); n     pair<int, int> p{u, v}; n     auto it = sol.find(p); n     if (it != sol.end()) n       cout << it->second <<   n ; n     else { n       ld ans = solve(u, v); n       sol[p] = ans; n       cout << ans <<   n ; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 300010; n const long long inf = 4e18; n int n, k, x1[N], var[N], x2[N], y2[N], x[N], y[N]; n pair<pair<int, int>, int> px[N], py[N], py1[N]; n long long ans; n long long solve(pair<pair<int, int>, int> *p, int n, int k, int w) { n   int j = n - k; n   long long res = inf; n   for (int i = 1; i <= k + 1; i++) { n     int yl = p[i].first.first; n     int yr = p[j].first.first; n     int h = yr - yl; n     if (h & 1) { n       h >>= 1; n       h++; n     } else { n       h >>= 1; n     } n     if (h == 0) h++; n     res = min(res, 1ll * w * h); n     j++; n   } n   return res; n } n void remove(pair<pair<int, int>, int> *p, int n, int val) { n   int j = -1; n   for (int i = 1; i <= n; i++) n     if (p[i].second == val) { n       j = i; n       break; n     } n   for (int i = j; i < n; i++) p[i] = p[i + 1]; n } n int main() { n   scanf( %d%d , &n, &k); n   for (int i = 1; i <= n; i++) n     scanf( %d%d%d%d , &x1[i], &var[i], &x2[i], &y2[i]); n   for (int i = 1; i <= n; i++) { n     x[i] = x1[i] + x2[i]; n     y[i] = var[i] + y2[i]; n   } n   for (int i = 1; i <= n; i++) py[i] = make_pair(make_pair(y[i], x[i]), i); n   sort(py + 1, py + n + 1); n   for (int i = 1; i <= n; i++) px[i] = make_pair(make_pair(x[i], y[i]), i); n   sort(px + 1, px + n + 1); n   int n1 = n; n   ans = inf; n   for (int i = 1; i <= k + 1; i++) { n     int n2 = n1; n     for (int j = 1; j <= n2; j++) py1[j] = py[j]; n     for (int j = n; j >= n - (k - (i - 1)); j--) { n       int xl = px[i].first.first; n       int xr = px[j].first.first; n       int w = xr - xl; n       if (w & 1) { n         w >>= 1; n         w++; n       } else { n         w >>= 1; n       } n       if (w == 0) w++; n       ans = min(ans, solve(py1, n2, k - (i - 1) - (n - j), w)); n       remove(py1, n2, px[j].second); n       n2--; n     } n     remove(py, n1, px[i].second); n     n1--; n   } n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n string s; n string str[] = { 012345 ,  041235 ,  034125 ,  023415 ,  104523 ,  120453 , n                  152043 ,  145203 ,  215304 ,  201534 ,  230154 ,  253014 }; n bool solve(string x, string y) { n   for (int i = 0; i < 12; i++) { n     string t1; n     for (int j = 0; j < 6; j++) { n       t1 += x[str[i][j] -  0 ]; n     } n     if (t1 == y) return true; n     string rev = str[i]; n     reverse(rev.begin(), rev.end()); n     string t2; n     for (int j = 0; j < 6; j++) { n       t2 += x[rev[j] -  0 ]; n     } n     if (t2 == y) return true; n   } n   return false; n } n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cin >> s; n   sort(s.begin(), s.end()); n   vector<string> v; n   do { n     bool flag = false; n     for (int i = 0; i < v.size(); i++) { n       if (solve(s, v[i])) { n         flag = true; n       } n     } n     if (!flag) v.push_back(s); n   } while (next_permutation(s.begin(), s.end())); n   cout << v.size() << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e5 + 5; n map<pair<int, int>, int> rel; n vector<int> x, y, color[4]; n int pos[N], block[N]; n char s[N]; n inline int read() { n   int x = 0, f = 1; n   char ch = getchar(); n   for (; ch <  0  || ch >  9 ; ch = getchar()) n     if (ch ==  - ) f = -1; n   for (; ch >=  0  && ch <=  9 ; ch = getchar()) n     x = (x << 1) + (x << 3) + ch -  0 ; n   return x * f; n } n void query() { n   if (x.size() == 0) return; n   printf( Q %d  , x.size()); n   for (int i = 0, len = x.size(); i < len; ++i) printf( %d %d  , x[i], y[i]); n   puts(  ); n   fflush(stdout); n   scanf( %s , s); n   for (int i = 0, len = x.size(); i < len; ++i) { n     rel[make_pair(x[i], y[i])] = s[i] -  0 ; n     rel[make_pair(y[i], x[i])] = s[i] -  0 ; n   } n   x.clear(); n   y.clear(); n } n void answer() { n   printf( A  ); n   for (int i = 1; i <= 3; ++i) printf( %d  , color[i].size()); n   puts(  ); n   for (int i = 1; i <= 3; ++i) { n     for (int j = 0, len = color[i].size(); j < len; ++j) n       printf( %d  , color[i][j]); n     puts(  ); n   } n   fflush(stdout); n } n int main() { n   int T = read(); n   while (T--) { n     int n = read(); n     rel.clear(); n     memset(pos, 0, sizeof pos); n     memset(block, 0, sizeof block); n     for (int i = 1; i < n; i += 2) { n       x.push_back(i); n       y.push_back(i + 1); n     } n     query(); n     for (int i = 2; i < n; i += 2) { n       x.push_back(i); n       y.push_back(i + 1); n     } n     query(); n     int tot = pos[1] = 1; n     for (int i = 2; i <= n; ++i) n       if (!rel[make_pair(i - 1, i)]) pos[++tot] = i; n     for (int i = 1; i < tot - 1; i += 4) { n       x.push_back(pos[i]); n       y.push_back(pos[i + 2]); n     } n     for (int i = 2; i < tot - 1; i += 4) { n       x.push_back(pos[i]); n       y.push_back(pos[i + 2]); n     } n     query(); n     for (int i = 3; i < tot - 1; i += 4) { n       x.push_back(pos[i]); n       y.push_back(pos[i + 2]); n     } n     for (int i = 4; i < tot - 1; i += 4) { n       x.push_back(pos[i]); n       y.push_back(pos[i + 2]); n     } n     query(); n     block[1] = 1; n     block[2] = tot > 1 ? 2 : 0; n     for (int i = 3; i <= tot; ++i) { n       if (rel[make_pair(pos[i], pos[i - 2])]) n         block[i] = block[i - 2]; n       else n         block[i] = 6 - block[i - 1] - block[i - 2]; n     } n     for (int i = 1; i <= 3; ++i) color[i].clear(); n     for (int i = 1, j = 1; i <= tot; ++i) n       while (j ^ pos[i + 1] && j <= n) color[block[i]].push_back(j++); n     answer(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int f(int a, int b) { n   int ans = 1; n   for (int i = 0; i < b; i++) ans *= a; n   return ans; n } n int main() { n   int a, b, sgn, n, ans = 1999; n   scanf( %d %d %d , &a, &b, &n); n   sgn = (a < 0) ^ (b < 0); n   a = abs(a); n   b = abs(b); n   if (sgn == 1 && n % 2 == 0 && b != 0) n     printf( No solution n ); n   else if (a == 0 && b == 0) n     printf( 0 n ); n   else if (a == 0) n     printf( No solution n ); n   else if (b % a != 0) n     printf( No solution n ); n   else { n     b /= a; n     for (int i = 0;; i++) { n       int k = f(i, n); n       if (k == b) { n         ans = i * (sgn == 1 ? -1 : 1); n         break; n       } n       if (k > b) { n         printf( No solution n ); n         break; n       } n     } n     if (ans != 1999) printf( %d n , ans); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int mod; n struct EDGE { n   int to, nxt; n } box[101000 * 2]; n int h[101000], top, f1[101000], f2[101000]; n void add(int u, int v) { n   box[top].to = v; n   box[top].nxt = h[u]; n   h[u] = top++; n } n int dp[610][610]; n int dfs(int a, int b) { n   if (dp[a][b] + 1) return dp[a][b]; n   int p = h[a]; n   dp[a][b] = 0; n   while (p != -1) { n     dp[a][b] = (dp[a][b] + dfs(box[p].to, b)) % mod; n     p = box[p].nxt; n   } n   return dp[a][b]; n } n long long ni(long long i) { n   int n = mod - 2; n   long long res = 1; n   while (n) { n     if (n & 1) res = res * i % mod; n     i = i * i % mod; n     n >>= 1; n   } n   return res; n } n int ss[610], sn[610], c1, c2; n int mat[610][610]; n int main() { n   int n, m; n   scanf( %d%d%d , &n, &m, &mod); n   memset(dp, -1, sizeof(dp)); n   memset(h, -1, sizeof(h)); n   for (int i = 1; i <= m; i++) { n     int a, b; n     scanf( %d%d , &a, &b); n     f1[a] = 1; n     f2[b] = 1; n     add(a, b); n   } n   for (int i = 1; i <= n; i++) dp[i][i] = 1; n   for (int i = 1; i <= n; i++) { n     for (int j = 1; j <= n; j++) { n       if (dp[i][j] == -1) dfs(i, j); n     } n   } n   for (int i = 1; i <= n; i++) { n     if (!f1[i]) sn[c1++] = i; n     if (!f2[i]) ss[c2++] = i; n   } n   for (int i = 0; i < c1; i++) { n     for (int j = 0; j < c1; j++) { n       mat[i][j] = dp[ss[i]][sn[j]]; n     } n   } n   long long ans = 1; n   for (int j = 0; j < c1; j++) { n     int r; n     for (r = j; r < c1; r++) { n       if (mat[r][j]) break; n     } n     if (r == c1) { n       ans = 0; n       break; n     } n     for (int k = j; k < c1; k++) swap(mat[j][k], mat[r][k]); n     if (r != j) ans = -ans; n     for (int i = 0; i < c1; i++) { n       if (i == j) continue; n       long long a = mat[i][j], b = mat[j][j]; n       for (int k = 0; k < c1; k++) { n         mat[i][k] = (mat[i][k] * b - mat[j][k] * a) % mod; n       } n       ans = ans * ni(b) % mod; n     } n   } n   for (int i = 0; i < c1; i++) ans = (ans * mat[i][i]) % mod; n   printf( %I64d n , (ans + mod) % mod); n } n


#include <bits/stdc++.h> n using namespace std; n vector<int> adj[100], kk; n int n, m, c1, c2, vb[100], cc, ff; n void dfs(int x) { n   c1++; n   vb[x] = 1; n   c2 += adj[x].size(); n   for (int u : adj[x]) { n     if (!vb[u]) dfs(u); n   } n   return; n } n bool cmp(int u, int v) { n   memset(vb, 0, sizeof(vb)); n   dfs(u); n   return (!vb[v]); n } n int main() { n   cin >> n >> m; n   for (int i = 1; i <= m; i++) { n     int x, y; n     cin >> x >> y; n     adj[x].push_back(y); n     adj[y].push_back(x); n   } n   for (int i = 1; i <= n; i++) { n     if (adj[i].size() > 2) { n       cout <<  NO  << endl; n       return 0; n     } n   } n   for (int i = 1; i <= n; i++) { n     if (!vb[i]) { n       c1 = c2 = 0; n       dfs(i); n       if (c1 * 2 == c2) cc = 1; n       ff++; n     } n   } n   if (cc && ff > 1) { n     cout <<  NO  << endl; n     return 0; n   } n   cout <<  YES  << endl; n   cout << n - m << endl; n   if (n == 1 && m == 0) { n     cout <<  1 1  << endl; n     return 0; n   } n   while (m < n) { n     for (int i = 1; i <= n; i++) n       if (adj[i].size() < 2) kk.push_back(i); n     if (m == n - 1 && kk.size() == 2) { n       cout << kk[0] <<     << kk[1] << endl; n       return 0; n     } n     for (int i = 1; i < kk.size(); i++) { n       if (cmp(kk[0], kk[i])) { n         cout << kk[0] <<     << kk[i] << endl; n         adj[kk[0]].push_back(kk[i]); n         adj[kk[i]].push_back(kk[0]); n         break; n       } n     } n     kk.clear(); n     m++; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long int n, m, a = 0; n   cin >> n >> m; n   for (int i = 0; i < n; i++) { n     long long int t = 100000000000; n     for (int j = 0; j < m; j++) { n       long long int r; n       cin >> r; n       t = min(t, r); n     } n     a = max(t, a); n   } n   cout << a; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long x[9999999]; n int a = 0, b, c, n; n int bbb(int iw, int zw, long long t) { n   if (t > x[zw] || t < x[iw]) return 0; n   while (zw >= iw) { n     int qccc = (iw + zw) >> 1; n     if (t < x[qccc]) n       zw = qccc - 1; n     else if (x[qccc] < t) n       iw = qccc + 1; n     else n       return 1; n   } n   return 0; n } n int checkk(int r) { n   for (int i = 32; i >= 0; i--) { n     long long qwe = (1LL << i); n     if (qwe <= x[r]) return 0; n     if (bbb(1, r - 1, qwe - x[r]) || bbb(r + 1, n, qwe - x[r])) return 1; n   } n   return 0; n } n int main() { n   scanf( %d , &n); n   for (int i = 1; i < n + 1; i++) { n     scanf( %lld , &x[i]); n   } n   sort(x + 1, x + n + 1); n   for (int i = 1; i <= n; i++) { n     if (!checkk(i)) a++; n   } n   printf( %d , a); n   return 0; n } n


#include<bits/stdc++.h> n using namespace std; n  n namespace program { n  const int N = 100005; n   n  struct edge { n   int to, nxt; n   edge() {} n   edge(int to_, int nxt_): to(to_), nxt(nxt_) {} n  } e[N << 1]; n  int hd[N], cnt, fir[N], end[N]; n  vector<pair<int, int> > del, ins; n   n  void adde(int u, int v) { n   e[++cnt] = edge(v, hd[u]); hd[u] = cnt; n   e[++cnt] = edge(u, hd[v]); hd[v] = cnt; n  } n  bool dfs(int u, int fa) { n   vector<int> son1, son2; n   for(int i = hd[u]; i; i = e[i].nxt) { n    int v = e[i].to; n    if(v == fa) continue; n    if(dfs(v, u)) { n     son1.push_back(v); n    } n    else { n     son2.push_back(v); n    } n   } n   if(!son1.size()) { n    end[u] = u; n   } n   else if(son1.size() == 1) { n    end[u] = end[son1[0]]; n   } n   else { n    end[u] = end[son1[0]]; n    fir[u] = end[son1[1]]; n    if(fa) del.push_back(make_pair(fa, u)); n    for(int i = 2; i < son1.size(); ++i) { n     del.push_back(make_pair(u, son1[i])); n     ins.push_back(make_pair(end[u], son1[i])); n     end[u] = end[son1[i]]; n    } n   } n   for(int i = 0; i < son2.size(); ++i) { n    ins.push_back(make_pair(end[u], fir[son2[i]])); n    end[u] = end[son2[i]]; n   } n   return son1.size() <= 1; n  } n  int main() { n   int T; n   scanf( %d , &T); n   while(T--) { n    int n; n    scanf( %d , &n); n    cnt = 0; n    memset(hd + 1, 0, sizeof(int) * n); n    for(int i = 1; i < n; ++i) { n     int u, v; n     scanf( %d%d , &u, &v); n     adde(u, v); n    } n    del.clear(); n    ins.clear(); n    dfs(1, 0); n    printf( %d n , del.size()); n    for(int i = 0; i < del.size(); ++i) { n     printf( %d %d %d %d n , del[i].first, del[i].second, ins[i].first, ins[i].second); n    } n   } n   return 0; n  } n } n int main() { n  return program::main(); n }


#include <bits/stdc++.h> n using namespace std; n const int maxn = 100000 + 10; n double power(double x, int n) { n   if (n <= 1) return n ? x : 1; n   double t = power(x, n / 2); n   if (n % 2) n     return t * t * x; n   else n     return t * t; n } n double p[maxn]; n int main() { n   int m, n; n   scanf( %d%d , &m, &n); n   for (int i = 1; i <= m; i++) p[i] = power(i * 1.0 / m, n); n   for (int i = m; i >= 1; i--) p[i] -= p[i - 1]; n   double ans = 0; n   for (int i = 1; i <= m; i++) ans += i * p[i]; n   printf( %.15f n , ans); n } n


#include <bits/stdc++.h> n using namespace std; n pair<int, int> dir[4]; n int ans = 0; n int n, m; n char maz[55][55]; n char op[105]; n void bfs() { n   int x, y; n   for (int i = 1; i <= n; ++i) { n     for (int j = 1; j <= m; ++j) { n       if (maz[i][j] ==  S ) { n         x = i, y = j; n       } n     } n   } n   for (int i = 0; op[i]; ++i) { n     int t = op[i] -  0 ; n     x += dir[t].first; n     y += dir[t].second; n     if (x < 1 || y < 1 || x > n || y > m || maz[x][y] ==  # ) return; n     if (maz[x][y] ==  E ) { n       ans++; n       return; n     } n   } n } n int main() { n   cin >> n >> m; n   for (int i = 1; i <= n; ++i) { n     scanf( %s , maz[i] + 1); n   } n   scanf( %s , op); n   dir[0] = pair<int, int>(0, 1); n   dir[1] = pair<int, int>(0, -1); n   dir[2] = pair<int, int>(1, 0); n   dir[3] = pair<int, int>(-1, 0); n   sort(dir, dir + 4); n   do { n     bfs(); n   } while (next_permutation(dir, dir + 4)); n   cout << ans << endl; n } n


#include <bits/stdc++.h> n using namespace std; n const long double pi = acos(-1); n const int n_ = 1e5 + 5; n int n; n long long a[n_], b[n_], dp[n_]; n vector<int> st; n vector<long double> p; n long double getIntersect(int i, int j) { n   return (dp[j] - dp[i]) / (long double)(b[i] - b[j]); n } n int main() { n   scanf( %d , &n); n   for (int i = 0; i < n; i++) scanf( %lld , a + i); n   for (int i = 0; i < n; i++) scanf( %lld , b + i); n   dp[0] = b[0]; n   st.push_back(0); n   p.push_back(2e9); n   int id; n   for (int i = 1; i < n; i++) { n     id = lower_bound(p.begin(), p.end(), (long double)a[i] - 1) - p.begin(); n     dp[i] = dp[st[id]] + b[st[id]] * (a[i] - 1) + b[i]; n     while (st.size() > 1 && getIntersect(st[st.size() - 2], st.back()) > n                                 getIntersect(st.back(), i)) { n       st.pop_back(); n       p.pop_back(); n     } n     p.back() = getIntersect(st.back(), i); n     p.push_back(2e9); n     st.push_back(i); n   } n   printf( %lld , dp[n - 1]); 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   long long int n, m, flag = 0; n   cin >> n >> m; n   long long int a[n][m], b[n][m]; n   for (long long int(i) = 0; (i) < (n); ++(i)) { n     for (long long int(j) = 0; (j) < (m); ++(j)) { n       cin >> a[i][j]; n     } n   } n   for (long long int(i) = 0; (i) < (n); ++(i)) { n     for (long long int(j) = 0; (j) < (m); ++(j)) { n       cin >> b[i][j]; n     } n   } n   for (long long int(i) = 0; (i) < (n); ++(i)) { n     for (long long int(j) = 0; (j) < (m); ++(j)) { n       if (a[i][j] > b[i][j]) swap(a[i][j], b[i][j]); n     } n   } n   for (long long int(i) = 0; (i) < (n); ++(i)) { n     for (long long int(j) = 0; (j) < (m - 1); ++(j)) { n       if (a[i][j] >= a[i][j + 1]) { n         flag = 1; n       } n     } n   } n   for (long long int(j) = 0; (j) < (m); ++(j)) { n     for (long long int(i) = 0; (i) < (n - 1); ++(i)) { n       if (a[i][j] >= a[i + 1][j]) { n         flag = 1; n       } n     } n   } n   for (long long int(i) = 0; (i) < (n); ++(i)) { n     for (long long int(j) = 0; (j) < (m - 1); ++(j)) { n       if (b[i][j] >= b[i][j + 1]) { n         flag = 1; n       } n     } n   } n   for (long long int(j) = 0; (j) < (m); ++(j)) { n     for (long long int(i) = 0; (i) < (n - 1); ++(i)) { n       if (b[i][j] >= b[i + 1][j]) { n         flag = 1; n       } n     } n   } n   if (flag == 0) n     cout <<  Possible ; n   else n     cout <<  Impossible ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n char s[100500]; n char c[200100]; n int l[100100][26], r[100100][26]; n int last[26]; n int main() { n   long long res; n   int tmp; n   int n, k, ll, l1, i, j; n   cin >> n >> k; n   scanf( %s , s); n   ll = strlen(s); n   memset(last, -1, sizeof(last)); n   for (i = 0; i < ll; i++) { n     last[s[i] -  a ] = i; n     for (j = 0; j < 26; j++) l[i][j] = last[j]; n   } n   memset(last, -1, sizeof(last)); n   for (i = ll - 1; i >= 0; i--) { n     last[s[i] -  a ] = i; n     for (j = 0; j < 26; j++) r[i][j] = last[j]; n   } n   for (int cnt = 0; cnt < n; cnt++) { n     scanf( %s , c); n     l1 = strlen(c); n     res = 0; n     for (j = 0; j < l1; j++) { n       tmp = 100500; n       if (l[min(j, ll - 1)][c[j] -  a ] != -1) n         tmp = abs(j - l[min(j, ll - 1)][c[j] -  a ]); n       if (r[min(j, ll - 1)][c[j] -  a ] != -1) n         tmp = min(tmp, abs(r[min(j, ll - 1)][c[j] -  a ] - j)); n       if (tmp == 100500) tmp = l1; n       res += tmp; n     } n     cout << res << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 2020; n int Arr[N][N]; n int Pre[N][N]; n int Add[N][N]; n bool Col[N]; n bool Row[N]; n int getSum(int x1, int y1, int x2, int y2) { n   return Pre[x2][y2] - Pre[x1 - 1][y2] - Pre[x2][y1 - 1] + Pre[x1 - 1][y1 - 1]; n } n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   int n; n   cin >> n; n   int k; n   cin >> k; n   for (int i = 1; i <= n; i++) n     for (int j = 1; j <= n; j++) { n       char ch; n       cin >> ch; n       Arr[i][j] = (ch ==  B ); n     } n   for (int i = 1; i <= n; i++) n     for (int j = 1; j <= n; j++) n       Pre[i][j] = Pre[i - 1][j] + Pre[i][j - 1] - Pre[i - 1][j - 1] + Arr[i][j]; n   int Total = 0; n   for (int i = 1; i <= n; i++) { n     if (!getSum(i, 1, i, n)) Row[i] = true, Total++; n     if (!getSum(1, i, n, i)) Col[i] = true, Total++; n   } n   for (int x = 1; x + k - 1 <= n; x++) { n     int Cur = Total; n     for (int i = 1; i < k; i++) n       if (!Col[i]) n         Cur += (getSum(1, i, n, i) - getSum(x, i, x + k - 1, i) == 0); n     for (int y = 1; y + k - 1 <= n; y++) { n       if (!Col[y + k - 1]) n         Cur += (getSum(1, y + k - 1, n, y + k - 1) - n                     getSum(x, y + k - 1, x + k - 1, y + k - 1) == n                 0); n       Add[x][y] += Cur; n       if (!Col[y]) n         Cur -= (getSum(1, y, n, y) - getSum(x, y, x + k - 1, y) == 0); n     } n   } n   for (int x = 1; x + k - 1 <= n; x++) { n     int Cur = 0; n     for (int i = 1; i < k; i++) n       if (!Row[i]) n         Cur += (getSum(i, 1, i, n) - getSum(i, x, i, x + k - 1) == 0); n     for (int y = 1; y + k - 1 <= n; y++) { n       if (!Row[y + k - 1]) n         Cur += (getSum(y + k - 1, 1, y + k - 1, n) - n                     getSum(y + k - 1, x, y + k - 1, x + k - 1) == n                 0); n       Add[y][x] += Cur; n       if (!Row[y]) n         Cur -= (getSum(y, 1, y, n) - getSum(y, x, y, x + k - 1) == 0); n     } n   } n   int Ans = 0; n   for (int i = 1; i <= n; i++) n     for (int j = 1; j <= n; j++) Ans = max(Ans, Add[i][j]); n   cout << Ans <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int X = 100; n char s[X][1000], a[15]; n int main() { n   int i = 0, j = 0, c = 0, x; n   memset(s,  . , sizeof(s)); n   for (x = 0; x < X; x++) s[x][999] = 0; n   scanf( %s , a); n   for (x = 0; a[x]; x++) { n     for (;;) { n       if (c == a[x]) { n         s[++i][0] =  X , j = 0; n         break; n       } else if (j) n         s[i][j] =  X , j += 2, c = (c + 255) % 256; n       else n         i += 2, s[i][j] =  X , j += 2, c = (c + 255) % 256; n     } n   } n   for (x = 0; x < X; x++) puts(s[x]); n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   cin.clear(); n   fflush(stdin); n   int n; n   cin >> n; n   while (n--) { n     string s; n     cin >> s; n     int year = stoi(s.substr(4, s.size() - 4)), p = 1, x = 1989; n     for (int i = 0; i < s.size() - 5; i++) { n       p *= 10; n       x += p; n     } n     p *= 10; n     while (year < x) { n       year += p; n     } n     cout << year << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct __s { n   __s() { n     if (1) { n       ios_base::Init i; n       cin.sync_with_stdio(0); n       cin.tie(0); n     } n   } n   ~__s() { n     if (!1) n       fprintf(stderr,  Execution time: %.3lf s. n , n               (double)clock() / CLOCKS_PER_SEC); n     long long n; n     cin >> n; n   } n } __S; n long long n, m; n long long x[55]; n long long y[55]; n long long first[55]; n long long second[55]; n long long I[55][55]; n long long J[55][55]; n bool u[55][55]; n bool up[55]; n bool ud[55]; n bool ok[55]; n vector<pair<pair<long long, long long>, pair<long long, long long> > > ans; n long long findUp() { n   long long idx = -1; n   for (long long i = 0; i < (long long)(m); i++) { n     if (up[i]) continue; n     if (idx == -1 || x[i] < x[idx] || (x[i] == x[idx] && y[i] < y[idx])) { n       idx = i; n     } n   } n   return idx; n } n long long findDown() { n   long long idx = -1; n   for (long long i = 0; i < (long long)(m); i++) { n     if (ud[i]) continue; n     if (idx == -1 || first[i] > first[idx] || n         (first[i] == first[idx] && second[i] < second[idx])) { n       idx = i; n     } n   } n   return idx; n } n void move(long long i, long long dx, long long dy) { n   u[x[i]][y[i]] = false; n   I[x[i]][y[i]] = -1; n   ans.push_back( n       make_pair(make_pair(x[i], y[i]), make_pair(x[i] + dx, y[i] + dy))); n   x[i] += dx; n   y[i] += dy; n   u[x[i]][y[i]] = true; n   I[x[i]][y[i]] = i; n } n void shift(long long i, long long dx, long long dy) { n   long long first = x[i] + dx; n   long long second = y[i] + dy; n   if (u[first][second]) shift(I[first][second], dx, dy); n   move(i, dx, dy); n } n int main(void) { n   cin >> n >> m; n   for (long long i = 0; i < (long long)(n); i++) n     for (long long j = 0; j < (long long)(n); j++) J[i][j] = -1; n   for (long long i = 0; i < (long long)(m); i++) { n     cin >> x[i] >> y[i]; n     x[i]--; n     y[i]--; n     u[x[i]][y[i]] = true; n     I[x[i]][y[i]] = i; n   } n   for (long long i = 0; i < (long long)(m); i++) { n     cin >> first[i] >> second[i]; n     first[i]--; n     second[i]--; n     J[first[i]][second[i]] = i; n   } n   for (long long i = 0; i < (long long)(m); i++) { n     long long idx = findUp(); n     while (x[idx] != 0 || y[idx] != i) { n       while (x[idx] != 0 && !u[x[idx] - 1][y[idx]]) { n         move(idx, -1, 0); n       } n       while (y[idx] != i) { n         while (y[idx] > i && !u[x[idx]][y[idx] - 1]) { n           move(idx, 0, -1); n         } n         while (y[idx] < i && !u[x[idx]][y[idx] + 1]) { n           move(idx, 0, 1); n         } n         if (y[idx] != i) { n           idx = I[x[idx]][y[idx] + 1]; n         } n       } n       while (x[idx] != 0 && !u[x[idx] - 1][y[idx]]) { n         move(idx, -1, 0); n       } n     } n     up[idx] = true; n   } n   for (long long i = 0; i < (long long)(m); i++) { n     long long idx = findDown(); n     ud[idx] = true; n     if (first[idx] < 2) continue; n     while (x[idx] != first[idx] || y[idx] != second[idx]) { n       while (x[idx] != first[idx] - 1) { n         move(idx, 1, 0); n       } n       while (y[idx] > second[idx]) { n         move(idx, 0, -1); n       } n       while (y[idx] < second[idx]) { n         move(idx, 0, 1); n       } n       move(idx, 1, 0); n     } n     ok[idx] = true; n   } n   vector<long long> w[2]; n   for (long long i = 0; i < (long long)(min(n, 2LL)); i++) { n     for (long long j = 0; j < (long long)(n); j++) { n       if (J[i][j] == -1) continue; n       w[i].push_back(J[i][j]); n     } n   } n   for (long long i = 0; i < (long long)(2); i++) { n     for (long long j = 1; j < w[i].size(); ++j) { n       if (y[w[i][j - 1]] < y[w[i][j]]) continue; n       move(w[i][j], 1, 0); n       while (y[w[i][j]] < y[w[i][j - 1]]) move(w[i][j], 0, 1); n       shift(w[i][j - 1], 0, -1); n       move(w[i][j], -1, 0); n     } n   } n   for (long long i = 0; i < (long long)(w[1].size()); i++) { n     move(w[1][i], 1, 0); n   } n   for (long long i = 0; i < (long long)(2); i++) { n     for (long long jj = 0; jj < (long long)(w[i].size()); jj++) { n       long long j = w[i].size() - 1 - jj; n       while (y[w[i][j]] != n - 1 - jj) move(w[i][j], 0, 1); n     } n     for (long long j = 0; j < (long long)(w[i].size()); j++) { n       while (y[w[i][j]] != second[w[i][j]]) move(w[i][j], 0, -1); n     } n   } n   cout << ans.size() <<   n ; n   for (long long i = 0; i < (long long)(ans.size()); i++) { n     cout << ans[i].first.first + 1 <<     << ans[i].first.second + 1 <<     n          << ans[i].second.first + 1 <<     << ans[i].second.second + 1 <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n char s[1000010]; n int max_pos_by_allowed_and_len[1 << 20][21]; n constexpr int full = (1 << 20) - 1; n template <typename T> n void mx(T& x, T y) { n   x = (x > y ? x : y); n } n void update(const int* __restrict__ a, int* __restrict__ b) { n   for (int i = 0; i < 21; i += 7) { n     mx(b[i + 0], a[i + 0]); n     mx(b[i + 1], a[i + 1]); n     mx(b[i + 2], a[i + 2]); n     mx(b[i + 3], a[i + 3]); n     mx(b[i + 4], a[i + 4]); n     mx(b[i + 5], a[i + 5]); n     mx(b[i + 6], a[i + 6]); n   } n } n int main() { n   scanf( %s , s); n   int n = strlen(s); n   for (int i = 0; i < n; ++i) s[i] -=  a ; n   for (int i = 1; i <= n; ++i) { n     int used = 0; n     for (int j = i; j > 0; --j) { n       if ((used >> s[j - 1]) & 1) break; n       used |= (1 << s[j - 1]); n       max_pos_by_allowed_and_len[full ^ used][i - j + 1] = i; n     } n   } n   for (int mask = full; mask > 0; --mask) { n     for (int bit = 0; bit < 20; ++bit) { n       int updated = mask ^ (1 << bit); n       if (updated > mask) continue; n       update(&max_pos_by_allowed_and_len[mask][0], n              &max_pos_by_allowed_and_len[updated][0]); n     } n   } n   int ans = 1; n   for (int i = 1; i <= n; ++i) { n     int used = 0; n     for (int j = i; j > 0; --j) { n       if ((used >> s[j - 1]) & 1) break; n       used |= (1 << s[j - 1]); n       for (int other_len = 20; other_len >= 0; --other_len) { n         if (max_pos_by_allowed_and_len[used][other_len] > i) { n           mx(ans, i - j + 1 + other_len); n           break; n         } n       } n     } n   } n   printf( %d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void qSort(long long **a, int first, int last) { n   long long i = first, j = last, x = a[(first + last) / 2][0]; n   do { n     while (a[i][0] < x) i++; n     while (a[j][0] > x) j--; n     if (i <= j) { n       if (i < j) { n         long long tmp0, tmp1; n         tmp0 = a[i][0]; n         tmp1 = a[i][1]; n         a[i][0] = a[j][0]; n         a[i][1] = a[j][1]; n         a[j][0] = tmp0; n         a[j][1] = tmp1; n       } n       i++; n       j--; n     } n   } while (i <= j); n   if (i < last) qSort(a, i, last); n   if (first < j) qSort(a, first, j); n } n void build(long long *t, long long *a, int v, int tl, int tr) { n   if (tl == tr) n     t[v] = max(a[tl], (long long)0); n   else { n     int tm = (tl + tr) / 2; n     build(t, a, v * 2, tl, tm); n     build(t, a, v * 2 + 1, tm + 1, tr); n     t[v] = max(t[v * 2], (long long)0) + max(t[v * 2 + 1], (long long)0); n   } n } n long long sum(long long *t, int v, int tl, int tr, int l, int r) { n   if (l > r) return 0; n   if (l == tl && r == tr) return t[v]; n   int tm = (tl + tr) / 2; n   return sum(t, v * 2, tl, tm, l, min(r, tm)) + n          sum(t, v * 2 + 1, tm + 1, tr, max(l, tm + 1), r); n } n int main() { n   int n; n   std::cin >> n; n   long long *t = new long long[4 * n]; n   long long *forest = new long long[n]; n   long long **_forest = new long long *[n]; n   for (int i = 0; i < n; i++) _forest[i] = new long long[2]; n   long long tmp; n   for (int i = 0; i < n; i++) { n     std::cin >> tmp; n     forest[i] = tmp; n     _forest[i][0] = tmp; n     _forest[i][1] = i; n   } n   qSort(_forest, 0, n - 1); n   build(t, forest, 1, 0, n - 1); n   long long **couples = new long long *[(n / 2) + 1]; n   for (int i = 0; i < n / 2 + 1; i++) couples[i] = new long long[2]; n   int couple_count = 0; n   long long now = -1000000000000000000; n   int i = 0; n   while (i < n) { n     now = _forest[i][0]; n     int min = n, max = 0; n     while (i < n && _forest[i][0] == now) { n       if (_forest[i][1] < min) min = _forest[i][1]; n       if (_forest[i][1] > max) max = _forest[i][1]; n       i++; n     } n     if (min != max && max != 0) { n       couples[couple_count][0] = min; n       couples[couple_count][1] = max; n       couple_count++; n     } n   } n   long long max = -100000000000000000; n   int num = 0; n   for (int i = 0; i < couple_count; i++) { n     long long su = 0; n     su += sum(t, 1, 0, n - 1, couples[i][0] + 1, couples[i][1] - 1); n     su = su + forest[couples[i][0]] + forest[couples[i][1]]; n     if (su > max) { n       max = su; n       num = i; n     } n   } n   std::vector<int> toDelete; n   int del = 0; n   del = del + couples[num][0] + (n - 1 - couples[num][1]); n   for (int i = couples[num][0] + 1; i < couples[num][1]; i++) n     if (forest[i] < 0) { n       del++; n       toDelete.push_back(i); n     } n   std::cout << max <<     << del << std::endl; n   for (int i = 0; i < couples[num][0]; i++) std::cout << i + 1 <<    ; n   for (std::vector<int>::iterator it = toDelete.begin(); it != toDelete.end(); n        it++) n     std::cout << *(it) + 1 <<    ; n   for (int i = couples[num][1] + 1; i < n; i++) std::cout << i + 1 <<    ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int x1 = 0, y1 = 0, x2 = 0, y2 = 0; n   int n = 0; n   cin >> x1 >> y1 >> x2 >> y2 >> n; n   int count = 0; n   for (int i = 0; i < n; ++i) { n     int a = 0, b = 0, c = 0; n     cin >> a >> b >> c; n     int d1 = (a * 1LL * x1 + b * 1LL * y1 + c < 0 ? -1 : 1); n     int d2 = (a * 1LL * x2 + b * 1LL * y2 + c < 0 ? -1 : 1); n     if ((d1 * 1LL * d2) < 0) ++count; n   } n   cout << count; n   return 0; n } n


#include <bits/stdc++.h> n  n using namespace std; n  n #define fr first n #define sc second n #define pb push_back n  n const int INF = 1e9 + 1; n const int mod = 1e9+9; n const int base = 1e9; n  n int main() { n     //freopen( input.txt ,  r , stdin); n     ios_base::sync_with_stdio(false); n     cin.tie(0); cout.tie(0); n     int t; n     cin>>t; n     while(t--) { n         int n, m; n         cin>>n>>m; n         vector<int> k(n), c(m); n         for(int i = 0; i < n; i++) { n             cin>>k[i]; n         } n         sort(k.rbegin(), k.rend()); n         for(int i = 0; i < m; i++) { n             cin>>c[i]; n         } n         int64_t ans = 0; n         int i = 0; n         for(auto p : k) { n             if(i < p) { n                 ans += c[i]; n                 i++; n             } else { n                 ans += c[p-1]; n             } n         } n         cout<<ans<<  n ; n     } n     return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int N, K; n bool good[1050]; n int main() { n   scanf( %d , &N); n   for (int i = 2; i <= N; i++) { n     bool isp = true; n     for (int j = 2; j < i; j++) n       if (i % j == 0) { n         isp = false; n         int x = i; n         while (x % j == 0) x /= j; n         if (x == 1) good[i] = true; n         break; n       } n     if (isp) good[i] = true; n     if (good[i]) ++K; n   } n   printf( %d n , K); n   for (int i = 2; i <= N; i++) n     if (good[i]) printf( %d  , i); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void solve() { n   long long a, b; n   cin >> a >> b; n   if ((a + b) % 3 != 0) { n     cout <<  NO  << endl; n     return; n   } n   long long mi = min(a, b); n   long long mx = max(a, b); n   long long d = mx - mi; n   if (mx > 2 * mi) { n     cout <<  NO  << endl; n   } else { n     cout <<  YES  << endl; n   } n } n int32_t main() { n   ios_base::sync_with_stdio(0); n   cin.tie(NULL); n   long long t; n   cin >> t; n   while (t--) { n     solve(); n   } n } n


#include <bits/stdc++.h> n using namespace std; n string t[100000]; n char mm[100000]; n int check[26]; n int ans() { n   int m = 0, c = 0; n   for (int i = 0; i < 26; ++i) m = max(m, check[i]); n   for (int i = 0; i < 26; ++i) { n     if (check[i] == m) ++c; n     if (c > 1) return 0; n   } n   return 1; n } n int main() { n   int n, shock = 0, c = 0; n   cin >> n; n   for (int i = 0; i < n; ++i) { n     cin >> mm[i] >> t[i]; n     if (mm[i] ==  !  or mm[i] ==  ? ) ++shock; n   } n   for (int i = 0; i < n - 1; ++i) { n     if (mm[i] ==  . ) { n       for (int j = 0; j < t[i].size(); ++j) check[t[i][j] -  a ] = -1; n     } n     if (mm[i] ==  ! ) { n       ++c; n       set<char> s; n       for (int j = 0; j < t[i].size(); ++j) { n         if (check[t[i][j] -  a ] != -1) { n           if (s.find(t[i][j] -  a ) == s.end()) { n             s.insert(t[i][j] -  a ); n             ++check[t[i][j] -  a ]; n           } n         } n       } n     } n     if (mm[i] ==  ? ) { n       ++c; n       check[t[i][0] -  a ] = -1; n     } n     if (ans()) { n       cout << shock - c - 1; n       return 0; n     } n   } n   cout <<  0 ; n } n


#include <bits/stdc++.h> n using namespace std; n const int inf = 1E7; n int a[2][200], u, v, c[200], n; n bool check() { n   for (int i = 0; i <= n; i++) n     if (c[i] > 1 || c[i] < -1) return 0; n   return 1; n } n void dfs(int x) { n   if (x > n) { n     cout << n << endl; n     for (int i = 0; i <= n; i++) printf( %d  , a[u][i]); n     printf(  n%d n , n - 1); n     for (int i = 0; i < n; i++) printf( %d  , a[v][i]); n     return; n   } n   for (int i = 1; i <= x; i++) c[i] = a[u][i - 1]; n   c[0] = 0; n   for (int i = 0; i < x; i++) c[i] += a[v][i]; n   if (!check()) { n     for (int i = 1; i <= x; i++) c[i] = a[u][i - 1]; n     c[0] = 0; n     for (int i = 0; i < x; i++) c[i] -= a[v][i]; n   } n   for (int i = 0; i <= x; i++) a[v][i] = c[i]; n   swap(u, v); n   dfs(x + 1); n } n int main() { n   cin >> n; n   u = 0; n   v = 1; n   a[0][0] = 1; n   dfs(1); n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long n, arr[100], k; n   set<int> todo; n   cin >> n >> k; n   for (int i = 0; i < n; i++) { n     cin >> arr[i]; n     k += arr[i]; n     for (long long x = 1; x * x <= arr[i]; x++) { n       todo.insert(x); n       todo.insert(arr[i] / x); n       todo.insert(arr[i] / x + 1); n     } n   } n   long long best = 1; n   auto nxtIt = todo.begin(); n   for (int i : todo) { n     nxtIt++; n     long long nxtVal = (nxtIt == todo.end()) ? 1LL << 55 : *nxtIt; n     long long sum = 0; n     for (int j = 0; j < n; j++) sum += (arr[j] + i - 1) / i; n     if (sum * i <= k) best = max(best, min(nxtVal - 1, k / sum)); n   } n   cout << best; n } n


#include <bits/stdc++.h> n using namespace std; n string s[100001]; n int main() { n   int n, m; n   cin >> n >> m; n   for (int i = 1; i <= n; i++) { n     cin >> s[i]; n   } n   long double x = 0; n   long long ans = 0; n   for (int i = n; i >= 1; i--) { n     if (s[i] ==  halfplus ) { n       x += 0.5; n     } n     ans += x * m; n     x *= 2; n   } n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const double pi = acos(-1.0); n const double eps = 1e-8; n const int INF = 1E9; n const int MAXN = 11111; n int n, maxc; n int l[MAXN], r[MAXN], b[MAXN]; n int _less[MAXN]; n int id[MAXN], mx; n double ans, dp[11][MAXN], sum[11][MAXN]; n double p[11]; n int main() { n   cin >> n; n   for (int i = 0; i < (int)(n); i++) { n     cin >> l[i] >> r[i]; n     p[i] = 1.0 / (r[i] - l[i] + 1); n   } n   for (int i = 0; i < (int)(n); i++) id[i] = i; n   ans = 0; n   while (true) { n     maxc = 1 << (n - 1); n     for (int mask = 0; mask < (int)(maxc); mask++) { n       for (int i = 0; i < (int)(n - 1); i++) _less[i] = (mask >> i) & 1; n       bool ok = 1; n       for (int i = 0; i < (int)(n - 1); i++) n         if (_less[i] == 1 && id[i] > id[i + 1]) { n           ok = 0; n           break; n         } n       if (!ok) continue; n       memset(dp, 0, sizeof(dp)); n       for (int i = l[id[n - 2]]; i <= r[id[n - 2]]; i++) { n         dp[n - 2][i] = 1.0 * i; n         if (_less[n - 2]) n           dp[n - 2][i] *= (l[id[n - 1]] <= i && i <= r[id[n - 1]] ? 1 : 0); n         else n           dp[n - 2][i] *= (i + 1 <= r[id[n - 1]] n                                ? (r[id[n - 1]] - max(l[id[n - 1]], i + 1) + 1) n                                : 0); n       } n       mx = r[id[n - 2]]; n       sum[n - 2][MAXN - 1] = dp[n - 2][MAXN - 1]; n       for (int i = (int)(MAXN - 1) - 1; i >= 0; i--) n         sum[n - 2][i] = sum[n - 2][i + 1] + dp[n - 2][i]; n       for (int i = n - 3; i >= 0; i--) { n         for (int j = l[id[i]]; j <= r[id[i]]; j++) { n           if (_less[i]) n             dp[i][j] = dp[i + 1][j]; n           else { n             dp[i][j] = sum[i + 1][j + 1]; n           } n         } n         sum[i][MAXN - 1] = dp[i][MAXN - 1]; n         for (int j = (int)(MAXN - 1) - 1; j >= 0; j--) n           sum[i][j] = sum[i][j + 1] + dp[i][j]; n       } n       ans += sum[0][0]; n     } n     if (!next_permutation(id, id + n)) break; n   } n   cout.precision(40); n   for (int i = 0; i < (int)(n); i++) ans *= p[i]; n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n signed main() { n   long long n; n   cin >> n; n   long long a[n]; n   long long sum = 0; n   for (long long i = 0; i < n; i++) { n     cin >> a[i]; n     sum += a[i]; n   } n   long long req = ceil(4.5 * n) - sum; n   vector<long long> b(n); n   for (long long i = 0; i < n; i++) { n     b[i] = 5 - a[i]; n   } n   bool check = 0; n   sort(b.begin(), b.end(), greater<long long>()); n   long long ans = 0; n   for (long long i = 0; i < n; i++) { n     if (req <= 0) { n       cout << ans; n       check = 1; n       break; n     } n     req -= b[i]; n     ans++; n   } n   if (!check) cout << n; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long MAXN = 4e5 + 47, mod = 1e9 + 7, inf = 1e9 + 7; n long long n, m; n vector<long long> val; n void read() { n   cin >> n; n   val.resize(n); n   for (long long i = 0; i < n; ++i) cin >> val[i]; n } n vector<long long> fenw(MAXN, 0); n void update(long long pos) { n   for (; pos < MAXN; pos = (pos | (pos + 1))) fenw[pos]++; n } n long long get_sum(long long l) { n   long long res = 0; n   for (; l >= 0; l = (l & (l + 1)) - 1) res += fenw[l]; n   return res; n } n bool comp(pair<long long, long long> &a, pair<long long, long long> &b) { n   if (a.first < b.first) return false; n   if (a.first > b.first) return true; n   return a.second <= b.second; n } n void solve() { n   cin >> m; n   vector<vector<pair<long long, long long> > > quer(n + 1); n   for (long long i = 0; i < m; ++i) { n     long long x, y; n     cin >> x >> y; n     quer[x - 1].push_back({y, i}); n   } n   vector<pair<long long, long long> > suck(n); n   for (long long i = 0; i < n; ++i) suck[i] = {val[i], i}; n   sort(suck.begin(), suck.end(), comp); n   vector<long long> ans(m, 0); n   for (long long len = 0; len < n; ++len) { n     update(suck[len].second); n     for (pair<long long, long long> p : quer[len]) { n       long long k = p.first; n       long long l = 0, r = n; n       while (l < r) { n         long long mid = l + (r - l) / 2; n         if (get_sum(mid) < k) n           l = mid + 1; n         else n           r = mid; n       } n       ans[p.second] = val[l]; n     } n   } n   for (long long i = 0; i < m; ++i) cout << ans[i] <<    ; n   cout << endl; n } n signed main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   long long t = 1; n   while (t--) { n     read(); n     solve(); n   } n } n


#include <bits/stdc++.h> n using namespace std; n struct node { n   int v, w; n   node() {} n   node(int V, int W) { n     v = V; n     w = W; n   } n }; n vector<node> G[105]; n int n, m; n int dp[105][105][30]; n int dfs(int first, int second, int s) { n   if (dp[first][second][s] != -1) return dp[first][second][s]; n   for (int i = 0; i < G[first].size(); i++) { n     int v = G[first][i].v; n     int w = G[first][i].w; n     if (w < s) continue; n     if (dfs(second, v, w) == 0) return dp[first][second][s] = 1; n   } n   return dp[first][second][s] = 0; n } n int main() { n   while (~scanf( %d %d , &n, &m)) { n     memset(dp, -1, sizeof(dp)); n     for (int i = 1; i <= n; i++) G[i].clear(); n     for (int i = 1; i <= m; i++) { n       int u, v; n       char s; n       scanf( %d %d %c , &u, &v, &s); n       G[u].push_back(node(v, s -  a )); n     } n     for (int i = 1; i <= n; i++) { n       for (int j = 1; j <= n; j++) n         if (dfs(i, j, 0)) n           printf( A ); n         else n           printf( B ); n       printf(  n ); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, a, i, c = 0, b; n   cin >> n; n   char name[15]; n   for (i = 0; i < n; i++) { n     cin >> name; n     cin >> b >> a; n     if (b >= 2400 && a > b) c = 1; n   } n   if (c == 1) n     cout <<  YES  << endl; n   else n     cout <<  NO  << endl; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1000001; n int main() { n   ios::sync_with_stdio(false); n   int n; n   string s; n   cin >> n >> s; n   sort(s.begin(), s.end()); n   cout << s; 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 const int MAXN = 100000; n const int MAXCAP = 300000; n typedef struct R { n   int w, c; n } R; n bool operator<(const R &a, const R &b) { n   return (long long)a.c * b.w > (long long)b.c * a.w; n } n int n, cap; n R r[MAXN]; n multiset<int> have[4]; n multiset<int> avail[4]; n long long getlargest(multiset<int> &a, int cnt) { n   if (((int)(a).size()) < cnt) return INT_MIN; n   auto it = a.end(); n   long long ret = 0; n   for (int i = (0); i < (cnt); ++i) { n     --it; n     ret += *it; n   } n   return ret; n } n long long getsmallest(multiset<int> &a, int cnt) { n   if (((int)(a).size()) < cnt) return INT_MAX; n   auto it = a.begin(); n   long long ret = 0; n   for (int i = (0); i < (cnt); ++i) { n     ret += *it; n     ++it; n   } n   return ret; n } n void removeone(multiset<int> &a, int val) { n   auto it = a.find(val); n   if (it == a.end()) n     printf( err (%d) n , val); n   else n     a.erase(it); n } n void take(int cnt, int type) { n   for (int i = (0); i < (cnt); ++i) { n     long long cur = getlargest(avail[type], 1); n     removeone(avail[type], cur); n     have[type].insert(cur); n   } n } n void remove(int cnt, int type) { n   for (int i = (0); i < (cnt); ++i) { n     long long cur = getsmallest(have[type], 1); n     removeone(have[type], cur); n     avail[type].insert(cur); n   } n } n void run() { n   scanf( %d%d , &n, &cap); n   for (int i = (0); i < (n); ++i) scanf( %d%d , &r[i].w, &r[i].c); n   sort(r, r + n); n   int left = cap; n   for (int i = (0); i < (4); ++i) have[i].clear(), avail[i].clear(); n   for (int i = (0); i < (n); ++i) avail[r[i].w].insert(r[i].c); n   long long ret = 0; n   for (int i = (0); i < (n); ++i) { n     if (r[i].w > left) break; n     left -= r[i].w; n     ret += r[i].c; n     have[r[i].w].insert(r[i].c); n     removeone(avail[r[i].w], r[i].c); n   } n   while (left >= 1) { n     long long best = 0; n     int besttype = -1; n     { n       long long cur = getlargest(avail[1], 1); n       if (cur > best) best = cur, besttype = 0; n     } n     { n       long long cur = getlargest(avail[2], 1) - getsmallest(have[1], 1); n       if (cur > best) best = cur, besttype = 1; n     } n     { n       long long cur = getlargest(avail[2], 2) - getsmallest(have[3], 1); n       if (cur > best) best = cur, besttype = 2; n     } n     { n       long long cur = getlargest(avail[3], 1) - getsmallest(have[2], 1); n       if (cur > best) best = cur, besttype = 3; n     } n     { n       long long cur = getlargest(avail[3], 1) - getsmallest(have[1], 2); n       if (cur > best) best = cur, besttype = 4; n     } n     if (besttype == -1) break; n     if (besttype == 0) n       take(1, 1); n     else if (besttype == 1) n       take(1, 2), remove(1, 1); n     else if (besttype == 2) n       take(2, 2), remove(1, 3); n     else if (besttype == 3) n       take(1, 3), remove(1, 2); n     else if (besttype == 4) n       take(1, 3), remove(2, 1); n     ret += best; n     --left; n   } n   while (left >= 2) { n     long long best = 0; n     int besttype = -1; n     { n       long long cur = getlargest(avail[2], 1); n       if (cur > best) best = cur, besttype = 0; n     } n     { n       long long cur = getlargest(avail[3], 1) - getsmallest(have[1], 1); n       if (cur > best) best = cur, besttype = 1; n     } n     { n       long long cur = getlargest(avail[3], 2) - getsmallest(have[2], 2); n       if (cur > best) best = cur, besttype = 2; n     } n     if (besttype == -1) break; n     if (besttype == 0) n       take(1, 2); n     else if (besttype == 1) n       take(1, 3), remove(1, 1); n     else if (besttype == 2) n       take(2, 3), remove(2, 2); n     ret += best; n     --left; n   } n   printf( %lld n , ret); n } n int main() { n   run(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void print(long long a[], int n) { n   cout << endl; n   for (int i = 0; i < n; i++) cout << a[i] <<    ; n   cout << endl; n } n const int N = 1111; n int db[2][N]; n int main() { n   int n, k, x; n   scanf( %d%d%d , &n, &k, &x); n   int minn = 1 << 30; n   int maxx = -minn; n   for (int i = 0; i < n; i++) { n     int r; n     scanf( %d , &r); n     db[0][r]++; n   } n   int b = 0; n   int p = 1; n   for (int i = 0; i < k; i++) { n     for (int j = 0; j < N; j++) db[p][j] = 0; n     int c = 0; n     for (int j = 0; j < N; j++) { n       if (db[b][j]) { n         int t = db[b][j]; n         if (c % 2 == 0) { n           db[p][j] += t / 2; n           db[p][j ^ x] += (t + 1) / 2; n         } else { n           db[p][j] += (t + 1) / 2; n           db[p][j ^ x] += t / 2; n         } n         c += t; n       } n     } n     swap(b, p); n   } n   for (int i = 0; i < N; i++) { n     if (db[b][i]) { n       minn = min(minn, i); n       maxx = max(maxx, i); n     } n   } n   cout << maxx <<     << minn << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 2222, mod = 998244353; n int add(int x, int y) { n   x += y; n   return x >= mod ? x - mod : x; n } n int mul(int x, int y) { return 1ll * x * y % mod; } n int qpow(int a, int b) { n   int ans = 1; n   while (b) { n     if (b & 1) ans = mul(ans, a); n     a = mul(a, a), b >>= 1; n   } n   return ans; n } n int f[maxn][maxn], g[maxn], h[maxn], n, p, q, a, b, pp[maxn], pq[maxn], n     cc[maxn]; n int main() { n   cin >> n >> a >> b; n   p = mul(a, qpow(b, mod - 2)); n   q = add(1, mod - p); n   for (int i = pp[0] = pq[0] = 1; i <= n; i++) n     pp[i] = mul(pp[i - 1], p), pq[i] = mul(pq[i - 1], q); n   f[0][0] = 1; n   for (int i = 0; i < n; i++) { n     for (int j = 0; j <= i; j++) { n       f[i + 1][j + 1] = add(f[i + 1][j + 1], mul(f[i][j], pq[i - j])); n       f[i + 1][j] = add(f[i + 1][j], mul(f[i][j], pp[j])); n     } n   } n   g[1] = 1, g[2] = 0; n   for (int i = 3; i <= n; i++) { n     g[i] = 1; n     for (int j = 1; j < i; j++) g[i] = add(g[i], mod - mul(g[j], f[i][j])); n     cc[i] = 1ll * i * (i - 1) / 2 % mod; n   } n   cc[2] = 1; n   h[1] = 0, h[2] = 1; n   for (int i = 3; i <= n; i++) { n     for (int j = 1; j < i; j++) { n       h[i] = n           add(h[i], mul(mul(f[i][j], g[j]), n                         add(h[j], add(h[i - j], add(cc[i], mod - cc[i - j]))))); n     } n     h[i] = add(h[i], mul(g[i], cc[i])); n     h[i] = mul(h[i], qpow(add(1, mod - g[i]), mod - 2)); n   } n   cout << h[n]; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, k; n vector<int> g[100005]; n int siz[100005]; n bool era[100005]; n long long rans; n int col[100005]; n int calc(int v, int p) { n   int cnt = 1; n   for (int i = 0; i < (int)g[v].size(); i++) { n     int to = g[v][i]; n     if (to == p || era[to]) continue; n     cnt += calc(to, v); n   } n   siz[v] = cnt; n   return cnt; n } n pair<int, int> fnd(int v, int p, int t) { n   pair<int, int> res = make_pair(1e9, -1); n   int cnt = 1, mx = 0; n   for (int i = 0; i < (int)g[v].size(); i++) { n     int to = g[v][i]; n     if (to == p || era[to]) continue; n     res = min(res, fnd(to, v, t)); n     cnt += siz[to]; n     mx = max(mx, siz[to]); n   } n   mx = max(mx, t - cnt); n   res = min(res, make_pair(mx, v)); n   return res; n } n void solve(int v, int lev) { n   calc(v, -1); n   int s = fnd(v, -1, siz[v]).second; n   era[s] = 1; n   col[s] = lev; n   for (int i = 0; i < (int)g[s].size(); i++) { n     if (era[g[s][i]]) continue; n     solve(g[s][i], lev + 1); n   } n   era[s] = 0; n } n int main() { n   scanf( %d , &n); n   for (int i = 0; i < n - 1; i++) { n     int a, b; n     scanf( %d %d , &a, &b); n     g[a].push_back(b); n     g[b].push_back(a); n   } n   solve(1, 1); n   for (int i = 1; i <= n; i++) n     if (col[i] > 26) { n       puts( Impossible! ); n       return 0; n     } n   for (int i = 1; i <= n; i++) printf( %c  , col[i] - 1 +  A ); n   puts(  ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 2e5 + 5; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   int t; n   cin >> t; n   while (t--) { n     int n; n     cin >> n; n     vector<int> v(n); n     for (int i = 0; i < n; i++) cin >> v[i]; n     long long ans = 0; n     for (int i = 30; i >= 0; i--) { n       vector<int> v1; n       int cnt1 = 0, cnt2 = 0; n       for (int x : v) { n         if (x & (1 << i)) { n           cnt1++; n         } else { n           cnt2++; n           v1.push_back(x); n         } n       } n       ans += (1LL * cnt1 * (cnt1 - 1)); n       int len = v.size(); n       while (len--) v.pop_back(); n       for (int x : v1) v.push_back(x); n     } n     cout << ans / 2 <<   n ; n     ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int tree[4 * 200005], cs[200005], ar[200005]; n struct dt { n   int b, e, h, id, res; n } st[100005]; n bool cmp1(dt x, dt y) { return x.b < y.b; } n bool cmp2(dt x, dt y) { return x.id < y.id; } n void update(int nd, int b, int e, int x, int v) { n   if (b > x || e < x) return; n   if (b == x && e == x) { n     tree[nd] = v; n     return; n   } n   int left = 2 * nd; n   int right = 2 * nd + 1; n   int md = (b + e) / 2; n   update(left, b, md, x, v); n   update(right, md + 1, e, x, v); n   tree[nd] = max(tree[left], tree[right]); n } n int query(int nd, int b, int e, int x, int y) { n   if (e < x || b > y) return 0; n   if (b >= x && e <= y) return tree[nd]; n   int left = 2 * nd; n   int right = 2 * nd + 1; n   int md = (b + e) / 2; n   int p1 = query(left, b, md, x, y); n   int p2 = query(right, md + 1, e, x, y); n   return max(p1, p2); n } n int main() { n   int n; n   scanf( %d , &n); n   set<int> ss; n   for (int i = 1; i <= n; i++) { n     int b, h, e; n     scanf( %d%d , &b, &h); n     e = b + h - 1; n     st[i].b = b; n     st[i].e = e; n     st[i].h = h; n     st[i].id = i; n     st[i].res = -1; n     ss.insert(b); n     ss.insert(e); n   } n   map<int, int> mp; n   set<int>::iterator it; n   int m = 0; n   for (it = ss.begin(); it != ss.end(); it++) mp[*it] = ++m; n   sort(st + 1, st + n + 1, cmp1); n   int sz = ss.size(); n   for (int i = 1; i <= n; i++) { n     int v = mp[st[i].b]; n     ar[v] = 1; n     int u = mp[st[i].e]; n     update(1, 1, sz, v, u); n   } n   cs[0] = 0; n   for (int i = 1; i <= sz; i++) cs[i] = cs[i - 1] + ar[i]; n   for (int i = n; i >= 1; i--) { n     int b = mp[st[i].b]; n     int e = mp[st[i].e]; n     int p = query(1, 1, sz, b, e); n     int res = cs[p] - cs[b - 1]; n     st[i].res = res; n     update(1, 1, sz, b, p); n   } n   sort(st + 1, st + n + 1, cmp2); n   for (int i = 1; i <= n; i++) { n     if (i == n) n       printf( %d n , st[i].res); n     else n       printf( %d  , st[i].res); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long modpow(long long a, long long n, long long temp) { n   long long res = 1, y = a; n   while (n > 0) { n     if (n & 1) res = (res * y) % temp; n     y = (y * y) % temp; n     n /= 2; n   } n   return res % temp; n } n long long ison(long long mask, long long pos) { return (mask & (1 << pos)); } n long long cbit(long long n) { n   long long k = 0; n   while (n) n &= (n - 1), k++; n   return k; n } n long long nbit(long long n) { n   long long k = 0; n   while (n) n /= 2, k++; n   return k; n } n long long mod = 1e9 + 7; n int sgn(long long x) { return x < 0 ? -1 : !!x; } n long long c[200005]; n long long d[200005]; n long long bit[500005], n, bit2[500005]; n void upd(long long ind, long long val) { n   if (ind == 0) return; n   while (ind <= n) { n     bit[ind] += val; n     ind += (ind & -ind); n   } n } n long long qu(long long ind) { n   long long k = 0; n   while (ind > 0) { n     k += bit[ind]; n     ind -= (ind & -ind); n   } n   return k; n } n void upd2(long long ind, long long val) { n   if (ind == 0) return; n   while (ind <= n) { n     bit2[ind] += val; n     ind += (ind & -ind); n   } n } n long long qu2(long long ind) { n   long long k = 0; n   while (ind > 0) { n     k += bit2[ind]; n     ind -= (ind & -ind); n   } n   return k; n } n long long xo(long long i) { n   if ((i & 3) == 0) return i; n   if ((i & 3) == 1) return 1; n   if ((i & 3) == 2) return i + 1; n   return 0; n } n int main() { n   long long k, a, b, l, r, t, i, q; n   cin >> n >> k >> a >> b >> q; n   while (q--) { n     cin >> t; n     if (t == 1) { n       long long d1, d2; n       cin >> d1 >> d2; n       long long x1 = min(b, c[d1] + d2); n       long long x2 = min(a, d[d1] + d2); n       upd(d1, x1 - c[d1]); n       upd2(d1, x2 - d[d1]); n       c[d1] = x1; n       d[d1] = x2; n     } else { n       long long r1; n       cin >> r1; n       long long ans = 0; n       ans += qu(r1 - 1); n       ans += (qu2(n) - qu2(r1 + k - 1)); n       cout << ans << endl; n     } n   } n } n


#include <bits/stdc++.h> n #pragma comment(linker,  /STACK:16777216 ) n using namespace std; n struct post { n   int pos[3]; n   int st[3]; n   int mv; n }; n inline int code(const post& p) { n   int q = p.mv; n   for (int _n((3) - 1), i(0); i <= _n; i++) q = q * 44 + p.pos[i]; n   for (int _n((3) - 1), i(0); i <= _n; i++) q = q * 5 + p.st[i]; n   return q; n } n inline post decode(int q) { n   post p; n   for (int i = (2), _b = (0); i >= _b; i--) { n     p.st[i] = q % 5; n     q /= 5; n   } n   for (int i = (2), _b = (0); i >= _b; i--) { n     p.pos[i] = q % 44; n     q /= 44; n   } n   p.mv = q; n   return p; n } n int a, b, c, d, i, j, n, m, k; n int ans = 0; n bool used[100000001]; n int fm[3], ft[3]; n queue<int> q; n inline void add(const post& p) { n   for (int _n((3) - 1), i(0); i <= _n; i++) { n     if (p.pos[i] < 0) return; n     if (p.pos[i] > ans) ans = p.pos[i]; n   } n   int qq = code(p); n   if (used[qq]) return; n   used[qq] = 1; n   q.push(qq); n } n int main() { n   post p; n   memset((p.pos), 0, sizeof(p.pos)); n   memset((p.st), 0, sizeof(p.st)); n   p.mv = 0; n   for (int _n((3) - 1), i(0); i <= _n; i++) { n     scanf( %d%d%d , &p.pos[i], &fm[i], &ft[i]); n     p.pos[i] += 2; n   } n   add(p); n   while (!q.empty()) { n     post cur = decode(q.front()); n     q.pop(); n     for (int _n((3) - 1), i(0); i <= _n; i++) { n       if (cur.mv & 1 << i) continue; n       if (cur.st[i] != 0 && cur.st[i] != 4) continue; n       bool ok = 1; n       for (int _n((3) - 1), j(0); j <= _n; j++) { n         if (cur.st[j] == i + 1) { n           ok = 0; n           break; n         } n       } n       if (!ok) continue; n       for (int _n(fm[i]), dx(-fm[i]); dx <= _n; dx++) { n         if (!dx) continue; n         post nx = cur; n         nx.pos[i] += dx; n         int o = 0; n         for (int _n((3) - 1), z(0); z <= _n; z++) { n           if (nx.pos[z] == nx.pos[i]) ++o; n         } n         if (o > 1) continue; n         nx.mv |= 1 << i; n         add(nx); n       } n     } n     for (int _n((3) - 1), i(0); i <= _n; i++) { n       if (cur.st[i] != 0) continue; n       bool ok = 1; n       for (int _n((3) - 1), j(0); j <= _n; j++) { n         if (cur.st[j] == i + 1) { n           ok = 0; n           break; n         } n       } n       if (!ok) continue; n       for (int _n((3) - 1), j(0); j <= _n; j++) { n         if (i == j) continue; n         if (cur.pos[j] != cur.pos[i] + 1 && cur.pos[j] != cur.pos[i] - 1) n           continue; n         bool ok = 1; n         for (int _n((3) - 1), z(0); z <= _n; z++) { n           if (cur.st[z] == j + 1) { n             ok = 0; n             break; n           } n         } n         if (!ok) continue; n         post nx = cur; n         nx.st[i] = j + 1; n         nx.pos[j] = nx.pos[i]; n         if (nx.st[j] != 0 && nx.st[j] != 4) { n           for (int _n((3) - 1), z(0); z <= _n; z++) nx.pos[z] = cur.pos[i]; n         } n         add(nx); n       } n     } n     for (int _n((3) - 1), i(0); i <= _n; i++) { n       if (cur.st[i] == 0 || cur.st[i] == 4) continue; n       bool ok = 1; n       for (int _n((3) - 1), j(0); j <= _n; j++) { n         if (cur.st[j] == i + 1) { n           ok = 0; n           break; n         } n       } n       if (!ok) continue; n       for (int _n(ft[i]), dx(-ft[i]); dx <= _n; dx++) { n         if (!dx) continue; n         bool ok = 1; n         for (int _n((3) - 1), z(0); z <= _n; z++) { n           if (cur.pos[z] == dx + cur.pos[i]) continue; n         } n         post nx = cur; n         int pt = cur.st[i] - 1; n         nx.pos[pt] = cur.pos[i] + dx; n         if (cur.st[pt] > 0 && cur.st[pt] < 4) n           nx.pos[cur.st[pt] - 1] = cur.pos[i] + dx; n         nx.st[i] = 4; n         add(nx); n       } n     } n   } n   printf( %d n , ans - 2); n } n


#include <bits/stdc++.h> n using namespace std; n int n, m; n char arr[55][55]; n int dp[55][55][55][55]; n int f(int ind, int let, int dig, int sim) { n   if (ind == n) { n     if (let && dig && sim) n       return 0; n     else n       return 1e9; n   } n   if (dp[ind][let][dig][sim] != -1) return dp[ind][let][dig][sim]; n   int &ret = dp[ind][let][dig][sim]; n   ret = 1e9; n   for (int i = 0; i < m; i++) { n     if (arr[ind][i] >=  a  && arr[ind][i] <=  z ) n       ret = min(ret, f(ind + 1, let + 1, dig, sim) + min(i, m - i)); n     else if (arr[ind][i] >=  0  && arr[ind][i] <=  9 ) n       ret = min(ret, f(ind + 1, let, dig + 1, sim) + min(i, m - i)); n     else n       ret = min(ret, f(ind + 1, let, dig, sim + 1) + min(i, m - i)); n   } n   return ret; n } n int main() { n   while (scanf( %d %d , &n, &m) == 2) { n     memset(dp, -1, sizeof dp); n     for (int i = 0; i < n; i++) scanf( %s , &arr[i]); n     int ans = f(0, 0, 0, 0); n     printf( %d n , ans); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long a[500005]; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   long long n, i, j, k, l, m; n   long long t; n   cin >> t; n   long long pret = t; n   while (t--) { n     cin >> n >> k; n     for (i = 1; i <= n; i++) cin >> a[i]; n     int flag1 = 0; n     int flag2 = 0; n     for (i = 1; i <= n; i++) { n       if (a[i] == k) { n         flag1 = 1; n       } n       if (a[i] != k) { n         flag2 = 1; n       } n     } n     if (flag1 == 0) { n       cout <<  no n ; n       continue; n     } n     if (flag2 == 0) { n       cout <<  yes n ; n       continue; n     } n     a[n + 1] = 0; n     int flag = 0; n     for (i = 1; i <= n - 1; i++) { n       if (min(a[i], a[i + 1]) >= k) { n         flag = 1; n         break; n       } n     } n     for (i = 2; i <= n - 1; i++) { n       vector<long long> v; n       v.push_back(a[i - 1]); n       v.push_back(a[i + 1]); n       v.push_back(a[i]); n       sort(v.begin(), v.end()); n       if (v[1] >= k) flag = 1; n     } n     if (flag) n       cout <<  yes n ; n     else n       cout <<  no n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 2e5 + 10; n const int maxt = (1 << 19) + 20; n vector<pair<pair<int, int>, int> > ad[maxn]; n vector<pair<pair<int, int>, int> > qs[maxn]; n set<pair<int, int> > st; n set<pair<int, int> >::iterator it1, it2, it3; n int n, q; n long long val[maxt], taga[maxt], tagb[maxt]; n void update(int x) { n   val[x] = val[x * 2] + val[x * 2 + 1]; n   if (val[x] >= 998244353) val[x] -= 998244353; n } n void build(int x, int l, int r) { n   val[x] = tagb[x] = 0; n   taga[x] = 1; n   if (l == r) return; n   int mid = (l + r) >> 1; n   build(x * 2, l, mid); n   build(x * 2 + 1, mid + 1, r); n } n void taketag(int x, int l, int r) { n   if (l == r) { n     taga[x] = 1; n     tagb[x] = 0; n     return; n   } n   int mid = (l + r) >> 1; n   val[x * 2] = (val[x * 2] * taga[x]) % 998244353; n   val[x * 2] = (val[x * 2] + tagb[x] * (mid - l + 1)) % 998244353; n   val[x * 2 + 1] = (val[x * 2 + 1] * taga[x]) % 998244353; n   val[x * 2 + 1] = (val[x * 2 + 1] + tagb[x] * (r - mid)) % 998244353; n   taga[x * 2] = (taga[x * 2] * taga[x]) % 998244353; n   tagb[x * 2] = (tagb[x * 2] * taga[x]) % 998244353; n   tagb[x * 2] = tagb[x * 2] + tagb[x]; n   if (tagb[x * 2] >= 998244353) tagb[x * 2] -= 998244353; n   taga[x * 2 + 1] = (taga[x * 2 + 1] * taga[x]) % 998244353; n   tagb[x * 2 + 1] = (tagb[x * 2 + 1] * taga[x]) % 998244353; n   tagb[x * 2 + 1] = tagb[x * 2 + 1] + tagb[x]; n   if (tagb[x * 2 + 1] >= 998244353) tagb[x * 2 + 1] -= 998244353; n   taga[x] = 1; n   tagb[x] = 0; n } n void modify(int x, int l, int r, int tl, int tr, long long a, long long b) { n   if (l > tr || r < tl) return; n   if (tl <= l && r <= tr) { n     val[x] = (val[x] * a) % 998244353; n     val[x] = (val[x] + b * (r - l + 1)) % 998244353; n     taga[x] = (taga[x] * a) % 998244353; n     tagb[x] = (tagb[x] * a + b) % 998244353; n     taketag(x, l, r); n     return; n   } n   taketag(x, l, r); n   int mid = (l + r) >> 1; n   modify(x * 2, l, mid, tl, tr, a, b); n   modify(x * 2 + 1, mid + 1, r, tl, tr, a, b); n   update(x); n } n long long query(int x, int l, int r, int tl, int tr) { n   if (l > tr || r < tl) return 0; n   if (tl <= l && r <= tr) return val[x]; n   taketag(x, l, r); n   int mid = (l + r) >> 1; n   long long ans = n       (query(x * 2, l, mid, tl, tr) + query(x * 2 + 1, mid + 1, r, tl, tr)) % n       998244353; n   return ans; n } n int main() { n   scanf( %d%d , &n, &q); n   for (int i = 1; i <= q; i++) { n     int t, l, r, x; n     scanf( %d%d%d , &t, &l, &r); n     if (t == 1) { n       scanf( %d , &x); n       ad[x].push_back(make_pair(make_pair(l, r), i)); n     } else n       qs[i].push_back(make_pair(make_pair(l, r), 0)); n   } n   for (int i = 1; i <= n; i++) n     if (!ad[i].empty()) { n       st.clear(); n       st.insert(make_pair(1, 0)); n       st.insert(make_pair(n, 1)); n       for (int j = 0; j < ad[i].size(); j++) { n         int l = ad[i][j].first.first; n         int r = ad[i][j].first.second; n         if (st.empty()) { n           qs[ad[i][j].second].push_back(make_pair(ad[i][j].first, 2)); n         } else { n           it1 = st.lower_bound(make_pair(l, -1)); n           if ((*it1).second == 1) { n             st.insert(make_pair(l - 1, 1)); n           } n           int pre = l; n           bool beg = 0, ed = 0; n           for (it2 = it1; it2 != st.end();) { n             int cur = (*it2).first; n             int op = (*it2).second; n             if (cur > r) break; n             if (op == 0) { n               if (pre <= cur - 1) { n                 qs[ad[i][j].second].push_back( n                     make_pair(make_pair(pre, cur - 1), 2)); n               } n               pre = cur; n             } else { n               qs[ad[i][j].second].push_back(make_pair(make_pair(pre, cur), 1)); n               pre = cur + 1; n             } n             it1 = it2; n             ++it2; n             st.erase(it1); n           } n           if (pre <= r) { n             int op = 0; n             if (it2 != st.end()) op = (*it2).second; n             if (op == 0) { n               if (pre <= r) { n                 qs[ad[i][j].second].push_back(make_pair(make_pair(pre, r), 2)); n               } n             } else { n               qs[ad[i][j].second].push_back(make_pair(make_pair(pre, r), 1)); n               st.insert(make_pair(r + 1, 0)); n             } n           } n         } n       } n     } n   build(1, 1, n); n   for (int i = 1; i <= q; i++) n     for (int j = 0; j < qs[i].size(); j++) { n       if (qs[i][j].second == 0) { n         printf( %I64d n , n                query(1, 1, n, qs[i][j].first.first, qs[i][j].first.second)); n       } else if (qs[i][j].second == 1) { n         modify(1, 1, n, qs[i][j].first.first, qs[i][j].first.second, 1, 1); n       } else { n         modify(1, 1, n, qs[i][j].first.first, qs[i][j].first.second, 2, 0); n       } n     } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 110000; n const double EPS = 1E-7; n struct pt { n   int x, y; n }; n int n; n pt p[MAXN]; n int l[MAXN], r[MAXN]; n long long r_sums[MAXN]; n inline long long vec(pt a, pt b, pt c) { n   return (b.x - a.x) * 1ll * (c.y - a.y) - (b.y - a.y) * 1ll * (c.x - a.x); n } n inline bool between(pt p, pt a, pt b, pt q) { n   return vec(p, a, q) > 0 && vec(p, q, b) > 0; n } n inline long long sum_r(int lt, int rt) { n   long long res = r_sums[rt]; n   if (lt) res -= r_sums[lt - 1]; n   return res; n } n long long solve(pt q) { n   int j = 0; n   for (int i = 0; i < n; ++i) { n     j = max(j, i + 1); n     while (j + 1 < n && !between(p[i], p[i + 1], p[j + 1], q)) ++j; n     r[i] = j; n     l[i] = j + 1; n   } n   for (int i = 0; i < n; ++i) { n     r_sums[i] = 0; n     if (i) r_sums[i] += r_sums[i - 1]; n     r_sums[i] += r[i]; n   } n   long long ans = 0; n   j = 0; n   for (int i = 0; i < n; ++i) { n     while (j < n && r[j] < l[i]) ++j; n     if (l[i] < n && j < n) { n       int left = j, right = r[i]; n       if (left > right) continue; n       ans += sum_r(left, right); n       ans -= (right - left + 1) * 1ll * (l[i] - 1); n     } n   } n   return ans; n } n bool inside(pt q) { n   bool res = false; n   for (int i = 0; i < n; ++i) { n     pt a = p[i], b = p[(i + 1) % n]; n     if (min(a.y, b.y) <= q.y && max(a.y, b.y) >= q.y) { n       if (q.y != a.y && q.y != b.y) { n         double cx = a.x + (b.x - a.x) * ((q.y - a.y) * 1. / (b.y - a.y)); n         if (cx >= q.x - EPS) res ^= true; n       } else { n         if (a.y == q.y && a.x > q.x || b.y == q.y && b.x > q.x) n           if (max(a.y, b.y) > q.y) res ^= true; n       } n     } n   } n   return res; n } n int main() { n   cin >> n; n   for (int i = 0; i < n; ++i) scanf( %d%d , &p[i].x, &p[i].y); n   reverse(p, p + n); n   int ts; n   cin >> ts; n   for (int tt = 0; tt < ts; ++tt) { n     pt q; n     scanf( %d%d , &q.x, &q.y); n     long long ans = 0; n     if (inside(q)) ans = solve(q); n     printf( %I64d n , ans); n   } n } n


#include <bits/stdc++.h> n const int maxn = 1e3 + 10; n using namespace std; n int n, x[110], y[110], cnt1, cnt2, sum1, sum2; n int main() { n   cin >> n; n   for (int i = 0; i < n; i++) { n     cin >> x[i] >> y[i]; n     if (x[i] % 2 == 1) { n       cnt1++; n     } n     if (y[i] % 2 == 1) { n       cnt2++; n     } n     sum1 += x[i]; n     sum2 += y[i]; n   } n   if (cnt1 % 2 == 0 && cnt2 % 2 == 0) { n     if (sum1 % 2 == 0 && sum2 % 2 == 0) { n       cout <<  0 ; n       return 0; n     } else { n       for (int i = 0; i < n; i++) { n         if (x[i] % 2 == 0 && y[i] % 2 == 1) { n           cout <<  1 ; n           return 0; n         } else if (x[i] % 2 == 1 && y[i] % 2 == 0) { n           cout <<  1 ; n           return 0; n         } n       } n     } n   } else if (cnt1 % 2 == 1 && cnt2 % 2 == 1) { n     for (int i = 0; i < n; i++) { n       if (x[i] % 2 == 0 && y[i] % 2 == 1) { n         cout <<  1 ; n         return 0; n       } else if (x[i] % 2 == 1 && y[i] % 2 == 0) { n         cout <<  1 ; n         return 0; n       } n     } n   } n   cout <<  -1 ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <class X, class Y> n bool minimize(X &x, const Y &y) { n   X eps = 1e-9; n   if (x > y + eps) { n     x = y; n     return true; n   } else n     return false; n } n template <class X, class Y> n bool maximize(X &x, const Y &y) { n   X eps = 1e-9; n   if (x + eps < y) { n     x = y; n     return true; n   } else n     return false; n } n template <class T> n T Abs(const T &x) { n   return (x < 0 ? -x : x); n } n const int BASE = (1LL << (8)); n struct Number { n   unsigned char d[3]; n   Number(int x = 0) { n     for (int i = 0, _n = (3); i < _n; i++) { n       d[i] = x & 255; n       x >>= 8; n     } n   } n   Number operator+(const Number &x) const { n     Number res; n     int rem = 0; n     for (int i = 0, _n = (3); i < _n; i++) { n       int tmp = rem + d[i] + x.d[i]; n       res.d[i] = tmp & 255; n       rem = tmp > 255; n     } n     return res; n   } n   Number operator-(const Number &x) const { n     Number res; n     int rem = 0; n     for (int i = 0, _n = (3); i < _n; i++) { n       int tmp = d[i] - rem - x.d[i]; n       res.d[i] = tmp < 0 ? tmp + BASE : tmp; n       rem = tmp < 0; n     } n     return res; n   } n   bool operator==(const Number &x) const { n     for (int i = 0, _n = (3); i < _n; i++) n       if (d[i] != x.d[i]) return false; n     return true; n   } n   int value(void) const { n     int res = 0; n     for (int i = (2), _a = (0); i >= _a; i--) res = (res << 8) ^ d[i]; n     return res; n   } n }; n long long value[1000100], asked; n int ___left[1000100], ___right[1000100], n; n Number numZero[62][1000100]; n void loadTree(void) { n   cin >> n >> asked; n   for (int i = (2), _b = (n); i <= _b; i++) { n     int p; n     long long w; n     cin >> p >> w; n     value[i] = value[p] ^ w; n   } n } n void prepare(void) { n   sort(value + 1, value + n + 1); n   for (int j = 0, _n = (62); j < _n; j++) n     for (int i = (1), _b = (n); i <= _b; i++) { n       numZero[j][i] = numZero[j][i - 1]; n       if (!(((value[i]) >> (j)) & 1)) numZero[j][i] = numZero[j][i] + 1; n     } n   for (int i = (1), _b = (n); i <= _b; i++) ___right[i] = n; n } n void process(void) { n   long long res = 0; n   for (int pos = (62 - 1), _a = (0); pos >= _a; pos--) { n     long long total = 0; n     for (int i = (1), _b = (n); i <= _b; i++) { n       int bit = (((value[i]) >> (pos)) & 1); n       int zero = (numZero[pos][___right[i]] - numZero[pos][___left[i]]).value(); n       int need = bit == 0 ? zero : ___right[i] - ___left[i] - zero; n       total += need; n       if (total > asked) break; n     } n     int newBit = total >= asked ? 0 : 1; n     if (asked > total) asked -= total; n     res |= (1LL * newBit) << pos; n     for (int i = (1), _b = (n); i <= _b; i++) { n       int need = newBit ^ (((value[i]) >> (pos)) & 1); n       int zero = (numZero[pos][___right[i]] - numZero[pos][___left[i]]).value(); n       if (need == 1) n         ___left[i] += zero; n       else n         ___right[i] = ___left[i] + zero; n     } n   } n   cout << res << endl; n } n int main(void) { n   ios::sync_with_stdio(false); n   cin.tie(NULL); n   loadTree(); n   prepare(); n   process(); 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   long long i, j, k, r, c, c1, c2, cnt, ans, x, y, p, t; n   vector<vector<long long>> v, a, b; n   unordered_map<long long, set<pair<long long, long long>>> m; n   vector<long long> re, q; n   cin >> t; n   i = 0; n   while (i < t) { n     cin >> r >> c; n     j = 0; n     while (j < r) { n       p = 0; n       while (p < c) { n         cin >> k; n         q.push_back(0); n         re.push_back(k); n         p++; n       } n       a.push_back(q); n       b.push_back(q); n       q.clear(); n       v.push_back(re); n       re.clear(); n       j++; n     } n     j = 0; n     a[0][0] = 0; n     m[0].insert(make_pair(0, 0)); n     while (j < r) { n       p = 0; n       while (p < c) { n         if (j + 1 < r) { n           a[j + 1][p] = 1 + a[j][p]; n           m[1 + a[j][p]].insert(make_pair(j + 1, p)); n         } n         if (p + 1 < c) { n           a[j][p + 1] = 1 + a[j][p]; n           m[1 + a[j][p]].insert(make_pair(j, p + 1)); n         } n         p++; n       } n       j++; n     } n     j = r - 1; n     b[r - 1][c - 1] = 0; n     m[0].insert(make_pair(r - 1, c - 1)); n     while (j >= 0) { n       p = c - 1; n       while (p >= 0) { n         if (j - 1 >= 0) { n           b[j - 1][p] = 1 + b[j][p]; n           m[1 + b[j][p]].insert(make_pair(j - 1, p)); n         } n         if (p - 1 >= 0) { n           b[j][p - 1] = 1 + b[j][p]; n           m[1 + b[j][p]].insert(make_pair(j, p - 1)); n         } n         p--; n       } n       j--; n     } n     unordered_map<long long, set<pair<long long, long long>>>::iterator it; n     set<pair<long long, long long>>::iterator u; n     set<pair<long long, long long>> s; n     it = m.begin(); n     cnt = 0; n     if ((r + c - 2) % 2 == 0) { n       k = (r + c - 2) / 2; n     } n     while (it != m.end()) { n       c1 = 0; n       c2 = 0; n       if (it->first != k) { n         s = it->second; n         u = s.begin(); n         while (u != s.end()) { n           if (v[(*u).first][(*u).second] == 0) { n             c1++; n           } else { n             c2++; n           } n           u++; n         } n         if (c1 <= c2) { n           u = s.begin(); n           while (u != s.end()) { n             if (v[(*u).first][(*u).second] == 0) { n               v[(*u).first][(*u).second] = 1; n             } n             u++; n           } n           cnt = cnt + c1; n         } else { n           u = s.begin(); n           while (u != s.end()) { n             if (v[(*u).first][(*u).second] == 1) { n               v[(*u).first][(*u).second] = 0; n             } n             u++; n           } n           cnt = cnt + c2; n         } n       } n       it++; n     } n     cout << cnt << endl; n     v.clear(); n     a.clear(); n     b.clear(); n     re.clear(); n     m.clear(); n     q.clear(); n     i++; n   } n   return (0); n } n


#include <bits/stdc++.h> n using namespace std; n int a[10]; n int main() { n   ios_base::sync_with_stdio(0); n   int n; n   cin >> n; n   string b; n   cin >> b; n   for (int i = 0; i < n; i++) { n     a[b[i] -  0 ] = 1; n   } n   bool flag = (((a[1] || a[2] || a[3]) && (a[0])) || n                ((a[1] || a[2] || a[3]) && (a[1] || a[4] || a[7])) && n                    ((a[3] || a[9] || a[6]) && (a[7] || a[0] || a[9]))); n   if (flag) n     cout <<  YES ; n   else n     cout <<  NO ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int dx[] = {0, 0, 1, -1, -1, -1, 1, 1}; n int dy[] = {1, -1, 0, 0, -1, 1, 1, -1}; n template <class T> n inline T biton(T n, T pos) { n   return n | ((T)1 << pos); n } n template <class T> n inline T bitoff(T n, T pos) { n   return n & ~((T)1 << pos); n } n template <class T> n inline T ison(T n, T pos) { n   return (bool)(n & ((T)1 << pos)); n } n template <class T> n inline T gcd(T a, T b) { n   while (b) { n     a %= b; n     swap(a, b); n   } n   return a; n } n template <typename T> n string NumberToString(T Number) { n   ostringstream second; n   second << Number; n   return second.str(); n } n inline int nxt() { n   int aaa; n   scanf( %d , &aaa); n   return aaa; n } n inline long long int lxt() { n   long long int aaa; n   scanf( %lld , &aaa); n   return aaa; n } n inline double dxt() { n   double aaa; n   scanf( %lf , &aaa); n   return aaa; n } n template <class T> n inline T bigmod(T p, T e, T m) { n   T ret = 1; n   for (; e > 0; e >>= 1) { n     if (e & 1) ret = (ret * p) % m; n     p = (p * p) % m; n   } n   return (T)ret; n } n int ar[1000010]; n vector<int> v; n vector<pair<int, int> > edge; n int cnt[1000010]; n int mark[1000010]; n set<int> st; n int get(int x) { n   for (int i = x - 1; i >= 1; i--) { n     if (st.find(i) == st.end() && !mark[i]) { n       mark[i] = 1; n       return i; n     } n   } n   return -1; n } n int main() { n   int n = nxt(); n   for (int i = 1; i < n; i++) { n     int a = nxt(); n     int b = nxt(); n     if (a > b) swap(a, b); n     if (b != n) { n       cout <<  NO  << endl; n       return 0; n     } n     if (a == b && b == n) { n       cout <<  NO  << endl; n       return 0; n     } n     st.insert(a); n     cnt[a]++; n     if (cnt[a] == 0) v.push_back(a); n   } n   for (int i = n - 1; i >= 1; i--) { n     vector<int> ok; n     if (cnt[i]) { n       ok.push_back(i); n       cnt[i]--; n       while (cnt[i]--) { n         int val = get(i); n         if (val == -1) { n           printf( NO n ); n           return 0; n         } n         ok.push_back(val); n       } n       sort(ok.begin(), ok.end()); n       for (int i = 1; i < ok.size(); i++) { n         edge.push_back(make_pair(ok[i], ok[i - 1])); n       } n       edge.push_back(make_pair(n, ok[0])); n     } n   } n   if (edge.size() != n - 1) { n     printf( NO n ); n     return 0; n   } n   printf( YES n ); n   for (auto it : edge) { n     printf( %d %d n , it.first, it.second); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n bool check(long long x, long long y, long long x1, long long y1) { n   if (x != 0 && y != 0 && x1 % x == 0 && y1 % y == 0 && x1 / x >= 0 && n       y1 / y >= 0 && x1 / x == y1 / y) n     return 1; n   if (x == 0 && y == 0 && y1 == 0 && x1 == 0) return 1; n   if (x == 0 && y != 0 && x1 == 0 && y1 % y == 0 && y1 / y >= 0) return 1; n   if (x != 0 && y == 0 && y1 == 0 && x1 % x == 0 && x1 / x >= 0) return 1; n   return 0; n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   long long a, b; n   cin >> a >> b; n   string s; n   cin >> s; n   long long a1 = 0, b1 = 0; n   for (int i = 0; i < s.size(); i++) { n     if (s[i] ==  U ) n       b1++; n     else if (s[i] ==  D ) n       b1--; n     else if (s[i] ==  R ) n       a1++; n     else n       a1--; n   } n   for (int i = 0; i < s.size(); i++) { n     long long a2 = a, b2 = b; n     for (int j = i; j >= 0; j--) { n       if (s[j] ==  U ) n         b2--; n       else if (s[j] ==  D ) n         b2++; n       else if (s[j] ==  R ) n         a2--; n       else n         a2++; n     } n     if (check(a1, b1, a2, b2)) { n       cout <<  Yes  << endl; n       return 0; n     } n   } n   if (a == 0 && b == 0) n     cout <<  Yes  << endl; n   else n     cout <<  No  << endl; n } n


#include <iostream> n #include <algorithm> n #include <vector> n #include <cmath> n  n using namespace std; n  n  n int main() n { n     int t = 0; n  n     cin>>t; n     for(int i = 0; i < t; i++) n     { n         int n = 0; n         cin>>n; n         int v[100] = {0}; n         for(int j = 0; j < n; j++) cin>>v[j]; n  n         if(v[0] == n && v[n-1] == 1) n         { n             cout<<3<<  n ; n  n         } n         else if(v[0] != 1 && v[n-1] != n) n         { n             cout<<2<<  n ; n         } n         else if(v[0] == 1 && v[n-1] != n) n         { n             cout<<1<<  n ; n         } n         else if(v[0] != 1 && v[n-1] == n) n         { n             cout<<1<<  n ; n         } n         else n         { n             bool d = false; n             for(int j = 0; j < n; j++) n             { n                 if(v[j] != j+1) n                 { n                     d = true; n                     break; n                 } n             } n             if(!d) cout<<0<<   ; n             else cout<<1<<   ; n         } n     } n  n     return 0; n } n


#include <bits/stdc++.h> n template <typename T> n inline void read(T &x) { n   bool b = 0; n   char c; n   while (!isdigit(c = getchar()) && c !=  - ) n     ; n   if (c ==  - ) c = getchar(), b = 1; n   x = c - 48; n   while (isdigit(c = getchar())) x = x * 10 + c - 48; n   if (b) x = -x; n } n void reads(std ::string &s) { n   char c; n   while (!isalpha(c = getchar())) n     ; n   s += c; n   while (isalpha(c = getchar())) s += c; n } n using namespace std; n const int N = 1e5 + 11; n int m; n long long n, f[N], ans[50], last[50]; n string s; n int main() { n   ios_base::sync_with_stdio(0); n   cout.tie(0); n   if (fopen( m  n              .inp , n              r )) { n     freopen( n          m  n          .inp , n          r , stdin); n   } n   read(m); n   long long lastl = 0, lastr = 0; n   int chl, chr; n   for (int ii = 1; ii <= m; ii++) { n     s =  # ; n     reads(s); n     n = s.size() - 1; n     for (int i = 0; i < 26; i++) ans[i] = 0; n     for (int i = 1; i <= n; i++) { n       if (s[i] == s[i - 1]) n         f[i] = f[i - 1]; n       else n         f[i] = 0; n       f[i]++; n       ans[s[i] -  a ] = max(ans[s[i] -  a ], f[i]); n     } n     long long l = 1, r = f[n]; n     for (int i = 2; i <= n; i++) { n       if (f[i] == 1) break; n       l = f[i]; n     } n     if (s[1] == s[n] && last[s[1] -  a ] > 0) n       ans[s[1] -  a ] = l + r + 1; n     else n       ans[s[1] -  a ] = l + (last[s[1] -  a ] > 0), n                  ans[s[n] -  a ] = r + (last[s[n] -  a ] > 0); n     if (l == n) ans[s[1] -  a ] = last[s[1] -  a ] + (last[s[1] -  a ] + 1) * l; n     if (s[1] == chl) l = lastl + (lastl + 1) * l; n     if (s[n] == chr) r = lastr + (lastr + 1) * r; n     for (int i = 0; i < 26; i++) { n       int ooooo = 0; n       if (last[i]) ooooo = 1; n       last[i] = ans[i]; n       if (!last[i]) last[i] = ooooo; n     } n     chl = s[1] -  a ; n     chr = s[n] -  a ; n     lastl = l; n     lastr = r; n   } n   long long res = max(lastl, lastr); n   for (int i = 0; i < 26; i++) res = max(res, last[i]); n   cout << res; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e3 + 7; n const long long mod = 998244353; n mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count()); n int n, s, d; n unsigned long long ok[N]; n unsigned long long d1[(1 << 20)], d2[(1 << 10)], a[N]; n unordered_map<unsigned long long, int> m; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   for (int i = 0; i < N; i++) ok[i] = rng(); n   cin >> n >> s >> d; n   for (int i = 0; i < s; i++) { n     int len; n     cin >> len; n     for (int j = 1; j <= len; j++) { n       int x; n       cin >> x; n       a[i] ^= ok[x]; n     } n   } n   int c = min(20, s); n   for (int i = 0; i < c; i++) { n     for (int j = 0; j < (1 << i); j++) { n       d1[(1 << i) + j] = (d1[j] ^ a[i]); n     } n   } n   for (int i = 0; i < s - c; i++) { n     for (int j = 0; j < (1 << i); j++) { n       d2[j + (1 << i)] = (d2[j] ^ a[i + c]); n     } n   } n   for (int i = 0; i < (1 << c); i++) { n     unsigned long long res = d1[i]; n     if (m.find(res) == m.end()) { n       m[res] = __builtin_popcount(i); n     } else { n       m[res] = min(m[res], __builtin_popcount(i)); n     } n   } n   for (int i = 1; i <= d; i++) { n     int len; n     cin >> len; n     unsigned long long a = 0; n     for (int j = 1; j <= len; j++) { n       int x; n       cin >> x; n       a ^= ok[x]; n     } n     int ans = 1e9; n     for (int j = 0; j < (1 << s - c); j++) { n       auto c = (a ^ d2[j]); n       if (m.find(c) != m.end()) { n         ans = min(ans, m[c] + __builtin_popcount(j)); n       } n     } n     cout << (ans == 1e9 ? -1 : ans) <<   n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 1e6 + 20; n int n, x, y, a[maxn], o = 0; n int main() { n   cin >> n >> x >> y; n   for (int i = 0; i < n; i++) cin >> a[i]; n   for (int i = 0; i < n; i++) { n     for (int j = max(0, i + x - 1); j < i; j++) { n       if (a[j] < a[i]) { n         o = 1; n         break; n       } n     } n     for (int j = i + 1; j < min(y + i + 1, n); j++) { n       if (a[j] < a[i]) { n         o = 1; n         break; n       } n     } n     if (o == 0) { n       cout << i + 1 << endl; n       return 0; n     } n     o = 0; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long mod = 1e9 + 7; n bool sortinrev(const pair<int, int> &a, const pair<int, int> &b) { n   return (a.first > b.first); n } n bool sortbysec(const pair<int, int> &a, const pair<int, int> &b) { n   return (a.second < b.second); n } n long long int power(long long int x, long long int y, long long int p) { n   long long int res = 1; n   x = x % p; n   while (y > 0) { n     if (y & 1) res = (res * x) % p; n     y = y >> 1; n     x = (x * x) % p; n   } n   return res; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   double x1, x2, y1, y2, r; n   cin >> r >> x1 >> y1 >> x2 >> y2; n   double d = sqrt((x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1)); n   double a = d / (2 * r); n   int b; n   if ((int)a * 2 * r == d) n     b = 0; n   else n     b = 1; n   if (b == 0) n     cout << (int)a; n   else n     cout << (int)a + 1; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 2e3; n int n, h, aa[N + 2], vis[2][N + 1][N + 1][2]; n double p, dp[2][N + 1][N + 1][2]; n double dfs(int s1, int lo, int hi, int s2) { n   if (vis[s1][lo][hi][s2]) return dp[s1][lo][hi][s2]; n   vis[s1][lo][hi][s2] = 1; n   if (lo > hi) return 0; n   double here = 0; n   int em; n   if (s1 == 0) n     em = min(aa[lo] - aa[lo - 1], h); n   else n     em = min(aa[lo] - aa[lo - 1] - h, h); n   here += 0.5 * p * em + 0.5 * p * dfs(0, lo + 1, hi, s2); n   em = aa[lo] + h; n   int i; n   for (i = lo + 1; i <= hi; i++) { n     if (aa[i] >= em) break; n     em = aa[i] + h; n   } n   if (i > hi) { n     if (s2 == 1) n       em = min(em, aa[hi + 1]); n     else n       em = min(em, aa[hi + 1] - h); n   } n   em -= aa[lo]; n   here += 0.5 * (1.0 - p) * em + 0.5 * (1.0 - p) * dfs(1, i, hi, s2); n   if (s2 == 1) n     em = min(aa[hi + 1] - aa[hi], h); n   else n     em = min(aa[hi + 1] - aa[hi] - h, h); n   here += 0.5 * (1.0 - p) * em + 0.5 * (1.0 - p) * dfs(s1, lo, hi - 1, 1); n   em = aa[hi] - h; n   for (i = hi - 1; i >= lo; i--) { n     if (aa[i] <= em) break; n     em = aa[i] - h; n   } n   if (i < lo) { n     if (s1 == 0) n       em = max(em, aa[lo - 1]); n     else n       em = max(em, aa[lo - 1] + h); n   } n   em = aa[hi] - em; n   here += 0.5 * p * em + 0.5 * p * dfs(s1, lo, i, 0); n   return dp[s1][lo][hi][s2] = here; n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cin >> n >> h >> p; n   for (int i = 1; i <= n; i++) cin >> aa[i]; n   sort(aa + 1, aa + n + 1); n   aa[0] = -1e9, aa[n + 1] = 1e9; n   cout << setprecision(9) << fixed << dfs(0, 1, n, 1) << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   cin >> n; n   int n1, n2, n3 = 0; n   bool flagm = true, flagr = true; n   while (n--) { n     cin >> n1 >> n2; n     if (n3 < n2 && n3 != 0) flagm = false; n     if (n1 != n2) flagr = false; n     n3 = n2; n   } n   if (!flagr) n     cout <<  rated ; n   else if (flagr && !flagm) n     cout <<  unrated ; n   else n     cout <<  maybe ; n } n


#include <bits/stdc++.h> n using namespace std; n int k[3], t[3]; n int n; n long long a[100000]; n long long was[100000]; n int main() { n   scanf( %d %d %d , &k[0], &k[1], &k[2]); n   scanf( %d %d %d , &t[0], &t[1], &t[2]); n   scanf( %d , &n); n   for (int i = 0; i < n; i++) { n     scanf( %I64d , &a[i]); n     was[i] = a[i]; n   } n   for (int i = 0; i < 3; i++) { n     for (int j = 0; j < k[i]; j++) { n       int pos = j; n       long long finished = -1000000; n       if (pos >= n) break; n       while (pos < n) { n         a[pos] = finished = max(finished + t[i], a[pos] + t[i]); n         pos += k[i]; n       } n     } n   } n   long long res = a[0] - was[0]; n   for (int i = 1; i < n; i++) n     if (res < a[i] - was[i]) res = a[i] - was[i]; n   printf( %I64d n , res); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, h; n int cnt; n int main() { n   cin >> n >> h; n   for (int e = 0; e < n; e++) { n     int in; n     cin >> in; n     if (in > h) { n       cnt += 2; n     } else { n       cnt++; n     } n   } n   cout << cnt; n } n


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


#include <bits/stdc++.h> n using namespace std; n using LL = long long; n using VI = vector<int>; n const int inf = 2e9; n LL calc(set<int>& st, int L, int R) { n   VI a; n   for (int i = L;;) { n     a.push_back(i); n     if (i == R) break; n     if (st.upper_bound(i) != st.end() && *st.upper_bound(i) < R) n       i = *st.upper_bound(i); n     else n       i = R; n   } n   int ans = 0; n   for (int i = 0; i + 1 < a.size(); i++) ans = max(ans, a[i + 1] - a[i]); n   return ans; n } n int main() { n   int n; n   cin >> n; n   set<int> a, b, c; n   for (int i = 0; i < n; i++) { n     int x; n     char ch; n     scanf( %d %c , &x, &ch); n     if (ch ==  B ) a.insert(x); n     if (ch ==  R ) b.insert(x); n     if (ch ==  P ) c.insert(x); n   } n   int B = 0, R = 0; n   int mn = inf, mx = -inf; n   if (a.size()) mn = min(mn, *a.begin()), mx = max(mx, *--a.end()); n   if (c.size()) mn = min(mn, *c.begin()), mx = max(mx, *--c.end()); n   if (mn != inf) B = mx - mn; n   mn = inf, mx = -inf; n   if (b.size()) mn = min(mn, *b.begin()), mx = max(mx, *--b.end()); n   if (c.size()) mn = min(mn, *c.begin()), mx = max(mx, *--c.end()); n   if (mn != inf) R = mx - mn; n   LL ans = (LL)B + R; n   if (!B || !R || c.empty()) { n     cout << ans << endl; n     return 0; n   } n   for (auto i = c.begin(); ++i != c.end(); i++) { n     auto j = i; n     i--; n     LL g = (*j - *i) - calc(a, *i, *j) - calc(b, *i, *j); n     if (g < 0) ans += g; n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int inf = 0x3f3f3f3f; n const int N = 1e6 + 10; n const int mod = 998244353; n int m, n, k = 0, flag = 0, cnt = 0; n int a[N], ans[N]; n long long pow2[N]; n bool did[N]; n int main() { n   ios::sync_with_stdio(false); n   cin >> n >> k; n   ; n   for (int i = 0; i < n; i++) { n     cin >> a[i]; n     if (a[i] <= k && !flag) { n       cnt++; n       did[i] = 1; n     } n     if (a[i] > k) flag = 1; n   } n   flag = 0; n   for (int i = n - 1; i >= 0; i--) { n     if (a[i] <= k && !flag && !did[i]) cnt++; n     if (a[i] > k) flag = 1; n   } n   cout << cnt << endl; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 100; n int cost[N][2], timee, tmm, num, clr, n; n char ha, arr[25]; n string tm1, tm2; n int main() { n   scanf(  %s , &arr); n   tm1 = arr; n   scanf(  %s%d , &arr, &n); n   tm2 = arr; n   while (n--) { n     scanf( %d %c%d %c , &timee, &tmm, &num, &clr); n     if (clr ==  y ) { n       if (tmm ==  h ) { n         cost[num][0]++; n       } else { n         cost[num][1]++; n       } n     } else { n       if (tmm ==  h ) { n         cost[num][0] += 2; n       } else { n         cost[num][1] += 2; n       } n     } n     if (cost[num][0] > 1) { n       cout << tm1 <<     << num <<     << timee <<   n ; n       cost[num][0] = -1e9; n     } n     if (cost[num][1] > 1) { n       cout << tm2 <<     << num <<     << timee <<   n ; n       cost[num][1] = -1e9; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n set<string> setao; n string s; n const int N = 10010; n int pd[N][4], n; n char solve(int k, int last) { n   if (k == n) return 1; n   if (n - k == 1 or k > n) return 0; n   if (pd[k][last] != -1) return pd[k][last]; n   string ant; n   for (int i = last; i >= 1; i--) { n     ant.push_back(s[k - i]); n   } n   string x; n   for (int i = 0; i < 2; i++) x.push_back(s[k + i]); n   char flag = 0; n   if (x != ant) { n     if (solve(k + 2, 2)) { n       setao.insert(x); n       flag = 1; n     } n   } n   if (n - k > 2) { n     x.push_back(s[k + 2]); n     if (x != ant) { n       if (solve(k + 3, 3)) { n         setao.insert(x); n         flag = 1; n       } n     } n   } n   return pd[k][last] = flag; n } n int main(void) { n   memset(pd, -1, sizeof pd); n   cin >> s; n   n = s.size(); n   for (int i = 5; i < n; i++) { n     solve(i, 0); n   } n   printf( %d n , (int)setao.size()); n   for (set<string>::iterator it = setao.begin(); it != setao.end(); it++) { n     cout << *it << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long int ara[1000000]; n int wow[7 * 22000]; n void make_wow(int x) { n   int xx; n   int l; n   int k = 1; n   stack<int> s; n   for (int i = 1; i <= x; i++) { n     xx = i; n     while (xx != 0) { n       l = xx % 10; n       xx /= 10; n       s.push(l); n     } n     while (!s.empty()) { n       wow[k++] = s.top(); n       s.pop(); n     } n   } n } n int find_block(int x, int b) { n   for (int i = 1; i <= b; i++) { n     if (ara[i] >= x) return i - 1; n   } n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   int q; n   cin >> q; n   int k = 1; n   int i = 1; n   int b = 1; n   long long int sum = 0, x, lb; n   while (ara[b - 1] <= 1000000000) { n     sum += log10(i++) + 1; n     ara[b] = ara[b - 1] + sum; n     b++; n   } n   make_wow(b); n   while (q--) { n     cin >> x; n     cout << wow[x - ara[find_block(x, b)]] << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n map<long long, vector<long long>> M; n bool res[100010]; n int main() { n   long long n; n   long long k; n   cin >> n >> k; n   long long S = 0; n   for (long long i = 1; i <= n; i++) { n     long long a; n     cin >> a; n     M[a].push_back(i); n     S += a; n   } n   if (S < k) { n     cout <<  -1 ; n     return (0); n   } n   if (S == k) return (0); n   long long lv_prev = 0; n   long long tmp = n; n   map<long long, vector<long long>>::iterator it = M.begin(); n   while (true) { n     long long lv = it->first; n     if (k >= (lv - lv_prev) * tmp) { n       k -= ((lv - lv_prev) * tmp); n       tmp -= it->second.size(); n     } else n       break; n     it++; n     lv_prev = lv; n   } n   long long dlt = k / tmp + lv_prev; n   k %= tmp; n   vector<pair<long long, long long>> V; n   for (; it != M.end(); it++) { n     for (long long i = 0; i < it->second.size(); i++) { n       V.push_back(make_pair(it->second[i], it->first - dlt)); n     } n   } n   sort(V.begin(), V.end()); n   cout <<   ; n   for (int i = 0; i < k; i++) { n     V[i].second -= 1; n     V.push_back(V[i]); n   } n   for (int i = k; i < V.size(); i++) n     if (V[i].second > 0) cout << V[i].first <<    ; n exit: n   return (0); n } n