#include <bits/stdc++.h>   const int MaxN = 100000, inf = 0x3f3f3f3f;   inline void read(int &ans) {     ans = 0;     char c = getchar();     while (c <  0  || c >  9 ) c = getchar();     while (c >=  0  && c <=  9 ) ans = ans * 10 + c - 48, c = getchar();     return;   }   int n, P;   struct Node {     int pos, t;     friend inline bool operator==(const Node &a, const Node &b) {       return a.pos == b.pos && a.t == b.t;     }   };   struct cmp_time {     inline bool operator()(const Node &a, const Node &b) const {       return a.t == b.t ? a.pos > b.pos : a.t > b.t;     }   };   struct cmp_pos {     inline bool operator()(const Node &a, const Node &b) const {       return a.pos > b.pos;     }   };   template <typename Element, typename cmp>   struct Exque {    private:     std::priority_queue<Element, std::vector<Element>, cmp> que, del;       public:     inline int size() { return que.size() - del.size(); }     inline void pop() {       if (del.size() && que.top() == del.top()) del.pop();       return que.pop();     }     inline void push(const Element &x) { return que.push(x); }     inline void delete_(const Element &x) {       return que.top() == x ? que.pop() : del.push(x);     }     inline Element top() {       while (del.size() && que.top() == del.top()) que.pop(), del.pop();       return que.top();     }   };   std::priority_queue<Node, std::vector<Node>, cmp_time> sitting;   std::priority_queue<Node, std::vector<Node>, cmp_pos> preparing;   Exque<Node, cmp_pos> inque;   std::queue<Node> queorder;   long long ans[MaxN + 1];   int cnt = 0;   int main() {     int i, t;     long long curtime = inf;     read(n), read(P);     for (i = 1; i <= n; ++i) {       read(t), sitting.push((Node){i, t});       if (t < curtime) curtime = t;     }     while (sitting.size() && sitting.top().t <= curtime)       preparing.push(sitting.top()), sitting.pop();     if (preparing.size() &&         (!inque.size() || preparing.top().pos < inque.top().pos))       inque.push(preparing.top()), queorder.push(preparing.top()),           preparing.pop();     while (cnt < n) {       ++cnt, ans[queorder.front().pos] = (curtime += P);       while (sitting.size() && sitting.top().t <= curtime)         if (sitting.top().pos < inque.top().pos)           inque.push(sitting.top()), queorder.push(sitting.top()), sitting.pop();         else           preparing.push(sitting.top()), sitting.pop();       inque.delete_(queorder.front()), queorder.pop();       while (sitting.size() && sitting.top().t <= curtime)         preparing.push(sitting.top()), sitting.pop();       if (preparing.size() &&           (!inque.size() || preparing.top().pos < inque.top().pos))         inque.push(preparing.top()), queorder.push(preparing.top()),             preparing.pop();       if (!inque.size() && sitting.size()) {         curtime = sitting.top().t;         while (sitting.size() && sitting.top().t <= curtime)           preparing.push(sitting.top()), sitting.pop();         inque.push(preparing.top()), queorder.push(preparing.top()),             preparing.pop();       }     }     for (i = 1; i <= n; ++i) printf( %I64d  , ans[i]);     putchar(  n );     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n, k, z;     cin >> n >> k;     vector<pair<int, int>> h, res;     h.reserve(n);     h.resize(k);     for (int i = 0; i < k; ++i) {       cin >> h[i].first;       h[i].second = i;     }     make_heap(begin(h), end(h));     res.reserve(n);     int64_t total = 0;     for (int i = k; i < n; ++i) {       cin >> z;       h.emplace_back(z, i);       push_heap(begin(h), end(h));       auto& p = h.front();       total += p.first * (int64_t)(i - p.second);       res.emplace_back(p.second, i);       pop_heap(begin(h), end(h));       h.pop_back();     }     for (int i = n; !h.empty(); ++i) {       auto& p = h.front();       total += p.first * (int64_t)(i - p.second);       res.emplace_back(p.second, i);       pop_heap(begin(h), end(h));       h.pop_back();     }     sort(begin(res), end(res));     cout << total << endl << res[0].second + 1;     for (int i = 1; i < n; ++i) cout <<     << res[i].second + 1;     cout << endl;   }


#include <bits/stdc++.h>   using namespace std;   const int MOD = 998244353;   const int N_MAX = 50;   int pwr(int a, int b) {     if (b == 0) return 1;     if (b & 1) return 1LL * pwr(a, (b ^ 1)) * a % MOD;     int aux = pwr(a, (b >> 1));     return 1LL * aux * aux % MOD;   }   int n;   int invf[N_MAX + 2];   struct Task {     int l, r;   };   Task tasks[N_MAX + 2];   int points[N_MAX * 2 + 2];   struct Segment {     int l, r;   };   Segment segments[N_MAX * 4 + 2];   int cntSeg;   int combrep[N_MAX * 4 + 2][N_MAX + 2];   int lmax[N_MAX + 2][N_MAX + 2];   int rmin[N_MAX + 2][N_MAX + 2];   int invplg[N_MAX + 2][N_MAX + 2];   int dp[N_MAX + 2][N_MAX * 4 + 2][N_MAX + 2];   int sdp[N_MAX + 2][N_MAX * 4 + 2];   int main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     cin >> n;     for (int i = n; i >= 1; i--) {       cin >> tasks[i].l >> tasks[i].r;       points[i * 2 - 1] = tasks[i].l;       points[i * 2] = tasks[i].r;     }     sort(points + 1, points + n * 2 + 1);     segments[++cntSeg] = Segment{points[1], points[1]};     for (int i = 2; i <= n * 2; i++)       if (points[i] > points[i - 1]) {         if (points[i - 1] + 1 <= points[i] - 1)           segments[++cntSeg] = Segment{points[i - 1] + 1, points[i] - 1};         segments[++cntSeg] = Segment{points[i], points[i]};       }     invf[0] = 1;     for (int i = 1; i <= n; i++)       invf[i] = 1LL * invf[i - 1] * pwr(i, MOD - 2) % MOD;     for (int i = 1; i <= cntSeg; i++)       for (int j = 1; j <= n; j++) {         combrep[i][j] = 1;         int lg = segments[i].r - segments[i].l + 1;         for (int k = lg; k <= lg + j - 1; k++)           combrep[i][j] = 1LL * combrep[i][j] * k % MOD;         combrep[i][j] = 1LL * combrep[i][j] * invf[j] % MOD;       }     for (int i = 1; i <= n; i++) {       lmax[i][i - 1] = INT_MIN;       rmin[i][i - 1] = INT_MAX;       invplg[i][i - 1] = 1;       for (int j = i; j <= n; j++) {         lmax[i][j] = max(lmax[i][j - 1], tasks[j].l);         rmin[i][j] = min(rmin[i][j - 1], tasks[j].r);         invplg[i][j] = 1LL * invplg[i][j - 1] *                        pwr(tasks[j].r - tasks[j].l + 1, MOD - 2) % MOD;       }     }     dp[0][0][0] = 1;     sdp[0][0] = 1;     for (int i = 1; i <= n; i++)       for (int j = 1; j <= cntSeg; j++)         for (int k = 1; k <= i; k++) {           if (lmax[i - k + 1][i] > segments[j].l ||               segments[j].r > rmin[i - k + 1][i])             break;           for (int j1 = 0; j1 < j; j1++) {             dp[i][j][k] += sdp[i - k][j1];             if (dp[i][j][k] >= MOD) dp[i][j][k] -= MOD;           }           dp[i][j][k] = 1LL * dp[i][j][k] * combrep[j][k] % MOD;           dp[i][j][k] = 1LL * dp[i][j][k] * invplg[i - k + 1][i] % MOD;           sdp[i][j] += dp[i][j][k];           if (sdp[i][j] >= MOD) sdp[i][j] -= MOD;         }     int ans = 0;     for (int j = 1; j <= cntSeg; j++) {       ans += sdp[n][j];       if (ans >= MOD) ans -= MOD;     }     cout << ans <<   n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long long N = 1e5 + 5;   const long long mod = 1e9 + 7;   const long long inf = 1e9;   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     int n, m;     cin >> n >> m;     vector<int> v(n);     for (int i = 0; i < n; i++) cin >> v[i];     sort(v.begin(), v.end());     int sum = 0;     for (int i = 0; i < min(n, m); i++) {       if (v[i] < 0) sum += v[i] * -1;     }     cout << sum;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     long long n;     cin >> n;     long long sum = 0;     vector<long long> a(n);     for (long long i = 0; i < n; ++i) {       cin >> a[i];       sum += a[i];     }     vector<long long> prime;     for (long long i = 2; i * i <= sum; ++i) {       if (sum % i == 0) {         prime.push_back(i);         while (sum % i == 0) sum /= i;       }     }     if (sum > 1) prime.push_back(sum);     long long ans = LLONG_MAX;     for (long long i : prime) {       long long now = 0;       long long cnt = 0;       for (long long j = 0; j < n; ++j) {         cnt += a[j];         now += min(cnt % i, i - cnt % i);       }       ans = min(ans, now);     }     cout << (ans == LLONG_MAX ? -1 : ans) << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n;   vector<long long> b;   int main() {     ios_base::sync_with_stdio(false);     cin >> n;     b.resize(n);     for (int i = 0; i < n; ++i) cin >> b[i];     long long mi = *min_element(b.begin(), b.end());     long long ma = *max_element(b.begin(), b.end());     if (mi == ma) {       if (mi == 0) {         cout <<  YES n ;         for (int i = 0; i < n; ++i) cout <<  1  ;       } else         cout <<  NO ;       return 0;     }     cout <<  YES n ;     b.push_back(b[0]);     int i = 1;     while (!(b[i] == ma && b[i - 1] < ma)) ++i;     i %= n;     for (int j = 0; j < n - 1; ++j) {       int ii = (i + n - 1) % n;       b[ii] += (j ? b[i] : 2 * b[i]);       i = ii;     }     for (int i = 0; i < n; ++i) cout << b[i] <<    ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 1555;   const int INF = 0x3f3f3f3f;   int sx, sy;   int n, m;   char maze[N][N];   int dir[4][2] = {0, 1, 1, 0, -1, 0, 0, -1};   struct node {     int x, y;   } vis[N][N];   bool bfs(int x, int y) {     queue<node> q;     node now, next;     now.x = x;     now.y = y;     q.push(now);     while (!q.empty()) {       now = q.front();       q.pop();       for (int i = 0; i < 4; i++) {         next.x = now.x + dir[i][0];         next.y = now.y + dir[i][1];         if (maze[(next.x % n + n) % n][(next.y % m + m) % m] !=  # ) {           if (vis[(next.x % n + n) % n][(next.y % m + m) % m].x == INF) {             vis[(next.x % n + n) % n][(next.y % m + m) % m].x = next.x;             vis[(next.x % n + n) % n][(next.y % m + m) % m].y = next.y;             q.push(next);           } else if (next.x !=                          vis[(next.x % n + n) % n][(next.y % m + m) % m].x ||                      next.y != vis[(next.x % n + n) % n][(next.y % m + m) % m].y)             return true;         }       }     }     return false;   }   int main() {     ios::sync_with_stdio(false);     cin.tie(0);     cin >> n >> m;     memset(vis, INF, sizeof(vis));     for (int i = 0; i < n; i++) {       for (int j = 0; j < m; j++) {         cin >> maze[i][j];         if (maze[i][j] ==  S ) sx = i, sy = j;       }     }     if (bfs(sx, sy))       cout <<  Yes  << endl;     else       cout <<  No  << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n, ar[1010];   bool b[1010];   int main() {     cin >> n;     for (int i = 1; i <= n; ++i) {       cin >> ar[i];     }     for (int i = 1; i <= n; ++i) {       int yk = i;       while (1) {         if (b[yk]) break;         b[yk] = 1;         yk = ar[yk];       }       cout << yk <<    ;       fill(b, b + 1010, 0);     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int bs;   int* cnt;   int ans;   class query {    public:     int l;     int r;     int index;   };   bool cmp(const query a, const query b) {     int s = a.l / bs;     int e = b.l / bs;     if (s == e) return a.r < b.r;     return s < e;   }   void add(int x) {     if (cnt[x] == x)       ans--;     else if (cnt[x] == x - 1)       ans++;     cnt[x]++;   }   void del(int x) {     if (cnt[x] == x)       ans--;     else if (cnt[x] == x + 1)       ans++;     cnt[x]--;   }   int main() {     int n, m, *a;     query* q;     scanf( %d %d , &n, &m);     bs = sqrt(n);     a = (int*)calloc(n + 1, sizeof(int));     cnt = (int*)calloc(n + 1, sizeof(int));     int* an = (int*)calloc(m + 1, sizeof(int));     q = (query*)calloc(m + 1, sizeof(query));     for (int i = 0; i < n; i++) scanf( %d , &a[i]);     for (int i = 0; i < m; i++) {       scanf( %d %d , &q[i].l, &q[i].r);       q[i].index = i;       q[i].l--;       q[i].r--;     }     sort(q, q + m, cmp);     ans = 0;     int l = 0, r = -1;     for (int i = 0; i < m; i++) {       int left = q[i].l;       int right = q[i].r;       while (r < right) {         r++;         if (a[r] < n + 1) add(a[r]);       }       while (r > right) {         if (a[r] < n + 1) del(a[r]);         r--;       }       while (l < left) {         if (a[l] < n + 1) del(a[l]);         l++;       }       while (l > left) {         l--;         if (a[l] < n + 1) add(a[l]);       }       an[q[i].index] = ans;     }     for (int i = 0; i < m; i++) printf( %d n , an[i]);     free(a);     free(an);     free(cnt);     free(q);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int MAXn = 1e5 + 10, MAXlg = 20;   long long dp[MAXn][MAXlg], h[MAXn], dp2[MAXn], n, m, dp3[MAXn], dp4[MAXn];   vector<int> v[MAXn];   bool flag[MAXn], flag2[MAXn];   int make_tree(int a) {     flag[a] = true;     int ans = 1;     h[a] = h[dp[a][0]] + 1;     if (v[a].size() == 1 && a != 1) {       dp2[a] = 1;       return ans;     }     for (int i = 0; i < v[a].size(); i++)       if (!flag[v[a][i]]) {         dp[v[a][i]][0] = a;         ans += make_tree(v[a][i]);         dp3[a] += dp3[v[a][i]];       }     dp3[a] += ans - 1;     dp2[a] = ans;     return ans;   }   void make_dp(int a) {     flag2[a] = true;     for (int i = 0; i < v[a].size(); i++)       if (!flag2[v[a][i]]) {         dp4[v[a][i]] = dp4[a] + (n - dp2[a]) + dp3[a] + (dp2[a] - dp2[v[a][i]]) -                        (dp3[v[a][i]] + dp2[v[a][i]]);         make_dp(v[a][i]);       }   }   void make_lca() {     for (int i = 1; i < MAXlg; i++)       for (int j = 1; j <= n; j++) dp[j][i] = dp[dp[j][i - 1]][i - 1];   }   long double get_lca(int a, int b) {     if (h[a] > h[b]) swap(a, b);     int aa = a, bb = b, cc = b;     long long dis = h[b] - h[a];     for (int i = 0; i < MAXlg; i++)       if ((dis >> i) & 1) b = dp[b][i];     if (a == b) {       long double ans = 0;       dis--;       for (int i = 0; i < MAXlg; i++)         if ((dis >> i) & 1) bb = dp[bb][i];       ans += (long double)(dp4[a] + dp3[a] - dp3[bb] - dp2[bb]) * dp2[cc];       ans += (long double)(dp3[cc]) * (n - dp2[bb]);       ans += (long double)(dis + 2) * (dp2[1] - dp2[bb]) * dp2[cc];       ans /= (long double)(dp2[1] - dp2[bb]) * dp2[cc];       return ans;     }     for (int i = MAXlg - 1; i >= 0; i--)       if (dp[a][i] != dp[b][i]) {         a = dp[a][i];         b = dp[b][i];       }     int lca = dp[a][0];     long double ans = 0;     ans += (long double)dp3[aa] * dp2[bb] + dp3[bb] * dp2[aa] +            (long double)((long double)-2 * h[lca] + h[aa] + h[bb] + 1) * dp2[aa] *                dp2[bb];     ans /= (long double)dp2[aa] * dp2[bb];     return ans;   }   int main() {     cin >> n >> m;     for (int i = 0; i < n - 1; i++) {       int a, b;       cin >> a >> b;       v[a].push_back(b);       v[b].push_back(a);     }     make_tree(1);     make_lca();     make_dp(1);     for (int i = 0; i < m; i++) {       int a, b;       cin >> a >> b;       cout << fixed << setprecision(10) << get_lca(a, b) << endl;     }   }


