#include <bits/stdc++.h> n using namespace std; n class SegmentTree { n  public: n   SegmentTree(int _n) : n(_n), N(_n << 1), data(N, INT_MAX) { n     h = 0; n     while (1 << h <= N) { n       ++h; n     } n   } n   void build(vector<int>& nums) { n     copy(nums.begin(), nums.end(), data.begin() + n); n     for (int i = n - 1; i > 0; --i) { n       data[i] = min(data[i << 1], data[i << 1 | 1]); n     } n   } n   int get(int l, int r) { n     l += n; n     r += n; n     int res = INT_MAX; n     while (l <= r) { n       if (l & 0x1) { n         res = min(res, data[l++]); n       } n       if (!(r & 0x1)) { n         res = min(res, data[r--]); n       } n       l >>= 1; n       r >>= 1; n     } n     return res; n   } n  n  private: n   int N; n   int n; n   int h; n   vector<int> data; n }; n class SuffixArray { n  public: n   SuffixArray(const string& s) : N(s.length()), st(N + 100) { build(s); } n   const vector<int>& getSuffixArray() { return sa; } n   const vector<int>& getIndexArray() { return indices; } n   const vector<int>& getLcpArray() { return lcp; } n   long long lcpOf(int l, int r) { n     if (l == r) { n       return N - l; n     } n     l = indices[l]; n     r = indices[r]; n     if (l > r) { n       swap(l, r); n     } n     return st.get(l + 1, r); n   } n  n  private: n   int N; n   vector<int> sa; n   vector<int> indices; n   vector<int> lcp; n   SegmentTree st; n   struct Datum { n    public: n     Datum(int i = -1, int k0 = -1, int k1 = -1) n         : index(i), key0(k0), key1(k1) {} n     bool operator<(const Datum& other) const { n       return key0 == other.key0 ? key1 < other.key1 : key0 < other.key0; n     } n     int index; n     int key0; n     int key1; n   }; n   void build(const string& s) { n     N = s.length(); n     sa.clear(); n     sa.resize(N, -1); n     indices.clear(); n     indices.resize(N, -1); n     vector<Datum> data(N); n     for (int i = 0; i < N; ++i) { n       data[i].index = i; n       data[i].key0 = s[i]; n     } n     auto update = [&](int gap) { n       int idx = -1, preKey0 = -1, preKey1 = -1; n       for (int i = 0; i < N; ++i) { n         int key0 = data[i].key0, key1 = data[i].key1; n         if (key0 != preKey0 || key1 != preKey1) { n           data[i].key0 = ++idx; n           preKey0 = key0; n           preKey1 = key1; n         } else { n           data[i].key0 = idx; n         } n         indices[data[i].index] = i; n       } n       for (int i = 0; i < N; ++i) { n         int j = data[i].index + gap; n         if (j < N) { n           data[i].key1 = data[indices[j]].key0; n         } else { n           data[i].key1 = -1; n         } n       } n     }; n     sort(data.begin(), data.end()); n     int gap = 1; n     update(gap); n     while (gap <= N) { n       sort(data.begin(), data.end()); n       update(gap <<= 1); n     } n     for (int i = 0; i < N; ++i) { n       sa[i] = data[i].index; n       indices[sa[i]] = i; n     } n     buildLcp(s); n   } n   void buildLcp(const string& s) { n     lcp.clear(); n     lcp.resize(N, 0); n     int len = 0; n     for (int i = 0; i < N; ++i) { n       int idx = indices[i]; n       if (idx == 0) { n         lcp[idx] = 0; n         len = 0; n         continue; n       } n       int j = sa[idx - 1]; n       while (i + len < N && j + len < N && s[i + len] == s[j + len]) { n         ++len; n       } n       lcp[idx] = len; n       if (len > 0) { n         --len; n       } n     } n     st.build(lcp); n   } n }; n class Solution { n  public: n   Solution(const string& s) n       : N(s.length()), n         sfxArr(s), n         lcp(N + 1000, make_pair(-1, -1)), n         cnts(N + 1000, 0), n         sums(N + 1000, 0LL), n         S(s) {} n   long long query(vector<int>& A, int n, vector<int>& B, int m) { n     const auto& indices = sfxArr.getIndexArray(); n     auto cmp = [&](int l, int r) { return indices[l] < indices[r]; }; n     sort(A.begin(), A.begin() + n, cmp); n     sort(B.begin(), B.begin() + m, cmp); n     if (n > m) { n       swap(A, B); n       swap(n, m); n     } n     long long res = 0; n     int size = 0; n     long long sum = 0; n     for (int i = 0, j = 0; i < n || j < m;) { n       if (j == m) { n         break; n       } n       if (i < n && indices[A[i]] <= indices[B[j]]) { n         int len = 0; n         int cnt = 0; n         if (i == 0) { n           len = N - A[i]; n           cnt = 0; n         } else { n           len = sfxArr.lcpOf(A[i - 1], A[i]); n           cnt = 0; n           while (size > 0) { n             if (lcp[size - 1].first >= len) { n               sum -= 1LL * lcp[size - 1].first * lcp[size - 1].second; n               cnt += lcp[size - 1].second; n               --size; n             } else { n               break; n             } n           } n         } n         if (cnt > 0) { n           lcp[size].first = len; n           lcp[size].second = cnt; n           sum += 1LL * len * cnt; n           cnts[size + 1] = cnts[size] + cnt; n           sums[size + 1] = sum; n           ++size; n         } n         lcp[size].first = N - A[i]; n         lcp[size].second = 1; n         sum += N - A[i]; n         cnts[size + 1] = cnts[size] + 1; n         sums[size + 1] = sum; n         assert(cnts[size + 1] == i + 1); n         ++size; n         ++i; n       } else { n         if (i - 1 >= 0) { n           int len = sfxArr.lcpOf(A[i - 1], B[j]); n           auto it = n               lower_bound(lcp.begin(), lcp.begin() + size, make_pair(len, 0)); n           int idx = it - lcp.begin() - 1; n           if (idx < 0) { n             res += 1LL * len * i; n           } else { n             res += sums[idx + 1] + 1LL * len * (i - cnts[idx + 1]); n           } n         } n         ++j; n       } n     } n     size = 0; n     sum = 0; n     for (int i = n - 1, j = m - 1; i >= 0 || j >= 0;) { n       if (j < 0) { n         break; n       } n       if (i >= 0 && indices[A[i]] > indices[B[j]]) { n         int len = 0; n         int cnt = 0; n         if (i == n - 1) { n           len = N - A[i]; n           cnt = 0; n         } else { n           len = sfxArr.lcpOf(A[i + 1], A[i]); n           cnt = 0; n           while (size > 0) { n             if (lcp[size - 1].first >= len) { n               sum -= 1LL * lcp[size - 1].first * lcp[size - 1].second; n               cnt += lcp[size - 1].second; n               --size; n             } else { n               break; n             } n           } n         } n         if (cnt > 0) { n           lcp[size].first = len; n           lcp[size].second = cnt; n           sum += 1LL * len * cnt; n           cnts[size + 1] = cnts[size] + cnt; n           sums[size + 1] = sum; n           ++size; n         } n         lcp[size].first = N - A[i]; n         lcp[size].second = 1; n         sum += N - A[i]; n         cnts[size + 1] = cnts[size] + 1; n         sums[size + 1] = sum; n         assert(cnts[size + 1] == n - i); n         ++size; n         --i; n       } else { n         if (i + 1 < n) { n           int len = sfxArr.lcpOf(A[i + 1], B[j]); n           auto it = n               lower_bound(lcp.begin(), lcp.begin() + size, make_pair(len, 0)); n           int idx = it - lcp.begin() - 1; n           if (idx < 0) { n             res += 1LL * len * (n - i - 1); n           } else { n             res += sums[idx + 1] + 1LL * len * (n - i - 1 - cnts[idx + 1]); n           } n         } n         --j; n       } n     } n     return res; n   } n  n  private: n   int N; n   string S; n   SuffixArray sfxArr; n   vector<pair<int, int>> lcp; n   vector<int> cnts; n   vector<long long> sums; n   void check(vector<int>& A, int n, vector<int>& B, int m, long long res) { n     long long ans = 0; n     int N = S.length(); n     auto LCP = [&](int x, int y) { n       int res = 0; n       for (int i = x, j = y; i < N && j < N; ++i, ++j) { n         if (S[i] == S[j]) { n           ++res; n         } else { n           break; n         } n       } n       return res; n     }; n     for (int i = 0; i < n; ++i) { n       for (int j = 0; j < m; ++j) { n         int a = A[i], b = B[j]; n         ans += LCP(a, b); n       } n     } n     if (ans != res) { n       cout <<  ERROR: WA   n            <<   got   << res <<   but expected   << ans << endl; n     } n   } n }; n void test() { n   vector<string> ts = {{ adsfadfqiewruypzdjfquwieroyzsdf , n                          zduoifyqewrjhlzjkdhfvuioqerwy , n                          adsfuiaednbklzjdrfyquioerwyhadnfb ,  aaaaaaaaaaaaaaa , n                          axaaxadaxaacaaxxaad }}; n   cout <<  test   << endl; n   for (auto s : ts) { n     Solution sol(s); n     int n = s.length(); n     vector<int> A(n); n     iota(A.begin(), A.end(), 0); n     vector<int> B(n); n     iota(B.begin(), B.end(), 0); n     sol.query(A, n, B, n); n   } n } n int main(int argc, char** argv) { n   ios::sync_with_stdio(false); n   cin.tie(0); n   int n, q; n   cin >> n >> q; n   string s; n   cin >> s; n   Solution sol(s); n   vector<int> A(n + 100); n   vector<int> B(n + 100); n   int k, l; n   while (q-- > 0) { n     cin >> k >> l; n     for (int i = 0; i < k; ++i) { n       cin >> A[i]; n       --A[i]; n     } n     for (int j = 0; j < l; ++j) { n       cin >> B[j]; n       --B[j]; n     } n     cout << sol.query(A, k, B, l) << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long n, l, x, y; n long long a[100100]; n int main() { n   cin >> n >> l >> x >> y; n   for (int i = 0; i < n; i++) cin >> a[i]; n   bool fx = false, fy = false; n   for (int i = 0; i < n; i++) { n     if (binary_search(a, a + n, a[i] + x)) { n       fx = true; n       break; n     } n   } n   for (int i = 0; i < n; i++) { n     if (binary_search(a, a + n, a[i] + y)) { n       fy = true; n       break; n     } n   } n   if (fx && fy) { n     printf( 0 n ); n     return 0; n   } n   if (fx && !fy) { n     printf( 1 n ); n     cout << y << endl; n     return 0; n   } n   if (!fx && fy) { n     printf( 1 n ); n     cout << x << endl; n     return 0; n   } n   for (int i = 0; i < n; i++) { n     if (binary_search(a, a + n, a[i] + x + y)) { n       printf( 1 n ); n       cout << a[i] + x << endl; n       return 0; n     } n   } n   for (int i = 0; i < n; i++) { n     if (binary_search(a, a + n, a[i] + y - x)) { n       if (a[i] + y > l && a[i] - x < 0) continue; n       if (a[i] + y <= l) { n         printf( 1 n ); n         cout << a[i] + y << endl; n       } else { n         printf( 1 n ); n         cout << a[i] - x << endl; n       } n       return 0; n     } n   } n   printf( 2 n ); n   cout << x <<     << y << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAX = 500010; n int n, m; n int seg[MAX]; n int vis[MAX]; n int p[MAX]; n int in[MAX], out[MAX]; n int aux = 0; n vector<int> adj[MAX]; n set<int> s; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   int x, s; n   cin >> x >> s; n   if (!s) { n     if (x == 1) n       cout <<  0 0 n ; n     else n       cout <<  -1 -1 n ; n     return 0; n   } n   if (9 * x < s) { n     cout <<  -1 -1 n ; n     return 0; n   } n   string sb; n   int s1 = s; n   for (int i = 1; i <= x; i++) { n     if (s > 9) n       sb +=  9 , s -= 9; n     else if (i == x) n       sb +=  0  + s; n     else if (s > 1) n       sb +=  0  + (s - 1), s = 1; n     else n       sb +=  0 ; n   } n   reverse(sb.begin(), sb.end()); n   cout << sb <<   n ; n   string st; n   for (int i = 1; i <= x; i++) { n     if (s1 >= 9) n       st +=  9 , s1 -= 9; n     else n       st += s1 +  0 , s1 = 0; n   } n   cout << st <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n const long long N = 1e5 + 5; n long long n, k; n long long a[N]; n bool check(long double x) { n   long double ans1 = 0, ans2 = 0; n   for (long long i = 0; i < n; i++) { n     if (a[i] > x) ans1 += (long double)(a[i] - x); n     if (x > a[i]) ans2 += (long double)(x - a[i]); n   } n   return ans1 * (100 - k) * 0.01 >= ans2; n } n int32_t main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cin >> n >> k; n   for (long long i = 0; i < n; i++) cin >> a[i]; n   long double l = *min_element(a, a + n), r = *max_element(a, a + n) + 1; n   for (long long i = 0; i < 300; i++) { n     long double mid = (l + r) / 2; n     if (check(mid)) n       l = mid; n     else n       r = mid; n   } n   cout << fixed << setprecision(12); n   cout << l <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n long long a[100000]; n int main() { n   long long n; n   long long cnt = 0; n   scanf( %I64d , &n); n   for (long long i = 0; i < n; i++) { n     scanf( %I64d , &a[i]); n     for (long long j = 0; j < i; j++) n       if (a[j] > a[i]) cnt++; n   } n   cout << (cnt % 2 + (cnt / 2) * 4) << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n; n vector<int> v; n map<int, int> a, b; n int main() { n   ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0); n   cin >> n; n   int m = n / 2; n   if (n % 2 != 0) { n     m++; n   } n   for (int i = 1; i <= n; i++) { n     int x, y; n     cin >> x >> y; n     a[x]++; n     if (x != y) { n       b[y]++; n     } n     v.push_back(x); n     v.push_back(y); n   } n   int ans = 1e9; n   for (int i = 0; i < 2 * n; i++) { n     int cur = v[i]; n     int cnt = a[cur]; n     int need = max(0, m - cnt); n     if (b[cur] >= need) { n       ans = min(ans, need); n     } n   } n   if (ans == 1e9) { n     cout << -1; n     return 0; n   } n   cout << ans; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   int n; n   cin >> n; n   int arr[n]; n   int count = 0; n   int count1 = 0; n   for (int i = 0; i < n; i++) { n     cin >> arr[i]; n     if (arr[i] & 1) { n       count++; n     } n   } n   int f[n]; n   int sum = 0; n   for (int i = 0; i < n; i++) { n     f[i] = floor(double(arr[i]) / 2); n     sum += f[i]; n   } n   for (int i = 0; i < n; i++) { n     if (sum == 0) break; n     if (arr[i] & 1) { n       sum++; n       f[i]++; n     } n   } n   for (int i = 0; i < n; i++) { n     cout << f[i] << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n inline void io() { n   freopen( input.txt ,  r , stdin); n   freopen( output.txt ,  w , stdout); n } n long long I() { n   long long a; n   cin >> a; n   return a; n } n void PV(vector<long long> v) { n   for (long long i = 0; i < (long long)v.size(); i++) cout << v[i] <<    ; n   cout <<   n ; n } n void PA(long long v[], long long n, long long x = 0) { n   for (long long i = x; i < n + x; i++) cout << v[i] <<    ; n   cout <<   n ; n } n void IA(long long a[], long long n, long long x = 0) { n   for (long long i = x; i < n + x; i++) cin >> a[i]; n } n const long long N = (2e5) + 7, N1 = 1e9 + 7, MOD = 1e9 + 7, INF = (1e17 + 7); n long long fact[N]; n long long power(long long a, long long b) { n   long long k = 1; n   for (; b; b /= 2, a = a * a % MOD) n     if (b % 2 == 1) k = k * a % MOD; n   return k % MOD; n } n void pre() { n   fact[0] = 1; n   for (long long i = 1; i < N; i++) fact[i] = (fact[i - 1] * i) % N1; n } n void solve() { n   long long n; n   cin >> n; n   long long ans = fact[2 * n] * (power(2, N1 - 2)); n   ans %= MOD; n   cout << ans <<   n ; n } n signed main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   long long t1 = 1, x0 = 1; n   pre(); n   cin >> t1; n   while (t1--) { n     solve(); n   } n } n


