#include <bits/stdc++.h>   using namespace std;   vector<int> disp[500][500];   int n, m, a, b;   int pos[4][2] = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}};   class edge {    public:     int e, nxt, cap;     edge(int e1 = -1, int nxt1 = -1, int cap1 = 0) {       e = e1, nxt = nxt1, cap = cap1;     }   };   int S, T;   int dep[1950];   bool vis[1950];   class graph {    public:     edge g[1000000];     int head[1950], tmpcnt;     int cur[1950];     void init() {       memset(head, 255, sizeof(head));       tmpcnt = 0;     }     void addedge(int s, int e, int cap) {       if (cap == 0) return;       g[tmpcnt] = edge(e, head[s], cap), head[s] = tmpcnt++;       g[tmpcnt] = edge(s, head[e], 0), head[e] = tmpcnt++;     }     bool bfs() {       memset(vis, 0, sizeof(vis));       memset(dep, 0, sizeof(dep));       queue<int> q;       q.push(S);       vis[S] = 1;       while (!q.empty()) {         int fr = q.front();         q.pop();         for (int i = head[fr]; i != -1; i = g[i].nxt) {           if (g[i].cap > 0 && !vis[g[i].e]) {             dep[g[i].e] = dep[fr] + 1;             vis[g[i].e] = 1;             q.push(g[i].e);           }         }       }       return vis[T];     }     int dfs(int np, int maxflow) {       int f;       int flow = 0;       if (np == T || !maxflow) return maxflow;       for (int i = cur[np]; i != -1; i = g[i].nxt) {         cur[np] = i;         if (g[i].cap > 0 && dep[g[i].e] == dep[np] + 1 &&             (f = dfs(g[i].e, min(g[i].cap, maxflow))) > 0) {           g[i].cap -= f;           g[i ^ 1].cap += f;           flow += f;           maxflow -= f;           if (!maxflow) break;         }       }       return flow;     }     int dinic() {       int out = 0;       while (bfs()) memcpy(cur, head, sizeof(cur)), out += dfs(S, 0x3f3f3f3f);       return out;     }   } G;   inline int id(int x, int y) { return (x - 1) * m + y; }   int dis[30][30];   char s[30][30];   void init(int x, int y) {     memset(dis, 255, sizeof(dis));     int st = id(x, y);     queue<pair<int, int> > q;     q.push(make_pair(x, y));     dis[x][y] = 0;     disp[st][0].push_back(st);     while (!q.empty()) {       pair<int, int> fr = q.front();       q.pop();       for (int i = 0; i < 4; i++) {         int nx = fr.first + pos[i][0], ny = fr.second + pos[i][1];         if (s[nx][ny] ==  .  && dis[nx][ny] == -1) {           dis[nx][ny] = dis[fr.first][fr.second] + 1;           disp[st][dis[nx][ny]].push_back(id(nx, ny));           q.push(make_pair(nx, ny));         }       }     }   }   class peo {    public:     int x, y, t;   } male[500], female[500];   int main() {     scanf( %d%d%d%d , &n, &m, &a, &b);     for (int i = 1; i <= n; i++) scanf( %s , s[i] + 1);     for (int i = 1; i <= n; i++) {       for (int j = 1; j <= m; j++) {         if (s[i][j] !=  # ) init(i, j);       }     }     if (b == a + 1) {       scanf( %d%d%d , &male[a + 1].x, &male[a + 1].y, &male[a + 1].t);     } else if (a == b + 1) {       scanf( %d%d%d , &female[b + 1].x, &female[b + 1].y, &female[b + 1].t);     } else {       puts( -1 );       return 0;     }     int K = max(a, b);     for (int i = 1; i <= a; i++)       scanf( %d%d%d , &male[i].x, &male[i].y, &male[i].t);     for (int i = 1; i <= b; i++)       scanf( %d%d%d , &female[i].x, &female[i].y, &female[i].t);     long long L = 0, R = 1ll << 40, ans = -1;     S = 2 * (K + n * m) + 1;     T = 2 * (K + n * m) + 2;     while (L <= R) {       long long mid = (L + R) >> 1;       G.init();       for (int i = 1; i <= K; i++) {         G.addedge(S, i + 2 * n * m, 1);         G.addedge(i + K + 2 * n * m, T, 1);         int pm = id(male[i].x, male[i].y);         for (int j = 0; j <= mid / male[i].t && j <= n * m; j++) {           for (auto &x : disp[pm][j]) G.addedge(i + 2 * n * m, x, 1);         }         pm = id(female[i].x, female[i].y);         for (int j = 0; j <= mid / female[i].t && j <= n * m; j++) {           for (auto &x : disp[pm][j]) G.addedge(x + n * m, i + K + 2 * n * m, 1);         }       }       for (int i = 1; i <= n * m; i++) G.addedge(i, i + n * m, 1);       if (G.dinic() != K)         L = mid + 1;       else         ans = mid, R = mid - 1;     }     printf( %lld n , ans);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int INF = (int)1e9;   const int mod = (int)1e9 + 7;   const int MAXN = (int)3e5 + 5;   vector<long long> primes[1000005];   bool vis[1000005];   long long x, p, k;   vector<pair<int, int> > v(1 << 7);   long long calc(long long a) {     int cnt = 1;     int num = 0;     long long tmp = a;     a = 0;     for (int i = 1; i < (1 << primes[p].size()); i++) {       if (cnt * 2 == i) {         cnt *= 2;         num++;       }       v[i] = {v[i - cnt].first * primes[p][num], v[i - cnt].second + 1};       if (v[i].second % 2) {         a += tmp / v[i].first;       } else {         a -= tmp / v[i].first;       }     }     return tmp - a;   }   void solve() {     cin >> x >> p >> k;     k += calc(x);     long long l = x + 1, r = 1e8;     while (l < r) {       long long m = (l + r) / 2;       if (calc(m) >= k) {         r = m;       } else {         l = m + 1;       }     }     cout << l <<   n ;   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(nullptr);     cout.tie(nullptr);     for (long long i = 2; i < 1000005; i++) {       if (!vis[i]) {         for (long long j = i; j < 1000005; j += i) {           primes[j].push_back(i);           vis[j] = 1;         }       }     }     v[0] = {1, 0};     int t = 1;     cin >> t;     while (t--) {       solve();     }   }


#include <bits/stdc++.h>   using namespace std;   const int MAX_N = 1e2 + 10;   const long long mod = 1e9 + 7;   const long long INF = 1e9;   inline long long bpow(long long t, long long n) {     long long ans = 1;     while (n > 0) {       if (n & 1) ans = (ans * t) % mod;       t = (t * t) % mod, n >>= 1;     }     return ans;   }   int a[MAX_N][MAX_N];   int row[MAX_N];   int col[MAX_N];   int main() {     ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);     cout << fixed << setprecision(0);     int n, m;     cin >> n >> m;     for (int i = 1; i <= n; i++)       for (int j = 1; j <= m; j++) cin >> a[i][j];     for (int i = 1; i <= n; i++)       for (int j = 1; j <= m; j++) row[i] += a[i][j], col[j] += a[i][j];     long long ans = 0;     for (int i = 1; i <= n; i++)       ans += (1ll << row[i]) - 1 + (1ll << (m - row[i])) - 1;     for (int j = 1; j <= m; j++)       ans += (1ll << col[j]) - 1 + (1ll << (n - col[j])) - 1;     ans -= n * m;     cout << ans;   }


#include<bits/stdc++.h>      using namespace std;      using ll = long long int;   using vi = vector<int>;   #define f(n) for(int i = 0; i < n; i++)   #define fj(n) for(int j = 0; j < n; j++)   #define fk(n) for(int k = 0; k < n; k++)   #define ci(n) cin >> n;   #define ic(n) int n; cin >> n;   #define lc(n) int n; cin >> n;   #define all(a) a.begin(), a.end()   #define pri(a, n) for(int i = 0; i < n; i++) cout << a[i] <<    ; cout <<   n ;   #define sc(a, n) int a[n] = {0}; for(int i = 0; i < n; i++) cin >> a[i];   #define scl(a, n) ll a[n] = {0}; for(int i = 0; i < n; i++) cin >> a[i];   #define pb push_back   #define eb emplace_back   #define pii pair<int, int>   #define ft first   //#define int long long   const int inf = 1e9, maxn = 2e5, mod = 1e9 + 7;   vector<int> tps;   int used[maxn] = {0}, cols[maxn] = {0};      void DFS(vector<int> g[], int v)   {       used[v] = 1;       for(int u : g[v])       {           if(!used[u]) DFS(g, u);       }       tps.push_back(v);   }      void DFS2(vector<int> g[], int v, int col)   {       cols[v] = col;       for(int u : g[v])       {           if(cols[u] == 0) DFS2(g, u, col);       }   }         signed main()   {       ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);       int n, m, k;       cin >> n >> m >> k;       string t[n], s[m];       int mt[m];       f(n) cin >> t[i];       f(m) cin >> s[i] >> mt[i];       map<string, int> mp;       f(n) mp[t[i]] = i + 1;       vector<int> g[n], ag[n];       f(m) {           vector<int> ls;           string cs = s[i];           fj(1 << k) {               cs = s[i];               for(int id = 0; id < k; id++) {                   if((j >> id) & 1) cs[id] =  _ ;               }               if(mp[cs]) ls.pb(mp[cs] - 1);           }           if(find(all(ls), mt[i] - 1) == ls.end()) {               cout <<  NO ;               return 0;           }           for(int u : ls) {               if(u != mt[i] - 1) {                   g[mt[i] - 1].pb(u);                   ag[u].pb(mt[i] - 1);               }           }       }       int id = 0;       while(id < n)       {           DFS(g, id);           while(id < n && used[id] == 1) id++;       }       reverse(begin(tps), end(tps));       id = 0;       int col = 1;       while(id < n)       {           DFS2(ag, tps[id], col);           col++;           while(id < n && cols[tps[id]] > 0) id++;       }       //pri(cols, n)       if(col == n + 1) {           cout <<  YES n ;           f(n) cout << tps[i] + 1 <<    ;       } else {           cout <<  NO n ;          }   }


#include <bits/stdc++.h>   using namespace std;   const int N = 3e5 + 1;   struct data {     int l, r, sum;   } tree[N << 2], sum_tree[N << 2];   struct node {     int to, next;   } a[N << 1];   int ans1 = 0, ans2 = 0, dp[N], head[N], st[N], ed[N], g[N][25], bx[N], by[N],       bw[N], deep[N];   int n, m, cnt = 0;   vector<int> qx[N];   void add(int x, int y) {     a[++cnt].to = y, a[cnt].next = head[x], head[x] = cnt;     a[++cnt].to = x, a[cnt].next = head[y], head[y] = cnt;   }   void dfs(int u, int fa) {     st[u] = ++cnt;     deep[u] = deep[fa] + 1;     g[u][0] = fa;     for (int i = head[u]; i != -1; i = a[i].next) {       if (a[i].to != fa) dfs(a[i].to, u);     }     ed[u] = cnt;   }   void yyqx() {     for (int i = 1; i < 25; ++i)       for (int j = 1; j <= n; ++j) g[j][i] = g[g[j][i - 1]][i - 1];   }   int GetLCA(int x, int y) {     if (deep[x] > deep[y]) swap(x, y);     for (int i = 24; i >= 0; --i)       if (deep[y] - (1 << i) >= deep[x]) y = g[y][i];     if (x == y) return x;     for (int i = 24; i >= 0; --i)       if (g[x][i] != g[y][i]) x = g[x][i], y = g[y][i];     return g[x][0];   }   void build(int index, int left, int right) {     int mid = (left + right) >> 1;     tree[index].sum = 0, sum_tree[index].sum = 0;     tree[index].l = left, tree[index].r = right;     sum_tree[index].l = left, sum_tree[index].r = right;     if (left == right) return;     build(index * 2, left, mid);     build(index * 2 + 1, mid + 1, right);   }   void change(int index, int left, int right, int d) {     if (left > right) return;     if (tree[index].l >= left && tree[index].r <= right) {       tree[index].sum += d;       return;     }     if (tree[index * 2].r >= left) change(index * 2, left, right, d);     if (tree[index * 2 + 1].l <= right) change(index * 2 + 1, left, right, d);   }   void search(int index, int x) {     ans2 += tree[index].sum;     if (tree[index].l == tree[index].r) return;     if (x <= tree[index * 2].r) search(index * 2, x);     if (x >= tree[index * 2 + 1].l) search(index * 2 + 1, x);   }   void sum_change(int index, int left, int right, int d) {     if (left > right) return;     if (sum_tree[index].l >= left && sum_tree[index].r <= right) {       sum_tree[index].sum += d;       return;     }     if (sum_tree[index * 2].r >= left) sum_change(index * 2, left, right, d);     if (sum_tree[index * 2 + 1].l <= right)       sum_change(index * 2 + 1, left, right, d);   }   void sum_search(int index, int x) {     ans1 += sum_tree[index].sum;     if (sum_tree[index].l == sum_tree[index].r) return;     if (x <= sum_tree[index * 2].r) sum_search(index * 2, x);     if (x >= sum_tree[index * 2 + 1].l) sum_search(index * 2 + 1, x);   }   void work(int u, int fa) {     int sum = 0;     for (int i = head[u]; i != -1; i = a[i].next) {       if (a[i].to != fa) {         work(a[i].to, u);         sum += dp[a[i].to];       }     }     sum_change(1, st[u], ed[u], sum);     dp[u] = sum;     for (int i = 0; i < qx[u].size(); ++i) {       int x = bx[qx[u][i]], y = by[qx[u][i]], w = bw[qx[u][i]];       int val = 0;       ans1 = 0;       sum_search(1, st[x]);       val += ans1;       ans1 = 0;       sum_search(1, st[y]);       val += ans1;       val -= sum;       ans2 = 0;       search(1, st[x]);       val -= ans2;       ans2 = 0;       search(1, st[y]);       val -= ans2;       dp[u] = max(dp[u], val + w);     }     ans1 = 0, ans2 = 0;     change(1, st[u], ed[u], dp[u]);   }   int main() {     int x, y, w;     scanf( %d%d , &n, &m);     memset(head, -1, sizeof(head));     for (int i = 1; i < n; ++i) {       scanf( %d , &x);       add(x, i + 1);     }     cnt = 0;     dfs(1, 0);     yyqx();     for (int i = 1; i <= m; ++i) {       scanf( %d%d%d , &x, &y, &w);       bx[i] = x, by[i] = y, bw[i] = w;       qx[GetLCA(x, y)].push_back(i);     }     build(1, 1, n);     work(1, 0);     printf( %d n , dp[1]);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long n, ans;   string str;   long long solve() {     long long k;     cin >> n >> k;     long long t = 3 * n;     while (t > k) {       t--;       ans++;     }     cout << ans;     return 0;   }   signed main() {     ios::sync_with_stdio(0);     long long t = 1;     while (t--) solve();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     string s, hours, mins;     cin >> s;     int a = 1, b = 1, colon, i, radix = 0;     for (i = 0; i < s.length(); i++) {       if (s[i] ==  : ) {         colon = i;         break;       }     }     for (i = 0; i < colon; i++) {       if (s[i] !=  0 ) {         break;       }     }     hours = s.substr(i, colon - i);     for (i = colon + 1; i < s.length(); i++) {       if (s[i] !=  0 ) {         break;       }     }     mins = s.substr(i);     if (hours.length() <= 1 and mins.length() <= 1) {       hours.length() == 1 and hours[0] -  A  + 10 >= 24 ? cout << 0 : cout << -1;       return 0;     }     for (i = 0; i < s.length(); i++) {       if (i != colon) {         int cur = s[i] >=  0  and s[i] <=  9  ? s[i] -  0  : s[i] -  A  + 10;         radix = max(radix, cur);       }     }     radix++;     if (radix == 1) {       radix = 2;     }     for (i = radix;; i++) {       int HOURS = 0, MINS = 0;       for (int j = 0; j < hours.length(); j++) {         HOURS *= i;         HOURS += hours[j] >=  0  and hours[j] <=  9  ? hours[j] -  0                                                       : hours[j] -  A  + 10;       }       for (int j = 0; j < mins.length(); j++) {         MINS *= i;         MINS += mins[j] >=  0  and mins[j] <=  9  ? mins[j] -  0                                                    : mins[j] -  A  + 10;       }       if (HOURS <= 23 and MINS <= 59) {         cout << i <<    ;       } else {         if (i == radix) {           cout << 0;         }         return 0;       }     }   }


