#include <bits/stdc++.h> n using namespace std; n const int N = 105; n char s[N], t[N]; n int dis(char a, char b) { n   int t = abs(a - b); n   return min(t, 26 - t); n } n int main() { n   int n; n   scanf( %d , &n); n   scanf( %s , s + 1); n   t[1] =  A , t[2] =  C , t[3] =  T , t[4] =  G ; n   int ans = 1e9; n   for (int i = 1; i + 3 <= n; i++) { n     int sum = 0; n     for (int j = 1; j <= 4; j++) sum += dis(t[j], s[i + j - 1]); n     ans = min(ans, sum); n   } n   cout << ans; n } n


#include <bits/stdc++.h> n using namespace std; n long long arr[1000]; n long long seg[1000005]; n long long gcd(long long a, long long b) { n   if (b == 0) { n     return a; n   } n   return gcd(b, a % b); n } n void buildtree(long long start, long long end, long long treenode) { n   if (start == end) { n     seg[treenode] = arr[start]; n     return; n   } n   long long mid = (start + end) / 2; n   buildtree(start, mid, 2 * treenode); n   buildtree(mid + 1, end, 2 * treenode + 1); n   seg[treenode] = gcd(seg[2 * treenode], seg[2 * treenode + 1]); n   return; n } n long long query(long long left, long long right, long long start, long long end, n                 long long treenode) { n   long long mid = (start + end) / 2; n   if (start > right || end < left) { n     return 0; n   } n   if (start >= left && end <= right) { n     return seg[treenode]; n   } n   long long ans = gcd(query(left, right, start, mid, 2 * treenode), n                       query(left, right, mid + 1, end, 2 * treenode + 1)); n   return ans; n } n bool check(long long n, long long k) { n   long long num1 = n; n   long long sum1 = 0; n   long long sum2 = 0; n   while (n > 0) { n     if (n >= k) { n       n = n - k; n       sum1 += k; n     } else { n       sum1 += n; n       n = 0; n       break; n     } n     if (n < 10) { n       continue; n     } n     sum2 += n / 10; n     n = n - n / 10; n   } n   return sum1 >= sum2; n } n long long dp[101][101][101]; n int solvedp(long long i, long long j, long long* pref, long long* suf, n             long long B, long long* arr) { n   if (i == j) { n     return 0; n   } n   if (B < 0) { n     return INT_MIN; n   } n   if (dp[i][j][B] != -1) { n     return dp[i][j][B]; n   } n   long long ans = 0; n   for (int k = i + 1; k < j - 1; k++) { n     if (i == 0) { n       if (!pref[k] && !suf[k + 1]) { n         ans = max((long long)1 + solvedp(k + 1, j, pref, suf, n                                          B - abs(arr[k + 1] - arr[k]), arr), n                   ans); n       } n     } else { n       if (!(pref[k] - pref[i - 1]) && !suf[k + 1]) { n         ans = max((long long)1 + solvedp(k + 1, j, pref, suf, n                                          B - abs(arr[k + 1] - arr[k]), arr), n                   ans); n       } n     } n   } n   dp[i][j][B] = ans; n   return dp[i][j][B]; n } n map<string, int> m1; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   long long n; n   cin >> n; n   vector<int> a = {0, 4, 10, 20, 35, 56, 83, 116, 155, 198, 244, 292}; n   if (n < a.size()) n     cout << a[n]; n   else n     cout << a[11] + 49 * (n - 11); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int type[100005]; n int lead[100005], degree[100005]; n vector<int> go(int n) { n   vector<int> v; n   while (true) { n     if (degree[n] > 1) { n       return v; n     } else if (lead[n] == -1) { n       v.push_back(n); n       return v; n     } n     v.push_back(n); n     n = lead[n]; n   } n } n int main() { n   int n; n   cin >> n; n   for (int i = 0; i < n; ++i) cin >> type[i]; n   for (int i = 0; i < n; ++i) { n     cin >> lead[i]; n     --lead[i]; n     if (lead[i] >= 0) ++degree[lead[i]]; n   } n   vector<int> best; n   for (int i = 0; i < n; ++i) { n     vector<int> cur; n     if (type[i]) cur = go(i); n     if (cur.size() > best.size()) best = cur; n   } n   reverse(best.begin(), best.end()); n   cout << best.size() <<   n ; n   for (int i = 0; i < best.size(); ++i) { n     cout << best[i] + 1 <<    ; n   } n   cout << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int n, k, h, cnt, tmp[100010], ans[100010]; n long double eps; n struct lemming { n   int v, w, id; n   bool operator<(const lemming &tmp) const { n     return w < tmp.w || (w == tmp.w && v < tmp.v); n   } n } a[100010]; n bool check(long double x) { n   int now = 1; n   memset(tmp, 0, sizeof(tmp)); n   for (int i = 1; i <= n; i++) { n     if ((long double)h * now <= (x + eps) * (long double)a[i].v) { n       tmp[now++] = a[i].id; n     } n   } n   return now > k; n } n int main() { n   cin >> n >> k >> h; n   eps = (n >= 1000 ? 1e-15 : 1e-18); n   for (int i = 1; i <= n; i++) a[i].id = i; n   for (int i = 1; i <= n; i++) cin >> a[i].w; n   for (int i = 1; i <= n; i++) cin >> a[i].v; n   sort(a + 1, a + n + 1); n   long double l = 0, r = k * h; n   while (l <= r - eps && cnt++ <= 1000) { n     long double mid = (l + r) / 2; n     if (check(mid)) { n       memcpy(ans, tmp, sizeof(ans)); n       r = mid; n     } else { n       l = mid; n     } n   } n   for (int i = 1; i <= k; i++) cout << ans[i] <<    ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int dp[100][100]; 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   if (n == 1 || n == 2) { n     cout << -1; n     return 0; n   } n   int m = INT_MAX; n   int mi = -1; n   int a[n]; n   int m2 = INT_MIN; n   int mi2 = -1; n   for (int i = 0; i < n; i++) { n     cin >> a[i]; n     if (m > a[i]) { n       m = a[i]; n       mi = i; n     } n     if (m == a[i] && i != 0 && i != n - 1) { n       mi = i; n     } n     if (m2 < a[i]) { n       m2 = a[i]; n       mi2 = i; n     } n     if (m2 == a[i] && i != 0 && i != n - 1) { n       mi2 = i; n     } n   } n   if (mi != mi2) { n     int t = a[mi2]; n     a[mi2] = a[mi]; n     a[mi] = t; n     int flag = 0; n     for (int i = 0; i < n - 1; i++) { n       if (a[i] >= a[i + 1]) { n       } else { n         flag = 1; n       } n     } n     if (flag) { n       int flag2 = 0; n       for (int i = 0; i < n - 1; i++) { n         if (a[i] <= a[i + 1]) { n         } else { n           flag2 = 1; n         } n       } n       if (flag2) { n         cout << mi + 1 <<     << mi2 + 1; n         return 0; n       } n     } n   } n   cout << -1; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m; n int state[10000]; n int cnt, sum; n void dfs(int depth) { n   if (depth >= m) { n     for (int i = 0; i < n - 1; ++i) { n       for (int j = i + 1; j < n; ++j) { n         if (state[i] > state[j]) ++cnt; n       } n     } n   } else { n     for (int i = 0; i < n; ++i) { n       for (int j = i; j < n; ++j) { n         for (int a = i, b = j; a < b; ++a, --b) swap(state[a], state[b]); n         dfs(depth + 1); n         for (int a = i, b = j; a < b; ++a, --b) swap(state[a], state[b]); n       } n     } n   } n } n int main() { n   scanf( %d%d , &n, &m); n   for (int i = 0; i < n; ++i) { n     scanf( %d , &state[i]); n   } n   cnt = 0; n   dfs(0); n   double ans = cnt / pow(n * (n - 1) / 2 + n, m); n   printf( %.12f n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long int mod = 1e9 + 7; n const long long int inf = 2e9 + 5; n double PI = 3.14159265358979323846; n void solve() { n   int n; n   cin >> n; n   int x = (int)sqrt(n); n   int best = inf; n   for (int i = max(1, x - 2); i <= x + 2; i++) { n     for (int j = max(1, x - 2); j <= x + 2; j++) { n       if (i * j >= n) { n         best = min(best, i + j - 2); n       } n     } n   } n   cout << best <<   n ; n } n int32_t main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   int T; n   cin >> T; n   while (T--) solve(); n   return 0; n } n


#include <bits/stdc++.h> n #pragma comment(linker,  /STACK:16777216 ) n using namespace std; n double pi = acos((double)-1); n const int MOD = 1000000007; n const int INF = 2147483647; n int main() { n   clock_t tStart = clock(); n   int tests = 1; n   for (int test = 1; test <= tests; test++) { n     int n, m, dx, dy; n     cin >> n >> m >> dx >> dy; n     vector<int> xi(m, 0), yi(m, 0); n     for (int i = 0; i < m; i++) { n       cin >> xi[i] >> yi[i]; n     } n     vector<int> y(n, 0); n     int step_x = 0, step_y = 0; n     for (int i = 0; i < n; i++) { n       y[step_x] = step_y; n       step_x += dx; n       step_y += dy; n       step_x %= n; n       step_y %= n; n     } n     vector<int> groups_counter(n, 0); n     int groups_counter_max = 0; n     int groups_counter_index = 0; n     for (int i = 0; i < m; i++) { n       int k = (yi[i] - y[xi[i]] + n) % n; n       groups_counter[k]++; n       if (groups_counter[k] > groups_counter_max) { n         groups_counter_max = groups_counter[k]; n         groups_counter_index = k; n       } n     } n     cout << 0 <<     << groups_counter_index << endl; n   } n } n


