#include <bits/stdc++.h>   using namespace std;   long long a[1000010];   long long l[1000010], r[1000010];   vector<pair<int, int>> g1[1000010], g2[1000010];   int n, q;   long long ans[1000010];   const int N = 1000000;   long long sum[N * 4];   long long flag[N * 4];   int cnt_v;   void build(int n) {     cnt_v = 1;     while (cnt_v <= n) {       cnt_v <<= 1;     }     memset(sum, 0, sizeof(sum));     memset(flag, 0, sizeof(flag));   }   void push_down(int t, long long l, long long r) {     if (flag[t]) {       long long mid = (l + r) >> 1;       sum[t << 1] += flag[t] * (mid - l + 1);       sum[(t << 1) + 1] += flag[t] * (r - mid);       flag[t << 1] += flag[t];       flag[(t << 1) + 1] += flag[t];       flag[t] = 0;     }   }   void push_up(int t) { sum[t] = sum[t << 1] + sum[(t << 1) + 1]; }   void upd(int x, int lx, int rx, int l, int r) {     if (lx > rx) return;     if (l > rx || r < lx) return;     if (lx >= l && rx <= r) {       sum[x] += (rx - lx + 1);       flag[x]++;       return;     }     {       push_down(x, lx, rx);       upd(x * 2, lx, (lx + rx) / 2, l, r);       upd(x * 2 + 1, (lx + rx) / 2 + 1, rx, l, r);       push_up(x);     }   }   long long get(int x, int lx, int rx, int l, int r) {     if (lx > rx) return 0;     if (l > rx || r < lx) return 0;     if (lx >= l && rx <= r) {       return sum[x];     } else {       push_down(x, lx, rx);       return get(x * 2, lx, (lx + rx) / 2, l, r) +              get(x * 2 + 1, (lx + rx) / 2 + 1, rx, l, r);     }   }   void init() { build(n); }   void upd(long long l, long long r, long long val) { upd(1, 1, cnt_v, l, r); }   long long get_sum(long long l, long long r) {     long long t = get(1, 0, cnt_v, l, r);     return t;   }   pair<int, int> st[1000040];   int main() {     scanf( %d%d , &n, &q);     for (int i = 1; i < n + 1; ++i) {       scanf( %lld , &a[i]);     }     for (int i = 0; i < q; ++i) {       scanf( %lld , &l[i]);     }     for (int i = 0; i < q; ++i) {       scanf( %lld , &r[i]);     }     for (int i = 0; i < q; ++i) {       g1[l[i]].push_back(make_pair(r[i], i));       g2[r[i]].push_back(make_pair(l[i], i));     }     init();     stack<pair<long long, int>> st;     st.push(make_pair(10000000, 0));     for (int i = 1; i < n + 1; ++i) {       while (!st.empty() && a[i] > st.top().first) {         st.pop();       }       long long s = st.top().second + 1;       long long e = i;       upd(s, e, 1);       st.push(make_pair(a[i], i));       for (int j = 0; j < g2[i].size(); ++j) {         long long s = g2[i][j].first;         int id = g2[i][j].second;         ans[id] += get_sum(s, i);       }     }     init();     while (!st.empty()) st.pop();     st.push(make_pair(10000000, n + 1));     for (int i = n; i > 0; --i) {       while (!st.empty() && a[i] > st.top().first) {         st.pop();       }       long long s = i;       long long e = st.top().second - 1;       upd(s, e, 1);       st.push(make_pair(a[i], i));       for (int j = 0; j < g1[i].size(); ++j) {         long long e = g1[i][j].first;         int id = g1[i][j].second;         ans[id] += get_sum(i, e);       }     }     for (int i = 0; i < q; ++i) {       printf( %lld  , ans[i] - (r[i] - l[i] + 1));     }     cout << endl;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 404040;   int cnt, cnt2, cnt3, h, n, m;   struct Node {     int x, y, w, d, i;   } a[N];   bool cmp(Node x, Node y) { return x.d < y.d; }   vector<int> s[N];   vector<pair<int, int> > ss[N];   int dfn[N], low[N], viss[N], q[N], color[N], as[N];   map<int, int> sum, vis;   void jiantu(int x, int y) { s[x].push_back(y); }   void tarjan(int x) {     int c, i, y;     q[h++] = x, viss[x] = 1;     dfn[x] = low[x] = ++cnt2;     c = s[x].size();     for (i = 0; i < c; i++) {       y = s[x][i];       if (!dfn[y])         tarjan(y), low[x] = min(low[x], low[y]);       else if (viss[y])         low[x] = min(low[x], dfn[y]);     }     if (low[x] == dfn[x]) {       cnt3++;       while (1) {         h--;         color[q[h]] = cnt3;         viss[q[h]] = 0;         if (q[h] == x) {           q[h] = 0;           break;         }         q[h] = 0;       }     }   }   int check(int x) {     int i, ans = 1;     for (i = x + 1; i <= m; i++) jiantu(i + i, i + i - 1);     cnt2 = 0, cnt3 = 0;     memset(dfn, 0, sizeof(dfn));     memset(low, 0, sizeof(low));     memset(viss, 0, sizeof(viss));     memset(color, 0, sizeof(color));     for (i = 1; i <= cnt; i++)       if (!dfn[i]) h = 0, tarjan(i);     for (i = 1; i <= m; i++)       if (color[i + i - 1] == color[i + i]) ans = 0;     for (i = x + 1; i <= m; i++) s[i + i].pop_back();     return ans;   }   int erf(int l, int r) {     while (l <= r) {       int mid = l + r >> 1;       if (check(mid))         r = mid - 1;       else         l = mid + 1;     }     return l;   }   void init() {     int c, i, y, j, pl;     cnt = m + m;     for (i = 1; i <= n; i++) {       c = ss[i].size();       for (j = 0; j < c; j++) {         y = ss[i][j].first;         pl = ss[i][j].second;         sum[a[pl].w]++;         if (sum[a[pl].w] == 3) {           puts( No );           exit(0);         } else if (sum[a[pl].w] == 2) {           jiantu(vis[a[pl].w] * 2 - 1, pl + pl);           jiantu(pl + pl - 1, vis[a[pl].w] * 2);         }         vis[a[pl].w] = pl;       }       for (j = 0; j < c; j++) sum[a[ss[i][j].second].w]--;       for (j = 0; j < c; j++) {         y = ss[i][j].second;         if (j != c - 1) {           cnt++;           if (j != 0) jiantu(cnt, cnt - 1);           jiantu(cnt, y + y - 1);         }         if (j != 0) {           if (j != c - 1)             jiantu(y + y, cnt - 1);           else             jiantu(y + y, cnt);         }       }       for (j = c - 1; j >= 0; j--) {         y = ss[i][j].second;         if (j != 0) {           cnt++;           jiantu(cnt, y + y - 1);           if (j != c - 1) jiantu(cnt, cnt - 1);         }         if (j != c - 1) {           if (j != 0)             jiantu(y + y, cnt - 1);           else             jiantu(y + y, cnt);         }       }     }   }   int main() {     int i, ans, cnt;     scanf( %d%d , &n, &m);     for (i = 1; i <= m; i++)       scanf( %d%d%d%d , &a[i].x, &a[i].y, &a[i].w, &a[i].d), a[i].i = i;     sort(a + 1, a + m + 1, cmp);     for (i = 1; i <= m; i++)       ss[a[i].x].push_back(make_pair(a[i].y, i)),           ss[a[i].y].push_back(make_pair(a[i].x, i));     init();     ans = erf(0, m);     if (!check(ans)) {       puts( No );       return 0;     }     puts( Yes );     cnt = 0;     for (i = 1; i <= m; i++)       if (color[i + i - 1] > color[i + i]) as[++cnt] = a[i].i;     printf( %d %d n , a[ans].d, cnt);     for (i = 1; i <= cnt; i++) printf( %d  , as[i]);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int maxn = 2 * 100000, maxk = 2 * 100000, inf = 0x3f3f3f3f;   int n, k, s, t;   struct car {     int c, v;   } a[maxn + 10];   int d[maxk + 10];   int ans = inf;   void init() {     scanf( %d%d%d%d , &n, &k, &s, &t);     for (int i = 1; i <= n; i++) scanf( %d%d , &a[i].c, &a[i].v);     for (int i = 1; i <= k; i++) scanf( %d , &d[i]);     d[k + 1] = s;   }   bool cmp(car x, car y) { return x.v < y.v; }   bool check(int x) {     double time = 0;     for (int i = 1; i <= k + 1; i++) {       double x1, x2;       int tmp = d[i] - d[i - 1];       x1 = (double)(max(0, 2 * tmp - a[x].v));       x2 = 2 * (tmp - x1);       if (x2 < 0) return false;       time += 2 * x1 + x2 / 2;       if (time > t) return false;     }     return true;   }   void solve() {     sort(a + 1, a + 1 + n, cmp);     sort(d + 1, d + 1 + k + 1);     int l = 1, r = n;     int mm = 0;     while (l <= r) {       int mid = (l + r) >> 1;       if (check(mid))         mm = mid, r = mid - 1;       else         l = mid + 1;     }     if (mm == 0)       printf( -1 );     else {       for (int i = mm; i <= n; i++) ans = min(ans, a[i].c);       printf( %d n , ans);     }   }   int main() {     init();     solve();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int sol(const vector<long long>& a, int n, int m, long long t) {     long long s = 0;     for (int i = 0; i < m; i++) s += a[i];     int ans = 0;     for (int i = 0; i <= n && s * i <= t; i++) {       int cnt = (m + 1) * i;       long long r = t - s * i;       for (int j = 0; j < m && r >= a[j]; j++) {         int c = min(r / a[j], (long long)(n - i));         r -= a[j] * c;         cnt += c;       }       ans = max(ans, cnt);     }     return ans;   }   int main() {     int n, m;     long long t;     scanf( %d%d%lld , &n, &m, &t);     vector<long long> a(m);     for (int i = 0; i < m; i++) scanf( %lld , &a[i]);     sort(a.begin(), a.end());     printf( %d n , sol(a, n, m, t));   }


#include <bits/stdc++.h>   using namespace std;   const int M = 19, N = (1 << M);   int a[N];   int32_t main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     int n;     cin >> n;     int id = -1;     for (int i = 0; i < n; i++) {       cin >> a[i];       if (a[i] == -1) id = i;     }     int rem = n - id - 1;     int t = 0;     long long ans = 0;     while (rem > 0) {       int mn = n - 1;       for (int i = n - 1; i >= n / (1 << t) - 1; i--)         if (a[mn] > a[i]) mn = i;       ans += a[mn];       a[mn] = 1e9 + 10;       rem -= n / (1 << (t + 1));       t++;       assert(t <= 20);       cerr << t <<   ****  n ;     }     cout << ans <<   n ;   }


#include <bits/stdc++.h>   using namespace std;   int n, t;   int main() {     cin >> n >> t;     long long z = pow(10, n - 2);     if (n == 1 and t == 10) return cout << -1 << endl, 0;     if (n == 1) return cout << t << endl, 0;     if (t == 2 or t == 4 or t == 5 or t == 8 or t == 10)       cout << 40;     else if (t == 7)       cout << 14;     else       cout << 18;     n -= 2;     while (n) {       cout <<  0 ;       n--;     }     cout << endl;   }


#include <bits/stdc++.h>   using namespace std;   long long t, n, sum, t1, ans;   string st;   int main() {     cin >> t;     while (t--) {       cin >> n;       cin >> st;       int a = 0, b = 0;       for (int i = 0; i < n; i++) {         if (st[i] ==  ( ) a++;         if (st[i] ==  ) ) {           if (a)             a--;           else             b++;         }       }       cout << min(a, b) <<   n ;     }   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int t;     cin >> t;     for (int i = 0; i < t; i++) {       int n, count = 1;       cin >> n;       while (n > 0) {         if (count % 3 != 0 && count % 10 != 3) {           count++;           n--;         } else {           count++;         }       }       int ans = count - 1;       cout << ans << endl;     }   }


#include <bits/stdc++.h>   using namespace std;   const int N = 1 << 23;   const int offset = (int)1e6;   int st_sum[2 * N];   int st_ma[2 * N];   int getMax(int a, int b) {     a += N + offset;     b += N + offset;     int ma = -1e9;     int sum_a = 0;     int ma_b = 0;     for (; a < b; a /= 2, b /= 2) {       if (a % 2 == 1) {         ma = max(ma, st_ma[a] + sum_a);         sum_a += st_sum[a];         ++a;       }       if (b % 2 == 0) {         ma_b = max(ma_b + st_sum[b], st_ma[b]);         --b;       }     }     if (a == b) {       ma = max(ma, st_ma[a] + sum_a);       sum_a += st_sum[a];     }     ma = max(ma, sum_a + ma_b);     return ma;   }   void updateSegtreePos(int pos) {     st_sum[pos] = st_sum[pos * 2] + st_sum[pos * 2 + 1];     st_ma[pos] = max(st_ma[pos * 2], st_sum[pos * 2] + st_ma[pos * 2 + 1]);   }   void addValue(int pos, int val) {     pos += N + offset;     st_sum[pos] += val;     st_ma[pos] += val;     for (pos /= 2; pos; pos /= 2) {       updateSegtreePos(pos);     }   }   set<int> pawns[(int)2e5 + 10];   multiset<int> intercept_ys;   int main() {     int n, k, m;     cin >> n >> k >> m;     for (int i = 0; i < N; ++i) {       st_sum[i + N] = -1;       st_ma[i + N] = -1;     }     for (int i = N - 1; i; --i) {       updateSegtreePos(i);     }     for (int i = 0; i < m; ++i) {       int x, y;       cin >> x >> y;       int intercept_y = (n - y) - abs(x - k);       if (pawns[x].count(y)) {         addValue(intercept_y, -1);         pawns[x].erase(y);         intercept_ys.erase(intercept_ys.find(intercept_y));       } else {         addValue(intercept_y, 1);         pawns[x].insert(y);         intercept_ys.insert(intercept_y);       }       if (intercept_ys.size()) {         cout << max(0, getMax(*intercept_ys.begin(), n) - *intercept_ys.begin())              << endl;       } else {         cout << 0 << endl;       }     }   }


