#include <bits/stdc++.h>   using namespace std;   int root;   void addEdge(vector<int> adj[], int u, int v) {     if (u == -1) {       root = v;       return;     }     adj[u].push_back(v);   }   int main(void) {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     cout.tie(NULL);     vector<int> res;     int n, ctr, c;     cin >> n;     vector<int> adj[n + 1];     vector<bool> vis(n + 1, false);     for (int i = 0; i < n; i++) {       cin >> ctr >> c;       addEdge(adj, ctr, i + 1);       if (c) vis[i + 1] = true;     }     for (int i = 1; i <= n; i++) {       if (!vis[i]) continue;       auto it = adj[i].begin();       for (; it != adj[i].end(); it++) {         if (!vis[*it]) break;       }       if (it == adj[i].end()) res.push_back(i);     }     if (res.size() == 0)       cout << -1;     else       for (int x : res) cout << x <<    ;   }


#include <bits/stdc++.h>   constexpr double EPS = 1e-9;   using namespace std;   int sz = 0;   int n, m;   vector<vector<pair<int, int>>> ss;   bool vis[51][51];   string grid[51];   void dfs(int r, int c) {     vis[r][c] = 1;     ss[sz].push_back({r, c});     for (int i = 0; i < n; i++) {       if (grid[i][c] ==  #  && !vis[i][c]) dfs(i, c);     }     for (int i = 0; i < m; i++) {       if (grid[r][i] ==  #  && !vis[r][i]) dfs(r, i);     }   }   int main() {     cin >> n >> m;     for (int i = 0; i < n; i++) cin >> grid[i];     for (int i = 0; i < n; i++) {       for (int j = 0; j < m; j++) {         if (grid[i][j] ==  #  && !vis[i][j]) {           ss.push_back(vector<pair<int, int>>());           dfs(i, j);           sz++;         }       }     }     for (auto el : ss) {       int rr[51] = {0};       int cc[51] = {0};       for (auto qq : el) {         rr[qq.first] = 1;         cc[qq.second] = 1;       }       int a, b;       a = b = 0;       for (int i = 0; i < 50; i++) {         a += rr[i];         b += cc[i];       }       if (a * b != el.size()) {         cout <<  No ;         return 0;       }     }     cout <<  Yes ;   }


#include <bits/stdc++.h>   using namespace std;   int n;   set<int> S[100001];   int main() {     scanf( %d , &n);     set<int> s;     int x, k;     for (int i = 0; i < n; ++i) {       scanf( %d%d , &x, &k);       if (x && S[k].find(x - 1) == S[k].end()) {         puts( NO );         return 0;       }       S[k].insert(x);     }     puts( YES );     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long long N = 2e5 + 5, inf = 1e9 + 7;   long long n, k, i, j;   long long C[N], f[3][N];   pair<long long, long long> A[N];   inline long long util(long long a, long long b, long long c) {     return f[c][a] - f[c][b];   }   signed main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     long long i, j, a, b, ans = 0, m, temp, c, d, e, ff, cur = 0;     cin >> n >> k;     for (i = 0; i < k; i++) {       cin >> A[i].first >> A[i].second;       C[A[i].first]++;       C[A[i].second + 1]--;     }     sort(A, A + k);     for (i = 1; i < n + 2; i++) C[i] += C[i - 1];     for (i = 1; i < n + 1; i++)       if (C[i]) cur++;     for (i = 1; i < n + 1; i++)       for (j = 1; j < 3; j++)         if (C[i] == j) f[j][i]++;     for (i = 1; i < n + 1; i++)       for (j = 1; j < 3; j++) f[j][i] += f[j][i - 1];     temp = 0;     for (i = 0; i < k; i++)       for (j = i + 1; j < k; j++) {         if (A[i].second < A[j].first) {           temp = cur - util(A[i].second, A[i].first - 1, 1) -                  util(A[j].second, A[j].first - 1, 1);         } else if (A[i].second <= A[j].second) {           a = A[i].first - 1;           b = A[j].first - 1;           c = A[j].first - 1;           d = A[i].second;           e = A[i].second;           ff = A[j].second;           temp = cur - util(b, a, 1) - util(d, c, 2) - util(ff, e, 1);         } else {           a = A[i].first - 1;           b = A[j].first - 1;           c = A[j].first - 1;           d = A[j].second;           e = A[j].second;           ff = A[i].second;           temp = cur - util(b, a, 1) - util(d, c, 2) - util(ff, e, 1);         }         ans = max(ans, temp);       }     cout << ans;   }


#include <bits/stdc++.h>   using namespace std;   const int maxn = (int)1e5 + 1;   int n, rt, tot, sz[maxn], bel[maxn], in[maxn], out[maxn], seg[maxn << 1 | 1];   vector<pair<int, int> > e[maxn];   set<int> sp[maxn];   long long ans;   void dfs(int u, long long dis, int ori) {     sz[u] = 1;     ans += dis;     bel[u] = ori;     in[u] = ++tot;     for (auto it : e[u]) {       int v = it.first;       int w = it.second;       if (!in[v]) {         dfs(v, dis + w, ori);         sz[u] += sz[v];       }     }     out[u] = tot;   }   inline int seg_idx(int L, int R) { return (L + R) | (L < R); }   void seg_upd(int L, int R, int x, int v) {     int rt = seg_idx(L, R);     if (L == R) {       seg[rt] = v;     } else {       int M = (L + R) >> 1, lch = seg_idx(L, M), rch = seg_idx(M + 1, R);       if (x <= M) {         seg_upd(L, M, x, v);       } else {         seg_upd(M + 1, R, x, v);       }       seg[rt] = min(seg[lch], seg[rch]);     }   }   int seg_que(int L, int R, int l, int r) {     if (l <= L && R <= r) return seg[seg_idx(L, R)];     int M = (L + R) >> 1, ret = n + 1;     if (l <= M) ret = min(ret, seg_que(L, M, l, r));     if (M < r) ret = min(ret, seg_que(M + 1, R, l, r));     return ret;   }   int main() {     scanf( %d , &n);     for (int i = 1; i < n; ++i) {       int u, v, w;       scanf( %d%d%d , &u, &v, &w);       e[u].push_back(make_pair(v, w));       e[v].push_back(make_pair(u, w));     }     int *que = bel;     tot = 0;     memset(in + 1, 0, n * sizeof(int));     que[++tot] = 1;     in[1] = tot;     for (int i = 1; i <= n; ++i) {       int u = que[i];       for (auto it : e[u]) {         int v = it.first;         if (!in[v]) {           que[++tot] = v;           in[v] = tot;         }       }     }     for (int i = n; i >= 1; --i) {       int u = que[i], mx = 0;       sz[u] = 1;       for (auto it : e[u]) {         int v = it.first;         if (in[v] > in[u]) {           sz[u] += sz[v];           mx = max(mx, sz[v]);         }       }       if (max(mx, n - sz[u]) << 1 <= n) rt = u;     }     tot = ans = 0;     memset(in + 1, 0, n * sizeof(int));     in[rt] = ++tot;     bel[rt] = rt;     for (auto it : e[rt]) {       int v = it.first;       int w = it.second;       dfs(v, w, v);       sp[sz[v] <<= 1].insert(v);     }     out[rt] = tot;     printf( %lld n , ans <<= 1);     for (int i = 1; i <= n; ++i) seg_upd(1, n, in[i], i);     for (int i = 1; i <= n; ++i) {       int p = 0;       for (auto u : sp[n - i + 1])         if (u != bel[i]) p = u;       if (!p && i == rt) p = rt;       if (p) {         p = seg_que(1, n, in[p], out[p]);       } else {         p = n + 1;         if (in[bel[i]] > 1) p = min(p, seg_que(1, n, 1, in[bel[i]] - 1));         if (out[bel[i]] < n) p = min(p, seg_que(1, n, out[bel[i]] + 1, n));       }       printf( %d%c , p,    n [i == n]);       seg_upd(1, n, in[p], n + 1);       if (i != rt) {         sp[sz[bel[i]]].erase(bel[i]);         sp[--sz[bel[i]]].insert(bel[i]);       }       if (p != rt) {         sp[sz[bel[p]]].erase(bel[p]);         sp[--sz[bel[p]]].insert(bel[p]);       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   map<long long, int> cnt;   int lp[100005];   long long p[100005][102];   void sieve() {     for (int i = 2; i <= 100000; i++) {       if (!lp[i]) {         lp[i] = i;         for (int j = i + i; j <= 100000; j += i)           if (!lp[j]) lp[j] = i;       }     }   }   int main() {     int n, k, a;     long long ans = 0;     sieve();     for (int i = 1; i <= 100000; i++) {       p[i][0] = 1;       for (int j = 1; j <= 100; j++) {         p[i][j] = p[i][j - 1] * i;         if (p[i][j] > 1e9) p[i][j] = 0;       }     }     scanf( %d%d , &n, &k);     for (int i = 0; i < n; i++) {       scanf( %d , &a);       long long cur = 1;       long long mate = 1;       while (a > 1) {         int cnt = 0;         int prime = lp[a];         while (a % prime == 0) {           a /= prime;           cnt++;         }         cnt %= k;         cur *= p[prime][cnt];         mate *= p[prime][(k - cnt) % k];         if (mate > 1e9) mate = 0;       }       if (mate != 0 && cnt.count(mate)) ans += cnt[mate];       cnt[cur]++;     }     printf( %lld n , ans);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     ios_base::sync_with_stdio(false);     cin.tie(nullptr);     cout.tie(nullptr);     int n, k, num = 0;     cin >> n >> k;     vector<pair<int, int> > a(n);     for (int i = 0; i < n; i++) {       cin >> a[i].first >> a[i].second;       a[i].first = -a[i].first;     }     (sort(a.begin(), a.end()));     int fi = a[k - 1].first, sec = a[k - 1].second;     for (int i = 0; i < n; i++) {       if (fi == a[i].first && sec == a[i].second) {         num++;       }     }     cout << num;   }


#include <bits/stdc++.h>   long long gcd(long long A, long long B) {     return A > B ? gcd(B, A) : A ? gcd(B % A, A) : B;   }   long long lcm(long long A, long long B) { return A / gcd(A, B) * B; }   long long pow(long long A, long long B, long long P) {     if (!B) return 1 % P;     if (B == 1) return A % P;     long long h = pow(A, B / 2, P);     h *= h;     h %= P;     if (B % 2) h *= A;     return h % P;   }   using namespace std;   int n, m, q, i, j, I, J;   int a[41][41];   int sum[41][41];   int res[41][41][41][41];   int all[41][41][41][41];   char ch;   int get(int i, int j, int I, int J) {     return sum[I][J] - sum[i - 1][J] - sum[I][j - 1] + sum[i - 1][j - 1];   }   bool f(int k) {     if (k == 0)       return true;     else       return false;   }   int main() {     cin >> n >> m >> q;     for (i = 1; i <= n; i++)       for (j = 1; j <= m; j++) {         cin >> ch;         a[i][j] = ch -  0 ;         sum[i][j] = sum[i - 1][j] + sum[i][j - 1] - sum[i - 1][j - 1] + a[i][j];       }     for (i = n; i >= 1; i--)       for (j = m; j >= 1; j--)         if (!a[i][j]) {           all[i][j][i][j] = 1;           for (I = i - 1; I >= 1; I--)             all[I][j][i][j] = all[I + 1][j][i][j] + f(get(I, j, i, j));           for (J = j - 1; J >= 1; J--)             all[i][J][i][j] = all[i][J + 1][i][j] + f(get(i, J, i, j));           for (I = i - 1; I >= 1; I--)             for (J = j - 1; J >= 1; J--)               all[I][J][i][j] = all[I][J + 1][i][j] + all[I + 1][J][i][j] -                                 all[I + 1][J + 1][i][j] + f(get(I, J, i, j));         }     for (i = 1; i <= n; i++)       for (j = 1; j <= m; j++) {         if (!a[i][j]) res[i][j][i][j] = 1;         for (I = i + 1; I <= n; I++)           res[i][j][I][j] = res[i][j][I - 1][j] + all[i][j][I][j];         for (J = j + 1; J <= m; J++)           res[i][j][i][J] = res[i][j][i][J - 1] + all[i][j][i][J];         for (I = i + 1; I <= n; I++)           for (J = j + 1; J <= m; J++)             res[i][j][I][J] = res[i][j][I][J - 1] + res[i][j][I - 1][J] -                               res[i][j][I - 1][J - 1] + all[i][j][I][J];       }     while (q--) {       cin >> i >> j >> I >> J;       cout << res[i][j][I][J] << endl;     }   }


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


