#include <bits/stdc++.h> n using namespace std; n const double PI = acos(-1.0); n const int INF = 1e9; n const long long LINF = INF * (long long)INF; n const double EPS = 1e-7; n const int MAX = 20 * 1000 + 47; n const int MAS = 2 * 1e5 + 10; n const int MOD = 998244353; n long long haves, haveb, havec; n long long priceb, prices, pricec; n long long money; n long long cntS, cntB, cntC; n bool check(long long cnt) { n   long long a1 = max(0LL, (cntS * cnt - haves) * prices); n   long long a2 = max(0LL, (cntB * cnt - haveb) * priceb); n   long long a3 = max(0LL, (cntC * cnt - havec) * pricec); n   return a1 + a2 + a3 <= money; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   string s; n   cin >> s; n   cntC = cntB = cntC = 0; n   for (int i = (0); i < ((int)s.size()); i++) { n     if (s[i] ==  S ) cntS++; n     if (s[i] ==  B ) { n       cntB++; n     } n     if (s[i] ==  C ) cntC++; n   } n   cin >> haveb >> haves >> havec; n   cin >> priceb >> prices >> pricec; n   cin >> money; n   long long l = 0; n   long long r = 1e14 + 10; n   for (int i = (0); i < (200); i++) { n     long long m = (l + r) / 2; n     if (check(m)) { n       l = m; n     } else { n       r = m; n     } n   } n   cout << l << endl; n } n


#include <bits/stdc++.h> n using namespace std; n struct pii { n   char C; n   int fi; n   int se; n }; n char A[1000][1000]; n void p(int L) { n   cout << L << endl; n   for (int i = 1; i <= L; ++i) { n     for (int j = 1; j <= L; ++j) { n       cout << A[i][j]; n     } n     cout << endl; n   } n } n void prin1(pii a, pii b, pii c) { n   int L = a.se; n   for (int i = 1; i <= L; ++i) { n     for (int j = 1; j <= a.fi; ++j) { n       A[i][j] = a.C; n     } n   } n   for (int i = 1; i <= L; ++i) { n     for (int j = a.fi + 1; j <= a.fi + b.fi; ++j) { n       A[i][j] = b.C; n     } n   } n   for (int i = 1; i <= L; ++i) { n     for (int j = a.fi + b.fi + 1; j <= L; ++j) { n       A[i][j] = c.C; n     } n   } n   p(L); n } n void prin2(pii a, pii b, pii c) { n   int L = a.fi; n   for (int i = 1; i <= L; ++i) { n     for (int j = 1; j <= a.se; ++j) { n       A[j][i] = a.C; n     } n   } n   for (int i = 1; i <= L; ++i) { n     for (int j = a.se + 1; j <= a.se + b.se; ++j) { n       A[j][i] = b.C; n     } n   } n   for (int i = 1; i <= L; ++i) { n     for (int j = a.se + b.se + 1; j <= L; ++j) { n       A[j][i] = c.C; n     } n   } n   p(L); n } n void prin3(pii a, pii b, pii c) { n   int L = c.se; n   for (int i = 1; i <= a.fi; ++i) { n     for (int j = 1; j <= a.se; ++j) { n       A[i][j] = a.C; n     } n   } n   for (int i = 1; i <= b.fi; ++i) { n     for (int j = a.se + 1; j <= L; ++j) { n       A[i][j] = b.C; n     } n   } n   for (int i = a.fi + 1; i <= L; ++i) { n     for (int j = 1; j <= L; ++j) { n       A[i][j] = c.C; n     } n   } n   p(L); n } n void prin4(pii a, pii b, pii c) { n   int L = a.fi; n   for (int i = 1; i <= b.fi; ++i) { n     for (int j = a.se + 1; j <= L; ++j) { n       A[i][j] = b.C; n     } n   } n   for (int i = b.fi + 1; i <= L; ++i) { n     for (int j = a.se + 1; j <= L; ++j) { n       A[i][j] = c.C; n     } n   } n   for (int i = 1; i <= L; ++i) { n     for (int j = 1; j <= a.se; ++j) { n       A[i][j] = a.C; n     } n   } n   p(L); n } n bool check(pii a, pii b, pii c) { n   if (a.fi + b.fi + c.fi == a.se and a.se == b.se and b.se == c.se) { n     prin1(a, b, c); n     return 1; n   } n   if (a.fi == b.fi and b.fi == c.fi and a.fi == a.se + b.se + c.se) { n     prin2(a, b, c); n     return 1; n   } n   if (a.fi == b.fi + c.fi and a.fi == a.se + b.se and b.se == c.se) { n     prin4(a, b, c); n     return 1; n   } n   if (a.se + b.se == c.se and c.se == a.fi + c.fi and a.fi == b.fi) { n     prin3(a, b, c); n     return 1; n   } n   return 0; n } n int main() { n   vector<pii> temp; n   pii a, b, c; n   cin >> a.fi >> a.se; n   cin >> b.fi >> b.se; n   cin >> c.fi >> c.se; n   a.C =  A ; n   b.C =  B ; n   c.C =  C ; n   vector<int> g; n   g.push_back(0); n   g.push_back(1); n   g.push_back(2); n   for (int mask = 0; mask < 8; ++mask) { n     if (mask & (1 << 0)) swap(a.fi, a.se); n     if (mask & (1 << 1)) swap(b.fi, b.se); n     if (mask & (1 << 2)) swap(c.fi, c.se); n     temp.clear(); n     sort(g.begin(), g.end()); n     temp.push_back(a); n     temp.push_back(b); n     temp.push_back(c); n     do { n       if (check(temp[g[0]], temp[g[1]], temp[g[2]])) return 0; n     } while (next_permutation(g.begin(), g.end())); n     if (mask & (1 << 0)) swap(a.fi, a.se); n     if (mask & (1 << 1)) swap(b.fi, b.se); n     if (mask & (1 << 2)) swap(c.fi, c.se); n   } n   cout << -1; n } n


#include <bits/stdc++.h> n using namespace std; n int hc(int a, int b) { n   if (b == 0) return a; n   return hc(b, a % b); n } n int main() { n   int n, a[10010], i, sum = 0; n   cin >> n; n   int k = n; n   for (i = 2; i < n; i++) { n     a[i] = i; n   } n   for (i = 2; i < n; i++) { n     while (n != 0) { n       sum = sum + n % a[i]; n       n = n / a[i]; n     } n     n = k; n   } n   int q = k - 2; n   int h = hc(sum, k - 2), b; n   sum = sum / h; n   b = q / h; n   cout << sum <<  /  << b << endl; n } n


#include <bits/stdc++.h> n #pragma GCC optimize( -O3 ) n using namespace std; n void _main(); n int main() { n   cin.tie(0); n   ios::sync_with_stdio(false); n   _main(); n } n template <typename T> n int gaussianElimination(vector<vector<T>> &A, int var = -1) { n   int N = A.size(), M = A[0].size(); n   if (var < 0) var = min(N, M); n   int k = 0, rank = var; n   for (int i = 0; i < var; i++) { n     int v = -1; n     for (int j = k; j < N && v == -1; j++) n       if (A[j][i] != 0) v = j; n     if (v == -1) { n       rank--; n       continue; n     } n     swap(A[k], A[v]); n     T x = A[k][i]; n     for (int j = 0; j < M; j++) A[k][j] = A[k][j] / x; n     for (int j = 0; j < N; j++) n       if (j != k) { n         T y = A[j][i]; n         for (int h = M - 1; h >= i; h--) A[j][h] = A[j][h] - A[k][h] * y; n       } n     k++; n   } n   return rank; n } n template <typename T> n void print(vector<vector<T>> &A) { n   int N = A.size(), M = A[0].size(); n   for (int y = 0; y < N; y++) { n     for (int x = 0; x < M; x++) cout << A[y][x] <<   t ; n     cout << endl; n   } n   cout << endl; n } n template <int MOD> n struct ModInt { n   static const int Mod = MOD; n   unsigned x; n   ModInt() : x(0) {} n   ModInt(signed sig) { x = sig < 0 ? sig % MOD + MOD : sig % MOD; } n   ModInt(signed long long sig) { x = sig < 0 ? sig % MOD + MOD : sig % MOD; } n   int get() const { return (int)x; } n   ModInt &operator+=(ModInt that) { n     if ((x += that.x) >= MOD) x -= MOD; n     return *this; n   } n   ModInt &operator-=(ModInt that) { n     if ((x += MOD - that.x) >= MOD) x -= MOD; n     return *this; n   } n   ModInt &operator*=(ModInt that) { n     x = (unsigned long long)x * that.x % MOD; n     return *this; n   } n   ModInt &operator/=(ModInt that) { return *this *= that.inverse(); } n   ModInt operator+(ModInt that) const { return ModInt(*this) += that; } n   ModInt operator-(ModInt that) const { return ModInt(*this) -= that; } n   ModInt operator*(ModInt that) const { return ModInt(*this) *= that; } n   ModInt operator/(ModInt that) const { return ModInt(*this) /= that; } n   ModInt inverse() const { n     long long a = x, b = MOD, u = 1, v = 0; n     while (b) { n       long long t = a / b; n       a -= t * b; n       std::swap(a, b); n       u -= t * v; n       std::swap(u, v); n     } n     return ModInt(u); n   } n   bool operator==(ModInt that) const { return x == that.x; } n   bool operator!=(ModInt that) const { return x != that.x; } n   ModInt operator-() const { n     ModInt t; n     t.x = x == 0 ? 0 : Mod - x; n     return t; n   } n }; n template <int MOD> n ModInt<MOD> operator^(ModInt<MOD> a, unsigned long long k) { n   ModInt<MOD> r = 1; n   while (k) { n     if (k & 1) r *= a; n     a *= a; n     k >>= 1; n   } n   return r; n } n template <int MOD> n std::ostream &operator<<(std::ostream &stream, const ModInt<MOD> a) { n   stream << a.get(); n   return stream; n }; n int N, M, Q; n string S[505], B[303]; n void _main() { n   cin >> N >> M; n   for (int i = 0; i < N; i++) cin >> S[i]; n   cin >> Q; n   for (int i = 0; i < Q; i++) cin >> B[i]; n   vector<vector<ModInt<5>>> A(M, vector<ModInt<5>>(N + Q)); n   for (int i = 0; i < N; i++) n     for (int j = 0; j < M; j++) A[j][i] = S[i][j] -  a ; n   for (int i = 0; i < Q; i++) n     for (int j = 0; j < M; j++) A[j][i + N] = B[i][j] -  a ; n   int rank = gaussianElimination(A, N); n   ModInt<1000000007> ans = 1; n   for (int i = 0; i < N - rank; i++) ans *= 5; n   vector<int> zero; n   for (int i = 0; i < M; i++) { n     int ok = 1; n     for (int j = 0; j < N; j++) n       if (A[i][j] == 1) ok = 0; n     if (ok) zero.push_back(i); n   } n   for (int i = 0; i < Q; i++) { n     int ok = 1; n     for (auto &j : zero) n       if (A[j][N + i] != 0) ok = 0; n     if (ok) n       printf( %d n , ans.get()); n     else n       printf( 0 n ); n   } n } n


#include <bits/stdc++.h> n using namespace std; n long long mod = 1000000007; n template <typename T> n void debug(vector<T> x) { n   for (auto e : x) { n     cout << e <<    ; n   } n   cout <<   n ; n } n void solve() { n   long long n; n   cin >> n; n   long long x = n / 4; n   long long y = n % 4; n   if (y % 4 == 0) { n     for (long long i = 0; i < n - x; i++) { n       cout <<  9 ; n     } n     for (long long i = 0; i < x; i++) { n       cout <<  8 ; n     } n     cout <<   n ; n     return; n     ; n   } else { n     for (long long i = 0; i < n - (x + 1); i++) { n       cout <<  9 ; n     } n     for (long long i = 0; i < x + 1; i++) { n       cout <<  8 ; n     } n     cout <<   n ; n     return; n     ; n   } n } n signed main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   long long 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 n, m, cur; n int fa[1005]; n set<int> s1, s2; n struct edge { n   int from, to; n   char color[2]; n } e[100005]; n int find(int x) { return fa[x] == x ? x : fa[x] = find(fa[x]); } n void init() { n   for (int i = 1; i <= n; i++) { n     fa[i] = i; n   } n   cur = n; n } n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cin >> n >> m; n   for (int i = 1; i <= m; i++) { n     cin >> e[i].from >> e[i].to >> e[i].color; n   } n   if (n % 2 == 0) { n     cout << -1 << endl; n     return 0; n   } n   init(); n   for (int i = 1; i <= m; i++) { n     if (e[i].color[0] ==  S ) { n       int fx = find(e[i].from); n       int fy = find(e[i].to); n       if (fx != fy) { n         fa[fx] = fy; n         cur--; n       } n     } n   } n   if (cur - 1 > (n - 1) / 2) { n     cout << -1 << endl; n     return 0; n   } n   for (int i = 1; i <= m; i++) { n     if (e[i].color[0] ==  M ) { n       int fx = find(e[i].from); n       int fy = find(e[i].to); n       if (fx != fy) { n         fa[fx] = fy; n         cur--; n         s2.insert(i); n       } n     } n   } n   if (cur != 1) { n     cout << -1 << endl; n     return 0; n   } n   init(); n   for (auto i : s2) { n     int fx = find(e[i].from); n     int fy = find(e[i].to); n     if (fx != fy) { n       fa[fx] = fy; n       cur--; n     } n   } n   for (int i = 1; i <= m; i++) { n     if (s2.size() == (n - 1) / 2) break; n     if (e[i].color[0] ==  M ) { n       int fx = find(e[i].from); n       int fy = find(e[i].to); n       if (fx != fy) { n         fa[fx] = fy; n         cur--; n         s2.insert(i); n       } n     } n   } n   if (s2.size() != (n - 1) / 2) { n     cout << -1 << endl; n     return 0; n   } n   for (int i = 1; i <= m; i++) { n     if (e[i].color[0] ==  S ) { n       int fx = find(e[i].from); n       int fy = find(e[i].to); n       if (fx != fy) { n         fa[fx] = fy; n         cur--; n         s1.insert(i); n       } n     } n   } n   cout << n - 1 << endl; n   for (auto i : s1) cout << i <<    ; n   for (auto i : s2) cout << i <<    ; n   cout << endl; n   return 0; n } n