#include <bits/stdc++.h>   using namespace std;   using ll = long long;   using cd = complex<double>;   ll base = 100;   void fft(vector<cd>& a, bool inv) {     int n = a.size();     int bits = __builtin_ctz(n);     for (int i = 0; i < n; ++i) {       int x = 0;       for (int j = 0; j < bits; ++j) x = (x << 1) | (i >> j & 1);       if (i > x) swap(a[i], a[x]);     }     for (int m = 2; m <= n; m *= 2) {       auto alpha = (inv ? -2 : 2) * 3.14159265358979323846 / m;       auto w = exp(cd(0, alpha));       for (int k = 0; k < n; k += m) {         cd x = 1;         for (int i = 0; i < m / 2; ++i) {           auto u = a[k + i];           auto v = a[k + i + m / 2] * x;           a[k + i] = u + v;           a[k + i + m / 2] = u - v;           x *= w;         }       }     }     if (inv)       for (auto& x : a) x /= n;   }   void int_mul(vector<ll>& a, ll v) {     ll carry = 0;     for (int i = 0; i < a.size() || carry; ++i) {       if (i >= a.size()) a.emplace_back(0ll);       a[i] = a[i] * v + carry;       carry = a[i] / base;       a[i] %= base;     }   }   void shrink(vector<ll>& a) {     while (a.size() > 1 && a.back() == 0) a.pop_back();   }   void normalize(vector<ll>& a) {     ll carry = 0;     for (int i = 0; i < a.size() || carry; ++i) {       if (i >= a.size()) a.emplace_back(0);       carry += a[i];       a[i] = carry % base;       carry -= a[i];       carry /= base;     }     shrink(a);   }   void sqr(vector<ll>& a) {     int n = a.size();     int m = 1;     while (m < n + n) m *= 2;     vector<cd> b(m);     for (int i = 0; i < n; ++i) b[i] = a[i];     fft(b, false);     for (auto& x : b) x *= x;     fft(b, true);     a.resize(b.size());     for (int i = 0; i < b.size(); ++i) a[i] = b[i].real() + 0.5;     normalize(a);   }   vector<ll> bp(int e) {     if (e == 0) return vector<ll>{1};     auto v = bp(e / 2);     sqr(v);     if (e & 1) int_mul(v, 3);     return v;   }   int compare(vector<ll> const& a, vector<ll> const& b) {     if (a.size() != b.size()) return a.size() < b.size() ? -1 : 1;     for (int i = int(a.size()) - 1; i >= 0; --i)       if (a[i] != b[i]) return a[i] < b[i] ? -1 : 1;     return 0;   }   int main() {     cin.tie(0);     cin.sync_with_stdio(0);     string number;     cin >> number;     vector<ll> num;     int n = number.size();     int x = 2;     num.reserve((n + x - 1) / x);     reverse(number.begin(), number.end());     for (int i = 0; i < n; i += x) {       auto s = number.substr(i, x);       reverse(s.begin(), s.end());       num.emplace_back(stoll(s));     }     int log3 = n / log10(3);     int start = max(0, log3 - 3);     int last = 5;     auto A = bp(start);     shrink(A);     auto B = A;     int_mul(B, 2);     auto C = B;     int_mul(C, 2);     int ans = 3 * 10 * 1000 * 1000;     for (int p = 0; p < last; ++p) {       int v = 3 * (start + p);       if (v < ans && compare(A, num) >= 0) {         ans = min(ans, v);         break;       } else if (v + 3 < ans) {         int_mul(A, 3);       } else {         break;       }     }     for (int p = 0; p < last; ++p) {       int v = 2 + 3 * (start + p);       if (v < ans && compare(B, num) >= 0) {         ans = min(ans, v);         break;       } else if (v + 3 < ans) {         int_mul(B, 3);       } else {         break;       }     }     for (int p = 0; p < last; ++p) {       int v = 4 + 3 * (start + p);       if (v < ans && compare(C, num) >= 0) {         ans = min(ans, v);         break;       } else if (v + 3 < ans) {         int_mul(C, 3);       } else {         break;       }     }     cout << max(1, ans);   }


#include <bits/stdc++.h>   using namespace std;   #pragma GCC optimize( Ofast )   void print(vector<long long> vvv) {     for (auto it : vvv) cout << it <<    ;     cout <<   n ;   }   long long mod = 1e9 + 7;   long long qpow(long long base, long long p) {     if (p == 0) return 1;     if (p == 1) return base % mod;     long long r = qpow(base, p / 2);     if (p % 2 == 0) return (r * r) % mod;     return (((r * r) % mod) * base) % mod;   }   long long inv(long long a, long long b) {     return (((a) % mod) * qpow(b, mod - 2)) % mod;   }   long long mand(pair<long long, long long> a, pair<long long, long long> b) {     return abs(a.first - b.first) + abs(a.second - b.second);   }   char invc(char a, char b, char c) {     if (a == b) return c;     return b;   }   int32_t main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.setf(ios::fixed);     cout.precision(0);     long long n;     cin >> n;     vector<long long> v, ans;     vector<pair<long long, long long> > v2;     for (long long i = 0; i < n; i++) {       long long x;       cin >> x;       ans.push_back(0);       v2.push_back({x, i});     }     sort(v2.begin(), v2.end());     for (long long i = 0; i < n; i++) {       if (i != n - 1) {         ans[v2[i].second] = v2[i + 1].first;       } else {         ans[v2[i].second] = v2[0].first;       }     }     print(ans);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int maxn = 2e5 + 5;   const long long inf = 1e18;   int ntest;   int n, m, k;   string s;   int cnt[55];   bool ok[maxn];   void read_input() {     cin >> n >> m >> k;     cin >> s;   }   void solve() {     fill(cnt, cnt + 26, 0);     for (int i = 0; i < k; ++i) ++cnt[s[i] -  A ];     long long ans = inf;     for (int i = 0; i < 26; ++i) {       fill(ok + 1, ok + k + 1, false);       ok[0] = true;       for (int j = 0; j < 26; ++j)         if (j != i) {           for (int sum = k - cnt[i]; sum >= 0; --sum)             if (ok[sum]) ok[sum + cnt[j]] = true;         }       for (int sum = 0; sum <= n; ++sum) {         if (!ok[sum]) continue;         int xn = n - sum;         int xm = max(0, m - (k - cnt[i] - sum));         if (xn + xm > cnt[i]) continue;         ans = min(ans, xn * 1LL * xm);       }     }     cout << ans <<   n ;   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(nullptr);     cin >> ntest;     while (ntest--) {       read_input();       solve();     }   }


#include <bits/stdc++.h>   using namespace std;   mt19937 mt(time(0));   long long n, k;   long long a[123456];   const long long inf = (int)(1e9);   int cnt = 0;   long long mas[123456];   long long solve() {     long long md;     for (int i = mt() % 123456 + (int)(1e9);; i++) {       bool ok = false;       for (int j = 2; j * j <= i; j++) {         if (i % j == 0) {           ok = true;           break;         }       }       if (!ok) {         md = i;         break;       }     }     long long tmp = 1;     for (int i = 0; i <= n; i++) {       mas[i] = tmp;       tmp *= k;       tmp %= md;     }     long long sum = 0;     for (int i = 0; i <= n; i++) {       long long t = (a[i] * mas[i]) % md;       sum += t;       sum %= md;     }     return sum;   }   int main() {     ios_base::sync_with_stdio(0);     cin >> n >> k;     for (int i = 0; i <= n; i++) {       string s;       cin >> s;       if (s ==  ? )         a[i] = inf;       else {         istringstream iss(s);         iss >> a[i];         cnt++;       }     }     if (cnt < n + 1) {       if (k == 0 && a[0] != 0 && a[0] != inf) {         cout <<  No  << endl;         exit(0);       }       if (n % 2 == 1) {         if (k == 0 && a[0] == inf && cnt % 2 == 0)           cout <<  No  << endl;         else           cout <<  Yes  << endl;       } else {         if ((k == 0 && a[0] == 0) || (k == 0 && a[0] == inf && cnt % 2 == 1))           cout <<  Yes  << endl;         else           cout <<  No  << endl;       }       exit(0);     }     long long attempt1 = solve();     long long attempt2 = solve();     long long attempt3 = solve();     if (attempt1 == 0 && attempt2 == 0 && attempt3 == 0)       cout <<  Yes  << endl;     else       cout <<  No  << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     long long int t;     cin >> t;     while (t--) {       long long int n, k;       cin >> n >> k;       string s, s1;       cin >> s;       long long int a[27] = {0}, c = 0, d = 0;       for (long long int i = 0; i < n; i++) {         if (a[s[i] -  a ] == 0) d++;         a[s[i] -  a ]++;       }       s1 = s;       sort(s1.begin(), s1.end());       if (k == 1)         cout << s1 << endl;       else {         long long int c1 = 0, c2 = 0;         for (long long int i = 0; i < n; i++) {           if (s1[i] == s1[0])             c1++;           else             break;         }         if (c1 < k) {           cout << s1[k - 1] << endl;         } else if (c1 == k) {           if (d == 1)             cout << s1[0] << endl;           else if (d == 2) {             long long int c2 = n - c1;             if (c2 % k == 0)               c2 = c2 / k;             else               c2 = (c2 / k) + 1;             cout << s1[0] <<   ;             for (long long int i = 0; i < c2; i++) cout << s1[c1] <<   ;             cout << endl;           } else {             cout << s1[0] <<   ;             for (long long int i = c1; i < n; i++) {               cout << s1[i] <<   ;             }             cout << endl;           }         } else {           if (d == 1) {             cout << s1[0] <<   ;             long long int c2 = c1 - k;             if (c2 % k == 0)               c2 = c2 / k;             else               c2 = (c2 / k) + 1;             for (long long int i = 0; i < c2; i++) cout << s1[0] <<   ;             cout << endl;           } else {             for (long long int i = k - 1; i < n; i++) cout << s1[i] <<   ;             cout << endl;           }         }       }     }   }