#include <bits/stdc++.h>   using namespace std;   using ll = int64_t;   using ull = uint64_t;   using ld = long double;   using uint = uint32_t;   const double EPS = 1e-8;   const int INF = 0x3F3F3F3F;   const ll LINF = 4611686018427387903;   const int MOD = 1e9 + 7;   const int mod = 1e6 + 3;   vector<vector<ll>> mat(11, vector<ll>(12));   ll mpow(ll a, int b) {     ll res = 1;     for (; b; b >>= 1, a = a * a % mod)       if (b & 1) res = res * a % mod;     return res;   }   signed main() {     ios::sync_with_stdio(0);     cin.tie(NULL);     for (int i = 0; i < 11; i++) {       cout <<  ?   << i + 1 << endl;       int w;       cin >> w;       for (int j = 0; j <= 10; j++) mat[i][j] = mpow(i + 1, j);       mat[i][11] = w;     }     for (int i = 0; i < 11; i++) {       for (int j = i; j < 11; j++) {         if (mat[j][i]) {           swap(mat[j], mat[i]);           break;         }       }       for (int j = 0; j < 11; j++) {         if (j == i || !mat[j][i]) continue;         ll c = mat[i][i] * mpow(mat[j][i], mod - 2) % mod;         for (int k = 0; k < 12; k++) mat[j][k] = mat[j][k] * c % mod;         for (int k = 0; k < 12; k++)           mat[j][k] = (mat[j][k] - mat[i][k] + mod) % mod;       }     }     vector<ll> xx(11);     for (int i = 0; i < 11; i++)       xx[i] = mat[i][11] * mpow(mat[i][i], mod - 2) % mod;     for (int i = 0; i < mod; i++) {       ll res = 0;       for (int j = 0; j < 11; j++) {         res = (res + xx[j] * mpow(i, j)) % mod;       }       if (res == 0) {         cout <<  !   << i <<   n ;         return 0;       }     }     cout <<  !   << -1 <<   n ;   }


#include <bits/stdc++.h>   char chars[200005];   int main() {     int n;     scanf( %d , &n);     scanf( %s , chars);     int sum = 0;     for (int i = 0; i < n; i++) {       if (chars[i] ==  < ) {         sum++;       } else {         break;       }     }     for (int i = n - 1; i >= 0 && sum < n; i--) {       if (chars[i] ==  > ) {         sum++;       } else {         break;       }     }     printf( %d n , sum);   }


#include <bits/stdc++.h>   using namespace std;   int q;   char dirs[100000];   int l, r, u, d;   int main() {     cin >> q;     for (int i = 0; i < q + 1; ++i) {       char c;       int ind = 0;       l = r = u = d = 0;       while ((c = getchar()) !=   n ) {         dirs[ind] = c;         ++ind;       }       for (int j = 0; j < ind; ++j) {         if (dirs[j] ==  L )           ++l;         else if (dirs[j] ==  R )           ++r;         else if (dirs[j] ==  U )           ++u;         else           ++d;       }       int min_lr = (l < r) ? l : r;       int min_ud = (u < d) ? u : d;       if (i != 0) {         if (min_lr == 0 && min_ud != 0) {           min_ud = 1;         }         if (min_ud == 0 && min_lr != 0) {           min_lr = 1;         }         cout << (2 * min_lr + 2 * min_ud) << endl;         for (int k = 0; k < min_lr; ++k) {           cout <<  L ;         }         for (int k = 0; k < min_ud; ++k) {           cout <<  U ;         }         for (int k = 0; k < min_lr; ++k) {           cout <<  R ;         }         for (int k = 0; k < min_ud; ++k) {           cout <<  D ;         }         cout << endl;       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n, m;     while (cin >> n >> m) {       int str[550][550], cnt[550], ans[550];       memset(str, 0, sizeof(str));       memset(cnt, 0, sizeof(cnt));       memset(ans, 0, sizeof(ans));       for (int i = 0; i < m; i++) {         int u, v;         cin >> u >> v;         str[u][v] = str[v][u] = 1;         cnt[u]++;         cnt[v]++;       }       for (int i = 1; i <= n; i++)         if (cnt[i] == n - 1) ans[i] = 1;       for (int i = 1; i <= n; i++)         if (ans[i] == 0)           for (int j = i + 1; j <= n; j++)             if (str[i][j] == 0) ans[j] = 2;       bool flag = 1;       for (int i = 1; i <= n; i++) {         for (int j = i + 1; j <= n; j++) {           if ((abs(ans[i] - ans[j]) == 2 && str[i][j]) ||               (abs(ans[i] - ans[j]) < 2 && !str[i][j])) {             cout <<  No  << endl;             flag = false;             break;           }         }         if (!flag) break;       }       if (!flag) continue;       cout <<  Yes  << endl;       for (int i = 1; i <= n; i++) printf( %c , ans[i] +  a );       cout << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   vector<long long> f(21);   long long fact(long long x) { return f[x]; }   long long solve(long long k) {     vector<long long> cnt(21), less(21);     long long d = 1, den = 1, num = 1, den1 = 1, num1 = 0;     while (k > 0) {       d += 1;       cnt[k % d] += 1;       k /= d;     }     less[d + 1] = d - 1;     den = fact(cnt[0]) * fact(cnt[1]);     for (long long i = d; i > 1LL; i--) {       den *= fact(cnt[i]);       less[i] = less[i + 1] - cnt[i];       num *= (less[i] - (i - 2LL));       if (num % den == 0) {         num /= den;         den = 1;       }     }     if (cnt[0] > 0) {       num1 = 1;       cnt[0]--;       less[d] -= 1;       den1 = fact(cnt[0]) * fact(cnt[1]);       for (long long i = d - 1; i > 1; i--) {         den1 *= fact(cnt[i]);         less[i] = less[i + 1] - cnt[i];         num1 *= (less[i] - (i - 2LL));         if (num1 % den1 == 0) {           num1 /= den1;           den1 = 1;         }       }     }     long long res = (num / den) - (num1 / den1) - 1LL;     return res;   }   int main() {     ios::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     f[0] = f[1] = 1;     for (long long i = 2; i < 21; i++) f[i] = i * f[i - 1];     long long t;     cin >> t;     while (t--) {       long long k;       cin >> k;       cout << solve(k) <<   n ;     }   }


// #pragma GCC optimize( Ofast )   #pragma GCC target ( sse4 )   #include <iostream>   #include <cstring>   #include <algorithm>   #include <cmath>   #include <map>   #include <queue>   using namespace std;      const int N = 2e5 + 10, M = N * 2, INT = 0x3f3f3f3f, mod = 1e9 + 7;      //#define int long long   #define pi 3.1415926   #define x first   #define y second   typedef long long ll;   typedef pair<int,int> pii;    //ios::sync_with_stdio(false);   ll gcd(ll a,ll b) {return b ? gcd(b, a % b) : a;}   // int gcd(int a,int b) {return b ? gcd(b, a % b) : a;}   ll qmi(ll a,ll b) {ll res = 1 % mod;while (b){if (b & 1) res = (res * a) % mod;a = a * a % mod;b >>= 1;}return res;}      int t, n, m, k;   int x;   deque<int> q1, q2, s1, s2;   int s[M];      int get(deque<int> &a,deque<int> &b)   {    if (a.size() == 0 || b.size() == 0) return 0;    s[b.size()] = 0;    // for (auto x : a) printf( %d  , x);    //  puts(  );    // for (auto x : b) printf( %d  , x);    //  puts(  );    for (int i = b.size() - 1, j = a.size() - 1; i >= 0; i -- )    {     while (a[j] > b[i] && j > 0) j --;     if (a[j] == b[i]) s[i] = 1;     else s[i] = 0;     s[i] += s[i + 1];    }    // for (int i = 0; i < b.size(); i ++ ) printf( %d  , s[i]);    //  puts(  );    int ans = 0;    for (int i = 0; i < b.size(); i ++ )    {     int p = upper_bound(a.begin(), a.end(), b[i]) - a.begin();     int l = lower_bound(b.begin(), b.end(), b[i] - p + 1) - b.begin();     ans = max(ans, s[i + 1] + i - l + 1);     // printf( b_i=%d p = %d  b_i-p=%d l = %d n , b[i], p, b[i] - p + 1, l);    }    // cout << ans <<  ------------------------- ;    return ans;   }      void solve()   {    q1.clear(), q2.clear(), s1.clear(), s2.clear();    cin >> n >> m;    for (int i = 1; i <= n; i ++ )    {     scanf( %d , &x);     if (x > 0)     {      q1.push_back(x);     }     else q2.push_front(-x);    }    for (int i = 1; i <= m; i ++ )    {     scanf( %d , &x);     if (x > 0)     {      s1.push_back(x);     }     else s2.push_front(-x);    }       int ans = get(q1, s1) + get(q2, s2);    // puts(  );    printf( %d n , ans);   }      signed main()   {       cin >> t;while (t -- )        solve();   }


#include <bits/stdc++.h>   using namespace std;   const int N = 400 + 5;   const int INF = 1e9;   int R, n, m;   pair<int, int> H[N], T[N];   int vis[N][N];   vector<pair<int, int> > adj[N][N];   queue<pair<int, int> > Q;   int dis[N][N];   int bfs(int h, int t) {     memset(dis, 0x3c, sizeof dis);     dis[h][t] = 0;     Q.push(make_pair(h, t));     while (Q.size()) {       int x = Q.front().first, y = Q.front().second;       Q.pop();       if (x + y == 0) return dis[x][y];       for (auto it : adj[x][y]) {         if (dis[x][y] + 1 < dis[it.first][it.second]) {           dis[it.first][it.second] = dis[x][y] + 1;           Q.push(it);         }       }     }     return INF;   }   bool isCycle(int h, int t) {     vis[h][t] = 1;     for (auto it : adj[h][t]) {       int x = it.first, y = it.second;       if (vis[x][y] == 1) return 1;       if (vis[x][y] == 2) continue;       if (isCycle(x, y)) return 1;     }     vis[h][t] = 2;     return 0;   }   int dp[N][N];   int fun(int x, int y) {     int &memo = dp[x][y];     if (memo != -1) return memo;     if (x + y > R) return memo = 0;     int ans = 0;     for (auto it : adj[x][y]) {       ans = max(ans, fun(it.first, it.second));     }     return memo = ans + 1;   }   int main() {     ios::sync_with_stdio(0);     cin.tie(0);     int h, t;     cin >> h >> t >> R;     cin >> n;     for (int i = 1; i <= n; i++) {       cin >> H[i].first >> H[i].second;     }     cin >> m;     for (int i = 1; i <= m; i++) {       cin >> T[i].first >> T[i].second;     }     for (int i = 0; i < N; i++) {       for (int j = 0; j < N; j++) {         if (i + j == 0)           ;         else if (i + j > R)           ;         else {           for (int k = 1; k <= n and k <= i; k++) {             adj[i][j].push_back(make_pair(i - k + H[k].first, j + H[k].second));           }           for (int k = 1; k <= m and k <= j; k++) {             adj[i][j].push_back(make_pair(i + T[k].first, j - k + T[k].second));           }         }       }     }     int ans = bfs(h, t);     if (ans != INF) {       cout <<  Ivan n ;       cout << ans <<   n ;       exit(0);     }     if (isCycle(h, t)) {       cout <<  Draw n ;       exit(0);     }     memset(dp, -1, sizeof dp);     cout <<  Zmey n ;     cout << fun(h, t) <<   n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   template <class T, class V>   ostream &operator<<(ostream &s, pair<T, V> a) {     s << a.first <<     << a.second;     return s;   }   struct NTT {     long long int MD, M = 1, R;     vector<int> r;     vector<long long int> w[2];     long long int bigmod(long long int a, long long int p, long long int m) {       long long int res = 1, x = a;       while (p) {         if (p & 1) res = (res * x) % m;         x = (x * x) % m;         p >>= 1;       }       return res;     }     long long int primitive_root(int n) {       int p = n - 1;       vector<int> d;       for (int i = 2; i * i <= p; i++) {         if (p % i == 0) {           d.push_back(i);           while (p % i == 0) p /= i;         }       }       if (p > 1) d.push_back(p);       for (int i = 2; i <= n; i++) {         bool ok = true;         for (auto x : d) ok &= (bigmod(i, (n - 1) / x, n) != 1);         if (ok) return i;       }       assert(false);     }     NTT(long long int n, long long int _MD) : MD(_MD) {       int u = 0;       while (M < n) M <<= 1, u++;       r.resize(M);       w[0].resize(M, 1);       w[1].resize(M, 1);       R = 3;       w[0][1] = bigmod(R, (MD - 1) / M, MD);       w[1][1] = bigmod(w[0][1], MD - 2, MD);       for (int i = 1; i < M; i++) {         int l = __builtin_ctz(i);         r[i] = r[i ^ (1 << l)] | (1 << (u - l - 1));         w[0][i] = w[0][i - 1] * w[0][1] % MD;         w[1][i] = w[1][i - 1] * w[1][1] % MD;       }     }     void fft(vector<long long int> &a, bool inv = false) {       for (int i = 0; i < M; i++)         if (i < r[i]) swap(a[i], a[r[i]]);       for (int len = 2; len <= M; len <<= 1) {         for (int i = 0, inc = M / len; i < M; i += len) {           for (int j = 0, l = 0; j < (len >> 1); j++, l += inc) {             long long int u = a[i + j],                           v = a[i + j + (len >> 1)] * w[inv][l] % MD;             a[i + j] = (u + v >= MD ? u + v - MD : u + v);             a[i + j + (len >> 1)] = (u < v ? u - v + MD : u - v);           }         }       }       if (inv) {         long long int r = bigmod(M, MD - 2, MD);         for (auto &x : a) x = x * r % MD;       }     }     vector<long long int> multiply(vector<long long int> &a,                                    vector<long long int> &b) {       vector<long long int> v1(M), v2(M);       for (int i = 0; i < a.size(); i++) v1[i] = a[i];       for (int i = 0; i < b.size(); i++) v2[i] = b[i];       fft(v1);       fft(v2);       for (int i = 0; i < M; i++) v1[i] = v1[i] * v2[i] % MD;       fft(v1, 1);       return v1;     }     vector<long long int> POWER(vector<long long int> &a, long long int n) {       vector<long long int> v1(M);       for (int i = 0; i < a.size(); i++) v1[i] = a[i];       fft(v1);       for (int i = 0; i < M; i++) v1[i] = bigmod(v1[i], n, MD);       fft(v1, 1);       return v1;     }   };   int main() {     ios::sync_with_stdio(false);     cin.tie(NULL);     int T = 1;     for (int qq = 1; qq <= T; qq++) {       int n, k;       cin >> n >> k;       vector<long long int> v(10);       while (k--) {         long long int x;         cin >> x;         v[x] = 1;       }       long long int u = n / 2;       NTT nt(1 << 20, (998244353));       v = nt.POWER(v, u);       long long int a = 0;       for (int i = 0; i < v.size(); i++) a = (v[i] * v[i] + a) % (998244353);       cout << a << endl;     }   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int a, b, c;     bool o00, o01, o10, o11;     a = b = c = 0;     string s;     cin >> s;     string::iterator its;     its = s.begin();     for (; its != s.end(); ++its) {       if (*its ==  1 )         ++a;       else if (*its ==  0 )         ++b;       else if (*its ==  ? )         ++c;     }     o00 = (a < b + c);     o11 = (a + c > b + 1);     --its;     o01 = o10 = false;     int x1, x2;     x1 = (b + c - a + (a + b + c) % 2) / 2;     x2 = c - x1;     if (x1 >= 0 && x1 <= c) {       if (*its ==  0  || (*its ==  ?  && x2)) o10 = true;       if (*its ==  1  || (*its ==  ?  && x1)) o01 = true;     }     if (o00) cout <<  00 n ;     if (o01) cout <<  01 n ;     if (o10) cout <<  10 n ;     if (o11) cout <<  11 n ;     return 0;   }


