#include <bits/stdc++.h> n using namespace std; n template <class T> n istream& operator>>(istream& is, vector<T>& v) { n   for (T& x : v) is >> x; n   return is; n } n template <class T> n ostream& operator<<(ostream& os, const vector<T>& v) { n   if (!v.empty()) { n     os << v.front(); n     for (int i = 1; i < v.size(); ++i) os <<     << v[i]; n   } n   return os; n } n const int N = 100010, L = 17; n int n, ns, nt, cnt; n int dist[N], ds[N], dt[N], ts[N], tt[N]; n pair<int, int> cs[N], ct[N], c[N], de[N], des[N], det[N]; n int ss[N], st[N]; n bool vis[N]; n vector<pair<int, int>> g[N]; n int prs[N][L], prt[N][L]; n bool tag; n void dfs(int u) { n   vis[u] = true; n   for (const auto& pr : g[u]) { n     int v, w; n     tie(v, w) = pr; n     if (!vis[v]) { n       dist[v] = dist[u] + w; n       dfs(v); n     } n   } n } n pair<int, int> decomp(int u) { n   if (tag) n     for (int i = 1; i < L; ++i) prt[u][i] = prt[prt[u][i - 1]][i - 1]; n   else n     for (int i = 1; i < L; ++i) prs[u][i] = prs[prs[u][i - 1]][i - 1]; n   pair<int, int> ret(0, u); n   vis[u] = true; n   for (const auto& pr : g[u]) { n     int v, w; n     tie(v, w) = pr; n     if (!vis[v]) { n       if (tag) n         prt[v][0] = u; n       else n         prs[v][0] = u; n       dist[v] = dist[u] + w; n       auto cur = decomp(v); n       cur.first += w; n       if (cur > ret) { n         if (ret.first) c[++cnt] = ret; n         ret = cur; n       } else n         c[++cnt] = cur; n     } n   } n   return de[u] = ret; n } n int s, t; n int query(int x, int y) { n   if (ds[x] > dt[x]) { n     if (y >= ns) return ss[ns]; n     if (ts[x] <= y) return ss[y]; n     int ret = ss[y] + ds[x] + des[x].first; n     for (int i = L - 1; i >= 0; --i) n       if (ts[prs[x][i]] > y) x = prs[x][i]; n     x = prs[x][0]; n     return ret - ds[x] - min(des[x].first, cs[y].first); n   } n   if (y >= nt) return st[nt]; n   if (tt[x] <= y) return st[y]; n   int ret = st[y] + dt[x] + det[x].first; n   for (int i = L - 1; i >= 0; --i) n     if (tt[prt[x][i]] > y) x = prt[x][i]; n   x = prs[x][0]; n   return ret - dt[x] - min(det[x].first, ct[y].first); n } n int main() { n   ios::sync_with_stdio(false); n   cin.tie(nullptr); n   int q; n   cin >> n >> q; n   for (int rep = 1; rep < n; ++rep) { n     int u, v, w; n     cin >> u >> v >> w; n     g[u].emplace_back(v, w); n     g[v].emplace_back(u, w); n   } n   dfs(1); n   s = max_element(dist + 1, dist + n + 1) - dist; n   memset(vis, 0, sizeof(vis)); n   dist[s] = 0; n   auto diam = decomp(s); n   t = diam.second; n   c[++cnt] = diam; n   sort(c + 1, c + cnt + 1, greater<pair<int, int>>()); n   copy(c + 1, c + cnt + 1, cs + 1); n   copy(dist + 1, dist + n + 1, ds + 1); n   copy(de + 1, de + n + 1, des + 1); n   ns = cnt; n   cnt = 0; n   memset(vis, 0, sizeof(vis)); n   dist[t] = 0; n   tag = true; n   diam = decomp(t); n   c[++cnt] = diam; n   sort(c + 1, c + cnt + 1, greater<pair<int, int>>()); n   copy(c + 1, c + cnt + 1, ct + 1); n   copy(dist + 1, dist + n + 1, dt + 1); n   copy(de + 1, de + n + 1, det + 1); n   nt = cnt; n   for (int i = 1; i <= ns; ++i) ss[i] = ss[i - 1] + cs[i].first; n   for (int i = 1; i <= nt; ++i) st[i] = st[i - 1] + ct[i].first; n   fill(ts + 1, ts + n + 1, -1); n   ts[s] = 0; n   for (int i = 1; i <= ns; ++i) { n     int u = cs[i].second; n     while (ts[u] == -1) { n       ts[u] = i; n       u = prs[u][0]; n     } n   } n   fill(tt + 1, tt + n + 1, -1); n   tt[t] = 0; n   for (int i = 1; i <= nt; ++i) { n     int u = ct[i].second; n     while (tt[u] == -1) { n       tt[u] = i; n       u = prt[u][0]; n     } n   } n   int ans = 0; n   while (q--) { n     int x, y; n     cin >> x >> y; n     x = (x + ans - 1) % n + 1; n     y = (y + ans - 1) % n + 1; n     cout << (ans = query(x, y * 2 - 1)) <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n char c[503][503]; n vector<pair<int, int> > v[503][503]; n int main() { n   int n, m, k; n   cin >> n >> m >> k; n   int s = 0; n   for (int i = 1; i <= n; i++) { n     for (int j = 1; j <= m; j++) { n       cin >> c[i][j]; n       if (c[i][j] ==  . ) s++; n     } n   } n   pair<int, int> st; n   for (int i = 1; i <= n; i++) { n     for (int j = 1; j <= m; j++) { n       if (c[i][j] ==  . ) { n         st.first = i; n         st.second = j; n         if (c[i - 1][j] == c[i][j]) { n           v[i][j].push_back(make_pair(i - 1, j)); n         } n         if (c[i + 1][j] == c[i][j]) { n           v[i][j].push_back(make_pair(i + 1, j)); n         } n         if (c[i][j - 1] == c[i][j]) { n           v[i][j].push_back(make_pair(i, j - 1)); n         } n         if (c[i][j + 1] == c[i][j]) { n           v[i][j].push_back(make_pair(i, j + 1)); n         } n       } n     } n   } n   queue<pair<int, int> > q; n   q.push(st); n   int num = s - k; n   if (num) { n     c[st.first][st.second] =  q ; n     num--; n   } n   while (!q.empty()) { n     pair<int, int> tp = q.front(); n     q.pop(); n     for (pair<int, int> kom : v[tp.first][tp.second]) { n       if (c[kom.first][kom.second] ==  .  && num > 0) { n         c[kom.first][kom.second] =  q ; n         num--; n         q.push(make_pair(kom.first, kom.second)); n       } n     } n   } n   for (int i = 1; i < n + 1; i++) { n     for (int j = 1; j <= m; j++) { n       if (c[i][j] ==  q ) n         cout <<  . ; n       else if (c[i][j] ==  .  && k) n         cout <<  X ; n       else n         cout << c[i][j]; n     } n     cout << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 32, M = 2e5 + 10; n int q[N], qz[M]; n bool insert(int x) { n   for (int i = N - 1; i >= 0; --i) { n     if ((x >> i) & 1) { n       if (!q[i]) { n         q[i] = x; n         return true; n       } else n         x ^= q[i]; n     } n   } n   return false; n } n int main() { n   int n; n   scanf( %d , &n); n   for (int i = 1; i <= n; ++i) scanf( %d , &qz[i]); n   for (int i = 1; i <= n; ++i) qz[i] ^= qz[i - 1]; n   int ans = 0; n   if (!qz[n]) return printf( -1 n ), 0; n   for (int i = n; i >= 1; --i) { n     if (insert(qz[i])) ans++; n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 100010; n int n, a[N], b[N], c[N]; n multiset<int> S; n multiset<int>::iterator it; n int main() { n   std::ios::sync_with_stdio(false); n   cin >> n; n   for (int i = 0; i < n; i++) cin >> a[i], c[a[i]] = i; n   for (int i = 0; i < n; i++) cin >> b[i]; n   for (int i = 0; i < n; i++) { n     int t = i - c[b[i]]; n     S.insert(t); n   } n   vector<int> r; n   for (int i = 0; i < n; i++) { n     int t = 1000000; n     it = S.lower_bound(i); n     if (it != S.end()) t = min(t, *(it)-i); n     if (it != S.begin()) t = min(t, i - *(--it)); n     r.push_back(t); n     S.erase(S.find(i - c[b[i]])); n     S.insert(n - c[b[i]] + i); n   } n   for (int i = 0; i < r.size(); i++) cout << r[i] << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   std::ios::sync_with_stdio(false); n   cin.tie(0); n   long long n, i, j; n   cin >> n; n   vector<long long> v(n / 2); n   for (i = 0; i < v.size(); i++) { n     cin >> v[i]; n   } n   sort(v.begin(), v.end()); n   long long mn = LLONG_MAX; n   j = 1; n   long long ans = 0; n   for (i = 0; i < v.size(); i++) { n     ans += abs(j - v[i]); n     j += 2; n   } n   mn = ans; n   reverse(v.begin(), v.end()); n   ans = 0; n   j = n; n   for (i = 0; i < v.size(); i++) { n     ans += abs(j - v[i]); n     j -= 2; n   } n   cout << min(mn, ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const unsigned long long hash1 = 201326611; n const double eps = 1e-8; n const long long INF = 0x3f3f3f3f3f3f3f3f; n const int inf = 0x3f3f3f3f; n const long long mod = 1e9 + 7; n const int N = 255; n const int M = 12; n const int dif = 26; n const double PI = acos(-1.0); n inline void Mod(long long& x) { n   if (x >= mod) x -= mod; n } n void BinaryBitset(int n) { cout << bitset<sizeof(int) * 4>(n) << endl; } n int n, m; n long long c[N][N], m0[N], m1[N], dp[N][N]; n void init(int n) { n   m0[0] = m1[0] = 1; n   for (int i = 1; i <= n; i++) { n     m0[i] = m0[i - 1] * m % mod; n     m1[i] = m1[i - 1] * (m - 1) % mod; n   } n   c[1][0] = 1; n   c[1][1] = 1; n   c[0][0] = 1; n   for (int i = 1; i <= n; i++) c[i][0] = 1, c[i][i] = 1; n   for (int i = 2; i <= n; i++) { n     for (int j = 1; j < n; j++) { n       c[i][j] = (c[i - 1][j - 1] + c[i - 1][j]) % mod; n     } n   } n } n int main() { n   scanf( %d%d , &n, &m); n   init(n); n   for (int i = 1; i <= n; i++) dp[1][i] = c[n][i] * m1[n - i] % mod; n   for (int i = 2; i <= n; i++) { n     for (int j = 0; j <= n; j++) { n       for (int k = 0; k <= j; k++) { n         dp[i][j] = (dp[i][j] + c[n - k][j - k] * dp[i - 1][k] % mod * n                                    m1[n - j] % mod * m0[k] % mod) % n                    mod; n         if (j == k) n           dp[i][j] = n               ((dp[i][j] - m1[n] * dp[i - 1][k] % mod + mod) % mod + mod) % mod; n       } n     } n   } n   printf( %lld n , dp[n][n]); n } n


#include <bits/stdc++.h> n #pragma optimization_level 3 n #pragma GCC optimize( Ofast ) n #pragma GCC target( avx,avx2,fma ) n using namespace std; n mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count()); n int getChar() { n   static char buf[1 << 16]; n   static int pos = 0, size = 0; n   if (pos == size) { n     pos = 0; n     size = (int)fread(buf, sizeof(buf[0]), sizeof(buf) / sizeof(buf[0]), stdin); n   } n   return buf[pos++]; n } n inline int ni() { n   char c = getChar(); n   for (; c ==   n  || c ==    ;) c = getChar(); n   int o = 0; n   for (; c !=     && c !=   n ; c = getChar()) o = o * 10 + c -  0 ; n   return o; n } n inline void putChar(int c) { n   static char buf[1 << 16]; n   static int pos = 0; n   if (c == EOF || pos == 1 << 16) { n     fwrite(buf, sizeof(buf[0]), pos, stdout); n     pos = 0; n   } n   if (c == EOF) return; n   buf[pos++] = c; n } n void printInt(int val) { n   char buf[7]; n   int pos = 0; n   if (val == 0) buf[pos++] = 48; n   for (; val; val /= 10) buf[pos++] = 48 + val % 10; n   for (pos--; pos >= 0;) putChar(buf[pos--]); n   putChar(  n ); n } n const int G = 400002; n int a, OPA = -1; n vector<int> l[G]; n int skok[G], o[G], zam[G], GR[G][2], mxd[G], mxp[G], dp[G][2], sz[G]; n int CALC(int v, int D) { n   mxd[v] = 1; n   sz[v] = 1; n   int c1 = 0, c2 = 0; n   for (int h : l[v]) { n     int x = CALC(h, D + 1); n     mxd[v] = max(mxd[v], x + 1); n     sz[v] += sz[h]; n     if (x >= c1) n       c2 = c1, c1 = x; n     else if (x > c2) n       c2 = x; n   } n   mxp[v] = c1 + c2 + 1; n   return mxd[v]; n } n int eba = 1; n void RIP_PAPA(int v, int p) { n   for (int q = 0; q < l[v].size(); q++) { n     if (l[v][q] == p) { n       l[v].erase(l[v].begin() + q); n       break; n     } n   } n   for (int h : l[v]) RIP_PAPA(h, v); n } n void rnm(int v, int p) { n   for (int h : l[v]) n     if (h != p) zam[h] = eba++; n   for (int h : l[v]) n     if (h != p) rnm(h, v); n } n int ST; n int GA(int LEN) { n   if (o[LEN] != -1) return o[LEN]; n   int d1, d2, w, rg; n   for (; ST >= 0 && mxp[ST] < OPA;) ST--; n   for (int q = ST; q >= 0; q--) { n     dp[q][0] = 0; n     d1 = d2 = 0, w = GR[q][0], rg = GR[q][1]; n     for (; w <= rg; w++) { n       dp[q][0] += dp[w][0]; n       if (dp[w][1] >= d1) n         d2 = d1, d1 = dp[w][1]; n       else n         d2 = max(d2, dp[w][1]); n     } n     if (d1 + d2 + 1 < LEN) n       dp[q][1] = d1 + 1; n     else n       dp[q][0]++, dp[q][1] = 0; n   } n   return o[LEN] = dp[0][0]; n } n int main() { n   cin.tie(0); n   cout.tie(0); n   cin.sync_with_stdio(0); n   cout.sync_with_stdio(0); n   ; n   memset(o, -1, sizeof(o)); n   a = ni(); n   o[1] = a; n   ; n   ST = a - 1; n   for (int q = 1; q <= a; q++) skok[a / q]++; n   for (int &i : skok) i /= 2; n   vector<pair<int, int> > reb(a - 1); n   for (int q = 0; q < a - 1; q++) { n     int x = ni() - 1, y = ni() - 1; n     reb[q] = {x, y}; n     l[x].push_back(y); n     l[y].push_back(x); n   } n   zam[0] = 0; n   rnm(0, -1); n   for (int q = 0; q < a; q++) l[q].clear(); n   for (pair<int, int> p : reb) { n     l[zam[p.first]].push_back(zam[p.second]); n     l[zam[p.second]].push_back(zam[p.first]); n   } n   RIP_PAPA(0, -1); n   for (int q = 0; q < a; q++) { n     GR[q][0] = l[q].size() ? l[q][0] : a; n     GR[q][1] = l[q].size() ? l[q].back() : q; n   } n   CALC(0, 1); n   int diam = 0; n   for (int &i : mxp) diam = max(i, diam); n   o[diam] = 1; n   for (int q = diam + 1; q <= a; q++) o[q] = 0; n   int H = min(a - 1, (int)(sqrt(diam))), U = 1, I = 2; n   for (; I <= H; I++) { n     o[I] = GA(I); n     OPA = I / 2; n   } n   int tyt; n   for (; I <= a;) { n     OPA = I / 2; n     tyt = GA(I); n     int lf = I, rg = min(a + 1, lf + skok[tyt] + 1); n     if (tyt == 0) break; n     if (tyt == 1) lf = diam, rg = diam + 1; n     for (; lf + 1 < rg;) { n       int md = (lf + rg) / 2; n       if (GA(md) == tyt) n         lf = md; n       else n         rg = md; n     } n     for (; I < rg; I++) o[I] = tyt; n   } n   for (int q = 1; q <= a; q++) { n     printInt(max(0, o[q])); n   } n   putChar(EOF); n } n