#include <bits/stdc++.h>   using namespace std;   struct target {     long double x;     long double y;     long long t;     long double p;     bool operator<(const target &b) const;   };   bool target::operator<(const target &b) const { return t < b.t; }   int main() {     cout.precision(30);     vector<target> v;     int n;     cin >> n;     for (int i = 0; i < n; i++) {       target t;       cin >> t.x >> t.y >> t.t >> t.p;       v.push_back(t);     }     sort(v.begin(), v.end());     long double score[1010];     for (long long i = n - 1; i >= 0; i--) {       long double baseScore = v[i].p;       score[i] = v[i].p;       for (long long i2 = i + 1; i2 < n; i2++) {         long double dist = sqrt((v[i].x - v[i2].x) * (v[i].x - v[i2].x) +                                 (v[i].y - v[i2].y) * (v[i].y - v[i2].y));         if (dist <= v[i2].t - v[i].t) {           if (baseScore + score[i2] > score[i]) {             score[i] = baseScore + score[i2];           }         }       }     }     long double maxScore = score[0];     for (int i = 1; i < n; i++) {       if (score[i] > maxScore) {         maxScore = score[i];       }     }     cout << maxScore << endl;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int t;     cin >> t;     while (t--) {       int x, n, m, temp = 0;       cin >> x >> n >> m;       for (int i = 0; i <= n; i++) {         if (x <= 10 * m) {           temp = 1;           break;         } else           x = x / 2 + 10;       }       if (temp == 1)         cout <<  YES  << endl;       else         cout <<  NO  << endl;     }   }


#include <bits/stdc++.h>   using namespace std;   static const double EPS = 1e-6;   int ROUND(double x) { return (int)(x + 0.5); }   bool ISINT(double x) { return fabs(ROUND(x) - x) <= EPS; }   bool ISEQUAL(double x, double y) {     return fabs(x - y) <= EPS * max(1.0, max(fabs(x), fabs(y)));   }   double SQSUM(double x, double y) { return x * x + y * y; }   template <class T>   bool INRANGE(T x, T a, T b) {     return a <= x && x <= b;   }   void split1(const string& str, vector<string>& out) {     string::size_type st = 0;     string::size_type next = 0;     int word_num = 0;     string tmp = str;     do {       next = tmp.find(   , st);       string word = tmp.substr(st, next - st);       if (word.length() >= 1) {         out.push_back(word);       }       st = next + 1;     } while (next != string::npos);   }   int main() {     int n;     scanf( %d  , &n);     vector<string> team_names;     for (int i = 0; i < n; i++) {       char str[256];       scanf( %s  , str);       team_names.push_back(string(str));     }     map<string, pair<int, pair<int, int> > > mp;     for (int i = 0; i < n * (n - 1) / 2; i++) {       char str[256];       char str2[256];       scanf( %s %s  , str, str2);       for (int k = 0; k < 256; k++) {         if (str[k] ==  -  || str[k] ==  : ) {           str[k] =    ;         }         if (str2[k] ==  -  || str2[k] ==  : ) {           str2[k] =    ;         }       }       vector<string> vs;       split1(string(str), vs);       vector<string> vs2;       split1(string(str2), vs2);       string team1, team2;       int goal1, goal2;       team1 = vs[0];       team2 = vs[1];       goal1 = atoi(vs2[0].c_str());       goal2 = atoi(vs2[1].c_str());       mp[team1].first += goal1 > goal2 ? 3 : goal1 == goal2 ? 1 : 0;       mp[team1].second.first += goal1 - goal2;       mp[team1].second.second += goal1;       mp[team2].first += goal2 > goal1 ? 3 : goal2 == goal1 ? 1 : 0;       mp[team2].second.first += goal2 - goal1;       mp[team2].second.second += goal2;     }     map<pair<int, pair<int, int> >, string> inv_mp;     {       map<string, pair<int, pair<int, int> > >::iterator it;       for (it = mp.begin(); it != mp.end(); it++) {         inv_mp[it->second] = it->first;       }     }     vector<string> winners;     {       map<pair<int, pair<int, int> >, string>::reverse_iterator inv_it =           inv_mp.rbegin();       for (int i = 0; i < n / 2; inv_it++, i++) {         winners.push_back(inv_it->second);       }     }     sort(winners.begin(), winners.end());     for (int i = 0; i < ((int)winners.size()); i++) {       printf( %s n , winners[i].c_str());     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long int power(long long int a, long long int b) {     long long int res = 1;     while (b) {       if (b % 2 == 1) res = (res * a) % 1000000007;       a = (a * a) % 1000000007;       b /= 2;     }     return res;   }   bool comp(long long int a, long long int b) { return a < b; }   long long int binary(long long int *a, long long int l, long long int r,                        long long int key) {     long long int mid;     while (l <= r) {       mid = (l + r) / 2;       if (a[mid] == key) {         return mid;       } else if (a[mid] < key) {         l = mid + 1;       } else {         r = mid - 1;       }     }     return -1;   }   bool isprime(long long int n) {     if (n <= 1) {       return false;     }     int i;     for (i = 2; i <= ceil(sqrt((double)n)); i++) {       if (n % i == 0) {         return false;       }     }     return true;   }   bool isPalindrome(long long int n) {     long long int rev = 0, temp = n, rem;     while (temp) {       rem = temp % 10;       rev = rev * 10 + rem;       temp /= 10;     }     if (n == rev) return true;     return false;   }   long long int mulmod(long long int a, long long int b, long long int m) {     long long int res = 0;     a = a % m;     while (b > 0) {       if (b % 2 == 1) {         res = (res + a) % m;       }       a = (a * 2) % m;       b /= 2;     }     return (res % m);   }   bool isLeapYear(int year) {     if (year % 400 == 0) return true;     if (year % 100 == 0) return false;     if (year % 4 == 0) return true;     return false;   }   int ones(long long int n) {     int ans = 0;     while (n) {       ans++;       n &= (n - 1);     }     return ans;   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     cout.tie(NULL);     long long int t, pro = 1, n, m, temp, k, i, l, r, mid, j, x, y, z, ind,                      mx = LONG_LONG_MIN, mn = LONG_LONG_MAX, ans = 0, cnt = 0,                      curr = 0, prev, sum = 0, flag = 0, i1 = -1, i2 = -1;     cin >> n;     long long int a[n];     for (i = 0; i <= n - 1; i++) {       cin >> a[i];       mn = min(mn, a[i]);     }     vector<long long int> v;     for (i = 0; i <= n - 1; i++) {       if (a[i] == mn) {         v.push_back(i);       }     }     ans = LONG_LONG_MAX;     for (i = 0; i <= v.size() - 2; i++) {       ans = min(ans, v[i + 1] - v[i]);     }     cout << ans;     return 0;   }


#include <bits/stdc++.h>   const long long INF = 2e18;   const double EPS = 1e-9;   using namespace std;   void WEZaa() {     std::ios_base::sync_with_stdio(0);     cin.tie(NULL);     cout.tie(NULL);   }   int main() {     WEZaa();     unsigned long long n, k;     cin >> n >> k;     vector<unsigned long long> a(n), b(n);     for (auto& i : a) cin >> i;     for (auto& i : b) cin >> i;     unsigned long long l = 0, r = 1e10, ans = 0, md;     while (l <= r) {       md = (l + r) / 2;       unsigned long long sum = 0;       for (int i = 0; i < n; i++) {         unsigned long long st = a[i] * md;         if (b[i] < st) sum += st - b[i];       }       if (sum <= k)         l = md + 1, ans = md;       else         r = md - 1;     }     cout << ans <<   n ;   }


#include <bits/stdc++.h>   using namespace std;   int n, ans1, ans2, nr, x;   vector<int> v;   int main() {     cin >> n;     for (int i = 1; i <= n; ++i) {       cin >> nr;       for (int j = 1; j <= nr; ++j) {         cin >> x;         if (nr % 2 == 1 && j == (nr + 1) / 2) {           v.push_back(-x);           continue;         }         if (j <= nr / 2)           ans1 += x;         else           ans2 += x;       }     }     sort(v.begin(), v.end());     for (int i = 0; i < v.size(); ++i) {       if (i % 2 == 0)         ans1 -= v[i];       else         ans2 -= v[i];     }     cout << ans1 <<     << ans2;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     long long int n, k, ans = INT_MAX;     cin >> n >> k;     for (long long int i = 0; i < n; i++) {       long long int x;       cin >> x;       if (k % x == 0) {         ans = min(ans, k / x);       }     }     cout << ans;     return 0;   }


#include <bits/stdc++.h>   using namespace ::std;   template <class T>   T SQR(T N) {     return (T)N * N;   }   template <class T>   double DIST(T a, T b) {     return 1.0 * sqrt(SQR(double(a)) + SQR(double(b)));   }   template <class T>   bool CROSS(pair<T, T> a, pair<T, T> b, pair<T, T> c) {     return (b.first - a.first) * (c.second - b.second) >=            (b.second - a.second) * (c.first - b.first);   }   template <class ll>   ll POW(ll a, ll b, ll mod) {     ll res = 1 % mod, x = a % mod;     while (b) {       if (b & 1) res = (res * x) % mod;       b /= 2, x = (x * a) % mod;     }     return res;   }   int px[] = {0, 1, -1, 0, -1, -1, 1, 1};   int py[] = {1, 0, 0, -1, -1, 1, -1, 1};   double EPS = 1e-9;   double PI = acos(-1);   double GR = 1.6180339887498948;   int F[7];   inline void Fail(string str) {     string s = str;     reverse(s.begin(), s.end());     s = s +  #  + str;     int len = s.length();     for (int i = 1; i < len; ++i) {       int j = F[i - 1];       while (j > 0 and s[i] != s[j]) j = F[j - 1];       if (s[i] == s[j]) ++j;       F[i] = j;     }   }   int main() {     int n, m, c, d, a, b, k;     int test;     scanf( %d%d%d , &n, &m, &d);     bool flag = 0;     b = c = 0;     for (int i = 0; i < n; ++i)       scanf( %d , &a), (a % d and !flag) ? flag = 1, b = i : flag;     flag = 0;     for (int i = 0; i < m; ++i)       scanf( %d , &a), (a % d and !flag) ? flag = 1, c = i : flag;     printf( %d n , b + c);   }


#include <bits/stdc++.h>   using namespace std;   long long derangements(long long n) {     if (n == 0) return 1;     if (n == 1) return 0;     if (n == 2) return 1;     return (n - 1) * (derangements(n - 1) + derangements(n - 2));   }   long long nCr(long long n, long long r) {     long long res = 1;     if (r == 0) return res;     for (long long i = 0; i < r; i++) res *= (n - i);     for (long long i = 2; i <= r; i++) res /= i;     return res;   }   int32_t main() {     ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);     long long n, k, ans = 0;     cin >> n >> k;     for (long long i = 0; i <= k; i++) ans += (nCr(n, i) * derangements(i));     cout << ans;     return 0;   }