#include <bits/stdc++.h>   using namespace std;   const int MAXN = 5010;   int v[MAXN], n, k;   int vec[1000100][12];   bool vis[1000100];   void crivo() {     for (int i = 1; i <= 1000001; i++) {       set<int> s;       for (int j = 0; j <= 1000000; j += i) {         for (int t = 1; t <= 11; t++) {           int at = vec[j][t];           if (at == -1) break;           s.insert(at);           if (s.size() > 2 * k) {             vis[i] = true;             break;           }         }         if (vis[i] == true) break;       }       if (vis[i] == true) continue;       int q = 0;       unordered_map<int, int> mp;       for (auto j : s) mp[v[j] % i]++;       for (auto j : mp) q += j.second - 1;       if (q > k) vis[i] = true;       if (vis[i] == false) {         printf( %d n , i);         return;       }     }   }   vector<int> vet[1000100];   int cnt[1000100];   int main() {     memset(cnt, 0, sizeof cnt);     memset(vec, -1, sizeof vec);     scanf( %d %d , &n, &k);     for (int i = 1; i <= n; i++) scanf( %d , &v[i]);     for (int i = 1; i <= n; i++) {       for (int j = 1; j <= n; j++) {         if (i == j) continue;         int h = abs(v[i] - v[j]);         if (cnt[h] > 10) continue;         vec[h][cnt[h] + 1] = i, cnt[h]++;       }     }     crivo();   }


#include <bits/stdc++.h>   using namespace std;   int a[1505];   int main() {     ios::sync_with_stdio(false), cin.tie(0);     int n;     cin >> n;     for (int i = 1; i <= n; i++) {       cin >> a[i];     }     bool isEven = true;     for (int i = 1; i <= n; i++) {       for (int j = i + 1; j <= n; j++) {         if (a[i] > a[j]) {           isEven ^= 1;         }       }     }     int q;     cin >> q;     for (int qq = 0; qq < q; qq++) {       int l, r;       cin >> l >> r;       int len = r - l + 1;       isEven ^= ((len * (len - 1)) / 2) % 2;       cout << (isEven ?  even  :  odd ) <<   n ;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n;     cin >> n;     long long int p = 1;     for (int i = 0; i < 5; i++) {       p *= n - i;     }     cout << p * (p / 120);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int inf = 1e9 + 7;   const long long INF = 1e18L + 7;   template <class n, class second>   ostream &operator<<(ostream &p, pair<n, second> x) {     return p <<  <  << x.first <<  ,   << x.second <<  > ;   }   template <class n>   auto operator<<(ostream &p, n y) ->       typename enable_if<!is_same<n, string>::value,                          decltype(y.begin(), p)>::type {     int o = 0;     p <<  { ;     for (auto c : y) {       if (o++) p <<  ,  ;       p << c;     }     return p <<  } ;   }   void dor() { cerr <<   n ; }   template <class n, class... second>   void dor(n p, second... y) {     cerr << p <<    ;     dor(y...);   }   template <class n, class second>   void mini(n &p, second y) {     if (p > y) p = y;   }   template <class n, class second>   void maxi(n &p, second y) {     if (p < y) p = y;   }   const int N = 57;   int n;   vector<int> G[N];   long double kom[N][N][N][N];   long double bez[N][N];   long double kra[N][N];   int sub[N];   void merge(int v, int u) {     long double tmp[N];     memset(tmp, 0, sizeof tmp);     for (int i = 0; i <= sub[v]; ++i) {       for (int j = 0; j <= sub[u]; ++j) {         tmp[i + j] +=             bez[v][i] * kra[u][j] * kom[sub[v] + sub[u]][i + j][sub[v]][i];       }     }     memcpy(bez[v], tmp, sizeof bez[v]);     sub[v] += sub[u];   }   void dfs(int v, int p) {     bez[v][0] = 1;     for (auto u : G[v]) {       if (u == p) continue;       dfs(u, v);       merge(v, u);       ;     }     sub[v]++;     if (v == 1)       ;     for (int second = 0; second <= sub[v]; ++second) {       for (int wyb = 1; wyb <= sub[v]; ++wyb) {         int oki = max(wyb - 1, second - 1);         long double prob = 1.0 / sub[v];         if (wyb > second) prob /= 2;         kra[v][second] += bez[v][oki] * prob;       }     }   }   int32_t main() {     ios_base::sync_with_stdio(0);     cin.tie(NULL);     for (int i = 0; i < N; ++i) {       for (int j = 0; j <= i; ++j) {         kom[i][j][0][0] = 1;       }     }     for (int i = 1; i < N; ++i) {       for (int j = 0; j < N; ++j) {         for (int k = 1; k < N; ++k) {           for (int l = 0; l < N; ++l) {             long double ok = (long double)j / i;             if (j > 0 && l > 0)               kom[i][j][k][l] += ok * kom[i - 1][j - 1][k - 1][l - 1];             kom[i][j][k][l] += (1 - ok) * kom[i - 1][j][k - 1][l];           }         }       }     };     cout << fixed << setprecision(15);     cin >> n;     if (n == 1) {       cout << 1 <<   n ;       return 0;     }     for (int i = 1; i < n; ++i) {       int a, b;       cin >> a >> b;       G[a].push_back(b);       G[b].push_back(a);     }     for (int i = 1; i <= n; ++i) {       for (int j = 1; j <= n; ++j) {         memset(bez[j], 0, sizeof bez[j]);         memset(kra[j], 0, sizeof kra[j]);       }       memset(sub, 0, sizeof sub);       dfs(i, -1);       cout << bez[i][0] <<   n ;     }   }


#include <bits/stdc++.h>   using namespace std;   const int MAXN = 200200;   int n, k, cnt[MAXN];   long long ans;   bool f[MAXN];   vector<int> g[MAXN];   void dfs(int v, int p = -1) {     cnt[v] = f[v];     for (const int &u : g[v])       if (u != p) dfs(u, v), cnt[v] += cnt[u];   }   void solve(int v, int p = -1) {     for (const int &u : g[v])       if (u != p) {         ans += min(2 * k - cnt[u], cnt[u]);         solve(u, v);       }   }   int main() {     scanf( %d %d , &n, &k);     for (int i = 1, x; i <= 2 * k; ++i) scanf( %d , &x), f[x] = true;     for (int i = 1, u, v; i < n; ++i)       scanf( %d %d , &u, &v), g[u].push_back(v), g[v].push_back(u);     dfs(1);     solve(1);     printf( %lld n , ans);   }


#include <bits/stdc++.h>   using namespace std;   int r, c;   string str_col[1000006];   string str_dir[1000006];   vector<int> e[1000006], rev[1000006];   int col[1000006], vis[1000006], vis2[1000006];   int cyc_len, black_cnt;   inline int id(int x, int y) { return c * x + y; }   vector<int> st;   int got[1000006];   void dfs2(int v, int mod, int d = 0) {     if (0) cerr <<  vis2   << v <<     << col[v] <<   n ;     vis2[v] = 1;     got[d] |= col[v];     for (int u : rev[v]) {       if (not vis2[u]) dfs2(u, mod, (d + 1) % mod);     }   }   void dfs(int v) {     if (vis[v] or vis2[v]) return;     if (0) cerr <<  dfs   << v <<   n ;     vis[v] = 1;     st.push_back(v);     for (int u : e[v]) {       if (not vis[u])         dfs(u);       else {         int cyc = st.end() - find(st.begin(), st.end(), u);         if (cyc == 0) continue;         fill(got, got + cyc, 0);         if (0) cerr <<  call cyc   << v <<     << u <<   n ;         dfs2(u, cyc);         black_cnt += accumulate(got, got + cyc, 0);         cyc_len += cyc;       }     }     st.pop_back();   }   void solve() {     cin >> r >> c;     for (int i = 0; i < r; i++) cin >> str_col[i];     for (int i = 0; i < r; i++) cin >> str_dir[i];     for (int x = 0; x < r; x++)       for (int y = 0; y < c; y++) {         int nx = x, ny = y;         if (str_dir[x][y] ==  U ) nx--;         if (str_dir[x][y] ==  D ) nx++;         if (str_dir[x][y] ==  L ) ny--;         if (str_dir[x][y] ==  R ) ny++;         e[id(x, y)].push_back(id(nx, ny));         rev[id(nx, ny)].push_back(id(x, y));         if (0) cerr <<  rev   << id(nx, ny) <<     << id(x, y) <<   n ;         col[id(x, y)] = str_col[x][y] ==  0 ;       }     int n = r * c;     for (int i = 0; i < n; i++) {       if (not vis[i] and not vis2[i]) {         if (0) cerr <<  call dfs   << i <<   n ;         dfs(i);       }     }     cout << cyc_len <<     << black_cnt <<   n ;   }   void reset() {     for (int i = 0; i < r * c; i++) {       e[i].clear();       rev[i].clear();       vis[i] = vis2[i] = cyc_len = black_cnt = 0;     }   }   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     int t = 1;     cin >> t;     for (int no = 1; no <= t; no++) {       solve();       reset();     }   }


#include <bits/stdc++.h>   using namespace std;   long long n, m;   vector<vector<long long> > a, b, orig;   bool swapd = false;   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     cout.tie(NULL);     cin >> n >> m;     orig.resize(n);     for (long long i = 0; i < n; i++) {       orig[i].resize(m);       for (long long j = 0; j < m; j++) {         orig[i][j] = i * m + j + 1;       }     }     if (n > m) {       swap(n, m);       swapd = true;     }     if (n == 1 && m == 1) {       cout <<  YES n ;       cout <<  1 n ;       return 0;     }     if (n == 1 && m == 2) {       cout <<  NO n ;       return 0;     }     if (n == 1 && m == 3) {       cout <<  NO n ;       return 0;     }     if (n == 2 && m == 2) {       cout <<  NO n ;       return 0;     }     if (n == 2 && m == 3) {       cout <<  NO n ;       return 0;     }     if (n == 3 && m == 3) {       cout <<  YES n6 1 8 n7 5 3 n2 9 4 n ;       return 0;     }     if (n == 1 && m >= 4) {       if (swapd == false) {         cout <<  YES n ;         for (long long i = 2; i <= m; i += 2) cout << i <<    ;         for (long long i = 1; i <= m; i += 2) cout << i <<    ;       } else {         cout <<  YES n ;         for (long long i = 2; i <= m; i += 2) cout << i <<   n ;         for (long long i = 1; i <= m; i += 2) cout << i <<   n ;       }       return 0;     }     cout <<  YES n ;     a.resize(n);     if (swapd) {       for (long long i = 0; i < n; i++) {         a[i].resize(m);         for (long long j = 0; j < m; j++) a[i][j] = orig[j][i];       }     } else {       for (long long i = 0; i < n; i++) {         a[i].resize(m);         for (long long j = 0; j < m; j++) {           a[i][j] = i * m + j + 1;         }       }     }     b.resize(n);     for (long long i = 0; i < n; i++) {       b[i].resize(m);       if (i % 2 == 1) {         b[i] = a[i];         continue;       }       for (long long j = 0; j < m; j++) {         b[i][j] = a[i][((j - 2) % m + m) % m];       }     }     a = b;     for (long long j = 0; j < m; j++) {       if (j % 2 == 0) {         for (long long i = 0; i < n; i++) {           b[i][j] = a[(i + 1) % n][j];         }       }     }     if (swapd) {       for (long long j = 0; j < m; j++) {         for (long long i = 0; i < n; i++) cout << b[i][j] <<    ;         cout <<   n ;       }     } else {       for (long long i = 0; i < n; i++) {         for (long long j = 0; j < m; j++) {           cout << b[i][j] <<    ;         }         cout <<   n ;       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long long mod = 1000000007;   const double PI = acos(-1.0);   const double epsilon = PI / 180.0;   long long powmod(long long a, long long b) {     long long res = 1;     a %= mod;     assert(b >= 0);     for (; b; b >>= 1) {       if (b & 1) res = res * a % mod;       a = a * a % mod;     }     return res;   }   long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; }   const int N = 3e3 + 10, M = 1e6 + 10;   int n;   int a[N];   long long num[N][N], f[N][N];   int main() {     int _;     cin >> _;     while (_--) {       memset(num, 0, sizeof num);       memset(f, 0, sizeof f);       cin >> n;       for (int i = 1; i < n + 1; i++) cin >> a[i];       for (int i = 1; i < n + 1; i++) {         for (int j = n + 1 - 1; j >= i + 1; j--) {           num[i][j] = num[i][j + 1];           if (a[i] == a[j]) num[i][j]++;         }       }       for (int k = 1; k < n + 1; k++) {         for (int i = 1; i < k + 1; i++) {           f[i][k] = f[i - 1][k] + num[i][k + 1];         }       }       long long ans = 0;       for (int i = 1; i < n + 1; i++) {         for (int j = i + 1; j < n + 1; j++) {           if (a[i] != a[j]) continue;           ans += (f[j - 1][j] - f[i][j]);         }       }       cout << ans <<   n ;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   using namespace chrono;   const int infinity = (int)1e9 + 42;   const int64_t llInfinity = (int64_t)1e18 + 256;   const int module = (int)1e9 + 7;   const long double eps = 1e-8;   mt19937_64 randGen(system_clock().now().time_since_epoch().count());   inline void raiseError(string errorCode) {     cerr <<  Error :   << errorCode << endl;     exit(42);   }   signed main() {     ios_base::sync_with_stdio(false);     int n, m;     cin >> n >> m;     vector<vector<int> > g(n);     for (int i = 0; i < m; i++) {       int a, b;       cin >> a >> b;       a--;       b--;       g[a].push_back(b);       g[b].push_back(a);     }     vector<int> d(n, INT_MAX);     vector<int> p(n, -1);     queue<int> q;     q.push(0);     d[0] = 0;     while (!q.empty()) {       int v = q.front();       q.pop();       for (int to : g[v]) {         if (d[to] == INT_MAX) {           d[to] = d[v] + 1;           p[to] = v;           q.push(to);         }       }     }     int ans1 = d[n - 1];     int idx2 = -1;     for (int i = 0; i < n; i++) {       if (d[i] == 2) {         idx2 = i;         break;       }     }     if (ans1 > 4 && idx2 >= 0) {       cout << 4 << endl;       cout << 1 <<     << p[idx2] + 1 <<     << idx2 + 1 <<     << 1 <<     << n            << endl;       return 0;     }     if (ans1 > 5) {       vector<char> used(n, false);       vector<vector<int> > komps;       vector<int> kompId(n, -1);       used[0] = true;       function<void(int)> dfs = [&](int v) {         if (used[v]) {           return;         }         used[v] = true;         kompId[v] = (int)komps.size() - 1;         komps.back().push_back(v);         for (int to : g[v]) {           dfs(to);         }       };       for (int i : g[0]) {         if (!used[i]) {           komps.emplace_back();           dfs(i);         }       }       for (int i = 0; i < n; i++) {         sort(g[i].begin(), g[i].end());       }       for (auto &komp : komps) {         sort(komp.begin(), komp.end());         bool klika = true;         int ngsz = (int)komp.size();         int x = -1, y = -1;         for (int i : komp) {           if ((int)g[i].size() == ngsz) {             continue;           }           for (int j : komp) {             if (i == j) {               continue;             }             if (!binary_search(g[i].begin(), g[i].end(), j)) {               x = i;               y = j;               break;             }           }           if (x < 0 || y < 0) {             for (;;)               ;           }           klika = false;           break;         }         if (klika) {           continue;         }         int z = -1;         vector<int> xReach;         for (int i = 1; i < n; i++) {           if (i == x || i == y) {             continue;           }           bool findX = binary_search(g[i].begin(), g[i].end(), x);           bool findY = binary_search(g[i].begin(), g[i].end(), y);           if (findX && findY) {             z = i;             break;           } else if (findX) {             xReach.push_back(i);           }         }         if (z < 0) {           z = -1;           for (int i : xReach) {             for (int j : g[i]) {               if (j == x || j == 0 ||                   binary_search(xReach.begin(), xReach.end(), j)) {                 continue;               }               y = j;               z = i;               break;             }             if (z >= 0) {               break;             }           }           assert(z >= 0);         }         cout << 5 << endl;         cout << 1 <<     << x + 1 <<     << z + 1 <<     << y + 1 <<     << x + 1              <<     << n << endl;         return 0;       }     }     if (ans1 == INT_MAX) {       cout << -1 << endl;       return 0;     }     cout << ans1 << endl;     vector<int> res;     int kur = n - 1;     while (kur >= 0) {       res.push_back(kur + 1);       kur = p[kur];     }     reverse(res.begin(), res.end());     for (int i : res) {       cout << i <<    ;     }     cout << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long cum[(int)1e6], input[(int)1e6], k, ans;   char c;   int sz, t;   map<long long, vector<int>> pos;   int main() {     cin >> sz >> k;     for (int i = 0; i < sz; i++) {       scanf( %c%d , &c, &t);       input[i] = t;       if (i == 0) {         cum[0] = t;       } else {         cum[i] = cum[i - 1] + input[i];       }       pos[cum[i]].push_back(i);     }     for (long long target = 1; abs(target) <= 1e16; target *= k) {       for (int i = 0; i < sz; i++) {         if (i == 0) {           ans += pos[target].size();         } else {           ans += pos[target + cum[i - 1]].end() -                  lower_bound(pos[target + cum[i - 1]].begin(),                              pos[target + cum[i - 1]].end(), i);         }       }       if ((k == 1) || (k == -1 && target == k)) {         cout << ans << endl;         return 0;       }     }     cout << ans << endl;     return 0;   }