#include <bits/stdc++.h> n using namespace std; n long long n, m, s, e, v, q; n vector<long long> vs, ve; n int32_t main() { n   ios::sync_with_stdio(0), cin.tie(0); n   cin >> n >> m >> s >> e >> v; n   vs.resize(s), ve.resize(e); n   for (auto& u : vs) { n     cin >> u; n   } n   for (auto& u : ve) { n     cin >> u; n   } n   cin >> q; n   while (q--) { n     long long x, y, a, b; n     cin >> x >> y >> a >> b; n     if (x == a) { n       cout << abs(y - b) <<   n ; n     } else { n       long long ans = 1e18, idx, es = (abs(x - a) + v - 1) / v; n       if (y > b) swap(y, b); n       idx = lower_bound(vs.begin(), vs.end(), y) - vs.begin(); n       if (idx < s) { n         ans = min(ans, abs(vs[idx] - y) + abs(vs[idx] - b) + abs(x - a)); n       } n       idx--; n       if (idx >= 0) { n         ans = min(ans, abs(vs[idx] - y) + abs(vs[idx] - b) + abs(x - a)); n       } n       idx = lower_bound(ve.begin(), ve.end(), y) - ve.begin(); n       if (idx < e) { n         ans = min(ans, abs(ve[idx] - y) + abs(ve[idx] - b) + es); n       } n       idx--; n       if (idx >= 0) { n         ans = min(ans, abs(ve[idx] - y) + abs(ve[idx] - b) + es); n       } n       cout << ans <<   n ; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long w[1000000], w1[100000]; n long long t[1000000]; n pair<long long, long long> ans; n long long a, b, c, n, m, q, k, x, c1, b1; n long long poww(long long x, long long y) { n   if (x == 0) return 0; n   if (x == 1) return 1; n   if (y == 0) return 1; n   if (y == 1) return x; n   long long d = poww(x, y / 2); n   if (y % 2) return d * d * x; n   return d * d; n } n map<long long, long long> M; n vector<long long> v; n int main() { n   cin >> a >> b >> c; n   for (int i = 1; i <= 1000000; i++) { n     a *= 10; n     if (a / b == c) return cout << i, 0; n     a %= b; n   } n   cout << -1; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   string s =  ROYGBIV ; n   long long int n, i; n   cin >> n; n   for (i = 0; i < n - 3; i++) { n     cout << s[i % 4]; n   } n   cout <<  BIV ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, s; n   cin >> n >> s; n   int sum = 100; n   bool res = false; n   for (int i = 0; i < n; i++) { n     int temp1, temp2; n     cin >> temp1 >> temp2; n     if (temp1 < s) { n       res = true; n       if (temp2 != 0 && temp2 < sum) sum = temp2; n     } n     if (temp1 == s && temp2 == 0) res = true; n   } n   if (res) n     cout << 100 - sum; n   else n     cout << -1; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int dx[8] = {0, -1, 0, 1, 1, -1, -1, 1}; n const int dy[8] = {-1, 0, 1, 0, -1, -1, 1, 1}; n const long long mod = 1000000007; n const int base = 311; n const int N = 6e5 + 5; n map<long long, vector<pair<int, int>>> e; n int n, m; n long long a[N]; n vector<int> dsk[N]; n long long pw[N]; n int dd[N]; n int k; n void dfs(int u, int &d) { n   dd[u] = 1; n   ++d; n   for (int v : dsk[u]) n     if (!dd[v]) dfs(v, d); n } n void gogo() { n   cin >> n >> m >> k; n   for (int i = 1; i <= n; ++i) cin >> a[i]; n   for (int i = 1; i <= m; ++i) { n     int u, v; n     cin >> u >> v; n     e[a[u] ^ a[v]].push_back(make_pair(u, v)); n   } n   pw[0] = 1; n   for (int i = 1; i <= 6e5; ++i) pw[i] = pw[i - 1] * 2 % mod; n   long long ans = pw[k] * pw[n] % mod; n   for (auto it : e) { n     vector<pair<int, int>> tmp = it.second; n     for (pair<int, int> i : tmp) { n       dsk[i.first].push_back(i.second); n       dsk[i.second].push_back(i.first); n     } n     vector<int> cnt; n     for (pair<int, int> i : tmp) { n       int d = 0; n       if (!dd[i.first]) { n         dfs(i.first, d); n         cnt.push_back(d); n       } n     } n     int sum = 0; n     for (int i : cnt) sum += i; n     long long comp = n - sum + ((int)(cnt).size()); n     ans = (ans - pw[n] + pw[comp] + mod * mod) % mod; n     for (pair<int, int> i : tmp) { n       dd[i.first] = dd[i.second] = 0; n       dsk[i.first].clear(); n       dsk[i.second].clear(); n     } n   } n   cout << ans; n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   if (fopen( sol  n              .inp , n              r )) { n     freopen( n          sol  n          .inp , n          r , stdin); n     freopen( n          sol  n          .out , n          w , stdout); n   } n   gogo(); n } n


#include <bits/stdc++.h> n using namespace std; n int n; n long long ans, X, Y, s[200010], w[30], t, A[131072], B[131072]; n inline long long gcd(long long a, long long b) { n   for (long long c; b; a = b, b = c) c = a % b; n   return a; n } n int main() { n   scanf( %d%lld%lld , &n, &X, &Y); n   for (int i = 1; i <= n; ++i) scanf( %lld , s + i); n   if (Y % X) return 0 & puts( 0 ); n   long long D = Y; n   for (int i = 2; D > 1 && i <= 1000000; ++i) n     if (D % i == 0) n       for (w[++t] = i; D % i == 0; D /= i) n         ; n   if (D > 1) { n     long long Q = sqrt(D); n     if (Q * Q == D) n       w[++t] = Q; n     else n       for (int i = 1; i <= n; ++i) { n         if ((Q = gcd(D, s[i])) > 1 && Q < D) { n           w[++t] = Q; n           w[++t] = D / Q; n           break; n         } n       } n     w[++t] = D; n   } n   int a[30] = {0}, b[30] = {0}, T; n   for (int i = 1; i <= t; ++i) { n     for (D = X; D % w[i] == 0; D /= w[i]) ++a[i]; n     for (D = Y; D % w[i] == 0; D /= w[i]) ++b[i]; n   } n   for (int i = 1; i <= n; ++i) n     if (Y % s[i] == 0) { n       int c[30] = {0}, k = 0; n       D = s[i]; n       for (int j = 1; j <= t; ++j) n         if (D % w[j] == 0) n           for (; D % w[j] == 0; ++c[j]) D /= w[j]; n       for (int j = 1; j <= t; ++j) n         if (c[j] == b[j]) k ^= (1 << j - 1); n       ++B[k]; n     } n   int N = 1 << t; n   for (int i = 1; i <= N; i <<= 1) n     for (int j = i; j < N; ++j& i ? 0 : j |= i) B[j ^ i] += B[j]; n   for (int i = 1; i <= n; ++i) n     if (s[i] % X == 0) { n       int c[30] = {0}, k = (1 << t) - 1; n       D = s[i]; n       for (int j = 1; j <= t; ++j) n         if (D % w[j] == 0) n           for (; D % w[j] == 0; ++c[j]) D /= w[j]; n       for (int j = 1; j <= t; ++j) n         if (c[j] == a[j] || a[j] == b[j]) k ^= (1 << j - 1); n       ans += B[k]; n     } n   printf( %lld n , ans); n } n


