#include <bits/stdc++.h> n using namespace std; n const int maxn = 1e6 + 17; n int N, root[maxn], sz[maxn]; n long long minv, maxv, val[maxn]; n vector<int> tree[maxn]; n vector<pair<long long, int> > nodes; n int find(int x) { n   if (root[x] == x) return x; n   return root[x] = find(root[x]); n } n long long merge(int first, int second) { n   int x = find(first), y = find(second); n   if (x == y) return 0LL; n   long long res = 1LL * sz[x] * sz[y] * val[first]; n   sz[x] += sz[y]; n   root[y] = x; n   return res; n } n int main() { n   scanf( %d , &N); n   for (int i = 1; i <= N; i++) n     scanf( %I64d , val + i), nodes.push_back(make_pair(val[i], i)); n   for (int i = 1; i < N; i++) { n     int A, B; n     scanf( %d %d , &A, &B); n     tree[A].push_back(B); n     tree[B].push_back(A); n   } n   sort(nodes.begin(), nodes.end()); n   for (int i = 0; i <= N; i++) root[i] = i, sz[i] = 1; n   for (int i = 0; i < N; i++) n     for (int v : tree[nodes[i].second]) n       if (val[nodes[i].second] >= val[v]) maxv += merge(nodes[i].second, v); n   for (int i = 0; i <= N; i++) root[i] = i, sz[i] = 1; n   for (int i = N - 1; i >= 0; i--) n     for (int v : tree[nodes[i].second]) n       if (val[nodes[i].second] <= val[v]) minv += merge(nodes[i].second, v); n   printf( %I64d n , maxv - minv); n   return 0; n } n


/** n   *  Author: dhruv_gheewala n   *  Problem: A. Spy Detected! n   *  Date:   12.04.2021 n   *  Time:   00:19:53 n **/ n  n #include  bits/stdc++.h  n using namespace std; n  n #ifndef DEBUG n  n #define fundri 108 n #define debug(...) 1729 n #define endl   n  n  n #endif n  n #define int int64_t n  n typedef pair<int, int> pii; n typedef vector<int> vi; n  n mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); n inline int rnd(int l = 0, int r = INT_MAX) {return uniform_int_distribution<int>(l, r)(rng);} n  n bool in_range(int x, int l, int r) {return l <= x && x <= r;} n template<typename H, typename ...T>void inp(H &head) {cin >> head;} n template<typename H, typename ...T>void inp(H &head, T &...tail) {cin >> head;inp(tail...);} n template<typename T>inline istream &operator >>(istream &in, vector<T> &a) {for(T &x : a)in >> x; return in;} n template<typename T, typename U>inline istream &operator >>(istream &in, pair<T, U> &a) {in >> a.first >> a.second; return in;} n  n // Multi-Dimension Vector n // Usage: vec<n, data-type> dp(dimention-1, dimention-2, ..., dimention-n, default = data-type()) n template<int D, typename T> struct vec : public vector<vec<D - 1, T>> { n     static_assert(D >= 1,  Vector dimensions must be greater than zero !! ); n     template<typename... Args> n     vec(int n = 0, Args... args) : vector<vec<D - 1, T>>(n, vec<D - 1, T>(args...)){} n }; n  n template<typename T> struct vec<1, T> : public vector<T> {vec(int n = 0, T val = T()) : vector<T>(n, val){}}; n  n const int inf = 1e15; n const bool testcases = true; n  n const bool ready = []() -> bool { n     ios_base::sync_with_stdio(false); n     cin.tie(nullptr); n  n #ifdef DHRUV_GHEEWALA n     freopen( in.txt ,  r , stdin); n     freopen( out.txt ,  w , stdout); n     freopen( debug.txt ,  w , stderr); n #endif n  n     return true; n }(); n  n void solve(int tc); n int32_t main(int32_t argc, char **argv) { n     int TC = 1; n     if(testcases) cin >> TC; n  n     // Pre-Processing goes here... n     for(int tc = 1; tc <= TC; ++tc) { n         solve(tc); n         fundri; n     } n     exit(0); n } n  n void solve(int tc) { n     int n; n     cin >> n; n  n     vector<int> a(n); n     cin >> a; n  n     map<int, int> mp; n     for(int x: a) n         mp[x]++; n  n     for(int i = 0; i < n; i++) { n         if(mp[a[i]] == 1) { n             cout << i + 1 << endl; n             return; n         } n     } n }


#include <bits/stdc++.h> n using namespace std; n int n, arr[110], ans[110]; n int primes[17] = {2,  3,  5,  7,  11, 13, 17, 19, 23, n                   29, 31, 37, 41, 43, 47, 53, 59}; n int fact[65]; n int memo[110][1 << 17]; n int find_min_cost(int ind, int mask) { n   if (ind >= n) return 0; n   if (memo[ind][mask] != -1) { n     return memo[ind][mask]; n   } n   memo[ind][mask] = 1 << 30; n   for (int i = 1; i < 2 * arr[ind]; i++) { n     if ((mask & fact[i]) == 0) { n       memo[ind][mask] = n           min(memo[ind][mask], n               abs(i - arr[ind]) + find_min_cost(ind + 1, (mask | fact[i]))); n     } n   } n   return memo[ind][mask]; n } n void compute_ans(int ind, int mask, int min_cost) { n   if (ind >= n) return; n   for (int i = 1; i < 2 * arr[ind]; i++) { n     if (((mask & fact[i]) == 0) and n         (min_cost == abs(i - arr[ind]) + memo[ind + 1][(mask | fact[i])])) { n       ans[ind] = i; n       compute_ans(ind + 1, (mask | fact[i]), memo[ind + 1][(mask | fact[i])]); n       return; n     } n   } n } n int main() { n   cin >> n; n   for (int i = 0; i < n; i++) cin >> arr[i]; n   memset(fact, 0, sizeof(fact)); n   for (int i = 2; i <= 62; i++) { n     for (int j = 0; j < 17; j++) { n       if ((i % primes[j]) == 0) fact[i] = (fact[i] | (1 << j)); n     } n   } n   for (int i = 0; i < n; i++) n     for (int j = 0; j < (1 << 17); j++) memo[i][j] = -1; n   for (int i = 0; i < (1 << 17); i++) memo[n][i] = 0; n   int min_cost = find_min_cost(0, 0); n   compute_ans(0, 0, min_cost); n   for (int i = 0; i < n; i++) cout << ans[i] <<    ; n   cout << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   int t; n   cin >> t; n   while (t--) { n     int a; n     cin >> a; n     int k = a; n     while (a--) { n       cout << 1 <<    ; n     } n     cout << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n  n int dx[4] = {0, 0, 1, -1}; n int dy[4] = {-1, 1, 0, 0}; n  n int lredge[600][599]; //n, m-1 n int tdedge[599][600]; //n-1, m n int n, m, k; n  n int distance(int i1, int j1, int i2, int j2){ n  //next cell is above n  if(i1 > i2){ n   return tdedge[i2][j1]; n  } n  //next cell is below n  else if(i2 > i1){ n   return tdedge[i1][j1]; n  } n  //next cell is to the left n  else if(j1 > j2){ n   return lredge[i1][j2]; n  } n  //next cell is to the right n  else if(j2 > j1){ n   return lredge[i1][j1]; n  } n  n  assert(1 == 0); n } n  n int dp[510][510][25]; n  n int32_t main(){ n  cin >> n >> m >> k; n  n  for(int i=0; i<510; i++) n   for(int j=0; j<510; j++) n    for(int k=1; k<25; k++) n     dp[i][j][k] = 1000000000; n  n  for(int i=0; i<n; i++){ n   for(int j=0; j<m-1; j++){ n    cin >> lredge[i][j]; n   } n  } n  n  for(int i=0; i<n-1; i++){ n   for(int j=0; j<m; j++){ n    cin >> tdedge[i][j]; n   } n  } n  n  if(k%2 == 1){ n   for(int i=0; i<n; i++){ n    for(int j=0; j<m; j++) n     cout << -1 <<    ; n    cout <<   n ; n   } n   return 0; n  } n  n  // for(int i=0; i<n; i++){ n  //  for(int j=0; j<m; j++){ n  //   for(int l=0; l<4; l++){ n  //    int newx = i + dx[l]; n  //    int newy = j + dy[l]; n  //    if(0 <= newx && newx <= n-1 && 0 <= newy && newy <= m-1){ n  //     dp[i][j][1] = min(dp[i][j][1], distance(i, j, newx, newy)); n  //    } n  //   } n  //  } n  // } n  n  // for(int i=0; i<n; i++){ n  //  for(int j=0; j<m; j++){ n  //   cout << dp[i][j][1] <<    ; n  //  } n  //  cout <<   n ; n  // } n  n  // for(int i=0; i<n; i++){ n  //  for(int j=0; j<m; j++){ n  //   cout << dp[i][j][0] <<    ; n  //  } n  //  cout <<   n ; n  // } n  n  for(int l=2; l<=k; l+=2){ n   for(int i=0; i<n; i++){ n    for(int j=0; j<m; j++){ n     for(int r=0; r<4; r++){ n      int newx = i + dx[r]; n      int newy = j + dy[r]; n      if(0 <= newx && newx <= n-1 && 0 <= newy && newy <= m-1){ n       dp[i][j][l] = min(dp[i][j][l], dp[newx][newy][l-2]+2*distance(i, j, newx, newy)); n      } n     } n    } n   } n  n   // debug( AA ); n   // for(int i=0; i<n; i++){ n   // for(int j=0; j<m; j++){ n   //  cout << dp[i][j][l] <<    ; n   // } n   // cout <<   n ; n  // } n  // cout <<   n ; n  } n  n  for(int i=0; i<n; i++){ n   for(int j=0; j<m; j++){ n    cout << dp[i][j][k] <<    ; n   } n   cout <<   n ; n  } n } n


#include <bits/stdc++.h> n long long sum = 0, dd = 0; n int N, R, V[(1 << 18) + 233]; n int main() { n   scanf( %d%d , &N, &R); n   dd = 1 << N; n   for (int i = 0; i < dd; ++i) { n     scanf( %d , V + i); n     sum += V[i]; n   } n   printf( %.10lf n , sum / (double)dd); n   for (int i = 0; i < R; ++i) { n     int z, g; n     scanf( %d%d , &z, &g); n     sum -= V[z]; n     V[z] = g; n     sum += g; n     printf( %.10lf n , sum / (double)dd); n   } n } n


