#include <bits/stdc++.h> n using namespace std; n inline long long read() { n   long long num = 0, neg = 1; n   char c = getchar(); n   while (!isdigit(c)) { n     if (c ==  - ) neg = -1; n     c = getchar(); n   } n   while (isdigit(c)) { n     num = (num << 3) + (num << 1) + c -  0 ; n     c = getchar(); n   } n   return num * neg; n } n const long long mod = 998244353; n long long n, a[100010], m, g[300010], f[300010], ans; n inline long long qpow(long long x, long long k) { n   long long res = 1; n   while (k) { n     if (k & 1) res = res * x % mod; n     x = x * x % mod; n     k >>= 1; n   } n   return res; n } n int main() { n   n = read(); n   g[0] = n - 1; n   for (int i = 1; i <= n; i++) a[i] = read(), m += a[i]; n   for (int i = 1; i <= m; i++) n     g[i] = (m * (n - 1) % mod + (n - 1) * i % mod * g[i - 1] % mod) % mod * n            qpow(m - i, mod - 2) % mod; n   f[m] = g[m]; n   for (int i = m - 1; i >= 0; i--) f[i] = (f[i + 1] + g[i]) % mod; n   for (int i = 1; i <= n; i++) ans = (ans + f[a[i]]) % mod; n   ans = (ans - ((n - 1) * f[0] % mod) + mod) % mod; n   printf( %lld n , ans * qpow(n, mod - 2) % mod); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const double EPS = 1e-8; n const int maxn = 105; n int n, m, a, b, c; n char mat[maxn][maxn]; n void draw(int x, int y, char c) { n   mat[x][y] = mat[x + 1][y] = mat[x][y + 1] = mat[x + 1][y + 1] = c; n } n void gao() { n   if (n * m % 2 == 1) { n     puts( IMPOSSIBLE ); n     return; n   } n   int tot = a * 2 + b * 2 + c * 4; n   if (tot < n * m) { n     puts( IMPOSSIBLE ); n     return; n   } n   int i, j, k; n   memset(mat, 0, sizeof mat); n   for (i = 0; i < n / 2; ++i) { n     int x0 = i * 2; n     if (i % 2 == 0) { n       for (j = 0; j < m / 2; ++j) { n         int y0 = j * 2; n         draw(x0, y0, j % 2 == 1 ?  a  :  b ); n         --c; n       } n     } else { n       for (j = 0; j < m / 2; ++j) { n         int y0 = j * 2; n         draw(x0, y0, j % 2 == 1 ?  c  :  d ); n         --c; n       } n     } n   } n   if (n % 2 == 1 || m % 2 == 1) { n     if (n % 2 == 1) { n       for (i = 0; i < m / 2; ++i) { n         mat[n - 1][i * 2] = mat[n - 1][i * 2 + 1] = (i % 2 ?  e  :  f ); n         --a; n       } n     } else { n       for (i = 0; i < n / 2; ++i) { n         mat[i * 2][m - 1] = mat[i * 2 + 1][m - 1] = (i % 2 ?  e  :  f ); n         --b; n       } n     } n   } n   bool flg = 1; n   if (c < 0) { n     for (i = 0; i < n / 2 && c < 0; ++i) { n       int x0 = i * 2; n       if (i % 2 == 0) { n         for (j = 0; j < m / 2 && c < 0; ++j) { n           int y0 = j * 2; n           if (a > b) { n             a -= 2; n             if (j % 2 == 1) { n               mat[x0][y0] = mat[x0][y0 + 1] =  g ; n               mat[x0 + 1][y0] = mat[x0 + 1][y0 + 1] =  h ; n             } else { n               mat[x0][y0] = mat[x0][y0 + 1] =  x ; n               mat[x0 + 1][y0] = mat[x0 + 1][y0 + 1] =  y ; n             } n             ++c; n           } else { n             b -= 2; n             if (j % 2 == 1) { n               mat[x0][y0] = mat[x0 + 1][y0] =  g ; n               mat[x0][y0 + 1] = mat[x0 + 1][y0 + 1] =  h ; n             } else { n               mat[x0][y0] = mat[x0 + 1][y0] =  x ; n               mat[x0][y0 + 1] = mat[x0 + 1][y0 + 1] =  y ; n             } n             ++c; n           } n         } n       } else { n         for (j = 0; j < m / 2 && c < 0; ++j) { n           int y0 = j * 2; n           if (a > b) { n             a -= 2; n             if (j % 2 == 1) { n               mat[x0][y0] = mat[x0][y0 + 1] =  i ; n               mat[x0 + 1][y0] = mat[x0 + 1][y0 + 1] =  j ; n             } else { n               mat[x0][y0] = mat[x0][y0 + 1] =  k ; n               mat[x0 + 1][y0] = mat[x0 + 1][y0 + 1] =  l ; n             } n             ++c; n           } else { n             b -= 2; n             if (j % 2 == 1) { n               mat[x0][y0] = mat[x0 + 1][y0] =  i ; n               mat[x0][y0 + 1] = mat[x0 + 1][y0 + 1] =  j ; n             } else { n               mat[x0][y0] = mat[x0 + 1][y0] =  k ; n               mat[x0][y0 + 1] = mat[x0 + 1][y0 + 1] =  l ; n             } n             ++c; n           } n         } n       } n     } n   } n   if (a < 0 || b < 0 || c < 0) n     puts( IMPOSSIBLE ); n   else n     for (i = 0; i < n; ++i) puts(mat[i]); n } n int main() { n   while (cin >> n >> m >> a >> b >> c) { n     gao(); n   } n   return 0; n } n


#include <bits/stdc++.h> n const int pi = acos(-1); n const long long MOD = 1000000007; n const int M = 100005; n const int N = 1000006; n using namespace std; n int n, m, x, y, ans; n string s; n char a[55][55]; n bool vis[55][55]; n vector<string> v; n void calc(int i, string s) { n   if (i == 4) { n     v.push_back(s); n     return; n   } n   bool u = 0, d = 0, l = 0, r = 0; n   for (int i = 0; i < s.size(); i++) { n     if (s[i] ==  u ) u = 1; n     if (s[i] ==  d ) d = 1; n     if (s[i] ==  l ) l = 1; n     if (s[i] ==  r ) r = 1; n   } n   if (!u) calc(i + 1, s +  u ); n   if (!d) calc(i + 1, s +  d ); n   if (!l) calc(i + 1, s +  l ); n   if (!r) calc(i + 1, s +  r ); n } n int bfs(string k) { n   map<char, char> lol; n   lol[ 0 ] = k[0]; n   lol[ 1 ] = k[1]; n   lol[ 2 ] = k[2]; n   lol[ 3 ] = k[3]; n   queue<pair<int, pair<int, int> > > q; n   q.push(make_pair(0, make_pair(x, y))); n   while (!q.empty()) { n     int x = q.front().second.first; n     int y = q.front().second.second; n     int i = q.front().first; n     q.pop(); n     if (x < 0 || x >= n || y < 0 || y >= m) return 0; n     if (a[x][y] ==  # ) return 0; n     if (a[x][y] ==  E ) return 1; n     if (i >= s.size()) return 0; n     char lel = lol[s[i]]; n     if (lel ==  d ) q.push(make_pair(i + 1, make_pair(x + 1, y))); n     if (lel ==  u ) q.push(make_pair(i + 1, make_pair(x - 1, y))); n     if (lel ==  r ) q.push(make_pair(i + 1, make_pair(x, y + 1))); n     if (lel ==  l ) q.push(make_pair(i + 1, make_pair(x, y - 1))); n   } n   return 0; n } n int main() { n   cin >> n >> m; n   for (int i = 0; i < n; i++) scanf( %s , a[i]); n   cin >> s; n   calc(0,   ); n   for (int i = 0; i < n; i++) n     for (int j = 0; j < m; j++) n       if (a[i][j] ==  S ) { n         x = i; n         y = j; n       } n   for (int i = 0; i < v.size(); i++) ans += bfs(v[i]); n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n using namespace std; n bool solve(int); n int main() { n   int test = 0; n   while (solve(test++)) n     ; n   return 0; n } n const int N = 100 * 1000 + 5; n const int INF = int(1e9); n int n, b, m; n pair<int, int> a[N]; n struct edge { n   int to, f, c, rev; n }; n const int V = 1000 * 1000 + 3; n vector<edge> g[V]; n int lev[V]; n int head, tail, q[V]; n inline bool bfs(int s, int t) { n   memset(lev, 63, sizeof(lev)); n   head = tail = 0; n   lev[s] = 0; n   q[head++] = s; n   while (head != tail) { n     int v = q[tail++]; n     for (int i = 0; i < int(int(g[v].size())); ++i) n       if (g[v][i].f < g[v][i].c && lev[g[v][i].to] > lev[v] + 1) { n         lev[g[v][i].to] = lev[v] + 1; n         q[head++] = g[v][i].to; n       } n   } n   return lev[t] < INF / 2; n } n inline void add(int from, int to, int c) { n   edge f = {to, 0, c, int(g[to].size())}; n   edge b = {from, 0, 0, int(g[from].size())}; n   g[from].push_back(f); n   g[to].push_back(b); n } n int ptr[V]; n int dfs(int v, int t, int f) { n   if (v == t) return f; n   for (; ptr[v] < int(g[v].size()); ptr[v]++) { n     edge& e = g[v][ptr[v]]; n     if (e.f == e.c || lev[e.to] != lev[v] + 1) continue; n     int df = dfs(e.to, t, min(f, e.c - e.f)); n     if (df > 0) { n       e.f += df; n       g[e.to][e.rev].f -= df; n       return df; n     } n   } n   return 0; n } n inline int dinic(int s, int t) { n   int ans = 0; n   while (bfs(s, t)) { n     memset(ptr, 0, sizeof(ptr)); n     for (int f; (f = dfs(s, t, INF)) != 0; ans += f) n       ; n   } n   return ans; n } n int cnt5[N][5]; n int calc(int r, int rem) { return cnt5[r][rem]; } n bool solve(int) { n   if (!(cin >> m >> b >> n)) return false; n   for (int i = 0; i < int(n); ++i) { n     scanf( %d %d , &a[i].first, &a[i].second); n     a[i].first++; n   } n   a[n++] = pair<int, int>(b + 1, m); n   a[n++] = pair<int, int>(1, 0); n   int cv = 5; n   int S = cv++; n   int T = cv++; n   for (int i = 0; i < int(5); ++i) add(i, T, m / 5); n   for (int i = 0; i < int(N - 1); ++i) { n     for (int j = 0; j < int(5); ++j) cnt5[i + 1][j] = cnt5[i][j]; n     cnt5[i + 1][(i + 1) % 5]++; n   } n   sort(a, a + n); n   for (int i = 0; i < int(n - 1); ++i) { n     int lf = a[i].first; n     int rg = a[i + 1].first; n     int cnt = a[i + 1].second - a[i].second; n     if (rg - lf < cnt || cnt < 0) { n       puts( unfair ); n       return true; n     } n     if (cnt == 0) continue; n     int v[5]; n     for (int i = 0; i < int(5); ++i) { n       v[i] = calc(rg - 1, i) - calc(lf - 1, i); n       add(cv, i, v[i]); n     } n     add(S, cv, cnt); n     cv++; n   } n   puts(dinic(S, T) == m ?  fair  :  unfair ); n   return true; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m; n int a[100]; n long long ans; n long long getval() { n   long long val = 0; n   for (int i = 0; i < n; ++i) { n     int mn = 100500; n     for (int j = i; j < n; ++j) { n       mn = min(mn, a[j]); n       val += mn; n     } n   } n   return val; n } n int main() { n   ios_base::sync_with_stdio(0); n   cin >> n >> m; n   for (int i = 0; i < n; ++i) { n     a[i] = i + 1; n   } n   do { n     long long val = getval(); n     ans = max(ans, val); n   } while (next_permutation(a, a + n)); n   cerr <<  ans =   << ans <<   n ; n   do { n     long long val = getval(); n     if (val == ans) { n       if (m == 1) { n         for (int i = 0; i < n; ++i) { n           cout << a[i] <<    ; n         } n         cout <<   n ; n         return 0; n       } n       --m; n     } n   } while (next_permutation(a, a + n)); n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long n, t, x, y; n   long long pos_ji, pos_ou; n   while (~scanf( %lld%lld , &n, &t)) { n     while (t--) { n       scanf( %lld%lld , &x, &y); n       long long ji = n / 2 * n + (n + 1) / 2 + 1; n       long long ou = n / 2 * n + 1; n       pos_ji = (x - 1) * n + y; n       if (n % 2 != 0) { n         if (pos_ji % 2 == 0) n           printf( %lld n , ji + (pos_ji - 1) / 2); n         else n           printf( %lld n , (pos_ji + 1) / 2); n       } else { n         if (x % 2 != 0 && y % 2 != 0) { n           printf( %lld n , (x / 2) * n + (y + 1) / 2); n         } else if (x % 2 != 0 && y % 2 == 0) { n           printf( %lld n , ou + (x / 2) * n + (y - 1) / 2); n         } else if (x % 2 == 0 && y % 2 != 0) { n           printf( %lld n , ou + n / 2 + (x - 2) / 2 * n + y / 2); n         } else n           printf( %lld n , n / 2 + (x - 2) / 2 * n + y / 2); n       } n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(0); n   ; n   int precision = numeric_limits<double>::digits10; n   ; n   int n, a, b; n   cin >> n >> a >> b; n   cout << (a + b - 1 + 100 * n) % n + 1; n } n