#include <bits/stdc++.h> n constexpr int P = 998244353; n struct Matrix { n   int a[4]; n   Matrix() : a{} {} n   friend Matrix operator*(const Matrix &lhs, const Matrix &rhs) { n     Matrix res; n     res.a[0] = (1ll * lhs.a[0] * rhs.a[0] + 1ll * lhs.a[1] * rhs.a[2]) % P; n     res.a[1] = (1ll * lhs.a[0] * rhs.a[1] + 1ll * lhs.a[1] * rhs.a[3]) % P; n     res.a[2] = (1ll * lhs.a[2] * rhs.a[0] + 1ll * lhs.a[3] * rhs.a[2]) % P; n     res.a[3] = (1ll * lhs.a[2] * rhs.a[1] + 1ll * lhs.a[3] * rhs.a[3]) % P; n     return res; n   } n }; n struct SegmentTree { n   const int n; n   std::vector<int> a; n   std::vector<Matrix> t; n   SegmentTree(const std::vector<int> &a) : n(a.size()), a(a), t(4 * n) { n     build(1, 0, n); n   } n   void build(int p, int l, int r) { n     if (r - l == 1) { n       t[p].a[0] = a[l] + 1; n       t[p].a[1] = 1; n       t[p].a[2] = (l && a[l - 1] == 1) ? (9 - a[l]) : 0; n     } else { n       int m = (l + r) / 2; n       build(2 * p, l, m); n       build(2 * p + 1, m, r); n       t[p] = t[2 * p] * t[2 * p + 1]; n     } n   } n   void update(int p, int l, int r, int x) { n     if (r - l == 1) { n       t[p].a[0] = a[l] + 1; n       t[p].a[2] = (l && a[l - 1] == 1) ? (9 - a[l]) : 0; n     } else { n       int m = (l + r) / 2; n       if (x < m) { n         update(2 * p, l, m, x); n       } else { n         update(2 * p + 1, m, r, x); n       } n       t[p] = t[2 * p] * t[2 * p + 1]; n     } n   } n   void modify(int x, int y) { n     a[x] = y; n     update(1, 0, n, x); n     if (x + 1 < n) update(1, 0, n, x + 1); n   } n   int query() { return t[1].a[0]; } n }; n int main() { n   std::ios::sync_with_stdio(false); n   std::cin.tie(nullptr); n   int n, m; n   std::cin >> n >> m; n   std::vector<int> a(n); n   for (int i = 0; i < n; ++i) { n     char c; n     std::cin >> c; n     a[i] = c -  0 ; n   } n   SegmentTree t(a); n   while (m--) { n     int x, y; n     std::cin >> x >> y; n     --x; n     t.modify(x, y); n     std::cout << t.query() <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int INF = 0x3f3f3f3f; n int a[205], b[105]; n set<int> st; n int n; n void solve() { n   cin >> n; n   for (int i = 1; i <= n; i++) cin >> b[i]; n   st.clear(); n   for (int i = 1; i <= 2 * n; i++) st.insert(i); n   for (int i = 1; i <= n; i++) { n     st.erase(b[i]); n   } n   if (st.size() != n) { n     cout << -1 << endl; n     return; n   } n   for (int i = 1; i <= n; i++) { n     a[2 * i - 1] = b[i]; n   } n   for (int i = 2; i <= 2 * n; i += 2) { n     auto tmp = st.upper_bound(a[i - 1]); n     if (tmp == st.end()) { n       cout << -1 << endl; n       return; n     } n     a[i] = *tmp; n     st.erase(tmp); n   } n   for (int i = 1; i <= 2 * n; i++) { n     cout << a[i] <<    ; n   } n   cout << endl; n } n int main() { n   ios::sync_with_stdio(false); n   int t; n   cin >> t; n   while (t--) { n     solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int Maxn = 100005; n const double eps = 1e-8, Pi = acos(-1.0); n int n; n double a, x[Maxn], y[Maxn], dis[Maxn]; n pair<double, double> pos[Maxn]; n priority_queue<double, vector<double>, greater<double> > Pr; n bool check(double r) { n   int m = 0; n   for (int i = 1; i <= n; i++) { n     double l = sqrt((x[i] - a) * (x[i] - a) + y[i] * y[i]); n     if (l > dis[i] + r || dis[i] > l + r || r > dis[i] + l) continue; n     double p = atan2(y[i], x[i] - a); n     double range = acos((l * l + r * r - dis[i] * dis[i]) / (2 * l * r)); n     double L = p - range, R = p + range; n     if (L < -Pi) L += 2 * Pi; n     if (L > Pi) L -= 2 * Pi; n     if (R < -Pi) R += 2 * Pi; n     if (R > Pi) R -= 2 * Pi; n     if (L > R) swap(L, R); n     pos[++m] = make_pair(L, R); n   } n   sort(pos + 1, pos + 1 + m); n   while (!Pr.empty()) Pr.pop(); n   for (int i = 1; i <= m; i++) { n     while (!Pr.empty() && Pr.top() < pos[i].first) Pr.pop(); n     if (!Pr.empty() && Pr.top() <= pos[i].second) return true; n     Pr.push(pos[i].second); n   } n   return false; n } n int main() { n   scanf( %d%lf , &n, &a); n   for (int i = 1; i <= n; i++) n     scanf( %lf%lf , &x[i], &y[i]), n         dis[i] = sqrt((x[i] + a) * (x[i] + a) + y[i] * y[i]); n   double l = 0, r = 2 * a; n   while (r - l >= eps) { n     double mid = (l + r) / 2; n     if (check(mid)) n       r = mid; n     else n       l = mid; n   } n   printf( %.10lf , l); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n inline long long small(char a, char b) { n   return min(abs(a - b), min(a + 26 - b, b + 26 - a)); n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   long long n, pos; n   cin >> n >> pos; n   string x; n   cin >> x; n   bool gotomid = 0; n   int mid = n / 2; n   if (pos > mid) { n     pos = n - pos + 1; n   } n   long long res = small(x[pos - 1], x[n - pos]); n   int right = -1, left = 1e5; n   for (int i = mid - 1; i >= pos; i--) n     if (x[i] != x[n - 1 - i]) { n       right = i; n       break; n     } n   for (int i = 0; i < pos - 1; i++) n     if (x[i] != x[n - 1 - i]) { n       left = i; n       break; n     } n   if (left == 1e5 && right == -1) n     cout << res; n   else if (left != 1e5 && right == -1) { n     for (int i = left; i < pos - 1; i++) n       if (x[i] != x[n - 1 - i]) res += small(x[i], x[n - 1 - i]); n     res += pos - 1 - left; n     cout << res; n   } else if (left == 1e5 && right != -1) { n     for (int i = right; i >= pos; i--) n       if (x[i] != x[n - 1 - i]) res += small(x[i], x[n - 1 - i]); n     res += right - (pos - 1); n     cout << res; n   } else { n     for (int i = left; i < pos - 1; i++) n       if (x[i] != x[n - 1 - i]) res += small(x[i], x[n - 1 - i]); n     for (int i = right; i >= pos; i--) n       if (x[i] != x[n - 1 - i]) res += small(x[i], x[n - 1 - i]); n     res += 2 * min(pos - 1 - left, right - (pos - 1)) + n            max(pos - 1 - left, right - (pos - 1)); n     cout << res; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int n, m, w[100010], in1, in2, in3; n double ans; n int main() { n   scanf( %d%d , &n, &m); n   for (int i = 1; i <= n; i++) scanf( %d , &w[i]); n   for (int i = 1; i <= m; i++) { n     scanf( %d%d%d , &in1, &in2, &in3); n     ans = max(ans, (w[in1] + w[in2]) * 1.0 / in3); n   } n   printf( %.15lf n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int64_t LINF = 1LL << 62; n struct Point { n   int x, y; n   bool operator<(const Point& o) const { n     return x < o.x || (x == o.x && y < o.y); n   } n }; n inline int64_t cross(const Point& o, const Point& a, const Point& b) { n   return (a.x - o.x) * (int64_t)(b.y - o.y) - n          (a.y - o.y) * (int64_t)(b.x - o.x); n } n int64_t solve(vector<Point> a, int m) { n   int n = ((int)(a).size()); n   Point o = a[0]; n   sort(a.begin() + 1, a.end(), n        [o](const Point& a, const Point& b) { return cross(o, a, b) > 0; }); n   vector<vector<int>> works(n); n   vector<vector<vector<int64_t>>> dp(n); n   works[0] = {0}; n   dp[0] = vector<vector<int64_t>>(1, vector<int64_t>(m + 1, -LINF)); n   dp[0][0][1] = 0; n   int64_t ans = 0; n   for (int i = 1; i < n; ++i) { n     for (int j = i - 1; j > 0; --j) { n       if (works[i].empty() || cross(a[i], a[works[i].back()], a[j]) < 0) { n         works[i].push_back(j); n       } n     } n     works[i].push_back(0); n     reverse(works[i].begin(), works[i].end()); n     dp[i] = vector<vector<int64_t>>(((int)(works[i]).size()), n                                     vector<int64_t>(m + 1, -LINF)); n     for (int j = 0; j < ((int)(works[i]).size()); ++j) { n       int prev = works[i][j]; n       int pos = 0, step = 1; n       while (step < ((int)(works[prev]).size())) { n         step *= 2; n       } n       for (; step > 0; step /= 2) { n         if (pos + step < ((int)(works[prev]).size()) && n             cross(a[i], a[prev], a[works[prev][pos + step]]) <= 0) { n           pos += step; n         } n       } n       for (int k = 2; k <= m; ++k) { n         dp[i][j][k] = max(j == 0 ? -LINF : dp[i][j - 1][k], n                           dp[prev][pos][k - 1] + cross(o, a[prev], a[i])); n       } n     } n     ans = max(ans, dp[i].back().back()); n   } n   return ans; n } n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   int n, m; n   cin >> n >> m; n   vector<Point> a(n); n   for (int i = 0; i < n; ++i) { n     cin >> a[i].x >> a[i].y; n   } n   sort(a.begin(), a.end()); n   int64_t ans = 0; n   for (int i = 0; i < n; ++i) { n     ans = max(ans, solve(vector<Point>(a.begin() + i, a.end()), m)); n   } n   cout << ans / 2; n   if (ans % 2 == 0) { n     cout <<  .00 n ; n   } else { n     cout <<  .50 n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, start, x; n   cin >> n >> start >> x; n   int ans = x; n   int val, nt; n   cout <<  ?   << start << endl; n   cin >> val >> nt; n   if (val >= x) { n     cout <<  !   << val << endl; n     return 0; n   } n   int best = start; n   long long diff = x - val; n   int step = sqrt(n); n   start = ((rand() << 4 + rand()) % n + n) % n + 1; n   bool was[n + 1]; n   fill(was, was + n + 1, 0); n   for (int i = 0; i < min(1000, n / 2); i++) { n     while (was[start]) { n       start = ((rand() << 4 + rand()) % n + n) % n + 1; n     } n     was[start] = 1; n     cout <<  ?   << start << endl; n     cin >> val >> nt; n     if (val < x and nt == -1) { n       cout <<  !   << -1 << endl; n       return 0; n     } n     if (val < x and x - val < diff) diff = x - val, best = start; n   } n   while (1) { n     cout <<  ?   << best << endl; n     cin >> val >> nt; n     if (val < x and nt == -1) { n       cout <<  !   << -1 << endl; n       return 0; n     } n     if (val >= x) { n       cout <<  !   << val << endl; n       return 0; n     } n     best = nt; n   } n   return 0; n } n