#include <bits/stdc++.h> n namespace FastIO { n const int SIZE = (1 << 21) + 1; n char ibuf[SIZE], *iS, *iT, obuf[SIZE], *oS = obuf, *oT = oS + SIZE - 1, c, n                                        qu[55]; n int qr; n inline void flush() { n   fwrite(obuf, 1, oS - obuf, stdout); n   oS = obuf; n } n inline void putc(char x) { n   *oS++ = x; n   if (oS == oT) flush(); n } n inline void wrap() { n   *oS++ =   n ; n   if (oS == oT) flush(); n } n template <class I> n inline void gi(I &x) { n   I sign = 1; n   for (c = (iS == iT ? (iT = (iS = ibuf) + fread(ibuf, 1, SIZE, stdin), n                        (iS == iT ? EOF : *iS++)) n                      : *iS++); n        c <  0  || c >  9 ; n        c = (iS == iT ? (iT = (iS = ibuf) + fread(ibuf, 1, SIZE, stdin), n                        (iS == iT ? EOF : *iS++)) n                      : *iS++)) n     if (c ==  - ) sign = -1; n   for (x = 0; c <=  9  && c >=  0 ; n        c = (iS == iT ? (iT = (iS = ibuf) + fread(ibuf, 1, SIZE, stdin), n                        (iS == iT ? EOF : *iS++)) n                      : *iS++)) n     x = x * 10 + (c & 15); n   x *= sign; n } n inline int gi() { n   int x, sign = 1; n   for (c = (iS == iT ? (iT = (iS = ibuf) + fread(ibuf, 1, SIZE, stdin), n                        (iS == iT ? EOF : *iS++)) n                      : *iS++); n        c <  0  || c >  9 ; n        c = (iS == iT ? (iT = (iS = ibuf) + fread(ibuf, 1, SIZE, stdin), n                        (iS == iT ? EOF : *iS++)) n                      : *iS++)) n     if (c ==  - ) sign = -1; n   for (x = 0; c <=  9  && c >=  0 ; n        c = (iS == iT ? (iT = (iS = ibuf) + fread(ibuf, 1, SIZE, stdin), n                        (iS == iT ? EOF : *iS++)) n                      : *iS++)) n     x = x * 10 + (c & 15); n   return x * sign; n } n inline long long gl() { n   long long x, sign = 1; n   for (c = (iS == iT ? (iT = (iS = ibuf) + fread(ibuf, 1, SIZE, stdin), n                        (iS == iT ? EOF : *iS++)) n                      : *iS++); n        c <  0  || c >  9 ; n        c = (iS == iT ? (iT = (iS = ibuf) + fread(ibuf, 1, SIZE, stdin), n                        (iS == iT ? EOF : *iS++)) n                      : *iS++)) n     if (c ==  - ) sign = -1; n   for (x = 0; c <=  9  && c >=  0 ; n        c = (iS == iT ? (iT = (iS = ibuf) + fread(ibuf, 1, SIZE, stdin), n                        (iS == iT ? EOF : *iS++)) n                      : *iS++)) n     x = x * 10 + (c & 15); n   return x * sign; n } n template <class I> n inline void print(I &x) { n   if (!x) putc( 0 ); n   while (x) qu[++qr] = x % 10 +  0 , x /= 10; n   while (qr) putc(qu[qr--]); n } n template <class I> n inline void println(I &x) { n   if (!x) putc( 0 ); n   while (x) qu[++qr] = x % 10 +  0 , x /= 10; n   while (qr) putc(qu[qr--]); n   wrap(); n } n }  // namespace FastIO n using namespace std; n int n; n struct Rectangle { n   int l, r, d, u; n } rec[1000 + 5]; n bool solve(vector<Rectangle> v) { n   if (v.size() <= 1) return 1; n   int maxr = 0, maxu = 0; n   bool flag = 0; n   vector<Rectangle> pre, suf; n   sort(v.begin(), v.end(), n        [](const Rectangle &a, const Rectangle &b) { return a.l < b.l; }); n   for (int i = v.size() - 1; ~i; --i) suf.push_back(v[i]); n   for (int i = 0; i < v.size() - 1; ++i) { n     pre.push_back(v[i]), suf.pop_back(); n     maxr = max(maxr, v[i].r); n     if (maxr <= v[i + 1].l && solve(pre) && solve(suf)) { n       flag = 1; n       break; n     } n   } n   if (flag) return true; n   sort(v.begin(), v.end(), n        [](const Rectangle &a, const Rectangle &b) { return a.d < b.d; }); n   pre.clear(); n   suf.clear(); n   for (int i = v.size() - 1; ~i; --i) suf.push_back(v[i]); n   for (int i = 0; i < v.size() - 1; ++i) { n     pre.push_back(v[i]), suf.pop_back(); n     maxu = max(maxu, v[i].u); n     if (maxu <= v[i + 1].d && solve(pre) && solve(suf)) { n       flag = 1; n       break; n     } n   } n   if (flag) return true; n   return false; n } n int main() { n   scanf( %d , &n); n   vector<Rectangle> v; n   for (int i = 1, a, b, c, d; i <= n; ++i) { n     scanf( %d%d%d%d , &a, &b, &c, &d); n     rec[i] = {a, c, b, d}; n     v.push_back(rec[i]); n   } n   if (solve(v)) n     printf( YES n ); n   else n     printf( NO n ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 5005; n const long long MOD = 998244353ll; n enum RESULT { OK, WIN }; n int n, v[N], nxt[N], prv[N]; n long long dp[N][2], acc[N], inv[N]; n long long expr(long long e, long long n) { n   if (n == 0) return 1ll; n   long long tp = expr(e, n / 2ll); n   tp = (tp * tp) % MOD; n   if (n % 2ll) tp = (tp * e) % MOD; n   return tp; n } n void ok(int t, int p) { n   int h = prv[p - 1]; n   dp[p][OK] = (acc[h] * inv[n - t + 1]) % MOD; n } n void win(int t, int p) { n   int r = nxt[p - 1]; n   int l = prv[p - 1]; n   dp[p][WIN] = n       (((acc[r] - acc[l] - dp[p][OK] + 2ll * MOD) % MOD) * inv[n - t + 1]) % n       MOD; n } n int main() { n   scanf( %d , &n); n   for (int i = 1; i <= n; i++) { n     scanf( %d , &v[i - 1]); n     dp[i][OK] = expr(n, MOD - 2) % MOD; n     acc[i] = (acc[i - 1] + dp[i][OK]) % MOD; n   } n   sort(v, v + n); n   long long resp = 0ll; n   for (int t = 1; t <= n; t++) inv[t] = expr(t, MOD - 2); n   for (int i = 0; i < n; i++) { n     prv[i] = lower_bound(v, v + n, v[i]) - v; n     nxt[i] = upper_bound(v, v + n, v[i]) - v; n   } n   for (int t = 2; t <= n; t++) { n     for (int p = 1; p <= n; p++) { n       win(t, p); n       ok(t, p); n       resp = (resp + dp[p][WIN]) % MOD; n     } n     for (int p = 1; p <= n; p++) acc[p] = (acc[p - 1] + dp[p][OK]) % MOD; n   } n   printf( %lld n , resp); n } n


#include <bits/stdc++.h> n using namespace std; n void solve() { n   long long n, m, i, j; n   cin >> n >> m; n   int x, y; n   vector<vector<int> > adj(n); n   vector<int> ncon(n, 0); n   for (i = (0); i < (m); i++) { n     cin >> x >> y; n     adj[x - 1].push_back(y - 1); n     adj[y - 1].push_back(x - 1); n     ncon[x - 1]++; n     ncon[y - 1]++; n   } n   vector<int> onecon; n   for (i = (0); i < (n); i++) n     if (ncon[i] == 1) onecon.push_back(i); n   int maxd = 0; n   queue<int> q; n   vector<int> dist(n, 0), rem(n, 0); n   for (auto i : onecon) { n     q.push(i); n     dist[i] = 1; n   } n   if ((int)((onecon).size()) != 0) maxd = 1; n   while (!q.empty()) { n     int cur = q.front(); n     q.pop(); n     rem[cur] = 1; n     if (ncon[cur] == 1) maxd = max(maxd, dist[cur]); n     for (auto next : adj[cur]) { n       if (rem[next] == 1) continue; n       ncon[next]--; n       if (ncon[next] == 1) { n         dist[next] = dist[cur] + 1; n         q.push(next); n       } n     } n   } n   cout << maxd <<   n ; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e3 + 5; n const int M = 1e4 + 5; n int n, m, w, vis[N], l[N], r[N]; n long long a[N], ans[N], s; n int main() { n   cin >> n >> s; n   long long sum = 0; n   for (int i = 1; i <= n; i++) { n     cin >> a[i]; n     sum += a[i]; n   } n   if (sum < s) { n     cout << -1 << endl; n     return 0; n   } else if (sum == s) { n     cout << 0 << endl; n     return 0; n   } n   sort(a + 1, a + 1 + n); n   int now = 1; n   sum = s; n   while (a[now] == a[1]) { n     now++; n   } n   for (int i = n; i >= now; i--) { n     sum -= (a[i] - a[1]); n   } n   if (sum) { n     int cnt = 0; n     while (sum > 0) { n       sum -= n; n       cnt++; n     } n     cout << a[1] - cnt << endl; n   } else { n     cout << a[1] << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int S, C, Ha, Hb, Wa, Wb; n long long Ans; n int main() { n   scanf( %d%d%d%d%d , &C, &Ha, &Hb, &Wa, &Wb); n   S = sqrt(C); n   if (Wa > Wb) swap(Ha, Hb), swap(Wa, Wb); n   if (Wb > S) n     for (int i = 0; 1LL * i * Wb <= C; i++) n       Ans = max(Ans, 1LL * i * Hb + 1LL * (C - i * Wb) / Wa * Ha); n   else { n     if (1LL * Ha * Wb > 1LL * Hb * Wa) swap(Ha, Hb), swap(Wa, Wb); n     for (int i = 0; i < Wb; i++) n       Ans = max(Ans, 1LL * i * Ha + 1LL * (C - i * Wa) / Wb * Hb); n   } n   printf( %I64d n , Ans); n } n


#include <bits/stdc++.h> n using namespace std; n int n, m; n char a[109][109], b[109][109]; n void add(int x, int y) { n   if (a[x][y] ==  * ) return; n   if (b[x][y] ==   0 ) n     b[x][y] =  1 ; n   else n     ++b[x][y]; n } n void fill(int x, int y) { n   if (x > 0 && y > 0) add(x - 1, y - 1); n   if (x > 0) { n     add(x - 1, y); n     add(x - 1, y + 1); n   } n   if (y > 0) { n     add(x, y - 1); n     add(x + 1, y - 1); n   } n   add(x, y + 1); n   add(x + 1, y); n   add(x + 1, y + 1); n } n bool check() { n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < m; j++) { n       if ((a[i][j] >=  1  && a[i][j] <=  8 ) || n           (b[i][j] >=  1  && b[i][j] <=  8 )) { n         if (a[i][j] != b[i][j]) { n           return false; n         } n       } n     } n   } n   return true; n } n int main() { n   cin >> n >> m; n   for (int i = 0; i < n; i++) { n     scanf( %s , a[i]); n   } n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < m; j++) { n       if (a[i][j] ==  * ) { n         fill(i, j); n       } n     } n   } n   if (check()) n     cout <<  YES  << endl; n   else n     cout <<  NO  << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n using ui32 = unsigned int; n using i64 = long long; n using f64 = long double; n using ui64 = unsigned long long; n void read(int& x) { scanf( %d , &x); } n void read(int n, vector<int>& a) { n   for (int i = 0; i < n; i++) read(a[i]); n } n void read(int n, int* a) { n   for (int i = 0; i < n; i++) read(a[i]); n } n void write(int x) { printf( %d n , x); } n void print(int x) { printf( %d  , x); } n void write(int n, vector<int> a) { n   for (int i = 0; i < n; i++) printf( %d n , a[i]); n } n void print(int n, int* a) { n   for (int i = 0; i < n; i++) printf( %d  , a[i]); n } n void print(string s) { printf( %s n , s.c_str()); } n i64 z[3 * 100007], lcp[3 * 100007], cnt[3 * 100007]; n signed main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   string s, t; n   cin >> s >> t; n   string concate = t +  #  + s; n   i64 n = (i64)(concate.size()), m = (i64)(t.size()); n   i64 l = 0; n   i64 r = 0; n   for (i64 i = 0; i < n; i++) { n     if (i > r) { n       l = i; n       r = i; n       while (r < n && concate[r - l] == concate[r]) { n         r++; n       } n       z[i] = r - l; n       r--; n     } else { n       i64 k = i - l; n       if (z[k] < r - i + 1) n         z[i] = z[k]; n       else { n         l = i; n         while (r < n && concate[r - l] == concate[r]) { n           r++; n         } n         z[i] = r - l; n         r--; n       } n     } n   } n   n = (i64)(s.size()); n   for (i64 i = m + 2; i < n + m + 1; i++) { n     cnt[z[i]]++; n   } n   for (i64 i = m; i > 0; i--) { n     cnt[i - 1] += cnt[i]; n   } n   l = 0; n   r = 1; n   while (r != m) { n     if (t[l] == t[r]) { n       l++; n       lcp[r] = l; n       r++; n     } else { n       if (l == 0) { n         lcp[r] = 0; n         r++; n       } else { n         l = lcp[l - 1]; n       } n     } n   } n   i64 ans = n * m; n   for (i64 i = 0; i < m; i++) { n     if (lcp[i] != 0) { n       ans -= cnt[i + 1 - lcp[i]]; n     } n   } n   cout << ans << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int isprime(int n) { n   int i; n   for (i = 2; i * i <= n; i++) { n     if (n % i == 0) return 0; n   } n   return 1; n } n int main() { n   long long int n, k = 0, i = 0, m, x = 0, y, q, r, s, l, t, a, v0, v1, c, b, n                    j = 0, d = 0, ct = 0, sum = 0, h[1000009] = {0}, n                    temp = 10000, fl = 0; n   cin >> c >> v0 >> v1 >> a >> l; n   for (i = 0;; i++) { n     if ((v0 + a * i) <= v1) n       c = c - (v0 + a * i); n     else n       c -= v1; n     if (c <= 0) break; n     c += l; n   } n   cout << i + 1; n } n