#include <bits/stdc++.h>   #pragma GCC optimize( O3 )   #pragma GCC target( sse4 )   using namespace std;   template <class c>   struct rge {     c b, e;   };   template <class c>   rge<c> range(c i, c j) {     return rge<c>{i, j};   }   template <class c>   auto dud(c* x) -> decltype(cerr << *x, 0);   template <class c>   char dud(...);   struct debug {     template <class c>     debug& operator<<(const c&) {       return *this;     }   };   const long long N = 1e5 + 5;   int32_t main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     long long n;     cin >> n;     vector<long long> a(n), b;     for (long long i = 0; i < n; i++) {       cin >> a[i];       if (a[i] & 1) {         b.push_back(i);       } else {         a[i] = a[i] / 2;       }     }     long long k = 0;     for (long long i = 0; i < (long long)b.size(); i++) {       if (a[b[i]] >= 0) {         if (!k) {           a[b[i]] = (a[b[i]] + 1) / 2;         } else {           a[b[i]] = a[b[i]] / 2;         }       } else {         if (k) {           a[b[i]] = (a[b[i]] - 1) / 2;         } else {           a[b[i]] = a[b[i]] / 2;         }       }       k ^= 1;     }     for (long long i = 0; i < n; i++) {       cout << a[i] <<   n ;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int N, K, X;     cin >> N >> K >> X;     vector<int> Balls[100];     for (int i = 0; i < N; i++) {       int color;       cin >> color;       Balls[0].push_back(color);     }     for (int i = 1; i < 100; i++) {       Balls[i] = Balls[0];     }     int result = 0;     for (int i = 0; i < N; i++) {       Balls[i].insert(Balls[i].begin() + i, X);       for (int j = 0; j < (int)Balls[i].size() - 2; j++) {         if (Balls[i][j] == Balls[i][j + 1] && Balls[i][j] == Balls[i][j + 2]) {           int color = Balls[i][j];           while (j != (int)Balls[i].size() && Balls[i][j] == color) {             Balls[i].erase(Balls[i].begin() + j);           }           j = -1;         }       }       result = max(result, N - (int)Balls[i].size());     }     cout << result;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int a;     cin >> a;     if (a <= 4)       cout << a % 3 + 1 << endl;     else if (a == 5)       cout << a - 4 << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   inline long long read() {     long long num = 0, neg = 1;     char c = getchar();     while (!isdigit(c)) {       if (c ==  - ) neg = -1;       c = getchar();     }     while (isdigit(c)) {       num = (num << 3) + (num << 1) + c -  0 ;       c = getchar();     }     return num * neg;   }   int p, lmt;   int n, cnt[200010], a[200010], sum[200010];   int exs[200010], exs1[200010], ans, first[400010];   vector<int> loc;   map<int, int> used;   inline bool check() {     int mx = 0, tot = 0;     for (int i = 1; i <= n; i++) {       if (cnt[i] > mx) mx = cnt[i], tot = 0, p = i;       if (cnt[i] == mx) tot++;     }     return tot > 1;   }   int main() {     n = read();     lmt = 400;     for (int i = 1; i <= n; i++) {       a[i] = read();       cnt[a[i]]++;     }     if (check()) {       printf( %d n , n);       return 0;     }     for (int i = 1; i <= n; i++)       if (a[i] == p) loc.push_back(i);     for (int u = 1; u <= n; u++)       if (cnt[u] > lmt && u != p) {         for (int i = 0; i <= 2 * n; i++) first[i] = n + 1;         first[n] = 0, sum[0] = n;         for (int i = 1; i <= n; i++) {           sum[i] = sum[i - 1] + (a[i] == u) - (a[i] == p);           if (first[sum[i]] <= n)             ans = max(ans, i - first[sum[i]]);           else             first[sum[i]] = i;         }       }     vector<pair<int, int> > seg;     for (int len = 1; len <= lmt + 1; len++) {       seg.clear();       for (int i = 0; i < loc.size(); i++) {         int j = i + len - 1;         if (j >= loc.size()) break;         int l = (i == 0 ? 1 : loc[i - 1] + 1),             r = (j == loc.size() - 1 ? n : loc[j + 1] - 1);         seg.push_back(make_pair(l, r));       }       int l = 1, r = 0, mx = 0;       memset(exs, 0, sizeof(exs));       memset(exs1, 0, sizeof(exs1));       for (int i = 0; i < seg.size(); i++) {         while (r < seg[i].second) {           mx += 2;           r++;           if (a[r] != p) {             exs1[exs[a[r]]]--;             exs[a[r]]++;             exs1[exs[a[r]]]++;           }           while (!exs1[mx] && mx) mx--;         }         while (l < seg[i].first) {           mx += 2;           if (a[l] != p) {             exs1[exs[a[l]]]--;             exs[a[l]]--;             exs1[exs[a[l]]]++;           }           l++;           while (!exs1[mx] && mx) mx--;         }         if (mx >= len) ans = max(ans, r - l + 1);       }     }     cout << ans;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   void solve() {     long long int i, j, n, s = 0, c = 0, x, k, y, m, z, ans = 0;     cin >> n >> m;     cin >> x >> z >> y;     vector<long long int> a(n), b(m);     for (i = 0; i < n; i++) {       cin >> a[i];     }     for (i = 0; i < m; i++) {       cin >> b[i];     }     if (n == m) {       for (i = 0; i < n; i++) {         if (a[i] != b[i]) {           cout << -1;           return;         }       }       cout << 0;       return;     }     if (m > n) {       cout << -1;       return;     }     i = 0;     j = 0;     k = 0;     long long int ma = INT_MIN, l;     while (i < n && j < m) {       if (a[i] != b[j]) {         ma = max(ma, a[i]);         i++;       } else {         l = i - k;         if (l == 0) {           i++;           j++;           k = i;           continue;         }         long long int maxel;         if (k > 0) {           maxel = max(a[k - 1], a[i]);         } else {           maxel = a[i];         }         if (l < z) {           if (maxel < ma) {             cout << -1;             return;           }           ans += l * y;         } else {           if (y * z < x && maxel > ma) {             ans += l * y;           } else if (y * z < x) {             l = l - z;             ans += x + l * y;           } else {             long long int rem = l % z;             long long int gro = l / z;             ans += gro * x + rem * y;           }         }         j++;         i++;         k = i;         ma = INT_MIN;       }     }     l = n - k;     for (long long int q = i; q < n; q++) {       ma = max(ma, a[q]);     }     if (l > 0) {       long long int maxel = a[k - 1];       if (l < z) {         if (maxel < ma) {           cout << -1;           return;         }         ans += l * y;       } else {         if (y * z < x && maxel > ma) {           ans += l * y;         } else if (y * z < x) {           l = l - z;           ans += x + l * y;         } else {           long long int rem = l % z;           long long int gro = l / z;           ans += gro * x + rem * y;         }       }       j++;       i++;       k = i;       ma = INT_MIN;     }     cout << ans;   }   int main() {     long long int t;     t = 1;     while (t--) {       solve();       cout <<   n ;     }   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int i, j, k, l, m, n, p, x, y;     string s1, s2, s3, s4;     cin >> s1 >> s2;     cin >> n;     cout << s1 <<     << s2;     cout << endl;     for (i = 0; i < n; i++) {       cin >> s3 >> s4;       if (s3 == s1) {         s1 = s4;         cout << s1 <<     << s2;       }       if (s3 == s2) {         s2 = s4;         cout << s1 <<     << s2;       }       cout << endl;     }   }


#include <bits/stdc++.h>   using namespace std;      typedef long double             ld;   typedef long long int           int64;   typedef unsigned long long int  uint64;   typedef pair<int, int>          PII;   typedef vector<int>             VI;   typedef vector<long long>       VLL;   typedef vector<PII>             VII;   typedef vector<VI>              VVI;      #define MEM(a, b)               memset(a, (b), sizeof(a))   #define clr(a)                  fill(a, 0)      #define FOR(i, j, k, in)        for (int i = (j); i < (k); i += (in))   #define FORW(i, j, k, in)       for (int i = (j); i <= (k); i += (in))   #define RFOR(i, j, k, in)       for (int i = (j); i >= (k); i -= (in))      #define all(cont)               cont.begin(), cont.end()   #define rall(cont)              cont.rbegin(), cont.rend()   #define sz(cont)                int((cont).size())   #define pb                      push_back   #define mp                      make_pair   #define fi                      first   #define se                      second      #define IINF                    0x3f3f3f3f   #define LLINF                   1000111000111000111LL   #define PI                      3.1415926535897932384626433832795      #define FastIO                  ios_base::sync_with_stdio(false); std::cin.tie(NULL); std::cout.tie(NULL);   #define hardio(name)            freopen(name .inp , r ,stdin), freopen(name .out , w ,stdout);   #define endl                      n          int main(int argc, char* argv[]) {       FastIO;   #ifndef ONLINE_JUDGE       hardio( input );   #endif          vector<int> a(26);       a[0] = 10010; a[1] = 11020; a[2] = 20011; a[3] = 21012;       a[4] = 11011; a[5] = 21021; a[6] = 22022; a[7] = 12021;       a[8] = 11011; a[9] = 12012; a[10] = 10120; a[11] = 11130;       a[12] = 20121; a[13] = 21122; a[14] = 11121; a[15] = 21131;       a[16] = 22132; a[17] = 12131; a[18] = 11121; a[19] = 12122;       a[20] = 10221; a[21] = 11231; a[22] = 12113; a[23] = 20222;       a[24] = 21223; a[25] = 11222;          int n; cin >> n;       string s =   ;       FOR(i, 0, n, 1) {           int cur = 0;           FOR(j, 0, 5, 1) {               int x; cin >> x;               cur = cur * 10 + x;           }           FOR(j, 0, 26, 1) if (cur == a[j]) { s += char(j +  a ); break; }       }          cout << s << endl;          return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long long P = 1e9 + 7;   const int N = 2e5 + 5;   const int inf = 0x3f3f3f3f;   const double pi = acos(-1);   mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());   int n, x, y, a[2];   vector<int> g[N];   void dfs(int x, int fa, int grp) {     a[grp]++;     for (auto u : g[x])       if (u != fa) dfs(u, x, 1 - grp);   }   int main() {     scanf( %d , &n);     for (int i = (1); i <= (n - 1); ++i) {       scanf( %d%d , &x, &y);       g[x].push_back(y), g[y].push_back(x);     }     dfs(1, 0, 0);     cout << min(a[0], a[1]) - 1 << endl;     return 0;   }


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


#include <bits/stdc++.h>   using namespace std;   string s;   int main() {     cin >> s;     for (int i = 1; i < s.size(); i++) {       if (s[i] == s[i - 1])         for (char j =  a ; j <=  z ; j++)           if (s[i - 1] != j && s[i + 1] != j) {             s[i] = j;             break;           }     }     cout << s << endl;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n, m;     cin >> n >> m;     int t[n];     for (int i = 0; i < n; i++) {       cin >> t[i];     }     if (n == 1) {       cout << m - t[0];       return 0;     }     int ans = 0;     for (int k = 0; k < n * n * n; k++) {       set<int> st;       for (int i = 0; i < n; i++) {         if (st.count(t[i]) == 0 && t[i] < m) {           st.insert(t[i]);           t[i]++;         }       }       if (st.size()) {         ans++;       }     }     cout << ans;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   struct node {     int cnt;     bool leaf;     int nx[26];     node() {       leaf = false;       cnt = 0;       memset(nx, -1, sizeof nx);     }   };   string s, d;   node t[300100];   int ans, sz;   vector<string> v;   void add(string s) {     int cur = 0;     for (int i = 0; i < s.size(); i++) {       if (t[cur].nx[s[i] -  a ] != -1)         cur = t[cur].nx[s[i] -  a ];       else {         t[cur].nx[s[i] -  a ] = ++sz;         cur = sz;       }       t[cur].cnt++;     }     t[cur].leaf = true;   }   bool check(string s) {     int cur = 0;     for (int i = 0; i < s.size(); i++) {       if (t[cur].nx[s[i] -  a ] != -1)         cur = t[cur].nx[s[i] -  a ];       else         return false;     }     if (t[cur].leaf) return true;     return false;   }   int calc(string s) {     int d = -1, cur = 0, mn = -1;     for (int i = 0; i < s.size(); i++) {       if (t[cur].nx[s[i] -  a ] != -1)         cur = t[cur].nx[s[i] -  a ];       else         break;       d = i + 1;     }     if (d == -1) return s.size();     for (int i = 0; i < 26; i++)       if (t[cur].nx[i] != -1) return s.size();     cur = 0;     for (int i = 0; i < s.size(); i++) {       if (t[cur].nx[s[i] -  a ] != -1)         cur = t[cur].nx[s[i] -  a ];       else         break;       if (t[cur].cnt == 1) {         mn = i + 1;         break;       }     }     if (mn == -1) return s.size();     return min(s.size(), mn + 1 + s.size() - d);   }   int main() {     while (getline(cin, d)) {       s += d;       s +=  # ;       ans++;       for (int i = 0; i < d.size(); i++)         if (!isalpha(d[i])) ans++;     }     for (int i = 0; i < s.size();) {       if (!isalpha(s[i]))         i++;       else {         int j = i + 1;         while (true) {           if (!isalpha(s[j])) break;           j++;         }         j--;         v.push_back(s.substr(i, j - i + 1));         i = j + 1;       }     }     for (auto a : v) {       ans += calc(a);       if (!check(a)) add(a);     }     cout << ans;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   using ll = long long;   using pii = pair<int, int>;   using vi = vector<int>;   int n, m, p, nm;   vector<char> g;   vector<bool> vis;   vector<int> s, cnt;   vector<queue<int>> ex;   queue<int> q;   bool isEmpty(int v) {     if (v < 0) return false;     int i = v / m, j = v % m;     if (i < 0 || i >= n) return false;     if (j < 0 || j >= m) return false;     return (g[v] ==  . );   }   int dist(int u, int v) {     int x1 = u / m, y1 = u % m;     int x2 = v / m, y2 = v % m;     return abs(x1 - x2) + abs(y1 - y2);   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     cout.tie(NULL);     cin >> n >> m >> p;     nm = n * m;     g.resize(nm);     s.resize(p);     cnt.resize(p);     ex.resize(p);     for (int& e : s) cin >> e;     for (int i = 0; i < n; i++) {       for (int j = 0; j < m; j++) {         int v = i * m + j;         char c;         cin >> c;         g[v] = c;         if (c !=  .  && c !=  # ) {           int pl = c -  1 ;           cnt[pl]++;           ex[pl].push(v);         }       }     }     bool found = true;     while (found) {       found = false;       for (int cur = 0; cur < p; cur++) {         int dis = 0;         while (dis != s[cur] && !ex[cur].empty()) {           queue<int> tmp;           while (!ex[cur].empty()) {             int u = ex[cur].front();             ex[cur].pop();             for (int mul : {-m, -1, m, 1}) {               int to = u + mul;               if (to / m != u / m && to % m != u % m) continue;               if (!isEmpty(to)) continue;               g[to] = cur +  1 ;               cnt[cur]++;               found = true;               tmp.push(to);             }           }           ex[cur] = tmp;           dis++;         }       }     }     for (int i : cnt) cout << i <<    ;     cout << endl;   }


