#include <bits/stdc++.h>   using namespace std;   const int mod = 1000000007;   int cnt[4];   int main() {     int n;     scanf( %d , &n);     getchar();     for (int i = 0; i < n; ++i) {       char ch = getchar();       if (ch ==  A ) {         ++cnt[0];       } else if (ch ==  C ) {         ++cnt[1];       } else if (ch ==  G ) {         ++cnt[2];       } else if (ch ==  T ) {         ++cnt[3];       }     }     int mx = *max_element(cnt, cnt + 4);     int nMax = count(cnt, cnt + 4, mx);     long long ans = 1;     for (int i = 0; i < n; ++i) {       ans = (ans * nMax) % mod;     }     cout << ans << endl;   }


#include <bits/stdc++.h>   using namespace std;   const long long mod = 1e9 + 7;   const long long MAX = 1e6 + 10;   long long a[100005];   int main() {     long long n, k, m, sum, t, len;     string s, d;     while (cin >> n) {       int flag = 1;       sum = 0;       if (n % 2 == 0) {         cout << n / 2 << endl;         for (long long i = (1); i <= (n / 2); ++i) cout <<  2  <<    ;         cout << endl;       } else {         cout << (n - 3) / 2 + 1 << endl;         for (long long i = (1); i <= ((n - 3) / 2); ++i) cout <<  2  <<    ;         cout <<  3  << endl;       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long a[200010], t;   int n;   long long b[200010];   int g[200010];   bool no[200010];   int main() {     scanf( %d %I64d , &n, &t);     for (int i = 1; i <= n; i++) scanf( %I64d , &a[i]);     for (int i = 1; i <= n; i++) {       int x;       scanf( %d , &x);       if (x < i) {         printf( No n );         return 0;       }       g[i]++;       g[x]--;       no[x] = 1;     }     int now = 0;     b[0] = 0;     for (int i = 1; i <= n; i++) {       now += g[i];       if (now > 0) {         if (no[i]) {           printf( No n );           return 0;         }         b[i] = max(a[i + 1] + t, b[i - 1] + 1);       } else {         b[i] = max(a[i] + t, b[i - 1] + 1);         if (no[i] && (b[i] >= a[i + 1] + t && i != n)) {           printf( No n );           return 0;         }       }     }     printf( Yes n );     for (int i = 1; i <= n; i++) printf( %I64d  , b[i]);     printf(  n );     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long long mod = 998244353;   const int maxn = 2e5 + 100;   long long dp[52][52][52];   bool vis[52][52][52];   long long val[52][52][52];   int a[maxn], w[maxn];   long long qmod(long long a, long long b) {     long long ans = 1;     while (b) {       if (b & 1) ans = ans * a % mod;       b >>= 1;       a = a * a % mod;     }     return ans;   }   int flag;   int dfs(int n, int i, int j, int k, long long p, long long v, long long sp,           long long sn) {     if (!n) {       return 1LL * p * v % mod;     }     if (vis[i][j][k]) {       return val[i][j][k] * qmod(dp[i][j][k], mod - 2) % mod * p % mod;     }     dp[i][j][k] = p;     val[i][j][k] +=         dfs(n - 1, i + 1, j, k,             1LL * p * sp % mod * qmod(v + sp + sn, mod - 2) % mod, v, sp + 1, sn);     val[i][j][k] += dfs(n - 1, i, j + 1, k,                         1LL * p * sn % mod * qmod(v + sp + sn, mod - 2) % mod, v,                         sp, max(sn - 1, 0LL));     val[i][j][k] += dfs(n - 1, i, j, k + 1,                         1LL * p * v % mod * qmod(v + sp + sn, mod - 2) % mod,                         max(v + flag, 0LL), sp, sn);     val[i][j][k] %= mod;     vis[i][j][k] = 1;     return val[i][j][k];   }   int main() {     ios::sync_with_stdio(false);     long long sn = 0, sp = 0;     int n, m;     cin >> n >> m;     for (int i = 0; i < n; ++i) {       cin >> a[i];     }     for (int i = 0; i < n; ++i) {       cin >> w[i];       if (a[i]) {         sp += w[i];       } else         sn += w[i];     }     for (int i = 0; i < n; ++i) {       memset(dp, 0, sizeof dp);       memset(vis, 0, sizeof vis);       memset(val, 0, sizeof val);       if (a[i]) {         flag = 1;         sp -= w[i];       } else {         flag = -1;         sn -= w[i];       }       cout << dfs(m, 0, 0, 0, 1, w[i], sp, sn) << endl;       if (a[i]) {         sp += w[i];       } else         sn += w[i];     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   using ll = long long;   using ii = pair<int, int>;   const int N = 200005;   const int M = 300;   const int OFF = N * M;   int _cnt[N + N * M];   int *cnt = _cnt + OFF;   int main() {     char s[N];     scanf( %s , s);     int n = strlen(s);     int psum[N];     for (int i = 0; i < n; i++) {       s[i] -=  0 ;       psum[i] = s[i];       if (i) psum[i] += psum[i - 1];     }     int sq = min(M, (int)sqrt(n));     ll ans = 0;     for (int j = 1; j < sq; j++) {       cnt[-1] = 1;       for (int i = 0; i < n; i++) {         int val = i - j * psum[i];         if (psum[i]) {           ans += cnt[val];         }         cnt[val]++;       }       for (int i = 0; i < n; i++) {         int val = i - j * psum[i];         cnt[val]--;       }     }     int nxt[N];     nxt[n] = n;     for (int i = n - 1; i >= 0; i--) {       nxt[i] = nxt[i + 1];       if (s[i] == 1) nxt[i] = i;     }     for (int i = 0; i < n; i++) {       int r = nxt[i];       int q = 1;       while (r < n && q * sq <= n - i) {         int p = nxt[r + 1];         int rig = max(r, i + q * sq - 1);         if (rig <= p) {           ans += (p - i) / q - (rig - i) / q;         }         r = p;         q++;       }     }     printf( %lld n , ans);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int MAXN = 2005, MOD = 998244353;   char ch[MAXN];   long long cnt[MAXN], dp[MAXN][MAXN];   long long KSM(long long a, long long t) {     long long ans = 1;     while (t) {       if (t & 1) ans = ans * a % MOD;       t >>= 1;       a = a * a % MOD;     }     return ans;   }   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     cin >> (ch + 1);     int n = strlen(ch + 1);     for (int i = 1; i <= n; i++) cnt[i] = cnt[i - 1] + (ch[i] ==  ? );     for (int j = 1; j <= n; j++)       for (int i = j - 1; i; i--) {         if (ch[i] !=  )  && ch[j] !=  ( )           dp[i][j] =               (dp[i][j] + dp[i + 1][j - 1] + KSM(2, cnt[j - 1] - cnt[i])) % MOD;         if (ch[i] !=  ( ) dp[i][j] = (dp[i][j] + dp[i + 1][j]) % MOD;         if (ch[j] !=  ) ) dp[i][j] = (dp[i][j] + dp[i][j - 1]) % MOD;         if (ch[i] !=  (  && ch[j] !=  ) )           dp[i][j] = (dp[i][j] - dp[i + 1][j - 1] + MOD) % MOD;       }     cout << dp[1][n];   }


#include <bits/stdc++.h>   #pragma GCC optimize( O3 )   using namespace std;   const int MAX = 2e3 + 5;   const long long MAX2 = 11;   const long long MOD = 998244353;   const long long MOD2 = 1000005329;   const long long INF = 2e18;   const int dr[] = {1, 0, -1, 0, 1, 1, -1, -1, 0};   const int dc[] = {0, 1, 0, -1, 1, -1, 1, -1, 0};   const double pi = acos(-1);   const double EPS = 1e-9;   const int block = 2000;   int q, x, y, cnt;   map<string, pair<long long, long long>> ans, dp;   map<string, bool> jen;   map<string, vector<string>> isi;   string s, ty, c;   pair<long long, long long> solve(string o) {     if (!jen.count(o)) return ans[o];     if (isi[o].empty()) return {0, 0};     if (dp.count(o)) return dp[o];     int hit = isi[o].size() - 1;     long long mx = 0, ttl = 0;     pair<long long, long long> res;     if (jen[o]) {       for (auto i : isi[o]) {         res = solve(i);         mx = max(mx, res.second);         ttl += res.first;       }       return dp[o] = {ttl + 2 * ans[o].first + hit * ans[o].second,                       mx + 2 * ans[o].first};     }     {       for (auto i : isi[o]) {         res = solve(i);         mx = max(mx, res.first);         ttl += res.second;       }       return dp[o] = {mx + 2 * ans[o].first,                       ttl + 2 * ans[o].first + hit * ans[o].second};     }   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     cin >> q;     while (q--) {       cin >> ty;       x = y = cnt = 0;       if (ty ==  Widget ) {         cin >> s;         c =   ;         for (auto i : s) {           if (i ==  (  || i ==  )  || i ==  , )             ++cnt;           else if (cnt == 1)             x = x * 10 + i -  0 ;           else if (cnt == 2)             y = y * 10 + i -  0 ;           else             c.push_back(i);         }         ans[c] = {x, y};       } else if (ty ==  VBox ) {         cin >> s;         ans[s];         jen[s];       } else if (ty ==  HBox ) {         cin >> s;         ans[s];         jen[s] = 1;       } else {         s = c =   ;         for (auto i : ty) {           if (i ==  . )             ++cnt;           else if (i ==  ( ) {             if (c ==  pack )               cnt = 2;             else if (c ==  set_border )               cnt = 3;             else if (c ==  set_spacing )               cnt = 4;             c =   ;           } else if (i ==  ) )             continue;           else if (cnt == 0)             s.push_back(i);           else if (cnt == 1 || cnt == 2)             c.push_back(i);           else if (cnt == 3 || cnt == 4)             x = x * 10 + i -  0 ;           else             return 1;         }         if (cnt == 2)           isi[s].push_back(c);         else if (cnt == 3)           ans[s].first = x;         else           ans[s].second = x;       }     }     pair<long long, long long> res;     for (auto i : ans) {       res = solve(i.first);       cout << i.first <<     << res.first <<     << res.second <<   n ;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long long int inf = 1e18;   int n, m, k, x, p;   int mat[1005][1005];   void solve(int tc) {     cin >> n >> m >> k >> p;     for (int i = 0; i < n; ++i) {       for (int j = 0; j < m; ++j) {         cin >> mat[i][j];       }     }     multiset<int, greater<int> > msRow, msCol;     for (int r = 0; r < n; ++r) {       long long int rowSum = 0LL;       for (int i = 0; i < m; ++i) {         rowSum += mat[r][i];       }       msRow.insert(rowSum);     }     for (int c = 0; c < m; ++c) {       long long int colSum = 0LL;       for (int i = 0; i < n; ++i) {         colSum += mat[i][c];       }       msCol.insert(colSum);     }     vector<long long int> rows(k + 1, 0LL), cols(k + 1, 0LL);     for (int i = 1; i <= k; ++i) {       rows[i] = *msRow.begin();       msRow.erase(msRow.begin());       msRow.insert(rows[i] - (1LL * p * m));       rows[i] += rows[i - 1];       cols[i] = *msCol.begin();       msCol.erase(msCol.begin());       msCol.insert(cols[i] - (1LL * p * n));       cols[i] += cols[i - 1];     }     long long int ans = -inf;     for (int i = 0; i <= k; ++i) {       long long int tp = rows[i] + cols[k - i];       tp -= 1LL * p * i * (k - i);       if (tp > ans) ans = tp;     }     cout << ans <<   n ;   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     int tt = 1;     for (int i = 1; i <= tt; ++i) {       solve(i);     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const double pi = 3.1415926535;   const double eps = 1e-6;   struct TE {     int a, b, c;   } E[501000];   int tr[501000], n, x[501000], m;   bool cmp(TE a, TE b) { return a.a < b.a; }   int low(int a) { return (a & (-a)); }   int ask(int a) {     int ret = 1992837465;     while (a) {       ret = min(ret, tr[a]);       a -= low(a);     }     return ret;   }   void ins(int a, int b) {     while (a <= n + 5) {       tr[a] = min(tr[a], b);       a += low(a);     }   }   int main() {     scanf( %d , &n);     for (int i = 0; i < n; i++) scanf( %d , &E[i].a);     for (int i = 0; i < n; i++) scanf( %d , &E[i].b);     for (int i = 0; i < n; i++) scanf( %d , &E[i].c);     for (int i = 0; i < n; i++) {       E[i].a = -E[i].a;       E[i].b = -E[i].b;       E[i].c = -E[i].c;       x[i] = E[i].b;     }     sort(x, x + n);     m = unique(x, x + n) - x;     for (int i = 0; i < n; i++) E[i].b = (lower_bound(x, x + m, E[i].b) - x) + 1;     sort(E, E + n, cmp);     for (int i = 0; i <= n + 5; i++) tr[i] = 1992837465;     int ans = 0;     for (int i = 0; i < n; i++) {       int h = i;       while (i < n && E[i].a == E[h].a) {         if (ask(E[i].b - 1) < E[i].c) {           ans++;         }         i++;       }       i--;       for (int j = h; j <= i; j++) ins(E[j].b, E[j].c);     }     printf( %d n , ans);   }