#include <bits/stdc++.h> n using namespace std; n void GO() { n   ios_base::sync_with_stdio(0); n   cin.tie(0), cout.tie(0); n } n const long long INF = 1e9; n const long long mod = (1e9) + 7; n const int maxn = (int)100; n const long double PI = acos(-1.0); n const long double eps = 0.000000001; n mt19937 rnd(time(0)); n long long step[19]; n long long lower(long long a_0, long long d, long long k) { n   long long l = 0, r = INF; n   while (r - l > 1) { n     long long m = r + l >> 1; n     long long a_m = a_0 + d * (m - 1); n     long long sum = (a_0 + a_m) * m / 2; n     (sum >= k ? r : l) = m; n   } n   return l; n } n void solve1(long long k) { n   for (long long i = 1; i < 19; ++i) { n     long long plus = i * (step[i] - step[i - 1]); n     if (plus == k) { n       cout <<  9 n ; n       return; n     } n     if (k > plus) { n       k -= plus; n       continue; n     } n     long long x = (k + i - 1) / i; n     k %= i; n     long long num = step[i - 1] + x - 1; n     string ans = to_string(num); n     if (!k) k = i; n     cout << ans[k - 1] <<   n ; n     return; n   } n } n void solve() { n   long long k; n   cin >> k; n   long long a_0 = 1; n   for (long long i = 1; i < 19; ++i) { n     long long n = step[i] - step[i - 1]; n     long long a_n = a_0 + i * (n - 1); n     long long sum = (a_0 + a_n) * n / 2ll; n     if (k == sum) { n       cout <<  9 n ; n       return; n     } n     if (k > sum) { n       k -= sum; n       a_0 = a_n + i + 1; n       continue; n     } n     long long x = lower(a_0, i, k); n     long long a_x = a_0 + i * (x - 1); n     k -= (a_0 + a_x) * x / 2; n     solve1(k); n     return; n   } n } n signed main() { n   GO(); n   step[0] = 1; n   for (int i = 1; i < 19; ++i) step[i] = step[i - 1] * 10ll; n   int Q = 1; n   cin >> Q; n   while (Q--) { n     solve(); n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e6 + 7; n int n, nn, m; n int a[N], b[N], c[N]; n int f[N]; n inline void add(int i, int v) { n   for (; i <= n; i += i & -i) f[i] += v; n } n inline int find(int v) { n   int i = 0; n   for (int k = 20; k >= 0; k--) { n     if (i + (1 << k) <= n && f[i + (1 << k)] < v) { n       i += 1 << k; n       v -= f[i]; n     } n   } n   return i + 1; n } n int main() { n   scanf( %d %d , &n, &m); n   for (int i = 1; i <= m; i++) scanf( %d , a + i); n   for (int i = 1; i <= n; i++) { n     scanf( %d , c + i); n     if (c[i] != -1) b[++nn] = c[i]; n   } n   for (int i = 1; i <= nn; i++) add(i, 1); n   nn = 0; n   for (int i = 1; i <= n; i++) { n     if (c[i] != -1) n       nn++; n     else { n       int k; n       for (k = 1; k <= m && a[k] <= nn; k++) add(find(a[k] - k + 1), -1); n       nn -= k - 1; n     } n   } n   if (nn == 0) { n     printf( Poor stack! n ); n     return 0; n   } n   for (int i = 1; i <= nn; i++) printf( %d , b[find(i)]); n } n


#include <bits/stdc++.h> n using namespace std; n int main(void) { n   int m; n   cin >> m; n   int C[m]; n   for (int i = 0; i < m; ++i) cin >> C[i]; n   int x, y; n   cin >> x >> y; n   int sum_1, sum_2; n   sum_1 = sum_2 = 0; n   int now = 0; n   for (int i = 0; i < m; ++i) sum_2 += C[i]; n   while (sum_1 < x || sum_1 > y || sum_2 < x || sum_2 > y) { n     if (now == m) break; n     sum_1 += C[now]; n     sum_2 -= C[now]; n     ++now; n   } n   if (now == m) n     cout << 0; n   else n     cout << now + 1; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long mxN = 52; n vector<long long> adj[mxN]; n signed main() { n   long long n; n   cin >> n; n   long long x = (n * (n - 1)) / 2; n   long long a[x - 1][2]; n   for (long long i = 0; i < x - 1; i++) { n     cin >> a[i][0] >> a[i][1]; n     adj[a[i][0]].push_back(a[i][1]); n     adj[a[i][1]].push_back(a[i][0]); n   } n   long long ans1 = -1, ans2 = -1; n   for (long long i = 1; i <= n; i++) { n     if (ans1 == -1 && adj[i].size() < n - 1) { n       ans1 = i; n     } else if (ans2 == -1 && adj[i].size() < n - 1) { n       ans2 = i; n     } n   } n   for (long long i = 1; i <= n; i++) { n     if (i == ans1 || i == ans2) { n       continue; n     } else { n       long long w1 = -1, w2 = -1; n       for (long long j = 0; j < x - 1; j++) { n         if (a[j][0] == ans1 && a[j][1] == i) { n           w1 = ans1; n         } else if (a[j][0] == i && a[j][1] == ans1) { n           w1 = i; n         } else if (a[j][0] == ans2 && a[j][1] == i) { n           w2 = ans2; n         } else if (a[j][0] == i && a[j][1] == ans2) { n           w2 = i; n         } n       } n       if (w1 == ans1 && w2 != ans2) { n         cout << ans1 <<     << ans2 << endl; n         return 0; n       } else if (w2 == ans2 && w1 != ans1) { n         cout << ans2 <<     << ans1 << endl; n         return 0; n       } n     } n   } n   cout << ans1 <<     << ans2 << endl; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 1010; n int n; n int dp[maxn][256], a[maxn], cur[8]; n vector<int> pos[maxn]; n int ok(int len) { n   int ans = -1; n   memset(dp, -1, sizeof(dp)); n   memset(cur, 0, sizeof(cur)); n   dp[0][0] = 0; n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < 256; j++) { n       if (dp[i][j] == -1) continue; n       for (int k = 0; k < 8; k++) { n         if (j & (1 << k)) continue; n         int p = cur[k] + len - 1; n         if (pos[k].size() <= p) continue; n         dp[pos[k][p] + 1][j | 1 << k] = n             max(dp[pos[k][p]][j | 1 << k], dp[i][j]); n         if (pos[k].size() <= p + 1) continue; n         dp[pos[k][p + 1] + 1][j | 1 << k] = n             max(dp[pos[k][p + 1] + 1][j | 1 << k], dp[i][j] + 1); n       } n     } n     cur[a[i]]++; n   } n   for (int i = 0; i < n + 1; i++) ans = max(ans, dp[i][255]); n   if (ans == -1) return ans; n   return ans * (len + 1) + (8 - ans) * len; n } n int read() { n   int x = 0, f = 1; n   char ch = getchar(); n   while (ch <  0  || ch >  9 ) { n     if (ch ==  - ) f = -1; n     ch = getchar(); n   } n   while (ch >=  0  && ch <=  9 ) { n     x = x * 10 + ch -  0 ; n     ch = getchar(); n   } n   return f * x; n } n int main() { n   n = read(); n   for (int i = 0; i < n; i++) a[i] = read() - 1; n   for (int i = 0; i < n; i++) pos[a[i]].push_back(i); n   int l = 1, r = n / 8; n   while (r - l > 1) { n     int mid = (l + r) >> 1; n     if (ok(mid) != -1) n       l = mid; n     else n       r = mid; n   } n   int ans = max(ok(l), ok(r)); n   if (ans == -1) { n     ans = 0; n     for (int i = 0; i < 8; i++) n       if (pos[i].size()) ans++; n   } n   printf( %d n , ans); n   return 0; n } n


#pragma GCC optimize( O3 , Ofast , unroll-loops , omit-frame-pointer , inline ) //Optimization flags n #pragma GCC option( arch=native , tune=native , no-zero-upper ) //Enable AVX n // #pragma GCC target( avx2 ) //Enable AVX n  n #include <bits/stdc++.h> n  n #define int long long n  n using namespace std; n  n #ifdef LOCAL  n #include  /media/brayand/BrayanD/debugger.h  n #else n #define db(...) false n #define dbl(...) false n #define dbg(...) false n #define dbm(...) false n #define dbs(...) false n #define dbas(...) false n #endif n  n const int oo = 1e18; n  n const int MAXN = 4e4+100; n  n int ST[MAXN*4]; n int lazy[MAXN*4]; n  n int dp[MAXN][120]; n  n void prop(int nodo) n { n  if(lazy[nodo] != 0) n  { n   ST[nodo] += lazy[nodo]; n   if(nodo*2 < MAXN*4) n   { n    lazy[nodo*2] += lazy[nodo]; n   } n   if(nodo*2+1 < MAXN*4) n   { n    lazy[nodo*2+1] += lazy[nodo]; n   } n   lazy[nodo] = 0; n  } n } n  n void build(int nodo, int l, int r, int k) n { n  lazy[nodo] = 0; n  if(l == r) n  { n   ST[nodo] = dp[l][k]; n   return; n  } n  int mid = (l+r)/2; n  build(nodo*2,l,mid,k); n  build(nodo*2+1,mid+1,r,k); n  ST[nodo] = max(ST[nodo*2], ST[nodo*2+1]); n } n  n void update(int nodo, int l, int r, int a, int b, int x) n { n  prop(nodo); n  if(r < a || l > b)return; n  if(a <= l && r <= b) n  { n   lazy[nodo] += x; n   prop(nodo); n   return; n  } n  int mid = (l+r)/2; n  update(nodo*2,l,mid,a,b,x); n  update(nodo*2+1,mid+1,r,a,b,x); n  ST[nodo] = max(ST[nodo*2], ST[nodo*2+1]); n } n  n int query(int nodo, int l, int r, int a, int b) n { n  prop(nodo); n  if(r < a || l > b)return -oo; n  if(a <= l && r <= b) n  { n   return ST[nodo]; n  } n  int mid = (l+r)/2; n  return max(query(nodo*2,l,mid,a,b), query(nodo*2+1,mid+1,r,a,b)); n } n  n int A[MAXN]; n  n vector<int> arr[MAXN]; n  n int T[MAXN]; n  n vector<int> ins[MAXN]; n vector<int> del[MAXN]; n  n int32_t main() n { n     ios_base::sync_with_stdio(0); n     cin.tie(0); n  n     int N, K; n     cin >> N >> K; n     K--; n  n     for(int i = 1 ; i <= N ; i++) n     { n      cin >> A[i]; n      arr[A[i]].push_back(i); n     } n  n     if(N == 1) n     { n      cout << 0 <<   n ; n      return 0; n     } n  n     vector<pair<int,int>> vect; n  n     int sum = 0; n  n     for(int i = 1 ; i <= N ; i++) n     { n      for(int j = 1 ; j < arr[i].size() ; j++) n      { n       vect.push_back({arr[i][j-1],arr[i][j]-1}); n      } n      if(arr[i].size()) n      { n       sum += arr[i].back()-arr[i][0]; n      } n     } n  n     for(auto x : vect) n     { n      int d = x.second-x.first+1; n      T[x.first] += d; n      T[x.second+1] -= d; n     } n  n     for(int i = 1 ; i <= N ; i++) n     { n      T[i] += T[i-1]; n     } n  n     for(int i = 0 ; i < MAXN ; i++) n     { n      for(int j = 0 ; j < 120 ; j++) n      { n       dp[i][j] = -oo; n      } n     } n  n     for(int i = 1 ; i < N ; i++) n     { n      dp[i][0] = 0; n      dp[i][1] = T[i]; n     } n  n     for(int i = 0 ; i < vect.size() ; i++) n     { n      ins[vect[i].first].push_back(i); n      del[vect[i].second+1].push_back(i); n     } n  n     for(int j = 2 ; j <= K ; j++) n     {  n      build(1,1,N-1,j-1); n  n      for(int i = 1 ; i < N ; i++) n      { n       for(auto x : ins[i]) n       { n        int v = vect[x].second-vect[x].first+1; n        update(1,1,N-1,vect[x].first, vect[x].second, -v); n       } n       for(auto x : del[i]) n       { n        int v = vect[x].second-vect[x].first+1; n        update(1,1,N-1,vect[x].first, vect[x].second, v); n       } n       if(i >= j)dp[i][j] = query(1,1,N-1,1,i-1) + T[i]; n      } n     } n  n     int res = 0; n  n     for(int i = 1 ; i < N ; i++) n     { n      res = max(res, dp[i][K]); n     } n  n     cout << sum - res <<   n ; n  n     return 0; n }


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   int n; n   cin >> n; n   int a[n]; n   vector<int> c; n   for (int i = 0; i < n; i++) a[i] = 0; n   for (int i = 0; i < n; i++) { n     int k; n     cin >> k; n     c.push_back(k); n     a[k - 1] += 1; n   } n   queue<int> q; n   int ans = 0; n   for (int i = 0; i < n; i++) { n     if (a[i] == 0) { n       q.push(i + 1); n       ans++; n     } n   } n   bool done[n]; n   for (int i = 0; i < n; i++) done[i] = 0; n   for (int i = 0; i < n; i++) { n     if (q.empty()) break; n     if (done[c[i] - 1] == 1) { n       a[c[i] - 1]--; n       c[i] = q.front(); n       q.pop(); n     } else { n       if (a[c[i] - 1] == 1) { n         done[c[i] - 1] = 1; n         a[c[i] - 1]--; n       } else if (q.front() > c[i]) { n         done[c[i] - 1] = 1; n         a[c[i] - 1]--; n       } else { n         a[c[i] - 1]--; n         c[i] = q.front(); n         q.pop(); n       } n     } n   } n   cout << ans << endl; n   for (int i = 0; i < n; i++) cout << c[i] <<    ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long t, a, b, c, d; n long long divide(long long x, long long y) { return (x + y - 1) / y; } n int32_t main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   cin >> t; n   while (t--) { n     cin >> a >> b >> c >> d; n     if (b >= a) n       cout << b <<   n ; n     else if (d >= c) n       cout <<  -1 n ; n     else n       cout << divide(a - b, c - d) * c + b <<   n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int sum = 0, sum1 = 0, c = 0; n   int n, x, y, d; n   cin >> n; n   d = n; n   while (n--) { n     cin >> x; n     sum = sum + x; n     cin >> y; n     sum1 = sum1 + y; n     if (x % 2 == 0 && y % 2 == 1 || (y % 2 == 0 && x % 2 == 1)) c++; n   } n   cout << (sum % 2 == 0 && sum1 % 2 == 0 n                ? 0 n                : (sum % 2 == 1 && sum1 % 2 == 1 && c > 0 && d > 1 ? 1 : -1)) n        << endl; n } n


