#include <bits/stdc++.h> n using namespace std; n int n; n string s; n vector<int> pos[26]; n int xd[26]; n int main() { n   cin >> s; n   n = s.size(); n   for (int i = 0; i < n; i++) { n     pos[s[i] -  a ].push_back(i); n     xd[s[i] -  a ]++; n   } n   double ans = 0; n   for (char c =  a ; c <=  z ; c++) { n     int best = 0; n     for (int shift = 0; shift < n; shift++) { n       vector<int> cnt(26, 0); n       for (int i : pos[c -  a ]) { n         cnt[s[(i + shift) % n] -  a ]++; n       } n       int nunique = 0; n       for (int i = 0; i < 26; i++) { n         if (cnt[i] == 1) { n           nunique++; n         } n       } n       best = max(best, nunique); n     } n     ans += 1.0 * best / n; n   } n   printf( %.10lf n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 3e+5; n bool vis[maxn]; n int main() { n   int T, n, m, l; n   cin.sync_with_stdio(false); n   cout.sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   while (cin >> n) { n     memset(vis, false, sizeof vis); n     int pos = n; n     int now = 0; n     cout <<  1  ; n     for (int i = 0; i < n; i++) { n       int x; n       cin >> x; n       now++; n       vis[x] = true; n       while (vis[pos]) { n         pos--; n         now--; n       } n       cout << now + 1 <<    ; n     } n     cout << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<int> d[101000]; n int k[101000]; n int main() { n   int n; n   scanf( %d , &n); n   for (int i = 0; i < n; i++) { n     scanf( %d , &k[i]); n     for (int j = 0; j < k[i]; j++) { n       int a; n       scanf( %d , &a); n       d[i].push_back(a); n     } n     sort(d[i].begin(), d[i].end()); n   } n   int res = 0; n   for (int i = 0; i < n; i++) { n     int a = (i + n - 1) % n; n     int b = (i + 1) % n; n     for (int j = 0; j < k[i] - 1; j++) { n       vector<int>::iterator ini = n           lower_bound(d[a].begin(), d[a].end(), d[i][j]); n       vector<int>::iterator fim = n           lower_bound(d[a].begin(), d[a].end(), d[i][j + 1]); n       int cta = fim - ini; n       ini = lower_bound(d[b].begin(), d[b].end(), d[i][j]); n       fim = lower_bound(d[b].begin(), d[b].end(), d[i][j + 1]); n       if (fim - ini != cta) { n         res++; n       } n     } n   } n   printf( %d n , res); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, m, k; n   cin >> n >> m >> k; n   if (n <= min(m, k)) n     cout <<  YES ; n   else n     cout <<  NO ; n } n


#include <bits/stdc++.h> n using namespace std; n template <class C> n void mini(C& _a4, C _b4) { n   _a4 = min(_a4, _b4); n } n template <class C> n void maxi(C& _a4, C _b4) { n   _a4 = max(_a4, _b4); n } n template <class TH> n void _dbg(const char* sdbg, TH h) { n   cerr << sdbg <<  =  << h << endl; n } n template <class TH, class... TA> n void _dbg(const char* sdbg, TH h, TA... a) { n   while (*sdbg !=  , ) cerr << *sdbg++; n   cerr <<  =  << h <<  , ; n   _dbg(sdbg + 1, a...); n } n struct Dinic { n   struct Edge { n     long long v, c, inv; n   }; n  n  public: n   Dinic() { n = -1; } n   void AddEdge(long long a, long long b, long long cap, long long bi_dir) { n     if (n < max(a, b)) { n       n = max(n, max(a, b)); n       ResizeVectors(); n     } n     e_orig[a].push_back(Edge{b, cap, ((long long)(e_orig[b]).size())}); n     e_orig[b].push_back( n         Edge{a, bi_dir * cap, ((long long)(e_orig[a]).size()) - 1}); n   } n   long long MaxFlow(long long s, long long t) { n     e = e_orig; n     long long result = 0; n     while (Bfs(s, t)) { n       for (long long i = 0; i <= n; i++) { n         beg[i] = 0; n       } n       result += Dfs(s, t, kInf); n     } n     return result; n   } n   vector<bool> MinCut(long long s, long long t) { n     assert(!Bfs(s, t)); n     vector<bool> res(n + 1); n     for (long long i = 0; i <= n; i++) { n       if (dis[i] <= n) { n         res[i] = true; n       } n     } n     return res; n   } n  n  private: n   long long n; n   vector<vector<Edge>> e_orig, e; n   vector<long long> dis, beg; n   vector<pair<long long, long long>> cut; n   bool Bfs(long long s, long long t) { n     for (long long i = 0; i <= n; i++) { n       dis[i] = n + 1; n     } n     dis[s] = 0; n     vector<long long> que; n     que.push_back(s); n     for (long long i = 0; i < ((long long)(que).size()); i++) { n       long long v = que[i]; n       for (auto edge : e[v]) { n         long long nei = edge.v; n         if (edge.c && dis[nei] > dis[v] + 1) { n           dis[nei] = dis[v] + 1; n           que.push_back(nei); n           if (nei == t) { n             cut.clear(); n             return true; n           } n         } else if (!edge.c) { n           cut.push_back(make_pair(v, nei)); n         } n       } n     } n     return false; n   } n   long long Dfs(long long v, long long t, long long min_cap) { n     long long result = 0; n     if (v == t || min_cap == 0) { n       return min_cap; n     } n     for (long long& i = beg[v]; i < ((long long)(e[v]).size()); i++) { n       long long nei = e[v][i].v; n       long long c = e[v][i].c; n       if (dis[nei] == dis[v] + 1 && c > 0) { n         long long flow_here = Dfs(nei, t, min(min_cap, c)); n         result += flow_here; n         min_cap -= flow_here; n         e[v][i].c -= flow_here; n         e[nei][e[v][i].inv].c += flow_here; n       } n       if (min_cap == 0) { n         break; n       } n     } n     return result; n   } n   void ResizeVectors() { n     e_orig.resize(n + 2); n     beg.resize(n + 2); n     dis.resize(n + 2); n   } n   static const long long kInf = 1e18; n }; n struct GomoryHu { n   vector<vector<pair<long long, long long>>> graph; n   vector<vector<long long>> nodes; n   vector<bool> visited; n   vector<long long> groupId, contrId; n   vector<long long> edgeFrom, edgeTo, edgeLen; n   long long wnode; n   long long n; n   GomoryHu(long long N) : graph(N), visited(N), groupId(N), contrId(N), n(N) {} n   void addEdge(long long u, long long v, long long cap) { n     graph[u].emplace_back(v, cap); n     graph[v].emplace_back(u, cap); n   } n   void dfs(long long v, long long type) { n     visited[v] = true; n     contrId[v] = type; n     for (long long i = 0; i < ((long long)(edgeFrom).size()); i++) { n       if (edgeFrom[i] == v && !visited[edgeTo[i]]) { n         dfs(edgeTo[i], type); n       } n     } n   } n   vector<vector<pair<long long, long long>>> run() { n     vector<long long> allNodes(n); n     iota((allNodes).begin(), (allNodes).end(), 0); n     nodes = vector<vector<long long>>{allNodes}; n     edgeFrom.clear(); n     edgeTo.clear(); n     edgeLen.clear(); n     fill((groupId).begin(), (groupId).end(), 0); n     for (long long step = 0; step < n - 1; step++) { n       Dinic dinic; n       for (long long i = 0; i < ((long long)(nodes).size()); i++) { n         if (((long long)(nodes[i]).size()) > 1) { n           wnode = i; n           break; n         } n       } n       random_shuffle((nodes[wnode]).begin(), (nodes[wnode]).end()); n       fill((visited).begin(), (visited).end(), false); n       visited[wnode] = true; n       for (long long i = 0; i < ((long long)(edgeFrom).size()); i++) { n         if (edgeFrom[i] == wnode) { n           dfs(edgeTo[i], edgeTo[i]); n         } n       } n       for (long long v = 0; v < n; v++) { n         long long a = groupId[v] == wnode ? v : contrId[groupId[v]] + n; n         for (auto& P : graph[v]) { n           long long b = groupId[P.first] == wnode n                             ? P.first n                             : contrId[groupId[P.first]] + n; n           dinic.AddEdge(a, b, P.second, 0); n         } n       } n       dinic.AddEdge(2 * n, 2 * n, 0, 0); n       long long a = nodes[wnode][0], b = nodes[wnode][1], n                 f = dinic.MaxFlow(a, b); n       auto cut = dinic.MinCut(a, b); n       for (long long i = 0; i < ((long long)(edgeFrom).size()); i++) { n         if (edgeFrom[i] != wnode && edgeTo[i] == wnode && n             !cut[edgeFrom[i] + n]) { n           edgeTo[i] = step + 1; n         } n         if (edgeTo[i] != wnode && edgeFrom[i] == wnode && !cut[edgeTo[i] + n]) { n           edgeFrom[i] = step + 1; n         } n       } n       edgeFrom.push_back(wnode); n       edgeTo.push_back(step + 1); n       edgeLen.push_back(f); n       edgeFrom.push_back(step + 1); n       edgeTo.push_back(wnode); n       edgeLen.push_back(f); n       vector<long long> A, B; n       for (long long v : nodes[wnode]) { n         (cut[v] ? A : B).push_back(v); n         if (!cut[v]) { n           groupId[v] = step + 1; n         } n       } n       nodes[wnode] = A; n       nodes.push_back(B); n     } n     vector<vector<pair<long long, long long>>> tree(n); n     for (long long i = 0; i < ((long long)(edgeFrom).size()); i++) { n       tree[nodes[edgeFrom[i]][0]].emplace_back(nodes[edgeTo[i]][0], edgeLen[i]); n     } n     return tree; n   } n }; n int main() { n   long long N, M; n   cin >> N >> M; n   GomoryHu gomory(N); n   for (long long i = 0; i < M; i++) { n     long long u, v, c; n     cin >> u >> v >> c; n     gomory.addEdge(u - 1, v - 1, c); n   } n   auto V = gomory.run(); n   vector<long long> connRoot(N); n   vector<list<long long>> vertList(N); n   iota((connRoot).begin(), (connRoot).end(), 0); n   for (long long i = 0; i < N; i++) { n     vertList[i].push_back(i); n   } n   vector<tuple<long long, long long, long long>> edges; n   for (long long i = 0; i < N; i++) { n     for (auto& P : V[i]) { n       if (i < P.first) { n         edges.emplace_back(-P.second, i, P.first); n       } n     } n   } n   sort((edges).begin(), (edges).end()); n   long long sum = 0; n   for (auto E : edges) { n     long long from = get<1>(E), to = get<2>(E); n     sum -= get<0>(E); n     from = connRoot[from]; n     to = connRoot[to]; n     if (from > to) { n       swap(from, to); n     } n     (from, to); n     vertList[from].splice(vertList[from].end(), vertList[to]); n     for (long long i = 0; i < N; i++) { n       if (connRoot[i] == to) { n         connRoot[i] = from; n       } n     } n   } n   cout << sum <<   n ; n   for (long long v : vertList[0]) { n     cout << v + 1 <<    ; n   } n   cout <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n using ll = long long; n using uint = unsigned int; n template <class T> n using V = vector<T>; n template <class T> n using VV = vector<vector<T>>; n template <class T, class U> n void chmax(T& x, U y) { n   if (x < y) x = y; n } n template <class T, class U> n void chmin(T& x, U y) { n   if (y < x) x = y; n } n template <class T> n void mkuni(V<T>& v) { n   sort(v.begin(), v.end()); n   v.erase(unique(v.begin(), v.end()), v.end()); n } n template <class S, class T> n ostream& operator<<(ostream& o, const pair<S, T>& p) { n   return o <<  (  << p.first <<  ,  << p.second <<  ) ; n } n template <class T> n ostream& operator<<(ostream& o, const vector<T>& vc) { n   o <<  { ; n   for (const T& v : vc) o << v <<  , ; n   o <<  } ; n   return o; n } n constexpr ll TEN(int n) { return (n == 0) ? 1 : 10 * TEN(n - 1); } n template <unsigned int mod_> n struct ModInt { n   using uint = unsigned int; n   using ll = long long; n   using ull = unsigned long long; n   constexpr static uint mod = mod_; n   uint v; n   ModInt() : v(0) {} n   ModInt(ll _v) : v(normS(_v % mod + mod)) {} n   explicit operator bool() const { return v != 0; } n   static uint normS(const uint& x) { return (x < mod) ? x : x - mod; } n   static ModInt make(const uint& x) { n     ModInt m; n     m.v = x; n     return m; n   } n   ModInt operator+(const ModInt& b) const { return make(normS(v + b.v)); } n   ModInt operator-(const ModInt& b) const { return make(normS(v + mod - b.v)); } n   ModInt operator-() const { return make(normS(mod - v)); } n   ModInt operator*(const ModInt& b) const { return make((ull)v * b.v % mod); } n   ModInt operator/(const ModInt& b) const { return *this * b.inv(); } n   ModInt& operator+=(const ModInt& b) { return *this = *this + b; } n   ModInt& operator-=(const ModInt& b) { return *this = *this - b; } n   ModInt& operator*=(const ModInt& b) { return *this = *this * b; } n   ModInt& operator/=(const ModInt& b) { return *this = *this / b; } n   ModInt& operator++(int) { return *this = *this + 1; } n   ModInt& operator--(int) { return *this = *this - 1; } n   ll extgcd(ll a, ll b, ll& x, ll& y) const { n     ll p[] = {a, 1, 0}, q[] = {b, 0, 1}; n     while (*q) { n       ll t = *p / *q; n       for (int i = 0; i < int(3); i++) swap(p[i] -= t * q[i], q[i]); n     } n     if (p[0] < 0) n       for (int i = 0; i < int(3); i++) p[i] = -p[i]; n     x = p[1], y = p[2]; n     return p[0]; n   } n   ModInt inv() const { n     ll x, y; n     extgcd(v, mod, x, y); n     return make(normS(x + mod)); n   } n   ModInt pow(ll p) const { n     if (p < 0) return inv().pow(-p); n     ModInt a = 1; n     ModInt x = *this; n     while (p) { n       if (p & 1) a *= x; n       x *= x; n       p >>= 1; n     } n     return a; n   } n   bool operator==(const ModInt& b) const { return v == b.v; } n   bool operator!=(const ModInt& b) const { return v != b.v; } n   friend istream& operator>>(istream& o, ModInt& x) { n     ll tmp; n     o >> tmp; n     x = ModInt(tmp); n     return o; n   } n   friend ostream& operator<<(ostream& o, const ModInt& x) { return o << x.v; } n }; n using mint = ModInt<998244353>; n V<mint> fact, ifact; n mint Choose(int a, int b) { n   if (b < 0 || a < b) return 0; n   return fact[a] * ifact[b] * ifact[a - b]; n } n void InitFact(int N) { n   fact.resize(N); n   ifact.resize(N); n   fact[0] = 1; n   for (int i = 1; i <= int(N - 1); i++) fact[i] = fact[i - 1] * i; n   ifact[N - 1] = fact[N - 1].inv(); n   for (int i = N - 2; i >= 0; i--) ifact[i] = ifact[i + 1] * (i + 1); n } n void test() { n   int N, M; n   cin >> N >> M; n   V<mint> f(N + 1); n   for (int x = int(N) - 1; x >= 0; x--) { n     void(0); n     mint sum_pi = 0; n     V<mint> p(N - x + 1), q(N - x + 1); n     for (int i = 0; i < int(N - x + 1); i++) { n       mint pi = 0; n       mint qi = 0; n       for (int j = 0; j < int(x + 1); j++) { n         mint pij = ifact[N + M] * fact[N + M - (i + j) - 1] * M * fact[i + j] * n                    Choose(N - x, i) * Choose(x, j); n         pi += pij; n         qi += pij * (i + j + 1); n       } n       p[i] = pi, q[i] = qi; n       void(0); n       void(0); n       void(0); n       sum_pi += pi; n     } n     { n       mint sum = 0; n       for (int i = 0; i < int(N - x + 1); i++) { n         sum += q[i]; n         if (i) sum += p[i] * f[x + i]; n       } n       f[x] = sum / (mint(1) - p[0]); n     } n     assert(sum_pi == 1); n   } n   V<mint> df(N); n   for (int i = 0; i < int(N); i++) df[i] = f[i] - f[i + 1]; n   reverse(df.begin(), df.end()); n   for (int i = int(N) - 1; i >= 0; i--) df[i] /= df[0]; n   cout << df << endl; n } n int main() { n   cin.tie(0); n   ios::sync_with_stdio(false); n   cout << fixed << setprecision(20); n   InitFact(3000000); n   int N, M; n   cin >> N >> M; n   mint ans = 0; n   for (int i = 0; i < int(N); i++) { n     if (i == 0) n       ans += mint(M).inv(); n     else n       ans += (mint(M + 1) * (i + 1)).inv(); n   } n   ans *= M; n   ans *= N + M + 1; n   cout << ans << endl; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T> n void read(T &x) { n   x = 0; n   bool f = 0; n   char c = getchar(); n   for (; !isdigit(c); c = getchar()) n     if (c ==  - ) f = 1; n   for (; isdigit(c); c = getchar()) x = x * 10 + (c ^ 48); n   if (f) x = -x; n } n template <typename F> n inline void write(F x, char ed =   n ) { n   static short st[30]; n   short tp = 0; n   if (x < 0) putchar( - ), x = -x; n   do st[++tp] = x % 10, x /= 10; n   while (x); n   while (tp) putchar( 0  | st[tp--]); n   putchar(ed); n } n template <typename T> n inline void Mx(T &x, T y) { n   x < y && (x = y); n } n template <typename T> n inline void Mn(T &x, T y) { n   x > y && (x = y); n } n const int N = 50005; n int a[N], n; n long long ask(int l, int r) { n   cout <<  ?   << l <<     << r << endl; n   fflush(stdout); n   long long t; n   cin >> t; n   return t; n } n int main() { n   read(n); n   long long t1 = ask(1, 2), t2 = ask(1, 3), t3 = ask(2, 3); n   a[3] = t2 - t1, a[2] = t3 - a[3], a[1] = t1 - a[2]; n   for (int i = 4; i <= n; i++) a[i] = ask(i - 1, i) - a[i - 1]; n   cout <<  !  ; n   for (int i = 1; i <= n; i++) cout << a[i] <<    ; n   fflush(stdout); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n using i64 = int64_t; n using i32 = int32_t; n template <class T, class U> n void init_n(vector<T>& v, size_t n, U x) { n   v = vector<T>(n, x); n } n template <class T> n void init_n(vector<T>& v, size_t n) { n   init_n(v, n, T()); n } n template <class T> n void read_n(vector<T>& v, size_t n, size_t o = 0) { n   v = vector<T>(n + o); n   for (size_t i = o; i < n + o; ++i) cin >> v[i]; n } n template <class T> n void read_n(T a[], size_t n, size_t o = 0) { n   for (size_t i = o; i < n + o; ++i) cin >> a[i]; n } n template <class T> n T gabs(const T& x) { n   return max(x, -x); n } n void solve() { n   int n; n   cin >> n; n   vector<int> p(n + 1); n   vector<int> q(n + 1); n   for (int i = 1; i <= n; ++i) { n     cin >> p[i]; n     q[p[i]] = i; n   } n   string res(n,  0 ); n   res[0] =  1 ; n   for (int i = 2, l = q[1], r = q[1]; i <= n; ++i) { n     l = min(l, q[i]); n     r = max(r, q[i]); n     if (i == r - l + 1) res[i - 1] =  1 ; n   } n   cout << res <<   n ; n } n int q; n int main() { n   cin >> q; n   while (q--) { n     solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n using lint = long long; n using P = pair<int, int>; n using vec = vector<int>; n using mat = vector<vector<int>>; n constexpr int MOD = 1000000007; n const int INF = 1 << 30; n bool is_prime(int n) { n   if (n <= 1) return false; n   for (int i = 2; i * i <= n; i++) { n     if (n % i == 0) return false; n   } n   return true; n } n vec get_primes() { n   vec p; n   for (int i = 2; i <= 1000; i++) { n     if (is_prime(i)) p.push_back(i); n   } n   return p; n } n int main() { n   int t; n   cin >> t; n   for (int _ = 0; _ < (int)(t); _++) { n     int n; n     cin >> n; n     vec a(n); n     for (int i = 0; i < (int)(n); i++) cin >> a[i]; n     auto p = get_primes(); n     vec c(n, -1); n     int now = 1; n     for (auto&& pm : p) { n       bool f = false; n       for (int i = 0; i < (int)(n); i++) { n         if (c[i] == -1) { n           if (a[i] % pm == 0) c[i] = now, f = true; n         } n       } n       if (f) now++; n     } n     int mx = -1; n     for (int i = 0; i < (int)(n); i++) mx = max(mx, c[i]); n     cout << mx <<   n ; n     for (int i = 0; i < (int)(n); i++) { n       if (i != 0) cout <<    ; n       cout << c[i]; n     } n     cout <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n inline long long rd() { n   long long _x = 0; n   int _ch = getchar(), _f = 1; n   for (; !isdigit(_ch) && (_ch !=  - ) && (_ch != EOF); _ch = getchar()) n     ; n   if (_ch ==  - ) { n     _f = 0; n     _ch = getchar(); n   } n   for (; isdigit(_ch); _ch = getchar()) _x = _x * 10 + _ch -  0 ; n   return _f ? _x : -_x; n } n void write(long long _x) { n   if (_x >= 10) n     write(_x / 10), putchar(_x % 10 +  0 ); n   else n     putchar(_x +  0 ); n } n inline void wrt(long long _x, char _p) { n   if (_x < 0) putchar( - ), _x = -_x; n   write(_x); n   if (_p) putchar(_p); n } n int Q = 13; n int n, cnt, to[1005]; n std::vector<int> vt[13]; n long long r[13]; n int main() { n   n = rd(); n   for (int i = 1; i < (1 << Q); i++) n     if (__builtin_popcount(i) == 6) { n       cnt++; n       to[cnt] = i; n       for (int j = 0; j < Q; j++) n         if (i & (1 << j)) { n           vt[j].push_back(cnt); n         } n       if (cnt == n) break; n     } n   for (int i = 0; i < 13; i++) n     if (vt[i].size()) { n       printf( ? %d  , (int)vt[i].size()); n       for (auto j : vt[i]) { n         printf( %d  , j); n       } n       printf(  n ); n       fflush(stdout); n       r[i] = rd(); n     } n   printf( !  ); n   for (int i = 1; i <= n; i++) { n     long long res = 0; n     for (int j = 0; j < Q; j++) n       if (!(to[i] & (1 << j))) { n         res |= r[j]; n       } n     printf( %lld  , res); n   } n   printf(  n ); n   fflush(stdout); n } n