#include <bits/stdc++.h>   using namespace std;   const int maxn = 300005;   int n, u, v, x, trie[maxn << 1][26], sz[maxn << 1], tot, cnt[maxn], res;   char op[2];   int merge(int a, int b) {     if (!a || !b) return a + b;     int rt = ++tot;     memset(trie[rt], 0, sizeof trie[rt]);     sz[rt] = 1;     for (int i = 0; i < 26; ++i) {       trie[rt][i] = merge(trie[a][i], trie[b][i]);       sz[rt] += sz[trie[rt][i]];     }     return rt;   }   void dfs(int u, int dep) {     sz[u] = 1;     for (int i = 0; i < 26; ++i)       if (trie[u][i]) {         dfs(trie[u][i], dep + 1);         sz[u] += sz[trie[u][i]];       }     cnt[dep] += sz[u];     tot = n;     int rt = 0;     for (int i = 0; i < 26; ++i) rt = merge(rt, trie[u][i]);     cnt[dep] -= max(1, sz[rt]);   }   int main() {     scanf( %d , &n);     for (int i = 0; i < n - 1; ++i) {       scanf( %d%d%s , &u, &v, op);       trie[u][op[0] -  a ] = v;     }     dfs(1, 0);     res = 0;     for (int i = 1; i < n; ++i)       if (cnt[i] > cnt[res]) res = i;     printf( %d n%d n , sz[1] - cnt[res], res + 1);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long long int p = 1e9 + 7;   const int N = 1e5 + 1;   long long int fact[N];   long long int ncr[N];   long long int n_1cr[N];   void factorial() {     fact[0] = fact[1] = 1;     for (int i = 2; i < N; i++) {       fact[i] = i * fact[i - 1];       fact[i] %= p;     }   }   long long int fastPower(long long int a, long long int b) {     if (b == 0) return 1;     long long int tmp = fastPower(a, b / 2);     if (b & 1) return (tmp % p * tmp % p * a % p) % p;     return (tmp % p * tmp % p) % p;   }   long long int nCr(int n) {     ncr[0] = 1;     n_1cr[0] = 1;     for (int i = 1; i <= n; i++) {       long long int iPower = fastPower(i, p - 2);       ncr[i] = (ncr[i - 1] * (n - i + 1)) % p;       ncr[i] *= iPower;       ncr[i] %= p;       n_1cr[i] = (n_1cr[i - 1] % p * (n - i) % p) % p;       n_1cr[i] *= iPower;       n_1cr[i] %= p;     }   }   long long int nCr(int n, int r) {     long long ans = 1;     ans = (fact[n - r] * fact[r]) % p;     ans = (fact[n] * fastPower(ans, p - 2)) % p;     return ans;   }   int main() {     int n;     long long int sum = 0;     cin >> n;     factorial();     for (int i = 1; i <= n; i++) {       sum += nCr(n - 1, i - 1) * nCr(n, i);       sum %= p;     }     sum *= 2;     sum %= p;     sum -= n;     cout << sum << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n, m, M;   vector<set<int> > A;   vector<set<pair<int, int> > > B;   vector<int> d, f, F, G;   vector<bool> v;   inline void upd(int& a, int b) {     if (d[a] > d[b]) a = b;   }   void ff(int x, int y) {     f[x] = x, v[x] = 1;     for (set<int>::iterator i = A[x].begin(); i != A[x].end(); i++)       if (v[*i]) {         if (*i != y) upd(f[x], *i);       } else         d[*i] = d[x] + 1, ff(*i, x), upd(f[x], f[*i]);   }   void ff(int x) {     f[x] = f[f[x]], v[x] = 0;     for (set<int>::iterator i = A[x].begin(); i != A[x].end(); i++)       if (v[*i]) ff(*i);   }   bool gg(int x, int y, int fa) {     if (x == y) return 1;     for (set<pair<int, int> >::iterator i = B[x].begin(); i != B[x].end(); i++)       if (i->first != fa)         if (gg(i->first, y, x)) {           v[i->second] = v[i->second ^ 1] = 1;           return 1;         }     return 0;   }   int main() {     scanf( %d%d , &n, &m);     A.resize(n);     for (int i = 0, x, y; i < m; i++)       scanf( %d%d , &x, &y), --x, --y, A[x].insert(y), A[y].insert(x);     if (n == 2) {       puts( -1 );       return 0;     }     d.resize(n, 0), f.resize(n), v.resize(n, 0);     ff(0, -1), ff(0);     F.resize(n);     for (int i = 0; i < n; i++)       if (f[i] == i) F[i] = (int)G.size(), G.push_back(i);     for (int i = 0; i < n; i++) F[i] = F[f[i]];     B.resize((int)G.size());     for (int i = 0; i < n; i++)       for (set<int>::iterator j = A[i].begin(); j != A[i].end(); j++)         if (F[i] != F[*j]) {           set<pair<int, int> >::iterator p =               B[F[i]].lower_bound(make_pair(F[*j], -1));           if (p != B[F[i]].end() && p->first == F[*j]) continue;           B[F[i]].insert(make_pair(F[*j], M++)),               B[F[*j]].insert(make_pair(F[i], M++));         }     if ((int)G.size() == 1) {       puts( 0 );       return 0;     }     if ((int)G.size() == 2) {       puts( 1 );       for (int i = 0; i < n; i++)         for (int j = i + 1; j < n; j++)           if (F[i] != F[j] && A[i].find(j) == A[i].end()) {             printf( %d %d n , i + 1, j + 1);             return 0;           }     }     vector<int> L;     for (int i = 0; i < (int)G.size(); i++)       if ((int)B[i].size() == 1) L.push_back(i);     printf( %d n , ((int)L.size() + 1) / 2);     while (1) {       random_shuffle(L.begin(), L.end());       bool F = 0, FF = 1;       if ((int)L.size() & 1) F = 1, L.push_back(L[0]);       v.clear(), v.resize(M, 0);       for (int i = 0; i < (int)L.size(); i += 2) gg(L[i], L[i + 1], -1);       for (int i = 0; i < M; i++)         if (!v[i]) {           FF = 0;           break;         }       if (FF) {         for (int i = 0; i < (int)L.size(); i += 2)           printf( %d %d n , G[L[i]] + 1, G[L[i + 1]] + 1);         return 0;       }       if (F) L.erase(L.end() - 1);     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     ios::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     long long int T = 1;     while (T--) {       long long int n, i, j;       cin >> n;       long long int a[n + 5];       long long int b[n + 5];       unordered_map<long long int, long long int> rnk;       long long int ans = 0;       for (i = 0; i < n; i++) {         cin >> a[i];       }       j = 0;       for (i = 0; i < n; i++) {         cin >> b[i];         if (b[i] != a[j]) {           ans++;           rnk[b[i]]++;         } else {           j++;           while (j < n && rnk.find(a[j]) != rnk.end()) j++;         }       }       cout << ans <<   n ;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long long MOD = 1e9 + 7;   const long long MAXN = 2e5 + 7;   long long a[MAXN];   bool good[MAXN];   long long want[MAXN], pref[MAXN], next_[MAXN];   signed main() {     ios_base::sync_with_stdio(false);     cout.precision(40);     long long n;     cin >> n;     for (long long i = 0; i < n; ++i) {       cin >> a[i];     }     stack<long long> st;     for (long long i = 0; i < n; ++i) {       while (st.size() && a[st.top()] < a[i]) {         next_[st.top()] = i;         st.pop();       }       st.push(i);     }     while (st.size()) {       next_[st.top()] = n;       st.pop();     }     long long mx1 = -MOD;     long long mx2 = -MOD;     for (long long i = 0; i < n; ++i) {       if (a[i] < mx1 && a[i] > mx2) {         want[i] = true;       }       if (a[i] > mx1) {         mx2 = mx1;         mx1 = a[i];       } else if (a[i] > mx2) {         mx2 = a[i];       }     }     pref[0] = want[0];     for (long long i = 1; i < n; ++i) {       pref[i] = pref[i - 1] + want[i];     }     long long mx = -MOD;     long long ans = -MOD;     long long best = -1;     for (long long i = 0; i < n; ++i) {       mx = max(mx, a[i]);       if (a[i] != mx) continue;       long long j = next_[i];       long long new_ans = pref[j - 1] - pref[i] - 1;       if (new_ans > ans || (new_ans == ans && a[i] < a[best])) {         ans = new_ans;         best = i;       }     }     mx = -MOD;     for (long long i = 0; i < n; ++i) {       mx = max(mx, a[i]);       if (a[i] == mx) continue;       long long new_ans = 0;       if (new_ans > ans || (new_ans == ans && a[i] < a[best])) {         ans = new_ans;         best = i;       }     }     cout << a[best] <<   n ;     return 0;   }


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


#include <bits/stdc++.h>   using namespace std;   int n;   int x, y;   int p[1000003];   int r[1000003];   map<int, int> a;   int ans[200003];   int s;   int parent(int x) {     if (p[x] < 0) return x;     return p[x] = parent(p[x]);   }   void join(int x, int y) { p[y] = parent(x); }   int main() {     fill_n(p, 1000003, -1);     scanf( %d , &n);     for (int i = 0; i < n; ++i) {       scanf( %d%d , &x, &y);       if (y) join(x, y);       a.insert(pair<int, int>(x, y));     }     int i = 2;     int j = 0;     while (a[j] != 0) {       ans[i] = a[j];       j = a[j];       i += 2;     }     i = 1;     j = 0;     while (p[j] < 0 || parent(j) == 0) j++;     j = parent(j);     ans[i] = j;     i += 2;     while (a[j] != 0) {       ans[i] = a[j];       j = a[j];       i += 2;     }     for (i = 1; i <= n; ++i) printf( %d  , ans[i]);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long C(int a, int b) {     long long ans = 1;     for (int i = 0; i < b; ++i) ans *= (a - i), ans /= (i + 1);     return ans;   }   int main() {     int n;     while (cin >> n) cout << C(n + 4, 5) * C(n + 2, 3) << endl;   }


#include <bits/stdc++.h>   using namespace std;   const long long maxn = 1e5 + 10;   struct Seg {     long long mn, id;   };   long long arr[maxn], N, P, ans[maxn];   Seg seg[maxn << 2];   inline Seg pushup(long long cur) {     Seg l = seg[cur << 1], r = seg[cur << 1 | 1];     if (l.mn == r.mn) {       return l;     } else if (l.mn < r.mn) {       Seg mer;       mer.id = l.id, mer.mn = l.mn;       return mer;     } else {       Seg mer;       mer.id = r.id, mer.mn = r.mn;       return mer;     }   }   long long query(long long cur, long long L, long long R, long long l,                   long long r) {     if (L > R) return -1;     if (l >= L && r <= R) {       return seg[cur].id;     }     long long mid = (l + r) >> 1;     long long lid = -1, rid = -1;     if (mid >= L) lid = query(cur << 1, L, R, l, mid);     if (mid < R) rid = query(cur << 1 | 1, L, R, mid + 1, r);     if (lid == -1)       return rid;     else if (rid == -1)       return lid;     else {       return arr[lid] > arr[rid] ? rid : lid;     }   }   long long query2(long long cur, long long l, long long r, const long long x) {     if (l == r) {       return l;     }     long long mid = (l + r) >> 1;     if (seg[cur << 1].mn <= x) {       return query2(cur << 1, l, mid, x);     }     return query2(cur << 1 | 1, mid + 1, r, x);   }   void build(long long l, long long r, long long cur) {     seg[cur].id = l, seg[cur].mn = arr[l];     if (l == r) return;     long long mid = (l + r) >> 1;     build(l, mid, cur << 1);     build(mid + 1, r, cur << 1 | 1);     seg[cur] = pushup(cur);   }   void update(long long pos, long long l, long long r, long long cur,               long long x) {     if (l == r) {       seg[cur].mn = x;       arr[pos] = x;       return;     }     long long mid = (l + r) >> 1;     if (mid >= pos) {       update(pos, l, mid, cur << 1, x);     } else {       update(pos, mid + 1, r, cur << 1 | 1, x);     }     seg[cur] = pushup(cur);   }   signed main() {     ios::sync_with_stdio(false);     cin >> N >> P;     for (register long long i = 1; i <= N; ++i) {       cin >> arr[i];     }     build(1, N, 1);     long long cnt = 0, cur = 0;     while (cnt < N) {       cur = max(cur, seg[1].mn);       long long x = query2(1, 1, N, cur);       ans[x] = cur += P;       ++cnt;       update(x, 1, N, 1, 0x3f3f3f3f3f3f3f3f);       while (cnt < N) {         x = query(1, 1, x - 1, 1, N);         if (x == -1 || arr[x] > cur) break;         ans[x] = cur += P, ++cnt;         update(x, 1, N, 1, 0x3f3f3f3f3f3f3f3f);       }     }     for (register long long i = 1; i <= N; ++i) {       cout << ans[i] <<    ;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n;     cin >> n;     int count = 0;     while ((count * (count + 1)) / 2 <= n) {       ++count;     }     --count;     int val = (count * (count + 1)) / 2;     int diff = n - val;     cout << count <<   n ;     for (int i = 1; i <= count; ++i) {       if (i == count) {         cout << count + diff <<    ;         break;       }       cout << i <<    ;     }   }