#include <bits/stdc++.h> n using namespace std; n double x[1005], y[1005], z[1005], r[1005]; n long long int n, m; n int main() { n   double res = (1ll << (long long int)(60)), ax, ay, az, vx, vy, vz, R; n   cin >> ax >> ay >> az >> vx >> vy >> vz >> R >> n; n   for (long long int i = 0; i < ((long long int)(n)); i++) { n     cin >> x[0] >> y[0] >> z[0] >> r[0] >> m; n     for (long long int j = 0; j < ((long long int)(m)); j++) n       cin >> x[j + 1] >> y[j + 1] >> z[j + 1]; n     for (long long int j = 0; j < ((long long int)(m)); j++) n       x[j + 1] += x[0], y[j + 1] += y[0], z[j + 1] += z[0]; n     for (long long int j = 0; j < ((long long int)(m + 1)); j++) { n       double t = -(vx * (ax - x[j]) + vy * (ay - y[j]) + vz * (az - z[j])) / n                  ((vx) * (vx) + (vy) * (vy) + (vz) * (vz)); n       if (t < 0) continue; n       if (((vx * t + ax - x[j]) * (vx * t + ax - x[j]) + n            (vy * t + ay - y[j]) * (vy * t + ay - y[j]) + n            (vz * t + az - z[j]) * (vz * t + az - z[j])) < n           (R + r[j]) * (R + r[j]) + 0.0000001) { n         double low = 0, high = t; n         for (long long int k = 0; k < ((long long int)(100)); k++) { n           double mid = (low + high) / 2.0; n           if (((vx * mid + ax - x[j]) * (vx * mid + ax - x[j]) + n                (vy * mid + ay - y[j]) * (vy * mid + ay - y[j]) + n                (vz * mid + az - z[j]) * (vz * mid + az - z[j])) < n               (R + r[j]) * (R + r[j])) n             high = mid; n           else n             low = mid; n         } n         res = min(res, low); n       } n     } n   } n   if (res == (1ll << (long long int)(60))) n     cout <<  -1  << endl; n   else n     printf( %0.20lf n , res); n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e6 + 20; n vector<int> a[N]; n vector<int> Q; n int t, x, p, k; n int solve(int n, int p) { n   int ret = 0; n   Q.clear(); n   Q.push_back(-1); n   vector<int>& f = a[p]; n   for (int i = 0; i < (int)f.size(); ++i) { n     int top = Q.size(); n     for (int j = 0; j < top; ++j) { n       Q.push_back(-1 * f[i] * Q[j]); n     } n   } n   for (int i = 1; i < (int)Q.size(); ++i) { n     ret += n / Q[i]; n   } n   return n - ret; n } n int main() { n   for (int i = 2; i <= 1000000; ++i) { n     if (a[i].size() > 0) continue; n     for (int j = i; j <= 1000000; j += i) { n       a[j].push_back(i); n     } n   } n   scanf( %d , &t); n   while (t--) { n     scanf( %d %d %d , &x, &p, &k); n     k += solve(x, p); n     int lb = 1, ub = 1e9, ans = x; n     while (lb <= ub) { n       int mid = (lb + ub) / 2; n       if (solve(mid, p) >= k) { n         ans = mid; n         ub = mid - 1; n       } else { n         lb = mid + 1; n       } n     } n     printf( %d n , ans); n   } n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T> n inline bool chkmin(T &a, T b) { n   return b < a ? a = b, 1 : 0; n } n template <typename T> n inline bool chkmax(T &a, T b) { n   return b > a ? a = b, 1 : 0; n } n inline int read() { n   int x(0), sgn(1); n   char ch(getchar()); n   for (; !isdigit(ch); ch = getchar()) n     if (ch ==  - ) sgn = -1; n   for (; isdigit(ch); ch = getchar()) x = (x * 10) + (ch ^ 48); n   return x * sgn; n } n void File() {} n int n, m; n const int inf = 0x3f3f3f3f; n template <int Maxn, int Maxm> n struct Dinic { n   int Head[Maxn], Next[Maxm], cap[Maxm], to[Maxm], e; n   Dinic() { e = 1; } n   inline void add_edge(int u, int v, int flow) { n     to[++e] = v; n     Next[e] = Head[u]; n     Head[u] = e; n     cap[e] = flow; n   } n   inline void Add(int u, int v, int flow) { n     add_edge(u, v, flow); n     add_edge(v, u, 0); n   } n   int S, T, dis[Maxn]; n   bool Bfs() { n     queue<int> Q; n     Q.push(S); n     memset(dis, 0, sizeof(dis)); n     dis[S] = 1; n     while (!Q.empty()) { n       int u = Q.front(); n       Q.pop(); n       for (int i = Head[u], v = to[i]; i; v = to[i = Next[i]]) n         if (cap[i] && !dis[v]) dis[v] = dis[u] + 1, Q.push(v); n     } n     return dis[T]; n   } n   int cur[Maxn]; n   int Dfs(int u, int flow) { n     if (u == T || !flow) return flow; n     int res = 0, f; n     for (int &i = cur[u]; i; i = Next[i]) { n       int v = to[i]; n       if (dis[v] == dis[u] + 1 && (f = Dfs(v, min(flow, cap[i])))) { n         cap[i] -= f, cap[i ^ 1] += f, res += f; n         if (!(flow -= f)) break; n       } n     } n     if (!res) dis[u] = 0; n     return res; n   } n   int Run() { n     int sum_flow = 0; n     while (Bfs()) memcpy(cur, Head, sizeof(cur)), sum_flow += Dfs(S, inf); n     return sum_flow; n   } n }; n const int N = 5050; n Dinic<(int)1e5, (int)4e5> D; n int tot; n int ans[N], Bef; n template <int Maxn> n struct Segment_Tree { n   int id[Maxn]; n   void Build(int o, int l, int r) { n     if (l == r) { n       id[o] = l; n       return; n     } n     id[o] = ++tot; n     int mid = (l + r) >> 1; n     Build(o << 1, l, mid); n     Build(o << 1 | 1, mid + 1, r); n     D.Add(id[o], id[o << 1], id[o << 1] <= m ? 1 : inf); n     D.Add(id[o], id[o << 1 | 1], id[o << 1 | 1] <= m ? 1 : inf); n   } n   inline void Connect(int o, int l, int r, int ql, int qr, int Id) { n     if (ql <= l && r <= qr) { n       D.Add(Id, id[o], 2); n       return; n     } n     int mid = (l + r) >> 1; n     if (ql <= mid) Connect(o << 1, l, mid, ql, qr, Id); n     if (qr > mid) Connect(o << 1 | 1, mid + 1, r, ql, qr, Id); n   } n   vector<int> find(int o, int l, int r) { n     if (l == r) return vector<int>(); n     vector<int> tmp; n     int mid = (l + r) >> 1; n     vector<int> ls = find(o << 1, l, mid), rs = find(o << 1 | 1, mid + 1, r); n     set_union((ls).begin(), (ls).end(), (rs).begin(), (rs).end(), n               inserter(tmp, tmp.end())); n     for (int i = D.Head[id[o]], v = D.to[i]; i; v = D.to[i = D.Next[i]]) n       if (v <= m && !D.cap[i]) tmp.push_back(v); n     for (int i = D.Head[id[o]], v = D.to[i]; i; v = D.to[i = D.Next[i]]) n       if (D.cap[i] && v > Bef) { n         int cur = tmp[tmp.size() - 1]; n         tmp.pop_back(); n         ans[cur] = v - Bef; n       } n     return tmp; n   } n }; n Segment_Tree<N << 2> T; n vector<int> V1, V2, V3; n int Ref[N], from[N], go[N]; n void Get_Ans() { n   for (int u : V1) { n     for (int i = D.Head[Ref[u]], v = D.to[i]; i; v = D.to[i = D.Next[i]]) n       if (v <= m && D.cap[i ^ 1]) ans[v] = u; n   } n   T.find(1, 1, m); n   for (int u : V3) { n     int flow = D.cap[from[u]]; n     for (int i = D.Head[Ref[u]], v = D.to[i]; i; v = D.to[i = D.Next[i]]) n       if (v != D.S && !D.cap[i]) ans[v] = u; n     if (flow == 1) n       for (int i = D.Head[Ref[u]], v = D.to[i]; i; v = D.to[i = D.Next[i]]) n         if (v != D.S && D.cap[i]) { n           ans[v] = u; n           break; n         } n   } n } n int main() { n   File(); n   n = read(), m = read(); n   tot = m; n   T.Build(1, 1, m); n   Bef = tot; n   D.S = tot + n + 1; n   D.T = tot + n + 2; n   for (register int i = (1), iend = (int)(m); i <= iend; ++i) n     D.Add(i, D.T, 1), go[i] = D.e - 1; n   for (register int i = (1), iend = (int)(n); i <= iend; ++i) { n     int opt = read(); n     D.Add(D.S, Ref[i] = ++tot, 1 + (opt == 2)); n     from[i] = D.e - 1; n     if (opt == 0) { n       int k = read(); n       V1.push_back(i); n       while (k--) D.Add(tot, read(), 1); n     } else if (opt == 1) { n       int l = read(), r = read(); n       V1.push_back(i); n       T.Connect(1, 1, m, l, r, tot); n     } else { n       int a = read(), b = read(), c = read(); n       V3.push_back(i); n       D.Add(tot, a, 1); n       D.Add(tot, b, 1); n       D.Add(tot, c, 1); n     } n   } n   printf( %d n , D.Run()); n   Get_Ans(); n   for (register int i = (1), iend = (int)(m); i <= iend; ++i) n     if (ans[i]) printf( %d %d n , ans[i], i); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const double PI = 2 * acos(0.0); n void solve(void) { n   string str; n   cin >> str; n   int one = 0, zero = 0; n   for (int i = 0; i < str.length(); i++) { n     if (str[i] ==  1 ) { n       one++; n       while (i < str.length() && str[i] ==  1 ) i++; n       i--; n     } else { n       zero++; n       while (i < str.length() && str[i] ==  0 ) i++; n       i--; n     } n   } n   if (one && zero) { n     if (zero <= 1) n       cout << 1 <<   n ; n     else n       cout << 2 <<   n ; n   } else if (zero) n     cout << 1 <<   n ; n   else if (one) n     cout << 0 <<   n ; n } n int main(void) { n   ios::sync_with_stdio(0), cin.tie(0); n   int t; n   cin >> t; n   while (t--) { n     solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T> n inline string tostring(const T &x) { n   ostringstream os; n   os << x; n   return os.str(); n } n inline int toint(const string &s) { n   istringstream is(s); n   int x; n   is >> x; n   return x; n } n inline int todecimal(string s) { n   int a = 0; n   for (int i = 0; i < s.size(); i++) a = 2 * a + (s[i] -  0 ); n   return a; n } n inline string tobinary(int a) { n   string s; n   while (a != 0) { n     s = (char)(a % 2 +  0 ) + s; n     a >>= 1; n   } n   return s; n } n template <typename T> n inline T sqr(T x) { n   return x * x; n } n template <typename T> n T gcd(T a, T b) { n   return (b == 0) ? abs(a) : gcd(b, a % b); n } n inline int isvowel(char c) { n   if (c ==  a  || c ==  e  || c ==  i  || c ==  o  || c ==  u ) return 1; n   return 0; n } n inline int isprime(int a) { n   for (int i = 2; i * i <= a; i++) n     if (!(a % i)) return 0; n   return 1; n } n inline void inp(int &n) { n   n = 0; n   int ch = getchar(); n   int sign = 1; n   while (ch <  0  || ch >  9 ) { n     if (ch ==  - ) sign = -1; n     ch = getchar(); n   } n   while (ch >=  0  && ch <=  9 ) n     n = (n << 3) + (n << 1) + ch -  0 , ch = getchar(); n   n = n * sign; n } n const int dx4[4] = {0, 0, 1, -1}; n const int dy4[4] = {1, -1, 0, 0}; n const int dx8[8] = {0, 0, 1, 1, 1, -1, -1, -1}; n const int dy8[8] = {1, -1, 0, 1, -1, 1, 0, -1}; n class node { n  public: n   int x, y, z; n   node(int a, int b, int c) { n     x = a; n     y = b; n     z = c; n   } n }; n bool operator<(const node &a, const node &b) { return a.x > b.x; } n bool compare(const node &a, const node &b) { return a.x < b.x; } n const int maxn = 66; n int grid[maxn][maxn]; n int n; n void solve() { n   memset((grid), (0), sizeof(grid)); n   grid[0][0] = n; n   int moving = 1; n   while (moving) { n     moving = 0; n     for (int i = 0; i < maxn; i++) { n       for (int j = 0; j < maxn; j++) { n         int x = grid[i][j] / 4; n         grid[i + 1][j] += x; n         grid[i][j + 1] += x; n         if (i) grid[i - 1][j] += x * (i == 1 ? 2 : 1); n         if (j) grid[i][j - 1] += x * (j == 1 ? 2 : 1); n         grid[i][j] %= 4; n         if (x) moving = 1; n       } n     } n   } n   int q = 0; n   ; n   cin >> q; n   int x, y; n   for (int i = 0; i < q; i++) { n     cin >> x >> y; n     if (x < 0) x = -x; n     if (y < 0) y = -y; n     if (x >= maxn || y >= maxn) n       cout << 0 << endl; n     else n       cout << grid[x][y] << endl; n   } n } n void input() { cin >> n; } n int main() { n   int testcase = 1; n   for (int i = 0; i < testcase; i++) { n     input(); n     solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n #pragma GCC optimize( inline,Ofast , 3) n #pragma GCC optimize(2) n #pragma GCC optimize(3) n #pragma GCC optimize( Ofast ) n #pragma GCC optimize( inline ) n #pragma GCC optimize( -fgcse ) n #pragma GCC optimize( -fgcse-lm ) n #pragma GCC optimize( -fipa-sra ) n #pragma GCC optimize( -ftree-pre ) n #pragma GCC optimize( -ftree-vrp ) n #pragma GCC optimize( -fpeephole2 ) n #pragma GCC optimize( -ffast-math ) n #pragma GCC optimize( -fsched-spec ) n #pragma GCC optimize( unroll-loops ) n #pragma GCC optimize( -falign-jumps ) n #pragma GCC optimize( -falign-loops ) n #pragma GCC optimize( -falign-labels ) n #pragma GCC optimize( -fdevirtualize ) n #pragma GCC optimize( -fcaller-saves ) n #pragma GCC optimize( -fcrossjumping ) n #pragma GCC optimize( -fthread-jumps ) n #pragma GCC optimize( -funroll-loops ) n #pragma GCC optimize( -fwhole-program ) n #pragma GCC optimize( -freorder-blocks ) n #pragma GCC optimize( -fschedule-insns ) n #pragma GCC optimize( inline-functions ) n #pragma GCC optimize( -fschedule-insns2 ) n #pragma GCC optimize( -fstrict-aliasing ) n #pragma GCC optimize( -fstrict-overflow ) n #pragma GCC optimize( -falign-functions ) n #pragma GCC optimize( -fcse-skip-blocks ) n #pragma GCC optimize( -fcse-follow-jumps ) n #pragma GCC optimize( -fsched-interblock ) n #pragma GCC optimize( -fpartial-inlining ) n #pragma GCC optimize( no-stack-protector ) n #pragma GCC optimize( -freorder-functions ) n #pragma GCC optimize( -findirect-inlining ) n #pragma GCC optimize( inline-small-functions ) n #pragma GCC optimize( -finline-small-functions ) n #pragma GCC optimize( -ftree-switch-conversion ) n #pragma GCC optimize( -foptimize-sibling-calls ) n #pragma GCC optimize( -fexpensive-optimizations ) n #pragma GCC optimize( -funsafe-loop-optimizations ) n #pragma GCC optimize( inline-functions-called-once ) n #pragma GCC optimize( -fdelete-null-pointer-checks ) n using namespace std; n long long n, m, q, fa[1000010], ans, l, r; n struct lujing { n   long long u, v, c, id; n } f[1000010]; n inline long long read() { n   long long w = 0, e = 1; n   char ch = getchar(); n   while (!isdigit(ch)) { n     e = ch ==  -  ? -1 : 1; n     ch = getchar(); n   } n   while (isdigit(ch)) { n     w = (w << 3) + (w << 1) + (ch ^ 48); n     ch = getchar(); n   } n   return w * e; n } n long long find(long long x) { return (fa[x] == x) ? x : (fa[x] = find(fa[x])); } n void add(long long x, long long y) { n   long long xx = find(x), yy = find(y); n   if (xx != yy) fa[xx] = yy; n } n bool pd(long long x, long long y) { n   long long xx = find(x), yy = find(y); n   if (xx == yy) return true; n   return false; n } n bool cmp(lujing x, lujing y) { return x.c > y.c; } n int main() { n   n = read(); n   m = read(); n   q = read(); n   for (register int i = 1; i <= m; ++i) n     f[i].u = read(), f[i].v = read(), f[i].c = read(), f[i].id = i; n   sort(f + 1, f + 1 + m, cmp); n   while (q--) { n     l = read(), r = read(); n     ans = -1; n     for (register int i = 1; i <= n * 2; ++i) fa[i] = i; n     for (register int i = 1; i <= m; ++i) { n       if (f[i].id >= l && f[i].id <= r) { n         if (pd(f[i].u, f[i].v)) { n           ans = f[i].c; n           break; n         } else { n           add(f[i].u + n, f[i].v); n           add(f[i].u, f[i].v + n); n         } n       } n     } n     cout << ans << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n int main() { n   int n, v, pre, rotting, a[4005], b[4005], i, j, bag, collect, day, v2; n   while (scanf( %d%d , &n, &v) != EOF) { n     day = 0; n     bag = 0; n     for (i = 0; i < n; i++) { n       scanf( %d%d , &a[i], &b[i]); n       if (a[i] > day) day = a[i]; n     } n     rotting = 0; n     day += 2; n     for (i = 1; i < day; i++) { n       pre = 0; n       for (j = 0; j < n; j++) { n         if (a[j] == i) { n           pre += b[j]; n         } n       } n       if (rotting > v) { n         bag += v; n         v2 = 0; n       } else { n         bag += rotting; n         v2 = v - rotting; n       } n       if (pre > v2) { n         bag += v2; n         pre -= v2; n       } else { n         bag += pre; n         pre = 0; n       } n       rotting = pre; n     } n     printf( %d n , bag); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n string doen() { n   char c; n   string waarde; n   while (cin.get(c) && c !=   n ) { n     waarde += c; n   } n   int aantal = stoi(waarde); n   string iets; n   getline(cin, iets); n   for (int i = 0; i < iets.length(); i = i + 2) { n     if (iets[i] ==  1 ) { n       return  HARD ; n     } n   } n   return  EASY ; n } n int main() { n   cout << doen(); n   return 0; n } n


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


