#include <bits/stdc++.h> n #pragma GCC optimize( Ofast ) n using namespace std; n namespace io { n const int L = (1 << 20) + 1; n char buf[L], *S, *T, c; n char getchar() { n   if (__builtin_expect(S == T, 0)) { n     T = (S = buf) + fread(buf, 1, L, stdin); n     return (S == T ? EOF : *S++); n   } n   return *S++; n } n int inp() { n   int x = 0, f = 1; n   char ch; n   for (ch = getchar(); !isdigit(ch); ch = getchar()) n     if (ch ==  - ) f = -1; n   for (; isdigit(ch); x = x * 10 + ch -  0 , ch = getchar()) n     ; n   return x * f; n } n unsigned inpu() { n   unsigned x = 0; n   char ch; n   for (ch = getchar(); !isdigit(ch); ch = getchar()) n     ; n   for (; isdigit(ch); x = x * 10 + ch -  0 , ch = getchar()) n     ; n   return x; n } n long long inp_ll() { n   long long x = 0; n   int f = 1; n   char ch; n   for (ch = getchar(); !isdigit(ch); ch = getchar()) n     if (ch ==  - ) f = -1; n   for (; isdigit(ch); x = x * 10 + ch -  0 , ch = getchar()) n     ; n   return x * f; n } n char B[25], *outs = B + 20, *outr = B + 20; n template <class T> n inline void print(register T a, register char x = 0) { n   if (x) *--outs = x, x = 0; n   if (!a) n     *--outs =  0 ; n   else n     while (a) *--outs = (a % 10) + 48, a /= 10; n   if (x) *--outs = x; n   fwrite(outs, outr - outs, 1, stdout); n   outs = outr; n } n };  // namespace io n using io ::inp; n using io ::inp_ll; n using io ::inpu; n using io ::print; n using i32 = int; n using i64 = long long; n using u8 = unsigned char; n using u32 = unsigned; n using u64 = unsigned long long; n using f64 = double; n using f80 = long double; n long long power(long long a, long long b, long long p) { n   if (!b) return 1; n   long long t = power(a, b / 2, p); n   t = t * t % p; n   if (b & 1) t = t * a % p; n   return t; n } n long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; } n template <class T> n inline void freshmin(T &a, const T &b) { n   if (a > b) a = b; n } n template <class T> n inline void freshmax(T &a, const T &b) { n   if (a < b) a = b; n } n const int MAXN = 100000; n const int DEG = 5; n const int BASE = 10; n const int MAXP = 10000000; n const int MOD = 1000000007; n const f80 MI = f80(1) / MOD; n const long long INF = 10000000000000000LL; n const long long inf = 5000000000000LL; n int n; n int modBASE[1111], modDEG[1111]; n template <class T> n struct node { n   T v[DEG]; n   node(T c = 0, int k = 0) { n     for (int i = 0; i < DEG; ++i) v[i] = 0; n     v[k] = c; n   } n   node &operator+=(const node &A) { n     for (int i = 0; i < DEG; ++i) v[i] += A.v[i]; n     return *this; n   } n   friend node operator*(const node &A, const node &B) { n     node ret; n     for (int i = 0; i < DEG; ++i) n       for (int j = 0; j < DEG; ++j) ret.v[modDEG[i + j]] += A.v[i] * B.v[j]; n     return ret; n   } n   node &operator*=(const node &A) { return *this = *this * A; } n   node shl(int shift) { n     node ret; n     for (int i = 0; i < DEG; ++i) n       ret.v[modDEG[i + shift]] = (shift & 1) ? -v[i] : v[i]; n     return ret; n   } n }; n template <class T, class V> n T power(T a, V n) { n   if (n == 0) return 1; n   T t = power(a, n / 2); n   t = t * t; n   if (n & 1) t = t * a; n   return t; n } n node<u64> a[MAXN]; n void fft(bool inv = 0) { n   int shift = inv ? BASE - 1 : 1; n   for (int step = 1; step < MAXN; step *= BASE) { n     for (int offset = 0; offset < MAXN; ++offset) { n       if (offset / step % BASE != 0) continue; n       node<u64> to[BASE]; n       for (int i = 0; i < BASE; ++i) n         for (int j = 0; j < BASE; ++j) n           to[i] += a[offset + j * step].shl(modBASE[i * j * shift]); n       for (int i = 0; i < BASE; ++i) a[offset + i * step] = to[i]; n     } n   } n } n int main() { n   for (int i = 0; i < 1111; ++i) { n     modBASE[i] = i % BASE; n     modDEG[i] = i % DEG; n   } n   n = inp(); n   for (int i = 1; i <= n; ++i) a[inp()] += 1; n   fft(); n   for (int i = 0; i < MAXN; ++i) a[i] = power(a[i], n); n   fft(1); n   for (int i = 0; i < n; ++i) { n     u64 ans = a[i].v[0] - a[i].v[4]; n     ans >>= 5; n     ans *= 6723469279985657373ULL; n     print(ans & ((1ULL << 58) - 1),   n ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 2e5 + 50; n const long long oo = 1e9; n const long long mod = 998244353; n long long dp[2][510][510], cnt[501], p[501]; n int main() { n   int n, k; n   scanf( %d%d , &n, &k); n   dp[0][0][0] = 1; n   for (int i = 0; i < n; i++) { n     int q = i % 2; n     memset(dp[!q], 0, sizeof(dp[!q])); n     for (int j = 0; j <= n; j++) { n       for (int k = 0; k <= n; k++) { n         dp[!q][j + 1][max(j + 1, k)] = n             (dp[!q][j + 1][max(j + 1, k)] + dp[q][j][k]) % mod; n         dp[!q][1][max(k, 1)] = (dp[!q][1][max(k, 1)] + dp[q][j][k]) % mod; n       } n     } n   } n   for (int i = 0; i <= n; i++) n     for (int j = 0; j <= n; j++) cnt[i] = (cnt[i] + dp[n % 2][j][i]) % mod; n   for (int i = 0; i <= n; i++) { n     p[i + 1] = (p[i + 1] + p[i]) % mod; n     p[i + 1] = (p[i + 1] + cnt[i]) % mod; n   } n   long long ans = 0; n   for (int i = 1; i <= n; ++i) n     ans = (ans + (cnt[i] * p[min(n + 1, (k - 1) / i + 1)]) % mod) % mod; n   ans = (ans * ((mod + 1) / 2)) % mod; n   cout << ans << endl; n } n


#include <bits/stdc++.h> n using namespace std; n const int SIZE = 1000; n long long a, b, p, x; n long long qpow(long long x, long long y, long long MOD) { n   long long res = 1; n   while (y) { n     if (y & 1) res = (res * x) % MOD; n     x = (x * x) % MOD; n     y = y >> 1; n   } n   return res; n } n int main() { n   scanf( %lld%lld%lld%lld , &a, &b, &p, &x); n   long long ans = 0; n   for (long long i = 1; i < p; i++) { n     long long tp = qpow(a, i, p); n     long long invt = qpow(tp, p - 2, p); n     long long invp = qpow(p - 1, p - 2, p); n     long long y = ((b * invt - i + p) % p * invp) % p; n     long long ty = (x - i) / (p - 1); n     if (i + y * (p - 1) > x) continue; n     long long anst = (ty - y) / p + 1; n     ans = ans + anst; n   } n   printf( %lld n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int INF = 1e9 + 7; n const int mod = 1e9 + 7; n long long int powmod(long long int a, long long int b) { n   long long int res = 1; n   a %= mod; n   assert(b >= 0); n   for (; b; b >>= 1) { n     if (b & 1) res = res * a % mod; n     a = a * a % mod; n   } n   return res; n } n int nxt() { n   int x; n   scanf( %d , &x); n   return x; n } n class FenwickTree { n  public: n   vector<int> ft; n   FenwickTree(int n) { ft.assign(n + 1, 0); } n   int rsq(int b) { n     int sum = 0; n     for (; b; b -= (b & (-b))) sum += ft[b]; n     return sum; n   } n   int rsq(int a, int b) { return rsq(b) - (a == 1 ? 0 : rsq(a - 1)); } n   void adjust(int k, int v) { n     for (; k < (int)ft.size(); k += (k & (-k))) ft[k] += v; n   } n }; n void cnt(int l, int r) {} n void add(int l) {} n void rmv(int l) {} n int main() { n   long long int n, k; n   cin >> n >> k; n   vector<int> a(n), b(n); n   for (int i = 0; i < n; i++) { n     a[i] = nxt(); n     b[i] = a[i]; n   } n   sort((b).begin(), (b).end()); n   for (int i = 0; i < n; i++) { n     a[i] = lower_bound((b).begin(), (b).end(), a[i]) - b.begin() + 1; n   } n   FenwickTree ft(n), ft2(n); n   long long int cnt = 0; n   for (int i = 0; i < n; i++) { n     cnt += (long long int)ft2.rsq(a[i] + 1, n); n     ft2.adjust(a[i], 1); n   } n   if (cnt <= k) { n     printf( %lld n , n * (n - 1) / 2); n     return 0; n   } n   int pr = 0; n   long long int tmp = cnt; n   cnt = 0; n   for (int i = 0; i < n; i++) { n     tmp += ft.rsq(a[i] + 1, n); n     tmp += ft2.rsq(a[i] - 1); n     ft.adjust(a[i], 1); n     while (tmp > k && pr <= n - 1) { n       tmp -= ft.rsq(a[pr] + 1, n); n       tmp -= ft2.rsq(a[pr] - 1); n       ft2.adjust(a[pr], -1); n       pr++; n     } n     cnt += max(0LL, n - pr); n   } n   printf( %lld n , cnt); n   return 0; n } n


