#include <bits/stdc++.h>   using namespace std;   struct chess {     bool used;     int x, y;     char p;   } up, down, Left, Right, up_Left, up_Right, down_Left, down_Right;   int n, kx, ky;   void add(int x, int y, char c) {     if (x == kx) {       if (y < ky) {         if (!Left.used || y > Left.y) {           Left.x = x;           Left.y = y;           Left.p = c;           Left.used = true;         }       } else {         if (!Right.used || y < Right.y) {           Right.x = x;           Right.y = y;           Right.p = c;           Right.used = true;         }       }     }     if (y == ky) {       if (x < kx) {         if (!up.used || x > up.x) {           up.x = x;           up.y = y;           up.p = c;           up.used = true;         }       } else {         if (!down.used || x < down.x) {           down.x = x;           down.y = y;           down.p = c;           down.used = true;         }       }     }     if (x - y == kx - ky) {       if (x < kx) {         if (!up_Left.used || x > up_Left.x) {           up_Left.x = x;           up_Left.y = y;           up_Left.p = c;           up_Left.used = true;         }       } else {         if (!down_Right.used || x < down_Right.x) {           down_Right.x = x;           down_Right.y = y;           down_Right.p = c;           down_Right.used = true;         }       }     }     if (x + y == kx + ky) {       if (x < kx) {         if (!up_Right.used || x > up_Right.x) {           up_Right.x = x;           up_Right.y = y;           up_Right.p = c;           up_Right.used = true;         }       } else {         if (!down_Left.used || x < down_Left.x) {           down_Left.x = x;           down_Left.y = y;           down_Left.p = c;           down_Left.used = true;         }       }     }   }   bool check() {     if (up.p ==  R  || up.p ==  Q  || down.p ==  R  || down.p ==  Q ) return true;     if (Left.p ==  R  || Left.p ==  Q  || Right.p ==  R  || Right.p ==  Q )       return true;     if (up_Right.p ==  B  || up_Right.p ==  Q  || up_Left.p ==  B  ||         up_Left.p ==  Q )       return true;     if (down_Right.p ==  B  || down_Right.p ==  Q  || down_Left.p ==  B  ||         down_Left.p ==  Q )       return true;     return false;   }   int main() {     int x, y;     ios::sync_with_stdio(false);     cin.tie(0);     char c;     cin >> n >> kx >> ky;     for (int i = 0; i < n; i++) {       cin >> c >> x >> y;       add(x, y, c);     }     if (check())       cout <<  YES ;     else       cout <<  NO ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     ;     long long x;     cin >> x;     for (long long i = 0; i <= 1000; i++) {       for (long long j = 0; j <= 10000; j++) {         long long g = i * 1234567 + j * 123456;         long long m = (x - g) / 1234;         double l = (double)((x - g) / 1234);         long long d = (long long)(l);         if (m == d && m >= 0 && g + m * 1234 == x) {           cout <<  YES  << endl;           return 0;         }       }     }     cout <<  NO  << endl;     return 0;   }


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


#include <bits/stdc++.h>   using namespace std;   using namespace std;   long long k, p;   long long getZCY(long long x) {     long long res = x;     while (x) {       res *= 10LL;       res += x % 10;       x /= 10;     }     return res;   }   int main() {     scanf( %I64d , &k);     scanf( %I64d , &p);     long long ans = 0;     for (int i = (1); i <= (int)(k); ++i) {       ans += getZCY(i);       ans %= p;     }     cout << ans << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   vector<int> neigh[200009];   int ans[200009];   int a[200009];   int b[200009];   int c[200009];   int f[200009];   int Q[200009];   int L, R;   int main() {     int n, m;     scanf( %d %d , &n, &m);     for (int i = 0; i < m; i++) {       scanf( %d %d %d , &a[i], &b[i], &c[i]);       f[a[i]] += c[i];       f[b[i]] += c[i];       neigh[a[i]].push_back(i);       neigh[b[i]].push_back(i);     }     f[1] = f[n] = 0;     for (int i = 2; i <= n - 1; i++) f[i] /= 2;     Q[R++] = 1;     while (L < R) {       int cur = Q[L++];       for (int i = 0; i < neigh[cur].size(); i++) {         int u;         if (cur == a[neigh[cur][i]])           u = b[neigh[cur][i]];         else           u = a[neigh[cur][i]];         if (u != n && f[u] == 0) continue;         if (u == a[neigh[cur][i]]) ans[neigh[cur][i]] = 1;         f[u] -= c[neigh[cur][i]];         if (u != n && f[u] == 0) Q[R++] = u;       }     }     for (int i = 0; i < m; i++) printf( %d n , ans[i]);   }


#include <bits/stdc++.h>   using namespace std;   long long modpow(long long base, long long exp, long long modulus) {     base %= modulus;     long long result = 1;     while (exp > 0) {       if (exp & 1) result = (result * base) % modulus;       base = (base * base) % modulus;       exp >>= 1;     }     return result;   }   long long n;   long long a[100005];   long long dp[5004][5005];   long long bin(long long lo, long long hi, long long val) {     long long ans = -1;     while (lo <= hi) {       long long mid = (lo + hi) / 2;       if (a[mid] >= val) {         ans = mid;         hi = mid - 1;       } else {         lo = mid + 1;       }     }     return ans;   }   long long doit(long long prev, long long curr, int flag) {     if (curr == n + 1) return 0;     if (dp[prev][curr] != -1) return dp[prev][curr];     long long ans = 0;     long long y = 100009;     long long x = 1 + doit(prev, curr + 1, flag);     long long val = a[curr - 1] - a[prev - 1] + a[curr - 1];     long long ind = bin(curr, n, val);     if (ind != -1) {       y = doit(curr, ind + 1, flag) + (ind - curr);     }     ans = min(x, y);     dp[prev][curr] = ans;     return ans;   }   int main() {     scanf( %lld , &n);     memset(dp, -1, sizeof(dp));     for (int i = 1; i <= n; i++) {       scanf( %lld , &a[i]);       a[i] = a[i] + a[i - 1];     }     long long ans = doit(1, 2, 0);     cout << ans;     printf(  n );     ;     return 0;   }


#include <iostream>   #include <vector>   #include <algorithm>   #include <string>    #include <set>   #include <cmath>   #include <bitset>   #include <iomanip>   #include <queue>   #include <stack>   #include <map>   #include <unordered_map>   #include <unordered_set>   #include <chrono>   #include <random>   #include <complex>   using namespace std;      mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());      inline void boostIO() {    ios_base::sync_with_stdio(0);    cin.tie(0);    cout.tie(0);    cout << fixed << setprecision(10);   }      bool isPrime(int x) {    if (x <= 4 || x % 2 == 0 || x % 3 == 0) return x == 2 || x == 3;    for (int i = 5; i * i <= x; i += 6) if (x % i == 0 || x % (i + 2) == 0) return 0; return 1;   }      typedef long long int ll;   typedef long double ld;   typedef unsigned long long ull;      typedef vector<int> vi;   typedef vector<ll> vll;   typedef pair<int, int> pii;      #define fori(n) for(int i = 0; i < (n); ++i)   #define forj(n) for(int j = 0; j < (n); ++j)   #define fork(n) for(int k = 0; k < (n); ++k)   #define forx(n) for(int x = 0; x < (n); ++x)      #define all(x) (x).begin(), (x).end()   #define rall(x) (x).rbegin(), (x).rend()   #define ff first   #define ss second   #define debug(x) cerr << #x <<   =   << x << endl;   #define debug_p(x) cerr << #x <<     << x.ff <<     << x.ss << endl;   #define debug_v(x) cerr << #x <<    ; for (auto ii : x) cout << i <<    ; cout << endl;   #define debug_vp(x) cerr << #x <<    ; for (auto ii : x) cout <<  [  << ii.ff <<     << ii.ss <<  ] ; cout << endl;   #define mp make_pair   #define rand kjsdflsdfsdlsjdlkfsdjflsjdfl       #define Return cerr<<endl<< RUNTIME:  <<1.0*clock()/CLOCKS_PER_SEC <<   s  <<endl; return 0;      #define PI 3.14159265      ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }   ll lcm(ll a, ll b) { return a * b / gcd(a, b); }      /////////////////////   #define int long long   int Mod = 1e9 + 7;   #define endl   n    /////////////////////      int inf = 1e18;      vector<int> A;   int dp[2000][2000] = {};   int seen[2000][2000] = {};      int rec(int l, int r) {    if (l == r) {     return 0;    }    if (seen[l][r]) {     return dp[l][r];    }    seen[l][r] = 1;    //here    int ans = inf;    ans = min(rec(l+1, r), rec(l, r-1)) + A[r] - A[l];    dp[l][r] = ans;    return dp[l][r];      }      int32_t main() {    boostIO();    int n; cin >> n;    A.assign(n, 0);    fori(n) {     cin >> A[i];    }     sort(all(A));    cout << rec(0, n - 1);   }


#include <bits/stdc++.h>   using namespace std;   const int INF = 0x3f3f3f3f;   const int MOD = 1e9 + 7;   int n, m;   vector<int> a;   int main() {     ios_base::sync_with_stdio(0), cin.tie(0);     cin >> n >> m;     a.resize(n);     for (int i = 0; i < n; i++) cin >> a[i];     sort(a.begin(), a.end());     int cnt = 0;     for (int i = n - 1; i >= 0; i--) {       if (m <= 0) break;       m -= a[i], cnt++;     }     cout << cnt <<   n ;   }


#include <bits/stdc++.h>   using namespace std;   int n, m, z;   int gcd(int n, int m) { return m == 0 ? n : gcd(m, n % m); }   int main() {     cin >> n >> m >> z;     cout << (z / ((long long)n * m / gcd(n, m)));     return 0;   }


#include <bits/stdc++.h>   using namespace std;   bool compare(string &s1, string &s2) { return s1.size() < s2.size(); }   bool sortmahstyle(const pair<int, int> &a, const pair<int, int> &b) {     if (a.first < b.first) return true;     return false;   }   void solve() {     long long int n, op;     cin >> n >> op;     string s;     cin >> s;     for (int i = 0; i < n - 2 && op > 0; i++) {       if (s[i] !=  4  && s[i + 1] ==  4  && s[i + 2] ==  7 ) {         if (i % 2)           s[i + 2] =  4 , op--;         else           s[i + 1] =  7 , op--;       } else if (s[i] ==  4  && s[i + 1] ==  7  && s[i + 2] !=  7 ) {         if (i % 2 == 0)           s[i + 1] =  4 , op--;         else           s[i] =  7 , op--;       } else if (s[i] ==  4  && s[i + 1] ==  7  && s[i + 2] ==  7 ) {         if (i % 2 == 0) {           if (op % 2) {             s[i + 1] =  4 ;             break;           } else {             break;           }         } else           s[i] =  7 , op--;       } else if (s[i] ==  4  && s[i + 1] ==  4  && s[i + 2] ==  7 ) {         if (i % 2 == 0) {           if (op % 2) {             s[i + 1] =  7 ;             break;           } else {             break;           }         } else           s[i + 2] =  4 , op--;       }     }     cout << s <<   n ;   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     long long int t = 1;     while (t-- > 0) solve();     return 0;   }