#include <bits/stdc++.h>   using namespace std;   static const int INF = 0x3f3f3f3f;   static const long long INFL = 0x3f3f3f3f3f3f3f3fLL;   template <typename T, typename U>   static void amin(T &x, U y) {     if (y < x) x = y;   }   template <typename T, typename U>   static void amax(T &x, U y) {     if (x < y) x = y;   }   const double PI = 3.141592653589793238462643383279L;   void fft_core(complex<double> *x, int logn, int sign) {     int n = 1 << logn;     for (int i = 1, j = 0; i < n; ++i) {       int h = n >> 1;       while (((j ^= h) & h) == 0) h >>= 1;       if (i < j) swap(x[i], x[j]);     }     for (int m = 2; m <= n; m <<= 1) {       int h = m >> 1;       double theta = sign * 2 * PI / m;       double pr = cos(theta), pi = sin(theta);       for (int i = 0; i < n; i += m) {         double wr = 1, wi = 0;         for (int j = i; j < i + h; ++j) {           int k = j + h;           double lr = x[k].real() * wr - x[k].imag() * wi;           double li = x[k].real() * wi + x[k].imag() * wr;           x[k] = complex<double>(x[j].real() - lr, x[j].imag() - li);           x[j] = complex<double>(x[j].real() + lr, x[j].imag() + li);           double nwr = wr * pr - wi * pi;           double nwi = wr * pi + wi * pr;           wr = nwr, wi = nwi;         }       }     }   }   void fft(int logn, complex<double> a[]) { fft_core(a, logn, +1); }   void inverse_fft(int logn, complex<double> a[]) {     fft_core(a, logn, -1);     complex<double> inv = double(1) / (1 << logn);     for (int(i) = 0; (i) < (int)(1 << logn); ++(i)) a[i] *= inv;   }   using Ring = complex<double>;   void FFT2D(int logh, int H, int logw, int W, vector<vector<Ring>> &A) {     assert(A.size() == 1 << logh);     for (int(i) = 0; (i) < (int)(H); ++(i)) {       assert(A[i].size() == 1 << logw);       fft(logw, A[i].data());     }     vector<Ring> tmp(1 << logh);     for (int(j) = 0; (j) < (int)(1 << logw); ++(j)) {       for (int(i) = 0; (i) < (int)(1 << logh); ++(i)) tmp[i] = A[i][j];       fft(logh, tmp.data());       for (int(i) = 0; (i) < (int)(1 << logh); ++(i)) A[i][j] = tmp[i];     }   }   void FFT2Dinv(int logh, int H, int logw, int W, vector<vector<Ring>> &A) {     vector<Ring> tmp(1 << logh);     for (int(j) = 0; (j) < (int)(1 << logw); ++(j)) {       for (int(i) = 0; (i) < (int)(1 << logh); ++(i)) tmp[i] = A[i][j];       inverse_fft(logh, tmp.data());       for (int(i) = 0; (i) < (int)(1 << logh); ++(i)) A[i][j] = tmp[i];     }     for (int(i) = 0; (i) < (int)(H); ++(i)) inverse_fft(logw, A[i].data());   }   int main() {     int tH;     int tW;     while (~scanf( %d%d , &tH, &tW)) {       vector<string> table(tH);       for (int(i) = 0; (i) < (int)(tH); ++(i)) {         char buf[401];         scanf( %s , buf);         table[i] = buf;       }       int pH;       int pW;       scanf( %d%d , &pH, &pW);       vector<string> pattern(pH);       for (int(i) = 0; (i) < (int)(pH); ++(i)) {         char buf[401];         scanf( %s , buf);         pattern[i] = buf;       }       int H = tH + pH, W = tW + pW;       int logh = 1, logw = 1;       while (1 << logh < H) ++logh;       while (1 << logw < W) ++logw;       vector<vector<Ring>> A(1 << logh, vector<Ring>(1 << logw)), B = A;       for (int(i) = 0; (i) < (int)(tH + pH); ++(i))         for (int(j) = 0; (j) < (int)(tW + pW); ++(j)) {           char c = table[i % tH][j % tW];           double theta = 2 * PI * (c -  a ) / 26;           A[tH + pH - 1 - i][tW + pW - 1 - j] = polar(double(1), theta);         }       for (int(i) = 0; (i) < (int)(pH); ++(i))         for (int(j) = 0; (j) < (int)(pW); ++(j)) {           char c = pattern[i][j];           if (c ==  ? ) continue;           double theta = -2 * PI * (c -  a ) / 26;           B[i][j] = polar(double(1), theta);         }       FFT2D(logh, H, logw, W, A);       FFT2D(logh, H, logw, W, B);       for (int(i) = 0; (i) < (int)(1 << logh); ++(i))         for (int(j) = 0; (j) < (int)(1 << logw); ++(j)) A[i][j] *= B[i][j];       FFT2Dinv(logh, H, logw, W, A);       int qs = 0;       for (int(i) = 0; (i) < (int)(pH); ++(i))         for (int(j) = 0; (j) < (int)(pW); ++(j)) qs += pattern[i][j] ==  ? ;       vector<string> ans(tH, string(tW,  ? ));       for (int(i) = 0; (i) < (int)(tH); ++(i))         for (int(j) = 0; (j) < (int)(tW); ++(j)) {           Ring r = A[tH + pH - 1 - i][tW + pW - 1 - j];           ans[i][j] = r.real() >= (pH * pW - qs) - 1e-6 ?  1  :  0 ;         }       for (int(i) = 0; (i) < (int)(tH); ++(i)) puts(ans[i].c_str());     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long int sett(long long int N, int pos) { return N = N | (1 << pos); }   int reset(int N, int pos) { return N = N & ~(1 << pos); }   bool check(int N, int pos) { return (bool)(N & (1 << pos)); }   void update(long long &x, long long val) {     x = x + val;     if (x > 1000000007) x -= 1000000007;   }   void fast() {     ios_base::sync_with_stdio(0);     cin.tie(NULL), cout.tie(NULL);   }   long long int poww(long long int a, long long int b) {     long long int ans = 1;     while (b--) {       ans *= a;     }     return ans;   }   int moves[4][2] = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}};   char a[300][300];   long long int num[300][300];   long long int ans[300][300];   int n, m;   long long int p, q;   int vis[250][250];   int valid(int i, int j) {     if (i < 0 || i >= n || j < 0 || j >= m) return 0;     if (a[i][j] ==  * ) return 0;     return 1;   }   void dfs(int i, int j, long long int val, int dfs_cnt) {     cout <<  now in      << i <<     << j << endl;     vis[i][j] = dfs_cnt;     ans[i][j] += val;     long long int next_level = val / 2;     if (!next_level) return;     for (int k = 0; k < 4; k++) {       int next_i = i + moves[k][0];       int next_j = j + moves[k][1];       if (valid(next_i, next_j) && vis[next_i][next_j] != dfs_cnt) {         dfs(next_i, next_j, next_level, dfs_cnt);       }     }   }   void bfs(int i, int j, long long int val, int bfs_cnt) {     queue<pair<pair<int, int>, long long int> > q;     q.push(make_pair(make_pair(i, j), val));     vis[i][j] = bfs_cnt;     while (!q.empty()) {       pair<pair<int, int>, long long int> frnt;       frnt = q.front();       q.pop();       int ii = frnt.first.first;       int jj = frnt.first.second;       long long int val = frnt.second;       ans[ii][jj] += val;       long long int next_level = val / 2;       if (next_level == 0) continue;       for (int k = 0; k < 4; k++) {         int next_i = ii + moves[k][0];         int next_j = jj + moves[k][1];         if (valid(next_i, next_j) && vis[next_i][next_j] != bfs_cnt) {           vis[next_i][next_j] = bfs_cnt;           pair<pair<int, int>, long long int> temp;           temp = make_pair(make_pair(next_i, next_j), next_level);           q.push(temp);         }       }     }   }   bool cmp(pair<pair<int, int>, int> a, pair<pair<int, int>, int> b) {     if (a.first.first > b.first.first)       return 1;     else if (a.first.first == b.first.first) {       if (a.first.second < b.first.second) return 1;     }     return 0;   }   int main() {     int n, k, m, a;     scanf( %d%d , &n, &k);     scanf( %d%d , &m, &a);     int vote_time[200][2];     memset(vote_time, 0, sizeof(vote_time));     int cnt = n;     for (int i = 1; i <= a; i++) {       int p;       scanf( %d , &p);       vote_time[p][0]++;       vote_time[p][1] = i;       if (vote_time[p][0] == 1) cnt--;     }     int ans[n + 10];     memset(ans, 0, sizeof(ans));     if (m == 0) {       for (int i = 1; i <= n; i++) printf( 3  );       return 0;     }     int flag0 = 0;     for (int i = 1; i <= n; i++) {       if (vote_time[i][0] == 0 && flag0 == 3) {         ans[i] = 3;         continue;       }       int candidates_vote = vote_time[i][0] + (m - a);       int candidates_time = vote_time[i][1];       if (m - a > 0) candidates_time = m;       if (candidates_vote == 0) {         ans[i] = 3;         flag0 = 3;       }       vector<pair<pair<int, int>, int> > bal;       pair<pair<int, int>, int> temp =           make_pair(make_pair(candidates_vote, candidates_time), i);       bal.push_back(temp);       for (int j = 1; j <= n; j++) {         if (i != j) {           temp = make_pair(make_pair(vote_time[j][0], vote_time[j][1]), j);           bal.push_back(temp);         }       }       sort(bal.begin(), bal.end(), cmp);       int pos = m;       for (int j = 0; j < n; j++) {         if (bal[j].second == i) {           pos = j + 1;           break;         }       }       if (pos > k) {         ans[i] = 3;         if (vote_time[i][0] == 0) {           flag0 = 3;         }       }     }     for (int i = 1; i <= n; i++) {       if (ans[i] > 0) continue;       if (vote_time[i][0] == 0) {         ans[i] == 2;         continue;       }       vector<pair<pair<int, int>, int> > bal;       pair<pair<int, int>, int> temp;       for (int j = 1; j <= n; j++) {         temp = make_pair(make_pair(vote_time[j][0], vote_time[j][1]), j);         bal.push_back(temp);       }       sort(bal.begin(), bal.end(), cmp);       int pos = m;       for (int j = 0; j < n; j++) {         if (bal[j].second == i) {           pos = j + 1;           break;         }       }       int sorte_hobe = k - pos + 1;       if (k == n) {         ans[i] = 1;         continue;       }       int tot_need = 0;       for (int j = pos; j < pos + sorte_hobe; j++) {         int gap = bal[pos - 1].first.first - bal[j].first.first + 1;         tot_need += gap;       }       int baki_ase = m - a;       if (tot_need > baki_ase) {         ans[i] = 1;       }       if (tot_need == 0 && baki_ase == 0) {         ans[i] = 1;       }     }     for (int i = 1; i <= n; i++) {       if (ans[i] == 0)         printf( 2  );       else         printf( %d  , ans[i]);     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   #pragma GCC diagnostic ignored  -Wmissing-declarations    inline int safe_mul(const int x, const int y)       __attribute__((warn_unused_result));   int const mod = 998244353;   inline int safe_mul(const int x, const int y) {     return x * static_cast<long long>(y) % mod;   }   inline void safe_add(int& x, const int y) {     x += y;     if (x >= mod) x -= mod;   }   inline int safe_pow(int x, int y) {     int res = 1;     for (; y > 0; y >>= 1) {       if (y & 1) res = safe_mul(x, res);       x = safe_mul(x, x);     }     return res;   }   const int root_pw = 1 << 21;   int root;   int root_1;   void fft(vector<int>& a, bool invert) {     int n = (int)a.size();     for (int i = 1, j = 0; i < n; ++i) {       int bit = n >> 1;       for (; j >= bit; bit >>= 1) j -= bit;       j += bit;       if (i < j) swap(a[i], a[j]);     }     for (int len = 2; len <= n; len <<= 1) {       int wlen = invert ? root_1 : root;       for (int i = len; i < root_pw; i <<= 1) wlen = safe_mul(wlen, wlen);       for (int i = 0; i < n; i += len) {         int w = 1;         for (int j = 0; j < len / 2; ++j) {           int u = a[i + j], v = safe_mul(a[i + j + len / 2], w);           a[i + j] = u + v < mod ? u + v : u + v - mod;           a[i + j + len / 2] = u - v >= 0 ? u - v : u - v + mod;           w = safe_mul(w, wlen);         }       }     }     if (invert) {       int nrev = safe_pow(n, mod - 2);       for (int i = 0; i < n; ++i) a[i] = safe_mul(a[i], nrev);     }   }   int need;   vector<int> multiply(vector<int> a, vector<int> b) {     size_t n = 1;     while (n < max(a.size(), b.size())) n <<= 1;     n <<= 1;     a.resize(n, 0), b.resize(n, 0);     fft(a, false), fft(b, false);     for (size_t i = 0; i < n; ++i) a[i] = safe_mul(a[i], b[i]);     fft(a, true);     while (!a.empty() && (a.back() == 0 || a.size() > need + 1)) a.pop_back();     return a;   }   int solve(map<int, int> primes) {     auto comp = [](const vector<int>& l, const vector<int>& r) {       return l.size() < r.size();     };     multiset<vector<int>, decltype(comp)> polys(comp);     for (auto pa : primes) polys.insert(vector<int>(pa.second + 1, 1));     while (polys.size() > 1) {       auto vec = multiply(*polys.begin(), *next(polys.begin()));       polys.erase(polys.begin());       polys.erase(polys.begin());       polys.insert(move(vec));     }     return polys.begin()->operator[](need);   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(0), cout.tie(0);     for (int i = 1; i < mod; ++i) {       if (safe_pow(i, root_pw) == 1 && safe_pow(i, root_pw >> 1) != 1) {         root = i;         root_1 = safe_pow(root, mod - 2);         break;       }     }     int n;     cin >> n;     need = n / 2;     map<int, int> cnt;     while (n--) {       int p;       cin >> p;       ++cnt[p];     }     cout << solve(cnt) << endl;   }


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