#include <bits/stdc++.h> n #pragma GCC optimize( Ofast ) n #pragma GCC target( avx2 ) n #pragma GCC optimization( O3 ) n #pragma GCC optimization( unroll-loops ) n using namespace std; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   int n, q; n   cin >> n >> q; n   std::vector<vector<int>> v; n   v.resize(2, vector<int>(n, 0)); n   int ans = 0; n   while (q--) { n     int change = 0; n     int x, y; n     cin >> x >> y; n     x--; n     y--; n     if (v[x][y] == 0) n       change++; n     else n       change--; n     v[x][y] = 1 - v[x][y]; n     for (int i = -1; i <= 1; i++) { n       if (y + i >= n && y + i <= 0) continue; n       if (v[1 - x][y + i] == 1) ans += change; n     } n     if (ans == 0) n       cout <<  YES n ; n     else n       cout <<  NO n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n template <class T> n long long size(const T& x) { n   return x.size(); n } n template <class T> n T smod(T a, T b) { n   return (a % b + b) % b; n } n long long n, k; n vector<long long> X; n vector<pair<long long, pair<long long, long long> > > A; n vector<vector<long long> > mem; n long long dp(int at, int chosen) { n   if (at >= n) return 0; n   if (mem[at][chosen] != -1) return mem[at][chosen]; n   long long mx = 0; n   if (chosen < 2) { n     mx = max(mx, A[at].first + dp(A[at].second.second + 1, chosen + 1)); n   } n   mx = max(mx, dp(at + 1, chosen)); n   return mem[at][chosen] = mx; n } n void solve() { n   cin >> n >> k; n   mem = vector<vector<long long> >(n + 10, vector<long long>(3, -1)); n   X = vector<long long>(n); n   A = vector<pair<long long, pair<long long, long long> > >(n); n   for (long long i = (0); i < (n); ++i) { n     cin >> X[i]; n   } n   sort(X.begin(), X.end()); n   for (long long i = (0); i < (n); ++i) { n     long long a; n     cin >> a; n   } n   for (long long i = (0); i < (n); ++i) { n     auto r = upper_bound(X.begin(), X.end(), X[i] + k); n     int index = r - X.begin() - 1; n     A[i] = pair<long long, pair<long long, long long> >( n         index - i + 1, pair<long long, long long>(i, index)); n   } n   cout << dp(0, 0) << endl; n } n int main() { n   cin.sync_with_stdio(false); n   cin.tie(nullptr); n   cout.tie(nullptr); n   long long t; n   cin >> t; n   while (t--) solve(); n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, k; n   cin >> n >> k; n   int i = 1; n   while ((n * i) % 10 != k && (n * i) % 10 != 0) { n     i++; n   } n   cout << i << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int INF = 1000000007; n int n, m; n int fa[200010]; n struct Line { n   int u, v, c; n } l[200010]; n int p[200010]; n bool cmp(int x, int y) { return l[x].c < l[y].c; } n bool intree[200010]; n struct Edge { n   int v, id, nxt; n } e[400010]; n int tot; n int first[200010]; n void build(int u, int v, int id) { n   e[++tot] = (Edge){v, id, first[u]}; n   first[u] = tot; n   return; n } n int find(int x) { return fa[x] == x ? x : fa[x] = find(fa[x]); } n bool merge(int u, int v) { n   u = find(u); n   v = find(v); n   if (u == v) return false; n   fa[u] = v; n   return true; n } n void init() { n   for (int i = 1; i <= n; i++) fa[i] = i; n   for (int i = 1; i <= m; i++) p[i] = i; n   sort(p + 1, p + m + 1, cmp); n   for (int i = 1; i <= m; i++) { n     int x = p[i]; n     if (merge(l[x].u, l[x].v)) { n       intree[x] = true; n       build(l[x].u, l[x].v, x); n       build(l[x].v, l[x].u, x); n     } n   } n   return; n } n const int M = 20; n int f[200010][20]; n int g[200010][20]; n int h[200010][20]; n int dep[200010]; n int Id[200010]; n void dfs(int u, int lst, int lste) { n   Id[u] = lste; n   dep[u] = dep[lst] + 1; n   f[u][0] = lst; n   g[u][0] = l[lste].c; n   h[u][0] = INF; n   for (int i = 1; i < M; i++) { n     f[u][i] = f[f[u][i - 1]][i - 1]; n     g[u][i] = max(g[u][i - 1], g[f[u][i - 1]][i - 1]); n     h[u][i] = INF; n   } n   for (int i = first[u]; i; i = e[i].nxt) n     if (e[i].v != lst) dfs(e[i].v, u, e[i].id); n   return; n } n int ans[200010]; n void solve(int x) { n   int u = l[x].u; n   int v = l[x].v; n   int c = l[x].c; n   ans[x] = 0; n   if (dep[u] < dep[v]) swap(u, v); n   int need = dep[u] - dep[v], cur = 0; n   while (need) { n     if (need & 1) { n       ans[x] = max(ans[x], g[u][cur]); n       h[u][cur] = min(h[u][cur], c); n       u = f[u][cur]; n     } n     need >>= 1; n     cur++; n   } n   if (u == v) return; n   for (int i = M - 1; i >= 0; i--) n     if (f[u][i] != f[v][i]) { n       ans[x] = max(ans[x], g[u][i]); n       ans[x] = max(ans[x], g[v][i]); n       h[u][i] = min(h[u][i], c); n       h[v][i] = min(h[v][i], c); n       u = f[u][i]; n       v = f[v][i]; n     } n   ans[x] = max(ans[x], g[u][0]); n   ans[x] = max(ans[x], g[v][0]); n   h[u][0] = min(h[u][0], c); n   h[v][0] = min(h[v][0], c); n   return; n } n int main() { n   scanf( %d %d , &n, &m); n   for (int i = 1; i <= m; i++) scanf( %d %d %d , &l[i].u, &l[i].v, &l[i].c); n   init(); n   dfs(1, 0, 0); n   for (int i = 1; i <= m; i++) n     if (!intree[i]) solve(i); n   for (int i = M - 1; i > 0; i--) n     for (int u = 1; u <= n; u++) { n       h[u][i - 1] = min(h[u][i - 1], h[u][i]); n       h[f[u][i - 1]][i - 1] = min(h[f[u][i - 1]][i - 1], h[u][i]); n     } n   for (int u = 2; u <= n; u++) ans[Id[u]] = h[u][0]; n   for (int i = 1; i <= m; i++) n     if (ans[i] != INF) n       printf( %d  , ans[i] - 1); n     else n       printf( -1  ); n   printf(  n ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<int> e[400010], b[400010]; n int ll[400010], rr[400010], son[400010], No, an[400010], now; n void dfs0(int x, int last) { n   son[x] = 1; n   ll[x] = No++; n   for (int i = 0; i < e[x].size(); i++) { n     if (e[x][i] == last) continue; n     dfs0(e[x][i], x); n     son[x] += son[e[x][i]]; n   } n   rr[x] = No++; n } n int ac[400010 << 1], ha[400010 << 1]; n void insert(int d[], int id, int L, int R, int L2, int R2, int v) { n   if (L2 <= L && R <= R2) { n     d[id] += v; n     return; n   } n   int M = (L + R) >> 1; n   if (R2 <= M) n     insert(d, id << 1, L, M, L2, R2, v); n   else if (M < L2) n     insert(d, (id << 1) | 1, M + 1, R, L2, R2, v); n   else { n     insert(d, id << 1, L, M, L2, R2, v); n     insert(d, (id << 1) | 1, M + 1, R, L2, R2, v); n   } n } n int qq(int d[], int id, int L, int R, int x) { n   if (L == x && x == R) { n     return d[id]; n   } n   int M = (L + R) >> 1; n   if (x <= M) n     return d[id] + qq(d, id << 1, L, M, x); n   else n     return d[id] + qq(d, (id << 1) | 1, M + 1, R, x); n } n int qq2(int d[], int id, int L, int R, int L2, int R2) { n   if (L2 <= L && R <= R2) { n     return d[id]; n   } n   int M = (L + R) >> 1; n   if (R2 <= M) n     return qq2(d, id << 1, L, M, L2, R2); n   else if (M < L2) n     return qq2(d, (id << 1) | 1, M + 1, R, L2, R2); n   else n     return qq2(d, id << 1, L, M, L2, R2) + n            qq2(d, (id << 1) | 1, M + 1, R, L2, R2); n } n void insert2(int d[], int id, int L, int R, int x, int v) { n   d[id] += v; n   if (L == x && x == R) return; n   int M = (L + R) >> 1; n   if (x <= M) n     insert2(d, id << 1, L, M, x, v); n   else n     insert2(d, (id << 1) | 1, M + 1, R, x, v); n } n void dfs(int x, int last) { n   vector<int> tmp, tmp2; n   int i; n   for (i = 0; i < b[x].size(); i++) { n     if (qq(ac, 1, 1, No, ll[b[x][i]])) n       continue; n     else { n       tmp.push_back(b[x][i]); n       insert(ac, 1, 1, No, ll[b[x][i]], rr[b[x][i]], 1); n       int get_v = qq2(ha, 1, 1, No, ll[b[x][i]], rr[b[x][i]]); n       insert2(ha, 1, 1, No, ll[b[x][i]], son[b[x][i]] - get_v); n       tmp2.push_back(son[b[x][i]] - get_v); n       now += son[b[x][i]] - get_v; n     } n   } n   an[x] = now; n   for (int i = 0; i < e[x].size(); i++) { n     if (e[x][i] == last) continue; n     dfs(e[x][i], x); n   } n   for (i = 0; i < tmp.size(); i++) { n     insert(ac, 1, 1, No, ll[tmp[i]], rr[tmp[i]], -1); n     insert2(ha, 1, 1, No, ll[tmp[i]], -tmp2[i]); n     now -= tmp2[i]; n   } n } n int main() { n   No = 1; n   int i, j, k, n, m; n   scanf( %d%d , &n, &m); n   for (i = 1; i < n; i++) { n     int x, y; n     scanf( %d%d , &x, &y); n     e[x].push_back(y); n     e[y].push_back(x); n   } n   for (i = 0; i < m; i++) { n     int x, y; n     scanf( %d%d , &x, &y); n     b[x].push_back(x); n     b[x].push_back(y); n     b[y].push_back(x); n     b[y].push_back(y); n   } n   dfs0(1, 0); n   dfs(1, 0); n   for (i = 1; i <= n; i++) { n     if (i != 1) printf(   ); n     printf( %d , an[i] > 0 ? (an[i] - 1) : 0); n   } n   puts(  ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void solve() { n   long long n, v; n   cin >> n >> v; n   if (n >= v) { n     long long t = (n - v) * (n - v + 1); n     t /= 2; n     t += v - 1; n     cout << t; n   } else { n     cout << n - 1; n   } n } n int32_t main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   long long t; n   t = 1; n   while (t--) solve(); n   return 0; n } n


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


#include <bits/stdc++.h> n using namespace std; n const int N = 100010, C = 1000000000; n int n, num; n int kd[N], t[N], x[N], fr[N], nxt[N], ans[N]; n map<int, int> S; n int lc[N * 40], rc[N * 40], s[N * 40]; n void change(int &i, int l, int r, int k, int d) { n   if (i == 0) i = ++num; n   s[i] += d; n   if (l == r) return; n   int mid = (l + r) >> 1; n   if (k <= mid) change(lc[i], l, mid, k, d); n   if (k > mid) change(rc[i], mid + 1, r, k, d); n } n int query(int i, int l, int r, int rr) { n   if (i == 0) return 0; n   if (r <= rr) return s[i]; n   int mid = (l + r) >> 1, res = 0; n   res += query(lc[i], l, mid, rr); n   if (mid < rr) res += query(rc[i], mid + 1, r, rr); n   return res; n } n int main() { n   scanf( %d , &n); n   for (int i = 1; i <= n; i++) { n     scanf( %d%d%d , &kd[i], &t[i], &x[i]); n     fr[i] = 1; n     if (S.count(x[i])) { n       nxt[S[x[i]]] = i; n       fr[i] = 0; n     } n     S[x[i]] = i; n   } n   for (int i = 1; i <= n; i++) n     if (fr[i]) { n       for (int j = 1; j <= num; j++) lc[j] = rc[j] = s[j] = 0; n       num = 1; n       int rt = 1; n       for (int j = i; j; j = nxt[j]) { n         if (kd[j] == 1) change(rt, 1, C, t[j], 1); n         if (kd[j] == 2) change(rt, 1, C, t[j], -1); n         if (kd[j] == 3) ans[j] = query(1, 1, C, t[j]); n       } n     } n   for (int i = 1; i <= n; i++) n     if (kd[i] == 3) printf( %d n , ans[i]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long in() { n   long long a; n   scanf( %lld , &a); n   return a; n } n bool isprime(long long n) { n   if (n == 1) return 0; n   if (n == 2) return 1; n   if (n % 2) return 0; n   for (int i = 2; i <= sqrt(n) + 2; i++) n     if (n % i == 0) return 0; n   return 1; n } n long long dao(long long n) { n   for (int i = 2; i <= (sqrt(n)); i++) n     if (n % i == 0) return i; n } n int main() { n   long long a = in(), b = in(); n   if (a == b) cout << -1 << endl, exit(0); n   if (a == 2) { n     if (b & 1) n       cout << b << endl; n     else n       cout << --b << endl; n     exit(0); n   } n   for (long long i = b; i >= a; i--) { n     if (dao(i) > a || isprime(i)) cout << i << endl, exit(0); n   } n   cout << -1 << endl; n } n