#include <bits/stdc++.h> n using namespace std; n stack<int> sk; n int main() { n   int T; n   string s; n   cin >> T; n   while (T--) { n     while (sk.size()) sk.pop(); n     cin >> s; n     int cnt = 0; n     for (int i = 0; i < s.size(); ++i) { n       if (sk.empty()) { n         sk.push(s[i] -  0 ); n         continue; n       } n       if (s[i] ==  0  && sk.top() == 1) { n         cnt++; n         sk.pop(); n       } else if (s[i] ==  1  && sk.top() == 0) { n         cnt++; n         sk.pop(); n       } else n         sk.push(s[i] -  0 ); n     } n     if (cnt % 2) n       puts( DA ); n     else n       puts( NET ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, m, i, j, k, chng_cnt; n   cin >> n >> m; n   int ara1[n + 1], ara2[n + 1], cnt[m + 1], maximizedMin; n   memset(cnt, 0, sizeof(cnt)); n   memset(ara1, 0, sizeof(ara1)); n   memset(ara2, 0, sizeof(ara2)); n   for (i = 1; i <= n; i++) { n     cin >> ara1[i]; n   } n   maximizedMin = n / m; n   for (i = 1; i <= n; i++) { n     if (ara1[i] > m) { n       ara2[i] = -1; n     } else { n       cnt[ara1[i]]++; n       if (cnt[ara1[i]] <= maximizedMin) { n         ara2[i] = ara1[i]; n       } else { n         ara2[i] = -1; n       } n     } n   } n   chng_cnt = 0; n   for (i = 1; i <= m; i++) { n     if (cnt[i] < maximizedMin) { n       for (j = 1; j <= n; j++) { n         if (cnt[i] == maximizedMin) break; n         if (ara2[j] == -1) { n           ara2[j] = i; n           cnt[i]++; n           chng_cnt++; n         } n       } n     } n   } n   for (i = 1; i <= n; i++) { n     if (ara2[i] == -1) { n       ara2[i] = ara1[i]; n     } n   } n   printf( %d %d n , maximizedMin, chng_cnt); n   for (i = 1; i <= n; i++) { n     if (i == n) n       printf( %d n , ara2[i]); n     else n       printf( %d  , ara2[i]); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1111; n const int M = 33333; n const int INF = 2e9 + 9; n int n, m; n int s, t; n int erased_edge; n struct Edge { n   int u, v, c; n   Edge() {} n   Edge(int u, int v, int c) { n     this->u = u; n     this->v = v; n     this->c = c; n   } n   int to(int node) { return u ^ node ^ v; } n } E[M]; n vector<int> a[N]; n vector<int> path; n vector<int> findPath() { n   queue<int> Q; n   vector<int> trace(n + 1, -1); n   trace[t] = 0; n   Q.push(t); n   while (!Q.empty()) { n     int u = Q.front(); n     Q.pop(); n     for (int e : a[u]) n       if (erased_edge != e) { n         int v = E[e].to(u); n         if (trace[v] != -1) continue; n         trace[v] = e; n         Q.push(v); n       } n   } n   vector<int> ans; n   if (trace[s] == -1) return ans; n   for (int u = s; u != t; u = E[trace[u]].to(u)) { n     ans.push_back(trace[u]); n   } n   return ans; n } n vector<int> solveOneEdge() { n   int best = INF; n   int ans = -1; n   for (int e : path) { n     erased_edge = e; n     if (findPath().empty() && best > E[e].c) { n       best = E[e].c; n       ans = e; n     } n   } n   vector<int> res; n   res.push_back(best); n   res.push_back(ans); n   return res; n } n int timer = 0; n int parent_edge[N], parent_cost[N]; n int num[N], low[N]; n bool containST[N]; n void dfs(int u) { n   ; n   num[u] = ++timer; n   low[u] = INF; n   containST[u] = u == t; n   for (int e : a[u]) { n     if (e == erased_edge || e == parent_edge[u]) continue; n     int v = E[e].to(u); n     if (num[v]) { n       ; n       low[u] = min(low[u], num[v]); n     } else { n       parent_edge[v] = e; n       parent_cost[v] = E[e].c; n       dfs(v); n       low[u] = min(low[u], low[v]); n       containST[u] |= containST[v]; n     } n   } n } n vector<int> solveTwoEdge() { n   int best = INF; n   int ans1 = -1, ans2 = -1; n   for (int e : path) { n     erased_edge = e; n     timer = 0; n     fill(parent_edge + 1, parent_edge + n + 1, 0); n     fill(num + 1, num + n + 1, 0); n     fill(containST + 1, containST + n + 1, 0); n     parent_edge[s] = -1; n     ; n     dfs(s); n     for (int v = 1; v <= n; ++v) n       if (v != s && low[v] >= num[v] && containST[v]) { n         if (best > E[erased_edge].c + parent_cost[v]) { n           best = E[erased_edge].c + parent_cost[v]; n           ans1 = erased_edge; n           ans2 = parent_edge[v]; n         }; n         ; n       } n   } n   vector<int> res; n   res.push_back(best); n   res.push_back(ans1); n   res.push_back(ans2); n   return res; n } n int main() { n   ios_base ::sync_with_stdio(0); n   cin >> n >> m; n   cin >> s >> t; n   for (int i = 1; i <= m; ++i) { n     int u, v, c; n     cin >> u >> v >> c; n     E[i] = Edge(u, v, c); n     a[u].push_back(i); n     a[v].push_back(i); n   } n   ::path = findPath(); n   ; n   if (::path.empty()) { n     cout << 0 <<   n  << 0 <<   n ; n     return 0; n   } n   vector<int> one = solveOneEdge(); n   vector<int> two = solveTwoEdge(); n   int mn = min(one[0], two[0]); n   if (mn == INF) { n     cout <<  -1 n ; n     return 0; n   } n   if (one[0] < two[0]) { n     cout << one[0] <<   n  << 1 <<   n  << one[1] <<   n ; n     return 0; n   } n   cout << two[0] <<   n  << 2 <<   n  << two[1] <<     << two[2] <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long i, j, k, n, arr[200010]; n   cin >> n; n   for (i = 0; i < n; i++) cin >> arr[i]; n   long long sum = arr[n - 1]; n   for (i = n - 1; i > 0; i--) { n     if (arr[i] <= arr[i - 1] && arr[i - 1] != 0) { n       if (arr[i] == 0) n         arr[i - 1] = arr[i]; n       else n         arr[i - 1] = arr[i] - 1; n     } n     sum += arr[i - 1]; n   } n   cout << sum; n } n


#include <bits/stdc++.h> n using namespace std; n const int MOD = 1e9 + 7; n const int SIZE = 1e6 + 10; n const int INF = 2147483647; n template <class T> n void maa(T& x, T y) { n   if (x < y) x = y; n } n template <class T> n void mii(T& x, T y) { n   if (x > y) x = y; n } n int a[SIZE]; n int record[SIZE][3], rn; n void update_answer(int ll, int rr, int cul) { n   record[rn][0] = ll; n   record[rn][1] = rr; n   record[rn][2] = cul; n   rn++; n } n int C, l[SIZE], r[SIZE], c[SIZE], cn, cc_pos[SIZE]; n pair<int, int> cc[SIZE]; n vector<pair<int, int> > qq; n int single_add[SIZE]; n int kerker[SIZE]; n set<pair<int, int> > singles; n pair<int, int> BIT[SIZE][2]; n void ins(int x, int v) { n   pair<int, int> me = make_pair(v, x); n   for (; x < SIZE; x += x & -x) { n     if (me > BIT[x][0]) { n       if (BIT[x][0].second != me.second) { n         BIT[x][1] = BIT[x][0]; n       } n       BIT[x][0] = me; n     } else if (me > BIT[x][1]) { n       BIT[x][1] = me; n     } n   } n } n int qq_BIT(int x, int id) { n   int res = 0; n   for (; x; x -= x & -x) { n     if (BIT[x][0].second != id) n       res = max(res, BIT[x][0].first); n     else n       res = max(res, BIT[x][1].first); n   } n   return res; n } n void update_single(int id, int v) { n   single_add[id] += v; n   ins(cc_pos[id], single_add[id]); n } n int get_single_ma(int id) { n   int it = upper_bound(cc + 1, cc + cn, make_pair(C - c[id], INF)) - cc - 1; n   if (it) { n     return qq_BIT(it, cc_pos[id]); n   } n   return 0; n } n pair<int, int> GP(int x, int y) { n   if (x > y) return make_pair(y, x); n   return make_pair(x, y); n } n int an[SIZE]; n int main() { n   int n; n   scanf( %d%d , &(n), &(C)); n   vector<pair<int, int> > rain; n   cn = 1; n   for (int i = (1); i < (n + 1); ++i) { n     scanf( %d%d%d , &(l[i]), &(r[i]), &(c[i])); n     rain.push_back(make_pair(l[i], i)); n     rain.push_back(make_pair(r[i], -i)); n     cc[cn++] = make_pair(c[i], i); n   } n   sort(cc + 1, cc + cn); n   for (int i = (1); i < (cn); ++i) cc_pos[cc[i].second] = i; n   sort((rain).begin(), (rain).end()); n   rain.push_back(make_pair(2000000000, 0)); n   int(m); n   scanf( %d , &m); n   for (int i = 0; i < (m); ++i) { n     int(x); n     scanf( %d , &x); n     qq.push_back(make_pair(x, i)); n   } n   sort((qq).begin(), (qq).end()); n   map<pair<int, int>, int> pair_add; n   set<int> now; n   int zero = 0; n   int pair_ma = 0; n   if (rain[0].first) { n     update_answer(0, rain[0].first, 0); n     zero += rain[0].first; n   } n   for (int i = 0, j; i < 2 * n; i = j) { n     for (j = i; j < 2 * n && rain[j].first == rain[i].first; j++) { n       int id = rain[j].second; n       if (id < 0) { n         now.erase(-id); n       } else { n         now.insert(id); n       } n     } n     if (((int)(now).size()) == 0) { n       update_answer(rain[i].first, rain[j].first, n                     zero + max(pair_ma, get_single_ma(0))); n       zero += rain[j].first - rain[i].first; n     } else if (((int)(now).size()) == 1) { n       int me = *now.begin(); n       if (c[me] <= C) { n         int v = single_add[me] + max(get_single_ma(me), kerker[me]); n         update_answer(rain[i].first, rain[j].first, zero + v); n         update_single(me, rain[j].first - rain[i].first); n         maa(pair_ma, v + rain[j].first - rain[i].first); n       } n     } else if (((int)(now).size()) == 2) { n       int me = *now.begin(); n       int you = *now.rbegin(); n       if (c[me] + c[you] <= C) { n         update_answer( n             rain[i].first, rain[j].first, n             zero + single_add[me] + single_add[you] + pair_add[GP(me, you)]); n         pair_add[GP(me, you)] += rain[j].first - rain[i].first; n         maa(pair_ma, single_add[me] + single_add[you] + pair_add[GP(me, you)]); n         maa(kerker[me], pair_add[GP(me, you)] + single_add[you]); n         maa(kerker[you], pair_add[GP(me, you)] + single_add[me]); n       } n     } n   } n   { n     int it = 0; n     for (int i = 0; i < (rn); ++i) { n       while (it < m && n              record[i][1] - record[i][0] + record[i][2] >= qq[it].first) { n         an[qq[it].second] = record[i][0] + (qq[it].first - record[i][2]); n         it++; n       } n     } n   } n   for (int i = 0; i < (m); ++i) printf( %d n , an[i]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long n, k; n   cin >> n >> k; n   string s; n   cin >> s; n   if (k == 0) { n     cout << s; n     return 0; n   } n   if (s.length() == 1) { n     if (k > 0) { n       cout <<  0 ; n       return 0; n     } n   } n   if (s[0] !=  1 ) { n     s[0] =  1 ; n     k--; n   } n   for (long long i = 1; i < n; i++) { n     if (k > 0) n       if (s[i] !=  0 ) { n         s[i] =  0 ; n         k--; n       } n   } n   cout << s; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int test; n   scanf( %d , &test); n   while (test--) { n     int fuck; n     string str; n     scanf( %d , &fuck); n     cin >> str; n     string ans =   ; n     for (auto &x : str) { n       if ((x -  0 ) % 2 == 1) { n         ans += x; n         if (ans.size() == 2) { n           break; n         } n       } n     } n     if (ans.size() == 2) { n       puts(ans.c_str()); n       continue; n     } n     puts( -1 ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n map<long long, int> MP; n int Q = 1; n vector<pair<int, int> > adj[500005]; n long long C[500005]; n set<int> S; n int p[500005]; n long long pw(int x) { n   if (!x) return 1LL; n   if (x & 1) return 2 * pw(x - 1) % 1000000007; n   long long hf = pw(x / 2); n   return hf * hf % 1000000007; n } n int par(int x) { n   if (p[x] == x) return x; n   return p[x] = par(p[x]); n } n int main() { n   int N, M, K; n   scanf( %d%d%d , &N, &M, &K); n   long long X = (1LL << K); n   for (int i = 1; i <= N; i++) scanf( %lld , &C[i]); n   for (int i = 0; i < M; i++) { n     int u, v; n     scanf( %d%d , &u, &v); n     long long x = C[u] ^ C[v]; n     int t = MP[x]; n     if (!t) { n       MP[x] = Q; n       adj[Q].push_back(pair<int, int>(u, v)); n       Q++; n     } else { n       adj[t].push_back(pair<int, int>(u, v)); n     } n   } n   int ans = 0; n   for (int i = 1; i <= N; i++) p[i] = i; n   for (int i = 1; i < Q; i++) { n     int sz = N; n     for (pair<int, int> j : adj[i]) { n       int u = j.first, v = j.second; n       S.insert(u); n       S.insert(v); n       if (par(u) != par(v)) { n         sz--; n         p[p[v]] = p[u]; n       } n     } n     ans = (ans + pw(sz)) % 1000000007; n     while (S.size()) { n       int now = *(S.begin()); n       p[now] = now; n       S.erase(S.begin()); n     } n   } n   printf( %lld n , n          ((X - Q + 1) % 1000000007 * pw(N) % 1000000007 + ans) % 1000000007); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T, typename L> n bool smax(T &x, L y) { n   return x < y ? (x = y, true) : false; n } n struct Q { n   int p, id; n }; n int get(int, int); n void dfs(int); n void sack(int, bool); n const int MAXN = 1e5 + 15, LG = 20; n int h[MAXN], cnt[MAXN]; n int st[MAXN], ft[MAXN], add[MAXN]; n int ans[MAXN]; n int par[LG][MAXN]; n vector<int> adj[MAXN]; n vector<Q> node[MAXN]; n int n, m; n int32_t main() { n   ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); n   cerr <<  HELLO WORLD :) n ; n   cin >> n; n   for (int i = 1; i <= n; i++) { n     int tmp; n     cin >> tmp; n     adj[tmp].push_back(i); n   } n   dfs(0); n   cin >> m; n   for (int i = 1; i <= m; i++) { n     int p, v; n     cin >> v >> p; n     node[get(v, p)].push_back({p, i}); n   } n   sack(0, 0); n   for (int i = 1; i <= m; i++) cout << ans[i] <<    ; n   return cout <<   n , false; n } n void dfs(int v) { n   static int t = 0; n   st[v] = t++; n   add[st[v]] = v; n   for (int i = 1; i < LG; i++) par[i][v] = par[i - 1][par[i - 1][v]]; n   for (auto u : adj[v]) { n     h[u] = h[v] + 1; n     par[0][u] = v; n     dfs(u); n   } n   ft[v] = t; n } n int get(int v, int p) { n   int u = v; n   for (int i = LG - 1; ~i; i--) n     if (p >> i & 1) u = par[i][u]; n   return u; n } n void sack(int v, bool keep) { n   int id = -1, mx = -1; n   for (auto u : adj[v]) n     if (smax(mx, ft[u] - st[u])) id = u; n   for (auto u : adj[v]) n     if (u != id) sack(u, 0); n   if (id != -1) sack(id, 1); n   for (auto u : adj[v]) n     if (u != id) n       for (int i = st[u]; i < ft[u]; i++) cnt[h[add[i]]]++; n   cnt[h[v]]++; n   if (v) n     for (auto u : node[v]) ans[u.id] = cnt[h[v] + u.p] - 1; n   if (!keep) n     for (int i = st[v]; i < ft[v]; i++) cnt[h[add[i]]]--; n } n