#include <bits/stdc++.h> n using namespace std; n const long long inf = ~0ULL >> 1; n const int maxV = 3e5 + 10; n const int maxE = 6e5 + 10; n struct EDGE { n   int v, nxt; n   long long w; n   EDGE() {} n   EDGE(int vv, int xx, long long ww) { v = vv, nxt = xx, w = ww; } n }; n EDGE edg[maxE]; n int EH[maxV], etot; n void initG() { n   memset(EH, -1, sizeof(EH)); n   etot = 0; n } n void addEdge(int u, int v, long long w) { n   edg[etot] = EDGE(v, EH[u], w); n   EH[u] = etot++; n } n int n, m; n short an[maxE]; n long long dist[maxV]; n short vs[maxV]; n struct Node { n   int src, id; n   long long dst; n   Node() {} n   Node(int ss, long long dd, int ii) { src = ss, dst = dd, id = ii; } n   bool operator<(const Node &rhs) const { n     return (rhs.dst < dst) || (dst == rhs.dst && edg[rhs.id].w < edg[id].w); n   } n }; n void spfa(int s) { n   for (int i = 1; i <= n; i++) dist[i] = inf; n   memset(vs, 0, sizeof(vs)); n   memset(an, 0, sizeof(an)); n   priority_queue<Node> que; n   dist[s] = 0; n   que.push(Node(s, dist[s], -1)); n   long long rs = 0; n   while (!que.empty()) { n     Node now = que.top(); n     que.pop(); n     int u = now.src; n     int id = now.id; n     if (vs[u]) continue; n     vs[u] = 1; n     if (id != -1) { n       rs += edg[id].w; n       an[id] = 1; n     } n     for (int p = EH[u]; ~p; p = edg[p].nxt) { n       int v = edg[p].v; n       long long w = edg[p].w; n       if (dist[v] >= dist[u] + w) { n         dist[v] = dist[u] + w; n         que.push(Node(v, dist[v], p)); n       } n     } n   } n   cout << rs << endl; n   m <<= 1; n   for (int i = 0; i < m; i++) n     if (an[i]) cout << (i / 2 + 1) <<    ; n   cout << endl; n } n int main() { n   int u, v, p; n   long long w; n   while (~scanf( %d%d , &n, &m)) { n     initG(); n     for (int i = 1; i <= m; i++) { n       scanf( %d%d%I64d , &u, &v, &w); n       addEdge(u, v, w); n       addEdge(v, u, w); n     } n     scanf( %d , &p); n     spfa(p); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 2e5 + 58, SQ = 500; n int LE[maxn], LO[maxn], par[maxn], a[maxn], root[maxn], lazy[maxn], m; n inline void make(int j) { n   int lase = -1, laso = -1; n   int l = j * SQ; n   for (int i = l; i - l < SQ; i++) { n     a[i] ^= lazy[j]; n     if (a[i]) n       laso = i; n     else n       lase = i; n     LE[i] = lase; n     LO[i] = laso; n     if (i - l >= m + 1) { n       if (a[i]) n         par[i] = LO[i - (m + 1)]; n       else n         par[i] = LE[i - (m + 1)]; n     } else n       par[i] = -1; n     if (par[i] == -1) n       root[i] = i; n     else n       root[i] = root[par[i]]; n   } n   lazy[j] = 0; n } n inline int getlast(int r) { n   while ((lazy[r / SQ] && (LE[r] == -1 || LE[r] / SQ != r / SQ)) || n          (!lazy[r / SQ] && (LO[r] == -1 || LO[r] / SQ != r / SQ))) { n     r = r / SQ * SQ - 1; n   } n   if (lazy[r / SQ]) return LE[r]; n   return LO[r]; n } n inline bool P(int r, int l) { n   while (r / SQ > l / SQ) { n     r = root[r]; n     r = getlast(r - m - 1); n   } n   while (r > l) { n     r = par[r]; n   } n   return r == l; n } n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   int n, q; n   cin >> n >> m >> q; n   int lase = -1, laso = -1; n   for (int i = 0; i < n; i++) { n     long long b; n     cin >> b; n     b &= 1; n     a[i] = b; n     if (b) n       laso = i; n     else n       lase = i; n     LE[i] = lase; n     LO[i] = laso; n     if (i % SQ >= m + 1) { n       if (b) n         par[i] = LO[i - (m + 1)]; n       else n         par[i] = LE[i - (m + 1)]; n       if (par[i] == -1 || par[i] / SQ < i / SQ) par[i] = -1; n     } else n       par[i] = -1; n     if (par[i] == -1) n       root[i] = i; n     else n       root[i] = root[par[i]]; n   } n   while (q--) { n     int t; n     cin >> t; n     if (t == 1) { n       int l, r; n       long long d; n       cin >> l >> r >> d; n       d &= 1; n       l--; n       r--; n       if (!d) continue; n       for (int i = l / SQ + 1; i < r / SQ; i++) { n         lazy[i] ^= 1; n       } n       if (l / SQ != r / SQ) { n         for (int i = l; i / SQ == l / SQ; i++) { n           a[i] ^= 1; n         } n         for (int i = r / SQ * SQ; i <= r; i++) { n           a[i] ^= 1; n         } n         make(l / SQ); n         make(r / SQ); n       } else { n         for (int i = l; i <= r; i++) { n           a[i] ^= 1; n         } n         make(l / SQ); n       } n     } else { n       int l, r; n       cin >> l >> r; n       l--; n       r--; n       if (a[l] == lazy[l / SQ]) { n         cout <<  1 n ; n         continue; n       } n       int x = getlast(r); n       if (P(x, l)) n         cout <<  2 n ; n       else n         cout <<  1 n ; n     } n   } n } n


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


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


#include <bits/stdc++.h> n using namespace std; n long long max(long long x, long long y) { return x > y ? x : y; } n long long min(long long x, long long y) { return x < y ? x : y; } n long long gcd(long long p, long long q) { n   if (q == 0) return p; n   return gcd(q, p % q); n } n bool check(long long b, long long p) { n   long long m; n   while (gcd(p, b) != 1) { n     m = gcd(p, b); n     while (p % m == 0) p = p / m; n     b = m; n   } n   if (p == 1) n     return true; n   else n     return false; n } n int main() { n   long long p, q, b; n   int n; n   scanf( %d , &n); n   while (n--) { n     scanf( %I64d%I64d%I64d , &p, &q, &b); n     if (p == 0) { n       printf( Finite n ); n       continue; n     } n     long long mmax = max(p, q); n     long long mmin = min(p, q); n     long long GCD = gcd(mmax, mmin); n     p = q / GCD; n     if (check(b, p)) n       printf( Finite n ); n     else n       printf( Infinite n ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n inline long long fexp(long long b, long long e) { n   long long x = 1; n   for (; e; e >>= 1, b = (b * b) % (1000000007)) n     if (e & 1) x = (x * b) % (1000000007); n   return x; n } n long long n, k, ok, i, j, x; n set<long long> S; n signed main() { n   ios_base::sync_with_stdio(0); n   cin >> n >> k; n   ok = 1; n   for (x = 1; x <= k && ok; x++) { n     S.clear(); n     for (i = 1; i <= x; i++) { n       if (S.count(n % i)) n         ok = 0; n       else n         S.insert(n % i); n     } n   } n   cout << (ok ?  Yes  :  No ) << endl; n   return 0; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int i = 0; n   string s; n   cin >> s; n   if (s[0] >=  a  && s[0] <=  z ) s[0] = char(s[0] - 32); n   cout << s << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const static int INF = 1000000000; n int t; n string input; n map<string, string> apa; n int main() { n   scanf( %d , &t); n   string a, b; n   string v =  void ; n   apa[v] =  void ; n   while (t--) { n     cin >> input; n     if (input ==  typedef ) { n       cin >> a >> b; n       string tipe =   ; n       int bintang = 0, dan = 0; n       for (int i = 0; a[i] !=   0 ; i++) { n         if (a[i] ==  * ) n           bintang++; n         else if (a[i] ==  & ) n           dan++; n         else n           tipe += a[i]; n       } n       string bener = apa[tipe]; n       if (bener ==  errtype  || tipe ==  errtype ) { n         apa[b] =  errtype ; n         continue; n       } n       if (bintang >= dan) { n         for (int i = 0; i < bintang - dan; i++) { n           bener +=  * ; n         } n         apa[b] = bener; n       } else if (dan > bintang) { n         string ganti =   ; n         dan = dan - bintang; n         for (int i = 0; bener[i] !=   0 ; i++) { n           if (bener[i] ==  * ) { n             if (dan > 0) n               dan--; n             else { n               ganti +=  * ; n             } n           } else n             ganti += bener[i]; n         } n         if (dan == 0) n           apa[b] = ganti; n         else n           apa[b] =  errtype ; n       } n     } else { n       cin >> a; n       string tipe =   ; n       int bintang = 0, dan = 0; n       for (int i = 0; a[i] !=   0 ; i++) { n         if (a[i] ==  * ) n           bintang++; n         else if (a[i] ==  & ) n           dan++; n         else n           tipe += a[i]; n       } n       string bener = apa[tipe]; n       if (bener ==  errtype  || bener ==   ) { n         printf( errtype n ); n         continue; n       } n       if (bintang >= dan) { n         for (int i = 0; i < bintang - dan; i++) { n           bener +=  * ; n         } n         printf( %s n , bener.c_str()); n       } else if (dan > bintang) { n         string ganti =   ; n         dan = dan - bintang; n         for (int i = 0; bener[i] !=   0 ; i++) { n           if (bener[i] ==  * ) { n             if (dan > 0) n               dan--; n             else { n               ganti +=  * ; n             } n           } else n             ganti += bener[i]; n         } n         if (dan == 0) n           printf( %s n , ganti.c_str()); n         else n           printf( errtype n ); n       } n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long pos = 0; n   long long distance; n   int n, flag = 1; n   char dir[20]; n   cin >> n; n   for (int j = 0; j < n; j++) { n     cin >> distance >> dir; n     if (pos == 0 && strcmp(dir,  South ) != 0) { n       flag = 0; n     } else if (pos == 20000 && strcmp(dir,  North ) != 0) { n       flag = 0; n     } else { n       if (strcmp(dir,  South ) == 0) { n         pos = pos + distance; n         if (pos > 20000) flag = 0; n       } else if (strcmp(dir,  North ) == 0) { n         pos = pos - distance; n         if (pos < 0) { n           flag = 0; n         } n       } n     } n   } n   if (pos) flag = 0; n   if (flag) n     cout <<  YES ; n   else n     cout <<  NO ; n   return 0; n } n