#include <bits/stdc++.h>   using std::swap;   const int N = (1 << 18) + 5, p = 998244353, G = 3;   int rev[N], invG, f[N], g[N], inv2;   inline int Dec(int x, int y) {     x -= y;     return x < 0 ? x + p : x;   }   inline int Plus(int x, int y) {     x += y;     return x >= p ? x - p : x;   }   inline void read(int &x) {     x = 0;     char ch = getchar();     while (ch >  9  || ch <  0 ) ch = getchar();     while (ch <=  9  && ch >=  0 ) x = x * 10 + (ch ^ 48), ch = getchar();   }   inline int quick_pow(int a, int b) {     int ans = 1;     while (b) {       if (b & 1) ans = a * 1ll * ans % p;       a = a * 1ll * a % p;       b >>= 1;     }     return ans;   }   inline void butterfly(int n) {     register int i;     for (i = 0; i < n; ++i) rev[i] = (rev[i >> 1] >> 1) | ((i & 1) ? n >> 1 : 0);   }   void NTT(int a[], bool op, int n) {     register int i, j, mid;     static int w[N];     w[0] = 1;     for (i = 0; i < n; ++i)       if (i < rev[i]) swap(a[i], a[rev[i]]);     for (mid = 1; mid < n; mid <<= 1) {       int g1 = quick_pow(op ? G : invG, (p - 1) / (mid << 1));       for (j = 1; j < mid; ++j) w[j] = w[j - 1] * 1ll * g1 % p;       for (i = 0; i < n; i += mid << 1)         for (j = 0; j < mid; ++j) {           int x = a[i + j], y = a[i + j + mid] * 1ll * w[j] % p;           a[i + j] = Plus(x, y), a[i + j + mid] = Dec(x, y);         }     }     if (!op) {       int invn = quick_pow(n, p - 2);       for (i = 0; i < n; ++i) a[i] = a[i] * 1ll * invn % p;     }   }   void Poly_Inv(int a[], int b[], int m) {     int n;     register int i, len;     for (n = 1; n < m; n <<= 1)       ;     static int w[N], tmp[N], r[N];     memset(w, 0, sizeof(int) * (n << 1));     memset(r, 0, sizeof(int) * (n << 1));     memset(tmp, 0, sizeof(int) * (n << 1));     w[0] = quick_pow(a[0], p - 2);     for (len = 2; len <= n; len <<= 1) {       int Len = len << 1;       memcpy(tmp, a, sizeof(int) * len);       memcpy(r, w, sizeof(int) * (len >> 1));       butterfly(Len);       NTT(r, 1, Len), NTT(tmp, 1, Len);       for (i = 0; i < Len; ++i) r[i] = r[i] * 1ll * r[i] % p * tmp[i] % p;       NTT(r, 0, Len);       for (i = 0; i < len; ++i) w[i] = Dec((w[i] << 1) % p, r[i]);       memset(r + len, 0, sizeof(int) * len);     }     memcpy(b, w, sizeof(int) * n);   }   void Poly_Sqrt(int a[], int b[], int m) {     int n;     register int i, len;     for (n = 1; n < m; n <<= 1)       ;     static int w[N], tmp[N], r[N];     memset(w, 0, sizeof(int) * (n << 1));     memset(r, 0, sizeof(int) * (n << 1));     memset(tmp, 0, sizeof(int) * (n << 1));     w[0] = a[0];     for (len = 2; len <= n; len <<= 1) {       int Len = len << 1;       Poly_Inv(w, r, len);       butterfly(Len);       memcpy(tmp, a, sizeof(int) * len);       NTT(r, 1, Len), NTT(tmp, 1, Len);       for (i = 0; i < Len; ++i) r[i] = r[i] * 1ll * tmp[i] % p;       NTT(r, 0, Len);       for (i = 0; i < len; ++i)         w[i] = Plus(inv2 * 1ll * w[i] % p, inv2 * 1ll * r[i] % p);     }     memcpy(b, w, sizeof(int) * n);   }   int main() {     int n, m, x;     register int i;     read(n), read(m);     f[0] = 1;     for (i = 1; i <= n; ++i) read(x), f[x] = 1;     for (i = 1; i <= m; ++i) f[i] = Dec(0, 4 * f[i]);     invG = quick_pow(G, p - 2), inv2 = quick_pow(2, p - 2);     Poly_Sqrt(f, g, m + 1);     ++g[0];     Poly_Inv(g, f, m + 1);     for (i = 1; i <= m; ++i) printf( %d n , f[i] * 2ll % p);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   bool isPrime(long long n) {     if (n == 1) return false;     if (n == 2) return true;     for (long long i = 2; i * i <= n; i++) {       if (n % i == 0) return false;     }     return true;   }   long long powm(long long a, long long b) {     long long res = 1;     while (b) {       if (b & 1) res = res * 1ll * a % 1000000007;       a = a * 1ll * a % 1000000007;       b >>= 1;     }     return res;   }   long long mul(long long x, long long y) { return (x * 1ll * y) % 1000000007; }   void solve() {     long long i = 0, j = 0, k = 0, n = 0, m = 0, rb, cb, rd, cd;     cin >> n >> m >> rb >> cb >> rd >> cd;     if (rd >= rb && cd >= cb) {       {         cout << min((rd - rb), (cd - cb)) <<   n ;         return;       };     }     if (cd >= cb) {       cout << min((n - rb) * 2 + abs(rb - rd), (cd - cb)) <<   n ;       return;     };     if (rd >= rb) {       cout << min((m - cb) * 2 + abs(cb - cd), (rd - rb)) <<   n ;       return;     };     {       cout << min((((n - rb) * 2) + abs(rb - rd)), ((m - cb) * 2 + abs(cb - cd)))            <<   n ;       return;     }   }   signed main() {     ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);     cout << setprecision(15);     long long t = 1;     cin >> t;     while (t--) {       solve();     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int maxn = 10001;   int st, en;   const int limit = -5000000;   const int maxlongint = 1 << 20;   struct edges {     int v, flow, rev;     int cost;   } make;   vector<edges> g[maxn];   int dist[maxn];   bool vis[maxn];   int tot;   bool ans[maxn];   int i, j, m, n, l, o, p, x, y, z;   int k;   struct pairs {     int x, y;   } pre[maxn];   struct tris {     int s, t, fee;   } sp[maxn];   inline bool spfa() {     for (int i = 0; i <= st; i++) dist[i] = maxlongint;     dist[st] = 0;     queue<int> q;     q.push(st);     while (!q.empty()) {       int x = q.front();       vis[x] = false;       q.pop();       for (int i = 0; i < g[x].size(); i++)         if (g[x][i].flow && dist[g[x][i].v] > dist[x] + g[x][i].cost) {           dist[g[x][i].v] = dist[x] + g[x][i].cost;           pre[g[x][i].v].x = x;           pre[g[x][i].v].y = i;           if (!vis[g[x][i].v]) {             q.push(g[x][i].v);             vis[g[x][i].v] = true;           }         }     }     return dist[en] < maxlongint;   }   inline void addedge(int x, int y, int flow, int cost) {     int v1 = g[x].size();     int v2 = g[y].size();     make.v = y;     make.flow = flow;     make.cost = cost;     make.rev = v2;     g[x].push_back(make);     make.v = x;     make.flow = 0;     make.rev = v1;     make.cost = -cost;     g[y].push_back(make);   }   int mincut(int x, int fw) {     if (x == st) return fw;     int f = mincut(pre[x].x, min(fw, g[pre[x].x][pre[x].y].flow));     g[pre[x].x][pre[x].y].flow -= f;     g[x][g[pre[x].x][pre[x].y].rev].flow += f;     return f;   }   int main() {     scanf( %d%d , &n, &k);     en = 2 * n + 1;     st = en + 1;     addedge(st, 0, k, 0);     for (int i = 1; i <= n; i++) {       scanf( %d%d%d , &sp[i].s, &sp[i].t, &sp[i].fee);       addedge(0, i * 2 - 1, 1, 0);       addedge(i * 2 - 1, i * 2, 1, -sp[i].fee);       addedge(i * 2, en, 1, 0);     }     for (int i = 1; i <= n; i++)       for (int j = 1; j <= n; j++)         if (sp[i].s + sp[i].t <= sp[j].s) addedge(i * 2, j * 2 - 1, 1, 0);     while (spfa()) mincut(en, maxlongint);     for (int i = 1; i <= n; i++)       for (j = 0; j < g[i * 2 - 1].size(); j++)         if (g[i * 2 - 1][j].v == i * 2 && g[i * 2 - 1][j].flow == 0)           ans[i] = true;     for (int i = 1; i <= n; i++) printf( %d  , ans[i]);     cout << endl;     fclose(stdin);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   namespace FFT {   const int N = 1 << 21;   struct Complex {     double r, i;   };   inline Complex operator+(const Complex &a, const Complex &b) {     return {a.r + b.r, a.i + b.i};   }   inline Complex operator-(const Complex &a, const Complex &b) {     return {a.r - b.r, a.i - b.i};   }   inline Complex operator*(const Complex &a, const Complex &b) {     return {a.r * b.r - a.i * b.i, a.i * b.r + a.r * b.i};   }   int rev[N];   const double PI = acos(-1);   void FFT(Complex a[], int n, int fl) {     for (int i = 0; i < n; i++)       if (i < rev[i]) swap(a[i], a[rev[i]]);     for (int s = 1; s < n; s <<= 1) {       Complex wn = {cos(PI / s), sin(PI / s) * fl};       for (int i = 0; i < n; i += s << 1) {         Complex w = {1, 0};         for (int j = 0; j < s; j++, w = w * wn) {           Complex x = a[i + j], y = w * a[i + s + j];           a[i + j] = x + y;           a[i + s + j] = x - y;         }       }     }     if (fl == -1)       for (int i = 0; i < n; i++) a[i].r /= n, a[i].i /= n;   }   Complex f[N], g[N];   int n, m;   void solve() {     int lim = 1, d = 0;     while (lim <= n + m) ++d, lim <<= 1;     for (int i = 0; i < lim; i++)       rev[i] = (rev[i >> 1] >> 1) | ((i & 1) << (d - 1));     for (int i = n + 1; i < lim; i++) f[i] = {0, 0};     for (int i = m + 1; i < lim; i++) g[i] = {0, 0};     FFT(f, lim, 1);     FFT(g, lim, 1);     for (int i = 0; i < lim; i++) f[i] = f[i] * g[i];     FFT(f, lim, -1);   }   }  // namespace FFT   const int N = 55, M = 105, K = 20005;   const double INF = 1e18;   int n, m, t, x, a[M], b[M];   double dis[N][N], c[M], p[M][K * 2], f[N][K * 2], g[M][K];   void solve(int l, int r) {     if (l == t) return;     if (l == r) {       for (int i = 1; i < n; i++) f[i][l] = INF;       for (int i = 1; i <= m; i++)         if (a[i] != n) f[a[i]][l] = min(f[a[i]][l], g[i][l] + c[i]);       return;     }     int mid = (l + r) >> 1;     solve(mid + 1, r);     FFT::n = r - l - 1;     FFT::m = r - mid - 1;     for (int i = 1; i <= m; i++)       if (a[i] != n) {         for (int j = 0; j <= FFT::n; j++) FFT::f[j] = {p[i][j + 1], 0};         for (int j = 0; j <= FFT::m; j++) FFT::g[j] = {f[b[i]][r - j], 0};         FFT::solve();         for (int j = l; j <= mid; j++) g[i][j] += FFT::f[r - j - 1].r;       }     solve(l, mid);   }   int main() {     ios::sync_with_stdio(false);     cin >> n >> m >> t >> x;     for (int i = 1; i <= n; i++)       for (int j = 1; j <= n; j++)         if (i != j) dis[i][j] = INF;     int t1;     for (int i = 1; i <= m; i++) {       cin >> a[i] >> b[i] >> t1;       c[i] = dis[a[i]][b[i]] = t1;       for (int j = 1; j <= t; j++) cin >> t1, p[i][j] = t1 / 1e5;     }     for (int k = 1; k <= n; k++)       for (int i = 1; i <= n; i++)         for (int j = 1; j <= n; j++)           dis[i][j] = min(dis[i][k] + dis[k][j], dis[i][j]);     for (int i = 0; i < t * 2; i++) f[n][i] = i <= t ? 0 : x;     for (int i = 1; i < n; i++)       for (int j = t; j < t * 2; j++) f[i][j] = dis[i][n] + x;     solve(0, t * 2 - 1);     printf( %.10lf n , f[1][0]);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   inline void write(long long a) {     if (a >= 10) write(a / 10);     putchar( 0  + a % 10);   }   inline void writeln(int a) {     write(a);     puts(  );   }   inline int read() {     int x = 0;     char ch = getchar();     bool positive = 1;     for (; !isdigit(ch); ch = getchar())       if (ch ==  - ) positive = 0;     for (; isdigit(ch); ch = getchar()) x = x * 10 + ch -  0 ;     return positive ? x : -x;   }   const int N = 200005, oo = 2000000005;   struct data {     int l, r, cost;   } a[N], b[N];   int n, m, ans, tong[N], j;   inline bool cmp1(data a, data b) { return a.r < b.r; }   inline bool cmp2(data a, data b) { return a.l < b.l; }   int main() {     n = read();     m = read();     for (int i = 1; i <= n; i++) {       a[i].l = read();       a[i].r = read();       a[i].cost = read();       b[i] = a[i];     }     sort(&a[1], &a[n + 1], cmp1);     ans = oo;     sort(&b[1], &b[n + 1], cmp2);     j = 1;     for (int i = 1; i <= n; i++) {       for (; a[j].r < b[i].l; j++)         if (!tong[a[j].r - a[j].l + 1])           tong[a[j].r - a[j].l + 1] = a[j].cost;         else           tong[a[j].r - a[j].l + 1] = min(tong[a[j].r - a[j].l + 1], a[j].cost);       if (m >= b[i].r - b[i].l + 1 && tong[m - (b[i].r - b[i].l + 1)])         ans = min(ans, b[i].cost + tong[m - (b[i].r - b[i].l + 1)]);     }     if (ans < oo)       writeln(ans);     else       puts( -1 );   }


