#include <bits/stdc++.h>   using namespace std;   const long long mod = 1e9 + 7;   const int N = 2e5 + 555;   int n, m, q, z;   pair<long long, int> a[N];   int qt[N];   long long ql[N], qr[N];   long long ans[N];   vector<pair<int, int> > ed[N];   long long bl[N], br[N], arin[N];   int f[N * 20];   const int iu = 18;   int lg[N * 2];   long long st[N][iu], nid[N][iu];   long long sh[N][iu];   void adde(int u, int v, int t) { ed[t].push_back({u, v}); }   void add(int id, int l, int r) {     int duh = lg[r - l + 1];     int e = r - (1 << duh) + 1;     st[l][duh] = max(st[l][duh], min(arin[id], sh[l][duh]));     st[e][duh] = max(st[e][duh], min(arin[id], sh[e][duh]));     adde(id, nid[l][duh], min(arin[id], sh[l][duh]));     adde(id, nid[e][duh], min(arin[id], sh[e][duh]));   }   int find(int x) {     if (f[x] != x) f[x] = find(f[x]);     return f[x];   }   void join(int x, int y) {     x = find(x);     y = find(y);     f[x] = y;   }   int main() {     ios::sync_with_stdio(false);     cin.tie(0);     cin >> n >> q;     for (int i = 2; i < 262144; i++) lg[i] = lg[i / 2] + 1;     a[++m] = {-1, -q - 1};     a[++m] = {1LL << n, q + 1};     for (int i = 1; i <= q; i++) {       string s;       cin >> s >> ql[i] >> qr[i];       if (s[0] ==  b )         qt[i] = 1;       else         qt[i] = 2;       if (qt[i] == 1) {         a[++m] = {ql[i], -i};         a[++m] = {qr[i], i};       }     }     sort(a + 1, a + m + 1);     for (int i = 1; i < m; i++) {       long long cl = a[i].first + (i % 2);       long long cr = a[i + 1].first - (i % 2);       int cc;       if (i % 2 == 1)         cc = q + 1;       else         cc = -a[i].second;       if (cl > cr) continue;       if (cl == cr) {         bl[++z] = cl;         br[z] = cr;         arin[z] = cc;       } else {         long long fk = cl ^ cr;         while (fk != (fk & -fk)) fk ^= fk & -fk;         long long mid = cl | (fk - 1);         bl[++z] = cl;         br[z] = mid;         arin[z] = cc;         bl[++z] = mid + 1;         br[z] = cr;         arin[z] = cc;       }     }     int ptr = z;     for (int j = 0; j < iu; j++) {       for (int i = 1; i <= z; i++) {         nid[i][j] = ++ptr;         if (j == 0)           sh[i][j] = arin[i];         else if (i + (1 << (j - 1)) <= z) {           sh[i][j] = max(sh[i][j - 1], sh[i + (1 << (j - 1))][j - 1]);         }       }     }     for (int i = 1; i <= z; i++) {       for (int k = n - 1; k >= 0; k--) {         long long cl, cr;         bool die = false;         if (br[i] - bl[i] + 1 > (1LL << k)) {           cr = bl[i] | ((1LL << (k + 1)) - 1);           cl = cr ^ ((1LL << (k + 1)) - 1);           die = true;         } else {           cl = bl[i] ^ (1LL << k);           cr = br[i] ^ (1LL << k);         }         int zl = lower_bound(br + 1, br + z + 1, cl) - br;         int zr = lower_bound(bl + 1, bl + z + 1, cr + 1) - bl - 1;         add(i, zl, zr);         if (die) break;       }       adde(i, nid[i][0], arin[i]);     }     for (int j = iu - 2; j >= 0; j--) {       for (int i = 1; i <= z; i++) {         st[i][j] = max(st[i][j], st[i][j + 1]);         adde(nid[i][j], nid[i][j + 1], min(st[i][j + 1], sh[i][j]));         if (i > (1 << j)) {           st[i][j] = max(st[i][j], st[i - (1 << j)][j + 1]);           adde(nid[i][j], nid[i - (1 << j)][j + 1],                min(st[i - (1 << j)][j + 1], sh[i][j]));         }       }     }     for (int i = 1; i <= ptr; i++) f[i] = i;     for (int i = q + 1; i >= 1; i--) {       for (auto c : ed[i]) join(c.first, c.second);       if (qt[i] == 2) {         int cl = lower_bound(br + 1, br + z + 1, ql[i]) - br;         int cr = lower_bound(br + 1, br + z + 1, qr[i]) - br;         ans[i] = find(cl) == find(cr);       }     }     for (int i = 1; i <= q; i++) {       if (qt[i] == 2) cout << ans[i] <<   n ;     }   }


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


#include <bits/stdc++.h>   using namespace std;   int main() {     int n, m;     int aray[100007];     while (scanf( %d %d , &n, &m) != EOF) {       for (int i = 1; i < n; i++) {         scanf( %d , &aray[i]);       }       int sum = 0, i = 1;       bool flag = false;       while (true) {         sum = (i + aray[i]);         i = sum;         if (sum == m) {           flag = true;           break;         }         if (sum > m) {           break;         }       }       (flag) ? printf( YES n ) : printf( NO n );     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int inf = 1e9 + 333;   const long long linf = 1e18 + inf;   const int LEN = 6e5 + 5;   int n, m, q, len;   char str[LEN], s[LEN];   int a[LEN], st[LEN], nd[LEN], suf[LEN], ord[LEN], lcp[LEN], w[LEN], root[LEN],       L[LEN], R[LEN];   pair<pair<int, int>, int> C[LEN];   long long cnt[LEN];   void add(int x, char str[]) {     int m = strlen(str);     s[++len] =  # ;     st[x] = len + 1;     for (int i = 0; i < m; i++) s[++len] = str[i];     nd[x] = len;   }   int f(int x) {     if (x == root[x]) return x;     return root[x] = f(root[x]);   }   void merge(int x, int y) {     x = f(x);     y = f(y);     if (x == y) return;     root[y] = x;     cnt[x] += cnt[y];     L[x] = min(L[x], L[y]);     R[x] = max(R[x], R[y]);   }   int main() {     scanf( %d , &n);     for (int i = 1; i <= n; i++) {       scanf( %s , str);       add(i, str);     }     for (int i = 1; i <= n; i++) scanf( %d , a + i);     for (int i = 1; i <= len; i++) suf[i] = s[i];     for (int it = 1; it < len; it *= 2) {       for (int j = 1; j <= len; j++) {         C[j] = make_pair(pair<int, int>(suf[j], suf[min(j + it, len + 1)]), j);       }       sort(C + 1, C + len + 1);       for (int j = 1; j <= len; j++) {         suf[C[j].second] = suf[C[j - 1].second] + (C[j].first != C[j - 1].first);       }     }     for (int i = 1; i <= len; i++) ord[suf[i]] = i;     for (int i = 1; i <= len; i++) {     }     int j = 0;     for (int i = 1; i <= len; i++) {       if (suf[i] == 1) continue;       while (i + j <= len and ord[suf[i] - 1] + j <= len and              s[i + j] == s[ord[suf[i] - 1] + j] and s[i + j] !=  # )         j++;       lcp[suf[i]] = j;       if (j) j--;     }     for (int i = 1; i <= len; i++) {       assert(s[ord[i] + lcp[i]] != s[ord[i - 1] + lcp[i]] or              s[ord[i] + lcp[i]] ==  # );     }     for (int i = 1; i <= len; i++) {     }     memset(w, -1, sizeof(w));     for (int i = 1; i <= n; i++) {       for (int j = st[i]; j <= nd[i]; j++) {         w[suf[j]] = i;       }     }     for (int i = 1; i <= len; i++) {       if (w[i] != -1) cnt[i] = a[w[i]];       L[i] = R[i] = root[i] = i;     }     vector<pair<int, int> > vs;     for (int i = 1; i <= len; i++) {       vs.push_back(pair<int, int>(lcp[i], i));     }     sort(vs.begin(), vs.end());     long long ans = 0;     for (int i = 1; i <= n; i++) {       int x = suf[st[i]];       if (nd[i] - st[i] + 1 > lcp[x] and nd[i] - st[i] + 1 > lcp[x + 1]) {         ans = max(ans, (long long)a[i] * (nd[i] - st[i] + 1));       }     }     for (int it = len - 1; it >= 0; it--) {       int l = vs[it].first, x = vs[it].second;       merge(x, x - 1);       x = f(x);       if (lcp[L[x]] < l and lcp[R[x] + 1] < l) ans = max(ans, cnt[f(x)] * l);     }     printf( %lld n , ans);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n, s, ll, v[100011];   int dp[100011];   const int oo = 1 << 28;   int seg[400001];   int at, l, r;   void add(int n, int s, int e) {     if (at < s || at > e || s > e) return;     if (s == e) {       seg[n] = dp[s];       return;     }     add(n * 2, s, (s + e) / 2);     add(n * 2 + 1, (s + e) / 2 + 1, e);     seg[n] = min(seg[n * 2], seg[n * 2 + 1]);   }   int get(int n, int s, int e) {     if (l > e || r < s || l > r || s > e) return oo;     if (s >= l && e <= r) return seg[n];     return min(get(n * 2, s, (s + e) / 2), get(n * 2 + 1, (s + e) / 2 + 1, e));   }   int main() {     scanf( %d%d%d , &n, &s, &ll);     for (int i = 1; i <= n; ++i) scanf( %d , v + i);     set<pair<int, int> > q;     int E = n;     for (int i = n; i > 0; --i) {       dp[i] = oo;       q.insert(make_pair(v[i], i));       while ((--q.end())->first - q.begin()->first > s) {         q.erase(make_pair(v[E], E));         --E;       }       l = i + ll;       r = E + 1;       if (r >= l) {         if (r == n + 1)           dp[i] = 1;         else           dp[i] = min(dp[i], 1 + get(1, 1, n));       }       at = i;       add(1, 1, n);     }     if (dp[1] > 500000) dp[1] = -1;     printf( %d n , dp[1]);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 2e3 + 10;   const int mod = 1e9 + 7;   int read() {     int x = 0, f = 1;     char c = getchar();     while (c <  0  || c >  9 ) {       if (c ==  - ) f = -1;       c = getchar();     }     while (c >=  0  && c <=  9 ) x = (x << 1) + (x << 3) + c -  0 , c = getchar();     return x * f;   }   int inc(int x, int y) { return (x + y >= mod) ? (x + y - mod) : (x + y); }   int del(int x, int y) { return (x - y < 0) ? (x - y + mod) : (x - y); }   int qpow(int a, int x, int mo) {     int res = 1;     while (x) {       if (x & 1) res = 1ll * res * a % mo;       a = 1ll * a * a % mod;       x >>= 1;     }     return res;   }   vector<pair<int, int> > g[N];   long long dp[N][N];   map<int, long long> mp;   int q[N];   int main() {     int n = read(), m = read(), Q = read();     for (int i = 0; i < m; i++) {       int u = read(), v = read(), w = read();       g[u].push_back(make_pair(v, w));       g[v].push_back(make_pair(u, w));     }     for (int i = 0; i <= m; i++)       for (int j = 1; j <= n; j++) dp[i][j] = -2e18;     dp[0][1] = 0;     int ans = 0;     for (int i = 1; i <= m; i++) {       long long res = 0;       for (int j = 1; j <= n; j++)         for (int t = 0; t < g[j].size(); t++) {           pair<int, int> now = g[j][t];           int u = now.first, v = j;           dp[i][u] = max(dp[i - 1][v] + now.second, dp[i][u]);           if (i == m)             mp[now.second] = max(dp[i - 1][v] + now.second, mp[now.second]);           res = max(res, dp[i][u]);         }       ans = inc(ans, res % mod);     }     vector<pair<int, int> > p;     for (auto it : mp) {       p.push_back(it);     }     int head = 1, tail = 0;     int Inv2 = qpow(2, mod - 2, mod);     for (int i = 0; i < p.size(); i++) {       while (head <= tail && p[q[tail]].second <= p[i].second) tail--;       q[++tail] = i;     }     Q -= m;     int cnt = 0;     int pre = 1;     for (int i = 2; i <= tail; i++) {       pair<int, int> now = p[q[pre]];       pair<int, int> nxt = p[q[i]];       bool ok = 1;       long long k = (now.second - nxt.second) / (nxt.first - now.first);       for (int j = i + 1; j <= tail; j++) {         pair<int, int> o = p[q[j]];         if ((now.second - o.second) / (o.first - now.first) <= k) {           ok = 0;           break;         }       }       k -= cnt;       if (k <= 0 || ok == 0) continue;       if (Q <= k) k = Q;       ans = inc(ans, inc(1ll * k * (1 + k) % mod * Inv2 % mod * now.first % mod,                          1ll * (now.second + 1ll * cnt * now.first % mod) % mod *                              k % mod));       Q -= k;       cnt += k;       pre = i;       if (Q == 0) break;     }     pair<int, int> now = p[q[tail]];     if (Q > 0)       ans = inc(ans, inc(1ll * Q * (1 + Q) % mod * Inv2 % mod * now.first % mod,                          1ll * (now.second + 1ll * cnt * now.first % mod) % mod *                              Q % mod));     printf( %d n , ans);   }