#include <bits/stdc++.h>   const double pi = acos(-1);   const int MOD = 1e9 + 7;   const long long INF = 1e18 + 7;   const int MAXN = 1e3 + 5;   const double eps = 1e-9;   using namespace std;   long long a[MAXN], dist[MAXN];   pair<long long, long long> loc[MAXN];   int par[MAXN];   long long cost(int x, int y, long long d) {     return d * (abs(loc[x].first - loc[y].first) +                 abs(loc[x].second - loc[y].second));   }   int main() {     long long n, d;     scanf( %I64d , &(n)), scanf( %I64d , &(d));     for (int i = 1; i + 1 < n; i++) scanf( %I64d , &(a[i]));     for (int i = 0; i < n; i++)       scanf( %I64d , &(loc[i].first)), scanf( %I64d , &(loc[i].second)),           dist[i] = INF;     dist[0] = 0;     priority_queue<pair<long long, long long>,                    vector<pair<long long, long long> >,                    greater<pair<long long, long long> > >         pq;     pq.push({0, 0});     while (!pq.empty()) {       pair<long long, long long> cur = pq.top();       pq.pop();       for (int i = 0; i < n; i++) {         if (i == cur.second) continue;         long long c = cost(i, cur.second, d);         if (dist[i] > dist[cur.second] + c - a[i]) {           dist[i] = dist[cur.second] + c - a[i];           pq.push({dist[i], i});           par[i] = cur.second;         }       }     }     printf( %I64d n , (dist[n - 1]));     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int inf = 0x3f3f3f3f, oo = inf;   inline long long read() {     register long long x = 0, f = 1;     register char c = getchar();     for (; !isdigit(c); c = getchar())       if (c ==  - ) f = -1;     for (; isdigit(c); c = getchar()) x = (x << 1) + (x << 3) + (c ^ 48);     return x * f;   }   void write(long long x) {     if (x < 0) x = -x, putchar( - );     if (x >= 10) write(x / 10);     putchar(x % 10 +  0 );   }   void writeln(long long x) {     write(x);     puts(  );   }   const long long maxn = 200005;   long long n, a[maxn], k;   bool check(long long m) {     long double sum = 0, s;     long long p, q;     for (register long long i = (0); i < (n); ++i) {       if (a[i] == 0) continue;       q = n - 1 - i + m - 1;       p = m - 1;       p = min(p, q - p);       s = a[i];       while (p) {         s = s * q / p;         p--, q--;         if (s >= k) return true;       }       sum += s;       if (sum >= k) return true;     }     return false;   }   long long solve(long long l, long long r) {     long long mid, ans;     while (l <= r) {       mid = (l + r) >> 1;       if (check(mid)) {         ans = mid;         r = mid - 1;       } else         l = mid + 1;     }     return ans;   }   signed main() {     n = read(), k = read();     for (register long long i = (0); i < (n); ++i) {       a[i] = read();       if (a[i] >= k) {         puts( 0 );         return 0;       }     }     writeln(solve(1, k));     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int sum[100010], ans[100010];   int data[100010];   bool sign[100010];   int main() {     int n, m, c;     while (scanf(  %d %d %d , &n, &m, &c) == 3) {       for (int i = 1; i <= n; i++) {         scanf(  %d , &ans[i]);       }       for (int i = 1; i <= m; i++) {         scanf(  %d , &sum[i]);         sum[i] += sum[i - 1];       }       memset(sign, 0, sizeof(sign));       for (int i = 1; i <= m && i <= n; i++) {         if (i + m - 1 <= n) {           ans[i] += sum[i];           sign[i] = 1;         } else {           ans[i] += sum[i] - sum[i + m - 1 - n];           sign[i] = 1;         }       }       for (int i = n; n - i + 1 <= m && i >= 1 && !sign[i]; i--) {         if (i - m + 1 >= 1) {           ans[i] += sum[m] - sum[m + i - n - 1];           sign[i] = 1;         } else {           ans[i] += sum[m] - sum[m + i - n - 1] - (sum[m] - sum[i]);           sign[i] = 1;         }       }       for (int i = 1; i <= n; i++) {         if (sign[i]) continue;         ans[i] += sum[m];       }       for (int i = 1; i < n; i++) {         printf( %d  , ans[i] % c);       }       printf( %d n , ans[n] % c);     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   vector<int> v[1000005];   int n, ar[100005];   int main() {     cin >> n;     set<int> st;     for (int i = 0; i < n; i++) {       cin >> ar[i];       st.insert(ar[i]);       v[ar[i]].push_back(i + 1);     }     vector<int> fr(st.begin(), st.end());     int mx = 0, l = 1, r = n, mnd = INT_MAX;     for (int i = 0; i < fr.size(); i++) {       int x = fr[i];       if (v[fr[i]].size() >= mx) {         if (mx < v[fr[i]].size() || v[x][v[x].size() - 1] - v[x][0] < mnd) {           l = v[x][0];           r = v[x][v[x].size() - 1];           mnd = v[x][v[x].size() - 1] - v[x][0];           mx = v[x].size();         }       }     }     cout << l <<     << r << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 100005;   struct Edge {     int to, next;   } e[N << 1];   int in[N], head[N], sz;   int del[N], leg[N];   int cnt[N];   int res, n;   void addedge(int u, int v) {     e[sz].to = v;     e[sz].next = head[u];     head[u] = sz++;   }   void init() {     memset(head, -1, sizeof(head));     memset(in, 0, sizeof(in));     memset(del, 0, sizeof(del));     memset(cnt, 0, sizeof(cnt));     sz = 0;     res = 1;   }   void dfs1(int u, int f = 0) {     int f1 = 0, f2 = 0;     for (int i = head[u]; i != -1; i = e[i].next) {       int v = e[i].to;       if (v == f) continue;       f1 = 1;       dfs1(v, u);       if (del[v]) f2 = 1;     }     if (!f1 || (f2 && in[u] < 3)) del[u] = 1;   }   void calc(int u) {     leg[u] = 0;     for (int i = head[u]; i != -1; i = e[i].next) {       int v = e[i].to;       if (del[v]) leg[u]++;     }     leg[u] = min(leg[u], 2);   }   bool check(int u) {     int cnt = 0;     for (int i = head[u]; i != -1; i = e[i].next) {       int v = e[i].to;       if (!del[v] && in[v] - leg[v] > 1) ++cnt;     }     return cnt > 2;   }   void solve() {     init();     int x, y;     for (int i = 1; i < n; i++) {       scanf( %d%d , &x, &y);       addedge(x, y);       addedge(y, x);       in[x]++;       in[y]++;     }     int s = 0;     for (int i = 1; i <= n; i++)       if (in[s] < in[i]) s = i;     if (in[s] <= 2) {       printf( Yes n );       return;     }     dfs1(s);     for (int i = 1; i <= n; i++) calc(i);     int flag = 0;     for (int i = 1; i <= n; i++) flag |= check(i);     printf(!flag ?  Yes n  :  No n );   }   int main() {     while (~scanf( %d , &n)) solve();     return 0;   }


#include <bits/stdc++.h>   int main() {     int t;     scanf( %d , &t);     int a[t], b[t], c[t];     for (int i = 0; i < t; i++) scanf( %d %d %d , &a[i], &b[i], &c[i]);     for (int i = 0; i < t; i++) {       if (a[i] == b[i] && b[i] == c[i]) {         printf( YES n );         printf( %d %d %d n , a[i], a[i], a[i]);       } else if (a[i] == b[i] && a[i] > c[i]) {         printf( YES n );         printf( %d %d %d n , a[i], c[i], c[i]);       } else if (b[i] == c[i] && b[i] > a[i]) {         printf( YES n );         printf( %d %d %d n , b[i], a[i], a[i]);       } else if (a[i] == c[i] && a[i] > b[i]) {         printf( YES n );         printf( %d %d %d n , a[i], b[i], b[i]);       } else {         printf( NO n );       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   struct query {     long long int l;     long long int r;     long long int i;   };   query Q[200001];   long long int arr[200001];   long long int f[1000001];   long long int ans[200001];   long long int cnt;   bool compare(query a, query b) {     if ((a.l / 448) != (b.l / 448)) return a.l / 448 < b.l / 448;     return a.r < b.r;   }   void add(long long int a) {     long long int x = f[arr[a]] * f[arr[a]] * arr[a];     cnt -= x;     f[arr[a]]++;     x = f[arr[a]] * f[arr[a]] * arr[a];     cnt += x;   }   void remove(long long int a) {     long long int x = f[arr[a]] * f[arr[a]] * arr[a];     cnt -= x;     f[arr[a]]--;     x = f[arr[a]] * f[arr[a]] * arr[a];     cnt += x;   }   int main() {     ios::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     long long int n, i, q;     cin >> n >> q;     for (i = 0; i < n; i++) cin >> arr[i];     for (i = 0; i < q; i++) {       cin >> Q[i].l >> Q[i].r;       Q[i].i = i;       Q[i].l--;       Q[i].r--;     }     sort(Q, Q + q, compare);     long long int cl = 0, cr = -1;     for (i = 0; i < q; i++) {       long long int L = Q[i].l;       long long int R = Q[i].r;       while (cl > L) {         cl--;         add(cl);       }       while (cr < R) {         cr++;         add(cr);       }       while (cl < L) {         remove(cl);         cl++;       }       while (cr > R) {         remove(cr);         cr--;       }       ans[Q[i].i] = cnt;     }     for (i = 0; i < q; i++) {       cout << ans[i] <<   n ;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n = 0, a = 0;     cin >> n;     int* arr = new int[n];     for (int i = 0; i < n; i++) {       cin >> arr[i];     }     sort(arr, arr + n);     for (int j = 0; j < n; j++) {       cout << arr[j] <<    ;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n, i;     cin >> n;     int a[n];     for (i = 0; i < n; i++) {       cin >> a[i];     }     int j, x;     for (i = 0; i < n; i++) {       for (j = 0; j < n; j++) {         if (a[i] != a[n - j - 1]) {           x = n - 1 - j - i;           break;         }       }       if (j < n) break;     }     for (i = 0; i < n; i++) {       for (j = 0; j < n; j++) {         if (a[j] != a[n - 1]) {           x = max(x, n - 1 - j);           break;         }       }       if (j < n) break;     }     cout << x;   }