#include <bits/stdc++.h> n #pragma comment(linker,  /STACK:200000000 ) n using namespace std; n template <typename T> n inline T Abs(T x) { n   return (x >= 0) ? x : -x; n } n template <typename T> n inline T sqr(T x) { n   return x * x; n } n template <typename T> n inline string toStr(T x) { n   stringstream st; n   st << x; n   string s; n   st >> s; n   return s; n } n template <typename T> n inline int bit(T mask, int b) { n   return (b >= 0 && (mask & (T(1) << b)) != 0) ? 1 : 0; n } n inline int nextInt() { n   int x; n   if (scanf( %d , &x) != 1) throw; n   return x; n } n inline long long nextInt64() { n   long long x; n   if (scanf( %lld , &x) != 1) throw; n   return x; n } n inline double nextDouble() { n   double x; n   if (scanf( %lf , &x) != 1) throw; n   return x; n } n const int INF = (int)1E9; n const long long INF64 = (long long)1E18; n const long double EPS = 1E-9; n const long double PI = 3.1415926535897932384626433832795; n const int MAXN = 500100; n const int OSN = (1 << 19) - 1; n int n, lst[MAXN], rst[MAXN], tree[2 * OSN + 10], tree0[2 * OSN + 10]; n vector<int> g[MAXN], st; n void DFS(int v, int prev) { n   lst[v] = int(st.size()); n   st.push_back(v); n   for (int i = 0; i < (int)(g[v].size()); i++) { n     int u = g[v][i]; n     if (u != prev) DFS(u, v); n   } n   rst[v] = int(st.size() - 1); n } n void update(int v, int value) { n   v += OSN; n   tree[v] = value; n   while (v > 0) { n     v = (v - 1) >> 1; n     tree[v] = max(tree[v], value); n   } n } n void update(int v, int l, int r, int ll, int rr, int value) { n   if (l > rr || ll > r || ll > rr) return; n   if (ll <= l && r <= rr) { n     tree0[v] = value; n     return; n   } n   int mid = (l + r) >> 1; n   update(2 * v + 1, l, mid, ll, rr, value); n   update(2 * v + 2, mid + 1, r, ll, rr, value); n } n int getMax(int v, int l, int r, int ll, int rr) { n   if (l > rr || ll > r || ll > rr) return 0; n   if (ll <= l && r <= rr) return tree[v]; n   int mid = (l + r) >> 1; n   return max(getMax(2 * v + 1, l, mid, ll, rr), n              getMax(2 * v + 2, mid + 1, r, ll, rr)); n } n int getMax(int v, int l, int r, int x) { n   if (l > x || x > r) return 0; n   if (l == r) return tree0[v]; n   int mid = (l + r) >> 1; n   int ans = tree0[v]; n   ans = max(ans, getMax(2 * v + 1, l, mid, x)); n   ans = max(ans, getMax(2 * v + 2, mid + 1, r, x)); n   return ans; n } n int check(int v) { n   int t0 = getMax(0, 0, OSN, lst[v], rst[v]); n   int t1 = getMax(0, 0, OSN, lst[v]); n   return int(t0 < t1); n } n int main() { n   n = nextInt(); n   for (int i = 0; i < (int)(n - 1); i++) { n     int v = nextInt() - 1; n     int u = nextInt() - 1; n     g[v].push_back(u); n     g[u].push_back(v); n   } n   DFS(0, 0); n   int q = nextInt(); n   for (int i = 0; i < (int)(q); i++) { n     int type = nextInt(); n     int v = nextInt() - 1; n     if (type == 1) update(0, 0, OSN, lst[v], rst[v], i + 1); n     if (type == 2) update(lst[v], i + 1); n     if (type == 3) printf( %d n , check(v)); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 2e5 + 5; n const long long inf = (1ll << 60); n int n, m, k, cnt, a, b, v[N], p[N], ta[N], tb[N], td[N], ca[N << 2], cb[N << 2], n     cd[N << 2], flag[N], pa, pb, pc, pd; n long long A[N], B[N], C[N], D[N], co[N], sum, ans = inf; n int read() { n   int x = 0, f = 1; n   char s; n   while ((s = getchar()) >  9  || s <  0 ) n     if (s ==  - ) f = -1; n   while (s <=  9  && s >=  0 ) n     x = (x << 1) + (x << 3) + (s ^ 48), s = getchar(); n   return x * f; n } n bool cmp(const int a, const int b) { return v[a] < v[b]; } n void build(const int o, const int l, const int r) { n   if (l == r) { n     ca[o] = ta[l], cb[o] = tb[l], cd[o] = td[l]; n     return; n   } n   int mid = l + r >> 1; n   build(o << 1, l, mid); n   build(o << 1 | 1, mid + 1, r); n   ca[o] = ca[o << 1 | 1]; n   cb[o] = cb[o << 1 | 1]; n   cd[o] = cd[o << 1 | 1]; n } n long long query(const int o, const int l, const int r, const int need) { n   if (l == r) n     return A[max(need, ca[o])] + B[max(need, cb[o])] + D[cd[o]] - n            co[l] * (max(need, ca[o]) + max(need, cb[o]) + cd[o] + k - need - m); n   int mid = l + r >> 1; n   long long t = n       max(need, ca[o << 1]) + max(need, cb[o << 1]) + cd[o << 1] + k - need; n   if (t >= m) n     return query(o << 1, l, mid, need); n   else n     return query(o << 1 | 1, mid + 1, r, need); n } n int main() { n   n = read(), m = read(), k = read(); n   for (int i = 1; i <= n; ++i) v[i] = read(), p[i] = i; n   sort(p + 1, p + n + 1, cmp); n   for (int i = 1; i <= n; ++i) { n     if (i == 1 || co[cnt] < v[p[i]]) co[++cnt] = v[p[i]]; n     v[p[i]] = cnt; n   } n   a = read(); n   for (int i = 1; i <= a; ++i) b = read(), flag[b] |= 1; n   a = read(); n   for (int i = 1; i <= a; ++i) b = read(), flag[b] |= 2; n   for (int i = 1; i <= n; ++i) { n     if (flag[i] == 0) D[++pd] = co[v[i]], ++td[v[i]]; n     if (flag[i] == 1) A[++pa] = co[v[i]], ++ta[v[i]]; n     if (flag[i] == 2) B[++pb] = co[v[i]], ++tb[v[i]]; n     if (flag[i] == 3) C[++pc] = co[v[i]]; n   } n   sort(A + 1, A + pa + 1); n   sort(B + 1, B + pb + 1); n   sort(C + 1, C + pc + 1); n   sort(D + 1, D + pd + 1); n   for (int i = 1; i <= cnt; ++i) n     ta[i] += ta[i - 1], tb[i] += tb[i - 1], td[i] += td[i - 1]; n   for (int i = 1; i <= pa; ++i) A[i] += A[i - 1]; n   for (int i = 1; i <= pb; ++i) B[i] += B[i - 1]; n   for (int i = 1; i <= pd; ++i) D[i] += D[i - 1]; n   build(1, 0, cnt); n   for (int i = 0; i <= min(m, pc); ++i) { n     sum += C[i]; n     if (min(pa, pb) >= k - i && 2 * k - 2 * i <= m - i && i + pa + pb + pd >= m) n       ans = min(ans, sum + query(1, 0, cnt, k - i)); n   } n   printf( %lld n , ans == inf ? -1 : ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int len, i; n   bool chk = true; n   string str; n   cin >> str; n   char ch =  a ; n   len = str.size(); n   for (i = 0; i < len; i++) { n     if (str[i] > ch) { n       chk = false; n       break; n     } else if (str[i] == ch) n       ch++; n   } n   chk ? printf( YES n ) : printf( NO n ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 4e5 + 10; n long long gi() { n   long long x = 0, o = 1; n   char ch = getchar(); n   while ((ch <  0  || ch >  9 ) && ch !=  - ) ch = getchar(); n   if (ch ==  - ) o = -1, ch = getchar(); n   while (ch >=  0  && ch <=  9 ) x = x * 10 + ch -  0 , ch = getchar(); n   return x * o; n } n int n, tt; n long long m, k, d[N]; n pair<long long, long long> p[N]; n int main() { n   int T = gi(); n   while (T--) { n     m = gi(); n     n = gi(); n     k = gi(); n     for (int i = 1; i <= n; i++) d[i] = gi(); n     d[++n] = m; n     for (int i = n; i; i--) d[i] -= d[i - 1]; n     if (!k) { n       cout << *max_element(d + 1, d + n + 1) - *min_element(d + 1, d + n + 1) n            <<   n ; n       continue; n     } n     long long l = 1, r = 1e15; n     while (l < r) { n       long long mid = (l + r + 1) >> 1, sum = 0; n       for (int i = 1; i <= n; i++) { n         sum += d[i] / mid - 1; n         if (sum >= k) break; n       } n       if (sum >= k) n         l = mid; n       else n         r = mid - 1; n     } n     long long mx = l; n     l = 1, r = 1e15; n     while (l < r) { n       long long mid = (l + r) >> 1, sum = 0; n       for (int i = 1; i <= n; i++) { n         sum += (d[i] + mid - 1) / mid - 1; n         if (sum > k) break; n       } n       if (sum <= k) n         r = mid; n       else n         l = mid + 1; n     } n     long long mn = l; n     tt = 0; n     for (int i = 1; i <= n; i++) { n       if ((d[i] + mn - 1) / mn > d[i] / mx) { n         long long t = d[i] / mx; n         p[++tt].first = d[i] / (t + 1); n         t = (d[i] + mn - 1) / mn; n         if (t == 1) n           p[tt].second = 1e18; n         else n           p[tt].second = (d[i] + t - 2) / (t - 1); n       } n     } n     sort(p + 1, p + tt + 1); n     long long low = mn, ans = 1e18; n     for (int i = 1, j; i <= tt; i = j + 1) { n       j = i; n       while (j < tt && p[j + 1].first == p[i].first) ++j; n       ans = min(ans, low - p[i].first); n       for (int k = i; k <= j; k++) low = max(low, p[k].second); n     } n     ans = min(ans, low - mx); n     cout << ans <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const double PI = 3.141592653589793238; n long long powmod(long long a, long long b) { n   long long res = 1; n   a %= 1000000007; n   for (; b; b >>= 1) { n     if (b & 1) res = res * a % 1000000007; n     a = a * a % 1000000007; n   } n   return res; n } n char str[200005]; n int lps[200005]; n int kmp() { n   int i = 0, j = 1, m = strlen(str); n   lps[0] = 0; n   while (j < m) { n     while (i && str[i] != str[j]) { n       i = lps[i - 1]; n     } n     if (str[i] == str[j]) ++i; n     lps[j++] = i; n   } n   if (m % (m - i) == 0) return (m - i); n   return m; n } n int main() { n   scanf( %s , str); n   cout << kmp() << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int mod = 1e9 + 7; n const int INF = 1e9; n int n, m; n char mt[1505][1505]; n const int dx[] = {0, 0, 1, -1}, dy[] = {1, -1, 0, 0}; n bool flag = false; n pair<int, int> vis[1505][1505]; n void dfs(int x, int y) { n   int ax = (x % n + n) % n, ay = (y % m + m) % m; n   if (mt[ax][ay] ==  # ) return; n   if (vis[ax][ay] != make_pair(INF, INF) && vis[ax][ay] != make_pair(x, y)) { n     flag = true; n     return; n   } n   if (vis[ax][ay] != make_pair(INF, INF)) return; n   vis[ax][ay] = make_pair(x, y); n   for (int i = 0; i < 4; i++) dfs(x + dx[i], y + dy[i]); n } n int main() { n   ios_base::sync_with_stdio(false); n   cin >> n >> m; n   int sx, sy; n   for (int i = 0; i < n; i++) n     for (int j = 0; j < m; j++) vis[i][j] = make_pair(INF, INF); n   for (int i = 0; i < n; i++) { n     cin >> mt[i]; n     for (int j = 0; j < m; j++) n       if (mt[i][j] ==  S ) sx = i, sy = j; n   } n   dfs(sx, sy), cout << (flag == true ?  Yes  :  No ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct bian { n   int next, point, w; n } b[10000]; n int d[2100][2100], n, x[2100], f[2100], pd[2100], pre[2100], p[2100], len; n long long e[2100][2100]; n int getmin() { n   int num = 2e9, where = 0; n   for (int i = 1; i <= n; i++) n     if (pd[i] == 0 && f[i] < num) { n       num = f[i]; n       where = i; n     } n   return where; n } n void ade(int k1, int k2, int k3) { n   b[++len] = (bian){p[k1], k2, k3}; n   p[k1] = len; n } n void add(int k1, int k2, int k3) { n   ade(k1, k2, k3); n   ade(k2, k1, k3); n } n void dfs(int k1, int k2, long long k3, int k4) { n   e[k4][k1] = k3; n   for (int i = p[k1]; i; i = b[i].next) { n     int j = b[i].point; n     if (j != k2) dfs(j, k1, k3 + b[i].w, k4); n   } n } n int main() { n   scanf( %d , &n); n   for (int i = 1; i <= n; i++) n     for (int j = 1; j <= n; j++) scanf( %d , &d[i][j]); n   for (int i = 1; i <= n; i++) n     if (d[i][i] != 0) { n       cout <<  NO  << endl; n       return 0; n     } n   for (int i = 1; i <= n; i++) n     for (int j = 1; j <= n; j++) n       if (d[i][j] != d[j][i]) { n         cout <<  NO  << endl; n         return 0; n       } n   memset(f, 0x3f, sizeof f); n   f[1] = 0; n   memset(pd, 0x00, sizeof pd); n   for (int i = 1; i <= n; i++) { n     int k1 = getmin(); n     if (k1 != 1 && f[k1] == 0) { n       cout <<  NO  << endl; n       return 0; n     } n     if (pre[k1]) add(pre[k1], k1, f[k1]); n     pd[k1] = 1; n     for (int j = 1; j <= n; j++) n       if (pd[j] == 0 && d[k1][j] < f[j]) { n         pre[j] = k1; n         f[j] = d[k1][j]; n       } n   } n   for (int i = 1; i <= n; i++) dfs(i, 0, 0, i); n   for (int i = 1; i <= n; i++) n     for (int j = 1; j <= n; j++) n       if (e[i][j] != d[i][j]) { n         cout <<  NO  << endl; n         return 0; n       } n   cout <<  YES  << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long n, m, mn, mx, l, t, mn2 = INT_MAX, mx2 = INT_MIN; n set<long long> q; n int main() { n   cin >> n >> m >> mn >> mx; n   long long a[m]; n   for (int i = 0; i < m; i++) { n     cin >> a[i]; n     if (a[i] < mn2) mn2 = a[i]; n     if (a[i] > mx2) mx2 = a[i]; n     if (a[i] >= mn && a[i] <= mx) t++; n   } n   if (t == m && m <= n) { n     if (mn2 != mn && mx2 != mx && t + 2 <= n) n       cout <<  Correct ; n     else if ((mn2 == mn && t + 1 <= n) || (mx2 == mx && t + 1 <= n)) n       cout <<  Correct ; n     else if (mn2 == mn && mx2 == mx && t <= n) n       cout <<  Correct ; n     else n       cout <<  Incorrect ; n   } else n     cout <<  Incorrect ; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   { n     ios_base::sync_with_stdio(false); n     cin.tie(0); n   }; n   int t; n   cin >> t; n   while (t--) { n     int n, m; n     cin >> n >> m; n     vector<int> matching; n     vector<int> vis(3 * n + 1, 0); n     for (int i = 1; i <= m; i++) { n       int u, v; n       cin >> u >> v; n       if (!vis[u] and !vis[v]) { n         matching.push_back(i); n         vis[u] = vis[v] = 1; n       } n     } n     if (matching.size() >= n) { n       cout <<  Matching  << endl; n       for (int i = 0; i < n; i++) cout << matching[i] <<    ; n       cout << endl; n     } else { n       cout <<  IndSet  << endl; n       int seen = 0; n       for (int i = 1; i <= 3 * n and seen < n; i++) { n         if (!vis[i]) { n           cout << i <<    ; n           seen++; n         } n       } n       cout << endl; n     } n   } n } n


#include <bits/stdc++.h> n char x[200005]; n int dp[200005], maxdp[26]; n int main() { n   int n; n   scanf( %d , &n); n   scanf( %s , x + 1); n   for (int i = 1; i <= n; i++) { n     x[i] = x[i] -  a ; n     dp[i] = 1; n   } n   maxdp[x[1]] = 1; n   for (int i = 2; i <= n; i++) { n     for (int j = x[i] + 1; j < 26; j++) { n       dp[i] = dp[i] < maxdp[j] + 1 ? maxdp[j] + 1 : dp[i]; n     } n     if (dp[i] > maxdp[x[i]]) maxdp[x[i]] = dp[i]; n   } n   int ans = -1; n   for (int i = 1; i <= n; i++) { n     if (dp[i] > ans) ans = dp[i]; n   } n   printf( %d n , ans); n   for (int i = 1; i <= n; i++) printf( %d  , dp[i]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int t, m; n   cin >> t >> m; n   vector<int> A; n   A.resize(m); n   int c = 0; n   for (int i = 0; i < m; ++i) A[i] = 0; n   for (int i = 0; i < t; ++i) { n     string z; n     int tmp; n     cin >> z; n     if (z[0] ==  d ) { n       for (int j = 0; j < m - 1; ++j) { n         for (int k = m - 1; k > j; k--) { n           if (A[k] != 0 && 0 == A[k - 1]) { n             int tmp2 = A[k]; n             A[k] = A[k - 1]; n             A[k - 1] = tmp2; n           } n         } n       } n     } else if (z[0] ==  e ) { n       cin >> tmp; n       int j = 0; n       while (j < m && A[j] != tmp) j++; n       if (j == m || tmp == 0) cout <<  ILLEGAL_ERASE_ARGUMENT  << endl; n       while (j < m && A[j] == tmp) { n         A[j] = 0; n         j++; n       } n     } else { n       cin >> tmp; n       int empty = 0; n       int j = 0; n       while (j < m && empty < tmp) { n         if (A[j] == 0) n           empty++; n         else n           empty = 0; n         j++; n       } n       if (j == m && empty < tmp) n         cout <<  NULL  << endl; n       else { n         c++; n         for (int k = j - 1; k > j - 1 - empty; --k) { n           A[k] = c; n         } n         cout << c << endl; n       } n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n inline int Rint() { n   int x; n   scanf( %d , &x); n   return x; n } n const int MOD = 1e9 + 7; n const int MAXN = 1e5 + 10; n int alll(char* str_a, char* str_b, int n) { n   int ret = 1; n   for (int i = 0; i < n; ++i) { n     if (str_a[i] ==  ? ) ret = ret * 10ll % MOD; n     if (str_b[i] ==  ? ) ret = ret * 10ll % MOD; n   } n   return ret; n } n int upp(char* str_a, char* str_b, int n) { n   int ans = 1; n   for (int i = 0; i < n; ++i) { n     if (str_a[i] == str_b[i]) { n       if (str_a[i] !=  ? ) n         ; n       else n         ans = ans * 55ll % MOD; n     } else if (str_a[i] ==  ? ) { n       ans = ans * (long long)( 9  - str_b[i] + 1) % MOD; n     } else if (str_b[i] ==  ? ) { n       ans = ans * (long long)(str_a[i] -  0  + 1) % MOD; n     } else if (str_a[i] >= str_b[i]) { n     } else n       return 0; n   } n   return ans; n } n int equ(char* str_a, char* str_b, int n) { n   int ret = 1; n   for (int i = 0; i < n; ++i) n     if (str_a[i] ==  ?  || str_b[i] ==  ? ) { n       if (str_a[i] ==  ?  && str_b[i] ==  ? ) ret = ret * 10ll % MOD; n     } else if (str_a[i] != str_b[i]) n       return 0; n   return ret; n } n int n; n char str_a[MAXN], str_b[MAXN]; n int main() { n   while (cin >> n) { n     scanf( %s , str_a); n     scanf( %s , str_b); n     int all = alll(str_a, str_b, n); n     int mod1 = upp(str_a, str_b, n); n     int mod2 = upp(str_b, str_a, n); n     int mod3 = equ(str_a, str_b, n); n     int ans = ((mod1 + mod2 - mod3) % MOD + MOD) % MOD; n     ans = (all - ans + MOD) % MOD; n     printf( %d n , ans); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct ip { n   long long a, b, c, d, m; n }; n ip getip(long long num, int mm) { n   return {(num & (255 << 24)) >> 24, (num & (255 << 16)) >> 16, n           (num & (255 << 8)) >> 8, num & 255, mm}; n } n vector<ip> numtoip(pair<long long, long long> p) { n   long long s = p.first, e = p.second; n   vector<ip> ans; n   while (s < e) { n     int k = (!s) ? 32 : __builtin_ctzll(s); n     while (s + (1ll << k) - 1 >= e) k--; n     ans.push_back(getip(s, 32 - k)); n     s += 1ll << k; n   } n   return ans; n } n pair<long long, long long> iptonum(ip x) { n   long long n = n       x.a * (1 << 24) + x.b * (1 << 16) + x.c * (1 << 8) + x.d * (1 << 0); n   bitset<32> low(n), high(n); n   for (int i = 0, ThxDem = 32 - x.m; i < ThxDem; ++i) low[i] = 0, high[i] = 1; n   return {low.to_ullong(), high.to_ullong() + 1}; n } n struct disjoint_intervals { n   set<pair<long long, long long> > s; n   void insert(pair<long long, long long> v) { n     if (v.first >= v.second) return; n     auto at = s.lower_bound(v); n     auto it = at; n     if (at != s.begin() && (--at)->second >= v.first) v.first = at->first, --it; n     for (; it != s.end() && it->first <= v.second; s.erase(it++)) n       v.second = max(v.second, it->second); n     s.insert(v); n   } n   bool intersect(pair<long long, long long> v) { n     auto it = s.upper_bound({v.first, -2}); n     return (it != s.end() && it->first < v.second) || n            (it != s.begin() && prev(it)->second > v.first); n   } n }; n disjoint_intervals w, b; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   int n; n   cin >> n; n   for (int i = 0, ThxDem = n; i < ThxDem; ++i) { n     string s; n     cin >> s; n     s +=  . ; n     int now = 0, white = s[0] ==  + ; n     vector<int> v; n     for (int i = 1, ThxDem = ((int)(s).size()); i < ThxDem; ++i) { n       if (s[i] >=  0  && s[i] <=  9 ) n         now = 10 * now + s[i] -  0 ; n       else n         v.push_back(now), now = 0; n     } n     if (v.size() < 5) v.push_back(32); n     auto ran = iptonum({v[0], v[1], v[2], v[3], v[4]}); n     if (white) n       w.insert(ran); n     else n       b.insert(ran); n   } n   for (auto x : w.s) { n     if (b.intersect(x)) { n       cout << -1 << endl; n       return 0; n     } n   } n   vector<pair<long long, long long> > good = {{-1, 0}}, ans; n   for (auto x : w.s) good.push_back(x); n   good.push_back({1ll << 32, (1ll << 32) + 1ll}); n   for (int i = 1, ThxDem = ((int)(good).size()); i < ThxDem; ++i) n     ans.push_back({good[i - 1].second, good[i].first}); n   vector<ip> res; n   for (auto xx : ans) { n     vector<ip> x = numtoip(xx); n     for (auto aux : x) { n       auto rr = iptonum(aux); n       if (b.intersect(rr)) res.push_back(aux); n     } n   } n   cout << res.size() << endl; n   for (auto x : res) n     cout << x.a <<  .  << x.b <<  .  << x.c <<  .  << x.d <<  /  << x.m << endl; n } n


