#include <bits/stdc++.h> n using namespace std; n queue<int> fila[100000 + 10]; n int bit[100000 + 10]; n int rsp[100000 + 10]; n pair<pair<int, int>, int> q[100000 + 10]; n int v[100000 + 10]; n int ls[100000 + 10]; n void add(int p, int v) { n   if (p == 0) return; n   for (; p <= 100000; p += (p & (-p))) bit[p] += v; n } n int get(int p) { n   int ret = 0; n   for (; p > 0; p -= (p & (-p))) ret += bit[p]; n   return ret; n } n int main() { n   int n, m; n   scanf( %d %d , &n, &m); n   memset(bit, 0, sizeof(bit)); n   for (int i = 1; i <= n; i++) scanf( %d , &v[i]), ls[i] = -1, fila[i].push(0); n   for (int i = 1; i <= m; i++) { n     scanf( %d %d , &q[i - 1].first.second, &q[i - 1].first.first); n     q[i - 1].second = i - 1; n   } n   sort(q, q + m); n   int ind = 0; n   for (int i = 1; i <= n; i++) { n     if (v[i] <= n) { n       if (ls[v[i]] != -1) { n         add(ls[v[i]] + 1, -1); n         add(fila[v[i]].front() + 1, +1); n       } n       if (fila[v[i]].size() == v[i]) { n         add(fila[v[i]].front() + 1, +1); n         ls[v[i]] = fila[v[i]].front(); n         fila[v[i]].pop(); n         fila[v[i]].push(i); n         add(fila[v[i]].front() + 1, -1); n       } else n         fila[v[i]].push(i); n     } n     while (ind < m && q[ind].first.first == i) { n       rsp[q[ind].second] = get(q[ind].first.second); n       ind++; n     } n   } n   for (int i = 0; i < m; i++) { n     printf( %d n , rsp[i]); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n inline int read() { n   int res = 0, f = 1; n   char ch = getchar(); n   while (!isdigit(ch)) { n     if (ch ==  - ) f = -f; n     ch = getchar(); n   } n   while (isdigit(ch)) { n     res = (res << 1) + (res << 3) + (ch ^ 48); n     ch = getchar(); n   } n   return res * f; n } n const int N = 200005; n int n, m, root; n int ls[N], rs[N], val[N], key[N], cnt[N], add[N], ans[N]; n inline void pushdown(int v) { n   if (add[v]) { n     add[ls[v]] += add[v]; n     add[rs[v]] += add[v]; n     val[ls[v]] += add[v]; n     val[rs[v]] += add[v]; n     add[v] = 0; n   } n   if (ans[v]) { n     ans[ls[v]] += ans[v]; n     ans[rs[v]] += ans[v]; n     cnt[ls[v]] += ans[v]; n     cnt[rs[v]] += ans[v]; n     ans[v] = 0; n   } n } n void split(int k, int &x, int &y, int v) { n   if (!k) { n     x = y = 0; n     return; n   } n   pushdown(k); n   if (val[k] < v) n     x = k, split(rs[k], rs[x], y, v); n   else n     y = k, split(ls[k], x, ls[y], v); n } n int merge(int x, int y) { n   if (!x || !y) return x + y; n   if (key[x] < key[y]) { n     pushdown(x); n     rs[x] = merge(rs[x], y); n     return x; n   } else { n     pushdown(y); n     ls[y] = merge(x, ls[y]); n     return y; n   } n } n int insert(int x, int y) { n   int rt1 = 0, rt2 = 0; n   split(x, rt1, rt2, val[y]); n   rt1 = merge(rt1, y); n   x = merge(rt1, rt2); n   return x; n } n int build(int v, int y) { n   if (!v) return y; n   pushdown(v); n   y = build(ls[v], y); n   y = build(rs[v], y); n   ls[v] = rs[v] = 0; n   return insert(y, v); n } n void dfs(int v) { n   if (!v) return; n   pushdown(v); n   dfs(ls[v]); n   dfs(rs[v]); n } n pair<int, int> a[N]; n int main() { n   scanf( %d , &n); n   for (int i = 1, c, q; i <= n; i++) { n     scanf( %d%d , &c, &q); n     a[i] = make_pair(-q, c); n   } n   sort(a + 1, a + 1 + n); n   scanf( %d , &m); n   for (int i = 1; i <= m; i++) { n     scanf( %d , &val[i]); n     key[i] = rand(); n     root = insert(root, i); n   } n   for (int i = 1; i <= n; i++) { n     int c = a[i].second; n     int r1 = 0, r2 = 0, r3 = 0, r4 = 0; n     split(root, r1, r2, c); n     val[r2] -= c; n     add[r2] -= c; n     cnt[r2]++; n     ans[r2]++; n     split(r2, r3, r4, c - 1); n     r1 = build(r3, r1); n     root = merge(r1, r4); n   } n   dfs(root); n   for (int i = 1; i <= m; i++) printf( %d  , cnt[i]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int Ans, Cap[55][55], From[55], K, N, T[55]; n vector<int> E[55]; n bool dijkstra() { n   priority_queue<pair<int, int>, vector<pair<int, int>>, n                  greater<pair<int, int>>> n       q; n   vector<int> d(55, 1005); n   d[1] = 0; n   q.push(make_pair(0, 1)); n   while (!q.empty()) { n     pair<int, int> p = q.top(); n     q.pop(); n     int n = p.second, c = p.first; n     if (n == N) return true; n     for (int i = 1; i <= N; i++) n       if (Cap[n][i] && c < d[i]) { n         d[i] = c; n         From[i] = n; n         T[i] = 0; n         q.push(make_pair(c, i)); n       } n     for (int v : E[n]) n       if (c + 1 < d[v]) { n         d[v] = c + 1; n         From[v] = n; n         T[v] = 1; n         q.push(make_pair(c + 1, v)); n       } n   } n   return false; n } n int main() { n   cin >> N >> K; n   for (int i = 1; i <= N; i++) n     for (int j = 1; j <= N; j++) { n       cin >> Cap[i][j]; n       if (Cap[i][j]) E[i].push_back(j); n     } n   while (true) { n     if (!dijkstra()) break; n     int cost = 0, flow = 1000000, n = N; n     while (From[n] > 0) { n       if (T[n] == 0) flow = min(flow, Cap[From[n]][n]); n       n = From[n]; n     } n     n = N; n     while (From[n] > 0) { n       if (T[n]) n         cost++; n       else n         Cap[From[n]][n] -= flow, Cap[n][From[n]] += flow; n       n = From[n]; n     } n     if (cost * flow <= K) { n       K -= cost * flow; n       Ans += flow; n     } else { n       Ans += K / cost; n       break; n     } n   } n   cout << Ans; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename... T> n void read(T &...args) { n   ((cin >> args), ...); n } n template <typename... T> n void write(T... args) { n   ((cout << args <<    ), ...); n   cout <<   n ; n } n long long gcd(long long a, long long b) { n   if (b > a) { n     return gcd(b, a); n   } n   if (b == 0) { n     return a; n   } n   return gcd(b, a % b); n } n long long add(long long a, long long b) { n   a = a % 1000000007; n   b = b % 1000000007; n   return (((a + b) % 1000000007) + 1000000007) % 1000000007; n } n long long sub(long long a, long long b) { n   a = a % 1000000007; n   b = b % 1000000007; n   return (((a - b) % 1000000007) + 1000000007) % 1000000007; n } n long long mul(long long a, long long b) { n   a = a % 1000000007; n   b = b % 1000000007; n   return (((a * b) % 1000000007) + 1000000007) % 1000000007; n } n long long power(long long x, long long y) { n   long long res = 1; n   x = x % 1000000007; n   if (x == 0) return 0; n   while (y > 0) { n     if (y & 1) { n       res = (res * x) % 1000000007; n     } n     y = y >> 1; n     x = (x * x) % 1000000007; n   } n   return res; n } n long long find(vector<long long> &parent, long long x) { n   if (parent[x] != x) parent[x] = find(parent, parent[x]); n   return parent[x]; n } n void join(vector<long long> &parent, vector<long long> &rank, long long x, n           long long y) { n   long long xParent = find(parent, x); n   long long yParent = find(parent, y); n   if (xParent == yParent) return; n   if (rank[xParent] > rank[yParent]) { n     parent[yParent] = xParent; n   } else if (rank[xParent] < rank[yParent]) { n     parent[xParent] = yParent; n   } else { n     parent[xParent] = yParent; n     rank[yParent]++; n   } n } n void solve() { n   long long n, m, k; n   read(n, m, k); n   vector<vector<long long> > mat(n, vector<long long>(m, 0)); n   for (long long i = 0; i < n; i++) { n     string str; n     cin >> str; n     for (long long j = 0; j < m; j++) { n       if (str[j] ==  * ) mat[i][j] = 1; n     } n   } n   for (long long i = 0; i < n; i++) { n     for (long long j = 0; j < m; j++) { n       if (mat[i][j] == 0) continue; n       long long len = 0; n       while (j - len >= 0 && i - len >= 0 && j + len < m) { n         if (mat[i - len][j - len] == 0 || mat[i - len][j + len] == 0) break; n         len++; n       } n       len--; n       if (len >= k) { n         for (long long p = 0; p < len + 1; p++) { n           mat[i - p][j - p] = 2; n           mat[i - p][j + p] = 2; n         } n       } n     } n   } n   bool found = false; n   for (long long i = 0; i < n; i++) { n     for (long long j = 0; j < m; j++) { n       if (mat[i][j] == 1) found = true; n     } n   } n   if (found) n     write( NO ); n   else n     write( YES ); n } n signed main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   long long iter; n   cin >> iter; n   while (iter) { n     solve(); n     iter--; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int t; n   cin >> t; n   while (t--) { n     int n, last = 0, count = 0, sum, sum1; n     cin >> n; n     while (n >= 10) { n       last = n / 10; n       n = n / 10; n       count += 1; n     } n     if (count + 1 == 1) n       sum1 = 1; n     else if (count + 1 == 2) n       sum1 = 3; n     else if (count + 1 == 3) n       sum1 = 6; n     else if (count + 1 == 4) n       sum1 = 10; n     sum = (last - 1) * 10 + sum1; n     if (n > 10) n       cout << sum << endl; n     else { n       last = n; n       sum = (last - 1) * 10 + sum1; n       cout << sum << endl; n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n struct line { n   long long x, y, dx, dy, s; n } a[25000]; n struct point { n   int i; n   bool f; n   long double x; n } b[50000], c[25000]; n bool comp(point a, point b) { n   if (a.x == b.x) { n     if (a.f == b.f) return (a.i < b.i); n     return (a.f < b.f); n   } n   return (a.x < b.x); n } n long double x; n bool operator<(point y, point z) { n   long double y1, y2, dy, dx, ix, iy; n   dy = a[y.i].dy; n   dx = a[y.i].dx; n   ix = a[y.i].x; n   iy = a[y.i].y; n   y1 = iy + (x - ix) * (dy / dx); n   dy = a[z.i].dy; n   dx = a[z.i].dx; n   ix = a[z.i].x; n   iy = a[z.i].y; n   y2 = iy + (x - ix) * (dy / dx); n   if (y1 == y2) return (y.i < z.i); n   return (y1 < y2); n } n long double compute(line a, long long xn, long long xd, long long yn, n                     long long yd) { n   long double x = xn; n   x = x / xd; n   x = (a.x - x) * (a.x - x); n   long double y = yn; n   y = y / yd; n   y = (a.y - y) * (a.y - y); n   long double ans = x + y; n   ans = sqrt(ans); n   ans = ans / a.s; n   return ans; n } n bool check(int i, int j, long double t) { n   long long xn, xd, yn, yd; n   long double t1 = -1, t2 = -1; n   if (a[i].dx * a[j].dy != a[j].dx * a[i].dy) { n     xn = a[i].dx * a[j].dy * a[j].x - a[j].dx * a[i].dy * a[i].x + n          a[j].dx * a[i].dx * (a[i].y - a[j].y); n     xd = a[i].dx * a[j].dy - a[j].dx * a[i].dy; n     yn = a[i].dy * a[j].dy * (a[j].x - a[i].x) + a[j].dy * a[i].dx * a[i].y - n          a[i].dy * a[j].dx * a[j].y; n     yd = a[i].dx * a[j].dy - a[j].dx * a[i].dy; n     if (xd < 0) { n       xn *= -1; n       xd *= -1; n     } n     if (yd < 0) { n       yn *= -1; n       yd *= -1; n     } n     if (((a[i].dx > 0 && (xn - xd * a[i].x) >= 0) || n          (a[i].dx < 0 && (xn - xd * a[i].x) <= 0)) && n         ((a[i].dy > 0 && (yn - yd * a[i].y) >= 0) || n          (a[i].dy < 0 && (yn - yd * a[i].y) <= 0))) n       t1 = compute(a[i], xn, xd, yn, yd); n     if (((a[j].dx > 0 && (xn - xd * a[j].x) >= 0) || n          (a[j].dx < 0 && (xn - xd * a[j].x) <= 0)) && n         ((a[j].dy > 0 && (yn - yd * a[j].y) >= 0) || n          (a[j].dy < 0 && (yn - yd * a[j].y) <= 0))) n       t2 = compute(a[j], xn, xd, yn, yd); n   } else if ((a[i].dx * (a[j].y - a[i].y)) == (a[i].dy * (a[j].x - a[i].x))) { n     bool f1 = false; n     xn = a[j].x; n     xd = 1; n     yn = a[j].y; n     yd = 1; n     if (((a[i].dx > 0 && (xn - xd * a[i].x) >= 0) || n          (a[i].dx < 0 && (xn - xd * a[i].x) <= 0)) && n         ((a[i].dy > 0 && (yn - yd * a[i].y) >= 0) || n          (a[i].dy < 0 && (yn - yd * a[i].y) <= 0))) n       f1 = true; n     bool f2 = false; n     xn = a[i].x; n     xd = 1; n     yn = a[i].y; n     yd = 1; n     if (((a[j].dx > 0 && (xn - xd * a[j].x) >= 0) || n          (a[j].dx < 0 && (xn - xd * a[j].x) <= 0)) && n         ((a[j].dy > 0 && (yn - yd * a[j].y) >= 0) || n          (a[j].dy < 0 && (yn - yd * a[j].y) <= 0))) n       f2 = true; n     if (f1 && f2) { n       xn = a[i].x * a[j].s + a[j].x * a[i].s; n       xd = a[i].s + a[j].s; n       yn = a[i].y * a[j].s + a[j].y * a[i].s; n       yd = a[i].s + a[j].s; n       t1 = compute(a[i], xn, xd, yn, yd); n       t2 = compute(a[j], xn, xd, yn, yd); n     } else if (f1) { n       xn = a[j].x; n       xd = 1; n       yn = a[j].y; n       yd = 1; n       t1 = compute(a[i], xn, xd, yn, yd); n       t2 = 0; n     } else if (f2) { n       xn = a[i].x; n       xd = 1; n       yn = a[i].y; n       yd = 1; n       t1 = 0; n       t2 = compute(a[j], xn, xd, yn, yd); n     } n   } n   if (t1 >= 0 && t2 >= 0 && max(t1, t2) <= t) return true; n   return false; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   bool flag = false; n   int n, i, j, z; n   cin >> n; n   for (i = 0; i < n; i++) n     cin >> a[i].x >> a[i].y >> a[i].dx >> a[i].dy >> a[i].s; n   long double l = 0, h = 1e13, m, dx, dy, s; n   set<point> t; n   set<point>::iterator it, it1, it2; n   for (z = 0; z < 70; z++) { n     m = (l + h) / 2; n     for (i = 0; i < n; i++) { n       b[2 * i].i = b[2 * i + 1].i = i; n       b[2 * i].x = a[i].x; n       dx = a[i].dx; n       dy = a[i].dy; n       s = a[i].s; n       b[2 * i + 1].x = b[2 * i].x + ((dx * s * m) / sqrt(dx * dx + dy * dy)); n       if (b[2 * i].x < b[2 * i + 1].x) { n         b[2 * i].f = 0; n         b[2 * i + 1].f = 1; n       } else { n         b[2 * i].f = 1; n         b[2 * i + 1].f = 0; n       } n     } n     j = 2 * n; n     sort(b, b + j, comp); n     for (i = 0; i < j; i++) { n       if (b[i].f) { n         x = b[i].x; n         it = t.lower_bound(c[b[i].i]); n         it1 = it; n         it1++; n         if (it != t.begin() && it1 != t.end()) { n           it2 = it; n           it2--; n           if (check(it1->i, it2->i, m)) break; n         } n         t.erase(it); n       } else { n         x = b[i].x; n         t.insert(b[i]); n         it = t.lower_bound(b[i]); n         it1 = it; n         it1++; n         if (it1 != t.end() && check(it->i, it1->i, m)) break; n         if (it != t.begin()) { n           it1 = it; n           it1--; n           if (check(it->i, it1->i, m)) break; n         } n         c[b[i].i] = b[i]; n       } n     } n     if (i < j) { n       flag = true; n       h = m; n     } else n       l = m; n     t.clear(); n   } n   if (!flag) n     cout <<  No show :( ; n   else { n     cout << fixed; n     cout << setprecision(15); n     cout << l; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   long long int M, k, Q, sum = 0, a = 1, W; n   vector<int> x; n   scanf( %d , &n); n   for (int i = 1; i <= n; i++) { n     scanf( %lld , &W); n     for (int j = 0; j < W; j++) { n       x.push_back(i); n     } n   } n   scanf( %lld , &M); n   for (k = 1; k <= M; k++) { n     scanf( %lld , &Q); n     printf( %d n , x[Q - 1]); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int dp[141][141][12]; n int peas[141][141]; n char track[141][141][12]; n int main() { n   int n, m, k; n   cin >> n >> m >> k; n   string str; n   for (int i = 0; i < 141; i++) n     for (int j = 0; j < 141; j++) n       for (int p = 0; p < 12; p++) dp[i][j][p] = -111111111; n   for (int i = 0; i < n; i++) { n     cin >> str; n     for (int j = 0; j < m; j++) { n       peas[n - i][j + 1] = str[j] -  0 ; n     } n   } n   for (int j = 1; j <= m; j++) dp[1][j][peas[1][j] % (k + 1)] = peas[1][j]; n   for (int i = 2; i <= n; i++) n     for (int j = 1; j <= m; j++) n       for (int r = 0; r < k + 1; r++) { n         int pr = (r - peas[i][j] + 1000 * k + 1000) % (k + 1); n         dp[i][j][r] = n             max(dp[i - 1][j - 1][pr], dp[i - 1][j + 1][pr]) + peas[i][j]; n         track[i][j][r] = n             (dp[i - 1][j - 1][pr] > dp[i - 1][j + 1][pr]) ?  R  :  L ; n       } n   int sol = -1, idx = 0; n   for (int j = 1; j <= m; j++) { n     if (sol < dp[n][j][0]) { n       sol = dp[n][j][0]; n       idx = j; n     } n   } n   if (sol < 0) { n     cout <<  -1  << endl; n     return 0; n   } n   int starting = 0; n   string ans =   ; n   int rem = 0; n   for (int i = n; i >= 1; i--) { n     if (i == 1) break; n     if (track[i][idx][rem] ==  R ) { n       rem = (rem + 1000 * k + 1000 - peas[i][idx]) % (k + 1); n       idx = idx - 1; n       ans +=  R ; n     } else { n       rem = (rem + 1000 * k + 1000 - peas[i][idx]) % (k + 1); n       idx = idx + 1; n       ans +=  L ; n     } n   } n   cout << sol << endl; n   cout << idx << endl; n   reverse(ans.begin(), ans.end()); n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int N, K, P[2100], S[2100], ans[(int)4e6 + 100], cost = 0; n int abs(int a) { return a > 0 ? a : -a; } n void swap(int i, int j) { n   cost += abs(i - j); n   ans[++K] = i; n   ans[++K] = j; n   P[i] ^= P[j]; n   P[j] ^= P[i]; n   P[i] ^= P[j]; n } n stack<int> box; n int main() { n   scanf( %d , &N); n   for (int i = 1; i <= N; i++) scanf( %d , P + i); n   for (int i = 1; i <= N; i++) { n     int tmp; n     scanf( %d , &tmp); n     S[tmp] = i; n   } n   for (int i = 1; i <= N; i++) P[i] = S[P[i]]; n   for (int i = 1; i <= N; i++) { n     if (P[i] > i) box.push(i); n     int pos = i; n     while (box.size() && P[pos] <= box.top() && P[box.top()] >= pos) { n       swap(box.top(), pos); n       i = pos - 1; n       pos = box.top(); n       box.pop(); n     } n   } n   printf( %d n%d n , cost, K >> 1); n   for (int i = 1; i <= K; i++) printf( %d%c , ans[i], i & 1 ? 32 : 10); n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 5e5 + 5; n struct edge { n   int next, v; n } e[MAXN << 1]; n int n, q, root, cnt, head[MAXN], dep[MAXN], anc[MAXN][30]; n void addEdge(int x, int y) { n   e[++cnt].v = y; n   e[cnt].next = head[x]; n   head[x] = cnt; n } n void dfs(int x) { n   for (int i = head[x]; i != -1; i = e[i].next) { n     int y = e[i].v; n     if (y == anc[x][0]) continue; n     dep[y] = dep[x] + 1; n     anc[y][0] = x; n     dfs(y); n   } n } n void init() { n   cnt = 0; n   root = 1; n   memset(head, -1, sizeof(head)); n } n int lca(int a, int b) { n   if (dep[a] > dep[b]) swap(a, b); n   int h = dep[b] - dep[a]; n   for (int i = 0; (1 << i) <= h; i++) n     if ((1 << i) & h) b = anc[b][i]; n   if (a != b) { n     for (int i = (int)log2(dep[a]); i >= 0; i--) { n       if (anc[a][i] != anc[b][i]) { n         a = anc[a][i]; n         b = anc[b][i]; n       } n     } n     a = anc[a][0]; n   } n   return a; n } n int dist(int x, int y) { return dep[x] + dep[y] - 2 * dep[lca(x, y)]; } n int main() { n   init(); n   scanf( %d , &n); n   for (int i = 1; i < n; i++) { n     int x, y; n     scanf( %d %d , &x, &y); n     addEdge(x, y); n     addEdge(y, x); n   } n   dep[root] = 0; n   dfs(root); n   for (int j = 1; (1 << j) <= n; j++) { n     for (int i = 1; i <= n; i++) { n       anc[i][j] = anc[anc[i][j - 1]][j - 1]; n     } n   } n   scanf( %d , &q); n   for (int kk = 1; kk < 10; kk++) int jj = kk + 1; n   for (int i = 1; i <= q; i++) { n     int x, y, a, b, k; n     scanf( %d %d %d %d %d , &x, &y, &a, &b, &k); n     int temp1 = dist(a, b); n     int temp2 = min(dist(a, x) + dist(y, b), dist(a, y) + dist(x, b)) + 1; n     int ans = k + 1; n     if (k % 2 == temp1 % 2) ans = min(ans, temp1); n     if (k % 2 == temp2 % 2) ans = min(ans, temp2); n     if (ans <= k) n       puts( YES ); n     else n       puts( NO ); n   } n } n