#include <bits/stdc++.h> n #pragma GCC optimize( O3 ) n using namespace std; n const int arr = 2e5 + 10; n const int ar = 2e3 + 10; n const long double pi = acos(-1); n const long double eps = 1e-10; n const long long md = 1e9 + 7; n #pragma GCC target( sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native ) n #pragma GCC optimize( unroll-loops ) n const int buf_size = 4096; n int buf_pos, buf_len; n unsigned char buf[buf_size]; n inline bool is_eof() { n   if (buf_pos == buf_len) { n     buf_pos = 0; n     buf_len = fread(buf, 1, buf_size, stdin); n     if (buf_len == 0) { n       return 1; n     } n   } n   return 0; n } n inline int get_char() { return is_eof() ? -1 : buf[buf_pos++]; } n inline int read_char() { n   int c = get_char(); n   while (c != -1 && c <= 32) { n     c = get_char(); n   } n   return c; n } n inline int read_int() { n   int x = 0; n   int c = read_char(); n   int m = 1; n   if (c ==  - ) { n     m = -1; n     c = get_char(); n   } n   while ( 0  <= c && c <=  9 ) { n     x = x * 10 + c -  0 ; n     c = get_char(); n   } n   return x * m; n } n int my_top[arr]; n int s[arr]; n int t[arr]; n int where_go[arr]; n int pred[arr]; n vector<int> reb[arr]; n int pos[arr]; n int last_pos = 0; n int a[arr]; n int n, m; n int ans = 0; n void foo(int* a, int n, int d) { n   if (d == 1) { n     for (int i = 0; i < n; i++) { n       a[i]++; n       ans += (a[i] == 1); n     } n   } else { n     for (int i = 0; i < n; i++) { n       a[i]--; n       ans -= (a[i] == 0); n     } n   } n } n void dooo(int q, int d) { n   while (q != -1) { n     int top = my_top[q]; n     foo(a + pos[top], pos[q] - pos[top] + 1, d); n     q = pred[top]; n   } n } n void work(int q) { n   if (q > 0) { n     if (a[pos[q]] > 0) { n       ans--; n     } n     a[pos[q]] -= md; n     dooo(pred[q], 1); n   } else { n     q = -q; n     a[pos[q]] += md; n     if (a[pos[q]] > 0) { n       ans++; n     } n     dooo(pred[q], -1); n   } n } n void dfs1(int now, int p = -1) { n   pred[now] = p; n   s[now] = 1; n   for (auto wh : reb[now]) { n     if (wh != p) { n       dfs1(wh, now); n       s[now] += s[wh]; n       if (where_go[now] == 0 || s[where_go[now]] < s[wh]) { n         where_go[now] = wh; n       } n     } n   } n } n void dfs2(int now, int p = -1) { n   if (!my_top[now]) { n     my_top[now] = now; n   } n   pos[now] = last_pos++; n   if (where_go[now]) { n     my_top[where_go[now]] = my_top[now]; n     dfs2(where_go[now], now); n   } n   for (auto wh : reb[now]) { n     if (wh != p && wh != where_go[now]) { n       dfs2(wh, now); n     } n   } n } n int main() { n   n = read_int(); n   m = read_int(); n   for (int i = 2; i <= n; i++) { n     int u; n     u = read_int(); n     reb[u].push_back(i); n     reb[i].push_back(u); n   } n   for (int i = 1; i <= n; i++) { n     t[i] = read_int(); n   } n   dfs1(1); n   dfs2(1); n   for (int i = 1; i <= n; i++) { n     a[pos[i]] = -t[i]; n   } n   while (m--) { n     int q; n     q = read_int(); n     work(q); n     cout << ans <<    ; n   } n   cout <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = int(1e5) + 10, mod = int(1e9) + 7; n const long long inf = (long long)(2e18); n int n, q; n vector<int> g[N]; n int first[N], c[N]; n long long t[N], a[N], b[N], add[N]; n set<pair<long long, int> > all; n multiset<long long> st[N]; n set<pair<long long, int> >::iterator it; n multiset<long long>::iterator is; n long long mn, mx; n int p[N], pn; n void globaldel(int i) { n   if (st[i].size() > 0) { n     it = all.find(make_pair(*st[i].begin() + add[i], i)); n     if (it != all.end()) all.erase(it); n     it = all.find(make_pair(*st[i].rbegin() + add[i], i)); n     if (it != all.end()) all.erase(it); n   } n } n void globaladd(int i) { n   if (st[i].size() > 0) { n     all.insert(make_pair(*st[i].begin() + add[i], i)); n     all.insert(make_pair(*st[i].rbegin() + add[i], i)); n   } n } n void Del(int i) { n   globaldel(first[i]); n   st[first[i]].erase(st[first[i]].find(a[i])); n   globaladd(first[i]); n } n void Add(int i) { n   globaldel(first[i]); n   st[first[i]].insert(a[i]); n   globaladd(first[i]); n } n int main() { n   scanf( %d%d , &n, &q); n   for (int i = 1; i <= n; i++) scanf( %I64d , &t[i]); n   for (int i = 1; i <= n; i++) { n     scanf( %d , &first[i]); n     g[first[i]].push_back(i); n     c[first[i]]++; n   } n   for (int i = 1; i <= n; i++) { n     long long val = t[i] / (c[i] + 2); n     b[i] = t[i] - (c[i] + 1) * val; n     a[i] += b[i]; n     for (int j = 0; j < g[i].size(); j++) { n       int to = g[i][j]; n       a[to] += val; n     } n     a[first[i]] += val; n   } n   for (int i = 1; i <= n; i++) { n     for (int j = 0; j < g[i].size(); j++) { n       int to = g[i][j]; n       st[i].insert(a[to]); n     } n     if (st[i].size() > 0) { n       all.insert(make_pair(*st[i].rbegin(), i)); n       all.insert(make_pair(*st[i].begin(), i)); n     } n   } n   int tp, i, j; n   for (int qq = 1; qq <= q; qq++) { n     scanf( %d , &tp); n     if (tp == 1) { n       scanf( %d%d , &i, &j); n       Del(i); n       Del(j); n       Del(first[j]); n       globaldel(j); n       a[j] -= b[j]; n       c[j]++; n       long long val = t[j] / (c[j] + 2); n       a[i] += t[j] / (c[j] + 1); n       ; n       b[j] = t[j] - (c[j] + 1) * val; n       a[j] += b[j]; n       val -= t[j] / (c[j] + 1); n       a[i] -= add[j]; n       add[j] += val; n       a[first[j]] += val; n       a[j] += t[i] / (c[i] + 2); n       a[i] += add[first[i]]; n       Add(j); n       Add(first[j]); n       int v = first[i]; n       Del(v); n       Del(first[v]); n       globaldel(v); n       first[i] = j; n       a[i] -= t[v] / (c[v] + 2); n       a[v] -= t[i] / (c[i] + 2); n       a[v] -= b[v]; n       c[v]--; n       val = t[v] / (c[v] + 2); n       b[v] = t[v] - (c[v] + 1) * val; n       a[v] += b[v]; n       val -= t[v] / (c[v] + 3); n       add[v] += val; n       a[first[v]] += val; n       Add(i); n       Add(v); n       Add(first[v]); n       globaladd(j); n       globaladd(v); n     } else if (tp == 2) { n       scanf( %d , &i); n       printf( %I64d n , a[i] + add[first[i]]); n     } else { n       printf( %I64d %I64d n , (*all.begin()).first, (*all.rbegin()).first); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct ask { n   int l, r, where; n } q[210000]; n const int mo = 1e9 + 7, N = 1000000; n int A[210000], x[210000], pd[1100000], n, m, B[210000], where[1100000], n     ans[210000]; n vector<int> p[1100000]; n int quick(int k1, int k2) { n   int k3 = 1; n   while (k2) { n     if (k2 & 1) k3 = 1ll * k3 * k1 % mo; n     k2 >>= 1; n     k1 = 1ll * k1 * k1 % mo; n   } n   return k3; n } n int compare(ask k1, ask k2) { return k1.r < k2.r; } n void add(int k1, int k2) { n   if (k1 == 0) return; n   for (; k1 <= n; k1 += k1 & (-k1)) B[k1] = 1ll * B[k1] * k2 % mo; n } n int find(int k1) { n   int ans = 1; n   for (; k1; k1 -= k1 & (-k1)) ans = 1ll * ans * B[k1] % mo; n   return ans; n } n void addin(int k1, int k2) { n   int w = 1ll * (k1 - 1) * quick(k1, mo - 2) % mo; n   add(where[k1], quick(w, mo - 2)); n   add(k2, w); n   where[k1] = k2; n } n void insert(int k1) { n   int w = x[k1]; n   for (int i = 0; i < p[w].size(); i++) { n     int k2 = p[w][i]; n     addin(k2, k1); n   } n } n int getans(int L, int R) { n   return 1ll * A[R] * quick(A[L - 1], mo - 2) % mo * find(R) % mo * n          quick(find(L - 1), mo - 2) % mo; n } n int main() { n   scanf( %d , &n); n   for (int i = 1; i <= n; i++) scanf( %d , &x[i]); n   scanf( %d , &m); n   ; n   A[0] = 1; n   for (int i = 1; i <= n; i++) A[i] = 1ll * A[i - 1] * x[i] % mo; n   for (int i = 1; i <= m; i++) { n     scanf( %d%d , &q[i].l, &q[i].r); n     q[i].where = i; n   } n   sort(q + 1, q + m + 1, compare); n   for (int i = 1; i <= n; i++) B[i] = 1; n   for (int i = 2; i <= N; i++) n     if (pd[i] == 0) n       for (int j = i; j <= N; j += i) { n         pd[j] = 1; n         p[j].push_back(i); n       } n   int now = 1; n   for (int i = 1; i <= n; i++) { n     insert(i); n     while (now <= m && q[now].r == i) n       ans[q[now].where] = getans(q[now].l, q[now].r), now++; n   } n   for (int i = 1; i <= m; i++) printf( %d n , ans[i]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n using LL = long long; n using LD = long double; n const int MX = 1 << 18 | 3; n int N, a[MX], Q; n LL sum; n int main() { n   ios::sync_with_stdio(0); n   cout << fixed << setprecision(10); n   cin >> N >> Q; n   for (int i = 0; i < (1 << N); i++) cin >> a[i], sum += a[i]; n   cout << (LD)sum / (1 << N) <<   n ; n   while (Q--) { n     int u, x; n     cin >> u >> x; n     sum = sum - a[u] + x; n     a[u] = x; n     cout << (LD)sum / (1 << N) <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n template <class T> n bool read(T &x) { n   char *s; n   s = (char *)malloc(10); n   if (sizeof(x) == 1) n     strcpy(s + 1,   %c ); n   else if (sizeof(x) == 4) n     strcpy(s + 1,  %d ); n   else if (sizeof(x) == 8) n     strcpy(s + 1,  %lld ); n   int k = scanf(s + 1, &x); n   free(s); n   return k != -1; n } n using namespace std; n char s[2000000]; n int sz1, sz2, len, pos[2000000], neg[2000000]; n int main() { n   scanf( %s , s + 1); n   len = strlen(s + 1); n   s[0] =  0 ; n   for (int i = len; i >= 0; i--) { n     if (s[i] ==  0 ) continue; n     if (s[i] ==  2 ) { n       s[i - 1]++; n       continue; n     } n     if (s[i] ==  1  and i > 0 and s[i - 1] ==  1 ) { n       neg[++sz2] = len - i; n       s[i - 1]++; n       continue; n     } n     pos[++sz1] = len - i; n   } n   cout << sz1 + sz2 << endl; n   for (int i = 1; i <= sz1; i++) printf( +2^%d n , pos[i]); n   for (int i = 1; i <= sz2; i++) printf( -2^%d n , neg[i]); n } n