#include <bits/stdc++.h>   using namespace std;   int mmax, sum, n, t;   int main() {     cin >> n;     sum = 0;     mmax = 0;     for (int i = 0; i < n; i++) {       cin >> t;       sum += t;       mmax = max(t, mmax);     }     cout << mmax - (sum - mmax) + 1;   }


#include <bits/stdc++.h>   using namespace std;   long long int fact[2000005], ifact[2000005];   long long int ppow(long long int b, int p) {     long long int ret = 1;     for (; p; p /= 2) {       if (p & 1) ret = (ret * b) % 1000000007;       b = (b * b) % 1000000007;     }     return ret;   }   void init_fact() {     fact[0] = 1;     for (int i = 1; i < 2000005; i++) fact[i] = (fact[i - 1] * i) % 1000000007;     ifact[2000005 - 1] = ppow(fact[2000005 - 1], 1000000007 - 2);     for (int i = 2000005 - 2; i >= 0; i--)       ifact[i] = (ifact[i + 1] * (i + 1)) % 1000000007;   }   int main() {     init_fact();     int n, m, g;     scanf( %d%d%d , &n, &m, &g);     long long int ans = 0;     if (n == 0) {       if (m == 1 and g == 1)         ans = 1;       else if (m > 1 and g == 0)         ans = 1;     } else if (m == 0) {       if (n == 1 and g == 0)         ans = 1;       else if (n > 1) {         if (n % 2 == 0 and g == 1)           ans = 1;         else if (n % 2 == 1 and g == 0)           ans = 1;       }     } else {       for (int start = 1; start <= n + 1; start++) {         if (start == n + 1 and m == 1) {           if (g == 0 and (n + m) % 2 == 0)             ans++;           else if (g == 1 and (n + m) % 2 == 1)             ans++;           continue;         }         if ((start + g) & 1) {           long long int val = fact[m - 1 + n - start + 1];           val = (val * ifact[m - 1]) % 1000000007;           val = (val * ifact[n - start + 1]) % 1000000007;           ans = (ans + val) % 1000000007;         }       }     }     cout << ans % 1000000007 << endl;   }


#include <bits/stdc++.h>   using namespace std;   const long long mod = 998244353;   long long power(long long a, long long b) {     if (b == 0)       return 1;     else if (b == 1) {       return a;     } else if (b & 1) {       return (a % mod * power((a % mod * a % mod) % mod, b / 2)) % mod;     } else {       return (power((a % mod * a % mod) % mod, b / 2)) % mod;     }   }   void solve() {     int n, m, a, b;     cin >> n >> m;     vector<int> adj[n + 1];     vector<int> deg(n + 1, 0), ans, dis(n + 1, 0), odd, even;     for (long long i = 0; i < m; i++) {       cin >> a >> b;       adj[a].push_back(b);       adj[b].push_back(a);       deg[a]++;       deg[b]++;     }     queue<pair<int, int> > q;     vector<bool> vis(n + 1, false);     q.push({1, 0});     vis[1] = 1;     while (!q.empty()) {       auto it = q.front();       q.pop();       dis[it.first] = it.second;       for (int j : adj[it.first]) {         if (!vis[j]) {           vis[j] = 1;           q.push({j, dis[it.first] + 1});         }       }     }     for (int i = 1; i <= n; i++) {       if (dis[i] & 1) {         odd.push_back(i);       } else {         even.push_back(i);       }     }     if (odd.size() > even.size()) {       cout << even.size() << endl;       for (int i = 0; i < even.size(); i++) {         cout << even[i] <<    ;       }       cout << endl;     } else {       cout << odd.size() << endl;       for (int i = 0; i < odd.size(); i++) {         cout << odd[i] <<    ;       }       cout << endl;     }   }   int main() {     ios::sync_with_stdio(false);     cin.tie(0);     int t;     cin >> t;     while (t--) {       solve();     }   }


#include <bits/stdc++.h>   using namespace std;   const int MAXN = 100000;   int a, b, m, n, C[MAXN], I[MAXN];   queue<int> Q;   vector<int> A, B, E[MAXN];   int main() {     cin >> n >> m;     while (m--) {       cin >> a >> b;       --a;       --b;       E[a].push_back(b);       E[b].push_back(a);       I[a] = I[b] = 1;     }     for (int i = 0; i < n; ++i) {       C[i] = -1;     }     for (int i = 0; i < n; ++i) {       if (C[i] > -1 || !I[i]) {         continue;       }       C[i] = 1;       Q.push(i);       while (!Q.empty()) {         a = Q.front();         Q.pop();         for (auto j : E[a]) {           if (C[j] == -1) {             C[j] = !C[a];             Q.push(j);           } else if (C[j] == C[a]) {             cout << -1 << endl;             return 0;           }         }       }     }     for (int i = 0; i < n; ++i) {       if (C[i] == 0) {         A.push_back(i);       } else if (C[i] == 1) {         B.push_back(i);       }     }     cout << A.size() << endl;     for (auto i : A) {       cout << i + 1 <<    ;     }     cout << endl;     cout << B.size() << endl;     for (auto i : B) {       cout << i + 1 <<    ;     }     cout << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int maxn = 1e7 + 5;   const int maxm = 2e5 + 5;   const int inf = 0x3f3f3f3f;   const long long mod = 1e9 + 7;   const double eps = 1e-9;   const double pi = acos(-1);   int m, n, w, k;   void init() {}   bool judge(int now) {}   long long quickpow(long long a, long long b) {     long long ans = 1;     while (b) {       if (b & 1) ans = ans * a % mod;       a = a * a % mod;       b >>= 1;     }     return ans;   }   char c[1005];   int main() {     int t;     scanf( %d , &t);     while (t--) {       scanf( %d , &n);       scanf( %s , c);       int first = -1, second = -1;       for (int i = 0; i < n; i++)         if (c[i] ==  1 ) {           first = i + 1;           break;         }       for (int i = n - 1; i >= 0; i--)         if (c[i] ==  1 ) {           second = i + 1;           break;         }       if (first == -1)         printf( %d n , n);       else         printf( %d n , 2 * max(second, n - first + 1));     }   }


#include <bits/stdc++.h>   template <class T>   int getbit(T s, int i) {     return (s >> i) & 1;   }   template <class T>   T onbit(T s, int i) {     return s | (T(1) << i);   }   template <class T>   T offbit(T s, int i) {     return s & (~(T(1) << i));   }   template <class T>   int cntbit(T s) {     return __builtin_popcount(s);   }   template <class T>   T gcd(T a, T b) {     T r;     while (b != 0) {       r = a % b;       a = b;       b = r;     }     return a;   }   template <class T>   T lcm(T a, T b) {     return a / gcd(a, b) * b;   }   using namespace std;   const int MAXN = 2e5 + 100;   const int MOD = 1e9 + 7;   const double eps = 1e-12;   const long long INF = 1e16;   inline string toStr(long long x) {     string tmp =   ;     do tmp = char(x % 10 +  0 ) + tmp;     while (x /= 10);     return tmp;   }   inline long long toInt(string s) {     long long res = 0;     for (auto x : s) res = res * 10 + x -  0 ;     return res;   }   int main() {     int n, x, mmax = 0;     cin >> n;     for (int i = 0, _a = (n); i < _a; ++i) cin >> x, mmax = max(mmax, x);     cout << (mmax ^ x);   }


