#include <bits/stdc++.h> n #include <stdio.h> n using namespace std; n  n #define all(v) v.begin(), v.end() n #define eb emplace_back n #define ll long long n  n const int N = 2e5 + 5; n  n const int inf = 1e9 + 7; n  n ll t[4 * N]; n ll z[4 * N]; n ll a[N]; n bool modify[4 * N]; n  n void push(int tl, int tr, int v) { n     if (modify[v]) { n         t[v] += z[v]; n         if (tl != tr) { n             modify[v + v + 1] = modify[v + v + 2] = 1; n             z[v + v + 1] += z[v]; n             z[v + v + 2] += z[v]; n         } n         modify[v] = 0; n         z[v] = 0; n     } n } n  n void build(int tl, int tr, int v) { n     if (tl == tr) { n         t[v] = a[tl]; n     } else { n         int tm = (tl + tr) >> 1; n         build(tl, tm, v + v + 1); n         build(tm + 1, tr, v + v + 2); n         t[v] = min(t[v + v + 1], t[v + v + 2]); n     } n } n  n ll query(int ql, int qr, int tl, int tr, int v) { n     if (ql > tr || qr < tl) { n         return inf; n     } n     push(tl, tr, v); n     if (ql <= tl && tr <= qr) { n         return t[v]; n     } n     int tm = (tl + tr) >> 1; n     return min( n                query(ql, qr, tl, tm, v + v + 1), n                query(ql, qr, tm + 1, tr, v + v + 2)); n } n  n void update(int ql, int qr, int val, int tl, int tr, int v) { n     push(tl, tr, v); n     if (ql > tr || qr < tl) { n         return; n     } n     if (ql <= tl && tr <= qr) { n         z[v] += val; n         modify[v] = 1; n         push(tl, tr, v); n     } else { n         int tm = (tl + tr) >> 1; n         update(ql, qr, val, tl, tm, v + v + 1); n         update(ql, qr, val, tm + 1, tr, v + v + 2); n         t[v] = min(t[v + v + 1], t[v + v + 2]); n     } n } n  n void solve() { n     int n; n     cin >> n; n     int answer = 0; n     vector<pair<int, int>> neg; n     for(int i = 0; i < n; i += 1) { n         int x; n         cin >> x; n         if (x < 0) { n             neg.emplace_back(x, i); n         } n         if (i) n             a[i] = a[i - 1]; n         if (x >= 0) { n             a[i] += x; n             answer += 1; n         } n     } n     build(0, n - 1, 0); n     sort(all(neg)); n     reverse(all(neg)); n  n //    for(int i = 0; i < n; i += 1) { n //        cout << query(i, i, 0, n - 1, 0) <<    ; n //    } n //    cout << endl; n     for(auto [val, id] : neg) { n         update(id, n - 1, val, 0, n - 1, 0); n         if (query(0, n - 1, 0, n - 1, 0) >= 0) { n             answer += 1; n //            cout <<  id =   << id << endl; n //            for(int i = 0; i < n; i += 1) { n //                cout << query(i, i, 0, n - 1, 0) <<    ; n //            } n //            cout << endl; n         } else { n             update(id, n - 1, -val, 0, n - 1, 0); n         } n     } n     cout << answer << endl; n } n  n int main() { n     ios_base::sync_with_stdio(false); n     cin.tie(0); n     cout.tie(0); n //    freopen( taskA.in ,  r , stdin); n //    freopen( taskA.out ,  w , stdout); n     int t = 1; n //    cin >> t; n     while(t--) { n         solve(); n     } n     return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int cnt[300007]; n int main() { n   int n, k; n   scanf( %d%d , &n, &k); n   string s; n   cin >> s; n   int check = 100; n   for (int i = (0); i <= ((int)s.size() - 1); ++i) ++cnt[s[i] -  a ]; n   for (int i = ( a ); i <= ( z ); ++i) { n     if (k >= cnt[i -  a ]) n       k -= cnt[i -  a ]; n     else { n       cnt[i -  a ] -= k; n       check = i -  a ; n       break; n     } n   } n   string str =   ; n   for (int i = (0); i <= ((int)s.size() - 1); ++i) { n     if (s[i] -  a  < check) continue; n     if (s[i] -  a  == check) { n       if (k) { n         --k; n         continue; n       } n     } n     str += s[i]; n   } n   cout << str << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n string num; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   cin >> num; n   for (int i = 0; i < num.length(); i++) { n     cout << num[i]; n   } n   for (int i = num.length() - 1; i >= 0; i--) { n     cout << num[i]; n   } 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-7; n const long long MOD = 1000000007LL; n const int INF = 0x3f3f3f3f; n const int _NAN = -0x3f3f3f3f; n const double EULC = 0.5772156649015328; n const int NIL = -1; n template <typename T> n void read(T &x) { n   x = 0; n   char ch = getchar(); n   long long f = 1; n   while (!isdigit(ch)) { n     if (ch ==  - ) f *= -1; n     ch = getchar(); n   } n   while (isdigit(ch)) { n     x = x * 10 + ch - 48; n     ch = getchar(); n   } n   x *= f; n } n const int maxn = 1e7 + 10; n bool isprime[maxn] = {1, 1}; n int prime[maxn], kase; n vector<long long> ve; n long long ls[12] = {0, 1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024}; n int ans; n void primeMaker() { n   for (int i = 2; i < maxn; ++i) { n     if (!isprime[i]) prime[kase++] = i; n     for (int j = 0; j < kase && prime[j] * i < maxn; ++j) { n       isprime[i * prime[j]] = true; n       if (!(i % prime[j])) break; n     } n   } n } n int solve(long long &n) { n   long long t2 = n; n   int __max = -1; n   for (int i = 0; i < kase && prime[i] <= t2; ++i) { n     if (!(t2 % prime[i])) { n       int cnt = 0; n       while (!(t2 % prime[i])) { n         ++cnt; n         t2 /= prime[i]; n       } n       if (__max != -1 && __max != cnt) ++ans; n       __max = max(__max, cnt); n       ve.push_back(prime[i]); n     } n   } n   if (ans) ans = 1; n   if (__max == -1) __max = 1; n   for (int i = 0; i < 10; ++i) { n     if (__max == ls[i]) break; n     if (__max > ls[i] && __max < ls[i + 1]) { n       __max = ls[i + 1]; n       if (ans != 1) ++ans; n       break; n     } n   } n   n = 1; n   if (!ve.empty()) { n     for (auto num : ve) n *= num; n   } n   return __max; n } n int main(void) { n   primeMaker(); n   long long n; n   while (~scanf( %lld , &n)) { n     ans = 0; n     if (!isprime[n]) { n       printf( %lld %d n , n, 0); n       continue; n     } n     int res = solve(n); n     ans += log2l(res) + eps; n     printf( %lld %d n , n, ans); n     ve.clear(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<int> g[100005]; n int par[18][100005]; n int depth[100005]; n void dfs(int u, int p = 0, int d = 0) { n   par[0][u] = p; n   depth[u] = d; n   for (int i = 0; i < g[u].size(); i++) { n     int v = g[u][i]; n     if (v == p) continue; n     dfs(v, u, d + 1); n   } n } n int LCA(int a, int b) { n   if (depth[a] < depth[b]) swap(a, b); n   int diff = depth[a] - depth[b]; n   for (int i = 17; i >= 0; i--) n     if ((diff >> i) & 1) a = par[i][a]; n   if (a == b) return a; n   for (int i = 17; i >= 0; i--) { n     if (par[i][a] != par[i][b]) a = par[i][a], b = par[i][b]; n   } n   return par[0][a]; n } n int dist(int a, int b) { n   int lca = LCA(a, b); n   return depth[a] + depth[b] - 2 * depth[lca]; n } n int main() { n   int n, q, p; n   scanf( %d%d , &n, &q); n   for (int i = 2; i <= n; i++) n     scanf( %d , &p), g[i].push_back(p), g[p].push_back(i); n   dfs(1); n   for (int j = 1; j <= 17; j++) n     for (int i = 1; i <= n; i++) n       if (par[j - 1][i] != 0) par[j][i] = par[j - 1][par[j - 1][i]]; n   while (q--) { n     int a[3]; n     scanf( %d%d%d , &a[0], &a[1], &a[2]); n     int ans = -1; n     for (int i = 0; i < 3; i++) { n       for (int j = 0; j < 3; j++) { n         for (int k = 0; k < 3; k++) { n           if (i == j || i == k || j == k) continue; n           int s = a[i], t = a[j], f = a[k]; n           ans = max(ans, (dist(s, f) + dist(t, f) - dist(s, t) + 1) / 2); n         } n       } n     } n     printf( %d n , ans + 1); n   } n } n


#include <bits/stdc++.h> n using namespace std; n bool is_integer(long double k) { return std::floor(k) == k; } n int main() { n   long long i, j = 0; n   string s; n   cin >> s; n   if (s.length() == 1 && s[0] ==  0 ) { n     cout << 0; n     return 0; n   } n   int c = 0; n   for (i = (s.length() - 1); i >= 0; i--) { n     if (s[i] ==  1 ) { n       c++; n     } n   } n   if (c == 1) { n     cout << floor(s.length() / 2.0); n   } else { n     cout << ceil(s.length() / 2.0); n   } n } n


