#include <bits/stdc++.h> n using namespace std; n const double PI = acos(-1); n const double eps = 1e-9; n const int inf = 2000000000; n const long long infLL = 9000000000000000000; n inline bool checkBit(long long n, int i) { return n & (1LL << i); } n inline long long setBit(long long n, int i) { return n | (1LL << i); } n inline long long resetBit(long long n, int i) { return n & (~(1LL << i)); } n int dx4[] = {0, 0, +1, -1}; n int dy4[] = {+1, -1, 0, 0}; n int dx8[] = {+1, 0, -1, 0, +1, +1, -1, -1}; n int dy8[] = {0, +1, 0, -1, +1, -1, +1, -1}; n inline bool EQ(double a, double b) { return fabs(a - b) < 1e-9; } n inline bool isLeapYear(long long year) { n   return (year % 400 == 0) || (year % 4 == 0 && year % 100 != 0); n } n inline bool isIntege(double num) { return (num == (int)num); } n inline bool collinear(int x1, int y1, int x2, int y2, int x3, int y3) { n   return (y1 - y2) * (x1 - x3) == (y1 - y3) * (x1 - x2); n } n inline double coDist(double x1, double y1, double x2, double y2) { n   return sqrt(((x1 - x2) * (x1 - x2)) + ((y1 - y2) * (y1 - y2))); n } n inline double TriangleAreaWithSide(double a, double b, double c) { n   double s = (a + b + c) / 2; n   double area = sqrt(s * (s - a) * (s - b) * (s - c)); n   return area; n } n inline double area3(double x1, double y1, double x2, double y2, double x3, n                     double y3) { n   double A = abs((x1 * y2 + x2 * y3 + x3 * y1) - (y1 * x2 + y2 * x3 + y3 * x1)); n   A /= 2.0; n   return A; n } n inline long double degreetoradian(long double x) { n   long double val = PI * x; n   val /= (180.0); n   return val; n } n inline void normal(long long &a) { n   a %= 1000000007; n   (a < 0) && (a += 1000000007); n } n template <typename T> n inline T gcd(T a, T b) { n   T c; n   while (b) { n     c = b; n     b = a % b; n     a = c; n   } n   return a; n } n inline long long modMul(long long a, long long b) { n   a %= 1000000007, b %= 1000000007; n   normal(a), normal(b); n   return (a * b) % 1000000007; n } n inline long long modAdd(long long a, long long b) { n   a %= 1000000007, b %= 1000000007; n   normal(a), normal(b); n   return (a + b) % 1000000007; n } n inline long long modSub(long long a, long long 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 modPow(long long b, long long p) { n   long long 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 modInverse(long long a) { return modPow(a, 1000000007 - 2); } n inline long long modDiv(long long a, long long b) { n   return modMul(a, modInverse(b)); n } n inline bool equalTo(double a, double b) { n   if (fabs(a - b) <= eps) n     return true; n   else n     return false; n } n inline bool notEqual(double a, double b) { n   if (fabs(a - b) > eps) n     return true; n   else n     return false; n } n inline bool lessThan(double a, double b) { n   if (a + eps < b) n     return true; n   else n     return false; n } n inline bool lessThanEqual(double a, double b) { n   if (a < b + eps) n     return true; n   else n     return false; n } n inline bool greaterThan(double a, double b) { n   if (a > b + eps) n     return true; n   else n     return false; n } n inline bool greaterThanEqual(double a, double b) { n   if (a + eps > b) n     return true; n   else n     return false; n } n inline string to_s(int t) { n   stringstream ss; n   ss << t; n   return ss.str(); n } n struct edge { n   int p, q, w; n }; n bool cmp(edge &a, edge &b) { return a.w < b.w; } 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 T> n ostream &operator<<(ostream &os, const multiset<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 <<   n ; } n template <typename T> n void faltu(T a[], int n) { n   for (int i = 0; i < n; ++i) cerr << a[i] <<    ; n   cerr <<   n ; n } n template <typename T, typename... hello> n void faltu(T arg, const hello &...rest) { n   cerr << arg <<    ; n   faltu(rest...); n } n const int mx = 1e5 + 5; n bool cnt[mx]; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   ; n   int n, k; n   cin >> n >> k; n   long long g; n   cin >> g; n   for (int i = 1; i < n; i++) { n     long long x; n     cin >> x; n     g = gcd(x, g); n   } n   vector<long long> st; n   for (long long i = 0, j = 0; i < k; i++, j += g) { n     int z = j % k; n     if (!cnt[z]) st.emplace_back(z), cnt[z] = 1; n   } n   cout << st.size() <<   n ; n   for (int i = 0; i < k; i++) { n     if (cnt[i]) cout << i <<    ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 1 << 18; n const int MOD = 2013265921, MAX = MOD / 2; n const int gen = 440564289; n long long power(long long x, int n) { n   if (n <= 1) return n ? x : 1LL; n   long long t = power(x, n / 2); n   if (n & 1) n     return (t * t % MOD) * x % MOD; n   else n     return t * t % MOD; n } n long long tmp[maxn]; n void DFT(long long *a, long long x, int n) { n   if (n == 1) { n     a[0] %= MOD; n     if (a[0] < 0) a[0] += MOD; n     return; n   } n   for (int i = 0; i < n; i++) tmp[i] = a[i]; n   for (int i = 0; i < n; i++) a[i % 2 ? n / 2 + i / 2 : i / 2] = tmp[i]; n   long long *a1 = a, *a2 = a + n / 2, xx = x * x % MOD; n   DFT(a1, xx, n / 2); n   DFT(a2, xx, n / 2); n   long long now = 1; n   for (int i = 0; i < n / 2; i++, now = now * x % MOD) { n     long long val = now * a2[i] % MOD; n     tmp[i] = a1[i] + val - MOD; n     tmp[i + n / 2] = a1[i] - val; n   } n   for (int i = 0; i < n; i++) a[i] = (tmp[i] < 0 ? tmp[i] + MOD : tmp[i]); n } n void mul(long long *a, long long *b, long long *c, int n) { n   long long x = power(gen, (1 << 27) / n), xinv = power(x, n - 1); n   long long ninv = power(n, MOD - 2); n   DFT(a, x, n); n   DFT(b, x, n); n   for (int i = 0; i < n; i++) c[i] = a[i] * b[i] % MOD; n   DFT(c, xinv, n); n   for (int i = 0; i < n; i++) { n     c[i] = c[i] * ninv % MOD; n     if (c[i] > MAX) c[i] -= MOD; n   } n } n int n, m, Q, L; n char s[maxn], t[maxn]; n unsigned long long sbit[maxn], tbit[maxn]; n int match(int x, int y, int len) { n   int ret = 0; n   for (; len >= 64; len -= 64, x += 64, y += 64) n     ret += __builtin_popcountll(sbit[x] ^ tbit[y]); n   if (len) n     ret += n         __builtin_popcountll((sbit[x] << (64 - len)) ^ (tbit[y] << (64 - len))); n   return ret; n } n long long *poly[maxn], a[maxn], b[maxn]; n void precal() { n   n = strlen(s), m = strlen(t); n   for (int i = 0; i < n; i++) n     for (int j = 0; j < 64 && i + j < n; j++) n       if (s[i + j] ==  1 ) sbit[i] |= (1ULL << j); n   for (int i = 0; i < m; i++) n     for (int j = 0; j < 64 && i + j < m; j++) n       if (t[i + j] ==  1 ) tbit[i] |= (1ULL << j); n   L = n * sqrt(150 * log2(n) / Q); n   int n2 = 1; n   while (n2 < L + m) n2 *= 2; n   for (int i = 0; i < n; i += L) { n     poly[i / L] = new long long[n2]; n     memset(a, 0, sizeof(a)); n     memset(b, 0, sizeof(b)); n     for (int j = 0; j < L && i + j < n; j++) a[j] = (s[i + j] ==  0  ? -1 : 1); n     for (int j = 0; j < m; j++) b[m - 1 - j] = (t[j] ==  0  ? -1 : 1); n     mul(a, b, poly[i / L], n2); n   } n } n int main() { n   scanf( %s%s%d , s, t, &Q); n   precal(); n   while (Q--) { n     int x, y, l; n     scanf( %d%d%d , &x, &y, &l); n     if (l <= L) { n       printf( %d n , match(x, y, l)); n       continue; n     } n     int ans = 0; n     if (x % L) { n       int l2 = L - x % L; n       ans += match(x, y, l2); n       x += l2; n       y += l2; n       l -= l2; n     } n     if ((x + l) % L) { n       int l2 = (x + l - 1) % L + 1; n       ans += match(x + l - l2, y + l - l2, l2); n       l -= l2; n     } n     if (!l) { n       printf( %d n , ans); n       continue; n     } n     for (int i = x, j = y; l; i += L, j += L, l -= L) n       ans += (L - poly[i / L][m - 1 - j]) / 2; n     printf( %d n , ans); n   } n } n