#include <bits/stdc++.h>   using namespace std;   const int N = (int)(1e6);   char S[N + 1], T[N + 1];   vector<int> multiply(vector<int> a, vector<int> b) {     int n = (int)a.size();     vector<int> ret;     for (int i = 0; i < n; i++) ret.push_back(a[b[i]]);     return ret;   }   vector<int> pow(vector<int> a, int b) {     int n = (int)a.size();     vector<int> ret;     if (b == 0) {       for (int i = 0; i < n; i++) ret.push_back(i);       return ret;     }     if (b % 2 == 1) return multiply(pow(a, b - 1), a);     ret = pow(a, b / 2);     return multiply(ret, ret);   }   void solve(int k, int d, int n) {     vector<int> P, vet, ret;     for (int i = 0; i < d; i++)       for (int j = i; j < k; j += d)         if (j > 0) P.push_back(j);     P.push_back(0);     for (int i = 0; i < k; i++) vet.push_back(i);     for (int i = 0, x = 0; i + k <= n; i++) {       ret.push_back(vet[x]);       vet[x] = i + k;       x = P[x];     }     vector<int> aux = pow(P, n - k + 1);     for (int i = 0; i < k - 1; i++) ret.push_back(vet[aux[i]]);     for (int i = 0; i < n; i++) T[i] = S[i];     for (int i = 0; i < n; i++) S[i] = T[ret[i]];     printf( %s n , S);   }   int main() {     scanf( %s , S);     int t;     scanf( %d , &t);     while (t-- > 0) {       int k, d;       scanf( %d %d , &k, &d);       solve(k, d, strlen(S));     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long gcd(long long a, long long b) {     if (a > b) swap(a, b);     if (a == 0) return b;     return gcd(b % a, a);   }   signed main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     long long t, i;     cin >> t;     while (t--) {       long long n;       cin >> n;       long long arr[n];       for (i = 0; i < n; i++) {         cin >> arr[i];       }       long long sum = 0;       for (i = 0; i < n; i++) {         if (i % 2 && arr[i] > 0)           arr[i] *= -1;         else if (i % 2 == 0 && arr[i] < 0)           arr[i] *= -1;       }       for (i = 0; i < n; i++) {         cout << arr[i] <<    ;       }       cout <<   n ;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long cas = 1;   long long dxx[] = {-1, -1, -1, 0, 0, 1, 1, 1};   long long dyy[] = {-1, 0, 1, -1, 1, -1, 0, 1};   long long dx[] = {-1, 0, 0, 1};   long long dy[] = {0, -1, 1, 0};   struct info {     long long mi, pro, cnt;   } tre[100005 * 3];   long long ai[100005], ans = 0;   void update(long long n, long long be, long long en, long long i, long long j,               long long v) {     if (i > en || j < be) return;     if (be >= i && en <= j) {       tre[n].cnt++;       tre[n].pro += v + (be - i);       return;     }     long long m, l, r;     m = (be + en) / 2;     l = n * 2;     r = l + 1;     if (tre[n].pro >= 1) {       tre[l].pro += tre[n].pro;       tre[l].cnt += tre[n].cnt;       tre[r].pro += tre[n].pro + tre[n].cnt * ((m + 1 - be));       tre[r].cnt += tre[n].cnt;       tre[n].pro = 0;       tre[n].cnt = 0;     }     update(l, be, m, i, j, v);     update(r, m + 1, en, i, j, v);   }   long long query(long long n, long long be, long long en, long long i) {     if (ans != -1) return ans;     if (be == i && en == i) {       tre[n].cnt++;       ans = tre[n].pro;       return tre[n].pro;     }     long long m, l, r;     m = (be + en) / 2;     l = n * 2;     r = l + 1;     long long a, b;     if (tre[n].pro >= 1) {       tre[l].pro += tre[n].pro;       tre[l].cnt += tre[n].cnt;       tre[r].pro += tre[n].pro + tre[n].cnt * ((m - be + 1));       tre[r].cnt += tre[n].cnt;       tre[n].pro = 0;       tre[n].cnt = 0;     }     if (be <= i && m >= i) return query(l, be, m, i);     return query(r, m + 1, en, i);   }   int main() {     memset(tre, 0, sizeof(tre));     long long n, m, q, i, k, j, v;     scanf( %lld%lld%lld , &n, &m, &q);     for (k = 0; k < m; k++) {       scanf( %lld%lld%lld , &i, &j, &v);       update(1, 1, n, i, j, v);     }     long long a1 = 0;     for (i = 1; i <= q; i++) {       scanf( %d , &k);       ans = -1;       ans = query(1, 1, n, k);       a1 += ans;     }     printf( %lld n , a1);   }


#include <bits/stdc++.h>   using namespace std;   int *a = new int[2000000];   int m, n;   void add(int x, int l);   void sub(int x, int l);   int query(int x, int y, int x0, int y0, int t);   int main() {     int h, i, j, dir = 1, r, l, o, x, y, len, mid;     cin >> n >> m;     for (i = 0; i < 2000000; i++) a[i] = 0;     for (i = 1; i <= n; i++) add(i, 1);     l = 1;     r = n;     for (i = 0; i < m; i++) {       cin >> o;       if (o == 2) {         cin >> x >> y;         x++;         if (dir == 1) {           x += l - 1;           y += l - 1;           cout << query(1, n, x, y, 1) << endl;         } else if (dir == 2) {           x = r - x + 1;           y = r - y + 1;           cout << query(1, n, y, x, 1) << endl;         }       } else {         cin >> len;         if (len <= (r - l + 1) / 2) {           if (dir == 1) {             mid = l + len;             for (j = 1; j <= len; j++) {               x = mid - j;               y = mid + j - 1;               h = query(1, n, x, x, 1);               add(y, h);             }             l += len;           } else if (dir == 2) {             mid = r - len;             for (j = 1; j <= len; j++) {               x = mid + j;               y = mid - j + 1;               h = query(1, n, x, x, 1);               add(y, h);             }             r -= len;           }         } else {           len = r - l + 1 - len;           if (dir == 2) {             mid = l + len;             for (j = 1; j <= len; j++) {               x = mid - j;               y = mid + j - 1;               h = query(1, n, x, x, 1);               add(y, h);             }             l += len;           } else if (dir == 1) {             mid = r - len;             for (j = 1; j <= len; j++) {               x = mid + j;               y = mid - j + 1;               h = query(1, n, x, x, 1);               add(y, h);             }             r -= len;           }           if (dir == 1)             dir = 2;           else             dir = 1;         }       }     }     delete[] a;     return 0;   }   void add(int x0, int l) {     int x = 1, y = n, mid, t = 1;     while (1) {       a[t] += l;       mid = (x + y) / 2;       if (x == y) break;       if (x0 <= mid) {         y = mid;         t = t * 2;       } else {         x = mid + 1;         t = t * 2 + 1;       }     }   }   void sub(int x0, int l) {     int x = 1, y = n, mid, t = 1;     while (1) {       a[t] -= l;       mid = (x + y) / 2;       if (x == y) break;       if (x0 <= mid) {         y = mid;         t = t * 2;       } else {         x = mid + 1;         t = t * 2 + 1;       }     }   }   int query(int x, int y, int x0, int y0, int t) {     int mid;     if (y0 < x) return 0;     if (y < x0) return 0;     if (x0 <= x && y <= y0) {       return a[t];     }     mid = (x + y) / 2;     return query(x, mid, x0, y0, t * 2) + query(mid + 1, y, x0, y0, t * 2 + 1);   }


#include <bits/stdc++.h>   using namespace std;   int main() {     const int MAX_N = 100001;     int n, x;     vector<long long int> dp(MAX_N, 0);     vector<long long int> c(MAX_N, 0);     cin >> n;     while (n--) {       cin >> x;       c[x]++;     }     dp[0] = 0;     dp[1] = c[1];     for (int i = 2; i < MAX_N; i++) {       dp[i] = max(dp[i - 1], dp[i - 2] + i * c[i]);     }     cout << dp[MAX_N - 1] << endl;   }


#include <bits/stdc++.h>   using namespace std;   int gcd(int a, int b) {     int rem = a % b;     while (rem != 0) {       a = b;       b = rem;       rem = a % b;     }     return b;   }   int main() {     int a, b, c, d;     cin >> a >> b >> c >> d;     int mul1 = 0, mul2 = 0;     int res1 = 0, res2 = 0;     int ans = 0;     mul1 = a * d;     mul2 = c * b;     if (mul1 <= mul2) {       res1 = mul2 - mul1;       res2 = mul2;     } else {       res1 = mul1 - mul2;       res2 = mul1;     }     ans = gcd(res1, res2);     if (gcd((res1 / ans), (res2 / ans)) == 1) {       cout << (res1 / ans) <<  /  << (res2 / ans) << endl;     } else {       return -1;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   signed main(void) {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     long long int t;     cin >> t;     while (t--) {       long long int n, x, y;       cin >> n >> x >> y;       vector<long long int> ar(n);       vector<long long int> arr(n + 2, 0);       vector<long long int> l[n + 2];       vector<long long int> ind(n + 2, 0);       for (long long int(i) = (0); (i) < (n); (i)++) {         cin >> ar[i];         arr[ar[i]] += 1;         l[ar[i]].push_back(i);       }       priority_queue<pair<long long int, long long int>> pq;       long long int k = -1;       for (long long int(i) = (1); (i) < (n + 2); (i)++) {         if (k == -1 && arr[i] == 0) {           k = i;         }         if (arr[i] != 0) {           pq.push({arr[i], i});         }       }       vector<long long int> arr2(n, -1);       for (long long int(i) = (0); (i) < (x); (i)++) {         pair<long long int, long long int> p1;         p1 = pq.top();         pq.pop();         arr2[l[p1.second][ind[p1.second]]] = p1.second;         ind[p1.second] += 1;         p1.first -= 1;         if (p1.first != 0) pq.push(p1);       }       vector<long long int> temp;       for (long long int(i) = (0); (i) < (n); (i)++) {         if (arr2[i] == -1) {           temp.push_back(ar[i]);         }       }       long long int d = (n - x) / 2;       vector<long long int> temp2 = temp;       sort((temp2).begin(), (temp2).end());       map<long long int, long long int> m1;       for (long long int(i) = (0); (i) < (temp2.size()); (i)++) {         m1[i] = l[temp2[i]][ind[temp2[i]]];         ind[temp2[i]] += 1;       }       for (long long int(i) = (0); (i) < (temp2.size()); (i)++) {         long long int j = (i + d) % temp2.size();         arr2[m1[j]] = temp2[i];       }       long long int c = 0;       for (long long int(i) = (0); (i) < (n); (i)++) {         if (arr2[i] == ar[i]) {           x--;           if (x < 0) {             c++;             arr2[i] = k;           }         }       }       if (c < n - y) {         for (long long int(i) = (0); (i) < (n); (i)++) {           if (arr2[i] != k && arr2[i] != ar[i]) {             c++;             arr2[i] = k;           }           if (c < n - y)             continue;           else             break;         }       }       if (c > n - y) {         cout <<  NO n ;       } else {         cout <<  YES n ;         for (long long int(i) = (0); (i) < (n); (i)++) {           cout << arr2[i] <<    ;         }         cout <<   n ;       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int maxn = 310;   const int mod = 1e9 + 7;   long long C[maxn][maxn], Fact[maxn];   void init() {     C[0][0] = 1, Fact[0] = 1;     for (int i = 1; i < maxn; i++) {       C[i][0] = 1, Fact[i] = Fact[i - 1] * i % mod;       for (int j = 1; j <= i; j++)         C[i][j] = (C[i - 1][j - 1] + C[i - 1][j]) % mod;     }   }   long long num[maxn], emp[maxn], dp[maxn][maxn];   int main() {     init();     int n;     scanf( %d , &n);     int tot = 0;     for (int i = 1; i <= n; i++) {       long long tmp;       scanf( %lld , &tmp);       int key = 1;       for (int j = 1; j <= tot; j++) {         long long x = (sqrt(tmp * emp[j]) + 0.5);         if (x * x == tmp * emp[j]) key = 0, num[j]++;       }       if (key) ++tot, num[tot] = 1, emp[tot] = tmp;     }     dp[0][0] = 1;     int m = 0;     for (int i = 1; i <= tot; i++) {       for (int j = 0; j <= n - 1 && j <= m + 1; j++) {         if (dp[i - 1][j] == 0) continue;         for (int k = 1; k <= num[i]; k++) {           long long tmp = dp[i - 1][j] * C[num[i] - 1][k - 1] % mod;           for (int p = 0; p <= k && p <= j + num[i] - k; p++) {             long long temp = tmp * C[j][p] % mod;             dp[i][j + num[i] - k - p] =                 (dp[i][j + num[i] - k - p] + temp * C[m + 1 - j][k - p] % mod) %                 mod;           }         }       }       m += num[i];     }     long long ans = dp[tot][0];     for (int i = 1; i <= tot; i++) ans = ans * Fact[num[i]] % mod;     printf( %lld n , ans);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     long long n, a, b, p, q, maxx, max_cost, minn, min_cost, t, nok, a1, b1;     cin >> n >> a >> b >> p >> q;     if (p > q) {       max_cost = p;       maxx = a;       minn = b;       min_cost = q;     } else {       max_cost = q;       maxx = b;       minn = a;       min_cost = p;     }     a1 = maxx;     b1 = minn;     while (b1 > 0) {       t = b1;       b1 = a1 % b1;       a1 = t;     }     nok = a * b / a1;     cout << ((n / maxx) * max_cost + (n / minn - n / nok) * min_cost);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   template <class A, class B>   A cvt(B x) {     stringstream ss;     ss << x;     A y;     ss >> y;     return y;   }   int n, a, b, t;   string s;   int solve() {     int tb = 0;     vector<int> tm;     for (int k = n; k >= 1; k--) {       tb += a;       if (s[k - 1] !=  h ) tb += b;       tb += 1;       tm.push_back(tb);     }     int best = 0;     int t1 = 0;     for (int k = 1; k <= n; k++) {       if (k != 1) t1 += a;       if (s[k - 1] !=  h ) t1 += b;       t1 += 1;       if (t1 > t) break;       int t2 = t - t1 - (k - 1) * a;       int l = lower_bound(tm.begin(), tm.end(), t2 + 1) - tm.begin();       best = max(best, min(k + l, n));     }     return best;   }   int main() {     cin >> n >> a >> b >> t >> s;     int s1 = solve();     reverse(s.begin() + 1, s.end());     int s2 = solve();     cout << max(s1, s2) << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   char s[105][105];   int sz[105];   vector<int> g[105];   int usd[30];   int ord[30], szo = 0, din[30];   bool dfs(int v) {     usd[v] = 1;     for (int i = 0; i < g[v].size(); ++i) {       int u = g[v][i];       if (usd[u] == 1)         return false;       else if (!usd[u])         if (!dfs(u)) return false;     }     usd[v] = 2;     ord[szo++] = v;     return true;   }   int main() {     int n;     cin >> n;     for (int i = 0; i < n; ++i) {       scanf( %s , s[i]);       sz[i] = strlen(s[i]);     }     for (int i = 1; i < n; ++i) {       int j, mn = min(sz[i], sz[i - 1]);       for (j = 0; j < mn; ++j)         if (s[i][j] != s[i - 1][j]) {           g[s[i - 1][j] -  a ].push_back(s[i][j] -  a );           din[s[i][j] -  a ]++;           break;         }       if (j == mn && sz[i] == mn) {         cout <<  Impossible n ;         return 0;       }     }     for (int i = 0; i < 26; ++i)       if (din[i] == 0)         if (!dfs(i)) {           cout <<  Impossible n ;           return 0;         }     if (szo < 26) {       cout <<  Impossible n ;       return 0;     }     for (int i = szo - 1; i >= 0; --i) printf( %c , ord[i] +  a );     printf(  n );     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n, m;   int a[200], b[200];   int check(int n, int d, int a[]) {     for (int i = 1; i <= n; ++i) {       int flag = 0;       for (int v = 1; v <= 100; ++v)         if (a[v] >= d) {           a[v] -= d;           flag = 1;           break;         }       if (flag == 0) return 0;     }     return 1;   }   int main() {     cin >> n >> m;     for (int i = 0; i < m; ++i) {       cin >> b[i];     }     int ans = 0;     int l = 1, r = m;     while (l <= r) {       memset(a, 0, sizeof(a));       for (int i = 0; i < m; ++i) a[b[i]]++;       int k = (l + r) / 2;       if (check(n, k, a)) {         ans = max(ans, k);         l = k + 1;       } else         r = k - 1;     }     memset(a, 0, sizeof(a));     for (int i = 0; i < m; ++i) a[b[i]]++;     if (check(n, l, a)) ans = max(ans, l);     memset(a, 0, sizeof(a));     for (int i = 0; i < m; ++i) a[b[i]]++;     if (check(n, r, a)) ans = max(ans, r);     cout << ans << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   void getint(int &v) {     char ch, fu = 0;     for (ch =  * ; (ch <  0  || ch >  9 ) && ch !=  - ; ch = getchar())       ;     if (ch ==  - ) fu = 1, ch = getchar();     for (v = 0; ch >=  0  && ch <=  9 ; ch = getchar()) v = v * 10 + ch -  0 ;     if (fu) v = -v;   }   int main() {     int maxmax = 0, max, x, sum = 0, n, m;     long long int sumsum = 0;     getint(n);     if (n == 1) printf( %d n , 0), exit(0);     for (int i = 0; i < n; ++i) {       getint(m);       max = 0;       for (int j = 0; j < m; ++j) {         getint(x);         if (x > max) max = x;       }       sumsum += (long long)max * m;       sum += m;       if (max > maxmax) maxmax = max;     }     printf( %lld n , (long long)sum * (long long)maxmax - sumsum);   }


