#include <bits/stdc++.h> n using namespace std; n template <class T> n struct rge { n   T b, e; n }; n template <class T> n rge<T> range(T i, T j) { n   return rge<T>{i, j}; n } n template <class T> n auto dud(T *x) -> decltype(cerr << *x, 0); n template <class T> n char dud(...); n struct debug { n   template <class T> n   debug &operator<<(const T &) { n     return *this; n   } n }; n const int N = 1e5 + 5; n const int M = N; n const int inf = 1e9 + 7; n struct dsu { n   vector<int> pr; n   dsu(int n) : pr(n + 5) { iota(pr.begin(), pr.end(), 0); } n   int find(int u) { return pr[u] == u ? u : pr[u] = find(pr[u]); } n   void unite(int u, int v) { n     u = find(u), v = find(v); n     if (u != v) { n       pr[v] = u; n       debug() <<  the boss of  n                      n               <<  v  n                   :   n               << (v) n               <<     n                   is  n                      n               <<  u  n                   :   n               << (u) <<    ; n     } n   } n }; n struct query { n   int t, u, v, doc; n }; n vector<int> graph[N]; n int pr[N], dep[N], tot[N], heavy[N]; n int head[N], pos[N], who[N]; n set<pair<int, int> > st[N]; n vector<query> queries; n int indeg[N]; n int timer = 0; n int n, m; n int dfs(int u) { n   tot[u] = 1, heavy[u] = head[u] = 0; n   for (int v : graph[u]) n     if (v != pr[u]) { n       pr[v] = u; n       dep[v] = dep[u] + 1; n       tot[u] += dfs(v); n       if (tot[v] > tot[heavy[u]]) heavy[u] = v; n     } n   return tot[u]; n } n void hld(int u, int h) { n   head[u] = h, pos[u] = ++timer, who[timer] = u; n   if (heavy[u] != 0) hld(heavy[u], h); n   for (int v : graph[u]) n     if (v != pr[u] && v != heavy[u]) hld(v, v); n } n template <class F> n void process_path(int u, int v, F fun) { n   for (; head[u] != head[v]; v = pr[head[v]]) { n     if (dep[head[u]] > dep[head[v]]) swap(u, v); n     fun(pos[head[v]], pos[v]); n   } n   if (dep[u] > dep[v]) swap(u, v); n   if (u != v) fun(pos[heavy[u]], pos[v]); n   fun(pos[u], pos[u]); n } n void update_path(int u, int v, int doc) { n   debug() <<  sign from  n                  n           <<  u  n               :   n           << (u) n           <<     n               to  n                  n           <<  v  n               :   n           << (v) n           <<     n                  n           <<  doc  n               :   n           << (doc) <<    ; n   process_path(u, v, [&](int l, int r) { st[doc].insert({l, r}); }); n } n int main() { n   ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); n   cin >> n >> m; n   int doc = 0; n   for (auto i = (0) - ((0) > (m)); i != m - ((0) > (m)); n        i += 1 - 2 * ((0) > (m))) { n     query q; n     cin >> q.t >> q.u; n     if (q.t == 1) { n       cin >> q.v; n       swap(q.u, q.v); n       graph[q.u].push_back(q.v); n       indeg[q.v]++; n     } else if (q.t == 2) { n       q.doc = ++doc; n       st[doc].insert({-inf, -inf}); n       st[doc].insert({inf, inf}); n     } else if (q.t == 3) n       cin >> q.doc; n     queries.push_back(q); n   } n   for (auto u = (1) - ((1) > (n + 1)); u != n + 1 - ((1) > (n + 1)); n        u += 1 - 2 * ((1) > (n + 1))) n     if (indeg[u] == 0) { n       dfs(u); n       hld(u, u); n     } n   dsu boss(n); n   for (auto &q : queries) { n     if (q.t == 1) n       boss.unite(q.u, q.v); n     else if (q.t == 2) n       update_path(q.u, boss.find(q.u), q.doc); n     else { n       debug() <<     n               <<  st[q.doc]  n                   :   n               << (st[q.doc]) <<    ; n       int k = pos[q.u]; n       auto it = --st[q.doc].upper_bound({k, inf}); n       debug() <<     n               <<  k  n                   :   n               << (k) n               <<     n                      n               <<  *it  n                   :   n               << (*it) <<    ; n       for (auto jt : {prev(it), it, next(it)}) n         if (jt != st[q.doc].end()) n           if (jt->first <= k && k <= jt->second) { n             k = inf; n             break; n           } n       cout << (k == inf ?  YES  :  NO ) <<   n ; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int gcd(int a, int b) { n   if (a == 0) return b; n   return gcd(b % a, a); n } n int lcm(int a, int b) { return (a * b) / gcd(a, b); } n int fact(int n) { n   if (n == 0 || n == 1) n     return 1; n   else n     return n * fact(n - 1); n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   int t; n   cin >> t; n   while (t--) { n     int n, a = 0, b = 0, l1 = 0, l2 = 0, c = 0, k = 0; n     cin >> n; n     deque<int> q; n     for (int i = 0, x; i < n; i++) { n       cin >> x; n       q.push_back(x); n     } n     while (!q.empty()) { n       while (l1 <= l2 && !q.empty()) { n         a += q.front(); n         l1 += q.front(); n         q.pop_front(); n         k = 1; n       } n       c += k; n       k = l2 = 0; n       while (l2 <= l1 && !q.empty()) { n         b += q.back(); n         l2 += q.back(); n         q.pop_back(); n         k = 1; n       } n       c += k; n       k = l1 = 0; n     } n     cout << c <<     << a <<     << b << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 2e5 + 5; n int n, m, st[N], fn[N], timer, ans[N]; n vector<int> adj[N], vec[N]; n struct node { n   long long sum, lazy, sz, one; n   node() { sum = one = sz = lazy = 0; } n } seg[N << 2]; n void dfs1(int v, int p = -1) { n   st[v] = timer++; n   for (auto u : adj[v]) { n     if (u == p) continue; n     dfs1(u, v); n   } n   fn[v] = timer; n } n void build(int id = 1, int b = 0, int e = n) { n   if (e - b == 1) { n     seg[id].sz = 1; n     return; n   } n   int mid = (b + e) >> 1, lc = id * 2, rc = lc | 1; n   build(lc, b, mid); n   build(rc, mid, e); n   seg[id].sz = seg[lc].sz + seg[rc].sz; n   return; n } n void add(int l, int r, int val, int id = 1, int b = 0, int e = n) { n   if (l >= e || b >= r) return; n   if (l <= b && e <= r) { n     seg[id].one += val; n     return; n   } n   int mid = (b + e) >> 1, lc = id * 2, rc = lc | 1; n   add(l, r, val, lc, b, mid); n   add(l, r, val, rc, mid, e); n   seg[id].sum = ((seg[lc].one) ? seg[lc].sz : seg[lc].sum) + n                 ((seg[rc].one) ? seg[rc].sz : seg[rc].sum); n   return; n } n void dfs2(int v, int p = -1) { n   if (vec[v].size()) vec[v].push_back(v); n   for (auto u : vec[v]) { n     int l = st[u], r = fn[u]; n     add(l, r, 1); n   } n   ans[v] = ((seg[1].one) ? n : seg[1].sum); n   if (ans[v]) ans[v]--; n   for (auto u : adj[v]) { n     if (u == p) continue; n     dfs2(u, v); n   } n   for (auto u : vec[v]) { n     int l = st[u], r = fn[u]; n     add(l, r, -1); n   } n } n int main() { n   ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); n   cin >> n >> m; n   for (int i = 0; i < n - 1; i++) { n     int v, u; n     cin >> v >> u; n     v--, u--; n     adj[v].push_back(u); n     adj[u].push_back(v); n   } n   dfs1(0); n   build(); n   for (int i = 0; i < m; i++) { n     int a, b; n     cin >> a >> b; n     a--, b--; n     int l = st[b], r = fn[b]; n     vec[a].push_back(b); n     l = st[a], r = fn[a]; n     vec[b].push_back(a); n   } n   dfs2(0); n   for (int i = 0; i < n; i++) cout << ans[i] <<    ; n   cout <<   n ; n } n


#include <bits/stdc++.h> n #define ll long long n #define F first n #define S second n #define pb push_back n #define P push n #define B break n #define C continue n #define R return n #define gcd(x,y) __gcd(x,y) n #define be(a) a.begin(),a.end() n #define lb(a,x) (lower_bound(be(a),x)-a.begin()) n #define ub(a,x) (upper_bound(be(a),x)-a.begin()) n #define uniq(a) a.resize(unique(be(a))-a.begin()) n #define fast ios::sync_with_stdio(NULL); cin.tie(0); cout.tie(0); n #define po(x) (x%mod+mod)%mod n #define SM(x,y) (po(x)+po(y))%mod n #define M(x,y)  (po(x)*po(y))%mod n using namespace std; n ll t,n,i,j,k,l,r,mid,mod=1e9+7,m,ans; n ll a[400005],f[400005],c[400005]; n char ch; n map<ll,ll>mp; n //set<ll>s; n string s,st; n //vector<ll>v[300005],u; n double x,y,z,v,d[105][105],eps=1e-9; n bool b[106][105]; n double dp(int i,int j){ n     double &ret=d[i][j]; n     if(b[i][j]) R ret; n     b[i][j]=1; n     ret=0; n     double X,Y,Z; n     Z=z+((i+j)*v)/2.0; n     X=x+(j*v/2.0)-i*v; n     Y=y+(i*v/2.0)-j*v; n     if(X<eps) { n         X*=-1; n         Y-=X/2.0; n         Z-=X/2.0; n         X=1; n         for(k=1;;k++) { n             ret+=Z*X*(i+j+k); n             if(Y<eps) R ret; n             X*=Y; n             Y-=v; n             Z+=v; n             Z=min(Z,1.0); n         } n         R ret; n     } n     if(Y<eps) { n         Y*=-1; n         X-=Y/2.0; n         Z-=Y/2.0; n         Y=1; n         for(k=1;;k++) { n             ret+=Z*Y*(i+j+k); n             if(X<eps) R ret; n             Y*=X; n             X-=v; n             Z+=v; n             Z=min(Z,1.0); n         } n         R ret; n     } n     R ret=X*dp(i+1,j)+Y*dp(i,j+1)+Z*(i+j+1); n } n int ff(){ n     cin>>x>>y>>z>>v; n     memset(b,0,sizeof b); n     double ans=dp(0,0); n     cout<<fixed<<setprecision(8)<<ans; n     R 0; n } n int main() n { n     fast n     cin>>t; n     while(t--){ n         ff(); n         cout<<  n ; n     } n } n /** n  n */ n


