#include <bits/stdc++.h> n using namespace std; n struct type { n   int a, b; n } b[17]; n int p[17]; n int a[17]; n int ans[101]; n bitset<2001> f[65536]; n int n, K, L, i, j, k, l, sum, len, s, tot; n bool cmp(type a, type b) { return a.b > b.b; } n int main() { n   p[1] = 1; n   for (i = 2; i <= 16; i++) p[i] = p[i - 1] << 1; n   scanf( %d%d , &n, &K); n   L = (p[n] << 1) - 1; n   for (i = 1; i <= n; i++) scanf( %d , &a[i]), sum += a[i]; n   sum /= K; n   f[0][0] = 1; n   for (i = 1; i <= L; i++) { n     for (j = 1; j <= n; j++) n       if (i & p[j]) f[i] |= f[i ^ p[j]] << a[j]; n     for (j = sum; j >= 1; j--) n       if (f[i][j * K]) f[i][j] = 1; n   } n   if (!f[L][1]) n     printf( NO n ); n   else { n     printf( YES n ); n     s = L; n     j = 1; n     while (j) { n       if (j <= sum && f[s][j * K]) { n         j *= K; n         ans[++len] = -1; n         continue; n       } n       for (i = 1; i <= n; i++) n         if (s & p[i] && j >= a[i] && f[s - p[i]][j - a[i]]) { n           s -= p[i]; n           j -= a[i]; n           ans[++len] = i; n           break; n         } n     } n     j = 0; n     for (i = 1; i <= len; i++) n       if (ans[i] == -1) n         ++j; n       else n         b[++tot] = {a[ans[i]], -j}; n     while (tot > 1) { n       sort(b + 1, b + tot + 1, cmp); n       printf( %d %d n , b[tot - 1].a, b[tot].a); n       b[tot - 1].a += b[tot].a; n       --tot; n       while (!(b[tot].a % K)) b[tot].a /= K, ++b[tot].b; n     } n   } n } n


#include <bits/stdc++.h> n const int N = 209, MOD = 1000000007; n long long f[N][N], area[N][N]; n struct point { n   long long x, y; n   point(const long long _x = 0, const long long _y = 0) : x(_x), y(_y) {} n } p[N]; n int n; n inline int sign(long long x) { return x < 0 ? -1 : x > 0; } n inline long long cpr(const point &a, const point &b, const point &c) { n   return (b.x - a.x) * (c.y - a.y) - (c.x - a.x) * (b.y - a.y); n } n inline bool cross(point &u, point &v, point &a, point &b) { n   if (std::max(u.x, v.x) < std::min(a.x, b.x) || n       std::min(u.x, v.x) > std::max(a.x, b.x)) n     return false; n   if (std::max(u.y, v.y) < std::min(a.x, b.y) || n       std::min(u.y, v.y) > std::max(a.y, b.y)) n     return false; n   if (sign(cpr(u, v, a)) * sign(cpr(u, v, b)) <= 0 && n       sign(cpr(a, b, u)) * sign(cpr(a, b, v)) <= 0) n     return true; n   return false; n } n bool check(int i, int j) { n   if (i + 1 == j || i == 1 && j == n) return true; n   if (sign(cpr(p[i], p[j], p[i + 1])) * sign(cpr(p[i], p[j], p[i - 1])) > 0) n     return false; n   for (int k = 1; k <= n; ++k) n     if (k != i && k % n + 1 != i && k != j && k % n + 1 != j) { n       if (cross(p[i], p[j], p[k], p[k + 1])) return false; n     } else if (!cpr(p[i], p[j], p[(k == i || k == j) ? k + 1 : k])) n       return false; n   return true; n } n long long F(int i, int j) { n   if (f[i][j] != -1) return f[i][j]; n   if (i + 1 == j) return 1; n   f[i][j] = 0; n   for (int k = i + 1; k < j; ++k) n     if (cpr(p[i], p[j], p[k]) > 0) f[i][j] += F(i, k) * F(k, j) % MOD; n   f[i][j] %= MOD; n   return f[i][j]; n } n int main() { n   memset(f, -1, sizeof f); n   scanf( %d , &n); n   long long S = 0; n   for (int i = 1; i <= n; ++i) scanf( %I64d%I64d , &p[i].x, &p[i].y); n   p[n + 1] = p[1]; n   p[0] = p[n]; n   for (int i = 1; i <= n; ++i) S += cpr(point(0, 0), p[i], p[i + 1]); n   if (S > 0) std::reverse(p + 1, p + 1 + n); n   p[n + 1] = p[1]; n   p[0] = p[n]; n   printf( %I64d n , F(1, n)); n } n


#include <bits/stdc++.h> n int main(void) { n   int n; n   scanf( %d , &n); n   while (1) { n     if (n % 2 == 1) { n       int atk = n - 1; n       n = n - atk; n     } else if (n % 2 == 0) { n       printf( Mahmoud n ); n       break; n     } n     if (n % 2 == 1) { n       printf( Ehab n ); n       break; n     } else { n       int atk2 = n - 1; n       n = n - atk2; n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n long long gcd(long long a, long long b) { n   if (b == 0) return a; n   return gcd(b, a % b); n } n long long power(long long first, unsigned long long second, n                 unsigned long long m) { n   if (second == 0) return 1; n   long long p = power(first, second / 2, m) % m; n   p = (p * p) % m; n   return (second % 2 == 0) ? p : (first * p) % m; n } n long long modInverse(long long a, long long m) { n   { return power(a, m - 2, m); } n } n const long long maxn = 500004; n void __print(long long first) { cerr << first; } n void __print(float first) { cerr << first; } n void __print(double first) { cerr << first; } n void __print(long double first) { cerr << first; } n void __print(char first) { cerr <<      << first <<     ; } n void __print(const char *first) { cerr <<     << first <<    ; } n void __print(const string &first) { cerr <<     << first <<    ; } n void __print(bool first) { cerr << (first ?  true  :  false ); } n template <typename T, typename V> n void __print(const pair<T, V> &first) { n   cerr <<  { ; n   __print(first.fi); n   cerr <<  , ; n   __print(first.se); n   cerr <<  } ; n } n template <typename T> n void __print(const T &first) { n   long long f = 0; n   cerr <<  { ; n   for (auto &i : first) cerr << (f++ ?  ,  :   ), __print(i); n   cerr <<  } ; n } n void _print() { cerr <<  ] n ; } n template <typename T, typename... V> n void _print(T t, V... v) { n   __print(t); n   if (sizeof...(v)) cerr <<  ,  ; n   _print(v...); n } n struct BIT { n   long long N; n   vector<long long> bit; n   void init(long long n) { n     N = n; n     bit.assign(n + 4, 0); n   } n   void update(long long idx, long long val) { n     idx++; n     while (idx <= N) { n       bit[idx] += val; n       idx += idx & -idx; n     } n   } n   long long query(long long idx) { n     long long ans = 0; n     idx++; n     while (idx > 0) { n       ans += bit[idx]; n       idx -= idx & -idx; n     } n     return ans; n   } n   long long search(long long val) { n     long long curSum = 0, pos = 0; n     for (long long i = log2(N) + 1; i >= 0; i--) { n       if (pos + (1 << i) < N && curSum + bit[pos + (1 << i)] < val) { n         curSum += bit[pos + (1 << i)]; n         pos += (1 << i); n       } n     } n     return pos + 1; n   } n   void updateMax(long long idx, long long val) { n     while (idx <= N) { n       bit[idx] = max(bit[idx], val); n       idx += idx & -idx; n     } n   } n   long long prefMax(long long idx) { n     long long ans = -2e9; n     while (idx > 0) { n       ans = max(ans, bit[idx]); n       idx -= idx & -idx; n     } n     return ans; n   } n }; n void solve() { n   BIT bit; n   long long n; n   cin >> n; n   bit.init(n + 4); n   long long a[n], b[n], c[n + 3]; n   memset(c, 0, sizeof c); n   memset(a, 0, sizeof a); n   memset(b, 0, sizeof b); n   for (long long i = 0; i < n; i++) { n     cin >> a[i]; n     ; n     bit.update(i, 1); n   } n   for (long long i = 0; i < n; i++) { n     c[n - i - 1] = bit.query(a[i]) - 1; n     bit.update(a[i], -1); n   } n   for (long long i = 0; i < n; i++) { n     cin >> b[i]; n     bit.update(i, 1); n   } n   for (long long i = 0; i < n; i++) { n     c[n - i - 1] += bit.query(b[i]) - 1; n     bit.update(b[i], -1); n   } n   for (long long i = 1; i <= n + 2; i++) { n     c[i + 1] += c[i] / (i + 1); n     c[i] %= i + 1; n   } n   c[n] = 0; n   for (long long i = 0; i < n; i++) bit.update(i, 1); n   for (long long i = 1; i <= n; i++) { n     long long first = c[n - i]; n     first++; n     long long l = 0, r = n - 1; n     long long wh; n     while (l <= r) { n       long long mid = l + r >> 1; n       if (bit.query(mid) >= first) { n         wh = mid; n         r = mid - 1; n       } else { n         l = mid + 1; n       } n     } n     cout << wh <<    ; n     bit.update(wh, -1); n   } n } n signed main() { n   ios_base ::sync_with_stdio(0); n   cin.tie(NULL); n   cout.tie(NULL); n   solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long mod = 998244353, INF = 1e9; n const long long N = 2e5 + 1; n const double ep = 1e-10; n const double pi = 3.141592653589793238; n void floyd() { n   long long n; n   cin >> n; n   string a, b; n   cin >> a >> b; n   long long a1 = 0, a2 = 0, k = 0, ans = INF, in = -1, in2 = -1; n   for (long long i = 0; i < n; i++) { n     if (a[i] ==  1 ) a1++; n     if (b[i] ==  1 ) a2++; n     if (a[i] ==  1  && b[i] ==  0 ) { n       k++; n       in2 = i; n     } n     if (a[i] ==  1  && b[i] ==  1 ) { n       in = i; n     } n   } n   if (a1 != a2 && (n - a1 + 1) != a2) { n     cout << -1 <<   n ; n     return; n   } n   if (a1 == a2) { n     ans = min(k * 2, INF); n   } n   string t = a; n   for (long long i = 0; i < n; i++) { n     if (a[i] ==  1 ) n       a[i] =  0 ; n     else n       a[i] =  1 ; n   } n   if (in != -1) { n     a[in] =  1 ; n     long long k1 = 0, k2 = 0, k3 = 0, d = INF; n     for (long long i = 0; i < n; i++) { n       if (a[i] ==  1 ) k1++; n       if (b[i] ==  1 ) k2++; n       if (a[i] ==  1  && b[i] ==  0 ) { n         k3++; n       } n     } n     if (k1 == k2) { n       d = k3 * 2 + 1; n     } n     ans = min(ans, d); n   } n   for (long long i = 0; i < n; i++) { n     if (t[i] ==  1 ) n       t[i] =  0 ; n     else n       t[i] =  1 ; n   } n   if (in2 != -1) { n     t[in2] =  1 ; n     long long k1 = 0, k2 = 0, k3 = 0, d = INF; n     for (long long i = 0; i < n; i++) { n       if (t[i] ==  1 ) k1++; n       if (b[i] ==  1 ) k2++; n       if (t[i] ==  1  && b[i] ==  0 ) { n         k3++; n       } n     } n     if (k1 == k2) { n       d = k3 * 2 + 1; n     } n     ans = min(ans, d); n   } n   if (ans == INF) { n     cout << -1 <<   n ; n     return; n   } n   cout << ans <<   n ; n } n int32_t main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   long long t = 1; n   cin >> t; n   while (t--) { n     floyd(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct node { n   int l, r; n } h[(200000 + 5)]; n int ql, qr; n namespace Seg_tree { n int hn, hv[(15000000 + 5)], ch[(15000000 + 5)][2]; n void Modify(int &o, int L, int R, int p) { n   if (!o) o = ++hn; n   if (L == R) { n     ++hv[o]; n     return; n   } n   if (p <= ((L + R) >> 1)) n     Modify(ch[o][0], L, ((L + R) >> 1), p); n   else n     Modify(ch[o][1], ((L + R) >> 1) + 1, R, p); n   hv[o] = hv[ch[o][0]] + hv[ch[o][1]]; n } n int Query(int &o, int L, int R) { n   if (!o) return 0; n   if (ql <= L && qr >= R) return hv[o]; n   int ret = 0; n   if (ql <= ((L + R) >> 1)) ret += Query(ch[o][0], L, ((L + R) >> 1)); n   if (qr > ((L + R) >> 1)) ret += Query(ch[o][1], ((L + R) >> 1) + 1, R); n   return ret; n } n };  // namespace Seg_tree n using namespace Seg_tree; n int n, ml[(200000 + 5)], mr[(200000 + 5)], mc[(200000 + 5)], rt[(200000 + 5)]; n inline int lowbit(int x) { return x & (-x); } n void Add(int x, int y) { n   while (x <= (n << 1)) { n     Modify(rt[x], 1, (n << 1), y); n     x += lowbit(x); n   } n } n int Sum(int x) { n   int ret = 0; n   while (x > 0) { n     ret += Query(rt[x], 1, n << 1); n     x -= lowbit(x); n   } n   return ret; n } n int Qus(int l1, int r1, int l2, int r2) { n   if (l1 > r1 || l2 > r2) return 0; n   ql = l2; n   qr = r2; n   return Sum(r1) - Sum(l1 - 1); n } n void Init() { n   for (int i = (1); i <= (int)(n); ++i) Add(h[i].l, h[i].r); n   for (int i = (1); i <= (int)(n); ++i) { n     ml[i] = Qus(h[i].l + 1, h[i].r - 1, h[i].l + 1, h[i].r - 1); n     mc[i] = Qus(1, h[i].l - 1, h[i].l + 1, h[i].r - 1) + n             Qus(h[i].l + 1, h[i].r - 1, h[i].r + 1, n << 1); n     mr[i] = n - 1 - ml[i] - mc[i]; n   } n } n int Calc(node a, int v) { n   if (v > a.l && v < a.r) return true; n   return false; n } n bool Cross(node a, node b) { n   int t1 = Calc(a, b.l), t2 = Calc(a, b.r); n   return t1 ^ t2; n } n bool Check(node me, node a, node b) { n   int ta = Calc(me, a.l), tb = Calc(me, b.l); n   return ta == tb; n } n int main() { n   scanf( %d , &n); n   for (int i = (1); i <= (int)(n); ++i) { n     scanf( %d%d , &h[i].l, &h[i].r); n     if (h[i].l > h[i].r) swap(h[i].l, h[i].r); n   } n   if (n < 3) { n     puts( 0 ); n     return 0; n   } n   if (n > 10000) n     Init(); n   else { n     for (int i = (1); i <= (int)(n); ++i) n       for (int j = (1); j <= (int)(n); ++j) n         if (i != j) { n           if (Cross(h[i], h[j])) n             ++mc[i]; n           else { n             if (Calc(h[i], h[j].l)) n               ++ml[i]; n             else n               ++mr[i]; n           } n         } n   } n   long long ans = 1ll * n * (n - 1) * (n - 2) / 6; n   for (int i = (1); i <= (int)(n); ++i) ans -= 1ll * ml[i] * mr[i]; n   long long cnt = 0; n   for (int i = (1); i <= (int)(n); ++i) cnt += 1ll * mc[i] * (ml[i] + mr[i]); n   cnt /= 2; n   ans -= cnt; n   printf( %I64d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MX = 200200, MSK = (1 << 20) - 1; n int T, n, m, i, x, y, z, pos, cnt, le, ri, h, fi[MX], lst[MX], s[MX], t[MX], n     ch[MX], a[18][MX], npw[MX], L[MX], R[MX], A[MX]; n priority_queue<long long, vector<long long>, greater<long long>> q; n long long seg[MX * 88], d[MX], sf, j; n vector<int> g[MX], f[MX]; n void dfs(int i, long long dep) { n   a[0][++T] = i; n   fi[i] = T; n   d[i] = dep; n   for (int j = 0; j < g[i].size(); j++) { n     dfs(g[i][j], dep + f[i][j]); n     a[0][++T] = i; n   } n   lst[i] = T; n } n int lca(int i, int j) { n   if (i > j) return lca(j, i); n   int len = npw[j - i + 1]; n   int le = a[len][i]; n   int ri = a[len][j - (1 << len) + 1]; n   if (d[le] <= d[ri]) return le; n   return ri; n } n int fndmax(int l, int r) { n   int res = max(A[l], A[r]), x, nxt; n   for (x = l;; x = nxt) { n     nxt = (x << 1) - (x & (x - 1)); n     if (nxt > r) break; n     res = max(res, L[x]); n   } n   res = max(res, A[x]); n   for (x = r;; x = nxt) { n     nxt = (x & (x - 1)); n     if (nxt < l) break; n     res = max(res, R[x]); n   } n   res = max(res, A[x]); n   return res; n } n void mdf(int i, int v) { n   A[i] = max(A[i], v); n   for (int x = i; x <= T; x = (x << 1) - (x & (x - 1))) R[x] = max(R[x], v); n   for (int x = i; x > 0; x &= x - 1) L[x] = max(L[x], v); n } n void printans(int i) { n   for (cnt = x = 0; x < i; x++) { n     y = (seg[x] >> 20) & MSK; n     if (t[seg[x] & MSK] + d[y] < q.top()) ++cnt; n   } n   printf( %I64d %d n , q.top(), cnt); n } n bool cmp(long long x, long long y) { n   if ((y >> 40) == 0) return false; n   if ((x >> 40) == 0) return true; n   return t[x >> 40] + d[(x >> 20) & MSK] < t[y >> 40] + d[(y >> 20) & MSK]; n } n int main() { n   scanf( %d%d , &n, &m); n   for (i = 1; i < n; i++) { n     scanf( %d%d%d , &x, &y, &z); n     g[x].push_back(y); n     f[x].push_back(z); n   } n   for (i = 1; i <= n; i++) ch[i] = int(g[i].size()) - 1; n   dfs(1, 0); n   for (x = 1; x < 18; x++) n     for (i = 1; i <= T; i++) { n       a[x][i] = a[x - 1][i]; n       y = i + (1 << (x - 1)); n       if (y <= T && d[a[x - 1][y]] < d[a[x][i]]) a[x][i] = a[x - 1][y]; n     } n   for (x = 1, y = -1, i = 1; i <= T; i++) { n     if (i == x) { n       x *= 2; n       y++; n     } n     npw[i] = y; n   } n   for (i = 1; i <= m; i++) { n     scanf( %d%d , &s[i], &t[i]); n     pos = fi[s[i]]; n     for (j = 1; j != s[i]; j = y) { n       z = fndmax(fi[j] + 1, lst[j]); n       if (z != 0) { n         j = lca(fi[s[z]], pos); n         if (j == s[i]) break; n       } n       y = g[j][ch[j]]; n       z = fndmax(fi[y], lst[y]); n       if (pos < fi[y] || pos > lst[y]) { n         le = 0; n         ri = int(g[j].size()) - 1; n         while (le < ri) { n           h = (le + ri) / 2; n           if (lst[g[j][h]] < pos) n             le = h + 1; n           else n             ri = h; n         } n         seg[cnt++] = ((((1LL * z) << 20) ^ j) << 20) ^ i; n         y = g[j][ch[j] = ri]; n       } n     } n     mdf(pos, i); n   } n   sort(seg, seg + cnt, cmp); n   for (j = i = 0; i < cnt; i++) { n     z = (seg[i] >> 40); n     y = (seg[i] >> 20) & MSK; n     sf = z ? t[z] + d[y] + 1 : 1; n     for (; j < sf && !q.empty(); j++) { n       if (q.top() < j) { n         printans(i); n         return 0; n       } n       q.pop(); n     } n     j = sf; n     q.push(t[seg[i] & MSK] + d[y]); n   } n   while (!q.empty()) { n     if (q.top() < j) { n       printans(cnt); n       return 0; n     } n     q.pop(); n     j++; n   } n   printf( -1 %d n , cnt); n   return 0; n } n


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


