#include <bits/stdc++.h> n using namespace std; n inline int read() { n   int x = 0, f = 1; n   char c = getchar(); n   while (c <  0  || c >  9 ) { n     if (c ==  - ) f = -1; n     c = getchar(); n   } n   while (c >=  0  && c <=  9 ) { n     x = (x << 3) + (x << 1) + (c ^ 48); n     c = getchar(); n   } n   return f == 1 ? x : ~x + 1; n } n inline void print(int x) { n   if (x < 0) putchar( - ), x = ~x + 1; n   if (x >= 10) print(x / 10); n   putchar((x % 10) | 48); n } n int typ; n int n; n int h[500010]; n int ans[500010]; n set<int> st; n int Id[500010]; n bool cmp(int x, int y) { return h[x] > h[y]; } n bool b[500010]; n int Ans; n struct node { n   int vl, vr, l, r, ans; n   bool full; n   node() { vl = vr = l = r = full = 0; } n   void init(int x) { n     vl = vr = x; n     l = r = ans = full = 1; n   } n } tre[2000010]; n node pushup(node ls, node rs) { n   node u; n   u.vl = ls.vl, u.vr = rs.vr; n   u.ans = max(ls.ans, rs.ans); n   u.l = ls.l, u.r = rs.r; n   u.full = 0; n   if (ls.vr ^ rs.vl) { n     u.ans = max(u.ans, ls.r + rs.l); n     u.full = (ls.full & rs.full); n     if (ls.full) u.l = ls.l + rs.l; n     if (rs.full) u.r = rs.r + ls.r; n   } n   return u; n } n void build(int p, int l, int r) { n   if (l == r) { n     tre[p].init(0); n     return; n   } n   int mid = l + r >> 1; n   build(p << 1, l, mid); n   build(p << 1 | 1, mid + 1, r); n   tre[p] = pushup(tre[p << 1], tre[p << 1 | 1]); n } n void modify(int p, int l, int r, int pos) { n   if (l == r) { n     tre[p].init(1); n     return; n   } n   int mid = l + r >> 1; n   if (pos <= mid) n     modify(p << 1, l, mid, pos); n   else n     modify(p << 1 | 1, mid + 1, r, pos); n   tre[p] = pushup(tre[p << 1], tre[p << 1 | 1]); n   return; n } n node queryl(int p, int l, int r, int pos) { n   if (l == r) return tre[p]; n   int mid = l + r >> 1; n   if (pos <= mid) return queryl(p << 1, l, mid, pos); n   node u = queryl(p << 1 | 1, mid + 1, r, pos); n   return pushup(tre[p << 1], u); n } n node queryr(int p, int l, int r, int pos) { n   if (l == r) return tre[p]; n   int mid = l + r >> 1; n   if (pos > mid) return queryr(p << 1 | 1, mid + 1, r, pos); n   node u = queryr(p << 1, l, mid, pos); n   return pushup(u, tre[p << 1 | 1]); n } n void firm(int l, int r, int x) { n   auto itl = st.lower_bound(l); n   if (itl == st.end()) return; n   vector<set<int>::iterator> vit; n   while (itl != st.end() && (*itl) <= r) { n     vit.push_back(itl); n     ans[*itl] = x; n     ++itl; n   } n   for (auto i : vit) st.erase(i); n } n int main() { n   n = read(); n   for (int i = 1; i <= n; ++i) { n     h[i] = read(); n     st.insert(i); n     Id[i] = i; n   } n   sort(Id + 1, Id + n + 1, cmp); n   build(1, 1, n); n   for (int i = 1; i <= n; ++i) { n     modify(1, 1, n, Id[i]); n     b[Id[i]] = 1; n     if (h[Id[i]] != h[Id[i + 1]]) Ans = max(Ans, tre[1].ans - 1 >> 1); n     node tl = queryl(1, 1, n, Id[i]); n     node tr = queryr(1, 1, n, Id[i]); n     int sl = Id[i] - tl.r + 1, sr = Id[i] + tr.l - 1; n     if (b[sl] == 0) { n       if (b[sr] == 1) { n         int len = (sr - sl + 1); n         firm(sr - (len >> 1) + 1, sr, h[Id[i]]); n       } n     } else { n       if (b[sr] == 0) { n         int len = sr - sl + 1; n         firm(sl, sl + (len >> 1) - 1, h[Id[i]]); n       } else n         firm(sl, sr, h[Id[i]]); n     } n     if (sl == Id[i] && Id[i] != 1) { n       node tmp = queryl(1, 1, n, Id[i] - 1); n       int len = tmp.r; n       firm(Id[i] - (len >> 1), Id[i], h[Id[i]]); n     } n     if (sr == Id[i] && Id[i] != n) { n       node tmp = queryr(1, 1, n, Id[i] + 1); n       int len = tmp.l; n       firm(Id[i], Id[i] + (len >> 1), h[Id[i]]); n     } n   } n   printf( %d n , Ans); n   for (int i = 1; i <= n; ++i) printf( %d  , ans[i]); n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   vector<string> v; n   int i, j, k, l, l1, l2; n   string a, b, s1, s2, s; n   cin >> a >> b; n   l1 = a.size(); n   l2 = b.size(); n   for (i = 1; i <= l1; i++) { n     for (j = 1; j <= l2; j++) { n       for (k = 0, s1 =   ; k < i; k++) { n         s1 += a[k]; n       } n       for (l = 0, s2 =   ; l < j; l++) { n         s2 += b[l]; n       } n       s = s1 + s2; n       v.push_back(s); n     } n   } n   sort(v.begin(), v.end()); n   cout << v[0] << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n; n int m; n struct st { n   int dp; n   int maxt; n   int cnt; n   bool use; n   st() { n     dp = maxt = cnt = 0; n     use = false; n   } n }; n vector<st> dp[123457]; n vector<int> v[123457]; n vector<int> nex[123457]; n bool oc[123457]; n bool visited[123457]; n pair<int, int> max1[123457]; n pair<int, int> max2[123457]; n int sum_cnt[123457]; n int not_contain[123457]; n int dpp[123457]; n inline void dfs(int a, int b) { n   if (dp[a][b].use) { n     return; n   } n   dp[a][b].use = true; n   if (visited[a]) { n     dp[a][b].cnt = sum_cnt[a]; n     dp[a][b].dp = dpp[a]; n     dp[a][b].maxt = max1[a].first; n     if (max1[a].second == b) { n       dp[a][b].maxt = max2[a].first; n     } n     dp[a][b].use = true; n     if (dp[v[a][b]][nex[a][b]].cnt) { n       dp[a][b].dp -= dp[v[a][b]][nex[a][b]].dp + 2; n       dp[a][b].cnt -= dp[v[a][b]][nex[a][b]].cnt; n     } n     dfs(v[a][not_contain[a]], nex[a][not_contain[a]]); n     if (dp[v[a][not_contain[a]]][nex[a][not_contain[a]]].cnt) { n       dp[a][b].dp += dp[v[a][not_contain[a]]][nex[a][not_contain[a]]].dp + 2; n       dp[a][b].cnt += dp[v[a][not_contain[a]]][nex[a][not_contain[a]]].cnt; n       dp[a][b].maxt = n           max(dp[a][b].maxt, n               dp[v[a][not_contain[a]]][nex[a][not_contain[a]]].maxt + 1); n     } n     return; n   } n   visited[a] = true; n   if (oc[a]) { n     dp[a][b].cnt++; n   } n   int siz = v[a].size(); n   pair<int, int> MA1 = make_pair(0, -1); n   pair<int, int> MA2 = make_pair(0, -1); n   for (int i = 0; i < siz; i++) { n     if (i == b) { n       continue; n     } n     int l = v[a][i]; n     int r = nex[a][i]; n     if (!dp[l][r].use) { n       dfs(l, r); n     } n     if (dp[l][r].cnt) { n       dp[a][b].cnt += dp[l][r].cnt; n       if (dp[l][r].maxt + 1 > dp[a][b].maxt) { n         dp[a][b].maxt = dp[l][r].maxt + 1; n       } n       pair<int, int> ne = make_pair(dp[l][r].maxt + 1, i); n       if (ne > MA1) { n         MA2 = MA1; n         MA1 = ne; n       } else { n         if (ne > MA2) { n           MA2 = ne; n         } n       } n       dp[a][b].dp += dp[l][r].dp + 2; n     } n   } n   sum_cnt[a] = dp[a][b].cnt; n   not_contain[a] = b; n   max1[a] = MA1; n   max2[a] = MA2; n   dpp[a] = dp[a][b].dp; n } n int main() { n   scanf( %d%d , &n, &m); n   for (int i = 1; i < n; i++) { n     int a, b; n     scanf( %d%d , &a, &b); n     a--; n     b--; n     nex[a].push_back(v[b].size()); n     nex[b].push_back(v[a].size()); n     v[a].push_back(b); n     v[b].push_back(a); n   } n   for (int i = 0; i < n; i++) { n     dp[i].assign(v[i].size(), st()); n   } n   for (int i = 0; i < m; i++) { n     int a; n     scanf( %d , &a); n     a--; n     oc[a] = true; n   } n   int ans = INT_MAX; n   int ans_index = 0; n   for (int i = 0; i < n; i++) { n     int maxx = 0; n     int sum = 0; n     int siz = v[i].size(); n     for (int j = 0; j < siz; j++) { n       int l = v[i][j]; n       int r = nex[i][j]; n       dfs(l, r); n       if (dp[l][r].cnt) { n         if (dp[l][r].maxt + 1 > maxx) { n           maxx = dp[l][r].maxt + 1; n         } n         sum += dp[l][r].dp + 2; n       } n     } n     sum -= maxx; n     if (ans > sum) { n       ans = sum; n       ans_index = i; n     } n   } n   printf( %d n , ans_index + 1); n   printf( %d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void ok(int x) { n   cout << x <<   n ; n   exit(0); n } n int main() { n   int n, p; n   cin >> n >> p; n   for (int k = 0; k < int(1e8); k++) { n     long long n1 = n - 1LL * p * k; n     if (n1 < 0) continue; n     if (n1 >= k && k >= __builtin_popcountll(n1)) ok(k); n   } n   ok(-1); n } n


