#include <bits/stdc++.h>   using namespace std;   const long long mod = 1000000007;   long long powmod(long long a, long long b) {     long long res = 1;     a %= mod;     for (; b; b >>= 1) {       if (b & 1) res = res * a % mod;       a = a * a % mod;     }     return res;   }   const int N = 101000;   int n, val[N];   pair<int, int> p[N];   long long A, cf, cm, m, s[N];   int main() {     scanf( %d%lld%lld%lld%lld , &n, &A, &cf, &cm, &m);     for (int i = 1; i < n + 1; i++) {       scanf( %d , &p[i].first);       p[i].second = i;     }     sort(p + 1, p + n + 1);     p[n + 1].first = A;     for (int i = 1; i < n + 1; i++) s[i] = s[i - 1] + p[i].first;     long long ans = 0;     int ansp = 0, ansl = 0, ansv = 0;     if (n * A - s[n] <= m) {       ans = A * cm + n * cf;       ansp = 0;       ansl = 0;       ansv = 0;     }     for (int i = 1; i < n + 1; i++) {       long long c = (n - i) * A - (s[n] - s[i]);       if (c > m) continue;       long long rm = m - c;       int l = 1, r = i + 1;       while (l + 1 < r) {         int md = (l + r) >> 1;         long long sm = (long long)p[md].first * md - s[md];         if (sm <= rm)           l = md;         else           r = md;       }       rm -= ((long long)p[l].first * l - s[l]);       long long nv = p[l + 1].first;       if (l == i) nv = A;       long long mm = min(p[l].first + rm / l, nv);       long long ret = mm * cm + (n - i) * cf;       if (ret >= ans) {         ans = ret;         ansp = i;         ansl = l;         ansv = mm;       }     }     printf( %lld n , ans);     for (int i = ansp + 1; i < n + 1; i++) p[i].first = A;     for (int i = 1; i < ansl + 1; i++) p[i].first = ansv;     for (int i = 1; i < n + 1; i++) val[p[i].second] = p[i].first;     for (int i = 1; i < n + 1; i++) printf( %d  , val[i]);     puts(  );   }


#include <bits/stdc++.h>   using namespace std;   int n;   int x_search(const int &down, const int &up, const int &c) {     int l = down, r = up, key = 0;     while (l <= r) {       int mid = (l + r) / 2, cnt;       cout <<  ?   << mid <<     << 1 <<     << n <<     << n <<   n ;       cin >> cnt;       if (cnt >= c) {         key = mid;         l = mid + 1;       } else         r = mid - 1;     }     return key;   }   int u_search(const int &left, const int &right, const int &c) {     int l = left, r = right, key = right + 1;     while (l <= r) {       int mid = (l + r) / 2, cnt;       cout <<  ?   << 1 <<     << 1 <<     << mid <<     << n <<   n ;       cin >> cnt;       if (cnt >= c) {         key = mid;         r = mid - 1;       } else         l = mid + 1;     }     return key;   }   int y_search(const int &left, const int &right, const int &c) {     int l = left, r = right, key = 0;     while (l <= r) {       int mid = (l + r) / 2, cnt;       cout <<  ?   << 1 <<     << mid <<     << n <<     << n <<   n ;       cin >> cnt;       if (cnt >= c) {         key = mid;         l = mid + 1;       } else         r = mid - 1;     }     return key;   }   int v_search(const int &left, const int &right, const int &c) {     int l = left, r = right, key = right + 1;     while (l <= r) {       int mid = (l + r) / 2, cnt;       cout <<  ?   << 1 <<     << 1 <<     << n <<     << mid <<   n ;       cin >> cnt;       if (cnt >= c) {         key = mid;         r = mid - 1;       } else         l = mid + 1;     }     return key;   }   void check(int x_1, int y_1, int u_1, int v_1, int x_2, int y_2, int u_2,              int v_2) {     if (x_1 > u_1 || y_1 > v_1 || x_2 > u_2 || y_2 > v_2) return;     int c1, c2;     cout <<  ?   << x_1 <<     << y_1 <<     << u_1 <<     << v_1 <<   n ;     cin >> c1;     cout <<  ?   << x_2 <<     << y_2 <<     << u_2 <<     << v_2 <<   n ;     cin >> c2;     if (c1 == 1 && c2 == 1)       cout <<  !   << x_1 <<     << y_1 <<     << u_1 <<     << v_1 <<     << x_2            <<     << y_2 <<     << u_2 <<     << v_2 <<   n ;   }   int main() {     cin >> n;     int x_1 = x_search(1, n, 2), x_2 = x_search(x_1, n, 1),         u_2 = u_search(1, n, 2), u_1 = u_search(1, u_2, 1),         y_1 = y_search(1, n, 2), y_2 = y_search(y_1, n, 1),         v_2 = v_search(1, n, 2), v_1 = v_search(1, v_2, 1);     int c;     check(x_1, y_1, u_1, v_1, x_2, y_2, u_2, v_2);     check(x_1, y_1, u_2, v_1, x_2, y_2, u_1, v_2);     check(x_1, y_1, u_1, v_2, x_2, y_2, u_2, v_1);     check(x_1, y_1, u_2, v_2, x_2, y_2, u_1, v_1);     check(x_1, y_2, u_1, v_1, x_2, y_1, u_2, v_2);     check(x_1, y_2, u_2, v_1, x_2, y_1, u_1, v_2);     check(x_1, y_2, u_1, v_2, x_2, y_1, u_2, v_1);     check(x_1, y_2, u_2, v_2, x_2, y_1, u_1, v_1);   }


#include <bits/stdc++.h>   using namespace std;   constexpr long long int mod = 1e9 + 7;   constexpr long long int MAXN = 1e6 + 3;   constexpr long long int inf = 1ll << 32;   int main() {     ios::sync_with_stdio(false);     cin.tie(0);     int n;     cin >> n;     vector<int> vx(n + 2);     int med = 0;     for (int c = 1; c <= n; c++) {       cin >> vx[c];       vx[c] += vx[c - 1];     }     if (vx[n] % 2 == 0)       med = vx[n] / 2;     else       med = vx[n] / 2 + 1;     for (int c = 1; c <= n; c++)       if (vx[c] >= med) {         cout << c;         return 0;       }   }


#include <bits/stdc++.h>   using namespace std;   const int INF = 0x3f3f3f3f;   int pos[11][1111];   int gra[1111][1111];   int main() {     int n, m;     cin >> n >> m;     int x;     for (int i = 0; i < m; i++) {       for (int j = 0; j < n; j++) {         cin >> x;         pos[i][x - 1] = j;       }     }     for (int i = 0; i < n; i++) {       for (int j = 0; j < n; j++) {         int flag = 1;         for (int k = 0; k < m; k++) {           flag &= (pos[k][i] < pos[k][j]);         }         if (flag == true)           gra[i][j] = 1;         else           gra[i][j] = -INF;       }     }     for (int k = 0; k < n; k++) {       for (int i = 0; i < n; i++) {         for (int j = 0; j < n; j++) {           gra[i][j] = max(gra[i][j], gra[i][k] + gra[k][j]);         }       }     }     int ans = 0;     for (int i = 0; i < n; i++) {       for (int j = 0; j < n; j++) {         ans = max(ans, gra[i][j]);       }     }     cout << ans + 1 << endl;     return 0;   }


#include <bits/stdc++.h>   #pragma GCC optimize( O3 )   using namespace std;   using ll = long long;   using ii = pair<int, int>;   using uint = unsigned int;   const int N = 1e5 + 5, MOD = 1e9 + 7;   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     int n;     cin >> n;     vector<int> deg(n + 1);     for (int i = 1, u, v; i < n; i++) {       cin >> u >> v;       deg[u]++;       deg[v]++;     }     if (find(deg.begin(), deg.end(), 2) == deg.end())       cout <<  YES n ;     else       cout <<  NO n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n, x, y;   set<pair<int, int> > S, T;   int solve(set<pair<int, int> >& s, set<pair<int, int> >& t) {     set<pair<int, int> >::iterator it1, it2, it3, it4;     it1 = s.begin();     it2 = s.end();     it2--;     it3 = t.begin();     it4 = t.end();     it4--;     while (it1->first < it2->first || it3->first < it4->first) {       int x = it1->first;       int xx = (++it1)->first;       if (xx > x + 1) {         set<pair<int, int> > res_s;         set<pair<int, int> > res_t;         while (1) {           it1 = s.begin();           if (it1->first == xx) break;           res_s.insert(make_pair(it1->first, it1->second));           res_t.insert(make_pair(it1->second, it1->first));           s.erase(make_pair(it1->first, it1->second));           t.erase(make_pair(it1->second, it1->first));         }         return solve(s, t) + solve(res_s, res_t);       }       x = it2->first;       xx = (--it2)->first;       if (xx < x - 1) {         set<pair<int, int> > res_s;         set<pair<int, int> > res_t;         while (1) {           it2 = s.end();           it2--;           if (it2->first == xx) break;           res_s.insert(make_pair(it2->first, it2->second));           res_t.insert(make_pair(it2->second, it2->first));           s.erase(make_pair(it2->first, it2->second));           t.erase(make_pair(it2->second, it2->first));         }         return solve(s, t) + solve(res_s, res_t);       }       int y = it3->first;       int yy = (++it3)->first;       if (yy > y + 1) {         set<pair<int, int> > res_s;         set<pair<int, int> > res_t;         while (1) {           it3 = t.begin();           if (it3->first == yy) break;           res_s.insert(make_pair(it3->second, it3->first));           res_t.insert(make_pair(it3->first, it3->second));           s.erase(make_pair(it3->second, it3->first));           t.erase(make_pair(it3->first, it3->second));         }         return solve(s, t) + solve(res_s, res_t);       }       y = it4->first;       yy = (--it4)->first;       if (yy < y - 1) {         set<pair<int, int> > res_s;         set<pair<int, int> > res_t;         while (1) {           it4 = t.end();           it4--;           if (it4->first == yy) break;           res_s.insert(make_pair(it4->second, it4->first));           res_t.insert(make_pair(it4->first, it4->second));           s.erase(make_pair(it4->second, it4->first));           t.erase(make_pair(it4->first, it4->second));         }         return solve(s, t) + solve(res_s, res_t);       }     }     return 1;   }   int main() {     scanf( %d , &n);     for (int i = 1; i <= n; i++) {       scanf( %d%d , &x, &y);       S.insert(make_pair(x, y));       T.insert(make_pair(y, x));     }     printf( %d n , solve(S, T));     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n, sn, i, j, sum, temp, nl;   map<int, int> myMap;   vector<int> t[105];   int main() {     cin >> n;     j = 0;     sn = n * n;     for (i = sn; i > sn / 2; i--) {       t[j % n].push_back(i);       t[j % n].push_back(sn - i + 1);       j++;     }     for (i = 0; i < n; i++) {       nl = t[i].size();       for (j = 0; j < nl; j++) cout << t[i][j] <<    ;       cout << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int Number[12][12];   int Next[110];   double dp[110];   int main() {     for (int i = 1; i <= 10; i++)       for (int j = 1; j <= 10; j++)         Number[i][j] = (10 - i) * 10 + (i % 2 == 0 ? j : 11 - j);     for (int i = 1; i <= 10; i++) {       for (int j = 1; j <= 10; j++) {         int x;         scanf( %d , &x);         if (x == 0)           Next[Number[i][j]] = Number[i][j];         else           Next[Number[i][j]] = Number[i - x][j];       }     }     dp[100] = 0;     double c = 1;     for (int i = 99; i >= 94; i--) {       dp[i] = (c + 6 - (100 - i)) / (100 - i);       c += dp[i] + 1;     }     for (int i = 93; i >= 1; i--) {       double temp = 0;       for (int j = 1; j <= 6; j++) temp += min(dp[i + j], dp[Next[i + j]]);       dp[i] = temp / 6 + 1;     }     printf( %.10f n , dp[1]);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int t;     cin >> t;     while (t--) {       int a, b, c, d;       cin >> a >> b >> c >> d;       if (a < b) swap(a, b);       if (c < d) swap(c, d);       cout << ((a == c && b + d == a) ?  Yes n  :  No n );     }   }


#include <bits/stdc++.h>   int main() {     int64_t d1, d2, d3;     std::cin >> d1 >> d2 >> d3;     int64_t dist = d1 + d2 + d3;     dist = std::min(dist, 2 * d1 + 2 * d2);     dist = std::min(dist, 2 * d1 + 2 * d3);     dist = std::min(dist, 2 * d2 + 2 * d3);     std::cout << dist << std::endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int INF = 2e9;   long long gcd(long long a, long long b) {     while (b) {       a %= b;       swap(a, b);     }     return a;   }   int main() {     long long l, r, x, y;     cin >> l >> r >> x >> y;     long long d = 1;     long long res = 0;     map<int, int> m;     while (d * d <= y) {       if (y % d == 0) {         if (!m[min(y / d, d * x)] && d * x >= l && d * x <= r && y / d >= l &&             y / d <= r && gcd(d * x, y / d) == x) {           res++;           if (d * x != y / d) res++;           m[min(y / d, d * x)]++;         }       }       d++;     }     cout << res << endl;   }