#include <bits/stdc++.h> n using namespace std; n long long mod, n, m, a[200005], b[200005], kq, vt1, vt2, l, r, mid, mn[200005]; n map<int, bool> mp; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   cin >> n >> m; n   mod = 998244353; n   for (int i = 1; i <= n; ++i) { n     cin >> a[i]; n     mp[a[i]] = true; n   } n   for (int i = 1; i <= m; ++i) { n     cin >> b[i]; n     if (!mp[b[i]]) { n       cout << 0; n       return 0; n     } n   } n   for (int i = 1; i <= n; ++i) { n     if (a[i] < b[1]) { n       cout << 0; n       return 0; n     } n     if (a[i] == b[m]) vt1 = i; n   } n   for (int i = n; i > vt1; --i) n     if (a[i] < b[m]) { n       cout << 0; n       return 0; n     } n   mn[n] = a[n]; n   for (int i = n - 1; i > 0; --i) mn[i] = min(a[i], mn[i + 1]); n   kq = 1; n   for (int i = 1; i < m; ++i) { n     l = 1; n     r = n - 1; n     while (l <= r) { n       mid = (l + r) / 2; n       if (mn[mid + 1] >= b[i + 1]) { n         vt1 = mid; n         r = mid - 1; n       } else n         l = mid + 1; n     } n     l = 1; n     r = n - 1; n     while (l <= r) { n       mid = (l + r) / 2; n       if (mn[mid + 1] > b[i + 1]) n         r = mid - 1; n       else { n         vt2 = mid; n         l = mid + 1; n       } n     } n     kq = kq * max(0LL, vt2 - vt1 + 1) % mod; n     ; n   } n   cout << kq; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, r, x, s = 0; n   cin >> n >> r >> x; n   int a[r + 1]; n   for (int i = 1; i <= r; i++) { n     a[i] = 10000000; n   } n   while (n--) { n     int l, m; n     cin >> l >> m; n     a[l] = min(a[l], m); n   } n   for (int i = 1; i <= r; i++) { n     s += a[i]; n   } n   cout << min(s, x); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n __int128 read() { n   __int128 s = 0, f = 1; n   char ch = getchar(); n   while (ch <  0  || ch >  9 ) { n     if (ch ==  - ) f = -1; n     ch = getchar(); n   } n   while (ch >=  0  && ch <=  9 ) n     s = (s << 3) + (s << 1) + ch -  0 , ch = getchar(); n   return f * s; n } n struct node { n   __int128 x, y; n } a[200]; n int n; n __int128 abs(__int128 a) { return a > 0 ? a : -a; } n int main() { n   a[0].x = read(), a[0].y = read(); n   __int128 ax = read(), ay = read(), bx = read(), by = read(); n   for (; a[n].x + a[n].y <= 1e18; n++) n     a[n + 1].x = a[n].x * ax + bx, a[n + 1].y = a[n].y * ay + by; n   __int128 xs = read(), ys = read(), t = read(); n   int ans = 0; n   for (int s = 0, i; s <= n; s++) { n     __int128 d = 0, x = xs, y = ys; n     for (i = s; ~i && d <= t; i--) { n       d += abs(x - a[i].x) + abs(y - a[i].y), x = a[i].x, y = a[i].y; n     } n     ans = max(ans, d <= t ? s - i : s - i - 1); n     d = 0, x = xs, y = ys; n     for (i = s; i <= n && d <= t; i++) { n       d += abs(x - a[i].x) + abs(y - a[i].y), x = a[i].x, y = a[i].y; n     } n     ans = max(ans, d <= t ? i - s : i - s - 1); n   } n   printf( %d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n using ull = unsigned long long; n const int kMod = 1e9 + 7; n mt19937_64 rng(time(0)); n struct SegTree { n   struct Node { n     ull dp = 0; n     int l = 0, r = 0; n     ull hash = 0; n   }; n   int n; n   vector<Node> T; n   vector<ull> rnd; n   SegTree(int n) : n(n), T(1), rnd(n) { n     for (int i = 0; i < n; ++i) rnd[i] = rng(); n   } n   pair<int, int> add(int node, int b, int e, int pos) { n     if (e <= pos) return {node, 0}; n     if (b == pos && T[node].dp + 1 == 0) return {0, 1}; n     T.emplace_back(); n     int ret = T.size() - 1; n     if (e - b == 64) { n       T[ret].dp = T[node].dp; n       T[ret].dp += (1ULL << (pos - b)); n       int state = 1 + (T[ret].dp > T[node].dp); n       for (int i = b; i < e; ++i) n         if (T[ret].dp & (1ULL << (i - b))) T[ret].hash += rnd[i]; n       return {ret, state}; n     } n     int state; n     int l = T[node].l, r = T[node].r; n     int m = (b + e) / 2; n     tie(l, state) = add(l, b, m, pos); n     if (state == 0) n       tie(r, state) = add(r, m, e, pos); n     else if (state == 1) n       tie(r, state) = add(r, m, e, m); n     T[ret].l = l; n     T[ret].r = r; n     T[ret].hash = T[l].hash + T[r].hash; n     T[ret].dp = min(T[l].dp, T[r].dp); n     return {ret, state}; n   } n   int Add(int node, int val) { n     auto [ret, state] = add(node, 0, n, val); n     assert(state == 2); n     return ret; n   } n   int compare(int x, int y, int b, int e) { n     if (T[x].hash == T[y].hash) return 0; n     if (e - b == 64) return T[x].dp < T[y].dp ? -1 : 1; n     int m = (b + e) / 2; n     int ret = compare(T[x].r, T[y].r, m, e); n     if (ret) return ret; n     return compare(T[x].l, T[y].l, b, m); n   } n   int Compare(int a, int b) { return compare(a, b, 0, n); } n   void get(int node, int b, int e, int& ans) { n     if (e - b == 64) { n       for (int i = b; i < e; ++i) ans = (ans + ans) % kMod; n       ans = (ans + T[node].dp % kMod) % kMod; n     } else { n       int m = (b + e) / 2; n       get(T[node].r, m, e, ans); n       get(T[node].l, b, m, ans); n     } n   } n   int Get(int node) { n     int ans = 0; n     get(node, 0, n, ans); n     return ans; n   } n }; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   int n, m; n   cin >> n >> m; n   vector<vector<pair<int, int>>> graph(n); n   for (int i = 0; i < m; ++i) { n     int a, b, c; n     cin >> a >> b >> c; n     --a; n     --b; n     graph[a].emplace_back(c, b); n     graph[b].emplace_back(c, a); n   } n   SegTree st(1 << 17); n   int s, t; n   cin >> s >> t; n   --s; n   --t; n   auto cmp = [&](pair<int, int> a, pair<int, int> b) { n     return st.Compare(a.first, b.first) == 1; n   }; n   priority_queue<pair<int, int>, vector<pair<int, int>>, decltype(cmp)> pq(cmp); n   vector<int> dist(n, -1), parent(n, -1), vis(n, false); n   dist[s] = 0; n   pq.emplace(0, s); n   while (pq.size()) { n     auto [d, node] = pq.top(); n     pq.pop(); n     if (d != dist[node]) continue; n     if (node == t) break; n     vis[node] = true; n     for (auto [cost, vec] : graph[node]) { n       if (vis[vec]) continue; n       int nd = st.Add(d, cost); n       if (dist[vec] == -1 || st.Compare(nd, dist[vec]) == -1) { n         dist[vec] = nd; n         parent[vec] = node; n         pq.emplace(nd, vec); n       } n     } n   } n   if (dist[t] == -1) { n     cout << -1 <<   n ; n   } else { n     vector<int> trace; n     for (int node = t; node != -1; node = parent[node]) trace.push_back(node); n     reverse(trace.begin(), trace.end()); n     cout << st.Get(dist[t]) << endl; n     cout << trace.size() << endl; n     for (auto x : trace) cout << x + 1 <<    ; n     cout << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int t, n, a[11]; n int main() { n   scanf( %d , &t); n   while (t--) { n     scanf( %d , &n); n     int day = 0; n     for (int i = 0; i < 7; i++) scanf( %d , a + i), day += a[i]; n     int week = (n - 1) / day, best = 7, left = n - week * day; n     for (int i = 0; i < 7; i++) { n       int tot = 0, cur = 7; n       for (int j = 0; j < 7; j++) n         if (a[j] && ++tot == left) { n           cur = j; n           break; n         } n       best = min(best, cur + 1); n       rotate(a, a + 1, a + 7); n     } n     printf( %d n , week * 7 + best); n   } n } n


#include <bits/stdc++.h> n using namespace std; n vector<char> ch1; n int main() { n   string s, str =   ; n   getline(cin, s); n   for (int i = 0; i < s.length(); i++) { n     if (s[i] ==  . ) { n       ch1.push_back( - ); n       i = i + 2; n       continue; n     } n     if (s[i] ==     && s[i + 1] >=  0  && s[i + 1] <=  9  && n         ch1.back() >=  0  && ch1.back() <=  9 ) { n       ch1.push_back( / ); n     } n     if (s[i] !=    ) ch1.push_back(s[i]); n   } n   for (int i = 0; i < ch1.size(); i++) { n     if (ch1[i] ==  -  && ch1[i + 1] !=  - ) n       str = str +  ... ; n     else if (ch1[i] ==  -  && ch1[i + 1] ==  - ) n       str = str +  ...  +    ; n     else if (ch1[i] ==  , ) n       str = str + ch1[i] +    ; n     else if (ch1[i] ==  / ) n       str = str +    ; n     else if (ch1[i] >=  0  && ch1[i] <=  9  && ch1[i + 1] ==  - ) n       str = str + ch1[i] +    ; n     else n       str = str + ch1[i]; n   } n   if (str.back() ==    ) str.erase(str.length() - 1, 1); n   cout << str; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, t; n   cin >> n >> t; n   int f = 0; n   int x; n   int sum = 1; n   for (int i = 1; i < n; i++) { n     cin >> x; n     if (i == sum) sum += x; n     if (sum == t) f = 1; n   } n   if (f == 1) n     cout <<  YES  << endl; n   else n     cout <<  NO  << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int xuli(long long l, long long r, long long d) { n   if (l > d || r < d) n     return d; n   else n     return (r / d + 1) * d; n } n int main() { n   long long n; n   cin >> n; n   long long i, l, r, d; n   for (i = 1; i <= n; i++) { n     cin >> l >> r >> d; n     cout << xuli(l, r, d) <<   n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n long long Set(long long N, long long pos) { return N = N | (1 << pos); } n long long reset(long long N, long long pos) { return N = N & ~(1 << pos); } n bool check(long long N, long long pos) { return (bool)(N & (1 << pos)); } n void CI(long long &_x) { scanf( %d , &_x); } n void CO(long long &_x) { cout << _x; } n template <typename T> n void getarray(T a[], long long n) { n   for (long long i = 0; i < n; i++) cin >> a[i]; n } n template <typename T> n void prLLIarray(T a[], long long n) { n   for (long long i = 0; i < n - 1; i++) cout << a[i] <<    ; n   cout << a[n - 1] << endl; n } n const double EPS = 1e-9; n const long long INF = 0x7f7f7f7f; n long long dr8[8] = {1, -1, 0, 0, 1, -1, -1, 1}; n long long dc8[8] = {0, 0, -1, 1, 1, 1, -1, -1}; n long long dr4[4] = {0, 0, 1, -1}; n long long dc4[4] = {-1, 1, 0, 0}; n long long kn8r[8] = {1, 2, 2, 1, -1, -2, -2, -1}; n long long kn8c[8] = {2, 1, -1, -2, -2, -1, 1, 2}; n long long pow(long long a, long long b) { n   long long ans = 1; n   for (long long i = 0; i < b; i++) ans *= a; n   return ans; n } n int main() { n   long long a, b, n; n   cin >> a >> b >> n; n   if (b == 0) { n     cout <<  0 n ; n     return 0; n   } n   if (a == 0) { n     puts( No solution ); n     return 0; n   } n   long long y = b / a; n   long long low = -(1001), hi = (1001); n   for (int i = 1; i <= 1000; i++) { n     long long kk = 0, temp = abs(y); n     while (temp % i == 0 && kk < n) { n       temp = temp / i; n       kk++; n     } n     if (kk == n && temp == 1) { n       if (y < 0) i *= -1; n       cout << i <<   n ; n       return 0; n     } n   } n   puts( No solution ); 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 int n; n     cin >> n; n     int cnt = 0; n     while (n > 1) { n       if (n % 5 == 0) { n         n = (4 * n) / 5; n         cnt++; n         continue; n       } else if (n % 3 == 0) { n         n = (2 * n) / 3; n         cnt++; n         continue; n       } else if (n % 2 == 0) { n         n = (n) / 2; n         cnt++; n         continue; n       } else { n         cnt = -1; n         break; n       } n     } n     cout << cnt << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 103; n using pi = pair<int, int>; n inline int rd() { n   int x = 0, f = 1, c; n   while (!isdigit(c = getchar())) n     if (c ==  - ) f = -1; n   for (; isdigit(c); c = getchar()) x = x * 10 + c -  0 ; n   return f * x; n } n int n, m, k; n char p[N][N]; n inline void solve() { n   n = rd(), m = rd(), k = rd(); n   int cntr = 0; n   for (int i = 1; i <= n; i++) n     for (int j = 1; j <= m; j++) cin >> p[i][j], cntr += p[i][j] ==  R ; n   vector<int> sz; n   for (int i = 1; i <= cntr - cntr / k * k; i++) sz.push_back(cntr / k + 1); n   while (sz.size() < k) sz.push_back(cntr / k); n   vector<char> ch; n   for (int i = 0; i < 26; i++) ch.push_back( A  + i), ch.push_back( a  + i); n   for (char i =  0 ; i <=  9 ; i++) ch.push_back(i); n   for (int r = 1, cr = 0; r <= n; r++) { n     if (r & 1) n       for (int c = 1; c <= m; c++) { n         if (p[r][c] ==  R ) cr++; n         p[r][c] = ch.back(); n         if (cr == sz.back()) { n           sz.pop_back(); n           cr = 0; n           if (sz.size()) ch.pop_back(); n         } n       } n     else n       for (int c = m; c; c--) { n         if (p[r][c] ==  R ) cr++; n         p[r][c] = ch.back(); n         if (cr == sz.back()) { n           sz.pop_back(); n           cr = 0; n           if (sz.size()) ch.pop_back(); n         } n       } n   } n   for (int i = 1; i <= n; i++) { n     for (int j = 1; j <= m; j++) cout << p[i][j]; n     cout << endl; n   } n } n int main() { n   for (int T = rd(); T--; solve()) n     ; n } n


#include <bits/stdc++.h> n using namespace std; n struct dd { n   int k1; n   char k2; n } a[25000]; n bool cmp(struct dd q, struct dd b) { return q.k1 > b.k1; } n int main() { n   int i, j, m, n, sum; n   char b[1000]; n   gets(b); n   sum = 0; n   char k =  a ; n   scanf( %d , &m); n   for (i = 0; i < 26; i++) { n     scanf( %d , &a[i].k1); n     a[i].k2 = k; n     k++; n   } n   int len1 = strlen(b); n   for (i = 0; i < len1; i++) { n     sum += a[(int)b[i] - 97].k1 * (i + 1); n   } n   sort(a, a + 26, cmp); n   for (i = len1; i < len1 + m; i++) { n     sum += a[0].k1 * (i + 1); n   } n   printf( %d n , sum); n } n


#include <bits/stdc++.h> n using namespace std; n template <class T> n T abs(T a) { n   return (a >= 0 ? a : -a); n } n int main() { n   int k, no[12]; n   cin >> k; n   for (int i = 0; i < 12; i++) cin >> no[i]; n   sort(no, no + 12); n   int res = 0, i = 11; n   while (k > 0 && i >= 0) { n     res++; n     k -= no[i]; n     i--; n   } n   if (k <= 0) n     cout << res; n   else n     cout <<  -1 ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long int N = 1e6 + 5; n const long long int INF = 1e18; n const long long int MOD = 1e9 + 7; n int a[N], has[N], hol[N]; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   ; n   long long int n, m, k, u, v; n   cin >> n >> m >> k; n   a[1] = 1; n   long long int last; n   for (long long int i = 1; i <= m; i++) { n     cin >> hol[i]; n     has[hol[i]] = 1; n   } n   for (long long int i = 1; i <= k; i++) { n     cin >> u >> v; n     if (has[u] && a[u]) { n       cout << u << endl; n       exit(0); n     } n     if (has[v] && a[v]) { n       cout << v << endl; n       exit(0); n     } n     swap(a[u], a[v]); n     if (has[u] && a[u]) { n       cout << u << endl; n       exit(0); n     } n     if (has[v] && a[v]) { n       cout << v << endl; n       exit(0); n     } n   } n   for (long long int i = 1; i <= n; i++) { n     if (a[i]) { n       cout << i << endl; n       exit(0); n     } n   } n } n