#include <bits/stdc++.h> n using namespace std; n int arr[503][503]; n int main() { n   int n, k; n   cin >> n >> k; n   int p = 1; n   for (int i = 1; i <= n; i++) { n     for (int j = 1; j < k; j++) { n       arr[i][j] = p++; n     } n   } n   for (int i = 1; i <= n; i++) { n     for (int j = k; j <= n; j++) { n       arr[i][j] = p++; n     } n   } n   int sum = 0; n   for (int i = 1; i <= n; i++) { n     sum += arr[i][k]; n   } n   cout << sum << endl; n   for (int i = 1; i <= n; i++) { n     for (int j = 1; j <= n; j++) { n       cout << arr[i][j] <<    ; n     } n     cout << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T, typename U> n std::istream& operator>>(std::istream& i, pair<T, U>& p) { n   i >> p.first >> p.second; n   return i; n } n template <typename T> n std::istream& operator>>(std::istream& i, vector<T>& t) { n   for (auto& v : t) { n     i >> v; n   } n   return i; n } n template <typename T, typename U> n std::ostream& operator<<(std::ostream& o, const pair<T, U>& p) { n   o << p.first <<     << p.second; n   return o; n } n template <typename T> n std::ostream& operator<<(std::ostream& o, const vector<T>& t) { n   if (t.empty()) o <<   n ; n   for (size_t i = 0; i < t.size(); ++i) { n     o << t[i] <<    n [i == t.size() - 1]; n   } n   return o; n } n template <typename T> n using minheap = priority_queue<T, vector<T>, greater<T>>; n template <typename T> n using maxheap = priority_queue<T, vector<T>, less<T>>; n template <typename T> n bool in(T a, T b, T c) { n   return a <= b && b < c; n } n unsigned int logceil(int first) { n   return 8 * sizeof(int) - __builtin_clz(first); n } n namespace std { n template <typename T, typename U> n struct hash<pair<T, U>> { n   hash<T> t; n   hash<U> u; n   size_t operator()(const pair<T, U>& p) const { n     return t(p.first) ^ (u(p.second) << 7); n   } n }; n }  // namespace std n template <typename T, typename F> n T bsh(T l, T h, const F& f) { n   T r = -1, m; n   while (l <= h) { n     m = (l + h) / 2; n     if (f(m)) { n       l = m + 1; n       r = m; n     } else { n       h = m - 1; n     } n   } n   return r; n } n template <typename F> n double bshd(double l, double h, const F& f, double p = 1e-9) { n   unsigned int r = 3 + (unsigned int)log2((h - l) / p); n   while (r--) { n     double m = (l + h) / 2; n     if (f(m)) { n       l = m; n     } else { n       h = m; n     } n   } n   return (l + h) / 2; n } n template <typename T, typename F> n T bsl(T l, T h, const F& f) { n   T r = -1, m; n   while (l <= h) { n     m = (l + h) / 2; n     if (f(m)) { n       h = m - 1; n       r = m; n     } else { n       l = m + 1; n     } n   } n   return r; n } n template <typename F> n double bsld(double l, double h, const F& f, double p = 1e-9) { n   unsigned int r = 3 + (unsigned int)log2((h - l) / p); n   while (r--) { n     double m = (l + h) / 2; n     if (f(m)) { n       h = m; n     } else { n       l = m; n     } n   } n   return (l + h) / 2; n } n template <typename T> n T gcd(T a, T b) { n   if (a < b) swap(a, b); n   return b ? gcd(b, a % b) : a; n } n template <typename T> n class vector2 : public vector<vector<T>> { n  public: n   vector2() {} n   vector2(size_t a, size_t b, T t = T()) n       : vector<vector<T>>(a, vector<T>(b, t)) {} n }; n template <typename T> n class vector3 : public vector<vector2<T>> { n  public: n   vector3() {} n   vector3(size_t a, size_t b, size_t c, T t = T()) n       : vector<vector2<T>>(a, vector2<T>(b, c, t)) {} n }; n template <typename T> n class vector4 : public vector<vector3<T>> { n  public: n   vector4() {} n   vector4(size_t a, size_t b, size_t c, size_t d, T t = T()) n       : vector<vector3<T>>(a, vector3<T>(b, c, d, t)) {} n }; n template <typename T> n class vector5 : public vector<vector4<T>> { n  public: n   vector5() {} n   vector5(size_t a, size_t b, size_t c, size_t d, size_t e, T t = T()) n       : vector<vector4<T>>(a, vector4<T>(b, c, d, e, t)) {} n }; n class vowels { n  public: n   void solve(istream& cin, ostream& cout) { n     int N; n     cin >> N; n     vector<int> D(1 << 24, 0); n     for (int i = 0; i < N; ++i) { n       string S; n       cin >> S; n       int u = 0; n       for (char c : S) u |= (1 << (c -  a )); n       u ^= (1 << 24) - 1; n       D[u]++; n     } n     for (int i = 0; i < 24; ++i) { n       for (int j = 0; j < (1 << 24); ++j) { n         if (j & (1 << i)) D[j ^ (1 << i)] += D[j]; n       } n     } n     int ans = 0; n     for (int i = 0; i < (1 << 24); ++i) { n       ans ^= (N - D[i]) * (N - D[i]); n     } n     cout << ans << endl; n   } n }; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(nullptr); n   cout.tie(nullptr); n   vowels solver; n   std::istream& in(std::cin); n   std::ostream& out(std::cout); n   solver.solve(in, out); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 310, INF = (1 << 27); n int d[MAXN][MAXN]; n int all[20][MAXN][MAXN]; n int ans[MAXN][MAXN]; n int main() { n   ios::sync_with_stdio(false); n   int n, m; n   cin >> n >> m; n   for (int i = 0; i < n; i++) n     for (int j = 0; j < n; j++) d[i][j] = INF; n   for (int i = 0; i < n; i++) d[i][i] = 0; n   for (int i = 0; i < m; i++) { n     int x, y, w1, w2; n     cin >> x >> y >> w1 >> w2; n     x--; n     y--; n     w1 = -w1; n     w2 = -w2; n     d[x][y] = w1; n     d[y][x] = w2; n   } n   for (int i = 0; i < n; i++) n     for (int j = 0; j < n; j++) all[0][i][j] = ans[i][j] = d[i][j]; n   for (int k = 0; k < n; k++) n     for (int i = 0; i < n; i++) n       for (int j = 0; j < n; j++) d[i][j] = min(d[i][j], d[i][k] + d[k][j]); n   bool hasNeg = false; n   for (int i = 0; i < n; i++) n     if (d[i][i] < 0) hasNeg = true; n   if (!hasNeg) { n     cout << 0 << endl; n     return 0; n   } n   int ind; n   for (ind = 1; (1 << ind) < n; ind++) { n     for (int i = 0; i < n; i++) n       for (int j = 0; j < n; j++) { n         all[ind][i][j] = INF; n         for (int k = 0; k < n; k++) n           all[ind][i][j] = n               min(all[ind][i][j], all[ind - 1][i][k] + all[ind - 1][k][j]); n       } n   } n   ind--; n   int length = 1; n   while (ind >= 0) { n     int tmp[MAXN][MAXN]; n     for (int i = 0; i < n; i++) n       for (int j = 0; j < n; j++) { n         tmp[i][j] = INF; n         for (int k = 0; k < n; k++) n           tmp[i][j] = min(tmp[i][j], ans[i][k] + all[ind][k][j]); n       } n     hasNeg = false; n     for (int i = 0; i < n; i++) n       if (tmp[i][i] < 0) hasNeg = true; n     if (!hasNeg) { n       for (int i = 0; i < n; i++) n         for (int j = 0; j < n; j++) ans[i][j] = tmp[i][j]; n       length += (1 << ind); n     } n     ind--; n   } n   cout << length + 1 << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 2e6 + 7; n const int mod = 51123987; n const int inv2 = mod + 1 >> 1; n struct Edge { n   int to, last, v; n } E[maxn]; n int fa[maxn]; n int len[maxn]; n int dep[maxn]; n int head[maxn]; n int tot; n int last; n int cnt; n int n; n char s[maxn]; n int pre[maxn]; n long long suf[maxn]; n void Init() { n   for (int i = 0; i <= tot; ++i) { n     fa[i] = len[i] = dep[i] = 0; n     head[i] = 0; n   } n   cnt = 0; n   fa[last = 0] = fa[tot = 1] = 1; n   len[1] = -1; n } n int Getfail(int x, int ed) { n   while (s[ed] ^ s[ed - len[x] - 1]) x = fa[x]; n   return x; n } n int Check(int x, int v) { n   for (int i = head[x], y; y = E[i].to, i; i = E[i].last) n     if (E[i].v == v) return E[i].to; n   return 0; n } n void Adde(int x, int y, int z) { n   E[++cnt] = (Edge){y, head[x], z}; n   head[x] = cnt; n } n void Extend(int x, int ed) { n   int p = Getfail(last, ed); n   int np = Check(p, x); n   if (!np) { n     np = ++tot; n     int pp = Getfail(fa[p], ed); n     fa[np] = Check(pp, x); n     Adde(p, np, x); n     len[np] = len[p] + 2; n     dep[np] = dep[fa[np]] + 1; n   } n   last = np; n } n int main() { n   Init(); n   scanf( %d , &n); n   scanf( %s , s + 1); n   for (int i = 1; i <= n; ++i) Extend(s[i] -  a , i), pre[i] = dep[last]; n   Init(); n   reverse(s + 1, s + n + 1); n   for (int i = 1; i <= n; ++i) n     Extend(s[i] -  a , i), suf[n - i + 1] = dep[last]; n   for (int i = n; i; --i) suf[i] += suf[i + 1], suf[i] %= mod; n   int ans = 0; n   for (int i = 1; i <= n; ++i) (ans += 1LL * pre[i] * suf[i + 1] % mod) %= mod; n   int sum = 1LL * suf[1] * (suf[1] - 1) / 2 % mod; n   ans = (sum - ans) % mod; n   ans < 0 ? ans += mod : 0; n   printf( %d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n bool vis[200005]; n vector<int> v[200005], ak, d1, dn; n vector<int> bfs(int n) { n   queue<int> q; n   vector<int> dis(200005); n   q.push(n); n   vis[n] = true; n   dis[n] = 0; n   while (!q.empty()) { n     int x = q.front(); n     q.pop(); n     for (int i = 0; i < v[x].size(); i++) { n       int u = v[x][i]; n       if (vis[u] == false) { n         vis[u] = true; n         dis[u] = dis[x] + 1; n         q.push(u); n       } n     } n   } n   return dis; n } n bool cmp(int x, int y) { return d1[x] < d1[y]; } n int main() { n   int n, m, k, n1, n2, sp, ans = 0; n   scanf( %d %d %d , &n, &m, &k); n   for (int i = 0; i < k; i++) { n     scanf( %d , &sp); n     ak.push_back(sp); n   } n   for (int i = 0; i < m; i++) { n     scanf( %d %d , &n1, &n2); n     v[n1].push_back(n2); n     v[n2].push_back(n1); n   } n   d1 = bfs(1); n   memset(vis, false, sizeof(vis)); n   dn = bfs(n); n   sort(ak.begin(), ak.end(), cmp); n   for (int i = 1; i < ak.size(); i++) { n     int x = ak[i - 1]; n     int y = ak[i]; n     ans = max(ans, d1[x] + 1 + dn[y]); n   } n   ans = min(ans, d1[n]); n   printf( %d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAX_N = 1050; n pair<int, int> a[MAX_N]; n int cnt[MAX_N]; n int sub[MAX_N]; n map<int, int> ids; n int inds[MAX_N]; n int id = 0; n int countplz(int f0, int f1) { n   memset((sub), 0, sizeof(sub)); n   ++sub[a[f0].second]; n   ++sub[a[f1].second]; n   int ans = 2; n   while (true) { n     int fnval = a[f0].first + a[f1].first; n     auto it = ids.find(fnval); n     if (it == ids.end()) { n       return ans; n     } n     int fnid = it->second; n     int fn = inds[fnid]; n     int rest = cnt[fnid] - sub[fnid]; n     if (rest <= 0) { n       return ans; n     } n     ++ans; n     sub[fnid]++; n     f0 = f1; n     f1 = fn; n   } n } n int main() { n   int n; n   cin >> n; n   for (int i = 0; i < n; ++i) { n     int x; n     cin >> x; n     if (ids.count(x) == 0) { n       ids[x] = id; n       inds[id] = i; n       ++id; n     } n     a[i] = make_pair(x, ids[x]); n     cnt[ids[x]]++; n   } n   int maxs = 2; n   if (ids.count(0) > 0) { n     maxs = max(maxs, cnt[ids[0]]); n   } n   for (int i = 0; i < n; ++i) { n     for (int j = 0; j < n; ++j) { n       if (i == j || (a[i].first == a[j].first && a[i].first == 0)) { n         continue; n       } n       maxs = max(maxs, countplz(i, j)); n     } n   } n   cout << maxs << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n class TaskB { n  public: n   long long dist(pair<long long, long long> f, pair<long long, long long> s) { n     return (f.first - s.first) * (f.first - s.first) + n            (f.second - s.second) * (f.second - s.second); n   } n   void solve(std::istream& cin, std::ostream& cout) { n     ios_base::sync_with_stdio(0); n     cin.tie(0); n     cout.tie(0); n     vector<pair<long long, long long> > v(3); n     set<pair<long long, long long> > st; n     for (long long i = 0; i < 3; i++) { n       cin >> v[i].first >> v[i].second; n     } n     vector<pair<long long, long long> > ans; n     if (1 || dist(v[1], v[2]) <= dist(v[0], v[1]) + dist(v[0], v[2])) { n       ans.push_back({v[1].first + v[2].first - v[0].first, n                      v[1].second + v[2].second - v[0].second}); n     } n     if (1 || dist(v[0], v[2]) <= dist(v[2], v[1]) + dist(v[1], v[0])) { n       ans.push_back({v[0].first + v[2].first - v[1].first, n                      v[0].second + v[2].second - v[1].second}); n     } n     if (1 || dist(v[1], v[0]) <= dist(v[2], v[1]) + dist(v[0], v[2])) { n       ans.push_back({v[1].first + v[0].first - v[2].first, n                      v[1].second + v[0].second - v[2].second}); n     } n     cout << ans.size() << endl; n     for (long long i = 0; i < ans.size(); i++) n       cout << ans[i].first <<     << ans[i].second << endl; n   } n }; n int main() { n   TaskB solver; n   std::istream& in(std::cin); n   std::ostream& out(std::cout); n   solver.solve(in, out); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n; n string a, b; n set<int> A, B; n queue<int> qA, qB; n map<char, queue<int> > q; n vector<pair<int, int> > resp; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cin >> n >> a >> b; n   for (int i = 0; i < n; ++i) { n     if (a[i] ==  ? ) n       qA.push(i); n     else { n       q[a[i]].push(i); n       A.insert(i); n     } n   } n   for (int i = 0; i < n; ++i) { n     if (b[i] ==  ? ) n       qB.push(i); n     else n       B.insert(i); n   } n   for (int i = 0; i < n; ++i) { n     if (!q[b[i]].empty()) { n       resp.push_back({q[b[i]].front(), i}); n       B.erase(i); n       A.erase(q[b[i]].front()); n       q[b[i]].pop(); n     } n   } n   while (!qA.empty() && !B.empty()) { n     auto it = B.begin(); n     resp.push_back({qA.front(), *it}); n     qA.pop(); n     B.erase(it); n   } n   while (!qB.empty() && !A.empty()) { n     auto it = A.begin(); n     resp.push_back({*it, qB.front()}); n     qB.pop(); n     A.erase(it); n   } n   while (!qA.empty() && !qB.empty()) { n     resp.push_back({qA.front(), qB.front()}); n     qA.pop(); n     qB.pop(); n   } n   cout << (int)resp.size() <<   n ; n   for (auto x : resp) cout << x.first + 1 <<     << x.second + 1 <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios::sync_with_stdio(0); n   int n; n   double p[111]; n   cin >> n; n   for (int i = 0; i < n; i++) cin >> p[i]; n   sort(p, p + n); n   double ans = p[n - 1], notAns = 1 - p[n - 1]; n   for (int i = n - 2; i >= 0 && ans < notAns; i--) { n     ans = ans * (1 - p[i]) + notAns * p[i]; n     notAns *= (1 - p[i]); n   } n   printf( %.12lf n , ans); n } n


#include <bits/stdc++.h> n using namespace std; n const long long inf = 1e18; n long long first, T, mt; n long long t[2], p[2], a[2]; n long long get(long long ile, long long czas) { n   if (ile == 0) { n     return 0; n   } n   if (min(ile * mt, ile * t[1]) > czas) { n     return inf; n   } n   if (ile * mt <= czas) { n     return 0; n   } n   long long st = 0, en = ile, mid = (st + en) / 2, res = inf; n   while (st <= en) { n     long long il = min(ile, mid * a[1]); n     if (il * t[1] + (ile - il) * mt <= czas) { n       res = min(res, mid); n       en = mid - 1; n     } else { n       st = mid + 1; n     } n     mid = (st + en) / 2; n   } n   return res * p[1]; n } n int main() { n   scanf( %lld%lld%lld , &first, &T, &mt); n   for (int i = 0; i < 2; i++) { n     scanf( %lld%lld%lld , a + i, t + i, p + i); n   } n   if (t[0] > t[1]) { n     swap(a[0], a[1]); n     swap(t[0], t[1]); n     swap(p[0], p[1]); n   } n   if (min(mt, t[0]) * first > T) { n     printf( -1 n ); n     return 0; n   } n   if (mt * first <= T) { n     printf( 0 n ); n     return 0; n   } n   long long res = inf; n   for (long long i = 0; (i - 1) * a[0] * t[0] + 1 <= T; i++) { n     long long ile = min(first, a[0] * i); n     long long cena = i * p[0] + get(first - ile, T - ile * t[0]); n     res = min(res, cena); n   } n   printf( %lld n , res); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int t, T, i, n, k, d, ans, val; n   cin >> T; n   vector<int> id(pow(10, 6) + 1, 0); n   for (t = 0; t < T; t++) { n     cin >> n >> k >> d; n     vector<int> v(n); n     for (i = 0; i < n; i++) cin >> v[i]; n     ans = n; n     val = 0; n     for (i = 0; i < d; i++) { n       id[v[i]]++; n       if (id[v[i]] == 1) val++; n     } n     ans = val; n     for (i = d; i < n; i++) { n       id[v[i - d]]--; n       if (id[v[i - d]] == 0) val--; n       id[v[i]]++; n       if (id[v[i]] == 1) val++; n       ans = min(val, ans); n     } n     for (i = n - d; i < n; i++) id[v[i]] = 0; n     cout << ans << endl; n   } n } n