#include <bits/stdc++.h> n #pragma GCC optimize  Ofast  n #pragma GCC optimize  unroll-loops  n #pragma GCC target  sse,sse2,sse3,sse4,abm,avx,mmx,popcnt,tune=native  n using namespace std; n class Debugger { n  public: n   Debugger(const std::string& _separator =   -  ) n       : first(true), separator(_separator) {} n   template <typename ObjectType> n   Debugger& operator,(const ObjectType& v) { n     if (!first) n     std: n       cerr << separator; n     std::cerr << v; n     first = false; n     return *this; n   } n   ~Debugger() { n   std: n     cerr << endl; n   } n  n  private: n   bool first; n   std::string separator; n }; n long long int n, k, d; n set<pair<long long int, long long int> > ans; n long long int s; n void dfs(long long int u, long long int dad, long long int d, n          long long int rem) { n   ans.insert({dad, u}); n   if (d) { n     for (long long int i = 0; i < rem && s < n; i++) dfs(++s, u, d - 1, k - 1); n   } n } n int32_t main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   cin >> n >> d >> k; n   if (n <= d || d > 1 && k < 2) { n     cout <<  NO ; n     return 0; n   } n   for (s = 1; s <= d; s++) ans.insert({s, s + 1}); n   for (long long int i = 2; i <= d && s < n; i++) { n     dfs(i, i - 1, min(i - 1, d + 1 - i), k - 2); n   } n   if (s < n) { n     cout <<  NO ; n     return 0; n   } n   cout <<  YES n ; n   for (auto it : ans) cout << it.first <<     << it.second <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long(n), (k); n   scanf( %lld%lld , &(n), &(k)); n   ; n   long long n2 = n; n   vector<long long> vec; n   while (n) { n     vec.push_back(n); n     if (n - 1 > 0) vec.push_back(n - 1); n     if (n - 2 > 0) vec.push_back(n - 2); n     if (n - 3 > 0) vec.push_back(n - 3); n     n /= 2; n   } n   long long maxi = 1; n   auto allok = [&](long long v) { n     long long mi = v, ma = (v % 2) ? v : v + 1; n     long long c = ma - mi + 1; n     while (ma <= n2) { n       mi *= 2; n       ma = 2 * ma + 1; n       if (mi < n2) c += min(ma, n2) - mi + 1; n     } n     return c >= k; n   }; n   for (auto i : vec) { n     if (allok(i)) maxi = max(maxi, i); n   } n   printf( %lld n , maxi); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long int mod = 1000000007; n const long long int inf = 0x3f3f3f3f3f3f3f3fLL; n const double eps = 1e-9; n const double PI = acos(-1); n long long int i, j, k; n void solve(void); n inline bool equal(double a, double b) { return fabs(a - b) < eps; } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout << fixed << setprecision(10); n   int t = 1; n   while (t--) solve(); n   return 0; n } n long long int m; n unordered_map<long long int, long long int> f; n long long int fib(long long int n) { n   if (n == 0) return 0; n   if (n == 1 || n == 2) return (f[n] = 1) % m; n   if (f[n]) return f[n]; n   long long int k = (n & 1) ? (n + 1) / 2 : n / 2; n   f[n] = (n & 1) ? ((fib(k) * fib(k)) % m + (fib(k - 1) * fib(k - 1)) % m) % m n                  : (((2 * fib(k - 1)) % m + fib(k)) % m * fib(k)) % m; n   return f[n]; n } n void solve() { n   long long int l, r, k; n   cin >> m >> l >> r >> k; n   long long int d = (r - l) / (k - 1); n   while (d > 1) { n     if (1 + r / d - (l + d - 1) / d >= k) break; n     long long int N = 1 + r / d; n     d -= ((N * d - r) + (N - 1)) / N; n   } n   cout << fib(d) <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n char str[100010]; n long long n, l[100010], r[100010], ofs, stk[100010], sa[100010], rk[100010], n     tmp[100010], hei[100010]; n vector<long long> ll[100010], rr[100010]; n void build_sa(char *str, long long sa[]); n void build_hei(char *str, long long sa[], long long hei[]); n int main() { n   scanf( %s , str); n   n = strlen(str); n   build_sa(str, sa); n   build_hei(str, sa, hei); n   long long i, k; n   for (k = 0, i = 1; i <= n - 1; i++) { n     while (k && hei[stk[k]] >= hei[i]) k--; n     l[i] = (k ? stk[k] + 1 : 1); n     stk[++k] = i; n   } n   for (k = 0, i = n - 1; i >= 1; i--) { n     while (k && hei[stk[k]] >= hei[i]) k--; n     r[i] = (k ? stk[k] - 1 : n - 1); n     stk[++k] = i; n   } n   for (i = 1; i <= n - 1; i++) n     if (hei[i] && (ll[hei[i]].empty() || l[i] != ll[hei[i]].back())) { n       ll[hei[i]].push_back(l[i]); n       rr[hei[i]].push_back(r[i]); n     } n   long long ans = 0, p, q; n   for (hei[0] = hei[n] = 0, i = 1; i <= n - 1; i++) n     while (!ll[i].empty()) { n       p = ll[i].back(); n       q = rr[i].back(); n       ans += (q - p + 2) * (q - p + 3) / 2 * (i - max(hei[p - 1], hei[q + 1])); n       ll[i].pop_back(); n       rr[i].pop_back(); n     } n   for (i = 1; i <= n; i++) ans += n - sa[i] + 1 - max(hei[i], hei[i - 1]); n   printf( %I64d n , ans); n   return 0; n } n bool cmp(long long i, long long j) { n   if (rk[i] != rk[j]) return rk[i] < rk[j]; n   long long ri = (i + ofs <= n ? rk[i + ofs] : -1), n             rj = (j + ofs <= n ? rk[j + ofs] : -1); n   return ri < rj; n } n void build_sa(char *str, long long sa[]) { n   long long i; n   for (i = 1; i <= n; i++) { n     sa[i] = i; n     rk[i] = str[i - 1]; n   } n   for (ofs = 1; ofs <= n; ofs *= 2) { n     sort(sa + 1, sa + n + 1, cmp); n     for (tmp[sa[1]] = 1, i = 2; i <= n; i++) n       tmp[sa[i]] = tmp[sa[i - 1]] + (cmp(sa[i - 1], sa[i]) ? 1 : 0); n     for (i = 1; i <= n; i++) rk[i] = tmp[i]; n   } n } n void build_hei(char *str, long long sa[], long long hei[]) { n   long long h = 0, i, j; n   for (i = 1; i <= n; i++) { n     if (rk[i] == n) continue; n     j = sa[rk[i] + 1]; n     if (h) h--; n     for (; j + h <= n && i + h <= n; h++) n       if (str[j + h - 1] != str[i + h - 1]) break; n     hei[rk[i]] = h; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int mod = 1e9 + 7, A = 5e5 + 5; n vector<vector<long long>> v; n long long n, m, val; n long long ans[A]; n bool vis[A], vis2[A]; n void DFS(long long node) { n   if (vis[node]) return; n   vis[node] = 1; n   for (auto child : v[node]) { n     if (vis[child]) continue; n     val++; n     DFS(child); n   } n } n void DFSmemo(long long node) { n   if (vis2[node]) return; n   vis2[node] = 1; n   for (auto child : v[node]) { n     if (vis2[child]) continue; n     ans[child] = val; n     DFSmemo(child); n   } n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   cin >> n >> m; n   v.resize(n + 2); n   for (int i = 0; i < m; i++) { n     long long g, x, y; n     cin >> g; n     if (g != 0) cin >> x; n     for (int j = 1; j < g; j++) { n       cin >> y; n       v[x].push_back(y), v[y].push_back(x); n     } n   } n   for (int i = 1; i <= n; i++) { n     if (ans[i] == 0) { n       val = 1; n       DFS(i); n       DFSmemo(i); n     } else n       val = ans[i]; n     cout << val <<    ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, m; n   cin >> n >> m; n   vector<set<int> > v(n); n   vector<set<int> > w(n); n   for (int i = 0; i < m; i++) { n     int a, b; n     cin >> a >> b; n     a--; n     b--; n     if (a > b) n       v[a].insert(b); n     else n       v[b].insert(a); n     if (a > b) n       w[b].insert(a); n     else n       w[a].insert(b); n   } n   vector<int> pairs(n), pairs2(n); n   pairs[0] = v[0].size(); n   for (int i = 1; i < n; i++) pairs[i] = pairs[i - 1] + v[i].size(); n   pairs2[n - 1] = 0; n   for (int i = n - 2; i >= 0; i--) pairs2[i] = pairs2[i + 1] + w[i].size(); n   long long ans = 0; n   for (int i = 0; i < n - 1; i++) n     if (pairs[i] == 0 && pairs2[i + 1] == 0) ans++; n   cout << ans <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long cnt(long long a, long long b, long long c) { n   if (a > b || b > c) return 0LL; n   if (a == b && b == c) return 1LL; n   if (a == b && b < c) return 3LL; n   if (a < b && b == c) return 3LL; n   if (a < b && b < c) return 6LL; n } n int main() { n   long long n; n   scanf( %lld , &n); n   if (n % 3LL) n     printf( 0 n ); n   else { n     long long ans = 0LL; n     n /= 3LL; n     for (long long sab = 1LL; sab * sab * sab <= n; sab++) n       if (n % sab == 0LL) { n         for (long long a = 1LL; a <= sab - a; a++) { n           long long b = sab - a; n           long long det = sab * sab - 4LL * a * b + 4LL * n / sab; n           if (det < 0LL) continue; n           long long sqdet = 0LL, dd = 1LL << 30LL; n           while (dd) { n             long long nsqdet = sqdet + dd; n             if (nsqdet * nsqdet <= det) sqdet = nsqdet; n             dd >>= 1LL; n           } n           if (sqdet * sqdet == det) { n             long long c = sqdet - sab; n             if (c % 2LL == 0) { n               c /= 2LL; n               ans += cnt(a, b, c); n             } n           } n         } n       } n     printf( %lld n , ans); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 1e6; n const int md = 1000000009; n int n; n long long two[maxn + 5], f[maxn + 5], ans; n int main() { n   two[0] = 1; n   for (int i = 1; i <= maxn; ++i) two[i] = two[i - 1] * 2 % md; n   scanf( %d , &n); n   f[n] = 2; n   for (int i = n - 1; i >= 2; --i) { n     if (i & 1) n       f[i] = (f[i + 1] + 2) % md; n     else { n       int k = i >> 1; n       f[i] = (2 + ((two[k - 1] - 1 + md) % md * 4 + 1) * f[i + 1]) % md; n     } n   } n   f[1] = (1 + f[2] * f[2]) % md; n   ans = f[1] * 2 % md; n   printf( %lld n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long gcd1(long long a, long long b) { n   if (a == 0) return b; n   return gcd1(b % a, a); n } n long long modx(long long base, long long ex) { n   long long ans = 1LL, val = base; n   while (ex > 0LL) { n     if (ex & 1LL) ans = (ans * val) % 998244353LL; n     val = (val * val) % 998244353LL; n     ex = ex >> 1LL; n   } n   return ans; n } n const int maxn = 5e5 + 10; n int a[maxn], n, c; n int pre[maxn], suff[maxn]; n int cnt[maxn], ans; n vector<int> v[maxn]; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   cin >> n >> c; n   for (int i = 1; i <= n; i++) { n     cin >> a[i]; n     v[a[i]].push_back(i); n     if (a[i] == c) n       cnt[i] = cnt[i - 1] + 1; n     else n       cnt[i] = cnt[i - 1]; n   } n   ans = cnt[n]; n   for (int i = 1; i < maxn; i++) { n     if (i == c) continue; n     if (v[i].size() == 0) continue; n     for (int j = 0; j < v[i].size(); j++) { n       pre[j] = cnt[v[i][j]]; n       pre[j]++; n       if (j > 0) pre[j] = max(pre[j], pre[j - 1] + 1); n     } n     for (int j = v[i].size() - 1; j >= 0; j--) { n       suff[j] = cnt[n] - cnt[v[i][j]]; n       suff[j]++; n       if (j + 1 < v[i].size()) suff[j] = max(suff[j], suff[j + 1] + 1); n     } n     for (int j = 0; j < v[i].size(); j++) { n       ans = max(ans, pre[j] + suff[j] - 1); n     } n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void solve() { n   int n; n   cin >> n; n   int base = sqrt(n); n   if (base * base == n) { n     cout << base <<     << 1 << endl; n     return; n   } n   n = n - base * base; n   if (n <= base + 1) { n     cout << n <<     << base + 1 << endl; n   } else n     cout << base + 1 <<     << (2 * (base + 1) - n) << endl; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   int t; n   cin >> t; n   while (t--) { n     solve(); n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   string a, s; n   cin >> a >> s; n   sort(s.begin(), s.end()); n   reverse(s.begin(), s.end()); n   int k = 0; n   for (int i = 0; i < a.size() && i < s.size(); i++) { n     bool found = false; n     for (int j = k; j < a.size(); j++) { n       if (s[i] > a[j]) { n         a[j] = s[i]; n         found = true; n         k = j; n         break; n       } n     } n     if (!found) break; n   } n   cout << a << endl; n } n