#include <bits/stdc++.h>   using namespace std;   using ll = long long;   const int N = 3005;   const double PI = acos(-1);   struct Point {     ll x, y;   };   inline Point operator+(const Point &a, const Point &b) {     return {a.x + b.x, a.y + b.y};   }   inline Point operator-(const Point &a, const Point &b) {     return {a.x - b.x, a.y - b.y};   }   inline ll cross(const Point &a, const Point &b) {     return a.x * b.y - a.y * b.x;   }   inline double polar(const Point &a) { return atan2(a.y, a.x); }   int n, m, r[N];   Point a[N], b[N];   void solve(vector<int> va, vector<pair<int, int>> vb) {     while (1) {       if (va.size() == 1) {         for (auto &i : vb) cout << i.first <<     << va[0] << endl;         return;       }       if (vb.size() == 1) {         for (auto &i : va) cout << vb[0].first <<     << i << endl;         return;       }       for (int i = 0; i < int(vb.size()); i++)         if (vb[i].second > vb[0].second) swap(vb[i], vb[0]);       vector<pair<double, pair<int, int>>> tmp((va.size() + vb.size()) * 2);       int idx = 0;       for (int i = 0; i < int(va.size()); i++)         tmp[++idx] = {polar(a[va[i]] - b[vb[0].first]), {va[i], 0}};       for (int i = 1; i < int(vb.size()); i++)         tmp[++idx] = {polar(b[vb[i].first] - b[vb[0].first]), vb[i]};       sort(tmp.begin() + 1, tmp.begin() + idx + 1);       for (int i = 1; i <= idx; i++)         tmp[idx + i] = tmp[i], tmp[idx + i].first += 2 * PI;       for (int i = 1, j = 0, sum = 0; i <= idx;            sum -= 1 - tmp[i++].second.second) {         if (sum >= 1 && sum <= vb[0].second - 1) {           vector<int> xa;           vector<pair<int, int>> xb;           xb.push_back({vb[0].first, sum});           vb[0].second -= sum;           vb.resize(1);           va.clear();           for (int k = i; k <= j; k++)             if (!tmp[k].second.second)               xa.push_back(tmp[k].second.first);             else               xb.push_back(tmp[k].second);           for (int k = j + 1; k <= i + idx - 1; k++)             if (!tmp[k].second.second)               va.push_back(tmp[k].second.first);             else               vb.push_back(tmp[k].second);           if (xa.size() + xb.size() > va.size() + vb.size())             swap(xa, va), swap(xb, vb);           solve(xa, xb);           break;         }         while (tmp[j + 1].first - tmp[i].first <= PI)           sum += 1 - tmp[++j].second.second;         if (sum >= 1 && sum <= vb[0].second - 1) {           vector<int> xa;           vector<pair<int, int>> xb;           xb.push_back({vb[0].first, sum});           vb[0].second -= sum;           vb.resize(1);           va.clear();           for (int k = i; k <= j; k++)             if (!tmp[k].second.second)               xa.push_back(tmp[k].second.first);             else               xb.push_back(tmp[k].second);           for (int k = j + 1; k <= i + idx - 1; k++)             if (!tmp[k].second.second)               va.push_back(tmp[k].second.first);             else               vb.push_back(tmp[k].second);           if (xa.size() + xb.size() > va.size() + vb.size())             swap(xa, va), swap(xb, vb);           solve(xa, xb);           break;         }       }     }   }   int main() {     ios::sync_with_stdio(false);     int T;     cin >> T;     while (T--) {       int n, m;       cin >> n >> m;       vector<int> va;       vector<pair<int, int>> vb;       for (int i = 1; i <= m; i++) cin >> r[i];       for (int i = 1; i <= n; i++) cin >> a[i].x >> a[i].y, va.push_back(i);       for (int i = 1; i <= m; i++)         cin >> b[i].x >> b[i].y, vb.push_back({i, r[i]});       cout <<  YES  << endl;       solve(va, vb);     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int maxm = 1e5 + 5;   const int maxn = 1e5 + 5;   const int inf = 0x3f3f3f3f;   int n, m;   int a[maxn];   struct edge {     int u, v, w, next;   } e[maxm * 2];   int head[maxn];   bool vis[maxn];   int cnt;   priority_queue<int, vector<int>, greater<int> > q;   void addedge(int u, int v, int w) {     e[cnt].u = u;     e[cnt].v = v;     e[cnt].w = w;     e[cnt].next = head[u];     head[u] = cnt++;   }   void add_edge(int u, int v, int w) {     addedge(u, v, w);     addedge(v, u, w);   }   void bfs() {     vis[1] = 1;     q.push(1);     int temp = 1;     while (!q.empty()) {       int u = q.top();       a[temp++] = u;       q.pop();       for (int i = head[u]; ~i; i = e[i].next) {         int v = e[i].v;         if (!vis[v]) {           q.push(v);           vis[v] = 1;         }       }     }   }   int main() {     cnt = 0;     memset(vis, 0, sizeof(vis));     memset(head, -1, sizeof(head));     cin >> n >> m;     int u, v;     for (int i = 1; i <= m; i++) {       cin >> u >> v;       add_edge(u, v, 1);     }     bfs();     for (int i = 1; i <= n; i++) {       printf( %d%c , a[i], i == n ?   n  :    );     }   }


#include <bits/stdc++.h>   #ifndef ONLINE_JUDGE   #define debug(x) cout << #x <<  :   << (x) << endl   #else   #define debug(x)   #endif   using namespace std;   typedef long long ll;   typedef vector<int> vi;   const int maxn=1e6+7,inf=0x3f3f3f3f,mod=1e9+7;      string s[4];   int n;   int cnt1[4],cnt0[4];      int match(int i,int j)   {       if(cnt0[i]>=n && cnt0[j]>=n && cnt0[i]>=cnt0[j])       {           vi b;           b.push_back(-1);           int cc=0;           for(int k=0;k<2*n;++k) if(s[j][k]== 0  && cc<n) b.push_back(k),cc++;           b.push_back(2*n);           cc=0;           for(int k=0;k<2*n;++k)           {               if(s[i][k]== 0  && cc<n)               {                   for(int x=b[cc]+1;x<b[cc+1];++x) cout<<s[j][x];                   cc++;               }               cout<<s[i][k];           }           for(int x=b[cc]+1;x<b[cc+1];++x) cout<<s[j][x];           cout<<  n ;           return true;       }       else if(cnt1[i]>=n && cnt1[j]>=n && cnt1[i]>=cnt1[j])       {           vi b;           b.push_back(-1);           int cc=0;           for(int k=0;k<2*n;++k) if(s[j][k]== 1  && cc<n) b.push_back(k),cc++;           b.push_back(2*n);           cc=0;           for(int k=0;k<2*n;++k)           {               if(s[i][k]== 1  && cc<n)               {                   for(int x=b[cc]+1;x<b[cc+1];++x) cout<<s[j][x];                   cc++;               }               cout<<s[i][k];           }           for(int x=b[cc]+1;x<b[cc+1];++x) cout<<s[j][x];           cout<<  n ;           return true;       }       return false;   }      int main()   {       ios::sync_with_stdio(false);       cin.tie(0);       int kase;       cin>>kase;       while(kase--)       {           cin>>n;           s[0].clear(),s[1].clear(),s[2].clear();           cin>>s[0]>>s[1]>>s[2];           for(int j=0;j<3;++j) cnt1[j]=0,cnt0[j]=0;           for(int j=0;j<3;++j) for(int i=0;i<2*n;++i) cnt1[j]+=(s[j][i]== 1 ),cnt0[j]+=(s[j][i]== 0 );           bool ok=0;           for(int i=0;i<3;++i)           {               for(int j=0;j<3;++j)               {                   if(i==j) continue;                   if(match(i,j))                   {                       ok=1;                       break;                   }               }               if(ok) break;           }           assert(ok);       }       return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     string n;     int count = 0;     cin >> n;     stack<char> s;     for (int i = 0; i < n.length(); i++) {       if (n[i] ==  ( ) {         s.push(n[i]);       } else if (n[i] ==  ) ) {         if (s.size() != 0) {           char tops = s.top();           s.pop();           if (tops ==  ( ) {             count += 2;           }         }       }     }     cout << count << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   struct nodo {     int v, c, i, d;     nodo() {}     nodo(int V, int C, int I, int D) { v = V, c = C, i = I, d = D; }   };   int ans[200055], I[200055];   int n, m;   void solve(vector<vector<nodo> > v) {     int d[m + 5];     memset(d, -1, sizeof(d));     queue<int> Q;     Q.push(0);     while (!Q.empty()) {       int u = Q.front();       Q.pop();       for (int i = 0; i < v[u].size(); i++) {         int id = v[u][i].i;         if (d[id] == -1) {           d[id] = v[u][i].d;           int go = v[u][i].v;           I[go] -= v[u][i].c;           if (go != n - 1 && I[go] == 0) {             Q.push(go);           }         }       }     }     for (int i = 0; i < m; i++) ans[i] = d[i];   }   int main() {     scanf( %d%d , &n, &m);     vector<vector<nodo> > v(n + 5);     int a, b, c;     for (int i = 0; i < m; i++) {       scanf( %d%d%d , &a, &b, &c);       a--;       b--;       v[a].push_back(nodo(b, c, i, 0));       v[b].push_back(nodo(a, c, i, 1));       I[a] += c;       I[b] += c;     }     for (int i = 0; i < n; i++) I[i] /= 2;     solve(v);     for (int i = 0; i < m; i++) printf( %d n , ans[i]);   }


#include <bits/stdc++.h>   using namespace std;   const long long maxn = 2e5 + 9;   vector<long long> g[maxn];   long long n, m, a, b;   bool used[maxn];   pair<long long, long long> dfs(long long v, long long u) {     used[v] = 1;     pair<long long, long long> ans(1, v == u);     for (long long to : g[v]) {       if (!used[to]) {         pair<long long, long long> cur = dfs(to, u);         ans.first += cur.first;         ans.second |= cur.second;       }     }     return ans;   }   void solve() {     cin >> n >> m >> a >> b;     a--;     b--;     for (long long i = 0; i < m; i++) {       long long x, y;       cin >> x >> y;       x--;       y--;       g[x].emplace_back(y);       g[y].emplace_back(x);     }     long long sz1 = 0, sz2 = 0;     for (long long i = 0; i < n; i++) used[i] = 0;     used[a] = 1;     for (long long to : g[a]) {       if (!used[to]) {         pair<long long, long long> cur = dfs(to, b);         if (!cur.second) sz1 += cur.first;       }     }     for (long long i = 0; i < n; i++) used[i] = 0;     used[b] = 1;     for (long long to : g[b]) {       if (!used[to]) {         pair<long long, long long> cur = dfs(to, a);         if (!cur.second) sz2 += cur.first;       }     }     for (long long i = 0; i < n; i++) {       g[i].resize(0);       g[i].shrink_to_fit();     }     cout << sz1 * sz2 <<   n ;   }   signed main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     long long t;     cin >> t;     for (long long j = 0; j < t; j++) solve();   }