#include <bits/stdc++.h> n const int dx[] = {1, 0, -1, 0}; n const int dy[] = {0, 1, 0, -1}; n using namespace std; n const int MAXN = 55; n int n; n int a[2][MAXN]; n int asum[2][MAXN]; n int b[MAXN]; n int main() { n   cin.tie(0); n   ios::sync_with_stdio(false); n   cin >> n; n   for (int j = 0; j < 2; j++) n     for (int i = 0; i < n - 1; i++) cin >> a[j][i]; n   for (int i = 0; i < n; i++) cin >> b[i]; n   for (int i = 1; i < n; i++) asum[0][i] = asum[0][i - 1] + a[0][i - 1]; n   for (int i = n - 2; i >= 0; i--) asum[1][i] = asum[1][i + 1] + a[1][i]; n   vector<int> length; n   for (int i = 0; i < n; i++) length.push_back(asum[0][i] + asum[1][i] + b[i]); n   sort(length.begin(), length.end()); n   cout << length[0] + length[1] << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 100010; n int num[N], tot; n int main() { n   int n, a1 = 2, a2 = 1, ans = 1; n   cin >> n; n   while (a1 - a2 <= n) { n     if (n % (a1 - a2) == 0) ans = a1 - a2; n     a1 *= 4; n     a2 *= 2; n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int k; n int values[107]; n int n = 0; n int ans[107][107]; n int cmb[107]; n int main() { n   int i, j, base; n   cin >> k; n   for (i = 3; i <= 100; i++) values[i] = i * (i - 1) * (i - 2) / 6; n   for (i = 2; i <= 100; i++) cmb[i] = i * (i - 1) / 2; n   i = 3; n   while (values[i] < k) i++; n   if (values[i] > k) i--; n   k -= values[i]; n   n = i; n   for (i = 1; i <= n; i++) n     for (j = 1; j <= n; j++) n       if (i != j) ans[i][j] = 1; n   while (k) { n     for (i = 2; cmb[i] < k; i++) n       ; n     if (cmb[i] > k) i--; n     k -= cmb[i]; n     n++; n     for (j = 1; j <= i; j++) ans[n][j] = ans[j][n] = 1; n   } n   cout << n <<   n ; n   for (i = 1; i <= n; i++) { n     for (j = 1; j <= n; j++) cout << ans[i][j]; n     cout <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct treenode { n   long long a, b, c; n   treenode(long long a, long long b, long long c) : a(a), b(b), c(c) {} n }; n int n, mm, s[111111], m[111111], r[111111], zero[111111], *add; n vector<treenode> tree1[4 * 111111], tree2[4 * 111111], *tree; n set<pair<int, int> > seg; n void mergetrees(vector<treenode> &v1, vector<treenode> &v2, n                 vector<treenode> &res) { n   for (int i = 0, j = 0;;) { n     if (v1[i].a > v2[j].a) n       res.push_back(treenode(v1[i].a, n                              v1[i].b + v2[j].b + v2[j].c * (v1[i].a - v2[j].a), n                              v2[j].c + v1[i].c)); n     else n       res.push_back(treenode(v2[j].a, n                              v2[j].b + v1[i].b + v1[i].c * (v2[j].a - v1[i].a), n                              v1[i].c + v2[j].c)); n     if (v1[i].a == 2000000000) break; n     if (v1[i + 1].a > v2[j + 1].a) n       j++; n     else if (v1[i + 1].a < v2[j + 1].a) n       i++; n     else n       i++, j++; n   } n } n void build(int node, int vl, int vr) { n   if (vl == vr) { n     tree[node].push_back(treenode(0, add[vl], r[vl])); n     if (r[vl]) n       tree[node].push_back(treenode((m[vl] - add[vl]) / r[vl] + 1, m[vl], 0)); n     tree[node].push_back(treenode(2000000000, r[vl] ? m[vl] : add[vl], 0)); n   } else { n     build(node * 2, vl, (vl + vr) / 2); n     build(node * 2 + 1, (vl + vr) / 2 + 1, vr); n     mergetrees(tree[node * 2], tree[node * 2 + 1], tree[node]); n   } n } n long long getsum(int t, int node, int vl, int vr, int l, int r) { n   if (l > vr || r < vl) n     return 0; n   else if (vl >= l && vr <= r) { n     vector<treenode> &v = tree[node]; n     int l = 0, r = (int)v.size() - 1; n     while (l != r) { n       if (v[(l + r) / 2 + 1].a > t) n         r = (l + r) / 2; n       else n         l = (l + r) / 2 + 1; n     } n     return tree[node][l].b + tree[node][l].c * (t - tree[node][l].a); n   } else { n     return getsum(t, node * 2, vl, (vl + vr) / 2, l, r) + n            getsum(t, node * 2 + 1, (vl + vr) / 2 + 1, vr, l, r); n   } n } n inline long long getdif(int tnow, int tprev, int l, int r) { n   if (tprev == -1) n     tree = tree2; n   else n     tree = tree1; n   return getsum((tprev == -1) ? tnow : tnow - tprev, 1, 0, n - 1, l - 1, r - 1); n } n int main() { n   cin >> n; n   for (int i = 0; i < n; i++) scanf( %d%d%d , &s[i], &m[i], &r[i]); n   cin >> mm; n   tree = tree1; n   add = zero; n   build(1, 0, n - 1); n   tree = tree2; n   add = s; n   build(1, 0, n - 1); n   seg.insert(make_pair(1, -1)); n   seg.insert(make_pair(n + 1, -1)); n   for (int i = 0; i < mm; i++) { n     int tt, ll, rr; n     scanf( %d%d%d , &tt, &ll, &rr); n     auto j = seg.upper_bound(make_pair(ll, 2000000000)); n     j--; n     long long ans = 0; n     bool flag = true; n     for (; j->first <= rr;) { n       auto next = j; n       next++; n       ans += getdif(tt, j->second, max(j->first, ll), min(next->first - 1, rr)); n       int l1 = j->first, t1 = j->second; n       seg.erase(j); n       if (l1 < ll) seg.insert(make_pair(l1, t1)); n       if (flag) seg.insert(make_pair(ll, tt)), flag = false; n       if (next->first - 1 > rr) seg.insert(make_pair(rr + 1, t1)); n       j = next; n     } n     printf( %I64d n , ans); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n using ll = long long; n using ld = long double; n using ii = pair<ll, ll>; n using vi = vector<ll>; n using vb = vector<bool>; n using vvi = vector<vi>; n using vii = vector<ii>; n using vvii = vector<vii>; n constexpr int INF = 2000000000; n constexpr ll LLINF = 9000000000000000000; n struct ConvexHullSet { n   struct Line { n     ld a, b; n     mutable ld x; n     bool type; n     bool operator<(const Line &rhs) const { n       return type || rhs.type ? x < rhs.x : a < rhs.a; n     } n     ld intersect(const Line &rhs) const { n       return ld(b - rhs.b) / ld(rhs.a - a); n     } n   }; n   set<Line> lines; n   static constexpr ld MAX = std::numeric_limits<ld>::max(); n   ld query(ld x) { n     auto it = lines.lower_bound(Line{0.0, 0.0, x, true}); n     return (it != lines.end() ? it->a * x + it->b : -1e300); n   } n   void adjust(set<Line>::iterator it) { n     if (it != lines.end()) n       it->x = next(it) != lines.end() ? it->intersect(*next(it)) : MAX; n     if (it != lines.begin()) n       prev(it)->x = it != lines.end() ? it->intersect(*prev(it)) : MAX; n   } n   void insert(ld a, ld b) { n     Line ln = Line{a, b, 0.0, false}; n     auto it1 = lines.lower_bound(ln); n     if (it1 != lines.end() && it1->a == a) { n       if (it1->b >= b) return; n       it1 = lines.erase(it1); n       adjust(it1); n     } n     ln.x = it1 != lines.end() ? ln.intersect(*it1) : MAX; n     while (it1 != lines.end() && ln.x >= it1->x) { n       it1 = lines.erase(it1); n       ln.x = it1 != lines.end() ? it1->intersect(ln) : MAX; n       adjust(it1); n     } n     while (it1 != lines.begin()) { n       --it1; n       ld nx = it1->intersect(ln); n       if (nx >= it1->x) return; n       if (it1 != lines.begin() && prev(it1)->x >= nx) { n         it1 = lines.erase(it1); n         adjust(it1); n       } else n         break; n     } n     it1 = lines.insert(ln).first; n     adjust(it1); n   } n }; n constexpr int L = 0, R = 1000000; n vector<ld> solve(vii &pairs, int s) { n   vector<ld> d(R - L + 1, 1e300); n   sort(pairs.begin(), pairs.end()); n   ConvexHullSet chs; n   for (int i = 0, p = L; p <= R; ++p) { n     while (i < (int)pairs.size() && pairs[i].first == p) { n       int x = pairs[i].first, v = pairs[i].second; n       ld a = 1.0 / ld(s - v) - ld(v) / (ld(s - v) * ld(s + v)); n       ld b = -ld(x) / ld(s - v) + ld(x) / ld(s + v) + n              ld(v) * ld(x) / (ld(s - v) * ld(s + v)); n       chs.insert(-a, -b); n       ++i; n     } n     d[p] = -chs.query(p); n   } n   return d; n } n int main() { n   ios::sync_with_stdio(false); n   cin.tie(NULL); n   int n, s; n   cin >> n >> s; n   vi x(n, 0), v(n, 0), t(n, 0); n   ld lr = 1e300, rr = 1e300; n   int minl = R + 1, maxr = L - 1; n   for (int i = 0; i < n; ++i) { n     cin >> x[i] >> v[i] >> t[i]; n     if (t[i] == 1) { n       lr = min(lr, ld(x[i]) / ld(v[i])); n       if (x[i] < minl) minl = x[i]; n     } else { n       rr = min(rr, ld(R - x[i]) / ld(v[i])); n       if (x[i] > maxr) maxr = x[i]; n     } n   } n   cerr <<  lr =   << lr <<  , rr =   << rr << endl; n   vector<ld> lp, rp; n   { n     vii pairs[2]; n     for (int i = 0; i < n; ++i) pairs[t[i] - 1].push_back({x[i], v[i]}); n     for (ii &pr : pairs[1]) pr.first = R - pr.first; n     lp = solve(pairs[0], s); n     rp = solve(pairs[1], s); n     reverse(rp.begin(), rp.end()); n   } n   ld ans = max(lr, rr); n   for (int p = L; p <= R; ++p) n     ans = min(ans, max(min(lr, lp[p]), min(rr, rp[p]))); n   printf( %.10lf n , double(ans)); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(0), cin.tie(0); n   int n, m; n   cin >> n >> m; n   vector<vector<int>> adj(n); n   for (int i = 0, u, v; i < m; ++i) { n     cin >> u >> v; n     --u, --v; n     adj[u].push_back(v); n     adj[v].push_back(u); n   } n   set<int> go; n   queue<int> q; n   for (int i = 0; i < n; ++i) go.insert(i); n   int ans = 0; n   for (int x = 0; x < n; ++x) n     if (go.find(x) != go.end()) { n       ++ans; n       go.erase(x); n       q.push(x); n       while (!q.empty()) { n         int u = q.front(); n         q.pop(); n         vector<int> add; n         for (auto v : adj[u]) n           if (go.find(v) != go.end()) { n             go.erase(v); n             add.push_back(v); n           } n         for (auto v : go) q.push(v); n         go.clear(); n         for (auto v : add) go.insert(v); n       } n     } n   cout << ans - 1 <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct edge { n   int v, w; n   edge* next; n }; n const int N = 100010; n int n, p, ordcnt; n bool vis[N], cut[N]; n int ord[N], sum[N], mex[N], q[N], dep[N], digit[N], rdigit[N], rprod[N], n     prod[N]; n edge* g[N]; n void add_edge(int u, int v, int w); n void calc_order(int u); n void calc_sum(int u); n void dfs(int u); n long long calc(int u); n int rev(int a); n void ex_gcd(int a, int b, int& x, int& y); n int main() { n   long long ans = 0; n   scanf( %d%d , &n, &p); n   for (int rep = 1; rep < n; ++rep) { n     int u, v, w; n     scanf( %d%d%d , &u, &v, &w); n     add_edge(u, v, w); n     add_edge(v, u, w); n   } n   prod[0] = 1; n   rprod[0] = 1; n   rprod[1] = rev(10); n   for (int i = 1; i <= n; ++i) prod[i] = 10LL * prod[i - 1] % p; n   for (int i = 2; i <= n; ++i) n     rprod[i] = (long long)rprod[1] * rprod[i - 1] % p; n   calc_order(0); n   memset(cut, 0, sizeof(cut)); n   for (int i = 0; i < n; ++i) { n     int u = ord[i]; n     dep[u] = 0; n     digit[u] = 0; n     rdigit[u] = 0; n     dfs(u); n     ans += calc(u); n     cut[u] = true; n     for (edge* p = g[u]; p; p = p->next) n       if (!cut[p->v]) ans -= calc(p->v); n     ans -= 1; n     ; n   } n   printf( %lld n , ans); n   return 0; n } n int rev(int a) { n   int x, y; n   ex_gcd(a, p, x, y); n   if (x < 0) x += p; n   return x; n } n void ex_gcd(int a, int b, int& x, int& y) { n   if (b == 0) { n     x = 1; n     y = 0; n     return; n   } n   ex_gcd(b, a % b, y, x); n   y -= a / b * x; n } n long long calc(int u) { n   int ql = 0, qr = 1; n   long long ret = 0; n   map<int, int> mp; n   q[0] = u; n   while (ql < qr) { n     int v = q[ql++]; n     vis[v] = true; n     map<int, int>::iterator it = mp.find(rdigit[v]); n     if (it != mp.end()) n       ++it->second; n     else n       mp.insert(make_pair(rdigit[v], 1)); n     for (edge* p = g[v]; p; p = p->next) n       if (!vis[p->v] && !cut[p->v]) q[qr++] = p->v; n   } n   for (int i = 0; i < qr; ++i) vis[q[i]] = false; n   for (int i = 0; i < qr; ++i) { n     int key = (long long)-digit[q[i]] * rprod[dep[q[i]]] % p; n     if (key < 0) key += p; n     map<int, int>::iterator it = mp.find(key); n     if (it != mp.end()) { n       ; n       ret += it->second; n     } n   } n   return ret; n } n void dfs(int u) { n   vis[u] = true; n   for (edge* p = g[u]; p; p = p->next) n     if (!vis[p->v] && !cut[p->v]) { n       dep[p->v] = dep[u] + 1; n       rdigit[p->v] = ((long long)prod[dep[u]] * p->w + rdigit[u]) % ::p; n       digit[p->v] = (digit[u] * 10LL + p->w) % ::p; n       dfs(p->v); n     } n   vis[u] = false; n } n void calc_order(int u) { n   calc_sum(u); n   int s = sum[u], ql = 0, qr = 1; n   q[0] = u; n   while (ql < qr) { n     int v = q[ql++]; n     vis[v] = true; n     mex[v] = max(mex[v], s - sum[v]); n     if (mex[v] < mex[u]) u = v; n     for (edge* p = g[v]; p; p = p->next) n       if (!vis[p->v] && !cut[p->v]) q[qr++] = p->v; n   } n   for (int i = 0; i < qr; ++i) vis[q[i]] = false; n   ord[ordcnt++] = u; n   cut[u] = true; n   for (edge* p = g[u]; p; p = p->next) n     if (!cut[p->v]) calc_order(p->v); n } n void calc_sum(int u) { n   vis[u] = true; n   mex[u] = 0; n   sum[u] = 1; n   for (edge* p = g[u]; p; p = p->next) n     if (!vis[p->v] && !cut[p->v]) { n       calc_sum(p->v); n       sum[u] += sum[p->v]; n       mex[u] = max(mex[u], sum[p->v]); n     } n   vis[u] = false; n } n void add_edge(int u, int v, int w) { n   static edge pool[N * 2]; n   static edge* p = pool; n   ++p; n   p->v = v; n   p->w = w; n   p->next = g[u]; n   g[u] = p; n } n