#include <bits/stdc++.h> n using namespace std; n vector<vector<int> > g, tr, comp; n vector<int> us, vis, whr, col; n void dfs(int v) { n   us[v] = 1; n   for (int i = 0; i < g[v].size(); i++) { n     int j = g[v][i]; n     if (!us[j]) dfs(j); n   } n   vis.push_back(v); n   return; n } n void dfstr(int v) { n   comp[comp.size() - 1].push_back(v); n   whr[v] = comp.size() - 1; n   us[v] = 1; n   for (int i = 0; i < tr[v].size(); i++) { n     int j = tr[v][i]; n     if (!us[j]) dfstr(j); n   } n   return; n } n void doit(int v, int c) { n   us[v] = 1; n   if ((v & 1) ^ c) n     col[v / 2] = 1; n   else n     col[v / 2] = 0; n   for (int i = 0; i < g[v].size(); i++) { n     int j = g[v][i]; n     if (!us[j]) doit(j, c); n   } n   return; n } n int main() { n   iostream::sync_with_stdio(0); n   cin.tie(0), cout.tie(0); n   int n; n   cin >> n; n   vector<string> all(n); n   vector<pair<string, string> > add(n); n   for (int i = 0; i < n; i++) { n     string s, c; n     cin >> s >> c; n     all[i] = s.substr(0, 2); n     add[i].first = s.substr(2, 1); n     add[i].second = c.substr(0, 1); n   } n   g.resize(2 * n); n   tr.resize(2 * n); n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < n; j++) { n       if (i == j) continue; n       if (all[i] + add[i].first == all[j] + add[j].first) { n         g[2 * i].push_back(2 * j + 1); n         g[2 * j].push_back(2 * i + 1); n         tr[2 * j + 1].push_back(2 * i); n         tr[2 * i + 1].push_back(2 * j); n         g[2 * i + 1].push_back(2 * j + 1); n         g[2 * j + 1].push_back(2 * i + 1); n         tr[2 * i + 1].push_back(2 * j + 1); n         tr[2 * j + 1].push_back(2 * i + 1); n       } n       if (all[i] + add[i].first == all[j] + add[j].second) { n         g[2 * i].push_back(2 * j); n         g[2 * j + 1].push_back(2 * i + 1); n         tr[2 * j].push_back(2 * i); n         tr[2 * i + 1].push_back(2 * j + 1); n       } n       if (all[i] + add[i].second == all[j] + add[j].second) { n         g[2 * i + 1].push_back(2 * j); n         g[2 * j + 1].push_back(2 * i); n         tr[2 * j].push_back(2 * i + 1); n         tr[2 * i].push_back(2 * j + 1); n       } n       if (all[i] + add[i].second == all[j] + add[j].first) { n         g[2 * i + 1].push_back(2 * j + 1); n         g[2 * j].push_back(2 * i); n         tr[2 * j + 1].push_back(2 * i + 1); n         tr[2 * i].push_back(2 * j); n       } n     } n   } n   us.resize(2 * n, 0); n   for (int i = 0; i < 2 * n; i++) { n     if (!us[i]) { n       dfs(i); n     } n   } n   us.assign(2 * n, 0); n   whr.resize(2 * n); n   for (int i = 2 * n - 1; i >= 0; i--) { n     int x = vis[i]; n     if (!us[x]) { n       comp.emplace_back(vector<int>()); n       dfstr(x); n     } n   } n   for (int i = 0; i < n; i++) { n     if (whr[2 * i] == whr[2 * i + 1]) { n       cout <<  NO n ; n       return 0; n     } n   } n   col.resize(n); n   us.assign(2 * n, 0); n   reverse(comp.begin(), comp.end()); n   for (int i = 0; i < comp.size(); i++) { n     for (int j = 0; j < comp[i].size(); j++) { n       if (!us[comp[i][j]]) doit(comp[i][j], 1); n     } n   } n   cout <<  YES n ; n   for (int i = 0; i < n; i++) { n     cout << all[i] << (col[i] == 0 ? add[i].first : add[i].second) <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long int conv[][3] = {{1, 2, 3}, {2, 3, 1}, {3, 1, 2}}; n void solve(long long int n) { n   int r = n % 3; n   if (r != 0) n = (n / 3 + 1) * 3; n   if (r == 0) n     r = 2; n   else n     r = r - 1; n   int k = 0; n   long long int cnt = 3; n   while (cnt < n) { n     k++; n     cnt += 3 * pow(4, k); n   } n   cnt -= 3 * pow(4, k); n   cnt = n - cnt; n   vector<int> v(k + 1); n   v[0] = 1; n   int j; n   for (int i = 1; i < k + 1; i++) { n     for (j = 0; j < 4; j++) { n       cnt -= 3 * pow(4, k - i); n       if (cnt <= 0) { n         cnt += 3 * pow(4, k - i); n         break; n       } n     } n     v[i] = j; n   } n   long long int ans = 0; n   for (int i = 0; i < k + 1; i++) { n     if (v[i] > 0) { n       ans += conv[v[i] - 1][r] << (2 * (k - i)); n     } n   } n   printf( %lld n , ans); n } n int main() { n   int t; n   long long int n; n   scanf( %d , &t); n   while (t--) { n     scanf( %lld , &n); n     solve(n); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long inf = 9.2e18; n const int maxn = 1e6 + 70; n const int INF = 2.1e9; n const int maxm = 1e6 + 70; n const int MOD = 1e9 + 7; n const double eps = 1e-7; n const double PI = acos(-1.0); n int n, m, k; n vector<int> G[maxn]; n int dep[maxn], mind[maxn], dp[maxn]; n int dfs(int u) { n   if (!G[u].size()) { n     mind[u] = dep[u] - k; n     dp[u] = 1; n     return 1; n   } else { n     int mx = 0; n     mind[u] = dep[u]; n     dp[u] = 0; n     for (int i = 0; i < G[u].size(); i++) { n       int v = G[u][i]; n       dep[v] = dep[u] + 1; n       int val = dfs(v); n       mind[u] = min(mind[u], mind[v]); n       if (mind[v] <= dep[u]) { n         dp[u] += dp[v]; n         mx = max(mx, val - dp[v]); n       } else { n         mx = max(mx, val); n       } n     } n     return dp[u] + mx; n   } n } n int main() { n   while (cin >> n >> k) { n     for (int i = 1; i <= n; i++) G[i].clear(); n     for (int i = 2; i <= n; i++) { n       int f; n       scanf( %d , &f); n       G[f].push_back(i); n     } n     dep[1] = 0; n     int ans = dfs(1); n     cout << ans << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   long long n, d, a, b, x, y; n   cin >> n >> d >> a >> b; n   pair<long long, long long> arr[n]; n   for (int i = 0; i < n; i++) { n     cin >> x >> y; n     x *= a; n     y *= b; n     arr[i] = {x + y, i + 1}; n   } n   sort(arr, arr + n); n   vector<int> vec; n   for (int i = 0; i < n; i++) { n     if (d >= arr[i].first) { n       d -= arr[i].first; n       vec.push_back(arr[i].second); n     } else n       break; n   } n   cout << vec.size() << endl; n   for (int i = 0; i < vec.size(); i++) cout << vec[i] <<    ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long gcd(long long a, long long b) { n   if (b == 0) n     return a; n   else n     return gcd(b, a % b); n } n const int maxn = 5e4 + 9; n int p[1009], a[1009]; n bool vis[1009]; n int dfs(int i) { n   if (vis[i]) return i; n   vis[i] = 1; n   int id = dfs(p[i]); n   return id; n } n int main() { n   int n; n   cin >> n; n   for (int i = 1; i <= n; i++) { n     cin >> p[i]; n   } n   for (int i = 1; i <= n; i++) { n     memset(vis, 0, sizeof(vis)); n     a[i] = dfs(i); n   } n   for (int i = 1; i <= n; i++) { n     cout << a[i] <<    ; n   } n   cout << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long int M = 1e3 + 7; n map<long long int, long long int> value, visit; n long long int n, m, points[M][M]; n long long int bfs(long long int node) { n   long long int i; n   for (i = 1; i < n + 1; i++) { n     if (points[node][i] == 1) { n       if (value[i] == 0) { n         value[i] = value[node]; n       } else if (value[i] != 2 && value[i] != value[node]) n         return -1; n     } n   } n   return 0; n } n long long int check() { n   long long int i, j; n   for (i = 1; i < n + 1; i++) { n     for (j = 1; j < n + 1; j++) { n       if (points[i][j] == 1) { n         if ((value[i] == 1 && value[j] == 3) || n             (value[i] == 3 && value[j] == 1)) { n           return 0; n         } n       } n     } n   } n } n int main() { n   scanf( %lld , &n); n   scanf( %lld , &m); n   long long int i, j, a, b, cnta = 0, cntb = 0, cntc = 0; n   for (i = 0; i < m; i++) { n     scanf( %lld , &a); n     scanf( %lld , &b); n     points[a][b] = 1; n     points[b][a] = 1; n   } n   for (i = 1; i < n + 1; i++) { n     long long int flag = 1; n     for (j = 1; j < n + 1; j++) { n       if (points[i][j] == 0 && i != j) flag = 0; n     } n     if (flag == 1) { n       value[i] = 2; n     } n   } n   for (i = 1; i < n + 1; i++) { n     for (j = 1; j < n + 1; j++) { n       if (points[i][j] == 0 && i != j) { n         if (value[i] == 0 || value[j] == 0) { n           if (value[i] == 0 && value[j] == 0) { n             value[i] = 1; n             value[j] = 3; n             if (bfs(i) == -1) { n               printf( No n ); n               return 0; n             } n             if (bfs(j) == -1) { n               printf( No n ); n               return 0; n             } n           } else if (value[i] == 0) { n             value[i] = 4 - value[j]; n             if (bfs(i) == -1) { n               printf( No n ); n               return 0; n             } n           } else { n             value[j] = 4 - value[i]; n             if (bfs(j) == -1) { n               printf( No n ); n               return 0; n             } n           } n         } else if (value[i] == value[j]) { n           printf( No n ); n           return 0; n         } n       } n     } n   } n   if (check() == 0) { n     printf( No n ); n     return 0; n   } n   printf( Yes n ); n   for (i = 1; i < n + 1; i++) { n     if (value[i] == 1) n       printf( a ); n     else if (value[i] == 2) n       printf( b ); n     else n       printf( c ); n   } n   printf(  n ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long N = 1 << 17; n const long long MN = N; n long long s[MN]; n long long m[MN]; n long long rr[MN]; n bool st[2 * N]; n long long tme[2 * N]; n long long lz[2 * N]; n long long nn; n vector<pair<long long, pair<long long, long long> > > manaFull[2 * N]; n struct second { n   long long s, m, r; n }; n void f1(long long k, long long l, long long r) { n   for (long long i = l; i <= r; ++i) { n     if (rr[i]) { n       manaFull[k].push_back({m[i] / rr[i] + !!(m[i] % rr[i]), {m[i], rr[i]}}); n     } else { n       manaFull[k].push_back({1e18, {m[i], rr[i]}}); n     } n   } n   sort(manaFull[k].begin(), manaFull[k].end()); n   for (long long i = 1; i < manaFull[k].size(); ++i) { n     manaFull[k][i].second.first += manaFull[k][i - 1].second.first; n   } n   for (long long i = (long long)manaFull[k].size() - 1; i > 0; --i) { n     manaFull[k][i - 1].second.second += manaFull[k][i].second.second; n   } n   if (l == r) return; n   long long mid = (l + r) / 2; n   f1(k * 2, l, mid); n   f1(k * 2 + 1, mid + 1, r); n } n long long getFirstNotFull(long long k, long long t) { n   long long lo = 0; n   long long hi = (long long)manaFull[k].size() - 1; n   long long res = hi + 1; n   while (lo <= hi) { n     long long mid = (lo + hi) / 2; n     if (manaFull[k][mid].first > t) { n       hi = mid - 1; n       res = mid; n     } else { n       lo = mid + 1; n     } n   } n   return res; n } n void lol(long long k, long long l, long long r, long long t) { n   if (l < r && lz[k]) { n     lz[k] = 0; n     lz[k * 2] = 1; n     lz[k * 2 + 1] = 1; n     tme[k * 2] = tme[k]; n     tme[k * 2 + 1] = tme[k]; n   } n } n bool eka[2 * N]; n long long absorbMana(long long k, long long l, long long r, long long x, n                      long long y, long long t) { n   long long res = 0; n   if (l > y || r < x) return 0; n   if (l >= x && r <= y && lz[k]) { n     long long firstNotFull = getFirstNotFull(k, t - tme[k]); n     if (firstNotFull != 0) { n       res += manaFull[k][firstNotFull - 1].second.first; n     } n     long long sz = r - l + 1; n     if (firstNotFull != sz) n       res += (manaFull[k][firstNotFull].second.second) * (t - tme[k]); n     if (eka[k]) { n       eka[k] = 0; n       res = min(m[k - N], s[k - N] + (t - tme[k]) * rr[k - N]); n     } n     lz[k] = 1; n     tme[k] = t; n     return res; n   } n   lol(k, l, r, t); n   long long mid = (l + r) / 2; n   long long q = absorbMana(k * 2, l, mid, x, y, t); n   long long w = absorbMana(k * 2 + 1, mid + 1, r, x, y, t); n   if (l >= x && r <= y) { n     lz[k] = 1; n     tme[k] = t; n   } n   return q + w; n } n int main() { n   long long n; n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cin >> n; n   nn = n; n   for (long long i = 0; i < n; ++i) { n     cin >> s[i] >> m[i] >> rr[i]; n     lz[i + N] = 1; n     eka[i + N] = 1; n   } n   f1(1, 0, N - 1); n   long long m; n   cin >> m; n   for (long long i = 0; i < m; ++i) { n     long long t, l, r; n     cin >> t >> l >> r; n     cout << absorbMana(1, 0, N - 1, l - 1, r - 1, t) <<   n ; n   } n } n