#include <bits/stdc++.h> n using namespace std; n using tint = tuple<int, int, int>; n const int maxn = 200004; n int N, M; n tint a[maxn]; n struct dsu { n   dsu() { iota(p, p + maxn, 0); } n   int p[maxn], add[maxn]; n   int root(int k) { return p[k] != k ? p[k] = root(p[k]) : k; } n   bool connect(int x, int y) { n     x = root(x); n     y = root(y); n     if (x == y) return false; n     p[y] = x; n     add[x] |= add[y]; n     return true; n   } n } d; n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cin >> N >> M; n   for (int i = 0; i < M; i++) { n     int x, y, w; n     cin >> x >> y >> w; n     a[i] = tint(x, y, w); n   } n   sort(a, a + M, [](tint x, tint y) { return get<2>(x) > get<2>(y); }); n   int ans = 0; n   for (int i = 0; i < M; i++) { n     int x, y, w; n     tie(x, y, w) = a[i]; n     if (d.root(x) != d.root(y) and not(d.add[d.root(x)] and d.add[d.root(y)])) { n       ans += w; n       d.connect(x, y); n     } else if (!d.add[d.root(x)]) { n       ans += w; n       d.add[d.root(x)] = true; n     } n   } n   cout << ans <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n const int INF = 1e9; n int totb = 0, totr = 0, totx = 0, deep; n int n; n struct data { n   int next, num; n } edgeb[400001], edger[400001]; n struct data2 { n   int x; n   bool judge; n } ans[200001]; n struct seg { n   int l, r, va; n } tr[1600001], tb[1600001]; n int headb[200001], headr[200001]; n int wb[200001], wr[200001], vr[200001], vb[200001], db[200001], dr[200001]; n int fab[200001][19], far[200001][19], lb[200001], rb[200001], lr[200001], n     rr[200001]; n int eb[200001], er[200001], vab[400001], var[400001], locb[400001], n     locr[400001], px; n bool visb[200001], visr[200001]; n queue<data2> q; n bool cmp(data2 p, data2 q) { return p.x < q.x; } n void add1(int u, int v) { n   edgeb[++totb].next = headb[u]; n   edgeb[totb].num = v; n   headb[u] = totb; n } n void add2(int u, int v) { n   edger[++totr].next = headr[u]; n   edger[totr].num = v; n   headr[u] = totr; n } n void dfsb1(int x) { n   wb[x] = vb[x]; n   db[x] = deep; n   lb[x] = totx + 1; n   totx += wb[x]; n   for (int i = headb[x]; i != -1; i = edgeb[i].next) { n     int kx = edgeb[i].num; n     if (!db[kx]) { n       deep++; n       fab[kx][0] = x; n       dfsb1(kx); n       deep--; n       wb[x] += wb[kx]; n     } n   } n   rb[x] = totx; n } n void dfsr1(int x) { n   wr[x] = vr[x]; n   dr[x] = deep; n   lr[x] = totx + 1; n   totx += wr[x]; n   for (int i = headr[x]; i != -1; i = edger[i].next) { n     int kx = edger[i].num; n     if (!dr[kx]) { n       deep++; n       far[kx][0] = x; n       dfsr1(kx); n       deep--; n       wr[x] += wr[kx]; n     } n   } n   rr[x] = totx; n } n void build(int i, int l, int r) { n   tr[i].l = tb[i].l = l; n   tr[i].r = tb[i].r = r; n   if (l == r) { n     tb[i].va = vab[l]; n     tr[i].va = var[l]; n     if (locr[l] == px) tr[i].va = INF; n     return; n   } n   int mid = (l + r) / 2; n   build(i * 2, l, mid); n   build(i * 2 + 1, mid + 1, r); n   tr[i].va = min(tr[i * 2].va, tr[i * 2 + 1].va); n   tb[i].va = min(tb[i * 2].va, tb[i * 2 + 1].va); n } n void deleb(int i, int l, int r, int va) { n   int nowl = tb[i].l, nowr = tb[i].r; n   if (nowl > r || nowr < l) return; n   if (tb[i].va > va) return; n   if (nowl == nowr) { n     tb[i].va = INF; n     if (visb[locb[nowl]]) return; n     tb[i].va = INF; n     data2 now; n     now.x = locb[nowl]; n     now.judge = 1; n     q.push(now); n     visb[now.x] = 1; n     return; n   } n   deleb(i * 2, l, r, va); n   deleb(i * 2 + 1, l, r, va); n   tb[i].va = min(tb[i * 2].va, tb[i * 2 + 1].va); n } n void deler(int i, int l, int r, int va) { n   int nowl = tr[i].l, nowr = tr[i].r; n   if (nowl > r || nowr < l) return; n   if (tr[i].va > va) return; n   if (nowl == nowr) { n     tr[i].va = INF; n     if (visr[locr[nowl]]) return; n     data2 now; n     now.x = locr[nowl]; n     now.judge = 0; n     q.push(now); n     visr[now.x] = 1; n     return; n   } n   deler(i * 2, l, r, va); n   deler(i * 2 + 1, l, r, va); n   tr[i].va = min(tr[i * 2].va, tr[i * 2 + 1].va); n } n void prelca() { n   fab[1][0] = far[1][0] = 1; n   for (int j = 1; j <= 18; j++) n     for (int i = 1; i <= n; i++) { n       far[i][j] = far[far[i][j - 1]][j - 1]; n       fab[i][j] = fab[fab[i][j - 1]][j - 1]; n     } n } n int lcab(int u, int v) { n   while (db[u] > db[v]) { n     int kx = log2(db[u] - db[v]); n     u = fab[u][kx]; n   } n   while (db[u] < db[v]) { n     int kx = log2(db[v] - db[u]); n     v = fab[v][kx]; n   } n   if (u == v) return u; n   int kg = log2(db[u]); n   for (int i = kg; i >= 0; i--) { n     if (fab[u][i] == fab[v][i]) continue; n     u = fab[u][i]; n     v = fab[v][i]; n   } n   return fab[u][0]; n } n int lcar(int u, int v) { n   while (dr[u] > dr[v]) { n     int kx = log2(dr[u] - dr[v]); n     u = far[u][kx]; n   } n   while (dr[u] < dr[v]) { n     int kx = log2(dr[v] - dr[u]); n     v = far[v][kx]; n   } n   if (u == v) return u; n   int kg = log2(dr[u]); n   for (int i = kg; i >= 0; i--) { n     if (far[u][i] == far[v][i]) continue; n     u = far[u][i]; n     v = far[v][i]; n   } n   return far[u][0]; n } n void prep() { n   memset(headr, -1, sizeof(headr)); n   memset(headb, -1, sizeof(headb)); n   scanf( %d , &n); n   for (int i = 2; i <= n; i++) { n     scanf( %d , &eb[i]); n     add1(i, eb[i]); n     add1(eb[i], i); n   } n   for (int i = 2; i <= n; i++) { n     scanf( %d , &er[i]); n     add2(i, er[i]); n     add2(er[i], i); n   } n   for (int i = 2; i <= n; i++) { n     vr[i]++; n     vr[eb[i]]++; n     vb[i]++; n     vb[er[i]]++; n   } n   deep = 1; n   totx = 0; n   dfsb1(1); n   deep = 1; n   totx = 0; n   dfsr1(1); n   memset(vr, 0, sizeof(vr)); n   memset(vb, 0, sizeof(vb)); n   prelca(); n   for (int i = 2; i <= n; i++) { n     vr[i]++; n     vr[eb[i]]++; n     var[vr[i] + lr[i] - 1] = var[vr[eb[i]] + lr[eb[i]] - 1] = n         dr[lcar(i, eb[i])]; n     locr[vr[i] + lr[i] - 1] = locr[vr[eb[i]] + lr[eb[i]] - 1] = i - 1; n     vb[i]++; n     vb[er[i]]++; n     vab[vb[i] + lb[i] - 1] = vab[vb[er[i]] + lb[er[i]] - 1] = n         db[lcab(i, er[i])]; n     locb[vb[i] + lb[i] - 1] = locb[vb[er[i]] + lb[er[i]] - 1] = i - 1; n   } n   scanf( %d , &px); n   build(1, 1, 2 * n - 2); n } n int getp(data2 x) { n   int i = x.x; n   if (x.judge == 0) { n     if (db[i + 1] < db[eb[i + 1]]) return eb[i + 1]; n     return i + 1; n   } n   if (dr[i + 1] < dr[er[i + 1]]) return er[i + 1]; n   return i + 1; n } n void work() { n   data2 gx; n   gx.judge = 0; n   gx.x = px; n   q.push(gx); n   visr[px] = 1; n   int headpx = 0; n   while (!q.empty()) { n     data2 k = q.front(); n     ans[++headpx] = k; n     q.pop(); n     int point = getp(k); n     if (k.judge == 0) n       deleb(1, lb[point], rb[point], db[point] - 1); n     else n       deler(1, lr[point], rr[point], dr[point] - 1); n   } n   printf( Blue n%d , ans[1].x); n   for (int i = 2; i <= headpx; i++) { n     if (ans[i].judge != ans[i - 1].judge) { n       int nowj = i; n       while (nowj <= headpx && ans[nowj].judge == ans[i].judge) nowj++; n       sort(ans + i, ans + nowj, cmp); n       if (ans[i].judge == 0) n         printf(  nBlue n ); n       else n         printf(  nRed n ); n     } n     printf( %d  , ans[i].x); n   } n } n int main() { n   prep(); n   work(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 3e6 + 7; n const int mod = 1e9 + 7; n long long int dp[maxn] = {0}; n int main() { n   int t; n   cin >> t; n   dp[1] = 0; n   dp[2] = 0; n   dp[3] = 1; n   for (int i = 4; i < maxn; i++) { n     dp[i] = 2 * dp[i - 2] + dp[i - 1]; n     if (i % 3 == 0) dp[i]++; n     dp[i] %= mod; n   } n   while (t--) { n     int n; n     cin >> n; n     cout << (dp[n] * 4 + mod) % mod << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 200010; n const int MOD = 1e9 + 7; n int read() { n   int x = 0, ch; n   while (isspace(ch = getchar())) n     ; n   do x = x * 10 + ch -  0 ; n   while (isdigit(ch = getchar())); n   return x; n } n int n; n map<int, int> idx, idy; n int nid; n namespace G { n int head[MAXN], nxt[MAXN], to[MAXN], m; n void init() { memset(head, -1, sizeof head); } n void adde(int u, int v) { n   to[m] = v, nxt[m] = head[u], head[u] = m++; n   to[m] = u, nxt[m] = head[v], head[v] = m++; n } n }  // namespace G n int fpow(int a, int b) { n   int c = 1; n   while (b) { n     if (b & 1) c = int(1LL * c * a % MOD); n     a = int(1LL * a * a % MOD); n     b >>= 1; n   } n   return c; n } n int vis[MAXN]; n queue<int> q; n int solve(int s) { n   using namespace G; n   int e = 0, o = 0; n   vis[s] = 1, q.push(s); n   while (!q.empty()) { n     int u = q.front(); n     q.pop(); n     ++o; n     for (int i = head[u]; ~i; i = nxt[i], ++e) n       if (!vis[to[i]]) vis[to[i]] = 1, q.push(to[i]); n   } n   e >>= 1; n   return e == o - 1 ? (fpow(2, o) - 1 + MOD) % MOD : fpow(2, o); n } n int main() { n   n = read(); n   G::init(); n   for (int i = 0; i < n; ++i) { n     int x = read(), y = read(); n     if (!idx[x]) idx[x] = ++nid; n     if (!idy[y]) idy[y] = ++nid; n     G::adde(idx[x], idy[y]); n   } n   int ans = 1; n   for (int i = 1; i <= nid; ++i) n     if (!vis[i]) ans = int(1LL * ans * solve(i) % MOD); n   printf( %d n , ans); n   return 0; n } n


#include <iostream> n #include <vector> n #include <algorithm> n #include <string> n #include <ctype.h> n #include <deque> n #include <cstring> n #include <set> n #include <bitset> n #include <map> n #include <chrono> n #include <random> n #include <unordered_map> n #include <stdio.h> n  n using namespace std; n  n typedef long long ll; n typedef long double ld; n typedef std::vector<int> vi; n typedef std::vector<bool> vb; n typedef std::vector<string> vs; n typedef std::vector<double> vd; n typedef std::vector<long long> vll; n typedef std::vector<std::vector<int> > vvi; n typedef vector<vll> vvll; n typedef std::vector<std::pair<int, int> > vpi; n typedef vector<vpi> vvpi; n typedef std::pair<int, int> pi; n typedef std::pair<ll, ll> pll; n typedef std::vector<pll> vpll; n  n const long long mod = 1000000007; n const unsigned gen_seed = std::chrono::system_clock::now().time_since_epoch().count(); n std::mt19937_64 gen(gen_seed); n  n #define all(c) (c).begin(),(c).end() n #define srt(c) sort(all(c)) n #define srtrev(c) sort(all(c)); reverse(all(c)) n #define forn(i, a, b) for(int i = a; i < b; i++) n #define read(x) scanf( %d , &x) n #define readv(x, n) vi x(n); forn(i,0,n) scanf( %d , &x[i]) n  n #define pb push_back n #define mp make_pair n  n void mul(vi & a, int k) { n     vi b(k,0); n     for(auto x : a) b.pb(x); n     forn(i,0,a.size()) b[i] = (b[i] + mod-a[i])%mod; n     a.resize(b.size() - 1); n     for(int i = b.size() - 1; i > 0; i--) { n         a[i-1] = b[i]; n         b[i-1] = (b[i] + b[i-1])%mod; n     } n } n vi c, b; n int n; n int get(int x) { n     vi p(1,1); n     int sb = 0; n     int sbt = 0; n     int add = 0; n     forn(i,0,n) { n         mul(p, c[i]+1); n         int cur = x*(i+1) + sbt - add; n         if(cur > (int)p.size()) { n             return 0; n         } n         if(cur>0) { n             forn(j,0,p.size() - cur) p[j] = p[j + cur]; n             p.resize(p.size() - cur); n             add += cur; n         } n          n         sb+=b[i]; n         sbt+=sb; n     } n     ll ans = 0; n     forn(i,0,p.size()) ans = ans+p[i]; n     return ans % mod; n } n set<int> cool; n int death = 1000; n  n void getcool() { n     int sb = 0; n     int sbt = 0; n     int dp = 0; n     forn(i,0,n) { n         dp += c[i]; n  n         forn(x,-sbt/(i+1),(dp-sbt)/(i+1)+2) { n             int cur = x*(i+1) + sbt; n             if(cur >0 && cur <= dp) { n                 cool.insert(x); n             } n             else if(cur > dp) { n                 death = min(death, x); n             } n         } n         sb+=b[i]; n         sbt+=sb; n     } n } n  n  n int main() n { n #ifndef ONLINE_JUDGE n     freopen( input.txt ,  rt , stdin); n     freopen( output.txt ,  wt , stdout); n #endif n      n     scanf( %d , &n); n //    readv(c,n); n //    readv(b,n-1); n     c.resize(n); n     b.resize(n-1); n     forn(i,0,n) scanf( %d , &c[i]); n     forn(i,0,n-1) scanf( %d , &b[i]); n     int q; n     read(q); n     getcool(); n     map<int, int> ans; n     for(auto x : cool) { n         if(x < death) ans[x] = get(x); n         else ans[x] = 0; n     } n     ll full = 1; n     forn(i,0,n) full = (full * (c[i]+1))%mod; n  n     while(q--) { n         int x; n         read(x); n         if(cool.find(x) != cool.end()) printf( %d n , ans[x]); n         else if(x >= death) printf( 0 n ); n         else printf( %d n , full); n     } n      n } n


#include <bits/stdc++.h> n using namespace std; n const int inf = ~0u >> 2; n const int N = 500010; n int Max[N << 2]; n struct node { n   int x, y, z; n } in[N]; n int tot, n; n int num[N]; n int max(int a, int b) { return a > b ? a : b; } n void pushup(int rt) { Max[rt] = max(Max[rt << 1], Max[rt << 1 | 1]); } n void build(int l, int r, int rt) { n   Max[rt] = 0; n   if (l == r) return; n   int m = (l + r) >> 1; n   build(l, m, rt << 1); n   build(m + 1, r, rt << 1 | 1); n } n void update(int p, int val, int l, int r, int rt) { n   if (l == r) { n     if (val > Max[rt]) Max[rt] = val; n     return; n   } n   int m = (l + r) >> 1; n   if (p <= m) n     update(p, val, l, m, rt << 1); n   else n     update(p, val, m + 1, r, rt << 1 | 1); n   pushup(rt); n } n int query(int L, int R, int l, int r, int rt) { n   if (L <= l && r <= R) return Max[rt]; n   int m = (l + r) >> 1; n   int ret = 0; n   if (L <= m) ret = max(ret, query(L, R, l, m, rt << 1)); n   if (R > m) ret = max(ret, query(L, R, m + 1, r, rt << 1 | 1)); n   return ret; n } n void init() { n   sort(num + 1, num + n + 1); n   tot = unique(num + 1, num + n + 1) - num - 1; n   build(1, tot, 1); n   for (int i = 1; i <= n; i++) n     in[i].x = lower_bound(num + 1, num + 1 + tot, in[i].x) - num; n } n inline int cmp(node a, node b) { return a.y > b.y; } n int main() { n   int i, j, k; n   scanf( %d , &n); n   for (i = 1; i <= n; i++) { n     scanf( %d , &in[i].x); n     num[i] = in[i].x; n   } n   for (i = 1; i <= n; i++) scanf( %d , &in[i].y); n   for (i = 1; i <= n; i++) scanf( %d , &in[i].z); n   init(); n   sort(in + 1, in + n + 1, cmp); n   int ans = 0; n   for (i = 1; i <= n; i = j) { n     for (j = i; j <= n && in[j].y == in[i].y; j++) { n       int z; n       z = query(in[j].x + 1, tot, 1, tot, 1); n       if (z > in[j].z) ans++; n     } n     for (; i <= j - 1; i++) { n       update(in[i].x, in[i].z, 1, tot, 1); n     } n   } n   printf( %d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n namespace chtholly { n char buf[1 << 23], *p1 = buf, *p2 = buf; n inline int read() { n   int x = 0, f = 1; n   char c = getchar(); n   for (; !isdigit(c); c = getchar()) f ^= c ==  - ; n   for (; isdigit(c); c = getchar()) x = (x << 3) + (x << 1) + (c ^  0 ); n   return f ? x : -x; n } n template <typename mitsuha> n inline bool read(mitsuha &x) { n   x = 0; n   int f = 1; n   char c = getchar(); n   for (; !isdigit(c) && ~c; c = getchar()) f ^= c ==  - ; n   if (!~c) return 0; n   for (; isdigit(c); c = getchar()) x = (x << 3) + (x << 1) + (c ^  0 ); n   return x = f ? x : -x, 1; n } n template <typename mitsuha> n inline int write(mitsuha x) { n   if (!x) return 0 & putchar(48); n   if (x < 0) putchar( - ), x = -x; n   int bit[20], i, p = 0; n   for (; x; x /= 10) bit[++p] = x % 10; n   for (i = p; i; --i) putchar(bit[i] + 48); n   return 0; n } n inline char fuhao() { n   char c = getchar(); n   for (; isspace(c); c = getchar()) n     ; n   return c; n } n }  // namespace chtholly n using namespace chtholly; n using namespace std; n const int yuzu = 1e5; n typedef long long fuko[yuzu | 10]; n fuko a, b, c, d; n int n, k; n typedef long long karen[yuzu << 2 | 13]; n struct segtree { n   karen val; n   void build(int rt = 1, int l = 1, int r = n) { n     if (l == r) n       val[rt] = read() % k; n     else { n       int mid = l + r >> 1; n       build(rt << 1, l, mid), build(rt << 1 | 1, mid + 1, r); n       val[rt] = val[rt << 1] * val[rt << 1 | 1] % k; n     } n   } n   long long query(int ql, int qr, int rt = 1, int l = 1, int r = n) { n     if (ql > r || qr < l) return 1; n     if (ql <= l && qr >= r) return val[rt]; n     int mid = l + r >> 1; n     return query(ql, qr, rt << 1, l, mid) * n            query(ql, qr, rt << 1 | 1, mid + 1, r) % k; n   } n } my; n int main() { n   read(n), read(k), my.build(); n   int i, l = 1, r = 1; n   long long llx = 0; n   for (; r <= n; llx += n - r + 1, ++l, r = max(l, r)) n     for (; r <= n && my.query(l, r); ++r) n       ; n   write(llx); n } n