#include <bits/stdc++.h> n using namespace std; n bool sort_d(long long x, long long y) { return x > y; } n int gcd(long long x, long long y) { n   if (!y) n     return x; n   else n     return gcd(y, x % y); n } n int lcm(long long x, long long y) { return (x * y) / gcd(x, y); } n int main() { n   int n, m, a, b; n   std::vector<int> Cs, Ce, Ps, Pe; n   cin >> n; n   for (int i = 0; i < n; i++) { n     cin >> a >> b; n     Cs.push_back(a); n     Ce.push_back(b); n   } n   cin >> m; n   for (int i = 0; i < m; i++) { n     cin >> a >> b; n     Ps.push_back(a); n     Pe.push_back(b); n   } n   sort(Cs.begin(), Cs.end()); n   sort(Ce.begin(), Ce.end()); n   sort(Ps.begin(), Ps.end()); n   sort(Pe.begin(), Pe.end()); n   a = max(Ps[Ps.size() - 1] - Ce[0], 0); n   b = max(Cs[Cs.size() - 1] - Pe[0], 0); n   cout << max(a, b) << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n inline int ni() { n   int a; n   scanf( %d , &a); n   return a; n } n inline double nf() { n   double a; n   scanf( %lf , &a); n   return a; n } n template <class T> n void out(T a, T b) { n   bool first = true; n   for (T i = a; i != b; i++) { n     if (!first) printf(   ); n     first = false; n     cout << *i; n   } n   puts(  ); n } n template <class T> n void outl(T a, T b) { n   for (T i = a; i != b; i++) cout << *i <<   n ; n } n int n, m; n vector<pair<int, int> > v; n map<int, vector<int> > p; n set<pair<int, int> > s; n vector<pair<int, int> > cur; n vector<pair<int, int> > w; n int mx; n int main() { n   n = ni(); n   m = ni(); n   v.resize(m); n   for (int i = 0; i < (int)(m); i++) { n     v[i].first = ni(); n     v[i].second = ni(); n     p[v[i].first].push_back(v[i].second); n     mx = max(mx, v[i].first); n   } n   for (map<int, vector<int> >::iterator it = p.begin(); it != p.end(); it++) n     sort((it->second).begin(), (it->second).end()); n   s.insert(make_pair(1, 1)); n   int last = 0; n   for (map<int, vector<int> >::iterator it = p.begin(); it != p.end();) { n     if (s.empty()) break; n     cur.clear(); n     if (last + 1 < it->first) { n       cur.push_back(make_pair(1, n)); n       last = it->first - 1; n     } else { n       int f = 1, lst = n; n       for (int i = 0; i < (int)(((int)(it->second).size())); i++) { n         if (f < it->second[i]) { n           cur.push_back(make_pair(f, it->second[i] - 1)); n         } n         f = it->second[i] + 1; n       } n       if (f <= lst) cur.push_back(make_pair(f, lst)); n       last = it->first; n       it++; n     } n     int d = 0; n     w.clear(); n     for (set<pair<int, int> >::iterator sit = s.begin(); n          sit != s.end() && d < ((int)(cur).size());) { n       while (d < ((int)(cur).size()) && cur[d].second < sit->first) d++; n       if (d < ((int)(cur).size()) && cur[d].first <= sit->second) { n         w.push_back(make_pair(max(cur[d].first, sit->first), cur[d].second)); n         d++; n       } else { n         sit++; n       } n     } n     s.clear(); n     for (int i = 0; i < (int)(((int)(w).size())); i++) s.insert(w[i]); n   } n   if (!s.empty()) { n     if (last == n) { n       if (s.rbegin()->second == n) n         cout << 2 * (n - 1) << endl; n       else n         puts( -1 ); n     } else { n       cout << 2 * (n - 1) << endl; n     } n   } else { n     cout << -1 << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const long long mod = 998244353; n int solve(); n void precomp(); n int main() { n   ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); n   int t = 1; n   if (0) cin >> t; n   precomp(); n   for (int tc = 1; tc <= t; tc++) { n     if (0 && 0) cout <<  Case #  << tc <<  :  ; n     solve(); n   } n } n void precomp() { return; } n int cnt[26][26], ans[26], tmp[26]; n string s; n int solve() { n   cin >> s; n   for (int i = 1; i <= s.size(); i++) { n     memset(cnt, 0, sizeof(cnt)); n     memset(tmp, 0, sizeof(tmp)); n     for (int j = 0; j < s.size(); j++) n       cnt[s[j] -  a ][s[(j + i) % s.size()] -  a ]++; n     for (int j = 0; j < s.size(); j++) n       if (cnt[s[j] -  a ][s[(i + j) % s.size()] -  a ] == 1) tmp[s[j] -  a ]++; n     for (int j = 0; j < 26; j++) ans[j] = max(ans[j], tmp[j]); n   } n   int tot = 0; n   for (int i = 0; i < 26; i++) tot += ans[i]; n   long double ans = (long double)tot / (long double)s.size(); n   cout << fixed << setprecision(10); n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long t; n   cin >> t; n   while (t--) { n     long long a, b, c; n     cin >> a >> b >> c; n     cout << a + b + c - 1 << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename Arg1> n void __f(const char* name, Arg1&& arg1) { n   cerr << name <<   :   << arg1 << std::endl; n } n template <typename Arg1, typename... Args> n void __f(const char* names, Arg1&& arg1, Args&&... args) { n   const char* comma = strchr(names + 1,  , ); n   cerr.write(names, comma - names) <<   :   << arg1 <<   |  ; n   __f(comma + 1, args...); n } n const int MOD = 1e9 + 7; n int main() { n   ios::sync_with_stdio(0), cin.tie(0), cout.tie(0); n   long long l, r; n   long long ans = -1; n   cin >> l >> r; n   for (int i = 0; l > 0 || r > 0; ++i) { n     if ((l & 1) != (r & 1)) ans = i; n     l >>= 1; n     r >>= 1; n   } n   cout << (1LL << (ans + 1)) - 1 <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n double __begin; n template <typename T1, typename T2, typename T3> n struct triple { n   T1 a; n   T2 b; n   T3 c; n   triple(){}; n   triple(T1 _a, T2 _b, T3 _c) : a(_a), b(_b), c(_c) {} n }; n template <typename T1, typename T2, typename T3> n bool operator<(const triple<T1, T2, T3>& t1, const triple<T1, T2, T3>& t2) { n   if (t1.a != t2.a) n     return t1.a < t2.a; n   else if (t1.b != t2.b) n     return t1.b < t2.b; n   else n     return t1.c < t2.c; n } n template <typename T1, typename T2, typename T3> n inline std::ostream& operator<<(std::ostream& os, const triple<T1, T2, T3>& t) { n   return os <<  (  << t.a <<  ,   << t.b <<  ,   << t.c <<  ) ; n } n inline int bits_count(int v) { n   v = v - ((v >> 1) & 0x55555555); n   v = (v & 0x33333333) + ((v >> 2) & 0x33333333); n   return ((v + (v >> 4) & 0xF0F0F0F) * 0x1010101) >> 24; n } n inline int bits_count(long long v) { n   int t = v >> 32; n   int p = (v & ((1LL << 32) - 1)); n   return bits_count(t) + bits_count(p); n } n unsigned int reverse_bits(register unsigned int x) { n   x = (((x & 0xaaaaaaaa) >> 1) | ((x & 0x55555555) << 1)); n   x = (((x & 0xcccccccc) >> 2) | ((x & 0x33333333) << 2)); n   x = (((x & 0xf0f0f0f0) >> 4) | ((x & 0x0f0f0f0f) << 4)); n   x = (((x & 0xff00ff00) >> 8) | ((x & 0x00ff00ff) << 8)); n   return ((x >> 16) | (x << 16)); n } n inline int sign(int x) { return x > 0; } n inline bool isPowerOfTwo(int x) { return (x != 0 && (x & (x - 1)) == 0); } n template <typename T1, typename T2> n inline std::ostream& operator<<(std::ostream& os, const std::pair<T1, T2>& p) { n   return os <<  (  << p.first <<  ,   << p.second <<  ) ; n } n template <typename T> n inline std::ostream& operator<<(std::ostream& os, const std::vector<T>& v) { n   bool first = true; n   os <<  [ ; n   for (unsigned int i = 0; i < v.size(); i++) { n     if (!first) os <<  ,  ; n     os << v[i]; n     first = false; n   } n   return os <<  ] ; n } n template <typename T> n inline std::ostream& operator<<(std::ostream& os, const std::set<T>& v) { n   bool first = true; n   os <<  [ ; n   for (typename std::set<T>::const_iterator ii = v.begin(); ii != v.end(); n        ++ii) { n     if (!first) os <<  ,  ; n     os << *ii; n     first = false; n   } n   return os <<  ] ; n } n template <typename T1, typename T2> n inline std::ostream& operator<<(std::ostream& os, const std::map<T1, T2>& v) { n   bool first = true; n   os <<  [ ; n   for (typename std::map<T1, T2>::const_iterator ii = v.begin(); ii != v.end(); n        ++ii) { n     if (!first) os <<  ,  ; n     os << *ii; n     first = false; n   } n   return os <<  ] ; n } n template <typename T, typename T2> n void printarray(T a[], T2 sz, T2 beg = 0) { n   for (T2 i = beg; i < sz; i++) cout << a[i] <<    ; n   cout << endl; n } n inline long long mulmod(long long x, long long n, long long _mod) { n   long long res = 0; n   while (n) { n     if (n & 1) res = (res + x) % _mod; n     x = (x + x) % _mod; n     n >>= 1; n   } n   return res; n } n inline long long powmod(long long x, long long n, long long _mod) { n   long long res = 1; n   while (n) { n     if (n & 1) res = (res * x) % _mod; n     x = (x * x) % _mod; n     n >>= 1; n   } n   return res; n } n inline long long gcd(long long a, long long b) { n   long long t; n   while (b) { n     a = a % b; n     t = a; n     a = b; n     b = t; n   } n   return a; n } n inline int gcd(int a, int b) { n   int t; n   while (b) { n     a = a % b; n     t = a; n     a = b; n     b = t; n   } n   return a; n } n inline long long lcm(long long a, long long b) { return a / gcd(a, b) * b; } n inline long long gcd(long long a, long long b, long long c) { n   return gcd(gcd(a, b), c); n } n inline int gcd(int a, int b, int c) { return gcd(gcd(a, b), c); } n template <class T> n inline void cinarr(T& a, int n) { n   for (int i = 0; i < n; ++i) cin >> a[i]; n } n int main() { n   int n, c_mx = -1, c_mn = 1e9, ans = 0; n   scanf( %d , &n); n   vector<int> a(n), p(n), s(n); n   for (int i = 0; i < (n); ++i) scanf( %d , &a[i]); n   for (int i = 0; i < (n); ++i) { n     if (a[i] > c_mx) c_mx = a[i]; n     p[i] = c_mx; n   } n   for (int i = (n)-1; i >= (0); --i) { n     if (a[i] < c_mn) c_mn = a[i]; n     s[i] = c_mn; n   } n   for (int i = 0; i < (n - 1); ++i) ans += (p[i] <= s[i + 1]); n   printf( %d , ans + 1); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int a, n, d, t, v; n double ans[100001]; n double gets_ans() { n   double t1 = v * 1.0 / a; n   if (0.5 * a * t1 * t1 <= d) return t + t1 + (d - 0.5 * a * t1 * t1) / v; n   return sqrt(2.0 * d / a) + t; n } n int main() { n   cin >> n >> a >> d; n   for (int i = 0; i < n; i++) { n     cin >> t >> v; n     ans[i] = gets_ans(); n     if (i != 0 && ans[i] < ans[i - 1]) ans[i] = ans[i - 1]; n     printf( %.10lf n , ans[i]); n   } n } n


#include <bits/stdc++.h> n using namespace std; n struct dsu { n   vector<int> p; n   vector<int> q; n   vector<int> pre; n   vector<int> nxt; n   vector<int> sz; n   int n; n   dsu(int _n) : n(_n) { n     p.resize(n); n     iota(p.begin(), p.end(), 0); n     q = p; n     sz.assign(n, 1); n     pre.assign(n, -1); n     nxt.assign(n, -1); n   } n   inline int get(int x) { n     if (p[x] == x) { n       return x; n     } else { n       return p[x] = get(p[x]); n     } n   } n   inline bool unite(int x, int y) { n     x = get(x); n     y = get(y); n     if (x == y) { n       return false; n     } n     if (sz[x] > sz[y]) { n       swap(x, y); n     } n     pre[x] = q[y]; n     nxt[q[y]] = x; n     p[x] = y; n     q[y] = q[x]; n     sz[y] += sz[x]; n     return true; n   } n   inline bool same(int x, int y) { return (get(x) == get(y)); } n }; n template <class T> n struct segtree { n   int n; n   T ie = -1; n   vector<T> node; n   function<T(T, T)> f = [&](T a, T b) { return max(a, b); }; n   segtree() : segtree(0) {} n   segtree(int _n) : segtree(vector<T>(_n, ie)) {} n   segtree(vector<T> v) { n     n = 1 << (32 - __builtin_clz((int)v.size() - 1)); n     node.resize(2 * n - 1, ie); n     for (int i = 0; i < (int)v.size(); i++) { n       node[i + n - 1] = v[i]; n     } n     for (int i = n - 2; i >= 0; i--) { n       node[i] = f(node[2 * i + 1], node[2 * i + 2]); n     } n   } n   void update(int x, T v) { n     x += n - 1; n     node[x] = v; n     while (x > 0) { n       x = (x - 1) / 2; n       node[x] = f(node[2 * x + 1], node[2 * x + 2]); n     } n   } n   T get(int a, int b, int x, int l, int r) { n     if (r <= a || b <= l) { n       return ie; n     } n     if (a <= l && r <= b) { n       return node[x]; n     } n     T vl = get(a, b, 2 * x + 1, l, (l + r) / 2); n     T vr = get(a, b, 2 * x + 2, (l + r) / 2, r); n     return f(vl, vr); n   } n   T get(int a, int b) { return get(a, b, 0, 0, n); } n   T get(int a) { return node[a + n - 1]; } n }; n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   int n, m, q; n   cin >> n >> m >> q; n   vector<int> p(n); n   for (int i = 0; i < n; i++) { n     cin >> p[i]; n   } n   vector<pair<int, int>> e(m); n   for (int i = 0; i < m; i++) { n     cin >> e[i].first >> e[i].second; n     e[i].first--, e[i].second--; n   } n   vector<int> del(m); n   vector<pair<int, int>> t(q); n   for (int i = 0; i < q; i++) { n     cin >> t[i].first >> t[i].second; n     t[i].first--, t[i].second--; n     if (t[i].first == 1) { n       del[t[i].second] = 1; n     } n   } n   dsu uf(n); n   for (int i = 0; i < m; i++) { n     if (!del[i]) { n       uf.unite(e[i].first, e[i].second); n     } n   } n   for (int i = q - 1; i >= 0; i--) { n     if (t[i].first == 1) { n       if (del[t[i].second]) { n         uf.unite(e[t[i].second].first, e[t[i].second].second); n       } n       t[i].first = -1; n     } else { n       int v = uf.get(t[i].second); n       t[i] = make_pair(v, uf.q[v]); n     } n   } n   vector<int> a; n   for (int i = 0; i < n; i++) { n     if (uf.get(i) == i) { n       int v = i; n       while (v != -1) { n         a.emplace_back(v); n         v = uf.nxt[v]; n       } n     } n   }; n   vector<int> b(n); n   vector<int> c(n); n   vector<int> d(n + 1, -1); n   for (int i = 0; i < n; i++) { n     b[a[i]] = i; n     c[i] = p[a[i]]; n     d[c[i]] = i; n   } n   segtree<int> st(c); n   for (int i = 0; i < q; i++) { n     if (t[i].first == -1) { n       continue; n     } n     int l = b[t[i].first], r = b[t[i].second]; n     int res = st.get(l, r + 1); n     cout << res <<   n ; n     if (res) { n       st.update(d[res], 0); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n #pragma comment(linker,  /STACK:100000000000000 ) n using namespace std; n long long n, cnt[5], fct[1000001]; n pair<long long, long long> dp[1000001]; n string st; n map<char, int> mp; n long long pw(long long a, long long b) { n   if (b == 0) return 1; n   if (b == 1) return a; n   long long ret = pw(a, b / 2); n   (ret *= ret) %= 1000000007; n   if (b & 1) (ret *= a) % 1000000007; n   return ret; n } n pair<long long, long long> rec(int lef) { n   if (lef == 0) return make_pair(0, 1); n   if (~dp[lef].first) return dp[lef]; n   pair<long long, long long> ret = make_pair(0, 0); n   for (int i = 0; i < 4; i++) { n     pair<long long, long long> tmp = rec(lef - 1); n     tmp.first += cnt[i]; n     if (tmp.first == ret.first) n       (ret.second += tmp.second) %= 1000000007; n     else if (tmp.first > ret.first) n       ret = tmp; n   } n   return dp[lef] = ret; n } n int main() { n   cin >> n >> st; n   mp[ A ] = 0; n   mp[ C ] = 1; n   mp[ G ] = 2; n   mp[ T ] = 3; n   fct[0] = fct[1] = 1; n   for (int i = 0; i < st.size(); i++) cnt[mp[st[i]]]++; n   for (int i = 2; i < 1000001; i++) fct[i] = (fct[i - 1] * i) % 1000000007; n   memset((dp), (-1), sizeof(dp)); n   pair<long long, long long> x = rec(n); n   cout << x.second << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int Nmax = 2005; n int n, k; n pair<double, double> mij[Nmax * Nmax]; n pair<int, int> a[Nmax]; n int main() { n   cin >> n; n   for (int i = 1; i <= n; i++) cin >> a[i].first >> a[i].second; n   for (int i = 1; i <= n; i++) n     for (int j = i + 1; j <= n; j++) n       mij[++k] = {(double)(a[i].first + a[j].first) / 2, n                   (double)(a[i].second + a[j].second) / 2}; n   sort(mij + 1, mij + k + 1); n   int cnt; n   long long sol = 0; n   cnt = 1; n   for (int i = 2; i <= k;) { n     while (i <= k && mij[i].first == mij[i - 1].first && n            mij[i].second == mij[i - 1].second) { n       i++; n       cnt++; n     } n     sol += (cnt * (cnt - 1)) / 2; n     cnt = 1; n     i++; n   } n   cout << sol <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n bool vis[500005]; n int low[500005], d[500005], par[500005], rnk[500005], lev[500005], timer; n vector<int> adj[500005], tree[500005]; n vector<pair<int, int>> edges; n map<int, map<int, int>> ed, br; n int get(int x) { return (par[x] == x ? x : par[x] = get(par[x])); } n void link(int x, int y) { n   x = get(x); n   y = get(y); n   if (x == y) return; n   par[x] = y; n } n void bridge_find(int u, int par) { n   timer++; n   d[u] = low[u] = timer; n   for (auto x : adj[u]) { n     if (x == par) continue; n     if (d[x] > 0) { n       low[u] = min(low[u], d[x]); n       if (ed[x][u] == 0) ed[u][x] = 1; n     } else { n       bridge_find(x, u); n       low[u] = min(low[u], low[x]); n       if (d[u] < low[x]) n         br[u][x] = 1, br[x][u] = 1; n       else { n         ed[u][x] = 1; n         link(u, x); n       } n     } n   } n } n void dfs(int child, int par) { n   vis[child] = 1; n   for (auto x : adj[child]) { n     if (x == par) continue; n     if (br[x][child]) ed[x][child] = 1; n     if (!vis[x]) dfs(x, child); n   } n } n int main() { n   int n, m; n   scanf( %d %d , &n, &m); n   for (int i = 1; i <= n; i++) rnk[i] = 1, par[i] = i; n   for (int i = 0; i < m; i++) { n     int x, y; n     scanf( %d %d , &x, &y); n     edges.push_back({x, y}); n     adj[x].push_back(y); n     adj[y].push_back(x); n   } n   bridge_find(1, -1); n   int mx = 0, cur; n   for (int i = 1; i <= n; i++) { n     int x; n     x = get(i); n     lev[x]++; n     if (mx < lev[x]) { n       mx = lev[x], cur = x; n     } n   } n   dfs(cur, -1); n   cout << mx << endl; n   for (int i = 0; i < m; i++) { n     if (ed[edges[i].first][edges[i].second]) n       cout << edges[i].first <<     << edges[i].second << endl; n     else if (ed[edges[i].second][edges[i].first]) n       cout << edges[i].second <<     << edges[i].first << endl; n   } n } n


