#include <bits/stdc++.h> n using namespace std; n const int N = 405; n int n, kk; n vector<pair<int, int>> v[N][N], e[N]; n int dp[N][N], sz[N], f[N]; n void dfs1(int pos, int fa) { n   sz[pos] = 1; n   for (auto &i : e[pos]) n     if (i.first != fa) { n       dfs1(i.first, pos); n       sz[pos] += sz[i.first]; n       f[i.first] = i.second; n     } n } n void dfs2(int pos, int fa) { n   if (pos == 1) n     dp[pos][sz[pos]] = 0; n   else n     dp[pos][sz[pos]] = 1; n   dp[pos][0] = 0; n   for (auto &i : e[pos]) { n     if (i.first == fa) continue; n     dfs2(i.first, pos); n     for (int j = sz[pos] - 1; j > 0; --j) { n       int tk = -1; n       for (int k = 0; k <= sz[i.first] && k <= j; k++) n         if (dp[pos][j] > dp[i.first][k] + dp[pos][j - k]) { n           dp[pos][j] = dp[i.first][k] + dp[pos][j - k], tk = k; n           v[pos][j] = v[pos][j - tk], v[pos][j].push_back({i.first, tk}); n         } n       if (tk != -1) n         v[pos][j] = v[pos][j - tk], v[pos][j].push_back({i.first, tk}); n     } n   } n } n void print_ans(int pos, int cnt) { n   if (sz[pos] == cnt) { n     cout << f[pos] <<    ; n     return; n   } n   for (auto &i : v[pos][cnt]) print_ans(i.first, i.second); n } n int main() { n   ios::sync_with_stdio(false); n   cin >> n >> kk; n   cin.tie(0); n   for (int i = 1; i < n; i++) { n     int t1, t2; n     cin >> t1 >> t2; n     e[t1].push_back({t2, i}); n     e[t2].push_back({t1, i}); n   } n   memset(dp, 63, sizeof(dp)); n   dfs1(1, 0); n   dfs2(1, 0); n   int ans = dp[1][n - kk], pos = 1; n   for (int i = 2; i <= n; i++) n     if (sz[i] >= kk && ans > dp[i][sz[i] - kk] + 1) n       ans = dp[i][sz[i] - kk] + 1, pos = i; n   cout << ans << endl; n   if (pos != 1) cout << f[pos] << endl; n   print_ans(pos, sz[pos] - kk); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int c3[100010], c2[1010]; n int graph[110][110]; n int main() { n   ios ::sync_with_stdio(0); n   cin.tie(0); n   memset(graph, 0, sizeof graph); n   for (int i = (3), _b = (100); i <= _b; ++i) { n     c3[i] = (i * (i - 1) * (i - 2)) / 6; n   } n   for (int i = (2), _b = (100); i <= _b; ++i) { n     c2[i] = (i * (i - 1)) / 2; n   } n   int k, esco; n   cin >> k; n   for (int i = (100), _b = (3); i >= _b; --i) { n     if (k >= c3[i]) { n       k -= c3[i]; n       esco = i; n       break; n     } n   } n   for (int i = 0; i < (int)(esco); ++i) { n     for (int j = 0; j < (int)(esco); ++j) { n       if (i != j) graph[i][j] = graph[j][i] = 1; n     } n   } n   int fixo = esco; n   while (k > 0) { n     int cont = 0; n     for (int i = (fixo), _b = (2); i >= _b; --i) { n       cont = i; n       if (k >= c2[i]) break; n     } n     for (int i = 0; i < (int)(cont); ++i) graph[i][esco] = graph[esco][i] = 1; n     k -= c2[cont]; n     esco++; n   } n   cout << esco << endl; n   for (int i = 0; i < (int)(esco); ++i) { n     for (int j = 0; j < (int)(esco); ++j) { n       cout << graph[i][j] <<   ; n     } n     cout << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   cout << fixed; n   cout << setprecision(10); n   int row, column; n   char temp; n   cin >> row >> column; n   for (int i = 0; i < int(row - 1); ++i) { n     for (int j = 0; j < int(column); ++j) { n       cin >> temp; n     } n   } n   string s; n   cin >> s; n   int s1 = 0; n   for (int i = 0; i < int(column); ++i) { n     if (s[i] ==  B ) { n       s1++; n       while (s[i] ==  B ) i++; n     } n   } n   cout << s1; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 2e5 + 100; n int test[maxn]; n int main() { n   int n, num(0), x; n   cin >> n; n   char ch; n   int l = 2e5; n   int r = l + 1; n   int mi, ma; n   for (int i = 0; i < n; i++) { n     cin >> ch >> x; n     if (ch ==  L ) { n       test[x] = l; n       num++; n       l--; n       mi = test[x]; n     } else if (ch ==  R ) { n       test[x] = r; n       num++; n       r++; n       ma = test[x]; n     } else if (ch ==  ? ) { n       int temp = test[x]; n       int ans = min(temp - mi, r - 1 - temp); n       cout << ans << endl; n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e6 + 2; n int len, ans = 0, maxdis[N]; n vector<int> adj[N], cur; n void dfs(int x, int p) { n   if (adj[x].size() == 1) { n     maxdis[x] = 0; n     return; n   } n   int i, idxmin1; n   for (i = 0; i < adj[x].size(); i++) { n     if (adj[x][i] != p) { n       dfs(adj[x][i], x); n     } n   } n   cur.clear(); n   for (i = 0; i < adj[x].size(); i++) { n     if (adj[x][i] != p) { n       cur.push_back(maxdis[adj[x][i]] + 1); n     } n   } n   sort(cur.begin(), cur.end()); n   for (i = 0; i < cur.size() - 1; i++) { n     if (cur[i] + cur[i + 1] > len) { n       break; n     } n   } n   ans += cur.size() - 1 - i; n   maxdis[x] = cur[i]; n } n int main() { n   ios::sync_with_stdio(0); n   int n, i, j, k, l = 0; n   cin >> n >> len; n   for (i = 1; i < n; i++) { n     cin >> j >> k; n     adj[j].push_back(k); n     adj[k].push_back(j); n   } n   for (i = 1; i <= n; i++) { n     if (adj[i].size() > 1) { n       l = i; n       break; n     } n   } n   dfs(l, l); n   cout << ans + 1; n } n


#include <bits/stdc++.h> n  n using namespace std; n  n struct node { n     int u , v , w; n     node(){} n     node(int u,int v,int w): n     u(u) , v(v) , w(w){}; n     bool operator < (const node &o) const { n         return w < o.w; n     } n }; n  n struct dsu { n     int par[200010]; n     void init() { n         for (int i = 1 ; i <= 200000 ; ++i) par[i] = i; n     } n     int fin(int u) { n         if (u != par[u]) par[u] = fin(par[u]); n         return par[u]; n     } n     bool join(int u , int v) { n         u = fin(u); n         v = fin(v); n         if (u == v) return false; n         if (par[u] > par[v]) swap(u , v); n         par[u] = v; n         return true; n     } n } a , b; n  n set < int > vertex; n vector < int > g[200010]; n vector < node > edge; n long long rest; n  n void DFS(int u) { n     vertex.erase(u); n     sort(g[u].begin(),g[u].end()); n     int v = 0; n     while (1) { n         auto it = vertex.lower_bound(v); n         if (it == vertex.end()) return; n         v = *it; n         if (!binary_search(g[u].begin(),g[u].end(),v)) { n             a.join(u , v); n             DFS(v); n             rest--; n         } n         v++; n     } n } n  n int main() { n     ios::sync_with_stdio(0); n     cin.tie(0);cout.tie(0); n     long long n , m; n     int x = 0; n     cin >> n >> m; n     rest = n * (n - 1) / 2 - m; n     for (int i = 1 ; i <= m ; ++i) { n         int u , v , w; n         cin >> u >> v >> w; n         x ^= w; n         g[u].push_back(v); n         g[v].push_back(u); n         edge.emplace_back(u , v , w); n     } n     a.init();b.init(); n     for (int i = 1 ; i <= n ; ++i) n         vertex.insert(i); n     for (int i = 1 ; i <= n ; ++i) n         if (vertex.count(i)) DFS(i); n     if (rest > 0) x = 0; n     int res = 0; n     sort(edge.begin(),edge.end()); n     for (auto p : edge) { n         int u = p.u , v = p.v , w = p.w; n         if (a.join(u , v)) { n             res += w; n             b.join(u , v); n         } else if (b.join(u , v)) x = min(x , w); n     } n     cout << res + x; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 2e5 + 5; n int a[N]; n vector<int> v; n int main() { n   int n, s; n   scanf( %d%d , &n, &s); n   int ans = 0; n   for (int i = 1; i <= n; i++) scanf( %d , &a[i]); n   if (a[s] != 0) ans++; n   for (int i = 1; i <= n; i++) n     if (i == s) n       continue; n     else n       v.push_back(a[i]); n   v.push_back(0); n   sort(v.begin(), v.end()); n   int cnt = 0; n   int idx = 0; n   while (v[idx] == 0 && idx < n) cnt++, idx++; n   ans += cnt; n   cnt--; n   ans--; n   int last = n - 1; n   for (int i = idx; i <= last; i++) { n     if (i == 0) continue; n     if (v[i] == v[i - 1]) continue; n     if (v[i - 1] == v[i] - 1) continue; n     int diff = v[i] - v[i - 1] - 1; n     if (diff <= cnt) { n       cnt -= diff; n       continue; n     } n     diff -= cnt; n     cnt = 0; n     while (diff--) { n       if (last >= i) { n         last--; n         ans++; n         continue; n       } n       if (last <= i) break; n     } n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int base = 998244353; n int sum(int a, int b) { n   int c = a + b; n   if (c >= base) return c - base; n   return c; n } n int diff(int a, int b) { n   int c = a - b; n   if (c < 0) return c + base; n   return c; n } n int mul(int a, int b) { return (int)(a * (long long)b % base); } n int pow(int a, int b) { n   if (!b) return 1; n   if (b == 1) return a; n   int d = pow(mul(a, a), b / 2); n   if (b % 2) return mul(d, a); n   return d; n } n int divis(int a, int b) { return mul(a, pow(b, base - 2)); } n struct segment_tree { n  public: n   segment_tree() : ts(0) {} n   explicit segment_tree(vector<int> &p) : ts(1) { n     size_t n = p.size(); n     while (ts < n) { n       ts <<= 1u; n     } n     st.resize(2 * ts); n     for (int i = ts; i < ts + n; ++i) { n       st[i] = p[i - ts]; n     } n     for (int i = (int)ts - 1; i; --i) { n       st[i] = mul(st[i * 2], st[2 * i + 1]); n     } n   } n   int get_mul(int l, int r) { n     l += ts; n     r += (int)ts - 1; n     int res = 1; n     while (l <= r) { n       if (l % 2) { n         res = mul(res, st[l++]); n       } n       if (r % 2 == 0) { n         res = mul(res, st[r--]); n       } n       l /= 2; n       r /= 2; n     } n     return res; n   } n  n  private: n   size_t ts; n   vector<int> st; n }; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   int n, q; n   cin >> n >> q; n   vector<int> p(n); n   for (auto &x : p) { n     cin >> x; n   } n   segment_tree st(p); n   vector<int> deg100(n + 1), pref(n + 1); n   deg100[0] = pref[0] = 1; n   for (int i = 1; i <= n; ++i) { n     deg100[i] = mul(100, deg100[i - 1]); n   } n   for (int i = 1; i <= n; ++i) { n     pref[i] = mul(pref[i - 1], p[i - 1]); n   } n   vector<int> dot_product(n); n   dot_product[0] = 100; n   for (int i = 1; i < n; ++i) { n     dot_product[i] = mul(100, sum(dot_product[i - 1], pref[i])); n   } n   int ans = divis(dot_product.back(), pref[n]); n   set<pair<int, int>> ind = {{0, ans}, {n, 0}}; n   function<int(int, int)> func = [&](int l, int r) { n     if (r - l == 1) { n       return 100; n     } n     int res = dot_product[r - 1]; n     int a = 0; n     if (l) { n       a = mul(deg100[r - l], dot_product[l - 1]); n     } n     res = diff(res, a); n     return divis(res, pref[l]); n   }; n   while (q--) { n     int i; n     cin >> i; n     --i; n     auto v = ind.lower_bound({i, 0}); n     if (v->first != i) { n       int r = v->first; n       --v; n       int l = v->first; n       ans = diff(ans, v->second); n       int fst = divis(func(l, i), st.get_mul(l, i)); n       int snd = divis(func(i, r), st.get_mul(i, r)); n       ind.insert({v->first, fst}); n       ind.erase(v); n       ind.insert({i, snd}); n       ans = sum(ans, fst); n       ans = sum(ans, snd); n     } else { n       auto u = prev(v); n       ans = diff(ans, v->second); n       ans = diff(ans, u->second); n       ind.erase(v); n       int l = u->first, r = next(u)->first; n       int val = divis(func(l, r), st.get_mul(l, r)); n       ans = sum(ans, val); n       ind.insert({u->first, val}); n       ind.erase(u); n     } n     cout << ans <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long m = 1e9 + 7; n long long c[4010][4010]; n long long dp[4010]; n int main() { n   c[0][1] = 0; n   for (int i = 0; i <= 4000; i++) c[i][0] = 1; n   for (int i = 1; i <= 4000; i++) { n     for (int j = 1; j < i + 2; j++) n       c[i][j] = (c[i - 1][j] + c[i - 1][j - 1]) % m; n   } n   dp[0] = 1; n   for (int i = 1; i <= 4000; i++) { n     dp[i] = 0; n     for (int j = 0; j < i; j++) { n       dp[i] = (dp[i] + dp[i - j - 1] * c[i - 1][j]) % m; n     } n   } n   int n; n   cin >> n; n   long long ans = 0; n   for (int i = 0; i < n; i++) ans = (ans + dp[i] * c[n][i]) % m; n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, m, p; n   long long int cost; n   bool na; n   string line; n   vector<int> a, b; n   pair<int, int> t; n   priority_queue<pair<int, int> > q; n   getline(cin, line); n   n = line.length(); n   a.resize(n); n   b.resize(n); n   m = 0; n   for (int i = 0; i < n; i++) { n     if (line[i] ==  ? ) { n       cin >> a[i] >> b[i]; n       ++m; n     } n   } n   p = 0; n   cost = 0LL; n   if (n & 1) { n     na = true; n   } else { n     na = false; n     for (int i = 0; i < n; i++) { n       if (line[i] ==  ( ) { n         ++p; n       } else if (line[i] ==  ) ) { n         --p; n       } else { n         --p; n         q.push(make_pair(b[i] - a[i], i)); n         line[i] =  ) ; n         cost += b[i]; n       } n       if (p < 0) { n         if (q.empty()) { n           na = true; n           break; n         } n         t = q.top(); n         q.pop(); n         cost -= t.first; n         line[t.second] =  ( ; n         p += 2; n       } n     } n     if (p) { n       na = true; n     } n   } n   if (na) { n     cout <<  -1  << endl; n   } else { n     cout << cost << endl; n     cout << line << endl; n   } n   return 0; n } n