#include <bits/stdc++.h>   using namespace std;   int t;   int n;   int a[100086];   int main() {     scanf( %d , &t);     while (t--) {       scanf( %d , &n);       for (int i = 1; i <= n; i++) scanf( %d , &a[i]);       int x = 0, y = a[1];       for (int i = 2; i <= n; i++) {         if (a[i] > a[i - 1]) {           x += a[i] - a[i - 1];         } else {           y -= a[i - 1] - a[i];         }       }       puts(y >= 0 ?  YES  :  NO );     }   }


#include <bits/stdc++.h>   using namespace std;   int k, b, n;   map<int, int>::iterator it;   int main() {     map<int, int> mp;     while (scanf( %d%d%d , &k, &b, &n) != EOF) {       mp.clear();       if (b == 0) {         int a, sum = 0;         long long res = 0;         mp[0] = 1;         for (int i = 1; i <= n; i++) {           scanf( %d , &a);           if (a) sum++;           if (mp.find(sum) == mp.end())             mp[sum] = 1;           else             mp[sum]++;         }         for (it = mp.begin(); it != mp.end(); it++) {           long long tmp = it->second;           res += tmp * (tmp - 1) / 2;         }         cout << res << endl;       } else {         int a, ss = 0;         long long res = 0, pre = 0;         mp[0] = 1;         for (int i = 1; i <= n; i++) {           scanf( %d , &a);           if (a) {             ss += a;             ss %= (k - 1);             int ff = (ss - b + k - 1) % (k - 1);             int st = mp.find(ff) == mp.end() ? 0 : mp[ff];             pre = st;             res += st;             if (mp.find(ss) == mp.end())               mp[ss] = 1;             else               mp[ss]++;           } else {             res += pre;             mp[ss]++;           }         }         cout << res << endl;       }     }   }


#include <bits/stdc++.h>   using namespace std;   const int N = 2e5 + 5;   int a[N], cnt[N * 2], n, m;   long long solve(int m) {     int s = n;     memset(cnt, 0, sizeof(cnt));     cnt[s] = 1;     long long add = 0, ans = 0;     for (int i = 1; i <= n; i++) {       if (a[i] >= m)         add += cnt[s], s++;       else         s--, add -= cnt[s];       cnt[s]++;       ans += add;     }     return ans;   }   int main() {     scanf( %d %d , &n, &m);     for (int i = 1; i <= n; i++) scanf( %d , &a[i]);     printf( %lld n , solve(m) - solve(m + 1));     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long t1, t2, x1, x2, t;   bool Check(long long y1, long long y2) {     return t1 * y1 + t2 * y2 >= t * (y1 + y2);   }   double HotBath(long long y1, long long y2) {     return 1.0 * (t1 * y1 + t2 * y2) / (y1 + y2);   }   int main() {     cin >> t1 >> t2 >> x1 >> x2 >> t;     long long Y1 = 0, Y2 = 0;     if (HotBath(x1, x2) >= t) Y1 = x1, Y2 = x2;     if (HotBath(x1, 0) >= t && x1 >= Y1 + Y2) Y1 = x1, Y2 = 0;     if (HotBath(0, x2) >= t && x2 >= Y1 + Y2) Y1 = 0, Y2 = x2;     for (long long y1 = 0; y1 <= x1; y1++) {       long long y2 = 0;       long long Start = 0, End = x2;       while (Start <= End) {         long long Mid = Start + End >> 1;         if (Check(y1, Mid)) {           y2 = Mid;           End = Mid - 1;         } else           Start = Mid + 1;       }       if (y1 + y2 == 0) continue;       double New = HotBath(y1, y2);       double Old = HotBath(Y1, Y2);       if (New >= 1.0 * t) {         if (New < Old) {           Old = New;           Y1 = y1;           Y2 = y2;         } else if (fabs(New - Old) <= 1e-9 && (Y1 + Y2 < y1 + y2)) {           Y1 = y1;           Y2 = y2;         }       }     }     cout << Y1 <<     << Y2 <<   n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int maxN = 2000 + 5;   int n, mark[maxN], m, st[maxN], start, high[maxN], on[maxN], cnt;   vector<int> adj[maxN];   vector<int> s;   void DFS(int v) {     s.push_back(v);     on[v] = mark[v] = 1;     st[v] = high[v] = start++;     for (int i = 0; i < adj[v].size(); i++) {       int x = adj[v][i];       if (mark[x]) {         if (on[x]) high[v] = min(high[v], high[x]);       } else {         DFS(x);         high[v] = min(high[v], high[x]);       }     }     if (st[v] == high[v]) {       int x = s.back();       while (!s.empty() && st[x] >= st[v]) {         on[x] = 0;         s.pop_back();         x = s.back();       }       cnt++;     }   }   int main() {     ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);     cin >> n;     for (int i = 0; i < n; i++) {       for (int j = 0; j < n; j++) {         int x;         cin >> x;         if (x) adj[i].push_back(j);       }     }     for (int i = 0; i < n; i++)       if (!mark[i]) DFS(i);     if (cnt == 1)       cout <<  YES n ;     else       cout <<  NO n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int w, h, ring, plate = 0;     cin >> w >> h >> ring;     for (int i = 0; i < ring; i++) {       if (i > 0) {         w -= 4;         h -= 4;       }       plate += (w * 2) + ((h - 2) * 2);     }     cout << plate;     return 0;   }


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


#include <bits/stdc++.h>   int main() {     long long int n;     std::cin >> n;     if (n % 2 == 0) {       std::cout << n / 2 << std::endl;     } else {       std::cout << -(n + 1) / 2 << std::endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long long MOD = 1e9 + 7;   const long long INF = 1e18;   const string nl =   n ;   int32_t main() {     ios::sync_with_stdio(0);     cin.tie(nullptr);     long long n, k, second, t;     cin >> n >> k >> second >> t;     vector<pair<long long, long long>> a(n);     for (auto& i : a) {       cin >> i.first >> i.second;     }     vector<long long> b(k);     for (auto& i : b) {       cin >> i;     }     sort(b.begin(), b.end());     vector<long long> temp = {0};     for (auto& i : b) {       temp.push_back(i);     }     temp.push_back(second);     b = temp;     auto check = [&](long long x) -> bool {       long long ans = 0;       for (long long i = 0; i < ((long long)(b).size()); ++i) {         long long dist = b[i] - (i == 0 ? 0 : b[i - 1]);         long long hld = min(x - dist, dist);         if (hld < 0) {           return 0;         }         ans += hld + (dist - hld) * 2;       }       return ans <= t;     };     long long l = 0, r = 2e9;     while (l < r) {       long long mid = (l + r) / 2;       if (check(mid)) {         r = mid;       } else {         l = mid + 1;       }     }     auto cmp = [&](pair<long long, long long> a,                    pair<long long, long long> b) -> bool {       return a.second < b.second;     };     sort(a.begin(), a.end(), cmp);     long long ans = INF;     for (long long i = 0; i < ((long long)(a).size()); ++i) {       if (a[i].second >= l) {         ans = min(ans, a[i].first);       }     }     cout << (ans == INF ? -1 : ans) << nl;   }


#include <bits/stdc++.h>   using namespace std;   const int oo = (int)1e9;   const double PI = 2 * acos(0.0);   const double eps = 1e-9;   int main() {     ios_base::sync_with_stdio(false);     int n, arra[100010], arrb[100010];     cin >> n;     for (int i = 0; i < (int)(n); i++) {       cin >> arra[i];       arrb[i] = arra[i];     }     sort(arrb, arrb + n);     int cnt = 0;     for (int i = 0; i < (int)(n); i++)       if (arra[i] != arrb[i]) cnt++;     if (cnt == 2 || cnt == 0)       cout <<  YES  << endl;     else       cout <<  NO  << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   inline int read() {     int x = 0;     char ch = getchar();     bool positive = 1;     for (; ch <  0  || ch >  9 ; ch = getchar())       if (ch ==  - ) positive = 0;     for (; ch >=  0  && ch <=  9 ; ch = getchar()) x = x * 10 + ch -  0 ;     return positive ? x : -x;   }   int N, K;   int a[105], b[105], s[105];   int t[105];   inline void trans_one(int p[]) {     for (int i = 1; i <= N; ++i) t[i] = p[a[i]];     for (int i = 1; i <= N; ++i) p[i] = t[i];   }   inline void trans_two(int p[]) {     for (int i = 1; i <= N; ++i) t[a[i]] = p[i];     for (int i = 1; i <= N; ++i) p[i] = t[i];   }   inline bool equal(int a[], int b[]) {     for (int i = 1; i <= N; ++i)       if (a[i] != b[i]) return 0;     return 1;   }   int main() {     N = read(), K = read();     for (int i = 1; i <= N; ++i) a[i] = read();     for (int i = 1; i <= N; ++i) s[i] = read();     for (int i = 1; i <= N; ++i) b[i] = i;     if (equal(b, s)) return printf( NO n ), 0;     int l = 1 << 30, r = 1 << 30;     for (int i = 1; i <= K + 1; ++i) {       trans_one(b);       if (equal(b, s)) {         r = i;         break;       }     }     for (int i = 1; i <= N; ++i) b[i] = i;     for (int i = 1; i <= K + 1; ++i) {       trans_two(b);       if (equal(b, s)) {         l = i;         break;       }     }     if (l % 2 == K % 2 && K >= l) {       if (l > 1 || r > 1 || K - l == 0) return printf( YES n ), 0;     }     if (r % 2 == K % 2 && K >= r) {       if (r > 1 || l > 1 || K - r == 0) return printf( YES n ), 0;     }     printf( NO n );     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long MOD = 1e9 + 7, MAX = 1e18;   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     ;     long long t;     cin >> t;     while (t--) {       long long n0, n1, n2, i;       cin >> n0 >> n1 >> n2;       string second =   ;       if (n1) {         if (n1 % 2) {           for (i = 0; i <= n1; i++) {             if (i % 2)               second.push_back( 0 );             else               second.push_back( 1 );           }           while (n2--) cout << 1;           cout << second;           while (n0--) cout << 0;           cout <<   n ;         } else {           for (i = 1; i <= n1; i++) {             if (i % 2)               second.push_back( 0 );             else               second.push_back( 1 );           }           while (n2--) cout << 1;           cout << 1;           while (n0--) cout << 0;           cout << second <<   n ;         }       } else {         if (n2)           for (i = 0; i <= n2; i++) cout << 1;         if (n0)           for (i = 0; i <= n0; i++) cout << 0;         cout <<   n ;       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int l[123], r[123];   int d[123];   int main() {     ios_base::sync_with_stdio(false);     int n;     cin >> n;     l[0] = 0;     for (int i = 1; i < n; ++i) {       int x;       cin >> x;       l[i] = l[i - 1] + x;     }     r[n - 1] = 0;     vector<int> a;     for (int i = 0; i < (int)n - 1; ++i) {       int x;       cin >> x;       a.push_back(x);     }     int k = n - 2;     for (int i = n - 2; i >= 0; --i) {       r[i] = r[i + 1] + a[k];       k--;     }     for (int i = 0; i < (int)n; ++i) cin >> d[i];     int res[123];     for (int i = 0; i < (int)n; ++i) res[i] = d[i] + l[i] + r[i];     sort(res, res + n);     cout << res[0] + res[1];     return 0;   }


#include <bits/stdc++.h>   using namespace std;   inline long long max3(long long a, long long b, long long c) {     return (a) > (b) ? ((a) > (c) ? (a) : (c)) : ((b) > (c) ? (b) : (c));   }   inline long long min3(long long a, long long b, long long c) {     return (a) < (b) ? ((a) < (c) ? (a) : (c)) : ((b) < (c) ? (b) : (c));   }   long long extgcd(long long a, long long b, long long &x, long long &y) {     if (b == 0) {       x = 1;       y = 0;       return a;     }     long long d = extgcd(b, a % b, y, x);     y -= a / b * x;     return d;   }   inline long long mod(long long a, long long m) { return (a % m + m) % m; }   long long modinv(long long a) {     long long x, y;     extgcd(a, 1000000007, x, y);     return mod(x, 1000000007);   }   long long gcd(long long u, long long v) {     long long r;     while (0 != v) {       r = u % v;       u = v;       v = r;     }     return u;   }   long long lcm(long long u, long long v) { return u / gcd(u, v) * v; }   void get_all_factors(int a[], long long int n) {     long long int w = sqrt(n);     for (int i = 1; i <= w; i++) {       if (n % i == 0) {         a[i]++;         a[n / i]++;       }     }     long long int x = ceil(sqrt(n));     if (sqrt(n) == ceil(sqrt(n))) a[(int)sqrt(n)]--;   }   long long int memo[100000][110] = {0};   long long C(long long n, long long r) {     if (r == 0 || r == n) return 1;     if (memo[n][r] != -1) return memo[n][r];     return memo[n][r] =                (C(n - 1, r - 1) % 1000000007 + C(n - 1, r) % 1000000007) %                1000000007;   }   int valid(string s, int i) {     if (s ==  lol  or i == -1000050000) {       return 0;     }     return 1;   }   void status(string s1 =  lol , long long int i1 = -1000050000,               string s2 =  lol , long long int i2 = -1000050000,               string s3 =  lol , long long int i3 = -1000050000) {     if (1) {       if (valid(s1, i1)) {         cout << s1 <<  =  << i1 <<     ;       }       if (valid(s2, i2)) {         cout << s2 <<  =  << i2 <<     ;       }       if (valid(s3, i3)) {         cout << s3 <<  =  << i3 <<     ;       }       cout << endl;     }   }   long long int poww(long long b, long long e) {     if (e == 0)       return 1;     else if (e % 2 == 0) {       long long a = pow(b, e / 2);       return a * a;     } else {       long long a = pow(b, e / 2);       return b * a * a;     }   }   long long int powm(long long x, long long y, long long m = 1000000007) {     x = x % m;     long long res = 1;     while (y) {       if (y & 1) res = res * x;       res %= m;       y = y >> 1;       x = x * x;       x %= m;     }     return res;   }   vector<pair<int, int> > range(1000000);   vector<int> adj[1000000];   vector<int> d(1000000, 0);   int p[1000000] = {0};   int T = 0;   void dfs(int v, int pa = -1, int dep = 0) {     p[v] = pa;     d[v] = dep;     range[v].first = T;     T++;     for (auto i : adj[v]) {       if (i != pa) {         dfs(i, v, dep + 1);       }     }     range[v].second = T;     T++;   }   int main() {     int n, m;     cin >> n >> m;     for (int i = 0; i < n - 1; i++) {       int u, v;       cin >> u >> v;       adj[u].push_back(v);       adj[v].push_back(u);     }     dfs(1);     while (m--) {       int k;       cin >> k;       int a[k];       for (int i = 0; i < k; i++) {         cin >> a[i];       }       int it = a[0];       for (auto i : a) {         if (d[i] > d[it]) {           it = i;         }       }       for (auto &i : a) {         if (it == i) {           continue;         }         if (p[i] != -1) {           i = p[i];         }       }       int ok = 1;       for (auto i : a) {         if (range[i].first <= range[it].first and             range[i].second >= range[it].second) {         } else {           ok = 0;           break;         }       }       if (ok) {         cout <<  YES  << endl;       } else {         cout <<  NO  << endl;       }     }   }