#include <bits/stdc++.h> n using namespace std; n const long long INF = 1e17; n const long long maxn = 2e5 + 700; n const int mod = 1e9 + 7; n template <typename T> n inline void read(T &a) { n   char c = getchar(); n   T x = 0, f = 1; n   while (!isdigit(c)) { n     if (c ==  - ) f = -1; n     c = getchar(); n   } n   while (isdigit(c)) { n     x = (x << 1) + (x << 3) + c -  0 ; n     c = getchar(); n   } n   a = f * x; n } n long long n, m, p; n char s[maxn]; n int a[maxn], b[maxn]; n int digit(long long x, int *d) { n   int ans = 0; n   while (x) { n     d[++ans] = x % 10; n     x /= 10; n   } n   return ans; n } n int path[maxn]; n int check(int pos, int lbound, int rbound) { n   if (!pos) return 1; n   if (!lbound && !rbound) return 1; n   int l = lbound ? a[pos] : 0; n   int r = rbound ? b[pos] : 9; n   for (int i = l; i <= r; i++) { n     if (path[i] > 0) { n       path[i]--; n       if (check(pos - 1, lbound && i == l, rbound && i == r)) { n         path[i]++; n         return 1; n       } n       path[i]++; n     } n   } n   return 0; n } n int pos, res = 0; n void dfs(int u, int w) { n   if (u == 9) { n     path[u] = w; n     if (check(pos, 1, 1)) res++; n     return; n   } n   for (int i = 0; i <= w; i++) { n     path[u] = i; n     dfs(u + 1, w - i); n   } n } n int main() { n   read(n); n   read(m); n   digit(n, a); n   pos = digit(m, b); n   dfs(0, pos); n   printf( %d n , res); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int q[3001], t, n; n int main() { n   scanf( %d , &n); n   for (int i = 2; i <= n; i++) { n     if (q[i - 1] == 2) t++; n     if (q[i] > 0) continue; n     for (int j = i; j <= n; j += i) q[j]++; n   } n   if (q[n] == 2) t++; n   printf( %d n , t); n } n


#include <bits/stdc++.h> n using namespace std; n long long n, x; n vector<string> ans; n long long dec(string s) { n   long long ret = 0; n   for (long long i = 0; i < s.size(); i++) { n     ret = ret * 10 + s[i] - 48; n   } n   return ret; n } n void add(long long v) { n   long long rs = 0; n   if (ans.size() == 0) { n     string tmp =   ; n     while (v > 9) { n       tmp =  9  + tmp; n       v -= 9; n     } n     tmp = to_string(v) + tmp; n     ans.push_back(tmp); n   } else { n     string p_s = ans[ans.size() - 1], req = p_s; n     long long cnt = 0; n     for (long long i = 0; i < req.size(); i++) { n       cnt += req[i] - 48; n     } n     if (cnt < v) { n       long long df = v - cnt; n       for (long long i = req.size() - 1; i >= 0; i--) { n         if (req[i] <  9  && df) { n           long long dy =  9  - req[i]; n           req[i] += min(df, dy); n           df -= min(df, dy); n           if (!df) { n             ans.push_back(req); n             return; n           } n         } n       } n       while (df >= 9) { n         req =  9  + req; n         df -= 9; n       } n       if (v) req = to_string(df) + req; n       ans.push_back(req); n     } else { n       string p_s = ans[ans.size() - 1]; n       long long idx = -1, sm = 0, tmp_sm = 0; n       for (long long i = 0; i < p_s.size(); i++) { n         tmp_sm += p_s[i] - 48; n         if (v > tmp_sm && p_s[i] <  9 ) { n           idx = i; n           sm = tmp_sm; n         } else if (v < sm) n           break; n       } n       if (idx == -1) { n         string p_s =  1 ; n         long long sz = ans[ans.size() - 1].size(); n         for (long long i = 0; i < sz; i++) p_s = p_s +  0 ; n         v -= 1; n         long long idx = p_s.size() - 1; n         while (idx > 0 && v > 0) { n           char c = (char)min((long long) 9 , v + 48); n           p_s[idx] = c; n           idx--, v -= 9; n         } n         ans.push_back(p_s); n       } else { n         sm = v - sm - 1; n         p_s[idx] += 1; n         for (long long j = idx + 1; j < p_s.size(); j++) { n           p_s[j] =  0 ; n         } n         for (long long i = p_s.size() - 1; i > idx; i--) { n           if (sm > 0) { n             long long dx =  9  - p_s[i]; n             p_s[i] += min(sm, dx); n             sm -= min(sm, dx); n           } else n             break; n         } n         ans.push_back(p_s); n       } n     } n   } n } n int main() { n   cin >> n; n   for (long long i = 0; i < n; i++) { n     cin >> x; n     add(x); n   } n   for (long long i = 0; i < ans.size(); i++) cout << ans[i] << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int N; n   scanf( %d , &N); n   if (N == 1) { n     puts( 1 ); n     return 0; n   } n   int s = 0; n   for (int i = 0; i < N; ++i) { n     int t; n     scanf( %d , &t); n     s += t; n   } n   printf( %d n , s % N == 0 ? N : N - 1); n } n


#include <bits/stdc++.h> n using namespace std; n const double PI = acos(-1.0); n #pragma GCC diagnostic ignored  -Wsign-compare  n int main() { n   int n, ind = 0; n   scanf( %d , &n); n   vector<long long> vals(n); n   vector<long long> pref(n); n   for (int i = 0; i < n; i++) { n     scanf( %d , &vals[i]); n   } n   42; n   partial_sum(vals.begin(), vals.end(), pref.begin()); n   vals.insert(vals.begin(), 0); n   pref.insert(pref.begin(), 0); n   vector<double> hull(n); n   vector<pair<int, int> > inds(n); n   for (int i = 1; i <= n; i++) { n     double cur = vals[i]; n     pair<int, int> use = make_pair(i, i); n     while (ind > 0 and hull[ind - 1] > cur) { n       ind--; n       cur = ((double)pref[i] - pref[inds[ind].first - 1]) / n             ((double)i - inds[ind].first + (double)1.000000); n       use = make_pair(inds[ind].first, i); n     } n     hull[ind] = cur; n     inds[ind] = use; n     ind++; n   } n   42; n   for (int i = 0; i < ind; i++) { n     for (int j = inds[i].first; j <= inds[i].second; j++) { n       printf( %.10f n , hull[i]); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long gcd(long long a, long long b) { n   if (a < b) { n     swap(a, b); n   } n   if (a % b == 0) { n     return b; n   } n   return gcd(b, a % b); n } n vector<vector<int>> gr; n vector<int> used; n void dfs(int v) { n   used[v] = 1; n   for (auto i : gr[v]) { n     if (!used[i]) { n       dfs(i); n     } n   } n } n void solution() { n   long int n; n   cin >> n; n   vector<long long> mass(n); n   long long minn = 1e10; n   long long minn_ind = 0; n   for (int i = 0; i < n; i++) { n     cin >> mass[i]; n     if (mass[i] < minn) { n       minn = mass[i]; n       minn_ind = i; n     } n   } n   gr = vector<vector<int>>(n); n   used = vector<int>(n, 0); n   for (int i = 0; i < n; i++) { n     if (gcd(mass[i], minn) == minn) { n       gr[i].push_back(minn_ind); n       gr[minn_ind].push_back(i); n     } n   } n   dfs(minn_ind); n   vector<long long> sorted = mass; n   sort(sorted.begin(), sorted.end()); n   vector<long long> get_ans; n   for (int i = 0; i < n; i++) { n     if (mass[i] != sorted[i] || i == minn_ind) { n       get_ans.push_back(i); n     } n   } n   bool ans = 1; n   for (auto i : get_ans) { n     if (!used[i]) { n       ans = 0; n       break; n     } n   } n   if (ans) { n     cout <<  YES ; n   } else { n     cout <<  NO ; n   } n   cout << endl; n } n int main() { n   int t; n   cin >> t; n   for (int i = 0; i < t; i++) { n     solution(); n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int res; n   cin >> res; n   if (res % 2 == 0) { n     cout <<  Mahmoud ; n   } else { n     cout <<  Ehab ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int grundy[] = {0, 1, 2, 1, 4, 3, 2, 1, 5, 6,  2, 1, 8, 7,  5, n                 9, 8, 7, 3, 4, 7, 4, 2, 1, 10, 9, 3, 6, 11, 12}; n int main() { n   int n; n   scanf( %d , &n); n   int ones = n; n   bool vis[100100] = {false}; n   vector<int> v; n   for (long long i = 2; i * i <= n; i++) { n     if (vis[i]) continue; n     int size = 0; n     for (long long j = i; j <= n; j = j * i) { n       if (j * j <= n) vis[j] = true; n       size++; n       ones--; n     } n     v.push_back(grundy[size]); n   } n   ones %= 2; n   for (int i = 0; i < (int)v.size(); i++) ones = ones ^ v[i]; n   if (ones) n     cout <<  Vasya  << endl; n   else n     cout <<  Petya  << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long xyp(long long x, long long y) { n   if (y == 0) return 1LL; n   if (y == 1) return x; n   if (y % 2) { n     long long p = xyp(x, y - 1); n     return (x * p) % 1000000007; n   } n   long long p = xyp(x, y / 2); n   return (p * p) % 1000000007; n } n long long inv(long long x) { return xyp(x, 1000000007 - 2); } n long long fact[10000007]; n long long pr[10000007]; n vector<long long> rp; n void init() { n   fact[0] = 1; n   for (long long i = 1; i < 10000007; i++) { n     fact[i] = (fact[i - 1] * i) % 1000000007; n   } n } n long long ncr(long long n, long long r) { n   if (r < 0) return 0LL; n   if (r == 0 || n == r) return 1LL; n   if (r == 1 || ((n - r) == 1)) return n; n   long long p = n       (((fact[n] * inv(fact[r])) % 1000000007) * inv(fact[n - r])) % 1000000007; n   return p; n } n void sieve() { n   memset(pr, 1, sizeof(pr)); n   pr[0] = pr[1] = 0; n   for (long long i = 2; i < 10000007; i++) { n     if (pr[i]) { n       rp.push_back(i); n       for (long long j = i * i; j < 10000007; j += i) { n         pr[j] = 0; n       } n     } n   } n } n void solve() { n   long long n, k; n   cin >> n >> k; n   long long a[n + 1], b[n + 1]; n   long long flag = 1; n   for (long long i = 1; i <= n; i++) { n     cin >> a[i]; n     b[i] = a[i]; n     if (a[i] == k) flag = 0; n   } n   if (flag) { n     cout <<  no n ; n     return; n   } n   sort(b + 1, b + n + 1); n   if (b[(n + 1) / 2] == k) { n     cout <<  yes n ; n     return; n   } n   if (n == 1) { n     if (a[1] == k) n       cout <<  yes n ; n     else n       cout <<  no n ; n     return; n   } n   if (n == 2) { n     if (b[1] == k) n       cout <<  yes n ; n     else n       cout <<  no n ; n     return; n   } n   vector<long long> v; n   for (long long i = 1; i <= n; i++) { n     if (a[i] >= k) { n       v.push_back(i); n     } n   } n   flag = 0; n   for (long long i = 0; i < v.size() - 1; i++) { n     if ((v[i + 1] - v[i]) <= 2) { n       flag = 1; n     } n   } n   if (flag) n     cout <<  yes n ; n   else n     cout <<  no n ; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   ; n   int testcases; n   cin >> testcases; n   while (testcases--) { n     solve(); n   } n } n