#include<cstdio> n #include<iostream> n #include<queue> n #include<algorithm> n #include<cstring> n #include<set> n #include<vector> n #include<cmath> n #define PII pair<int,int> n #define pb push_back n #define ep emplace_back n #define mp make_pair n #define fi first n #define se second n using namespace std; n inline int read(){ n  int x=0,f=1;char c=getchar(); n  while(c< 0 ||c> 9 ){ if(c== - ) f=-1; c=getchar();} n  while(c>= 0 &&c<= 9 ){x=(x<<3)+(x<<1)+(c^48);c=getchar();} n  return f==1?x:~x+1; n } n inline void print(int x){ n  if(x<0) putchar( - ),x=~x+1; n  if(x>=10) print(x/10); n  putchar((x%10)|48); n } n int n; n int a[100010]; n char str[10]; n bool add,fminus,times; n char ans[100010]; n const int mxn=131072; n void solve(int l,int r){ n  if(l>r) return ; n  int sum=1; n  int cnt=0; n  vector<int>pos; n  pos.pb(l-1); n  for(int i=l;i<=r;++i){ n   if(sum<=mxn) sum*=a[i]; n   if(a[i]>1){ n    ++cnt; n    pos.pb(i); n   } n  } n  ++cnt;pos.pb(r+1); n  if(sum>mxn){ n   for(int i=l;i<r;++i) ans[i]= + ; n   for(int i=pos[1];i<pos[cnt-1];++i) ans[i]= * ;  n   return ; n  } n  vector<int>dp(cnt+1),pre(cnt+1);dp[0]=0; n  for(int i=0;i<cnt;++i){ n   int s=dp[i]+pos[i+1]-pos[i]-1,t=1; n   for(int j=i+1;j<=cnt;++j){ n    t=t*a[pos[j]]; n    int r=s+t; n    if(r>dp[j]){ n     pre[j]=i;dp[j]=r; n    } n   } n  } n  int ps=cnt; n  while(ps){ n   int t=ps; n   ps=pre[ps]; n   for(int i=max(l,pos[ps]);i<pos[ps+1];++i) ans[i]= + ; n   for(int i=pos[ps+1];i<pos[t];++i) ans[i]= * ; n  } n }  n int main(){ n  n=read(); n  for(int i=1;i<=n;++i) a[i]=read(); n  scanf( %s ,str+1); n  for(int i=1;i<=strlen(str+1);++i){ n   switch(str[i]){ n    case  + :{add=1; break;} n    case  - :fminus=1;break ; n    case  * :times=1;break ; n   } n  } n  if(!times){ n   if(add) for(int i=1;i<n;++i) printf( %d+ ,a[i]); n   else for(int i=1;i<n;++i) printf( %d- ,a[i]);printf( %d n ,a[n]); n   return 0; n  } n  if(!add){ n   if(fminus){ n    int p=n+1; n    for(int i=1;i<=n;++i) if(a[i]==0){p=i;break ;} n    if(p==n+1){  n     for(int i=1;i<n;++i) printf( %d* ,a[i]);printf( %d n ,a[n]); n     return 0; n    } n    for(int i=1;i<p-1;++i) printf( %d* ,a[i]); n    if(p!=1) printf( %d- ,a[p-1]); n    for(int i=p;i<n;++i) printf( %d* ,a[i]);printf( %d n ,a[n]); n   } n   else{ n //   if(n==67878) printf( 43423423 n ); n    for(int i=1;i<n;++i) printf( %d* ,a[i]);printf( %d n ,a[n]); n   } n   return 0; n  } n  int l=1,r=l; n  while(l<=n){ n   while(a[r]!=0) ++r; n   ans[r-1]= + ,ans[r]= + ; n   solve(l,r-1);l=r+1;r=l; n  } n  for(int i=1;i<n;++i) printf( %d%c ,a[i],ans[i]);printf( %d n ,a[n]); n  return 0; n }


#include <bits/stdc++.h> n using namespace std; n int main() { n   int singles, doubles, n; n   cin >> n >> singles >> doubles; n   int refused = 0, partlyOccDoubles = 0; n   for (int i = 0; i < n; i++) { n     int cur; n     cin >> cur; n     if (cur == 1) { n       if (singles > 0) { n         singles--; n       } else if (doubles > 0) { n         doubles--; n         partlyOccDoubles++; n       } else if (partlyOccDoubles > 0) { n         partlyOccDoubles--; n       } else { n         refused += cur; n       } n     } else { n       if (doubles == 0) n         refused += cur; n       else n         doubles--; n     } n   } n   cout << refused; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void run(); n int main() { n   ios::sync_with_stdio(0); n   cout.precision(10); n   run(); n } n struct vec2 { n   int x, y, sum; n   vec2 *c, *rgb; n   inline bool operator<(vec2 const &a) const { n     return x < a.x or x == a.x and y < a.y; n   } n }; n int yc[1000002], n; n struct thingy { n   vec2 *things; n   int n, pivot; n   thingy *l, *r; n   thingy(vec2 *u, vec2 *v) { n     pivot = u[rand() % (n = (v - u))].y; n     auto a = u, b = v - 1, c = b; n     while (a <= b) n       if (b->y == pivot) n         swap(a++[0], b[0]); n       else n         --b; n     for (sort(u, b = a); b < c;) { n       while (b < c and b->y < pivot) ++b; n       while (b < c and c->y > pivot) --c; n       if (b < c) swap(*b, *c); n     } n     while (b < v and b->y < pivot) ++b; n     l = (a < b ? new thingy(a, b) : nullptr); n     r = (b < v ? new thingy(b, v) : nullptr); n     int sum = 0; n     vec2 *last[2] = {}; n     things = new vec2[n]; n     for (int i = 0, j = 0, k = 0, t = 0; t < n; ++t) { n       vec2 &from = n           (i < (a - u) and (j == b - a or u[i] < l->things[j]) and n                    (k == v - b or u[i] < r->things[k]) n                ? u[i++] n            : j < (b - a) and (k == v - b or l->things[j] < r->things[k]) n                ? l->things[j++] n                : r->things[k++]); n       things[t] = {from.x, from.y, sum += (from.y <= pivot), &from, n                    max((from.y <= pivot ? last[1] : nullptr), n                        (from.y >= pivot ? last[0] : nullptr))}; n       if (things[t].y != pivot) last[pivot < things[t].y] = things + t; n     } n   } n   int query(int x, int y) const { n     vec2 *a = things - 1, *b = things + n; n     while (a + 1 < b) { n       vec2 *m = a + (b - a) / 2; n       (m->x <= x ? a : b) = m; n     } n     if (a < things) return 0; n     thingy const *on = this; n     int res = 0; n     while (a and on) { n       if (on->pivot <= y) res += a->sum; n       if (y == on->pivot) return res; n       while (a and (a->y == on->pivot or (a->y < on->pivot) != (y < on->pivot))) n         a = a->rgb; n       if (a) on = (y < on->pivot ? on->l : on->r), a = a->c; n     } n     return res; n   } n   ~thingy() { n     delete things; n     delete l; n     delete r; n   } n }; n vec2 v[100002]; n void run() { n   int worth[9]; n   cin >> n; n   for (int i = n; i--;) cin >> v[i].x >> v[i].y, yc[i] = v[i].y; n   sort(yc, yc + n); n   yc[n] = 1 << 30; n   for (int i = 9; i--;) cin >> worth[i]; n   sort(worth, worth + 9); n   thingy data(v, v + n); n   sort(v, v + n); n   v[n].x = 1 << 30; n   do { n     int sx[3] = {}, sy[3] = {}, si[3] = {}, sj[3] = {}; n     for (int i = 9; i--;) si[i / 3] += worth[i], sj[i % 3] += worth[i]; n     for (int i = 0; i < 2; i++) si[i + 1] += si[i], sj[i + 1] += sj[i]; n     for (int i = 3; i--;) si[i]--, sj[i]--; n     if ((sx[0] = v[si[0]].x) == v[si[0] + 1].x) continue; n     if ((sx[1] = v[si[1]].x) == v[si[1] + 1].x) continue; n     if ((sy[0] = yc[sj[0]]) == yc[sj[0] + 1]) continue; n     if ((sy[1] = yc[sj[1]]) == yc[sj[1] + 1]) continue; n     sx[2] = sy[2] = 1 << 30; n     int u = data.query(sx[0], sy[0]); n     if (u - 0 != worth[0]) continue; n     int v = data.query(sx[1], sy[0]); n     if (v - u != worth[3]) continue; n     int w = data.query(sx[2], sy[0]); n     if (w - v != worth[6]) continue; n     int a = data.query(sx[0], sy[1]); n     if (a - 0 != worth[0] + worth[1]) continue; n     int b = data.query(sx[1], sy[1]); n     if (b - a != worth[3] + worth[4]) continue; n     int c = data.query(sx[2], sy[1]); n     if (c - b != worth[6] + worth[7]) continue; n     long double ra = sx[0] + 0.5, rb = sx[1] + 0.5, rc = sy[0] + 0.5, n                 rd = sy[1] + 0.5; n     cout << ra <<     << rb <<   n  << rc <<     << rd << endl; n     return; n   } while (next_permutation(worth, worth + 9)); n   cout <<  -1  << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int f[1001000]; n int dp[22][1001000]; n int get(int x, int k) { n   for (int i = 21; i >= 0; i--) n     if (k & (1 << i)) x = dp[i][x]; n   return x; n } n int dist[1001000]; n int foi[1001000]; n vector<int> L[1001000]; n char str[1001000]; n char nova[1001000]; n void bfs(int x, int dc) { n   queue<pair<int, int> > Q; n   Q.push(pair<int, int>(x, 0)); n   while (Q.size()) { n     pair<int, int> u = Q.front(); n     Q.pop(); n     for (vector<int>::iterator it = L[u.first].begin(); it < L[u.first].end(); n          it++) n       if (!foi[*it]) { n         foi[*it] = 1; n         dist[*it] = 1 + u.second; n         Q.push(pair<int, int>(*it, dist[*it])); n       } n   } n } n char debugs[1001000]; n int main() { n   int n, m; n   scanf(  %s , str); n   n = strlen(str); n   scanf( %d , &m); n   while (m--) { n     int d, k; n     scanf( %d%d , &k, &d); n     for (int i = 0; i <= n; i++) { n       L[i].clear(); n       dist[i] = 9999999; n       foi[i] = 0; n     } n     int cont = 0; n     for (int r = 0; r < d; r++) n       for (int i = r; i < k; i += d) { n         L[i + 1].push_back(cont); n         f[cont] = i + 1; n         ; n         cont++; n       } n     f[k] = k; n     bfs(k, 0); n     for (int i = 0; i < k; i++) dp[0][i] = f[i]; n     dp[0][k] = k; n     for (int j = 1; j < 22; j++) { n       for (int i = 0; i < k; i++) { n         dp[j][i] = dp[j - 1][dp[j - 1][i]]; n         ; n       } n       dp[j][k] = k; n     } n     for (int i = 0; i < k; i++) n       ; n     for (int i = 0; i < n; i++) { n       int y0 = 0; n       int p0 = i; n       int u = get(0, i + 1) - 1; n       if (i + k > n) y0 = i - n + k, p0 = n - k; n       u = get(y0, p0 + 1) - 1; n       ; n       if (dist[y0] > p0) { n         nova[i] = str[u]; n         continue; n       } n       int p = dist[y0]; n       nova[i] = str[p0 - p + k]; n       debugs[i] = str[p0 - p + k]; n       if (debugs[i] <  a  || debugs[i] >  z ) debugs[i] =  ? ; n     } n     for (int i = 0; i < n; i++) str[i] = nova[i]; n     printf( %s n , str); n     for (int i = 0; i < n; i++) n       ; n     ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int dp[10][10][10][10], pres_path; n void dfs(int x, int y, int u, int v, int p); n void dfs_next(int x, int y, int u, int v, int p) { n   int &t = dp[x][y][u][v]; n   if (t == 0 or t > pres_path + 1) { n     t = ++pres_path; n     if (p < 10) dfs(x, y, u, v, p + 1); n     --pres_path; n   } n } n void dfs(int x, int y, int u, int v, int p) { n   dfs_next(x, y, u, (v + x) % 10, p), dfs_next(x, y, u, (v + y) % 10, p); n } n int main() { n   string s; n   ios::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr), cin >> s; n   for (int x = 0; x < int(10); ++x) n     for (int y = 0; y < int(10); ++y) n       for (int u = 0; u < int(10); ++u) dfs(x, y, u, u, 1); n   for (int x = 0; x < int(10); ++x) { n     for (int y = 0; y < int(10); ++y) { n       bool valid = true; n       int count = 0; n       for (int l, i = 0, j = 1, k = s.size(); j < k and valid; i = j++) n         if ((l = dp[x][y][s[i] -  0 ][s[j] -  0 ])) n           count += l - 1; n         else n           count = -1, valid = false; n       cout << count <<    ; n     } n     cout << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 1000007; n char s[maxn]; n int n, cnt, a[maxn]; n bool check(int p, bool need_touch) { n   int x = 0, maxv = 0; n   bool touched = 0; n   for (int i = 1, _c = cnt; i <= _c; i++) { n     maxv = max(maxv, x); n     x += a[i]; n     if (x <= p) { n       x = p + 1; n       touched = 1; n     } n   } n   return (x > maxv) && (!need_touch || touched); n } n int main() { n   cnt = 0; n   gets(s + 1); n   n = strlen(s + 1); n   int x = 1; n   while (x <= n) { n     int y = x; n     while (y < n && s[y + 1] == s[x]) ++y; n     a[++cnt] = y - x + 1; n     if (s[x] ==  L ) a[cnt] *= -1; n     x = y + 1; n   } n   if (a[cnt] < 0) n     for (int i = 1, _c = cnt; i <= _c; i++) a[i] *= -1; n   if (check(-n - 1, 0)) { n     puts( 1 ); n     return 0; n   } n   int res = 0; n   int l = -n, r = -1, mid, pos = 0; n   while (l <= r) { n     mid = (l + r) / 2; n     if (check(mid, 1)) { n       pos = mid; n       r = mid - 1; n     } else n       l = mid + 1; n   } n   res -= pos; n   cout << res << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int read() { n   int x = 0; n   int sign = 0; n   char c = getchar(); n   for (; !isdigit(c); c = getchar()) { n     sign |= c ==  - ; n   } n   for (; isdigit(c); c = getchar()) { n     x = x * 10 + c -  0 ; n   } n   return sign ? -x : x; n } n const int ms = 1 << 22; n const int lim = (1 << 22) - 1; n int arr[ms], pai[ms], comps; n bool hav[ms]; n bool vis[ms]; n int find(int x) { n   if (x == pai[x]) return x; n   return pai[x] = find(pai[x]); n } n void unite(int x, int y) { n   x = find(x); n   y = find(y); n   if (x != y) { n     --comps; n     pai[x] = y; n   } n } n void dfs(int u, int mask) { n   if (hav[mask]) unite(u, mask); n   if (vis[mask]) return; n   vis[mask] = true; n   if (mask == 0) return; n   int last_bit = 31 - __builtin_clz(mask); n   for (int i = last_bit; i >= 0; --i) { n     int nmask = mask & (~(1 << i)); n     dfs(u, nmask); n   } n } n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   iota(pai, pai + ms, 0); n   int n = read(), m = read(); n   comps = m; n   for (int i = 0; i < m; ++i) { n     arr[i] = read(); n     hav[arr[i]] = true; n   } n   for (int i = 0; i < m; ++i) { n     dfs(arr[i], ~arr[i] & lim); n   } n   cout << comps <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, k; n   cin >> n >> k; n   if (k > (n % 2 == 0 ? n * n / 2 : (n * n + 1) / 2)) { n     cout <<  NO  << endl; n     return 0; n   } n   cout <<  YES  << endl; n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < n; j++) { n       if (k && (i + j) % 2 == 0) { n         cout <<  L ; n         k--; n       } else { n         cout <<  S ; n       } n     } n     cout << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1000000 + 10; n int tot; n vector<int> primes; n int notP[N]; n vector<long long> divisors[N]; n map<pair<long long, int>, int> dp; n int isP(long long x) { n   for (int i = 0; (long long)primes[i] * primes[i] <= x; ++i) { n     if (x % primes[i] == 0) return false; n   } n   return true; n } n int dfs(long long x, int cur) { n   if (dp.count(make_pair(x, cur))) { n     return dp[make_pair(x, cur)]; n   } n   int &ret = dp[make_pair(x, cur)]; n   if (cur == tot) { n     if (x == 1) { n       ret++; n     } else if (x > N) { n       ret += isP(x - 1); n     } n     return ret; n   } n   ret += dfs(x, cur + 1); n   for (int i = 0; i < divisors[cur].size(); ++i) { n     if (x % divisors[cur][i] == 0) { n       ret += dfs(x / divisors[cur][i], cur + 1); n     } n   } n   return ret; n } n void solve() { n   long long n; n   cin >> n; n   tot = 0; n   for (int i = 0; i < primes.size(); ++i) { n     int prime = primes[i]; n     int flag = false; n     for (long long tmp = prime; tmp < n; tmp *= prime) { n       if (n % (tmp + 1) == 0) { n         if (!flag) { n           flag = true; n           ++tot; n         } n         divisors[tot - 1].push_back(tmp + 1); n       } n     } n   } n   cout << dfs(n, 0) << endl; n } n int main() { n   for (int i = 2; i < N; ++i) { n     if (notP[i]) continue; n     primes.push_back(i); n     for (long long j = (long long)i * i; j < N; j += i) { n       notP[j] = true; n     } n   } n   solve(); n   return 0; n } n