#include <bits/stdc++.h> n using namespace std; n string imp =  Impossible n ; n mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count()); n clock_t startTime; n double getCurrentTime() { n   return (double)(clock() - startTime) / CLOCKS_PER_SEC; n } n const int N = 1e5 + 1e3; n long long MOD = 1e9 + 7; n vector<int> aa[N]; n int a[N], b[N], c[N]; n int n, m, k, i, j; n bool d[N]; n string s; n bool iscons(char c) { n   if (c !=  a  && c !=  e  && c !=  i  && c !=  o  && c !=  u ) return true; n   return false; n } n int main() { n   startTime = clock(); n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   int tests = 1; n   while (tests--) { n     cin >> s; n     int ns = s.size(); n     if (ns < 3) { n       cout << s; n       return 0; n     } n     int cons = 0; n     if (iscons(s[1])) { n       if (iscons(s[0])) n         cons += 2; n       else n         cons++; n     } n     cout << s[0] << s[1]; n     for (int i = 2; i < ns; i++) { n       if (iscons(s[i])) { n         cons++; n         if (cons == 3) { n           if (s[i] == s[i - 1] && s[i - 1] == s[i - 2]) cons = 2; n         } n       } else n         cons = 0; n       if (cons == 3) { n         cout <<    ; n         cons = 1; n       } n       cout << s[i]; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n  n using namespace std; n void mex(string &s) n { n     s.back()++; n     for(int i=s.length()-1;i>=1;i--) n     { n         if(s[i]> z ) n         { n             s[i]= a ; n             s[i-1]++; n         } n          n     } n     if(s[0]> z ) n         { n             s.push_back( a ); n             s[0]= a ; n         } n } n  n int main() n { n     int t;cin>>t; n     while(t--) n     { n         int n;cin>>n; n         string s;cin>>s; n         string k= a ; n         while(s.find(k,0)!=string::npos) n         { n             mex(k); n         } n         cout<<k<<endl; n     } n } n


#include <bits/stdc++.h> n using namespace std; n int a[105]; n int b[105]; n int main() { n   int n, m, i, j; n   scanf( %d%d , &n, &m); n   for (i = 1; i <= n; i++) scanf( %d%d , &a[i], &b[i]); n   int con = 0; n   for (i = 1; i <= 10000; i++) { n     int s = 0; n     for (j = 1; j <= n; j++) s += (i * a[j] / b[j] + 1); n     if (s == m) con++; n   } n   printf( %d n , con); n } n


#include <bits/stdc++.h> n #pragma GCC optimize( O3 ) n #pragma GCC target( sse4 ) n using namespace std; n int n, m, k, second; n int a[500005]; n int b[500005]; n int cnt[500005]; n int rbd[500005]; n int stat[500005]; n int app[500005]; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cin >> m >> k >> n >> second; n   for (int i = (1); i <= (signed)(m); i++) { n     cin >> a[i]; n   } n   for (int i = (1); i <= (signed)(second); i++) { n     int x; n     cin >> x; n     b[x]++; n   } n   int rb = 0; n   int sat = 0; n   for (int i = (1); i <= (signed)(m); i++) { n     while (rb <= m && sat < second) { n       rb++; n       int c = a[rb]; n       if (b[c] > cnt[c]) { n         sat++; n       } n       cnt[c]++; n     } n     rbd[i] = max(rb, i + k - 1); n     int c = a[i]; n     if (b[c] >= cnt[c]) sat--; n     cnt[c]--; n   } n   int dc = 0; n   for (int i = (1); i <= (signed)(m); i++) { n     if (rbd[i] == m + 1) break; n     int bef = (i - 1) / k; n     int aft = (m - rbd[i]) / k; n     if (bef + aft + 1 >= n) { n       for (int j = (i); j <= (signed)(rbd[i]); j++) { n         int c = a[j]; n         if (b[c] > app[c]) { n           stat[j] = 1; n         } n         app[c]++; n       } n       int added = second; n       for (int j = (i); j <= (signed)(rbd[i]); j++) { n         if (added < k && stat[j] == 0) { n           added++; n           stat[j] = 1; n         } else if (added == k && stat[j] == 0) { n           stat[j] = -1; n           dc++; n         } n       } n       int td = (i - 1) % k; n       if (bef >= n) { n         td += (bef + 1 - n) * k; n       } n       for (int i = (1); i <= (signed)(td); i++) { n         stat[i] = -1; n         dc++; n       } n       cout << dc <<   n ; n       for (int i = (1); i <= (signed)(m); i++) { n         if (stat[i] == -1) cout << i <<    ; n       } n       return 0; n     } n   } n   cout <<  -1 n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long mod = 1e9 + 7; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   long long n; n   string t, s; n   cin >> s >> t; n   n = t.size(); n   vector<long long> pi(n + 1, 0); n   for (long long i = (long long)1; i != (long long)n; i = i + 1) { n     long long j = pi[i - 1]; n     while (j >= 0 && t[i] != t[j]) { n       j = j > 0 ? pi[j - 1] : -1; n     } n     pi[i] = j + 1; n   } n   vector<long long> dp1(s.size() + 1, 0), dp2(s.size() + 1, 0), n       dp3(s.size() + 1, 0); n   vector<long long> pos; n   long long curpi = 0; n   for (long long i = (long long)0; i != (long long)s.size(); i = i + 1) { n     long long j = curpi < n ? curpi : pi[curpi - 1]; n     while (j >= 0 && s[i] != t[j]) { n       j = j > 0 ? pi[j - 1] : -1; n     } n     curpi = j + 1; n     if (curpi == n) { n       pos.push_back(i); n     } n   } n   if (pos.size() == 0) { n     cout << 0 <<   n ; n     return 0; n   } n   long long j = 0; n   for (long long i = (long long)0; i != (long long)s.size(); i = i + 1) { n     if (j < pos.size() && i == pos[j]) { n       j++; n       dp1[i + 1] = (dp3[i + 1 - n] + i + 2 - n) % mod; n       dp2[i + 1] = (dp1[i + 1] + dp2[i]) % mod; n     } else { n       dp1[i + 1] = dp1[i]; n       dp2[i + 1] = (dp2[i] + dp1[i + 1]) % mod; n     } n     dp3[i + 1] = (dp3[i] + dp2[i + 1]) % mod; n   } n   cout << dp2[s.size()] <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n int a[101], b[101]; n int main() { n   int n, m; n   cin >> n; n   for (int i = 0; i < n; ++i) cin >> a[i]; n   sort(a, a + n); n   cin >> m; n   for (int i = 0; i < m; ++i) cin >> b[i]; n   sort(b, b + m); n   cout << a[n - 1] <<     << b[m - 1] << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n char name[105][20]; n long long type[105], w[105], h[105], border[105], space[105]; n int link[105][105]; n bool caled[105]; n int n, m; n void cal(int x) { n   if (caled[x]) return; n   caled[x] = true; n   if (link[x][0] == 0) return; n   int i, k; n   if (type[x] == 2) { n     for (i = 1; i <= link[x][0]; i++) { n       k = link[x][i]; n       cal(k); n       w[x] += w[k]; n       if (h[k] > h[x]) h[x] = h[k]; n     } n     w[x] += border[x] * 2; n     h[x] += border[x] * 2; n     w[x] += space[x] * (link[x][0] - 1); n     return; n   } n   for (i = 1; i <= link[x][0]; i++) { n     k = link[x][i]; n     cal(k); n     h[x] += h[k]; n     if (w[k] > w[x]) w[x] = w[k]; n   } n   w[x] += border[x] * 2; n   h[x] += border[x] * 2; n   h[x] += space[x] * (link[x][0] - 1); n } n void exchange(int x, int y) { n   int i, k; n   char c; n   k = max(strlen(name[x]), strlen(name[y])); n   for (i = 0; i <= k; i++) { n     c = name[x][i]; n     name[x][i] = name[y][i]; n     name[y][i] = c; n   } n } n int main() { n   int i, j, k, l, t; n   char s[1000], tmp[1000]; n   scanf( %d , &m); n   n = 0; n   for (i = 1; i <= m; i++) { n     scanf( %s , s); n     if (!strcmp(s,  Widget )) { n       scanf( %s , s); n       n++; n       type[n] = 1; n       caled[n] = true; n       j = 0; n       while (s[j] !=  ( ) { n         name[n][j] = s[j]; n         j++; n       } n       name[n][j] =   0 ; n       j++; n       w[n] = 0; n       h[n] = 0; n       while (s[j] !=  , ) { n         w[n] = w[n] * 10 + (s[j] -  0 ); n         j++; n       } n       j++; n       while (s[j] !=  ) ) { n         h[n] = h[n] * 10 + (s[j] -  0 ); n         j++; n       } n       continue; n     } n     if ((!strcmp(s,  HBox )) || (!strcmp(s,  VBox ))) { n       n++; n       caled[n] = false; n       w[n] = 0; n       h[n] = 0; n       border[n] = 0; n       space[n] = 0; n       link[n][0] = 0; n       if (!strcmp(s,  HBox )) n         type[n] = 2; n       else n         type[n] = 3; n       scanf( %s , s); n       strcpy(name[n], s); n       continue; n     } n     j = 0; n     while (s[j] !=  . ) { n       tmp[j] = s[j]; n       j++; n     } n     tmp[j] =   0 ; n     j++; n     k = 1; n     while (strcmp(tmp, name[k]) != 0) k++; n     t = 0; n     while (s[j] !=  ( ) { n       tmp[t] = s[j]; n       j++; n       t++; n     } n     j++; n     tmp[t] =   0 ; n     if (!strcmp(tmp,  pack )) { n       t = 0; n       while (s[j] !=  ) ) { n         tmp[t] = s[j]; n         t++; n         j++; n       } n       tmp[t] =   0 ; n       l = 1; n       while (strcmp(tmp, name[l]) != 0) l++; n       if (k == l) continue; n       link[k][0]++; n       link[k][link[k][0]] = l; n       continue; n     } n     if (!strcmp(tmp,  set_border )) { n       border[k] = 0; n       while (s[j] !=  ) ) { n         border[k] = border[k] * 10 + (s[j] -  0 ); n         j++; n       } n       continue; n     } n     if (!strcmp(tmp,  set_spacing )) { n       space[k] = 0; n       while (s[j] !=  ) ) { n         space[k] = space[k] * 10 + (s[j] -  0 ); n         j++; n       } n       continue; n     } n   } n   for (i = 1; i <= n; i++) cal(i); n   for (i = 1; i <= n - 1; i++) n     for (j = i + 1; j <= n; j++) n       if (strcmp(name[i], name[j]) > 0) { n         swap(w[i], w[j]); n         swap(h[i], h[j]); n         exchange(i, j); n       } n   for (i = 1; i <= n; i++) printf( %s %I64d %I64d n , name[i], w[i], h[i]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAX = 505; n int dx[] = {0, 0, 1, -1}; n int dy[] = {1, -1, 0, 0}; n char dc[] = { R ,  L ,  D ,  U }; n string ans; n int n; n bool grid[MAX][MAX], vis[MAX][MAX]; n int dp[MAX][MAX][4]; n bool valid(int x, int y, int dir) { n   return dp[x][y][dir] = ~dp[x][y][dir]                 ? dp[x][y][dir] n                          : (!x || !y || x > n || y > n) ? 0 n                          : grid[x][y]                   ? 1 n                                       : valid(x + dx[dir], y + dy[dir], dir); n } n void dfs1(int x, int y) { n   vis[x][y] = 1; n   if (!grid[x][y]) ans +=  1 , grid[x][y] = 1; n   for (int i = 0; i < 4; ++i) { n     int nx = x + dx[i], ny = y + dy[i]; n     if (vis[nx][ny] || !valid(nx, ny, i)) continue; n     ans += dc[i]; n     dfs1(nx, ny); n     ans += dc[i ^ 1]; n   } n } n void dfs0(int x, int y) { n   grid[x][y] = 0; n   for (int i = 0; i < 4; ++i) { n     int nx = x + dx[i], ny = y + dy[i]; n     if (!grid[nx][ny]) continue; n     ans += dc[i]; n     dfs0(nx, ny); n     ans += dc[i ^ 1]; n   } n   ans +=  2 ; n } n int main() { n   int sx, sy; n   memset(dp, -1, sizeof dp); n   cin >> n >> sx >> sy; n   for (int i = 1; i <= n; ++i) { n     vis[0][i] = vis[i][0] = vis[0][n + 1] = vis[n + 1][0] = 1; n     for (int j = 1; j <= n; ++j) scanf( %d , grid[i] + j); n   } n   dfs1(sx, sy); n   dfs0(sx, sy); n   for (int i = 1; i <= n; ++i) n     for (int j = 1; j <= n; ++j) n       if (grid[i][j]) return puts( NO ), 0; n   puts( YES ); n   puts(ans.c_str()); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct Grass { n   int sizee; n   char dir; n } a[100005]; n int n; n bool flag[100005]; n int main() { n   cin >> n; n   for (int i = 1; i <= n; i++) { n     cin >> a[i].dir; n   } n   for (int i = 1; i <= n; i++) { n     cin >> a[i].sizee; n   } n   int nowx = 1; n   flag[nowx] = 1; n   while (true) { n     if (a[nowx].dir ==  > ) { n       nowx += a[nowx].sizee; n     } else { n       nowx -= a[nowx].sizee; n     } n     if (nowx > n || nowx < 1) { n       cout <<  FINITE ; n       break; n     } n     if (flag[nowx] == 1) { n       cout <<  INFINITE ; n       break; n     } n     flag[nowx] = 1; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, a[200000], x, y, ans; n multimap<int, int> M; n pair<int, int> P; n int main() { n   scanf( %d , &n); n   for (int i = 1; i <= (n); ++i) scanf( %d , &a[i]); n   scanf( %d%d , &x, &y); n   sort(a + 1, a + n + 1); n   n = unique(a + 1, a + n + 1) - a - 1; n   for (int i = 1; i <= (n); ++i) n     if (x - x % a[i] >= y) M.insert(make_pair(x - x % a[i], a[i])); n   while (x > y) { n     if (M.empty()) { n       ans += x - y; n       break; n     } n     multimap<int, int>::iterator it = M.begin(); n     if (it->first < x) n       x = it->first; n     else n       --x; n     ++ans; n     for (it = M.end(); !M.empty() && (--it)->first > x; it = M.end()) { n       P.first = x - x % it->second; n       P.second = it->second; n       M.erase(it); n       if (P.first >= y) M.insert(P); n     } n   } n   printf( %d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<pair<int, pair<int, int>>> adj[(long)(1e5)]; n int sub[(long)(1e5)]; n int w[(long)(1e5)]; n int c[(long)(1e5)]; n int dfs(int r, int p) { n   int cnt = 0; n   for (int i = 0; i < adj[r].size(); i++) { n     if (adj[r][i].first != p) { n       w[adj[r][i].first] = adj[r][i].second.first; n       c[adj[r][i].first] = adj[r][i].second.second; n       cnt += dfs(adj[r][i].first, r); n     } n   } n   if (!cnt) cnt = 1; n   sub[r] = cnt; n   return cnt; n } n void solve() { n   int n; n   long long s; n   scanf( %d%lld , &n, &s); n   for (int i = 0; i < n; i++) adj[i].clear(); n   for (int i = 0; i < n - 1; i++) { n     int u, v, ww, cc; n     scanf( %d%d%d%d , &u, &v, &ww, &cc); n     adj[u - 1].push_back({v - 1, {ww, cc}}); n     adj[v - 1].push_back({u - 1, {ww, cc}}); n   } n   for (int i = 0; i < n; i++) sub[i] = 0; n   dfs(0, -1); n   vector<long long> vs[2]; n   for (int k = 0; k < 2; k++) { n     long long sum = 0; n     set<pair<long long, int>> di; n     for (int i = 1; i < n; i++) { n       if (c[i] == k + 1) { n         di.insert({(1ll * sub[i] * w[i] - 1ll * sub[i] * (w[i] / 2)), i}); n         sum += 1ll * sub[i] * w[i]; n       } n     } n     vs[k].push_back(sum); n     while (sum > 0) { n       int i = di.rbegin()->second; n       long long decr = di.rbegin()->first; n       di.erase(prev(di.end())); n       sum -= decr; n       w[i] = w[i] / 2; n       di.insert({(1ll * sub[i] * w[i] - 1ll * sub[i] * (w[i] / 2)), i}); n       vs[k].push_back(sum); n     } n   } n   int flip = 0; n   if (vs[0].size() > vs[1].size()) { n     swap(vs[1], vs[0]); n     flip = 1; n   } n   int ans = 1e9; n   for (int i = 0; i < vs[0].size(); i++) { n     int lo = 0, hi = vs[1].size() - 1; n     while (lo <= hi) { n       int mid = (lo + hi) / 2; n       if (vs[0][i] + vs[1][mid] <= s) n         hi = mid - 1; n       else n         lo = mid + 1; n     } n     if (vs[0][i] + vs[1][lo] <= s) n       ans = min(ans, i * (1 + flip) + lo * (2 - flip)); n   } n   printf( %d n , ans); n } n int main() { n   int tc; n   scanf( %d , &tc); n   while (tc--) { n     solve(); n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 2e5 + 10; n struct Edge { n   int to, nxt; n } e[N << 1]; n int h[N], idx, a[N]; n void Ins(int a, int b) { n   e[++idx].to = b; n   e[idx].nxt = h[a]; n   h[a] = idx; n } n long long dis[N], siz[N]; n void dfs(int u, int fa) { n   siz[u] = a[u]; n   for (int i = h[u]; i; i = e[i].nxt) { n     int v = e[i].to; n     if (v == fa) continue; n     dfs(v, u); n     siz[u] += siz[v]; n     dis[1] += siz[v]; n   } n } n long long sum; n void dfs1(int u, int fa) { n   for (int i = h[u]; i; i = e[i].nxt) { n     int v = e[i].to; n     if (v == fa) continue; n     dis[v] = dis[u] + sum - 2ll * siz[v]; n     dfs1(v, u); n   } n } n int main() { n   int n; n   scanf( %d , &n); n   for (int i = 1; i <= n; i++) scanf( %d , &a[i]), sum += a[i]; n   for (int i = 2; i <= n; i++) { n     int x, y; n     scanf( %d%d , &x, &y); n     Ins(x, y); n     Ins(y, x); n   } n   dfs(1, 0); n   dfs1(1, 0); n   long long res = 0; n   for (int i = 1; i <= n; i++) res = max(res, dis[i]); n   printf( %lld n , res); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, x, y, i, j; n   int l = 0, t = 0, m = 0, max = 0; n   cin >> n; n   cin >> i >> j; n   l = j; n   while (t < n) { n     t++; n     cin >> x >> y; n     m = j - x + y; n     if (m > max) { n       max = m; n     } n     j = m; n   } n   if (max == 0 && l > 0 || l > max) n     cout << l; n   else n     cout << max; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void solve() { n   long long n, t, a, x, cur = 0, sum = 0, sz = 1; n   cin >> n; n   vector<long long> arr(n + 20), ch(n + 20); n   for (int i = 0; i < n; i++) { n     cin >> t; n     if (t == 1) { n       cin >> a >> x; n       ch[a - 1] += x; n       sum += a * x; n     } else if (t == 2) { n       cin >> x; n       sum += x; n       arr[++cur] = x; n     } else { n       sum -= (arr[cur] + ch[cur]); n       ch[cur - 1] += ch[cur]; n       ch[cur] = 0; n       arr[cur] = 0; n       cur--; n     } n     cout << (sum * 1.0) / (1.0 * (cur + 1)) <<   n ; n   } n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   cout << fixed << setprecision(12); n   solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int INF = (int)1e9 + 1; n const int MAXN = (int)5001; n int solve(vector<pair<int, int> > sg, int pos) { n   sg.erase(sg.begin() + pos); n   int mx = sg[0].first, mn = sg[0].second; n   for (int i = (1); i < (int)(sg.size()); i++) { n     if (sg[i].first > mn || sg[i].second < mx) return 0; n     mx = max(mx, sg[i].first); n     mn = min(mn, sg[i].second); n   } n   return abs(mx - mn); n } n bool comp1(const pair<int, int> &a, const pair<int, int> &b) { n   return (a.first == b.first) ? (a.second - a.first > b.second - b.first) n                               : (a.first < b.first); n } n bool comp2(const pair<int, int> &a, const pair<int, int> &b) { n   return (a.second == b.second) ? (a.second - a.first < b.second - b.first) n                                 : (a.second < b.second); n } n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   int n, x, y; n   cin >> n; n   vector<pair<int, int> > sgm; n   for (int i = (0); i < (int)(n); i++) { n     cin >> x >> y; n     sgm.push_back(make_pair(x, y)); n   } n   sort((sgm).begin(), (sgm).end(), comp1); n   x = solve(sgm, n - 1); n   sort((sgm).begin(), (sgm).end(), comp2); n   cout << max(x, solve(sgm, 0)); n } n