#include <bits/stdc++.h>   const int maxn = 10000050;   using namespace std;   char a[1005][6];   void solve() {     memset(a, 0, sizeof(a));     int n;     cin >> n;     for (int i = 0; i < n; i++) cin >> a[i];     int flag = 0;     for (int i = 0; i < n; i++) {       if (a[i][0] ==  O  && a[i][1] ==  O ) {         a[i][0] =  + , a[i][1] =  + ;         flag = 1;         break;       }       if (a[i][3] ==  O  && a[i][4] ==  O ) {         a[i][3] =  + , a[i][4] =  + ;         flag = 1;         break;       }     }     if (flag) {       cout <<  YES  << endl;       for (int i = 0; i < n; i++) cout << a[i] << endl;     } else       cout <<  NO  << endl;   }   int main() { solve(); }


#include <bits/stdc++.h>   using namespace std;   long long Rev[35], C[35][35];   long long powmod(long long x, long long y = 998244353 - 2,                    long long p = 998244353) {     long long ans = 1;     while (y) {       if (y & 1) ans = ans * x % 998244353;       x = x * x % 998244353;       y >>= 1;     }     return ans;   }   struct Poly {     long long X[35], L, R;     Poly() { memset(X, 0, sizeof X); }     Poly(long long _1) {       memset(X, 0, sizeof X);       X[0] = _1;     }     long long getval(long long x) {       long long ans = 0;       for (long long nw = 1, i = 0; i < 35; i++, nw = nw * x % 998244353)         ans = (ans + nw * X[i]) % 998244353;       return ans;     }     Poly minus(long long x) {       Poly Ans;       for (long long i = 0; i < 35; i++)         for (long long j = 0, s = 1; j <= i;              j++, s = s * (998244353 - x) % 998244353)           Ans.X[i - j] =               (Ans.X[i - j] + X[i] * C[i][j] % 998244353 * s) % 998244353;       return Ans;     }     Poly intg() {       Poly Ans;       for (long long i = 1; i < 35; i++) Ans.X[i] = X[i - 1] * Rev[i] % 998244353;       Ans.X[0] = 0;       return Ans;     }     Poly operator+(const Poly& A) {       Poly Ans;       for (long long i = 0; i < 35; i++) Ans.X[i] = (X[i] + A.X[i]) % 998244353;       return Ans;     }     Poly operator-(const Poly& A) {       Poly Ans;       for (long long i = 0; i < 35; i++) Ans.X[i] = (X[i] - A.X[i]) % 998244353;       return Ans;     }     void operator/=(const long long& x) {       long long tmp = powmod(x);       for (long long i = 0; i < 35; i++) X[i] = X[i] * tmp % 998244353;     }   };   long long n, K[33];   vector<Poly> S[33];   int main() {     for (long long i = 1; i < 35; i++) Rev[i] = powmod(i);     for (long long i = 0; i < 35; i++)       for (long long j = 0; j <= i; j++)         C[i][j] = !j ? 1 : (C[i - 1][j - 1] + C[i - 1][j]) % 998244353;     scanf( %lld , &n);     for (long long i = 1; i <= n; i++) {       double x;       scanf( %lf , &x);       K[i] = (long long)(x * 1e6 + 0.5);     }     S[1].resize(3);     S[1][0].L = -1000000;     S[1][0].R = 0;     S[1][1].L = 0;     S[1][1].R = min(K[1], 1000000ll);     S[1][1].X[0] = powmod(1000000);     S[1][2].L = min(K[1], 1000000ll);     S[1][2].R = 1e18;     for (long long i = 2; i <= n; i++) {       long long l = 0, r = 1, nw = -1000000;       S[i].resize(1);       S[i][0].L = -1000000;       S[i][0].R = 0;       K[i] = min(K[i - 1] + 1000000, K[i]);       while (l < S[i - 1].size()) {         long long mn = min(S[i - 1][l].R - nw, S[i - 1][r].R - nw - 1000000);         if (nw + mn + 1000000 >= K[i]) mn = K[i] - nw - 1000000;         long long x = nw + 1000000;         Poly X;         X = X + Poly(S[i - 1][l].intg().getval(S[i - 1][l].R)) -             S[i - 1][l].intg().minus(1000000);         X = X + S[i - 1][r].intg().minus(0) -             Poly(S[i - 1][r].intg().getval(S[i - 1][r].L));         for (long long j = l + 1; j < r; j++)           X = X + Poly(S[i - 1][j].intg().getval(S[i - 1][j].R) -                        S[i - 1][j].intg().getval(S[i - 1][j].L));         X.L = x;         X.R = x + mn;         if (S[i - 1][l].R == mn + x - 1000000) l++;         if (S[i - 1][r].R == mn + x) r++;         X /= 1000000;         S[i].push_back(X);         if (mn + x == K[i]) break;         nw = mn + nw;       }       Poly X;       X.L = K[i];       X.R = 1e18;       S[i].push_back(X);     }     long long ans = 0;     for (long long i = 1; i + 1 < S[n].size(); i++)       ans = (ans + S[n][i].intg().getval(S[n][i].R) -              S[n][i].intg().getval(S[n][i].L)) %             998244353;     printf( %lld n , (ans % 998244353 + 998244353) % 998244353);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   bool isIntersect(int x1, int y1, int x2, int y2, int x3, int y3, int x4,                    int y4) {     double t1 = (y3 - y4) * (x1 - x3) + (x4 - x3) * (y1 - y3);     double t2 = (x4 - x3) * (y1 - y2) - (x1 - x2) * (y4 - y3);     double r1 = t1 / t2;     double f1 = (y1 - y2) * (x1 - x3) + (x2 - x1) * (y1 - y3);     double f2 = (x4 - x3) * (y1 - y2) - (x1 - x2) * (y4 - y3);     double r2 = f1 / f2;     if (r1 >= 0 && r1 <= 1 && r2 >= 0 && r2 <= 1) {       return true;     }     return false;   }   double isInside(int x0, int y0, int x1, int y1, int x2, int y2) {     double num = (y2 - y1) * x0 - (x2 - x1) * y0 + x2 * y1 - y2 * x1;     num = abs(num);     double den = sqrt(pow(y2 - y1, 2) + pow(x2 - x1, 2));     return num / den;   }   int main() {     int first[4][2];     int second[4][2];     int minX = 200;     int maxY = -200;     int minY = 200;     int maxX = -200;     int min1X = 200;     int max1X = -200;     int min2Y = 200;     int max2Y = -200;     for (int i = 0; i < 4; i++) {       for (int j = 0; j < 2; j++) {         cin >> first[i][j];         if (j == 0) {           minX = min(minX, first[i][j]);           maxX = max(maxX, first[i][j]);         } else {           minY = min(minY, first[i][j]);           maxY = max(maxY, first[i][j]);         }       }     }     for (int i = 0; i < 4; i++) {       for (int j = 0; j < 2; j++) {         cin >> second[i][j];         if (j == 0) {           min1X = min(min1X, second[i][j]);           max1X = max(max1X, second[i][j]);         } else {           min2Y = min(min2Y, second[i][j]);           max2Y = max(max2Y, second[i][j]);         }       }     }     for (int i = 0; i < 4; i++) {       if (second[i][0] >= minX && second[i][0] <= maxX && second[i][1] >= minY &&           second[i][1] <= maxY) {         cout <<  YES ;         return 0;       }       if (isInside(first[i][0], first[i][1], second[0][0], second[0][1],                    second[2][0], second[2][1]) +               isInside(first[i][0], first[i][1], second[1][0], second[1][1],                        second[3][0], second[3][1]) <=           (max2Y - min2Y) / 2) {         cout <<  YES ;         return 0;       }     }     for (int i = 0; i < 4; i++) {       int j = (i + 1) % 4;       for (int ii = 0; ii < 4; ii++) {         int jj = (ii + 1) % 4;         if (isIntersect(first[i][0], first[i][1], first[j][0], first[j][1],                         second[ii][0], second[ii][1], second[jj][0],                         second[jj][1])) {           cout <<  YES ;           return 0;         }       }     }     cout <<  NO ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int NMAX = 1e5;   int n, x, k;   int a[NMAX + 2];   long long ans;   int ndiv(int f, int l) { return l / x - (f - 1) / x; }   pair<int, int> bs(int arg) {     pair<int, int> last;     int st = arg, dr = n;     last.first = last.second = -1;     while (st <= dr) {       int med = (st + dr) / 2;       if (ndiv(a[arg], a[med]) >= k) {         dr = med - 1;         last.first = med;       } else         st = med + 1;     }     st = arg, dr = n;     while (st <= dr) {       int med = (st + dr) / 2;       if (ndiv(a[arg], a[med]) > k)         dr = med - 1;       else {         st = med + 1;         last.second = med;       }     }     return last;   }   int main() {     cin >> n >> x >> k;     for (int i = 1; i <= n; i++) cin >> a[i];     sort(a + 1, a + n + 1);     int nr;     for (int i = 1; i <= n; i++) {       if (a[i] != a[i - 1]) {         pair<int, int> pos = bs(i);         if (pos.first != -1 && pos.second != -1)           nr = pos.second - pos.first + 1;         else           nr = 0;       }       ans += 1LL * nr;     }     cout << ans <<   n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n, a[500010], x, sz;   long long ans;   int main() {     scanf( %d , &n);     for (int step = 1; step <= n; step++) {       scanf( %d , &x);       while (a[sz - 1] >= a[sz] && a[sz] <= x && sz >= 2) {         ans += min(a[sz - 1], x);         sz--;       }       a[++sz] = x;     }     sort(a + 1, a + sz + 1);     for (int i = 1; i <= sz - 2; i++) ans += a[i];     printf( %I64d , ans);   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n, x, a, b, in[100], on[100], i = 0, r, y;     cin >> n >> x;     int t = n;     while (t--) {       cin >> a >> b;       r = min(a, b);       y = max(a, b);       in[i] = r;       on[i] = y;       i++;     }     sort(in, in + n);     sort(on, on + n);     if (in[n - 1] < on[0]) {       if (x <= in[n - 1])         cout << in[n - 1] - x;       else if (x >= on[0])         cout << x - on[0];       else if (x > in[n - 1] && x < on[0])         cout << 0;     } else if (in[n - 1] == on[0])       cout << abs(x - on[0]);     else       cout << -1;   }