#include <bits/stdc++.h> n using namespace std; n   n #define fastIO ios_base::sync_with_stdio(0); cin.tie(NULL); cout.tie(NULL); n #define endl   n  n #define lli long long int n #define ld long double n #define p_b push_back n #define m_p make_pair n #define fs first n #define sc second n #define sz(x) ((lli)x.size()) n #define all(x) x.begin(),x.end() n const lli mod = 1e9 + 7; n const lli N = 1e6 + 5; n /*****************************************************/ n lli t,n,m,a[N]={0},target; n bool dp[105][200005],vis[105][200005]; n lli F(lli idx,lli ss) n { n     if(vis[idx][ss]) n         return dp[idx][ss]; n     vis[idx][ss]=true; n     if(idx>n) n     { n         if(ss==target) n             return dp[idx][ss]=true; n         return dp[idx][ss]=false; n     } n     bool ret=F(idx+1,ss),ret1=F(idx+1,ss+a[idx]); n     if(ret || ret1) n         return dp[idx][ss]=true; n     return dp[idx][ss]=false; n } n int main() n { n     fastIO n     lli i,j,k,x,y; n     lli ss=0; n     cin>>n; n     for(i=1;i<=n;i++) n     { n         cin>>a[i]; n         ss+=a[i]; n     } n     if(ss%2!=0) n     { n         cout<< 0 ; n         return 0; n     } n     target=ss/2; n     memset(vis,false,sizeof vis); n     memset(dp,false,sizeof dp); n     if(F(1,0)==false) n     { n         cout<< 0 ; n         return 0; n     } n     while(1) n     { n         for(i=1;i<=n;i++) n         { n             if(a[i]%2!=0) n             { n                 cout<< 1 n <<i; n                 return 0; n             } n             a[i]/=2; n         }  n     } n      n     // cerr<<   n  <<  Time elapsed :  << clock() * 1000.0 / CLOCKS_PER_SEC <<   ms n  ; n     return 0; n }


#include <bits/stdc++.h> n using namespace std; n int time_to_minute(int h, int m) { return h * 60 + m; } n void minute_to_time(int t, int& h, int& m) { n   h = t / 60; n   m = t % 60; n } n int main() { n   ios_base::sync_with_stdio(false); n   int h, m, a; n   cin >> h; n   cin.ignore(1); n   cin >> m >> a; n   minute_to_time((time_to_minute(h, m) + a) % (24 * 60), h, m); n   cout << (h < 10 ?  0  :   ) << h <<  :  << (m < 10 ?  0  :   ) << m; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 100010; n int n, BIT[N]; n void update(int idx, int val) { n   while (idx <= n) { n     BIT[idx] = max(val, BIT[idx]); n     idx += idx & -idx; n   } n } n int get(int idx) { n   int ret = 0; n   while (idx > 0) { n     ret = max(BIT[idx], ret); n     idx -= idx & -idx; n   } n   return ret; n } n int main() { n   scanf( %d , &n); n   int ans = 0; n   for (int i = 0, x; i < n; ++i) { n     scanf( %d , &x); n     update(x, get(x) + 1); n   } n   printf( %d n , get(n)); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int iinf = 1e9 + 7; n const long long linf = 1ll << 60; n const double dinf = 1e60; n template <typename T> n inline void scf(T &x) { n   bool f = 0; n   x = 0; n   char c = getchar(); n   while ((c <  0  || c >  9 ) && c !=  - ) c = getchar(); n   if (c ==  - ) { n     f = 1; n     c = getchar(); n   } n   while (c >=  0  && c <=  9 ) { n     x = x * 10 + c -  0 ; n     c = getchar(); n   } n   if (f) x = -x; n   return; n } n template <typename T1, typename T2> n void scf(T1 &x, T2 &y) { n   scf(x); n   return scf(y); n } n template <typename T1, typename T2, typename T3> n void scf(T1 &x, T2 &y, T3 &z) { n   scf(x); n   scf(y); n   return scf(z); n } n template <typename T1, typename T2, typename T3, typename T4> n void scf(T1 &x, T2 &y, T3 &z, T4 &w) { n   scf(x); n   scf(y); n   scf(z); n   return scf(w); n } n inline char mygetchar() { n   char c = getchar(); n   while (c ==     || c ==   n ) c = getchar(); n   return c; n } n template <typename T> n void chkmax(T &x, const T &y) { n   if (y > x) x = y; n   return; n } n template <typename T> n void chkmin(T &x, const T &y) { n   if (y < x) x = y; n   return; n } n const int N = 510; n const int M = 110; n int n, m, k; n double A[N][N + N]; n double w[M][M], p[N][N]; n bool f[N]; n vector<int> trap; n double beg[N]; n void calc_w() { n   for (int i = (1); i <= (n); ++i) { n     A[i][i] = 1.0; n     if (!f[i]) n       for (int j = (1); j <= (n); ++j) n         if (i != j) A[i][j] = -p[i][j]; n     A[i][i + n] = 1.0; n   } n   for (int row = 1, col = 1; row <= n; ++row) { n     int pivot = row; n     for (int i = (row + 1); i <= (n); ++i) n       if (fabs(A[i][col]) > fabs(A[pivot][col])) pivot = i; n     if (fabs(A[pivot][col]) <= 1e-6) continue; n     swap(A[pivot], A[row]); n     double foo = A[row][col]; n     for (int i = (1); i <= (n + n); ++i) A[row][i] /= foo; n     for (int i = (1); i <= (n); ++i) n       if (i != row) { n         foo = A[i][col]; n         for (int j = (1); j <= (n + n); ++j) A[i][j] -= foo * A[row][j]; n       } n     ++col; n   } n   for (int i = 0; i < (m); ++i) n     for (int j = 0; j < (m); ++j) { n       int u = trap[i], v = trap[j]; n       for (int k = (1); k <= (n); ++k) w[i][j] += p[u][k] * A[k][v + n]; n     } n   for (int i = 0; i < (m); ++i) beg[i] = A[1][trap[i] + n]; n   return; n } n void TZL() { n   static int g[N][N], deg[N]; n   memset((g), (0), sizeof((g))); n   memset((deg), (0), sizeof((deg))); n   scf(n, m, k); n   for (int i = (1); i <= (n); ++i) { n     scf(f[i]); n     if (f[i]) trap.push_back(i); n   } n   for (int i = 0; i < (m); ++i) { n     int u, v; n     scf(u, v); n     g[u][v]++; n     g[v][u]++; n     deg[u]++; n     deg[v]++; n   } n   m = (int)trap.size(); n   for (int i = (1); i <= (n); ++i) n     for (int j = (1); j <= (n); ++j) p[i][j] = (1.0 * g[i][j]) / (1.0 * deg[i]); n   return calc_w(); n } n struct mat { n   double num[M][M]; n   mat() { memset((num), (0), sizeof((num))); } n   mat(int x) { n     memset((num), (0), sizeof((num))); n     for (int i = 0; i < (M); ++i) num[i][i] = x; n   } n   mat(double a[][M]) { n     for (int i = 0; i < (M); ++i) n       for (int j = 0; j < (M); ++j) num[i][j] = a[i][j]; n   } n   double &operator()(int i, int j) { return num[i][j]; } n   double operator()(int i, int j) const { return num[i][j]; } n   friend mat operator*(const mat &a, const mat &b) { n     mat c; n     for (int i = 0; i < (M); ++i) n       for (int j = 0; j < (M); ++j) n         for (int k = 0; k < (M); ++k) c(i, j) += a(i, k) * b(k, j); n     return c; n   } n } g; n mat kiss_me(mat a, int n) { n   mat ret = mat(1); n   while (n) { n     if (n & 1) ret = ret * a; n     a = a * a; n     n >>= 1; n   } n   return ret; n } n void RANK1() { n   g = mat(w); n   g = kiss_me(g, k - 2); n   double ans = 0.0; n   for (int i = 0; i < (m); ++i) ans += beg[i] * g(i, m - 1); n   printf( %.12f n , ans); n   return; n } n int main() { n   TZL(); n   RANK1(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m; n long long dis[100010]; n vector<int> mom[100010]; n vector<pair<int, int> > vertex[100010]; n set<int> q; n void readin() { n   scanf( %d%d , &n, &m); n   int u, v, c; n   for (int i = 0; i < m; ++i) { n     scanf( %d%d%d , &u, &v, &c); n     vertex[u].push_back(make_pair(v, c)); n     vertex[v].push_back(make_pair(u, c)); n   } n   int cnt; n   for (int i = 1; i <= n; ++i) { n     scanf( %d , &cnt); n     while (cnt--) { n       scanf( %d , &v); n       mom[i].push_back(v); n     } n   } n } n void spfa() { n   int u, v, c, nt, sz; n   vector<int>::iterator low; n   memset(dis, -1, sizeof(dis)); n   while (!q.empty()) q.clear(); n   q.insert(1); n   dis[1] = 0; n   while (!q.empty()) { n     u = *q.begin(); n     q.erase(u); n     if (u == n) continue; n     nt = dis[u]; n     low = lower_bound(mom[u].begin(), mom[u].end(), nt); n     for (; low != mom[u].end(); low++) { n       if (*low == nt) n         nt++; n       else if (*low > nt) n         break; n     } n     sz = vertex[u].size(); n     for (int i = 0; i < sz; ++i) { n       v = vertex[u][i].first; n       c = vertex[u][i].second; n       if (dis[v] == -1 || dis[v] > nt + c) { n         dis[v] = nt + c; n         q.insert(v); n       } n     } n   } n } n int main() { n   readin(); n   spfa(); n   printf( %I64d , dis[n]); n } n