#include <bits/stdc++.h> n using namespace std; n int n, m; n string s; n set<string> st; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   while (cin >> s) { n     st.clear(); n     for (int i = 0; i < s.size(); i++) { n       string s1 =   ; n       for (int j = 0; j < s.size(); j++) s1 += s[(i + j) % s.size()]; n       st.insert(s1); n     } n     cout << st.size() << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<vector<int>> moves = {{0, 1}, {1, 0}, {-1, 0}, {0, -1}}; n bool isValid(int a, int b, int x, int y) { n   return (a >= 0 && a < x) && (b >= 0 && b < y); n } n bool isPath(int n, int m, vector<string>& mat) { n   bool isPossible = false; n   int cnt = 0; n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < m; j++) { n       if (mat[i][j] ==  G ) cnt++; n     } n   } n   if (mat[n - 1][m - 1] ==  #  && cnt > 0) return isPossible; n   vector<vector<bool>> vis(n, vector<bool>(m)); n   queue<pair<int, int>> q; n   q.push({n - 1, m - 1}); n   vis[n - 1][m - 1] = true; n   while (!q.empty()) { n     pair<int, int> curr = q.front(); n     q.pop(); n     if (mat[curr.first][curr.second] ==  G ) { n       cnt--; n     } n     for (int i = 0; i < 4; i++) { n       int nx = curr.first + moves[i][0]; n       int ny = curr.second + moves[i][1]; n       if (isValid(nx, ny, n, m) && mat[nx][ny] !=  #  && !vis[nx][ny]) { n         q.push({nx, ny}); n         vis[nx][ny] = true; n       } n     } n   } n   isPossible = cnt <= 0 ? true : false; n   return isPossible; n } n int main() { n   int t; n   cin >> t; n   while (t--) { n     int n, m; n     cin >> n >> m; n     vector<string> mat(n); n     for (int i = 0; i < n; i++) { n       cin >> mat[i]; n     } n     bool isPossible = true; n     for (int i = 0; i < n; i++) { n       for (int j = 0; j < m; j++) { n         if (mat[i][j] ==  B ) { n           for (int k = 0; k < 4; k++) { n             int nx = i + moves[k][0]; n             int ny = j + moves[k][1]; n             if (isValid(nx, ny, n, m)) { n               if (mat[nx][ny] ==  G ) { n                 isPossible = false; n                 break; n               } else if (mat[nx][ny] ==  . ) { n                 mat[nx][ny] =  # ; n               } n             } n           } n         } n         if (!isPossible) break; n       } n       if (!isPossible) break; n     } n     if (!isPossible) { n       cout <<  No  << endl; n     } else { n       isPossible = isPath(n, m, mat); n       if (!isPossible) n         cout <<  No  << endl; n       else n         cout <<  Yes  << endl; n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n int t = 0; n int d[300000]; n int par[300000]; n int t1[300000]; n int t2[300000]; n void dfs(int s, vector<vector<int> > &ed, int y) { n   d[s] = y; n   t1[s] = t++; n   for (int i = 0; i < ed[s].size(); i++) { n     if (ed[s][i] != par[s]) { n       par[ed[s][i]] = s; n       dfs(ed[s][i], ed, y + 1); n     } n   } n   t2[s] = t++; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   int n, m; n   cin >> n >> m; n   vector<vector<int> > v(n); n   int f, s; n   for (int i = 1; i < n; i++) { n     cin >> f >> s; n     f--; n     s--; n     v[f].push_back(s); n     v[s].push_back(f); n   } n   for (int i = 0; i < n; i++) { n     sort(v[i].begin(), v[i].end()); n   } n   par[0] = -1; n   dfs(0, v, 0); n   for (int i = 0; i < m; i++) { n     cin >> f; n     vector<int> h(f); n     for (int j = 0; j < f; j++) { n       cin >> h[j]; n       h[j]--; n     } n     int u = 0; n     for (int j = 0; j < f; j++) { n       if (d[h[j]] > d[u]) { n         u = h[j]; n       } n     } n     int flag = 1; n     for (int j = 0; j < f; j++) { n       if (!((t1[h[j]] <= t1[u] && t2[h[j]] >= t2[u]) || n             (t1[par[h[j]]] <= t1[u] && t2[par[h[j]]] >= t2[u]))) { n         flag = 0; n         break; n       } n     } n     if (flag == 0) { n       cout <<  NO n ; n     } else { n       cout <<  YES n ; n     } n   } n   return 0; n } n


#include<bits/stdc++.h> n using namespace std; n  n typedef long long ll; n  n int q(int l, int r){ n     cout <<  ?   << l <<     << r << endl; n     int idx; cin >> idx; n     return idx; n } n void solve(){ n     int n; n     cin >> n; n     int idx = q(1, n); n     int L = -1, R = -1; n     if(1 < idx) L = q(1, idx); n     if(n > idx) R = q(idx, n); n     if(R == idx){ // 最大值在右 n         int ans = n, l = idx + 1, r = n, mid; n         while(l <= r){ n             mid = l + r >> 1; n             int t = q(idx, mid); n             if(t == idx){ n                 ans = min(ans, mid); n                 r = mid - 1; n             }else{ n                 l = mid + 1; n             } n         } n         cout <<  !   << ans << endl; n     }else{ n         int ans = 1, l = 1, r = idx - 1, mid; n         while(l <= r){ n             mid = l + r >> 1; n             int t = q(mid, idx); n             if(t == idx){ n                 ans = max(ans, mid); n                 l = mid + 1; n             }else{ n                 r = mid - 1; n             } n         } n         cout <<  !   << ans << endl; n     } n } n // 5 1 2 3 4 n int main(){ n     // #define LOCAL n #ifdef LOCAL n     freopen( in.in ,  r , stdin); n     freopen( out.out ,  w , stdout); n #endif   n     int _ = 1; n     // cin >> _; n     for(int i = 1; i <= _; ++i) solve(); n }