#include <bits/stdc++.h> n const int maxn = 400010, maxq = 400010; n int n, cnt[maxn], tmp[maxn], lg[maxn]; n int min(int a, int b) { return a < b ? a : b; } n int max(int a, int b) { return a > b ? a : b; } n struct SA { n   char S[maxn]; n   int nxt[maxn], mn[maxn], sa[maxn], r[2][maxn], *rk, ht[20][maxn]; n   void make() { n     for (int i = 0; i < 128; i++) cnt[i] = 0, tmp[i] = 1 << 30; n     mn[n] = 1 << 30; n     for (int i = n; i--;) n       nxt[i] = tmp[S[i]], tmp[S[i]] = i, mn[i] = min(nxt[i], mn[i + 1]); n     for (int i = 0; i < n; i++) cnt[S[i]]++; n     for (int i = 1; i < 128; i++) cnt[i] += cnt[i - 1]; n     for (int i = n; i--;) sa[--cnt[S[i]]] = i; n     int tot = 0; n     rk = *r; n     for (int i = 0; i < n; i++) n       rk[sa[i]] = i && S[sa[i - 1]] == S[sa[i]] ? tot - 1 : tot++; n     for (int k = 1, *t = r[1]; tot < n; k *= 2) { n       for (int i = 0; i < k; i++) tmp[i] = i + n - k; n       for (int i = 0, j = k; i < n; i++) n         if (sa[i] >= k) tmp[j++] = sa[i] - k; n       for (int i = 0; i < tot; i++) cnt[i] = 0; n       for (int i = 0; i < n; i++) cnt[rk[i]]++; n       for (int i = 1; i < tot; i++) cnt[i] += cnt[i - 1]; n       for (int i = n; i--;) sa[--cnt[rk[tmp[i]]]] = tmp[i]; n       rk[n] = -1; n       for (int i = tot = 0; i < n; i++) n         t[sa[i]] = i && rk[sa[i - 1]] == rk[sa[i]] && n                            rk[sa[i - 1] + k] == rk[sa[i] + k] n                        ? tot - 1 n                        : tot++; n       std::swap(rk, t); n     } n     for (int i = 0, h = 0; i < n; ht[0][rk[i++]] = h) n       for (h ? --h : 0; rk[i] + 1 < n && S[i + h] == S[sa[rk[i] + 1] + h]; h++) n         ; n     for (int k = 1; k <= lg[n]; k++) n       for (int i = 0; i + (1 << k) <= n; i++) n         ht[k][i] = min(ht[k - 1][i], ht[k - 1][i + (1 << k - 1)]); n   } n   int lcp(int i, int j) { n     if (i == n || j == n) return 0; n     int k; n     if ((i = rk[i]) > (j = rk[j])) n       return k = lg[i - j], min(ht[k][j], ht[k][i - (1 << k)]); n     return k = lg[j - i], min(ht[k][i], ht[k][j - (1 << k)]); n   } n } S0, S1; n int R[maxq], ans[maxq]; n std::vector<int> pf[maxn], qs[maxn]; n int ul[maxn], ur[maxn], dl[maxn], dr[maxn], ml[maxn], pl[maxn], pr[maxn], n     list[maxn]; n int find(int* u, int i) { return u[i] == i ? i : u[i] = find(u, u[i]); } n void upd(int* u, int* d, int l, int r, int x) { n   while ((l = find(u, l)) < r) d ? d[l] = x : 0, u[l] = find(u, l + 1); n } n int calc(int l, int r, int br) { n   int len = r - l; n   for (int i = pf[len].size(); i--;) { n     int p = len / pf[len][i]; n     if (S0.lcp(l, l + p) >= len - p) return 1; n   } n   if (dl[l] && l + dl[l] * 2 <= r) return 2; n   if (dr[r] && r - dr[r] * 2 >= l) return 2; n   if (r > br && ul[r] > r) return 2; n   for (int i = 1; l + i < r && l + i <= br; i++) n     if (S0.lcp(l, r - i) >= i) return 2; n   if (S0.nxt[l] < r || S1.nxt[n - r] < n - l || ml[l] <= r) return 3; n   if (S0.mn[l] < r) return 4; n   return -1; n } n int main() { n   scanf( %d%s , &n, S0.S); n   for (int i = n, j = 0; i--; j++) S1.S[j] = S0.S[i]; n   for (int i = 2; i <= n; i++) { n     lg[i] = lg[i / 2] + 1; n     if (!pf[i].size()) n       for (int j = i; j <= n; j += i) pf[j].push_back(i); n   } n   S0.make(); n   S1.make(); n   for (int i = 0; i <= n + 1; i++) ul[i] = ur[i] = i; n   for (int l = 1; l <= n / 2; l++) { n     int la = -1; n     for (int i = 0; i + l <= n; i += l) n       if (i > la) { n         int pl = i - S1.lcp(n - i - l, n - i), pr = i + S0.lcp(i, i + l); n         if (pr - pl >= l) { n           upd(ul, dl, pl, pr - l + 1, l); n           upd(ur, dr, pl + l * 2, pr + l + 1, l); n         } n         la = pr; n       } n   } n   ml[n] = 1 << 30; n   for (int i = n; i--;) { n     ml[i] = ml[i + 1]; n     if (dl[i]) ml[i] = min(ml[i], i + dl[i] * 2); n   } n   int q; n   scanf( %d , &q); n   for (int i = 0, l; i < q; i++) { n     scanf( %d%d , &l, R + i); n     qs[l - 1].push_back(i); n   } n   int b = 0; n   while (b * b < n * 4) b++; n   for (int bl = 0, br = b; bl < n; bl = br, br += b) { n     if (br > n) br = n; n     for (int i = 0; i <= br; i++) cnt[i] = 0; n     for (int i = br + 1; i <= n; i++) { n       ul[i] = i; n       pl[i] = br - S1.lcp(n - i, n - br); n       cnt[pl[i]]++; n     } n     for (int i = 0; i < br; i++) cnt[i + 1] += cnt[i]; n     int tot = cnt[br]; n     for (int i = n; i > br; i--) tmp[--cnt[pl[i]]] = i; n     for (int l = bl, i = 0; l < n && l < br; l++) { n       for (; i < tot && pl[tmp[i]] <= l; i++) n         upd(ul, 0, tmp[i], tmp[i] + S0.lcp(br, tmp[i]) + 1, 0); n       for (int id = qs[l].size(); id--;) { n         int x = qs[l][id]; n         ans[x] = calc(l, R[x], br); n       } n     } n   } n   for (int i = 0; i < q; i++) printf( %d n , ans[i]); n } n


#include <bits/stdc++.h> n using namespace std; n string ls[] = {  ,      one ,  two ,    three ,  four , n                 five ,  six ,  seven ,  eight ,  nine }; n string fr[] = {  ,        ,       twenty ,   thirty ,  forty , n                 fifty ,  sixty ,  seventy ,  eighty ,  ninety }; n int main() { n   ios_base::sync_with_stdio(false); n   int n; n   cin >> n; n   int first = n / 10, second = n % 10; n   switch (n) { n     case 0: n       cout <<  zero  << endl; n       break; n     case 10: n       cout <<  ten  << endl; n       break; n     case 11: n       cout <<  eleven  << endl; n       break; n     case 12: n       cout <<  twelve  << endl; n       break; n     case 13: n       cout <<  thirteen  << endl; n       break; n     case 14: n       cout <<  fourteen  << endl; n       break; n     case 15: n       cout <<  fifteen  << endl; n       break; n     case 16: n       cout <<  sixteen  << endl; n       break; n     case 17: n       cout <<  seventeen  << endl; n       break; n     case 18: n       cout <<  eighteen  << endl; n       break; n     case 19: n       cout <<  nineteen  << endl; n       break; n     default: { n       if (!first) n         cout << ls[second] << endl; n       else if (!second) n         cout << fr[first] << endl; n       else n         cout << fr[first] <<  -  << ls[second] << endl; n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int t; n   cin >> t; n   while (t--) { n     double n, m, k; n     cin >> n >> m >> k; n     if (m == 0) n       cout << 0 << endl; n     else if (n / k >= m) { n       cout << (int)m << endl; n     } else { n       double zz = (m - (n / k)); n       int tt; n       tt = ceil(zz / (k - 1)); n       cout <<    << (int)(n / k) - tt << endl; n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int r, h, sum, n; n   float k, l; n   cin >> r >> h; n   n = h / r; n   h -= n * r; n   l = r / 2.0; n   k = sqrt(3) / 2 * r; n   if (h < l) sum = n * 2 + 1; n   if (h >= l && h < k) sum = n * 2 + 2; n   if (h >= k) sum = n * 2 + 3; n   cout << sum << endl; n   return 0; n } n