#include <bits/stdc++.h> n using namespace std; n const long double PI = acos(-1.0); n const long double EPS = 1e-14; n const long long INF = 1e18; n const long long MOD = 1e9 + 7; n const long long p = 97; n signed main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.precision(9); n   long long n; n   cin >> n; n   vector<long long> a(n); n   for (long long i = 0; i < n; i++) { n     cin >> a[i]; n   } n   vector<vector<long long>> pr(n, vector<long long>(6, INF)); n   pr[0] = vector<long long>(6, -1); n   for (long long i = 1; i < n; i++) { n     for (long long j = 1; j <= 5; j++) { n       if (pr[i - 1][j] == INF) continue; n       for (long long z = 1; z <= 5; z++) { n         if (a[i] > a[i - 1]) { n           if (z > j) { n             pr[i][z] = j; n           } n         } else if (a[i] < a[i - 1]) { n           if (z < j) { n             pr[i][z] = j; n           } n         } else { n           if (z != j) { n             pr[i][z] = j; n           } n         } n       } n     } n   } n   long long last = -1; n   for (long long i = 1; i <= 5; i++) { n     if (pr[n - 1][i] != INF) last = i; n   } n   if (last == -1) { n     cout << -1; n     return 0; n   } n   long long num = n - 1; n   vector<long long> ans; n   while (num != -1) { n     ans.push_back(last); n     last = pr[num][last]; n     num--; n   } n   reverse(ans.begin(), ans.end()); n   for (long long i = 0; i < ans.size(); i++) { n     cout << ans[i] <<    ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int nCases; n   cin >> nCases; n   for (int i = 0; i < nCases; i++) { n     long long nCandy = 0; n     cin >> nCandy; n     if (nCandy == 1 || nCandy == 2) { n       cout << 0 << endl; n     } else if (nCandy % 2 == 0) { n       cout << (nCandy / 2) - 1 << endl; n     } else { n       cout << nCandy / 2 << endl; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 300005, B = 35; n int n; n char t[N]; n struct AC { n   struct Trie { n     int ch[26], fail, c, c2; n   } A[N]; n   int siz[B], root[N], top, cnt; n   string str[N]; n   void build(int RT) { n     queue<int> q; n     q.push(RT); n     while (!q.empty()) { n       int x = q.front(), F, k; n       q.pop(); n       for (int i = 0; i < 26; i++) n         if (A[x].ch[i]) { n           F = A[x].fail; n           k = A[x].ch[i]; n           while (F && !A[F].ch[i]) F = A[F].fail; n           if (F) n             A[k].fail = A[F].ch[i]; n           else n             A[k].fail = RT; n           q.push(k); n           A[k].c2 = A[A[k].fail].c2 + A[k].c; n         } n     } n   } n   int merge(int x, int y) { n     if (!x || !y) return x + y; n     A[x].c = A[x].c | A[y].c; n     for (int i = 0; i < 26; i++) A[x].ch[i] = merge(A[x].ch[i], A[y].ch[i]); n     return x; n   } n   void Add(string s) { n     root[++top] = ++cnt; n     int x = root[top], len = s.length(), k; n     for (int i = 0; i < len; i++) { n       k = s[i] -  a ; n       if (!A[x].ch[k]) A[x].ch[k] = ++cnt; n       x = A[x].ch[k]; n     } n     A[x].c++; n     siz[top] = 1; n     build(root[top]); n     while (top && siz[top] == siz[top - 1]) { n       merge(root[top - 1], root[top]); n       build(root[top - 1]); n       siz[top - 1] *= 2; n       top--; n     } n   } n   int Ask(string s) { n     int len = s.length(), sum = 0; n     for (int i = 1; i <= top; i++) { n       int x = root[i], k; n       for (int j = 0; j < len; j++) { n         k = s[j] -  a ; n         while (x && !A[x].ch[k]) x = A[x].fail; n         if (x) n           x = A[x].ch[k]; n         else n           x = root[i]; n         sum += A[x].c2; n       } n     } n     return sum; n   } n } IN, OUT; n int main() { n   scanf( %d , &n); n   int tp; n   while (n--) { n     scanf( %d %s , &tp, t); n     if (tp == 1) n       IN.Add(t); n     else if (tp == 2) n       OUT.Add(t); n     else n       cout << IN.Ask(t) - OUT.Ask(t) <<   n  << flush; n   } n } n


#include <bits/stdc++.h> n using namespace std; n typedef long long ll; n  n inline int read(){ n     int x=0,f=1; n     char ch=getchar(); n     while(ch< 0 ||ch> 9 ){ n         if(ch== - ) n             f=-1; n         ch=getchar(); n     } n     while(ch>= 0 &&ch<= 9 ){ n         x=(x<<1)+(x<<3)+(ch^48); n         ch=getchar(); n     } n     return x*f; n } n const int maxn = 3e4 + 9;  n int t; n int a[3]; n int solve(){ n  int l = a[0], m = a[1], r = a[2]; n  int avg = (l + m + r) / 3; n  //printf( avg: %d n , avg); n  int ans = 0; n  if (l < avg){ n   if (r > avg){ n    if (m > avg){ n     ans += (m - avg) * 2 + r - avg; n     return ans; n    } n    else if (m < avg){ n     ans += avg - l + (avg - m) * 2; n     return ans; n    } n    else{ n     ans += avg - l; n     return ans; n    } n   } n   else if (r < avg){ n    ans += (avg - l) * 2 + avg - r; n    return ans; n   } n   else{ n    ans += (avg - l) * 2; n    return ans; n   } n  } n  else if (l == avg){ n   if (m == avg){ n    return ans; n   } n   else if (m < avg){ n    ans += (avg - m) * 2; n    return ans; n   } n   else{ n    ans += m - avg; n    return ans; n   } n  } n  else{ n   if (m < avg){ n    if (r > avg){ n     ans += (r - avg) * 2 + l - avg; n     return ans; n    } n    else if (r < avg){ n     ans += (avg - r) * 2 + avg - m; n     return ans; n    } n    else{ n     ans += avg - m; n     return ans; n    } n   } n   else if (m > avg){ n    ans += (l - avg) * 2 + m - avg; n    return ans; n   } n   else{ n    ans = (l - avg) * 2; n    return ans; n   } n  } n  return ans; n } n int main(){ n     t = read(); n     while(t--){ n      int n = read(); n      int m; n      a[0] = a[1] = a[2] = 0; n      for (int i = 1; i <= n; i++){ n       m = read(); n       a[m % 3]++; n   } n   //sort(a, a + 3); n   int ans = solve(); n   printf( %d n , ans); n  } n  return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int mod = 998244353, N = 20, M = 1 << 5; n int n, m; n int f[2][N][N][N][N][2][2], (*nxt)[N][N][N][2][2] = f[1], n                             (*cur)[N][N][N][2][2] = *f; n int snx[M], sny[M], spx[M], spy[M], A; n void add(int &x, int y) { n   x += y - mod; n   x += x >> 31 & mod; n } n int main() { n   scanf( %d%d , &n, &m); n   A = (1 << n) - 1; n   int x, y; n   for (int i = 0; i < n; i++) { n     scanf( %d%d , &x, &y); n     if (x < 0) n       snx[1 << i] -= x; n     else n       spx[1 << i] += x; n     if (y < 0) n       sny[1 << i] -= y; n     else n       spy[1 << i] += y; n   } n   for (int i = 1; i <= A; i++) n     spx[i] = spx[i - (i & -i)] + spx[i & -i], n     spy[i] = spy[i - (i & -i)] + spy[i & -i], n     snx[i] = snx[i - (i & -i)] + snx[i & -i], n     sny[i] = sny[i - (i & -i)] + sny[i & -i]; n   ******nxt = 1; n   for (; m; m >>= 1) { n     int c = m & 1, tmp; n     swap(nxt, cur); n     memset(nxt, 0, sizeof *f); n     for (int ppx = 0; ppx <= spx[A]; ppx++) n       for (int ppy = 0; ppy <= spy[A]; ppy++) n         for (int pnx = 0; pnx <= snx[A]; pnx++) n           for (int pny = 0; pny <= sny[A]; pny++) n             for (int ox = 0; ox < 2; ox++) n               for (int oy = 0; oy < 2; oy++) n                 if (tmp = cur[ppx][pnx][ppy][pny][ox][oy]) n                   for (int d = 0; d <= A; d++) { n                     int npx = ppx + spx[d], npy = ppy + spy[d], n                         nnx = pnx + snx[d], nny = pny + sny[d]; n                     if (((npx ^ nnx) | (npy ^ nny)) & 1) continue; n                     add(nxt[npx >> 1][nnx >> 1][npy >> 1][nny >> 1] n                            [(npx ^ c) & 1 ? !c : ox][(npy ^ c) & 1 ? !c : oy], n                         tmp); n                   } n   } n   printf( %d n , ((******nxt) + mod - 1) % mod); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 100010; n char s[N]; n int main() { n   int t; n   scanf( %d , &t); n   while (t--) { n     map<long long, long long> mp; n     long long n; n     scanf( %lld , &n); n     scanf( %s , s); n     long long sum = 0, ans = 0; n     long long x; n     for (int i = 1; i <= n; i++) { n       x = s[i - 1] -  0  - 1; n       sum += x; n       if (sum == 0) ans++; n       ans += mp[sum]; n       mp[sum]++; n     } n     printf( %lld n , ans); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int par[200005], vis[200005], tree[200005]; n set<pair<int, int> > par_change; n void dfs(int u, vector<int> next[], int dfs_par) { n   vis[u] = 1; n   if (par[u] == u) { n     par_change.insert({1, u}); n   } n   for (int i = 0; i < next[u].size(); ++i) { n     int v = next[u][i]; n     if (!vis[v]) { n       dfs(v, next, u); n     } else if (v != dfs_par) { n       if (par[u] == v) { n         par_change.insert({2, u}); n       } else { n         par_change.insert({2, v}); n       } n     } n   } n   vis[u] = 2; n } n int main() { n   int n, cnt = 0; n   scanf( %d , &n); n   vector<int> next[n + 5]; n   for (int i = 1; i <= n; ++i) { n     scanf( %d , &par[i]); n     tree[i] = par[i]; n     if (i != par[i]) { n       next[i].push_back(par[i]); n       next[par[i]].push_back(i); n     } n   } n   for (int i = 1; i <= n; ++i) { n     if (!vis[i]) { n       dfs(i, next, 0); n     } n   } n   int flag = 0; n   tree[par_change.begin()->second] = par_change.begin()->second; n   auto it1 = par_change.begin(), it2 = ++it1; n   it1 = par_change.begin(); n   for (; it2 != par_change.end(); ++it2, ++it1) { n     tree[it2->second] = it1->second; n   } n   for (int i = 1; i <= n; ++i) { n     if (tree[i] != par[i]) { n       ++cnt; n     } n   } n   printf( %d n , cnt); n   for (int i = 1; i <= n; ++i) { n     printf( %d  , tree[i]); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 2e6 + 10; n int cnt[maxn]; n int main() { n   int n; n   scanf( %d , &n); n   for (int i = 0; i < n; i++) { n     int t; n     scanf( %d , &t); n     ++cnt[t]; n   } n   int ans = 0; n   for (int i = 0; i < maxn - 1; i++) { n     cnt[i + 1] += (cnt[i] / 2); n     cnt[i] %= 2; n     ans += cnt[i]; n   } n   printf( %d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void swap(int ar[], int i, int j) { n   int aux = ar[j]; n   ar[j] = ar[i]; n   ar[i] = ar[j]; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   int n, m; n   cin >> n >> m; n   int Ma[n][m]; n   int Mb[n][m]; n   for (int i = 0; i < n; i++) n     for (int k = 0; k < m; k++) cin >> Ma[i][k]; n   for (int i = 0; i < n; i++) n     for (int k = 0; k < m; k++) { n       int aux; n       cin >> aux; n       if (aux <= Ma[i][k]) { n         int aux2 = Ma[i][k]; n         Ma[i][k] = aux; n         Mb[i][k] = aux2; n       } else n         Mb[i][k] = aux; n     } n   for (int i = 0; i < n; i++) n     for (int k = 1; k < m; k++) n       if (Ma[i][k] <= Ma[i][k - 1] || Mb[i][k] <= Mb[i][k - 1]) { n         cout <<  Impossible ; n         return 0; n       } n   for (int k = 0; k < m; k++) n     for (int i = 1; i < n; i++) n       if (Ma[i][k] <= Ma[i - 1][k] || Mb[i][k] <= Mb[i - 1][k]) { n         cout <<  Impossible ; n         return 0; n       } n   cout <<  Possible ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n inline void read(int &x) { n   char c; n   int f = 1; n   while (!isdigit(c = getchar())) n     if (c ==  - ) f = -1; n   x = (c & 15); n   while (isdigit(c = getchar())) x = (x << 1) + (x << 3) + (c & 15); n   x *= f; n } n const int dx[4] = {-1, 0, 1, 0}, dy[4] = {0, -1, 0, 1}; n int _, n, m, i, j, k, a[100005], dis[100015], s = 0, t = 100001, ss = 100002, n                                               tt = 100003, l[100015]; n int dir[100005], res[100005]; n int px[2000005], py[2000005], pdx[2000005], pdy[2000005]; n int id(int x, int y) { return (x - 1) * m + y; } n vector<int> bi[100015]; n struct ii { n   int y, z; n } e[2000005]; n int tot; n void init() { n   int i; n   for ((i) = 1; (i) <= (tot); (i)++) px[i] = py[i] = pdx[i] = pdy[i] = 0; n   tot = 0; n   memset(l, 0, sizeof(l)); n   for ((i) = 0; (i) <= (tt); (i)++) bi[i].clear(); n } n void add(int x, int y, int z) { n   tot++; n   bi[x].push_back(tot + tot - 2); n   bi[y].push_back(tot + tot - 1); n   e[tot + tot - 2] = (ii){y, z}; n   e[tot + tot - 1] = (ii){x, 0}; n } n bool bfs(int s, int t) { n   memset(dis, 0x3f, sizeof(dis)); n   dis[s] = 0; n   queue<int> qx; n   qx.push(s); n   while (!qx.empty()) { n     int x = qx.front(); n     qx.pop(); n     for (__typeof((bi[x]).begin()) it = (bi[x]).begin(); it != (bi[x]).end(); n          ++it) n       if (e[*it].z && dis[e[*it].y] > dis[x] + 1) { n         dis[e[*it].y] = dis[x] + 1; n         qx.push(e[*it].y); n       } n   } n   return dis[t] < dis[100014]; n } n int dfs(int x, int tt, int flw) { n   if (x == tt || !flw) return flw; n   int ans = 0; n   for (__typeof((bi[x]).begin()) it = (bi[x]).begin(); it != (bi[x]).end(); n        ++it) n     if (dis[x] + 1 == dis[e[*it].y] && e[*it].z) { n       int t = dfs(e[*it].y, tt, min(flw, e[*it].z)); n       ans += t; n       flw -= t; n       e[*it].z -= t; n       e[(*it) ^ 1].z += t; n     } n   if (flw) dis[x] = -1; n   return ans; n } n int dinic(int s, int t) { n   int ans = 0; n   while (bfs(s, t)) { n     ans += dfs(s, t, 0x3f3f3f3f); n   } n   return ans; n } n int main() { n   read(_); n   while (_--) { n     read(n); n     read(m); n     for ((i) = 1; (i) <= (n); (i)++) n       for ((j) = 1; (j) <= (m); (j)++) read(a[id(i, j)]); n     init(); n     add(t, s, 0x3f3f3f3f); n     for ((i) = 1; (i) <= (n * m); (i)++) { n       dir[i] = res[i] = 0; n     } n     for ((i) = 1; (i) <= (n); (i)++) n       for ((j) = 1; (j) <= (m); (j)++) { n         bool f = 0; n         for ((k) = 0; (k) < (4); (k)++) { n           int x = i + dx[k], y = j + dy[k]; n           if (x < 1 || x > n || y < 1 || y > m) continue; n           if (a[id(i, j)] == a[id(x, y)] && ((i + j) & 1)) { n             add(id(i, j), id(x, y), 1); n             px[tot] = id(i, j); n             py[tot] = id(x, y); n             pdx[tot] = k; n             pdy[tot] = (k ^ 2); n           } n           if (a[id(i, j)] > a[id(x, y)]) { n             dir[id(i, j)] = k; n             res[id(i, j)] = a[id(i, j)] - a[id(x, y)]; n             f = 1; n           } n         } n         if ((i + j) & 1) { n           if (!f) { n             l[s]--; n             l[id(i, j)]++; n           } else n             add(s, id(i, j), 1); n         } else { n           if (!f) { n             l[id(i, j)]--; n             l[t]++; n           } else n             add(id(i, j), t, 1); n         } n       } n     int sum = 0, ctot = tot; n     for ((i) = 0; (i) <= (t); (i)++) { n       if (l[i] > 0) { n         add(ss, i, l[i]); n         sum += l[i]; n       } n       if (l[i] < 0) { n         add(i, tt, -l[i]); n       } n     } n     int ans = dinic(ss, tt); n     if (ans != sum) { n       puts( NO ); n       continue; n     } n     puts( YES ); n     for ((i) = (2); (i) <= (ctot); (i)++) n       if (e[i + i - 1].z) { n         dir[px[i]] = pdx[i]; n         dir[py[i]] = pdy[i]; n         res[px[i]] = 1; n         res[py[i]] = a[py[i]] - 1; n       } n     for ((i) = 1; (i) <= (n); (i)++) { n       for ((j) = 1; (j) <= (m); (j)++) { n         printf( %d%c , res[id(i, j)], (j == m ?   n  :    )); n       } n     } n     for ((i) = 1; (i) <= (n); (i)++) { n       for ((j) = 1; (j) <= (m); (j)++) { n         printf( %c%c , dir[id(i, j)][ ULDR ], (j == m ?   n  :    )); n       } n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAX = 1e5 + 5; n vector<pair<int, int> > opt[18 * MAX]; n map<pair<int, int>, vector<pair<int, int> > > mp; n int par[MAX]; n int col[MAX]; n int sz[MAX]; n int an[MAX]; n vector<int> roll_back_num; n vector<pair<pair<int, pair<int, int> >, pair<int, pair<int, int> > > > n     roll_back; n void insert(int t, int l, int r, int a, int b, int c, int d) { n   if (l > b || r < a) { n     return; n   } n   if (l >= a && r <= b) { n     opt[t].push_back(make_pair(c, d)); n     return; n   } n   int mid = (l + r) / 2; n   insert(t * 2, l, mid, a, b, c, d); n   insert(t * 2 + 1, mid + 1, r, a, b, c, d); n } n int not_cor; n int find(int x) { n   while (x != par[x]) { n     x = par[x]; n   } n   return x; n } n int color(int x) { n   int ans = 0; n   while (x != par[x]) { n     ans ^= col[x]; n     x = par[x]; n   } n   return (ans ^ col[x]); n } n void unite(int a, int b) { n   int ra = find(a), rb = find(b); n   int ca = color(a), cb = color(b); n   if (ra == rb) { n     if (ca == cb) { n       not_cor++; n       roll_back_num.push_back(-1); n     } n   } else { n     if (sz[ra] > sz[rb]) { n       swap(a, b); n       swap(ra, rb); n       swap(ca, cb); n     } n     roll_back.push_back({{ra, {ra, sz[ra]}}, {rb, {col[ra], sz[rb]}}}); n     par[ra] = rb; n     sz[rb] += sz[ra]; n     if (ca == cb) { n       col[ra] ^= 1; n     } n   } n } n void solve(int t, int l, int r) { n   int curn = roll_back_num.size(); n   int cur = roll_back.size(); n   for (auto it : opt[t]) { n     unite(it.first, it.second); n   } n   if (l == r) { n     if (not_cor) { n       an[l] = 0; n     } else { n       an[l] = 1; n     } n   } else { n     int mid = (l + r) / 2; n     solve(2 * t, l, mid); n     solve(2 * t + 1, mid + 1, r); n   } n   while (roll_back_num.size() > curn) { n     not_cor += roll_back_num.back(); n     roll_back_num.pop_back(); n   } n   while (roll_back.size() > cur) { n     pair<pair<int, pair<int, int> >, pair<int, pair<int, int> > > tmp = n         roll_back.back(); n     par[tmp.first.first] = tmp.first.second.first; n     sz[tmp.first.first] = tmp.first.second.second; n     col[tmp.first.first] = tmp.second.second.first; n     sz[tmp.second.first] = tmp.second.second.second; n     roll_back.pop_back(); n   } n } n int main() { n   int n, q; n   scanf( %d %d , &n, &q); n   for (int i = 1; i <= q; i++) { n     int a, b; n     scanf( %d %d , &a, &b); n     if (a > b) { n       swap(a, b); n     } n     vector<pair<int, int> >& tmp = mp[make_pair(a, b)]; n     if (tmp.empty() || (((tmp.back()).second) != -1)) { n       tmp.push_back(make_pair(i, -1)); n     } else { n       (tmp.back()).second = (i - 1); n     } n   } n   for (auto it : mp) { n     for (auto it1 : it.second) { n       if (it1.second == -1) { n         it1.second = q; n       } n       insert(1, 1, q, it1.first, it1.second, it.first.first, it.first.second); n     } n   } n   for (int i = 1; i <= n; i++) { n     sz[i] = 1; n     par[i] = i; n   } n   solve(1, 1, q); n   for (int i = 1; i <= q; i++) { n     if (an[i] == 0) { n       printf( NO n ); n     } else { n       printf( YES n ); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int mxn = 100006; n const int mxm = 100006 * 2; n int n, m; n int fe[mxn] = {}; n int ne[mxm]; n int ev[mxm]; n bool v[mxm]; n int el = 1; n void add1(int L, int R) { n   ++el; n   ne[el] = fe[L]; n   fe[L] = el; n   ev[el] = R; n } n bool vis[mxn]; n int dfs(int x) { n   if (vis[x]) return 0; n   vis[x] = true; n   int last = 0; n   for (int i = fe[x]; i; i = ne[i]) n     if (!v[i]) { n       v[i] = v[i ^ 1] = true; n       int t = dfs(ev[i]); n       if (t) { n         printf( %d %d %d n , x, ev[i], t); n       } else { n         if (last) { n           printf( %d %d %d n , last, x, ev[i]); n           last = 0; n         } else { n           last = ev[i]; n         } n       } n     } n   return last; n } n void EXEC() { n   cin >> n >> m; n   if (m % 2 == 1) { n     cout <<  No solution  << endl; n     return; n   } n   for (int i = 0; i < m; ++i) { n     int L, R; n     scanf( %d%d , &L, &R); n     add1(L, R); n     add1(R, L); n   } n   dfs(1); n } n int main() { n   EXEC(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct people { n   long long a, b, id; n } p[300010]; n long long n, s[300010], t; n bool v[300010], ans[300010]; n bool cmp(people a, people b) { return a.a == b.a ? a.b > b.b : a.a > b.a; } n long double check(people a, people b) { n   return (long double)a.a * b.a * (b.b - a.b) / (b.a - a.a) / b.b / a.b; n } n int main() { n   scanf( %lld , &n); n   for (int i = 1; i <= n; i++) scanf( %lld%lld , &p[i].a, &p[i].b), p[i].id = i; n   sort(p + 1, p + n + 1, cmp); n   s[1] = 1, t = 1; n   long long maxb = p[1].b; n   for (int i = 2; i <= n; i++) n     if (p[i].b <= maxb) n       v[i] = 1; n     else n       maxb = p[i].b; n   for (int i = 2; i <= n; i++) { n     if (v[i] || check(p[i], p[s[t]]) > 0) continue; n     while (t > 1 && check(p[i], p[s[t]]) < check(p[s[t - 1]], p[s[t]])) t--; n     s[++t] = i; n   } n   for (int i = 1; i <= t; i++) { n     ans[p[s[i]].id] = 1; n     for (int j = s[i] + 1; j <= n && p[s[i]].a == p[j].a && p[s[i]].b == p[j].b; n          j++) n       ans[p[j].id] = 1; n   } n   for (int i = 1; i <= n; i++) n     if (ans[i]) printf( %d  , i); n   return 0; n } n