#include <bits/stdc++.h> n using namespace std; n long long int a[10000010]; n int main() { n   long long int n; n   long long ans = 0; n   cin >> n; n   for (long long int i = 0; i < n; i++) cin >> a[i]; n   sort(a, a + n); n   ans = a[n - 1] * n; n   long long int c = n; n   for (long long int i = n - 2; i >= 0; i--) { n     ans += (c * a[i]); n     c--; n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n #pragma comment(linker,  /STACK:64000000 ) n using namespace std; n int a[1100][1100], pt[1100], b[] = {25, 18, 15, 12, 10, 8, 6, 4, 2, 1}, ef, n; n map<string, int> m; n string st[1100]; n int f(int aa, int bb) { n   if (pt[aa] != pt[bb]) n     return pt[aa] > pt[bb]; n   else { n     for (int i = 0; i < 60; i++) n       if (a[aa][i] != a[bb][i]) return a[aa][i] > a[bb][i]; n     return 0; n   } n } n int f2(int aa, int bb) { n   if (a[aa][0] != a[bb][0]) n     return a[aa][0] > a[bb][0]; n   else n     return f(aa, bb); n } n int main() { n   int t; n   cin >> t; n   for (int ii = 0; ii < t; ii++) { n     cin >> n; n     for (int i = 0; i < n; i++) { n       string s; n       cin >> s; n       if (m.find(s) == m.end()) { n         m[s] = ef++; n         st[ef - 1] = s; n       } n       a[m[s]][i]++; n       if (i <= 9) pt[m[s]] += b[i]; n     } n   } n   int r[1100]; n   for (int i = 0; i < m.size(); i++) r[i] = i; n   sort(r, r + m.size(), f); n   cout << st[r[0]] << endl; n   sort(r, r + m.size(), f2); n   cout << st[r[0]] << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   char st[500]; n   cin >> st; n   int n = strlen(st); n   int left = 0, right = 0, i = 0; n   for (; i < n; ++i) { n     if (st[i] ==  | ) n       break; n     else n       left++; n   } n   right = n - 1 - i; n   char a[500]; n   cin >> a; n   int x = strlen(a), av = x; n   int he, ch = 0; n   if (left > right) { n     int diff = left - right; n     if (diff <= x) { n       right = left; n       av = av - diff; n       if (av % 2 == 0) { n         for (int j = 0; j < av / 2; ++j) cout << a[j]; n         cout << st; n         for (int j = av / 2; j < x; ++j) cout << a[j]; n       } else { n         cout <<  Impossible n ; n       } n     } else { n       cout <<  Impossible ; n     } n   } else if (right > left) { n     int diff = right - left; n     if (diff <= x) { n       left = right; n       av = av - diff; n       if (av % 2 == 0) { n         for (int j = 0; j < x - av / 2; ++j) cout << a[j]; n         cout << st; n         for (int j = x - av / 2; j < x; ++j) cout << a[j]; n       } else { n         cout <<  Impossible n ; n       } n     } else { n       cout <<  Impossible ; n     } n   } else { n     if (x % 2 == 0) { n       for (int j = 0; j < x / 2; ++j) cout << a[j]; n       cout << st; n       for (int j = x / 2; j < x; ++j) cout << a[j]; n     } else n       cout <<  Impossible n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n string inc[6] = { 011112 ,  103231 ,  110121 ,  121011 ,  ,112101 ,  211110 }; n string near[6] = { 1234 ,  0254 ,  0153 ,  4520 ,  3510 ,  4321 }; n int opp[6] = {5, 3, 4, 1, 2, 0}; n char t[7], s[7]; n string t_p; n bool seen[6]; n set<string> se; n void dfs(int x, int v) { n   seen[x] = true; n   t[v] = s[x]; n   if (v == 5) { n     t_p = min((string)t, t_p); n   } else { n     for (int i = 0; i < 6; i++) { n       if (inc[x][i] ==  1  && !seen[i]) { n         dfs(i, v + 1); n       } n     } n   } n   seen[x] = false; n } n int X[4] = {1, 2, 3, 4}, Y[4] = {0, 1, 5, 3}, Z[4] = {0, 2, 5, 4}; n void cycle(int* r) { n   int x = t[r[0]]; n   t[r[0]] = t[r[1]]; n   t[r[1]] = t[r[2]]; n   t[r[2]] = t[r[3]]; n   t[r[3]] = x; n } n int main() { n   t[6] =   0 ; n   while (cin >> s) { n     se.clear(); n     sort(s, s + 6); n     do { n       for (int i = 0; i < 6; i++) t[i] = s[i]; n       t_p =  ZZZZZZ ; n       for (int i = 0; i < 4; i++) { n         for (int j = 0; j < 4; j++) { n           for (int k = 0; k < 4; k++) { n             t_p = min((string)t, t_p); n             cycle(X); n           } n           cycle(Y); n         } n         cycle(Z); n       } n       se.insert(t_p); n     } while (next_permutation(s, s + 6)); n     cout << se.size() << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n long long mo(const long long input, const long long ceil) { n   return input >= ceil ? input % ceil : input; n } n const long long maxn = 5e5 + 10; n long long cnt, cnta, cntb; n long long vis[maxn]; n vector<long long> e[maxn]; n void dfs(long long k, long long t) { n   vis[k] = t; n   cnt++; n   for (auto i : e[k]) n     if (vis[i] != t) dfs(i, t); n } n void solve(long long t) { n   long long n, m, a, b; n   cin >> n >> m >> a >> b; n   for (long long i = 1; i <= n; i++) e[i].clear(); n   for (long long i = 1; i <= m; i++) { n     long long x, y; n     cin >> x >> y; n     e[x].push_back(y); n     e[y].push_back(x); n   } n   vis[a] = t * 2 - 1; n   cnt = 1; n   dfs(b, t * 2 - 1); n   cnta = n - cnt; n   vis[b] = t * 2; n   cnt = 1; n   dfs(a, t * 2); n   cntb = n - cnt; n   cout << cnta * cntb <<   n ; n } n signed main() { n   ios_base::sync_with_stdio(0); n   cin.tie(NULL); n   cout.tie(NULL); n   long long tt = 1; n   cin >> tt; n   for (long long i = 1; i <= tt; i++) solve(i); n   cerr <<   n n nTime :   n        << 1000 * (long double)clock() / (long double)CLOCKS_PER_SEC <<  ms n ; n   ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int ans[10]; n int main() { n   memset(ans, 0, sizeof(ans)); n   int n; n   char c; n   scanf( %d , &n); n   while (n--) { n     scanf(  %c , &c); n     if (c ==  9 ) { n       ans[7]++; n       ans[3] += 2; n       ans[2]++; n     } n     if (c ==  8 ) { n       ans[7]++; n       ans[2] += 3; n     } n     if (c ==  7 ) ans[7]++; n     if (c ==  6 ) { n       ans[5]++; n       ans[3]++; n     } n     if (c ==  5 ) ans[5]++; n     if (c ==  4 ) { n       ans[3]++; n       ans[2] += 2; n     } n     if (c ==  3 ) ans[3]++; n     if (c ==  2 ) ans[2]++; n   } n   for (int i = 9; i > 1; i--) n     for (int j = 0; j < ans[i]; j++) printf( %d , i); n   printf(  n ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n inline long long int __gcd(long long int a, long long int b); n void pdash(int n = 1); n int bitcount(long long int u); n long long int power(long long int x, long long int y); n long long int power(long long int x, long long int y, long long int z); n long long int modInverse(long long int n, long long int p); n long long int nCrF(long long int n, long long int r, long long int p); n void cordinate_compression(vector<int>& v); n void make_unique(vector<int>& vec); n const long long int mod = 1e9 + 7; n void solve() { n   int n, q; n   cin >> n >> q; n   string str; n   cin >> str; n   vector<int> vec(n); n   for (int i = 0; i < n; i++) { n     vec[i] = (str[i] -  0 ); n   } n   for (int i = 1; i < n; i++) { n     vec[i] += vec[i - 1]; n   } n   while (q--) { n     int zero, one; n     int a, b; n     cin >> a >> b; n     a--; n     b--; n     one = vec[b] - (((a - 1) >= 0) ? vec[a - 1] : 0); n     zero = (b - a) + 1 - one; n     cout << ((power(2, zero, mod) * ((power(2, one, mod) - 1 + mod) % mod)) % n              mod) n          <<   n ; n   } n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   int t = 1; n   while (t--) { n     solve(); n   } n } n inline long long int __gcd(long long int a, long long int b) { n   if (a == 0 || b == 0) { n     return max(a, b); n   } n   long long int tempa, tempb; n   while (1) { n     if (a % b == 0) n       return b; n     else { n       tempa = a; n       tempb = b; n       a = tempb; n       b = tempa % tempb; n     } n   } n } n void pdash(int n) { n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < 30; j++) { n       cout <<  - ; n     } n     cout <<   n ; n   } n } n long long int power(long long int x, long long int y) { n   long long int result = 1; n   while (y > 0) { n     if (y & 1) { n       result = (result * x); n     } n     y = y >> 1; n     x = (x * x); n   } n   return result; n } n long long int power(long long int x, long long int y, long long int z) { n   long long int result = 1; n   x = x % z; n   while (y > 0) { n     if (y & 1) { n       result = (result * x) % z; n     } n     y = y >> 1; n     x = (x * x) % z; n   } n   return result; n } n long long int modInverse(long long int n, long long int p) { n   return power(n, p - 2, p); n } n long long int nCrF(long long int n, long long int r, long long int p) { n   if (r == 0) return 1; n   long long int f[n + 1]; n   f[0] = 1; n   for (long long int i = 1; i <= n; i++) f[i] = f[i - 1] * i % p; n   return (f[n] * modInverse(f[r], p) % p * modInverse(f[n - r], p) % p) % p; n } n void cordinate_compression(vector<int>& v) { n   vector<int> p = v; n   make_unique(p); n   for (int i = 0; i < (int)((v).size()); i++) n     v[i] = (int)(lower_bound(p.begin(), p.end(), v[i]) - p.begin()); n } n void make_unique(vector<int>& vec) { n   sort(vec.begin(), vec.end()); n   vec.erase(unique(vec.begin(), vec.end()), vec.end()); n } n int bitcount(long long int u) { n   int cnt = 0; n   while (u) { n     u = u & (u - 1); n     cnt++; n   } n   return cnt; n } n