#include <bits/stdc++.h> n using namespace std; n const long long linf = 1e18 + 9; n const int inf = 1e9 + 9; n const int N = 1e3 + 9; n int n, m, c[N], d[N], a[N], b[N], dp[N][N], ans; n int main() { n   cin >> n >> m >> c[0] >> d[0]; n   for (int i = 1; i <= m; i++) cin >> a[i] >> b[i] >> c[i] >> d[i]; n   for (int i = 1; i <= n; i++) { n     for (int j = 1; j <= m; j++) { n       int mx = -inf; n       for (int k = 0; k <= a[j] / b[j]; k++) { n         if (i - c[j] * k < 0) continue; n         mx = max(mx, dp[i - c[j] * k][j - 1] + d[j] * k); n       } n       dp[i][j] = mx; n     } n   } n   for (int i = 0; i <= n; i++) ans = max(ans, dp[i][m] + (n - i) / c[0] * d[0]); n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n using ll = long long; n using ull = unsigned long long; n using ld = long double; n template <typename T> n ostream& operator<<(ostream& os, const vector<T>& v) { n   for (size_t i = 0; i < v.size(); ++i) { n     os << v[i]; n     if (i + 1 != v.size()) { n       os <<    ; n     } n   } n   return os; n } n void solve() { n   int n; n   cin >> n; n   vector<ll> a(n); n   for (int i = 0; i < n; ++i) { n     cin >> a[i]; n   } n   vector<ll> v = a; n   sort(v.begin(), v.end()); n   reverse(v.begin(), v.end()); n   int m; n   cin >> m; n   while (m--) { n     int k, pos; n     cin >> k >> pos; n     --k; n     int mn = v[k]; n     int j = k - 1; n     while (j >= 0 && v[k] == v[j]) { n       --j; n     } n     int cnt = k - j; n     for (int i = 0; i < n; ++i) { n       if (a[i] > mn) { n         --pos; n       } else if (a[i] == mn) { n         if (cnt > 0) { n           --pos; n           --cnt; n         } n       } n       if (pos == 0) { n         cout << a[i] <<   n ; n         break; n       } n     } n   } n } n int main() { n   solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const double pi = acos(-1); n const int INF = 1e9; n const double inf = 1e15; n const double eps = 1e-9; n const long long MOD = 1e9 + 7; n long long toNum(string s) { n   stringstream ss(s); n   long long ret; n   ss >> ret; n   return ret; n } n string toString(long long x) { n   stringstream ss; n   ss << x; n   return ss.str(); n } n long long mulMod(long long a, long long b) { return (a * b) % MOD; } n long long addMod(long long a, long long b) { return (a + b) % MOD; } n long long redMod(long long a, long long b) { n   return addMod(addMod(a, -b), MOD); n } n long long fast(long long a, long long b) { n   if (b == 0) return 1; n   long long tmp = fast(a, b / 2); n   tmp = mulMod(tmp, tmp); n   if (b & 1) tmp = mulMod(tmp, a); n   return tmp; n } n long long GCD(long long a, long long b) { n   if (a == 0) return b; n   return GCD(b % a, a); n } n const int N = 1e5 + 5; n const int BLOCK = 300; n const int CBLOCK = N / BLOCK + 5; n pair<int, int> jmp[N]; n int arr[N]; n int n, q; n vector<pair<int, int> > lst; n pair<int, int> query(int x) { n   int cur = x; n   int nxt = jmp[cur].first; n   int nod = x, val = 1; n   while (cur != nxt) { n     val += jmp[cur].second; n     nod = nxt; n     cur = nxt; n     nxt = jmp[cur].first; n   } n   return make_pair(nod, val); n } n void update(int a, int b) { n   arr[a] = b; n   int idx = (a - 1) / BLOCK; n   int st = idx * BLOCK + 1; n   int en = min(n, (idx + 1) * BLOCK); n   for (int i = en; i >= st; i -= 1) { n     int nxt = i + arr[i]; n     nxt = min(nxt, n + 1); n     if (nxt == n + 1) { n       jmp[i] = make_pair(i, 0); n     } else { n       if ((nxt - 1) / BLOCK != idx) { n         jmp[i] = make_pair(nxt, 1); n       } else { n         jmp[i] = make_pair(jmp[nxt].first, jmp[nxt].second + 1); n       } n     } n   } n } n int main() { n   ios_base ::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   cin >> n >> q; n   for (int i = 1; i <= n; i += 1) cin >> arr[i]; n   for (int i = n; i >= 1; i -= 1) { n     int nxt = i + arr[i]; n     nxt = min(nxt, n + 1); n     if (nxt == n + 1) { n       jmp[i] = make_pair(i, 0); n     } else { n       if ((nxt - 1) / BLOCK != (i - 1) / BLOCK) { n         jmp[i] = make_pair(nxt, 1); n       } else { n         jmp[i] = make_pair(jmp[nxt].first, jmp[nxt].second + 1); n       } n     } n   } n   int ins, a, b; n   for (int i = 1; i <= q; i += 1) { n     cin >> ins >> a; n     if (ins == 0) { n       cin >> b; n       update(a, b); n     } else { n       pair<int, int> ans = query(a); n       cout << ans.first <<     << ans.second << endl; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long iinf = 1 << 29; n const long long inf = 1ll << 60; n const long long mod = 1e9 + 7; n void GG() { n   cout <<  No n ; n   exit(0); n } n long long mpow(long long a, long long n) { n   long long re = 1; n   while (n > 0) { n     if (n & 1) re = re * a % mod; n     a = a * a % mod; n     n >>= 1; n   } n   return re; n } n long long inv(long long b) { n   if (b == 1) return b; n   return (mod - mod / b) * inv(mod % b) % mod; n } n const long long maxn = 5e6 + 1; n const double PI = acos(-1.0); n void FFT(vector<complex<double> > &a, bool rev = 0) { n   long long n = (long long)a.size(); n   assert(__builtin_popcount(n) <= 1); n   for (long long i = 1, j = 0; i < n; i++) { n     long long bit = n / 2; n     while (j >= bit) j -= bit, bit /= 2; n     j += bit; n     if (i < j) swap(a[i], a[j]); n   } n   for (long long B = 2; B <= n; B *= 2) { n     double ang = 2 * PI / B * (rev ? -1 : 1); n     complex<double> w0(cos(ang), sin(ang)); n     for (long long i = 0; i < n; i += B) { n       complex<double> w(1, 0); n       for (long long j = 0; j < B / 2; j++) { n         complex<double> u = a[i + j], v = w * a[i + j + B / 2]; n         a[i + j] = u + v, a[i + j + B / 2] = u - v; n         w *= w0; n       } n     } n   } n   if (rev) n     for (long long i = 0; i < (n); i++) a[i] /= n; n } n vector<long long> mul(vector<long long> a, vector<long long> b) { n   long long n = 1; n   while (n < (long long)a.size() + (long long)b.size()) n *= 2; n   vector<complex<double> > x(n), y(n); n   for (long long i = 0; i < ((long long)a.size()); i++) n     x[i] = complex<double>(a[i], 0); n   for (long long j = 0; j < ((long long)b.size()); j++) n     y[j] = complex<double>(b[j], 0); n   FFT(x); n   FFT(y); n   for (long long i = 0; i < (n); i++) x[i] *= y[i]; n   FFT(x, 1); n   vector<long long> re(n); n   for (long long i = 0; i < (n); i++) re[i] = (long long)(round(x[i].real())); n   while (re.size() > 1 && re.back() == 0) re.pop_back(); n   return re; n } n signed main() { n   long long n, x; n   cin >> n >> x; n   vector<long long> a(n); n   vector<long long> b; n   long long cnt = 1; n   long long zed = 0; n   for (long long i = 0; i < (n); i++) { n     cin >> a[i]; n     if (a[i] < x) { n       b.push_back(cnt); n       cnt = 0; n     } n     cnt++; n   } n   b.push_back(cnt); n   for (long long y : b) zed += (y - 1) * (y) / 2; n   vector<long long> c = b; n   reverse(c.begin(), c.end()); n   vector<long long> d = mul(b, c); n   cout << zed <<    ; n   for (long long i = ((long long)b.size()); i < ((long long)b.size() + n); n        i++) { n     if (i < (long long)d.size()) n       cout << d[i] <<    ; n     else n       cout << 0 <<    ; n   } n } n


#include <bits/stdc++.h> n #pragma GCC optimize( O3 ) n const double PI = acos(-1.0); n using namespace std; n const int N = 509, M = 1000000; n int n, m, dis[N][N], in[N][N], ans[N][N], t[N][N]; n vector<int> adj[N]; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   ; n   cin >> n >> m; n   int u, v, d; n   memset(dis,  ? , sizeof dis); n   for (int i = 1; i <= m; i++) { n     cin >> u >> v >> d; n     adj[u].push_back(v); n     adj[v].push_back(u); n     dis[u][v] = dis[v][u] = d; n     t[u][v] = t[v][u] = d; n   } n   for (int i = 1; i <= n; i++) dis[i][i] = 0; n   for (int k = 1; k <= n; k++) n     for (int i = 1; i <= n; i++) n       for (int j = 1; j <= n; j++) n         dis[i][j] = min(dis[i][j], dis[i][k] + dis[k][j]); n   for (int i = 1; i <= n; i++) { n     for (int j = 1; j <= n; j++) { n       if (i == j) continue; n       for (auto k : adj[j]) { n         if (dis[i][k] + t[k][j] == dis[i][j]) in[i][j]++; n       } n     } n   } n   for (int i = 1; i <= n; i++) { n     for (int j = 1; j <= n; j++) { n       for (int k = 1; k <= n; k++) { n         if (dis[i][k] + dis[k][j] == dis[i][j]) ans[i][j] += in[i][k]; n       } n     } n   } n   for (int i = 1; i <= n; i++) n     for (int j = i + 1; j <= n; j++) { n       if (dis[i][j] >= dis[502][502]) n         cout <<  0  ; n       else n         cout << ans[i][j] <<    ; n     } n   return 0; n } n


#include <bits/stdc++.h> n int main() { n   int tux; n   scanf( %d , &tux); n   int foo = 0, bar = 0, baz = 0, quz = 1; n   for (int i = 0; i < tux; i++) { n     int pur; n     scanf( %d , &pur); n     foo += pur; n     bar++; n     if (foo * quz >= bar * baz) { n       baz = foo; n       quz = bar; n     } n   } n   printf( %.6lf n , (double)baz / quz); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int t; n   cin >> t; n   while (t--) { n     int n; n     cin >> n; n     int arr[n]; n     for (int i = 0; i < n; i++) cin >> arr[i]; n     for (int i = 0; i < n; i++) arr[i] -= 1; n     for (int i = 0; i < n; i++) { n       int count = 1; n       int index = arr[i]; n       while (i != index) { n         index = arr[index]; n         count++; n       } n       cout << count <<    ; n     } n     cout << endl; n     ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 100005; n long long n, a[maxn], aa[maxn], bb[maxn]; n int main() { n   cin >> n; n   for (int i = 1; i <= n; i++) { n     cin >> a[i]; n   } n   for (int i = 1; i <= n; i++) { n     aa[i] = aa[i - 1] + a[i]; n   } n   long long b[maxn]; n   for (int i = 1; i <= n; i++) { n     b[i] = a[i]; n   } n   sort(b + 1, b + n + 1); n   for (int i = 1; i <= n; i++) { n     bb[i] = bb[i - 1] + b[i]; n   } n   int T; n   cin >> T; n   while (T--) { n     int type, l, r; n     long long sum = 0; n     cin >> type >> l >> r; n     if (type == 1) { n       sum = aa[r] - aa[l - 1]; n     } else { n       sum = bb[r] - bb[l - 1]; n     } n     cout << sum << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n using ll = long long; n using db = double; n using pii = pair<int, int>; n using vi = vector<int>; n mt19937 mrand(time(0)); n ll get(ll r) { return ((ll)mrand() * mrand() % r + r) % r; } n ll get(ll l, ll r) { return get(r - l + 1) + l; } n int t, T, n, m, L, R = 1e9, c[100100]; n pii seg[100100]; n vi G[100100]; n bool in(pii seg, int x) { return seg.first <= x && x <= seg.second; } n bool dfs(int x) { n   for (int y : G[x]) n     if (!c[y]) { n       c[y] = 3 - c[x]; n       if (!dfs(y)) return false; n     } else if (c[y] == c[x]) n       return false; n   return true; n } n signed main() { n   scanf( %d %d %d %d , &t, &T, &n, &m); n   for (int i = 1; i <= n; i++) n     scanf( %d %d , &seg[i].first, &seg[i].second), L = max(L, seg[i].first), n                                                    R = min(R, seg[i].second); n   if (L + R > T) R = T - L; n   if (L + R <= t) L = t - R; n   if (L < 0 || R < 0) return puts( IMPOSSIBLE ), 0; n   for (int i = 1; i <= n; i++) { n     int l_ = in(seg[i], L), r_ = in(seg[i], R); n     if (!l_ && !r_) return puts( IMPOSSIBLE ), 0; n     if (l_ && !r_) c[i] = 1; n     if (!l_ && r_) c[i] = 2; n   } n   for (int i = 1, x, y; i <= m; i++) n     scanf( %d %d , &x, &y), G[x].push_back(y), G[y].push_back(x); n   for (int i = 1; i <= n; i++) n     if (c[i] && !dfs(i)) return puts( IMPOSSIBLE ), 0; n   for (int i = 1; i <= n; i++) n     if (!c[i] && (c[i] = 1) && !dfs(i)) return puts( IMPOSSIBLE ), 0; n   string ans; n   for (int i = 1; i <= n; i++) ans += c[i] +  0 ; n   puts( POSSIBLE ), printf( %d %d n , L, R), cout << ans <<   n ; n   fprintf(stderr,  time=%.4f n , (db)clock() / CLOCKS_PER_SEC); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void Read(int &n) { n   int sign = 1; n   n = 0; n   char c; n   do { n     c = getchar(); n   } while (!isdigit(c) && c !=  - ); n   if (c ==  - ) { n     c = getchar(); n     sign = -1; n   } n   do { n     n = n * 10 + c - 48; n     c = getchar(); n   } while (isdigit(c)); n   n *= sign; n } n void Read(long long &n) { n   int sign = 1; n   n = 0; n   char c; n   do { n     c = getchar(); n   } while (!isdigit(c) && c !=  - ); n   if (c ==  - ) { n     c = getchar(); n     sign = -1; n   } n   do { n     n = n * 10ll + c - 48; n     c = getchar(); n   } while (isdigit(c)); n   n *= sign; n } n void Write(int n) { n   if (n == 0) { n     putchar( 0 ); n     return; n   } else if (n < 0) n     putchar( - ), n = -n; n   char C[30]; n   int d = 0; n   while (n) { n     C[++d] = (n % 10) + 48; n     n /= 10; n   } n   for (int i = d; i >= 1; --i) putchar(C[i]); n } n void Write(long long n) { n   if (n == 0) { n     putchar( 0 ); n     return; n   } else if (n < 0) n     putchar( - ), n = -n; n   char C[30]; n   int d = 0; n   while (n) { n     C[++d] = (n % 10ll) + 48; n     n /= 10ll; n   } n   for (int i = d; i >= 1; --i) putchar(C[i]); n } n const int maxn = 1005; n char S[maxn]; n int P1 = 0, P2 = 0; n bool DigitOrDot(char c) { return ( 0  <= c && c <=  9 ) || c ==  . ; } n void Solve(int s, int e) { n   int val = 0; n   int pos = -1; n   for (int i = s; i <= e; ++i) { n     if (S[i] ==  . ) { n       pos = i; n       continue; n     } n     val = val * 10 + (S[i] -  0 ); n   } n   if (e - pos == 2) { n     P1 += val / 100; n     P2 += val % 100; n   } else n     P1 += val; n } n int main() { n   scanf( %s , S + 1); n   int n = strlen(S + 1); n   for (int i = 1; i <= n; ++i) { n     int s = i, e, j = i; n     if (DigitOrDot(S[i])) n       while (j <= n && DigitOrDot(S[j])) ++j; n     e = j - 1; n     if (s <= e) Solve(s, e); n     i = j; n   } n   P1 += P2 / 100; n   P2 %= 100; n   string ans =   ; n   if (P2 != 0) n     for (int i = 1; i <= 2; ++i) ans += char((P2 % 10) +  0 ), P2 /= 10; n   if (ans !=   ) ans +=  . ; n   int d = 0; n   if (P1 == 0) ans +=  0 ; n   while (P1) { n     ans += (char)((P1 % 10) +  0 ); n     P1 /= 10; n     d++; n     if (d == 3) d = 0, ans +=  . ; n   } n   if (ans[ans.size() - 1] ==  . ) ans.erase(ans.size() - 1); n   reverse(ans.begin(), ans.end()); n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n set<int> remaining; n set<pair<long long int, long long int> > edge; n int cur = 1; n int cmp[300011]; n int bfs(int v) { n   int cnt = 0; n   queue<int> q; n   q.push(v); n   vector<int> second; n   while (q.size()) { n     cnt++; n     v = q.front(); n     q.pop(); n     cmp[v] = cur; n     second.clear(); n     for (auto x : remaining) { n       if (!edge.count({x, v})) { n         second.push_back(x); n       } n     } n     for (auto x : second) { n       remaining.erase(x); n       q.push(x); n     } n   } n   return cnt; n } n int main() { n   int n, m, k, u, v; n   cin >> n >> m >> k; n   for (int j = 0; j < m; j++) { n     cin >> u >> v; n     edge.insert({u, v}); n     edge.insert({v, u}); n   } n   for (int i = 2; i <= n; i++) remaining.insert(i); n   int cnt = 0; n   while (remaining.size()) { n     int v = *remaining.begin(); n     remaining.erase(v); n     cnt += bfs(v); n     cur++; n   } n   set<int> q; n   int w = 0; n   for (int i = 2; i <= n; i++) { n     if (!edge.count({1, i})) { n       q.insert(cmp[i]); n       w++; n     } n   } n   if (q.size() <= k and q.size() > 0 and cnt + 1 == n and n       q.size() == cur - 1 and w >= k) { n     cout <<  possible ; n   } else { n     cout <<  impossible ; n   } n } n


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