#include <bits/stdc++.h> n using namespace std; n vector<pair<int, int> > ans; n vector<int> e[100010]; n void dfs(int x, int p, int entry) { n   ans.emplace_back(x, entry); n   if (x == 1) { n     for (int it : e[x]) n       if (it != p) dfs(it, x, ++entry), ans.emplace_back(1, entry); n   } else { n     int cur = entry; n     int sz = e[x].size() - 1; n     if (entry - 1 >= sz) { n       cur = entry - 1 - sz; n       ans.emplace_back(x, cur); n       for (int it : e[x]) n         if (it != p) { n           cur++; n           dfs(it, x, cur), ans.emplace_back(x, cur); n         } n     } else { n       for (int it : e[x]) n         if (it != p) { n           ++cur; n           dfs(it, x, cur), ans.emplace_back(x, cur); n           if (cur == sz + 1) { n             cur = 0; n             ans.emplace_back(x, 0); n           } n         } n     } n   } n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   int n; n   cin >> n; n   for (int i = 1, u, v; i < n; i++) { n     cin >> u >> v; n     e[u].emplace_back(v); n     e[v].emplace_back(u); n   } n   dfs(1, 0, 0); n   cout << ans.size() <<   n ; n   for (auto &[u, v] : ans) cout << u <<     << v <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   string s; n   int n, sumA = 0, sumG = 0; n   cin >> n; n   vector<pair<int, int>> vec(n); n   for (int i = 0; i < n; i++) cin >> vec[i].first >> vec[i].second; n   if (vec[0].first > vec[0].second) { n     sumG = vec[0].second; n     s.push_back( G ); n   } else { n     sumA = vec[0].first; n     s.push_back( A ); n   } n   for (int i = 1; i < n; i++) { n     if (sumA + vec[i].first - sumG <= 500) { n       sumA += vec[i].first; n       s.push_back( A ); n     } else { n       sumG += vec[i].second; n       s.push_back( G ); n     } n   } n   if (abs(sumA - sumG) <= 500) n     cout << s; n   else n     cout << -1; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<int> spl; n int p[2000006]; n void prime() { n   p[0] = p[1] = 0; n   for (int i = 2; i < 1420; i++) { n     if (!p[i]) { n       spl.push_back(i); n       for (int j = i * i; j < 2000006; j += i) p[j] = i; n     } n   } n } n int n, q; n int a[100005]; n vector<int> edge[100005]; n int par[100005]; n int siz[100005]; n int dep[100005]; n int dfs1(int u, int p, int d) { n   par[u] = p; n   siz[u] = 1; n   dep[u] = d; n   for (auto v : edge[u]) n     if (v != p) siz[u] += dfs1(v, u, d + 1); n   return siz[u]; n } n int cnt; n int reid[100005]; n int reidm1[100005]; n int head[100005]; n void dfs2(int u, int h) { n   reid[u] = cnt; n   reidm1[cnt++] = u; n   head[reid[u]] = reid[h]; n   int ch = 0, s = 0; n   for (auto v : edge[u]) n     if (v != par[u] && s < siz[v]) s = siz[v], ch = v; n   if (!ch) return; n   dfs2(ch, h); n   for (auto v : edge[u]) n     if (v != par[u] && v != ch) dfs2(v, v); n } n vector<int> prv[2000006]; n void scan() { n   for (int id = 1; id <= n; id++) { n     int val = a[reidm1[id]]; n     while (p[val] > 0) { n       prv[p[val]].push_back(id); n       int k = p[val]; n       while (val % k == 0) val /= k; n     } n     if (val > 1) prv[val].push_back(id); n   } n } n void build(int md) { n   if (md) dfs1(1, 0, 1); n   if (md) { n     cnt = 1; n     dfs2(1, 1); n   } n   scan(); n } n int find(int id, int p) { n   if (id < 1) return -1; n   auto it = upper_bound(prv[p].begin(), prv[p].end(), id); n   it--; n   if (head[id] <= *it && *it <= id) n     return *it; n   else n     return find(reid[par[reidm1[head[id]]]], p); n } n void sol() { n   while (q--) { n     int op, x, y; n     scanf( %d , &op); n     if (op == 1) { n       scanf( %d , &x); n       int val = a[x], ans = -1; n       while (p[val]) { n         ans = max(ans, find(reid[par[x]], p[val])); n         int k = p[val]; n         while (val % k == 0) val /= k; n       } n       if (val > 1) ans = max(ans, find(reid[par[x]], val)); n       printf( %d n , ans == -1 ? -1 : reidm1[ans]); n     } else { n       scanf( %d%d , &x, &y); n       int id = reid[x]; n       int val = a[x]; n       while (p[val] > 0) { n         int idx = lower_bound(prv[p[val]].begin(), prv[p[val]].end(), id) - n                   prv[p[val]].begin(); n         prv[p[val]].erase(prv[p[val]].begin() + idx); n         int k = p[val]; n         while (val % k == 0) val /= k; n       } n       if (val > 1) { n         int idx = lower_bound(prv[val].begin(), prv[val].end(), id) - n                   prv[val].begin(); n         prv[val].erase(prv[val].begin() + idx); n       } n       val = a[x] = y; n       while (p[val] > 0) { n         prv[p[val]].push_back(id); n         sort(prv[p[val]].begin(), prv[p[val]].end()); n         int k = p[val]; n         while (val % k == 0) val /= k; n       } n       if (val > 1) { n         prv[val].push_back(id); n         sort(prv[val].begin(), prv[val].end()); n       } n     } n   } n } n int main() { n   prime(); n   scanf( %d%d , &n, &q); n   for (int i = 1; i <= n; i++) scanf( %d , &a[i]); n   for (int i = 1; i < n; i++) { n     int x, y; n     scanf( %d%d , &x, &y); n     edge[x].push_back(y); n     edge[y].push_back(x); n   } n   build(1); n   sol(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T> n T gcd(T a, T b) { n   if (a == 0) return b; n   return gcd(b % a, a); n } n template <typename T> n T pow(T a, T b, long long m) { n   T ans = 1; n   while (b > 0) { n     if (b % 2 == 1) ans = ((ans % m) * (a % m)) % m; n     b /= 2; n     a = ((a % m) * (a % m)) % m; n   } n   return ans % m; n } n int n; n int a[1005]; n int dp[1005][1005]; n int solve(int f, int s) { n   if (f > n) return 0; n   if (s == n) { n     return max(a[f], a[s]); n   } n   if (s > n) { n     return a[f]; n   } n   if (dp[f][s] != -1) return dp[f][s]; n   int ans = (long long)1e9 + 2; n   ans = min(ans, max(a[f], a[s]) + solve(s + 1, s + 2)); n   ans = min(ans, max(a[f], a[s + 1]) + solve(s, s + 2)); n   ans = min(ans, max(a[s], a[s + 1]) + solve(f, s + 2)); n   return dp[f][s] = ans; n } n void print(int f, int s) { n   if (f > n) return; n   if (s == n) { n     cout << f <<     << s <<   n ; n     return; n   } n   if (s > n) { n     cout << f; n     return; n   } n   if (dp[f][s] == max(a[f], a[s]) + solve(s + 1, s + 2)) { n     cout << f <<     << s <<   n ; n     print(s + 1, s + 2); n   } else if (dp[f][s] == max(a[f], a[s + 1]) + solve(s, s + 2)) { n     cout << f <<     << s + 1 <<   n ; n     print(s, s + 2); n   } else if (dp[f][s] == max(a[s], a[s + 1]) + solve(f, s + 2)) { n     cout << s <<     << s + 1 <<   n ; n     print(f, s + 2); n   } n } n int main() { n   cin >> n; n   for (int i = 1; i <= n; i++) cin >> a[i]; n   memset(dp, -1, sizeof dp); n   cout << solve(1, 2) <<   n ; n   print(1, 2); n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXH = 200000 + 10, MAXN = 2000 + 10; n const long long MOD = 1000000007; n struct Point { n   int x, y; n   bool operator<(const Point &p1) const { return x + y < p1.x + p1.y; } n } p[MAXN]; n long long fac[MAXH], inv[MAXH]; n long long f[MAXN]; n inline long long Fastpow(long long a, long long b) { n   long long res = 1; n   while (b) { n     if (b & 1) (res *= a) %= MOD; n     (a *= a) %= MOD; n     b >>= 1; n   } n   return res; n } n long long Calc(int a, int b) { n   return ((((fac[a + b - 2] * inv[a - 1]) % MOD) * inv[b - 1]) % MOD); n } n int main() { n   int h, w, n, mx; n   scanf( %d%d%d , &h, &w, &n); n   mx = h + w; n   for (int i = 1; i <= n; ++i) scanf( %d%d , &p[i].x, &p[i].y); n   p[++n] = (Point){h, w}; n   sort(p + 1, p + 1 + n); n   fac[0] = inv[0] = 1; n   for (int i = 1; i <= mx; ++i) fac[i] = fac[i - 1] * (long long)i % MOD; n   for (int i = 1; i <= mx; ++i) inv[i] = Fastpow(fac[i], MOD - 2); n   for (int i = 1; i <= n; ++i) { n     f[i] = Calc(p[i].x, p[i].y); n     for (int j = 1; j < i; ++j) n       if (p[j].x <= p[i].x && p[j].y <= p[i].y) { n         f[i] -= f[j] * Calc(p[i].x - p[j].x + 1, p[i].y - p[j].y + 1) % MOD; n         ((f[i] %= MOD) += MOD) %= MOD; n       } n   } n   cout << f[n] << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, x = 0, y = 0; n   cin >> n; n   int *C = new int[n]; n   for (int i = 0; i < n; i++) { n     cin >> C[i]; n   } n   for (int j = 0; j < n; j++) { n     if (j == 0) { n       y = abs(C[j] - C[j + 1]); n       x = abs(C[j] - C[n - 1]); n     } else if (j == n - 1) { n       x = max(abs(C[j] - C[n - 1]), abs(C[j] - C[0])); n       y = abs(C[j] - C[j - 1]); n     } else { n       x = max(abs(C[j] - C[n - 1]), abs(C[j] - C[0])); n       y = min(abs(C[j] - C[j + 1]), abs(C[j] - C[j - 1])); n     } n     cout << y <<     << x << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int modul(int a, int b) { n   if (a - b < 0) return b - a; n   return a - b; n } n int main() { n   int n, k, st, et, s, e, d, v[103], j, m, stm, etm; n   cin >> n >> k; n   st = et = 0; n   d = 0; n   for (int i = 1; i <= n; i++) { n     cin >> v[i]; n     if (v[i] == 1) n       et++; n     else n       st++; n   } n   stm = etm = 0; n   for (int i = 1; i <= k; i++) { n     s = st; n     e = et; n     j = i; n     while (j <= n) { n       if (v[j] == 1) n         e--; n       else n         s--; n       j += k; n     } n     m = modul(s, e); n     if (m > d) { n       stm = s; n       etm = e; n       d = m; n     } n   } n   cout << d; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1010, L = 33, B = 7340033, root = 3; n int dp[L][N << 2]; n int mypw(int x, int k) { n   int ret = 1; n   while (k) { n     if (k & 1) ret = 1ll * ret * x % B; n     k >>= 1; n     x = 1ll * x * x % B; n   } n   return ret; n } n void change(int y[], int l) { n   for (int i = 1, j = l / 2; i < l - 1; ++i) { n     if (i < j) swap(y[i], y[j]); n     int k = l / 2; n     while (j >= k) { n       j -= k; n       k >>= 1; n     } n     if (j < k) j += k; n   } n } n void fft(int y[], int l, int on) { n   change(y, l); n   for (int h = 2; h <= l; h <<= 1) { n     int wn = mypw(root, (B - 1) / h); n     if (on == -1) wn = mypw(wn, B - 2); n     for (int j = 0; j < l; j += h) { n       int w = 1; n       for (int k = j; k < j + h / 2; ++k) { n         int u = y[k]; n         int t = 1ll * y[k + h / 2] * w % B; n         y[k] = u + t; n         if (y[k] >= B) y[k] -= B; n         y[k + h / 2] = u + B - t; n         if (y[k + h / 2] >= B) y[k + h / 2] -= B; n         w = 1ll * w * wn % B; n       } n     } n   } n   if (on == -1) { n     int inv = mypw(l, B - 2); n     for (int i = 0; i < l; ++i) { n       y[i] = 1ll * y[i] * inv % B; n     } n   } n } n void calc(int a[], int b[], int l) { n   l *= 2; n   for (int i = 0; i < l; ++i) { n     if (i < l / 2) n       a[i] = b[i]; n     else n       a[i] = 0; n   } n   fft(a, l, 1); n   for (int i = 0; i < l; ++i) a[i] = 1ll * a[i] * a[i] % B; n   fft(a, l, -1); n } n void init() { n   dp[0][0] = 1; n   for (int i = 1; i < L; ++i) { n     calc(dp[i], dp[i - 1], 1 << 10); n     calc(dp[i], dp[i], 1 << 10); n     for (int j = (1 << 10) - 1; j > 0; --j) { n       dp[i][j] = dp[i][j - 1]; n     } n     dp[i][0] = 1; n   } n } n int main() { n   int q; n   init(); n   scanf( %d , &q); n   while (q--) { n     int n, k; n     scanf( %d%d , &n, &k); n     int cnt = 0; n     while (n > 1 && (n & 1)) { n       ++cnt; n       n >>= 1; n     } n     printf( %d n , dp[cnt][k]); n   } n   return 0; n } n


#include <bits/stdc++.h> n const int BUFFSIZE = 10240; n char BUFF[BUFFSIZE + 1], *p = BUFF; n int CHAR, SIGN, BYTES = 0; n using namespace std; n vector<long long> V; n void dfs(long long N) { n   if (N > 5000000000LL) return; n   V.push_back(N); n   dfs(N * 10 + 4); n   dfs(N * 10 + 7); n } n int main() { n   dfs(0); n   sort(V.begin(), V.end()); n   long long pl, pr, vl, vr, k; n   cin >> pl >> pr >> vl >> vr >> k; n   double den = (pr - pl + 1) * (vr - vl + 1); n   double num = 0; n   for (int i = 1; i + k - 1 < V.size(); i++) { n     long long x = V[i]; n     long long y = V[i + k - 1]; n     long long lftp = V[i - 1] + 1; n     long long rgtp = x; n     long long lftv = y; n     long long rgtv = V[i + k] - 1; n     lftp = max(lftp, pl); n     rgtp = min(rgtp, pr); n     lftv = max(lftv, vl); n     rgtv = min(rgtv, vr); n     if (lftp <= rgtp && lftv <= rgtv) { n       double prob = (rgtp - lftp + 1.0) * (rgtv - lftv + 1.0); n       num += prob; n     } n     lftv = V[i - 1] + 1; n     rgtv = x; n     lftp = y; n     rgtp = V[i + k] - 1; n     lftp = max(lftp, pl); n     rgtp = min(rgtp, pr); n     lftv = max(lftv, vl); n     rgtv = min(rgtv, vr); n     if (lftp <= rgtp && lftv <= rgtv) { n       double prob = (rgtp - lftp + 1.0) * (rgtv - lftv + 1.0); n       num += prob; n     } n   } n   if (k == 1) { n     for (int i = 1; i < V.size(); i++) n       if (V[i] >= pl && V[i] <= pr && V[i] >= vl && V[i] <= vr) num -= 1; n   } n   double res = 1.0 * num / den; n   printf( %.20lf n , res); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n using ll = long long; n bitset<10000> B; n int BIT[100010]; n int n; n void update(int pos, int x) { n   for (; pos < n; pos |= pos + 1) BIT[pos] += x; n } n int qu(int pos) { n   int ret = 0; n   for (; pos; pos &= pos - 1) ret += BIT[pos - 1]; n   return ret; n } n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   for (int len = 1; len <= 4; ++len) { n     for (int mask = 0; mask < 1 << len; ++mask) { n       int num = 0; n       for (int bit = 0; bit < len; ++bit) n         if (mask & (1 << bit)) n           num = num * 10 + 4; n         else n           num = num * 10 + 7; n       B[num] = 1; n     } n   } n   int m; n   cin >> n >> m; n   int a[n]; n   for (int i = 0; i < n; ++i) { n     cin >> a[i]; n     if (B[a[i]]) update(i, 1); n   } n   while (m--) { n     string type; n     int l, r; n     cin >> type >> l >> r; n     if (type ==  count ) { n       cout << qu(r) - qu(l - 1) <<   n ; n     } else { n       int d; n       cin >> d; n       for (int i = l - 1; i <= r - 1; ++i) { n         if (B[a[i]]) update(i, -1); n         a[i] += d; n         if (B[a[i]]) update(i, 1); n       } n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n const long long mod = 1e9 + 7; n const int maxn = 1e5 + 5; n long long dp[205][15005]; n long long p2[205], p5[205]; n long long a[205]; n int n, k; n int main() { n   cin >> n >> k; n   for (int i = 1; i <= n; i++) { n     cin >> a[i]; n     while (a[i] % 2 == 0) a[i] /= 2, p2[i]++; n     while (a[i] % 5 == 0) a[i] /= 5, p5[i]++; n   } n   memset(dp, -0x3f3f3f3f, sizeof(dp)); n   dp[0][0] = 0; n   for (int i = 1; i <= n; i++) { n     for (int j = k; j >= 1; j--) { n       for (int k = 12005; k >= p2[i]; k--) { n         dp[j][k] = max(dp[j][k], dp[j - 1][k - p2[i]] + p5[i]); n       } n     } n   } n   long long mx = 0; n   for (long long i = 0; i <= 12005; i++) { n     mx = max(mx, min(i, dp[k][i])); n   } n   printf( %lld n , mx); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n map<string, int> types; n int count(const string &data) { n   int amps = 0, stars = 0, i, j; n   for (i = 0; i < (int)data.size() && data[i] ==  & ; i++, amps++) n     ; n   for (j = data.size() - 1; j >= 0 && data[j] ==  * ; j--, stars++) n     ; n   string t = data.substr(i, j - i + 1); n   if (!types.count(t) || types[t] == -1) return -1; n   int answer = types[t]; n   answer += stars - amps, answer = max(-1, answer); n   return answer; n } n int n; n string command, a, b; n int main() { n   cin >> n; n   types[ void ] = 0, types[ errtype ] = -1; n   for (int i = 0; i < n; i++) { n     cin >> command >> a; n     if (command ==  typedef ) n       cin >> b, types[b] = count(a); n     else { n       int p = count(a); n       if (p == -1) n         cout <<  errtype  << endl; n       else { n         cout <<  void ; n         for (int i = 0; i < p; i++) cout <<  * ; n         cout << endl; n       } n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int Case, n, sz, ans, A[300000 + 5], Min[300000 + 5], Max[300000 + 5], n     Id[300000 + 5]; n int main() { n   for (scanf( %d , &Case); Case; Case--) { n     scanf( %d , &n); n     for (int i = 1; i <= n; i++) Id[i] = 0, Min[i] = n + 1, Max[i] = 0; n     for (int i = 1; i <= n; i++) { n       scanf( %d , A + i); n       Id[A[i]] = 1; n     } n     for (int i = 1; i <= n; i++) Id[i] += Id[i - 1]; n     sz = Id[n]; n     for (int i = 1; i <= n; i++) { n       A[i] = Id[A[i]]; n       Min[A[i]] = min(Min[A[i]], i); n       Max[A[i]] = max(Max[A[i]], i); n     } n     ans = sz; n     for (int l = 1, r = 0, mx; l <= sz; l++) { n       if (r < l) r = l, mx = Max[l]; n       for (; r < sz && Min[r + 1] > mx; r++) mx = Max[r + 1]; n       ans = min(ans, sz - (r - l + 1)); n     } n     printf( %d n , ans); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int k; n long long int m, lim = 2e18; n long long int dp[70][70][2]; n long long int solve(string s, int pos, int k, bool f) { n   if (k < 0) return 0; n   if (pos == (int)s.size()) { n     return (k == 0); n   } n   if (dp[pos][k][f] != -1) return dp[pos][k][f]; n   long long int ret = 0; n   for (char c =  0 ; c <=  1 ; ++c) { n     if (!f and s[pos] < c) continue; n     ret += solve(s, pos + 1, k - (c ==  1 ), f | (s[pos] > c)); n   } n   return dp[pos][k][f] = ret; n } n long long int get(string s) { n   if (s ==  1 ) { n     if (k == 1) n       return 1; n     else n       return 0; n   } n   memset(dp, -1, sizeof dp); n   return solve(s, 0, k, 0); n } n string binary(long long int num) { n   string ret =   ; n   while (num) { n     if (num & 1) n       ret +=  1 ; n     else n       ret +=  0 ; n     num >>= 1LL; n   } n   reverse((ret).begin(), (ret).end()); n   return ret; n } n long long int search(long long int l, long long int r) { n   long long int mid = (l + r) / 2LL; n   long long int tot = get(binary(2 * mid)) - get(binary(mid)); n   if (tot == m) n     return mid; n   else if (tot < m) n     return search(mid + 1, r); n   return search(l, mid - 1); n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   while (cin >> m >> k) { n     long long int ans = search(1LL, lim); n     cout << ans <<   n ; n   } n } n