#include<bits/stdc++.h>   using namespace std;   void __print(int x) {cerr << x;}   void __print(long x) {cerr << x;}   void __print(long long x) {cerr << x;}   void __print(unsigned x) {cerr << x;}   void __print(unsigned long x) {cerr << x;}   void __print(unsigned long long x) {cerr << x;}   void __print(float x) {cerr << x;}   void __print(double x) {cerr << x;}   void __print(long double x) {cerr << x;}   void __print(char x) {cerr <<      << x <<     ;}   void __print(const char *x) {cerr <<     << x <<    ;}   void __print(const string &x) {cerr <<     << x <<    ;}   void __print(bool x) {cerr << (x ?  true  :  false );}       template<typename T, typename V>   void __print(const pair<T, V> &x) {cerr <<  { ; __print(x.first); cerr <<  , ; __print(x.second); cerr <<  } ;}   template<typename T>   void __print(const T &x) {int f = 0; cerr <<  { ; for (auto &i: x) cerr << (f++ ?  ,  :   ), __print(i); cerr <<  } ;}   void _print() {cerr <<  ] n ;}   template <typename T, typename... V>   void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr <<  ,  ; _print(v...);}   #ifndef ONLINE_JUDGE   #define debug(x...) cerr <<  [  << #x <<  ] = [ ; _print(x)   #else   #define debug(x...)   #endif       #define ll long long   #define f first   #define s second   #define Fast ios_base::sync_with_stdio(false);cin.tie(NULL);   typedef pair<ll , pair<ll, ll> > pi;   int pow(int x,int y){       int res=1;       while(y){           if(y&1) res*=x;           y>>=1;           x*=x;       }       return res;   }      struct Compare {       constexpr bool operator()(pi const & a,                                 pi const & b) const noexcept       { return a.first < b.first || (a.first == b.first && a.second.first > b.second.first); }       };   void prefix_function( string s,ll arr[] )   {        long long border=0; arr[0]=0;       for(long long i=1;i<s.size();i++)       {         while(border>0 && s[i]!=s[border])           border=arr[border-1];                if(s[i]==s[border])            border++;         else          border=0;                  arr[i]=border;       }   }//send mod-2 for a^-1 if mod is a prime number       ll mod=998244353;   ll add( ll a , ll b)   {       return (((a%mod)+(b%mod))%mod);   }   ll mul(ll a,ll b)   {     return (((a%mod)*(b%mod))%mod);   }   ll binpow(ll a, ll b) {       ll res = 1;       while (b) {           if (b & 1) res = mul(res, a);           a = mul(a, a);           b >>= 1;       }       return res;   }   ll subs(ll a,ll b)   {       return (((a%mod)-(b%mod)+mod)%mod);   }   ll dv(ll a,ll b)   {       ll inv=binpow(b,mod-2);       return mul(a,inv);   }       ll dsu_arr[100000];   ll dsu_sz[100000];   void dsu(ll n)   {       for(ll i=0;i<=n;i++)       {           dsu_arr[i]=i;           dsu_sz[i]=1;       }   }   ll find(ll x)   {        ll root=x;       while (root!=dsu_arr[root])       {           root=dsu_arr[root];       }       while(x!=dsu_arr[x])       {           dsu_arr[x]=root;           x=dsu_arr[x];       }       return root;   }   ll merge(ll x,ll y)   {       ll root1=find(x);       ll root2=find(y);              if(root1==root2)       return 0ll;           if(dsu_sz[x]>dsu_sz[y]){       dsu_arr[root2]=root1;       dsu_sz[root1]+=dsu_sz[root2];       }       else       {            dsu_sz[root2]+=dsu_sz[root1];           dsu_arr[root1]=root2;       }       return 1ll;   }   /*   vector<ll>adj[100005];   bool vis[100005];   ll dist[100005];   void bfs(ll c)   {     vis[c]=true;     dist[c]=0;     queue<ll>q;     q.push(c);   while(!q.empty())   {       ll x=q.front();       q.pop();             for(ll i=0;i<adj[x].size();i++)       {           ll y=adj[x][i];           if(!vis[y])           {               vis[y]=true;               dist[y]=dist[x]+1;                              q.push(y);           }       }       }   }   */       int main()   {       Fast    ll test;    cin>>test;    while(test--)    {        string s;        cin>>s;        map<char,ll>cnt;        for(char a= a ;a<= z ;a++)        {            cnt[a]=-1;        }        for(ll i=0;i<s.size();i++)        {            cnt[s[i]]=max(cnt[s[i]],i+1);        }        for(char a= a ;a<= z ;a++)        {           if(cnt[a]==-1)           {               cnt[a]=1000000000;           }        }        stack<char>ans;        map<char,bool>taken;        for(ll i=0;i<s.size();i++)        {            char x=s[i];            ll r=i+1;            if(taken[s[i]])            continue;            bool hbe=true;                         while(!ans.empty())              {                  ll u=ans.top();                                    if(u<x && cnt[u]>r){                      taken[u]=false;                  ans.pop();                  }                  else                  break;              }              ans.push(x);              taken[x]=true;                                            // cout<<ans.size()<<   <<ans.top()<<endl;        }        string sura;        while (!ans.empty())        {            sura+=ans.top();             ans.pop();        }        for(ll i=sura.size()-1;i>=0;i--)        {            cout<<sura[i];        }        cout<<  n ;        }           }


#include <bits/stdc++.h>   using namespace std;   int main() {     int m, n, k, i, j, t, x;     int c[200];     cin >> n;     int a[n];     for (i = 0; i < n; i++) {       cin >> a[i];       c[i] = a[i];     }     cin >> m;     int b[m];     for (j = 0; j < m; j++) {       cin >> b[j];       c[i + j] = b[j];     }     x = m + n;     for (i = 0; i < n; i++) {       for (j = 0; j < m; j++) {         t = 0;         for (k = 0; k < x; k++) {           if (a[i] + b[j] == c[k]) {             t = 1;             break;           }         }         if (t == 0) {           cout << a[i] <<     << b[j] << endl;           goto here;         }       }     }   here:     return 0;   }


#include <bits/stdc++.h>   using namespace std;   struct point {     double x, y;     point(double x, double y) : x(x), y(y) {}     point() {}     inline void read() { scanf( %lf%lf , &x, &y); }     inline double len() const { return sqrt(x * x + y * y); }     inline double len2() const { return x * x + y * y; }     inline point rot() { return point(-y, x); }   };   inline int sgn(double a) {     if (a > 1e-7)       return 1;     else if (a < -1e-7)       return -1;     return 0;   }   inline point operator-(const point &a, const point &b) {     return point(a.x - b.x, a.y - b.y);   }   inline point operator+(const point &a, const point &b) {     return point(a.x + b.x, a.y + b.y);   }   inline point operator*(const point &a, double b) {     return point(b * a.x, b * a.y);   }   inline point operator*(double b, const point &a) {     return point(b * a.x, b * a.y);   }   inline double operator*(const point &a, const point &b) {     return a.x * b.x + a.y * b.y;   }   inline double operator%(const point &a, const point &b) {     return a.x * b.y - a.y * b.x;   }   inline bool operator==(const point &a, const point &b) {     return sgn(a.x - b.x) == 0 && sgn(a.y - b.y) == 0;   }   inline bool operator!=(const point &a, const point &b) { return !(a == b); }   inline double dist(const point &a, const point &b) { return (a - b).len(); }   struct line {     point a, b, v;     line() {}     line(const point &a, const point &b) : a(a), b(b), v(b - a) {}   };   double sqr(double a) { return a * a; }   inline double dist(const point &a, const line &b) {     return abs((a - b.a) % b.v) / b.v.len();   }   point operator*(const line &a, const line &b) {     return b.a + (((a.a - b.a) % a.v) / (b.v % a.v)) * b.v;   }   bool operator*(const point &a, const line &b) {     return ((a - b.a) * b.v) * ((a - b.b) * b.v) < -1e-7;   }   bool Solve(point a, point b, point c) {     if (a == b || b == c || c == a) return false;     if (sgn((a - b) % (c - b)) == 0) return false;     line l0((a + b) * 0.5, (a + b) * 0.5 + (b - a).rot()),         l1(1.5 * b - 0.5 * c, 1.5 * b - 0.5 * c + (c - b).rot());     point p[4];     p[0] = l0 * l1;     p[1] = 2 * b - p[0];     p[3] = 2 * a - p[0];     p[2] = 2 * c - p[1];     if (sgn((p[1] - p[0]) % (p[2] - p[1])) == 1) reverse(p, p + 4);     for (int i = 0; i < 4; i++) {       if (sgn((p[(i + 1) % 4] - p[i]) % (p[(i + 2) % 4] - p[(i + 1) % 4])) >= 0)         return false;     }     printf( YES n );     for (int i = 0; i < 4; i++) printf( %.9lf %.9lf  , p[i].x, p[i].y);     printf(  n );     return true;   }   int main() {     int T;     scanf( %d , &T);     while (T--) {       point a, b, c;       a.read(), b.read(), c.read();       if (Solve(a, b, c)) continue;       if (Solve(a, c, b)) continue;       if (Solve(b, a, c)) continue;       if (Solve(b, c, a)) continue;       if (Solve(c, a, b)) continue;       if (Solve(c, b, a)) continue;       printf( NO n n );     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int MAXN = 1e5 + 5;   int n, m, h, t, u, v, src[MAXN], dst[MAXN], num[MAXN], used[MAXN];   vector<int> g[MAXN];   void out(vector<int> &v) {     for (int i = 0; i < v.size(); ++i) {       cout << v[i] <<    n [i == v.size() - 1];     }   }   bool check(int u, int v, int ver) {     int cnt = 0;     num[u] = num[v] = -1;     for (int i = 0; i < g[u].size() && i < h + t; ++i) {       int w = g[u][i];       if (w == v) continue;       used[w] = ver;       num[w] = 1;       ++cnt;     }     for (int i = 0; i < g[v].size() && i < h + t; ++i) {       int w = g[v][i];       if (w == u) continue;       if (used[w] == ver) {         num[w] = 2;         continue;       }       used[w] = ver;       num[w] = 1;       ++cnt;     }     int degu = g[u].size() - 1, degv = g[v].size() - 1;     if (h >= t != degu >= degv) swap(degu, degv);     return degu >= h && degv >= t && cnt >= h + t;   }   void solve() {     for (int mm = 0; mm < m; ++mm) {       if (check(src[mm], dst[mm], mm + 1)) {         cout <<  YES n ;         u = src[mm], v = dst[mm];         if (h >= t != g[u].size() >= g[v].size()) swap(u, v);         cout << u <<     << v << endl;         vector<int> ans;         for (int i = 0; i < g[u].size() && ans.size() < h; ++i) {           int v = g[u][i];           if (num[v] == 1) {             num[v] == -1;             ans.push_back(v);           }         }         for (int i = 0; i < g[u].size() && ans.size() < h; ++i) {           int v = g[u][i];           if (num[v] > 1) {             ans.push_back(v);             num[v] = -1;           }         }         out(ans);         ans.clear();         swap(u, v);         for (int i = 0; i < g[u].size() && ans.size() < t; ++i) {           int v = g[u][i];           if (num[v] != -1) ans.push_back(v);         }         out(ans);         return;       }     }     cout <<  NO n ;   }   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout << setprecision(6) << fixed;     clog.copyfmt(cout);     cin >> n >> m >> h >> t;     for (int i = 0; i < m; ++i) {       cin >> u >> v;       src[i] = u, dst[i] = v;       g[u].push_back(v);       g[v].push_back(u);     }     solve();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long double eps = 1e-13;   const long double PI = acos(-1);   const int INF = (int)1e9;   const long long INFF = (long long)1e18;   const int mod = (int)1e9 + 7;   const int MXN = (int)2e5 + 7;   char s[MXN];   int cnt[MXN];   int num[MXN], num2[MXN], a[MXN];   bool check() {     for (int i = 0; i < 1 << 6; i++) num2[i] = num[i];     for (int i = 0; i < 6; i++) {       for (int j = 0; j < 1 << 6; j++) {         if (j & (1 << i)) num2[j] += num2[j ^ (1 << i)];       }     }     for (int i = 0; i < 1 << 6; i++) {       int sum = 0;       for (int j = 0; j < 6; j++)         if (i & (1 << j)) sum += cnt[j];       if (sum < num2[i]) return false;     }     return true;   }   char ans[MXN];   int main() {     scanf( %s , s + 1);     int l = strlen(s + 1);     for (int i = 1; s[i]; i++) {       cnt[s[i] -  a ]++;       a[i] = (1 << 6) - 1;     }     int m;     scanf( %d , &m);     while (m--) {       int p;       scanf( %d %s , &p, s + 1);       a[p] = 0;       for (int i = 1; s[i]; i++) a[p] ^= (1 << (s[i] -  a ));     }     for (int i = 1; i <= l; i++) {       num[a[i]]++;     }     for (int i = 1; i <= l; i++) {       num[a[i]]--;       for (int j = 0; j < 6; j++) {         if (!cnt[j] || !(a[i] & (1 << j))) continue;         cnt[j]--;         if (check()) {           ans[i] = j +  a ;           break;         } else {           cnt[j]++;         }       }       if (!ans[i]) {         puts( Impossible );         return 0;       }     }     printf( %s n , ans + 1);     return 0;   }


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