#include <bits/stdc++.h> n constexpr int MAXN = 1e2 + 2; n constexpr int MAXE = 1e4 + 4; n using namespace std; n int n; n vector<double> choc, prefixSum, suffixSum, cpy; n int main() { n   std::ios::sync_with_stdio(false); n   cin >> n; n   choc.resize(n), prefixSum.resize(n), suffixSum.resize(n); n   for (int i = 0; i < n; i++) { n     cin >> choc[i]; n     if (i == 0) n       prefixSum[0] = choc[0]; n     else n       prefixSum[i] = prefixSum[i - 1] + choc[i]; n   } n   for (int i = n - 1; i >= 0; i--) { n     if (i == n - 1) n       suffixSum[i] = choc[i]; n     else n       suffixSum[i] = suffixSum[i + 1] + choc[i]; n   } n   cpy = suffixSum; n   reverse(cpy.begin(), cpy.end()); n   double mid = prefixSum[n - 1] / 2; n   int midPos = upper_bound(prefixSum.begin(), prefixSum.end(), mid) - n                prefixSum.begin() - 1; n   int i = 0, j = n - 1; n   for (i = 0; i <= midPos; i++) { n     int tAlice = prefixSum[i]; n     j = upper_bound(cpy.begin(), cpy.end(), tAlice * 1.0) - cpy.begin() - 1; n     j = n - 1 - j; n     int tBob = suffixSum[j]; n     if (i + 1 == j) { n       if (tAlice - choc[i] > tBob) n         i--; n       else if (tAlice - choc[i] == tBob) n         break; n       break; n     } n     if (i + 2 == j) { n       if (tAlice == tBob) n         i++; n       else if (tAlice < tBob) n         i++; n       break; n     } n     if (i >= j) break; n   } n   cout << i + 1 <<     << n - i - 1 << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n #pragma warning(disable : 4996) n const long double PI = 3.14159265358979323846; n const long long MOD = 998244353; n mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); n void solve() { n   int n; n   cin >> n; n   vector<int> a(n); n   map<int, deque<int>> d; n   vector<int> b; n   for (int i = 0; i < n; i++) { n     cin >> a[i]; n     b.push_back(a[i]); n   } n   sort(b.begin(), b.end()); n   b.resize(unique(b.begin(), b.end()) - b.begin()); n   for (int i = 0; i < n; i++) { n     a[i] = lower_bound(b.begin(), b.end(), a[i]) - b.begin(); n     d[a[i]].push_back(i); n   } n   map<int, int> dp; n   int ans = 0; n   for (auto c : d) { n     dp[c.first] = d[c.first].size(); n     if (!d[c.first - 1].empty() && d[c.first - 1].back() < d[c.first].front()) { n       dp[c.first] = max(dp[c.first], dp[c.first - 1] + (int)d[c.first].size()); n     } n     int cnt = 0; n     for (auto kek : d[c.first - 1]) { n       if (kek < d[c.first].front()) { n         cnt++; n       } n     } n     dp[c.first] = max(dp[c.first], cnt + (int)d[c.first].size()); n     ans = max(ans, dp[c.first]); n     int ret = 0; n     for (auto kek : d[c.first]) { n       if (!d[c.first - 1].empty() && kek > d[c.first - 1].back()) { n         ret++; n       } n     } n     ans = max(ans, dp[c.first - 1] + ret); n     vector<pair<int, int>> arr; n     for (auto d : d[c.first - 1]) { n       arr.push_back({d, 0}); n     } n     for (auto d : d[c.first]) { n       arr.push_back({d, 1}); n     } n     sort(arr.begin(), arr.end()); n     int dp0 = 0, dp1 = 0; n     for (auto c : arr) { n       if (c.second == 1) { n         dp1++; n       } else { n         dp0++; n         dp1 = max(dp1, dp0); n       } n     } n     ans = max(ans, dp1); n   } n   cout << n - ans <<   n ; n } n int main() { n   srand(time(0)); n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   int q; n   cin >> q; n   while (q--) solve(); n } n


#include <bits/stdc++.h> n using std::cin; n using std::cout; n using std::endl; n const int maxn = 200005; n long long a[maxn], b[maxn]; n int n; n int ans, base; n std::map<std::pair<long long, long long>, int> mmp; n long long gcd(long long x, long long y) { return y == 0 ? x : gcd(y, x % y); } n int main() { n   cin >> n; n   for (int i = 1; i <= n; i++) cin >> a[i]; n   for (int i = 1; i <= n; i++) cin >> b[i]; n   for (int i = 1; i <= n; i++) { n     if (a[i] == 0) { n       if (b[i] == 0) base++; n     } else { n       long long temp1 = b[i], temp2 = a[i]; n       if (temp1 < 0) n         temp1 = -temp1, temp2 = -temp2; n       else if (temp1 == 0) n         temp2 = 0; n       long long g = gcd(llabs(temp1), llabs(temp2)); n       if (g) temp1 /= g, temp2 /= g; n       mmp[std::make_pair(temp1, temp2)]++; n     } n   } n   for (auto it : mmp) { n     ans = std::max(ans, it.second); n   } n   cout << ans + base << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 300005; n template <typename T> n inline void read(T &AKNOI) { n   T x = 0, flag = 1; n   char ch = getchar(); n   while (!isdigit(ch)) { n     if (ch ==  - ) flag = -1; n     ch = getchar(); n   } n   while (isdigit(ch)) { n     x = x * 10 + ch -  0 ; n     ch = getchar(); n   } n   AKNOI = flag * x; n } n namespace ModCalculator { n const int MOD = 1e9 + 7; n inline void Inc(int &x, int y) { n   x += y; n   if (x >= MOD) x -= MOD; n } n inline void Dec(int &x, int y) { n   x -= y; n   if (x < 0) x += MOD; n } n inline int Add(int x, int y) { n   Inc(x, y); n   return x; n } n inline int Sub(int x, int y) { n   Dec(x, y); n   return x; n } n inline int Mul(int x, int y) { return 1LL * x * y % MOD; } n inline int Ksm(int x, int k) { n   int ret = 1; n   for (; k; k >>= 1) { n     if (k & 1) ret = Mul(ret, x); n     x = Mul(x, x); n   } n   return ret; n } n inline int Inv(int x) { return Ksm(x, MOD - 2); } n }  // namespace ModCalculator n using namespace ModCalculator; n int n, fac[MAXN], finv[MAXN], sum[MAXN]; n vector<int> a[MAXN]; n inline int C(int x, int y) { return Mul(fac[x], Mul(finv[y], finv[x - y])); } n void init() { n   read(n); n   for (int i = 1; i <= n; ++i) { n     int x; n     read(x); n     for (int j = 2; j * j <= x; ++j) { n       if (x % j) continue; n       int cnt = 0; n       while (x % j == 0) { n         ++cnt; n         x /= j; n       } n       a[j].push_back(cnt); n     } n     if (x > 1) { n       a[x].push_back(1); n     } n   } n   fac[0] = 1; n   for (int i = 1; i <= n; ++i) { n     fac[i] = Mul(fac[i - 1], i); n   } n   finv[n] = Inv(fac[n]); n   for (int i = n; i >= 1; --i) { n     finv[i - 1] = Mul(finv[i], i); n   } n   sum[0] = 1; n   for (int i = 1; i < n; ++i) { n     sum[i] = Add(sum[i - 1], C(n - 1, i)); n   } n } n void solve() { n   int ans = 0; n   for (int i = 2; i < MAXN; ++i) { n     if (!a[i].size()) continue; n     sort(a[i].begin(), a[i].end()); n     int r = n - a[i].size(); n     for (auto x : a[i]) { n       ++r; n       int coef = (r > 1 ? sum[r - 2] : 0); n       Dec(coef, Sub(sum[n - 1], sum[r - 1])); n       Inc(ans, Mul(x, coef)); n     } n   } n   printf( %d n , ans); n } n int main() { n   init(); n   solve(); n   return 0; n } n


#include <bits/stdc++.h> n #pragma GCC optimize( O3 ) n using namespace std; n const int N = 1e3 + 5, OO = 0x3f3f3f3f, MOD = 1000000007, M = 200000 + 5; n const double EPS = 0.000000001; n int n, x, y; n char str[N]; n int main() { n   scanf( %d %s , &n, str); n   for (int i = 0; i < n; ++i) { n     x += str[i] ==  R  ? 1 : str[i] ==  L  ? -1 : 0; n     y += str[i] ==  U  ? 1 : str[i] ==  D  ? -1 : 0; n   } n   x = abs(x); n   y = abs(y); n   printf( %d n , n - x - y); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int a, q, w, t; n int main() { n   cin >> a; n   cout << (a + 1) / 36 <<     << (a + 1) % 36 / 3; n } n


#include <bits/stdc++.h> n using namespace std; n long long n, ans, val[3001000]; n priority_queue<int> q; n int main() { n   scanf( %lld , &n); n   for (int i = 1; i <= n; i++) scanf( %lld , &val[i]); n   for (int i = 1; i <= n; i++) { n     q.push(-val[i]); n     q.push(-val[i]); n     ans += val[i] + q.top(); n     q.pop(); n   } n   printf( %lld n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 2e5; n int p[N + 10], a[N + 10], pos[N + 10]; n struct seg { n   int l, r, min; n } t[N * 4 + 10]; n int nxt[N + 10][20]; n int get(int x, int n) { n   int ans = x, cnt = 0; n   while (n) { n     if (n & 1) ans = nxt[ans][cnt]; n     if (ans >= 0x7f7f7f7f) break; n     n >>= 1; n     cnt++; n   } n   return ans; n } n int n; n void build(int p, int l, int r) { n   t[p].l = l; n   t[p].r = r; n   if (l == r) { n     t[p].min = get(l, n - 1); n     return; n   } n   int mid = (l + r) / 2; n   build(p * 2, l, mid); n   build(p * 2 + 1, mid + 1, r); n   t[p].min = min(t[p * 2].min, t[p * 2 + 1].min); n } n int query(int p, int l, int r) { n   if (l <= t[p].l && t[p].r <= r) return t[p].min; n   int mid = (t[p].l + t[p].r) / 2, ans = 0x7f7f7f7f; n   if (l <= mid) ans = min(ans, query(p * 2, l, r)); n   if (r > mid) ans = min(ans, query(p * 2 + 1, l, r)); n   return ans; n } n int ans[N + 10]; n int main() { n   int m, q; n   scanf( %d%d%d , &n, &m, &q); n   for (int i = 1; i <= n; i++) { n     scanf( %d , &p[i]); n     pos[p[i]] = i; n   } n   for (int i = 1; i <= m; i++) { n     scanf( %d , &a[i]); n     a[i] = pos[a[i]]; n   } n   memset(pos, 0x7f, sizeof(pos)); n   for (int i = m; i; i--) { n     nxt[i][0] = pos[a[i] == n ? 1 : a[i] + 1]; n     pos[a[i]] = i; n   } n   int logn = (int)(log(n) / log(2)) + 1; n   for (int s = 1; s <= logn; s++) { n     for (int i = 1; i <= m; i++) { n       if (i + (1 << s) - 1 > m) { n         nxt[i][s] = 0x7f7f7f7f; n         continue; n       } n       if (nxt[i][s - 1] > m) { n         nxt[i][s] = 0x7f7f7f7f; n         continue; n       } n       nxt[i][s] = nxt[nxt[i][s - 1]][s - 1]; n     } n   } n   build(1, 1, m); n   for (int i = 1; i <= q; i++) { n     int l, r; n     scanf( %d%d , &l, &r); n     int tmp = query(1, l, r); n     ans[i] = (tmp <= r); n   } n   for (int i = 1; i <= q; i++) putchar(48 ^ ans[i]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long T, n; n   cin >> T; n   while (T--) { n     cin >> n; n     double x = (3.141592653589793238) / (2 * n); n     cout << fixed << setprecision(10) << cos(x) / sin(x) <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n inline void out(int a) { printf( %d n , a); } n inline void out(int a, int b) { printf( %d %d n , a, b); } n inline void outf(double a) { printf( %3.lf n , a); } n inline void outf(double a, double b) { printf( %3.lf %3.lf n , a, b); } n inline void base() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n } n bool cek(long long a, long long b) { n   long long tmp = 0; n   long long ctr = 1; n   while (a) { n     if (a % 10 == 7 || a % 10 == 4) { n       tmp = tmp + ctr * (a % 10); n       ctr *= 10; n     } n     a /= 10; n   } n   return tmp == b; n } n int main() { n   base(); n   long long a, b; n   cin >> a >> b; n   a++; n   while (!cek(a, b)) { n     a++; n   } n   cout << a << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n string a, b, c, s; n const long long mod = 1000000007; n long long res[310000], sum; n int N, ar[310000], srt[310000], dad[310000]; n int where[310000], lcp[310000], F[310000], S[310000], T[310000]; n pair<int, int> as[310000]; n vector<pair<int, int> > q; n int comp(const int &a, const int &b) { return as[a] < as[b]; } n int find(int x) { n   if (dad[x] == x) return x; n   return dad[x] = find(dad[x]); n } n int main() { n   cin >> a >> b >> c; n   N = a.size() + b.size() + c.size() + 2; n   s =     + a +  #  + b +  0  + c; n   for (int i = 1; i <= N; i++) ar[i] = s[i]; n   for (int z = 0; (1 << z) <= N; z++) { n     for (int i = 1; i <= N; i++) n       as[i] = pair<int, int>(ar[i], n                              ((i + (1 << z)) <= N) ? (ar[i + (1 << z)]) : (-1)), n       srt[i] = i; n     sort(srt + 1, srt + 1 + N, comp); n     pair<int, int> prev = pair<int, int>(-1, -1); n     for (int i = 1, cnt = 0; i <= N; i++) { n       if (as[srt[i]] != prev) { n         prev = as[srt[i]]; n         ++cnt; n       } n       ar[srt[i]] = cnt; n     } n   } n   for (int i = 1; i <= N; i++) where[ar[i]] = i; n   for (int i = 1, ss = 0; i <= N; i++) { n     if (ar[i] == N) continue; n     int x = where[ar[i] + 1]; n     while (s[i + ss] == s[x + ss] && i + ss <= N && x + ss <= N) ss++; n     lcp[ar[i]] = ss; n     q.push_back(pair<int, int>(ss, ar[i])); n     if (ss) ss--; n   } n   for (int i = 1; i <= N; i++) { n     dad[i] = i; n     if (where[i] <= a.size()) n       F[i] = 1; n     else if (where[i] <= a.size() + b.size() + 1) n       S[i] = 1; n     else n       T[i] = 1; n   } n   sort(q.begin(), q.end()); n   reverse(q.begin(), q.end()); n   int M = min(a.size(), min(b.size(), c.size())); n   for (int i = M, j = 0; i; i--) { n     while (j < (int)q.size() && q[j].first >= i) { n       int x = q[j].second, y = q[j].second + 1; n       long long pl = -1LL * F[find(x)] * S[find(x)] * T[find(x)]; n       pl -= 1LL * F[find(y)] * S[find(y)] * T[find(y)]; n       F[find(x)] += F[find(y)]; n       S[find(x)] += S[find(y)]; n       T[find(x)] += T[find(y)]; n       dad[find(y)] = find(x); n       pl += 1LL * F[find(x)] * S[find(x)] * T[find(x)]; n       sum = (sum + pl) % mod; n       j++; n     } n     res[i] = sum; n   } n   for (int i = 1; i <= M; i++) printf( %d  , res[i]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1 << 20; n long long sum; n int k, p, n, a[N + 1], i, b[N + 1], left_tree[N + 1], right_tree[N + 1]; n vector<long long> leftv, rightv; n void update(int index, int value, int tree[]) { n   while (index < N + 1) { n     tree[index] += value; n     index += (index & -index); n   } n } n int read(int index, int tree[]) { n   int sum = 0; n   while (index > 0) { n     sum += tree[index]; n     index -= (index & -index); n   } n   return sum; n } n int main() { n   cin >> n; n   for (i = 0; i < n; i++) { n     cin >> a[i]; n     b[i] = a[i]; n   } n   sort(b, b + n); n   for (i = 0; i < n; i++) { n     a[i] = upper_bound(b, b + n, a[i]) - b; n   } n   for (i = 0; i < n; i++) { n     update(a[i], 1, left_tree); n     leftv.push_back(read(N, left_tree) - read(a[i], left_tree)); n   } n   for (i = n - 1; i >= 0; i--) { n     rightv.push_back(read(a[i], right_tree)); n     update(a[i], 1, right_tree); n   } n   reverse(rightv.begin(), rightv.end()); n   for (i = 0; i < n; i++) { n     sum += rightv[i] * leftv[i]; n   } n   cout << sum; n } n


