#include <bits/stdc++.h>   using namespace std;   const int N = 1e5;   const int B = 350;   int a[N];   int b[N];   int tot = 0;   mt19937 rng(4112003);   struct Node {     Node *l, *r;     Node *p;     int key, pri;     int nCh, cnt[B];     Node(int x = 0) : key(x), pri(rng() << 16 | rng()), nCh(1) {       l = r = 0;       for (int i = 0; i < tot; ++i) cnt[i] = 0;       if (b[x] >= 0) cnt[b[x]] = 1;     };   } * p[N];   int Siz(Node *x) {     if (x)       return x->nCh;     else       return 0;   }   void pull(Node *x) {     Node *u = x->l;     Node *v = x->r;     x->nCh = 1;     x->nCh += Siz(u);     x->nCh += Siz(v);     for (int i = 0; i < tot; ++i) {       x->cnt[i] = 0;       if (u) x->cnt[i] += u->cnt[i];       if (v) x->cnt[i] += v->cnt[i];     }     if (b[x->key] >= 0) x->cnt[b[x->key]]++;     if (u) u->p = x;     if (v) v->p = x;   }   Node *join(Node *x, Node *y) {     if (!x) return y;     if (!y) return x;     if (x->pri > y->pri) {       x->r = join(x->r, y);       pull(x);       return x;     } else {       y->l = join(x, y->l);       pull(y);       return y;     }   }   void split(Node *x, Node *&l, Node *&r, int p) {     if (!x) {       l = r = 0;       return;     }     if (p >= Siz(x->l) + 1) {       p -= Siz(x->l) + 1;       split(x->r, x->r, r, p);       pull(l = x);     } else       split(x->l, l, x->l, p), pull(r = x);   }   vector<int> pos[N];   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     srand(time(NULL));     int n;     cin >> n;     for (int i = 0; i < n; ++i) {       int x;       cin >> x;       --x;       pos[x].emplace_back(i);       a[i] = x;     }     for (int i = 0; i < n; ++i) {       if (pos[i].size() > B)         b[i] = tot++;       else         b[i] = -1;     }     Node *R = 0;     for (int i = 0; i < n; ++i) R = join(R, p[i] = new Node(a[i]));     int q;     cin >> q;     int lst = 0;     for (int i = 0; i < q; ++i) {       int t;       cin >> t;       int l;       cin >> l;       l = (l + lst - 1) % n;       int r;       cin >> r;       r = (r + lst - 1) % n;       if (l > r) swap(l, r);       if (t == 1) {         Node *x, *y, *z;         split(R, R, x, l);         split(x, x, y, r - l + 1);         split(x, x, z, r - l);         x = join(z, x);         x = join(x, y);         R = join(R, x);       }       if (t == 2) {         int x;         cin >> x;         x = (x + lst - 1) % n;         lst = 0;         if (b[x] >= 0) {           Node *p, *q;           split(R, R, p, l);           split(p, p, q, r - l + 1);           lst = p->cnt[b[x]];           p = join(p, q);           R = join(R, p);         } else {           for (int a : pos[x]) {             Node *T = p[a];             int cur = Siz(T->l);             while (T != R) {               if (T->p->r == T) cur += Siz(T->p->l) + 1;               T = T->p;             }             if (cur < l) continue;             if (cur > r) continue;             lst++;           }         }         cout << lst <<   n ;       }     }   }


#include <bits/stdc++.h>   using namespace std;   template <class T>   inline bool checkMax(T &a, const T &b) {     return a < b ? a = b, 1 : 0;   }   template <typename T, typename... Args>   inline void checkMax(T &a, const Args... arg) {     checkMax(a, max(arg...));   }   template <class T>   inline bool checkMin(T &a, const T &b) {     return a > b ? a = b, 1 : 0;   }   template <typename T, typename... Args>   inline void checkMin(T &a, const Args... arg) {     checkMin(a, min(arg...));   }   const int INF = 0x3f3f3f3f;   const long long llINF = 0x3f3f3f3f3f3f3f3f;   const int MAXN = 2e5 + 5;   int n, m, K, cnt, tot;   int a[MAXN];   long long num[MAXN];   struct Rect {     int x100, y100, x2, y2;   } rec[MAXN];   struct Line {     int x, y100, y2, op;   } line[MAXN];   struct SegmentTree {     static const int MAXM = MAXN << 2;     struct Tree {       int l, r;       int cov, sum;     } tree[MAXM];     void update(int k) {       if (tree[k].cov > 0)         tree[k].sum = a[tree[k].r] - a[tree[k].l - 1];       else         tree[k].sum = tree[k << 1].sum + tree[k << 1 | 1].sum;     }     void build(int k, int l, int r) {       tree[k].l = l;       tree[k].r = r;       tree[k].cov = tree[k].sum = 0;       if (l == r) return;       int mid = (l + r) >> 1;       build(k << 1, l, mid);       build(k << 1 | 1, mid + 1, r);     }     void modify(int k, int l, int r, int v) {       if (tree[k].l >= l && tree[k].r <= r) {         tree[k].cov += v;         update(k);         return;       }       int mid = (tree[k].l + tree[k].r) >> 1;       if (l <= mid) modify(k << 1, l, r, v);       if (r > mid) modify(k << 1 | 1, l, r, v);       update(k);     }     int query() { return tree[1].sum; }   } T;   long long Solve(int t) {     tot = cnt = 0;     for (int i = 1; i <= m; i++) {       int x100 = (rec[i].x100 - 1) / t + 1, x2 = rec[i].x2 / t;       int y100 = (rec[i].y100 - 1) / t + 1, y2 = rec[i].y2 / t;       if (x100 > x2 || y100 > y2) continue;       a[++cnt] = y100 - 1, a[++cnt] = y2;       line[++tot] = {x100 - 1, y100, y2, 1};       line[++tot] = {x2, y100, y2, -1};     }     if (!tot) return 0;     sort(a + 1, a + 1 + cnt);     cnt = unique(a + 1, a + 1 + cnt) - a - 1;     T.build(1, 1, cnt);     sort(line + 1, line + 1 + tot,          [](const Line &x, const Line &y) { return x.x < y.x; });     long long ans = 0;     for (int i = 1; i <= tot; i++) {       ans += (long long)(line[i].x - line[i - 1].x) * T.query();       int y100 = lower_bound(a + 1, a + 1 + cnt, line[i].y100) - a;       int y2 = lower_bound(a + 1, a + 1 + cnt, line[i].y2) - a;       T.modify(1, y100, y2, line[i].op);     }     return ans;   }   int main() {     scanf( %d%d%d , &n, &m, &K);     for (int i = 1; i <= m; i++) {       int x100, y100, x2, y2;       scanf( %d%d%d%d , &x100, &y100, &x2, &y2);       rec[i] = {x100, y100, x2, y2};     }     int w = 0;     while ((1 << (w + 1)) <= K) w += 1;     for (int i = 0; i <= w; i++) {       num[i] = Solve(1 << i);       if (i && ((num[i] - num[i - 1]) & 1)) {         printf( Hamed );         return 0;       }     }     if (num[w] & 1)       printf( Hamed n );     else       printf( Malek n );     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int N, Q;   string s;   struct Info {     int len;     int l_len, r_len;     char left, right;     int ans;     void Output() {       printf( len=%d l_len=%d r_len=%d left=  %c   right=  %c   ans=%d n , len,              l_len, r_len, left, right, ans);     }   };   class TreeNode {    public:     TreeNode *lch, *rch;     int l, r;     bool tag;     Info front, back;     TreeNode(int l, int r) : l(l), r(r) { tag = false; }     void TurnOver() {       tag = !tag;       swap(front, back);     }     void PushDown() {       if (tag) {         tag = false;         lch->TurnOver();         rch->TurnOver();       }     }     void Update() {       front = Merge(lch->front, rch->front);       back = Merge(lch->back, rch->back);     }     void Output() {       printf( l: %d  r: %d  tag: %s n , l, r, tag ?  true  :  false );       printf( front:  );       front.Output();       printf( back:  );       back.Output();     }     static Info Merge(const Info& a, const Info& b) {       Info now;       now.len = a.len + b.len;       now.left = a.left;       now.right = b.right;       now.ans = max(a.ans, b.ans);       if ((a.right ==  <  && b.left ==  < ) ||           (a.right ==  >  && b.left ==  > ) ||           (a.right ==  >  && b.left ==  < )) {         if (a.r_len == a.len) {           now.l_len = a.len + b.l_len;         } else {           now.l_len = a.l_len;         }         if (b.l_len == b.len) {           now.r_len = b.len + a.r_len;         } else {           now.r_len = b.r_len;         }         now.ans = max(now.ans, a.r_len + b.l_len);       } else {         now.l_len = a.l_len;         now.r_len = b.r_len;       }       return now;     }   };   TreeNode* BuildTree(const string& s, int l, int r) {     auto v = new TreeNode(l, r);     if (l == r) {       v->front.len = 1;       v->front.l_len = v->front.r_len = 1;       v->front.left = v->front.right =  < ;       v->front.ans = 1;       v->back.len = 1;       v->back.l_len = v->back.r_len = 1;       v->back.left = v->back.right =  > ;       v->back.ans = 1;       if (s[l] ==  > ) swap(v->front, v->back);       return v;     }     int mid = (l + r) / 2;     v->lch = BuildTree(s, l, mid);     v->rch = BuildTree(s, mid + 1, r);     v->Update();     return v;   }   void Change(TreeNode* v, int ww, int ee, vector<TreeNode*>& visit) {     if (ww <= v->l && v->r <= ee) {       v->TurnOver();       visit.push_back(v);       return;     }     v->PushDown();     int mid = (v->l + v->r) / 2;     if (ww <= mid) Change(v->lch, ww, ee, visit);     if (ee > mid) Change(v->rch, ww, ee, visit);     v->Update();   }   int main() {     cin >> N >> Q;     cin >> s;     auto root = BuildTree(s, 0, N - 1);     vector<TreeNode*> visit;     for (int i = 0; i < Q; i++) {       int a, b;       cin >> a >> b;       a--, b--;       visit.clear();       Change(root, a, b, visit);       Info info = visit[0]->front;       for (int i = 1; i < visit.size(); i++) {         info = TreeNode::Merge(info, visit[i]->front);       }       cout << info.ans << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n, m;     cin >> n >> m;     int min2 = n, max1 = 1;     while (m--) {       int u, v;       cin >> u >> v;       if (v > u) {         v = u ^ v;         u = v ^ u;         v = u ^ v;       }       min2 = min(u, min2);       max1 = max(v, max1);     }     cout << max(0, min2 - max1);   }


#include <bits/stdc++.h>   using namespace std;   int main() {     string s;     static int x[27];     int n, k;     cin >> n >> k;     cin >> s;     for (int i = 0; i < k; i++) {       char r;       cin >> r;       x[r -  a ] = 1;     }     long long res = 0, tr = 0;     for (int i = 0; s[i]; i++) {       if (x[s[i] -  a ])         tr++;       else {         res += (tr * (tr + 1)) / 2;         tr = 0;       }     }     res += (tr * (tr + 1)) / 2;     cout << res;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   vector<int> parent;   vector<int> Rank;   int root(int v) {     if (parent[v] == v) return v;     return parent[v] = root(parent[v]);   }   void DSU(int a, int b) {     a = root(a);     b = root(b);     if (a != b) {       if (Rank[b] > Rank[a]) swap(a, b);       Rank[a] += Rank[b];       parent[b] = a;     }   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     cout.tie(NULL);     int n, m, q;     cin >> n >> m >> q;     parent.resize(n + m + 3);     for (int i = 0; i < parent.size(); i++) {       parent[i] = i;     }     Rank.resize(n + m + 3, 1);     for (int i = 0; i < q; i++) {       int a, b;       cin >> a >> b;       b += n;       DSU(a, b);     }     set<int> roots;     for (int i = 1; i <= n + m; i++) {       roots.insert(root(i));     }     cout << roots.size() - 1 << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   vector<pair<int, int> > Sh;   const int tmax = 155;   char table[tmax][tmax];   pair<int, int> mow[tmax];   pair<int, int> ccount(int row, int side, int ai) {     if (mow[row].second == 0 && mow[row + 1].second == 0) return make_pair(0, ai);     if (mow[row].second == 0) {       if (side == 0 && ai < mow[row + 1].second)         return make_pair(mow[row + 1].second - ai, mow[row + 1].second);       else if (side == 1 && ai >= mow[row + 1].first)         return make_pair(ai - mow[row + 1].first, mow[row + 1].first);       else         return make_pair(0, ai);     }     if (mow[row + 1].second == 0) {       if (side == 0)         return make_pair(mow[row].second - ai, mow[row].second);       else         return make_pair(ai - mow[row].first, mow[row].first);     }     if (side == 0)       return make_pair(max(mow[row + 1].second, mow[row].second) - ai,                        max(mow[row + 1].second, mow[row].second));     else       return make_pair(ai - min(mow[row + 1].first, mow[row].first),                        min(mow[row + 1].first, mow[row].first));   }   int main() {     int N, M;     scanf( %d %d , &N, &M);     for (int i = 0; i < tmax; i++) mow[i].first = 1 << 30, mow[i].second = 0;     int last = 0;     for (int i = 1; i <= N; i++) {       for (int j = 1; j <= M; j++) {         scanf(  %c , &table[i][j]);         if (table[i][j] ==  W ) {           mow[i].first = min(j, mow[i].first);           mow[i].second = max(j, mow[i].second);           last = max(last, i);         }       }     }     int side = 0, howmany = 0;     int ai = 1;     for (int i = 1; i < N; i++) {       pair<int, int> ret = ccount(i, side, ai);       howmany += ret.first;       ai = ret.second;       side ^= 1;     }     if (mow[N].second) {       if (side)         howmany += ai - mow[N].first;       else         howmany += mow[N].second - ai;     }     printf( %d n , max(howmany + last - 1, 0));     return 0;   }