#include <bits/stdc++.h> n using namespace std; n const double INF = 1000000000; n struct point { n   long long x, y; n   point() {} n   point(long long x, long long y) : x(x), y(y) {} n   point operator-(point P) { return point(x - P.x, y - P.y); } n }; n double abs(point P) { return sqrt(P.x * P.x + P.y * P.y); } n double dist(point P, point Q) { return abs(Q - P); } n long long dot(point P, point Q) { return P.x * Q.x + P.y * Q.y; } n long long cross(point P, point Q) { return P.x * Q.y - P.y * Q.x; } n int main() { n   cout << fixed << setprecision(20); n   int N, K; n   cin >> N >> K; n   K--; n   vector<point> P(N); n   for (int i = 0; i < N; i++) { n     cin >> P[i].x >> P[i].y; n   } n   if (N == 3) { n     int a = (K + 1) % 3; n     int b = (K + 2) % 3; n     cout << min(dist(P[K], P[a]), dist(P[K], P[b])) + dist(P[a], P[b]) << endl; n   } else { n     mt19937 mt(chrono::steady_clock::now().time_since_epoch().count()); n     int p; n     int x; n     point D; n     while (true) { n       int a = mt() % N; n       int b = mt() % N; n       int c = mt() % N; n       if (a != b && b != c && c != a) { n         if (cross(P[b] - P[a], P[c] - P[a]) == 0) { n           x = a; n           D = P[b] - P[a]; n           for (int i = 0; i < N; i++) { n             if (cross(P[i] - P[a], D) != 0) { n               p = i; n             } n           } n           break; n         } n       } n     } n     vector<pair<long long, int>> L; n     for (int i = 0; i < N; i++) { n       if (i != p) { n         L.push_back(make_pair(dot(P[i] - P[x], D), i)); n       } n     } n     sort(L.begin(), L.end()); n     vector<point> Q(N - 1); n     for (int i = 0; i < N - 1; i++) { n       Q[i] = P[L[i].second]; n     } n     if (p == K) { n       cout << min(dist(P[K], Q[0]), dist(P[K], Q[N - 2])) + dist(Q[0], Q[N - 2]) n            << endl; n     } else { n       int s; n       for (int i = 0; i < N - 1; i++) { n         if (L[i].second == K) { n           s = i; n         } n       } n       double ans = INF; n       ans = min(ans, n                 dist(Q[s], Q[0]) + dist(Q[0], Q[N - 2]) + dist(Q[N - 2], P[p])); n       ans = min(ans, n                 dist(Q[s], Q[N - 2]) + dist(Q[N - 2], Q[0]) + dist(Q[0], P[p])); n       for (int i = s; i < N - 2; i++) { n         double d11 = dist(Q[s], Q[i]) + dist(Q[i], Q[0]) + dist(Q[0], P[p]); n         double d12 = dist(Q[s], Q[0]) + dist(Q[0], Q[i]) + dist(Q[i], P[p]); n         double d1 = min(d11, d12); n         double d2 = min(dist(P[p], Q[i + 1]), dist(P[p], Q[N - 2])) + n                     dist(Q[i + 1], Q[N - 2]); n         ans = min(ans, d1 + d2); n       } n       for (int i = 1; i <= s; i++) { n         double d11 = n             dist(Q[s], Q[i]) + dist(Q[i], Q[N - 2]) + dist(Q[N - 2], P[p]); n         double d12 = n             dist(Q[s], Q[N - 2]) + dist(Q[N - 2], Q[i]) + dist(Q[i], P[p]); n         double d1 = min(d11, d12); n         double d2 = n             min(dist(P[p], Q[i - 1]), dist(P[p], Q[0])) + dist(Q[i - 1], Q[0]); n         ans = min(ans, d1 + d2); n       } n       cout << ans << endl; n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int inf = 0x3f3f3f3f; n const int _inf = 0xc0c0c0c0; n const long long INF = 0x3f3f3f3f3f3f3f3f; n const long long _INF = 0xc0c0c0c0c0c0c0c0; n const long long mod = (int)1e9 + 7; n const int Z = 100; n const int N = Z * Z; n const int M = Z * Z * Z; n int px[N], py[N]; n int head[N], to[M], ct[M], w[M], nt[M]; n int d[N], vis[N]; n int pre[N], id[N]; n int s, t, tot; n void add(int u, int v, int flow, int cost) { n   to[tot] = v; n   ct[tot] = cost; n   w[tot] = flow; n   nt[tot] = head[u]; n   head[u] = tot++; n   to[tot] = u; n   ct[tot] = -cost; n   w[tot] = 0; n   nt[tot] = head[v]; n   head[v] = tot++; n } n void init() { n   memset(head, -1, sizeof(head)); n   tot = 0; n } n int spfa(int s, int t) { n   queue<int> q; n   memset(d, inf, sizeof(d)); n   memset(vis, 0, sizeof(vis)); n   memset(pre, -1, sizeof(pre)); n   d[s] = 0; n   q.push(s); n   while (!q.empty()) { n     int u = q.front(); n     q.pop(); n     vis[u] = 0; n     for (int i = head[u]; ~i; i = nt[i]) { n       if (w[i] > 0 && d[to[i]] > d[u] + ct[i]) { n         d[to[i]] = d[u] + ct[i]; n         pre[to[i]] = u; n         id[to[i]] = i; n         if (!vis[to[i]]) { n           vis[to[i]] = 1; n           q.push(to[i]); n         } n       } n     } n   } n   return d[t] < inf; n } n int MaxFlow(int s, int t) { n   int Mi = inf; n   int sum = 0; n   int tt = 0; n   while (spfa(s, t)) { n     Mi = inf; n     for (int i = t; i != s; i = pre[i]) Mi = min(Mi, w[id[i]]); n     for (int i = t; i != s; i = pre[i]) { n       w[id[i]] -= Mi; n       w[id[i] ^ 1] += Mi; n     } n     tt += Mi; n     sum += d[t] * Mi; n   } n   return sum; n } n int main() { n   init(); n   int n, m, k, c, d; n   scanf( %d%d%d%d%d , &n, &m, &k, &c, &d); n   int s = 0, t = n * 100 + 1; n   for (int i = 1; i <= 100; ++i) add(i, t, 100, 0); n   for (int i = 1, v; i <= k; ++i) { n     scanf( %d , &v); n     add(s, (v - 1) * 100 + 1, 1, 0); n   } n   for (int i = 1; i <= n; ++i) { n     for (int j = 1; j < 100; ++j) { n       add((i - 1) * 100 + j, (i - 1) * 100 + j + 1, 100, c); n     } n   } n   for (int i = 1, u, v; i <= m; ++i) { n     scanf( %d%d , &u, &v); n     for (int j = 1; j < 100; ++j) { n       int idx = (u - 1) * 100 + j; n       int idy = (v - 1) * 100 + j + 1; n       for (int k = 1; k <= 20; ++k) { n         add(idx, idy, 1, c + (k * k - (k - 1) * (k - 1)) * d); n       } n     } n     for (int j = 1; j < 100; ++j) { n       int idx = (v - 1) * 100 + j; n       int idy = (u - 1) * 100 + j + 1; n       for (int k = 1; k <= 20; ++k) { n         add(idx, idy, 1, c + (k * k - (k - 1) * (k - 1)) * d); n       } n     } n   } n   printf( %d n , MaxFlow(s, t)); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int odd_a = 0, odd_b = 0, even_a = 0, even_b = 0; n   int a, b, c; n   cin >> a >> b; n   for (int i = 0; i < a; i++) { n     cin >> c; n     c % 2 ? odd_a++ : even_a++; n   } n   for (int i = 0; i < b; i++) { n     cin >> c; n     c % 2 ? odd_b++ : even_b++; n   } n   int ans = 0; n   ans += min(odd_a, even_b); n   ans += min(even_a, odd_b); n   printf( %d , ans); n } n


#include <bits/stdc++.h> n using namespace std; n using ll = long long; n int const N = 1e5 + 86; n int const M = 1e9 + 7; n int a[N], f[111][N]; n ll ext_gcd(ll a, ll b, ll &x, ll &y) { n   if (!b) { n     x = 1, y = 0; n     return a; n   } n   ll ret = ext_gcd(b, a % b, x, y); n   ll t = x; n   x = y, y = t - a / b * y; n   return ret; n } n ll mod_inv(ll a, ll n) { n   ll x, y; n   ext_gcd(a, n, x, y); n   x %= n; n   if (x < 0) x += n; n   return x; n } n void add(int &x, int y) { n   x += y; n   if (x >= M) x -= M; n } n int main() { n   int n, m; n   scanf( %d%d , &n, &m); n   for (int i = 0; i < n; ++i) scanf( %d , a + i); n   for (int l, r, k; m--;) { n     scanf( %d%d%d , &l, &r, &k), --l, --r; n     add(f[k][l], 1), add(f[k][r + 1], M - 1); n     ll c = 1, m = r - l + 1; n     for (int i = 1; i <= k; ++i, ++m) { n       c = c * m % M; n       c = c * mod_inv(i, M) % M; n       add(f[k - i][r + 1], M - c); n     } n   } n   for (int i = 100; i >= 0; --i) { n     for (int j = 1; j < n; ++j) add(f[i][j], f[i][j - 1]); n     for (int j = 0; i && j < n; ++j) add(f[i - 1][j], f[i][j]); n   } n   for (int i = 0; i < n; ++i) add(a[i], f[0][i]); n   for (int i = 0; i < n; ++i) printf( %d%s , a[i], i == n - 1 ?   n  :    ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long int a1[200005]; n long long int a2[29]; n vector<long long int> v, v1; n bool p[100000008]; n void seive() { n   long long int i, j; n   for (i = 1; i * i <= 100; i++) { n     if (p[i] == false) { n       for (j = i * i; j <= 100; j += i) { n         p[j] = true; n       } n     } n   } n   v.push_back(2); n   for (i = 3; i <= 100; i += 2) { n     if (p[i] == false) { n       v.push_back(i); n       cout << i << endl; n     } n   } n } n int main() { n   long long int n, d = 0, k = 0, c = 0, x = 0, i = 0, m, f = 0, j = 0, j1, n                    sum = 0, s = 0, mx = 100000000000000000; n   cin >> n; n   while (n--) { n     cin >> m; n     k = 1; n     sum = 0; n     for (i = 3; i <= m; i += 2) { n       d = (i * 2) + (i - 2) * 2; n       sum += (d * k); n       k++; n     } n     cout << sum << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long read() { n   long long r = 0, t = 1, c = getchar(); n   while (c <  0  || c >  9 ) { n     t = c ==  -  ? -1 : 1; n     c = getchar(); n   } n   while (c >=  0  && c <=  9 ) { n     r = r * 10 + c - 48; n     c = getchar(); n   } n   return r * t; n } n const int N = 6010, mod = 998244353; n long long n, m, ans, r; n vector<pair<long long, long long> > c, a[N], b[N]; n set<pair<long long, long long> > s; n void insert(set<pair<long long, long long> > &s, pair<long long, long long> a) { n   auto it = s.upper_bound(pair<long long, long long>(a.first, (~0ull) >> 1)); n   if (it != s.begin()) { n     it--; n     if (it->first <= a.first && it->second >= a.second) return; n   } n   it = s.lower_bound(pair<long long, long long>(a.first, 0)); n   while (it != s.end() && a.first <= it->first && a.second >= it->second) n     it = s.erase(it); n   s.insert(a); n   assert(s.size() <= 200000); n } n int main() { n   n = read(); n   for (int i = (1), end_i = (n); i <= end_i; i++) { n     long long l = read(), r = read(); n     while (r - l >= (l & (-l)) - 1) { n       a[i].push_back(pair<long long, long long>(l, l + (l & (-l)) - 1)); n       l += l & (-l); n     } n     for (int j = (60), end_j = (0); j >= end_j; j--) n       if (r - l + 1 >= (1ll << j)) { n         a[i].push_back(pair<long long, long long>(l, l + (1ll << j) - 1)); n         l += (1ll << j); n       } n   } n   m = read(); n   for (int i = (1), end_i = (m); i <= end_i; i++) { n     long long l = read(), r = read(); n     while (l + (l & (-l)) - 1 <= r) { n       b[i].push_back(pair<long long, long long>(l, l + (l & (-l)) - 1)); n       l += l & (-l); n     } n     for (int j = (60), end_j = (0); j >= end_j; j--) n       if (r - l + 1 >= (1ll << j)) { n         b[i].push_back(pair<long long, long long>(l, l + (1ll << j) - 1)); n         l += (1ll << j); n       } n   } n   for (int i = (1), end_i = (n); i <= end_i; i++) n     for (int j = (1), end_j = (m); j <= end_j; j++) { n       s.clear(); n       for (auto A : a[i]) n         for (auto B : b[j]) { n           long long w = max(A.second - A.first, B.second - B.first); n           insert(s, pair<long long, long long>((A.first ^ B.first) & (~w), n                                                (A.first ^ B.first) | w)); n         } n       for (auto S : s) c.push_back(S); n     } n   sort(c.begin(), c.end(), n        [](pair<long long, long long> a, pair<long long, long long> b) { n          return a.first == b.first ? a.second > b.second : a.first < b.first; n        }); n   for (auto C : c) n     if (C.second > r) { n       ans = (ans + ((C.second - C.first + 1) % mod) * n                        ((C.first + C.second) % mod) % mod * ((mod + 1) / 2)) % n             mod; n       r = C.second; n     } n   printf( %d n , (int)ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 1e6 + 7; n const int mod = 1e9 + 7; n const long long inf = 1e18; n const double PI = acos(-1); n const double eps = 1e-8; n long long qkmod(long long base, long long cnt) { n   long long res = 1; n   while (cnt) { n     if (cnt & 1) res = res * base % mod; n     base = base * base % mod; n     cnt >>= 1; n   } n   return res; n } n int a[maxn]; n int main() { n   int T; n   scanf( %d , &T); n   int n, p; n   while (T--) { n     scanf( %d %d , &n, &p); n     for (int i = 0; i < n; i++) cin >> a[i]; n     if (p == 1) { n       printf(n & 1 ?  1 n  :  0 n ); n       continue; n     } n     sort(a, a + n); n     long long res = 0, sum = 0, pre = 1e6 + 7; n     bool flag = false; n     int i = n - 1, j = n - 1; n     while (~i) { n       int k = a[i--]; n       int d = pre - k; n       pre = k; n       res = res * qkmod(p, d) % mod; n       for (int i = 0; i < min(20, d); i++) { n         sum *= p; n         if (sum > maxn) flag = true; n       } n       while (~j && a[j] == k) { n         j--; n         if (flag || sum > 0) { n           sum--; n           res += mod - 1; n         } else { n           sum++; n           res++; n         } n       } n       res %= mod; n     } n     printf( %d n , (res * qkmod(p, pre)) % mod); n   } n } n


#include <bits/stdc++.h> n int main() { n   int t = 0, i, n; n   char str[60]; n   scanf( %d , &n); n   scanf( %s , &str); n   for (i = 1; t < n; i = i + 1) { n     printf( %c , str[t]); n     t += i; n   } n   return 0; n } n