#include <bits/stdc++.h> n using namespace std; n long long int n, k; n long long int a[200100]; n bool check(long long int x) { n   long long int moves = 0; n   for (__typeof(n + 1) i = (n / 2 + 1) - ((n / 2 + 1) > (n + 1)); n        i != (n + 1) - ((n / 2 + 1) > (n + 1)); n        i += 1 - 2 * ((n / 2 + 1) > (n + 1))) { n     moves += max((long long int)0, x - a[i]); n     if (moves > k) return false; n   } n   if (moves <= k) return true; n   return false; n } n void solve() { n   cin >> n >> k; n   for (__typeof(n + 1) i = (1) - ((1) > (n + 1)); n        i != (n + 1) - ((1) > (n + 1)); i += 1 - 2 * ((1) > (n + 1))) { n     cin >> a[i]; n   } n   sort(a + 1, a + n + 1); n   long long int l = 1, r = 2e9; n   bool can = false; n   while (l < r) { n     long long int mid = (l + r + 1) / 2; n     if (check(mid)) { n       l = mid; n     } else { n       r = mid - 1; n     } n   } n   cout << l << endl; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   int t; n   t = 1; n   while (t--) { n     solve(); n   } n } n


#include <bits/stdc++.h> n using namespace std; n char s[1000002]; n int o = 0, len; n int main() { n   cin >> s; n   if ((s[0] ==  1 ) && (strlen(s) == 1)) { n     cout << 0; n     return 0; n   } n   for (int i = strlen(s); i >= 0; i--) { n     s[i + 1] = s[i]; n   } n   s[0] =  0 ; n   int i = strlen(s) - 1; n   while (i > 0) { n     if (s[i] ==  0 ) { n       o += 1; n       i--; n     } else { n       if (i == 1) break; n       o++; n       while (s[i] ==  1 ) { n         o++; n         i--; n       } n       s[i] =  1 ; n     } n   } n   cout << o; n } n