#include <bits/stdc++.h> n #pragma GCC target( sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx ) n using namespace std; n using ll = int64_t; n const size_t kBuben = 10; n const int kNone = -1; n struct Pair { n   int value, count; n }; n struct ByValue { n   inline bool operator()(const Pair& a, const Pair& b) const { n     return a.value < b.value; n   } n }; n struct ByCount { n   inline bool operator()(const Pair& a, const Pair& b) const { n     if (a.count != b.count) return a.count > b.count; n     return a.value < b.value; n   } n }; n struct Segment { n   Segment* l; n   Segment* r; n   array<Pair, kBuben> top; n   int len, middle, value_to_push; n   Segment(int length) { n     l = r = nullptr; n     len = length; n     clear(); n   } n   inline void clear() { n     value_to_push = kNone; n     top.fill(Pair{kNone, 0}); n   } n   inline void assign(int value) { n     clear(); n     top[0] = Pair{value, len}; n     value_to_push = value; n   } n   inline void rebuild(const Segment& a, const Segment& b) { n     array<Pair, 2 * kBuben> result; n     array<Pair, kBuben> add; n     size_t add_count = 0; n     result.fill(Pair{kNone, 0}); n     add.fill(Pair{kNone, 0}); n     copy(a.top.begin(), a.top.end(), result.begin()); n     sort(result.begin(), result.end(), ByValue()); n     for (const auto& segment : b.top) { n       auto it = lower_bound(result.begin(), result.end(), segment, ByValue()); n       if (it == result.end() || it->value != segment.value) { n         add[add_count++] = segment; n       } else { n         it->count += segment.count; n       } n     } n     copy(add.begin(), add.begin() + add_count, result.begin()); n     sort(result.begin(), result.end(), ByCount()); n     copy(result.begin(), result.begin() + kBuben, top.begin()); n   } n }; n Segment* root; n inline bool push(Segment* current) { n   if (current->value_to_push != kNone) { n     current->l->assign(current->value_to_push); n     current->r->assign(current->value_to_push); n     current->value_to_push = kNone; n     return true; n   } else { n     return false; n   } n } n inline int get_rand(int l, int r) { return l + (rand() % (r - l + 1)); } n Segment* build(int l, int r, const vector<int>& values) { n   Segment* current = new Segment(r - l + 1); n   if (l == r) { n     current->assign(values[l]); n   } else { n     int m = (l + r) / 2; n     current->middle = m; n     current->l = build(l, m, values); n     current->r = build(m + 1, r, values); n     current->rebuild(*current->l, *current->r); n   } n   return current; n } n void assign(Segment* current, int l, int r, int tl, int tr, int value) { n   if (l == tl && r == tr) { n     current->assign(value); n   } else { n     push(current); n     int m = current->middle; n     if (tl <= m) assign(current->l, l, m, tl, min(m, tr), value); n     if (tr > m) assign(current->r, m + 1, r, max(m + 1, tl), tr, value); n     current->rebuild(*current->l, *current->r); n   } n } n bool get(Segment* current, int l, int r, int tl, int tr, vector<Pair>& bests) { n   if (l == tl && r == tr) { n     for (const auto& p : current->top) { n       bests.push_back(p); n     } n     return false; n   } else { n     bool pushed = push(current); n     int m = current->middle; n     if (tl <= m) pushed |= get(current->l, l, m, tl, min(m, tr), bests); n     if (tr > m) pushed |= get(current->r, m + 1, r, max(m + 1, tl), tr, bests); n     if (pushed) current->rebuild(*current->l, *current->r); n     return pushed; n   } n } n int main() { n   srand(1224); n   ios_base::sync_with_stdio(false); n   cin.tie(nullptr); n   int n, m, P; n   cin >> n >> m >> P; n   int k = 100 / P; n   vector<int> values(n); n   for (int i = 0; i < n; i++) { n     cin >> values[i]; n   } n   root = build(0, n - 1, values); n   vector<Pair> bests; n   vector<Pair> result; n   for (int i = 0; i < m; i++) { n     int type, l, r, id; n     cin >> type >> l >> r; n     --l, --r; n     if (type == 1) { n       cin >> id; n       assign(root, 0, n - 1, l, r, id); n     } else { n       bests.clear(); n       get(root, 0, n - 1, l, r, bests); n       sort(bests.begin(), bests.end(), ByValue()); n       result.clear(); n       int i = 0; n       while (i < bests.size()) { n         int j = i, cnt = 0; n         while (j < bests.size() && bests[i].value == bests[j].value) { n           cnt += bests[j].count; n           ++j; n         } n         result.push_back(Pair{bests[i].value, cnt}); n         i = j; n       } n       sort(result.begin(), result.end(), ByCount()); n       result.resize(k); n       cout << k; n       for (const auto& x : result) { n         cout <<     << x.value; n       } n       cout <<   n ; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int mod = 1000000007; n const int maxn = 1e5 + 15; n const int inf = 1e9; n const int maxe = 200000; n vector<int> vec[maxn]; n int small[maxn]; n int same[maxn]; n int a[maxn]; n int N; n void init(int n) { n   for (int i = 1; i <= n; i++) n     for (int j = i; j <= n; j += i) vec[j].push_back(i); n   for (int i = 1; i <= n; i++) { n     small[i] = lower_bound(a, a + N, i) - a; n     same[i] = upper_bound(a, a + N, i) - a - small[i]; n   } n } n long long powmod(long long a, long long b, long long mod) { n   long long ans = 1; n   while (b) { n     if (b & 1) ans = ans * a % mod; n     a = a * a % mod; n     b = b / 2; n   } n   return ans; n } n int main() { n   int maxx = 0; n   ; n   scanf( %d , &N); n   for (int i = 0; i < N; i++) { n     scanf( %d , &a[i]); n     maxx = max(maxx, a[i]); n   } n   sort(a, a + N); n   init(maxx); n   long long sum = 1; n   for (int i = maxx; i > 1; i--) { n     long long ans = 1; n     int len = vec[i].size(); n     for (int j = 0; j < len - 1; j++) { n       int now = vec[i][j]; n       int next = vec[i][j + 1]; n       int cnt = small[next] - small[now]; n       ans = ans * powmod(j + 1, cnt, mod) % mod; n     } n     int cnt = N - small[vec[i][len - 1]]; n     ans = ans * n           ((powmod(len, cnt, mod) - powmod(len - 1, cnt, mod) + mod) % mod) % n           mod; n     sum = (sum + ans) % mod; n   } n   printf( %lld n , sum); n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 1e5 + 100; n vector<int> v[maxn]; n int childsNo[maxn]; n double expected_finish_time[maxn]; n void dfs_calc_number_of_childs(int vert, int par) { n   for (int i = 0; i < v[vert].size(); i++) { n     int child = v[vert][i]; n     if (child != par) { n       dfs_calc_number_of_childs(child, vert); n       childsNo[vert] += childsNo[child]; n     } n   } n   childsNo[vert] += 1; n } n void dfs_calc_expected_finish_time(int vert, int par) { n   for (int i = 0; i < v[vert].size(); i++) { n     int child = v[vert][i]; n     if (child != par) { n       expected_finish_time[child] = n           expected_finish_time[vert] + 1 + n           (childsNo[vert] - childsNo[child] - 1) / 2.0; n       dfs_calc_expected_finish_time(child, vert); n     } n   } n } n int main() { n   int n; n   cin >> n; n   for (int i = 2; i <= n; i++) { n     int p; n     cin >> p; n     v[p].push_back(i); n     v[i].push_back(p); n   } n   dfs_calc_number_of_childs(1, -1); n   expected_finish_time[1] = 1; n   dfs_calc_expected_finish_time(1, -1); n   for (int i = 1; i <= n; i++) cout << expected_finish_time[i] <<    ; n   cout << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n char op; n char s[200100]; n int n; n int ans; n char c; n int tam; n set<char> good, bad; n set<char>::iterator it; n bool ok = false; n int main() { n   scanf( %d , &n); n   for (int i = 0; i < n; ++i) { n     scanf(  %c %s , &op, s); n     tam = strlen(s); n     if (!ok) { n       if (op ==  . ) { n         for (int j = 0; j < tam; ++j) { n           good.insert(s[j]); n           if (bad.count(s[j])) { n             bad.erase(s[j]); n           } n         } n       } n       if (op ==  ! ) { n         if (bad.size() == 0) { n           for (int j = 0; j < tam; ++j) { n             if (!good.count(s[j])) { n               bad.insert(s[j]); n             } n           } n           for (char j =  a ; j <=  z ; ++j) { n             if (!bad.count(j)) { n               good.insert(j); n             } n           } n         } else { n           set<char> aux; n           for (int j = 0; j < tam; ++j) { n             aux.insert(s[j]); n           } n           vector<char> rem; n           for (it = bad.begin(); it != bad.end(); ++it) { n             if (!aux.count(*it)) { n               rem.push_back(*it); n             } n           } n           for (int i = 0; i < rem.size(); ++i) { n             bad.erase(rem[i]); n             good.insert(rem[i]); n           } n           for (char j =  a ; j <=  z ; ++j) { n             if (!bad.count(j) && !aux.count(j)) { n               good.insert(j); n             } n           } n         } n       } n       if (op ==  ? ) { n         good.insert(s[0]); n         if (bad.count(s[0])) { n           bad.erase(s[0]); n         } n       } n       if (bad.size() == 1) { n         ok = true; n         c = *bad.begin(); n       } else if (good.size() == 25) { n         for (char j =  a ; j <=  z ; ++j) { n           if (!good.count(j)) { n             ok = true; n             c = j; n             break; n           } n         } n       } n     } else { n       if (op ==  ! ) ans++; n       if (op ==  ?  && i < n - 1) ans++; n     } n   } n   printf( %d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n; n int x[5001]; n int ans[5001][5001]; n int init() { n   int i, j; n   for (i = 1; i <= n; i++) ans[i][1] = x[i]; n   for (j = 2; j <= n; j++) { n     for (i = 1; i + j - 1 <= n; i++) n       ans[i][j] = ans[i][j - 1] ^ ans[i + 1][j - 1]; n   } n   for (j = 2; j <= n; j++) n     for (i = 1; i + j - 1 <= n; i++) n       ans[i][j] = max(max(ans[i][j], ans[i][j - 1]), ans[i + 1][j - 1]); n   return 0; n } n int main() { n   int i; n   cin >> n; n   for (i = 1; i <= n; i++) cin >> x[i]; n   init(); n   int j, k, p, q; n   cin >> k; n   while (k--) { n     cin >> p >> q; n     cout << ans[p][q - p + 1] << endl; n   } n   return 0; n } n


#include<bits/stdc++.h> n #define ll long long n #define ull unsigned long long n #define pii pair<int,int> n using namespace std; n long long mod=1000000007; n ll gcd(ll a, ll b)  n {  n     if (b == 0)  n         return a;  n     return gcd(b, a % b);  n }  n void solve() n { n     ll n;cin>>n; n     ll a[n]; n     ll sum=0; n     for(int i=0;i<n;i++) cin>>a[i],sum+=a[i]; n     sort(a,a+n); n     ll req=(sum+n-2)/(n-1); n    // cout<<req<<   ; n     ll ans=req*(n-1); n     if(req<a[n-1]) ans=a[n-1]*(n-1); n     cout<<ans-sum<<endl; n } n int main() n { n     ios_base::sync_with_stdio(false); n     cin.tie(0);cout.tie(0); n     int t;cin>>t; n     while(t--) n     { n         solve(); n     } n     return 0; n } n


#include <bits/stdc++.h> n int inp() { n   char c = getchar(); n   while (c <  0  || c >  9 ) c = getchar(); n   int sum = 0; n   while (c >=  0  && c <=  9 ) { n     sum = sum * 10 + c -  0 ; n     c = getchar(); n   } n   return sum; n } n struct Node { n   int l; n   int r; n   int id; n } s[200010]; n int a[200010]; n long long cnt[1000010], ans, res[200010]; n bool cmp(Node a, Node b) { n   if ((a.l / 450) == (b.l / 450)) { n     if ((a.l / 450) & 1) return a.r < b.r; n     return a.r > b.r; n   } n   return a.l < b.l; n } n void add(long long x) { n   x = a[x]; n   ans -= (cnt[x] * cnt[x]) * x; n   cnt[x]++; n   ans += (cnt[x] * cnt[x]) * x; n } n void del(long long x) { n   x = a[x]; n   ans -= (cnt[x] * cnt[x]) * x; n   cnt[x]--; n   ans += (cnt[x] * cnt[x]) * x; n } n int main() { n   int n = inp(); n   int m = inp(); n   for (int i = 1; i <= n; i++) a[i] = inp(); n   for (int i = 1; i <= m; i++) { n     s[i].l = inp(); n     s[i].r = inp(); n     s[i].id = i; n   } n   std::sort(s + 1, s + m + 1, cmp); n   int l = 1; n   int r = 0; n   for (int i = 1; i <= m; i++) { n     while (l > s[i].l) add(--l); n     while (r < s[i].r) add(++r); n     while (l < s[i].l) del(l++); n     while (r > s[i].r) del(r--); n     res[s[i].id] = ans; n   } n   for (int i = 1; i <= m; i++) printf( %lld n , res[i]); n } n


#include <bits/stdc++.h> n using namespace std; n const int NMAX = 300000; n const double eps = 1e-6; n double sy, a, b; n int n, q; n double L[NMAX], R[NMAX]; n double sum[NMAX]; n int dcmp(double x, double y) { n   if (fabs(x - y) < eps) return 0; n   if (x > y) return 1; n   return -1; n } n int bin_search(double x) { n   int l = 1, r = n, ret = 0; n   while (l <= r) { n     int mid = (l + r) / 2; n     if (dcmp(x, R[mid]) >= 0) { n       ret = mid; n       l = mid + 1; n     } else n       r = mid - 1; n   } n   return ret; n } n int main() { n   scanf( %lf%lf%lf , &sy, &a, &b); n   scanf( %d , &n); n   for (int i = 1; i <= n; i++) { n     scanf( %lf%lf , &L[i], &R[i]); n     sum[i] = sum[i - 1] + R[i] - L[i]; n   } n   scanf( %d , &q); n   while (q--) { n     double x0, y0, x1, x2; n     scanf( %lf%lf , &x0, &y0); n     if (dcmp(x0, a) == 0) n       x1 = a; n     else { n       double k = (y0 - sy) / (x0 - a); n       x1 = a - sy / k; n     } n     if (dcmp(x0, b) == 0) n       x2 = b; n     else { n       double k = (y0 - sy) / (x0 - b); n       x2 = b - sy / k; n     } n     int r1 = bin_search(x1); n     int r2 = bin_search(x2); n     double ans = (sum[r2] - sum[r1]); n     if (r1 + 1 <= n && dcmp(L[r1 + 1], x1) < 0) ans -= x1 - L[r1 + 1]; n     if (r2 + 1 <= n && dcmp(L[r2 + 1], x2) < 0) ans += x2 - L[r2 + 1]; n     printf( %.15lf n , (b - a) * ans / (x2 - x1)); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; 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 fast_pow(long long int a, long long int b, n                               long long int mod) { n   long long int res = 1; n   a %= mod; n   while (b > 0) { n     if (b & 1) res = res * a % mod; n     b >>= 1; n     a = a * a % mod; n   } n   return res; n } n inline bool isPrime(long long int a) { n   for (long long int i = 2; i * i <= a; i++) n     if (a % i == 0) return 0; n   return 1; n } n int n, a, b, c, ans, cnt, anss = INT_MAX; n string person1, likes, person2, temp; n pair<string, string> arr[42]; n map<string, int> nametoidx; n vector<string> per; n int main() { n   ios::sync_with_stdio(0); n   cin.tie(NULL); n   nametoidx[ Anka ] = 0; n   nametoidx[ Chapay ] = 1; n   nametoidx[ Cleo ] = 2; n   nametoidx[ Troll ] = 3; n   nametoidx[ Dracul ] = 4; n   nametoidx[ Snowy ] = 5; n   nametoidx[ Hexadecimal ] = 6; n   cin >> n; n   for (int i = 0; i < n; i++) { n     cin >> person1 >> likes >> person2; n     arr[i] = make_pair(person1, person2); n   } n   cin >> a >> b >> c; n   for (int i = 0; i < 5; i++) { n     for (int j = 0; j < 5 - i; j++) { n       int t = max(a / (i + 1), max(b / (j + 1), c / (5 - i - j))) - n               min(a / (i + 1), min(b / (j + 1), c / (5 - i - j))); n       temp =   ; n       if (anss > t) { n         per.clear(); n         anss = t; n         for (int k = 0; k < i + 1; k++) temp +=  0 ; n         for (int k = 0; k < j + 1; k++) temp +=  1 ; n         for (int k = 0; k < 5 - i - j; k++) temp +=  2 ; n         per.push_back(temp); n       } else if (anss == t) { n         for (int k = 0; k < i + 1; k++) temp +=  0 ; n         for (int k = 0; k < j + 1; k++) temp +=  1 ; n         for (int k = 0; k < 5 - i - j; k++) temp +=  2 ; n         per.push_back(temp); n       } n     } n   } n   cout << anss <<    ; n   for (int j = 0; j < per.size(); j++) { n     temp = per[j]; n     do { n       cnt = 0; n       for (int i = 0; i < n; i++) { n         if (temp[nametoidx[arr[i].first]] == temp[nametoidx[arr[i].second]]) n           cnt++; n       } n       ans = max(ans, cnt); n     } while (next_permutation(temp.begin(), temp.end())); n   } n   cout << ans <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e5 + 5, MOD = 1e9 + 7; n const double EPS = 1e-9; n double PI = acos(-1); n double square(long long x) { n   double ret = double(x) * double(x); n   return ret; n } n vector<pair<double, int> > V; n double deg(double x) { return (x * 180) / PI; } n double mod(double x) { n   while (x < 0) x += 2 * PI; n   while (x >= 2 * PI) x -= 2 * PI; n   return x; n } n int main() { n   int n; n   long long x0, y0, v, T, d, x, y, r; n   cin >> x0 >> y0 >> v >> T; n   double dist = v * T; n   double start = 0, ans = 0; n   int cntr = 0; n   cin >> n; n   for (int i = 1; i <= n; i++) { n     cin >> x >> y >> r; n     x -= x0, y -= y0; n     double d = sqrt(square(x) + square(y)); n     if (d <= r) { n       cout <<  1.000000000 n ; n       return 0; n     } n     if (dist >= d - r) { n       double angle; n       if (square(x) + square(y) - square(r) <= square(dist)) n         angle = asin(r / d); n       else n         angle = acos((square(dist) + square(x) + square(y) - square(r)) / n                      (2 * dist * d)); n       angle += EPS; n       double slope = atan2(y, x); n       double a = mod(slope - angle), b = mod(slope + angle); n       V.push_back({a, 1}); n       V.push_back({b, -1}); n       if (a > b) cntr++; n     } n   } n   sort(V.begin(), V.end()); n   for (auto v : V) { n     if (v.second == 1) { n       if (cntr == 0) start = v.first; n       cntr++; n     } else { n       if (cntr == 1) ans += (v.first - start); n       cntr--; n     } n   } n   if (cntr > 0) ans += 2 * PI - start; n   ans = ans / (2 * PI); n   cout << setprecision(10) << fixed << ans << endl; n } n