#include <bits/stdc++.h> n namespace std { n long long abs(long long x) { return x < 0 ? -x : x; } n }  // namespace std n using namespace std; n void dprintf(char* format, ...) { n   fprintf(stderr,  DEBUG:  ); n   va_list argp; n   va_start(argp, format); n   vfprintf(stderr, format, argp); n   va_end(argp); n   fprintf(stderr,   n ); n } n struct Exception { n   const char* message; n   const int line; n   Exception(const char* m, int l) : message(m), line(l) {} n }; n static char stringReader[2097152]; n static inline bool ReadLine() { n   char* ptr = fgets(stringReader, sizeof(stringReader), stdin); n   if (ptr == 0) return false; n   int N = strlen(stringReader); n   if (stringReader[N - 1] ==   n ) stringReader[N - 1] = 0; n   return true; n } n const long long ooLL = 0x3f3f3f3f3f3f3f3fLL; n const int oo = 0x3f3f3f3f; n const double eps = 1e-9; n int main() { n   try { n     int N; n     scanf( %d , &N); n     int nt = 0; n     int sol = oo; n     vector<int> cnt(2 * N + 1, 0); n     char t[1024]; n     scanf( %s , t); n     for (int i = 0; i < N; ++i) { n       if (t[i] ==  T ) { n         ++nt; n         cnt[i + 1] = cnt[i + N + 1] = 1; n       } n     } n     for (int i = 1; i <= 2 * N; ++i) { n       cnt[i] += cnt[i - 1]; n     } n     int nh = N - nt; n     for (int i = 1; i <= N; ++i) { n       int have = cnt[i + nt - 1] - cnt[i - 1]; n       sol = min(sol, nt - have); n     } n     printf( %d n , sol); n     return 0; n   } catch (const Exception& exception) { n     fprintf(stderr,  ****************************** n ); n     fprintf(stderr,  ERROR.%d: %s n , exception.line, exception.message); n     fprintf(stderr,  ****************************** n ); n     throw exception; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int arr[200000 + 10]; n int arr2[200000 + 10]; n int main() { n   int n, num1 = 0, num2 = 0, tem1, l1 = 0, l2 = 0, num = 0; n   long long ans = 0; n   cin >> n; n   for (int i = 0; i < n; i++) { n     cin >> tem1; n     if (tem1 == 0) { n       ans += num; n     } else n       num++; n   } n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long n, m; n   char T; n   string a[100]; n   cin >> n >> m; n   for (int i = 0; i < n; i++) { n     cin >> a[i]; n   } n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < a[i].size(); j++) { n       if (i % 2 == 0) { n         if (j % 2 == 0) { n           T =  B ; n         } else { n           T =  W ; n         } n       } else { n         if (j % 2 == 0) { n           T =  W ; n         } else { n           T =  B ; n         } n       } n       if (a[i][j] ==  . ) { n         a[i][j] = T; n       } else { n         a[i][j] =  - ; n       } n     } n   } n   for (int i = 0; i < n; i++) { n     cout << a[i] << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, k, l, c, d, p, nl, np, a, b, f, mina; n   cin >> n >> k >> l >> c >> d >> p >> nl >> np; n   a = (l * k) / nl; n   b = (c * d); n   f = p / np; n   if (a < b) n     mina = a; n   else n     mina = b; n   if (mina > f) mina = f; n   cout << (mina / n); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long a[10]; n int n; n int cnt[10], sump; n vector<long long> tree; n int rec(int cur, int trees, int treessum) { n   if (cur > n) return treessum + (trees > 1) + n - sump; n   int ans = (int)1e9; n   for (int i = 0; i < tree.size(); ++i) n     if (tree[i] % a[cur] == 0) { n       tree[i] /= a[cur]; n       tree.push_back(a[cur]); n       ans = min(ans, rec(cur + 1, trees + (!i), treessum + (!i) * cnt[cur])); n       tree[i] *= a[cur]; n       tree.pop_back(); n     } n   return ans; n } n bool cmp(long long a, long long b) { return a > b; } n int main() { n   scanf( %d , &n); n   for (int i = 1; i <= n; ++i) scanf( %I64d , a + i); n   sort(a + 1, a + 1 + n, cmp); n   for (int i = 1; i <= n; ++i) { n     long long x = a[i]; n     for (int j = 2; 1ll * j * j <= x; ++j) n       while (x % j == 0) { n         ++cnt[i]; n         x /= j; n       } n     if (x > 1) ++cnt[i]; n     if (cnt[i] == 1) ++sump; n   } n   tree.push_back(0); n   printf( %d n , rec(1, 0, 0)); n   return 0; n } n


#include <bits/stdc++.h> n #pragma comment(linker,  /STACK:10000000000 ) n #pragma GCC optimize( O3 ) n using namespace std; n const int MOD = 1000000007; n const int INF = 1000000007LL; n const long long INF2 = 1LL << 62LL; n const long double EPS = 1e-9; n const int SIZE = 200010; n mt19937 rng(time(0)); n uniform_int_distribution<int> uid(-1000000000, 1000000000); n int flag, u[SIZE], d[SIZE], a[SIZE], n; n int rec(int i) { n   if (i == n - 1) return 1; n   if (u[i] == flag) return d[i]; n   u[i] = flag; n   if (a[i] == 1) { n     return d[i] = !rec(i + 1); n   } n   return d[i] = 1; n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   int tt; n   cin >> tt; n   for (int rep_iter = 0; rep_iter < tt; ++rep_iter) { n     int n; n     cin >> n; n     string a, b; n     cin >> a >> b; n     int l = 0, r = n - 1, inv = 0; n     for (auto& i : a) i -=  0 ; n     for (auto& i : b) i -=  0 ; n     vector<int> ans; n     for (int len = n; len >= 1; --len) { n       if ((a[r] ^ inv) != b[len - 1]) { n         if ((a[l] ^ inv) == b[len - 1]) { n           ans.push_back(1); n           a[l] = !a[l]; n         } n         ans.push_back(len); n         inv = !inv; n         swap(r, l); n       } n       if (r >= l) n         --r; n       else n         ++r; n     } n     cout << ans.size() <<    ; n     for (auto& i : ans) cout << i <<    ; n     cout <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   int n, m; n   cin >> n >> m; n   int A[n][m], row[n], col[m]; n   char c; n   memset(row, 0, sizeof(row)); n   memset(col, 0, sizeof(col)); n   for (int i = 0; i < n; ++i) { n     for (int j = 0; j < m; ++j) { n       cin >> c; n       if (c ==  . ) n         A[i][j] = 0; n       else n         A[i][j] = 1; n     } n   } n   for (int i = 0; i < n; ++i) { n     if (row[i] == 0) { n       for (int j = 0; j < m; ++j) { n         if (A[i][j] == 1 && col[j]) { n           cout <<  No  << endl; n           return 0; n         } n       } n       vector<int> X; n       for (int j = i + 1; j < n; ++j) { n         int ans = 1; n         if (row[j]) continue; n         for (int k = 0; k < m; ++k) { n           if (A[i][k] != A[j][k]) { n             ans = 0; n             break; n           } n         } n         if (ans == 1) X.push_back(j); n       } n       for (int j = 0; j < X.size(); ++j) row[X[j]] = 1; n       for (int j = 0; j < m; ++j) n         if (A[i][j]) col[j] = 1; n     } n   } n   cout <<  Yes  << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 100007; n int n, m, q[N], a[N], mn = 100000007, k, l; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   cin >> m; n   for (int i = 0; i < m; i++) { n     cin >> q[i]; n     mn = min(mn, q[i]); n   } n   cin >> n; n   for (int i = 0; i < n; i++) { n     cin >> a[i]; n   } n   sort(a, a + n); n   for (int i = n - 1; i >= 0; i--) { n     k += a[i]; n     l++; n     if (l == mn) { n       l = 0; n       if (i == 1) n         i--; n       else if (i >= 2) n         i -= 2; n     } n   } n   cout << k; n   return 0; n } n