#include <bits/stdc++.h> n using namespace std; n int q, tipe, temp, x, k, s; n vector<int> v; n set<int> cek; n int val[100000 + 5]; n map<pair<int, pair<int, int> >, int> temp2; n int gcd(int a, int b) { n   if (b == 0) return a; n   return gcd(b, a % b); n } n int main() { n   scanf( %d , &q); n   while (q--) { n     scanf( %d , &tipe); n     if (tipe == 1) { n       scanf( %d , &temp); n       if (!val[temp]) { n         val[temp] = true; n         cek.insert(temp); n         temp2.clear(); n       } n     } else { n       scanf( %d %d %d , &x, &k, &s); n       if (x % k != 0 or x >= s) n         printf( -1 n ); n       else { n         if (temp2[make_pair(x, make_pair(k, s))]) { n           printf( %d n , temp2[make_pair(x, make_pair(k, s))]); n           continue; n         } n         int xam = -1; n         int res = -1; n         auto its = cek.upper_bound(s - x); n         int it = *its; n         if (it <= 0 or it < (s - x) / k) { n           for (auto b = cek.begin(); b != its and (*b) + x <= s; ++b) { n             if (xam < ((*b) ^ x) and gcd(*b, x) % k == 0) { n               xam = ((*b) ^ x); n               res = *b; n             } n           } n         } else { n           for (int i = k; i <= 100000 and i + x <= s; i += k) { n             if (val[i] and xam < (i ^ x)) { n               xam = (i ^ x); n               res = i; n             } n           } n         } n         temp2[make_pair(x, make_pair(k, s))] = res; n         printf( %d n , res); n       } n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 200010; n pair<double, double> A[MAXN]; n int n, r, ord[MAXN], st[MAXN]; n bool comp(int a, int b) { n   if (A[a].first != A[b].first) return A[a].first < A[b].first; n   return A[a].second < A[b].second; n } n int ccw(pair<double, double> a, pair<double, double> b, n         pair<double, double> c) { n   double tmp = a.first * b.second + b.first * c.second + c.first * a.second - n                a.second * b.first - b.second * c.first - c.second * a.first; n   if (tmp > 0) return 1; n   if (tmp < 0) return -1; n   return 0; n } n int main() { n   scanf( %d , &n); n   int i, m, ni; n   for (i = 1; i <= n; i++) scanf( %lf%lf , &A[i].first, &A[i].second); n   ni = n; n   scanf( %d , &m); n   for (i = 1; i <= m; i++) scanf( %lf%lf , &A[n + i].first, &A[n + i].second); n   n += m; n   for (i = 1; i <= n; i++) ord[i] = i; n   sort(ord + 1, ord + n + 1, comp); n   st[++r] = ord[1]; n   st[++r] = ord[2]; n   for (i = 3; i <= n; i++) { n     while (r >= 2 && ccw(A[st[r - 1]], A[st[r]], A[ord[i]]) > 0) r--; n     st[++r] = ord[i]; n   } n   for (i = n - 1; i >= 1; i--) { n     while (r >= 2 && ccw(A[st[r - 1]], A[st[r]], A[ord[i]]) > 0) r--; n     st[++r] = ord[i]; n   } n   r--; n   for (i = 1; i <= r; i++) n     if (st[i] > ni) { n       printf( NO n ); n       return 0; n     } n   printf( YES n ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n char s[200001], s1[200001]; n int n, m, sum, gang, ans[200001], cnt; n int main() { n   scanf( %s , s1 + 1); n   m = strlen(s1 + 1); n   for (int i = 1; i <= m; i++) { n     if (s1[i] ==  (  || s1[i] ==  ) ) s[++n] = s1[i]; n     if (s1[i] ==  # ) { n       s[++n] =  ) ; n       s[++n] =  # ; n       cnt++; n     } n   } n   sum = 0; n   int now = cnt + 1; n   for (int i = n; i; i--) { n     if (s[i] ==  ) ) { n       gang++; n     } n     if (s[i] ==  ( ) { n       if (gang) n         gang--; n       else n         sum++; n     } n     if (s[i] ==  # ) { n       if (sum > 0) ans[now] = sum; n       sum = 0; n       now--; n     } n     if ((now > cnt) && (sum > 0)) exit(puts( -1 )); n   } n   if (gang) exit(puts( -1 )); n   ans[now] = sum; n   sum = 0; n   for (int i = 1; i <= cnt; i++) printf( %d n , ans[i] + 1); n } n


#include <bits/stdc++.h> n using namespace std; n long long n; n long long Tree[1000001]; n long long arr[200001]; n void add(long long x, long long val) { n   for (; x <= n; x += x & (-x)) Tree[x] += val; n   return; n } n long long get(long long x) { n   long long sum = 0; n   for (; x > 0; x -= x & (-x)) sum += Tree[x]; n   return sum; n } n void solve() { n   long long x = 1; n   if (get(1) == 0) { n     cout << 1 <<   n ; n     return; n   } n   while (x < n) { n     long long curr_val = get(x) * 2; n     if (get(x + 1) == curr_val) { n       cout << x + 1 <<   n ; n       return; n     } n     long long lo = x + 1; n     long long hi = n; n     long long lo_bound; n     while (lo <= hi) { n       if (lo == hi) { n         lo_bound = lo; n         break; n       } n       long long mi = (lo + hi) / 2; n       if (get(mi) < curr_val) n         lo = mi + 1; n       else n         hi = mi; n     } n     long long k = lo_bound - 1; n     x = (x == k) ? (k + 1) : k; n   } n   cout << -1 <<   n ; n } n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   long long q; n   memset(Tree, 0, sizeof Tree); n   cin >> n >> q; n   for (long long i = 1; i <= n; i++) { n     cin >> arr[i]; n     add(i, arr[i]); n   } n   long long x, y; n   while (q--) { n     cin >> x >> y; n     add(x, y - arr[x]); n     arr[x] = y; n     solve(); n   } n   return 0; n } n


#include<bits/stdc++.h> n #include<unordered_map> n using namespace std; n typedef long long ll; n typedef vector<ll> vll; n const ll M = 1000000007; n  n bool compare(vll x, vll y) { n     if (x[0] < y[0]) { n         return true; n     } n     return false; n } n void solve() { n     vector<vll> v(3); n     ll n1, n2, n3; n     cin >> n1 >> n2 >> n3; n     vll v1(n1), v2(n2), v3(n3); n     ll f1 = 0, f2 = 0; n     ll tsum = 0; n     for (ll i = 0; i < n1; i++) { n         cin >> v1[i]; n         tsum += v1[i]; n     } n     for (ll i = 0; i < n2; i++) { n         cin >> v2[i]; n         tsum += v2[i]; n     } n     for (ll i = 0; i < n3; i++) { n         cin >> v3[i]; n         tsum += v3[i]; n     } n     sort(v1.begin(), v1.end()); n     sort(v2.begin(), v2.end()); n     sort(v3.begin(), v3.end()); n     v[0] = v1; n     v[1] = v2; n     v[2] = v3; n     sort(v.begin(), v.end(), compare); n     // for (int i = 0; i < 3; i++) { n         for (int j = 1; j < v[0].size(); j++) { n             f1 += v[0][j]; n         } n         for (int j = 1; j < v[1].size(); j++) { n             f2 += v[1][j]; n         } n     // } n     ll sub = 0; n     if (f1 == 0) { n         sub = 2 * v[0][0]; n     } else if (f1 > 0 && f2 == 0) { n         if (v[1][0] > f1) { n             sub = 2 * v[0][0] + 2 * f1; n         } else { n             sub = 2 * v[1][0]; n         } n     } else { n         sub = 2 * min(f2, v[0][0]) + 2 * min(f1, v[1][0]); n     } n      n     if (v[2].size() == 1) { n         sub = min(sub , 2 * v[2][0]); n     } n     cout << tsum - sub << endl; n } n  n int main() { n     ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); n     // This macro supported by cc, cf, cses n     #ifndef ONLINE_JUDGE n         freopen( in.txt ,  r , stdin); n         freopen( out.txt ,  w , stdout); n     #endif n     // int tt; n     // cin >> tt; n     // for(int i = 1; i <= tt; i++) { n         solve(); n     // } n     return 0; n }


#include <bits/stdc++.h> n using namespace std; n const int MOD = 1e9 + 7; n int main() { n   cin.tie(0), cout.tie(0); n   ios::sync_with_stdio(false); n   int t; n   cin >> t; n   while (t--) { n     int u = 0, l = 0, d = 0; n     string s; n     cin >> s; n     for (int i = 0; i < s.size(); i++) { n       if (s[i] >=  a  && s[i] <=  z ) { n         l++; n       } n       if (s[i] >=  A  && s[i] <=  Z ) { n         u++; n       } n       if (s[i] >=  0  && s[i] <=  9 ) { n         d++; n       } n     } n     for (int i = 0; i < s.size(); i++) { n       if (s[i] >=  a  && s[i] <=  z  && l > 1) { n         if (!u) { n           s[i] =  A ; n           u++; n         } else if (!d) { n           s[i] =  0 ; n           d++; n         } n       } else if (s[i] >=  A  && s[i] <=  Z  && u > 1) { n         if (!l) { n           s[i] =  a ; n           l++; n         } else if (!d) { n           s[i] =  0 ; n           d++; n         } n       } else if (s[i] >=  0  && s[i] <=  9  && d > 1) { n         if (!l) { n           s[i] =  a ; n           l++; n         } else if (!u) { n           s[i] =  A ; n           u++; n         } n       } n     } n     cout << s << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   string s, s2; n   cin >> s; n   cin >> s2; n   vector<char> v, t; n   for (int i = 0; i < s.length(); i++) { n     v.push_back(s[i]); n   } n   vector<char>::iterator it; n   int i = 0; n   for (it = v.end() - 1; it >= v.begin(); it--) { n     t.push_back(*it); n   } n   int cnt = 0, n = s.length(); n   for (int i = 0; i < n; i++) { n     if (t[i] == s2[i]) cnt++; n   } n   if (cnt == n) n     cout <<  YES ; n   else n     cout <<  NO ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int32_t main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   ; n   long long n, sum; n   cin >> n >> sum; n   long long temp = 0; n   long long cnt = 0; n   while (temp < sum) { n     if (temp + n <= sum) { n       temp += n; n       cnt++; n     } else { n       n--; n     } n   } n   cout << cnt <<   n ; n   ; n   return 0; n } n


#include <bits/stdc++.h> n namespace competitive_programming { n inline namespace utility {} n namespace debugging {} n namespace io {} n }  // namespace competitive_programming n namespace competitive_programming::utility { n class timer { n  private: n   std::chrono::time_point<std::chrono::steady_clock> begin, end; n  n  public: n   timer() : begin(), end() {} n   ~timer() {} n }; n }  // namespace competitive_programming::utility n constexpr bool test_cases = true; n void yes() { std::cout <<  YES n ; } n void no() { std::cout <<  NO n ; } n auto solve() -> void { n   int64_t x, y; n   std::cin >> x >> y; n   if (x - y < 2) n     no(); n   else n     yes(); n } n int main() { n   std::ios::sync_with_stdio(false); n   std::cin.tie(nullptr); n   std::cout.precision(10); n   std::cerr.precision(10); n   std::cout << std::fixed << std::boolalpha; n   std::cerr << std::fixed << std::boolalpha; n   competitive_programming::timer t; n   int32_t cases = 1; n   if (test_cases) std::cin >> cases; n   while (cases--) solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m; n string s[2000]; n string t[2000]; n int main() { n   cin >> n >> m; n   for (int i = 0; i < n; i++) cin >> s[i]; n   for (int j = 0; j < m; j++) { n     bool flag = true; n     for (int i = 0; i < n - 1; i++) { n       if (s[i][j] > s[i + 1][j]) { n         flag = false; n         break; n       } n     } n     if (flag) { n       for (int i = 0; i < n; i++) t[i] += s[i][j]; n       for (int k = j + 1; k < m; k++) { n         flag = true; n         for (int f = 0; f < n - 1; f++) n           if (t[f] + s[f][k] > t[f + 1] + s[f + 1][k]) { n             flag = false; n             break; n           } n         if (flag) { n           for (int f = 0; f < n; f++) t[f] += s[f][k]; n         } n       } n       break; n     } n   } n   cout << m - t[0].length(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int t; n   cin >> t; n   while (t--) { n     long long n, m; n     cin >> n >> m; n     long long a[n][m], f1 = 0; n     for (long long i = 0; i < n; i++) { n       for (long long j = 0; j < m; j++) { n         cin >> a[i][j]; n         if (a[i][j] > 4) f1 = 1; n       } n     } n     if (f1) { n       cout <<  NO  << endl; n       continue; n     } n     for (long long i = 0; i < n; i++) { n       for (long long j = 0; j < m; j++) { n         long long c = a[i][j]; n         if ((i == 0 && j == 0) || (i == 0 && j == m - 1) || n             (i == n - 1 && j == m - 1) || (i == n - 1 && j == 0)) { n           if (c > 2) n             f1 = 1; n           else n             a[i][j] = 2; n         } else if ((i == n - 1 || i == 0 || j == 0 || j == m - 1)) { n           if (a[i][j] > 3) n             f1 = 1; n           else n             a[i][j] = 3; n         } else n           a[i][j] = 4; n       } n     } n     if (f1) { n       cout <<  NO  << endl; n       continue; n     } else { n       cout <<  YES  << endl; n       for (long long i = 0; i < n; i++) { n         for (long long j = 0; j < m; j++) { n           cout << a[i][j] <<    ; n         } n         cout << endl; n       } n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   long long int n, k, i; n   cin >> n >> k; n   vector<long long int> num(k + 1); n   long long int maxi = 1e18; n   long long int ind = -1; n   for (i = 1; i <= k; i++) { n     cin >> num[i]; n     if (n % num[i] < maxi) { n       maxi = n % num[i]; n       ind = i; n     } n   } n   cout << ind <<     << n / num[ind] << endl; n } n


#include <bits/stdc++.h> n using namespace std; n const int sz = 2e5 + 10; n int pr[sz], si[sz], ar[sz][3]; n bool us[sz]; n bool comp(int a, int b) { return ar[a][2] > ar[b][2]; } n int find_se(int v) { n   if (pr[v] == v) n     return v; n   else { n     pr[v] = find_se(pr[v]); n     return pr[v]; n   } n } n void unite(int v, int v2) { n   if (v != v2) { n     if (si[v] < si[v2]) swap(v, v2); n     pr[v2] = v, si[v] += si[v2]; n   } n } n int main() { n   int n, m, an = 0; n   cin >> n >> m; n   int p[m]; n   for (int a = 0; a < n; a++) { n     pr[a] = a, si[a] = 1, us[a] = 0; n   } n   for (int a = 0; a < m; a++) { n     p[a] = a; n     for (int b = 0; b < 3; b++) scanf( %d , &ar[a][b]); n     ar[a][0]--, ar[a][1]--; n   } n   sort(p, p + m, comp); n   for (int a = 0; a < m; a++) { n     int i = p[a]; n     int l = find_se(ar[i][0]), r = find_se(ar[i][1]), va = ar[i][2]; n     if (l == r) { n       if (us[l] == 0) { n         us[l] = 1, an += va; n       } n     } else if (us[l] == 1 and us[r] == 0) { n       us[r] = 1, an += va; n     } else if (us[l] == 0 and us[r] == 1) { n       us[l] = 1, an += va; n     } else if (us[l] == 0 and us[r] == 0) { n       unite(l, r), an += va; n     } n   } n   cout << an; n } n