#include <bits/stdc++.h>   using namespace std;   const int maxn = 5e2;   pair<pair<int, int>, bool> pr[maxn][maxn][2];   queue<pair<pair<int, int>, bool> > q;   vector<int> neighbor[maxn];   void bfs(int s, int t) {     for (int i = 0; i < maxn; i++)       for (int j = 0; j < maxn; j++)         for (int k = 0; k < 2; k++)           pr[i][j][k] = make_pair(make_pair(-1, -1), false);     q.push(make_pair(make_pair(s, t), 0));     while (!q.empty()) {       int v = q.front().first.second, u = q.front().first.first;       bool b = q.front().second;       q.pop();       if (!b) {         for (int i = 0; i < neighbor[u].size(); i++)           if (pr[neighbor[u][i]][v][!b].first.first == -1) {             pr[neighbor[u][i]][v][!b] = make_pair(make_pair(u, v), b);             q.push(make_pair(make_pair(neighbor[u][i], v), !b));           }       } else {         for (int i = 0; i < neighbor[v].size(); i++)           if (pr[u][neighbor[v][i]][!b].first.first == -1 &&               neighbor[v][i] != u && (u != s || neighbor[v][i] != t)) {             pr[u][neighbor[v][i]][!b] = make_pair(make_pair(u, v), b);             q.push(make_pair(make_pair(u, neighbor[v][i]), !b));           }       }     }   }   int main() {     int n, m;     cin >> n >> m;     for (int i = 0; i < m; i++) {       int u, v;       cin >> u >> v;       neighbor[u - 1].push_back(v - 1);       neighbor[v - 1].push_back(u - 1);     }     bfs(0, n - 1);     if (pr[n - 1][0][0].first.first == -1)       cout << -1;     else {       vector<int> fi, se;       pair<pair<int, int>, bool> kl = make_pair(make_pair(n - 1, 0), 0);       while (kl.first.first != -1) {         if (kl.second)           se.push_back(kl.first.second);         else           fi.push_back(kl.first.first);         kl = pr[kl.first.first][kl.first.second][kl.second];       }       cout << fi.size() - 1 << endl;       for (int i = fi.size() - 1; i > -1; i--) cout << fi[i] + 1 <<    ;       cout << endl;       for (int i = se.size() - 1; i > -1; i--) cout << se[i] + 1 <<    ;       cout << 1;     }   }


#include <bits/stdc++.h>   using namespace std;   long long n, m;   pair<long long, long long> seg[500000 + 1];   long long dp[20 + 1][500000 + 1];   signed main() {     cin.tie(0);     cout.tie(0);     ios_base::sync_with_stdio(false);     cin >> n >> m;     for (long long i = 1; i < n + 1; i++) {       long long l, r;       cin >> l >> r;       seg[i] = {l, r};       if (dp[0][l] < r) {         dp[0][l] = r;       }     }     for (long long i = 1; i < 500000 + 1; i++) {       dp[0][i] = max(dp[0][i], dp[0][i - 1]);       if (dp[0][i] < i) dp[0][i] = 0;     }     for (long long i = 1; i < 20 + 1; i++) {       for (long long j = 0; j < 500000 + 1; j++) {         dp[i][j] = dp[i - 1][dp[i - 1][j]];         if (dp[i][j] < j) dp[i][j] = 0;       }     }     for (long long j = 1; j < m + 1; j++) {       long long x, y;       cin >> x >> y;       long long ans = 0;       for (long long i = 20; i >= 0; i--) {         if (dp[i][x] < y and dp[i][x] != 0) {           ans += (1 << i);           x = dp[i][x];         }       }       if (dp[0][x] >= y)         cout << ans + 1 <<   n ;       else         cout << -1 <<   n ;     }   }


#include <bits/stdc++.h>   using namespace std;   int param[51][51], n, targ;   vector<int> visted(51);   stack<int> s;   bool dfs(int root) {     if (root == targ) {       return true;     }     if (visted[root] == 0) s.push(root);     visted[root] = 1;     int nxt = -1;     for (int i = 1; i <= n; i++) {       if (param[root][i] == 1 && visted[i] == 0) {         nxt = i;       }     }     if (nxt == -1 && s.size() > 1) {       s.pop();       nxt = s.top();     }     if (nxt != -1)       dfs(nxt);     else       return false;   }   int GetNumberOfDigits(int i) { return i > 0 ? (int)log2(i) + 1 : 1; }   int main() {     int n, m, d, mx = 0, ret = 0;     vector<int> v;     cin >> n >> m;     for (int i = int(0); i < int(n); i++) {       cin >> d;       v.push_back(d);     }     int tm;     mx = m;     for (int i = int(0); i < int(n); i++) {       tm = m;       if (v[i] <= tm) {         ret = tm / v[i];         tm -= (ret * v[i]);       } else {         continue;       }       for (int j = int(i + 1); j < int(n); j++) {         int x = v[j] * ret + tm;         if (mx < x) mx = x;       }     }     cout << mx;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long int MOD = 1e9 + 7;   const int N = 1e6 + 5, M = 1e5 + 10, NM1 = N - 1;   long long int OO = 1e18 + 8;   int oo = 1e9 + 8;   void readFile() {}   int n, m, l, r, x, top, arr[N], st[N];   long long int sum[N];   int main() {     readFile();     scanf( %d , &n);     for (int i = 1; i <= n; i++) scanf( %d , arr + i);     for (int i = 1; i <= n + 1; i++) {       while (arr[i] < arr[st[top]]) {         l = st[top] - st[top - 1];         r = i - st[top];         sum[1] += x = arr[st[top--]];         sum[l + 1] -= x;         sum[r + 1] -= x;         sum[l + r + 1] += x;       }       st[++top] = i;     }     for (int i = 1; i <= n; i++) sum[i] += sum[i - 1];     for (int i = 1; i <= n; i++) sum[i] += sum[i - 1];     scanf( %d , &m);     for (int i = 0; i < m; i++) {       scanf( %d , &x);       printf( %0.12f n , (double)sum[x] / (n - x + 1.0));     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main(void) {     int n, k;     int v[200001];     scanf( %d%d , &n, &k);     for (int i = 0; i < n; i++) scanf( %d , v + i);     sort(v, v + n);     int l = 1;     int r = 1000000000;     int m;     for (int it = 0; l <= r and it < 50; it++) {       m = l + (r - l) / 2;       auto i = upper_bound(v, v + n, m);       if ((i - v) > k)         r = m;       else if ((i - v) == k)         break;       else         l = m + 1;     }     auto i = upper_bound(v, v + n, m);     if ((i - v) == k and (m > 0 and m <= 1000000000))       printf( %d n , m);     else       printf( -1 n );     return 0;   }


#include <bits/stdc++.h>   const int MAXN = 2e5 + 7;   struct Edge {     int t, next;   } edge[MAXN << 1];   int head[MAXN], cnt;   int size[MAXN];   int dfn[MAXN], pos[MAXN], ts = 0;   int n, m;   int dist[MAXN], deep[MAXN];   int f[MAXN][19];   int t1 = 1, t2 = 1;   struct SegmentTree;   struct Questions;   SegmentTree *newNode(int, int, SegmentTree *, SegmentTree *);   struct SegmentTree {     int l, r, mid;     SegmentTree *lc, *rc;     int val, add, max;     static SegmentTree *build(int l, int r) {       SegmentTree *ret = NULL;       if (l == r) {         ret = newNode(l, r, NULL, NULL);         ret->val = ret->max = dist[pos[l]];         return ret;       } else {         int mid = l + (r - l) / 2;         ret = newNode(l, r, build(l, mid), build(mid + 1, r));         ret->pushUp();         return ret;       }     }     void pushUp() { max = std::max(lc->max, rc->max); }     void pushDown() {       if (add) {         lc->plus(add), rc->plus(add);         add = 0;       }     }     void plus(int a) {       if (l == r) val += a;       add += a;       max += a;     }     void solveAdd(int left, int right, int delta) {       if (l == left && r == right) return plus(delta);       pushDown();       if (right <= mid)         lc->solveAdd(left, right, delta);       else if (left > mid)         rc->solveAdd(left, right, delta);       else         lc->solveAdd(left, mid, delta), rc->solveAdd(mid + 1, right, delta);       pushUp();     }     int queryMax(int left, int right) {       if (l == left && r == right) return max;       pushDown();       if (right <= mid)         return lc->queryMax(left, right);       else if (left > mid)         return rc->queryMax(left, right);       else         return std::max(lc->queryMax(left, mid), rc->queryMax(mid + 1, right));     }   } * root, pool[MAXN], *tail = pool;   SegmentTree *newNode(int l, int r, SegmentTree *lc, SegmentTree *rc) {     SegmentTree *ret = ++tail;     ret->l = l, ret->r = r, ret->mid = l + (r - l) / 2;     ret->lc = lc, ret->rc = rc;     ret->val = 0, ret->add = 0, ret->max = INT_MIN;     return ret;   }   struct Questions {     int u, v, mid;   } q[MAXN];   struct Ans {     int r, node, pos, ans;   } a1[MAXN], a2[MAXN];   std::vector<Ans> v1[MAXN], v2[MAXN];   void add(int u, int v) {     cnt++;     edge[cnt].t = v;     edge[cnt].next = head[u];     head[u] = cnt;   }   void dfs1(int u, int pre) {     f[u][0] = pre;     dfn[u] = ++ts;     size[u] = 1;     pos[ts] = u;     for (int e = head[u]; e; e = edge[e].next) {       int v = edge[e].t;       if (v == pre) continue;       dist[v] = dist[u] + 1;       deep[v] = deep[u] + 1;       dfs1(v, u);       size[u] += size[v];     }   }   int getLca(int x, int y) {     if (deep[x] > deep[y]) std::swap(x, y);     for (int i = 18; i >= 0; i--) {       if (deep[f[y][i]] >= deep[x]) y = f[y][i];     }     if (x == y) return x;     for (int i = 18; i >= 0; i--) {       if (f[x][i] != f[y][i]) {         x = f[x][i];         y = f[y][i];       }     }     return f[x][0];   }   int getMid(int u, int len) {     int e = 0;     while (len) {       if (len & 1) u = f[u][e];       len >>= 1;       e++;     }     return u;   }   void dfs2(int u, int pre) {     while (a1[t1].r == u && t1 <= m) {       a1[t1].ans = root->queryMax(dfn[a1[t1].node],                                   dfn[a1[t1].node] + size[a1[t1].node] - 1);       t1++;     }     while (a2[t2].r == u && t2 <= m) {       if (dfn[a2[t2].node] > 1)         a2[t2].ans =             std::max(a2[t2].ans, root->queryMax(1, dfn[a2[t2].node] - 1));       if (dfn[a2[t2].node] + size[a2[t2].node] - 1 < n)         a2[t2].ans = std::max(             a2[t2].ans, root->queryMax(dfn[a2[t2].node] + size[a2[t2].node], n));       t2++;     }     for (int e = head[u]; e; e = edge[e].next) {       int v = edge[e].t;       if (v == pre) continue;       root->solveAdd(dfn[v], dfn[v] + size[v] - 1, -1);       if (dfn[v] > 1) root->solveAdd(1, dfn[v] - 1, 1);       if (dfn[v] + size[v] - 1 < n) root->solveAdd(dfn[v] + size[v], n, 1);       dfs2(v, u);       root->solveAdd(dfn[v], dfn[v] + size[v] - 1, 1);       if (dfn[v] > 1) root->solveAdd(1, dfn[v] - 1, -1);       if (dfn[v] + size[v] - 1 < n) root->solveAdd(dfn[v] + size[v], n, -1);     }   }   bool cmp1(Ans x, Ans y) {     if (dfn[x.r] == dfn[y.r]) return dfn[x.node] < dfn[y.node];     return dfn[x.r] < dfn[y.r];   }   bool cmp2(Ans x, Ans y) { return x.pos < y.pos; }   int main(int argc, char *argv[]) {     scanf( %d , &n);     int u, v;     for (int i = 1; i <= n - 1; i++) {       scanf( %d %d , &u, &v);       add(u, v);       add(v, u);     }     deep[1] = 1;     dfs1(1, 0);     root = SegmentTree::build(1, n);     for (int j = 1; j <= 18; j++) {       for (int i = 1; i <= n; i++) {         f[i][j] = f[f[i][j - 1]][j - 1];       }     }     int lca, len, mid;     scanf( %d , &m);     for (int i = 1; i <= m; i++) {       scanf( %d %d , &u, &v);       lca = getLca(u, v);       len = dist[u] + dist[v] - 2 * dist[lca];       if (deep[u] < deep[v]) std::swap(u, v);       mid = getMid(u, (len - 1) / 2);       q[i] = Questions{u, v, mid};       a1[i] = Ans{u, mid, i, INT_MIN};       a2[i] = Ans{v, mid, i, INT_MIN};     }     std::sort(a1 + 1, a1 + m + 1, cmp1);     std::sort(a2 + 1, a2 + m + 1, cmp1);     dfs2(1, 0);     std::sort(a1 + 1, a1 + m + 1, cmp2);     std::sort(a2 + 1, a2 + m + 1, cmp2);     for (int i = 1; i <= m; i++) {       printf( %d n , std::max(a1[i].ans, a2[i].ans));     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int MOD = 998244353;   vector<vector<int>> edges;   long long DP[200010];   long long fac[200010];   void solve(const int u, const int p) {     DP[u] = 1;     int tot = 0;     for (auto& e : edges[u]) {       if (e != p) {         tot++;         solve(e, u);         DP[u] = (DP[u] * DP[e]) % MOD;       }     }     DP[u] = (DP[u] * fac[tot]) % MOD;     if (u != 1 && tot) DP[u] = ((tot + 1) * DP[u]) % MOD;   }   int main() {     fac[0] = 1;     for (int i = 1; i <= 200000; i++) fac[i] = (fac[i - 1] * i) % MOD;     int N, t, f;     scanf( %d , &N);     edges.assign(N + 5, vector<int>());     for (int i = 0; i < N - 1; i++) {       scanf( %d %d , &t, &f);       edges[t].push_back(f);       edges[f].push_back(t);     }     solve(1, 1);     printf( %d n , int((N * DP[1]) % MOD));     return 0;   }