#include <bits/stdc++.h>   using namespace std;   const int mod = 1e9 + 7;   const int maxn = 1e6 + 5;   long long int power(long long int base, long long int exp) {     long long int res = 1;     while (exp) {       if (exp % 2) res *= base;       base *= base;       res %= mod;       base %= mod;       exp /= 2;     }     return res;   }   long long int x[2], y[2], val[2];   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     cout.tie(NULL);     long long int i, n, a, b, c;     for (long long int j = 0; j < 2; j++) cin >> x[j] >> y[j];     cin >> n;     long long int ans = 0;     for (i = 1; i <= n; i++) {       cin >> a >> b >> c;       for (long long int j = 0; j < 2; j++) {         val[j] = -1;         if (a * x[j] + b * y[j] + c > 0) val[j] = 1;       }       if (val[1] > 0 && val[0] < 0) ans++;       if (val[1] < 0 && val[0] > 0) ans++;     }     cout << ans;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int const bound = 1e6 + 2, N = 2e5 + 1;   long long OO = 1e18;   double eps = 1e-6;   int oo = 1e9, mod = 1e9 + 7;   int n, m, s, app[N][2];   pair<int, int> par[N][2];   vector<int> adj[N], rev[N];   queue<pair<int, int> > q;   int vis[N];   void buildpath() {     puts( Win );     int cur = s, c = 1;     while (cur != -1) {       printf( %d  , cur);       pair<int, int> ne = par[cur][c];       cur = ne.first;       c = ne.second;     }   }   bool cyc(int u) {     vis[u] = 2;     for (auto v : rev[u]) {       if (vis[v] == 2) return 1;       if (!vis[v] && cyc(v)) return 1;     }     vis[u] = 1;     return 0;   }   int main() {     scanf( %d%d , &n, &m);     for (int(i) = 1; (i) <= (n); (i)++) {       int sz;       scanf( %d , &sz);       if (!sz) {         q.push({i, 0});         app[i][0] = 1;         par[i][0] = {-1, -1};       }       while (sz--) {         int a;         scanf( %d , &a);         adj[a].push_back(i);         rev[i].push_back(a);       }     }     scanf( %d , &s);     while (!q.empty()) {       pair<int, int> cur = q.front();       q.pop();       for (auto v : adj[cur.first]) {         int cost = cur.second + 1;         int md = cost % 2;         if (app[v][md]) continue;         app[v][md] = 1;         par[v][md] = {cur.first, cur.second};         if (v == s && md) {           buildpath();           return 0;         }         q.push({v, cost % 2});       }     }     if (cyc(s)) {       puts( Draw );       return 0;     }     puts( Lose );     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int t, n;     cin >> t;     while (t--) {       cin >> n;       int a = 0, b = 0, k = 2;       for (int i = 0; i < n / 2 - 1; i++) {         a += k;         k *= 2;       }       for (int i = 0; i < n / 2; i++) {         b += k;         k *= 2;       }       a += k;       cout << abs(a - b) << endl;     }   }


#include <bits/stdc++.h>   using namespace std;   int k, t, state[16];   unordered_map<unsigned long long, long long> M[10];   unsigned long long tobeull(int state[]) {     int tmp[16] = {         0,     };     unsigned long long ret = 0;     for (int i = 0; i < 16; i++) tmp[i] = state[i];     sort(tmp, tmp + 16);     for (int i = 0; i < 16; i++) {       ret |= (tmp[i] << (i * 4));     }     return ret;   }   long long dp(int len, int state[]) {     if (len == 0) {       return 1;     }     auto it = M[len].emplace(tobeull(state), 0);     if (it.second) {       long long ret = 0;       for (int i = 0; i < 16; i++) {         if (state[i] == t) continue;         state[i]++;         ret += dp(len - 1, state);         state[i]--;       }       it.first->second = ret;       return it.first->second;     } else       return it.first->second;   }   int main() {     scanf( %d %d , &k, &t);     if (k <= 15) {       printf( %lx , k);       return 0;     }     int mylen = 1;     state[15]++;     long long rem = 0;     while (1) {       long long key = dp(mylen - 1, state) * (long long)15;       if (rem + key >= k) {         break;       } else         rem += key;       ++mylen;     }     int st = 1;     state[15] = 0;     for (int digit = 1; digit <= mylen; digit++) {       long long rem = 0;       if (st == 1) {         state[15] = 1;         for (int ite = mylen - 2; ite >= 0; ite--)           rem += dp(ite, state) * (long long)15;         state[15] = 0;       }       for (int num = st; num < 16; num++) {         if (state[num] == t) continue;         state[num]++;         long long cnt = dp(mylen - digit, state);         if (rem + cnt >= k) {           printf( %x , num);           break;         } else {           rem += cnt;           state[num]--;         }       }       k -= rem;       st = 0;     }   }


#include <bits/stdc++.h>   using namespace std;   void solve() {     int n, ans = -2;     cin >> n;     vector<int> v(n);     for (auto &a : v) cin >> a;     vector<int> max_ele_index;     int maxx = 0;     for (auto a : v) maxx = max(a, maxx);     for (int i = 0; i < n; i++)       if (v[i] == maxx) max_ele_index.push_back(i);     if (v[0] == maxx and v[1] != maxx) {       cout << 1 <<   n ;       return;     }     if (v[0] != maxx and v[1] == maxx) {       cout << 2 <<   n ;       return;     }     if (v[n - 1] == maxx and v[n - 2] != maxx) {       cout << n <<   n ;       return;     }     for (int i = 1; i < n - 1; i++)       if (v[i] == maxx and (v[i - 1] != maxx or v[i + 1] != maxx)) {         ans = i;         break;       }     cout << ans + 1 <<   n ;   }   int main() {     int t;     cin >> t;     while (t--) solve();   }


#include <bits/stdc++.h>   using namespace std;   int a[5100], b[5100];   long long c[5100];   int main() {     int n;     cin >> n;     for (int i = 0; i < n; i++) {       cin >> a[i];       b[i] = a[i];     }     sort(b, b + n);     for (int i = 0; i < n; i++)       for (int j = 0; j < n; j++) {         int d = b[j] - a[i];         if (d > 0)           c[j] = min(c[j - 1], d + c[j]);         else           c[j] -= d;       }     long long ans = c[0];     while (--n)       if (ans > c[n]) ans = c[n];     cout << ans << endl;   }


#include <bits/stdc++.h>   using namespace std;   const int MOD = 1000 * 1000 * 1000 + 7;   const int INF = 2000 * 1000 * 1000;   const double EPS = 1e-9;   const double pi = acos(-1.0);   template <typename T>   inline T sqr(T n) {     return (n * n);   }   long long gcd(int a, int b) {     while (a && b) {       a %= b;       swap(a, b);     }     return a + b;   }   struct Point {     long long x, y;     Point() {}     Point(int x, int y) : x(x), y(y) {}     Point(const Point& a) { *this = a; }     Point operator-(const Point& a) const { return Point(x - a.x, y - a.y); }     inline long long dist() { return sqr(x) + sqr(y); }     bool operator<(const Point& p1) const {       return (x < p1.x || (x == p1.x && y < p1.y));     }   };   struct Line {     long long a, b, c;     Line() {}     inline void normalize() {       if (a < 0 || (a == 0 && b < 0)) {         a *= -1;         b *= -1;         c *= -1;       }       long long g = gcd(gcd(abs(a), abs(b)), abs(c));       if (g > 1) {         a /= g;         b /= g;         c /= g;       }     }     Line(int aa, int bb, int cc) {       a = aa, b = bb, c = cc;       normalize();     }     Line(const Point& p1, const Point& p2) {       a = p2.y - p1.y;       b = p1.x - p2.x;       c = p1.y * p2.x - p1.x * p2.y;       normalize();     }   };   const int maxn = 300010;   vector<pair<Point, int> > v[maxn];   vector<int> vec[maxn];   map<long long, int> position;   vector<Point> tmp[maxn];   Point center[2000];   long long rad[2000];   long long get_hash(const Line& l) {     long long a = l.a + 50000;     long long b = l.b + 50000;     long long c = l.c + 2000000000;     long long ret = c * 2000000000LL;     ret += a * 100000;     ret += b;     return ret;   }   void prepare(int pos) {     vector<pair<Point, int> >& cur = v[pos];     sort((cur).begin(), (cur).end());     vec[pos].resize(cur.size());     int cnt = 0;     for (size_t i = 0; i < cur.size(); i++) {       if (cur[i].second == 0)         cnt++;       else         cnt--;       vec[pos][i] = cnt;     }   }   long long get_ans(const Point& p1, const Point& p2) {     Line cur(p1, p2);     Point mid((p1.x + p2.x) / 2, (p1.y + p2.y) / 2);     Point norm(cur.a, cur.b);     norm.x += mid.x;     norm.y += mid.y;     cur = Line(mid, norm);     long long h = get_hash(cur);     if (!position.count(h)) return 0;     int pos = position[h];     int q = upper_bound((v[pos]).begin(), (v[pos]).end(), make_pair(mid, 1)) -             v[pos].begin() - 1;     if (q != -1) return vec[pos][q];     return 0;   }   int main() {     int n, m;     scanf( %d%d , &n, &m);     Point p1, p2;     int sz = 0;     for (int i = 0; i < n; i++) {       scanf( %I64d%I64d%I64d%I64d , &p1.x, &p1.y, &p2.x, &p2.y);       p1.x *= 2, p1.y *= 2;       p2.x *= 2, p2.y *= 2;       Line cur(p1, p2);       long long h = get_hash(cur);       int pos = 0;       if (position.count(h))         pos = position[h];       else {         pos = sz++;         position[h] = pos;       }       v[pos].push_back(make_pair(p1, (p1 < p2) ? 0 : 2));       v[pos].push_back(make_pair(p2, (p1 < p2) ? 2 : 0));     }     for (int i = 0; i < sz; i++) prepare(i);     long long ans = 0;     for (int i = 0; i < m; i++) {       scanf( %I64d%I64d%I64d , &center[i].x, &center[i].y, &rad[i]);       center[i].x *= 2;       center[i].y *= 2;       rad[i] *= 2;       for (size_t j = 0; j < tmp[rad[i]].size(); j++) {         if ((center[i] - tmp[rad[i]][j]).dist() > 4 * sqr(rad[i]))           ans += get_ans(center[i], tmp[rad[i]][j]);       }       tmp[rad[i]].push_back(center[i]);     }     printf( %I64d , ans);     return 0;   }


#include <bits/stdc++.h>   #pragma hdrstop   #pragma argsused   using namespace std;   int main(int argc, char* argv[]) {     int i, j, resp, resn, resv, resl, res, n, a, b;     vector<int> x, y;     scanf( %d , &n);     for (i = 0; i < n; i++) {       scanf( %d %d , &a, &b);       x.push_back(a);       y.push_back(b);     }     res = 0;     for (i = 0; i < n; i++) {       resv = 0;       resn = 0;       resp = 0;       resl = 0;       for (j = 0; j < n; j++) {         if (i != j) {           if (x[i] == x[j] && y[i] > y[j])             resn++;           else if (x[i] == x[j] && y[i] < y[j])             resv++;           else if (x[i] > x[j] && y[i] == y[j])             resl++;           else if (x[i] < x[j] && y[i] == y[j])             resp++;         }         if (resp != 0 && resl != 0 && resv != 0 && resn != 0) {           res++;           break;         }       }     }     printf( %d , res);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n;     long long d;     cin >> n >> d;     vector<long long> v;     long long x;     for (int i = 0; i < n; i++) {       cin >> x;       v.push_back(x);     }     int count = 0;     for (int i = 0; i < n; i++) {       for (int j = i + 1; j < n; j++) {         if (v[i] - v[j] >= 0 && v[i] - v[j] <= d)           count++;         else if (v[j] - v[i] >= 0 && v[j] - v[i] <= d)           count++;       }     }     cout << count * 2 << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n, k;   long long gcd(long long a, long long b) { return b == 0 ? a : gcd(b, a % b); }   long long lcm(long long a, long long b) { return a * b / gcd(a, b); }   int main() {     ios::sync_with_stdio(false);     cin.tie(NULL);     cin >> n >> k;     vector<int> c(n);     for (int &x : c) cin >> x;     long long cur = 1;     for (int &x : c) {       cur = gcd(k, lcm(cur, x));     }     if (cur == k) {       cout <<  Yes n ;     } else {       cout <<  No n ;     }     return 0;   }