#include<bits/stdc++.h> n #define pb(x) push_back(x) n #define all(x) x.begin(), x.end() n #define N 200005 n #define FIFO ios::sync_with_stdio(false) n #define cout3(x, y, z) cout << x <<     << y <<     << z << endl n #define cout2(x, y) cout << x <<     << y << endl n #define INF (1<<30) n #define MOD 1000000007 n #define ones(x) __builtin_popcountll(x) n #define EPS 1e-6 n  n using namespace std; n  n long double v; n map<pair<long double, long double>, long double>memo; n  n long double DP(long double c, long double m, long double p){ n   n  //cout3(c, m, p); n  if(c < EPS && m < EPS)return p; n  if(memo.find(make_pair(c, m)) != memo.end())return memo[make_pair(c, m)]; n   n  long double ans = 0; n  //chosing c n  if(c > EPS){ n    n   if(c + EPS <= v){ n     n    if(m > EPS)ans += c * (1 + DP(0, m + c/2., p + c/2.));  n    else ans += c * (1 + DP(0, m, p + c)); n   } n   else{ n     n    if(m > EPS)ans += c * (1 + DP(c - v, m + v/2, p + v/2)); n    else ans += c * (1 + DP(c - v, m, p + v));  n   } n  } n   n  if(m > EPS){ n   if(m + EPS <= v){ n     n    if(c > EPS)ans += m * (1 + DP(c + m/2, 0, p + m/2)); n    else ans += m * (1 + DP(c, 0, p + m)); n   } n   else{ n     n    if(c > EPS)ans += m * (1 + DP(c + v/2, m - v, p + v/2)); n    else ans += m * (1 + DP(c, m - v, p + v));  n   } n  } n   n  if(p > EPS)ans += p; n    n  return memo[make_pair(c, m)] = ans; n   n } n  n  n  n //string cases[7] = { 2 ,  02 ,  012 ,  0112 ,  12 ,  102 ,  1002 }; n  n int main(){ n  n  FIFO; n  n  int tc = 0; n  cin >> tc; n  n  while(tc--){ n  n   long double c, m, p, ans = 0; n  n   cin >> c >> m >> p >> v; n   memo.clear(); n    n   cout << fixed << setprecision(6) << DP(c, m, p) << endl; n   n   n  /* n   for(int i = 0; i < 7; i++){ n     n    double curProp[] = {c, m, p}; n    bool ok = true; n    double prop = 1.0; n     n    for(int j = 0, id; j < cases[i].size() && ok; j++){ n      n     id = cases[i][j] -  0 ; n     if(curProp[id] == 0)ok = false; n     else{ n       n      prop *= curProp[id]; n      if(curProp[id] <= v){ n        n       vector<int>candidates; n       int len = 0; n        n       for(int l = 0; l < 3; l++)if(l != id && curProp[l] != 0)candidates.pb(l); n        n       len = candidates.size(); n       double add = curProp[id]/len; n       for(int l = 0; l < candidates.size(); l++)curProp[candidates[l]] += add; n        n       curProp[id] = 0; n      } n      else{ n         n       vector<int>candidates; n       int len = 0; n        n       for(int l = 0; l < 3; l++)if(l != id && curProp[l] != 0)candidates.pb(l); n        n       len = candidates.size(); n       double add = v/len; n       for(int l = 0; l < candidates.size(); l++)curProp[candidates[l]] += add; n        n       curProp[id] -= v; n      } n     } n      n     //for(int l = 0; l < 3; l++)cout  << curProp[l] <<    ; n     //cout << endl; n    } n     n    //cout2(ok, prop); n    if(ok)ans += prop * cases[i].size();  n   } n    n    n   cout << ans << endl; n    n */ n  } n  n  n } n


#include <bits/stdc++.h> n using namespace std; n const long long mod = 1e9 + 7; n const long long mod1 = 998244353; n long long power(long long a, long long b) { n   long long res = 1; n   while (b > 0) { n     if (b % 2 == 1) { n       res = (res * a) % mod; n     } n     a = (a * a) % mod; n     b >>= 1; n   } n   return res; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   int t; n   t = 1; n   long long lookup[1001][11]; n   for (int i = 0; i < 1001; i++) { n     for (int j = 0; j < 11; j++) { n       lookup[i][j] = 0; n     } n   } n   lookup[0][0] = 0; n   for (int i = 1; i <= 10; i++) { n     lookup[1][i] = 1; n     lookup[0][i] = 1; n   } n   for (int i = 1; i <= 1000; i++) { n     lookup[i][1] = i; n     lookup[i][0] = 1; n   } n   for (int i = 2; i <= 1000; i++) { n     for (int j = 2; j <= 10; j++) { n       lookup[i][j] += lookup[i - 1][j] + lookup[i][j - 1]; n       lookup[i][j] %= mod; n     } n   } n   while (t--) { n     int n, m; n     cin >> n >> m; n     long long ans = 0; n     long long temp1, temp2, temp3; n     for (int i = 1; i <= n; i++) { n       temp1 = n - i + 1; n       temp1 = lookup[temp1][m]; n       temp2 = i; n       temp2 = lookup[temp2][m - 1]; n       temp3 = (temp1 * temp2) % mod; n       ans += temp3; n       ans %= mod; n     } n     cout << ans; n   } n } n