#include <bits/stdc++.h>   using namespace std;   int const INF = numeric_limits<int>::max();   long long const LLINF = numeric_limits<long long>::max();   long double const EPS = 1e-9;   int n, m, k, q;   int const N = 505;   int a[N][N];   struct qu {     int x, y, t;   };   bool operator<(const qu& a, const qu& b) { return a.t < b.t; }   qu qq[N * N];   bool check() {     int ans = 0;     vector<int> d(m, -1), d1(m), d2(m);     stack<int> st;     for (int i = 0; i < n; ++i) {       for (int j = 0; j < m; ++j)         if (a[i][j] == 0) d[j] = i;       while (!st.empty()) st.pop();       for (int j = 0; j < m; ++j) {         while (!st.empty() && d[st.top()] <= d[j]) st.pop();         d1[j] = st.empty() ? -1 : st.top();         st.push(j);       }       while (!st.empty()) st.pop();       for (int j = m - 1; j >= 0; --j) {         while (!st.empty() && d[st.top()] <= d[j]) st.pop();         d2[j] = st.empty() ? m : st.top();         st.push(j);       }       for (int j = 0; j < m; ++j)         ans = max(ans, min((i - d[j]), (d2[j] - d1[j] - 1)));     }     return ans >= k;   }   int main() {     ios::sync_with_stdio(false);     cin >> n >> m >> k >> q;     for (int i = 0; i < q; i++) {       cin >> qq[i].x >> qq[i].y >> qq[i].t;       qq[i].x--;       qq[i].y--;       a[qq[i].x][qq[i].y] = 1;     }     if (!check()) {       cout << -1 << endl;       return 0;     }     sort(qq, qq + q);     int l = 0, r = q - 1;     while (r - l > 1) {       int mid = (l + r) / 2;       for (int i = 0; i < n; i++) {         for (int j = 0; j < m; j++) {           a[i][j] = 0;         }       }       for (int i = 0; i <= mid; i++) {         a[qq[i].x][qq[i].y] = 1;       }       if (check()) {         r = mid;       } else {         l = mid;       }     }     while (r >= l) {       for (int i = 0; i < n; i++) {         for (int j = 0; j < m; j++) {           a[i][j] = 0;         }       }       for (int i = 0; i <= l; i++) {         a[qq[i].x][qq[i].y] = 1;       }       if (check()) {         break;       } else {         l++;       }     }     cout << qq[l].t << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   struct node {     int x, y;   };   int main() {     int n;     while (scanf( %d , &n) != EOF) {       int i, j, cnt = 0;       node a[110];       for (i = 0; i < n; i++) {         scanf( %d%d , &a[i].x, &a[i].y);       }       bool u[110] = {0};       queue<int> q;       for (i = 0; i < n; i++) {         if (u[i]) continue;         u[i] = 1;         q.push(i);         while (!q.empty()) {           node t = a[q.front()];           q.pop();           for (j = 0; j < n; j++) {             if (u[j] == 0 && (t.x == a[j].x || t.y == a[j].y)) {               u[j] = 1;               q.push(j);             }           }         }         cnt += 1;       }       printf( %d n , cnt - 1);     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     long long t;     cin >> t;     while (t--) {       long long a, b;       cin >> a >> b;       long long xx = fabs(a - b);       long long d = xx / 2;       if ((a + b) % 2) {         long long yy = a + b - 2 * d + 1;         cout << yy <<   n ;         for (long long i = d; i <= a + b - d; i++) {           cout << i <<    ;         }         cout <<   n ;       } else {         long long yy = (a + b - 2 * d) / 2 + 1;         cout << yy <<   n ;         for (long long i = d; i <= a + b - d; i += 2) {           cout << i <<    ;         }         cout <<   n ;       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());   const long long N = 2e5 + 5, M = N;   long long mx, n;   vector<long long> a;   long long dp[N];   void solve() {     long long i, j, m;     cin >> n;     a.resize(n);     map<long long, long long> mapp;     for (i = 0; i < n; i++) {       cin >> a[i];       mapp[a[i] - i] += a[i];     }     for (auto it : mapp) {       mx = max(it.second, mx);     }     cout << mx;   }   int32_t main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     solve();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int power(int a, int b, int c) {     long long x = 1, y = a;     while (b > 0) {       if (b % 2 == 1) {         x = (x * y) % c;       }       y = (y * y) % c;       b /= 2;     }     return x % c;   }   long long int gcd(long long int a, long long int b) {     if (b == 0)       return a;     else       return gcd(b, a % b);   }   int dp[26][1 << 21];   int main() {     long long int n, m;     string str1, str2;     memset(dp, 0, sizeof(dp));     long long int ans = 0, i;     cin >> n >> m;     cin >> str1 >> str2;     long long int g = gcd(int((str1).size()), int((str2).size()));     for (i = 0; i < int((str1).size()); i++) {       dp[str1[i] -  a ][i % g]++;     }     for (i = 0; i < int((str2).size()); i++) {       ans += dp[str2[i] -  a ][i % g];     }     ans = int((str2).size()) * m - ans * gcd(n, m);     cout << ans << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int maxn = 2e+5 + 10;   const int INF = 0x7fffffff;   struct edge {     int v, n;   } e[maxn * 2];   struct node {     int mx, mi, i, c;     bool operator<(const node &sec) const { return mx > sec.mx; }   } a[maxn];   int n, m, k;   int head[maxn], rd[maxn];   void read() {     int i, u, v;     scanf( %d%d%d , &n, &m, &k);     for (i = 1; i <= n; i++) {       scanf( %d , &a[i].c);       a[i].i = i;       if (a[i].c)         a[i].mx = a[i].mi = a[i].c;       else         a[i].mx = k, a[i].mi = 1;     }     memset(head, -1, sizeof(head));     for (i = 0; i < m; i++) {       scanf( %d%d , &u, &v);       e[i << 1].v = v;       e[i << 1].n = head[u];       head[u] = i << 1;       e[i << 1 | 1].v = u;       e[i << 1 | 1].n = head[v];       head[v] = i << 1 | 1;     }   }   void tuopu(int d) {     int i, u, v;     queue<int> q;     memset(rd, 0, sizeof(rd));     for (i = 0; i < m; i++) rd[e[i << 1 | d].v]++;     for (i = 1; i <= n; i++)       if (!rd[i]) q.push(i);     while (!q.empty()) {       u = q.front();       q.pop();       for (i = head[u]; i != -1; i = e[i].n)         if ((i & 1) == d) {           v = e[i].v;           rd[v]--;           if (!rd[v]) q.push(v);           if (d)             a[v].mi = max(a[v].mi, a[u].mi + 1);           else             a[v].mx = min(a[v].mx, a[u].mx - 1);         }     }     for (i = 1; i <= n; i++)       if (rd[i]) {         printf( -1 n );         exit(0);       }   }   bool cmpa(node aa, node bb) { return aa.mi < bb.mi; }   bool cmpi(node aa, node bb) { return aa.i < bb.i; }   void check() {     for (int i = 1; i <= n; i++)       if (a[i].mx < a[i].mi) {         printf( -1 n );         exit(0);       }   }   void solve() {     int i, j = 1;     priority_queue<node> q;     bool del;     for (i = 1; i <= k; i++) {       del = false;       for (; j <= n && a[j].mi == i; j++) {         node tmp = a[j];         tmp.i = j;         q.push(tmp);       }       while (!q.empty() && q.top().mx == i) {         node tmp = q.top();         q.pop();         a[tmp.i].c = i;         del = true;       }       if (!del) {         if (q.empty()) {           printf( -1 n );           exit(0);         }         node tmp = q.top();         q.pop();         a[tmp.i].c = i;       }     }   }   void print() {     for (int i = 1; i <= n; i++) printf( %d  , a[i].c);   }   int main() {     read();     tuopu(0);     tuopu(1);     sort(a + 1, a + n + 1, cmpa);     check();     solve();     sort(a + 1, a + n + 1, cmpi);     print();     return 0;   }


#include <bits/stdc++.h>   #pragma comment(linker,  /STACK:1024000000,1024000000 )   using namespace std;   int pre[1005], c[1005];   vector<int> a[1005];   int find(int x) { return pre[x] = (x == pre[x] ? x : find(pre[x])); }   void Union(int a, int b) {     if (a != b) pre[a] = b;   }   int main() {     int n, m, cnt = 0;     cin >> n >> m;     for (int i = 1; i <= n; i++) pre[i] = i;     for (int i = 1; i <= n; i++) {       int k, b;       cin >> c[i];       k = c[i];       if (k == 0) cnt++;       while (k--) {         cin >> b;         a[b].push_back(i);       }     }     for (int i = 1; i <= m; i++) {       for (int j = 1; j < a[i].size(); j++) {         Union(find(a[i][0]), find(a[i][j]));       }     }     int ans = 0;     for (int i = 1; i <= n; i++)       if (pre[i] == i && c[i]) ans++;     cout << max(0, ans - 1) + cnt << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   double calcC(int N, int M) {     if (M > N) return 0;     M = min(N - M, M);     double res = 1;     for (int i = 1; i <= M; ++i) res *= N - M + i, res /= i;     return res;   }   int main() {     int N, M;     while (cin >> N >> M) {       double result = 0;       for (int k = 1; k <= N; ++k) {         double tmp = 1;         tmp = calcC(M, k) * calcC(N * M - M, N - k) * k * k;         tmp /= calcC(N * M, N) * N * N;         result += tmp;       }       result *= N;       cout << setprecision(10) << result << endl;     }   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n, i, l, r, l1 = 0, r1 = 1e9, l2 = 0, r2 = 1e9;     cin >> n;     for (i = 0; i < n; i++) {       cin >> l >> r;       l1 = max(l1, l);       r1 = min(r1, r);     }     cin >> n;     for (i = 0; i < n; i++) {       cin >> l >> r;       l2 = max(l2, l);       r2 = min(r2, r);     }     cout << max(l1 - r2, max(l2 - r1, 0));     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n;   char A[200];   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     cin >> n;     for (int i = 1; i <= n; i++) cin >> A[i];     for (int i = 1; i <= n; i++) {       for (int j = 1; 4 * j <= n; j++) {         if (A[i] ==  * )           if (A[i] == A[i + j])             if (A[i + j] == A[i + 2 * j])               if (A[i + 2 * j] == A[i + 3 * j])                 if (A[i + 3 * j] == A[i + 4 * j]) {                   cout <<  yes ;                   return 0;                 }       }     }     cout <<  no ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     ios::sync_with_stdio(false);     cin.tie(0);     int n = 4;     vector<int> a(n);     for (int i = 0; i < n; i++) {       cin >> a[i];     }     if (max(a[2], a[3]) - 1 > min(a[2], a[3])) {       cout << -1;       return 0;     }     string ans =   ;     vector<int> cnt(2, 0);     if (a[2] > a[3]) {       for (int i = 0; i < a[2]; i++) {         ans +=  47 ;       }     } else {       if (a[2] == a[3]) {         if (a[0] > a[2]) {           for (int i = 0; i < a[2]; i++) {             ans +=  47 ;           }           ans +=  4 ;         } else {           if (a[1] > a[3]) {             ans +=  7 ;             for (int i = 0; i < a[2]; i++) {               ans +=  47 ;             }           } else {             cout << -1 <<   n ;             return 0;           }         }       } else {         for (int i = 0; i < a[3]; i++) {           ans +=  74 ;         }       }     }     cnt[0] = cnt[1] = 0;     for (char c : ans) {       if (c ==  4 ) cnt[0]++;       if (c ==  7 ) cnt[1]++;     }     if (cnt[0] > a[0] || cnt[1] > a[1]) {       cout << -1 <<   n ;       return 0;     }     a[0] -= cnt[0];     a[1] -= cnt[1];     for (int i = 0; i < (int)ans.size(); i++) {       if (ans[i] ==  4  && a[0] > 0) {         string nans =   ;         for (int j = 0; j <= i; j++) {           nans += ans[j];         }         for (int j = 0; j < a[0]; j++) {           nans +=  4 ;         }         for (int j = i + 1; j < (int)ans.size(); j++) {           nans += ans[j];         }         swap(ans, nans);         break;       }     }     for (int i = (int)ans.size() - 1; i >= 0; i--) {       if (ans[i] ==  7  && a[1] > 0) {         string nans =   ;         for (int j = 0; j <= i; j++) {           nans += ans[j];         }         for (int j = 0; j < a[1]; j++) {           nans +=  7 ;         }         for (int j = i + 1; j < (int)ans.size(); j++) {           nans += ans[j];         }         swap(ans, nans);         break;       }     }     cout << ans <<   n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     ios::sync_with_stdio(0);     long long n, q;     cin >> n >> q;     long long* m = (long long*)malloc((n + 2) * sizeof(long long));     for (int i = 0; i < n + 2; i++) {       m[i] = 0;     }     long long* num = (long long*)malloc(n * sizeof(long long));     for (int i = 0; i < n; i++) cin >> num[i];     while (q--) {       long long a, b;       cin >> a >> b;       b++;       m[a]++;       m[b]--;     }     for (int i = 1; i < n + 2; i++) m[i] += m[i - 1];     sort(num, num + n);     sort(m + 1, m + n + 1);     long long answer = 0;     for (int i = 0; i < n; i++) {       answer += num[i] * m[i + 1];     }     cout << answer << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   vector<pair<int, int> > days;   vector<int> visited;   int main() {     int k, n;     pair<int, int> temp;     cin >> n >> k;     for (int i = 0; i < n; i++) {       temp.first = i + 1;       cin >> temp.second;       days.push_back(temp);     }     for (int i = 0; i < (n - 1); i++) {       for (int j = 0; j < (n - i - 1); j++) {         if (days[j].second > days[j + 1].second) swap(days[j], days[j + 1]);       }     }     int total = 0;     for (int i = 0; i < n; i++) {       if ((total + days[i].second) <= k) {         total += days[i].second;         visited.push_back(days[i].first);       }     }     int sz = visited.size();     for (int i = 0; i < (sz - 1); i++) {       for (int j = 0; j < (sz - i - 1); j++) {         if (visited[j] > visited[j + 1]) swap(visited[j], visited[j + 1]);       }     }     cout << sz << endl;     for (int i = 0; i < sz; i++) cout << visited[i] <<    ;     cout << endl;   }