#include <bits/stdc++.h> n using namespace std; n int n, a[100001], b[100001]; n long long int k, SS; n int compare(const void* a, const void* b) { return (*(int*)a - *(int*)b); } n int main() { n   cin >> n >> k; n   for (int i = 0; i < n; i++) { n     cin >> a[i]; n     b[i] = a[i]; n     SS = SS + a[i]; n   } n   if (SS < k) { n     cout << -1; n     return 0; n   } n   if (SS == k) { n     return 0; n   } n   qsort(b, n, sizeof(int), compare); n   int s = 0; n   long long int K = k; n   for (int i = 0; i < n; i++) n     if (b[i] > s) { n       if (K >= (long long int)(n - i) * (b[i] - s)) { n         K = K - (long long int)(n - i) * (b[i] - s); n         s = b[i]; n       } else { n         break; n       } n     } n   int conlai = n; n   for (int i = 0; i < n; i++) { n     if (a[i] <= s) { n       a[i] = -1; n       conlai--; n     } else n       a[i] = a[i] - s; n   } n   for (int i = 0; i < n; i++) n     if (a[i] != -1) { n       a[i] = a[i] - K / conlai; n       if (a[i] == 0) a[i] == -1; n     } n   K = K % conlai; n   if (K > 0) n     for (int i = 0; i < n; i++) { n       if (a[i] >= 1) { n         K--; n         if (a[i] == 1) a[i] = -1; n         if (K == 0) { n           for (int j = i + 1; j < n; j++) n             if (a[j] != -1) cout << j + 1 <<    ; n           for (int j = 0; j <= i; j++) n             if (a[j] != -1) cout << j + 1 <<    ; n         } n       } n     } n   else n     for (int i = 0; i < n; i++) n       if (a[i] != -1) cout << i + 1 <<    ; n } n


#include <bits/stdc++.h> n using namespace std; n template <class T> n void pv(T a, T b) { n   for (T i = a; i != b; ++i) cout << *i <<    ; n   cout << endl; n } n int main() { n   int N; n   scanf( %d , &N); n   int cnt[3] = {}; n   for (int i = 0; i < (int)(N); ++i) { n     int a; n     scanf( %d , &a); n     ++cnt[a - 1]; n   } n   cout << (N - *max_element(cnt, cnt + 3)) << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <class C> n constexpr int sz(const C &c) { n   return int(c.size()); n } n using ll = long long; n using ld = long double; n constexpr const char nl =   n , sp =    ; n using T = long double; n constexpr const T EPS = 1e-9; n bool lt(T a, T b) { return a + EPS < b; } n bool le(T a, T b) { return !lt(b, a); } n bool gt(T a, T b) { return lt(b, a); } n bool ge(T a, T b) { return !lt(a, b); } n bool eq(T a, T b) { return !lt(a, b) && !lt(b, a); } n bool ne(T a, T b) { return lt(a, b) || lt(b, a); } n int sgn(T a) { return lt(a, 0) ? -1 : lt(0, a) ? 1 : 0; } n template <class F> n struct Functor { n   F f; n   Functor(F f) : f(f) {} n   template <class U> n   bool operator()(const U &a, const U &b) const { n     return f(a, b); n   } n }; n template <class F> n Functor<F> makeFunctor(F f) { n   return Functor<F>(f); n } n using pt = complex<T>; n istream &operator>>(istream &stream, pt &p) { n   T X, Y; n   stream >> X >> Y; n   p = pt(X, Y); n   return stream; n } n ostream &operator<<(ostream &stream, const pt &p) { n   return stream << p.real() <<     << p.imag(); n } n bool operator<(const pt &a, const pt &b) { n   return eq(a.real(), b.real()) ? lt(a.imag(), b.imag()) n                                 : lt(a.real(), b.real()); n } n bool operator<=(const pt &a, const pt &b) { return !(b < a); } n bool operator>(const pt &a, const pt &b) { return b < a; } n bool operator>=(const pt &a, const pt &b) { return !(a < b); } n bool operator==(const pt &a, const pt &b) { return !(a < b) && !(b < a); } n bool operator!=(const pt &a, const pt &b) { return a < b || b < a; } n struct pt_lt { n   bool operator()(const pt &a, const pt &b) const { return a < b; } n }; n struct pt_le { n   bool operator()(const pt &a, const pt &b) const { return a <= b; } n }; n struct pt_gt { n   bool operator()(const pt &a, const pt &b) const { return a > b; } n }; n struct pt_ge { n   bool operator()(const pt &a, const pt &b) const { return a >= b; } n }; n struct pt_eq { n   bool operator()(const pt &a, const pt &b) const { return a == b; } n }; n struct pt_ne { n   bool operator()(const pt &a, const pt &b) const { return a != b; } n }; n T dot(const pt &a, const pt &b) { n   return a.real() * b.real() + a.imag() * b.imag(); n } n T cross(const pt &a, const pt &b) { n   return a.real() * b.imag() - a.imag() * b.real(); n } n T norm(const pt &a) { return dot(a, a); } n T distSq(const pt &a, const pt &b) { return norm(b - a); } n T dist(const pt &a, const pt &b) { return abs(b - a); } n T ang(const pt &a, const pt &b) { return arg(b - a); } n T ang(const pt &a, const pt &b, const pt &c) { n   return remainder(ang(b, a) - ang(b, c), 2 * acos(T(-1))); n } n T area2(const pt &a, const pt &b, const pt &c) { return cross(b - a, c - a); } n int ccw(const pt &a, const pt &b, const pt &c) { return sgn(area2(a, b, c)); } n pt rot(const pt &a, const pt &p, const T &theta) { n   return (a - p) * pt(polar(T(1), theta)) + p; n } n pt perp(const pt &a) { return pt(-a.imag(), a.real()); } n template <class It> n It sortByAng(It st, It en, const pt &pivot) { n   en = partition(st, en, [&](const pt &p) { return p != pivot; }); n   It mid = partition(st, en, [&](const pt &p) { n     return eq(p.imag(), pivot.imag()) ? lt(p.real(), pivot.real()) n                                       : lt(p.imag(), pivot.imag()); n   }); n   auto cmp = [&](const pt &p, const pt &q) { return 0 < ccw(pivot, p, q); }; n   sort(st, mid, cmp); n   sort(mid, en, cmp); n   return en; n } n struct Circle { n   pt o; n   T r; n   Circle(T r = 0) : o(0, 0), r(r) {} n   Circle(const pt &o, T r) : o(o), r(r) {} n   int inCircle(const pt &p) { return sgn(distSq(o, p) - r * r); } n }; n int circleCircleIntersection(const Circle &c1, const Circle &c2, n                              vector<pt> &res) { n   pt d = c2.o - c1.o; n   T d2 = norm(d); n   if (eq(d2, 0)) return eq(c1.r, c2.r) ? 2 : 0; n   T pd = (d2 + c1.r * c1.r - c2.r * c2.r) / 2; n   T h2 = c1.r * c1.r - pd * pd / d2; n   if (!lt(h2, 0)) { n     pt p = c1.o + d * pd / d2, h = perp(d) * sqrt(max(h2 / d2, T(0))); n     res.push_back(p - h); n     res.push_back(p + h); n   } n   sort(res.begin(), res.end(), pt_lt()); n   res.erase(unique(res.begin(), res.end(), pt_eq()), res.end()); n   return !res.empty(); n } n const T PI = acos(T(-1)); n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   int N; n   T D; n   cin >> N >> D; n   vector<T> R(N); n   vector<pt> P(N); n   pt O(0, 0); n   vector<pair<T, int>> A; n   for (int i = 0; i < N; i++) { n     cin >> P[i] >> R[i]; n     T d1 = dist(O, P[i]) - R[i], d2 = dist(O, P[i]) + R[i]; n     int st = max(int(floor(d1 / D)) - 1, 1), en = max(int(ceil(d2 / D)) + 1, 1); n     for (int k = st; k <= en; k++) { n       vector<pt> Q; n       circleCircleIntersection(Circle(O, k * D), Circle(P[i], R[i]), Q); n       if (sz(Q) == 1) Q.push_back(Q[0]); n       if (sz(Q) == 2) { n         if (ccw(O, Q[0], Q[1]) == -1) swap(Q[0], Q[1]); n         T ang0 = arg(Q[0]), ang1 = arg(Q[1]); n         if (gt(ang0, ang1)) ang1 += 2 * PI; n         for (int j = 0; j <= 2; j++) { n           A.emplace_back(ang0, -1); n           A.emplace_back(ang1, 1); n           ang0 += 2 * PI; n           ang1 += 2 * PI; n         } n       } n     } n   } n   sort(A.begin(), A.end()); n   int sm = 0, mx = 0; n   for (auto &&a : A) { n     sm -= a.second; n     mx = max(mx, sm); n   } n   cout << mx << nl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int a, b, c, k, k1, ans; n int main() { n   cin >> a >> b >> c; n   if (b > c) swap(b, c); n   if (b <= a / 2 && c > a / 2) { n     cout <<  Final!  << endl; n     return 0; n   } n   while (c != b) { n     b++; n     c++; n     b /= 2; n     c /= 2; n     ans++; n   } n   cout << ans << endl; n } n


