#include <bits/stdc++.h> n using std::max; n using std::min; n const int inf = 0x3f3f3f3f, Inf = 0x7fffffff; n const long long INF = 0x3f3f3f3f3f3f3f3f; n __inline__ __attribute__((always_inline)) unsigned int rnd() { n   static unsigned int seed = 416; n   return seed += 0x71dad4bfu, seed ^= seed >> 5, seed += 0xc6f74d88u, n          seed ^= seed << 17, seed += 0x25e6561u, seed ^= seed >> 13; n } n template <typename _Tp> n _Tp gcd(const _Tp &a, const _Tp &b) { n   return (!b) ? a : gcd(b, a % b); n } n template <typename _Tp> n __inline__ __attribute__((always_inline)) _Tp abs(const _Tp &a) { n   return a >= 0 ? a : -a; n } n template <typename _Tp> n __inline__ __attribute__((always_inline)) void chmax(_Tp &a, const _Tp &b) { n   (a < b) && (a = b); n } n template <typename _Tp> n __inline__ __attribute__((always_inline)) void chmin(_Tp &a, const _Tp &b) { n   (b < a) && (a = b); n } n template <typename _Tp> n __inline__ __attribute__((always_inline)) void read(_Tp &x) { n   char ch(getchar()); n   bool f(false); n   while (!isdigit(ch)) f |= ch == 45, ch = getchar(); n   x = ch & 15, ch = getchar(); n   while (isdigit(ch)) x = (((x << 2) + x) << 1) + (ch & 15), ch = getchar(); n   f && (x = -x); n } n template <typename _Tp, typename... Args> n __inline__ __attribute__((always_inline)) void read(_Tp &t, Args &...args) { n   read(t); n   read(args...); n } n template <typename _Tp, typename... Args> n __inline__ __attribute__((always_inline)) _Tp min(const _Tp &a, const _Tp &b, n                                                   const Args &...args) { n   return a < b ? min(a, args...) : min(b, args...); n } n template <typename _Tp, typename... Args> n __inline__ __attribute__((always_inline)) _Tp max(const _Tp &a, const _Tp &b, n                                                   const Args &...args) { n   return a < b ? max(b, args...) : max(a, args...); n } n __inline__ __attribute__((always_inline)) int read_str(char *s) { n   char ch(getchar()); n   while (ch ==     || ch ==   r  || ch ==   n ) ch = getchar(); n   char *tar = s; n   *tar = ch, ch = getchar(); n   while (ch !=     && ch !=   r  && ch !=   n  && ch != EOF) n     *(++tar) = ch, ch = getchar(); n   return tar - s + 1; n } n const int N = 65; n const int mod = 1000000007; n template <typename _Tp1, typename _Tp2> n __inline__ __attribute__((always_inline)) void add(_Tp1 &a, _Tp2 b) { n   (a += b) >= mod && (a -= mod); n } n template <typename _Tp1, typename _Tp2> n __inline__ __attribute__((always_inline)) void sub(_Tp1 &a, _Tp2 b) { n   (a -= b) < 0 && (a += mod); n } n int n, a[N], fa[N]; n int find(int x) { return x == fa[x] ? x : fa[x] = find(fa[x]); } n int dp[1 << 15][N], sum[1 << 15]; n std::pair<int, int> solve(int ___) { n   std::vector<int> nd; n   for (int i = 1; i <= n; ++i) n     if (find(i) == ___) nd.push_back(a[i]); n   std::vector<int> A, B; n   for (auto a : nd) { n     int cnt = 0; n     for (auto b : nd) n       if (a != b && !(a % b)) ++cnt; n     if (!cnt) n       A.push_back(a); n     else n       B.push_back(a); n   } n   if (((int)B.size()) <= 1) return std::make_pair(0, 1); n   memset(dp, 0, sizeof(dp)); n   memset(sum, 0, sizeof(sum)); n   for (auto &&it : B) { n     int qwq = 0; n     for (int i = 0; i < ((int)A.size()); ++i) n       if (!(it % A[i])) qwq |= 1 << i; n     it = qwq, ++sum[it]; n     ++dp[it][1]; n   } n   for (int i = 0; i < ((int)A.size()); ++i) n     for (int st = 0; st < 1 << ((int)A.size()); ++st) n       if ((st >> i) & 1) sum[st] += sum[st ^ (1 << i)]; n   for (int st = 0; st < 1 << ((int)A.size()); ++st) { n     for (int j = 1; j <= ((int)B.size()); ++j) { n       if (!dp[st][j]) continue; n       add(dp[st][j + 1], 1ll * dp[st][j] * (sum[st] - j) % mod); n       for (auto it : B) n         if ((it & st) && (st & it) != it) add(dp[st | it][j + 1], dp[st][j]); n     } n   } n   return std::make_pair(((int)B.size()) - 1, n                         dp[(1 << ((int)A.size())) - 1][((int)B.size())]); n } n int C[N][N]; n int main() { n   for (int i = 0; i < N; ++i) n     for (int j = C[i][0] = 1; j <= i; ++j) n       C[i][j] = C[i - 1][j - 1], add(C[i][j], C[i - 1][j]); n   read(n); n   for (int i = 1; i <= n; ++i) read(a[i]), fa[i] = i; n   std::sort(a + 1, a + n + 1); n   for (int i = 1; i <= n; ++i) n     for (int j = 1; j <= n; ++j) n       if (!(a[i] % a[j])) fa[find(i)] = find(j); n   long long ans = 1, res = 0; n   for (int i = 1; i <= n; ++i) { n     if (fa[i] == i) { n       std::pair<int, int> tmp = solve(i); n       res += tmp.first; n       ans = ans * C[res][tmp.first] % mod * tmp.second % mod; n     } n   } n   printf( %lld n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 2e7 + 7; n long long mn = (long long)(1e18 + 7), mx = -(long long)(1e18 + 7); n long long n, m, k, s, q, t; n long long a, b, c, d, e; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   cin >> n >> a >> b >> c >> d; n   for (int i = 0; i <= N; i++) { n     if (i * c <= n) mx = max(mx, i * a + ((n - i * c) / d * b)); n     if (i * d <= n) mx = max(mx, i * b + ((n - i * d) / c * a)); n   } n   cout << mx; n } n


#include <bits/stdc++.h> n using namespace std; n const long double PI = n     3.14159265358979323846264338327950288419716939937510582097494459230; n template <class T> n int getbit(T s, int i) { n   return (s >> 1) & 1; n } n template <class T> n T onbit(T s, int i) { n   return s | (T(1) << i); n } n template <class T> n T offbit(T s, int i) { n   return s & (~(T(1) << i)); n } n template <class T> n int cntbit(T s) { n   return __builtin_popcount(s); n } n long long N; n long long modPow(long long a, long long b) { n   if (b == 0) return 1; n   if (b == 1) return a; n   long long tmp = modPow(a, b / 2); n   if (b % 2 == 0) return ((tmp * tmp) % 1000000007LL); n   return ((((tmp * tmp) % 1000000007LL) * a) % 1000000007LL); n } n void VarInput() { cin >> N; } n void ProSolve() { n   long long ans = modPow(27, N) - modPow(7, N); n   while (ans < 0) ans += 1000000007LL; n   cout << ans; n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(NULL); n   VarInput(); n   ProSolve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 300010; n int h[MAXN], v[MAXN]; n int main() { n   int n; n   scanf(  %d , &n); n   for (int i = 0; i < (n); ++i) { n     scanf(  %d , &h[i]); n   } n   v[0] = 1; n   v[n - 1] = 1; n   for (int i = (1); i < (n - 1); ++i) { n     v[i] = min(h[i], v[i - 1] + 1); n   } n   for (int i = n - 2; i >= 0; --i) { n     v[i] = min(v[i], min(h[i], v[i + 1] + 1)); n   } n   int ans = -1; n   for (int i = 0; i < (n); ++i) ans = max(ans, v[i]); n   printf( %d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n const int nmax = 500000; n int ans[nmax + 5]; n int main() { n   int n, i, j, x1, y1, x2, y2; n   scanf( %d , &n); n   for (i = 1; i <= n; i++) { n     scanf( %d%d%d%d , &x1, &y1, &x2, &y2); n     ans[i] = (((x2 % 2) + 2) % 2) * 2 + ((y2 % 2) + 2) % 2 + 1; n   } n   printf( YES n ); n   for (i = 1; i <= n; i++) printf( %d n , ans[i]); n   return 0; n } n


#include <bits/stdc++.h> n int main() { n   long long n; n   while (~scanf( %I64d , &n)) { n     long long m = sqrt(n); n     long long m1 = sqrt(m); n     long long i; n     long long sum; n     int flag = 0; n     for (i = m + 1; i >= m + 1 - 100; i--) { n       sum = 0; n       long long b = i; n       while (b) { n         sum += b % 10; n         b /= 10; n       } n       long long num = sum * i + i * i; n       if (num == n) { n         printf( %I64d n , i); n         flag = 1; n         break; n       } n     } n     if (flag == 0) { n       printf( -1 n ); n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n int bit[200005]; n void update(long long int x) { n   while (x < 200005) { n     bit[x] += 1; n     if (x == 0) break; n     x += x & (-x); n   } n } n long long int query(long long int x) { n   long long int ans = 0; n   while (x >= 0) { n     ans += bit[x]; n     if (x == 0) break; n     x -= x & (-x); n   } n   return ans; n } n int main() { n   long long int n, t; n   cin >> n >> t; n   vector<long long int> v(n); n   for (long long int i = 0; i < n; i++) { n     cin >> v[i]; n     if (i) v[i] += v[i - 1]; n   } n   vector<long long int> v2 = v; n   sort(v.begin(), v.end()); n   long long int ans = 0; n   for (long long int i = (n - 1); i >= 0; i--) { n     int pos = lower_bound(v.begin(), v.end(), v2[i] + t) - v.begin(); n     ans += query(pos - 1); n     update(lower_bound(v.begin(), v.end(), v2[i]) - v.begin()); n   } n   ans += query(lower_bound(v.begin(), v.end(), t) - v.begin() - 1); n   cout << ans << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int get(int a) { n   int maxx = 0; n   for (int i = 0; i < 21; i++) n     if (a & (1 << i)) maxx = i; n   int ans = (1 << (maxx + 1)) - 1; n   return (ans ^ a); n } n int main() { n   int n; n   cin >> n; n   vector<int> a(n + 1); n   for (int i = n; i >= 0; i--) { n     if (a[i] == 0) { n       for (int j = 21; j >= 0; j--) { n         int tmp = get(i); n         if ((i ^ tmp) == i + tmp && a[tmp] == 0) { n           a[tmp] = i; n           a[i] = tmp; n           break; n         } n       } n     } n   } n   long long ans = 0; n   for (int i = 0; i <= n; i++) ans += (a[i] ^ i); n   cout << ans << endl; n   for (int i = 0; i <= n; i++) cout << a[i] <<    ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long INF = 0x3f3f3f3f; n const int N = 4e5 + 10; n const int M = 1e6 + 10; n const long long mod = 1e9 + 7; n const double PI = acos(-1); n long long w[N]; n int fa[N][21]; n long long wi[N][21]; n long long mx[N][21]; n int Find(int x, int y) { n   for (int i = 20; i >= 0; --i) n     if (mx[x][i] < y) x = fa[x][i]; n   return x; n } n int dd[21]; n int main() { n   dd[0] = 1; n   for (int i = 1; i <= 20; ++i) dd[i] = dd[i - 1] * 2; n   int cnt = 1, last = 0; n   w[0] = 1e16; n   for (int i = 0; i <= 20; ++i) fa[0][i] = 0, wi[0][i] = 1e16, mx[0][i] = 1e16; n   w[1] = 0, fa[1][0] = 0, wi[1][0] = 0; n   for (int i = 1; i <= 20; ++i) fa[1][i] = 0, wi[1][i] = 1e16, mx[1][i] = 1e16; n   int q, to, op; n   long long x, y; n   scanf( %d , &q); n   while (q--) { n     scanf( %d%I64d%I64d , &op, &x, &y); n     x ^= last, y ^= last; n     if (op == 1) { n       w[++cnt] = y; n       fa[cnt][0] = Find(x, y); n       mx[cnt][0] = wi[cnt][0] = w[cnt]; n       for (int i = 1; i <= 20; ++i) { n         to = fa[cnt][i - 1]; n         fa[cnt][i] = fa[to][i - 1]; n         mx[cnt][i] = max(mx[cnt][i - 1], mx[to][i - 1]); n         wi[cnt][i] = wi[cnt][i - 1] + wi[to][i - 1]; n       } n     } else { n       last = 0; n       for (int i = 20; i >= 0; --i) { n         if (wi[x][i] <= y) { n           last += dd[i]; n           y -= wi[x][i]; n           x = fa[x][i]; n         } n       } n       printf( %d n , last); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int prime = 999983; n const int INF = 0x7FFFFFFF; n const long long INFF = 0x7FFFFFFFFFFFFFFF; n const double pi = acos(-1.0); n const double inf = 1e18; n const double eps = 1e-6; n const long long mod = 1e9 + 7; n long long qpow(long long a, long long b) { n   long long s = 1; n   while (b > 0) { n     if (b & 1) s = s * a % mod; n     a = a * a % mod; n     b >>= 1; n   } n   return s; n } n long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; } n int dr[2][4] = {1, -1, 0, 0, 0, 0, -1, 1}; n const int maxm = 2e7, maxn = 3e5 + 10; n int a[maxn]; n int num[maxm][2]; n int main(void) { n   int n; n   cin >> n; n   long long ans = 0; n   num[0][0] = 1; n   for (int i = 1; i < n + 1; i++) { n     scanf( %d , &a[i]); n     a[i] ^= a[i - 1]; n     ans += num[a[i]][i % 2]; n     num[a[i]][i % 2]++; n   } n   cout << ans << endl; n   return 0; n } n


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