#include <bits/stdc++.h>   using namespace std;   struct point {     int x, id;   };   struct node {     int l, r, t;     inline void in() { scanf( %d%d , &l, &r), --r; }     inline bool operator<(const node &a) const { return l < a.l; }   } a[500005];   int n, K;   long long sta[2][500005];   int f[500005], shu[500005], pos[500005];   set<node> cc;   vector<point> buc[500005];   inline void T_add(int x, int y) {     long long num = 1ll * x * y;     for (; x <= n; x += x & -x) sta[0][x] += y, sta[1][x] += num;   }   inline long long query(int opt, int x) {     long long sum = 0;     for (; x; x -= x & -x) sum += sta[opt][x];     return sum;   }   inline int calc_(int x) {     memset(shu, 0, sizeof(shu));     int ans = 0, cnt = 0, head = 1;     for (int i = 1; i <= n; ++i) {       ans += f[i];       int size = buc[i].size();       for (int j = 1; j <= size; ++j) {         point t = buc[i][j - 1];         if (t.id < head)           ans += t.x;         else           shu[t.id] += t.x;       }       while (head <= i && ans >= x) {         ans += shu[head];         ans -= a[head].r - a[head].l + 1;         ++head;       }       cnt += head - 1;       pos[i] = head - 1;       if (cnt > K) return K + 1;     }     return cnt;   }   int main() {     scanf( %d%d , &n, &K);     for (int i = 1; i <= n; ++i) a[i].in();     for (int i = 1; i <= n; ++i) {       f[i] = a[i].r - a[i].l + 1;       while (cc.size()) {         set<node>::iterator it;         it = cc.upper_bound((node){a[i].l - 1, 0, 0});         if (it == cc.end()) break;         if (it->l > a[i].r) break;         f[i] -= min(it->r, a[i].r) - it->l + 1;         buc[i].push_back((point){min(it->r, a[i].r) - it->l + 1, it->t});         int l = a[i].r + 1, r = it->r, t = it->t;         cc.erase(it);         if (l <= r) cc.insert((node){l, r, t});       }       if (cc.size() && cc.begin()->l < a[i].l) {         set<node>::iterator it;         it = cc.upper_bound((node){a[i].l - 1, 0, 0});         it--;         if (it->r >= a[i].l) {           f[i] -= min(it->r, a[i].r) - a[i].l + 1;           buc[i].push_back((point){min(it->r, a[i].r) - a[i].l + 1, it->t});           int l = it->l, r = it->r, t = it->t;           cc.erase(it);           cc.insert((node){l, a[i].l - 1, t});           if (r > a[i].r) cc.insert((node){a[i].r + 1, r, t});         }       }       cc.insert((node){a[i].l, a[i].r, i});     }     int Min, num;     for (int l = 1, r = 1e9, mid, cnt; l <= r;) {       mid = l + r >> 1;       cnt = calc_(mid);       if (cnt > K)         l = mid + 1;       else         Min = mid, r = mid - 1;     }     num = calc_(Min);     long long ans = 0;     for (int i = 1; i <= n; ++i) {       int size = buc[i].size();       for (int j = 1; j <= size; ++j) {         point t = buc[i][j - 1];         T_add(t.id, -t.x);       }       T_add(i, a[i].r - a[i].l + 1);       if (pos[i]) {         ans += query(1, pos[i]);         ans += (query(0, i) - query(0, pos[i])) * pos[i];       }     }     printf( %lld n , ans + 1ll * (K - num) * (Min - 1));   }


#include <bits/stdc++.h>   using namespace std;   int main() {     map<long long, pair<long long, long long> > mpl;     vector<long long> ml;     long long n, q, xi, amyor = 0;     scanf( %lld %lld , &n, &q);     deque<long long> dq;     for (int i = (0); i < int(n); i++) {       scanf( %lld , &xi);       amyor = max(amyor, xi);       dq.push_back(xi);     }     long long cnt = 0;     while (1) {       if (dq.front() == amyor) break;       long long A = dq.front();       dq.pop_front();       long long B = dq.front();       dq.pop_front();       if (B > A) {         dq.push_front(B);         dq.push_back(A);       } else {         dq.push_front(A);         dq.push_back(B);       }       mpl[cnt] = {A, B};       cnt++;     }     dq.pop_front();     while (!dq.empty()) {       ml.push_back(dq.front());       dq.pop_front();     }     while (q--) {       long long m;       scanf( %lld , &m);       if (m <= cnt) {         m--;         long long x, y;         printf( %lld %lld n , mpl[m].first, mpl[m].second);       } else {         long long N = cnt;         long long rango = (m - N);         N = ml.size();         long long D = rango % N;         if (D == 0) D = N;         D--;         m = D;         printf( %lld %lld n , amyor, ml[m]);       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   vector<long long> factors;   int IT_MAX = 1 << 17;   int MOD = 1000000007;   const int INF = 0x3f3f3f3f;   const long long LL_INF = 0x3f3f3f3f3f3f3f3f;   const double PI = acos(-1);   const double ERR = 1e-10;   template <typename T>   int BS(vector<T> &mas, T val) {     int l, r;     l = 0;     r = mas.size() - 1;     while (r - l > 1) {       int mid = l + (r - l) / 2;       if (mas[mid] < val) {         l = mid;       } else         r = mid;     }     if (mas[r] == val) return r;     if (mas[l] == val) return l;     return -1;   }   template <typename T>   int UBS(vector<T> &mas, T val) {     int l, r;     l = 0;     r = mas.size() - 1;     while (r - l > 1) {       int mid = l + (r - l) / 2;       if (mas[mid] < val) {         l = mid;       } else         r = mid;     }     if (mas[l] >= val) return l;     if (mas[r] >= val) return r;     return -1;   }   long long mod(long long a, long long m) {     return (a + (abs(a) / m) * m + m) % m;   }   struct elem {     long long col5;     long long col2;     long long m5 = LL_INF;     long long m2 = LL_INF;     char from5;     char from2;   };   vector<vector<elem>> matr52;   vector<vector<long long>> matr;   long long count2(long long n) {     long long res = 0;     if (n == 0) return 0;     while (n % 2 == 0) {       ++res;       n /= 2;     }     return res;   }   long long count5(long long n) {     long long res = 0;     if (n == 0) return 0;     while (n % 5 == 0) {       ++res;       n /= 5;     }     return res;   }   long long dp2(long long i, long long j) {     if (matr52[i][j].m2 == LL_INF) {       if (i && j) {         long long t1 = dp2(i - 1, j) + matr52[i][j].col2;         long long t2 = dp2(i, j - 1) + matr52[i][j].col2;         if (t1 < t2) {           matr52[i][j].m2 = t1;           matr52[i][j].from2 =  D ;         } else {           matr52[i][j].m2 = t2;           matr52[i][j].from2 =  R ;         }       } else if (i) {         matr52[i][j].m2 = dp2(i - 1, j) + matr52[i][j].col2;         matr52[i][j].from2 =  D ;       } else if (j) {         matr52[i][j].m2 = dp2(i, j - 1) + matr52[i][j].col2;         matr52[i][j].from2 =  R ;       } else {         return matr52[0][0].m2;       }     }     return matr52[i][j].m2;   }   long long dp5(long long i, long long j) {     if (matr52[i][j].m5 == LL_INF) {       if (i && j) {         long long t1 = dp5(i - 1, j) + matr52[i][j].col5;         long long t2 = dp5(i, j - 1) + matr52[i][j].col5;         if (t1 < t2) {           matr52[i][j].m5 = t1;           matr52[i][j].from5 =  D ;         } else {           matr52[i][j].m5 = t2;           matr52[i][j].from5 =  R ;         }       } else if (i) {         matr52[i][j].m5 = dp5(i - 1, j) + matr52[i][j].col5;         matr52[i][j].from5 =  D ;       } else if (j) {         matr52[i][j].m5 = dp5(i, j - 1) + matr52[i][j].col5;         matr52[i][j].from5 =  R ;       } else {         return matr52[0][0].m5;       }     }     return matr52[i][j].m5;   }   int main() {     ios_base::sync_with_stdio(false);     cout.tie(0);     cin.tie(0);     long long n;     cin >> n;     matr.assign(n, vector<long long>(n));     matr52.assign(n, vector<elem>(n));     bool checkz = false;     long long zi, zj;     for (int i = 0; i < n; ++i) {       for (int j = 0; j < n; ++j) {         cin >> matr[i][j];         if (matr[i][j] == 0) {           checkz = true;           zi = i;           zj = j;         }         matr52[i][j].col2 = count2(matr[i][j]);         matr52[i][j].col5 = count5(matr[i][j]);       }     }     matr52[0][0].m2 = matr52[0][0].col2;     matr52[0][0].m5 = matr52[0][0].col5;     long long t1 = dp2(n - 1, n - 1);     long long t2 = dp5(n - 1, n - 1);     if (t1 && t2 && checkz) {       cout << 1 <<   n ;       for (int i = 0; i < zj; ++i) {         cout <<  R ;       }       for (int i = 0; i < n - 1; ++i) {         cout <<  D ;       }       for (int i = zj; i < n - 1; ++i) {         cout <<  R ;       }       return 0;     }     if (t1 < t2) {       cout << t1 <<   n ;       vector<char> ans;       int i, j;       i = n - 1;       j = n - 1;       while (i || j) {         ans.push_back(matr52[i][j].from2);         if (matr52[i][j].from2 ==  D )           --i;         else           --j;       }       reverse((ans).begin(), (ans).end());       for (auto i : ans) cout << i;     } else {       cout << t2 <<   n ;       vector<char> ans;       int i, j;       i = n - 1;       j = n - 1;       while (i || j) {         ans.push_back(matr52[i][j].from5);         if (matr52[i][j].from5 ==  D )           --i;         else           --j;       }       reverse((ans).begin(), (ans).end());       for (auto i : ans) cout << i;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n;   vector<vector<int> > g;   vector<bool> used;   vector<bool> cyc;   vector<int> cl;   vector<int> p;   int cycle_st, cycle_end;   bool dfs(int v, int parent) {     cl[v] = 1;     for (size_t i = 0; i < g[v].size(); ++i) {       int to = g[v][i];       if (to == parent) continue;       if (cl[to] == 0) {         p[to] = v;         if (dfs(to, v)) return true;       } else if (cl[to] == 1) {         cycle_end = v;         cycle_st = to;         return true;       }     }     cl[v] = 2;     return false;   }   bool dfs2(int k, int step) {     if (cyc[k]) {       cout << step <<    ;       return true;     }     used[k] = true;     for (vector<int>::iterator i = g[k].begin(); i != g[k].end(); ++i) {       if (!used[*i]) {         if (dfs2(*i, step + 1)) return true;       }     }     return false;   }   int main(int argc, const char* argv[]) {     cin >> n;     vector<int> temp;     g.assign(n, temp);     cyc.assign(n, false);     for (int i = 0; i < n; i++) {       int k1, k2;       cin >> k1 >> k2;       g[k1 - 1].push_back(k2 - 1);       g[k2 - 1].push_back(k1 - 1);     }     p.assign(n, -1);     cl.assign(n, 0);     cycle_st = -1;     for (int i = 0; i < n; ++i)       if (dfs(i, i)) break;     cyc[cycle_st] = true;     for (int v = cycle_end; v != cycle_st; v = p[v]) {       cyc[v] = true;     }     for (int i = 0; i < n; i++) {       used.assign(n, false);       dfs2(i, 0);     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n = 0, m = 0, z = 0, res = 0;     cin >> n >> m >> z;     for (int i = 1; i <= z; i++)       if (i % n == 0 && i % m == 0) res++;     cout << res;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   string itosm(long long x) {     if (x == 0) return  0 ;     string ans =   ;     while (x > 0) {       ans += ((x % 10) +  0 );       x /= 10;     }     reverse(ans.begin(), ans.end());     return ans;   }   long long stoim(string str) {     long long ans = 0;     long long k = 1;     int p = 0;     if (str[0] ==  - ) p++;     for (int i = str.length() - 1; i >= p; i--) {       ans += (str[i] -  0 ) * k;       k *= 10;     }     return ans;   }   const long long infll = 1e18 + 3;   const int inf = 1009000999;   const long double eps = 1e-7;   const int maxn = 1e6 + 1146;   const int baseint = 1000200013;   const long long basell = 1e18 + 3;   const long double PI = acos(-1.0);   const long long mod = 1e9 + 7;   long long a[maxn];   long long t[maxn];   long long f[maxn];   long long n, r, k;   bool check(long long x) {     for (int i = 0; i < n; i++) f[i] = x - a[i], t[i] = 0;     long long sum = 0, ans = 0;     for (int i = 0; i < n; i++) {       if (f[i] - sum > 0) {         long long d = f[i] - sum;         sum += d;         ans += d;         t[2 * r + i] += d;         if (ans > k) return 0;       }       sum -= t[i];     }     return 1;   }   void solve() {     cin >> n >> r >> k;     for (int i = 0; i < n; i++) cin >> a[i];     long long sum = 0;     for (int i = 0; i < n; i++) {       if (i - r - 1 >= 0) sum -= a[i - r - 1];       t[i] = sum;       sum += a[i];     }     sum = 0;     for (int i = n - 1; i >= 0; i--) {       sum -= a[i + r + 1];       sum += a[i];       t[i] += sum;     }     for (int i = 0; i < n; i++) a[i] = t[i];     long long l = 0, r = 1.2e18, mid;     while (l < r) {       mid = (l + r + 1) / 2;       if (check(mid))         l = mid;       else         r = mid - 1;     }     cout << l;   }   int main() {     ios_base::sync_with_stdio(0);     ;     cin.tie(0);     cout.tie(0);     solve();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     int n, i, j, ll, l;     cin >> n;     string a[n], s;     for (i = 0; i < n; i++) {       cin >> a[i];     }     sort(a, a + n,          [&](const string &a, const string &t) { return a.size() < t.size(); });     for (i = 0; i < n - 1; i++) {       if (a[i + 1].find(a[i]) == string::npos) {         cout <<  NO n ;         return 0;       }     }     cout <<  YES n ;     for (i = 0; i < n; i++) cout << a[i] <<   n ;     return 0;   }