#include <bits/stdc++.h> n namespace IO { n const unsigned int Buffsize = 1 << 15, Output = 1 << 23; n static char Ch_[Buffsize], *S_ = Ch_, *T_ = Ch_; n static char Out[Output], *nowps = Out; n inline void flush() { n   fwrite(Out, 1, nowps - Out, stdout); n   nowps = Out; n } n template <typename T> n inline T Read() { n   T _ = 0; n   static char __; n   T ___ = 1; n   for (__ = ((S_ == T_) && (T_ = (S_ = Ch_) + fread(Ch_, 1, Buffsize, stdin), n                            S_ == T_) n                  ? 0 n                  : *S_++); n        !isdigit(__); n        __ = ((S_ == T_) && (T_ = (S_ = Ch_) + fread(Ch_, 1, Buffsize, stdin), n                            S_ == T_) n                  ? 0 n                  : *S_++)) n     if (__ ==  - ) ___ = -1; n   for (; isdigit(__); n        __ = ((S_ == T_) && (T_ = (S_ = Ch_) + fread(Ch_, 1, Buffsize, stdin), n                            S_ == T_) n                  ? 0 n                  : *S_++)) n     _ = (_ << 3) + (_ << 1) + (__ ^ 48); n   return _ *= ___; n } n template <typename T> n inline void write(T _, char __ =   n ) { n   if (!_) *nowps++ =  0 ; n   if (_ < 0) *nowps++ =  - , _ = -_; n   static unsigned int sta[111], tp; n   for (tp = 0; _; _ /= 10) sta[++tp] = _ % 10; n   for (; tp; *nowps++ = sta[tp--] ^ 48) n     ; n   (*nowps++) = __; n } n template <class T> n inline bool chkmax(T &_, T __) { n   return _ < __ ? _ = __, 1 : 0; n } n template <class T> n inline bool chkmin(T &_, T __) { n   return _ > __ ? _ = __, 1 : 0; n } n }  // namespace IO n using namespace std; n using namespace IO; n const int N = 2e5 + 10, Mod = 1e9 + 7; n int n; n int fa[N], sz[N], flag[N], ans; n int find(int first) { n   return first == fa[first] ? first : fa[first] = find(fa[first]); n } n void merge(int first, int second) { n   int fx = find(first), fy = find(second); n   if (fx != fy) { n     fa[fx] = fy; n     sz[fy] += sz[fx]; n     flag[fy] |= flag[fx]; n   } else n     flag[fy] = 1; n } n int main() { n   n = Read<int>(); n   for (int i = 1; i <= 2 * n; ++i) fa[i] = i, sz[i] = 1, flag[i] = 0; n   for (register int i = (1), i_end_ = (n); i <= i_end_; ++i) { n     int u = Read<int>(), v = Read<int>(); n     if (u != v) n       merge(u, v); n     else n       flag[u] = 2; n   } n   ans = 1; n   for (register int i = (1), i_end_ = (2 * n); i <= i_end_; ++i) n     if (find(i) == i) { n       if (flag[i] == 0) n         ans = (1ll * ans * sz[i]) % Mod; n       else if (flag[i] == 1) n         ans = (ans * 2ll) % Mod; n     } n   printf( %d n , ans); n   return flush(), 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   scanf( %d , &n); n   vector<int> v(n); n   vector<pair<int, int>> vv(n); n   for (int i = 0; i < n; i++) { n     scanf( %d , &v[i]); n   } n   sort(v.begin(), v.end()); n   reverse(v.begin(), v.end()); n   for (int i = 0; i < n; i++) { n     scanf( %d , &vv[i].first); n     vv[i].second = i; n   } n   sort(vv.begin(), vv.end()); n   vector<int> ans(n); n   for (int i = 0; i < n; i++) { n     ans[vv[i].second] = v[i]; n   } n   for (int i = 0; i < ans.size(); i++) cout << ans[i] <<    ; n   cout <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n ostream& operator<<(ostream& os, vector<int>& x) { n   for (int i = 0; i < x.size(); i++) os << x[i] <<    ; n   return os; n } n ostream& operator<<(ostream& os, pair<int, int> x) { n   cout << x.first <<     << x.second <<    ; n   return os; n } n ostream& operator<<(ostream& os, vector<pair<int, int> >& x) { n   for (int i = 0; i < x.size(); i++) os << x[i] <<   n ; n   return os; n } n vector<pair<int, int> > changed; n bool mark[500005]; n struct dsu { n   int par[500005]; n   dsu() { n     for (int i = 1; i <= 500005 - 5; i++) par[i] = i; n   } n   int find(int x, bool cache = false) { n     if (cache) { n       if (!mark[x]) { n         mark[x] = true; n         changed.push_back(make_pair(x, par[x])); n       } n     } n     if (par[x] == x) return x; n     return par[x] = find(par[x]); n   } n   bool uni(int x, int y, bool cache = false) { n     x = find(x, cache); n     y = find(y, cache); n     if (x != y) { n       if (cache) { n         if (!mark[x]) { n           mark[x] = true; n           changed.push_back(make_pair(x, par[x])); n         } n       } n       par[x] = y; n       return true; n     } n     return false; n   } n } d; n struct data { n   int u, v, c; n   data(int _u, int _v, int _c) : u(_u), v(_v), c(_c){}; n }; n bool byPrice(data x, data y) { return x.c < y.c; } n int n, m, q; n vector<data> edge; n vector<data> oredge; n vector<vector<vector<int> > > query(500005); n vector<vector<pair<int, int> > > withW(500005); n vector<vector<int> > tmp(500005); n bool ans[500005]; n signed main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   cin >> n >> m; n   for (int i = 1; i <= m; i++) { n     int u, v, c; n     cin >> u >> v >> c; n     edge.push_back(data(u, v, c)); n     withW[c].push_back(make_pair(u, v)); n   } n   cin >> q; n   for (int i = 1; i <= q; i++) { n     int num; n     cin >> num; n     vector<int> zz; n     while (num--) { n       int t; n       cin >> t; n       t--; n       zz.push_back(t); n       tmp[edge[t].c].push_back(t); n     } n     for (int j = 0; j < zz.size(); j++) { n       int c = edge[zz[j]].c; n       if (tmp[c].empty()) continue; n       tmp[c].push_back(i); n       query[c].push_back(tmp[c]); n       tmp[c].clear(); n     } n   } n   memset(ans, 1, sizeof(ans)); n   for (int w = 1; w <= 500000; w++) { n     for (int i = 0; i < query[w].size(); i++) { n       for (int j = 0; j < query[w][i].size() - 1; j++) { n         int num = query[w][i][j]; n         assert(edge[num].c == w); n         int u = edge[num].u; n         int v = edge[num].v; n         d.find(u); n         d.find(v); n       } n     } n     for (int i = 0; i < query[w].size(); i++) { n       int which = query[w][i].back(); n       bool ok = true; n       for (int j = 0; j < query[w][i].size() - 1; j++) { n         int num = query[w][i][j]; n         int u = edge[num].u; n         int v = edge[num].v; n         if (!d.uni(u, v, 1)) { n           ok = false; n           break; n         } n       } n       if (!ok) { n         ans[which] = false; n       } n       for (int j = 0; j < changed.size(); j++) { n         d.par[changed[j].first] = changed[j].second; n         mark[changed[j].first] = false; n       } n       changed.clear(); n     } n     for (int i = 0; i < withW[w].size(); i++) { n       int u = withW[w][i].first; n       int v = withW[w][i].second; n       d.uni(u, v); n     } n   } n   for (int i = 1; i <= q; i++) { n     if (ans[i] == 1) n       cout <<  YES  <<   n ; n     else n       cout <<  NO  <<   n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   string s; n   cin >> n >> s; n   int a1[n], a2[n]; n   for (int i = 0; i < n; i++) a1[i] = s[i] -  0 ; n   for (int i = n; i < 2 * n; i++) a2[i - n] = s[i] -  0 ; n   for (int i = 0; i < n; i++) { n     int s1(i), s2(i); n     for (int j = i + 1; j < n; j++) { n       if (a1[j] < a1[s1]) s1 = j; n       if (a2[j] < a2[s2]) s2 = j; n     } n     swap(a1[i], a1[s1]); n     swap(a2[i], a2[s2]); n   } n   int l(0), m(0); n   for (int i = 0; i < n; i++) n     if (a1[i] < a2[i]) n       l += 1; n     else if (a1[i] > a2[i]) n       m += 1; n     else if (a1[i] == a2[i]) { n       cout <<  NO  << endl; n       return 0; n     } n   if ((l == n) || (m == n)) n     cout <<  YES  << endl; n   else n     cout <<  NO  << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int INF = int(1e9); n long long n, m; n pair<long long, long long> p = make_pair(0, 0); n long long b[222222]; n long long c[222222]; n long long ans; n map<long long, long long> mp; n int main() { n   ios_base::sync_with_stdio(false); n   cin >> n; n   for (int i = 1; i <= n; i++) { n     int x; n     cin >> x; n     mp[x]++; n   } n   cin >> m; n   for (int i = 1; i <= m; i++) cin >> b[i]; n   for (int i = 1; i <= m; i++) cin >> c[i]; n   for (int i = 1; i <= m; i++) n     if (p <= make_pair(mp[b[i]], mp[c[i]])) n       ans = i, p = make_pair(mp[b[i]], mp[c[i]]); n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   int n; n   cin >> n; n   vector<long long> heros(n); n   for (auto& x : heros) cin >> x; n   sort(heros.begin(), heros.end()); n   long long sum = accumulate(heros.begin(), heros.end(), 0ll); n   int m; n   cin >> m; n   while (m--) { n     long long atk, dfns; n     cin >> atk >> dfns; n     int id = lower_bound(heros.begin(), heros.end(), atk) - heros.begin(); n     long long ans = 2e18 + 5; n     if (id > 0) { n       ans = min(ans, n                 atk - heros[id - 1] + max(0ll, dfns - (sum - heros[id - 1]))); n     } n     if (id < n) { n       ans = min(ans, max(0ll, dfns - (sum - heros[id]))); n     } n     cout << ans << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 3e5 + 3; n int last[22][22]; n int a[maxn], dp[20][maxn]; n int main() { n   ios_base::sync_with_stdio(false); n   int n, q; n   cin >> n >> q; n   for (int i = 1; i <= n; i++) cin >> a[i]; n   int l = 19; n   memset(last, 63, sizeof last); n   memset(dp, 63, sizeof dp); n   for (int i = n; i >= 1; i--) { n     for (int fb = 0; fb < l; fb++) n       for (int sb = 0; sb < l; sb++) n         if (a[i] & (1 << fb) and a[i] & (1 << sb)) last[fb][sb] = i; n     for (int fb = 0; fb < l; fb++) n       for (int sb = 0; sb < l; sb++) n         if (a[i] & (1 << sb)) dp[fb][i] = min(dp[fb][i], last[fb][sb]); n     for (int fb = 0; fb < l; fb++) n       for (int sb = 0; sb < l; sb++) n         if (dp[sb][i] <= n) dp[fb][i] = min(dp[fb][i], dp[fb][dp[sb][i]]); n   } n   while (q--) { n     int x, y; n     cin >> x >> y; n     bool ret = 0; n     for (int i = 0; i < l; i++) n       if (a[y] & (1 << i) and dp[i][x] <= y) ret |= 1; n     if (ret == 1) n       cout <<  Shi n ; n     else n       cout <<  Fou n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int n, k; n int a[100005]; n int b[100005]; n bool ok(long long x) { n   long long s = k; n   for (int i = 0; i < n; i++) { n     if (x * a[i] >= b[i]) { n       s -= (x * a[i] - b[i]); n     } n     if (s < 0) { n       return false; n     } n   } n   return true; n } n int main() { n   cin >> n >> k; n   for (int i = 0; i < n; i++) { n     cin >> a[i]; n   } n   for (int j = 0; j < n; j++) { n     cin >> b[j]; n   } n   if (n == 1) { n     cout << (b[0] + k) / a[0] << endl; n   } else { n     int sum = 0; n     long long l = 0, r = 0xffffffff; n     long long mid; n     while (l <= r) { n       mid = (l + r) >> 1; n       if (ok(mid)) { n         l = mid + 1; n         sum = mid; n       } else { n         r = mid - 1; n       } n     } n     cout << sum << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int INF = 1000000000; n int main() { n   ios_base::sync_with_stdio(0); n   int n; n   cin >> n; n   vector<string> a(n); n   map<string, int> M; n   for (int i = 0; i < n; ++i) { n     cin >> a[i]; n     M[a[i]] = i; n   } n   vector<vector<int> > Inv(1 << n, vector<int>(n, 0)); n   for (int mask = 1; mask < (1 << n); ++mask) n     for (int j = 0; j < n; ++j) n       if ((mask & (1 << j)) == 0) n         for (int l = 0; l < n; ++l) n           if ((mask & (1 << l)) > 0 && M[a[j]] < M[a[l]]) ++Inv[mask][j]; n   int m; n   cin >> m; n   int p = -1, nr; n   for (int i = 0; i < m; ++i) { n     int k; n     cin >> k; n     vector<string> b(k); n     for (int j = 0; j < k; ++j) cin >> b[j]; n     vector<vector<int> > nast(k, vector<int>(n, INF)); n     if (M.find(b[k - 1]) != M.end()) nast[k - 1][M[b[k - 1]]] = k - 1; n     for (int j = k - 2; j >= 0; --j) { n       for (int l = 0; l < n; ++l) nast[j][l] = nast[j + 1][l]; n       if (M.find(b[j]) != M.end()) nast[j][M[b[j]]] = j; n     } n     vector<vector<int> > dp((1 << n), vector<int>(n * (n - 1) / 2 + 1, INF)); n     for (int j = 0; j < n; ++j) n       if (nast[0][j] != INF) dp[1 << j][0] = nast[0][j]; n     for (int mask = 1; mask < (1 << n); ++mask) { n       for (int inv = 0; inv <= n * (n - 1) / 2; ++inv) { n         if (dp[mask][inv] == INF) continue; n         for (int j = 0; j < n; ++j) { n           if ((mask & (1 << j)) == 0) { n             int noweInv = inv + Inv[mask][j]; n             if (dp[mask][inv] + 1 < k && nast[dp[mask][inv] + 1][j] != INF) n               dp[mask | (1 << j)][noweInv] = n                   min(dp[mask | (1 << j)][noweInv], nast[dp[mask][inv] + 1][j]); n           } n         } n       } n     } n     for (int j = 0; j <= n * (n - 1) / 2; ++j) { n       if (dp[(1 << n) - 1][j] != INF && n * (n - 1) / 2 - j + 1 > p) { n         p = n * (n - 1) / 2 - j + 1; n         nr = i; n       } n     } n   } n   if (p == -1) n     cout <<  Brand new problem! ; n   else { n     cout << nr + 1 << endl; n     cout <<  [: ; n     for (int i = 0; i < p; ++i) cout <<  | ; n     cout <<  :] ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 14; n const int Q = 101; n int n, m, q; n long long dp[N][1 << N]; n bool edge[N][N]; n int a[Q], b[Q], c[Q]; n int lowbit(int x) { return x & (-x); } n int in(int i, int S) { return ((S >> i) & 1); } n bool check(int u, int k) { n   for (int i = 0; i < n; i++) n     if (i != u) { n       for (int j = 0; j < n; j++) n         if (j != u) n           if (edge[i][j] && (in(i, k) ^ in(j, k))) return false; n     } n   return true; n } n long long DP(int u, int S) { n   if (dp[u][S] != -1) return dp[u][S]; n   dp[u][S] = 0; n   int status = S - (1 << u); n   int t; n   for (t = 0; t < n; t++) n     if (in(t, status)) break; n   for (int k = status; k; k = (k - 1) & status) n     if (in(t, k)) { n       if (!check(u, k)) continue; n       int v, cnt = 0; n       for (int i = 0; i < n; i++) { n         if (edge[u][i] && in(i, k)) { n           cnt++; n           v = i; n         } n       } n       if (cnt >= 2) continue; n       bool flag = true; n       for (int i = 1; i <= q; i++) { n         if (c[i] == u && in(a[i], k) && in(b[i], k)) { n           flag = false; n           break; n         } n         if (in(c[i], k) && (!in(a[i], k) || !in(b[i], k))) { n           flag = false; n           break; n         } n       } n       if (!flag) continue; n       if (cnt == 1) { n         dp[u][S] += DP(v, k) * DP(u, S - k); n       } else { n         for (v = 0; v < n; v++) n           if (in(v, k)) dp[u][S] += DP(v, k) * DP(u, S - k); n       } n     } n   return dp[u][S]; n } n int main() { n   scanf( %d%d%d , &n, &m, &q); n   for (int i = 1; i <= m; i++) { n     int x, y; n     scanf( %d%d , &x, &y); n     x--, y--; n     edge[x][y] = edge[y][x] = true; n   } n   for (int i = 1; i <= q; i++) { n     scanf( %d%d%d , &a[i], &b[i], &c[i]); n     a[i]--; n     b[i]--; n     c[i]--; n   } n   memset(dp, -1, sizeof(dp)); n   for (int i = 0; i < n; i++) dp[i][1 << i] = 1; n   printf( %lld , DP(0, (1 << n) - 1)); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int t = 1; n void solve() { n   long long n, i, k, s = 0, b; n   cin >> n >> k; n   long long a[n]; n   for (i = 0; i < n; i++) { n     cin >> a[i]; n   } n   sort(a, a + n); n   b = a[0]; n   for (i = 1; i < n; i++) { n     long long c = (k - a[i]) / b; n     if (c > 0) s += c; n   } n   cout << s <<   n ; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cin >> t; n   while (t--) { n     solve(); n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 3e5 + 20; n const int inf = 0x3f3f3f3f; n const int modd = 1e9 + 7; n inline int read() { n   int x = 0, f = 1; n   char c = getchar(); n   while (c !=  -  && (c <  0  || c >  9 )) c = getchar(); n   if (c ==  - ) f = -1, c = getchar(); n   while (c >=  0  && c <=  9 ) x = x * 10 + c -  0 , c = getchar(); n   return f * x; n } n template <class T> n inline void sc(T &x) { n   char c; n   x = 0; n   while ((c = getchar()) <  0 ) n     ; n   while (c >=  0  && c <=  9 ) x = x * 10 + (c - 48), c = getchar(); n } n inline long long gcd(long long a, long long b) { n   return b == 0 ? a : gcd(b, a % b); n } n inline long long exgcd(long long a, long long b, long long &x, long long &y) { n   long long d; n   (b == 0 ? (x = 1, y = 0, d = a) n           : (d = exgcd(b, a % b, y, x), y -= a / b * x)); n   return d; n } n inline long long qpow(long long a, long long n) { n   long long sum = 1; n   while (n) { n     if (n & 1) sum = sum * a % modd; n     a = a * a % modd; n     n >>= 1; n   } n   return sum; n } n inline long long qmul(long long a, long long n) { n   long long sum = 0; n   while (n) { n     if (n & 1) sum = (sum + a) % modd; n     a = (a + a) % modd; n     n >>= 1; n   } n   return sum; n } n inline long long inv(long long a) { return qpow(a, modd - 2); } n int a[maxn]; n int main() { n   int n, m; n   scanf( %d%d , &n, &m); n   if (m && n < 3) return printf( -1 n ), 0; n   int i = 1; n   for (; i <= n;) { n     if (m >= (i - 1) / 2) { n       a[i] = i, m -= (i - 1) / 2; n       i++; n       continue; n     } n     if (m > 0) a[i] = 2 * (i - m) - 1, m = 0, i++; n     if (!m) break; n   } n   for (int j = n; j >= i; j--) a[j] = (int)9e8 - (n - j) * (a[i - 1] + 1); n   if (!m) { n     for (int i = 1; i <= n; i++) printf( %d%c , a[i],    n [i == n]); n     return 0; n   } n   printf( -1 n ); n } n