#include <bits/stdc++.h> n using namespace std; n long long int ma = -1, mi = 1000000000000000; n inline long long int gcd(long long int a, long long int b) { n   return b ? gcd(b, a % b) : a; n } n inline long long int lcm(long long int a, long long int b, long long int MOD) { n   return a / gcd(a, b) * b % MOD; n } n long long int power(long long int x, unsigned int y) { n   if (y == 0) n     return 1; n   else if (y % 2 == 0) n     return power(x, y / 2) * power(x, y / 2); n   else n     return x * power(x, y / 2) * power(x, y / 2); n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(NULL); n   cout.tie(NULL); n   long long int i, j, k, a[100000] = {0}, n, m, b[100009] = {0}, c = 0, d = 0, n                          e[30] = {0}, x = 0; n   cin >> n >> m; n   for (i = 0; i < n; i++) { n     cin >> a[i]; n   } n   for (i = 0; i < n - 1; i++) { n     if (a[i] + a[i + 1] < m) { n       d = m - a[i] - a[i + 1]; n       a[i + 1] += d; n       c += d; n     } n   } n   cout << c << endl; n   for (i = 0; i < n; i++) cout << a[i] <<    ; n } n


#include <bits/stdc++.h> n using namespace std; n int AA[100010]; n int BB[100010]; n long long dp[100010]; n int N; n vector<long long> M; n vector<long long> B; n bool bad(long long l1, long long l2, long long l3) { n   long double XX1 = 1.00000 * (M[l1] - M[l2]) / (M[l1] - M[l3]); n   long double XX2 = 1.00000 * (B[l2] - B[l1]) / (B[l3] - B[l1]); n   return XX1 < XX2; n } n void add(long long m, long long b) { n   M.push_back(m); n   B.push_back(b); n   while (M.size() >= 3 && bad(M.size() - 3, M.size() - 2, M.size() - 1)) { n     M.erase(M.end() - 2); n     B.erase(B.end() - 2); n   } n } n int pointer; n long long query(long long x) { n   if (pointer >= M.size()) pointer = M.size() - 1; n   while (pointer < M.size() - 1 && n          M[pointer + 1] * x + B[pointer + 1] > M[pointer] * x + B[pointer]) n     pointer++; n   return M[pointer] * x + B[pointer]; n } n int main() { n   ios_base::sync_with_stdio(0); n   cin >> N; n   for (int i = 1; i <= N; i++) { n     cin >> AA[i]; n   } n   for (int i = 1; i <= N; i++) { n     cin >> BB[i]; n   } n   dp[1] = 0LL; n   add(-BB[1], -dp[1]); n   for (int i = 2; i <= N; i++) { n     dp[i] = -1LL * query(AA[i]); n     add(-BB[i], -dp[i]); n   } n   cout << dp[N] << endl; n   return 0; n } n


#include <bits/stdc++.h> n #pragma warning(disable : 4996) n using namespace std; n int n, m; n int a[200000]; n char tmp[200000]; n int b[200000]; n int bi; n int c[200000]; n int ci; n int d[200000]; n vector<int> dab; n void calc() { n   int i, j; n   int cnt = 0; n   d[0] = -((1 << 30) - 1); n   ci = 0; n   for (i = 0; i < bi; i++) { n     if (b[i] == ((1 << 30) - 1)) n       cnt++; n     else { n       c[++ci] = cnt; n       d[ci] = b[i]; n       cnt = 0; n     } n   } n   c[++ci] = cnt; n   d[ci++] = ((1 << 30) - 1); n   for (i = 0; i < ci - 1; i++) { n     if (d[i + 1] - d[i] - 1 < c[i + 1]) { n       printf( Incorrect sequence ); n       exit(0); n     } n   } n   dab.clear(); n   for (i = 0; i < ci - 1; i++) { n     if (c[i + 1] == 0) continue; n     if (d[i] <= 0 && d[i + 1] <= 0) { n       for (j = d[i + 1] - c[i + 1]; j <= d[i + 1] - 1; j++) dab.push_back(j); n     } else if (d[i] >= 0 && d[i + 1] >= 0) { n       for (j = d[i] + 1; j <= d[i] + c[i + 1]; j++) dab.push_back(j); n     } else { n       if (min(abs(d[i + 1] - 1), abs(d[i] + 1)) * 2 + 1 >= c[i + 1]) { n         for (j = -(c[i + 1] - 1) / 2; j <= (c[i + 1] / 2); j++) n           dab.push_back(j); n       } else { n         int u = min(abs(d[i + 1] - 1), abs(d[i] + 1)); n         if (abs(d[i + 1] - 1) > abs(d[i] + 1)) { n           for (j = -u; j <= u; j++) dab.push_back(j); n           for (j = u + 1; n                j <= n                u + c[i + 1] - min(abs(d[i + 1] - 1), abs(d[i] + 1)) * 2 - 1; n                j++) n             dab.push_back(j); n         } else { n           for (j = -u - n                    (c[i + 1] - min(abs(d[i + 1] - 1), abs(d[i] + 1)) * 2 - 1); n                j <= -u - 1; j++) n             dab.push_back(j); n           for (j = -u; j <= u; j++) dab.push_back(j); n         } n       } n     } n   } n } n int main() { n   int i, j, k; n   cin >> n >> m; n   for (i = 0; i < n; i++) { n     scanf( %s , tmp); n     if (tmp[0] ==  ? ) n       a[i] = ((1 << 30) - 1); n     else n       a[i] = atoi(tmp); n   } n   for (i = 0; i < m; i++) { n     bi = 0; n     for (j = i; j < n; j += m) { n       b[bi++] = a[j]; n     } n     calc(); n     int u = 0; n     for (j = i; j < n; j += m) { n       if (a[j] == ((1 << 30) - 1)) a[j] = dab[u++]; n     } n   } n   for (i = 0; i < n; i++) printf( %d  , a[i]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int delta = (int)1e9 + 7; n int n, a[(int)3e5 + 20], b[(int)3e5 + 20], x, y, z, ans, h[(int)3e5 + 20]; n vector<int> G[(int)3e5 + 20]; n bool cmp(int u, int v) { n   if (a[u] == a[v]) return u < v; n   return a[u] > a[v]; n } n bool ac1() { n   for (int i = 0; i < G[b[1]].size(); ++i) h[G[b[1]][i]] = 1; n   int w = 2; n   while (a[b[w]] == a[b[1]] - 1 && w <= n) { n     if (!h[b[w]]) return 1; n     ++w; n   } n   return 0; n } n bool ac2() { n   for (int i = 1; i <= n; ++i) n     if (a[i] == z) ++y; n   for (int i = 1; i <= n; ++i) { n     if (a[i] == z) n       x = 1; n     else n       x = 0; n     for (int j = 0; j < G[i].size(); ++j) n       if (a[G[i][j]] == z) ++x; n     if (x == y) return 0; n   } n   return 1; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cin >> n; n   for (int i = 1; i <= n; ++i) cin >> a[i], b[i] = i; n   for (int i = 1; i < n; ++i) { n     cin >> x >> y; n     G[x].push_back(y); n     G[y].push_back(x); n   } n   sort(b + 1, b + n + 1, cmp); n   z = ans = a[b[1]]; n   if (n == 1 || a[b[1]] - a[b[2]] > 1) n     ; n   else if (a[b[1]] == a[b[2]] + 1) { n     if (ac1()) ++ans; n   } else { n     y = 0; n     ans += ac2() + 1; n   } n   cout << ans; n   return cout << endl, 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(long long first) { n   return first ? 8 * sizeof(long long) - __builtin_clzll(first) : 0; 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 DPowerTree { n  public: n   int N; n   vector<vector<int>> E; n   vector<long long> C; n   vector<int> Omit; n   vector<bool> Take; n   vector<pair<long long, long long>> Ans; n   pair<long long, long long> solve(int u, int p) { n     if (E[u].size() == 1 && u != 0) { n       return Ans[u] = {0, C[u]}; n     }; n     long long a = 0, b = 0; n     for (int v : E[u]) { n       if (v != p) { n         auto s = solve(v, u); n         a = min(a + s.second, b + s.first); n         b += s.second; n       } n     } n     return Ans[u] = {a, min(b, a + C[u])}; n   } n   void collect(int u, int p, bool allPaths, vector<int>& Ret) { n     long long diff = Ans[u].second - Ans[u].first; n     if (allPaths && C[u] <= diff) Ret.push_back(u + 1); n     int worst = -1; n     int cnt = 0; n     for (int v : E[u]) { n       if (v != p) { n         int myDiff = Ans[v].second - Ans[v].first; n         if (myDiff > worst) { n           worst = myDiff; n           cnt = 0; n         } n         cnt += myDiff == worst; n       } n     } n     for (int v : E[u]) { n       if (v != p) { n         int myDiff = Ans[v].second - Ans[v].first; n         collect(v, u, myDiff < worst || cnt > 1 || (worst <= C[u] && allPaths), n                 Ret); n       } n     } n   } n   void solve(istream& cin, ostream& cout) { n     cin >> N; n     C.resize(N); n     cin >> C; n     E.resize(N); n     Ans.resize(N); n     for (int i = 0; i < N - 1; ++i) { n       int u, v; n       cin >> u >> v; n       --u; n       --v; n       E[u].push_back(v); n       E[v].push_back(u); n     } n     auto s = solve(0, -1); n     vector<int> X; n     collect(0, -1, true, X); n     sort(X.begin(), X.end()); n     cout << s.second <<     << X.size() << endl; n     cout << X; n   } n }; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(nullptr); n   cout.tie(nullptr); n   DPowerTree 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 long long int f[100010], n, a[100010]; n const long long int inf = 2000000000000; n int main() { n   long long int i, j, k; n   scanf( %I64d , &n); n   for (i = 1; i <= n; i++) { n     scanf( %I64d , &a[i]); n   } n   f[0] = 0; n   sort(a + 1, a + 1 + n); n   for (i = 1; i <= n; i++) { n     f[i] = f[i - 1] + 20; n     long long int t; n     t = lower_bound(a + 1, a + 1 + n, a[i] - 89) - a - 1; n     f[i] = min(f[i], f[t] + 50); n     t = lower_bound(a + 1, a + 1 + n, a[i] - 1439) - a - 1; n     f[i] = min(f[i], f[t] + 120); n     printf( %I64d n , f[i] - f[i - 1]); n   } n   return 0; n } n


#include <bits/stdc++.h> n long long read() { n   long long x = 0, f = 1; n   char c = getchar(); n   while (c <  0  || c >  9 ) { n     if (c ==  - ) f = -1; n     c = getchar(); n   } n   while (c >=  0  && c <=  9 ) { n     x = x * 10 + c -  0 ; n     c = getchar(); n   } n   return x * f; n } n using namespace std; n const long long maxn = 1e5 + 7; n const double eps = 1e-10; n const long long mod = 1e9 + 7; n long long n; n long long totA = 0, totB = 0; n long long casA[maxn * 2], casB[maxn * 2]; n long long tot = 0; n struct Node { n   long long set, score; n }; n Node node[maxn]; n bool cmp(Node a, Node b) { n   if (a.set < b.set) return 1; n   if (a.set == b.set && a.score < b.score) return 1; n   return 0; n } n int32_t main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   ; n   cin >> n; n   for (long long i = 1; i < 2 * maxn; i++) n     casA[i] = casB[i] = 9223372036854775807; n   bool flagA = 0, flagB = 0; n   for (long long i = 1; i <= n; i++) { n     long long x; n     cin >> x; n     if (x == 1) n       casA[++totA] = i; n     else n       casB[++totB] = i; n     if (i == n) { n       if (x == 1) n         flagA = 1; n       else n         flagB = 1; n     } n   } n   for (long long i = n; i >= 1; i--) { n     long long winA = 0, winB = 0, tarA = 0, tarB = 0; n     long long now = 1; n     bool flag = 1; n     while (now <= n) { n       if (casA[tarA + i] == 9223372036854775807 && n           casB[tarB + i] == 9223372036854775807) { n         flag = 0; n         break; n       } n       if (casA[tarA + i] < casB[tarB + i]) { n         winA++; n         tarA += i; n         tarB = casA[tarA] - tarA; n         now = casA[tarA] + 1; n       } else { n         winB++; n         tarB += i; n         tarA = casB[tarB] - tarB; n         now = casB[tarB] + 1; n       } n     } n     if (winA == winB) flag = 0; n     if (winA > winB && flagB) flag = 0; n     if (winA < winB && flagA) flag = 0; n     if (flag) { n       node[++tot].set = max(winA, winB); n       node[tot].score = i; n     } n   } n   sort(node, node + tot, cmp); n   cout << tot <<   n ; n   if (tot) { n     for (long long i = 1; i <= tot; i++) n       cout << node[i].set <<     << node[i].score <<   n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 3E5 + 5; n int n, m, sz, val, x, y, color[MAXN]; n vector<int> v[MAXN], G[MAXN]; n template <class T> n void read(T& x, T f = 1, char ch = getchar()) { n   x = 0; n   while (ch <  0  || ch >  9 ) f = (ch ==  -  ? -1 : 1), ch = getchar(); n   while (ch >=  0  && ch <=  9 ) x = x * 10 + ch - 48, ch = getchar(); n   x *= f; n } n void dfs(int now, int f) { n   map<int, int> vis; n   for (int i = 0; i < v[now].size(); i++) n     if (color[v[now][i]]) vis[color[v[now][i]]] = 1; n   int l = 0; n   for (int i = 0; i < v[now].size(); i++) { n     if (!color[v[now][i]]) { n       while (vis[++l]) n         ; n       color[v[now][i]] = l; n     } n   } n   for (int i = 0; i < G[now].size(); i++) { n     int to = G[now][i]; n     if (to != f) dfs(to, now); n   } n } n signed main() { n   read(n), read(m); n   int ans = 1; n   for (int i = 1; i <= n; i++) { n     read(sz); n     for (int j = 1; j <= sz; j++) { n       read(val); n       v[i].push_back(val); n     } n     ans = max(ans, sz); n   } n   for (int i = 1; i <= n - 1; i++) { n     read(x), read(y); n     G[x].push_back(y), G[y].push_back(x); n   } n   dfs(1, 1); n   printf( %d n , ans); n   for (int i = 1; i <= m; i++) printf( %d  , color[i] ? color[i] : 1); n   return 0; n } n