#include <bits/stdc++.h>   using namespace std;   int main() {     int t;     cin >> t;     while (t--) {       int n;       cin >> n;       int arr[n];       for (int i = 0; i < n; i++) {         cin >> arr[i];       }       int res[101] = {0};       for (int i = 0; i < n; i++) {         res[arr[i]]++;       }       int cnt[101] = {0};       for (int i = 0; i < 101; i++) {         if (res[i] >= 1) {           res[i]--;           cnt[i]++;         }       }       int f, s;       for (int i = 0; i < 101; i++) {         if (res[i] == 0) {           f = i;           break;         }       }       for (int i = 0; i < 101; i++) {         if (cnt[i] == 0) {           s = i;           break;         }       }       cout << f + s <<   n ;     }   }


#include <bits/stdc++.h>   using namespace std;   int K, P, cd;   int cal(int ct, vector<pair<int, int> > vt) {     int t = vt.size(), res = 0;     for (int st = 1; st < (1 << t); st++) {       int val = 1, sl = 0;       for (int j = 0; j < t; j++) {         if ((st >> j) % 2 == 1) val *= vt[j].first, sl++;       }       if (sl % 2 == 0)         res -= ct / val;       else         res += ct / val;     }     return ct - res;   }   void solve() {     vector<pair<int, int> > vt;     int x = P;     for (int i = 2; i * i <= P; i++) {       if (x % i == 0) {         int t = 0;         while (x % i == 0) t++, x /= i;         vt.push_back(pair<int, int>(i, t));       }     }     if (x > 1) vt.push_back(pair<int, int>(x, 1));     int t = vt.size();     int res0 = cal(cd, vt), r = 10000000, l = cd;     int ans;     while (1) {       int mid = (l + r) / 2;       int v = cal(mid, vt) - res0;       if (v == K) {         ans = mid;         break;       }       if (v > K) {         r = mid - 1;       } else         l = mid + 1;     }     while (1) {       int ok = 1;       for (int j = 0; j < vt.size(); j++) {         if (ans % vt[j].first == 0) {           ok = 0;           break;         }       }       if (ok)         break;       else         ans--;     }     cout << ans << endl;   }   int main() {     int T;     cin >> T;     for (int i = 0; i < T; i++) {       cin >> cd >> P >> K;       solve();     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int maxn = (int)1e5 * 2 + 7;   int a[maxn], b[maxn], t[4 * maxn], n, W, H;   void build(int v, int l, int r) {     if (r - l == 1) {       t[v] = a[l];       return;     }     build(2 * v + 1, l, (l + r) / 2);     build(2 * v + 2, (l + r) / 2, r);     t[v] = max(t[2 * v + 1], t[2 * v + 2]);   }   int get(int v, int l, int r, int ql, int qr) {     if (ql <= l && qr >= r) return t[v];     if (ql >= r || qr <= l) return 0;     return max(get(2 * v + 1, l, (l + r) / 2, ql, qr),                get(2 * v + 2, (l + r) / 2, r, ql, qr));   }   void upd(int v, int l, int r, int pos, int x) {     if (r - l == 1) {       t[v] = x;       return;     }     if (pos < (l + r) / 2) {       upd(2 * v + 1, l, (l + r) / 2, pos, x);     } else {       upd(2 * v + 2, (l + r) / 2, r, pos, x);     }     t[v] = max(t[2 * v + 1], t[2 * v + 2]);   }   int main() {     scanf( %d , &n);     for (int i = 0; i < n; i++) {       int ww, hh;       scanf( %d %d , &ww, &hh);       a[i] = hh;       b[i] = ww;       W += ww;     }     build(0, 0, n);     for (int i = 0; i < n; i++) {       W -= b[i];       upd(0, 0, n, i, -10000);       H = get(0, 0, n, 0, n);       printf( %d  , H * W);       W += b[i];       upd(0, 0, n, i, a[i]);     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     long long left;     long long right;     scanf( %lld%lld , &left, &right);     long long ans = 0LL;     for (int i = (62); i >= (0); --i) {       long long bitLeft = left & (1LL << i);       long long bitRight = right & (1LL << i);       if (bitRight && !bitLeft) {         ans = (1LL << (i + 1)) - 1LL;         printf( %lld n , ans);         return 0;       }     }     printf( %lld n , ans);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   template <class Ta, class Tb>   inline Tb cast(Ta a) {     stringstream ss;     ss << a;     Tb b;     ss >> b;     return b;   };   const double EPS = 1e-9;   const int INF = 1000000000;   const int MOD = 1000000007;   const int diri[8] = {-1, 0, 1, 0, -1, 1, 1, -1};   const int dirj[8] = {0, 1, 0, -1, 1, 1, -1, -1};   vector<int> tree;   int root(int n) {     if (tree[n] == -1) return n;     return tree[n] = root(tree[n]);   }   int main() {     int n, m;     cin >> n >> m;     vector<int> deg(n, 0);     tree = vector<int>(n, -1);     for (int i = 0; i < m; ++i) {       int x, y;       cin >> x >> y;       --x;       --y;       ++deg[x];       ++deg[y];       int a = root(x), b = root(y);       if (a != b) {         tree[b] = a;       }     }     int c = 0;     for (int i = 0; i < n; ++i)       if (deg[i] != 0 and tree[i] == -1) ++c;     if (deg[0] == 0 and c > 0) ++c;     int s = 0;     for (int i = 0; i < n; ++i)       if (deg[i] % 2 == 1) ++s;     if (c == 1)       cout << s / 2 << endl;     else {       vector<int> v(n, 0);       for (int i = 0; i < n; ++i)         if (deg[i] % 2 == 1) v[root(i)] = 1;       int t = 0;       for (int i = 0; i < n; ++i) t += v[i];       cout << c + s / 2 - t << endl;     }   }


#include <bits/stdc++.h>   using namespace std;   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     priority_queue<int, vector<int>, greater<int>> pq;     int n, k;     cin >> n >> k;     for (int i = 0; i < n; i++) {       int x;       cin >> x;       pq.push(x);     }     int ans = 0;     for (int i = 0; i < k; i++) {       ans += pq.top();       pq.pop();     }     cout << ans <<   n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 1e5 + 5;   map<int, int> v[N];   int rec(int node, int w) {     auto it = v[node].lower_bound(w);     if (it == v[node].begin()) {       return 1;     }     it--;     return (*it).second + 1;   }   int32_t main() {     ios::sync_with_stdio(0);     cin.tie(0);     int n, m;     cin >> n >> m;     int ans = 0;     while (m--) {       int a, b, w;       cin >> a >> b >> w;       int val = rec(a, w);       if (rec(b, w + 1) > val) {         continue;       }       v[b][w] = val;       auto it = v[b].upper_bound(w);       while (it != v[b].end() && (*it).second <= val) {         it = v[b].erase(it);       }       ans = max(ans, val);     }     cout << ans;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     long long int a, b;     scanf( %lld%lld , &a, &b);     long long int c = a, d = b;     long long int cnt = 0, rem;     while (c >= 1 && d >= 1) {       if (d == 1) {         printf( %lld , cnt + c);         return 0;       }       if (c >= d) {         cnt += c / d;         c = c % d;       } else {         cnt += d / c;         d = d % c;       }     }     printf( %lld n , cnt);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = (int)5e5 + 9;   vector<int> T[N];   bool use[2 * N];   int p;   int l[N], r[N];   void dfs(int u, int par) {     if (par == -1) {       l[u] = 1;       r[u] = T[u].size() + 2;       use[1] = true;       use[T[u].size() + 2] = true;       p = T[u].size() + 3;       vector<pair<int, int> > srt;       int fl = 2;       for (auto x : T[u]) {         if (x == par) continue;         dfs(x, u);         srt.push_back(make_pair(r[x], x));       }       sort(srt.begin(), srt.end());       reverse(srt.begin(), srt.end());       for (int i = 0; i < srt.size(); i++) {         l[srt[i].second] = fl++;       }       return;     }     vector<int> rr;     for (auto x : T[u]) {       if (x == par) {         continue;       }       while (use[p]) p++;       use[p] = true;       rr.push_back(p);     }     while (use[p]) p++;     r[u] = p;     use[p] = true;     vector<pair<int, int> > srt;     for (int i = 0; i < T[u].size(); i++) {       if (T[u][i] == par) continue;       dfs(T[u][i], u);       srt.push_back(make_pair(r[T[u][i]], T[u][i]));     }     sort(srt.begin(), srt.end());     reverse(srt.begin(), srt.end());     for (int i = 0; i < srt.size(); i++) {       l[srt[i].second] = rr[i];     }   }   int main() {     ios::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     ;     int n;     cin >> n;     int x, y;     for (int i = 1; i < n; i++) {       cin >> x >> y;       T[x].push_back(y);       T[y].push_back(x);     }     dfs(1, -1);     for (int i = 1; i <= n; i++) cout << l[i] <<     << r[i] <<   n ;     return 0;   }


#include <bits/stdc++.h>   #pragma GCC optimize( O3 )   #pragma GCC target( avx2 )   using namespace std;   const long double two = 2;   const long double INF = 2e18 + 100;   const long long MOD = 998244353;   const long long N = 1e5 + 10;   const long long nul = 0;   const long long SIZE = 1e6 * 3;   struct exam {     long long s, d, c, ind;     exam(long long s1, long long d1, long long c1, long long ind1) {       s = s1;       d = d1;       c = c1;       ind = ind1;     }   };   vector<long long> ans;   vector<exam> smth;   bool lol(long long st, long long len, long long fin, long long ind) {     long long i = st;     while (len--) {       while (ans[i] != 0 && i < fin) i++;       if (i == fin) return 0;       ans[i] = ind + 1;     }     return 1;   }   bool comp(exam a, exam b) {     if (a.d < b.d)       return 1;     else       return 0;   }   signed main() {     long long n, m;     cin >> n >> m;     ans.resize(n, 0);     smth;     for (long long i = 0; i < m; i++) {       long long s, d, c;       cin >> s >> d >> c;       s--;       d--;       ans[d] = m + 1;       smth.push_back(exam(s, d, c, i));     }     sort((smth).begin(), (smth).end(), comp);     bool flag = 0;     for (long long i = 0; i < m; i++) {       if (lol(smth[i].s, smth[i].c, smth[i].d, smth[i].ind) == 0) flag = 1;     }     if (flag)       cout << -1 << endl;     else {       for (long long i : ans) cout << i <<    ;       cout << endl;     }   }


#include <bits/stdc++.h>   using namespace std;   bool sol(long long n, long long m, long long a, long long l, long long r,            long long& b, long long& c) {     if (abs(n * a - m) <= r - l) {       long long diff = m - n * a;       b = diff > 0 ? r : l;       c = b - diff;       return true;     }     return false;   }   int main() {     int T;     cin >> T;     while (T--) {       long long l, r, m;       cin >> l >> r >> m;       for (long long a = l; a <= r; a++) {         long long n0 = m / a, n1 = m / a + 1;         long long b, c;         if (n0 > 0 && sol(n0, m, a, l, r, b, c)) {           printf( %lld %lld %lld n , a, b, c);           break;         }         if (n1 > 0 && sol(n1, m, a, l, r, b, c)) {           printf( %lld %lld %lld n , a, b, c);           break;         }       }     }   }


#include <bits/stdc++.h>   using namespace std;   int main() {     ios_base::sync_with_stdio(false), cin.tie(nullptr);     string a, b, c =   ;     cin >> a >> b;     for (int i = 0; i < a.length(); i++) {       if (a[i] + 1 < b[i]) {         c += a[i] + 1;         for (int j = i + 1; j < a.length(); j++) c +=  z ;         break;       } else if (a[i] + 1 == b[i]) {         bool big = 0;         for (int j = i + 1; j < a.length(); j++) {           if (a[j] !=  z ) big = 1;         }         if (big) {           c += a[i];           for (int j = i + 1; j < a.length(); j++) c +=  z ;           break;         } else {           c += a[i] + 1;           for (int j = i + 1; j < a.length(); j++) c +=  a ;           break;         }       } else         c += a[i];     }     if (c != a && c != b)       cout << c;     else       cout <<  No such string ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int arr[5] = {10, 15, 60, 4, 19};   int result = 0;   int getMin(int r, int g, int b) {     if (r < g) {       if (r < b) {         return r;       } else {         return b;       }     } else {       if (g < b) {         return g;       } else {         return b;       }     }   }   int getMax(int r, int g, int b) {     if (r > g) {       if (r > b) {         return r;       } else {         return b;       }     } else {       if (g > b) {         return g;       } else {         return b;       }     }   }   int getMin(int a, int b) {     if (a < b) {       return a;     } else {       return b;     }   }   void solve(int r, int g, int b) {     int count = 0;     int sum = r + g + b;     int min = getMin(r, g, b);     int max = getMax(r, g, b);     int mid = sum - (min + max);     result = getMin(sum / 2, min + mid);   }   void print(int arr[], int n) {     for (int i = 0; i < n; i++) {       printf( %d  , arr[i]);     }   }   void merge(int a[], int l, int m, int r) {     int n1 = m - l + 1;     int n2 = r - m;     int *L = new int[n1];     int *R = new int[n2];     for (int i = 0; i < n1; i++) {       L[i] = arr[l + i];     }     for (int i = 0; i < n2; i++) {       R[i] = arr[m + 1 + i];     }     int i = 0, j = 0, k = l;     while (i < n1 && j < n2) {       if (L[i] < R[j]) {         arr[k] = L[i];         k++;         i++;       } else {         arr[k] = R[j];         j++;         k++;       }     }     while (i < n1) {       arr[k] = L[i];       k++;       i++;     }     while (j < n2) {       arr[k] = R[j];       k++;       j++;     }   }   void merge(int a[], int l, int r) {     if (l < r) {       int m = l + (r - l) / 2;       merge(arr, l, m);       merge(arr, m + 1, r);       merge(arr, l, m, r);     }   }   int main() {     int n;     int r, g, b;     char ch;     int min = 9999999, max = -9999999;     scanf( %d , &n);     for (int i = 0; i < n; i++) {       scanf( %d , &r);       if (r > max) {         max = r;       }       scanf( %d , &g);       if (g > max) {         max = g;       }       scanf( %d , &b);       if (b > max) {         max = b;       }       solve(r, g, b);       printf( %d n , result);     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     string s;     cin >> s;     long long a = -1, b = -1;     int flag = 0;     for (int i = 0; i < s.length(); i++) {       if (s[i] ==  [ ) {         a = i;         flag = 1;         break;       }     }     if (flag == 0) {       cout <<  -1 ;       return 0;     }     flag = 0;     for (int i = a; i < s.length(); i++) {       if (s[i] ==  : ) {         a = i;         flag = 1;         break;       }     }     if (flag == 0) {       cout <<  -1 ;       return 0;     }     flag = 0;     for (int i = s.length() - 1; i >= 0; i--) {       if (s[i] ==  ] ) {         b = i;         flag = 1;         break;       }     }     if (flag == 0) {       cout <<  -1 ;       return 0;     }     flag = 0;     for (int i = b; i >= 0; i--) {       if (s[i] ==  : ) {         b = i;         flag = 1;         break;       }     }     if (flag == 0) {       cout <<  -1 ;       return 0;     }     if (a >= b) {       cout <<  -1 ;       return 0;     }     long long ans = 4;     for (int i = a + 1; i <= b - 1; i++) {       if (s[i] ==  | ) ans++;     }     cout << ans << endl;   }