#include <bits/stdc++.h>   using namespace std;   const int inf = (int)1e9;   const long long mod = (long long)1e9 + 7;   const double eps = (double)1e-9;   const double pi = acos(-1.0);   const int dx[] = {0, 0, 1, 0, -1};   const int dy[] = {0, 1, 0, -1, 0};   const int N = 100500;   int n, ans, a[N], dp[N];   int main() {     cin >> n;     for (int i = 1; i <= n; i++) cin >> a[i];     ans = inf;     for (int i = 1; i <= n; i++) {       dp[a[i]] = dp[a[i] - 1] + 1;       ans = min(ans, n - dp[a[i]]);     }     cout << ans;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long double PI = 3.14159265359;   long double DEL = 1e-10;   int mod = 1000000007;   long long fpow(long long x, long long n) {     long long res = 1;     while (n) {       if (n & 1) res = res * x % mod;       x = x * x % mod;       n >>= 1;     }     return res;   }   long long gcd(long long a, long long b) {     if (b == 0) return a;     return gcd(b, a % b);   }   void sieve(long long n) {     bool prime[1000006];     memset(prime, true, sizeof(prime));     long long rootn = (long long)sqrt(n);     for (long long p = 2; p <= rootn; p++)       if (prime[p] == true)         for (long long i = p * p; i <= n; i += p) prime[i] = false;     prime[1] = 0;   }   const long long N = 300050;   long long cnt, sum, mid, mx, mn, ans, a[N], b[N];   long long n, m, d, i, j, k, l, p, q, r, t, w, x, y, z;   bool f, f1, f2;   string s;   int32_t main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     cin >> n >> k;     cout << (n / k + 1) * k;   }


#include <bits/stdc++.h>   using namespace std;   int main(int argc, char const *argv[]) {     int n;     cin >> n;     int a[n];     for (int i = 0; i < n; ++i) {       cin >> a[i];     }     int cnt = 0;     std::vector<int> v;     for (int i = n - 1; i > 0; --i) {       if (a[i] != 10000) {         for (int j = i - 1; j >= 0; --j) {           if (a[j] == a[i]) {             a[j] = 10000;           }         }         v.push_back(a[i]);       }     }     if (a[0] != 10000) {       v.push_back(a[0]);     }     cout << v.size() << endl;     for (int i = v.size() - 1; i >= 0; --i) {       cout << v[i] <<    ;     }     cout << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     long long n;     cin >> n;     vector<pair<long long, long long>> a(n);     vector<long long> ord(n);     for (long long i = 0; i < n; i++) {       cin >> a[i].first;       a[i].second = i;     }     sort(a.begin(), a.end());     for (long long i = 0; i < n; i++) {       ord[a[i].second] = i;     }     for (long long i = 0; i < n; i++) {       cout << (a[(ord[i] + 1) % n].first) <<    ;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main(void) {     string second[5] = { Danil ,  Olya ,  Slava ,  Ann ,  Nikita };     int sum = 0;     string t;     cin >> t;     for (int i = 0; i < 5; i++) {       int pos = t.find(second[i], 0);       while (pos != string::npos) {         sum++;         pos = t.find(second[i], pos + 1);       }     }     if (sum == 1)       cout <<  YES  << endl;     else       cout <<  NO  << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int a[102] = {0};   int main() {     int n, m, i, ans = 0, x, l, r, num, mid;     cin >> n >> m;     for (i = 0; i < m; i++) {       cin >> x;       a[x]++;     }     sort(a + 1, a + 101);     l = 1;     r = 10000;     while (l <= r) {       num = 0;       mid = (l + r) >> 1;       for (i = 100; i > 0; i--) {         if (a[i] < mid) break;         num += a[i] / mid;       }       if (num >= n) {         ans = mid;         l = mid + 1;       } else         r = mid - 1;     }     cout << ans << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int mxn = 110;   int i, j, k;   int x, y, z, xx, xy, xz, yx, yy, yz;   int m, n, p;   char a[mxn];   int main() {     scanf( %d , &n);     for (i = 1; i <= n; i++) {       scanf( %s , a);       m = strlen(a);       if (a[0] != a[m - 1]) {         a[0] = a[m - 1];       }       printf( %s n , a);     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n;     scanf( %d , &n);     int k = 0;     int sum = 0;     for (int i = 2; i <= n - 1; i++) {       int z = n;       while (z >= i) {         sum += z % i;         z = z / i;       }       sum += z;       k++;     }     for (int i = 2; i <= n - 1; i++) {       while (k % i == 0 && sum % i == 0) {         sum = sum / i;         k = k / i;       }     }     cout << sum <<  /  << k << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   queue<int> q;   vector<int> v[200001];   set<pair<int, int> > se;   int mdis, node;   void dfs(int x, int l, int p) {     if (l > mdis) {       mdis = l;       node = x;     }     for (int y : v[x]) {       if (y == p) continue;       dfs(y, l + 1, x);     }   }   int main() {     int p, n, k, i, j, x, y, a, b, c;     scanf( %d %d , &n, &k);     p = k + 1;     for (i = 1; i <= k; ++i) {       se.insert(make_pair(p, i));       q.push(i);     }     for (i = k + 2; i <= n; ++i) {       x = q.front();       q.pop();       se.erase(make_pair(p, x));       se.insert(make_pair(p, i));       se.insert(make_pair(i, x));       q.push(i);     }     for (auto it : se) {       x = it.first;       y = it.second;       v[x].push_back(y);       v[y].push_back(x);     }     dfs(p, 0, 0);     mdis = 0;     dfs(node, 0, 0);     printf( %d n , mdis);     for (auto it : se) {       printf( %d %d n , it.first, it.second);     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int t, m, n, z, s, q, p;     cin >> t;     while (t--) {       cin >> n >> m;       int a[n][m], z = 200, s = 0, p = 0;       for (int i = 0; i < n; ++i) {         for (int j = 0; j < m; ++j) {           cin >> a[i][j];           if (a[i][j] < 0) {             ++p;           }           z = min(z, abs(a[i][j]));           s = s + abs(a[i][j]);         }       }       if (p % 2 == 1) {         s = s - 2 * z;         cout << s << endl;       } else         cout << s << endl;     }   }


#include <bits/stdc++.h>   using namespace std;   vector<long long int> x;   vector<vector<int> > adjList;   vector<bool> visited;   long long int ans = 0;   long long int add(long long int a, long long int b) {     return (a + b) % 1000000007;   }   long long int mul(long long int a, long long int b) {     return (a * b) % 1000000007;   }   long long int gcd(long long int a, long long int b) {     if (b == 0)       return a;     else       return gcd(b, a % b);   }   void dfs(int node, unordered_map<long long int, long long int> mp) {     long long int gc;     unordered_map<long long int, long long int> mp2;     for (auto elem : mp) {       gc = gcd(elem.first, x[node]);       ans = add(ans, mul(gc, elem.second));       mp2[gc] += elem.second;     }     ans = add(x[node], ans);     mp2[x[node]] += 1LL;     for (auto elem : adjList[node]) {       if (!visited[elem]) {         visited[elem] = true;         dfs(elem, mp2);       }     }   }   int main() {     ios_base::sync_with_stdio(false);     int n;     cin >> n;     x.resize(n + 1, 0);     visited.resize(n + 1, false);     adjList.resize(n + 1);     for (register int i = 1; i <= n; i++) cin >> x[i];     int a, b;     for (int i = 1; i < n; i++) {       cin >> a >> b;       adjList[a].push_back(b);       adjList[b].push_back(a);     }     unordered_map<long long int, long long int> mp;     visited[1] = true;     dfs(1, mp);     cout << ans <<   n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 3e5 + 9;   const int mod = 1e9 + 7;   int32_t main() {     int t;     cin >> t;     while (t--) {       int n, m;       cin >> n >> m;       if (n % m == 0)         cout <<  YES n ;       else         cout <<  NO n ;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int A[1000010];   int main() {     ios::sync_with_stdio(false);     cin.tie(NULL);     int n;     cin >> n;     for (int i = 0; i < n; i++) cin >> A[i];     string s;     cin >> s;     int la = -1e9, ha = 1e9, lb = -1e9, hb = 1e9;     multiset<int> fg;     for (int i = 0; i < 4; i++) fg.insert(A[i]);     for (int i = 4; i < n; i++) {       fg.insert(A[i]);       if (s[i] ==  0 ) {         if (s[i - 1] ==  0 ) {           if (s[i - 1] ==  0  && s[i - 2] ==  0  && s[i - 3] ==  0  &&               s[i - 4] ==  0 ) {             ha = min(la, *fg.rbegin());           }         } else {           hb = min(hb, *fg.begin() - 1);         }       } else if (s[i] ==  1 ) {         if (s[i - 1] ==  1 ) {           if (s[i - 1] ==  1  && s[i - 2] ==  1  && s[i - 3] ==  1  &&               s[i - 4] ==  1 ) {             lb = max(lb, *fg.begin());           }         } else {           la = max(la, (*fg.rbegin()) + 1);         }       }       fg.erase(fg.find(A[i - 4]));     }     cout << la <<     << hb <<   n ;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     long long n;     vector<long long> v;     cin >> n;     long long a = 1;     while (a * (a + 1) / 2 <= n) v.push_back(a * (a + 1) / 2), a++;     for (long long i = 1; i * (i + 1) / 2 <= n; i++) {       int cur = n - i * (i + 1) / 2;       if (binary_search(v.begin(), v.end(), cur)) {         puts( YES );         return 0;       }     }     puts( NO );     return 0;   }


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


#include <bits/stdc++.h>   using namespace std;   bool primes[1000007];   void sieve(int n) {     for (long long i = 2; i <= n; i++) {       if (primes[i]) continue;       for (long long j = i * i; j <= n; j += i) {         primes[j] = 1;       }     }   }   int main() {     int n;     cin >> n;     sieve(1e6);     while (n--) {       long long tmp;       scanf( %I64d , &tmp);       long long limit = sqrt(tmp);       if (tmp == 1)         printf( NO n );       else if (limit * limit == tmp && tmp % 2 && !primes[limit])         printf( YES n );       else if (tmp == 4)         printf( YES n );       else         printf( NO n );     }   }


#include <bits/stdc++.h>   using namespace std;   int dis[200009];   int con[200009];   bool cmp(int a, int b) { return a < b; }   int main() {     int i, p, j, n, t, y;     int x;     long long int nail;     scanf( %d , &n);     for (i = 0; i <= n - 1; i++) scanf( %d , &dis[i]);     sort(dis, dis + n, cmp);     nail = 99999999999;     j = 0;     y = 0;     for (i = 1; i <= n - 1; i++) {       x = dis[i] - dis[i - 1];       con[i] = x;       if (x < nail) nail = x;     }     for (i = 1; i <= n - 1; i++)       if (nail == con[i]) y++;     printf( %lld %d n , nail, y);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n;     cin >> n;     long long mas[n];     long long sum = 0;     for (int i = 0; i < n; i++) {       cin >> mas[i];       sum += mas[i];     }     sort(mas, mas + n);     int k;     cin >> k;     int c;     while (k > 0) {       cin >> c;       cout << sum - mas[n - c] << endl;       k--;     }   }


#include <bits/stdc++.h>   using namespace std;   const int INF = 1 << 29;   int killed[100000], val[100000];   int main() {     ios::sync_with_stdio(0);     int n, ans = 0;     vector<int> S(1, 0);     cin >> n;     killed[0] = -1;     for (int i = 0; i < n; i++) cin >> val[i];     for (int i = 1; i < n; i++) {       while (S.size() > 0 && val[S.back()] < val[i]) {         killed[i] = max(killed[i], killed[S.back()] + 1);         S.pop_back();       }       if (S.empty()) killed[i] = -1;       ans = max(ans, killed[i] + 1);       S.push_back(i);     }     cout << ans << endl;   }