#include <bits/stdc++.h>   using namespace std;   ofstream outfile;   ifstream infile;   int main() {     infile.open( input.txt );     outfile.open( output.txt );     long long int a, b;     cin >> a >> b;     if (a > b) {       cout <<  0 n ;       return 0;     } else if (b - a <= 20) {       long long int digit = 1;       for (long long int i = a + 1; i <= b; ++i) {         digit = (digit * i) % 10;       }       cout << digit <<   n ;       return 0;     }     cout <<  0 n ;   }


#include <bits/stdc++.h>   using namespace std;   template <typename T>   T inline SQR(const T &a) {     return a * a;   }   long long MOD = 1000000007LL;   long long mypow(long long a, long long p) {     if (p == 0) return 1LL;     long long ans = mypow(a, p / 2);     ans *= ans;     ans %= MOD;     if (p & 1) return (a * ans) % MOD;     return ans;   }   int n, m, k;   int dp[1005][1005];   int solve(int pLeft, int sd) {     if (pLeft == 0) return 1;     if (sd <= 0) return 0;     int &val = dp[pLeft][sd];     if (val != -1) return val;     long long aux = 0;     for (int i = 1; i < sd - 1; ++i) {       aux += 1ll * (sd - 1 - i) * solve(pLeft - 1, i);     }     return val = aux % MOD;   }   int main() {     ios::sync_with_stdio(false);     cin >> n >> m >> k;     int MAXS = max(n, m) + 1;     for (int i = 0; i < k + 1; ++i)       for (int j = 0; j < MAXS; ++j) dp[i][j] = -1;     cout << (1ll * solve(k, n) * solve(k, m)) % MOD << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int apsz = 26;   const long long o = 1;   const int maxn = 300007;   const int ooi = 1e9 + 7;   const int up = 62;   int n;   long long mask[maxn];   long long val[maxn];   inline int hb(long long x) {     int k = 0;     while ((o << (k + 1)) <= x) k++;     return k;   }   int main() {     ios::sync_with_stdio(false);     cin.tie(0);     cin >> n;     long long sum = 0;     for (int i = (0); i < (n); ++i) cin >> val[i] >> mask[i], sum += val[i];     vector<vector<int> > pos(up);     vector<vector<int> > exist(up);     for (int i = (0); i < (n); ++i) {       pos[hb(mask[i])].push_back(i);       for (int j = (0); j < (up); ++j)         if ((o << j) & mask[i]) exist[j].push_back(i);     }     long long ans = 0;     for (int i = (0); i < (up); ++i) {       if (pos[i].empty()) continue;       long long tmp = 0;       for (auto x : pos[i]) tmp += val[x];       if (tmp == 0) continue;       if (tmp > 0 == sum > 0) {         ans |= (o << (long long)i);         for (auto x : exist[i]) val[x] = -val[x];       }     }     cout << ans << endl;     return 0;   }


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


#include <bits/stdc++.h>   using namespace std;   using ll = long long;   using ld = long double;   using D = double;   using uint = unsigned int;   const int maxn = 100005;   ll t[maxn], others[maxn], me[maxn];   int f[maxn];   vector<int> from[maxn];   int kv[maxn];   ll ans[maxn];   int n, m;   multiset<ll> global;   multiset<ll> cur[maxn];   bool in[maxn], in2[maxn];   void pop(int x) {     if (!in[x]) return;     in[x] = false;     if (cur[x].size() > 0) {       global.erase(global.find(others[x] + *cur[x].begin()));       global.erase(global.find(others[x] + *cur[x].rbegin()));     }   }   void push(int x) {     if (in[x]) return;     in[x] = true;     if (cur[x].size() > 0) {       global.insert(others[x] + *cur[x].begin());       global.insert(others[x] + *cur[x].rbegin());     }   }   void delfrom(int a, int b) {     if (!in2[a]) return;     in2[a] = false;     cur[b].erase(cur[b].find(ans[a]));   }   void addto(int a, int b) {     if (in2[a]) return;     in2[a] = true;     cur[b].insert(ans[a]);   }   int main() {     scanf( %d%d , &n, &m);     for (int i = 0; i < n; i++) scanf( %lld , &t[i]);     for (int i = 0; i < n; i++) {       scanf( %d , &f[i]);       f[i]--;       kv[i]++;       kv[f[i]]++;       from[f[i]].push_back(i);     }     for (int i = 0; i < n; i++) {       others[i] = t[i] / (kv[i] + 1);       me[i] = t[i] - (kv[i] * others[i]);     }     for (int i = 0; i < n; i++) {       ans[i] = me[i];       for (auto z : from[i]) ans[i] += others[z];     }     for (int i = 0; i < n; i++) {       for (auto z : from[i]) {         cur[i].insert(ans[z]);       }       if (cur[i].size() > 0) {         global.insert(others[i] + *cur[i].begin());         global.insert(others[i] + *cur[i].rbegin());       }       in[i] = true;       in2[i] = true;     }     for (int i = 0; i < m; i++) {       int t;       scanf( %d , &t);       if (t == 1) {         int a, b;         scanf( %d%d , &a, &b);         a--, b--;         int was = f[a];         pop(a);         pop(was);         pop(f[was]);         pop(f[f[was]]);         pop(b);         pop(f[b]);         pop(f[f[b]]);         delfrom(a, was);         delfrom(was, f[was]);         delfrom(f[was], f[f[was]]);         delfrom(b, f[b]);         delfrom(f[b], f[f[b]]);         ans[was] -= others[a];         ans[f[was]] -= others[was];         kv[was]--;         others[was] = ::t[was] / (kv[was] + 1);         ans[was] -= me[was];         me[was] = ::t[was] - (kv[was] * others[was]);         ans[was] += me[was];         ans[f[was]] += others[was];         f[a] = b;         ans[b] += others[a];         ans[f[b]] -= others[b];         kv[b]++;         others[b] = ::t[b] / (kv[b] + 1);         ans[b] -= me[b];         me[b] = ::t[b] - (kv[b] * others[b]);         ans[b] += me[b];         ans[f[b]] += others[b];         addto(a, b);         addto(was, f[was]);         addto(f[was], f[f[was]]);         addto(b, f[b]);         addto(f[b], f[f[b]]);         push(a);         push(was);         push(f[was]);         push(f[f[was]]);         push(b);         push(f[b]);         push(f[f[b]]);       } else if (t == 2) {         int x;         scanf( %d , &x);         x--;         printf( %lld n , ans[x] + others[f[x]]);       } else {         printf( %lld %lld n , *global.begin(), *global.rbegin());       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long long INF = 0x3f3f3f3f3f3f3f3fLL;   const int MOD = 1e9 + 7;   int main() {     ios::sync_with_stdio(false);     cin.tie(nullptr);     cout.tie(nullptr);     int n, m;     cin >> n >> m;     vector<vector<int>> e(n + 1);     for (int i = 0; i < m; i++) {       int x, y;       cin >> x >> y;       e[x].push_back(y);       e[y].push_back(x);     }     vector<int> val(n + 1);     for (int i = 1; i <= n; i++) {       cin >> val[i];     }     vector<int> order(n);     iota(order.begin(), order.end(), 1);     sort(order.begin(), order.end(),          [&](int i, int j) { return val[i] < val[j]; });     for (int i = 0; i < n; i++) {       set<int> s;       for (int x : e[order[i]]) {         s.insert(val[x]);       }       int c = 1;       while (s.count(c)) {         c++;       }       if (c != val[order[i]]) {         cout << -1 <<   n ;         return 0;       }     }     for (int i : order) {       cout << i <<    ;     }     cout <<   n ;   }


#include <bits/stdc++.h>   using namespace std;   long long h[100000], p[100000], l, r;   int i, j, n, m;   long long getleft(long long x) {     int l = 0, r = m - 1;     while (r - l > 1) {       int mid = (l + r) / 2;       if (p[mid] > x)         r = mid;       else         l = mid;     }     if (p[l] > x)       return p[l];     else if (p[r] > x)       return p[r];     else       return -1;   }   bool ok(long long t) {     long long x1, x2, r = 0, q;     for (int i = 0; i < n; i++) {       long long x = t;       q = getleft(r);       if (q <= r || q >= h[i])         r = max(r, h[i] + x);       else if (t - (h[i] - q) < 0)         return false;       else {         if ((t - h[i] + q) / 2 > 0)           x2 = h[i] + (t - h[i] + q) / 2;         else           x2 = 0;         x1 = max(h[i], h[i] + (t - 2 * (h[i] - q)));         r = max(r, max(x1, x2));       }     }     if (p[m - 1] > r) return false;     return true;   }   int main() {     cin >> n >> m;     for (i = 0; i < n; i++) cin >> h[i];     for (i = 0; i < m; i++) cin >> p[i];     l = 0;     r = 100000000001;     while (r - l > 1) {       long long mid = (l + r) / 2;       if (ok(mid))         r = mid;       else         l = mid;     }     if (ok(l))       cout << l;     else if (ok(r))       cout << r;   }


#include <bits/stdc++.h>   const int MAXN = 100;   char cake[MAXN][MAXN + 1];   int main() {     int n;     scanf( %d , &n);     for (int row = 0; row < n; ++row) {       scanf( %s , cake[row]);     }     int pairs = 0;     for (int i = 0; i < n; ++i) {       int inRow = 0, inCol = 0;       for (int j = 0; j < n; ++j) {         inRow += cake[i][j] ==  C ;         inCol += cake[j][i] ==  C ;       }       pairs += inRow * (inRow - 1) / 2 + inCol * (inCol - 1) / 2;     }     printf( %d , pairs);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int INF = 10000000;   int main() {     int n;     cin >> n;     string a;     cin >> a;     for (int j = 1; j <= 25; j++)       for (int i = 0; i + 4 * j < a.size(); i++) {         if (a[i] ==  *  && a[i + j] ==  *  && a[i + 2 * j] ==  *  &&             a[i + 3 * j] ==  *  && a[i + 4 * j] ==  * ) {           cout <<  yes ;           return 0;         }       }     cout <<  no ;   }


#include <bits/stdc++.h>   using namespace std;   struct Pair {     int x, position;   } valiue[543211], b[543211];   int n, k, len, N, Partition, L, R;   int QWER[543211], REWQ[543211], otvet[543211], kol, i;   int Lowofbound[543211], Behind[543211], sled[543211], l, r, mn;   vector<pair<pair<int, int>, int> > a[800];   bool cmd(Pair q, Pair w) {     if (q.position < w.position)       return true;     else       return false;   }   bool cmp(Pair q, Pair w) {     if (q.x < w.x)       return true;     else       return false;   }   int main() {     ios_base::sync_with_stdio(false);     cin >> n >> k;     for (i = 1; i <= n; ++i) {       cin >> valiue[i].x;       b[i].position = i;       b[i].x = valiue[i].x;     }     sort(b + 1, b + n + 1, cmp);     len = 0;     for (i = 1; i <= n; ++i) {       if (i == 1 || b[i].x != b[i - 1].x) {         ++len;       }       valiue[i].x = len;       valiue[i].position = b[i].position;     }     sort(valiue + 1, valiue + n + 1, cmd);     N = int(sqrt(n));     for (i = 1; i <= k; ++i) {       cin >> l >> r;       Partition = (l - 1) / N + 1;       a[Partition].push_back(make_pair(make_pair(r, l), i));     }     for (i = 1; i <= n; ++i) {       if (!QWER[valiue[i].x])         QWER[valiue[i].x] = i;       else         Behind[i] = QWER[valiue[i].x], QWER[valiue[i].x] = i;     }     for (i = n; i >= 1; --i) {       if (!REWQ[valiue[i].x])         REWQ[valiue[i].x] = i;       else         sled[i] = REWQ[valiue[i].x], REWQ[valiue[i].x] = i;     }     for (i = 1; i <= n; ++i) Lowofbound[(i - 1) / N + 1] = i;     for (i = 1; i <= 720; ++i)       if (a[i].size() > 0) {         sort(a[i].begin(), a[i].end());         mn = 2000000000;         R = Lowofbound[i];         L = R + 1;         for (int j = 0; j < a[i].size(); ++j) {           l = a[i][j].first.second;           r = a[i][j].first.first;           if (L <= r) {             while (R < r) {               R++;               if (Behind[R] > Lowofbound[i]) mn = min(mn, R - Behind[R]);             }             kol = mn;             for (int c = Lowofbound[i]; c >= l; --c)               if (sled[c] > 0 && sled[c] <= R) kol = min(kol, sled[c] - c);           } else {             kol = mn;             for (int c = l; c <= r; ++c)               if (sled[c] > 0 && sled[c] <= r) kol = min(kol, sled[c] - c);           }           if (kol != 2000000000)             otvet[a[i][j].second] = kol;           else             otvet[a[i][j].second] = -1;         }       }     for (i = 1; i <= k; ++i) cout << otvet[i] << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n;     cin >> n;     string s;     cin >> s;     int minval = 100000000, currsum = 0;     for (int i = 0; i < n; i++) {       if (s[i] ==  ( ) {         currsum++;         minval = min(minval, currsum);       } else {         currsum--;         minval = min(minval, currsum);       }     }     if (minval < -1)       cout <<  NO ;     else if (minval == -1) {       if (!currsum)         cout <<  YES ;       else         cout <<  NO ;     } else {       if (currsum)         cout <<  NO ;       else         cout <<  YES ;     }     return 0;   }


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


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