#include <bits/stdc++.h> n #pragma GCC optimize( Ofast ) n #pragma GCC optimize( unroll-loops ) n #pragma GCC optimize( -O2 ) n using namespace std; n const long long N = 500000 + 10; n const long long MOD = 1000000000 + 7; n const long long INF = 100000000000000010; n const long long LOG = 25; n long long dp[N], n, a[N], lazy[N << 2], m, b[N], seg[N << 2], p[N], koj[N]; n void modify(long long id, long long x) { n   seg[id] += x; n   lazy[id] += x; n } n void shift(long long id) { n   modify(id << 1, lazy[id]); n   modify(id << 1 | 1, lazy[id]); n   lazy[id] = 0; n } n void add(long long id, long long lq, long long rq, long long x, long long l, n          long long r) { n   if (rq <= l || r <= lq) return; n   if (lq <= l && r <= rq) { n     modify(id, x); n     return; n   } n   long long md = (l + r) >> 1; n   shift(id); n   add(id << 1, lq, rq, x, l, md); n   add(id << 1 | 1, lq, rq, x, md, r); n   seg[id] = seg[id << 1] + seg[id << 1 | 1]; n } n long long get(long long id, long long lq, long long rq, long long l, n               long long r) { n   if (rq <= l || r <= lq) return 0; n   if (lq <= l && r <= rq) return seg[id]; n   shift(id); n   long long md = (l + r) >> 1; n   return get(id << 1, lq, rq, l, md) + get(id << 1 | 1, lq, rq, md, r); n } n void Set(long long id, long long lq, long long rq, long long x, long long l, n          long long r) { n   if (rq <= l || r <= lq) return; n   if (lq <= l && r <= rq) { n     seg[id] = x; n     return; n   } n   long long md = (l + r) >> 1; n   shift(id); n   Set(id << 1, lq, rq, x, l, md); n   Set(id << 1 | 1, lq, rq, x, md, r); n   seg[id] = seg[id << 1] + seg[id << 1 | 1]; n } n int32_t main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   cin >> n; n   for (long long i = 1; i <= n; i++) { n     cin >> a[i]; n   } n   for (long long i = 1; i <= n; i++) cin >> p[i]; n   cin >> m; n   for (long long i = 1; i <= m; i++) { n     cin >> b[i]; n     koj[b[i]] = i; n   } n   add(1, 1, m + 1, INF, 0, m + 1); n   for (long long i = 1; i <= n; i++) { n     if (koj[a[i]] == 0) { n       long long x = lower_bound(b + 1, b + m + 1, a[i]) - b; n       add(1, 0, x, p[i], 0, m + 1); n       long long zed = min(0ll, p[i]); n       add(1, x, m + 1, zed, 0, m + 1); n     } else { n       long long j = koj[a[i]]; n       long long zed = min(0ll, p[i]); n       long long mn = n           min(get(1, j - 1, j, 0, m + 1), get(1, j, j + 1, 0, m + 1) + zed); n       Set(1, j, j + 1, mn, 0, m + 1); n       add(1, 0, j, p[i], 0, m + 1); n       add(1, j + 1, m + 1, zed, 0, m + 1); n     } n   } n   long long ans = get(1, m, m + 1, 0, m + 1); n   if (ans > 1000000000000000) return cout <<  NO , 0; n   cout <<  YES n ; n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = (int)1e5 + 100; n double alpha[maxn], beta[maxn]; n double a[maxn], b[maxn]; n int main() { n   int n; n   scanf( %d , &n); n   for (int i = 0; i < n; i++) scanf( %lf , &beta[i]); n   for (int i = 0; i < n; i++) scanf( %lf , &alpha[i]); n   double Sa = 0, Sb = 0; n   for (int i = 0; i < n; i++) { n     double c = alpha[i] + beta[i]; n     double A = 1, B = -(c + (Sb - Sa)), C = beta[i] - c * Sa; n     double x1 = (-B + sqrt(max(0., B * B - 4. * A * C))) / 2.; n     double y1 = c - x1; n     a[i] = x1; n     b[i] = y1; n     Sa += a[i]; n     Sb += b[i]; n   } n   cout.precision(15); n   for (int i = 0; i < n; i++) printf( %.9lf  , a[i]); n   printf(  n ); n   for (int i = 0; i < n; i++) printf( %.9lf  , b[i]); n   printf(  n ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 5010; n const int mod = 1e9 + 7; n const long long INF = 1e15; n int nex[N]; n long long x[N], a[N], b[N], c[N], d[N]; n int main() { n   int n, s, e; n   while (~scanf( %d%d%d , &n, &s, &e)) { n     nex[s] = e; n     for (int i = 1; i <= n; i++) scanf( %I64d , &x[i]); n     for (int i = 1; i <= n; i++) scanf( %I64d , &a[i]); n     for (int i = 1; i <= n; i++) scanf( %I64d , &b[i]); n     for (int i = 1l; i <= n; i++) scanf( %I64d , &c[i]); n     for (int i = 1; i <= n; i++) scanf( %I64d , &d[i]); n     for (int i = 1; i <= n; i++) { n       if (i == s || i == e) continue; n       long long mi = INF, id; n       for (int j = s; j != e; j = nex[j]) { n         long long tmpans = 0; n         int from = j, to = i; n         if (to < from) n           tmpans += abs(x[from] - x[to]) + c[from] + b[to]; n         else n           tmpans += abs(x[from] - x[to]) + d[from] + a[to]; n         from = i, to = nex[j]; n         if (to < from) n           tmpans += abs(x[from] - x[to]) + c[from] + b[to]; n         else n           tmpans += abs(x[from] - x[to]) + d[from] + a[to]; n         from = j, to = nex[j]; n         if (to < from) n           tmpans -= abs(x[from] - x[to]) + c[from] + b[to]; n         else n           tmpans -= abs(x[from] - x[to]) + d[from] + a[to]; n         if (mi > tmpans) { n           mi = tmpans; n           id = j; n         } n       } n       int tmpnex = nex[id]; n       nex[id] = i; n       nex[i] = tmpnex; n     } n     long long ans = 0; n     for (int i = s; i != e; i = nex[i]) { n       long long tmpans = 0; n       int from = i, to = nex[i]; n       if (to < from) n         tmpans += abs(x[from] - x[to]) + c[from] + b[to]; n       else n         tmpans += abs(x[from] - x[to]) + d[from] + a[to]; n       ans += tmpans; n     } n     printf( %I64d n , ans); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n using namespace chrono; n long long int ceil(long long int r, long long int d) { n   return (r / d) + (bool)(r % d); n } n void inputAArray(int siz, vector<int> &arr) { n   for (int i = 0; i < siz; i++) arr[i] = 1 + rand(); n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   srand(time(0)); n   int t; n   cin >> t; n   while (t--) { n     string s; n     cin >> s; n     int n = s.length(); n     if (n & 1) { n       cout <<  NO  << endl; n       continue; n     } n     if (s.substr(0, n / 2) == s.substr(n / 2)) n       cout <<  YES  << endl; n     else n       cout <<  NO  << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n double n, l, v1, v2, k; n bool c(double x) { n   int num; n   if ((int)n % (int)k == 0) n     num = n / k; n   else n     num = n / k + 1; n   if (num * x + (num - 1) * x * (v2 - v1) / (v2 + v1) <= x + (l - x * v2) / v1) n     return 1; n   return 0; n } n int main() { n   scanf( %lf%lf%lf%lf%lf , &n, &l, &v1, &v2, &k); n   double lb = 0, rb = 1000000001; n   for (int i = 0; i < 100; i++) { n     double m = (lb + rb) / 2; n     if (c(m)) n       lb = m; n     else n       rb = m; n   } n   printf( %lf , rb + (l - rb * v2) / v1); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int t, n, m, i, ans, a; n int main() { n   cin >> t; n   while (t--) { n     cin >> n >> m; n     ans = 0; n     for (i = 1; i <= n; i++) { n       cin >> a; n       ans += a; n     } n     if (ans == m) n       cout <<  YES  << endl; n     else n       cout <<  NO  << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T> n inline bool chkmin(T &a, const T &b) { n   return b < a ? a = b, 1 : 0; n } n template <typename T> n inline bool chkmax(T &a, const T &b) { n   return a < b ? a = b, 1 : 0; n } n const int oo = 0x3f3f3f3f; n const int maxn = 1000100; n int n; n char a[maxn + 5]; n inline bool check(int first) { n   vector<int> man, aster; n   for (int i = (0), i_end_ = (n); i < i_end_; ++i) { n     if (a[i] ==  P ) n       man.push_back(i); n     else if (a[i] ==  * ) n       aster.push_back(i); n   } n   int nman = (int((man).size())), naster = (int((aster).size())); n   static bool dp[maxn + 5][2]; n   memset(dp, 0, sizeof dp); n   int j = nman - 1; n   dp[naster][0] = dp[naster][1] = 1; n   for (int i = naster - 1; i >= 0; --i) { n     j = min((int((man).size())) - 1, j + 1); n     while (j >= 0 && man[j] > aster[i]) { n       bool ok = 0; n       if (man[j] - first <= aster[i]) { n         int tmp = man[j]; n         if (j && man[j - 1] > aster[i]) chkmax(tmp, man[j - 1] + first); n         int fir = n             upper_bound((aster).begin(), (aster).end(), tmp) - aster.begin(); n         if (fir == naster || n             (j + 1 < (int((man).size())) && aster[fir] > man[j + 1])) n           ok = dp[fir][1]; n         else n           ok = dp[fir][0]; n       } n       if (ok) { n         dp[i][0] = 1; n         break; n       } n       --j; n     } n     if (dp[i][0]) n       dp[i][1] = 1; n     else { n       if (j >= 0) { n         int tmp = man[j] + first; n         int fir = n             upper_bound((aster).begin(), (aster).end(), tmp) - aster.begin(); n         if (fir == naster || n             (j + 1 < (int((man).size())) && aster[fir] > man[j + 1])) { n           dp[i][1] = dp[fir][1]; n         } else n           dp[i][1] = dp[fir][0]; n       } n     } n   } n   return dp[0][1]; n } n int main() { n   scanf( %d , &n); n   scanf( %s , a); n   bool unique = 1; n   int p = -1; n   int Max = 0, Min = oo; n   int tot = 0, left = 0; n   for (int i = (0), i_end_ = (n); i < i_end_; ++i) n     if (a[i] ==  P ) { n       if (~p) { n         unique = 0; n       } else { n         left = tot; n         p = i; n       } n     } else if (a[i] ==  * ) { n       ++tot; n       chkmin(Min, i), chkmax(Max, i); n     } n   if (unique) { n     int right = tot - left; n     int left_dis = p - Min; n     int right_dis = Max - p; n     auto tmp = max(make_pair(left, -left_dis), make_pair(right, -right_dis)); n     tmp.second = -tmp.second; n     printf( %d %d n , tmp.first, tmp.second); n   } else { n     int l = 0, r = n; n     while (l < r) { n       int mid = (l + r) >> 1; n       if (check(mid)) n         r = mid; n       else n         l = mid + 1; n     } n     printf( %d %d n , tot, l); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 1000005; n int N, M, dp[MAXN], res = 0; n vector<pair<int, int>> adj[MAXN]; n int check(int v) { n   if (dp[v]) return dp[v]; n   dp[v] = 1; n   for (auto p : adj[v]) { n     int n = p.first; n     if (check(n) + 1 > dp[v]) { n       dp[v] = dp[n] + 1; n       res = max(res, p.second); n     } n   } n   return dp[v]; n } n int main() { n   scanf( %d%d , &N, &M); n   for (int i = 0; i < M; i++) { n     int a, b; n     scanf( %d%d , &a, &b); n     a -= 1, b -= 1; n     adj[a].push_back({b, i + 1}); n   } n   for (int i = 0; i < N; i++) { n     if (check(i) == N) { n       printf( %d , res); n       return 0; n     } n   } n   printf( -1 ); n } n