#include <bits/stdc++.h>   long long int lcm, k, ans, a, b;   using namespace std;   int main() {     int n;     cin >> n;     map<int, int> mp;     mp[n] = 1;     n++;     int c = 1;     while (1) {       if (mp[n] == 1 && n % 10 != 0)         break;       else {         if (n % 10 == 0) {           while (n % 10 == 0) n /= 10;         } else {           mp[n] = 1;           n++;           c++;         }       }     }     cout << c <<   n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long double pi = acos(-1);   const long long md = 1e9 + 7;   long long r, b, i, x, y;   int main() {     ios::sync_with_stdio(0);     cin.tie(0);     cin >> r >> b;     for (i = 1; i <= r + b; i++) cin >> x >> y;     cout << (r == b ?  Yes  :  No ) <<   n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n, m, kecil, tamp1 = 0, tamp2 = 0, tambah = 0;     cin >> n;     cin >> m;     tamp1 = n;     tamp2 = m;     while (n != 0) {       n = tamp1;       if (tambah >= 1) m += tamp2;       if (m > n) {         cout << -1;         return 0;       }       for (int i = 0; i < m; i++) {         if (n == m - i)           n -= 1;         else           n -= 2;       }       tambah++;     }     cout << m;   }


#include <bits/stdc++.h>   using namespace std;   void input(long long ara[], long long n) {     for (long long i = 0; i < n; i++) cin >> ara[i];   }   void output(long long ara[], long long n) {     for (long long i = 0; i < n; i++) cout << ara[i] <<    ;   }   long long mx1(long long ara[], long long n) {     long long mx = -1;     for (long long i = 0; i < n; i++)       if (ara[i] > mx) mx = ara[i];     return mx;   }   long long mn1(long long ara[], long long n) {     long long mn = 1e18;     for (long long i = 1; i <= n; i++)       if (ara[i] < mn) mn = ara[i];     return mn;   }   int maxSubArraySum(long long a[], long long size) {     long long max_so_far = INT_MIN, max_ending_here = 0;     for (long long i = 0; i < size; i++) {       max_ending_here = max_ending_here + a[i];       if (max_so_far < max_ending_here) max_so_far = max_ending_here;       if (max_ending_here < 0) max_ending_here = 0;     }     return max_so_far;   }   int main() {     (ios_base::sync_with_stdio(false), cin.tie(NULL));     long long t;     cin >> t;     while (t--) {       long long n;       cin >> n;       long long ara[n + 1];       input(ara, n);       long long k = 0, sum = 0;       for (long long i = 0; i < n; i++) {         sum += ara[i];         if (sum <= 0) {           k = 1;         }       }       sum = 0;       for (long long i = n - 1; i >= 0; i--) {         sum += ara[i];         if (sum <= 0) {           k = 1;           break;         }       }       if (k == 1)         cout <<  NO  << endl;       else         cout <<  YES  << endl;     }   }


#include <bits/stdc++.h>   using namespace std;   __int128 read() {     __int128 x = 0, f = 1;     char ch = getchar();     while (ch <  0  || ch >  9 ) {       if (ch ==  - ) f = -1;       ch = getchar();     }     while (ch >=  0  && ch <=  9 ) {       x = x * 10 + ch -  0 ;       ch = getchar();     }     return x * f;   }   void print(__int128 x) {     if (x < 0) {       putchar( - );       x = -x;     }     if (x > 9) print(x / 10);     putchar(x % 10 +  0 );   }   __int128 gcd(__int128 a, __int128 b) { return b ? gcd(b, a % b) : a; }   __int128 ex_gcd(__int128 a, __int128 b, __int128& x, __int128& y) {     if (b == 0) {       x = 1;       y = 0;       return a;     }     __int128 d = ex_gcd(b, a % b, x, y);     __int128 temp = x;     x = y;     y = temp - a / b * y;     return d;   }   bool liEu(__int128 a, __int128 b, __int128 c, __int128& x, __int128& y) {     __int128 d = ex_gcd(a, b, x, y);     if (c % d != 0) return 0;     __int128 k = c / d;     x *= k;     y *= k;     return 1;   }   signed main() {     __int128 n, p, w, d;     n = read();     p = read();     w = read();     d = read();     __int128 x, y;     if (!liEu(w, d, p, x, y)) return (std::cout <<  -1 n , 0);     __int128 t = d / gcd(w, d);     __int128 xt = (x % t + t) % t;     __int128 yt = (p - w * xt) / d;     if (xt >= 0 && yt >= 0 && xt + yt <= n) {       print(xt);       printf(   );       print(yt);       printf(   );       print(n - xt - yt);       printf(  n );       return 0;     }     t = w / gcd(w, d);     yt = (y % t + t) % t;     xt = (p - d * yt) / w;     if (xt >= 0 && yt >= 0 && xt + yt <= n) {       print(xt);       printf(   );       print(yt);       printf(   );       print(n - xt - yt);       printf(  n );       return 0;     }     std::cout << -1 <<   n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     int n;     cin >> n;     string s;     cin >> s;     int depth = 0;     for (int i = 0; i < n; i++) {       depth += (s[i] ==  (  ? 1 : -1);     }     if (depth != 2 && depth != -2) {       cout <<  0 n ;       return 0;     }     if (depth == 2) {       depth = 0;       int ans = 0;       bool can = true;       for (int i = 0; i < n; i++) {         if (s[i] ==  ( ) {           if (depth - 1 >= 0) {             ans++;           }         }         depth += (s[i] ==  (  ? 1 : -1);         if (depth <= 1) {           ans = 0;         }         if (depth < 0) {           can = false;         }       }       if (!can) {         cout <<  0 n ;         return 0;       }       cout << ans <<   n ;     } else if (depth == -2) {       depth = 0;       int ans = 0;       bool can = true;       for (int i = 0; i < n; i++) {         if (s[i] ==  ) ) {           ans++;         }         depth += (s[i] ==  (  ? 1 : -1);         if (depth < 0) {           break;         }       }       depth = 0;       for (int i = 0; i < n; i++) {         depth += (s[i] ==  (  ? 1 : -1);         if (depth < -2) {           can = false;         }       }       if (!can) {         cout <<  0 n ;         return 0;       }       cout << ans <<   n ;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int MOD = int(1e9) + 7;   int n;   int a[100000], b[100000];   int who[200005];   bool used[200005] = {};   vector<int> g[200005];   int mul;   int far[200005];   int tr[200005];   void dfs(int v) {     ++mul;     used[v] = true;     for (int to : g[v]) {       dfs(to);     }   }   int main() {     scanf( %d , &n);     for (int i = 0; i < (int)(n); ++i) {       scanf( %d%d , a + i, b + i);       --a[i];       --b[i];       g[b[i]].push_back(a[i]);     }     memset(who, -1, sizeof who);     for (int i = 0; i < (int)(n); ++i) who[a[i]] = i;     int ans = 1;     for (int i = 0; i < (int)(2 * n); ++i)       if (who[i] == -1) {         mul = 0;         dfs(i);         ans = (long long)ans * mul % MOD;       }     memset(far, -1, sizeof far);     for (int i = 0; i < (int)(n); ++i) far[a[i]] = b[i];     for (int _ = 0; _ < (int)(25); ++_)       for (int i = 0; i < (int)(2 * n); ++i) {         if (far[i] != -1) {           far[i] = far[far[i]];         }       }     for (int i = 0; i < (int)(n); ++i) tr[a[i]] = b[i];     for (int i = 0; i < (int)(2 * n); ++i)       if (far[i] != -1 && !used[far[i]]) {         int c = 0;         for (int x = far[i]; !used[x]; x = tr[x]) {           ++c;           used[x] = true;         }         if (c > 1) {           ans = 2ll * ans % MOD;         }       }     cout << ans << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int MAXN = 2e5 + 40;   int num[MAXN];   int a[MAXN];   int pref[MAXN];   bool isneg = false;   int n;   int max_k;   int mabs(int x) {     if (x < 0) {       return -x;     }     return x;   }   void neg() {     isneg = !isneg;     for (int i = 0; i < MAXN; ++i) {       num[i] = -num[i];       a[i] = -a[i];     }   }   void bin() {     int carry = 0;     for (int i = 0; i < MAXN; ++i) {       if (mabs(num[i] + carry) % 2 == 0) {         carry = (num[i] + carry) / 2;         num[i] = 0;       } else {         carry = (num[i] + carry - 1) / 2;         num[i] = 1;       }     }   }   void calc_pref() {     pref[0] = num[0];     for (int i = 1; i < MAXN; ++i) {       pref[i] = pref[i - 1] + num[i];     }   }   int cnt_ones(int from, int to) {     if (from < to) {       return 0;     }     return pref[from] - (to > 0 ? pref[to - 1] : 0);   }   void prepare_num() {     int carry = 0;     for (int i = 0; i < MAXN; ++i) {       if (mabs(num[i] + carry) % 2 == 0) {         carry = (num[i] + carry) / 2;       } else {         carry = (num[i] + carry - 1) / 2;       }     }     if (carry < 0) {       neg();     }     bin();   }   void add(int x, int power) {     int k = x < 0 ? -1 : 1;     x = k * x;     while (x != 0) {       num[power++] += k * (x % 2);       x /= 2;     }   }   bool try_for(int index) {     bool zero_allowed = index != n;     int a_i = a[index];     int k = a_i < 0 ? -1 : 1;     a_i *= k;     int carry = 0;     long long cur_num = 0;     int power = 0;     for (int i = 0; i <= 32; ++i) {       int cur_digit = num[index++] - k * (a_i % 2) + carry;       a_i /= 2;       if (mabs(cur_digit) % 2 == 0) {         carry = cur_digit / 2;       } else {         carry = (cur_digit - 1) / 2;         cur_num += 1ll << power;       }       ++power;       if (cnt_ones(MAXN - 1, index) == 0 && carry == -1) {         while (cur_num > 0) {           cur_num += 1ll << (power++);         }         break;       }     }     if (cnt_ones(MAXN - 1, index) != 0 || (carry != 0 && carry != -1) ||         cur_num > max_k || cur_num < -max_k || (cur_num == 0 && !zero_allowed)) {       return false;     } else {       return true;     }   }   int main() {     scanf( %d%d , &n, &max_k);     for (int i = 0; i <= n; ++i) {       scanf( %d , &a[i]);       add(a[i], i);     }     prepare_num();     calc_pref();     int ans = 0;     for (int i = 0; i <= n; ++i) {       if (cnt_ones(i - 1, 0) > 0) {         break;       }       ans += try_for(i);     }     printf( %d n , ans);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long long maxn = 2e5 + 8, inf = 1e18 + 9, mod = 1e9 + 7;   char s[maxn];   long long n, m;   void solve() {     long long i, j, ans = 1;     cin >> n >> (s + 1);     for (i = 2; i <= n; i++)       if (s[i] != s[i - 1]) ans++;     cout << min(ans + 2, n) << endl;   }   signed main() {     ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);     long long t = 1;     while (t--) solve();     return 0;   }