#include <bits/stdc++.h>   using namespace std;   int PASSED_POINT = 0;   void Duxar() {}   void read_int(int &value) {     int sign = 1;     char ch;     value = 0;     while (!isdigit(ch = getchar())) {       (ch ==  - ) && (sign = -1);     }     do {       value = value * 10 + (ch -  0 );     } while (isdigit(ch = getchar()));     value *= sign;   }   int N, M;   vector<int> hero, state;   vector<pair<int, int> > actionTeam;   int nr_bits(int x) {     int ans = 0;     while (x) {       ans += (x & 1);       x >>= 1;     }     return ans;   }   int main() {     int i, mask, act;     string s;     read_int(N);     hero.resize(N);     for (i = 0; i < N; ++i) {       read_int(hero[i]);     }     sort((hero).rbegin(), (hero).rend());     read_int(M);     state.resize((1 << M) - 1);     actionTeam.resize(M);     for (i = 0; i < M; ++i) {       cin >> s >> actionTeam[i].second;       actionTeam[i].first = 0;       if (s[0] ==  p ) actionTeam[i].first = 1;     }     state[(1 << M) - 1] = 0;     for (mask = (1 << M) - 2; mask >= 0; --mask) {       act = nr_bits(mask);       if (actionTeam[act].second == 1) {         state[mask] = -1000000007;         for (i = 0; i < M; ++i) {           if ((mask & (1 << i)) == 0) {             state[mask] = max(state[mask], state[mask | (1 << i)] +                                                actionTeam[act].first * hero[i]);           }         }       } else {         state[mask] = 1000000007;         for (i = 0; i < M; ++i) {           if ((mask & (1 << i)) == 0) {             state[mask] = min(state[mask], state[mask | (1 << i)] -                                                actionTeam[act].first * hero[i]);           }         }       }     }     printf( %d n , state[0]);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 8e5 + 5, B = 20;   char s[N];   int n, fa[N], len[N], sz[N], a[N], cc[N], np = 1, cnt = 1;   map<int, int> c[N];   int poi[N];   vector<int> e[N];   int st[B][N << 1], dfn[N], pos[N], tim;   int qq[N << 1], pre[N], za[N], zb[N], siz;   bool cmp(int a, int b) { return dfn[a] < dfn[b]; }   void dfs(int u) {     dfn[u] = ++tim;     pos[tim] = u;     st[0][tim] = dfn[u];     for (auto v : e[u]) dfs(v), st[0][++tim] = dfn[u];   }   void bui_st() {     for (int i = 1; i <= B - 1; ++i)       for (int j = 1; j + (1 << i) - 1 <= tim; ++j)         st[i][j] = min(st[i - 1][j], st[i - 1][j + (1 << i - 1)]);   }   int lca(int a, int b) {     if ((a = dfn[a]) > (b = dfn[b])) swap(a, b);     int d = floor(log2(b - a + 1));     return pos[min(st[d][a], st[d][b - (1 << d) + 1])];   }   void append(int x) {     int p = np;     np = ++cnt;     len[np] = len[p] + 1;     sz[np] = 1;     for (; p && !c[p][x]; p = fa[p]) c[p][x] = np;     if (!p)       fa[np] = 1;     else {       int q = c[p][x];       if (len[q] == len[p] + 1)         fa[np] = q;       else {         int nq = ++cnt;         len[nq] = len[p] + 1;         c[nq] = c[q];         fa[nq] = fa[q];         fa[q] = fa[np] = nq;         for (; c[p][x] == q; p = fa[p]) c[p][x] = nq;       }     }   }   void get_edge() {     for (int i = 1; i <= cnt; ++i) e[fa[i]].push_back(i);   }   void ini2() {     while (siz) za[qq[siz]] = zb[qq[siz]] = 0, --siz;     int la, lb, x;     scanf( %d%d , &la, &lb);     for (int i = 1; i <= la; ++i)       scanf( %d , &x), ++za[qq[++siz] = poi[n + 1 - x]];     for (int i = 1; i <= lb; ++i)       scanf( %d , &x), ++zb[qq[++siz] = poi[n + 1 - x]];     sort(qq + 1, qq + siz + 1, cmp);     for (int i = siz; i >= 2; --i) qq[++siz] = lca(qq[i], qq[i - 1]);     sort(qq + 1, qq + siz + 1, cmp);     siz = unique(qq + 1, qq + siz + 1) - qq - 1;     stack<int> Q;     Q.push(qq[1]);     pre[qq[1]] = 0;     for (int i = 2; i <= siz; ++i) {       for (int t; t = Q.top(), lca(t, qq[i]) != t;) Q.pop();       pre[qq[i]] = Q.top();       Q.push(qq[i]);     }   }   long long solve() {     long long re = 0;     for (int i = siz; i >= 1; --i) {       int x = qq[i];       re += 1ll * za[x] * zb[x] * (len[x] - len[pre[x]]);       za[pre[x]] += za[x];       zb[pre[x]] += zb[x];     }     return re;   }   int main() {     int q;     scanf( %d%d%s , &n, &q, s + 1);     reverse(s + 1, s + n + 1);     for (int i = 1; i <= n; ++i) append(s[i] -  a ), poi[i] = np;     get_edge();     dfs(1);     bui_st();     while (q--) {       ini2();       printf( %lld n , solve());     }   }


#include <bits/stdc++.h>   using namespace std;   inline int Max(int a, int b) { return (((b - a) >> (32 - 1)) & (a ^ b)) ^ b; }   inline int Min(int a, int b) { return (((b - a) >> (32 - 1)) & (a ^ b)) ^ a; }   string s;   long long num[2][1000005], len[2][1000005];   long long rnum[2][1000005];   int main() {     cin.tie(0);     ios::sync_with_stdio(false);     int n;     while (cin >> n) {       cin >> s;       memset(num, 0, sizeof(num));       memset(rnum, 0, sizeof(rnum));       memset(len, 0, sizeof(len));       for (int i = (1); i <= (n); i++) {         for (int j = (0); j <= (1); j++) {           num[j][i] = num[j][i - 1];           len[j][i] = len[j][i - 1];         }         if (s[i - 1] ==  U ) {           num[0][i]++;           len[0][i] += i;           rnum[0][num[0][i]] = i;         } else {           num[1][i]++;           len[1][i] += i;           rnum[1][num[1][i]] = i;         }       }       for (int i = (1); i <= (n); i++) {         long long ret = 0;         int a = num[1][n] - num[1][i];         int b = num[0][i - 1];         if (s[i - 1] ==  U ) {           if (a <= b) {             int ps = rnum[0][b - a];             ret = 2 * ((len[1][n] - len[1][i]) - (len[0][i - 1] - len[0][ps])) +                   n - i + 1;           } else {             int ps = rnum[1][num[1][i] + b + 1];             ret = 2 * ((len[1][ps] - len[1][i]) - len[0][i - 1]) - i;           }         } else {           if (a < b) {             int ps = rnum[0][b - a - 1];             ret = 2 * ((len[1][n] - len[1][i]) - (len[0][i - 1] - len[0][ps])) +                   n + i + 1;           } else {             int ps = rnum[1][num[1][i] + b];             ret = 2 * ((len[1][ps] - len[1][i]) - len[0][i - 1]) + i;           }         }         cout << ret <<    ;       }       cout << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long long int mod = 1e9 + 1;   const long long int mod2 = 1e9 + 9;   const long long int maxn = 1e6 + 1;   const int root = 1e7 + 5;   const long long inf = 1e18 + 18;   double pie4 = 3.1415926535;   long long int visit[maxn], a[maxn];   void die() {     cout <<  INFINITE ;     exit(0);   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     cout.tie(NULL);     long long int n, i, pos;     string s;     cin >> n;     cin >> s;     for (i = 0; i < n; i++) cin >> a[i];     pos = 0;     visit[0] = 0;     while (1) {       if (s[pos] ==  > ) {         pos += a[pos];         if (pos >= n || pos < 0) {           cout <<  FINITE ;           return 0;         }         if (visit[pos]) die();         visit[pos] = 1;       } else {         pos -= a[pos];         if (pos >= n || pos < 0) {           cout <<  FINITE ;           return 0;         }         if (visit[pos]) die();         visit[pos] = 1;       }       if (pos >= n || pos < 0) {         cout <<  FINITE ;         return 0;       }     }   }


#include <bits/stdc++.h>   using namespace std;   void solve() {     long long a, b, c;     cin >> a >> b >> c;     long long flag = 0, temp;     temp = b - a % b;     for (long long i = temp; (i + a) <= c; i += b) {       cout << i <<    ;       flag = 1;     }     if (!flag) cout <<  -1 ;   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     solve();   }


#include <bits/stdc++.h>   using namespace std;   using ll = long long int;   ll ara[300005], ans_ara[300005];   set<ll> st;   vector<pair<ll, ll> > vv;   int main() {     ll n, k, i, j, ans = 0;     scanf( %I64d %I64d , &n, &k);     for (i = 1; i <= n; i++) {       scanf( %I64d , &ara[i]);       ans -= (ara[i] * i);       st.insert(i + k);       vv.push_back({ara[i], i});     }     sort(vv.rbegin(), vv.rend());     for (pair<ll, ll> u : vv) {       ll num = *st.lower_bound(u.second);       ans += (num * u.first);       st.erase(st.lower_bound(u.second));       ans_ara[u.second] = num;     }     printf( %I64d n , ans);     for (i = 1; i <= n; i++) {       printf( %I64d  , ans_ara[i]);     }     printf(  n );     return 0;   }


#include <bits/stdc++.h>   int Abs(int x) { return x < 0 ? -x : x; }   int main() {     int i, N, ans = 0, sum = 0, c = 0, d = 0;     double p;     scanf( %d , &N);     for (i = 0; i < 2 * N; i++) {       scanf( %lf , &p);       int t = ((int)(p * 1000 + 0.1)) % 1000;       if (!t) c++;       sum += t;     }     ans = 1000 * N - sum;     int q = Abs(ans);     for (i = 1; i <= c; i++) {       if (q > Abs(ans - i * 1000)) q = Abs(ans - i * 1000);     }     printf( %d.%03d n , q / 1000, q % 1000);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     vector<int> v(4);     for (int i = 0; i < 4; i++) {       cin >> v[i];     }     if (v[3] > v[2] * 2) {       cout <<  -1 ;       return 0;     }     if (v[2] > v[3] * 2) {       cout <<  -1 ;       return 0;     }     vector<int> s(3);     for (int i = 0; i < 2; i++) {       if (v[i] == v[3]) {         cout <<  -1 ;         return 0;       }       if (v[i] < v[3])         s[i] = v[i];       else         s[i] = v[i] * 2;     }     if (v[2] > v[3])       s[2] = v[2];     else       s[2] = v[3];     for (int i = 0; i < 3; i++) {       cout << s[i] << endl;     }     return 0;   }


#include <bits/stdc++.h>   struct Edge {     int u, v, next;   } edge[100000];   int head[105], num[105];   bool visited[105];   int en, cnt, tt;   void add_edge(int u, int v) {     edge[en].u = u;     edge[en].v = v;     edge[en].next = head[u];     head[u] = en++;   }   void DFS1(int u) {     int v;     visited[u] = true;     for (int id = head[u]; id != -1; id = edge[id].next) {       v = edge[id].v;       if (!visited[v]) DFS1(v);     }   }   void DFS2(int u, int p) {     int v;     num[u] = tt++;     visited[u] = true;     for (int id = head[u]; id != -1; id = edge[id].next) {       v = edge[id].v;       if (v == p) continue;       if (!visited[v])         DFS2(v, u);       else if (num[v] < num[u])         cnt++;     }   }   int main() {     int n, m, u, v;     en = 0;     memset(head, 0xff, sizeof head);     scanf( %d%d , &n, &m);     for (int i = 0; i < m; i++) {       scanf( %d%d , &u, &v);       add_edge(u, v);       add_edge(v, u);     }     memset(visited, false, sizeof visited);     DFS1(1);     cnt = 0;     for (int i = 1; i <= n; i++)       if (!visited[i]) cnt++;     if (cnt)       puts( NO );     else {       memset(visited, false, sizeof visited);       cnt = tt = 0;       DFS2(1, -1);       if (cnt == 1)         puts( FHTAGN! );       else         puts( NO );     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     long long n, k;     cin >> n >> k;     vector<long long> a(n);     for (long long i = 0; i < n; i++) cin >> a[i];     vector<long long> q(n);     for (long long i = 0; i < k; i++) q[k - 1] += a[i];     for (long long i = k; i < n; i++) q[i] = q[i - 1] + a[i] - a[i - k];     vector<pair<long long, long long>> left(n);     left[0] = {q[0], 0};     for (long long i = 1; i < n; i++) {       if (q[i] > left[i - 1].first)         left[i] = {q[i], i};       else         left[i] = left[i - 1];     }     long long ans = 0, l = 0, r = 0;     for (long long i = 2 * k - 1; i < n; i++) {       long long first_ans = q[i];       pair<long long, long long> second_ans = left[i - k];       if (first_ans + second_ans.first > ans) {         ans = first_ans + second_ans.first;         l = second_ans.second;         r = i;       }     }     l++, l++, r++, r++;     l -= k, r -= k;     cout << l <<     << r;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     long long int i, j, k, n, m, r, s;     cin >> n;     if (n % 2 == 0) {       m = n / 2;     } else {       m = n - 1;       m = m / 2 - n;     }     cout << m << endl;   }