#include <bits/stdc++.h> n using namespace std; n const int INF = 1000000000; n template <typename T> n using V = std::vector<T>; n struct debugger { n   template <typename T> n   debugger& operator,(const T& v) { n     cerr << v <<    ; n     return *this; n   } n } dbg; n int gcd(int a, int b) { return a ? gcd(b % a, a) : b; } n int main() { n   int n, m, k; n   scanf( %d , &n), scanf( %d , &m), scanf( %d , &k); n   int d = gcd(n, k); n   int n1 = n / d; n   k /= d; n   d = gcd(m, k); n   int m1 = m / d; n   k /= d; n   if (k > 2) { n     puts( NO ); n     return 0; n   } n   puts( YES ); n   if (k == 1) { n     if (n1 * 2 <= n) n       n1 *= 2; n     else n       m1 *= 2; n   } n   printf( 0 0 n0 %d n%d 0 n , m1, n1); n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   cin >> n; n   pair<string, string> arr[n]; n   for (int i = 0; i < n; i++) cin >> arr[i].first >> arr[i].second; n   for (int i = 0; i < n; i++) n     if (arr[i].second ==  rat ) cout << arr[i].first << endl; n   for (int i = 0; i < n; i++) n     if (arr[i].second ==  woman  || arr[i].second ==  child ) n       cout << arr[i].first << endl; n   for (int i = 0; i < n; i++) n     if (arr[i].second ==  man ) cout << arr[i].first << endl; n   for (int i = 0; i < n; i++) n     if (arr[i].second ==  captain ) cout << arr[i].first << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n std::map<long long, long long> factors; n void primeFactors(long long n) { n   while (n % 2 == 0) { n     factors[2]++; n     n = n / 2; n   } n   for (long long i = 3; i <= sqrt(n); i = i + 2) { n     while (n % i == 0) { n       factors[i]++; n       n = n / i; n     } n   } n   if (n > 2) factors[n]++; n } n long long i, j; n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   long long n; n   cin >> n; n   long long ans = 0; n   for (i = 2; i <= n; i++) { n     long long x = n / (i); n     ans += (i) * (x - 1); n   } n   cout << ans * 4 <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long powmod(long long a, long long b) { n   if (b == 0) return 1; n   long long x = powmod(a, b / 2); n   long long y = (x * x) % 1000000007; n   if (b % 2) return (a * y) % 1000000007; n   return y % 1000000007; n } n long long mulmod(long long a, long long b, long long c) { n   long long x = 0, y = a % c; n   while (b > 0) { n     if (b % 2 == 1) { n       x = (x + y) % c; n     } n     y = (y * 2) % c; n     b /= 2; n   } n   return x % c; n } n int main() { n   long long a, b, x, n; n   cin >> a >> b >> n >> x; n   long long nm1, nm2, nm3, nm4, nm5; n   if (a > 1) { n     nm1 = powmod(a, n); n     nm2 = (nm1 - 1 + 1000000007) % 1000000007; n     nm3 = (x * nm1) % 1000000007; n     nm4 = (a - 1 + 1000000007) % 1000000007; n     nm5 = (nm2 * powmod(nm4, 1000000007 - 2)) % 1000000007; n     long long nm6 = (nm3 + (nm5 * b) % 1000000007) % 1000000007; n     cout << nm6 << endl; n   } else n     cout << (x + mulmod(b, n, 1000000007)) % 1000000007 << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n char buf[1 << 21], *p1 = buf, *p2 = buf; n inline long long qread() { n   register char c = n       (p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1 << 21, stdin), p1 == p2) n            ? EOF n            : *p1++); n   register long long x = 0, f = 1; n   while (c <  0  || c >  9 ) { n     if (c ==  - ) f = -1; n     c = (p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1 << 21, stdin), p1 == p2) n              ? EOF n              : *p1++); n   } n   while (c >=  0  && c <=  9 ) { n     x = (x << 3) + (x << 1) + c - 48; n     c = (p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1 << 21, stdin), p1 == p2) n              ? EOF n              : *p1++); n   } n   return x * f; n } n inline long long Abs(const long long& x) { return (x > 0 ? x : -x); } n inline long long Max(const long long& x, const long long& y) { n   return (x > y ? x : y); n } n inline long long Min(const long long& x, const long long& y) { n   return (x < y ? x : y); n } n long long n, l, wmax; n vector<long long> ml, mr; n inline long long Div(long long x, long long y) { n   if (!y) { n     if (x > 0) n       return 2147483647; n     else n       return -2147483647; n   } n   if (x % y < 0) x -= (y + x % y); n   return x / y; n } n inline void Read() { n   n = qread(); n   l = qread(); n   wmax = qread(); n   for (register long long i = 1; i <= n; i++) { n     register long long x = qread(), v = qread(); n     if (v == 1) n       mr.push_back(x); n     else n       ml.push_back(x); n   } n } n inline void Solve() { n   register long long siz = ml.size(); n   register long long ans = 0; n   for (register long long i = 0; i < siz; i++) { n     auto lmost = lower_bound(mr.begin(), mr.end(), -ml[i] - l); n     ans += upper_bound( n                mr.begin(), lmost, n                Min(Div((ml[i] + l) * (wmax + 1) - 1, wmax - 1), -ml[i] - l)) - n            mr.begin(); n     ans += n         upper_bound(lmost, mr.end(), n                     Min(Div((ml[i] + l) * (wmax - 1) - 1, wmax + 1), ml[i])) - n         lmost; n   } n   printf( %lld , ans); n } n signed main() { n   Read(); n   sort(ml.begin(), ml.end()); n   sort(mr.begin(), mr.end()); n   Solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long n, d, k, l, m, i, j, a[105], b[2]; n int main() { n   cin >> n >> d >> l; n   b[0] = n / 2; n   b[1] = n - b[0]; n   k = 0; n   if (d > l * b[1] - b[0] || d < b[1] - l * b[0]) { n     cout <<  -1  << endl; n     return 0; n   } else { n     for (i = 1; i <= n; i += 2) { n       a[i] = 1; n       k++; n     } n     for (i = 2; i <= n; i += 2) { n       a[i] = l; n       k -= l; n     } n   } n   i = 1; n   while (d != k && i <= n) { n     if (k + l - 1 <= d) { n       a[i] = l; n       k = k - 1 + l; n     } else { n       a[i] += d - k; n       k = d; n     } n     i += 2; n   } n   i = 2; n   while (d != k && i <= n) { n     if (k + l - 1 <= d) { n       a[i] = 1; n       k = k - 1 + l; n     } else { n       a[i] -= d - k; n       k = d; n     } n     i += 2; n   } n   cout << a[1]; n   for (i = 2; i <= n; i++) cout <<     << a[i]; n   cout << endl; n } n