#include <bits/stdc++.h> n int arr[1005]; n int n; n using namespace std; n int main() { n   int n; n   scanf( %d , &n); n   int sum = 0; n   for (int i = 0; i < 505; i++) { n     sum += i; n     arr[i + 1] = sum; n   } n   if (n == 0) { n     printf( a n ); n     return 0; n   } n   for (int i = 0; i < 26; i++) { n     if (n == 0) { n       return 0; n     } n     int howmany = lower_bound(arr, arr + 500, n) - arr; n     if (arr[howmany] == n) { n       for (int j = 0; j < howmany; j++) { n         printf( %c ,  a  + i); n       } n       printf(  n ); n       return 0; n     } n     if (arr[howmany] > n) { n       n -= arr[howmany - 1]; n       for (int j = 0; j < howmany - 1; j++) { n         printf( %c ,  a  + i); n       } n       continue; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long n; n   cin >> n; n   vector<long long> p(n); n   for (auto& i : p) cin >> i; n   vector<long long> dp(n, 1); n   vector<int> exist(n, false); n   for (long long i = 0; i < (n); ++i) { n     exist[p[i] - 1] = true; n     if (p[i] != 1) n       if (exist[p[i] - 2]) { n         dp[p[i] - 1] = 1 + dp[p[i] - 2]; n       } n   } n   long long max_c = 0; n   for (long long i = 0; i < (n); ++i) max_c = max(max_c, dp[i]); n   cout << n - max_c << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long N = 5 * 1e5 + 1; n int main() { n   long long q; n   cin >> q; n   vector<long long> v(N); n   for (int i = 0; i <= N; i++) { n     v[i] = i; n   } n   deque<long long> dq; n   vector<pair<long long, long long>> pq(q); n   long long idx = 0; n   while (q--) { n     long long a; n     cin >> a; n     if (a == 1) { n       long long b; n       cin >> b; n       pq[idx].first = b; n       pq[idx].second = 0; n       idx++; n     } else { n       long long b, c; n       cin >> b >> c; n       pq[idx].first = b; n       pq[idx].second = c; n       idx++; n     } n   } n   for (int i = idx - 1; i >= 0; i--) { n     if (pq[i].first != 0 && pq[i].second != 0) { n       v[pq[i].first] = v[pq[i].second]; n     } else { n       dq.push_front(v[pq[i].first]); n     } n   } n   for (auto it : dq) { n     cout << it <<    ; n   } n   cout << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int cap = 1e3; n int n, m; n char grid[cap][cap]; n int vis[4][cap][cap] = {}; n vector<pair<int, int>> dirs = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}}; n bool dfs(int i, int j, int d, int t) { n   if (i < 0 || i == n || j < 0 || j == m || grid[i][j] ==  *  || n       (vis[d][i][j] != -1 && t >= vis[d][i][j])) n     return false; n   if (grid[i][j] ==  T ) return true; n   vis[d][i][j] = t; n   int x, y; n   x = i + dirs[d].first; n   y = j + dirs[d].second; n   if (dfs(x, y, d, t)) return true; n   if (t < 2) n     for (int k = 0; k < 4; ++k) n       if (k != d) { n         x = i + dirs[k].first; n         y = j + dirs[k].second; n         if (dfs(x, y, k, t + 1)) return true; n       } n   return false; n } n int main() { n   memset(vis, -1, sizeof vis); n   scanf( %d %d , &n, &m); n   int i_s = -1, j_s = -1; n   for (int i = 0; i < n; ++i) n     for (int j = 0; j < m; ++j) { n       scanf(  %c , &grid[i][j]); n       if (grid[i][j] ==  S ) { n         i_s = i; n         j_s = j; n       } n     } n   for (int d = 0; d < 4; ++d) n     if (dfs(i_s, j_s, d, 0)) { n       printf( YES ); n       return 0; n     } n   printf( NO ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long n, pos, ans = 10000000000000000; n long long a[3000 + 5], b[3000 + 5], f[3000 + 5][3000 + 5], Min[3000 + 5]; n long long read() { n   char c; n   long long x = 0, f = 1; n   c = getchar(); n   while (c >  9  || c <  0 ) { n     if (c ==  - ) f = -1; n     c = getchar(); n   } n   while (c >=  0  && c <=  9 ) { n     x = (x << 3) + (x << 1) + c -  0 ; n     c = getchar(); n   } n   return x * f; n } n signed main() { n   n = read(); n   for (register long long i = 1; i <= n; ++i) a[i] = read() - i, b[i] = a[i]; n   sort(b + 1, b + n + 1); n   pos = unique(b + 1, b + n + 1) - b - 1; n   for (register long long i = 1; i <= n; ++i) { n     for (register long long j = 1; j <= pos; ++j) n       f[i][j] = Min[j] + abs(a[i] - b[j]); n     Min[0] = 10000000000000000; n     for (register long long j = 1; j <= pos; ++j) n       Min[j] = min(Min[j - 1], f[i][j]); n   } n   for (register long long i = 1; i <= pos; ++i) ans = min(ans, f[n][i]); n   printf( %lld , ans); n   return 0; n } n


#include <bits/stdc++.h> n #define int long long int n #define us unsigned int n #define null 0 n #define m_p make_pair n #define f_i first n #define s_e second n #define p_b push_back n  n using namespace std; n  n  n void IOS() n { n     ios_base::sync_with_stdio(false); n     cin.tie(NULL); n } n  n  n  n  n  n int32_t main() n { n     IOS(); n int test; n cin>>test; n while(test--) n { n     int n; n     cin>>n; n     string s; n     cin>>s; n     map<char,int> mp; n     bool flag=true; n  n     for(int i=0;i<n;i++) n     { n         if(mp.find(s[i])==mp.end()) n         { n             mp[s[i]]=i; n         } n         else if(i-mp[s[i]]>1) n         { n             flag=false; n             break; n         } n         mp[s[i]]=i; n     } n  n     if(flag) n     { n         cout<< YES <<endl; n     } n  n     else n     { n         cout<< NO <<endl; n     } n } n  n } n  n  n  n  n  n  n  n  n  n  n  n  n  n  n  n  n  n  n


#include <bits/stdc++.h> n using namespace std; n const int Y = 10; n int n; n string fcol =   ; n bool place(int y, int x) { n   cout << x <<     << y << endl; n   string ret; n   cin >> ret; n   return (ret == fcol); n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cin >> n; n   cout <<  0   << Y << endl; n   cin >> fcol; n   bool lastbl = true; n   int cx = 0; n   int jmp = (1 << 28); n   for (int i = 0; i < n - 1; ++i) { n     if (lastbl) { n       lastbl = place(Y, cx + jmp); n       cx += jmp; n     } else { n       lastbl = place(Y, cx - jmp); n       cx -= jmp; n     } n     jmp /= 2; n   } n   if (lastbl) { n     cout << cx <<     << Y + 1 <<     << cx + 1 <<     << Y - 1 << endl; n   } else { n     cout << cx <<     << Y + 1 <<     << cx - 1 <<     << Y - 1 << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct FlowEdge { n   int to, cap, rev; n   FlowEdge(int to_, int cap_, int rev_) : to(to_), cap(cap_), rev(rev_) {} n }; n class FordFulkerson { n  public: n   FordFulkerson() {} n   FordFulkerson(int n) : G(n), used(n) {} n   void add(int from, int to, int cap) { n     G[from].emplace_back(to, cap, (int)G[to].size()); n     G[to].emplace_back(from, 0, (int)G[from].size() - 1); n   } n   int get(int s, int t) { n     int f = 1, res = 0; n     while (f) { n       fill(begin(used), end(used), false); n       f = dfs(s, t, INT_MAX); n       res += f; n     } n     return res; n   } n  n  private: n   vector<vector<FlowEdge>> G; n   vector<bool> used; n   int dfs(int v, int t, int f) { n     if (v == t) return f; n     used[v] = 1; n     for (auto &e : G[v]) { n       if (!used[e.to] && e.cap > 0) { n         int d = dfs(e.to, t, min(e.cap, f)); n         if (d > 0) { n           e.cap -= d; n           G[e.to][e.rev].cap += d; n           return d; n         } n       } n     } n     return 0; n   } n }; n const string T =  fair n , F =  unfair n ; n const int MAXV = 20099, SRC = 0, QUERY = 10001, SINK = 20010, MOD5 = 20001; n int N, B, Q, upTo[10099], quantity[10099]; n pair<int, int> query[10099]; n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cin >> N >> B >> Q; n   for (int i = 0; i < (int)Q; ++i) cin >> upTo[i] >> quantity[i]; n   for (int i = 0; i < (int)Q; ++i) query[i] = make_pair(upTo[i], quantity[i]); n   query[Q] = make_pair(0, 0); n   query[Q] = make_pair(B, N); n   Q += 2; n   sort(query, query + Q); n   FordFulkerson flowG(MAXV); n   for (int i = 0; i < (int)5; ++i) flowG.add(SRC, MOD5 + i, N / 5); n   for (int num = 1; num < (int)B + 1; ++num) { n     int mod5 = num % 5; n     flowG.add(MOD5 + mod5, num, 1); n   } n   for (int i = 0; i < (int)Q - 1; ++i) { n     int lef = query[i].first, rig = query[i + 1].first; n     int cnt = query[i + 1].second - query[i].second; n     if (cnt < 0) { n       cout << F; n       return 0; n     } n     for (int num = lef + 1; num <= rig; ++num) { n       flowG.add(num, QUERY + i, 1); n     } n     flowG.add(QUERY + i, SINK, cnt); n   } n   int maxFlow = flowG.get(SRC, SINK); n   cout << (maxFlow == N ? T : F); n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int t, x = 0, k = 0; n   cin >> t; n   while (t--) { n     int m, n, x, y = 0; n     cin >> m; n     cin >> n; n     x = m * n; n     int a[x]; n     for (int i = 0; i < x; i++) { n       cin >> a[i]; n       if (a[i] < 0) { n         a[i] = 0 - a[i]; n         k++; n       } n       y = y + a[i]; n     } n     sort(a, a + x); n     if (k % 2 == 0) n       cout << y << endl; n     else n       cout << y - 2 * a[0] << endl; n     k = 0; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long MOD = 1000000007; n void runCase(int caseNum) { n   string s; n   cin >> s; n   int res = true; n   if (s.size()) { n     stack<char> st; n     st.push(s[0]); n     for (int i = 1; i < s.size(); ++i) { n       if (!st.empty() && st.top() == s[i]) n         st.pop(); n       else n         st.push(s[i]); n     } n     res = st.empty(); n   } n   cout << (res ?  Yes  :  No ) << endl; n } n int main(int argc, char* argv[]) { n   runCase(0); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long MAGIC = 400; n const long long N = 1e5 + 2; n const long long inf = 1e14 + 2; n long long lzval[MAGIC], lzmul[MAGIC], ar[N], add[N]; n bool used[N]; n vector<long long> idx[MAGIC]; n struct hull { n   long long ptr; n   struct line { n     long long a, b, idx; n     long long y(long long x) { return a * x + b; } n   }; n   vector<line> hull; n   bool bad(line x, line y, line z) { n     assert(y.a != x.a); n     if (z.a == y.a) { n       return z.b > y.b; n     } n     return (1.0 * (x.b - z.b)) / (z.a - x.a) < n            (1.0 * (y.b - z.b)) / (z.a - y.a); n   } n   void insert(line a) { n     while (hull.size() > 1) { n       if (bad(hull[hull.size() - 2], hull[hull.size() - 1], a)) { n         hull.pop_back(); n       } else { n         break; n       } n     } n     if (hull.size() && hull.back().a == a.a) { n       if (hull.back().b > a.b) { n         return; n       } else { n         hull.pop_back(); n       } n     } n     hull.push_back(a); n   } n   void build(vector<long long> &wow) { n     hull.clear(); n     ptr = 0; n     for (long long i = 0; i < wow.size(); i++) { n       long long idx = wow[i]; n       if (used[idx]) { n         continue; n       } n       line tmp; n       tmp.a = ar[idx]; n       tmp.b = add[idx]; n       tmp.idx = idx; n       insert(tmp); n     } n   } n   pair<long long, long long> get(long long idx) { n     while (ptr + 1 < hull.size() && n            hull[ptr].y(lzmul[idx]) < hull[ptr + 1].y(lzmul[idx])) { n       ptr++; n     } n     return pair<long long, long long>(hull[ptr].y(lzmul[idx]) + lzval[idx], n                                       -hull[ptr].idx); n   } n } lis[MAGIC]; n long long grp(long long x) { return (x + MAGIC - 1) / MAGIC; } n bool cmp(long long x, long long y) { n   if (ar[x] != ar[y]) { n     return ar[x] > ar[y]; n   } n   return add[x] > add[y]; n } n signed main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   long long n, i, j, k, l, mx, ans = 0, mxans = 0, z, q; n   cin >> n; n   for (i = 1; i <= n; i++) { n     cin >> ar[i]; n     add[i] = ar[i]; n     idx[grp(i)].push_back(i); n   } n   mx = grp(n); n   for (i = 1; i <= mx; i++) { n     sort(idx[i].begin(), idx[i].end(), cmp); n     lis[i].build(idx[i]); n   } n   for (i = 1; i <= n; i++) { n     pair<long long, long long> res = {-inf, -1}; n     for (j = 1; j <= mx; j++) { n       if (lis[j].hull.size()) { n         res = max(res, lis[j].get(j)); n         pair<long long, long long> kek = lis[j].get(j); n       } n     } n     ans += res.first; n     mxans = max(mxans, ans); n     q = j = -res.second; n     used[j] = true; n     j = grp(j); n     for (k = j + 1; k <= mx; k++) { n       lzmul[k]++; n     } n     for (k = 1; k < j; k++) { n       lzval[k] += ar[q]; n     } n     k = (j - 1) * MAGIC + 1, l = min(j * MAGIC, n); n     for (z = k; z <= l; z++) { n       add[z] += lzval[j] + lzmul[j] * ar[z]; n       if (z >= q) { n         add[z] += ar[z]; n       } else { n         add[z] += ar[q]; n       } n     } n     lzval[j] = lzmul[j] = 0; n     lis[j].build(idx[j]); n   } n   cout << mxans; n } n