#include <bits/stdc++.h>   #pragma GCC optimize( Ofast )   #pragma GCC optimize( unroll-loops )   #pragma GCC target( sse2 )   using namespace std;   long long gcd(long long a, long long b) {     if (b == 0) return a;     return gcd(b, a % b);   }   int32_t main() {     ios::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     ;     long long n;     cin >> n;     vector<long long> cnt(n + 1, 0);     bool ch = 1;     for (long long i = 0; i < n - 1; i++) {       long long a, b;       cin >> a >> b;       if (a == n && b == n) ch = 0;       cnt[a]++;       cnt[b]++;     }     if (cnt[n] != n - 1) ch = 0;     vector<vector<long long> > g(n + 1);     vector<long long> v1;     vector<long long> v2;     for (long long i = n - 1; i >= 1; i--) {       if (cnt[i])         v1.push_back(i);       else         v2.push_back(i);     }     long long s2 = v2.size();     long long j = 0;     for (long long i = 0; i < v1.size(); i++) {       long long d = cnt[v1[i]] - 1;       long long p = n;       while (d && j < s2) {         g[p].push_back(v2[j]);         p = v2[j];         if (p > v1[i]) ch = 0;         j++;         d--;       }       if (d) ch = 0;       g[p].push_back(v1[i]);     }     if (j != s2) ch = 0;     if (ch) {       cout <<  YES             <<   n ;       for (long long i = 1; i <= n; i++) {         for (auto it : g[i]) {           cout << i <<     << it <<   n ;         }       }     } else       cout <<  NO ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   char str[25][510];   int g[25][25];   int main() {     int n, m, i, j, l1, l2, fuck, temp, k;     scanf( %d%d , &n, &m);     for (i = 0; i < n; i++) scanf( %s , str[i]);     for (i = 0; i < n; i++) {       for (j = i + 1; j < n; j++) {         temp = 0;         l1 = strlen(str[i]);         l2 = strlen(str[j]);         for (k = 0; k < l1 && k < l2; k++)           if (str[i][k] != str[j][k]) break;         k = min(k, l1);         k = min(k, l2);         g[i][j] = k;       }     }     int tot = (1 << n), ans = 0;     for (i = 0; i < tot; i++) {       fuck = 0;       for (j = 0; j < n; j++) {         if ((i & (1 << j))) fuck++;       }       if (fuck != m) continue;       temp = 0;       for (j = 0; j < n; j++) {         if ((i & (1 << j)) == 0) continue;         for (k = j + 1; k < n; k++) {           if ((i & (1 << k)) == 0) continue;           temp += g[j][k];         }       }       ans = max(ans, temp);     }     printf( %d n , ans);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int A[10010] = {0};   int D;   int N, M, P;   int g;   int fact[20010] = {0};   int fp = 0;   int Hash[20010] = {0};   int place[10010] = {0};   int F[20010] = {0};   long long power(long long a, long long k, long long Mod) {     long long o = 1;     for (; k > 0; k >>= 1) {       if (k & 1) o = o * a % Mod;       a = a * a % Mod;     }     return o;   }   int gcd(int a, int b) { return a % b == 0 ? b : gcd(b, a % b); }   int getplace(int k) {     for (int l = 1, r = fp; l <= r;) {       int mid = (l + r) >> 1;       if (fact[mid] == k) return mid;       if (fact[mid] > k)         r = mid - 1;       else         l = mid + 1;     }     return 0;   }   int main() {     cin >> N >> M >> P;     for (int i = 1; i * i <= P - 1; i++) {       if ((P - 1) % i != 0) continue;       fact[++fp] = i;       if (i * i != (P - 1)) fact[++fp] = (P - 1) / i;     }     sort(fact + 1, fact + fp + 1);     for (int i = 1; i <= N; i++) {       scanf( %d , &A[i]);       A[i] %= P;       for (int j = 1; j <= fp; j++) {         if (power(A[i], fact[j], P) == 1) {           A[i] = (P - 1) / fact[j];           break;         }       }     }     D = P - 1;     for (int i = 1; i <= M; i++) {       int B;       scanf( %d , &B);       D = gcd(D, B);     }     P = (P - 1) / D;     fp = 0;     for (int i = 1; i * i <= P; i++) {       if (P % i != 0) continue;       fact[++fp] = i;       if (i * i != P) fact[++fp] = P / i;     }     sort(fact + 1, fact + fp + 1);     int Ans = 0;     for (int i = 1; i <= N; i++) {       A[i] = gcd(A[i], P);       int place = getplace(A[i]);       if (Hash[place] == 0) Ans += P / fact[place];       F[place] = 1;       Hash[place] = 1;     }     for (int i = fp; i >= 1; i--) {       for (int j = i + 1; j <= fp; j++) {         if (fact[j] % fact[i] == 0) F[j] += F[i];       }     }     for (int i = 1; i <= fp; i++) {       if (Hash[i] == 1) {         int Need = -(F[i] - 1);         Ans += Need * (P / fact[i]);         for (int j = i + 1; j <= fp; j++)           if (fact[j] % fact[i] == 0) {             Hash[j] = 1;             F[j] += Need;           }       }     }     cout << Ans << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int t, n, a[200005], p[200005];   int main() {     cin >> t;     while (t--) {       cin >> n;       for (int i = 1; i <= n; i++) {         cin >> a[i];       }       sort(a + 1, a + n + 1);       for (int i = 1; i <= n; i++) {         p[i] = i - 1;       }       int sum, cnt, x, ans = 0;       for (int i = 1; i <= n; i++) {         sum = a[i], cnt = 1, x = i;         while (cnt < 3 && x >= 1) {           if (a[i] % a[x]) cnt++, sum += a[x];           while (p[x] && a[i] % a[p[x]] == 0) p[x] = p[p[x]];           x = p[x];         }         ans = max(ans, sum);       }       cout << ans << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int maxn = 2e5 + 5;   int n, m;   int h[maxn], in[maxn], q[maxn], sg[maxn], sum[maxn], vis[maxn];   vector<int> G[maxn];   int main() {     scanf( %d%d , &n, &m);     for (int i = 1; i <= n; ++i) scanf( %d , &h[i]);     for (int i = 1; i <= m; ++i) {       int u, v;       scanf( %d%d , &u, &v);       G[u].push_back(v);       ++in[v];     }     int l = 1, r = 0;     for (int i = 1; i <= n; ++i)       if (!in[i]) q[++r] = i;     while (l <= r) {       int u = q[l++];       for (int i = 0; i < G[u].size(); ++i) {         int v = G[u][i];         if (!--in[v]) q[++r] = v;       }     }     for (int i = n; i; --i) {       int u = q[i];       for (int j = 0; j < G[u].size(); ++j) {         int v = G[u][j];         vis[sg[v]] = 1;       }       for (; vis[sg[u]]; ++sg[u])         ;       for (int j = 0; j < G[u].size(); ++j) {         int v = G[u][j];         vis[sg[v]] = 0;       }     }     for (int i = 1; i <= n; ++i) sum[sg[i]] ^= h[i];     int p = -1;     for (int i = n; ~i; --i)       if (sum[i]) {         p = i;         break;       }     if (p == -1) {       puts( LOSE );       return 0;     }     puts( WIN );     for (int i = 1; i <= n; ++i)       if (sg[i] == p) {         if ((sum[p] ^ h[i]) > h[i]) continue;         h[i] ^= sum[p];         for (int j = 0; j < G[i].size(); ++j) {           int v = G[i][j];           h[v] ^= sum[sg[v]];           sum[sg[v]] = 0;         }         break;       }     for (int i = 1; i <= n; ++i) printf( %d  , h[i]);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n, v[10004];   vector<array<int, 3> > ans;   void go(int x, int y, int z) {     v[x] -= x * z;     v[y] += x * z;     ans.push_back({x, y, z});   }   int main() {     int t;     cin >> t;     while (t--) {       ans.clear();       int sum = 0;       cin >> n;       for (int i = 1; i <= n; ++i) {         cin >> v[i];         sum += v[i];       }       if (sum % n) {         cout << -1 <<   n ;         continue;       }       for (int i = 2; i <= n; ++i) {         if (v[i] % i) {           go(1, i, i - v[i] % i);         }         go(i, 1, v[i] / i);       }       for (int i = 2; i <= n; ++i) {         go(1, i, sum / n);       }       assert((int)ans.size() <= 3 * n);       cout << ans.size() <<   n ;       for (auto &[x, y, z] : ans) {         cout << x <<     << y <<     << z <<   n ;       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   struct Envelope {     int s, t, d, w;   };   const int NMAX = 100010, MMAX = 210, KMAX = 100010;   const long long LLINF = 0x7fffffffffffffffLL;   int n, m, k;   Envelope red[KMAX];   vector<int> v[NMAX];   int red_t[NMAX];   long long f[NMAX][MMAX];   bool cmp(int a, int b) {     if (red[a].w != red[b].w) return red[a].w > red[b].w;     if (red[a].d != red[b].d) return red[a].d > red[b].d;     return a < b;   }   set<int, decltype(cmp) *> s(cmp);   int main() {     scanf( %d%d%d , &n, &m, &k);     for (int i = 1; i <= k; i++) {       scanf( %d%d%d%d , &red[i].s, &red[i].t, &red[i].d, &red[i].w);       v[red[i].s].push_back(i);       v[red[i].t + 1].push_back(-i);     }     for (int i = 1; i <= n; i++) {       for (int j = 0; j < v[i].size(); j++) {         if (v[i][j] < 0) {           s.erase(-v[i][j]);         } else {           s.insert(v[i][j]);         }       }       auto iter = s.begin();       red_t[i] = -1;       if (iter != s.end()) red_t[i] = *iter;     }     for (int i = 1; i <= n + 1; i++)       for (int j = 0; j <= m; j++) f[i][j] = LLINF;     f[1][0] = 0;     for (int i = 1; i <= n; i++) {       for (int j = 0; j <= m; j++) {         if (f[i][j] == LLINF) continue;         int cur = red_t[i];         long long &trans1 = f[red[cur].d + 1][j];         long long &trans2 = f[i + 1][j + 1];         long long &trans3 = f[i + 1][j];         if (cur == -1) {           trans3 = min(trans3, f[i][j]);           continue;         }         trans1 = min(trans1, f[i][j] + red[cur].w);         if (j != m) trans2 = min(trans2, f[i][j]);       }     }     long long ans = LLINF;     for (int i = 0; i <= m; i++) ans = min(ans, f[n + 1][i]);     cout << ans << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   vector<int> v[300005];   int n, m, s;   int edgeck[300005], Etype[300005];   pair<int, int> E[300005];   int vis[300005];   void bfs() {     memset(vis, 0, sizeof(vis));     queue<int> que;     que.push(s);     while (!que.empty()) {       int now = que.front();       que.pop();       vis[now] = 1;       for (int i = 0; i < v[now].size(); i++) {         int Enum = v[now][i];         int nxt = -1;         if (edgeck[Enum] == -1) {           nxt = E[Enum].second;           if (nxt != now) {             edgeck[Enum] = 0;           } else {             nxt = E[Enum].first;             edgeck[Enum] = 1;           }           if (!vis[nxt]) {             vis[nxt] = 1;             que.push(nxt);           }         } else {           nxt = (edgeck[Enum] == 0 ? E[Enum].second : E[Enum].first);           if (vis[nxt]) continue;           vis[nxt] = 1;           que.push(nxt);         }       }     }     for (int i = 0; i < m; i++) {       if (edgeck[i] == -1) edgeck[i] = 0;     }     int cnt = 0;     for (int i = 1; i <= n; i++) {       if (vis[i]) ++cnt;     }     printf( %d n , cnt);     for (int i = 0; i < m; i++) {       if (Etype[i] != 2) continue;       if (edgeck[i] == 0)         printf( + );       else         printf( - );     }     puts(  );   }   void bfs2() {     memset(vis, 0, sizeof(vis));     queue<int> que;     que.push(s);     while (!que.empty()) {       int now = que.front();       que.pop();       vis[now] = 1;       for (int i = 0; i < v[now].size(); i++) {         int Enum = v[now][i];         int nxt = -1;         if (edgeck[Enum] == -1) {           nxt = E[Enum].second;           if (nxt != now) {             edgeck[Enum] = 1;           } else {             nxt = E[Enum].first;             edgeck[Enum] = 0;           }         } else {           nxt = (edgeck[Enum] == 0 ? E[Enum].second : E[Enum].first);           if (vis[nxt]) continue;           vis[nxt] = 1;           que.push(nxt);         }       }     }     for (int i = 0; i < m; i++) {       if (edgeck[i] == -1) edgeck[i] = 0;     }     int cnt = 0;     for (int i = 1; i <= n; i++) {       if (vis[i]) ++cnt;     }     printf( %d n , cnt);     for (int i = 0; i < m; i++) {       if (Etype[i] != 2) continue;       if (edgeck[i] == 0)         printf( + );       else         printf( - );     }     puts(  );   }   int main() {     memset(edgeck, -1, sizeof(edgeck));     scanf( %d %d %d , &n, &m, &s);     for (int ty, a, b, i = 0; i < m; i++) {       scanf( %d %d %d , &ty, &a, &b);       v[a].push_back(i);       v[b].push_back(i);       if (ty == 1) {         edgeck[i] = 0;       } else {         Etype[i] = 2;       }       E[i] = {a, b};     }     bfs();     for (int i = 0; i < m; i++) {       if (Etype[i] != 2) continue;       edgeck[i] = -1;     }     bfs2();   }