#include <bits/stdc++.h> n using namespace std; n static const int INF = 500000000; n template <class T> n void debug(T a, T b) { n   for (; a != b; ++a) cerr << *a <<    ; n   cerr << endl; n } n int n; n pair<pair<int, int>, int> es[5005]; n vector<pair<int, int> > g[5005]; n int size[5005]; n int cut[5005]; n int prep(int v, int p) { n   size[v] = 1; n   for (int i = 0; i < g[v].size(); ++i) { n     int to = g[v][i].first; n     if (to == p || cut[to]) continue; n     size[v] += prep(to, v); n   } n   return size[v]; n } n int type; n int cost[2], root[2], all[2]; n void dfs(int v, int p) { n   int maxi = 0; n   for (int i = 0; i < g[v].size(); ++i) { n     int to = g[v][i].first; n     if (to == p || cut[to]) continue; n     maxi = max(maxi, size[to]); n     dfs(to, v); n   } n   if (max(maxi, all[type] - size[v]) < cost[type]) { n     cost[type] = max(maxi, all[type] - size[v]); n     root[type] = v; n   } n } n long long int tot; n int dfs2(int v, int p) { n   size[v] = 1; n   for (int i = 0; i < g[v].size(); ++i) { n     int to = g[v][i].first; n     if (to == p || cut[to]) continue; n     size[v] += dfs2(to, v); n     tot += size[to] * (all[type] - size[to]) * (long long int)g[v][i].second; n     tot += size[to] * (long long int)all[type ^ 1] * g[v][i].second; n   } n   return size[v]; n } n int main() { n   scanf( %d , &n); n   for (int i = 0; i < n - 1; ++i) { n     int a, b, c; n     scanf( %d%d%d , &a, &b, &c); n     --a; n     --b; n     g[a].push_back(make_pair(b, c)); n     g[b].push_back(make_pair(a, c)); n     es[i] = make_pair(make_pair(a, b), c); n   } n   long long int ans = 1e18; n   for (int i = 0; i < n - 1; ++i) { n     int a = es[i].first.first, b = es[i].first.second; n     tot = 0; n     cost[0] = cost[1] = INF; n     type = 0; n     cut[b] = 1; n     all[0] = prep(a, b); n     all[1] = n - all[0]; n     dfs(a, b); n     dfs2(root[0], -1); n     type = 1; n     cut[b] = 0; n     cut[a] = 1; n     all[1] = prep(b, a); n     dfs(b, a); n     dfs2(root[1], -1); n     cut[a] = 0; n     tot += all[0] * all[1] * (long long int)es[i].second; n     ans = min(ans, tot); n   } n   cout << ans << endl; 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(0); n   int t; n   cin >> t; n   while (t--) { n     vector<pair<int, int>> st, re; n     int a, b; n     int n; n     cin >> n; n     for (int i = 0; i < n; i++) { n       cin >> a >> b; n       st.emplace_back(a, b); n       re.emplace_back(b, a); n     } n     sort(st.begin(), st.end()); n     sort(re.begin(), re.end()); n     cout << max(st[n - 1].first - re[0].first, 0) <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long inf = 1000000007; n long long a[1100][1100]; n long long b[1100][1100]; n long long A(int n, int k); n long long B(int n, int k); n long long A(int n, int k) { n   if (a[n][k] != -1) { n     return a[n][k]; n   } n   if (n < k) { n     return a[n][k] = 0; n   } n   if (n == 1) { n     return a[n][k] = (k == 0); n   } n   if (n == 2) { n     return a[n][k] = (k != 1); n   } n   long long ret = A(n - 1, k); n   ret += A(n - 1, k + 1) * (k + 1) - B(n - 1, k + 1) + B(n - 1, k) - n          B(n - 1, k + 1) + B(n - 1, k); n   ret += A(n - 1, k) * (n - 1 - k); n   ret -= A(n - 1, k) - A(n - 2, k) - B(n - 1, k); n   if (k > 0) { n     ret += A(n - 1, k - 1) - A(n - 2, k - 1) - B(n - 1, k - 1); n   } n   ret -= A(n - 1, k) - A(n - 2, k) - B(n - 1, k); n   if (k > 0) { n     ret += A(n - 1, k - 1) - A(n - 2, k - 1) - B(n - 1, k - 1); n   } n   ret -= A(n - 2, k); n   if (k > 1) { n     ret += A(n - 2, k - 2); n   } n   return a[n][k] = (ret % inf + inf) % inf; n } n long long B(int n, int k) { n   if (b[n][k] != -1) { n     return b[n][k]; n   } n   if (n < k) { n     return (b[n][k] = 0); n   } n   if (n == 1) { n     return b[n][k] = 0; n   } n   if (n == 2) { n     return b[n][k] = (k == 2); n   } n   if (k == 0) { n     return b[n][k] = 0; n   } n   if (k == 1) { n     return b[n][k] = n                ((A(n - 1, 0) - A(n - 2, 0) + B(n - 1, 1)) % inf + inf) % inf; n   } n   return b[n][k] = (A(n - 2, k - 2) + B(n - 1, k) + A(n - 1, k - 1) - n                     B(n - 1, k - 1) - A(n - 2, k - 1)) % n                    inf; n } n int main() { n   memset(a, -1, sizeof(a)); n   memset(b, -1, sizeof(b)); n   int n, k; n   cin >> n >> k; n   cout << A(n, k) << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const double eps = 1e-8; n const double pi = acos(-1.0); n struct Q { n   int a, b, c; n   Q(int a, int b, int c) : a(a), b(b), c(c) {} n   bool operator<(const Q& o) const { n     if (a == o.a) return c < o.c; n     return a < o.a; n   } n }; n vector<pair<int, int> > ans; n set<Q> q; n int in[100000]; n int main() { n   int n, i, a, b; n   scanf( %d , &n); n   for (i = 0; i < n; ++i) { n     scanf( %d%d , &a, &b); n     in[i] = a; n     if (a) q.insert(Q(a, b, i)); n   } n   while (!q.empty()) { n     Q u = *(q.begin()); n     q.erase(q.begin()); n     int cur = u.c; n     ans.push_back(make_pair(u.b, u.c)); n     in[u.b]--; n     in[u.c]--; n     u = *q.lower_bound(Q(in[u.b] + 1, 0, u.b)); n     q.erase(u); n     u.a--; n     u.b ^= cur; n     if (u.a) q.insert(u); n   } n   printf( %d n , ans.size()); n   for (i = 0; i < ans.size(); ++i) n     printf( %d %d n , ans[i].first, ans[i].second); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int pos[4][2] = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}}; n int n; n char s[510][510]; n int v[510][510]; n vector<pair<int, int> > vec, ans; n int operator*(const pair<int, int> &a, const pair<int, int> &b) { n   return a.first * b.second - a.second * b.first; n } n pair<int, int> operator-(const pair<int, int> &a, const pair<int, int> &b) { n   return make_pair(a.first - b.first, a.second - b.second); n } n pair<int, int> stk[250010]; n int main() { n   while (1) { n     ans.clear(); n     scanf( %d , &n); n     if (n == 0) break; n     for (int i = n; i >= 1; i--) scanf( %s , s[i] + 1); n     for (int i = 1; i <= n; i++) n       for (int j = 1; j <= n; j++) v[i][j] = s[i][j] -  0 ; n     vec.clear(); n     for (int i = 1; i <= n; i++) { n       for (int j = 1; j <= n; j++) { n         int cnt = 0; n         for (int k = -1; k <= 0; k++) { n           for (int l = -1; l <= 0; l++) { n             cnt += v[i + k][j + l]; n           } n         } n         if (cnt == 1) { n           if (v[i][j] == 1) n             vec.push_back(make_pair(i, j)); n           else if (v[i - 1][j] == 1) n             vec.push_back(make_pair(i - 2, j)); n           else if (v[i][j - 1] == 1) n             vec.push_back(make_pair(i, j - 2)); n           else n             vec.push_back(make_pair(i - 2, j - 2)); n         } n       } n     } n     for (auto &x : vec) swap(x.first, x.second); n     sort(vec.begin(), vec.end()); n     int top = 0; n     for (auto &x : vec) { n       while (top > 1 && (stk[top] - stk[top - 1]) * (x - stk[top]) >= 0) top--; n       stk[++top] = x; n     } n     for (int i = 1; i <= top; i++) ans.push_back(stk[i]); n     reverse(vec.begin(), vec.end()); n     top = 0; n     for (auto &x : vec) { n       while (top > 1 && (stk[top] - stk[top - 1]) * (x - stk[top]) >= 0) top--; n       stk[++top] = x; n     } n     for (int i = 2; i < top; i++) ans.push_back(stk[i]); n     printf( %d n , ans.size()); n     for (auto &x : ans) printf( %d %d n , x.first, x.second); n   } n   return 0; n } n


#include <bits/stdc++.h> n #pragma GCC target( avx2 ) n #pragma GCC optimization( O3 ) n #pragma GCC optimization( unroll-loops ) n using namespace std; n const int mod = 1e9 + 7; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   int t; n   cin >> t; n   while (t--) { n     long long n, m, a, b, i, j; n     cin >> n; n     deque<long long> dq, tmp; n     long long ar[n], ans[n]; n     set<long long> s; n     for (i = 0; i < n; i++) { n       cin >> a; n       ar[a - 1] = i; n       ans[i] = 0; n     } n     a = n, b = 0; n     for (i = 0; i < n; i++) { n       a = min(a, ar[i]); n       b = max(b, ar[i]); n       if (b - a == i) ans[i] = 1; n     } n     for (i = 0; i < n; i++) cout << ans[i]; n     cout << endl; n   } n   return 0; 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   long long n, p, b = 0; n   cin >> n >> p; n   vector<long long> v; n   for (int i = 0; i < n; ++i) { n     string s; n     cin >> s; n     if (s ==  half ) { n       v.push_back(0); n     } else n       v.push_back(1), b++; n   } n   reverse(v.begin(), v.end()); n   long long at = 0; n   for (int i = 0; i < n; ++i) { n     at *= 2; n     at += v[i]; n   } n   cout << (at * p) - (b * p / 2) << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAX = 1005; n vector<int> v[MAX]; n int dp[105][MAX]; n int main() { n   ios::sync_with_stdio(false); n   int n, t; n   cin >> n >> t; n   for (int i = 0; i < n; i++) { n     int t, p; n     cin >> t >> p; n     v[t].push_back(p); n   } n   for (int i = 0; i < MAX; i++) { n     sort(v[i].begin(), v[i].end()); n     v[i].push_back(0); n     reverse(v[i].begin(), v[i].end()); n     for (int j = 1; j < v[i].size(); j++) v[i][j] += v[i][j - 1]; n   } n   for (int i = 1; i <= t; i++) n     for (int j = 1; j < MAX; j++) n       for (int k = 0; k < min((int)v[i].size(), j + 1); k++) n         dp[i][j] = n             max(dp[i][j], dp[i - 1][min(MAX - 1, (j - k) * 2)] + v[i][k]); n   cout << dp[t][1] << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long M = 7e2 + 5; n const long long mod = 1e9 + 7; n char sz[M]; n long long a[M], js; n inline long long read() { n   register long long x = 0, y = 1; n   register char c = getchar(); n   while (!isdigit(c)) { n     if (c ==  - ) y = 0; n     c = getchar(); n   } n   while (isdigit(c)) { n     x = x * 10 + (c ^ 48); n     c = getchar(); n   } n   return y ? x : -x; n } n long long f[M][M][10]; n long long dfs(long long pos, long long limit, long long sum, long long l) { n   if (!pos) return !sum % mod; n   if (!limit && ~f[pos][sum][l]) return f[pos][sum][l] % mod; n   long long up = limit ? a[pos] : 9; n   long long cnt = 0; n   for (long long i = 0; i <= up; i++) n     cnt = (cnt + dfs(pos - 1, limit && i == up, sum - (i >= l), l)) % mod; n   if (!limit) f[pos][sum][l] = cnt; n   return cnt % mod; n } n signed main() { n   scanf( %s , sz + 1); n   memset(f, -1, sizeof(f)); n   long long len = strlen(sz + 1); n   for (long long i = 1; i <= len; i++) a[i] = sz[len - i + 1] -  0 ; n   a[0] = 0; n   for (long long i = 1; i <= 9; i++) n     for (long long j = 1, puz = 1; j <= len; n          j++, puz = (10ll * puz + 1ll) % mod) n       js = (js + dfs(len, 1, j, i) * puz % mod) % mod; n   printf( %lld n , js); 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   int A[n][m], B[n][m], C[n][m]; n   vector<int> a, b; n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < m; j++) { n       cin >> A[i][j]; n       a.push_back(A[i][j]); n     } n   } n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < m; j++) { n       cin >> B[i][j]; n       b.push_back(B[i][j]); n     } n   } n   sort(a.begin(), a.end()); n   sort(b.begin(), b.end()); n   for (int i = 0; i < a.size(); i++) { n     if (a[i] != b[i]) { n       cout <<  NO  << endl; n       return 0; n     } n   } n   if (n >= m) { n     for (int i = 0; i < n; i++) { n       long long x = 0; n       for (int j = 0; j < min(i + 1, m); j++) { n         x ^= (long long)(A[i - j][j] ^ B[i - j][j]); n       } n       if (x) { n         cout <<  NO  << endl; n         return 0; n       } n     } n     for (int i = 0; i < m - 1; i++) { n       long long x = 0; n       for (int j = 1; j + i < m; j++) { n         x ^= (long long)(A[n - j][j + i] ^ B[n - j][j + i]); n       } n       if (x) { n         cout <<  NO  << endl; n         return 0; n       } n     } n   } else { n     for (int i = 0; i < m; i++) { n       long long x = 0; n       for (int j = 0; j < min(i + 1, n); j++) { n         x ^= (long long)(A[j][i - j] ^ B[j][i - j]); n       } n       if (x) { n         cout <<  NO  << endl; n         return 0; n       } n     } n     for (int i = 0; i < n - 1; i++) { n       long long x = 0; n       for (int j = 1; j + i < n; j++) { n         x ^= (long long)(A[j + i][m - j] ^ B[j + i][m - j]); n       } n       if (x) { n         cout <<  NO  << endl; n         return 0; n       } n     } n   } n   cout <<  YES  << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int n, q; n int x[100100], y[100100]; n bool ans[100100]; n int par[100100], sz[100100]; n int distToPar[100100]; n int getpar(int now) { return (par[now] == now) ? now : getpar(par[now]); } n bool getdist(int now) { n   bool ret = (par[now] == now) ? 0 : (getdist(par[now]) ^ distToPar[now]); n   return ret; n } n stack<pair<pair<int, int>, bool> > prevStat; n map<pair<int, int>, int> mp; n vector<pair<pair<int, int>, pair<int, int> > > opSeg; n vector<pair<int, int> > add[400400]; n void addSeg(int tl, int tr, pair<int, int> val, int l, int r, int k) { n   if (tl > r || l > tr) return; n   if (tl <= l && r <= tr) { n     add[k].push_back(val); n     return; n   } n   int mid = (l + r) >> 1; n   addSeg(tl, tr, val, l, mid, k + k); n   addSeg(tl, tr, val, mid + 1, r, k + k + 1); n } n bool isBipar = 1; n void addEdge(pair<int, int> now) { n   int u = now.first, v = now.second; n   int pu = getpar(u), pv = getpar(v); n   if (pu == pv) { n     prevStat.push(make_pair(make_pair(-1, -1), isBipar)); n     if ((getdist(u) ^ getdist(v)) == 0) { n       isBipar = 0; n     } n     return; n   } n   if (sz[pu] > sz[pv]) swap(u, v), swap(pu, pv); n   prevStat.push(make_pair(make_pair(pu, sz[pu]), isBipar)); n   distToPar[pu] = getdist(u) ^ getdist(v) ^ 1; n   par[pu] = pv; n   sz[pv] += sz[pu]; n } n void undo() { n   int pos = prevStat.top().first.first, presz = prevStat.top().first.second, n       preIsBipar = prevStat.top().second; n   prevStat.pop(); n   if (pos >= 0) { n     sz[pos] = presz; n     sz[par[pos]] -= sz[pos]; n     par[pos] = pos; n   } n   isBipar = preIsBipar; n } n void solve(int l, int r, int k) { n   for (int i = 0; i < (int)add[k].size(); i++) { n     addEdge(add[k][i]); n   } n   if (l == r) { n     ans[l] = isBipar; n   } else { n     int mid = (l + r) >> 1; n     solve(l, mid, k + k); n     solve(mid + 1, r, k + k + 1); n   } n   for (int i = 0; i < (int)add[k].size(); i++) undo(); n } n int main() { n   ios::sync_with_stdio(false); n   cin >> n >> q; n   for (int i = 0; i < q; i++) { n     cin >> x[i] >> y[i]; n     x[i]--, y[i]--; n     if (x[i] > y[i]) swap(x[i], y[i]); n   } n   for (int i = 0; i < n; i++) par[i] = i, sz[i] = 1; n   for (int i = 0; i < q; i++) { n     pair<int, int> now = make_pair(x[i], y[i]); n     if (!mp.count(now) || mp[now] < 0) { n       mp[now] = i; n     } else { n       opSeg.push_back(make_pair(make_pair(mp[now], i), now)); n       mp[now] = -1; n     } n   } n   for (map<pair<int, int>, int>::iterator it = mp.begin(); it != mp.end(); n        it++) { n     if (it->second >= 0) { n       opSeg.push_back(make_pair(make_pair(it->second, q), it->first)); n     } n   } n   for (int i = 0; i < (int)opSeg.size(); i++) { n     addSeg(opSeg[i].first.first, opSeg[i].first.second - 1, opSeg[i].second, 0, n            q - 1, 1); n   } n   solve(0, q - 1, 1); n   for (int i = 0; i < q; i++) cout << (ans[i] ?  YES  :  NO ) << endl; n   return 0; 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   int count = 0; n   for (int i = 0; i < n - 1; i++) { n     if (s[i] == s[i + 1]) { n       count++; n     } n   } n   cout << count; n } n