#include <bits/stdc++.h> n using namespace std; n long long m[1000001], cd, kq[100000]; n map<long long, long long> ruler; n int main() { n   cin >> cd; n   for (long long i = 1; i <= cd; i++) { n     cin >> m[i]; n     ruler[m[i]] = 1; n   } n   for (long long i = 1; i <= cd; i++) { n     long long e = 1; n     kq[e] = m[i]; n     long long t = m[i]; n     while (e <= cd) { n       if (ruler[t * 2] == 1) { n         kq[e + 1] = t * 2; n         t = t * 2; n       } else if (t % 3 == 0 && ruler[t / 3] == 1) { n         kq[e + 1] = t / 3; n         t = t / 3; n       } else n         break; n       e++; n       if (e == cd) { n         for (long long k = 1; k <= cd; k++) cout << kq[k] <<    ; n         exit(0); n       } n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n map<string, int> A; n string s; n int main() { n   A[ void ] = 100; n   A[ errtype ] = 0; n   int n; n   cin >> n; n   for (int i = 0; i < n; ++i) { n     cin >> s; n     if (s[4] ==  d ) { n       string a, b; n       cin >> a >> b; n       int l = a.length(); n       int p = 0, q = 0; n       for (int j = 0; j < l; ++j) n         if (a[j] ==  & ) n           ++p; n         else if (a[j] ==  * ) n           ++q; n       a = a.substr(p, l - p - q); n       q -= p; n       if (A[a] + q < 100) n         A[b] = 0; n       else n         A[b] = A[a] + q; n     } else { n       string a; n       cin >> a; n       int p = 0, q = 0; n       int l = a.length(); n       for (int j = 0; j < l; ++j) n         if (a[j] ==  & ) n           ++p; n         else if (a[j] ==  * ) n           ++q; n       a = a.substr(p, l - p - q); n       q -= p; n       if (A[a] + q < 100) n         puts( errtype ); n       else { n         printf( void ); n         int t = A[a] + q; n         for (int j = 100; j < t; ++j) printf( * ); n         puts(  ); n       } n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using std::map; n const int MAXN = 1000 + 3; n int table[MAXN][MAXN]; n int n, m, k; n map<int, int> r, c; n int main(int argc, char *argv[]) { n   scanf( %d%d%d , &n, &m, &k); n   for (int i = 1; i <= n; ++i) { n     r[i] = i; n     for (int j = 1; j <= m; ++j) { n       if (i == 1) c[j] = j; n       scanf( %d , &table[i][j]); n     } n   } n   char cmd; n   int tmp, x, y; n   while (k--) { n     scanf( %s%d%d , &cmd, &x, &y); n     if (cmd ==  r ) { n       tmp = r[x]; n       r[x] = r[y]; n       r[y] = tmp; n     } else if (cmd ==  c ) { n       tmp = c[x]; n       c[x] = c[y]; n       c[y] = tmp; n     } else n       printf( %d n , table[r[x]][c[y]]); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long i, j, d = 0, e = 0, f = 0, sum = 0, c = 0, n; n   cin >> n; n   char a[4][4]; n   map<int, int> m; n   for (i = 0; i < 4; i++) { n     for (j = 0; j < 4; j++) { n       cin >> a[i][j]; n     } n   } n   for (i = 0; i < 4; i++) { n     for (j = 0; j < 4; j++) { n       if (a[i][j] >= 49 && a[i][j] <= 57) { n         m[a[i][j]]++; n       } n     } n   } n   for (auto x : m) { n     if (x.second > (2 * n)) { n       c++; n     } n   } n   if (c == 0) { n     cout <<  YES ; n   } else n     cout <<  NO ; n } n


#include <bits/stdc++.h> n using namespace std; n using ll = long long; n const int MOD = (int)998244353; n const int MAXN = (int)1e6 + 5; n template <typename T> n class BIT { n  public: n   BIT(T* _a, int _n) { n     a = _a; n     n = _n; n     bit = new T[n + 1]; n     for (int i = 1; i <= n; ++i) bit[i] = 0; n     for (int i = 1; i <= n; ++i) upd_(i, a[i]); n   } n   void upd(int i, T val) { n     T diff = val - a[i]; n     a[i] = val; n     while (i <= n) { n       bit[i] = bit[i] + diff; n       i += i & (-i); n     } n   } n   T get(int i) { n     T ret = 0; n     while (i > 0) { n       ret = ret + bit[i]; n       i -= i & (-i); n     } n     return ret; n   } n  n  private: n   void upd_(int i, T val) { n     while (i <= n) { n       bit[i] = bit[i] + val; n       i += i & (-i); n     } n   } n   T *bit, *a; n   int n; n }; n ll n, k, m, a1[MAXN], a2[MAXN], a3[MAXN]; n struct Segment { n   Segment(int _l, int _r, int _c, int _p) : l(_l), r(_r), c(_c), p(_p) { n     end = 0; n   }; n   int l, r, c, p; n   bool end; n   int pos; n   int get_coord() { n     if (end) return r; n     return l; n   } n }; n bool cmp1(Segment& lhs, Segment& rhs) { return lhs.p < rhs.p; } n bool cmp2(Segment& lhs, Segment& rhs) { n   int lhsx = (lhs.end == 1) ? lhs.r : lhs.l; n   int rhsx = (rhs.end == 1) ? rhs.r : rhs.l; n   if (lhsx == rhsx) return lhs.end < rhs.end; n   return lhsx < rhsx; n } n vector<Segment> segs; n bool can(BIT<ll>& bit1, BIT<ll>& bit2, int pos) { return (bit1.get(pos) >= k); } n int main() { n   cin >> n >> k >> m; n   for (int i = 0; i < m; ++i) { n     int l, r, c, p; n     scanf( %d%d%d%d , &l, &r, &c, &p); n     Segment seg(l, r, c, p); n     segs.push_back(seg); n   } n   sort(segs.begin(), segs.end(), cmp1); n   int sz = segs.size(); n   for (int i = 1; i <= sz; ++i) { n     segs[i - 1].pos = i; n     a3[i] = segs[i - 1].p; n     Segment seg = segs[i - 1]; n     seg.end = 1; n     segs.push_back(seg); n   } n   sort(segs.begin(), segs.end(), cmp2); n   ll sol = 0; n   BIT<ll> bit1(a1, m), bit2(a2, m); n   int segpos = 0; n   for (int i = 1; i <= n; ++i) { n     while (segpos < 2 * m && n            (segs[segpos].get_coord() < i || n             (segs[segpos].get_coord() == i && segs[segpos].end == 0))) { n       int pos = segs[segpos].pos; n       if (segs[segpos].end == 0) { n         bit1.upd(pos, segs[segpos].c); n         bit2.upd(pos, 1LL * segs[segpos].c * segs[segpos].p); n       } else { n         bit1.upd(pos, 0); n         bit2.upd(pos, 0); n       } n       segpos++; n     } n     ll solc = m; n     int l = 1, r = m; n     while (l <= r) { n       int mid = (l + r) / 2; n       if (can(bit1, bit2, mid)) { n         solc = min(solc, (ll)mid); n         r = mid - 1; n       } else n         l = mid + 1; n     } n     if (bit1.get(m) < k) n       sol += bit2.get(m); n     else { n       ll diff = bit1.get(solc) - k; n       sol += bit2.get(solc) - diff * a3[solc]; n     } n   } n   cout << sol; n   return 0; n } n


#include <bits/stdc++.h> n struct node { n   int mx, se, cmx; n   int cnt; n   long long sum; n   node() : mx(0), se(-1), cmx(0), cnt(0), sum(0) {} n   node(int v) : mx(v), se(-1), cmx(1), cnt(1), sum(v) {} n }; n node operator+(const node &a, const node &b) { n   node res; n   if (a.mx == b.mx) { n     res.mx = a.mx; n     res.se = std::max(a.se, b.se); n     res.cmx = a.cmx + b.cmx; n   } else if (a.mx > b.mx) { n     res.mx = a.mx; n     res.se = std::max(a.se, b.mx); n     res.cmx = a.cmx; n   } else { n     res.mx = b.mx; n     res.se = std::max(a.mx, b.se); n     res.cmx = b.cmx; n   } n   res.cnt = a.cnt + b.cnt; n   res.sum = a.sum + b.sum; n   return res; n } n int enlarge(int n) { n   int res = 1; n   while (res < n) { n     res <<= 1; n   } n   return res; n } n class segment_tree { n   int n; n   std::vector<node> a; n   std::vector<int> tag, mxtag; n   void node_add(int u, int v) { n     a[u].mx += v; n     a[u].se += v; n     a[u].sum += 1ll * v * a[u].cnt; n     tag[u] += v; n   } n   void node_add_mx(int u, int v) { n     a[u].mx += v; n     a[u].sum += 1ll * v * a[u].cmx; n     mxtag[u] += v; n   } n   void down(int u) { n     if (tag[u]) { n       node_add(u << 1, tag[u]); n       node_add(u << 1 | 1, tag[u]); n       tag[u] = 0; n     } n     if (mxtag[u]) { n       int lmx = a[u << 1].mx, rmx = a[u << 1 | 1].mx; n       if (lmx >= rmx) { n         node_add_mx(u << 1, mxtag[u]); n       } n       if (rmx >= lmx) { n         node_add_mx(u << 1 | 1, mxtag[u]); n       } n       mxtag[u] = 0; n     } n   } n   void modify(int u, int l, int r, int x, int v) { n     if (l + 1 == r) { n       a[u] = node(v); n       return; n     } n     int mid = (l + r + 1) >> 1; n     down(u); n     if (x < mid) { n       modify(u << 1, l, mid, x, v); n     } else { n       modify(u << 1 | 1, mid, r, x, v); n     } n     a[u] = a[u << 1] + a[u << 1 | 1]; n   } n   void add(int u, int l, int r, int L, int R, int v) { n     if (L <= l && r <= R) { n       node_add(u, v); n       return; n     } n     int mid = (l + r + 1) >> 1; n     down(u); n     if (L < mid) { n       add(u << 1, l, mid, L, R, v); n     } n     if (mid < R) { n       add(u << 1 | 1, mid, r, L, R, v); n     } n     a[u] = a[u << 1] + a[u << 1 | 1]; n   } n   void chmin(int u, int l, int r, int L, int R, int v) { n     if (a[u].mx <= v) { n       return; n     } n     if (L <= l && r <= R && a[u].se < v) { n       node_add_mx(u, v - a[u].mx); n       return; n     } n     int mid = (l + r + 1) >> 1; n     down(u); n     if (L < mid) { n       chmin(u << 1, l, mid, L, R, v); n     } n     if (mid < R) { n       chmin(u << 1 | 1, mid, r, L, R, v); n     } n     a[u] = a[u << 1] + a[u << 1 | 1]; n   } n   int get_cnt(int u, int l, int r, int L, int R) { n     if (L <= l && r <= R) { n       return a[u].cnt; n     } n     int mid = (l + r + 1) >> 1; n     down(u); n     if (R <= mid) { n       return get_cnt(u << 1, l, mid, L, R); n     } else if (L >= mid) { n       return get_cnt(u << 1 | 1, mid, r, L, R); n     } else { n       return get_cnt(u << 1, l, mid, L, R) + get_cnt(u << 1 | 1, mid, r, L, R); n     } n   } n  n  public: n   segment_tree(int _n) n       : n(_n), n         a(enlarge(n) << 1), n         tag(enlarge(n) << 1), n         mxtag(enlarge(n) << 1) {} n   void modify(int x, int v) { modify(1, 0, n, x, v); } n   void add(int l, int r, int v) { n     if (l >= r) { n       return; n     } n     add(1, 0, n, l, r, v); n   } n   void chmin(int l, int r, int v) { n     if (l >= r) { n       return; n     } n     chmin(1, 0, n, l, r, v); n   } n   int get_cnt(int l, int r) { n     if (l >= r) { n       return 0; n     } n     return get_cnt(1, 0, n, l, r); n   } n   long long get_sum() { return a[1].sum; } n }; n int main() { n   std::ios_base::sync_with_stdio(false); n   std::cin.tie(0); n   int n; n   std::cin >> n; n   std::vector<int> p(n); n   for (int i = 0; i < n; ++i) { n     int v; n     std::cin >> v; n     --v; n     p[v] = i; n   } n   std::vector<long long> ans(n); n   for (int _ = 0; _ < 2; ++_) { n     segment_tree T(n); n     for (int i = 0; i < n; ++i) { n       T.modify(p[i], i + 1); n       T.chmin(0, p[i], T.get_cnt(0, p[i])); n       T.add(p[i] + 1, n, 1); n       ans[i] += T.get_sum() - 1ll * i * (i + 1) / 2; n     } n     for (int i = 0; i < n; ++i) { n       p[i] = n - 1 - p[i]; n     } n   } n   for (int i = 0; i < n; ++i) { n     std::cout << ans[i] - i - 1 <<   n ; n   } n } n