#include <bits/stdc++.h>   using namespace std;   const int MX = 100005;   template <typename T>   void cmin(T &x, const T &y) {     if (y < x) x = y;   }   template <typename T>   void cmax(T &x, const T &y) {     if (y > x) x = y;   }   template <typename T>   void read(T &x) {     x = 0;     char c = getchar();     bool f = 0;     while (!isdigit(c) && c !=  - ) c = getchar();     if (c ==  - ) f = 1, c = getchar();     while (isdigit(c)) x = x * 10 + c -  0 , c = getchar();     if (f) x = -x;   }   int n, q;   int fa[MX];   int deg[MX];   long long mass[MX];   long long self_son[MX];   long long for_others(int i) { return mass[i] / (deg[i] + 1); }   long long for_self(int i) { return mass[i] - for_others(i) * deg[i]; }   struct SONS {     struct cmp {       bool operator()(int a, int b) const {         if (self_son[a] != self_son[b])           return self_son[a] < self_son[b];         else           return a < b;       }     };     set<int, cmp> s;     void most(vector<int> &ret) {       if (!s.size())         return;       else if (s.size() == 1)         ret.push_back(*s.begin());       else         ret.push_back(*s.begin()), ret.push_back(*s.rbegin());     }   } son[MX];   multiset<long long> global;   void input() {     read(n), read(q);     for (int i = 1; i <= n; i++) read(mass[i]);     for (int i = 1; i <= n; i++) read(fa[i]), deg[i]++, deg[fa[i]]++;   }   long long get_sum(int x) { return self_son[x] + for_others(fa[x]); }   void build() {     for (int i = 1; i <= n; i++) {       self_son[i] += for_self(i);       self_son[fa[i]] += for_others(i);     }     for (int i = 1; i <= n; i++) son[fa[i]].s.insert(i);     for (int i = 1; i <= n; i++) {       vector<int> most;       son[i].most(most);       for (auto j : most) global.insert(get_sum(j));     }   }   void work() {     for (int i = 1; i <= q; i++) {       int op;       read(op);       if (op == 1) {         vector<int> most;         int a, b, c;         read(a), read(c), b = fa[a];         most.clear();         son[b].most(most);         son[c].most(most);         son[fa[b]].most(most);         son[fa[c]].most(most);         son[fa[fa[b]]].most(most);         son[fa[fa[c]]].most(most);         sort(most.begin(), most.end());         most.erase(unique(most.begin(), most.end()), most.end());         for (auto x : most) global.erase(global.find(get_sum(x)));         son[b].s.erase(a);         son[fa[b]].s.erase(b);         son[fa[c]].s.erase(c);         son[fa[fa[b]]].s.erase(fa[b]);         son[fa[fa[c]]].s.erase(fa[c]);         self_son[fa[b]] -= for_others(b);         self_son[fa[c]] -= for_others(c);         self_son[b] -= for_others(a);         self_son[b] -= for_self(b);         self_son[c] -= for_self(c);         deg[b]--;         fa[a] = c;         deg[c]++;         self_son[c] += for_self(c);         self_son[b] += for_self(b);         self_son[c] += for_others(a);         self_son[fa[b]] += for_others(b);         self_son[fa[c]] += for_others(c);         son[fa[fa[b]]].s.insert(fa[b]);         son[fa[fa[c]]].s.insert(fa[c]);         son[fa[b]].s.insert(b);         son[fa[c]].s.insert(c);         son[c].s.insert(a);         most.clear();         son[b].most(most);         son[c].most(most);         son[fa[b]].most(most);         son[fa[c]].most(most);         son[fa[fa[b]]].most(most);         son[fa[fa[c]]].most(most);         sort(most.begin(), most.end());         most.erase(unique(most.begin(), most.end()), most.end());         for (auto x : most) global.insert(get_sum(x));       } else if (op == 2) {         int x;         read(x);         printf( %lld n , get_sum(x));       } else {         printf( %lld %lld n , *global.begin(), *global.rbegin());       }     }   }   int main() {     input();     build();     work();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int maxn = 100005;   int d[maxn], n;   struct node {     int a, b;   } data[maxn];   int main() {     cin >> n;     d[0] = -1;     for (int i = 1; i <= n; i++) cin >> d[i];     sort(d + 1, d + 1 + n);     int cnt = 0;     for (int i = 1; i <= n; i++) {       if (d[i] == d[i - 1])         data[cnt].b++;       else {         cnt++;         data[cnt].a = d[i];         data[cnt].b = 1;       }     }     bool flag = 1;     for (int i = 2; i <= cnt; i++) {       if (data[i].a - 1 != data[i - 1].a) {         flag = 0;         break;       }     }     if (flag) {       data[2].b -= data[1].b - 1;       for (int i = 2; i < cnt; i++) {         if (data[i].b < 2) {           flag = 0;           break;         } else           data[i + 1].b -= data[i].b - 2;       }       if (data[cnt].b != 1) flag = 0;     }     if (flag)       cout <<  YES  << endl;     else       cout <<  NO  << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   using ll = long long;   using ld = long double;   using D = double;   using uint = unsigned int;   template <typename T>   using pair2 = pair<T, T>;   const long double pi = acos((ld)-1.0);   const int maxn = 1 << 16;   complex<double> oddc[maxn], evenc[maxn], tmp[maxn];   complex<double> w[maxn], invw[maxn];   ll odd[maxn], even[maxn];   int oldodd[maxn], oldeven[maxn];   int btr[maxn];   int n, A, mod;   int st2;   int answer;   inline int bitrev(int x) { return btr[x]; }   void fft(complex<double> *a, int n, complex<double> *w, complex<double> *res) {     for (int i = 0; i < n; i++) res[bitrev(i)] = a[i];     for (int len = 2; len <= n; len *= 2) {       int step = n / len;       for (int i = 0; i < n; i += len) {         int curw = 0;         for (int j = 0; j < len / 2; j++) {           complex<double> x = res[i + j];           complex<double> y = res[i + len / 2 + j] * w[curw];           res[i + j] = x + y;           res[i + len / 2 + j] = x - y;           curw += step;         }       }     }   }   void preparefft(int n) {     for (int i = 0; i < n; i++) {       int cur = i;       for (int j = 0; (1 << j) < n; j++) {         btr[i] = (btr[i] << 1) | (cur & 1);         cur >>= 1;       }     }     for (int i = 0; i < n; i++) {       w[i] = complex<double>(cos(2 * pi / n * i), sin(2 * pi / n * i));       invw[i] = w[i];     }     reverse(invw + 1, invw + n);   }   void conv(ll *from, complex<double> *to) {     for (int i = n + 1; i < st2; i++) tmp[i] = 0;     for (int i = 0; i <= n; i++) tmp[i] = from[i];     fft(tmp, st2, w, to);   }   void convback(complex<double> *from, ll *to) {     fft(from, st2, invw, tmp);     for (int i = 0; i < st2; i++) tmp[i] /= st2;     for (int i = 0; i <= n; i++) {       to[i] = (ll)(tmp[i].real() + 0.5);       assert(abs(tmp[i].imag()) < 0.1);     }   }   void addans() {     for (int i = 1; i <= n; i += 2) answer = (answer + odd[i]) % mod;   }   void go(int A) {     if (A == 1) {       even[0] = 1;       odd[0] = 0;       odd[1] = 1;       addans();       return;     }     go(A / 2);     for (int i = 0; i <= n; i++) oldodd[i] = odd[i];     for (int i = 0; i <= n; i++) oldeven[i] = even[i];     conv(even, evenc);     conv(odd, oddc);     for (int i = 0; i < st2; i++) {       if ((A / 2) % 2 == 0) {         tie(oddc[i], evenc[i]) =             make_pair((oddc[i] + evenc[i]) * oddc[i],                       (oddc[i] + evenc[i]) * (evenc[i] - (complex<double>)1));       } else {         tie(evenc[i], oddc[i]) =             make_pair((oddc[i] + evenc[i]) * oddc[i],                       (oddc[i] + evenc[i]) * (evenc[i] - (complex<double>)1));       }     }     convback(oddc, odd);     convback(evenc, even);     for (int i = n + 1; i <= 2 * n; i++) {       odd[i] = 0;       even[i] = 0;     }     for (int i = 0; i <= n; i++) {       odd[i] = (odd[i] + oldodd[i]) % mod;       even[i] = (even[i] + oldeven[i]) % mod;     }     if (A % 2 == 1) {       for (int i = n; i >= 1; i--) {         odd[i] = (odd[i] + odd[i - 1] + even[i - 1]) % mod;       }     }     addans();   }   int main() {     cin >> n >> A >> mod;     if (A == 1) {       cout << 0 << endl;       return 0;     }     st2 = 0;     while ((1 << st2) <= n) st2++;     st2++;     st2 = 1 << st2;     preparefft(st2);     go(A / 2);     cout << answer << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     long long int s, x, y, a, b = 0;     cin >> s >> x;     int f = 1;     if (s < x) f = 0;     if (s == x) b = 2;     y = s - x;     if (y % 2 == 1) f = 0;     if (f) {       y /= 2, a = 1;       while (x > 0 || y > 0) {         if (x % 2 == 1) {           if (y % 2 == 0)             a *= 2;           else             f = 0;         }         x /= 2, y /= 2;       }     }     a *= f;     cout << a - b << endl;   }


#include <bits/stdc++.h>   using namespace std;   using ll = long long;   const int maxn = 1 << 18;   struct node {     int mx;     node() { mx = -1e9; }     node(int val) { mx = val; }   };   node temp;   node merge(node l, node r) {     temp.mx = min(l.mx, r.mx);     return temp;   }   struct segment_tree {     int n;     node t[2 * maxn];     void init(int sz) {       n = sz;       for (int i = 0; i < n; i++) t[i + n] = node();       for (int i = n - 1; i > 0; --i) t[i] = merge(t[i << 1], t[i << 1 | 1]);     }     void modify(int p, const node &value) {       for (t[p += n] = value; p >>= 1;) t[p] = merge(t[p << 1], t[p << 1 | 1]);     }     node query(int l, int r) {       node resl(1 << 30), resr(1 << 30);       for (l += n, r += n; l < r; l >>= 1, r >>= 1) {         if (l & 1) resl = merge(resl, t[l++]);         if (r & 1) resr = merge(t[--r], resr);       }       return merge(resl, resr);     }   };   vector<int> solve(int n, int m, vector<array<int, 2>> co,                     vector<array<int, 4>> qs) {     vector<int> ans(qs.size());     vector<vector<array<int, 4>>> qr(n + 1);     vector<vector<int>> here(n + 1);     for (auto [x, y] : co) here[x].push_back(y);     for (int i = 0; i < qs.size(); i++) {       qr[qs[i][2]].push_back({i, qs[i][1], qs[i][3], qs[i][0]});     }     segment_tree st;     st.init(m + 1);     for (int i = 1; i <= n; i++) {       for (auto j : here[i]) {         st.modify(j, node(i));       }       for (auto [id, yl, yr, xl] : qr[i]) {         ans[id] = st.query(yl, yr + 1).mx >= xl;       }     }     return ans;   }   int main() {     cin.tie(0)->sync_with_stdio(0);     int n, m, k, q;     cin >> n >> m >> k >> q;     vector<array<int, 2>> co(k);     vector<array<int, 4>> qs(q);     for (auto &i : co) cin >> i[0] >> i[1];     for (auto &i : qs) cin >> i[0] >> i[1] >> i[2] >> i[3];     auto ans1 = solve(n, m, co, qs);     for (auto &i : co) swap(i[0], i[1]);     for (auto &i : qs) swap(i[0], i[1]);     for (auto &i : qs) swap(i[2], i[3]);     auto ans2 = solve(m, n, co, qs);     for (int i = 0; i < q; i++)       cout << (ans1[i] || ans2[i] ?  YES  :  NO ) <<   n ;   }


