#include <bits/stdc++.h>   using namespace std;   const int iinf = 1e9 + 7;   const long long linf = 1ll << 60;   const double dinf = 1e60;   template <typename T>   inline void scf(T &x) {     bool f = 0;     x = 0;     char c = getchar();     while ((c <  0  || c >  9 ) && c !=  - ) c = getchar();     if (c ==  - ) {       f = 1;       c = getchar();     }     while (c >=  0  && c <=  9 ) {       x = x * 10 + c -  0 ;       c = getchar();     }     if (f) x = -x;     return;   }   template <typename T1, typename T2>   void scf(T1 &x, T2 &y) {     scf(x);     return scf(y);   }   template <typename T1, typename T2, typename T3>   void scf(T1 &x, T2 &y, T3 &z) {     scf(x);     scf(y);     return scf(z);   }   template <typename T1, typename T2, typename T3, typename T4>   void scf(T1 &x, T2 &y, T3 &z, T4 &w) {     scf(x);     scf(y);     scf(z);     return scf(w);   }   inline char mygetchar() {     char c = getchar();     while (c ==     || c ==   n ) c = getchar();     return c;   }   template <typename T>   void chkmax(T &x, const T &y) {     if (y > x) x = y;     return;   }   template <typename T>   void chkmin(T &x, const T &y) {     if (y < x) x = y;     return;   }   void TZL();   void RANK1();   int main() {     TZL();     RANK1();     return 0;   }   void TZL() {     long long k;     int n;     vector<long long> a;     a.clear();     scf(n, k);     for (int i = 0, _end_ = (n); i < _end_; ++i) {       long long x;       scf(x);       a.push_back(x);     }     sort(a.begin(), a.end());     long long ans = 0;     for (int i = 1; i < n; ++i) {       long long x = a[i] - a[0];       if (x % k != 0) {         puts( -1 );         return;       }       ans += x / k;       ;     }     cout << ans << endl;   }   void RANK1() {}


#include <bits/stdc++.h>   using namespace std;   namespace solve {   long long int a[200007], sum[200007], dpDown[200007], dpUp[200007];   vector<int> graph[200007];   void rec(int u, int p) {     sum[u] = a[u];     for (int v : graph[u]) {       if (v == p) continue;       rec(v, u);       dpDown[u] += dpDown[v] + sum[v];       sum[u] += sum[v];     }   }   void dfs(int u, int p) {     for (int v : graph[u]) {       if (v == p) continue;       dpUp[v] = dpDown[u] - dpDown[v] - sum[v] + (sum[1] - sum[v]) + dpUp[u];       dfs(v, u);     }   }   void takeInput() {     ios_base::sync_with_stdio(false);     int n;     cin >> n;     for (int i = 1; i <= n; i++) {       cin >> a[i];     }     for (int i = 1; i < n; i++) {       int u, v;       cin >> u >> v;       graph[u].push_back(v);       graph[v].push_back(u);     }     rec(1, -1);     dfs(1, -1);     long long int res = 0;     for (int i = 1; i <= n; i++) {       res = max(res, dpDown[i] + dpUp[i]);     }     cout << res << endl;   }   }  // namespace solve   int main() {     solve::takeInput();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int N;   string board[30];   int mask[42][30];   int dp[42][1 << 20];   int func(int i, int j, int c, bool first) {     if (dp[i][j] != 10000) return dp[i][j];     int next;     int ans = -(1 << 29);     if (i == 2 * (N - 1)) {       ans = 0;     } else {       for ((next) = 0; (next) < (int)(26); (next)++) {         int i2 = i + 1;         int j2 = (mask[i + 1][next] & (j | (j << 1)));         if (j2 != 0) ans = max(ans, -func(i2, j2, next, !first));       }     }     if (first) {       if (c == 0)         ans++;       else if (c == 1)         ans--;     } else {       if (c == 0)         ans--;       else if (c == 1)         ans++;     }     return dp[i][j] = ans;   }   string calc() {     int i, j, k;     for ((i) = 0; (i) < (int)(2 * N); (i)++)       for ((j) = 0; (j) < (int)(26); (j)++)         for ((k) = 0; (k) < (int)(N); (k)++) {           int x = k;           int y = i - x;           if (y >= 0 && y < N && board[x][y] -  a  == j) mask[i][j] |= (1 << k);         }     for ((i) = 0; (i) < (int)(2 * N); (i)++)       for ((j) = 0; (j) < (int)((1 << N)); (j)++) dp[i][j] = 10000;     int ans = func(0, 1, board[0][0] -  a , false);     if (ans < 0) return  FIRST ;     if (ans > 0) return  SECOND ;     return  DRAW ;   }   void solve(istream &in, ostream &out) {     in >> N;     for (int i = 0; i < N; i++) in >> board[i];     out << calc() << endl;   }   int main() {     solve(cin, cout);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long in() {     long long re = 0, f = 1;     char x = getchar();     while (x <  0  || x >  9 ) {       if (x ==  - ) f = -1;       x = getchar();     }     while (x >=  0  && x <=  9 ) re = re * 10 + x - 48, x = getchar();     return re * f;   }   void out(long long x) {     if (x < 0) putchar( - ), x = -x;     if (!x) {       putchar( 0 );       return;     }     char z[21], ct = 0;     while (x) {       z[++ct] = x % 10 + 48, x /= 10;     }     while (ct) {       putchar(z[ct]);       --ct;     }   }   const long long mod1 = 1e9 + 7;   const long long mod2 = 998244353;   const long long base1 = 3001;   const long long base2 = 2333;   long long upd(long long a, long long b) { return a < 0 ? a + b : a; }   struct ull {     long long x, y;     ull operator-(const ull &b) const {       return (ull){upd(x - b.x, mod1), upd(y - b.y, mod2)};     }     ull operator+(const long long &b) const {       return (ull){(x + b) % mod1, (y + b) % mod2};     }     ull operator+(const ull &b) const {       return (ull){(x + b.x) % mod1, (y + b.y) % mod2};     }     ull operator*(const ull &b) const {       return (ull){(x * b.x) % mod1, (y * b.y) % mod2};     }     bool operator==(const ull &b) const { return x == b.x && y == b.y; }     bool operator!=(const ull &b) const { return x != b.x || y != b.y; }   } base;   ull hs[1000010], pw[1000010];   ull geths(int l, int r) {     int len = r - l + 1;     ull re = hs[r] - hs[l - 1] * pw[len];     return re;   }   long long ls[200010], lt[200010];   char cs[200010], ct[200010];   long long S[500000], T[500000];   int n, m;   int main() {     n = in();     m = in();     for (int i = 1; i <= n; ++i) {       scanf( %d-%c , &ls[i], &cs[i]);       if (cs[i] == cs[i - 1]) {         ls[i - 1] += ls[i];         --i;         --n;       }     }     for (int i = 1; i <= m; ++i) {       scanf( %d-%c , &lt[i], &ct[i]);       if (ct[i] == ct[i - 1]) {         lt[i - 1] += lt[i];         --i;         --m;       }     }     if (m == 1) {       char c = ct[1];       long long ans = 0;       for (int i = 1; i <= n; ++i)         if (cs[i] == c) ans = ans + max(0ll, ls[i] - lt[1] + 1);       cout << ans <<   n ;       return 0;     }     base = (ull){base1, base2};     int cnt1 = lt[1], cnt2 = lt[m], ch = ct[m];     for (int i = 1; i <= n; ++i) S[i * 2 - 1] = ls[i], S[i * 2] = cs[i];     T[1] = ct[1];     for (int i = 2; i < m; ++i) T[i * 2 - 2] = lt[i], T[i * 2 - 1] = ct[i];     int tlen = m * 2 - 3;     ull hT = (ull){0, 0};     for (int i = 1; i <= tlen; ++i) hT = hT * base + (long long)T[i];     pw[0].x = pw[0].y = 1;     for (int ed = max(tlen, n * 2), i = 1; i <= ed; ++i) pw[i] = pw[i - 1] * base;     for (int i = 1; i <= n * 2; ++i) hs[i] = hs[i - 1] * base + (long long)S[i];     long long ans = 0;     for (int i = 2; i + tlen + 1 <= n * 2; i += 2) {       int l = i, r = i + tlen - 1;       ull hS = geths(l, r);       if (hS == hT) {         if (S[l - 1] < cnt1) continue;         if (S[r + 1] < cnt2) continue;         if (S[r + 2] != ch) continue;         ++ans;       }     }     cout << ans <<   n ;   }


#include <bits/stdc++.h>   using namespace std;   const int maxn = 2e5 + 1;   vector<int> g[maxn];   int n, m, k, num[maxn], pre[maxn], maxx = 0, flag = 0;   void dfs(int u, int fa, int deep) {     num[u] = deep;     pre[u] = fa;     for (int to : g[u]) {       if (flag) return;       if (to == fa) continue;       if (num[to] > 0) {         if (num[u] - num[to] + 1 >= k + 1) {           printf( %d n , num[u] - num[to] + 1);           while (u != to) {             printf( %d  , u);             u = pre[u];           }           printf( %d , to);           flag = 1;           return;         }         continue;       }       dfs(to, u, deep + 1);     }   }   int main() {     scanf( %d%d%d , &n, &m, &k);     for (int i = 1, u, v; i <= m; ++i) {       scanf( %d%d , &u, &v);       g[u].push_back(v);       g[v].push_back(u);     }     dfs(1, 0, 1);   }


#include <bits/stdc++.h>   using namespace std;   int m, n, l, r, mid, t;   int a[200002], b[200002], ida[200002], idb[200002], to[200002];   long long ans[200002] = {};   template <class T>   void read(T &x) {     x = 0;     int f = 0;     char ch = getchar();     while (ch <  0  || ch >  9 ) f |= (ch ==  - ), ch = getchar();     while (ch >=  0  && ch <=  9 )       x = (x << 3) + (x << 1) + (ch ^ 48), ch = getchar();     x = f ? -x : x;     return;   }   inline bool cmpa(int x, int y) { return a[x] > a[y] ? 1 : 0; }   inline bool cmpb(int x, int y) { return b[x] > b[y] ? 1 : 0; }   inline void add(int l, int r, long long d) {     --l, r -= (l / n) * n, l %= n, ++l;     if (r <= n)       ans[l] += d, ans[r] -= d;     else       ans[l] += d, ans[1] += d, ans[(r - 1) % n + 1] -= d;   }   int main() {     read(m), read(n);     for (int i = 1; i <= n; ++i) read(a[i]), ida[i] = i;     for (int i = 1; i <= n; ++i) read(b[i]), idb[i] = i;     sort(ida + 1, ida + n + 1, cmpa), sort(idb + 1, idb + n + 1, cmpb);     for (int i = 1; i <= n; ++i) {       t = l = 1, r = n;       while (l <= r) {         mid = ((l + r) >> 1);         if (a[ida[i]] < b[idb[mid]] - m / 2)           l = mid + 1;         else           r = mid - 1;       }       add(t + n - i + 1, l + n - i + 1, m + a[ida[i]]), t = l, r = n;       while (l <= r) {         mid = ((l + r) >> 1);         if (a[ida[i]] < b[idb[mid]])           l = mid + 1;         else           r = mid - 1;       }       add(t + n - i + 1, l + n - i + 1, -a[ida[i]]), t = l, r = n;       while (l <= r) {         mid = ((l + r) >> 1);         if (a[ida[i]] <= b[idb[mid]] + m / 2)           l = mid + 1;         else           r = mid - 1;       }       add(t + n - i + 1, l + n - i + 1, a[ida[i]]),           add(l + n - i + 1, 2 * n - i + 2, m - a[ida[i]]), t = l = 1, r = n;       while (l <= r) {         mid = ((l + r) >> 1);         if (b[idb[i]] < a[ida[mid]] - m / 2)           l = mid + 1;         else           r = mid - 1;       }       add(n - l + i + 2, n - t + i + 2, b[idb[i]]), t = l, r = n;       while (l <= r) {         mid = ((l + r) >> 1);         if (b[idb[i]] <= a[ida[mid]])           l = mid + 1;         else           r = mid - 1;       }       add(n - l + i + 2, n - t + i + 2, -b[idb[i]]), t = l, r = n;       while (l <= r) {         mid = ((l + r) >> 1);         if (b[idb[i]] <= a[ida[mid]] + m / 2)           l = mid + 1;         else           r = mid - 1;       }       add(n - l + i + 2, n - t + i + 2, b[idb[i]]),           add(i + 1, n - l + i + 2, -b[idb[i]]);     }     t = 1;     for (int i = 2; i <= n; ++i)       if ((ans[i] += ans[i - 1]) < ans[t]) t = i;     for (int i = 1; i <= n; ++i) to[ida[i]] = idb[(i + t - 2) % n + 1];     printf( %lld n , ans[t]);     for (int i = 1; i <= n; ++i) printf( %d  , to[i]);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   using ll = long long;   using vi = vector<long long>;   using vpi = vector<pair<long long, long long>>;   using vvi = vector<vi>;   using tp = tuple<long long, long long, long long>;   using pi = pair<long long, long long>;   void __READ__() {     ios_base ::sync_with_stdio(false);     cin.tie(NULL);   }   template <typename T>   inline istream &operator>>(istream &_in, vector<T> &_v) {     for (auto &_i : _v) {       _in >> _i;     };     return _in;   }   template <typename T, typename U>   inline istream &operator>>(istream &_in, pair<T, U> &_p) {     _in >> _p.first >> _p.second;     return _in;   }   const long long nax = 1e5 + 7;   const long long mod = 1e9 + 7;   const long long inf = numeric_limits<long long>::max() - (long long)9e6;   void test() {     long long n;     cin >> n;     vi v(n);     cin >> v;     long long ans = inf;     map<long long, long long> m;     long long rsum = 0;     for (long long x : v) {       rsum += x;       m[rsum]++;       ans = min(ans, n - m[rsum]);     }     cout << ans;   }   signed main() {     __READ__();     long long T = 1;     for (long long tt = 1; tt <= T; tt++) {       test();       cout <<   n ;     }     return 0;   }