#include <bits/stdc++.h>   #pragma GCC optimize( O3 )   using namespace std;   long long gcd(long long x, long long y) {     if (x > y) swap(x, y);     if (x == 0) return y;     if (y % x == 0) return x;     return gcd(y % x, x);   }   int main() {     long long n, k, t;     long long m, d, w, g;     long long x, y, z;     long long s, ans;     bool v = true;     ans = 0;     scanf( %lld , &t);     for (long long tt = 0; tt < (long long)(t); tt++) {       scanf( %lld %lld %lld , &m, &d, &w);       if ((m == 1) || (d == 1)) {         printf( 0 n );         continue;       }       g = gcd(d - 1, w);       k = w / g;       x = min(m, d);       y = x % k;       x = x / k;       s = (x + 1) * x;       s /= 2;       ans = s * y;       s = x * (x - 1);       s /= 2;       ans += (s * (k - y));       printf( %lld n , ans);     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long long mod = 1000000007ll;   long long s[1000010];   int h[1000010];   int main() {     int n;     scanf( %d , &n);     for (int i = 1; i <= n; i++) {       scanf( %d , &h[i]);       h[i]--;     }     s[2] = min(h[1], h[2]);     for (int i = 2; i < n; i++)       s[i + 1] = (s[i] * min(h[i - 1], min(h[i], h[i + 1])) % mod +                   min(h[i], h[i + 1])) %                  mod;     long long ans = 0;     for (int i = 1; i <= n; i++) ans = (ans + h[i]) % mod;     for (int i = 2; i <= n; i++)       ans = (ans + min(h[i - 1], h[i]) * s[i] % mod) % mod;     printf( %I64d n , ans);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int a;   char str[100002];   int sum[100002];   int cnt[100002];   int main() {     scanf( %d n , &a);     gets(str);     int len = strlen(str);     sum[0] = (str[0] -  0 );     for (int i = 1; i < len; i++) {       sum[i] = sum[i - 1] + (str[i] -  0 );     }     int id, x, y, mx = 0;     for (int i = 0; i < len; i++) {       for (int j = i; j < len; j++) {         if (i > 0)           id = sum[j] - sum[i - 1];         else           id = sum[j];         cnt[id]++;         mx = max(mx, id);       }     }     long long ans = 0;     for (int i = 0; i < len; i++) {       for (int j = i; j < len; j++) {         if (i > 0)           x = sum[j] - sum[i - 1];         else           x = sum[j];         if (a == 0) {           ans += cnt[0];         }         if (x == 0) continue;         if (a % x != 0) continue;         y = a / x;         if (y > mx) continue;         ans = ans + (long long)(cnt[y]);       }     }     cout << ans << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int INF = (-1u) / 2;   const long long int INF2 = (-1ull) / 2;   int a, b, i, d[1011000], j, k, n, m, timer = 0, l, r, x, y;   int c[1011000], cnt = 0, fl = 0, a2, a3 = -1000000, ans = 0;   pair<int, int> p[100100];   int main() {     ios_base::sync_with_stdio(0);     cin >> n >> a >> b;     for (i = 0; i < n; i++) {       cin >> l;       if (b < a)         p[i] = make_pair(l, i);       else         p[i] = make_pair(l, i * -1);     }     sort(p, p + n);     for (i = n - 1; i >= n - min(a, b); i--) {       if (a > b)         c[abs(p[i].second)] = 2;       else         c[abs(p[i].second)] = 1;     }     for (i = 0; i < n; i++) {       if (a == b) {         if (i < a)           cout <<  1  ;         else           cout <<  2  ;       } else {         if (a > b && c[i] == 0)           c[i] = 1;         else if (a < b && c[i] == 0)           c[i] = 2;         cout << c[i] <<    ;       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   using pii = pair<int, int>;   using ll = long long;   using llu = long long unsigned;   using ld = long double;   const ld EPS = 1e-9;   inline int cmp(ld first, ld second = 0, ld tol = EPS) {     return (first <= second + tol) ? (first + tol < second) ? -1 : 0 : 1;   }   const int MOD = 1;   inline int mod(ll first, int m = MOD) { return (int)(((first % m) + m) % m); }   const int P = 1e5 + 100;   ll n;   bool not_prime[P];   vector<int> primes;   int main() {     not_prime[0] = not_prime[1] = true;     for (int i = 2; i * i < P; i++) {       if (!not_prime[i])         for (int j = i * i; j < P; j += i) not_prime[j] = true;     }     for (int i = 2; i < P; i++)       if (!not_prime[i]) primes.push_back(i);     scanf( %lld , &n);     bool isPrime = true;     int pos;     for (int i = 0; i < ((int)(primes).size()); i++)       if (n % primes[i] == 0) {         isPrime = false;         pos = (primes[i] - 3) / 2;         break;       }     if (isPrime)       printf( 1 n );     else if (n % 2 == 0 or n % 3 == 0)       printf( %lld n , n / 2);     else       printf( %lld n , (n / 2) - pos);     return 0;   }


#include <bits/stdc++.h>   long long strange[100001];   int main() {     for (int i = 1; i <= 100000; i++) {       long long a = i, b = i;       while (b) {         a *= 10;         a += b % 10;         b /= 10;       }       strange[i] = a;     }     long long k, p;     while (scanf( %lld%lld , &k, &p) != EOF) {       long long sum = 0;       for (int i = 1; i <= k; i++) sum += strange[i];       printf( %lld n , sum % p);     }   }


#include <bits/stdc++.h>   using namespace std;   const int INF = 0x3f3f3f3f;   const long long int LINF = 0x3f3f3f3f3f3f3f3fll;   int g;   bool endswith(string a, string b) {     if ((int)((a).size()) < (int)((b).size())) return 0;     for (int i = 0; i < (int)((b).size()); i++)       if (a[(int)((a).size()) - 1 - i] != b[(int)((b).size()) - 1 - i]) return 0;     return 1;   }   bool is_adj(string second) {     if (endswith(second,  lios ))       g = 1;     else if (endswith(second,  liala ))       g = 2;     else       return 0;     return 1;   }   bool is_noun(string second) {     if (endswith(second,  etr ))       g = 1;     else if (endswith(second,  etra ))       g = 2;     else       return 0;     return 1;   }   bool is_verb(string second) {     if (endswith(second,  initis ))       g = 1;     else if (endswith(second,  inites ))       g = 2;     else       return 0;     return 1;   }   int main() {     ios::sync_with_stdio(false);     vector<string> v;     string second;     while (cin >> second) v.push_back(second);     if ((int)((v).size()) == 1) {       if (is_adj(v[0]) or is_noun(v[0]) or is_verb(v[0]))         cout <<  YES  << endl;       else         cout <<  NO  << endl;       return 0;     }     int last = -1;     int n = 0;     bool masc, deu = 1;     for (int i = 0; i < (int)((v).size()); i++) {       if (is_adj(v[i])) {         if (last > 0) deu = 0;         last = 0;       } else if (is_noun(v[i])) {         if (last > 1) deu = 0;         n++;         last = 1;       } else if (is_verb(v[i])) {         last = 2;       } else         deu = 0;       if (!i)         masc = (g == 1);       else if (masc and g == 2)         deu = 0;       else if (!masc and g == 1)         deu = 0;     }     if (n != 1) deu = 0;     int cu;     cout << (deu ?  YES  :  NO ) << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   inline int in() {     int x;     scanf( %d , &x);     return x;   }   const int INF = 1e9 + 10;   const long long LINF = 1000ll * 1000 * 1000 * 1000 * 1000 * 1000 + 100;   const int MN = 3e3 + 10;   int n;   vector<pair<int, int> > edges[MN];   int ans;   int dp1[MN], dp2[MN];   int sz[MN];   void input() {     n = in();     for (int i = 0; i < n - 1; ++i) {       int a, b;       a = in(), b = in();       edges[a].push_back(pair<int, int>(b, 1));       edges[b].push_back(pair<int, int>(a, 0));     }   }   void dfs1(int s, int pr) {     for (int i = 0; i < ((int)(edges[s]).size()); ++i) {       if (edges[s][i].first == pr) continue;       if (edges[s][i].second == 0) ++sz[s];       dfs1(edges[s][i].first, s);       sz[s] += sz[edges[s][i].first];     }   }   void dfs2(int s, int pr) {     for (int i = 0; i < ((int)(edges[s]).size()); ++i) {       if (edges[s][i].first == pr) continue;       dfs2(edges[s][i].first, s);     }     int Mn = sz[s];     for (int i = 0; i < ((int)(edges[s]).size()); ++i) {       if (edges[s][i].first == pr) continue;       if (edges[s][i].second) {         Mn = min(Mn, dp1[edges[s][i].first] + 1 + sz[s] - sz[edges[s][i].first]);       } else {         Mn = min(Mn, dp1[edges[s][i].first] + sz[s] - 1 - sz[edges[s][i].first]);       }     }     dp1[s] = Mn;     Mn = INF;     for (int i = 0; i < ((int)(edges[s]).size()); ++i) {       if (edges[s][i].first == pr) continue;       if (edges[s][i].second) {         Mn = min(Mn, dp2[edges[s][i].first] + sz[s] - sz[edges[s][i].first]);       } else {         Mn = min(Mn, dp1[edges[s][i].first] + sz[s] - sz[edges[s][i].first] - 1);         Mn = min(Mn, dp2[edges[s][i].first] + sz[s] - sz[edges[s][i].first]);       }     }     dp2[s] = Mn;   }   int main() {     input();     int mn = INF;     for (int i = 0; i < n; ++i) {       memset(dp1, 0, sizeof dp1);       memset(dp2, 0, sizeof dp2);       memset(sz, 0, sizeof sz);       dfs1(i, -1);       dfs2(i, -1);       mn = min(mn, dp2[i]);     }     if (n <= 2) mn = 0;     cout << mn <<   n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long long int MOD = 1e9 + 7;   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     long long int t = 1;     while (t--) {       long long int n, i, j;       cin >> n;       long long int a[n];       for (i = 0; i < n; i++) cin >> a[i];       sort(a, a + n);       std::vector<long long int> ans;       long long int last = 64 - __builtin_clzll(a[n - 1]);       for (i = 0; i < last + 1; i++) {         std::vector<long long int> temp;         for (j = 0; j < n; j++) {           bitset<70> b(a[j]);           if (b[i]) temp.push_back(a[j]);         }         bool f = 0;         for (j = i - 1; j > -1; j--) {           bool f1 = 0;           for (auto x : temp) {             bitset<70> b(x);             if (b[j] == 0) f1 = 1;           }           if (!f1) {             f = 1;             break;           }         }         if (f) continue;         if (temp.size()) {           ans = temp;         }       }       cout << ans.size() <<   n ;       for (auto x : ans) cout << x <<    ;     }     cerr <<  Time Taken:  << (float)clock() / CLOCKS_PER_SEC * 1000 <<  ms           <<   n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());   inline long long int rand(long long int x, long long int y) {     ++y;     return (rng() % (y - x)) + x;   }   string to_string(char c) {     string second(1, c);     return second;   }   string to_string(bool b) { return (b ?  true  :  false ); }   template <typename A, typename B>   string to_string(pair<A, B> p) {     return  (  + to_string(p.first) +  ,   + to_string(p.second) +  ) ;   }   template <typename A>   string to_string(A v) {     bool first = true;     string res =  { ;     for (const auto &x : v) {       if (!first) {         res +=  ,  ;       }       first = false;       res += to_string(x);     }     res +=  } ;     return res;   }   void degug_out() { cerr << endl; }   template <typename Head, typename... Tail>   void degug_out(Head H, Tail... T) {     cerr <<     << to_string(H);     degug_out(T...);   }   inline long long int gcd(long long int a, long long int b) {     if (a > b) swap(a, b);     if (a == 0) return b;     return gcd(b % a, a);   }   long long int n, m;   vector<vector<char>> grid;   vector<vector<int>> dist, dist2;   long long int dx[] = {1, 0, -1, 0};   long long int dy[] = {0, 1, 0, -1};   void bfs(long long int x, long long int y, long long int op) {     dist[x][y] = 0;     queue<pair<long long int, long long int>> q;     q.emplace(x, y);     while (q.size()) {       long long int x = q.front().first, y = q.front().second;       q.pop();       if (op == 0) {         for (long long int i = (0); i <= (long long int)(1); ++i) {           long long int nx = x + dx[i];           long long int ny = y + dy[i];           if (nx < 0 || ny < 0 || nx >= n || ny >= m || ~dist[nx][ny] ||               grid[nx][ny] ==  # )             continue;           dist[nx][ny] = dist[x][y] + 1;           q.emplace(nx, ny);         }       } else {         for (long long int i = (2); i <= (long long int)(3); ++i) {           long long int nx = x + dx[i];           long long int ny = y + dy[i];           if (nx < 0 || ny < 0 || nx >= n || ny >= m || ~dist[nx][ny] ||               grid[nx][ny] ==  # )             continue;           dist[nx][ny] = dist[x][y] + 1;           q.emplace(nx, ny);         }       }     }     return;   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     cin >> n >> m;     grid.resize(n, vector<char>(m,  * ));     dist.resize(n, vector<int>(m, -1));     dist2.resize(n, vector<int>(m, -1));     for (long long int i = (0); i <= (long long int)(n - 1); ++i)       for (long long int j = (0); j <= (long long int)(m - 1); ++j)         cin >> grid[i][j];     bfs(0, 0, 0);     swap(dist, dist2);     bfs(n - 1, m - 1, 1);     if (dist2[n - 1][m - 1] == -1) {       cout << 0;       return 0;     }     map<long long int, long long int> co;     for (long long int i = (0); i <= (long long int)(n - 1); ++i)       for (long long int j = (0); j <= (long long int)(m - 1); ++j)         if (~dist[i][j] && ~dist2[i][j] && dist2[i][j] &&             dist2[i][j] != (n - 1 + m - 1))           co[dist2[i][j]]++;     for (long long int i = (0); i <= (long long int)(n - 1); ++i)       for (long long int j = (0); j <= (long long int)(m - 1); ++j)         if (~dist[i][j] && ~dist2[i][j] && dist2[i][j] &&             dist2[i][j] != (n - 1 + m - 1))           if (co[dist2[i][j]] == 1) {             cout << 1;             return 0;           }     cout << 2;   }