#include <bits/stdc++.h>   using namespace std;   #pragma comment(linker,  /STACK:400000000 )   int n, t;   int H;   string s;   int ok(int rest, int last) {     int time = 0;     for (int i = 0; i < (last + 1); i++) {       time++;       if (time > t) return 0;       if (time + (last - i) * 2 <= t) return 1;       if (s[i] ==  S ) rest++;       if (s[i] ==  H ) rest--;       if (rest < 0) {         int from = i;         while (rest < 0) {           i++;           if (s[i] ==  S ) rest++;           if (s[i] ==  H ) rest--;         }         time += (i - from) * 3;       }     }     return time <= t;   }   int main() {     cin >> n >> t >> s;     for (int i = 0; i < (n); i++) H += s[i] ==  H ;     int lo = -1, hi = H + 1;     while (lo + 1 < hi) {       int mi = (lo + hi) / 2;       int rest = mi;       int last = 0;       for (int i = 0; i < (n); i++) {         if (s[i] ==  S ) {           if (rest < 0) last = i;           rest++;         }         if (s[i] ==  H ) {           last = i;           rest--;         }       }       if (rest < 0)         lo = mi;       else {         if (ok(mi, last))           hi = mi;         else           lo = mi;       }     }     if (hi == H + 1)       cout << -1 << endl;     else       cout << hi << endl;   }


#include <bits/stdc++.h>   using namespace std;   const long long int N = 2e6, OO = 1e17, M = 1e9 + 7;   pair<long long, long long> p[N];   long long dp[N];   int main() {     ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);     long long n;     cin >> n;     for (int i = 0; i < n; i++) {       cin >> p[i].first >> p[i].second;     }     sort(p, p + n);     for (int i = 0; i < n; i++) {       int t = lower_bound(p, p + n,                           make_pair(p[i].first - p[i].second, (long long)0)) -               p - 1;       if (t < 0)         dp[i] = 1;       else         dp[i] = dp[t] + 1;     }     cout << n - *max_element(dp, dp + n);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int sum = 0;   vector<vector<int>> mat;   vector<string> mat2;   void f1(vector<string>& ans, int f, string s) {     if (f == 0) return;     string s1 = to_string(f) +     + s;     ans.push_back(s1);     sum += f * s.length();     vector<int> temp(2, 0);     temp[0] = f;     temp[1] = s.length();     mat.push_back(temp);     mat2.push_back(s);   }   string frs(char c, int fre) {     string ans =   ;     for (int i = 0; i < fre; i++) {       ans = ans + c;     }     return ans;   }   int main() {     int n, m, k;     cin >> n >> m >> k;     vector<string> ans;     string s;     int f = (m - 1) / 4;     f1(ans, f,  RRRR );     if ((m - 1) % 4) f1(ans, 1, frs( R , (m - 1) % 4));     f = (m - 1) / 4;     f1(ans, f,  LLLL );     if ((m - 1) % 4) f1(ans, 1, frs( L , (m - 1) % 4));     for (int i = 0; i < n - 1; i++) {       f1(ans, 1,  D );       f1(ans, m - 1,  RUD );       f = (m - 1) / 4;       f1(ans, f,  LLLL );       if ((m - 1) % 4) f1(ans, 1, frs( L , (m - 1) % 4));     }     f = (n - 1) / 4;     f1(ans, f,  UUUU );     if ((n - 1) % 4) f1(ans, 1, frs( U , (n - 1) % 4));     int N = ans.size();     if (sum < k) {       cout <<  NO ;     } else {       cout <<  YES  << endl;       vector<string> v;       int cur = 0;       for (int i = 0; i < N; i++) {         if (cur == k) break;         int l = mat[i][0] * mat[i][1];         if (cur + l <= k) {           v.push_back(ans[i]);           cur += l;         } else {           int len = k - cur;           f = len / mat[i][1];           string s;           if (f) {             s = to_string(f) +     + mat2[i];             v.push_back(s);           }           if (len % (mat[i][1])) {             s =  1   + mat2[i].substr(0, len % (mat[i][1]));             v.push_back(s);           }           break;         }       }       N = v.size();       cout << N << endl;       for (int j = 0; j < N; j++) {         cout << v[j] << endl;       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n;     cin >> n;     int s1[n];     int s2[n];     for (int i = 0; i < n; i++) cin >> s1[i];     for (int i = 0; i < n; i++) cin >> s2[i];     for (int i = 0; i < n; i++) s1[i]--, s2[i]--;     int arr[n];     for (int i = 0; i < n; i++) {       arr[s2[i]] = i;     }     for (int i = 0; i < n - 1; i++) {       if (arr[s1[i]] > arr[s1[i + 1]]) {         cout << n - 1 - i <<   n ;         return 0;       }     }     cout <<  0 n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   using ll = long long;      int MOD;   struct modint {      private:          int v;          static int minv(int a, int m) {           a %= m;           assert(a);           return a == 1 ? 1 : int(m - ll(minv(m, a)) * ll(m) / a);       }      public:          modint() : v(0) {}       modint(ll v_) : v(int(v_ % MOD)) { if (v < 0) v += MOD; }       explicit operator int() const { return v; }       friend std::ostream& operator << (std::ostream& out, const modint& n) { return out << int(n); }       friend std::istream& operator >> (std::istream& in, modint& n) { ll v_; in >> v_; n = modint(v_); return in; }          friend bool operator == (const modint& a, const modint& b) { return a.v == b.v; }       friend bool operator != (const modint& a, const modint& b) { return a.v != b.v; }          modint inv() const {           modint res;           res.v = minv(v, MOD);           return res;       }       friend modint inv(const modint& m) { return m.inv(); }       modint neg() const {           modint res;           res.v = v ? MOD-v : 0;           return res;       }       friend modint neg(const modint& m) { return m.neg(); }          modint operator- () const {           return neg();       }       modint operator+ () const {           return modint(*this);       }          modint& operator ++ () {           v ++;           if (v == MOD) v = 0;           return *this;       }       modint& operator -- () {           if (v == 0) v = MOD;           v --;           return *this;       }       modint& operator += (const modint& o) {           v += o.v;           if (v >= MOD) v -= MOD;           return *this;       }       modint& operator -= (const modint& o) {           v -= o.v;           if (v < 0) v += MOD;           return *this;       }       modint& operator *= (const modint& o) {           v = int(ll(v) * ll(o.v) % MOD);           return *this;       }       modint& operator /= (const modint& o) {           return *this *= o.inv();       }          friend modint operator ++ (modint& a, int) { modint r = a; ++a; return r; }       friend modint operator -- (modint& a, int) { modint r = a; --a; return r; }       friend modint operator + (const modint& a, const modint& b) { return modint(a) += b; }       friend modint operator - (const modint& a, const modint& b) { return modint(a) -= b; }       friend modint operator * (const modint& a, const modint& b) { return modint(a) *= b; }       friend modint operator / (const modint& a, const modint& b) { return modint(a) /= b; }   };   namespace IO {       template<class T>       void _R(T &x) { cin >> x; }       void _R(int &x) { scanf( %d , &x); }       void _R(ll &x) { scanf( %lld , &x); }       void _R(double &x) { scanf( %lf , &x); }       void _R(char &x) { x = getchar(); }       void _R(char *x) { scanf( %s , x); }       void R() {}       template<class T, class... U>       void R(T &head, U &... tail) { _R(head), R(tail...); }       template<class T>       void _W(const T &x) { cout << x; }       void _W(const int &x) { printf( %d , x); }       void _W(const ll &x) { printf( %lld , x); }       void _W(const double &x) { printf( %.16f , x); }       void _W(const char &x) { putchar(x); }       void _W(const char *x) { printf( %s , x); }       template<class T, class U>       void _W(const pair<T, U> &x) { _W(x.first), putchar(   ), _W(x.second); }       template<class T>       void _W(const vector<T> &x) { for (auto i = x.begin(); i != x.end(); _W(*i++)) if (i != x.cbegin()) putchar(   ); }       void W() {}       template<class T, class... U>       void W(const T &head, const U &... tail) { _W(head), putchar(sizeof...(tail) ?     :   n ), W(tail...); }   }   using namespace IO;   template <typename T> T pow(T a, long long b) {       assert(b >= 0);       T r = 1; while (b) { if (b & 1) r *= a; b >>= 1; a *= a; } return r;   }      const int maxn = 2e5+50;   const int maxp = maxn*32+5;   const int K = 20;   const int M = 1 << K;   int ls[maxp], rs[maxp], tot;   // 0, 1, cnt, XOR   int t0[maxp], t1[maxp], p[maxp], txor[maxp];      inline void pushup(int x) {       p[x] = p[ls[x]] + p[rs[x]];       t0[x] = t0[ls[x]] | t0[rs[x]];       t1[x] = t1[ls[x]] | t1[rs[x]];   }   inline void XOR(int x, int t, int dep = K) {       if (!x) return;       txor[x] ^= t;       if (t >> (dep - 1) & 1) swap(ls[x], rs[x]);       int a = (t0[x] & (~t)) | (t1[x] & t), b = (t1[x] & (~t)) | (t0[x] & t);       t0[x] = a, t1[x] = b;   }   inline void pushdown(int x, int dep) {       if (txor[x]) {           XOR(ls[x], txor[x], dep - 1), XOR(rs[x], txor[x], dep - 1);           txor[x] = 0;       }   }   inline void insert(int &x, int s, int dep = K) {       if (!x) x = ++tot;       if (!dep) {           t1[x] = s, t0[x] = s ^ (M - 1);           p[x] = 1;           return;       }       insert((s >> (dep - 1) & 1) ? rs[x] : ls[x], s, dep - 1);       pushup(x);   }   inline void split(int &x, int &y, int l, int r, int le, int re, int dep = K) {       if (le <= l && r <= re) {           y = x; x = 0;           return;       }       int mid = l + r >> 1; pushdown(x, dep);       y = ++tot;       if (le <= mid) split(ls[x], ls[y], l, mid, le, re, dep - 1);       if (re > mid) split(rs[x], rs[y], mid + 1, r, le, re, dep - 1);       pushup(x), pushup(y);   }   inline void merge(int &x, int y, int dep = K) {       if (!x || !y) {           x = x + y;           return;       }       if (!dep) return;       pushdown(x, dep), pushdown(y, dep);       merge(ls[x], ls[y], dep - 1), merge(rs[x], rs[y], dep - 1);       pushup(x);   }   inline void OR(int x, int s, int dep = K) {       if (!x) return;       if (!(s & t0[x] & t1[x])) {           XOR(x, s & t0[x], dep);           return;       }       pushdown(x, dep);       if (s >> (dep - 1) & 1) {           XOR(ls[x], 1 << (dep - 1), dep - 1);           merge(rs[x], ls[x], dep - 1);           ls[x] = 0;       }       OR(ls[x], s, dep - 1), OR(rs[x], s, dep - 1);       pushup(x);   }      int main() {       int n, q, x = 0;       R(n, q);       for (int i = 1; i <= n; ++i) {           int v; R(v); insert(x, v);       }       for (; q; --q) {           int oth;           int t, l, r, v; R(t, l, r);           split(x, oth, 0, M - 1, l, r);           if (t == 1) R(v),XOR(oth, M - 1),OR(oth, v ^ (M - 1)),XOR(oth, M - 1);           else if (t == 2) R(v),OR(oth, v);           else if (t == 3) R(v), XOR(oth, v);           else W(p[oth]);           merge(x, oth);       }       return 0;   }


#include <bits/stdc++.h>   using namespace std;   void solve(string s) {     int n = s.size();     s =  #  + s;     vector<int> pi(n + 1);     pi[0] = 0, pi[1] = 0;     for (int i = 2; i <= n; i++) {       int j = pi[i - 1];       while (j > 0 && s[i] != s[j + 1]) j = pi[j];       if (s[i] == s[j + 1]) j++;       pi[i] = j;     }     int dp[n + 1];     for (int i = 0; i <= n; i++) dp[i] = 1;     for (int i = n; i >= 1; i--) dp[pi[i]] += dp[i];     vector<pair<int, int> > res;     res.push_back(pair<int, int>(n, 1));     int j = pi[n];     if (j == 0) {       cout << 1 << endl;       cout << n <<   1  << endl;       return;     }     res.push_back(pair<int, int>(j, dp[j]));     while (j > 0) {       j = pi[j];       if (j == 0) break;       res.push_back(pair<int, int>(j, dp[j]));     }     cout << res.size() << endl;     for (int i = res.size() - 1; i >= 0; i--)       cout << res[i].first <<     << res[i].second << endl;   }   int main() {     string s;     cin >> s;     solve(s);   }


#include <bits/stdc++.h>   using namespace std;   int n;   string txt, ans;   int main() {     ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);     cin >> n >> txt;     for (int i = 0; i < n - (2 + (n & 1)); i++) {       if ((i & 1) == 0 && i) ans +=  - ;       ans += txt[i];     }     if (n > 3) ans +=  - ;     for (int i = n - (2 + (n & 1)); i < n; i++) ans += txt[i];     return cout << ans << endl, 0;   }