#include <bits/stdc++.h> n const int INF = 1e9 + 7; n struct Node { n   int min, max; n   int low, high; n   int lcnt, rcnt, cnt; n } st[2000000]; n int as[500005]; n int query(int w, int L, int R, int& low, int& high) { n   if (R - L == 1) { n     return as[L] >= low && as[L] <= high; n   } n   int M = (L + R) / 2; n   if (low > st[w].max) return 0; n   if (high < st[w].min) return 0; n   if (low <= st[w].min && high >= st[w].max) return st[w].cnt; n   if (low > st[w].high) { n     return query(w * 2 + 1, L, M, low, high); n   } else if (high < st[w].low) { n     return query(w * 2 + 2, M, R, low, high); n   } else { n     high = std::max(high, st[w].high); n     low = std::min(low, st[w].low); n     int res = 0; n     int ign1, ign2; n     if (high > st[w].high) n       res += query(w * 2 + 1, L, M, ign1 = st[w].high + 1, high); n     if (low < st[w].low) n       res += query(w * 2 + 2, M, R, low, ign2 = st[w].low - 1); n     if (low <= st[w].low && high >= st[w].high && st[w].low <= st[w].high) n       res++; n     return res; n   } n } n void pull(int w, int L, int R) { n   int ign1, ign2; n   assert(R - L > 1); n   int M = (L + R) / 2; n   st[w].min = std::min(st[w * 2 + 1].min, st[w * 2 + 2].min); n   st[w].max = std::max(st[w * 2 + 1].max, st[w * 2 + 2].max); n   st[w].high = st[w * 2 + 2].max; n   st[w].low = st[w * 2 + 1].min; n   query(w * 2 + 1, L, M, st[w].low, st[w].high); n   query(w * 2 + 2, M, R, st[w].low, st[w].high); n   if (st[w].low <= st[w].high) { n     st[w].lcnt = query(w * 2 + 1, L, M, ign1 = st[w].high + 1, ign2 = INF); n     st[w].rcnt = query(w * 2 + 2, M, R, ign1 = -INF, ign2 = st[w].low - 1); n     st[w].cnt = st[w].lcnt + st[w].rcnt + 1; n   } else { n     st[w].lcnt = st[w * 2 + 1].cnt; n     st[w].rcnt = st[w * 2 + 2].cnt; n     st[w].cnt = st[w].lcnt + st[w].rcnt; n   } n } n void build(int w, int L, int R, int a, int b) { n   if (a >= R || b <= L) return; n   if (R - L == 1) { n     st[w].min = st[w].max = as[L]; n     st[w].cnt = 1; n   } else { n     int M = (L + R) / 2; n     build(w * 2 + 1, L, M, a, b); n     build(w * 2 + 2, M, R, a, b); n     pull(w, L, R); n   } n } n void dump(int w, int L, int R) { n   int M = (L + R) / 2; n   if (R - L > 1) { n     printf( [%d,%d): min=%d max=%d low=%d high=%d lcnt=%d rcnt=%d cnt=%d n , L, n            R, st[w].min, st[w].max, st[w].low, st[w].high, st[w].lcnt, n            st[w].rcnt, st[w].cnt); n     dump(w * 2 + 1, L, M); n     dump(w * 2 + 2, M, R); n   } n } n int main() { n   int N, Q; n   scanf( %d %d , &N, &Q); n   for (int i = 0; i < N; i++) { n     scanf( %d , &as[i]); n   } n   build(0, 0, N, 0, N); n   for (int i = 0; i < Q; i++) { n     int I, X; n     scanf( %d %d , &I, &X); n     I--; n     as[I] = X; n     build(0, 0, N, I, I + 1); n     printf( %d n , st[0].cnt); n   } n } n