#include <bits/stdc++.h> n using namespace std; n int n, x, y; n int a[1000]; n bool inrange(int v) { return (v >= x && v <= y); } n int main() { n   ios ::sync_with_stdio(false); n   cin >> n; n   for (int i = 1; i <= n; i++) { n     cin >> a[i]; n     a[i] = a[i - 1] + a[i]; n   } n   cin >> x >> y; n   for (int i = 1; i <= n; i++) n     if (inrange(a[i - 1]) && inrange(a[n] - a[i - 1])) { n       cout << i; n       return 0; n     } n   cout << 0; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, kangaroo[500000]; n int main() { n   cin >> n; n   for (int _n = n, i = 0; i < _n; ++i) cin >> kangaroo[i]; n   sort(kangaroo, kangaroo + n); n   int middle = (n / 2) - !(n % 2); n   int i = middle, j = n - 1; n   while (i >= 0 && j > middle) { n     if (kangaroo[i] * 2 <= kangaroo[j]) j--; n     i--; n   } n   cout << j + 1 << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long int mod = 1e9 + 7; n const long double PI = 3.141592653589793; n int main() { n   ios_base::sync_with_stdio(false); n   long long int t; n   cin >> t; n   long long int l = 0, r = 0; n   set<long long int> le, ri; n   vector<long long int> v(2e5 + 10); n   while (t--) { n     char a; n     long long int b; n     cin >> a >> b; n     if (l == 0 && r == 0) { n       v[b] = 1; n       l++; n       r++; n       continue; n     } n     if (a ==  L ) { n       l++; n       le.insert(b); n       v[b] = l; n     } else if (a ==  R ) { n       r++; n       ri.insert(b); n       v[b] = r; n     } else { n       auto k = le.find(b); n       if (k != le.end()) n         cout << min(l - (v[b] - 1), r + (v[b] - 1)) - 1; n       else n         cout << min(r - (v[b] - 1), l + (v[b] - 1)) - 1; n       cout <<   n ; n     } n   } n   cerr <<  Time Lapsed :   << 1.0 * clock() / CLOCKS_PER_SEC <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long int c[104][104]; n long long int dp[104][104 * 104]; n long long int mul[104][104]; n long long int pw(long long int x, long long int y) { n   long long int ans(1); n   while (y) { n     if (y & 1) ans = (ans * x) % 1000000007; n     x = (x * x) % 1000000007; n     y >>= 1; n   } n   return ans; n } n int main() { n   long long int n, m, k; n   cin >> n >> m >> k; n   c[1][0] = c[1][1] = 1; n   for (long long int i(2); i <= 100; ++i) { n     c[i][i] = c[i][0] = 1; n     c[i][1] = i; n     for (long long int j(2); j < i; ++j) { n       c[i][j] = (c[i - 1][j] + c[i - 1][j - 1]) % 1000000007; n     } n   } n   for (long long int i(0); i < n; ++i) { n     for (long long int j(0); j <= n; ++j) { n       mul[i][j] = pw(c[n][j], (m - i - 1) / n + 1); n     } n   } n   dp[0][0] = 1; n   for (long long int i(0); i < n; ++i) { n     for (long long int j(0); j <= k; ++j) { n       if (dp[i][j] == 0) continue; n       for (long long int kk(0); kk <= n && j + kk <= k; ++kk) { n         dp[i + 1][j + kk] = n             (dp[i + 1][j + kk] + dp[i][j] * mul[i][kk]) % 1000000007; n       } n     } n   } n   cout << dp[n][k] << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, len1, len2, ans; n string s; n int main() { n   cin >> n; n   getline(cin, s), getline(cin, s); n   while (len1 < s.size()) { n     len2 = len1 + n - 1; n     while (len2 > len1) n       if (s[len2] ==  .  || s[len2] ==  ?  || s[len2] ==  ! ) { n         len1 = len2 + 2; n         break; n       } else n         len2--; n     if (len2 == len1) return puts( Impossible ), 0; n     ans++; n   } n   cout << ans << endl; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 300005; n int n, ip[MAXN], p[MAXN], q[MAXN]; n int tree[4 * MAXN], flag[4 * MAXN]; n void push(int i, int L, int R) { n   if (L < R) { n     flag[2 * i] += flag[i]; n     flag[2 * i + 1] += flag[i]; n   } n   tree[i] += flag[i]; n   flag[i] = 0; n } n void update(int i, int L, int R, int u, int v, int val) { n   push(i, L, R); n   if (R < u || v < L) return; n   if (u <= L && R <= v) { n     flag[i] += val; n     push(i, L, R); n     return; n   } n   int mid = (L + R) / 2; n   update(2 * i, L, mid, u, v, val); n   update(2 * i + 1, mid + 1, R, u, v, val); n   tree[i] = max(tree[2 * i], tree[2 * i + 1]); n } n int main() { n   scanf( %d , &n); n   for (int i = 0; i < n; i++) { n     scanf( %d , &p[i]); n     p[i]--; n     ip[p[i]] = i; n   } n   int j = n - 1; n   update(1, 0, n - 1, 0, ip[j], 1); n   for (int i = 0; i < n; i++) { n     scanf( %d , &q[i]); n     q[i]--; n     if (i > 0) printf(   ); n     printf( %d , j + 1); n     if (i != n - 1) { n       update(1, 0, n - 1, 0, q[i], -1); n       while (true) { n         if (tree[1] > 0) { n           break; n         } else { n           j--; n           update(1, 0, n - 1, 0, ip[j], 1); n         } n       } n     } n   } n   printf(  n ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 1e6 + 5; n struct Node { n   int p; n   vector<int> v; n   Node() { p = 0, v = {1}; } n   inline int size() { return v.size(); } n }; n vector<int> G[maxn]; n int ans[maxn]; n void merge(Node& a, Node b) { n   b.v.push_back(0); n   if (a.size() < b.size()) swap(a, b); n   int shift = a.size() - b.size(); n   for (int i = 0; i < b.size(); i++) { n     a.v[i + shift] += b.v[i]; n     if (a.v[i + shift] >= a.v[a.p]) a.p = i + shift; n   } n } n Node dfs(int u, int fa) { n   Node cur; n   for (auto& v : G[u]) { n     if (v != fa) { n       merge(cur, dfs(v, u)); n     } n   } n   ans[u] = cur.size() - cur.p - 1; n   return cur; 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   for (int i = 1; i < n; i++) { n     int u, v; n     cin >> u >> v; n     G[u].push_back(v), G[v].push_back(u); n   } n   dfs(1, 0); n   for (int i = 1; i <= n; i++) { n     cout << ans[i] <<   n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int sad = 0; n int* visited; n vector<int>* adj; n void dfs(int curr, int prev) { n   visited[curr] = 1; n   for (auto& e : adj[curr]) { n     if (e == prev) continue; n     if (visited[e]) n       sad++; n     else n       dfs(e, curr); n   } n } n int main() { n   int n, k; n   cin >> n >> k; n   visited = new int[n]; n   memset(visited, 0, n * sizeof(int)); n   adj = new vector<int>[n]; n   unordered_set<string> chk; n   int dups = 0; n   for (int i = 0; i < k; i++) { n     int a, b; n     scanf( %d%d , &a, &b); n     int tmp = min(a, b); n     int tmp2 = max(a, b); n     string key = to_string(tmp) + to_string(tmp2); n     if (chk.count(key)) { n       dups++; n       continue; n     } n     chk.insert(key); n     adj[a - 1].push_back(b - 1); n     adj[b - 1].push_back(a - 1); n   } n   for (int i = 0; i < n; i++) { n     if (visited[i]) continue; n     dfs(i, -1337); n   } n   cout << sad / 2 + dups; n } n


#include <bits/stdc++.h> n using namespace std; n int vis[10100]; n int dist[10100]; n int pre[10100], cntt[30]; n char str[10100]; n struct Edge { n   int u, v, c, cost, next; n } edge[1000100]; n int anss, head[10100], cnt; n void init() { n   cnt = 0; n   memset(head, -1, sizeof(head)); n } n void addedge(int u, int v, int c, int cost) { n   edge[cnt].u = u; n   edge[cnt].v = v; n   edge[cnt].cost = cost; n   edge[cnt].c = c; n   edge[cnt].next = head[u]; n   head[u] = cnt++; n   edge[cnt].u = v; n   edge[cnt].v = u; n   edge[cnt].cost = -cost; n   edge[cnt].c = 0; n   edge[cnt].next = head[v]; n   head[v] = cnt++; n } n bool spfa(int begin, int end) { n   int u, v; n   queue<int> q; n   for (int i = 0; i <= end + 2; i++) { n     pre[i] = -1; n     vis[i] = 0; n     dist[i] = 99999999; n   } n   vis[begin] = 1; n   dist[begin] = 0; n   q.push(begin); n   while (!q.empty()) { n     u = q.front(); n     q.pop(); n     vis[u] = 0; n     for (int i = head[u]; i != -1; i = edge[i].next) { n       if (edge[i].c > 0) { n         v = edge[i].v; n         if (dist[v] > dist[u] + edge[i].cost) { n           dist[v] = dist[u] + edge[i].cost; n           pre[v] = i; n           if (!vis[v]) { n             vis[v] = true; n             q.push(v); n           } n         } n       } n     } n   } n   return dist[end] != 99999999; n } n int MCMF(int begin, int end) { n   int ans = 0, flow; n   int flow_sum = 0; n   while (spfa(begin, end)) { n     flow = 99999999; n     for (int i = pre[end]; i != -1; i = pre[edge[i].u]) n       if (edge[i].c < flow) flow = edge[i].c; n     for (int i = pre[end]; i != -1; i = pre[edge[i].u]) { n       edge[i].c -= flow; n       edge[i ^ 1].c += flow; n     } n     ans += dist[end] * flow; n     flow_sum += flow; n   } n   anss = flow_sum; n   return ans; n } n int main() { n   int n, m, a, b, c, cost, i, j, cc, len; n   scanf( %s , str); n   len = strlen(str); n   init(); n   for (i = 0; i < strlen(str); i++) cntt[str[i] - 97]++; n   scanf( %d , &n); n   for (i = n + 1; i <= n + 26; i++) addedge(i, n + 27, cntt[i - n - 1], 0); n   for (i = 1; i <= n; i++) { n     scanf( %s%d , str, &cc); n     addedge(0, i, cc, i); n     memset(cntt, 0, sizeof(cntt)); n     for (j = 0; j < strlen(str); j++) cntt[str[j] - 97]++; n     for (j = 0; j < 26; j++) addedge(i, n + 1 + j, cntt[j], 0); n   } n   int z = MCMF(0, n + 27); n   if (anss == len) n     printf( %d n , z); n   else n     printf( -1 ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 2e5 + 100; n char s[N]; n int n, m, t[N]; n int a[N * 4], b[N * 4], ab[N * 4], ba[N * 4], aba[N * 4], tag[N * 4]; n void pushup(int now) { n   a[now] = max(a[(now << 1)], a[(now << 1 | 1)]); n   b[now] = min(b[(now << 1)], b[(now << 1 | 1)]); n   ab[now] = max(ab[(now << 1)], n                 max(ab[(now << 1 | 1)], a[(now << 1)] - 2 * b[(now << 1 | 1)])); n   ba[now] = max(ba[(now << 1)], n                 max(ba[(now << 1 | 1)], a[(now << 1 | 1)] - 2 * b[(now << 1)])); n   aba[now] = max(max(aba[(now << 1)], aba[(now << 1 | 1)]), n                  max(ab[(now << 1)] + a[(now << 1 | 1)], n                      ba[(now << 1 | 1)] + a[(now << 1)])); n } n void build(int now, int l, int r) { n   if (l + 1 == r) { n     a[now] = b[now] = t[l]; n     ab[now] = ba[now] = -t[l]; n     return; n   } n   int mid = (l + r) >> 1; n   build((now << 1), l, mid); n   build((now << 1 | 1), mid, r); n   pushup(now); n } n void down(int now, int val) { n   a[now] += val; n   b[now] += val; n   ab[now] -= val; n   ba[now] -= val; n   tag[now] += val; n } n void pushdown(int now) { n   down((now << 1), tag[now]); n   down((now << 1 | 1), tag[now]); n   tag[now] = 0; n } n void Insert(int now, int l, int r, int a, int b, int val) { n   if (a <= l && b >= r - 1) { n     down(now, val); n     return; n   } n   int mid = (l + r) >> 1; n   pushdown(now); n   if (a < mid) Insert((now << 1), l, mid, a, b, val); n   if (b >= mid) Insert((now << 1 | 1), mid, r, a, b, val); n   pushup(now); n } n int main() { n   int x, y, z; n   scanf( %d%d , &n, &m); n   n = n * 2 - 2; n   scanf( %s , s + 1); n   for (int i = 1; i <= n; ++i) { n     if (s[i] ==  ( ) n       t[i] = t[i - 1] + 1; n     else n       t[i] = t[i - 1] - 1; n     Insert(1, 1, n + 1, i, i, t[i]); n   } n   printf( %d n , aba[1]); n   for (int i = 0; i < m; ++i) { n     scanf( %d%d , &x, &y); n     if (x > y) swap(x, y); n     if (s[x] ==  ( ) n       z = -2; n     else n       z = 2; n     Insert(1, 1, n + 1, x, y - 1, z); n     swap(s[x], s[y]); n     printf( %d n , aba[1]); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const double eps(1e-8), Inf(1e10); n const double pi(3.14159265358979); n const int N = 6060; n struct point { n   double x, y; n } p[N + N] = {}; n int n, q, k = 0, a, b, x[N] = {}; n double ans[N] = {}; n void init() { n   cin >> n >> q >> a >> b; n   for (int i = 1; i <= n; ++i) cin >> x[i]; n } n void work() { n   p[++k] = {1.0, 2.0 * (-x[1])}; n   p[++k] = {Inf, 2.0 * (Inf - x[1])}; n   ans[1] = x[1]; n   double zero = x[1]; n   for (int i = 2, d = 1; i <= n; ++i) { n     for (int j = k; j > d; --j) p[j + 2] = p[j]; n     k += 2; n     p[d + 1] = {zero, 0}; n     p[d + 2] = {zero, 0}; n     for (int j = 1; j <= d + 1; ++j) p[j].x += a; n     for (int j = d + 2; j <= k; ++j) p[j].x += b; n     for (int j = 1; j <= k; ++j) p[j].y += 2 * (p[j].x - x[i]); n     if (p[1].y > 0) n       zero = p[1].x, d = 0; n     else { n       for (d = 1; p[d].y * p[d + 1].y > 0; ++d) n         ; n       zero = (-p[d].y) / (p[d + 1].y - p[d].y) * (p[d + 1].x - p[d].x) + p[d].x; n     } n     ans[i] = zero; n   } n   ans[n] = min<double>(ans[n], q); n   for (int i = n - 1; i >= 1; --i) { n     ans[i] = max(ans[i + 1] - b, ans[i]); n     ans[i] = min(ans[i + 1] - a, ans[i]); n   } n   double sum = 0; n   for (int i = 1; i <= n; ++i) sum += (x[i] - ans[i]) * (x[i] - ans[i]); n   for (int i = 1; i <= n; ++i) printf( %.6f  , ans[i]); n   printf(  n%.6f n , sum); n } n int main() { n   init(); n   work(); n   return 0; n } n