#include <bits/stdc++.h>   using namespace ::std;   const long long maxn = 1e5 + 500;   const long long mod = 1e9 + 7;   const long long inf = 1e9 + 500;   long long out[maxn];   long long in[maxn];   long long a[maxn];   long long b[maxn];   long long dp[2][maxn];   int main() {     fill(out, out + maxn, -1);     fill(in, in + maxn, -1);     long long n, q, t;     cin >> n >> q >> t;     for (long long i = 0; i < n; i++) {       cin >> a[i];     }     for (long long i = 0; i < q; i++) {       long long v, u;       cin >> v >> u;       v--;       u--;       out[u] = v;       in[v] = u;     }     for (long long i = 0; i < n; i++) {       for (long long j = 0; j < n; j++) {         if (out[j] == -1) {           b[j] = a[j];         } else {           if (b[out[j]] != 0) {             b[j] = b[out[j]] + a[j];           }         }       }     }     for (long long i = 0; i < n; i++) {       if (b[i] == 0) {         cout << 0;         return 0;       }       if (in[i] != -1) {         t -= b[i];       }     }     if (t < 0) {       cout << 0;       return 0;     }     dp[1][0] = 1;     for (long long i = 0; i < n; i++) {       for (long long j = 0; j < b[i]; j++) {         dp[i & 1][j] = dp[(i & 1) ^ 1][j];       }       for (long long j = b[i]; j <= t; j++) {         dp[i & 1][j] =             ((long long)dp[(i & 1) ^ 1][j] + dp[(i & 1)][j - b[i]]) % mod;       }     }     cout << dp[(n - 1) & 1][t];   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int T;     cin >> T;     while (T--) {       long long n, k;       cin >> n >> k;       long long cur = 1, now = 1, ans = 0;       while (now <= k && cur < n) {         cur += now;         now <<= 1;         ans++;       }       if (cur < n) {         ans += (n - cur + k - 1) / k;       }       printf( %lld n , ans);     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n, a, b;   int check(int p) {     int res;     res = a / p + b / p;     if (a / p == 0 || b / p == 0) return 0;     if (res >= n)       return 1;     else       return 0;   }   int main() {     int l, r, mid;     scanf( %d%d%d , &n, &a, &b);     r = a + b;     l = 1;     while (r - l > 1) {       mid = (r + l) / 2;       if (check(mid))         l = mid;       else         r = mid;     }     printf( %d n , l);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   struct node {     int u;     long long int cost;     node(int _u, long long int _cost) {       u = _u;       cost = _cost;     }     bool operator<(const node& p) const {       return (long long int)cost < (long long int)p.cost;     }   };   int numNodes, numEdges;   int source;   bool p;   vector<vector<int> > G;   map<pair<int, int>, int> cost;   vector<long long int> dist;   vector<int> par;   void clear() {     G.clear();     cost.clear();     dist.clear();   }   void dijstkra() {     priority_queue<pair<long long int, int>, vector<pair<long long int, int> >,                    greater<pair<long long int, int> > >         q;     dist[source] = 0LL;     q.push({0LL, source});     while (!q.empty()) {       int u = q.top().second;       q.pop();       for (auto v : G[u]) {         if (dist[u] + cost[{u, v}] < dist[v]) {           dist[v] = dist[u] + cost[{u, v}];           par[v] = u;           q.push({dist[v], v});         }       }     }   }   void find_path(int s, int e) {     if ((s == e) || (e == -1)) {       if (p) cout <<    ;       p = true;       cout << s;     } else {       find_path(s, par[e]);       if (p) cout <<    ;       p = true;       cout << e;     }   }   int main() {     ios_base::sync_with_stdio(false);     p = false;     source = 1;     cin >> numNodes >> numEdges;     dist.resize(numNodes + 1, LLONG_MAX);     par.resize(numNodes + 1, -1);     G.resize(numNodes + 1);     for (int i = 1; i <= numEdges; i++) {       int u, v;       int w;       cin >> u >> v >> w;       G[u].push_back(v);       G[v].push_back(u);       cost[pair<int, int>(u, v)] = w;       cost[pair<int, int>(v, u)] = w;     }     dijstkra();     if (par[numNodes] == -1) {       cout <<  -1 n ;       return 0;     }     find_path(source, numNodes);     clear();   }


#include <bits/stdc++.h>   using namespace std;   int Id[2 * 105];   int L[2 * 105];   int W[2 * 105];   long long int dp[2 * 105][3005];   bool vis[2 * 105][3005];   int n, l;   long long int solve(int LastTp, int Len) {     if (Len > l) return 0LL;     if (Len == l) return 1LL;     if (vis[LastTp][Len] == 1) return dp[LastTp][Len];     vis[LastTp][Len] = 1;     long long int ret = 0;     for (int i = 1; i <= n; i++) {       if (Id[i] == Id[LastTp]) continue;       if (W[LastTp] == L[i]) ret = (ret + solve(i, Len + L[i])) % 1000000007;     }     return dp[LastTp][Len] = ret;   }   int main() {     int N, x, y;     scanf( %d %d , &N, &l);     for (int i = 1; i <= N; i++) {       scanf( %d %d , &x, &y);       L[++n] = x;       W[n] = y;       Id[n] = i;       if (x != y) {         L[++n] = y;         W[n] = x;         Id[n] = i;       }     }     long long int Sum = 0;     for (int i = 1; i <= n; i++) Sum = (Sum + solve(i, L[i])) % 1000000007;     cout << Sum << endl;   }


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


#include <bits/stdc++.h>   using namespace std;   int main(int argc, const char* argv[]) {     int n, cnt = 0;     cin >> n;     int a[100];     memset(a, 0, sizeof(a));     for (int i = 0; i < n; i++) {       int k, x = 0;       cin >> k;       while (k != 0) {         x = x + k % 10;         k = k / 10;       }       a[x]++;     }     for (int i = 1; i < 100; i++) {       while (a[i] > 0) {         a[i] = a[i] - 1;         for (int j = i; j < 100; j++) {           if (a[j] > 0 && (i + j) % 3 == 0) {             cnt++;             a[j]--;             break;           }         }       }     }     cout << cnt;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int INF = 1000000000 + 5;   const int N = 2e5 + 5;   const long long oo = 1e18 + 5;   const long long mod = 998244353;   int a[N], b[N];   int mul(int a, int b) { return 1ll * a * b % mod; }   void solve() {     int n, k;     cin >> n >> k;     vector<int> pos(n + 2);     vector<int> lv(n + 2);     for (int i = 1; i <= n; ++i) {       int x;       cin >> x;       a[i] = x;       pos[x] = i;       lv[i] = 0;     }     for (int i = 1; i <= k; ++i) {       int x;       cin >> x;       b[i] = x;       lv[pos[x]] = i;     }     lv[0] = lv[n + 1] = INF;     int ans = 1;     for (int i = 1; i <= n; ++i) {       if (lv[i]) {         if (lv[i] > lv[i - 1] && lv[i] > lv[i + 1])           ans = mul(ans, 2);         else {           if (lv[i] <= lv[i - 1] && lv[i] <= lv[i + 1]) {             cout << 0 <<   n ;             return;           }         }       }     }     cout << ans <<   n ;   }   int main() {     ios::sync_with_stdio(0);     cin.tie(0);     int t;     cin >> t;     while (t--) solve();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   struct Node {     int l, r;     pair<int, int> v;     int mid() { return (l + r) >> 1; }   };   struct HNode {     int m, p;     long long x, y, r, d;     bool operator<(const HNode &cur) const { return d < cur.d; }   };   Node tree[4 * 250005];   HNode hook[250005];   int n;   vector<int> has;   void push_up(int pos) {     tree[pos].v = min(tree[pos << 1].v, tree[pos << 1 | 1].v);   }   void build(int pos, int l, int r) {     tree[pos].l = l;     tree[pos].r = r;     if (l == r) {       tree[pos].v = make_pair(hook[l].m, l);       return;     }     int mid = tree[pos].mid();     build(pos << 1, l, mid);     build(pos << 1 | 1, mid + 1, r);     push_up(pos);   }   void update(int pos, int idx, int mass) {     if (tree[pos].l == tree[pos].r) {       tree[pos].v.first = mass;       return;     }     int mid = tree[pos].mid();     if (idx <= mid)       update(pos << 1, idx, mass);     else       update(pos << 1 | 1, idx, mass);     push_up(pos);   }   pair<int, int> query(int pos, int l, int r) {     if (l <= tree[pos].l && tree[pos].r <= r) return tree[pos].v;     int mid = tree[pos].mid();     pair<int, int> ret = make_pair(0x3fffffff, 0x3fffffff);     if (l <= mid) ret = min(ret, query(pos << 1, l, r));     if (r > mid) ret = min(ret, query(pos << 1 | 1, l, r));     return ret;   }   int bifind(long long v) {     int l = 1, r = n, mid;     if (hook[r].d <= v) return r;     if (hook[l].d > v) return -1;     while (l + 1 < r) {       mid = (l + r) >> 1;       if (hook[mid].d <= v)         l = mid;       else         r = mid;     }     return l;   }   int main() {     int i, j, k;     while (scanf( %I64d%I64d%d%I64d%d , &hook[0].x, &hook[0].y, &hook[0].p,                  &hook[0].r, &n) != EOF) {       for (i = 1; i <= n; i++)         scanf( %I64d%I64d%d%d%I64d , &hook[i].x, &hook[i].y, &hook[i].m,               &hook[i].p, &hook[i].r);       for (i = 0; i <= n; i++) {         hook[i].r *= hook[i].r;         hook[i].d = (hook[0].x - hook[i].x) * (hook[0].x - hook[i].x) +                     (hook[0].y - hook[i].y) * (hook[0].y - hook[i].y);       }       sort(hook + 1, hook + 1 + n);       has.clear();       has.push_back(0);       build(1, 1, n);       for (i = 0; i < has.size(); i++) {         int rs = bifind(hook[has[i]].r);         while (rs != -1) {           pair<int, int> cur = query(1, 1, rs);           if (cur.first <= hook[has[i]].p) {             update(1, cur.second, 0x3fffffff);             has.push_back(cur.second);           } else             break;         }       }       printf( %d n , has.size() - 1);     }     return 0;   }


#include <bits/stdc++.h>   #pragma GCC optimize( -O3 )   #pragma GCC optimize( Ofast )   #pragma GCC optimize( unroll-loops )   using namespace std;   long long n, m, k, i, ost, x, y, ans, a[200010], f[200010], ost1, sum1;   vector<pair<long long, long long>> v3, v1, v2;   pair<long long, long long> sum;   multiset<long long> st, st1;   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     cin >> n >> m >> k;     for (i = 1; i <= n; i++) cin >> a[i];     cin >> x;     for (i = 0; i < x; i++) {       cin >> y;       f[y]++;     }     cin >> x;     for (i = 0; i < x; i++) {       cin >> y;       f[y] += 2;     }     for (i = 1; i <= n; i++) {       if (f[i] == 1) v1.push_back(make_pair(a[i], i));       if (f[i] == 2) v2.push_back(make_pair(a[i], i));       if (f[i] == 3) v3.push_back(make_pair(a[i], i));     }     sort(v1.begin(), v1.end());     sort(v2.begin(), v2.end());     sort(v3.begin(), v3.end());     x = v3.size();     x = min(x, k);     y = k - x;     if (min((long long)v1.size(), (long long)v2.size()) < y or m < x + y * 2) {       cout << -1 << endl;       return 0;     }     for (i = 0; i < x; i++) {       sum.first += v3[i].first;       a[v3[i].second] = 1e18;     }     for (i = 0; i < y; i++) {       sum.first += v1[i].first;       a[v1[i].second] = 1e18;       sum.first += v2[i].first;       a[v2[i].second] = 1e18;     }     sort(a + 1, a + n + 1);     ost = m - x - 2 * y;     for (i = 1; i <= n; i++) {       if (i <= ost)         st1.insert(a[i]), sum.second += a[i];       else         st.insert(a[i]);     }     ans = sum.first + sum.second;     while (x) {       if (min((long long)v1.size(), (long long)v2.size()) <= y or y > k or           x + (y + 1) * 2 - 1 > m)         break;       if (ost > 0) ost--;       x--;       sum.first -= v3[x].first;       sum.first += v1[y].first + v2[y].first;       st.insert(v3[x].first);       auto it = st.find(v1[y].first);       if (it != st.end())         st.erase(it);       else {         it = st1.find(v1[y].first);         st1.erase(it);         sum.second -= v1[y].first;       }       it = st.find(v2[y].first);       if (it != st.end())         st.erase(it);       else {         it = st1.find(v2[y].first);         st1.erase(it);         sum.second -= v2[y].first;       }       y++;       ost1 = st1.size(), sum1 = sum.second;       it = st1.end();       while (ost1 > ost) {         ost1--;         it--;         sum1 -= *it;       }       for (auto it = st.begin(); it != st.end(); it++) {         ost1++;         if (ost1 > ost) break;         sum1 += *it;       }       ans = min(ans, sum.first + sum1);     }     cout << ans << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   vector<vector<int> > ad;   vector<int> cam(0);   vector<bool> vis;   bool k = false;   void dfs(const int &m) {     vis[m] = true;     if (m == ad.size() - 1) {       k = true;     } else       for (int i = 0; i < ad[m].size(); i++) {         if (!vis[ad[m][i]]) dfs(ad[m][i]);         if (k) break;       }     if (k) {       cam.push_back(m);     }   }   int main() {     int n;     cin >> n;     ad.resize(n + 1, vector<int>(0));     vis.resize(n + 1, false);     for (int i = int(2); i < int(n + 1); i += int(1)) {       int x;       cin >> x;       ad[i].push_back(x);       ad[x].push_back(i);     }     dfs(1);     for (int i = cam.size() - 1; i >= 0; i--) cout << cam[i] <<    ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 2e5 + 10;   int a[N];   long long sum[N];   int main() {     int n;     scanf( %d , &n);     for (int i = 1; i <= n; i++) scanf( %d , a + i);     sort(a + 1, a + n + 1);     for (int i = 1; i <= n; i++) sum[i] = sum[i - 1] + a[i];     long long ans_sum = 0;     int ans_len = 0, pos = 1, len = 1;     for (int i = 1; i <= n; i++) {       int l = 0, r = min(i - 1, n - i);       while (l < r) {         int ml = (2 * l + r) / 3, mr = (2 * r + l + 2) / 3;         long long suml = sum[i] - sum[i - ml - 1] + sum[n] - sum[n - ml];         long long sumr = sum[i] - sum[i - mr - 1] + sum[n] - sum[n - mr];         if (suml * (2 * mr + 1) < sumr * (2 * ml + 1))           l = ml + 1;         else           r = mr - 1;       }       long long tmp_sum =           sum[i] - sum[i - l - 1] + sum[n] - sum[n - l] - (l * 2 + 1LL) * a[i];       if (tmp_sum * len > ans_sum * (2 * l + 1))         ans_sum = tmp_sum, ans_len = l, pos = i, len = 2 * l + 1;     }     printf( %d n , ans_len * 2 + 1);     for (int i = pos - ans_len; i <= pos; i++) printf( %d  , a[i]);     for (int i = n - ans_len + 1; i <= n; i++) printf( %d  , a[i]);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int N, p;     scanf( %d%d , &N, &p);     int i;     p--;     string S;     cin >> S;     int sz = S.size(), add = 0, flag = 0, first, last;     for (i = 0; i < sz / 2; i++) {       char c1 = S[i], c2 = S[sz - 1 - i];       if (c1 != c2) {         if (flag == 0) {           first = i;           flag = 1;         }         last = i;       }       add += min(abs(c1 - c2), abs(26 - abs(c1 - c2)));     }     if (p > sz / 2) {       p = sz - 1 - p;     }     if (flag == 0) {       printf( 0 );       return 0;     }     if (p != sz / 2) {       if (last == first) {         printf( %d , abs(p - last) + add);         return 0;       }       if (last < p) {         printf( %d , abs(p - first) + add);         return 0;       }       if (first > p) {         printf( %d , abs(p - last) + add);         return 0;       }       printf(            %d ,           min(2 * (p - first) + (last - p), 2 * (last - p) + (p - first)) + add);       return 0;     }     if (p == sz / 2) {       if (sz % 2) {         printf( %d , p - first + add);         return 0;       } else {         p--;         printf( %d , p - first + add);         return 0;       }     }   }