#include <bits/stdc++.h> n int main() { n   int a, b, i, d, c = 0, m = 0, j; n   scanf( %d %d , &a, &b); n   d = b; n   while (d != 0) { n     c++; n     d = d / 10; n   } n   for (j = 1, i = 1; j < c; j++) { n     i = i * 10; n   } n   while (b != 0) { n     m = m + (b % 10) * i; n     b = b / 10; n     i = i / 10; n   } n   printf( %d n , a + m); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n #pragma comment(linker,  /STACK:256000000 ) n template <class T> n T sqr(T a) { n   return a * a; n } n int n, m, b; n long long a[1 << 20]; n int was[1 << 20]; n struct dr { n   int mon, pl, zad; n   int mask; n   const bool operator<(dr a) const { n     if (mon < a.mon) n       return true; n     else n       return false; n   } n }; n dr fr[100]; n void init() { n   scanf( %d%D%D , &n, &m, &b); n   for (int i = 0; i < n; ++i) { n     scanf( %d%d%D , &fr[i].pl, &fr[i].mon, &fr[i].zad); n     for (int j = 0; j < fr[i].zad; ++j) { n       int t; n       scanf( %D , &t); n       t--; n       fr[i].mask |= 1 << t; n     } n   } n   sort(fr, fr + n); n } n int q = 0; n long long ot = -1, ma = 1e18, co; n void solve() { n   was[0] = 1; n   for (int i = 0; i < (1 << m); ++i) a[i] = 1e18; n   a[0] = 0; n   for (int i = 0; i < n; ++i) { n     co = fr[i].mon; n     int t = fr[i].mask; n     for (int j = (1 << m) - 1; j >= 0; --j) { n       if (!was[j]) continue; n       was[j | t] = 1; n       a[j | t] = min(a[j | t], a[j] + fr[i].pl); n     } n     if (!was[(1 << m) - 1]) continue; n     if (ot == -1) n       ot = a[(1 << m) - 1] + co * (long long)b; n     else n       ot = min(ot, a[(1 << m) - 1] + co * (long long)b); n   } n   cout << ot; n } n int main() { n   init(); n   solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace ::std; n int a[110], n, c, i, j, o, m; n int main() { n   cin >> n >> c; n   cin >> a[0]; n   for (i = 1; i < n; i++) { n     cin >> a[i]; n     m = max(a[i - 1] - a[i] - c, m); n   } n   cout << m; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, m, x, y; n   cin >> n >> m >> x >> y; n   int vis[105][105]; n   int i, j; n   for (i = 0; i < 105; i++) n     for (j = 0; j < 105; j++) vis[i][j] = 0; n   vis[x][y] = 1; n   cout << x <<     << y << endl; n   cout << x <<     << m << endl; n   for (i = x - 1; i >= 1; i--) cout << i <<     << m << endl; n   for (i = x + 1; i <= n; i++) cout << i <<     << m << endl; n   int flag = 1; n   for (i = m - 1; i >= 1; i--) { n     if (flag) { n       for (j = n; j >= 1; j--) { n         if (vis[j][i] == 0) { n           cout << j <<     << i << endl; n           vis[j][i] = 1; n         } n       } n       flag = !flag; n     } else { n       for (j = 1; j <= n; j++) { n         if (vis[j][i] == 0) { n           cout << j <<     << i << endl; n           vis[j][i] = 1; n         } n       } n       flag = !flag; n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n map<string, int> mp; n int n, m, cc; n char s[1010]; n int a[1010], cnt[1010]; n void getcc(string s) { n   if (mp.find(s) == mp.end()) mp[s] = cc++; n   cnt[mp[s]]++; n } n int main() { n   int i, j, k; n   while (scanf( %d%d , &n, &m) > 0) { n     for (i = 0; i < n; i++) scanf( %d , &a[i]); n     sort(a, a + n); n     mp.clear(); n     cc = 0; n     memset(cnt, 0, sizeof(cnt)); n     for (i = 0; i < m; i++) { n       scanf( %s , s); n       getcc(s); n     } n     sort(cnt, cnt + cc); n     int ma = 0, mi = 0; n     for (i = 0; i < cc; i++) { n       mi += a[i] * cnt[cc - 1 - i]; n       ma += a[n - 1 - i] * cnt[cc - 1 - i]; n     } n     printf( %d %d n , mi, ma); n   } n   return 0; n } n


#include <bits/stdc++.h> n const int MAXINT = 2147483640; n const long long MAXLL = 9223372036854775800LL; n const long long MAXN = 1000000; n using namespace std; n int l[MAXN], r[MAXN]; n int n; n int ans[MAXN]; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   int T; n   cin >> T; n   while (T--) { n     cin >> n; n     for (int i = 1; i <= n; ++i) ans[i] = 0; n     for (int i = 1; i <= n; ++i) cin >> l[i] >> r[i]; n     int t = l[1]; n     ans[1] = t; n     for (int i = 2; i <= n; ++i) { n       if (r[i] <= t) n         ans[i] = 0; n       else { n         ans[i] = max(l[i], t + 1); n         t = ans[i]; n       } n     } n     for (int i = 1; i <= n; ++i) cout << ans[i] <<    ; n     cout <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main(void) { n   int t; n   cin >> t; n   while (t--) { n     long long n, g, b; n     cin >> n >> g >> b; n     long long t = (n + 1) / 2; n     long long r = t / g * (g + b); n     if (t % g == 0) { n       r -= b; n     } else { n       r += t % g; n     } n     cout << max(r, n) <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long a[10]; n long long ans = 0; n void DFS(long long n, long long u, long long r[10]) { n   if (n == 10) { n     long long sum = 1; n     for (long long i = 2; i <= u; i++) sum *= i; n     for (long long i = 1; i < 10; i++) n       for (long long j = 2; j <= r[i]; j++) sum /= j; n     long long b = 0; n     for (long long i = 1; i <= a[0]; i++) { n       long long d = 1, f = 1; n       for (long long j = 0; j < i; j++) { n         d *= u + j; n         f *= j + 1; n       } n       b += d / f; n     } n     if (a[0] == 0) b = 1; n     ans += b * sum; n     return; n   } n   if (a[n] == 0) { n     r[n] = 0; n     DFS(n + 1, u, r); n     return; n   } n   for (long long i = 1; i <= a[n]; i++) { n     r[n] = i; n     DFS(n + 1, u + i, r); n   } n } n int main() { n   long long d[10] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; n   long long s; n   cin >> s; n   while (s) { n     a[s % 10]++; n     s /= 10; n   } n   DFS(1, 0, d); n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long INF = 1e18; n const long long MOD = 1e9 + 7; n const int inf = 1e9; n const int N = 3e5 + 5; n bool flag; n template <typename T> n istream& operator>>(istream& is, vector<T>& v) { n   for (auto& i : v) is >> i; n   return is; n } n template <typename T> n void mul(T& a, T b) { n   a += b; n   if (b < (T)0) a += MOD; n   if (a >= (T)MOD) a -= MOD; n } n void solve() { n   int n, a, b; n   cin >> n >> a >> b; n   if (abs(a - b) > 1 or a + b + 2 > n) { n     cout << -1; n     return; n   } n   if (a == 0 and b == 0) { n     for (int i = 1; i <= n; i++) cout << i <<    ; n     return; n   } n   vector<int> ans; n   int l = 1, r = n; n   if (a > b) { n     ans.push_back(l++); n     for (int i = 1; i <= a + b; i++) { n       if (i % 2) n         ans.push_back(r--); n       else n         ans.push_back(l++); n     } n     for (int i = a + b + 1; i < n; i++) ans.push_back(r--); n   } else { n     ans.push_back(r--); n     for (int i = 1; i <= a + b; i++) { n       if (i % 2 == 0) n         ans.push_back(r--); n       else n         ans.push_back(l++); n     } n     for (int i = a + b + 1; i < n; i++) ans.push_back(a == b ? r-- : l++); n   } n   for (auto now : ans) cout << now <<    ; n } n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   cout << fixed << setprecision(12); n   int tt = 1; n   cin >> tt; n   while (tt--) { n     flag = 0; n     solve(); n     cout <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <class T> n void debug(T a, T b) { n   ; n } n template <class T> n void chmin(T& a, const T& b) { n   if (a > b) a = b; n } n template <class T> n void chmax(T& a, const T& b) { n   if (a < b) a = b; n } n namespace std { n template <class S, class T> n ostream& operator<<(ostream& out, const pair<S, T>& a) { n   out <<  (  << a.first <<  ,  << a.second <<  ) ; n   return out; n } n }  // namespace std n long long int readL() { n   long long int res; n   scanf( %I64d , &res); n   return res; n } n void printL(long long int res) { printf( %I64d , res); } n int n, S; n struct pack { n   int in, out, w, s, v; n }; n pack par[505]; n bool cmp(const pack& a, const pack& b) { n   return make_pair(a.in, -a.out) < make_pair(b.in, -b.out); n } n int tmp[1005][1005]; n int dp[505][1005]; n void renew(int i, int base) { n   int lim = min(S, par[base].w + par[base].s); n   for (int j = 0; j < (lim); ++j) { n     chmax(tmp[i][j + 1], tmp[i][j]); n   } n } n void update(pack p, int id, int base) { n   int l = p.in, r = p.out; n   renew(l, base); n   int lim = min(S, par[base].w + par[base].s); n   for (int i = (par[base].w); i < (lim + 1); ++i) n     if (tmp[l][i] >= 0 && dp[id][i - par[base].w] >= 0) { n       chmax(tmp[r][i], tmp[l][i] + dp[id][i - par[base].w]); n     } n } n void bring(int i, int base) { n   renew(i, base); n   int lim = min(S, par[base].w + par[base].s); n   for (int j = 0; j < (lim + 1); ++j) { n     chmax(tmp[i + 1][j], tmp[i][j]); n   } n } n int main() { n   cin >> n >> S; n   for (int i = 0; i < (n); ++i) { n     cin >> par[i].in >> par[i].out >> par[i].w >> par[i].s >> par[i].v; n   } n   par[n] = (pack){0, 2 * n, 0, S + 1, 0}; n   ++n; n   sort(par, par + n, cmp); n   memset(dp, -1, sizeof(dp)); n   int res = 0; n   for (int i = n - 1; i >= 0; --i) { n     pack p = par[i]; n     int l = par[i].in, r = par[i].out; n     for (int j = (l); j < (r + 1); ++j) n       for (int k = 0; k < (S + 1); ++k) tmp[j][k] = -1; n     tmp[l][p.w] = p.v; n     renew(l, i); n     int cur = l; n     for (int j = (i + 1); j < (n); ++j) { n       if (l <= par[j].in && par[j].out <= r) { n         while (cur < par[j].in) { n           bring(cur, i); n           ++cur; n         } n         update(par[j], j, i); n       } n     } n     while (cur < r) { n       bring(cur, i); n       ++cur; n     } n     renew(r, i); n     int lim = min(S, p.w + p.s); n     for (int j = 0; j < (lim + 1); ++j) { n       dp[i][j] = tmp[r][j]; n       chmax(res, tmp[r][j]); n     } n     for (int j = 0; j < (S); ++j) chmax(dp[i][j + 1], dp[i][j]); n   } n   cout << res << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n bool PIIfs(const pair<int, int>& a, const pair<int, int>& b) { n   return a.first < b.first || (a.first == b.first && a.second < b.second); n } n bool PIIsf(const pair<int, int>& a, const pair<int, int>& b) { n   return a.second < b.second || (a.second == b.second && a.first < b.first); n } n bool PIIFS(const pair<int, int>& a, const pair<int, int>& b) { n   return a.first > b.first || (a.first == b.first && a.second > b.second); n } n bool PIISF(const pair<int, int>& a, const pair<int, int>& b) { n   return a.second > b.second || (a.second == b.second && a.first > b.first); n } n bool PIIfS(const pair<int, int>& a, const pair<int, int>& b) { n   return a.first < b.first || (a.first == b.first && a.second > b.second); n } n bool PIIsF(const pair<int, int>& a, const pair<int, int>& b) { n   return a.second < b.second || (a.second == b.second && a.first > b.first); n } n bool PIIFs(const pair<int, int>& a, const pair<int, int>& b) { n   return a.first > b.first || (a.first == b.first && a.second < b.second); n } n bool PIISf(const pair<int, int>& a, const pair<int, int>& b) { n   return a.second > b.second || (a.second == b.second && a.first < b.first); n } n template <typename T> n inline T max(T a, T b, T c) { n   return max(max(a, b), c); n } n template <typename T> n inline T max(T a, T b, T c, T d) { n   return max(max(a, b, c), d); n } n template <typename T> n inline T max(T a, T b, T c, T d, T e) { n   return max(max(a, b, c, d), e); n } n template <typename T> n inline T min(T a, T b, T c) { n   return min(min(a, b), c); n } n template <typename T> n inline T min(T a, T b, T c, T d) { n   return min(min(a, b, c), d); n } n template <typename T> n inline T min(T a, T b, T c, T d, T e) { n   return min(min(a, b, c, d), e); n } n template <typename T> n inline istream& RD(T& x) { n   return cin >> x; n } n template <typename T, typename U> n inline istream& RD(T& a, U& b) { n   return cin >> a >> b; n } n template <typename T, typename U, typename V> n inline istream& RD(T& a, U& b, V& c) { n   return cin >> a >> b >> c; n } n template <typename T, typename U, typename V, typename W> n inline istream& RD(T& a, U& b, V& c, W& d) { n   return cin >> a >> b >> c >> d; n } n template <typename T, typename U, typename V, typename W, typename X> n inline istream& RD(T& a, U& b, V& c, W& d, X& e) { n   return cin >> a >> b >> c >> d >> e; n } n template <typename T> n void checkmin(T& a, const T& b) { n   if (b < a) a = b; n } n template <typename T> n void checkmax(T& a, const T& b) { n   if (b > a) a = b; n } n int n; n void dfs(const vector<string>& vs, bitset<300>& mark, int node, n          vector<int>& ans) { n   if (mark.test(node)) return; n   mark.set(node); n   ans.push_back(node); n   for (int i = 0; i < n; i++) n     if (vs[node][i] ==  1 ) dfs(vs, mark, i, ans); n } n void perm(vector<int>& vi, vector<int>& si) { n   int x = si.size(); n   vector<int> tmp(x); n   for (int i = 0; i < (int)(x); ++i) tmp[i] = vi[si[i]]; n   sort(tmp.begin(), tmp.end()); n   sort(si.begin(), si.end()); n   for (int i = 0; i < (int)(x); ++i) vi[si[i]] = tmp[i]; n } n int main() { n   std::ios::sync_with_stdio(false); n   cin >> n; n   vector<int> vi(n); n   for (int i = 0; i < (int)(n); ++i) cin >> vi[i]; n   vector<string> vs; n   string s; n   for (int i = 0; i < (int)(n); ++i) { n     cin >> s; n     vs.push_back(s); n   } n   vector<set<int> > circles; n   bitset<300> found; n   for (int i = 0; i < n; i++) { n     vector<int> ans; n     dfs(vs, found, i, ans); n     if (ans.size() > 1) { n       perm(vi, ans); n     } n   } n   for (__typeof__(vi.begin()) it = vi.begin(); it != vi.end(); ++it) n     cout << *it <<    ; n   cout <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long double DP[202][202][202]; n long double P[202]; n int A[202]; n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   int n, l, k; n   cin >> n >> l >> k; n   k = min(k, n); n   for (int i = 1; i <= n; i++) cin >> P[i], P[i] /= 100.0; n   for (int i = 1; i <= n; i++) cin >> A[i]; n   DP[1][0][k] = 1; n   for (int i = 1; i <= n; i++) n     for (int j = 0; j < i; j++) n       for (int z = k; z <= n; z++) { n         DP[i + 1][j][z] += DP[i][j][z] * (1.0 - P[i]); n         if (A[i] == -1) n           DP[i + 1][j + 1][z] += DP[i][j][z] * P[i]; n         else n           DP[i + 1][j + 1][min(n, z + A[i] + 1)] += DP[i][j][z] * P[i]; n       } n   long double ats = 0; n   for (int j = l; j <= n; j++) n     for (int z = max(k, j); z <= n; z++) ats += DP[n + 1][j][z]; n   cout << setprecision(13) << fixed << ats; n } n