#include <bits/stdc++.h> n long long CMB[50][50]; n void computeCMB() { n   for (long long i = 0; i < 50; ++i) { n     CMB[i][0] = 1; n     for (long long j = 1; j <= i; ++j) { n       CMB[i][j] = CMB[i - 1][j - 1] + CMB[i - 1][j]; n     } n   } n } n long long n; n long long pathsFrom(long long i, long long j) { n   long long steps = (n - i) + (n - j); n   long long v = (n - i); n   return CMB[steps][v]; n } n long long V[25 + 1][25 + 1]; n long long maxPath(long long x, long long y) { n   long long sum = 0; n   while (x < n) sum += V[x][y], ++x; n   while (y < n) sum += V[x][y], ++y; n   return sum; n } n long long minCost(long long X, long long Y) { n   long long sum = 0; n   long long x = 1, y = 1; n   while (y < Y) sum += V[x][y], ++y; n   while (x < X) sum += V[x][y], ++x; n   return sum; n } n int32_t main() { n   std::ios_base::sync_with_stdio(false); n   std::cin.tie(NULL); n   std::cout.tie(NULL); n   computeCMB(); n   std::cin >> n; n   for (long long j = n - 1, v = 0; j >= 1; --j) { n     for (long long i = 2; i <= n; ++i) { n       v += pathsFrom(i, j + 1); n       V[i][j] = v - maxPath(i, j + 1) - minCost(i, j); n     } n   } n   for (long long i = 1; i <= n; ++i) { n     for (long long j = 1; j <= n; ++j) { n       std::cout << V[i][j] <<    ; n     } n     std::cout << std::endl; n     std::cout.flush(); n   } n   long long q; n   std::cin >> q; n   for (long long i = 0, k; i < q; ++i) { n     std::cin >> k; n     long long x = 1, y = 1; n     long long weight = 0; n     for (long long ln = 0; ln < 2 * n - 1; ++ln) { n       std::cout << x <<     << y << std::endl; n       std::cout.flush(); n       long long v = maxPath(x, y + 1); n       if (y == n || weight + v < k) { n         ++x; n       } else { n         ++y; n       } n       weight += V[x][y]; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using vi = std::vector<long long int>; n using vvi = std::vector<vi>; n using pii = std::pair<long long int, long long int>; n using vpii = std::vector<pii>; n using vvpii = std::vector<vpii>; n using namespace std; n const long long int N = 2e6 + 10; n const long long int inf = 1e18 + 10; n const long long int M = 1e9 + 7; n const long double Pi = 3.14159265; n long long int n, b, k, x, a[N]; n long long int mul(long long int a, long long int b) { n   return ((a % M) * (b % M)) % M; n } n long long int add(long long int a, long long int b) { n   return ((a % M) + (b % M)) % M; n } n vvi p(vvi a, vvi b) { n   vvi x(n, vi(n, 0)); n   for (long long int i = 0; i <= n - 1; i++) n     for (long long int j = 0; j <= n - 1; j++) n       for (long long int k = 0; k <= n - 1; k++) n         x[i][j] = add(x[i][j], mul(a[i][k], b[k][j])); n   return x; n } n vvi matrix_power_final(vvi A, long long int x) { n   vvi result(n, vi(n, 0)); n   for (long long int i = 0; i <= n - 1; i++) result[i][i] = 1; n   while (x) { n     if (x & 1) result = p(result, A); n     A = p(A, A); n     x = x / 2; n   } n   return result; n } n void solve() { n   cin >> n >> b >> k >> x; n   vvi v(x, vi(x, 0)); n   for (long long int i = 1; i <= n; i++) { n     cin >> a[i]; n   } n   for (long long int i = 1; i <= n; i++) { n     for (long long int j = 0; j <= x - 1; j++) { n       a[i] %= x; n       v[j][(j * 10 + a[i]) % x]++; n     } n   } n   n = x; n   v = matrix_power_final(v, b); n   cout << v[0][k]; n } n int32_t main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   solve(); n   long long int xx = 0; n   return 0; n } n