#include <bits/stdc++.h> n int D[61]; n int main() { n   int i, j, k, cnt; n   for (i = 0; i <= 60; i++) { n     cnt = 0; n     for (j = 1, k = i; k > 0; j++, k -= j) cnt++; n     D[i] = cnt; n   } n   int n, x, p = 0; n   scanf( %d , &n); n   for (i = 0; i < n; i++) { n     scanf( %d , &x); n     p ^= D[x]; n   } n   printf( %s n , p == 0 ?  YES  :  NO ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n bool win(long long a, long long b) { n   if (a > b) swap(a, b); n   if (a == 0) return false; n   if (!win(b % a, a)) return true; n   long long df = b - b % a; n   if (a % 2) n     return df % 2 == 0; n   else n     return df / a % (a + 1) % 2 == 0; n } n void solve() { n   long long a, b; n   cin >> a >> b; n   if (win(a, b)) n     cout <<  First  << endl; n   else n     cout <<  Second  << endl; n } n int main() { n   ios_base::sync_with_stdio(0); n   int T; n   cin >> T; n   for (int _ = 0; _ < (int)(T); _++) solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, a; n   cin >> n; n   int hasil[101] = {0}; n   for (int i = 1; i <= n; i++) { n     cin >> a; n     if (a / 2 != 1) { n       while (a % 2 != 0) { n         a++; n         hasil[i]++; n       } n     } else { n       a++; n       hasil[i]++; n       while (a % 2 != 0) { n         a++; n         hasil[i]++; n       } n     } n   } n   for (int i = 1; i <= n; i++) { n     cout << hasil[i] << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 5e5 + 5; n int n; n char s[N]; n struct SuffixAutomaton { n   struct node { n     int ch[26], par; n     int len; n   } t[N << 1]; n   int root, tail, cnt; n   inline int newnode(int len) { n     t[++cnt].len = len; n     return cnt; n   } n   inline void init() { root = tail = newnode(0); } n   inline int extend(int c) { n     int p = tail, np = newnode(t[tail].len + 1); n     for (; p && !t[p].ch[c]; p = t[p].par) t[p].ch[c] = np; n     if (!p) n       t[np].par = root; n     else if (t[t[p].ch[c]].len == t[p].len + 1) n       t[np].par = t[p].ch[c]; n     else { n       int q = t[p].ch[c], nq = newnode(t[p].len + 1); n       memcpy(t[nq].ch, t[q].ch, sizeof(t[nq].ch)); n       t[nq].par = t[q].par; n       t[np].par = t[q].par = nq; n       for (; p && t[p].ch[c] == q; p = t[p].par) t[p].ch[c] = nq; n     } n     return tail = np; n   } n } SAM; n int id[N]; n namespace tree { n int hed[N << 1], to[N << 1], nxt[N << 1], fa[N << 1], cnt, anc[N << 1][20]; n int fromlen[N << 1]; n inline void adde(int u, int v) { n   ++cnt; n   to[cnt] = v, nxt[cnt] = hed[u]; n   hed[u] = cnt; n } n vector<pair<int, int> > tags[N << 1]; n inline void dfs(int x, int pre) { n   fa[x] = pre; n   anc[x][0] = fa[x]; n   fromlen[x] = SAM.t[SAM.t[x].par].len + 1; n   if (x == 1) fromlen[x] = 0; n   for (int i = 1; i < 20; i++) anc[x][i] = anc[anc[x][i - 1]][i - 1]; n   for (int i = hed[x]; i; i = nxt[i]) { n     int v = to[i]; n     dfs(v, x); n   } n } n inline void pushtag(int x, int id, int f) { n   int pos = x; n   for (int i = 19; ~i; i--) { n     if (SAM.t[anc[pos][i]].len > f) pos = anc[pos][i]; n   } n   while (tags[pos].size() == 0 || tags[pos].back().first <= f) { n     if (tags[pos].size() != 0 && tags[pos].back().first == SAM.t[pos].len) n       break; n     tags[pos].push_back(pair<int, int>(min(SAM.t[pos].len, f), id)); n     pos = fa[pos]; n   } n } n inline int find(int x, int id) { n   int pos = x; n   for (int i = 19; ~i; i--) { n     int nxtpos = anc[pos][i]; n     if (!nxtpos) continue; n     if (tags[nxtpos].size() == 0) n       pos = nxtpos; n     else { n       if (fromlen[nxtpos] + tags[nxtpos][0].second > id) { n         pos = nxtpos; n       } n     } n   } n   pos = fa[pos]; n   if (!pos) return 0; n   int l = 0, r = (int)tags[pos].size() - 1; n   while (l < r) { n     int mid = (l + r + 1) >> 1; n     int stlen = (mid == 0 ? fromlen[pos] : tags[pos][mid - 1].first + 1); n     if (stlen + tags[pos][mid].second <= id) n       l = mid; n     else n       r = mid - 1; n   } n   int finalend = min(tags[pos][l].first, id - tags[pos][l].second); n   return finalend; n } n }  // namespace tree n int f[N]; n int main() { n   SAM.init(); n   cin >> n; n   scanf( %s , s + 1); n   reverse(s + 1, s + n + 1); n   for (int i = 1; i <= n; ++i) id[i] = SAM.extend(s[i] -  a ); n   for (int i = 1; i <= SAM.cnt; i++) n     if (SAM.t[i].par) tree::adde(SAM.t[i].par, i); n   tree::dfs(1, 0); n   int ans = 0; n   for (int i = 1; i <= n; i++) { n     f[i] = max(f[i], tree::find(id[i], i - 1) + 1); n     if (i < n) f[i + 1] = max(f[i + 1], tree::find(id[i], i) + 1); n     ans = max(ans, f[i]); n     tree::pushtag(id[i], i, f[i]); n   } n   cout << ans << endl; n } n


#include <bits/stdc++.h> n const long long Inf = 1e18; n const long double eps = 1e-7; n long long LINF = (long long)2e18; n using namespace std; n long long mod = 1e9 + 7; n long long mod5 = 1e9 + 9; n long long mod3 = 998244353; n long long mod4 = 1000003; n long long mod2 = 1e9 + 123; n const int MAXN = 4000001; n const int INF = 1000000000; n int n, r; n vector<pair<int, int> > xy; n struct Tree { n   vector<long long> lazy; n   vector<long long> t; n   Tree(int n) { n     t.resize(4 * n); n     lazy.resize(4 * n, 0); n   } n   void push(int v, int tl, int tr) { n     t[v] += lazy[v]; n     if (tl != tr) { n       lazy[2 * v] += lazy[v]; n       lazy[2 * v + 1] += lazy[v]; n     } n     lazy[v] = 0; n   } n   void update(int v, int tl, int tr, int l, int r, int x) { n     push(v, tl, tr); n     if (l == tl && r == tr) { n       lazy[v] += x; n       push(v, tl, tr); n       return; n     } n     int tm = (tl + tr) / 2; n     if (r <= tm) { n       update(2 * v, tl, tm, l, r, x); n       push(2 * v + 1, tm + 1, tr); n     } else if (l > tm) { n       update(2 * v + 1, tm + 1, tr, l, r, x); n       push(2 * v, tl, tm); n     } else { n       update(2 * v, tl, tm, l, tm, x); n       update(2 * v + 1, tm + 1, tr, tm + 1, r, x); n     } n     t[v] = max(t[2 * v], t[2 * v + 1]); n   } n   long long mx(int v, int tl, int tr, int l, int r) { n     push(v, tl, tr); n     if (l == tl && r == tr) { n       return t[v]; n     } n     int tm = (tl + tr) / 2; n     if (r <= tm) n       return mx(2 * v, tl, tm, l, r); n     else if (l > tm) n       return mx(2 * v + 1, tm + 1, tr, l, r); n     else { n       return max(mx(2 * v, tl, tm, l, tm), n                  mx(2 * v + 1, tm + 1, tr, tm + 1, r)); n     } n   } n }; n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   cout.setf(ios::fixed); n   cout.precision(20); n   cin >> n >> r; n   xy.resize(n); n   int mn = 1e9; n   for (int(i) = 0; (i) < n; (i)++) { n     int x, y; n     cin >> x >> y; n     xy[i].first = x + y; n     xy[i].second = x - y; n     mn = min(mn, xy[i].second); n   } n   for (int(i) = 0; (i) < n; (i)++) { n     xy[i].second -= mn; n   } n   sort((xy).begin(), (xy).end()); n   int itl = 0, itr = 0; n   r *= 2; n   long long ans = 0; n   Tree t(MAXN); n   while (itl != n) { n     if (itr < n && xy[itr].first - xy[itl].first <= r) { n       t.update(1, 0, MAXN - 1, max(0, xy[itr].second - r), xy[itr].second, 1); n       ans = max(ans, t.mx(1, 0, MAXN - 1, 0, MAXN - 1)); n       itr++; n     } else { n       t.update(1, 0, MAXN - 1, max(0, xy[itl].second - r), xy[itl].second, -1); n       itl++; n     } n   } n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long read() { n   long long ans = 0; n   char last =    , ch = getchar(); n   while (ch <  0  || ch >  9 ) last = ch, ch = getchar(); n   while (ch >=  0  && ch <=  9 ) ans = ans * 10 + ch -  0 , ch = getchar(); n   if (last ==  - ) ans = -ans; n   return ans; n } n const int N = 2e5 + 5; n int n; n long long k, sum, tmp, suf[N], a[N], c, ans; n long long accCeil(long long a, long long b) { n   long long res = a / b; n   if (res * b < a) res++; n   return res; n } n int main() { n   int T = read(); n   while (T--) { n     n = read(), k = read(); n     sum = 0; n     for (int i = 1; i <= n; ++i) { n       a[i] = read(); n       sum += a[i]; n     } n     if (sum <= k) { n       printf( 0 n ); n       continue; n     } n     if (n == 1) { n       printf( %lld n , sum - k); n       continue; n     } n     sort(a + 1, a + n + 1); n     suf[n + 1] = 0; n     ans = 1e18; n     for (int i = n; i > 1; --i) { n       suf[i] = suf[i + 1] + a[i]; n       tmp = sum - k - suf[i] + (n - i + 1) * a[1]; n       c = max(accCeil(tmp, n - i + 2), 0ll); n       ans = min(ans, n - i + 1 + c); n     } n     printf( %lld n , min(ans, sum - k)); n   } n   return 0; n } n


#include <bits/stdc++.h> n int main() { n   int x, y, n; n   int k, i, t; n   scanf( %d , &t); n   for (i = 0; i < t; i++) { n     scanf( %d %d %d , &x, &y, &n); n     if (n - n % x + y <= n) { n       k = n - n % x + y; n     } else { n       k = n - n % x - (x - y); n     } n     printf( %d n , k); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n bool check(string s) { n   for (int i = 0; i < s.size() - 1; i++) { n     if (s[i] == s[i + 1]) return false; n   } n   return true; n } n void solve() { n   string s; n   cin >> s; n   for (int i = 0; i < s.size(); i++) { n     if (s[i] ==  ?  && i != 0 && i != s.size() - 1) { n       if (s[i - 1] ==  a  && s[i + 1] ==  c  || n           s[i - 1] ==  c  && s[i + 1] ==  a ) n         s[i] =  b ; n       else if (s[i - 1] ==  a  && s[i + 1] ==  b  || n                s[i - 1] ==  b  && s[i + 1] ==  a ) n         s[i] =  c ; n       else if (s[i - 1] ==  b  && s[i + 1] ==  c  || n                s[i - 1] ==  c  && s[i + 1] ==  b ) n         s[i] =  a ; n       else if (s[i - 1] ==  a ) n         s[i] =  b ; n       else if (s[i - 1] ==  b ) n         s[i] =  c ; n       else if (s[i - 1] ==  c ) n         s[i] =  a ; n     } else if (s[i] ==  ?  && i == 0) { n       if (s[i + 1] ==  a ) n         s[i] =  b ; n       else if (s[i + 1] ==  b ) n         s[i] =  c ; n       else if (s[i + 1] ==  c ) n         s[i] =  a ; n       else n         s[i] =  a ; n     } else if (s[i] ==  ?  && i == s.size() - 1) { n       if (s[i - 1] ==  a ) n         s[i] =  b ; n       else if (s[i - 1] ==  b ) n         s[i] =  c ; n       else if (s[i - 1] ==  c ) n         s[i] =  a ; n       else n         s[i] =  a ; n     } n   } n   if (check(s)) { n     cout << s << endl; n   } else n     cout << -1 << endl; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   int t; n   cin >> t; n   while (t--) { n     solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m; n int a[555][555]; n char ans[555]; n void die() { n   for (int i = 0; i < n; ++i) { n     for (int j = 0; j < n; ++j) { n       if (i != j) { n         if (abs(ans[i] - ans[j]) <= 1 && !a[i][j]) { n           cout <<  No n ; n           exit(0); n         } n         if (abs(ans[i] - ans[j]) > 1 && a[i][j]) { n           cout <<  No n ; n           exit(0); n         } n       } n     } n   } n   cout <<  Yes n ; n   ans[n] = 0; n   cout << ans << endl; n   exit(0); n } n int main() { n   scanf( %d%d , &n, &m); n   for (int i = 0; i < m; ++i) { n     int first, second; n     scanf( %d%d , &first, &second); n     --first; n     --second; n     a[first][second] = a[second][first] = 1; n   } n   int A = -1, B = -1, C = -1; n   for (int i = 0; i < n; ++i) { n     for (int j = 0; j < n; ++j) { n       for (int k = 0; k < n; ++k) { n         if (i == j || j == k) { n           continue; n         } n         if (a[i][j] && a[i][k]) { n           if (!a[j][k]) { n             A = j; n             B = i; n             C = k; n           } n         } n       } n     } n   } n   if (A == -1) { n     ans[0] =  a ; n     for (int i = 1; i < n; ++i) { n       if (a[0][i]) { n         ans[i] =  a ; n       } else { n         ans[i] =  c ; n       } n     } n     die(); n   } n   ans[B] =  b ; n   ans[A] =  a ; n   ans[C] =  c ; n   for (int i = 0; i < n; ++i) { n     if (i == A || i == B || i == C) continue; n     if (a[A][i] && a[B][i] && a[C][i]) n       ans[i] =  b ; n     else if (a[A][i] && a[B][i]) n       ans[i] =  a ; n     else n       ans[i] =  c ; n   } n   die(); n } n