#include <bits/stdc++.h> n using namespace std; n int mod = 1e9 + 7; n vector<long long> num; n long long gcd(long long x, long long y) { n   if (y > x) return gcd(y, x); n   return x % y ? gcd(y, x % y) : y; n } n int a[3]; n vector<vector<int> > grid; n vector<vector<int> > dist; n void bfs(int x, vector<bool>& vis) { n   int n = vis.size(); n   queue<int> q; n   q.push(a[x]); n   vis[a[x]] = 1; n   dist[a[x]][x] = 0; n   int step = 1; n   while (q.size()) { n     int t = q.size(); n     while (t--) { n       int tmp = q.front(); n       q.pop(); n       for (int i = 0; i < grid[tmp].size(); i++) { n         int next = grid[tmp][i]; n         if (!vis[next]) { n           dist[next][x] = step; n           vis[next] = 1; n           q.push(next); n         } n       } n     } n     step++; n   } n   return; n } n long long shu[63]; n int find(long long x) { n   for (int i = 0; i < 63; i++) { n     if (shu[i] >= x) return i; n   } n   return 63; n } n long long zhuan(string s) { n   int n = s.size(); n   long long ans = 0; n   for (int i = 0; i < n; i++) { n     ans = ans * 2 + (long long)(s[i] -  0 ); n   } n   return ans; n } n int main() { n   int n; n   cin >> n; n   while (n--) { n     long long t; n     cin >> t; n     t--; n     long long pos = t % 3; n     t = t / 3; n     long long sum = 1; n     int cnt = 1; n     while (sum <= t) { n       t -= sum; n       sum *= 4; n       cnt += 2; n     } n     string ans =   ; n     if (pos == 0) n       ans +=  1 ; n     else { n       if (pos == 1) n         ans +=  10 ; n       else n         ans +=  11 ; n     } n     string str[12] = { 00 ,  01 ,  10 ,  11 ,  00 ,  10 , n                        11 ,  01 ,  00 ,  11 ,  01 ,  10 }; n     while (ans.size() < cnt) { n       ans.insert((pos == 0 ? 1 : 2), str[pos * 4 + t % 4]); n       t /= 4; n     } n     cout << zhuan(ans) << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n bool query = 0; n struct line { n   long long m, b; n   mutable function<const line*()> succ; n   bool operator<(const line rhs) const { n     if (!query) return (m < rhs.m); n     const line* s = succ(); n     return s ? (b - s->b) < (rhs.m * (s->m - m)) : 0; n   } n }; n struct hull : multiset<line> { n   bool bad(iterator y) { n     auto z = next(y); n     if (y == begin()) { n       if (z == end()) return 0; n       return (y->m == z->m) && (y->b <= z->b); n     } n     auto x = prev(y); n     if (z == end()) return (y->m == x->m) && (y->b <= x->b); n     return (1.0) * (x->b - y->b) * (z->m - y->m) >= n            (1.0) * (y->b - z->b) * (y->m - x->m); n   } n   void add(long long m, long long b) { n     auto it = insert({m, b}); n     it->succ = [=] { return (next(it) == end()) ? 0 : &*next(it); }; n     if (bad(it)) { n       erase(it); n       return; n     } n     while (next(it) != end() && bad(next(it))) erase(next(it)); n     while (it != begin() && bad(prev(it))) erase(prev(it)); n   } n   long long eval(long long x) { n     query = 1; n     line l = *lower_bound({x, 0}); n     query = 0; n     return (l.m * x + l.b); n   } n }; n hull h; n int N; n int A[200005]; n long long SumA[200005]; n long long SumAi[200005]; n long long Solve() { n   for (int i = 1; i <= N; i++) { n     SumA[i] = SumA[i - 1] + A[i]; n     SumAi[i] = SumAi[i - 1] + (1LL * A[i] * i); n   } n   long long Ans = -9e18; n   for (int i = 1; i <= N; i++) { n     h.add(-i, -SumA[i - 1]); n     Ans = max(Ans, h.eval(-A[i]) - 1LL * A[i] * i + SumA[i - 1] + SumAi[N]); n   } n   h.clear(); n   for (int i = N; i; i--) { n     h.add(i, -SumA[i]); n     Ans = max(Ans, h.eval(A[i]) - 1LL * A[i] * i + SumA[i] + SumAi[N]); n   } n   return Ans; n } n int main() { n   scanf( %d , &N); n   for (int i = 1; i <= N; i++) scanf( %d , &A[i]); n   printf( %I64d n , Solve()); n } n