#include <bits/stdc++.h>   using namespace std;   void fun() {     int n, k;     cin >> n >> k;     map<string, int> m;     string s;     string a[n];     for (int i = 0; i < n; i++) {       cin >> s;       a[i] = s;       m[s]++;     }     int c = 0;     for (int i = 0; i < n; i++)       for (int j = i + 1; j < n; j++) {         s =   ;         for (int p = 0; p < k; p++) {           if (a[i][p] == a[j][p])             s += a[i][p];           else             s += (char)( S  +  E  +  T  - a[i][p] - a[j][p]);         }         c += m[s];       }     cout << c / 3 << endl;   }   int main() {     { fun(); }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int maxx = 600010;   const double pi = acos(-1.0);   const double eps = 1e-15;   stack<int> h;   vector<int> g[maxx];   int t, n, m;   int ans = 0, total = 0, sum = 0, cnt = 0;   int a[maxx], bj[maxx], vis[maxx], dfn[maxx], low[maxx];   char ch[3111];   char c[3111][3111], s[3111][3111];   void tarjan(int x, int father) {     dfn[x] = low[x] = ++cnt, vis[x] = 1, h.push(x);     for (int i = 0; i < g[x].size(); i++) {       int to = g[x][i];       if (to == father) continue;       if (!dfn[to]) {         tarjan(to, x);         low[x] = min(low[x], low[to]);       } else if (vis[to])         low[x] = min(low[x], dfn[to]);     }     if (low[x] == dfn[x]) {       bj[x] = ++total, vis[x] = 0;       while (h.top() != x) {         bj[h.top()] = total, vis[h.top()] = 0, h.pop();       }       h.pop();     }   }   int main() {     scanf( %d , &n);     for (int i = 1; i <= n; i++)       for (int j = 1; j <= n; j++) scanf(  %c , &c[i][j]);     for (int i = 1; i <= n; i++)       for (int j = 1; j <= n; j++) scanf(  %c , &s[i][j]);     for (int i = 1; i <= n; i++) scanf(  %c , &ch[i]);     for (int i = 1; i <= n; i++)       for (int j = 1; j <= n; j++) {         if (c[i][j] ==  0 ) {           if (s[i][j] ==  1 ) {             if (ch[j] ==  0  && ch[i] ==  0 ) {               ans = -1;               i = n + 1;               break;             }             if (ch[j] ==  0  && ch[i] ==  1 ) g[j + 3 * n].push_back(j + n);             if (ch[j] ==  1  && ch[i] ==  0 ) g[i + 2 * n].push_back(i);             if (ch[j] ==  1  && ch[i] ==  1 )               g[i].push_back(j + 3 * n), g[j + 3 * n].push_back(i),                   g[i + 2 * n].push_back(j + n), g[j + n].push_back(i + 2 * n);           } else {             if (ch[j] ==  0  && ch[i] ==  1 ) g[j + n].push_back(j + 3 * n);             if (ch[j] ==  1  && ch[i] ==  0 ) g[i].push_back(i + 2 * n);             if (ch[j] ==  1  && ch[i] ==  1 )               g[i].push_back(j + n), g[j + n].push_back(i),                   g[i + 2 * n].push_back(j + 3 * n),                   g[j + 3 * n].push_back(i + 2 * n);           }         } else {           if (s[i][j] ==  1 ) {             if (ch[j] ==  0  && ch[i] ==  1 ) g[j + n].push_back(j + 3 * n);             if (ch[j] ==  1  && ch[i] ==  0 ) g[i].push_back(i + 2 * n);             if (ch[j] ==  1  && ch[i] ==  1 )               g[i].push_back(j + n), g[i + 2 * n].push_back(j + 3 * n),                   g[j + n].push_back(i), g[j + 3 * n].push_back(i + 2 * n);           } else {             if (ch[j] ==  0  && ch[i] ==  0 ) {               i = n + 1;               ans = -1;               break;             }             if (ch[j] ==  0  && ch[i] ==  1 ) g[j + 3 * n].push_back(j + n);             if (ch[j] ==  1  && ch[i] ==  0 ) g[i + 2 * n].push_back(i);             if (ch[j] ==  1  && ch[i] ==  1 )               g[j + n].push_back(i + 2 * n), g[i].push_back(j + 3 * n),                   g[i + 2 * n].push_back(j + n), g[j + 3 * n].push_back(i);           }         }       }     if (ans)       printf( %d , ans);     else {       for (int i = 1; i <= 4 * n; i++)         if (!dfn[i]) tarjan(i, i);       for (int i = 1; i <= 2 * n; i++)         if (bj[i] == bj[i + 2 * n]) {           ans = 1;           break;         }       if (ans)         printf( -1 );       else {         for (int i = 1; i <= n; i++)           if (bj[i] < bj[i + 2 * n]) ans++;         for (int i = n + 1; i <= 2 * n; i++)           if (bj[i] < bj[i + 2 * n]) ans++;         printf( %d n , ans);         for (int i = 1; i <= n; i++)           if (bj[i] < bj[i + 2 * n]) printf( row %d n , i - 1);         for (int i = n + 1; i <= 2 * n; i++)           if (bj[i] < bj[i + 2 * n]) printf( col %d n , i - n - 1);       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   signed main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     long long t;     cin >> t;     vector<long long> v;     long long h = 1, m;     while (h <= 25820) {       m = ((3 * h + 1) * h) / 2;       v.push_back(m);       h++;     }     for (long long j = 0; j < t; j++) {       long long n, pos = 0;       cin >> n;       for (long long i = 0; i < v.size(); i++) {         if (n <= v[i]) {           pos = i;           break;         }       }       long long ans = 0;       while (n > 0 && pos >= 0) {         ans += (n / v[pos]);         n %= v[pos];         pos--;       }       cout << ans <<   n ;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int MaxN = 111;   int op[123456], val[123456], b[123456];   vector<int> x;   vector<int>::iterator it;   long long sum[333][5];   int k[333], sumk;   map<int, int> ma;   char s[5];   int main(void) {     int i, j, n;     int no, ni;     long long res;     scanf( %d , &n);     x.clear();     for (i = 0; i < n; i++) {       scanf(  %s , s);       if (s[0] ==  a )         op[i] = 1;       else if (s[0] ==  d )         op[i] = 2;       else         op[i] = 3;       if (op[i] < 3) {         scanf( %d , &val[i]);         x.push_back(val[i]);       }     }     sort(x.begin(), x.end());     it = unique(x.begin(), x.end());     x.resize(it - x.begin());     for (i = 0; i < x.size(); i++) ma[x[i]] = i;     memset(sum, 0, sizeof(sum));     memset(k, 0, sizeof(k));     memset(b, 0, sizeof(b));     for (i = 0; i < n; i++) {       if (op[i] == 1) {         b[ma[val[i]]]++;         no = ma[val[i]] / 320;         k[no]++;         for (j = 0; j < 5; j++) sum[no][j] = 0;         j = 0;         for (ni = 0; ni < 320; ni++) {           if (b[no * 320 + ni]) {             sum[no][j] = sum[no][j] + x[no * 320 + ni];             j++;             if (j == 5) j = 0;           }         }       } else if (op[i] == 2) {         b[ma[val[i]]]--;         no = ma[val[i]] / 320;         k[no]--;         for (j = 0; j < 5; j++) sum[no][j] = 0;         j = 0;         for (ni = 0; ni < 320; ni++) {           if (b[no * 320 + ni]) {             sum[no][j] = sum[no][j] + x[no * 320 + ni];             j++;             if (j == 5) j = 0;           }         }       } else if (op[i] == 3) {         res = 0;         sumk = 2;         for (no = 0; no < 320; no++) {           res += sum[no][4 - sumk % 5];           sumk += k[no];         }         printf( %I64d n , res);       }     }     return 0;   }


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


#include <bits/stdc++.h>   using namespace std;   long long a[1000000];   int main() {     long long i, num, res = 0, n, k, rem, fl = 0;     cin >> n >> k;     for (i = 0; i < n; i++) {       cin >> a[i];     }     sort(a, a + n);     for (i = 1; i < n; i++) {       if ((a[i] - a[0]) % k != 0) {         fl = 1;         break;       } else {         rem = (a[i] - a[0]) / k;         res += rem;       }     }     if (fl) {       cout << -1 << endl;     } else       cout << res << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n, a[101], piller;   int main() {     cin >> n;     for (auto i = 0; i < n; i++) cin >> a[i];     sort(a, a + n);     for (auto i = 0; i < n; i++) {       if (piller * a[i] + piller <= i) piller++;     }     cout << piller;   }


#include <bits/stdc++.h>   using namespace std;   int t[3000];   int main() {     int n, k;     cin >> n >> k;     string s;     cin >> s;     for (int i = 0; i < n; i++) {       t[s[i]] += 1;     }     int lk = t[ A ];     for (char c =  A ; c <  A  + k; c++) {       if (t[c] < lk) {         lk = t[c];       }     }     cout << lk * k;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 2e5 + 5;   const int mod = 1e9 + 7;   struct SAM {     int n, tot, last;     int ch[N][26], dep[N], fa[N];     int root[N], ls[N * 20], rs[N * 20], cnt;     vector<int> G[N];     void init() {       tot = last = 1;       memset(ch[1], 0, sizeof(ch[1]));     }     inline int newnode(int _dep) {       dep[++tot] = _dep;       memset(ch[tot], 0, sizeof(ch[tot]));       return tot;     }     void add(int alp) {       ++n;       alp -=  a ;       int u, p = newnode(dep[last] + 1);       for (u = last; u && !ch[u][alp]; u = fa[u]) ch[u][alp] = p;       if (!u)         fa[p] = 1;       else {         int v = ch[u][alp];         if (dep[v] == dep[u] + 1)           fa[p] = v;         else {           int nv = newnode(dep[u] + 1);           fa[nv] = fa[v];           fa[v] = fa[p] = nv;           memcpy(ch[nv], ch[v], sizeof(ch[v]));           for (; u && ch[u][alp] == v; u = fa[u]) ch[u][alp] = nv;         }       }       last = p;     }     int build(int l, int r, int p) {       if (l == r) return ++cnt;       int mid = l + r >> 1;       int now = ++cnt;       if (p <= mid)         ls[now] = build(l, mid, p);       else         rs[now] = build(mid + 1, r, p);       return now;     }     int merge(int x, int y) {       if (x == 0 || y == 0) return x + y;       int now = ++cnt;       ls[now] = merge(ls[x], ls[y]);       rs[now] = merge(rs[x], rs[y]);       return now;     }     void dfs(int x) {       for (int i = 0; i < G[x].size(); ++i) {         int u = G[x][i];         dfs(u);         root[x] = merge(root[x], root[u]);       }     }     void build_segtree(char* s) {       int now = 1;       int n = strlen(s + 1);       for (int i = 1; i <= n; ++i) {         now = ch[now][s[i] -  a ];         root[now] = build(1, n, i);       }       for (int i = 1; i <= tot; ++i) G[fa[i]].push_back(i);       dfs(1);     }     string ans;     bool chk(int k, int l, int r, int p, int q) {       if (k == 0) return false;       if (l >= p && r <= q) return true;       if (l > q || r < p) return false;       int mid = l + r >> 1;       return chk(ls[k], l, mid, p, q) || chk(rs[k], mid + 1, r, p, q);     }     bool check(int l, int r, int p) { return chk(p, 1, n, l, r); }     bool solve(int now, int l, int r, char* s) {       if (l > r) return false;       if (*s == 0) {         for (int i = 0; i < 26; i++) {           if (ch[now][i] && check(l, r, root[ch[now][i]])) {             ans.push_back( a  + i);             return true;           }         }         return false;       } else {         int i = *s -  a ;         if (ch[now][i] && check(l, r, root[ch[now][i]]) &&             solve(ch[now][i], l + 1, r, s + 1)) {           ans.push_back( a  + i);           return true;         }         for (i = i + 1; i < 26; ++i) {           if (ch[now][i] && check(l, r, root[ch[now][i]])) {             ans.push_back( a  + i);             return true;           }         }         return false;       }     }   } sam;   int n, m;   char s[N];   int main() {     sam.init();     scanf( %s , s + 1);     n = strlen(s + 1);     for (int i = 1; i <= n; i++) sam.add(s[i]);     sam.build_segtree(s);     scanf( %d , &m);     while (m--) {       int l, r;       scanf( %d %d %s , &l, &r, s + 1);       sam.ans.clear();       if (!sam.solve(1, l, r, s + 1))         printf( -1 n );       else {         reverse(sam.ans.begin(), sam.ans.end());         printf( %s n , sam.ans.c_str());       }     }   }