#include <bits/stdc++.h>   int main() {     int J, M;     float QJ, QM;     int i;     scanf( %d , &J);     scanf( %d , &M);     i = 1;     while (1) {       J = J - i;       if (J < 0) {         printf( Vladik n );         break;       }       i++;       M = M - i;       if (M < 0) {         printf( Valera n );         break;       }       i++;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int a[3001];   void swap(int i, int j) {     int temp = a[i];     a[i] = a[j];     a[j] = temp;   }   int main() {     int n;     cin >> n;     ;     for (int i = 0; i < n; i++) {       cin >> a[i];     }     vector<pair<int, int> > x;     for (int i = 0; i < n; i++) {       int min = a[i];       int sw = i;       for (int j = i + 1; j < n; j++) {         if (a[j] < min) {           min = a[j];           sw = j;         }       }       if (sw != i) {         x.push_back(make_pair(i, sw));         swap(i, sw);       }     }     cout << x.size() << endl;     for (vector<pair<int, int> >::iterator i = x.begin(); i != x.end(); i++) {       cout << (*i).first <<     << (*i).second << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 3e5 + 5;   const int INF = 1e7;   struct info {     pair<int, int> x = {-INF, -1};   };   struct segtree {     int n;     vector<info> t;     segtree(int n) {       this->n = n;       t.resize(n * 4);     }     void Merge(info &node, info &l, info &r) { node.x = max(l.x, r.x); }     void update(int node, int l, int r, int i, int j, int add) {       if (l > j || r < i) {         return;       }       if (l >= i && r <= j) {         t[node].x = {add, i};         return;       }       int mid = (l + r) / 2;       update(node * 2, l, mid, i, j, add);       update(node * 2 + 1, mid + 1, r, i, j, add);       Merge(t[node], t[node * 2], t[node * 2 + 1]);     }     void update(int l, int add) { update(1, 0, n - 1, l, l, add); }     info query(int node, int l, int r, int i, int j) {       if (l > j || r < i) {         info x;         return x;       }       if (l >= i && r <= j) {         return t[node];       }       int mid = (l + r) / 2;       info x = query(node * 2, l, mid, i, j);       info y = query(node * 2 + 1, mid + 1, r, i, j);       info ret;       Merge(ret, x, y);       return ret;     }     pair<int, int> query(int l, int r) {       info ret = query(1, 0, n - 1, l, r);       return ret.x;     }   };   int n;   int a[N];   int b[N];   int dist[N][2];   vector<int> adj[N];   bool vis[N][2];   int p[N][2];   int go[N][2];   int32_t main() {     ios_base::sync_with_stdio(false);     cin.tie(nullptr);     cin >> n;     segtree seg(n + 1);     for (int i = 0; i <= n; i++) {       dist[i][0] = INF;       dist[i][1] = INF;       seg.update(i, INF);     }     for (int i = 1; i <= n; i++) {       cin >> a[i];     }     for (int i = 1; i <= n; i++) {       cin >> b[i];     }     priority_queue<pair<int, pair<int, int> > > pq;     pq.push({0, {n, 0}});     dist[n][0] = 0;     dist[n][1] = 0;     seg.update(n, 0);     while (!pq.empty()) {       pair<int, pair<int, int> > t = pq.top();       pq.pop();       int v = t.second.first;       int state = t.second.second;       if (vis[v][state]) continue;       vis[v][state] = 1;       if (state == 1) {         int koi = v + b[v];         if (dist[koi][0] > dist[v][1]) {           dist[koi][0] = dist[v][1];           go[koi][0] = v;           pq.push({-dist[koi][0], {koi, 0}});         }         continue;       }       int l = v - a[v];       int r = v;       while (true) {         pair<int, int> x = seg.query(l, r);         if (x.first <= dist[v][0] + 1) {           break;         }         int koi = x.second;         dist[koi][1] = dist[v][0] + 1;         go[koi][1] = v;         seg.update(koi, dist[koi][1]);         pq.push({-dist[koi][1], {koi, 1}});       }     }     if (dist[0][0] >= INF) {       cout <<  -1 n ;       return 0;     }     cout << dist[0][0] <<   n ;     pair<int, int> now = {0, 0};     vector<int> ans;     while (1) {       if (now.second == 1) {         ans.push_back(now.first);       }       if (now.first == n) {         break;       }       now.first = go[now.first][now.second];       now.second ^= 1;     };     reverse(ans.begin(), ans.end());     for (int i : ans) cout << i <<    ;     cout <<   n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n, a[111], f[(1 << (23))], x[33];   int main() {     while (cin >> n) {       for (int i = 0, _a = (n); i < _a; i++) cin >> a[i];       if (n == 1) {         cout << 1 << endl;         continue;       }       int res = n + 1;       for (int S = 0, _a = ((1 << (n))); S < _a; S++) f[S] = n + 1;       f[1] = 1;       for (int S = (1), _b = ((1 << (n)) - 2); S <= _b; S++) {         int last = -1;         for (int i = (n - 1), _b = (0); i >= _b; i--)           if ((S & (1 << (i)))) {             last = i;             break;           }         int cur = f[S];         if (last == n - 1) {           res = min(res, cur);           continue;         }         int next = last + 1;         bool ok = false;         int nx = 0;         for (int i = 0, _a = (n); i < _a; i++)           if ((S & (1 << (i)))) x[++nx] = a[i];         sort(x + 1, x + nx + 1);         int j = nx, i = 1;         while (i <= j) {           if (x[i] + x[j] == a[next]) {             ok = true;             break;           } else if (x[i] + x[j] > a[next]) {             --j;           } else             ++i;         }         if (!ok) continue;         int S2 = S | (1 << (next));         f[S2] = min(f[S2], max(cur, __builtin_popcount(S2)));         for (int i = 0, _a = (n); i < _a; i++)           if ((S & (1 << (i)))) {             S2 = S - (1 << (i)) + (1 << (next));             f[S2] = min(f[S2], max(cur, __builtin_popcount(S2)));           }       }       if (res > n)         cout << -1 << endl;       else         cout << res << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     long long a, b;     cin >> a >> b;     long long ans = 0;     while (true) {       if (!a || !b) {         cout << ans;         return 0;       }       if (a == b || (a % b == 0)) {         cout << (a / b + ans);         return 0;       }       if (a > b) {         long long tmp = a / b;         a -= tmp * b;         ans += tmp;       } else {         long long tmp = b / a;         b -= tmp * a;         ans += tmp;       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   template <class T>   bool ckmax(T& x, T y) {     return x < y ? x = y, 1 : 0;   }   template <class T>   bool ckmin(T& x, T y) {     return x > y ? x = y, 1 : 0;   }   inline int read() {     int x = 0, f = 1;     char ch = getchar();     while (!isdigit(ch)) {       if (ch ==  - ) f = 0;       ch = getchar();     }     while (isdigit(ch)) x = x * 10 + ch -  0 , ch = getchar();     return f ? x : -x;   }   inline char rdopt() {     char ch = getchar();     while (!isalpha(ch)) ch = getchar();     return ch;   }   const int N = 1 << 20;   int n, m, t1, t2, nq, pre[N];   long long tr[N], ans[N];   vector<int> e1[N], e2[N], u2[N];   vector<pair<int, int> > u1[N], q[N];   struct dsu {     int F[N], siz[N];     inline void init(const int& n = N - 1) {       for (int i = 1, iend = n; i <= iend; ++i) F[i] = i, siz[i] = 1;     }     inline int anc(int x) { return x == F[x] ? x : F[x] = anc(F[x]); }   } d1, d2;   inline void add(int x, long long d) {     for (int i = x; i <= m; i += i & -i) tr[i] += d;   }   inline long long ask(int x) {     long long res = 0;     for (int i = x; i > 0; i -= i & -i) res += tr[i];     return res;   }   inline int query(int tim) {     int res = 0, lim = ask(tim);     for (int i = 19; i >= 0; --i)       if (res + (1 << i) <= m && tr[res + (1 << i)] < lim)         res += 1 << i, lim -= tr[res];     return res;   }   void dfs2(int u) {     for (int i : u2[u]) add(i, 1);     for (pair<int, int> i : q[u]) pre[i.second] = query(i.first);     for (int v : e2[u]) dfs2(v);     for (int i : u2[u]) add(i, -1);   }   void dfs1(int u) {     for (pair<int, int> i : u1[u]) add(i.first, i.second);     for (pair<int, int> i : q[u])       ans[i.second] = ask(i.first) - ask(pre[i.second]);     for (int v : e1[u]) dfs1(v);     for (pair<int, int> i : u1[u]) add(i.first, -i.second);   }   signed main() {     n = read(), m = read();     d1.init(n), d2.init(n), t1 = t2 = n;     for (int i = 1; i <= m; ++i) {       char op = rdopt();       if (op ==  U ) {         int x = read(), y = read(), fx = d1.anc(x), fy = d1.anc(y);         ++t1, d1.F[fx] = d1.F[fy] = d1.F[t1] = t1,               d1.siz[t1] = d1.siz[fx] + d1.siz[fy];         e1[t1].push_back(fx), e1[t1].push_back(fy);       } else if (op ==  M ) {         int x = read(), y = read(), fx = d2.anc(x), fy = d2.anc(y);         ++t2, d2.F[fx] = d2.F[fy] = d2.F[t2] = t2;         e2[t2].push_back(fx), e2[t2].push_back(fy);       } else if (op ==  A ) {         int x = read(), fx = d1.anc(x);         u1[fx].push_back(make_pair(i, d1.siz[fx]));       } else if (op ==  Z ) {         int x = read(), fx = d2.anc(x);         u2[fx].push_back(i);       } else if (op ==  Q ) {         int x = read();         q[x].push_back(make_pair(i, ++nq));       }     }     for (int i = 1, iend = t2; i <= iend; ++i)       if (d2.F[i] == i) dfs2(i);     for (int i = 1, iend = t1; i <= iend; ++i)       if (d1.F[i] == i) dfs1(i);     for (int i = 1; i <= nq; ++i) printf( %lld n , ans[i]);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 1e6 + 100;   int n, m, k, a[N];   bool mark[N];   int main() {     cin >> n >> m >> k;     for (int i = 1; i <= n; i++) cin >> a[i], mark[a[i]] = 1;     deque<int> que;     int ans = 0;     for (int i = 1; i < N; i++) {       while (que.size() && que.front() <= i - m) que.pop_front();       if (mark[i]) que.push_back(i);       if (que.size() >= k)         while (que.size() >= k) que.pop_back(), ans++;     }     cout << ans << endl;     return 0;   }