#include <bits/stdc++.h>   #pragma GCC optimize(2)   #pragma GCC optimize(3)   #pragma GCC optimize( Ofast )   #pragma GCC optimize( inline )   #pragma GCC optimize( -fgcse )   #pragma GCC optimize( -fgcse-lm )   #pragma GCC optimize( -fipa-sra )   #pragma GCC optimize( -ftree-pre )   #pragma GCC optimize( -ftree-vrp )   #pragma GCC optimize( -fpeephole2 )   #pragma GCC optimize( -ffast-math )   #pragma GCC optimize( -fsched-spec )   #pragma GCC optimize( unroll-loops )   #pragma GCC optimize( -falign-jumps )   #pragma GCC optimize( -falign-loops )   #pragma GCC optimize( -falign-labels )   #pragma GCC optimize( -fdevirtualize )   #pragma GCC optimize( -fcaller-saves )   #pragma GCC optimize( -fcrossjumping )   #pragma GCC optimize( -fthread-jumps )   #pragma GCC optimize( -funroll-loops )   #pragma GCC optimize( -fwhole-program )   #pragma GCC optimize( -freorder-blocks )   #pragma GCC optimize( -fschedule-insns )   #pragma GCC optimize( inline-functions )   #pragma GCC optimize( -ftree-tail-merge )   #pragma GCC optimize( -fschedule-insns2 )   #pragma GCC optimize( -fstrict-aliasing )   #pragma GCC optimize( -fstrict-overflow )   #pragma GCC optimize( -falign-functions )   #pragma GCC optimize( -fcse-skip-blocks )   #pragma GCC optimize( -fcse-follow-jumps )   #pragma GCC optimize( -fsched-interblock )   #pragma GCC optimize( -fpartial-inlining )   #pragma GCC optimize( no-stack-protector )   #pragma GCC optimize( -freorder-functions )   #pragma GCC optimize( -findirect-inlining )   #pragma GCC optimize( -fhoist-adjacent-loads )   #pragma GCC optimize( -frerun-cse-after-loop )   #pragma GCC optimize( inline-small-functions )   #pragma GCC optimize( -finline-small-functions )   #pragma GCC optimize( -ftree-switch-conversion )   #pragma GCC optimize( -foptimize-sibling-calls )   #pragma GCC optimize( -fexpensive-optimizations )   #pragma GCC optimize( -funsafe-loop-optimizations )   #pragma GCC optimize( inline-functions-called-once )   #pragma GCC optimize( -fdelete-null-pointer-checks )   #pragma GCC target(         sse,sse2,sse3,ssse3,sse4.1,sse4.2,avx,avx2,popcnt,tune=native )   using namespace std;   struct node {     int next, to;   } w[2000010];   int n, m, cnt = 1, ans[1000005], h[1000005], top, ln, op, c[1000005],             dfn[1000005], timer, bj[1000005], v[1000005], id[2000005];   const int MAX_SIZE = 1 << 21;   char *l, *r, buf[MAX_SIZE];   inline int gc() {     if (l == r)       if (l == (r = (l = buf) + fread(buf, 1, MAX_SIZE, stdin))) return -1;     return *l++;   }   inline void read(int &x) {     register char c = gc();     for (; c < 48 || c > 57; c = gc())       ;     for (x = 0; c > 47 && c < 58; c = gc()) x = (x << 3) + (x << 1) + (c ^ 48);   }   inline void write(int x) {     if (x > 9) write(x / 10);     putchar(x % 10 +  0 );   }   void add(int x, int y, int z) {     cnt++;     w[cnt].to = y;     w[cnt].next = h[x];     id[cnt] = z;     h[x] = cnt;   }   inline void DFS1(int u, int co, int tag) {     register int i;     c[u] = co;     dfn[u] = ++timer;     for (i = h[u]; i; i = w[i].next) {       if (i == (tag ^ 1)) {         continue;       }       int e = w[i].to;       if (c[e] == 0) {         DFS1(e, 3 - co, i);       } else {         if (dfn[u] < dfn[e]) {           continue;         }         if (c[u] == c[e]) {           op++;           ln = i;           bj[u]++;           bj[e]--;         } else {           bj[u]--;           bj[e]++;         }       }     }   }   inline void DFS2(int u, int tag) {     register int i;     v[u] = 1;     for (i = h[u]; i; i = w[i].next) {       if (i == (tag ^ 1)) continue;       int e = w[i].to;       if (v[e] == 0) {         DFS2(e, i);         bj[u] += bj[e];       }     }     if (bj[u] == op) {       top++;       ans[top] = tag;     }   }   int main() {     register int i, x, y;     read(n);     read(m);     for (i = 1; i <= m; i++) {       read(x);       read(y);       if (x == y) {         op++;         add(0, 0, i);         ln = cnt;         continue;       }       add(x, y, i);       add(y, x, i);     }     for (i = 1; i <= n; i++) {       if (c[i] == 0) {         DFS1(i, 1, 0);       }     }     for (i = 1; i <= n; i++) {       if (v[i] == 0) {         DFS2(i, 0);       }     }     if (op == 0) {       write(m);       putchar(  n );       for (i = 1; i <= m; i++) {         write(i);         putchar(   );       }     } else {       if (op == 1) {         top++;         ans[top] = ln;       }       write(top);       putchar(  n );       sort(ans + 1, ans + 1 + top);       for (i = 1; i <= top; i++) {         write(id[ans[i]]);         putchar(   );       }     }   }


#include <bits/stdc++.h>   using namespace std;   char s[1111][1111];   int d[1111][1111];   queue<pair<int, int> > Q;   vector<pair<int, int> > v;   int n, m, lim;   int dx[4] = {1, 0, -1, 0};   int dy[4] = {0, 1, 0, -1};   void check(int x, int y, int z) {     if (x < 0 || x >= n || y < 0 || y >= m) return;     if (s[x][y] ==  T ) return;     if (s[x][y] ==  S ) lim = z;     d[x][y] = z;     if ( 0  <= s[x][y] && s[x][y] <=  9 )       v.push_back(make_pair(s[x][y] -  0 , z));     s[x][y] =  T ;     Q.push(make_pair(x, y));   }   int main() {     scanf( %d%d , &n, &m);     for (int i = 0; i < n; i++) scanf( %s , s[i]);     for (int i = 0; i < n; i++)       for (int j = 0; j < m; j++)         if (s[i][j] ==  E ) Q.push(make_pair(i, j));     while (!Q.empty()) {       pair<int, int> now = Q.front(), tmp;       Q.pop();       for (int dir = 0; dir < 4; dir++)         check(now.first + dx[dir], now.second + dy[dir],               d[now.first][now.second] + 1);     }     int ans = 0;     for (vector<pair<int, int> >::iterator ii = v.begin(); ii != v.end(); ii++)       if (ii->second <= lim) ans += ii->first;     cout << ans << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   struct less_key {     bool operator()(pair<double, int64_t> p1, pair<double, int64_t> p2) {       return (p1.first > p2.first) ||              ((p1.first == p2.first) && (p1.second < p2.second));     }   };   struct pair_hash {     std::size_t operator()(const pair<int64_t, int64_t>& k) const {       return static_cast<size_t>(k.first ^ k.second);     }   };   const int64_t mod = 100000000007ll;   const int64_t inf = 10000000000000007ll;   const double eps = 0.00000001;   int64_t n, k;   string s;   int64_t amin[256];   int64_t amax[256];   int main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     cin >> n >> k;     cin >> s;     for (int64_t i = 0; i < s.size(); i++) {       if (!amin[s[i]]) {         amin[s[i]] = i + 1;       }       amax[s[i]] = i + 1;     }     set<pair<int64_t, int64_t> > q;     for (int64_t i =  A ; i <=  Z ; i++) {       if (amin[i]) {         q.insert({amin[i], 0});         q.insert({amax[i], 1});       }     }     int64_t z = 0;     for (auto i : q) {       if (i.second) {         z--;       } else {         z++;       }       if (z > k) {         cout <<  YES  << endl;         return 0;       }     }     cout <<  NO  << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long long MOD = 1e9 + 7;   int main() {     int n;     cin >> n;     vector<int> a(n);     for (int i = 0; i < n; i++) {       cin >> a[i];     }     map<vector<int>, int> S;     for (int bit = 0; bit < (1 << 15); bit++) {       vector<int> b(n);       for (int i = 0; i < n; i++) {         int c = a[i] ^ bit;         for (int j = 0; j < 15; j++) {           if (c & (1 << j)) b[i]++;         }       }       vector<int> vec(n - 1);       for (int i = 1; i < n; i++) {         vec[i - 1] = b[i] - b[0];       }       S[vec] = bit;     }     for (int bit = 0; bit < (1 << 15); bit++) {       vector<int> b(n);       for (int i = 0; i < n; i++) {         int c = a[i] ^ (bit << 15);         for (int j = 15; j < 30; j++) {           if (c & (1 << j)) b[i]++;         }       }       vector<int> vec(n - 1);       for (int i = 1; i < n; i++) {         vec[i - 1] = b[0] - b[i];       }       if (S.find(vec) != S.end()) {         cout << (bit << 15) + S[vec] << endl;         return 0;       }     }     cout << -1 << endl;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int a, b, c;     cin >> a >> b >> c;     int Time = 0;     a -= 2;     int Turn = 2;     while (a > 0 || b > 0 || c > 0) {       if (Turn == 1) {         Time++;         a -= 2;         Turn = 2;       } else if (Turn == 2) {         Time++;         b -= 2;         Turn = 3;       } else if (Turn == 3) {         Time++;         c -= 2;         Turn = 1;       }     }     Time += 30;     cout << Time;   }


#include <bits/stdc++.h>   using namespace std;   int gcd(int x, int y) {     if (x == 0) return y;     return gcd(y % x, x);   }   int main() {     int n, m, x, g;     cin >> n >> m >> x;     g = x - 1;     for (int i = 1; i < n; ++i) {       scanf( %d , &x);       g = gcd(g, x - 1);     }     set<int> second;     int d = 1;     while (d * d <= g) {       if (g % d == 0) {         for (int p = 1; p * d <= m; p *= 2) second.insert(p * d);         for (int p = 1; p * (g / d) <= m; p *= 2) second.insert(p * (g / d));       }       ++d;     }     long long ans = 0;     for (set<int>::iterator it = second.begin(); it != second.end(); it++)       ans += m - (*it);     cout << ans;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int a, b, c, f[6];     cin >> a >> b >> c;     f[0] = a + b + c;     f[1] = a + b * c;     f[2] = a * b + c;     f[3] = a * b * c;     f[4] = (a + b) * c;     f[5] = a * (b + c);     sort(f, f + 6);     cout << f[5];     return 0;   }


#include <bits/stdc++.h>   using namespace std;   vector<long long int> ar[100005];   long long int vis[100005];   long long int mi;   long long int val[100005];   void dfs(long long int a) {     vis[a] = 1;     mi = min(val[a], mi);     for (auto it : ar[a]) {       if (vis[it] == 0) {         dfs(it);       }     }   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     long long int n, i, m, a, b, sum = 0;     cin >> n >> m;     for (i = 1; i <= n; i++) cin >> val[i];     for (i = 1; i <= m; i++) {       cin >> a >> b;       ar[a].push_back(b);       ar[b].push_back(a);     }     for (i = 1; i <= n; i++) {       if (vis[i] == 0) {         mi = INT_MAX;         dfs(i);         sum = sum + mi;       }     }     cout << sum <<   n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n;     int arr[10] = {0};     string s;     cin >> n >> s;     for (int i = 0; i < n; i++) arr[s[i] -  0 ]++;     if ((arr[1] || arr[2] || arr[3]) && (arr[7] || arr[0] || arr[9]) &&         (((arr[1] || arr[4] || arr[7]) && (arr[3] || arr[6] || arr[9])) ||          arr[0]))       cout <<  YES ;     else       cout <<  NO n ;   }