#include <bits/stdc++.h> n int n, m, num; n char tab[100][100]; n int xx[10000][4]; n int yy[10000][4]; n int ch[100][100]; n int id[100][100]; n int getdir(int cd, int dd) { n   if (cd == 1) n     return (dd + 1) % 4; n   else n     return dd; n } n int main() { n   scanf( %d %d , &n, &num); n   for (int i = 0; i < n; i++) { n     scanf( %s , tab[i]); n     m = strlen(tab[i]); n     for (int j = 0; j < m; j++) { n       tab[i][j] -=  0 ; n     } n   } n   int count = 1; n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < m; j++) { n       if (tab[i][j] == 0) { n         id[i][j] = 0; n         continue; n       } n       if (i - 1 >= 0 && tab[i - 1][j] == tab[i][j]) { n         id[i][j] = id[i - 1][j]; n       } else if (j - 1 >= 0 && tab[i][j - 1] == tab[i][j]) { n         id[i][j] = id[i][j - 1]; n       } else { n         id[i][j] = count++; n         xx[id[i][j]][0] = i; n         yy[id[i][j]][0] = j; n         xx[id[i][j]][1] = i; n         yy[id[i][j]][3] = j; n       } n       if ((i + 1 >= n || tab[i][j] != tab[i + 1][j]) && n           (j + 1 >= m || tab[i][j] != tab[i][j + 1])) { n         yy[id[i][j]][1] = j; n         xx[id[i][j]][3] = i; n         xx[id[i][j]][2] = i; n         yy[id[i][j]][2] = j; n       } n     } n   } n   int tx, ty, cd = 0, dd = 1; n   int pos = id[tx][ty]; n   for (int p = 0; p < num; p++) { n     int dir = getdir(cd, dd); n     tx = xx[pos][dir]; n     ty = yy[pos][dir]; n     int dx, dy; n     if (dd == 0) { n       dx = -1; n       dy = 0; n     } n     if (dd == 1) { n       dx = 0; n       dy = 1; n     } n     if (dd == 2) { n       dx = 1; n       dy = 0; n     } n     if (dd == 3) { n       dx = 0; n       dy = -1; n     } n     if (tx + dx < 0 || tx + dx >= n || ty + dy < 0 || ty + dy >= m || n         id[tx + dx][ty + dy] == 0) { n       if (cd == 0) { n         cd = 1; n       } else { n         cd = 0; n         dd += 1; n         dd %= 4; n       } n     } else { n       pos = id[tx + dx][ty + dy]; n       tx += dx; n       ty += dy; n     } n   } n   printf( %d n , tab[tx][ty]); n } n


#include  bits/stdc++.h  n using namespace std; n using ll = long long; n using pii = pair<int,int>; n using pll = pair<ll,ll>; n template<typename T> n int sz(const T &a){return int(a.size());} n const int MN=601; n int dist[MN]; n bool used[MN]; n int arr[MN][MN]; n int main(){ n     cin.tie(NULL); n     ios_base::sync_with_stdio(false); n     int n,m,a,b,c; n     cin>>n>>m; n     for(int i=0;i<n;i++)for(int j=0;j<n;j++)arr[i][j]=INT_MAX; n     for(int i=1;i<=m;i++){ n         cin>>a>>b>>c; n         arr[a][b]=c; n     } n     for(int i=0;i<n;i++){ n         for(int j=0;j<n;j++)dist[j]=INT_MAX,used[j]=false; n         dist[i]=0; n         for(int level=1;level<n;level++){ n             int cur=-1; n             for(int j=0;j<n;j++){ n                 if(!used[j]&&(cur==-1||dist[j]<dist[cur]))cur=j; n             } n             used[cur]=true; n             int mi=INT_MAX; n             for(int j=0;j<n;j++)if(arr[cur][(j-dist[cur]%n+n)%n]!=INT_MAX)mi=min(mi,arr[cur][(j-dist[cur]%n+n)%n]+n-1-j); n             for(int j=0;j<n;j++){ n                 mi=min(mi+1,arr[cur][(j-dist[cur]%n+n)%n]); n                 dist[j]=min(dist[j],dist[cur]+mi); n             } n         } n         for(int j=0;j<n;j++)printf( %d%c ,dist[j],   n [j==n-1]); n     } n     return 0; n }


#include <bits/stdc++.h> n using namespace std; n const double Pi = acos(-1.0); n const int MAXN = 1e6 + 5; n int n, m, t; n int v[MAXN]; n char s[MAXN]; n int main() { n   cin >> t; n   while (t--) { n     cin >> n >> s + 1; n     int sum1 = 0, sum2 = 0, now = 0; n     for (int i = 1; i <= n * 2; i++) { n       if (s[i] ==  1 ) n         now++; n       else { n         sum1 += max(now - 1, 0); n         now = 0; n       } n     } n     if (now) sum1 += now - 1; n     now = 0; n     for (int i = 1; i <= n * 2; i++) { n       if (s[i] ==  0 ) n         now++; n       else { n         sum2 += max(now - 1, 0); n         now = 0; n       } n     } n     if (now) sum2 += now - 1; n     cout << max(sum1, sum2) << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int MAX = 300005; n pair<int, int> seg[2 * MAX]; n void update(int l, int r, int fight, int value) { n   for (l += MAX, r += MAX; l < r; l >>= 1, r >>= 1) { n     if (l & 1) seg[l++] = make_pair(fight, value); n     if (r & 1) seg[--r] = make_pair(fight, value); n   } n } n int query(int p) { n   pair<int, int> res = make_pair(INT_MAX, 0); n   for (p += MAX; p > 1; p >>= 1) res = min(res, seg[p]); n   return res.second; n } n int main() { n   int n, m; n   cin >> n >> m; n   vector<pair<pair<int, int>, int> > v; n   for (int i = 0; i < 2 * MAX; i++) seg[i] = make_pair(INT_MAX, 0); n   for (int i = 0; i < m; i++) { n     int a, b, x; n     cin >> a >> b >> x; n     v.push_back(make_pair(make_pair(a, b), x)); n   } n   for (int i = m - 1; i >= 0; i--) { n     int l = v[i].first.first; n     int r = v[i].first.second; n     int win = v[i].second; n     update(l - 1, win - 1, i, win); n     update(win, r, i, win); n   } n   for (int i = 0; i < n; i++) cout << query(i) <<    ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int *c; n int main() { n   int a, b, t, f, n, m, d, g, sum = 0; n   int mas[4]; n   cin >> mas[0] >> mas[1] >> mas[2] >> mas[3] >> a >> b; n   c = min_element(mas, mas + 4); n   *c = min(*c, b + 1); n   if (*c - a >= 0) n     cout << *c - a; n   else n     cout <<  0 ; n } n


#include <bits/stdc++.h> n using namespace std; n int main(int argc, char const *argv[]) { n   int t, n, m, x = 0, y = 0, flag = 0, cnt = 0, sum = 0, sum1 = 0, sum0 = 0, n                flag1 = 0; n   int a1, a2, a3, b1, b2, b3; n   vector<int> d; n   cin >> t; n   int a[t]; n   for (int i = 0; i < t; i++) { n     cin >> a[i]; n   } n   x = t - 1; n   while (x > 0 && a[x - 1] < a[x]) { n     x--; n   } n   cout << x <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, k, tab[70], p; n int main() { n   scanf( %d %d , &n, &k); n   for (int i = 0; i < n; i++) scanf( %d , &tab[i]); n   sort(tab, tab + n); n   if (k > n) { n     printf( -1 n ); n     return 0; n   } n   printf( %d %d n , tab[n - k], tab[n - k]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAX_N = int(1e6) + 10; n const int MAX_M = 21; n int n, r, c; n int a[MAX_N], b[MAX_N]; n int f[MAX_N][MAX_M]; n char buffer[MAX_N * 6]; n int main() { n   scanf( %d%d%d n , &n, &r, &c); n   c++; n   gets(buffer); n   int m = strlen(buffer); n   buffer[m++] =    ; n   for (int i = 0, j = 0; i < n; i++, j++) { n     b[i] = j; n     for (a[i] = 0; j < m && buffer[j] !=    ; j++, a[i]++) n       ; n   } n   b[n] = m; n   for (int i = 0, j = 0, sum = 0; i < n; i++) { n     for (; j < n && sum + a[j] + 1 <= c; sum += a[j++] + 1) n       ; n     f[i][0] = j - i; n     if (i < j) { n       sum -= a[i] + 1; n     } else { n       j++; n     } n   } n   for (int j = 1; 1 << j <= r; j++) { n     for (int i = 0; i < n; i++) { n       f[i][j] = f[i][j - 1] + f[i + f[i][j - 1]][j - 1]; n     } n   } n   int result = 0, start = 0, l = 0; n   for (; 1 << l <= r; l++) n     ; n   for (int i = 0; i < n; i++) { n     int sum = 0, tmp = r; n     for (int k = l; k >= 0; k--) { n       if (tmp - (1 << k) >= 0) { n         sum += f[i + sum][k]; n         tmp -= 1 << k; n       } n     } n     if (sum > result) { n       result = sum; n       start = i; n     } n   } n   for (int i = 0, p = start; i < r; i++) { n     int sum = 0; n     for (; p < n && (sum + a[p] + 1) <= c; sum += a[p++] + 1) { n       if (sum > 0) { n         putchar(   ); n       } n       for (int j = b[p]; j < b[p + 1] - 1; j++) { n         putchar(buffer[j]); n       } n     } n     if (sum > 0) { n       puts(  ); n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   cin >> n; n   int s[n + 1]; n   s[0] = 0; n   for (int i = 1; i <= n; i++) cin >> s[i]; n   int dp[n + 1][n + 1]; n   memset(dp, 0, sizeof(dp)); n   int ans = 0; n   for (int i = 1; i <= n; i++) { n     int prev = 0; n     for (int j = 0; j < i; j++) { n       dp[i][j] = dp[j][prev] + 1; n       if (s[i] == s[j]) prev = j; n       ans = max(ans, dp[i][j]); n     } n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int32_t MAX = 1000000 + 1; n int64_t ans[MAX]; n int main(int argc, char *argv[]) { n   ios_base::sync_with_stdio(false); n   ifstream in( input.txt ); n   ofstream out( output.txt ); n   int64_t n, q, k = 0, type, evenSh = 0, oddSh = 0, parity = 0, lastSh = 0; n   scanf( %I64d , &n); n   scanf( %I64d , &q); n   for (int32_t t = 0; t < q; ++t) { n     scanf( %I64d , &type); n     if (type == 1) { n       scanf( %I64d , &k); n       evenSh += k; n       oddSh += k; n       lastSh += k; n     } else { n       if ((parity + lastSh) % 2 == 0) { n         lastSh = 0; n         parity = 1; n         evenSh -= 1; n         oddSh += 1; n       } else { n         lastSh = 0; n         parity = 0; n         evenSh += 1; n         oddSh -= 1; n       } n     } n   } n   if (oddSh < 0) oddSh = n - ((-oddSh) % n); n   if (evenSh < 0) evenSh = n - ((-evenSh) % n); n   for (int64_t i = 0; i < n; ++i) n     if ((i + 1) % 2 == 1) n       ans[(i + oddSh) % n] = i + 1; n     else n       ans[(i + evenSh) % n] = i + 1; n   for (int64_t i = 0; i < n; ++i) printf( %I64d  , ans[i]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 101010; n int n, ans; n int pa[N << 1], ns[N], nt[N], ma[N], pos[N]; n vector<pair<int, int> > res; n char s[N], sr[N]; n void Manacher(char *s, int n, int *pa) { n   pa[0] = 1; n   for (int i = 1, j = 0; i < (n << 1) - 1; ++i) { n     int p = i >> 1, q = i - p, r = ((j + 1) >> 1) + pa[j] - 1; n     pa[i] = r < q ? 0 : min(r - q + 1, pa[(j << 1) - i]); n     while (0 <= p - pa[i] && q + pa[i] < n && s[p - pa[i]] == s[q + pa[i]]) n       pa[i]++; n     if (q + pa[i] - 1 > r) j = i; n   } n   for (int i = (0); i < (n); ++i) pa[i] = pa[i << 1]; n } n void kmp(char *s, int *ns, char *t, int *nt) { n   int lens = strlen(s); n   int lent = strlen(t); n   nt[0] = -1; n   for (int i = 0, j = -1; i < lens; ++i) { n     while (j >= 0 && s[i] != t[j + 1]) j = nt[j]; n     if (s[i] == t[j + 1]) ++j; n     ns[i] = j; n     if (j + 1 == lent) j = nt[j]; n   } n } n int main() { n   std::ios::sync_with_stdio(0); n   std::cin.tie(0); n   cin >> s; n   n = strlen(s); n   Manacher(s, n, pa); n   for (int i = (0); i < (n); ++i) sr[i] = s[n - 1 - i]; n   sr[n] =   0 ; n   kmp(sr + 1, nt + 1, sr, nt); n   kmp(s, ns, sr, nt); n   for (int i = (0); i < (n); ++i) n     if (~ns[i]) ++ns[i]; n   ma[0] = ns[0]; n   pos[0] = 0; n   for (int i = (1); i < (n); ++i) { n     ma[i] = ma[i - 1]; n     pos[i] = pos[i - 1]; n     if (ma[i] < ns[i]) { n       ma[i] = ns[i]; n       pos[i] = i; n     } n   } n   for (int i = (0); i < (n); ++i) { n     int k = i - pa[i]; n     int t = max(0, min(k < 0 ? 0 : ma[k], n - i - pa[i])); n     int c = pa[i] * 2 - 1 + t * 2; n     if (ans < c) { n       ans = c; n       res.clear(); n       res.push_back(make_pair(i - pa[i] + 1, pa[i] * 2 - 1)); n       if (t) { n         res.push_back(make_pair(n - t, t)); n         int o = pos[k] - 2 * ns[pos[k]] + t + 1; n         res.push_back(make_pair(o, t)); n       } n     } n   } n   sort(res.begin(), res.end()); n   cout << (int)res.size() <<   n ; n   for (auto u : res) cout << u.first + 1 <<     << u.second <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 100010; n int a[maxn], b[maxn]; n int main() { n   int n, i, j; n   cin >> n; n   for (i = 0; i < n; i++) cin >> j, a[j] = i; n   multiset<int> s; n   for (i = 0; i < n; i++) cin >> b[i], s.insert(i - a[b[i]]); n   for (int i = 0; i < n; i++) { n     multiset<int>::iterator it = s.lower_bound(i); n     int ans = maxn; n     if (it != s.end()) ans = min(ans, *it - i); n     if (it != s.begin()) ans = min(ans, i - *(--it)); n     printf( %d n , ans); n     it = s.find(i - a[b[i]]); n     s.erase(it); n     s.insert(i - a[b[i]] + n); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 100010; n const int sq = 320; n int n, q, p[N], r[N], dp[N]; n int f(int x) { n   int y = x; n   for (int i = 0; i < sq; i++) x = p[x]; n   return dp[y] = x; n } n int main() { n   ios::sync_with_stdio(0), cin.tie(0), cout.tie(0); n   cin >> n >> q; n   for (int i = 1; i <= n; i++) { n     cin >> p[i]; n     r[p[i]] = i; n   } n   for (int i = 1; i <= n; i++) f(i); n   while (q--) { n     int t, x, y; n     cin >> t >> x >> y; n     if (t == 1) { n       swap(r[p[x]], r[p[y]]); n       swap(p[x], p[y]); n       vector<int> v = {x, y}; n       for (int au : v) { n         int nxt = f(au); n         for (int i = 0; i < sq; i++) { n           dp[au] = nxt; n           au = r[au]; n           nxt = r[nxt]; n         } n       } n     } else { n       while (y >= sq) { n         y -= sq; n         x = dp[x]; n       } n       while (y) { n         y--; n         x = p[x]; n       } n       cout << x <<   n ; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n signed main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   long long n, k; n   cin >> n >> k; n   map<char, long long> mp; n   for (long long i = 0; i < n; i++) { n     char x; n     cin >> x; n     mp[x]++; n   } n   vector<pair<long long, char>> v; n   for (auto it : mp) { n     pair<long long, char> p; n     p.first = it.second; n     p.second = it.first; n     v.push_back(p); n   } n   sort(v.begin(), v.end(), greater<pair<long long, char>>()); n   long long ans = 0; n   for (long long i = 0; i < v.size() && k > 0; i++) { n     long long x = min(k, v[i].first); n     ans += (x * x); n     k -= x; n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n char s[] =  GB ; n int main() { n   int n, k, a, b; n   scanf( %d%d%d%d , &n, &k, &a, &b); n   if (a < b) { n     swap(a, b); n     swap(s[0], s[1]); n   } n   int num = (a - 1) / k + 1; n   if (b < num - 1) { n     puts( NO ); n     return 0; n   } n   while (num--) { n     int cnt = min(a, k); n     a -= cnt; n     while (cnt--) { n       putchar(s[0]); n     } n     cnt = 0; n     while (cnt < k && b > 0 && b > num - 1) { n       putchar(s[1]); n       ++cnt; n       --b; n     } n   } n   puts(  ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int t, a, b; n   scanf( %d , &t); n   while (t--) { n     scanf( %d%d , &a, &b); n     double output; n     if (b == 0) { n       printf( 1 n ); n       continue; n     } else if (a == 0) { n       printf( 0.5 n ); n       continue; n     } n     if (a < 4 * b) n       output = (double)(a + 8 * b) / 16.0 / b; n     else n       output = (double)(a - b) / (double)a; n     printf( %lf n , output); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int i, j, n, kol = 0; n   vector<int> a, b; n   cin >> n; n   for (i = 0; i < n; i++) { n     cin >> j; n     a.push_back(j); n     b.push_back(j); n   } n   sort(b.begin(), b.end()); n   for (i = 0; i < n; i++) n     if (a[i] != b[i]) kol++; n   if (kol == 0 || kol == 2) n     cout <<  YES ; n   else n     cout <<  NO ; n   return 0; n } n