#include<cstdio>   #include<cstring>   #include<algorithm>   #include<iostream>   #include<string>   #include<vector>   #include<stack>   #include<bitset>   #include<cstdlib>   #include<cmath>   #include<set>   #include<list>   #include<deque>   #include<map>   #include<queue>      #define rep(i, a, n) for (ll  i=a;i<=n;i++)   #define per(i, a, n) for (ll  i=a;i>=n;i--)   #define IOS ios::sync_with_stdio(false);cin.tie(0); cout.tie(0)   #pragma GCC optimize(3)//O3   #pragma GCC optimize(2)//O2   //#define endl   n    #define Mod(x) x % mod   #define toNum(x) x - 0    #define fi first   #define se second   #define mm(a, b) memset(a,b, sizeof(a));   #define dbg(x) cout << #x <<  =  << x << endl;   using namespace std;   typedef long long ll;   typedef unsigned long long ull;   typedef pair<ll, ll> PII;   const ll maxn = 3e6 + 10;   const ll mod = 1e9 + 7;   const ll N = 1e6 + 10;   const ll INF = 0x3f3f3f3f;   #define cf ll  t;cin>>t;while (t--) solve();   int n, m, sum, cnk, maxx, minn = INF, u, l, r, pre, op, p, k, z, ok, x, y, temp, q, sum1, sum2, A, B, cnt, ans, b, ask, t, nums, len;   char s[110];      void solve() {       cin >> n >> k >> s + 1;       sum = 0;       for (int i = 1, j = n; j > i; i++, j--) {           if (s[i] == s[j]) {               sum++;           } else break;       }       if(!(n & 1) && k == n / 2) puts( NO );       else if (k <= sum) puts( YES );       else puts( NO );   }      int main() {       IOS;       cf;       return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int maxn = 100000;   int te, A, B, C, num[maxn + 5], AB, AC, BC, ABC, sum[8];   long long ans;   int gcd(int a, int b) { return b ? gcd(b, a % b) : a; }   inline bool check(int i, int j, int k) { return (i & 1) && (j & 2) && (k & 4); }   inline long long C3(long long x) { return x * (x - 1) * (x - 2) / 6; }   inline long long C2(long long x) { return x * (x - 1) / 2; }   int main() {     for (int i = 1; i <= maxn; i++)       for (int j = i; j <= maxn; j += i) num[j]++;     for (scanf( %d , &te); te; printf( %lld n , ans), ans = 0, te--) {       scanf( %d%d%d , &A, &B, &C);       AB = gcd(A, B);       AC = gcd(A, C);       BC = gcd(B, C);       ABC = gcd(AB, C);       sum[1] = num[A] - num[AB] - num[AC] + num[ABC];       sum[2] = num[B] - num[AB] - num[BC] + num[ABC];       sum[3] = num[AB] - num[ABC];       sum[4] = num[C] - num[AC] - num[BC] + num[ABC];       sum[5] = num[AC] - num[ABC];       sum[6] = num[BC] - num[ABC];       sum[7] = num[ABC];       for (int i = 1; i < 8; i++)         for (int j = i; j < 8; j++)           for (int k = j; k < 8; k++)             if (check(i, j, k) || check(i, k, j) || check(j, i, k) ||                 check(j, k, i) || check(k, i, j) || check(k, j, i))               if (i == j && j == k)                 ans += C3(sum[i] + 2);               else if (i == j)                 ans += C2(sum[i] + 1) * sum[k];               else if (j == k)                 ans += C2(sum[j] + 1) * sum[i];               else                 ans += (long long)sum[i] * sum[j] * sum[k];     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 1e6 + 15, OO = 1e6 + 3;   int n, q, t1, t2;   int inv[N], y[15], de[15];   long long MOD(long long a, long long b) {     a %= b;     if (a < 0) a += b;     return a;   }   void rangeInv(long long m) {     inv[1] = 1;     for (long long i = 2; i < m; ++i) inv[i] = (m - (m / i) * inv[m % i] % m) % m;   }   int f(int x) {     printf( ? %d n , x);     fflush(stdout);     scanf( %d , &x);     return x;   }   int F(int x) {     int ret = 0;     for (int i = 0; i < 11; ++i) {       int l = 1;       for (int j = 0; j < 11; ++j) {         if (j == i) continue;         l = MOD(1ll * l * (x - j), OO);       }       int li = de[i];       l = (1ll * li * l) % OO;       ret += (1ll * l * y[i]) % OO;       if (ret >= OO) ret -= OO;     }     return ret;   }   int main() {     rangeInv(OO);     for (int i = 0; i < 11; ++i) {       y[i] = f(i);       de[i] = 1;       for (int j = 0; j < 11; ++j) {         if (j == i) continue;         de[i] = MOD(de[i] * (i - j), OO);       }       de[i] = inv[de[i]];     }     for (int i = 0; i <= OO; ++i) {       if (F(i) == 0) {         printf( ! %d n , i);         fflush(stdout);         return 0;       }     }     printf( ! -1 n );     fflush(stdout);   }


#include <bits/stdc++.h>   using namespace std;   int n, m, cnt, b[1000100], dfn[1000100], low[1000100], q[1000100], tot, top,       sccflag[1000100], pre[1000100], nxt[1000100];   int cnt2, cnt3, cntc;   int cntans, ans[1000100];   vector<int> p[1000100];   struct edge {     int v;     edge *next;   } pool[2 * 1000100], *h[1000100];   void addedge(int u, int v) {     edge *p = &pool[++cnt];     p->v = v;     p->next = h[u];     h[u] = p;   }   struct node {     int u, v, c, w, id;   } a[1000100];   void tarjan(int u) {     dfn[u] = low[u] = ++tot;     q[++top] = u;     int v;     for (edge *p = h[u]; p; p = p->next) {       if (dfn[p->v] == 0) {         tarjan(p->v);         low[u] = min(low[u], low[p->v]);       } else if (sccflag[p->v] == 0)         low[u] = min(low[u], dfn[p->v]);     }     if (dfn[u] == low[u]) {       cntc++;       while (q[top + 1] != u) {         sccflag[q[top]] = cntc;         top--;       }     }   }   bool cmp(node x, node y) { return x.w < y.w; }   bool cmp2(int x, int y) {     if (a[x].c != a[y].c)       ;     return a[x].c < a[y].c;     return a[x].w < a[y].w;   }   bool check(int mid) {     memset(h, 0, sizeof(h));     memset(pool, 0, sizeof(pool));     cnt = 0;     for (int i = 1; i <= m; i++) {       if (a[i].w > mid) {         addedge(i, i + m);       }     }     cnt2 = 2 * m;     for (int k = 1; k <= n; k++) {       cnt3 = 0;       for (int i = 0; i < p[k].size(); i++) b[++cnt3] = p[k][i];       sort(b + 1, b + cnt3 + 1, cmp2);       for (int i = 1; i <= cnt3; i++) pre[i] = ++cnt2, nxt[i] = ++cnt2;       for (int i = 1; i <= cnt3 - 1; i++)         addedge(pre[i], pre[i + 1]), addedge(pre[i], b[i + 1] + m);       for (int i = 2; i <= cnt3; i++)         addedge(nxt[i], nxt[i - 1]), addedge(b[i], nxt[i - 1]);       for (int i = 1; i <= cnt3; i++)         addedge(b[i], pre[i]), addedge(nxt[i], b[i] + m);       for (int i = 1; i <= cnt3; i++) pre[i] = ++cnt2, nxt[i] = ++cnt2;       for (int i = 1; i <= cnt3; i++) {         int R = i;         while (R < cnt3 && a[b[R + 1]].c == a[b[i]].c) R++;         for (int j = i; j <= R; j++) {           addedge(pre[j], b[j]);           addedge(b[j] + m, nxt[j]);           if (j != R) {             addedge(nxt[j], b[j + 1]);             addedge(b[j + 1] + m, pre[j]);             addedge(pre[j + 1], pre[j]);             addedge(nxt[j], nxt[j + 1]);           }         }         i = R;       }     }     tot = top = cntc = 0;     for (int i = 1; i <= cnt2; i++) dfn[i] = low[i] = sccflag[i] = 0;     for (int i = 1; i <= cnt2; i++) {       if (dfn[i] == 0) tarjan(i);     }     for (int i = 1; i <= m; i++) {       if (sccflag[i] == sccflag[i + m]) return false;     }     return true;   }   int main() {     scanf( %d%d , &n, &m);     for (int i = 1; i <= m; i++)       scanf( %d%d%d%d , &a[i].u, &a[i].v, &a[i].c, &a[i].w), a[i].id = i;     sort(a + 1, a + m + 1, cmp);     for (int i = 1; i <= m; i++) {       p[a[i].u].push_back(i);       p[a[i].v].push_back(i);     }     int left = 0, right = 1000000000, mid, Ans = -1;     while (left <= right) {       mid = (left + right) / 2;       if (check(mid) == true)         right = mid - 1, Ans = mid;       else         left = mid + 1;     }     if (Ans == -1) {       printf( No n );       return 0;     }     bool Mo = check(Ans);     for (int i = 1; i <= m; i++) {       if (sccflag[i] < sccflag[i + m]) ans[++cntans] = a[i].id;     }     printf( Yes n );     printf( %d %d n , Ans, cntans);     for (int i = 1; i <= cntans; i++) {       printf( %d  , ans[i]);     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   signed main() {     ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);     long long n;     cin >> n;     vector<long long> apr(n + 1), arr(n);     long long now = 1, next = 2;     for (long long i = 0; i < n - 1; i++, next++) {       long long a = 0, b = 0;       cout <<  ?   << now <<     << next <<   n ;       cout.flush();       cin >> a;       cout <<  ?   << next <<     << now <<   n ;       cout.flush();       cin >> b;       if (a > b) {         arr[now - 1] = a;         apr[a] = 1;         now = next;       } else {         arr[next - 1] = b;         apr[b] = 1;       }     }     for (long long i = 1; i <= n; i++) {       if (apr[i] == false) {         arr[now - 1] = i;         break;       }     }     cout <<  !  ;     for (long long i = 0; i < n; i++) {       cout << arr[i] <<    ;     }     cout <<   n ;     cout.flush();   }


#include <bits/stdc++.h>   using namespace std;   const int MAX_N = 101;   int a[MAX_N];   int n;   vector<vector<int>> ans;   pair<int, int> pop(multiset<pair<int, int>, std::greater<pair<int, int>>> &s) {     pair<int, int> x = *s.begin();     s.erase(s.begin());     return x;   }   bool check(int d) {     ans.clear();     multiset<pair<int, int>, std::greater<pair<int, int>>> s;     int sum = 0;     for (int i = 0; i < n; i++) {       if (a[i] < d) return false;       s.insert({a[i] - d, i});       sum += a[i] - d;     }     if (sum % 2 == 1) {       if (n < 3) return false;       pair<int, int> x = pop(s), y = pop(s), z = pop(s);       if (z.first == 0) return false;       ans.push_back({x.second, y.second, z.second});       s.insert({x.first - 1, x.second});       s.insert({y.first - 1, y.second});       s.insert({z.first - 1, z.second});       sum -= 3;     }     while (sum > 0) {       if (n < 2) return false;       pair<int, int> x = pop(s), y = pop(s);       if (y.first == 0) return false;       ans.push_back({x.second, y.second});       s.insert({x.first - 1, x.second});       s.insert({y.first - 1, y.second});       sum -= 2;     }     return true;   }   void dummy() {     ans.clear();     multiset<pair<int, int>, std::greater<pair<int, int>>> s;     for (int i = 0; i < n; i++) {       s.insert({a[i], i});     }     while ((*s.begin()).first > 0) {       pair<int, int> x = pop(s), y = pop(s);       s.insert({(x.first > 1 ? x.first - 1 : 0), x.second});       s.insert({(y.first > 1 ? y.first - 1 : 0), y.second});       ans.push_back({x.second, y.second});     }   }   int main() {     cin >> n;     int r = MAX_N;     for (int i = 0; i < n; i++) {       cin >> a[i];       r = min(r, a[i]);     }     r++;     int l = 0;     while (l < r) {       int m = l + (r - l) / 2;       if (check(m)) {         l = m + 1;       } else {         r = m;       }     }     if (l == 0 || !check(l - 1)) {       l = 1;       dummy();     }     cout << l - 1 << endl << ans.size() << endl;     for (auto p : ans) {       for (int i = 0; i < n; i++) {         bool ok = 0;         for (int x : p) {           ok |= x == i;         }         cout << (ok ?  1  :  0 );       }       cout << endl;     }     return 0;   }