#include <bits/stdc++.h>   using namespace std;   template <typename T>   bool mmax(T &m, const T q) {     if (m < q) {       m = q;       return true;     } else       return false;   }   template <typename T>   bool mmin(T &m, const T q) {     if (m > q) {       m = q;       return true;     } else       return false;   }   const int MOD = 1e9 + 7;   const int MAXN = 5e5 + 3;   const int INF = 1e9;   int main(int argc, char **argv) {     ios::sync_with_stdio(false);     cin.tie(0);     int ntest;     cin >> ntest;     while (ntest--) {       long long x, y, a, b;       cin >> x >> y >> a >> b;       long long ans = a * (abs(x) + abs(y));       mmin(ans, b * abs(x) + a * abs(y - x));       mmin(ans, b * abs(y) + a * abs(x - y));       cout << ans <<   n ;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int maxn = 2e5 + 5;   int lazy[maxn << 2];   int a[maxn], c[maxn];   void pushup(int l, int r, int rt) {     lazy[rt << 1] = lazy[rt << 1 | 1] = lazy[rt];     lazy[rt] = 0;   }   void update(int L, int R, int c, int l, int r, int rt) {     if (L <= l && R >= r) {       lazy[rt] = c;       return;     }     if (lazy[rt]) pushup(l, r, rt);     if (L <= ((l + r) >> 1)) update(L, R, c, l, ((l + r) >> 1), rt << 1);     if (R > ((l + r) >> 1)) update(L, R, c, ((l + r) >> 1) + 1, r, rt << 1 | 1);   }   void query(int l, int r, int rt) {     if (l == r) {       c[l] = lazy[rt];       return;     }     if (lazy[rt]) pushup(l, r, rt);     query(l, ((l + r) >> 1), rt << 1);     query(((l + r) >> 1) + 1, r, rt << 1 | 1);   }   struct T {     int l, r;   } b[maxn];   int main() {     map<int, int> mp;     int n, k;     cin >> n >> k;     for (int i = 1; i <= n; i++) cin >> a[i];     for (int i = 1; i <= n; i++) {       if (a[i] == 0) continue;       if (mp[a[i]])         b[a[i]].r = i;       else         mp[a[i]] = 1, b[a[i]].l = i, b[a[i]].r = i;     }     for (int i = 1; i <= k; i++) {       if (mp[i]) {         update(b[i].l, b[i].r, i, 1, n, 1);       }     }     query(1, n, 1);     bool d = true;     for (int i = 1; i <= n; i++) {       if (c[i] != a[i] && a[i] != 0) d = false;     }     if (!d) {       cout <<  NO  << endl;       return 0;     }     bool ff = true;     for (int i = 1; i <= n; i++) {       if (a[i] == k) ff = false;     }     if (ff) {       bool dd = false;       for (int i = 1; i <= n; i++)         if (a[i] == 0) {           dd = true;           c[i] = k;           break;         }       if (!dd) {         cout <<  NO  << endl;         return 0;       }     }     cout <<  YES  << endl;     for (int i = 1; i <= n; i++) {       if (a[i] == 0 && c[i] == 0) {         c[i] = 1;       }       cout << c[i] <<    ;     }     cout << endl;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int t, n, x;     scanf( %d , &n);     priority_queue<int, vector<int>, greater<int> > que;     long long ans = 0;     for (int i = 0; i < n; i++) {       scanf( %d , &x);       if (que.empty() || que.top() > x)         que.push(x);       else {         ans += x - que.top();         que.pop();         que.push(x);         que.push(x);       }     }     printf( %lld n , ans);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   mt19937 rndll(chrono::steady_clock::now().time_since_epoch().count());   const int INF = 0x3f3f3f3f, MOD = 1e9 + 7;   const int N = 2e5 + 5;   int n, q, a, b, cnt, k, par[N];   vector<int> adj[N], v;   pair<int, int> rng[N];   void dfs(int u, int p) {     par[u] = (p != -1 ? p : u);     rng[u] = {INF, -INF};     for (auto v : adj[u])       if (v != p) {         dfs(v, u);         rng[u].first = min(rng[u].first, rng[v].first);         rng[u].second = max(rng[u].second, rng[v].second);       }     if (rng[u].first == INF) cnt++, rng[u] = {cnt, cnt};   }   bool query(vector<int> &v) {     int l = -INF, r = INF;     for (auto x : v)       l = max(l, rng[par[x]].first), r = min(r, rng[par[x]].second);     return l <= r;   }   int main() {     ios_base::sync_with_stdio(0), cin.tie(0);     cin >> n >> q;     for (int i = 0; i < n - 1; i++) {       cin >> a >> b;       adj[a].push_back(b), adj[b].push_back(a);     }     dfs(1, -1);     while (q--) {       cin >> k;       v.resize(k);       for (int i = 0; i < k; i++) cin >> v[i];       cout << (query(v) ?  YES n  :  NO n );     }   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n;     vector<int> v;     cin >> n;     int x = 1;     for (int i = 1; i <= n; i++) {       int d;       cin >> d;       if (d == x) {         v.push_back(i + 2000);         x++;       }     }     cout << v.size() << endl;     for (int i = 0; i < v.size(); i++) cout << v[i] <<    ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int maxn = 2e5 + 7;   const int inf = 0x3f3f3f3f;   const int mod = 1e9 + 7;   long long ans;   int a[maxn];   int ned[maxn];   int n;   int main() {     scanf( %d , &n);     for (int i = 1; i <= n; i++) {       scanf( %d , &a[i]);       ned[i] = a[i] + 1;     }     for (int i = 2; i <= n; i++) {       ned[i] = max(ned[i], ned[i - 1]);     }     for (int i = n; i >= 1; i--) {       ned[i] = max(ned[i + 1] - 1, ned[i]);       ans += (ned[i] - a[i] - 1);     }     printf( %I64d n , ans);   }


#include <bits/stdc++.h>   using namespace std;   vector<long long> A;   long long B[1000000] = {0};   bool cmp(long long x, long long y) { return x > y; }   int main() {     A.clear();     string S1, S2;     cin >> S1;     for (long long i = 0; i < S1.size(); i++) A.push_back(S1[i] -  0 );     cin >> S2;     for (long long i = 0; i < S2.size(); i++) B[i] = S2[i] -  0 ;     sort(B, B + S2.size(), cmp);     long long find = 0;     for (long long i = 0; i < A.size(); i++)       if (A[i] < B[find]) A[i] = B[find++];     for (long long i = 0; i < A.size(); i++) cout << A[i];   }


#include <bits/stdc++.h>   using namespace std;   struct Point {     int num;     int next[3001];     int count;   };   Point a[3001];   int dis[3001];   int p[3001];   int cir[3001];   int cir1[3001];   int f(int n) {     int i, num = 0, d = 1;   loop:     while (1) {       num = 0;       for (i = 1; i <= n; i++) {         if (a[i].num - a[i].count == 1) {           a[i].count++;           p[num++] = i;         }       }       for (i = 0; i < num; i++) {         for (int j = 0; j < a[p[i]].num; j++) a[a[p[i]].next[j]].count++;       }       for (i = 1; i <= n; i++) {         if (a[i].num - a[i].count > 0 && a[i].num - a[i].count != 2) goto loop;       }       num = 0;       for (i = 1; i <= n; i++) {         if (a[i].num - a[i].count == 2) cir[num++] = i;       }       return num;     }   }   int main() {     int b[4], n, i, t, sum, num, flag;     int from, to;     cin >> n;     for (i = 1; i <= n; i++) {       cin >> from >> to;       a[from].next[a[from].num++] = to;       a[to].next[a[to].num++] = from;     }     int step = 0;     sum = num = f(n);     memset(dis, 0xff, sizeof(dis));     for (i = 0; i < num; i++) dis[cir[i]] = step;     while (sum < n) {       int k = 0;       step++;       flag = 0;       for (i = 0; i < num; i++) {         for (int j = 0; j < a[cir[i]].num; j++) {           int next = a[cir[i]].next[j];           if (dis[next] == -1) {             sum++;             flag = 1;             dis[next] = step;             cir1[k++] = next;             continue;           }         }         if (!flag) cir[i] = -1;       }       memcpy(cir, cir1, k * sizeof(cir1[0]));       num = k;     }     for (i = 1; i <= n; i++)       if (i != n)         cout << dis[i] <<    ;       else         cout << dis[i] << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   template <typename T>   inline bool chkmin(T &x, T y) {     return x > y ? x = y, 1 : 0;   }   template <typename T>   inline bool chkmax(T &x, T y) {     return x < y ? x = y, 1 : 0;   }   template <typename T>   inline void read(T &x) {     char ch = getchar();     int f = 1;     x = 0;     while (ch <  0  || ch >  9 ) {       if (ch ==  - ) f = -1;       ch = getchar();     }     while (ch >=  0  && ch <=  9 ) x = x * 10 + ch -  0 , ch = getchar();     x *= f;   }   template <typename T, typename... Args>   inline void read(T &x, Args &...args) {     char ch = getchar();     int f = 1;     x = 0;     while (ch <  0  || ch >  9 ) {       if (ch ==  - ) f = -1;       ch = getchar();     }     while (ch >=  0  && ch <=  9 ) x = x * 10 + ch -  0 , ch = getchar();     x *= f;     read(args...);   }   int n, m, C, emp, dlt, len[300050], crs[300050], ans[300050];   set<int> se;   map<int, int> both[300050];   struct Time {     int t, x;     bool operator<(const Time &rhs) const { return t < rhs.t; }   } a[300050 << 2];   struct cloud {     int l, r, c;     bool operator<(const cloud &rhs) const { return c < rhs.c; }   } c[300050];   struct Query {     int x, id;     bool operator<(const Query &rhs) const { return x < rhs.x; }   } q[300050];   int maxv[300050 << 2];   void modify(int u, int l, int r, int x) {     if (l == r) return (void)(maxv[u] = len[x]);     if (x <= ((l + r) >> 1))       modify(u << 1, l, ((l + r) >> 1), x);     else       modify(u << 1 | 1, ((l + r) >> 1) + 1, r, x);     maxv[u] = max(maxv[u << 1], maxv[u << 1 | 1]);   }   int query(int u, int l, int r, int tl, int tr) {     if (tl > tr) return 0;     if (tl <= l && r <= tr) return maxv[u];     int ret = 0;     if (tl <= ((l + r) >> 1)) ret = query(u << 1, l, ((l + r) >> 1), tl, tr);     if (((l + r) >> 1) < tr)       chkmax(ret, query(u << 1 | 1, ((l + r) >> 1) + 1, r, tl, tr));     return ret;   }   int main() {     read(n, C);     for (register int i = 1; i <= n; ++i) read(c[i].l, c[i].r, c[i].c);     sort(c + 1, c + n + 1);     int tot = 0;     for (register int i = 1; i <= n; ++i)       a[++tot] = Time{c[i].l, i}, a[++tot] = Time{c[i].r, -i};     a[++tot] = Time{int(2e9), 0};     sort(a + 1, a + tot + 1);     read(m);     for (register int i = 1; i <= m; ++i) read(q[i].x), q[i].id = i;     sort(q + 1, q + m + 1);     int pos = 1;     for (register int i = 1; i <= tot; ++i) {       int T = a[i].t - a[i - 1].t;       if (se.empty())         emp += T;       else if (se.size() == 1) {         int x = *se.begin(), cur = -1e9;         len[x] += T, crs[x] += T, modify(1, 1, n, x);         int rr = upper_bound(c + 1, c + n + 1, cloud{0, 0, C - c[x].c}) - c - 1;         if (rr >= x)           chkmax(cur, query(1, 1, n, 1, x - 1)),               chkmax(cur, query(1, 1, n, x + 1, rr));         else           chkmax(cur, query(1, 1, n, 1, rr));         chkmax(crs[x], len[x] + cur);         if (c[x].c <= C) chkmax(dlt, crs[x]);       } else if (se.size() == 2) {         int x = *se.begin(), y = *se.rbegin();         if (c[x].c + c[y].c <= C) {           both[x][y] += T;           chkmax(crs[x], both[x][y] + len[x] + len[y]),               chkmax(crs[y], both[x][y] + len[x] + len[y]);           chkmax(dlt, max(crs[x], crs[y]));         }       }       while (pos <= m && dlt + emp >= q[pos].x)         ans[q[pos].id] = a[i].t - (dlt + emp - q[pos].x), pos++;       if (a[i].x > 0)         se.insert(a[i].x);       else         se.erase(-a[i].x);     }     for (register int i = 1; i <= m; ++i) printf( %d n , ans[i]);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int _ = 600 + 7;   const int __ = 1e5 + 7;   const int S = 601, T = 602;   const int inf = 0x3f3f3f3f;   int n;   int lst[_], nxt[__], to[__], cap[__], tot = 1;   long long ans;   int gi() {     int x = 0;     bool f = 0;     char c = getchar();     while (!isdigit(c) and c !=  - ) c = getchar();     if (c ==  - ) f = 1, c = getchar();     while (isdigit(c)) x = (x << 3) + (x << 1) + c -  0 , c = getchar();     return f ? -x : x;   }   void Add(int x, int y, int c) {     nxt[++tot] = lst[x];     to[tot] = y;     cap[tot] = c;     lst[x] = tot;     nxt[++tot] = lst[y];     to[tot] = x;     cap[tot] = 0;     lst[y] = tot;   }   void Init() {     n = gi();     for (int i = 1; i <= n; ++i) {       int m = gi();       for (int j = 1; j <= m; ++j) Add(i, gi() + n, inf);     }     for (int i = 1; i <= n; ++i) {       int v = gi();       ans -= v;       Add(S, i, inf - v), Add(i + n, T, inf);     }   }   int d[_], cur[_];   queue<int> q;   bool Bfs() {     memset(d, -1, sizeof d);     while (!q.empty()) q.pop();     d[S] = 0, q.push(S);     while (!q.empty()) {       int u = q.front();       q.pop();       for (int i = lst[u]; i; i = nxt[i])         if (cap[i] and d[to[i]] == -1) {           d[to[i]] = d[u] + 1, q.push(to[i]);           if (to[i] == T) return 1;         }     }     return 0;   }   int Dfs(int u, int flow) {     if (u == T) return flow;     int res = flow;     for (int &i = cur[u]; i; i = nxt[i])       if (cap[i] and d[to[i]] == d[u] + 1) {         int tmp = Dfs(to[i], min(res, cap[i]));         cap[i] -= tmp, cap[i ^ 1] += tmp;         res -= tmp;         if (!res) return flow;       }     return flow - res;   }   void Run() {     long long maxFlow = -1ll * n * inf;     int flow;     while (Bfs()) {       memcpy(cur, lst, sizeof cur);       do {         flow = Dfs(S, inf);         maxFlow += flow;       } while (flow);     }     cout << min(0ll, -(ans - maxFlow)) << endl;   }   int main() {     Init();     Run();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n, k, a[100010];     cin >> n >> k;     if (n / 2 > k)       printf( -1 n );     else {       if (n == 1) {         if (k == 0)           printf( 1 n );         else           printf( -1 n );       } else {         int t = k - (n / 2 - 1);         printf( %d %d  , t, 2 * t);         int cnt = 1;         for (int i = 1; i < n / 2; i++) {           printf(  %d %d  , 900000000 + cnt, 900000000 + cnt + 1);           cnt += 2;         }         if (n % 2) printf( 1000000000 );       }     }   }