#include <bits/stdc++.h> n const int MAXN = 200005; n int nn; n int x[MAXN]; n long long tt; n long long a[MAXN]; n long long mi[MAXN], mx[MAXN]; n long long ans[MAXN]; n int tag[MAXN]; n int main() { n   using namespace std; n   ios::sync_with_stdio(false); n   cin.tie(0); n   cin >> nn >> tt; n   for (int i = 1; i <= nn; i++) { n     cin >> a[i]; n     mi[i] = a[i] + tt; n     mx[i] = (long long)6e18; n   } n   for (int i = 1; i <= nn; i++) { n     cin >> x[i]; n     if (x[i] < i) { n       cout <<  No n ; n       return 0; n     } n     if (x[i] != nn) mx[x[i]] = a[x[i] + 1] + tt - 1; n     tag[i]++; n     tag[x[i]]--; n   } n   for (int i = 1; i <= nn; i++) { n     tag[i] += tag[i - 1]; n     if (tag[i] > 0 && i != nn) mi[i] = a[i + 1] + tt; n     if (mi[i] > mx[i] || ans[i - 1] + 1 > mx[i]) { n       cout <<  No n ; n       return 0; n     } n     ans[i] = max(mi[i], ans[i - 1] + 1); n   } n   cout <<  Yes n ; n   for (int i = 1; i <= nn; i++) cout << ans[i] <<    ; n   cout <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename A, typename B> n string to_string(pair<A, B> p); n template <typename A, typename B, typename C> n string to_string(tuple<A, B, C> p); n template <typename A, typename B, typename C, typename D> n string to_string(tuple<A, B, C, D> p); n string to_string(const string& s) { return     + s +    ; } n string to_string(const char* s) { return to_string((string)s); } n string to_string(bool b) { return (b ?  true  :  false ); } n string to_string(vector<bool> v) { n   bool first = true; n   string res =  { ; n   for (int i = 0; i < static_cast<int>(v.size()); i++) { n     if (!first) { n       res +=  ,  ; n     } n     first = false; n     res += to_string(v[i]); n   } n   res +=  } ; n   return res; n } n template <size_t N> n string to_string(bitset<N> v) { n   string res =   ; n   for (size_t i = 0; i < N; i++) { n     res += static_cast<char>( 0  + v[i]); n   } n   return res; n } n template <typename A> n string to_string(A v) { n   bool first = true; n   string res =  { ; n   for (const auto& x : v) { n     if (!first) { n       res +=  ,  ; n     } n     first = false; n     res += to_string(x); n   } n   res +=  } ; n   return res; n } n template <typename A, typename B> n string to_string(pair<A, B> p) { n   return  (  + to_string(p.first) +  ,   + to_string(p.second) +  ) ; n } n template <typename A, typename B, typename C> n string to_string(tuple<A, B, C> p) { n   return  (  + to_string(get<0>(p)) +  ,   + to_string(get<1>(p)) +  ,   + n          to_string(get<2>(p)) +  ) ; n } n template <typename A, typename B, typename C, typename D> n string to_string(tuple<A, B, C, D> p) { n   return  (  + to_string(get<0>(p)) +  ,   + to_string(get<1>(p)) +  ,   + n          to_string(get<2>(p)) +  ,   + to_string(get<3>(p)) +  ) ; n } n void debug_out() { cerr << endl; } n template <typename Head, typename... Tail> n void debug_out(Head H, Tail... T) { n   cerr <<     << to_string(H); n   debug_out(T...); n } n template <typename T> n T inverse(T a, T m) { n   T u = 0, v = 1; n   while (a != 0) { n     T t = m / a; n     m -= t * a; n     swap(a, m); n     u -= t * v; n     swap(u, v); n   } n   assert(m == 1); n   return u; n } n template <typename T> n class Modular { n  public: n   using Type = typename decay<decltype(T::value)>::type; n   constexpr Modular() : value() {} n   template <typename U> n   Modular(const U& x) { n     value = normalize(x); n   } n   template <typename U> n   static Type normalize(const U& x) { n     Type v; n     if (-mod() <= x && x < mod()) n       v = static_cast<Type>(x); n     else n       v = static_cast<Type>(x % mod()); n     if (v < 0) v += mod(); n     return v; n   } n   const Type& operator()() const { return value; } n   template <typename U> n   explicit operator U() const { n     return static_cast<U>(value); n   } n   constexpr static Type mod() { return T::value; } n   Modular& operator+=(const Modular& other) { n     if ((value += other.value) >= mod()) value -= mod(); n     return *this; n   } n   Modular& operator-=(const Modular& other) { n     if ((value -= other.value) < 0) value += mod(); n     return *this; n   } n   template <typename U> n   Modular& operator+=(const U& other) { n     return *this += Modular(other); n   } n   template <typename U> n   Modular& operator-=(const U& other) { n     return *this -= Modular(other); n   } n   Modular& operator++() { return *this += 1; } n   Modular& operator--() { return *this -= 1; } n   Modular operator++(int) { n     Modular result(*this); n     *this += 1; n     return result; n   } n   Modular operator--(int) { n     Modular result(*this); n     *this -= 1; n     return result; n   } n   Modular operator-() const { return Modular(-value); } n   template <typename U = T> n   typename enable_if<is_same<typename Modular<U>::Type, int>::value, n                      Modular>::type& n   operator*=(const Modular& rhs) { n     value = normalize(static_cast<int64_t>(value) * n                       static_cast<int64_t>(rhs.value)); n     return *this; n   } n   template <typename U = T> n   typename enable_if<is_same<typename Modular<U>::Type, long long>::value, n                      Modular>::type& n   operator*=(const Modular& rhs) { n     long long q = static_cast<long long>(static_cast<long double>(value) * n                                          rhs.value / mod()); n     value = normalize(value * rhs.value - q * mod()); n     return *this; n   } n   template <typename U = T> n   typename enable_if<!is_integral<typename Modular<U>::Type>::value, n                      Modular>::type& n   operator*=(const Modular& rhs) { n     value = normalize(value * rhs.value); n     return *this; n   } n   Modular& operator/=(const Modular& other) { n     return *this *= Modular(inverse(other.value, mod())); n   } n   friend const Type& abs(const Modular& x) { return x.value; } n   template <typename U> n   friend bool operator==(const Modular<U>& lhs, const Modular<U>& rhs); n   template <typename U> n   friend bool operator<(const Modular<U>& lhs, const Modular<U>& rhs); n   template <typename V, typename U> n   friend V& operator>>(V& stream, Modular<U>& number); n  n  private: n   Type value; n }; n template <typename T> n bool operator==(const Modular<T>& lhs, const Modular<T>& rhs) { n   return lhs.value == rhs.value; n } n template <typename T, typename U> n bool operator==(const Modular<T>& lhs, U rhs) { n   return lhs == Modular<T>(rhs); n } n template <typename T, typename U> n bool operator==(U lhs, const Modular<T>& rhs) { n   return Modular<T>(lhs) == rhs; n } n template <typename T> n bool operator!=(const Modular<T>& lhs, const Modular<T>& rhs) { n   return !(lhs == rhs); n } n template <typename T, typename U> n bool operator!=(const Modular<T>& lhs, U rhs) { n   return !(lhs == rhs); n } n template <typename T, typename U> n bool operator!=(U lhs, const Modular<T>& rhs) { n   return !(lhs == rhs); n } n template <typename T> n bool operator<(const Modular<T>& lhs, const Modular<T>& rhs) { n   return lhs.value < rhs.value; n } n template <typename T> n Modular<T> operator+(const Modular<T>& lhs, const Modular<T>& rhs) { n   return Modular<T>(lhs) += rhs; n } n template <typename T, typename U> n Modular<T> operator+(const Modular<T>& lhs, U rhs) { n   return Modular<T>(lhs) += rhs; n } n template <typename T, typename U> n Modular<T> operator+(U lhs, const Modular<T>& rhs) { n   return Modular<T>(lhs) += rhs; n } n template <typename T> n Modular<T> operator-(const Modular<T>& lhs, const Modular<T>& rhs) { n   return Modular<T>(lhs) -= rhs; n } n template <typename T, typename U> n Modular<T> operator-(const Modular<T>& lhs, U rhs) { n   return Modular<T>(lhs) -= rhs; n } n template <typename T, typename U> n Modular<T> operator-(U lhs, const Modular<T>& rhs) { n   return Modular<T>(lhs) -= rhs; n } n template <typename T> n Modular<T> operator*(const Modular<T>& lhs, const Modular<T>& rhs) { n   return Modular<T>(lhs) *= rhs; n } n template <typename T, typename U> n Modular<T> operator*(const Modular<T>& lhs, U rhs) { n   return Modular<T>(lhs) *= rhs; n } n template <typename T, typename U> n Modular<T> operator*(U lhs, const Modular<T>& rhs) { n   return Modular<T>(lhs) *= rhs; n } n template <typename T> n Modular<T> operator/(const Modular<T>& lhs, const Modular<T>& rhs) { n   return Modular<T>(lhs) /= rhs; n } n template <typename T, typename U> n Modular<T> operator/(const Modular<T>& lhs, U rhs) { n   return Modular<T>(lhs) /= rhs; n } n template <typename T, typename U> n Modular<T> operator/(U lhs, const Modular<T>& rhs) { n   return Modular<T>(lhs) /= rhs; n } n template <typename T, typename U> n Modular<T> power(const Modular<T>& a, const U& b) { n   assert(b >= 0); n   Modular<T> x = a, res = 1; n   U p = b; n   while (p > 0) { n     if (p & 1) res *= x; n     x *= x; n     p >>= 1; n   } n   return res; n } n template <typename T> n bool IsZero(const Modular<T>& number) { n   return number() == 0; n } n template <typename T> n string to_string(const Modular<T>& number) { n   return to_string(number()); n } n template <typename U, typename T> n U& operator<<(U& stream, const Modular<T>& number) { n   return stream << number(); n } n template <typename U, typename T> n U& operator>>(U& stream, Modular<T>& number) { n   typename common_type<typename Modular<T>::Type, long long>::type x; n   stream >> x; n   number.value = Modular<T>::normalize(x); n   return stream; n } n using ModType = int; n struct VarMod { n   static ModType value; n }; n ModType VarMod::value; n ModType& md = VarMod::value; n using Mint = Modular<VarMod>; n template <typename T> n vector<int> z_function(int n, const T& s) { n   vector<int> z(n, n); n   int l = 0, r = 0; n   for (int i = 1; i < n; i++) { n     z[i] = (i > r ? 0 : min(r - i + 1, z[i - l])); n     while (i + z[i] < n && s[z[i]] == s[i + z[i]]) { n       z[i]++; n     } n     if (i + z[i] - 1 > r) { n       l = i; n       r = i + z[i] - 1; n     } n   } n   return z; n } n template <typename T> n vector<int> z_function(const T& s) { n   return z_function((int)s.size(), s); n } n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   string s_; n   cin >> s_; n   string x_; n   cin >> x_; n   int n = (int)s_.size(); n   int m = (int)x_.size(); n   vector<int> s(n); n   for (int i = 0; i < n; i++) { n     s[i] = (int)(s_[i] -  0 ); n   } n   vector<int> x(m); n   for (int i = 0; i < m; i++) { n     x[i] = (int)(x_[i] -  0 ); n   } n   vector<int> seq = x; n   seq.insert(seq.end(), s.begin(), s.end()); n   auto z = z_function(seq); n   auto Check = [&](int l1, int r1, int l2, int r2) { n     int i = r1; n     int j = r2; n     int c = 0; n     for (int q = m - 1; q >= 0; q--) { n       if (i >= l1) { n         c += s[i]; n         i -= 1; n       } n       if (j >= l2) { n         c += s[j]; n         j -= 1; n       } n       int nc = 0; n       if (c >= 10) { n         c -= 10; n         nc = 1; n       } n       if (x[q] != c) { n         return; n       } n       c = nc; n     } n     if (c != 0 || i >= l1 || j >= l2) { n       return; n     } n     cout << l1 + 1 <<     << r1 + 1 <<   n ; n     cout << l2 + 1 <<     << r2 + 1 <<   n ; n     exit(0); n   }; n   { n     mt19937 rng( n         (unsigned int)chrono::steady_clock::now().time_since_epoch().count()); n     while (true) { n       md = (int)1e8 + rng() % (int)9e8; n       bool pr = true; n       for (int i = 2; i * i <= md; i++) { n         if (md % i == 0) { n           pr = false; n           break; n         } n       } n       if (pr) { n         break; n       } n     } n   } n   vector<Mint> p10(n + 1); n   p10[0] = 1; n   for (int i = 1; i <= n; i++) { n     p10[i] = p10[i - 1] * 10; n   } n   Mint inv_10 = 1 / Mint(10); n   vector<Mint> inv_p10(n + 1); n   inv_p10[0] = 1; n   for (int i = 1; i <= n; i++) { n     inv_p10[i] = inv_p10[i - 1] * inv_10; n   } n   vector<Mint> suf(n + 1); n   for (int i = n - 1; i >= 0; i--) { n     suf[i] = suf[i + 1] + p10[n - 1 - i] * s[i]; n   } n   vector<pair<int, int>> all_suf(n + 1); n   for (int i = 0; i <= n; i++) { n     all_suf[i] = make_pair(suf[i](), i); n   } n   sort(all_suf.begin(), all_suf.end()); n   Mint x_val = 0; n   for (int i = 0; i < m; i++) { n     x_val = x_val * 10 + x[i]; n   } n   for (int k = m; k >= m - 1 && k >= 1; k--) { n     for (int i = 0; i <= n - k; i++) { n       Mint me = (suf[i] - suf[i + k]) * inv_p10[n - (i + k)]; n       Mint goal = x_val - me; n       { n         auto Test = [&](int len) { n           if (len >= 1 && len <= m) { n             int j = i + k + len; n             if (j <= n) { n               if ((suf[i + k] - suf[j]) * inv_p10[n - j] == goal) { n                 Check(i, i + k - 1, i + k, j - 1); n               } n             } n           } n         }; n         if (k == m - 1) { n           Test(m - 1); n           Test(m); n         } else { n           int match = min(m, z[m + i]); n           Test(m - match); n           Test(m - match - 1); n         } n       } n       { n         Mint needle = goal * p10[n - i] + suf[i]; n         auto it = lower_bound(all_suf.begin(), all_suf.end(), n                               make_pair(needle(), -1)); n         while (it != all_suf.end() && it->first == needle) { n           int j = it->second; n           if (j < i && i - j <= m) { n             Check(j, i - 1, i, i + k - 1); n           } n           ++it; n         } n       } n     } n   } n   assert(false); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long N; n vector<int> E[100000]; n bool visit[100000] = {0}; n long long component[100000] = {0}; n int cN = 0; n stack<int> stk; n void DFS(int s) { n   if (visit[s]) return; n   visit[s] = true; n   stk.push(s); n   while (!stk.empty()) { n     int v = stk.top(); n     stk.pop(); n     component[cN]++; n     for (int i = (0); i < (E[v].size()); i++) { n       if (visit[E[v][i]]) continue; n       visit[E[v][i]] = true; n       stk.push(E[v][i]); n     } n   } n   cN++; n } n int main() { n   cin >> N; n   for (int i = (0); i < (N - 1); i++) { n     int a, b; n     char w[20]; n     scanf( %d%d%s , &a, &b, w); n     bool islucky = 1; n     int len = strlen(w); n     for (int i = (0); i < (len); i++) { n       if (w[i] ==  4  || w[i] ==  7 ) n         ; n       else n         islucky = 0; n     } n     if (islucky) continue; n     E[a - 1].push_back(b - 1); n     E[b - 1].push_back(a - 1); n   } n   for (int i = (0); i < (N); i++) DFS(i); n   long long ret = 0; n   for (int i = (0); i < (cN); i++) { n     ret += component[i] * (N - component[i]) * (N - component[i] - 1); n   } n   cout << ret << endl; n } n