#include <bits/stdc++.h> n #pragma GCC optimize( Ofast ) n using namespace std; n vector<int> edge[1005]; n pair<int, int> ans[1005]; n set<int> eQQ; n int main() { n   ios::sync_with_stdio(0), cin.tie(0), cout.tie(0); n   int n, a, b; n   cin >> n; n   for (int i = 1; i < n; ++i) { n     cin >> a >> b; n     if (b != n) return cout <<  NO n , 0; n     edge[a].push_back(i); n   } n   for (int i = n - 1; i > 0; --i) n     if (edge[i].empty()) eQQ.insert(i); n   for (int i = n - 1, l; i >= 0; --i) { n     l = n; n     for (auto x : edge[i]) n       if (x == edge[i].back()) n         ans[x] = make_pair(l, i); n       else { n         auto p = eQQ.lower_bound(i); n         if (p == eQQ.begin()) n           return cout <<  NO n , 0; n         else n           --p, ans[x] = make_pair(l, *p), l = *p, eQQ.erase(p); n       } n   } n   cout <<  YES n ; n   for (int i = 1; i < n; ++i) n     cout << ans[i].first <<     << ans[i].second <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n pair<int, int> a[100 * 100 + 5]; n pair<int, int> as[100 * 10000 + 5]; n pair<int, int> ass[100 * 1000 * 7 + 5]; n int p = 0; n int badbakhti(int l, int r) { n   if (r - l < 2) { n     return 1; n   } n   int h = -10000 * 100000, e = (r + l) / 2; n   h = a[e].first; n   for (int i = l; i < r; i++) { n     if (i != e) { n       as[p].first = a[e].first; n       as[p].second = a[i].second; n       p++; n     } n   } n   badbakhti(l, e); n   badbakhti(e, r); n   return 1; n } n int main() { n   int n; n   cin >> n; n   int d, q; n   for (int i = 0; i < n; i++) { n     cin >> d >> q; n     a[i] = make_pair(d, q); n     as[p] = make_pair(d, q); n     p++; n   } n   sort(a, a + n); n   badbakhti(0, n); n   sort(as, as + p); n   int u = 0, er = 0; n   for (int i = 0; i < p; i++) { n     u = i; n     while (as[i].first == as[u].first && as[i].second == as[u].second) u++; n     ass[er].first = as[i].first; n     ass[er].second = as[i].second; n     er++; n     i = u - 1; n   } n   cout << er << endl; n   for (int i = 0; i < er; i++) { n     cout << ass[i].first <<     << ass[i].second << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1000100; n const int M = 1001000; n const int P = 1000000007; n int fac[M], phi[M]; n vector<int> prime; n long long val[M], sum[M], res[M]; n long long ans[M], vec[M], rec[M]; n void init_() { n   fac[1] = 1; n   for (int i = 2; i <= N; i++) { n     if (!fac[i]) { n       fac[i] = i; n       prime.push_back(i); n     } n     for (int j = 0; j <= (int)prime.size() - 1; j++) { n       int p = prime[j]; n       if (p * i > N) break; n       fac[p * i] = p; n       if (fac[i] == p) break; n     } n   } n   phi[1] = 1; n   for (int i = 2; i <= N; i++) { n     int p = fac[i]; n     int j = i / p; n     int p_ = fac[j]; n     if (p == p_) n       phi[i] = phi[j] * p; n     else n       phi[i] = phi[j] * (p - 1); n   } n   for (int i = 1; i <= N; i++) val[i] = (val[i - 1] + i) % P; n   for (int i = 1; i <= N; i++) sum[i] = (sum[i - 1] + (long long)i * i) % P; n   for (int i = 1; i <= N; i++) n     for (int j = 1; j <= N / i; j++) (res[i * j] += (long long)i * phi[j]) %= P; n   for (int i = 1; i <= N; i++) n     ans[i] = n         (sum[i] + sum[i - 1] + 2 * (2 * P - val[i] - val[i - 1] + res[i])) % P; n   for (int i = 1; i <= N; i++) vec[i] = (vec[i - 1] + ans[i] * i) % P; n   for (int i = 1; i <= N; i++) rec[i] = (rec[i - 1] + ans[i] * i % P * i) % P; n   for (int i = 1; i <= N; i++) ans[i] = (ans[i - 1] + ans[i]) % P; n } n int main() { n   init_(); n   int _; n   cin >> _; n   while (_--) { n     int n, m; n     scanf( %d%d , &n, &m); n     if (n > m) swap(n, m); n     n++, m++; n     int o = (ans[n] * n % P * m % P - (n + m) * vec[n] % P + rec[n] + P) % P; n     printf( %d n , o); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int mxN = 1e6 + 3; n int nxt[mxN], vis[mxN]; n void get(string s) { n   nxt[0] = -1; n   int i = 0, k = -1, len = s.size(); n   while (i < len) { n     if (k == -1 or s[i] == s[k]) n       nxt[++i] = ++k; n     else n       k = nxt[k]; n   } n } n int main() { n   int t; n   int n, m, i, j, c, a, b, k, cn = 0, sm = 0, mx = INT_MIN, mn = INT_MAX; n   string s, ss, sr; n   bool f = false, ff = true; n   cin >> s; n   get(s); n   nxt[0] = 0; n   n = nxt[s.size()]; n   for (i = 0; i < s.size(); i++) vis[nxt[i]] = 1; n   while (n) { n     if (vis[n]) break; n     n = nxt[n]; n   } n   if (!n) n     return cout <<  Just a legend  n                 <<   n , n            0; n   for (i = 0; i < n; i++) cout << s[i]; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long a[10] = {}; n int fac[13] = {1}; n inline void p(vector<int> q, vector<int> w, int cnt, long long pr) { n   if (q.size() == 0) { n     a[cnt] += pr; n     return; n   } n   int qq = q[q.size() - 1], ww = w[w.size() - 1]; n   q.pop_back(); n   w.pop_back(); n   p(q, w, cnt, pr * qq); n   p(q, w, cnt + 1, pr * ww); n } n inline int c(int a, int b) { return fac[a] / fac[b] / fac[a - b]; } n inline int cal(int r, int n) { n   if (r <= 0) return 0; n   if (n == 6) return 1; n   long long ret = 0; n   for (int i = 0; i < r; ++i) { n     if (a[i] <= 0) continue; n     --a[i]; n     ret = (ret + cal(r - i, n + 1) * (a[i] + 1)) % 1000000007; n     ++a[i]; n   } n   return ret; n } n int main() { n   char m[11]; n   int cnt = 0, t = 1; n   long long ans = 0, tmp = 1; n   vector<int> q, w; n   for (int i = 1; i < 13; ++i) fac[i] = fac[i - 1] * i; n   scanf( %s , m); n   for (int i = 0; i < strlen(m); ++i) { n     if (m[i] ==  0 ) continue; n     q.clear(); n     w.clear(); n     t = 1; n     if (m[i] <=  4 ) n       t = m[i] -  0 ; n     else if (m[i] <=  7 ) { n       q.push_back(m[i] -  0  - 1); n       w.push_back(1); n     } else { n       q.push_back(m[i] -  0  - 2); n       w.push_back(2); n     } n     for (int j = i + 1; j < strlen(m); ++j) { n       q.push_back(8); n       w.push_back(2); n     } n     p(q, w, cnt, t); n     if (m[i] ==  4  || m[i] ==  7 ) ++cnt; n   } n   ++a[cnt]; n   --a[0]; n   for (int i = 1; i < 10 && a[i] > 0; ++i) { n     --a[i]; n     ans = (ans + (long long)cal(i, 0) * (a[i] + 1)) % 1000000007; n     ++a[i]; n   } n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int32_t main() { n   string s; n   cin >> s; n   long long n = s.length(); n   map<char, long long> m; n   m[ B ] = 1; n   m[ u ] = 1; n   m[ l ] = 1; n   m[ b ] = 1; n   m[ a ] = 1; n   m[ s ] = 1; n   m[ r ] = 1; n   for (long long i = 0; i < n; i++) { n     if (m[s[i]] > 0) { n       m[s[i]]++; n     } n   } n   long long c1 = min(m[ u ] - 1, m[ a ] - 1); n   long long c11 = min(m[ B ] - 1, m[ b ] - 1); n   long long c22 = min(m[ l ] - 1, m[ r ] - 1); n   c22 = min(m[ s ] - 1, c22); n   long long c3 = min(c11, c22); n   long long num1 = c1 / 2; n   long long num2 = c3; n   cout << min(num1, num2) <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 100005; n int a[N]; n struct Block { n   int len, st, ed, num; n   Block(const int len = 0, const int st = 0, const int ed = 0, n         const int num = 0) n       : len(len), st(st), ed(ed), num(num){}; n }; n vector<Block> v; n bool islucky(int a) { n   bool ret = true; n   while (a && ret) { n     ret = a % 10 == 4 || a % 10 == 7; n     a /= 10; n   } n   return ret; n } n struct Node { n   int l, r; n   Node *left, *right; n   Node(const int l = 0, const int r = 0) n       : l(l), r(r), left(NULL), right(NULL) {} n }; n unsigned long long cal(const unsigned long long l, const unsigned long long r) { n   return (r - l) * (r - l + 1) / 2; n } n unsigned long long cal3(const unsigned long long n) { n   return n * (n + 1) / 2 * n * (n + 1) / 2; n } n unsigned long long cal3(const unsigned long long l, n                         const unsigned long long r) { n   return (r * (r + 1) / 2 + (l - 1) * l / 2) * n          (r * (r + 1) / 2 - (l - 1) * l / 2); n } n unsigned long long cal2(const unsigned long long n) { n   return n * (2 * n + 1) * (n + 1) / 6; n } n unsigned long long cal2(const unsigned long long l, n                         const unsigned long long r) { n   return cal2(r) - cal2(l - 1); n } n unsigned long long cal1(const unsigned long long l, n                         const unsigned long long r) { n   return (r + l) * (r - l + 1) / 2; n } n unsigned long long cal0(const unsigned long long l, n                         const unsigned long long r) { n   return r - l + 1; n } n int main() { n   unsigned long long n; n   cin >> n; n   int last = 0; n   for (int i = 0; i < n; ++i) { n     scanf( %d , a + i); n     if (islucky(a[i])) { n       v.push_back(Block(i - last + 1, last, i, a[i])); n       last = i + 1; n     } n   } n   a[n] = -1; n   if (last != n) { n     v.push_back(Block(n - last, last, n, -1)); n   } n   map<int, vector<int> > tr; n   for (int i = 0; i < (int)v.size(); ++i) { n     tr[v[i].num].push_back(v[i].ed); n   } n   unsigned long long ans = 0; n   for (int i = 0; i < v.size(); ++i) { n     unsigned long long bpSum = 0; n     set<int> bp; n     set<int> contain; n     unsigned long long tmp = 0; n     tmp = 1 * cal3(v[i].st, v[i].ed - 1); n     tmp += (n * (n - 1) + n * (v[i].st - 1) + (n - 1) * (v[i].st - 1)) * n            cal1(v[i].st, v[i].ed - 1); n     tmp -= (n + n - 1 + (v[i].st - 1)) * cal2(v[i].st, v[i].ed - 1); n     tmp -= (v[i].st - 1) * n * (n - 1) * cal0(v[i].st, v[i].ed - 1); n     ans += tmp / 2; n     bp.insert(n); n     bp.insert(v[i].ed - 1); n     bpSum = cal(v[i].ed, n); n     for (int j = i; j + 1 < (int)v.size(); ++j) { n       int bar = v[j + 1].ed; n       while (*bp.begin() < bar) { n         bpSum -= cal(*bp.begin() + 1, *(++bp.begin())); n         bp.erase(bp.begin()); n       } n       bp.insert(bar - 1); n       bpSum += cal(*bp.begin() + 1, *(++bp.begin())); n       if (!contain.count(v[j].num)) { n         contain.insert(v[j].num); n         vector<int> breaks = tr[v[j].num]; n         for (int k = 0; k < breaks.size(); ++k) { n           if (breaks[k] >= v[j + 1].ed) { n             int cut = breaks[k]; n             set<int>::iterator it = bp.upper_bound(cut); n             int right = *it, left = *(--it); n             bpSum -= cal(left + 1, right); n             bp.insert(cut); n             bpSum += cal(left + 1, cut) + cal(cut + 1, right); n           } n         } n       } n       unsigned long long k, tmp1, rem = bpSum; n       k = *(++bp.begin()); n       rem *= (v[j + 1].ed - v[j].ed); n       tmp1 = 1 * cal2(v[j].ed, v[j + 1].ed - 1) + n              (2 * k - v[j + 1].ed) * (v[j + 1].ed - 1) * n                  cal0(v[j].ed, v[j + 1].ed - 1) - n              (2 * k - v[j + 1].ed + v[j + 1].ed - 1) * n                  cal1(v[j].ed, v[j + 1].ed - 1); n       ans += v[i].len * (rem + tmp1 / 2); n     } n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<pair<long long, pair<long long, long long>>> v; n long long a, b, w, cnt; n long long parent[100005], sz[100005]; n void init(long long n) { n   for (long long i = 1; i <= n; i++) parent[i] = i, sz[i] = 1; n } n long long root(long long x) { n   if (parent[x] == x) return x; n   return parent[x] = root(parent[x]); n } n long long mst(long long n, long long k) { n   long long ans = 0; n   for (long long i = 0; i < n; i++) { n     long long x = v[i].second.first, y = v[i].second.second, z = v[i].first; n     if (x == a && y == b) continue; n     long long px = root(x), py = root(y); n     if (px != py) { n       if (sz[px] < sz[py]) swap(px, py); n       ans += z; n       sz[px] += sz[py]; n       parent[py] = px; n       cnt++; n     } n   } n   if (cnt != k) return -1; n   return ans; n } n int main() { n   long long t, n, i, j, m, ans = 0; n   cin >> n >> m; n   for (i = 1; i <= m; i++) { n     long long x, y, z; n     cin >> x >> y >> z; n     if (x > y) swap(x, y); n     v.push_back({z, {x, y}}); n   } n   a = v[0].second.first, b = v[0].second.second, w = v[0].first; n   sort(v.begin(), v.end()); n   init(n); n   cnt = 2; n   sz[a] = 2; n   parent[b] = a; n   long long ans1 = w + mst(m, n); n   init(n); n   cnt = 1; n   long long ans2 = mst(m, n); n   if (ans2 >= 0) n     cout << w - (ans1 - ans2); n   else n     cout << 1000000000; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 1000001; n int n; n int l[MAXN]; n int r[MAXN]; n int num[MAXN]; n int izq[MAXN]; n int der[MAXN]; n long long int ans[MAXN]; n long long int bit[2][MAXN]; n vector<pair<int, int> > L[MAXN]; n vector<pair<int, int> > R[MAXN]; n void update(int id, int a, int v) { n   while (a <= n) { n     bit[id][a] += v; n     a += a & -a; n   } n } n void update_range(int a, int b, int v) { n   a++, b++; n   update(0, a, v); n   update(0, b + 1, -v); n   update(1, a, v * (a - 1)); n   update(1, b + 1, -v * b); n } n long long int query(int id, int a) { n   long long int ans = 0; n   while (a > 0) { n     ans += bit[id][a]; n     a -= a & -a; n   } n   return ans; n } n long long int query(int b) { return query(0, b) * b - query(1, b); } n long long int query_range(int a, int b) { n   if (a > b) return 0ll; n   a++, b++; n   return query(b) - query(a - 1); n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   int q; n   cin >> n >> q; n   vector<pair<int, int> > A; n   for (int i = 0; i < n; ++i) { n     cin >> num[i]; n     A.emplace_back(num[i], i); n   } n   sort(A.begin(), A.end()); n   reverse(A.begin(), A.end()); n   set<int> s; n   s.insert(-1); n   s.insert(n); n   for (int i = 0; i < n; ++i) { n     auto it = s.lower_bound(A[i].second); n     der[A[i].second] = *it; n     it--; n     izq[A[i].second] = *it; n     s.insert(A[i].second); n   } n   for (int i = 0; i < q; ++i) cin >> l[i]; n   for (int i = 0; i < q; ++i) cin >> r[i]; n   for (int i = 0; i < q; ++i) { n     l[i]--, r[i]--; n     L[l[i]].emplace_back(r[i], i); n     R[r[i] + 1].emplace_back(l[i], i); n   } n   for (int i = n - 1; i >= 0; i--) { n     update_range(izq[i] + 1, der[i] - 1, 1); n     for (int j = 0; j < R[i].size(); ++j) n       ans[R[i][j].second] -= query_range(R[i][j].first, i - 1); n     for (int j = 0; j < L[i].size(); ++j) n       ans[L[i][j].second] += query_range(i, L[i][j].first); n   } n   for (int i = 0; i < q; ++i) cout << ans[i] <<    ; n   cout <<   n ; n   return 0; n } n