#include <bits/stdc++.h> n struct { n   inline operator int() { n     int x; n     return scanf( %d , &x), x; n   } n   inline operator long long() { n     long long x; n     return scanf( %lld , &x), x; n   } n   template <class T> n   inline void operator()(T &x) { n     x = *this; n   } n   template <class T, class... A> n   inline void operator()(T &x, A &...a) { n     x = *this; n     this->operator()(a...); n   } n } read; n const int maxn = 300005; n int l[maxn], r[maxn]; n int main() { n   int T = read; n   while (T--) { n     int n = read; n     std::fill(l, l + n + 1, 0); n     std::fill(r, r + n + 1, 0); n     for (int i = 1; i <= n; i++) { n       int x = read; n       if (!l[x]) l[x] = i; n       r[x] = i; n     } n     int ans = 0, max = 0; n     for (int x = 1, y = 0, now = 0; x <= n; x++) n       if (l[x]) { n         if (r[y] < l[x]) n           ++now; n         else n           now = 1; n         ++ans; n         max = std::max(max, now); n         y = x; n       } n     ans -= max; n     printf( %d n , ans); n   } n } n


#include <bits/stdc++.h> n using namespace std; n vector<int> E[100005]; n int head, tail, A[300], B[300]; n int N, M, H, T, marked[100005]; n bool ok; n void checkHydra(int x) { n   int heads = 0; n   for (int i = 0; i < int(E[x].size()); ++i) marked[E[x][i]] = 1, ++heads; n   if (heads >= H) { n     for (vector<int>::iterator it = E[x].begin(); it != E[x].end() && !ok; n          ++it) { n       --heads; n       int tails = 0; n       int common = 0; n       for (vector<int>::iterator jt = E[*it].begin(); jt != E[*it].end() && !ok; n            ++jt) { n         if (*jt == x) continue; n         ++tails; n         if (marked[*jt]) ++common; n         if (heads >= H && tails >= T && heads + tails - common >= H + T) { n           ok = true; n           head = x; n           tail = *it; n         } n       } n       ++heads; n     } n   } n   for (int i = 0; i < int(E[x].size()); ++i) marked[E[x][i]] = 0; n } n int main() { n   cin >> N >> M >> H >> T; n   for (int i = 0; i < M; ++i) { n     int x, y; n     cin >> x >> y; n     E[x].push_back(y); n     E[y].push_back(x); n   } n   ok = false; n   for (int i = 1; i <= N && !ok; ++i) checkHydra(i); n   if (!ok) { n     cout <<  NO ; n     return 0; n   } n   cout <<  YES n ; n   cout << head <<     << tail <<   n ; n   for (auto &it : E[head]) n     if (it != tail) marked[it]++; n   for (auto &it : E[tail]) n     if (it != head) marked[it]++; n   N = M = 0; n   for (auto &it : E[head]) n     if (marked[it] == 1) n       if (N < H) { n         A[N++] = it; n         marked[it] = 0; n       } n   for (auto &it : E[tail]) n     if (marked[it] == 1) n       if (M < T) { n         B[M++] = it; n         marked[it] = 0; n       } n   for (auto &it : E[head]) n     if (marked[it] == 2) n       if (N < H) { n         A[N++] = it; n         marked[it] = 0; n       } n   for (auto &it : E[tail]) n     if (marked[it] == 2) n       if (M < T) { n         B[M++] = it; n         marked[it] = 0; n       } n   for (int i = 0; i < H; ++i) cout << A[i] <<    ; n   cout <<   n ; n   for (int i = 0; i < T; ++i) cout << B[i] <<    ; n   cout <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   string start; n   string end; n   cin >> start; n   cin >> end; n   int startInt = (stoi(start.substr(0, 3))) * 60 + (stoi(start.substr(3, 5))); n   int endInt = (stoi(end.substr(0, 3))) * 60 + (stoi(end.substr(3, 5))); n   int dif = (endInt - startInt) / 2; n   int hoursToAdd = dif / 60; n   int minutesToAdd = dif % 60; n   int first = (stoi(start.substr(0, 3)) + hoursToAdd); n   int second = (stoi(start.substr(3, 5)) + minutesToAdd); n   first += second / 60; n   second = second % 60; n   string firstS = to_string(first); n   string secondS = to_string(second); n   if (firstS.size() == 1) firstS.insert(0,  0 ); n   if (secondS.size() == 1) secondS.insert(0,  0 ); n   cout << firstS <<  :  << secondS <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n int v[100005]; n int st[100005][22]; n bool mark[100005]; n long long dp[100005]; n int n, k; n void build() { n   int i, j; n   for (j = 1; 1 << j <= n; j++) { n     for (i = 0; i + (1 << j) <= n; i++) { n       st[i][j] = min(st[i][j - 1], st[i + (1 << (j - 1))][j - 1]); n     } n   } n } n int getMin(int l, int r) { n   int num = r - l + 1, k = 0; n   while (num >= 2) num /= 2, k++; n   return min(st[l][k], st[r - (1 << k) + 1][k]); n } n long long f(int i) { n   if (i >= n) return 0; n   if (mark[i]) return dp[i]; n   mark[i] = 1; n   long long ret = getMin(i - k + 1, i) + f(i + k); n   ret = max(ret, f(i + 1)); n   return dp[i] = ret; n } n int main() { n   scanf( %d %d , &n, &k); n   long long sum = 0; n   for (int i = 0; i < n; i++) { n     scanf( %d , &st[i][0]); n     sum += st[i][0]; n   } n   build(); n   printf( %lld n , sum - f(k - 1)); n   return 0; n } n


#include <bits/stdc++.h> n #pragma GCC optimize( O3 ,  unroll-loops ) n #pragma GCC target( avx2 ) n using namespace std; n template <class T, class U> n inline void checkmin(T &x, U y) { n   if (y < x) x = y; n } n template <class T, class U> n inline void checkmax(T &x, U y) { n   if (y > x) x = y; n } n template <class T, class U> n inline bool ifmax(T &x, U y) { n   if (y > x) return x = y, true; n   return false; n } n template <class T, class U> n inline bool ifmin(T &x, U y) { n   if (y < x) return x = y, true; n   return false; n } n template <class T> n inline void sort(T &a) { n   sort(a.begin(), a.end()); n } n template <class T> n inline void rsort(T &a) { n   sort(a.rbegin(), a.rend()); n } n template <class T> n inline void reverse(T &a) { n   reverse(a.begin(), a.end()); n } n template <class T, class U> n inline istream &operator>>(istream &str, pair<T, U> &p) { n   return str >> p.first >> p.second; n } n template <class T> n inline istream &operator>>(istream &str, vector<T> &a) { n   for (auto &i : a) str >> i; n   return str; n } n template <class T> n inline T sorted(T a) { n   sort(a); n   return a; n } n void read(int &x) { n   static char c; n   c = getchar(); n   if (c ==  - ) { n     x = 0; n     while ((c = getchar()) >=  0 ) x = (x << 3) + (x << 1) + (c -  0 ); n     x = -x; n   } else { n     x = c -  0 ; n     while ((c = getchar()) >=  0 ) x = (x << 3) + (x << 1) + (c -  0 ); n   } n } n void print(int x) { n   if (x >= 10) print(x / 10); n   putchar(x % 10 +  0 ); n } n const int N = 1e6 + 1; n int a[N], ind[N]; n bool used[N]; n void solve() { n   int n; n   read(n); n   for (int i = 0; i < n; ++i) read(a[i]), used[i] = 0; n   int v = 0, pind = 0; n   while (!used[v]) { n     used[v] = true; n     ind[pind++] = v; n     v -= a[v]; n   } n   bool flag = false; n   for (int i = 0;; ++i) n     if (ind[i] == v) { n       print(pind - i); n       putchar(  n ); n       for (int j = i; j < pind; ++j) print(ind[j] + 1), putchar(   ); n       putchar(  n ); n       return; n     } n } n signed main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   cout << fixed << setprecision(12); n   srand(94385); n   int t; n   read(t); n   while (t--) solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, a[300005], s[300005], X, Y; n long long ans; n int main() { n   scanf( %d , &n); n   for (int i = 1; i <= n; i++) { n     long long x; n     scanf( %lld , &x); n     for (long long j = 0; j < 60; j++) n       if (x >> j & 1) a[i]++; n     s[i] = s[i - 1] + a[i]; n   } n   X++; n   for (int i = 1; i <= n; i++) { n     if (s[i] & 1) { n       ans += Y; n       Y++; n     } else { n       ans += X; n       X++; n     } n   } n   for (int i = 1; i <= n; i++) { n     X = a[i]; n     for (int j = i; j <= min(n, i + 60 - 1); j++) { n       X = max(X, a[j]); n       if ((s[j] - s[i - 1]) % 2 == 0 && (s[j] - s[i - 1]) < X + X) ans--; n     } n   } n   printf( %lld n , ans); n   return 0; n } n


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


#include <bits/stdc++.h> n using namespace std; n long n, m, h, ts, calc[2000]; n double p2, p1; n double gc(long n, long m) { n   if (m > n) return 0; n   double res = 0; n   for (int i = n; i > n - m; i--) n     res = res + log(i + .0), res = res - log(.0 + n - i + 1); n   return exp(res); n } n int main() { n   ios_base::sync_with_stdio(0); n   cin >> n >> m >> h; n   for (int i = 1; i <= m; i++) { n     cin >> calc[i]; n     ts += calc[i]; n   } n   if (ts < n) { n     cout << -1 << endl; n     return 0; n   } n   if (n == 1) { n     cout << 0 << endl; n     return 0; n   } n   if (calc[h] == 1) { n     cout << 0 << endl; n     return 0; n   } n   --n; n   --calc[h]; n   --ts; n   double ans = 1.0; n   for (int i = 1; i <= n; i++) n     ans = ans * (ts - calc[h] - i + 1) / (ts - i + 1); n   cout << fixed << 1.0 - ans << endl; n   cin.get(); n   cin.get(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n; n vector<int> a, b, g, ar; n vector<vector<int> > edg, nxt; n vector<long long> d; n long long f(int v) { n   long long res = 0; n   for (int w : edg[v]) { n     long long tmp = f(w) + a[w]; n     if (tmp > 0) { n       res += tmp; n       nxt[w].push_back(v); n       g[v]++; n     } else { n       nxt[v].push_back(w); n       g[w]++; n     } n   } n   return d[v] = res; n } n int main(void) { n   cin >> n; n   a.assign(n, 0); n   b.assign(n, 0); n   g.assign(n, 0); n   edg.assign(n, vector<int>()); n   nxt.assign(n, vector<int>()); n   for (int i = 0; i < n; i++) cin >> a[i]; n   vector<int> c; n   for (int i = 0; i < n; i++) { n     cin >> b[i]; n     if (b[i] == -1) { n       c.push_back(i); n       continue; n     } n     b[i]--; n     edg[b[i]].push_back(i); n   } n   long long ans = 0; n   d.assign(n, 0); n   for (int i = 0; i < n; i++) { n     if (b[i] == -1) f(i); n   } n   for (int i = 0; i < n; i++) { n     ans += d[i] + a[i]; n   } n   queue<int> q; n   for (int i = 0; i < n; i++) { n     if (g[i] == 0) q.push(i); n   } n   while (!q.empty()) { n     int v = q.front(); n     q.pop(); n     ar.push_back(v); n     for (int w : nxt[v]) { n       g[w]--; n       if (g[w] == 0) q.push(w); n     } n   } n   cout << ans << endl; n   for (int v : ar) cout << v + 1 <<    ; n   cout << endl; n } n


#include <bits/stdc++.h> n using namespace std; n const long long INF = 1e9; n long long gcd(long long a, long long b) { return (a ? gcd(b % a, a) : b); } n long long power(long long a, long long n) { n   long long p = 1; n   while (n > 0) { n     if (n % 2) { n       p = p * a; n     } n     n >>= 1; n     a *= a; n   } n   return p; n } n int main() { n   ios_base::sync_with_stdio(false); n   string s, t; n   int n, m, p, a, b, c, d, e, x, mx = -INF; n   cin >> n; n   for (int(i) = 0; (i) < (n); (i)++) { n     cin >> s >> p >> m >> a >> b >> c >> d >> e; n     x = 100 * p - 50 * m + a + b + c + d + e; n     if (mx < x) { n       t = s; n       mx = x; n     } n   } n   cout << t; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int a; n   string s; n   cin >> a; n   for (int i = 1; i <= a + 5; i++) { n     stringstream sa; n     string ssa; n     sa << i; n     sa >> ssa; n     s.append(ssa); n   } n   cout << s[a - 1]; n   return 0; n } n


#include <bits/stdc++.h> n  n using namespace std; n  n bool cutPaper(int n, int x, int y) n { n     int count = 1; n  n     while ( x % 2 == 0 || y % 2 == 0 ) n     {    n         if ( x % 2 == 0 ){ n             x /= 2; n             count *= 2;  n         }else if ( y % 2 == 0 )  { n             y /= 2;  n             count *= 2;  n         } n  n         if ( count >= n ) n             return true;    n     } n  n     if ( count >= n ) n             return true;    n  n     return false; n      n } n int main() n { n  n     int t; n     cin >> t; n  n     while (t-- > 0) n     { n         int n, x, y; n  n         cin >> x >> y >> n; n  n         if (cutPaper(n, x, y)) n         { n             cout <<  YES  << endl; n         } n         else n         { n             cout <<  NO  << endl; n         } n     } n }