#include <bits/stdc++.h>   using namespace std;   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     long long int t;     cin >> t;     while (t--) {       long long int a, b, n, s;       cin >> a >> b >> n >> s;       long long int r = s % n;       long long int q = s / n;       if (q <= a) {         if (r <= b) {           cout <<  YES  <<   n ;         } else           cout <<  NO  <<   n ;       } else if (((q - a) * n + r) <= b) {         cout <<  YES  <<   n ;       } else {         cout <<  NO  <<   n ;       }     }   }


#include <bits/stdc++.h>   using namespace std;   string itos(int x) {     stringstream ss;     ss << x;     return ss.str();   }   int xorsum, n, a[110];   map<int, int> grun;   int sieve[31700];   vector<int> p;   int mex(set<int> &s) {     for (int i = 0;; i++)       if (not s.count(i)) return i;   }   int getGrundy(int x) {     if (grun.count(x)) return grun[x];     if (x == 1) return grun[x] = 0;     set<int> s;     for (int i = 1; (1 << i) <= x; i++)       s.insert(getGrundy(x >> i | (x & ((1 << i) - 1))));     return grun[x] = mex(s);   }   void solve(int x) {     int mask = 0;     for (int i = 0; i < n; i++) {       int cnt = 0;       while (a[i] % x == 0) cnt++, a[i] /= x;       mask |= (1 << cnt);     }     xorsum ^= getGrundy(mask);   }   int main() {     ios_base::sync_with_stdio(false);     cin >> n;     for (int i = 0; i < n; i++) cin >> a[i];     for (int i = 2; i < 31700; i++) {       if (not sieve[i]) {         p.push_back((i));         for (int j = i * i; j < 31700; j += i) sieve[j] = 1;       }     }     for (int i = 0; i < n; i++) {       auto it1 = p.begin();       while (a[i] > 1) {         if (it1 == p.end()) {           solve(a[i]);           break;         } else if (a[i] % *it1 == 0)           solve(*it1);         it1++;       }     }     cout << (xorsum ?  Mojtaba  :  Arpa ) << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long long int Maxn3 = 1e3 + 10;   const long long int Maxn4 = 1e4 + 10;   const long long int Maxn5 = 1e5 + 10;   const long long int Maxn6 = 1e6 + 10;   const long long int Maxn7 = 1e7 + 10;   const long long int Maxn8 = 1e8 + 10;   const long long int Maxn9 = 1e9 + 10;   const long long int Maxn18 = 1e18 + 10;   const long long int Mod1 = 1e7 + 7;   const long long int Mod2 = 1e9 + 7;   const long long int LLMax = LLONG_MAX;   const long long int LLMin = LLONG_MIN;   const long long int INTMax = INT_MAX;   const long long int INTMin = INT_MIN;   long long int mn = LLMax, mx = LLMin;   char s[Maxn6];   int main() {     ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL);     string s1( ... n.X. n ), s2( .X. n ), s;     long long int res = 0;     cin >> s;     for (long long int i = 0; i < s.size(); i++) {       if (res < s[i]) res += 256;       while (res > s[i]) {         res--;         cout << s1;       }       cout << s2;     }   }


#include <bits/stdc++.h>   std::mt19937 rng(       (int)std::chrono::steady_clock::now().time_since_epoch().count());   int main() {     std::ios_base::sync_with_stdio(false);     std::cin.tie(NULL);     int n, w, m;     std::cin >> n >> w >> m;     std::cout << std::setprecision(10) << std::fixed;     std::vector<std::vector<std::pair<int, int>>> ans(m);     w *= m;     int cup = n * w / m;     int rest = w;     for (int i = 0, j = 0; i < m; i++) {       if (rest > 0 && rest != w && rest > cup) {         std::cout <<  NO n ;         return 0;       }       int want = cup;       while (want > 0) {         int use = std::min(rest, want);         assert(use != 0);         ans[i].emplace_back(use, j + 1);         want -= use;         rest -= use;         if (rest == 0) {           j++;           rest = w;         }       }     }     std::cout <<  YES n ;     for (auto vec : ans) {       for (int i = 0; i < (int)vec.size(); i++) {         std::cout << vec[i].second <<     << (double)vec[i].first / m                   << (i + 1 == (int)vec.size() ?   n  :    );       }     }   }


#include <bits/stdc++.h>   using namespace std;   constexpr int INF = 1e9 + 1;   constexpr long long LLINF = 1e18 + 1;   long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; }   long long lcm(long long a, long long b) { return a / gcd(a, b) * b; }   void solve() {     int n, t;     cin >> n;     vector<int> arr(101);     for (__typeof(n) i = 0; i < n; i++) {       cin >> t;       arr[t] = 1;     }     int col = 0;     for (int i = 1; i <= 100; i++)       if (arr[i]) {         for (int j = i; j <= 100; j += i) {           arr[j] = 0;         }         col++;       }     printf( %d n , col);   }   int main() {     ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);     int t = 1;     while (t--) solve();   }


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


#include <bits/stdc++.h>   using namespace std;   const int mod = 998244353;   int power(int v, int u) {     if (!u) return 1;     int ok = power(v, u >> 1);     if (u & 1) return (long long)ok * ok % mod * v % mod;     return (long long)ok * ok % mod;   }   const int maxm = 2e5;   int b[maxm + 10];   int main() {     int n, m, A;     scanf( %d%d%d , &n, &m, &A);     for (int i = 1; i <= m; i++) scanf( %d , &b[i]);     int ans = 1;     int inv2 = power(2, mod - 2);     for (int i = 1; i <= m; i++)       ans = (long long)ans *             (((long long)power(A, 2 * (b[i] - b[i - 1])) +               power(A, b[i] - b[i - 1])) %              mod * inv2 % mod) %             mod;     ans = (long long)ans * power(A, n - b[m] - b[m]) % mod;     printf( %d n , ans);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long double eps = 1e-9;   const int inf = (1 << 30) - 1;   const long long inf64 = ((long long)1 << 62) - 1;   const long double pi = acos(-1);   template <class T>   T sqr(T x) {     return x * x;   }   template <class T>   T abs(T x) {     return x < 0 ? -x : x;   }   const int MAXK = 105;   const int magic = 100 * 1000;   int a[MAXK];   int ans_small[MAXK][magic];   long long get_ans(long long n, int k) {     if (k == 0) {       return n;     }     if (n < magic) {       return ans_small[k][n];     }     return get_ans(n, k - 1) - get_ans(n / a[k - 1], k - 1);   }   int main() {     ios_base::sync_with_stdio(0);     long long n;     int k;     cin >> n >> k;     for (int i = 0; i < k; ++i) {       cin >> a[i];     }     sort(a, a + k);     for (int i = 0; i < magic; ++i) {       ans_small[0][i] = i;     }     for (int i = 1; i <= k; ++i) {       for (int j = 0; j < magic; ++j) {         ans_small[i][j] = ans_small[i - 1][j] - ans_small[i - 1][j / a[i - 1]];       }     }     cout << get_ans(n, k) << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long long mod = 1000000007;   long long Power(long long a, long long b) {     if (a == 0) return 0LL;     long long ans = 1;     while (b) {       if (b & 1) ans = (ans * a) % mod;       a = (a * a) % mod;       b >>= 1;     }     return ans;   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     long long n, m, k;     cin >> n >> m >> k;     if (k == -1 && n % 2 != m % 2) {       cout << 0 << endl;       return 0;     }     long long ans = Power(Power(2, (n - 1)), m - 1);     cout << ans << endl;   }


#include <bits/stdc++.h>   using namespace std;   const int mod = 1e9 + 7;   long long dp[20][20][20];   int a[20];   long long num[20];   long long dfs(int pos, int pre, int sta, bool flag) {     if (pos == 0) {       if (pre == sta)         return 1;       else         return 0;     }     if (dp[pos][pre][sta] != -1 && !flag) return dp[pos][pre][sta];     int maxx = flag ? a[pos] : 9;     long long sum = 0;     for (int i = 0; i <= maxx; i++) {       sum += dfs(pos - 1, pre + (i == 4 || i == 7), sta, flag && (i == a[pos]));     }     if (!flag) dp[pos][pre][sta] = sum;     return sum;   }   long long solve(int pos, int res) {     long long ans = 0;     if (pos == 7) return 1;     for (int i = 0; i <= res; i++) {       if (num[i]) {         num[i]--;         ans = (ans + (num[i] + 1) * solve(pos + 1, res - i) % mod) % mod;         num[i]++;       }     }     return ans;   }   void cal(int x) {     int len = 0;     while (x) {       len++;       a[len] = x % 10;       x /= 10;     }     for (int i = 0; i <= len; i++) {       num[i] = dfs(len, 0, i, 1);     }     num[0]--;     long long ans = 0;     for (int i = 1; i <= len; i++) {       ans = (ans + num[i] * solve(1, i - 1) % mod) % mod;     }     printf( %lld n , ans);   }   int main() {     memset(dp, -1, sizeof dp);     int m;     scanf( %d , &m);     cal(m);   }


#include <bits/stdc++.h>   using namespace std;   int a[29];   int main() {     int n, k;     char s[105];     scanf( %d , &n);     scanf( %d , &k);     scanf( %s , s + 1);     bool f(true);     for (int i = 1; i <= n; ++i) {       int temp = s[i] -  a ;       a[temp]++;       if (a[temp] > k) {         f = false;         break;       }     }     if (f)       printf( YES n );     else       printf( NO n );     return 0;   }