#include <bits/stdc++.h> n #pragma GCC optimize(2) n #pragma GCC optimize(3) n #pragma GCC optimize( Ofast ) n #pragma GCC optimize( inline ) n #pragma GCC optimize( -fgcse ) n #pragma GCC optimize( -fgcse-lm ) n #pragma GCC optimize( -fipa-sra ) n #pragma GCC optimize( -ftree-pre ) n #pragma GCC optimize( -ftree-vrp ) n #pragma GCC optimize( -fpeephole2 ) n #pragma GCC optimize( -ffast-math ) n #pragma GCC optimize( -fsched-spec ) n #pragma GCC optimize( unroll-loops ) n #pragma GCC optimize( -falign-jumps ) n #pragma GCC optimize( -falign-loops ) n #pragma GCC optimize( -falign-labels ) n #pragma GCC optimize( -fdevirtualize ) n #pragma GCC optimize( -fcaller-saves ) n #pragma GCC optimize( -fcrossjumping ) n #pragma GCC optimize( -fthread-jumps ) n #pragma GCC optimize( -funroll-loops ) n #pragma GCC optimize( -fwhole-program ) n #pragma GCC optimize( -freorder-blocks ) n #pragma GCC optimize( -fschedule-insns ) n #pragma GCC optimize( inline-functions ) n #pragma GCC optimize( -ftree-tail-merge ) n #pragma GCC optimize( -fschedule-insns2 ) n #pragma GCC optimize( -fstrict-aliasing ) n #pragma GCC optimize( -fstrict-overflow ) n #pragma GCC optimize( -falign-functions ) n #pragma GCC optimize( -fcse-skip-blocks ) n #pragma GCC optimize( -fcse-follow-jumps ) n #pragma GCC optimize( -fsched-interblock ) n #pragma GCC optimize( -fpartial-inlining ) n #pragma GCC optimize( no-stack-protector ) n #pragma GCC optimize( -freorder-functions ) n #pragma GCC optimize( -findirect-inlining ) n #pragma GCC optimize( -fhoist-adjacent-loads ) n #pragma GCC optimize( -frerun-cse-after-loop ) n #pragma GCC optimize( inline-small-functions ) n #pragma GCC optimize( -finline-small-functions ) n #pragma GCC optimize( -ftree-switch-conversion ) n #pragma GCC optimize( -foptimize-sibling-calls ) n #pragma GCC optimize( -fexpensive-optimizations ) n #pragma GCC optimize( -funsafe-loop-optimizations ) n #pragma GCC optimize( inline-functions-called-once ) n #pragma GCC optimize( -fdelete-null-pointer-checks ) n using namespace std; n const long long maxn = 1005; n const long long maxm = 1000005; n const long long dd = 998244353; n long long n, m; n long long a[maxm], sav[maxm], dp[maxn][maxn], col[maxn][maxn], L[maxn], R[maxn]; n vector<long long> t[maxn]; n struct nod { n   long long val, pos; n } uni[maxm]; n inline bool cmp(nod x, nod y) { return x.val < y.val; } n inline long long read() { n   long long ret = 0, f = 1; n   char ch = getchar(); n   while (!isdigit(ch)) { n     if (ch ==  - ) f = -f; n     ch = getchar(); n   } n   while (isdigit(ch)) { n     ret = ret * 10 + ch -  0 ; n     ch = getchar(); n   } n   return ret * f; n } n void scan() { n   m = read(); n   n = read(); n   for (long long k = 1; k <= n; k++) { n     uni[k].val = read(); n     uni[k].pos = k; n   } n } n void prework() { n   long long lst = 0; n   sort(uni + 1, uni + n + 1, cmp); n   for (long long k = 1; k <= n; k++) { n     if (uni[k].val != uni[k - 1].val) lst++; n     sav[uni[k].pos] = lst; n   } n   m = lst; n   long long now = 0; n   for (long long k = 1; k <= n; k++) n     if (sav[k] != a[now]) a[++now] = sav[k]; n   n = now; n   for (long long k = 1; k <= n; k++) t[a[k]].push_back(k); n   for (long long k = 1; k <= m; k++) n     L[k] = t[k][0], R[k] = t[k][t[k].size() - 1]; n   for (long long k = 1; k <= m; k++) n     for (long long j = k + 1; j <= m; j++) n       if ((L[j] <= R[k] && R[k] < R[j]) || (R[j] >= L[k] && L[k] > L[j])) { n         puts( 0 ); n         exit(0); n       } n   for (long long k = 1; k <= n; k++) { n     long long now = k; n     for (long long j = k; j <= n; j++) n       col[k][j] = a[j] > a[now] ? now : now = j; n   } n } n void solve() { n   for (long long k = 0; k <= n + 1; k++) n     for (long long j = 0; j <= k; j++) dp[k][j] = 1; n   dp[0][1] = dp[1][1]; n   for (long long k = 2; k <= n; k++) { n     for (long long l = 1; l + k - 1 <= n; l++) { n       long long r = l + k - 1; n       long long x = col[l][r], siz = t[a[x]].size(); n       long long ll = L[a[x]], rr = R[a[x]]; n       long long ans1 = 0, ans2 = 0, ans3 = 1; n       for (long long k = l - 1; k < ll; k++) n         ans1 = (ans1 + dp[l][k] * dp[k + 1][ll - 1]) % dd; n       for (long long k = r + 1; k > rr; k--) n         ans2 = (ans2 + dp[k][r] * dp[rr + 1][k - 1]) % dd; n       dp[l][r] = ans1 * ans2 % dd; n       for (long long k = 0; k < siz - 1; k++) n         ans3 = ans3 * dp[t[a[x]][k] + 1][t[a[x]][k + 1] - 1] % dd; n       dp[l][r] = dp[l][r] * ans3 % dd; n     } n   } n   printf( %lld n , dp[1][n]); n } n signed main() { n   scan(); n   prework(); n   solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e6 + 7; n const int mod = 1e9 + 7; n int n, m, f[N], jc[N], njc[N], ans; n int qpow(int x, int y) { n   if (!x) return 0; n   int res = 1; n   for (; y; x = 1ll * x * x % mod, y >>= 1) n     if (y & 1) res = 1ll * res * x % mod; n   return res; n } n int ny(int x) { return qpow(x, mod - 2); } n int C(int x, int y) { return 1ll * jc[x] * njc[x - y] % mod * njc[y] % mod; } n int main() { n   scanf( %d%d , &n, &m); n   jc[0] = njc[0] = 1; n   for (int i = 1, iE = n; i <= iE; i++) n     jc[i] = 1ll * jc[i - 1] * i % mod, njc[i] = ny(jc[i]); n   ans = n; n   f[0] = 1; n   int sum = 1; n   for (int i = 1, iE = n - 1; i <= iE; i++) { n     f[i] = sum, f[i] = 1ll * f[i] * ny(i) % mod, (sum += f[i]) %= mod; n     if (i >= m) (sum += mod - f[i - m]) %= mod; n   } n   for (int i = 0, iE = n - 1; i <= iE; i++) (ans += mod - f[i]) %= mod; n   ans = 1ll * ans * jc[n - 1] % mod; n   printf( %d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n #pragma 03 n using namespace std; n int main() { n   cout <<  INTERCAL n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n pair<int, int> a[26]; n int b[26]; n int main() { n   for (int i = 0; i < 26; i++) a[i].second = i; n   string s; n   cin >> s; n   int d; n   cin >> d; n   for (int i = 0; i < int(s.size()); i++) a[int(s[i] -  a )].first++; n   sort(a, a + 26); n   int tmp = 0; n   for (int i = 0; i < 26; i++) n     if (d >= a[i].first) { n       b[tmp] = a[i].second; n       tmp++; n       d -= a[i].first; n     } n   bool g; n   cout << 26 - tmp << endl; n   for (int i = 0; i < int(s.size()); i++) { n     g = 1; n     for (int j = 0; j < tmp; j++) n       if (int(s[i] -  a ) == b[j]) g = 0; n     if (g) cout << s[i]; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long int res = 0; n   int a[200005], n, i, mm = INT_MAX; n   scanf( %d , &n); n   for (i = 1; i <= n; ++i) { n     scanf( %d , &a[i]); n     mm = min(mm, a[i]); n   } n   set<int> second; n   for (i = 1; i <= n; ++i) n     if (a[i] == mm) second.insert(i); n   res = 1ll * mm * n; n   long long int mx = 0; n   for (i = 1; i <= n; ++i) { n     if (a[i] == mm) continue; n     auto it = second.upper_bound(i); n     if (it != second.end()) n       mx = max(mx, (long long int)(*it - i)); n     else n       mx = max(mx, (long long int)(n - i + *second.begin())); n   } n   res += mx; n   printf( %lld , res); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int points[11] = {0, 25, 18, 15, 12, 10, 8, 6, 4, 2, 1}; n struct T { n   vector<int> a; n   string name; n   T() { n     for (int i = (1); i <= (55); i++) a.push_back(0); n   } n   bool operator<(const T &b) const { return a > b.a; } n } ma[1100]; n int n, games; n map<string, int> name; n int main() { n   cin >> games; n   while (games--) { n     int man; n     cin >> man; n     for (int i = (1); i <= (man); i++) { n       string s; n       cin >> s; n       int id = name[s]; n       if (id == 0) { n         id = name[s] = ++n; n         ma[id].name = s; n       } n       if (i <= 10) ma[id].a[0] += points[i]; n       ma[id].a[i]++; n     } n   } n   sort(ma + 1, ma + n + 1); n   cout << ma[1].name << endl; n   for (int i = (1); i <= (n); i++) swap(ma[i].a[0], ma[i].a[1]); n   sort(ma + 1, ma + n + 1); n   cout << ma[1].name << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   int n; n   while (cin >> n) { n     int pos, val; n     multimap<int, int> mp1, mp2; n     while (n--) { n       cin >> pos >> val; n       pos < 0 ? mp1.insert(pair<int, int>(pos, val)) n               : mp2.insert(pair<int, int>(pos, val)); n     } n     int min_size = min(mp1.size(), mp2.size()); n     multimap<int, int>::reverse_iterator it1 = mp1.rbegin(); n     multimap<int, int>::iterator it2 = mp2.begin(); n     long long ans = 0; n     for (int i = 0; i < min_size; ++i, ++it1, ++it2) n       ans += ((it1->second) + (it2->second)); n     if (mp1.size() > mp2.size()) ans += (it1->second); n     if (mp1.size() < mp2.size()) ans += (it2->second); n     cout << ans << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 3e5 + 10; n vector<int> G[maxn]; n int p[maxn], d[maxn], vis[maxn], mx, rt; n int find(int x) { n   if (p[x] != x) p[x] = find(p[x]); n   return p[x]; n } n void dfs(int u, int fa, int dep, int tp) { n   vis[u] = 1; n   if (dep > mx) { n     mx = dep; n     if (!tp) rt = u; n   } n   if (tp) p[u] = rt; n   for (int i = 0; i < G[u].size(); i++) { n     int v = G[u][i]; n     if (v != fa) dfs(v, u, dep + 1, tp); n   } n } n int main() { n   int n, m, q, tp, u, v; n   scanf( %d%d%d , &n, &m, &q); n   for (int i = 1; i <= n; i++) p[i] = i; n   for (int i = 1; i <= m; i++) { n     scanf( %d%d , &u, &v); n     G[u].push_back(v); n     G[v].push_back(u); n   } n   for (int i = 1; i <= n; i++) n     if (!vis[i]) { n       mx = rt = 0; n       dfs(i, 0, 0, 0); n       dfs(rt, 0, 0, 1); n       d[rt] = mx; n     } n   while (q--) { n     scanf( %d , &tp); n     if (tp == 2) { n       scanf( %d%d , &u, &v); n       int fu = find(u), fv = find(v); n       if (fu != fv) { n         p[fu] = fv; n         int t1 = d[fu] / 2 + d[fu] % 2; n         int t2 = d[fv] / 2 + d[fv] % 2; n         d[fv] = max(d[fu], d[fv]); n         d[fv] = max(d[fv], t1 + t2 + 1); n       } n     } else { n       scanf( %d , &u); n       int fu = find(u); n       printf( %d n , d[fu]); n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int Maxn = 1010, Maxm = 20; n const unsigned long long Seed = 19990213; n const long long Mod = 1000000007; n int n, m, cnt, tot1, fst1[Maxn], pre1[Maxn << 1], to1[Maxn << 1], tot2, n     fst2[Maxm], pre2[Maxm << 1], to2[Maxm << 1], bel[Maxm], tot[Maxm], n     stat[(1 << 12) + 10][Maxm], cur[Maxm], S, w[Maxm], size[Maxm]; n bool vis[Maxn], leaf[Maxm]; n long long f[Maxn][Maxm], g[Maxn][(1 << 12) + 10], ans; n unsigned long long ha[Maxm]; n map<unsigned long long, bool> Hash; n vector<int> son[Maxn], sub[Maxm]; n int Get() { n   char ch; n   int v = 0; n   bool f = false; n   while (!isdigit(ch = getchar())) n     if (ch ==  - ) f = true; n   v = ch - 48; n   while (isdigit(ch = getchar())) v = v * 10 + ch - 48; n   if (f) n     return -v; n   else n     return v; n } n void AddE1(int x, int y) { n   pre1[++tot1] = fst1[x], fst1[x] = tot1, to1[tot1] = y; n   pre1[++tot1] = fst1[y], fst1[y] = tot1, to1[tot1] = x; n } n void AddE2(int x, int y) { n   pre2[++tot2] = fst2[x], fst2[x] = tot2, to2[tot2] = y; n   pre2[++tot2] = fst2[y], fst2[y] = tot2, to2[tot2] = x; n } n bool cmp(const int &a, const int &b) { n   return ha[a] < ha[b] || (ha[a] == ha[b] && size[a] < size[b]); n } n void Add(long long &x, long long y) { n   x += y; n   if (x >= Mod) x -= Mod; n } n void Calc(int x, int fa) { n   leaf[x] = true, size[x] = 1; n   sub[x].clear(); n   for (int i = fst2[x]; i; i = pre2[i]) n     if (to2[i] != fa) { n       Calc(to2[i], x); n       size[x] += size[to2[i]]; n       sub[x].push_back(to2[i]); n       leaf[x] = false; n     } n   if (leaf[x]) n     ha[x] = 666072; n   else { n     ha[x] = 0; n     sort(sub[x].begin(), sub[x].end(), cmp); n     for (int i = 0; i < sub[x].size(); i++) n       ha[x] = (ha[x] * Seed + ha[sub[x][i]]) % Mod; n     ha[x] = ha[x] * 1234 % Mod; n   } n } n void Transform(int x, int w, int s) { n   if (x == cnt + 1) { n     S = max(S, s); n     for (int i = 1; i <= cnt; i++) stat[s][i] = cur[i]; n     return; n   } else { n     for (int i = 0; i <= tot[x]; i++) { n       cur[x] = i; n       Transform(x + 1, w * (tot[x] + 1), s + i * w); n     } n   } n } n void dfs(int x) { n   vis[x] = true; n   son[x].clear(); n   for (int i = fst1[x]; i; i = pre1[i]) n     if (!vis[to1[i]]) son[x].push_back(to1[i]), dfs(to1[i]); n   int len1 = int(son[x].size()); n   for (int i = 1; i <= m; i++) n     if (leaf[i]) n       f[x][i] = 1; n     else if (len1 > 0) { n       f[x][i] = 0, cnt = 0, S = 0; n       int len2 = int(sub[i].size()); n       for (int j = 0; j <= len2 - 1; j++) { n         bel[++cnt] = sub[i][j], tot[cnt] = 1; n         for (; j < len2 - 1 && ha[sub[i][j]] == ha[sub[i][j + 1]] && n                size[sub[i][j]] == size[sub[i][j + 1]]; n              j++) n           tot[cnt]++; n       } n       w[1] = 1; n       for (int j = 2; j <= cnt; j++) w[j] = w[j - 1] * (tot[j - 1] + 1); n       Transform(1, 1, 0); n       for (int s = 0; s <= S; s++) g[0][s] = 0; n       g[0][S] = 1; n       for (int j = 0; j <= len1 - 1; j++) { n         int u = son[x][j]; n         for (int s = 0; s <= S; s++) g[j + 1][s] = g[j][s]; n         for (int k = 1; k <= cnt; k++) { n           int v = bel[k]; n           if (f[u][v]) { n             for (int s = 0; s <= S; s++) n               if (stat[s][k]) { n                 Add(g[j + 1][s - w[k]], g[j][s] * f[u][v] % Mod); n               } n           } n         } n       } n       f[x][i] = g[len1][0]; n     } n } n void solve(int root) { n   for (int i = 1; i <= n; i++) vis[i] = false; n   dfs(1); n   long long res = 0; n   for (int i = 1; i <= n; i++) { n     Add(ans, f[i][root]); n     Add(res, f[i][root]); n     for (int j = 1; j <= m; j++) f[i][j] = 0; n   } n } n int main() { n   n = Get(); n   int x, y; n   for (int i = 1; i <= n - 1; i++) x = Get(), y = Get(), AddE1(x, y); n   m = Get(); n   for (int i = 1; i <= m - 1; i++) x = Get(), y = Get(), AddE2(x, y); n   for (int i = 1; i <= m; i++) { n     Calc(i, -1); n     if (!Hash[ha[i]]) { n       Hash[ha[i]] = true; n       solve(i); n     } n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int a[300005]; n int lastone[300005]; n int nextone[300005]; n long long sum[300005]; n vector<int> vt[3]; n int main() { n   int n; n   scanf( %d , &n); n   for (int i = 1; i <= n; i++) { n     scanf( %d , &a[i]); n     sum[i] = sum[i - 1] + a[i]; n     lastone[i] = (a[i] == 1000) ? i : lastone[i - 1]; n   } n   for (int i = n; i >= 1; i--) { n     nextone[i] = (a[i] == 1000) ? i : nextone[i + 1]; n   } n   long long ans = sum[n]; n   for (int i = 1; i <= n; i++) { n     long long bonus = sum[i] / 10; n     long long rem = sum[n] - sum[i]; n     long long res = sum[i] + rem - min(rem, bonus); n     ans = min(ans, res); n     if (a[i] == 2000) { n       int las = lastone[i]; n       if (las) { n         res = sum[las - 1]; n         bonus = sum[las - 1] / 10; n         res += a[las] - min(a[las] + 0ll, bonus); n         bonus -= min(a[las] + 0ll, bonus); n         bonus += (sum[i] - sum[las]) / 10; n         res += (sum[i] - sum[las]); n         rem = sum[n] - sum[i]; n         res += rem - min(rem, bonus); n         ans = min(ans, res); n       } n       las = nextone[i + 1]; n       if (las) { n         res = sum[i]; n         bonus = sum[i] / 10; n         res += (sum[las - 1] - sum[i]) - min(sum[las - 1] - sum[i], bonus); n         bonus -= min(sum[las - 1] - sum[i], bonus); n         bonus += a[las] / 10; n         res += a[las]; n         rem = sum[n] - sum[las]; n         res += rem - min(rem, bonus); n         ans = min(ans, res); n       } n     } n   } n   printf( %lld n , ans); n } n