#include <bits/stdc++.h> n using namespace std; n int triangle(long long n) { n   if (n == 1) n     return 1; n   else n     return n + triangle(n - 1); n } n int heart(long long a) { n   if (a == 1) n     return 1; n   else n     return (6 * (a - 1)) + heart(a - 1); n } n int balls(long long a) { n   if (a == 1) n     return 1; n   else n     return heart(a) + (triangle(a - 1) * 6); n } n int main() { n   long long a, b; n   cin >> a; n   b = balls(a); n   cout << b; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m; n set<int> nx[1000100]; n vector<pair<int, int> > nw; n int a[1000100]; n vector<pair<int, int> > ans; n set<pair<int, int> > order; n set<pair<int, int> > lst; n int main() { n   ios_base ::sync_with_stdio(0); n   cin.tie(); n   cout.tie(); n   srand(23095); n   cin >> n >> m; n   for (int i = 0; i < m; i++) { n     int x, y; n     cin >> x >> y; n     x--; n     y--; n     nx[x].insert(y); n     nx[y].insert(x); n     a[x]++; n     a[y]++; n   } n   for (int i = 0; i < n; i++) { n     a[i] = (a[i] / 2) + (a[i] % 2); n     order.insert(make_pair(a[i], i)); n   } n   while (!order.empty()) { n     int v = order.begin()->second; n     order.erase(order.begin()); n     nw.clear(); n     for (auto u : nx[v]) { n       nw.push_back(make_pair(a[u], u)); n     } n     sort(nw.rbegin(), nw.rend()); n     for (int j = 0; a[v] > 0; j++) { n       a[v]--; n       if (a[nw[j].second] > 0) { n         order.erase(make_pair(a[nw[j].second], nw[j].second)); n       } n       a[nw[j].second]--; n       if (a[nw[j].second] > 0) { n         order.insert(make_pair(a[nw[j].second], nw[j].second)); n       } n       ans.push_back(make_pair(v, nw[j].second)); n       nx[v].erase(nw[j].second); n       nx[nw[j].second].erase(v); n     } n   } n   cout << ans.size() <<   n ; n   for (auto i : ans) { n     cout << i.first + 1 <<     << i.second + 1 <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long a[201010]; n int main() { n   long long n, m, k; n   while (~scanf( %lld%lld%lld , &n, &m, &k)) { n     memset(a, 0, sizeof(a)); n     long long i, j, ans = 0, sum = k; n     for (i = 1; i <= n; i++) scanf( %lld , &a[i]); n     for (i = n; i >= 1; i--) { n       if (a[i] > k) continue; n       if (a[i] <= sum) { n         sum -= a[i]; n         ans++; n       } else { n         m--; n         if (m <= 0) break; n         sum = k; n         i++; n       } n     } n     printf( %lld n , ans); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long gcd(long long a, long long b) { n   if (b == 0) return a; n   return gcd(b, a % b); n } n long long power(long long a, long long b) { n   if (b == 1) return a; n   if (b == 0) return 1; n   long long ans = power(a, b / 2); n   ans = (ans * ans); n   if (b & 1) ans = (ans * a); n   return ans; n } n void solve() { n   long long n, k; n   cin >> n >> k; n   if (n == 1) { n     cout << 0 <<   n ; n     return; n   } n   if (k == 1) { n     cout << n - 1 <<   n ; n     return; n   } n   long long x = 1; n   long long cnt = 0; n   while (x < k && n - x >= 0) { n     cnt++; n     x *= 2; n   } n   n -= x; n   cnt += (n + k - 1) / k; n   cout << cnt <<   n ; n } n signed main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   long long tt = 1; n   cin >> tt; n   while (tt--) { n     solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long int gcd(long long int a, long long int b) { n   if (a == 0) { n     return b; n   } n   if (b == 0) { n     return a; n   } n   return gcd(b, a % b); n } n long long int power(long long int x, long long int n) { n   long long ans = 1; n   while (n > 0) { n     if (n & 1) ans = (ans * x) % 1000000007; n     x = (x * x) % 1000000007; n     n /= 2; n   } n   if (x == 0) ans = 0; n   return ans; n } n long long int getsum(int g, int a[], int n, int x) { n   long long int i = n - 1, tmp = 0, k = g, count = 0; n   while (i >= 0) { n     while (k != 0 && i >= 0) { n       tmp += max((long long)0, (a[i] - count)); n       i--; n       k--; n     } n     count++; n     k = g; n   } n   return tmp; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   int t = 1; n   while (t--) { n     int n, m, i; n     cin >> n >> m; n     int a[n]; n     for (i = 0; i < n; i++) cin >> a[i]; n     sort(a, a + n); n     int lo = 1, hi = n, mid; n     while (lo < hi) { n       mid = lo + (hi - lo) / 2; n       long long int tmp = getsum(mid, a, n, m); n       if (tmp < m) { n         lo = mid + 1; n       } else { n         hi = mid; n       } n     } n     if (getsum(lo, a, n, m) >= m) { n       cout << lo; n     } else { n       cout << -1; n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n template <typename... Args> n void trace(Args... args) {} n template <typename... Args> n void recInit(Args... args) {} n template <typename... Args> n void recEnd(Args... args) {} n struct node { n   long long sum, lp, rp, L, R, sz; n } tree[4 * 200020], np; n node merge(node l, node r) { n   node id; n   id.sz = l.sz + r.sz; n   id.L = l.L; n   id.R = r.R; n   if (l.lp == l.sz) { n     id.lp = l.sz + (l.R <= r.L) * r.lp; n   } else { n     id.lp = l.lp; n   } n   if (r.rp == r.sz) { n     id.rp = r.sz + (l.R <= r.L) * l.rp; n   } else { n     id.rp = r.rp; n   } n   id.sum = l.sum + r.sum; n   if (l.R <= r.L) { n     id.sum += l.rp * r.lp; n   } n   return id; n } n void build(int id, int l, int r, long long* a) { n   if (l == r) { n     tree[id] = node({1, 1, 1, a[l], a[l], 1}); n     return; n   } n   int m = (l + r) / 2; n   build(id * 2, l, m, a); n   build(id * 2 + 1, m + 1, r, a); n   tree[id] = merge(tree[id * 2], tree[id * 2 + 1]); n } n void update(int id, int l, int r, int x, long long y) { n   if (r < x || x < l) return; n   if (l == r) { n     tree[id] = node({1, 1, 1, y, y, 1}); n     return; n   } n   int m = (l + r) / 2; n   update(id * 2, l, m, x, y); n   update(id * 2 + 1, m + 1, r, x, y); n   tree[id] = merge(tree[id * 2], tree[id * 2 + 1]); n } n node query(int id, int l, int r, int lq, int rq) { n   if (lq > rq || r < lq || rq < l) return np; n   if (lq <= l && r <= rq) return tree[id]; n   int m = (l + r) / 2; n   return merge(query(id * 2, l, m, lq, rq), n                query(id * 2 + 1, m + 1, r, lq, rq)); n } n long long t = 1, n, q, ans, a[200020]; n int main() { n   np = node({0, 0, 0, -10000000000LL, 10000000000LL, 0}); n st: n   ans = 0; n   scanf( %lld %lld , &n, &q); n   for (int i = 1; i <= n; i++) { n     scanf( %lld , &a[i]); n   } n   build(1, 1, n, a); n   while (q--) { n     long long t, x, y; n     scanf( %lld %lld %lld , &t, &x, &y); n     if (t == 1) { n       a[x] = y; n       update(1, 1, n, x, y); n     } else if (t == 2) { n       printf( %lld n , query(1, 1, n, x, y).sum); n     } else n       assert(false); n   } n   if (--t) goto st; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T> n inline void read(T &x) { n   int f = 0, c = getchar(); n   x = 0; n   while (!isdigit(c)) f |= c == 45, c = getchar(); n   while (isdigit(c)) x = (x << 3) + (x << 1) + (c ^ 48), c = getchar(); n   if (f) x = -x; n } n template <typename T, typename... Args> n inline void read(T &x, Args &...args) { n   read(x); n   read(args...); n } n const int maxn = 2e5 + 50; n int fa[maxn], bj[maxn], n, m, ans; n struct node { n   int x, y, z; n } e[maxn]; n bool cmp(node a, node b) { return a.z > b.z; } n inline int find(int x) { n   if (fa[x] == x) return x; n   return fa[x] = find(fa[x]); n } n signed main() { n   read(n, m); n   for (int i = 1; i <= n; i++) fa[i] = i; n   for (int i = 1; i <= m; i++) read(e[i].x, e[i].y, e[i].z); n   stable_sort(e + 1, e + 1 + m, cmp); n   for (int i = 1; i <= m; i++) { n     int f1 = find(e[i].x), f2 = find(e[i].y); n     if (f1 == f2) { n       if (bj[f1] == 0) n         bj[f1] = 1, ans += e[i].z; n       else n         continue; n     } else { n       if (bj[f1] + bj[f2] == 0) fa[f1] = f2, ans += e[i].z; n       if (bj[f1] == 1 && bj[f2] == 0) fa[f2] = f1, ans += e[i].z; n       if (bj[f1] == 0 && bj[f2] == 1) fa[f1] = f2, ans += e[i].z; n     } n   } n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n char mat[55][55]; n int n; n int A[] = {-1, 0, 1, 0}; n int B[] = {0, 1, 0, -1}; n vector<pair<int, int> > ilha1, ilha2; n void flood_fill(int i, int j, bool prima) { n   if (i < 0 or j < 0 or i >= n or j >= n or mat[i][j] !=  0 ) return; n   mat[i][j] =  1 ; n   if (prima) n     ilha1.push_back({i, j}); n   else n     ilha2.push_back({i, j}); n   for (int k = 0; k < 4; k++) { n     flood_fill(i + A[k], j + B[k], prima); n   } n } n int main() { n   pair<int, int> p1, p2; n   scanf( %d , &n); n   scanf( %d%d%d%d , &p1.first, &p1.second, &p2.first, &p2.second); n   p1.first--; n   p1.second--; n   p2.first--; n   p2.second--; n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < n; j++) { n       scanf(  %c , &mat[i][j]); n     } n   } n   flood_fill(p1.first, p1.second, true); n   if (mat[p2.first][p2.second] ==  1 ) { n     puts( 0 ); n     return 0; n   } n   flood_fill(p2.first, p2.second, false); n   int ans = INT_MAX; n   for (int i = 0; i < (int)ilha1.size(); i++) { n     for (int j = 0; j < (int)ilha2.size(); j++) { n       int a = abs(ilha1[i].first - ilha2[j].first); n       int b = abs(ilha1[i].second - ilha2[j].second); n       ans = min(ans, a * a + b * b); n     } n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n inline void PAUSE() { n   char tmp; n   cin >> tmp; n } n char s[102]; n int main() { n   int n, k; n   int f = 0; n   scanf( %d %d , &n, &k); n   if (n == k) f = 1; n   cin >> s; n   if (n >= 2 * k) n     for (int i = k - 1; i >= 1; i--) { n       cout <<  LEFT  << endl; n     } n   else n     for (int i = k + 1; i <= n; i++) { n       cout <<  RIGHT  << endl; n       f = 1; n     } n   if (f == 0) n     for (int i = 1; i <= n; i++) { n       cout <<  PRINT   << s[i - 1] << endl; n       if (i != n) cout <<  RIGHT  << endl; n     } n   else n     for (int i = n; i >= 1; i--) { n       cout <<  PRINT   << s[i - 1] << endl; n       if (i != 1) cout <<  LEFT  << endl; n     } n   PAUSE(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAX = 2e6 + 5; n int ends_here[MAX]; n int qs, qe; n struct data { n   int child[26]; n   int score; n   bool leaf; n   int suf; n   int ends; n }; n data arr[MAX]; n int que[MAX]; n int cur; n struct aho_corasick { n   int root; n   int size; n   void init() { root = (++cur); } n   void insert(string s, int ind) { n     int node = root; n     for (int i = 0; i < s.length(); i++) { n       int c = s[i] -  a ; n       if (!arr[node].child[c]) { n         arr[node].child[c] = (++cur); n       } n       node = arr[node].child[c]; n     } n     ++size; n     arr[node].leaf = 1; n     arr[node].ends = ind; n   } n   int find(int node, int c) { n     while (node != root) { n       if (arr[node].child[c]) { n         return arr[node].child[c]; n       } n       node = arr[node].suf; n     } n     if (arr[root].child[c]) { n       return arr[root].child[c]; n     } else { n       return root; n     } n   } n   void prepare() { n     qs = 0; n     qe = 0; n     que[qe++] = root; n     arr[root].suf = root; n     while (qs < qe) { n       int node = que[qs++]; n       for (int i = 0; i < 26; i++) { n         if (arr[node].child[i]) { n           int nxt = arr[node].child[i]; n           if (node != root) { n             arr[nxt].suf = find(arr[node].suf, i); n           } else { n             arr[nxt].suf = root; n           } n           arr[nxt].score = arr[arr[nxt].suf].score + arr[nxt].leaf; n           que[qe++] = nxt; n         } n       } n     } n   } n   long long query(string s) { n     int node = root; n     long long ans = 0; n     for (int i = 0; i < s.length(); i++) { n       int c = s[i] -  a ; n       node = find(node, c); n       ans += arr[node].score; n     } n     return ans; n   } n   void dfs(int s1, int s2) { n     arr[s1].leaf |= arr[s2].leaf; n     for (int i = 0; i < 26; i++) { n       if (!arr[s2].child[i]) { n         continue; n       } n       if (!arr[s1].child[i]) { n         arr[s1].child[i] = arr[s2].child[i]; n         continue; n       } n       dfs(arr[s1].child[i], arr[s2].child[i]); n     } n   } n   void merge(aho_corasick other) { n     dfs(root, other.root); n     size += other.size; n   } n }; n int n, k; n set<int> pos; n vector<int> hlp; n void check(int ind) { n   int f = 0; n   for (int i = ind;; i += k) { n     if (ends_here[i + k - 1] == 0) { n       f = 1; n       break; n     } n     if (pos.find(ends_here[i + k - 1]) != pos.end()) { n       f = 1; n       break; n     } n     pos.insert(ends_here[i + k - 1]); n     hlp.push_back(ends_here[i + k - 1]); n     if (pos.size() == n) { n       break; n     } n   } n   if (f == 0) { n     printf( YES n ); n     for (auto x : hlp) { n       printf( %d  , x); n     } n     exit(0); n   } else { n     pos.clear(); n     hlp.clear(); n   } n } n string str[100005]; n int main() { n   aho_corasick a; n   a.init(); n   scanf( %d %d , &n, &k); n   string s; n   cin >> s; n   s = s + s; n   int m; n   scanf( %d , &m); n   for (int i = 1; i <= m; i++) { n     cin >> str[i]; n     a.insert(str[i], i); n   } n   a.prepare(); n   int st = a.root; n   for (int i = 0; i < s.length(); i++) { n     int c = s[i] -  a ; n     st = a.find(st, c); n     if (arr[st].score > 0) { n       ends_here[i] = arr[st].ends; n     } n   } n   for (int i = 0; i < k; i++) { n     check(i); n   } n   printf( NO n ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n inline int read() { n   int x = 0, f = 1, c = getchar(); n   while (c < 48) c ==  -  && (f = -1), c = getchar(); n   while (c > 47) x = x * 10 + c -  0 , c = getchar(); n   return x * f; n } n const int MAXN = 100005; n std::vector<long long> adj; n long long n, ans; n inline long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; } n inline long long lcm(long long a, long long b) { return a / gcd(a, b) * b; } n int main(int argc, char const *argv[]) { n   n = read(); n   for (int i = 1; i <= n; ++i) adj.push_back(read()); n   sort(adj.rbegin(), adj.rend()); n   long long mx[5] = {1, *adj.begin(), 1, 1, 1}; n   ans = 0; n   if (adj.size() > 1) mx[2] = adj[1]; n   if (adj.size() > 2) mx[3] = adj[2]; n   for (long long i : adj) n     if (i % 2 != mx[1] % 2) mx[4] = max(mx[4], i); n   for (int i = 1; i <= 4; ++i) n     for (long long x : adj) ans = max(ans, lcm(mx[i], x)); n   printf( %lld n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 2E5 + 10; n long long jc[15]; n int main() { n   jc[0] = 1; n   for (int i = 1; i <= 14; ++i) jc[i] = jc[i - 1] * i; n   int n, m; n   cin >> n >> m; n   int z = max(n - 13, 1); n   long long ad = 0, p = jc[n - z + 1]; n   while (m--) { n     int k; n     cin >> k; n     if (k == 1) { n       int l, r; n       cin >> l >> r; n       if (r < z) n         cout << (l + r) * 1ll * (r - l + 1) / 2 << endl; n       else { n         long long ans = (z - 1) * (r - max(l, z) + 1), da = ad; n         if (l < z) ans += (l + z - 1) * 1ll * (z - l) / 2; n         bool b[15] = {0}; n         for (int i = z; i <= r; ++i) { n           long long t = da / jc[n - i]; n           int j = 1; n           da -= t * jc[n - i]; n           for (; t || b[j]; ++j) n             if (!b[j]) --t; n           b[j] = 1; n           if (i >= l) ans += j; n         } n         cout << ans << endl; n       } n     } else { n       int t; n       cin >> t; n       ad = (ad + t) % p; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long Q; n long long sum[105]; n multiset<long long> s; n int main() { n   cin.tie(0); n   ios_base::sync_with_stdio(0); n   cin >> Q; n   while (Q--) { n     char op; n     long long f; n     cin >> op >> f; n     if (op ==  + ) { n       long long i = 0; n       while ((1 << (i + 1)) <= f) i++; n       sum[i] += f; n       s.insert(f); n     } else { n       long long i = 0; n       while ((1 << (i + 1)) <= f) i++; n       sum[i] -= f; n       s.erase(s.find(f)); n     } n     long long t = s.size(); n     long long k = 0; n     long long ant = 0; n     for (long long i = 0; i <= 30; i++) { n       ant += sum[i]; n       auto it = s.lower_bound((1 << i)); n       if (it == s.end()) continue; n       if ((1 << (i + 1)) <= *it) continue; n       if (*it > 2 * (ant - sum[i])) k++; n     } n     cout << t - k <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int b[205]; n int a[205]; n int main() { n   int n, m, k; n   int x; n   scanf( %d%d%d , &n, &m, &k); n   for (int i = 1; i <= k; i++) { n     scanf( %d , &x); n     a[i] = x; n   } n   int sum = 0; n   int mm = m; n   while (n--) { n     mm = m; n     while (mm--) { n       scanf( %d , &x); n       int sb; n       for (int j = 1; j <= k; j++) { n         if (x == a[j]) { n           sb = j; n         } n       } n       sum += sb; n       b[1] = a[sb]; n       int count = 2; n       for (int j = 1; j <= k; j++) { n         if (j != sb) { n           b[count++] = a[j]; n         } n       } n       for (int j = 1; j <= k; j++) { n         a[j] = b[j]; n       } n     } n   } n   cout << sum << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   char dir; n   cin >> dir; n   string m; n   cin >> m; n   string kboard =  qwertyuiopasdfghjkl;zxcvbnm,./ ; n   if (dir ==  R ) { n     for (int i = 0; i < m.length(); i++) { n       int index = kboard.find(m[i]); n       cout << kboard[index - 1]; n     } n     cout << endl; n   } else { n     for (int i = 0; i < m.length(); i++) { n       int index = kboard.find(m[i]); n       cout << kboard[index + 1]; n     } n     cout << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int mod = 998244353; n const int N = 1e5 + 5; n vector<int> g[N]; n vector<pair<int, int> > ans; n int maxx; n void dfs(int x, int fa, int t) { n   int siz = g[x].size(); n   int tmp = t; n   ans.push_back(make_pair(x, t)); n   for (int i = 0; i <= siz - 1; i++) { n     int v = g[x][i]; n     if (v == fa) continue; n     if (t == maxx) t = maxx - siz, ans.push_back(make_pair(x, t)); n     dfs(v, x, ++t); n     ans.push_back(make_pair(x, t)); n   } n   if (t >= tmp && x - 1) ans.push_back(make_pair(x, tmp - 1)); n } n int main() { n   int n, x, y; n   scanf( %d , &n); n   for (int i = 1; i <= n - 1; i++) { n     scanf( %d%d , &x, &y); n     g[x].push_back(y); n     g[y].push_back(x); n   } n   for (int i = 1; i <= n; i++) maxx = max(maxx, (int)g[i].size()); n   dfs(1, 1, 0); n   int num = (int)ans.size(); n   printf( %d n , num); n   for (int i = 0; i <= num - 1; i++) { n     printf( %d %d n , ans[i].first, ans[i].second); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <class T> n inline void chkmax(T &x, T y) { n   if (x < y) x = y; n } n template <class T> n inline void chkmin(T &x, T y) { n   if (x > y) x = y; n } n int q, n, f, inv[100100], fac[100100], p[6]; n void Euclid(int a, int b, int &x, int &y) { n   if (!a) n     x = 0, y = 1; n   else { n     int q = b / a, r = b % a; n     Euclid(r, a, y, x); n     x -= q * y; n   } n } n int cal(int n, int m) { n   if (m > n) return 0; n   return 1ll * fac[n - 1] * inv[m - 1] % 1000000007 * inv[n - m] % 1000000007; n } n int main() { n   fac[0] = 1; n   for (int i = 1; i < 100100; i++) fac[i] = 1ll * fac[i - 1] * i % 1000000007; n   int x, y; n   Euclid(fac[100100 - 1], 1000000007, x, y); n   inv[100100 - 1] = x; n   for (int i = 100100 - 1; i > 0; i--) n     inv[i - 1] = 1ll * inv[i] * i % 1000000007; n   scanf( %d , &q); n   while (q--) { n     scanf( %d %d , &n, &f); n     x = 0, y = n; n     for (int i = 2; i <= sqrt(y); i++) { n       if (y % i) continue; n       p[x++] = i; n       while (y % i == 0) y /= i; n     } n     if (y > 1) p[x++] = y; n     y = 0; n     for (int i = 0; i < (1 << x); i++) { n       int tmp = n, cnt = 0; n       for (int j = 0; j < x; j++) n         if (i & (1 << j)) cnt++, tmp /= p[j]; n       if (cnt & 1) n         y = (y - cal(tmp, f) + 1000000007) % 1000000007; n       else n         y = (y + cal(tmp, f)) % 1000000007; n     } n     printf( %d n , y); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios::sync_with_stdio(false); n   int n; n   cin >> n; n   set<int> v; n   int a; n   for (int i = 0; i < n; ++i) { n     cin >> a; n     v.insert(a); n   } n   v.erase(0); n   cout << v.size(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long rr[] = {0, 1, 1, 1, 0, -1, -1, -1}; n long long cc[] = {1, 1, 0, -1, -1, -1, 0, 1}; n const long long mod = 1e9 + 7; n const long long N = 300050; n long long m, fact[N]; n void pre() { n   fact[0] = 1; n   for (long long i = 1; i < N; i++) { n     fact[i] = (fact[i - 1] * i) % m; n   } n } n signed main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   ; n   long long test = 1; n   while (test--) { n     long long n; n     cin >> n >> m; n     pre(); n     long long ans = 0; n     for (long long i = 1; i <= n; i++) { n       long long x = fact[n - i + 1] * (n - i + 1) % m; n       x = (x * fact[i]) % m; n       ans = (ans + x) % m; n     } n     cout << ans <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int tab[300000]; n vector<int> numbers; n int main() { n   int n; n   cin >> n; n   numbers.resize(n + 10); n   vector<int> unused; n   for (int i = 1; i <= n; i++) { n     cin >> numbers[i]; n     if (numbers[i] > n || numbers[i] < 1) { n       unused.push_back(i); n     } else if (tab[numbers[i]] == 0) { n       tab[numbers[i]] = i; n     } else { n       unused.push_back(i); n     } n   } n   for (int i = 1; i <= n; i++) { n     if (tab[i] == 0) { n       tab[i] = unused.back(); n       unused.pop_back(); n     } n   } n   for (int i = 1; i <= n; i++) { n     numbers[tab[i]] = i; n   } n   for (int i = 1; i <= n; i++) { n     cout << numbers[i] <<    ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n long long int mod = 998244353; n long double eps = 1e-12; n long long int dx[] = {0, 0, 1, -1, 1, 1, -1, -1}; n long long int dy[] = {1, -1, 0, 0, 1, -1, 1, -1}; n long long int n, res = 0, k = 0, h, ans = 0, m; n long long int gcd(long long int a, long long int b) { n   if (b == 0) return a; n   return gcd(b, a % b); n } n long long int lcm(long long int x, long long int y) { n   return (x * y) / gcd(x, y); n } n long long int pot(long long int a, long long int b) { n   long long int r = 1; n   while (b) { n     if (b % 2) r = (r * a) % mod; n     a = (a * a) % mod; n     b /= 2; n   } n   return r % mod; n } n void pr(long long int a[], long long int n) { n   for (long long int i = 1; i <= n; i++) cout << a[i] <<   n ; n   cout <<   n ; n } n void read(long long int a[], long long int n) { n   for (long long int i = 1; i <= n; i++) cin >> a[i]; n } n void yes() { cout <<  Yes n ; } n void no() { cout <<  No n ; } n vector<long long int> v[300015]; n void add_edge(long long int x, long long int y) { n   v[x].push_back(y); n   v[y].push_back(x); n } n string s2, ch =   , s, s1 =   , t; n vector<pair<string, long long int> > vs; n vector<pair<pair<long long int, long long int>, long long int> > vvv, vvv2; n vector<pair<long long int, long long int> > vv, vv2; n long long int a[300015], b[300015] = {0}; n long long int bit[300015]; n void update(long long int x, long long int val) { n   for (; x <= n; x += x & -x) bit[x] += val; n } n long long int query(long long int x) { n   long long int sum = 0; n   for (; x > 0; x -= x & -x) { n     sum += bit[x]; n   } n   return sum; n } n int main() { n   ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); n   long long int i = 0, x = 0, z = 0, y = 0, j = 0, q, mx = 0, mm, idx = 0, n                 ok = 0, l = 0, r, negatif = 0, positif = 0; n   set<pair<long long int, long long int> >::iterator itw, ith; n   multiset<long long int>::iterator it; n   multiset<long long int> mst, mst2; n   cin >> n; n   read(a, n); n   for (i = 1; i <= (long long int)(n); i++) { n     vv.push_back({a[i], -i}); n   } n   sort(vv.rbegin(), vv.rend()); n   for (i = 0; i < vv.size(); i++) vv[i].second = abs(vv[i].second); n   cin >> q; n   for (i = 1; i <= (long long int)(q); i++) { n     long long int k, pos; n     cin >> k >> pos; n     vvv.push_back({{k, pos}, i}); n   } n   sort(vvv.begin(), vvv.end()); n   j = 0; n   for (i = 0; i < vvv.size(); i++) { n     long long int k = vvv[i].first.first; n     long long int pos = vvv[i].first.second; n     long long int qidx = vvv[i].second; n     while (j < k) { n       long long int val = vv[j].first; n       long long int idx = vv[j].second; n       update(idx, 1); n       j++; n     } n     l = 1; n     r = n; n     res = 0; n     while (l <= r) { n       long long int mid = (l + r) / 2; n       long long int sum = query(mid); n       if (sum < pos) { n         res = mid; n         l = mid + 1; n       } else { n         r = mid - 1; n       } n     } n     res++; n     b[qidx] = a[res]; n   } n   pr(b, q); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int nmax = 1e5 + 42, mod = 1e9 + 7; n int n, inp[nmax]; n vector<int> seen[nmax], dvd[nmax]; n int phi[nmax]; n int cnt[nmax]; n long long sum_gcd(vector<int> cur) { n   long long alone = 0; n   for (auto w : cur) alone = (alone + w) % mod; n   long long together = 0; n   for (auto p : cur) { n     for (auto q : dvd[p]) { n       together = (together + 1LL * phi[q] * cnt[q]) % mod; n       cnt[q]++; n     } n   } n   for (auto p : cur) { n     for (auto q : dvd[p]) cnt[q] = 0; n   } n   long long ret = (alone + together * 2) % mod; n   return ret; n } n int main() { n   scanf( %i , &n); n   for (int i = 1; i <= n; i++) scanf( %i , &inp[i]); n   for (int i = 1; i < nmax; i++) phi[i] = i; n   for (int i = 2; i < nmax; i++) n     if (phi[i] == i) { n       for (int j = i; j < nmax; j = j + i) phi[j] = phi[j] / i * (i - 1); n     } n   for (int i = 1; i <= n; i++) { n     for (int j = i; j <= n; j = j + i) seen[i].push_back(inp[j]); n   } n   for (int i = 1; i < nmax; i++) { n     for (int j = i; j < nmax; j = j + i) dvd[j].push_back(i); n   } n   long long outp = 0; n   for (int i = 1; i <= n; i++) n     outp = (outp + 1LL * phi[i] * sum_gcd(seen[i])) % mod; n   printf( %lld n , outp); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void cal(long long number, long long x, long long s) { n   long long d = number ^ x; n   long long temp = 2 * number - (s + d); n   if (temp % 2 == 0) { n     cout << 3 << endl; n     cout << d <<     << temp / 2 <<     << temp / 2 << endl; n   } n   if (temp % 2 == 1) { n     number -= 1; n     d = number ^ x; n     temp = 2 * number - (s + d); n     cout << 3 << endl; n     cout << d <<     << temp / 2 <<     << temp / 2 << endl; n   } n } n void solve() { n   long long n; n   cin >> n; n   vector<long long> a(n); n   long long s = 0, x = 0, number = 0; n   for (long long i = 0; i < 57; i++) { n     number = number | 1LL << i; n   } n   for (long long i = 0; i < n; i++) { n     cin >> a[i]; n     s = s + a[i]; n     x = x ^ a[i]; n   } n   cal(number, x, s); n } n signed main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   long long t = 1; n   cin >> t; n   while (t--) solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int kor = 1e7; n int main() { n   long long n, rad, v; n   cin >> n >> rad >> v; n   for (int i = 0; i < n; i++) { n     int l, r; n     cin >> l >> r; n     long long d = r - l; n     int ma = 0; n     while (d < kor) { n       d *= 10; n       rad *= 10; n       ma++; n     } n     long long mnv = 0; n     long long mxv = d; n     while (mnv != mxv) { n       long long cv = (mnv + mxv) / 2; n       double cd = cv + rad * sin((double)cv / rad); n       if (cd < (d / 2)) { n         mnv = cv + 1; n         continue; n       } n       mxv = cv; n     } n     long long ev = mnv; n     mnv = 0; n     mxv = d; n     while (mnv != mxv) { n       long long cv = (mnv + mxv) / 2; n       double cd = cv - rad * sin((double)cv / rad); n       if (cd < (d / 2)) { n         mnv = cv + 1; n         continue; n       } n       mxv = cv; n     } n     long long bv = min(ev, mnv); n     bv *= 2; n     double t = (double)bv / v; n     while (ma > 0) { n       t /= 10; n       rad /= 10; n       ma--; n     } n     cout << setprecision(20) << t << endl; n   } n   cin >> n; n } n