#include <bits/stdc++.h>   namespace _ {   const int inf = 1e9 + 7;   namespace _in {   template <class T>   inline T read() {     char ch;     bool flag = 0;     T x = 0;     while (ch = getchar(), !isdigit(ch))       if (ch ==  - ) flag = 1;     while (isdigit(ch)) x = x * 10 + ch - 48, ch = getchar();     return flag ? -x : x;   }   struct {     inline operator long long() { return read<long long>(); }     inline operator int() { return read<int>(); }     inline operator bool() { return read<bool>(); }     template <class T>     inline void operator()(T &x) {       x = *this;     }     template <class T, class... A>     inline void operator()(T &x, A &...a) {       x = *this, this->operator()(a...);     }   } IN;   }  // namespace _in   using namespace _in;   namespace _std {   template <class T>   inline void swap(T &x, T &y) {     x ^= y ^= x ^= y;   }   template <class T>   inline T max(T x, T y) {     if (x < y) x = y;     return x;   }   template <class T>   inline T min(T x, T y) {     if (x > y) x = y;     return x;   }   template <class T>   inline void chkmax(T &x, T y) {     if (x < y) x = y;   }   template <class T>   inline void chkmin(T &x, T y) {     if (x > y) x = y;   }   }  // namespace _std   using namespace _std;   }  // namespace _   using namespace _;   const int N = 1e5 + 2;   const int LogN = 18;   struct Edge_Node {     int to, val;   };   struct Query {     int id1, id2, typ, val;   };   char tmp_str[N];   int n, m, q, ans[N], fa_ch[N], len_str[N];   int *str[N];   std::vector<Edge_Node> son[N];   std::vector<Query> que_nod[N];   namespace Hash {   const std::pair<unsigned int, unsigned int> base =       std::pair<unsigned int, unsigned int>(7, 23);   inline std::pair<unsigned int, unsigned int> operator+(       const std::pair<unsigned int, unsigned int> &a,       const std::pair<unsigned int, unsigned int> &b) {     return std::pair<unsigned int, unsigned int>(a.first + b.first,                                                  a.second + b.second);   }   inline std::pair<unsigned int, unsigned int> operator-(       const std::pair<unsigned int, unsigned int> &a,       const std::pair<unsigned int, unsigned int> &b) {     return std::pair<unsigned int, unsigned int>(a.first - b.first,                                                  a.second - b.second);   }   inline std::pair<unsigned int, unsigned int> operator*(       const std::pair<unsigned int, unsigned int> &a,       const std::pair<unsigned int, unsigned int> &b) {     return std::pair<unsigned int, unsigned int>(a.first * b.first,                                                  a.second * b.second);   }   inline bool operator==(const std::pair<unsigned int, unsigned int> &a,                          const std::pair<unsigned int, unsigned int> &b) {     return (a.first == b.first) && (a.second == b.second);   }   }  // namespace Hash   using namespace Hash;   struct BIT {     int lim, res, c[N];     inline int lowbit(int x) { return x & (-x); }     inline void update(int x, int y) {       for (; x <= lim; x += lowbit(x)) c[x] += y;     }     inline int _sum(int x) {       res = 0;       for (; x; x -= lowbit(x)) res += c[x];       return res;     }     inline int query(int l, int r) { return _sum(r) - _sum(l - 1); }   } bit[2];   struct AC_AutoMaton {     std::queue<int> que;     std::vector<int> son[N];     int tot, tim, ch[N][26], fail[N], pos[N], dfn[N], siz[N];     inline void build() {       for (int i = 0; i < 26; ++i)         if (ch[0][i]) fail[ch[0][i]] = 0, que.push(ch[0][i]);       while (!que.empty()) {         int u = que.front();         que.pop();         for (int c = 0; c < 26; ++c)           if (ch[u][c])             fail[ch[u][c]] = ch[fail[u]][c], que.push(ch[u][c]);           else             ch[u][c] = ch[fail[u]][c];       }     }     inline void insert(int id) {       int now = 0, len = len_str[id];       for (int i = 1; i <= len; ++i) {         if (!ch[now][str[id][i]]) ch[now][str[id][i]] = ++tot;         now = ch[now][str[id][i]];       }       pos[id] = now;     }     void get_son() {       for (int i = 1; i <= tot; ++i) son[fail[i]].push_back(i);     }     void pre_dfn(int u) {       dfn[u] = ++tim, siz[u] = 1;       for (int v : son[u]) pre_dfn(v), siz[u] += siz[v];     }   } acm[2];   namespace LCA {   int dep[N], fa[N][LogN];   void get_fa(int u, int f) {     fa[u][0] = f, dep[u] = dep[f] + 1;     for (int i = 1; i < 18; ++i) fa[u][i] = fa[fa[u][i - 1]][i - 1];     for (Edge_Node now : son[u])       if (now.to != f) get_fa(now.to, u), fa_ch[now.to] = now.val;   }   inline int Lca(int x, int y) {     if (x == y) return x;     if (dep[x] < dep[y]) swap(x, y);     for (int i = 17; ~i; --i)       if (dep[fa[x][i]] >= dep[y]) x = fa[x][i];     if (x == y) return x;     for (int i = 17; ~i; --i)       if (fa[x][i] != fa[y][i]) x = fa[x][i], y = fa[y][i];     return fa[x][0];   }   inline int kth(int x, int d) {     if (dep[x] < d) return -1;     for (int i = 17; ~i; --i)       if (dep[fa[x][i]] >= d) x = fa[x][i];     return x;   }   }  // namespace LCA   using namespace LCA;   std::pair<unsigned int, unsigned int> sum[N], has_pow[N];   inline std::pair<unsigned int, unsigned int> tree_calc(int x, int y) {     return sum[x] - sum[y] * has_pow[dep[x] - dep[y]];   }   int tot_bor[2];   struct Border {     int s, t, d;   } border[N][2];   struct KMP {     int n, *las, *nxt, *del;     inline void build(int id) {       n = len_str[id], nxt = new int[(n + 2)](), las = new int[(n + 2)](),       del = new int[(n + 2)]();       del[1] = las[1] = 1;       for (int i = 2, t = 0; i <= n; ++i) {         while (t && str[id][i] != str[id][t + 1]) t = nxt[t];         if (str[id][i] == str[id][t + 1]) ++t;         nxt[i] = t, del[i] = i - t, las[i] = (del[i] == del[t]) ? las[t] : i;       }     }     inline void get_border(int pos, int len, int typ) {       while (pos > len) pos = nxt[pos];       while (pos > 0) {         border[++tot_bor[typ]][typ] = (Border){las[pos], pos, del[pos]};         pos = nxt[las[pos]];       }     }   } kmp[N][2];   int hep[N];   struct Suffix_Array {     std::pair<unsigned int, unsigned int> *has;     int n, m, *x, *y, *sa, *ch;     inline std::pair<unsigned int, unsigned int> sa_calc(int l, int r) {       return has[r] - has[l - 1] * has_pow[r - l + 1];     }     inline void sort() {       for (int i = 0; i <= m; ++i) hep[i] = 0;       for (int i = 1; i <= n; ++i) ++hep[x[i]];       for (int i = 1; i <= m; ++i) hep[i] += hep[i - 1];       for (int i = n; i >= 1; --i) sa[hep[x[y[i]]]--] = y[i];     }     inline void build(int id) {       n = len_str[id], m = 27;       x = new int[(n + 2)](), y = new int[(n + 2)](), sa = new int[(n + 2)](),       ch = new int[(n + 2)]();       has = new std::pair<unsigned int, unsigned int>[(n + 2)]();       has[0] = std::pair<unsigned int, unsigned int>(1, 1);       for (int i = 1; i <= n; ++i)         has[i] = has[i - 1] * base +                  std::pair<unsigned int, unsigned int>(str[id][i], str[id][i]);       for (int i = 1; i <= n; ++i)         x[y[i] = i] = str[id][i] + 1, ch[i] = str[id][i];       sort();       for (int w = 1, p = 0; m = p, p < n; w <<= 1) {         for (int i = 1; i <= w; ++i) y[p = i] = n - w + i;         for (int i = 1; i <= n; ++i)           if (sa[i] > w) y[++p] = sa[i] - w;         sort(), std::swap(x, y), x[sa[1]] = p = 1;         for (int i = 2; i <= n; ++i)           x[sa[i]] =               (y[sa[i]] == y[sa[i - 1]] && y[sa[i] + w] == y[sa[i - 1] + w])                   ? p                   : ++p;       }     }     inline int lcp(int p, int u, int lca, int len = 0) {       if (u == lca) return 0;       p = sa[p], len = n - p + 1;       if (len >= dep[u] - dep[lca] &&           tree_calc(u, lca) == sa_calc(p, p + dep[u] - dep[lca] - 1))         return dep[u] - dep[lca];       else {         int las;         for (int i = 17; ~i; --i)           if (dep[u] - dep[lca] > (1 << i)) {             las = u, u = fa[u][i];             if (len >= dep[u] - dep[lca] &&                 tree_calc(u, lca) == sa_calc(p, p + dep[u] - dep[lca] - 1))               u = las;           }         u = fa[u][0];         return dep[u] - dep[lca];       }     }     inline bool check(int mid, int u, int lca) {       int now = lcp(mid, u, lca);       if (now == n - sa[mid] + 1) return true;       if (now == dep[u] - dep[lca]) return false;       return ch[sa[mid] + now] < fa_ch[kth(u, dep[lca] + now + 1)];     }     inline void get_maxlen(int u, int lca, int id, int typ) {       int l = 1, r = n, mid, ans = 0;       while (l <= r)         check(mid = (l + r) >> 1, u, lca) ? ans = mid, l = mid + 1 : r = mid - 1;       if (!ans) return;       int len = lcp(ans, u, lca), pos = n - sa[ans] + 1;       kmp[id][typ].get_border(pos, len, typ);     }   } sa[N][2];   inline void addedge(int u = 0, int v = 0) {     IN(u, v), scanf( %s , tmp_str);     son[u].push_back((Edge_Node){v, tmp_str[0] -  a }),         son[v].push_back((Edge_Node){u, tmp_str[0] -  a });   }   void get_ac_ans(int u, int t1, int t2) {     bit[0].update(acm[0].dfn[t1], 1);     bit[1].update(acm[1].dfn[t2], 1);     for (Query now : que_nod[u]) {       int id = now.id2, p = now.typ, v = now.val, k = acm[p].pos[now.id1];       ans[id] +=           v * bit[p].query(acm[p].dfn[k], acm[p].dfn[k] + acm[p].siz[k] - 1);     }     for (Edge_Node now : son[u]) {       int v = now.to, c = now.val;       if (v == fa[u][0]) continue;       get_ac_ans(v, acm[0].ch[t1][c], acm[1].ch[t2][c]);     }     bit[0].update(acm[0].dfn[t1], -1);     bit[1].update(acm[1].dfn[t2], -1);   }   void get_hash(int u) {     for (Edge_Node now : son[u]) {       int v = now.to, c = now.val;       if (v == fa[u][0]) continue;       sum[v] = sum[u] * base + std::pair<unsigned int, unsigned int>(c, c),       get_hash(v);     }   }   inline int exgcd(int a, int b, int &x, int &y) {     if (!b) return x = 1, y = 0, a;     int t = exgcd(b, a % b, y, x);     return y -= a / b * x, t;   }   inline int get_ans(int a, int b, int la, int lb, int c) {     int x, y, gcd = exgcd(a, b, x, y);     if (c % gcd) return 0;     x *= c / gcd, y *= c / gcd;     int _a = a / gcd, _b = b / gcd, _x = (x % _b + _b) % _b;     y += ((x - _x) / _b) * _a, x = _x;     if (y < 0) return 0;     if (y > lb) {       int t = (y - lb - 1) / _a + 1;       y -= t * _a, x += t * _b;     }     if (x < 0 || y < 0 || x > la || y > lb) return 0;     return min((la - x) / _b + 1, y / _a + 1);   }   inline int kmp_solve(int u, int v, int lca, int id) {     tot_bor[0] = tot_bor[1] = 0;     sa[id][1].get_maxlen(u, lca, id, 0);     sa[id][0].get_maxlen(v, lca, id, 1);     int res = 0;     for (int i = 1; i <= tot_bor[0]; ++i)       for (int j = 1; j <= tot_bor[1]; ++j)         if (border[i][0].s + border[j][1].s <= len_str[id] &&             border[i][0].t + border[j][1].t >= len_str[id]) {           int la = (border[i][0].t - border[i][0].s) / border[i][0].d;           int lb = (border[j][1].t - border[j][1].s) / border[j][1].d;           res += get_ans(border[i][0].d, border[j][1].d, la, lb,                          len_str[id] - border[i][0].s - border[j][1].s);         }     return res;   }   int u, v, id;   int main() {     IN(n, m, q);     for (int i = 2; i <= n; ++i) addedge();     get_fa(1, 0), get_hash(1);     has_pow[0] = std::pair<unsigned int, unsigned int>(1, 1);     for (int i = 1; i < N; ++i) has_pow[i] = has_pow[i - 1] * base;     for (int i = 1; i <= m; ++i) {       scanf( %s , tmp_str), len_str[i] = strlen(tmp_str);       str[i] = new int[len_str[i] + 2]();       for (int j = 1; j <= len_str[i]; ++j) str[i][j] = tmp_str[j - 1] -  a ;       str[i][0] = str[i][len_str[i] + 1] = -1;       acm[0].insert(i);       sa[i][0].build(i);       kmp[i][0].build(i);       std::reverse(str[i] + 1, str[i] + 1 + len_str[i]);       acm[1].insert(i);       sa[i][1].build(i);       kmp[i][1].build(i);       std::reverse(str[i] + 1, str[i] + 1 + len_str[i]);     }     acm[0].build(), acm[0].get_son(), acm[0].pre_dfn(0);     acm[1].build(), acm[1].get_son(), acm[1].pre_dfn(0);     bit[0].lim = acm[0].tim, bit[1].lim = acm[1].tim;     for (int t = 1; t <= q; ++t) {       IN(u, v, id);       int lca = Lca(u, v);       ans[t] = kmp_solve(u, v, lca, id);       int t1 = kth(u, min(dep[u], dep[lca] + len_str[id] - 1));       int t2 = kth(v, min(dep[v], dep[lca] + len_str[id] - 1));       if (t1 != u)         que_nod[u].push_back((Query){id, t, 1, 1}),             que_nod[t1].push_back((Query){id, t, 1, -1});       if (t2 != v)         que_nod[v].push_back((Query){id, t, 0, 1}),             que_nod[t2].push_back((Query){id, t, 0, -1});     }     get_ac_ans(1, 0, 0);     for (int i = 1; i <= q; ++i) printf( %d n , ans[i]);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   template <typename T>   T mabs(const T &a) {     return a < 0 ? -a : a;   }   const int mod = 1e9 + 7;   const int Row = 14, Mask = 4782969;   int D[2][2][Mask];   int setbit[Mask][3];   int getbit[Mask][3];   int p3[20] = {1};   inline int setBit(int mask, int pos, int val) {     return (mask / p3[pos + 1] * 3 + val) * p3[pos] + mask % p3[pos];   }   inline int getBit(int mask, int pos) { return mask / p3[pos] % 3; }   inline int up(int i, int mask) {     if (i == 0) return 3;     return getbit[mask][i - 1];   }   inline int left(int i, int j, int mask) {     if (j == 0) return 3;     return getbit[mask][i];   }   inline void addTo(int &a, int val) {     a += val;     if (a >= mod) a -= mod;   }   inline int &next(int flag, int flag2, int i, int n, int mask) {     if (i == n - 1) return D[flag ^ 1][0][mask];     return D[flag][flag2 ^ 1][mask];   }   void run() {     char str[] = { AHIMOTUVWXY };     char A[100100];     scanf( %s , A);     bool ok = 1;     int n = strlen(A);     for (int i = (0), ei = (n / 2); i < ei; i++)       if (A[i] != A[n - 1 - i]) ok = 0;     for (int i = (0), ei = (n); i < ei; i++)       if (strchr(str, A[i]) == 0) ok = 0;     puts(ok ?  YES  :  NO );   }   int main() {     run();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   char cc[8][9];   int win;   void dfs(int t, int i, int j) {     int x, y;     if (win) {       return;     }     if (i < 0 || i >= 8 || j < 0 || j >= 8) {       return;     }     if (i - t >= 0 && cc[i - t][j] ==  S ) {       return;     }     if (i + 1 - t >= 0 && i + 1 - t < 8 && cc[i + 1 - t][j] ==  S ) {       return;     }     if (t >= 8) {       win = 1;       return;     }     for (x = -1; x <= 1; x++) {       for (y = -1; y <= 1; y++) {         dfs(t + 1, i + x, j + y);       }     }   }   int main() {     int i;     for (i = 0; i < 8; i++) {       scanf( %s , cc[i]);     }     dfs(0, 7, 0);     printf(win ?  WIN n  :  LOSE n );     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     long long int l1, l2, r1, r2, k, temp, ans, st, en;     cin >> l1 >> l2 >> r1 >> r2 >> k;     if (l1 <= r1) {       st = r1;     } else       st = l1;     if (l2 <= r2)       en = l2;     else       en = r2;     if (k >= st && k <= en)       ans = (en - st);     else       ans = (en - st + 1);     if (ans < 0)       cout << 0 << endl;     else       cout << ans << endl;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     long long int n, k, b, a, c;     cin >> n >> k;     a = n / k;     if (a % 2 == 0)       cout <<  NO  << endl;     else       cout <<  YES  << endl;   }