#include <bits/stdc++.h> n using namespace std; n long long quickMod(long long a, long long b, long long c) { n   long long ans = 1; n   while (b) { n     if (b & 1) ans = ans * a % c; n     b >>= 1; n     a = a * a % c; n   } n   return ans; n } n long long Euler(long long x) { n   long long res = x; n   for (long long i = 2; i * i <= x; i++) { n     if (x % i == 0) { n       res = res - res / i; n       while (x % i == 0) x /= i; n     } n   } n   if (x > 1) res = res - res / x; n   return res; n } n int main() { n   long long c, tmp, ans, phi, result; n   char b[1000005], n[1000005]; n   scanf( %s%s%lld , b, n, &c); n   phi = Euler(c); n   tmp = 0; n   for (long long i = 0; b[i]; i++) tmp = (tmp * 10 + b[i] -  0 ) % c; n   ans = 0; n   bool flag = false; n   for (long long i = 0; n[i]; i++) { n     ans = ans * 10 + n[i] -  0 ; n     if (ans > phi) { n       flag = true; n       ans %= phi; n     } n   } n   if (flag) ans += phi; n   ans--; n   if (tmp == 0) tmp = c; n   result = (((tmp + c - 1) % c) * quickMod(tmp, ans, c)) % c; n   printf( %lld n , result ? result : c); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T> n T gcd(T a, T b) { n   if (a == 0) return b; n   return gcd(b % a, a); n } n template <typename T> n T pow(T a, T b, long long m) { n   T ans = 1; n   while (b > 0) { n     if (b % 2 == 1) ans = ((ans % m) * (a % m)) % m; n     b /= 2; n     a = ((a % m) * (a % m)) % m; n   } n   return ans % m; n } n long long a[10]; n long long fac[202]; n void factorial() { n   fac[0] = 1; n   for (int i = 1; i < 200; i++) { n     fac[i] = fac[i - 1] * i; n     fac[i] %= (long long)(1000 * 1000 * 1000 + 7); n   } n } n long long c(long long n, long long k) { n   long long nemo = fac[n]; n   long long deno = fac[k] * fac[n - k]; n   deno %= (long long)(1000 * 1000 * 1000 + 7); n   long long invdeno = pow(deno, (long long)(1000 * 1000 * 1000 + 7) - 2, n                           (long long)(1000 * 1000 * 1000 + 7)); n   long long pro = nemo * invdeno; n   pro %= (long long)(1000 * 1000 * 1000 + 7); n   return pro; n } n long long dp[105][10]; n long long solve(int len, int curnum) { n   if (len < 0) return 0; n   if (curnum == 9) { n     return len >= a[9]; n   } n   long long ans = 0; n   if (dp[len][curnum] != -1) return dp[len][curnum]; n   if (curnum == 0) { n     for (int i = a[curnum]; i < len; i++) { n       ans += solve(len - i, curnum + 1) * c(len - 1, i); n       ans %= (long long)(1000 * 1000 * 1000 + 7); n     } n   } else { n     for (int i = a[curnum]; i <= len; i++) { n       ans += solve(len - i, curnum + 1) * c(len, i); n       ans %= (long long)(1000 * 1000 * 1000 + 7); n     } n   } n   return dp[len][curnum] = (ans % (long long)(1000 * 1000 * 1000 + 7)); n } n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   ; n   int n; n   cin >> n; n   factorial(); n   for (int i = 0; i < 10; i++) cin >> a[i]; n   long long ans = 0; n   for (int i = 0; i <= n; i++) { n     if (i >= a[9]) n       dp[i][9] = 1; n     else n       dp[i][9] = 0; n   } n   for (int k = 0; k <= n; k++) { n     for (int j = 8; j >= 0; j--) { n       if (j != 0) { n         for (int l = a[j]; l <= k; l++) { n           dp[k][j] += dp[k - l][j + 1] * c(k, l); n           dp[k][j] %= (long long)(1000 * 1000 * 1000 + 7); n         } n       } else { n         for (int l = a[j]; l <= k; l++) { n           dp[k][j] += dp[k - l][j + 1] * c(k - 1, l); n           dp[k][j] %= (long long)(1000 * 1000 * 1000 + 7); n         } n       } n     } n   } n   for (int i = 1; i <= n; i++) { n     ans += dp[i][0]; n     ans %= (long long)(1000 * 1000 * 1000 + 7); n   } n   cout << ans; n } n


#include <bits/stdc++.h> n using namespace std; n const double pi = acos(-1.0); n const double eps = 1e-8; n const int inf = 0x7FFFFFFF; n template <class T> n void show(T a, int n) { n   for (int i = 0; i < n; ++i) cout << a[i] <<    ; n   cout << endl; n } n template <class T> n void show(T a, int r, int l) { n   for (int i = 0; i < r; ++i) show(a[i], l); n   cout << endl; n } n int k[7]; n int main() { n   int i, j, ans; n   int a, b, c, x, y, z; n   cin >> a >> b >> c >> x >> y >> z; n   for (i = 0; i < 6; i++) cin >> k[i]; n   ans = 0; n   if (a > x) { n     ans += k[5]; n   } else if (a < 0) n     ans += k[4]; n   if (b > y) n     ans += k[1]; n   else if (b < 0) n     ans += k[0]; n   if (c > z) n     ans += k[3]; n   else if (c < 0) n     ans += k[2]; n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   ; n   int t; n   cin >> t; n   while (t--) { n     long long p, f, s, w, cntS, cntW; n     cin >> p >> f >> cntS >> cntW >> s >> w; n     if (s > w) { n       swap(cntS, cntW); n       swap(s, w); n     } n     long long ans = 0; n     for (long long i = 0; i <= cntS; i++) { n       if (i * s > p) break; n       long long sf = i; n       long long ss = min(f / s, cntS - i); n       long long leftf = p - sf * s; n       long long lefts = f - ss * s; n       long long wf = min(cntW, leftf / w); n       long long ws = min(cntW - wf, lefts / w); n       ans = max(ans, ss + sf + wf + ws); n     } n     cout << ans << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int INF = 1e9 + 9; n const int MOD = 1e9 + 7; n const int MAXN = 300005; n long long N, k, m; n long long cook[MAXN], t[MAXN], l[MAXN]; n vector<int> ch[MAXN]; n int good, bad; n template <class T, class Operation = plus<T>> n struct SegmentTree { n   int n; n   vector<T> st; n   Operation op; n   const T identity; n   T query(int L, int R) { n     T resl = identity, resr = identity; n     for (L += n, R += n; L < R; L >>= 1, R >>= 1) { n       if (L & 1) resl = op(resl, st[L++]); n       if (R & 1) resr = op(st[--R], resr); n     } n     return op(resl, resr); n   } n   void update(int p, T val) { n     for (st[p += n] = val; p > 1; p >>= 1) st[p >> 1] = op(st[p], st[p ^ 1]); n   } n   SegmentTree(const vector<T> &A, const T &identity = 0, n               const Operation &op = Operation()) n       : n((int)A.size()), st(n << 1), op(op), identity(identity) { n     copy(A.begin(), A.end(), st.begin() + n); n     for (int i = n - 1; i > 0; --i) n       st[i] = this->op(st[i << 1], st[i << 1 | 1]); n   } n }; n struct Op { n   pair<long long, long long> operator()(const pair<long long, long long> &a, n                                         const pair<long long, long long> &b) { n     return pair<long long, long long>(a.first + b.first, a.second + b.second); n   } n }; n SegmentTree<pair<long long, long long>, Op> st( n     vector<pair<long long, long long>>(1000006), n     pair<long long, long long>(0, 0)); n long long calc(int u) { n   pair<long long, long long> val = st.query(t[u], t[u] + 1); n   st.update(t[u], n             st.op(val, pair<long long, long long>(cook[u], cook[u] * t[u]))); n   long long maxi1 = 0, maxi2 = 0; n   for (int i = 0; i < (int)ch[u].size(); i++) { n     if (k - l[ch[u][i]] >= 0) { n       k -= l[ch[u][i]]; n       long long chdp = calc(ch[u][i]); n       if (chdp > maxi1) { n         maxi2 = maxi1; n         maxi1 = chdp; n       } else if (chdp > maxi2) { n         maxi2 = chdp; n       } n       k += l[ch[u][i]]; n     } n   } n   int lo = 0, hi = min(k + 2, (long long)1000004); n   while (lo + 1 < hi) { n     int med = lo + (hi - lo) / 2; n     pair<long long, long long> sum = st.query(0, med); n     if (sum.second > k) { n       hi = med; n     } else { n       lo = med; n     } n   } n   pair<long long, long long> sum = st.query(0, lo); n   long long f = sum.first; n   if (lo > 0) f += min((k - sum.second) / lo, st.st[st.n + lo].first); n   st.update(t[u], val); n   if (u == 0) { n     return max(f, maxi1); n   } else { n     return max(f, maxi2); n   } n } n int main() { n   cin >> N >> k; n   for (int i = 0; i < N; i++) { n     cin >> cook[i]; n   } n   for (int i = 0; i < N; i++) { n     cin >> t[i]; n   } n   for (int i = 1; i < N; i++) { n     cin >> m >> l[i]; n     l[i] *= 2; n     ch[m - 1].push_back(i); n   } n   cout << calc(0) << endl; n } n