#include <bits/stdc++.h>   using namespace std;   struct edge {     edge(int to, int back, int orient = 0) : to(to), back(back), orient(orient) {}     int to;     int back;     int orient;   };   class Tarjan {    public:     vector<vector<int> > adj;     vector<int> comp;     int nb_cfc;       private:     vector<int> index;     vector<int> lowlink;     stack<int> s;     int id;     void tarjan(int i) {       int j, k;       index[i] = id;       lowlink[i] = id;       ++id;       s.push(i);       for (k = 0; k < (int)adj[i].size(); ++k) {         j = adj[i][k];         if (index[j] == -1) tarjan(j);         if (comp[j] == -1) lowlink[i] = min(lowlink[i], lowlink[j]);       }       if (lowlink[i] == index[i]) {         do {           j = s.top();           s.pop();           comp[j] = nb_cfc;         } while (i != j);         ++nb_cfc;       }     }       public:     void cfc() {       id = 0;       nb_cfc = 0;       comp.clear();       index.clear();       lowlink.clear();       comp.resize(adj.size(), -1);       index.resize(adj.size(), -1);       lowlink.resize(adj.size(), -1);       for (int i = 0; i < (int)adj.size(); ++i) {         if (index[i] == -1) tarjan(i);       }     }   };   void dfs(vector<vector<edge> > &graph, vector<bool> &mark, int x) {     if (mark[x]) return;     mark[x] = true;     for (int i = 0; i < (graph[x].size()); ++i) {       int y = graph[x][i].to;       if (graph[x][i].orient == 0) {         int j = graph[x][i].back;         graph[x][i].orient = 1;         graph[y][j].orient = -1;         dfs(graph, mark, y);       }     }   }   int main(void) {     int n, m;     cin >> n >> m;     vector<vector<edge> > graph(n);     for (int i = 0; i < (m); ++i) {       int x, y;       cin >> x >> y;       --x, --y;       int u = graph[x].size();       int v = graph[y].size();       graph[x].push_back(edge(y, v));       graph[y].push_back(edge(x, u));     }     vector<bool> mark(n, false);     dfs(graph, mark, 0);     Tarjan g;     g.adj.assign(n, vector<int>());     for (int x = 0; x < (n); ++x)       for (int i = 0; i < (graph[x].size()); ++i) {         int y = graph[x][i].to;         if (graph[x][i].orient > 0) g.adj[x].push_back(y);       }     g.cfc();     if (g.nb_cfc > 1)       cout << 0 << endl;     else {       for (int i = 0; i < (n); ++i)         for (int j = 0; j < (graph[i].size()); ++j) {           int x = i, y = graph[i][j].to;           if (y < x) continue;           if (graph[i][j].orient < 0) swap(x, y);           ++x, ++y;           cout << x <<     << y << endl;         }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   inline long long add(long long a, long long b) {     a = (a + b);     if (a >= 1000000007) a -= 1000000007;     return a;   }   inline long long sub(long long a, long long b) {     a = a - b;     if (a < 0) a += 1000000007;     return a;   }   inline long long mul(long long a, long long b) {     a = (a * b) % 1000000007;     return a;   }   const long long N = 1e6 + 5;   vector<long long> arr1(N, 0);   void pre() {     for (long long i = 2; i < N; i++) {       if (arr1[i] == 0) {         for (long long j = i; j < N; j += i) {           if (arr1[j] == 0) arr1[j] = i;         }       }     }   }   void mymain() {     long long n;     cin >> n;     vector<long long> arr(n);     for (long long& i : arr) cin >> i;     long long ans = 1;     vector<long long> dp(n + 1, 1);     for (long long i = 1; i <= n; i++) {       for (long long j = 2; j * i <= n; j++) {         long long k = j * i;         if (arr[k - 1] > arr[i - 1]) {           dp[k] = max(dp[k], dp[i] + 1);         }       }     }     for (long long i : dp) ans = max(i, ans);     cout << ans <<   n ;   }   signed main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     ;     long long t = 1;     cin >> t;     pre();     for (long long tt = 0; tt < t; tt++) {       mymain();     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long long lcm = 840;   long long w, cnt[10];   long long dp[10][lcm * 8 + 5];   int main() {     ios::sync_with_stdio(false);     cin >> w;     for (int i = 1; i <= 8; i++) {       cin >> cnt[i];     }     memset(dp, -1, sizeof(dp));     dp[0][0] = 0;     for (int i = 1; i <= 8; i++) {       for (int j = 0; j < 8 * lcm; j++) {         long long xx = min(lcm / i - 1, cnt[i]);         for (int k = 0; k <= xx; k++) {           if (j - k * i >= 0 && dp[i - 1][j - k * i] != -1)             dp[i][j] = max(dp[i][j], dp[i - 1][j - k * i] +                                          (cnt[i] - k) / (lcm / (long long)i));         }       }     }     long long ans = 0;     for (long long i = 0; i < 8 * lcm; i++) {       if (dp[8][i] == -1 || i > w) continue;       ans = max(ans, i + lcm * min(dp[8][i], (w - i) / lcm));     }     cout << ans;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n;     scanf( %d , &n);     vector<int> v(n + 1);     for (int i = 1; i <= n; ++i) cin >> v[i];     int x, y;     scanf( %d %d , &x, &y);     for (int i = 1; i <= n; ++i) {       int a = 0, b = 0;       for (int j = 1; j <= n; ++j) {         if (j < i)           a += v[j];         else           b += v[j];       }       if (x <= a and a <= y and x <= b and b <= y) return !printf( %d n , i);     }     puts( 0 );   }


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


#include <bits/stdc++.h>   using namespace std;   bool check(long n, long k) {     long dem = 0;     while (n > 0) {       long x = n % 10;       n /= 10;       (x == 4 || x == 7) ? dem++ : dem = dem;     }     if (dem <= k) return 1;     return 0;   }   int main() {     long n, k, dem = 0;     cin >> n >> k;     long a[n];     for (long i = 0; i < n; i++) {       cin >> a[i];       if (check(a[i], k) == 1) dem++;     }     cout << dem;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int m, n, i, j, k;     scanf( %d%d , &n, &m);     vector<int> A;     for (i = 0; i < n; i++) A.push_back(i + 1);     int M = 0;     do {       int s = 0;       for (i = 0; i < n; i++) {         for (j = i + 1; j < n + 1; j++) {           int s1 = 1000;           for (k = i; k < j; k++) s1 = min(s1, A[k]);           s += s1;         }       }       M = max(M, s);     } while (next_permutation(A.begin(), A.end()));     do {       int s = 0;       for (i = 0; i < n; i++) {         for (j = i + 1; j < n + 1; j++) {           int s1 = 1000;           for (k = i; k < j; k++) s1 = min(s1, A[k]);           s += s1;         }       }       if (s == M) {         m--;         if (m == 0) {           for (i = 0; i < n; i++) printf( %d  , A[i]);           return 0;         }       }     } while (next_permutation(A.begin(), A.end()));     printf( %d n , m);   }


#include <bits/stdc++.h>   using namespace std;   int n, num, k;   pair<int, int> a[200005];   bool comp(pair<int, int> a, pair<int, int> b) { return a.second < b.second; }   int main() {     ios::sync_with_stdio(0);     cin.tie(NULL);     int t = 1;     cin >> t;     while (t--) {       cin >> n >> k;       unordered_map<int, int> mp;       int color = 1;       for (int i = 0; i < n; i++) {         cin >> a[i].first;         a[i].second = i;       }       sort(a, a + n);       for (int i = 0; i < n; i++) {         mp[a[i].first]++;         if (mp[a[i].first] > k)           a[i].first = 0;         else {           a[i].first = color;           if (color == k)             color = 1;           else             color++;         }       }       color--;       for (int i = n - 1; i >= 0; i--) {         if (a[i].first == color) {           a[i].first = 0;           color--;         }       }       sort(a, a + n, comp);       for (int i = 0; i < n; i++) cout << a[i].first <<    ;       cout << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int a[1001];   int main() {     int n, t, i, j, k, m;     cin >> t;     while (t--) {       cin >> n >> m;       for (i = 0; i < n; i++) cin >> a[i];       int ans = a[0];       for (i = 1; i < n; i++) {         if (ans + a[i] <= m)           ans += a[i];         else {           ans = m;           break;         }       }       cout << ans << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int32_t main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     long long n, k;     cin >> n >> k;     vector<long long> v(n);     for (long long &i : v) cin >> i;     sort(v.begin(), v.end(), [](long long a, long long b) { return a > b; });     vector<long long> sm(n);     sm[0] = v[0];     for (long long i = 1; i < n; ++i) {       sm[i] = sm[i - 1] + v[i];     }     long long num, cnt = 0;     for (long long i = 0; i < n; ++i) {       long long l = i, r = n;       while (r - l > 1) {         long long m = (l + r) / 2;         if ((m - i) * v[i] - (sm[m] - sm[i]) <= k)           l = m;         else           r = m;       }       if (l - i + 1 >= cnt) {         cnt = l - i + 1;         num = v[i];       }     }     cout << cnt <<     << num;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const double Pi = acos(-1.0);   struct C {     double x, y, r;   } base[3050];   double Ans;   int i, j, k;   long long dp[3050][2][2];   int N, mark[3050], fa[3050];   inline double dist(C A, C B) {     return sqrt((A.x - B.x) * (A.x - B.x) + (A.y - B.y) * (A.y - B.y));   }   inline double get_S(C A) { return Pi * A.r * A.r; }   inline bool check_in(C A, C B) { return A.r + B.r > dist(A, B); }   namespace DP {   struct Edge {     int next, to;   } E[3050 << 1];   int head[3050], cnt;   inline void Add(int u, int v) {     E[++cnt].to = v, E[cnt].next = head[u], head[u] = cnt;     E[++cnt].to = u, E[cnt].next = head[v], head[v] = cnt;   }   void do_dp(int u, int faa) {     long long f[2][2];     memset(f, 0, sizeof(f));     for (int k = head[u]; k; k = E[k].next) {       if (E[k].to == faa) continue;       do_dp(E[k].to, u);       for (int ii = 0; ii <= 1; ++ii)         for (int jj = 0; jj <= 1; ++jj) f[ii][jj] += dp[E[k].to][ii][jj];     }     for (int ii = 0; ii <= 1; ++ii)       for (int jj = 0; jj <= 1; ++jj)         dp[u][ii][jj] =             max(f[ii ^ 1][jj] + (1ll * (ii ? -1 : 1) * base[u].r * base[u].r),                 f[ii][jj ^ 1] + (1ll * (jj ? -1 : 1) * base[u].r * base[u].r));   }   inline bool Comp(C A, C B) { return A.r > B.r; }   void solve1() {     sort(base + 1, base + N + 1, Comp);     for (i = 1; i <= N; ++i)       for (j = i + 1; j <= N; ++j)         if (check_in(base[i], base[j]))           if (!fa[j] || base[fa[j]].r > base[i].r) fa[j] = i;     for (i = 1; i <= N; ++i)       if (fa[i]) Add(i, fa[i]);     for (i = 1; i <= N; ++i)       if (!fa[i]) do_dp(i, 0), Ans += dp[i][0][0];     printf( %.8lf , Ans * Pi);   }   }  // namespace DP   namespace Greedy {   inline bool Comp(C A, C B) { return A.r > B.r; }   void solve2() {     sort(base + 1, base + N + 1, Comp);     for (i = 1; i <= N; ++i)       for (j = i + 1; j <= N; ++j)         if (check_in(base[i], base[j])) ++mark[j];     for (i = 1; i <= N; ++i)       if (!mark[i] || (mark[i] & 1))         Ans += get_S(base[i]);       else         Ans -= get_S(base[i]);     printf( %.8lf , Ans);   }   }  // namespace Greedy   int main() {     cin >> N;     for (i = 1; i <= N; ++i)       scanf( %lf%lf%lf , &base[i].x, &base[i].y, &base[i].r);     if (N >= 500)       DP ::solve1();     else       Greedy ::solve2();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long double eps = 1e-9;   const int inf = (1 << 30) - 1;   const long long inf64 = ((long long)1 << 62) - 1;   const long double pi = 3.1415926535897932384626433832795;   template <class T>   T sqr(T x) {     return x * x;   }   template <class T>   T abs(T x) {     return x < 0 ? -x : x;   }   const int nmax = 15000;   int n, k;   int a[nmax], pos[nmax];   int main() {     cin >> n >> k;     for (int i = 0; i < (int)(n); i++) {       a[i] = 1;       pos[i] = i + 1;     }     a[n - 1] = 0;     pos[n - 1] = n - 1;     for (int i = 0; i < (int)(k); i++) {       for (int j = 0; j < (int)(n); j++) {         while (a[j] + a[pos[j]] > n - j - 1) pos[j]++;         printf( %d  , pos[j] + 1);         a[j] += a[pos[j]];       }       puts(  );     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 100 + 2;   const int MOD = 1 << 30;   const double pi = acos(-1);   struct line {     double a, b, c;   };   struct point {     double x, y;   };   void pointsToLine(point p1, point p2, line &l) {     if (fabs(p1.x - p2.x) < 1e-2) {       l.a = 1.0;       l.b = 0.0;       l.c = -p1.x;     } else {       l.a = -(double)(p1.y - p2.y) / (p1.x - p2.x);       l.b = 1.0;       l.c = -(double)(l.a * p1.x) - p1.y;     }   }   bool areParallel(line l1, line l2) {     return ((fabs(l1.a - l2.a) < 1e-2) && (fabs(l2.b - l2.b) < 1e-2));   }   bool areSame(line l1, line l2) {     return (areParallel(l1, l2) && (fabs(l1.c - l2.c) < 1e-2));   }   bool areIntersect(line l1, line l2, point &p) {     if (areParallel(l1, l2)) return false;     p.x = (l2.b * l1.c - l1.b * l2.c) / (l2.a * l1.b - l1.a * l2.b);     if (fabs(l1.b) > 1e-2)       p.y = -(l1.a * p.x + l1.c);     else       p.y = -(l2.a * p.x + l2.c);     return true;   }   vector<pair<int, int> > AdjList[N];   int n, m;   int vis[N];   void dfs(int u, int c) {     vis[u] = true;     for (int i = 0; i < (int)AdjList[u].size(); ++i) {       pair<int, int> v = AdjList[u][i];       if (!vis[v.first]) {         if (v.second == c) dfs(v.first, v.second);       }     }   }   void solve() {     cin >> n >> m;     for (int i = 0; i < m; ++i) {       int u, v, w;       cin >> u >> v >> w;       AdjList[u].push_back({v, w});       AdjList[v].push_back({u, w});     }     int q;     cin >> q;     while (q--) {       int u, v;       cin >> u >> v;       int ans = 0;       for (int i = 1; i <= m; ++i) {         memset(vis, false, sizeof vis);         dfs(u, i);         if (vis[v]) ans++;       }       cout << ans <<   n ;     }   }   int main(void) {     ios::sync_with_stdio(false);     cin.tie(NULL);     cout.tie(NULL);     int tc = 1;     while (tc--) solve();   }