#include <bits/stdc++.h> n using namespace std; n int a[3509]; n int n, t, m, k; n deque<int> d; n vector<int> v; n int main() { n   scanf( %d , &t); n   for (int i = 0; i < t; i++) { n     scanf( %d%d%d , &n, &m, &k); n     for (int j = 0; j < n; j++) scanf( %d , &a[j]); n     int g = n - m + 1; n     v.clear(); n     for (int j = g - 1; j < n; j++) v.push_back(max(a[j - g + 1], a[j])); n     int vlen = v.size(); n     int g2 = vlen - k; n     g2 = max(1, g2); n     d.clear(); n     for (int i = 0; i < g2; i++) { n       if (!d.empty()) { n         while (!d.empty() && d.back() > v[i]) d.pop_back(); n       } n       d.push_back(v[i]); n     } n     int ans = d.front(); n     for (int i = g2; i < vlen; i++) { n       if (!d.empty() && d.front() == v[i - g2]) { n         d.pop_front(); n       } n       while (!d.empty() && d.back() > v[i]) d.pop_back(); n       d.push_back(v[i]); n       ans = max(ans, d.front()); n     } n     cout << ans <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1000004; n int a[N], arr[N], nxt[N], big[N], len[N]; n int main() { n   int n; n   scanf( %d , &n); n   int i, idx; n   int mx1 = 0, mx2 = -1; n   for (i = 0; i < n; i++) { n     scanf( %d , &a[i]); n     if (a[i] > mx1) { n       mx2 = mx1; n       mx1 = a[i]; n       idx = i; n     } else if (a[i] > mx2) n       mx2 = a[i]; n   } n   int id = idx; n   for (i = 1; i <= n; i++) { n     arr[i] = a[id]; n     id = (id + 1) % n; n   } n   arr[n + 1] = 1000000007; n   stack<int> st; n   st.push(n + 1); n   for (i = n; i >= 1; i--) { n     while (!st.empty() && arr[st.top()] < arr[i]) st.pop(); n     nxt[i] = st.top(); n     st.push(i); n   } n   while (!st.empty()) st.pop(); n   st.push(n + 1); n   for (i = n; i >= 1; i--) { n     while (!st.empty() && arr[st.top()] <= arr[i]) st.pop(); n     big[i] = st.top(); n     st.push(i); n   } n   len[n + 1] = 1; n   for (i = n; i >= 1; i--) len[i] = len[nxt[i]] + 1; n   long long ans = 0; n   for (i = 1; i <= n; i++) ans = ans + (len[i + 1] - len[big[i]] + 1); n   for (i = 1; i <= n; i++) { n     if (arr[i] == mx1 || arr[i] == mx2) ans--; n   } n   printf( %I64d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n const int maxn = 5e5 + 5; n std::vector<std::pair<int, int>> G[maxn]; n std::vector<std::tuple<int, int, int>> q[maxn]; n int in[maxn], out[maxn], id[maxn], tot, n, m; n long long dis[maxn], ans[maxn]; n struct SegTree { n   long long sum[maxn << 2], lazy[maxn << 2]; n   void pushdown(int o) { n     if (lazy[o]) { n       sum[o << 1] += lazy[o]; n       sum[o << 1 | 1] += lazy[o]; n       lazy[o << 1] += lazy[o]; n       lazy[o << 1 | 1] += lazy[o]; n       lazy[o] = 0; n     } n   } n   void build(int o, int l, int r) { n     if (l == r) { n       sum[o] = dis[l]; n       return; n     } n     int m = (l + r) >> 1; n     build(o << 1, l, m); n     build(o << 1 | 1, m + 1, r); n     sum[o] = std::min(sum[o << 1], sum[o << 1 | 1]); n   } n   void update(int o, int l, int r, int ql, int qr, int val) { n     if (ql <= l && r <= qr) { n       sum[o] += val; n       lazy[o] += val; n       return; n     } n     int m = (l + r) >> 1; n     pushdown(o); n     if (ql <= m) update(o << 1, l, m, ql, qr, val); n     if (qr > m) update(o << 1 | 1, m + 1, r, ql, qr, val); n     sum[o] = std::min(sum[o << 1], sum[o << 1 | 1]); n   } n   long long query(int o, int l, int r, int ql, int qr) { n     if (ql <= l && r <= qr) { n       return sum[o]; n     } n     int m = (l + r) >> 1; n     long long ans = 0x3f3f3f3f3f3f3f3f; n     pushdown(o); n     if (ql <= m) ans = std::min(ans, query(o << 1, l, m, ql, qr)); n     if (qr > m) ans = std::min(ans, query(o << 1 | 1, m + 1, r, ql, qr)); n     return ans; n   } n } st; n void dfs1(int u, long long d) { n   in[u] = ++tot; n   for (auto &it : G[u]) dfs1(it.first, it.second + d); n   if (G[u].empty()) n     dis[u] = d; n   else n     dis[u] = 0x3f3f3f3f3f3f3f3f; n   out[u] = tot; n } n void dfs2(int u, long long d) { n   for (auto &it : q[u]) n     ans[std::get<2>(it)] = n         st.query(1, 1, n, std::get<0>(it), std::get<1>(it)) + d; n   for (auto &it : G[u]) { n     st.update(1, 1, n, in[it.first], out[it.first], -2 * it.second); n     dfs2(it.first, it.second + d); n     st.update(1, 1, n, in[it.first], out[it.first], 2 * it.second); n   } n } n int main() { n   scanf( %d%d , &n, &m); n   for (int i = 2; i <= n; i++) { n     int u, w; n     scanf( %d%d , &u, &w); n     G[u].emplace_back(i, w); n   } n   dfs1(1, 0); n   st.build(1, 1, n); n   for (int i = 1; i <= m; i++) { n     int v, l, r; n     scanf( %d%d%d , &v, &l, &r); n     q[v].emplace_back(l, r, i); n   } n   dfs2(1, 0); n   for (int i = 1; i <= m; i++) printf( %lld n , ans[i]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <class T> n bool umin(T& a, T b) { n   if (a > b) { n     a = b; n     return 1; n   } n   return 0; n } n template <class T> n bool umax(T& a, T b) { n   if (a < b) { n     a = b; n     return 1; n   } n   return 0; n } n int p[100009]; n int main() { n   int n; n   scanf( %d , &n); n   for (int i = 1; i <= n; i++) p[i] = i; n   if (n & 1) { n     for (int i = n; i > 1; i -= 2) swap(p[i], p[i - 1]); n   } else { n     for (int i = 1; i < n; i += 2) swap(p[i], p[i + 1]); n   } n   for (int i = 1; i <= n; i++) printf( %d  , p[i]); n   puts(  ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 300010, maxlg = 18; n struct edge { n   int to, nxt; n   long long val; n }; n edge G[maxn << 1], G2[maxn]; n int head[maxn], cnt; n int head2[maxn], cnt2; n struct node { n   int u; n   long long d; n   inline bool operator<(const node &rhs) const { return d > rhs.d; } n }; n priority_queue<node> PQ; n long long dis[maxn]; n bool vis[maxn]; n int deg[maxn]; n edge T[maxn]; n int headT[maxn], cntT; n queue<int> Q; n int lg[maxn]; n int anc[maxn][maxlg], dep[maxn]; n inline int lca(int, int); n void dfs(const int &); n int siz[maxn]; n int main() { n   int n, m, s; n   scanf( %d %d %d , &n, &m, &s); n   int u, v, w; n   while (m--) { n     scanf( %d %d %d , &u, &v, &w); n     G[++cnt] = {v, head[u], w}, head[u] = cnt; n     G[++cnt] = {u, head[v], w}, head[v] = cnt; n   } n   memset(dis, 0x3f, sizeof(dis)); n   dis[s] = 0, PQ.push({s, 0}); n   while (!PQ.empty()) { n     u = PQ.top().u, PQ.pop(); n     if (vis[u]) continue; n     vis[u] = true; n     for (int i = head[u]; i; i = G[i].nxt) n       if (dis[G[i].to] > dis[u] + G[i].val) { n         dis[G[i].to] = dis[u] + G[i].val; n         PQ.push({G[i].to, dis[G[i].to]}); n       } n   } n   for (int i = 1; i <= n; i++) { n     anc[i][0] = -1; n     for (int j = head[i]; j; j = G[j].nxt) n       if (dis[G[j].to] == dis[i] + G[j].val) n         G2[++cnt2] = {G[j].to, head2[i]}, head2[i] = cnt2, deg[G[j].to]++; n   } n   for (int i = 2; i <= n; i++) lg[i] = lg[i >> 1] + 1; n   Q.push(s); n   while (!Q.empty()) { n     u = Q.front(), Q.pop(); n     if (u != s) { n       dep[u] = dep[anc[u][0]] + 1; n       T[++cntT] = {u, headT[anc[u][0]], 0}, headT[anc[u][0]] = cntT; n       for (int i = 1; i <= lg[dep[u]]; i++) n         anc[u][i] = anc[anc[u][i - 1]][i - 1]; n     } n     for (int i = head2[u]; i; i = G2[i].nxt) n       if (deg[G2[i].to]) { n         anc[G2[i].to][0] = lca(anc[G2[i].to][0], u); n         if (!--deg[G2[i].to]) Q.push(G2[i].to); n       } n   } n   dfs(s); n   int ans = 0; n   for (int i = 1; i <= n; i++) n     if (i != s && siz[i] > ans) ans = siz[i]; n   printf( %d n , ans); n   return 0; n } n inline int lca(int u, int v) { n   if (u == -1) return v; n   if (dep[u] < dep[v]) u ^= v ^= u ^= v; n   while (dep[u] != dep[v]) u = anc[u][lg[dep[u] - dep[v]]]; n   if (u == v) return u; n   for (int i = lg[dep[u]]; i >= 0; i--) n     if (anc[u][i] != anc[v][i]) u = anc[u][i], v = anc[v][i]; n   return anc[u][0]; n } n void dfs(const int &u) { n   siz[u] = 1; n   for (int i = headT[u]; i; i = T[i].nxt) dfs(T[i].to), siz[u] += siz[T[i].to]; n   return; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   cin >> n; n   vector<int> v1, v2; n   for (int i = 0; i < n; i++) { n     int a; n     cin >> a; n     if (a) v1.push_back(a); n   } n   for (int i = 0; i < n; i++) { n     int a; n     cin >> a; n     if (a) v2.push_back(a); n   } n   for (int i = 0; i < n - 1; i++) { n     if (v1[0] == v2[i]) { n       rotate(v2.begin(), v2.begin() + i, v2.end()); n       break; n     } n   } n   for (int i = 0; i < n - 1; i++) { n     if (v1[i] != v2[i]) { n       cout <<  NO ; n       return 0; n     } n   } n   cout <<  YES ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long k, m; n   string s; n   vector<int> rooms; n   cin >> k >> m; n   cin >> s; n   for (int i = 0; i < s.length(); i++) { n     if (s[i] ==  0 ) { n       rooms.push_back(i); n     } n   } n   long select = k; n   k = 0; n   while (k + m < rooms.size()) { n     for (int i = k; i <= k + m; i++) { n       if (max(rooms[i] - rooms[k], rooms[k + m] - rooms[i]) < select) n         select = max(rooms[i] - rooms[k], rooms[k + m] - rooms[i]); n     } n     k++; n   } n   cout << select; n } n