#include <bits/stdc++.h> n using namespace std; n const long long mod = 998244353; n struct Node { n   long long val[4], len; n   Node() { n     memset(val, 0, sizeof(val)); n     len = 0; n   } n }; n vector<Node> tree; n long long n, m; n string s; n long long ch[100]; n long long choice(long long i, long long j = -1) { n   long long res = s[i] -  0 ; n   if (~j) res = res * 10 + s[j] -  0 ; n   return ch[res]; n } n Node join(Node &left, Node &right, long long l, long long r) { n   Node res; n   long long na = left.len == 1 ? 0 : 1; n   long long nb = right.len == 1 ? 0 : 2; n   res.len = left.len + right.len; n   res.val[0] = left.val[na] * right.val[nb] % mod; n   res.val[1] = left.val[na] * right.val[3] % mod; n   res.val[2] = left.val[3] * right.val[nb] % mod; n   res.val[3] = left.val[3] * right.val[3] % mod; n   if (s[l] ==  1 ) { n     na = left.len == 1 ? 2 : 0; n     nb = right.len == 1 ? 1 : 0; n     res.val[na + nb] += left.val[0] * right.val[0] % mod * choice(l, r) % mod; n     res.val[na + 1] += left.val[0] * right.val[1] % mod * choice(l, r) % mod; n     res.val[2 + nb] += left.val[2] * right.val[0] % mod * choice(l, r) % mod; n     res.val[3] += left.val[2] * right.val[1] % mod * choice(l, r) % mod; n   } n   for (long long i = 0; i < 4; ++i) res.val[i] %= mod; n   return res; n } n void build(long long node = 1, long long start = 0, long long end = n - 1) { n   tree[node].len = end - start + 1; n   if (start == end) { n     tree[node].val[0] = 1; n     tree[node].val[3] = s[start] -  0  + 1; n     return; n   } n   long long mid = start + end >> 1; n   build(node << 1, start, mid); n   build(node << 1 | 1, mid + 1, end); n   tree[node] = join(tree[node << 1], tree[node << 1 | 1], mid, mid + 1); n } n void update(long long id, long long val, long long node = 1, n             long long start = 0, long long end = n - 1) { n   if (start == end) { n     s[id] = char(val +  0 ); n     tree[node].val[3] = val + 1; n     return; n   } n   long long mid = start + end >> 1; n   if (id <= mid) n     update(id, val, node << 1, start, mid); n   else n     update(id, val, node << 1 | 1, mid + 1, end); n   tree[node] = join(tree[node << 1], tree[node << 1 | 1], mid, mid + 1); n } n signed main() { n   ios::sync_with_stdio(false); n   cin.tie(nullptr); n   cout.tie(nullptr); n   for (long long i = 0; i <= 9; ++i) { n     for (long long j = 0; j <= 9; ++j) ch[i + j]++; n   } n   cin >> n >> m; n   cin >> s; n   tree.resize((n + 10) << 2); n   build(); n   while (m--) { n     long long id, x; n     cin >> id >> x; n     id--; n     update(id, x); n     cout << tree[1].val[3] <<   n ; 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   int n; n   cin >> n; n   map<int, vector<int> > pos; n   map<int, int> result; n   for (int i = 0; i < n; i++) { n     int t; n     cin >> t; n     if (pos.find(t) == pos.end()) { n       vector<int> temp; n       temp.push_back(i + 1); n       pos.insert(pair<int, vector<int> >(t, temp)); n     } else { n       pos[t].push_back(i + 1); n     } n   } n   for (map<int, vector<int> >::iterator iter = pos.begin(); iter != pos.end(); n        iter++) { n     int len = iter->second.size(); n     if (len == 1) { n       result.insert(pair<int, int>(iter->first, 0)); n       continue; n     } n     int count = 2; n     int diff = iter->second[1] - iter->second[0]; n     for (int i = 1; i < len - 1; i++) { n       if (diff != iter->second[i + 1] - iter->second[i]) break; n       count++; n     } n     if (count == len) result.insert(pair<int, int>(iter->first, diff)); n   } n   cout << result.size() << endl; n   for (map<int, int>::iterator iter = result.begin(); iter != result.end(); n        iter++) n     cout << iter->first <<     << iter->second << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int trees(vector<vector<int> >& v, int a, int b, int y, int x) { n   int trees = 0; n   for (int i = y; i < (y + a); ++i) { n     for (int j = x; j < (x + b); ++j) { n       trees += v[i][j]; n     } n   } n   return trees; n } n int main() { n   int n, m; n   cin >> n >> m; n   vector<vector<int> > v(n); n   int totTree = 0; n   for (int i = 0; i < n; ++i) { n     for (int j = 0; j < m; ++j) { n       int tmp; n       cin >> tmp; n       v[i].push_back(tmp); n       totTree += tmp; n     } n   } n   int a, b; n   cin >> a >> b; n   int minTree = totTree; n   for (int i = 0; i + a - 1 < n; ++i) { n     for (int j = 0; j + b - 1 < m; ++j) { n       const int tr = trees(v, a, b, i, j); n       if (tr < minTree) { n         minTree = tr; n       } n     } n   } n   for (int i = 0; i + b - 1 < n; ++i) { n     for (int j = 0; j + a - 1 < m; ++j) { n       const int tr = trees(v, b, a, i, j); n       if (tr < minTree) { n         minTree = tr; n       } n     } n   } n   cout << minTree << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   string s; n   int n, m, len; n   cin >> n >> m; n   if (n >= 1 && n <= 100 && m >= 1 && m <= 100) { n     cin >> s; n     len = s.size(); n     if (len == n) { n       int l, r, z; n       char c1, c2; n       while (m--) { n         cin >> l >> r >> c1 >> c2; n         if (l >= 1 && l <= r && r <= n && c1 >=  a  && c1 <=  z  && c2 >=  a  && n             c2 <=  z ) n           for (z = l - 1; z <= r - 1; z++) { n             if (s[z] == c1) { n               s[z] = c2; n             } n           } n       } n       cout << s << endl; n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int mod = 998244353, inv2 = mod + 1 >> 1; n inline void add(int &a, int b) { (a += b) >= mod && (a -= mod); } n inline int Add(int a, int b) { return add(a, b), a; } n inline void sub(int &a, int b) { (a -= b) < 0 && (a += mod); } n inline int Sub(int a, int b) { return sub(a, b), a; } n inline void mul(int &a, int b) { a = 1ll * a * b % mod; } n inline int Mul(int a, int b) { return 1ll * a * b % mod; } n const int N = 40, M = 1 << 20; n int m, n, S, f[N][M], g[M], siz[M], x[M], y[M], res[M], ans[N]; n long long b[M]; n inline void XOR(int *a, int n, bool rev) { n   for (int k = 2, m = 1; k <= n; k <<= 1, m <<= 1) { n     for (int j = 0; j < n; j += k) n       for (int i = 0; i < m; ++i) { n         int x = a[j + i], y = a[j + i + m]; n         if (!rev) { n           a[j + i] = Add(x, y); n           a[j + i + m] = Sub(x, y); n         } else { n           a[j + i] = Mul(Add(x, y), inv2); n           a[j + i + m] = Mul(Sub(x, y), inv2); n         } n       } n   } n } n inline void FWT(int *a, int *b, int n) { n   memcpy(x, a, sizeof(x)), memcpy(y, b, sizeof(y)); n   XOR(x, n, 0), XOR(y, n, 0); n   for (int i = 0; i < n; ++i) res[i] = Mul(x[i], y[i]); n   XOR(res, n, 1); n } n void dfs1(long long x, int y) { n   if (y == m) return ++g[x], void(); n   dfs1(x, y + 1); n   if (b[y]) dfs1(x ^ b[y], y + 1); n } n void dfs2(long long x, int y) { n   if (y == n) return ++f[siz[x >> m]][x & (S - 1)], void(); n   dfs2(x, y + 1); n   if (b[y]) dfs2(x ^ b[y], y + 1); n } n int main() { n   scanf( %d%d , &m, &n); n   int tot = 1; n   for (int i = 1; i <= m; ++i) { n     long long x; n     scanf( %lld , &x), mul(tot, 2); n     for (int k = n - 1; ~k; --k) n       if (x >> k & 1) { n         if (!b[k]) { n           b[k] = x; n           mul(tot, inv2); n           break; n         } n         x ^= b[k]; n       } n   } n   m = n + 1 >> 1, S = 1 << m; n   for (int i = 0; i < S; ++i) siz[i] = siz[i >> 1] + (i & 1); n   dfs1(0, 0), dfs2(0, m); n   for (int i = 0; i <= n - m; ++i) { n     FWT(f[i], g, S); n     for (int s = 0; s < S; ++s) add(ans[i + siz[s]], res[s]); n   } n   for (int i = 0; i <= n; ++i) printf( %d  , Mul(ans[i], tot)); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int v[1005]; n int a[1005], b[1005]; n char s[1005]; n vector<int> c, d; n int main() { n   cin >> s + 1; n   int len = strlen(s + 1); n   int num = 0; n   for (int i = 1; i <= len; i++) { n     if (s[i] ==  ( ) { n       a[i]++; n       c.push_back(i); n     } n     a[i] += a[i - 1]; n   } n   for (int i = len; i; i--) { n     if (s[i] ==  ) ) { n       d.push_back(i); n       b[i]++; n     } n     b[i] += b[i - 1]; n   } n   sort((d).begin(), (d).end()); n   int l = 0, r = d.size() - 1; n   while (l < c.size() && r >= 0 && c[l] < d[r]) { n     l++; n     r--; n     num++; n   } n   if (!num) n     puts( 0 ); n   else { n     puts( 1 ); n     cout << l + d.size() - r - 1 << endl; n     for (int i = 0; i < l; i++) cout << c[i] <<    ; n     for (int i = r + 1; i < d.size(); i++) cout << d[i] <<    ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   ; n   long long t; n   cin >> t; n   while (t--) { n     long long a, b, n, m; n     cin >> a >> b >> n >> m; n     if (a + b < n + m) { n       cout <<  no  n            <<   n ; n     } else { n       long long v1 = min(a, b); n       long long v2 = max(a, b); n       if (v1 >= m) { n         cout <<  yes  n              <<   n ; n       } else { n         cout <<  no  n              <<   n ; n       } n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n int n; n char str[250000]; n int main() { n   while (~scanf( %d , &n)) { n     scanf( %s , str); n     int len = strlen(str); n     int sum1 = 0, sum2 = 0; n     for (int i = 0; i < len; i++) { n       if (str[i] ==  < ) { n         sum1++; n       } else { n         break; n       } n     } n     for (int i = len - 1; i >= 0; i--) { n       if (str[i] ==  > ) { n         sum2++; n       } else { n         break; n       } n     } n     printf( %d n , sum1 + sum2); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MaxN = 100009; n unsigned long long a[MaxN], b[MaxN], c[MaxN]; n int main() { n   unsigned long long n, ans = 0, p, sum = 0, sum2 = 0; n   cin >> n >> p; n   for (int i = 0; i < n; i++) cin >> a[i]; n   for (int i = 0; i < n; i++) { n     sum += a[i]; n     b[i] = sum % p; n   } n   for (int i = n - 1; i >= 0; i--) { n     sum2 += a[i]; n     c[i] = sum2 % p; n   } n   for (int i = 0; i < n; i++) ans = max(ans, (b[i] + c[i + 1])); n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n #pragma GCC optimize( O3 ) n const long long MOD = 1e9 + 7; n long long n_bits(long long n) { n   long long x = __builtin_popcount(n); n   return x; n } n int pow(int a, int b, int m) { n   int ans = 1; n   while (b) { n     if (b & 1) ans = (ans * a) % m; n     b /= 2; n     a = (a * a) % m; n   } n   return ans; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   long long t = 1; n   while (t--) { n     int n, p; n     cin >> n >> p; n     long long sum = 0; n     int a[n]; n     long long left = 0; n     for (int i = 0; i < n; i++) { n       cin >> a[i]; n       sum += a[i]; n     } n     long long final = (sum - a[n - 1]) % p + a[n - 1] % p; n     sum -= a[n - 1]; n     left = a[n - 1]; n     for (int i = n - 2; i > 0; i--) { n       left += a[i]; n       sum -= a[i]; n       final = max(final, left % p + sum % p); n     } n     cout << final; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n using ll = long long; n template <class t, class u> n void chmax(t& first, u second) { n   if (first < second) first = second; n } n template <class t, class u> n void chmin(t& first, u second) { n   if (second < first) first = second; n } n template <class t> n using vc = vector<t>; n template <class t> n using vvc = vc<vc<t>>; n using pi = pair<ll, ll>; n using vi = vc<ll>; n template <class t, class u> n ostream& operator<<(ostream& os, const pair<t, u>& p) { n   return os <<  {  << p.first <<  ,  << p.second <<  } ; n } n template <class t> n ostream& operator<<(ostream& os, const vc<t>& v) { n   os <<  { ; n   for (auto e : v) os << e <<  , ; n   return os <<  } ; n } n using uint = unsigned; n using ull = unsigned long long; n template <class t, size_t n> n ostream& operator<<(ostream& os, const array<t, n>& first) { n   return os << vc<t>(first.begin(), first.end()); n } n template <ll i, class T> n void print_tuple(ostream&, const T&) {} n template <ll i, class T, class H, class... Args> n void print_tuple(ostream& os, const T& t) { n   if (i) os <<  , ; n   os << get<i>(t); n   print_tuple<i + 1, T, Args...>(os, t); n } n template <class... Args> n ostream& operator<<(ostream& os, const tuple<Args...>& t) { n   os <<  { ; n   print_tuple<0, tuple<Args...>, Args...>(os, t); n   return os <<  } ; n } n template <class t> n void print(t x, ll suc = 1) { n   cout << x; n   if (suc == 1) cout <<   n ; n   if (suc == 2) cout <<    ; n } n ll read() { n   ll i; n   cin >> i; n   return i; n } n vi readvi(ll n, ll off = 0) { n   vi v(n); n   for (ll i = ll(0); i < ll(n); i++) v[i] = read() + off; n   return v; n } n template <class T> n void print(const vector<T>& v, ll suc = 1) { n   for (ll i = ll(0); i < ll(v.size()); i++) n     print(v[i], i == ll(v.size()) - 1 ? suc : 2); n } n string readString() { n   string s; n   cin >> s; n   return s; n } n template <class T> n T sq(const T& t) { n   return t * t; n } n void yes(bool ex = true) { n   cout <<  Yes  n        <<   n ; n   if (ex) exit(0); n } n void no(bool ex = true) { n   cout <<  No  n        <<   n ; n   if (ex) exit(0); n } n void possible(bool ex = true) { n   cout <<  Possible  n        <<   n ; n   if (ex) exit(0); n } n void impossible(bool ex = true) { n   cout <<  Impossible  n        <<   n ; n   if (ex) exit(0); n } n constexpr ll ten(ll n) { return n == 0 ? 1 : ten(n - 1) * 10; } n const ll infLL = LLONG_MAX / 3; n const ll inf = infLL; n ll topbit(signed t) { return t == 0 ? -1 : 31 - __builtin_clz(t); } n ll topbit(ll t) { return t == 0 ? -1 : 63 - __builtin_clzll(t); } n ll botbit(signed first) { return first == 0 ? 32 : __builtin_ctz(first); } n ll botbit(ll first) { return first == 0 ? 64 : __builtin_ctzll(first); } n ll popcount(signed t) { return __builtin_popcount(t); } n ll popcount(ll t) { return __builtin_popcountll(t); } n bool ispow2(ll i) { return i && (i & -i) == i; } n ll mask(ll i) { return (ll(1) << i) - 1; } n bool inc(ll first, ll second, ll c) { return first <= second && second <= c; } n template <class t> n void mkuni(vc<t>& v) { n   sort(v.begin(), v.end()); n   v.erase(unique(v.begin(), v.end()), v.end()); n } n ll rand_int(ll l, ll r) { n   static mt19937_64 gen(chrono::steady_clock::now().time_since_epoch().count()); n   return uniform_int_distribution<ll>(l, r)(gen); n } n template <class t> n void myshuffle(vc<t>& first) { n   for (ll i = ll(0); i < ll(ll(first.size())); i++) n     swap(first[i], first[rand_int(0, i)]); n } n template <class t> n ll lwb(const vc<t>& v, const t& first) { n   return lower_bound(v.begin(), v.end(), first) - v.begin(); n } n signed main() { n   cin.tie(0); n   ios::sync_with_stdio(0); n   cout << fixed << setprecision(20); n   ll n, m; n   cin >> n >> m; n   vi lv = readvi(n, -1); n   vi cost = readvi(n); n   vi c = readvi(n + m); n   void(0); n   vvc<ll> dp(m); n   for (ll i = ll(0); i < ll(n); i++) dp[lv[i]].push_back(-inf); n   { n     ll pre = 0; n     for (ll i = ll(0); i < ll(m); i++) { n       pre >>= 1; n       pre += ll(dp[i].size()); n       if (ll(dp[i].size())) dp[i].resize(pre, -inf); n     } n   } n   ll ans = 0; n   for (ll i = ll(n) - 1; i >= ll(0); i--) { n     for (ll j = ll(ll(dp[lv[i]].size()) - 1) - 1; j >= ll(0); j--) n       chmax(dp[lv[i]][j + 1], dp[lv[i]][j] - cost[i]); n     chmax(dp[lv[i]][0], -cost[i]); n     for (ll j = ll(0); j < ll(lv[i]); j++) { n       ll ad = 0; n       for (ll k = ll(0); k < ll(ll(dp[j].size())); k++) { n         ll u = popcount(k ^ (k + 1)); n         for (ll l = ll(0); l < ll(min(u, lv[i] - j)); l++) ad += c[j + l]; n         void(0); n         void(0); n         chmax(dp[lv[i]][(k + 1) >> min(ll(60), lv[i] - j)], n               dp[j][k] + ad - cost[i]); n       } n     } n   } n   void(0); n   for (ll i = ll(0); i < ll(m); i++) n     for (ll j = ll(0); j < ll(ll(dp[i].size())); j++) { n       ll cur = i, cnt = j + 1; n       ll val = dp[i][j]; n       void(0); n       while (cnt) { n         val += c[cur] * cnt; n         cur++; n         cnt >>= 1; n       } n       if (val == 7) { n         void(0); n         void(0); n       } n       chmax(ans, val); n     } n   print(ans); n } n