#include <bits/stdc++.h> n using namespace std; n const long long INF = 2000000000000000000; n long double eps = 1e-12; n long long t, n, m, a; n vector<long long> vec; n void Stefan() { n   cin >> n >> m; n   long long cur = 1; n   for (int i = 0; i < n; i++) { n     cin >> a; n     vec.push_back(a); n   } n   if (n > m) { n     cout << 0; n     return; n   } n   for (int i = 0; i < n; i++) { n     for (int j = i + 1; j < n; j++) { n       cur *= abs(vec[i] - vec[j]); n       cur %= m; n       if (!cur) { n         cout << 0; n         return; n       } n     } n   } n   cout << cur; n } n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   t = 1; n   while (t) { n     Stefan(); n     t--; n   } n } n


#include <bits/stdc++.h> n using namespace std; n  n #define f first n #define s second n #define pb push_back n #define ar array n #define all(x) x.begin(), x.end() n #define siz(x) (int)x.size() n  n #define FOR(x, y, z) for(int x = (y); x < (z); x++) n #define ROF(x, z, y) for(int x = (y-1); x >= (z); x--) n #define F0R(x, z) FOR(x, 0, z) n #define R0F(x, z) ROF(x, 0, z) n #define trav(x, y) for(auto&x:y) n  n using ll = long long; n using vi = vector<int>; n using vl = vector<long long>; n using pii = pair<int, int>; n using vpii = vector<pair<int, int>>; n  n template<class T> inline bool ckmin(T&a, T b) {return b < a ? a = b, 1 : 0;} n template<class T> inline bool ckmax(T&a, T b) {return b > a ? a = b, 1 : 0;} n mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); n  n const char nl =   n ; n const int mxN = 2e5 + 10; n const int MOD = 1e9 + 7; n const long long infLL = 1e18; n  n struct dsu{ n     int n; vector<int> sz, par; n     void init(int _n){ n         n = _n; sz.resize(n); par.resize(n); n         F0R(i, n) sz[i] = 1, par[i] = i; n     } n     int find(int x){ return par[x] = ( x == par[x] ? par[x] : find(par[x])); } n     void comb(int a, int b) { n         a=find(a); b=find(b);   n         if(a!=b) {sz[a]+=sz[b]; par[b] = a; } n     } n }; n #undef D n #define D QWERWQWEQWEQWEQ n dsu D; n     set<ar<int, 3>> pq; n  n int32_t main(){ n     ios_base::sync_with_stdio(0); cin.tie(0); n     int n; cin >> n; n     vector<vi> a(n, vi(n)); n     vi par(n*10); n     D.init(10*n); n     vector<int> ans(10*n); n     int cnt = n; n     F0R(i, n){ n         F0R(j, n){ n             cin >> a[i][j]; n             if(i<=j) n             pq.insert(ar<int, 3>{a[i][j], i, j}); n         } n     } n     while(!pq.empty()){ n         ar<int, 3> t = *pq.begin(); pq.erase(t); n         if(t[1]==t[2]){ n             ans[t[1]] = t[0]; n         } n         else{                 n             int u = D.find(t[1]), v = D.find(t[2]); n             if(t[0]==ans[u]) par[v] = u, D.comb(u, v); n             else if(t[0]==ans[v]) par[u] = v, D.comb(v, u); n             else { n                 ans[cnt] = t[0]; n                 par[u]=cnt; n                 par[v]=cnt; n                 D.comb(cnt, u); n                 D.comb(cnt, v); n                 cnt++; n             } n         } n     } n     cout << cnt << nl; n     F0R(i, cnt){ n         cout << ans[i] <<    ; n     } n     cout << nl; n     cout << cnt << nl; n     F0R(i, cnt-1){ n         cout << i+1 <<     << par[i]+1 << nl; n     } n     return 0; n }


#include <bits/stdc++.h> n using namespace std; n int n; n int main() { n   cin >> n; n   const int maxN = n; n   int r[maxN]; n   int c[maxN]; n   int a[maxN][maxN]; n   for (int i = 0; i < n; i++) { n     r[i] = 0; n     for (int j = 0; j < n; j++) { n       cin >> a[i][j]; n       r[i] = r[i] + a[i][j]; n     } n   } n   for (int i = 0; i < n; i++) { n     c[i] = 0; n     for (int j = 0; j < n; j++) { n       c[i] = c[i] + a[j][i]; n     } n   } n   int ans = 0; n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < n; j++) { n       if (c[j] > r[i]) { n         ans++; n       } n     } n   } n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T> n inline T read() { n   T f = 0, x = 0; n   char c = getchar(); n   while (!isdigit(c)) f = c ==  - , c = getchar(); n   while (isdigit(c)) x = x * 10 + c - 48, c = getchar(); n   return f ? -x : x; n } n namespace run { n const int mod = 998244353, N = 1 << 16, inv2 = (mod + 1) / 2; n inline int add(int x, int y) { return x + y >= mod ? x - mod + y : x + y; } n inline int sub(int x, int y) { return x >= y ? x - y : x + mod - y; } n inline int qpow(int x, int y) { n   int ret = 1; n   while (y) { n     if (y & 1) ret = 1LL * x * ret % mod; n     x = 1LL * x * x % mod, y >>= 1; n   } n   return ret; n } n int inv[17], fac[17], ifac[17]; n inline void Getln(int *a, int *b, int len) { n   assert(a[0] == 1), b[0] = 0; n   for (int i = 0; i < len - 1; i++) { n     int sum = 1LL * a[i + 1] * (i + 1) % mod; n     for (int j = 0; j < i; j++) n       sum = sub(sum, 1LL * a[i - j] * (j + 1) % mod * b[j + 1] % mod); n     b[i + 1] = 1LL * sum * inv[i + 1] % mod; n   } n } n inline void Getexp(int *a, int *b, int len) { n   assert(a[0] == 0), b[0] = 1; n   for (int i = 0; i < len - 1; i++) { n     b[i + 1] = 0; n     for (int j = 0; j <= i; j++) n       b[i + 1] = (1LL * (j + 1) * a[j + 1] % mod * b[i - j] + b[i + 1]) % mod; n     b[i + 1] = 1LL * inv[i + 1] * b[i + 1] % mod; n   } n } n inline void fwt_xor(int *a, int inv, int limit) { n   for (int mid = 1; mid < limit; mid <<= 1) n     for (int i = 0; i < limit; i += (mid << 1)) n       for (int j = 0; j < mid; j++) { n         int x = a[i + j], y = a[i + j + mid]; n         if (inv == -1) x = 1LL * inv2 * x % mod, y = 1LL * inv2 * y % mod; n         a[i + j] = add(x, y), a[i + j + mid] = sub(x, y); n       } n } n int a[N], n, f[N], K, C, vis[N]; n map<vector<int>, int> mp; n vector<int> st[209]; n int count[N], tot, top, num[209], r[209], A[209]; n int main() { n   n = read<int>(), K = read<int>(), C = read<int>(); n   inv[0] = inv[1] = fac[0] = ifac[0] = ifac[1] = 1, count[0] = 0; n   for (int i = 1; i <= K; i++) fac[i] = 1LL * fac[i - 1] * i % mod; n   for (int i = 2; i <= K; i++) n     inv[i] = 1LL * (mod - mod / i) * inv[mod % i] % mod, n     ifac[i] = 1LL * inv[i] * ifac[i - 1] % mod; n   for (int i = 1; i < 1 << C; i++) count[i] = count[i >> 1] + (i & 1); n   int xorsum = 0; n   for (int i = 0; i < n; i++) xorsum ^= (a[i] = read<int>()); n   for (int i = 0; i < n; i++) { n     vector<int> b; n     for (int j = 0; j <= K; j++) b.push_back(a[i] ^ (a[i] - j)); n     mp[b]++; n   } n   for (map<vector<int>, int>::iterator i = mp.begin(); i != mp.end(); i++) n     st[++top] = i->first, num[top] = i->second; n   for (int s = 0; s < 1 << C; s++) { n     tot = 0; n     for (int i = 1; i <= top; i++) { n       int tmp = 0; n       for (int j = 0; j <= K; j++) tmp = (tmp << 1) | (count[st[i][j] & s] & 1); n       if (!vis[tmp]) n         A[++tot] = tmp, r[tot] = num[i], vis[tmp] = tot; n       else n         r[vis[tmp]] += num[i]; n     } n     static int c[17], d[17], ret[17]; n     memset(ret, 0, sizeof ret); n     for (int i = 1; i <= tot; i++) { n       for (int j = 0; j <= K; j++) n         c[j] = (A[i] >> (K - j) & 1) ? mod - ifac[j] : ifac[j]; n       Getln(c, d, K + 1); n       for (int j = 0; j <= K; j++) ret[j] = (1LL * d[j] * r[i] + ret[j]) % mod; n       vis[A[i]] = 0; n     } n     Getexp(ret, c, K + 1), f[s] = 1LL * c[K] * fac[K] % mod; n   } n   fwt_xor(f, -1, 1 << C); n   int p = qpow(qpow(n, K), mod - 2); n   for (int i = 0; i < 1 << C; i++) n     printf( %lld  , 1LL * f[i ^ xorsum] * p % mod); n   return 0; n } n }  // namespace run n int main() { return run::main(); } n