#include <bits/stdc++.h>   using namespace std;   using namespace std;   const int INF = 1000000000;   const int inf = INF;   struct Edge {     int from, to, cap, flow, index;     Edge(int from, int to, int cap, int flow, int index)         : from(from), to(to), cap(cap), flow(flow), index(index) {}   };   struct Dinic {     int N;     vector<vector<Edge> > G;     vector<Edge *> dad;     vector<int> Q;     Dinic(int N) : N(N), G(N), dad(N), Q(N) {}     void AddEdge(int from, int to, int cap) {       G[from].push_back(Edge(from, to, cap, 0, G[to].size()));       if (from == to) G[from].back().index++;       G[to].push_back(Edge(to, from, 0, 0, G[from].size() - 1));     }     long long BlockingFlow(int s, int t) {       fill(dad.begin(), dad.end(), (Edge *)NULL);       dad[s] = &G[0][0] - 1;       int head = 0, tail = 0;       Q[tail++] = s;       while (head < tail) {         int x = Q[head++];         for (int i = 0; i < G[x].size(); i++) {           Edge &e = G[x][i];           if (!dad[e.to] && e.cap - e.flow > 0) {             dad[e.to] = &G[x][i];             Q[tail++] = e.to;           }         }       }       if (!dad[t]) return 0;       long long totflow = 0;       for (int i = 0; i < G[t].size(); i++) {         Edge *start = &G[G[t][i].to][G[t][i].index];         int amt = INF;         for (Edge *e = start; amt && e != dad[s]; e = dad[e->from]) {           if (!e) {             amt = 0;             break;           }           amt = min(amt, e->cap - e->flow);         }         if (amt == 0) continue;         for (Edge *e = start; amt && e != dad[s]; e = dad[e->from]) {           e->flow += amt;           G[e->to][e->index].flow -= amt;         }         totflow += amt;       }       return totflow;     }     long long GetMaxFlow(int s, int t) {       long long totflow = 0;       while (long long flow = BlockingFlow(s, t)) totflow += flow;       return totflow;     }   };   struct node {     int r, c, t;     node() {}     node(int r, int c, int t) : r(r), c(c), t(t) {}   };   vector<node> qm, qf;   node read() {     int r, c, t;     cin >> r >> c >> t;     r--;     c--;     return node(r, c, t);   }   const int MAXN = 25;   const int MAXT = 600;   char mat[MAXN][MAXN];   int n, m, male, female;   int id[MAXN][MAXN];   int dis[MAXT][MAXT];   int main() {     cin >> n >> m >> male >> female;     for (int i = 0; i < n; i++)       for (int j = 0; j < m; j++) cin >> mat[i][j];     if (male < female)       qm.push_back(read());     else       qf.push_back(read());     for (int i = 0; i < male; i++) qm.push_back(read());     for (int i = 0; i < female; i++) qf.push_back(read());     if (male < female)       male++;     else       female++;     if (male != female) {       cout << -1 << endl;       return 0;     }     int tot = 0;     for (int i = 0; i < n; i++)       for (int j = 0; j < m; j++)         if (mat[i][j] ==  . ) id[i][j] = tot++;     for (int i = 0; i < tot; i++)       for (int j = 0; j < tot; j++) dis[i][j] = (i == j) ? 0 : inf;     for (int i = 0; i < n; i++)       for (int j = 0; j < m; j++)         if (mat[i][j] ==  . )           for (int ii = 0; ii < n; ii++)             for (int jj = 0; jj < m; jj++)               if (mat[ii][jj] ==  .  && abs(i - ii) + abs(j - jj) == 1)                 dis[id[i][j]][id[ii][jj]] = 1;     for (int k = 0; k < tot; k++)       for (int i = 0; i < tot; i++)         for (int j = 0; j < tot; j++)           dis[i][j] = min(dis[i][j], dis[i][k] + dis[k][j]);     long long lo = 0, hi = 1LL << 60, ans = -1;     while (lo <= hi) {       long long mid = (lo + hi) / 2;       Dinic res(male + female + 2 * tot + 2);       for (int i = 0; i < (int)qm.size(); i++) {         int index = id[qm[i].r][qm[i].c];         for (int j = 0; j < tot; j++)           if (dis[index][j] != inf && (long long)dis[index][j] * qm[i].t <= mid)             res.AddEdge(i, male + j, 1);       }       for (int j = 0; j < tot; j++) res.AddEdge(male + j, male + tot + j, 1);       for (int j = 0; j < tot; j++)         for (int k = 0; k < (int)qf.size(); k++) {           int index = id[qf[k].r][qf[k].c];           if (dis[index][j] != inf && (long long)dis[index][j] * qf[k].t <= mid)             res.AddEdge(male + tot + j, male + tot + tot + k, 1);         }       int source = male + tot + tot + female;       int sink = source + 1;       for (int i = 0; i < male; i++) res.AddEdge(source, i, 1);       for (int i = 0; i < female; i++) res.AddEdge(male + tot + tot + i, sink, 1);       if (res.GetMaxFlow(source, sink) == male)         ans = mid, hi = mid - 1;       else         lo = mid + 1;     }     cout << ans << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int a[200000 + 1];   int n;   int b[200000 + 1];   int main() {     cin >> n;     for (int i = 1; i <= n; i++) {       cin >> a[i];       b[a[i]] = 0;     }     int max = -1;     int k = 0;     int numb;     for (int i = n; i > 0; i--) {       k = 0;       if (b[a[i]] == 0) {         b[a[i]] = 1;         k = n - i;       }       if (k > max) {         max = k;         numb = a[i];       }     }     cout << numb;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int modulo = 1000000009;   long long A(int events, int days) {     if (events < days) return 0;     long long result = 1;     for (int e = events - 1; e >= days; --e) {       result *= e;       result %= modulo;     }     for (int i = 0; i < days; i++) {       result *= events;       result %= modulo;       --events;     }     return result;   }   int main() {     int n, w, b;     scanf( %i%i%i , &n, &w, &b);     long long result = 0;     for (int i = 2; i < n; i++) {       result += ((i - 1) * A(w, i)) % modulo * A(b, n - i);       result %= modulo;     }     printf( %i n , (int)result);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   double p[100005], e1[100005], e2[100005];   int main() {     int n;     scanf( %d , &n);     for (register int i = 1; i <= n; ++i) scanf( %lf , p + i);     for (register int i = 1; i <= n; ++i) {       e1[i] = (e1[i - 1] + 1) * p[i];       e2[i] = e2[i - 1] + (e1[i - 1] + e1[i - 1] + 1) * p[i];     }     printf( %.15lf , e2[n]);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int MAXN = (int)3e3 + 5;   const int INF = (int)1e9;   const string T =  aeiou ;   int dp[MAXN], par[MAXN];   bool space[MAXN];   bool vowel[256];   string s;   int n;   bool bad(char a, char b, char c) {     if (a == b && b == c) {       return 0;     }     if (!vowel[a] && !vowel[b] && !vowel[c]) {       return 1;     }     return 0;   }   void solve() {     cin >> s;     n = s.size();     s =  #  + s;     for (char c : T) {       vowel[c] = 1;     }     for (int i = 1; i <= n; ++i) {       dp[i] = INF;     }     for (int l = 1; l <= n; ++l) {       bool good = 1;       for (int r = l; r <= n; ++r) {         if (r - 2 >= l && bad(s[r - 2], s[r - 1], s[r])) {           good = 0;           break;         }         if (dp[l - 1] + 1 < dp[r]) {           dp[r] = dp[l - 1] + 1;           par[r] = l - 1;         }       }     }     for (int v = n; v > 0; v = par[v]) {       space[v] = 1;     }     for (int i = 1; i <= n; ++i) {       printf( %c , s[i]);       if (space[i]) {         printf(   );       }     }   }   int main() {     int tt = 1;     while (tt--) {       solve();     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int vis[6000];   int dp[5][100], mp[5][55], num[55];   int main() {     int n;     cin >> n;     for (int i = 1; i <= 2; i++)       for (int j = 1; j < n; j++) cin >> mp[i][j];     for (int i = 1; i <= n; i++) cin >> num[i];     memset(dp, 0, sizeof dp);     for (int i = n - 1; i >= 1; i--) dp[2][i] = dp[2][i + 1] + mp[2][i];     int t1 = 10000000, t2 = 10000000, cnt = 0;     for (int i = 1; i <= n; i++) {       int kl = dp[2][i] + num[i] + cnt;       if (kl < t2) {         if (kl < t1)           t2 = t1, t1 = kl;         else           t2 = kl;       }       cnt += mp[1][i];     }     int ans = t1 + t2;     cout << ans << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   set<string> ss;   string s;   void readInput() { cin >> s; }   void solve() {     for (int i = 0; i <= s.length(); i++) {       for (char c =  a ; c <=  z ; c++) {         string tmp;         tmp += s.substr(0, i) + c + s.substr(i, s.length());         ss.insert(tmp);       }     }     cout << ss.size();   }   int main() {     readInput();     solve();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int t, n;     scanf( %d , &t);     while (t--) {       scanf( %d , &n);       printf( %d n , n - 1);       for (int i = 2; i <= n; ++i) printf( %d  , i);       printf(  n );     }   }


#include <bits/stdc++.h>   using namespace std;   const int MAX_TAILLE = 100 * 1000 + 3;   long long T[1];   long long paire[MAX_TAILLE], impaire[MAX_TAILLE];   int n, e, a;   long long best;   int main() {     paire[0] = 0;     impaire[0] = 0;     int p = 1, ip = 1;     cin >> n >> e;     T[0] = 0;     for (int i = 1; i <= n; i++) {       cin >> a;       if (i % 2 == 1) {         paire[p] = paire[p - 1] + a - T[0];         p++;       } else {         impaire[ip] = impaire[ip - 1] + a - T[0];         ip++;       }       T[0] = a;     }     if ((n + 1) % 2 == 1) {       paire[p] = paire[p - 1] + e - T[0];       ip--;     } else {       impaire[ip] = impaire[ip - 1] + e - T[0];       p--;     }     best = paire[p];     for (int i = 1; i <= p; i++) {       best = max(best, paire[i] + impaire[ip] - impaire[i - 1] - 1);     }     cout << best;   }


#include <bits/stdc++.h>   using namespace std;   #pragma GCC optimize( Ofast )   #pragma GCC optimize( unroll-loops )   const long long maxn = 4005, maxk = 1e5 + 2, mod = 1e9 + 7;   long long a[maxn];   vector<long long> v;   bool mrk[maxn];   int32_t main() {     long long t;     cin >> t;     while (t--) {       long long n, mx = 0, lst = 0;       cin >> n >> mx;       for (long long i = 1; i < 2 * n; i++) {         cin >> a[i];         if (a[i] > mx) {           mx = a[i];           v.push_back(i - lst);           lst = i;         }       }       v.push_back(2 * n - lst);       for (long long i = 1; i <= n; i++) mrk[i] = false;       mrk[0] = true;       for (long long i = 0; i < (long long)(v).size(); i++) {         for (long long j = n - v[i]; j >= 0; j--) mrk[j + v[i]] |= mrk[j];       }       if (mrk[n])         cout <<  YES  <<   n ;       else         cout <<  NO  <<   n ;       v.clear();     }   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n, k;     cin >> n >> k;     int turn = 2 * k + 1;     if (n % turn == 0)       cout << n / turn <<    ;     else       cout << n / turn + 1 <<    ;     double upperbound = (n - 1) % turn + 1;     int MAXN = 0;     upperbound /= 2;     int temp = ceil(upperbound);     for (int i = min(k + 1, temp); i <= n; i += turn) {       cout << i <<    ;       MAXN = i;     }     if (MAXN + k < n) cout << MAXN + 2 * k + 1;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     long long n, l, r;     cin >> n >> l >> r;     cout << (n - l) + (int)pow(2, l) - 1 <<              << (n - r) * ((int)pow(2, r - 1)) + ((int)pow(2, r) - 1) << endl;   }


#include <bits/stdc++.h>   using namespace std;   inline int read() {     char c;     int x = 0;     int b = 1;     do {       c = getchar();       if (c == 45) b = -1;     } while (c > 57 || c < 48);     do x = x * 10 + c - 48, c = getchar();     while (c <= 57 && c >= 48);     x *= b;     return x;   }   int n, m, t;   const int maxn = 300 + 5;   const int logn = 10 + 5;   int f[logn][maxn][maxn];   int g[maxn][maxn], w[maxn][maxn];   int main() {     n = read(), m = read();     t = log2(n);     memset(f, 0xcf, sizeof(f));     memset(w, 0xcf, sizeof(w));     for (int i = 1, x, y, z, l; i <= m; i++) {       x = read(), y = read();       z = read();       l = read();       f[0][x][y] = z, f[0][y][x] = l;     }     for (int i = 1; i <= n; i++) w[i][i] = f[0][i][i] = 0;     for (int s = 1; s <= 10; s++) {       for (int k = 1; k <= n; k++) {         for (int i = 1; i <= n; i++) {           for (int j = 1; j <= n; j++) {             f[s][i][j] = max(f[s][i][j], f[s - 1][i][k] + f[s - 1][k][j]);           }         }       }     }     int ans = 0;     for (int s = 10; s >= 0; s--) {       memset(g, 0xcf, sizeof(g));       for (int k = 1; k <= n; k++) {         for (int i = 1; i <= n; i++) {           for (int j = 1; j <= n; j++) {             g[i][j] = max(g[i][j], w[i][k] + f[s][k][j]);           }         }       }       int sig = 0;       for (int i = 1; i <= n; i++)         if (g[i][i] > 0) sig = 1;       if (sig)         continue;       else {         for (int i = 1; i <= n; i++) {           for (int j = 1; j <= n; j++) {             w[i][j] = g[i][j];           }         }         ans += (1 << s);       }     }     printf( %d n , ans >= n ? 0 : ans + 1);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long int a[1001];   map<long long int, int> used;   int ans = 0;   map<pair<long long int, long long int>, bool> us;   void rec(long long int x, long long int y, int kol) {     ans = max(ans, kol);     long long int z = x + y;     if (z > 1000000000 || z < -1000000000) return;     x = y;     y = z;     if (used[z] > 0) {       used[z]--;       rec(x, y, kol + 1);       used[z]++;     }   }   int main() {     int n;     cin >> n;     for (int i = 1; i <= n; ++i) {       cin >> a[i];       used[a[i]]++;     }     for (int i = 1; i <= n; ++i) {       for (int j = 1; j <= n; ++j) {         if (us[make_pair(a[i], a[j])] || i == j) continue;         us[make_pair(a[i], a[j])] = true;         used[a[i]]--;         used[a[j]]--;         rec(a[i], a[j], 2);         used[a[i]]++;         used[a[j]]++;       }     }     cout << ans << endl;     return 0;   }