#include <bits/stdc++.h> n using namespace std; n const string nameFiles =   ; n void error(vector<string>::iterator) {} n template <typename T, typename... Args> n void error(vector<string>::iterator cur_var, T a, Args... args) { n   cerr << cur_var->substr((*cur_var)[0] ==    ) <<   =   << a << endl; n   error(++cur_var, args...); n } n vector<string> split(string const& str, char c) { n   vector<string> res; n   stringstream ss(str); n   string x; n   while (getline(ss, x, c)) res.emplace_back(x); n   return move(res); n } n int main() { n   if (!nameFiles.empty()) { n     freopen((nameFiles +  .in ).c_str(),  r , stdin); n     freopen((nameFiles +  .out ).c_str(),  w , stdout); n   } n   int n, m; n   cin >> n >> m; n   vector<int> a(n); n   unordered_set<int> mod; n   for (int i = 0; i < n; ++i) { n     cin >> a[i]; n     a[i] %= m; n     if (a[i] == 0) { n       cout <<  YES  << endl; n       return 0; n     } n   } n   for (int i = 0; i < n; ++i) { n     vector<int> ttt; n     for (int x : mod) { n       int temp = (x + a[i]) % m; n       if (temp == 0) { n         cout <<  YES  << endl; n         return 0; n       } n       ttt.push_back(temp); n     } n     mod.insert(ttt.begin(), ttt.end()); n     mod.insert(a[i]); n   } n   cout <<  NO  << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int INF = 0x7f7f7f7f; n const int MOD = 1e9 + 7; n const double eps = 1e-6; n int main() { n   int n, r, v; n   scanf( %d%d%d , &n, &r, &v); n   double s = acos(-1.0) * r * 2; n   while (n--) { n     int a, b; n     scanf( %d%d , &a, &b); n     double d = b - a; n     int c = (d / s); n     double len = d - c * s; n     double L = 0, R = len; n     double mid = (L + R) / 2; n     while (R - L >= eps) { n       if (mid + 2 * r * sin(mid / 2 / r) >= len) n         R = mid; n       else n         L = mid; n       mid = (L + R) / 2; n     } n     printf( %.12lf n , (c * s + mid) / v); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T> n T gcd(T a, T b) { n   return !b ? a : gcd(b, a % b); n } n template <typename T> n T lcm(T a, T b) { n   return a * (b / gcd(a, b)); n } n template <typename T> n T sqr(T a) { n   return a * a; n } n template <typename T> n T cube(T a) { n   return a * a * a; n } n template <typename T> n inline void smin(T &a, T b) { n   a = a < b ? a : b; n } n template <typename T> n inline void smax(T &a, T b) { n   a = a > b ? a : b; n } n int in() { n   int n; n   scanf( %d , &n); n   return n; n } n long long Lin() { n   long long n; n   scanf( %lld , &n); n   return n; n } n double Din() { n   double n; n   scanf( %lf , &n); n   return n; n } n const long long inf = (long long)1e17; n const long long mod = (long long)1e9 + 7; n const int N = 1e5 + 5; n struct CHT_DEC_MIN { n   vector<long long> m, b; n   bool bad(int f1, int f2, int f3) { n     return 1.0 * (b[f2] - b[f1]) * (m[f1] - m[f3]) >= n            1.0 * (b[f3] - b[f1]) * (m[f1] - m[f2]); n   } n   void add(long long M, long long B) { n     m.push_back(M), b.push_back(B); n     int sz = (int)m.size(); n     while (sz >= 3 && bad(sz - 3, sz - 2, sz - 1)) { n       m.erase(m.end() - 2); n       b.erase(b.end() - 2); n       sz--; n     } n   } n   long long f(int idx, long long X) { return m[idx] * X + b[idx]; } n   long long query(long long X) { n     int low = 0, high = (int)m.size() - 1; n     while (high - low > 5) { n       int mid1 = (low + low + high) / 3; n       int mid2 = (low + high + high) / 3; n       if (f(mid1, X) >= f(mid2, X)) { n         low = mid1 + 1; n       } else { n         high = mid2 - 1; n       } n     } n     long long res = f(low, X); n     for (int i = low + 1; i <= high; i++) { n       res = min(res, f(i, X)); n     } n     return res; n   } n }; n long long a[N], s[N]; n CHT_DEC_MIN tree[4 * N]; n void update(int pos, int left, int right, int x, long long M, long long B) { n   tree[pos].add(M, B); n   if (left == right) return; n   int mid = (left + right) >> 1; n   if (x <= mid) n     update(pos * 2, left, mid, x, M, B); n   else n     update(pos * 2 + 1, mid + 1, right, x, M, B); n } n long long query(int pos, int left, int right, int x, int y, long long X) { n   if (left > y || right < x) return (long long)1e17; n   if (left >= x && right <= y) { n     return tree[pos].query(X); n   } n   int mid = (left + right) >> 1; n   long long m = query(pos * 2, left, mid, x, y, X); n   long long n = query(pos * 2 + 1, mid + 1, right, x, y, X); n   return min(m, n); n } n int solve() { n   int n = in(); n   vector<pair<long long, int> > tem; n   for (int i = 1; i <= n; i++) { n     a[i] = Lin(); n     s[i] += s[i - 1] + a[i]; n     tem.push_back({a[i], i}); n   } n   sort(tem.begin(), tem.end()); n   for (int i = 0; i < n; i++) { n     int idx = tem[i].second; n     long long M = -a[idx], B = a[idx] * idx - s[idx]; n     update(1, 1, n, idx, M, B); n   } n   int q = in(); n   while (q--) { n     int x = in(), y = in(); n     x = y - x + 1; n     printf( %lld n , s[y] + query(1, 1, n, x, y, (long long)x - 1)); n   } n   return 0; n } n int main() { n   int test = 1, tc = 0; n   while (test--) { n     solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T> n void write(ostream& out, T t) { n   out << t <<   n ; n } n template <typename T, typename... Args> n void write(ostream& out, T t, Args... args) { n   out << t <<    ; n   write(out, args...); n } n template <typename... Args> n void debug(Args... args) {} n template <typename T> n inline T abs(T t) { n   return t < 0 ? -t : t; n } n const long long modn = 1000000007; n const long long modn2 = 10000019; n inline long long mod(long long x) { return x % modn; } n const int MAXK = 1003, MAXQ = 2 * 30004 + 10010; n const int MAXN = MAXQ; n const long long INF = 9000000000LL; n int n, m, k; n vector<pair<long long, int> > s; n long long dp[15009][MAXK]; n void prep_cin() { n   ios::sync_with_stdio(false); n   cin.tie(0); n } n struct qry { n   int t, id; n }; n vector<qry> q; n int L[MAXN], R[MAXN]; n int acu[MAXN]; n long long pot[MAXK]; n int h; n inline long long f(int h) { n   long long res = 0; n   long long mx = 0; n   for (int a = 1; a <= k; a++) { n     mx = mod(max(mx, dp[h][a])); n     res = mod(res + mod(mx * pot[a - 1])); n   } n   return res; n } n inline bool sup(int i, int j, int l, int r) { return (i < l && j > r); } n inline void add(int id) { n   h++; n   int pes = s[id].second; n   long long val = s[id].first; n   for (int a = 0; a <= k; a++) { n     dp[h][a] = dp[h - 1][a]; n     if (a >= pes && dp[h - 1][a - pes] != -INF) n       dp[h][a] = max(dp[h][a], dp[h - 1][a - pes] + val); n   } n } n void go(int i, int j) { n   if (i == j) { n     if (q[i].t == 3) printf( %lld n , f(h)); n     return; n   } n   int m = (i + j) / 2; n   if (acu[m] - (i == 0 ? 0 : acu[i - 1]) != 0) { n     int old = h; n     for (int a = m + 1; a <= j; a++) { n       if (q[a].t == 1 || q[a].t == 3) continue; n       int id = q[a].id; n       if (sup(L[id], R[id], i, m)) add(id); n     } n     go(i, m); n     h = old; n   } n   if (acu[j] - acu[m] != 0) { n     int old = h; n     for (int a = i; a <= m; a++) { n       if (q[a].t == 2 || q[a].t == 3) continue; n       int id = q[a].id; n       if (sup(L[id], R[id], m + 1, j)) add(id); n     } n     go(m + 1, j); n     h = old; n   } n } n int main() { n   scanf( %d%d , &n, &k); n   pot[0] = 1; n   for (int a = 1; a < k; a++) pot[a] = mod(pot[a - 1] * modn2); n   const int inf = 1e7; n   for (int a = 0; a < n; a++) { n     pair<long long, int> x; n     scanf( %lld %d , &x.first, &x.second); n     s.push_back(x); n     q.push_back({1, a}); n     L[a] = a; n     R[a] = inf; n   } n   scanf( %d , &m); n   for (int a = 0; a < m; a++) { n     qry p; n     scanf( %d , &p.t); n     if (q.size() > 0) acu[q.size()] = acu[q.size() - 1]; n     if (p.t == 1) { n       pair<long long, int> x; n       scanf( %lld %d , &x.first, &x.second); n       s.push_back(x); n       p.id = (int)s.size() - 1; n       L[p.id] = q.size(); n       R[p.id] = inf; n     } else if (p.t == 2) { n       scanf( %d , &p.id); n       p.id--; n       R[p.id] = q.size(); n     } else n       acu[q.size()]++; n     q.push_back(p); n   } n   n = s.size(); n   m = q.size(); n   for (int a = 0; a <= k; a++) dp[0][a] = -INF; n   dp[0][0] = 0; n   go(0, m - 1); n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   long long t, n, k, p; n   long long a[50]; n   cin >> t; n   while (t--) { n     cin >> n >> k; n     for (int i = 0; i < n; i++) cin >> a[i]; n     long long limit = 10e16; n     p = 1; n     while (p < limit) { n       p *= k; n     } n     if (p > limit) { n       p /= k; n     } n     while (p != 0) { n       for (int i = 0; i < n; i++) { n         if (a[i] >= p) { n           a[i] -= p; n           break; n         } n       } n       p /= k; n     } n     bool checker = true; n     for (int i = 0; i < n; i++) { n       if (a[i] != 0) { n         checker = false; n         break; n       } n     } n     if (checker) { n       cout <<  YES n ; n     } else { n       cout <<  NO n ; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, a, b, ans = 0; n   cin >> n >> a >> b; n   vector<int> vec(n); n   for (int i = 0; i < n; i++) cin >> vec[i]; n   for (int i = 0; i < n / 2 + (n % 2); i++) { n     if (vec[i] != vec[n - i - 1] && vec[i] != 2 && vec[n - i - 1] != 2) { n       cout << -1; n       return 0; n     } n     if (vec[i] == vec[n - i - 1] && vec[i] == 2 && i != n - i - 1) { n       ans += 2 * (min(a, b)); n     } else if (vec[i] == vec[n - i - 1] && vec[i] == 2 && i == n - i - 1) { n       ans += min(a, b); n     } n     if (vec[i] == 1 && vec[n - i - 1] == 2 || n         vec[n - i - 1] == 1 && vec[i] == 2) { n       ans += b; n     } n     if (vec[i] == 0 && vec[n - i - 1] == 2 || n         vec[n - i - 1] == 0 && vec[i] == 2) { n       ans += a; n     } n   } n   cout << ans; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAX_N = 100005, MAX_K = 105; n int N, M, K, S; n int good_type[MAX_N]; n int distances[MAX_N][MAX_K]; n vector<int> adj[MAX_N]; n int dist[MAX_N]; n void RunBFS(int type) { n   queue<int> q; n   for (int i = 0; i < N; ++i) { n     if (good_type[i] == type) { n       distances[i][type] = 0; n       q.push(i); n     } n   } n   while (!q.empty()) { n     int u = q.front(); n     q.pop(); n     for (const int& v : adj[u]) { n       if (distances[v][type] == -1) { n         distances[v][type] = distances[u][type] + 1; n         q.push(v); n       } n     } n   } n } n int main() { n   scanf( %d %d %d %d , &N, &M, &K, &S); n   for (int i = 0; i < N; ++i) { n     scanf( %d , &good_type[i]); n     --good_type[i]; n   } n   for (int i = 0; i < M; ++i) { n     int u, v; n     scanf( %d %d , &u, &v); n     --u; n     --v; n     adj[u].push_back(v); n     adj[v].push_back(u); n   } n   memset(distances, -1, sizeof distances); n   for (int i = 0; i < K; ++i) { n     RunBFS(i); n   } n   for (int i = 0; i < N; ++i) { n     sort(distances[i], distances[i] + K); n     long long sum = 0; n     for (int j = 0; j < S; ++j) sum += distances[i][j]; n     printf( %lld  , sum); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long N = 1e6 + 7; n const long long mod = 1e9 + 7; n long long gcd(long long a, long long b) { n   if (b == 0) return a; n   return gcd(b, a % b); n } n map<long long, long long> mp; n int32_t main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   long long i = 1; n   vector<long long> v; n   long long n, a, b, m; n   cin >> m >> a >> b; n   long long k = gcd(a, b); n   long long z = m / k; n   long long val1 = m * z - (k * z * (z + 1) / 2) + z + m + 1; n   long long val2 = 0; n   long long start = 0; n   long long cnt = 0; n   long long ans = 0; n   long long currmx = 0; n   while (start <= (a + b)) { n     if (mp[start]) break; n     mp[start] = 1; n     currmx = max(currmx, start); n     if (start <= m) { n       ans += min(currmx - start, m - start + 1); n     } n     if (start >= b) n       start -= b; n     else n       start += a; n   } n   cout << val1 - ans <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long linf = 1LL << 62; n const int iinf = 1000000009; n const double dinf = 1e17; n const int Mod = 1e9 + 9; n const int maxn = 1000005; n int n; n long long a, b[maxn]; n long long dp[3][maxn]; n void solve() { n   ios::sync_with_stdio(false); n   cin >> n; n   for (int i = 1; i <= n; i++) cin >> a, b[i] = __builtin_popcountll(a); n   long long suf = 0; n   dp[0][n + 1] = 1; n   long long res = 0; n   for (int i = n; i >= 1; i--) { n     long long sum = 0, mx = 0, ret = 0; n     for (int j = i; j <= min(n, i + 64); j++) { n       sum += b[j]; n       mx = max(b[j], mx); n       if (mx > sum - mx && sum % 2 == 0) ret--; n     } n     suf += b[i]; n     if (suf & 1) n       ret += dp[1][i + 1]; n     else n       ret += dp[0][i + 1]; n     res += ret; n     dp[0][i] = dp[0][i + 1]; n     dp[1][i] = dp[1][i + 1]; n     if (suf & 1) n       dp[1][i]++; n     else n       dp[0][i]++; n   } n   cout << res << endl; n } n int main() { n   int t = 1; n   for (int i = 1; i <= t; i++) solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m, q; n int x[1000][1000]; n int r[1000][1000][4]; n void upd1(int j) { n   int i = 0; n   for (int k = 0; k < n; ++k) { n     if (x[k][j] == 0) { n       i = k + 1; n       r[k][j][0] = -1; n     } else n       r[k][j][0] = i; n   } n   i = n - 1; n   for (int k = n - 1; k > -1; --k) { n     if (x[k][j] == 0) { n       i = k - 1; n       r[k][j][1] = -1; n     } else n       r[k][j][1] = i; n   } n } n void upd2(int i) { n   int j = 0; n   for (int k = 0; k < m; ++k) { n     if (x[i][k] == 0) { n       j = k + 1; n       r[i][k][2] = -1; n     } else n       r[i][k][2] = j; n   } n   j = m - 1; n   for (int k = m - 1; k > -1; --k) { n     if (x[i][k] == 0) { n       j = k - 1; n       r[i][k][3] = -1; n     } else n       r[i][k][3] = j; n   } n } n int main() { n   cin.sync_with_stdio(false); n   cin >> n >> m >> q; n   for (int i = 0; i < n; ++i) n     for (int j = 0; j < m; ++j) cin >> x[i][j]; n   for (int j = 0; j < m; ++j) upd1(j); n   for (int i = 0; i < n; ++i) upd2(i); n   for (int k = 0; k < q; ++k) { n     int op; n     int i, j; n     cin >> op; n     cin >> i >> j; n     i--; n     j--; n     if (op == 1) { n       x[i][j] = 1 - x[i][j]; n       upd1(j); n       upd2(i); n     } else { n       int res = 0; n       if (x[i][j] != 0) { n         int up = 0; n         int down = n - 1; n         for (int k = j; k <= r[i][j][3]; ++k) { n           up = max(up, r[i][k][0]); n           down = min(down, r[i][k][1]); n           res = max(res, (down - up + 1) * (k - j + 1)); n         } n         up = 0; n         down = n - 1; n         for (int k = j; k >= r[i][j][2]; --k) { n           up = max(up, r[i][k][0]); n           down = min(down, r[i][k][1]); n           res = max(res, (down - up + 1) * (j - k + 1)); n         } n         int left = 0; n         int right = m - 1; n         for (int k = i; k <= r[i][j][1]; ++k) { n           left = max(left, r[k][j][2]); n           right = min(right, r[k][j][3]); n           res = max(res, (right - left + 1) * (k - i + 1)); n         } n         left = 0; n         right = m - 1; n         for (int k = i; k >= r[i][j][0]; --k) { n           left = max(left, r[k][j][2]); n           right = min(right, r[k][j][3]); n           res = max(res, (right - left + 1) * (i - k + 1)); n         } n       } n       cout << res << endl; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int max_n = 511, inf = 1e9 + 100; n bool m[max_n][max_n]; n bool vis[max_n][max_n]; n int mini[max_n], maxi[max_n]; n int minj[max_n], maxj[max_n]; n int si, sj; n int n; n int di[] = {0, 0, 1, -1}; n int dj[] = {-1, 1, 0, 0}; n char dir[] =  LRDU ; n string result; n bool on_field(int i, int j) { return i >= 0 && j >= 0 && i < n && j < n; } n void upd_mx(int i, int j) { n   mini[j] = min(mini[j], i); n   maxi[j] = max(maxi[j], i); n   minj[i] = min(minj[i], j); n   maxj[i] = max(maxj[i], j); n } n void dfs(int i, int j) { n   if (vis[i][j]) { n     return; n   } n   vis[i][j] = 1; n   if (m[i][j] == 0) { n     result +=  1 ; n     m[i][j] = 1; n     upd_mx(i, j); n   } n   if (on_field(i - 1, j) && !vis[i - 1][j] && mini[j] < i) { n     result +=  U ; n     dfs(i - 1, j); n     result +=  D ; n   } n   if (on_field(i + 1, j) && !vis[i + 1][j] && maxi[j] > i) { n     result +=  D ; n     dfs(i + 1, j); n     result +=  U ; n   } n   if (on_field(i, j - 1) && !vis[i][j - 1] && minj[i] < j) { n     result +=  L ; n     dfs(i, j - 1); n     result +=  R ; n   } n   if (on_field(i, j + 1) && !vis[i][j + 1] && maxj[i] > j) { n     result +=  R ; n     dfs(i, j + 1); n     result +=  L ; n   } n } n int main() { n   for (int i = 0; i < max_n; ++i) { n     minj[i] = mini[i] = max_n; n     maxj[i] = maxi[i] = -1; n   } n   cin >> n >> si >> sj; n   --si, --sj; n   for (int i = 0; i < n; ++i) { n     for (int j = 0; j < n; ++j) { n       cin >> m[i][j]; n       if (m[i][j] == 1) { n         mini[j] = min(mini[j], i); n         maxi[j] = max(maxi[j], i); n         minj[i] = min(minj[i], j); n         maxj[i] = max(maxj[i], j); n       } n     } n   } n   dfs(si, sj); n   for (int i = 0; i < n; ++i) { n     for (int j = 0; j < n; ++j) { n       if (m[i][j] && !vis[i][j]) { n         cout <<  NO  << endl; n         return 0; n       } n     } n   } n   int fi = si, fj = sj; n   while (si > 0) { n     int upj = -1; n     for (int j = 0; j < n; ++j) { n       if (m[si][j] && m[si - 1][j]) { n         upj = j; n         break; n       } n     } n     if (upj == -1) { n       break; n     } n     while (sj < upj) { n       result +=  R ; n       ++sj; n     } n     while (sj > upj) { n       result +=  L ; n       --sj; n     } n     result +=  U ; n     --si; n   } n   while (si < fi) { n     int dj = -1; n     for (int j = 0; j < n; ++j) { n       if (m[si][j] && m[si + 1][j]) { n         dj = j; n         break; n       } n     } n     if (dj == -1) { n       break; n     } n     while (sj < dj) { n       result +=  R ; n       ++sj; n     } n     while (sj > dj) { n       result +=  L ; n       --sj; n     } n     while (sj > 0 && m[si][sj - 1]) { n       result +=  L ; n       --sj; n     } n     while (sj < dj) { n       result +=  2 ; n       result +=  R ; n       ++sj; n     } n     while (sj + 1 < n && m[si][sj + 1]) { n       result +=  R ; n       ++sj; n     } n     while (sj > dj) { n       result +=  2 ; n       result +=  L ; n       --sj; n     } n     result +=  2 ; n     result +=  D ; n     ++si; n   } n   while (si + 1 < n) { n     int dj = -1; n     for (int j = 0; j < n; ++j) { n       if (m[si][j] && m[si + 1][j]) { n         dj = j; n         break; n       } n     } n     if (dj == -1) { n       break; n     } n     while (sj < dj) { n       result +=  R ; n       ++sj; n     } n     while (sj > dj) { n       result +=  L ; n       --sj; n     } n     result +=  D ; n     ++si; n   } n   while (si > fi) { n     int upj = -1; n     for (int j = 0; j < n; ++j) { n       if (m[si][j] && m[si - 1][j]) { n         upj = j; n         break; n       } n     } n     if (upj == -1) { n       break; n     } n     while (sj < upj) { n       result +=  R ; n       ++sj; n     } n     while (sj > upj) { n       result +=  L ; n       --sj; n     } n     while (sj > 0 && m[si][sj - 1]) { n       result +=  L ; n       --sj; n     } n     while (sj < upj) { n       result +=  2 ; n       result +=  R ; n       ++sj; n     } n     while (sj + 1 < n && m[si][sj + 1]) { n       result +=  R ; n       ++sj; n     } n     while (sj > upj) { n       result +=  2 ; n       result +=  L ; n       --sj; n     } n     result +=  2 ; n     result +=  U ; n     --si; n   } n   while (sj < fj) { n     result +=  R ; n     ++sj; n   } n   while (sj > fj) { n     result +=  L ; n     --sj; n   } n   while (sj > 0 && m[si][sj - 1]) { n     result +=  L ; n     --sj; n   } n   while (sj < fj) { n     result +=  2 ; n     result +=  R ; n     ++sj; n   } n   while (sj + 1 < n && m[si][sj + 1]) { n     result +=  R ; n     ++sj; n   } n   while (sj > fj) { n     result +=  2 ; n     result +=  L ; n     --sj; n   } n   result +=  2 ; n   if (result.size() > 2e6) { n     exit(228); n   } n   cout <<  YES  << endl; n   cout << result << endl; n   return 0; n } n