#include <bits/stdc++.h>   using namespace std;   const int MIN = 1e3 + 2;   const int MXN = 1e6 + 3;   const int INF = 1e9 + 7;   const long long LINF = 1e18 + 15;   const double EPS = 1e-9;   int s, x1, x2;   int t1, t2;   int p, d;   int main() {     cin >> s >> x1 >> x2;     cin >> t1 >> t2;     cin >> p >> d;     if (x1 <= x2) {       if (p <= x1) {         if (d == 1) {           cout << min((x2 - x1) * t2, (x2 - p) * t1);           cerr <<     << 1;           return 0;         }         cout << min((x2 - x1) * t2, (x2 + p) * t1);         cerr <<     << 2;         return 0;       }       if (d == 1) {         cout << min((x2 - x1) * t2, (s - p + s + x2) * t1);         cerr <<     << 3;         return 0;       }       cout << min((x2 - x1) * t2, (x2 + p) * t1);       cerr <<     << 4;       return 0;     }     if (p >= x1) {       if (d == 1) {         cout << min((x1 - x2) * t2, (s - x2 + s - p) * t1);         cerr <<     << 5;         return 0;       }       cout << min((x1 - x2) * t2, (p - x2) * t1);       cerr <<     << 6;       return 0;     }     if (d == 1) {       cout << min((x1 - x2) * t2, (s - x2 + s - p) * t1);       cerr <<     << 7;       return 0;     }     cout << min((x1 - x2) * t2, (s + p + s - x2) * t1);     cerr <<     << 8;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   struct vec {     long long x, y;   };   bool operator==(const vec& a, const vec& b) {     return a.x == b.x and a.y == b.y;   }   bool operator!=(const vec& a, const vec& b) { return not(a == b); }   bool operator<(const vec& a, const vec& b) {     return make_pair(a.x, a.y) < make_pair(b.x, b.y);   }   vec operator+(const vec& a, const vec& b) {     return (vec){a.x + b.x, a.y + b.y};   }   int main() {     ios_base::sync_with_stdio(false);     long long n, m;     vec d;     cin >> n >> m >> d.x >> d.y;     vector<vec> apple(m);     for (int i = 0; i < (m); ++i) cin >> apple[i].x >> apple[i].y;     sort(apple.begin(), apple.end());     vector<int> x2y(n);     {       vec p = d;       while (p != (vec){0, 0}) {         x2y[p.x] = p.y;         p = p + d;         p.x %= n;         p.y %= n;       }     }     vector<int> bucket(n);     {       int i = 0;       for (int x = 0; x < (n); ++x) {         for (; i < m and apple[i].x <= x; ++i) {           bucket[(apple[i].y - x2y[x] + n) % n] += 1;         }       }     }     {       int ix;       int sz = -1;       for (int i = 0; i < (n); ++i)         if (sz < bucket[i]) {           sz = bucket[i];           ix = i;         }       cout << 0 <<     << ix << endl;     }     return 0;   }


#include <bits/stdc++.h>   const double pi = acos(-1.0), eps = 1e-9;   const int dx[8] = {1, -1, 0, 0, 1, 1, -1, -1};   const int dy[8] = {0, 0, 1, -1, 1, -1, -1, 1};   const int MO = (int)(1e9 + 7);   using namespace std;   struct Tcmp {     bool operator()(const pair<int, int>& A, const pair<int, int>& B) {       int lenA = A.second - A.first + 1, lenB = B.second - B.first + 1;       lenA = lenA + 1 >> 1;       lenB = lenB + 1 >> 1;       if (lenA == lenB)         return A.first < B.first;       else         return lenA > lenB;     }   };   set<int> s;   set<pair<int, int>, Tcmp> heap;   int n, m;   int where[2000001];   int findnext(int t) {     set<int>::iterator sit = s.upper_bound(t);     if (sit == s.end()) return -1;     return *sit;   }   int findpre(int t) {     set<int>::iterator sit = s.lower_bound(t);     if (sit == s.begin()) return -1;     --sit;     return *sit;   }   int main() {     scanf( %d%d , &n, &m);     for (int i = 1; i <= m; i++) {       if (i == 8) {         int t = 1;         ++t;       }       int kind, id;       scanf( %d%d , &kind, &id);       if (kind == 1) {         pair<int, int> ans = make_pair(0, 0);         if (s.size() == 0) {           s.insert(1);           puts( 1 );           where[id] = 1;           continue;         }         if (s.count(1) == 0) {           pair<int, int> temp = make_pair(-(findnext(1) - 1), 1);           if (temp < ans) ans = temp;         }         if (s.count(n) == 0) {           pair<int, int> temp = make_pair(-(n - findpre(n)), n);           if (temp < ans) ans = temp;         }         if (heap.size()) {           pair<int, int> now = *heap.begin();           pair<int, int> temp = make_pair(-(now.second - now.first + 2 >> 1), 0);           temp.second = now.first - 1 - temp.first;           if (temp < ans) {             ans = temp;             heap.erase(heap.begin());           }         }         s.insert(ans.second);         int t = findpre(ans.second);         if (t != -1 && t != ans.second - 1)           heap.insert(make_pair(t + 1, ans.second - 1));         t = findnext(ans.second);         if (t != -1 && t != ans.second + 1)           heap.insert(make_pair(ans.second + 1, t - 1));         where[id] = ans.second;         printf( %d n , ans.second);       } else {         s.erase(where[id]);         int l = findpre(where[id]), r = findnext(where[id]);         if (l != -1) heap.erase(make_pair(l + 1, where[id] - 1));         if (r != -1) heap.erase(make_pair(where[id] + 1, r - 1));         if (l != -1 && r != -1) heap.insert(make_pair(l + 1, r - 1));       }     }   }


#include <bits/stdc++.h>   using namespace std;   struct segtree {     vector<long long> t;     segtree(long long _n) { t.resize(4 * _n); }     void update(long long id, long long l, long long r, long long pos,                 long long val) {       if (l == r) {         t[id] = val;         return;       }       long long mid = (l + r) / 2;       if (pos <= mid) {         update(2 * id, l, mid, pos, val);       } else {         update(2 * id + 1, mid + 1, r, pos, val);       }       t[id] = t[2 * id] + t[2 * id + 1];     }     long long query(long long id, long long l, long long r, long long lq,                     long long rq) {       if (lq > r || l > rq) return 0;       if (lq <= l && r <= rq) return t[id];       long long mid = (l + r) / 2;       long long L = query(2 * id, l, mid, lq, rq);       long long R = query(2 * id + 1, mid + 1, r, lq, rq);       return L + R;     }   };   signed main() {     ios::sync_with_stdio(false);     cin.tie(0);     long long N;     cin >> N;     vector<long long> A(N);     for (auto& a : A) cin >> a;     segtree t1(N), t2(N);     for (long long i = 0; i < N; i++) {       if (i % 2) {         t1.update(1, 0, N - 1, i, A[i]);       } else {         t2.update(1, 0, N - 1, i, A[i]);       }     }     long long best = A[0];     for (long long i = 0; i + 1 < N; i++) {       long long foo = A[i] + A[i + 1];       long long from = i + 3, to = N - 1;       if (to >= from) {         if (i % 2) {           foo += t2.query(1, 0, N - 1, from, to);         } else {           foo += t1.query(1, 0, N - 1, from, to);         }       }       from = 0;       to = i - 2;       if (to >= from) {         long long z = to - from + 1;         if (z % 2) {           foo += t2.query(1, 0, N - 1, from, to);         } else {           foo += t1.query(1, 0, N - 1, from, to);         }       }       best = max(best, foo);     }     long long foo = 0;     if (N > 1) {       foo = A[0] + A[N - 1];       for (long long i = 2; i <= N - 3; i += 2) foo += A[i];     }     cout << max(best, foo) <<   n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long N, P;   long long L[100001];   long long R[100001];   long long getAc(long long l, long long r, long long p) {     if ((l + p - 1) / p * p > r) return 0;     return (r - (l + p - 1) / p * p) / p + 1;   }   int main() {     scanf( %lld%lld , &N, &P);     for (int i = 0; i < N; i++) scanf( %lld%lld , &L[i], &R[i]);     double ans = 0;     for (int i = 0; i < N; i++) {       long long a = getAc(L[i], R[i], P);       long long b = getAc(L[(i + 1) % N], R[(i + 1) % N], P);       long long cur = (R[i] - L[i] + 1) * b;       cur += (R[(i + 1) % N] - L[(i + 1) % N] + 1) * a;       cur -= a * b;       ans += (double)cur * 2000 / (R[i] - L[i] + 1) /              (R[(i + 1) % N] - L[(i + 1) % N] + 1);     }     printf( %.8f , (double)ans);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n, m, a[200010], w[200010], f[3010][3010], g[3010][3010], inv[3010 * 2],       sum[3];   const int mod = 998244353;   int getInverse(int x, int y) {     int ret = 1;     while (y) {       if (y & 1) ret = (long long)ret * x % mod;       x = (long long)x * x % mod;       y >>= 1;     }     return ret;   }   int main() {     cin >> n >> m;     for (int i = 1; i <= n; ++i) cin >> a[i];     for (int i = 1; i <= n; ++i) {       cin >> w[i];       sum[a[i]] += w[i];       sum[2] += w[i];     }     for (int i = max(0, m - sum[0]); i <= 2 * m; ++i)       inv[i] = getInverse(sum[2] + i - m, mod - 2);     for (int i = m; i >= 0; --i) {       f[i][m - i] = g[i][m - i] = 1;       for (int j = min(m - i - 1, sum[0]); j >= 0; --j) {         f[i][j] = ((long long)(sum[1] + i + 1) * f[i + 1][j] +                    (long long)(sum[0] - j) * f[i][j + 1]) %                   mod * inv[i - j + m] % mod;         g[i][j] = ((long long)(sum[1] + i) * g[i + 1][j] +                    (long long)(sum[0] - j - 1) * g[i][j + 1]) %                   mod * inv[i - j + m] % mod;       }     }     for (int i = 1; i <= n; ++i)       cout << ((long long)w[i] * (a[i] ? f[0][0] : g[0][0])) % mod << endl;     return 0;   }


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