#include <bits/stdc++.h>   using namespace std;   const int N = 4e5 + 5;   int n, q, l[N], r[N], level[N], p[N][20];   pair<int, int> res[N];   int import[N], numCity, numVirus, node[N], total_num;   vector<int> adj[N], newAdj[N];   struct T {     int city, speed;   };   T virus[N];   int cnt = 0;   void dfs(int x, int pa) {     cnt++;     l[x] = cnt;     p[x][0] = pa;     for (auto &u : adj[x]) {       if (u == pa) continue;       level[u] = level[x] + 1;       dfs(u, x);     }     r[x] = cnt;   }   void init_lca() {     for (int k = 1; k <= 18; k++) {       for (int i = 1; i <= n; i++) p[i][k] = p[p[i][k - 1]][k - 1];     }   }   int lca(int x, int y) {     if (x == 1 || y == 1) return 1;     if (level[x] > level[y]) swap(x, y);     int diff = level[y] - level[x];     for (int k = 18; k >= 0; k--)       if (diff >> k & 1) y = p[y][k];     if (x == y) return x;     for (int k = 18; k >= 0; k--) {       if (p[x][k] != p[y][k]) {         x = p[x][k];         y = p[y][k];       }     }     return p[x][0];   }   bool isParent(int p, int u) { return (l[p] <= l[u] && l[u] <= r[p]); }   bool lf(const int &x, const int &y) { return l[x] < l[y]; }   int dist(int x, int y) { return level[x] + level[y] - 2 * level[lca(x, y)]; }   void compress_tree() {     total_num = numCity + numVirus;     for (int i = 1; i <= numVirus; i++) node[i] = virus[i].city;     for (int i = numVirus + 1; i <= total_num; i++)       node[i] = import[i - numVirus];     sort(node + 1, node + total_num + 1, lf);     total_num = unique(node + 1, node + total_num + 1) - node - 1;     int now = total_num;     for (int i = 1; i < now; i++) {       total_num++;       node[total_num] = lca(node[i], node[i + 1]);     }     sort(node + 1, node + total_num + 1, lf);     total_num = unique(node + 1, node + total_num + 1) - node - 1;     for (int i = 1; i <= total_num; i++) newAdj[node[i]].clear();     stack<int> st;     st.push(node[1]);     for (int i = 2; i <= total_num; i++) {       while (!st.empty() && !isParent(st.top(), node[i])) st.pop();       if (!st.empty()) {         int par = st.top();         newAdj[par].push_back(node[i]);         newAdj[node[i]].push_back(par);       }       st.push(node[i]);     }   }   int calc(int dist, int s) { return (dist - 1 + s) / s; }   void solve() {     for (int i = 1; i <= total_num; i++) res[node[i]] = make_pair(1e9, 0);     priority_queue<pair<pair<int, int>, int>, vector<pair<pair<int, int>, int>>,                    greater<pair<pair<int, int>, int>>>         pq;     for (int i = 1; i <= numVirus; i++) {       int id = virus[i].city;       res[id] = make_pair(0, i);       pq.push(make_pair(res[id], id));     }     while (!pq.empty()) {       auto now = pq.top();       pq.pop();       if (res[now.second] != now.first) continue;       for (auto &u : newAdj[now.second]) {         if (res[u] > make_pair(calc(dist(u, virus[now.first.second].city),                                     virus[now.first.second].speed),                                now.first.second)) {           res[u] = make_pair(calc(dist(u, virus[now.first.second].city),                                   virus[now.first.second].speed),                              now.first.second);           pq.push(make_pair(res[u], u));         }       }     }     for (int i = 1; i <= numCity; i++) cout << res[import[i]].second <<    ;     cout <<   n ;   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(nullptr);     cin >> n;     for (int i = 1; i < n; i++) {       int u, v;       cin >> u >> v;       adj[u].push_back(v);       adj[v].push_back(u);     }     dfs(1, 0);     init_lca();     cin >> q;     while (q--) {       cin >> numVirus >> numCity;       for (int i = 1; i <= numVirus; i++) cin >> virus[i].city >> virus[i].speed;       for (int i = 1; i <= numCity; i++) cin >> import[i];       compress_tree();       solve();     }   }


#include <bits/stdc++.h>   using namespace std;   long long n, A[100005];   int main() {     ios_base::sync_with_stdio(false);     cin >> n;     for (int i = 0; i < n; i++) cin >> A[i];     sort(A, A + n);     int mid = n / 2;     long long res = 0;     for (int i = 0; i < mid; i++) {       res = (res + A[i] * A[n - 1 - i] * 2) % 10007;     }     if (n & 1) res = (res + A[mid] * A[mid]) % 10007;     cout << res <<   n ;   }


#include <bits/stdc++.h>   using namespace std;   struct node {     int x, y, next;   } a[1010000];   int len, last[510000];   inline void ins(int x, int y) {     len++;     a[len].x = x;     a[len].y = y;     a[len].next = last[x];     last[x] = len;   }   int tt[510000];   int col[510000];   int cnt, belong[510000];   bool dfs(int x, int c) {     if (col[x] != -1) return col[x] == c;     col[x] = c;     belong[x] = cnt;     for (int k = last[x]; k > 0; k = a[k].next) {       int y = a[k].y;       if (tt[x] != tt[y]) continue;       if (!dfs(y, 1 - c)) return false;     }     return true;   }   bool b[510000];   int tg[510000];   struct edge {     int x, y, c, next;     bool bk;   } e[1010000];   map<int, map<int, int>> east;   inline void ins(int x, int y, int c) {     len++;     e[len].x = x;     e[len].y = y;     e[len].c = c;     e[len].next = east[x][tg[y]];     east[x][tg[y]] = len;     e[len].bk = true;   }   int Al, Be, Ga;   bool De;   void color(int x, int c) {     if (col[x] < Ga) {       col[x] = c;       int ty = tg[x] == Al ? Be : Al;       for (int k = east[x][ty]; k > 0; k = e[k].next) {         int y = e[k].y;         e[k].bk = false;         color(y, c ^ e[k].c);       }     } else if (col[x] == (c ^ 1))       De = false;   }   set<pair<int, int>> S;   int main() {     int n, m, k;     scanf( %d%d%d , &n, &m, &k);     for (int i = 1; i <= n; i++) scanf( %d , &tt[i]);     len = 0;     memset(last, 0, sizeof(last));     for (int i = 1; i <= m; i++) {       int x, y;       scanf( %d%d , &x, &y);       ins(x, y);       ins(y, x);     }     memset(col, -1, sizeof(col));     memset(b, true, sizeof(b));     cnt = 0;     for (int i = 1; i <= n; i++) {       if (col[i] == -1 && b[tt[i]]) {         cnt++;         if (!dfs(i, 0)) {           k--;           b[tt[i]] = false;         }       }     }     for (int i = 1; i <= n; i++) tg[belong[i]] = tt[i];     len = 0;     for (int i = 1; i <= m; i++) {       int x = a[i * 2].x, y = a[i * 2].y;       if (b[tt[x]] && b[tt[y]] && tt[x] != tt[y]) {         ins(belong[x], belong[y], col[x] ^ col[y]);         ins(belong[y], belong[x], col[x] ^ col[y]);       }     }     memset(col, -1, sizeof(col));     Ga = 0;     for (int i = 1; i <= len; i += 2) {       int x = e[i].x, y = e[i].y;       if (tg[x] > tg[y]) x ^= y ^= x ^= y;       if (e[i].bk) {         Al = tg[x];         Be = tg[y];         De = true;         color(x, Ga);         if (!De) S.insert({tg[x], tg[y]});         Ga += 2;       }     }     printf( %lld n , 1ll * k * (k - 1) / 2 - S.size());     return 0;   }


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


#include <bits/stdc++.h>   using namespace std;   template <class T>   void print(T &x) {     cout << x;   }   template <class T, class U>   void print(pair<T, U> &p) {     cout <<  ( ;     print(p.first);     cout <<  ,  ;     print(p.second);     cout <<  ) ;   }   template <class T>   void print(vector<T> &v) {     cout <<  { ;     if (((int)(v).size())) print(v[0]);     for (int i = 1; i < ((int)(v).size()); i++) cout <<  ,  , print(v[i]);     cout <<  } n ;   }   const int n = 5e5 + 5;   int N, M, K, T;   vector<long long> A;   int cnt(vector<long long> A, int b) {     if (((int)(A).size()) == 1) return 1;     if (((int)(A).size()) == 0) return 0;     vector<long long> B;     while (!A.empty() && A[((int)(A).size()) - 1] >= 1LL << b) {       long long v = A[((int)(A).size()) - 1];       A.pop_back();       B.push_back(v ^ (1LL << b));     }     int szA = ((int)(A).size());     int t1 = cnt(A, b - 1) + (((int)(B).size()) > 0 ? 1 : 0);     sort(B.begin(), B.end());     int t2 = cnt(B, b - 1) + (szA > 0 ? 1 : 0);     return max(t1, t2);   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     cin >> N;     A.resize(N);     for (long long &a : A) cin >> a;     sort(A.begin(), A.end());     int c = cnt(A, 31);     cout << N - c << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int maxn = 2e5 + 50;   const int inf = 0x3f3f3f3f;   int n, mod;   long long ans;   int head[maxn], nxt[maxn << 1], to[maxn << 1], cost[maxn << 1], edge_tot;   void add_edge(int u, int v, int c) {     nxt[++edge_tot] = head[u];     head[u] = edge_tot;     to[edge_tot] = v;     cost[edge_tot] = c;   }   void change(long long &x, int mod) { x = (x % mod + mod) % mod; }   void ext_gcd(long long a, long long b, long long &d, long long &x,                long long &y) {     if (!b) {       d = a;       x = 1;       y = 0;     } else {       ext_gcd(b, a % b, d, y, x);       y -= x * (a / b);     }   }   long long fac[maxn], inv[maxn];   void pre() {     fac[0] = 1;     for (int i = 1; i < maxn; i++) fac[i] = fac[i - 1] * 10 % mod;     for (int i = 0; i < maxn; i++) {       long long a = fac[i], b = mod;       long long d, x, y;       ext_gcd(a, b, d, x, y);       change(x, mod);       inv[i] = x;     }   }   int root;   bool vis[maxn];   int siz[maxn], mxson[maxn];   void ndc_init() { mxson[0] = inf; }   void get_root(int u, int f, int sum) {     siz[u] = 1;     mxson[u] = 0;     for (int i = head[u]; i; i = nxt[i]) {       int v = to[i];       if (vis[v] || v == f) continue;       get_root(v, u, sum);       siz[u] += siz[v];       if (mxson[u] < siz[v]) mxson[u] = siz[v];     }     if (mxson[u] < sum - siz[u]) mxson[u] = sum - siz[u];     if (mxson[u] < mxson[root]) root = u;   }   map<long long, int> mp;   void add1(int u, int f, register long long sum, int dep, int op) {     mp[sum] += op;     for (int i = head[u]; i; i = nxt[i]) {       int v = to[i];       if (vis[v] || v == f) continue;       add1(v, u, (sum + fac[dep] * cost[i] % mod) % mod, dep + 1, op);     }   }   void calc(int u, int f, register long long sum, int dep) {     if (!sum) ans++;     long long fd = -inv[dep] * sum;     change(fd, mod);     if (mp.count(fd)) ans += mp[fd];     for (int i = head[u]; i; i = nxt[i]) {       int v = to[i];       if (vis[v] || v == f) continue;       calc(v, u, (sum * 10 + cost[i]) % mod, dep + 1);     }   }   void solve(int u = 1, int sum = n) {     root = 0;     get_root(u, 0, sum);     vis[root] = 1;     for (int i = head[root]; i; i = nxt[i]) {       int v = to[i];       if (vis[v]) continue;       add1(v, root, cost[i] % mod, 1, 1);     }     if (mp.count(0)) ans += mp[0];     for (int i = head[root]; i; i = nxt[i]) {       int v = to[i];       if (vis[v]) continue;       add1(v, root, cost[i] % mod, 1, -1);       calc(v, root, cost[i] % mod, 1);       add1(v, root, cost[i] % mod, 1, 1);     }     mp.clear();     int root_sz = siz[root];     for (int i = head[root]; i; i = nxt[i]) {       int v = to[i];       if (vis[v]) continue;       solve(v, siz[v]);     }   }   int main() {     ans = 0;     scanf( %d%d , &n, &mod);     for (int i = 1; i < n; i++) {       int u, v, c;       scanf( %d%d%d , &u, &v, &c);       u++, v++;       add_edge(u, v, c);       add_edge(v, u, c);     }     pre();     ndc_init();     solve();     printf( %lld n , ans);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long dp[41];   void prec() {     dp[0] = 0;     long long x = 1;     for (int i = int(1); i < int(41); i++) {       dp[i] = 2 * dp[i - 1] + x;       x = x << 1;     }   }   int main(int argc, const char* argv[]) {     long long n;     cin >> n;     prec();     n--;     long long ans = 0;     long long original = n;     while (n) {       long long r = n & (-n);       ans += dp[(int)log2(r)];       n -= r;     }     long long i = 1;     while (i <= original) {       ans += i & original;       i = i << 1;     }     cout << ans << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n, a[100001], i;     set<int> mysetp, mysetn, left, right;     set<int>::iterator l, r, p;     scanf( %d , &n);     scanf( %d , &a[1]);     mysetn.insert(-a[1]);     mysetp.insert(a[1]);     for (i = 2; i <= n; i++) {       scanf( %d , &a[i]);       l = mysetn.lower_bound(-a[i]);       r = mysetp.lower_bound(a[i]);       if (*l > 0 || -(*l) > a[i]) {         left.insert(*r);         printf( %d n , *r);       } else if (*r < a[i]) {         printf( %d n , -(*l));       } else if (-(*l) == *r) {         if (*r > a[i]) left.insert(*r);         printf( %d n , *r);       } else {         p = left.lower_bound(*r);         if (*p == *r) {           printf( %d n , -(*l));         } else {           left.insert(*r);           printf( %d n , *r);         }       }       mysetp.insert(a[i]);       mysetn.insert(-a[i]);     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   using DB = double;   using LL = long long;   using PII = pair<int, int>;   void err() { puts(  ); }   template <template <typename...> class T, typename t, typename... Args>   void err(T<t> a, Args... args) {     for (auto x : a) cout << x <<    ;     err(args...);   }   template <typename T, typename... Args>   void err(T a, Args... args) {     cout << a <<    ;     err(args...);   }   const int N = 2e5 + 10;   char s[N], t[N];   void work() {     int n, m;     scanf( %d%d%s%s , &n, &m, s, t);     int id = -1;     for (int i = 0; i < n; i++)       if (s[i] ==  * ) id = i;     if (id == -1) {       printf( %s n , strcmp(s, t) == 0 ?  YES  :  NO );     } else {       bool flag = 0;       if (n - 1 <= m) {         flag = 1;         for (int i = 0; i < id; i++) {           if (s[i] != t[i]) flag = 0;         }         for (int i = n - 1, j = m - 1; i > id; i--, j--) {           if (s[i] != t[j]) flag = 0;         }       }       printf( %s n , flag ?  YES  :  NO );     }   }   int main() {     work();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 1000010;   const long long inf = 1000000000000000000LL;   int a[N];   long long prefix_cost[N], cost[N], suffix_cost[N];   set<int> factors;   set<int>::iterator it;   void pf(int x) {     for (int j = x - 1; j <= x + 1; j++) {       int d = j;       for (int i = 2; i * i <= j; i++) {         if (d % i == 0) {           factors.insert(i);           while (d % i == 0) d /= i;         }       }       if (d != 1) factors.insert(d);     }   }   int main() {     int n, p, A, B;     scanf( %d %d %d , &n, &A, &B);     for (int i = 1; i <= n; i++) scanf( %d , &a[i]);     if (n == 1) {       printf( 0 n );       return 0;     }     pf(a[1]);     pf(a[n]);     long long ans = inf;     for (it = factors.begin(); it != factors.end(); ++it) {       prefix_cost[0] = 0LL;       p = *it;       for (int i = 1; i <= n; i++) {         if (a[i] % p == 0)           cost[i] = 0LL;         else if ((a[i] + 1) % p == 0 || (a[i] - 1) % p == 0)           cost[i] = (long long)B;         else           cost[i] = inf;         prefix_cost[i] = prefix_cost[i - 1] + cost[i];         if (prefix_cost[i] > inf) prefix_cost[i] = inf;       }       prefix_cost[0] = inf;       suffix_cost[n + 1] = 0LL;       long long suffix_sum = 0LL;       for (int i = n; i > 0; i--) {         suffix_sum += cost[i];         if (suffix_sum > inf) suffix_sum = inf;         suffix_cost[i] = min(A + suffix_cost[i + 1], suffix_sum);       }       ans = min(ans, suffix_cost[1]);       for (int i = 2; i <= n; i++)         ans = min(ans, prefix_cost[i - 1] + suffix_cost[i]);     }     printf( %I64d n , ans);     return 0;   }