#include <bits/stdc++.h>   #pragma GCC optimize(2)   using namespace std;   template <typename T>   void read(T &x) {     x = 0;     char op = getchar();     int F = 1;     while (!isdigit(op)) {       if (op ==  - ) F *= -1;       op = getchar();     }     while (isdigit(op)) {       x = (x << 1) + (x << 3) + op -  0 ;       op = getchar();     }     x *= F;   }   template <typename T, typename... Args>   void read(T &x, Args &...args) {     read(x);     read(args...);   }   template <typename T1, typename T2>   void ckmax(T1 &x, T2 y) {     if (x < y) x = y;   }   template <typename T1, typename T2>   void ckmin(T1 &x, T2 y) {     if (x > y) x = y;   }   int a[1005];   int n, m, c;   void work() {     int rec;     cin >> rec;     if (2 * rec <= c) {       for (int i = 1; i <= n; i++) {         if (a[i] > rec || !a[i]) {           a[i] = rec;           cout << i <<   n ;           break;         }       }     } else {       for (int i = n; i >= 1; i--) {         if (a[i] < rec || !a[i]) {           a[i] = rec;           cout << i <<   n ;           break;         }       }     }     fflush(stdout);   }   int main() {     cin >> n >> m >> c;     while (m--) work();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long long mod = 1e9 + 7;   const int maxn = 1e5 + 100;   long long a[maxn];   map<long long, long long> p[maxn];   vector<int> G[maxn];   long long gcd(long long a, long long b) { return (b == 0) ? a : gcd(b, a % b); }   long long ans = 0;   void dfs(int v, int fa) {     for (auto it : p[fa]) {       long long t = gcd(a[v], it.first);       p[v][t] += it.second;       ans = (ans + t % mod * it.second % mod) % mod;     }     p[v][a[v]] += 1;     ans = (ans + a[v]) % mod;     for (int i = 0; i < G[v].size(); i++) {       int to = G[v][i];       if (to == fa) continue;       dfs(to, v);     }   }   int main() {     std::ios::sync_with_stdio(false);     int N;     cin >> N;     for (int i = 1; i <= N; i++) cin >> a[i];     for (int i = 1; i < N; i++) {       int x;       int y;       cin >> x >> y;       G[x].push_back(y);       G[y].push_back(x);     }     dfs(1, 0);     cout << ans << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n, m;     cin >> n >> m;     string str[n];     for (int i = 0; i < n; i++) cin >> str[i];     int brr[m];     for (int i = 0; i < m; i++) cin >> brr[i];     int count = 0;     for (int i = 0; i < m; i++) {       int arr[5] = {0};       for (int j = 0; j < n; j++) {         arr[str[j][i] -  A ]++;       }       int k = *max_element(arr, arr + 5);       count += k * brr[i];     }     cout << count;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     double n, a, d;     cin >> n >> a >> d;     vector<int> t;     vector<int> v;     double prev = 0;     int bit = 1;     for (int i = 0; i < n; i++) {       double t1, v1;       cin >> t1 >> v1;       double t2 = 0, t3 = 0, tot = 0;       double dis = 0;       t2 = (double)(v1) / double(a);       dis = a * (t2) * (t2);       dis = (float)dis / (float)2;       if (dis >= d) {         double velo = 2 * a * d;         velo = sqrt(velo);         t2 = velo / a;         tot = t2 + (double)t1;       } else {         dis = d - dis;         t3 = (double)dis / (double)v1;         tot = (double)t2 + t3 + (double)t1;       }       if (tot > prev || bit == 1) {         cout << setprecision(12) << tot << endl;         prev = tot;         bit = 0;       } else {         cout << setprecision(12) << prev << endl;         bit = 0;       }     }   }


#include <bits/stdc++.h>   using namespace std;   class Mod {    public:     int mod;     Mod(int mod_ = 1e9 + 7) : mod(mod_) {}     int add(int a, int b) { return (a + b) % mod; }     int mul(long long a, int b) { return (a * b) % mod; }     int pow(long long a, int x) {       int ans = 1;       while (x) {         if (x & 1) {           ans = (ans * a) % mod;         }         x >>= 1;         a = (a * a) % mod;       }       return ans;     }   };   class IO {    public:     static void read(int &val) { scanf( %d , &val); }     static void read(long long &val) { scanf( %lld , &val); }     static void read(char *val) { scanf( %s , val); }     static void read(string &val) {       const int BUFFER_SIZE = 1e5 + 5;       static char buffer[BUFFER_SIZE];       scanf( %s , buffer);       val = buffer;     }     static void read_n(vector<int> &val, int n) {       val.resize(n);       for (int i = 0; i < n; i++) {         scanf( %d , &val[i]);       }     }     static void write(int val) { printf( %d n , val); }     static void write(long long val) { printf( %lld n , val); }     static void write(const char *val) { printf( %s n , val); }     static void write(const string &val) { printf( %s n , val.c_str()); }     static void write(const vector<int> &val) {       for (int i = 0; i < val.size(); i++) {         if (i > 0) {           putchar(   );         }         printf( %d , val[i]);       }       putchar(  n );     }   };   long long gcd_ex(long long a, long long b, long long &x, long long &y) {     if (b == 0) {       x = 1;       y = 0;       return a;     }     long long ans = gcd_ex(b, a % b, y, x);     y -= (a / b) * x;     return ans;   }   class Solver {     long long x;     stringstream ans;     int cnt_ans = 0;     void print(long long x, long long a) {       long long cur = 0;       while (a) {         if (a & 1) {           if (cur > 0) {             ans << cur <<   +   << x << endl;             cnt_ans++;           }           cur += x;         }         a >>= 1;         ans << x <<   +   << x << endl;         cnt_ans++;         x <<= 1;       }     }       public:     void read() { IO::read(x); }     void solve() {       long long e = 1;       while ((e << 1) <= x) {         e <<= 1;       }       print(x, e);       ans << e * x <<   ^   << x << endl;       cnt_ans++;       long long y = ((e * x) ^ x);       long long a, b;       gcd_ex(x, y, a, b);       if (a < 0) {         swap(a, b);         swap(x, y);       }       b = -b;       if (!(a & 1)) {         a += y;         b += x;       }       print(x, a);       print(y, b);       ans << a * x <<   ^   << b * y << endl;       cnt_ans++;       IO::write(cnt_ans);       IO::write(ans.str());     }   };   void init() { ios::sync_with_stdio(false); }   int main() {     init();     int t = 1;     while (t--) {       Solver solver;       solver.read();       solver.solve();     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 1e5 + 1;   map<int, int> m[N];   set<pair<int, int> > s[N];   vector<int> g[N];   int n, c[N];   long long ans[N];   void dfs(int u, int p) {     m[u][c[u]] = 1;     s[u].insert(pair<int, int>(1, c[u]));     ans[u] = c[u];     for (int v : g[u]) {       if (v == p) continue;       dfs(v, u);       if (m[u].size() < m[v].size()) {         m[u].swap(m[v]);         s[u].swap(s[v]);         ans[u] = ans[v];       }     }     for (int v : g[u]) {       if (v == p) continue;       for (auto x : m[v]) {         s[u].erase(pair<int, int>(m[u][x.first], x.first));         m[u][x.first] += x.second;         if (s[u].empty() || m[u][x.first] > (*--s[u].end()).first)           ans[u] = x.first;         else if (m[u][x.first] == (*--s[u].end()).first)           ans[u] += x.first;         s[u].insert(pair<int, int>(m[u][x.first], x.first));       }     }   }   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     cin >> n;     for (int i = 1; i <= n; ++i) cin >> c[i];     for (int i = 1; i < n; ++i) {       int x, y;       cin >> x >> y;       g[x].push_back(y);       g[y].push_back(x);     }     dfs(1, 0);     for (int i = 1; i <= n; ++i) cout << ans[i] <<    ;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 5e5 + 5;   long long INF = 1e18;   int n, s;   vector<int> ans;   long long ansV = 0;   long long dis[N];   int edge[N], val[N];   vector<pair<int, int> > adj[N];   void solve() {     for (int i = 1; i <= n; i++) dis[i] = INF;     priority_queue<pair<long long, int>, vector<pair<long long, int> >,                    greater<pair<long long, int> > >         pq;     dis[s] = 0;     pq.push({dis[s], s});     while (!pq.empty()) {       int u = pq.top().second;       long long Cost = pq.top().first;       pq.pop();       if (Cost > dis[u]) continue;       if (u != s) {         ans.push_back(edge[u]);         ansV += val[edge[u]];       }       for (auto P : adj[u]) {         int v = P.first, idx = P.second;         if (Cost + val[idx] < dis[v]) {           dis[v] = Cost + val[idx];           edge[v] = idx;           pq.push({dis[v], v});         } else if (Cost + val[idx] == dis[v] && val[idx] < val[edge[v]])           edge[v] = idx;       }     }   }   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     int m;     cin >> n >> m;     for (int i = 0; i < m; i++) {       int u, v;       cin >> u >> v >> val[i];       adj[u].push_back({v, i});       adj[v].push_back({u, i});     }     cin >> s;     solve();     sort(ans.begin(), ans.end());     cout << ansV << endl;     for (auto v : ans) cout << v + 1 <<    ;     cout << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long double PI = acosl(-1.0);   const double EPS = 1e-12;   void showBit(int no, int N) {     for (int i = N - 1; i >= 0; i--)       if ((no >> i) & 1)         printf( 1 );       else         printf( 0 );     printf(  n );   }   int binToDec(string s) {     int ln = s.length();     int sum = 0;     for (int i = ln - 1, k = 0; i >= 0; i--, k++)       sum = sum + (s[i] -  0 ) * (1 << k);     return sum;   }   void combination(long long com[][1005]) {     com[0][0] = 1;     int sz = 1000;     for (int i = 1; i <= sz; i++) {       com[i][0] = 1;       for (int j = 1; j <= i; j++) {         com[i][j] = com[i - 1][j] + com[i - 1][j - 1];         com[i][j] = com[i][j] % 1000000007;       }     }   }   int find_par(int cn) {}   int gcd(int u, int v) {     if (v == 0) return u;     return gcd(v, u % v);   }   void insert_ind(int cs, int ci, int s, int e) {     if (s == e) {       return;     }     int mid = (s + e) / 2;     if (ci <= mid)       insert_ind(2 * cs, ci, s, mid);     else       insert_ind(2 * cs + 1, ci, mid + 1, e);   }   void update_ind(int cs, int ci, int s, int e, int val) {     if (s == e) {       return;     }     int mid = (s + e) / 2;     if (ci <= mid)       update_ind(2 * cs, ci, s, mid, val);     else       update_ind(2 * cs + 1, ci, mid + 1, e, val);   }   void update_seg(int cs, int ds, int de, int s, int e, int val) {     if (ds == s && de == e) {       return;     }     int mid = (s + e) / 2;     if (ds <= mid) update_seg(2 * cs, ds, min(mid, de), s, mid, val);     if (de > mid) update_seg(2 * cs + 1, max(mid + 1, ds), de, mid + 1, e, val);   }   void pattern_match(char pat[], const int len, int pi[]) {     pi[1] = 0;     for (int i = 2; i <= len; i++) {       int st = i - 1;       while (st >= 0 && pat[pi[st] + 1] != pat[i]) st = pi[st];       if (st == 0)         pi[i] = 0;       else         pi[i] = pi[st] + 1;     }   }   void pattern_match(char pat[], int pi[], char tar[], const int len1, int gi[]) {     if (pat[0] == tar[0]) gi[0] = 1;     for (int i = 1; i < len1; i++) {       int st = gi[i - 1];       while (pat[st] != tar[i]) {         if (st == 0) {           st = -1;           break;         }         st = pi[st - 1];       }       if (st == -1)         gi[i] = 0;       else         gi[i] = st + 1;     }   }   struct myArray {     int mat[10][10];   };   myArray matrix_mul(myArray mat1, myArray mat2, const int sz) {     int tmp[sz][sz];     memset((tmp), 0, sizeof((tmp)));     for (int r = 0; r < sz; r++)       for (int c = 0; c < sz; c++)         for (int cr = 0; cr < sz; cr++)           tmp[r][c] += mat1.mat[r][cr] * mat2.mat[c][cr];     myArray res;     for (int r = 0; r < sz; r++)       for (int c = 0; c < sz; c++) res.mat[r][c] = tmp[r][c];     return res;   }   myArray unit_matrix(const int sz) {     myArray ma;     memset((ma.mat), 0, sizeof((ma.mat)));     for (int i = 0; i < sz; i++) ma.mat[i][i] = 1;     return ma;   }   myArray matrix_expo(int p, myArray mat, const int sz) {     if (p == 0) {       return unit_matrix(sz);     }     if (p & 1) return matrix_mul(mat, matrix_expo(p - 1, mat, sz), sz);     myArray tmp = matrix_expo(p >> 1, mat, sz);     return matrix_mul(tmp, tmp, sz);   }   int X[] = {0, 0, 1, -1};   int Y[] = {1, -1, 0, 0};   map<string, int> msi;   void cal_state(string s, int tran[][30]) {     int ln = s.length();     string s1 = s;     for (int i = 0; i < ln; i++) {       msi[s1] = ln - i;       s1 = s1.substr(0, ln - i - 1);     }     s1 =   ;     for (int i = 0; i <= ln; i++) {       string ts = s1;       for (int j = 0; j < 26; j++) {         s1 = ts + (char)( a  + j);         int tln = i + 1;         while (msi[s1] == 0) {           s1 = s1.substr(1);           tln--;           if (tln == 0) break;         }         if (tln == 0) s1 =   ;         tran[i][j] = msi[s1];       }       if (i < ln) s1 = s.substr(0, i + 1);     }   }   string s, b;   int tran[105][30];   int n, k;   bool col[105];   bool dp[105][105][105];   int c[105][105][105], ps[105][105][105];   int prp[105][105][105];   void print(int ln, int cs, int rp) {     if (ln == 0) return;     print(ln - 1, ps[ln][cs][rp], prp[ln][cs][rp]);     printf( %c ,  a  + c[ln][cs][rp]);   }   int main() {     cin >> n >> k;     cin >> s;     cal_state(s, tran);     cin >> b;     int ln = b.length();     int cnt = 0;     for (int i = (0); i < (ln); ++i)       if (b[i] ==  1 ) col[i] = true, cnt++;     dp[0][0][0] = true;     ln = s.length();     for (int i = 0; i < n; i++)       for (int j = 0; j <= n; j++)         for (int rp = 0; rp <= cnt; rp++)           if (dp[i][j][rp]) {             for (int ne = 0; ne < k; ne++) {               int ns = tran[j][ne];               if (ns == ln) {                 if (col[i - ln + 1] && rp + 1 <= cnt) {                   dp[i + 1][ns][rp + 1] = true;                   c[i + 1][ns][rp + 1] = ne;                   ps[i + 1][ns][rp + 1] = j;                   prp[i + 1][ns][rp + 1] = rp;                 }               } else {                 dp[i + 1][ns][rp] = true;                 c[i + 1][ns][rp] = ne;                 ps[i + 1][ns][rp] = j;                 prp[i + 1][ns][rp] = rp;               }             }           }     for (int i = 0; i <= n; i++)       if (dp[n][i][cnt] == true) {         print(n, i, cnt);         printf(  n );         return 0;       }     printf( No solution n );     return 0;   }