#include <bits/stdc++.h>   using namespace std;   const int maxn = 50015;   const int INF = 0x3f3f3f3f;   struct edge {     int to, capacity, rev;     long long cost;     edge() {}     edge(int to, int _capacity, long long _cost, int _rev)         : to(to), capacity(_capacity), cost(_cost), rev(_rev) {}   };   struct Min_Cost_Max_Flow {     int V, PreV[maxn + 5], PreE[maxn + 5];     long long dis[maxn + 5], H[maxn + 5];     vector<edge> G[maxn + 5];     void Init(int n) {       V = n;       for (int i = 0; i <= V; ++i) G[i].clear();     }     void Add_Edge(int from, int to, int cap, long long cost) {       G[from].emplace_back(edge(to, cap, cost, G[to].size()));       G[to].emplace_back(edge(from, 0, -cost, G[from].size() - 1));     }     long long Max_cost_max_flow(int s, int t, int f, int& flow) {       long long res = 0;       fill(H, H + 1 + V, 0);       while (f) {         priority_queue<pair<int, int>> q;         fill(dis, dis + 1 + V, -INF);         dis[s] = 0;         q.push(pair<int, int>(0, s));         while (!q.empty()) {           pair<int, int> now = q.top();           q.pop();           int v = now.second;           if (dis[v] > now.first) continue;           for (int i = 0; i < G[v].size(); ++i) {             edge& e = G[v][i];             if (e.capacity > 0 && dis[e.to] < dis[v] + e.cost + H[v] - H[e.to]) {               dis[e.to] = dis[v] + e.cost + H[v] - H[e.to];               PreV[e.to] = v;               PreE[e.to] = i;               q.push(pair<int, int>(dis[e.to], e.to));             }           }         }         if (dis[t] == -INF) break;         for (int i = 0; i <= V; ++i) H[i] += dis[i];         int d = f;         for (int v = t; v != s; v = PreV[v])           d = min(d, G[PreV[v]][PreE[v]].capacity);         f -= d;         flow += d;         if (H[t] < 0) return res;         res += d * H[t];         for (int v = t; v != s; v = PreV[v]) {           edge& e = G[PreV[v]][PreE[v]];           e.capacity -= d;           G[v][e.rev].capacity += d;         }       }       return res;     }   } fuck;   int main() {     int n;     cin >> n;     fuck.Init(n + n + 2);     int a, b;     int s = 0, t = n + n + 1;     long long k;     for (int i = 1; i <= n; i++) {       scanf( %d%d%lld , &a, &b, &k);       for (int j = 1; j <= n; j++) {         fuck.Add_Edge(i, n + j, 1, (a - 1LL * min(1LL * j - 1, k) * b));       }       fuck.Add_Edge(s, i, 1, 0);       fuck.Add_Edge(i + n, t, 1, 0);     }     int tmp;     long long ans = fuck.Max_cost_max_flow(s, t, INF, tmp);     printf( %lld n , ans);     return 0;   }


#include <bits/stdc++.h>   #pragma GCC optimize(2)   using namespace std;   const int INF = 1e9 + 7;   const int N = 2e5 + 7;   const int M = 5e6 + 7;   int a[N], f[N], ans[N];   int get(int x) {     int k = ceil(sqrt(abs(x) * 1.00));     for (int i = 2; i <= k; i++)       while (x % (i * i) == 0) x /= i * i;     return x;   }   signed main() {     ios::sync_with_stdio(false);     int n;     cin >> n;     for (int i = 1; i <= n; i++) {       cin >> a[i];       a[i] = get(a[i]);     }     f[1] = -1;     for (int i = 2; i <= n; i++) {       for (int j = i - 1; j >= 1; --j)         if (a[i] == a[j]) {           f[i] = j;           break;         }     }     for (int i = 1; i <= n; i++) {       int num = 0;       for (int j = i; j <= n; j++) {         if (f[j] < i && a[j] != 0) num++;         if (!num)           ans[1]++;         else           ans[num]++;       }     }     for (int i = 1; i <= n; i++) cout << ans[i] <<   n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long int dp[100000 + 10];   vector<int> ma[100000 + 10];   int a[100000 + 10];   long long int dp1[100000 + 10];   long long int gcd(long long int x, long long int y) {     return y == 0 ? x : gcd(y, x % y);   }   long long int lcm(long long int x, long long int y) {     long long int tmp = gcd(x, y);     return x / tmp * y;   }   void tree_dp(int fa, int n) {     if (ma[n].size() == 1) {       dp1[n] = 1;       dp[n] = (long long int)a[n];       return;     }     int i;     long long int tmp = 1;     long long int mins = LLONG_MAX;     for (i = 0; i < ma[n].size(); i++) {       if (ma[n][i] == fa) continue;       tree_dp(n, ma[n][i]);       tmp = lcm(tmp, dp1[ma[n][i]]);       if (tmp == 0) tmp = 1;       mins = min(mins, dp[ma[n][i]]);     }     dp[n] = (long long int)a[n] + mins / tmp * tmp * (ma[n].size() - 1);     dp1[n] = tmp * (ma[n].size() - 1);   }   int main() {     int n;     cin >> n;     int i;     long long int all = 0;     for (i = 1; i <= n; i++) {       cin >> a[i];       ma[i].clear();       all += a[i];     }     ma[1].push_back(1);     for (i = 0; i < n - 1; i++) {       int x, y;       cin >> x >> y;       ma[x].push_back(y);       ma[y].push_back(x);     }     tree_dp(1, 1);     cout << all - dp[1] << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long long inf = 1e18;   const long long mod = 1e9 + 7;   inline long long bigmod(long long B, long long P) {     long long R = 1;     while (P > 0) {       if (P & 1) {         R = (R * B) % mod;       }       P >>= 1;       B = (B * B) % mod;     }     return R;   }   const long long mxn = 500;   long long Mod(long long a) {     if (a < 0)       return (a + mod) % mod;     else       return a % mod;   }   long long Mul(long long a, long long b) { return Mod(Mod(a) * Mod(b)); }   long long Add(long long a, long long b) { return Mod(Mod(a) + Mod(b)); }   vector<long long> adj[mxn];   long long vis[mxn], pred[mxn], dist[mxn];   vector<long long> adj2[mxn];   long long vis2[mxn], pred2[mxn], dist2[mxn];   long long n;   bool bfs(long long root) {     queue<long long> q;     q.push(root);     vis[root] = 1;     while (!q.empty()) {       long long u = q.front();       q.pop();       for (long long v : adj[u]) {         if (!vis[v]) {           vis[v] = 1;           dist[v] = dist[u] + 1;           q.push(v);           if (v == n) return true;         }       }     }     return false;   }   bool bfs2(long long root) {     queue<long long> q;     q.push(root);     vis2[root] = 1;     while (!q.empty()) {       long long u = q.front();       q.pop();       for (long long v : adj2[u]) {         if (!vis2[v]) {           vis2[v] = 1;           dist2[v] = dist2[u] + 1;           q.push(v);           if (v == n) return true;         }       }     }     return false;   }   signed main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     ;     long long tt = 1;     for (long long tc = 1; tc <= tt; tc++) {       long long m, f = 0;       cin >> n >> m;       map<pair<long long, long long>, bool> trainase;       for (long long i = 0; i < m; i++) {         long long u, v;         cin >> u >> v;         adj[u].push_back(v);         adj[v].push_back(u);         trainase[{u, v}] = 1;         trainase[{v, u}] = 1;         if (u == n || v == n) f = 1;       }       if (!f)         cout << -1 <<   n ;       else {         for (long long i = 1; i <= n; i++) {           for (long long j = i + 1; j <= n; j++) {             if (!trainase[{i, j}]) {               adj2[i].push_back(j);               adj2[j].push_back(i);             }           }         }         if (!bfs2(1) || !bfs(1)) {           cout << -1 <<   n ;           return 0;         }         bfs(1);         bfs2(1);         cout << max(dist2[n], dist[n]) <<   n ;       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int INF = 0x3f3f3f3f;   const long long INFF = 0x3f3f3f3f3f3f3f3fll;   const long long M = 1e9 + 7;   const long long maxn = 2e5 + 7;   const double eps = 0.00000001;   long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; }   template <typename T>   inline T abs(T a) {     return a > 0 ? a : -a;   }   void exgcd(long long a, long long b, long long &d, long long &x, long long &y) {     if (!b) {       d = a;       x = 1;       y = 0;     } else {       exgcd(b, a % b, d, y, x);       y -= a / b * x;     }   }   int n, m;   int i, j;   bool used[maxn];   vector<int> v[maxn];   int num[maxn], fa[maxn], mx;   int pp = 1;   void print(int mx) {     if (fa[mx]) print(fa[mx]);     long long a, b, t;     for (i = 0; i < v[mx].size(); i++) {       exgcd(pp, m, t, a, b);       printf( %I64d  , (v[mx][i] / t * a % m + m) % m);       pp = v[mx][i];     }   }   int main() {     scanf( %d%d , &n, &m);     for (i = 0; i < n; i++) scanf( %d , &j), used[j] = 1;     for (i = 1; i < m; i++)       if (!used[i]) v[gcd(i, m)].push_back(i);     for (i = 1; i < m; i++) {       if ((num[i] += v[i].size()) > num[mx]) mx = i;       for (j = i << 1; j < m; j += i)         if (num[j] < num[i]) num[j] = num[i], fa[j] = i;     }     printf( %d n , num[mx] + !used[0]);     print(mx);     if (!used[0])       puts( 0 );     else       puts(  );   }


#include <bits/stdc++.h>   int n, m;   using namespace std;   vector<int> used(5005, 0);   vector<int> used2(5005, 0);   int jlo1 = 3;   vector<int> a;   long long res = 0;   vector<vector<int>> graph(5005, a);   vector<int> b;   vector<vector<int>> graph1(5005, b);   int q = 0;   bool flag = false;   void dfs(int p) {     {       q++;       used[p] = 1;       for (int i = 0; i < graph[p].size(); i++) {         if (!used[graph[p][i]]) dfs(graph[p][i]);       }     }   }   void dfs2(int p) {     {       q++;       used2[p] = 1;       for (int i = 0; i < graph[p].size(); i++) {         if (!used2[graph[p][i]]) dfs2(graph[p][i]);       }     }   }   int main() {     int k, t, t1;     cin >> n >> m >> k;     for (int i = 0; i < m; i++) {       cin >> t >> t1;       graph[t].push_back(t1);     }     dfs(k);     vector<pair<int, int>> p_q;     for (int i = 1; i <= n; i++) {       if (!used[i]) {         used2.assign(5005, 0);         q = 0;         dfs2(i);         p_q.push_back(make_pair(q, i));       }     }     sort(p_q.rbegin(), p_q.rend());     long long res = 0;     for (int i = 0; i < p_q.size(); i++) {       if (!used[p_q[i].second]) {         res++;         dfs(p_q[i].second);       }     }     cout << res <<   n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 100010;   vector<int> vec[N], fenw[N];   int n, k, x[N], r[N], f[N], id[N], rev[N];   void update(int who, int p, int v) {     while (p < fenw[who].size()) fenw[who][p] += v, p += p & -p;   }   int pref(int who, int p) {     int ret = 0;     while (p) ret += fenw[who][p], p -= p & -p;     return ret;   }   inline int get(int who, int l, int r) {     if (l > r) return 0;     return pref(who, r) - (l ? pref(who, l - 1) : 0);   }   int main() {     cin >> n >> k;     for (int i = 1; i <= n; ++i) {       scanf( %d %d %d , x + i, r + i, f + i);       id[i] = i, vec[f[i]].emplace_back(i);     }     for (int i = 0; i < N; ++i) {       if (vec[i].empty()) continue;       vec[i].emplace_back(0);       sort(vec[i].begin(), vec[i].end(),            [](int p, int q) { return x[p] < x[q]; });       fenw[i].resize(vec[i].size(), 0);       for (int j = 0; j < vec[i].size(); ++j) {         rev[vec[i][j]] = j, vec[i][j] = x[vec[i][j]];       }     }     sort(id + 1, id + n + 1, [](int i, int j) { return r[i] > r[j]; });     long long ans = 0;     for (int it = 1; it <= n; ++it) {       int i = id[it];       for (int oth = max(0, f[i] - k); oth <= f[i] + k; ++oth) {         if (vec[oth].empty()) continue;         int lt = lower_bound(vec[oth].begin(), vec[oth].end(), x[i] - r[i]) -                  vec[oth].begin();         int rt = upper_bound(vec[oth].begin(), vec[oth].end(), x[i] - 1) -                  vec[oth].begin() - 1;         ans += get(oth, lt, rt);         lt = lower_bound(vec[oth].begin(), vec[oth].end(), x[i] + 1) -              vec[oth].begin();         rt = upper_bound(vec[oth].begin(), vec[oth].end(), x[i] + r[i]) -              vec[oth].begin() - 1;         ans += get(oth, lt, rt);       }       update(f[i], rev[i], 1);     }     cout << ans <<   n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   template <class T1, class T2>   void pr(const pair<T1, T2>& x);   template <class T, size_t SZ>   void pr(const array<T, SZ>& x);   template <class T>   void pr(const vector<T>& x);   template <class T>   void pr(const set<T>& x);   template <class T1, class T2>   void pr(const map<T1, T2>& x);   template <class T>   void pr(const T& x) {     cout << x;   }   template <class Arg, class... Args>   void pr(const Arg& first, const Args&... rest) {     pr(first);     pr(rest...);   }   template <class T1, class T2>   void pr(const pair<T1, T2>& x) {     pr( { , x.first,  ,  , x.second,  } );   }   template <class T>   void prContain(const T& x) {     pr( { );     bool fst = 1;     for (auto& a : x) pr(!fst ?  ,   :   , a), fst = 0;     pr( } );   }   template <class T, size_t SZ>   void pr(const array<T, SZ>& x) {     prContain(x);   }   template <class T>   void pr(const vector<T>& x) {     prContain(x);   }   template <class T>   void pr(const set<T>& x) {     prContain(x);   }   template <class T1, class T2>   void pr(const map<T1, T2>& x) {     prContain(x);   }   void ps() { pr(  n ); }   template <class Arg, class... Args>   void ps(const Arg& first, const Args&... rest) {     pr(first,    );     ps(rest...);   }   template <typename T>   int remin(T& a, const T& b) {     if (b < a) {       a = b;       return true;     }     return false;   }   template <typename T>   int remax(T& a, const T& b) {     if (b > a) {       a = b;       return true;     }     return false;   }   int n;   int G[5005][5005];   short int nxt[5005][5005];   vector<unsigned short int> H[5005];   void dfs_parent(int from, vector<int>& p, int parent = -1) {     for (auto to : H[from])       if (to != parent) {         p[to] = from;         dfs_parent(to, p, from);       }   }   void dfs(int u, int v, int w, const vector<int>& p) {     while (u != v) {       int n = p[v];       remax(G[v][n], w);       remax(G[n][v], w);       v = n;     }   }   void check(int u, int v, int w, const vector<int>& p) {     int exp = 1E9;     while (u != v) {       int n = p[v];       if (G[v][n] == -1) {         G[v][n] = 1000000;         G[n][v] = 1000000;       }       remin(exp, G[v][n]);       v = n;     }     if (exp != w) {       printf( -1 n );       exit(0);     }   }   int main(int argc, const char** argv) {     scanf( %d , &n);     vector<pair<int, int>> edges;     for (int i = 0; i < n - 1; ++i) {       int u, v;       scanf( %d%d , &u, &v);       --u, --v;       G[u][v] = -1;       G[v][u] = -1;       edges.emplace_back(u, v);       H[u].push_back(v);       H[v].push_back(u);     }     vector<vector<int>> p(n, vector<int>(n, -1));     for (int u = 0; u < n; ++u) {       dfs_parent(u, p[u]);     }     int q;     scanf( %d , &q);     vector<pair<pair<int, int>, int>> qu(q);     for (int qq = 0; qq < q; ++qq) {       int u, v, w;       scanf( %d%d%d , &u, &v, &w);       --u, --v;       qu[qq] = make_pair(pair<int, int>(u, v), w);       dfs(u, v, w, p[u]);     }     for (auto [c, w] : qu) {       check(c.first, c.second, w, p[c.first]);     }     vector<int> ans(n - 1);     for (int i = 0; i < n - 1; ++i) {       int u = edges[i].first;       int v = edges[i].second;       int mn = min(u, v);       int mx = max(u, v);       printf( %d  , G[mn][mx] == -1 ? 1000000 : G[mn][mx]);     }     printf(  n );     return 0;   }