#include <bits/stdc++.h> n using namespace std; n int n, leaf; n vector<int> vt[100005]; n int ara[100005], chk[100005], level[100005], child[100005]; n map<int, int> mp; n void bfs() { n   int i, j, k; n   queue<int> qu; n   qu.push(0); n   chk[0] = 1; n   level[0] = 0; n   while (!qu.empty()) { n     k = qu.front(); n     qu.pop(); n     for (i = 0; i < vt[k].size(); ++i) { n       j = vt[k][i]; n       if (!chk[j]) { n         qu.push(j); n         level[j] = level[k] + 1; n         chk[j] = 1; n       } n     } n   } n } n int main() { n   long long i, j, k; n   long long xr, ans; n   scanf( %d , &n); n   for (i = 0; i < n; ++i) scanf( %d , &ara[i]); n   for (i = 1; i < n; ++i) { n     scanf( %d , &k); n     k--; n     child[k] = 1; n     vt[i].push_back(k); n     vt[k].push_back(i); n   } n   bfs(); n   for (i = 0; i < n; ++i) n     if (!child[i]) leaf = i; n   for (i = 0, xr = 0, k = n; i < n; ++i) { n     if (level[leaf] % 2 == level[i] % 2) xr ^= ara[i], k--, mp[ara[i]]++; n     ; n   } n   ans = 0; n   if (xr == 0) { n     ans += (k * (k - 1)) / 2; n     k = n - k; n     ans += (k * (k - 1)) / 2; n     for (i = 0; i < n; ++i) { n       if (level[leaf] % 2 != level[i] % 2) ans += mp[ara[i]]; n     } n   } else { n     for (i = 0; i < n; ++i) { n       if (level[leaf] % 2 == level[i] % 2) continue; n       k = xr ^ ara[i]; n       ans += mp[k]; n     } n   } n   cout << ans << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int testnum; n const int maxn = 100005; n int par[maxn]; n int size[maxn]; n int find(int u) { n   if (par[u] == u) return u; n   return par[u] = find(par[u]); n } n bool unify(int u, int v) { n   int cu = find(u), cv = find(v); n   if (cu == cv) return true; n   if (size[cu] > size[cv]) { n     par[cv] = cu; n     size[cu] += size[cv]; n   } else { n     par[cu] = cv; n     size[cv] += size[cu]; n   } n   return false; n } n int n, m; n void preprocess() { n   for (int i = 0; i < maxn; i++) { n     par[i] = i; n     size[i] = 1; n   } n } n long long mod = 1000000009; n void solve() { n   int a, b; n   long long ans = 1; n   for (int i = 0; i < m; i++) { n     scanf( %d , &a); n     scanf( %d , &b); n     bool z = unify(a, b); n     if (z) { n       ans = (ans + ans) % mod; n     } n     cout << (ans - 1) << endl; n   } n } n bool input() { n   scanf( %d , &n); n   scanf( %d , &m); n   return true; n } n int main() { n   preprocess(); n   int T = 1; n   for (testnum = 1; testnum <= T; testnum++) { n     if (!input()) break; n     solve(); n   } n } n


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


#include <bits/stdc++.h> n using namespace std; n const long long MOD = 1000000007; n const long long N = 100000; n template <typename T> n ostream &operator<<(ostream &os, const vector<T> &v) { n   os <<  { ; n   for (auto it = v.begin(); it != v.end(); ++it) { n     if (it != v.begin()) os <<  ,  ; n     os << *it; n   } n   return os <<  } ; n } n template <typename first, typename second> n ostream &operator<<(ostream &os, const pair<first, second> &p) { n   return os <<  (  << p.first <<  ,   << p.second <<  ) ; n } n template <typename T> n ostream &operator<<(ostream &os, const set<T> &v) { n   os <<  [ ; n   for (auto it = v.begin(); it != v.end(); ++it) { n     if (it != v.begin()) os <<  ,  ; n     os << *it; n   } n   return os <<  ] ; n } n template <typename T> n ostream &operator<<(ostream &os, const multiset<T> &v) { n   os <<  [ ; n   for (auto it = v.begin(); it != v.end(); ++it) { n     if (it != v.begin()) os <<  ,  ; n     os << *it; n   } n   return os <<  ] ; n } n template <typename first, typename second> n ostream &operator<<(ostream &os, const map<first, second> &v) { n   os <<  [ ; n   for (auto it = v.begin(); it != v.end(); ++it) { n     if (it != v.begin()) os <<  ,  ; n     os << it->first <<   =   << it->second; n   } n   return os <<  ] ; n } n pair<long long, long long> get(multiset<long long> &m) { n   auto it = m.end(); n   it--; n   return {*m.begin(), *it}; n } n int32_t main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   long long n; n   cin >> n; n   long long p; n   cin >> p; n   long long q; n   cin >> q; n   long long r; n   cin >> r; n   vector<long long> a(n); n   for (long long i = 0; i < n; i++) { n     cin >> a[i]; n   } n   vector<long long> temp; n   temp.push_back(p); n   temp.push_back(q); n   temp.push_back(r); n   multiset<long long> m(a.begin(), a.end()), pref; n   ; n   long long res = LLONG_MIN; n   for (long long i = 0; i < n; i++) { n     pref.insert(a[i]); n     pair<long long, long long> left = get(pref); n     pair<long long, long long> ryt = get(m); n     long long mid = a[i]; n     long long cur = mid * temp[1]; n     if (temp[0] > 0) { n       cur += (temp[0] * left.second); n     } else { n       cur += (temp[0] * left.first); n     } n     if (temp[2] > 0) { n       cur += (temp[2] * ryt.second); n     } else { n       cur += (temp[2] * ryt.first); n     } n     res = max(res, cur); n     m.erase(m.find(a[i])); n   } n   cout << res <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int ab, bc, ca, abc; n   int temp[4]; n   for (int i = 0; i < 4; i++) { n     cin >> temp[i]; n   } n   sort(temp, temp + 4); n   ab = temp[0]; n   bc = temp[1]; n   ca = temp[2]; n   abc = temp[3]; n   int a, b, c; n   a = abc - bc; n   b = ab - a; n   c = ca - a; n   cout << a <<     << b <<     << c << endl; n } n