#include <bits/stdc++.h>   using namespace std;   vector<pair<int, int> > result;   void move(pair<int, int> &from, pair<int, int> &to) {     from.first -= to.first;     to.first *= 2;     result.push_back(make_pair(to.second, from.second));   }   void removeone(pair<int, int> list[3]) {     sort(list, list + 3);     if (list[0].first == 0) {       swap(list[0], list[2]);       return;     }     int q = list[1].first / list[0].first;     for (int x = 1; q; x <<= 1)       if (q & x) {         move(list[1], list[0]);         q -= x;       } else         move(list[2], list[0]);     removeone(list);   }   int main(int argc, char **argv) {     int n;     vector<pair<int, int> > nums;     scanf( %d , &n);     for (int i = 0; i < n; i++) {       int v;       scanf( %d , &v);       if (v) nums.push_back(make_pair(v, i));     }     if (nums.size() < 2) {       printf( -1 n );       return 0;     }     result.clear();     while (nums.size() > 2) {       removeone(&nums[nums.size() - 3]);       nums.pop_back();     }     printf( %d n , (int)result.size());     for (int i = 0; i < (int)result.size(); i++)       printf( %d %d n , result[i].first + 1, result[i].second + 1);     return 0;   }


#include <bits/stdc++.h>   #pragma GCC optimize( O3 )   long long x, y, z, a, b, c, d, n, m, mod = 1000000007;   using namespace std;   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     ;     string s;     bool w = true;     cin >> n >> m >> s;     a = s.find( G );     b = s.find( T );     if (a > b && (a - b) % m == 0) {       for (int i = a; i > b; i -= m) {         if (s[i] ==  # ) {           w = false;           break;         }       }     } else if (a < b && (b - a) % m == 0) {       for (int i = a; i < b; i += m) {         if (s[i] ==  # ) {           w = false;           break;         }       }     }     if (w == false || abs(b - a) % m != 0)       cout <<  NO ;     else       cout <<  YES ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     std::ios::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL);     string a, b;     int k;     cin >> a >> b >> k;     int n = a.length(), l = 0;     long long gd = 0, bd = 0;     int f = 0;     if (a.length() != b.length()) {       cout << 0;       return 0;     }     for (int i = 0; i < n; i++) {       int j;       for (j = 0; j < n; j++)         if (a[(i + j) % n] != a[j]) break;       if (j == n)         gd++;       else         bd++;       for (j = 0; j < n; j++)         if (a[(i + j) % n] != b[j]) break;       if (j == n) f = 1;     }     if (f == 0) {       cout << 0;       return 0;     }     vector<int> dp(2, 0), dp1(2, 0);     if (a == b)       dp[0] = 1;     else       dp1[0] = 1;     long long mod = 1e9 + 7;     for (int i = 1; i <= k; i++) {       long long x = dp[1 - i % 2] * (gd - 1) + dp1[1 - i % 2] * gd;       x %= mod;       dp[i % 2] = x;       x = dp[1 - i % 2] * bd + dp1[1 - i % 2] * (bd - 1);       x %= mod;       dp1[i % 2] = x;     }     cout << dp[k % 2] <<   n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 2e5 + 5;   int gcd(int a, int b) { return b ? gcd(b, a % b) : a; }   void slove() {     int n, m, k;     cin >> n >> m >> k;     int ls = 0;     if (n % 2) {       n--, ls += m / 2;     }     if (m % 2) m--;     int flag = 1;     if (ls > k) flag = 0;     ls += (n * m) / 2;     if (ls < k) flag = 0;     if (ls % 2 != k % 2) flag = 0;     if (flag)       puts( YES );     else       puts( NO );   }   int main() {     int T;     scanf( %d , &T);     while (T--) {       slove();     }     return 0;   }


#include <bits/stdc++.h>   template <class T, class F, class C>   auto binary_search(T l, T r, F func, C cmp) -> T;   auto division(int64_t n, int64_t d) -> int64_t { return n / d; }   int main() {     int64_t n, k, m, d;     std::cin >> n >> k >> m >> d;     int64_t answer = 0;     for (int64_t q_k = 1; q_k <= d; ++q_k) {       int64_t x_max = binary_search(           int64_t(1), m + 1,           [n, q_k, k](int64_t x) { return (division(n, x) - 1) / k + 1 >= q_k; },           [](int64_t l, int64_t r) { return l + 1 >= r; });       if ((division(n, x_max) - 1) / k + 1 != q_k) {         continue;       }       answer = std::max(answer, x_max * q_k);     }     std::cout << answer <<   n ;     return 0;   }   template <class T, class F, class C>   auto binary_search(T l, T r, F func, C cmp) -> T {     bool dir = func(l);     for (auto mid = (l + r) / 2; !cmp(l, r); mid = (l + r) / 2) {       (func(mid) == dir ? l : r) = mid;     }     return l;   }


#include <algorithm>   #include <cmath>   #include <vector>   #include <iostream>   using namespace std;      void solve() {       int n, totalCost, prev, x, cost;       totalCost = 0;       cin >> n;          if (n <= 1) {           cout << 0 <<   n ;           return;       }          cin >> prev;          int m, M;          for (int i = 1; i < n; ++i) {           cin >> x;              if (x < prev) {               m = x;               M = prev;           } else {               m = prev;               M = x;           }              if (m * 2 < M) {               cost = int(ceil(log2(float(M) / float(m)))) - 1;              // cout <<  ->  << m <<     << M <<     << cost <<   n ;               totalCost += cost;           }              prev = x;       }          cout << totalCost <<   n ;   }      int main() {       ios_base::sync_with_stdio(false);       cin.tie(nullptr);          int T;       cin >> T;          for (int t = 0; t < T; ++t) {           solve();       }          return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int mod = 1000000007;   char s[105];   int main() {     int n, nr = 0;     gets(s);     n = strlen(s);     int x = 0;     for (int i = 0; i < n; i++) x = (x * 2 + s[i] -  0 ) % mod;     for (int i = 1; i < n; i++) x = (x * 2) % mod;     printf( %d n , x);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n, m, b, mod;     cin >> n >> m >> b >> mod;     int dp[502][502];     int bugs[n + 2];     for (int i = 0; i < n; i++) cin >> bugs[i];     memset(dp, 0, sizeof dp);     dp[0][0] = 1;     for (int k = 0; k < n; k++) {       for (int j = 0; j <= b - bugs[k]; j++) {         for (int i = 0; i < m; i++) {           dp[i + 1][j + bugs[k]] += dp[i][j];           dp[i + 1][j + bugs[k]] %= mod;         }       }     }     int ans = 0;     for (int i = 0; i <= b; i++) {       ans += dp[m][i];       ans %= mod;     }     cout << ans;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   mt19937_64 rang(       chrono::high_resolution_clock::now().time_since_epoch().count());   int rng(int lim) {     uniform_int_distribution<int> uid(0, lim - 1);     return uid(rang);   }   const int INF = 0x3f3f3f3f;   const int mod = 1e9 + 7;   void solve() {     int sum, limit;     cin >> sum >> limit;     vector<int> res;     for (int i = limit; i >= 1; i--) {       int lowbit = i ^ (i & (i - 1));       if (sum >= lowbit) res.push_back(i), sum -= lowbit;       if (sum == 0) break;     }     if (sum != 0)       cout << -1;     else {       cout << res.size() <<   n ;       for (auto x : res) cout << x <<    ;     }   }   int main() {     ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);     srand(chrono::high_resolution_clock::now().time_since_epoch().count());     int t = 1;     while (t--) {       solve();     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = (int)4e6 + 11;   const long long inf = 1e9 + 7;   int n, m, p[1 << 20], can[1 << 20];   int find_parent(int x) {     if (x == p[x]) {       return x;     }     return p[x] = find_parent(p[x]);   }   int main() {     scanf( %d %d , &n, &m);     for (int i = 0; i < 1 + n; ++i) {       p[i] = i;     }     memset(can, -1, sizeof(can));     for (int i = 0; i < m; ++i) {       int l, r, x;       scanf( %d %d %d , &l, &r, &x);       --x;       l = find_parent(l - 1);       while (l < r) {         if (l != x) {           can[l] = x;           p[l] = 1 + l;         }         l = find_parent(1 + l);       }     }     for (int i = 0; i < n; ++i) {       printf( %d  , can[i] + 1);     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int maxn = 110, alpha = 26;   int n, m;   int dp[maxn][maxn][alpha];   bool mark[maxn];   vector<int> ts;   vector<pair<int, int> > G[maxn];   void dfs(int a) {     mark[a] = 1;     for (pair<int, int> p : G[a]) {       int b = p.first;       if (!mark[b]) {         dfs(b);       }     }     ts.push_back(a);   }   int32_t main() {     cin >> n >> m;     for (int i = int(0); i < int(m); i++) {       int a, b;       char c;       cin >> a >> b >> c;       a--, b--;       G[a].push_back({b, c -  a });     }     for (int i = int(0); i < int(n); i++) {       if (!mark[i]) {         dfs(i);       }     }     for (int size = int(0); size < int(2 * n - 1); size++) {       for (int i = int(0); i < int(min(size + 1, n)); i++) {         int j = size - i;         if (j >= n) continue;         int a = ts[i], b = ts[j];         for (int x = int(0); x < int(alpha); x++) {           for (pair<int, int> p : G[a]) {             int ap = p.first, c = p.second;             if (c >= x) dp[a][b][x] = max(dp[a][b][x], 1 - dp[b][ap][c]);           }         }       }     }     for (int i = int(0); i < int(n); i++) {       for (int j = int(0); j < int(n); j++) {         cout << (dp[i][j][0] ?  A  :  B );       }       cout << endl;     }   }


#include <bits/stdc++.h>   using namespace std;   int a[105];   int main() {     int n, i, f;     cin >> n;     for (i = 0; i < n; i++) cin >> a[i];     sort(a, a + n);     f = 0;     for (i = 1; i < n; i++) {       if (a[i] != a[0]) {         f = 1;         break;       }     }     if (f == 1)       cout << a[i];     else       cout <<  NO ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int maxn = 110;   string s[maxn];   int cur[maxn], c[maxn];   int n, m, ans = 0;   void dfs(int x) {     if (x == n) {       int flag = 1;       for (int i = 1; i <= m; i++)         if (cur[i] - c[i]) flag = 0;       ans += flag;       return;     }     for (char v =  0 ; v <=  1 ; v++) {       int can = 1;       for (int i = 1; i <= m; i++) {         cur[i] += (v == s[i][x]);         if (cur[i] > c[i]) can = 0;       }       if (can) dfs(x + 1);       for (int i = 1; i <= m; i++) cur[i] -= (v == s[i][x]);     }   }   int main() {     cin >> n >> m;     for (int i = 1; i <= m; i++) cin >> s[i] >> c[i];     memset(cur, 0, sizeof(cur));     dfs(0);     cout << ans << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   struct Hint {     int up, cnt;     bool operator<(Hint o) const { return up < o.up; }   };   int n, b, q;   Hint h[10013];   int has[10013][5], take[10013];   int main() {     scanf( %d%d%d , &n, &b, &q);     for (int i = 1; i <= q; i++) {       scanf( %d%d , &h[i].up, &h[i].cnt);     }     h[++q] = {b, n};     sort(h, h + q + 1);     for (int i = 1; i <= q; i++) {       for (int j = h[i - 1].up + 1; j <= h[i].up; j++) has[i][j % 5] += 1;       take[i] = h[i].cnt - h[i - 1].cnt;       if (0 > take[i] || take[i] > h[i].up - h[i - 1].up)         return printf( unfair n ), 0;     }     for (int i = 1; i < 32; i++) {       int need = n / 5 * __builtin_popcount(i);       int can = 0;       for (int k = 1; k <= q; k++) {         int cur = 0;         for (int j = 0; j < 5; j++)           if (i & (1 << j)) {             cur += has[k][j];           }         can += min(cur, take[k]);       }       if (need > can) return printf( unfair n ), 0;     }     printf( fair n );     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 60;   map<double, int> p;   bitset<N * N> g[N * N + 5];   int n, m;   double ya[N + 5], yb[N + 5];   int main() {     scanf( %d%d , &n, &m);     for (int i = 1; i <= n; i++) scanf( %lf , &ya[i]);     for (int i = 1; i <= m; i++) scanf( %lf , &yb[i]);     int tot = 0;     for (int i = 1; i <= n; i++) {       for (int j = 1; j <= m; j++) {         double now = (ya[i] + yb[j]) / 2;         if (p.find(now) == p.end()) tot++, p[now] = tot;         g[p[now]].set(i, 1);         g[p[now]].set(j + n, 1);       }     }     int ans = -1;     for (int i = 1; i <= tot; i++) {       for (int j = i; j <= tot; j++) {         bitset<N * N> s;         s = g[i] | g[j];         ans = max(ans, (int)s.count());       }     }     printf( %d n , ans);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 1e6 + 100;   int ans;   int main() {     ios_base::sync_with_stdio(false);     int n;     cin >> n;     int a[1111] = {}, cnt[1111] = {};     for (int i = 1; i <= n; i++) {       cin >> a[i];     }     sort(a + 1, a + n + 1);     for (int i = 1; i <= n; i++) {       if (cnt[i] == 0) {         ans++;         for (int j = i + 1; j <= n; j++) {           if (a[j] % a[i] == 0) {             cnt[j] = 1;           }         }       }     }     cout << ans;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int t;     cin >> t;     while (t--) {       long long n, k;       cin >> n;       vector<long long> dp(n + 3, 0);       vector<long long> v;       long long sum = 0;       for (int j = 0; j < n; j++) {         cin >> k;         if (j % 2 == 0) {           sum = sum + k;         }         v.push_back(k);       }       dp[0] = 0;       long long ans = 0;       long long z = 0;       if (n > 1) {         dp[1] = max(z, v[1] - v[0]);         ans = max(ans, dp[1]);       }       k = 2;       for (int j = 1; j < n - 1; j++) {         if (j % 2 == 0) {           dp[k] = max(z, dp[k - 2] + (v[j + 1] - v[j]));         } else {           dp[k] = max(z, dp[k - 2] + (v[j] - v[j + 1]));         }         ans = max(ans, dp[k]);         k++;       }       cout << sum + ans << endl;     }   }