#include <bits/stdc++.h> n const long double eps = 1e-9; n const long double pi = fabsl(atan2(0.0, -1.0)); n const int INF = 0x7f7f7f7f; n const long long LINF = 1ll << 60; n const long double LDINF = 1e42; n using namespace std; n int n, k; n string s; n int a[110]; n int res[110]; n int p[110]; n void Load() { n   cin >> n >> k; n   cin >> s; n   p[1] = 0; n   int i, k = 0; n   for (i = 2; i <= s.size(); i++) { n     while (k > 0 && s[k] != s[i - 1]) k = p[k]; n     if (s[k] == s[i - 1]) k++; n     p[i] = k; n   } n   { n     int c = 0; n     while ((c = getchar()) != 10 && c != EOF) n       ; n   }; n   for (int i = 0; i < n - s.size() + 1; i++) { n     a[i] = getchar() -  0 ; n   } n } n void Solve() { n   int k = 0, i, j, t; n   for (i = 0; i < n; i++) { n     if (a[i] == 1) { n       if (res[i] != 0) { n         for (j = 0; j < s.size(); j++) { n           if (res[i + j] != 0 && res[i + j] != s[j]) { n             cout <<  No solution ; n             return; n           } else { n             res[i + j] = s[j]; n           } n         } n       } else { n         for (j = 0; j < s.size(); j++) { n           res[i + j] = s[j]; n         } n       } n     } n     if (res[i] == 0) { n       int rr = 110, rch; n       for (j =  a ; j <=  a  + ::k - 1; j++) { n         t = k; n         while (t > 0 && s[t] != j) t = p[t]; n         if (s[t] == j) t++; n         if (rr > t) { n           rr = t, rch = j; n         } n       } n       cerr << rr <<     << (char)rch; n       res[i] = rch; n       k = rr; n       if (k == s.size() && a[i - s.size() + 1] == 0) { n         cout <<  No solution ; n         return; n       } n     } else { n       t = k; n       while (t > 0 && s[t] != res[i]) t = p[t]; n       if (s[t] == res[i]) t++; n       if (t == s.size() && a[i - s.size() + 1] == 0) { n         cout <<  No solution ; n         return; n       } n       k = t; n     } n   } n   for (i = 0; i < n; i++) { n     putchar(res[i]); n   } n } n int main() { n   Load(); n   Solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxN = 2 * 300000 + 1000; n int block[maxN], start[maxN], fin[maxN]; n int main() { n   ios::sync_with_stdio(0); n   ; n   int n, m; n   cin >> n >> m; n   string s; n   cin >> s; n   set<int> st; n   st.insert(-1); n   st.insert(n); n   for (int i = 0; i < n; i++) { n     if (s[i] !=  . ) st.insert(i); n   } n   int f = 0; n   int cur = 0; n   for (int i = 0; i < n; i++) { n     if (s[i] ==  . ) n       cur++; n     else { n       if (cur) f += cur - 1; n       cur = 0; n     } n   } n   if (cur) f += cur - 1; n   while (m--) { n     int pos; n     char c; n     cin >> pos >> c; n     pos--; n     if (c ==  . ) { n       auto left = st.lower_bound(pos); n       if (*left != pos) { n         cout << f <<   n ; n         continue; n       } n       left--; n       int l = *left; n       int r = *st.upper_bound(pos); n       int sub = r - pos - 2; n       if (sub > 0) f -= sub; n       sub = pos - l - 2; n       if (sub > 0) f -= sub; n       st.erase(pos); n       sub = r - l - 2; n       if (sub > 0) f += sub; n     } else { n       auto left = st.lower_bound(pos); n       if (*left == pos) { n         cout << f <<   n ; n         continue; n       } n       left--; n       int l = *left; n       int r = *st.lower_bound(pos); n       int sub = r - l - 2; n       f -= sub; n       sub = pos - l - 2; n       if (sub > 0) f += sub; n       sub = r - pos - 2; n       if (sub > 0) f += sub; n       st.insert(pos); n     } n     cout << f <<   n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int n; n struct pt { n   int ai; n   int wz; n }; n int cmp(pt a, pt b) { return a.ai > b.ai; } n pt cun[1005]; n int ans; n int ans1[1005]; n int main() { n   scanf( %d , &n); n   for (int i = 1; i <= n; i++) { n     scanf( %d , &cun[i].ai); n     cun[i].wz = i; n   } n   sort(cun + 1, cun + 1 + n, cmp); n   for (int i = 1; i <= n; i++) { n     ans += cun[i].ai * (i - 1) + 1; n   } n   printf( %d n , ans); n   for (int i = 1; i <= n; i++) { n     printf( %d  , cun[i].wz); n   } n   printf(  n ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, k, sum1, sum2; n string a; n int f[300011]; n void solve() { n   scanf( %d%d , &n, &k); n   cin >> a; n   memset(f, -1, sizeof(f)); n   for (int i = 0; i < n; i++) { n     if (a[i] !=  ? ) { n       if (f[i % k] == -1) n         f[i % k] = a[i] -  0 ; n       else if (f[i % k] != (int)(a[i] -  0 )) { n         cout <<  NO  << endl; n         return; n       } n     } n   } n   sum1 = sum2 = 0; n   for (int i = 0; i < k; i++) { n     sum1 += (f[i] == 0); n     sum2 += (f[i] == 1); n   } n   if (sum1 > k / 2 || sum2 > k / 2) n     cout <<  NO  << endl; n   else n     cout <<  YES  << endl; n } n int main() { n   int T; n   scanf( %d , &T); n   while (T--) solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct tree { n   int maxv[650001]; n   void set(int x, int d, int o, int l, int r) { n     if (l == r) { n       maxv[o] = d; n       return; n     } n     int m = (l + r) / 2; n     if (x <= m) n       set(x, d, o * 2, l, m); n     else n       set(x, d, o * 2 + 1, m + 1, r); n     maxv[o] = max(maxv[o * 2], maxv[o * 2 + 1]); n   } n   int query(int x, int y, int o, int l, int r) { n     if (x <= l && r <= y) return maxv[o]; n     int m = (l + r) / 2, ans = 0; n     if (x <= m) ans = max(ans, query(x, y, o * 2, l, m)); n     if (y > m) ans = max(ans, query(x, y, o * 2 + 1, m + 1, r)); n     return ans; n   } n } Tx, Ty; n int n, m; n map<int, int> h, pos; n void modify(int x, int y, int val) { n   Tx.set(x, val, 1, 1, n); n   Ty.set(y, val, 1, 1, m + 20); n } n void remove_all(map<int, int>::iterator del) { n   modify(del->first, del->second, 0); n   pos.erase(pos.lower_bound(del->second)); n   h.erase(del); n } n int main() { n   scanf( %d%d , &n, &m); n   for (int T = 1; T <= m; T++) { n     int op, p, v; n     scanf( %d%d , &op, &p); n     if (op == 1) { n       scanf( %d , &v); n       v += m - T; n       h[p] = v; n       pos[v] = p; n       auto it = pos.lower_bound(v), it2 = it; n       for (;; it2--) { n         modify(it2->second, it2->first, 0); n         if (it2 == pos.begin()) break; n       } n       for (;; it--) { n         int x = it->second, y = it->first, val = Tx.query(x, n, 1, 1, n) + 1; n         modify(x, y, val); n         if (it == pos.begin()) break; n       } n     } else { n       auto del = h.begin(); n       for (int i = 1; i < p; i++) del++; n       if (del == h.begin()) n         remove_all(del); n       else { n         auto it = del, it2 = --it; n         remove_all(del); n         for (;; it2--) { n           modify(it2->first, it2->second, 0); n           if (it2 == h.begin()) break; n         } n         for (;; it--) { n           int x = it->first, y = it->second, n               val = Ty.query(y + 1, m + 20, 1, 1, m + 20) + 1; n           modify(x, y, val); n           if (it == h.begin()) break; n         } n       } n     } n     printf( %d n , Tx.query(1, n, 1, 1, n)); n   } n } n