#include <bits/stdc++.h> n using namespace std; n template <class T1> n void deb(T1 e) { n   cout << e << endl; n } n template <class T1, class T2> n void deb(T1 e1, T2 e2) { n   cout << e1 <<     << e2 << endl; n } n template <class T1, class T2, class T3> n void deb(T1 e1, T2 e2, T3 e3) { n   cout << e1 <<     << e2 <<     << e3 << endl; n } n template <class T1, class T2, class T3, class T4> n void deb(T1 e1, T2 e2, T3 e3, T4 e4) { n   cout << e1 <<     << e2 <<     << e3 <<     << e4 << endl; n } n template <class T1, class T2, class T3, class T4, class T5> n void deb(T1 e1, T2 e2, T3 e3, T4 e4, T5 e5) { n   cout << e1 <<     << e2 <<     << e3 <<     << e4 <<     << e5 << endl; n } n template <class T1, class T2, class T3, class T4, class T5, class T6> n void deb(T1 e1, T2 e2, T3 e3, T4 e4, T5 e5, T6 e6) { n   cout << e1 <<     << e2 <<     << e3 <<     << e4 <<     << e5 <<     << e6 n        << endl; n } n string st; n long long cum1[500010], cum2[500010], cum3[500010], cumhelp3[500010]; n bool okay(char ch) { n   return ch ==  A  || ch ==  E  || ch ==  I  || ch ==  O  || ch ==  U  || n          ch ==  Y ; n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   ; n   int mini, i, j, n, baki1, baki2; n   cin >> st; n   for (i = 0; i < st.size(); i++) { n     if (!okay(st[i])) continue; n     baki1 = i; n     baki2 = st.size() - i - 1; n     mini = min(baki1, baki2); n     mini++; n     cum1[1]++; n     cum1[mini + 1]--; n     if (baki1 > baki2) swap(baki1, baki2); n     if (baki2 > baki1) { n       cum2[baki1 + 2] += mini; n       cum2[baki2 + 2] -= mini; n     } n     mini--; n     cumhelp3[st.size() - mini + 2]--; n     cum3[st.size() - mini + 1] += mini; n   } n   double ans = 0; n   for (i = 1; i <= st.size(); i++) { n     cum1[i] += cum1[i - 1]; n     cum2[i] += cum2[i - 1]; n     cumhelp3[i] += cumhelp3[i - 1]; n     cum3[i] += cum3[i - 1] + cumhelp3[i]; n   } n   for (i = 1; i <= st.size(); i++) { n     cum1[i] *= (long long)i; n     ans += (double)(cum1[i] + cum2[i] + cum3[i]) / (double)i; n   } n   cout << fixed << setprecision(12) << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, a, b, T, ans = 0; n   cin >> n >> a >> b >> T; n   vector<int> d(n, 1); n   for (int& p : d) { n     char c; n     cin >> c; n     if (c ==  w ) p += b; n   } n   vector<int> rtime(n), ltime(n); n   rtime[0] = d[0], ltime[0] = d[0]; n   for (int i = 1; i < n; i++) { n     rtime[i] = d[i] + a + rtime[i - 1]; n     ltime[i] = d[n - i] + a + ltime[i - 1]; n   } n   if (d[0] <= T) ans = 1; n   for (int i = 1; i < n; i++) { n     int time = T - rtime[i]; n     if (time >= 0) ans = max(ans, 1 + i); n     time -= i * a; n     if (time > 0) { n       time += d[0]; n       int num_left = n           lower_bound(ltime.begin(), ltime.end(), time) - ltime.begin(); n       if (ltime[num_left] > time && num_left > 0) num_left--; n       ans = max(ans, 1 + i + num_left); n     } n   } n   for (int i = 1; i < n; i++) { n     int time = T - ltime[i]; n     if (time >= 0) ans = max(ans, 1 + i); n     time -= i * a; n     if (time > 0) { n       time += d[0]; n       int num_right = n           lower_bound(rtime.begin(), rtime.end(), time) - rtime.begin(); n       if (rtime[num_right] > time && num_right > 0) num_right--; n       ans = max(ans, 1 + i + num_right); n     } n   } n   cout << min(n, ans) << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n signed main() { n   ios_base::sync_with_stdio(0), cin.tie(0); n   int n, l; n   cin >> n >> l; n   int arr[n]; n   for (int i = 0; i < n; i++) { n     cin >> arr[i]; n   } n   sort(arr, arr + n); n   double maxy = INT_MIN * 1.0; n   for (int i = 0; i < n - 1; i++) { n     if (arr[i + 1] - arr[i] > maxy) maxy = arr[i + 1] - arr[i]; n   } n   maxy *= 1.0; n   if (arr[0] == 0 or arr[n - 1] == l) { n     if (arr[0] == 0 and arr[n - 1] == l) { n       cout << fixed << setprecision(10) << maxy / 2; n     } else if (arr[0] == 0) { n       cout << fixed << setprecision(10) << max(maxy / 2, (l - arr[n - 1]) * 1.0) n            <<   n ; n     } else { n       cout << fixed << setprecision(10) << max(maxy / 2, (arr[0]) * 1.0) n            <<   n ; n     } n   } else { n     cout << fixed << setprecision(10) n          << max(maxy / 2, max(l - arr[n - 1], arr[0]) * 1.0) <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long max(long long a, long long b) { n   if (a > b) n     return a; n   else n     return b; n } n long long min(long long a, long long b) { n   if (a < b) n     return a; n   else n     return b; n } n long long power(long long B, long long P) { n   if (P == 0) return 1; n   long long X = power(B, P / 2); n   if (P % 2 == 0) n     return X * X; n   else n     return B * X * X; n } n long long fx4[] = {1, -1, 0, 0}; n long long fy4[] = {0, 0, 1, -1}; n int main() { n   long long T, N, M, X, Y, W, K, Q, R, P; n   cin >> N >> M; n   long long e1 = 0, o1 = 0, e2 = 0, o2 = 0; n   for (int i = 1; i <= N; i++) { n     cin >> X; n     if (X % 2) n       o1++; n     else n       e1++; n   } n   for (int i = 1; i <= M; i++) { n     cin >> X; n     if (X % 2) n       o2++; n     else n       e2++; n   } n   cout << (min(e1, o2) + min(o1, e2)) << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int INF = 0x6fffffff; n const int MINF = 0x80000000; n const long long mod = 1000000007; n const int cons = 100001; n const double pi = 3.141592653589793; n vector<int> pos; n bool isOk(int x, int t) { n   double res = 0; n   for (int i = 1; i < pos.size(); i++) { n     int diff = pos[i] - pos[i - 1]; n     int l = -1; n     int r = x + 1; n     while (l + 1 < r) { n       int mid = l + r >> 1; n       int y = x - mid; n       double avail = (double)y / 2 + mid; n       if (avail >= (double)diff) n         r = mid; n       else n         l = mid; n     } n     if (r == x + 1) return false; n     diff -= r; n     res += (double)r * 2; n     res += min((double)diff, ((double)x - r) / 2); n   } n   return res <= (double)t; n } n int main() { n   int n, k, s, t; n   cin >> n >> k >> s >> t; n   map<int, int> M; n   for (int i = 0; i < n; i++) { n     int c, v; n     scanf( %d%d , &c, &v); n     M[c] = max(M[c], v); n   } n   int cur = 0; n   vector<pair<int, int> > dat; n   for (auto it : M) { n     if (cur < it.second) { n       cur = it.second; n       dat.push_back(it); n     } n   } n   pos.resize(k); n   for (int i = 0; i < k; i++) { n     scanf( %d , &pos[i]); n   } n   pos.push_back(0); n   pos.push_back(s); n   sort(pos.begin(), pos.end()); n   int L = -1; n   int R = dat.size(); n   while (L + 1 < R) { n     int mid = L + R >> 1; n     if (isOk(dat[mid].second, t)) n       R = mid; n     else n       L = mid; n   } n   if (R == dat.size()) n     puts( -1 ); n   else n     printf( %d n , dat[R].first); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n #pragma comment(linker,  /STACK:1024000000,1024000000 ) n template <typename T> n T Max(T a, T b) { n   return a > b ? a : b; n } n template <typename T> n T Min(T a, T b) { n   return a < b ? a : b; n } n template <typename T> n T Abs(T A) { n   return A > 0 ? A : -1 * A; n } n template <typename T> n void Swap(T &a, T &b) { n   T tmp = a; n   a = b; n   b = tmp; n } n const long long LINF = 0x3FFFFFFFFFFFFFFLL; n const int INF = 0X3FFFFFFF; n const int MAXN = 1e5 + 5; n long long sum[MAXN << 2][5]; n int cnt[MAXN << 2]; n char opt[MAXN][5]; n int a[MAXN], c[MAXN]; n inline void PushUp(int rt) { n   for (int i = 0; i < 5; i++) n     sum[rt][i] = n         sum[rt << 1][i] + sum[rt << 1 | 1][((i - cnt[rt << 1]) % 5 + 5) % 5]; n } n void Build(int l, int r, int rt) { n   cnt[rt] = 0; n   fill(sum[rt], sum[rt] + 5, 0); n   if (l == r) return; n   int m = (l + r) >> 1; n   Build(l, m, rt << 1); n   Build(m + 1, r, rt << 1 | 1); n } n void update(int pos, int val, int col, int l, int r, int rt) { n   if (col) n     cnt[rt]++; n   else n     cnt[rt]--; n   if (l == r) { n     sum[rt][0] = (long long)col * val; n     return; n   } n   int m = (l + r) >> 1; n   if (pos <= m) n     update(pos, val, col, l, m, rt << 1); n   else n     update(pos, val, col, m + 1, r, rt << 1 | 1); n   PushUp(rt); n } n int main() { n   int n; n   while (~scanf( %d , &n)) { n     int id = 0; n     for (int i = 1; i <= n; i++) { n       scanf( %s , opt[i]); n       if (opt[i][0] !=  s ) { n         scanf( %d , c + i); n         a[id++] = c[i]; n       } n     } n     sort(a, a + id); n     int num = unique(a, a + id) - a; n     if (num) n       Build(1, num, 1); n     else n       sum[1][2] = 0; n     for (int i = 1; i <= n; i++) { n       int pos = lower_bound(a, a + num, c[i]) - a + 1; n       if (opt[i][0] ==  a ) n         update(pos, c[i], 1, 1, num, 1); n       else if (opt[i][0] ==  d ) { n         if (cnt[1]) update(pos, c[i], 0, 1, num, 1); n       } else { n         if (cnt[1] == 0) sum[1][2] = 0; n         printf( %I64d n , sum[1][2]); n       } n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxN = 2e5 + 5; n const long long base = 9999991; n int n, m; n char s1[maxN], s2[maxN]; n vector<int> res; n long long p[maxN], h1[30], h2[30]; n int mark[30]; n int check() { n   for (int i = 0; i <= 25; ++i) n     if (h1[i]) { n       mark[i] = -1; n       for (int j = 0; j <= 25; ++j) n         if (h1[i] == h2[j]) mark[i] = j; n       if (mark[i] == -1) return 0; n     } else n       mark[i] = -2; n   for (int i = 0; i <= 25; ++i) n     if (h1[i] && mark[mark[i]] != i && mark[mark[i]] != -2) return 0; n   return 1; n } n int main() { n   scanf( %d %d n%s n%s , &n, &m, s1 + 1, s2 + 1); n   p[0] = 1; n   for (int i = 1; i <= n; ++i) p[i] = p[i - 1] * 2 % base; n   for (int i = 1; i <= m; ++i) n     for (int j = 0; j <= 25; ++j) { n       if (s1[i] -  a  == j) n         h1[j] = (h1[j] * 2 + 1) % base; n       else n         h1[j] = h1[j] * 2 % base; n       if (s2[i] -  a  == j) n         h2[j] = (h2[j] * 2 + 1) % base; n       else n         h2[j] = h2[j] * 2 % base; n     } n   if (check()) res.push_back(1); n   for (int i = m + 1; i <= n; ++i) { n     for (int j = 0; j <= 25; ++j) { n       if (s1[i - m] -  a  == j) h1[j] = (h1[j] - p[m - 1] + base) % base; n       ; n       if (s1[i] -  a  == j) n         h1[j] = (h1[j] * 2 + 1) % base; n       else n         h1[j] = h1[j] * 2 % base; n     } n     if (check()) res.push_back(i - m + 1); n   } n   printf( %d n , (int)res.size()); n   for (int i = 0; i < (int)res.size(); ++i) printf( %d  , res[i]); n   return 0; n } n


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