#include <bits/stdc++.h>   using namespace std;   vector<int> g[100005];   int n, fa[100005], p[100005], size[100005], tsize[100005], q, d[100005];   long long ans = 0, sum[100005];   void dfs(int x, int f) {     p[x] = f, fa[x] = x, size[x] = tsize[x] = 1, d[x] = d[f] + 1,     ans += 1ll * (n - 2) * (n - 1);     for (int y : g[x]) {       if (y == f) continue;       dfs(y, x), tsize[x] += tsize[y], sum[x] += 1ll * tsize[y] * (tsize[y] - 1);     }     ans -= 1ll * (n - tsize[x]) * (n - tsize[x] - 1) + sum[x];   }   int gf(int x) { return x == fa[x] ? x : fa[x] = gf(fa[x]); }   void Add(int x, long long dlt) {     ans += dlt * size[x] *            (1ll * (n - size[x]) * (n - size[x] - 1) - sum[x] -             1ll * (n - tsize[x]) * (n - tsize[x] - 1));     ans += dlt * size[x] * (size[x] - 1) * (n - size[x]) * 2;     ans += dlt * size[x] * (size[x] - 1) * (size[x] - 2);   }   void Merge(int x, int y) {     x = gf(x), y = gf(y);     if (x == y) return;     Add(x, -1), Add(y, -1), fa[x] = y, size[y] += size[x],                             sum[y] += sum[x] - 1ll * (tsize[x] - 1) * tsize[x],                             Add(y, 1);   }   int main() {     cin >> n;     for (int i = 1, x, y; i < n; i++)       cin >> x >> y, g[x].push_back(y), g[y].push_back(x);     dfs(1, 0), cout << ans <<   n , cin >> q;     while (q--) {       int x, y;       cin >> x >> y, x = gf(x), y = gf(y);       while (x ^ y) {         if (d[gf(p[x])] < d[gf(p[y])]) swap(x, y);         Merge(x, p[x]), x = gf(p[x]), y = gf(y);       }       cout << ans <<   n ;     }     return 0;   }


#include <bits/stdc++.h>   int n, ans, a[15][15];   inline void maxx(int& a, int b) { a = (a > b ? a : b); }   inline void minx(int& a, int b) { a = (a < b ? a : b); }   int main() {     scanf( %d , &n);     for (int i = 1; i <= n; i++)       for (int j = 1; j <= n; j++) scanf( %d , &a[i][j]);     for (int k = 1; k <= n; k++)       for (int i = 1; i <= n; i++)         for (int j = 1; j <= n; j++) minx(a[i][j], a[i][k] + a[k][j]);     for (int i = 1; i <= n; i++)       for (int j = 1; j <= n; j++) maxx(ans, a[i][j]);     printf( %d n , ans);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int N, K, ans;   vector<int> ady[1000005];   set<int> dfs(int x, int p) {     set<int> s;     if (ady[x].size() == 1) {       s.insert(0);       return s;     }     for (auto v : ady[x]) {       if (v == p) continue;       set<int> tmp = dfs(v, x);       for (auto aux : tmp) {         aux++;         if (aux > K)           ans++;         else if (s.find(aux) != s.end() && aux + aux > K)           ans++;         else           s.insert(aux);       }     }     auto it = s.begin();     while (s.begin() != s.end()) {       auto it2 = it;       it2++;       if (it2 == s.end()) break;       if (*it + *it2 <= K) s.erase(it);       it = it2;     }     return s;   }   int main() {     cin.tie(0);     ios_base::sync_with_stdio(0);     cin >> N >> K;     for (int i = 1; i < N; i++) {       int u, v;       cin >> u >> v;       ady[u].push_back(v);       ady[v].push_back(u);     }     int root = 1;     while (ady[root].size() == 1) root++;     ans += dfs(root, 0).size();     cout << ans;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n, m;     cin >> n >> m;     long long ans = 0;     for (int i = (0); i < (n); ++i) {       long long t, T, x, cost;       cin >> t >> T >> x >> cost;       if (t >= T) {         ans += cost + m * x;         continue;       }       long long aux1 = cost;       if (m > (T - t)) aux1 += m * x;       long long aux2 = (long long)ceil((double)(m - (T - t)) / (T - t)) + 1;       aux2 *= cost;       ans += min(aux1, aux2);     }     cout << ans << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const double eps = 1e-10;   struct rec {     double x, y;   } a[110], s, e, f[2];   int g[2], n, m, i;   double ans;   double XJ(rec a, rec b, rec c) {     return (b.x - a.x) * (c.y - a.y) - (b.y - a.y) * (c.x - a.x);   }   double dist(rec a, rec b) {     return sqrt((a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y));   }   void cross(rec a, rec b, rec c, rec d) {     double t1 = XJ(a, c, b), t2 = XJ(a, b, d), t3 = XJ(c, a, d), t4 = XJ(c, d, b);     if (fabs(t1) < eps && fabs(t2) < eps) return;     if (fabs(dist(a, c) + dist(b, c) - dist(a, b)) < eps) {       f[m] = c;       g[m++] = i;       return;     }     if (t1 * t2 > eps && t3 * t4 > eps) {       f[m].x = (t1 * d.x + t2 * c.x) / (t1 + t2);       f[m].y = (t1 * d.y + t2 * c.y) / (t1 + t2);       g[m++] = i;       return;     }   }   double calc(int st, int ed, int s, int e) {     if (ed == 1)       ed = n;     else       ed--;     double ans = dist(f[s], a[st]);     for (int i = st; i != ed; i == n ? i = 1 : i++)       ans += dist(a[i], a[i == n ? 1 : i + 1]);     return ans + dist(a[ed], f[e]);   }   int main() {     scanf( %lf%lf%lf%lf%d , &s.x, &s.y, &e.x, &e.y, &n);     for (i = 1; i <= n; i++) cin >> a[i].x >> a[i].y;     a[0] = a[n];     for (i = 1; i <= n; i++) cross(s, e, a[i - 1], a[i]);     if (m > 1) {       if (dist(s, f[0]) > dist(s, f[1])) swap(f[0], f[1]), swap(g[0], g[1]);       ans = min(calc(g[0], g[1], 0, 1), calc(g[1], g[0], 1, 0));       ans = min(ans, 2 * dist(f[0], f[1]));       ans += dist(s, f[0]) + dist(f[1], e);     } else       ans = dist(s, e);     printf( %.10lf n , ans);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long a[2500000];   int main() {     int all;     scanf( %d , &all);     for (int i = 1; i <= all; i++) {       scanf( %I64d , &a[i]);     }     sort(a + 1, a + all + 1);     reverse(a + 1, a + all + 1);     long long sum = 0;     for (int fro = 1; fro <= all; fro = fro << 2) {       for (int p = 1; p <= fro; p++) {         sum += a[p];       }     }     long long temp = 0, area = 1;     printf( %I64d , sum);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n, k;     cin >> n >> k;     vector<int> a(n + 1);     vector<vector<int> > inc(n + 1);     vector<vector<int> > dec(n + 2);     for (int i = 0; i < n; i++) {       cin >> a[i + 1];     }     for (int i = 0; i <= n; i++) {       if (i == 0)         ;       else {         inc[i] = inc[i - 1];         inc[i].push_back(a[i]);         sort(inc[i].begin(), inc[i].end());       }     }     for (int i = n; i >= 1; i--) {       if (i == n)         dec[i].push_back(a[i]);       else {         dec[i] = dec[i + 1];         dec[i].push_back(a[i]);         sort(dec[i].begin(), dec[i].end());       }     }     int fsum = accumulate(a.begin(), a.end(), 0);     for (int i = 1; i <= n; i++) {       for (int j = i; j <= n; j++) {         vector<int> tmp(j - i + 1);         copy(a.begin() + i, a.begin() + j + 1, tmp.begin());         sort(tmp.begin(), tmp.end(), greater<int>());         vector<int> incb = inc[i - 1];         vector<int> deca = dec[j + 1];         int swaps = 0, tmpsum = 0;         while (swaps < k && tmp.size()) {           int src = 0;           int v1 = INT_MIN, v2 = INT_MIN, finval = INT_MIN;           if (incb.size()) v1 = incb.back();           if (deca.size()) v2 = deca.back();           if (v1 == INT_MIN && v2 == INT_MIN) break;           if (v1 >= v2) {             src = 1;             finval = v1;           } else if (v1 < v2) {             src = 2;             finval = v2;           }           swaps++;           if (finval > tmp.back()) {             tmpsum += finval;             if (src == 1)               incb.pop_back();             else if (src == 2)               deca.pop_back();             tmp.pop_back();           } else             break;         }         fsum = max(fsum, tmpsum + accumulate(tmp.begin(), tmp.end(), 0));       }     }     cout << fsum;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int zsern = 1e6 + 1000;   const long long mod = 1e9 + 7;   const double pi = acos(-1.0);   long long n, k;   long long a[zsern];   std::vector<long long> num;   void gst(int id, int zser) {     if (id - 1 >= 0 && id - 1 != zser)       gst(id - 1, zser);     else if (id - 2 >= 0 && id - 2 != zser)       gst(id - 2, zser);     num.push_back(1 << id);     if (id - 1 >= 0 && id - 1 != zser)       gst(id - 1, zser);     else if (id - 2 >= 0 && id - 2 != zser)       gst(id - 2, zser);   }   void AC() {     long long x;     while (~scanf( %lld%lld , &n, &x)) {       long long zser = 0;       for (int i = 0; i <= 18; i++) {         if ((1ll << i) <= x) zser = i;       }       for (int i = n - 1; i >= 0; i--) {         if (i != zser) {           gst(i, zser);           break;         }       }       cout << num.size() << endl;       for (int i = 0; i < num.size(); i++) printf( %lld  , num[i]);     }   }   int main() {     AC();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     string s;     cin >> s;     int n = s.size(), c = 0, c1 = 0, k = 0, k1 = 0;     if (n % 2 != 0)       cout << -1;     else {       for (int i = 0; i < n; i++) {         if (s[i] ==  R )           c++;         else if (s[i] ==  L )           c1++;         else if (s[i] ==  U )           k++;         else if (s[i] ==  D )           k1++;       }       n = abs(c - c1) + abs(k - k1);       n = n / 2;       cout << n;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int32_t mod = 1e9 + 7;   const long long inf = 1e18;   set<long long> S;   void doq() {     for (long long i = 0; i < 40; i++) {       for (long long j = 0; j < 40; j++) {         long long a = 3 * i + 7 * j;         if (a <= 100) S.insert(a);       }     }   }   void solve() {     long long n;     cin >> n;     if (S.find(n) != S.end())       cout <<  YES n ;     else       cout <<  NO n ;   }   int32_t main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     doq();     long long tcs = 1;     cin >> tcs;     while (tcs--) {       solve();     }   }