#include <bits/stdc++.h>   using namespace std;   using ll = long long;   using vi = vector<int>;   using vl = vector<ll>;   using pi = pair<int, int>;   namespace output {   void pr(int x) { cout << x; }   void pr(long x) { cout << x; }   void pr(ll x) { cout << x; }   void pr(unsigned x) { cout << x; }   void pr(unsigned long x) { cout << x; }   void pr(unsigned long long x) { cout << x; }   void pr(float x) { cout << x; }   void pr(double x) { cout << x; }   void pr(long double x) { cout << x; }   void pr(char x) { cout << x; }   void pr(const char* x) { cout << x; }   void pr(const string& x) { cout << x; }   void pr(bool x) { pr(x ?  true  :  false ); }   template <class T1, class T2>   void pr(const pair<T1, T2>& x);   template <class T>   void pr(const T& x);   template <class T, class... Ts>   void pr(const T& t, const Ts&... ts) {     pr(t);     pr(ts...);   }   template <class T1, class T2>   void pr(const pair<T1, T2>& x) {     pr( { , x.first,  ,  , x.second,  } );   }   template <class T>   void pr(const T& x) {     pr( { );     bool fst = 1;     for (const auto& a : x) pr(!fst ?  ,   :   , a), fst = 0;     pr( } );   }   void ps() { pr(  n ); }   template <class T, class... Ts>   void ps(const T& t, const Ts&... ts) {     pr(t);     if (sizeof...(ts)) pr(   );     ps(ts...);   }   void pc() { cout <<  ]  << endl; }   template <class T, class... Ts>   void pc(const T& t, const Ts&... ts) {     pr(t);     if (sizeof...(ts)) pr( ,  );     pc(ts...);   }   }  // namespace output   using namespace output;   const int MX = 200005;   const int MOD = (int)(1e9 + 7);   const ll INF = (ll)1e18;   int main() {     ios::sync_with_stdio(false);     cin.tie(0);     int n;     cin >> n;     vi a(n);     vi cnt(101);     for (int i = 0; i < n; i++) {       cin >> a[i];       cnt[a[i]]++;     }     pi best = make_pair(-1, -1);     for (int i = 1; i <= 100; i++) {       if (cnt[i] > best.second) best = make_pair(i, cnt[i]);     }     for (int i = 1; i <= 100; i++) {       if (i != best.first && cnt[i] == best.second) {         cout << n <<   n ;         return 0;       }     }     int ret = 0;     for (int v = 1; v <= 100; v++) {       if (v == best.first) continue;       int ans = 0;       vi nv(n);       for (int i = 0; i < n; i++) {         if (a[i] == v)           nv[i] = -1;         else if (a[i] == best.first)           nv[i] = 1;       }       vi mn(2 * n + 1, -2);       mn[n] = -1;       int sum = 0;       for (int i = 0; i < n; i++) {         sum += nv[i];         if (mn[sum + n] != -2)           ans = max(ans, i - mn[sum + n]);         else           mn[sum + n] = i;       }       ret = max(ret, ans);     }     cout << ret <<   n ;   }


#include <bits/stdc++.h>   using namespace std;   const int maxn = 1e5 + 10;   long long int cal(long long int num) { return (num * (num + 1)) / 2ll; }   void solve() {     long long int n;     scanf( %lld , &n);     n--;     long long int ans = 0, k = 1;     for (long long int i = 1; i <= n; i <<= 1) {       ans += ((n - i) / (i << 1) + 1) * i;     }     printf( %lld n , ans);   }   int main() {     int t = 1;     while (t--) {       solve();     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int MOD = 1e9 + 9;   const double eps = 1e-5;   const int maxN = 2e3 + 10;   const int inf = 1e9;   int n, m;   vector<pair<int, int> > a[maxN];   int vis[maxN], c[maxN];   vector<int> pat;   inline void dfs(int v, int f = -1) {     for (int i = 0; i < a[v].size(); i++) {       if (a[v][i].first <= f) continue;       if (!vis[a[v][i].second]) {         vis[a[v][i].second] = 1;         dfs(a[v][i].first);       }     }     pat.push_back(v);   }   inline void init() {     cin >> n >> m;     for (int i = 0; i < m + 1; i++) {       cin >> c[i];       c[i]--;     }     for (int i = 1; i < m + 1; i++) {       int v = c[i - 1], u = c[i];       a[v].push_back(make_pair(u, i - 1));       a[u].push_back(make_pair(v, i - 1));     }     for (int i = 0; i < n; i++) sort(a[i].begin(), a[i].end());   }   inline void solve() {     for (int i = m - 1; i >= 0; i--) {       memset(vis, 0, sizeof vis);       pat.clear();       for (int j = 0; j < i; j++) vis[j] = 1;       dfs(c[i], c[i + 1]);       if (i + pat.size() == m + 1) {         reverse(pat.begin(), pat.end());         for (int j = 0; j < i; j++) cout << c[j] + 1 <<    ;         for (int j = 0; j < pat.size(); j++) cout << pat[j] + 1 <<    ;         cout << endl;         return;       }     }     cout <<  No solution  << endl;   }   int main() {     ios_base::sync_with_stdio(0);     cin.tie();     cout.tie();     init();     solve();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n, m;   vector<int> edge[110000];   vector<pair<int, int> > R;   set<int> N;   int find(int rt, int to) {     int i;     for (i = 0; i < edge[rt].size(); i++) {       if (edge[rt][i] == to) return 1;     }     return 0;   }   int dfs(int rt) {     if (R.size() == m || R.size() == n - 1) return 1;     N.erase(rt);     set<int>::iterator it;     for (it = N.begin(); it != N.end(); it++) {       if (find(rt, *it)) continue;       R.push_back(make_pair(rt, *it));       if (dfs(*it)) return 1;       R.pop_back();       it = N.find(*it);     }     N.insert(rt);     return 0;   }   int main() {     int a, b, i;     scanf( %d%d , &n, &m);     for (i = 1; i <= m; i++) {       scanf( %d%d , &a, &b);       edge[a].push_back(b);       edge[b].push_back(a);     }     for (i = 1; i <= n; i++) {       N.insert(i);     }     for (i = 1; i <= n; i++) {       if (dfs(i)) {         if (n == m) R.push_back(make_pair(R[0].first, R[R.size() - 1].second));         for (i = 0; i < m; i++) printf( %d %d n , R[i].first, R[i].second);         return 0;       }     }     puts( -1 );     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     string s[40] = {          Washington ,  Adams ,      Jefferson ,  Madison ,     Monroe ,          Adams ,       Jackson ,    Van Buren ,  Harrison ,    Tyler ,          Polk ,        Taylor ,     Fillmore ,   Pierce ,      Buchanan ,          Lincoln ,     Johnson ,    Grant ,      Hayes ,       Garfield ,          Arthur ,      Cleveland ,  Harrison ,   Cleveland ,   McKinley ,          Roosevelt ,   Taft ,       Wilson ,     Harding ,     Coolidge ,          Hoover ,      Roosevelt ,  Truman ,     Eisenhower ,  Kennedy ,          Johnson ,     Nixon ,      Ford ,       Carter ,      Reagan };     int a;     cin >> a;     cout << s[a - 1] <<   n ;   }