#include <bits/stdc++.h>   const long long N = 1000006;   using namespace std;   const long long MOD = 1000000007LL;   template <typename T>   T gcd(T a, T b) {     if (a == 0) return b;     return gcd(b % a, a);   }   template <typename T>   T power(T x, T y, long long m = MOD) {     T ans = 1;     while (y > 0) {       if (y & 1LL) ans = (ans * x) % m;       y >>= 1LL;       x = (x * x) % m;     }     return ans % m;   }   int32_t main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     long long n;     cin >> n;     if (n == 1) {       cout <<  0 ;       return 0;     }     multiset<long long> l, r;     vector<pair<long long, long long> > v;     for (long long(i) = (0); i < (n); i++) {       long long x, y;       cin >> x >> y;       l.insert(x);       r.insert(y);       v.emplace_back(make_pair(x, y));     }     long long ans = 0;     for (long long(i) = (0); i < (n); i++) {       l.erase(l.find(v[i].first));       r.erase(r.find(v[i].second));       long long dist = max(0LL, *r.begin() - *l.rbegin());       ans = max(ans, dist);       l.insert(v[i].first);       r.insert(v[i].second);     }     cout << ans;   }


#include <bits/stdc++.h>   using namespace std;   int a[100001], n;   inline long long sum(int t) {     long long s = 0;     for (int i = 1; i <= n; ++i) s += min(a[i], t);     return s;   }   int main() {     int i, j, l, r, m, t = 1;     int nr = 0;     long long k, s = 0, smin;     scanf( %d %I64d , &n, &k);     for (i = 1; i <= n; ++i) {       scanf( %d , &a[i]);       s += a[i];     }     if (s < k)       printf( -1 );     else if (s == k)       printf(  );     else {       l = 0;       r = 1000000000;       while (l + 1 < r) {         m = (l + r) >> 1;         if (sum(m) <= k)           l = m;         else           r = m;       }       k -= sum(l);       if (s == k)         printf(  );       else {         for (i = 1; i <= n; ++i) {           if (k == 0) break;           if (a[i] > l) {             --k;             --a[i];           }         }         for (j = i; j <= n; ++j)           if (a[j] > l) printf( %d  , j);         for (j = 1; j < i; ++j)           if (a[j] > l) printf( %d  , j);       }     }     printf(  n );     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int MAXN = 100000;   int n, m, k, start = MAXN + 1;   int type[MAXN + 3], a[MAXN + 3], counter[MAXN + 3], stc[MAXN + 3];   bool check[MAXN + 3];   int main() {     scanf( %d , &n);     for (int i = 1; i <= n; i++) scanf( %d , &type[i]);     for (int i = 1; i <= n; i++) scanf( %d , &a[i]);     for (int i = 1; i <= n; i++) counter[a[i]]++;     for (int i = 1; i <= n; i++)       if (counter[i] == 1) check[i] = true;     int cnt = 0;     for (int i = n; i >= 1; i--) {       if (type[i] == 1) {         cnt = 1;         int v = a[i];         while (v != 0 && check[v]) {           cnt++;           v = a[v];         }         if (cnt > k) k = cnt, start = i;       }     }     printf( %d n , k);     for (int i = 0, v = start; i < k; i++, v = a[v]) stc[i] = v;     for (int i = k - 1; i >= 0; i--) printf( %d  , stc[i]);     puts(  );     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long l, r, ans;   vector<long long> lucky;   void lu(int f, int s) {     string x =   ;     for (int i = 0; i < f; i++) x +=  4 ;     for (int i = 0; i < s; i++) x +=  7 ;     do lucky.push_back(atoll(x.c_str()));     while (next_permutation(x.begin(), x.end()));   }   int main() {     cin >> l >> r;     for (int i = 0; i <= 10; i++)       for (int j = 0; j <= 10; j++)         if (i + j <= 10) lu(i, j);     sort(lucky.begin(), lucky.end());     int j, k;     j = k = 0;     while (lucky[j++] < l)       ;     j--;     while (lucky[k++] < r)       ;     k--;     ans += lucky[j] * (min(r - l, lucky[j] - l) + 1);     if (lucky[j] == lucky[k]) return cout << ans << endl, 0;     ans += lucky[k] * (r - lucky[k - 1]);     for (int i = j + 1; i < k; i++) ans += (lucky[i] - lucky[i - 1]) * lucky[i];     cout << ans << endl;   }


#include <bits/stdc++.h>   using namespace std;   int dx[] = {0, 0, 1, -1, 1, 1, -1, -1};   int dy[] = {1, -1, 0, 0, 1, -1, 1, -1};   int a[33];   string s;   int main() {     for (int i = 0; i < 26; ++i) scanf( %d , &a[i]);     cin >> s;     int len = s.length();     vector<long long> v(len + 1);     for (int i = 0; i < len; ++i) {       v[i + 1] = a[s[i] -  a ] + v[i];     }     map<pair<int, long long>, int> m;     long long res = 0;     for (int i = 0; i < len; i++) {       if (m[make_pair(s[i] -  a , v[i + 1])]) {         res += m[make_pair(s[i] -  a , v[i + 1])];       }       m[make_pair(s[i] -  a , v[i + 1] + a[s[i] -  a ])]++;     }     cout << res <<   n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n;     scanf( %d , &n);     int arr[101][101];     int row[101], col[101];     for (int i = 0; i <= n - 1; i++) {       row[i] = col[i] = 0;     }     int temp;     for (int i = 0; i <= n - 1; i++) {       for (int j = 0; j <= n - 1; j++) {         scanf( %d , &temp);         arr[i][j] = temp;         row[i] += temp;         col[j] += temp;       }     }     int ans = 0;     for (int i = 0; i <= n - 1; i++) {       for (int j = 0; j <= n - 1; j++) {         if (col[j] > row[i]) ans++;       }     }     printf( %d n , ans);   }


#include <bits/stdc++.h>   using namespace std;   int m, n, k, t;   int a[100001];   int main() {     scanf( %d %d , &n, &k);     for (int i = 0; i < n; ++i) {       scanf( %d , &t);       a[i % k] += t;     }     m = 0;     for (int i = 1; i < k; ++i)       if (a[i] < a[m]) m = i;     printf( %d n , m + 1);   }


#include <bits/stdc++.h>   using namespace std;   vector<int> vec[1000006], V;   struct Node {     int x, y, w;     bool operator<(const Node& a) const { return y < a.y; }   } node[500006];   int n;   struct Info {     long long len, mx, w;     int p;     Info() {}     Info(long long _len, long long _mx, long long _w, int _p) {       len = _len;       mx = _mx;       w = _w;       p = _p;     }     Info operator+(const Info& a) {       Info ret;       ret.len = len + a.len;       ret.w = w + a.w;       if (a.mx >= a.w + mx - a.len) {         ret.mx = a.mx;         ret.p = a.p;       } else {         ret.mx = a.w + mx - a.len;         ret.p = p;       }       return ret;     }     void pr() { printf( %lld %lld %lld %d n , len, mx, w, p); }   } info[1000006 << 2];   void build(int p, int l, int r) {     if (l == r) {       int len = 0;       if (l > 1) len = V[l - 1] - V[l - 2];       info[p] = Info(len, 0, 0, l);       return;     }     int mid = (l + r) >> 1;     build(p << 1, l, mid);     build(p << 1 | 1, mid + 1, r);     info[p] = info[p << 1] + info[p << 1 | 1];   }   void update(int p, int l, int r, int aim, long long w) {     if (l == r) {       info[p].w += w;       info[p].mx += w;       return;     }     int mid = (l + r) >> 1;     if (aim <= mid)       update(p << 1, l, mid, aim, w);     else       update(p << 1 | 1, mid + 1, r, aim, w);     info[p] = info[p << 1] + info[p << 1 | 1];   }   Info query(int p, int l, int r, int L, int R) {     if (L <= l && r <= R) {       return info[p];     }     int mid = (l + r) >> 1;     if (L <= mid && R > mid)       return query(p << 1, l, mid, L, R) + query(p << 1 | 1, mid + 1, r, L, R);     else if (L <= mid)       return query(p << 1, l, mid, L, R);     else if (R > mid)       return query(p << 1 | 1, mid + 1, r, L, R);   }   int main() {     scanf( %d , &n);     for (int i = 1; i <= n; i++) {       scanf( %d%d%d , &node[i].x, &node[i].y, &node[i].w);       if (node[i].x > node[i].y) swap(node[i].x, node[i].y);       V.push_back(node[i].x);       V.push_back(node[i].y);     }     sort(node + 1, node + n + 1);     sort(V.begin(), V.end());     V.resize(unique(V.begin(), V.end()) - V.begin());     for (int i = 1; i <= n; i++) {       node[i].x = lower_bound(V.begin(), V.end(), node[i].x) - V.begin();       node[i].y = lower_bound(V.begin(), V.end(), node[i].y) - V.begin();       vec[node[i].y].push_back(i);     }     int End = V.size();     build(1, 1, End);     long long ans = -1e15;     int ansl, ansr;     for (int i = 0; i < End; i++) {       int siz = (int)vec[i].size();       for (int j = 0; j < siz; j++)         update(1, 1, End, node[vec[i][j]].x + 1, node[vec[i][j]].w);       Info tmp = query(1, 1, End, 1, i + 1);       if (tmp.mx > ans) {         ans = tmp.mx;         ansl = V[tmp.p - 1];         ansr = V[i];       }     }     if (ans < 0) ans = 0, ansl = ansr = 2e9;     printf( %lld n%d %d %d %d , ans, ansl, ansl, ansr, ansr);     return 0;   }