#include <bits/stdc++.h> n using namespace std; n template <class T> n using vc = vector<T>; n template <class T> n using vvc = vc<vc<T>>; n template <class T> n void mkuni(vector<T>& v) { n   sort(v.begin(), v.end()); n   v.erase(unique(v.begin(), v.end()), v.end()); n } n long long rand_int(long long l, long long r) { n   static mt19937_64 gen(chrono::steady_clock::now().time_since_epoch().count()); n   return uniform_int_distribution<long long>(l, r)(gen); n } n template <class T> n void print(T x, int suc = 1) { n   cout << x; n   if (suc == 1) n     cout <<   n ; n   else n     cout <<    ; n } n template <class T> n void print(const vector<T>& v, int suc = 1) { n   for (int i = 0; i < v.size(); i++) n     print(v[i], i == (int)(v.size()) - 1 ? suc : 2); n } n const int maxn = 1e6 + 7; n int ed[maxn]; n int p[maxn]; n struct Edge { n   int u, v; n } edge[maxn]; n struct Q { n   int tp, x; n }; n int fa[maxn]; n int Find(int x) { return x == fa[x] ? x : fa[x] = Find(fa[x]); } n int tot; n vector<int> G[maxn]; n void Union(int x, int y) { n   x = Find(x), y = Find(y); n   if (x == y) return; n   tot++; n   fa[x] = tot; n   fa[y] = tot; n   G[tot].push_back(x); n   G[tot].push_back(y); n } n int ti[maxn], ot[maxn]; n int T; n void dfs(int u, int fa = -1) { n   ti[u] = ++T; n   for (auto v : G[u]) n     if (v != fa) dfs(v, u); n   ot[u] = T; n } n pair<int, int> mx[maxn << 2]; n void modify(int p, int v, int L = 1, int R = tot, int o = 1) { n   if (L == R) { n     mx[o] = make_pair(v, p); n     return; n   } n   int mid = L + R >> 1; n   if (p <= mid) n     modify(p, v, L, mid, o << 1); n   else n     modify(p, v, mid + 1, R, o << 1 | 1); n   mx[o] = max(mx[o << 1], mx[o << 1 | 1]); n } n pair<int, int> ask(int l, int r, int L = 1, int R = tot, int o = 1) { n   if (l <= L && r >= R) return mx[o]; n   pair<int, int> ret = {0, 0}; n   int mid = L + R >> 1; n   if (l <= mid) ret = max(ret, ask(l, r, L, mid, o << 1)); n   if (r > mid) ret = max(ret, ask(l, r, mid + 1, R, o << 1 | 1)); n   return ret; n } n int main() { n   ios::sync_with_stdio(false); n   cin.tie(nullptr); n   int n, m, q; n   cin >> n >> m >> q; n   for (int i = 1; i <= n + m + q; i++) fa[i] = i; n   tot = n; n   for (int i = 1; i <= n; i++) cin >> p[i]; n   for (int i = 1; i <= m; i++) cin >> edge[i].u >> edge[i].v, ed[i] = q; n   vc<Q> query(q); n   for (int i = 0; i < q; i++) { n     int tp, x; n     cin >> tp >> x; n     if (tp == 2) ed[x] = i; n     query[i] = {tp, x}; n   } n   for (int i = 1; i <= m; i++) n     if (ed[i] == q) Union(edge[i].u, edge[i].v); n   for (int i = q - 1; i >= 0; i--) { n     int x = query[i].x; n     if (query[i].tp == 2) n       Union(edge[x].u, edge[x].v); n     else { n       query[i].x = Find(query[i].x); n       ; n     } n   } n   for (int i = 1; i <= tot; i++) n     if (Find(i) == i) dfs(i); n   for (int i = 1; i <= n; i++) modify(ti[i], p[i]); n   for (int i = 0; i < q; i++) { n     if (query[i].tp == 1) { n       int x = query[i].x; n       auto tar = ask(ti[x], ot[x]); n       print(tar.first); n       modify(tar.second, 0); n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n vector<vector<char>> g; n map<long long, bool> dp; n void brute(int n, vector<int> &p) { n   int x = find(p.begin(), p.end(), -1) - p.begin(); n   if (x == int(p.size())) { n     vector<vector<char>> dp2(1 << n, vector<char>(n)); n     vector<int> pos1(n), pos2(n); n     for (int i = 0; i < int(p.size()); i++) { n       pos1[p[i]] = pos2[p[i]]; n       pos2[p[i]] = i; n     } n     for (int i = 0; i < int(n); i++) n       if (g[pos1[i]][pos2[i]]) dp2[1 << i][i] = true; n     for (int mask = 0; mask < int(1 << n); mask++) n       for (int i = 0; i < int(n); i++) n         if (dp2[mask][i]) { n           for (int j = 0; j < int(n); j++) n             if (!((mask >> j) & 1)) { n               dp2[mask | (1 << j)][j] |= n                   (g[pos1[i]][pos1[j]] && g[pos2[i]][pos2[j]]); n               dp2[mask | (1 << j)][j] |= n                   (g[pos1[i]][pos2[j]] && g[pos2[i]][pos1[j]]); n             } n         } n     for (int i = 0; i < int(n); i++) n       if (dp2[(1 << n) - 1][i]) { n         long long num = 0; n         for (int x : p) num = num * 6 + x; n         dp[num] = true; n         break; n       } n     return; n   } n   for (int y = x + 1; y < int(p.size()); ++y) n     if (p[y] == -1) { n       p[x] = p[y] = n; n       brute(n + 1, p); n       p[x] = p[y] = -1; n     } n } n bool dfs(vector<int> p) { n   vector<int> used(int(p.size()), -1); n   int cnt = 0; n   for (int i = 0; i < int(p.size()); i++) n     if (used[p[i]] == -1) used[p[i]] = cnt++; n   long long num = 0; n   for (int &x : p) { n     x = used[x]; n     num = num * 6 + x; n   } n   if (dp.count(num)) return dp[num]; n   bool res = false; n   vector<int> cur(cnt); n   for (int i = 0; i < int(p.size()); i++) ++cur[p[i]]; n   for (int i = 0; i < int(p.size()); i++) n     if (cur[p[i]] > 2) { n       int x = p[i]; n       for (int j = i + 1; j < int(p.size()); ++j) n         if (p[j] == p[i]) { n           p[i] = p[j] = cnt; n           if (dfs(p)) { n             res = true; n             break; n           } n           p[i] = p[j] = x; n         } n       break; n     } n   return dp[num] = res; n } n void brute2(int n, vector<int> &p) { n   int x = find(p.begin(), p.end(), -1) - p.begin(); n   if (x == int(p.size())) { n     dfs(p); n     return; n   } n   for (int i = 0; i < int(n + 1); i++) { n     for (int y = x + 1; y < int(p.size()); ++y) n       if (p[y] == -1) { n         p[x] = p[y] = i; n         brute2(max(n, i + 1), p); n         p[x] = p[y] = -1; n       } n   } n } n int main() { n   int n, m, k; n   scanf( %d%d%d , &n, &m, &k); n   g.resize(n, vector<char>(n)); n   for (int _ = 0; _ < int(m); _++) { n     int v, u; n     scanf( %d%d , &v, &u); n     --v, --u; n     g[v][u] = g[u][v] = 1; n   } n   vector<int> cur(n, -1); n   brute(0, cur); n   brute2(0, cur); n   vector<long long> fact(k + 1); n   fact[0] = 1; n   for (int i = 1; i <= k; ++i) fact[i] = fact[i - 1] * i; n   long long ans = 0; n   for (auto it : dp) n     if (it.second) { n       long long num = it.first; n       long long mx = 1; n       while (num) { n         mx = max(mx, num % 6 + 1); n         num /= 6; n       } n       if (mx <= k) { n         ans += fact[k] / fact[k - mx]; n       } n     } n   printf( %lld n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 1e5 + 5; n const long long mod = 1e9 + 7; n int prime[500], judge[500], lcm[500][40], cnt, tol; n int tree[100][maxn << 2], root[maxn], ls[maxn << 6], rs[maxn << 6], n     sum[maxn << 6], last[maxn << 1]; n int a[maxn], ba = 1 << (int)ceil(log2(1e5 + 1)); n inline void sieve() { n   int sq = sqrt(maxn << 1); n   for (int i = 2; i <= sq; i++) { n     if (!judge[i]) prime[judge[i] = ++cnt] = i; n     for (int j = 1, v; j <= judge[i] && (v = i * prime[j]) <= sq; j++) n       judge[v] = j; n   } n } n inline void build(int n, int no) { n   int x, v = prime[no]; n   for (int i = 1; i <= n; i++) { n     x = 0; n     while (!(a[i] % v)) { n       a[i] /= v; n       x++; n     } n     tree[no][ba + i - 1] = x; n   } n   for (int i = ba - 1; i >= 1; i--) n     tree[no][i] = max(tree[no][i << 1], tree[no][i << 1 | 1]); n } n inline int query(int l, int r, int no) { n   l = l + ba - 1; n   r = r + ba - 1; n   int ans = 0; n   while (l <= r) { n     if (l & 1) ans = max(ans, tree[no][l++]); n     if (~r & 1) ans = max(ans, tree[no][r--]); n     l >>= 1; n     r >>= 1; n   } n   return ans; n } n inline int powmod(long long a, int b) { n   long long ans = 1; n   while (b) { n     if (b & 1) ans = ans * a % mod; n     a = a * a % mod; n     b >>= 1; n   } n   return ans; n } n inline int buildrt(int l, int r) { n   int rt = ++tol; n   if (l == r) return rt; n   int mid = l + r >> 1; n   ls[rt] = buildrt(l, mid); n   rs[rt] = buildrt(mid + 1, r); n   return rt; n } n inline int updatert(int k, int l, int r, int rt, long long v) { n   int now = ++tol; n   ls[now] = ls[rt], rs[now] = rs[rt], sum[now] = max(sum[rt], 1) * v % mod; n   if (l == r) return now; n   int mid = l + r >> 1; n   if (k <= mid) n     ls[now] = updatert(k, l, mid, ls[now], v); n   else n     rs[now] = updatert(k, mid + 1, r, rs[now], v); n   return now; n } n inline long long queryrt(int rt, int l, int r, int L, int R) { n   if (R < l || r < L) return 1; n   if (L <= l && r <= R) return max(1, sum[rt]); n   int mid = l + r >> 1; n   return queryrt(ls[rt], l, mid, L, R) * queryrt(rs[rt], mid + 1, r, L, R) % n          mod; n } n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   ; n   sieve(); n   int n, ans = 0, l, r, q; n   cin >> n; n   for (int i = 1; i <= n; i++) cin >> a[i]; n   for (int i = 1; i <= cnt; i++) build(n, i); n   root[0] = buildrt(1, n); n   for (int i = 1; i <= n; i++) { n     root[i] = root[i - 1]; n     if (a[i] == 1) continue; n     if (last[a[i]]) n       root[i] = updatert(last[a[i]], 1, n, root[i], powmod(a[i], mod - 2)); n     root[i] = updatert(i, 1, n, root[i], a[i]); n     last[a[i]] = i; n   } n   cin >> q; n   while (q--) { n     cin >> l >> r; n     l = (ans + l) % n + 1; n     r = (ans + r) % n + 1; n     if (l > r) swap(l, r); n     ans = 1; n     for (int i = 1; i <= cnt; i++) n       ans = (long long)ans * powmod(prime[i], query(l, r, i)) % mod; n     cout << (ans = ans * queryrt(root[r], 1, n, l, r) % mod) <<   n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n vector<int> p; n vector<int> t; n int main() { n   int n, c, a, ans1 = 0, ans2 = 0, t1 = 0, t2 = 0; n   cin >> n >> c; n   for (int i = 0; i < n * 2; i++) { n     cin >> a; n     if (i < n) { n       p.push_back(a); n     } else { n       t.push_back(a); n     } n   } n   for (int i = 0; i < p.size(); i++) { n     t1 += t[i]; n     ans1 += max(0, p[i] - c * t1); n   } n   for (int i = n - 1; i >= 0; i--) { n     t2 += t[i]; n     ans2 += max(0, p[i] - c * t2); n   } n   if (ans1 > ans2) n     cout <<  Limak ; n   else if (ans2 > ans1) n     cout <<  Radewoosh ; n   else n     cout <<  Tie ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int const N = 3e5 + 5; n int const NN = 20; n int const MAX = 1e9 + 1; n int n, a[N]; n struct ITMAX { n   pair<int, int> tree[4 * N]; n   pair<int, int> get_interval(pair<int, int> p, pair<int, int> q) { n     return make_pair(min(p.first, q.first), max(p.second, q.second)); n   } n   void build(int node, int l, int r, pair<int, int> val[]) { n     if (l == r) { n       tree[node] = val[l]; n       return; n     } n     int mid = (l + r) / 2; n     build(node * 2, l, mid, val); n     build(node * 2 + 1, mid + 1, r, val); n     tree[node] = get_interval(tree[node * 2], tree[node * 2 + 1]); n   } n   pair<int, int> get_max(int node, int l, int r, int d, int c) { n     if (l > c || r < d) { n       return make_pair(MAX, -MAX); n     } n     if (d <= l && r <= c) { n       return tree[node]; n     } n     int mid = (l + r) / 2; n     return get_interval(get_max(node * 2, l, mid, d, c), n                         get_max(node * 2 + 1, mid + 1, r, d, c)); n   } n }; n ITMAX tree[NN]; n pair<int, int> dp[NN][N]; n void solve_nxt() { n   for (int i = 1; i <= n; i++) { n     dp[0][i] = make_pair(max(i - a[i], 1), min(i + a[i], n)); n   } n   tree[0].build(1, 1, n, dp[0]); n   for (int j = 1; j < NN; j++) { n     for (int i = 1; i <= n; i++) { n       dp[j][i] = n           tree[j - 1].get_max(1, 1, n, dp[j - 1][i].first, dp[j - 1][i].second); n     } n     tree[j].build(1, 1, n, dp[j]); n   } n } n int main() { n   ios_base::sync_with_stdio(0); n   cin >> n; n   for (int i = 1; i <= n; i++) { n     cin >> a[i]; n     a[i + n] = a[i]; n     a[i + n + n] = a[i]; n   } n   if (n == 1) { n     cout << 0 << endl; n     return 0; n   } n   n *= 3; n   solve_nxt(); n   int sq = n / 3; n   for (int i = sq + 1; i <= sq + sq; i++) { n     int l = i; n     int r = i; n     int ans = 0; n     for (int j = NN - 1; j >= 0; j--) { n       pair<int, int> interval = tree[j].get_max(1, 1, n, l, r); n       int newL = interval.first; n       int newR = interval.second; n       if (newR - newL + 1 < sq) { n         l = newL; n         r = newR; n         ans += (1 << j); n       } n     } n     cout << ans + 1 <<    ; n   } n   cout << endl; n } n