#include <bits/stdc++.h>   using namespace std;   const double pi = acos(-1.0);   int n, m;   long long minb[100010];   long long maxg[100010];   int main() {     cin >> n >> m;     for (int i = 0; i < n; i++) {       scanf( %lld , minb + i);     }     sort(minb, minb + n);     for (int i = 0; i < m; i++) {       scanf( %lld , maxg + i);     }     sort(maxg, maxg + m);     long long ans = 0;     if (minb[n - 1] > maxg[0]) {       cout << -1 << endl;       return 0;     }     if (m == 1) {       if (minb[n - 1] != maxg[0]) {         cout << -1 << endl;         return 0;       } else {         for (int i = 0; i < n; i++) {           ans += minb[i];         }         cout << ans << endl;         return 0;       }     }     for (int i = 0; i < n - 1; i++) {       ans += minb[i] * m;     }     for (int i = 1; i < m; i++) {       ans += maxg[i];     }     if (minb[n - 1] == maxg[0]) {       ans += minb[n - 1];     } else {       if (n == 1) {         cout << -1 << endl;         return 0;       } else {         ans += minb[n - 1] + maxg[0] - minb[n - 2];       }     }     cout << ans << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   template <typename T>   T gcd(T a, T b) {     return !b ? a : gcd(b, a % b);   }   template <typename T>   T lcm(T a, T b) {     return a * (b / gcd(a, b));   }   template <typename T>   T sqr(T a) {     return a * a;   }   template <typename T>   T cube(T a) {     return a * a * a;   }   template <typename T>   inline void smin(T &a, T b) {     a = a < b ? a : b;   }   template <typename T>   inline void smax(T &a, T b) {     a = a > b ? a : b;   }   template <typename T>   void fill(T a[], T n, T b) {     for (int i = 0; i <= n; i++) a[i] = b;   }   int in() {     int n;     scanf( %d , &n);     return n;   }   long long Lin() {     long long n;     scanf( %lld , &n);     return n;   }   double Din() {     double n;     scanf( %lf , &n);     return n;   }   const long long inf = (long long)1e17;   const long long mod = (long long)1e9 + 7;   const int N = 2e5 + 5;   int n, q, a[N];   set<pair<int, pair<int, int> > > s;   int tree[4 * N], prop[4 * N];   void propagate(int pos, int left, int right) {     int mid = (left + right) >> 1;     prop[pos] = 0;     tree[pos * 2] = mid - left + 1;     tree[pos * 2 + 1] = right - mid;     prop[pos * 2] = 1;     prop[pos * 2 + 1] = 1;   }   void update(int pos, int left, int right, int x, int y) {     if (left > y || right < x) return;     if (left >= x && right <= y) {       tree[pos] = right - left + 1;       prop[pos] = 1;       return;     }     if (prop[pos] > 0) propagate(pos, left, right);     int mid = (left + right) >> 1;     update(pos * 2, left, mid, x, y);     update(pos * 2 + 1, mid + 1, right, x, y);     tree[pos] = tree[pos * 2] + tree[pos * 2 + 1];   }   int query(int pos, int left, int right, int x, int y) {     if (left > y || right < x) return 0;     if (left >= x && right <= y) return tree[pos];     if (prop[pos] > 0) propagate(pos, left, right);     int mid = (left + right) >> 1;     int n = query(pos * 2, left, mid, x, y);     int m = query(pos * 2 + 1, mid + 1, right, x, y);     return m + n;   }   int root[N], Rank[N], lp[N], rp[N];   void init(int n) {     for (int i = 1; i <= n; i++) {       root[i] = i, Rank[i] = 1, lp[i] = i, rp[i] = i;     }   }   int Find(int u) { return root[u] == u ? u : root[u] = Find(root[u]); }   void Union(int u, int v) {     u = Find(u), v = Find(v);     if (u != v) {       root[v] = u;       Rank[u] += Rank[v];       rp[u] = rp[v];     }   }   int bs1(int l, int r) {     int ans = -1;     while (l <= r) {       int m = (l + r) >> 1;       if (query(1, 1, n, m, r) != (r - m + 1))         ans = m, l = m + 1;       else         r = m - 1;     }     return ans;   }   int bs2(int l, int r) {     int ans = n + 1;     while (l <= r) {       int m = (l + r) >> 1;       if (query(1, 1, n, l, m) != (m - l + 1))         ans = m, r = m - 1;       else         l = m + 1;     }     return ans;   }   int solve() {     n = in();     for (int i = 1; i <= n; i++) {       a[i] = in();     }     init(n);     int l = 1, cur = 1;     for (int i = 2; i <= n; i++) {       if (a[i] != a[i - 1]) {         s.insert({-cur, {l, i - 1}});         cur = 1, l = i;       } else {         Union(i - 1, i);         cur++;       }     }     s.insert({-cur, {l, n}});     int ans = 0;     while (!s.empty()) {       auto it = s.begin();       int u = (*it).second.first, v = (*it).second.second;       s.erase(it);       if (query(1, 1, n, u, v) != (v - u + 1)) {         ans++;         update(1, 1, n, u, v);         int x = bs1(1, u - 1);         int y = bs2(u + 1, n);         if (x >= 1 && y <= n && a[x] == a[y]) {           Union(x, y);           int par = Find(x);           s.insert({-Rank[par], {lp[par], rp[par]}});         }       }     }     cout << ans <<   n ;     return 0;   }   int main() {     int test = 1, tc = 0;     while (test--) {       solve();     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int mod = 1e9 + 7;   const int maxn = 550;   struct DDF {     int n;     int v[maxn];     int dp[maxn][maxn];     int dfs(int l, int r) {       if (l >= r) return dp[l][r] = 1;       if (~dp[l][r]) return dp[l][r];       long long ans = dfs(l + 1, r);       for (int i = l + 1; i <= r; i++) {         if (v[l + 1] < v[i + 1]) {           ans += 1LL * dfs(l + 1, i - 1) * (dfs(i, r));           ans %= mod;         }       }       return dp[l][r] = ans;     }     void solve() {       memset(dp, -1, sizeof dp);       scanf( %d , &n);       for (int i = 1; i <= n; i++) scanf( %d , &v[i]);       printf( %d n , dfs(1, n));     }   } solver;   int main() {     solver.solve();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   using ll = long long;   const int ac = 1e6 + 5;   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     int t;     cin >> t;     while (t--) {       int n, k;       cin >> n >> k;       cout << n <<   n ;       string s;       cin >> s;       int p = 0;       for (int i = 0; i < n / 2 - k + 1; i++) {         int j = p;         while (s[j] !=  ( ) j++;         cout << p + 1 <<     << j + 1 <<   n ;         reverse(s.begin() + p, s.begin() + j + 1);         p++;       }       for (int i = 0; i < n / 2 - k + 1; i++) {         int j = p;         while (s[j] !=  ) ) j++;         cout << p + 1 <<     << j + 1 <<   n ;         reverse(s.begin() + p, s.begin() + j + 1);         p++;       }       for (int i = 0; i < (k - 1) * 2; i++) {         if (i % 2 == 0) {           int j = p;           while (s[j] !=  ( ) j++;           cout << p + 1 <<     << j + 1 <<   n ;           reverse(s.begin() + p, s.begin() + j + 1);           p++;         } else {           int j = p;           while (s[j] !=  ) ) j++;           cout << p + 1 <<     << j + 1 <<   n ;           reverse(s.begin() + p, s.begin() + j + 1);           p++;         }       }     }   }


#include <bits/stdc++.h>   using namespace std;   const int N = 110;   pair<int, int> a[N];   int main() {     int n;     cin >> n;     for (int i = 0; i < n; i++) scanf( %d , &a[i].first), a[i].second = i + 1;     sort(a + 1, a + n);     int Sum1 = a[0].first;     int Sum2 = 0;     for (int i = 1; i < n; i++) Sum2 += a[i].first;     vector<int> Ans({1});     for (int i = 1; i < n && Sum1 <= Sum2 && 2 * a[i].first <= a[0].first; i++) {       Sum1 += a[i].first;       Sum2 -= a[i].first;       Ans.push_back(a[i].second);     }     if (Sum1 <= Sum2) return 0 * puts( 0 );     sort(Ans.begin(), Ans.end());     cout << Ans.size() << endl;     for (int x : Ans) printf( %d  , x);   }


#include <bits/stdc++.h>   #pragma warning(disable : 4996)   #pragma comment(linker,  /STACK:336777216 )   using namespace std;   int IT_MAX = 1 << 18;   const long long MOD = 1000000007;   const int INF = 0x3f3f3f3f;   const long long LL_INF = 0x3f3f3f3f3f3f3f3f;   const double PI = acos(-1);   const double ERR = 1e-10;   int ans[100050];   void aa1(int x) {     if (x == 2) {       ans[1] = 2;       ans[2] = 1;       return;     }     int i;     for (i = 2; i <= 30; i++)       if ((1 << i) > x + 2) break;     int v = i - 1;     if (x + 2 == (1 << v)) {       for (int i = 1; i <= x; i++) ans[i] = x + 1 - i;       return;     }     int nx;     for (i = (1 << v); i <= x; i++) {       int t = (1 << (v + 1)) - 1 - i;       ans[i] = t;       ans[t] = i;       nx = t;     }     aa1(nx - 1);   }   void aa2(int N) {     int v = 1;     while (v * 2 <= N) v *= 2;     int i;     for (i = 2; i < v; i += 2) {       ans[i] = i + 1;       ans[i + 1] = i;     }     vector<int> Vu;     for (i = v; i <= N; i++) Vu.push_back(i);     if (Vu.back() % 2 == 0) swap(Vu[Vu.size() - 2], Vu.back());     ans[Vu.back()] = 1;     ans[1] = Vu.back();     Vu.pop_back();     for (i = 0; i < Vu.size(); i++) ans[Vu[i]] = Vu[(i + 1) % Vu.size()];   }   int main() {     int N;     scanf( %d , &N);     if (N % 2 == 1)       printf( NO n );     else {       printf( YES n );       aa1(N);       for (int i = 1; i <= N; i++) printf( %d  , ans[i]);       printf(  n );     }     if (N <= 5)       printf( NO n );     else {       int t = 1;       while (t * 2 <= N) t *= 2;       if (t == N) return !printf( NO n );       if (t + 1 == N) {         ans[N - 1] = N;         ans[N] = N - 1;         aa2(N - 2);       } else         aa2(N);       printf( YES n );       for (int i = 1; i <= N; i++) printf( %d  , ans[i]);       printf(  n );     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int arr[50100];   int tmp[50100];   int ans[50100];   map<int, bool> vis;   int n;   bool Check(int mid) {     vis.clear();     for (int i = 1; i <= n; ++i) {       tmp[i] = arr[i];       while (tmp[i] > mid) tmp[i] >>= 1;       if (tmp[i] == 0) return false;       while (tmp[i] && vis[tmp[i]] == true) tmp[i] >>= 1;       if (tmp[i] == 0) return false;       vis[tmp[i]] = true;     }     return true;   }   int main() {     scanf( %d , &n);     for (int i = 1; i <= n; ++i) scanf( %d , &arr[i]);     int l, r;     l = 1, r = 2000000000;     while (l < r) {       int mid = l + ((r - l) >> 1);       if (Check(mid)) {         r = mid;         for (int i = 1; i <= n; ++i) ans[i] = tmp[i];       } else         l = mid + 1;     }     for (int i = 1; i <= n; ++i) {       if (i == 1)         printf( %d , ans[i]);       else         printf(  %d , ans[i]);     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 100010;   int n, x, y, i, j, l, pos1, pos2, A;   long long m, tmp1, tmp2, tmp3, ans;   struct node {     int x, y;   } a[N];   int cmp1(const node& a, const node& b) { return a.x > b.x; }   int cmp2(const node& a, const node& b) { return a.y < b.y; }   int main() {     scanf( %d%d%d%d%I64d , &n, &A, &x, &y, &m);     for (i = 1; i <= n; i++) scanf( %d , &a[i].x), a[i].y = i;     sort(a + 1, a + n + 1, cmp1);     for (tmp1 = 0, i = 1; i <= n && tmp1 + A - a[i].x <= m; i++)       tmp1 += A - a[i].x;     ans = 0;     j = n, tmp1 = m - tmp1 - A + a[i].x;     for (i--; i >= 0; i--) {       tmp1 = tmp1 + A - a[i + 1].x;       while (j > i + 1) {         tmp2 = (long long)(n - j + 1) * (a[j - 1].x - a[j].x);         if (tmp1 < tmp2)           break;         else           tmp1 -= tmp2, j--;       }       if (i < j)         tmp3 = min(tmp1 / (n - j + 1) + a[j].x, (long long)A);       else         tmp3 = A;       tmp2 = (long long)i * x + tmp3 * y;       if (tmp2 >= ans) ans = tmp2, pos1 = i, pos2 = j, l = tmp3;     }     for (i = 1; i <= pos1; i++) a[i].x = A;     for (i = pos2; i <= n; i++) a[i].x = l;     sort(a + 1, a + n + 1, cmp2);     for (printf( %I64d n , ans), i = 1; i < n; i++) printf( %d  , a[i].x);     return printf( %d n , a[n].x), 0;   }