#include <bits/stdc++.h> n #pragma GCC optimize( Ofast ) n #pragma GCC target( avx,avx2,fma ) n using namespace std; n typedef int ll; n const ll MAX = 1e3; n const ll mod = 1e4; n const ll inf = 1e9; n struct FlowEdge { n   int v, u; n   int cap, flow = 0; n   FlowEdge(int v, int u, int cap) : v(v), u(u), cap(cap) {} n }; n struct Dinic { n   const int flow_inf = 1e9; n   vector<FlowEdge> edges; n   vector<vector<int>> adj; n   int n, m = 0; n   int s, t; n   vector<int> level, ptr; n   queue<int> q; n   Dinic(int n, int s, int t) : n(n), s(s), t(t) { n     adj.resize(n); n     level.resize(n); n     ptr.resize(n); n   } n   void add_edge(int v, int u, int cap) { n     edges.emplace_back(v, u, cap); n     edges.emplace_back(u, v, 0); n     adj[v].push_back(m); n     adj[u].push_back(m + 1); n     m += 2; n   } n   bool bfs() { n     while (!q.empty()) { n       int v = q.front(); n       q.pop(); n       for (int id : adj[v]) { n         if (edges[id].cap - edges[id].flow < 1) continue; n         if (level[edges[id].u] != -1) continue; n         level[edges[id].u] = level[v] + 1; n         q.push(edges[id].u); n       } n     } n     return level[t] != -1; n   } n   int dfs(int v, int pushed) { n     if (pushed == 0) return 0; n     if (v == t) return pushed; n     for (int& cid = ptr[v]; cid < (int)adj[v].size(); cid++) { n       int id = adj[v][cid]; n       int u = edges[id].u; n       if (level[v] + 1 != level[u] || edges[id].cap - edges[id].flow < 1) n         continue; n       int tr = dfs(u, min(pushed, edges[id].cap - edges[id].flow)); n       if (tr == 0) continue; n       edges[id].flow += tr; n       edges[id ^ 1].flow -= tr; n       return tr; n     } n     return 0; n   } n   int flow() { n     int f = 0; n     while (true) { n       fill(level.begin(), level.end(), -1); n       level[s] = 0; n       q.push(s); n       if (!bfs()) break; n       fill(ptr.begin(), ptr.end(), 0); n       while (int pushed = dfs(s, flow_inf)) { n         f += pushed; n       } n     } n     return f; n   } n }; n int c[MAX], ans[MAX]; n vector<ll> adj[MAX]; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   int tc, n, m, k, s, t, u, v; n   cin >> tc; n   while (tc--) { n     cin >> n >> m >> k; n     s = 0, t = n + m + 1; n     Dinic dinic(n + m + 2, s, t); n     for (ll i = 1; i < (ll)m + 1; i++) { n       cin >> u >> v; n       c[u]++, c[v]++; n       dinic.add_edge(n + i, t, 1); n       dinic.add_edge(u, n + i, 1); n       dinic.add_edge(v, n + i, 1); n     } n     int to = 0; n     for (ll i = 1; i < (ll)n + 1; i++) n       if (c[i] > k) dinic.add_edge(s, i, 2 * (c[i] - k)), to += 2 * (c[i] - k); n     if (dinic.flow() == to) { n       for (FlowEdge it : dinic.edges) { n         if (1 <= it.v && it.v <= n && n + 1 <= it.u && it.u <= n + m && n             it.flow == 1) { n           adj[it.v].push_back(it.u - n); n         } n       } n       ll col = 1; n       for (ll i = 1; i < (ll)n + 1; i++) { n         for (ll j = 0; j < (ll)adj[i].size(); j += 2) n           ans[adj[i][j]] = ans[adj[i][j + 1]] = col++; n       } n       for (ll i = 1; i < (ll)m + 1; i++) n         if (ans[i] == 0) ans[i] = col++; n     } n     for (ll i = 1; i < (ll)m + 1; i++) cout << ans[i] <<    , ans[i] = 0; n     cout << endl; n     for (ll i = 1; i < (ll)n + 1; i++) adj[i].clear(), c[i] = 0; n   } n   return 0; n } n


#pragma GCC optimize( Ofast ) n #include<bits/stdc++.h> n #define int long long n using namespace std; n void fileio(const string &s) n { n  freopen((s+ .in ).c_str(), r ,stdin); n  freopen((s+ .out ).c_str(), w ,stdout); n } n const int INF=4e18; n inline int read() n { n  int x=0; n  bool flag=1; n  char c=getchar(); n  while(c< 0 ||c> 9 ) n  { n   if(c== - ) n    flag=0; n   c=getchar(); n  } n  while(c>= 0 &&c<= 9 ) n  { n   x=(x<<1)+(x<<3)+c- 0 ; n   c=getchar(); n  } n  return (flag?x:~(x-1)); n } n int t,tx,ty,ax,ay,bx,by;  n void solve() n { n  ax=read(); n  ay=read(); n  bx=read(); n  by=read(); n  int ans=abs(ax-bx)+abs(ay-by); n  tx=read(); n  ty=read(); n  if(tx==ax&&tx==bx) n  { n   if(ay<ty&&ty<by) n    ans+=2; n   if(ay>ty&&ty>by) n    ans+=2; n  } n  if(ty==ay&&ty==by) n  { n   if(ax<tx&&tx<bx) n    ans+=2; n   if(ax>tx&&tx>bx) n    ans+=2; n  } n  cout<<ans<<endl; n } n signed main() n { n  t=read(); n  while(t--) n   solve(); n  return 0; n } n  n