#include <bits/stdc++.h> n using namespace std; n const int mod = 998244353; n int main() { n   ios::sync_with_stdio(false); n   long long n; n   cin >> n; n   string s; n   cin >> s; n   if (s[0] == s[n - 1]) { n     char a = s[0]; n     char b = s[n - 1]; n     long long x = 0, y = 0; n     for (int i = 0; s[i] == a; i++) { n       x++; n     } n     for (int i = n - 1; s[i] == b; i--) { n       y++; n     } n     cout << ((x + 1) * (y + 1)) % mod; n   } else { n     char a = s[0]; n     char b = s[n - 1]; n     long long x = 0, y = 0; n     for (int i = 0; s[i] == a; i++) { n       x++; n     } n     for (int i = n - 1; s[i] == b; i--) { n       y++; n     } n     cout << (x + y + 1) % mod; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long int pow(long long int a, long long int b) { n   long long int ans = 1; n   while (b) { n     if (b & 1) ans *= a; n     a *= a; n     b >>= 1; n   } n   return ans; n } n long long int powmod(long long int a, long long int b) { n   long long int ans = 1; n   while (b) { n     if (b & 1) ans = (ans * a) % 10000000007; n     a = (a * a) % 10000000007; n     b >>= 1; n   } n   return ans; n } n bool ispal(string &a) { n   for (int i = 0; i < a.size() / 2; i++) n     if (a[i] != a[a.size() - i - 1]) return false; n   return true; n } n unsigned long long int sum(unsigned long long int n) { n   unsigned long long int x = 0; n   while (n) { n     x += n % 10; n     n /= 10; n   } n   return x; n } n int main() { n   { n     ios_base::sync_with_stdio(false); n     cin.tie(NULL); n     cout.tie(NULL); n   }; n   unsigned long long int n, m; n   cin >> n >> m; n   unsigned long long int ans = 0, p = 1; n   for (int i = 0; i < m; i++) { n     unsigned long long int x; n     cin >> x; n     ans += (p > x) ? n - p + x : x - p; n     p = x; n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int inf = 1e9 + 7; n struct node { n   int x, d; n }; n vector<int> adj[100005]; n vector<int> good[105]; n int vis[100005]; n int a[100005]; n int d[100005][105]; n void bfs(int s) { n   queue<node> q; n   for (int i = 0; i < good[s].size(); i++) { n     int v = good[s][i]; n     node tmp; n     tmp.x = v; n     tmp.d = 0; n     q.push(tmp); n   } n   while (!q.empty()) { n     node t = q.front(); n     q.pop(); n     d[t.x][s] = min(d[t.x][s], t.d); n     for (int i = 0; i < adj[t.x].size(); i++) { n       int v = adj[t.x][i]; n       if (vis[v] == 0) { n         vis[v] = 1; n         node tmp; n         tmp.x = v; n         tmp.d = t.d + 1; n         q.push(tmp); n       } n     } n   } n } n int main() { n   int n, m, k, s, u, v; n   scanf( %d%d%d%d , &n, &m, &k, &s); n   for (int i = 1; i <= n; i++) { n     scanf( %d , &a[i]); n     good[a[i]].push_back(i); n   } n   for (int i = 0; i < m; i++) { n     scanf( %d%d , &u, &v); n     adj[u].push_back(v); n     adj[v].push_back(u); n   } n   fill(d[0], d[0] + 105 * 100005, inf); n   for (int i = 1; i <= k; i++) { n     memset(vis, 0, sizeof(vis)); n     bfs(i); n   } n   for (int i = 1; i <= n; i++) { n     sort(d[i] + 1, d[i] + 1 + k); n     long long sum = 0; n     for (int j = 1; j <= s; j++) sum += d[i][j]; n     printf( %lld  , sum); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 2005; n int n, a[N], s1[N], s2[N], ans = 0; n void judge(int x, int y, int mid) { n   int tmp = 0; n   if (x == y) n     tmp = s1[mid] + s2[n] - s2[mid]; n   else n     tmp = s1[x - 1] + s2[n] - s2[y] + s2[mid] - s2[x - 1] + s1[y] - s1[mid]; n   ans = max(ans, tmp); n } n int main() { n   scanf( %d , &n); n   int i, j, x, y; n   for (i = 1; i <= n; i++) scanf( %d , &a[i]); n   for (i = 1; i <= n; i++) { n     s1[i] = s1[i - 1]; n     s2[i] = s2[i - 1]; n     if (a[i] == 1) n       s1[i]++; n     else n       s2[i]++; n   } n   int mx, val, mid; n   for (x = 1; x <= n; x++) { n     if (a[x] == 1) n       val = -1; n     else n       val = 1; n     mid = x; n     mx = val; n     judge(x, x, x); n     judge(x, x, x - 1); n     for (y = x + 1; y <= n; y++) { n       if (a[y] == 1) n         val--; n       else n         val++; n       if (val > mx) mx = val, mid = y; n       judge(x, y, mid); n     } n   } n   printf( %d , ans); n   return 0; n } n


#include <bits/stdc++.h> n #pragma GCC optimize(3,  Ofast ,  inline ) n using namespace std; n long long read() { n   long long x = 0, f = 1; n   char ch = getchar(); n   while (ch <  0  || ch >  9 ) { n     if (ch ==  - ) f = -1; n     ch = getchar(); n   } n   while (ch >=  0  && ch <=  9 ) { n     x = (x << 3) + (x << 1) + (ch ^ 48); n     ch = getchar(); n   } n   return x * f; n } n long long n, m, ans, a[10], G[4005][4005]; n long long gcd(long long x, long long y) { n   if (!y) return x; n   if (G[x][y]) return G[x][y]; n   return G[x][y] = gcd(y, x % y); n } n long long Get(long long x, long long opt) { n   if (opt == 1) { n     if (n % 2 == 0) { n       if (x) n         return n / 2; n       else n         return n / 2 + 1; n     } else n       return n / 2 + 1; n   } else { n     if (m % 2 == 0) { n       if (x) n         return m / 2; n       else n         return m / 2 + 1; n     } else n       return m / 2 + 1; n   } n } n void calc() { n   if ((a[3] - a[1]) * (a[6] - a[2]) - (a[5] - a[1]) * (a[4] - a[2]) % 2 != 0) n     return; n   long long x = Get(a[1], 1) * Get(a[2], 2), y = Get(a[3], 1) * Get(a[4], 2), n             z = Get(a[5], 1) * Get(a[6], 2); n   if (a[1] == a[3] && a[2] == a[4]) y--; n   if (a[1] == a[5] && a[2] == a[6]) z--; n   if (a[3] == a[5] && a[4] == a[6]) z--; n   ans += x * y % 1000000007 * z % 1000000007; n   ans %= 1000000007; n } n void dfs(long long x) { n   if (x == 7) { n     calc(); n     return; n   } n   a[x] = 0; n   dfs(x + 1); n   a[x] = 1; n   dfs(x + 1); n } n signed main() { n   n = read(); n   m = read(); n   for (long long i = 0; i <= 4000; i++) { n     for (long long j = 0; j <= 4000; j++) { n       G[i][j] = gcd(i, j); n     } n   } n   dfs(1); n   for (long long i = 2; i <= n; i++) { n     for (long long j = 2; j <= m; j++) { n       long long GCD = G[i][j]; n       ans -= (n - i + 1) % 1000000007 * (m - j + 1) % 1000000007 * (GCD - 1) % n              1000000007 * 12ll % 1000000007 % 1000000007; n       ans %= 1000000007; n       ans += 1000000007; n       ans %= 1000000007; n     } n   } n   for (long long i = 0; i <= n; i++) { n     if (i >= 2) n       ans -= (m + 1) * (i * (i - 1) / 2) % 1000000007 * 6 % 1000000007; n     ans += 1000000007; n     ans %= 1000000007; n   } n   for (long long i = 0; i <= m; i++) { n     if (i >= 2) n       ans -= (n + 1) * (i * (i - 1) / 2) % 1000000007 * 6 % 1000000007; n     ans += 1000000007; n     ans %= 1000000007; n   } n   ans %= 1000000007; n   ans += 1000000007; n   ans += 1000000007; n   ans %= 1000000007; n   printf( %d n , ans); n } n


#include <bits/stdc++.h> n using namespace std; n vector<pair<int, int> > q[500005]; n int n, m, tot, nms, ans[500005], dep[500005], ms[500005], sz[500005], n     t[500005][30], a[500005], hd[500005], to[500005], nxt[500005]; n char ch[500005]; n int read() { n   int x = 0, fl = 1; n   char ch = getchar(); n   while (ch <  0  || ch >  9 ) { n     if (ch ==  - ) fl = -1; n     ch = getchar(); n   } n   while (ch >=  0  && ch <=  9 ) { n     x = (x << 1) + (x << 3) + ch -  0 ; n     ch = getchar(); n   } n   return x * fl; n } n void add(int x, int y) { n   tot++; n   to[tot] = y; n   nxt[tot] = hd[x]; n   hd[x] = tot; n   return; n } n void dfs(int x, int f) { n   sz[x] = 1; n   int mx = 0; n   for (int i = hd[x]; i; i = nxt[i]) { n     int y = to[i]; n     if (y == f) continue; n     dep[y] = dep[x] + 1, dfs(y, x); n     sz[x] += sz[y]; n     if (sz[y] > mx) mx = sz[y], ms[x] = y; n   } n   return; n } n void calc(int x, int f, int o) { n   t[dep[x]][a[x]] += o; n   for (int i = hd[x]; i; i = nxt[i]) { n     int y = to[i]; n     if (y == f || y == nms) continue; n     calc(y, x, o); n   } n   return; n } n void dsu(int x, int f, int o) { n   for (int i = hd[x]; i; i = nxt[i]) { n     int y = to[i]; n     if (y == f || y == ms[x]) continue; n     dsu(y, x, -1); n   } n   if (ms[x]) dsu(ms[x], x, 1), nms = ms[x]; n   calc(x, f, 1), nms = 0; n   for (int p = 0; p < (int)q[x].size(); p++) { n     int d = q[x][p].first, id = q[x][p].second, cnt = 0; n     for (int i = 1; i <= 26; i++) cnt += (t[d][i] & 1); n     ans[id] = (cnt <= 1); n   } n   if (o == -1) calc(x, f, -1); n   return; n } n int main() { n   n = read(), m = read(); n   for (int i = 2; i <= n; i++) { n     int x = read(); n     add(x, i); n   } n   scanf( %s , ch + 1); n   for (int i = 1; i <= n; i++) a[i] = ch[i] -  a  + 1; n   for (int i = 1; i <= m; i++) { n     int x = read(), y = read(); n     q[x].push_back(make_pair(y, i)); n   } n   dep[1] = 1, dfs(1, 0), dsu(1, 0, -1); n   for (int i = 1; i <= m; i++) printf( %s n , ans[i] ?  Yes  :  No ); n   return 0; n } n


#include <bits/stdc++.h> n const int SIZE = 5e5 + 5; n using namespace std; n vector<pair<int, int> > v; n set<pair<int, int> > H; n bool used[SIZE]; n int b[SIZE]; n int main() { n   int(n); n   scanf( %d , &n); n   for (int i = 0; i < (n); ++i) { n     int(x); n     scanf( %d , &x); n     v.push_back(make_pair(x, i)); n     H.insert(make_pair(i, x)); n   } n   long long an = 0; n   sort((v).begin(), (v).end()); n   for (int i = 0; i < (((int)(v).size())); ++i) { n     if (v[i].second == 0 || v[i].second == n - 1) continue; n     set<pair<int, int> >::iterator it = n         H.lower_bound(make_pair(v[i].second, 0)); n     set<pair<int, int> >::iterator it2 = it; n     int now = it->second; n     it2--; n     int tmp = it2->second; n     it2++; n     it2++; n     tmp = min(it2->second, tmp); n     if (now <= tmp) { n       used[i] = 1; n       an += tmp; n       H.erase(it); n     } n   } n   int bn = 0; n   for (set<pair<int, int> >::iterator it = H.begin(); it != H.end(); it++) { n     b[bn++] = it->second; n   } n   if (bn > 2) { n     sort(b, b + bn); n     for (int i = 0; i + 2 < bn; i++) an += b[i]; n   } n   cout << an << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n typedef long long lint; n typedef pair<int, int> pi; n const int MAXN = 250005; n  n const int mod = 998244353; //1e9 + 7;//998244353; n  n template<typename T> n T gcd(const T &a, const T &b) { n     return b == T(0) ? a : gcd(b, a % b); n } n  n struct mint { n     int val; n     mint() { val = 0; } n     mint(const lint& v) { n         val = (-mod <= v && v < mod) ? v : v % mod; n         if (val < 0) val += mod; n     } n  n     friend ostream& operator<<(ostream& os, const mint& a) { return os << a.val; } n     friend bool operator==(const mint& a, const mint& b) { return a.val == b.val; } n     friend bool operator!=(const mint& a, const mint& b) { return !(a == b); } n     friend bool operator<(const mint& a, const mint& b) { return a.val < b.val; } n  n     mint operator-() const { return mint(-val); } n     mint& operator+=(const mint& m) { if ((val += m.val) >= mod) val -= mod; return *this; } n     mint& operator-=(const mint& m) { if ((val -= m.val) < 0) val += mod; return *this; } n     mint& operator*=(const mint& m) { val = (lint)val*m.val%mod; return *this; } n     friend mint ipow(mint a, lint p) { n         mint ans = 1; for (; p; p /= 2, a *= a) if (p&1) ans *= a; n         return ans; n     } n     friend mint inv(const mint& a) { assert(a.val); return ipow(a, mod - 2); } n     mint& operator/=(const mint& m) { return (*this) *= inv(m); } n  n     friend mint operator+(mint a, const mint& b) { return a += b; } n     friend mint operator-(mint a, const mint& b) { return a -= b; } n     friend mint operator*(mint a, const mint& b) { return a *= b; } n     friend mint operator/(mint a, const mint& b) { return a /= b; } n     operator int64_t() const {return val; } n }; n char str[MAXN]; n  n int main(){ n  int t; n  scanf( %d ,&t); n  while(t--){ n   int n; n   scanf( %d ,&n); n   scanf( %s , str); n   int empt = n; n   int ret = 0; n   for(int i = 0; i < n; i++){ n    if(str[i] ==  0 ) continue; n    int e = i; n    while(e < n && str[e] ==  1 ) e++; n    empt -= e - i; n  //  if((e - i) % 2) empt--; n    ret += (e - i) / 2; n    i = e; n   } n   mint dap = 1, divi = 1; n   for(int i = 0; i < empt + ret; i++) dap *= mint(i + 1); n   for(int i = 0; i < empt; i++) divi *= mint(i + 1); n   for(int i = 0; i < ret; i++) divi *= mint(i + 1); n   cout << dap / divi << endl; n  } n } n


#include <bits/stdc++.h> n using namespace std; n string s; n long long k, n; n int index_1[(int)(1e6 + 5)], num = 0; n void solve() { n   cin >> k >> s; n   n = s.size(); n   if (k == 0) { n     long long last = 0, ans = 0; n     for (int i = 0; i < n; i++) { n       if (s[i] ==  0 ) n         last++; n       else { n         ans += last * (last + 1) / 2; n         last = 0; n       } n     } n     ans += last * (last + 1) / 2; n     cout << ans; n     return; n   } n   for (int i = 0; i < n; i++) { n     if (s[i] ==  1 ) index_1[num++] = i; n   } n   long long ans = 0; n   for (int i = k - 1; i < num; i++) { n     long long t1 = n - 1 - index_1[i] + 1; n     if (i < num - 1) t1 = index_1[i + 1] - index_1[i]; n     long long t2 = index_1[i - (k - 1)] - 0 + 1; n     if (i - (k - 1) > 0) t2 = index_1[i - (k - 1)] - index_1[i - (k - 1) - 1]; n     ans += t1 * t2; n   } n   cout << ans; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   int t = 1; n   while (t--) { n     solve(); n   } n } n