#include <bits/stdc++.h> n using namespace std; n inline long long mod(long long n, long long m) { n   long long ret = n % m; n   if (ret < 0) ret += m; n   return ret; n } n long long gcd(long long a, long long b) { n   return (b == 0LL ? a : gcd(b, a % b)); n } n long long exp(long long a, long long b, long long m) { n   if (b == 0LL) return 1LL; n   if (b == 1LL) return mod(a, m); n   long long k = mod(exp(a, b / 2, m), m); n   if (b & 1LL) { n     return mod(a * mod(k * k, m), m); n   } else n     return mod(k * k, m); n } n const long long N = 150; n long long a[N]; n long long b[N * N]; n long long n, k; n long long ok() { n   long long sum = 0; n   for (long long i = 1; i <= k; i++) { n     sum += b[i]; n   } n   long long s2 = sum; n   long long tam = n; n   for (long long i = k + 1; i <= tam; i++) { n     s2 -= a[i - k]; n     s2 += a[i]; n     if (s2 != sum) return false; n   } n   return true; n } n long long T; n long long go(long long n, long long m, long long obj, long long t) { n   if (n == obj) { n     T = min(T, t); n     return t; n   } n   if (n > obj) return 1e9; n   long long has = 1e9; n   for (long long x = m; x >= 0; x--) { n     has = min(has, go(n + x + m, m + x, obj, t + 1)); n   } n   return has; n } n void go2(long long n, long long m, long long obj, long long t) { n   if (n == obj) { n     return; n   } n   for (long long x = m; x >= 0; x--) { n     if (go(n + x + m, m + x, obj, t + 1) == T) { n       cout << x <<    ; n       go2(n + x + m, m + x, obj, t + 1); n       return; n     } n   } n } n void solve(long long coisa = 0) { n   long long n, m; n   if (coisa == 0) n     cin >> n; n   else n     n = coisa; n   if (n == 3) { n     cout << 1 <<   n ; n     cout << 1 <<   n ; n     return; n   } n   long long t = 0; n   for (long long i = 30; i >= 0; i--) { n     if (n & (1LL << i)) { n       t = i; n       break; n     } n   } n   cout << t <<   n ; n   T = t; n   if (n < 10) { n     go2(1, 1, n, 0); n     cout <<   n ; n   } else if ((1 << t) == n) { n     long long has = 1; n     for (long long x = 1; x <= t; x++) { n       if (x == 1) n         cout << 0 <<    ; n       else { n         cout << has <<    ; n         has = has + has; n       } n     } n     cout <<   n ; n   } else { n     long long has = 1; n     long long m = 1; n     for (long long x = 1; x < t - 1; x++) { n       cout << has <<    ; n       m = m + has + has; n       has = has + has; n     } n     long long op1 = m + has + has; n     long long has2 = has + has; n     op1 = op1 + has2; n     if (n < (op1)) { n       for (long long x = 1; x <= has; x *= 2) { n         has2 = has + x; n         long long m2 = m + has2; n         if (n - m2 - has2 <= has2 and n - m2 - has2 >= 0) { n         } else n           continue; n         cout << x <<    ; n         has += x; n         m += has; n         cout << n - m - has <<   n ; n       } n     } else { n       cout << has <<    ; n       has += has; n       m += has; n       cout << n - m - has <<   n ; n     } n   } n } n int32_t main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   ; n   long long t; n   cin >> t; n   while (t--) { n     solve(); 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, m, x, y; n   cin >> n >> m >> x >> y; n   vector<pair<long long, long long>> ans; n   stack<pair<long long, long long>> st; n   long long mat[n + 1][m + 1]; n   for (long long i = 1; i <= n; i++) n     for (long long j = 1; j <= m; j++) mat[i][j] = 0; n   st.push({x, y}); n   while (!st.empty()) { n     pair<long long, long long> cur = st.top(); n     if (mat[cur.first][cur.second] == 0) { n       mat[cur.first][cur.second] = 1; n       ans.push_back({cur.first, cur.second}); n     } n     st.pop(); n     for (long long i = 1; i <= n; i++) { n       if (mat[i][cur.second] == 0) { n         st.push({i, cur.second}); n       } n     } n     for (long long j = 1; j <= m; j++) { n       if (mat[cur.first][j] == 0) { n         st.push({cur.first, j}); n       } n     } n   } n   for (auto x : ans) { n     cout << x.first <<     << x.second <<   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   while (t--) { n     solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long int N, cn = 0, Z = 0, max = 0; n   cin >> N; n   int arr[N]; n   for (int i = 0; i < N; i++) { n     cin >> arr[i]; n   } n   for (int i = 0; i < N; i++) { n     cn = 0; n     for (int j = i + 1; j < N; j++) { n       if (arr[i] == arr[j] && arr[j] != 0) { n         cn++; n         arr[j] = 0; n       } n     } n     if (max <= cn) { n       max = cn; n     } n     if (cn >= 1) { n       Z += cn; n     } n   } n   cout << max + 1 <<     << N - Z; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n using ui32 = unsigned; n using ui64 = unsigned long long; n using i64 = long long; n using i32 = int; n static constexpr bool debug = false; n struct node { n   node *c[2] = {0, 0}; n   int cnt = 0; n }; n node *root = new node; n void insert_rec(node *nd, ui32 number, int i) { n   ++nd->cnt; n   if (i != -1) { n     int bit = (number >> i) & 1; n     if (nd->c[bit] == nullptr) nd->c[bit] = new node; n     insert_rec(nd->c[bit], number, i - 1); n   } n } n void insert(ui32 number) { insert_rec(root, number, 31); } n bool remove_rec(node *nd, ui32 number, int i) { n   if (i != -1) { n     int bit = (number >> i) & 1; n     if (remove_rec(nd->c[bit], number, i - 1)) nd->c[bit] = nullptr; n   } n   if (--nd->cnt == 0) { n     delete nd; n     return true; n   } else { n     return false; n   } n } n void remove(ui32 number) { remove_rec(root, number, 31); } n void print_numbers_rec(node *nd, ui32 number, int i) { n   if (nd == nullptr) return; n   if (i == -1) { n     for (int j = 0; j < nd->cnt; ++j) { n       cerr << number <<    ; n     } n   } n   print_numbers_rec(nd->c[0], number << 1, i - 1); n   print_numbers_rec(nd->c[1], (number << 1) | 1, i - 1); n } n void print_numbers() { n   print_numbers_rec(root, 0, 31); n   cerr << endl; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(nullptr); n   int n; n   cin >> n; n   vector<ui32> a(n); n   for (int i = 0; i < n; ++i) cin >> a[i]; n   for (int i = 0; i < n; ++i) { n     ui32 t; n     cin >> t; n     insert(t); n   } n   vector<int> o(n); n   for (int i = 0; i < n; ++i) { n     node *cur = root; n     ui32 xored = 0; n     for (int j = 31; j >= 0; --j) { n       int bit = (a[i] >> j) & 1; n       if (cur->c[bit] != nullptr) { n         cur = cur->c[bit]; n         xored = (xored << 1) | bit; n       } else { n         cur = cur->c[bit ^ 1]; n         xored = (xored << 1) | (bit ^ 1); n       } n     } n     o[i] = a[i] ^ xored; n     remove(xored); n   } n   for (int i = 0; i < n; ++i) { n     cout << o[i] <<    n [i + 1 == n]; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int MAX = 1e5 + 55; n const int inf = 1e9 + 77; n const int MOD = 1e9 + 7; n const double eps = 1e-7; n map<int, int> mp; n pair<int, int> pi; n vector<long long> v; n map<char, int> mp1; n deque<int> d; n int x[MAX], y[MAX]; n char c[MAX]; n int cnt[MAX]; n int main() { n   int n; n   cin >> n; n   long long even = 0; n   long long odd = 0; n   for (int i = 0; i < n; i++) { n     long long a; n     cin >> a; n     if (a % 2 != 0) n       odd++; n     else n       even++; n   } n   if (odd == 0) n     cout <<  Second ; n   else n     cout <<  First ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long n, k; n   cin >> n >> k; n   if (k == 0 || k == n) { n     cout <<  0 0 ; n     return 0; n   } n   cout << min(1LL, n - k) <<     << min(n - k, k * 2LL); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, tx, ty, ans = 0; n   vector<int> x, y; n   cin >> n; n   for (int i = 0; i <= n; i++) { n     cin >> tx >> ty; n     x.push_back(tx), y.push_back(ty); n   } n   for (int i = 1; i < n; i++) { n     int fx = x[i + 1], fy = y[i + 1]; n     int cx = x[i], cy = y[i]; n     int px = x[i - 1], py = y[i - 1]; n     if ((fy > cy && cx > px) || (cx > fx && cy > py) || (fy < cy && cx < px) || n         (fx > cx && cy < py)) n       ans++; n   } n   cout << ans <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long int INF = 9223372036854775807; n const long long int mod = 1000000007; n long long int MOD(long long int a, long long int b) { n   if (a > b) n     return a - b; n   else n     return b - a; n } n long long int max3(long long int a, long long int b, long long int c) { n   return max(c, max(a, b)); n } n long long int min3(long long int a, long long int b, long long int c) { n   return min(a, min(b, c)); n } n long long int power(long long int x, long long int y) { n   long long int res = 1; n   while (y > 0) { n     if (y & 1) res = (res * x); n     y = y >> 1; n     x = (x * x); n   } n   return res; n } n long long int power1(long long int x, long long int y) { n   long long int res = 1; n   while (y > 0) { n     if (y & 1) res = (res * x) % mod; n     y = y >> 1; n     x = (x * x) % mod; n   } n   return res % mod; n } n long long int logg(long long int a) { n   long long int x = 0; n   while (a > 1) { n     x++; n     a /= 2; n   } n   return x; n } n long long int gcd(long long int a, long long int b) { n   if (a == 0) return b; n   return gcd(b % a, a); n } n long long int abso(long long int x) { n   if (x < 0) { n     return -x; n   } n   return x; n } n long long int ceiling(long long int a, long long int b) { n   if (a % b == 0) { n     return a / b; n   } else { n     return a / b + 1; n   } n } n long long int modinv(long long int x) { return power1(x, mod - 2); } n vector<vector<int>> mat; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   ; n   int n, p; n   cin >> n >> p; n   mat.resize(p); n   string str; n   cin >> str; n   for (int i = 0; i < p; i++) { n     mat[i].resize(p); n     for (int j = 0; j < p; j++) { n       cin >> mat[i][j]; n     } n   } n   vector<vector<vector<int>>> pair_masks(p); n   vector<int> cnt(p); n   for (int i = 0; i < n; i++) { n     cnt[str[i] -  a ]++; n   } n   for (int i = 0; i < p; i++) { n     pair_masks[i].resize(p); n     for (int j = 0; j < p; j++) { n       pair_masks[i][j].resize(1 << p); n     } n   } n   for (int i = 0; i < p; i++) { n     for (int j = 0; j < p; j++) { n       int prev = -1; n       int mask = 0; n       for (int k = 0; k < n; k++) { n         if (str[k] ==  a  + i || str[k] ==  a  + j) { n           if (prev == -1) { n             prev = str[k] -  a ; n             mask = 0; n           } else { n             if (i != j) { n               if (prev == str[k] -  a ) { n                 mask = 0; n               } else { n                 pair_masks[i][j][mask] = 1; n                 prev = str[k] -  a ; n                 mask = 0; n               } n             } else { n               pair_masks[i][j][mask] = 1; n               prev = str[k] -  a ; n               mask = 0; n             } n           } n         } else { n           mask = mask | 1 << (str[k] -  a ); n         } n       } n     } n   } n   for (int i = 0; i < p; i++) { n     for (int j = 0; j < p; j++) { n       for (int m = 0; m < p; m++) { n         for (int k = 0; k < (1 << p); k++) { n           if (m != i && m != j) { n             pair_masks[i][j][k | 1 << m] += pair_masks[i][j][k]; n           } n         } n       } n     } n   } n   for (int i = 0; i < p; i++) { n     for (int j = 0; j < p; j++) { n       for (int k = 0; k < (1 << p); k++) { n         pair_masks[i][j][k] = min(1, pair_masks[i][j][k]); n       } n     } n   } n   vector<int> dp(1 << p, 0); n   dp[0] = 1; n   for (int mask = 1; mask < (1 << p); mask++) { n     bool c = false; n     for (int i = 0; i < p; i++) { n       for (int j = 0; j < p; j++) { n         if (pair_masks[i][j][mask] && !mat[i][j]) { n           c = true; n           break; n         } n       } n       if (c) { n         break; n       } n     } n     if (c) { n       dp[mask] = 0; n       continue; n     } n     for (int i = 0; i < p; i++) { n       if (mask & (1 << i)) { n         if (dp[mask ^ (1 << i)]) { n           dp[mask] = 1; n         } n       } n     } n   } n   int ans = n; n   for (int mask = 0; mask < (1 << p); mask++) { n     if (dp[mask]) { n       int len = n; n       for (int k = 0; k < p; k++) { n         if (mask & 1 << k) { n           len -= cnt[k]; n         } n       } n       ans = min(ans, len); n     } n   } n   cout << ans; n } n