#include <bits/stdc++.h> n using namespace std; n int n, m, dx, dy; n int a[1000001]; n int cnt[1000000] = {}; n bool vis[1000000] = {}; n inline int gcd(int a, int b) { n   while (b) a %= b, swap(a, b); n   return a; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin >> n >> m >> dx >> dy; n   int g = gcd(dx, dy); n   dx /= g, dy /= g; n   a[0] = 0; n   int x = 0, y = 0; n   for (int i = 1; i <= n; ++i) { n     x += dx; n     if (x >= n) x -= n; n     y += dy; n     if (y >= n) y -= n; n     a[x] = y; n   } n   for (int i = 0; i < (int)(m); ++i) { n     cin >> x >> y; n     int c = y - a[x]; n     if (c < 0) c += n; n     ++cnt[c]; n   } n   int best = -1, bestind = -1; n   int delta = a[n]; n   for (int i = 0; i < (int)(n); ++i) n     if (!vis[i]) { n       int cur = 0; n       int j = i; n       while (!vis[j]) { n         vis[j] = true; n         cur += cnt[j]; n         j += delta; n       } n       if (cur > best) { n         best = cur; n         bestind = i; n       } n     } n   cout << 0 <<     << bestind <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <class T> n inline T checkmin(T &a, T b) { n   return (a < b) ? a : a = b; n } n template <class T> n inline T checkmax(T &a, T b) { n   return (a > b) ? a : a = b; n } n template <class T> n T GCD(T a, T b) { n   if (a < 0) return GCD(-a, b); n   if (b < 0) return GCD(a, -b); n   return (a == 0) ? b : GCD(b % a, a); n } n template <class T> n T LCM(T a, T b) { n   if (a < 0) return LCM(-a, b); n   if (b < 0) return LCM(a, -b); n   return (a == 0 || b == 0) ? 0 : a / GCD(a, b) * b; n } n template <class T> n inline T sqr(T X) { n   return X * X; n } n namespace Poor { n const int MaxiN = 100005; n const int LogMaxiN = 18; n const int CharSet = 256; n int N; n char St[MaxiN]; n long long Ra[CharSet]; n long long X[LogMaxiN][MaxiN]; n int SA[MaxiN], H[MaxiN]; n int LCP(int A, int B) { n   int Rec = A; n   for (int j = LogMaxiN - 1; j >= 0; --j) n     if (A + (1 << j) <= N && B + (1 << j) <= N && X[j][A] == X[j][B]) { n       A += (1 << j); n       B += (1 << j); n     } n   return A - Rec; n } n bool Cmp(int A, int B) { n   int L = LCP(A, B); n   return A + L == N || (B + L < N && St[A + L] < St[B + L]); n } n long long Solve(int L, int R, int C) { n   int Len = LCP(SA[L], SA[R]); n   long long res = n       (long long)(R - L + 1) * (R - L + 2) / 2LL * (long long)(Len - C); n   if (L == R) return res; n   if (LCP(SA[L], SA[L]) == Len) return res + Solve(L + 1, R, Len); n   if (LCP(SA[R], SA[R]) == Len) return res + Solve(L, R - 1, Len); n   int l = L, r = R; n   while (l < r) { n     int mid = (l + r + 1) >> 1; n     if (LCP(SA[L], SA[mid]) > Len) n       l = mid; n     else n       r = mid - 1; n   } n   res += Solve(L, l, Len) + Solve(l + 1, R, Len); n   return res; n } n void Run() { n   srand(time(NULL)); n   gets(St); n   N = strlen(St); n   for (int i = 0; i < CharSet; ++i) n     Ra[i] = (long long)rand() + (((long long)rand()) << 32LL); n   for (int i = 0; i < N; ++i) X[0][i] = Ra[(int)St[i]]; n   for (int j = 0; j < LogMaxiN - 1; ++j) n     for (int i = 0; i < N - (1 << j); ++i) n       X[j + 1][i] = (X[j][i] * Ra[j + 1]) ^ (X[j][i + (1 << j)]); n   for (int i = 0; i < N; ++i) SA[i] = i; n   sort(SA, SA + N, Cmp); n   cout << Solve(0, N - 1, 0) << endl; n } n }  // namespace Poor n int main() { n   Poor::Run(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n bool ansnull(int a, int b, int c, int d) { n   int ans = 0; n   if (a != 0) ++ans; n   if (b != 0) ++ans; n   if (c != 0) ++ans; n   if (d != 0) ++ans; n   if (ans >= 2) n     return true; n   else n     return false; n } n int main() { n   int a[100010][5]; n   int n, k; n   cin >> n >> k; n   for (int i = 0; i <= n; ++i) n     for (int j = 1; j <= 4; ++j) a[i][j] = 0; n   for (int i = 1; i <= n; ++i) n     for (int j = 1; j <= k; ++j) cin >> a[i][j]; n   vector<int> v(20, 0); n   for (int i = 1; i <= n; ++i) { n     int sum = 0; n     for (int j = 1; j <= 4; ++j) { n       sum += a[i][j] * (1 << (4 - j)); n     } n     v[sum]++; n   } n   if (v[0] != 0) { n     cout <<  YES ; n     return 0; n   } n   if (ansnull(v[1], v[2], v[4], v[8])) { n     cout <<  YES ; n     return 0; n   } n   int ind = -1; n   for (int i = 0; i <= 3; ++i) n     if (v[(1 << i)] != 0) ind = (1 << i); n   if (ansnull(v[12], v[3], 0, 0)) { n     cout <<  YES ; n     return 0; n   } n   if (ansnull(v[6], v[9], 0, 0)) { n     cout <<  YES ; n     return 0; n   } n   if (ansnull(v[5], v[10], 0, 0)) { n     cout <<  YES ; n     return 0; n   } n   if (ind != -1) { n     if (v[15 - ind] != 0) { n       cout <<  YES ; n       return 0; n     } n     if (ind == 1) { n       if (v[12] != 0 || v[6] != 0 || v[10] != 0) { n         cout <<  YES ; n         return 0; n       } n     } n     if (ind == 2) { n       if (v[12] != 0 || v[9] != 0 || v[5] != 0) { n         cout <<  YES ; n         return 0; n       } n     } n     if (ind == 4) { n       if (v[9] != 0 || v[3] != 0 || v[10] != 0) { n         cout <<  YES ; n         return 0; n       } n     } n     if (ind == 8) { n       if (v[6] != 0 || v[3] != 0 || v[5] != 0) { n         cout <<  YES ; n         return 0; n       } n     } n   } n   cout <<  NO ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long a, b, c, k; n   cin >> a >> b >> c >> k; n   if (k >= a + b + c - 3) { n     cout << a * b * c << endl; n   } else { n     long long res = 1; n     for (int x1 = 0; x1 < a; x1++) { n       long long help = (-k + x1) / (-2); n       for (int n = -1; n < 1; n++) { n         long long x2 = min(min(help + n, b - 1), k - x1); n         long long x3 = min(k - x1 - x2, c - 1); n         res = max(res, (1 + x1) * (1 + x2) * (1 + x3)); n       } n     } n     for (int x1 = 0; x1 < a; x1++) { n       long long help = (-k + x1) / (-2); n       for (int n = -1; n < 1; n++) { n         long long x2 = min(min(help + n, c - 1), k - x1); n         long long x3 = min(k - x1 - x2, b - 1); n         res = max(res, (1 + x1) * (1 + x2) * (1 + x3)); n       } n     } n     cout << res << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, i, j; n   scanf( %d , &n); n   int arr[n]; n   for (i = 0; i < n; i++) scanf( %d , &arr[i]); n   int t = 0; n   int index = -1; n   for (i = 0; i < n; i++) n     if (arr[i] - arr[(i + 1) % n] > 0) { n       t++; n       index = i; n     } n   if (t > 1) { n     cout <<  -1 n ; n     return 0; n   } n   if (index == -1) n     cout <<  0 n ; n   else n     cout << (n - index - 1) << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   cin >> n; n   for (int i = 0; i < n; ++i) { n     int x; n     cin >> x; n     cout << x - !(x & 1) <<    ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n struct node { n   int x, y, z; n }; n node q[200001]; n int n, m; n bool ok[2][100001]; n char s[100005]; n int main(void) { n   scanf( %d %d , &n, &m); n   for (int i = 0; i < 2; ++i) { n     scanf( %s , s + 1); n     for (int j = 1; j <= n; ++j) n       if (s[j] ==  - ) ok[i][j] = true; n   } n   bool flag = false; n   q[1].x = 0; n   q[1].y = 1; n   q[1].z = 0; n   int h = 1, e = 1; n   ok[0][1] = false; n   while (h <= e) { n     node p = q[h]; n     ++h; n     int i = p.x, j = p.y, k = p.z; n     if (j <= k) continue; n     if ((j + 1 > n) || (j + m > n)) { n       flag = true; n       break; n     } n     if (ok[i][j + 1]) { n       ++e; n       q[e].x = i; n       q[e].y = j + 1; n       q[e].z = k + 1; n       ok[i][j + 1] = false; n     } n     if (ok[i ^ 1][j + m]) { n       ++e; n       q[e].x = i ^ 1; n       q[e].y = j + m; n       q[e].z = k + 1; n       ok[i ^ 1][j + m] = false; n     } n     if (ok[i][j - 1]) { n       ++e; n       q[e].x = i; n       q[e].y = j - 1; n       q[e].z = k + 1; n       ok[i][j - 1] = false; n     } n   } n   if (flag) n     printf( YES n ); n   else n     printf( NO n ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int t, n, cnt[2000000]; n   scanf( %d , &n); n   for (int i = 0; i < 2000000; ++i) cnt[i] = 0; n   for (int i = 0; i < n; ++i) { n     scanf( %d , &t); n     cnt[t]++; n   } n   long long ans = 0; n   for (int i = 0; i < 2000000 - 1; ++i) { n     cnt[i + 1] += cnt[i] / 2; n     if (cnt[i] % 2) ++ans; n   } n   cout << ans << endl; n } n


#include <bits/stdc++.h> n #pragma GCC optimize( Ofast,no-stack-protector,unroll-loops,fast-math ) n #pragma GCC target(   n      sse,sse2,sse3,ssse3,sse4.1,sse4.2,avx,avx2,popcnt,tune=native ) n using namespace std; n namespace _c { n const double pi = acos(-1.0); n namespace min { n const int i8 = -128; n const int i16 = -32768; n const int i = -2147483647 - 1; n const long long l = -9223372036854775807LL - 1; n }  // namespace min n namespace max { n const int i8 = 127; n const int i16 = 32767; n const int i = 2147483647; n const long long l = 9223372036854775807LL; n }  // namespace max n }  // namespace _c n namespace _f { n template <typename T> n inline const T gcd(T m, T n) { n   while (n != 0) { n     T t = m % n; n     m = n; n     n = t; n   } n   return m; n } n template <typename T> n inline const T max(const T &a, const T &b) { n   return a > b ? a : b; n } n template <typename T> n inline const T min(const T &a, const T &b) { n   return a < b ? a : b; n } n template <typename T> n inline const T abs(const T &a) { n   return a > 0 ? a : -a; n } n template <typename T> n inline T pow(T a, T b) { n   T res = 1; n   while (b > 0) { n     if (b & 1) { n       res = res * a; n     } n     a = a * a; n     b >>= 1; n   } n   return res; n } n template <typename T> n inline T pow(T a, T b, const T &m) { n   a %= m; n   T res = 1; n   while (b > 0) { n     if (b & 1) { n       res = res * a % m; n     } n     a = a * a % m; n     b >>= 1; n   } n   return res % m; n } n }  // namespace _f n namespace io { n template <typename T> n inline void read(T &t) { n   register T res = 0, neg = 1; n   char g = getchar(); n   for (; !isdigit(g); g = getchar()) { n     if (g ==  - ) { n       neg = -1; n     } n   } n   for (; isdigit(g); g = getchar()) { n     res = res * 10 + g -  0 ; n   } n   t = res * neg; n } n inline int in() { n   register int T; n   read(T); n   return T; n } n inline long long in_ll() { n   register long long T; n   read(T); n   return T; n } n }  // namespace io n int Ans, T(2), x[11000], y[11000], z[11000], r[11000], f[11000], F[11000], n     s[1100000], b[11000], n, m, t[11000], c[11000], K, q[11000], Q[11000]; n void In(int a, int b, int c, int d) { n   x[T] = f[a]; n   y[T] = b; n   z[T] = c; n   r[T] = d; n   f[a] = T++; n   x[T] = f[b]; n   y[T] = a; n   z[T] = 0; n   r[T] = -d; n   f[b] = T++; n } n int main() { n   int i, j, k; n   scanf( %d%d , &n, &K); n   for (i = 0; i < n; i++) { n     scanf( %d%d%d , s + i, t + i, c + i); n     t[i] += s[i]; n     q[m++] = s[i]; n     q[m++] = t[i]; n   } n   q[m++] = -1; n   std::sort(q, q + m); n   for (i = j = 1; i < m; i++) n     if (q[i] != q[j - 1]) q[j++] = q[i]; n   m = j; n   for (i = 0; i + 1 < m; i++) In(i, i + 1, K, 0); n   for (i = 0; i < n; i++) { n     for (j = 0; j < m; j++) n       if (q[j] == s[i]) break; n     s[i] = j; n     for (j = 0; j < m; j++) n       if (q[j] == t[i]) break; n     t[i] = j; n     Q[i] = T; n     In(s[i], t[i], 1, -c[i]); n   } n   s[0] = 0; n   for (;;) { n     memset(b, 127, sizeof(b)); n     b[0] = 0; n     for (i = 0, j = 1; i < j; i++) n       for (k = f[s[i]]; k; k = x[k]) n         if (z[k] && b[s[i]] + r[k] < b[y[k]]) { n           b[y[k]] = b[s[i]] + r[k]; n           F[y[k]] = k; n           s[j++] = y[k]; n         } n     if (b[m - 1] == 2139062143) break; n     j = 2139062143; n     for (i = m - 1; i; i = y[F[i] ^ 1]) n       if (j > z[F[i]]) j = z[F[i]]; n     for (i = m - 1; i; i = y[F[i] ^ 1]) { n       Ans += j * r[F[i]]; n       z[F[i]] -= j; n       z[F[i] ^ 1] += j; n     } n   } n   for (i = 0; i < n; i++) n     if (z[Q[i]]) n       printf( 0  ); n     else n       printf( 1  ); n   printf(  n ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n auto random_address = [] { n   char *p = new char; n   delete p; n   return (uint64_t)p; n }; n mt19937 rng(chrono::steady_clock::now().time_since_epoch().count() * n             (random_address() | 1)); n const int INF = 1e9 + 5; n struct edge { n   int a, b, color, index; n }; n int N, M, K; n vector<edge> edges; n vector<vector<int>> color_count; n void set_color(edge &e, int color) { n   color_count[e.a][e.color]--; n   color_count[e.b][e.color]--; n   e.color = color; n   color_count[e.a][e.color]++; n   color_count[e.b][e.color]++; n } n bool fix() { n   vector<int> minimum(N, INF), maximum(N, 0); n   for (int i = 0; i < N; i++) n     for (int k = 0; k < K; k++) { n       minimum[i] = min(minimum[i], color_count[i][k]); n       maximum[i] = max(maximum[i], color_count[i][k]); n     } n   bool change = false; n   for (edge &e : edges) n     if ((maximum[e.a] - minimum[e.a] > 2 && n          color_count[e.a][e.color] == maximum[e.a]) || n         (maximum[e.b] - minimum[e.b] > 2 && n          color_count[e.b][e.color] == maximum[e.b])) { n       set_color(e, rng() % K); n       change = true; n     } n   return change; n } n int main() { n   cin >> N >> M >> K; n   color_count.assign(N, vector<int>(K, 0)); n   for (int i = 0; i < N; i++) { n     int _; n     cin >> _; n   } n   edges.resize(M); n   for (int i = 0; i < M; i++) { n     edge &e = edges[i]; n     cin >> e.a >> e.b; n     e.a--; n     e.b--; n     e.index = i; n     e.color = rng() % K; n     color_count[e.a][e.color]++; n     color_count[e.b][e.color]++; n   } n   bool bad; n   do { n     shuffle(edges.begin(), edges.end(), rng); n     bad = false; n     while (fix()) bad = true; n   } while (bad); n   vector<int> answers(M); n   for (edge &e : edges) answers[e.index] = e.color + 1; n   for (int i = 0; i < M; i++) cout << answers[i] <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n #pragma GCC diagnostic warning  -Wall  n int main() { n   int n; n   scanf( %d , &n); n   vector<vector<int> > sec(n); n   for (typeof(n) i = (0); i < (n); i++) { n     int k; n     scanf( %d , &k); n     sec[i].resize(k); n     for (typeof(k) j = (0); j < (k); j++) scanf( %d , &sec[i][j]); n     sort((sec[i]).begin(), (sec[i]).end()); n   } n   int count = 0; n   for (typeof(n) i = (0); i < (n); i++) { n     int k = int(sec[i].size()); n     if (not k) continue; n     int lasth = sec[i][0]; n     ; n     int l = (i - 1 + n) % n; n     int r = (i + 1) % n; n     for (typeof(k) j = (1); j < (k); j++) { n       int nl = lower_bound((sec[l]).begin(), (sec[l]).end(), sec[i][j]) - n                lower_bound((sec[l]).begin(), (sec[l]).end(), lasth); n       int nr = lower_bound((sec[r]).begin(), (sec[r]).end(), sec[i][j]) - n                lower_bound((sec[r]).begin(), (sec[r]).end(), lasth); n       if (nl != nr) count++; n       lasth = sec[i][j]; n     } n   } n   printf( %d n , count); n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long n, g; n   cin >> n >> g; n   vector<long long> v, ans; n   map<long long, long long> m; n   map<long long, long long>::iterator it; n   for (int i = 0; i < n; i++) { n     long long a; n     cin >> a; n     m[a]++; n   } n   for (it = m.begin(); it != m.end(); ++it) { n     v.push_back(it->second); n   } n   long long s1 = 0; n   for (int i = 0; i < v.size() - 1; i++) { n     long long s = 0; n     for (int j = i + 1; j < v.size(); j++) { n       s += v[j]; n     } n     s1 += s * v[i]; n   } n   cout << s1 << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0), cout.tie(0); n   cout << fixed << setprecision(15); n   int N, M, S, K; n   cin >> N >> M >> S >> K; n   vector<int> type(N); n   for (int i = 0; i < N; i++) { n     cin >> type[i]; n     --type[i]; n   } n   vector<vector<int>> adj(N); n   for (int i = 0; i < M; i++) { n     int u, v; n     cin >> u >> v; n     adj[u - 1].push_back(v - 1); n     adj[v - 1].push_back(u - 1); n   } n   vector<vector<int>> opt(N); n   for (int tp = 0; tp < S; tp++) { n     vector<int> dist(N, -1); n     queue<pair<int, int>> q; n     for (int i = 0; i < N; i++) { n       if (type[i] == tp) { n         q.push({i, 0}); n         dist[i] = 0; n       } n     } n     while (!q.empty()) { n       int loc = q.front().first; n       int tot = q.front().second; n       q.pop(); n       for (int nbr : adj[loc]) { n         if (dist[nbr] != -1) continue; n         dist[nbr] = tot + 1; n         q.push({nbr, tot + 1}); n       } n     }; n     ; n     for (int i = 0; i < N; i++) { n       opt[i].push_back(dist[i]); n     } n   } n   for (int i = 0; i < N; i++) { n     sort((opt[i]).begin(), (opt[i]).end()); n     int ans = 0; n     for (int j = 0; j < K; j++) { n       ans += opt[i][j]; n     } n     cout << ans <<    ; n   } n   cout << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   cin >> n; n   int list[n]; n   int i; n   for (i = 0; i < n; i++) cin >> list[i]; n   int total = accumulate(list, list + n, 0); n   n++; n   int count = 0; n   for (i = 1; i <= 5; i++) { n     if ((total + i) % n != 1) { n       count++; n     } n   } n   cout << count; n } n