#include <bits/stdc++.h>   using namespace std;   void solve() {     long long n;     cin >> n;     vector<long long> a(n);     for (long long i = 0; i < n; i++) cin >> a[i];     long long mx = 0;     vector<long long> ans(n);     for (long long i = 0; i < n; i++) {       long long cnt = a[i], mn = a[i];       vector<long long> tmp(n);       tmp[i] = a[i];       for (long long j = i + 1; j < n; j++) {         if (a[j] >= mn) {           tmp[j] = mn;           cnt += mn;         } else {           tmp[j] = a[j];           mn = a[j];           cnt += mn;         }       }       mn = a[i];       for (long long j = i - 1; j >= 0; j--) {         if (a[j] >= mn) {           tmp[j] = mn;           cnt += mn;         } else {           tmp[j] = a[j];           mn = a[j];           cnt += mn;         }       }       if (cnt > mx) {         mx = cnt;         for (long long j = 0; j < n; j++) ans[j] = tmp[j];       }     }     for (long long i = 0; i < n; i++) cout << ans[i] <<    ;   }   int main() {     ios::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     ;     long long t;     t = 1;     while (t--) solve();   }


#include <bits/stdc++.h>   using namespace std;   int n;   vector<pair<int, int> > v;   vector<int> tail;   int getIdx(int l, int r, int key) {     while (l < r) {       int mid = (l + r) / 2;       if (tail[mid] < key)         r = mid;       else         l = mid + 1;     }     return r;   }   int LIS() {     int length = 1;     tail[0] = v[0].second;     for (int i = 1; i < n; i++) {       if (v[i].second > tail[0])         tail[0] = v[i].second;       else if (v[i].second < tail[length - 1])         tail[length++] = v[i].second;       else {         tail[getIdx(0, length - 1, v[i].second)] = v[i].second;       }     }     return length;   }   int main() {     int x, y;     scanf( %d , &n);     v = vector<pair<int, int> >(n);     for (int i = 0; i < n; i++) {       scanf( %d , &x);       x--;       v[x].first = i;     }     for (int i = 0; i < n; i++) {       scanf( %d , &y);       y--;       v[y].second = i;     }     sort(v.begin(), v.end());     tail = vector<int>(n, 0);     cout << LIS();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   void fast();   int main() {     fast();     int n, cookies[105] = {0};     cin >> n;     int sum = 0;     for (int i = 0; i < n; ++i) {       cin >> cookies[i];       sum += cookies[i];     }     int cnt = 0;     for (int i = 0; i < n; ++i) {       int cook = sum;       cook -= cookies[i];       if (cook % 2 == 0) ++cnt;     }     cout << cnt;     return 0;   }   void fast() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);   }


#include <bits/stdc++.h>   using namespace std;   struct st {     long long n, v;     bool operator<(const st &other) const { return v < other.v; }   };   template <typename T>   string ts(T val) {     std::ostringstream oss;     oss << val;     return oss.str();   }   int main() {     ios_base::sync_with_stdio(0);     long long n, a, b, a1, b1, a2, b2, k1, k2, k3;     cin >> n >> a >> b >> a1 >> b1 >> a2 >> b2;     k1 = a;     k2 = a1;     k3 = a2;     while (k1 + k2 + k3 < n && k1 < b) k1++;     while (k1 + k2 + k3 < n && k2 < b1) k2++;     while (k1 + k2 + k3 < n && k3 < b2) k3++;     cout << k1 <<     << k2 <<     << k3;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 1e5 + 5;   const int K = 1048576;   int n;   long long t[2][2 * K];   vector<int> g[N];   int T[N];   long long h[N], x[N], dp[N];   long long total;   void build() {     for (int i = 0; i < 2 * K; i++) t[0][i] = t[1][i] = 0;   }   void upd(int id, long long X, int type) {     id += K - 1;     while (id > 0) {       t[type][id] += X;       id /= 2;     }   }   long long get(int L, int R, int type) {     if (R == 0)       return 0;     else if (type == 0 && R == K)       return 2e18;     L += K - 1;     R += K - 1;     long long res = 0;     while (L <= R) {       if (L % 2 == 1) res += t[type][L++];       if (R % 2 == 0) res += t[type][R--];       L /= 2;       R /= 2;     }     return res;   }   void dfs(int v) {     upd(T[v], x[v] * T[v], 0);     upd(T[v], x[v], 1);     int l = 1, r = K;     while (l + 1 < r) {       int mid = (l + r) / 2;       if (h[v] + h[v] + get(1, mid - 1, 0) <= total)         l = mid;       else         r = mid;     }     dp[v] = get(1, l - 1, 1);     long long residue = total - get(1, l - 1, 0) - h[v] - h[v];     dp[v] += min(t[0][l + K - 1], residue) / l;     for (int u : g[v]) {       h[u] += h[v];       dfs(u);     }     if (v == 1) {       for (int u : g[v]) dp[v] = max(dp[v], dp[u]);     } else {       long long max1 = 0, max2 = 0;       for (int u : g[v]) {         if (dp[u] > max1) {           max2 = max1;           max1 = dp[u];         } else if (dp[u] > max2) {           max2 = dp[u];         }       }       dp[v] = max(dp[v], max2);     }     upd(T[v], -x[v] * T[v], 0);     upd(T[v], -x[v], 1);   }   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cin >> n >> total;     for (int i = 1; i <= n; i++) cin >> x[i];     for (int i = 1; i <= n; i++) cin >> T[i];     for (int i = 2; i <= n; i++) {       int parent;       cin >> parent >> h[i];       g[parent].push_back(i);     }     build();     h[1] = 0;     dfs(1);     cout << dp[1] <<   n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int z[1000005];   string s;   void z_function() {     int n = (int)s.length();     for (int i = 0; i < n; i++) z[i] = 0;     for (int i = 1, l = 0, r = 0; i < n; ++i) {       if (i <= r) z[i] = min(r - i + 1, z[i - l]);       while (i + z[i] < n && s[z[i]] == s[i + z[i]]) ++z[i];       if (i + z[i] - 1 > r) l = i, r = i + z[i] - 1;     }   }   long long power(long long a, long long b, long long m) {     long long x = 1;     a %= m;     while (b) {       if ((1 & b)) x = x * a % m;       a = a * a % m;       b >>= 1;     }     return x;   }   int y[1000005];   int main() {     int n, m;     scanf( %d , &n);     scanf( %d , &m);     cin >> s;     z_function();     int p = s.length();     for (int i = 1; i <= m; i++) {       scanf( %d , &y[i]);     }     y[m + 1] = n + 1;     int pos = 0;     pos += y[1] - 1;     for (int i = 2; i <= m + 1; i++) {       if (y[i] < y[i - 1] + p) {         if (z[y[i] - y[i - 1]] < p - y[i] + y[i - 1]) {           printf( 0 );           return 0;         }       } else         pos += y[i] - y[i - 1] - p;     }     printf( %lld n , power(26, pos, 1000000007));   }