#include <bits/stdc++.h> n using namespace std; n   n #define all(cont) cont.begin(), cont.end() n #define rall(cont) cont.end(), cont.begin() n #define FOREACH(it, l) for (auto it = l.begin(); it != l.end(); it++) n #define IN(A, B, C) assert(B <= A && A <= C) n #define SORT(v) sort(v.begin(), v.end()) n #define MP make_pair n #define PB push_back n #define INF (int)1e9 n #define EPS 1e-9 n #define PI 3.1415926535897932384626433832795 n #define MOD 1000000007 n const double pi = acos(-1.0); n typedef long int int32; n typedef unsigned long int uint32; n typedef long long int int64; n typedef unsigned long long int uint64; n const int N = 1e5 + 5; n   n void solve() { n         int64 n, q, l, r; n         cin>>n>>q; n         string s; n         cin>>s; n         vector<int64> v, v1(n); n         while(n--){ n             v.PB(s[s.size()-n-1]-96); n         } n         partial_sum(v.begin(), v.end(), v1.begin()); n         while(q--){ n             cin>>l>>r; n             cout<<v1[r-1]-v1[l-1]+v[l-1]<<  n ; n         } n          n          n } n   n int main() { n         solve(); n     return 0; n }


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long int n; n   cin >> n; n   long long int i = 1; n   long long int a[n]; n   while (i <= n) { n     cin >> a[i]; n     i++; n   } n   cout << n + 1 << endl; n   cout << 1 <<     << n <<     << 5600 << endl; n   i = 1; n   while (i <= n) { n     cout << 2 <<     << i <<     << a[i] + 5600 - i << endl; n     i++; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int dist[551000]; n long long int sum = 0; n set<long long int> ans; n int fTemp[551000]; n int freq[551000]; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(nullptr); n   int n; n   cin >> n; n   for (int i = 0; i < n; i++) { n     cin >> dist[i + 1]; n     if (i + 1 >= 2) { n       dist[i] = dist[i + 1] - dist[i]; n       sum += dist[i]; n     } n   } n   int m; n   cin >> m; n   for (int i = 0; i < m; i++) { n     int p; n     cin >> p; n     freq[p]++; n   } n   int dir = 1; n   int stop = 1; n   int end = -1; n   set<int> good; n   long long int d = 0; n   for (int i = 0; i < m; i++) { n     fTemp[stop]++; n     if (i == m - 1) { n       end = stop; n       break; n     } n     if (stop + dir <= n && stop + dir >= 1) { n       d += dist[((stop > stop + dir) ? stop + dir : stop)]; n       stop += dir; n     } n     if (stop == n) n       dir = -1; n     else if (stop == 1) n       dir = 1; n   } n   for (int i = 1; i < n + 1; i++) { n     if (fTemp[i] == freq[i]) good.insert(i); n   } n   if (good.size() == n) { n     ans.insert(d); n   } n   if (end == n) n     dir = -1; n   else if (end == 1) n     dir = -1; n   for (int i = 2; i < n + 1; i++) { n     fTemp[i - 1]--; n     d -= dist[i - 1]; n     if (end == n) { n       dir = -1; n     } else if (end == 1) { n       dir = 1; n     } n     fTemp[end + dir]++; n     d += dist[((end + dir > end) ? end : end + dir)]; n     if (fTemp[i - 1] == freq[i - 1]) n       good.insert(i - 1); n     else { n       good.erase(i - 1); n     } n     if (fTemp[end + dir] == freq[end + dir]) n       good.insert(end + dir); n     else { n       good.erase(end + dir); n     } n     if (good.size() == n) { n       ans.insert(d); n     } n     end += dir; n   } n   if (end == n) n     dir = -1; n   else if (end == 1) n     dir = -1; n   for (int i = n - 1; i > 1; i--) { n     fTemp[i + 1]--; n     d -= dist[i]; n     if (end == n) { n       dir = -1; n     } else if (end == 1) { n       dir = 1; n     } n     fTemp[end + dir]++; n     d += dist[((end + dir > end) ? end : end + dir)]; n     if (fTemp[i + 1] == freq[i + 1]) n       good.insert(i + 1); n     else { n       good.erase(i + 1); n     } n     if (fTemp[end + dir] == freq[end + dir]) n       good.insert(end + dir); n     else { n       good.erase(end + dir); n     } n     if (good.size() == n) { n       ans.insert(d); n     } n     end += dir; n   } n   if (ans.size() > 1) { n     cout << -1; n   } else { n     cout << *ans.begin(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int md1 = 1e9 + 7, md2 = 1e9 + 9, B = 130; n struct H { n   int x, y; n   H(int a = 0, int b = 0) : x(a), y(b) {} n   bool operator==(H o) { return x == o.x && y == o.y; } n   void operator+=(char c) { n     x = ((long long)x * B + c) % md1; n     y = ((long long)y * B + c) % md2; n   } n } h[2000]; n char p[1000001], s[201]; n int k, n, m; n inline void no() { printf( 0 n ), exit(0); } n H gth(int i, int o) { n   H h; n   for (int j = i; j < m; j += o) h += s[j]; n   return h; n } n string gt(int o) { n   string an(k,  0 ); n   int z = o; n   for (int i = k - 1; i >= 0 && z; --i) n     if (gth(z - 1, o) == h[i]) an[i] =  1 , --z; n   if (z) return string(); n   return an; n } n int main() { n   gets(p); n   gets(s); n   scanf( %d , &k); n   n = strlen(p); n   m = strlen(s); n   if (m > n) no(); n   for (int i = 0; i < (int)(k); ++i) n     for (int j = i; j < n; j += k) h[i] += p[j]; n   int z = (n + k - 1) / k; n   int a = (m + z - 1) / z, b = z - 1 ? m / (z - 1) : a; n   string an; n   for (int i = a; i < (int)(b + 1); ++i) { n     string t = gt(i); n     if (!t.empty() && (an.empty() || t < an)) an.swap(t); n   } n   if (an.empty()) no(); n   printf( %s n , an.c_str()); n } n


#include <bits/stdc++.h> n using namespace std; n template <class C> n void mini(C& _a4, C _b4) { n   _a4 = min(_a4, _b4); n } n template <class C> n void maxi(C& _a4, C _b4) { n   _a4 = max(_a4, _b4); n } n template <class TH> n void _dbg(const char* sdbg, TH h) { n   cerr << sdbg <<  =  << h << endl; n } n template <class TH, class... TA> n void _dbg(const char* sdbg, TH h, TA... a) { n   while (*sdbg !=  , ) cerr << *sdbg++; n   cerr <<  =  << h <<  , ; n   _dbg(sdbg + 1, a...); n } n template <class T> n ostream& operator<<(ostream& os, vector<T> V) { n   os <<  [ ; n   for (auto& vv : V) os << vv <<  , ; n   os <<  ] ; n   return os; n } n long long n, l; n long long war[401]; n string z[401]; n map<string, long long> ma; n long long il = 0; n void add(string x) { n   auto xx = ma.find(x); n   if (xx == ma.end()) { n     (x, il); n     ma[x] = il; n     il++; n   } n } n long long szu(string z) { n   while (1) { n     auto xx = ma.find(z); n     if (xx != ma.end()) { n       return xx->second; n     } n     z = z.substr(1); n   } n } n long long ww[401]; n long long t[401][401], pom2[401][401], res[401], pom[401]; n int32_t main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout << fixed << setprecision(11); n   if (0) cout << fixed << setprecision(6); n   cin >> n >> l; n   for (long long i = (0); i <= ((long long)(n)-1); i++) { n     cin >> war[i]; n   } n   for (long long i = (0); i <= ((long long)(n)-1); i++) { n     cin >> z[i]; n     for (long long j = (0); n          j <= ((long long)(((long long)(z[i]).size()) + 1) - 1); j++) { n       add(z[i].substr(0, j)); n     } n   } n   for (long long i = (0); i <= ((long long)(n)-1); i++) { n     for (auto& x : ma) { n       if (((long long)(z[i]).size()) > ((long long)(x.first).size())) continue; n       if (z[i] == x.first.substr(((long long)(x.first).size()) - n                                      ((long long)(z[i]).size()), n                                  ((long long)(z[i]).size()))) n         ww[x.second] += war[i]; n     } n   } n   for (long long i = (0); i <= ((long long)(il)-1); i++) n     for (long long j = (0); j <= ((long long)(il)-1); j++) t[i][j] = -1e18; n   for (auto& x : ma) { n     for (char k =  a ; k <=  z ; k++) { n       string pom = x.first + k; n       long long gd = szu(pom); n       maxi(t[x.second][gd], ww[gd]); n     } n   } n   for (long long i = (0); i <= ((long long)(il)-1); i++) res[i] = -1e18; n   res[0] = 0; n   while (l) { n     if (l & 1) { n       for (long long i = (0); i <= ((long long)(il)-1); i++) pom[i] = -1e18; n       for (long long i = (0); i <= ((long long)(il)-1); i++) n         for (long long j = (0); j <= ((long long)(il)-1); j++) n           maxi(pom[j], res[i] + t[i][j]); n       for (long long i = (0); i <= ((long long)(il)-1); i++) res[i] = pom[i]; n     } n     l /= 2; n     for (long long i = (0); i <= ((long long)(il)-1); i++) n       for (long long j = (0); j <= ((long long)(il)-1); j++) pom2[i][j] = -1e18; n     for (long long i = (0); i <= ((long long)(il)-1); i++) n       for (long long j = (0); j <= ((long long)(il)-1); j++) n         for (long long k = (0); k <= ((long long)(il)-1); k++) n           maxi(pom2[i][k], t[i][j] + t[j][k]); n     for (long long i = (0); i <= ((long long)(il)-1); i++) n       for (long long j = (0); j <= ((long long)(il)-1); j++) n         t[i][j] = pom2[i][j]; n   } n   long long out = 0; n   for (long long i = (0); i <= ((long long)(il)-1); i++) maxi(out, res[i]); n   cout << out <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n int32_t main() { n   ios::sync_with_stdio(false); n   cin.tie(nullptr); n   cout.tie(nullptr); n   long long n; n   cin >> n; n   string s; n   cin >> s; n   set<char> all; n   for (auto c : s) all.insert(c); n   const long long sz = all.size(); n   if (sz == 1) { n     cout <<  1 n ; n     return 0; n   } n   long long lo = 1; n   long long hi = n; n   long long a = n; n   while (lo <= hi) { n     long long mid = (lo + hi) / 2; n     map<char, long long> freq; n     bool poss = false; n     long long dist = 0; n     for (long long i = 0; i < mid; i++) { n       freq[s[i]]++; n       if (freq[s[i]] == 1) dist++; n     } n     for (long long i = mid; i < n; i++) { n       if (dist == sz) poss = true; n       if (--freq[s[i - mid]] == 0) dist--; n       if (++freq[s[i]] == 1) dist++; n       if (dist == sz) poss = true; n     } n     if (poss) { n       a = min(a, mid); n       hi = mid - 1; n     } else n       lo = mid + 1; n   } n   cout << a <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int k, n, m, ans = 0; n int dx[] = {0, 0, 0, 0, -1, 1}; n int dy[] = {0, 0, 1, -1, 0, 0}; n int dz[] = {1, -1, 0, 0, 0, 0}; n char a[11][11][11]; n bool vis[11][11][11]; n void dfs(int x, int y, int z) { n   if (x < 0 || y < 0 || z < 0 || x >= n || y >= m || z >= k || n       a[x][y][z] ==  #  || vis[x][y][z]) n     return; n   vis[x][y][z] = 1; n   ans++; n   int xx, yy, zz; n   for (int i = 0; i < 6; i++) { n     xx = x + dx[i]; n     yy = y + dy[i]; n     zz = z + dz[i]; n     dfs(xx, yy, zz); n   } n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cin >> k >> n >> m; n   for (int t = 0; t < k; t++) { n     for (int i = 0; i < n; i++) { n       for (int j = 0; j < m; j++) { n         cin >> a[i][j][t]; n       } n     } n   } n   int x, y; n   cin >> x >> y; n   dfs(x - 1, y - 1, 0); n   cout << ans; n } n


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


#include <bits/stdc++.h> n using namespace std; n const int N = 2e5 + 5; n int n, ans; n struct Node { n   int val, p, cnt; n   int pre, nxt; n } a[N]; n struct Comp { n   bool operator()(const pair<int, int> &x, const pair<int, int> &y) { n     return x.first == y.first ? x.second < y.second : x.first > y.first; n   } n }; n set<pair<int, int>, Comp> st; n void solve() { n   scanf( %d , &n); n   int p = -1, k = 0; n   for (int i = 0; i < n; i++) { n     int tmp; n     scanf( %d , &tmp); n     if (tmp != k) { n       if (p != -1) a[p].nxt = i; n       a[i].pre = p; n       k = tmp; n       p = i; n       a[i].p = i; n       a[i].val = tmp; n       a[i].nxt = -1; n       a[i].cnt = 1; n     } else { n       a[p].cnt++; n     } n   } n   for (int i = 0; i < n; i++) n     if (a[i].cnt) st.insert(make_pair(a[i].cnt, i)); n   while (!st.empty()) { n     auto it = st.begin(); n     int pos = it->second; n     int pp = a[pos].pre, nn = a[pos].nxt; n     ans++; n     st.erase(it); n     if (pp != -1) a[pp].nxt = nn; n     if (nn != -1) a[nn].pre = pp; n     if (pp != -1 && nn != -1 && a[pp].val == a[nn].val) { n       int nnn = a[nn].nxt; n       a[nnn].pre = pp; n       a[pp].nxt = nnn; n       st.erase(st.find(make_pair(a[nn].cnt, nn))); n       st.erase(st.find(make_pair(a[pp].cnt, pp))); n       a[pp].cnt += a[nn].cnt; n       st.insert(make_pair(a[pp].cnt, pp)); n     } n   } n   printf( %d n , ans); n } n int main() { n   solve(); n   return 0; n } n


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


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long ele, i, j, ele1, k, n, a, b, q; n   cin >> q; n   for (i = 0; i < q; i++) { n     cin >> n >> a >> b; n     if (n == 1) { n       cout << a << endl; n       continue; n     } else if (2 * a > b) { n       ele = n % 2; n       ele1 = n / 2; n       if (ele == 0) { n         cout << ele1 * b << endl; n       } else { n         cout << ele1 * b + a << endl; n       } n     } else if (2 * a <= b) { n       cout << n * a << endl; n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n void solve(string a, string b, int m, int n) { n   int lcs[m + 1][n + 1]; n   int result = 0; n   for (int i = 0; i <= m; i++) { n     for (int j = 0; j <= n; j++) { n       if (i == 0 || j == 0) n         lcs[i][j] = 0; n       else if (a[i - 1] == b[j - 1]) { n         lcs[i][j] = lcs[i - 1][j - 1] + 1; n         result = max(result, lcs[i][j]); n       } else n         lcs[i][j] = 0; n     } n   } n   int answer = m + n - 2 * result; n   cout << answer <<   n ; n } n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   cin.exceptions(ios::badbit | ios::failbit); n   int t = 1; n   cin >> t; n   while (t--) { n     int n; n     cin >> n; n     string s, temp; n     cin >> s; n     temp = s; n     sort(s.begin(), s.end()); n     int count = 0; n     for (int i = 0; i < n; i++) { n       if (temp[i] != s[i]) count++; n     } n     cout << count <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n pair<long long, long long> add_pair(pair<long long, long long> a, n                                     pair<long long, long long> b) { n   return make_pair(a.first + b.first, a.second + b.second); n } n long long power(long long a, long long b) { n   long long res = 1; n   a = a % ((long long)1e9 + 7); n   while (b > 0) { n     if (b & 1) { n       res = (res * a) % ((long long)1e9 + 7); n       b--; n     } n     a = (a * a) % ((long long)1e9 + 7); n     b >>= 1; n   } n   return res; n } n long long fermat_inv(long long y) { return power(y, ((long long)1e9 + 7) - 2); } n long long gcd(long long a, long long b) { return (b == 0) ? a : gcd(b, a % b); } n long long mul(long long a, long long b) { n   return (a * (b % ((long long)1e9 + 7))) % ((long long)1e9 + 7); n } n long long sub(long long a, long long b) { n   return ((a - b) % ((long long)1e9 + 7) + ((long long)1e9 + 7)) % n          ((long long)1e9 + 7); n } n long long add(long long a, long long b) { n   return ((a + b) % ((long long)1e9 + 7) + ((long long)1e9 + 7)) % n          ((long long)1e9 + 7); n } n vector<vector<int> > G; n int n, ans; n void bfs(int rt) { n   int x; n   queue<int> q; n   vector<int> d(n), par(n); n   vector<bool> vis(n, false); n   d[rt] = 0; n   par[rt] = rt; n   q.push(rt); n   while (!q.empty()) { n     x = q.front(); n     vis[x] = true; n     q.pop(); n     for (auto it = (G[x]).begin(); it != (G[x]).end(); it++) { n       if (!vis[*it]) { n         d[*it] = d[x] + 1; n         par[*it] = x; n         vis[*it] = true; n         q.push(*it); n       } else { n         if (*it != par[x] && par[*it] != x) { n           ans = min(ans, d[x] + d[*it] + 1); n           if (ans == 3) return; n         } n       } n     } n   } n } n void solve() { n   int i, j; n   long long x; n   cin >> n; n   vector<long long> a; n   vector<int> cnt(61, 0ll); n   for (i = (0); i < (n); i += (1)) { n     cin >> x; n     if (x == 0) continue; n     a.push_back(x); n     for (j = 0; j < 61; j++) { n       if (x & (1ll << j)) { n         cnt[j]++; n       } n     } n   } n   for (i = (0); i < (61); i += (1)) { n     if (cnt[i] > 2) { n       cout << 3 <<   n ; n       return; n     } n   } n   n = (a).size(); n   G = vector<vector<int> >(n, vector<int>()); n   ans = n + 1; n   for (i = (0); i < (n); i += (1)) { n     for (j = (0); j < (i); j += (1)) { n       if (a[i] & a[j]) { n         G[i].push_back(j); n         G[j].push_back(i); n       } n     } n   } n   for (i = (0); i < (n); i += (1)) { n     bfs(i); n     if (ans == 3) break; n   } n   if (ans == n + 1) n     cout << -1 <<   n ; n   else n     cout << ans <<   n ; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   long long t; n   t = 1; n   while (t--) { n     solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int max_n = 2000222, inf = 1000111222; n int t, n, a[max_n], b[max_n]; n long long sum[max_n]; n int main() { n   scanf( %d , &t); n   while (t--) { n     scanf( %d , &n); n     for (int i = 0; i < n; ++i) { n       scanf( %d , &a[i]); n     } n     for (int i = 0; i < n; ++i) { n       scanf( %d , &b[i]); n     } n     for (int i = n; i < 2 * n; ++i) { n       a[i] = a[i - n]; n       b[i] = b[i - n]; n     } n     a[2 * n] = b[2 * n] = 0; n     for (int i = 0; i <= 2 * n; ++i) { n       sum[i] = a[i] - b[i]; n       if (i) { n         sum[i] += sum[i - 1]; n       } n     } n     bool ok = sum[n - 1] <= 0; n     long long mx = sum[2 * n]; n     for (int i = 2 * n - 1; i >= 0; --i) { n       long long res = mx - sum[i] - b[i]; n       if (res > 0) { n         ok = false; n         break; n       } n       mx = max(mx, sum[i]); n     } n     if (ok) { n       puts( YES ); n     } else { n       puts( NO ); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n #define int long long n const int MAX = 1e6 + 7; n int num[MAX]; n signed main() n { n  int T; n  cin >> T; n  while (T--) n  { n   int N, u, v; n   cin >> N >> u >> v; n   for (int i = 1; i <= N; i++) n   { n    cin >> num[i]; n   } n   int canget = 0; n   int flag = 0; n   for (int i = 1; i < N; i++) n   { n    if (abs(num[i] - num[i + 1]) > 1) n    { n     canget = 1; n     cout << 0 <<   n ; n     flag = 1; n     break; n    } n   } n   if (flag == 1) n   { n    continue; n   } n   if (canget) n   { n    continue; n   } n   int ban = 0; n   for (int i = 1; i < N; i++) n   { n    if (num[i] != num[i + 1]) n    { n     ban = 1; n    } n   } n   if (ban) n   { n    cout << min(u, v) <<   n ; n   } n   else n   { n    cout << min(u + v, v * 2) <<   n ; n   } n  } n  return 0; n }


#include <bits/stdc++.h> n using namespace std; n const long long MOD = 1e9 + 7; n long long lgput(long long a, long long b, long long mod) { n   long long ret = 1; n   while (b) { n     if (b & 1) ret = (ret * a) % mod; n     a = (a * a) % mod; n     b >>= 1; n   } n   return (ret % mod); n } n inline long long inv(long long x, long long MOD) { n   return lgput(x, MOD - 2, MOD); n } n const long double PI = acos(-1); n const long double eps = 1e-6; n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   cout.precision(12); n   cout.fixed; n   int n; n   cin >> n; n   vector<int> a(n); n   vector<int> b(n); n   for (int i = 0; i < n; ++i) cin >> a[i] >> b[i]; n   vector<int> order(n); n   for (int i = 0; i < n; ++i) order[i] = i; n   sort(order.begin(), order.end(), n        [&](int i, int j) -> bool { return a[i] + b[j] > a[j] + b[i]; }); n   long long ret = 0; n   for (int i = 0; i < n; ++i) { n     ret += 1ll * i * a[order[i]] + 1ll * (n - 1 - i) * b[order[i]]; n   } n   cout << ret <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n map<string, int> M; n int eval(string s) { n   int c1 = 0, c2 = 0; n   string t; n   int indl = 0; n   while (indl < s.size() && s[indl] ==  & ) ++indl, ++c1; n   int ind2 = s.size() - 1; n   while (ind2 >= 0 && s[ind2] ==  * ) --ind2, ++c2; n   t = s.substr(indl, ind2 - indl + 1); n   if (!M.count(t)) return -1; n   int x = M[t]; n   if (x == -1) return -1; n   x += c2; n   x -= c1; n   if (x < 0) return -1; n   return x; n } n int main() { n   int n; n   cin >> n; n   M[ void ] = 0; n   for (int i = 0; i < n; ++i) { n     string ws; n     cin >> ws; n     if (ws ==  typeof ) { n       string s; n       cin >> s; n       int x = eval(s); n       if (x == -1) n         printf( errtype n ); n       else { n         printf( void ); n         for (int j = 0; j < x; ++j) printf( * ); n         printf(  n ); n       } n     } else { n       string s, t; n       cin >> s >> t; n       M[t] = eval(s); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n double a[10100]; n int main() { n   int n, m; n   cin >> n >> m; n   double ans = 0.0; n   for (int i = 1; i <= n; i++) cin >> a[i]; n   for (int i = 1; i <= m; i++) { n     int aa, bb; n     double cc; n     cin >> aa >> bb >> cc; n     ans = max((a[aa] + a[bb]) / cc, ans); n   } n   cout << fixed << setprecision(15) << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int dx[] = {-1, 1, 0, 0}; n int dy[] = {0, 0, -1, 1}; n int n, m; n vector<long long> a; n void solve() { n   long long cnt = 0; n   vector<vector<pair<int, int>>> ps; n   queue<pair<int, int>> buff; n   ps.resize(m); n   for (int i = 0; i < m; i++) ps[i].clear(); n   for (int i = 0; i < n; i++) ps[a[i] % m].push_back({a[i], i}); n   for (int i = 0; i < m; i++) n     if ((int)ps[i].size() > n / m) { n       for (int j = 0; j < m + m; j++) { n         int cur = (i + j) % m; n         while ((int)ps[cur].size() > n / m) { n           pair<int, int> v = ps[cur].back(); n           ps[cur].pop_back(); n           buff.push(v); n         } n         while ((int)ps[cur].size() < n / m && (int)buff.size() > 0) { n           pair<int, int> v = buff.front(); n           buff.pop(); n           int d = (cur + m - (v.first % m)) % m; n           v.first += (long long)d; n           ps[cur].push_back(v); n           cnt += (long long)d; n         } n       } n       break; n     } n   vector<long long> res; n   res.resize(n); n   for (int i = 0; i < m; i++) { n     assert((int)ps[i].size() == n / m); n     for (pair<int, int> j : ps[i]) { n       res[j.second] = j.first; n     } n   } n   cout << cnt << endl; n   for (int i = 0; i < n; i++) { n     if (i < n - 1) n       cout << res[i] <<    ; n     else n       cout << res[i] << endl; n   } n } n int main() { n   cin >> n >> m; n   a.resize(n); n   for (int i = 0; i < n; i++) cin >> a[i]; n   solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n bool clash[30][30]; n int dp[100001][27]; n string s; n int solve(int index, int lastchar) { n   if (index == s.size()) { n     return 0; n   } n   int mn = (1 << 30); n   if (dp[index][lastchar] != -1) { n     return dp[index][lastchar]; n   } n   if (!clash[s[index] -  a ][lastchar]) n     mn = min(mn, solve(index + 1, s[index] -  a )); n   mn = min(mn, solve(index + 1, lastchar) + 1); n   return dp[index][lastchar] = mn; n } n int main() { n   cin >> s; n   int n; n   cin >> n; n   string s2; n   int sum = 0; n   memset(dp, -1, sizeof(dp)); n   for (int i = 0; i < n; i++) { n     cin >> s2; n     clash[s2[0] -  a ][s2[1] -  a ] = true; n     clash[s2[1] -  a ][s2[0] -  a ] = true; n   } n   cout << solve(0, 26) << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n bool _cmp(pair<int, int> a, pair<int, int> b) { return a.second < b.second; } n int main() { n   int n, x, y; n   scanf( %d , &n); n   vector<pair<int, int> > V(n); n   for (int i = 0; i < n; i++) { n     scanf( %d %d , &x, &y); n     V[i].first = min(x, y); n     V[i].second = max(x, y); n   } n   sort(V.begin(), V.end(), _cmp); n   bool U[n]; n   memset(U, false, sizeof U); n   vector<int> Res; n   for (int i = 0; i < n; i++) { n     if (U[i]) continue; n     Res.push_back(V[i].second); n     for (int j = i + 1; j < n; j++) n       if (V[j].first <= V[i].second) U[j] = true; n   } n   printf( %d n , Res.size()); n   for (int i = 0; i < Res.size(); i++) n     printf( %s%d , (i == 0 ?    :    ), Res[i]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct Q { n   int a, b, c, d; n   Q(int x, int y, int z, int w) : a(x), b(y), c(z), d(w) {} n }; n pair<int, int> query(Q z) { n   int u, v; n   printf( %d%d%d%d n , z.a, z.b, z.c, z.d); n   fflush(stdout); n   scanf( %d%d , &u, &v); n   return make_pair(u, v); n } n pair<int, int> dist(Q e, Q f) { n   int bulls = 0, cows = 0; n   if (e.a == f.a) bulls++; n   if (e.b == f.b) bulls++; n   if (e.c == f.c) bulls++; n   if (e.d == f.d) bulls++; n   if ((e.a == f.a) || (e.a == f.b) || (e.a == f.c) || (e.a == f.d)) cows++; n   if ((e.b == f.a) || (e.b == f.b) || (e.b == f.c) || (e.b == f.d)) cows++; n   if ((e.c == f.a) || (e.c == f.b) || (e.c == f.c) || (e.c == f.d)) cows++; n   if ((e.d == f.a) || (e.d == f.b) || (e.d == f.c) || (e.d == f.d)) cows++; n   return make_pair(bulls, cows - bulls); n } n int main() { n   vector<Q> X; n   for (int i = 0; i < 10; ++i) n     for (int j = 0; j < 10; ++j) n       if (i - j) { n         for (int k = 0; k < 10; ++k) n           if ((i - k) && (j - k)) { n             for (int l = 0; l < 10; ++l) n               if ((i - l) && (j - l) && (k - l)) { n                 Q h(i, j, k, l); n                 X.push_back(h); n               } n           } n       } n   int bulls = 0, cows = 0; n   while (bulls < 4) { n     pair<int, int> Z = query(X[0]); n     bulls = Z.first; n     cows = Z.second; n     vector<Q> Y; n     Q V = X[0]; n     for (int i = 0; i < X.size(); ++i) { n       pair<int, int> T = dist(V, X[i]); n       if (T.first == bulls && T.second == cows) Y.push_back(X[i]); n     } n     X = Y; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long n, A, cf, cm, m; n struct node { n   long long x; n   int i; n } a[100001]; n long long sum[100001]; n long long ans = 0; n bool cmp(node a, node b) { return a.x < b.x; } n bool cmp1(node a, node b) { return a.i < b.i; } n long long minc = 0; n long long bsearch(long long x) { n   int l = 0; n   int r = n; n   while (l < r) { n     int mid = l + (r - l) / 2; n     if (a[mid].x > x) n       r = mid; n     else n       l = mid + 1; n   } n   return l; n } n void solve() { n   for (int i = 0; i < n; ++i) { n     long long num = m - (A * (long long)i - (sum[n - 1] - sum[n - 1 - i])); n     if (num < 0) break; n     long long l = a[0].x - 1; n     long long r = A; n     while (l < r) { n       long long mid = l + (r - l) / 2; n       long long k = bsearch(mid) - 1; n       if (k + i >= n) k = n - 1 - i; n       long long sum1 = (mid * (k + 1) - sum[k]); n       if (sum1 > num) n         r = mid; n       else n         l = mid + 1; n     } n     l--; n     if (ans <= (long long)i * cf + (long long)l * cm) { n       ans = (long long)i * cf + (long long)l * cm; n       minc = l; n     } n   } n } n int main() { n   while (cin >> n >> A >> cf >> cm >> m) { n     for (int i = 0; i < n; ++i) { n       scanf( %lld , &a[i].x); n       a[i].i = i; n     } n     a[n].x = A + 1; n     a[n].i = n; n     ans = 0; n     sort(a, a + n, cmp); n     sum[0] = a[0].x; n     for (int i = 1; i < n; ++i) sum[i] = sum[i - 1] + a[i].x; n     if (A * n <= sum[n - 1] + m) { n       ans = n * cf + A * cm; n       minc = A; n     } else { n       solve(); n     } n     for (int i = 0; i < n; ++i) { n       if (a[i].x < minc) { n         m -= minc - a[i].x; n         a[i].x = minc; n       } else n         break; n     } n     for (int i = n - 1; i >= 0; --i) { n       if (A - a[i].x <= m) { n         m -= A - a[i].x; n         a[i].x = A; n       } else { n         a[i].x += m; n         m = 0; n         break; n       } n     } n     sort(a, a + n, cmp1); n     printf( %lld n , ans); n     for (int i = 0; i < n; ++i) { n       if (i == n - 1) n         printf( %lld n , a[i].x); n       else n         printf( %lld  , a[i].x); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, z[100005], a[100005], ans[100005]; n string s; n int main() { n   int i, l = 0, r = 0; n   cin >> s; n   n = s.length(); n   memset(z, 0, sizeof(z)); n   memset(ans, 0, sizeof(ans)); n   ans[1] = 1; n   for (i = 1; i < n; i++) { n     if (i <= r) z[i] = min(z[i - l], r - i + 1); n     while (i + z[i] < n && s[i + z[i]] == s[z[i]]) z[i]++; n     if (i + z[i] - 1 > r) r = i + z[i] - 1, l = i; n     ans[1]++; n     ans[z[i] + 1]--; n   } n   for (i = 1; i <= n; i++) ans[i] += ans[i - 1]; n   a[0] = 0; n   for (i = 1; i <= n; i++) n     if (z[n - i] == i) a[++a[0]] = i; n   a[++a[0]] = n; n   printf( %d n , a[0]); n   for (i = 1; i <= a[0]; i++) printf( %d %d n , a[i], ans[a[i]]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAX_L = 200; n map<int, pair<int, int> > ds; n int ops[MAX_L]; n int powmod(int a, int n, int mod) { n   int pm = 1; n   while (n > 0) { n     if (n & 1) pm = (long long)pm * a % mod; n     a = (long long)a * a % mod; n     n >>= 1; n   } n   return pm; n } n bool check(int v, int u, int uc, queue<int> &q, int &mst, int &mgl) { n   map<int, pair<int, int> >::iterator mit = ds.find(v); n   if (mit != ds.end()) { n     if (uc * mit->second.first < 0) { n       if (uc > 0) n         mst = u, mgl = v; n       else n         mst = v, mgl = u; n       return true; n     } n   } else { n     ds[v] = pair<int, int>(uc, u); n     q.push(v); n   } n   return false; n } n int getop(int u, int v, int p) { n   if ((u + 1) % p == v) return 1; n   if ((u + p - 1) % p == v) return 2; n   return 3; n } n int main() { n   int st, gl, p; n   scanf( %d%d%d , &st, &gl, &p); n   if (st == gl) { n     puts( 2 n1 2 ); n     return 0; n   } n   ds[st] = pair<int, int>(1, -1); n   ds[gl] = pair<int, int>(-1, -1); n   queue<int> q; n   q.push(st), q.push(gl); n   int mst = -1, mgl = -1; n   while (!q.empty()) { n     int u = q.front(); n     q.pop(); n     pair<int, int> ud = ds[u]; n     int uc = ud.first, up = ud.second; n     if (check((u + 1) % p, u, uc, q, mst, mgl) || n         check((u + p - 1) % p, u, uc, q, mst, mgl) || n         check(powmod(u, p - 2, p), u, uc, q, mst, mgl)) n       break; n   } n   vector<int> vst, vgl; n   for (int u = mst; u >= 0; u = ds[u].second) vst.push_back(u); n   for (int u = mgl; u >= 0; u = ds[u].second) vgl.push_back(u); n   int l = 0; n   for (int i = vst.size() - 1; i > 0; i--) n     ops[l++] = getop(vst[i], vst[i - 1], p); n   ops[l++] = getop(mst, mgl, p); n   for (int i = 0; i + 1 < vgl.size(); i++) n     ops[l++] = getop(vgl[i], vgl[i + 1], p); n   printf( %d n , l); n   for (int i = 0; i < l; i++) printf( %d%c , ops[i], (i + 1 < l) ?     :   n ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 1E5; n int a[MAXN]; n long long pref[MAXN]; n long long suff[MAXN]; n int main() { n   int n, d, m; n   cin >> n >> d >> m; n   int bigger = 0; n   for (int i = 0; i < n; i++) { n     cin >> a[i]; n     if (a[i] > m) bigger++; n   } n   sort(a, a + n); n   long long ans = 0; n   for (int i = 0; i < n; i++) { n     if (i) n       pref[i] = pref[i - 1] + a[i]; n     else n       pref[i] = a[i]; n   } n   if (bigger == 0) { n     ans = 0; n     for (int i = 0; i < n; i++) ans += a[i]; n     cout << ans <<   n ; n     return 0; n   } n   long long lowSum = 0; n   for (int i = 0; i < n; i++) { n     if (a[i] <= m) lowSum += a[i]; n   } n   for (int i = n - 1; i >= 0; i--) { n     if (i == n - 1) n       suff[i] = a[i]; n     else n       suff[i] = a[i] + suff[i + 1]; n   } n   int atLeast = (n + d) / (d + 1); n   for (int i = 1; i <= min(bigger, atLeast); i++) { n     int minShadow = max((i - 1) * d, bigger - i); n     int maxShadow = min(i * d, n - i); n     if (minShadow > maxShadow) continue; n     long long temp = 0; n     if (minShadow - (bigger - i) - 1 >= 0) n       temp = suff[n - i] + lowSum - pref[minShadow - (bigger - i) - 1]; n     else n       temp = suff[n - i] + lowSum; n     ans = max(ans, temp); n   } n   cout << ans <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n int main() { n   int n; n   int k; n   char a[101]; n   int i = 0; n   scanf( %d %d , &n, &k); n   scanf( %s , a); n   if (k > n / 2) { n     i = k; n     while (i < n) { n       printf( RIGHT n ); n       i++; n     } n   } else { n     i = k; n     while (i > 1) { n       printf( LEFT n ); n       i--; n     } n   } n   if (i == 1) { n     i--; n     printf( PRINT %c n , a[i]); n     i++; n     while (i < n) { n       printf( RIGHT n ); n       printf( PRINT %c n , a[i]); n       i++; n     } n   } else { n     printf( PRINT %c n , a[i - 1]); n     i--; n     while (i > 0) { n       printf( LEFT n ); n       printf( PRINT %c n , a[i - 1]); n       i--; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n typedef int arr[500005]; n arr Next, a, Sa, Lcp, pos, tmp, S; n int n, len; n char P[500005]; n vector<int> Val[2 * 500005]; n bool cmp(int p, int q) { n   if (pos[p] != pos[q]) return pos[p] < pos[q]; n   p += len; n   q += len; n   if (p <= n && q <= n) return pos[p] < pos[q]; n   return p > q; n } n void BuildSA() { n   for (int i = 1; i <= n; i++) { n     Sa[i] = i; n     pos[i] = ((P[i] ==  ( ) ? 1 : 2); n   } n   for (len = 1; len <= n; len *= 2) { n     sort(Sa + 1, Sa + n + 1, cmp); n     for (int i = 1; i <= n; i++) tmp[i] = tmp[i - 1] + cmp(Sa[i - 1], Sa[i]); n     for (int i = 1; i <= n; i++) pos[Sa[i]] = tmp[i]; n   } n } n void BuildLCP() { n   int cur = 0; n   for (int i = 1; i <= n; i++) { n     int j = Sa[pos[i] - 1]; n     if (j == 0) continue; n     while (P[i + cur] == P[j + cur]) cur++; n     Lcp[pos[i]] = cur; n     if (cur) cur--; n   } n } n int Get(int V, int L, int R) { n   int l = lower_bound(Val[V].begin(), Val[V].end(), L) - Val[V].begin(); n   int r = upper_bound(Val[V].begin(), Val[V].end(), R) - Val[V].begin(); n   return max(r - l, 0); n } n int main() { n   cin >> n; n   for (int i = 1; i <= n; i++) { n     cin >> P[i]; n     a[i] = a[i - 1]; n     if (P[i] ==  ( ) a[i]++; n     if (P[i] ==  ) ) a[i]--; n     Val[a[i] + 500005].push_back(i); n   } n   int top = 0; n   S[0] = n + 1; n   for (int i = n; i >= 0; i--) { n     while (a[i] <= a[S[top]] && top) top--; n     Next[i] = S[top]; n     S[++top] = i; n   } n   BuildSA(); n   BuildLCP(); n   long long ans = 0; n   for (int i = 1; i <= n; i++) { n     int L = Sa[i] + Lcp[i]; n     int R = Next[Sa[i] - 1] - 1; n     ans += Get(a[Sa[i]] - 1 + 500005, L, R); n   } n   cout << ans; n } n


#include <bits/stdc++.h> n using namespace std; n int n; n pair<long long, long long> x[4000]; n long long d[4000]; n int main() { n   scanf( %d , &n); n   for (int i = 1; i <= n; i++) cin >> x[i].first >> x[i].second; n   sort(x + 1, x + n + 1); n   d[n] = x[n].second; n   for (int i = n - 1; i >= 1; i--) { n     d[i] = 1000000000000000000LL; n     long long s = 0; n     for (int j = i + 1; j <= n; j++) n       d[i] = min(d[i], s + d[j]), s += x[j].first - x[i].first; n     d[i] = min(d[i], s); n     d[i] += x[i].second; n   } n   cout << d[1] << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); n const long long N = 2e5 + 100; n const long long INF = 1e18; n long long x[N], p[N], a[N]; n struct node { n   long long ans = 0, pref = 0, suff = 0, sum = 0; n   node() {} n   node(long long x) { n     pref = suff = sum = x; n     ans = max(x, 0ll); n   } n } t[4 * N]; n node merge(node l, node r) { n   node ret; n   ret.ans = max(l.ans, r.ans); n   ret.pref = max(l.pref, l.sum + r.pref); n   ret.suff = max(r.suff, r.sum + l.suff); n   ret.sum = l.sum + r.sum; n   ret.ans = max({ret.ans, ret.pref, ret.suff}); n   ret.ans = max(ret.ans, l.suff + r.pref); n   return ret; n } n void build(long long v, long long l, long long r) { n   if (l + 1 == r) { n     t[v] = node(a[l]); n     return; n   } n   long long mid = (l + r) / 2; n   build(v * 2 + 1, l, mid); n   build(v * 2 + 2, mid, r); n   t[v] = merge(t[v * 2 + 1], t[v * 2 + 2]); n } n node qu(long long v, long long l, long long r, long long ql, long long qr) { n   if (qr <= l || r <= ql) return node(); n   if (ql <= l && r <= qr) return t[v]; n   long long mid = (l + r) / 2; n   return merge(qu(v * 2 + 1, l, mid, ql, qr), qu(v * 2 + 2, mid, r, ql, qr)); n } n signed main() { n   ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); n   long long n, m, c; n   cin >> n >> m >> c; n   for (long long i = 0; i < n; i++) { n     cin >> x[i]; n   } n   n--; n   for (long long i = 0; i < n; i++) { n     x[i] = 50 * (x[i + 1] - x[i]); n   } n   for (long long i = 0; i < n; i++) { n     cin >> p[i]; n   } n   for (long long i = 0; i < n; i++) { n     a[i] = (x[i] - p[i] * c); n   } n   build(0, 0, n); n   long long ans = 0; n   for (long long i = 0; i < m; i++) { n     long long l, r; n     cin >> l >> r; n     l--; n     r--; n     ans += qu(0, 0, n, l, r).ans; n   } n   cout << fixed << setprecision(20) << ans / 100.0 <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n const long long mod = 1000000007; n const int N = 1e5 + 9; n vector<int> v[N]; n bool visited[N]; n int c = 0; n map<pair<int, int>, int> m; n void dfs(int x, int parent) { n   visited[x] = true; n   bool b = false; n   for (int i = 0; i < v[x].size(); i++) { n     if (!visited[v[x][i]]) { n       b = true; n       dfs(v[x][i], x); n     } n   } n   if (!b) m[{parent, x}] = c++; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   ; n   int x; n   cin >> x; n   int l[x - 1], r[x - 1]; n   for (int i = 0; i < x - 1; i++) { n     cin >> l[i] >> r[i]; n     v[l[i]].push_back(r[i]); n     v[r[i]].push_back(l[i]); n   } n   dfs(l[0], l[0]); n   for (int i = 0; i < x - 1; i++) { n     if (m.find({l[i], r[i]}) != m.end()) n       cout << m[{l[i], r[i]}] <<   n ; n     else if (m.find({r[i], l[i]}) != m.end()) n       cout << m[{r[i], l[i]}] <<   n ; n     else n       cout << c++ <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   cin >> n; n   cout <<  0 0  << endl; n   string left; n   cin >> left; n   int l = 0, r = 1e9; n   for (int i = 1; i < n; i++) { n     int mid = (l + r) / 2; n     cout << mid <<   1  << endl; n     string s; n     cin >> s; n     if (s == left) { n       l = mid; n     } else { n       r = mid; n     } n   } n   cout << l <<   2   << r <<   0  << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main(int argc, const char* argv[]) { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   int a, b, c, x, y, z; n   int spa = 0, spb = 0, spc = 0; n   int req = 0; n   cin >> a >> b >> c; n   cin >> x >> y >> z; n   if (a > x) n     spa = a - x; n   else n     req += x - a; n   if (b > y) n     spb = b - y; n   else n     req += y - b; n   if (c > z) n     spc = c - z; n   else n     req += z - c; n   if (spa > 0) req = req - spa / 2; n   if (spb > 0) req = req - spb / 2; n   if (spc > 0) req = req - spc / 2; n   if (req <= 0) n     cout <<  Yes ; n   else n     cout <<  No ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n bool comp(pair<long long int, long long int> a, n           pair<long long int, long long int> b) { n   if (a.first + a.second == b.first + b.second) { n     return a.first < b.first; n   } n   return (a.first + a.second) < (b.first + b.second); n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   long long int q; n   cin >> q; n   while (q--) { n     long long int n; n     cin >> n; n     vector<pair<long long int, long long int> > A(n + 1, {0, 0}); n     for (long long int i = 0; i < n; i++) { n       long long int a, first; n       cin >> a >> first; n       if (first == 0) { n         A[a].second++; n       } else { n         A[a].first++; n       } n     } n     sort(A.rbegin(), A.rend(), comp); n     pair<long long int, long long int> sol = {A[0].first + A[0].second, n                                               A[0].first}; n     for (long long int i = 1; i < n + 1; i++) { n       if (A[i - 1].first + A[i - 1].second <= 1) break; n       if (A[i].first + A[i].second < A[i - 1].first + A[i - 1].second) { n         sol.first += A[i].first + A[i].second; n         sol.second += A[i].first; n       } else if (A[i].first + A[i].second >= A[i - 1].first + A[i - 1].second) { n         long long int second = A[i - 1].first + A[i - 1].second - 1; n         long long int ma = 0, mai = -1; n         for (long long int j = i; j < n + 1; j++) { n           if (second <= A[j].first + A[j].second) { n             if (A[j].first >= ma) { n               mai = j; n               ma = A[j].first; n             } n           } else { n             break; n           } n         } n         if (A[mai].first >= second) { n           A[mai].first = second; n           A[mai].second = 0; n           sol.first += second; n           sol.second += second; n         } else { n           second = second - A[mai].first; n           sol.first += A[mai].first; n           sol.second += A[mai].first; n           A[mai].second = second; n           sol.first += second; n         } n         swap(A[i], A[mai]); n       } n     } n     cout << sol.first <<     << sol.second <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long dp[101][101][11][2]; n long long k1, k2; n long long solve(long long n1, long long n2, long long count, long long flag) { n   if (n1 < 0 || n2 < 0) { n     return 0; n   } n   if (n1 == 0 && n2 == 0) { n     return 1; n   } n   if (flag != -1 && dp[n1][n2][count][flag] != -1) { n     return dp[n1][n2][count][flag]; n   } n   long long i; n   long long ans = 0; n   if (flag != 1) { n     for (i = 1; i <= k2; i++) { n       ans += solve(n1, n2 - i, i, 1); n       ans %= (long long)1e8; n     } n   } n   if (flag != 0) { n     for (i = 1; i <= k1; i++) { n       ans += solve(n1 - i, n2, i, 0); n       ans %= (long long)1e8; n     } n   } n   if (flag != -1) dp[n1][n2][count][flag] = ans; n   return ans; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   long long n1, n2; n   cin >> n1 >> n2 >> k1 >> k2; n   memset(dp, -1, sizeof(dp)); n   cout << solve(n1, n2, 0, -1); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n char x[55]; n int swim(char *c = x, int res = 0, int round = 0) { n   int l = strlen(c); n   if (round == 2) { n     if (l == 0 || l > 7 || (c[0] ==  0  && l > 1)) return -1; n     int ba2y; n     sscanf(c,  %d , &ba2y); n     if (ba2y > 1000000) return -1; n     return ba2y + res; n   } n   int ma3aya = 0; n   int nateg = -1; n   for (; c[0]; c++) { n     ma3aya *= 10; n     ma3aya += c[0] -  0 ; n     nateg = max(nateg, swim(c + 1, ma3aya + res, round + 1)); n     if (ma3aya == 0 || (ma3aya * 10) + (c[1]) -  0  > 1000000) break; n   } n   return nateg; n } n int main() { n   scanf( %s , &x); n   printf( %d , swim()); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main(int argc, const char* argv[]) { n   char a[4]; n   char b[4]; n   char x; n   int f = 0; n   int r = 0; n   for (int i = 0; i < 4; i++) { n     cin >> x; n     if (x !=  X ) { n       a[r] = x; n       r++; n     } else { n       if (i < 2) f = 1; n     } n   } n   if (f) { n     char t = a[1]; n     a[1] = a[2]; n     a[2] = t; n     f = 0; n   } n   r = 0; n   for (int i = 0; i < 4; i++) { n     cin >> x; n     if (x !=  X ) { n       b[r] = x; n       r++; n     } else { n       if (i < 2) f = 1; n     } n   } n   if (f) { n     char t = b[1]; n     b[1] = b[2]; n     b[2] = t; n   } n   int err = 0; n   int j = 0; n   for (int i = 0; i < 3; i++) { n     if (!i) { n       while (a[i] != b[j]) { n         j++; n       } n     } else { n       if (a[i] != b[j]) { n         err = 1; n         break; n       } n     } n     j = (j + 1) % 3; n   } n   if (!err) { n     cout <<  YES ; n   } else { n     cout <<  NO ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n void __print(int x) { cerr << x; } n void __print(long x) { cerr << x; } n void __print(long long x) { cerr << x; } n void __print(unsigned x) { cerr << x; } n void __print(unsigned long x) { cerr << x; } n void __print(unsigned long long x) { cerr << x; } n void __print(float x) { cerr << x; } n void __print(double x) { cerr << x; } n void __print(long double x) { cerr << x; } n void __print(char x) { cerr <<      << x <<     ; } n void __print(const char *x) { cerr <<     << x <<    ; } n void __print(const string &x) { cerr <<     << x <<    ; } n void __print(bool x) { cerr << (x ?  true  :  false ); } n template <typename T, typename V> n void __print(const pair<T, V> &x) { n   cerr <<  { ; n   __print(x.first); n   cerr <<  , ; n   __print(x.second); n   cerr <<  } ; n } n template <typename T> n void __print(const T &x) { n   int f = 0; n   cerr <<  { ; n   for (auto &i : x) cerr << (f++ ?  ,  :   ), __print(i); n   cerr <<  } ; n } n void _print() { cerr <<  ] n ; } n template <typename T, typename... V> n void _print(T t, V... v) { n   __print(t); n   if (sizeof...(v)) cerr <<  ,  ; n   _print(v...); n } n long long dp[31][31][51]; n long long calc(long long x, long long y, long long k) { n   if (dp[x][y][k] || !k || x * y == k) return dp[x][y][k]; n   long long ans = 1e9; n   for (long long i = 1; 2 * i <= x; i++) { n     for (long long j = 0; j <= k; j++) { n       ans = min(ans, y * y + calc(i, y, j) + calc(x - i, y, k - j)); n     } n   } n   for (long long i = 1; 2 * i <= y; i++) { n     for (long long j = 0; j <= k; j++) { n       ans = min(ans, x * x + calc(x, i, j) + calc(x, y - i, k - j)); n     } n   } n   return dp[x][y][k] = ans; n } n void solve() { n   long long t; n   cin >> t; n   while (t--) { n     long long n, m, k; n     cin >> n >> m >> k; n     cout << calc(n, m, k) <<   n ; n   } n } n signed main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   long long tests; n   tests = 1; n   for (long long i = 0; i < tests; i++) { n     solve(); n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int N = (int)2e5 + 45; n const int inf = 0x3f3f3f3f; n void ttf() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n } n int z[N], p[N]; n void solve() { n   string s; n   cin >> s; n   int n = (int)s.length(); n   z[0] = n; n   for (int i = 1, l = 0, r = 0; i < n; ++i) { n     if (i <= r) z[i] = min(r - i + 1, z[i - l]); n     while (i + z[i] < n && s[z[i]] == s[i + z[i]]) ++z[i]; n     if (i + z[i] - 1 > r) l = i, r = i + z[i] - 1; n   } n   for (int i = 0; i < n; ++i) { n     p[z[i]]++; n   } n   for (int i = n - 1; i >= 1; --i) { n     p[i] += p[i + 1]; n   } n   vector<pair<int, int> > v; n   for (int i = 0; i < n; ++i) { n     if (z[i] + i == n) { n       v.push_back(make_pair(z[i], p[z[i]])); n     } n   } n   cout << ((int)(v.size())) << endl; n   reverse(v.begin(), v.end()); n   for (int i = 0; i < ((int)(v.size())); ++i) n     cout << v[i].first <<     << v[i].second << endl; n } n int main() { n   ttf(); n   int t = 1; n   for (int i = 1; i <= t; i++) { n     solve(); n   } n   return 0; n } n


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


#include <bits/stdc++.h> n using namespace std; n const int N = 100010; n const int MOD = 1e9 + 7; n vector<int> v; n long long f[301][N]; n int a[N]; n int main() { n   for (int i = 1; i <= 300; i++) { n     f[i][0] = 1; n     for (int j = 1; j < N; j++) { n       f[i][j] = f[i][j - 1] * i % MOD; n     } n   } n   int n, x, sum; n   while (scanf( %d , &n) != EOF) { n     for (int i = 1; i <= n; i++) { n       scanf( %d , &x); n       a[x]++; n     } n     for (int i = 100000; i > 1; i--) a[i - 1] += a[i]; n     long long ans = 1; n     for (int i = 2; i <= 100000; i++) { n       if (a[i] == 0) continue; n       v.clear(); n       for (int j = 1; j * j <= i; j++) { n         if (i % j == 0) { n           v.push_back(j); n           if (j != i / j) v.push_back(i / j); n         } n       } n       sort(v.begin(), v.end()); n       int len = v.size(); n       long long ret1 = 1, ret2 = 1; n       sum = 0; n       for (int j = len - 1; j >= 0; j--) { n         if (a[v[j]] <= sum) continue; n         ret1 = ret1 * f[j + 1][a[v[j]] - sum] % MOD; n         sum = a[v[j]]; n       } n       sum = 0; n       for (int j = len - 2; j >= 0; j--) { n         if (a[v[j]] <= sum) continue; n         ret2 = ret2 * f[j + 1][a[v[j]] - sum] % MOD; n         sum = a[v[j]]; n       } n       ans = ((ans + ret1 - ret2) % MOD + MOD) % MOD; n     } n     printf( %I64d n , ans); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int N, M, cnt[101]; n int main() { n   int N, M, cnt[101] = {0}; n   scanf( %d%d , &N, &M); n   for (int i = 0; i < N; i++) { n     int t; n     scanf( %d , &t); n     int now = M - t; n     int ans = i; n     for (int j = 1; j <= 100; j++) { n       int get = min(now / j, cnt[j]); n       now -= get * j; n       ans -= get; n     } n     cnt[t]++; n     printf( %d%c , ans, i == N - 1 ? 10 : 32); n   } n } n


#include <bits/stdc++.h> n std::vector<long long> G[100001]; n std::vector<long long> psn[100001]; n std::vector<long long> arr[100001][20]; n long long par[100001], lvl[100001]; n long long P[100001][20]; n void dfs_visit(long long s, long long p) { n   if (p != -1) { n     par[s] = p; n     lvl[s] = 1 + lvl[p]; n   } n   for (long long i = 0, j = (long long)G[s].size(); i < j; i++) { n     if (G[s][i] != p) dfs_visit(G[s][i], s); n   } n   return; n } n std::vector<long long> merger(std::vector<long long> left, n                               std::vector<long long> right) { n   std::vector<long long> res; n   long long n1 = (long long)left.size(), n2 = (long long)right.size(); n   long long i = 0, j = 0, k = 0; n   while (i != n1 && j != n2 && k != 10) { n     if (left[i] < right[j]) { n       res.push_back(left[i]); n       i++; n     } else if (left[i] > right[j]) { n       res.push_back(right[j]); n       j++; n     } else { n       res.push_back(left[i]); n       i++; n       j++; n     } n     k++; n   } n   while (i != n1 && k != 10) { n     res.push_back(left[i]); n     i++; n     k++; n   } n   while (j != n2 && k != 10) { n     res.push_back(right[j]); n     j++; n     k++; n   } n   return res; n } n void ST_build(long long n) { n   for (long long i = 1; i <= n; i++) { n     for (long long j = 0; (1 << j) <= n; j++) P[i][j] = -1; n   } n   for (long long i = 1; i <= n; i++) { n     P[i][0] = par[i]; n     arr[i][0] = merger(psn[i], psn[par[i]]); n   } n   for (long long j = 1; (1 << j) <= n; j++) { n     for (long long i = 1; i <= n; i++) { n       if (P[i][j - 1] != -1) { n         P[i][j] = P[P[i][j - 1]][j - 1]; n         arr[i][j] = merger(arr[i][j - 1], arr[P[i][j - 1]][j - 1]); n       } n     } n   } n   return; n } n std::vector<long long> LCA_find(long long p, long long q) { n   std::vector<long long> res = merger(psn[p], psn[q]); n   if (lvl[p] < lvl[q]) { n     long long tmp = p; n     p = q; n     q = tmp; n   } n   long long lx = 0; n   for (lx = 1; (1 << lx) <= lvl[p]; lx++) n     ; n   lx--; n   for (long long i = lx; i >= 0; i--) { n     if ((lvl[p] - (1 << i)) >= lvl[q]) { n       res = merger(res, arr[p][i]); n       p = P[p][i]; n     } n   } n   if (p == q) return res; n   for (long long i = lx; i >= 0; i--) { n     if ((P[p][i] != -1) && (P[p][i] != P[q][i])) { n       res = merger(res, arr[p][i]); n       res = merger(res, arr[q][i]); n       p = P[p][i]; n       q = P[q][i]; n     } n   } n   res = merger(res, arr[p][0]); n   res = merger(res, arr[q][0]); n   return res; n } n int main(void) { n   std::ios_base::sync_with_stdio(false); n   std::cin.tie(NULL); n   std::cout.tie(NULL); n   long long n, m, q; n   std::cin >> n >> m >> q; n   for (long long i = 0; i < n - 1; i++) { n     long long s, d; n     std::cin >> s >> d; n     G[s].push_back(d); n     G[d].push_back(s); n   } n   for (long long i = 1; i <= m; i++) { n     long long t; n     std::cin >> t; n     if ((long long)psn[t].size() < 10) psn[t].push_back(i); n   } n   dfs_visit(1, -1); n   ST_build(n); n   while (q--) { n     long long v, u, a; n     std::cin >> v >> u >> a; n     std::vector<long long> ans = LCA_find(v, u); n     a = std::min(a, (long long)ans.size()); n     std::cout << a <<    ; n     for (long long i = 0; i < a; i++) std::cout << ans[i] <<    ; n     std::cout <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int arr[10001]; n int main() { n   int test; n   cin >> test; n   while (test--) { n     int n; n     cin >> n; n     vector<int> ans1, ans2; n     for (int i = 0; i < 2 * n; i++) cin >> arr[i]; n     vector<pair<int, int> > vp; n     for (int i = 0; i < 2 * n; i++) { n       if (arr[i] % 2 == 0) n         ans2.push_back(i + 1); n       else n         ans1.push_back(i + 1); n     } n     for (int i = 1; i < ans2.size(); i += 2) n       vp.push_back({ans2[i], ans2[i - 1]}); n     for (int i = 1; i < ans1.size(); i += 2) n       vp.push_back({ans1[i], ans1[i - 1]}); n     for (int i = 0; i < n - 1; i++) n       cout << vp[i].first <<     << vp[i].second <<   n ; n   } n } n


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


#include <bits/stdc++.h> n using namespace std; n using ll = long long; n using ld = long double; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(nullptr); n   ll t; n   cin >> t; n   for (ll i = 0; i < t; i++) { n     ll n; n     cin >> n; n     vector<ll> in(n); n     vector<ll> count(n + 1); n     for (ll i = 0; i < n; i++) { n       cin >> in[i]; n       count[in[i]]++; n     } n     vector<ll> ops; n     while (true) { n       ll mex = 0; n       while (count[mex] > 0) mex++; n       if (mex < n) { n         ops.push_back(mex); n         count[in[mex]]--; n         in[mex] = mex; n         count[mex]++; n       } else { n         ll j = 0; n         while (in[j] == j) j++; n         if (j >= n) break; n         ops.push_back(j); n         count[in[j]]--; n         in[j] = mex; n         count[mex]++; n       } n     } n     cout << (ll)(ops).size() << endl; n     for (ll x : ops) { n       cout << x + 1 <<    ; n     } n     cout << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T, typename U> n inline void amin(T &x, U y) { n   if (y < x) x = y; n } n template <typename T, typename U> n inline void amax(T &x, U y) { n   if (x < y) x = y; n } n struct UnionFind { n   vector<int> data; n   UnionFind(int size_) : data(size_, -1) {} n   bool unionSet(int x, int y) { n     x = root(x); n     y = root(y); n     if (x != y) { n       if (data[y] < data[x]) swap(x, y); n       data[x] += data[y]; n       data[y] = x; n     } n     return x != y; n   } n   bool findSet(int x, int y) { return root(x) == root(y); } n   int root(int x) { return data[x] < 0 ? x : data[x] = root(data[x]); } n   int size(int x) { return -data[root(x)]; } n }; n struct Edge { n   int x, y; n   int w; n   Edge() {} n   Edge(int x_, int y_, int w_) : x(x_), y(y_), w(w_) {} n }; n bool operator<(const Edge &a, const Edge &b) { return a.w < b.w; } n vector<vector<int> > g; n vector<long long> dist; n vector<vector<int> > d; n void dfs(int i, int p, long long t) { n   dist[i] = t; n   for (typeof((g[i]).begin()) j = ((g[i]).begin()); j != (g[i]).end(); ++j) n     if (*j != p) dfs(*j, i, t + d[i][*j]); n } n int main() { n   int n; n   scanf( %d , &n); n   d.assign(n, vector<int>(n)); n   for (int(i) = 0; (i) < (int)(n); ++(i)) n     for (int(j) = 0; (j) < (int)(n); ++(j)) scanf( %d , &d[i][j]); n   bool ok = true; n   for (int(i) = 0; (i) < (int)(n); ++(i)) n     for (int(j) = 0; (j) < (int)(i); ++(j)) { n       ok &= d[i][j] == d[j][i]; n       ok &= d[i][j] > 0; n     } n   for (int(i) = 0; (i) < (int)(n); ++(i)) ok &= d[i][i] == 0; n   if (!ok) { n     puts( NO ); n     return 0; n   } n   vector<Edge> edges; n   edges.reserve(n * (n - 1) / 2); n   for (int(i) = 0; (i) < (int)(n); ++(i)) n     for (int(j) = 0; (j) < (int)(i); ++(j)) n       edges.push_back(Edge(i, j, d[i][j])); n   sort(edges.begin(), edges.end()); n   UnionFind uf(n); n   g.assign(n, vector<int>()); n   for (int i = 0; i < (int)edges.size(); i++) { n     int x = edges[i].x, y = edges[i].y; n     if (uf.unionSet(x, y)) { n       g[x].push_back(y); n       g[y].push_back(x); n     } n   } n   dist.resize(n); n   for (int(i) = 0; (i) < (int)(n); ++(i)) { n     dfs(i, -1, 0); n     for (int(j) = 0; (j) < (int)(n); ++(j)) ok &= dist[j] == d[i][j]; n   } n   puts(ok ?  YES  :  NO ); n   return 0; n } n


#include <bits/stdc++.h> n namespace Factor { n const int N = 1010000; n int64_t C, fac[10010], n, mut, a[1001000]; n int T, cnt, i, l, prime[N], p[N], psize, _cnt; n int64_t _e[100], _pr[100]; n std::vector<int64_t> d; n inline int64_t mul(int64_t a, int64_t b, int64_t p) { n   if (p <= 1000000000) n     return a * b % p; n   else if (p <= 1000000000000ll) n     return (((a * (b >> 20) % p) << 20) + (a * (b & ((1 << 20) - 1)))) % p; n   else { n     return (int64_t)((__int128_t)a * b % p); n   } n } n void prime_table() { n   int i, j, tot, t1; n   for (i = 1; i <= psize; i++) p[i] = i; n   for (i = 2, tot = 0; i <= psize; i++) { n     if (p[i] == i) prime[++tot] = i; n     for (j = 1; j <= tot && (t1 = prime[j] * i) <= psize; j++) { n       p[t1] = prime[j]; n       if (i % prime[j] == 0) break; n     } n   } n } n void init(int ps) { n   psize = ps; n   prime_table(); n } n int64_t powl(int64_t a, int64_t n, int64_t p) { n   int64_t ans = 1; n   for (; n; n >>= 1) { n     if (n & 1) ans = mul(ans, a, p); n     a = mul(a, a, p); n   } n   return ans; n } n bool witness(int64_t a, int64_t n) { n   int t = 0; n   int64_t u = n - 1; n   for (; ~u & 1; u >>= 1) t++; n   int64_t x = powl(a, u, n), _x = 0; n   for (; t; t--) { n     _x = mul(x, x, n); n     if (_x == 1 && x != 1 && x != n - 1) return 1; n     x = _x; n   } n   return _x != 1; n } n bool miller(int64_t n) { n   if (n < 2) return 0; n   if (n <= psize) return p[n] == n; n   if (~n & 1) return 0; n   for (int j = 0; j <= 7; j++) n     if (witness(rand() % (n - 1) + 1, n)) return 0; n   return 1; n } n int64_t gcd(int64_t a, int64_t b) { n   int64_t ret = 1; n   while (a != 0) { n     if ((~a & 1) && (~b & 1)) n       ret <<= 1, a >>= 1, b >>= 1; n     else if (~a & 1) n       a >>= 1; n     else if (~b & 1) n       b >>= 1; n     else { n       if (a < b) std::swap(a, b); n       a -= b; n     } n   } n   return ret * b; n } n int64_t rho(int64_t n) { n   for (;;) { n     int64_t X = rand() % n, Y, Z, T = 1, *lY = a, *lX = lY; n     int tmp = 20; n     C = rand() % 10 + 3; n     X = mul(X, X, n) + C; n     *(lY++) = X; n     lX++; n     Y = mul(X, X, n) + C; n     *(lY++) = Y; n     for (; X != Y;) { n       int64_t t = X - Y + n; n       Z = mul(T, t, n); n       if (Z == 0) return gcd(T, n); n       tmp--; n       if (tmp == 0) { n         tmp = 20; n         Z = gcd(Z, n); n         if (Z != 1 && Z != n) return Z; n       } n       T = Z; n       Y = *(lY++) = mul(Y, Y, n) + C; n       Y = *(lY++) = mul(Y, Y, n) + C; n       X = *(lX++); n     } n   } n } n void _factor(int64_t n) { n   for (int i = 0; i < cnt; i++) { n     if (n % fac[i] == 0) n /= fac[i], fac[cnt++] = fac[i]; n   } n   if (n <= psize) { n     for (; n != 1; n /= p[n]) fac[cnt++] = p[n]; n     return; n   } n   if (miller(n)) n     fac[cnt++] = n; n   else { n     int64_t x = rho(n); n     _factor(x); n     _factor(n / x); n   } n } n void dfs(int64_t x, int dep) { n   if (dep == _cnt) n     d.push_back(x); n   else { n     dfs(x, dep + 1); n     for (int i = 1; i <= _e[dep]; i++) dfs(x *= _pr[dep], dep + 1); n   } n } n void norm() { n   std::sort(fac, fac + cnt); n   _cnt = 0; n   for (int i = 0; i < cnt; ++i) n     if (i == 0 || fac[i] != fac[i - 1]) n       _pr[_cnt] = fac[i], _e[_cnt++] = 1; n     else n       _e[_cnt - 1]++; n } n std::vector<int64_t> getd() { n   d.clear(); n   dfs(1, 0); n   return d; n } n std::vector<int64_t> factor(int64_t n) { n   cnt = 0; n   _factor(n); n   norm(); n   return getd(); n } n std::vector<std::pair<int64_t, int64_t>> factorG(int64_t n) { n   cnt = 0; n   _factor(n); n   norm(); n   std::vector<std::pair<int64_t, int64_t>> d; n   for (int i = 0; i < _cnt; ++i) d.emplace_back(_pr[i], _e[i]); n   return d; n } n bool is_primitive(int64_t a, int64_t p) { n   assert(miller(p)); n   std::vector<std::pair<int64_t, int64_t>> D = factorG(p - 1); n   for (int i = 0; i < D.size(); ++i) n     if (powl(a, (p - 1) / D[i].first, p) == 1) return 0; n   return 1; n } n }  // namespace Factor n int64_t powll(int64_t a, int64_t n) { n   int64_t ans = 1; n   for (; n; n >>= 1) { n     if (n & 1) ans = ans * a; n     a = a * a; n   } n   return ans; n } n int64_t mul(int64_t x, int64_t y, int64_t m) { n   x %= m; n   y %= m; n   assert(x >= 0 && y >= 0); n   int64_t d = ((long double)x * y / m); n   d = x * y - d * m; n   if (d >= m) d -= m; n   if (d < 0) d += m; n   return d; n } n int64_t Inv(int64_t q, int64_t m) { n   if (q == 0) return 0; n   assert(q >= 0); n   int64_t a1 = m, b1 = 0, a2 = q, b2 = 1, a3, b3, t; n   while (a2 != 1) { n     t = a1 / a2, a3 = a1 - t * a2, b3 = b1 - mul(t, b2, m), a1 = a2, a2 = a3, n     b1 = b2, b2 = b3; n     if (b2 < 0) b2 += m; n   } n   return b2; n } n int main() { n   Factor::init(500000); n   int64_t m, x; n   std::cin >> m >> x; n   auto factors = Factor::factor(m); n   int64_t ans = 0; n   for (int64_t n : factors) { n     int64_t a = x % n; n     auto for_phi = Factor::factorG(n); n     int64_t phi = n; n     for (auto pr : for_phi) { n       phi -= phi / pr.first; n     } n     auto pfac = Factor::factorG(phi); n     int64_t t = phi; n     for (auto& i : pfac) { n       t /= powll(i.first, i.second); n       int64_t a1 = Factor::powl(a, t, n); n       while (a1 != 1) { n         a1 = Factor::powl(a1, i.first, n); n         t = mul(t, i.first, n); n       } n     } n     ans += phi / t; n   } n   std::cout << ans <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n int n; n struct point { n   double x, y; n } a, b, t, q[100010]; n struct wxz { n   double d1, d2; n } f[100010]; n double ans, delta1[100010], delta2[100010], delta; n double dist(point u, point v) { n   return sqrt((u.x - v.x) * (u.x - v.x) + (u.y - v.y) * (u.y - v.y)); n } n bool vis[100010]; n int main() { n   scanf( %lf%lf%lf%lf%lf%lf , &a.x, &a.y, &b.x, &b.y, &t.x, &t.y); n   scanf( %d , &n); n   for (int i = 1; i <= n; i++) scanf( %lf%lf , &q[i].x, &q[i].y); n   for (int i = 1; i <= n; i++) ans += 2 * dist(t, q[i]); n   for (int i = 1; i <= n; i++) f[i].d1 = dist(a, q[i]) - dist(t, q[i]); n   for (int i = 1; i <= n; i++) f[i].d2 = dist(b, q[i]) - dist(t, q[i]); n   double delta = 1e18, dd1 = 1e18, dd2 = 1e18; n   for (int i = 1; i <= n; i++) { n     delta = min(delta, min(dd1 + f[i].d2, dd2 + f[i].d1)); n     dd1 = min(dd1, f[i].d1), dd2 = min(dd2, f[i].d2); n   } n   printf( %lf n , ans + min(delta, min(dd1, dd2))); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n bool vis[105][105]; n int main() { n   int n; n   scanf( %d , &n); n   int m; n   scanf( %d , &m); n   for (int i = 1; i <= m; i++) { n     int x; n     scanf( %d , &x); n     vis[1][x] = true; n   } n   for (int i = 2; i <= n; i++) { n     int m; n     scanf( %d , &m); n     for (int j = 1; j <= m; j++) { n       int x; n       scanf( %d , &x); n       if (vis[i - 1][x]) vis[i][x] = true; n     } n   } n   for (int i = 1; i <= 100; i++) n     if (vis[n][i]) printf( %d  , i); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int i, n, k; n double jumlah, x[101], y[101]; n int main() { n   scanf( %d %d , &n, &k); n   for (i = 1; i <= n; ++i) { n     scanf( %lf %lf , &x[i], &y[i]); n   } n   for (i = 1; i < n; ++i) { n     jumlah += sqrt(((x[i] - x[i + 1]) * (x[i] - x[i + 1])) + n                    ((y[i] - y[i + 1]) * (y[i] - y[i + 1]))); n   } n   jumlah = ((jumlah / 50) * k); n   printf( %.9lf n , jumlah); n } n


#include <bits/stdc++.h> n using namespace std; n long long a[20], b[20]; n long long ans = 0; n void print() { n   for (int i = 0; i < 14; i++) cout << b[i] <<    ; n   cout << endl; n } n int main() { n   for (int i = 0; i < 14; i++) cin >> a[i]; n   for (int i = 0; i < 14; i++) { n     if (a[i]) { n       for (int j = 0; j < 14; j++) b[j] = a[j]; n       long long score = 0; n       long long tmp = b[i]; n       b[i] = 0; n       for (int j = 0; j < 14; j++) b[j] += tmp / 14; n       for (int j = 1; j <= tmp % 14; j++) b[(i + j) % 14]++; n       for (int j = 0; j < 14; j++) n         if (b[j] % 2 == 0) score += b[j]; n       ans = max(ans, score); n     } n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int inf = 0x3f3f3f3f; n const int maxn = 1e6 + 7; n long long t, n, k, temp; n int main(int argc, char* argv[]) { n   cin >> t; n   while (t--) { n     cin >> n >> k; n     long long cnt = 0; n     while (n > 0) { n       if (n % k != 0) { n         cnt += n % k; n         n -= n % k; n       } else { n         cnt++; n         n /= k; n       } n     } n     cout << cnt << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n int main() { n   long long n, x; n   int q, i; n   scanf( %lld %i , &n, &q); n   while (q--) { n     scanf( %lld , &x); n     while (!(x & 1)) { n       x += (n - (x >> 1)); n     } n     printf( %lld n , (x + 1) / 2); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int a[111111], b[111111], n, m, k; n int main() { n   while (scanf( %d%d%d , &n, &m, &k) != EOF) { n     for (int i = 1; i <= n; i++) scanf( %d , &a[i]); n     for (int i = 1; i <= m; i++) scanf( %d , &b[i]); n     sort(a + 1, a + 1 + n); n     sort(b + 1, b + 1 + m); n     bool yes = 0; n     for (int i = 0; i <= max(n, m); i++) { n       if (n - i < 1 || m - i < 1) break; n       if (a[n - i] > b[m - i]) { n         yes = 1; n         printf( YES n ); n         break; n       } n     } n     if (yes) continue; n     if (n > m) n       printf( YES n ); n     else n       printf( NO n ); n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int t; n   cin >> t; n   while (t--) { n     string s; n     cin >> s; n     if (s.size() % 2) n       cout <<  NO n ; n     else { n       string word1 =   , word2 =   ; n       for (int i = 0; i < s.size() / 2; i++) word1 += s[i]; n       for (int i = s.size() / 2; i < s.size(); i++) word2 += s[i]; n       if (word1 == word2) n         cout <<  YES n ; n       else n         cout <<  NO n ; n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); n const int MOD = 100, MAX = 1e6 + 5; n const long long INF = 1e18 + 5; n int main() { n   string s; n   cin >> s; n   long long n = (s[1] -  0 ) + (s[3] -  0 ) * 10 + (s[4] -  0 ) * 100 + n                 (s[2] -  0 ) * 1000 + (s[0] -  0 ) * 10000; n   long long Ans = 1; n   for (int i = 0; i < 5; i++) { n     Ans = (Ans * n) % 100000; n   } n   string x = to_string(Ans); n   reverse(x.begin(), x.end()); n   while (x.size() != 5) { n     x +=  0 ; n   } n   reverse(x.begin(), x.end()); n   cout << x << endl; n } n


#include <bits/stdc++.h> n using namespace std; n const long long M = 1e10; n set<long long> s; n void bt(long long x) { n   if (x >= M) return; n   s.insert(x); n   bt(x * 10 + 4); n   bt(x * 10 + 7); n } n int main() { n   bt(0); n   int l, r; n   cin >> l >> r; n   long long c = 0; n   for (set<long long>::iterator it = s.begin(); it != s.end(); it++) { n     if (*it >= l) { n       if (r <= *it) { n         c += (r - l + 1) * *it; n         break; n       } else n         c += (*it - l + 1) * *it; n       l = *it + 1; n     } n   } n   cout << c << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAX = 2e5 + 5; n int n; n pair<int, int> a[MAX]; n struct par { n   int a, b; n   par(int a, int b) : a(a), b(b) {} n   bool operator<(const par &p) const { return b < p.b; } n }; n multiset<par> s; n multiset<int> sizes; n int main() { n   scanf( %d , &n); n   for (int i = 1; i <= n; i++) { n     scanf( %d , &a[i].first); n     a[i].second = i; n   } n   sort(a + 1, a + n + 1); n   int best = 1, opt = a[n].first + 1; n   for (int i = 1; i <= n; i++) { n     if (!sizes.empty()) { n       auto x = sizes.begin(); n       auto y = sizes.rbegin(); n       int v = a[i - 1].first + 1; n       if (*x == *y) { n         if ((int)sizes.size() > best) { n           best = sizes.size(); n           opt = v; n         } else if ((int)sizes.size() == best && a[i].first < opt) n           opt = v; n       } n     } n     auto u = s.upper_bound(par(0, a[i].second)); n     par intervalo = par(a[i].second, a[i].second); n     vector<par> del; n     if (u != s.end()) { n       auto o = *u; n       if (o.a - 1 == a[i].second) { n         del.push_back(o); n         intervalo.b = o.b; n       } n     } n     if (u != s.begin()) { n       u--; n       auto o = *u; n       if (o.b + 1 == a[i].second) { n         del.push_back(o); n         intervalo.a = o.a; n       } n     } n     for (auto o : del) { n       s.erase(s.find(o)); n       int sz = o.b - o.a + 1; n       sizes.erase(sizes.find(sz)); n     } n     s.insert(intervalo); n     sizes.insert(intervalo.b - intervalo.a + 1); n   } n   printf( %d n , opt); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, h; n int main() { n   cin >> n >> h; n   for (int i = 1; i < n; i++) { n     double h1 = h * sqrt(1.0 * i / n); n     printf( %.12llf  , h1); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <class T> n void dbs(string str, T t) { n   cerr << str <<   :   << t <<   n ; n } n template <class T, class... S> n void dbs(string str, T t, S... s) { n   long long idx = str.find( , ); n   cerr << str.substr(0, idx) <<   :   << t <<  ,  ; n   dbs(str.substr(idx + 1), s...); n } n template <class S, class T> n ostream& operator<<(ostream& os, const pair<S, T>& p) { n   return os <<  (  << p.first <<  ,   << p.second <<  ) ; n } n template <class T> n ostream& operator<<(ostream& os, const vector<T>& p) { n   os <<  [  ; n   for (auto& it : p) os << it <<    ; n   return os <<  ] ; n } n template <class T> n ostream& operator<<(ostream& os, const set<T>& p) { n   os <<  [  ; n   for (auto& it : p) os << it <<    ; n   return os <<  ] ; n } n template <class S, class T> n ostream& operator<<(ostream& os, const map<S, T>& p) { n   os <<  [  ; n   for (auto& it : p) os << it <<    ; n   return os <<  ] ; n } n template <class T> n void prc(T a, T b) { n   cerr <<  [ ; n   for (T i = a; i != b; ++i) { n     if (i != a) cerr <<  ,  ; n     cerr << *i; n   } n   cerr <<  ] n ; n } n int32_t main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   long long t, i, j, k, n, m, q; n   long long x, y, l, r; n   string s; n   long long len; n   cin >> len; n   cin >> s; n   vector<long long> pos(len + 7, -1); n   long long cur = 1; n   for (i = 0; i < len; i++) { n     if (s[i] ==  8 ) { n       pos[cur++] = i; n     } n   } n   long long moves = (len - 11) / 2; n   cur = min(cur - 1, moves); n   dbs( cur, pos[cur], pos[cur + 1] , cur, pos[cur], pos[cur + 1]); n   if (pos[cur + 1] == -1 || pos[cur + 1] - (cur) > moves) { n     cout <<  NO  << endl; n   } else { n     cout <<  YES  << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n namespace chtholly { n char buf[1 << 23], *p1 = buf, *p2 = buf; n inline int read() { n   int x = 0, f = 1; n   char c = getchar(); n   for (; !isdigit(c); c = getchar()) f ^= c ==  - ; n   for (; isdigit(c); c = getchar()) x = (x << 3) + (x << 1) + (c ^  0 ); n   return f ? x : -x; n } n template <typename mitsuha> n inline bool read(mitsuha &x) { n   x = 0; n   int f = 1; n   char c = getchar(); n   for (; !isdigit(c) && ~c; c = getchar()) f ^= c ==  - ; n   if (!~c) return 0; n   for (; isdigit(c); c = getchar()) x = (x << 3) + (x << 1) + (c ^  0 ); n   return x = f ? x : -x, 1; n } n template <typename mitsuha> n inline int write(mitsuha x) { n   if (!x) return 0 & putchar(48); n   if (x < 0) putchar( - ), x = -x; n   int bit[20], i, p = 0; n   for (; x; x /= 10) bit[++p] = x % 10; n   for (i = p; i; --i) putchar(bit[i] + 48); n   return 0; n } n inline char fuhao() { n   char c = getchar(); n   for (; isspace(c); c = getchar()) n     ; n   return c; n } n }  // namespace chtholly n using namespace chtholly; n using namespace std; n const int yuzu = 2e5; n typedef int fuko[yuzu | 10]; n vector<int> lj[yuzu | 10]; n fuko a; n int zxy; n int dfs(int u, int lxy, int fa = 0) { n   if (a[u] % lxy) return 0; n   for (; a[u] % lxy == 0; a[u] /= lxy) n     ; n   vector<int> v; n   for (int i : lj[u]) n     if (i ^ fa) v.push_back(dfs(i, lxy, u)); n   sort(v.begin(), v.end(), greater<int>()); n   int llx = 1; n   if (v.size()) llx += v[0]; n   if (v.size() > 1) zxy = max(zxy, llx + v[1]); n   return zxy = max(zxy, llx), llx; n } n int main() { n   int i, j, n = read(), u, v; n   for (i = 1; i <= n; ++i) a[i] = read(); n   for (i = 1; i < n; ++i) { n     read(u), read(v); n     lj[u].push_back(v); n     lj[v].push_back(u); n   } n   for (i = 1; i <= n; ++i) { n     for (j = 2; j * j <= a[i]; ++j) n       if (a[i] % j == 0) dfs(i, j); n     if (a[i] > 1) dfs(i, a[i]); n   } n   write(zxy), puts(  ); n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int T; n   cin >> T; n   while (T--) { n     long long a, b, c, d; n     cin >> a >> b >> c >> d; n     if (c <= d) { n       if (a <= b * c) n         cout << a << endl; n       else { n         cout << -1 << endl; n       } n     } else { n       long long len = c % d ? c / d + 1 : c / d; n       long long ans = 0; n       long long x = (b * d + 2 * a) / 2 / b / d; n       x = min(x, len); n       ans = a * x - x * (x - 1) / 2 * b * d; n       x++; n       x = min(x, len); n       ans = max(ans, a * x - x * (x - 1) / 2 * b * d); n       if (a > b * c) n         cout << -1 << endl; n       else { n         ans = max(ans, a * len - (len - 1) * len / 2 * d * b); n         cout << ans << endl; n       } n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   int flag = 0; n   scanf( %d , &n); n   for (int i = 0; i < n; i++) { n     int num, num1; n     string str; n     cin >> str >> num >> num1; n     if (num >= 2400 && (num1 - num) > 0) { n       flag = 1; n       break; n     } n   } n   if (flag) n     cout <<  YES n ; n   else n     cout <<  NO n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int limite = 100005; n pair<int, int> maximo[limite]; n void inserta(int pos, int val) { n   pair<int, int> valor(val, pos); n   for (; pos < limite; pos += pos & -pos) maximo[pos] = max(maximo[pos], valor); n } n int computamaximo(int pos) { n   pair<int, int> maxi(0, 0); n   for (; pos > 0; pos -= pos & -pos) maxi = max(maxi, maximo[pos]); n   return maxi.second; n } n int n; n int a[limite]; n long long int sr[limite]; n long long int sol = 0; n int main() { n   ios::sync_with_stdio(false); n   cin >> n; n   for (int i = 1; i < n; i++) cin >> a[i]; n   for (int i = n - 1; i >= 1; i--) { n     if (a[i] == n) n       sr[i] = n - i; n     else { n       int jmin = computamaximo(a[i]); n       sr[i] = a[i] - i + sr[jmin] - (a[i] - jmin) + (n - a[i]); n     } n     sol += sr[i]; n     inserta(i, a[i]); n   } n   cout << sol << endl; n } n


#include <bits/stdc++.h> n #pragma GCC optimize( O3 ) n using namespace std; n const long long inf = 1e9; n const int MAXM = 1048800; n const int MAXN = 1e7 + 1; n bool visit[MAXM]; n int a[MAXM]; n int k; n vector<int> v; n int main() { n   int n, i, j, k; n   cin >> n; n   n *= 2; n   for (int i = 0; i < n; i++) { n     cin >> a[i]; n   } n   int ans = INT_MAX, cnt = 0; n   sort(a, a + n); n   for (int i = 0; i < n; i++) { n     for (int j = i + 1; j < n; j++) { n       v.clear(); n       cnt = 0; n       for (int k = 0; k < n; k++) { n         if (k == i or k == j) continue; n         v.push_back(a[k]); n       } n       int nn = v.size(); n       for (int k = 0; k < nn; k += 2) { n         cnt += v[k + 1] - v[k]; n       } n       ans = min(ans, cnt); n     } n   } n   cout << ans << endl; n   return 0; n } n