#include <bits/stdc++.h>   int main() {     int t, arr[110], i, l, s, m, n, j;     scanf( %d , &t);     l = -1;     s = 1001;     for (i = 0; i < t; i++) scanf( %d , &arr[i]);     for (i = 0; i < t; i++) {       for (j = i + 1; j < t; j++) {         if (arr[j] < arr[i]) {           s = arr[j];           arr[j] = arr[i];           arr[i] = s;         }       }     }     printf( %d  , arr[t - 1]);     for (i = 1; i < t - 1; i++) {       printf( %d  , arr[i]);     }     printf( %d n , arr[0]);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n, m, a[3010][6010], fa[18010000];   int dx[9] = {-1, -1, -1, 0, 0, 1, 1, 1}, dy[9] = {-1, 0, 1, -1, 1, -1, 0, 1};   inline int read() {     int x = 0, f = 0;     char ch = getchar();     while (!isdigit(ch)) f = ch ==  - , ch = getchar();     while (isdigit(ch)) x = (x << 3) + (x << 1) + ch - 48, ch = getchar();     return f ? -x : x;   }   inline int find(int x) { return fa[x] == x ? x : fa[x] = find(fa[x]); }   inline int calc(int x, int y) { return (x - 1) * m * 2 + y; }   int main() {     int ans = 0;     n = read(), m = read();     int Q = read();     for (int i = 1; i <= n; i++)       for (int j = 1; j <= 2 * m; j++) fa[calc(i, j)] = calc(i, j);     if (m == 1) {       puts( 0 );       return 0;     }     while (Q--) {       int x = read(), y = read();       int flag = 0;       for (int i = 0; i < 8; i++) {         int xx = x + dx[i], yy = y + dy[i];         if (yy == 0) yy = 2 * m;         if (xx < 1 || xx > n || !a[xx][yy]) continue;         for (int j = 0; j < 8; j++) {           int xxx = x + dx[j], yyy = y + m + dy[j];           if (yyy > 2 * m) yyy = 1;           if (xxx < 1 || xxx > n || !a[xxx][yyy]) continue;           if (find(calc(xx, yy)) == find(calc(xxx, yyy))) {             flag = 1;             break;           }         }       }       if (flag) continue;       a[x][y] = a[x][y + m] = 1;       ans++;       int fx1 = find(calc(x, y)), fx2 = find(calc(x, y + m));       for (int i = 0; i < 8; i++) {         int xx = x + dx[i], yy = y + dy[i];         if (yy > m) {           if (a[xx][yy]) fa[find(calc(xx, yy))] = fx1;           if (a[xx][1]) fa[find(calc(xx, 1))] = fx2;         } else if (yy < 1) {           if (a[xx][yy + m]) fa[find(calc(xx, yy + m))] = fx2;           if (a[xx][2 * m]) fa[find(calc(xx, 2 * m))] = fx1;         } else {           if (a[xx][yy]) fa[find(calc(xx, yy))] = fx1;           if (a[xx][yy + m]) fa[find(calc(xx, yy + m))] = fx2;         }       }     }     printf( %d n , ans);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n;     cin >> n;     int a[n], i;     for (i = 0; i < n; i++) {       cin >> a[i];     }     if (n == 1) {       cout << 1;       return 0;     }     int lmarr[n + 1], rmarr[n + 1];     lmarr[0] = INT_MIN;     rmarr[n] = INT_MAX;     for (i = 0; i < n; i++) {       lmarr[i + 1] = max(a[i], lmarr[i]);     }     for (i = n - 1; i >= 0; i--) {       rmarr[i] = min(rmarr[i + 1], a[i]);     }     int count = 1;     for (i = 1; i < n; i++) {       if (lmarr[i] <= rmarr[i]) {         count++;       }     }     cout << count;   }


#include <bits/stdc++.h>   #pragma GCC optimize( O3 )   using namespace std;   struct custom_hash {     static uint64_t splitmix64(uint64_t x) {       x += 0x9e3779b97f4a7c15;       x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;       x = (x ^ (x >> 27)) * 0x94d049bb133111eb;       return x ^ (x >> 31);     }     size_t operator()(uint64_t x) const {       static const uint64_t FIXED_RANDOM =           chrono::steady_clock::now().time_since_epoch().count();       return splitmix64(x + FIXED_RANDOM);     }   };   template <typename T, size_t N>   int SIZE(const T (&t)[N]) {     return N;   }   template <typename T>   int SIZE(const T &t) {     return t.size();   }   string to_string(const string s, int x1 = 0, int x2 = 1e9) {     return     + ((x1 < s.size()) ? s.substr(x1, x2 - x1 + 1) :   ) +    ;   }   string to_string(const char *s) { return to_string((string)s); }   string to_string(const bool b) { return (b ?  true  :  false ); }   string to_string(const char c) { return string({c}); }   template <size_t N>   string to_string(const bitset<N> &b, int x1 = 0, int x2 = 1e9) {     string t =   ;     for (int __iii__ = min(x1, SIZE(b)), __jjj__ = min(x2, SIZE(b) - 1);          __iii__ <= __jjj__; ++__iii__) {       t += b[__iii__] +  0 ;     }     return     + t +    ;   }   template <typename A, typename... C>   string to_string(const A(&v), int x1 = 0, int x2 = 1e9, C... coords);   int l_v_l_v_l = 0, t_a_b_s = 0;   template <typename A, typename B>   string to_string(const pair<A, B> &p) {     l_v_l_v_l++;     string res =  (  + to_string(p.first) +  ,   + to_string(p.second) +  ) ;     l_v_l_v_l--;     return res;   }   template <typename A, typename... C>   string to_string(const A(&v), int x1, int x2, C... coords) {     int rnk = rank<A>::value;     string tab(t_a_b_s,    );     string res =   ;     bool first = true;     if (l_v_l_v_l == 0) res +=   n ;     res += tab +  [ ;     x1 = min(x1, SIZE(v)), x2 = min(x2, SIZE(v));     auto l = begin(v);     advance(l, x1);     auto r = l;     advance(r, (x2 - x1) + (x2 < SIZE(v)));     for (auto e = l; e != r; e = next(e)) {       if (!first) {         res +=  ,  ;       }       first = false;       l_v_l_v_l++;       if (e != l) {         if (rnk > 1) {           res +=   n ;           t_a_b_s = l_v_l_v_l;         };       } else {         t_a_b_s = 0;       }       res += to_string(*e, coords...);       l_v_l_v_l--;     }     res +=  ] ;     if (l_v_l_v_l == 0) res +=   n ;     return res;   }   void dbgm() { ; }   template <typename Heads, typename... Tails>   void dbgm(Heads H, Tails... T) {     cout << to_string(H) <<   |  ;     dbgm(T...);   }   const long long MOD = (int)1e9 + 7;   const long long INF = 2e18 + 5;   struct hashLL {     size_t operator()(long long x) const {       x = (x ^ (x >> 30)) * UINT64_C(0xbf58476d1ce4e5b9);       x = (x ^ (x >> 27)) * UINT64_C(0x94d049bb133111eb);       x = x ^ (x >> 31);       return x;     }   };   long long inv(long long a, long long b) {     return 1 < a ? b - inv(b % a, a) * b / a : 1;   }   long long gcd(long long a, long long b, long long &x, long long &y) {     if (a == 0) {       x = 0;       y = 1;       return b;     }     long long x1, y1;     long long d = gcd(b % a, a, x1, y1);     x = y1 - (b / a) * x1;     y = x1;     return d;   }   long long distsq(long long x1, long long y1, long long x2, long long y2) {     return (x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1);   }   const int N = 100001;   long long n, m, k, x, y, w;   unordered_map<int, unordered_map<int, int>> g;   string s;   pair<long long, long long> solve(long long curr, long long par) {     long long returnwt = 0;     long long endwt = 0;     vector<long long> v;     for (auto p : g[curr]) {       long long child = p.first;       long long wt = p.second;       if (child == par) continue;       returnwt += wt;       pair<long long, long long> x = solve(child, curr);       returnwt += x.first;       returnwt += wt;       if (-wt - wt - x.first + x.second + wt < endwt) {         endwt = -wt - wt - x.first + x.second + wt;       }     }     return {returnwt, returnwt + endwt};   }   int main() {     ios::sync_with_stdio(false);     cin.tie(0);     cin >> n;     for (int i = 0; i < (n - 1); i++) {       cin >> x >> y >> w;       g[x][y] = w;       g[y][x] = w;     }     long long res = solve(1, 0).second;     cout << res <<   n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n, l, r;   vector<pair<int, int> > G[100005];   int seq[100005], cnts, siz[100005], rch[100005], num[100005];   bool vis[100005];   int rt, Min;   void dfs(int now, int ff, int tsz) {     int Max = 0;     siz[now] = 1;     rch[now] = 0;     for (int i = 0, v, sz = G[now].size(); i < sz; i++)       if ((v = G[now][i].first) != ff && !vis[v]) {         dfs(v, now, tsz);         rch[now] = max(rch[now], rch[v] + 1);         siz[now] += siz[v];         Max = max(Max, siz[v]);       }     Max = max(Max, tsz - siz[now]);     if (Max < Min) Min = Max, rt = now;   }   int Gert(int now, int tsz) {     Min = 0x3f3f3f3f;     dfs(now, -1, tsz);     return rt;   }   bool cmp(const pair<int, int> &a, const pair<int, int> &b) {     return rch[a.first] < rch[b.first];   }   void setsiz(int now, int ff) {     siz[now] = 1, rch[now] = 0;     for (int i = 0, sz = G[now].size(), v; i < sz; i++)       if ((v = G[now][i].first) != ff && !vis[v]) {         setsiz(v, now);         siz[now] += siz[v];         rch[now] = max(rch[now], rch[v] + 1);       }   }   void Solve(int now) {     seq[++cnts] = now;     setsiz(now, -1);     vis[now] = 1;     rch[now] = 0;     sort(G[now].begin(), G[now].end(), cmp);     for (int v, i = 0, sz = G[now].size(); i < sz; i++)       if (!vis[(v = G[now][i].first)]) Solve(Gert(v, siz[v]));   }   int f[100005], g[100005], floc[100005], gloc[100005], ansx, ansy;   int Maxdep;   void ser(int now, int ff, int dist, int cost, int Lim) {     Maxdep = max(Maxdep, dist);     if (cost > g[dist]) g[dist] = cost, gloc[dist] = now;     for (int i = 0, sz = G[now].size(), v; i < sz; i++)       if ((v = G[now][i].first) != ff && !vis[v])         ser(v, now, dist + 1, cost + (G[now][i].second >= Lim ? 1 : -1), Lim);   }   int Q[100005], L, R;   bool check(int Lim) {     memset(vis, 0, sizeof vis);     memset(f, -0x3f, sizeof f);     memset(g, -0x3f, sizeof g);     f[0] = 0;     int premaxdep = 0;     for (int u, id = 1; id <= n; id++) {       for (int i = 1; i <= premaxdep; i++) f[i] = -0x3f3f3f3f, g[i] = -0x3f3f3f3f;       premaxdep = 0;       vis[u = seq[id]] = 1;       floc[0] = u;       for (int predep = 0, i = 0, v, sz = G[u].size(); i < sz; i++)         if (!vis[v = G[u][i].first]) {           for (int j = 1; j <= predep; j++) g[j] = -0x3f3f3f3f;           Maxdep = 0;           ser(v, -1, 1, G[u][i].second >= Lim ? 1 : -1, Lim);           L = R = 0;           for (int j = 0, k = min(r, Maxdep); j <= predep; j++) {             for (; j + k >= l && k >= 0; k--) {               for (; L < R && g[k] >= g[Q[R - 1]]; R--)                 ;               Q[R++] = k;             }             for (; L < R && Q[L] + j > r; L++)               ;             if (L < R && f[j] + g[Q[L]] >= 0) {               ansx = floc[j];               ansy = gloc[Q[L]];               return 1;             }           }           for (int j = 1; j <= Maxdep; j++)             if (f[j] < g[j]) f[j] = g[j], floc[j] = gloc[j];           predep = Maxdep;           premaxdep = max(premaxdep, Maxdep);         }     }     return 0;   }   int main() {     int u, v, w;     scanf( %d%d%d , &n, &l, &r);     for (int i = 1; i < n; i++) {       scanf( %d%d%d , &u, &v, &w);       G[u].push_back(make_pair(v, w));       G[v].push_back(make_pair(u, w));     }     Solve(Gert(1, n));     int L = 0, R = 1000000000, Mid;     while (L < R) {       Mid = (L + R + 1) >> 1;       if (check(Mid))         L = Mid;       else         R = Mid - 1;     }     check(L);     printf( %d %d n , ansx, ansy);   }