#include <bits/stdc++.h>   using namespace std;   long long int a[500005];   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     long long int t, n, m, q, i, j, k, x, y = 2, z;     cin >> n >> k;     set<long long int> s;     for (i = 0; i < n; i++) cin >> a[i];     sort(a, a + n);     for (i = 0; i < n - 1; i++) {       x = a[i] + k;       if (x + k <= a[i + 1]) s.insert(x);       x = a[i + 1] - k;       if (a[i] + k <= x) s.insert(x);     }     cout << s.size() + 2;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n, x, y;     cin >> n >> x >> y;     int z = n / 2;     if (x >= z && x <= z + 1 && y >= z && y <= z + 1)       cout <<  NO ;     else       cout <<  YES ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int MAXN = 405;   char G[405][405], P[405][405];   bitset<MAXN> nolet[405][27];   int main() {     int N, M;     scanf( %d%d , &N, &M);     for (int i = 0; i < N; ++i) {       scanf( %s , G[i]);       for (int c = 0; c < 26; ++c)         for (int j = 0; j < M; ++j)           if (G[i][j] -  a  != c) nolet[i][c].set(j);     }     int R, C;     scanf( %d%d , &R, &C);     for (int i = 0; i < R; ++i) {       scanf( %s , P[i]);     }     for (int i = 0; i < N; ++i) {       bitset<MAXN> no;       for (int r = 0; r < R; ++r)         for (int c = 0; c < C; ++c) {           if (P[r][c] ==  ? ) continue;           int ii = (i + r) % N;           auto b = nolet[ii][P[r][c] -  a ];           no |= (b >> c) | (b << (M - c));         }       for (int j = 0; j < M; ++j) printf( %d , !no[j]);       printf(  n );     }   }


#include <bits/stdc++.h>   int a[100000];   int main() {     int y, k, n, i;     while (scanf( %d %d %d , &y, &k, &n) != EOF) {       int flag = 0;       int l = n / k;       for (i = 1; i <= l; i++) {         a[i] = i * k;         if (a[i] > y) {           printf( %d  , a[i] - y);           flag = 1;         }       }       if (flag == 0) printf( -1 );       printf(  n );     }   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n;     cin >> n;     char a[n][5];     for (int i = 1; i <= n; i++) {       for (int j = 1; j <= 5; j++) {         cin >> a[i][j];       }     }     int flag = 0;     for (int i = 1; i <= n; i++) {       for (int j = 1; j <= 4; j++) {         if ((a[i][j] ==  O ) && (a[i][j + 1] ==  O )) {           a[i][j] =  + ;           a[i][j + 1] =  + ;           flag = 1;           break;         }       }       if (flag) break;     }     if (flag) {       cout <<  YES  << endl;       for (int i = 1; i <= n; i++) {         for (int j = 1; j <= 5; j++) {           cout << a[i][j];         }         cout << endl;       }     } else       cout <<  NO  << endl;     return 0;   }


#include <bits/stdc++.h>   const int inf = (1ll << 30) - 1;   const int maxn = (int)1e5 + 10;   using namespace std;   pair<long long, long long> e[100100];   int en;   vector<int> g[100100];   long long dp[100100], d[100100];   int pr[100100];   int used[100100];   long long mi[100100];   int n, m, L, second, t;   void solve() {     scanf( %d%d%d%d%d , &n, &m, &L, &second, &t);     long long cons = (long long)1e9 + 1;     for (int i = 0, a, b, w; i < m; i++) {       scanf( %d%d%d , &a, &b, &w);       if (w == 0) {         g[a].push_back(en);         g[b].push_back(en);         e[en++] = make_pair(a ^ b, cons);       } else {         g[a].push_back(en);         g[b].push_back(en);         e[en++] = make_pair(a ^ b, w);       }     }     for (int i = 0; i < n; i++) {       dp[i] = 2ll * cons * n;     }     dp[t] = 0;     for (int i = 0; i < n; i++) {       int v = -1;       for (int j = 0; j < n; j++) {         if (used[j]) continue;         if (v == -1 || dp[v] > dp[j]) v = j;       }       used[v] = 1;       for (int j = 0; j < g[v].size(); j++) {         int ind = g[v][j];         int to = e[ind].first ^ v;         if (used[to]) continue;         if (e[ind].second < cons) {           dp[to] = min(dp[v] + e[ind].second, dp[to]);         }       }     }     for (int i = 0; i < n; i++) {       d[i] = 2ll * cons * n;       used[i] = 0;     }     d[second] = 0;     for (int i = 0; i < n; i++) {       int v = -1;       for (int j = 0; j < n; j++) {         if (used[j]) continue;         if (v == -1 || d[v] > d[j]) v = j;       }       used[v] = 1;       for (int j = 0; j < g[v].size(); j++) {         int ind = g[v][j];         int to = e[ind].first ^ v;         if (used[to]) continue;         if (e[ind].second < cons) {           d[to] = min(d[v] + e[ind].second, d[to]);         }       }     }     for (int i = 0; i < n; i++) {       used[i] = 0;     }     for (int i = 0; i < n; i++) {       int v = -1;       for (int j = 0; j < n; j++) {         if (used[j]) continue;         if (v == -1 || d[v] > d[j]) v = j;       }       used[v] = 1;       for (int j = 0; j < g[v].size(); j++) {         int ind = g[v][j];         int to = e[ind].first ^ v;         if (e[ind].second < cons) {           d[to] = min(d[v] + e[ind].second, d[to]);         } else {           if (mi[ind] == 0) {             mi[ind] = max(L - d[v] - dp[to], 1ll);           } else {             mi[ind] = max(L - d[v] - dp[to], mi[ind]);           }           d[to] = min(d[v] + mi[ind], d[to]);         }       }     }     for (int i = 0; i < n; i++) {       d[i] = 2ll * cons * n;       used[i] = 0;     }     for (int i = 0; i < en; i++) {       if (e[i].second == cons) e[i].second = mi[i];     }     d[second] = 0;     for (int i = 0; i < n; i++) {       int v = -1;       for (int j = 0; j < n; j++) {         if (used[j]) continue;         if (v == -1 || d[v] > d[j]) v = j;       }       used[v] = 1;       for (int j = 0; j < g[v].size(); j++) {         int ind = g[v][j];         int to = e[ind].first ^ v;         d[to] = min(d[v] + e[ind].second, d[to]);       }     }     if (d[t] != L) {       printf( NO n );       return;     }     printf( YES n );     for (int i = 0; i < n; i++) {       for (int j = 0; j < g[i].size(); j++) {         int ind = g[i][j];         int to = e[ind].first ^ i;         if (to > i) {           printf( %d %d %I64d n , to, i, e[ind].second);         }       }     }   }   int main() {     int t = 1;     for (int i = 1; i <= t; i++) {       solve();     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n, m;   int sz = 1;   string data[109];   struct node {     int x, y;     int pos;   };   bool operator<(const node& a, const node& b) {     if (a.x == b.x) {       if (a.y == b.y) {         return a.pos < b.pos;       }       return a.y < b.y;     }     return a.x < b.x;   }   map<node, int> mem;   int conv(vector<int> v) {     int ret = 0;     for (typeof(v.begin()) it = (v.begin()); it != v.end(); it++) {       ret = ret * 4 + (*it);     }     return ret;   }   vector<int> conv2(int pos) {     vector<int> ret;     for (long long i = 0; i < (long long)sz - 1; i++) {       ret.push_back(pos % 4);       pos /= 4;     }     reverse(ret.begin(), ret.end());     return ret;   }   int main() {     scanf( %d , &n);     scanf( %d , &m);     for (long long i = 0; i < (long long)n; i++) cin >> data[i];     queue<node> q;     vector<int> p;     int x = -1, y;     node top;     for (long long i = 0; i < (long long)n; i++) {       for (long long j = 0; j < (long long)m; j++) {         if (data[i][j] ==  1 ) {           x = i;           y = j;         }         if (data[i][j] >  1  && data[i][j] <=  9 ) {           sz = max(sz, (int)(data[i][j] -  0 ));         }       }     }     top.x = x;     top.y = y;     int xx, yy, px = x, py = y;     for (long long i = 0; i < (long long)sz - 1; i++) {       if (px - 1 >= 0) {         if ((data[px - 1][py] -  1 ) == i + 1) {           px--;           p.push_back(0);         }       }       if (px + 1 < n) {         if ((data[px + 1][py] -  1 ) == i + 1) {           px++;           p.push_back(1);         }       }       if (py - 1 >= 0) {         if ((data[px][py - 1] -  1 ) == i + 1) {           py--;           p.push_back(2);         }       }       if (py + 1 < m) {         if ((data[px][py + 1] -  1 ) == i + 1) {           py++;           p.push_back(3);         }       }     }     top.pos = conv(p);     q.push(top);     mem[top] = 1;     while (!q.empty()) {       top = q.front();       x = top.x;       y = top.y;       int pos = top.pos;       int cost = mem[top];       q.pop();       p = conv2(pos);       if (data[x][y] ==  @ ) {         cout << cost - 1 << endl;         return 0;       }       px = x;       py = y;       if (px - 1 >= 0 && ((data[px - 1][py]) !=  # )) {         vector<int> nw;         nw.push_back(1);         nw.insert(nw.end(), p.begin(), p.end() - 1);         int xx, yy;         xx = px - 1;         yy = py;         node temp;         temp.x = xx;         temp.y = yy;         xx = x;         yy = y;         int flag = 0;         for (int i = 1; i < sz - 1; i++) {           if (nw[i] == 0) {             xx--;           } else if (nw[i] == 1)             xx++;           else if (nw[i] == 2)             yy--;           else             yy++;           if (xx == temp.x && yy == temp.y) {             flag = 1;             break;           }         }         if (flag == 0) {           temp.pos = conv(nw);           if (mem.find(temp) == mem.end()) {             mem[temp] = cost + 1;             q.push(temp);           }         }       }       if (px + 1 < n && ((data[px + 1][py]) !=  # )) {         vector<int> nw;         nw.push_back(0);         nw.insert(nw.end(), p.begin(), p.end() - 1);         int xx, yy;         xx = px + 1;         yy = py;         node temp;         temp.x = xx;         temp.y = yy;         xx = x;         yy = y;         int flag = 0;         for (int i = 1; i < sz - 1; i++) {           if (nw[i] == 0) {             xx--;           } else if (nw[i] == 1)             xx++;           else if (nw[i] == 2)             yy--;           else             yy++;           if (xx == temp.x && yy == temp.y) {             flag = 1;             break;           }         }         if (flag == 0) {           temp.pos = conv(nw);           if (mem.find(temp) == mem.end()) {             mem[temp] = cost + 1;             q.push(temp);           }         }       }       if (py - 1 >= 0 && ((data[px][py - 1]) !=  # )) {         vector<int> nw;         nw.push_back(3);         nw.insert(nw.end(), p.begin(), p.end() - 1);         int xx, yy;         xx = px;         yy = py - 1;         node temp;         temp.x = xx;         temp.y = yy;         xx = x;         yy = y;         int flag = 0;         for (int i = 1; i < sz - 1; i++) {           if (nw[i] == 0) {             xx--;           } else if (nw[i] == 1)             xx++;           else if (nw[i] == 2)             yy--;           else             yy++;           if (xx == temp.x && yy == temp.y) {             flag = 1;             break;           }         }         if (flag == 0) {           temp.pos = conv(nw);           if (mem.find(temp) == mem.end()) {             mem[temp] = cost + 1;             q.push(temp);           }         }       }       if (py + 1 < m && ((data[px][py + 1]) !=  # )) {         vector<int> nw;         nw.push_back(2);         nw.insert(nw.end(), p.begin(), p.end() - 1);         int xx, yy;         xx = px;         yy = py + 1;         node temp;         temp.x = xx;         temp.y = yy;         xx = x;         yy = y;         int flag = 0;         for (int i = 1; i < sz - 1; i++) {           if (nw[i] == 0) {             xx--;           } else if (nw[i] == 1)             xx++;           else if (nw[i] == 2)             yy--;           else             yy++;           if (xx == temp.x && yy == temp.y) {             flag = 1;             break;           }         }         if (flag == 0) {           temp.pos = conv(nw);           if (mem.find(temp) == mem.end()) {             mem[temp] = cost + 1;             q.push(temp);           }         }       }     }     cout << -1 << endl;   }