#include <bits/stdc++.h> n #pragma GCC optimize( O3 ) n #pragma GCC optimize( unroll-loops ) n #pragma comment(linker,  /STACK:2000000 ) n using namespace std; n template <int D, typename T> n struct Vec : public vector<Vec<D - 1, T>> { n   static_assert(D >= 1,  Vector dimension must be greater than zero! ); n   template <typename... Args> n   Vec(int n = 0, Args... args) n       : vector<Vec<D - 1, T>>(n, Vec<D - 1, T>(args...)) {} n }; n template <typename T> n struct Vec<1, T> : public vector<T> { n   Vec(int n = 0, T val = T()) : vector<T>(n, val) {} n }; n using ll = long long; n using db = long double; n using ii = pair<int, int>; n const int N = 2e5 + 5, LG = 19, MOD = 998244353; n const int SQ = 320; n const long double EPS = 1e-7; n void printCase(int cs) { cout <<  Case #  << cs <<  :  ; } n int p[N], n, q; n int fast(int b, int e) { n   int res = 1; n   for (; e; e >>= 1, b = 1ll * b * b % MOD) n     if (e & 1) { n       res = 1ll * res * b % MOD; n     } n   return res; n } n int t[N << 2], lz[N << 2], cur = 1; n void build(int node, int s, int e) { n   lz[node] = 1; n   if (s == e) { n     t[node] = cur; n     cur = 1ll * cur * p[s] % MOD; n     return; n   } n   int md = (s + e) >> 1; n   build(node << 1, s, md); n   build(node << 1 | 1, md + 1, e); n   t[node] = t[node << 1] + t[node << 1 | 1]; n   if (t[node] >= MOD) t[node] -= MOD; n } n void push(int node, int s, int e) { n   if (lz[node] == 1) return; n   t[node] = 1ll * t[node] * lz[node] % MOD; n   if (s != e) { n     lz[node << 1] = 1ll * lz[node] * lz[node << 1] % MOD; n     lz[node << 1 | 1] = 1ll * lz[node] * lz[node << 1 | 1] % MOD; n   } n   lz[node] = 1; n } n int qry(int node, int s, int e, int l, int r) { n   push(node, s, e); n   if (r < s || e < l) return 0; n   if (l <= s && e <= r) return t[node]; n   int md = (s + e) >> 1; n   int ret = qry(node << 1, s, md, l, r) + qry(node << 1 | 1, md + 1, e, l, r); n   if (ret >= MOD) ret -= MOD; n   return ret; n } n void upd(int node, int s, int e, int l, int r, int v) { n   push(node, s, e); n   if (r < s || e < l) return; n   if (l <= s && e <= r) { n     lz[node] = 1ll * lz[node] * v % MOD; n     push(node, s, e); n     return; n   } n   int md = (s + e) >> 1; n   upd(node << 1, s, md, l, r, v); n   upd(node << 1 | 1, md + 1, e, l, r, v); n   t[node] = t[node << 1] + t[node << 1 | 1]; n   if (t[node] >= MOD) t[node] -= MOD; n } n int sum[N]; n int qry(int l, int r) { return 1ll * sum[r] * fast(sum[l - 1], MOD - 2) % MOD; } n int get(int l, int r) { n   return 1ll * qry(1, 1, n, l, r) * fast(qry(l, r), MOD - 2) % MOD; n } n int32_t main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cin >> n >> q; n   const int INV100 = fast(100, MOD - 2); n   sum[0] = 1; n   for (int i = 1; i < n + 1; i++) { n     cin >> p[i]; n     p[i] = 1ll * p[i] * INV100 % MOD; n     sum[i] = 1ll * sum[i - 1] * p[i] % MOD; n   } n   build(1, 1, n); n   set<int> checkPoints({1, n + 1}); n   int ans = get(1, n); n   while (q--) { n     int x; n     cin >> x; n     if (checkPoints.count(x)) { n       int l = *(--checkPoints.find(x)); n       int r = *(checkPoints.upper_bound(x)); n       ans -= get(l, x - 1); n       if (ans < 0) ans += MOD; n       ans -= get(x, r - 1); n       if (ans < 0) ans += MOD; n       int toMul = 1ll * qry(1, 1, n, x - 1, x - 1) * p[x - 1] % MOD; n       upd(1, 1, n, x, r - 1, toMul); n       ans += get(l, r - 1); n       if (ans >= MOD) ans -= MOD; n       checkPoints.erase(x); n     } else { n       int l = *(--checkPoints.upper_bound(x)); n       int r = *(checkPoints.upper_bound(x)); n       ans = (ans - get(l, r - 1)); n       if (ans < 0) ans += MOD; n       ans += get(l, x - 1); n       if (ans >= MOD) ans -= MOD; n       int toDivide = 1ll * qry(1, 1, n, x - 1, x - 1) * p[x - 1] % MOD; n       toDivide = fast(toDivide, MOD - 2); n       upd(1, 1, n, x, r - 1, toDivide); n       ans += get(x, r - 1); n       if (ans >= MOD) ans -= MOD; n       checkPoints.insert(x); n     } n     cout << ans <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long b, c, m, n, k, l; n   cin >> m >> k; n   l = k; n   for (int i = 0; i < k; i++) { n     if (m % 10 == 0) n       m /= 10; n     else n       m--; n   } n   cout << m; n } n


#include <bits/stdc++.h> n int main() { n   int t; n   scanf( %d , &t); n   while (t--) { n     int n; n     scanf( %d , &n); n     int a[n]; n     for (int i = 0; i < n; i++) { n       scanf( %d , &a[i]); n     } n     for (int i = 0; i < n; i++) { n       for (int j = 0; j < n - i - 1; j++) { n         if (a[j + 1] < a[j]) { n           int t = a[j]; n           a[j] = a[j + 1]; n           a[j + 1] = t; n         } n       } n     } n     int ans = INT_MAX; n     for (int i = 0; i < n - 1; i++) { n       if (a[i + 1] - a[i] < ans) { n         ans = a[i + 1] - a[i]; n         if (ans == 0) break; n       } n     } n     printf( %d n , ans); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXD = 500; n const int MAXS = 5000; n bool vis[MAXD][MAXS + 1]; n int dig[MAXD][MAXS + 1], from[MAXD][MAXS + 1]; n struct Data { n   int last, c, prev_r; n   int r, sum; n   bool operator<(const Data &other) const { return last < other.last; } n }; n queue<Data> mst; n int main() { n   int d, s; n   ios::sync_with_stdio(false); n   cin >> d >> s; n   mst.push({0, 0, 0, 0, 0}); n   int last = 0; n   while (!mst.empty()) { n     auto it = mst.front(); n     int r = it.r, sum = it.sum; n     int prev_r = it.prev_r, c = it.c; n     mst.pop(); n     if (vis[r][sum]) { n       continue; n     } n     vis[r][sum] = 1; n     dig[r][sum] = c; n     from[r][sum] = prev_r; n     for (c = 0; c < 10; c++) { n       int cur = (r * 10 + c) % d; n       if (sum + c <= s && vis[cur][sum + c] == 0) { n         mst.push({++last, c, r, cur, sum + c}); n       } n     } n   } n   if (vis[0][s]) { n     vector<int> sol; n     int r = 0; n     while (s > 0) { n       int cur = dig[r][s]; n       sol.push_back(cur); n       r = from[r][s]; n       s -= cur; n     } n     reverse(sol.begin(), sol.end()); n     for (auto it : sol) { n       cout << it; n     } n     return 0; n   } n   cout << -1; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n signed main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   long long n; n   cin >> n; n   if (n == 2) { n     cout << 2; n   } else { n     cout << 1; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int mod = 998244353; n const int M = 2e5 + 10; n const int N = 5e2 + 10; n inline long long read() { n   long long b = 1, sum = 0; n   char c = getchar(); n   while (!isdigit(c)) { n     if (c ==  - ) b = -1; n     c = getchar(); n   } n   while (isdigit(c)) { n     sum = sum * 10 + c -  0 ; n     c = getchar(); n   } n   return b * sum; n } n int hs, gs; n int n, m, k, T; n int h, g, a[1 << 22]; n long long sum; n vector<int> ans; n void change(int x) { n   int l = 2 * x, r = 2 * x + 1; n   if (a[l] == 0 && a[r] == 0) { n     a[x] = 0; n   } else { n     if (a[l] > a[r]) { n       a[x] = a[l]; n       change(l); n     } else { n       a[x] = a[r]; n       change(r); n     } n   } n } n int pull(int x) { n   int l = 2 * x, r = 2 * x + 1; n   if (a[l] == 0 && a[r] == 0) { n     return x; n   } else { n     if (a[l] > a[r]) { n       return pull(l); n     } else { n       return pull(r); n     } n   } n } n void solv(int x) { n   if (a[x] == 0) return; n   int l = 2 * x, r = 2 * x + 1; n   while (pull(x) >= gs) { n     ans.push_back(x); n     sum -= a[x]; n     change(x); n   } n   solv(l); n   solv(r); n } n int main() { n   int i, j; n   T = read(); n   while (T--) { n     sum = 0; n     h = read(), g = read(); n     hs = (1 << h); n     gs = (1 << g); n     n = hs - gs; n     for (i = 1; i <= hs - 1; i++) { n       a[i] = read(); n       a[i + n] = a[i + 2 * n] = 0; n       sum += a[i]; n     } n     ans.clear(); n     solv(1); n     printf( %lld n , sum); n     for (i = 0; i < ans.size(); i++) { n       printf( %d  , ans[i]); n     } n     printf(  n ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n; n int v[1100]; n int pd[1100][1 << 8]; n int qtd1, qtd2; n int vis[1100][1 << 8]; n int inv[1100]; n vector<int> pos[10]; n bool check(int mask) { n   for (int i = 0; i < 8; ++i) { n     if (mask & (1 << i)) continue; n     if (qtd1) return false; n   } n   return true; n } n int f(int x, int mask) { n   if (x >= n) { n     if (check(mask)) n       return 0; n     else n       return -0x3F3F3F3F; n   } n   int &p = pd[x][mask]; n   if (vis[x][mask] < qtd2) { n     vis[x][mask] = qtd2; n     p = f(x + 1, mask); n     if (!(mask & (1 << v[x]))) { n       int fim, qtd; n       qtd = pos[v[x]].size() - inv[x]; n       if (qtd1 && qtd >= qtd1) { n         fim = pos[v[x]][inv[x] + qtd1 - 1] + 1; n         p = max(p, f(fim, mask | (1 << v[x])) + qtd1); n       } n       qtd = pos[v[x]].size() - inv[x]; n       if (qtd >= qtd2) { n         fim = pos[v[x]][inv[x] + qtd2 - 1] + 1; n         p = max(p, f(fim, mask | (1 << v[x])) + qtd2); n       } n     } n   } n   return p; n } n int main() { n   int ans = 1; n   scanf( %d , &n); n   for (int i = 0; i < n; ++i) { n     scanf( %d , &v[i]); n     v[i]--; n     inv[i] = (int)pos[v[i]].size(); n     pos[v[i]].push_back(i); n   } n   for (int i = 0; i < n; ++i) { n     qtd1 = i, qtd2 = i + 1; n     ans = max(ans, f(0, 0)); n   } n   printf( %d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const double pi = acos(-1.0); n const double eps = 1e-11; n const int inf = 0x7FFFFFFF; n template <class T> n inline void checkmin(T &a, T b) { n   if (b < a) a = b; n } n template <class T> n inline void checkmax(T &a, T b) { n   if (b > a) a = b; n } n template <class T> n inline T sqr(T x) { n   return x * x; n } n template <class T> n void show(T a, int n) { n   for (int i = 0; i < n; ++i) cout << a[i] <<    ; n   cout << endl; n } n template <class T> n void show(T a, int r, int l) { n   for (int i = 0; i < r; ++i) show(a[i], l); n   cout << endl; n } n struct S { n   long long w, h; n   long long bord, space; n   string tp; n   vector<int> son; n   int vis; n   void init(int x, int y, string t) { n     w = x, h = y; n     bord = space = 0; n     tp = t; n     son.clear(); n     vis = false; n   } n   void dfs(); n } s[100000]; n char buf[10000]; n void S::dfs() { n   vis = true; n   if (son.size() > 0) { n     for (int i = 0; i < son.size(); i++) n       if (!s[son[i]].vis) { n         s[son[i]].dfs(); n       } n     if (tp ==  VBox ) { n       for (int i = 0; i < son.size(); i++) { n         int v = son[i]; n         w = max(s[v].w, w); n         if (i > 0) h += space; n         h += s[v].h; n       } n       w += 2 * bord; n       h += 2 * bord; n     } else if (tp ==  HBox ) { n       for (int i = 0; i < son.size(); i++) { n         int v = son[i]; n         h = max(s[v].h, h); n         if (i > 0) w += space; n         w += s[v].w; n       } n       w += 2 * bord; n       h += bord * 2; n     } n   } n } n string nn[10000]; n struct SS { n   string s; n   long long x, y; n   const bool operator<(const SS &b) const { return s < b.s; } n } ss[10000]; n int main() { n   int n; n   while (cin >> n) { n     map<string, int> mp; n     int cnt = 0; n     for (int i = 0; i < n; i++) { n       scanf(  %[^ n] , buf); n       bool newtp = true; n       for (int i = 0; buf[i]; i++) n         if (buf[i] ==  (  || buf[i] ==  )  || buf[i] ==  .  || buf[i] ==  , ) { n           if (buf[i] ==  . ) newtp = false; n           buf[i] =    ; n         } n       istringstream sin(buf); n       string t, nm, nm2; n       sin >> t; n       if (t ==  Widget  || t ==  VBox  || t ==  HBox ) { n         int x = 0, y = 0; n         sin >> nm; n         mp[nm] = cnt; n         nn[cnt] = nm; n         if (t ==  Widget ) { n           sin >> x >> y; n         } n         s[cnt].init(x, y, t); n         cnt++; n       } else { n         nm = t; n         int x; n         sin >> t; n         if (t ==  set_border ) { n           sin >> x; n           s[mp[nm]].bord = x; n         } else if (t ==  set_spacing ) { n           sin >> x; n           s[mp[nm]].space = x; n         } else if (t ==  pack ) { n           sin >> nm2; n           s[mp[nm]].son.push_back(mp[nm2]); n         } else n           puts( fuck ); n       } n     } n     for (int i = 0; i < cnt; i++) { n       if (!s[i].vis) s[i].dfs(); n     } n     for (int i = 0; i < cnt; i++) { n       ss[i].s = nn[i]; n       ss[i].x = s[i].w; n       ss[i].y = s[i].h; n     } n     sort(ss, ss + cnt); n     for (int i = 0; i < cnt; i++) { n       cout << ss[i].s <<     << ss[i].x <<     << ss[i].y << endl; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int inf = 1e9 + 7; n template <class T> n void dbs(string str, T t) { n   cerr << str <<   :   << t <<   n ; n } n template <class T, class... second> n void dbs(string str, T t, second... s) { n   int idx = str.find( , ); n   cerr << str.substr(0, idx) <<   :   << t <<  ,  ; n   dbs(str.substr(idx + 1), s...); n } n template <class second, class T> n ostream& operator<<(ostream& os, const pair<second, T>& p) { n   return os <<  (  << p.first <<  ,   << p.second <<  ) ; 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 const int N = 2020; n long long first[N][N]; n long long G[N][N]; n string s; n long long g(int n, int k); n long long f(int n, int k) { n   if (k < 0) return 0; n   if (n == 0) return k == 0; n   long long& ans = first[n][k]; n   if (ans != -1) return ans; n   ans = 0; n   int ch = s[int(s.size()) - n] -  a ; n   ans = ch * f(n - 1, k) + (25 - ch) * f(n - 1, k - n) + g(n - 1, k) + (k == 0); n   int nk = k; n   for (int i = 1; i <= n - 1; ++i) { n     int ch = s[int(s.size()) - n + i] -  a ; n     nk = k - (i + 1) * (n - i); n     if (nk < 0) { n       for (int i = n - 1; i >= 1; --i) { n         int ch = s[int(s.size()) - n + i] -  a ; n         nk = k - (i + 1) * (n - i); n         if (nk < 0) break; n         ans += (25 - ch) * f(n - i - 1, nk); n         ans %= inf; n       } n       break; n     } n     ans += (25 - ch) * f(n - i - 1, nk); n     ans %= inf; n   } n   ans %= inf; n   return ans; n } n long long g(int n, int k) { n   if (k < 0) return 0; n   if (n == 0) return 0; n   long long& ans = G[n][k]; n   if (ans != -1) return ans; n   int ch = s[int(s.size()) - n] -  a ; n   ans = ch * f(n - 1, k) + g(n - 1, k); n   ans %= inf; n   return ans; n } n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   int n, k; n   cin >> n >> k; n   memset(first, -1, sizeof first); n   memset(G, -1, sizeof G); n   cin >> s; n   cout << f(n, k) <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m, g[505][505], f[505][505], tot[505], ans[505][505]; n int main() { n   scanf( %d%d , &n, &m); n   memset(g, 0x3f, sizeof(g)); n   memset(f, 0x3f, sizeof(f)); n   for (int i = 1; i <= n; i++) f[i][i] = 0; n   for (int i = 1; i <= m; i++) { n     int u, v, w; n     scanf( %d%d%d , &u, &v, &w); n     g[u][v] = g[v][u] = f[u][v] = f[v][u] = min(g[u][v], w); n   } n   for (int k = 1; k <= n; k++) n     for (int i = 1; i <= n; i++) n       for (int j = 1; j <= n; j++) n         f[i][j] = f[j][i] = min(f[i][j], f[i][k] + f[k][j]); n   for (int i = 1; i <= n; i++) { n     memset(tot, 0, sizeof(tot)); n     for (int j = 1; j <= n; j++) n       for (int k = 1; k <= n; k++) n         if (f[i][k] + g[k][j] == f[i][j]) tot[j]++; n     for (int j = 1; j <= n; j++) n       for (int k = 1; k <= n; k++) n         if (f[i][k] + f[k][j] == f[i][j]) ans[i][j] += tot[k]; n   } n   for (int i = 1; i <= n; i++) n     for (int j = i + 1; j <= n; j++) { n       if (i == j) continue; n       if (f[i][j] >= 0x3f3f3f3f - 100) ans[i][j] = 0; n       printf( %d  , ans[i][j]); n     } n   printf(  n ); n } n