#include <bits/stdc++.h>   template <typename T>   inline void read(T &x) {     x = 0;     char c = getchar();     while (!isdigit(c)) c = getchar();     while (isdigit(c)) x = x * 10 + (c ^ 48), c = getchar();   }   using namespace std;   int n, m;   struct Edge {     int u, v, w, id;     Edge() {}     Edge(int jzp, int zzz, int lsr, int chr) {       u = jzp, v = zzz, w = lsr, id = chr;     }   } E[301000], Ev[301000];   bool cmp(const Edge &a, const Edge &b) { return a.w < b.w; }   int stkx[301000], stky[301000], stop, siz[301000], oddcnt, fath[301000];   int find(int cur) { return fath[cur] == cur ? cur : find(fath[cur]); }   inline void merge(int x, int y) {     x = find(x), y = find(y);     if (x == y) return;     if (siz[x] > siz[y]) swap(x, y);     stkx[++stop] = x, stky[stop] = y;     if (siz[x] & 1) --oddcnt;     if (siz[y] & 1) --oddcnt;     siz[y] += siz[x];     fath[x] = y;     if (siz[y] & 1) ++oddcnt;   }   inline void cancl(int x, int y) {     fath[x] = x;     if (siz[y] & 1) --oddcnt;     siz[y] -= siz[x];     if (siz[x] & 1) ++oddcnt;     if (siz[y] & 1) ++oddcnt;   }   int st[301000], ed[301000];   int ans[301000];   int h[301000], ltot;   void sol(int l, int r, int d, int u) {     if (l > r) return;     int memo = stop, mid = (l + r) >> 1;     for (int i = l; i <= mid; ++i) {       if (E[i].w <= d) merge(E[i].u, E[i].v);     }     int memo_ = stop;     if (!oddcnt) {       ans[mid] = d;     } else {       for (int i = d + 1; i <= u; ++i) {         for (int j = st[i]; j <= ed[i]; ++j) {           if (Ev[j].id <= mid) merge(Ev[j].u, Ev[j].v);         }         if (!oddcnt) {           ans[mid] = i;           break;         }       }     }     if (!ans[mid]) {       while (stop != memo_) cancl(stkx[stop], stky[stop]), --stop;       sol(mid + 1, r, d, u);       while (stop != memo) cancl(stkx[stop], stky[stop]), --stop;       return;     } else {       while (stop != memo_) cancl(stkx[stop], stky[stop]), --stop;       sol(mid + 1, r, d, ans[mid]);       while (stop != memo) cancl(stkx[stop], stky[stop]), --stop;       for (int i = d + 1; i <= ans[mid]; ++i) {         for (int j = st[i]; j <= ed[i]; ++j) {           if (Ev[j].id < l) merge(Ev[j].u, Ev[j].v);         }       }       sol(l, mid - 1, ans[mid], u);       while (stop != memo) cancl(stkx[stop], stky[stop]), --stop;       return;     }   }   int main() {     read(n), read(m);     for (int i = 1; i <= n; ++i) siz[i] = 1, fath[i] = i;     oddcnt = n;     for (int i = 1; i <= m; ++i) {       int u, v, w;       read(u), read(v), read(w);       h[i] = w;       Ev[i] = Edge(u, v, w, i);     }     sort(h + 1, h + 1 + m);     ltot = unique(h + 1, h + 1 + m) - h - 1;     for (int i = 1; i <= m; ++i)       Ev[i].w = lower_bound(h + 1, h + 1 + ltot, Ev[i].w) - h;     memcpy(E, Ev, sizeof(Ev));     sort(Ev + 1, Ev + 1 + m, cmp);     for (int i = 1; i <= m; ++i)       if (Ev[i].w != Ev[i - 1].w) ed[Ev[i - 1].w] = i - 1, st[Ev[i].w] = i;     ed[Ev[m].w] = m;     sol(1, m, 1, ltot);     h[0] = -1;     for (int i = 1; i <= m; ++i) printf( %d n , h[ans[i]]);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long modpow(long long a, long long b,                    long long mod = (long long)(1e9 + 7)) {     if (!b) return 1;     a %= mod;     return modpow(a * a % mod, b / 2, mod) * (b & 1 ? a : 1) % mod;   }   mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());   const int DEBUG = 1;   const int mxn = 2e5 + 7;   int n, k;   int dsu[mxn], sz[mxn], mini[mxn], maxi[mxn];   bool ac[mxn];   string ans;   int a[mxn], b[mxn];   void init() {     for (int i = 0; i < mxn; i++)       ac[i] = 0, dsu[i] = i, sz[i] = 1, mini[i] = i, maxi[i] = i;   }   int root(int i) { return dsu[i] = dsu[i] == i ? i : root(dsu[i]); }   void merge(int i, int j) {     if (!(0 <= i && i <= n - 1) || !(0 <= j && j <= n - 1)) return;     i = root(i);     j = root(j);     if (i == j) return;     if (sz[i] < sz[j]) swap(i, j);     dsu[j] = i;     sz[i] += sz[j];     (mini[i]) = min((mini[i]), (mini[j]));     (maxi[i]) = max((maxi[i]), (maxi[j]));   }   void activate(int i) {     ac[i] = 1;     if (i && ac[i - 1]) merge(i, i - 1);     if ((0 <= i + 1 && i + 1 <= n - 1) && ac[i + 1]) merge(i, i + 1);   }   void solve() {     scanf( %d , &n);     scanf( %d , &k);     for (int i = 0; i < n; i++) scanf( %d , &a[i]);     init();     ans = string(n,    );     for (int i = 0; i < n; i++) b[--a[i]] = i;     int t = 0;     for (int i = n - 1; i >= 0; i--)       if (!ac[b[i]]) {         activate(b[i]);         ans[b[i]] =  1  + t;         int cnt = k, j = b[i];         while (cnt-- && j < n) {           if (ac[j]) j = maxi[root(j)] + 1;           if (j >= n) break;           activate(j);           ans[j++] =  1  + t;         }         cnt = k;         j = b[i];         while (cnt-- && j >= 0) {           if (ac[j]) j = mini[root(j)] - 1;           if (j < 0) break;           activate(j);           ans[j--] =  1  + t;         }         t ^= 1;       }     cout << ans << endl;   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     solve();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   inline int Abs(int x) { return x > 0 ? x : -x; }   set<int> peo;   set<int>::iterator p1, p2;   bool vis[200050], boy[200050];   char s[200050];   int a[200050];   struct node {     int l, r;     int val;     node(int a = 0, int b = 0, int c = 0) : l(a), r(b), val(c) {}     bool operator<(const node &x) const {       if (val != x.val) return x.val > val;       if (x.l != l) return x.l > l;       return x.r > r;     }   };   set<node> myset;   set<node>::iterator it;   vector<node> ans;   void work() {     while (myset.size()) {       it = myset.begin();       node u = *it;       myset.erase(it);       if (vis[u.l] || vis[u.r]) continue;       vis[u.l] = vis[u.r] = 1;       ans.push_back(u);       p1 = peo.find(u.l);       if (p1 == peo.begin()) {         peo.erase(p1);         peo.erase(u.r);         continue;       }       p2 = peo.find(u.r);       p2++;       if (p2 == peo.end()) {         peo.erase(p1);         peo.erase(u.r);         continue;       }       p1--;       peo.erase(u.l);       peo.erase(u.r);       u.l = *p1;       u.r = *p2;       if (boy[u.l] == boy[u.r]) continue;       u.val = Abs(a[u.l] - a[u.r]);       myset.insert(u);     }   }   int n;   int main() {     int i, j;     while (cin >> n) {       myset.clear();       peo.clear();       scanf( %s , s + 1);       for (i = 1; i <= n; i++) scanf( %d , &a[i]);       boy[1] = (s[1] ==  B );       for (i = 1; i <= n; i++) {         peo.insert(i);         boy[i + 1] = (s[i + 1] ==  B );         if (i < n && boy[i] != boy[i + 1])           myset.insert(node(i, i + 1, Abs(a[i] - a[i + 1])));       }       memset(vis, 0, sizeof vis);       ans.clear();       work();       cout << ans.size() << endl;       for (i = 0; i < ans.size(); i++) printf( %d %d n , ans[i].l, ans[i].r);     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   struct custom_hash {     static uint64_t splitmix64(uint64_t x) {       x += 0x9e3779b97f4a7c15;       x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;       x = (x ^ (x >> 27)) * 0x94d049bb133111eb;       return x ^ (x >> 31);     }     size_t operator()(uint64_t x) const {       static const uint64_t FIXED_RANDOM =           chrono::steady_clock::now().time_since_epoch().count();       return splitmix64(x + FIXED_RANDOM);     }   };   const long long MOD = 1e9 + 7;   const long long INF = 1e9 + 8;   const double pi = 3.14159265359;   long long binpow(long long a, long long b, long long m) {     a %= m;     long long res = 1;     while (b > 0) {       if (b & 1) res = res * a % m;       a = a * a % m;       b >>= 1;     }     return res;   }   long long inverse(long long x) { return binpow(x, MOD - 2, MOD); }   void solve() {     long long n, x;     cin >> n >> x;     if (n <= 2) {       cout << 1 << endl;       return;     }     cout << (n - 3) / x + 2 << endl;   }   signed main() {     ios_base::sync_with_stdio(0);     cin.tie(0), cout.tie(0);     long long t;     cin >> t;     while (t--) solve();     cerr <<  Time elapsed :   << 1.0 * clock() / CLOCKS_PER_SEC <<   sec  n  ;   }


#include <bits/stdc++.h>   using namespace std;   long long num[100010];   int main() {     int n, q, req;     while (~scanf( %d%d%d , &n, &q, &req)) {       bool res[100010];       res[0] = true;       res[n + 1] = true;       for (int i = 1; i <= n; i++) {         scanf( %d , &num[i]);         if (num[i] <= req)           res[i] = true;         else           res[i] = false;       }       int qiu = 0;       bool flag = true;       for (int i = 1; i <= n; i++) {         if (flag == true && res[i] == false) {           qiu++;           flag = false;         }         if (res[i] == true) flag = true;       }       for (int i = 0; i < q; i++) {         int cur;         scanf( %d , &cur);         if (cur == 0) {           cout << qiu << endl;         } else {           int a;           long long b;           scanf( %d%lld , &a, &b);           num[a] += b;           if (res[a] == true && num[a] > req) {             res[a] = false;             if (res[a - 1] == false && res[a + 1] == false) qiu--;             if (res[a - 1] == true && res[a + 1] == true) qiu++;           }         }       }     }   }


#include <bits/stdc++.h>   using namespace std;   const long long int mod = 1e9 + 7;   long long int t, n, m;   set<long long int> vis;   queue<long long int> q;   vector<long long int> temp, v[500005], g[500005];   int main() {     ios_base::sync_with_stdio(0);     cin.tie(NULL);     cout.tie(NULL);     long long int i, j, k, x, y;     cin >> n >> m;     for (i = 1; i <= m; i++) {       cin >> x >> y;       g[x].push_back(y);       g[y].push_back(x);     }     for (i = 1; i <= n; i++) {       sort(g[i].begin(), g[i].end());       vis.insert(i);     }     long long int comp = 0;     for (i = 1; i <= n; i++) {       if (vis.find(i) == vis.end())         continue;       else         x = i;       vis.erase(x);       q.push(x);       comp++;       while (!q.empty()) {         x = q.front();         q.pop();         v[comp].push_back(x);         temp.clear();         for (auto it : vis) {           if (!binary_search(g[x].begin(), g[x].end(), it)) {             temp.push_back(it);             q.push(it);           }         }         for (auto it : temp) vis.erase(it);       }     }     cout << comp <<   n ;     for (i = 1; i <= comp; i++) {       cout << v[i].size() <<    ;       for (auto it : v[i]) cout << it <<    ;       cout <<   n ;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const double pi = acos(-1);   const int N = (int)1e6 + 7, mod = 1000000007, M = 2e9, lvl = 21;   char s[N];   int n, c[10], x[4] = {1, 6, 8, 9};   vector<int> base, a;   void push(int x) {     a.push_back(x / 100);     x %= 100;     a.push_back(x / 10);     x %= 10;     a.push_back(x);   }   bool div7() {     if (a.size() <= 4) {       int ret = a.back();       a.pop_back();       if (a.empty()) return ret % 7 == 0;       ret += a.back() * 10;       a.pop_back();       if (a.empty()) return ret % 7 == 0;       ret += a.back() * 100;       a.pop_back();       if (a.empty()) return ret % 7 == 0;       ret += a.back() * 1000;       a.pop_back();       if (a.empty()) return ret % 7 == 0;     }     int t = a.back() * 2, bg;     a.pop_back();     bg = a.back();     a.pop_back();     bg += a.back() * 10;     a.pop_back();     bg += a.back() * 100;     a.pop_back();     push(bg - t);     return div7();   }   void print() {     for (int i : base) printf( %d , i);     for (int i = 0; i < 4; i++) printf( %d , x[i]);     while (c[0]--) printf( 0 );     exit(0);   }   int main() {     scanf( %s , s);     n = strlen(s);     for (int i = 0; i < n; i++) c[s[i] -  0 ]++;     c[1]--;     c[6]--;     c[8]--;     c[9]--;     for (int i = 1; i <= 9; i++) {       while (c[i]--) base.push_back(i);     }     do {       a.clear();       for (int i : base) a.push_back(i);       for (int i = 0; i < 4; i++) a.push_back(x[i]);       if (div7()) print();       n++;     } while (next_permutation(x, x + 4));     puts( 0 );     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n, num;     scanf( %d , &n);     int f = 0;     int pre = 0;     bool ans = 1;     for (int i = 0; i < n; i++) {       scanf( %d , &num);       if ((f == 1 && num > pre) || (f == 2 && num >= pre)) ans = 0;       if (num == pre) f = 1;       if (num < pre) f = 2;       pre = num;     }     if (ans)       printf( YES n );     else       printf( NO n );     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     ios::sync_with_stdio(false);     cin.tie(0);     int t;     cin >> t;     while (t--) {       int n, k;       cin >> n >> k;       vector<int> a(n, 0);       for (int i = 0; i < n; i++) cin >> a[i];       vector<int> prev(n, 0);       prev[0] = 0;       int m = -1;       int s = 0;       int l = 0;       int r = 0;       for (int i = 1; i < n - 1; i++) {         if (a[i] > a[i + 1] && a[i - 1] < a[i]) {           s++;         }         prev[i] = s;       }       for (int i = n - 1; i >= k - 1; i--) {         if (prev[i - 1] - prev[i - k + 1] >= m) {           m = prev[i - 1] - prev[i - k + 1];           l = i - k + 1;         }       }       cout << m + 1 <<     << l + 1 << endl;     }   }