#include <stdio.h>      int main() {       int t;       scanf( %d , &t);       while (t--) {           int x, i;           scanf( %d , &x);           for (i = 1; i * (i + 1) / 2 < x; i++);           if (i * (i + 1) / 2 - x != 1) {               printf( %d n , i);           } else {               printf( %d n , i + 1);           }       }   }


#include <bits/stdc++.h>   template <typename Y>   inline bool updmin(Y &a, Y b) {     if (a > b) {       a = b;       return 1;     }     return 0;   }   template <typename Y>   inline bool updmax(Y &a, Y b) {     if (a < b) {       a = b;       return 1;     }     return 0;   }   template <typename Y>   inline Y abs(Y a) {     if (a < 0) a = -a;     return a;   }   template <typename Y>   inline Y sqr(Y a) {     return a * a;   }   int read() {     int w = 1, q = 0, ch =    ;     for (; ch <  0  || ch >  9 ; ch = getchar())       if (ch ==  - ) w = -1;     for (; ch >=  0  && ch <=  9 ; ch = getchar()) q = q * 10 + ch - 48;     return q * w;   }   inline void FileIO() {     freopen(                     .in ,          r , stdin);     freopen(                     .out ,          w , stdout);   }   int n, m, h;   const int N = 100020;   std::vector<int> G[N], s;   int tim = 0, cnt = 0;   int low[N], dfn[N], vis[N], bel[N];   void tarjan(int p) {     vis[p] = 1;     s.push_back(p);     low[p] = dfn[p] = ++tim;     for (int t : G[p]) {       if (!dfn[t]) {         tarjan(t);         updmin(low[p], low[t]);       } else if (vis[t]) {         updmin(low[p], dfn[t]);       }     }     if (low[p] == dfn[p]) {       ++cnt;       while (s.back() != p) {         int cur = s.back();         s.pop_back();         vis[cur] = 0;         bel[cur] = cnt;       }       bel[p] = cnt;       vis[p] = 0;       s.pop_back();     }   }   int u[N], size[N], out[N];   int main() {     n = read();     m = read();     h = read();     for (int i = 1; i <= n; i++) {       u[i] = read();     }     for (int i = 1; i <= m; i++) {       int x = read(), y = read();       if ((u[x] + 1) % h == u[y]) {         G[x].push_back(y);       }       if ((u[y] + 1) % h == u[x]) {         G[y].push_back(x);       }     }     for (int i = 1; i <= n; i++) {       if (!dfn[i]) {         tarjan(i);       }     }     for (int i = 1; i <= n; i++) {       size[bel[i]]++;       for (int j : G[i]) {         if (bel[i] != bel[j]) {           out[bel[i]]++;         }       }     }     int ans = n + 1, pos = 0;     for (int i = 1; i <= cnt; i++) {       if (!out[i] && updmin(ans, size[i])) {         pos = i;       }     }     printf( %d n , ans);     for (int i = 1; i <= n; i++) {       if (bel[i] == pos) {         printf( %d  , i);       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     string s;     cin >> s;     int n = s.size();     string r;     int i = 0;     while (i < n) {       if (n - i >= 3) {         if (s.substr(i, 3) ==  WUB ) {           if (r.size() && r.back() !=    ) r +=    ;           i += 3;         } else           r += s[i++];       } else {         r += s[i++];       }     }     cout << r << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long ara[12];   int main() {     long long n, i, j, k;     for (i = 0; i < 4; i++) {       cin >> k;       ara[i] = k;     }     if (ara[0] == 0 && ara[3] == 0 && ara[2] > 0) {       printf( 0 n );     } else {       if (ara[0] == ara[3])         printf( 1 n );       else         printf( 0 n );     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   bool isvowel(char c) {     return c ==  a  || c ==  e  || c ==  i  || c ==  o  || c ==  u ;   }   int main() {     char arr[110];     scanf( %s , arr);     int len = strlen(arr);     bool check = true;     for (int i = 0; i < len; i++) {       if (isvowel(arr[i])) {       } else if (arr[i] ==  n ) {       } else {         if (i == len - 1 || !isvowel(arr[i + 1])) check = false;       }     }     puts(check ?  YES  :  NO );     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long int power(long long int a, long long int x) {     long long int res = 1;     while (x > 0) {       if (x & 1) {         res *= a;       }       a *= a;       x = x / 2;     }     return res;   }   int main() {     long long int n;     scanf( %lld , &n);     long long int ans = 0;     ans = power(4, n - 2) * 6;     if (n > 3) {       ans += (n - 3) * 4 * 9 * power(4, n - 4);     }     printf( %lld n , ans);   }


#include <bits/stdc++.h>   using namespace std;   const int A = 1e5 + 11;   const int B = 1e6 + 11;   const int mod = 1e9 + 7;   const int inf = 0x3f3f3f3f;   inline int read() {     char c = getchar();     int x = 0, f = 1;     for (; !isdigit(c); c = getchar())       if (c ==  - ) f = -1;     for (; isdigit(c); c = getchar()) x = x * 10 + (c ^ 48);     return x * f;   }   struct edgexx {     int v, nxt, w;   } edge[B];   struct edgex {     int v, nxt;   } edge_[B];   struct node {     long long d;     int id;     bool operator<(const node &a) const { return d > a.d; }   };   struct xxs {     int u, v;     long long w;     bool operator<(const xxs &e) const { return w < e.w; }   } e[B];   int n, cnt, js, k, c, t, head[B], m, q, head2[B], fa[B], u_fa[B];   long long dis[B], wnd[B];   int sz[B], son[B], top[B], dep[B];   priority_queue<node> pq;   void add1(int u, int v, int w) {     edge[++cnt].nxt = head[u];     edge[cnt].v = v;     edge[cnt].w = w;     head[u] = cnt;   }   void add2(int u, int v) {     edge_[++js].nxt = head2[u];     edge_[js].v = v;     head2[u] = js;   }   int find(int x) {     if (x == u_fa[x]) return x;     return u_fa[x] = find(u_fa[x]);   }   void dfs1(int u, int f) {     sz[u] = 1;     dep[u] = dep[fa[u] = f] + 1;     for (int i = head2[u]; i; i = edge_[i].nxt) {       int v = edge_[i].v;       if (v == f) continue;       dfs1(v, u);       sz[u] += sz[v];       if (sz[v] > sz[son[u]]) son[u] = v;     }   }   void dfs2(int u, int topf) {     top[u] = topf;     if (son[u]) dfs2(son[u], topf);     for (int i = head2[u]; i; i = edge_[i].nxt) {       int v = edge_[i].v;       if (v == fa[u] || v == son[u]) continue;       dfs2(v, v);     }   }   int lca(int u, int v) {     while (top[u] != top[v]) {       if (dep[top[u]] < dep[top[v]]) swap(u, v);       u = fa[top[u]];     }     return dep[u] < dep[v] ? u : v;   }   int main() {     cin >> n >> m >> k >> q;     for (int i = 1; i <= m; i++) {       int x = read(), y = read(), z = read();       add1(x, y, z);       add1(y, x, z);       e[i] = (xxs){x, y, z};     }     memset(dis, 0x3f, sizeof(dis));     for (int i = 1; i <= k; i++) pq.push((node){dis[i] = 0, i});     while (!pq.empty()) {       long long d = pq.top().d;       int u = pq.top().id;       pq.pop();       if (d != dis[u]) continue;       for (int i = head[u]; i; i = edge[i].nxt) {         int v = edge[i].v;         if (dis[v] > d + edge[i].w) pq.push((node){dis[v] = d + edge[i].w, v});       }     }     for (int i = 1; i <= m; i++) e[i].w += dis[e[i].u] + dis[e[i].v];     sort(e + 1, e + 1 + m);     for (int i = 1; i <= 2 * n; i++) u_fa[i] = i;     cnt = n;     for (int i = 1; i <= m; i++) {       int u = e[i].u, v = e[i].v;       long long w = e[i].w;       u = find(u);       v = find(v);       if (u == v) continue;       u_fa[u] = u_fa[v] = ++cnt;       wnd[cnt] = w;       add2(cnt, u);       add2(cnt, v);     }     dfs1(cnt, 0);     dfs2(cnt, cnt);     while (q--) {       int u = read(), v = read();       printf( %lld n , wnd[lca(u, v)]);     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   map<int, int> mp;   vector<int> object, ans;   map<int, int> counter;   int gcd(int a, int b) { return a == 0 ? b : gcd(b % a, a); }   int main() {     int n, num;     scanf( %d , &n);     for (int i = 0; i < n * n; i++) {       scanf( %d , &num);       object.push_back(num);     }     sort(object.begin(), object.end());     for (int i = object.size() - 1; i >= 0; i--) {       if (counter[object[i]]) {         counter[object[i]]--;         continue;       }       for (int j = 0; j < ans.size(); j++) {         int temp = gcd(ans[j], object[i]);         counter[temp] += 2;       }       ans.push_back(object[i]);     }     for (int i = 0; i < n; i++) {       if (i) printf(   );       printf( %d , ans[i]);     }     printf(  n );     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int MAXN = 100005;   vector<long long> g[100005];   long long vis[MAXN], s[MAXN], gc[MAXN];   long long n;   void dfs(long long v) {     vis[v] = 1;     s[v] = 1;     for (auto i : g[v]) {       if (!vis[i]) {         dfs(i);         s[v] += s[i];         gc[v] = max(gc[v], s[i]);       }     }     gc[v] = max(gc[v], n - s[v]);   }   int pa[MAXN], sz[MAXN];   long long find_pa(long long pos) {     if (pa[pos] != pos) pa[pos] = find_pa(pa[pos]);     return pa[pos];   }   void join(int a, int b) {     int x = find_pa(a), y = find_pa(b);     if (x != y) {       if (sz[x] >= sz[y])         sz[x] += sz[y], pa[y] = x;       else         sz[y] += sz[x], pa[x] = y;     }   }   void dsu_init(long long n) {     for (long long i = 0, ggdem = n; i < ggdem; ++i) pa[i] = i, sz[i] = 1;   }   int main() {     ios::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     long long t;     cin >> t;     while (t--) {       cin >> n;       for (long long i = 0, ggdem = n - 1; i < ggdem; ++i) {         long long u, v;         cin >> u >> v;         u--;         v--;         g[u].push_back(v);         g[v].push_back(u);       }       dfs(0);       long long mini = n;       for (long long i = 0, ggdem = n; i < ggdem; ++i) {         mini = min(mini, gc[i]);       }       vector<long long> v;       for (long long i = 0, ggdem = n; i < ggdem; ++i) {         if (gc[i] == mini) v.push_back(i);       }       assert(((int)v.size()) <= 2);       sort(v.begin(), v.end());       if (((int)v.size()) == 1) {         for (long long k = 0, ggdem = 2; k < ggdem; ++k)           cout << 1 <<     << g[0][0] + 1 <<   n ;       } else {         assert(mini == n / 2 && n % 2 == 0);         dsu_init(n);         for (long long i = 0, ggdem = n; i < ggdem; ++i) {           for (auto j : g[i]) {             if (min(i, j) != v[0] || max(i, j) != v[1]) {               join(i, j);             }           }         }         long long br = 0;         for (long long i = 0, ggdem = n; i < ggdem; ++i) {           if (((int)g[i].size()) == 1 && i != v[0] &&               find_pa(i) != find_pa(v[1])) {             cout << i + 1 <<     << g[i][0] + 1 <<   n ;             cout << i + 1 <<     << v[1] + 1 <<   n ;             br++;             break;           }         }         assert(br);       }       for (long long i = 0, ggdem = n; i < ggdem; ++i) g[i].clear();       for (long long i = 0, ggdem = n; i < ggdem; ++i)         vis[i] = 0, s[i] = 0, gc[i] = 0;     }     return 0;   }