#include <bits/stdc++.h> n using namespace std; n int main() { n   int ans = INT_MAX; n   char W[100001]; n   scanf( %100000s , W); n   string s = W, t =  Bulbasaur ; n   map<char, int> I, M; n   for (char ch : t) ++I[ch]; n   for (char ch : s) ++M[ch]; n   for (auto p : I) { n     ans = min(ans, M[p.first] / p.second); n   } n   printf( %d , ans); n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 2e5; n const int lg = 22; n int spt[2 * N + 100][lg], par[N + 100][lg], H[N + 100], E[2 * N + 100], n     L[2 * N + 100]; n int dept[2 * N + 100], vis[N + 100], mx[N + 100][lg], P[N + 100], n     rg[2 * N + 100], idx = 1; n vector<pair<int, int> > g[N + 100]; n vector<pair<long, pair<int, int> > > edge1, edge2; n int find(int i) { n   if (P[i] == i) return i; n   return P[i] = find(P[i]); n } n bool sameset(int i, int j) { return (find(i) == find(j)); } n void dfs(int v, int dep, int p) { n   dept[v] = dep; n   H[v] = idx; n   L[idx] = dep; n   E[idx++] = v; n   for (auto it : g[v]) { n     if (it.first != p) { n       mx[it.first][0] = it.second; n       par[it.first][0] = v; n       dfs(it.first, dep + 1, v); n       E[idx] = v; n       L[idx++] = dep; n     } n   } n } n int getmin(int i, int j) { n   int l = min(i, j), r = max(i, j); n   int lr = rg[r - l + 1]; n   if (L[spt[l][lr]] <= L[spt[r - (1 << lr) + 1][lr]]) n     return spt[l][lr]; n   else n     return spt[r - (1 << lr) + 1][lr]; n } n int main() { n   int n, m; n   scanf( %d%d , &n, &m); n   for (int i = 1; i <= m; i++) { n     int u, v, w; n     scanf( %d%d%d , &u, &v, &w); n     edge1.push_back(make_pair(w, make_pair(u, v))); n     edge2.push_back(make_pair(w, make_pair(u, v))); n   } n   sort(edge1.begin(), edge1.end()); n   for (int i = 1; i <= n; i++) P[i] = i; n   for (int i = 0; (1 << i) <= 2 * N; i++) { n     rg[1 << i] = i; n   } n   for (int i = 1; i <= 2 * N; i++) { n     if (!rg[i]) rg[i] = rg[i - 1]; n   } n   long long ans = 0; n   for (auto it : edge1) { n     int t1 = it.first; n     int u = it.second.first, v = it.second.second; n     if (!sameset(u, v)) { n       ans += t1; n       g[u].push_back(make_pair(v, t1)); n       g[v].push_back(make_pair(u, t1)); n       u = find(u); n       v = find(v); n       P[v] = u; n     } n   } n   memset(dept, -1, sizeof dept); n   dfs(1, 0, 0); n   for (int j = 1; j < lg; j++) { n     for (int i = 1; i <= n; i++) { n       par[i][j] = par[par[i][j - 1]][j - 1]; n       mx[i][j] = max(mx[i][j - 1], mx[par[i][j - 1]][j - 1]); n     } n   } n   for (int i = 1; i < 2 * n; i++) { n     spt[i][0] = i; n   } n   for (int j = 1; j < lg; j++) { n     for (int i = 1; i + (1 << j) - 1 < 2 * n; i++) { n       if (L[spt[i][j - 1]] <= L[spt[i + (1 << (j - 1))][j - 1]]) n         spt[i][j] = spt[i][j - 1]; n       else n         spt[i][j] = spt[i + (1 << (j - 1))][j - 1]; n     } n   } n   for (auto it : edge2) { n     int u = it.second.first, v = it.second.second; n     int lcs = E[getmin(H[u], H[v])]; n     int minus = 0; n     int cur = u; n     for (int i = lg - 1; i >= 0; i--) { n       if (dept[par[cur][i]] >= dept[lcs]) { n         minus = max(minus, mx[cur][i]); n         cur = par[cur][i]; n       } n     } n     cur = v; n     for (int i = lg - 1; i >= 0; i--) { n       if (dept[par[cur][i]] >= dept[lcs]) { n         minus = max(minus, mx[cur][i]); n         cur = par[cur][i]; n       } n     } n     printf( %lld n , ans + it.first - minus); n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 100000 + 10; n int n; n int data[maxn]; n int f[25][maxn]; n bool mark[(1 << 25)], tt; n int res = 0, p; n int tmp; n int tm, tp; n int main() { n   cin >> n; n   for (int i = 1; i <= n; i++) cin >> data[i]; n   memset(mark, 0, sizeof(mark)); n   for (int k = 0; k <= 20; k++) { n     p = -1; n     for (int i = n; i >= 1; i--) { n       if ((data[i] & (1 << k)) == 0) { n         if (p == -1) { n           p = i; n         } n         f[k][i] = p; n       } else n         p = -1; n     } n   } n   for (int i = 1; i <= n; i++) { n     tmp = data[i]; n     if (!mark[tmp]) { n       mark[tmp] = 1; n       res++; n     } n     p = i; n     while (p <= n) { n       tm = n + 1; n       for (int k = 0; k <= 20; k++) n         if ((tmp & (1 << k)) == 0 && f[k][i] < tm && f[k][i] > 0) { n           tp = k; n           tm = f[k][i]; n         } n       if (tm >= n) break; n       for (int k = 0; k <= 20; k++) n         if (f[k][i] == tm) tmp += (1 << k); n       if (!mark[tmp]) { n         mark[tmp] = 1; n         res++; n       } n       p = tm + 1; n     } n   } n   cout << res << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n map<string, int> person; n vector<pair<int, string> > points; n int cnt; n bool compare(pair<int, string> a, pair<int, string> b) { n   if (a.first == b.first) return a.second < b.second; n   return a.first > b.first; n } n void update(string name, int p = 0) { n   if (!person[name]) { n     person[name] = ++cnt; n     points.push_back(make_pair(p, name)); n   } else n     points[person[name] - 1].first += p; n } n int main() { n   string me; n   int n; n   cin >> me >> n; n   for (int i = 0; i < n; i++) { n     string X, Y, type, temp; n     cin >> X >> type; n     int point; n     if (type ==  posted ) { n       cin >> temp >> Y >> temp; n       point = 15; n     } else if (type ==  commented ) { n       cin >> temp >> Y >> temp; n       point = 10; n     } else { n       cin >> Y >> temp; n       point = 5; n     } n     Y = Y.substr(0, Y.length() - 2); n     if (X == me) n       update(Y, point); n     else if (Y == me) n       update(X, point); n     else n       update(X), update(Y); n   } n   sort(points.begin(), points.end(), compare); n   for (int i = 0; i < cnt; i++) cout << points[i].second << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n using ll = long long; n const int maxn = 2e6 + 5; n char s[maxn], t[maxn]; n int m; n struct Suffixam { n   struct state { n     int len, link; n     map<char, int> next; n   }; n   int id[maxn << 1], size[maxn << 1], cnt[maxn << 1]; n   state st[maxn << 1]; n   int last, sz; n   int vis[maxn << 1]; n   void sam_init() { n     memset(vis, -1, sizeof(vis)); n     sz = 0; n     st[0].len = 0; n     st[0].link = -1; n     st[0].next.clear(); n     sz++; n     last = 0; n   } n   void sam_extend(char c) { n     int cur = sz++; n     st[cur].len = st[last].len + 1; n     st[cur].next.clear(); n     size[cur] = 1; n     int p = last; n     while (~p && !st[p].next.count(c)) { n       st[p].next[c] = cur; n       p = st[p].link; n     } n     if (p == -1) { n       st[cur].link = 0; n     } else { n       int q = st[p].next[c]; n       if (st[p].len + 1 == st[q].len) { n         st[cur].link = q; n       } else { n         int clone = sz++; n         st[clone].len = st[p].len + 1; n         st[clone].next = st[q].next; n         st[clone].link = st[q].link; n         while (~p && st[p].next[c] == q) { n           st[p].next[c] = clone; n           p = st[p].link; n         } n         st[q].link = st[cur].link = clone; n       } n     } n     last = cur; n   } n   void get() { n     for (int i = 1; i < sz; ++i) { n       ++cnt[st[i].len]; n     } n     for (int i = 1; i < sz; ++i) { n       cnt[i] += cnt[i - 1]; n     } n     for (int i = 1; i < sz; ++i) { n       id[cnt[st[i].len]--] = i; n     } n     for (int i = sz; i >= 1; --i) { n       size[st[id[i]].link] += size[id[i]]; n     } n   } n   void solve(const char *ss, int x) { n     ll ans = 0; n     int len = 0, now = 0; n     for (int i = 0; ss[i]; ++i) { n       if (st[now].next.count(ss[i]) != 0) { n         now = st[now].next[ss[i]]; n         len++; n         while (~now && st[st[now].link].len >= m) { n           now = st[now].link; n           len = st[now].len; n         } n         if (vis[now] != x && len >= m) { n           ans += size[now]; n           vis[now] = x; n         } n       } else { n         while (~now && !st[now].next.count(ss[i])) { n           now = st[now].link; n         } n         if (now != -1) { n           len = st[now].len + 1; n           now = st[now].next[ss[i]]; n         } else { n           len = 0; n           now = 0; n         } n         while (~now && st[st[now].link].len >= m) { n           now = st[now].link; n           len = st[now].len; n         } n         if (vis[now] != x && len >= m) { n           ans += size[now]; n           vis[now] = x; n         } n       } n     } n     printf( %lld n , ans); n   } n } sam; n int main() { n   scanf( %s , s + 1); n   int n = strlen(s + 1); n   sam.sam_init(); n   for (int i = 1; i <= n; ++i) { n     sam.sam_extend(s[i]); n   } n   sam.get(); n   int q; n   scanf( %d , &q); n   while (q--) { n     scanf( %s , t + 1); n     m = strlen(t + 1); n     for (int i = 1; i <= m; ++i) { n       t[i + m] = t[i]; n     } n     t[2 * m + 1] = 0; n     sam.solve(t + 1, q); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int mN = 30; n int a[mN]; n int n; n void output(int x) { n   string s; n   string mid; n   for (int i = 1; i <= n; i++) { n     if (a[i] / x % 2) mid += char(i +  a  - 1); n     for (int j = 1; j <= a[i] / x / 2; j++) s += char(i +  a  - 1); n   } n   s += mid; n   for (int i = n; i >= 1; i--) { n     for (int j = 1; j <= a[i] / x / 2; j++) s += char(i +  a  - 1); n   } n   for (int i = 1; i <= x; i++) { n     cout << s; n     reverse(s.begin(), s.end()); n   } n } n int gcd(int x, int y) { n   if (x % y == 0) return y; n   return gcd(y, x % y); n } n int main() { n   cin >> n; n   int odd = 0; n   for (int i = 1; i <= n; i++) { n     cin >> a[i]; n     if (a[i] % 2) odd++; n   } n   int maxl = a[1]; n   for (int i = 2; i <= n; i++) maxl = gcd(maxl, a[i]); n   if (odd > 1) { n     cout << 0 << endl; n     output(1); n   } else if (maxl > 1) { n     if (odd <= 1) { n       cout << maxl << endl; n       output(maxl); n     } n   } else { n     cout << maxl << endl; n     output(maxl); n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int maxN = 130 * 1000 + 5; n const int mod = 1000 * 1000 * 1000 + 7; n short dp[21][32][maxN]; n int mas[maxN]; n int ss[6][maxN]; n int a[20], b[20]; n vector<int> v[6]; n int c[20], d[20]; n int n, m; n char ans[20][20]; n int p; n void solve(int lev, int mask, int state) { n   if (lev == 0) return; n   int tmp = state; n   for (int z = 0; z < n; z++) { n     c[z] = tmp % p; n     tmp /= p; n   } n   int smask = dp[lev][mask][state]; n   for (int j = 0; j < n; j++) { n     d[j] = c[j]; n     if ((mask >> j & 1) != (smask >> j & 1)) { n       if (mask >> j & 1) d[j]--; n     } n   } n   int sstate = 0; n   for (int j = n - 1; j >= 0; j--) sstate *= p, sstate += d[j]; n   solve(lev - 1, smask, sstate); n   for (int i = 0; i < n; i++) ans[i][lev - 1] = ((mask >> i & 1) ?  *  :  . ); n } n int tof[32][32], tofm[32][32]; n int main() { n   ios::sync_with_stdio(0); n   cin.tie(); n   memset(dp, -1, sizeof dp); n   cin >> n >> m; n   for (int mask = 0; mask < (1 << n); mask++) { n     int bef = 0, st = 0; n     for (int i = 0; i < n; i++) n       if (mask >> i & 1) { n         if (bef == 0) st++; n         bef = 1; n       } else n         bef = 0; n     v[st].push_back(mask); n   } n   for (int i = 0; i < n; i++) cin >> a[i]; n   for (int i = 0; i < m; i++) cin >> b[i]; n   p = *max_element(a, a + n); n   p++; n   int stat = 0; n   for (int i = n - 1; i >= 0; i--) stat *= p, stat += a[i]; n   for (int state = 0; state < stat + 1; state++) { n     int tmp = state; n     for (int z = 0; z < n; z++) { n       ss[z][state] = tmp % p; n       if (ss[z][state] == 0) mas[state] |= (1 << z); n       tmp /= p; n     } n   } n   for (int mask = 0; mask < (1 << n); mask++) n     for (int smask = 0; smask < (1 << n); smask++) { n       int pp = 0; n       for (int j = 0; j < n; j++) { n         d[j] = 0; n         if ((mask >> j & 1) != (smask >> j & 1)) { n           if (mask >> j & 1) d[j]--, tofm[mask][smask] |= (1 << j); n         } n       } n       int sstate = 0; n       for (int j = n - 1; j >= 0; j--) sstate *= p, sstate += d[j]; n       tof[mask][smask] = sstate; n     } n   for (int i = 1; i < m + 1; i++) { n     int ind = b[i - 1]; n     for (int state = 0; state < stat + 1; state++) { n       for (auto mask : v[ind]) { n         for (auto smask : v[(i == 1 ? 0 : b[i - 2])]) { n           if (tofm[mask][smask] & mas[state]) continue; n           int sstate = state + tof[mask][smask]; n           if (dp[i - 1][smask][sstate] != -1 || n               (i == 1 && sstate == 0 && smask == 0)) { n             dp[i][mask][state] = smask; n             break; n           } n         } n       } n     } n   } n   int answer = -1; n   for (int i = 0; i < (1 << n); i++) { n     if (dp[m][i][stat] > -1) { n       answer = i; n       break; n     } n   } n   solve(m, answer, stat); n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < m; j++) cout << ans[i][j]; n     cout << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n long long min(long long x, long long y) { n   if (x > y) n     return y; n   else n     return x; n } n int main() { n   long long n, m; n   long long k; n   scanf( %I64d %I64d %I64d , &n, &m, &k); n   long long l = 1, r = n * m, mid, sum; n   while (l <= r) { n     mid = (l + r) >> 1; n     sum = 0; n     for (int i = 1; i <= n; i++) sum += min(m, (mid - 1) / i); n     if (sum < k) n       l = mid + 1; n     else n       r = mid - 1; n   } n   printf( %I64d , l - 1); n   scanf(   ); n } n