#include <bits/stdc++.h> n #pragma GCC optimize( Ofast ) n using namespace std; n long long n, a[100007], x, ans, t, res, tmp; n vector<long long> v, cur; n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   cin >> n; n   for (long int i = 0; i < n; i++) { n     cin >> x; n     if (x) v.push_back(i); n   } n   if (v.size() < 2) { n     cout << -1; n     return 0; n   } n   if (v.size() < 4) { n     if (v.size() == 2) n       cout << v[1] - v[0]; n     else n       cout << v[2] - v[0]; n     return 0; n   } n   ans = 1e12; n   for (long int i = 2; i * i <= v.size(); i++) { n     if (v.size() % i != 0) continue; n     tmp = i; n     for (long int k = 0; k < 2; k++, i = v.size() / i) { n       t = 0; n       res = 0; n       for (auto j : v) { n         cur.push_back(j); n         t++; n         if (t % i == 0) { n           t = 0; n           for (auto h : cur) res += abs(cur[i / 2] - h); n           cur.clear(); n         } n       } n       ans = min(ans, res); n     } n     i = tmp; n   } n   res = 0; n   for (auto i : v) { n     res += abs(i - v[v.size() / 2]); n   } n   cout << min(res, ans); n } n


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


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   cin >> n; n   int key; n   vector<int> v; n   for (int i = 0; i < n; ++i) { n     cin >> key; n     v.push_back(key); n   } n   int max = v[0]; n   int min = v[0]; n   int amz = 0; n   for (int i = 1; i < n; ++i) { n     if (v[i] > max) { n       amz++; n       max = v[i]; n     } n     if (v[i] < min) { n       amz++; n       min = v[i]; n     } n   } n   cout << amz; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   int t; n   cin >> t; n   for (int i = 0; i < t; i++) { n     int n; n     cin >> n; n     vector<int> p(n), result; n     for (int& j : p) { n       cin >> j; n     } n     vector<int> maximums; n     vector<bool> used(n, false); n     result.push_back(p[0]); n     used[p[0] - 1] = true; n     int idx = 0; n     for (idx; idx < p[0] - 1; idx++) { n       maximums.push_back(idx + 1); n     } n     idx++; n     for (int j = 1; j < n; j++) { n       if (p[j] > p[j - 1]) { n         result.push_back(p[j]); n         used[p[j] - 1] = true; n         for (idx; idx < p[j] - 1; idx++) { n           if (!used[idx]) maximums.push_back(idx + 1); n         } n         idx++; n       } else { n         if (maximums.size() > 0) { n           used[maximums[maximums.size() - 1] - 1] = true; n           result.push_back(maximums[maximums.size() - 1]); n           maximums.pop_back(); n         } n       } n     } n     if (result.size() < n) n       cout << -1 << endl; n     else { n       for (int& i : result) { n         cout << i <<    ; n       } n       cout << endl; n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n int mat[105][105]; n int nodes[105]; n int n, m; n int cycles = 0; n void cycle_det(int cur, int parent); n int main() { n   cin >> n >> m; n   memset(mat, 0, sizeof mat); n   memset(nodes, 0, sizeof nodes); n   for (int i = 0; i < m; i++) { n     int _1, _2; n     cin >> _1 >> _2; n     mat[_1 - 1][_2 - 1] = 1; n     mat[_2 - 1][_1 - 1] = 1; n   } n   cycle_det(0, -1); n   int count = 0; n   for (int i = 0; i < n; i++) n     if (nodes[i] == 1) count++; n   if (cycles != 2 || count != n) n     cout <<  NO n ; n   else n     cout <<  FHTAGN! n ; n } n void cycle_det(int cur, int parent) { n   if (cycles > 2) return; n   nodes[cur] = 1; n   for (int i = 0; i < n; i++) { n     int tmp = 0; n     if (mat[cur][i] == 1 && !nodes[i]) { n       nodes[i] = 1; n       cycle_det(i, cur); n     } else if (mat[cur][i] == 1 && i != parent && nodes[i]) { n       cycles++; n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n const long long INF = 1e9 + 7; n const int MAXN = 1e6 + 7; n const double EPS = 1e-8; n long long power(long long a, long long k) { n   if (!k) { n     return 1; n   } n   long long b = power(a, k / 2); n   b = b * b % INF; n   if (k % 2) { n     return a * b % INF; n   } else { n     return b; n   } n } n vector<vector<long long> > gr; n vector<long long> p, r; n long long all_sz = 0; n long long get(long long v) { return (p[v] == v ? v : p[v] = get(p[v])); } n bool uni(long long v, long long u) { n   v = p[v]; n   u = p[u]; n   if (v == u) { n     return false; n   } n   if (r[v] == r[u]) { n     r[u]++; n   } n   if (r[v] > r[u]) { n     swap(v, u); n   } n   p[v] = u; n   all_sz--; n   return true; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   long long n, m, k; n   cin >> n >> m >> k; n   p.resize(n, 0); n   iota(p.begin(), p.end(), 0); n   r.resize(n, 0); n   vector<long long> c(n); n   for (long long i = 0; i < n; i++) { n     cin >> c[i]; n   } n   long long all = power(2, n + k); n   gr.resize(n); n   map<long long, vector<pair<long long, long long> > > d; n   for (long long i = 0; i < m; i++) { n     long long v, u; n     cin >> v >> u; n     v--, u--; n     d[c[v] ^ c[u]].push_back({v, u}); n   } n   long long bad = 0; n   for (auto pt : d) { n     all_sz = n; n     set<long long> used; n     for (auto t : pt.second) { n       long long v = t.first, u = t.second; n       used.insert(v); n       used.insert(u); n       if (get(v) == get(u)) continue; n       bad = (bad + power(2, all_sz - 1)) % INF; n       uni(v, u); n     } n     for (long long v : used) { n       p[v] = v; n       r[v] = 0; n     } n   } n   cout << (all - bad + INF) % INF; n } n


#include <bits/stdc++.h> n using namespace std; n long double pi = 3.141592653589793238; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   ; n   long long t; n   cin >> t; n   while (t--) { n     string x; n     cin >> x; n     long long n = x.length(); n     if (n == 0) { n       cout << 0 << endl; n     } n     if (n == 1) { n       cout << 5 << endl; n       continue; n     } n     set<tuple<long long, long long, long long, long long>> r1; n     long long curx = 0; n     long long cury = 0; n     long long cnt = 5; n     if (x[0] ==  N ) { n       cury++; n       r1.insert(make_tuple(curx, cury - 1, curx, cury)); n       r1.insert(make_tuple(curx, cury, curx, cury - 1)); n     } n     if (x[0] ==  S ) { n       cury--; n       r1.insert(make_tuple(curx, cury + 1, curx, cury)); n       r1.insert(make_tuple(curx, cury, curx, cury + 1)); n     } n     if (x[0] ==  E ) { n       curx++; n       r1.insert(make_tuple(curx - 1, cury, curx, cury)); n       r1.insert(make_tuple(curx, cury, curx - 1, cury)); n     } n     if (x[0] ==  W ) { n       curx--; n       r1.insert(make_tuple(curx + 1, cury, curx, cury)); n       r1.insert(make_tuple(curx, cury, curx + 1, cury)); n     } n     for (long long i = 1; i < n; i++) { n       if (x[i] ==  N ) { n         cury++; n         if (r1.find(make_tuple(curx, cury - 1, curx, cury)) == r1.end()) { n           r1.insert(make_tuple(curx, cury - 1, curx, cury)); n           cnt += 5; n           r1.insert(make_tuple(curx, cury, curx, cury - 1)); n         } else n           cnt++; n       } n       if (x[i] ==  S ) { n         cury--; n         if (r1.find(make_tuple(curx, cury + 1, curx, cury)) == r1.end()) { n           r1.insert(make_tuple(curx, cury + 1, curx, cury)); n           cnt += 5; n           r1.insert(make_tuple(curx, cury, curx, cury + 1)); n         } else n           cnt++; n       } n       if (x[i] ==  E ) { n         curx++; n         if (r1.find(make_tuple(curx - 1, cury, curx, cury)) == r1.end()) { n           r1.insert(make_tuple(curx - 1, cury, curx, cury)); n           cnt += 5; n           r1.insert(make_tuple(curx, cury, curx - 1, cury)); n         } else n           cnt++; n       } n       if (x[i] ==  W ) { n         curx--; n         if (r1.find(make_tuple(curx + 1, cury, curx, cury)) == r1.end()) { n           r1.insert(make_tuple(curx + 1, cury, curx, cury)); n           cnt += 5; n           r1.insert(make_tuple(curx, cury, curx + 1, cury)); n         } else n           cnt++; n       } n     } n     cout << cnt << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 2e3 + 10; n long long n, k; n char s[MAXN]; n int num[33]; n int main() { n   while (cin >> s) { n     cin >> k; n     int len = strlen(s); n     if (len < k) { n       cout <<  impossible  << endl; n     } else { n       memset(num, 0, sizeof num); n       for (int i = 0; i < len; ++i) { n         num[s[i] -  a ]++; n       } n       long long ans = 0; n       for (int i = 0; i < 26; ++i) { n         if (num[i]) ++ans; n       } n       if (ans <= k) n         cout << k - ans << endl; n       else n         cout << 0 << endl; n     } n   } n   return 0; n } n


#include<bits/stdc++.h> n  n #define mamun()         ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); n #define endl              n  n #define tab               t  n #define ll              long long n #define lli             long long int n #define pb              push_back n #define fin(file)       freopen(file .txt ,  r , stdin) n #define fout(file)      freopen(file .txt ,  w , stdout) n #define FOR(i, a, b)    for (int i=a; i<=b; i++) n #define loop(i, n)      for (int i=0; i<n; i++) n #define srt(v)          sort(v.begin(), v.end()) n #define rsrt(v)         sort(v.rbegin(), v.rend()) n #define Fill(ar, val)   memset(ar, val, sizeof(ar)) n #define testcase()      ll t; cin>>t; while(t--) n  n using namespace std; n  n int main() n { n     mamun(); n     //fin( input ); n  n     testcase() n     { n         ll n, Ecnt=0, Ocnt=0; n         cin>> n; n         vector<ll> v(2*n); n  n         loop(i, 2*n) cin>> v[i]; n         // srt(v); n  n         loop(i, 2*n) n         { n             if(v[i] % 2 == 0) n             { n                 Ecnt++; n             } n             else n             { n                 Ocnt++; n             } n         } n         if(Ecnt == Ocnt) n             cout<<  Yes  << endl; n         else n             cout<<  No  << endl; n     } n  n     return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n; n string ans, word; n vector<int> get_pi(string s) { n   int j = 0, k = -1; n   vector<int> q; n   q.clear(); n   q.push_back(-1); n   while (j < s.size()) { n     if (k == -1 || s[j] == s[k]) { n       q.push_back(++k); n       ++j; n     } else { n       k = q[k]; n     } n   } n   return q; n } n int main() { n   ios::sync_with_stdio(false); n   ans =   ; n   cin >> n; n   for (int i = 1; i <= n; ++i) { n     cin >> word; n     int l = min(word.size(), ans.size()); n     string s = word +  # ; n     s.insert(s.end(), ans.end() - l, ans.end()); n     vector<int> pi = get_pi(s); n     int move = pi[pi.size() - 1]; n     ans.insert(ans.end(), word.begin() + move, word.end()); n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int dcount(long long i) { n   int c = 0; n   while (i > 0) { n     i = i / 10; n     c++; n   } n   return c; n } n int main() { n   long long n, cnt = 0; n   cin >> n; n   int num = dcount(n); n   for (long long int i = 0; i < num; i++) { n     cnt += n > (pow(10, i + 1) - 1) ? (pow(10, i + 1) - pow(10, i)) * (i + 1) n                                     : (n - pow(10, i) + 1) * (i + 1); n   } n   cout << cnt; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   int n; n   cin >> n; n   int a = 1, b = 2; n   while (n--) { n     int c; n     cin >> c; n     if (c == a) { n       b = 6 - (a + b); n     } else if (c == b) { n       a = 6 - (a + b); n     } else { n       cout <<  NO n ; n       return 0; n     } n   } n   cout <<  YES n ; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 3e5 + 5; n long long a[N]; n struct tr { n   int l, r, maxi; n } t[N * 4]; n int sign(long long x) { n   if (x > 0) return 1; n   if (x < 0) return -1; n   return 0; n } n inline void cal(int cur, int l, int r) { n   int m = (l + r) / 2; n   int dcur = cur + cur, u = dcur, v = u + 1; n   t[cur].maxi = max(t[u].maxi, t[v].maxi); n   t[cur].l = t[u].l; n   t[cur].r = t[v].r; n   if ((!a[m] || !a[m + 1] || sign(a[m]) < sign(a[m + 1]))) { n   } else { n     t[cur].maxi = max(t[cur].maxi, t[u].r + t[v].l); n     if (t[u].maxi == m - l + 1) { n       t[cur].l = t[u].l + t[v].l; n     } n     if (t[v].maxi == r - m) { n       t[cur].r = t[v].r + t[u].r; n     } n   } n } n void build(int pos, int l, int r) { n   if (l == r) { n     int x = !!a[l]; n     t[pos].l = t[pos].r = t[pos].maxi = x; n     return; n   } n   int mid = (l + r) >> 1; n   build(pos * 2, l, mid); n   build(pos * 2 + 1, mid + 1, r); n   cal(pos, l, r); n } n void update(int pos, int l, int r, int k, int d) { n   int mid = (l + r) >> 1; n   if (l == r) { n     a[l] += d; n     int x = !!a[l]; n     t[pos].l = t[pos].r = t[pos].maxi = x; n     return; n   } else { n     int mid = (l + r) >> 1; n     if (k <= mid) n       update(pos * 2, l, mid, k, d); n     else n       update(pos * 2 + 1, mid + 1, r, k, d); n     cal(pos, l, r); n   } n } n int main() { n   int u, v, n, i, m, l, r, d; n   cin >> n; n   cin >> u; n   for (i = 1; i < n; ++i) scanf( %d , &v), a[i - 1] = v - u, u = v; n   if (n > 1) build(1, 0, n - 2); n   cin >> m; n   while (m--) { n     scanf( %d%d%d , &l, &r, &u); n     if (n == 1) { n       printf( 1 n ); n     } else { n       if (l > 1) update(1, 0, n - 2, l - 2, u); n       if (r < n) update(1, 0, n - 2, r - 1, -u); n       printf( %d n , t[1].maxi + 1); n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n vector<long long> v; n map<long long, int> mp, mpp; n long long ans[200005]; n int main() { n   int n; n   cin >> n; n   for (int i = 0; i < n; i++) { n     long long l, r; n     cin >> l >> r; n     if (!mpp[l]) { n       v.push_back(l); n       mpp[l] = 1; n     } n     if (!mpp[r + 1]) { n       v.push_back(r + 1); n       mpp[r + 1] = 1; n     } n     mp[l]++; n     mp[r + 1]--; n   } n   sort(v.begin(), v.end()); n   long long prev = 0; n   int active = 0; n   for (int i = 0; i < v.size(); i++) { n     ans[active] += v[i] - prev; n     active += mp[v[i]]; n     prev = v[i]; n   } n   for (int i = 1; i <= n; i++) cout << ans[i] <<    ; n   cout << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n inline bool EQ(double a, double b) { return fabs(a - b) < 1e-9; } n inline void normal(long long int &a) { n   a %= 1000000007; n   (a < 0) && (a += 1000000007); n } n inline long long int modMul(long long int a, long long int b) { n   a %= 1000000007, b %= 1000000007; n   normal(a), normal(b); n   return (a * b) % 1000000007; n } n inline long long int modAdd(long long int a, long long int b) { n   a %= 1000000007, b %= 1000000007; n   normal(a), normal(b); n   return (a + b) % 1000000007; n } n inline long long int modSub(long long int a, long long int b) { n   a %= 1000000007, b %= 1000000007; n   normal(a), normal(b); n   a -= b; n   normal(a); n   return a; n } n inline long long int modPow(long long int b, long long int p) { n   long long int r = 1; n   while (p) { n     if (p & 1) r = modMul(r, b); n     b = modMul(b, b); n     p >>= 1; n   } n   return r; n } n inline long long int modInverse(long long int a) { n   return modPow(a, 1000000007 - 2); n } n inline long long int modDiv(long long int a, long long int b) { n   return modMul(a, modInverse(b)); n } n inline bool isInside(pair<int, int> p, long long int n, long long int m) { n   return (p.first >= 0 && p.first < n && p.second >= 0 && p.second < m); n } n inline bool isInside(pair<int, int> p, long long int n) { n   return (p.first >= 0 && p.first < n && p.second >= 0 && p.second < n); n } n inline bool isSquare(long long int x) { n   long long int s = sqrt(x); n   return (s * s == x); n } n inline bool isFib(long long int x) { n   return isSquare(5 * x * x + 4) || isSquare(5 * x * x - 4); n } n inline bool isPowerOfTwo(long long int x) { n   return ((1LL << (long long int)log2(x)) == x); n } n template <typename first, typename second> n ostream &operator<<(ostream &os, const pair<first, second> &p) { n   return os <<  (  << p.first <<  ,   << p.second <<  ) ; n } n template <typename T> n ostream &operator<<(ostream &os, const vector<T> &v) { n   os <<  { ; n   for (auto it = v.begin(); it != v.end(); ++it) { n     if (it != v.begin()) os <<  ,  ; n     os << *it; n   } n   return os <<  } ; n } n template <typename T> n ostream &operator<<(ostream &os, const set<T> &v) { n   os <<  [ ; n   for (auto it = v.begin(); it != v.end(); ++it) { n     if (it != v.begin()) os <<  ,  ; n     os << *it; n   } n   return os <<  ] ; n } n template <typename first, typename second> n ostream &operator<<(ostream &os, const map<first, second> &v) { n   os <<  [ ; n   for (auto it = v.begin(); it != v.end(); ++it) { n     if (it != v.begin()) os <<  ,  ; n     os << it->first <<   =   << it->second; n   } n   return os <<  ] ; n } n clock_t tStart = clock(); n void faltu() { cerr << endl; } n template <typename T> n void faltu(T a[], int n) { n   for (int i = 0; i < n; ++i) cerr << a[i] <<    ; n   cerr << endl; n } n template <typename T, typename... hello> n void faltu(T arg, const hello &...rest) { n   cerr << arg <<    ; n   faltu(rest...); n } n struct func { n   bool operator()(pair<int, int> const &a, pair<int, int> const &b) { n     if (a.first == b.first) return (a.second < b.second); n     return (a.first < b.first); n   } n }; n const long long int INF = 0x3f3f3f3f3f3f3f3f; n const long double EPS = 1e-9; n const int inf = 0x3f3f3f3f; n const int mx = (int)1e5 + 9; n long long int n, m, a, b, t, i, j, d, cs = 0, counT = 0, k, ans = 0, l = 0, n                                       sum1 = 0, sum = 0, Max, Min, num; n vector<long long int> vc; n map<long long int, long long int> mp; n int main() { n   ios_base::sync_with_stdio(false); n   ; n   t = 1; n   cin >> t; n   while (t--) { n     cin >> n; n     for (i = n; i >= 1; i--) cout << i <<    ; n     cout <<   n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 100010; n int dp[550][550], A[550], B[550], path[550][550], n, m; n bool print(int x) { n   if (!x) return false; n   if (print(path[n][x])) n     printf(  %d , B[x]); n   else n     printf( %d , B[x]); n   return true; n } n int main() { n   int i, j, tp, ans, pos; n   while (~scanf( %d , &n)) { n     for (i = 1; i <= n; i++) scanf( %d , &A[i]); n     scanf( %d , &m); n     for (i = 1; i <= m; i++) scanf( %d , &B[i]); n     memset(dp, 0, sizeof dp); n     for (i = 1; i <= n; i++) { n       tp = pos = 0; n       for (j = 1; j <= m; j++) { n         dp[i][j] = dp[i - 1][j]; n         path[i][j] = path[i - 1][j]; n         if (A[i] == B[j] && tp + 1 > dp[i][j]) n           dp[i][j] = tp + 1, path[i][j] = pos; n         if (B[j] < A[i] && dp[i - 1][j] > tp) tp = dp[i - 1][j], pos = j; n       } n     } n     ans = 1; n     for (i = 1; i <= m; i++) n       if (dp[n][i] > dp[n][ans]) ans = i; n     printf( %d n , dp[n][ans]); n     if (dp[n][ans]) print(ans); n     printf(  n ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct debugger { n   template <typename T> n   debugger& operator,(const T& v) { n     cerr << v <<    ; n     return *this; n   } n } debug; n template <typename T1, typename T2> n inline ostream& operator<<(ostream& os, const pair<T1, T2>& p) { n   return os <<  (  << p.first <<  ,   << p.second <<  ) ; n } n template <typename T> n inline ostream& operator<<(ostream& os, const vector<T>& v) { n   bool first = true; n   os <<  [ ; n   for (typename vector<T>::const_iterator ii = v.begin(); ii != v.end(); ++ii) { n     if (!first) os <<  ,  ; n     os << *ii; n     first = false; n   } n   return os <<  ] ; n } n const int NN = (1 << 18); n int seg[NN], dp[NN]; n int od[NN], ans[NN]; n bool vowel(char ch) { n   ch = tolower(ch); n   if (ch ==  a  or ch ==  e  or ch ==  i  or ch ==  o  or ch ==  u ) n     return true; n   return false; n } n bool cmp(int x, int y) { n   if (dp[x] == dp[y]) return (x < y); n   return (dp[x] < dp[y]); n } n void update(int i, int v) { n   for (i += 5; i < NN; i += i & -i) seg[i] += v; n } n int findcum(int cum) { n   int id = 0, bitmask = NN >> 1; n   while (bitmask != 0) { n     int tid = id + bitmask; n     if (seg[tid] < cum) cum -= seg[tid], id = tid; n     bitmask >>= 1; n   } n   return id + 1 - 5; n } n void solve() { n   string msg; n   cin >> msg; n   int n = (int)msg.size(); n   dp[0] = od[0] = 0; n   for (int i = 1; i <= n; ++i) n     dp[i] = dp[i - 1] + ((vowel(msg[i - 1])) ? -1 : 2), od[i] = i; n   sort(od, od + n + 1, cmp); n   int res = numeric_limits<int>::min(); n   for (int i = 0; i <= n; ++i) { n     update(od[i], 1); n     int id = findcum(1); n     if (id < od[i]) res = max(res, od[i] - id), ++ans[od[i] - id]; n   } n   if (res <= 0) n     puts( No solution ); n   else n     cout << res <<     << ans[res] << endl; n } n int main() { n   ios_base::sync_with_stdio(0); n   solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int INF = 1000000000; n const int MOD = INF + 7; n int memo[3000 + 1][100 + 1][100]; n int N; n vector<int> a, b; n int solve(int len, int w, int nouse) { n   if (len == 0) return 1; n   if (len < 0) return 0; n   if (memo[len][w][nouse] != -1) return memo[len][w][nouse]; n   int res = 0; n   for (int i = 0; i < N; i++) n     if (i != nouse) { n       if (a[i] == w) n         res = (res + solve(len - b[i], b[i], i)) % MOD; n       else if (b[i] == w) n         res = (res + solve(len - a[i], a[i], i)) % MOD; n     } n   memo[len][w][nouse] = res; n   return res; n } n int main() { n   ios_base::sync_with_stdio(false); n   memset(memo, -1, sizeof(memo)); n   int l; n   cin >> N >> l; n   a.resize(N); n   b.resize(N); n   for (int i = 0, _n = N; i < _n; i++) { n     cin >> a[i] >> b[i]; n   } n   int res = 0; n   for (int i = 0, _n = 100; i < _n; i++) n     res = (res + solve(l, i + 1, -1)) % MOD; n   cout << res << endl; n } n


#include <bits/stdc++.h> n using namespace std; n const long long mod = 1e9 + 7; n void solve() { n   long long n; n   cin >> n; n   string s; n   cin >> s; n   vector<long long> v(n); n   for (long long i = 0; i <= n - 1; i++) cin >> v[i]; n   long long ans = mod; n   for (long long i = 0; i <= n - 2; i++) { n     if (s[i] ==  R  && s[i + 1] ==  L ) ans = min(ans, abs(v[i] - v[i + 1])); n   } n   if (ans == mod) n     cout << -1; n   else n     cout << ans / 2; n } n int main() { n   long long tc = 1; n   while (tc--) { n     solve(); n   } n } n


#include <bits/stdc++.h> n using namespace std; n int A[100000]; n int main() { n   int n; n   cin >> n; n   int sh = 0; n   for (int i = 0; i < n - 1; i++) { n     int a; n     cin >> a; n     A[i] = A[i] + a; n     int k = n - i - 1; n     int l = 1; n     while (l <= k) { n       if (l * 2 > k) { n         break; n       } else { n         l = l * 2; n       } n     } n     A[i + l] = A[i + l] + A[i]; n     sh = sh + A[i]; n     cout << sh <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long INF = 1e18; n const long long mod = 1e9 + 7; n const long long N = 1e5 + 10; n struct node { n   int ind, type, row, color; n   node(int a, int b, int c, int d) { n     ind = a; n     type = b; n     row = c; n     color = d; n   } n }; n bool comp(node x, node y) { return x.ind < y.ind; } n int main() { n   ios::sync_with_stdio(false); n   int n, m, k; n   cin >> n >> m >> k; n   vector<vector<int> > ans(n, vector<int>(m, 0)); n   vector<vector<pair<int, int> > > v(2, vector<pair<int, int> >(max(n, m))); n   for (int i = 0; i < k; i++) { n     int a, b, c; n     cin >> a >> b >> c; n     v[a - 1][b - 1] = make_pair(i, c); n   } n   vector<node> vec; n   for (int i = 0; i < 2; i++) n     for (int j = 0; j < max(n, m); j++) n       if (v[i][j].second != 0) n         vec.push_back(node(v[i][j].first, i, j, v[i][j].second)); n   sort(vec.begin(), vec.end(), comp); n   for (int i = 0; i < vec.size(); i++) { n     if (vec[i].type == 0) { n       for (int j = 0; j < m; j++) ans[vec[i].row][j] = vec[i].color; n     } else { n       for (int j = 0; j < n; j++) ans[j][vec[i].row] = vec[i].color; n     } n   } n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < m; j++) cout << ans[i][j] <<    ; n     cout << endl; n   } n   return 0; n } n


//I ll always miss you like a darling. n #include <bits/stdc++.h> n using namespace std; n  n #define LL long long n #define LD long double n #define ull unsigned long long n #define x first n #define y second n #define pb push_back n #define pf push_front n #define mp make_pair n #define Pair pair<int,int> n #define pLL pair<LL,LL> n #define pii pair<double,double> n #define LOWBIT(x) x & (-x) n  n #define rep(i,a,b) for (int i=a;i<=b;i++) n #define REP(i,a,b) for (int i=a;i>=b;i--) n  n const int INF=2e9; n const LL LINF=2e16; n const int magic=348; n const int MOD=1e9+7; n const double eps=1e-10; n const double pi=acos(-1); n  n struct fastio n { n     static const int S=1e7; n     char rbuf[S+48],wbuf[S+48];int rpos,wpos,len; n     fastio() {rpos=len=wpos=0;} n     inline char Getchar() n     { n         if (rpos==len) rpos=0,len=fread(rbuf,1,S,stdin); n         if (!len) return EOF; n         return rbuf[rpos++]; n     } n     template <class T> inline void Get(T &x) n     { n         char ch;bool f;T res; n         while (!isdigit(ch=Getchar()) && ch!= - ) {} n         if (ch== - ) f=false,res=0; else f=true,res=ch- 0 ; n         while (isdigit(ch=Getchar())) res=res*10+ch- 0 ; n         x=(f?res:-res); n     } n     inline void getstring(char *s) n     { n         char ch; n         while ((ch=Getchar())<=32) {} n         for (;ch>32;ch=Getchar()) *s++=ch; n         *s=  0 ; n     } n     inline void flush() {fwrite(wbuf,1,wpos,stdout);fflush(stdout);wpos=0;} n     inline void Writechar(char ch) n     { n         if (wpos==S) flush(); n         wbuf[wpos++]=ch; n     } n     template <class T> inline void Print(T x,char ch) n     { n         char s[20];int pt=0; n         if (x==0) s[++pt]= 0 ; n         else n         { n             if (x<0) Writechar( - ),x=-x; n             while (x) s[++pt]= 0 +x%10,x/=10; n         } n         while (pt) Writechar(s[pt--]); n         Writechar(ch); n     } n     inline void printstring(char *s) n     { n         int pt=1; n         while (s[pt]!=  0 ) Writechar(s[pt++]); n     } n }io; n  n template<typename T> inline void check_max(T &x,T cmp) {x=max(x,cmp);} n template<typename T> inline void check_min(T &x,T cmp) {x=min(x,cmp);} n template<typename T> inline T myabs(T x) {return x>=0?x:-x;} n template<typename T> inline T mygcd(T x,T y) {return y==0?x:mygcd(y,x%y);} n inline int add(int x) {if (x>=MOD) x-=MOD;return x;} n inline int add(int x,int MO) {if (x>=MO) x-=MO;return x;} n inline int sub(int x) {if (x<0) x+=MOD;return x;} n inline int sub(int x,int MO) {if (x<0) x+=MO;return x;} n inline void Add(int &x,int y) {x=add(x+y);} n inline void Add(int &x,int y,int MO) {x=add(x+y,MO);} n inline void Sub(int &x,int y) {x=sub(x-y);} n inline void Sub(int &x,int y,int MO) {x=sub(x-y,MO);} n template<typename T> inline int quick_pow(int x,T y) {int res=1;while (y) {if (y&1) res=1ll*res*x%MOD;x=1ll*x*x%MOD;y>>=1;}return res;} n template<typename T> inline int quick_pow(int x,T y,int MO) {int res=1;while (y) {if (y&1) res=1ll*res*x%MO;x=1ll*x*x%MO;y>>=1;}return res;} n  n int print(int l,int r) n { n     printf( ? %d %d n ,l,r); n     fflush(stdout); n     int res;scanf( %d ,&res);return res; n } n  n int main () n { n #ifndef ONLINE_JUDGE n     double TIME=clock(); n     cerr<< Running... <<endl; n #endif n     int n;scanf( %d ,&n); n     int pos=print(1,n);int l=1,r=n; n     while (r-l+1>=4) n     { n         int mid=(l+r)>>1; n         if (l<=pos && pos<=r) n         { n             if (pos<=mid) n             { n                 int res=print(l,mid); n                 if (res==pos) r=mid; else l=mid+1; n             } n             else n             { n                 int res=print(mid+1,r); n                 if (res==pos) l=mid+1; else r=mid; n             } n         } n         else n         { n             if (pos<l) n             { n                 int res=print(pos,mid); n                 if (res==pos) r=mid; else l=mid+1; n             } n             else n             { n                 int res=print(mid+1,pos); n                 if (res==pos) l=mid+1; else r=mid; n             } n         } n     } n     if (l==r) printf( ! %d n ,l); n     else if (l+1==r) n     { n         int res=print(l,r),ans=l+r-res; n         printf( ! %d n ,ans); n     } n     else n     { n         int res=print(l,r); n         if (res==l) n         { n             int rres=print(l+1,r),ans=l+1+r-rres; n             printf( ! %d n ,ans); n         } n         else if (res==r) n         { n             int rres=print(l,r-1),ans=l+r-1-rres; n             printf( ! %d n ,ans); n         } n         else n         { n             int rres=print(l,r-1); n             if (rres==r-1) printf( ! %d n ,l); else printf( ! %d n ,r); n         } n     } n #ifndef ONLINE_JUDGE n     cerr<< Exec Time:  <<(clock()-TIME)/CLOCKS_PER_SEC<<endl; n #endif n     return 0; n }


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long n, p, num, mi = 1000000, ma = 0, ans = 1; n   cin >> n >> p; n   vector<long long> v, res; n   for (long long i = 0; i < n; i++) { n     cin >> num; n     v.push_back(num); n     mi = min(mi, num); n     ma = max(ma, num); n   } n   sort(v.begin(), v.end()); n   for (long long i = mi; i <= ma; i++) { n     ans = 1; n     for (long long j = 0; j < n; j++) { n       auto it = upper_bound(v.begin(), v.end(), i + j); n       it--; n       ans *= (distance(v.begin(), it) + 1 - j); n       ans = ans % p; n     } n     if (ans) { n       if (ans % p != 0) res.push_back(i); n     } n   } n   cout << res.size() << endl; n   for (long long i = 0; i < res.size(); i++) cout << res[i] <<    ; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   long long int t; n   cin >> t; n   while (t--) { n     long long int a, b; n     cin >> a >> b; n     long long int r1 = 45; n     long long int s1 = 10; n     long long int r2 = 20; n     long long int s2 = 5; n     long long int r3 = 45; n     long long int s3 = 10; n     long long int r4 = 20; n     long long int s4 = 5; n     long long int r5 = 5; n     long long int s5 = 2; n     long long int r6 = 20; n     long long int s6 = 5; n     long long int r7 = 45; n     long long int s7 = 10; n     long long int r8 = 20; n     long long int s8 = 5; n     long long int r9 = 45; n     long long int s9 = 10; n     long long int digit = b % 10; n     long long int sum = 0; n     if (digit == 1) { n       sum = (a / (b * s1)) * r1; n       long long int loop = ((b * s1) * (a / (b * s1))) + b; n       for (long long int i = loop; i <= a; i = i + b) { n         sum += i % 10; n       } n     } n     if (digit == 2) { n       sum = (a / (b * s2)) * r2; n       long long int loop = ((b * s2) * (a / (b * s2))) + b; n       for (long long int i = loop; i <= a; i = i + b) { n         sum += i % 10; n       } n     } n     if (digit == 3) { n       sum = (a / (b * s3)) * r3; n       long long int loop = ((b * s3) * (a / (b * s3))) + b; n       for (long long int i = loop; i <= a; i = i + b) { n         sum += i % 10; n       } n     } n     if (digit == 4) { n       sum = (a / (b * s4)); n       sum *= r4; n       long long int loop = (a / (b * s4)); n       loop *= (b * s4); n       loop += b; n       for (long long int i = loop; i <= a; i = i + b) { n         sum += i % 10; n       } n     } n     if (digit == 5) { n       sum = (a / (b * s5)) * r5; n       long long int loop = ((b * s5) * (a / (b * s5))) + b; n       for (long long int i = loop; i <= a; i = i + b) { n         sum += i % 10; n       } n     } n     if (digit == 6) { n       sum = (a / (b * s6)) * r6; n       long long int loop = ((b * s6) * (a / (b * s6))) + b; n       for (long long int i = loop; i <= a; i = i + b) { n         sum += i % 10; n       } n     } n     if (digit == 7) { n       sum = (a / (b * s7)) * r7; n       long long int loop = ((b * s7) * (a / (b * s7))) + b; n       for (long long int i = loop; i <= a; i = i + b) { n         sum += i % 10; n       } n     } n     if (digit == 8) { n       sum = (a / (b * s8)) * r8; n       long long int loop = ((b * s8) * (a / (b * s8))) + b; n       for (long long int i = loop; i <= a; i = i + b) { n         sum += i % 10; n       } n     } n     if (digit == 9) { n       sum = (a / (b * s9)) * r9; n       long long int loop = ((b * s9) * (a / (b * s9))) + b; n       for (long long int i = loop; i <= a; i = i + b) { n         sum += i % 10; n       } n     } n     cout << sum <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   cin >> n; n   if ((n - 2) % 3 != 0) n     cout <<  1   n          <<  1   << n - 2; n   else n     cout <<  1   n          <<  2   << n - 3; n } n


#include <bits/stdc++.h> n using namespace std; n int getint() { n   unsigned int c; n   int x = 0; n   while (((c = getchar()) -  0 ) >= 10) { n     if (c ==  - ) return -getint(); n     if (!~c) exit(0); n   } n   do { n     x = (x << 3) + (x << 1) + (c -  0 ); n   } while (((c = getchar()) -  0 ) < 10); n   return x; n } n int getc_str(char str[]) { n   unsigned int c, n = 0; n   while ((c = getchar()) <=    ) n     ; n   if (!~c) exit(0); n   do { n     str[n++] = c; n   } while ((c = getchar()) >    ); n   str[n] =   0 ; n   return n; n } n char in[100]; n int main() { n   int i, j, tcc, tc = 1 << 28; n   for (tcc = 0; tcc < tc; tcc++) { n     int n = getint(); n     getc_str(in); n     bool ok = 1; n     for (i = 0; i < n; i++) { n       if (in[i] !=  4  and in[i] !=  7 ) ok = 0; n     } n     int firstSum = 0, secondSum = 0; n     for (i = 0; i < n / 2; i++) firstSum += in[i]; n     for (i = 0; i < n / 2; i++) secondSum += in[i + n / 2]; n     if (firstSum != secondSum) ok = 0; n     if (ok) { n       puts( YES ); n     } else { n       puts( NO ); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const double PI = acos(-1.0); n const int dx4[] = {-1, 0, 1, 0}; n const int dy4[] = {0, 1, 0, -1}; n const int dx8[] = {-1, -1, -1, 0, 0, 1, 1, 1}; n const int dy8[] = {-1, 0, 1, -1, 1, -1, 0, 1}; n inline long long Int() { n   long long num = 0; n   char c = getchar_unlocked(); n   bool sign = true; n   while (!((c >=  0  && c <=  9 ) || c ==  - )) c = getchar_unlocked(); n   if (c ==  - ) { n     sign = false; n     c = getchar_unlocked(); n   } n   while (c >=  0  && c <=  9 ) { n     num = (num << 1) + (num << 3) + c -  0 ; n     c = getchar_unlocked(); n   } n   return sign ? num : -num; n } n template <class S, class T> n ostream& operator<<(ostream& out, const pair<S, T>& P) { n   out <<  {  << P.first <<  ,   << P.second <<  } ; n   return out; n } n template <class T> n ostream& operator<<(ostream& out, const vector<T>& v) { n   out <<  [ ; n   for (int i = 0; i < (int)v.size(); ++i) { n     out << v[i]; n     if (i + 1 < (int)v.size()) out <<  ,  ; n   } n   out <<  ] ; n   return out; n } n template <class S, class T> n ostream& operator<<(ostream& out, const map<S, T>& mp) { n   out <<  [ n ; n   for (const auto& x : mp) { n     out <<  {  << x.first <<  ,   << x.second <<  } n ; n   } n   out <<  ] n ; n   return out; n } n template <class T> n ostream& operator<<(ostream& out, const set<T>& cs) { n   out <<  { ; n   for (auto it = cs.begin(); it != cs.end();) { n     out << *it; n     it++; n     if (it != cs.end()) n       out <<  ,  ; n     else n       out <<  } ; n   } n   return out; n } n void solve(); n int main() { n   cout << fixed << setprecision(15); n   cerr << fixed << setprecision(15); n   solve(); n   return 0; n } n const double eps = 1e-10; n const int mod = (int)1e9 + 7; n const int N = (int)2e5 + 5; n int sm(string S) { n   int r = 0; n   for (char c : S) r += (c - 48); n   return r; n } n int sm(long long x) { n   int r = 0; n   while (x) { n     r += (x % 10); n     x /= 10; n   } n   return r; n } n string sub(string a, string b) { n   reverse((a).begin(), (a).end()); n   reverse((b).begin(), (b).end()); n   while (((int)(b).size()) < ((int)(a).size())) b +=  0 ; n   string res =   ; n   int carry = 0; n   for (int i = 0; i < (((int)(a).size())); ++i) { n     int x = a[i] - 48, y = b[i] - 48; n     int add = -1; n     if (x >= y + carry) { n       add = x - (y + carry); n       carry = 0; n     } else { n       add = (x + 10) - (y + carry); n       carry = 1; n     } n     res += (char)(add + 48); n   } n   if (carry) { n     cout <<  ERROR  <<   n ; n     return   ; n   } n   reverse((res).begin(), (res).end()); n   return res; n } n void print(string b) { n   int i = 0; n   while (i < ((int)(b).size()) && b[i] ==  0 ) ++i; n   while (i < ((int)(b).size())) putchar(b[i++]); n } n void solve() { n   int n = Int(), m = Int(); n   string ab =   ; n   for (int i = 0; i < (m); ++i) ab +=  1 ; n   string a =   ; n   for (int i = 0; i < (n); ++i) a +=  1 ; n   while (((int)(ab).size()) < ((int)(a).size()) + 1) ab +=  0 ; n   cout << a <<   n ; n   string b = sub(ab, a); n   print(b); n   putchar(  n ); n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 333; n long long n, ans[N], a[N][N], pos[N]; n long long absv(long long val) { n   if (val < 0) val *= -1; n   return val; n } n int main() { n   std::ios::sync_with_stdio(false); n   cin >> n; n   for (int i = 0; i <= n; i++) { n     for (int j = 0; j < n; j++) cin >> a[i][j]; n   } n   for (int i = 0; i < n; i++) pos[i] = N; n   for (int i = 0; i < n; i++) { n     long long num = i + 1, num2 = i + 1, jx = 0; n     for (int j = 0; j < n; j++) { n       if (a[n][j] <= num) { n         num = a[n][j]; n         jx = j; n         break; n       } n     } n     for (int j = jx + 1; j < n; j++) { n       if (a[n][j] <= num2) { n         num2 = a[n][j]; n         break; n       } n     } n     for (int j = 0; j < n; j++) { n       long long tmp = num; n       if (j + 1 == num) n         if (num != num2) n           num = num2; n         else n           continue; n       for (int k = 0; k < n; k++) { n         if (a[j][k] == num) n           if (k < pos[j]) { n             pos[j] = k; n             ans[j] = i + 1; n           } n       } n       num = tmp; n     } n   } n   for (int i = 0; i < n; i++) cout << ans[i] <<    ; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   int a[3] = {0}; n   int n, t; n   cin >> n; n   while (n--) { n     cin >> t; n     a[t]++; n   } n   int res = min(a[1], a[2]); n   a[1] -= a[2]; n   if (a[1] <= 0) a[1] = 0; n   res += a[1] / 3; n   cout << res << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <class T> n T abs(T x) { n   return x > 0 ? x : -x; n } n const int LEN = 300; n const int MAX = (100000 + LEN - 1) / LEN; n int n; n int m; n int was[100000], x[100000], y[100000]; n int l[MAX], r[MAX], w[MAX]; n vector<pair<pair<int, int>, int> > v; n vector<int> res; n int get(int a, int b) { n   int A = a / LEN; n   int B = b / LEN; n   if (abs(A - B) <= 1) { n     for (int i = a; i <= b; i++) n       if (!was[i]) return i; n   } else { n     for (int i = a; i <= r[A]; i++) n       if (!was[i]) return i; n     for (int i = A + 1; i < B; i++) n       if (w[i] > 0) { n         for (int j = l[i]; j <= r[i]; j++) n           if (!was[j]) return j; n       } n     for (int i = l[B]; i <= b; i++) n       if (!was[i]) return i; n   } n   return -1; n } n int add(int x) { n   was[x] = 1; n   int y = x / LEN; n   w[y]--; n   return 0; n } n int main() { n   scanf( %d%d , &n, &m); n   for (int i = 0; i < m; i++) { n     scanf( %d%d , &x[i], &y[i]); n     x[i]--; n     y[i]--; n     v.push_back(make_pair(make_pair(x[i], y[i]), i)); n   } n   int NUM = (n + LEN - 1) / LEN; n   for (int i = 0; i < NUM; i++) { n     l[i] = i * LEN; n     r[i] = min((i + 1) * LEN, n) - 1; n     w[i] = r[i] - l[i] + 1; n   } n   sort((v).begin(), (v).end()); n   for (int i = 0; i < m; i++) { n     int j = v[i].second; n     int a = n - y[j] - 1; n     int b = x[j]; n     int c = get(a, b); n     if (c != -1) { n       res.push_back(j + 1); n       add(c); n     } n   } n   sort((res).begin(), (res).end()); n   printf( %d n , ((int)(res).size())); n   for (int i = 0; i < ((int)(res).size()); i++) printf( %d  , res[i]); n   printf(  n ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename item> n item abs(item i) { n   if (i < 0) i = -i; n   return i; n } n char *s; n int n; n int pos; n int main() { n   cin >> n; n   cin >> pos; n   s = new char[n + 2]; n   cin >> s + 1; n   int cnt = 0; n   if (n == 1) { n     cout << 0; n     return 0; n   } n   if (n % 2 == 1 and pos == n / 2 + 1) pos++, cnt++; n   if (pos > n / 2) pos = n - pos + 1; n   int last = n / 2, fst = 1; n   while (fst < n - fst + 1 and s[fst] == s[n - fst + 1]) fst++; n   while (last > 0 and s[last] == s[n + 1 - last]) last--; n   if (pos >= fst and pos <= last) { n     int a = pos - fst, b = last - pos; n     cnt += 2 * (min(a, b)) + max(a, b); n   } else if (pos >= fst and pos >= last) n     cnt += pos - min(fst, last); n   else if (pos <= last and pos <= fst) n     cnt += max(fst, last) - pos; n   for (int i = 1; i <= n / 2; i++) { n     int cmp = n - i + 1; n     if (s[i] > s[cmp]) swap(s[i], s[cmp]); n     int a = abs((int)(s[i] - s[cmp])), b =  z  - s[cmp] + s[i] -  a  + 1; n     cnt += min(a, b); n   } n   cout << cnt; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 5000; n const double INF = 1e18; n const double ep = 1e-8; n struct Edge { n   int from, to; n   long long cap, flow; n   int id; n   Edge() {} n   Edge(int u, int v, long long c, long long f, int id_) n       : from(u), to(v), cap(c), flow(f), id(id_) {} n }; n struct Dinic { n   int n, m, s, t; n   vector<Edge> edges; n   vector<int> G[maxn]; n   int d[maxn], cur[maxn]; n   bool vis[maxn]; n   void init(int n) { n     for (int i = 0; i < maxn; i++) G[i].clear(); n     edges.clear(); n   } n   void add(int from, int to, long long cap, int id) { n     edges.push_back(Edge(from, to, cap, 0, id)); n     edges.push_back(Edge(to, from, 0, 0, -1)); n     m = edges.size(); n     G[from].push_back(m - 2); n     G[to].push_back(m - 1); n   } n   bool BFS() { n     memset(vis, 0, sizeof(vis)); n     queue<int> Q; n     Q.push(s); n     d[s] = 0; n     vis[s] = 1; n     while (!Q.empty()) { n       int x = Q.front(); n       Q.pop(); n       for (int i = 0; i < G[x].size(); i++) { n         Edge& e = edges[G[x][i]]; n         if (!vis[e.to] && e.cap - e.flow > ep) { n           vis[e.to] = 1; n           d[e.to] = d[x] + 1; n           Q.push(e.to); n         } n       } n     } n     return vis[t]; n   } n   long long DFS(int x, long long a) { n     if (x == t || a == 0) return a; n     long long flow = 0, f; n     for (int& i = cur[x]; i < G[x].size(); i++) { n       Edge& e = edges[G[x][i]]; n       if (d[x] + 1 == d[e.to] && (f = DFS(e.to, min(a, e.cap - e.flow))) > 0) { n         e.flow += f; n         edges[G[x][i] ^ 1].flow -= f; n         flow += f; n         a -= f; n         if (a == 0) break; n       } n     } n     return flow; n   } n   long long maxFlow(int s, int t) { n     this->s = s; n     this->t = t; n     long long flow = 0; n     while (BFS()) { n       memset(cur, 0, sizeof(cur)); n       flow += DFS(s, INF); n     } n     return flow; n   } n   void show() { n     for (auto& e : edges) { n       printf( (%d -> %d) => cap = %lld , flow = %lld n , e.from, e.to, e.cap, n              e.flow); n     } n   } n   void DFS2(int v) { n     vis[v] = true; n     for (int i = 0; i < G[v].size(); ++i) { n       Edge e = edges[G[v][i]]; n       if (!vis[e.to] && e.cap - e.flow > ep) { n         DFS2(e.to); n       } n     } n   } n   void showMinCut(int v) { n     DFS2(v); n     for (auto& e : edges) { n       if (vis[e.from] && !vis[e.to] && e.cap != 0 && e.flow != 0) n         printf( (%d -> %d) => cap = %lld , flow = %lld n , e.from, e.to, e.cap, n                e.flow); n     } n   } n }; n int N1, N2, M; n int deg[4005]; n vector<int> ans[2005]; n int s, t, tot; n int main() { n   Dinic G; n   scanf( %d %d %d , &N1, &N2, &M); n   tot = N1 + N2 + 15; n   s = tot - 2, t = tot - 1; n   G.init(tot); n   for (int i = 1; i <= M; ++i) { n     int x, y; n     scanf( %d %d , &x, &y); n     G.add(x, N1 + y, 1, i); n     ++deg[x]; n     ++deg[y + N1]; n   } n   int min_deg = M + 10; n   for (int i = 1; i <= N1 + N2; ++i) { n     min_deg = min(min_deg, deg[i]); n   } n   for (int i = 1; i <= N1; ++i) { n     G.add(s, i, deg[i] - min_deg, -1); n   } n   for (int i = 1; i <= N2; ++i) { n     G.add(i + N1, t, deg[i + N1] - min_deg, -1); n   } n   for (int k = min_deg; ~k; --k) { n     G.maxFlow(s, t); n     for (auto& e : G.edges) { n       if (e.from >= 1 && e.from <= N1 && e.to >= N1 + 1 && e.to <= N2 + N1) { n         if (e.flow == 0 && e.cap == 1) { n           ans[k].push_back(e.id); n         } n       } n     } n     for (int i = 1; i <= N1; ++i) { n       G.add(s, i, 1, -1); n     } n     for (int i = 1; i <= N2; ++i) { n       G.add(i + N1, t, 1, -1); n     } n   } n   for (int i = 0; i <= min_deg; ++i) { n     printf( %d  , (int)ans[i].size()); n     for (int j : ans[i]) { n       printf( %d  , j); n     } n     printf(  n ); n   } n } n


#include <bits/stdc++.h> n using namespace std; n const long long MOD = (1e9) + 7; n const long double EPS = 0.0000001; n int main(void) { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   string s; n   cin >> s; n   string t; n   cin >> t; n   int szs = s.size(); n   int szt = t.size(); n   int cur = 0; n   int pref[szt + 1]; n   int suff[szt + 1]; n   for (int i = 0; i < szs; i++) { n     if (s[i] == t[cur]) { n       pref[cur] = i; n       cur++; n     } n     if (cur == szt) break; n   } n   cur--; n   for (int i = szs - 1; i >= 0; i--) { n     if (s[i] == t[cur]) { n       suff[cur] = i; n       cur--; n     } n     if (cur == -1) break; n   } n   int ans = 0; n   ans = max(ans, szs - 1 - suff[szt - 1]); n   ans = max(ans, suff[0]); n   ans = max(ans, pref[0]); n   ans = max(ans, szs - 1 - pref[szt - 1]); n   for (int i = 0; i < szt - 1; i++) { n     ans = max(ans, suff[i + 1] - pref[i] - 1); n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n inline int read() { n   int num = 0; n   char c = getchar(); n   if (c ==  - ) return -read(); n   while (c >=  0  && c <=  9 ) { n     num = (num << 3) + (num << 1) + c -  0 ; n     c = getchar(); n   } n   return num; n } n inline long long READ() { n   long long num = 0; n   char c = getchar(); n   if (c ==  - ) return -READ(); n   while (c >=  0  && c <=  9 ) { n     num = (num << 3) + (num << 1) + c -  0 ; n     c = getchar(); n   } n   return num; n } n vector<int> nz[100007], nf[100007]; n pair<int, int> en[100007]; n int cost[100007]; n int djg = 0; n bool lgl[100007], l8[100007]; n int minv = ((int)0x3f3f3f3f), mincnt = 0; n long long ans = 1, ans2 = 0; n int n, m; n void dfs(int now) { n   l8[now] = 1; n   for (int i = 0; i < ((int)(nz[now]).size()); i++) n     if (!l8[nz[now][i]]) dfs(nz[now][i]); n   en[now] = make_pair(djg++, now); n } n void dfs2(int now) { n   lgl[now] = 1; n   if (cost[now] < minv) { n     minv = cost[now]; n     mincnt = 1; n   } else if (cost[now] == minv) n     mincnt++; n   for (int i = 0; i < ((int)(nf[now]).size()); i++) n     if (!lgl[nf[now][i]]) dfs2(nf[now][i]); n } n int main() { n   scanf( %d , &n); n   for (int i = 1; i <= n; i++) scanf( %d , cost + i); n   scanf( %d , &m); n   for (int i = 0; i < m; i++) { n     int a, b; n     scanf( %d%d , &a, &b); n     nz[a].push_back(b); n     nf[b].push_back(a); n   } n   for (int i = 1; i <= n; i++) n     if (!l8[i]) dfs(i); n   sort(en + 1, en + n + 1); n   for (int i = n; i >= 1; i--) n     if (!lgl[en[i].second]) { n       minv = ((int)0x3f3f3f3f); n       dfs2(en[i].second); n       ans = ans * mincnt % 1000000007; n       ans2 += minv; n     } n   printf( %I64d %I64d n , ans2, ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int slope[150 + 1]; n int main(void) { n   int w, h; n   scanf( %d%d , &w, &h); n   for (int i = 0; i < 2; i++) { n     int ww, hh; n     scanf( %d%d , &ww, &hh); n     slope[hh] = ww; n   } n   while (h > 0) { n     w += h; n     if (slope[h] > 0) w = max(0, w - slope[h]); n     h--; n   } n   printf( %d n , w); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, t, s, ma; n int main() { n   cin >> n; n   for (int i = 1; i <= n; ++i) { n     cin >> t; n     s += t; n     ma = max(ma, t); n   } n   cout << max(ma, (2 * s + n) / n); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long sign(long long x) { return x < 0 ? -1 : 1; } n pair<long long, long long> part(long long from, long long to, long long s, n                                 long long dir) { n   pair<long long, long long> res{dir, abs(to - from)}; n   if (to != from && sign(to - from) != sign(dir)) { n     res.first *= -1; n     res.second += 2 * abs(from - (dir == -1 ? 0 : s)); n   } n   return res; n } n long long solve(long long s, long long x1, long long x2, long long t1, n                 long long t2, long long p, long long d) { n   auto toTram = part(p, x1, s, d); n   return min(part(x1, x2, s, sign(x2 - x1)).second * t2, n              (toTram.second + part(x1, x2, s, toTram.first).second) * t1); n } n void tests() { n   assert(7 == solve(10, 3, 8, 1, 2, 1, 1)); n   assert(9 == solve(10, 3, 8, 1, 2, 1, -1)); n   assert(10 == solve(10, 3, 8, 1, 2, 2, -1)); n   assert(12 == solve(10, 3, 8, 1, 5, 4, -1)); n   assert(24 == solve(10, 3, 8, 1, 5, 4, 1)); n   assert(20 == solve(10, 3, 8, 1, 4, 4, 1)); n   assert(15 == solve(10, 3, 8, 1, 3, 10, -1)); n   assert(18 == solve(10, 3, 8, 1, 4, 10, -1)); n   assert(19 == solve(10, 3, 8, 1, 4, 9, 1)); n   assert(20 == solve(10, 3, 8, 2, 4, 9, 1)); n   assert(5 == solve(10, 3, 8, 1, 4, 3, 1)); n   assert(11 == solve(10, 3, 8, 1, 4, 3, -1)); n   assert(20 == solve(10, 3, 8, 1, 5, 8, 1)); n   assert(16 == solve(10, 3, 8, 1, 4, 8, -1)); n   assert(5 + 4 + 5 == solve(10, 8, 3, 1, 4, 3, 1)); n   assert(6 + 5 + 4 + 5 == solve(10, 8, 3, 1, 5, 3, -1)); n   assert(4 + 5 == solve(10, 8, 3, 1, 4, 8, 1)); n   assert(5 == solve(10, 8, 3, 1, 4, 8, -1)); n   assert(8 == solve(4, 2, 4, 3, 4, 1, 1)); n   assert(7 == solve(5, 4, 0, 1, 2, 3, 1)); n } n int main() { n   tests(); n   long long s, x1, x2, t1, t2, p, d; n   while (cin >> s >> x1 >> x2 >> t1 >> t2 >> p >> d) { n     cout << solve(s, x1, x2, t1, t2, p, d) << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long int f1(long int x, long int a) { n   if ((x < 0 && x > (-1 * ((double)a) / 2)) || (x > 0 && x < ((double)a) / 2) || n       x == 0) n     return 1; n   else n     return 0; n } n long long int f2(long long int x, long long int a) { n   if ((x < 0 && x > (-1 * ((double)a))) || (x > 0 && x < ((double)a))) n     return 1; n   else n     return 0; n } n int main() { n   long long int x, y, a, num, p, flag = 1, ans; n   cin >> a >> x >> y; n   num = (y - a) % (2 * a); n   ans = (y - a - num) / a; n   ans = 1 + (ans / 2) * 3; n   p = num / a; n   if (p * a == num) n     flag = 0; n   else { n     if (y < a) { n       if (!f1(x, a)) flag = 0; n     } else { n       if (p < 1) { n         if (f1(x, a)) n           ans += 1; n         else n           flag = 0; n       } else { n         if (f2(x, a)) { n           if (x < 0) n             ans += 2; n           else n             ans += 3; n         } else n           flag = 0; n       } n     } n   } n   if (flag) n     cout << ans; n   else n     cout << -1; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long a, x = 1e18; n int main() { n   cin >> a; n   cout << a - x % a * 9 % a * 9 % a <<     << a - x % a * 9 % a * 9 % a + x - 1; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long mod = 998244353; n const int maxn = 1e5 + 5; n const int inf = 0x3f3f3f3f; n const double PI = acos(-1.0); n const long long INF = 0x3f3f3f3f3f3f3f3f; n int a[maxn], val[maxn]; n int main() { n   int n; n   cin >> n; n   for (int i = 1; i <= n; ++i) { n     cin >> a[i]; n     val[i] = a[i] + 1; n   } n   for (int i = 1; i <= n; ++i) { n     val[i] = max(val[i], val[i - 1]); n   } n   for (int i = n; i > 0; --i) { n     val[i] = max(val[i + 1] - 1, val[i]); n   } n   long long ans = 0; n   for (int i = 1; i <= n; ++i) { n     ans += val[i] - a[i] - 1; n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n #pragma comment(linker,  /STACK:268435456 ) n using namespace std; n const double PI = 3.1415926535897932384626433832795; n template <class T> n T min(T &a, T &b) { n   return (a < b) ? a : b; n } n template <class T> n T max(T &a, T &b) { n   return (a > b) ? a : b; n } n template <class T> n T sqr(T a) { n   return a * a; n } n template <class T> n T abs(T &a) { n   return (a < 0) ? (-a) : a; n } n inline void read(int &x) { scanf( %d , &x); } n inline void readln(int &x) { scanf( %d n , &x); } n inline void write(int x) { printf( %d , x); } n inline void writeln(int x) { printf( %d n , x); } n inline void read(long long &x) { scanf( %lld , &x); } n inline void readln(long long &x) { scanf( %lld n , &x); } n inline void write(long long x) { printf( %lld , x); } n inline void writeln(long long x) { printf( %lld n , x); } n inline void read(double &x) { scanf( %lf , &x); } n inline void readln(double &x) { scanf( %lf n , &x); } n inline void write(double x) { printf( %.15lf , x); } n inline void writeln(double x) { printf( %.15lf n , x); } n inline void read(char *s) { scanf( %s , s); } n inline void readln(char *s) { scanf( %s n , s); } n inline void write(char *s) { printf( %s , s); } n inline void writeln(char *s) { printf( %s n , s); } n inline int readInt() { n   int x; n   read(x); n   return x; n } n inline long long readLong() { n   long long x; n   read(x); n   return x; n } n inline double readDouble() { n   double x; n   read(x); n   return x; n } n inline void showTime() {} n const double eps = 1e-10; n double x, y, yw, r; n double ya, yb, yao, ybo; n double wp(double x1, double y1, double x2, double y2) { n   return x1 * y2 - x2 * y1; n } n double dist(double x1, double y1, double x2, double y2) { n   return sqrt(sqr(x1 - x2) + sqr(y1 - y2)); n } n bool goodb(double xt) { n   double x1 = x; n   double y1 = y; n   double x2 = xt; n   double y2 = yw; n   double s = wp(x2 - x1, y2 - y1, 0 - x1, ybo - y1); n   double d = s / dist(x1, y1, x2, y2); n   if (d < r + eps) return false; n   return true; n } n bool gooda(double xt) { n   double x1 = x; n   double y1 = y; n   double x2 = xt; n   double y2 = yw; n   double s = wp(x2 - x1, y2 - y1, 0 - x1, yao - r - y1); n   double d = s / dist(x1, y1, x2, y2); n   if (d > -eps) return false; n   return true; n } n int main() { n   srand((unsigned)time(NULL)); n   cin >> ya >> yb >> yw >> x >> y >> r; n   yw -= r; n   yao = 2 * yw - ya; n   ybo = 2 * yw - yb; n   double xt = 0; n   double step = x; n   while (step > eps) { n     if (!goodb(xt + step)) xt += step; n     step /= 2; n   } n   while (!goodb(xt + step)) xt += step; n   xt += step * 100000; n   if (gooda(xt)) { n     cout.setf(ios::fixed); n     cout.precision(10); n     cout << xt; n   } else { n     cout << -1; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 3e5 + 5; n const double pi = acos(-1.0); n const long long int inf = 0x3f3f3f3f3f3f3f3f; n const long long int mod = 998244353; n bool isPowerOfTwo(int x) { return x && (!(x & (x - 1))); } n void fast() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n } n string s[2]; n int n, k, h; n bool a[2][100005]; n void dfs(int x, int y) { n   if (s[x][y] !=  -  || a[x][y] == true || y < h) return; n   if (y + k >= n) { n     cout <<  YES ; n     exit(0); n   } n   h++; n   a[x][y] = true; n   dfs(1 - x, y + k); n   dfs(x, y + 1); n   dfs(x, y - 1); n   h--; n } n int main() { n   cin >> n >> k >> s[0] >> s[1]; n   dfs(0, 0); n   cout <<  NO ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long N = 1e5 + 3, K = 11, M = N * 4; n const long long MOD = 998244353, oo = 1e9, OO = 1e18, mod = MOD; n const double pi = acos(-1), eps = 1e-17; n long long di[] = {0, 0, 1, -1}; n long long dj[] = {1, -1, 0, 0}; n long long pos[N]; n int32_t main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   ; n   long long ret = 1; n   long long n; n   cin >> n; n   long long arr2[n * n]; n   for (long long i = 0; i < n * n; i++) cin >> arr2[i]; n   vector<long long> v; n   for (long long i = 0; i < n * n; i++) v.push_back(i); n   do { n     long long arr[n * n]; n     for (long long i = 0; i < n * n; i++) arr[i] = arr2[v[i]]; n     map<long long, long long> mp; n     long long sum = 0; n     for (long long i = 0; i < n; i++) { n       sum += arr[i * n + i]; n     } n     mp[sum]++; n     if (mp.size() > 1) continue; n     sum = 0; n     for (long long i = 0; i < n; i++) { n       sum = 0; n       for (long long j = 0; j < n; j++) sum += arr[i * n + j]; n       mp[sum]++; n       if (mp.size() > 1) continue; n     } n     for (long long j = 0; j < n; j++) { n       sum = 0; n       for (long long i = 0; i < n; i++) sum += arr[i * n + j]; n       mp[sum]++; n       if (mp.size() > 1) continue; n     } n     long long r = 0, c = n - 1; n     sum = 0; n     while (c >= 0) { n       sum += arr[r * n + c]; n       r++, c--; n     } n     mp[sum]++; n     if (mp.size() > 1) continue; n     if (mp.size() == 1) { n       cout << sum <<   n ; n       for (long long i = 0; i < n * n; i++) { n         if (i && i % 3 == 0) cout <<   n ; n         cout << arr[i] <<    ; n       } n       return 0; n     } n   } while (next_permutation(v.begin(), v.end())); n } n