#include<bits/stdc++.h> n using namespace std; n const int MAXN = 1010; n int vis[MAXN*2][MAXN*2],T,n,m,ans; n /* n void dfs(int x,int y,int step,int now){ n  if(step==n){ n    n   if(!vis[x+MAXN][y+MAXN]) ans++,vis[x+MAXN][y+MAXN] = 1; n   return; n  } n  if(now==1){ n   dfs(x+1,y,step+1,2); n   dfs(x-1,y,step+1,2); n  } n  else if(now==2){ n   dfs(x,y+1,step+1,1); n   dfs(x,y-1,step+1,1); n  } n  else{ n   dfs(x+1,y,step+1,2); n   dfs(x-1,y,step+1,2); n   dfs(x,y+1,step+1,1); n   dfs(x,y-1,step+1,1); n  } n } n */ n int main(){ n  int aa = -1; n   cin>>n; n   long long a1 = 3*n*(n+4)+2,a2 = n%2==0?aa*(n*(n + 4)+2):-aa*(n*(n + 4)+2); n   cout<<1 + (a1+a2)/8<<endl; n }


#include <bits/stdc++.h> n using namespace std; n vector<int> q, s; n int n, k; n vector<int> apply(vector<int> &a, vector<int> q) { n   vector<int> res(n); n   for (int i = 0; i < n; i++) { n     res[i] = a[q[i]]; n   } n   return res; n } n vector<int> rev_apply(const vector<int> &a, vector<int> &q) { n   vector<int> res(n); n   for (int i = 0; i < n; i++) { n     res[q[i]] = a[i]; n   } n   return res; n } n int main() { n   cin >> n >> k; n   q = vector<int>(n); n   s = vector<int>(n); n   for (int i = 0; i < n; i++) { n     cin >> q[i]; n     q[i]--; n   } n   for (int i = 0; i < n; i++) { n     cin >> s[i]; n     s[i]--; n   } n   vector<int> a(n), b, c; n   for (int i = 0; i < n; i++) { n     a[i] = i; n   } n   if (a == s) { n     cout <<  NO  << endl; n     return 0; n   } n   b = apply(a, q); n   c = rev_apply(a, q); n   if (b == s && c == s) { n     if (k == 1) { n       cout <<  YES  << endl; n     } else { n       cout <<  NO  << endl; n     } n     return 0; n   } n   b = a; n   c = a; n   int min_b = -1, min_c = -1; n   for (int i = 0; i < k; i++) { n     b = apply(b, q); n     c = rev_apply(c, q); n     if (b == s && min_b == -1) { n       min_b = i + 1; n     } n     if (c == s && min_c == -1) { n       min_c = i + 1; n     } n   } n   if (min_b >= 0 && min_b % 2 == k % 2) { n     cout <<  YES  << endl; n     return 0; n   } n   if (min_c >= 0 && min_c % 2 == k % 2) { n     cout <<  YES  << endl; n     return 0; n   } n   cout <<  NO  << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long sz = 3e5 + 10; n long long ara[sz], pos[sz], other[sz], mn[sz]; n int main() { n   long long n, m; n   cin >> n >> m; n   for (long long i = 1; i <= n; ++i) { n     scanf( %lld , &ara[i]); n     pos[ara[i]] = i; n   } n   for (long long i = 1; i <= n; ++i) other[i] = mn[i] = n + 1; n   for (long long i = 1; i <= m; ++i) { n     long long p1, p2; n     scanf( %lld , &p1), scanf( %lld , &p2); n     if (pos[p1] > pos[p2]) swap(p1, p2); n     other[pos[p1]] = min(other[pos[p1]], pos[p2]); n   } n   mn[n + 1] = n + 1; n   for (long long i = n; i >= 1; --i) mn[i] = min(mn[i + 1], other[i]); n   long long ans = 0; n   for (long long i = 1; i <= n; ++i) ans += (mn[i] - i); n   cout << ans <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL); n   int q; n   cin >> q; n   while (q--) { n     string s; n     cin >> s; n     vector<char> v[2]; n     for (int i = 0; i < s.size(); i++) v[s[i] % 2].push_back(s[i]); n     int p1, p2; n     for (p1 = 0, p2 = 0; p1 < v[0].size() && p2 < v[1].size();) { n       if (v[0][p1] < v[1][p2]) n         cout << v[0][p1++]; n       else n         cout << v[1][p2++]; n     } n     while (p1 < v[0].size()) cout << v[0][p1++]; n     while (p2 < v[1].size()) cout << v[1][p2++]; n     cout <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, k, temp; n   cin >> n >> k; n   int arr[n], sum[n]; n   for (int i = 0; i < n; i++) cin >> arr[i]; n   for (int i = 0; i < n; i++) { n     if (arr[i] == 0) n       sum[i] = 0; n     else n       sum[i] = sum[arr[i] - 1]; n     temp = i > k ? k : i; n     temp += i + k < n ? k : (n - i - 1); n     temp++; n     if (arr[i] + k > i - k && arr[i] != 0) { n       if (arr[i] + k > n) n         temp = 0; n       else if (i - k < 0) n         temp -= arr[i] + k; n       else n         temp -= arr[i] + k - i + k; n     } n     sum[i] += temp; n     cout << sum[i] <<    ; n   } n   cout << endl; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 200005; n pair<long long, char> s[MAXN], t[MAXN]; n int nxt[MAXN]; n void kmp(pair<long long, char> str[], int len) { n   memset(nxt, -1, sizeof(nxt)); n   for (int i = 0; i < len - 1; i++) { n     int p = nxt[i]; n     while (p != -1 && str[i + 1] != str[p + 1]) p = nxt[p]; n     nxt[i + 1] = p + (str[i + 1] == str[p + 1]); n   } n } n long long solve(pair<long long, char> s[], int lens, pair<long long, char> t[], n                 int lent) { n   long long res = 0; n   kmp(t, lent); n   int p = -1; n   if (lent == 0 && s[0].second == t[0].second && s[0].first >= t[0].first && n       s[-1].second == t[-1].second && s[-1].first >= t[-1].first) n     res++; n   for (int i = 0; i < lens; i++) { n     while (p != -1 && s[i] != t[p + 1]) p = nxt[p]; n     if (p + 1 < lent && s[i] == t[p + 1]) p++; n     if (p == lent - 1) { n       if (s[i + 1].second == t[p + 1].second && n           s[i + 1].first >= t[p + 1].first && n           s[i - p - 1].second == t[-1].second && n           s[i - p - 1].first >= t[-1].first) n         res++; n       p = nxt[p]; n     } n   } n   return res; n } n int main() { n   int n, m; n   scanf( %d%d , &n, &m); n   long long l; n   char c[5]; n   for (int i = 0; i < n; i++) { n     scanf( %I64d-%s , &l, c); n     s[i] = make_pair(l, *c); n   } n   for (int i = 0; i < m; i++) { n     scanf( %I64d-%s , &l, c); n     t[i] = make_pair(l, *c); n   } n   int now = 0; n   for (int i = 0; i + 1 < n; i++) { n     if (s[i + 1].second == s[now].second) n       s[now].first += s[i + 1].first; n     else n       s[++now] = s[i + 1]; n   } n   n = now + 1; n   now = 0; n   for (int i = 0; i + 1 < m; i++) { n     if (t[i + 1].second == t[now].second) n       t[now].first += t[i + 1].first; n     else n       t[++now] = t[i + 1]; n   } n   m = now + 1; n   if (m == 1) { n     long long res = 0; n     for (int i = 0; i < n; i++) n       if (s[i].second == t[0].second) n         res += max(0LL, s[i].first - t[0].first + 1); n     return 0 * printf( %I64d , res); n   } n   printf( %I64d , solve(s + 1, n - 2, t + 1, m - 2)); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const unsigned long long p = 31; n const unsigned long long m = 1e9 + 7; n bool compare(string a, string b) { n   sort(a.begin(), a.end()); n   sort(b.begin(), b.end()); n   int n = a.length(); n   unsigned long long hash_a = 0, hash_b = 0; n   unsigned long long p_pow = 1; n   for (int i = 0; i < n; i++) { n     hash_a = (hash_a + (a[i] -  a  + 1) * p_pow) % m; n     hash_b = (hash_b + (b[i] -  a  + 1) * p_pow) % m; n     p_pow = (p_pow * p) % m; n   } n   if (hash_a == hash_b) return true; n   return false; n } n bool check(string a, string b) { n   int blen = b.length(); n   int alen = a.length(); n   for (int i = 0; i + blen - 1 < alen; i++) { n     string aux = a.substr(i, blen); n     if (compare(b, aux)) return true; n   } n   return false; n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   int t; n   cin >> t; n   while (t--) { n     string a, b; n     cin >> a >> b; n     if (a.length() > b.length()) n       cout <<  NO  <<   n ; n     else if (check(b, a)) n       cout <<  YES  <<   n ; n     else n       cout <<  NO  <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 10101; n const int BIT = 30; n int N; n int arr[MAXN], brr[MAXN]; n bool isb[MAXN]; n vector<pair<int, int> > vans; n void action(int a, int b) { n   arr[b] ^= arr[a]; n   vans.push_back(make_pair(b + 1, a + 1)); n } n void aswap(int a, int b) { n   if (a == b) return; n   action(a, b); n   action(b, a); n   action(a, b); n } n void fail() { n   cout << -1 << endl; n   exit(0); n } n void calc() { n   int basis[BIT]; n   memset((basis), -1, sizeof(basis)); n   memset((isb), 0, sizeof(isb)); n   for (int i = 0; i < BIT; i++) { n     for (int j = 0; j < N; j++) { n       if (isb[j]) continue; n       if (arr[j] & (1 << i)) { n         basis[i] = j; n         isb[j] = true; n         break; n       } n     } n     if (basis[i] == -1) continue; n     for (int j = 0; j < N; j++) { n       if (j == basis[i]) continue; n       if (arr[j] & (1 << i)) { n         action(basis[i], j); n       } n     } n   } n   bool bused[BIT]; n   memset((bused), 0, sizeof(bused)); n   for (int i = 0; i < N; i++) { n     int x = brr[i]; n     for (int j = 0; j < BIT; j++) { n       if (basis[j] == -1) continue; n       if (x & (1 << j)) { n         x ^= arr[basis[j]]; n         if (!bused[j]) { n           bused[j] = true; n         } n       } n     } n     if (x) fail(); n   } n   for (int i = 0; i < N; i++) { n     if (arr[i]) continue; n     int x = brr[i]; n     for (int j = 0; j < BIT; j++) { n       if (basis[j] == -1) continue; n       if (x & (1 << j)) { n         x ^= arr[basis[j]]; n         action(basis[j], i); n       } n     } n   } n   int mat[BIT], mat2[BIT]; n   memset((mat), 0, sizeof(mat)); n   memset((mat2), 0, sizeof(mat2)); n   memset((bused), 0, sizeof(bused)); n   for (int i = 0; i < BIT; i++) { n     if (basis[i] == -1) continue; n     mat2[i] = 1 << i; n     int x = brr[basis[i]]; n     for (int j = 0; j < BIT; j++) { n       if (basis[j] == -1) continue; n       if (x & (1 << j)) { n         x ^= arr[basis[j]]; n         mat[i] ^= 1 << j; n       } n     } n   } n   for (int i = 0; i < BIT; i++) { n     if (basis[i] == -1) continue; n     int pv = -1; n     for (int j = 0; j < BIT; j++) { n       if (basis[j] == -1) continue; n       if (bused[j]) continue; n       if (mat[j] & 1 << i) { n         pv = j; n         bused[j] = true; n         break; n       } n     } n     if (pv == -1) continue; n     swap(bused[i], bused[pv]); n     swap(mat[i], mat[pv]); n     swap(mat2[i], mat2[pv]); n     for (int j = 0; j < BIT; j++) { n       if (basis[j] == -1) continue; n       if (j == i) continue; n       if (mat[j] & 1 << i) { n         mat[j] ^= mat[i]; n         mat2[j] ^= mat2[i]; n       } n     } n   } n   for (int i = 0; i < BIT; i++) { n     if (basis[i] == -1) continue; n     if (!(mat[i] & 1 << i)) { n       for (int j = 0; j < BIT; j++) { n         if (basis[j] == -1) continue; n         if (j == i) continue; n         if (mat[j] & 1 << i) action(basis[i], basis[j]); n       } n       action(basis[i], basis[i]); n     } n   } n   for (int i = 0; i < BIT; i++) { n     if (basis[i] == -1) continue; n     int pv = -1; n     for (int j = i; j < BIT; j++) { n       if (basis[j] == -1) continue; n       if (mat2[j] & 1 << i) { n         pv = j; n         break; n       } n     } n     swap(mat2[i], mat2[pv]); n     aswap(basis[i], basis[pv]); n     for (int j = 0; j < BIT; j++) { n       if (basis[j] == -1) continue; n       if (j == i) continue; n       if (mat2[j] & 1 << i) { n         mat2[j] ^= mat2[i]; n         action(basis[i], basis[j]); n       } n     } n   } n } n int main() { n   ios_base::sync_with_stdio(0); n   cin >> N; n   for (int i = 0; i < N; i++) cin >> arr[i]; n   for (int i = 0; i < N; i++) cin >> brr[i]; n   calc(); n   int A = vans.size(); n   cout << A <<   n ; n   for (__typeof(vans.begin()) it = vans.begin(); it != vans.end(); it++) { n     cout << it->first <<     << it->second <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 100001, BAR = 500, MOD = 1000000007; n int fac[N], inv[N]; n int powMod(int a, int n) { n   int ret = 1; n   while (n) { n     if (n & 1) { n       ret = (long long)ret * a % MOD; n     } n     a = (long long)a * a % MOD; n     n >>= 1; n   } n   return ret; n } n int binom(int n, int m) { n   return (long long)fac[n] * inv[m] % MOD * inv[n - m] % MOD; n } n int m; n char s[N]; n int ans[N]; n void prepare(int l) { n   int base = 1; n   for (int i = 0; i < N; ++i) { n     ans[i] = i < l ? 0 : (long long)binom(i - 1, l - 1) * base % MOD; n     if (i >= l) { n       base = (long long)base * 25 % MOD; n     } n   } n   for (int i = 0; i < N; ++i) { n     if (ans[i] >= MOD) { n       ans[i] -= MOD; n     } n     if (i + 1 < N) { n       ans[i + 1] += (long long)ans[i] * 26 % MOD; n     } n   } n } n int main() { n   fac[0] = 1; n   for (int i = 1; i < N; ++i) { n     fac[i] = (long long)fac[i - 1] * i % MOD; n   } n   inv[N - 1] = powMod(fac[N - 1], MOD - 2); n   assert((long long)inv[N - 1] * fac[N - 1] % MOD == 1); n   for (int i = N - 2; i >= 0; --i) { n     inv[i] = (long long)inv[i + 1] * (i + 1) % MOD; n   } n   scanf( %d , &m); n   scanf( %s , s); n   int len = strlen(s); n   if (len > BAR) { n     prepare(len); n   } n   while (m--) { n     int op; n     scanf( %d , &op); n     if (op == 1) { n       scanf( %s , s); n       len = strlen(s); n       if (len > BAR) { n         prepare(len); n       } n     } else { n       int n; n       scanf( %d , &n); n       if (len > BAR) { n         printf( %d n , ans[n]); n       } else { n         if (n < len) { n           printf( 0 n ); n         } else { n           int ans = powMod(26, n); n           int base = powMod(25, n - len + 1); n           for (int i = len - 1; i >= 0; --i) { n             ans -= (long long)binom(n, i) * base % MOD; n             if (ans < 0) { n               ans += MOD; n             } n             base = (long long)base * 25 % MOD; n           } n           printf( %d n , ans); n         } n       } n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n int main() { n   int n, t, i; n   scanf( %d , &t); n   for (i = 0; i < t; i++) { n     scanf( %d , &n); n     printf( %d n , n / 2); n   } n } n