#include <bits/stdc++.h>   using namespace std;   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     long long int n, x, d = 0;     cin >> n >> x;     while (n--) {       long long int t;       char s;       cin >> s;       cin >> t;       if (s ==  + ) {         x += t;       } else {         if (x - t >= 0)           x -= t;         else           d++;       }     }     cout << x <<     << d;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   void setIO(string s =   ) {     ios_base::sync_with_stdio(0);     cin.tie(0);     if ((int)s.size()) {       freopen((s +  .in ).c_str(),  r , stdin);       freopen((s +  .out ).c_str(),  w , stdout);     }   }   int main() {     setIO();     int n;     string s;     cin >> n >> s;     string result;     result += s[0];     for (int i = 1; i < n; i++) {       if (i % 2) {         result = s[i] + result;       } else {         result += s[i];       }     }     if (n % 2) {       cout << result << endl;     } else {       reverse((result).begin(), (result).end());       cout << result << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long long N = 1000;   bool fg[55][55];   long long n, m, dy[4] = {1, 0, -1, 0}, dx[4] = {0, 1, 0, -1};   bool flag;   char s[55][55];   void dfs(long long x, long long y, long long st, long long ed) {     fg[x][y] = true;     for (long long i = 0; i < 4; i++) {       long long a = x + dx[i], b = y + dy[i];       if (a >= 1 && a <= n && b >= 1 && b <= m && s[a][b] == s[x][y] &&           (st != a || ed != b)) {         if (fg[a][b]) {           flag = true;           break;         }         dfs(a, b, x, y);       }     }   }   signed main() {     memset(fg, false, sizeof fg);     cin >> n >> m;     flag = false;     for (long long i = 1; i <= n; i++) scanf( %s , s[i] + 1);     for (long long i = 1; i <= n; i++) {       for (long long j = 1; j <= m; j++) {         if (!fg[i][j]) {           fg[i][j] = true;           dfs(i, j, i, j);           if (flag) break;         }       }       if (flag) break;     }     if (flag)       cout <<  Yes ;     else       cout <<  No ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   using ll = long long;   #define pb push_back   #define endl   n    #define F first   #define S second   #define pp pop_back   ll mod = 1e9+7;   ll dp[200200][2];   ll st[200200];   ll n;      ll rec(ll level,int curr){       if(st[level]==1||st[level]==0){           if(curr!=st[level])return 0;       }       if(level==n-1){           return 1;       }       if(dp[level][curr]!=-1)return dp[level][curr];       return dp[level][curr] = 1+rec(level+1,1-curr);   }         int main()   {       ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);       ll t;cin>>t;       while(t--){           memset(dp,-1,sizeof(dp));memset(st,-1,sizeof(st));           string s;cin>>s;           n = (ll)s.size();           for(ll i=0;i<n;i++){               if(s[i]== 1  ||s[i]== 0 ){st[i]=s[i]- 0 ;}               else{                   st[i]=3;               }           }           ll ans = 0;           for(int i=0;i<n;i++){               ans+=max(rec(i,1),rec(i,0));           }           cout<<ans<<endl;       }       return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     long long n, m;     cin >> n >> m;     long long a[n][m];     vector<long long> b(n);     for (int i = 0; i < n; i++) {       long long k = 0;       for (int j = 0; j < m; j++) {         cin >> a[i][j];         k += a[i][j];       }       b[i] = k;     }     if (n <= 4 || m <= 4) {       long long res = 0;       for (int i = 0; i < n; i++) res += b[i];       cout << res;       return 0;     }     vector<long long> c(m);     for (int j = 0; j < m; j++) {       long long k = 0;       for (int i = 0; i < n; i++) {         k += a[i][j];       }       c[j] = k;     }     long long res = 0;     vector<long long> d = b;     vector<long long> e = c;     sort(d.begin(), d.end());     sort(e.begin(), e.end());     res = max(d[n - 1] + d[n - 2] + d[n - 3] + d[n - 4],               e[m - 1] + e[m - 2] + e[m - 3] + e[m - 4]);     for (int i = 0; i < n; i++) {       vector<long long> f;       for (int j = 0; j < m; j++) {         f.push_back(c[j] - a[i][j]);       }       sort(f.begin(), f.end());       res = max(res, b[i] + f[m - 1] + f[m - 2] + f[m - 3]);     }     for (int j = 0; j < m; j++) {       vector<long long> f;       for (int i = 0; i < n; i++) {         f.push_back(b[i] - a[i][j]);       }       sort(f.begin(), f.end());       res = max(res, c[j] + f[n - 1] + f[n - 2] + f[n - 3]);     }     if (n <= m) {       for (int i = 0; i < n; i++) {         for (int l = i + 1; i < n; i++) {           vector<long long> f;           for (int j = 0; j < m; j++) {             f.push_back(c[j] - a[i][j] - a[l][j]);           }           sort(f.begin(), f.end());           res = max(res, b[i] + b[l] + f[m - 1] + f[m - 2]);         }       }     } else {       for (int j = 0; j < m; j++) {         for (int l = j + 1; j < m; j++) {           vector<long long> f;           for (int i = 0; i < n; i++) {             f.push_back(b[i] - a[i][j] - a[i][l]);           }           sort(f.begin(), f.end());           res = max(res, c[j] + c[l] + f[n - 1] + f[n - 2]);         }       }     }     cout << res;   }


#include <bits/stdc++.h>   using namespace std;   int a[100010];   int b[100010];   int ans[100010];   int main() {     int n;     cin >> n;     for (int i = 1; i <= n; i++) {       cin >> a[i];       b[a[i]] = i;     }     ans[b[n]] = 0;     for (int i = n - 1; i >= 1; i--) {       int idx = b[i];       int flag = 0;       for (int j = idx + i; j <= n; j += i) {         if (a[j] > i && ans[j] == 0) {           flag = 1;           break;         }       }       for (int j = idx - i; j >= 1; j -= i) {         if (a[j] > i && ans[j] == 0) {           flag = 1;           break;         }       }       if (flag == 1) {         ans[idx] = 1;       } else {         ans[idx] = 0;       }     }     for (int i = 1; i <= n; i++) {       if (ans[i]) {         cout <<  A ;       } else {         cout <<  B ;       }     }   }


#include <bits/stdc++.h>   using namespace std;   const long long N = 1e7;   vector<long long> primes;   vector<long long> p(N, 1);   signed main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     long long n, p, w, d;     cin >> n >> p >> w >> d;     long long l = 0, r = n;     while (r - l > 10000000) {       long long wins = (l + r) / 2;       if (w * wins > p) {         r = wins - 1;         continue;       }       long long draws = (p - (wins * w)) / d;       if (draws < 0) {         l = wins + 1;         continue;       }       long long points = w * wins + d * draws;       cerr << wins <<     << draws <<     << points <<   n ;       if (points == p) {         if (wins + draws <= n) {           cout << wins <<     << draws <<     << n - wins - draws <<   n ;           return 0;         } else {           l = wins + 1;         }       } else {         if (points < p) {           l = wins + 1;         } else {           r = wins - 1;         }       }     }     for (long long wins = l; wins <= r; wins++) {       if (w * wins > p) {         continue;       }       long long draws = (p - (wins * w)) / d;       if (draws < 0) {         continue;       }       long long points = w * wins + d * draws;       if (points == p) {         if (wins + draws <= n) {           cout << wins <<     << draws <<     << n - wins - draws <<   n ;           return 0;         }       }     }     cout << -1 <<   n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   struct chip {     int l;     int r;     int u;     int d;   };   vector<char> a[5005];   vector<chip> link1[5005], link2[5005];   int n, m, kq, res;   void nhap() {     ios_base::sync_with_stdio(0);     cin >> n >> m;     char x;     for (int i = 1; i <= n; ++i) a[i].push_back(   );     for (int i = 1; i <= n; ++i)       for (int j = 1; j <= m; ++j) {         cin >> x;         a[i].push_back(x);       }     for (int i = 1; i <= n; ++i) a[i].push_back(   );     for (int i = 0; i <= m + 1; ++i) {       a[0].push_back(   );       a[n + 1].push_back(   );     }     for (int i = 0; i <= n + 1; ++i)       for (int j = 0; j <= m + 1; ++j) {         chip x;         x.l = 0;         x.r = 0;         x.u = 0;         x.d = 0;         link1[i].push_back(x);         link2[i].push_back(x);       }   }   void prep() {     for (int i = 1; i <= n; ++i)       for (int j = 1; j <= m; ++j)         if (a[i][j] !=  . ) {           int l = j - 1, r = j + 1, u = i - 1, d = i + 1;           while (a[i][l] ==  .  && l > 0) --l;           while (a[i][r] ==  .  && r <= m) ++r;           while (a[u][j] ==  .  && u > 0) --u;           while (a[d][j] ==  .  && d <= n) ++d;           link1[i][j].l = l;           link1[i][j].r = r;           link1[i][j].u = u;           link1[i][j].d = d;         }   }   void xl(int x, int y) {     int dem = 0;     for (int i = 1; i <= n; ++i)       for (int j = 1; j <= m; ++j) link2[i][j] = link1[i][j];     while (a[x][y] !=  .  && x > 0 && x <= n && y > 0 && y <= m) {       ++dem;       int l = link2[x][y].l, r = link2[x][y].r, u = link2[x][y].u,           d = link2[x][y].d;       link2[x][l].r = r;       link2[x][r].l = l;       link2[u][y].d = d;       link2[d][y].u = u;       if (a[x][y] ==  L )         y = l;       else if (a[x][y] ==  R )         y = r;       else if (a[x][y] ==  U )         x = u;       else if (a[x][y] ==  D )         x = d;     }     kq = max(kq, dem);   }   void xl1(int x, int y) {     int dem = 0;     for (int i = 1; i <= n; ++i)       for (int j = 1; j <= m; ++j) link2[i][j] = link1[i][j];     while (a[x][y] !=  .  && x > 0 && x <= n && y > 0 && y <= m) {       ++dem;       int l = link2[x][y].l, r = link2[x][y].r, u = link2[x][y].u,           d = link2[x][y].d;       link2[x][l].r = r;       link2[x][r].l = l;       link2[u][y].d = d;       link2[d][y].u = u;       if (a[x][y] ==  L )         y = l;       else if (a[x][y] ==  R )         y = r;       else if (a[x][y] ==  U )         x = u;       else if (a[x][y] ==  D )         x = d;     }     if (dem == kq) ++res;   }   int main() {     nhap();     prep();     for (int i = 1; i <= n; ++i)       for (int j = 1; j <= m; ++j)         if (a[i][j] !=  . ) xl(i, j);     cout << kq <<    ;     for (int i = 1; i <= n; ++i)       for (int j = 1; j <= m; ++j)         if (a[i][j] !=  . ) xl1(i, j);     cout << res;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n, m;     scanf( %d%d , &n, &m);     vector<vector<pair<int, int> > > e(n, vector<pair<int, int> >());     for (int i = 1; i < n; ++i) {       int u, v, d;       scanf( %d%d%d , &u, &v, &d);       e[--u].push_back(make_pair(--v, d));     }     vector<pair<int, int> > info;     vector<set<int> > adt(n, set<int>()), eve(n, set<int>());     for (int i = 0; i < m; ++i) {       int s, t;       scanf( %d%d , &s, &t);       info.push_back(make_pair(--s, t));       adt[s].insert(i);     }     vector<pair<long long, long long> > seg, rseg;     auto upd_seg = [&](int x, int y, long long adt, bool add = 1) {       auto tmp = make_pair(info[x].second + adt, info[y].second + adt);       if (add) {         seg.push_back(tmp);       } else {         rseg.push_back(tmp);       }     };     vector<int> dsu(n, -1);     function<int(int)> dsu_find = [&](int u) {       return dsu[u] < 0 ? u : (dsu[u] = dsu_find(dsu[u]));     };     auto dsu_check = [&](int x, int y) {       return dsu_find(info[x].first) == dsu_find(info[y].first);     };     function<void(int, long long)> dfs = [&](int u, long long dis) {       for (auto &it : e[u]) {         int &v = it.first;         dfs(v, dis + it.second);         if (eve[u].size() < eve[v].size()) eve[u].swap(eve[v]);         set<int>::iterator vL = eve[v].begin();         while (vL != eve[v].end()) {           set<int>::iterator uR = eve[u].lower_bound(*vL);           if (uR != eve[u].begin()) {             set<int>::iterator uL = uR;             --uL;             if (uR != eve[u].end() && !dsu_check(*uL, *uR))               upd_seg(*uL, *uR, dis, 0);             upd_seg(*uL, *vL, dis);           }           if (uR == eve[u].end()) {             eve[u].insert(vL, eve[v].end());             vL = eve[v].end();           } else {             int upp = *uR;             set<int>::iterator vR = vL;             while (vR != eve[v].end() && *vR < upp) {               eve[u].insert(*vR);               vL = vR++;             }             upd_seg(*vL, upp, dis);             if (vR != eve[v].end() && !dsu_check(*vL, *vR))               upd_seg(*vL, *vR, dis, 0);             ++vL;           }         }         set<int>().swap(eve[v]);       }       if (!eve[u].empty()) {         int low = *eve[u].begin();         if (dsu_find(info[low].first) != e[u].back().first)           seg.push_back(make_pair(0LL, info[low].second + dis));       }       eve[u].insert(adt[u].begin(), adt[u].end());       set<int>().swap(adt[u]);       for (auto &it : e[u]) dsu[it.first] = u;     };     dfs(0, 0);     sort(seg.begin(), seg.end());     sort(rseg.begin(), rseg.end());     auto it = seg.begin(), jt = it, kt = rseg.begin();     while (it != seg.end()) {       if (kt != rseg.end() && *it == *kt) {         ++kt;       } else if (it == jt) {         ++jt;       } else {         *(jt++) = *it;       }       ++it;     }     assert(kt == rseg.end());     seg.erase(jt, seg.end());     long long cur = 0, lim = -1;     it = seg.begin();     priority_queue<long long, vector<long long>, greater<long long> > Q;     while (it != seg.end() || !Q.empty()) {       if (Q.empty() && (it->first) > cur) cur = it->first;       while (it != seg.end() && (it->first) <= cur) Q.push((it++)->second);       long long upp = Q.top();       Q.pop();       if (cur >= upp) {         lim = upp;         break;       }       ++cur;     }     int cnt = 0;     if (lim == -1) {       cnt += (int)seg.size();     } else {       for (auto &it : seg) cnt += it.second < lim;     }     printf( %lld %d n , lim, cnt);     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 = 2e6 + 7;   const double eps = 0.00000001;   long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; }   template <typename T>   inline T abs(T a) {     return a > 0 ? a : -a;   }   template <typename T>   inline T powMM(T a, T b) {     T ret = 1;     for (; b; b >>= 1ll, a = a * a % M) ret = 1ll * ret * a % M;     return ret;   }   set<int> SET;   set<int>::iterator it;   int n, m, t;   int i;   int len;   int mx;   char a[maxn], s[maxn];   int main() {     scanf( %d , &n);     for (i = 0; i <= 2e6; i++) SET.insert(i);     while (n--) {       scanf( %s , s);       len = strlen(s);       scanf( %d , &m);       while (m--) {         scanf( %d , &t);         t--;         for (it = SET.lower_bound(t); it != SET.end() && *it < t + len;              it = SET.lower_bound(t)) {           a[*it] = s[*it - t];           if (mx < *it) mx = *it;           SET.erase(it);         }       }     }     for (i = 0; i < mx + 1; i++) {       if (a[i])         putchar(a[i]);       else         putchar( a );     }   }