#include <bits/stdc++.h>   using namespace std;   int n;   int p;   int a[300010];   int ans;   int main() {     int i, j, k;     scanf( %d , &n);     ans = 1;     a[n + 1] = -1;     printf( 1 );     for (k = 1; k <= n; ++k) {       scanf( %d , &p);       if (a[p + 1] == -1) {         i = p - 1;         a[p] = -1;         while ((i > 0) && (a[i] == 1)) {           a[i] = -1;           --ans;           --i;         }       } else {         if (!a[p]) {           a[p] = 1;           ++ans;         }       }       printf(  %d , ans);     }   }


#include <bits/stdc++.h>   struct Seg {     int l;     int r;     int y;   };   struct Event {     int x;     enum { BEG, END } type;     Seg seg;     static Event beginOf(Seg seg) { return Event{seg.l, BEG, seg}; }     static Event endOf(Seg seg) { return Event{seg.r, END, seg}; }     friend bool operator<(const Event& a, const Event& b) {       return std::tie(a.x, a.type, a.seg.y) < std::tie(b.x, b.type, b.seg.y);     }   };   int main() {     int sx, sy;     std::cin >> sy >> sx;     std::vector<Event> events;     events.reserve(sy * sx);     for (int y = 0; y < sy; y++) {       int intervals;       std::cin >> intervals;       for (int i = 0; i < intervals; i++) {         int l, r;         std::cin >> l >> r;         Seg seg{l - 1, r, y};         events.push_back(Event::beginOf(seg));         events.push_back(Event::endOf(seg));       }     }     std::sort(events.begin(), events.end());     std::vector<std::vector<int>> dp(sx + 1, std::vector<int>(sx + 1, 0));     for (int len = 1; len <= sx; len++) {       for (int l = 0, r = len; r <= sx; l++, r++) {         int currHeight = 0;         auto currEv = events.begin();         for (int x = l; x < r; x++) {           for (; currEv != events.end() && currEv->x <= x; ++currEv) {             if (l <= currEv->seg.l && currEv->seg.r <= r) {               if (currEv->type == Event::BEG)                 currHeight++;               else                 currHeight--;             }           }           dp[l][r] = std::max(dp[l][r],                               dp[l][x] + dp[x + 1][r] + currHeight * currHeight);         }       }     }     std::cout << dp[0][sx] << std::endl;   }


#include <bits/stdc++.h>   using namespace std;   inline int read() {     int x = 0, f = 1;     char c = getchar();     while (c <  0  || c >  9 ) {       if (c ==  - ) f = -1;       c = getchar();     }     while (c >=  0  && c <=  9 ) {       x = (x << 3) + (x << 1) + (c ^ 48);       c = getchar();     }     return f == 1 ? x : ~x + 1;   }   inline void print(int x) {     if (x < 0) putchar( - ), x = ~x + 1;     if (x >= 10) print(x / 10);     putchar((x % 10) | 48);   }   int typ;   int n;   int h[500010];   int ans[500010];   set<int> st;   int Id[500010];   bool cmp(int x, int y) { return h[x] > h[y]; }   bool b[500010];   int Ans;   struct node {     int vl, vr, l, r, ans;     bool full;     node() { vl = vr = l = r = full = 0; }     void init(int x) {       vl = vr = x;       l = r = ans = full = 1;     }   } tre[2000010];   node pushup(node ls, node rs) {     node u;     u.vl = ls.vl, u.vr = rs.vr;     u.ans = max(ls.ans, rs.ans);     u.l = ls.l, u.r = rs.r;     u.full = 0;     if (ls.vr ^ rs.vl) {       u.ans = max(u.ans, ls.r + rs.l);       u.full = (ls.full & rs.full);       if (ls.full) u.l = ls.l + rs.l;       if (rs.full) u.r = rs.r + ls.r;     }     return u;   }   void build(int p, int l, int r) {     if (l == r) {       tre[p].init(0);       return;     }     int mid = l + r >> 1;     build(p << 1, l, mid);     build(p << 1 | 1, mid + 1, r);     tre[p] = pushup(tre[p << 1], tre[p << 1 | 1]);   }   void modify(int p, int l, int r, int pos) {     if (l == r) {       tre[p].init(1);       return;     }     int mid = l + r >> 1;     if (pos <= mid)       modify(p << 1, l, mid, pos);     else       modify(p << 1 | 1, mid + 1, r, pos);     tre[p] = pushup(tre[p << 1], tre[p << 1 | 1]);     return;   }   node queryl(int p, int l, int r, int pos) {     if (l == r) return tre[p];     int mid = l + r >> 1;     if (pos <= mid) return queryl(p << 1, l, mid, pos);     node u = queryl(p << 1 | 1, mid + 1, r, pos);     return pushup(tre[p << 1], u);   }   node queryr(int p, int l, int r, int pos) {     if (l == r) return tre[p];     int mid = l + r >> 1;     if (pos > mid) return queryr(p << 1 | 1, mid + 1, r, pos);     node u = queryr(p << 1, l, mid, pos);     return pushup(u, tre[p << 1 | 1]);   }   void firm(int l, int r, int x) {     auto itl = st.lower_bound(l);     if (itl == st.end()) return;     vector<set<int>::iterator> vit;     while (itl != st.end() && (*itl) <= r) {       vit.push_back(itl);       ans[*itl] = x;       ++itl;     }     for (auto i : vit) st.erase(i);   }   int main() {     n = read();     for (int i = 1; i <= n; ++i) {       h[i] = read();       st.insert(i);       Id[i] = i;     }     sort(Id + 1, Id + n + 1, cmp);     build(1, 1, n);     for (int i = 1; i <= n; ++i) {       modify(1, 1, n, Id[i]);       b[Id[i]] = 1;       if (h[Id[i]] != h[Id[i + 1]]) Ans = max(Ans, tre[1].ans - 1 >> 1);       node tl = queryl(1, 1, n, Id[i]);       node tr = queryr(1, 1, n, Id[i]);       int sl = Id[i] - tl.r + 1, sr = Id[i] + tr.l - 1;       if (b[sl] == 0) {         if (b[sr] == 1) {           int len = (sr - sl + 1);           firm(sr - (len >> 1) + 1, sr, h[Id[i]]);         }       } else {         if (b[sr] == 0) {           int len = sr - sl + 1;           firm(sl, sl + (len >> 1) - 1, h[Id[i]]);         } else           firm(sl, sr, h[Id[i]]);       }       if (sl == Id[i] && Id[i] != 1) {         node tmp = queryl(1, 1, n, Id[i] - 1);         int len = tmp.r;         firm(Id[i] - (len >> 1), Id[i], h[Id[i]]);       }       if (sr == Id[i] && Id[i] != n) {         node tmp = queryr(1, 1, n, Id[i] + 1);         int len = tmp.l;         firm(Id[i], Id[i] + (len >> 1), h[Id[i]]);       }     }     printf( %d n , Ans);     for (int i = 1; i <= n; ++i) printf( %d  , ans[i]);   }


#include <bits/stdc++.h>   using namespace std;   int main() {     vector<string> v;     int i, j, k, l, l1, l2;     string a, b, s1, s2, s;     cin >> a >> b;     l1 = a.size();     l2 = b.size();     for (i = 1; i <= l1; i++) {       for (j = 1; j <= l2; j++) {         for (k = 0, s1 =   ; k < i; k++) {           s1 += a[k];         }         for (l = 0, s2 =   ; l < j; l++) {           s2 += b[l];         }         s = s1 + s2;         v.push_back(s);       }     }     sort(v.begin(), v.end());     cout << v[0] << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n;   int m;   struct st {     int dp;     int maxt;     int cnt;     bool use;     st() {       dp = maxt = cnt = 0;       use = false;     }   };   vector<st> dp[123457];   vector<int> v[123457];   vector<int> nex[123457];   bool oc[123457];   bool visited[123457];   pair<int, int> max1[123457];   pair<int, int> max2[123457];   int sum_cnt[123457];   int not_contain[123457];   int dpp[123457];   inline void dfs(int a, int b) {     if (dp[a][b].use) {       return;     }     dp[a][b].use = true;     if (visited[a]) {       dp[a][b].cnt = sum_cnt[a];       dp[a][b].dp = dpp[a];       dp[a][b].maxt = max1[a].first;       if (max1[a].second == b) {         dp[a][b].maxt = max2[a].first;       }       dp[a][b].use = true;       if (dp[v[a][b]][nex[a][b]].cnt) {         dp[a][b].dp -= dp[v[a][b]][nex[a][b]].dp + 2;         dp[a][b].cnt -= dp[v[a][b]][nex[a][b]].cnt;       }       dfs(v[a][not_contain[a]], nex[a][not_contain[a]]);       if (dp[v[a][not_contain[a]]][nex[a][not_contain[a]]].cnt) {         dp[a][b].dp += dp[v[a][not_contain[a]]][nex[a][not_contain[a]]].dp + 2;         dp[a][b].cnt += dp[v[a][not_contain[a]]][nex[a][not_contain[a]]].cnt;         dp[a][b].maxt =             max(dp[a][b].maxt,                 dp[v[a][not_contain[a]]][nex[a][not_contain[a]]].maxt + 1);       }       return;     }     visited[a] = true;     if (oc[a]) {       dp[a][b].cnt++;     }     int siz = v[a].size();     pair<int, int> MA1 = make_pair(0, -1);     pair<int, int> MA2 = make_pair(0, -1);     for (int i = 0; i < siz; i++) {       if (i == b) {         continue;       }       int l = v[a][i];       int r = nex[a][i];       if (!dp[l][r].use) {         dfs(l, r);       }       if (dp[l][r].cnt) {         dp[a][b].cnt += dp[l][r].cnt;         if (dp[l][r].maxt + 1 > dp[a][b].maxt) {           dp[a][b].maxt = dp[l][r].maxt + 1;         }         pair<int, int> ne = make_pair(dp[l][r].maxt + 1, i);         if (ne > MA1) {           MA2 = MA1;           MA1 = ne;         } else {           if (ne > MA2) {             MA2 = ne;           }         }         dp[a][b].dp += dp[l][r].dp + 2;       }     }     sum_cnt[a] = dp[a][b].cnt;     not_contain[a] = b;     max1[a] = MA1;     max2[a] = MA2;     dpp[a] = dp[a][b].dp;   }   int main() {     scanf( %d%d , &n, &m);     for (int i = 1; i < n; i++) {       int a, b;       scanf( %d%d , &a, &b);       a--;       b--;       nex[a].push_back(v[b].size());       nex[b].push_back(v[a].size());       v[a].push_back(b);       v[b].push_back(a);     }     for (int i = 0; i < n; i++) {       dp[i].assign(v[i].size(), st());     }     for (int i = 0; i < m; i++) {       int a;       scanf( %d , &a);       a--;       oc[a] = true;     }     int ans = INT_MAX;     int ans_index = 0;     for (int i = 0; i < n; i++) {       int maxx = 0;       int sum = 0;       int siz = v[i].size();       for (int j = 0; j < siz; j++) {         int l = v[i][j];         int r = nex[i][j];         dfs(l, r);         if (dp[l][r].cnt) {           if (dp[l][r].maxt + 1 > maxx) {             maxx = dp[l][r].maxt + 1;           }           sum += dp[l][r].dp + 2;         }       }       sum -= maxx;       if (ans > sum) {         ans = sum;         ans_index = i;       }     }     printf( %d n , ans_index + 1);     printf( %d n , ans);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   void ok(int x) {     cout << x <<   n ;     exit(0);   }   int main() {     int n, p;     cin >> n >> p;     for (int k = 0; k < int(1e8); k++) {       long long n1 = n - 1LL * p * k;       if (n1 < 0) continue;       if (n1 >= k && k >= __builtin_popcountll(n1)) ok(k);     }     ok(-1);   }