#include <bits/stdc++.h> n using namespace std; n int in() { n   int n; n   scanf( %d , &n); n   return n; n } n long long Lin() { n   long long n; n   scanf( %lld , &n); n   return n; n } n double Din() { n   double n; n   scanf( %lf , &n); n   return n; n } n const long long inf = (long long)1e17; n const long long mod = (long long)1e9 + 7; n const int N = 1e5 + 5; n vector<int> g[N]; n bool vis[N]; n int par[18][N], dep[N], sz[N]; n void lca_build(int u, int d = 0) { n   vis[u] = true, dep[u] = d, sz[u] = 1; n   for (int i = 1; i <= 17; i++) { n     par[i][u] = par[i - 1][par[i - 1][u]]; n   } n   for (int i = 0; i < g[u].size(); i++) { n     int v = g[u][i]; n     if (!vis[v]) { n       par[0][v] = u; n       lca_build(v, dep[u] + 1); n       sz[u] += sz[v]; n     } n   } n } n int lca_query(int u, int v) { n   if (dep[u] < dep[v]) swap(u, v); n   for (int i = 17; i >= 0 && u != v; i--) { n     if (dep[par[i][u]] >= dep[v]) u = par[i][u]; n   } n   if (u == v) return u; n   for (int i = 17; i >= 0; i--) { n     if (par[i][u] != par[i][v]) u = par[i][u], v = par[i][v]; n   } n   return par[0][u]; n } n int lifting(int u, int p) { n   for (int i = 17; i >= 0; i--) { n     if ((1 << i) <= p) u = par[i][u], p -= (1 << i); n   } n   return u; n } n int dis(int u, int v, int l) { return dep[u] + dep[v] - 2 * dep[l]; } n int solve() { n   int n = in(); n   for (int i = 1; i < n; i++) { n     int u = in(), v = in(); n     g[u].push_back(v); n     g[v].push_back(u); n   } n   par[0][1] = 1; n   lca_build(1); n   int q = in(); n   while (q--) { n     int u = in(), v = in(); n     int l = lca_query(u, v); n     int d = dis(u, v, l); n     int ans = 0; n     if (u == v) { n       ans = n; n     } else if (d & 1) { n       ans = 0; n     } else if (dep[u] == dep[v]) { n       int x = lifting(u, d / 2 - 1); n       int y = lifting(v, d / 2 - 1); n       ans = n - sz[x] - sz[y]; n     } else if (dep[u] > dep[v]) { n       int x = lifting(u, d / 2); n       int y = lifting(u, d / 2 - 1); n       ans = sz[x] - sz[y]; n     } else { n       int x = lifting(v, d / 2); n       int y = lifting(v, d / 2 - 1); n       ans = sz[x] - sz[y]; n     } n     printf( %d n , ans); n   } n   return 0; n } n int main() { n   int test = 1, tc = 0; n   while (test--) { n     solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long a[200005]; n long long cum[200005]; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   long long tc; n   cin >> tc; n   while (tc--) { n     long long n, p, k; n     cin >> n >> p >> k; n     for (int i = 0; i < n; i++) cin >> a[i]; n     sort(a, a + n); n     long long ans = 0; n     long long cnt = 0; n     long long temp = p; n     for (int i = k - 1; i < n; i += k) { n       if (a[i] <= temp) { n         temp -= a[i]; n         cnt += k; n       } else n         break; n     } n     long long sum = 0; n     for (int i = 0; i < n; i++) { n       sum += a[i]; n       cum[i] = sum; n     } n     ans = max(ans, cnt); n     int in = 0; n     while (in < k && (temp - a[in]) >= 0) { n       cnt = 0; n       temp = p; n       temp -= cum[in]; n       if (temp < 0) break; n       for (int i = in + k; i < n; i += k) { n         if (a[i] <= temp) { n           temp -= a[i]; n           cnt += k; n         } else n           break; n       } n       ans = max(ans, cnt + in + 1); n       in++; n     } n     cout << ans << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n using lint = long long; n const lint linf = 1e18 + 7; n const lint inf = 1e9 + 7; n const int MOD = 1000000007; n void press(vector<long long>& x) { n   map<int, int> memo; n   int n = x.size(); n   for (int i = 0; i < n; ++i) { n     memo[x[i]] += 1; n   } n   int cur = 1; n   for (auto& val : memo) { n     val.second = cur; n     ++cur; n   } n   for (int i = 0; i < n; ++i) { n     x[i] = memo[x[i]]; n   } n } n void solve() { n   int n; n   cin >> n; n   vector<lint> a(n, 0); n   for (int i = 0; i < n; ++i) cin >> a[i]; n   auto b = a; n   press(b); n   vector<vector<int>> dp(n + 5, vector<int>(3, 0)); n   vector<int> cnt(n + 5, 0); n   for (int i = 0; i < n; ++i) cnt[b[i]] += 1; n   int res = n - 1; n   for (int i = 0; i < n; ++i) { n     if (dp[b[i]][0] == 0) { n       dp[b[i]][0] = max(dp[b[i] - 1][1], dp[b[i] - 1][2]) + 1; n     } n     if (dp[b[i]][1] + 1 < cnt[b[i]]) n       ++dp[b[i]][1]; n     else { n       dp[b[i]][2] = dp[b[i]][0] + dp[b[i]][1]; n       res = min(res, n - dp[b[i]][2]); n       dp[b[i]][1] += 1; n     } n     int hoge = dp[b[i]][1] + cnt[b[i] + 1] - dp[b[i] + 1][1]; n     res = min(res, n - hoge); n     hoge = cnt[b[i]] - dp[b[i]][1] + dp[b[i] - 1][2] + 1; n     res = min(res, n - hoge); n   } n   cout << res <<   n ; n } n signed main() { n   cin.tie(0); n   ios::sync_with_stdio(false); n   int t; n   cin >> t; n   for (int i = 0; i < t; ++i) { n     solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int inf = 0x3f3f3f3f; n const int mod = 10007; n const long long INF = 1e18 + 1234; n int setbit(int n, int pos) { return n = n | (1 << pos); } n int resetbit(int n, int pos) { return n = n & ~(1 << pos); } n bool checkbit(int n, int pos) { return (bool)(n & (1 << pos)); } n template <typename T> n T mul(T x, T y) { n   x %= mod; n   y %= mod; n   return (x * y) % mod; n } n template <typename T> n T add(T x, T y) { n   x %= mod; n   y %= mod; n   return (x + y) % mod; n } n template <typename T> n T modPow(T x, T k, T mod) { n   if (k == 0) return 1; n   if (k & 1) { n     return ((x % mod) * modPow(x, k - 1, mod) % mod) % mod; n   } else { n     T ret = modPow(x, k / 2, mod); n     ret %= mod; n     return (ret * ret) % mod; n   } n } n template <typename T> n T ext_gcd(T num1, T num2, T& X, T& Y) { n   T r2, r1, q2, q1, x2, x1, y2, y1, x, y, r; n   x2 = 1; n   y2 = 0; n   x1 = 0; n   y1 = 1; n   for (r2 = num1, r1 = num2; r1 != 0; n        y2 = y1, y1 = y, x2 = x1, x1 = x, r2 = r1, r1 = r) { n     q1 = r2 / r1; n     x = x2 - q1 * x1; n     y = y2 - q1 * y1; n     r = r2 % r1; n   } n   X = x2; n   Y = y2; n   return r2; n } n template <typename T> n void print(const T& v) { n   cerr << v <<    ; n } n template <typename T1, typename... T2> n void print(const T1& first, const T2&... rest) { n   print(first); n   print(rest...); n } n template <class T> n string to_str(T t) { n   stringstream second; n   second << t; n   return second.str(); n } n template <typename T> n T modInv(T x, T m) { n   return modPow(x, m - 2, m); n } n template <typename T> n void dbg_a(T a[], int n = 10) { n   cerr <<  [  ; n   for (int i = 0; i < n; i++) cerr << a[i] <<    ; n   cerr <<   ]  n        <<   n ; n } n template <typename F, typename S> n ostream& operator<<(ostream& os, const pair<F, S>& p) { n   return os <<  [   << p.first <<  ,   << p.second <<   ] ; n } n template <typename T> n ostream& operator<<(ostream& os, const vector<T>& v) { n   os <<  [  ; n   for (int i = 0; i < v.size(); i++) os << v[i] <<    ; n   return os <<   ] n ; n } n const int M = (int)1e6 + 123; n const int maxn = (int)1e6; n bool check(int x, int c, int m) { n   int k = min(m, c); n   c -= x; n   m -= x; n   if (c + m >= x) n     return 1; n   else n     return 0; n } n void solve() { n   int x = 2048, y, z, k, ans = 0, c, m; n   cin >> c >> m >> x; n   k = min(c, min(m, x)); n   ans = k; n   c -= k; n   m -= k; n   x -= k; n   if (c == 0 or m == 0) { n     cout << ans <<   n ; n     return; n   } n   int s = 0, e = min(c, m), mid; n   int ret = 0; n   while (s <= e) { n     mid = s + (e - s) / 2; n     if (check(mid, c, m)) { n       ret = mid; n       s = mid + 1; n     } else { n       e = mid - 1; n     } n   } n   cout << ans + ret <<   n ; n } n int main() { n   ios ::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   long long t = 1, c = 0; n   cin >> t; n   while (t--) { n     solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 2020; n int prv[N], nxt[N], n; n long long h, x[N]; n int px[N], nx[N], pxa[N], nxa[N]; n bool mark[N][N][4]; n double p, dp[N][N][4]; n void prep() { n   for (int i = n - 1; i >= 0; i--) { n     if (i + 1 == n || x[i + 1] - x[i] >= h) { n       nxa[i] = h; n       nxt[i] = i; n     } else { n       nxt[i] = nxt[i + 1]; n       nxa[i] = min(h, x[i + 1] - x[i]); n     } n     if (i + 1 == n) n       nx[i] = h; n     else if (x[i + 1] - x[i] >= 2 * h) n       nx[i] = h; n     else n       nx[i] = max(0LL, x[i + 1] - x[i] - h); n   } n   for (int i = 0; i < n; i++) { n     if (i == 0 || x[i] - x[i - 1] >= h) { n       pxa[i] = h; n       prv[i] = i; n     } else { n       pxa[i] = min(h, x[i] - x[i - 1]); n       prv[i] = prv[i - 1]; n     } n     if (i == 0) n       px[i] = h; n     else if (x[i] - x[i - 1] >= 2 * h) n       px[i] = h; n     else n       px[i] = max(0LL, x[i] - x[i - 1] - h); n   } n } n double f(int l, int r, int mask) { n   if (l > r) return 0; n   if (l == r) { n     int h1 = pxa[l], h2 = nxa[l]; n     if (mask & 1) h1 = px[l]; n     if (mask & 2) h2 = nx[l]; n     return p * h2 + (1.0 - p) * h1; n   } n   double &ret = dp[l][r][mask]; n   if (mark[l][r][mask]) return ret; n   int nxtr = min(r, nxt[l]); n   int h1 = nxa[nxtr]; n   if (mask & 2 && nxtr == r) h1 = nx[nxtr]; n   ret += p * ((x[nxtr] - x[l] + h1) + f(nxtr + 1, r, mask | 1)); n   h1 = pxa[l]; n   if (mask & 1) h1 = px[l]; n   ret += (1.0 - p) * (h1 + f(l + 1, r, mask & 2)); n   ret /= 2.0; n   int prvl = max(l, prv[r]); n   h1 = pxa[prvl]; n   if (mask & 1 && prvl == l) h1 = px[prvl]; n   double ret2 = (1.0 - p) * ((x[r] - x[prvl] + h1) + f(l, prvl - 1, mask | 2)); n   int hh = nxa[r]; n   if (mask & 2) hh = nx[r]; n   ret2 += p * (hh + f(l, r - 1, mask & 1)); n   ret2 *= 0.5; n   ret += ret2; n   mark[l][r][mask] = true; n   return ret; n } n int main(void) { n   ios::sync_with_stdio(false); n   cin >> n >> h >> p; n   p = 1.0 - p; n   for (int i = 0; i < n; i++) cin >> x[i]; n   sort(x, x + n); n   prep(); n   printf( %.11lf n , f(0, n - 1, 0)); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n = 3 * 100000 + 1; n   vector<int> v(n); n   v[0] = 0; n   for (int i = 1; i < n; i++) { n     v[i] = i ^ v[i - 1]; n   } n   int a, b, t, ans; n   cin >> t; n   while (t--) { n     cin >> a >> b; n     if (v[a - 1] == b) { n       ans = a; n     } else if ((v[a - 1] ^ b) == a) n       ans = a + 2; n     else n       ans = a + 1; n     cout << ans << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, a, b, i, p = 2e9; n int main() { n   for (cin >> n; i < n; i++) { n     cin >> a >> b; n     if (max(a, b) <= p) n       p = max(a, b); n     else if (a <= p) n       p = a; n     else if (b <= p) n       p = b; n     else n       return cout <<  NO , 0; n   } n   cout <<  YES ; n } n