#include <bits/stdc++.h>   const int N = 1e6 + 5;   const int mod = 1e9 + 7;   const int MOD = mod - 1;   const double eps = 1e-18;   const double PI = acos(-1.0);   using namespace std;   char s[200];   int main() {     int n, m;     scanf( %d , &n), scanf( %d , &m);     scanf( %s , s + 1);     int l = strlen(s + 1);     while (m--) {       int l, r;       char a, b;       scanf( %d %d %c %c , &l, &r, &a, &b);       getchar();       for (int i = l; i <= r; i++)         if (s[i] == a) s[i] = b;     }     puts(s + 1);   }


#include <bits/stdc++.h>   using namespace std;   long long M = 1000000007;   int sum(int n) {     int ans = 0;     while (n) {       ans += n % 10;       n = n / 10;     }     return ans;   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     cout.tie(NULL);     int k;     cin >> k;     int ans = 18;     while (k) {       ans++;       if (sum(ans) == 10) k--;     }     cout << ans;   }


#include <bits/stdc++.h>   using namespace std;   template <typename X>   inline X abs(const X& a) {     return a < 0 ? -a : a;   }   template <typename X>   inline X sqr(const X& a) {     return a * a;   }   long long combine(long long n, int k) {     long long ans = 1;     for (int i = 0; i < k; i++) ans = (ans * (n - i)) / (i + 1);     return ans;   }   long long pw(long long a, long long p) {     long long ans = 1;     for (int i = 0; i < p; i++) ans *= a;     return ans;   }   int toint(string s) {     int n = 0, k = 1;     for (int i = (int)s.size() - 1; i >= 0; i--) {       n += (s[i] -  0 ) * k;       k *= 10;     }     return n;   }   int main() {     int k, m, n, p;     int a = 0, b;     cin >> n >> k >> p, b = n * p;     while (n > 1) {       m = 1;       while (2 * m <= n) m <<= 1;       a += m * k + m / 2;       n = n - m + m / 2;     }     cout << a <<     << b << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   pair<int, int> a[210000], b[2100000];   vector<pair<int, int> > ans;   int main() {     int N, M, i, j, k;     scanf( %d%d , &N, &M);     if (N <= M) {       puts( -1 );       return 0;     }     for (i = 0; i < N; i++) scanf( %d%d , &a[i].first, &a[i].second);     sort(a, a + N);     for (i = 0; i <= M; i++)       for (j = 0; i + j <= M; j++) {         int l = i, r = N - 1 - j;         int first = a[l].first + a[r].first, second = a[l].second + a[r].second;         bool flag = 1;         for (k = l; k <= r; k++)           b[k] = make_pair(first - a[k].first, second - a[k].second);         reverse(b + l, b + r + 1);         int p = l, q = l, cnt = 0;         while (p <= r && q <= r) {           if (a[p] < b[q])             ++p;           else if (a[p] > b[q])             ++q;           else {             ++cnt;             ++p;             ++q;           }         }         if (r - l + 1 - cnt + i + j <= M) ans.push_back(make_pair(first, second));       }     sort(ans.begin(), ans.end());     ans.resize(unique(ans.begin(), ans.end()) - ans.begin());     cout << ans.size() << endl;     for (i = 0; i < ans.size(); i++)       printf( %.12f %.12f n , ans[i].first / 2.0, ans[i].second / 2.0);   }


#include <bits/stdc++.h>   using namespace std;   long long N, L, W;   vector<long long> neg, pos;   long long INF32 = 0x7fffffff;   inline long long div_floor(long long a, long long b) {     return (long long)floor(1.0 * a / b);   }   int main() {     cin >> N >> L >> W;     for (int i = 0; i < N; ++i) {       long long x, d;       cin >> x >> d;       (d == 1 ? pos : neg).push_back(d == 1 ? x : x + L);     }     sort(pos.begin(), pos.end());     sort(neg.begin(), neg.end());     long long ans = 0;     for (auto x : pos) {       long long lval = max(-x - 1, x);       if (W == 1) {         if (x < 0) {         } else {           lval = INF32;         }       } else {         lval = max(lval, div_floor(((W + 1) * x), (W - 1)));       }       auto l = upper_bound(neg.begin(), neg.end(), lval);       ans += int(neg.end() - l);       lval = max(x, div_floor((W - 1) * x, (W + 1)));       long long rval = -x;       l = upper_bound(neg.begin(), neg.end(), lval);       auto r = lower_bound(neg.begin(), neg.end(), rval);       ans += max(0, int(r - l));     }     cout << ans << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long long inf = 1000000000000000;   const int mod = 1000000007;   int n, m, k;   int s[1000010];   long long a[1000010];   bool pos[1000010];   int lst[1000010];   long long get(int l) {     int r = 0;     int i = -1;     long long res = 0;     while (r < n) {       if (lst[r] <= i) {         return inf;       }       i = lst[r];       r = lst[r] + l;       ++res;     }     return res;   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     cin >> n >> m >> k;     for (int i = 0; i < m; i++) {       cin >> s[i];     }     for (int i = 0; i < k; i++) {       cin >> a[i];     }     for (int i = 0; i < n; i++) {       pos[i] = true;     }     for (int i = 0; i < m; i++) {       pos[s[i]] = false;     }     for (int i = 0; i < n; i++) {       if (pos[i]) {         lst[i] = i;       } else if (i) {         lst[i] = lst[i - 1];       } else         lst[i] = -1;     }     long long ans = inf;     for (int i = 0; i < k; i++) {       long long t = get(i + 1);       if (t != inf) {         ans = min(ans, a[i] * t);       }     }     if (ans == inf) {       ans = -1;     }     cout << ans <<   n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int Pow10(int deg) {     int res = 1;     while (deg-- > 0) res *= 10;     return res;   }   int ToInt(const string& from) {     int res = 0;     for (int i = 0; i < ((int)(from).size()); ++i) res = 10 * res + from[i] -  0 ;     return res;   }   void Solve() {     string buf;     do {       getline(cin, buf);     } while (((int)(buf).size()) < 4);     string year = buf.substr(4);     int lo = 1988;     for (int i = ((int)(year).size()) - 1; i >= 0; --i) {       int add = Pow10(((int)(year).size()) - i);       int base = lo / add;       int rem = ToInt(year.substr(i));       while (base * add + rem <= lo) ++base;       lo = base * add + rem;     }     cout << lo <<   n ;   }   int main() {     ios_base::sync_with_stdio(false);     int q;     cin >> q;     while (q-- > 0) Solve();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long long int N = 1e5 + 1, mod = 1000000007;   vector<long long int> adj[N];   signed main() {     ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL);     long long int pro = 1, temp, t, n, i, m, k, j, l, r, mid, x, y, z, rem,                   carry = 0, ind, ans = 0, mx = -LONG_LONG_MAX,                   mn = LONG_LONG_MAX, cnt = 0, curr = 0, prev, sum = 0, flag = 0,                   i1 = -1, i2 = -1;     cin >> n >> m;     for (i = 1; i <= m; i++) {       cin >> x >> y;       adj[x].push_back(y);       adj[y].push_back(x);     }     if (n < 2 || m >= (n * (n - 1)) / 2) {       return cout <<  NO , 0;     }     cout <<  YES n ;     long long int a[n + 1], b[n + 1];     for (i = 1; i <= n; i++) {       b[i] = 1;     }     curr = 2;     for (i = 1; i <= n; i++) {       flag = 0;       for (auto it : adj[i]) {         if (b[it] == b[i]) {           flag = 1;           b[it] = curr;         }       }       if (flag) {         curr++;       }     }     vector<pair<long long int, long long int> > v;     v.push_back({-1, 0});     for (i = 1; i <= n; i++) {       v.push_back({b[i], i});     }     sort(v.begin(), v.end());     for (i = 1; i <= n; i++) {       a[v[i].second] = i;     }     for (i = 1; i <= n; i++) {       cout << a[i] <<    ;     }     cout << endl;     for (i = 1; i <= n; i++) {       cout << b[i] <<    ;     }     return 0;   }