#include <bits/stdc++.h> n #pragma GCC optimize( O3 ) n #pragma GCC optimize( unroll-loops ) n #pragma GCC target( avx2 ) n using namespace std; n const int MOD = 1000 * 1000 * 1000 + 7; n signed main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   int a, b, x1, y1, x2, y2; n   cin >> a >> b >> x1 >> y1 >> x2 >> y2; n   int x1_ = x1 + y1; n   int y1_ = x1 - y1; n   int x2_ = x2 + y2; n   int y2_ = x2 - y2; n   x1 = x1_ / (2 * a); n   if (x1_ > 0) x1++; n   x2 = x2_ / (2 * a); n   if (x2_ > 0) x2++; n   y1 = y1_ / (2 * b); n   if (y1_ > 0) y1++; n   y2 = y2_ / (2 * b); n   if (y2_ > 0) y2++; n   cout << max(abs(y2 - y1), abs(x1 - x2)) <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<int> a[200005], b; n set<int> c1; n int n, k, p, f[200005], x[200005], c[200005], c2[200005]; n int getdeepth(int i) { n   for (vector<int>::iterator j = a[i].begin(); j != a[i].end(); j++) n     if ((*j) != f[i]) { n       f[*j] = i; n       int x = getdeepth(*j); n       c[i] += c[*j]; n       if (c[*j] >= k) return x; n     } n   return i; n } n void getdeepth1(int i) { n   for (vector<int>::iterator j = a[i].begin(); j != a[i].end(); j++) n     if ((*j) != f[i]) getdeepth1(*j); n   if (c2[i]) b.push_back(i), c1.erase(i); n } n int main() { n   cin.sync_with_stdio(false); n   cout.sync_with_stdio(false); n   cin >> n >> k; n   for (int i = 1, j, l; i < n; i++) { n     cin >> j >> l; n     a[j].push_back(l); n     a[l].push_back(j); n   } n   for (int i = 1, j; i <= 2 * k; i++) { n     cin >> j; n     c[j] = c2[j] = 1; n     c1.insert(j); n   } n   p = getdeepth(1); n   getdeepth1(p); n   for (set<int>::iterator i = c1.begin(); i != c1.end(); i++) b.push_back(*i); n   cout << 1 << endl << p << endl; n   for (int i = 0; i < k; i++) n     cout << b[i] <<     << b[i + k] <<     << p << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, val[200100], m; n set<pair<int, int> > s[200100]; n struct opt { n   int tp, x, y; n } q[200100]; n inline void read(int &x) { n   x = 0; n   register char c = getchar(); n   while (c >  9  || c <  0 ) c = getchar(); n   while (c >=  0  && c <=  9 ) n     x = (x << 3) + (x << 1) + (c ^ 48), c = getchar(); n } n inline void print(int x) { n   if (x <= 9) n     putchar( 0  + x); n   else n     print(x / 10), putchar( 0  + x % 10); n } n void ins(int x, pair<int, int> k) { n   while (x) s[x].insert(k), x -= x & -x; n } n void del(int x, pair<int, int> k) { n   while (x) s[x].erase(k), x -= x & -x; n } n pair<int, int> ask(int x, pair<int, int> k) { n   pair<int, int> res = make_pair(0x3f3f3f3f, 0x3f3f3f3f); n   while (x <= m) res = min(res, *s[x].lower_bound(k)), x += x & -x; n   return res; n } n int main() { n   read(n); n   for (register int i = 1; i <= n; i++) { n     char str[10]; n     scanf( %s , str), read(q[i].x), read(q[i].y); n     if (str[0] ==  a ) q[i].tp = 1; n     if (str[0] ==  r ) q[i].tp = 2; n     if (str[0] ==  f ) q[i].tp = 3, q[i].x++, q[i].y++; n     val[++m] = q[i].y; n   } n   sort(val + 1, val + m + 1), m = unique(val + 1, val + m + 1) - val + 1; n   for (int i = 1; i <= m; i++) s[i].insert(make_pair(0x3f3f3f3f, 0x3f3f3f3f)); n   for (register int i = 1; i <= n; i++) { n     if (q[i].tp == 1) n       ins(lower_bound(val + 1, val + m, q[i].y) - val, n           make_pair(q[i].x, q[i].y)); n     if (q[i].tp == 2) n       del(lower_bound(val + 1, val + m, q[i].y) - val, n           make_pair(q[i].x, q[i].y)); n     if (q[i].tp == 3) { n       register pair<int, int> tmp = n           ask(lower_bound(val + 1, val + m, q[i].y) - val, n               make_pair(q[i].x, q[i].y)); n       if (tmp == make_pair(0x3f3f3f3f, 0x3f3f3f3f)) n         putchar( - ), putchar( 1 ), putchar(  n ); n       else n         print(tmp.first), putchar(   ), print(tmp.second), putchar(  n ); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n template <typename T, typename U> n inline void swap(T &a, U &b) { n   a = a ^ b; n   b = a ^ b; n   a = a ^ b; n } n inline void swap(int &a, int &b) { n   a = a ^ b; n   b = a ^ b; n   a = a ^ b; n } n inline void swap(long long &a, long long &b) { n   a = a ^ b; n   b = a ^ b; n   a = a ^ b; n } n template <typename T, typename U> n inline void smax(T &a, U b) { n   if (b > a) a = b; n } n template <typename T, typename U> n inline void smin(T &a, U b) { n   if (b < a) a = b; n } n inline int gcd(int a, int b) { n   if (a < b) swap(a, b); n   while (b > 0) { n     a %= b; n     swap(a, b); n   } n   return a; n } n long long nchose(int a, int b) { n   long long ans = 1; n   smax(a, b - a); n   int p = 2; n   for (int i = (b - a + 1); i <= (b); i++) { n     ans *= (long long)i; n     while (p <= a && ans % p == 0) ans /= p++; n   } n   return ans; n } n using namespace std; n static long long MOD = 1000000009; n string s, t; n int main() { n   cin >> s >> t; n   sort(s.begin(), s.end()); n   for (char &i : s) { n     if (i !=  0 ) { n       swap(i, s[0]); n       break; n     } n   } n   if (s == t) { n     cout <<  OK ; n   } else { n     cout <<  WRONG_ANSWER ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long n, m, i, j; n   string s; n   cin >> s; n   n = s.size(); n   string cur; n   if (s.size() <= 6) { n     cout << 0; n     return 0; n   } n   for (i = 5; i < s.size(); i++) { n     cur += s[i]; n   } n   if (cur.size() == 2) { n     cout << 1 << endl << cur; n     return 0; n   } n   reverse(cur.begin(), cur.end()); n   string inf =  --- ; n   vector<string> dp2(10500), dp3(10500); n   vector<string> ans; n   dp2[0] = inf; n   dp2[1] += cur[0]; n   dp2[1] += cur[1]; n   dp3[0] = inf; n   dp3[1] = inf; n   dp3[2] += cur[0]; n   dp3[2] += cur[1]; n   dp3[2] += cur[2]; n   dp2[2] = inf; n   ans.push_back(dp2[1]); n   ans.push_back(dp3[2]); n   for (i = 3; i < cur.size(); i++) { n     bool two = true; n     if (dp2[i - 2] == inf && dp3[i - 2] == inf) { n       dp2[i] = inf; n       two = false; n     } n     { n       if (dp2[i - 2] != inf) { n         string ss; n         ss += cur[i - 1]; n         ss += cur[i]; n         if (ss != dp2[i - 2]) { n           dp2[i] = ss; n           ans.push_back(ss); n         } else n           two = false; n       } n       { n         if (dp3[i - 2] != inf) { n           two = true; n           string ss; n           ss += cur[i - 1]; n           ss += cur[i]; n           dp2[i] = ss; n           ans.push_back(ss); n         } n       } n     } n     if (!two) dp2[i] = inf; n     bool three = true; n     if (dp3[i - 3] == inf && dp2[i - 3] == inf) { n       dp3[i] = inf; n       three = false; n     } n     { n       if (dp3[i - 3] != inf) { n         string ss; n         ss += cur[i - 2]; n         ss += cur[i - 1]; n         ss += cur[i]; n         if (ss != dp3[i - 3]) { n           dp3[i] = ss; n           ans.push_back(ss); n         } else n           three = false; n       } n       { n         if (dp2[i - 3] != inf) { n           string ss; n           ss += cur[i - 2]; n           ss += cur[i - 1]; n           ss += cur[i]; n           ans.push_back(ss); n           dp3[i] = ss; n           three = true; n         } n       } n     } n     if (!three) dp3[i] = inf; n   } n   for (i = 0; i < ans.size(); i++) { n     reverse(ans[i].begin(), ans[i].end()); n   } n   sort(ans.begin(), ans.end()); n   set<string> h; n   set<string>::iterator ii; n   for (i = 0; i < ans.size(); i++) h.insert(ans[i]); n   cout << h.size() << endl; n   for (ii = h.begin(); ii != h.end(); ii++) cout << *ii << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int a, k1 = 0, k2 = 0; n   cin >> a; n   string h; n   cin >> h; n   string *s = new string[a]; n   for (int i = 0; i < a; i++) { n     cin >> s[i]; n     if (s[i].substr(s[i].size() - 3, 3) == h) n       k1++; n     else n       k2++; n   } n   if (k1 == k2) n     cout <<  home ; n   else n     cout <<  contest ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long p, d, i, j, tmp, res; n   cin >> p >> d; n   res = p; n   for (i = 999999999999999999ll; i; i /= 10) { n     j = i + 1; n     if (i > p) continue; n     if (i == p) break; n     if (p % j == i) { n       tmp = (p / j) * j + i; n       if (p - tmp <= d) { n         res = tmp; n         break; n       } n     } else { n       tmp = (p / j - 1) * j + i; n       if (p - tmp <= d) { n         res = tmp; n         break; n       } n     } n   } n   cout << res << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 1e6; n int n; n int l[maxn], c[maxn]; n int gcd(int x, int y) { n   if (x == 0) n     return y; n   else n     return gcd(y % x, x); n } n map<int, int> mp; n int main() { n   while (scanf( %d , &n) != EOF) { n     for (int i = 1; i <= n; i++) scanf( %d , &l[i]); n     for (int i = 1; i <= n; i++) scanf( %d , &c[i]); n     mp.clear(); n     mp[0] = 0; n     for (int i = 1; i <= n; i++) { n       for (map<int, int>::iterator it = mp.begin(); it != mp.end(); it++) { n         int v = gcd(it->first, l[i]); n         int cc = it->second + c[i]; n         if (mp.count(v) > 0) { n           mp[v] = min(mp[v], cc); n         } else n           mp[v] = cc; n       } n     } n     if (mp.count(1) > 0) n       printf( %d n , mp[1]); n     else n       printf( -1 n ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long n, mx[400000], mn[400000]; n long long seg[400000 * 4]; n vector<long long> compress(vector<long long> x) { n   vector<long long> ans = x; n   vector<pair<long long, long long>> p; n   p.push_back({0, 0}); n   for (long long i = 1; i < x.size(); i++) { n     p.push_back({x[i], i}); n   } n   sort(p.begin(), p.end()); n   ans[p[1].second] = 1; n   for (long long i = 2; i < p.size(); i++) { n     if (p[i].first > p[i - 1].first) n       ans[p[i].second] = ans[p[i - 1].second] + 1; n     else n       ans[p[i].second] = ans[p[i - 1].second]; n   } n   return ans; n } n void update(long long pos, long long val, long long l1, long long r1, n             long long node) { n   if (l1 == r1) { n     seg[node] = val; n     return; n   } n   long long mid = (l1 + r1) / 2; n   if (pos <= mid) { n     update(pos, val, l1, mid, 2 * node); n   } else n     update(pos, val, mid + 1, r1, 2 * node + 1); n   seg[node] = max(seg[2 * node], seg[2 * node + 1]); n } n long long queri(long long l, long long r, long long l1, long long r1, n                 long long node) { n   if (l1 > r or r1 < l) return 0; n   if (l <= l1 and r1 <= r) return seg[node]; n   long long mid = (l1 + r1) / 2; n   return max(queri(l, r, l1, mid, 2 * node), n              queri(l, r, mid + 1, r1, 2 * node + 1)); n } n void solv() { n   cin >> n; n   vector<long long> a(n + 1); n   set<long long> s; n   for (long long i = 1; i < n + 1; i++) { n     cin >> a[i]; n     s.insert(a[i]); n     mx[i] = 0; n     mn[i] = 400000; n   } n   a = compress(a); n   for (long long i = 1; i < n + 1; i++) { n     mx[a[i]] = i; n     mn[a[i]] = min(mn[a[i]], i); n   } n   long long l = 1; n   long long len = 0; n   for (long long i = 1; i < s.size() + 1; i++) { n     update(i, mx[i], 1, n, 1); n   } n   for (long long i = 1; i < s.size() + 1; i++) { n     while (queri(l, i - 1, 1, n, 1) > mn[i] and l < i) l++; n     len = max(len, i - l + 1); n   } n   long long c = s.size(); n   cout << c - len <<   n ; n } n signed main() { n   cin.tie(0); n   cout.tie(0); n   ios_base::sync_with_stdio(false); n   long long q; n   cin >> q; n   while (q--) solv(); n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 25; n const int maxm = 3000; n char maze[maxn][maxn]; n int g[maxn * maxn][maxn * maxn]; n int n, m, N, M; n int src, sink; n long long ans; n struct Node { n   int r, c, t; n   Node() {} n   Node(int r, int c, int t) : r(r), c(c), t(t) {} n }; n vector<Node> males, fmales; n struct Edge { n   int u, v, c; n   Edge() {} n   Edge(int u, int v, int c) : u(u), v(v), c(c) {} n }; n struct MaxFlow { n   int n, m; n   int Now[maxm], Dfn[maxm]; n   vector<Edge> edges; n   vector<int> G[maxm]; n   void init(int n) { n     this->n = n; n     for (auto &x : G) x.clear(); n     edges.clear(); n   } n   void add(int u, int v, int c) { n     edges.push_back(Edge(u, v, c)); n     edges.push_back(Edge(v, u, 0)); n     m = ((int)((edges).size())); n     G[u].push_back(m - 2); n     G[v].push_back(m - 1); n   } n   int ISAP(int s, int flow) { n     if (s == sink) return flow; n     int v, tab = n, now = 0, vary; n     for (int i = 0; i < ((int)((G[s]).size())); i++) { n       Edge &e = edges[G[s][i]]; n       if (e.c > 0) { n         if (Dfn[s] == Dfn[v = e.v] + 1) n           vary = ISAP(v, min(flow - now, e.c)), now += vary, e.c -= vary, n           edges[G[s][i] ^ 1].c += vary; n         if (Dfn[src] == n) return now; n         if (e.c > 0) tab = min(tab, Dfn[v]); n         if (now == flow) break; n       } n     } n     if (now == 0) { n       if (--Now[Dfn[s]] == 0) Dfn[src] = n; n       ++Now[Dfn[s] = tab + 1]; n     } n     return now; n   } n   int getAns() { n     int flow = 0; n     memset(Dfn, 0, sizeof Dfn); n     memset(Now, 0, sizeof Now); n     Now[0] = n; n     while (Dfn[src] < n) flow += ISAP(src, 0x0f0f0f0f); n     return flow; n   } n } solver; n int idx(char x, int y) { return x * m + y; } n bool cango(int x, int y) { return x >= 0 && x < n && y >= 0 && y < m; } n int dx[] = {0, 0, -1, 1}; n int dy[] = {-1, 1, 0, 0}; n void calDist() { n   memset(g, 0x0f, sizeof g); n   for (int i = 0; i < n * m; i++) g[i][i] = 0; n   for (int x = 0; x < n; x++) n     for (int y = 0; y < m; y++) { n       if (maze[x][y] !=  . ) continue; n       int u = idx(x, y); n       for (int k = 0; k < 4; k++) { n         int nx = x + dx[k]; n         int ny = y + dy[k]; n         if (!cango(nx, ny) || maze[nx][ny] ==  # ) continue; n         int v = idx(nx, ny); n         g[u][v] = 1; n       } n     } n   for (int k = 0; k < n * m; k++) n     for (int i = 0; i < n * m; i++) n       for (int j = 0; j < n * m; j++) { n         if (g[i][k] == 0x0f0f0f0f || g[k][j] == 0x0f0f0f0f) continue; n         g[i][j] = min(g[i][j], g[i][k] + g[k][j]); n       } n } n bool check(long long mid) { n   src = 2 * N + 2 * n * m; n   sink = src + 1; n   solver.init(sink + 1); n   for (int j = 0; j < n; j++) n     for (int k = 0; k < m; k++) { n       if (maze[j][k] ==  # ) continue; n       int v = idx(j, k); n       int id1 = 2 * N + v * 2; n       int id2 = id1 + 1; n       solver.add(id1, id2, 1); n     } n   for (int i = 0; i < N; i++) { n     int x = males[i].r, y = males[i].c, t = males[i].t; n     int u = idx(x, y); n     solver.add(src, i, 1); n     for (int j = 0; j < n; j++) n       for (int k = 0; k < m; k++) { n         if (maze[j][k] ==  # ) continue; n         int v = idx(j, k); n         int id1 = 2 * N + v * 2; n         int id2 = id1 + 1; n         if (g[u][v] != 0x0f0f0f0f && 1.0 * g[u][v] * t <= mid) n           solver.add(i, id1, 1); n       } n     x = fmales[i].r, y = fmales[i].c, t = fmales[i].t; n     u = idx(x, y); n     solver.add(i + N, sink, 1); n     for (int j = 0; j < n; j++) n       for (int k = 0; k < m; k++) { n         if (maze[j][k] ==  # ) continue; n         int v = idx(j, k); n         int id1 = 2 * N + v * 2; n         int id2 = id1 + 1; n         if (g[u][v] != 0x0f0f0f0f && 1.0 * g[u][v] * t <= mid) n           solver.add(id2, i + N, 1); n       } n   } n   return solver.getAns() == N; n } n int main() { n   cin >> n >> m >> N >> M; n   for (int i = 0; i < n; i++) scanf( %s , maze[i]); n   int ok = 1; n   int r, c, t; n   scanf( %d%d%d , &r, &c, &t); n   r--, c--; n   if (N - M == 1) { n     fmales.push_back(Node(r, c, t)); n   } else if (M - N == 1) { n     males.push_back(Node(r, c, t)); n   } else { n     ok = 0; n   } n   for (int i = 0; i < N; i++) { n     scanf( %d%d%d , &r, &c, &t); n     r--, c--; n     males.push_back(Node(r, c, t)); n   } n   for (int i = 0; i < M; i++) { n     int r, c, t; n     scanf( %d%d%d , &r, &c, &t); n     r--, c--; n     fmales.push_back(Node(r, c, t)); n   } n   if (!ok) { n     puts( -1 ); n     return 0; n   } n   N = max(N, M); n   calDist(); n   long long L = 0, R = (long long)1e15; n   while (R - L > 0) { n     long long mid = (L + R) >> 1; n     if (check(mid)) n       R = mid; n     else n       L = mid + 1; n   } n   if (R != (long long)1e15) { n     printf( %lld n , R); n   } else n     puts( -1 ); n   return 0; n } n


#include <bits/stdc++.h> n int n; n long long ans = 0x7fffffffffffffff, sum, p[1000002]; n int a[1000002], b[1000002], c[1000002], pcnt; n inline int abs(int x) { return x > 0 ? x : -x; } n signed main() { n   scanf( %d , &n); n   for (register int i = 1; i <= n; i++) scanf( %d , &a[i]), sum += a[i]; n   register long long temp = sum; n   for (register int i = 2; (long long)i * i <= temp;) n     if (sum % i == 0) n       sum /= i, p[++pcnt] = i; n     else n       i++; n   if (sum > 1) p[++pcnt] = sum; n   for (register long long _k = 1, k = p[_k]; _k <= pcnt; _k++, k = p[_k]) { n     if (p[_k] == p[_k - 1]) continue; n     long long res = 0; n     for (register int i = 1; i <= n; i++) b[i] = a[i] % k; n     for (register long long i = 1, last = 1, t = 0; i <= n;) { n       t += b[i]; n       c[i] = b[i], b[i] = 0; n       if (t >= k) { n         c[i] -= t - k, b[i] = t - k; n         t = k; n         register int mid; n         register long long s = 0; n         for (mid = last; mid <= i; mid++) { n           s += c[mid]; n           if (s >= (k + 1) / 2) break; n         } n         for (register int j = last; j <= i; j++) n           res += (long long)c[j] * abs(j - mid); n         t = 0, last = i; n       } else n         i++; n     } n     if (res < ans) ans = res; n   } n   printf( %I64d , ans == 0x7fffffffffffffff ? -1 : ans); n   return 0; n } n


#include <bits/stdc++.h> n int main() { n   int a, b, y, ans; n   scanf( %d%d , &a, &b); n   y = a * b; n   ans = y / 2; n   printf( %d , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, k; n   long long res = 0; n   cin >> n; n   vector<bool> v(6000, false); n   for (int i = 0; i < n; ++i) { n     cin >> k; n     if (!v[k - 1]) n       v[k - 1] = true; n     else n       while (v[k - 1]) { n         ++res; n         ++k; n         v[k - 2] = true; n       } n     v[k - 1] = true; n   } n   cout << res; n } n