#include <bits/stdc++.h>   long long int M = 1000000007;   using namespace std;   vector<int> cnt(1e6 + 1);   void SieveOfEratosthenes(int n) {     bool prime[n + 1];     memset(prime, true, sizeof(prime));     for (int p = 2; p * p <= n; p++) {       if (prime[p] == true) {         for (int i = p * p; i <= n; i += p) prime[i] = false;       }     }     int c = 0;     cnt[1] = c;     cnt[0] = c;     for (int p = 2; p <= n; p++)       if (prime[p])         cnt[p] = ++c;       else         cnt[p] = c;   }   int main() {     ios::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     cout << setprecision(25);     ;     SieveOfEratosthenes(1e6 + 1);     int T;     cin >> T;     while (T--) {       int n;       cin >> n;       int r = floor(sqrt(n));       cout << 1 + cnt[n] - cnt[r] <<   n ;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int INF = 0x3f3f3f3f;   const long long INFF = 0x3f3f3f3f3f3f3f3fll;   const long long M = 1e9 + 7;   const long long maxn = 1e6 + 107;   const double pi = acos(-1.0);   const double eps = 0.0000000001;   long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; }   template <typename T>   inline void pr2(T x, int k = 64) {     long long i;     for (i = 0; i < k; i++) fprintf(stderr,  %d , (x >> i) & 1);     putchar(   );   }   template <typename T>   inline void add_(T &A, int B, long long MOD = M) {     A += B;     (A >= MOD) && (A -= MOD);   }   template <typename T>   inline void mul_(T &A, long long B, long long MOD = M) {     A = (A * B) % MOD;   }   template <typename T>   inline void mod_(T &A, long long MOD = M) {     A %= MOD;     A += MOD;     A %= MOD;   }   template <typename T>   inline void max_(T &A, T B) {     (A < B) && (A = B);   }   template <typename T>   inline void min_(T &A, T B) {     (A > B) && (A = B);   }   template <typename T>   inline T abs(T a) {     return a > 0 ? a : -a;   }   inline long long powMM(long long a, long long b, long long mod = M) {     long long ret = 1;     for (; b; b >>= 1ll, a = a * a % mod)       if (b & 1) ret = ret * a % mod;     return ret;   }   int startTime;   void startTimer() { startTime = clock(); }   void printTimer() {     fprintf(stderr,  /--- Time: %ld milliseconds ---/ n , clock() - startTime);   }   const int mod = 1e9 + 7;   struct mint {     long long x;     mint() : x(0) {}     mint(long long x) : x((x % mod + mod) % mod) {}     mint &fix() {       x = (x % mod + mod) % mod;       return *this;     }     mint operator-() const { return mint(0) - *this; }     mint operator~() const { return mint(1) / *this; }     mint &operator+=(const mint &a) {       if ((x += a.x) >= mod) x -= mod;       return *this;     }     mint &operator-=(const mint &a) {       if ((x += mod - a.x) >= mod) x -= mod;       return *this;     }     mint &operator*=(const mint &a) {       (x *= a.x) %= mod;       return *this;     }     mint &operator/=(const mint &a) {       (x *= a.pow(mod - 2).x) %= mod;       return *this;     }     mint operator+(const mint &a) const { return mint(*this) += a; }     mint operator-(const mint &a) const { return mint(*this) -= a; }     mint operator*(const mint &a) const { return mint(*this) *= a; }     mint operator/(const mint &a) const { return mint(*this) /= a; }     mint pow(long long t) const {       if (!t) return 1;       mint res = pow(t / 2);       res *= res;       return (t & 1) ? res * x : res;     }     bool operator<(const mint &a) const { return x < a.x; }     bool operator==(const mint &a) const { return x == a.x; }   };   int bruteforce(string s) {     set<string> S, T;     S.insert(  );     for (auto c : s) {       for (auto str : S) {         if (str.length() == 0)           T.insert(str + c);         else {           if (c ==  0 ) {             T.insert(str);             T.insert(str +  0 );           }           if (c ==  1 ) {             T.insert(str +  1 );             *str.rbegin() =  1 ;             T.insert(str);           }         }       }       S.swap(T);       set<string>().swap(T);     }     printf( %d n , (int)T.size());     return T.size();   }   mint val_kzero[maxn], to_next[maxn];   mint dp[maxn];   char str[maxn];   int main() {     scanf( %s , str + 1);     int n = strlen(str + 1), i;     for (i = 1; i <= n; i++)       if (str[i] ==  1 ) break;     if (i == n + 1) return 0 * printf( %d  , n);     int noww = i - 1;     n -= noww;     for (i = 1; i <= n; i++) str[i] = str[i + noww];     int count = 0;     to_next[0] = mint(1);     for (i = 1; i <= n; i++) {       if (str[i] ==  1 ) {         count = 0;         dp[i] = to_next[count];         to_next[0] = dp[i];         to_next[1] += dp[i];       } else {         count++;         dp[i] = to_next[count];         to_next[count] = mint(0);         to_next[count + 1] += dp[i];         to_next[0] += dp[i];       }       val_kzero[count] += dp[i];     }     mint ans(0);     for (i = 0; i <= count; i++) ans += val_kzero[i];     printf( %lld n , ans.x * (noww + 1) % M);   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int i, j, k, m, n, boys[105], girls[105];     while (cin >> n >> m) {       memset(boys, 0, sizeof(boys));       memset(girls, 0, sizeof(girls));       cin >> k;       while (k--) {         cin >> j;         boys[j] = 1;       }       cin >> k;       while (k--) {         cin >> j;         girls[j] = 1;       }       int sqr = max(n, m);       sqr = sqr * sqr;       for (i = 0; i < sqr; i++) {         j = (i % n);         k = (i % m);         boys[j] = (boys[j] || girls[k]);         girls[k] = (boys[j] || girls[k]);       }       bool possible = true;       for (i = 0; i < n; i++) {         if (boys[i] == 0) possible = false;       }       for (i = 0; i < m; i++) {         if (girls[i] == 0) possible = false;       }       if (possible)         cout <<  Yes n ;       else         cout <<  No n ;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int MN = 1e5 + 5;   int T, N, arr[MN];   void solve() {     scanf( %d , &N);     for (int i = 0; i < N; i++) scanf( %d , arr + i);     sort(arr, arr + N);     printf( %d n , min(min(arr[N - 1], arr[N - 2]) - 1, N - 2));   }   int main() {     scanf( %d , &T);     while (T--) solve();   }


#include <bits/stdc++.h>   using namespace std;   const int N = 1210, inf = 0x3f3f3f3f;   int n, m, k, x, y, rd[N], s, t, cnt = 1, head[N], stk[N], col[N], tot, sum,                                   vis[N];   struct edge {     int to, nxt, c;   } e[N * 10];   void Adde(int x, int y, int c) {     e[++cnt].to = y;     e[cnt].nxt = head[x];     head[x] = cnt;     e[cnt].c = c;   }   void adde(int x, int y, int c) {     Adde(x, y, c);     Adde(y, x, 0);   }   void gg() {     for (int i = (1); i <= (m); i++) printf( 0  );     puts(  );   }   bool bfs() {     queue<int> q;     q.push(s);     for (int i = (1); i <= (t); i++) vis[i] = -1;     vis[s] = 1;     while (!q.empty()) {       int u = q.front();       q.pop();       for (int i = head[u], v; i; i = e[i].nxt)         if (v = e[i].to, e[i].c && vis[v] == -1) vis[v] = vis[u] + 1, q.push(v);     }     return vis[t] != -1;   }   int dfs(int u, int flow) {     if (u == t) return flow;     int used = 0, w;     for (int i = head[u], v; i; i = e[i].nxt)       if (v = e[i].to, e[i].c && vis[v] == vis[u] + 1) {         w = dfs(v, min(e[i].c, flow - used));         e[i].c -= w;         e[i ^ 1].c += w;         used += w;         if (used == flow) return flow;       }     if (!used) vis[u] = -1;     return used;   }   int dinic() {     int res = 0;     while (bfs()) res += dfs(s, inf);     return res;   }   int Main() {     scanf( %d%d%d , &n, &m, &k);     s = n + m + 1, t = s + 1;     tot = sum = 0;     for (int i = (1); i <= (m); i++) {       scanf( %d%d , &x, &y), rd[x]++, rd[y]++;       adde(i + n, x, 1), adde(i + n, y, 1), adde(s, i + n, 1);     }     for (int i = (1); i <= (n); i++)       if (rd[i] > k) adde(i, t, 2 * (rd[i] - k)), sum += 2 * (rd[i] - k);     if (dinic() == sum) {       memset(col, 0, m + 1 << 2);       for (int u = (1); u <= (n); u++) {         int top = 0;         for (int i = head[u]; i; i = e[i].nxt)           if (e[i].c) stk[++top] = e[i].to - n;         for (int i = 1; i <= top; i += 2) col[stk[i]] = col[stk[i + 1]] = ++tot;       }       for (int i = (1); i <= (m); i++)         if (!col[i]) col[i] = ++tot;       for (int i = (1); i <= (m); i++) printf( %d  , col[i]);       puts(  );     } else       gg();     cnt = 1;     memset(head, 0, t + 1 << 2);     memset(rd, 0, n + 1 << 2);     return 0;   }   int main() {     int T;     scanf( %d , &T);     while (T--) Main();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int maxn = 200010;   vector<int> g[maxn + 1];   int father[maxn + 1], a[maxn + 1];   int f[maxn + 1], fup[maxn + 1];   int n, K;   int result;   int ge[maxn + 1], upge[maxn + 1];   int MIN;   void init() {     int i, x, y;     scanf( %d%d , &n, &K);     for (i = 1; i <= n; ++i) scanf( %d , &a[i]);     for (i = 1; i < n; ++i) {       scanf( %d%d , &x, &y);       g[x].push_back(y);       g[y].push_back(x);     }   }   void calc_ge(int p, int fa) {     int i, j;     father[p] = fa;     ge[p] = 1;     for (i = 0; i < g[p].size(); ++i) {       j = g[p][i];       if (j != fa) {         calc_ge(j, p);         ge[p] += ge[j];       }     }   }   void calc_f(int p) {     int i, j, zd = 0, sum = 0;     if ((g[p].size() == 1) && (father[p] == g[p][0])) {       if (a[p] >= MIN)         f[p] = 1;       else         f[p] = 0;       return;     }     if (a[p] < MIN) {       f[p] = 0;       for (i = 0; i < g[p].size(); ++i) {         j = g[p][i];         if (j != father[p]) calc_f(j);       }       return;     }     for (i = 0; i < g[p].size(); ++i) {       j = g[p][i];       if (j != father[p]) {         calc_f(j);         if (f[j] == ge[j])           sum += f[j];         else {           if (f[j] > zd) zd = f[j];         }       }     }     f[p] = 1 + sum + zd;   }   void calc_fup(int p) {     int i, j, zd = 0, zd2 = 0, sum = 0;     if ((g[p].size() == 1) && (father[p] == g[p][0])) return;     if (a[p] < MIN) {       for (i = 0; i < g[p].size(); ++i) {         j = g[p][i];         if (j != father[p]) {           fup[j] = 0;           calc_fup(j);         }       }       return;     }     if (fup[p] == upge[p])       sum = fup[p];     else       zd = fup[p];     for (i = 0; i < g[p].size(); ++i) {       j = g[p][i];       if (j != father[p]) {         if (f[j] == ge[j])           sum += f[j];         else {           if (f[j] > zd) {             zd2 = zd;             zd = f[j];           } else if (f[j] > zd2)             zd2 = f[j];         }       }     }     for (i = 0; i < g[p].size(); ++i) {       j = g[p][i];       if (j != father[p]) {         if (f[j] == ge[j])           fup[j] = 1 + sum - f[j] + zd;         else if (f[j] == zd)           fup[j] = 1 + sum + zd2;         else           fup[j] = 1 + sum + zd;       }     }     for (i = 0; i < g[p].size(); ++i) {       j = g[p][i];       if (j != father[p]) calc_fup(j);     }   }   bool ky(int now) {     int p, i, j, sum, zd;     MIN = now;     calc_f(1);     fup[1] = 0;     calc_fup(1);     for (p = 1; p <= n; ++p) {       sum = zd = 0;       if (fup[p] == upge[p])         sum = fup[p];       else         zd = fup[p];       if (a[p] < MIN) continue;       for (i = 0; i < g[p].size(); ++i) {         j = g[p][i];         if (j != father[p]) {           if (f[j] == ge[j])             sum += f[j];           else {             if (f[j] > zd) zd = f[j];           }         }       }       if (1 + sum + zd >= K) return true;     }     return false;   }   void work() {     int i, start, stop, mid;     father[1] = 0;     calc_ge(1, 0);     for (i = 1; i <= n; ++i) upge[i] = n - ge[i];     start = 1;     stop = 1000010;     while (start <= stop) {       mid = (start + stop) / 2;       if (ky(mid)) {         result = mid;         start = mid + 1;       } else         stop = mid - 1;     }   }   void output() { printf( %d n , result); }   int main() {     init();     work();     output();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long double PI = 3.141592653589793238462643383;   struct Rcompare {     bool operator()(pair<int, int> lhs, pair<int, int> rhs) {       return lhs.first > rhs.first;     }   };   struct compare {     bool operator()(pair<int, int> lhs, pair<int, int> rhs) {       return lhs.first < rhs.first;     }   };   long long int gcd(long long int a, long long int b) {     if (a == 0) return b;     return gcd(b % a, a);   }   long long int lcm(long long int a, long long int b) {     return a * (b / gcd(a, b));   }   long long int Abs(long long int a) {     if (a > 0) return a;     return -a;   }   long long int Ceil(long long int a, long long int b) {     if (a % b == 0)       return a / b;     else       return a / b + 1;   }   double Abs(double a) {     if (a > 0) return a;     return -a;   }   long long int pow(long long int x, long long int y) {     if (y == 0) return 1;     long long int tmp = pow(x, y / 2);     if (y % 2 == 1) return ((tmp * tmp) * x);     return (tmp * tmp);   }   const int MAX = 100009;   const int MOD = 1e9 + 7;   const int inf = 1e9 + 10;   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     int a, b, c, m;     cin >> c >> b >> a >> m;     if (2 * a < m || a > 2 * m) {       cout << -1 <<   n ;       return 0;     } else if (2 * m > 2 * b || 2 * m > 2 * c) {       cout << -1 <<   n ;       return 0;     } else {       for (int i = max(2 * m + 1, c); i <= (2 * c); ++i)         for (int j = max(2 * m + 1, b); j <= (2 * b); ++j)           for (int k = max(a, m); k <= (min(2 * a, 2 * m)); ++k) {             if (k < j && j < i) {               cout << i <<   n  << j <<   n  << k <<   n ;               return 0;             }           }     }     cout << -1 <<   n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long int arr[100005];   int main() {     long long int n, a, b, x, val, pos;     cin >> n >> a >> b;     for (long long int i = 0; i < n; i++) {       cin >> x;       long long int val = x * a;       long long int t = val / b;       long long int tb = t * b;       long long int bad = ceil((long double)tb / a);       cout << x - bad << endl;     }   }