#include <bits/stdc++.h> n using namespace std; n const long double PI = 3.14159265359; n const long long MOD = (long long)998244353ll; n const long long MAXN = (long long)3e5 + 10; n const long long INF = (long long)2242545357980376863; n const long double EPS = (long double)1e-8; n long long source[MAXN], d0[MAXN], dis[MAXN], done[MAXN], par[MAXN]; n vector<long long> C[MAXN]; n vector<pair<long long, long long> > G[MAXN]; n vector<pair<long long, pair<long long, long long> > > E; n set<pair<long long, long long> > dj; n void change_key(long long node, long long last, long long now) { n   dj.erase({last, node}); n   dis[node] = now; n   dj.insert({now, node}); n   return; n } n long long A[MAXN], B[MAXN], ans[MAXN]; n long long W; n long long get_par(long long u) { n   if (par[u] == u) return u; n   return par[u] = get_par(par[u]); n } n bool merge(long long u, long long v) { n   long long U = u, V = v; n   u = get_par(u); n   v = get_par(v); n   if (u == v) return false; n   if (C[u].size() < C[v].size()) swap(u, v); n   par[v] = u; n   for (auto x : C[v]) { n     C[u].push_back(x); n     if (get_par(A[x]) == get_par(B[x])) ans[x] = min(ans[x], W); n   } n   C[v].clear(); n   return true; n } n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   memset(ans, 31, sizeof ans); n   for (int i = 0; i < MAXN; i++) par[i] = i; n   memset(dis, 31, sizeof dis); n   memset(d0, 31, sizeof d0); n   long long n, m, k, q; n   scanf( %lld%lld%lld%lld , &n, &m, &k, &q); n   long long u, v, w; n   for (int i = 0; i < m; i++) { n     scanf( %lld%lld%lld , &u, &v, &w); n     G[u].push_back({v, w}); n     G[v].push_back({u, w}); n   } n   long long p, f; n   for (int i = 1; i <= k; i++) { n     p = i; n     source[p] = p; n     dj.insert({0, p}); n     dis[p] = 0; n   } n   long long fr, d, adj; n   while (dj.size()) { n     d = (*dj.begin()).first; n     fr = (*dj.begin()).second; n     dj.erase(dj.begin()); n     for (auto edge : G[fr]) { n       adj = edge.first; n       w = edge.second; n       if (done[adj] == 1) { n         E.push_back({dis[adj] + dis[fr] + w, {source[fr], source[adj]}}); n       } n       if (dis[adj] > d + w) { n         change_key(adj, dis[adj], d + w); n         source[adj] = source[fr]; n       } n     } n     done[fr] = 1; n   } n   for (int i = 0; i < q; i++) { n     scanf( %lld%lld , A + i, B + i); n     C[A[i]].push_back(i); n     C[B[i]].push_back(i); n   } n   sort(E.begin(), E.end()); n   for (auto edge : E) { n     W = edge.first; n     if (merge(edge.second.first, edge.second.second)) { n     } n   } n   for (int i = 0; i < q; i++) printf( %lld n , ans[i]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<pair<int, int> > adj[200001]; n int totalIn, totalOut; n int inEdges[200001]; n int outEdges[200001]; n void dfs(int u, int par, int in, int out) { n   inEdges[u] = in; n   outEdges[u] = out; n   for (pair<int, int> &p : adj[u]) { n     int v = p.first; n     int type = p.second; n     if (v != par) { n       if (type == 1) { n         dfs(v, u, in, out + 1); n         totalOut++; n       } else { n         dfs(v, u, in + 1, out); n         totalIn++; n       } n     } n   } n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   int n; n   cin >> n; n   int s, t; n   for (int i = 0; i < n - 1; i++) { n     cin >> s >> t; n     adj[s].push_back({t, 1}); n     adj[t].push_back({s, -1}); n   } n   dfs(1, -1, 0, 0); n   int in; n   vector<int> cities; n   int minIn = totalIn; n   cities.push_back(1); n   for (int i = 2; i <= n; i++) { n     in = totalIn - inEdges[i] + outEdges[i]; n     if (in < minIn) { n       minIn = in; n       cities.clear(); n       cities.push_back(i); n     } else if (in == minIn) { n       cities.push_back(i); n     } n   } n   cout << minIn <<   n ; n   for (int &i : cities) cout << i <<    ; n   cout <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long n, arr[26], s, k, fuck[19], ans; n vector<pair<long long, long long> > v1; n map<pair<long long, long long>, long long> maph; n void rek(long long x, long long r, long long z, long long o, bool p) { n   if (x >= r) { n     if (!p) n       v1.push_back({z, o}); n     else n       maph[{z, o}]++; n     return; n   } n   if (arr[x] + z <= s) rek(x + 1, r, z + arr[x], o, p); n   if (o < k and arr[x] <= 18 and z + fuck[arr[x]] <= s) n     rek(x + 1, r, z + fuck[arr[x]], o + 1, p); n   rek(x + 1, r, z, o, p); n } n int main() { n   cin >> n >> k >> s; n   fuck[0] = 1; n   for (int i = 1; i <= 18; i++) fuck[i] = fuck[i - 1] * i; n   for (int i = 0; i < n; i++) cin >> arr[i]; n   rek(0, n / 2, 0, 0, 0), rek(n / 2, n, 0, 0, 1); n   for (int i = 0; i < v1.size(); i++) { n     for (int j = 0; j <= k - v1[i].second; j++) { n       if (maph.find({s - v1[i].first, j}) != maph.end()) n         ans += maph[{s - v1[i].first, j}]; n     } n   } n   cout << ans; n } n