#include <bits/stdc++.h>   #pragma GCC optimize( O3 )   using namespace std;   struct custom_hash {     static uint64_t splitmix64(uint64_t x) {       x += 0x9e3779b97f4a7c15;       x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;       x = (x ^ (x >> 27)) * 0x94d049bb133111eb;       return x ^ (x >> 31);     }     size_t operator()(uint64_t x) const {       static const uint64_t FIXED_RANDOM =           chrono::steady_clock::now().time_since_epoch().count();       return splitmix64(x + FIXED_RANDOM);     }   };   vector<string> vec_splitter(string s) {     s +=  , ;     vector<string> res;     while (!s.empty()) {       res.push_back(s.substr(0, s.find( , )));       s = s.substr(s.find( , ) + 1);     }     return res;   }   void debug_out(vector<string> __attribute__((unused)) args,                  __attribute__((unused)) int idx,                  __attribute__((unused)) int LINE_NUM) {     cerr << endl;   }   template <typename Head, typename... Tail>   void debug_out(vector<string> args, int idx, int LINE_NUM, Head H, Tail... T) {     if (idx > 0)       cerr <<  ,  ;     else       cerr <<  Line(  << LINE_NUM <<  )  ;     stringstream ss;     ss << H;     cerr << args[idx] <<   =   << ss.str();     debug_out(args, idx + 1, LINE_NUM, T...);   }   const long long MOD = (int)1e9 + 7;   const long long INF = 2e18 + 5;   struct hashLL {     size_t operator()(long long x) const {       x = (x ^ (x >> 30)) * UINT64_C(0xbf58476d1ce4e5b9);       x = (x ^ (x >> 27)) * UINT64_C(0x94d049bb133111eb);       x = x ^ (x >> 31);       return x;     }   };   long long inv(long long a, long long b) {     return 1 < a ? b - inv(b % a, a) * b / a : 1;   }   long long gcd(long long a, long long b, long long& x, long long& y) {     if (a == 0) {       x = 0;       y = 1;       return b;     }     long long x1, y1;     long long d = gcd(b % a, a, x1, y1);     x = y1 - (b / a) * x1;     y = x1;     return d;   }   long long distsq(long long x1, long long y1, long long x2, long long y2) {     return (x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1);   }   const int N = 200001;   long long n, m, k, t, u, v;   string s;   unordered_set<int> ks;   unordered_map<int, vector<int>> g;   int visited[N];   long long tosink[N];   long long tosource[N];   void bfs(int start, long long arr[]) {     for (int i = 0; i < n + 1; i++) arr[i] = -1;     deque<int> q;     q.push_back(start);     arr[start] = 0;     while (!q.empty()) {       int n = q.size();       for (int i = 0; i < (n); i++) {         int curr = q.front();         q.pop_front();         for (int c : g[curr]) {           if (arr[c] == -1) {             arr[c] = arr[curr] + 1;             q.push_back(c);           }         }       }     }   }   int main() {     ios::sync_with_stdio(false);     cin.tie(0);     cin >> n >> m >> k;     for (int i = 0; i < (k); i++) {       cin >> u;       ks.emplace(u);     }     for (int i = 0; i < (m); i++) {       cin >> u >> v;       g[u].push_back(v);       g[v].push_back(u);     }     bfs(1, tosource);     bfs(n, tosink);     vector<pair<int, int>> tok;     map<int, unordered_set<int>> towardssink;     for (auto& p : ks) {       tok.push_back(make_pair(tosource[p], p));       towardssink[-tosink[p]].emplace(p);     }     sort((tok).begin(), (tok).end());     long long best = 0;     for (auto& p : tok) {       long long mintosource = p.first;       long long node = p.second;       towardssink[-tosink[node]].erase(node);       if (towardssink[-tosink[node]].empty()) {         towardssink.erase(-tosink[node]);       }       if (towardssink.empty()) break;       auto& pr = *towardssink.begin();       long long mxtosink = -pr.first;       long long curr = 1;       curr = curr + mintosource + min(tosink[node], mxtosink);       best = max(best, curr);     }     long long normaldist = tosink[1];     if (best < normaldist) {       cout << best << endl;     } else {       cout << normaldist << endl;     }   }


#include <bits/stdc++.h>   #pragma GCC optimize( O3 )   #pragma GCC target( sse4 )   using namespace std;   template <class T>   T gcd(T a, T b) {     return ((b == 0) ? a : gcd(b, a % b));   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     int n, q;     cin >> n >> q;     vector<int> a(n);     set<int> st;     multiset<int> mlt;     for (int i = 0; i < (int)(n); i++) cin >> a[i], st.insert(a[i]);     sort(a.begin(), a.end());     for (int i = (int)(1); i < (int)(n); i++) mlt.insert(a[i] - a[i - 1]);     int r = a[n - 1], l = a[0];     for (int i = 0; i < (int)(q + 1); i++) {       if (mlt.size()) {         auto it = --mlt.end();         int mx = *it;         cout << r - l - mx <<   n ;       } else         cout << 0 <<   n ;       if (i == q) continue;       int t;       cin >> t;       if (t == 0) {         int x;         cin >> x;         if (*st.begin() == x) {           auto it = st.begin();           it++;           if (it != st.end()) {             auto it2 = mlt.find(*it - *st.begin());             if (it2 != mlt.end()) mlt.erase(it2);           }         } else {           auto it = st.lower_bound(x);           it--;           if (mlt.find(x - *it) != mlt.end()) mlt.erase(mlt.find(x - *it));           auto it1 = it;           it1++;           it1++;           if (it1 != st.end()) {             auto it2 = mlt.find(*it1 - x);             if (it2 != mlt.end()) mlt.erase(it2);             mlt.insert(*it1 - *it);           }         }         auto it = st.find(x);         if (it != st.end()) st.erase(it);       } else {         int x;         cin >> x;         if (st.size()) {           auto it = st.lower_bound(x);           if (it != st.end()) {             mlt.insert(*it - x);             if (it != st.begin()) {               auto it1 = it;               it1--;               mlt.insert(x - *it1);               auto it2 = mlt.find(*it - *it1);               if (it2 != mlt.end()) mlt.erase(it2);             }           } else {             if (it != st.begin()) {               it--;               mlt.insert(x - *it);             }           }         }         st.insert(x);       }       if (st.size()) {         l = *st.begin();         r = *(--st.end());       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long m;   int k;   vector<int> a;   long long memo[66][2][66];   long long dp(int id, int tight, int cnt) {     long long &res = memo[id][tight][cnt];     if (res == -1) {       res = 0;       if (id == a.size()) {         if (cnt == k) res = 1;       } else {         for (int i = 0; i < 2; i++) {           if (tight && i > a[id]) continue;           int new_tight = tight ? i == a[id] : false;           int new_cnt = cnt + (i == 1 ? 1 : 0);           res += dp(id + 1, new_tight, new_cnt);         }       }     }     return res;   }   long long solve(long long n) {     a.clear();     while (n) {       a.push_back(n % 2);       n /= 2;     }     a.push_back(0);     reverse(a.begin(), a.end());     memset(memo, -1, sizeof(memo));     return dp(0, 1, 0);   }   long long possible(long long n) { return solve(2 * n) - solve(n); }   int main() {     ios::sync_with_stdio(false);     cin.tie(NULL);     cin >> m >> k;     long long lo = 1, hi = (long long)1e18 + 15;     long long ans = -1;     while (lo <= hi) {       long long mid = (lo + hi) / 2;       if (possible(mid) == m) {         ans = mid;         break;       }       if (possible(mid) < m) {         lo = mid + 1;       } else         hi = mid - 1;     }     assert(ans != -1);     cout << ans << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   bool compare(int a, int b) { return a > b; }   int main() {     int n, a, index;     cin >> n;     vector<int> A;     int temp;     int count = 0;     for (int i = 0; i < n; i++) {       cin >> a;       A.push_back(a);     }     for (int i = 0; i < n; i++) {       if (A[i] == 1) {         count++;         index = i;         break;       }     }     for (int i = index + 1; i < n; i++) {       if (A[i] == 1 && A[i - 1] == 1)         count++;       else if (A[i] == 1 && A[i - 1] == 0)         count += 2;       else         continue;     }     cout << count;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   void solve() {     vector<long long> v, ans;     long long n;     cin >> n;     for (long long i = 0; i < n; i++) {       long long q;       cin >> q;       v.push_back(q);     }     sort(v.begin(), v.end());     bool cur = 0;     while (!v.empty()) {       if (!cur) {         cout << v.front();         v.erase(v.begin());       } else {         cout << v.back();         v.erase(v.begin() + v.size() - 1);       }       cout <<    ;       cur = 1 - cur;     }   }   signed main() {     long long t = 1;     while (t--) solve();   }


#include <bits/stdc++.h>   using namespace std;   const long long INF64 = (long long)(1e18) + 10;   int main() {     ios_base::sync_with_stdio(false);     cin.tie(nullptr);     cout.tie(nullptr);     int q = 1, i, j, test, k;     for (test = 1; test < (q + 1); test++) {       long long n, ans = 0, x = 0;       cin >> n;       vector<long long> v(n);       for (i = 0; i < (n); i++) cin >> v[i];       map<long long, long long> prefodd, prefeven;       prefodd[0]++;       for (i = 0; i < (n); i++) {         x = x ^ v[i];         if (i & 1)           ans += prefodd[x], prefodd[x]++;         else           ans += prefeven[x], prefeven[x]++;       }       cout << ans <<   n ;     }   }