#include <bits/stdc++.h> n using namespace std; n const long long int mod = 1000000007; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(NULL); n   cout.tie(NULL); n   long long int n, h; n   cin >> n >> h; n   long long int arr[n]; n   for (long long int i = 0; i < n; i++) cin >> arr[i]; n   long long int ans = 0; n   for (long long int i = 0; i < n; i++) { n     vector<long long int> op; n     for (long long int j = 0; j <= i; j++) op.push_back(arr[j]); n     sort(op.begin(), op.end()); n     if (op.size() == 1) { n       if (op[0] <= h) ans = i + 1; n       continue; n     } n     long long int sum = 0; n     for (long long int j = op.size() - 1; j >= 0; j -= 2) sum += op[j]; n     if (sum <= h) n       ans = i + 1; n     else n       break; n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, p, q, a, c = 1, b; n   cin >> n; n   cin >> a >> b; n   p = a; n   q = b; n   int num = n; n   n--; n   int max = 1; n   while (n--) { n     cin >> a >> b; n     if (p == a && q == b) { n       c++; n     } else if (c > 1) { n       if (max < c) max = c; n       c = 1; n     } n     p = a; n     q = b; n   } n   if (c > max) max = c; n   cout << max; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int M = 2e5; n int infty = 1e9; n int p[M], t[M]; n int main() { n   int n, d, h; n   cin >> n >> d >> h; n   if (2 * h < d || (n > 2 && d == 1)) { n     cout << -1 <<   n ; n   } else { n     int h1 = h, h2 = d - h, cur = 2; n     for (int i = 0; i < h; i++) { n       cout << cur - 1 <<     << cur <<   n ; n       cur++; n     } n     if (d == h) { n       for (int i = cur; i <= n; i++) { n         cout << 2 <<     << i <<   n ; n       } n     } else { n       if (h2 > 0) { n         cout <<  1   << cur <<   n ; n         cur++; n       } n       for (int i = 1; i < h2; i++) { n         cout << cur - 1 <<     << cur <<   n ; n         cur++; n       } n       for (int i = cur; i <= n; i++) { n         cout << 1 <<     << i <<   n ; n       } n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n #pragma comment(linker,  /STACK:102400000,102400000 ) n using namespace std; n int a[(100000 + 10)], cur[(100000 + 10)]; n double dp[(100000 + 10)][110]; n int C[110][110]; n void init() { n   C[0][0] = 1; n   int n = 110; n   for (int i = 1; i < n; ++i) { n     C[i][0] = C[i][i] = 1; n     for (int j = 1; j < i; ++j) C[i][j] = C[i - 1][j] + C[i - 1][j - 1]; n   } n } n double f(int a, int b) { n   double ret = 1; n   for (int i = 1; i <= b; ++i) ret *= (a - i + 1.0) / i; n   return ret; n } n double cal(int cur, int i, int j, int k) { n   if (cur - i - j < 0 || k - j < 0 || k > cur) return 0; n   if (k - j > cur - i - j) return 0; n   return 1.0 * f(cur - i - j, k - j) * f(i + j, j) / f(cur, k); n } n int read() { n   char c; n   while ((c = getchar()) && !( 0  <= c && c <=  9 )) n     ; n   int ret = c -  0 ; n   while ((c = getchar()) &&  0  <= c && c <=  9 ) ret = ret * 10 + c -  0 ; n   return ret; n } n int main() { n   int n; n   init(); n   scanf( %d , &n); n   for (int i = 1; i <= n; ++i) { n     scanf( %d , &a[i]); n     cur[i] = a[i]; n     dp[i][a[i]] = 1; n   } n   double ans = 0; n   for (int i = 1; i <= n; ++i) ans += dp[i][0]; n   int q; n   scanf( %d , &q); n   while (q--) { n     int u, v, k; n     u = read(); n     v = read(); n     k = read(); n     ans -= dp[u][0]; n     for (int i = 0; i <= a[u]; ++i) { n       dp[u][i] = dp[u][i] * cal(cur[u], i, 0, k); n       for (int j = 1; i + j <= a[u] && j <= k; ++j) { n         dp[u][i] += dp[u][i + j] * cal(cur[u], i, j, k); n       } n     } n     ans += dp[u][0]; n     cur[u] -= k; n     cur[v] += k; n     printf( %.10lf n , ans); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long N = 1e6 + 10, P = 1e9 + 7, U = 1e6 - 1; n long long f1[N], f2[N], f3[N], n, A[N], t[N], val[N], bit[N], ans; n int main() { n   cin >> n; n   for (long long i = bit[0] = 1; i <= 6; i++) bit[i] = 10 * bit[i - 1]; n   for (long long i = t[0] = 1; i <= n; i++) t[i] = 2ll * t[i - 1] % P; n   for (long long i = 1, x; i <= n; i++) n     scanf( %lld , &x), f1[x]++, (f2[x] += x) %= P, n         (f3[x] += 1ll * x * x % P) %= P; n   for (long long j = 0; j < 6; j++) n     for (long long i = U; i >= 0; i--) n       if (i / bit[j] % 10 < 9) { n         f1[i] += f1[i + bit[j]]; n         (f2[i] += f2[i + bit[j]]) %= P; n         (f3[i] += f3[i + bit[j]]) %= P; n       } n   for (long long i = 0; i <= U; i++) { n     long long res = (f2[i] * f2[i] % P - f3[i] + P) % P * (P / 2 + 1) % P; n     if (f1[i]) val[i] = (t[f1[i] - 1] * (f3[i] + res) % P + P) % P; n   } n   for (long long j = 0; j < 6; j++) n     for (long long i = 0; i <= U; i++) n       if (i / bit[j] % 10 < 9) (val[i] += P - val[i + bit[j]]) %= P; n   for (long long i = 0; i <= U; i++) ans ^= val[i] * i; n   cout << ans << endl; n } n


#include <bits/stdc++.h> n int main() { n   long long t; n   scanf( %lld , &t); n   long int n, x = 1; n   for (int i = 0; i < t; i++) { n     scanf( %ld , &n); n     int k = 1, p = 1, x; n     for (; p <= n;) { n       k++; n       p = p + pow(2, k - 1); n       if (n % p == 0) { n         x = n / p; n         break; n       } n     } n     printf( %ld n , x); n   } n   return 0; n } n


#include <bits/stdc++.h> n #pragma GCC omptimize( unroll-loops ) n #pragma optimize( SEX_ON_THE_BEACH ) n #pragma GCC optimize( no-stack-protector ) n #pragma comment(linker,  /STACK:1000000000 ) n using namespace std; n using ll = long long int; n using ull = unsigned long long int; n using dd = long double; n using ldd = long double; n using si = short int; n using pii = pair<int, int>; n using pll = pair<ll, ll>; n ll get_seed(string s) { n   ll ans = 0; n   for (int i = 0; i < s.size(); ++i) ans += s[i]; n   return ans; n } n int popcount(int x) { return __builtin_popcount(x); } n ll popcountll(ll x) { return __builtin_popcount(x); } n int way[10][10]; n void build(int x, int y) { n   for (int i = 0; i < 10; ++i) n     for (int j = 0; j < 10; ++j) way[i][j] = 1e5; n   for (int i = 0; i < 10; ++i) way[i][(i + x) % 10] = way[i][(i + y) % 10] = 1; n   for (int k = 0; k < 10; ++k) n     for (int i = 0; i < 10; ++i) n       for (int j = 0; j < 10; ++j) n         way[i][j] = min(way[i][j], way[i][k] + way[k][j]); n   for (int i = 0; i < 10; ++i) n     for (int j = 0; j < 10; ++j) n       if (way[i][j] >= (int)(1e5)) way[i][j] = -1; n } n int get_ans(int x, int y, string& s) { n   build(x, y); n   ll ans = 0; n   int curr = 0; n   for (int i = 1; i < s.size(); ++i) { n     if (way[curr][s[i] -  0 ] == -1) return -1; n     ans += way[curr][s[i] -  0 ] - 1; n     curr = s[i] -  0 ; n   } n   return ans; n } n void solve(int ii) { n   string s; n   cin >> s; n   for (int x = 0; x < 10; ++x) { n     for (int y = 0; y < 10; ++y) cout << get_ans(x, y, s) <<    ; n     cout <<   n ; n   } n } n int main() { n   ios_base::sync_with_stdio(0); n   cout.tie(0); n   cin.tie(0); n   int t; n   t = 1; n   for (int i = 0; i < t; ++i) solve(i); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   int t; n   cin >> t; n   while (t--) { n     int n; n     cin >> n; n     std::vector<pair<long long int, long long int> > ev; n     std::vector<pair<long long int, long long int> > od; n     for (int i = 1; i <= 2 * n; i++) { n       int x; n       cin >> x; n       if (x % 2 == 0) n         ev.push_back({x, i}); n       else n         od.push_back({x, i}); n     } n     if (ev.size() % 2 == 0) { n       if (ev.size() == 0) { n         od.pop_back(); n         od.pop_back(); n       } else { n         ev.pop_back(); n         ev.pop_back(); n       } n     } else { n       ev.pop_back(); n       od.pop_back(); n     } n     for (int i = 0; i < ev.size(); i += 2) n       cout << ev[i].second <<     << ev[i + 1].second <<   n ; n     for (int i = 0; i < od.size(); i += 2) n       cout << od[i].second <<     << od[i + 1].second <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void FAST(); n long long int big_mod(long long int val, long long int pow, long long int mod); n long long int mod_inverse(long long int val, long long int mod); n int main() { n   FAST(); n   int m, n; n   scanf( %d %d , &n, &m); n   if (m < 2) { n     printf( 1 ); n   } else { n     if (n & 1 == 0) { n       if (m >= n / 2) { n         printf( %d , n - m); n       } else { n         printf( %d , m); n       } n     } else { n       if (m > n / 2) { n         printf( %d , n - m); n       } else { n         printf( %d , m); n       } n     } n   } n   return 0; n } n void FAST() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n } n long long int big_mod(long long int val, long long int pow, long long int mod) { n   long long int res = 1; n   val = val % mod; n   while (pow > 0) { n     if (pow & 1) { n       res = (res * val) % mod; n     } n     pow = pow >> 1; n     val = (val * val) % mod; n   } n   return res; n } n long long int mod_inverse(long long int val, long long int mod) { n   long long int mod1 = mod; n   if (mod == 1) { n     return 0; n   } n   long long int y = 0; n   long long int x = 1; n   long long int quotient, temp; n   while (val > 1) { n     quotient = val / mod; n     temp = mod; n     mod = val % mod; n     val = temp; n     temp = y; n     y = x - quotient * y; n     x = temp; n   } n   if (x < 0) { n     x += mod1; n   } n   return x; n } n


#include <bits/stdc++.h> n using namespace std; n int n, d; n long long h[100001]; n struct A { n   long long h; n   int i; n   bool operator<(const A &B) const { return h < B.h || h == B.h && i < B.i; } n } H[100001]; n map<long long, int> MP; n map<long long, int> MPSORT; n int N, Kn; n int st[100001 << 2]; n int sti[100001 << 2]; n void init() { n   N = 1; n   while (N < Kn + 2) N <<= 1; n   memset(st, 0, sizeof(st)); n   memset(sti, 0, sizeof(sti)); n   st[N] = st[2 * N - 1] = -1; n } n int query(int L, int R, int &I) { n   int MAX = 0; n   I = 0; n   for (int s = N + L - 1, t = N + R + 1; s ^ t ^ 1; s >>= 1, t >>= 1) { n     if (~s & 1) n       if (MAX < st[s ^ 1]) MAX = st[s ^ 1], I = sti[s ^ 1]; n     if (t & 1) n       if (MAX < st[t ^ 1]) MAX = st[t ^ 1], I = sti[t ^ 1]; n   } n   return MAX; n } n void update(int X, int C, int Oldi) { n   int s = X + N; n   st[s] = C; n   sti[s] = Oldi; n   s >>= 1; n   while (s > 0) { n     if (st[s << 1] > st[s << 1 | 1]) n       st[s] = st[s << 1], sti[s] = sti[s << 1]; n     else n       st[s] = st[s << 1 | 1], sti[s] = sti[s << 1 | 1]; n     s >>= 1; n   } n } n int BS1(long long X) { n   if (X < H[1].h) return 0; n   int L, R, M; n   L = 1, R = Kn + 1; n begin: n   if (L + 1 == R) goto end; n   M = (L + R) >> 1; n   if (H[M].h <= X) n     L = M; n   else n     R = M; n   goto begin; n end: n   return L; n } n int BS2(long long X) { n   if (X > H[Kn].h) return 0; n   int L, R, M; n   L = 0, R = Kn; n begin: n   if (L + 1 == R) goto end; n   M = (L + R) >> 1; n   if (H[M].h < X) n     L = M; n   else n     R = M; n   goto begin; n end: n   return R; n } n int dp[100001]; n int pre[100001]; n int main(void) { n   while (~scanf( %d%d , &n, &d)) { n     MP.clear(); n     for (int i = 1; i <= n; ++i) n       scanf( %I64d , &h[i]), H[i].h = h[i], H[i].i = i; n     sort(H + 1, H + n + 1); n     Kn = 0; n     for (int i = 1; i <= n; ++i) n       if (!MP.count(H[i].h)) n         MP[H[i].h] = ++Kn, H[Kn].h = H[i].h, H[Kn].i = H[i].i; n     init(); n     for (int i = 1; i <= n; ++i) dp[i] = 1, pre[i] = i; n     for (int i = 1; i <= n; ++i) { n       int ANS, ANS1, ANS2, D; n       int D1 = BS1(h[i] - d), D2 = BS2(h[i] + d); n       if (D1) n         ANS1 = query(1, D1, D1); n       else n         ANS1 = 0; n       if (D2) n         ANS2 = query(D2, Kn, D2); n       else n         ANS2 = 0; n       if (ANS1 > ANS2) n         ANS = ANS1, D = D1; n       else n         ANS = ANS2, D = D2; n       if (ANS > 0) { n         dp[i] = ANS + 1; n         pre[i] = D; n       } n       update(MP[h[i]], dp[i], i); n     } n     int ANS = 0, ANSN; n     for (int i = 1; i <= n; ++i) n       if (dp[i] > ANS) ANS = dp[i], ANSN = i; n     printf( %d n , ANS); n     vector<int> V; n     while (pre[ANSN] != ANSN) { n       V.push_back(ANSN); n       ANSN = pre[ANSN]; n     } n     printf( %d , ANSN); n     for (int i = V.size() - 1; i >= 0; --i) printf(  %d , V[i]); n     cout << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 5007; n int a[maxn], b[maxn], n, m, g[maxn]; n int gcd(int a, int b) { n   if (a < b) return gcd(b, a); n   if (b == 0) return a; n   return gcd(b, a % b); n } n bool found(int v) { n   int l = 1, r = m, mid; n   while (l <= r) { n     mid = (l + r) / 2; n     if (b[mid] == v) n       return 1; n     else if (v < b[mid]) n       r = mid - 1; n     else n       l = mid + 1; n   } n   return 0; n } n int cal(int v) { n   int res = 0, s; n   for (int i = 2; i * i <= v; ++i) n     if (v % i == 0) { n       s = 0; n       while (v % i == 0) { n         v /= i; n         ++s; n       } n       if (found(i)) s = -s; n       res += s; n     } n   if (v > 1) { n     s = 1; n     if (found(v)) s = -s; n     res += s; n   } n   return res; n } n int main() { n   scanf( %d%d , &n, &m); n   for (int i = 1, _c = n; i <= _c; i++) scanf( %d , &a[i]); n   for (int i = 1, _c = m; i <= _c; i++) scanf( %d , &b[i]); n   g[0] = 0; n   int mul = 1; n   int res = 0; n   for (int i = 1, _c = n; i <= _c; i++) n     g[i] = gcd(g[i - 1], a[i]), res += cal(a[i]); n   for (int i = n; i >= 1; i--) { n     int gx = g[i] / mul; n     int vx = cal(gx); n     if (vx < 0) { n       res -= vx * i; n       mul *= gx; n     } n   } n   printf( %d n , res); n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, x, y; n   char sex; n   int m[367], f[367]; n   memset(m, 0, sizeof(m)); n   memset(f, 0, sizeof(f)); n   cin >> n; n   for (int i = 0; i < n; i++) { n     cin >> sex >> x >> y; n     if (sex ==  M ) { n       for (int j = x; j <= y; j++) m[j]++; n     } else { n       for (int j = x; j <= y; j++) f[j]++; n     } n   } n   int res = 0; n   for (int i = 1; i <= 366; i++) { n     res = max(res, min(f[i], m[i])); n   } n   cout << 2 * res; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int j = 0; n   string a, b; n   cin >> a; n   cin >> b; n   for (int i = a.length() - 1; i >= 0; i--) { n     if (a[i] == b[j]) { n       j++; n     } else { n       cout <<  NO ; n       return 0; n     } n   } n   cout <<  YES ; n } n