#include <bits/stdc++.h>   using namespace std;   const int MAXN = 1000100;   struct seg {     seg *fe, *fd;     int meio, l, r, xo;     seg(int x, int y) {       l = x, r = y;       meio = (x + y) / 2;       xo = 0;       if (l == r) return;       fe = new seg(x, meio);       fd = new seg(meio + 1, y);     }     void up(int pos, int novo) {       if (l == r) {         xo = novo;         return;       }       if (pos <= meio)         fe->up(pos, novo);       else         fd->up(pos, novo);       xo = (fe->xo ^ fd->xo);     }     int get(int a, int b) {       if (l >= a && r <= b) return xo;       if (l > b || r < a) return 0;       return (fe->get(a, b) ^ fd->get(a, b));     }   };   seg st(1, MAXN);   seg ss(1, MAXN);   struct query {     int l, r, id;   };   query q[MAXN];   int resp[MAXN];   int v[MAXN];   bool comp1(query a, query b) { return a.r < b.r; }   int main() {     int n;     scanf( %d , &n);     map<int, int> mapa;     for (int i = 1; i <= n; i++) scanf( %d , &v[i]), ss.up(i, v[i]);     int m;     scanf( %d , &m);     for (int i = 1; i <= m; i++) {       scanf( %d %d , &q[i].l, &q[i].r);       q[i].id = i;     }     sort(q + 1, q + m + 1, comp1);     int p = 1;     for (int i = 1; i <= n; i++) {       int h = mapa[v[i]];       if (mapa[v[i]] != 0) st.up(h, 0);       mapa[v[i]] = i;       st.up(i, v[i]);       while (p <= m && q[p].r <= i) {         int r1 = ss.get(q[p].l, q[p].r);         int r2 = st.get(q[p].l, q[p].r);         resp[q[p].id] = (r1 ^ r2);         p++;       }     }     for (int i = 1; i <= m; i++) printf( %d n , resp[i]);   }


#include <bits/stdc++.h>   using namespace std;   const int N = 1000010;   const double eps = 1e-8;   int n, T, cnt1, cnt2;   long double sum1;   double sum2;   struct water {     int a, t;   } A[N], B[N], Tmp[N];   bool cmp1(const water &a, const water &b) { return a.t < b.t; }   bool cmp2(const water &a, const water &b) { return a.t > b.t; }   int dcmp(long double x) { return (fabs(x) < eps) ? 0 : x < 0 ? -1 : 1; }   int main() {     scanf( %d%d , &n, &T);     for (int i = 1; i <= n; i++) scanf( %d , &Tmp[i].a);     for (int i = 1; i <= n; i++) scanf( %d , &Tmp[i].t);     for (int i = 1; i <= n; i++) {       if (Tmp[i].t <= T)         A[++cnt1] = Tmp[i];       else         B[++cnt2] = Tmp[i];       sum1 += 1ll * Tmp[i].a * Tmp[i].t;       sum2 += Tmp[i].a;     }     sort(A + 1, A + cnt1 + 1, cmp1);     sort(B + 1, B + cnt2 + 1, cmp2);     if (!dcmp(sum1 / sum2 - T)) printf( %.8lf , sum2), exit(0);     int flag = 0;     if (dcmp(sum1 / sum2 - T) < 0) {       for (int i = 1; i <= cnt1; i++) {         long double del = (sum1 - T * sum2) / (A[i].t - T);         if (dcmp(del - A[i].a) <= 0) {           sum1 -= del * A[i].t;           sum2 -= del;           flag = 1;           break;         } else           sum1 -= 1ll * A[i].a * A[i].t, sum2 -= A[i].a;       }     } else {       for (int i = 1; i <= cnt2; i++) {         long double del = (sum1 - T * sum2) / (B[i].t - T);         if (dcmp(del - B[i].a) <= 0) {           sum1 -= del * B[i].t;           sum2 -= del;           flag = 1;           break;         } else           sum1 -= 1ll * B[i].a * B[i].t, sum2 -= B[i].a;       }     }     if (flag)       printf( %.8lf n , sum2);     else       puts( 0.000000 );     return 0;   }


#include <bits/stdc++.h>   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;     }   };   void solve() {     int n;     cin >> n;     string a, b;     cin >> a >> b;     vector<int> res;     for (int i = 0; i < a.length(); ++i) {       if (a[i] != b[i]) {         if (i > 0) res.push_back(i + 1);         res.push_back(1);         if (i > 0) res.push_back(i + 1);       }     }     cout << (int)res.size() <<    ;     for (long long i = 0; i < int((int)res.size()); i++) cout << res[i] <<    ;     cout <<   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;   const int maxn = 300005;   const int SIGMA_SIZE = 26;   int SZ = 0;   struct Node {     int sz, ch[SIGMA_SIZE];     Node() { memset(ch, 0, sizeof(ch)); }   } nd[maxn << 1];   int newNode() {     ++SZ;     memset(nd[SZ].ch, 0, sizeof(nd[SZ].ch));     return SZ;   }   void maintain(int u) {     nd[u].sz = 1;     for (int i = 0; i < SIGMA_SIZE; i++)       if (nd[u].ch[i]) nd[u].sz += nd[nd[u].ch[i]].sz;   }   void merge(int &a, int b) {     if (a == 0) {       a = b;     } else {       for (int i = 0; i < SIGMA_SIZE; i++) {         if (nd[b].ch[i]) merge(nd[a].ch[i], nd[b].ch[i]);       }     }     maintain(a);   }   int N;   long long C[maxn];   char S[maxn];   vector<int> G[maxn];   void init() {     scanf( %d , &N);     for (int i = 1; i <= N; i++) {       G[i].clear();       scanf( %lld , &C[i]);     }     scanf( %s , S + 1);     int u, v;     for (int i = 1; i < N; i++) {       scanf( %d%d , &u, &v);       G[u].push_back(v);       G[v].push_back(u);     }   }   int dfs(int u, int f) {     int p = newNode();     for (int i = 0; i < G[u].size(); i++) {       int v = G[u][i];       if (v == f) continue;       merge(p, dfs(v, u));     }     maintain(p);     int r = newNode();     nd[r].ch[S[u] -  a ] = p;     maintain(r);     C[u] += nd[r].sz - 1;     return r;   }   int main() {     init();     dfs(1, 0);     long long ans = 0, cnt;     for (int i = 1; i <= N; i++) {       if (C[i] > ans) {         ans = C[i];         cnt = 0;       }       if (C[i] == ans) {         cnt++;       }     }     printf( %I64d n%I64d n , ans, cnt);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int p = 998244353, inv2 = 499122177;   inline void XOR(int *a, int lim, int type) {     for (register int mid = 1; mid < lim; mid <<= 1) {       for (register int i = mid << 1, j = 0; j < lim; j += i) {         for (register int k = 0; k < mid; k++) {           a[j + k] += a[j + k + mid];           if (a[j + k] >= p) a[j + k] -= p;           a[j + k + mid] = a[j + k] - a[j + k + mid] - a[j + k + mid];           if (a[j + k + mid] >= p)             a[j + k + mid] -= p;           else             while (a[j + k + mid] < 0) a[j + k + mid] += p;           if (type == -1)             a[j + k] = 1ll * a[j + k] * inv2 % p,                   a[j + k + mid] = 1ll * a[j + k + mid] * inv2 % p;         }       }     }   }   int n, m, lim;   char s[25][2000086];   int a[2000086], b[2000086];   int ans = 0x3f3f3f3f;   int main() {     scanf( %d%d , &n, &m), lim = 1 << n;     for (int i = 1; i <= n; i++) scanf( %s , s[i] + 1);     for (int i = 1; i <= m; i++) {       int sum = 0;       for (int j = 1, k = 1; j <= n; j++, k <<= 1)         if (s[j][i] ==  1 ) sum += k;       ++a[sum];     }     for (int i = 1; i <= lim; i++) {       for (int j = i; j; j >>= 1)         if (j & 1) ++b[i];       b[i] = min(b[i], n - b[i]);     }     XOR(a, lim, 1), XOR(b, lim, 1);     for (int i = 0; i < lim; i++) a[i] = 1ll * a[i] * b[i] % p;     XOR(a, lim, -1);     for (int i = 0; i < lim; i++) ans = min(ans, a[i]);     printf( %d , ans);   }


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


#include <bits/stdc++.h>   using namespace std;   inline int read() {     char ch = getchar();     int w = 1, c = 0;     for (; !isdigit(ch); ch = getchar())       if (ch ==  - ) w = -1;     for (; isdigit(ch); ch = getchar()) c = (c << 1) + (c << 3) + (ch ^ 48);     return w * c;   }   const int mod = 1e9 + 7, M = 1e6 + 10;   struct node {     long long x, y, z;     node(long long _ = 1, long long __ = 0, long long ___ = 0) {       x = _;       y = __;       z = ___;     }   } a[M];   long long b[M];   node operator+(node A, node B) {     return node(A.x * B.x % mod, (A.y * B.x + B.y * A.x) % mod,                 (A.x * B.z + A.z * B.x + 2ll * A.y * B.y) % mod);   }   int n;   int main() {     n = read();     for (int i = (1); i <= (n); ++i) {       int x = read();       a[x] = a[x] + node(2, x, 1ll * x * x % mod);     }     for (int len = 1; len <= 100000; len *= 10) {       for (int j = (999999); j >= (0); --j) {         if ((j / len) % 10) {           a[j - len] = a[j - len] + a[j];         }       }     }     for (int i = (0); i <= (999999); ++i) b[i] = a[i].z;     for (int len = 1; len <= 100000; len *= 10) {       for (int j = (0); j <= (999999); ++j) {         if ((j / len) % 10) {           b[j - len] += mod - b[j];           b[j - len] %= mod;         }       }     }     long long ans = 0;     for (int i = (0); i <= (999999); ++i) {       ans ^= b[i] * i;     }     cout << ans <<   n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int m = 1000000007;   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     int n, k;     cin >> n >> k;     string s;     cin >> s;     for (int i = 0; i <= 25; i++) {       for (int j = 0; j < n and k > 0; j++) {         if (s[j] ==  a  + i) {           k--;           s[j] =  0 ;         }       }     }     for (auto &x : s) {       if (x !=  0 ) {         cout << x;       }     }   }


#include <bits/stdc++.h>   using namespace std;   void solve() {     string s, s1;     cin >> s;     int count = 0;     sort(s.begin(), s.end());     for (int i = 0; i < s.length(); ++i) {       if (s[i] ==  + ) count++;     }     std::vector<string> v(s.length());     for (int i = count; i < s.length(); ++i) {       v[i] = s[i];     }     if (count != 0)       for (int i = count; i < v.size() - 1; ++i) {         cout << v[i] <<  + ;       }     cout << v[v.size() - 1];   }   int main() { solve(); }


#include <bits/stdc++.h>   using namespace std;   int ans0[1000001];   int tail = 1;   int a[1001];   int n, m;   int ans = 0;   int main() {     int i, j, k;     int id;     int xx, yy;     int temp;     scanf( %d , &n);     for (i = 1; i <= n; i++) {       for (j = 1; j <= n; j++) {         scanf( %d , &temp);         if (i == j) {           a[i] = temp;           if (temp == 1) {             ans = 1 - ans;           }         }       }     }     scanf( %d , &m);     for (i = 1; i <= m; i++) {       scanf( %d , &id);       if (id == 1 || id == 2) {         scanf( %d , &temp);         ans = 1 - ans;         a[temp] = 1 - a[temp];       } else {         ans0[tail] = ans;         tail++;       }     }     for (i = 1; i <= tail - 1; i++) {       printf( %d , ans0[i]);     }     printf(  n );     return 0;   }