#include <bits/stdc++.h> n using namespace std; n const int sieve_size = 15000002; n bool sieve[sieve_size + 1]; n vector<long long> prime; n void Sieve() { n   prime.push_back(2); n   for (int i = 3; i * i <= sieve_size; i += 2) { n     if (!sieve[i]) { n       long long p = i * 2; n       for (int j = i * i; j <= sieve_size; j += p) { n         sieve[j] = true; n       } n     } n   } n   for (int i = 3; i <= sieve_size; i += 2) { n     if (!sieve[i]) prime.push_back(i); n   } n } n int dx[] = {1, 0, -1, 0}, dy[] = {0, 1, 0, -1}; n inline int GA(long long n, vector<long long>& vec) { n   for (int i = 0; i < n; ++i) { n     long long x; n     cin >> x; n     vec.push_back(x); n   } n   return 0; n } n inline int GAEG(long long m, vector<long long> vec[]) { n   for (int i = 0; i < m; ++i) { n     long long x, y; n     cin >> x >> y; n     x--, y--; n     vec[x].push_back(y); n     vec[y].push_back(x); n   } n   return 0; n } n bool isvalid(long long xx, long long yy, long long n, long long m) { n   return (xx >= 0 && xx < n && yy >= 0 && yy < m); n } n long long pw(long long x, long long y) { n   if (y == 0) return 1; n   long long o = y / 2; n   long long f = pw(x, o); n   if (y % 2) { n     return (((f * f)) * x); n   } else n     return (f * f); n } n string s[60]; n int main() { n   long long t; n   cin >> t; n   while (t--) { n     long long n, m; n     cin >> n >> m; n     for (int i = 0; i < n; ++i) cin >> s[i]; n     long long ans = 4; n     bool bl = false; n     for (int i = 0; i < n; ++i) { n       int p = s[i].find( A ); n       if (p != -1) { n         bl = true; n         break; n       } n     } n     if (!bl) { n       cout <<  MORTAL  << endl; n     } else { n       if (s[0].find( A ) != -1 || s[n - 1].find( A ) != -1) ans = 3; n       for (int i = 0; i < n; ++i) { n         if (s[i][0] ==  A  || s[i][m - 1] ==  A ) { n           ans = 3; n           break; n         } n       } n       if (s[0][0] ==  A  || s[n - 1][0] ==  A  || s[n - 1][m - 1] ==  A  || n           s[0][m - 1] ==  A ) n         ans = 2; n       for (int i = 0; i < n; ++i) { n         bool qq = false; n         for (int j = 0; j < m; ++j) { n           if (s[i][j] !=  A ) { n             qq = true; n             break; n           } n         } n         if (!qq) { n           if (i == 0 || i == n - 1) n             ans = 1; n           else n             ans = min(ans, 2ll); n         } n       } n       bool isP = false; n       for (int i = 0; i < m; ++i) { n         bool qq = false; n         for (int j = 0; j < n; ++j) { n           if (s[j][i] !=  A ) { n             qq = true; n             isP = true; n             break; n           } n         } n         if (!qq) { n           if (i == 0 || i == m - 1) n             ans = 1; n           else n             ans = min(ans, 2ll); n         } n       } n       if (!isP) n         cout << 0 << endl; n       else n         cout << ans << endl; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n namespace IO { n inline char gc() { n   static const int Rlen = 1 << 22 | 1; n   static char buf[Rlen], *p1, *p2; n   return (p1 == p2) && (p2 = (p1 = buf) + fread(buf, 1, Rlen, stdin), p1 == p2) n              ? EOF n              : *p1++; n } n template <typename T> n T get_integer() { n   char c; n   bool f = false; n   while (!isdigit(c = gc())) f = c ==  - ; n   T x = c ^ 48; n   while (isdigit(c = gc())) x = ((x + (x << 2)) << 1) + (c ^ 48); n   return f ? -x : x; n } n inline int gi() { return get_integer<int>(); } n }  // namespace IO n using namespace IO; n using std::cerr; n using std::cout; n using pii = std::pair<int, int>; n const int N = 1.5e5 + 7; n int n, m, k; n long long ans; n std::vector<int> G[N]; n std::vector<pii> Q[N]; n int fa[N], in[N], out[N]; n int sz[N], son[N], top[N]; n int d[N], ps[N], dfc; n void dfs1(int u, int p) { n   fa[u] = p, d[u] = d[p] + 1; n   for (int v : G[u]) n     if (v != p) { n       dfs1(v, u); n       sz[u] += sz[v]; n       if (sz[v] > sz[son[u]]) son[u] = v; n     } n   ++sz[u]; n } n void dfs2(int u, int tp) { n   top[u] = tp; n   ps[in[u] = ++dfc] = u; n   if (son[u]) dfs2(son[u], tp); n   for (int v : G[u]) n     if (v != fa[u] && v != son[u]) dfs2(v, v); n   out[u] = dfc; n } n int LCA(int u, int v) { n   while (top[u] != top[v]) n     d[top[u]] < d[top[v]] ? v = fa[top[v]] : u = fa[top[u]]; n   return d[u] < d[v] ? u : v; n } n int jump(int u, int k) { n   while (k > d[u] - d[top[u]]) { n     k -= d[u] - d[top[u]] + 1; n     u = fa[top[u]]; n   } n   return ps[in[u] - k]; n } n namespace SGT { n const int N = ::N * 40; n int lc[N], rc[N], sm[N], ct; n void ins(int &u, int l, int r, int p) { n   if (!u) u = ++ct; n   ++sm[u]; n   if (l == r) return; n   int m = (l + r) >> 1; n   p <= m ? ins(lc[u], l, m, p) : ins(rc[u], m + 1, r, p); n } n int qy(int u, int l, int r, int ql, int qr) { n   if ((ql <= l && r <= qr) || !u) return sm[u]; n   int m = (l + r) >> 1; n   if (qr <= m) return qy(lc[u], l, m, ql, qr); n   if (m < ql) return qy(rc[u], m + 1, r, ql, qr); n   return qy(lc[u], l, m, ql, qr) + qy(rc[u], m + 1, r, ql, qr); n } n void merge(int &u, int v) { n   if (!u || !v) { n     u |= v; n     return; n   } n   merge(lc[u], lc[v]); n   merge(rc[u], rc[v]); n   sm[u] += sm[v]; n } n void ins(int &u, int p) { ins(u, 1, n, p); } n int qy(int u, int l, int r) { return qy(u, 1, n, l, r); } n void clear() { n   while (ct) lc[ct] = rc[ct] = sm[ct] = 0, --ct; n } n }  // namespace SGT n namespace BIT { n int tr[N]; n void add(int l, int r) { n   for (int register p = l; p <= n; p += p & -p) ++tr[p]; n   for (int register p = r + 1; p <= n; p += p & -p) --tr[p]; n } n int qy(int p) { n   int r = 0; n   for (; p; p &= p - 1) r += tr[p]; n   return r; n } n }  // namespace BIT n int rt[N]; n std::vector<int> q[N]; n void solve_q(int u, int Rt) { n   rt[u] = 0; n   int od = std::max(0, k - d[u] + d[Rt]); n   for (int v : q[u]) { n     if (d[v] >= od + d[Rt]) { n       int p = jump(v, d[v] - d[Rt] - od); n       ans += SGT::qy(rt[u], in[p], out[p]); n     } n     SGT::ins(rt[u], in[v]); n   } n   for (int w : G[u]) n     if (Rt != u || w != son[u]) { n       solve_q(w, Rt); n       if (q[w].size() > q[u].size()) q[u].swap(q[w]), std::swap(rt[u], rt[w]); n       for (int v : q[w]) { n         if (d[v] >= od + d[Rt]) { n           int p = jump(v, d[v] - d[Rt] - od); n           ans += SGT::qy(rt[u], in[p], out[p]); n         } n         q[u].push_back(v); n       } n       q[w].clear(); n       SGT::merge(rt[u], rt[w]); n     } n } n void solve(int u) { n   for (int v : G[u]) solve(v); n   for (auto &t : Q[u]) ans += BIT::qy(in[t.second]); n   for (auto &t : Q[u]) n     if (d[t.second] >= d[u] + k) { n       int p = jump(t.second, d[t.second] - d[u] - k); n       BIT::add(in[p], out[p]); n     } n   for (auto &t : Q[u]) ans += BIT::qy(in[t.first]); n   for (auto &t : Q[u]) n     if (d[t.first] >= d[u] + k) { n       int p = jump(t.first, d[t.first] - d[u] - k); n       BIT::add(in[p], out[p]); n     } n   for (auto &t : Q[u]) q[t.first].push_back(t.second); n   solve_q(u, u); n   q[u].clear(); n   SGT::clear(); n } n void Main() { n   n = gi(), m = gi(), k = gi(); n   for (int register i = 1; i < n; ++i) { n     int u = gi(), v = gi(); n     G[u].push_back(v); n     G[v].push_back(u); n   } n   dfs1(1, 0); n   dfs2(1, 1); n   for (int register i = 2; i <= n; ++i) n     G[i].erase(std::find(G[i].begin(), G[i].end(), fa[i])); n   for (int register i = 1; i <= m; ++i) { n     int u = gi(), v = gi(); n     if (in[u] > in[v]) std::swap(u, v); n     if (in[v] > out[u]) std::swap(u, v); n     Q[LCA(u, v)].push_back({u, v}); n   } n   solve(1); n   cout << ans <<   n ; n } n inline void file() {} n signed main() { n   file(); n   Main(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long int n, q; n   cin >> n >> q; n   long long int ans[q]; n   for (long long int i = 0; i < q; i++) { n     long long int x, y, k = 0, cto = 0; n     ; n     cin >> x >> y; n     if (n % 2 == 0) { n       k = (n / 2) * (x - 1) + ((y / 2) + (y % 2)); n       if ((x + y) % 2 == 1) { n         k += (n * n) / 2; n       } n     } n     if (n % 2 == 1) { n       if (x % 2 == 0) { n         cto = (x / 2); n       } n       if (x % 2 == 1) { n         cto = (x - 1) / 2; n       } n       k = (n / 2) * (x - 1) + cto + ((y / 2) + (y % 2)); n       if ((x + y) % 2 == 1) { n         k += (n * n) / 2; n         if (x % 2 == 1) { n           k++; n         } n       } n     } n     ans[i] = k; n   } n   for (long long int i = 0; i < q; i++) { n     cout << ans[i] << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n long long arr[200022]; n long long b[200022]; n vector<vector<long long> > vect(4 * 200022); n void merge(long long i, long long a, long long j, long long b1, long long pos) { n   long long s = i; n   long long e = b1; n   while (i <= a && j <= b1) { n     if (b[i] < b[j]) { n       vect[pos].push_back(b[i++]); n     } else n       vect[pos].push_back(b[j++]); n   } n   while (i <= a) vect[pos].push_back(b[i++]); n   while (j <= b1) vect[pos].push_back(b[j++]); n   long long k = 0; n   for (; s <= e; s++) b[s] = vect[pos][k++]; n   return; n } n void mergesort(long long i, long long j, long long pos) { n   if (i < j) { n     long long mid = i + j; n     mid /= 2; n     mergesort(i, mid, pos * 2 + 1); n     mergesort(mid + 1, j, pos * 2 + 2); n     merge(i, mid, mid + 1, j, pos); n   } else if (i == j) { n     vect[pos].push_back(b[i]); n   } n } n long long query(long long s, long long e, long long st, long long ed, n                 long long pos, long long ele) { n   if (s > ed || e < st) n     return 0; n   else if (s <= st && e >= ed) { n     return vect[pos].end() - n            lower_bound((vect[pos]).begin(), (vect[pos]).end(), ele); n   } else { n     return query(s, e, st, (st + ed) / 2, pos * 2 + 1, ele) + n            query(s, e, ((st + ed) / 2) + 1, ed, pos * 2 + 2, ele); n   } n } n int main() { n   long long n; n   cin >> n; n   for (long long i = 0; i < n; i++) { n     cin >> arr[i]; n     arr[i] = min(arr[i], n); n     b[i] = arr[i]; n   } n   mergesort(0, n - 1, 0); n   long long ans = 0; n   for (long long i = n; i >= 1; i--) { n     long long end = min(i, arr[i - 1]); n     if (end == i) end--; n     ans += query(0, end - 1, 0, n - 1, 0, i); n   } n   cout << ans <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n int dfs(int n) {} n int main() { n   int n, k, x, y, a[200200], b[200200]; n   cin >> n >> k; n   vector<vector<int> > v(n, vector<int>()); n   for (int i = 0; i < n; i++) { n     cin >> a[i]; n     b[i] = a[i]; n   } n   for (int i = 0; i < k; i++) { n     cin >> x >> y; n     v[x - 1].push_back(y - 1); n     v[y - 1].push_back(x - 1); n   } n   sort(b, b + n); n   for (int i = 0; i < n; i++) { n     int x = i, res = lower_bound(b, b + n, a[i]) - b; n     for (int j = 0; j < v[i].size(); j++) n       if (a[v[i][j]] < a[i]) res--; n     cout << res <<    ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 1000; n const int MAX = 1000; n const int inf = 999999999; n const int p1 = 104393321, p2 = 104393329; n int a[MAX], adj[MAX][MAX], ans = inf; n int main() { n   ios::sync_with_stdio(0); n   int n, m; n   cin >> n >> m; n   for (int i = (0); i < int(n); ++i) cin >> a[i]; n   for (int i = (0); i < int(m); ++i) { n     int x, y; n     cin >> x >> y; n     x--; n     y--; n     adj[x][y] = 1; n     adj[y][x] = 1; n   } n   for (int i = (0); i < int(n); ++i) { n     for (int j = (i + 1); j < int(n); ++j) { n       for (int t = (j + 1); t < int(n); ++t) { n         if (adj[i][j] && adj[j][t] && adj[i][t]) n           ans = min(ans, a[i] + a[t] + a[j]); n       } n     } n   } n   if (ans == inf) n     cout << -1 << endl; n   else n     cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, cnt = 0; n map<int, int> mp; n int main() { n   cin >> n; n   mp[n]++; n   cnt++; n   while (n) { n     n++; n     while (!(n % 10)) n /= 10; n     if (mp[n]) break; n     mp[n]++; n     cnt++; n   } n   cout << cnt << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <class T> n int getbit(int i, T X) { n   return (X & (1 << (i - 1))); n } n template <class T> n T onbit(int i, T X) { n   return (X | (1 << (i - 1))); n } n template <class T> n T offbit(int i, T X) { n   return (X | (1 << (i - 1)) - (1 << (i - 1))); n } n template <class T> n T sqr(T x) { n   return (x * x); n } n template <class T> n T cube(T x) { n   return (x * x * x); n } n template <class T> n T gcd(T a, T b) { n   T r; n   while (b != 0) { n     r = a % b; n     a = b; n     b = r; n   } n   return a; n } n template <class T> n T lcm(T a, T b) { n   return a / gcd(a, b) * b; n } n int csx[4] = {0, 0, -1, 1}; n int csy[4] = {-1, 1, 0, 0}; n const int MOD = 1000000007; n const int infi = 1e9; n const int maxn = 300; n const int maxv = 800; n struct Dinic { n   int n, s, t, E, adj[maxv + 5], next[maxv + 5], last[maxn + 5], run[maxv + 5], n       level[maxv + 5], que[maxv + 5], pre[maxv + 5]; n   long long flow[maxv + 5], cap[maxv + 5]; n   void init(int _n, int _s, int _t) { n     n = _n; n     s = _s; n     t = _t; n     E = 0; n     for (typeof(n) i = 0; i <= n; i++) last[i] = -1; n   } n   void add(int u, int v, long long c1, long long c2) { n     adj[E] = v; n     flow[E] = 0; n     cap[E] = c1; n     pre[E] = u; n     next[E] = last[u]; n     last[u] = E++; n     adj[E] = u; n     flow[E] = 0; n     cap[E] = c2; n     pre[E] = v; n     next[E] = last[v]; n     last[v] = E++; n   } n   bool bfs() { n     for (typeof(n) i = 0; i <= n; i++) level[i] = -1; n     level[s] = 0; n     int qsize = 0; n     que[qsize++] = s; n     for (typeof(qsize) i = 0; i < qsize; i++) { n       for (int u = que[i], e = last[u]; e != -1; e = next[e]) { n         int v = adj[e]; n         if (flow[e] < cap[e] && level[v] == -1) { n           level[v] = level[u] + 1; n           que[qsize++] = v; n         } n       } n     } n     return level[t] != -1; n   } n   long long dfs(int u, long long bot) { n     if (u == t) return bot; n     for (int &e = run[u]; e != -1; e = next[e]) { n       int v = adj[e]; n       long long delta = 0; n       if (level[v] == level[u] + 1 && flow[e] < cap[e] && n           (delta = dfs(v, min(bot, cap[e] - flow[e] * 1ll))) > 0) { n         flow[e] += delta; n         flow[e ^ 1] -= delta; n         return delta; n       } n     } n     return 0; n   } n   long long maxFlow() { n     long long total = 0; n     while (bfs()) { n       for (typeof(n) i = 0; i <= n; i++) run[i] = last[i]; n       for (int delta = dfs(s, infi); delta > 0; delta = dfs(s, infi)) n         total += delta; n     } n     return total; n   } n }; n int n, m, a[maxn + 5]; n pair<int, long long> P[maxn + 5]; n map<int, int> M[maxn + 5]; n set<int> prime; n Dinic dinic; n void enter() { n   scanf( %d %d , &n, &m); n   for (typeof(n) i = 1; i <= n; i++) scanf( %d , &a[i]); n   for (typeof(m) i = 1; i <= m; i++) scanf( %d %d , &P[i].first, &P[i].second); n } n void Phantich(int id, int n) { n   for (int i = 2; i * i <= n; i++) n     if (n % i == 0) { n       int cnt = 0; n       while (n % i == 0) { n         cnt++; n         n /= i; n       } n       M[id][i] = cnt; n       prime.insert(i); n     } n   if (n > 1) { n     M[id][n] = 1; n     prime.insert(n); n   } n } n void solve() { n   for (typeof(n) i = 1; i <= n; i++) { n     Phantich(i, a[i]); n   } n   long long res = 0; n   for (typeof(prime.begin()) it = prime.begin(); it != prime.end(); it++) { n     dinic.init(n + 2, 0, n + 1); n     for (typeof(m) i = 1; i <= m; i++) { n       int u = P[i].first; n       int v = P[i].second; n       if (u % 2) swap(u, v); n       if ((M[u].find(*it) == M[u].end()) || (M[v].find(*it) == M[v].end())) n         continue; n       int c = min(M[u][*it], M[v][*it]); n       dinic.add(u, v, c, 0); n     } n     for (typeof(n) i = 1; i <= n; i++) n       if (i % 2 == 0) { n         if (M[i].find(*it) == M[i].end()) continue; n         dinic.add(0, i, M[i][*it], 0); n       } else { n         if (M[i].find(*it) == M[i].end()) continue; n         dinic.add(i, n + 1, M[i][*it], 0); n       } n     res += dinic.maxFlow(); n   } n   cout << res << endl; n } n int main() { n   enter(); n   solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int SET(int n, int pos) { return n = n | (1 << pos); } n int RESET(int n, int pos) { return n = n & ~(1 << pos); } n int CHECK(int n, int pos) { return (bool)(n & (1 << pos)); } n int str2int(string s) { n   stringstream ss(s); n   int x; n   ss >> x; n   return x; n } n string int2str(int a) { n   stringstream ss; n   ss << a; n   string str = ss.str(); n   return str; n } n string char2str(char a) { n   stringstream ss; n   ss << a; n   string str = ss.str(); n   return str; n } n long long int bigMod(long long int n, long long int power, long long int MOD) { n   if (power == 0) return 1; n   if (power % 2 == 0) { n     long long int ret = bigMod(n, power / 2, MOD); n     return ((ret % MOD) * (ret % MOD)) % MOD; n   } else n     return ((n % MOD) * (bigMod(n, power - 1, MOD) % MOD)) % MOD; n } n long long int modInverse(long long int n, long long int MOD) { n   return bigMod(n, MOD - 2, MOD); n } n int POW(int x, int y) { n   int res = 1; n   for (; y;) { n     if ((y & 1)) { n       res *= x; n     } n     x *= x; n     y >>= 1; n   } n   return res; n } n int inverse(int x) { n   double p = ((double)1.0) / x; n   return (p) + 1e-9; n } n int gcd(int a, int b) { n   while (b) b ^= a ^= b ^= a %= b; n   return a; n } n int nC2(int n) { return n * (n - 1) / 2; } n long long int MOD(long long int n, long long int mod) { n   if (n >= 0) n     return n % mod; n   else if (-n == mod) n     return 0; n   else n     return mod + (n % mod); n } n int n, data[105], Mask[65], dp[105][(1 << 18) + 1], path[105][(1 << 18) + 1]; n bool isPrime[65]; n vector<int> prime; n void seive_N_logN(int N) { n   memset(isPrime, true, sizeof isPrime); n   prime.clear(); n   isPrime[1] = false; n   for (int i = 4; i <= N; i = i + 2) isPrime[i] = false; n   for (int i = 3; i * i <= N; i = i + 2) { n     if (isPrime[i]) { n       for (int j = i * i; j <= N; j += i) isPrime[j] = false; n     } n   } n   for (int i = 1; i < N; i++) n     if (isPrime[i]) prime.push_back(i); n   for (int i = 1; i <= 60; i++) { n     for (int j = 0; j < prime.size(); j++) { n       if (i % prime[j] == 0) Mask[i] = SET(Mask[i], j); n     } n   } n } n int call(int i, int mask) { n   if (i >= n) { n     return 0; n   } n   int &ret = dp[i][mask]; n   if (ret != -1) return ret; n   ret = 1000000000; n   for (int j = 1; j <= 60; j++) { n     if (mask & Mask[j]) continue; n     int xx = call(i + 1, mask | Mask[j]) + abs(data[i] - j); n     if (xx < ret) { n       ret = xx; n       path[i][mask] = j; n     } n   } n   return ret; n } n int main() { n   seive_N_logN(60); n   int t, cas = 0; n   scanf( %d , &n); n   for (int i = 0; i < n; i++) { n     scanf( %d , &data[i]); n   } n   memset(dp, -1, sizeof(dp)); n   call(0, 0); n   int mask = 0; n   for (int i = 0; i < n; i++) { n     int ans = path[i][mask]; n     printf( %d  , ans); n     mask |= Mask[ans]; n   } n   printf(  n ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n using namespace std::chrono; n int main() { n   string s; n   cin >> s; n   string ans; n   bool found{false}; n   for (int i = 0; i < (int)s.size(); i++) { n     if ((i == (int)s.size() - 1) && !found) continue; n     if (s[i] ==  1  || found) n       ans += s[i]; n     else n       found = true; n   } n   cout << ans <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n set<int> st; n int arr[1000005]; n bool taken[1000005]; n map<int, int> mp; n int main() { n   int n; n   (scanf( %d , &n)); n   for (int i = 1; i <= n; i++) { n     (scanf( %d , &arr[i])); n     mp[arr[i]]++; n     st.insert(i); n   } n   for (int i = 1; i <= n; i++) st.erase(arr[i]); n   memset(taken, false, sizeof(taken)); n   cout << st.size() <<   n ; n   for (int i = 1; i <= n; i++) { n     if (mp[arr[i]] > 1) { n       if (*st.begin() > arr[i] && taken[arr[i]] == false) n         taken[arr[i]] = true; n       else { n         mp[arr[i]]--; n         arr[i] = *st.begin(); n         st.erase(st.begin()); n       } n     } n   } n   for (int i = 1; i <= n; i++) cout << arr[i] <<    ; n } n