#include <bits/stdc++.h>   #pragma GCC optimization( O3 )   using namespace std;   template <typename T>   inline void Cin(T& first) {     char c;     T sign = 1;     first = 0;     for (c = getchar(); c <  0  || c >  9 ; c = getchar())       if (c ==  - ) sign = -1;     for (; c >=  0  && c <=  9 ; c = getchar()) first = first * 10 + c -  0 ;     first *= sign;   }   template <typename T>   inline void Out(T first) {     if (first > 9) Out(first / 10);     putchar(first % 10 +  0 );   }   template <typename T>   inline void Cout(T first, char c) {     if (first < 0) putchar( - );     first = abs(first);     Out(first);     putchar(c);   }   template <typename T, typename... Args>   inline void Cin(T& a, Args&... args) {     Cin(a);     Cin(args...);   }   template <typename T, typename... Args>   inline void Cout(T a, char c, Args... args) {     Cout(a, c);     Cout(args...);   }   const int N = 107;   const long double eps = 1e-8;   const long double e = 2.7182818284590452353602874713527;   int n, k, first[N], second[N];   long double low, high, mid, s, w, p[N], f[N][N];   void Enter() {     cin >> n >> k >> w;     w = 1 - w / 1000;     for (int i = 0; i <= n; ++i) cin >> first[i] >> second[i];   }   int SqrDis(int i) {     return (first[i] - first[0]) * (first[i] - first[0]) +            (second[i] - second[0]) * (second[i] - second[0]);   }   bool Check() {     for (int i = 1; i <= n; ++i) {       if ((long double)SqrDis(i) <= mid * mid)         p[i] = 1;       else         p[i] = pow(e, (1 - (long double)SqrDis(i) / (mid * mid)));     }     memset(&f, 0, sizeof f);     f[1][1] = p[1];     f[1][0] = 1 - p[1];     for (int i = 2; i <= n; ++i) {       f[i][0] = f[i - 1][0] * (1 - p[i]);       f[i][i] = f[i - 1][i - 1] * p[i];       for (int j = 1; j < i; ++j)         f[i][j] = f[i - 1][j - 1] * p[i] + f[i - 1][j] * (1 - p[i]);     }     s = 0;     for (int i = k; i <= n; ++i) s += f[n][i];     return s >= w;   }   void Solve() {     low = eps, high = 5000;     while (low + eps <= high) {       mid = (low + high) / 2.0;       if (Check())         high = mid - eps;       else         low = mid + eps;     }     cout << fixed << setprecision(9) << low;   }   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     if (fopen( test                 .inp ,                r )) {       freopen(            test             .inp ,            r , stdin);       freopen(            test             .out ,            w , stdout);     }     Enter(), Solve();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   template <typename T>   void maxtt(T& t1, T t2) {     t1 = max(t1, t2);   }   template <typename T>   void mintt(T& t1, T t2) {     t1 = min(t1, t2);   }   bool debug = 0;   int n, m, k;   int dx[4] = {0, 1, 0, -1}, dy[4] = {1, 0, -1, 0};   string direc =  URDL ;   const long long MOD2 = (long long)998244353 * (long long)998244353;   long long ln, lk, lm;   void etp(bool f = 0) {     puts(f ?  YES  :  NO );     exit(0);   }   void addmod(int& x, int y, int mod = 998244353) {     assert(y >= 0);     x += y;     if (x >= mod) x -= mod;     assert(x >= 0 && x < mod);   }   void et(int x = -1) {     printf( %d n , x);     exit(0);   }   long long fastPow(long long x, long long y, int mod = 998244353) {     long long ans = 1;     while (y > 0) {       if (y & 1) ans = (x * ans) % mod;       x = x * x % mod;       y >>= 1;     }     return ans;   }   long long gcd1(long long x, long long y) { return y ? gcd1(y, x % y) : x; }   pair<pair<int, int>, int> p[100035][4];   void cal(set<pair<pair<int, int>, int> >* S) {     n = (int)S[0].size();     if (n == 1) return;     set<pair<pair<int, int>, int> >::iterator it[4];     vector<int> vp(5, -(1 << 30));     for (int(j) = 0; (j) < (int)(4); (j)++) it[j] = S[j].begin();     for (int(cnt) = 1; (cnt) <= (int)(n); (cnt)++) {       for (int(j) = 0; (j) < (int)(4); (j)++)         maxtt(vp[j], (it[j]++)->first.second);       for (int(j) = 0; (j) < (int)(4); (j)++)         if (vp[j] <= it[j]->first.first) {           set<pair<pair<int, int>, int> > T[4];           auto it = S[j].begin();           for (int(i) = 0; (i) < (int)(cnt); (i)++) {             int id = it->second;             it++;             for (int(j) = 0; (j) < (int)(4); (j)++) {               T[j].insert(p[id][j]);               S[j].erase(p[id][j]);             }           }           cal(S);           cal(T);           return;         }     }     etp();   }   void fmain(int tid) {     scanf( %d , &n);     for (int(i) = 1; (i) <= (int)(n); (i)++) {       int a, b, c, d;       scanf( %d%d%d%d , &a, &b, &c, &d);       p[i][0] = {{a, c}, i};       p[i][1] = {{b, d}, i};       p[i][2] = {{-c, -a}, i};       p[i][3] = {{-d, -b}, i};     }     set<pair<pair<int, int>, int> > s[4];     for (int(i) = 1; (i) <= (int)(n); (i)++)       for (int(j) = 0; (j) < (int)(4); (j)++) s[j].insert(p[i][j]);     cal(s);     etp(1);   }   int main() {     int t = 1;     for (int(i) = 1; (i) <= (int)(t); (i)++) {       fmain(i);     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int OHT = 100100;   const int MAXINF = (2 << 26);   const int MININF = (~(2 << 26));   int vis1[110], vis2[110];   int main() {     int n, m, i, j;     cin >> n >> m;     vector<pair<int, int> > v;     for (i = 2; i <= n; i++) v.push_back(make_pair(i, 1));     for (j = 1; j <= m; j++) v.push_back(make_pair(1, j));     cout << v.size() << endl;     for (i = 0; i < v.size(); i++)       cout << v[i].first <<     << v[i].second << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long long MOD = 1000000007;   long long POW(long long a, long long b, long long MMM = MOD) {     long long ret = 1;     for (; b; b >>= 1, a = (a * a) % MMM)       if (b & 1) ret = (ret * a) % MMM;     return ret;   }   long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; }   long long lcm(long long a, long long b) {     if (a == 0 || b == 0) return a + b;     return a * (b / gcd(a, b));   }   int dx[] = {0, 1, 0, -1}, dy[] = {1, 0, -1, 0};   int ddx[] = {0, 0, 1, 1, 1, -1, -1, -1}, ddy[] = {1, -1, 1, 0, -1, 1, 0, -1};   tuple<int, int, int, int> query[100000];   int ans[100000];   map<int, vector<tuple<int, int, int> > > M;   int cc;   void upd(int i, vector<int>& tree, int k) {     int n = tree.size();     while (i < n) {       tree[i] += k;       i += (i & -i);     }   }   int sum(int i, vector<int>& tree) {     int c = 0;     while (i > 0) {       c += tree[i];       i -= (i & -i);     }     return c;   }   int main() {     int n;     scanf( %d , &n);     for (int(i) = (0); (i) <= (n - 1); (i) += (1)) {       int x, y, z;       scanf( %d%d%d , &x, &y, &z);       if (x == 3)         M[z].push_back(tuple<int, int, int>(x, y, cc++));       else         M[z].push_back(tuple<int, int, int>(x, y, -1));     }     for (auto it = M.begin(); it != M.end(); it++) {       vector<tuple<int, int, int> > v = it->second;       int N = v.size();       vector<int> vv;       for (int(i) = (0); (i) <= (N - 1); (i) += (1)) vv.push_back(get<1>(v[i]));       sort(vv.begin(), vv.end());       vv.resize(unique(vv.begin(), vv.end()) - vv.begin());       for (int(i) = (0); (i) <= (N - 1); (i) += (1))         get<1>(v[i]) =             lower_bound(vv.begin(), vv.end(), get<1>(v[i])) - vv.begin();       vector<int> tree(N + 5, 0);       for (int(i) = (0); (i) <= (N - 1); (i) += (1)) {         int x, y, z;         tie(x, y, z) = v[i];         if (x == 1)           upd(y + 1, tree, 1);         else if (x == 2)           upd(y + 1, tree, -1);         else           ans[z] = sum(y + 1, tree);       }     }     for (int(i) = (0); (i) <= (cc - 1); (i) += (1)) printf( %d n , ans[i]);   }


#include <bits/stdc++.h>   using namespace std;   int main() {     string n;     int index = 0;     cin >> n;     int l = n.length();     for (int i = l - 1; i > -1; i--) {       if (n[i] ==  4 )         index += pow(2, (l - 1 - i));       else         index += 2 * pow(2, (l - 1 - i));     }     cout << index << endl;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 113;   int add(int x, int y) {     x += y;     if (x >= 1000000007) x -= 1000000007;     return x;   }   int sub(int x, int y) {     if (x < y) x += 1000000007;     x -= y;     return x;   }   int mul(int x, int y) { return (long long)x * y % 1000000007; }   int qpow(int x, int y, int p = 1000000007) {     int res = 1;     while (y) {       if (y & 1) res = mul(res, x);       x = mul(x, x);       y >>= 1;     }     return res;   }   vector<int> G[N];   int f[N][N][N], g[N], h[N], d[N][N], sz[N];   void dfs(int x, int fa) {     f[x][1][0] = 1;     sz[x] = 1;     for (int j = 0; j < G[x].size(); j++)       if (G[x][j] != fa) {         int v = G[x][j];         dfs(v, x);         memset(d, 0, sizeof d);         for (int xsz = sz[x]; xsz >= 1; xsz--) {           for (int xedges = sz[x]; xedges >= 0; xedges--) {             if (f[x][xsz][xedges]) {               for (int sonedges = sz[v]; sonedges >= 0; sonedges--) {                 for (int sonsz = sz[v]; sonsz >= 1; sonsz--)                   if (f[v][sonsz][sonedges]) {                     d[xsz][xedges + sonedges] =                         add(d[xsz][xedges + sonedges],                             mul(f[x][xsz][xedges], f[v][sonsz][sonedges]));                     d[xsz + sonsz][xedges + sonedges + 1] =                         add(d[xsz + sonsz][xedges + sonedges + 1],                             mul(mul(f[x][xsz][xedges], f[v][sonsz][sonedges]),                                 mul(qpow(mul(xsz, sonsz), 1000000007 - 2),                                     xsz + sonsz)));                   }               }             }           }         }         memcpy(f[x], d, sizeof f[x]);         sz[x] += sz[v];       }   }   int n;   int binom[N][N];   int main() {     binom[0][0] = 1;     for (int i = 1; i < N; i++) {       binom[i][0] = 1;       for (int j = 1; j <= i; j++) {         binom[i][j] = add(binom[i - 1][j], binom[i - 1][j - 1]);       }     }     scanf( %d , &n);     for (int i = 1; i < n; i++) {       int u, v;       scanf( %d%d , &u, &v);       G[u].push_back(v);       G[v].push_back(u);     }     dfs(1, 0);     for (int num = 0; num < n; num++) {       for (int sz = 1; sz <= n; sz++) {         g[num] = add(g[num], f[1][sz][num]);       }       g[num] = mul(g[num], qpow(n, 1000000007 - 1 + n - num - 2));     }     for (int num = n - 1; num >= 0; num--) {       for (int _num = num; _num < n; _num++) {         h[num] = add(h[num], (_num - num) & 1                                  ? 1000000007 - mul(binom[_num][num], g[_num])                                  : mul(binom[_num][num], g[_num]));       }     }     for (int i = 0; i < n; i++) printf( %d  , h[i]);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int read() {     int res = 0, p = 1;     char ch = getchar();     while (!isdigit(ch)) {       if (ch ==  - ) p = -1;       ch = getchar();     }     while (isdigit(ch)) res = res * 10 + ch -  0 , ch = getchar();     return res * p;   }   const int mod = 1e9 + 7;   int t;   int main() {     t = read();     while (t--) {       long long n = read() * 2;       long long x = 1;       for (int i = 3; i <= n; ++i) {         x = (x * i) % mod;       }       printf( %d n , x);     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n, p, flag;   char s[1111];   int a[1111];   void dfs(int x) {     if (!x) {       flag = 0;       return;     }     while (1) {       a[x]++;       if (a[x] > p) {         dfs(x - 1);         a[x] = 0;         continue;       }       if (!(x > 1 && a[x] == a[x - 1] || x > 2 && a[x] == a[x - 2])) break;     }     if (x == n)       for (int i = 0; i < n; i++) s[i] = a[i + 1] - 1 +  a ;   }   int main() {     cin >> n >> p;     gets(s);     gets(s);     for (int i = 0; i < n; i++) a[i + 1] = s[i] -  a  + 1;     flag = 1;     dfs(n);     if (flag)       puts(s);     else       puts( NO );   }