#include <bits/stdc++.h>   using namespace std;   const int mod = 99999997;   const int mmax = 200010;   const double eps = 1e-8;   const double pi = acos(-1.0);   const int inf = 0x3fffffff;   int a[200010], b[200010];   int main() {     int n, m, sa, sb;     int cnt1, cnt2, df;     while (cin >> n) {       for (int i = 0; i < n; i++) scanf( %d , &a[i]);       cin >> m;       for (int i = 0; i < m; i++) scanf( %d , &b[i]);       sort(a, a + n);       sort(b, b + m);       sa = 3 * n, sb = 3 * m;       df = sa - sb;       cnt1 = 0, cnt2 = 0;       while (cnt1 < n && cnt2 < m) {         if (a[cnt1] == b[cnt2])           cnt1++, cnt2++;         else if (a[cnt1] < b[cnt2]) {           df -= 1;           cnt1++;         } else {           df += 1;           if (df > sa - sb) {             sa = 2 * cnt1 + 3 * (n - cnt1);             sb = 2 * (cnt2 + 1) + 3 * (m - cnt2 - 1);           }           cnt2++;         }       }       if (2 * n - 2 * m > sa - sb) sa = 2 * n, sb = 2 * m;       cout << sa <<  :  << sb << endl;     }     return 0;   }


#include <bits/stdc++.h>   #pragma GCC optimize( Ofast )   using namespace std;   int main() {     ios::sync_with_stdio(false);     cin.tie(NULL);     int n, m;     cin >> n >> m;     vector<int> a(n);     for (int i = 0; i < n; ++i) {       cin >> a[i];     }     int lo = 0, hi = m;     while (lo < hi) {       int mid = (lo + hi) / 2;       int last = 0;       bool b = true;       for (int i = 0; i < n; ++i) {         int delta = (last - a[i] + m) % m;         if (delta <= mid) continue;         if (a[i] < last) {           b = false;           break;         }         last = a[i];       }       if (b) {         hi = mid;       } else {         lo = mid + 1;       }     }     cout << lo <<   n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long int sieve[1000003 + 5];   long long int num_primes[1000005] = {0};   void calc_sieve() {     for (long long int i = 0; i <= 1000003; i++) sieve[i] = 0;     sieve[0] = sieve[1] = 1;     for (long long int i = 2; i <= 1000003; i++) {       num_primes[i] = num_primes[i - 1];       if (sieve[i] == 0) {         num_primes[i]++;         for (long long int j = i + i; j <= 1000003; j = j + i) sieve[j]++;       }     }   }   long long int ans = -1;   bool check(long long int a, long long int b, long long int l, long long int k) {     for (long long int i = a; i <= b - l + 1; i++) {       if (num_primes[i + l - 1] - num_primes[i - 1] < k) return false;     }     ans = l;     return true;   }   void binary_search_primes(long long int a, long long int b, long long int k,                             long long int start, long long int end) {     if (end - start <= 1) {       if (!check(a, b, start, k)) check(a, b, end, k);       return;     }     long long int mid = (start + end) / 2;     if (check(a, b, mid, k)) {       binary_search_primes(a, b, k, start, mid - 1);     } else {       binary_search_primes(a, b, k, mid + 1, end);     }   }   int main() {     calc_sieve();     long long int a, b, k;     cin >> a >> b >> k;     binary_search_primes(a, b, k, 1, b - a + 1);     cout << ans;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int32_t main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     int t;     cin >> t;     while (t--) {       int n;       cin >> n;       int n1 = n;       int k1;       int k[n + 1];       int pri[n + 1];       for (int i = 1; i <= n; i++) {         k[i] = 0;         pri[i] = 0;       }       int f = 0;       std::vector<int> nhi;       for (int j = 1; j <= n; j++) {         f = 0;         cin >> k1;         int pr[k1 + 1];         for (int i = 1; i <= k1; i++) {           cin >> pr[i];         }         for (int i = 1; i <= k1; i++) {           if (k[pr[i]] == 0) {             k[pr[i]] = 1;             pri[j] = 1;             break;           }         }         if (pri[j] == 0) {           nhi.push_back(j);         }       }       if (nhi.empty())         cout <<  OPTIMAL               <<   n ;       else {         int g;         for (int i = 1; i <= n; i++) {           if (k[i] == 0) {             g = i;             break;           }         }         cout <<  IMPROVE               <<   n ;         cout << nhi[0] <<     << g <<   n ;       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   char buf[25];   const int maxn = 1000010;   int a[maxn * 2], b[maxn * 2];   int n, m, x, y, t, mx;   int read() {     int x = 0, f = 0;     char ch = getchar();     while (ch <  0  || ch >  9 ) {       if (ch ==  - ) f = 1;       ch = getchar();     }     while (ch >=  0  && ch <=  9 ) {       x = (x << 1) + (x << 3) + (ch ^ 48);       ch = getchar();     }     return f ? -x : x;   }   void write(int x) {     if (!x) {       putchar( 0 );       return;     }     if (x < 0) {       putchar( - );       x = -x;     }     int cnt = 0;     while (x) {       buf[++cnt] =  0  + x % 10;       x /= 10;     }     for (int i = cnt; i >= 1; --i) putchar(buf[i]);   }   int main() {     t = read();     for (int i = 1; i <= t; ++i) {       int v = read();       mx = max(mx, v);       ++a[v];     }     int x = 1;     for (int i = 1; i <= mx; ++i)       if (a[i] != i * 4) {         x = i;         break;       }     for (int i = x * 2 - 1; i <= t; ++i)       if (t % i == 0) {         n = i;         m = t / i;         y = n + m - x - mx;         if (y <= 0 || y * 2 - 1 > m) continue;         bool flag = true;         int t1 = 0, t2 = 0;         for (int j = 1; j <= n; ++j) {           bool f = true;           for (int k = 1; k <= m; ++k) {             t1 = j;             t2 = k;             int tmp = abs(j - x) + abs(k - y);             ++b[tmp];             if (b[tmp] > a[tmp]) {               flag = false;               f = false;               break;             }           }           if (!f) break;         }         for (int j = 1; j <= n; ++j) {           bool f = true;           for (int k = 1; k <= m; ++k) {             int tmp = abs(j - x) + abs(k - y);             --b[tmp];             if (j == t1 && k == t2) {               f = false;               break;             }           }           if (!f) break;         }         if (flag) {           write(n);           putchar(   );           write(m);           putchar(  n );           write(x);           putchar(   );           write(y);           putchar(  n );           return 0;         }       }     write(-1);     putchar(  n );     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n;     cin >> n;     string s;     cin >> s;     int a[n], i, j, k, cnt = 1e9, tmp = 0;     for (i = 0; i < n; i++) cin >> a[i];     for (i = 0; i < s.size() - 1; i++) {       if (s[i] ==  R  && s[i + 1] ==  L ) {         tmp = (a[i + 1] - a[i]) / 2;         if (tmp < cnt) cnt = tmp;       }     }     if (cnt == 1e9)       cout << -1 << endl;     else       cout << cnt << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n, m, q;   const int max_w = 100;   vector<pair<int, int>> cnt_sort(vector<pair<int, int>>& a) {     vector<int> cnt(n * max_w + 5);     for (int i = 0; i < a.size(); ++i) {       ++cnt[a[i].first + 1];     }     for (int i = 1; i < cnt.size(); ++i) {       cnt[i] += cnt[i - 1];     }     vector<pair<int, int>> res(a.size());     for (int i = 0; i < a.size(); ++i) {       res[cnt[a[i].first]++] = a[i];     }     return res;   };   int main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     cin >> n >> m >> q;     vector<int> w(n);     for (int i = 0; i < n; ++i) {       cin >> w[i];     }     reverse(w.begin(), w.end());     vector<int> cnt(1 << n, 0);     string s;     for (int i = 0; i < m; ++i) {       cin >> s;       int res = 0;       for (int j = 0; j < n; ++j) {         res <<= 1;         res |= (s[j] ==  1 );       }       ++cnt[res];     }     vector<vector<pair<int, int>>> ans(1 << n, vector<pair<int, int>>());     vector<vector<int>> sum(1 << n, vector<int>(1 << n));     for (int i = 0; i < ans.size(); ++i) {       for (int j = 0; j < cnt.size(); ++j) {         if (cnt[j] == 0) continue;         int kok = i ^ j;         int res = 0;         for (int k = 0; k < n; ++k) {           if (!((kok >> k) & 1)) {             res += w[k];           }         }         ans[i].push_back({res, cnt[j]});       }       ans[i] = cnt_sort(ans[i]);       sum[i][0] = ans[i][0].second;       for (int j = 1; j < ans[i].size(); ++j) {         sum[i][j] = sum[i][j - 1] + ans[i][j].second;       }     }     int k;     while (q--) {       cin >> s >> k;       int res = 0;       for (int j = 0; j < n; ++j) {         res <<= 1;         res |= (s[j] ==  1 );       }       int j =           upper_bound(ans[res].begin(), ans[res].end(), make_pair(k, 100000000)) -           ans[res].begin();       --j;       if (j < 0) {         cout << 0;       } else {         cout << sum[res][j];       }       cout <<   n ;     }   }


#include <bits/stdc++.h>   using namespace std;   int p[1000006], n, k;   int main() {     ios::sync_with_stdio(false);     cin >> n >> k;     iota(p + 1, p + n + 1, 1);     for (int i = 2; i <= n; i++) {       if (p[i] == i) {         p[i]--;         for (int j = i + i; j <= n; j += i) p[j] = p[j] / i * (i - 1);       }     }     sort(p + 1, p + n + 1);     long long ans = 0;     for (int i = 1; i <= k + 2; i++) {       ans += p[i];     }     if (k == 1) ans--;     cout << ans <<   n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   struct Query {     int id, left, right, time, blockLeft, blockRight;     Query()         : id(-1), left(-1), right(-1), time(-1), blockLeft(-1), blockRight(-1) {}     Query(int id, int left, int right, int time, int blockLeft, int blockRight)         : id(id),           left(left),           right(right),           time(time),           blockLeft(blockLeft),           blockRight(blockRight) {}     bool operator<(const Query &otherQuery) const {       if (blockLeft != otherQuery.blockLeft) {         return blockLeft < otherQuery.blockLeft;       }       if (blockRight != otherQuery.blockRight) {         return blockRight < otherQuery.blockRight;       }       return time < otherQuery.time;     }   };   struct Operation {     int pos, oldValue, newValue;     Operation() : pos(-1), oldValue(-1), newValue(-1) {}     Operation(int pos, int oldValue, int newValue)         : pos(pos), oldValue(oldValue), newValue(newValue) {}   };   void del(int value, int *cnts, int *saveValues) {     if (cnts[value] > 0) saveValues[cnts[value]]--;     cnts[value]--;     if (cnts[value] > 0) saveValues[cnts[value]]++;   }   void add(int value, int *cnts, int *saveValues) {     if (cnts[value] > 0) saveValues[cnts[value]]--;     cnts[value]++;     if (cnts[value] > 0) saveValues[cnts[value]]++;   }   int main() {     ios::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     int n, q;     cin >> n >> q;     int sqrtValue = pow(n, 2.0 / 3.0), *values = new int[n + 1],         *tmpValues = new int[n + 1];     if (sqrtValue == 0) sqrtValue = 1;     vector<int> disValues;     for (int i = 1; i <= n; i++) {       cin >> values[i];       tmpValues[i] = values[i];       disValues.push_back(values[i]);     }     Query *querys = new Query[q + 1];     Operation *opts = new Operation[q + 1];     int cntQuerys = 0, cntOpts = 0;     for (int i = 1; i <= q; i++) {       int opt;       cin >> opt;       if (opt == 1) {         int left, right;         cin >> left >> right;         ++cntQuerys;         querys[cntQuerys] = Query(cntQuerys, left, right, cntOpts,                                   left / sqrtValue, right / sqrtValue);       } else {         int pos, value;         cin >> pos >> value;         ++cntOpts;         opts[cntOpts] = Operation(pos, tmpValues[pos], value);         tmpValues[pos] = value;         disValues.push_back(value);       }     }     sort(disValues.begin(), disValues.end());     disValues.resize(unique(disValues.begin(), disValues.end()) -                      disValues.begin());     for (int i = 1; i <= cntOpts; i++) {       Operation &tmpOpt = opts[i];       tmpOpt.oldValue =           lower_bound(disValues.begin(), disValues.end(), tmpOpt.oldValue) -           disValues.begin();       tmpOpt.newValue =           lower_bound(disValues.begin(), disValues.end(), tmpOpt.newValue) -           disValues.begin();     }     for (int i = 1; i <= n; i++) {       values[i] = lower_bound(disValues.begin(), disValues.end(), values[i]) -                   disValues.begin();     }     int valueSize = static_cast<int>(disValues.size()),         *cnts = new int[valueSize + 3], *saveValues = new int[n + 2];     memset(cnts, 0, sizeof(int) * (valueSize + 3));     memset(saveValues, 0, sizeof(int) * (n + 2));     int *results = new int[cntQuerys + 1];     sort(querys + 1, querys + cntQuerys + 1);     int l = 1, r = 0, time = 0;     for (int i = 1; i <= cntQuerys; i++) {       Query &tmpQuery = querys[i];       while (l > tmpQuery.left) {         l--;         add(values[l], cnts, saveValues);       }       while (l < tmpQuery.left) {         del(values[l], cnts, saveValues);         l++;       }       while (r > tmpQuery.right) {         del(values[r], cnts, saveValues);         r--;       }       while (r < tmpQuery.right) {         r++;         add(values[r], cnts, saveValues);       }       while (time < tmpQuery.time) {         time++;         Operation &tmpOpt = opts[time];         if (tmpOpt.pos >= l && tmpOpt.pos <= r) {           del(tmpOpt.oldValue, cnts, saveValues);           add(tmpOpt.newValue, cnts, saveValues);         }         values[tmpOpt.pos] = tmpOpt.newValue;       }       while (time > tmpQuery.time) {         Operation &tmpOpt = opts[time];         if (tmpOpt.pos >= l && tmpOpt.pos <= r) {           del(tmpOpt.newValue, cnts, saveValues);           add(tmpOpt.oldValue, cnts, saveValues);         }         values[tmpOpt.pos] = tmpOpt.oldValue;         time--;       }       for (int i = 1;; i++) {         if (i > n) {           results[tmpQuery.id] = 0;         }         if (saveValues[i] == 0) {           results[tmpQuery.id] = i;           break;         }       }     }     for (int i = 1; i <= cntQuerys; i++) {       cout << results[i] << endl;     }     return 0;   }