#include <bits/stdc++.h> n using namespace std; n const int inf = 0x3f3f3f3f; n const int neginf = 0xc0c0c0c0; n const int mod = 1000000007; n bool cmp(const double& a, const double& b, const double TOL = 1e-9) { n   return (a + TOL > b) ? (b + TOL > a) ? 0 : 1 : -1; n } n long long powmod(long long a, long long b) { n   long long res = 1; n   a %= mod; n   assert(b >= 0); n   for (; b; b >>= 1) { n     if (b & 1) res = (res * a) % mod; n     a = (a * a) % mod; n   } n   return res; n } n int n, A, B, C, T; n int main() { n   cin >> n >> A >> B >> C >> T; n   vector<int> msg(n); n   for (int(i) = (0); (i) < (n); ++(i)) cin >> msg[i]; n   sort((msg).begin(), (msg).end()); n   int last = msg.back(); n   long long best = 0; n   for (int v : msg) { n     int gain; n     if (C > B) { n       gain = A + (T - v) * (C - B); n     } else n       gain = A; n     best += gain; n   } n   cout << best << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n bool done[123][123]; n char s[123][123]; n vector<pair<pair<long long, long long>, long long int> > v; n int main() { n   long long int n, m; n   scanf( %lld%lld , &n, &m); n   for (int i = 0; i < n; ++i) n     for (int j = 0; j < m; ++j) { n       done[i][j] = false; n       scanf(  %c , &s[i][j]); n     } n   for (int i = 1; i < n - 1; ++i) n     for (int j = 1; j < m - 1; ++j) n       if (s[i][j] !=  . ) { n         if (s[i - 1][j] ==  *  && s[i + 1][j] ==  *  && s[i][j - 1] ==  *  && n             s[i][j + 1] ==  * ) { n           long long int up = 0, dn = 0, lt = 0, rt = 0; n           for (int k = i - 1; k >= 0; k--) n             if (s[k][j] ==  . ) n               break; n             else n               up++; n           for (int k = i + 1; k < n; k++) n             if (s[k][j] ==  . ) n               break; n             else n               dn++; n           for (int k = j - 1; k >= 0; k--) n             if (s[i][k] ==  . ) n               break; n             else n               lt++; n           for (int k = j + 1; k < m; k++) n             if (s[i][k] ==  . ) n               break; n             else n               rt++; n           long long int mn = min(up, min(dn, min(lt, rt))); n           if (mn) { n             v.push_back( n                 make_pair(pair<long long, long long>(i + 1, j + 1), mn)); n             int k, cnt; n             cnt = mn; n             done[i][j] = true; n             for (k = i - 1; cnt; k--, cnt--) done[k][j] = true; n             cnt = mn; n             for (k = i + 1; cnt; k++, cnt--) done[k][j] = true; n             cnt = mn; n             for (k = j - 1; cnt; k--, cnt--) done[i][k] = true; n             cnt = mn; n             for (k = j + 1; cnt; k++, cnt--) done[i][k] = true; n           } n         } n       } n   for (int i = 0; i < n; ++i) n     for (int j = 0; j < m; ++j) n       if (s[i][j] ==  *  && done[i][j] == false) { n         printf( -1 n ); n         return 0; n       } n   printf( %lld n , (long long int)v.size()); n   for (int i = 0; i != (long long int)v.size(); ++i) n     printf( %lld %lld %lld n , v[i].first.first, v[i].first.second, n            v[i].second); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m, len[400], cnt, Block, pos[100010]; n long long c[100010], sum[400], v[100010], cov[400], dlt[400]; n bool mak[400]; n void BuildBlocks() { n   int tail; n   for (int i = 1; i <= n; i = tail + 1) { n     tail = min(i + Block - 1, n); n     ++cnt; n     len[cnt] = tail; n     for (int j = i; j <= tail; j++) pos[j] = cnt; n   } n } n long long Abs(long long x) { return (x < 0) ? -x : x; } n void work(int x, int y, long long k) { n   int now = pos[x]; n   long long delta; n   for (int i = x; i <= y; i++) { n     delta = Abs(v[i] - k); n     v[i] = k; n     c[i] += delta; n     sum[now] += delta; n   } n } n void pushdown(int now) { n   if (cov[now] != 0) { n     for (int i = len[now - 1] + 1; i <= len[now]; i++) v[i] = cov[now]; n     cov[now] = 0; n   } n } n void Change(int x, int y, long long k) { n   int px, py, H, T; n   px = pos[x]; n   py = pos[y]; n   H = px + 1; n   T = py - 1; n   if (px == py) { n     pushdown(px); n     work(x, y, k); n     mak[px] = false; n     return; n   } n   if (x == len[px - 1] + 1) n     --H; n   else { n     pushdown(px); n     mak[px] = false; n     work(x, len[px], k); n   } n   if (y == len[py]) n     ++T; n   else { n     pushdown(py); n     mak[py] = false; n     work(len[py - 1] + 1, y, k); n   } n   if (H > T) return; n   for (int i = H; i <= T; i++) n     if (mak[i] == false) { n       work(len[i - 1] + 1, len[i], k); n       mak[i] = true; n     } else { n       if (cov[i] == 0) cov[i] = v[len[i]]; n       sum[i] += (long long)(len[i] - len[i - 1]) * Abs(k - cov[i]); n       dlt[i] += Abs(k - cov[i]); n       cov[i] = k; n     } n } n long long count(int x, int y) { n   long long ans = 0; n   int now = pos[x]; n   for (int i = x; i <= y; i++) ans += c[i] + dlt[now]; n   return ans; n } n long long Query(int x, int y) { n   int px, py, H, T; n   long long ans = 0; n   px = pos[x]; n   py = pos[y]; n   H = px + 1; n   T = py - 1; n   if (px == py) return count(x, y); n   if (x == len[px - 1] + 1) n     --H; n   else n     ans += count(x, len[px]); n   if (y == len[py]) n     ++T; n   else n     ans += count(len[py - 1] + 1, y); n   for (int i = H; i <= T; i++) ans += sum[i]; n   return ans; n } n int main() { n   scanf( %d%d , &n, &m); n   for (int i = 1; i <= n; i++) v[i] = i; n   Block = ceil(sqrt(n)); n   BuildBlocks(); n   for (int i = 1; i <= m; i++) { n     int opt, x, y, k; n     scanf( %d%d%d , &opt, &x, &y); n     if (opt == 1) { n       scanf( %d , &k); n       Change(x, y, k); n     } else n       printf( %I64d n , Query(x, y)); n   } n   return 0; n } n


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


#include <bits/stdc++.h> n using namespace std; n const int N = 2e5 + 5; n long long a[N], b[N], x[N]; n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   long long n, t; n   cin >> n >> t; n   for (int i = 1; i <= n; ++i) cin >> a[i]; n   for (int i = 1; i <= n; ++i) cin >> x[i]; n   for (int i = 1; i <= n; ++i) n     if (x[i] < i || x[i] < x[i - 1]) return puts( No ), 0; n   for (int i = 1; i < n; ++i) n     if (x[i] != x[i + 1] && x[i] != i) return puts( No ), 0; n   a[n + 1] = a[n] + 5; n   for (int i = 1; i <= n; ++i) { n     if (x[i] == i) n       b[i] = a[i + 1] + t - 1; n     else n       b[i] = a[i + 1] + t; n     if (b[i] == b[i - 1]) return puts( No ), 0; n   } n   cout <<  Yes  << endl; n   for (int i = 1; i <= n; ++i) cout << b[i] <<    ; n   cout <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n using li = long long; n using ld = long double; n void solve(bool); n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(nullptr); n   cout << fixed << setprecision(20); n   solve(true); n   return 0; n } n const int maxn = (int)1e5 + 5; n int n, a[maxn]; n map<int, int> m; n void solve(bool __attribute__((unused)) read) { n   cin >> n; n   for (int i = 0; i < n; ++i) { n     cin >> a[i]; n     if (a[i] > 0 && !m.count(a[i] - 1)) { n       cout << i + 1 <<   n ; n       return; n     } n     m[a[i]] = 1; n   } n   cout << -1 <<   n ; n   return; n } n