#include <bits/stdc++.h> n using namespace std; n stringstream ans; n int final_sum; n string print_line(string a0) { n   int S = 0; n   for (int i = 0; i < a0.size() - 1; i++) { n     int digit = a0[i] -  0 ; n     S += digit; n     ans << digit <<  + ; n   } n   int digit = a0[a0.size() - 1] -  0 ; n   S += digit; n   ans << digit << endl; n   final_sum = S; n   return to_string(S); n } n void print289(string a0) { print_line(print_line(print_line(a0))); } n void print999(string a0, int S, int offset = 0) { n   ans.str(  ); n   int i = 0; n   int nz = 0; n   int dt = 0; n   int ddd = 0; n   while (i < offset) { n     ans << a0[i] <<  + ; n     dt = 10 * dt + a0[i] -  0 ; n     ddd += a0[i] -  0 ; n     i++; n   } n   for (i += 1; i < a0.size() - 1; i += 2) { n     int a = a0[i - 1] -  0 ; n     int b = a0[i] -  0 ; n     int dt = 10 * a + b - a - b; n     S += dt; n     ans << a << b <<  + ; n     if (S >= 1000) break; n   } n   if (S < 1000 && i < a0.size()) { n     int a = a0[i - 1] -  0 ; n     int b = a0[i] -  0 ; n     int dt = 10 * a + b - a - b; n     S += dt; n     ans << a << b; n   } n   i++; n   for (i; i < a0.size() - 1; i++) ans << a0[i] <<  + ; n   for (i; i < a0.size(); i++) ans << a0[a0.size() - 1]; n   ans << endl; n   print_line(print_line(to_string(S))); n } n bool is_close(int S) { n   if (S == 0) return true; n   int div = 1; n   while (S / div > 0) div *= 10; n   if (S * 10 == div) return true; n   return div - S < 1000; n } n bool is_mega_close(int S) { n   if (S == 0) return true; n   int div = 1; n   while (S / div > 0) div *= 10; n   if (S * 10 == div) return true; n   return S - (div / 10) < 100; n } n void print(string a0, int S, int offset = 0) { n   ans.str(  ); n   int i = 0; n   int nz = 0; n   int dt = 0; n   int ddd = 0; n   while (i < offset) { n     ans << a0[i] <<  + ; n     dt = dt * 10 + a0[i]; n     ddd += a0[i] -  0 ; n     i++; n   } n   if (!is_close(S)) { n     for (i += 2; i < a0.size() - 2; i += 3) { n       int a = a0[i - 2] -  0 ; n       int b = a0[i - 1] -  0 ; n       int c = a0[i] -  0 ; n       int dt = 100 * a + 10 * b + c - a - b - c; n       S += dt; n       ans << a << b << c <<  + ; n       if (is_close(S)) break; n     } n     if (i < a0.size() - 2) i++; n   } n   if (!is_mega_close(S)) n     for (i += 2; i < a0.size() - 2; i += 3) { n       int a = a0[i - 2] -  0 ; n       int b = a0[i - 1] -  0 ; n       int c = a0[i] -  0 ; n       int dt = 9 * a; n       S += dt; n       ans << a << b <<  +  << c <<  + ; n       if (is_mega_close(S)) break; n     } n   if (i >= a0.size() - 2) n     i -= 2; n   else n     i++; n   for (i; i < a0.size() - 1; i++) ans << a0[i] <<  + ; n   for (i; i < a0.size(); i++) ans << a0[a0.size() - 1]; n   ans << endl; n   print_line(print_line(to_string(S))); n } n int main() { n   int n; n   cin >> n; n   string a0; n   cin >> a0; n   int S = 0; n   for (int i = 0; i < a0.size(); i++) { n     int digit = a0[i] -  0 ; n     S += digit; n   } n   if (S < 199) { n     print289(a0); n     cout << ans.str(); n     return 0; n   } n   if (S < 1000) { n     print999(a0, S); n     int k = 1; n     while (final_sum > 9 && k < 2) print999(a0, S, k++); n     if (false && n == 155096) { n       cout << k << endl; n       cout << final_sum << endl; n       return 0; n     } n     cout << ans.str(); n     return 0; n   } n   print(a0, S); n   int k = 1; n   while (final_sum > 9 && k < 3) print(a0, S, k++); n   cout << ans.str(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T> n inline T read() { n   T s = 0, f = 1; n   char ch = getchar(); n   while (!isdigit(ch)) { n     if (ch ==  - ) f = -1; n     ch = getchar(); n   } n   while (isdigit(ch)) { n     s = (s << 3) + (s << 1) + ch - 48; n     ch = getchar(); n   } n   return s * f; n } n template <typename T> n inline void print(T x) { n   if (x < 0) putchar( - ), x = -x; n   if (x > 9) print(x / 10); n   putchar(x % 10 +  0 ); n } n const int N = 2e5 + 100; n const int mod = 1e9 + 7; n long long h[N], w[N], sum[N]; n struct Segment { n   static const int maxn = 2e5 + 100; n   struct star { n     long long lsum, rsum, sum; n   } s[maxn << 2]; n   void pushup(int node, int l, int r) { n     int mid = l + r >> 1; n     s[node].lsum = n         max(s[node << 1].lsum, h[l] + sum[mid - 1] - sum[l - 1] + n                                    s[node << 1 | 1].lsum - h[mid + 1] + w[mid]); n     s[node].lsum = max(s[node].lsum, h[l] + h[mid + 1] + sum[mid] - sum[l - 1]); n     s[node].rsum = n         max(s[node << 1 | 1].rsum, n             h[r] + sum[r - 1] - sum[mid] + s[node << 1].rsum - h[mid] + w[mid]); n     s[node].rsum = max(s[node].rsum, h[r] + h[mid] + sum[r - 1] - sum[mid - 1]); n     s[node].sum = max(s[node << 1].rsum + s[node << 1 | 1].lsum - h[mid] - n                           h[mid + 1] + w[mid], n                       max(s[node << 1].sum, s[node << 1 | 1].sum)); n     s[node].sum = max(s[node].sum, max(s[node].lsum, s[node].rsum)); n     s[node].sum = max(s[node].sum, h[r] + h[l] + sum[r - 1] - sum[l - 1]); n     s[node].sum = max( n         s[node].sum, max(s[node << 1].rsum - h[mid] + h[mid + 1] + w[mid], n                          s[node << 1 | 1].lsum - h[mid + 1] + h[mid] + w[mid])); n     s[node].sum = max(s[node].sum, h[mid] + h[mid + 1] + w[mid]); n   } n   void build(int node, int l, int r) { n     if (l == r) { n       s[node].lsum = s[node].rsum = h[l]; n       s[node].sum = 0; n       return; n     } n     int mid = l + r >> 1; n     build(node << 1, l, mid); n     build(node << 1 | 1, mid + 1, r); n     pushup(node, l, r); n   } n   star query(int node, int l, int r, int L, int R) { n     if (L == l && R == r) { n       return s[node]; n     } n     int mid = l + r >> 1; n     if (R <= mid) n       return query(node << 1, l, mid, L, R); n     else if (L > mid) n       return query(node << 1 | 1, mid + 1, r, L, R); n     else { n       star lnode = query(node << 1, l, mid, L, mid); n       star rnode = query(node << 1 | 1, mid + 1, r, mid + 1, R); n       star now; n       now.lsum = max(lnode.lsum, h[L] + sum[mid - 1] - sum[L - 1] + rnode.lsum - n                                      h[mid + 1] + w[mid]); n       now.lsum = max(now.lsum, h[L] + h[mid + 1] + sum[mid] - sum[L - 1]); n       now.rsum = max(rnode.rsum, h[R] + sum[R - 1] - sum[mid] + lnode.rsum - n                                      h[mid] + w[mid]); n       now.rsum = max(now.rsum, h[R] + h[mid] + sum[R - 1] - sum[mid - 1]); n       now.sum = max(lnode.rsum + rnode.lsum - h[mid] - h[mid + 1] + w[mid], n                     max(lnode.sum, rnode.sum)); n       now.sum = max(now.sum, max(now.lsum, now.rsum)); n       now.sum = max(now.sum, h[L] + h[R] + sum[R - 1] - sum[L - 1]); n       now.sum = max(now.sum, max(lnode.rsum - h[mid] + h[mid + 1] + w[mid], n                                  rnode.lsum - h[mid + 1] + h[mid] + w[mid])); n       now.sum = max(now.sum, h[mid] + h[mid + 1] + w[mid]); n       return now; n     } n   } n } tree; n int main() { n   int n = read<int>(), m = read<int>(); n   for (int i = 1; i <= n; ++i) { n     w[i] = read<long long>(); n     w[i + n] = w[i]; n     sum[i] = sum[i - 1] + w[i]; n   } n   for (int i = 1; i <= n; ++i) { n     h[i] = read<long long>(); n     h[i] = h[i] + h[i]; n     h[i + n] = h[i]; n   } n   for (int i = n + 1; i <= n + n; ++i) { n     sum[i] = sum[i - 1] + w[i]; n   } n   tree.build(1, 1, n + n); n   for (int i = 1; i <= m; ++i) { n     int l = read<int>(), r = read<int>(); n     int lc, rc; n     if (r >= l) n       lc = r + 1, rc = l + n - 1; n     else n       lc = r + 1, rc = l - 1; n     printf( %lld n , tree.query(1, 1, n + n, lc, rc).sum); n   } n } n


#include <bits/stdc++.h> n using namespace std; n int n, m, a[1001000], cnt[1001000], z[1001000]; n int main() { n   ios::sync_with_stdio(false); n   memset(cnt, 0, sizeof(cnt)); n   memset(z, 0, sizeof(z)); n   cin >> n >> m; n   for (int i = 1; i <= n; i++) { n     cin >> a[i]; n     if (a[i] <= m) cnt[a[i]]++; n   } n   int ans = 0, flag = 0; n   for (int i = 1; i <= m; i++) { n     if (cnt[i]) { n       for (int k = 1; k * i <= m; k++) z[k * i] += cnt[i]; n     } n   } n   for (int i = 1; i <= m; i++) { n     if (z[i] > ans) { n       ans = z[i]; n       flag = i; n     } n   } n   cout << (flag == 0 ? 1 : flag) <<     << ans << endl; n   for (int i = 1; i <= n; i++) { n     if (flag % a[i] == 0 && flag != 0) cout << i <<    ; n   } n   cout << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n signed long long int n, m, k, x, y; n signed long long int ct[3], myct; n int main() { n   cin >> n >> m >> k >> x >> y; n   signed long long int f1, q, val, rem = k; n   if (n == 1) { n     f1 = m; n     q = rem / f1; n     rem -= q * f1; n     myct = q; n     ct[1] = q; n     ct[2] = q; n     if (y <= rem) myct++; n     if (rem != 0) ct[1]++; n     cout << ct[1] <<     << ct[2] <<     << myct <<   n ; n     return 0; n   } else if (n == 2) { n     f1 = 2 * m; n     q = rem / f1; n     rem -= q * f1; n     myct = q; n     ct[1] = ct[2] = q; n     if (((x - 1) * m + y) <= rem) myct++; n     if (rem != 0) ct[1]++; n     cout << ct[1] <<     << ct[2] <<     << myct <<   n ; n     return 0; n   } n   f1 = ((n - 2) * m * 2) + (2 * m); n   q = rem / f1; n   rem -= q * f1; n   ct[0] = 2 * q; n   ct[1] = q; n   ct[2] = q; n   int grp = -1; n   if (x == 1) { n     myct = q; n     grp = 1; n   } else if (x == n) { n     myct = q; n     grp = 2; n   } else { n     myct = 2 * q; n     grp = 0; n   } n   int stp = 0; n   if (rem > 0 && rem <= m) { n     ct[1]++; n     if (((x - 1) * m + y) <= rem) myct++; n   } else if (rem > m && rem <= (n - 1) * m) { n     ct[0]++; n     ct[1]++; n     if (((x - 1) * m + y) <= rem) myct++; n   } else if (rem > (n - 1) * m && rem <= n * m) { n     ct[0]++; n     ct[1]++; n     stp = 3; n     if (rem == n * m) ct[2]++; n     if (((x - 1) * m + y) <= rem) myct++; n   } else if (rem > n * m && rem <= f1) { n     ct[0] += 2; n     ct[1]++; n     ct[2]++; n     myct++; n     stp = 4; n     if (x == 1 || x == n) { n     } else { n       rem -= n * m; n       val = (n - x - 1) * m + y; n       if (val <= rem) myct++; n     } n   } n   signed long long int mini = ct[0], maxi = ct[0]; n   for (int i = 1; i < 3; i++) { n     mini = min(mini, ct[i]); n     maxi = max(maxi, ct[i]); n   } n   cout << maxi <<     << mini <<     << myct <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, mncnt = 1000000000, cnt; n   string a; n   string c =  ACTG ; n   cin >> n >> a; n   for (int i = 0; i <= n - 4; i++) { n     cnt = 0; n     string b = a.substr(i, 4); n     for (int j = 0; j < 4; j++) { n       int x = c[j] -  A ; n       int y = b[j] -  A ; n       if (abs(y - x) <= 13) n         cnt += abs(y - x); n       else n         cnt += 26 - (abs(y - x)); n     } n     mncnt = min(mncnt, cnt); n   } n   cout << mncnt << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int a[12][12], nxt[110]; n int id(int x, int y) { n   if (x & 1) return (10 - x) * 10 + 11 - y; n   return (10 - x) * 10 + y; n } n double dp[120]; n int main() { n   for (int i = 1; i <= 10; i++) n     for (int j = 1; j <= 10; j++) a[i][j] = id(i, j); n   for (int i = 1; i <= 10; i++) { n     for (int j = 1, k; j <= 10; j++) { n       scanf( %d , &k); n       nxt[a[i][j]] = a[i - k][j]; n     } n   } n   for (int i = 1; i < 6; i++) dp[100 - i] = 6; n   for (int i = 94; i; i--) { n     double x = 0; n     for (int j = 1; j <= 6; j++) { n       x += min(dp[i + j], dp[nxt[i + j]]); n     } n     dp[i] = x / 6 + 1; n   } n   printf( %.10f n , dp[1]); n } n