#include <bits/stdc++.h>   using namespace std;   vector<pair<int, int> > ans;   vector<int> e[100010];   void dfs(int x, int p, int entry) {     ans.emplace_back(x, entry);     if (x == 1) {       for (int it : e[x])         if (it != p) dfs(it, x, ++entry), ans.emplace_back(1, entry);     } else {       int cur = entry;       int sz = e[x].size() - 1;       if (entry - 1 >= sz) {         cur = entry - 1 - sz;         ans.emplace_back(x, cur);         for (int it : e[x])           if (it != p) {             cur++;             dfs(it, x, cur), ans.emplace_back(x, cur);           }       } else {         for (int it : e[x])           if (it != p) {             ++cur;             dfs(it, x, cur), ans.emplace_back(x, cur);             if (cur == sz + 1) {               cur = 0;               ans.emplace_back(x, 0);             }           }       }     }   }   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     int n;     cin >> n;     for (int i = 1, u, v; i < n; i++) {       cin >> u >> v;       e[u].emplace_back(v);       e[v].emplace_back(u);     }     dfs(1, 0, 0);     cout << ans.size() <<   n ;     for (auto &[u, v] : ans) cout << u <<     << v <<   n ;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     string s;     int n, sumA = 0, sumG = 0;     cin >> n;     vector<pair<int, int>> vec(n);     for (int i = 0; i < n; i++) cin >> vec[i].first >> vec[i].second;     if (vec[0].first > vec[0].second) {       sumG = vec[0].second;       s.push_back( G );     } else {       sumA = vec[0].first;       s.push_back( A );     }     for (int i = 1; i < n; i++) {       if (sumA + vec[i].first - sumG <= 500) {         sumA += vec[i].first;         s.push_back( A );       } else {         sumG += vec[i].second;         s.push_back( G );       }     }     if (abs(sumA - sumG) <= 500)       cout << s;     else       cout << -1;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   vector<int> spl;   int p[2000006];   void prime() {     p[0] = p[1] = 0;     for (int i = 2; i < 1420; i++) {       if (!p[i]) {         spl.push_back(i);         for (int j = i * i; j < 2000006; j += i) p[j] = i;       }     }   }   int n, q;   int a[100005];   vector<int> edge[100005];   int par[100005];   int siz[100005];   int dep[100005];   int dfs1(int u, int p, int d) {     par[u] = p;     siz[u] = 1;     dep[u] = d;     for (auto v : edge[u])       if (v != p) siz[u] += dfs1(v, u, d + 1);     return siz[u];   }   int cnt;   int reid[100005];   int reidm1[100005];   int head[100005];   void dfs2(int u, int h) {     reid[u] = cnt;     reidm1[cnt++] = u;     head[reid[u]] = reid[h];     int ch = 0, s = 0;     for (auto v : edge[u])       if (v != par[u] && s < siz[v]) s = siz[v], ch = v;     if (!ch) return;     dfs2(ch, h);     for (auto v : edge[u])       if (v != par[u] && v != ch) dfs2(v, v);   }   vector<int> prv[2000006];   void scan() {     for (int id = 1; id <= n; id++) {       int val = a[reidm1[id]];       while (p[val] > 0) {         prv[p[val]].push_back(id);         int k = p[val];         while (val % k == 0) val /= k;       }       if (val > 1) prv[val].push_back(id);     }   }   void build(int md) {     if (md) dfs1(1, 0, 1);     if (md) {       cnt = 1;       dfs2(1, 1);     }     scan();   }   int find(int id, int p) {     if (id < 1) return -1;     auto it = upper_bound(prv[p].begin(), prv[p].end(), id);     it--;     if (head[id] <= *it && *it <= id)       return *it;     else       return find(reid[par[reidm1[head[id]]]], p);   }   void sol() {     while (q--) {       int op, x, y;       scanf( %d , &op);       if (op == 1) {         scanf( %d , &x);         int val = a[x], ans = -1;         while (p[val]) {           ans = max(ans, find(reid[par[x]], p[val]));           int k = p[val];           while (val % k == 0) val /= k;         }         if (val > 1) ans = max(ans, find(reid[par[x]], val));         printf( %d n , ans == -1 ? -1 : reidm1[ans]);       } else {         scanf( %d%d , &x, &y);         int id = reid[x];         int val = a[x];         while (p[val] > 0) {           int idx = lower_bound(prv[p[val]].begin(), prv[p[val]].end(), id) -                     prv[p[val]].begin();           prv[p[val]].erase(prv[p[val]].begin() + idx);           int k = p[val];           while (val % k == 0) val /= k;         }         if (val > 1) {           int idx = lower_bound(prv[val].begin(), prv[val].end(), id) -                     prv[val].begin();           prv[val].erase(prv[val].begin() + idx);         }         val = a[x] = y;         while (p[val] > 0) {           prv[p[val]].push_back(id);           sort(prv[p[val]].begin(), prv[p[val]].end());           int k = p[val];           while (val % k == 0) val /= k;         }         if (val > 1) {           prv[val].push_back(id);           sort(prv[val].begin(), prv[val].end());         }       }     }   }   int main() {     prime();     scanf( %d%d , &n, &q);     for (int i = 1; i <= n; i++) scanf( %d , &a[i]);     for (int i = 1; i < n; i++) {       int x, y;       scanf( %d%d , &x, &y);       edge[x].push_back(y);       edge[y].push_back(x);     }     build(1);     sol();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   template <typename T>   T gcd(T a, T b) {     if (a == 0) return b;     return gcd(b % a, a);   }   template <typename T>   T pow(T a, T b, long long m) {     T ans = 1;     while (b > 0) {       if (b % 2 == 1) ans = ((ans % m) * (a % m)) % m;       b /= 2;       a = ((a % m) * (a % m)) % m;     }     return ans % m;   }   int n;   int a[1005];   int dp[1005][1005];   int solve(int f, int s) {     if (f > n) return 0;     if (s == n) {       return max(a[f], a[s]);     }     if (s > n) {       return a[f];     }     if (dp[f][s] != -1) return dp[f][s];     int ans = (long long)1e9 + 2;     ans = min(ans, max(a[f], a[s]) + solve(s + 1, s + 2));     ans = min(ans, max(a[f], a[s + 1]) + solve(s, s + 2));     ans = min(ans, max(a[s], a[s + 1]) + solve(f, s + 2));     return dp[f][s] = ans;   }   void print(int f, int s) {     if (f > n) return;     if (s == n) {       cout << f <<     << s <<   n ;       return;     }     if (s > n) {       cout << f;       return;     }     if (dp[f][s] == max(a[f], a[s]) + solve(s + 1, s + 2)) {       cout << f <<     << s <<   n ;       print(s + 1, s + 2);     } else if (dp[f][s] == max(a[f], a[s + 1]) + solve(s, s + 2)) {       cout << f <<     << s + 1 <<   n ;       print(s, s + 2);     } else if (dp[f][s] == max(a[s], a[s + 1]) + solve(f, s + 2)) {       cout << s <<     << s + 1 <<   n ;       print(f, s + 2);     }   }   int main() {     cin >> n;     for (int i = 1; i <= n; i++) cin >> a[i];     memset(dp, -1, sizeof dp);     cout << solve(1, 2) <<   n ;     print(1, 2);   }


#include <bits/stdc++.h>   using namespace std;   const int MAXH = 200000 + 10, MAXN = 2000 + 10;   const long long MOD = 1000000007;   struct Point {     int x, y;     bool operator<(const Point &p1) const { return x + y < p1.x + p1.y; }   } p[MAXN];   long long fac[MAXH], inv[MAXH];   long long f[MAXN];   inline long long Fastpow(long long a, long long b) {     long long res = 1;     while (b) {       if (b & 1) (res *= a) %= MOD;       (a *= a) %= MOD;       b >>= 1;     }     return res;   }   long long Calc(int a, int b) {     return ((((fac[a + b - 2] * inv[a - 1]) % MOD) * inv[b - 1]) % MOD);   }   int main() {     int h, w, n, mx;     scanf( %d%d%d , &h, &w, &n);     mx = h + w;     for (int i = 1; i <= n; ++i) scanf( %d%d , &p[i].x, &p[i].y);     p[++n] = (Point){h, w};     sort(p + 1, p + 1 + n);     fac[0] = inv[0] = 1;     for (int i = 1; i <= mx; ++i) fac[i] = fac[i - 1] * (long long)i % MOD;     for (int i = 1; i <= mx; ++i) inv[i] = Fastpow(fac[i], MOD - 2);     for (int i = 1; i <= n; ++i) {       f[i] = Calc(p[i].x, p[i].y);       for (int j = 1; j < i; ++j)         if (p[j].x <= p[i].x && p[j].y <= p[i].y) {           f[i] -= f[j] * Calc(p[i].x - p[j].x + 1, p[i].y - p[j].y + 1) % MOD;           ((f[i] %= MOD) += MOD) %= MOD;         }     }     cout << f[n] << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n, x = 0, y = 0;     cin >> n;     int *C = new int[n];     for (int i = 0; i < n; i++) {       cin >> C[i];     }     for (int j = 0; j < n; j++) {       if (j == 0) {         y = abs(C[j] - C[j + 1]);         x = abs(C[j] - C[n - 1]);       } else if (j == n - 1) {         x = max(abs(C[j] - C[n - 1]), abs(C[j] - C[0]));         y = abs(C[j] - C[j - 1]);       } else {         x = max(abs(C[j] - C[n - 1]), abs(C[j] - C[0]));         y = min(abs(C[j] - C[j + 1]), abs(C[j] - C[j - 1]));       }       cout << y <<     << x << endl;     }   }


#include <bits/stdc++.h>   using namespace std;   int modul(int a, int b) {     if (a - b < 0) return b - a;     return a - b;   }   int main() {     int n, k, st, et, s, e, d, v[103], j, m, stm, etm;     cin >> n >> k;     st = et = 0;     d = 0;     for (int i = 1; i <= n; i++) {       cin >> v[i];       if (v[i] == 1)         et++;       else         st++;     }     stm = etm = 0;     for (int i = 1; i <= k; i++) {       s = st;       e = et;       j = i;       while (j <= n) {         if (v[j] == 1)           e--;         else           s--;         j += k;       }       m = modul(s, e);       if (m > d) {         stm = s;         etm = e;         d = m;       }     }     cout << d;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 1010, L = 33, B = 7340033, root = 3;   int dp[L][N << 2];   int mypw(int x, int k) {     int ret = 1;     while (k) {       if (k & 1) ret = 1ll * ret * x % B;       k >>= 1;       x = 1ll * x * x % B;     }     return ret;   }   void change(int y[], int l) {     for (int i = 1, j = l / 2; i < l - 1; ++i) {       if (i < j) swap(y[i], y[j]);       int k = l / 2;       while (j >= k) {         j -= k;         k >>= 1;       }       if (j < k) j += k;     }   }   void fft(int y[], int l, int on) {     change(y, l);     for (int h = 2; h <= l; h <<= 1) {       int wn = mypw(root, (B - 1) / h);       if (on == -1) wn = mypw(wn, B - 2);       for (int j = 0; j < l; j += h) {         int w = 1;         for (int k = j; k < j + h / 2; ++k) {           int u = y[k];           int t = 1ll * y[k + h / 2] * w % B;           y[k] = u + t;           if (y[k] >= B) y[k] -= B;           y[k + h / 2] = u + B - t;           if (y[k + h / 2] >= B) y[k + h / 2] -= B;           w = 1ll * w * wn % B;         }       }     }     if (on == -1) {       int inv = mypw(l, B - 2);       for (int i = 0; i < l; ++i) {         y[i] = 1ll * y[i] * inv % B;       }     }   }   void calc(int a[], int b[], int l) {     l *= 2;     for (int i = 0; i < l; ++i) {       if (i < l / 2)         a[i] = b[i];       else         a[i] = 0;     }     fft(a, l, 1);     for (int i = 0; i < l; ++i) a[i] = 1ll * a[i] * a[i] % B;     fft(a, l, -1);   }   void init() {     dp[0][0] = 1;     for (int i = 1; i < L; ++i) {       calc(dp[i], dp[i - 1], 1 << 10);       calc(dp[i], dp[i], 1 << 10);       for (int j = (1 << 10) - 1; j > 0; --j) {         dp[i][j] = dp[i][j - 1];       }       dp[i][0] = 1;     }   }   int main() {     int q;     init();     scanf( %d , &q);     while (q--) {       int n, k;       scanf( %d%d , &n, &k);       int cnt = 0;       while (n > 1 && (n & 1)) {         ++cnt;         n >>= 1;       }       printf( %d n , dp[cnt][k]);     }     return 0;   }


#include <bits/stdc++.h>   const int BUFFSIZE = 10240;   char BUFF[BUFFSIZE + 1], *p = BUFF;   int CHAR, SIGN, BYTES = 0;   using namespace std;   vector<long long> V;   void dfs(long long N) {     if (N > 5000000000LL) return;     V.push_back(N);     dfs(N * 10 + 4);     dfs(N * 10 + 7);   }   int main() {     dfs(0);     sort(V.begin(), V.end());     long long pl, pr, vl, vr, k;     cin >> pl >> pr >> vl >> vr >> k;     double den = (pr - pl + 1) * (vr - vl + 1);     double num = 0;     for (int i = 1; i + k - 1 < V.size(); i++) {       long long x = V[i];       long long y = V[i + k - 1];       long long lftp = V[i - 1] + 1;       long long rgtp = x;       long long lftv = y;       long long rgtv = V[i + k] - 1;       lftp = max(lftp, pl);       rgtp = min(rgtp, pr);       lftv = max(lftv, vl);       rgtv = min(rgtv, vr);       if (lftp <= rgtp && lftv <= rgtv) {         double prob = (rgtp - lftp + 1.0) * (rgtv - lftv + 1.0);         num += prob;       }       lftv = V[i - 1] + 1;       rgtv = x;       lftp = y;       rgtp = V[i + k] - 1;       lftp = max(lftp, pl);       rgtp = min(rgtp, pr);       lftv = max(lftv, vl);       rgtv = min(rgtv, vr);       if (lftp <= rgtp && lftv <= rgtv) {         double prob = (rgtp - lftp + 1.0) * (rgtv - lftv + 1.0);         num += prob;       }     }     if (k == 1) {       for (int i = 1; i < V.size(); i++)         if (V[i] >= pl && V[i] <= pr && V[i] >= vl && V[i] <= vr) num -= 1;     }     double res = 1.0 * num / den;     printf( %.20lf n , res);     return 0;   }