#include <bits/stdc++.h> n using namespace std; n inline void read(int &a) { n   a = 0; n   int c = getchar(), b = 1; n   while (c >  9  || c <  0 ) { n     if (c ==  - ) b = -1; n     c = getchar(); n   } n   while (c >=  0  && c <=  9 ) a = (a << 3) + (a << 1) + c - 48, c = getchar(); n   a *= b; n } n inline int Rem() { n   int a = 0, c = getchar(), b = 1; n   while (c >  9  || c <  0 ) { n     if (c ==  - ) b = -1; n     c = getchar(); n   } n   while (c >=  0  && c <=  9 ) a = (a << 3) + (a << 1) + c - 48, c = getchar(); n   return a *= b; n } n inline void write(long long x) { n   if (x > 9) write(x / 10); n   putchar( 0  + x % 10); n } n inline void W(long long x) { n   if (x < 0) { n     putchar( - ), x = -x; n   } n   write(x); n } n const int N = 2e5 + 5, M = 1e6 + 5; n int n, m, a[N], pos[N], block; n long long ans, rec[N], cnt[M]; n struct node { n   int l, r, id; n } q[N]; n inline bool cmp(node a, node b) { n   return (pos[a.l] == pos[b.l]) ? (pos[a.l] & 1 ? a.r < b.r : a.r > b.r) n                                 : a.l < b.l; n } n inline void add(int x) { n   cnt[x]++; n   ans += ((cnt[x] * cnt[x]) - ((cnt[x] - 1) * (cnt[x] - 1))) * x; n } n inline void del(int x) { n   ans -= ((cnt[x] * cnt[x]) - ((cnt[x] - 1) * (cnt[x] - 1))) * x; n   cnt[x]--; n } n int main() { n   read(n); n   read(m); n   block = sqrt(n); n   for (register int i = 1; i <= n; i++) read(a[i]), pos[i] = i / block; n   for (register int i = 1; i <= m; i++) { n     read(q[i].l); n     read(q[i].r); n     q[i].id = i; n   } n   sort(q + 1, q + 1 + m, cmp); n   for (register int i = 1, ql, qr, l = 1, r = 0; i <= m; i++) { n     ql = q[i].l, qr = q[i].r; n     while (ql < l) add(a[--l]); n     while (ql > l) del(a[l++]); n     while (qr < r) del(a[r--]); n     while (qr > r) add(a[++r]); n     rec[q[i].id] = ans; n   } n   for (register int i = 1; i <= m; i++) W(rec[i]), putchar(  n ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const double eps = 1e-10, pi = 3.1415926535898; n const int mod = 998244353, maxn = 2e5 + 10; n int T, n, m, k, x, y, a[maxn], p, b; n long long fac[maxn] = {1, 1}, inv[maxn] = {1, 1}, inv_fac[maxn] = {1, 1}; n char st[maxn]; n void init() { n   for (int i = 2; i < maxn; ++i) { n     fac[i] = fac[i - 1] * i % mod; n     inv[i] = (mod - mod / i) * inv[mod % i] % mod; n     inv_fac[i] = inv_fac[i - 1] * inv[i] % mod; n   } n } n long long C(long long n, long long m) { n   if (m < 0 || n < 0 || n < m) return 0; n   return fac[n] * inv_fac[n - m] % mod * inv_fac[m] % mod; n } n int main(int argc, char const *argv[]) { n   init(); n   scanf( %d %d , &n, &m); n   if (n == 2) { n     printf( 0 n ); n     return 0; n   } n   long long ans = 0, cnt = 0; n   for (int i = n - 2; i <= m - 1; ++i) { n     cnt += C(i, n - 2); n   } n   cnt %= mod; n   for (int i = 2; i <= n - 1; ++i) { n     long long cc = cnt; n     cc = cc * C(n - 3, i - 2) % mod * (n - 2) % mod; n     ans += cc; n   } n   cout << ans % mod << endl; n   ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   string a, b, str; n   int n, cont = 0, ca = 0, cb = 0; n   cin >> a >> b; n   n = a.size(); n   for (int i = 0; i < n; i++) { n     if (a[i] != b[i]) cont++; n   } n   if (cont % 2 == 0) { n     for (int i = 0; i < n; i++) { n       if (a[i] != b[i]) { n         if (ca > cb) n           cb++, str += b[i]; n         else n           ca++, str += a[i]; n       } else n         str += a[i]; n     } n     cout << str << endl; n   } else n     printf( impossible n ); n } n


#include <bits/stdc++.h> n using namespace std; n inline void input(long long &x) { n   long long y = 1; n   char c = getchar(); n   while (c <  0  || c >  9 ) { n     if (c ==  - ) y = -1; n     c = getchar(); n   } n   x = 0; n   while (c >=  0  && c <=  9 ) { n     x = x * 10 + (c -  0 ); n     c = getchar(); n   } n   x = x * y; n } n const long long inf = 1e18; n long long f, t, temp, a1, cnt, p1, a2, now, p2, resf, rest; n long long ans = inf; n signed main() { n   input(f); n   input(t); n   input(temp); n   input(a1); n   input(cnt); n   input(p1); n   input(a2); n   input(now); n   input(p2); n   if (cnt > now) { n     swap(a1, a2); n     swap(p1, p2); n     swap(cnt, now); n   } n   for (register long long i = 0; (i - 1) * a1 <= f; ++i) { n     resf = f - min(i * a1, f); n     rest = t - min(i * a1, f) * cnt; n     if (!resf) { n       if (rest < 0) break; n       ans = min(ans, i * p1); n       break; n     } n     if (temp * resf > rest && now * resf > rest) continue; n     if (temp * resf <= rest) n       ans = min(ans, i * p1); n     else { n       long long x, pk; n       if ((rest - temp * resf) % (now - temp) == 0) n         x = (rest - temp * resf) / (now - temp) + 0; n       else n         x = (rest - temp * resf) / (now - temp) + 1; n       if (x % a2 == 0) n         pk = (x / a2) + 0; n       else n         pk = (x / a2) + 1; n       ans = min(ans, pk * p2 + i * p1); n     } n   } n   ans = max(ans, 0ll); n   if (ans < inf) n     printf( %lld n , ans); n   else n     puts( -1 ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = (int)1e5 + 10; n int n, cnt = 0; n string a, b; n void check(string a, string b) { n   int p = 0; n   for (; p < ((int)(a).size()) && a[p] == b[p]; ++p) n     ; n   if (a.substr(0, p) == b.substr(0, p) && a.substr(p) == b.substr(p + 1)) ++cnt; n } n int main() { n   cin >> n >> a >> b; n   int p = 0; n   for (; a[p] == b[p]; ++p) n     ; n   if (a.substr(0, p) == b.substr(0, p) && a.substr(p + 1) == b.substr(p + 1)) n     puts( 2 ), exit(0); n   check(b.substr(p + 1), a.substr(p)); n   check(a.substr(p + 1), b.substr(p)); n   cout << cnt << endl; n   fclose(stdin); n   fclose(stdout); n   return 0; n } n


#include <bits/stdc++.h> n  n using namespace std; n typedef long long ll; n ll a[200009],n,p; n bool vis[200009][2]; n pair <ll,ll> g[200009]; n int main() n { n     ios::sync_with_stdio(0); n     int t; n     cin>>t; n     while(t--) n     { n         cin>>n>>p; n         ll ans=0; n         for(int i=1;i<=n;i++) n         { n             cin>>a[i]; n             vis[i][0]=vis[i][1]=0; n             g[i]=make_pair(a[i],i); n         } n         vis[1][0]=vis[n][1]=1; n       sort(g+1,g+n+1); n       for(int i=1;i<=n;i++) n       { n           int w=g[i].second; n           if(a[w]>p)break; n           int l,r; n           l=r=w; n           while(!vis[l][0]&&(a[l-1]%a[w]==0))l--; n           while(!vis[r][1]&&(a[r+1]%a[w]==0))r++; n           ans+=(r-l)*a[w]; n           if(l!=r){vis[l][1]=vis[r][0]=1;} n           for(int h=l+1;h<r;h++)vis[h][0]=vis[h][1]=1; n  n  n       } n  n      for(int i=1;i<=n;i++)if(!vis[i][1])ans+=p; n      cout<<ans<<endl; n     } n     return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <class T1, class T2> n inline istream& operator>>(istream& fin, pair<T1, T2>& pr) { n   fin >> pr.first >> pr.second; n   return fin; n } n template <class T0, class T1, class T2> n inline istream& operator>>(istream& fin, tuple<T0, T1, T2>& t) { n   fin >> get<0>(t) >> get<1>(t) >> get<2>(t); n   return fin; n } n template <class T> n inline istream& operator>>(istream& fin, vector<T>& a) { n   for (auto& u : a) fin >> u; n   return fin; n } n template <class T, size_t n> n inline istream& operator>>(istream& fin, array<T, n>& a) { n   for (auto& u : a) fin >> u; n   return fin; n } n int f542() { n   size_t n, time; n   cin >> n >> time; n   typedef struct { n     size_t buf, val; n   } value_t; n   const auto comp = [](const value_t& a, const value_t& b) { n     return a.buf < b.buf || (a.buf == b.buf && a.val < b.val); n   }; n   priority_queue<value_t, vector<value_t>, decltype(comp)> heap(comp); n   for (size_t i = 0; i < n; ++i) { n     size_t val, lag; n     cin >> lag >> val; n     if (lag < time + 1) heap.push({time - lag, val}); n   } n   size_t val = 0; n   while (!heap.empty()) { n     const auto top = heap.top(); n     heap.pop(); n     if (top.buf == 0 || heap.empty()) n       val = max(val, top.val); n     else { n       const auto next = heap.top(); n       if (top.buf != next.buf) n         heap.push({next.buf, top.val}); n       else { n         heap.pop(); n         heap.push({top.buf - 1, top.val + next.val}); n       } n     } n   } n   return val; n } n int main(const int argc, char* argv[]) { n   cout << f542(); n   return EXIT_SUCCESS; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   int n, r; n   cin >> n >> r; n   vector<double> a(1 << n); n   double sum = 0, div = (double)(1 << n); n   for (int i = 0; i < (1 << n); i++) { n     int x; n     cin >> x; n     a[i] = (double)x; n     sum += a[i]; n   } n   double ans = sum / div; n   cout << setprecision(7) << fixed << ans <<   n ; n   while (r--) { n     int z, g; n     cin >> z >> g; n     double gg = (double)g; n     sum += (gg - a[z]); n     a[z] = gg; n     ans = sum / div; n     cout << setprecision(7) << fixed << ans <<   n ; n   } n   return 0; n } n