#include <bits/stdc++.h> n using namespace std; n bool vis[8][8]; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(nullptr); n   char t; n   int y, x; n   cin >> t >> y; n   --y; n   x = t -  a ; n   for (int i = 0; i < 8; i++) { n     vis[i][y] = true; n     vis[x][i] = true; n   } n   auto mark = [&](int x, int y) { n     vis[x][y] = true; n     for (int i = -2; i <= 2; i++) { n       for (int j = -2; j <= 2; j++) { n         if (i * i + j * j != 5) continue; n         int dx = x + i, dy = y + j; n         if (dx >= 0 && dx < 8 && dy >= 0 && dy < 8) vis[dx][dy] = true; n       } n     } n   }; n   mark(x, y); n   cin >> t >> y; n   t; n   --y; n   mark(t -  a , y); n   int cnt = 0; n   for (int i = 0; i < 8; i++) { n     for (int j = 0; j < 8; j++) cnt += vis[i][j]; n   } n   cout << 64 - cnt; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long INF = (long long)1e16; n const int N = 900300; n const int K = 40; n int n; n long long a[N]; n long long sum[N]; n long long dp[N][2]; n int par[N][2][3]; n int intPoint[N]; n int m; n vector<int> ans; n long long getCost(int L, int R, int f1, int f2) { n   int len = R - L; n   if (len == 1) { n     if (a[L] == 0) return 0; n     if (f1 == 0 || f2 == 0) return -1; n     return a[L]; n   } n   if (len == 2) { n     if (a[L] == a[L + 1]) return 0; n     if (a[L] < a[L + 1]) { n       if (f2 == 0) return -1; n       return abs(a[L + 1] - a[L]); n     } else { n       if (f1 == 0) return -1; n       return abs(a[L + 1] - a[L]); n     } n   } n   if (f1 == 0 && a[L] > a[L + 1]) return -1; n   if (f2 == 0 && a[R - 1] > a[R - 2]) return -1; n   long long cost = abs(sum[R] - sum[L]); n   if (len == 3) { n     if (a[L] + a[L + 2] <= a[L + 1]) return cost; n     if (f1 == 1 && f2 == 1) return cost; n     if (f1 == 0 && f2 == 0) return -1; n     return cost; n   } n   return cost; n } n void restoreAns(int L, int R, int f1, int f2) { n   int len = R - L; n   if (len == 1) { n     return; n   } n   if (len == 2) { n     if (a[L] == 0 || a[L + 1] == 0) return; n     ans.push_back(L); n     return; n   } n   if (f1 == 0 && a[L] != 0 && a[L + 1] != 0) { n     long long x = min(a[L], a[L + 1]); n     ans.push_back(L); n     a[L] -= x; n     a[L + 1] -= x; n   } n   if (f2 == 0 && a[R - 1] != 0 && a[R - 2] != 0) { n     long long x = min(a[R - 1], a[R - 2]); n     ans.push_back(R - 2); n     a[R - 2] -= x; n     a[R - 1] -= x; n   } n   for (int i = L; i < R - 1; i++) { n     if (a[i] != 0 && a[i + 1] != 0) { n       long long x = min(a[i], a[i + 1]); n       ans.push_back(i); n       a[i] -= x; n       a[i + 1] -= x; n     } n   } n } n int main() { n   scanf( %d , &n); n   for (int i = 0; i < n; i++) { n     scanf( %lld , &a[i]); n     sum[i + 1] = sum[i]; n     if (i % 2 == 0) n       sum[i + 1] += a[i]; n     else n       sum[i + 1] -= a[i]; n   } n   for (int i = 0; i <= n; i++) { n     if (i < 5 || i > n - 5) { n       intPoint[m++] = i; n       continue; n     } n     if (a[i - 1] <= a[i - 2] || a[i] <= a[i + 1]) intPoint[m++] = i; n   } n   for (int i = 0; i <= m; i++) dp[i][0] = dp[i][1] = -INF; n   dp[0][0] = 0; n   dp[0][1] = 0; n   for (int i = 0; i < m; i++) n     for (int f1 = 0; f1 < 2; f1++) { n       if (dp[i][f1] < 0) continue; n       for (int j = i + 1; j < min(m, i + K); j++) { n         for (int f2 = 0; f2 < 2 - f1; f2++) n           for (int f3 = 0; f3 < 2; f3++) { n             long long cost = getCost(intPoint[i], intPoint[j], f2, f3); n             if (cost == -1) continue; n             if (dp[i][f1] + cost > dp[j][f3]) { n               dp[j][f3] = dp[i][f1] + cost; n               par[j][f3][0] = i; n               par[j][f3][1] = f1; n               par[j][f3][2] = f2; n             } n           } n       } n     } n   int x = m - 1; n   int f = 0; n   if (dp[x][1] > dp[x][0]) f = 1; n   while (x > 0) { n     restoreAns(intPoint[par[x][f][0]], intPoint[x], par[x][f][2], f); n     int nx = par[x][f][0]; n     f = par[x][f][1]; n     x = nx; n   } n   printf( %d n , (int)ans.size()); n   for (int y : ans) printf( %d n , 1 + y); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int p[111], s[111], v[111]; n int main() { n   int n, m, k, i, Nr = 0, c; n   cin >> n >> m >> k; n   for (i = 1; i <= n; ++i) cin >> p[i]; n   for (i = 1; i <= n; ++i) { n     cin >> s[i]; n     if (p[i] > v[s[i]]) v[s[i]] = p[i]; n   } n   for (i = 1; i <= k; ++i) { n     cin >> c; n     if (p[c] < v[s[c]]) Nr++; n   } n   cout << Nr <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long n, m, u, cnt = 1, ch[4000005][4], dp[4000005][4], dpp[60][4][4]; n long long rev[2][4], nw = 0, ans = 0; n char s[100005]; n const long long inf = 2e18; n void dfs(int p) { n   for (int i = 0; i < 4; i++) dp[p][i] = inf; n   for (int i = 0; i < 4; i++) { n     if (!ch[p][i]) n       dp[p][i] = 0; n     else { n       dfs(ch[p][i]); n       for (int j = 0; j < 4; j++) dp[p][j] = min(dp[p][j], dp[ch[p][i]][j] + 1); n     } n   } n } n int main() { n   scanf( %lld%s , &n, s + 1); n   m = strlen(s + 1); n   for (int i = 1; i <= m; i++) { n     int lim = min(m, 1ll * i + 39); n     u = 1; n     for (int j = i; j <= lim; j++) { n       int le = s[j] -  A ; n       if (!ch[u][le]) ch[u][le] = ++cnt; n       u = ch[u][le]; n     } n   } n   dfs(1); n   for (int i = 0; i < 4; i++) n     for (int j = 0; j < 4; j++) dpp[0][i][j] = dp[ch[1][i]][j] + 1; n   for (int i = 1; i < 60; i++) { n     for (int j = 0; j < 4; j++) { n       for (int k = 0; k < 4; k++) { n         dpp[i][j][k] = inf; n         for (int u = 0; u < 4; u++) n           dpp[i][j][k] = min(dpp[i][j][k], dpp[i - 1][j][u] + dpp[i - 1][u][k]); n       } n     } n   } n   ans = 0; n   for (int i = 59; i >= 0; i--) { n     for (int j = 0; j < 4; j++) rev[!nw][j] = inf; n     for (int j = 0; j < 4; j++) n       for (int k = 0; k < 4; k++) n         rev[!nw][k] = min(rev[!nw][k], rev[nw][j] + dpp[i][j][k]); n     long long sum = inf; n     for (int j = 0; j < 4; j++) sum = min(sum, rev[!nw][j]); n     if (sum < n) { n       ans += (1ll << i); n       for (int j = 0; j < 4; j++) n         if (rev[!nw][j] >= n) rev[!nw][j] = inf; n     } else n       for (int j = 0; j < 4; j++) rev[!nw][j] = rev[nw][j]; n     nw = !nw; n   } n   printf( %lld n , ans + 1); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   vector<int> ans, a; n   int n, temp; n   scanf( %d , &n); n   for (int i = 0; i < n; i++) { n     scanf( %d , &temp); n     a.push_back(temp); n   } n   for (int i = 0; i < a.size(); i++) { n     if (a[i] == 1) { n       ans.push_back(1); n     } else { n       ans[ans.size() - 1]++; n     } n   } n   printf( %d n%d , (int)ans.size(), ans[0]); n   for (int i = 1; i < ans.size(); i++) { n     printf(  %d , ans[i]); n   } n   printf(  n ); n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   cin >> n; n   string s; n   cin >> s; n   string gen =  ACTG ; n   int sum = 10000; n   vector<bool> used_big(n, false); n   vector<bool> used_gen(4, false); n   for (int i = 0; i <= n - 4; i++) { n     int tmp_sum = 0; n     for (int j = 0; j < 4; j++) { n       int dif = abs(s[i + j] - gen[j]); n       if (26 - dif < dif) dif = 26 - dif; n       tmp_sum += dif; n     } n     if (tmp_sum < sum) sum = tmp_sum; n   } n   cout << sum; n } n


#include <bits/stdc++.h> n using namespace std; n void precalc(){}; n const int N = 3010; n int n, m; n vector<vector<int>> g; n vector<vector<int>> dist; n pair<int, int> pre[N][3], post[N][3]; n void bfs(int x) { n   queue<int> q; n   q.push(x); n   while (q.size()) { n     int head = q.front(); n     q.pop(); n     for (auto c : g[head]) { n       if (dist[x][c] > n) { n         dist[x][c] = dist[x][head] + 1; n         q.push(c); n       } n     } n   } n } n inline bool distinct(int a, int b, int c, int d) { n   vector<int> z = {a, b, c, d}; n   sort(z.begin(), z.end()); n   return z[0] != z[1] && z[1] != z[2] && z[2] != z[3]; n } n void solve(int ncase) { n   cin >> n >> m; n   g.assign(n + 1, vector<int>()); n   dist.assign(n + 1, vector<int>(n + 1, n + 1)); n   for (int i = 1; i <= n; i++) { n     dist[i][i] = 0; n   } n   for (int i = 0; i < m; i++) { n     int x, y; n     cin >> x >> y; n     g[x].push_back(y); n   } n   for (int i = 1; i <= n; i++) { n     bfs(i); n   } n   for (int i = 1; i <= n; i++) { n     for (int j = 1; j <= n; j++) { n       if (dist[j][i] <= n) { n         pair<int, int> tmppre = pair<int, int>(dist[j][i], j); n         if (tmppre > pre[i][2]) { n           pre[i][2] = tmppre; n           if (pre[i][2] > pre[i][1]) { n             swap(pre[i][2], pre[i][1]); n             if (pre[i][1] > pre[i][0]) { n               swap(pre[i][1], pre[i][0]); n             } n           } n         } n       } n       if (dist[i][j] <= n) { n         pair<int, int> tmppost = pair<int, int>(dist[i][j], j); n         if (tmppost > post[i][2]) { n           post[i][2] = tmppost; n           if (post[i][2] > post[i][1]) { n             swap(post[i][2], post[i][1]); n             if (post[i][1] > post[i][0]) { n               swap(post[i][1], post[i][0]); n             } n           } n         } n       } n     } n   } n   int ans = -1; n   vector<int> ret(4); n   for (int i = 1; i <= n; i++) { n     for (int j = 1; j <= n; j++) { n       if (i == j || dist[i][j] > n) continue; n       for (int a = 0; a < 3; a++) { n         if (pre[i][a].second == 0) continue; n         for (int b = 0; b < 3; b++) { n           if (post[j][b].second == 0) continue; n           int tmp = pre[i][a].first + dist[i][j] + post[j][b].first; n           if (tmp > ans && n               distinct(pre[i][a].second, i, j, post[j][b].second)) { n             ans = tmp; n             ret = {pre[i][a].second, i, j, post[j][b].second}; n           } n         } n       } n     } n   } n   cout << ret[0] <<     << ret[1] <<     << ret[2] <<     << ret[3] << endl; n } n int main() { n   int T = 1; n   int ncase = 0; n   while (T--) { n     solve(++ncase); n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, t, c = 1; n   cin >> n >> t; n   int mn = INT_MAX; n   for (int i = 1; i <= n; i++) { n     int s, d; n     cin >> s >> d; n     while (s < t) { n       s += d; n     } n     if (s < mn) { n       mn = s; n       c = i; n     } n   } n   cout << c << endl; 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(0); n   cerr.tie(0); n   string a; n   cin >> a; n   vector<int> res[2]; n   for (char e : a) { n     vector<int> ne[2]; n     if (e ==  a ) { n       ne[0] = res[1]; n       ne[0].push_back(1); n       ne[1] = res[1]; n       ne[1].push_back(0); n     } else { n       ne[0] = res[0]; n       ne[0].push_back(0); n       ne[1] = res[0]; n       ne[1].push_back(1); n     } n     res[0] = ne[0]; n     res[1] = ne[1]; n   } n   for (int e : res[0]) { n     cout << e <<    ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int v, m, d, f; n int main() { n   int n, a, b, c; n   cin >> n; n   for (int i = 0; i < n; i++) { n     cin >> a >> b >> c; n     if (a == 1) { n       v += b; n       m += c; n     } else { n       d += b; n       f += c; n     } n   } n   if (v >= m) n     cout <<  LIVE  <<   n ; n   else n     cout <<  DEAD  <<   n ; n   if (d >= f) n     cout <<  LIVE  <<   n ; n   else n     cout <<  DEAD  <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int dx[] = {0, 0, 1, -1, -1, -1, 1, 1}; n int dy[] = {1, -1, 0, 0, -1, 1, 1, -1}; n template <class T> n inline T biton(T n, T pos) { n   return n | ((T)1 << pos); n } n template <class T> n inline T bitoff(T n, T pos) { n   return n & ~((T)1 << pos); n } n template <class T> n inline T ison(T n, T pos) { n   return (bool)(n & ((T)1 << pos)); n } n template <class T> n inline T gcd(T a, T b) { n   while (b) { n     a %= b; n     swap(a, b); n   } n   return a; n } n template <typename T> n string NumberToString(T Number) { n   ostringstream second; n   second << Number; n   return second.str(); n } n inline int nxt() { n   int aaa; n   scanf( %d , &aaa); n   return aaa; n } n inline long long int lxt() { n   long long int aaa; n   scanf( %lld , &aaa); n   return aaa; n } n inline double dxt() { n   double aaa; n   scanf( %lf , &aaa); n   return aaa; n } n template <class T> n inline T bigmod(T p, T e, T m) { n   T ret = 1; n   for (; e > 0; e >>= 1) { n     if (e & 1) ret = (ret * p) % m; n     p = (p * p) % m; n   } n   return (T)ret; n } n int ar[200010]; n vector<int> v; n map<int, int> mp; n int main() { n   int c = 0; n   int i = 1; n   while (1) { n     if (c > 100000) break; n     c += i; n     v.push_back(c); n     mp[c] = i; n     i++; n   } n   int n = nxt(); n   if (n == 0) { n     cout <<  a  << endl; n     return 0; n   } n   vector<int> ans; n   while (n) { n     for (int i = v.size() - 1; i >= 0; i--) { n       if (v[i] <= n) { n         ans.push_back(v[i]); n         n -= v[i]; n       } n     } n   } n   int t = 0; n   for (int i = 0; i < ans.size(); i++) { n     int x = ans[i]; n     int lagbe = mp[x]; n     for (int j = 0; j <= lagbe; j++) { n       printf( %c , t +  a ); n     } n     t++; n   } n   printf(  n ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long INF = 0x3f3f3f3f; n const long long MOD = 1e9 + 7; n const long double EPS = 1e-9; n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   string grid[3][3][3]; n   for (int i = 0; i < 3; i++) { n     for (int j = 0; j < 3; j++) { n       for (int k = 0; k < 3; k++) { n         cin >> grid[i][k][j]; n       } n     } n   } n   int a, b; n   cin >> a >> b; n   int gi = (a - 1) % 3; n   int gj = (b - 1) % 3; n   bool shit = true; n   for (int i = 0; i < 3; i++) { n     for (int j = 0; j < 3; j++) { n       if (grid[gi][gj][i][j] ==  . ) shit = false; n     } n   } n   for (int i = 0; i < 3; i++) { n     for (int j = 0; j < 3; j++) { n       for (int k = 0; k < 3; k++) { n         if (shit || i == gi && k == gj) { n           for (int l = 0; l < 3; l++) { n             if (grid[i][k][j][l] ==  . ) n               cout <<  ! ; n             else n               cout << grid[i][k][j][l]; n           } n           cout <<    ; n         } else { n           cout << grid[i][k][j] <<    ; n         } n       } n       cout <<   n ; n     } n     cout <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, a, b, arr[100009]; n int main() { n   cin >> n >> a >> b; n   for (int i = 0; i < n; i++) cin >> arr[i]; n   sort(arr, arr + n); n   cout << max(arr[n - a] - arr[n - a - 1], 0); n } n


#include <bits/stdc++.h> n using namespace std; n int32_t main() { n   ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr); n   long long n, m; n   cin >> n >> m; n   map<long long, set<long long>> adj; n   for (long long i = 0; i < m; i++) { n     long long a, b; n     cin >> a >> b; n     adj[a].insert(b); n     adj[b].insert(a); n   } n   long long mx = INT_MIN, v = 0; n   map<long long, long long> deg; n   for (auto i : adj) { n     deg[i.first] = i.second.size(); n     if (mx < i.second.size()) { n       mx = i.second.size(); n       v = i.first; n     } n   } n   deg[v] = 0; n   for (auto i : adj[v]) { n     deg[i]--; n   } n   long long f = 0; n   for (long long i = 1; i <= n; i++) { n     if (deg[i] > 0) { n       f = 1; n       break; n     } n   } n   if (!f) { n     cout <<  YES n ; n     return 0; n   } n   mx = INT_MIN, v = 0; n   for (long long i = 1; i <= n; i++) { n     if (mx < deg[i]) { n       mx = deg[i]; n       v = i; n     } n   } n   deg[v] = 0; n   for (auto i : adj[v]) { n     deg[i]--; n   } n   f = 0; n   for (long long i = 1; i <= n; i++) { n     if (deg[i] > 0) { n       f = 1; n       break; n     } n   } n   if (!f) { n     cout <<  YES n ; n     return 0; n   } n   cout <<  NO n ; n   return 0; n } n