#include <bits/stdc++.h>   using namespace std;   bool b[1000050];   vector<int> v;   vector<int> v1;   int main() {     int n;     cin >> n;     int u = sqrt(n);     if (n == 1) {       cout << 1;       return 0;     }     for (int i = 2; i <= u; i++) {       if (!b[i]) {         for (int j = i * i; j <= n; j += i) b[j] = 1;       }     }     for (int i = 2; i <= n; i++)       if (!b[i]) {         v.push_back(i);       }     v1.push_back(n);     int j = 0;     while (1) {       while (n % v[j] != 0) j++;       while (n % v[j] == 0) {         n /= v[j];         v1.push_back(n);       }       if (j == v.size() - 1 || n == 1) break;     }     for (int i = 0; i < v1.size(); i++) cout << v1[i] <<    ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   vector<int> lk;   void make(int num, int len) {     if (len == 10) return;     if (num != 0) lk.push_back(num);     make(num * 10 + 4, len + 1);     make(num * 10 + 7, len + 1);   }   double gao(int l1, int l2, int r1, int r2) {     double l = max(l1, r1), r = min(l2, r2);     return max(0.0, r - l + 1);   }   int main() {     make(0, 0);     sort(lk.begin(), lk.end());     int p1, p2, v1, v2, k;     while (cin >> p1 >> p2 >> v1 >> v2 >> k) {       int c1 = max(p1, v1), c2 = min(c2, v2);       double tot = (p2 - p1 + 1.0) * (v2 - v1 + 1.0), sum = 0;       int sz = lk.size();       for (int i = 0; i + k - 1 < sz; i++) {         int l1, l2 = lk[i], r1 = lk[i + k - 1], r2;         if (i == 0)           l1 = 0;         else           l1 = lk[i - 1] + 1;         if (i + k == sz)           r2 = 1000000000;         else           r2 = lk[i + k] - 1;         sum += gao(l1, l2, p1, p2) * gao(r1, r2, v1, v2);         sum += gao(l1, l2, v1, v2) * gao(r1, r2, p1, p2);         sum -= gao(c1, c2, max(l1, r1), min(l2, r2));       }       printf( %.12f n , sum / tot);     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   struct pnt {     int x, y;     pnt(int x = 0, int y = 0) : x(x), y(y) {}     bool operator<(const pnt &p) const { return x == p.x ? y < p.y : x < p.x; }     bool operator==(const pnt &p) const { return x == p.x && y == p.y; }   };   const int inf = 1e9;   int ans;   pnt p[4], rlt[4];   void check(pnt *q) {     sort(q, q + 4);     while (1) {       bool flag = 0;       int cur = -1;       for (int i = 0; i < 4; i++) {         if (q[i].x != p[i].x && q[i].y != p[i].y) {           flag = 1;           break;         }         cur = max(cur, abs(p[i].x - q[i].x) + abs(p[i].y - q[i].y));       }       if (!flag) {         if (cur < ans) {           ans = cur;           for (int i = 0; i < 4; i++) rlt[i] = q[i];         }       }       if (!next_permutation(q, q + 4)) break;     }   }   int dx[4] = {1, 0, -1, 0};   int dy[4] = {0, 1, 0, -1};   void solve() {     for (int i = 0; i < 4; i++) cin >> p[i].x >> p[i].y;     vector<pnt> u;     for (int i = 0; i < 4; i++)       for (int j = 0; j < 4; j++) {         u.push_back(pnt(p[i].x, p[j].y));       }     sort(u.begin(), u.end());     u.erase(unique(u.begin(), u.end()), u.end());     vector<int> a;     for (int i = 0; i < 4; i++)       for (int j = 0; j < 4; j++) {         a.push_back(abs(p[i].x - p[j].x));         a.push_back(abs(p[i].y - p[j].y));       }     sort(a.begin(), a.end());     a.erase(unique(a.begin(), a.end()), a.end());     vector<pnt> v;     vector<int> dist;     for (int i = 0; i < a.size(); i++)       for (int j = i; j < a.size(); j++) {         int d = a[j] - a[i];         dist.push_back(d >> 1);         dist.push_back(d + 1 >> 1);       }     sort(dist.begin(), dist.end());     dist.erase(unique(dist.begin(), dist.end()), dist.end());     for (int k = 0; k < u.size(); k++) {       for (int h = 0; h < 4; h++) {         for (int r = 0; r < dist.size(); r++)           v.push_back(pnt(u[k].x + dist[r] * dx[h], u[k].y + dist[r] * dy[h]));       }     }     ans = inf;     sort(v.begin(), v.end());     v.erase(unique(v.begin(), v.end()), v.end());     for (int i = 0; i < v.size(); i++) {       for (int j = 1; j < a.size(); j++) {         pnt q0[4] = {pnt(v[i].x, v[i].y), pnt(v[i].x, v[i].y + a[j]),                      pnt(v[i].x + a[j], v[i].y),                      pnt(v[i].x + a[j], v[i].y + a[j])};         check(q0);         pnt q1[4] = {pnt(v[i].x, v[i].y), pnt(v[i].x, v[i].y - a[j]),                      pnt(v[i].x + a[j], v[i].y),                      pnt(v[i].x + a[j], v[i].y - a[j])};         check(q1);         pnt q2[4] = {pnt(v[i].x, v[i].y), pnt(v[i].x, v[i].y + a[j]),                      pnt(v[i].x - a[j], v[i].y),                      pnt(v[i].x - a[j], v[i].y + a[j])};         check(q2);         pnt q3[4] = {pnt(v[i].x, v[i].y), pnt(v[i].x, v[i].y - a[j]),                      pnt(v[i].x - a[j], v[i].y),                      pnt(v[i].x - a[j], v[i].y - a[j])};         check(q3);       }     }     if (ans == inf) {       puts( -1 );     } else {       printf( %d n , ans);       for (int i = 0; i < 4; i++) printf( %d %d n , rlt[i].x, rlt[i].y);     }   }   int main() {     int t;     cin >> t;     while (t--) {       solve();     }   }


#include <bits/stdc++.h>   using namespace std;   int main() {     ios::sync_with_stdio(0), ios_base::sync_with_stdio(0), cin.tie(0),         cout.tie(0);     int n;     cin >> n;     int arr[n];     for (int i = 0; i < n; i++) cin >> arr[i];     for (int i = 0; i < n; i++) {       for (int j = 0; j < n; j++) {         if (j != i) {           for (int k = 0; k < n; k++) {             if (k != j) {               if (arr[i] + arr[j] == arr[k]) {                 cout << k + 1 <<     << j + 1 <<     << i + 1;                 return 0;               }             }           }         }       }     }     cout <<  -1 ;     return 0;   }


#include <bits/stdc++.h>   const int N = 2e5 + 10;   const int MOD = 1e9 + 7;   const int INF = 2e9;   using namespace std;   vector<int> a;   int f[N], d[N];   int c, n;   int main() {     ios::sync_with_stdio(false);     cin >> c >> n;     for (int i = 0; i < n; i++) {       int x;       cin >> x;       a.push_back(x);     }     sort(a.begin(), a.end());     for (int i = 1; i <= N; i++) d[i] = -1;     for (int i = 0; i < n; i++)       if (d[a[i]] == -1) d[a[i]] = i;     vector<int>::iterator it;     for (int i = 1; i <= c; i++) {       it = lower_bound(a.begin(), a.end(), i);       if (*it == i)         f[i] = it - a.begin();       else if (it > a.begin())         f[i] = it - a.begin() - 1;       else         f[i] = -1;     }     for (int i = 1; i < c; i++) {       int tmp = c, prev = n + 1;       bool ok = true;       while (1) {         int y = f[tmp];         if (y >= prev) y = prev - 1;         if (y < 0) {           cout << i;           return 0;         }         if (y == -1) {           cout << i;           return 0;         } else if (ok && a[y] <= i && i <= tmp) {           tmp -= i;           ok = false;         } else {           int x = a[y];           int pos = d[x];           int t1 = y - pos + 1;           int t2 = tmp / x;           if (t1 > t2) {             tmp -= t2 * x;             prev = y - t2 + 1;           } else {             tmp -= t1 * x;             prev = pos;           }         }         if (tmp == 0) break;       }     }     cout <<  Greed is good ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 100005;   char A[N];   char B[N];   int buck[128];   int ans[128];   bool ok() {     for (int i =  a ; i <=  z ; ++i)       if (buck[i] > ans[i]) return false;     return true;   }   int main() {     scanf( %s , A);     scanf( %s , B);     int aa = strlen(A);     int bb = strlen(B);     for (int i = 0; i < bb; ++i) {       buck[A[i]]++;       ans[B[i]]++;     }     int cnt = 0;     for (int i = bb - 1; i < aa; ++i) {       if (ok()) cnt++;       buck[A[i - bb + 1]]--;       buck[A[i + 1]]++;     }     printf( %d n , cnt);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     string s1, s2, left, line;     cin >> line;     bool first = true;     for (int i = 0; i < line.size(); i++) {       if (line[i] ==  | )         first = false;       else if (first)         s1 += line[i];       else         s2 += line[i];     }     cin >> left;     int l1 = s1.size();     int l2 = s2.size();     int l3 = left.size();     int m = (l1 + l2 + l3) / 2;     if ((l1 + l2 + l3) % 2 == 1 || l1 > m || l2 > m)       cout <<  Impossible  << endl;     else {       cout << s1;       int p = m - l1;       for (int i = 0; i < p; i++) cout << left[i];       cout <<  | ;       cout << s2;       for (int i = p; i < l3; i++) cout << left[i];       cout << endl;     }   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n;     char tmp[11];     deque<string> dq;     set<string> st;     scanf( %d , &n);     for (int i = 0; i < n; i++) {       scanf( %s , &tmp);       dq.push_front(tmp);     }     for (deque<string>::iterator i = dq.begin(); i != dq.end(); i++) {       if (st.count(*i) == 0) {         printf( %s n , (*i).c_str());         st.insert(*i);       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 4010;   int n, a, b;   vector<pair<int, int>> ans;   deque<int> s[3];   inline void print() {     if (n == 1248)       for (pair<int, int> p : ans) printf( %d %d n , p.first, p.second);   }   inline void move(int tp, int c) {     if (!c) return;     if (tp == 0 && c > a) print(), puts( NO ), exit(0);     if (tp == 1 && c > b) print(), puts( NO ), exit(0);     if (c > s[tp].size()) print(), puts( NO ), exit(0);     if (c + s[tp + 1].size() > n) print(), puts( NO ), exit(0);     ans.push_back(make_pair(tp + 1, c));     for (int i = c - 1; i >= 0; --i) s[tp + 1].push_front(s[tp][i]);     for (int i = c - 1; i >= 0; --i) s[tp].pop_front();   }   inline bool judge(int tp, int c) {     if (tp == 0 && c > a) return false;     if (tp == 1 && c > b) return false;     return true;   }   int main() {     scanf( %d%d%d , &n, &a, &b);     for (int i = 1, x; i <= n; ++i) scanf( %d , &x), s[0].push_back(x);     while (s[2].size() != n) {       if (!s[0].size() && !s[1].size()) print(), puts( NO ), exit(0);       for (int i = 1; i < s[1].size(); ++i)         if (s[1][i - 1] < s[1][i] - 1) print(), puts( NO ), exit(0);       int flag = 0;       for (int i = 0; i < s[0].size(); ++i) {         if (s[0][i] == (n - s[2].size())) {           for (int j = 0; j <= i; ++j) move(0, 1);           flag = 1;         }         if (i < s[0].size() && s[0][i] + 1 < s[0][i + 1]) break;       }       if (flag) continue;       for (int i = 0; i < s[1].size(); ++i) {         if (s[1][i] == (n - s[2].size())) {           move(1, i + 1);           flag = 1;         }       }       if (flag) continue;       int l = 0, cnt = 1, r = s[0].size() - 1;       vector<int> st;       st.push_back(l);       for (int i = 1; i < s[0].size(); ++i) {         if (s[0][i - 1] + 1 < s[0][i]) {           r = i - 1;           break;         }         if (s[0][i] < s[0][i - 1]) ++cnt, st.push_back(i);       }       int mn = s[0][0], mx = s[0][0];       for (int i = l; i <= r; ++i) mn = min(mn, s[0][i]), mx = max(mx, s[0][i]);       if (mx - mn + 1 > r - l + 1)         move(0, r - l + 1);       else {         int mxlen = 0;         for (int i = 0; i < st.size(); ++i)           mxlen = max(mxlen, (i < st.size() - 1 ? st[i + 1] : r + 1) - st[i]);         if (mxlen <= a && r - l + 1 <= b) {           for (int i = 0; i < st.size(); ++i)             move(0, (i < st.size() - 1 ? st[i + 1] : r + 1) - st[i]);         } else {           if (r - l + 1 > b) {             if (cnt == 1)               for (int i = 1; i <= r - l + 1; ++i) move(0, 1);             else if (cnt > 2)               move(0, r - l + 1);             else {               for (int i = l; i <= r; ++i) {                 if (judge(0, i - l + 1) && judge(0, r - i)) {                   if (i == st[1] - 1) continue;                   if (i != r) {                     if (i < st[1] - 1 &&                         max(st[1] - i, r - l + 1 - (st[1] - i)) > b)                       continue;                     if (i >= st[1] &&                         max(i - st[1] + 1, r - l + 1 - (i - st[1] + 1)) > b)                       continue;                   }                   if (i == r) {                     if (mxlen > a) continue;                   }                   move(0, i - l + 1);                   move(0, r - i);                   flag = 1;                   break;                 }               }               if (!flag) print(), puts( NO ), exit(0);             }           } else {             if (cnt == 1)               for (int i = 1; i <= r - l + 1; ++i) move(0, 1);             else if (cnt > 2)               print(), puts( NO ), exit(0);             else {               for (int i = l; i <= r; ++i) {                 if (judge(0, i - l + 1) && judge(0, r - i)) {                   move(0, i - l + 1);                   move(0, r - i);                   flag = 1;                   break;                 }               }               if (!flag) print(), puts( NO ), exit(0);             }           }         }       }     }     puts( YES );     printf( %d n , ans.size());     for (pair<int, int> p : ans) printf( %d %d n , p.first, p.second);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int t, a, b, c, d, k;     cin >> t;     while (t--) {       cin >> a >> b >> c >> d >> k;       int pen = (a + c - 1) / c;       int penc = (b + d - 1) / d;       if (pen + penc <= k)         cout << pen <<     << penc << endl;       else         cout << -1 << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int maxn = 105;   int n, a[maxn], vis[maxn];   int gcd(int a, int b) {     while (b) {       int c = a % b;       a = b;       b = c;     }     return a;   }   int main() {     scanf( %d , &n);     for (int i = 1; i <= n; i++) scanf( %d , &a[i]);     int flag = 0;     vector<int> ans;     for (int i = 1; i <= n; i++) {       if (vis[i]) continue;       int now = i;       int cnt = 0;       while (!vis[now]) {         vis[now] = i;         now = a[now];         cnt++;       }       if (now != i) return puts( -1 ), 0;       if (cnt % 2 == 0) cnt /= 2;       ans.push_back(cnt);     }     long long A = ans[0];     for (int i = 1; i < ans.size(); i++) A = A * ans[i] / gcd(A, ans[i]);     cout << A << endl;   }