#include <bits/stdc++.h> n using namespace std; n struct data { n   int x, h, p; n } a[100005]; n int n, x[100005], kq[100005], st[4 * 100005]; n void update(int id, int l, int r, int p, int v) { n   if (l > p || r < p) return; n   if (l == p && l == r) { n     st[id] = v; n     return; n   } n   int m = (l + r) >> 1; n   update(id << 1, l, m, p, v); n   update((id << 1) + 1, m + 1, r, p, v); n   st[id] = max(st[id << 1], st[(id << 1) + 1]); n } n int getmax(int id, int l, int r, int d, int c) { n   if (l > c || r < d) return 0; n   if (l >= d && r <= c) return st[id]; n   int m = (l + r) >> 1; n   return max(getmax((id << 1), l, m, d, c), n              getmax((id << 1) + 1, m + 1, r, d, c)); n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   cin >> n; n   for (int i = 1; i <= n; ++i) { n     cin >> a[i].x >> a[i].h; n     a[i].p = i; n     a[i].h = a[i].x + a[i].h - 1; n   } n   sort(a + 1, a + n + 1, [](data a, data b) { return a.x < b.x; }); n   for (int i = n; i >= 1; --i) { n     x[i] = a[i].x; n     int pos = upper_bound(x + i + 1, x + n + 1, a[i].h) - x - 1; n     int t = getmax(1, 1, n, i + 1, pos); n     if (t < i) t = i; n     kq[a[i].p] = t - i + 1; n     update(1, 1, n, i, t); n   } n   for (int i = 1; i <= n; ++i) cout << kq[i] <<    ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int now[15][3], ans[15][3], nA, nB, nC, minN = 12, cnt, t[13]; n void dfs(int a, int b, int c, int num, int jw, int cou, bool ifA, bool ifB) { n   if (!a && !b && !c && !jw) { n     minN = num; n     cnt = cou; n     memcpy(ans, now, sizeof(now)); n     return; n   } n   if (a % 10 + b % 10 - c % 10 + jw == 0 && c) n     dfs(a / 10, b / 10, c / 10, num, 0, cou + 1, a ? 0 : 1, b ? 0 : 1); n   else if (a % 10 + b % 10 - c % 10 + jw == 10 && c) n     dfs(a / 10, b / 10, c / 10, num, 1, cou + 1, a ? 0 : 1, b ? 0 : 1); n   else { n     if (minN - ++num <= 0) return; n     now[num][2] = ++cou; n     if (!ifA && c) { n       now[num][0] = 1; n       now[num][1] = (c % 10 - b % 10 - jw + 10) % 10; n       bool f = ifB; n       if (!b) ifB = 1; n       dfs(a, b / 10, c / 10, num, (now[num][1] + b % 10 + jw - c % 10) / 10, n           cou, ifA, ifB); n       ifB = f; n     } n     if (!ifB && c) { n       now[num][0] = 2; n       now[num][1] = (c % 10 - a % 10 + 10 - jw) % 10; n       bool f = ifA; n       if (a == 0) ifA = 1; n       dfs(a / 10, b, c / 10, num, (now[num][1] + a % 10 + jw - c % 10) / 10, n           cou, ifA, ifB); n       ifA = f; n     } n     now[num][0] = 3; n     now[num][1] = (a % 10 + b % 10 + jw) % 10; n     if (!a) ifA = 1; n     if (!b) ifB = 1; n     dfs(a / 10, b / 10, c, num, (a % 10 + b % 10 + jw - now[num][1]) / 10, cou, n         ifA, ifB); n   } n } n int main() { n   int a, b, c; n   bool f; n   scanf( %d+%d=%d , &a, &b, &c); n   dfs(a, b, c, 0, 0, 0, 0, 0); n   int p = 1; n   while (p <= minN && ans[p][0] - 1) p++; n   for (int i = 1; i <= cnt; i++) n     if (p <= minN && ans[p][2] == i) { n       t[i] = ans[p++][1]; n       while (p <= minN && ans[p][0] - 1) p++; n     } else { n       t[i] = a % 10; n       a /= 10; n     } n   f = 0; n   for (int i = cnt; i; i--) n     if (f || t[i]) { n       putchar(t[i] +  0 ); n       f = 1; n     } n   putchar( + ); n   p = 1; n   while (p <= minN && ans[p][0] - 2) p++; n   for (int i = 1; i <= cnt; i++) n     if (p <= minN && ans[p][2] == i) { n       t[i] = ans[p++][1]; n       while (p <= minN && ans[p][0] - 2) p++; n     } else { n       t[i] = b % 10; n       b /= 10; n     } n   f = 0; n   for (int i = cnt; i; i--) n     if (f || t[i]) { n       putchar(t[i] +  0 ); n       f = 1; n     } n   putchar( = ); n   p = 1; n   while (p <= minN && ans[p][0] - 3) p++; n   for (int i = 1; i <= cnt; i++) n     if (p <= minN && ans[p][2] == i) { n       t[i] = ans[p][1]; n       p++; n       while (p <= minN && ans[p][0] - 3) p++; n     } else { n       t[i] = c % 10; n       c /= 10; n     } n   f = 0; n   for (int i = cnt; i; i--) n     if (f || t[i]) { n       putchar(t[i] +  0 ); n       f = 1; n     } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int mas[1010][1010]; n int main() { n   int N, K; n   cin >> N >> K; n   for (int i = 1; i <= N; i++) { n     int k = 0; n     for (int j = 1; j <= N; j++) { n       if (k == K) n         break; n       else if (i == j) n         continue; n       else if (mas[j][i] || mas[i][j]) n         continue; n       else { n         mas[i][j] = 1; n         k++; n       } n     } n     if (k < K) { n       cout << -1 << endl; n       return 0; n     } n   } n   int cnt = 0; n   for (int i = 1; i <= N; i++) { n     for (int j = 1; j <= N; j++) { n       if (mas[i][j]) cnt++; n     } n   } n   cout << cnt << endl; n   for (int i = 1; i <= N; i++) { n     for (int j = 1; j <= N; j++) { n       if (mas[i][j]) cout << i <<     << j << endl; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long n, m, k, mod = 1e9 + 7; n map<long long, long long> mp; n string s; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   srand(time(0)); n   cin >> n >> k; n   vector<long long> a(n + 2); n   vector<long long> b(k + 2); n   for (long long i = 1; i <= n; ++i) cin >> a[i], a[i] -= i; n   a[0] = -2e9; n   a[n + 1] = 2e9; n   b[k + 1] = n + 1; n   for (long long i = 1; i <= k; ++i) cin >> b[i]; n   long long ans = 0; n   for (long long i = 0; i <= k; ++i) { n     long long l = b[i]; n     long long r = b[i + 1]; n     if (a[l] > a[r]) { n       cout << -1; n       exit(0); n     } n     vector<long long> v; n     for (long long j = l + 1; j < r; ++j) { n       if (a[j] > a[r] || a[j] < a[l]) continue; n       auto x = upper_bound(v.begin(), v.end(), a[j]); n       if (x == v.end()) n         v.push_back(a[j]); n       else n         *x = a[j]; n     } n     ans += r - l - 1 - v.size(); n   } n   cout << ans; n } n


#include <bits/stdc++.h> n using namespace std; n int N, K, M, S; n int arr[500000]; n int raito[500005]; n int seen[500005]; n int bucket[500005]; n int shift[500005]; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cin >> M >> K >> N >> S; n   for (int i = 0; i < M; i++) { n     cin >> arr[i]; n   } n   int t; n   for (int i = 0; i < S; i++) { n     cin >> t; n     bucket[t]++; n   } n   for (int i = 0; i < 500005; i++) seen[i] = -1; n   for (int i = M - 1; i >= 0; i--) { n     raito[i] = seen[arr[i]]; n     seen[arr[i]] = i; n   } n   int maxima = 0; n   for (int i = 0; i < 500005; i++) { n     if (bucket[i] == 0) continue; n     int cur = seen[i]; n     int p = 1; n     while (cur != -1 && p < bucket[i]) { n       cur = raito[cur]; n       p++; n     } n     if (cur == -1) { n       cout << -1; n       return 0; n     } n     shift[i] = cur; n     maxima = max(maxima, cur); n   } n   vector<int> adj; n   for (int i = 0; i < M;) { n     int total = i / K + 1 + ((M - 1) - (maxima + 1) + 1) / K; n     if (total >= N) { n       int extra = K - S; n       for (int j = i; j <= maxima; j++) { n         if (extra <= 0 && bucket[arr[j]] == 0) { n           adj.push_back(j + 1); n         } else if (bucket[arr[j]] == 0) { n           extra--; n         } else { n           bucket[arr[j]]--; n         } n       } n       cout << adj.size() <<   n ; n       for (int j = 0; j < adj.size(); j++) { n         cout << adj[j] <<    ; n       } n       return 0; n     } n     if (i + 1 == M) break; n     int pp = 0; n     if (bucket[arr[i]] != 0) { n       shift[arr[i]] = raito[shift[arr[i]]]; n       maxima = max(maxima, shift[arr[i]]); n       if (shift[arr[i]] == -1) { n         cout << -1; n         return 0; n       } n     } n     i++; n   } n   cout << -1; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 400010; n int n; n int fa[MAXN][21]; n int dep[MAXN]; n int LCA(int x, int y) { n   if (dep[x] < dep[y]) swap(x, y); n   for (int i = 20; i >= 0; i--) { n     if (dep[x] - (1 << i) >= dep[y]) { n       x = fa[x][i]; n     } n   } n   if (x == y) return x; n   for (int i = 20; i >= 0; i--) { n     if (fa[x][i] != fa[y][i]) { n       x = fa[x][i]; n       y = fa[y][i]; n     } n   } n   return fa[x][0]; n } n int main() { n   scanf( %d , &n); n   int L, R; n   L = R = 1; n   int mx = 0; n   int p; n   for (int i = 2; i <= n; i++) { n     scanf( %d , &p); n     fa[i][0] = p; n     dep[i] = dep[p] + 1; n     for (int j = 1; j <= 20; j++) { n       fa[i][j] = fa[fa[i][j - 1]][j - 1]; n     } n     int z = LCA(i, L); n     int ans1 = dep[i] + dep[L] - 2 * dep[z]; n     z = LCA(i, R); n     int ans2 = dep[i] + dep[R] - 2 * dep[z]; n     if (ans1 > ans2 && ans1 > mx) { n       mx = ans1; n       R = i; n     } n     if (ans2 >= ans1 && ans2 > mx) { n       L = i; n       mx = ans2; n     } n     if (i == n) n       printf( %d n , mx); n     else n       printf( %d  , mx); n   } n } n