#include <bits/stdc++.h> n using namespace std; n long long INF = 1e18; n int INFi = 2e9; n double eps = 1e-6; n long long mod = 1e9 + 7; n vector<char> used; n vector<vector<int>> edge; n vector<int> p; n vector<int> cnt; n int sz = 0; n void add(int beg, int end) { n   while (beg != end) { n     cnt[beg]++; n     beg = p[beg]; n   } n } n void sub(int beg) { n   while (used[beg] == 2) { n     cnt[beg]--; n     beg = p[beg]; n   } n } n void dfs(int v) { n   used[v] = 1; n   for (int u : edge[v]) { n     if (used[u] == -1) { n       continue; n     } n     if (used[u] == 2) { n       sub(u); n       continue; n     } n     if (used[u] == 1) { n       add(v, u); n       sz++; n       continue; n     } n     p[u] = v; n     dfs(u); n   } n   used[v] = 2; n } n int main() { n   cin.tie(0); n   cout.tie(0); n   ios_base::sync_with_stdio(false); n   int n, m; n   cin >> n >> m; n   edge.resize(n); n   used.resize(n); n   p.resize(n); n   cnt.resize(n); n   int t1, t2; n   for (int i = 0; i < m; i++) { n     cin >> t1 >> t2; n     t1--; n     t2--; n     edge[t1].push_back(t2); n   } n   for (int i = 0; i < n; i++) { n     if (!used[i]) dfs(i); n     for (int j = 0; j < n; j++) { n       if (used[j] == 2) used[j] = -1; n     } n   } n   for (int i = 0; i < n; i++) { n     if (cnt[i] == sz) { n       cout <<  YES n ; n       return 0; n     } n   } n   if (sz == 1) { n     cout <<  YES n ; n   } else { n     cout <<  NO n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long int power(long long int x, long long int y) { n   if (y == 0) n     return 1; n   else if (y % 2 == 0) n     return power(x, y / 2) * power(x, y / 2); n   else n     return x * power(x, y / 2) * power(x, y / 2); n } n double slope(long long int x1, long long int y1, long long int x2, n              long long int y2) { n   return (double)((double)(y2 - y1) / (x2 - x1)); n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   long long int ax, ay, bx, by, cx, cy; n   cin >> ax >> ay >> bx >> by >> cx >> cy; n   long long int dab = power((bx - ax), 2) + power((by - ay), 2); n   long long int dbc = power((cx - bx), 2) + power((cy - by), 2); n   if (dab != dbc) { n     cout <<  No n ; n   } else if (slope(ax, ay, bx, by) == slope(bx, by, cx, cy)) { n     cout <<  No n ; n   } else { n     cout <<  Yes n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 1e5 + 5; n int n, m, ROOT, head; n bool vis[MAXN]; n int depth[MAXN], deg[MAXN]; n vector<int> adj[MAXN]; n vector<int> c; n stack<int> stk; n pair<bool, vector<int> > independentSet() { n   set<pair<int, int> > s; n   vector<int> v; n   for (auto i = 0; i < (n); i++) s.insert(make_pair(deg[i], i)); n   while (!s.empty()) { n     int node = s.begin()->second; n     s.erase(s.begin()); n     if (vis[node]) continue; n     v.push_back(node); n     vis[node] = true; n     for (auto u : adj[node]) { n       if (vis[u]) continue; n       vis[u] = true; n       for (auto v : adj[u]) { n         if (v == u) continue; n         --deg[v]; n         s.insert(make_pair(deg[v], v)); n       } n     } n   } n   if (v.size() >= ROOT) n     return make_pair(true, v); n   else n     return make_pair(false, vector<int>{}); n } n void simpleCycle() { n   while (!stk.empty() && stk.top() != head) { n     c.push_back(stk.top()); n     stk.pop(); n   } n   c.push_back(head); n   reverse(c.begin(), c.end()); n   cout <<  2 n  << c.size() <<   n ; n   for (auto u : c) cout << u + 1 <<    ; n   exit(0); n } n void dfs(int node, int d) { n   if (depth[node] > 0) { n     if (d - depth[node] == ROOT) { n       head = node; n       simpleCycle(); n     } n     return; n   } n   depth[node] = d; n   stk.push(node); n   for (auto u : adj[node]) { n     dfs(u, d + 1); n   } n   stk.pop(); n } n signed main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   cin >> n >> m; n   for (auto i = 0; i < (m); i++) { n     int u, v; n     cin >> u >> v; n     --u; n     --v; n     adj[u].push_back(v); n     adj[v].push_back(u); n     ++deg[u]; n     ++deg[v]; n   } n   ROOT = (int)sqrt(n); n   if (ROOT * ROOT < n) ++ROOT; n   auto is = independentSet(); n   if (is.first) { n     cout <<  1 n ; n     for (auto i = 0; i < (ROOT); i++) cout << is.second[i] + 1 <<    ; n     exit(0); n   } else { n     dfs(0, 1); n   } n } n


#include <bits/stdc++.h> n using namespace std; n namespace whatever { n int readu() { n   char ch = getchar(); n   while (!isdigit(ch)) ch = getchar(); n   int value = ch -  0 ; n   ch = getchar(); n   while (isdigit(ch)) { n     value = value * 10 + ch -  0 ; n     ch = getchar(); n   } n   return value; n } n long long readull() { n   char ch = getchar(); n   while (!isdigit(ch)) ch = getchar(); n   long long value = ch -  0 ; n   ch = getchar(); n   while (isdigit(ch)) { n     value = value * 10 + ch -  0 ; n     ch = getchar(); n   } n   return value; n } n void writeull(long long n) { n   if (n < 10) n     putchar(n +  0 ); n   else { n     writeull(n / 10); n     putchar(n % 10 +  0 ); n   } n } n void run() { n   int n = readu(); n   long long t = readull(); n   static int a[200000]; n   for (int i = 0; i < n; ++i) a[i] = readu(); n   long long result = 0; n   while (n != 0) { n     long long sum = accumulate(a, a + n, 0ll); n     result += t / sum * n; n     t %= sum; n     for (int i = 0; i < n; ++i) n       if (t >= a[i]) { n         t -= a[i]; n         ++result; n       } n     n = remove_if(a, a + n, [t](int a) { return a > t; }) - a; n   } n   writeull(result); n   putchar(  n ); n } n }  // namespace whatever n int main() { whatever::run(); } n


#include <bits/stdc++.h> n using namespace std; n const int buffer_size = 1 << 20; n int rd_char() { n   static char buf[buffer_size]; n   static int l = buffer_size, r = buffer_size; n   if (l == r && r == buffer_size) { n     r = fread(buf, sizeof(char), 1 << 20, stdin); n     l = 0; n   } n   if (l == r) return EOF; n   return buf[l++]; n } n unsigned int rd_uint() { n   int c; n   unsigned int x = 0; n   while ( 0  <= (c = rd_char())) x = x * 10 + c -  0 ; n   return x; n } n int rd_int() { n   bool neg = false; n   int c = rd_char(); n   long long x = 0; n   if (c ==  - ) n     neg = true; n   else n     x = c -  0 ; n   while ( 0  <= (c = rd_char())) x = x * 10 + c -  0 ; n   return neg ? -x : x; n } n unsigned long long rd_ull() { n   int c; n   unsigned long long x = 0; n   while ( 0  <= (c = rd_char())) x = x * 10 + c -  0 ; n   return x; n } n long long rd_ll() { n   bool neg = false; n   int c = rd_char(); n   long long x = 0; n   if (c ==  - ) n     neg = true; n   else n     x = c -  0 ; n   while ( 0  <= (c = rd_char())) x = x * 10 + c -  0 ; n   return neg ? -x : x; n } n string rd_str() { n   string ret; n   int c; n   while ((c = rd_char()) !=   n  && c !=     && c != EOF) ret += c; n   return ret; n } n struct writer_struct { n   char buf[buffer_size]; n   int l = 0; n   ~writer_struct() { fwrite(buf, sizeof(char), l, stdout); } n } writer; n void flush() { n   fwrite(writer.buf, sizeof(char), writer.l, stdout); n   fflush(stdout); n   writer.l = 0; n } n void wt(char c) { n   if (writer.l == buffer_size) { n     fwrite(writer.buf, sizeof(char), buffer_size, stdout); n     writer.l = 0; n   } n   writer.buf[writer.l++] = c; n } n void wt(unsigned int x) { n   if (x == 0) n     wt( 0 ); n   else { n     char f[10]; n     int sz = 0; n     while (x) { n       f[sz++] = x % 10 +  0 ; n       x /= 10; n     } n     while (sz) wt(f[--sz]); n   } n } n void wt(int x) { n   if (x < 0) wt( - ), x = -x; n   wt((unsigned int)x); n } n void wt(unsigned long long x) { n   if (x == 0) n     wt( 0 ); n   else { n     char f[20]; n     int sz = 0; n     while (x) { n       f[sz++] = x % 10 +  0 ; n       x /= 10; n     } n     while (sz) wt(f[--sz]); n   } n } n void wt(long long x) { n   if (x < 0) wt( - ), x = -x; n   wt((unsigned long long)x); n } n void wt(const char* s) { n   while (*s !=   0 ) wt(*s++); n } n void wt(const string& s) { n   for (char c : s) wt(c); n } n const int LIM = 3e6; n int N, C[LIM + 1]; n long long cnt[LIM + 1]; n int main() { n   N = rd_uint(); n   for (int i = 0; i < N; i++) C[rd_uint()]++; n   for (int i = 1; i <= LIM; i++) { n     if ((long long)i * i <= LIM) n       cnt[i * i] += (long long)C[i] * (C[i] - 1); n     else n       cnt[LIM] += (long long)C[i] * (C[i] - 1); n     int rest = N - C[i]; n     for (int j = 1; (long long)i * j <= LIM; j++) { n       if (i != j) cnt[i * j] += (long long)C[i] * C[j], rest -= C[j]; n     } n     cnt[LIM] += (long long)C[i] * rest; n   } n   for (int i = LIM; i >= 1; i--) cnt[i - 1] += cnt[i]; n   int M = rd_uint(); n   for (; M--;) { n     int p = rd_uint(); n     wt(cnt[p]); n     wt(  n ); n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(nullptr); n   int n, i; n   vector<int> arr, sol, orig; n   cin >> n; n   arr.resize(n); n   for (i = 0; i < n; ++i) { n     cin >> arr[i]; n   } n   orig = arr; n   sort(orig.begin(), orig.end()); n   for (i = 0; i < n; ++i) { n     for (int k = 0; k + 1 < arr.size(); ++k) { n       while (arr[i] - arr[i + 1] >= 2) ++arr[i + 1], --arr[i]; n       while (arr[i] - arr[i + 1] <= -2) --arr[i + 1], ++arr[i]; n     } n     int j = -1, x = -1; n     for (int k = 0; k < arr.size(); ++k) { n       if (arr[k] > x) { n         x = arr[k]; n         j = k; n       } n     } n     sol.push_back(arr[j]); n     arr.erase(arr.begin() + j); n   } n   reverse(sol.begin(), sol.end()); n   cout << (sol == orig ?  YES  :  NO ) <<   n ; n } n


#include<bits/stdc++.h> n using namespace std; n int main() n {   n     int t,p,k,q,b; n     cin>>t; n     string s; n     while(t--) n     { n         cin>>s; n         k=0; q=0; b=0; p=0; n         for(int i=0;i<s.size();i++) n         { n             if(s[0]== ) || s[s.size()-1]== ( ) n             { n                 p=0; break;} n             else if( s[i]== ( ) n             { n                 k++; n             } n             else if(s[i]== ? ) n               q++; n              else if(s[i]== ) ) n              b++; n         } n          n         if(k==b && q%2!=0 && k>0) n         p=0; n         else if(k==b&& q%2==0&& k>0) n         p=1; n         else if(q== abs(k-b) && k>0) n         p=1; n         if(p==1) n         cout<< yes <<endl; n         else n         cout<< NO <<endl; n     } n }


#include<bits/stdc++.h> n using namespace std; n #define il inline n #define rg register n #define int long long n #define M 998244353 n const int P=3,Pn=332748118; n il int read() n { n    int re=0,k=1;char ch=getchar(); n     while(ch> 9 ||ch< 0 ){if(ch== - )k=-1;ch=getchar();} n     while(ch<= 9 &&ch>= 0 ){re=re*10+ch-48;ch=getchar();} n     return re*k; n } n il void write(long long x) n { n     if(x<0)return putchar( - ),write(-x),void(); n     if(x<10)return putchar(x+ 0 ),void(); n     return write(x/10),putchar(x%10+ 0 ),void(); n } n signed main() n { n  int T,a,b,c,d; n  T=read(); n  while(T--) n  { n   d=read();a=read();b=read();c=read(); n   int ans=min((a-d%a)%a,min((b-d%b)%b,(c-d%c)%c)); n   write(ans);puts(  ); n  } n }