#include <bits/stdc++.h> n using namespace std; n int n, i, v[5005]; n int ans(int st, int dr, int lvl) { n   if (st > dr) return 0; n   int mini = 1000000001; n   int minipoz; n   for (int i = st; i <= dr; i++) { n     if (v[i] <= lvl) { n       return ans(st, i - 1, lvl) + ans(i + 1, dr, lvl); n     } n     if (v[i] < mini) { n       mini = v[i]; n       minipoz = i; n     } n   } n   return min(dr - st + 1, mini - lvl + ans(st, minipoz - 1, mini) + n                               ans(minipoz + 1, dr, mini)); n } n int main() { n   cin >> n; n   for (i = 1; i <= n; i++) cin >> v[i]; n   cout << ans(1, n, 0); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<int> a, b, e; n char s[5]; n long long ans; n int c; n int main() { n   int n, f; n   scanf( %d , &n); n   while (n--) { n     scanf( %s %d , &s, &f); n     if (s[0] ==  0  && s[1] ==  1 ) n       a.push_back(f); n     else if (s[0] ==  1  && s[1] ==  0 ) n       b.push_back(f); n     else if (s[0] ==  0  && s[1] ==  0 ) n       e.push_back(f); n     else { n       c++; n       ans += f; n     } n   } n   int temp = c + 2 * min((int)a.size(), (int)b.size()); n   c += min((int)a.size(), (int)b.size()); n   if (!a.empty()) sort(a.rbegin(), a.rend()); n   if (!b.empty()) sort(b.rbegin(), b.rend()); n   for (int i = 0; i < min((int)a.size(), (int)b.size()); ++i) { n     ans += a[i]; n     ans += b[i]; n   } n   if (a.size() > b.size()) { n     for (int i = min(a.size(), b.size()); i < a.size(); ++i) e.push_back(a[i]); n   } else if (b.size() > a.size()) { n     for (int i = min(a.size(), b.size()); i < b.size(); ++i) e.push_back(b[i]); n   } n   if (!e.empty()) sort(e.rbegin(), e.rend()); n   for (int i = 0; i < e.size(); ++i) { n     if (((temp + 2) / 2) <= c) { n       ans += e[i]; n       temp++; n     } n   } n   printf( %lld , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long int n; n   int m; n   scanf( %I64d %d , &n, &m); n   std::vector<long long int> v(n, 0); n   for (int i = 0; i < m; i++) { n     int x, y; n     scanf( %d%d , &x, &y); n     v[x - 1]++; n     v[y - 1]++; n   } n   long long int res = 0; n   for (int i = 0; i < n; i++) { n     res -= v[i] * (n - 1 - v[i]); n   } n   res /= 2; n   res += (n * (n - 1) * (n - 2)) / 6; n   printf( %I64d , res); n } n