#include <bits/stdc++.h> n using namespace std; n #pragma GCC optimize( Ofast,no-stack-protector ) n #pragma GCC target( sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native ) n #pragma GCC optimize( unroll-loops ) n long long fun(long long n, long long m, long long mid) { n   long long val = 0; n   for (long long i = 1; i <= n; i++) val += min(m, (mid - 1) / i); n   return val; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   ; n   long long tst = 1; n   for (long long tt = 1; tt <= tst; tt++) { n     long long n, m, k; n     cin >> n >> m >> k; n     long long l = 1, r = (n * m) + 1; n     while (l < r) { n       long long mid = (l + r) / 2; n       if (fun(n, m, mid) < k) n         l = mid + 1; n       else n         r = mid; n     } n     cout << l - 1 << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; } n long long lcm(long long a, long long b) { return a / gcd(a, b) * b; } n int main() { n   long long a, b; n   while (cin >> a >> b) { n     long long suma = 0, sumb = 0; n     long long ca = 0, cb = 0; n     long long limit = lcm(a, b); n     long long now = 0, prev = 0; n     while (now <= limit) { n       if (now % b != 0 || (now % a == 0 && a > b)) { n         suma += now - prev; n       } else { n         sumb += now - prev; n       } n       prev = now; n       now = min(a * (ca + 1), b * (cb + 1)); n       if (now % a == 0) ca++; n       if (now % b == 0) cb++; n     } n     cout << (suma == sumb ?  Equal  : suma > sumb ?  Dasha  :  Masha ) << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, k, dp[2000 + 10][2000 + 10]; n bool v[2000 + 10][2000 + 10]; n vector<int> vec[2000 + 10]; n int solve(int curr, int kk) { n   if (kk > k) return 0; n   if (kk == k) return 1; n   if (v[curr][kk]) return dp[curr][kk]; n   for (int i = 0; i < vec[curr].size(); i++) { n     dp[curr][kk] += (solve(vec[curr][i], kk + 1)) % 1000000007; n     dp[curr][kk] %= 1000000007; n   } n   v[curr][kk] = true; n   return dp[curr][kk]; n } n int main() { n   ios::sync_with_stdio(false), cin.tie(0); n   cin >> n >> k; n   for (int i = 1; i <= n; i++) { n     for (int j = i; j <= n; j += i) { n       if (!(j % i)) vec[i].push_back(j); n     } n   } n   cout << solve(1, 0) <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int a[100005]; n int dp[302][100005]; n int main() { n   ios::sync_with_stdio(0); n   int n, q; n   cin >> n; n   for (int i = 1; i <= n; ++i) cin >> a[i]; n   cin >> q; n   memset(dp, 0, sizeof(dp)); n   for (int k = min(300, n); k >= 1; --k) { n     for (int i = n; i >= 1; --i) { n       if (i + a[i] + k > n) n         dp[k][i] = 1; n       else n         dp[k][i] = 1 + dp[k][i + a[i] + k]; n     } n   } n   while (q--) { n     int p, k; n     cin >> p >> k; n     if (k <= 300) n       cout << dp[k][p] << endl; n     else { n       int ans = 0; n       while (p <= n) { n         p = p + a[p] + k; n         ans++; n       } n       cout << ans << endl; n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n long long n, ans = 0, minm; n long long A[10 * 100000 + 6]; n unordered_map<long long, long long> M; n long long pos, neg, cc = 0, k = 0; n int main() { n   scanf( %lld , &n); n   for (int i = 1; i <= n; ++i) { n     scanf( %lld , &A[i]); n     M[A[i] - i]++; n     ans += abs(A[i] - i); n     if (A[i] <= i) n       neg++; n     else n       pos++; n   } n   minm = ans; n   for (long long i = 1, j = n; i <= n - 1; ++i, --j) { n     if (A[j] - j - cc <= 0) n       --neg; n     else n       --pos; n     ans -= abs(A[j] - j - cc); n     ans += A[j]; n     ++pos; n     M[A[j] + cc]++; n     M[A[j] - j]--; n     ++cc; n     ans -= pos; n     ans += neg; n     pos -= M[cc]; n     neg += M[cc]; n     if (ans <= minm) { n       minm = ans; n       k = i; n     } n   } n   printf( %lld %lld n , minm, k); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int a[200005], ans1 = 1, ans2 = 0; n int main() { n   int n, b, b1; n   scanf( %d , &n); n   for (int i = 1; i <= n; ++i) { n     scanf( %d , &a[i]); n   } n   sort(a + 1, a + 1 + n); n   b1 = 0; n   for (int i = 2; i <= n; ++i) { n     b = a[i] - a[i - 1]; n     if (b == 0) { n       continue; n     } else if (b == 1) { n       if (b1 == 2) { n         ans1++; n         b1 = 0; n       } else if (b1 == 1) { n         b1 = 2; n       } else if (b1 == 0) { n         b1 = 1; n       } n     } else if (b == 2) { n       if (b1 == 0) { n         b1 = 2; n       } else if (b1 >= 1) { n         b1 = 0; n         ans1++; n       } n     } else { n       b1 = 0; n       ans1++; n     } n   } n   for (int i = 1; i <= n; ++i) { n     b = a[i] - a[i - 1]; n     if (i == 1) { n       a[i]--; n       ans2++; n       continue; n     } n     if (b >= 1) ans2++; n     if (b == 0 && a[i] != a[i + 1]) { n       ans2++; n       a[i]++; n     } else if (b == 1 && a[i] == a[i + 1] - 1) { n       continue; n     } else if (b == 1 && a[i] == a[i + 1] - 2) { n       continue; n     } else if (b == 1 && a[i] != a[i + 1]) { n       a[i]++; n     } else if (b >= 2) { n       a[i]--; n     } n   } n   printf( %d %d n , ans1, ans2); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main(int argc, const char* argv[]) { n   int cases; n   cin >> cases; n   while (cases--) { n     int num; n     scanf( %d , &num); n     vector<pair<int, int> > coords; n     pair<int, int> z(0, 0); n     coords.push_back(z); n     for (int i = 0; i < num; ++i) { n       int x, y; n       scanf( %d%d , &x, &y); n       coords.push_back({x, y}); n     } n     sort(coords.begin(), coords.end()); n     string out; n     bool ok = true; n     for (int i = 1; i <= num; ++i) { n       int diffx = coords[i].first - coords[i - 1].first; n       int diffy = coords[i].second - coords[i - 1].second; n       if (diffx < 0 || diffy < 0) { n         ok = false; n         break; n       } n       while (diffx--) out +=  R ; n       while (diffy--) out +=  U ; n     } n     if (ok) { n       cout <<  YES  << endl; n       cout << out << endl; n     } else { n       cout <<  NO  << endl; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int M = 110; n int a[M]; n int main() { n   int n, cnt = 0; n   scanf( %d , &n); n   for (int i = 1; i <= n; i++) { n     scanf( %d , a + i); n     if (a[i] % 2 == 0) cnt++; n   } n   if (n % 2 == 0) { n     printf( No n ); n     return 0; n   } n   if (a[1] % 2 == 0 || a[n] % 2 == 0) { n     printf( No n ); n     return 0; n   } else { n     printf( Yes n ); n     return 0; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <class T> n T mulmod(T a, T b) { n   if (b == 0) return 0; n   if (b == 1) return a; n   T x = mulmod(a, b >> 1); n   x = (x + x) % 1000000007; n   if (b & 1) x = (x + a) % 1000000007; n   return x; n } n template <class T> n T larger(T a, T b) { n   return (a > b ? a : b); n } n template <class T> n T smaller(T a, T b) { n   return (a < b ? a : b); n } n template <class T> n T gcd(T a, T b) { n   if (b == 0) return a; n   return gcd(b, a % b); n } n template <class T> n T lcm(T a, T b) { n   return (a * b) / gcd(a, b); n } n template <class T> n string converter(T n) { n   stringstream x; n   x << n; n   return x.str(); n } n template <class T> n T pw(T a, T b) { n   if (b == 0) return 1; n   T x = pw(a, b >> 1); n   x = mulmod(x, x); n   if (b & 1) x = mulmod(x, a); n   return x; n } n template <class T1> n void deb(T1 e1) { n   cout << e1 << endl; n } n template <class T1, class T2> n void deb(T1 e1, T2 e2) { n   cout << e1 <<     << e2 << endl; n } n template <class T1, class T2, class T3> n void deb(T1 e1, T2 e2, T3 e3) { n   cout << e1 <<     << e2 <<     << e3 << endl; n } n template <class T1, class T2, class T3, class T4> n void deb(T1 e1, T2 e2, T3 e3, T4 e4) { n   cout << e1 <<     << e2 <<     << e3 <<     << e4 << endl; n } n template <class T1, class T2, class T3, class T4, class T5> n void deb(T1 e1, T2 e2, T3 e3, T4 e4, T5 e5) { n   cout << e1 <<     << e2 <<     << e3 <<     << e4 <<     << e5 << endl; n } n template <class T1, class T2, class T3, class T4, class T5, class T6> n void deb(T1 e1, T2 e2, T3 e3, T4 e4, T5 e5, T6 e6) { n   cout << e1 <<     << e2 <<     << e3 <<     << e4 <<     << e5 <<     << e6 n        << endl; n } n void letsgo() { n   long long int a, b, x, n; n   cin >> a >> b >> n >> x; n   if (a == 1) { n     cout << (x + (n % 1000000007 * b) % 1000000007) % 1000000007; n     return; n   } n   long long int aN = pw(a, n); n   long long int ans = (aN * x) % 1000000007; n   long long int z = ((aN - 1 + 1000000007) % 1000000007 * b) % 1000000007; n   z = (z * pw(a - 1, 1000000007 - 2ll)) % 1000000007; n   cout << (ans + z) % 1000000007; n } n int main() { n   letsgo(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int inf = 0x3f3f3f3f; n const double eps = 1e-8; n const double PI = acos(-1.0); n const int maxn = 200 + 10; n char str[maxn]; n struct TwoSat { n   int n; n   vector<int> G[maxn * 2]; n   bool mark[maxn * 2]; n   int S[maxn * 2], c; n   bool isv[maxn]; n   int sz; n   bool dfs(int x) { n     if (mark[x ^ 1]) return false; n     if (mark[x]) return true; n     if (x / 2 < xi) { n       int id = str[x / 2] -  a ; n       if (isv[id] != (x & 1)) return false; n       tmp[x / 2] = str[x / 2]; n     } else if (x / 2 == xi) { n       int t = get_min(str[xi] -  a , (x & 1) ? true : false); n       if (t == -1) return false; n       tmp[x / 2] = t +  a ; n     } else { n       int t = get_min(-1, (x & 1) ? true : false); n       if (t == -1) return false; n       tmp[x / 2] = t +  a ; n     } n     mark[x] = true; n     S[c++] = x; n     for (int i = 0; i < G[x].size(); i++) n       if (!dfs(G[x][i])) return false; n     return true; n   } n   void init(int n, char *s) { n     this->n = n; n     for (int i = 0; i < n * 2; i++) G[i].clear(); n     memset(mark, false, sizeof(mark)); n     sz = strlen(s); n     for (int i = 0; i < sz; i++) isv[i] = s[i] ==  V  ? true : false; n   } n   void add_clause(int x, int xval, int y, int yval) { n     x = x * 2 + xval; n     y = y * 2 + yval; n     G[x ^ 1].push_back(y); n     G[y ^ 1].push_back(x); n   } n   int get_min(int pos, bool c) { n     for (int i = pos + 1; i < sz; i++) { n       if (c == isv[i]) return i; n     } n     return -1; n   } n   char ans[maxn], tmp[maxn]; n   int xi; n   void solve() { n     bool OK = false; n     for (int pos = 0; pos <= n; pos++) { n       for (int x = 0; x <= 1; x++) { n         xi = pos; n         bool ok = false; n         memset(mark, false, sizeof(mark)); n         strcpy(tmp, str); n         if (pos < n) { n           int t = get_min(str[pos] -  a , (x & 1) ? true : false); n           if (t == -1) continue; n           tmp[pos] = t +  a ; n         } n         for (int i = pos + 1; i < n; i++) tmp[i] =  a ; n         c = 0; n         for (int i = 0; i < n; i++) { n           int x1 = (i << 1) | (isv[tmp[i] -  a ]); n           if (!dfs(x1)) { n             ok = true; n             break; n           } n         } n         if (n == 200) { n         } n         if (ok) continue; n         OK = true; n         tmp[n] = 0; n         if (ans[0] == 0) n           strcpy(ans, tmp); n         else if (strcmp(tmp, ans) < 0) n           strcpy(ans, tmp); n       } n     } n     if (OK) n       printf( %s n , ans); n     else n       printf( -1 n ); n   } n } sat; n int main() { n   int n, m; n   while (~scanf( %s , str)) { n     scanf( %d%d , &n, &m); n     sat.init(n, str); n     for (int i = 0, x, y, xval, yval; i < m; i++) { n       char t1[5], t2[5]; n       scanf( %d%s%d%s , &x, t1, &y, t2); n       xval = t1[0] ==  V  ? 1 : 0; n       yval = t2[0] ==  V  ? 1 : 0; n       --x; n       --y; n       sat.add_clause(x, xval ^ 1, y, yval); n     } n     scanf( %s , str); n     sat.solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int thien, n, tt, h; n string s; n int main() { n   cin >> tt; n   for (int ii = 1; ii <= tt; ii++) { n     cin >> n; n     thien = 0; n     h = 0; n     for (int i = 1; i <= n; i++) { n       cin >> s; n       if (s.length() % 2 != 0) thien++; n       for (int j = 0; j < s.length(); j++) n         if (s[j] ==  1 ) h++; n     } n     if (h % 2 != 0 && thien == 0) n       cout << n - 1 << endl; n     else n       cout << n << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 300010; n int p1[maxn], p2[maxn], q[maxn]; n int main() { n   int n, m; n   scanf( %d%d , &n, &m); n   for (int i = 0; i < n; i++) { n     scanf( %d , &p1[i]); n     p1[i]--; n     p2[p1[i]] = i; n     q[i] = n; n   } n   for (int i = 0; i < m; i++) { n     int x, y; n     scanf( %d%d , &x, &y); n     x--; n     y--; n     x = p2[x]; n     y = p2[y]; n     if (x > y) swap(x, y); n     q[x] = min(q[x], y); n   } n   int r = n - 1; n   long long ans = 0; n   for (int i = n - 1; i >= 0; i--) { n     r = min(q[i] - 1, r); n     ans += r - i + 1; n   } n   printf( %lld n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, a[200001]; n const double esl = 1e-11; n double cal(double x) { n   double t = 0; n   double ans = 0; n   for (int i = 1; i <= n; i++) { n     if (t + a[i] - x < 0) { n       ans = max(ans, t); n       t = 0; n     } else { n       t += ((double)a[i] - x); n       ans = max(ans, t); n     } n   } n   ans = max(ans, t); n   double t1 = 0; n   for (int i = 1; i <= n; i++) { n     if (t1 + a[i] - x > 0) { n       ans = max(ans, -t1); n       t1 = 0; n     } else { n       t1 += ((double)a[i] - x); n       ans = max(ans, -t1); n     } n   } n   ans = max(ans, -t1); n   return ans; n } n int main() { n   scanf( %d , &n); n   for (int i = 1; i <= n; i++) { n     scanf( %d , &a[i]); n   } n   double l = -(1e5); n   double r = 1e5; n   for (int i = 1; i <= 500; i++) { n     double d = (2.0 * l + r) / 3; n     double c = (2.0 * r + l) / 3; n     if (cal(d) > cal(c)) { n       l = d; n     } else n       r = c; n   } n   printf( %.10f , cal(l)); n   return 0; n } n


#include <bits/stdc++.h> n const int N = 5e4 + 10; n int n, m; n std::vector<char> a[N]; n int sx[N], sy[N]; n int main() { n   int T; n   scanf( %d , &T); n   while (T--) { n     scanf( %d%d , &n, &m); n     for (int i = 1; i <= n; ++i) { n       a[i].resize(m + 2); n       getchar(); n       for (int j = 1; j <= m; ++j) scanf( %c , &a[i][j]); n     } n     int cnt_x = 0, cnt_y = 0, p_x = 1, p_y = 1; n     for (int i = 1; i <= n; ++i) { n       int sum = 0; n       for (int j = 1; j <= m; ++j) sum += (a[i][j] ==  * ); n       sx[i] = sum; n     } n     for (int i = 1; i <= m; ++i) { n       int sum = 0; n       for (int j = 1; j <= n; ++j) sum += (a[j][i] ==  * ); n       sy[i] = sum; n     } n     int ans = 0x3f3f3f3f; n     for (int i = 1; i <= n; ++i) n       for (int j = 1; j <= m; ++j) { n         int now = n + m - sx[i] - sy[j]; n         if (a[i][j] ==  . ) --now; n         ans = std::min(ans, now); n       } n     printf( %d n , ans); n     for (int i = 1; i <= n; ++i) a[i].clear(), sx[i] = 0; n     for (int i = 1; i <= m; ++i) sy[i] = 0; n   } n   return 0; n } n