#include <bits/stdc++.h> n using namespace std; n const int N = 1e4 + 10; n const double eps = 1e-8; n const double pi = acos(-1); n int main() { n   int T; n   cin >> T; n   while (T--) { n     string s; n     cin >> s; n     int cnt = 0; n     bool flag0 = false, flag1 = false, flag2 = false; n     for (int i = 0; i < s.size() - 1; i++) { n       if (s[i] ==  0 ) flag0 = true; n       if (s[i] ==  1 ) flag1 = true; n       if (s[i] ==  2 ) flag2 = true; n       if (s[i] ==  0  && s[i + 1] !=  0 ) cnt++; n     } n     if (s[s.size() - 1] ==  0 ) cnt++; n     if (s[s.size() - 1] ==  0 ) flag0 = true; n     if (s[s.size() - 1] ==  1 ) flag1 = true; n     if (s[s.size() - 1] ==  2 ) flag2 = true; n     if (!flag0) n       cnt = min(cnt, 0); n     else if (!flag1) n       cnt = min(cnt, 1); n     else if (!flag2) n       cnt = min(cnt, 2); n     cout << cnt << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 5e3 + 5; n int readint() { n   int x = 0, f = 1; n   char s = getchar(); n   while (s <  0  || s >  9 ) { n     if (s ==  - ) f = -1; n     (s = getchar()); n   } n   while (s >=  0  && s <=  9 ) { n     x = (x << 3) + (x << 1) + (s ^ 48); n     (s = getchar()); n   } n   return x * f; n } n long long dp[maxn][maxn]; n int a[maxn]; n int q[maxn]; n int main() { n   int n = readint(), k = readint(), x = readint(); n   for (int i = 1; i <= n; i++) { n     a[i] = readint(); n   } n   memset(dp, -0x3f, sizeof(dp)); n   dp[0][0] = 0; n   for (int j = 1; j <= x; j++) { n     int head = 1, tail = 0; n     q[++tail] = 0; n     for (int i = 1; i <= n; i++) { n       while (head <= tail && i - q[head] > k) head++; n       dp[i][j] = dp[q[head]][j - 1] + a[i] + 0ll; n       while (head <= tail && dp[q[tail]][j - 1] <= dp[i][j - 1]) tail--; n       q[++tail] = i; n     } n   } n   long long ans = -1; n   for (int i = n; i >= n - k + 1; i--) { n     ans = max(ans, dp[i][x]); n   } n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const double PI = 3.1415926535897932384626433; n const long long mod = 1000000007; n const int INF = 0x3f3f3f3f; n const double eps = 1e-8; n template <class T> n T gcd(T a, T b) { n   if (!b) return a; n   return gcd(b, a % b); n } n const int maxn = 1e5 + 10; n vector<int> edg[maxn], vi, ans; n int now[maxn], nxt[maxn], len1, len2, n, m, vis[maxn], st[maxn], flg; n void dfs(int u) { n   if (st[u] < 0) flg = 0; n   if (flg == 0) return; n   vis[u] = 1; n   ans.push_back(u); n   for (int i = 0; i < edg[u].size(); i++) { n     int nxt = edg[u][i]; n     if (st[nxt] < 0) flg = 0; n     if (!vis[nxt]) dfs(nxt); n   } n } n int deal(int *a1, int *a2) { n   int len = min(len1, len2); n   for (int i = 0; i < len; i++) { n     if (a1[i] < a2[i]) { n       edg[a2[i]].push_back(a1[i]); n       return 1; n     } else if (a1[i] > a2[i]) { n       vi.push_back(a1[i]); n       if (st[a1[i]] == -1 || st[a2[i]] == 1) return 0; n       st[a1[i]] = 1; n       st[a2[i]] = -1; n       return 1; n     } n   } n   return len1 <= len2; n } n int solve() { n   memset(st, 0, sizeof(st)); n   int *a1 = now, *a2 = nxt; n   scanf( %d , &len1); n   for (int i = 0; i < len1; i++) scanf( %d , &a1[i]); n   flg = 1; n   for (int t = 1; t < n; t++) { n     scanf( %d , &len2); n     for (int i = 0; i < len2; i++) scanf( %d , &a2[i]); n     if (deal(a1, a2) == 0) flg = 0; n     swap(a1, a2); n     len1 = len2; n   } n   memset(vis, 0, sizeof(vis)); n   for (int i = 0; i < vi.size(); i++) n     if (!vis[vi[i]]) dfs(vi[i]); n   return flg; n } n int main() { n   scanf( %d , &n); n   scanf( %d , &m); n   if (solve()) { n     printf( Yes n%d n , ans.size()); n     for (int i = 0; i < ans.size(); i++) printf( %d  , ans[i]); n     puts(  ); n   } else { n     printf( No n ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, k, a[5000] = {0}; n   cin >> n >> k; n   for (int i = 1; i <= k; i++) { n     int la = 0; n     cin >> la; n     if (la == 1) { n       int p, q; n       cin >> p >> q; n       a[p] = 1; n       a[q] = 1; n     } else if (la == 2) { n       int p; n       cin >> p; n       a[p] = 1; n     } n   } n   int w = 0; n   for (int i = 1; i < n; i++) { n     if (a[i] == 0) { n       w++; n     } n   } n   int max = w, min = w + 1; n   for (int i = 1; i <= n; i++) { n     if (a[i] == 0 && a[i + 1] == 0) { n       min--; n       i++; n     } n   } n   cout << min <<     << max << endl; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 2001; n const int inf = 1000000000; n int d[maxn], a[maxn], n, k; n bool check(int x) { n   int ans = inf; n   for (int i = 1; i <= n; i++) { n     d[i] = i - 1; n     for (int j = 1; j < i; j++) n       if (abs(a[i] - a[j]) <= (i - j) * 1ll * x) n         d[i] = min(d[i], d[j] + i - j - 1); n     ans = min(ans, d[i] + n - i); n   } n   if (ans <= k) return true; n   return false; n } n int main() { n   cin >> n >> k; n   int l = -1, r = 0, mid; n   for (int i = 1; i <= n; i++) { n     cin >> a[i]; n     r = max(r, (i == 1 ? r : abs(a[i] - a[i - 1]))); n   } n   while (r - l > 1) { n     mid = (l + 0ll + r) / 2; n     if (check(mid)) n       r = mid; n     else n       l = mid; n   } n   cout << r; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int mod = 998244353; n const int INF = 1e9; n const int maxn = 1e6 + 10; n const double eps = 1e-8; n int add(int x, int y) { n   x += y; n   return x >= mod ? x - mod : x; n } n int sub(int x, int y) { n   x -= y; n   return x < 0 ? x + mod : x; n } n int mul(int x, int y) { return 1LL * x * y % mod; } n int dep[maxn], tot, ans; n vector<pair<int, int> > g[maxn]; n queue<pair<int, int> > q; n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   int n; n   cin >> n; n   bool ok = false; n   for (int i = 1; i <= n; i++) { n     vector<int> fac; n     int x; n     cin >> x; n     for (int d = 2; d * d <= x; d++) n       if (x % d == 0) { n         int cnt = 0; n         while (x % d == 0) cnt++, x /= d; n         if (cnt & 1) fac.push_back(d); n       } n     if (x > 1) fac.push_back(x); n     if (fac.size() == 2) { n       int p = fac[0], q = fac[1]; n       g[p].push_back(pair<int, int>{q, ++tot}), n           g[q].push_back(pair<int, int>{p, tot}); n     } else if (fac.size() == 1) { n       int p = fac[0]; n       g[p].push_back(pair<int, int>{1, ++tot}), n           g[1].push_back(pair<int, int>{p, tot}); n     } else n       ok = true; n   } n   if (ok) { n     cout << 1 << endl; n     return 0; n   } n   ans = INF; n   fill(dep + 1, dep + 1000000 + 1, INF); n   for (int i = 1; i <= 1000; i++) { n     vector<int> save; n     dep[i] = 0, q.push(pair<int, int>{i, 0}); n     while (!q.empty()) { n       pair<int, int> x = q.front(); n       q.pop(); n       int u = x.first, pre = x.second; n       save.push_back(u); n       for (auto e : g[u]) { n         int v = e.first, id = e.second; n         if (id == pre) continue; n         if (dep[v] == INF) { n           dep[v] = dep[u] + 1; n           q.push(pair<int, int>{v, id}); n         } else n           ans = min(ans, dep[u] + dep[v] + 1); n       } n     } n     for (auto x : save) dep[x] = INF; n   } n   if (ans == INF) n     cout << -1 << endl; n   else n     cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 2010; n int n; n int t[maxn], c[maxn]; n long long d[maxn][maxn]; n int main() { n   cin >> n; n   for (int i = 0; i < n; i++) cin >> t[i] >> c[i]; n   for (int i = 0; i < n + 1; i++) d[0][i] = 1ll << 62; n   d[0][0] = 0; n   for (int i = 1; i < n + 1; i++) { n     d[i][0] = 0; n     for (int j = 1; j < n + 1; j++) { n       d[i][j] = d[i - 1][j]; n       int tmp = max(0, j - t[i - 1] - 1); n       if (d[i - 1][tmp] + c[i - 1] < d[i][j]) n         d[i][j] = d[i - 1][tmp] + c[i - 1]; n     } n   } n   cout << d[n][n] << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int l[500000], r[500000]; n long long a[500000]; n int L, R; n int n, q; n int main() { n   scanf( %d%d , &n, &q); n   a[0] = 0; n   for (int i = 1; i <= n; i++) scanf( %lld , &a[i]); n   for (int i = n; i >= 1; i--) a[i] = abs(a[i] - a[i - 1]); n   stack<int> S; n   for (int i = 2; i <= n; i++) { n     while (!S.empty() && a[i] >= a[S.top()]) { n       r[S.top()] = i - 1; n       S.pop(); n     } n     if (!S.empty()) n       l[i] = S.top() + 1; n     else n       l[i] = 2; n     S.push(i); n   } n   while (!S.empty()) { n     r[S.top()] = n; n     S.pop(); n   } n   while (q--) { n     scanf( %d%d , &L, &R); n     L++; n     long long ans = 0; n     for (int i = L; i <= R; i++) { n       ans += (long long)(i - max(L, l[i]) + 1) * n              (long long)(min(R, r[i]) - i + 1) * a[i]; n     } n     cout << ans << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n multiset<int> val[33]; n long long sum[33]; n int n = 0; n int calc() { n   long long allsum = 0; n   int res = 0; n   for (int i = 0; i < 30; i++) { n     if (val[i].empty()) continue; n     if (allsum * 2 < *(val[i].begin())) res++; n     allsum += sum[i]; n   } n   return res; n } n int main() { n   int q; n   scanf( %d , &q); n   while (q--) { n     char s[2]; n     int x; n     scanf( %s%d , s, &x); n     int mn = 0; n     for (int i = 0; i < 30; i++) n       if (x >> i & 1) mn = i; n     if (s[0] ==  - ) { n       n--; n       sum[mn] -= x; n       val[mn].erase(val[mn].find(x)); n     } else { n       sum[mn] += x; n       val[mn].insert(x); n       n++; n     } n     printf( %d n , n - calc()); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n namespace IO { n const int sz = 1 << 20; n char a[sz + 5], b[sz + 5], *p1 = a, *p2 = a, *t = b, p[105]; n inline char gc() { n   return getchar(); n   if (p1 == p2) p2 = (p1 = a) + fread(a, 1, sz, stdin); n   return *p1++; n } n template <class T> n inline T read() { n   T ret = 0; n   char c = gc(); n   int f = 1; n   for (; c <  0  || c >  9 ; c = gc()) n     if (c ==  - ) f = -1; n   for (; c >=  0  && c <=  9 ; c = gc()) ret = ret * 10 + (c -  0 ); n   return ret * f; n } n template <class T> n inline void read(T& ret) { n   ret = read<T>(); n } n inline void flush() { fwrite(b, 1, t - b, stdout), t = b; } n inline void pc(char x) { n   *t++ = x; n   if (t - b == sz) flush(); n } n template <class T> n inline void print(T x, char c =   n ) { n   if (x < 0) x = -x, pc( - ); n   if (x == 0) pc( 0 ); n   int t = 0; n   for (; x; x /= 10) p[++t] = x % 10 +  0 ; n   for (; t; --t) pc(p[t]); n   pc(c); n } n struct F { n   ~F() { flush(); } n } f; n }  // namespace IO n using IO::print; n using IO::read; n const int mo = 1e9 + 7; n inline int add(int a, int b) { return a + b < mo ? a + b : a + b - mo; } n inline int dec(int a, int b) { return a < b ? a + mo - b : a - b; } n inline int mul(int a, int b) { return 1LL * a * b % mo; } n const int N = 1e3 + 10; n int k; n int c[N], s[N]; n int C[N][N]; n int f[N]; n int main() { n   for (int i = 0; i <= 1000; i++) C[i][0] = C[0][i] = 1; n   C[0][0] = 0; n   for (int i = 1; i <= 1000; i++) n     for (int j = 1; j <= 1000; j++) C[i][j] = add(C[i - 1][j], C[i][j - 1]); n   read(k); n   for (int i = 1; i <= k; read(c[i]), s[i] = add(s[i - 1], c[i]), i++) n     ; n   f[1] = 1; n   for (int i = 2; i <= k; i++) f[i] = mul(f[i - 1], C[s[i - 1]][c[i] - 1]); n   print(f[k]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long n, a[2000005], num[2000005], ans; n long long ksm(long long x, long long y) { n   long long ans = 1, t = x; n   while (y) { n     if (y & 1) ans = ans * t % 1000000007; n     t = t * t % 1000000007; n     y >>= 1; n   } n   return ans; n } n int main() { n   scanf( %lld , &n); n   for (long long i = 1; i <= n; i++) { n     scanf( %lld , &a[i]); n     if (a[i] != -1) num[a[i]]++; n   } n   for (long long i = 1; i <= n; i++) { n     if (a[i] == -1) ans = (ans + ksm(2, num[i]) - 1 + 1000000007) % 1000000007; n   } n   printf( %lld , (ksm(2, n - 1) - 1 - ans + 1000000007) % 1000000007); n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 100010, M = N * 2, mod = 1e9 + 7; n const double PI = acos(-1.0), eps = 1e-6; n int T, n, m, k, c1, c2; n int a[N * 10], b[N], tot, cnt[N], s[N * 10], ans; n map<int, int> mp; n vector<int> v; n string g[N * 10]; n signed main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   cin >> n >> m; n   for (int i = 0; i < n; i++) cin >> g[i]; n   for (int i = 0; i < n; i++) n     for (int j = 0; j < m; j++) n       if (i - 1 >= 0 && g[i - 1][j] ==  X  && j - 1 >= 0 && g[i][j - 1] ==  X ) n         a[j + 1] = 1; n   for (int i = 1; i <= m; i++) s[i] = s[i - 1] + a[i]; n   int q; n   cin >> q; n   while (q--) { n     int l, r; n     cin >> l >> r; n     if (s[r] - s[l] > 0) n       cout <<  NO  <<   n ; n     else n       cout <<  YES  <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n map<long long, bool> mapa; n long long _abs(long long a) { n   if (a >= 0) n     return a; n   else n     return -a; n } n int main() { n   long long b1, q, l, m; n   cin >> b1 >> q >> l >> m; n   for (int i = 0; i < m; i++) { n     long long a; n     cin >> a; n     mapa[a] = 1; n   } n   if (b1 == 0LL) { n     if (!mapa.count(0)) n       cout <<  inf n ; n     else n       cout <<  0 n ; n     return 0; n   } n   if (q == 0LL) { n     if (_abs(b1) > l) n       cout <<  0 n ; n     else if (!mapa.count(0)) n       cout <<  inf n ; n     else if (!mapa.count(b1)) n       cout <<  1 n ; n     else n       cout <<  0 n ; n     return 0; n   } else if (q == 1LL) { n     if (_abs(b1) <= l && !mapa.count(b1)) n       cout <<  inf n ; n     else n       cout <<  0 n ; n     return 0; n   } else if (q == -1LL) { n     if (_abs(b1) > l) n       cout <<  0 n ; n     else if (!mapa.count(b1) || !mapa.count(-b1)) n       cout <<  inf n ; n     else n       cout <<  0 n ; n     return 0; n   } n   long long val = b1; n   int cont = 0; n   while (_abs(val) <= l) { n     if (!mapa.count(val)) cont++; n     val *= q; n   } n   cout << cont <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n int n, k, p; n int tot, head[100010], next1[100010 << 1], to[100010 << 1]; n int deep[100010], cnt[100010]; n void add(int x, int y) { n   to[++tot] = y; n   next1[tot] = head[x]; n   head[x] = tot; n } n void dfs(int x, int f) { n   deep[x] = deep[f] + 1; n   cnt[deep[x]]++; n   for (int i = head[x]; i; i = next1[i]) { n     int y = to[i]; n     if (y == f) continue; n     dfs(y, x); n   } n } n int main() { n   scanf( %d%d%d , &n, &k, &p); n   for (int i = 1; i < n; i++) { n     int x, y; n     scanf( %d%d , &x, &y); n     add(x, y); n     add(y, x); n   } n   dfs(1, 0); n   sort(deep + 1, deep + n + 1); n   int l = 2, r = 2, ans = 1, tmp = 0; n   while (1) { n     if (l >= n || r >= n) break; n     r++; n     if (deep[r] != deep[r - 1]) tmp += (r - l); n     while (tmp > p || r - l + 1 > k) { n       tmp -= (deep[r] - deep[l]); n       l++; n     } n     ans = max(ans, (r - l + 1)); n   } n   printf( %d , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   int k, b, n; n   cin >> k >> b >> n; n   map<int, int> freq; n   freq[0]++; n   int cont_zeros = 0; n   int cur_mod = 0; n   long long total_zeros = 0; n   long long total_mod = 0; n   for (int i = 0; i < n; ++i) { n     int u; n     cin >> u; n     if (u == 0) { n       cont_zeros++; n       total_zeros += cont_zeros; n     } else { n       cont_zeros = 0; n     } n     cur_mod = (cur_mod + u) % (k - 1); n     int tt = (k - 1 + cur_mod - b) % (k - 1); n     total_mod += freq[tt]; n     freq[cur_mod]++; n   } n   if (b == 0) { n     cout << total_zeros <<   n ; n   } else if (b == k - 1) { n     cout << total_mod - total_zeros <<   n ; n   } else { n     cout << total_mod <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void fast() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n } n const int N = 1E5 + 5; n long long n, l, r, ql, qr; n long long arr[N], pre[N]; n int main() { n   fast(); n   cin >> n >> l >> r >> ql >> qr; n   for (int i = 1; i <= n; i++) cin >> arr[i]; n   for (int i = 1; i <= n; ++i) { n     pre[i] = arr[i] + pre[i - 1]; n   } n   long long mini = n       min(pre[n] * l + (ql * (n - 1)), pre[n] * r + (qr * (n - 1))); n   for (int i = 1; i <= n; ++i) { n     long long x = (pre[i] * l) + ((pre[n] - pre[i]) * r); n     int takenL = i, takenR = n - i; n     if (takenL < takenR) { n       x += (takenR - takenL - 1) * qr; n     } else if (takenR < takenL) n       x += (takenL - takenR - 1) * ql; n     mini = min(mini, x); n   } n   cout << mini <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n struct node { n   bool w, l; n   int a[26]; n   node() { n     w = false; n     l = false; n     memset(a, -1, sizeof a); n   } n }; n node a[100030]; n long long cnt; n char second[100010]; n void insert(long long n) { n   int curr = 0; n   int i = 0; n   while (curr < n) { n     if (a[i].a[second[curr] -  a ] == -1) { n       a[i].a[second[curr] -  a ] = ++cnt; n     } n     i = a[i].a[second[curr] -  a ]; n     curr++; n   } n } n void dfs(long long curr) { n   if (curr == -1) { n     return; n   } n   int cnt = 0; n   for (int i = 0; i < 26; i++) { n     if (a[curr].a[i] != -1) { n       cnt++; n       dfs(a[curr].a[i]); n       if (a[a[curr].a[i]].w == false) { n         a[curr].w = true; n       } n       if (a[a[curr].a[i]].l == false) { n         a[curr].l = true; n       } n     } n   } n   if (cnt == 0) { n     a[curr].w = false; n     a[curr].l = true; n   } n } n int main() { n   long long n, k; n   cnt = 0; n   cin >> n >> k; n   for (int i = 0; i < n; i++) { n     scanf( %s , second); n     insert(strlen(second)); n   } n   dfs(0); n   if (a[0].w == true && a[0].l == true) { n     cout <<  First  << endl; n   } else if (a[0].w == false) { n     cout <<  Second  << endl; n   } else if (a[0].w == true && a[0].l == false) { n     if (k % 2 == 1) { n       cout <<  First  << endl; n     } else { n       cout <<  Second  << endl; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 200005; n struct point { n   int x, y; n } s[N], t[N]; n vector<int> q[2]; n int cntx, cnty, vis[N], n, now, dis[N], S, ax[N], ay[N]; n set<pair<int, int> > a[N]; n set<pair<int, int> >::iterator it; n int lowbit(int x) { return x & (-x); } n void upd(int x, pair<int, int> y) { n   while (x <= cntx) { n     a[x].insert(y); n     x += lowbit(x); n   } n } n void get(int x, int y, int id) { n   while (x) { n     it = a[x].begin(); n     for (pair<int, int> i : a[x]) { n       if (i.first > y) break; n       it++; n       if (vis[i.second]) continue; n       q[now].push_back(i.second); n       vis[i.second] = id; n     } n     a[x].erase(a[x].begin(), it); n     x -= lowbit(x); n   } n } n void bfs(int d) { n   if (!q[now].size()) return; n   if (vis[n]) return; n   now ^= 1; n   q[now].clear(); n   for (int x : q[now ^ 1]) get(t[x].x, t[x].y, x); n   for (int x : q[now]) dis[x] = d; n   bfs(d + 1); n } n int main() { n   scanf( %d , &n); n   memset(dis, 0x3f, sizeof(dis)); n   for (int i = 1; i <= n; i++) { n     scanf( %d%d%d%d , &s[i].x, &s[i].y, &t[i].x, &t[i].y); n     if (s[i].x == 0 && s[i].y == 0) { n       q[0].push_back(i); n       dis[i] = 1, vis[i] = -1; n     } n     ax[++cntx] = s[i].x, ax[++cntx] = t[i].x; n     ay[++cnty] = s[i].y, ay[++cnty] = t[i].y; n   } n   sort(ax + 1, ax + cntx + 1); n   sort(ay + 1, ay + cnty + 1); n   cntx = unique(ax + 1, ax + cntx + 1) - ax - 1; n   cnty = unique(ay + 1, ay + cnty + 1) - ay - 1; n   for (int i = 1; i <= n; i++) { n     s[i].x = lower_bound(ax + 1, ax + cntx + 1, s[i].x) - ax; n     s[i].y = lower_bound(ay + 1, ay + cnty + 1, s[i].y) - ay; n     upd(s[i].x, make_pair(s[i].y, i)); n     t[i].x = lower_bound(ax + 1, ax + cntx + 1, t[i].x) - ax; n     t[i].y = lower_bound(ay + 1, ay + cnty + 1, t[i].y) - ay; n   } n   now = 0; n   bfs(2); n   if (!vis[n]) n     puts( -1 ); n   else { n     printf( %d n , dis[n]); n     int x = n, cnt = 0, ans[N]; n     while (x != -1) { n       ans[++cnt] = x; n       x = vis[x]; n     } n     for (int i = cnt; i >= 1; i--) printf( %d  , ans[i]); n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 100005; n const long long M = 1000000007; n const long double PI = acos((long double)-1.0); n template <typename T> n struct SegmentTree { n   T data[N << 2]; n   T calc(const T &x, const T &y) const { return max(x, y); } n   void push_up(int rt) { data[rt] = calc(data[rt << 1], data[rt << 1 | 1]); } n   void build(int l, int r, int rt) { n     data[rt] = 0; n     if (l == r) { n       return; n     } n     int m = (l + r) >> 1; n     build(l, m, rt << 1); n     build(m + 1, r, rt << 1 | 1); n   } n   void update(int p, T val, int l, int r, int rt) { n     if (l == r) { n       data[rt] = val; n       return; n     } n     int m = (l + r) >> 1; n     if (p <= m) { n       update(p, val, l, m, rt << 1); n     } else { n       update(p, val, m + 1, r, rt << 1 | 1); n     } n     push_up(rt); n   } n   T query(int L, int R, int l, int r, int rt) { n     if (L > R) { n       return 0; n     } n     if (L <= l && r <= R) { n       return data[rt]; n     } n     int m = (l + r) >> 1; n     T ret = 0; n     if (L <= m) { n       ret = calc(ret, query(L, R, l, m, rt << 1)); n     } n     if (m < R) { n       ret = calc(ret, query(L, R, m + 1, r, rt << 1 | 1)); n     } n     return ret; n   } n }; n SegmentTree<long long> st; n int n; n struct Node { n   long long v; n   int id; n } a[N]; n bool cmp(const Node &a, const Node &b) { n   return a.v < b.v || (a.v == b.v && a.id > b.id); n } n int main() { n   while (~scanf( %d , &n)) { n     for (int i = 1; i <= n; i++) { n       long long r, h; n       scanf( %I64d%I64d , &r, &h); n       a[i].v = r * r * h; n       a[i].id = i; n     } n     sort(a + 1, a + n + 1, cmp); n     st.build(1, n, 1); n     long long ans = a[0].v; n     for (int i = 1; i <= n; i++) { n       long long val = st.query(1, a[i].id - 1, 1, n, 1); n       st.update(a[i].id, val + a[i].v, 1, n, 1); n       ans = max(ans, val + a[i].v); n     } n     printf( %.10f n , double(PI * ans)); n   } n } n