#include <bits/stdc++.h>   using namespace std;   bool isBeautiful(int n) {     bool flag[10];     for (int i = 0; i < 10; i++) flag[i] = false;     while (n > 0) {       int digit = n % 10;       if (flag[digit]) return false;       flag[digit] = true;       n = n / 10;     }     return true;   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     int n;     cin >> n;     if (isBeautiful(n)) n++;     while (!isBeautiful(n)) {       n++;     }     cout << n;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long INF = (1e15) + 1;   int n, dp[100004], par[100004];   long long d, arr[100004];   struct Node {     Node *leftNode, *rightNode;     long long leftBound, rightBound;     pair<int, int> mx;     Node() {       leftNode = rightNode = NULL;       leftBound = rightBound = 0LL;       mx = {0, 0};     }   };   void update(Node *par, long long pos, int dpMax, int ind) {     if (par->leftBound == pos && par->rightBound == pos) {       if ((par->mx).first < dpMax) {         par->mx = {dpMax, ind};       }       return;     }     long long mid = (par->leftBound + par->rightBound) >> 1;     if (pos <= mid) {       if (par->leftNode == NULL) par->leftNode = new Node();       par->leftNode->leftBound = par->leftBound;       par->leftNode->rightBound = mid;       update(par->leftNode, pos, dpMax, ind);       if ((par->leftNode->mx).first > (par->mx).first) {         par->mx = par->leftNode->mx;       }     } else {       if (par->rightNode == NULL) par->rightNode = new Node();       par->rightNode->leftBound = mid + 1;       par->rightNode->rightBound = par->rightBound;       update(par->rightNode, pos, dpMax, ind);       if ((par->rightNode->mx).first > (par->mx).first) {         par->mx = par->rightNode->mx;       }     }   }   pair<int, int> query(Node *par, long long l, long long r) {     if (par == NULL || par->leftBound > r || par->rightBound < l) {       return {0, 0};     }     if (par->leftBound >= l && par->rightBound <= r) {       return par->mx;     }     long long mid = (par->leftBound + par->rightBound) >> 1;     pair<int, int> p1 = query(par->leftNode, l, r);     pair<int, int> p2 = query(par->rightNode, l, r);     if (p1.first > p2.first)       return p1;     else       return p2;   }   int main() {     scanf( %d%lld , &n, &d);     for (int i = 1; i <= n; i++) scanf( %lld , &arr[i]);     Node *root = new Node();     root->leftBound = 0LL;     root->rightBound = INF;     int ans = 0, curInd = 0;     for (int i = 1; i <= n; i++) {       long long l = max(0LL, arr[i] - d);       long long r = min(INF, arr[i] + d);       pair<int, int> p1 = query(root, 1LL, l);       pair<int, int> p2 = query(root, r, INF);       pair<int, int> p = p1;       if (p.first < p2.first) p = p2;       dp[i] = p.first + 1;       par[i] = p.second;       update(root, arr[i], dp[i], i);       if (dp[i] > ans) {         ans = dp[i];         curInd = i;       }     }     printf( %d n , ans);     vector<int> inds;     while (curInd != 0) {       inds.push_back(curInd);       curInd = par[curInd];     }     reverse(inds.begin(), inds.end());     for (int i = 0; i <= inds.size() - 1; i++) {       printf( %d  , inds[i]);     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   template <typename T>   inline void Read(T &x) {     x = 0;     char c;     while (!isdigit(c = getchar()))       ;     do {       x = x * 10 + c -  0 ;     } while (isdigit(c = getchar()));   }   long long read() {     long long tmp;     cin >> tmp;     return tmp;   }   void giuncute() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);   }   void OF() {     freopen(          f           .inp ,          r , stdin);     freopen(          f           .out ,          w , stdout);   }   const long long MOD = 1e9 + 7, mxn = 5e3 + 2;   long long n, a[mxn] = {0}, numNode[mxn] = {0}, p[mxn];   int down[mxn][mxn] = {{0}}, up[mxn][2] = {{0}};   long long Pow(long long u, long long v) {     if (!v) return 1;     long long tmp = Pow(u, v >> 1);     (tmp *= tmp) %= MOD;     if (v & 1) return (tmp * u) % MOD;     return tmp;   }   int main() {     giuncute();     cin >> n;     numNode[1] = 1;     for (long long i = 1; i <= n - 1; ++i) {       cin >> a[i];       numNode[i + 1] = (numNode[i] * a[i]) % MOD;       p[i] = Pow(a[i], MOD - 2);     }     for (long long i = 1; i <= n; ++i) down[i][1] = a[i], up[i][1] = 1;     up[1][1] = 0;     for (long long i = n - 1; i >= 1; --i)       for (long long j = 2; j <= n - i; ++j)         down[i][j] = (a[i] * down[i + 1][j - 1]) % MOD;     long long p2 = Pow(2, MOD - 2);     for (long long j = 1; j <= n * 2 - 2; ++j) {       long long cnt_up = 0, cnt_down = 0;       if (j != 1)         for (long long i = 2; i <= n; ++i) {           if (j > n + i - 2) continue;           (up[i][j & 1] += up[i - 1][~j & 1]) %= MOD;           if (j <= n && down[i - 1][j - 1])             (up[i][j & 1] +=              (down[i - 1][j - 1] * (((a[i - 1] - 1) * p[i - 1]) % MOD)) % MOD) %=                 MOD;         }       for (long long i = 1; i <= n; ++i) {         up[i][~j & 1] = 0;         if (j > n + i - 2) continue;         (cnt_up += (numNode[i] * up[i][j & 1]) % MOD) %= MOD;         if (j < n) (cnt_down += (numNode[i] * down[i][j]) % MOD) %= MOD;       }       cout << (((cnt_up + cnt_down) % MOD) * p2) % MOD <<    ;     }   }


#include <bits/stdc++.h>   using namespace std;   template <typename A, typename B>   ostream& operator<<(ostream& out, const pair<A, B>& p) {     out <<  (  << p.first <<  ,  << p.second <<  )  ;     return out;   }   template <typename A, typename B>   void printP(const pair<A, B>& p, const char* str = NULL) {     if (!(1 && !1)) return;     if (str != NULL) cout << str <<  : ;     cout <<  (  << p.first <<  ,  << p.second <<  )  ;   }   template <typename T>   void printV(const vector<T>& v, const char* str = NULL) {     if (!(1 && !1)) return;     if (str != NULL) cout << str <<  :  ;     for (int i = 0; i < (v.size()); ++i) cout << v[i] <<    ;     cout << endl;   }   template <typename T>   void printC(T& container, const char* str = NULL) {     if (!(1 && !1)) return;     if (str != NULL) cout << str <<  : ;     for (__typeof((container).begin()) it = ((container).begin());          it != (container).end(); ++it) {       cout << (*it) <<    ;     }     cout << endl;   }   const int INF = 1000010111;   const int S_N = 152;   const int MOD = 1000000007;   int main() {     int n;     cin >> n;     vector<int> t1, t2;     for (int i = 0; i < n; i++) {       int thick, width;       cin >> thick >> width;       ((thick == 1) ? t1 : t2).push_back(width);     }     sort(t1.begin(), t1.end(), greater<int>());     sort(t2.begin(), t2.end(), greater<int>());     int mini = INF;     for (int c1 = 0; c1 <= t1.size(); c1++) {       for (int c2 = 0; c2 <= t2.size(); c2++) {         int tSum = 1 * c1 + 2 * c2;         int wSum = 0;         for (int i = c1; i < t1.size(); i++) wSum += t1[i];         for (int i = c2; i < t2.size(); i++) wSum += t2[i];         if (tSum >= wSum) mini = min(mini, tSum);       }     }     cout << mini << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 500010;   int n, a;   multiset<int> st;   int main() {     cin >> n;     st.insert(0);     for (int i = 1; i <= n; ++i) {       scanf( %d , &a);       if (st.find(a) != st.end()) {         st.erase(st.find(a));         st.insert(i);       } else {         st.insert(i);       }     }     cout << st.size() << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long arr[200005];   long long n;   map<long long, long long> ans;   bool debug = false;   long long rec(long long node) {     if (debug) cout <<  entering   << node << endl;     if (node == 1) return 1000000000000;     if (node <= 0 || node > n) return 0;     if (ans.count(node) && ans[node] == -1000000000000)       return ans[node] = 1000000000000;     if (ans.count(node)) {       if (debug) cout <<  already computed for   << node << endl;       return ans[node];     }     ans[node] = -1000000000000;     long long val = 0;     long long n1 = node + arr[node];     val += arr[node];     if (n1 <= 0 || n1 > n) return ans[node] = val;     long long n2 = n1 - arr[n1];     val += arr[n1];     return ans[node] = val + rec(n2);   }   int main() {     scanf( %lld , &n);     for (__typeof(n - 1) i = 0; i < n - 1; i++) scanf( %lld , &arr[i + 2]);     long long k;     for (__typeof(n) i = 2; i <= n; i++) {       long long val = i - 1;       long long nex = i - arr[i];       val += arr[i];       k = rec(nex) + val;       if (k >= 1000000000000)         cout << -1 << endl;       else         cout << k << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int maxn = 1e4 + 100;   int a[25];   int ans[25];   map<int, int> mp;   int main() {     int n;     scanf( %d , &n);     for (int i = 0; i < n; i++) {       scanf( %d , &a[i]);       ans[i] = a[i];     }     sort(ans, ans + n);     for (int i = 0; i < n; i++) {       if (i == 0)         mp[ans[i]] = ans[n - 1];       else         mp[ans[i]] = ans[i - 1];     }     for (int i = 0; i < n; i++) printf( %d  , mp[a[i]]);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n, a;     cin >> n >> a;     vector<int> vi(n);     for (int i = 0; i < n; ++i) cin >> vi[i];     sort((vi).begin(), (vi).end());     if (n == 1)       cout << 0;     else if (a <= vi[0])       cout << llabs(vi[n - 2] - a);     else if (a >= vi[n - 1])       cout << llabs(vi[1] - a);     else {       long long ans1, ans2, ans3, ans4;       ans1 = llabs(vi[0] - a) + llabs(vi[0] - vi[n - 2]);       ans2 = llabs(vi[1] - a) + llabs(vi[1] - vi[n - 1]);       ans3 = llabs(vi[n - 1] - a) + llabs(vi[n - 1] - vi[1]);       ans4 = llabs(vi[n - 2] - a) + llabs(vi[n - 2] - vi[0]);       cout << min(min(ans1, ans2), min(ans3, ans4));     }     cout <<   n ;   }


#include <bits/stdc++.h>   using namespace std;   int m[11];   int dig(int i) {     while (i) {       if (m[i % 10]) return 1;       i /= 10;     }     return 0;   }   int main() {     int n, i, s(0);     cin >> n;     int temp = n;     for (int i = 0; i < 11; i++) m[i] = 0;     while (temp) {       m[temp % 10] = 1;       temp /= 10;     }     for (int i = 1; i <= sqrt(n); i++) {       if (n % i == 0) {         s += dig(i);         if (i != sqrt(n)) s += dig(n / i);       }     }     cout << s << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     long long n, a, b, ans;     cin >> n >> a >> b;     if (b > 0) {       ans = (a + b) % n;       if (ans == 0) ans = n;     } else if (b < 0) {       if (a > abs(b)) {         ans = a - abs(b);       } else {         ans = n - abs(a + b) % n;       }     } else       ans = a;     cout << ans;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     map<char, int> ma;     int p;     char a, k[1000];     cin >> p;     for (int i = 0; i < p; i++) {       cin >> k[i];       a = tolower(k[i]);       ma[a]++;     }     if (ma.size() == 26)       cout <<  YES  << endl;     else       cout <<  NO  << endl;   }


#include <bits/stdc++.h>   using namespace std;   const long long oo = 3e18, M = 54;   long long q, n, k;   string F =        What are you doing at the end of the world? Are you busy? Will you save          us? ;   string X =  What are you doing while sending   ;   string Y =   ? Are you busy? Will you send   ;   string Z =   ? ;   long long length(int n) {     if (n >= M) {       return oo;     }     long long tot = X.size() + Y.size() + Z.size();     long long len = ((1LL << n) - 1) * tot + (1LL << n) * F.size();     return len;   }   char solve(long long i, long long k) {     if (i == 0) {       return k > F.size() ?  .  : F[k - 1];     }     long long len = length(i - 1);     if (X.size() + len + Y.size() + len + Z.size() < k) {       return  . ;     } else if (X.size() + len + Y.size() + len < k) {       k -= X.size() + len + Y.size() + len;       return Z[k - 1];     } else if (X.size() + len + Y.size() < k) {       k -= X.size() + len + Y.size();       return solve(i - 1, k);     } else if (X.size() + len < k) {       k -= X.size() + len;       return Y[k - 1];     } else if (X.size() < k) {       k -= X.size();       return solve(i - 1, k);     } else {       return X[k - 1];     }   }   int main() {     cin >> q;     while (q--) {       cin >> n >> k;       cout << solve(n, k);     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int maxn = 1e5 + 7;   const double eps = 1e-9;   int a[maxn];   int main() {     std::ios::sync_with_stdio(false);     int n, sum = 0;     cin >> n;     for (int i = 0; i < n; ++i) cin >> a[i], sum += a[i];     sort(a, a + n);     int ans = 0;     while (fabs(round(1.0 * sum / n) - 5.0) > eps) {       sum += 5 - a[0];       ++ans;       a[0] = 5;       sort(a, a + n);     }     cout << ans << endl;     return 0;   }


#include <bits/stdc++.h>   #pragma GCC optimize( O3 )   #pragma comment(linker,  /stack:200000000 )   #pragma GCC optimize( unroll-loops )   using namespace std;   using namespace std;   int main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     ;     long long n, m, p;     cin >> n >> m >> p;     vector<long long> a(n);     for (long long i = 0; i < n; ++i) cin >> a[i];     vector<long long> b(m);     for (long long i = 0; i < m; ++i) cin >> b[i];     for (long long i = 0; i < n; ++i) {       bool flag = false;       if (a[i] % p != 0) {         for (long long j = 0; j < m; ++j) {           if (b[j] % p != 0) {             flag = true;             cout << i + j;             break;           }         }         if (flag) {           break;         }       }     }     return 0;   }