#include <bits/stdc++.h> n /** #include <ext/pb_ds/assoc_container.hpp> n  * #include <ext/pb_ds/tree_policy.hpp> n  * using namespace __gnu_pbds; */ n using namespace std; n  n #define ll long long n #define ull unsigned long long n #define f(a, b) for (ll i = a; i < b; i++) n #define mod 1000000007 n #define pb push_back n #define vll vector<ll> n #define pll vector<pair<ll, ll>> n #define ld long double n #define fr(a, b) for (ll j = a; j >= b; j--) n #define fi(a, b) for (ll j = a; j < b; j++) n #define fii(a, b) for (ll k = a; k < b; k++) n /** typedef tree<ll n  * ,null_type,less<ll>,rb_tree_tag,tree_order_statistics_node_update> n  * ordered_set; */ n  n template <class T> ostream &operator<<(ostream &os, vector<T> V) { n   os <<  [  ; n   for (auto v : V) n     os << v <<    ; n   os <<  ] ; n   return os; n } n template <class T> ostream &operator<<(ostream &os, set<T> S) { n   os <<  {  ; n   for (auto s : S) n     os << s <<    ; n   return os <<  } ; n } n template <class L, class R> ostream &operator<<(ostream &os, map<L, R> M) { n   os <<  {  ; n   for (auto m : M) n     os <<  (  << m.first <<  :  << m.second <<  )  ; n   return os <<  } ; n } n template <class L, class R> ostream &operator<<(ostream &os, pair<L, R> P) { n   return os <<  (  << P.first <<  ,  << P.second <<  ) ; n } n  n const ll N = 200005; n vll adj[N]; n ll n, color[N], parent[N], cycle_end, cycle_start, ans; n bool C[N]; n  n bool dfs(ll v, ll par) { n   color[v] = 1; n   for (ll u : adj[v]) { n     if (u == par) n       continue; n     if (color[u] == 0) { n       parent[u] = v; n       if (dfs(u, parent[u])) n         return true; n     } else if (color[u] == 1) { n       cycle_end = v; n       cycle_start = u; n       return true; n     } n   } n   color[v] = 2; n   return false; n } n  n ll DFS(ll x, ll pr) { n   ll k = 1; n   f(0, adj[x].size()) { n     if (adj[x][i] != pr) n       k += DFS(adj[x][i], x); n   } n   return k; n } n  n void solve() { n   cycle_start = -1; n   for (int v = 1; v <= n; v++) { n     if (color[v] == 0 && dfs(v, parent[v])) n       break; n   } n   vector<int> cycle; n   C[cycle_start] = 1; n   cycle.push_back(cycle_start); n   for (int v = cycle_end; v != cycle_start; v = parent[v]) n     cycle.push_back(v), C[v] = 1; n   vll V; n   f(0, cycle.size()) { n     ll v = cycle[i]; n     ll tot = 0; n     fi(0, adj[v].size()) { n       if (!C[adj[v][j]]) { n         tot += DFS(adj[v][j], v); n       } n     } n     V.pb(tot); n   } n   ll l = cycle.size(); n   ans += l * (l - 1); n   f(0, V.size()) { n     ll k = V[i]; n     ans += k * (l - 1) * 2 + k; n     ans += k * (k - 1) / 2; n   } n   ll sum = 0; n   f(0, V.size()) sum += V[i]; n   f(0, V.size()) { n     sum -= V[i]; n     ans += V[i] * sum * 2; n   } n } n  n int main() { n #ifndef ONLINE_JUDGE n   freopen( input.txt ,  rt , stdin); n   freopen( output.txt ,  wt , stdout); n #endif n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n  n   ll t; n   cin >> t; n   while (t--) { n     cin >> n; n     ans = 0; n     f(1, n + 1) adj[i].clear(), color[i] = 0, parent[i] = -1, C[i] = false; n     f(0, n) { n       ll x, y; n       cin >> x >> y; n       adj[x].pb(y); n       adj[y].pb(x); n     } n     solve(); n     cout << ans <<   n ; n   } n  n #ifndef ONLINE_JUDGE n   cout <<   nTime Elapsed :   << 1000 * (ld)clock() / (ld)CLOCKS_PER_SEC n        <<   ms n ; n #endif n   return 0; n }


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   cin >> n; n   cout << n / 2 * 2 + (n + 1) / 2 << endl; n   for (int i = 2; i <= n; i += 2) cout << i <<    ; n   for (int i = 1; i <= n; i += 2) cout << i <<    ; n   for (int i = 2; i <= n; i += 2) cout << i <<    ; n } n