#include <bits/stdc++.h>   const int N = 200005;   const int M = 500005;   using namespace std;   pair<int, int> a[N];   int p[20][M];   int main() {     int n, m;     scanf( %d%d , &n, &m);     int i, j;     for (i = 0; i < n; i++) {       scanf( %d%d , &a[i].first, &a[i].second);     }     sort(a, a + n);     memset(p, -1, sizeof(p));     int maxe = -1;     j = 0;     for (i = 0; i < M; i++) {       while (j < n && a[j].first <= i) {         if (a[j].second > maxe) maxe = a[j].second;         j++;       }       if (maxe >= i)         p[0][i] = maxe;       else         p[0][i] = -1;     }     for (j = 0; j < 19; j++)       for (i = 0; i < M; i++)         if (p[j][i] == -1)           p[j + 1][i] = -1;         else           p[j + 1][i] = p[j][p[j][i]];     int u, v;     for (i = 0; i < m; i++) {       scanf( %d%d , &u, &v);       int ans = 0;       for (j = 19; j >= 0; j--)         if (p[j][u] < v) {           u = p[j][u];           ans += 1 << j;         }       u = p[0][u];       ans++;       if (u >= v)         printf( %d n , ans);       else         printf( -1 n );     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n, m, t;   int main() {     cin >> t;     while (t--) {       int s = 0, a;       cin >> n >> m;       for (int i = 1; i <= n; i++) {         cin >> a;         s += 2 * a;       }       if (m < n || n < 3) {         cout << -1 << endl;         continue;       }       cout << s << endl;       for (int i = 1; i < n; i++) cout << i <<     << i + 1 << endl;       cout << n <<     << 1 << endl;     }   }


#include <bits/stdc++.h>   using namespace std;   void solve();   int main() {     ios::sync_with_stdio(0);     solve();     return 0;   }   list<pair<deque<int>, int> > basket;   int N, bN;   int p[100001];   void update(pair<deque<int>, int>& it) {     it.second = it.first[0];     for (int i = 0; i < (int)((it.first).size()); i++) {       it.second = max(it.second, it.first[i]);     }   }   void solve() {     cin >> N;     bN = 250;     basket.push_back(pair<deque<int>, int>(deque<int>(), 0));     for (int i = 0; i < N; i++) {       int a, c;       cin >> a >> c;       p[a] = i + 1;       int cc = c;       list<pair<deque<int>, int> >::iterator it = basket.begin();       for (; it != basket.end(); it++) {         if (it->second > a || (int)it->first.size() > cc) {           break;         }         cc -= it->first.size();       }       if (it == basket.end()) {         it--;         cc += it->first.size();       }       deque<int>& b = it->first;       b.push_front(a);       for (int j = 0; j < (int)((b).size()) - 1 && cc > 0; j++, cc--) {         if (b[j] < b[j + 1]) break;         swap(b[j], b[j + 1]);       }       if ((int)((b).size()) >= 2 * bN) {         pair<deque<int>, int> item(deque<int>(), 0);         while ((int)((b).size()) > bN) {           item.first.push_back(b.front());           b.pop_front();         }         update(*it);         update(item);         basket.insert(it, item);       } else {         update(*it);       }     }     vector<int> ans;     for (list<pair<deque<int>, int> >::iterator it = basket.begin();          it != basket.end(); it++) {       deque<int>& b = it->first;       for (int i = 0; i < (int)((b).size()); i++) {         ans.push_back(p[b[i]]);       }     }     reverse(ans.begin(), ans.end());     for (int i = 0; i < (int)((ans).size()); i++) cout << ans[i] <<    ;   }


#include <bits/stdc++.h>   using namespace std;   const int maxn = 5005;   const int maxm = maxn * 2;   const int maxe = 200005;   const int mod = 1e9 + 7;   int n, a, b;   string str;   int cc[26][maxn][26];   int exist[26];   int main() {     cin >> str;     n = str.length();     for (int i = 0; i < n; i++) {       int idx = str[i] -  a ;       exist[idx]++;       for (int j = 1; j < n; j++) {         int pos = (i + j) % n;         int idx2 = str[pos] -  a ;         cc[idx][j][idx2]++;       }     }     double ans = 0;     for (int i = 0; i < 26; i++) {       double cccc = 0;       for (int j = 1; j < n; j++) {         double tp = 0;         for (int ii = 0; ii <= n; ii++) {           if (str[ii] -  a  != i) continue;           int pos = (ii + j) % n;           int idx2 = str[pos] -  a ;           if (cc[i][j][idx2] == 1) tp++;         }         cccc = max(cccc, tp);       }       ans += cccc;     }     printf( %.15lf n , ans / n);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     long long n, m, k;     scanf( %lld%lld%lld , &n, &m, &k);     if (n % 2 == 0) {       printf( Marsel n );       return 0;     }     if (n % 2 == 1) {       if (k == 1 && m > 1) {         printf( Timur n );         return 0;       }       for (long long a = 2; a <= sqrt(m); a++)         if (m % a == 0)           if ((m / a) >= k) {             printf( Timur n );             return 0;           }     }     printf( Marsel n );     return 0;   }


#include <bits/stdc++.h>   using namespace std;   #pragma comment(linker,  /STACK:33554432 )   int N, Q, cnt[3][100005];   char A[100005];   int main() {     int i, j, k;     scanf( %s , A + 1);     N = strlen(A + 1);     for (i = 1; i <= N; i++) {       for (j = 0; j < 3; j++) cnt[j][i] += cnt[j][i - 1];       cnt[A[i] -  x ][i]++;     }     for (scanf( %d , &Q); Q--;) {       int c[3] =           {               0,           },           s, e;       scanf( %d%d , &s, &e);       for (i = 0; i < 3; i++) c[i] = cnt[i][e] - cnt[i][s - 1];       int mx = 0, mn = 1e9;       for (i = 0; i < 3; i++) mx = max(mx, c[i]), mn = min(mn, c[i]);       if (e - s + 1 < 3 || mx - mn < 2)         puts( YES );       else         puts( NO );     }   }


#include <bits/stdc++.h>   using namespace std;   long long n, m, i, j, k, it, t;   long long r[101], rr[101], c[101], cc[101];   long long a[101][101];   long long R, C, x;   vector<long long> ans, ans2;   int main() {     cin >> n >> m;     int k = 1;     x = 3;     while (n != 0) {       if (n % 2 == 1) {         k = k * x % m;       }       x = x * x % m;       n = n / 2;     }     cout << (k - 1 + m) % m;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   double pi = 3.1415926536;   int main() {     int n;     cin >> n;     int r[n];     for (int i = 0; i < n; i++) cin >> r[i];     double area = 0;     sort(r, r + n);     for (int i = n - 1; i >= 0; i -= 2) {       if (i > 0)         area += (r[i] * r[i] - r[i - 1] * r[i - 1]);       else         area += r[0] * r[0];     }     area *= pi;     printf( %0.10lf , area);     return 0;   }


#include <bits/stdc++.h>   const bool DEBUG = false;   using namespace std;   const long long maxn = 1e5 + 2;   const long long INF = 4e18;   const long long M = 1e9 + 7;   const int lg = 21;   const int z = 256 * 256;   int n, m, a[maxn], t[maxn], dp[maxn];   string s;   vector<int> g[2], v[2];   bool f(vector<int>(&v), int l, int r) {     if (lower_bound(v.begin(), v.end(), l) == v.end()) return 1;     int l1 = lower_bound(v.begin(), v.end(), l) - v.begin();     if (lower_bound(v.begin(), v.end(), r + 1) == v.end()) return 0;     int l2 = lower_bound(v.begin(), v.end(), r + 1) - v.begin();     if (l1 == l2) return 1;     return 0;   }   bool check(int l, int r) {     if (l % 2 == 0)       if (f(v[1], l, r) and f(g[0], l, r)) return 1;     if (l % 2 == 1)       if (f(v[0], l, r) and f(g[1], l, r)) return 1;     return 0;   }   int par(int l, int r) {     if (l == 0) return a[r];     return a[r] - a[l - 1];   }   int main() {     ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);     cin >> n >> s >> m;     a[0] = bool(s[0] ==  ? );     for (int i = 1; i < n; i++) a[i] = a[i - 1] + bool(s[i] ==  ? );     for (int i = 0; i < n; i++)       if (s[i] ==  a )         v[(i % 2)].push_back(i);       else if (s[i] ==  b )         g[(i % 2)].push_back(i);     t[n - m] = check(n - m, n - 1);     for (int i = n - m - 1; i >= 0; i--) {       if (check(i, i + m - 1))         t[i] = 1 + t[i + m];       else         t[i] = t[i + 1];     }     int maxx = t[0];     if (t[n - m] == 1) dp[n - m] = par(n - m, n - 1);     for (int i = n - m - 1; i >= 0; i--) {       if (check(i, i + m - 1)) {         dp[i] = par(i, i + m - 1) + dp[i + m];         if (t[i + 1] == t[i]) dp[i] = min(dp[i], dp[i + 1]);       } else         dp[i] = dp[i + 1];     }     cout << dp[0] << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long GCD(long long a, long long b) { return b == 0 ? a : GCD(b, a % b); }   const long long inf = 0x7f7f7f7f;   const long long mod = 1e9 + 7;   double pi = acos(-1.0);   long long a[120];   int main() {     ios::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     long long n, m, q;     cin >> n >> m >> q;     long long t = GCD(n, m);     n /= t;     m /= t;     long long x1, y1, x2, y2;     for (int i = 1; i <= q; i++) {       cin >> x1 >> y1 >> x2 >> y2;       if (t == 1) {         cout <<  YES  << endl;         continue;       }       long long p1, p2;       if (x1 == 1) {         p1 = y1 / n;         if (y1 % n) p1++;       } else {         p1 = y1 / m;         if (y1 % m) p1++;       }       if (x2 == 1) {         p2 = y2 / n;         if (y2 % n) p2++;       } else {         p2 = y2 / m;         if (y2 % m) p2++;       }       if (p1 == p2)         cout <<  YES  << endl;       else         cout <<  NO  << endl;     }   }