#include <bits/stdc++.h> n using namespace std; n string s; n int x, y, len, a, b; n int main() { n   cin >> s; n   len = s.length(); n   for (int i = 0; i < len; ++i) { n     if (s[i] ==  ( ) n       ++x; n     else if (s[i] ==  ) ) n       --x; n     else if (s[i] ==  # ) n       --x, ++y; n     if (x < 0) { n       puts( -1 ); n       exit(0); n     } n   } n   for (int i = 0; i < len; ++i) { n     if (s[i] ==  # ) { n       ++a; n       if (a == y) b -= x; n       --b; n     } n     if (s[i] ==  ( ) n       ++b; n     else if (s[i] ==  ) ) n       --b; n     if (b < 0) { n       puts( -1 ); n       exit(0); n     } n   } n   for (int i = 1; i < y; ++i) puts( 1 ); n   printf( %d , x + 1); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m, a[105][105]; n int sum[2][105]; n bool inv[2][105]; n int main() { n   while (scanf( %d%d , &n, &m) == 2) { n     memset(inv, false, sizeof(inv)); n     memset(sum, 0, sizeof(sum)); n     for (int i = 0; i < n; i++) { n       for (int j = 0; j < m; j++) { n         scanf( %d , a[i] + j); n         sum[0][i] += a[i][j]; n         sum[1][j] += a[i][j]; n       } n     } n     while (1) { n       int idx = -1; n       for (int i = 0; i < n; i++) { n         if (sum[0][i] < 0) { n           idx = i; n           break; n         } n       } n       if (idx != -1) { n         inv[0][idx] ^= 1; n         sum[0][idx] = -sum[0][idx]; n         for (int i = 0; i < m; i++) { n           sum[1][i] -= a[idx][i]; n           a[idx][i] = -a[idx][i]; n           sum[1][i] += a[idx][i]; n         } n         continue; n       } n       for (int i = 0; i < m; i++) { n         if (sum[1][i] < 0) { n           idx = i; n           break; n         } n       } n       if (idx != -1) { n         inv[1][idx] ^= 1; n         sum[1][idx] = -sum[1][idx]; n         for (int i = 0; i < n; i++) { n           sum[0][i] -= a[i][idx]; n           a[i][idx] = -a[i][idx]; n           sum[0][i] += a[i][idx]; n         } n         continue; n       } n       break; n     } n     int cnt = 0; n     for (int i = 0; i < n; i++) n       if (inv[0][i]) cnt++; n     printf( %d , cnt); n     for (int i = 0; i < n; i++) n       if (inv[0][i]) printf(  %d , i + 1); n     printf(  n ); n     cnt = 0; n     for (int i = 0; i < m; i++) n       if (inv[1][i]) cnt++; n     printf( %d , cnt); n     for (int i = 0; i < m; i++) n       if (inv[1][i]) printf(  %d , i + 1); n     printf(  n ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int inf = 2e9; n const long long INF = 8e18; n const int maxn = 2e5 + 5; n int n, k, cnt; n int a[maxn], b[maxn], p[maxn], fp[maxn], num[maxn]; n vector<int> zz[maxn]; n inline void init() { n   cin >> n >> k; n   for (int i = 1; i <= n; ++i) { n     int x; n     cin >> x; n     ++num[x]; n   } n   for (int i = 1; i <= k; ++i) { n     int x; n     cin >> x; n     if (!p[x]) p[x] = ++cnt, fp[cnt] = x; n     a[cnt] += num[i]; n     while (num[i]--) zz[cnt].push_back(i); n   } n   for (int i = 1; i <= cnt; ++i) b[i] = fp[i] - fp[i + 1]; n   int x = 0; n   for (int i = cnt; i >= 1; --i) { n     if (!a[i]) n       x += b[i]; n     else n       b[i] += x, x = 0; n   } n } n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   init(); n   vector<vector<int> > ans; n   vector<pair<int, int> > now, last; n   for (int i = cnt; i >= 1; --i) { n     if (!a[i]) continue; n     last.push_back({i, a[i]}); n   } n   while (!last.empty()) { n     int val = 0, z = 0; n     vector<int> res; n     for (auto &x : last) { n       int id = x.first, w = x.second; n       val += b[id]; n       b[id] += z; n       int mi = min(x.second, val); n       w -= mi; n       val -= mi; n       while (mi--) res.push_back(id); n       if (w) n         now.push_back({id, w}), z = 0; n       else n         z = b[id]; n     } n     ans.push_back(res); n     last = now; n     now.clear(); n   } n   cout << ans.size() <<   n ; n   for (auto &x : ans) { n     cout << x.size() <<    ; n     for (auto y : x) { n       cout << zz[y].back() <<    ; n       zz[y].pop_back(); n     } n     cout <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n char BUFFER[100000 + 5]; n bool readn(int &n) { return scanf( %d , &n) == 1; } n bool readl(long long &n) { return scanf( %I64d , &n) == 1; } n bool readd(double &n) { return scanf( %lf , &n) == 1; } n bool reads(string &s) { n   s =   ; n   int n = scanf( %s , BUFFER); n   if (n == 1) s = BUFFER; n   return n == 1; n } n bool readln(string &s) { n   char *valid = gets(BUFFER); n   if (valid) s = BUFFER; n   return ((bool)valid); n } n int Print(long long a, long long b) { n   cout << a <<     << b; n   return 0; n } n void update(long long &su, long long n, long long mod) { n   su *= n; n   su %= mod; n } n int doit(long long n, long long mod) { n   long long su = 1; n   int i; n   for (i = 1; i < n; i++) { n     su *= 10; n     su %= mod; n   } n   update(su, 3, mod); n   update(su, 3, mod); n   update(su, 5, mod); n   update(su, 2, mod); n   update(su, 3, mod); n   update(su, 3, mod); n   su++; n   su %= mod; n   long long a = 1, b = 1; n   for (i = 0; i < n; i++) b *= 10; n   if (0 == su) return Print(a, b); n   su = mod - su; n   return Print(a + su, b + su); n } n int main() { n   long long a; n   cin >> a; n   doit(18, a); n   printf(  n ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MOD = 1000000007; n long double PI = 4 * atan(1); n int n, r, val[2001], x, flag, ch[2001]; n bool isChild[2001][2001]; n pair<int, int> p[2001]; n vector<int> v[2001]; n queue<int> q1, q2, ans; n void solve(int i) { n   int rem = ch[i] - p[i].second; n   while (ans.size()) { n     x = ans.front(); n     if (isChild[i][x]) { n       if (rem > 0) { n         val[x]++; n         q2.push(x); n       } else { n         if (rem == 0) { n           q2.push(i); n           val[i] = val[x] + 1; n         } n         q2.push(x); n       } n       rem--; n     } else { n       q1.push(x); n     } n     ans.pop(); n   } n   if (rem == 0 || !q2.size()) { n     q2.push(i); n     val[i] = 1; n   } n   while (q1.size() && q2.size()) { n     if (val[q1.front()] >= val[q2.front()]) { n       ans.push(q1.front()); n       q1.pop(); n     } else { n       ans.push(q2.front()); n       q2.pop(); n     } n   } n   while (q1.size()) { n     ans.push(q1.front()); n     q1.pop(); n   } n   while (q2.size()) { n     ans.push(q2.front()); n     q2.pop(); n   } n } n void dfs(int i, int p) { n   for (int a : v[i]) { n     if (a != p) { n       dfs(a, i); n     } n   } n   solve(i); n   return; n } n void calc(int par, int i, int p) { n   ch[par]++; n   isChild[par][i] = true; n   for (int a : v[i]) { n     if (a != p) { n       calc(par, a, i); n     } n   } n } n int main() { n   ios::sync_with_stdio(0), cin.tie(0); n   cin >> n; n   for (int i = 1; i < n + 1; i++) { n     cin >> p[i].first >> p[i].second; n     if (p[i].first != 0) n       v[p[i].first].push_back(i); n     else n       r = i; n   } n   for (int i = 1; i < n + 1; i++) calc(i, i, p[i].first), ch[i]--; n   for (int i = 1; i < n + 1; i++) { n     if (p[i].second > ch[i]) { n       cout <<  NO ; n       return 0; n     } n   } n   dfs(r, 0); n   cout <<  YES n ; n   for (int i = 1; i < n + 1; i++) cout << val[i] <<    ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 50000 + 10; n struct query { n   int type, x, y; n } qu[maxn]; n int n, fa[maxn]; n long long val[maxn], sz[maxn]; n vector<int> v[maxn]; n bool mark[maxn], have[maxn]; n long long d[maxn]; n int dfs(int x) { n   sz[x] = 1; n   int ret = 0; n   for (auto i : v[x]) ret += dfs(i), sz[x] += sz[i]; n   for (auto i : v[x]) d[i] = (sz[x] - sz[i]) * val[x]; n   have[x] = ((ret || mark[x]) ? 1 : 0); n   if (ret >= 2 || mark[x]) { n     mark[x] = 1; n     return 1; n   } else n     return ret; n } n struct P { n   int to, ssum; n   long long dsum; n }; n vector<P> v2[maxn]; n int fa2[maxn]; n long long sz2[maxn], sz2sum[maxn]; n long long pnum[maxn]; n bool leaf[maxn]; n void dfs2(int x, bool isfir = 0) { n   if (v2[x].empty()) { n     if (!isfir) return; n     pnum[x] = sz[x] * sz[x]; n     for (auto i : v[x]) pnum[x] -= sz[i] * sz[i]; n     return; n   } n   sz[x] = 1 + sz2[x]; n   pnum[x] = 0; n   for (auto i : v2[x]) n     dfs2(i.to, isfir), sz[x] += sz[i.to] + i.ssum, n         pnum[x] -= (sz[i.to] + i.ssum) * (sz[i.to] + i.ssum); n   pnum[x] += sz[x] * sz[x] - sz2sum[x]; n } n int getid(int x, int y) { n   for (int i = 0;; i++) n     if (v2[x][i].to == y) return i; n } n void build_graph(int num) { n   fill(mark, mark + n + 1, 0); n   fill(have, have + n + 1, 0); n   fill(leaf, leaf + n + 1, 1); n   for (int i = 1; i <= n; i++) v[i].clear(), v2[i].clear(); n   for (int i = 2; i <= n; i++) v[fa[i]].push_back(i); n   for (int i = 0; i < num; i++) { n     mark[qu[i].x] = 1; n     if (qu[i].type == 1) mark[qu[i].y] = 1; n   } n   mark[1] = 1; n   dfs(1); n   for (int i = 1; i <= n; i++) n     if (mark[i]) { n       sz2[i] = sz2sum[i] = 0; n       for (auto j : v[i]) n         if (!have[j]) sz2[i] += sz[j], sz2sum[i] += sz[j] * sz[j]; n       if (i == 1) continue; n       int j, ssum = 0; n       long long dsum = 0LL; n       for (j = i;; j = fa[j]) { n         dsum += d[j]; n         if (mark[fa[j]]) break; n       } n       ssum = sz[j] - sz[i]; n       j = fa[j]; n       v2[j].push_back((P){i, ssum, dsum}); n       fa2[i] = j; n       leaf[j] = 0; n     } n } n bool first = 1; n void process(int num) { n   build_graph(num); n   long long nowans = 0LL, tot = (long long)n * n; n   for (int i = 1; i <= n; i++) { n     long long add = sz[i] * sz[i]; n     for (auto j : v[i]) add -= sz[j] * sz[j]; n     nowans += add * val[i]; n   } n   if (first) printf( %.15f n , ((double)nowans) / tot), first = 0; n   dfs2(1, 1); n   for (int i = 0; i < num; i++) { n     if (qu[i].type == 2) { n       int x = qu[i].x, nval = qu[i].y; n       nowans += pnum[x] * (nval - val[x]); n       for (auto &j : v2[x]) n         j.dsum += (sz[x] - j.ssum - sz[j.to]) * (nval - val[x]); n       val[x] = nval; n       printf( %.15f n , ((double)nowans) / tot); n       continue; n     } n     int x = qu[i].y, y = qu[i].x, id; n     for (int j = x; j != 1; j = fa2[j]) n       if (j == y || y == 1) { n         swap(x, y); n         break; n       } n     fa[y] = x; n     long long dif = 0LL; n     for (int j = y; j != 1; j = fa2[j]) { n       sz[fa2[j]] -= sz[y]; n       for (auto &k : v2[fa2[j]]) n         if (k.to != j) n           k.dsum -= val[fa2[j]] * sz[y]; n         else n           dif -= k.dsum; n     } n     int f = fa2[y]; n     id = getid(f, y); n     sz2[f] += v2[f][id].ssum; n     sz2sum[f] += (long long)v2[f][id].ssum * v2[f][id].ssum; n     swap(v2[f][id], v2[f][v2[f].size() - 1]); n     v2[f].pop_back(); n     v2[x].push_back((P){y, 0, val[x] * sz[x]}); n     fa2[y] = x; n     if (v2[f].empty()) pnum[f] = sz[f] * sz[f] - sz2sum[f]; n     for (int j = y; j != 1; j = fa2[j]) { n       sz[fa2[j]] += sz[y]; n       for (auto &k : v2[fa2[j]]) n         if (k.to != j) n           k.dsum += val[fa2[j]] * sz[y]; n         else n           dif += k.dsum; n     } n     nowans += 2 * dif * sz[y]; n     printf( %.15f n , ((double)nowans) / tot); n     dfs2(1); n   } n } n int main() { n   scanf( %d , &n); n   fa[1] = 1; n   for (int i = 2; i <= n; i++) scanf( %d , &fa[i]); n   for (int i = 1; i <= n; i++) scanf( %I64d , &val[i]); n   int Q; n   scanf( %d , &Q); n   int sq = (int)sqrt(Q + 0.5); n   for (int i = 0; i < Q; i++) { n     char c[5]; n     scanf( %s%d%d , c, &qu[i % sq].x, &qu[i % sq].y); n     qu[i % sq].type = (c[0] ==  P  ? 1 : 2); n     if ((i % sq) == sq - 1 || i == Q - 1) process(i % sq + 1); n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long int a, s; n   scanf( %lld , &a); n   a = a - 2; n   if (a % 3 == 0) { n     printf( 1 2 %lld , a - 1); n   } else n     printf( 1 1 %lld , a); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int gcd(int a, int b) { n   if (b == 0) { n     return a; n   } n   return gcd(b, a % b); n } n int findGCD(int arr[], int n) { n   int result = arr[0]; n   for (int i = 1; i < n; i++) { n     result = gcd(arr[i], result); n   } n   return result; n } n void swap(int* a, int* b) { n   int t = *a; n   *a = *b; n   *b = t; n } n void merge(int arr[], int l, int m, int r) { n   int i, j, k; n   int n1 = m - l + 1; n   int n2 = r - m; n   int L[n1], R[n2]; n   for (i = 0; i < n1; i++) { n     L[i] = arr[l + i]; n   } n   for (j = 0; j < n2; j++) { n     R[j] = arr[m + 1 + j]; n   } n   i = 0; n   j = 0; n   k = l; n   while (i < n1 && j < n2) { n     if (L[i] <= R[j]) { n       arr[k] = L[i]; n       i++; n     } else { n       arr[k] = R[j]; n       j++; n     } n     k++; n   } n   while (i < n1) { n     arr[k] = L[i]; n     i++; n     k++; n   } n   while (j < n2) { n     arr[k] = R[j]; n     j++; n     k++; n   } n } n void mergeSort(int arr[], int l, int r) { n   if (l < r) { n     int m = l + (r - l) / 2; n     mergeSort(arr, l, m); n     mergeSort(arr, m + 1, r); n     merge(arr, l, m, r); n   } n } n int main() { n   int n, i, gcd, pointsToBeAdded = 0; n   cin >> n; n   int arr[n]; n   int differences[n - 1]; n   for (i = 0; i < n; i++) { n     cin >> arr[i]; n   } n   mergeSort(arr, 0, n - 1); n   for (i = 1; i < n; i++) { n     differences[i - 1] = arr[i] - arr[i - 1]; n   } n   gcd = findGCD(differences, n - 1); n   for (i = 0; i < n - 1; i++) { n     differences[i] /= gcd; n     pointsToBeAdded += differences[i] - 1; n   } n   cout << pointsToBeAdded; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n using i64 = long long; n using f64 = double; n using f80 = long double; n using pii = pair<int, int>; n using ptx = pair<f64, f64>; n const int N = 105; n bool f[N][N]; n int n, m, first, second; n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0), cout.tie(0); n   int tip = 1; n   int a, b; n   cin >> n >> m; n   cin >> first >> second; n   cout << first <<     << second <<   n ; n   f[first][second] = true; n   for (int j = 1; j <= m; ++j) { n     if (!f[first][j]) { n       cout << first <<     << j <<   n ; n       f[first][j] = true; n       a = first, b = j; n     } n   } n   tip = -1; n   for (int i = 1; i <= n; ++i) n     if (!f[i][m] && !f[i][1]) { n       for (a = i; true; b += tip) { n         cout << a <<     << b <<   n ; n         f[a][b] = true; n         if (!(1 <= b + tip && b + tip <= m)) break; n       } n       tip *= -1; n     } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m, k, a[1003][1003], row[1003], col[1003]; n int main() { n   scanf( %d%d%d , &n, &m, &k); n   int i, j; n   for (i = 1; i <= n; i++) n     for (j = 1; j <= m; j++) scanf( %d , &a[i][j]); n   for (i = 0; i <= n; i++) row[i] = i; n   for (i = 0; i <= m; i++) col[i] = i; n   char s; n   int x, y; n   for (i = 0; i < k; i++) { n     scanf(  n%c %d %d , &s, &x, &y); n     if (s ==  g ) printf( %d n , a[row[x]][col[y]]); n     if (s ==  c ) { n       j = col[x]; n       col[x] = col[y]; n       col[y] = j; n     } n     if (s ==  r ) { n       j = row[x]; n       row[x] = row[y]; n       row[y] = j; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long r(long long a) { n   long long i = 0; n   while (a % 2 == 0) { n     a /= 2; n     ++i; n   } n   return i; n } n void one_case() { n   long long a, b; n   cin >> a >> b; n   auto ra = r(a); n   auto rb = r(b); n   a /= (1ll << ra); n   b /= (1ll << rb); n   if (a != b) { n     cout <<  -1 n ; n     return; n   } n   cout << (abs(ra - rb) + 2) / 3 <<   n ; n } n int main() { n   std::ios::sync_with_stdio(false); n   std::cin.tie(nullptr); n   int t = 1; n   cin >> t; n   while (t--) { n     one_case(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int high[500005], ata[500005], cost[500005], par[500005], der[500005]; n long long ans; n vector<int> v[500005]; n int n, k, m, x, y, z; n vector<pair<int, pair<int, int> > > e; n int bul(int node, int ar[]) { n   if (node == ar[node]) return node; n   return ar[node] = bul(ar[node], ar); n } n void update(int a, int b, int cst) { n   a = bul(a, high); n   b = bul(b, high); n   while (a != b) { n     if (der[a] < der[b]) swap(a, b); n     cost[a] = cst; n     a = high[a] = bul(par[a], high); n   } n } n void dfs(int node, int ata) { n   par[node] = ata; n   der[node] = der[ata] + 1; n   for (int i : v[node]) n     if (i != ata) dfs(i, node); n } n int main() { n   scanf( %d %d %d , &n, &k, &m); n   for (int i = 1; i <= n; i++) ata[i] = i; n   for (int i = 1; i <= k; i++) { n     scanf( %d %d , &x, &y); n     ata[bul(x, ata)] = bul(y, ata); n     v[x].push_back(y); n     v[y].push_back(x); n   } n   for (int i = 1; i <= m; i++) { n     scanf( %d %d %d , &x, &y, &z); n     e.push_back({z, {x, y}}); n   } n   sort(e.begin(), e.end()); n   for (int i = 0; i < m; i++) { n     pair<int, pair<int, int> > edge = e[i]; n     if (bul(edge.second.first, ata) != bul(edge.second.second, ata)) { n       v[edge.second.first].push_back(edge.second.second); n       v[edge.second.second].push_back(edge.second.first); n       ata[ata[edge.second.first]] = ata[edge.second.second]; n       ans -= edge.first; n     } n   } n   dfs(1, 0); n   for (int i = 1; i <= n; i++) { n     high[i] = i; n     cost[i] = 2000000000; n   } n   for (int i = 0; i < m; i++) { n     pair<int, pair<int, int> > edge = e[i]; n     update(edge.second.first, edge.second.second, edge.first); n   } n   for (int i = 2; i <= n; i++) { n     if (cost[i] == 2000000000) { n       printf( -1 ); n       return 0; n     } n     ans += cost[i]; n   } n   printf( %lld , ans); n } n