#include <bits/stdc++.h> n using namespace std; n void solve() { n   int n, d = 0; n   long long x, amount; n   char sign; n   int quantity; n   cin >> n >> amount; n   for (int i = 0; i < n; i++) { n     cin >> sign >> quantity; n     if (sign ==  + ) n       amount += quantity; n     else if (amount < quantity) n       d++; n     else n       amount -= quantity; n   } n   cout << amount <<     << d; n } n int main() { n   solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T> n inline bool cmin(T& a, const T& b) { n   return a > b ? a = b, 1 : 0; n } n template <typename T> n inline bool cmax(T& a, const T& b) { n   return a < b ? a = b, 1 : 0; n } n int inp() { n   int x = 0, f = 1; n   char ch; n   for (ch = getchar(); !isdigit(ch); ch = getchar()) n     if (ch ==  - ) f = -1; n   for (; isdigit(ch); x = x * 10 + ch -  0 , ch = getchar()) n     ; n   return x * f; n } n struct comp { n   double r, i; n   inline comp() {} n   inline comp(double a, double b) : r(a), i(b) {} n }; n const int N = (1 << 19), MaxN = (N << 1) + 10; n const int K = 32767, L = 15; n int P; n inline comp add(comp a, comp b) { return comp(a.r + b.r, a.i + b.i); } n inline comp sub(comp a, comp b) { return comp(a.r - b.r, a.i - b.i); } n inline comp mul(comp a, comp b) { n   return comp(a.r * b.r - a.i * b.i, a.r * b.i + a.i * b.r); n } n inline comp div(comp a, double b) { return comp(a.r / b, a.i / b); } n inline comp conj(comp x) { return comp(x.r, -x.i); } n inline int add(int x, int y) { n   return x + (long long)y >= P ? x + (long long)y - P : x + y; n } n inline int sub(int x, int y) { return x >= y ? x - y : x - (long long)y + P; } n inline int mul(int x, int y) { return (long long)x * y % P; } n int exp(int x, long long n) { n   int res = 1; n   for (; n; n >>= 1) { n     if (n & 1) res = mul(res, x); n     x = mul(x, x); n   } n   return res; n } n comp eps[MaxN]; n void init() { n   double pi = acos(-1); n   for (int i = 0; i < N; i++) n     eps[i] = comp(cos(2 * pi * i / N), sin(2 * pi * i / N)); n } n inline void trans(int n, comp x[], comp w[]) { n   for (int i = 0, j = 0; i != n; i++) { n     if (i < j) std::swap(x[i], x[j]); n     for (int l = n >> 1; (j ^= l) < l; l >>= 1) n       ; n   } n   for (int i = 2; i <= n; i <<= 1) { n     int l = i >> 1, d = N / i; n     for (int j = 0; j != n; j += i) n       for (int k = 0; k != l; k++) { n         comp t = mul(x[j + k + l], w[d * k]); n         x[j + k + l] = sub(x[j + k], t); n         x[j + k] = add(x[j + k], t); n       } n   } n } n inline void dft(int n, comp x[]) { trans(n, x, eps); } n inline void idft(int n, comp x[]) { n   trans(n, x, eps); n   reverse(x + 1, x + n); n   for (int i = 0; i < n; i++) x[i] = div(x[i], n); n } n void conv(int na, int a[], int nb, int b[], int nc, int c[], int same = 0) { n   int n, i; n   static comp x[MaxN], y[MaxN], z[MaxN], w[MaxN]; n   for (n = 1; n < na + nb - 1; n <<= 1) n     ; n   for (i = 0; i < n; i++) { n     x[i] = i < na ? comp(a[i] & K, a[i] >> L) : comp(0, 0); n     y[i] = i < nb ? comp(b[i] & K, b[i] >> L) : comp(0, 0); n   } n   dft(n, x); n   if (!same) n     dft(n, y); n   else n     for (i = 0; i < n; i++) y[i] = x[i]; n   comp r0(0.5, 0), r1(0, -0.5), r(0, 1); n   for (i = 0; i < n; i++) { n     int j = (n - i) & (n - 1); n     comp x0, x1, y0, y1; n     x0 = mul(add(x[i], conj(x[j])), r0); n     x1 = mul(sub(x[i], conj(x[j])), r1); n     y0 = mul(add(y[i], conj(y[j])), r0); n     y1 = mul(sub(y[i], conj(y[j])), r1); n     z[i] = mul(x0, add(y0, mul(y1, r))); n     w[i] = mul(x1, add(y0, mul(y1, r))); n   } n   idft(n, z); n   idft(n, w); n   for (i = 0; i < nc; i++) { n     int c00 = (long long)(z[i].r + 0.5) % P, n         c01 = (long long)(z[i].i + 0.5) % P; n     int c10 = (long long)(w[i].r + 0.5) % P, n         c11 = (long long)(w[i].i + 0.5) % P; n     c[i] = ((((long long)c11 << L) + c01 + c10 << L) + c00) % P; n   } n } n int nL; n int n, l, r, x[MaxN], y[MaxN]; n void pow(int x[], int y[], int n) { n   y[0] = 1; n   int sN = 3; n   nL = 1; n   for (; n; n >>= 1) { n     if (n & 1) n       conv(sN, x, nL, y, min(nL + sN, 2 * ::n + 3), y), n           nL = min(nL + sN, 2 * ::n + 3); n     conv(sN, x, sN, x, min(sN << 1, 2 * ::n + 3), x, 1); n     sN = min(sN << 1, 2 * ::n + 3); n   } n } n int res(int x) { return y[x + n]; } n int main() { n   int ans; n   init(); n   cin >> n >> P >> l >> r; n   x[0] = x[1] = x[2] = 1; n   pow(x, y, n); n   ans = ((long long)res(l) + res(l + 1) - res(r + 1) - res(r + 2)) % P; n   cout << ((long long)ans + P) % P << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long mod = (long long)1000000007; n int M, K; n int par[100000 + 5]; n vector<int> adj[100000 + 5]; n long long dp[100000 + 5][11][3]; n struct bin_tree_type { n   int le; n   int ri; n }; n bin_tree_type tree[100000 + 5]; n long long exp(long long ret, int pow) { n   if (pow == 0) { n     return 1; n   } n   long long sav = 1; n   while (pow ^ 1) { n     if (pow & 1) { n       sav *= ret; n       sav %= mod; n     } n     pow >>= 1; n     ret *= ret; n     ret %= mod; n   } n   ret *= sav; n   ret %= mod; n   return ret; n } n long long inv(long long inp) { return exp(inp, mod - 2); } n void dfs_lcrs(int nod, int pt) { n   int lastchild = -1; n   par[nod] = pt; n   for (int ke : adj[nod]) { n     if (ke == pt) { n       continue; n     } n     if (lastchild == -1) { n       tree[nod].le = ke; n     } else { n       tree[lastchild].ri = ke; n     } n     dfs_lcrs(ke, nod); n     lastchild = ke; n   } n } n long long DP(int nod, int rem, int status) { n   if (nod == 0) { n     if (rem == 0) { n       return 1; n     } else { n       return 0; n     } n   } n   long long &ret = dp[nod][rem][status]; n   if (ret >= 0) { n     return ret; n   } n   ret = 0; n   if (rem && status != 2) { n     if (status == 1) { n       long long sumc = 0; n       for (int i = 0; i <= rem - 1; i++) { n         sumc += DP(tree[nod].le, i, 2) * DP(tree[nod].ri, rem - 1 - i, 1); n         sumc %= mod; n       } n       ret += sumc; n     } else { n       long long mult; n       if (nod != 1) { n         mult = ((long long)(K - 1) * inv(M - 1)) % mod; n       } else { n         mult = 1; n       } n       long long sumc = 0; n       for (int i = 0; i <= rem - 1; i++) { n         sumc += DP(tree[nod].le, i, 2) * DP(tree[nod].ri, rem - 1 - i, 1); n         sumc %= mod; n       } n       sumc *= mult; n       sumc %= mod; n       ret += sumc; n     } n   } n   if (status == 0) { n     long long mult = M - 1; n     long long sumc = 0; n     for (int i = 0; i <= rem; i++) { n       sumc += DP(tree[nod].le, i, 0) * DP(tree[nod].ri, rem - i, 0); n       sumc %= mod; n     } n     sumc *= mult; n     sumc %= mod; n     ret += sumc; n   } else if (status == 1) { n     long long mult = M - 1; n     long long sumc = 0; n     for (int i = 0; i <= rem; i++) { n       sumc += DP(tree[nod].le, i, 0) * DP(tree[nod].ri, rem - i, 1); n       sumc %= mod; n     } n     sumc *= mult; n     sumc %= mod; n     ret += sumc; n   } else if (status == 2) { n     long long mult = K - 1; n     long long sumc = 0; n     for (int i = 0; i <= rem; i++) { n       sumc += DP(tree[nod].le, i, 1) * DP(tree[nod].ri, rem - i, 2); n       sumc %= mod; n     } n     sumc *= mult; n     sumc %= mod; n     ret += sumc; n   } n   return ret; n } n int main() { n   memset(dp, -1, sizeof(dp)); n   int N, a, b, X; n   long long has = 0; n   scanf( %d %d , &N, &M); n   int edge = N - 1; n   while (edge--) { n     scanf( %d %d , &a, &b); n     adj[a].push_back(b); n     adj[b].push_back(a); n   } n   scanf( %d %d , &K, &X); n   if (K == 1 && N > 1) { n     printf( %lld n , exp(M - 1, N)); n     return 0; n   } n   dfs_lcrs(1, 1); n   for (int i = X; i >= 0; i--) { n     has += DP(1, i, 0); n     has %= mod; n   } n   printf( %lld n , has); n } n


#include <bits/stdc++.h> n using namespace std; n const int dir[8][2] = {{1, 0},  {0, 1},  {1, 1},   {1, -1}, n                        {-1, 0}, {0, -1}, {-1, -1}, {-1, 1}}; n int n, m, cnt[1000 + 5][1000 + 5]; n char a[1000 + 5][1000 + 5]; n void work(int x, int y) { n   bool flag = true; n   for (int i = 0; i < 8; i++) { n     int nx = x + dir[i][0], ny = y + dir[i][1]; n     if (a[nx][ny] ==  . ) flag = false; n   } n   if (flag) n     for (int i = 0; i < 8; i++) { n       int nx = x + dir[i][0], ny = y + dir[i][1]; n       cnt[nx][ny]++; n     } n } n bool judge() { n   for (int i = 1; i <= n; i++) n     for (int j = 1; j <= m; j++) n       if (a[i][j] ==  #  && cnt[i][j] == 0) return false; n   return true; n } n int main() { n   cin >> n >> m; n   for (int i = 1; i <= n; i++) n     for (int j = 1; j <= m; j++) cin >> a[i][j]; n   for (int i = 2; i <= n - 1; i++) n     for (int j = 2; j <= m - 1; j++) work(i, j); n   if (judge()) n     cout <<  YES ; n   else n     cout <<  NO ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n inline void read(int &x) { n   int v = 0, f = 1; n   char c = getchar(); n   while (!isdigit(c) && c !=  - ) c = getchar(); n   if (c ==  - ) n     f = -1; n   else n     v = (c & 15); n   while (isdigit(c = getchar())) v = (v << 1) + (v << 3) + (c & 15); n   x = v * f; n } n inline void read(long long &x) { n   long long v = 0ll, f = 1ll; n   char c = getchar(); n   while (!isdigit(c) && c !=  - ) c = getchar(); n   if (c ==  - ) n     f = -1; n   else n     v = (c & 15); n   while (isdigit(c = getchar())) v = (v << 1) + (v << 3) + (c & 15); n   x = v * f; n } n inline void readc(char &x) { n   char c; n   while (((c = getchar()) ==    ) || c ==   n ) n     ; n   x = c; n } n int n, m, i, j, vis[100005], ve[100005], dep[100005]; n long long a[100005], px[100005], py[100005], res[100005]; n vector<int> e[100005]; n void dfs1(int x) { n   vis[x] = 1; n   for (__typeof((e[x]).begin()) it = (e[x]).begin(); it != (e[x]).end(); it++) { n     if (!ve[*it]) { n       int y = px[*it] ^ py[*it] ^ x; n       if (!vis[y]) { n         ve[*it] = 1; n         dep[y] = dep[x] + 1; n         dfs1(y); n         res[*it] += a[y]; n         a[x] -= a[y]; n         a[y] = 0; n       } n     } n   } n } n int main() { n   read(n); n   read(m); n   for (((i)) = (1); ((i)) <= ((n)); ((i))++) read(a[i]); n   for (((i)) = (1); ((i)) <= ((m)); ((i))++) { n     int x, y; n     read(x); n     read(y); n     px[i] = x; n     py[i] = y; n     e[x].push_back(i); n     e[y].push_back(i); n   } n   dfs1(1); n   for (((i)) = (1); ((i)) <= ((m)); ((i))++) n     if (!ve[i]) { n       if (!((dep[px[i]] ^ dep[py[i]]) & 1)) { n         if (dep[px[i]] & 1) n           res[i] -= a[1] / 2; n         else n           res[i] = +a[1] / 2; n         a[px[i]] -= res[i]; n         a[py[i]] -= res[i]; n         break; n       } n     } n   memset(vis, 0, sizeof(vis)); n   memset(ve, 0, sizeof(ve)); n   dfs1(1); n   if (a[1]) { n     puts( NO ); n     return 0; n   } n   puts( YES ); n   for (((i)) = (1); ((i)) <= ((m)); ((i))++) { n     printf( %lld n , res[i]); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct node { n   int t, o, c; n }; n char s[4000010]; n int n, o[4000010], c[4000010], t[4000010]; n void build(int id, int l, int r) { n   if (r - l < 1) { n     if (s[r] ==  ( ) n       o[id] = 1; n     else n       c[id] = 1; n     return; n   } n   int mid = (l + r) / 2; n   build(2 * id, l, mid); n   build(2 * id + 1, mid + 1, r); n   int temp = min(o[2 * id], c[2 * id + 1]); n   t[id] = t[2 * id] + t[2 * id + 1] + temp; n   o[id] = o[2 * id] + o[2 * id + 1] - temp; n   c[id] = c[2 * id] + c[2 * id + 1] - temp; n } n node query(int x, int y, int l, int r, int id) { n   if (y < l || x > r || r < l) return {0, 0, 0}; n   if (r - l < 1) { n     if (s[r] ==  ( ) n       return {0, 1, 0}; n     else n       return {0, 0, 1}; n   } n   if (x <= l && y >= r) return {t[id], o[id], c[id]}; n   node na, nb; n   na = query(x, y, l, (l + r) / 2, 2 * id); n   nb = query(x, y, (l + r) / 2 + 1, r, 2 * id + 1); n   int t = min(na.o, nb.c); n   return {na.t + nb.t + t, na.o + nb.o - t, na.c + nb.c - t}; n } n int main() { n   int i, x, y, j, num, k, m, w, l, r, p; n   scanf( %s , s); n   n = strlen(s); n   build(1, 0, n - 1); n   scanf( %d , &m); n   for (i = (1); i <= (m); i++) { n     scanf( %d%d , &l, &r); n     node nd = query(l - 1, r - 1, 1 - 1, n - 1, 1); n     printf( %d n , 2 * nd.t); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   stack<char> s; n   int n, count = 0; n   char c; n   cin >> n; n   for (int i = 0; i < n; i++) { n     cin >> c; n     if (s.empty()) n       s.push(c); n     else if (s.top() == c) { n       s.pop(); n       s.push(c); n       count++; n     } else n       s.push(c); n   } n   cout << count << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int i, j; n int main() { n   string str; n   int b = 0, e = 0, a = 0, r = 0, s = 0; n   cin >> str; n   if (str.size() >= 4) n     for (i = 0; i < str.size() - 3; i++) { n       for (j = i; j < str.size(); j++) { n         if (str[j] ==  b  && str[j + 1] ==  e  && str[j + 2] ==  a  && n             str[j + 3] ==  r ) { n           s = s + str.size() - j - 3; n           break; n         } n       } n     } n   cout << s; n } n


#include <bits/stdc++.h> n using namespace std; n long long MOD = 1e9 + 7; n bool debug = 1; n const int N = 1e5 + 10; n bool vis[N]; n int g[N]; n int dfs(int i) { n   vis[i] = 1; n   if (!vis[g[i]]) { n     return 1 + dfs(g[i]); n   } n   return 1; n } n int main() { n   int n, x; n   scanf( %d , &n); n   for (int i = 0; i < n; i++) { n     scanf( %d , &x); n     x--; n     g[i] = x; n   } n   vector<int> vc; n   for (int i = 0; i < n; i++) { n     if (!vis[i]) vc.push_back(dfs(i)); n   } n   long long tot = 0; n   sort(vc.begin(), vc.end(), greater<int>()); n   for (int i = 0; i < min((int)vc.size(), 2); i++) { n     tot += vc[i]; n   } n   tot = tot * tot; n   for (int i = 2; i <= vc.size() - 1; i++) { n     tot += 1ll * vc[i] * vc[i]; n   } n   cout << tot << endl; n   return 0; n } n