#include <bits/stdc++.h>   using namespace std;   using ll = long long;   using lll = __int128_t;   const int N = 105;   int n;   vector<int> v[N];   void del(int x, int y) {     v[x].erase(find(v[x].begin(), v[x].end(), y));     v[y].erase(find(v[y].begin(), v[y].end(), x));   }   int f[N], rk[N];   lll dfs(int pos, int fa) {     f[pos] = fa;     vector<lll> vv;     for (auto &i : v[pos])       if (i != fa) vv.push_back(dfs(i, pos));     if (vv.empty()) return 0;     vector<int> ee(vv.size());     auto chk = [&](lll x) {       auto val = vv;       int mxid = -1;       lll mx;       for (int i = N - 1; i >= 0; --i)         if ((x >> i) & 1) {           mx = -(lll(1) << N);           for (int j = 0; j < int(vv.size()); j++)             if (val[j] > mx) mx = val[j], mxid = j;           ee[mxid] = i;           val[mxid] -= lll(1) << i;         }       for (int j = 0; j < int(vv.size()); j++)         if (val[j] >= 0) return 0;       return 1;     };     lll tt = (lll(1) << N) - 1;     for (int i = N - 1; i >= 0; --i)       if (chk(tt - (lll(1) << i))) tt -= lll(1) << i;     assert(chk(tt));     int idx = 0;     for (auto &i : v[pos])       if (i != fa) rk[i] = ee[idx++];     lll ret = 0;     for (int i = 0; i < int(vv.size()); i++)       ret |= vv[i] & (~((lll(1) << ee[i]) - 1)) | lll(1) << ee[i];     return ret;   }   int xx;   inline void upd(int x, int y) {     if (f[x] == y) swap(x, y);     if (!xx || rk[y] > rk[xx]) xx = y;   }   void gete(int pos, int fa) {     for (auto &i : v[pos])       if (i != fa) gete(i, pos), upd(pos, i);   }   int main() {     ios::sync_with_stdio(false);     cin >> n;     int t1, t2;     for (int i = 1; i < n; i++)       cin >> t1 >> t2, v[t1].push_back(t2), v[t2].push_back(t1);     dfs(1, 0);     int x = 1;     while (!v[x].empty()) {       xx = 0;       gete(x, 0);       del(xx, f[xx]);       cout <<  ?   << xx <<     << f[xx] << endl;       cin >> x;     }     cout <<  !   << x << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   void solve() {     long long x, n;     cin >> x >> n;     int pp = n % 4;     long long z;     if (pp == 0)       z = 0;     else if (pp == 1)       z = -n;     else if (pp == 2)       z = 1;     else       z = n + 1;     long long dre = x % 2 == 0 ? 1 : -1;     x += dre * z;     cout << x << endl;   }   int main() {     ios::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     cin.tie(NULL);     long long t = 1;     cin >> t;     while (t--) {       solve();     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int MAXN = (int)(1e5 + 123);   vector<int> gg[MAXN], gb[MAXN];   bool was[MAXN], been[MAXN];   int cnt = 0;   void dfs(int v) {     cnt++;     was[v] = been[v] = true;     for (auto to : gg[v]) {       if (was[to]) continue;       dfs(to);     }   }   int main() {     int n;     cin >> n;     int good;     cin >> good;     for (int i = 1; i <= good; i++) {       int a, b;       cin >> a >> b;       gg[a].push_back(b);       gg[b].push_back(a);     }     int bad;     cin >> bad;     for (int i = 1; i <= bad; i++) {       int a, b;       cin >> a >> b;       gb[a].push_back(b);       gb[b].push_back(a);     }     int ans = 0;     for (int i = 1; i <= n; i++) {       if (was[i]) continue;       cnt = 0;       dfs(i);       bool no = false;       for (int j = 1; j <= n; j++) {         if (been[j]) {           for (auto to : gb[j])             if (been[to]) {               no = true;               break;             }         }         if (no) break;       }       if (!no) ans = max(ans, cnt);       for (int j = 1; j <= n; j++) been[j] = false;     }     cout << ans << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);     int n;     cin >> n;     string s;     cin >> s;     char arr[n + 10];     if (n % 2 == 1) {       int temp = n / 2;       int j = 0;       int k = 1;       for (int i = 0; i < (int)s.size(); i++) {         if (i % 2 == 0) {           arr[temp + j++] = s[i];         } else {           arr[temp - k++] = s[i];         }       }       string ans(arr, arr + n);       cout << ans << endl;     } else {       int temp = n / 2 - 1;       int j = 0;       int k = 1;       for (int i = 0; i < (int)s.size(); i++) {         if (i % 2 == 0) {           arr[temp - j++] = s[i];         } else {           arr[temp + k++] = s[i];         }       }       string ans(arr, arr + n);       cout << ans << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 1e5 + 7;   int n, a[N], pre[N], suf[N];   char ans[N];   struct Fenwick {     int n, c[N];     void init(int _n) {       n = _n;       memset(c, 0, sizeof(c[0]) * (n + 1));     }     void upd(int i, int v) {       while (i <= n) {         c[i] = max(c[i], v);         i += i & -i;       }     }     int qry(int i) {       int r = 0;       while (i > 0) {         r = max(r, c[i]);         i -= i & -i;       }       return r;     }   } fw;   void solve(int f[]) {     fw.init(*max_element(a, a + n));     for (int i = (0); i < (n); ++i) {       f[i] = fw.qry(a[i] - 1) + 1;       fw.upd(a[i], f[i]);     }   }   map<pair<int, int>, int> R;   int main() {     scanf( %d , &n);     for (int i = (0); i < (n); ++i) scanf( %d , a + i);     solve(pre);     reverse(a, a + n);     int L = *max_element(a, a + n);     for (int i = (0); i < (n); ++i) a[i] = L - a[i] + 1;     solve(suf);     reverse(suf, suf + n);     L = *max_element(pre, pre + n);     for (int i = (0); i < (n); ++i)       if (pre[i] + suf[i] - 1 == L) {         ++R[make_pair(pre[i], suf[i])];       } else {         ans[i] =  1 ;       }     for (int i = (0); i < (n); ++i)       if (pre[i] + suf[i] - 1 == L)         ans[i] =  2  + (R[make_pair(pre[i], suf[i])] == 1);     ans[n] =   0 ;     puts(ans);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   struct po {     int x, id, st;   } a[444444];   int n, m, i, j, x, y, t, k, ans[111111], id;   bool cmp(po a, po b) {     if (a.x == b.x) {       return a.st < b.st;     }     return a.x < b.x;   }   int main() {     cin >> n >> m;     for (i = 1; i <= n; i++) {       scanf( %d%d , &x, &y);       a[++t].x = k + 1, a[t].id = i, a[t].st = 0;       k += x * y;     }     for (i = 1; i <= m; i++) {       scanf( %d , &x);       a[++t].x = x, a[t].id = i, a[t].st = 1;     }     sort(a + 1, a + t + 1, &cmp);     for (i = 1; i <= t; i++) {       if (!a[i].st) {         id = a[i].id;       } else {         ans[a[i].id] = id;       }     }     for (i = 1; i <= m; i++) {       printf( %d n , ans[i]);     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   vector<int> fac;   void solve() {     long long int n, s, c = 0;     cin >> n;     vector<long long int> a, o;     for (long long int i = 0; i < n; i++) {       cin >> s;       a.push_back(s);     }     for (long long int i = 0; i < n; i++) {       cin >> s;       o.push_back(s);     }     int a1 = *min_element(a.begin(), a.end());     int o1 = *min_element(o.begin(), o.end());     for (long long int i = 0; i < n; i++) {       long long int e = a[i] - a1;       long long int f = o[i] - o1;       c += max(e, f);     }     cout << c << endl;   }   int main() {     int t;     cin >> t;     while (t--) {       solve();     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     int n;     cin >> n;     vector<vector<int> > adj(n, vector<int>(n, 0));     set<int> s;     int x;     for (int i = 2; i < n + 1; i++) {       cin >> x;       adj[x - 1][i - 1] = 1;       s.insert(x - 1);     }     stack<int> st;     int i = 0;     int j;     int count = 0;     while (i < n) {       if (s.find(i) == s.end()) {         i++;         continue;       }       for (j = 0; j < n; j++) {         if (adj[i][j] == 1 && (s.find(j) == s.end())) {           count++;         }       }       if (count < 3) {         cout <<  No  << endl;         return 0;       }       count = 0;       i++;     }     cout <<  Yes  << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     long long b, d, s, res = 0;     cin >> b;     cin >> d;     cin >> s;     long long val = max(b, max(d, s)) - 1;     if (val > b) res += val - b;     if (val > d) res += val - d;     if (val > s) res += val - s;     cout << res;   }


#include <bits/stdc++.h>   using namespace std;   struct node {     bool flag;     vector<int> op;   };   map<string, node> def;   map<char, int> op;   void del_space(string tmp) {     int x = tmp.find_first_not_of(   );     tmp.erase(0, x);     x = tmp.find_last_not_of(   );     tmp.erase(x + 1, tmp.length());     string s =   ;     for (int i = 0; i < tmp.length(); i++) {       if (tmp[i] !=    ) {         s += tmp[i];       } else {         if (tmp[i + 1] !=    ) s += tmp[i];       }     }     x = s.find( define ) + 7;     int y = x;     while (s[y] !=    ) y++;     string s1, s2;     s1.append(s.begin() + x, s.begin() + y);     for (int i = y + 1; i < s.length(); i++) {       if (s[i] !=    ) s2 += s[i];     }     node tmp_node;     tmp_node.flag = 0;     int cnt = 0;     for (int i = 0; i < s2.length(); i++) {       if (s2[i] ==  ( ) cnt++;       if (s2[i] ==  ) ) cnt--;       if (op[s2[i]] && cnt == 0) {         tmp_node.op.push_back(op[s2[i]]);       }     }     for (int i = 0; i < s2.length(); i++) {       if (s2[i] ==  *  || s2[i] ==  / ) {         string ss1, ss2;         bool bracket = 0;         if (s2[i + 1] ==  ( ) bracket = 1;         for (int j = i + 1; s2[j] !=  )  && !op[s2[j]] && j < s2.length(); j++) {           ss1 += s2[j];         }         map<string, node>::iterator it = def.find(ss1);         if (it != def.end()) {           if (def[ss1].flag == 1) {             tmp_node.flag = 1;           }           if (bracket == 0) {             for (int k = 0; k < def[ss1].op.size(); k++) {               if (def[ss1].op[k] == 1 || def[ss1].op[k] == 2) {                 tmp_node.flag = 1;               }             }           }         }         bracket = 0;         if (s2[i - 1] ==  ) ) bracket = 1;         for (int j = i - 1; s2[j] !=  (  && !op[s2[j]] && j >= 0; j--) {           ss2 += s2[j];         }         reverse(ss2.begin(), ss2.end());         it = def.find(ss2);         if (it != def.end()) {           if (def[ss2].flag == 1) {             tmp_node.flag = 1;           }           if (bracket == 0) {             for (int k = 0; k < def[ss2].op.size(); k++) {               if (def[ss2].op[k] == 1 || def[ss2].op[k] == 2) {                 tmp_node.flag = 1;               }             }           }         }       }       if (s2[i] ==  - ) {         string ss1;         bool bracket = 0;         if (s2[i + 1] ==  ( ) bracket = 1;         for (int j = i + 1; s2[j] !=  )  && !op[s2[j]] && j < s2.length(); j++) {           ss1 += s2[j];         }         map<string, node>::iterator it = def.find(ss1);         if (it != def.end()) {           if (def[ss1].flag == 1) {             tmp_node.flag = 1;           }           if (bracket == 0) {             for (int k = 0; k < def[ss1].op.size(); k++) {               if (def[ss1].op[k] == 1 || def[ss1].op[k] == 2) {                 tmp_node.flag = 1;               }             }           }         }       }       if (s2[i] ==  / ) {         string ss1;         bool bracket = 0;         if (s2[i + 1] ==  ( ) bracket = 1;         for (int j = i + 1; s2[j] !=  )  && !op[s2[j]] && j < s2.length(); j++) {           ss1 += s2[j];         }         map<string, node>::iterator it = def.find(ss1);         if (it != def.end()) {           if (def[ss1].flag == 1 || (bracket == 0 && !def[ss1].op.empty())) {             tmp_node.flag = 1;           }         }       }     }     def[s1] = tmp_node;   }   int main() {     op[ + ] = 1;     op[ - ] = 2;     op[ * ] = 3;     op[ / ] = 4;     int n, flag = 1;     cin >> n;     getchar();     while (n--) {       string tmp;       getline(cin, tmp);       del_space(tmp);     }     string tmp, exp;     getline(cin, tmp);     int x = tmp.find_first_not_of(   );     tmp.erase(0, x);     x = tmp.find_last_not_of(   );     tmp.erase(x + 1, tmp.length());     for (int i = 0; i < tmp.length(); i++) {       if (tmp[i] !=    ) {         exp += tmp[i];       }     }     for (map<string, node>::iterator it = def.begin(); it != def.end(); ++it) {       if (exp.find(it->first) != string::npos && it->second.flag == 1) {         cout <<  Suspicious  << endl;         return 0;       }     }     for (int i = 0; i < exp.length(); i++) {       if (exp[i] ==  *  || exp[i] ==  / ) {         string s1, s2;         bool bracket = 0;         if (exp[i + 1] ==  ( ) bracket = 1;         for (int j = i + 1; exp[j] !=  )  && !op[exp[j]] && j < exp.length();              j++) {           s1 += exp[j];         }         map<string, node>::iterator it = def.find(s1);         if (it != def.end()) {           if (def[s1].flag == 1) {             cout <<  Suspicious  << endl;             return 0;           }           if (bracket == 0) {             for (int k = 0; k < def[s1].op.size(); k++) {               if (def[s1].op[k] == 1 || def[s1].op[k] == 2) {                 cout <<  Suspicious  << endl;                 return 0;               }             }           }         }         bracket = 0;         if (exp[i - 1] ==  ) ) bracket = 1;         for (int j = i - 1; exp[j] !=  (  && !op[exp[j]] && j >= 0; j--) {           s2 += exp[j];         }         reverse(s2.begin(), s2.end());         it = def.find(s2);         if (it != def.end()) {           if (def[s2].flag == 1) {             cout <<  Suspicious  << endl;             return 0;           }           if (bracket == 0) {             for (int k = 0; k < def[s2].op.size(); k++) {               if (def[s2].op[k] == 1 || def[s2].op[k] == 2) {                 cout <<  Suspicious  << endl;                 return 0;               }             }           }         }       }       if (exp[i] ==  - ) {         string s1;         bool bracket = 0;         if (exp[i + 1] ==  ( ) bracket = 1;         for (int j = i + 1; exp[j] !=  )  && !op[exp[j]] && j < exp.length();              j++) {           s1 += exp[j];         }         map<string, node>::iterator it = def.find(s1);         if (it != def.end()) {           if (def[s1].flag == 1) {             cout <<  Suspicious  << endl;             return 0;           }           if (bracket == 0) {             for (int k = 0; k < def[s1].op.size(); k++) {               if (def[s1].op[k] == 1 || def[s1].op[k] == 2) {                 cout <<  Suspicious  << endl;                 return 0;               }             }           }         }       }       if (exp[i] ==  / ) {         string s1;         bool bracket = 0;         if (exp[i + 1] ==  ( ) bracket = 1;         for (int j = i + 1; exp[j] !=  )  && !op[exp[j]] && j < exp.length();              j++) {           s1 += exp[j];         }         map<string, node>::iterator it = def.find(s1);         if (it != def.end()) {           if (def[s1].flag == 1 || (bracket == 0 && !def[s1].op.empty())) {             cout <<  Suspicious  << endl;             return 0;           }         }       }     }     cout <<  OK  << endl;     return 0;   }