#include <bits/stdc++.h> n using namespace std; n const int64_t inf = 1e18; n vector<vector<int64_t> > adj; n int64_t inp() { n   int64_t x; n   cin >> x; n   return x; n } n vector<int64_t> level; n int64_t res = 0; n void dfs(int64_t u, int64_t LV = 0) { n   level[u] = LV; n   res = max(res, LV); n   for (auto v : adj[u]) { n     dfs(v, LV + 1); n   } n } n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   for (int64_t _ = (1); _ >= (1); _--) { n     int64_t n; n     cin >> n; n     adj.resize(n + 1); n     level.resize(n + 1); n     for (int64_t node = (1); node <= (n); node++) n       adj[max(inp(), int64_t(0))].push_back(node); n     dfs(0); n     cout << res; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long po(long long a, long long x, long long m) { n   if (x == 0) { n     return 1; n   } n   long long ans = 1; n   long long k = 1; n   while (k <= x) { n     if (x & k) { n       ans = ((ans * a) % m); n     } n     k <<= 1; n     a *= a; n     a %= m; n   } n   return ans; n } n int32_t main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   long long t; n   cin >> t; n   while (t--) { n     long long n; n     cin >> n; n     string a, b; n     cin >> a >> b; n     bool f[n + 1][2]; n     for (long long i = 0; i < n; i++) { n       if (a[i] ==  1  || a[i] ==  2 ) n         f[i][0] = false; n       else n         f[i][0] = true; n       if (b[i] ==  1  || b[i] ==  2 ) n         f[i][1] = false; n       else n         f[i][1] = true; n     } n     long long bll = 0; n     bool flag = true; n     bool k = false; n     for (long long i = 0; i < n; i++) { n       if (i != n && !f[i][bll]) { n         continue; n       } n       if (i != n && (f[i][0] && !f[i][1]) || (f[i][1] && !f[i][0])) { n         flag = false; n         break; n       } n       bll ^= 1; n     } n     if (flag && bll == 1) n       cout <<  YES  n            <<   n ; n     else n       cout <<  NO  n            <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MX = 200100; n int N, M; n char S[MX]; n int A[MX]; n struct Segment { n   int a, b, c, d, e; n   int z, l, r; n   void add(int x) { a += x, b += x, c -= x, d -= x, z += x; } n } T[MX << 2]; n inline void PushUp(int i) { n   Segment &t = T[i], &x = T[(i << 1)], &y = T[(i << 1 | 1)]; n   t.a = max(x.a, y.a); n   t.b = min(x.b, y.b); n   t.c = max(max(x.c, y.c), x.a - 2 * y.b); n   t.d = max(max(x.d, y.d), y.a - 2 * x.b); n   t.e = max(max(x.e, y.e), max(x.c + y.a, x.a + y.d)); n } n inline void PushDown(int i) { n   if (T[i].z) T[(i << 1)].add(T[i].z), T[(i << 1 | 1)].add(T[i].z), T[i].z = 0; n } n void Build(int i, int l, int r) { n   T[i].l = l, T[i].r = r; n   if (l == r) { n     T[i].a = T[i].b = A[l], T[i].c = T[i].d = -A[l]; n     return; n   } n   int m = (l + r) >> 1; n   Build((i << 1), l, m), Build((i << 1 | 1), m + 1, r); n   PushUp(i); n } n void Add(int i, int a, int b, int x) { n   if (a <= T[i].l && T[i].r <= b) { n     T[i].add(x); n     return; n   } n   PushDown(i); n   if (a <= T[(i << 1)].r) Add((i << 1), a, b, x); n   if (b >= T[(i << 1 | 1)].l) Add((i << 1 | 1), a, b, x); n   PushUp(i); n } n int main() { n   int n; n   scanf( %d%d%s , &n, &M, S + 1), N = strlen(S + 1); n   for (int i = (1); i <= (N); ++i) A[i] = A[i - 1] + (S[i] ==  (  ? 1 : -1); n   Build(1, 0, N); n   printf( %d n , T[1].e); n   for (int i = (1); i <= (M); ++i) { n     int l, r; n     scanf( %d%d , &l, &r); n     if (l > r) swap(l, r); n     if (S[l] != S[r]) Add(1, l, r - 1, S[l] ==  (  ? -2 : 2), swap(S[l], S[r]); n     printf( %d n , T[1].e); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 1e6 + 5; n int a[maxn]; n int n, l, t; n int main() { n   scanf( %d%d%d , &n, &l, &t); n   for (int i = 0; i < n; i++) { n     scanf( %d , &a[i]); n   } n   double ans = 0; n   int k = 2 * t / l, c = 2 * t % l; n   for (int i = 0; i < n; i++) { n     int w = a[i] - c; n     int cnt = 0; n     if (w >= 0) { n       cnt = i - (lower_bound(a, a + n, w) - a); n     } else { n       cnt += i; n       cnt += n - (lower_bound(a, a + n, l + w) - a); n     } n     ans += 0.5 * 0.5 * k * (n - 1) + 0.5 * 0.5 * cnt; n   } n   cout.setf(ios::fixed); n   cout << setprecision(9) << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<vector<int> > dist(51, vector<int>(3, 100)); n int n, m; n int dp[51][8]; n void calc_s(int i, string &s) { n   for (int j = 0; j < s.length(); j++) { n     if (s[j] ==  #  || s[j] ==  *  || s[j] ==  & ) { n       dist[i][2] = min(dist[i][2], j); n       dist[i][2] = min(dist[i][2], m - j); n     } else if (s[j] >=  a  && s[j] <=  z ) { n       dist[i][0] = min(dist[i][0], j); n       dist[i][0] = min(dist[i][0], m - j); n     } else if (s[j] >=  0  && s[j] <=  9 ) { n       dist[i][1] = min(dist[i][1], j); n       dist[i][1] = min(dist[i][1], m - j); n     } n   } n } n int calc(int i, int j) { n   if (j == 7) return 0; n   if (i == (n + 1)) return 100; n   if (dp[i][j] != -1) return dp[i][j]; n   int x = 100; n   for (int k = 0; k < 3; k++) { n     x = min(x, dist[i][k] + calc(i + 1, (j | (1 << k)))); n   } n   dp[i][j] = x; n   return dp[i][j]; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   cin >> n >> m; n   for (int i = 1; i <= n; i++) { n     string s; n     cin >> s; n     calc_s(i, s); n   } n   memset(dp, -1, sizeof(dp)); n   cout << calc(1, 0); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int mult(int a, int b, int p = 1000000007) { return ((a % p) * (b % p)) % p; } n int multbig(int a, int b, int mod) { n   if (a == 0 or b == 0) return 0; n   if (a == 1 or b == 1) return (a * b) % mod; n   int cur = multbig(a, b / 2, mod); n   cur = (2 * cur) % mod; n   if (b % 2) cur = (cur + a) % mod; n   return cur; n } n int add(int a, int b, int p = 1000000007) { return (a % p + b % p) % p; } n int fpow(int n, int k, int p = 1000000007) { n   int r = 1; n   for (; k; k >>= 1LL) { n     if (k & 1LL) r = mult(r, n, p); n     n = mult(n, n, p); n   } n   return r; n } n int inv(int a, int p = 1000000007) { return fpow(a, p - 2, p); } n int inv_euclid(int a, int m = 1000000007) { n   int m0 = m; n   int y = 0, x = 1; n   if (m == 1) return 0; n   while (a > 1) { n     int q = a / m; n     int t = m; n     m = a % m, a = t; n     t = y; n     y = x - q * y; n     x = t; n   } n   if (x < 0) x += m0; n   return x; n } n vector<pair<int, int>> vvv; n pair<int, int> aa[200003]; n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   int n, m; n   cin >> n >> m; n   for (int i = 0; i < (int)m; i++) { n     int p, q; n     cin >> p >> q; n     if (p > q) { n       swap(p, q); n     } n     aa[i].first = p; n     aa[i].second = q; n     vvv.push_back(make_pair(p, q)); n   } n   sort(vvv.begin(), vvv.end()); n   int f = 0; n   for (int i = 2; i <= n; i++) { n     if (n % i == 0) { n       int lol = n / i; n       vector<pair<int, int>> vv; n       for (int i = 0; i < (int)m; i++) { n         int p = aa[i].first + lol; n         int q = aa[i].second + lol; n         if (p > n) { n           p = p - n; n         } n         if (q > n) { n           q = q - n; n         } n         if (p > q) { n           swap(p, q); n         } n         vv.push_back(make_pair(p, q)); n       } n       sort(vv.begin(), vv.end()); n       if (vv == vvv) { n         f = 1; n         break; n       } n     } n   } n   if (f) { n     cout <<  Yes n ; n   } else n     cout <<  No n ; n } n


#include <bits/stdc++.h> n using namespace std; n void dice() { n   int x, y; n   cin >> x >> y; n   x = max(x, y); n   vector<string> str; n   str = { 1/6 ,  1/3 ,  1/2 ,  2/3 ,  5/6 ,  1/1 }; n   x = 6 - x + 1; n   cout << str[x - 1] << endl; n } n int main() { dice(); } n