#include <bits/stdc++.h> n using namespace std; n const long long N = 1e6 + 1ll; n long long lim = 0, Ans = 0; n struct Plan { n   long long pos; n   long long c, p; n } a[N]; n struct Tree { n   long long c, p; n   Tree() { c = p = 0; }; n } t[N << 2ll]; n struct Acht { n   long long a, b; n   Acht() { a = b = 0; }; n }; n bool ctrl(Plan u, Plan v) { return u.pos < v.pos; } n inline void pushup(long long u) { n   t[u].c = t[u << 1ll].c + t[u << 1ll | 1ll].c; n   t[u].p = t[u << 1ll].p + t[u << 1ll | 1ll].p; n } n void update(long long u, long long l, long long r, long long pos) { n   if (l == r && l == a[pos].p) { n     t[u].c += a[pos].c; n     t[u].p += a[pos].c * a[pos].p; n     return; n   } n   long long mid = (l + r) >> 1ll; n   if (a[pos].p <= mid) n     update(u << 1ll, l, mid, pos); n   else n     update(u << 1ll | 1ll, mid + 1ll, r, pos); n   pushup(u); n } n Acht query(long long u, long long l, long long r, long long rnk) { n   if (l == r) { n     Acht res; n     if (rnk == t[u].c) n       res.a = l; n     else n       res.a = l - 1ll, res.b = l * rnk; n     return res; n   } n   if (t[u].c == rnk) { n     Acht res; n     res.a = r; n     return res; n   } n   long long mid = (l + r) >> 1ll; n   if (t[u << 1ll].c >= rnk) return query(u << 1ll, l, mid, rnk); n   return query(u << 1ll | 1ll, mid + 1ll, r, rnk - t[u << 1ll].c); n } n long long query(long long u, long long l, long long r, long long a, n                 long long b) { n   if (a <= l && r <= b) return t[u].p; n   long long mid = (l + r) >> 1ll; n   long long res = 0; n   if (a <= mid) res += query(u << 1ll, l, mid, a, b); n   if (b > mid) res += query(u << 1ll | 1ll, mid + 1ll, r, a, b); n   return res; n } n signed main() { n   long long n, k, m; n   scanf( %lld%lld%lld , &n, &k, &m); n   for (long long i = 1ll; i <= m; ++i) { n     long long y = i << 1ll, x = y - 1ll; n     scanf( %lld%lld%lld%lld , &a[x].pos, &a[y].pos, &a[x].c, &a[x].p); n     a[y].pos++, a[y].c = -a[x].c, a[y].p = a[x].p; n     lim = max(lim, a[x].p); n   } n   sort(a + 1ll, a + (m << 1ll | 1ll), ctrl); n   update(1ll, 1ll, lim, 1ll); n   for (long long i = 2; i <= (m << 1ll); ++i) { n     long long itv = a[i].pos - a[i - 1ll].pos; n     if (t[1ll].c <= k) n       Ans += t[1ll].p * itv; n     else { n       Acht tmp = query(1ll, 1ll, lim, k); n       long long res = tmp.b; n       if (tmp.a) res += query(1ll, 1ll, lim, 1ll, tmp.a); n       Ans += res * itv; n     } n     update(1ll, 1ll, lim, i); n   } n   printf( %lld , Ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct s { n   int x; n   int y; n   int str; n   s(int a, int b, int st) { n     x = a; n     y = b; n     str = st; n   } n }; n bool func(s x, s y) { return x.str > y.str; } n int main() { n   int n; n   cin >> n; n   vector<s> v; n   for (int i = 2; i <= 2 * n; i++) { n     for (int j = 1; j < i; j++) { n       int d; n       cin >> d; n       v.push_back(s(i, j, d)); n     } n   } n   sort(v.begin(), v.end(), func); n   map<int, int> m; n   for (int i = 0; i < v.size(); i++) { n     if (m.find(v[i].x) != m.end() || m.find(v[i].y) != m.end()) { n       continue; n     } n     m[v[i].x] = v[i].y; n     m[v[i].y] = v[i].x; n   } n   for (int i = 1; i <= 2 * n; i++) { n     cout << m[i] <<    ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <class T> n T read1() { n   T t = 0; n   bool vis = 0; n   char k; n   do (k = getchar()) ==  -  && (vis = 1); n   while ( 0  > k || k >  9 ); n   while ( 0  <= k && k <=  9 ) n     t = (t << 3) + (t << 1) + (k ^  0 ), k = getchar(); n   return vis ? -t : t; n } n vector<long long> x[500005]; n vector<pair<int, int> > G[500005]; n int s, k; n long long dfs(int n, int f) { n   long long t = 0; n   for (int i = 0; i < G[n].size(); ++i) n     if (G[n][i].first ^ f) { n       long long o = dfs(G[n][i].first, n); n       t += o; n       x[n].back() += G[n][i].second - o; n     } n   sort(x[n].begin(), x[n].end()); n   reverse(x[n].begin(), x[n].end()); n   int j = 0; n   while (j + 1 < k && j < x[n].size() && x[n][j] > 0) t += x[n][j++]; n   x[f].push_back(t); n   if (j < x[n].size() && x[n][j] > 0) t += x[n][j]; n   return t; n } n int main() { n   for (int T = read1<int>(); T--;) { n     s = read1<int>(), k = read1<int>(); n     for (int i = 0; i <= s; ++i) G[i].clear(), x[i].clear(); n     for (int i = 1; i < s; ++i) { n       int u = read1<int>(), v = read1<int>(), t = read1<int>(); n       G[u].push_back(make_pair(v, t)); n       G[v].push_back(make_pair(u, t)); n     } n     printf( %lld n , dfs(1, 0)); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int a, b, ans; n   while (cin >> a >> b) { n     if (a == 0 && b == 0) n       cout <<  NO  << endl; n     else { n       ans = abs(a - b); n       if (ans <= 1) n         cout <<  YES  << endl; n       else n         cout <<  NO  << endl; n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T1, typename T2> n inline void chkmin(T1 &x, T2 y) { n   if (x > y) x = y; n } n template <typename T1, typename T2> n inline void chkmax(T1 &x, T2 y) { n   if (x < y) x = y; n } n inline int readChar(); n template <class T = int> n inline T readInt(); n template <class T> n inline void writeInt(T x, char end = 0); n inline void writeChar(int x); n inline void writeWord(const char *s); n static const int buf_size = 4096; n inline int getChar() { n   static char buf[buf_size]; n   static int len = 0, pos = 0; n   if (pos == len) { n     pos = 0, len = fread(buf, 1, buf_size, stdin); n   } n   if (pos == len) { n     return -1; n   } n   return buf[pos++]; n } n inline int readChar() { n   int c = getChar(); n   while (c <= 32) { n     c = getChar(); n   } n   return c; n } n template <class T> n inline T readInt() { n   int s = 1, c = readChar(); n   T x = 0; n   if (c ==  - ) s = -1, c = getChar(); n   while ( 0  <= c && c <=  9 ) x = x * 10 + c -  0 , c = getChar(); n   return s == 1 ? x : -x; n } n static int write_pos = 0; n static char write_buf[buf_size]; n inline void writeChar(int x) { n   if (write_pos == buf_size) n     fwrite(write_buf, 1, buf_size, stdout), write_pos = 0; n   write_buf[write_pos++] = x; n } n template <class T> n inline void writeInt(T x, char end) { n   if (x < 0) writeChar( - ), x = -x; n   char s[24]; n   int n = 0; n   while (x || !n) s[n++] =  0  + x % 10, x /= 10; n   while (n--) writeChar(s[n]); n   if (end) writeChar(end); n } n inline void writeWord(const char *s) { n   while (*s) writeChar(*s++); n } n struct Flusher { n   ~Flusher() { n     if (write_pos) fwrite(write_buf, 1, write_pos, stdout), write_pos = 0; n   } n } flusher; n const int MAXN = 100228; n const int MAXK = 10; n const double eps = 1e-7; n struct point { n   int x, y; n }; n point v[MAXK]; n struct query { n   double a; n   int x, y, z; n }; n query w[MAXN * 2]; n bool compv(const point &a, const point &b) { n   return atan2(a.y, a.x) < atan2(b.y, b.x); n } n bool compw(const query &a, const query &b) { n   return (fabs(a.a - b.a) < eps ? a.z < b.z : a.a < b.a); n } n int a[MAXN], c[MAXN]; n int cn, sx, sy; n int px[MAXN], py[MAXN], t[MAXN]; n long long ans[MAXN], s[MAXN]; n inline void add(int k, int x) { n   for (; k <= MAXN; k += k & -k) { n     s[k] += x; n   } n } n int x[MAXN], y[MAXN]; n long long sum(int k) { n   long long res = 0; n   for (; k; k -= k & -k) { n     res += s[k]; n   } n   return res; n } n int main() { n   int k = readInt(), n = readInt(), q = readInt(); n   for (int i = 1; i <= k; i++) { n     v[i].x = readInt(); n     v[i].y = readInt(); n     if (v[i].x < 0) { n       v[i].x = -v[i].x; n       v[i].y = -v[i].y; n     } n     if (!v[i].x && v[i].y < 0) { n       v[i].y = -v[i].y; n     } n     sx += v[i].x, sy += v[i].y; n   } n   sort(v + 1, v + k + 1, compv); n   for (int i = 1; i <= n; i++) { n     c[i] = x[i] = readInt(), y[i] = readInt(), a[i] = readInt(); n   } n   sort(c + 1, c + n + 1); n   cn = unique(c + 1, c + n + 1) - c - 1; n   for (int i = 1; i <= q; i++) { n     px[i] = readInt(), py[i] = readInt(), t[i] = readInt(); n     px[i] -= sx * t[i]; n     py[i] -= sy * t[i]; n     t[i] <<= 1; n   } n   for (int i = 1; i <= k; i++) { n     if (!v[i].x) { n       for (int j = 1; j <= q; j++) { n         py[j] += v[i].y * t[j]; n       } n       continue; n     } n     for (int j = 1; j <= n; j++) { n       w[j] = (query){y[j] - (double)x[j] * v[i].y / v[i].x, x[j], a[j], 0}; n     } n     for (int j = 1; j <= q; j++) { n       w[n + j] = (query){py[j] - (double)px[j] * v[i].y / v[i].x, px[j], n                          px[j] + v[i].x * t[j], -j}, n             px[j] += v[i].x * t[j]; n       py[j] += v[i].y * t[j]; n     } n     sort(w + 1, w + n + q + 1, compw); n     memset(s, 0, sizeof(s)); n     for (int j = 1; j <= n + q; j++) { n       if (w[j].z) { n         ans[-w[j].z] -= n             sum((upper_bound(c + 1, c + cn + 1, w[j].y) - c - 1)) - n             sum((upper_bound(c + 1, c + cn + 1, w[j].x - (i < 2)) - c - 1)); n       } else { n         add((upper_bound(c + 1, c + cn + 1, w[j].x) - c - 1), w[j].y); n       } n     } n   } n   for (int i = 1; i <= k; i++) { n     if (!v[i].x) { n       break; n     } n     for (int j = 1; j <= n; j++) { n       w[j] = (query){y[j] - (double)x[j] * v[i].y / v[i].x, x[j], a[j], 0}; n     } n     for (int j = 1; j <= q; j++) { n       w[n + j] = (query){py[j] - (double)px[j] * v[i].y / v[i].x, n                          px[j] - v[i].x * t[j], px[j], j}, n             px[j] -= v[i].x * t[j], py[j] -= v[i].y * t[j]; n     } n     sort(w + 1, w + n + q + 1, compw); n     memset(s, 0, sizeof(s)); n     for (int j = 1; j <= n + q; j++) { n       if (w[j].z) { n         ans[w[j].z] += n             sum((upper_bound(c + 1, c + cn + 1, w[j].y - (i > 1)) - c - 1)) - n             sum((upper_bound(c + 1, c + cn + 1, w[j].x - 1) - c - 1)); n       } else { n         add((upper_bound(c + 1, c + cn + 1, w[j].x) - c - 1), w[j].y); n       } n     } n   } n   for (int i = 1; i <= q; i++) { n     writeInt(ans[i],   n ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   int number_of_ones = 0; n   string binary_string; n   cin >> n >> binary_string; n   for (int i = 0; i < n; i++) { n     if (binary_string[i] ==  1 ) number_of_ones++; n     if (binary_string[i] ==  0 ) { n       cout << number_of_ones; n       number_of_ones = 0; n     } n   } n   cout << number_of_ones << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long inf = 1e18 + 1e17; n const long long mod = 1e9 + 7; n long long modInverse(long long a, long long m) { n   long long m0 = m; n   long long y = 0, x = 1; n   if (m == 1) return 0; n   while (a > 1) { n     long long q = a / m; n     long long t = m; n     m = a % m, a = t; n     t = y; n     y = x - q * y; n     x = t; n   } n   if (x < 0) x += m0; n   return x; n } n long long powm(long long a, long long b) { n   a = a % mod; n   long long res = 1; n   while (b) { n     if (b & 1) res = (res * a) % mod; n     a = (a * a) % mod; n     b >>= 1; n   } n   return (res % mod); n } n const long long N = 3e5 + 6; n struct st { n   long long a, b, c, d; n }; n bool comp(st x, st y) { return x.a < y.a; } n long long arr[N][2]; n long long n, m; n vector<pair<long long, long long> > graph[N]; n bool vis[N]; n long long ans = 0; n void dijk(long long pos, long long par, long long f) { n   for (pair<long long, long long> i : graph[pos]) n     if (i.first != par) { n       arr[i.first][f] = arr[pos][f] + i.second; n       dijk(i.first, pos, f); n     } n } n void sol() { n   vector<st> v; n   multiset<long long> s; n   for (long long i = 0; i < n; i++) { n     v.push_back({arr[i][0] - arr[i][1], arr[i][0], arr[i][1], i}); n     s.insert(arr[i][1]); n   } n   sort(v.begin(), v.end(), comp); n   for (long long i = 0; i < n - 1; i++) { n     s.erase(s.find(v[i].c)); n     vector<long long> va; n     for (pair<long long, long long> j : graph[v[i].d]) { n       if (s.find(arr[j.first][1]) != s.end()) { n         s.erase(s.find(arr[j.first][1])); n         va.push_back(arr[j.first][1]); n       } n     } n     if (s.size() != 0) ans = max(v[i].b + (*s.rbegin()), ans); n     for (long long j : va) s.insert(j); n   } n } n void solve() { n   cin >> n >> m; n   for (long long i = 0; i < n; i++) { n     arr[i][0] = inf; n     arr[i][1] = inf; n   } n   for (long long i = 0; i < n - 1; i++) { n     long long aa, bb, cc; n     cin >> aa >> bb >> cc; n     aa--; n     bb--; n     graph[aa].push_back(make_pair(bb, cc)); n     graph[bb].push_back(make_pair(aa, cc)); n   } n   memset(vis, 0, sizeof(vis)); n   arr[0][0] = 0; n   arr[n - 1][1] = 0; n   dijk(0, -1, 0); n   dijk(n - 1, -1, 1); n   sol(); n   for (long long i = 0; i < m; i++) { n     long long x; n     cin >> x; n     cout << (min(arr[0][1], ans + x)) <<   n ; n   } n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   long long t = 1; n   for (long long i = 1; i <= t; i++) { n     solve(); n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1000000; n double ans, T, sumb, sumc; n int n, nb, nc; n struct node { n   double x, y; n   bool operator<(const node &a) const { return y < a.y; } n } a[N], b[N], c[N]; n int main() { n   scanf( %d%lf , &n, &T); n   for (int i = 1; i <= n; i++) scanf( %lf , &a[i].x); n   for (int i = 1; i <= n; i++) scanf( %lf , &a[i].y); n   sort(a + 1, a + 1 + n); n   for (int i = 1; i <= n; i++) n     if (a[i].y < T) n       b[++nb] = (node){a[i].x, T - a[i].y}, sumb = sumb + b[nb].x * b[nb].y; n     else if (a[i].y == T) n       ans = ans + a[i].x; n     else if (a[i].y > T) n       c[++nc] = (node){a[i].x, a[i].y - T}, sumc = sumc + c[nc].x * c[nc].y; n   for (int i = 1; i < nb / 2; i++) swap(b[i], b[nb - i + 1]); n   double x = min(sumb, sumc); n   for (int i = 1; i <= nb; i++) n     if (x > 1e-12) { n       double y = min(b[i].x, x / b[i].y); n       ans += y; n       x -= y * b[i].y; n     } n   x = min(sumb, sumc); n   for (int i = 1; i <= nc; i++) n     if (x > 1e-12) { n       double y = min(c[i].x, x / c[i].y); n       ans += y; n       x -= y * c[i].y; n     } n   printf( %.12lf n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int a, b, c, d, e, k[30]; n string s; n int main() { n   ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0); n   cin >> a >> s; n   for (b = 1; b < a; b++) { n     if (s[b] < s[b - 1]) { n       cout <<  YES  << endl; n       cout << b <<     << b + 1; n       return 0; n     } n   } n   cout <<  NO ; n   return 0; n } n