#include <bits/stdc++.h> n using namespace std; n bool isPrime(int n) { n   if (n <= 3) { n     return true; n   } n   if (n % 2 == 0 || n % 3 == 0) return false; n   for (int i = 5; i * i <= n; i = i + 6) n     if (n % i == 0 || n % (i + 2) == 0) return false; n   return true; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   int n, k; n   cin >> n >> k; n   int i = 1; n   int sum; n   while (1) { n     sum = n * i; n     if (sum % 10 == k || sum % 10 == 0) { n       break; n     } n     i++; n   } n   cout << i; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, m, r, i; n   cin >> n >> m; n   r = m % ((n * (n + 1) / 2)); n   for (i = 0; i < n; i++) { n     if (i + 1 > r) { n       cout << r; n       break; n     } else n       r -= i + 1; n   } n   return 0; n } n


#include <bits/stdc++.h> n #pragma GCC optimize( O3 ) n #pragma GCC target( sse4 ) n using namespace std; n const int dx[8] = {1, 1, 1, 0, 0, -1, -1, -1}; n const int dy[8] = {1, -1, 0, 1, -1, -1, 1, 0}; n template <class T> n T gcd(T a, T b) { n   return ((b == 0) ? a : gcd(b, a % b)); n } n int n, m; n vector<string> s; n vector<string> g; n vector<vector<int> > dist, dist1; n bool valid(int i, int j) { return (min(i, j) >= 0 && i < n && j < m); } n bool check(int t) { n   queue<pair<int, int> > q; n   for (int i = 0; i < (int)(n); i++) n     for (int j = 0; j < (int)(m); j++) { n       g[i][j] =  . ; n       dist1[i][j] = 0; n       if (dist[i][j] > t) { n         g[i][j] =  X ; n         dist1[i][j] = t + 1; n         q.push(make_pair(i, j)); n       } n     } n   while (!q.empty()) { n     pair<int, int> cur = q.front(); n     q.pop(); n     for (int i = 0; i < (int)(8); i++) { n       int x = cur.first + dx[i], y = cur.second + dy[i]; n       if (valid(x, y) && dist1[x][y] < (dist1[cur.first][cur.second] - 1)) { n         dist1[x][y] = dist1[cur.first][cur.second] - 1; n         q.push(make_pair(x, y)); n       } n     } n   } n   for (int i = 0; i < (int)(n); i++) n     for (int j = 0; j < (int)(m); j++) { n       if (s[i][j] ==  . ) { n         if (dist1[i][j] > 0) return 0; n       } else if (dist1[i][j] <= 0) n         return 0; n     } n   return 1; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cin >> n >> m; n   s.resize(n); n   for (int i = 0; i < (int)(n); i++) cin >> s[i]; n   string a =   ; n   for (int j = 0; j < (int)(m); j++) a +=  . ; n   g.resize(n); n   for (int i = 0; i < (int)(n); i++) g[i] = a; n   dist.resize(n); n   dist1.resize(n); n   vector<int> b(m, 0); n   for (int i = 0; i < (int)(n); i++) dist[i] = dist1[i] = b; n   queue<pair<int, int> > q; n   for (int i = 0; i < (int)(n); i++) n     for (int j = 0; j < (int)(m); j++) { n       if (s[i][j] ==  . ) { n         dist[i][j] = 0; n         q.push(make_pair(i, j)); n       } else n         dist[i][j] = 1e9; n     } n   while (!q.empty()) { n     pair<int, int> cur = q.front(); n     q.pop(); n     for (int i = 0; i < (int)(8); i++) { n       int x = cur.first + dx[i], y = cur.second + dy[i]; n       if (valid(x, y) && dist[x][y] == 1e9) { n         dist[x][y] = dist[cur.first][cur.second] + 1; n         q.push(make_pair(x, y)); n       } n     } n   } n   for (int i = 0; i < (int)(n); i++) n     for (int j = 0; j < (int)(m); j++) { n       dist[i][j] = min({dist[i][j], i + 1, j + 1, (n - i), (m - j)}); n     } n   int l = 0, r = min(n, m) + 1; n   while (l < r) { n     int mid = (l + r + 1) / 2; n     if (check(mid)) n       l = mid; n     else n       r = mid - 1; n   } n   cout << l << endl; n   assert(check(l)); n   for (int i = 0; i < (int)(n); i++) cout << g[i] <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long int inf = pow(10, 9); n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   long long int n; n   cin >> n; n   vector<pair<long long int, string>> v(n); n   for (long long int i = 0; i < n; ++i) cin >> v[i].first >> v[i].second; n   map<string, long long int> ma; n   for (long long int i = 0; i < n; ++i) { n     sort(v[i].second.begin(), v[i].second.end()); n     if (ma.find(v[i].second) == ma.end()) n       ma[v[i].second] = v[i].first; n     else n       ma[v[i].second] = min(ma[v[i].second], v[i].first); n   } n   vector<long long int> a; n   if (ma.find( A ) != ma.end() and ma.find( B ) != ma.end() and n       ma.find( C ) != ma.end()) n     a.push_back(ma[ A ] + ma[ B ] + ma[ C ]); n   if (ma.find( A ) != ma.end() and ma.find( BC ) != ma.end()) n     a.push_back(ma[ A ] + ma[ BC ]); n   if (ma.find( B ) != ma.end() and ma.find( AC ) != ma.end()) n     a.push_back(ma[ B ] + ma[ AC ]); n   if (ma.find( C ) != ma.end() and ma.find( AB ) != ma.end()) n     a.push_back(ma[ C ] + ma[ AB ]); n   if (ma.find( AB ) != ma.end() and ma.find( BC ) != ma.end()) n     a.push_back(ma[ AB ] + ma[ BC ]); n   if (ma.find( AB ) != ma.end() and ma.find( AC ) != ma.end()) n     a.push_back(ma[ AB ] + ma[ AC ]); n   if (ma.find( AC ) != ma.end() and ma.find( BC ) != ma.end()) n     a.push_back(ma[ AC ] + ma[ BC ]); n   if (ma.find( ABC ) != ma.end()) a.push_back(ma[ ABC ]); n   if (a.size() == 0) n     cout << -1; n   else n     cout << *min_element(a.begin(), a.end()); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n string s, t; n int dp[1005][1005]; n int solve(int i, int j) { n   if (i == s.size()) return t.size() - j; n   if (j == t.size()) return s.size() - i; n   if (dp[i][j] != -1) return dp[i][j]; n   if (s[i] == t[j]) return solve(i + 1, j + 1); n   int c1 = solve(i + 1, j + 1) + 1; n   int c2 = solve(i, j + 1) + 1; n   int c3 = solve(i + 1, j) + 1; n   return dp[i][j] = (min(c1, min(c2, c3))); n } n int shft1 = 0; n void print(int i, int j) { n   if (i == s.size()) { n     for (int xx = j; xx < t.size(); xx++) { n       cout <<  INSERT   << 1 + xx <<     << t[xx] << endl; n     } n     return; n   } n   if (j == t.size()) { n     for (int xx = 0; xx < s.size() - i; xx++) { n       cout <<  DELETE   << j + 1 << endl; n     } n     return; n   } n   int c = -2; n   if (s[i] == t[j]) { n     return print(i + 1, j + 1); n   } n   int c1 = solve(i + 1, j + 1) + 1; n   int c2 = solve(i, j + 1) + 1; n   int c3 = solve(i + 1, j) + 1; n   int optimal = solve(i, j); n   if (optimal == c1) { n     cout <<  REPLACE   << j + 1 <<     << t[j] << endl; n     print(i + 1, j + 1); n   } else if (optimal == c2) { n     cout <<  INSERT   << j + 1 <<     << t[j] << endl; n     shft1++; n     print(i, j + 1); n   } else if (optimal == c3) { n     cout <<  DELETE   << j + 1 <<     << endl; n     shft1--; n     print(i + 1, j); n   } n } n int main() { n   cin >> s >> t; n   memset(dp, -1, sizeof dp); n   cout << solve(0, 0) << endl; n   print(0, 0); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void solve() { n   int a, b, c, d, e, f; n   cin >> a >> b >> c >> d >> e >> f; n   int suit1 = min(a, d); n   int suit2 = min(b, min(c, d)); n   long long cost = 0; n   for (int i = 0; i <= suit1; i++) { n     long long temp = i * e; n     temp += min(b, min(c, d - i)) * f; n     cost = max(cost, temp); n   } n   for (int i = 0; i <= suit2; i++) { n     long long temp = i * f; n     temp += min(a, d - i) * e; n     cost = max(cost, temp); n   } n   cout << cost << endl; n } n int main() { n   int q = 1; n   while (q--) { n     solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int BITS = 20; n template <typename T> n struct xor_basis { n   T basis[BITS]; n   int n = 0; n   T min_value(T start = 0) const { n     for (int i = 0; i < n; i++) start = min(start, start ^ basis[i]); n     return start; n   } n   T max_value(T start = 0) const { n     for (int i = 0; i < n; i++) start = max(start, start ^ basis[i]); n     return start; n   } n   bool add(T x) { n     if (n == BITS) return false; n     x = min_value(x); n     if (x == 0) return false; n     basis[n++] = x; n     for (int k = n - 1; k > 0 && basis[k] > basis[k - 1]; k--) n       swap(basis[k], basis[k - 1]); n     return true; n   } n   void merge(const xor_basis<T> &other) { n     for (int i = 0; i < other.n && n < BITS; i++) add(other.basis[i]); n   } n }; n struct segment { n   xor_basis<int> basis; n   int get_max() const { return basis.max_value(); } n   void join(const segment &other) { basis.merge(other.basis); } n   void join(const segment &a, const segment &b) { n     *this = a; n     join(b); n   } n }; n struct basic_seg_tree { n   int tree_n = 0; n   vector<segment> tree; n   basic_seg_tree(int n = 0) { n     if (n > 0) init(n); n   } n   void init(int n) { n     tree_n = n; n     tree.assign(2 * tree_n, segment()); n   } n   void build(const vector<segment> &initial) { n     int n = initial.size(); n     assert(n <= tree_n); n     for (int i = 0; i < n; i++) tree[tree_n + i] = initial[i]; n     for (int position = tree_n - 1; position > 0; position--) n       tree[position].join(tree[2 * position], tree[2 * position + 1]); n   } n   segment query(int a, int b) { n     segment answer; n     for (a += tree_n, b += tree_n; a < b; a /= 2, b /= 2) { n       if (a & 1) answer.join(tree[a++]); n       if (b & 1) answer.join(tree[--b]); n     } n     return answer; n   } n }; n int N, Q; n int main() { n   scanf( %d , &N); n   vector<segment> initial; n   for (int i = 0; i < N; i++) { n     int burger; n     scanf( %d , &burger); n     initial.emplace_back(); n     initial.back().basis.add(burger); n   } n   basic_seg_tree tree(N); n   tree.build(initial); n   scanf( %d , &Q); n   for (int q = 0; q < Q; q++) { n     int L, R; n     scanf( %d %d , &L, &R); n     L--; n     printf( %d n , tree.query(L, R).get_max()); n   } n } n