#include<bits/stdc++.h>   using namespace std;      #define INF 1234567890   #define ll long long      int T;   int N, K;   int A[200201];      int main()   {    //ios::sync_with_stdio(0); cin.tie(0);    scanf( %d , &T);    while(T--)    {     scanf( %d %d , &N, &K);     for(int i=1; i<=N; i++)     {      if (i <= K) A[i] = i;      else A[i] = K-(i-K);     }     // for(int i=1; i<=N; i++)     //  printf( %d  , A[i]);     // printf(  n );        int cnt = A[N]-1; // prefix     for(int i=1; i<=cnt; i++)      printf( %d  , i);     for(int i=cnt+1; i<=K; i++)      printf( %d  , cnt+1+K-i);     printf(  n );    }    return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int inf = 1e9 + 5;   const long long linf = 1e18 + 5;   const int N = 3000 + 5;   int n, x, y, all, up[N], down[N], w[N];   vector<pair<int, int> > v[N];   void dfs(int root, int x, int w, int up, int down) {     ::up[x] = up;     ::down[x] = down;     ::w[x] = w;     for (__typeof(v[x].begin()) it = v[x].begin(); it != v[x].end(); it++)       if (it->first != root) {         all += it->second;         dfs(x, it->first, w, up + it->second, down + !it->second);       }   }   int solve(int x) {     all = 0;     w[x] = 0;     up[x] = 0;     down[x] = 0;     for (__typeof(v[x].begin()) it = v[x].begin(); it != v[x].end(); it++) {       all += it->second;       dfs(x, it->first, it->first, it->second, !it->second);     }     int ans = all, mni = -1;     for (int i = 1; i <= n; i++) {       if (all - up[i] + down[i] < ans) {         ans = all - up[i] + down[i];         mni = w[i];       }     }     int ans2 = ans;     for (int i = 1; i <= n; i++) {       if (w[i] != mni and ans - up[i] + down[i] < ans2) {         ans2 = ans - up[i] + down[i];       }     }     return ans2;   }   int main() {     ios ::sync_with_stdio(0);     cin >> n;     for (int i = 1; i <= n - 1; i++) {       cin >> x >> y;       v[x].push_back(make_pair(y, 0));       v[y].push_back(make_pair(x, 1));     }     int ans = inf;     for (int i = 1; i <= n; i++) ans = min(ans, solve(i));     cout << ans <<   n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n, temp;     cin >> n;     vector<int> num;     while (n != 0) {       cin >> temp;       num.push_back(temp);       n--;     }     sort(num.begin(), num.end());     int smallest = 1;     for (int i = 0; i < num.size(); i++) {       if (smallest != num[i]) {         break;       }       smallest++;     }     cout << smallest;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int a[300001];   int m, n, res = 1, x;   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     cout.tie(NULL);     cin >> n;     m = n;     printf( 1  );     for (int i = 1; i <= n; ++i) {       cin >> x;       a[x] = 1;       res++;       while (a[m] == 1 && m > 0) {         res--;         m--;       }       printf( %d  , res);     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main(int argc, char const *argv[]) {     int n, a, b, ans = 0;     cin >> n >> a >> b;     if (b >= 0) {       if (b >= n) {         ans = b % n;         ans += a;       } else         ans = a + b;       if (ans > n) ans %= n;     } else if (n == -b) {       ans = a;     } else {       if (b == -1) {         a += b;         if (a == 0)           ans = n;         else           ans = a;       } else {         int k = 0;         while (k != b) {           a--;           k--;           if (a == 0) a = n;         }         if (a < 0)           ans = -a;         else           ans = a;       }     }     cout << ans <<   n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int cycles;   vector<vector<int> > g;   vector<int> vis;   void dfs(int u) {     vis[u] = 1;     for (auto v : g[u]) {       if (vis[v]) {         if (vis[v] == 1) cycles++;         continue;       }       dfs(v);     }     vis[u] = 2;   }   long long gcd(long long a, long long b) {     long long x = max(a, b);     long long y = min(a, b);     if (y == 0) return x;     if (x % y == 0) return y;     return gcd(y, x % y);   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     int t;     cin >> t;     while (t--) {       string s;       cin >> s;       int l = s.length();       int d = s[0] -  0 ;       int ans = (d - 1) * (4 + 3 + 2 + 1);       for (int i = 1; i <= l; i++) ans += i;       cout << ans <<   n ;     }   }


#include <bits/stdc++.h>   using namespace std;   long long N = 1e5 + 10;   vector<vector<long long> > v1(N, vector<long long>(2));   vector<vector<long long> > v;   long long dfs(long long s, long long n, vector<long long>& level) {     long long val = 0;     for (auto u : v[s]) {       if (level[u] == 0) {         val = max(val, dfs(u, n, level));       } else if (level[u]) {         val = max(val, level[u]);       }     }     level[s] = val + 1;     return level[s];   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     cout.tie(NULL);     long long n, m;     cin >> n >> m;     for (long long i = 1; i <= m; i++) {       cin >> v1[i][0] >> v1[i][1];     }     long long low = 1, high = m, ans = -1;     while (low <= high) {       long long mid = (low + high) / 2;       v.clear();       v.resize(n + 1);       vector<long long> par(n + 1, -1), level(n + 1);       for (long long i = 1; i <= mid; i++) {         v[v1[i][0]].push_back(v1[i][1]);         par[v1[i][1]] = v1[i][0];       }       for (long long i = 1; i <= n; i++) {         if (par[i] == -1) {           if (dfs(i, n, level) == n) {             ans = mid;             high = mid - 1;           } else {             low = mid + 1;           }           break;         }       }     }     cout << ans;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 1e5 + 7;   int n, m, k, s, t;   vector<int> g[N];   int dis[N];   bool spc[N];   bool ok(int lim) {     priority_queue<pair<int, int> > stk;     fill_n(dis, n, INT_MAX);     dis[s] = 0;     stk.push({-dis[s], s});     while (!stk.empty()) {       int d, u;       tie(d, u) = stk.top();       stk.pop();       if (d != -dis[u] || -d >= lim) continue;       for (auto v : g[u]) {         if (dis[v] > dis[u] + 1) {           dis[v] = dis[u] + 1;           if (spc[v]) dis[v] = 0;           stk.push({-dis[v], v});         }       }     }     return dis[t] < INT_MAX;   }   int main() {     scanf( %d%d%d , &n, &m, &k);     for (int i = (0); i < (k); ++i) {       int p;       scanf( %d , &p);       p--;       spc[p] = true;     }     for (int i = (0); i < (m); ++i) {       int a, b;       scanf( %d%d , &a, &b);       a--;       b--;       g[a].push_back(b);       g[b].push_back(a);     }     scanf( %d%d , &s, &t);     s--;     t--;     int ans = -1;     for (int l = 1, r = n, mid = l + r >> 1; l <= r; mid = l + r >> 1)       ok(mid) ? r = (ans = mid) - 1 : l = mid + 1;     printf( %d , ans);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     ios::sync_with_stdio(false);     cin.tie(NULL);     cout.tie(NULL);     string s;     cin >> s;     int a[7];     for (int i = 0; i < 6; i++) {       a[i] = s[i] -  0 ;     }     int ans = 3;     for (int i = 0; i <= 9; i++) {       for (int j = 0; j <= 9; j++) {         for (int k = 0; k <= 9; k++) {           for (int x = 0; x <= 9; x++) {             for (int y = 0; y <= 9; y++) {               for (int z = 0; z <= 9; z++) {                 if (i + j + k == x + y + z) {                   int res = 0;                   if (i != a[0]) res++;                   if (j != a[1]) res++;                   if (k != a[2]) res++;                   if (x != a[3]) res++;                   if (y != a[4]) res++;                   if (z != a[5]) res++;                   ans = min(ans, res);                 }               }             }           }         }       }     }     for (int i = 0; i <= 9; i++) {       for (int j = 0; j <= 9; j++) {         for (int k = 0; k <= 9; k++) {           if (i + j + k == a[3] + a[4] + a[5]) {             int res = 0;             if (i != a[0]) res++;             if (j != a[1]) res++;             if (k != a[2]) res++;             ans = min(ans, res);           }         }       }     }     cout << ans <<   n ;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 100 + 10;   int a[N];   int main() {     int n;     scanf( %d , &n);     for (int i = 0; i < n + n; i++) scanf( %d , &a[i]);     sort(a, a + n + n);     if (a[n] > a[n - 1])       puts( YES );     else       puts( NO );     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     ios::sync_with_stdio(false);     cin.tie(0);     int n, k;     cin >> n >> k;     vector<vector<int>> g(n);     for (int i = 0; i < n - 1; ++i) {       int x, y;       cin >> x >> y;       --x;       --y;       g[x].push_back(y);       g[y].push_back(x);     }     int ans = 1;     function<int(int, int)> dfs = [&](int v, int pv) {       if ((int)g[v].size() == 1) {         return 0;       }       vector<int> d;       for (int to : g[v]) {         if (to == pv) {           continue;         }         d.push_back(1 + dfs(to, v));       }       sort(d.begin(), d.end());       while ((int)d.size() >= 2) {         if (d.back() + d[(int)d.size() - 2] <= k) {           break;         }         ++ans;         d.pop_back();       }       return d.back();     };     for (int i = 0; i < n; ++i) {       if ((int)g[i].size() > 1) {         dfs(i, -1);         break;       }     }     cout << ans <<   n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int inf = 1e09 + 5e3;   const long long linf = 2e18 + 5e3;   const int mxn = 5e2 + 5;   void solve() {     int n;     cin >> n;     vector<int> a(n), b(n);     vector<pair<int, int> > as, bs;     for (int i = 0; i < n; i++) {       cin >> a[i];     }     for (int i = 0; i < n; i++) {       cin >> b[i];     }     long long out = 1;     if (n & 1) out = a[n / 2] == b[n / 2];     for (int i = 0, j = n - 1; i < n / 2; i++, j--) {       as.emplace_back(min(a[i], a[j]), max(a[i], a[j]));       bs.emplace_back(min(b[i], b[j]), max(b[i], b[j]));     }     sort((as).begin(), (as).end());     sort((bs).begin(), (bs).end());     cout << (out && as == bs ?  Yes  :  No ) <<   n ;   }   int main() {     ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);     int t;     cin >> t;     while (t--) {       solve();     }   }


#include <bits/stdc++.h>   using namespace std;   long long n;   long long a[100005];   long long preA[100005];   int main() {     cin >> n;     for (long long i = 0; i < n; ++i) {       cin >> a[i];     }     preA[0] = 0;     for (long long i = 1; i < n; ++i) {       preA[i] = preA[i - 1] + a[i];     }     set<long long> nos;     map<long long, long long> freq;     for (long long i = 0; i < n; ++i) {       if (nos.find(preA[i]) != nos.end()) {         freq[preA[i]]++;       } else {         nos.insert(preA[i]);         freq[preA[i]] = 1;       }     }     long long maxFreq = -1;     for (set<long long>::iterator it = nos.begin(); it != nos.end(); ++it) {       if (maxFreq < freq[*it]) {         maxFreq = freq[*it];       }     }     cout << n - maxFreq;   }