#include <bits/stdc++.h> n using namespace std; n vector<long long> z, q; n long long w, p = 10, l = 60; n long long mul(long long x, long long y) { n   long long z = 0; n   for (; y; y >>= 1, x = x * 2 % p) n     if (y & 1) z = (z + x) % p; n   return z; n } n pair<long long, long long> F(long long x) { n   if (x == 0) return pair<long long, long long>(0, 1); n   if (x & 1) { n     pair<long long, long long> u = F(x - 1); n     return pair<long long, long long>(u.second, (u.first + u.second) % p); n   } else { n     pair<long long, long long> u = F(x / 2); n     return pair<long long, long long>( n         mul(u.first, 2 * u.second + p - u.first), n         (mul(u.first, u.first) + mul(u.second, u.second)) % p); n   } n } n int main() { n   cin >> w; n   for (int i = 0; i < l; i++) n     if (F(i).first == w % p) z.push_back(i); n   for (int i = 0; i < 12; i++) { n     p *= 10; n     q.clear(); n     for (long long j : z) n       for (int k = 0; k < 10; k++) n         if (F(j + k * l).first == w % p) q.push_back(j + k * l); n     z.swap(q); n     l *= 10; n   } n   if (!z.size()) n     puts( -1 ); n   else n     cout << *min_element(z.begin(), z.end()); n } n


#include <bits/stdc++.h> n using namespace std; n const int inf = 1e9 + 7; n string to_string(string s) { return     + s +    ; } n string to_string(char s) { return string(1, s); } n string to_string(const char *s) { return to_string((string)s); } n string to_string(bool b) { return (b ?  true  :  false ); } n template <typename A> n string to_string(A); 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> n string to_string(A v) { n   bool f = 1; n   string r =  { ; n   for (const auto &x : v) { n     if (!f) r +=  ,  ; n     f = 0; n     r += to_string(x); n   } n   return r +  } ; n } n void debug_out() { cout << endl; } n template <typename Head, typename... Tail> n void debug_out(Head H, Tail... T) { n   cout <<     << to_string(H); n   debug_out(T...); n } n const int N = 1e5 + 10; n int n, k; n vector<pair<int, int>> g[N]; n vector<pair<long long, long long>> A1[N]; n pair<long long, long long> f(int u, int p) { n   auto &a1 = A1[u]; n   for (auto it : g[u]) n     if (it.first != p) { n       int v = it.first, c = it.second; n       auto now = f(v, u); n       a1.emplace_back(now.first + c, max(now.first, now.second) + c); n     } n   sort((a1).begin(), (a1).end()); n   reverse((a1).begin(), (a1).end()); n   pair<long long, long long> ans(0, 0); n   for (auto i = (0); i <= (min(int(a1.size()), k - 1) - 1); ++i) { n     ans.first += a1[i].first; n     ans.second += a1[i].first; n   } n   long long cur = ans.second; n   for (auto i = (0); i <= (int(a1.size()) - 1); ++i) { n     if (i <= k - 2) { n       ans.second = n           max(ans.second, cur - a1[i].first + a1[i].second + n                               (k - 1 < int(a1.size()) ? a1[k - 1].first : 0)); n     } else { n       ans.second = max(ans.second, cur + a1[i].second); n     } n   } n   return ans; n } n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cin >> n >> k; n   for (auto i = (1); i <= (n - 1); ++i) { n     int x, y, c; n     cin >> x >> y >> c; n     g[x].emplace_back(y, c); n     g[y].emplace_back(x, c); n   } n   pair<int, int> ans = f(0, -1); n   cout << max(ans.first, ans.second) <<   n ; n   return 0; n } n


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


#include <bits/stdc++.h> n using namespace std; n int N; n int arr[51][51]; n bool checkSum(int num, int row, int col) { n   for (int i = 0; i < N; i++) { n     for (int j = 0; j < N; j++) { n       if (arr[row][i] + (long long int)arr[j][col] == num) { n         return true; n       } n     } n   } n   return false; n } n int main() { n   scanf( %d , &N); n   for (int i = 0; i < N; i++) { n     for (int j = 0; j < N; j++) { n       scanf( %d , &arr[i][j]); n     } n   } n   bool flag = true; n   for (int i = 0; i < N && flag; i++) { n     for (int j = 0; j < N && flag; j++) { n       if (arr[i][j] != 1) { n         flag = checkSum(arr[i][j], i, j); n       } n     } n   } n   if (flag) { n     printf( Yes n ); n   } else { n     printf( No n ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const double eps = 1e-8; n double e[100010], r[100010], p[100010]; n int main() { n   int n; n   { n     scanf( %d , &n); n     for (int i = 0; i < n; i++) { n       scanf( %lf , &p[i]); n     } n     e[0] = p[0]; n     r[0] = p[0]; n     for (int i = 1; i < n; i++) { n       e[i] = e[i - 1] + p[i] + 2.0 * p[i] * r[i - 1]; n       r[i] = (r[i - 1] + 1.0) * p[i]; n     } n     printf( %.8lf n , e[n - 1]); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n bool cmp(pair<char, int> &a, pair<char, int> &b) { return a.second < b.second; } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   string s[3]; n   int i; n   bool imp = false; n   unordered_map<char, int> m; n   vector<pair<char, int>> v; n   for (i = 0; i < 3; i++) { n     cin >> s[i]; n   } n   for (i = 0; i < 3; i++) { n     if (s[i][1] ==  > ) { n       m[s[i][0]]++; n       m[s[i][2]]--; n     } else { n       m[s[i][0]]--; n       m[s[i][2]]++; n     } n   } n   for (auto it = m.begin(); it != m.end(); it++) { n     v.push_back(make_pair(it->first, it->second)); n   } n   if (v[0].second == v[1].second || v[1].second == v[2].second || n       v[0].second == v[2].second) { n     imp = true; n   } n   if (imp) { n     cout <<  Impossible n ; n   } else { n     sort(v.begin(), v.end(), cmp); n     for (i = 0; i < v.size(); i++) { n       cout << v[i].first; n     } n     cout <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void _dbg(string) { cout << endl; } n template <class H, class... T> n void _dbg(string s, H h, T... t) { n   int l = s.find( , ); n   cout << s.substr(0, l) <<   =   << h <<  ,  ; n   _dbg(s.substr(l + 1), t...); n } n template <class T, class U> n ostream &operator<<(ostream &o, const pair<T, U> &p) { n   o <<  (  << p.first <<  ,  << p.second <<  ) ; n   return o; n } n template <class T> n ostream &operator<<(ostream &o, const vector<T> &v) { n   o <<  [ ; n   for (T t : v) { n     o << t <<  , ; n   } n   o <<  ] ; n   return o; n } n class UnionFind { n  public: n   vector<int> par, rank; n   UnionFind(int sz) : par(sz, -1), rank(sz, 0) {} n   int find(int x) { n     if (par[x] < 0) n       return x; n     else n       return par[x] = find(par[x]); n   } n   void unite(int x, int y) { n     x = find(x); n     y = find(y); n     if (x == y) return; n     if (rank[x] < rank[y]) { n       par[y] += par[x]; n       par[x] = y; n     } else { n       par[x] += par[y]; n       par[y] = x; n       if (rank[x] == rank[y]) rank[x]++; n     } n   } n   inline bool same(int x, int y) { return find(x) == find(y); } n   inline int size(int x) { return -par[find(x)]; } n }; n int main() { n   int n; n   cin >> n; n   vector<int> v(n); n   for (int i = (int)(0); i < (int)(n); i++) scanf( %d , &v[i]); n   for (int i = (int)(0); i < (int)(n); i++) v[i]--; n   UnionFind uf(n + 1); n   vector<bool> appeared(n + 1, false); n   appeared[n] = true; n   cout <<  1  ; n   for (int i = (int)(0); i < (int)(n); i++) { n     if (v[i] > 0 && appeared[v[i] - 1]) uf.unite(v[i] - 1, v[i]); n     if (appeared[v[i] + 1]) uf.unite(v[i], v[i] + 1); n     appeared[v[i]] = true; n     int sz = uf.size(n) - 1; n     printf( %d%c , (i + 2) - sz,    n [i == n - 1]); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const double pi = acos(-1.0); n int main() { n   double s, f, r, v; n   int n; n   scanf( %d%lf%lf , &n, &r, &v); n   while (n--) { n     scanf( %lf%lf , &s, &f); n     double ans = 0, d = f - s; n     int cnt = (int)floor(d / (2 * pi * r)); n     ans += cnt * 2 * pi * r / v; n     d -= cnt * (2 * pi * r); n     d /= 2; n     double L = 0, R = d; n     for (int _i = 0; _i < 60; _i++) { n       double mid = (L + R) / 2; n       if (mid + sin(mid / r) * r > d) n         R = mid; n       else n         L = mid; n     } n     ans += 2 * L / v; n     printf( %f n , ans); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long d1[] = {0, 2, 3, 1}; n long long fn1(long long start, long long pos, long long len) { n   long long zzz = len / 4; n   if (len == 1) return start; n   for (long long i = 0; i < 4; i++) { n     long long l = i * zzz; n     long long r = l + (zzz - 1); n     if (l <= pos && pos <= r) return fn1(start + d1[i] * zzz, pos - l, zzz); n   } n   return -1; n } n long long d2[] = {0, 3, 1, 2}; n long long fn2(long long start, long long pos, long long len) { n   long long zzz = len / 4; n   if (len == 1) return start; n   for (long long i = 0; i < 4; i++) { n     long long l = i * zzz; n     long long r = l + (zzz - 1); n     if (l <= pos && pos <= r) return fn2(start + d2[i] * zzz, pos - l, zzz); n   } n   return -1; n } n long long solve() { n   long long n; n   cin >> n; n   n--; n   long long row = n / 3; n   long long col = n % 3; n   if (col == 0) { n     long long pos = row; n     long long temp = 1; n     long long start = 1; n     while (true) { n       if (pos - temp >= 0) { n         pos -= temp; n         temp *= 4; n         start *= 4; n         continue; n       } n       break; n     } n     cout << start + pos <<   n ; n   } n   if (col == 1) { n     long long pos = row; n     long long temp = 1; n     long long start = 2; n     while (true) { n       if (pos - temp >= 0) { n         pos -= temp; n         temp *= 4; n         start *= 4; n         continue; n       } n       break; n     } n     cout << fn1(start, pos, temp) <<   n ; n   } n   if (col == 2) { n     long long pos = row; n     long long temp = 1; n     long long start = 3; n     while (true) { n       if (pos - temp >= 0) { n         pos -= temp; n         temp *= 4; n         start *= 4; n         continue; n       } n       break; n     } n     cout << fn2(start, pos, temp) <<   n ; n   } n   return 0; n } n int main() { n   ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0); n   ; n   long long t = 1; n   cin >> t; n   while (t-- != 0) { n     long long stat = solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<long long> g[100009], df; n long long n, v[100009]; n long long used[100009], x, sum, plu[100009], minu[100009], br; n void dfs(int node) { n   used[node] = 1; n   long long mxpl = 0, mxmi = 0; n   for (int i = 0; i < g[node].size(); i++) { n     if (used[g[node][i]] == 0) { n       dfs(g[node][i]); n       mxpl = max(mxpl, plu[g[node][i]]); n       mxmi = max(mxmi, minu[g[node][i]]); n     } n   } n   if (v[node] + mxpl > mxmi) { n     plu[node] = mxpl; n     minu[node] = mxpl + v[node]; n   } else { n     minu[node] = mxmi; n     plu[node] = minu[node] - v[node]; n   } n } n int main() { n   cin >> n; n   for (int i = 1; i < n; i++) { n     int a, b; n     cin >> a >> b; n     g[a].push_back(b); n     g[b].push_back(a); n   } n   for (int i = 1; i <= n; i++) { n     cin >> v[i]; n   } n   dfs(1); n   for (int i = 1; i <= n; i++) { n   } n   sum = minu[1] + plu[1]; n   cout << sum << endl; n } n


#include <bits/stdc++.h> n using namespace std; n long long num[100500]; n long long d1[15], d2[15]; n int main() { n   long long n; n   long long ans = 0; n   scanf( %lld , &n); n   for (int i = 1; i <= n; i++) { n     scanf( %lld , &num[i]); n   } n   for (int i = 1; i <= n; i++) { n     long long digit = 0; n     long long a = num[i]; n     long long di = 1; n     while (a) { n       d1[++digit] = a % 10; n       a /= 10; n     } n     for (int k = 1; k <= digit; k++) { n       ans += d1[k] * di + d1[k] * di * 10; n       di *= 100; n       di = di % 998244353; n       ans = ans % 998244353; n     } n   } n   ans *= n; n   ans = ans % 998244353; n   printf( %lld , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T> n T sqr(T x) { n   return x * x; n } n template <typename T> n T abs(T x) { n   return x > 0 ? x : -x; n } n int cur = 0; n string get_next() { n   string ans =   ; n   if (cur >= 26) { n     ans =  B ; n   } else { n     ans =  A ; n   } n   ans += (char)( a  + cur % 26); n   cur++; n   return ans; n } n int main() { n   int n, k; n   cin >> n >> k; n   vector<bool> used(n - k + 1); n   for (int i = 0; i < n - k + 1; i++) { n     string s; n     cin >> s; n     if (s ==  NO ) { n       used[i] = true; n     } n   } n   vector<string> ans(n); n   for (int i = 0; i < n; i++) { n     if (i >= k - 1 && used[i - k + 1]) { n       ans[i] = ans[i - k + 1]; n     } else { n       ans[i] = get_next(); n     } n   } n   for (int i = 0; i < n; i++) { n     cout << ans[i] <<    ; n   } n   cout <<   n ; n   return 0; n } n