#include <bits/stdc++.h> n using namespace std; n template <class T> n inline T bigmod(T p, T e, T M) { n   T ret = 1; n   for (; e > 0; e >>= 1) { n     if (e & 1) ret = (ret * p) % M; n     p = (p * p) % M; n   } n   return (T)ret; n } n template <class T> n inline T gcd(T a, T b) { n   if (b == 0) return a; n   return gcd(b, a % b); n } n template <class T> n inline T modinverse(T a, T M) { n   return bigmod(a, M - 2, M); n } n template <class T, class X> n inline bool getbit(T a, X i) { n   T t = 1; n   return ((a & (t << i)) > 0); n } n template <class T, class X> n inline T setbit(T a, X i) { n   T t = 1; n   return (a | (t << i)); n } n template <class T, class X> n inline T resetbit(T a, X i) { n   T t = 1; n   return (a & (~(t << i))); n } n int n, m; n pair<int, int> arr[100009]; n int ob[200009]; n pair<pair<int, int>, int> trans[100009]; n int cnt[100009]; n void solve() { n   memset(ob, 0, sizeof(ob)); n   sort(trans + 1, trans + m + 1); n   for (int i = (1); i <= (m); i++) { n     pair<int, int> p = trans[i].first; n     int ind = trans[i].second; n     if (ob[p.second] != 0) { n       cnt[ob[p.second]]++; n       cnt[ind]++; n     } n     ob[p.second] = ind; n   } n } n int ans[9]; n int main() { n   scanf( %d %d , &n, &m); n   for (int i = (1); i <= (m); i++) n     scanf( %d %d , &arr[i].first, &arr[i].second); n   for (int i = (1); i <= (m); i++) { n     trans[i].first = arr[i]; n     trans[i].second = i; n   } n   solve(); n   for (int i = (1); i <= (m); i++) { n     trans[i].first = make_pair(arr[i].second, arr[i].first); n     trans[i].second = i; n   } n   solve(); n   for (int i = (1); i <= (m); i++) { n     trans[i].first = make_pair(arr[i].second + arr[i].first - 1, n                                arr[i].second - arr[i].first + n); n     trans[i].second = i; n   } n   solve(); n   for (int i = (1); i <= (m); i++) { n     trans[i].first = make_pair(arr[i].second - arr[i].first + n, n                                arr[i].second + arr[i].first - 1); n     trans[i].second = i; n   } n   solve(); n   for (int i = (1); i <= (m); i++) ans[cnt[i]]++; n   for (int i = (0); i <= (8); i++) printf( %d  , ans[i]); n   printf(  n ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int knigths[300006]; n set<int> s; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(NULL), cout.tie(NULL); n   ios::sync_with_stdio(false), cin.tie(0); n   int n, m; n   cin >> n >> m; n   for (int i = 1; i <= n + 1; i++) s.insert(i); n   for (int i = 0; i < m; i++) { n     int a, b, c; n     cin >> a >> b >> c; n     set<int>::iterator itl = s.lower_bound(a); n     set<int>::iterator itu = s.upper_bound(b); n     while (*itl < *itu) { n       if (*itl != c) { n         knigths[*itl] = c; n       } n       s.erase(itl); n       itl = s.lower_bound(a); n       itu = s.upper_bound(b); n     } n     s.insert(c); n   } n   for (int i = 1; i <= n; i++) cout << knigths[i] <<    ; n   cout << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 4e5 + 5; n const string s[] = { NW ,  NE ,  SE ,  SW }; n inline int read() { n   int x = 0; n   char ch = getchar(); n   while (!isdigit(ch)) ch = getchar(); n   while (isdigit(ch)) { n     x = x * 10 + ch -  0 ; n     ch = getchar(); n   } n   return x; n } n struct Node { n   int x, y, k; n   inline bool operator==(const Node B) const { n     return this->x == B.x && this->y == B.y && this->k == B.k; n   } n   inline bool operator!=(const Node B) const { return !(*this == B); } n }; n int n, m, k; n long long ans; n set<int> s1[MAXN], s2[MAXN]; n map<pair<int, int>, int> mp; n inline void add(int x, int y) { n   s1[(1 == 1 ? x - y + m + 1 : x + y)].insert(x); n   s2[(2 == 1 ? x - y + m + 1 : x + y)].insert(x); n   mp[make_pair(x, y)] = 1; n } n inline void prepare() { n   for (int i = 0; i <= n + 1; i++) add(i, 0), add(i, m + 1); n   for (int i = 0; i <= m + 1; i++) add(0, i), add(n + 1, i); n } n inline Node change(Node x, int opt = 1) { n   Node now = x; n   int cur = 0; n   if (x.k == 1) { n     cur = *(--s1[(1 == 1 ? x.x - x.y + m + 1 : x.x + x.y)].lower_bound(x.x)); n     now.x -= (abs(cur - x.x) - 1), now.y -= (abs(cur - x.x) - 1); n     if ((mp.find(make_pair(now.x - 1, now.y)) != mp.end()) && n         (mp.find(make_pair(now.x, now.y - 1)) != mp.end())) n       now.k = 3; n     else if ((mp.find(make_pair(now.x - 1, now.y)) != mp.end())) n       now.y--, now.k = 4; n     else if ((mp.find(make_pair(now.x, now.y - 1)) != mp.end())) n       now.x--, now.k = 2; n     else n       now.k = 3; n   } n   if (x.k == 2) { n     cur = *(--s2[(2 == 1 ? x.x - x.y + m + 1 : x.x + x.y)].lower_bound(x.x)); n     now.x -= (abs(cur - x.x) - 1), now.y += (abs(cur - x.x) - 1); n     if ((mp.find(make_pair(now.x - 1, now.y)) != mp.end()) && n         (mp.find(make_pair(now.x, now.y + 1)) != mp.end())) n       now.k = 4; n     else if ((mp.find(make_pair(now.x - 1, now.y)) != mp.end())) n       now.y++, now.k = 3; n     else if ((mp.find(make_pair(now.x, now.y + 1)) != mp.end())) n       now.x--, now.k = 1; n     else n       now.k = 4; n   } n   if (x.k == 3) { n     cur = *(s1[(1 == 1 ? x.x - x.y + m + 1 : x.x + x.y)].lower_bound(x.x)); n     now.x += (abs(cur - x.x) - 1), now.y += (abs(cur - x.x) - 1); n     if ((mp.find(make_pair(now.x + 1, now.y)) != mp.end()) && n         (mp.find(make_pair(now.x, now.y + 1)) != mp.end())) n       now.k = 1; n     else if ((mp.find(make_pair(now.x + 1, now.y)) != mp.end())) n       now.y++, now.k = 2; n     else if ((mp.find(make_pair(now.x, now.y + 1)) != mp.end())) n       now.x++, now.k = 4; n     else n       now.k = 1; n   } n   if (x.k == 4) { n     cur = *(s2[(2 == 1 ? x.x - x.y + m + 1 : x.x + x.y)].lower_bound(x.x)); n     now.x += (abs(cur - x.x) - 1), now.y -= (abs(cur - x.x) - 1); n     if ((mp.find(make_pair(now.x + 1, now.y)) != mp.end()) && n         (mp.find(make_pair(now.x, now.y - 1)) != mp.end())) n       now.k = 2; n     else if ((mp.find(make_pair(now.x + 1, now.y)) != mp.end())) n       now.y--, now.k = 1; n     else if ((mp.find(make_pair(now.x, now.y - 1)) != mp.end())) n       now.x++, now.k = 3; n     else n       now.k = 2; n   } n   opt && (ans += abs(cur - x.x)); n   return now; n } n inline bool check_it(Node x) { n   Node tmp = x; n   Node y = change(x); n   x = y; n   if (((tmp.k + 1) % 4 == y.k - 1)) return false; n   while (tmp != x) { n     y = change(y); n     if (((x.k + 1) % 4 == y.k - 1)) return false; n     x = y; n   } n   return 1; n } n int main() { n   n = read(), m = read(); n   k = read(), prepare(); n   for (int i = 1; i <= k; i++) { n     int x = read(); n     int y = read(); n     add(x, y); n   } n   int sx = read(), sy = read(), sd = -1; n   string str; n   cin >> str; n   for (int i = 0; i < 4; i++) n     if (str == s[i]) sd = i + 1; n   assert(~sd); n   Node st = Node{sx, sy, sd}; n   st = change(st, 0); n   if (!check_it(st)) { n     ans--; n     st.k = (st.k + 1) % 4 + 1; n     check_it(st); n   } n   printf( %lld n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long t; n   cin >> t; n   while (t--) { n     long long n; n     cin >> n; n     vector<int> v(n); n     for (int i = 0; i < (int)n; ++i) cin >> v[i]; n     sort((v).begin(), (v).end()); n     for (int i = (int)n - 1; i >= 0; --i) cout << v[i] <<    ; n     cout <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n string s; n map<string, vector<int> > m; n map<string, int> memoria; n string codigo(string s1, string s2) { return s1 +  #  + s2; } n int calculadicot(int la, vector<int> &va, int lb, vector<int> &vb) { n   int sol = 1000000000; n   for (int ia = 0; ia < int(va.size()); ia++) { n     int inf = -1; n     int sup = int(vb.size()); n     while (inf + 1 < sup) { n       int med = (inf + sup) / 2; n       if (vb[med] <= va[ia]) n         inf = med; n       else n         sup = med; n     } n     if (inf >= 0) sol = min(sol, max(va[ia] + la, vb[inf] + lb) - vb[inf]); n     if (sup < int(vb.size())) n       sol = min(sol, max(va[ia] + la, vb[sup] + lb) - va[ia]); n   } n   return sol; n } n int calculafusion(int la, vector<int> &va, int lb, vector<int> &vb) { n   int ia = 0; n   int ib = 0; n   int sol = 1000000000; n   while (ia < int(va.size()) and ib < int(vb.size())) { n     if (va[ia] < vb[ib]) { n       sol = min(sol, max(va[ia] + la, vb[ib] + lb) - va[ia]); n       ia++; n     } else { n       sol = min(sol, max(va[ia] + la, vb[ib] + lb) - vb[ib]); n       ib++; n     } n   } n   return sol; n } n int calcula(int la, vector<int> &va, int lb, vector<int> &vb) { n   if (int(va.size()) > int(vb.size())) return calcula(lb, vb, la, va); n   if (int(va.size()) == 0) return -1; n   if (int(va.size()) * 10 > int(va.size()) + int(vb.size())) n     return calculafusion(la, va, lb, vb); n   return calculadicot(la, va, lb, vb); n } n int main() { n   cin >> s; n   for (int i = 0; i < int(s.size()); i++) n     for (int l = 1; l <= 4; l++) m[s.substr(i, l)].push_back(i); n   int q; n   cin >> q; n   for (int cas = 0; cas < q; cas++) { n     string a, b; n     cin >> a >> b; n     if (a > b) swap(a, b); n     string c = codigo(a, b); n     if (memoria.count(c)) n       cout << memoria[c] << endl; n     else { n       vector<int> &va = m[a]; n       vector<int> &vb = m[b]; n       memoria[c] = calcula(int(a.size()), va, int(b.size()), vb); n       cout << memoria[c] << endl; n     } n   } n } n


#include <queue> n #include <cstdio> n #include <cstring> n using namespace std; n const int Maxn=200000; n const int Inf=0x3f3f3f3f; n int n,k; n int head[Maxn+5],arrive[Maxn<<1|5],nxt[Maxn<<1|5],tot; n void add_edge(int from,int to){ n  arrive[++tot]=to; n  nxt[tot]=head[from]; n  head[from]=tot; n } n queue<int> q; n int a[Maxn+5]; n int dis[Maxn+5]; n void init_bfs(){ n  memset(dis,0x3f,sizeof dis); n  for(int i=1;i<=k;i++){ n   dis[a[i]]=0; n   q.push(a[i]); n  } n  while(!q.empty()){ n   int u=q.front(); n   q.pop(); n   for(int i=head[u];i;i=nxt[i]){ n    int v=arrive[i]; n    if(dis[v]!=Inf){ n     continue; n    } n    dis[v]=dis[u]+1; n    q.push(v); n   } n  } n } n int ans[Maxn+5]; n bool del[Maxn+5]; n int sz[Maxn+5]; n int st[Maxn+5],top; n int dep[Maxn+5]; n void size_dfs(int u,int fa){ n  sz[u]=1; n  for(int i=head[u];i;i=nxt[i]){ n   int v=arrive[i]; n   if(v==fa||del[v]){ n    continue; n   } n   size_dfs(v,u); n   sz[u]+=sz[v]; n  } n } n int get_weight(int x){ n  size_dfs(x,0); n  int num=sz[x]; n  top=0; n  st[++top]=x; n  int maxn; n  int root=x,root_mx=sz[x]-1; n  while(top>0){ n   int u=st[top--]; n   maxn=num-sz[u]; n   for(int i=head[u];i;i=nxt[i]){ n    int v=arrive[i]; n    if(del[v]||sz[v]>sz[u]){ n     continue; n    } n    st[++top]=v; n    maxn=max(maxn,sz[v]); n   } n   if(maxn<root_mx){ n    root=u; n    root_mx=maxn; n   } n  } n  return root; n } n void dep_sz_dfs(int u,int fa){ n  dep[u]=dep[fa]+1; n  sz[u]=1; n  for(int i=head[u];i;i=nxt[i]){ n   int v=arrive[i]; n   if(del[v]||v==fa){ n    continue; n   } n   dep_sz_dfs(v,u); n   sz[u]+=sz[v]; n  } n } n int lis[Maxn+5],len; n int tot_sz; n void lis_dfs(int u,int fa){ n  if(dis[u]-1-dep[u]>=0){ n   lis[min(dis[u]-1-dep[u],tot_sz)]=max(lis[min(dis[u]-1-dep[u],tot_sz)],dis[u]); n  } n  for(int i=head[u];i;i=nxt[i]){ n   int v=arrive[i]; n   if(del[v]||v==fa){ n    continue; n   } n   lis_dfs(v,u); n  } n } n void calc_dfs(int u,int fa){ n  ans[u]=max(ans[u],lis[dep[u]]); n  for(int i=head[u];i;i=nxt[i]){ n   int v=arrive[i]; n   if(del[v]||v==fa){ n    continue; n   } n   calc_dfs(v,u); n  } n } n void calc(int u){ n  u=get_weight(u); n  dep_sz_dfs(u,0); n  tot_sz=sz[u]; n  for(int i=0;i<=sz[u];i++){ n   lis[i]=-Inf; n  } n  lis_dfs(u,0); n  for(int i=sz[u]-1;i>=0;i--){ n   lis[i]=max(lis[i],lis[i+1]); n  } n  calc_dfs(u,0); n  del[u]=1; n  for(int i=head[u];i;i=nxt[i]){ n   int v=arrive[i]; n   if(del[v]){ n    continue; n   } n   calc(v); n  } n } n int main(){ n  scanf( %d ,&n); n  for(int i=1;i<n;i++){ n   int u,v; n   scanf( %d%d ,&u,&v); n   add_edge(u,v); n   add_edge(v,u); n  } n  scanf( %d ,&k); n  for(int i=1;i<=k;i++){ n   scanf( %d ,&a[i]); n  } n  init_bfs(); n  dep[0]=-1; n  calc(1); n  for(int i=1;i<=n;i++){ n   printf( %d  ,ans[i]); n  } n  puts(  ); n  return 0; n } n  n