#include <bits/stdc++.h> n using namespace std; n char s1[128]; n int l1; n char s2[128]; n int l2; n char virus[128]; n int lv; n int nxt[128][128]; n vector<int> froms[128][128]; n void calc_nxt() { n   char tmp[128]; n   strcpy(tmp, virus); n   for (int l = 0; l < lv; l++) { n     for (char ch =  A ; ch <=  Z ; ch++) { n       tmp[l] = ch; n       for (int i = l + 1; i >= 0; i--) { n         if (!strncmp(tmp + l + 1 - i, virus, i)) { n           nxt[l][(int)ch] = i; n           break; n         } n       } n       froms[nxt[l][(int)ch]][(int)ch].push_back(l); n     } n     tmp[l] = virus[l]; n   } n } n struct Path { n   int i, j, k; n }; n bool operator<(const Path &a, const Path &b) { n   if (a.i != b.i) return a.i < b.i; n   if (a.j != b.j) return a.j < b.j; n   if (a.k != b.k) return a.k < b.k; n   return false; n } n pair<int, Path> dp[128][128][128]; n pair<int, Path> takeDP(int i, int j, int k) { n   pair<int, Path> t = dp[i][j][k]; n   t.second = {i, j, k}; n   return t; n } n string getPath(pair<int, Path> runner) { n   string s; n   while (runner.first != 0) { n     Path p = runner.second; n     auto nr = dp[p.i][p.j][p.k]; n     Path np = nr.second; n     if (np.i < p.i && np.j < p.j) { n       s += s1[p.i - 1]; n     } n     runner = nr; n   } n   reverse(s.begin(), s.end()); n   if (s ==   ) return  0 ; n   return s; n } n bool isSub(string s, string p) { n   size_t l = 0; n   for (auto ch : p) { n     cout << ch; n     while (l < s.size() && s[l] != ch) { n       l++; n     } n     if (l == s.size()) { n       cout << endl; n       return false; n     } n     l++; n   } n   cout << endl; n   return true; n } n int TestNum; n int main() { n   scanf( %s%s%s , s1, s2, virus); n   l1 = strlen(s1); n   l2 = strlen(s2); n   lv = strlen(virus); n   calc_nxt(); n   for (int i = 0; i < l1; i++) { n     for (int k = 1; k <= lv; k++) { n       dp[i][0][k].first = -0x1f1f1f1f; n     } n   } n   for (int i = 0; i < l2; i++) { n     for (int k = 1; k <= lv; k++) { n       dp[0][i][k].first = -0x1f1f1f1f; n     } n   } n   dp[0][0][0].first = 0; n   for (int i = 0; i < l1; i++) { n     for (int j = 0; j < l2; j++) { n       for (int k = 0; k < lv; k++) { n         auto &to = dp[i + 1][j + 1][k]; n         auto from1 = takeDP(i + 1, j, k), from2 = takeDP(i, j + 1, k); n         to = max(from1, from2); n         if (s1[i] == s2[j]) { n           for (auto tk : froms[k][(int)s1[i]]) { n             auto from = takeDP(i, j, tk); n             from.first += 1; n             to = max(to, from); n           } n         } n       } n     } n   } n   auto best = dp[0][0][0]; n   for (int k = 0; k < lv; k++) { n     best = max(best, takeDP(l1, l2, k)); n   } n   string s = getPath(best); n   cout << s << endl; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 3001; n const int mod = 1000000007; n char s[N][N]; n int f[N][N]; n int calc(int sr, int sc, int tr, int tc) { n   if (tr < sr || tc < sc) return 0; n   int n = tr - sr + 1, m = tc - sc + 1; n   f[0][0] = (s[sr][sc] !=  # ); n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < m; j++) n       if (i || j) { n         f[i][j] = 0; n         if (i > 0) f[i][j] += f[i - 1][j]; n         if (j > 0) f[i][j] += f[i][j - 1]; n         f[i][j] %= mod; n         if (s[i + sr][j + sc] ==  # ) f[i][j] = 0; n       } n   } n   return f[n - 1][m - 1]; n } n int main() { n   int n, m; n   scanf( %d%d , &n, &m); n   for (int i = 0; i < n; i++) scanf( %s , s[i]); n   int a, b, c, d; n   a = calc(0, 1, n - 2, m - 1); n   b = calc(1, 0, n - 1, m - 2); n   c = calc(0, 1, n - 1, m - 2); n   d = calc(1, 0, n - 2, m - 1); n   printf( %lld n , ((1LL * a * b - 1LL * c * d) % mod + mod) % mod); n   return 0; n } n


#include <bits/stdc++.h> n template <typename T> n T inline SQR(const T &a) { n   return a * a; n } n template <typename T> n T inline ABS(const T &a) { n   return a < 0 ? -a : a; n } n template <typename T> n T inline MIN(const T &a, const T &b) { n   if (a < b) return a; n   return b; n } n template <typename T> n T inline MAX(const T &a, const T &b) { n   if (a > b) return a; n   return b; n } n const double EPS = 1e-9; n inline int sgn(double a) { return a > EPS ? 1 : (a < -EPS ? -1 : 0); } n inline int cmp(double a, double b) { return sgn(a - b); } n using namespace std; n struct orientacao_t { n   char f, c; n   char v[4]; n   orientacao_t() {} n   orientacao_t(char frente, char costas, char vizinhos[]) n       : f(frente), c(costas) { n     for (int i = 0; i < 4; i++) v[i] = vizinhos[i]; n   } n   bool operator==(const orientacao_t &o) const { n     if (f == o.f && c == o.c) { n       if (v[0] == o.v[0] && v[1] == o.v[1] && v[2] == o.v[2] && v[3] == o.v[3]) n         return true; n       if (v[0] == o.v[1] && v[1] == o.v[2] && v[2] == o.v[3] && v[3] == o.v[0]) n         return true; n       if (v[0] == o.v[2] && v[1] == o.v[3] && v[2] == o.v[0] && v[3] == o.v[1]) n         return true; n       if (v[0] == o.v[3] && v[1] == o.v[0] && v[2] == o.v[1] && v[3] == o.v[2]) n         return true; n     } n     if (f == o.c && c == o.f) { n       if (v[2] == o.v[0] && v[1] == o.v[1] && v[0] == o.v[2] && v[3] == o.v[3]) n         return true; n       if (v[2] == o.v[1] && v[1] == o.v[2] && v[0] == o.v[3] && v[3] == o.v[0]) n         return true; n       if (v[2] == o.v[2] && v[1] == o.v[3] && v[0] == o.v[0] && v[3] == o.v[1]) n         return true; n       if (v[2] == o.v[3] && v[1] == o.v[0] && v[0] == o.v[1] && v[3] == o.v[2]) n         return true; n     } n     return false; n   } n   void operator=(const orientacao_t &o) { n     f = o.f; n     c = o.c; n     for (int i = 0; i < 4; i++) v[i] = o.v[i]; n   } n }; n struct cubo_t { n   orientacao_t orientacao[3]; n   cubo_t() {} n   cubo_t(char a, char b, char c, char d, char e, char f) { n     char v[4]; n     v[0] = e; n     v[1] = d; n     v[2] = c; n     v[3] = b; n     orientacao[0] = orientacao_t(a, f, v); n     v[0] = e; n     v[1] = a; n     v[2] = c; n     v[3] = f; n     orientacao[1] = orientacao_t(b, d, v); n     v[0] = d; n     v[1] = a; n     v[2] = b; n     v[3] = f; n     orientacao[2] = orientacao_t(e, c, v); n   } n   bool operator==(const cubo_t &c) const { n     for (int i = 0; i < 3; i++) n       for (int j = 0; j < 3; j++) n         if (orientacao[i] == c.orientacao[j]) return true; n     return false; n   } n   void operator=(const cubo_t &c) { n     orientacao[0] = c.orientacao[0]; n     orientacao[1] = c.orientacao[1]; n     orientacao[2] = c.orientacao[2]; n   } n }; n int main(int argc, char *argv[]) { n   ios::sync_with_stdio(false); n   string s; n   cin >> s; n   sort(s.begin(), s.end()); n   vector<cubo_t> cubes; n   int eq = 0; n   do { n     cubo_t aux = cubo_t(s[0], s[1], s[2], s[3], s[4], s[5]); n     for (int i = 0; i < int((cubes).size()); ++i) n       if (aux == cubes[i]) goto hell; n     cubes.push_back(aux); n   hell: n     eq++; n   } while (next_permutation(s.begin(), s.end())); n   cout << int((cubes).size()) <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<int> adj[100005]; n struct Tree { n   vector<vector<int> > anc; n   vector<vector<int> > cost; n   vector<int> T, X; n   vector<int> depth; n   vector<bool> vis; n   int n; n   int root; n   Tree(int N = 1e5, int r = 1) { n     n = N + 5; n     root = r; n     anc = vector<vector<int> >(18, vector<int>(n, 0)); n     depth = vector<int>(n, 0); n     vis = vector<bool>(n, false); n     X = T = vector<int>(n, 0); n     init(root, -1); n     for (int d = 1; d < 18; d++) { n       for (int x = 0; x < n; x++) { n         anc[d][x] = anc[d - 1][anc[d - 1][x]]; n       } n     } n   } n   void init(int x, int pre) { n     static int t = 0; n     T[x] = t++; n     for (int i = 0; i < adj[x].size(); i++) { n       int y = adj[x][i]; n       if (y == pre) continue; n       anc[0][y] = x; n       depth[y] = depth[x] + 1; n       init(y, x); n     } n     X[x] = t++; n   } n   int LCA(int x, int y) { n     if (depth[x] < depth[y]) swap(x, y); n     for (int w = (1 << 17), d = 17; d >= 0; w /= 2, d--) { n       if (depth[x] - depth[y] >= w) { n         x = anc[d][x]; n       } n     } n     if (x == y) return x; n     for (int i = 17; i >= 0; i--) { n       if (anc[i][x] != anc[i][y]) { n         x = anc[i][x]; n         y = anc[i][y]; n       } n     } n     return anc[0][x]; n   } n   int up(int x, int k) { n     for (int w = (1 << 17), d = 17; d >= 0; w /= 2, d--) { n       if (k >= w) { n         x = anc[d][x]; n         k -= w; n       } n     } n     return x; n   } n   int dist(int x, int y) { n     int l = LCA(x, y); n     return depth[x] + depth[y] - 2 * depth[l]; n   } n   bool isparent(int x, int y) { return LCA(x, y) == x; } n } A; n vector<int> aux[100005]; n vector<int> event(100005); n vector<int> val(100005), resv; n vector<bool> out(100005, false), tmp(100005, false); n int n, m, sq, s; n int ans = 0, lans = 0; n vector<int> temp; n vector<int> anc(100005, 0); n int root = 1; n vector<pair<int, int> > edge[100005]; n vector<int> lazy(100005, 0); n vector<pair<int, int> > ptr(100005, {0, 0}); n void compress(int b) { n   for (auto x : temp) { n     aux[x].clear(); n     lazy[x] = 0; n     ptr[x] = {0, 0}; n     edge[x].clear(); n     anc[x] = 0; n     tmp[x] = false; n   } n   temp.clear(); n   temp.push_back(1); n   for (int i = b * sq; i < (b + 1) * sq && i < m; i++) { n     temp.push_back(abs(event[i])); n   } n   sort((temp).begin(), (temp).end(), n        [&](int x, int y) { return A.T[x] < A.T[y]; }); n   int ss = ((int)(temp).size()); n   for (int i = 1; i < ss; i++) { n     temp.push_back(A.LCA(temp[i - 1], temp[i])); n   } n   sort((temp).begin(), (temp).end(), n        [&](int x, int y) { return A.T[x] < A.T[y]; }); n   for (auto x : temp) tmp[x] = true; n   stack<int> st; n   root = 1; n   for (auto x : temp) { n     while (!st.empty() && A.X[st.top()] < A.T[x]) st.pop(); n     if (!st.empty() && st.top() != x) { n       aux[x].push_back(st.top()); n       aux[st.top()].push_back(x); n       anc[x] = st.top(); n       int l = A.anc[0][x]; n       map<int, int> cnt; n       while (l != st.top()) { n         if (!out[l]) cnt[val[l]]++; n         l = A.anc[0][l]; n       } n       for (auto itr : cnt) edge[x].push_back({itr.first, itr.second}); n       for (int i = 0; i < ((int)(edge[x]).size()); i++) { n         if (edge[x][i].first < 0) n           ptr[x] = {ptr[x].first + 1, ptr[x].second + edge[x][i].second}; n         else n           break; n       } n     } n     st.push(x); n   } n } n int currTree(int x, int pre) { n   int v = 0; n   if (out[x]) v--; n   for (auto y : adj[x]) { n     if (y == pre) continue; n     v += currTree(y, x); n   } n   val[x] += v; n   return v; n } n bool stat(int x, int pre) { n   bool down = false; n   if (tmp[x]) down = true; n   for (auto y : adj[x]) { n     if (y == pre) continue; n     down |= stat(y, x); n   } n   if (!down) { n     if (!out[x] && val[x] < 0) lans++; n   } n   return down; n } n void update(int x, int v) {} n int32_t main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   cin >> n >> m; n   for (int i = 2; i <= n; i++) { n     int p; n     cin >> p; n     adj[p].push_back(i); n     adj[i].push_back(p); n   } n   A = Tree(); n   for (int i = 1; i <= n; i++) cin >> val[i]; n   resv = val; n   for (int i = 0; i < m; i++) cin >> event[i]; n   sq = 460; n   s = (m - 1) / sq + 1; n   for (int b = 0; b < s; b++) { n     compress(b); n     lans = 0; n     stat(1, 0); n     for (int i = b * sq; i < (b + 1) * sq && i < m; i++) { n       if (event[i] < 0) n         out[-event[i]] = false; n       else n         out[event[i]] = true; n       int p = abs(event[i]); n       int v = abs(event[i]) / event[i]; n       while (p != 0) { n         lazy[p] += v; n         val[p] -= v; n         while (ptr[p].first > 0 && edge[p][ptr[p].first - 1].first >= lazy[p]) n           ptr[p] = {ptr[p].first - 1, n                     ptr[p].second - edge[p][ptr[p].first - 1].second}; n         while (ptr[p].first < ((int)(edge[p]).size()) && n                edge[p][ptr[p].first].first < lazy[p]) n           ptr[p] = {ptr[p].first + 1, n                     ptr[p].second + edge[p][ptr[p].first].second}; n         p = anc[p]; n       } n       ans = 0; n       int l = -1; n       for (auto x : temp) { n         if (x == l) continue; n         ans += ptr[x].second; n         if (!out[x] && val[x] < 0) ans++; n         l = x; n       } n       cout << lans + ans <<    ; n     } n     val = resv; n     currTree(1, 0); n   } n   cerr << 1.0 * clock() / CLOCKS_PER_SEC << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int read() { n   int q = 0; n   char ch =    ; n   while (ch <  0  || ch >  9 ) ch = getchar(); n   while (ch >=  0  && ch <=  9 ) q = q * 10 + ch -  0 , ch = getchar(); n   return q; n } n const int N = 200005, inf = 0x3f3f3f3f; n int n, m, Q, SZ, st_top, tim; n int w[N], dfn[N], low[N], st[N]; n multiset<int> S[N]; n struct graph { n   int tot, h[N], ne[N], to[N]; n   void add(int x, int y) { to[++tot] = y, ne[tot] = h[x], h[x] = tot; } n } G, T; n void tarjan(int x, int las) { n   dfn[x] = low[x] = ++tim, st[++st_top] = x; n   for (register int i = G.h[x]; i; i = G.ne[i]) { n     if (G.to[i] == las) continue; n     int y = G.to[i]; n     if (!dfn[y]) { n       tarjan(y, x), low[x] = min(low[x], low[y]); n       if (low[y] == dfn[x]) { n         ++SZ, w[SZ] = inf, T.add(x, SZ); n         while (st[st_top] != y) { n           T.add(SZ, st[st_top]), S[SZ].insert(w[st[st_top]]); n           w[SZ] = min(w[SZ], w[st[st_top]]), --st_top; n         } n         T.add(SZ, y), S[SZ].insert(w[y]), w[SZ] = min(w[SZ], w[y]), --st_top; n       } else if (low[y] > dfn[x]) n         ++SZ, w[SZ] = w[y], S[SZ].insert(w[y]), T.add(x, SZ), T.add(SZ, y); n     } else n       low[x] = min(low[x], dfn[y]); n   } n   if (dfn[x] == low[x]) --st_top; n } n int sz[N], fa[N], top[N], pos[N], repos[N], dep[N], mi[N << 2]; n void dfs1(int x) { n   sz[x] = 1, dep[x] = dep[fa[x]] + 1; n   for (register int i = T.h[x]; i; i = T.ne[i]) n     fa[T.to[i]] = x, dfs1(T.to[i]), sz[x] += sz[T.to[i]]; n } n void dfs2(int x) { n   int bj = 0, mx = 0; n   pos[x] = ++tim, repos[tim] = x; n   for (register int i = T.h[x]; i; i = T.ne[i]) n     if (sz[T.to[i]] > mx) mx = sz[T.to[i]], bj = T.to[i]; n   if (bj) top[bj] = top[x], dfs2(bj); n   for (register int i = T.h[x]; i; i = T.ne[i]) n     if (T.to[i] != bj) top[T.to[i]] = T.to[i], dfs2(T.to[i]); n } n void build(int s, int t, int i) { n   if (s == t) { n     mi[i] = w[repos[s]]; n     return; n   } n   int mid = (s + t) >> 1; n   build(s, mid, i << 1), build(mid + 1, t, (i << 1) | 1); n   mi[i] = min(mi[i << 1], mi[(i << 1) | 1]); n } n void chan(int x, int s, int t, int i, int v) { n   if (s == t) { n     mi[i] = v; n     return; n   } n   int mid = (s + t) >> 1; n   if (x <= mid) chan(x, s, mid, i << 1, v); n   if (mid + 1 <= x) chan(x, mid + 1, t, (i << 1) | 1, v); n   mi[i] = min(mi[i << 1], mi[(i << 1) | 1]); n } n int query(int l, int r, int s, int t, int i) { n   if (l <= s && t <= r) return mi[i]; n   int mid = (s + t) >> 1, re = inf; n   if (l <= mid) re = query(l, r, s, mid, i << 1); n   if (mid + 1 <= r) re = min(re, query(l, r, mid + 1, t, (i << 1) | 1)); n   return re; n } n int getans(int x, int y) { n   int re = inf; n   while (top[x] != top[y]) { n     if (dep[top[x]] < dep[top[y]]) swap(x, y); n     re = min(re, query(pos[top[x]], pos[x], 1, SZ, 1)); n     x = fa[top[x]]; n   } n   if (pos[x] > pos[y]) swap(x, y); n   re = min(re, query(pos[x], pos[y], 1, SZ, 1)); n   if (x > n) re = min(re, query(pos[fa[x]], pos[fa[x]], 1, SZ, 1)); n   return re; n } n int main() { n   int x, y; n   char ch[10]; n   n = read(), m = read(), Q = read(); n   for (register int i = 1; i <= n; ++i) w[i] = read(); n   for (register int i = 1; i <= m; ++i) n     x = read(), y = read(), G.add(x, y), G.add(y, x); n   SZ = n, tarjan(1, 0); n   dfs1(1), tim = 0, top[1] = 1, dfs2(1), build(1, SZ, 1); n   while (Q--) { n     scanf( %s , ch), x = read(), y = read(); n     if (ch[0] ==  C ) { n       chan(pos[x], 1, SZ, 1, y); n       if (x != 1) { n         S[fa[x]].erase(S[fa[x]].find(w[x])); n         S[fa[x]].insert(y); n         int kw = *(S[fa[x]].begin()); n         if (kw != w[fa[x]]) chan(pos[fa[x]], 1, SZ, 1, kw), w[fa[x]] = kw; n       } n       w[x] = y; n     } else n       printf( %d n , getans(x, y)); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long max(long long a, long long b) { n   if (a > b) return a; n   return b; n } n long long min(long long a, long long b) { n   if (a > b) return b; n   return a; n } n int main() { n   { n     ios_base::sync_with_stdio(false); n     cin.tie(NULL); n   }; n   int q; n   cin >> q; n   while (q--) { n     long long a, b, c, r; n     cin >> a >> b >> c >> r; n     long long ans = 0; n     if (b < a) { n       long long temp = b; n       b = a; n       a = temp; n     } n     long long low = c - r; n     long long high = c + r; n     if (a <= low && high <= b) { n       ans = b - a - (high - low); n     } else if (low <= a && high <= b && a <= high) { n       ans = b - a - (high - a); n     } else if (b <= high && a <= low && low <= b) { n       ans = b - a - (b - low); n     } else if (low <= a && b <= high) { n       ans = 0; n     } else { n       ans = b - a; n     } n     cout << ans <<   n ; n   } n   return 0; n } n


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