#include <bits/stdc++.h>   using namespace std;   template <typename T>   void out(T x) {     cout << x << endl;     exit(0);   }   const int maxn = 1e6 + 5;   int n, m;   int a[maxn];   vector<int> g[maxn];   vector<pair<int, pair<int, int>>> edges;   int par[maxn], siz[maxn];   int f(int x) { return par[x] == x ? x : par[x] = f(par[x]); }   void join(int x, int y) {     x = f(x);     y = f(y);     if (x == y) return;     if (siz[x] < siz[y]) swap(x, y);     siz[x] += siz[y];     par[y] = x;   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     cin >> n >> m;     for (int i = 1; i <= n; i++) {       cin >> a[i];       par[i] = i;       siz[i] = 1;     }     for (int i = 0; i < m; i++) {       int u, v;       cin >> u >> v;       g[u].push_back(v);       g[v].push_back(u);       edges.push_back({min(a[u], a[v]), {u, v}});     }     auto cmp = [](pair<int, pair<int, int>> p1, pair<int, pair<int, int>> p2) {       return p1.first > p2.first;     };     long double num = 0;     long double den = 0;     sort(edges.begin(), edges.end(), cmp);     for (auto p : edges) {       int wei = p.first;       int u = f(p.second.first);       int v = f(p.second.second);       if (u == v) continue;       long double paths = 1LL * siz[u] * siz[v];       den += paths;       num += 1.0 * wei * paths;       join(u, v);     }     long double ans = num / den;     cout << fixed << setprecision(9) << ans << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   using ll = long long;   class flow {    private:     int n;     vector<int> head, d, cur;     vector<tuple<int, ll, int>> edge;     bool bfs(int s, int t) {       d.assign(n, -1);       queue<int> que;       que.push(s);       d[s] = 0;       while (!que.empty()) {         int u = que.front();         que.pop();         for (int i = head[u]; i != -1; i = get<2>(edge[i])) {           int v = get<0>(edge[i]);           if (get<1>(edge[i]) && d[v] == -1) {             d[v] = d[u] + 1;             if (v == t) {               return true;             }             que.push(v);           }         }       }       return false;     }     ll dfs(int u, int t, ll f) {       if (u == t) {         return f;       }       ll res = f;       for (int &i = cur[u]; i != -1 && res; i = get<2>(edge[i])) {         int v = get<0>(edge[i]);         ll &c = get<1>(edge[i]);         if (d[v] == d[u] + 1 && c) {           ll aug = dfs(v, t, min(res, c));           res -= aug;           c -= aug;           get<1>(edge[i ^ 1]) += aug;         }       }       return f - res;     }       public:     flow(int _n) {       n = _n;       head.assign(n, -1);     }     void insert_edge(int u, int v, ll c) {       edge.emplace_back(v, c, head[u]);       head[u] = edge.size() - 1;       edge.emplace_back(u, 0, head[v]);       head[v] = edge.size() - 1;     }     ll max_flow(int s, int t) {       ll flow = 0;       while (bfs(s, t)) {         cur = head;         flow += dfs(s, t, 1e18);       }       return flow;     }   };   int main() {     ios::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     int n, m;     cin >> n >> m;     vector<vector<int>> dis(n, vector<int>(n, 1e9));     for (int i = 0; i < n; ++i) {       dis[i][i] = 0;     }     for (int i = 0; i < m; ++i) {       int u, v;       cin >> u >> v;       --u;       --v;       dis[u][v] = 1;       dis[v][u] = 1;     }     for (int k = 0; k < n; ++k) {       for (int i = 0; i < n; ++i) {         for (int j = 0; j < n; ++j) {           dis[i][j] = min(dis[i][j], dis[i][k] + dis[k][j]);         }       }     }     int s, b, k;     cin >> s >> b >> k;     vector<tuple<int, int, int, int>> ship;     vector<vector<pair<int, int>>> base(n);     for (int i = 0; i < s; ++i) {       int x, a, f, p;       cin >> x >> a >> f >> p;       --x;       ship.emplace_back(x, a, f, p);     }     for (int i = 0; i < b; ++i) {       int x, d, g;       cin >> x >> d >> g;       --x;       base[x].emplace_back(d, g);     }     for (int i = 0; i < n; ++i) {       sort(base[i].begin(), base[i].end());       for (int j = 1; j < base[i].size(); ++j) {         base[i][j].second = max(base[i][j].second, base[i][j - 1].second);       }     }     vector<ll> gain(s);     for (int i = 0; i < s; ++i) {       int x = get<0>(ship[i]);       int a = get<1>(ship[i]);       int f = get<2>(ship[i]);       int p = get<3>(ship[i]);       gain[i] = -1e14;       for (int j = 0; j < n; ++j) {         if (dis[x][j] <= f && !base[j].empty() && a >= base[j][0].first) {           gain[i] =               max(gain[i], 1ll * (--upper_bound(base[j].begin(), base[j].end(),                                                 make_pair(a, (int)1e9)))                                        ->second -                                p);         }       }     }     vector<bool> used(s);     vector<pair<int, int>> rel;     for (int i = 0; i < k; ++i) {       int x, y;       cin >> x >> y;       --x;       --y;       used[x] = true;       used[y] = true;       rel.emplace_back(x, y);     }     int cnt = 0;     ll ans = 0;     vector<int> id(s, -1);     for (int i = 0; i < s; ++i) {       if (used[i]) {         id[i] = cnt++;       } else if (gain[i] > 0) {         ans += gain[i];       }     }     flow G(cnt + 2);     int S = cnt;     int T = cnt + 1;     for (int i = 0; i < s; ++i) {       if (used[i]) {         if (gain[i] > 0) {           G.insert_edge(S, id[i], gain[i]);           ans += gain[i];         } else if (gain[i] < 0) {           G.insert_edge(id[i], T, -gain[i]);         }       }     }     for (int i = 0; i < k; ++i) {       G.insert_edge(id[rel[i].first], id[rel[i].second], 1e18);     }     ans -= G.max_flow(S, T);     cout << ans << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int MOD = (int)1e9 + 7;   const int FFTMOD = 119 << 23 | 1;   const int INF = (int)1e9 + 23111992;   const long long LINF = (long long)1e18 + 23111992;   const long double PI = acos((long double)-1);   const long double EPS = 1e-9;   inline long long gcd(long long a, long long b) {     long long r;     while (b) {       r = a % b;       a = b;       b = r;     }     return a;   }   inline long long lcm(long long a, long long b) { return a / gcd(a, b) * b; }   inline long long fpow(long long n, long long k, int p = MOD) {     long long r = 1;     for (; k; k >>= 1) {       if (k & 1) r = r * n % p;       n = n * n % p;     }     return r;   }   template <class T>   inline int chkmin(T& a, const T& val) {     return val < a ? a = val, 1 : 0;   }   template <class T>   inline int chkmax(T& a, const T& val) {     return a < val ? a = val, 1 : 0;   }   inline unsigned long long isqrt(unsigned long long k) {     unsigned long long r = sqrt(k) + 1;     while (r * r > k) r--;     return r;   }   inline long long icbrt(long long k) {     long long r = cbrt(k) + 1;     while (r * r * r > k) r--;     return r;   }   inline void addmod(int& a, int val, int p = MOD) {     if ((a = (a + val)) >= p) a -= p;   }   inline void submod(int& a, int val, int p = MOD) {     if ((a = (a - val)) < 0) a += p;   }   inline int mult(int a, int b, int p = MOD) { return (long long)a * b % p; }   inline int inv(int a, int p = MOD) { return fpow(a, p - 2, p); }   inline int sign(long double x) { return x < -EPS ? -1 : x > +EPS; }   inline int sign(long double x, long double y) { return sign(x - y); }   mt19937 mt(chrono::high_resolution_clock::now().time_since_epoch().count());   inline int mrand() { return abs((int)mt()); }   inline int mrand(int k) { return abs((int)mt()) % k; }   template <class num_t>   struct SegmentTree {     struct node_t {       num_t max[2];       num_t pref[2];       num_t suff[2];       num_t pref2[2];       num_t suff2[2];       num_t lz;       int size;       node_t() {         lz = 0;         size = -1;       }     };     int n;     vector<node_t> st;     void init(int n_) {       n = n_;       st.resize(n << 2);     }     node_t merge(node_t u, node_t v) {       if (u.size == -1) return v;       if (v.size == -1) return u;       node_t c;       for (int i = (0); i < (2); ++i) {         c.max[i] = max(u.max[i], v.max[i]);         c.pref[i] = u.pref[i];         c.suff[i] = v.suff[i];         c.pref2[i] = u.pref2[i];         c.suff2[i] = v.suff2[i];       }       for (int i = (0); i < (2); ++i) {         if (u.pref[i] == u.size) {           c.pref[i] = u.pref[i] + v.pref[i];         }         if (v.suff[i] == v.size) {           c.suff[i] = u.suff[i] + v.suff[i];         }         if (u.pref[i ^ 1] == u.size) {           c.pref2[i] = u.pref[i ^ 1] + v.pref2[i];         }         if (v.suff[i ^ 1] == v.size) {           c.suff2[i] = u.suff2[i] + v.suff[i ^ 1];         }         if (u.pref2[i] == u.size) {           chkmax(c.pref2[i], u.pref2[i] + v.pref[i]);         }         if (v.suff2[i] == v.size) {           chkmax(c.suff2[i], u.suff[i] + v.suff2[i]);         }       }       for (int i = (0); i < (2); ++i) {         chkmax(c.pref2[i], c.pref[i]);         chkmax(c.suff2[i], c.suff[i]);       }       for (int i = (0); i < (2); ++i) {         chkmax(c.max[i], c.pref[i]);         chkmax(c.max[i], c.suff[i]);         chkmax(c.max[i], c.pref2[i]);         chkmax(c.max[i], c.suff2[i]);         chkmax(c.max[i], u.suff[i] + v.pref2[i]);         chkmax(c.max[i], u.suff2[i] + v.pref[i]);       }       c.size = u.size + v.size;       return c;     }     void build(int p, int L, int R, vector<int>& a) {       if (L == R) {         st[p].max[0] = st[p].max[1] = 1;         for (int i = (0); i < (2); ++i) {           st[p].pref[i] = st[p].pref2[i] = (a[L] == i);           st[p].suff[i] = st[p].suff2[i] = (a[R] != i);         }         st[p].size = 1;         return;       }       build(p << 1, L, L + R >> 1, a);       build(p << 1 | 1, (L + R >> 1) + 1, R, a);       st[p] = merge(st[p << 1], st[p << 1 | 1]);     }     void push(int p, int L, int R) {       if (st[p].lz) {         swap(st[p].max[0], st[p].max[1]);         swap(st[p].pref[0], st[p].pref[1]);         swap(st[p].suff[0], st[p].suff[1]);         swap(st[p].pref2[0], st[p].pref2[1]);         swap(st[p].suff2[0], st[p].suff2[1]);         if (L < R) {           st[p << 1].lz ^= st[p].lz;           st[p << 1 | 1].lz ^= st[p].lz;         }         st[p].lz = 0;       }     }     void upd(int p, int l, int r, int L, int R, num_t val) {       push(p, L, R);       if (r < L || R < l) return;       if (l <= L && R <= r) {         st[p].lz = val;         push(p, L, R);         return;       }       upd(p << 1, l, r, L, L + R >> 1, val);       upd(p << 1 | 1, l, r, (L + R >> 1) + 1, R, val);       st[p] = merge(st[p << 1], st[p << 1 | 1]);     }     node_t query(int p, int l, int r, int L, int R) {       push(p, L, R);       if (r < L || R < l) return node_t();       if (l <= L && R <= r) {         return st[p];       }       return merge(query(p << 1, l, r, L, L + R >> 1),                    query(p << 1 | 1, l, r, (L + R >> 1) + 1, R));     }     void upd(int l, int r, num_t val) { upd(1, l, r, 0, n - 1, val); }     node_t query(int l, int r) { return query(1, l, r, 0, n - 1); }   };   SegmentTree<int> seg;   void chemthan() {     int n, q;     cin >> n >> q;     string s;     cin >> s;     vector<int> a(n);     for (int i = (0); i < (n); ++i) a[i] = s[i] ==  > ;     seg.init(n);     seg.build(1, 0, n - 1, a);     while (q--) {       int l, r;       cin >> l >> r;       l--, r--;       seg.upd(l, r, 1);       cout << seg.query(l, r).max[0] <<   n ;     }   }   int main(int argc, char* argv[]) {     ios_base::sync_with_stdio(0), cin.tie(0);     if (argc > 1) {       assert(freopen(argv[1],  r , stdin));     }     if (argc > 2) {       assert(freopen(argv[2],  wb , stdout));     }     chemthan();     cerr <<   nTime elapsed:   << 1000 * clock() / CLOCKS_PER_SEC <<  ms n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main(int argc, char** argv) {     int T;     cin >> T;     while (T--) {       long long a, b, c, d;       cin >> a >> b >> c >> d;       d -= min(a, d / c) * c;       if (d > b)         puts( NO );       else         puts( YES );     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     string s[10];     s[0] =  O-|-OOOO ;     s[1] =  O-|O-OOO ;     s[2] =  O-|OO-OO ;     s[3] =  O-|OOO-O ;     s[4] =  O-|OOOO- ;     s[5] =  -O|-OOOO ;     s[6] =  -O|O-OOO ;     s[7] =  -O|OO-OO ;     s[8] =  -O|OOO-O ;     s[9] =  -O|OOOO- ;     int n;     cin >> n;     if (n == 0) cout << s[0] << endl;     while (n != 0) {       int z = n % 10;       cout << s[z] << endl;       n /= 10;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int ROW[] = {+0, +0, -1, +1};   int COL[] = {+1, -1, +0, +0};   int X[] = {+0, +0, +1, -1, -1, +1, -1, +1};   int Y[] = {-1, +1, +0, +0, +1, +1, -1, -1};   int KX[] = {-2, -2, -1, -1, 1, 1, 2, 2};   int KY[] = {-1, 1, -2, 2, -2, 2, -1, 1};   long long calc(long long num, long long val, long long cnt1, long long cnt2) {     while (cnt1 >= cnt2) {       num /= val;       cnt1--;     }     return num;   }   int main() {     int t;     scanf( %d , &t);     while (t--) {       long long p, q, n, m;       scanf( %lld %lld , &p, &q);       n = p;       m = q;       if (p % q) {         printf( %lld n , p);         continue;       }       map<long long, long long> mp, mpp;       for (long long i = 2; i * i <= q; i++) {         if (q % i) continue;         while (q % i == 0) {           mp[i]++;           q /= i;         }       }       if (q > 1) mp[q]++;       long long ans = 1LL;       for (auto it : mp) {         while (p % it.first == 0) {           p /= it.first;           mpp[it.first]++;         }         ans = max(ans, calc(n, it.first, mpp[it.first], it.second));       }       printf( %lld n , ans);     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   string s;   int poz, sol;   int main() {     sol = INT_MAX;     cin >> s;     int n = s.size();     for (int c = 1; c <= 26; c++) {       char ch = c +  a  - 1;       poz = -1;       int maxx = 0;       for (int i = 0; i < n; i++) {         if (s[i] == ch) {           if (i - poz > maxx) maxx = i - poz;           poz = i;         }       }       if (n - poz > maxx) maxx = n - poz;       if (sol > maxx) sol = maxx;     }     cout << sol;     return 0;   }


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