#include <bits/stdc++.h> n using namespace std; n int n, cnt, pos, sz, mx, last, start; n char s[2000005], ans[2000005]; n int main() { n   scanf( %d , &n); n   memset(ans,   000 , sizeof(ans)); n   for (register int i = 1; i <= n; i++) { n     scanf( %s , s); n     scanf( %d , &cnt); n     sz = strlen(s); n     for (register int j = 1; j <= cnt; j++) { n       scanf( %d , &pos); n       if (j == 1) { n         start = 0; n       } else { n         start = last + sz - pos; n         if (start < 0) { n           start = 0; n         } n       } n       for (register int k = start; k < sz; k++) { n         ans[k + pos] = s[k]; n       } n       last = pos; n       mx = max(mx, pos + sz - 1); n     } n   } n   while (ans[mx] ==   000 ) { n     mx--; n   } n   for (register int i = 1; i <= mx; i++) { n     if (ans[i] ==   000 ) { n       ans[i] =  a ; n     } n     printf( %c , ans[i]); n   } n   printf(  n ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int t; n   cin >> t; n   while (t--) { n     double n; n     cin >> n; n     double r = 1 / (2 * tan((360 / (4 * n)) * (3.1415926535 / 180))); n     cout << setprecision(15) << 2 * r << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int c = 0, m = 0, y = 0, w = 0, g = 0, b = 0, d, e; n   cin >> d >> e; n   char arr[d][e]; n   for (int i = 0; i < d; i++) { n     for (int j = 0; j < e; j++) { n       cin >> arr[i][j]; n       if (arr[i][j] ==  M ) { n         m++; n       } else if (arr[i][j] ==  C ) { n         c++; n       } else if (arr[i][j] ==  Y ) { n         y++; n       } else if (arr[i][j] ==  W ) { n         w++; n       } else if (arr[i][j] ==  G ) { n         g++; n       } else if (arr[i][j] ==  B ) { n         b++; n       } n     } n   } n   if (y == 0 && c == 0 && m == 0) { n     cout <<  #Black&White ; n   } else { n     cout <<  #Color ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   unsigned long long t; n   cin >> t; n   for (int ti = (int)(0); ti < (int)(t); ti++) { n     unsigned long long n; n     cin >> n; n     if (n % 2) { n       unsigned long long tmp = (n + 1u) * n; n       unsigned long long res; n       if (tmp % (4u * n) == 0) { n         res = n; n       } else if (tmp % (4u * n) == 2u * n) { n         res = n * 2u; n       } else { n         res = n * 4u; n       } n       cout << res + 1u << endl; n     } else { n       cout << n * 4u + 1 << endl; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int M = 2e6 + 10; n int a[M], b[M], c[M], d[M], e[M], f[M]; n vector<int> T[M]; n struct NTT { n   static const int G = 3, P = 998244353; n   int N, na, nb, w[2][M], rev[M]; n   long long kpow(long long a, int b) { n     long long c = 1; n     for (; b; b >>= 1, a = a * a % P) n       if (b & 1) c = c * a % P; n     return c; n   } n   void FFT(int *a, int f) { n     for (int i = (0); i < (N); i++) n       if (i < rev[i]) swap(a[i], a[rev[i]]); n     for (int i = 1; i < N; i <<= 1) n       for (int j = 0, t = N / (i << 1); j < N; j += i << 1) n         for (int k = 0, l = 0, x, y; k < i; k++, l += t) n           x = (long long)w[f][l] * a[j + k + i] % P, y = a[j + k], n           a[j + k] = (y + x) % P, a[j + k + i] = (y - x + P) % P; n     if (f) n       for (int i = 0, x = kpow(N, P - 2); i < N; i++) n         a[i] = (long long)a[i] * x % P; n   } n   void work() { n     int d = __builtin_ctz(N); n     w[0][0] = w[1][0] = 1; n     for (int i = 1, x = kpow(G, (P - 1) / N), y = kpow(x, P - 2); i < N; i++) { n       rev[i] = (rev[i >> 1] >> 1) | ((i & 1) << (d - 1)); n       w[0][i] = (long long)x * w[0][i - 1] % P, n       w[1][i] = (long long)y * w[1][i - 1] % P; n     } n   } n   void doit(int *a, int *b, int na, int nb) { n     for (N = 1; N < na + nb - 1; N <<= 1) n       ; n     for (int i = (na); i < (N); i++) a[i] = 0; n     for (int i = (nb); i < (N); i++) b[i] = 0; n     work(), FFT(a, 0), FFT(b, 0); n     for (int i = (0); i < (N); i++) a[i] = (long long)a[i] * b[i] % P; n     FFT(a, 1); n   } n   void get_inv(int *a, int *b, int na) { n     if (na == 1) { n       b[0] = kpow(a[0], P - 2); n       return; n     } n     get_inv(a, b, (na + 1) >> 1); n     for (N = 1; N < (na << 1); N <<= 1) n       ; n     work(); n     for (int i = (0); i < (na); i++) c[i] = a[i]; n     for (int i = (na); i < (N); i++) c[i] = 0; n     FFT(c, 0), FFT(b, 0); n     for (int i = (0); i < (N); i++) n       b[i] = 1ll * (2 - 1ll * c[i] * b[i] % P + P) % P * b[i] % P; n     FFT(b, 1); n     for (int i = (na); i < (N); i++) b[i] = 0; n   } n   void get_ln(int *a, int *b, int na) { n     for (int i = (0); i < (na - 1); i++) n       d[i] = (long long)(a[i + 1]) * (i + 1) % P; n     for (int i = (na); i < (N); i++) d[i] = 0; n     d[na - 1] = 0; n     get_inv(a, b, na); n     doit(b, d, na, na); n     for (int i = (na)-1; i >= (1); i--) n       b[i] = (long long)b[i - 1] * kpow(i, P - 2) % P; n     b[0] = 0; n   } n   void get_exp(int *a, int *b, int na) { n     if (na == 1) { n       b[0] = 1; n       return; n     } n     get_exp(a, b, (na + 1) >> 1); n     for (int i = 0; i < (na << 1); i++) f[i] = 0; n     get_ln(b, f, na); n     for (N = 1; N < (na << 1); N <<= 1) n       ; n     work(); n     f[0] = ((long long)a[0] + 1 - f[0] + P) % P; n     for (int i = (1); i < (na); i++) f[i] = ((long long)a[i] - f[i] + P) % P; n     FFT(f, 0); n     FFT(b, 0); n     for (int i = (0); i < (N); i++) b[i] = (long long)b[i] * f[i] % P; n     FFT(b, 1); n     for (int i = (na); i < (N); i++) b[i] = f[i] = 0; n   } n   void get_pow(int *a, int *b, int na, int k) { n     get_ln(a, e, na); n     for (int i = (0); i < (na); i++) e[i] = (long long)e[i] * k % P; n     get_exp(e, b, na); n   } n   void fast_pow(int *a, int *b, int na, int k, int maxn) { n     for (N = 1; N < max(((maxn + 1) << 1), (na << 1)); N <<= 1) n       ; n     for (int i = (na); i < (N); i++) a[i] = 0; n     for (int i = (0); i < (N); i++) b[i] = 0; n     b[0] = 1; n     work(); n     while (k) { n       FFT(a, 0); n       if (k & 1) { n         FFT(b, 0); n         for (int i = (0); i < (N); i++) b[i] = (long long)a[i] * b[i] % P; n         FFT(b, 1); n         for (int i = (max(na, maxn + 1)); i < (N); i++) b[i] = 0; n       } n       for (int i = (0); i < (N); i++) a[i] = (long long)a[i] * a[i] % P; n       FFT(a, 1); n       for (int i = (max(na, maxn + 1)); i < (N); i++) a[i] = 0; n       k >>= 1; n     } n   } n   priority_queue<pair<int, int> > H; n   void Merge(int m) { n     while (!H.empty()) H.pop(); n     for (int i = (1); i < (m + 1); i++) H.emplace(make_pair(-T[i].size(), i)); n     while (H.size() >= 2) { n       int x = H.top().second; n       H.pop(); n       int y = H.top().second; n       H.pop(); n       for (int i = (0); i < (T[x].size()); i++) a[i] = T[x][i]; n       for (int i = (0); i < (T[y].size()); i++) b[i] = T[y][i]; n       doit(a, b, T[x].size(), T[y].size()); n       int na = T[x].size() + T[y].size(); n       T[x].clear(); n       T[y].clear(); n       for (int i = (0); i < (na); i++) T[x].push_back(a[i]); n       H.emplace(-T[x].size(), x); n     } n   } n } ntt; n int F[10]; n int dis[300000 + 10][7][7]; n int find(int i) { n   if (F[i] == i) return i; n   return F[i] = find(F[i]); n } n int main() { n   string s, t; n   cin >> s >> t; n   int n = s.size(), m = t.size(); n   reverse(t.begin(), t.end()); n   for (int i = (0); i < (6); i++) { n     for (int j = (0); j < (6); j++) { n       if (i == j) continue; n       for (int k = (0); k < (n); k++) { n         if (s[k] == i +  a ) n           a[k] = 1; n         else n           a[k] = 0; n       } n       for (int k = (0); k < (m); k++) { n         if (t[k] == j +  a ) n           b[k] = 1; n         else n           b[k] = 0; n       } n       ntt.doit(a, b, n, m); n       for (int k = (m - 1); k < (n); k++) { n         dis[k][i][j] = a[k]; n       } n     } n   } n   for (int i = (m - 1); i < (n); i++) { n     for (int j = (0); j < (6); j++) F[j] = j; n     for (int j = (0); j < (6); j++) { n       for (int k = (0); k < (6); k++) { n         if (j == k) continue; n         if (dis[i][j][k]) F[find(j)] = find(k); n       } n     } n     int ans = 0; n     for (int j = (0); j < (6); j++) { n       if (F[j] == j) ans++; n     } n     cout << 6 - ans <<    ; n   } n   return 0; n } n


#include <bits/stdc++.h> n int a[105], stor[105]; n int main() { n   int n; n   scanf( %d , &n); n   int i; n   for (i = 0; i < n; i++) scanf( %d , &a[i]); n   int j, k = 0, max; n   max = a[2] - a[0]; n   for (i = 1; i < n - 1; i++) { n     if (i == 1) { n       max = a[2] - a[0]; n       for (j = 2; j < n - 1; j++) { n         if (a[j + 1] - a[j] > max) max = a[j + 1] - a[j]; n       } n     } else { n       max = a[1] - a[0]; n       for (j = 0; j < n; j++) { n         if (j == i) { n           if (a[j + 1] - a[j - 1] > max) max = a[j + 1] - a[j - 1]; n         } n         if (j + 1 == i) { n           if (a[j + 2] - a[j] > max) max = a[j + 2] - a[j]; n         } else if (a[j + 1] - a[j] > max) n           max = a[j + 1] - a[j]; n       } n     } n     stor[k++] = max; n   } n   int min = stor[0]; n   for (i = 0; i < k; i++) { n     if (stor[i] < min) min = stor[i]; n   } n   printf( %d , min); n   printf(  n ); n   return 0; n } n


#define _USE_MATH_DEFINES n #include <bits/stdc++.h> n using namespace std; n  n //template n #define rep(i,a,b) for(int i=(int)(a);i<(int)(b);i++) n #define ALL(v) (v).begin(),(v).end() n using ll=long long int; n const int inf = 0x3fffffff; const ll INF = 0x1fffffffffffffff; const double eps=1e-12; n template<typename T>inline bool chmax(T& a,T b){if(a<b){a=b;return 1;}return 0;} n template<typename T>inline bool chmin(T& a,T b){if(a>b){a=b;return 1;}return 0;} n //end n  n void solve(int _rot){ n    // printf( Case #%d:  ,_rot); n    int n; n    cin>>n; n    string s[3]; n    rep(i,0,3)cin>>s[i]; n    rep(_,0,3){ n       string S=s[_],T=s[(_+1)%3]; n       string res; n  n       int S0=0,T0=0; n       rep(i,0,n*2)if(S[i]== 0 )S0++; n       rep(i,0,n*2)if(T[i]== 0 )T0++; n       if((S0>n) and (T0<n))continue; n       if((S0<n) and (T0>n))continue; n       char mx; n       if(S0>=n and T0>=n)mx= 0 ; n       if(S0<=n and T0<=n)mx= 1 ; n  n       int i1=0,i2=0; n       for(;;){ n          int c1=0,c2=0; n          while(i1<n*2 and S[i1]!=mx){ n             i1++; c1++; n          } n          while(i2<n*2 and T[i2]!=mx){ n             i2++; c2++; n          } n          res+=string(max(c1,c2),mx^ 0 ^ 1 ); n          res+=mx; n          i1=min(i1+1,n*2); n          i2=min(i2+1,n*2); n          if(i1==n*2 and i2==n*2)break; n       } n       while(res.size()>n*3)res.pop_back(); n       while(res.size()<n*3)res+= 0 ; n       cout<<res<<  n ; n       return; n    } n } n  n int main(){ n    int t; scanf( %d ,&t); n    rep(rot,0,t)solve(rot+1); n    return 0; n }


#include <bits/stdc++.h> n using namespace std; n const double PI = acos(-1.0); n const int INF = 0x3f3f3f3f; n const double eps = 1e-8; n const long long mod = 10007; n const int N = 110; n int val[110]; n int main() { n   int n; n   scanf( %d , &n); n   for (int i = 1; i <= n; i++) { n     for (int j = i; j <= n; j++) { n       for (int k = i; k <= j; k++) { n         val[k]++; n       } n     } n   } n   int ans = -1; n   for (int i = 1; i <= n; i++) { n     ans = max(ans, val[i]); n   } n   printf( %d n , ans); n   return 0; n } n