#include <bits/stdc++.h> n using namespace std; n std::vector<string> v(2000); n vector<vector<int> > mark(2000); n set<pair<int, int> > ans; n int bfs(int x, int y) { n   int m = x, n = y; n   vector<pair<int, int> > q; n   int i = 0; n   while (v[x][y] !=  #  && mark[x][y] == -2) { n     q.push_back(make_pair(x, y)); n     int Y = y, X = x; n     Y -= (v[x][y] ==  < ); n     Y += (v[x][y] ==  > ); n     X -= (v[x][y] ==  ^ ); n     X += (v[x][y] ==  v ); n     mark[x][y] = -1; n     x = X; n     y = Y; n   } n   if (v[x][y] ==  # ) { n     mark[x][y] = 0; n     int cnt = 1; n     for (i = q.size() - 1; i >= 0; i--) { n       mark[q[i].first][q[i].second] = cnt + mark[x][y]; n       cnt++; n     } n   } else if (mark[x][y] == -1) { n     for (i = 0; i < q.size(); i++) { n       mark[q[i].first][q[i].second] = -1; n     } n   } else { n     int cnt = 1; n     for (i = q.size() - 1; i >= 0; i--) { n       mark[q[i].first][q[i].second] = cnt + mark[x][y]; n       cnt++; n     } n   } n   return mark[m][n]; n } n void check() { n   while (ans.size() != 1) { n     vector<pair<int, int> > u; n     set<pair<int, int> >::iterator it; n     for (it = ans.begin(); it != ans.end(); it++) { n       int x = (*it).first; n       int y = (*it).second; n       int X = x, Y = y; n       Y -= (v[x][y] ==  < ); n       Y += (v[x][y] ==  > ); n       X -= (v[x][y] ==  ^ ); n       X += (v[x][y] ==  v ); n       if (v[X][Y] ==  # ) { n         return; n       } n       u.push_back(make_pair(X, Y)); n     } n     int i; n     ans.clear(); n     for (i = 0; i < u.size(); i++) { n       ans.insert(u[i]); n     } n   } n } n int main() { n   int n, m; n   cin >> n >> m; n   int i, j; n   for (i = 0; i < n; i++) { n     cin >> v[i]; n   } n   for (i = 0; i < n; i++) { n     for (j = 0; j < m; j++) { n       mark[i].push_back(-2); n     } n   } n   int maxi = -100; n   for (i = 0; i < n; i++) { n     for (j = 0; j < m; j++) { n       if (mark[i][j] == -2) { n         int a = bfs(i, j); n         if (a == -1) { n           cout <<  -1  << endl; n           return 0; n         } n         if (maxi < a) { n           ans.clear(); n           maxi = a; n           ans.insert(make_pair(i, j)); n         } else if (maxi == a) { n           ans.insert(make_pair(i, j)); n         } n       } n     } n   } n   mark.clear(); n   check(); n   if (ans.size() == 1) { n     if (v[(*(ans.begin())).first][(*(ans.begin())).second] ==  # ) n       cout <<  0  << endl; n     else n       cout << 2 * maxi - 1 << endl; n   } else { n     cout << 2 * maxi << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long add(long long n) { n   long long mi = INT_MAX, ma = INT_MIN; n   while (n > 0) { n     mi = min(n % 10, mi); n     ma = max(n % 10, ma); n     n = n / 10; n   } n   return mi * ma; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   int t; n   scanf( %d , &t); n   while (t--) { n     long long n, k; n     scanf( %lld%lld , &n, &k); n     k--; n     while (k--) { n       long long j = add(n); n       n = n + j; n       if (j == 0) { n         break; n       } n     } n     cout << n << endl; n   } 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   cout.tie(NULL); n   int n, m, k, cntr = 1, s = 1; n   cin >> n >> m >> k; n   string str; n   cin >> str >> str; n   if (str ==  head ) cntr = -1; n   cin >> str; n   int l = str.length(); n   if (m < k) s = -1; n   for (int i = 0; i < l; i++) { n     if (str[i] ==  0 ) { n       if (m != 1 && m != n) m += s; n     } else { n       if (cntr == 1) n         m = 1; n       else n         m = n; n     } n     k += cntr; n     if (k == n) n       cntr = -1; n     else if (k == 1) n       cntr = 1; n     if (m == k) { n       cout <<  Controller   << i + 1 <<   n ; n       return 0; n     } n   } n   cout <<  Stowaway n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(false); n   long long sum, xr; n   cin >> sum >> xr; n   long long dp[2] = {1, 0}; n   for (int bit = 0; bit < 50; bit++) { n     long long ndp[2] = {0, 0}; n     int needSum = (sum >> bit) & 1; n     int needXor = (xr >> bit) & 1; n     for (int was = 0; was < 2; was++) { n       for (int b1 = 0; b1 < 2; b1++) { n         for (int b2 = 0; b2 < 2; b2++) { n           int mySum = (b1 + b2 + was) % 2; n           int myXor = b1 ^ b2; n           if (myXor == needXor && mySum == needSum) { n             ndp[(b1 + b2 + was) >> 1] += dp[was]; n           } n         } n       } n     } n     for (int _n(2), i(0); i < _n; i++) dp[i] = ndp[i]; n   } n   cout << (dp[0] - (sum == xr ? 2 : 0)) << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n inline void splay(long long &v) { n   v = 0; n   char c = 0; n   int p = 1; n   while (c <  0  || c >  9 ) { n     if (c ==  - ) p = -1; n     c = getchar(); n   } n   while (c >=  0  && c <=  9 ) { n     v = (v << 3) + (v << 1) + c -  0 ; n     c = getchar(); n   } n   v *= p; n } n long long a[1000010]; n long long dp[1000010], ans; n int main() { n   long long n; n   splay(n); n   for (int i = 1; i <= n; i++) { n     splay(a[i]); n     a[i]--; n   } n   dp[1] = min(a[1], a[2]); n   ans += a[1]; n   for (int i = 2; i <= n; i++) { n     ans += a[i]; n     long long x = min(a[i], a[i - 1]); n     long long y = min(x, a[i + 1]), z = min(a[i], a[i + 1]); n     ans += dp[i - 1] * x; n     dp[i] = dp[i - 1] * y + z; n     ans %= 1000000007LL; n     dp[i] %= 1000000007LL; n   } n   cout << ans << endl; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 2000010; n int main() { n   int len, cnt; n   char s, d, st[maxn], word[26]; n   cin >> len >> cnt; n   cin >> st; n   for (int i = 0; i < 26; ++i) { n     word[i] =  a  + i; n   } n   for (int i = 1; i <= cnt; ++i) { n     cin >> s >> d; n     if (s == d) continue; n     for (int j = 0; j < 26; ++j) { n       if (word[j] == s) n         word[j] = d; n       else if (word[j] == d) n         word[j] = s; n     } n   } n   for (int i = 0; i < len; ++i) cout << word[st[i] -  a ]; n   cout << endl; n   return 0; n } n


#include <bits/stdc++.h> n const int N = 100010; n using ll = long long; n int a[N]; n int main() { n   int n, d, m; n   scanf( %d%d%d , &n, &d, &m); n   for (int i = 0; i < n; ++i) { n     scanf( %d , &a[i]); n   } n   std::sort(a, a + n); n   int last = n; n   for (int i = 0; i < n; ++i) { n     if (a[i] > m) { n       last = i; n       break; n     } n   } n   std::sort(a, a + last, std::greater<int>()); n   ll ans = 0, sum = 0; n   for (int i = 0; i < last; ++i) { n     sum += a[i]; n   } n   for (int i = last - 1, j = n - 1; i >= -1; --i) { n     while (j >= last) { n       int num = i + 1 + (n - j) * (d + 1) - d; n       if (num > n) { n         break; n       } n       sum += a[j--]; n     } n     ans = std::max(ans, sum); n     if (i >= 0) { n       sum -= a[i]; n     } n   } n   printf( %lld n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, arr[100]; n   cin >> n; n   for (int i = 0; i < n; i++) { n     cin >> arr[i]; n   } n   for (int i = 0; i < n - 1; i++) { n     for (int j = i; j < n; j++) { n       if (arr[i] > arr[j]) { n         int swap = arr[j]; n         arr[j] = arr[i]; n         arr[i] = swap; n       } n     } n   } n   for (int i = 0; i < n; i++) { n     cout << arr[i] << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   cin >> n; n   while (n--) { n     string s; n     cin >> s; n     if (s.length() > 10) n       cout << s[0] << (s.length() - 2) << s[s.length() - 1] << endl; n     else n       cout << s << endl; n   } n } n