#include <bits/stdc++.h>   using namespace std;   long long mod = 998244353;   long long powmod(long long a, long long b) {     long long r = 1;     while (b) {       if (b & 1) r = (r * a) % mod;       a = (a * a) % mod, b >>= 1;     }     return r;   }   long long modd(long long a, long long b) { return ((a % b) + b) % b; }   long long extendedEuclid(long long a, long long b, long long& x, long long& y) {     long long xx = y = 0, yy = x = 1, t;     while (b) {       long long q = a / b;       a %= b;       swap(a, b);       t = xx;       xx = x - q * xx;       x = t;       t = yy;       yy = y - q * yy;       y = t;     }     return a;   }   long long modularInverse(long long a) {     long long x, y, d = extendedEuclid(a, mod, x, y);     if (d > 1) return -1;     return modd(x, mod);   }   long long f[300000], fi[300000];   long long binom(long long a, long long b) {     return (((f[a] * fi[a - b]) % mod) * fi[b]) % mod;   }   int n, k, h[300000];   int main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     cout << fixed << setprecision(10);     cin >> n >> k;     for (int i = 0; i < n; i++) cin >> h[i];     f[0] = 1;     fi[0] = 1;     for (int i = 1; i <= n; i++)       f[i] = (f[i - 1] * i) % mod, fi[i] = modularInverse(f[i]);     int c = 0;     for (int i = 0; i < n; i++) c += h[i] != h[(i + 1) % n];     long long r = 0;     for (int s = 1; s <= c; s++) {       long long h = powmod(2, s);       if ((s & 1) == 0) h = (h + mod - binom(s, s / 2)) % mod;       h = (h * fi[2]) % mod;       h = (h * binom(c, s)) % mod;       r = (r + h * powmod(k - 2, c - s)) % mod;     }     r = (r * powmod(k, n - c)) % mod;     cout << r <<   n ;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n, b, cnt[105] = {0};     cin >> n;     vector<int> a;     for (int i = 0; i < n; i++) {       cin >> b;       a.push_back(b);       cnt[a[i]] = cnt[a[i]] + 1;     }     int y = 0;     int x;     x = *max_element(a.begin(), a.end());     for (int i = 0; i <= x; i++) {       if (cnt[i] >= y) {         y = cnt[i];       }     }     cout << y << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   template <class T, class U>   inline bool chkmax(T& a, U b) {     return (a < b) ? (a = b, true) : false;   }   template <class T, class U>   inline bool chkmin(T& a, U b) {     return (a > b) ? (a = b, true) : false;   }   template <class T>   inline vector<T> uniq(vector<T> vec) {     sort((vec).begin(), (vec).end()),         vec.erase(unique((vec).begin(), (vec).end()), end(vec));     return vec;   }   void solve();   int main(int argc, char** argv) {     ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);     cout.setf(ios_base::fixed), cout.precision(10);     solve();     double times = (clock() * 1.0 / CLOCKS_PER_SEC);     do {     } while (0);   }   template <int NV>   class undirectedEulerPath {    public:     vector<int> path;     multiset<int> G[NV];     void addedge(int u, int v) { G[u].insert(v), G[v].insert(u); }     void dfs(int cur) {       while (!G[cur].empty()) {         int tar = *G[cur].begin();         G[cur].erase(G[cur].begin());         G[tar].erase(G[tar].find(cur));         dfs(tar);       }       path.push_back(cur);     }     bool get() {       int src = -1, odd = 0, tot = 0;       for (int i = 0; i < NV; i++) {         tot += G[i].size();         if (G[i].size() % 2 == 1) {           odd++, src = (~src) ? src : i;         }       }       if (odd != 0 && odd != 2) return false;       dfs(odd ? src : 0);       reverse(path.begin(), path.end());       return (int)path.size() == tot / 2 + 1;     }   };   undirectedEulerPath<202020> solver;   void solve() {     int n;     cin >> n;     vector<int> b(n - 1), c(n - 1);     for (int i = 0; i < n - 1; i++) cin >> b[i];     for (int i = 0; i < n - 1; i++) cin >> c[i];     for (int i = 0; i < n - 1; i++) {       if (b[i] > c[i]) do {           cout << ( -1 ) <<   n ;           return;         } while (0);     }     vector<int> index;     for (int i = 0; i < n - 1; i++) {       index.push_back(b[i]), index.push_back(c[i]);     }     index = uniq(index);     for (int i = 0; i < n - 1; i++) {       b[i] = lower_bound((index).begin(), (index).end(), b[i]) - index.begin();       c[i] = lower_bound((index).begin(), (index).end(), c[i]) - index.begin();       solver.addedge(b[i], c[i]);     }     if (solver.get()) {       for (auto& x : solver.path) cout << index[x] <<    ;     } else {       do {         cout << ( -1 ) <<   n ;         return;       } while (0);     }   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int m, k, d[2000], s[2000], n = 0, big = 0, ans = 0;     cin >> m >> k;     for (int i = 0; i < m; i++) cin >> d[i];     for (int i = 0; i < m; i++) cin >> s[i];     for (int i = 0; i < m; i++) {       n += s[i];       if (s[i] > big) big = s[i];       while (n < d[i]) {         n += big;         ans += k;       }       n -= d[i];       ans += d[i];     }     cout << ans << endl;   }


#include <bits/stdc++.h>   using namespace std;   template <class T>   void rd(T &x) {     x = 0;     int f = 1;     char ch = getchar();     while (ch <  0  || ch >  9 ) {       if (ch ==  - ) f = -1;       ch = getchar();     }     while (ch >=  0  && ch <=  9 )       x = (x << 3) + (x << 1) + ch -  0 , ch = getchar();     x *= f;   }   const long long inf = 1e16;   const int maxn = 505;   int n;   long long f[maxn][maxn];   int nxt(int a, int b) {     int c = a + b;     if (c > n)       return c % n;     else       return c;   }   long long dfs(int l, int r) {     if (nxt(l, 1) == r) return 0;     if (f[l][r]) return f[l][r];     if (nxt(l, 2) == r) return l * nxt(l, 1) * nxt(l, 2);     long long res = inf;     int u = nxt(l, 1);     while (u != r) {       res = min(res, l * r * u + dfs(l, u) + dfs(u, r));       u = nxt(u, 1);     }     return f[l][r] = res;   }   int main() {     rd(n);     printf( %lld n , dfs(1, n));     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long int MOD = 998244353;   long long int INF = 1e18;   long long int vv;   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     cout.tie(NULL);     long long int i, j, k = 0, l = 0, r = 0, m = 0, n, c, p = 0, cnt = 0,                         cnt1 = 0, x, y, z = 0, a, b, low, mid, high, ans = 0,                         maxi = 0, mini = INF, t, x1, y1;     string s;     cin >> s;     n = s.size();     queue<long long int> q;     long long int V[n];     bool visited[10];     long long int dist[10];     long long int ds[10][10];     for (i = 0; i < n; i++) {       k = s[i];       k = k - 48;       V[i] = k;     }     for (i = 0; i <= 9; i++) {       for (j = 0; j <= 9; j++) {         for (l = 0; l <= 9; l++) {           for (r = 0; r <= 9; r++) {             ds[l][r] = -1;           }         }         for (l = 0; l <= 9; l++) {           for (r = 0; r <= 9; r++) {             visited[r] = false;             dist[r] = INF;           }           q.push(l);           dist[l] = 0;           if (i == 0 || j == 0) {             ds[l][l] = 1;             visited[l] = true;           }           while (!q.empty()) {             k = q.front();             q.pop();             r = (k + i) % 10;             if (visited[r] == false) {               visited[r] = true;               dist[r] = (dist[k] + 1);               ds[l][r] = dist[r];               q.push(r);             }             r = (k + j) % 10;             if (visited[r] == false) {               visited[r] = true;               dist[r] = (dist[k] + 1);               ds[l][r] = dist[r];               q.push(r);             }           }         }         cnt = 0;         for (l = 0; l < n - 1; l++) {           if (ds[V[l]][V[l + 1]] == -1) {             cnt = -1;             goto last;           } else {             cnt += ds[V[l]][V[l + 1]] - 1;           }         }       last:         cout << cnt <<    ;       }       cout << endl;     }   }


#include <bits/stdc++.h>   using namespace std;   long long n, m;   long long a[201];   char s[30];   long long b[30];   long long dp[2000000];   long long f(long long mask) {     if (mask == (1 << m) - 1) return 0;     if (dp[mask] != -1) return dp[mask];     long long count = __builtin_popcount(mask);     char ch = s[count];     long long move = b[count];     long long ans = -1e8;     long long sign = 1;     if (move == 2) sign = -1;     ans *= sign;     for (long long i = 0; i < m; i++) {       if ((mask) & (1 << i)) continue;       if (ch ==  p ) {         if (sign == 1)           ans = max(ans, f((mask | (1 << i))) + sign * a[i]);         else           ans = min(ans, f((mask | (1 << i))) + sign * a[i]);       } else {         if (sign == 1)           ans = max(ans, f((mask | (1 << i))));         else           ans = min(ans, f((mask | (1 << i))));       }     }     return dp[mask] = ans;   }   int32_t main() {     ios::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     ;     cin >> n;     for (long long i = 0; i < n; i++) cin >> a[i];     sort(a, a + n);     reverse(a, a + n);     cin >> m;     for (long long i = 0; i < m; i++) cin >> s[i] >> b[i];     memset(dp, -1, sizeof(dp));     cout << f(0);   }


#include <bits/stdc++.h>   using namespace std;   int a[2010], b[2010];   bool vis[2010];   char *p1, *p2, buf[100000];   int rd() {     int x = 0, f = 1;     char c =         (p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 100000, stdin), p1 == p2)              ? EOF              : *p1++);     while (c < 48) {       if (c ==  - ) f = -1;       c = (p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 100000, stdin), p1 == p2)                ? EOF                : *p1++);     }     while (c > 47) {       x = (((x << 2) + x) << 1) + (c ^ 48),       c = (p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 100000, stdin), p1 == p2)                ? EOF                : *p1++);     }     return x * f;   }   map<int, int> MP;   int n;   bool check(int x) {     for (int i = 1; i <= n - x + 1; i++) {       memset(vis, false, sizeof vis);       bool flag = true;       for (int j = 1; j < i; j++) {         if (vis[a[j]]) {           flag = false;         }         vis[a[j]] = true;       }       for (int j = i + x; j <= n; j++) {         if (vis[a[j]]) {           flag = false;         }         vis[a[j]] = true;       }       if (flag) {         return true;       }     }     return false;   }   int main() {     n = rd();     for (int i = 1; i <= n; i++) {       a[i] = rd();       b[i] = a[i];     }     sort(b + 1, b + n + 1);     b[0] = b[1] - 1;     int cnt = 0;     for (int i = 1; i <= n; i++) {       if (b[i] != b[i - 1]) {         MP[b[i]] = ++cnt;       }     }     for (int i = 1; i <= n; i++) {       a[i] = MP[a[i]];     }     if (cnt == n) {       puts( 0 );       return 0;     }     int ans = n;     int l = 1, r = n;     while (l <= r) {       int mid = (l + r) >> 1;       if (check(mid)) {         ans = mid;         r = mid - 1;       } else {         l = mid + 1;       }     }     cout << ans << endl;   }


#include <bits/stdc++.h>   #pragma GCC optimize( O2 )   using namespace std;   inline long long input() {     long long n;     cin >> n;     return n;   }   string base2(long long n) {     string a =   ;     while (n >= 2) {       a += (char)(n % 2 +  0 );       n /= 2;     }     a += (char)(n +  0 );     reverse((a).begin(), (a).end());     return a;   }   const int MAXN = 1e6 + 10;   const int MOD = 1e9 + 7;   const int MOD2 = 998244353;   const long long INF = 8e18;   long long pw(long long a, long long b) {     return (!b ? 1                : (b & 1 ? (a * pw(a * a % MOD, b / 2)) % MOD                         : pw(a * a % MOD, b / 2)));   }   int cnt[256], p[256];   int main() {     ios::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     ;     int q;     cin >> q;     while (q--) {       for (int i = 0; i < 256; i++) {         cnt[i] = 0;         p[i] = 0;       }       string s, s2, t;       cin >> s >> s2;       int n = (int)s.size();       for (int i = 0; i < n; i++) {         p[(int)s[i]] = i;         cnt[(int)s[i]]++;       }       for (int i = 0; i < 256; i++) {         for (int j = 0; j < cnt[i]; j++) {           t += string(1, i);         }       }       for (int i = 0; i < n; i++) {         if (s[i] != t[i]) {           swap(s[i], s[p[(int)t[i]]]);           break;         }       }       if (s >= s2) {         cout <<  ---               <<   n ;       } else {         cout << s <<   n ;       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int maxn = 100000 + 5;   int g[10][10];   int a[5];   int ans;   int cal() {     int sum = 0;     for (int i = 0; i < 5; ++i) {       for (int j = i; j < 4; j += 2) sum += g[a[j]][a[j + 1]] + g[a[j + 1]][a[j]];     }     return sum;   }   int main() {     for (int i = 0; i < 5; ++i)       for (int j = 0; j < 5; ++j) cin >> g[i][j];     for (int i = 0; i < 5; ++i) a[i] = i;     ans = -1;     do {       ans = max(ans, cal());     } while (next_permutation(a, a + 5));     cout << ans << endl;   }


#include <bits/stdc++.h>   using namespace std;   long long arr[330];   set<long long> st;   map<long long, bool> mp;   int main() {     long long t, n, i, j, k, l, m;     ios_base::sync_with_stdio(0);     cin.tie(0);     long long p;     cin >> t;     while (t--) {       cin >> n;       for (i = 1; i <= n; i++) cin >> arr[i], mp[arr[i]] = 1;       sort(arr + 1, arr + n + 1);       i = 1, j = n;       while (i <= j) {         long long op = arr[i] * arr[j];         st.insert(op);         i++;         j--;       }       if (st.size() == 1) {         bool flag = 1;         long long ans = *(st.begin());         for (i = 2; i <= sqrt(ans); i++) {           if (ans % i == 0) {             if (mp.find(i) == mp.end() || mp.find(ans / i) == mp.end())               flag = false;           }         }         if (flag)           cout << *(st.begin()) <<   n ;         else           cout << -1 <<   n ;       } else         cout << -1 <<   n ;       st.clear();       mp.clear();     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n, m, s;   int even_odd[100005][2];   int visited[100005];   vector<int> ans;   vector<int> v[100005];   int dfs(int s, int p) {     if (v[s].empty() && p) {       ans.push_back(s);       return 1;     }     for (auto it : v[s]) {       if (even_odd[it][p ^ 1] == 0) {         even_odd[it][p ^ 1] = s;         if (dfs(it, p ^ 1) == 1) {           ans.push_back(s);           return 1;         }       }     }     return 0;   }   bool dfs2(int source) {     visited[source] = 1;     for (auto it : v[source])       if (visited[it] == 1 || (visited[it] == 0 && dfs2(it))) return true;     visited[source] = 2;     return false;   }   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     cin >> n >> m;     for (int i = 0; i < n; i++) {       int k;       cin >> k;       for (int j = 0; j < k; j++) {         int a;         cin >> a;         v[i + 1].push_back(a);       }     }     cin >> s;     if (dfs(s, 0)) {       cout <<  Win n ;       for (int i = ans.size() - 1; i >= 0; i--) cout << ans[i] <<    ;       return 0;     }     if (dfs2(s)) {       cout <<  Draw n ;       return 0;     }     cout <<  Lose n ;     return 0;   }


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