#include <bits/stdc++.h>   using namespace std;   const long long inf = 1e9, maxn = 5e5 + 5, mod = 1e9 + 7;   long long x[maxn], y[maxn], sz[maxn], n, m;   set<long long> S;   unordered_map<long long, long long> cnt, id;   vector<long long> son[maxn];   long long gc(long long x) {     long long ret = 0, j = 0;     while (x <= n) {       ret += min(x + (1 << j) - 1, n) - x + 1;       x <<= 1;       ++j;       ret %= mod;     }     return ret;   }   void dfs(long long x) {     if (S.count(x)) {       cnt[x] = 1;       if ((x << 1) <= n) dfs(x << 1);       if ((x << 1 | 1) <= n) dfs(x << 1 | 1);     } else       cnt[x] = gc(x);   }   long long vis[maxn];   long long dfs2(long long x) {     vis[x] = 1;     long long ret = sz[x];     for (auto js : son[x])       if (!vis[js]) {         (ret += dfs2(js)) %= mod;       }     vis[x] = 0;     return ret;   }   signed main() {     scanf( %lld%lld , &n, &m);     if (m == 0) return printf( %lld n , 1ll * n * n % mod), 0;     for (long long i = 1; i <= m; i++) {       scanf( %lld%lld , &x[i], &y[i]);       long long tmp = x[i];       while (tmp) {         S.insert(tmp);         tmp >>= 1;       }       tmp = y[i];       while (tmp) {         S.insert(tmp);         tmp >>= 1;       }     }     S.insert(1);     dfs(1);     long long idc = 0;     for (auto i : cnt) id[i.first] = ++idc;     for (auto i : cnt) {       long long u = id[i.first];       sz[u] = i.second;       for (long long j = 0; j <= 1; j++) {         long long v = i.first << 1 | j;         if (!cnt.count(v)) continue;         v = id[v];         son[u].push_back(v);         son[v].push_back(u);       }     }     for (long long i = 1; i <= m; i++)       son[id[x[i]]].push_back(id[y[i]]), son[id[y[i]]].push_back(id[x[i]]);     long long ans = 0;     for (long long i = 1; i <= idc; i++)       ans = (ans + 1ll * sz[i] * sz[i] % mod) % mod;     for (long long i = 1; i <= idc; i++) {       vis[i] = 1;       long long cura = 0;       for (auto js : son[i]) {         cura = (cura + dfs2(js)) % mod;       }       ans = (ans + 1ll * cura * sz[i] % mod) % mod;       vis[i] = 0;     }     printf( %lld n , ans);     return 0;   }


#include <bits/stdc++.h>   int main() {     int n, a, b, c = 0;     scanf( %d , &n);     while (n--) {       scanf( %d , &a);       c += a != b;       b = a;     }     printf( %d , c);   }


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


#include <bits/stdc++.h>   using namespace std;   const int maxn = 100000 + 100;   pair<int, int> khar[maxn];   bool mk[10 * maxn];   int main() {     int n;     cin >> n;     for (int i = 0; i < n; i++) {       int a, b;       cin >> a >> b;       mk[a] = true;       khar[i] = pair<int, int>(a, b);     }     sort(khar, khar + n);     int k = khar[n - 1].first;     int x[k + 1];     fill(x, x + k + 1, 0);     if (mk[0]) x[0] = 1;     for (int i = 1; i <= k; i++) {       if (mk[i])         x[i] += x[i - 1] + 1;       else         x[i] += x[i - 1];     }     int h[n];     fill(h, h + n, 0);     for (int i = 1; i < n; i++) {       if (khar[i].first - khar[i].second <= 0) {         h[i] = i;         continue;       }       h[i] = x[khar[i].first - 1] - x[khar[i].first - khar[i].second - 1];     }     int g[n];     fill(g, g + n, 0);     for (int i = 1; i < n; i++) {       if (khar[i].first - khar[i].second <= 0) {         g[i] = i;         continue;       }       if (h[i] == 0)         g[i] = g[i - 1];       else         g[i] = h[i] + g[i - h[i] - 1];     }     for (int i = 0; i < n; i++) g[i] += n - i - 1;     sort(g, g + n);     cout << g[0] << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int dirx[] = {-1, -1, -1, 0, 0, 1, 1, 1};   int diry[] = {-1, 0, 1, -1, 1, -1, 0, 1};   int dx[] = {-1, 1, 0, 0};   int dy[] = {0, 0, -1, 1};   class SorT {    public:     bool operator()(pair<int, int>& p1, pair<int, int>& p2) {       return p1.second < p2.second;     }   };   int ans[1000010];   int main() {     string s;     cin >> s;     int left = 0, right = s.size() - 1;     for (int i = 0; i < s.size(); i++) {       if (s[i] ==  l )         ans[right--] = i + 1;       else         ans[left++] = i + 1;     }     for (int i = 0; i < s.size(); i++) {       printf( %d n , ans[i]);     }   }


#include <bits/stdc++.h>   using namespace std;   long long int mod = 1e9 + 7;   void solve() {     long long int n;     cin >> n;     long long int a[n];     long long int hi = 0;     for (long long int i = 0; i < n; ++i) {       cin >> a[i];       hi += a[i];     }     sort(a, a + n);     long long int ans = hi - n;     if (n > 50) {       cout << ans;       return;     }     for (long long int i = 1; i < 1e9 + 1; ++i) {       long long int c = i;       long long int tmp = 0;       long long int dig = 0;       while (c > 0) {         c = c / 10;         dig++;       }       long long int no = 1;       c = i;       bool ex = true;       for (long long int j = 0; j < n; ++j) {         tmp += abs(a[j] - no);         if ((no * c) > 1e14 || (no * c) < 0) {           ex = false;           break;         }         long long int digi = 0, yo = no;         while (yo > 0) {           yo = yo / 10;           digi++;         }         if (digi + dig > 16) {           ex = false;           break;         }         no = no * c;       }       if (ex)         ans = min(ans, tmp);       else {         break;       }     }     cout << ans;   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     long long int t = 1;     while (t--) {       solve();     }     cerr <<  Time elapsed:   << 1000 * clock() / CLOCKS_PER_SEC <<  ms n ;   }


#include <bits/stdc++.h>   using namespace std;   long long n, x, y;   long long bs1(long long l, long long r, long long num) {     long long mid = (l + r) / 2;     if (r - l <= 0) {       return -1;     }     long long time = mid * x;     long long highnow = mid + ((mid * x + x - 1) / y);     long long lownow = mid + ((time) / y);     if (highnow >= num && lownow < num) {       return mid;     }     if (highnow < num) {       return bs1(mid + 1, r, num);     }     return bs1(l, mid, num);   }   long long bs2(long long l, long long r, long long num) {     long long mid = (l + r) / 2;     if (r - l <= 0) {       return -1;     }     long long time = mid * y;     long long highnow = mid + ((mid * y + y - 1) / x);     long long lownow = mid + ((time) / x);     if (highnow >= num && lownow < num) {       return mid;     }     if (highnow < num) {       return bs2(mid + 1, r, num);     }     return bs2(l, mid, num);   }   int main() {     cin >> n >> x >> y;     for (int i = 0; i < n; i++) {       long long num;       cin >> num;       long long ansy = bs1(0, num + 1, num);       long long ansx = bs2(0, num + 1, num);       if (ansy == -1 && ansx == -1) {         cout <<  Both  << endl;         continue;       }       if (ansx == -1) {         cout <<  Vanya  << endl;         continue;       }       if (ansy == -1) {         cout <<  Vova  << endl;         continue;       }       cout <<  Both  << endl;     }   }


#include <bits/stdc++.h>   using namespace std;   using ll = long long;   using ii = pair<int, int>;   const int mod = 1e9 + 7;   int v[200005];   int n, k;   bool guess(int x) {     ll spent = x - v[n / 2];     for (int i = n / 2 + 1; i < n; i++) {       spent += max(0, x - v[i]);       if (spent > (ll)k) return false;     }     return spent <= (ll)k;   }   int solve() {     int x = -1;     for (int b = v[n - 1] + k; b >= 1; b /= 2)       while (guess(x + b)) x += b;     return x;   }   int main() {     ios::sync_with_stdio(false);     cin.tie(0);     cin >> n >> k;     for (int i = 0; i < n; i++) cin >> v[i];     sort(v, v + n);     cout << solve() <<   n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int maxn = 100001;   vector<pair<long long, int>> g[maxn];   vector<pair<long long, int>> _g[maxn];   int n, m, s, t;   int u[maxn], v[maxn];   long long c[maxn];   void enter() {     cin >> n >> m >> s >> t;     for (int i = (0), _b = (m); i < _b; ++i) {       cin >> u[i] >> v[i] >> c[i];       g[u[i]].push_back(pair<long long, int>(c[i], v[i]));       _g[v[i]].push_back(pair<long long, int>(c[i], u[i]));     }   }   const long long mod = 1000000321;   long long f1[maxn];   long long c1[maxn];   long long c2[maxn];   long long f2[maxn];   bool d[maxn];   const long long oo = 1000000000000123;   void dijkstra1() {     for (int i = (1), _b = (n + 1); i < _b; ++i) f1[i] = oo;     f1[s] = 0;     c1[s] = 1;     priority_queue<pair<long long, int>, vector<pair<long long, int>>,                    greater<pair<long long, int>>>         heap;     heap.push(pair<long long, int>(0, s));     while (!heap.empty()) {       pair<long long, int> q = heap.top();       heap.pop();       if (!d[q.second]) {         d[q.second] = true;         f1[q.second] = q.first;         for (auto i : g[q.second])           if (!d[i.second]) {             if (q.first + i.first < f1[i.second]) {               f1[i.second] = q.first + i.first;               c1[i.second] = c1[q.second];               heap.push(pair<long long, int>(f1[i.second], i.second));             } else if (q.first + i.first == f1[i.second]) {               c1[i.second] += c1[q.second];               c1[i.second] = c1[i.second] % mod;             }           }       }     }   }   void dijkstra2() {     for (int i = (1), _b = (n + 1); i < _b; ++i) {       f2[i] = oo;       d[i] = false;     }     f2[t] = 0;     c2[t] = 1;     priority_queue<pair<long long, int>, vector<pair<long long, int>>,                    greater<pair<long long, int>>>         heap;     heap.push(pair<long long, int>(0, t));     while (!heap.empty()) {       pair<long long, int> q = heap.top();       heap.pop();       if (!d[q.second]) {         d[q.second] = true;         f2[q.second] = q.first;         for (auto i : _g[q.second])           if (!d[i.second]) {             if (q.first + i.first < f2[i.second]) {               f2[i.second] = q.first + i.first;               c2[i.second] = c2[q.second];               heap.push(pair<long long, int>(f2[i.second], i.second));             } else if (q.first + i.first == f2[i.second]) {               c2[i.second] += c2[q.second];               c2[i.second] = c2[i.second] % mod;             }           }       }     }   }   long long cnp(long long l, long long r, int i) {     if (l == r)       return l;     else {       long long mid = ((l + r) / 2) + 1;       if (f1[u[i]] + f2[v[i]] + mid < f1[t])         return cnp(mid, r, i);       else         return cnp(l, mid - 1, i);     }   }   void solve() {     for (int i = (0), _b = (m); i < _b; ++i) {       if (f1[u[i]] + c[i] + f2[v[i]] == f1[t] &&           (c1[u[i]] * c2[v[i]]) % mod == c1[t])         cout <<  YES n ;       else {         long long ans = cnp(0, c[i], i);         if (ans == 0)           cout <<  NO n ;         else           cout <<  CAN   << c[i] - ans <<   n ;       }     }   }   int main() {     ios_base::sync_with_stdio(0);     cin.tie();     cout.tie();     enter();     dijkstra1();     dijkstra2();     solve();   }


#include <bits/stdc++.h>   using namespace std;   const int N = 2e5 + 100, LEN = 21, M = 1 << LEN, inF = N * 30;   int n, len, ps[N], dp0[N], dp1[N], mini[M][LEN];   string s[N];   void _min(int &a, int b) { a = min(a, b); }   int same(string a, string b) {     int A = 0, B = 0, res = 0;     for (int i = 0; i < len; i++) {       A |= (a[len - i - 1] -  0 ) << i;       B = (B << 1) | (b[i] -  0 );       if (A == B) res = i + 1;     }     return res;   }   int dif(string a, string b) { return len - same(a, b); }   void add(int dex) {     _min(mini[0][0], dp1[dex] - ps[dex] + len - 0);     int val = 0;     for (int i = 1; i <= len; i++)       val |= (s[dex - 1][len - i] -  0 ) << (i - 1),           _min(mini[val][i], dp1[dex] - ps[dex] + len - i);   }   void find_dp() {     dp0[0] = dp1[0] = len;     for (int i = 1; i < n; i++) {       dp0[i] = min(dp0[i - 1], dp1[i - 1]) + dif(s[i - 1], s[i]);       dp1[i] = min((i == 1 ? len << 1 : dp1[i - 1] + dif(s[i - 2], s[i])),                    ps[i - 1] + len);       int val = 0;       _min(dp1[i], mini[val][0] + ps[i - 1]);       for (int j = 1; j <= len; j++)         val = (val << 1) | (s[i][j - 1] -  0 ),         _min(dp1[i], mini[val][j] + ps[i - 1]);       if (i != n - 1) add(i);     }   }   int main() {     for (int i = 0; i < M; i++) fill(mini[i], mini[i] + LEN, inF);     cin >> n;     for (int i = 0; i < n; i++) {       cin >> s[i];       ps[i] = (i ? ps[i - 1] + dif(s[i - 1], s[i]) : len = s[i].length());     }     find_dp();     cout << min(dp0[n - 1], dp1[n - 1]);     return 0;   }