#include <bits/stdc++.h> n using namespace std; n inline int re_ad() { n   int x = 0, f = 1; n   char ch = getchar(); n   while (ch >  9  || ch <  0 ) { n     if (ch ==  - ) f = -1; n     ch = getchar(); n   } n   while (ch >=  0  && ch <=  9 ) x = x * 10 + (ch ^ 48), ch = getchar(); n   return x * f; n } n int a, b, m; n int rd[2010], ans; n int col[2010][2010]; n struct node { n   int u, v; n } e[100010]; n int main() { n   register int i, j, x, y; n   register int co[2], now; n   a = re_ad(); n   b = re_ad(); n   m = re_ad(); n   for (i = 1; i <= m; ++i) { n     e[i].u = x = re_ad(); n     e[i].v = y = re_ad() + a; n     ++rd[x]; n     ++rd[y]; n   } n   for (i = 1; i <= a + b; ++i) ans = max(ans, rd[i]); n   for (i = 1; i <= m; ++i) { n     for (j = 1; j <= ans; ++j) n       if (!col[e[i].u][j]) { n         co[0] = j; n         break; n       } n     for (j = 1; j <= ans; ++j) n       if (!col[e[i].v][j]) { n         co[1] = j; n         break; n       } n     col[e[i].u][co[0]] = e[i].v; n     col[e[i].v][co[1]] = e[i].u; n     if (co[0] == co[1]) continue; n     now = 1; n     x = e[i].v; n     while (x) { n       swap(col[x][co[1]], col[x][co[0]]); n       x = col[x][co[now]]; n       now ^= 1; n     } n   } n   printf( %d n , ans); n   for (i = 1; i <= m; ++i) { n     for (j = 1; j <= ans; ++j) { n       if (col[e[i].u][j] == e[i].v) { n         printf( %d  , j); n         break; n       } n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n long long x[1000006] = {}, y[1000006] = {}, a[1000006] = {}, b[1000006] = {}, n           x2[1000006][3], y2[1000006][3]; n long long dfs(int i, long long xx, long long yy, long long px[4][2]) { n   int k = i % 4; n   for (int jj = 0; jj < k - 1; jj++) { n     if (xx == px[jj][0] && yy == px[jj][1]) return -1; n   } n   px[k][0] = xx; n   px[k][1] = yy; n   if (i % 4 < 3) { n     long long c1 = dfs(i + 1, x[i + 1], y[i + 1], px); n     long long c2 = -1; n     for (int jj = 0; jj < 3; jj++) { n       long long c3 = dfs(i + 1, x2[i + 1][jj], y2[i + 1][jj], px); n       if (c3 >= 0 && c2 == -1) n         c2 = c3 + jj + 1; n       else if (c3 >= 0 && c2 >= 0) n         c2 = min(c2, c3 + jj + 1); n     } n     if (c1 < 0 && c2 < 0) n       return -1; n     else if (c1 >= 0 && c2 >= 0) n       return min(c1, c2); n     else if (c1 >= 0) n       return c1; n     else n       return c2; n   } n   vector<long long> vm; n   map<long long, long long> jk; n   for (int ii = 0; ii < 4; ii++) { n     for (int jj = ii + 1; jj < 4; jj++) { n       long long rr = (px[ii][0] - px[jj][0]) * (px[ii][0] - px[jj][0]) + n                      (px[ii][1] - px[jj][1]) * (px[ii][1] - px[jj][1]); n       if (jk.find(rr) == jk.end()) { n         jk[rr] = 1; n         vm.push_back(rr); n       } else n         jk[rr]++; n     } n   } n   if (vm.size() > 2) return -1; n   sort(vm.begin(), vm.end()); n   if (jk[vm[0]] == 4 && jk[vm[1]] == 2 && 2 * vm[0] == vm[1]) n     return 0; n   else n     return -1; n } n long long check(int st) { n   int i = st; n   long long px[4][2] = {}; n   long long c1 = dfs(st, x[st], y[st], px); n   long long c2 = -1; n   for (int jj = 0; jj < 3; jj++) { n     long long c3 = dfs(st, x2[i][jj], y2[i][jj], px); n     if (c3 >= 0 && c2 == -1) n       c2 = c3 + jj + 1; n     else if (c3 >= 0 && c2 >= 0) n       c2 = min(c2, c3 + jj + 1); n   } n   if (c1 < 0 && c2 < 0) n     return -1; n   else if (c1 >= 0 && c2 >= 0) n     return min(c1, c2); n   else if (c1 >= 0) n     return c1; n   else n     return c2; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   int n; n   cin >> n; n   for (int i = 0; i < 4 * n; i++) { n     cin >> x[i] >> y[i] >> a[i] >> b[i]; n     int fx = x[i] - a[i]; n     int fy = y[i] - b[i]; n     for (int j = 2; j >= 0; j--) { n       x2[i][j] = fy; n       y2[i][j] = (-1) * fx; n       fx = x2[i][j]; n       fy = y2[i][j]; n     } n     for (int j = 0; j < 3; j++) { n       x2[i][j] += a[i]; n       y2[i][j] += b[i]; n     } n   } n   for (int i = 0; i < 4 * n; i += 4) { n     long long ct = 0; n     ct = check(i); n     if (ct >= 0) n       cout << ct << endl; n     else n       cout <<  -1 n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int count = 0, sum = 0; n   int n; n   cin >> n; n   int arr[101]; n   for (int i = 0; i < n; i++) { n     cin >> arr[i]; n   } n   for (int i = 0; i < n; i++) { n     sum += arr[i]; n   } n   for (int i = 0; i < n; i++) { n     if ((sum - arr[i]) % 2 == 0) count++; n   } n   cout << count; n   return 0; n } n


#include <bits/stdc++.h> n #pragma comment(linker,  /STACK:256000000 ) n using namespace std; n const int maxN = 80; n long long f[maxN]; n int d[maxN]; n const int maxD = 1000; n const int K = 13; n int dp[maxD][maxD]; n bool filled = false; n pair<int, int> get(int order, long long x) { n   if (order == K && filled) { n     return make_pair(dp[x][1], dp[x][f[K]]); n   } n   if (order == 0) { n     return make_pair(0, 0); n   } n   if (order == 1) { n     return make_pair(x != 1, x != 2); n   } n   if (x > f[order - 1]) { n     int add = 0; n     while (order > 1 && x > f[order - 1]) { n       x -= f[order - 1]; n       order -= 2; n       ++add; n     } n     pair<int, int> current = get(order, x); n     return make_pair(current.first + add, current.second); n   } n   pair<int, int> current = get(order - 1, x); n   int toOne = min(current.first, current.second + 2); n   int toN = min(current.first, current.second) + 1 + d[order - 2]; n   return make_pair(toOne, toN); n } n int calc(int order, long long x, long long y) { n   if (order == K && filled) { n     return dp[x][y]; n   } n   if (x == y) { n     return 0; n   } n   if (x > y) { n     swap(x, y); n   } n   if (order == 1) { n     return x != y; n   } n   if (order == 0) { n     return 0; n   } n   if (f[order - 1] >= y) { n     pair<int, int> a = get(order - 1, x); n     pair<int, int> b = get(order - 1, y); n     int score = min(a.first + b.second, a.second + b.first) + 2; n     return min(score, calc(order - 1, x, y)); n   } n   if (x > f[order - 1]) { n     while (order > 1 && x > f[order - 1]) { n       x -= f[order - 1]; n       y -= f[order - 1]; n       order -= 2; n     } n     return calc(order, x, y); n   } n   pair<int, int> a = get(order - 2, y - f[order - 1]); n   pair<int, int> b = get(order - 1, x); n   return min(b.first, b.second) + 1 + a.first; n } n void gen() { n   int t = 100000; n   int n = 1000; n   cout << t <<     << n << endl; n   for (int i = 0; i < t; ++i) { n     long long x = 1LL * rand() * rand() * rand() + 1LL; n     long long y = 1LL * rand() * rand() * rand() + 1LL; n     cout << x <<     << y << endl; n   } n } n int main() { n   f[0] = 1; n   f[1] = 2; n   for (int i = 2; i < maxN; ++i) { n     f[i] = f[i - 1] + f[i - 2]; n   } n   d[0] = 0; n   d[1] = 1; n   for (int i = 2; i < maxN; ++i) { n     d[i] = d[i - 2] + 1; n   } n   for (int i = 1; i <= f[K]; ++i) { n     for (int j = i; j <= f[K]; ++j) { n       dp[i][j] = dp[j][i] = calc(K, i, j); n     } n   } n   cerr << clock() << endl; n   filled = true; n   int t, n; n   scanf( %d%d , &t, &n); n   if (n >= maxN) { n     n = maxN - 1; n   } n   for (int i = 1; i <= t; ++i) { n     long long u, v; n     scanf( %I64d%I64d , &u, &v); n     if (u > v) { n       swap(u, v); n     } n     int m = n; n     while (m > 20 && f[m - 5] >= v) { n       --m; n     } n     printf( %d n , calc(m, u, v)); n   } n   cerr << clock() << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, i, m, r = 0, t; n   cin >> n; n   for (i = 0; i < n; i++) { n     cin >> m; n     while ((m) && (r)) { n       if (m > r) n         m %= r; n       else n         r %= m; n     } n     r += m; n   } n   cout << r * n << endl; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 8; n const int M = 100010; n const int INF = 1 << 30; n int n, q; n map<int, int> sum, val; n void init() { n   sum.clear(); n   val.clear(); n } n void add(int p, int v) { n   if (val.find(p) == val.end()) val[p] = 0; n   val[p] += v; n   while (p) { n     if (sum.find(p) == sum.end()) sum[p] = 0; n     sum[p] += v; n     p /= 2; n   } n } n double dfs(int h, int u, double Max) { n   if (h == n) { n     if (val.find(u) == val.end()) n       return 0.0; n     else n       return max(Max, (double)val[u]); n   } n   int l = u * 2, r = u * 2 + 1; n   double L = (sum[u] - sum[r]); n   double R = (sum[u] - sum[l]); n   double ans1 = max(Max, R); n   double ans2 = max(Max, L); n   if ((double)sum[l] > Max) ans1 = max(ans1, dfs(h + 1, l, ans1)); n   if ((double)sum[r] > Max) ans2 = max(ans2, dfs(h + 1, r, ans2)); n   return (ans1 + ans2) * 0.5; n } n void solve() { n   char qry[3]; n   while (q--) { n     scanf( %s , &qry); n     if (qry[0] ==  a ) { n       int p, v; n       scanf( %d %d , &p, &v); n       add(p, v); n     } else { n       double res = dfs(0, 1, 0.0); n       printf( %.10lf n , res); n     } n   } n } n int main() { n   while (cin >> n >> q) { n     init(); n     solve(); n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int M = 220000; n vector<int> e1[M], e2[M]; n int p[M], dis[M]; n int main() { n   int n, m; n   cin >> n >> m; n   for (int i = 0; i < m; i++) { n     int u, v; n     cin >> u >> v; n     e1[u].push_back(v); n     e2[v].push_back(u); n   } n   int k; n   cin >> k; n   for (int i = 0; i < k; i++) cin >> p[i]; n   queue<int> q; n   q.push(p[k - 1]); n   dis[p[k - 1]] = 1; n   while (!q.empty()) { n     int u = q.front(); n     q.pop(); n     for (int v : e2[u]) { n       if (dis[v] == 0) { n         dis[v] = dis[u] + 1; n         q.push(v); n       } n     } n   } n   int mi = 0, mx = 0; n   for (int i = 0; i < k - 1; i++) { n     if (dis[p[i]] != dis[p[i + 1]] + 1) ++mi; n     for (int v : e1[p[i]]) { n       if (v != p[i + 1] && dis[p[i]] == dis[v] + 1) { n         ++mx; n         break; n       } n     } n   } n   cout << mi <<     << mx << endl; n   return 0; n } n


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


#include <bits/stdc++.h> n using namespace std; n int treeNo[111111]; n vector<vector<int> > BIT; n vector<pair<int, int> > E[111111]; n int in[111111]; n int base[111111]; n int P[111111], depth[111111]; n void update(int x, int val, int id) { n   for (; x < BIT[id].size(); x += (x & -x)) BIT[id][x] += val; n } n int read(int x, int id) { n   int sum = 0; n   for (; x; x -= (x & -x)) sum += BIT[id][x]; n   return sum; n } n void dfs(int first, int par, int d, int id) { n   P[first] = par; n   depth[first] = d; n   BIT[id].push_back(0); n   treeNo[first] = id; n   for (int i = 0; i < E[first].size(); i++) { n     int u = E[first][i].first; n     if (u == par) continue; n     dfs(u, first, d + 1, id); n     base[E[first][i].second] = u; n   } n } n int main() { n   int N; n   cin >> N; n   int u, first; n   for (int i = 1; i < N; i++) { n     cin >> u >> first; n     E[u].push_back({first, i}); n     E[first].push_back({u, i}); n     in[u]++; n     in[first]++; n   } n   int root = 0; n   for (int i = 1; i <= N; i++) n     if (in[i] > 2) root = i; n   if (root == 0) n     for (int i = 1; i <= N; i++) n       if (in[i] == 1) root = i; n   BIT.resize(E[root].size()); n   for (int i = 0; i < E[root].size(); i++) { n     BIT[i].push_back(0); n     dfs(E[root][i].first, root, 1, i); n     base[E[root][i].second] = E[root][i].first; n   } n   int Q, t; n   cin >> Q; n   while (Q--) { n     scanf( %d %d , &t, &first); n     if (t == 3) { n       scanf( %d , &u); n       if (u == root or first == root) { n         if (u == root) swap(u, first); n         int sum = read(depth[u], treeNo[u]); n         if (sum == 0) n           printf( %d n , depth[u]); n         else n           printf( -1 n ); n       } else if (treeNo[u] == treeNo[first]) { n         if (depth[u] == depth[first]) n           printf( 0 n ); n         else { n           if (depth[first] < depth[u]) swap(u, first); n           int sum = n               read(depth[first], treeNo[first]) - read(depth[u], treeNo[first]); n           if (sum == 0) n             printf( %d n , depth[first] - depth[u]); n           else n             printf( -1 n ); n         } n       } else { n         int sum = read(depth[u], treeNo[u]) + read(depth[first], treeNo[first]); n         if (sum == 0) n           printf( %d n , depth[first] + depth[u]); n         else n           printf( -1 n ); n       } n     } else if (t == 2) { n       int b = base[first]; n       update(depth[b], 1, treeNo[b]); n     } else if (t == 1) { n       int b = base[first]; n       update(depth[b], -1, treeNo[b]); n     } n   } n } n


#include <bits/stdc++.h> n long long exp(long long x, long long p) { n   long long ret = 1; n   while (p > 0) { n     if (p & 1) { n       ret = (ret * x) % 1000000007; n     } n     p >>= 1; n     x = (x * x) % 1000000007; n   } n   return ret; n } n signed main() { n   std::ios::sync_with_stdio(false); n   std::cin.tie(0); n   std::cout.tie(0); n   long long t; n   std::cin >> t; n   std::vector<long long> powers(2000000, 0); n   while (t--) { n     long long n, p; n     std::cin >> n >> p; n     long long arr[n]; n     for (long long i = 0; i < n; i++) { n       std::cin >> arr[i]; n     } n     std::sort(arr, arr + n); n     if (p == 1) { n       if (n % 2) n         std::cout << 1 << std::endl; n       else n         std::cout << 0 << std::endl; n       continue; n     } n     long long ans = 0; n     std::set<long long> pows; n     long long r = n - 1; n     long long cur = -1; n     while (r >= 0) { n       cur = arr[r]; n       r--; n       while (r >= 0) { n         powers[arr[r]]++; n         pows.insert(arr[r]); n         long long next = arr[r]; n         while (powers[next] == p) { n           powers[next] = 0; n           pows.erase(next); n           powers[next + 1]++; n           pows.insert(next + 1); n           next++; n         } n         if (powers[cur]) { n           powers[cur] = 0; n           cur = -1; n           pows.erase(cur); n           r--; n           break; n         } n         r--; n       } n     } n     while (!pows.empty()) { n       long long u = *pows.begin(); n       pows.erase(pows.begin()); n       ans = (ans + (exp(p, u) * powers[u]) % 1000000007) % 1000000007; n       powers[u] = 0; n     } n     if (cur != -1) { n       ans = (exp(p, cur) + 1000000007 - ans) % 1000000007; n     } n     std::cout << ans << std::endl; n   } n   return 0; n } n