#include <bits/stdc++.h>   using namespace std;   int32_t main() {     ios::sync_with_stdio(false);     cin.tie(0);     long long n, w;     cin >> n >> w;     vector<long long> arr(n);     for (long long i = 0; i < n; ++i) cin >> arr[i];     vector<long long> runsum(n);     long long min_elem = arr[0];     long long max_elem = arr[0];     vector<long long> run(n);     long long cur = 0;     long long tillnow = -1e9;     for (long long i = 0; i < n; ++i) {       cur = max(arr[i], cur + arr[i]);       tillnow = max(tillnow, cur);     }     if (tillnow > w) {       cout << 0 << endl;       return 0;     }     max_elem = tillnow;     cur = 0;     tillnow = 1e9;     for (long long i = 0; i < n; ++i) {       cur = min(arr[i], cur + arr[i]);       tillnow = min(tillnow, cur);     }     if (tillnow < -w) {       cout << 0 << endl;       return 0;     }     min_elem = tillnow;     long long answer = w - max(abs(max_elem), abs(min_elem)) + 1;     cout << answer << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   void ASS(bool bb) {     if (!bb) {       ++*(int*)0;     }   }   #pragma comment(linker,  /STACK:106777216 )   int main() {     int a, b, m;     cin >> a >> b >> m;     for (int i = 0; i < (int)(min(a + 1, m)); i++) {       int x = i * (long long)1000000000 % m;       x = (m - x) % m;       if (x > b) {         printf( 1 %09d n , i);         return 0;       }     }     cout << 2 << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int start, ed = 0, sz = 130000000;   long long marked[(130000000 / 64) + 5];   vector<int> primes;   inline bool check(int pos) { return !(marked[pos / 64] & (1LL << (pos % 64))); }   int pos;   bool isPrime(int n) {     if (n != 2 and n % 2 == 0) return false;     int sq = sqrt(n);     for (int i = 3; i <= sq; i += 2) {       if (n % i == 0) return false;     }     return true;   }   void genPrimes(int n) {     int sq = sqrt(n);     for (int i = 2; i <= sq; i++) {       if (isPrime(i)) primes.push_back(i);     }   }   void segSieve(int L, int R) {     if (L <= 1 and R >= 1)       pos = 1 - start, marked[pos / 64] |= (1LL << (pos % 64));     int sq = sqrt(R), st;     for (int i = 0; primes[i] <= sq and i < primes.size(); i++) {       if (L <= primes[i])         st = primes[i] + primes[i];       else         st = ((L + primes[i] - 1) / primes[i]) * primes[i];       for (int j = st; j <= R; j += primes[i]) {         pos = j - start, marked[pos / 64] |= (1LL << (pos % 64));       }     }   }   unsigned int ans = 0, a, b, c, d;   inline unsigned int F(unsigned int x) { return x * (x * (a * x + b) + c) + d; }   void call(int n) {     int sum = 0;     long long cur;     for (int i = 2; i <= n; i++) {       if (i > ed) {         memset(marked, 0, sizeof(marked));         start = ed + 1;         ed = start + sz - 1;         segSieve(start, min(ed, 300000000));       }       if (!check(i - start)) continue;       cur = i, sum = 0;       while (cur <= n) {         sum += (n / cur);         cur *= i;       }       ans += sum * F(i);     }   }   int main() {     int n;     cin >> n >> a >> b >> c >> d;     genPrimes(n);     call(n);     cout << ans << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   struct node {     int l, r;     int num;   } Treex[4000009], Treey[4000009];   map<int, int> vis;   int i;   int tot = 1;   int Tot = 1;   int x, y;   char str[5];   void update_y(int ll, int rr, int c, int l, int r, int t) {     if (ll <= l && r <= rr)       Treey[t].num = max(Treey[t].num, c);     else {       if (ll <= ((l + r) >> 1)) {         if (!Treey[t].l) Treey[t].l = ++tot;         update_y(ll, rr, c, l, ((l + r) >> 1), Treey[t].l);       }       if (rr > ((l + r) >> 1)) {         if (!Treey[t].r) Treey[t].r = ++tot;         update_y(ll, rr, c, ((l + r) >> 1) + 1, r, Treey[t].r);       }     }   }   void update_x(int ll, int rr, int c, int l, int r, int t) {     if (ll <= l && r <= rr)       Treex[t].num = max(Treex[t].num, c);     else {       if (ll <= ((l + r) >> 1)) {         if (!Treex[t].l) Treex[t].l = ++Tot;         update_x(ll, rr, c, l, ((l + r) >> 1), Treex[t].l);       }       if (rr > ((l + r) >> 1)) {         if (!Treex[t].r) Treex[t].r = ++Tot;         update_x(ll, rr, c, ((l + r) >> 1) + 1, r, Treex[t].r);       }     }   }   void query_x(int c, int l, int r, int rt) {     i = max(i, Treex[rt].num);     if (l == r) return;     if (((l + r) >> 1) >= c)       query_x(c, l, ((l + r) >> 1), Treex[rt].l);     else       query_x(c, ((l + r) >> 1) + 1, r, Treex[rt].r);   }   void query_y(int c, int l, int r, int rt) {     i = max(i, Treey[rt].num);     if (l == r) return;     if (((l + r) >> 1) >= c)       query_y(c, l, ((l + r) >> 1), Treey[rt].l);     else       query_y(c, ((l + r) >> 1) + 1, r, Treey[rt].r);   }   int main() {     int n, q;     scanf( %d%d , &n, &q);     update_x(0, n, 0, 0, n, 1);     update_y(0, n, 0, 0, n, 1);     while (q--) {       scanf( %d%d%s , &x, &y, str);       if (vis[x]) {         puts( 0 );       } else {         vis[x] = 1;         i = 0;         if (str[0] ==  U ) {           query_x(x, 0, n, 1);           printf( %d n , y - i);           update_y(i, y, x, 0, n, 1);         } else {           query_y(y, 0, n, 1);           printf( %d n , x - i);           update_x(i, x, y, 0, n, 1);         }       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long long PINF = 1E9;   const long long NINF = -1E9;   const long long M = 1E9 + 7;   const double EPS = 1E-9;   const double PI = 2 * acos(0);   void print_arr(const vector<long long> &a) {     for (int i = 0; i < (int)a.size(); i++) cout << a[i] <<    ;     cout <<   n ;   }   int m;   vector<long long> weights, ans;   bool visited[1001][11][11];   void dfs(int step, int balance, int prev_weight) {     if (visited[step][balance][prev_weight]) return;     visited[step][balance][prev_weight] = true;     if (step == m) {       cout <<  YES             <<   n ;       print_arr(ans);       exit(0);     }     for (int i = 0; i < (int)weights.size(); i++) {       int w = weights[i];       if (prev_weight != w && abs(balance) < w) {         ans[step] = w;         if (step % 2 == 0)           dfs(step + 1, balance + w, w);         else           dfs(step + 1, balance - w, w);       }       ans[step] = 0;     }     visited[step][balance][prev_weight] = false;   }   int main() {     ios::sync_with_stdio(false);     cin.tie(NULL);     string s;     cin >> s;     for (int i = 0; i < 10; i++) {       if (s[i] ==  1 ) weights.push_back(i + 1);     }     cin >> m;     ans.resize(m);     dfs(0, 0, 0);     cout <<  NO           <<   n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   template <class T>   inline T bigmod(T p, T e, T M) {     long long ret = 1;     for (; e > 0; e >>= 1) {       if (e & 1) ret = (ret * p) % M;       p = (p * p) % M;     }     return (T)ret;   }   template <class T>   inline T gcd(T a, T b) {     if (b == 0) return a;     return gcd(b, a % b);   }   template <class T>   inline T modinverse(T a, T M) {     return bigmod(a, M - 2, M);   }   template <class T>   inline T lcm(T a, T b) {     a = abs(a);     b = abs(b);     return (a / gcd(a, b)) * b;   }   template <class T, class X>   inline bool getbit(T a, X i) {     T t = 1;     return ((a & (t << i)) > 0);   }   template <class T, class X>   inline T setbit(T a, X i) {     T t = 1;     return (a | (t << i));   }   template <class T, class X>   inline T resetbit(T a, X i) {     T t = 1;     return (a & (~(t << i)));   }   inline long long power(long long a, long long b) {     long long multiply = 1;     for (int i = (0); i < (b); i++) {       multiply *= a;     }     return multiply;   }   vector<int> v[102];   int main() {     int n, m, x;     int first = 0, second = 0;     cin >> n;     for (int i = (0); i < (n); i++) {       cin >> m;       for (int j = (0); j < (m); j++) {         cin >> x;         v[i].push_back(x);       }     }     vector<int> rest;     for (int i = (0); i < (n); i++) {       if (v[i].size() % 2 == 0) {         for (int j = (0); j < (v[i].size() / 2); j++) first += v[i][j];         for (int j = (v[i].size() / 2); j < (v[i].size()); j++) second += v[i][j];       } else {         for (int j = (0); j < (v[i].size() / 2); j++) first += v[i][j];         for (int j = (v[i].size() / 2 + 1); j < (v[i].size()); j++)           second += v[i][j];       }     }     for (int j = (0); j < (n); j++) {       if (v[j].size() % 2 == 0) continue;       rest.push_back(v[j][v[j].size() / 2]);     }     sort(rest.begin(), rest.end(), greater<int>());     for (int j = (0); j < (rest.size()); j++) {       if (j % 2 == 0)         first += rest[j];       else         second += rest[j];     }     cout << first <<     << second << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   struct edge {     int t, nxt;   } e[600010];   int n, cnt, be[300010], mn_mx[300010], f, vid[300010], tot;   void add(const int& x, const int& y) {     e[++cnt].t = y, e[cnt].nxt = be[x], be[x] = cnt;   }   void dfs(const int& x) {     if (!be[x]) return (void)(++tot, vid[x] = 1);     if (mn_mx[x]) {       vid[x] = 1e9;       for (int i = be[x]; i; i = e[i].nxt)         dfs(e[i].t), vid[x] = min(vid[x], vid[e[i].t]);     } else       for (int i = be[x]; i; i = e[i].nxt) dfs(e[i].t), vid[x] += vid[e[i].t];   }   int main() {     scanf( %d , &n);     for (int i = 1; i <= n; ++i) scanf( %d , &mn_mx[i]);     for (int i = 2; i <= n; ++i) scanf( %d , &f), add(f, i);     dfs(1), printf( %d , tot - vid[1] + 1);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long int mod = 1000000007;   long long int MAX = 1e18;   void solve() {     long long int n;     scanf( %lld , &n);     long long int a[n];     vector<pair<long long int, long long int> > v(n), ans;     for (long long int i = 0; i < n; i++) {       long long int num;       scanf( %lld , &num);       a[i] = num;       v[i] = {num - 1, i};     }     sort(begin(v), end(v));     for (long long int i = n / 2, j = n / 2 - 1; i < n; i++, j--) {       if (v[i].second > i) {         ans.push_back({v[i].second + 1, 1});         ans.push_back({1, i + 1});         ans.push_back({v[i].second + 1, 1});         v[a[i] - 1].second = v[i].second;         swap(a[i], a[v[i].second]);       } else if (v[i].second < i) {         ans.push_back({v[i].second + 1, n});         ans.push_back({n, 1});         ans.push_back({1, i + 1});         ans.push_back({n, 1});         ans.push_back({v[i].second + 1, n});         v[a[i] - 1].second = v[i].second;         swap(a[i], a[v[i].second]);       }       if (v[j].second < j) {         ans.push_back({v[j].second + 1, n});         ans.push_back({j + 1, n});         ans.push_back({n, v[j].second + 1});         v[a[j] - 1].second = v[j].second;         swap(a[j], a[v[j].second]);       } else if (v[j].second > j) {         ans.push_back({v[j].second + 1, 1});         ans.push_back({1, n});         ans.push_back({j + 1, n});         ans.push_back({n, 1});         ans.push_back({v[j].second + 1, 1});         v[a[j] - 1].second = v[j].second;         swap(a[j], a[v[j].second]);       }     }     cout << ans.size() << endl;     for (long long int i = 0; i < ans.size(); i++)       printf( %lld %lld n , ans[i].first, ans[i].second);   }   int main() {     long long int t = 1;     while (t--) {       solve();     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     long long int n, c = 0;     cin >> n;     long long int d;     cin >> d;     long long int s[n + 5];     for (long long int i = 0; i < n; i++) {       cin >> s[i];     }     sort(s, s + n);     for (long long int i = 0; i < n - 1; i++) {       for (long long int j = i + 1; j < n; j++) {         if (s[j] - s[i] <= d) {           c++;         }       }     }     cout << c * 2 << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long power(long long x, long long y, long long p);   inline int D() {     int t;     scanf( %d , &t);     return t;   }   inline long long llD() {     long long t;     scanf( %lld , &t);     return t;   }   const int N = 1e6 + 5;   long long arr[N], cost[N];   int main() {     long long n = llD(), k = llD(), l = 1, r = 0, mid = -1;     for (int i = 0; i < n; ++i) r += pow(2, i);     while (mid != k) {       mid = (l + r) / 2;       if (mid > k)         r = mid - 1, --n;       else if (mid < k)         l = mid + 1, --n;     }     printf( %lld , n);   }   long long power(long long x, long long y, long long p) {     long long res = 1;     x = x % p;     while (y > 0) {       if (y & 1) res = (res * x) % p;       y = y >> 1;       x = (x * x) % p;     }     return res;   }


#include <bits/stdc++.h>   using namespace std;   const int M = 1e6 + 20;   struct Edge {     int to, w, next;   } edge[M];   int head[M], cnt, fa[M];   inline void add(int u, int v, int w) {     edge[++cnt].to = v, edge[cnt].w = w, edge[cnt].next = head[u], head[u] = cnt;   }   long long mxp[M];   int val[M];   long long dfs1(int now) {     mxp[now] = val[now];     for (int i = head[now]; i; i = edge[i].next) {       if (edge[i].to == fa[now]) continue;       fa[edge[i].to] = now,       mxp[now] = max(mxp[now], val[now] - edge[i].w + dfs1(edge[i].to));     }     return mxp[now];   }   int main() {     int n;     scanf( %d , &n);     for (int i = 1; i <= n; ++i) scanf( %d , &val[i]);     for (int i = 1; i <= n - 1; ++i) {       int u, v, w;       scanf( %d%d%d , &u, &v, &w), add(u, v, w), add(v, u, w);     }     dfs1(1);     long long ans = 0;     for (int i = 1; i <= n; ++i) {       long long mx1 = 0, mx2 = 0;       for (int j = head[i]; j; j = edge[j].next) {         if (edge[j].to == fa[i]) continue;         long long c = mxp[edge[j].to] - edge[j].w;         if (c > mx1)           mx2 = mx1, mx1 = c;         else if (c > mx2)           mx2 = c;       }       ans = max(ans, val[i] + mx1 + mx2);     }     printf( %lld n , ans);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 1e5 + 5;   int main() {     int n;     scanf( %d , &n);     vector<vector<pair<int, int>>> a(n);     for (int i = 0; i < n - 1; i++) {       int u, v;       scanf( %d %d , &u, &v);       --u, --v;       a[u].emplace_back(v, i);       a[v].emplace_back(u, i);     }     vector<int> lev(n, 1), pr(n);     function<void(int, int)> dfs = [&](int u, int p) {       pr[u] = p;       for (auto [v, id] : a[u]) {         if (v != p) {           lev[v] = 1 + lev[u];           dfs(v, u);         }       }     };     dfs(0, -1);     vector<vector<int>> sp(n, vector<int>(22, -1));     for (int i = 0; i < 22; i++) {       for (int j = 0; j < n; j++) {         if (i == 0) {           sp[j][i] = pr[j];         } else if (sp[j][i - 1] != -1) {           sp[j][i] = sp[sp[j][i - 1]][i - 1];         }       }     }     function<int(int, int)> lca = [&](int u, int v) {       if (lev[u] < lev[v]) {         swap(u, v);       }       int lg = 0;       while ((1 << lg) <= lev[u]) {         ++lg;       }       --lg;       for (int i = lg; i >= 0; i--) {         if (lev[u] >= lev[v] + (1 << i)) {           u = sp[u][i];         }       }       if (u == v) {         return u;       }       for (int i = lg; i >= 0; i--) {         if (sp[u][i] != sp[v][i]) {           u = sp[u][i];           v = sp[v][i];         }       }       return (u == v) ? u : pr[u];     };     int k;     scanf( %d , &k);     vector<int> val(n);     for (int i = 0; i < k; i++) {       int u, v;       scanf( %d %d , &u, &v);       --u, --v;       int g = lca(u, v);       ++val[u], ++val[v];       val[g] -= 2;     }     vector<int> ans(n - 1);     function<void(int, int)> dfs2 = [&](int u, int p) {       for (auto [v, id] : a[u]) {         if (v != p) {           dfs2(v, u);           val[u] += val[v];           ans[id] = val[v];         }       }     };     dfs2(0, -1);     for (int v : ans) {       printf( %d  , v);     }     puts(  );     return 0;   }