#include <bits/stdc++.h> n using namespace std; n template <class _T> n inline _T sqr(const _T& x) { n   return x * x; n } n template <class _T> n inline string tostr(const _T& a) { n   ostringstream os(  ); n   os << a; n   return os.str(); n } n const long double PI = 3.1415926535897932384626433832795; n const long double EPS = 1e-11; n const int dx[4] = {0, -1, 0, 1}; n const int dy[4] = {-1, 0, 1, 0}; n int n, m, k; n char a[1024][1024]; n char a0[1024][1024]; n int c[1024][1024]; n bool u[1024][1024]; n bool u2[12]; n void dfs(int x, int y, int px, int py) { n   u[x][y] = true; n   int cnt = 0; n   int t[4] = {0, 1, 2, 3}; n   for (int i = 0; i < (int)(4); i++) { n     swap(t[i], t[rand() % 4]); n   } n   for (int l1 = 0; l1 < (int)(4); l1++) { n     int l = t[l1]; n     int xn = x + dx[l]; n     int yn = y + dy[l]; n     if (xn < 0 || xn >= n || yn < 0 || yn >= m || u[xn][yn] || a[xn][yn] ==  # ) n       continue; n     dfs(xn, yn, x, y); n     if (c[xn][yn] == -1) cnt++; n   } n   if (cnt) { n     for (int l = 0; l < (int)(4); l++) { n       int xn = x + dx[l]; n       int yn = y + dy[l]; n       if (xn < 0 || xn >= n || yn < 0 || yn >= m || a[xn][yn] ==  #  || n           c[xn][yn] != -1 || (xn == px && yn == py)) n         continue; n       c[xn][yn] = k; n     } n     c[x][y] = k++; n   } n } n void dfs2(int x, int y, int col) { n   u[x][y] = true; n   for (int l = 0; l < (int)(4); l++) { n     int xn = x + dx[l]; n     int yn = y + dy[l]; n     if (xn < 0 || xn >= n || yn < 0 || yn >= m || a[xn][yn] ==  # ) continue; n     if (c[xn][yn] == col) { n       if (!u[xn][yn]) dfs2(xn, yn, col); n     } else if (a[xn][yn] !=  . ) { n       u2[a[xn][yn] -  0 ] = true; n     } n   } n } n void dfs3(int x, int y, int col, int nc) { n   a[x][y] =  0  + nc; n   c[x][y] = -1; n   for (int l = 0; l < (int)(4); l++) { n     int xn = x + dx[l]; n     int yn = y + dy[l]; n     if (xn < 0 || xn >= n || yn < 0 || yn >= m || a[xn][yn] ==  # ) continue; n     if (c[xn][yn] == col) { n       dfs3(xn, yn, col, nc); n     } n   } n } n int check(int x, int y) { n   int ans = 1; n   u[x][y] = true; n   for (int l = 0; l < (int)(4); l++) { n     int xn = x + dx[l]; n     int yn = y + dy[l]; n     if (xn < 0 || xn >= n || yn < 0 || yn >= m || u[xn][yn] || n         a[xn][yn] ==  #  || a[xn][yn] != a[x][y]) n       continue; n     ans += check(xn, yn); n   } n   return ans; n } n int check2(int x, int y) { n   int ans = 1; n   u[x][y] = true; n   for (int l = 0; l < (int)(4); l++) { n     int xn = x + dx[l]; n     int yn = y + dy[l]; n     if (xn < 0 || xn >= n || yn < 0 || yn >= m || u[xn][yn] || n         a[xn][yn] ==  #  || c[xn][yn] != c[x][y]) n       continue; n     ans += check2(xn, yn); n   } n   return ans; n } n int main() { n   cout << setiosflags(ios::fixed) << setprecision(10); n   srand(672465); n   memset(c, 0xff, sizeof(c)); n   scanf( %d%d , &n, &m); n   memset((a), 0, sizeof(a)); n   for (int i = 0; i < (int)(n); i++) { n     scanf( %s , a[i]); n     a[i][m] =   0 ; n   } n   memcpy(a0, a, sizeof(a)); n   memset((u), 0, sizeof(u)); n   k = 0; n   for (int i = 0; i < (int)(n); i++) { n     for (int j = 0; j < (int)(m); j++) { n       if (!u[i][j] && a[i][j] !=  # ) { n         dfs(i, j, -1, -1); n         if (c[i][j] == -1) { n           bool bb = true; n           for (int l = 0; l < (int)(4); l++) { n             int xn = i + dx[l]; n             int yn = j + dy[l]; n             if (xn < 0 || xn >= n || yn < 0 || yn >= m || a[xn][yn] ==  # ) n               continue; n             if (c[xn][yn] != -1) { n               c[i][j] = c[xn][yn]; n               bb = false; n               break; n             } n           } n           if (bb) { n             puts( -1 ); n             return 0; n           } n         } n       } n     } n   } n   memset((u), 0, sizeof(u)); n   for (int i = 0; i < (int)(n); i++) { n     for (int j = 0; j < (int)(m); j++) { n       if (!u[i][j] && a[i][j] !=  # ) { n         int cnt = check2(i, j); n         if (cnt < 2 || cnt > 5) { n         } n       } n     } n   } n   memset((u), 0, sizeof(u)); n   for (int i = 0; i < (int)(n); i++) { n     for (int j = 0; j < (int)(m); j++) { n       if (a[i][j] ==  . ) { n         memset((u2), 0, sizeof(u2)); n         dfs2(i, j, c[i][j]); n         bool bbb = true; n         for (int t = 0; t < (int)(12); t++) { n           if (!u2[t]) { n             dfs3(i, j, c[i][j], t); n             bbb = false; n             break; n           } n         } n         if (bbb) return 1; n       } n     } n   } n   memset((u), 0, sizeof(u)); n   for (int i = 0; i < (int)(n); i++) { n     for (int j = 0; j < (int)(m); j++) { n       if (a0[i][j] ==  # ) { n         if (a[i][j] !=  # ) cout <<  Botwa ! 1 !  << endl; n       } else { n         if (!isdigit(a[i][j])) cout <<  Botwa ! 2 !  << endl; n       } n       if (a[i][j] !=  #  && !isdigit(a[i][j])) n         cout <<  a[  << i <<  ][  << j <<  ] =    << a[i][j] <<     << endl; n       if (!u[i][j] && a[i][j] !=  # ) { n         int cnt = check(i, j); n         if (cnt < 2 || cnt > 5) { n           for (int ii = (-4); ii <= (int)(4); ii++) { n             for (int jj = (-4); jj <= (int)(4); jj++) { n               if (i + ii >= 0 && j + jj >= 0) cout << a[i + ii][j + jj]; n             } n             if (i + ii >= 0) cout << endl; n           } n           cout <<  ?   << cnt <<     << a[i][j] << endl; n         } n       } n     } n   } n   for (int i = 0; i < (int)(n); i++) { n     puts(a[i]); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long int n; n string m =  abacaba ; n long long int bal(string s) { n   long long int bl = 0; n   for (long long int i = 0; i < n - 6; i++) { n     bool b = true; n     for (long long int j = 0; j < m.size(); j++) { n       if (s[i + j] != m[j]) { n         b = false; n         break; n       } n     } n     if (b) bl++; n   } n   return bl; n } n int main() { n   long long int tc; n   scanf( %lld , &tc); n   while (tc--) { n     scanf( %lld , &n); n     string s; n     cin >> s; n     long long int koto = bal(s); n     if (koto == 1) { n       for (long long int i = 0; i < n; i++) { n         if (s[i] ==  ? ) s[i] =  d ; n       } n       cout <<  YES  << endl << s << endl; n       continue; n     } n     if (koto >= 2) { n       cout <<  NO  << endl; n       continue; n     } n     long long int oh = 0; n     for (long long int i = 0; i < n - 6; i++) { n       string ans = s; n       for (long long int j = 0; j < m.size(); j++) { n         if (ans[i + j] ==  ? ) ans[i + j] = m[j]; n       } n       koto = bal(ans); n       if (koto > 1 || koto == 0) continue; n       for (long long int j = 0; j < n; j++) { n         if (ans[j] ==  ? ) ans[j] =  d ; n       } n       oh = 1; n       s = ans; n       break; n     } n     if (oh) { n       cout <<  YES  << endl << s << endl; n     } else n       cout <<  NO  << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const long long mod = 1e9 + 7; n int main() { n   int t; n   cin >> t; n   while (t--) { n     string s; n     cin >> s; n     int n = (int)s.size(); n     vector<int> nxt(n); n     int pos = -1; n     for (int i = 0; i < n; i++) { n       if (s[i] ==  1 ) pos = i; n       nxt[i] = pos; n     } n     int ans = 0; n     for (int r = 0; r < n; r++) { n       int val = 0; n       for (int l = r; r - l < 20 && l >= 0; l--) { n         if (s[l] ==  0 ) continue; n         val += 1 << (r - l); n         if (val <= r - ((l != 0) ? nxt[l - 1] : -1)) ans++; n       } n     } n     cout << ans <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, p[200001], bl, cnt, cntbl, visited[200001], flag; n int main() { n   cin >> n; n   for (int i = 1; i <= n; i++) { n     cin >> p[i]; n   } n   for (int i = 1; i <= n; i++) { n     if (!visited[i]) { n       int j = i, tmp = j; n       while (!visited[j]) { n         visited[j] = 1; n         j = p[j]; n       } n       if (j != tmp) flag = 1; n       cnt++; n     } n   } n   for (int i = 0; i < n; i++) { n     cin >> bl; n     if (bl) cntbl++; n   } n   cout << ((cnt == 1 && !flag ? 0 : cnt) + (cntbl % 2 == 0 ? 1 : 0)); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int NMax = 110000; n int N; n long long A[NMax], B[NMax], dp[NMax]; n int Q[NMax], L, R; n int main() { n   scanf( %d , &N); n   for (int i = 1; i <= N; i++) scanf( %I64d , A + i); n   for (int i = 1; i <= N; i++) scanf( %I64d , B + i); n   L = 0; n   R = 0; n   Q[L] = 1; n   for (int i = 2; i <= N; i++) { n     while (R > L && n            (double)(dp[Q[L + 1]] - dp[Q[L]]) / (B[Q[L]] - B[Q[L + 1]]) < n                (double)A[i]) n       L++; n     dp[i] = dp[Q[L]] + B[Q[L]] * A[i]; n     Q[++R] = i; n     while (R > L + 1) { n       int c = R, b = R - 1, a = R - 2; n       if ((double)(dp[Q[c]] - dp[Q[b]]) / (B[Q[b]] - B[Q[c]]) < n           (double)(dp[Q[b]] - dp[Q[a]]) / (B[Q[a]] - B[Q[b]])) { n         Q[b] = Q[c]; n         R--; n       } else n         break; n     } n   } n   printf( %I64d n , dp[N]); n   getchar(); n   getchar(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   string s, t; n   cin >> s >> t; n   string p(s.size(),  0 ); n   int dif = 0; n   for (int i = 0; i < s.size(); i++) { n     if (s[i] != t[i]) dif++; n   } n   if (dif % 2 != 0) return cout <<  impossible , 0; n   int a(0), b(0); n   for (int i = 0; i < s.size(); i++) { n     if (s[i] != t[i]) { n       if (a <= b) { n         p[i] = t[i]; n         a++; n       } else { n         p[i] = s[i]; n         b++; n       } n     } n   } n   cout << p; n } n


#include <bits/stdc++.h> n using namespace std; n long long aa[258], bb[258]; n int main() { n   ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0), cerr <<   ; n   string a, b; n   long long n; n   cin >> n; n   cin >> a >> b; n   long double s = 0; n   for (int i = 1; i <= n; ++i) n     aa[b[i - 1]] += i, n         s += (n - i + 1) * (aa[a[i - 1]] + bb[b[i - 1]]) * 1.0 / n, n         bb[a[i - 1]] += i; n   long double l = s * 1.0 / (n + 1) / (2 * n + 1); n   l *= 6.0; n   cout.precision(9); n   cout << fixed << l; n } n


#include <bits/stdc++.h> n using namespace std; n signed main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   int t; n   cin >> t; n   for (int tt = 0; tt < t; tt++) { n     int n, flag = 0; n     cin >> n; n     vector<int> q(n + 1); n     vector<int> p(n + 1, -1); n     vector<int> use(n + 1, 0); n     for (int i = 1; i <= n; i++) cin >> q[i]; n     p[1] = q[1]; n     use[q[1]] = 1; n     for (int i = 2; i <= n; i++) n       if (q[i] > q[i - 1]) { n         p[i] = q[i]; n         use[q[i]] = 1; n       } n     vector<int> w; n     for (int i = 1; i <= n; i++) n       if (use[i] == 0) w.push_back(i); n     int u = 0; n     for (int i = 1; i <= n; i++) n       if (p[i] == -1) { n         p[i] = w[u]; n         u++; n       } n     int ma = p[1]; n     for (int i = 1; i <= n; i++) { n       ma = max(ma, p[i]); n       if (ma != q[i]) flag = 1; n     } n     if (flag == 0) { n       for (int i = 1; i <= n; i++) cout << p[i] <<    ; n     } else n       cout << -1; n     cout << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n double dist(double x1, double y1, double x2, double y2) { n   double dx = x1 - x2; n   double dy = y1 - y2; n   return sqrt(dx * dx + dy * dy); n } n double catet(double a, int b) { n   double z = a * a - b * b; n   if (z < 1e-12) z = 0; n   return sqrt(z); n } n int main() { n   int xp, yp, vp; n   cin >> xp >> yp >> vp; n   int x, y, v, r; n   cin >> x >> y >> v >> r; n   const double eps = 1e-10; n   const double R = sqrt(1.0 * xp * xp + 1.0 * yp * yp); n   double lo = 0; n   double hi = 1e7; n   double T; n   while ((hi - lo) / hi > eps) { n     T = (lo + hi) / 2; n     const double alpha = T / (R / vp); n     double nx = xp * cos(alpha) - yp * sin(alpha); n     double ny = xp * sin(alpha) + yp * cos(alpha); n     double a = ny - y; n     double b = x - nx; n     double c = -(a * x + b * y); n     double z = sqrt(a * a + b * b); n     a /= z; n     b /= z; n     c /= z; n     double need; n     if (fabs(c) < r - 1e-8) { n       double mx = a * (-c); n       double my = b * (-c); n       if (dist(x, y, mx, my) + dist(mx, my, nx, ny) > n           dist(x, y, nx, ny) + 1e-8) { n         need = dist(x, y, nx, ny); n       } else { n         need = catet(dist(x, y, 0, 0), r) + catet(dist(nx, ny, 0, 0), r); n         double alp = fabs(atan2(y, x) - atan2(ny, nx)); n         if (alp > M_PI) alp = 2 * M_PI - alp; n         alp -= acos(r / dist(x, y, 0, 0)); n         alp -= acos(r / dist(nx, ny, 0, 0)); n         need += alp * r; n       } n     } else { n       need = dist(x, y, nx, ny); n     } n     if (need <= v * T) { n       hi = T; n     } else { n       lo = T; n     } n   } n   printf( %.6f n , T); n   return 0; n } n