#include <bits/stdc++.h> n const int MAX_N = 1000000; n const int MOD = 1000000007; n std::vector<int> groups; n int table[1 + MAX_N], dp[1 + MAX_N]; n std::deque<int> tablePositions; n int main() { n   std::string s; n   std::cin >> s; n   groups.push_back(0); n   int cnt = 0; n   for (int i = 0; i < s.size(); ++i) n     if (s[i] ==  0 ) n       ++cnt; n     else { n       groups.push_back(cnt); n       cnt = 0; n     } n   groups.push_back(cnt); n   dp[1] = groups[1] + 1; n   for (int i = 2; i < groups.size(); ++i) { n     if (i < groups.size() - 1) dp[i] = dp[i - 1]; n     for (int lastDigit = 0; lastDigit <= groups[i]; ++lastDigit) { n       dp[i] = (dp[i] + dp[i - 1]) % MOD; n       while (!tablePositions.empty() && tablePositions.front() < lastDigit) n         tablePositions.pop_front(); n       if (i < groups.size() - 1 && !tablePositions.empty()) n         dp[i] = (dp[i] + MOD - table[tablePositions.front()]) % MOD; n     } n     tablePositions.push_front(groups[i]); n     table[groups[i]] = dp[i - 1]; n   } n   if (groups.size() == 2) dp[groups.size() - 1]--; n   printf( %d , dp[groups.size() - 1]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAX = 200200; n vector<int> adj[MAX]; n int ch[MAX], sz, from[MAX], dpthMx[MAX], dpthMn[MAX], n; n void dfs(int cur, int par) { n   for (auto i : adj[cur]) n     if (i != par) { n       dfs(i, cur); n       dpthMn[cur] = min(dpthMn[cur], dpthMn[i] + 1); n       dpthMx[cur] = max(dpthMx[cur], dpthMx[i] + 1); n     } n   if (dpthMn[cur] == MAX) dpthMn[cur] = 0; n } n pair<int, int> dfs0(int cur, int par, int d) { n   from[cur] = par; n   pair<int, int> ret(d, cur); n   for (auto i : adj[cur]) n     if (i != par) ret = max(ret, dfs0(i, cur, d + 1)); n   return ret; n } n int main() { n   scanf( %d , &n); n   fill(dpthMn, dpthMn + n + 1, MAX); n   for (int i = 1, u, v; i < n; ++i) { n     scanf( %d%d , &u, &v); n     adj[u].push_back(v); n     adj[v].push_back(u); n   } n   pair<int, int> diam = dfs0(dfs0(1, 0, 0).second, 0, 0); n   while (diam.first > 0) diam.first -= 2, diam.second = from[diam.second]; n   dfs(diam.second, 0); n   for (auto i : adj[diam.second]) { n     if (dpthMx[i] != dpthMn[i]) return puts( -1 ), 0; n     ch[sz++] = dpthMx[i] + 1; n   } n   sort(ch, ch + sz); n   sz = unique(ch, ch + sz) - ch; n   if (sz > 2) return puts( -1 ), 0; n   ch[sz] = 0; n   int ans = ch[0] + ch[1]; n   while (!(ans & 1)) ans >>= 1; n   printf( %d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAX = 100005; n template <typename T> n void readv(vector<T>& v, int n) { n   for (int i = 0; i < n; ++i) { n     T tmp; n     cin >> tmp; n     v.push_back(tmp); n   } n } n struct cmp { n   bool operator()(const pair<int, int>& i1, const pair<int, int>& i2) const { n     int sign = (i1.second - i1.first) - (i2.second - i2.first); n     if (sign != 0) return sign > 0; n     return i1.first > i2.first; n   } n }; n set<pair<int, int>, cmp> intervals; n map<int, pair<int, int> > ileft, iright; n void insert_intvl(const pair<int, int>& intvl) { n   intervals.insert(intvl); n   ileft[intvl.second + 1] = intvl; n   iright[intvl.first - 1] = intvl; n } n void erase_intvl(const pair<int, int>& intvl) { n   intervals.erase(intvl); n   ileft.erase(intvl.second + 1); n   iright.erase(intvl.first - 1); n } n map<int, int> points; n class node { n  public: n   node() : left(0), right(0), size(0) {} n   node *left, *right; n   int size; n }; n node root; n void add_node(node* nd, int x, int shift) { n   nd->size++; n   if (shift < 0) { n     return; n   } n   if ((x & (1 << shift)) == 0) { n     if (!nd->left) { n       nd->left = new node; n     } n     add_node(nd->left, x, shift - 1); n   } else { n     if (!nd->right) { n       nd->right = new node; n     } n     add_node(nd->right, x, shift - 1); n   } n } n void remove_node(node* nd, int x, int shift) { n   nd->size -= 1; n   if (shift < 0) return; n   if ((x & (1 << shift)) == 0) { n     remove_node(nd->left, x, shift - 1); n   } else { n     remove_node(nd->right, x, shift - 1); n   } n } n int query_tree(node* nd, int b, int e, int l, int r, int shift) { n   if ((e < l) || (b > r)) { n     return 0; n   } n   if ((b >= l) && (e <= r)) { n     return nd->size; n   } n   int mid = (b + e) / 2; n   int s1 = nd->left ? query_tree(nd->left, b, mid, l, r, shift - 1) : 0; n   int s2 = nd->right ? query_tree(nd->right, mid + 1, e, l, r, shift - 1) : 0; n   return s1 + s2; n } n const int BITS = 30; n void add_emp(int e, int p) { n   add_node(&root, p, BITS - 1); n   points[e] = p; n } n void remove_emp(int e) { n   remove_node(&root, points[e], BITS - 1); n   points.erase(e); n } n int query(int l, int r) { n   return query_tree(&root, 0, (1 << BITS) - 1, l, r, BITS); n } n int main(int argc, char** argv) { n   int n, q; n   cin >> n >> q; n   intervals.insert(make_pair(1, n)); n   for (int i = 0; i < q; ++i) { n     int e; n     cin >> e; n     if (e == 0) { n       int l, r; n       cin >> l >> r; n       cout << query(l, r) << endl; n     } else { n       if (points.count(e) == 0) { n         pair<int, int> longest = *intervals.begin(); n         erase_intvl(longest); n         int mid = (longest.first + longest.second + 1) / 2; n         if (longest.first <= (mid - 1)) { n           insert_intvl(make_pair(longest.first, mid - 1)); n         } n         if ((mid + 1) <= longest.second) { n           insert_intvl(make_pair(mid + 1, longest.second)); n         } n         add_emp(e, mid); n       } else { n         int pe = points[e]; n         pair<int, int> intvl = make_pair(pe, pe); n         if (ileft.count(pe) == 1) { n           intvl.first = ileft[pe].first; n           erase_intvl(ileft[pe]); n         } n         if (iright.count(pe) == 1) { n           intvl.second = iright[pe].second; n           erase_intvl(iright[pe]); n         } n         insert_intvl(intvl); n         remove_emp(e); n       } n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void file_io() { n   freopen( input.txt ,  r , stdin); n   freopen( output.txt ,  w , stdout); n } n void PLAY() { n   ios_base::sync_with_stdio(false); n   cin.tie(0), cout.tie(0); n } n int main() { n   PLAY(); n   int n; n   cin >> n; n   int x[1000000] = {0}; n   for (int i = 0; i < n - 1; i++) { n     int l, r; n     cin >> l >> r; n     x[l]++; n     x[r]++; n   } n   for (int i = 1; i <= n; i++) { n     if (x[i] == 2) return cout <<  NO , 0; n   } n   cout <<  YES ; n } n


#include <bits/stdc++.h> n using namespace std; n long long n; n int licznik; n long long sym(long long k) { n   long long eat = k, m = n - k; n   while (m > 0) { n     licznik++; n     m -= m / 10; n     eat += min(k, m); n     m -= k; n   } n   return eat; n } n long long binsuche(long long lewy, long long prawy) { n   if (lewy < prawy) { n     if (sym((lewy + prawy) / 2) * 2 >= n) n       return binsuche(lewy, (lewy + prawy) / 2); n     else n       return binsuche((lewy + prawy) / 2 + 1, prawy); n   } n   return lewy; n } n int main() { n   licznik = 0; n   int a, b; n   cin >> n; n   cout << binsuche(1, n); n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 5e5 + 10, INF = 1e9; n inline int read() { n   int x = 0, f = 1; n   char ch = getchar(); n   while (!isdigit(ch)) { n     if (ch ==  - ) f = -f; n     ch = getchar(); n   } n   while (isdigit(ch)) { n     x = x * 10 + ch - 48; n     ch = getchar(); n   } n   return x * f; n } n int n, l[N], r[N], a[N], b[N], c[N]; n int ans, sum, pos, cnt, tot; n int t1, t2; n signed main() { n   n = read(); n   for (int i = 1; i <= n; i++) { n     l[i] = read(); n     r[i] = read(); n     a[++cnt] = l[i]; n     a[++cnt] = r[i]; n   } n   sort(a + 1, a + cnt + 1); n   for (int i = 1; i <= cnt; i++) n     if (i == 1 || a[i] != a[i - 1]) a[++tot] = a[i]; n   for (int i = 1; i <= n; i++) { n     t1 = lower_bound(a + 1, a + tot + 1, l[i]) - a; n     t2 = lower_bound(a + 1, a + tot + 1, r[i]) - a; n     c[t1] = l[i], c[t2] = r[i]; n     b[t1]++, b[t2]--; n   } n   for (int i = 1; i <= tot; i++) { n     sum += b[i]; n     if (sum > ans) ans = sum, pos = c[i]; n   } n   printf( %d %d , pos, ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n double a, b, c; n int main() { n   while (~scanf( %lf%lf%lf , &a, &b, &c)) { n     double x1 = (-b + sqrt(b * b - 4 * a * c)) / (2 * a), n            x2 = (-b - sqrt(b * b - 4 * a * c)) / (2 * a); n     if (a < 0) swap(x1, x2); n     printf( %.6lf n%.6lf n , x1, x2); n   } n   return 0; n   ; n } n


#include <bits/stdc++.h> n using namespace std; n void _fill_int(int* p, int val, int rep) { n   int i; n   for (i = 0; i < rep; i++) p[i] = val; n } n signed long long GETi() { n   signed long long i; n   scanf( %lld , &i); n   return i; n } n int N, X, Y; n int mat[501][501]; n int vis[501][501]; n int LM[501], RM[501], TM[501], BM[501]; n char str[3000002]; n char* P; n void dfs(int cy, int cx, int pre, bool on) { n   char move[16] =  *LR*U***D ; n   int i; n   if (on && (mat[cy][cx] & 1) == 0) *P++ =  1 ; n   if ((vis[cy][cx] & 1) && (pre != 1)) *P++ =  L , dfs(cy, cx - 1, 2, on); n   if ((vis[cy][cx] & 2) && (pre != 2)) *P++ =  R , dfs(cy, cx + 1, 1, on); n   if ((vis[cy][cx] & 4) && (pre != 4)) *P++ =  U , dfs(cy - 1, cx, 8, on); n   if ((vis[cy][cx] & 8) && (pre != 8)) *P++ =  D , dfs(cy + 1, cx, 4, on); n   if (!on) *P++ =  2 ; n   if (pre != 0) *P++ = move[pre]; n } n void solve() { n   int f, i, j, k, l, x, y; n   cin >> N >> Y >> X; n   X--; n   Y--; n   for (y = 0; y < N; y++) n     for (x = 0; x < N; x++) cin >> mat[y][x]; n   for (y = 0; y < N; y++) LM[y] = TM[y] = N, RM[y] = BM[y] = -1; n   l = 0; n   mat[Y][X] |= 2; n   for (y = 0; y < N; y++) n     for (x = 0; x < N; x++) n       if (mat[y][x]) { n         l++; n         if (LM[y] > x) LM[y] = x; n         RM[y] = x; n         if (TM[x] > y) TM[x] = y; n         BM[x] = y; n       } n   if (l == 1 && (mat[Y][X] & 1)) return (void)printf( YES n2 n ); n   P = str; n   stack<int> Q; n   Q.push(Y * 1000 + X); n   while (!Q.empty()) { n     int k = Q.top(); n     Q.pop(); n     int cx = k % 1000, cy = k / 1000; n     TM[cx] = min(TM[cx], cy); n     BM[cx] = max(BM[cx], cy); n     LM[cy] = min(LM[cy], cx); n     RM[cy] = max(RM[cy], cx); n     for (i = 0; i < 4; i++) { n       int dx[] = {0, 0, 1, -1}, dy[] = {1, -1, 0, 0}; n       int tx = cx + dx[i], ty = cy + dy[i]; n       if (tx < 0 || ty < 0 || tx >= N || ty >= N) continue; n       if (dy[i] > 0 && BM[cx] < ty) continue; n       if (dy[i] < 0 && TM[cx] > ty) continue; n       if (dx[i] > 0 && RM[cy] < tx) continue; n       if (dx[i] < 0 && LM[cy] > tx) continue; n       if (mat[ty][tx] & 2) continue; n       mat[ty][tx] |= 2; n       if (dx[i] < 0) vis[cy][cx] |= 1, vis[ty][tx] |= 2; n       if (dx[i] > 0) vis[cy][cx] |= 2, vis[ty][tx] |= 1; n       if (dy[i] < 0) vis[cy][cx] |= 4, vis[ty][tx] |= 8; n       if (dy[i] > 0) vis[cy][cx] |= 8, vis[ty][tx] |= 4; n       Q.push(ty * 1000 + tx); n     } n   } n   for (y = 0; y < N; y++) n     for (x = 0; x < N; x++) n       if (mat[y][x] == 1) return (void)printf( NO n ); n   dfs(Y, X, 0, true); n   dfs(Y, X, 0, false); n   (void)printf( YES n%s n , str); n } n int main(int argc, char** argv) { n   if (argc > 1) freopen(argv[1],  r , stdin); n   solve(); n   return 0; n } n