#include <bits/stdc++.h> n using namespace std; n const int N = 1000000 + 5; n int vis[N], n, m; n pair<int, int> Get(int x) { n   int a, b; n   if (x >= n) n     a = x - n + 1; n   else n     a = n - x + 1; n   x = m - x + 1; n   if (x >= n) n     b = m - (x - n + 1) + 1; n   else n     b = m - (n - x + 1) + 1; n   return pair<int, int>(a, b); n } n void dfs(int x) { n   pair<int, int> cur = Get(x); n   pair<int, int> nxt = Get(cur.first); n   if (!vis[nxt.first]) { n     vis[nxt.first] = 1; n     dfs(nxt.first); n   } n   if (!vis[nxt.second]) { n     vis[nxt.second] = 1; n     dfs(nxt.second); n   } n   nxt = Get(cur.second); n   if (!vis[nxt.first]) { n     vis[nxt.first] = 1; n     dfs(nxt.first); n   } n   if (!vis[nxt.second]) { n     vis[nxt.second] = 1; n     dfs(nxt.second); n   } n } n int main() { n   scanf( %d%d , &n, &m); n   if (n > m) swap(n, m); n   int ans = 0; n   for (int i = 1; i <= m; i++) n     if (!vis[i]) { n       ans++; n       vis[i] = 1; n       dfs(i); n     } n   printf( %d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int gcd(int a, int b) { n   if (a == 0) return b; n   if (b == 0) return a; n   if (a == b) return a; n   if (a > b) return gcd(a - b, b); n   return gcd(a, b - a); n } n void sol() { n   long long int L, v, l, r, count = 0; n   cin >> L >> v >> l >> r; n   long long int p = L / v; n   long long int q = (r / v) - ((l - 1) / v); n   cout << p - q << endl; n } n int main() { n   int t; n   cin >> t; n   while (t--) sol(); n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   long long x, y, z; n   scanf( %d , &n); n   for (int i = 1; i <= n; i++) { n     long long ans = 0; n     scanf( %lld%lld , &x, &y); n     if (x >= y) { n       ans += y; n       printf( %lld n , ans); n       continue; n     } n     long long d = (y - 1) / x + 1; n     z = x * d - y; n     ans += z * (d - 1) * (d - 1) + (x - z) * d * d; n     printf( %lld n , ans); n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 200005, P = 1000000007; n int n, l[N], r[N], p[N], p2[N], ans; n int power(int a, int x) { n   int ans = 1; n   for (; x; x >>= 1, a = 1LL * a * a % P) n     if (x & 1) ans = 1LL * ans * a % P; n   return ans; n } n int main() { n   scanf( %d , &n); n   for (int i = 1; i <= n; ++i) scanf( %d , l + i); n   for (int i = 1; i <= n; ++i) n     scanf( %d , r + i), p[i] = power(r[i] - l[i] + 1, P - 2); n   for (int i = 1; i + 2 <= n; ++i) { n     int x = max({l[i], l[i + 1], l[i + 2]}), n         y = min({r[i], r[i + 1], r[i + 2]}); n     if (x <= y) n       ans = (ans + 1LL * (y - x + 1) * p[i] % P * p[i + 1] % P * p[i + 2]) % P; n   } n   int sum = 0; n   for (int i = 1; i + 1 <= n; ++i) { n     int x = max({l[i], l[i + 1]}), y = min({r[i], r[i + 1]}); n     if (x <= y) n       sum = (sum + (p2[i] = 1LL * (y - x + 1) * p[i] % P * p[i + 1] % P)) % P; n   } n   ans = 2LL * ans % P; n   for (int i = 1; i + 1 <= n; ++i) n     ans = (ans + 1LL * p2[i] * n                      (0LL + sum - p2[i] - (i == 1 ? 0 : p2[i - 1]) - n                       (i == n - 1 ? 0 : p2[i + 1]))) % n           P; n   ans = (ans + sum) % P; n   int res = (1LL * n * n - 2LL * n * sum + ans) % P; n   printf( %d n , (res + P) % P); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   string s; n   cin >> s; n   int count = 0, check = 0; n   for (int i = 0; i < s.size(); i++) { n     if (s[i] ==  1 ) { n       check = 1; n     } n     if (check == 1 && s[i] ==  0 ) count++; n   } n   if (count >= 6) n     cout <<  yes ; n   else { n     cout <<  no ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int value[100005]; n int main() { n   int n, k, m; n   while (cin >> n >> k >> m) { n     map<string, int> mymap; n     for (int i = 1; i <= n; i++) { n       string s; n       cin >> s; n       mymap[s] = i; n     } n     for (int i = 1; i <= n; i++) scanf( %d , &value[i]); n     for (int i = 1; i <= k; i++) { n       int cnt, minv = 1000000000; n       int t[100005]; n       scanf( %d , &cnt); n       for (int j = 0; j < cnt; j++) { n         scanf( %d , &t[j]); n         minv = min(minv, value[t[j]]); n       } n       for (int j = 0; j < cnt; j++) value[t[j]] = minv; n     } n     long long ans = 0; n     for (int i = 0; i < m; i++) { n       string s; n       cin >> s; n       ans += value[mymap[s]]; n     } n     cout << ans << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   char a[250]; n   int k; n   cin >> a; n   cin >> k; n   int temp = k; n   int curr = 0; n   a[strlen(a)] =   0 ; n   if (strlen(a) > 1) { n     while (temp) { n       int max_v = a[curr] - 48, max_p = curr; n       for (int j = curr + 1; j <= curr + temp && j < strlen(a); j++) { n         if (a[j] - 48 > max_v) { n           max_v = a[j] - 48; n           max_p = j; n         } n       } n       if (max_p != curr) { n         for (int j = max_p; j > curr; j--) { n           if (temp) { n             swap(a[j], a[j - 1]); n             temp--; n           } else n             break; n         } n       } n       curr++; n       if (curr >= strlen(a)) { n         break; n       } n       a[strlen(a)] =   0 ; n     } n   } n   cout << a; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 105; n int n, m; n char g[maxn][maxn]; n int vis[3]; n map<char, int> mp; n int main() { n   mp[ R ] = 0; n   mp[ G ] = 1; n   mp[ B ] = 2; n   scanf( %d%d , &n, &m); n   for (int i = 0; i < n; i++) scanf( %s , g[i]); n   bool ok = true; n   if (g[0][0] == g[0][m - 1] && n % 3 == 0) { n     int w = n / 3; n     for (int i = 0; i < n && ok; i++) { n       char x = g[i][0]; n       int but = i + w; n       for (; i < but; i++) n         for (int j = 0; j < m; j++) n           if (g[i][j] != x) ok = false; n       int id = mp[x]; n       if (vis[id]) { n         ok = false; n         break; n       } n       vis[id] = 1; n       i--; n     } n   } else if (g[0][0] == g[n - 1][0] && m % 3 == 0) { n     int w = m / 3; n     for (int j = 0; j < m && ok; j++) { n       char x = g[0][j]; n       int r = j + w; n       for (; j < r; j++) n         for (int i = 0; i < n; i++) n           if (g[i][j] != x) ok = false; n       int id = mp[x]; n       if (vis[id]) { n         ok = false; n         break; n       } n       vis[id] = 1; n       j--; n     } n   } else n     ok = false; n   puts(ok ?  YES  :  NO ); n   return 0; n } n


#include <bits/stdc++.h> n #pragma comment(linker,  /STACK:256000000 ) n using namespace std; n const long long inf = 1e18; n const double eps = 1e-9; n const double pi = 4 * atan(1.0); n const int N = int(2e5) + 100; n const int ITERS = 80; n int m; n pair<int, int> a[N]; n vector<pair<int, int> > group[N]; n long long sum_t[N]; n int p[N]; n double fen_suff[N]; n inline long long val(const pair<int, int>& a, const pair<int, int>& b) { n   return 1LL * a.first * b.second - 1LL * b.first * a.second; n } n inline bool cmp(const pair<int, int>& a, const pair<int, int>& b) { n   return val(a, b) > 0; n } n inline void add_suff(int cp, double val) { n   int pos = lower_bound(p, p + m, cp) - p; n   assert(pos < m && p[pos] == cp); n   while (pos >= 0) { n     if (val < fen_suff[pos]) { n       fen_suff[pos] = val; n     } else { n       break; n     } n     pos = (pos & (pos + 1)) - 1; n   } n } n inline double get_suff(int cp) { n   int pos = lower_bound(p, p + m, cp) - p; n   assert(pos < m && p[pos] == cp); n   double res = 1e100; n   ++pos; n   while (pos < m) { n     res = min(res, fen_suff[pos]); n     pos = (pos | (pos + 1)); n   } n   return res; n } n int main() { n   int n; n   cin >> n; n   for (int i = 0; i < n; ++i) { n     scanf( %d , &a[i].first); n   } n   for (int i = 0; i < n; ++i) { n     scanf( %d , &a[i].second); n   } n   sort(a, a + n, cmp); n   for (int i = 0; i < n; ++i) { n     p[i] = a[i].first; n   } n   sort(p, p + n); n   m = unique(p, p + n) - p; n   int i = 0, sz = 0; n   while (i < n) { n     int j = i; n     while (j + 1 < n && val(a[i], a[j + 1]) == 0) { n       ++j; n     } n     for (int z = i; z <= j; ++z) { n       group[sz].push_back(a[z]); n     } n     sort((group[sz]).begin(), (group[sz]).end()); n     ++sz; n     i = j + 1; n   } n   for (int i = 0; i < sz; ++i) { n     if (i > 0) { n       sum_t[i] += sum_t[i - 1]; n     } n     for (pair<int, int> p : group[i]) { n       sum_t[i] += p.second; n     } n   } n   double l = 0, r = 1; n   for (int it = 0; it < ITERS; ++it) { n     double mid = (l + r) / 2; n     for (int i = 0; i < m; ++i) { n       fen_suff[i] = 1e100; n     } n     bool fl = false; n     for (int i = sz - 1; i >= 0 && !fl; --i) { n       int ptr = ((int)(group[i]).size()) - 1; n       for (int j = ((int)(group[i]).size()) - 1; j >= 0; --j) { n         while (group[i][ptr].first > group[i][j].first) { n           add_suff( n               group[i][ptr].first, n               group[i][ptr].first * (1.0 - mid * sum_t[i] / sum_t[sz - 1])); n           --ptr; n         } n         if (group[i][j].first * n                 (1.0 - mid * ((i > 0 ? sum_t[i - 1] : 0) + group[i][j].second) / n                            sum_t[sz - 1]) > n             get_suff(group[i][j].first)) { n           fl = true; n           break; n         } n       } n       while (ptr >= 0) { n         add_suff(group[i][ptr].first, n                  group[i][ptr].first * (1.0 - mid * sum_t[i] / sum_t[sz - 1])); n         --ptr; n       } n     } n     if (fl) { n       r = mid; n       continue; n     } n     for (int i = 0; i < m; ++i) { n       fen_suff[i] = 1e100; n     } n     for (int i = 0; i < sz && !fl; ++i) { n       int ptr = ((int)(group[i]).size()) - 1; n       for (int j = ((int)(group[i]).size()) - 1; j >= 0; --j) { n         while (group[i][ptr].first > group[i][j].first) { n           add_suff( n               group[i][ptr].first, n               group[i][ptr].first * (1.0 - mid * sum_t[i] / sum_t[sz - 1])); n           --ptr; n         } n         if (group[i][j].first * n                 (1.0 - mid * ((i > 0 ? sum_t[i - 1] : 0) + group[i][j].second) / n                            sum_t[sz - 1]) > n             get_suff(group[i][j].first)) { n           fl = true; n           break; n         } n       } n       while (ptr >= 0) { n         add_suff(group[i][ptr].first, n                  group[i][ptr].first * (1.0 - mid * sum_t[i] / sum_t[sz - 1])); n         --ptr; n       } n     } n     if (fl) { n       r = mid; n     } else { n       l = mid; n     } n   } n   printf( %.18lf n , (l + r) / 2); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int INF = (int)1e9; n int main() { n   int n; n   cin >> n; n   long long int a; n   cin >> a; n   while (1) { n     if (a % 2 == 0) n       a /= 2; n     else if (a % 3 == 0) n       a /= 3; n     else n       break; n   } n   bool f = 1; n   for (int i = 0; i < n; i++) { n     long long int b; n     cin >> b; n     while (1) { n       if (b % 2 == 0) n         b /= 2; n       else if (b % 3 == 0) n         b /= 3; n       else n         break; n     } n     if (a != b) f = 0; n     a = b; n   } n   if (f) n     cout <<  YES  << endl; n   else n     cout <<  NO  << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   int n; n   cin >> n; n   int nums[n]; n   for (int i = 0; i < n; i++) { n     cin >> nums[i]; n   } n   int ans = 0; n   int maxi = 0; n   int mini = 1e9; n   for (int i = 0; i < n; i++) { n     maxi = max(maxi, nums[i]); n     mini = min(mini, nums[i]); n   } n   int f = -1, l = -1; n   for (int i = 0; i < n; i++) { n     if (nums[i] == maxi) { n       f = i; n       break; n     } n   } n   for (int i = n - 1; i >= 0; i--) { n     if (nums[i] == mini) { n       l = i; n       break; n     } n   } n   ans += f + n - 1 - l; n   if (l < f) ans--; n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int gcd(int a, int b) { n   if (b == 0) return a; n   return gcd(b, a % b); n } n int main() { n   int t; n   cin >> t; n   while (t--) { n     int n; n     cin >> n; n     int ar[n]; n     for (int i = 0; i < n; i++) { n       cin >> ar[i]; n       --ar[i]; n     } n     for (int i = 0; i < n; i++) { n       int pos = i, j = i, cnt = 1, kaku = ar[i]; n       while (kaku != pos) { n         j = ar[j]; n         kaku = ar[j]; n         cnt++; n       } n       cout << cnt <<    ; n     } n     cout << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void solve() { n   string Fillipino =  po ; n   string Japanese1 =  desu ; n   string Japanese2 =  masu ; n   string Korean =  mnida ; n   string s; n   cin >> s; n   long long n = s.size(); n   if (s.substr(n - 2, 2) == Fillipino) { n     cout <<  FILIPINO  << endl; n     return; n   } n   if (s.substr(n - 4, 4) == Japanese1) { n     cout <<  JAPANESE  << endl; n     return; n   } n   if (s.substr(n - 4, 4) == Japanese2) { n     cout <<  JAPANESE  << endl; n     return; n   } n   if (s.substr(n - 5, 5) == Korean) { n     cout <<  KOREAN  << endl; n     return; n   } n } n int32_t main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   long long t; n   cin >> t; n   while (t--) solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<int> st; n int main() { n   ios::sync_with_stdio(0); n   int n, x; n   cin >> n; n   for (int i = 0; i < n; i++) { n     cin >> x; n     if (st.empty()) n       st.push_back(x); n     else { n       st.push_back(x); n       while (st.size() > 1) { n         if (st[int(st.size()) - 1] != st[int(st.size()) - 2]) break; n         st.pop_back(); n         st[int(st.size()) - 1]++; n       } n     } n   } n   cout << int(st.size()) <<   n ; n   for (auto v : st) cout << v <<    ; n   return 0; n } n