#include <bits/stdc++.h> n using namespace std; n struct node { n   long long l; n   long long r; n   long long s; n   node* ln; n   node* rn; n   void change(long long n, long long k) { n     if (l == r) n       s += k; n     else { n       if (n <= (l + r) / 2) { n         if (ln == 0) { n           ln = new node; n           *ln = {l, (l + r) / 2, 0}; n         } n         ln->change(n, k); n       } else { n         if (rn == 0) { n           rn = new node; n           *rn = {(l + r) / 2 + 1, r, 0}; n         } n         rn->change(n, k); n       } n       s = 0; n       if (ln != 0) s += ln->s; n       if (rn != 0) s += rn->s; n     } n   } n   long long query(long long a, long long b) { n     if (a == l && b == r) return s; n     if (b < l || a > r) return 0; n     long long v = 0; n     if (ln != 0) v += ln->query(a, min(b, (l + r) / 2)); n     if (rn != 0) v += rn->query(max(a, (l + r) / 2 + 1), b); n     return v; n   } n }; n int main() { n   cin.sync_with_stdio(false); n   cin.tie(0); n   int n; n   cin >> n; n   unordered_map<int, node> m; n   for (int i = 0; i < n; i++) { n     long long a, b, c; n     cin >> a >> b >> c; n     if (!m.count(c)) m[c] = {1, 1 << 30, 0}; n     if (a == 1) n       m[c].change(b, 1); n     else if (a == 2) n       m[c].change(b, -1); n     else n       cout << m[c].query(1, b) <<   n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int t; n   cin >> t; n   while (t--) { n     long long int x, y, k; n     cin >> x >> y >> k; n     long long int tot = y * k + k; n     if (x >= tot) { n       cout << 1 + k << endl; n       continue; n     } else if (x == 2) { n       cout << tot - 1 + k << endl; n     } else { n       if ((tot - x) % (x - 1) != 0) { n         cout << ((tot - x) / (x - 1)) + 2 + k << endl; n       } else { n         cout << ((tot - x) / (x - 1)) + 1 + k << endl; n       } n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, k, c[1001], i, x, a, b; n int main() { n   cin >> n >> k; n   for (int x, i = 1; i <= n; ++i) { n     cin >> x; n     c[x]++; n   } n   for (i = 1; i <= k; ++i) { n     a = a + c[i] / 2 * 2; n     b = b + c[i] % 2; n   } n   cout << a + (b + 1) / 2; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 2e6; n const int maxv = 4e5; n const long long INF = 3e14 + 333; n struct Node { n   int u, v, c; n   void in() { scanf( %d%d%d , &u, &v, &c); } n } wo[maxn]; n vector<int> G[maxv], st[maxv], ed[maxv]; n int n, m; n int dfn = 0; n int d[maxv][2]; n int wd[maxv]; n void dfs(int v, int f) { n   d[v][0] = dfn; n   for (int i = 0; i < G[v].size(); i++) { n     int u = G[v][i]; n     if (u == f) continue; n     dfs(u, v); n   } n   for (int i = 0; i < st[v].size(); i++) wd[st[v][i]] = dfn++; n   d[v][1] = dfn; n } n unsigned long long dp[maxn]; n unsigned long long val[maxn], add[maxn]; n void push(int o, int l, int r) { n   if (r - l <= 1 || !add[o]) return; n   val[((o << 1))] += add[o]; n   val[((o << 1) | 1)] += add[o]; n   add[((o << 1))] += add[o]; n   add[((o << 1) | 1)] += add[o]; n   add[o] = 0; n } n void change(int o, int p, int l, int r, unsigned long long x) { n   push(o, l, r); n   if (r - l <= 1) { n     val[o] = x; n     return; n   } n   if (p < ((r + l) >> 1)) n     change(((o << 1)), p, l, ((r + l) >> 1), x); n   else n     change(((o << 1) | 1), p, ((r + l) >> 1), r, x); n   val[o] = min(val[((o << 1))], val[((o << 1) | 1)]); n } n void Add(int o, int a, int b, int l, int r, unsigned long long x) { n   if (a >= r || b <= l) return; n   push(o, l, r); n   if (l >= a && r <= b) { n     val[o] += x; n     add[o] += x; n     return; n   } n   Add(((o << 1)), a, b, l, ((r + l) >> 1), x); n   Add(((o << 1) | 1), a, b, ((r + l) >> 1), r, x); n   val[o] = min(val[((o << 1))], val[((o << 1) | 1)]); n } n unsigned long long rmq(int o, int a, int b, int l, int r) { n   if (a >= r || b <= l) return INF; n   push(o, l, r); n   if (l >= a && r <= b) return val[o]; n   return min(rmq(((o << 1)), a, b, l, ((r + l) >> 1)), n              rmq(((o << 1) | 1), a, b, ((r + l) >> 1), r)); n } n unsigned long long ans = 0; n void DP(int v, int f) { n   unsigned long long sum = 0; n   for (int i = 0; i < G[v].size(); i++) { n     int u = G[v][i]; n     if (u == f) continue; n     DP(u, v); n     sum += dp[u]; n   } n   for (int i = 0; i < G[v].size(); i++) { n     int u = G[v][i]; n     if (u == f) continue; n     Add(1, d[u][0], d[u][1], 0, m, sum - dp[u]); n   } n   for (int i = 0; i < st[v].size(); i++) { n     int w = st[v][i]; n     change(1, wd[w], 0, m, sum + wo[w].c); n   } n   for (int i = 0; i < ed[v].size(); i++) { n     int w = ed[v][i]; n     change(1, wd[w], 0, m, INF); n   } n   if (v == 1) ans = sum; n   dp[v] = rmq(1, d[v][0], d[v][1], 0, m); n } n int main() { n   cin >> n >> m; n   for (int i = 0; i < n - 1; i++) { n     int x, y; n     scanf( %d%d , &x, &y); n     G[x].push_back(y); n     G[y].push_back(x); n   } n   for (int i = 0; i < m; i++) { n     wo[i].in(); n     st[wo[i].u].push_back(i); n     ed[wo[i].v].push_back(i); n   } n   for (int i = 0; i < m; i++) change(1, i, 0, m, INF); n   dfs(1, -1); n   DP(1, -1); n   if (ans >= INF) n     cout << -1 << endl; n   else n     cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int a, b, c, d; n   cin >> a >> b >> c >> d; n   int pa = a, pb = b, pc = c, pd = d; n   a = pa; n   b = pb; n   c = pc; n   d = pd; n   vector<int> ans; n   if (a > 0) { n     ans.push_back(0); n     a--; n     while (a + b + c + d != 0) { n       if (ans.back() == 0) { n         if (b != 0) { n           b--; n           ans.push_back(1); n         } else { n           break; n         } n       } else if (ans.back() == 1) { n         if (a != 0) { n           ans.push_back(0); n           a--; n         } else if (c != 0) { n           ans.push_back(2); n           c--; n         } else { n           break; n         } n       } else if (ans.back() == 2) { n         if (d != 0) { n           ans.push_back(3); n           d--; n         } else if (b != 0) { n           ans.push_back(1); n           b--; n         } else { n           break; n         } n       } else { n         if (c != 0) { n           ans.push_back(2); n           c--; n         } else { n           break; n         } n       } n     } n     if (a + b + c + d == 0) { n       cout <<  YES  << endl; n       for (int i = 0; i < ans.size(); i++) { n         cout << ans[i] <<    ; n       } n       return 0; n     } n   } n   ans.resize(0); n   a = pa; n   b = pb; n   c = pc; n   d = pd; n   if (b >= 1) { n     ans.push_back(1); n     b--; n     while (a + b + c + d != 0) { n       if (ans.back() == 0) { n         if (b != 0) { n           b--; n           ans.push_back(1); n         } else { n           break; n         } n       } else if (ans.back() == 1) { n         if (a != 0) { n           ans.push_back(0); n           a--; n         } else if (c != 0) { n           ans.push_back(2); n           c--; n         } else { n           break; n         } n       } else if (ans.back() == 2) { n         if (d != 0) { n           ans.push_back(3); n           d--; n         } else if (b != 0) { n           ans.push_back(1); n           b--; n         } else { n           break; n         } n       } else { n         if (c != 0) { n           ans.push_back(2); n           c--; n         } else { n           break; n         } n       } n     } n     if (a + b + c + d == 0) { n       cout <<  YES  << endl; n       for (int i = 0; i < ans.size(); i++) { n         cout << ans[i] <<    ; n       } n       return 0; n     } n   } n   a = pa; n   b = pb; n   c = pc; n   d = pd; n   ans.resize(0); n   if (c >= 1) { n     ans.push_back(2); n     c--; n     while (a + b + c + d != 0) { n       if (ans.back() == 0) { n         if (b != 0) { n           b--; n           ans.push_back(1); n         } else { n           break; n         } n       } else if (ans.back() == 1) { n         if (a != 0) { n           ans.push_back(0); n           a--; n         } else if (c != 0) { n           ans.push_back(2); n           c--; n         } else { n           break; n         } n       } else if (ans.back() == 2) { n         if (d != 0) { n           ans.push_back(3); n           d--; n         } else if (b != 0) { n           ans.push_back(1); n           b--; n         } else { n           break; n         } n       } else { n         if (c != 0) { n           ans.push_back(2); n           c--; n         } else { n           break; n         } n       } n     } n     if (a + b + c + d == 0) { n       cout <<  YES  << endl; n       for (int i = 0; i < ans.size(); i++) { n         cout << ans[i] <<    ; n       } n       return 0; n     } n   } n   a = pa; n   b = pb; n   c = pc; n   d = pd; n   ans.resize(0); n   if (d >= 1) { n     ans.push_back(3); n     d--; n     while (a + b + c + d != 0) { n       if (ans.back() == 0) { n         if (b != 0) { n           b--; n           ans.push_back(1); n         } else { n           break; n         } n       } else if (ans.back() == 1) { n         if (a != 0) { n           ans.push_back(0); n           a--; n         } else if (c != 0) { n           ans.push_back(2); n           c--; n         } else { n           break; n         } n       } else if (ans.back() == 2) { n         if (d != 0) { n           ans.push_back(3); n           d--; n         } else if (b != 0) { n           ans.push_back(1); n           b--; n         } else { n           break; n         } n       } else { n         if (c != 0) { n           ans.push_back(2); n           c--; n         } else { n           break; n         } n       } n     } n     if (a + b + c + d == 0) { n       cout <<  YES  << endl; n       for (int i = 0; i < ans.size(); i++) { n         cout << ans[i] <<    ; n       } n       return 0; n     } n   } n   cout <<  NO ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const double eps = 1e-12; n int main() { n   int n; n   double r, v, T; n   scanf( %d%lf%lf , &n, &r, &v); n   T = 2 * acos(-1) * r / v; n   for (int i = 0; i < n; i++) { n     double s, f, tim, tot = 0; n     scanf( %lf%lf , &s, &f); n     tim = (f - s) / v; n     tot = T * floor(tim / T); n     tim -= tot; n     double lef = 0.0, rig = T; n     for (int j = 0; j < 100; j++) { n       double t = (lef + rig) / 2.0; n       double g = t + (2.0 * r / v) * sin(v * t / (2.0 * r)); n       if (g - tim >= -eps) n         rig = t; n       else n         lef = t; n     } n     tot += lef; n     printf( %.12lf n , tot); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void solve() { n   int n, k; n   cin >> n >> k; n   int ans = n; n   if (k >= n) { n     cout << 1; n   } else { n     for (int i = 2; i <= sqrt(n); i++) { n       if (n % i == 0) { n         if (i <= k) { n           ans = min(ans, n / i); n         } n         if (n / i <= k) { n           ans = min(ans, i); n         } n       } n     } n     cout << ans; n   } n } n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   int test; n   cin >> test; n   for (int i = 1; i <= test; i++) { n     solve(); n     cout <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n; n char c[1010]; n int main() { n   scanf( %d , &n); n   scanf( %s , c + 1); n   c[0] =  0 , c[n + 1] =  0 ; n   if (n == 1) { n     if (c[1] ==  0 ) return 0 * puts( No ); n     return 0 * puts( Yes ); n   } n   for (int i = 1; i < n; i++) { n     if (c[i] ==  1  && c[i + 1] ==  1 ) return 0 * puts( No ); n   } n   for (int i = 1; i <= n; i++) { n     if (c[i - 1] ==  0  && c[i] ==  0  && c[i + 1] ==  0 ) n       return 0 * puts( No ); n   } n   puts( Yes ); n   return 0; n } n