#include <bits/stdc++.h> n using namespace std; n int ask(int c, int d) { n   cout <<  ?   << c <<     << d << endl; n   fflush(stdout); n   int x; n   cin >> x; n   return x; n } n void go(int a, int b) { n   cout <<  !   << a <<     << b << endl; n   fflush(stdout); n   exit(0); n } n bool same[50]; n signed main() { n   int type = ask(0, 0); n   if (type == 0) { n     int ans = 0; n     for (int i = 29; i >= 0; i--) { n       int q = ask((1 << i), 0); n       if (q == 0) { n         continue; n       } n       if (q == 1) { n         ans += 0; n       } else { n         ans += (1 << i); n       } n     } n     go(ans, ans); n   } n   bool A, B; n   if (type == 1) { n     A = 1; n     B = 0; n   } else { n     A = 0; n     B = 1; n   } n   int ansA = 0, ansB = 0; n   for (int i = 29; i >= 0; i--) { n     int q = ask(ansA + (1 << i), ansB + (1 << i)); n     if (q == 0) { n       for (int pos = i; pos >= 0; pos--) { n         same[pos] = 1; n       } n       break; n     } n     if (A == 1) { n       if (q == 1) { n         same[i] = 1; n       } else { n         ansA += (1 << i); n         ansB += 0; n         int qq = ask(ansA, ansB); n         if (qq == 1) A = 1, B = 0; n         if (qq == -1) A = 0, B = 1; n       } n     } else { n       if (q == -1) { n         same[i] = 1; n       } else { n         ansA += 0; n         ansB += (1 << i); n         int qq = ask(ansA, ansB); n         if (qq == 1) A = 1, B = 0; n         if (qq == -1) A = 0, B = 1; n       } n     } n   } n   for (int i = 29; i >= 0; i--) { n     if (same[i]) { n       int q = ask(ansA + (1 << i), ansB); n       if (q == 0) { n         continue; n       } n       if (q == 1) { n         ansA += 0; n         ansB += 0; n       } else { n         ansA += (1 << i); n         ansB += (1 << i); n       } n     } n   } n   go(ansA, ansB); n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 1010; n bool z[MAXN] = {0}; n void prework() { n   for (int i = 2; i <= 1000; i++) { n     if (z[i]) continue; n     for (int k = i + i; k <= 1000; k += i) z[k] = true; n   } n } n struct node { n   char c; n   int n; n   bool operator<(node b) const { return n > b.n; } n }; n vector<int> v; n int num[50] = {0}; n string s; n node a[27]; n char ans[1010] = {0}; n int main() { n   prework(); n   string s; n   cin >> s; n   int L = s.size(); n   int all = 0; n   for (int i = 2; i <= L; i++) { n     if (z[i]) continue; n     if (i * 2 <= L) continue; n     v.push_back(i); n     all++; n   } n   v.push_back(1); n   all++; n   for (string::iterator k = s.begin(); k != s.end(); k++) num[*k -  a  + 1]++; n   for (int i = 1; i <= 26; i++) { n     a[i].c =  a  + i - 1; n     a[i].n = num[i]; n   } n   sort(a + 1, a + 27); n   all = L - all; n   if (a[1].n < all) { n     cout <<  NO  << endl; n     return 0; n   } n   a[1].n -= all; n   int now = 1; n   for (vector<int>::iterator k = v.begin(); k != v.end(); k++) { n     while (!a[now].n) now++; n     ans[*k] = a[now].c; n     a[now].n--; n   } n   for (int i = 1; i <= L; i++) n     if (!ans[i]) ans[i] = a[1].c; n   cout <<  YES  << endl; n   string sss(ans + 1); n   cout << sss << endl; n } n


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


#include <bits/stdc++.h> n using namespace std; n const int Mo = (int)1e9 + 7; n int i, j, m, n, p, k, sum, id, x, y, a[1000005], flag, ans; n int power(int x, int y) { n   int sum = 1; n   for (; y; y >>= 1) { n     if (y & 1) sum = 1ll * x * sum % Mo; n     x = 1ll * x * x % Mo; n   } n   return sum; n } n int main() { n   scanf( %d%d%d , &n, &m, &k); n   id = 1; n   for (i = 1; i <= m; ++i) { n     scanf( %d%d , &x, &y); n     if (y - x != 1 && y - x != k + 1) { n       printf( 0 n ); n       return 0; n     } else if (y - x != 1) n       a[x]++, ++sum, id = x; n   } n   for (i = 1; i <= n; ++i) a[i] += a[i - 1]; n   for (i = id; i <= n && i + k + 1 <= n; ++i) n     if (a[max(0, i - k - 1)] == 0) n       ans = (ans + power(2, (i - 1 - max(0, i - k - 1) - n                              (a[i - 1] - a[max(0, i - k - 1)])))) % n             Mo; n   if (!a[n]) ans = (ans + 1) % Mo; n   printf( %d n , ans); n } n


#include <bits/stdc++.h> n using namespace std; n const int MOD = int(1e9) + 7; n const long long int MOD64 = (long long int)(1e18) + 7; n const int INF = 0x7fffffff; n const long long int INF64 = 0x7fffffffffffffff; n const int N = 5000 + 10; n int a[N], lnum[N], rnum[N]; n int n, m; n vector<int> H[N]; n long long int cal(int x, int val) { n   int l = 0, r = H[x].size() - 1, ans = -1; n   while (l <= r) { n     int mid = (l + r) / 2; n     if (H[x][mid] <= val) { n       ans = mid; n       l = mid + 1; n     } else n       r = mid - 1; n   } n   return ans + 1; n } n pair<int, long long int> solve() { n   int ans = 0; n   long long int num = 1; n   for (int i = int(1); i < int(n + 1); i++) { n     long long int tmp = 0; n     tmp = cal(i, lnum[i]) * cal(i, rnum[i]) - cal(i, min(lnum[i], rnum[i])); n     if (tmp > 0) { n       ans += 2; n       num = (num * tmp) % MOD; n       continue; n     } n     tmp = cal(i, lnum[i]) + cal(i, rnum[i]); n     if (tmp != 0) { n       ans += 1; n       num = (num * tmp) % MOD; n     } n   } n   return {ans, num}; n } n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   cin >> n >> m; n   for (int i = int(1); i < int(n + 1); i++) { n     cin >> a[i]; n     rnum[a[i]]++; n   } n   for (int i = int(0); i < int(m); i++) { n     int f, h; n     cin >> f >> h; n     H[f].push_back(h); n   } n   for (int i = int(1); i < int(n + 1); i++) sort((H[i]).begin(), (H[i]).end()); n   int ans = 0; n   long long int num = 0; n   for (int i = int(0); i < int(n + 1); i++) { n     rnum[a[i]]--; n     lnum[a[i]]++; n     pair<int, long long int> tmp = solve(); n     if (ans < tmp.first) { n       ans = tmp.first; n       num = tmp.second; n     } else if (ans == tmp.first) { n       num = (num + tmp.second) % MOD; n     } n   } n   memset(lnum, 0, sizeof(lnum)); n   memset(rnum, 0, sizeof(rnum)); n   for (int i = int(1); i < int(n + 1); i++) rnum[a[i]]++; n   for (int i = int(0); i < int(n); i++) { n     rnum[a[i + 1]]--; n     lnum[a[i]]++; n     pair<int, long long int> tmp = solve(); n     if (tmp.first == ans) num = (num - tmp.second + MOD) % MOD; n   } n   cout << ans <<     << num << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int a[1000]; n int main() { n   int n, m; n   scanf( %d%d , &n, &m); n   for (int i = 1; i <= n; ++i) scanf( %d , &a[i]); n   double ans = 0; n   for (int i = 1; i <= m; ++i) { n     int x, y, z; n     scanf( %d%d%d , &x, &y, &z); n     ans = max(ans, ((double)a[x] + a[y]) / z); n   } n   printf( %.15f n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct node { n   int num; n   int num_num; n } num[105]; n int main() { n   int N, M; n   scanf( %d%d , &N, &M); n   num[0].num = 0; n   num[0].num_num = 0; n   for (int i = 1; i <= N; i++) { n     int flag; n     scanf( %d , &flag); n     num[i].num = num[i - 1].num + flag; n     num[i].num_num = 0; n   } n   while (M--) { n     int left, right; n     scanf( %d%d , &left, &right); n     if (left == right && num[left].num - num[left - 1].num > 0) n       num[left].num_num++; n     else if (left < right && num[right].num - num[left - 1].num >= 0) { n       for (int i = left; i <= right; i++) num[i].num_num++; n     } n   } n   long long int sum = 0; n   for (int i = 1; i <= N; i++) { n     sum += (num[i].num - num[i - 1].num) * num[i].num_num; n   } n   printf( %I64d n , sum); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 1e5 + 5; n long long ar[maxn]; n int main() { n   long long n, m; n   cin >> n >> m; n   long long a, b, c; n   cin >> a >> b >> c; n   ar[a] = 1, ar[b] = 2, ar[c] = 3; n   for (int i = 1; i < m; ++i) { n     cin >> a >> b >> c; n     if (!ar[a] and !ar[b] and !ar[c]) n       ar[a] = 1, ar[b] = 2, ar[c] = 3; n     else { n       if (ar[a]) { n         if (ar[a] == 1) n           ar[b] = 2, ar[c] = 3; n         else if (ar[a] == 2) n           ar[b] = 1, ar[c] = 3; n         else n           ar[b] = 1, ar[c] = 2; n       } else if (ar[b]) { n         if (ar[b] == 1) n           ar[a] = 2, ar[c] = 3; n         else if (ar[b] == 2) n           ar[a] = 1, ar[c] = 3; n         else n           ar[a] = 1, ar[c] = 2; n       } else if (ar[c]) { n         if (ar[c] == 1) n           ar[a] = 2, ar[b] = 3; n         else if (ar[c] == 2) n           ar[a] = 1, ar[b] = 3; n         else n           ar[a] = 1, ar[b] = 2; n       } n     } n   } n   for (int i = 1; i < n + 1; ++i) cout << ar[i] <<    ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const double fs = 6e-2, Pi = acos(-1.0); n struct point { n   double x, y; n } p[4]; n double a, b, c, r, S, C, P, C1, A1, B1, R1, Ans; n double s(point a, point b) { n   return sqrt((a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y)); n } n double gcd(double a, double b) { n   return (fabs(b) > fs) ? gcd(b, fmod(a, b)) : a; n } n int main() { n   for (int i = 1; i <= 3; ++i) scanf( %lf%lf , &p[i].x, &p[i].y); n   a = s(p[1], p[2]), b = s(p[2], p[3]), c = s(p[1], p[3]), P = (a + b + c) / 2; n   S = sqrt(P * (P - a) * (P - b) * (P - c)), r = a * b * c / (4 * S); n   C1 = acos(1 - c * c / (2 * r * r)), A1 = acos(1 - a * a / (2 * r * r)), n   B1 = 2 * Pi - A1 - C1; n   R1 = gcd(A1, gcd(B1, C1)); n   return printf( %.6lf , (Pi * r * r * sin(R1)) / R1), 0; n } n


#include <bits/stdc++.h> n using namespace std; n int dx[4] = {0, -1, 0, 1}; n int dy[4] = {1, 0, -1, 0}; n int x[100000], y[100000], DP[100000], CP[100000], R, C, used[50][50][4][4]; n char s[64][64]; n bool Out(int xx, int yy) { return xx < 0 || yy < 0 || xx >= R || yy >= C; } n void next(int x1, int y1, int DP1, int CP1, int &x2, int &y2, int &DP2, n           int &CP2) { n   int i; n   for (i = 1;; i++) { n     int nx = x1 + dx[DP1] * i; n     int ny = y1 + dy[DP1] * i; n     if (Out(nx, ny) || s[nx][ny] != s[x1][y1]) break; n   } n   i--; n   x1 += dx[DP1] * i; n   y1 += dy[DP1] * i; n   for (i = 1;; i++) { n     int nx = x1 + dx[(DP1 + CP1) & 3] * i; n     int ny = y1 + dy[(DP1 + CP1) & 3] * i; n     if (Out(nx, ny) || s[nx][ny] != s[x1][y1]) break; n   } n   i--; n   x1 += dx[(DP1 + CP1) & 3] * i; n   y1 += dy[(DP1 + CP1) & 3] * i; n   int nx = x1 + dx[DP1]; n   int ny = y1 + dy[DP1]; n   if (Out(nx, ny) || !s[nx][ny]) { n     x2 = x1; n     y2 = y1; n     if (CP1 == 1) { n       CP2 = 3; n       DP2 = DP1; n     } else { n       CP2 = 1; n       DP2 = (DP1 + 3) & 3; n     } n   } else { n     x2 = nx; n     y2 = ny; n     DP2 = DP1; n     CP2 = CP1; n   } n } n int main() { n   int i, j, k, m; n   scanf( %d%d , &R, &m); n   for (i = 0; i < R; i++) scanf( %s , s[i]); n   C = strlen(s[0]); n   for (i = 0; i < R; i++) n     for (j = 0; j < C; j++) s[i][j] -=  0 ; n   CP[0] = 1; n   memset(used, -1, sizeof(used)); n   used[0][0][0][1] = 0; n   for (i = 1;; i++) { n     next(x[i - 1], y[i - 1], DP[i - 1], CP[i - 1], x[i], y[i], DP[i], CP[i]); n     if (used[x[i]][y[i]][DP[i]][CP[i]] >= 0) { n       break; n     } n     used[x[i]][y[i]][DP[i]][CP[i]] = i; n   } n   int tmp = used[x[i]][y[i]][DP[i]][CP[i]]; n   if (m < i) n     printf( %d n , (int)s[x[m]][y[m]]); n   else n     printf( n          %d n , n         (int)s[x[tmp + (m - tmp) % (i - tmp)]][y[tmp + (m - tmp) % (i - tmp)]]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n string eq(string c) { n   string s =   ; n   for (int i = 0; i < c.size(); i++) { n     if (c[i] ==  2 ) n       s +=  2 ; n     else if (c[i] ==  3 ) n       s +=  3 ; n     else if (c[i] ==  4 ) n       s +=  223 ; n     else if (c[i] ==  5 ) n       s +=  5 ; n     else if (c[i] ==  6 ) n       s +=  53 ; n     else if (c[i] ==  7 ) n       s +=  7 ; n     else if (c[i] ==  8 ) n       s +=  7222 ; n     else if (c[i] ==  9 ) n       s +=  7332 ; n   } n   sort(s.begin(), s.end()); n   reverse(s.begin(), s.end()); n   return s; n } n int main() { n   int n; n   string s; n   cin >> n >> s; n   cout << eq(s); n   return 0; n } n