#include <bits/stdc++.h> n using namespace std; n int n, siz[200005], l[200005], r[200005], a[200005], p[200005], an = 0, n                                                                 b[200005]; n bool vis[200005]; n vector<int> vt[200005], vec; n set<int> st; n set<int>::iterator it; n int root(int node) { n   while (p[node] != node) { n     p[node] = p[p[node]]; n     node = p[node]; n   } n   return node; n } n int merging(int n1, int n2) { n   int p1 = root(n1), p2 = root(n2); n   if (p1 == 0) return p2; n   if (p2 == 200005 - 1) return p1; n   if (p1 == p2) return p1; n   if (siz[p1] > siz[p2]) { n     siz[p1] += siz[p2]; n     p[p2] = p[p1]; n     l[p1] = min(l[p1], l[p2]); n     r[p1] = max(r[p1], r[p2]); n     return p1; n   } else { n     siz[p2] += siz[p1]; n     p[p1] = p[p2]; n     l[p2] = min(l[p2], l[p1]); n     r[p2] = max(r[p1], r[p2]); n     return p2; n   } n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   ; n   scanf( %d , &n); n   int lef = 1, cur = 1, ri; n   st.insert(0); n   st.insert(200005 - 1); n   for (int i = 1; i <= n; i++) { n     scanf( %d , &a[i]); n     if (i != 1 && a[i] != a[i - 1]) { n       l[cur] = lef, r[cur] = i - 1; n       siz[cur] = r[cur] - l[cur] + 1; n       vt[siz[cur]].push_back(l[cur]); n       p[cur] = cur; n       cur++; n       lef = i; n     } n     st.insert(i); n     b[i] = cur; n   } n   l[cur] = lef, r[cur] = n, siz[cur] = n - lef + 1; n   vt[siz[cur]].push_back(l[cur]); n   p[cur] = cur; n   for (int i = 200005 - 1; i >= 1; i--) { n     sort(vt[i].begin(), vt[i].end()); n     for (int j = 0, par, tmp, tpp, lol, now; j < vt[i].size(); j++) { n       now = vt[i][j]; n       if (vis[now]) continue; n       par = root(b[now]); n       it = st.lower_bound(l[par]); n       it--; n       tmp = *it; n       it = st.lower_bound(r[par]); n       it++; n       tpp = *it; n       if (a[tmp] == a[tpp] && a[tmp]) { n         lol = merging(b[tmp], b[tpp]); n         if (siz[lol] >= i) { n           vt[i][j] = l[lol]; n           j--; n         } else n           vt[siz[lol]].push_back(l[lol]); n       } n       for (it = st.lower_bound(l[par]); *it <= r[par]; it++) vec.push_back(*it); n       for (int k = 0; k < vec.size(); k++) { n         st.erase(vec[k]), vis[vec[k]] = true; n       } n       vec.clear(); n       an++; n     } n   } n   printf( %d n , an); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   cin >> n; n   string s; n   cin >> s; n   int maxi = 0; n   int ind = 0; n   string ret =   ; n   for (int i = 0; i < n; i++) { n     string temp; n     int d = 10 - (s[i] - 48); n     int j = i; n     int ctr = 0; n     while (ctr < n) { n       ctr++; n       temp += ((s[j] - 48 + d) % 10) + 48; n       j = (j + 1) % s.length(); n     } n     if (ret ==   ) n       ret = temp; n     else if (temp < ret) n       ret = temp; n   } n   cout << ret; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(false); n   long long n; n   cin >> n; n   vector<vector<long long> > p(2, vector<long long>(n)), n       suf(2, vector<long long>(n + 1)), ebalVLevo(2, vector<long long>(n + 1)), n       ebalVPravo(2, vector<long long>(n + 1)); n   for (long long i = 0; i < 2; i++) { n     for (long long j = 0; j < n; j++) { n       cin >> p[i][j]; n     } n   } n   for (long long i = 0; i < 2; i++) { n     suf[i][n - 1] = p[i][n - 1]; n     for (long long j = n - 2; j >= 0; j--) { n       suf[i][j] = suf[i][j + 1] + p[i][j]; n     } n   } n   for (long long i = 0; i < 2; i++) { n     ebalVPravo[i][0] = (n)*p[i][0]; n     for (long long j = 1; j < n; j++) { n       ebalVPravo[i][j] = ebalVPravo[i][j - 1] + p[i][j] * (n + j); n     } n   } n   for (long long i = 0; i < 2; i++) { n     ebalVLevo[i][n - 1] = p[i][n - 1]; n     for (long long j = n - 2; j >= 0; j--) { n       ebalVLevo[i][j] = ebalVLevo[i][j + 1] + p[i][j] * (n - j); n     } n   } n   long long sum = 0; n   long long ans = 0; n   long long i = 0, j = 0; n   bool lastI = false; n   for (long long t = 0; t < 2 * n; t++) { n     sum += t * p[i][j]; n     long long tmp = sum; n     long long tmpR = n         ebalVPravo[i][n - 1] - ebalVPravo[i][j] - (n - t + j) * suf[i][j + 1]; n     long long tmpL = 0; n     long long tmpLL = 0; n     if (t == 0) { n       tmpL = ebalVLevo[1][j] + (t + (n - j) - 1) * suf[1][j]; n     } else if (i == 0) { n       tmpL = ebalVLevo[1][j + 1] + (t + (n - j) - 1) * suf[1][j + 1]; n     } else if (i == 1) { n       tmpL = ebalVLevo[0][j + 1] + (t + (n - j) - 1) * suf[0][j + 1]; n     } n     ans = max(ans, tmp + tmpL + tmpR); n     if (!lastI) { n       if (!i) { n         i++; n       } else { n         i--; n       } n       lastI = true; n     } else { n       j++; n       lastI = false; n     } n   } n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long dp[300005]; n vector<long long> g[300005]; n long long ans = 1; n long long subs[300005]; n long long f[300005][2]; n void dfs(long long s, long long p) { n   dp[s] = 0; n   long long mx1 = 0; n   long long mx2 = 0; n   long long cnt = 0; n   for (auto it : g[s]) { n     if (it != p) { n       dfs(it, s); n       if (mx1 < dp[it]) { n         mx2 = mx1; n         mx1 = dp[it]; n       } else if (mx2 < dp[it]) n         mx2 = dp[it]; n       cnt++; n     } n   } n   f[s][0] = mx1; n   f[s][1] = mx2; n   subs[s] = cnt; n   dp[s] += mx1 + cnt; n } n void ans_dfs(long long s, long long p) { n   for (auto it : g[s]) { n     if (it != p) ans_dfs(it, s); n   } n   if (s == p) { n     ans = max(ans, 1 + dp[s] + f[s][1]); n     return; n   } n   long long y = f[p][0]; n   if (y == dp[s]) y = f[p][1]; n   long long z = max(y, f[s][1]); n   ans = max(ans, 1 + dp[s] + z + 1); n } n int32_t main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   ; n   long long t; n   cin >> t; n   while (t--) { n     long long n; n     cin >> n; n     for (long long i = 1; i <= n; i++) { n       dp[i] = 0; n       g[i].clear(); n     } n     for (long long i = 0; i < n - 1; i++) { n       long long a, b; n       cin >> a >> b; n       g[a].push_back(b); n       g[b].push_back(a); n     } n     ans = 1; n     dfs(1, 1); n     ans_dfs(1, 1); n     cout << ans <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 18; n long long dp[(1 << N)][105]; n string n; n int qwq; n int m, pw[20]; n bool used[10]; n void init() { n   pw[0] = 1; n   for (int i = 1; i <= n.size(); i++) pw[i] = pw[i - 1] * 10 % m; n } n int main() { n   cin >> n >> m; n   dp[0][0] = 1; n   init(); n   for (int i = 1; i < (1 << n.size()); i++) { n     int sz = n.size() - __builtin_popcount(i); n     for (int k = 0; k < m; k++) { n       for (int j = 0; j < 10; j++) used[j] = false; n       for (int j = 0; j < n.size(); j++) n         if (((i >> j) & 1) && !used[n[j] -  0 ]) n           if (sz != (int)n.size() - 1 || n[j] !=  0 ) n             dp[i][k] += n                 dp[i - (1 << j)][(m + k - (n[j] -  0 ) * pw[sz] % m) % m], n                 used[n[j] -  0 ] = true; n     } n   } n   cout << dp[(1 << n.size()) - 1][0] <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXM = 500 + 5; n const int MAXN = 500 + 5; n const int MAXQ = 300 + 5; n const int MAXK = 800 + 5; n const int P = 5; n const int MOD = 1e9 + 7; n int n, m, q, a[MAXN][MAXK]; n int inv[P], ans = 1; n char buf[MAXM]; n int main() { n   for (int i = 0; i < P; i++) inv[i] = i * i * i % P; n   scanf( %d%d , &n, &m); n   for (int i = 1; i <= n; i++) { n     scanf( %s , buf + 1); n     for (int j = 1; j <= m; j++) a[j][i] = buf[j] -  a ; n   } n   scanf( %d , &q); n   for (int i = 1; i <= q; i++) { n     scanf( %s , buf + 1); n     for (int j = 1; j <= m; j++) a[j][i + n] = buf[j] -  a ; n   } n   int cur = 1, r = 0; n   for (int i = 1; i <= m && cur <= n; i++) { n     while (cur <= n) { n       int t = i; n       for (int j = i + 1; j <= m; j++) n         if (a[j][cur]) t = j; n       for (int j = 1; j <= n + q; j++) swap(a[i][j], a[t][j]); n       if (a[i][cur]) break; n       cur++; n     } n     if (cur == n + 1) break; n     for (int j = cur + 1; j <= n + q; j++) n       a[i][j] = a[i][j] * inv[a[i][cur]] % P; n     a[i][cur] = 1; n     for (int j = i + 1; j <= m; j++) { n       for (int k = cur + 1; k <= n + q; k++) { n         a[j][k] -= a[j][cur] * a[i][k] % P; n         if (a[j][k] < 0) a[j][k] += P; n       } n       a[j][cur] = 0; n     } n     r++; n   } n   for (int i = 1; i <= n - r; i++) ans = ans * 5ll % MOD; n   for (int i = 1; i <= q; i++) { n     int x = ans; n     for (int j = 1; j <= m; j++) { n       bool flg = 1; n       for (int k = 1; k <= n; k++) flg &= (a[j][k] == 0); n       flg &= (a[j][n + i] > 0); n       if (flg) x = 0; n     } n     printf( %d n , x); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<int> st[100000], edge[200000], topo; n vector<pair<int, int> > ps; n int incnt[200000], tol; n priority_queue<int> que; n bool visit[200000]; n int main(void) { n   int n, m, in; n   while (scanf( %d %d , &n, &m) == 2) { n     for (int i = 0; i < m; ++i) st[i].clear(); n     for (int i = 0; i < n; ++i) n       for (int j = 0; j < m; ++j) { n         scanf( %d , &in); n         st[j].push_back(in); n       } n     memset(incnt, 0, sizeof(incnt)); n     tol = m; n     for (int i = 0; i < 200000; ++i) edge[i].clear(); n     for (int i = 0; i < n; ++i) { n       ps.clear(); n       for (int j = 0; j < m; ++j) n         if (st[j][i] >= 0) ps.push_back(make_pair(st[j][i], j)); n       sort(ps.begin(), ps.end()); n       for (int a = 0, b, k = ps.size(), pre = -1; a < k; a = b) { n         for (b = a; (b < k) && (ps[b].first == ps[a].first); ++b) { n           int now = ps[b].second; n           if (pre >= 0) { n             edge[pre].push_back(now); n             ++incnt[now]; n           } n           edge[now].push_back(tol); n           ++incnt[tol]; n         } n         pre = tol++; n       } n     } n     topo.clear(); n     for (int i = 0; i < tol; ++i) n       if (!incnt[i]) que.push(-i); n     while (!que.empty()) { n       int now = -que.top(); n       que.pop(); n       if (now < m) topo.push_back(now + 1); n       for (int i = 0, j = edge[now].size(); i < j; ++i) { n         --incnt[edge[now][i]]; n         if (!incnt[edge[now][i]]) que.push(-edge[now][i]); n       } n     } n     if (topo.size() != m) n       puts( -1 ); n     else { n       printf( %d , topo[0]); n       for (int i = 1, j = topo.size(); i < j; ++i) printf(  %d , topo[i]); n       putchar(  n ); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n map<string, int> mp; n map<int, string> mp_back; n vector<int> way[5005 * 2]; n int n, j, i, ans[5005 * 2] = {}; n int main() { n   cin >> n; n   int w = 0; n   for (i = 1; i <= n; i++) { n     int v1, v2; n     string s1, s2; n     cin >> s1 >> s2; n     if (!mp[s1]) n       ++w, mp[s1] = w, v1 = w, mp_back[w] = s1; n     else n       v1 = mp[s1]; n     if (!mp[s2]) n       ++w, mp[s2] = w, v2 = w, mp_back[w] = s2; n     else n       v2 = mp[s2]; n     way[v1].push_back(v2); n     way[v2].push_back(v1); n   } n   for (i = 1; i <= w; i++) { n     int friends[5005 * 2] = {}, friends2[5005 * 2] = {}; n     for (j = 0; j < way[i].size(); j++) friends[way[i][j]] = 1; n     int maxf = 0; n     for (j = 1; j <= w; j++) n       if (!friends[j] && j != i) { n         int size = 0; n         for (int h = 0; h < way[j].size(); h++) n           if (friends[way[j][h]]) ++size; n         maxf = max(maxf, size); n         friends2[j] = size; n       } n     for (j = 1; j <= w; j++) n       if (friends2[j] == maxf && !friends[j] && j != i) ans[i]++; n   } n   cout << w << endl; n   for (i = 1; i <= w; i++) cout << mp_back[i] <<     << ans[i] << endl; n   return 0; n } n


#include <bits/stdc++.h> n #pragma GCC optimize( O3 ) n #pragma GCC target( avx2 ) n #pragma comment(linker,  /stack:200000000 ) n #pragma GCC optimize( Ofast ) n #pragma GCC target( sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native ) n using namespace std; n void solve() { n   int n; n   cin >> n; n   vector<int> ans; n   for (int i = 1; i <= 1000; i += 2) { n     ans.push_back(i); n     ans.push_back(i); n   } n   if (ans.size() < n) { n     for (int i = 0; i < ans.size(); i++) { n       cout << ans[i] <<    ; n     } n     n -= ans.size(); n     for (int i = 0; i < n; i++) { n       cout << ans[i] <<    ; n     } n     cout << endl; n     return; n   } n   for (int i = int(0); i < int(n); i++) { n     cout << ans[i] <<    ; n   } n   cout << endl; n } n signed main() { n   int t; n   cin >> t; n   while (t--) { n     solve(); n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int a, b, c, i, j, sum, p[3][3]; n   stack<int> s; n   queue<int> q1, q2; n   for (i = 0; i < 3; i++) n     for (j = 0; j < 3; j++) p[i][j] = (j + 3 - i) % 3; n   while (s.size()) s.pop(); n   while (q1.size()) q1.pop(); n   while (q2.size()) q2.pop(); n   scanf( %d%d , &a, &b); n   while (a) { n     q1.push(a % 3); n     a /= 3; n   } n   while (b) { n     q2.push(b % 3); n     b /= 3; n   } n   if (q1.size() > q2.size()) n     for (i = q2.size(); i < q1.size(); i++) q2.push(0); n   else { n     if (q2.size() > q1.size()) n       for (i = q1.size(); i < q2.size(); i++) q1.push(0); n   } n   while (q1.size()) { n     a = q1.front(); n     q1.pop(); n     b = q2.front(); n     q2.pop(); n     s.push(p[a][b]); n   } n   sum = 0; n   while (s.size()) { n     c = s.top(); n     s.pop(); n     sum = sum * 3 + c; n   } n   printf( %d n , sum); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const double eps = 1e-7; n const int MOD = 1e9 + 7; n const long long INFLL = 0x3f3f3f3f3f3f3f3f; n const int INF = 0x3f3f3f3f; n const int MAXN = 4e5 + 5; n char s[MAXN]; n int vis[200], t[200]; n int check(int n, int l) { n   int need = (l / 2) * n, cnt = 0; n   for (int i = 0; i < 200; i++) cnt += vis[i] / 2; n   if (cnt >= need) return 1; n   return 0; n } n void solve(int n, int l) { n   vector<char> v1, v2; n   printf( %d n , n); n   for (int i = 0; i < 200; i++) { n     while (vis[i] >= 2) v1.push_back((char)i), vis[i] -= 2; n     if (vis[i]) v2.push_back((char)i); n   } n   for (int i = 0; i < n; i++) { n     int p = 0; n     while (p < l / 2) { n       s[p] = s[l - 1 - p] = v1[v1.size() - 1]; n       v1.pop_back(); n       p++; n     } n     if (l & 1) { n       if (v2.empty()) n         s[p] = v1[v1.size() - 1], v2.push_back(v1[v1.size() - 1]), n         v1.pop_back(); n       else n         s[p] = v2[v2.size() - 1], v2.pop_back(); n     } n     s[l] = 0; n     printf( %s  , s); n   } n } n int main() { n   int n; n   cin >> n; n   scanf( %s , s + 1); n   for (int i = 1; i <= n; i++) vis[s[i]]++; n   int maxn = INF; n   for (int i = 1; i * i <= n; i++) { n     if (n % i == 0) { n       if (check(i, n / i)) maxn = min(i, maxn); n       if (check(n / i, i)) maxn = min(n / i, maxn); n     } n   } n   solve(maxn, n / maxn); n } n


#include <bits/stdc++.h> n using namespace std; n int n, a[100001]; n vector<vector<pair<int, int>>> o[6], ans; n void relax(vector<int>& m) { n   o[m.size()].push_back({}); n   for (size_t i = 0; i < m.size(); i++) n     o[m.size()].back().push_back({m[i], m[(i + 1) % m.size()]}); n   m.resize(1); n } n void gao(int x, int y) { n   if (!y) ans.insert(ans.end(), o[x].begin(), o[x].end()); n   while (o[x].size() > (x == y) && o[y].size()) { n     ans.push_back(o[x].back()); n     o[x].pop_back(); n     ans.back().insert(ans.back().end(), o[y].back().begin(), o[y].back().end()); n     o[y].pop_back(); n   } n } n int main() { n   scanf( %d , &n); n   for (int i = 1; i <= n; i++) scanf( %d , a + i); n   for (int i = 1; i <= n; i++) n     if (a[i] != i) { n       vector<int> u = {i}, m = {i}; n       while (a[u.back()] != i) u.push_back(a[u.back()]); n       for (size_t c = 1; c < u.size(); c++) { n         if (m.size() == 5) relax(m); n         m.push_back(a[u[c]] = u[c]); n       } n       relax(m); n     } n   gao(5, 0); n   gao(4, 0); n   gao(2, 3); n   while (o[3].size() >= 2) { n     auto u = o[3].back(); n     o[3].pop_back(); n     o[2].push_back({{u[0].first, u[1].first}, {u[1].first, u[0].first}}); n     o[2].push_back({{u[1].first, u[2].first}, {u[2].first, u[1].first}}); n     gao(2, 3); n   } n   gao(2, 2); n   gao(3, 0); n   gao(2, 0); n   printf( %d n , ans.size()); n   for (auto& c : ans) { n     printf( %d n , c.size()); n     for (auto x : c) printf( %d  , x.first); n     puts(  ); n     for (auto x : c) printf( %d  , x.second); n     puts(  ); n   } n } n


#include <bits/stdc++.h> n using namespace std; n int n, m, tot, a[100010], ans[100010]; n vector<pair<int, int> > q[100010]; n set<int> s; n int main() { n   scanf( %d%d , &n, &m); n   for (int i = 1; i <= n; i++) scanf( %d , &a[i]); n   for (int i = 1, l, x; i <= m; i++) { n     scanf( %d%d , &l, &x); n     q[l].emplace_back(i, x); n   } n   s.insert(0); n   tot = 1; n   for (int i = 1; i <= n; i++) { n     if (s.count(a[i])) n       tot = 1ll * tot * 2 % 1000000007; n     else { n       vector<int> v; n       for (int u : s) v.push_back(u ^ a[i]); n       for (int u : v) s.insert(u); n     } n     for (auto &p : q[i]) ans[p.first] = s.count(p.second) ? tot : 0; n   } n   for (int i = 1; i <= m; i++) printf( %d n , ans[i]); n } n


#include <bits/stdc++.h> n using namespace std; n template <typename Arg1> n void __f(const char* name, Arg1&& arg1) { n   std::cerr << name <<   :   << arg1 << endl; n } n template <typename Arg1, typename... Args> n void __f(const char* names, Arg1&& arg1, Args&&... args) { n   const char* comma = strchr(names + 1,  , ); n   std::cerr.write(names, comma - names) <<   :   << arg1 <<   |  ; n   __f(comma + 1, args...); n } n long long int solve() { n   long long int n; n   cin >> n; n   std::vector<long long int> v; n   long long int seg = 0; n   long long int count = 0; n   for (long long int i = 0; i < n; i++) { n     long long int a; n     cin >> a; n     v.push_back(a); n     if (a != i + 1) { n       count++; n     } else { n       if (count) { n         seg++; n         count = 0; n       } n     } n   } n   if (count) { n     seg++; n   } n   if (seg == 0) { n     cout << 0 <<   n ; n   } else if (seg == 1) { n     cout << 1 <<   n ; n   } else { n     cout << 2 <<   n ; n   } n   return 0; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   long long int T = 1; n   cin >> T; n   while (T--) { n     solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long n, sav, mn = 1e9; n struct point { n   long long x, y, uz; n } p[2005]; n string s; n int det(point A, point B, point C) { n   if (A.x * B.y + A.y * C.x + B.x * C.y - A.x * C.y - A.y * B.x - B.y * C.x < 0) n     return -1; n   return 1; n } n int main() { n   ios::sync_with_stdio(false); n   cin >> n; n   for (int i = 1; i <= n; i++) { n     cin >> p[i].x >> p[i].y; n     if (p[i].y < mn) { n       mn = p[i].y; n       sav = i; n     } n   } n   cin >> s; n   p[sav].uz = 1; n   cout << sav <<    ; n   for (int i = 0; i < s.size(); i++) { n     int crt = 1, j, sgn; n     while (p[crt].uz) crt++; n     j = crt + 1; n     if (s[i] ==  R ) n       sgn = 1; n     else n       sgn = -1; n     while (j <= n) { n       while (j <= n && p[j].uz) j++; n       if (j > n) break; n       if (det(p[sav], p[crt], p[j]) == sgn) crt = j; n       j++; n     } n     sav = crt; n     p[crt].uz = 1; n     cout << sav <<    ; n   } n   for (int i = 1; i <= n; i++) n     if (!p[i].uz) cout << i <<    ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct node { n   int va, id; n } a[200015], b[200015]; n int n, Q, rv[200015]; n long long dp[200015]; n inline bool cmp(node x, node y) { return x.va > y.va; } n int main() { n   scanf( %d%d , &n, &Q); n   for (int i = 1; i <= n; i++) scanf( %d , &a[i].va), a[i].id = i; n   for (int i = 1; i <= n; i++) scanf( %d , &b[i].va), b[i].id = i; n   sort(a + 1, a + 1 + n, cmp); n   sort(b + 1, b + 1 + n, cmp); n   for (int i = 1; i <= n; i++) rv[b[i].id] = i; n   while (Q--) { n     int x, y; n     scanf( %d%d , &x, &y); n     swap(rv[x], rv[y]); n     b[rv[x]].id = x, b[rv[y]].id = y; n     for (int i = 1; i <= n; i++) { n       dp[i] = 0; n       if (a[i].id ^ b[i].id) n         dp[i] = max(dp[i], dp[i - 1] + 1ll * a[i].va * b[i].va); n       if (i > 1) { n         if ((a[i].id ^ b[i - 1].id) && (a[i - 1].id ^ b[i].id)) { n           dp[i] = max(dp[i], dp[i - 2] + 1ll * a[i].va * b[i - 1].va + n                                  1ll * a[i - 1].va * b[i].va); n         } n       } n       if (i > 2) { n         if ((a[i].id ^ b[i - 2].id) && (a[i - 1].id ^ b[i].id) && n             (a[i - 2].id ^ b[i - 1].id)) { n           dp[i] = max(dp[i], dp[i - 3] + 1ll * a[i - 2].va * b[i - 1].va + n                                  1ll * a[i - 1].va * b[i].va + n                                  1ll * a[i].va * b[i - 2].va); n         } n         if ((a[i - 2].id ^ b[i].id) && (a[i - 1].id ^ b[i - 2].id) && n             (a[i].id ^ b[i - 1].id)) { n           dp[i] = max(dp[i], dp[i - 3] + 1ll * a[i - 2].va * b[i].va + n                                  1ll * a[i - 1].va * b[i - 2].va + n                                  1ll * a[i].va * b[i - 1].va); n         } n         if ((a[i].id ^ b[i - 2].id) && (a[i - 2].id ^ b[i].id) && n             (a[i - 1].id ^ b[i - 1].id)) { n           dp[i] = max(dp[i], dp[i - 3] + 1ll * a[i].va * b[i - 2].va + n                                  1ll * a[i - 1].va * b[i - 1].va + n                                  1ll * a[i - 2].va * b[i].va); n         } n       } n     } n     printf( %lld n , dp[n]); n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   cin >> n; n   vector<int> a; n   vector<string> ss; n   bool possible = false; n   for (int i = 0; i < n; ++i) { n     string s; n     cin >> s; n     if (!possible) { n       if (s.substr(0, 2) ==  OO ) { n         possible = true; n         s[0] =  + ; n         s[1] =  + ; n       } else if (s.substr(3, 2) ==  OO ) { n         possible = true; n         s[3] =  + ; n         s[4] =  + ; n       } n     } n     ss.push_back(s); n   } n   if (possible) { n     cout <<  YES n ; n     for (int j = 0; j < n; ++j) { n       cout << ss[j] <<   n ; n     } n   } else { n     cout <<  NO ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long int maxd(long long int n) { n   long long int temp = 10 * n, mm = 0; n   while (temp /= 10) mm = max(mm, temp % 10); n   return mm; n } n int main() { n   long long int n, ans = 0; n   cin >> n; n   while (n) { n     n -= maxd(n); n     ans++; n   } n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int inf = 1e9 + 1; n const int N = 100123; n int z, n; n int a[N]; n int best[N]; n long long ans[N]; n long long res; n pair<int, int> tree[N << 2]; n void build(int v, int l, int r) { n   if (l == r) { n     tree[v] = {a[l], l}; n     return; n   } n   int mid = (l + r) >> 1; n   build(2 * v, l, mid); n   build(2 * v + 1, mid + 1, r); n   tree[v] = max(tree[2 * v], tree[2 * v + 1]); n } n pair<int, int> query(int v, int l, int r, int ql, int qr) { n   if (ql <= l && qr >= r) { n     return tree[v]; n   } n   int mid = (l + r) >> 1; n   pair<int, int> res = {0, 0}; n   if (ql <= mid) res = max(res, query(2 * v, l, mid, ql, qr)); n   if (qr >= mid + 1) res = max(res, query(2 * v + 1, mid + 1, r, ql, qr)); n   return res; n } n int main() { n   ios_base::sync_with_stdio(0); n   cin >> n; n   for (int i = 1; i <= n - 1; i++) { n     cin >> a[i]; n   } n   a[n] = n; n   build(1, 1, n); n   for (int i = 1; i <= n; i++) { n     pair<int, int> b = query(1, 1, n, i, a[i]); n     best[i] = b.second; n   } n   ans[n - 1] = 1; n   res++; n   for (int i = n - 2; i >= 1; i--) { n     ans[i] = ans[best[i]] + (best[i] - i) + (n - a[i]); n     res += ans[i]; n   } n   cout << res << endl; n } n


#include <bits/stdc++.h> n using namespace std; n const long long md = 1e9 + 7; n const int N = 2e6 + 34; n char s[N]; n long long p[N]; n int n, k, m; n int lst[100]; n int c, cur, mn; n int main() { n   scanf( %d %d n , &n, &k); n   gets(s); n   m = strlen(s); n   for (int i = 0; i < k; i++) lst[i] = -1; n   p[0] = 0; n   p[1] = 1; n   for (int i = 0; i < m; i++) { n     c = s[i] -  a ; n     cur = p[i + 1] - p[lst[c] + 1]; n     p[i + 2] = (p[i + 1] + cur + md) % md; n     lst[c] = i; n   } n   for (int i = m; i < m + n; i++) { n     mn = 0; n     for (int j = 1; j < k; j++) n       if (lst[j] < lst[mn]) mn = j; n     c = mn; n     cur = p[i + 1] - p[lst[c] + 1]; n     p[i + 2] = (p[i + 1] + cur + md) % md; n     lst[c] = i; n   } n   cout << p[n + m + 1]; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n map<string, string> mp; n set<string> st; n int n; n char in[30]; n string read() { n   scanf(  %s , in); n   return (string)in; n } n int main() { n   scanf( %d , &n); n   for (int i = 1; i <= n; i += 1) { n     string a = read(); n     string b = read(); n     if (st.find(b) != st.end()) { n       continue; n     } n     st.insert(a); n     if (mp.find(a) != mp.end()) { n       mp[b] = mp[a]; n       mp.erase(a); n       continue; n     } n     mp[b] = a; n   } n   printf( %d n , mp.size()); n   for (auto x : mp) { n     if (x.second ==  _ ) { n       continue; n     } n     printf( %s %s n , x.second.c_str(), x.first.c_str()); n   } n } n


#include <bits/stdc++.h> n using namespace std; n int n, m; n vector<vector<int>> p; n bool is(int val) { n   int c = 0; n   vector<bool> pres(n, 0); n   bool flag = 0; n   for (int i = 0; i < m; i++) { n     c = 0; n     for (int j = 0; j < n; j++) { n       if (p[i][j] >= val) { n         pres[j] = true; n         c++; n       } n     } n     if (c > 1) flag = 1; n   } n   if (!flag) return false; n   bool ans = 1; n   for (bool i : pres) n     if (!i) return false; n   return true; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   int t; n   cin >> t; n   while (t--) { n     cin >> m >> n; n     p.assign(m, vector<int>(n)); n     int minn = INT_MAX, maxx; n     for (int i = 0; i < m; i++) n       for (int j = 0; j < n; j++) cin >> p[i][j]; n     for (int i = 0; i < n; i++) { n       maxx = -1; n       for (int j = 0; j < m; j++) { n         maxx = max(maxx, p[j][i]); n       } n       minn = min(maxx, minn); n     } n     int l = 1, r = minn + 3; n     while (r - l > 1) { n       int mid = (l + r) / 2; n       if (is(mid)) n         l = mid; n       else n         r = mid; n     } n     cout << l <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int dp[80][80][80][80]; n int dfs(int l1, int r1, int l2, int r2, int dep) { n   if (l1 > r1 || l2 > r2) return 0; n   if (l1 == l2 && r1 == r2) return r1 - l1 + 1; n   if (r1 < 80 && r2 < 80 && dp[l1][r1][l2][r2] != -1) return dp[l1][r1][l2][r2]; n   int mid = (1 << dep - 1) - 1; n   int res = 0; n   if (l1 <= mid && mid <= r1 && l2 <= mid && mid <= r2) { n     res = max(res, min(r1, r2) - max(l1, l2) + 1); n   } n   int l1_next[] = {l1, max(l1, mid + 1) - (mid + 1)}; n   int r1_next[] = {min(r1, mid - 1), r1 - (mid + 1)}; n   int l2_next[] = {l2, max(l2, mid + 1) - (mid + 1)}; n   int r2_next[] = {min(r2, mid - 1), r2 - (mid + 1)}; n   for (int i = 0; i < (2); i++) n     for (int j = 0; j < (2); j++) n       res = max(res, n                 dfs(l1_next[i], r1_next[i], l2_next[j], r2_next[j], dep - 1)); n   if (r1 < 80 && r2 < 80) dp[l1][r1][l2][r2] = res; n   return res; n } n int main() { n   int l1, r1, l2, r2; n   scanf( %d%d%d%d , &l1, &r1, &l2, &r2); n   l1--; n   r1--; n   l2--; n   r2--; n   memset(dp, -1, sizeof dp); n   printf( %d n , dfs(l1, r1, l2, r2, 30)); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 4e5 + 123; n long long n, m, k; n int num[maxn]; n int dp[maxn][5]; n int presum[maxn]; n int tmp[maxn]; n int main(int argc, char const *argv[]) { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   std::cin >> n; n   for (int i = 1; i <= n; i++) { n     long long u; n     std::cin >> u; n     int one = 0; n     while (u) { n       if (u % 2 == 1) { n         one++; n       } n       u /= 2; n     } n     num[i] = one; n   } n   long long ans = 0; n   for (int i = 1; i <= n; i++) { n     dp[i][1] = dp[i - 1][1]; n     dp[i][0] = dp[i - 1][0]; n     presum[i] = presum[i - 1] + num[i]; n     if (presum[i] % 2 == 1) { n       dp[i][1] += 1; n     } else { n       dp[i][0] += 1; n     } n   } n   int cur = n + 1; n   for (int i = n; i >= 0; --i) { n     tmp[i] = cur; n     if (num[i] != 0) { n       cur = i; n     } n   } n   for (int i = 1; i <= n; i++) { n     int k = presum[i - 1] % 2; n     int cur = i; n     int sum = num[i]; n     int maxx = num[i]; n     for (int j = 1; j < 71; j++) { n       int kk = tmp[cur]; n       if (maxx <= sum / 2) { n         ans += dp[kk - 1][k] - dp[cur - 1][k]; n       } n       maxx = max(maxx, num[kk]); n       sum += num[kk]; n       cur = kk; n       if (cur > n) { n         break; n       } n     } n     ans += dp[n][k] - dp[cur - 1][k]; n   } n   std::cout << ans <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long int cnt(long long int x) { n   long long int ans = 0; n   while (x) { n     x /= 2; n     ans++; n   } n   return ans; n } n int main() { n   int t; n   cin >> t; n   while (t--) { n     long long int d, m; n     cin >> d >> m; n     long long int temp = cnt(d) - 1; n     long long int ans = d - pow(2, temp) + 2; n     for (long long int i = temp - 1; i >= 0; i--) { n       ans = ans * (pow(2, i) + 1); n       ans %= m; n     } n     ans = (ans - 1) % m; n     if (ans < 0) ans = m + ans; n     cout << ans << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n inline int nextint() { n   int res = 0; n   char c = 0; n   while (c <  0  || c >  9 ) c = getchar(); n   while (c >=  0  && c <=  9 ) { n     res = res * 10 + c -  0 ; n     c = getchar(); n   } n   return res; n } n int n; n long long a[200000], b[200000]; n bool good[200000]; n bool ans[200000]; n long long diff[200000]; n const int maxh = 17; n const int maxn = (1 << maxh); n long long tree[2 * maxn]; n void build() { n   for (int i = maxn - 1; i; i--) tree[i] = min(tree[i + i], tree[i + i + 1]); n } n long long get(int i, int j) { n   long long result = (long long)1e18; n   for (i += maxn - 1, j += maxn; i + 1 != j; i >>= 1, j >>= 1) { n     if (!(i & 1)) result = min(result, tree[i + 1]); n     if (j & 1) result = min(result, tree[j - 1]); n   } n   return result; n } n long long get_min(int i, int j) { n   long long result = (long long)1e18; n   if (i <= j) result = get(i + 1, j + 2); n   return result; n } n void go() { n   memset(tree, 0, sizeof(tree)); n   for (int i = 0; i < (int)(n); ++i) n     tree[maxn + i + 1] = tree[maxn + i] + diff[i]; n   build(); n   for (int i = 0; i < (int)(n); ++i) { n     long long presum = tree[maxn + i]; n     long long postsum = tree[maxn + n] - presum; n     long long m_main = get_min(i, n - 1); n     long long m_oth = get_min(0, i - 1); n     bool ok = true; n     if (-presum + m_main < 0) ok = false; n     if (postsum + m_oth < 0) ok = false; n     good[i] = ok; n   } n } n int main() { n   cin >> n; n   for (int i = 0; i < (int)(n); ++i) { n     int _a; n     scanf( %d , &_a); n     a[i] = _a; n   } n   for (int i = 0; i < (int)(n); ++i) { n     int _b; n     scanf( %d , &_b); n     b[i] = _b; n   } n   for (int i = 0; i < (int)(n); ++i) diff[i] = a[i] - b[i]; n   go(); n   for (int i = 0; i < (int)(n); ++i) n     if (good[i]) ans[i] = true; n   memset(good, 0, sizeof(good)); n   reverse(a + 1, a + n); n   reverse(b, b + n); n   for (int i = 0; i < (int)(n); ++i) diff[i] = a[i] - b[i]; n   go(); n   for (int i = 0; i < (int)(n); ++i) n     if (good[i]) ans[(n - i) % n] = true; n   int ans_size = 0; n   for (int i = 0; i < (int)(n); ++i) n     if (ans[i]) ans_size++; n   cout << ans_size << endl; n   for (int i = 0; i < (int)(n); ++i) n     if (ans[i]) cout << (i + 1) <<    ; n   cout << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<int> edges[8]; n int calc(int a, int b) { n   vector<int> cnt(8); n   for (auto e : edges[a]) cnt[e]++; n   for (auto e : edges[b]) cnt[e]++; n   int sol = 0; n   for (int i = 1; i <= 7; i++) n     if (cnt[i] == 2) sol++; n   return sol; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   int n, m, a, b; n   cin >> n >> m; n   for (int i = 0; i < m; i++) { n     cin >> a >> b; n     edges[a].push_back(b); n     edges[b].push_back(a); n   } n   if (n <= 6) { n     cout << m <<   n ; n     return 0; n   } n   int mmin = 1e9; n   for (int i = 1; i <= n; i++) { n     for (int j = i + 1; j <= n; j++) { n       mmin = min(mmin, calc(i, j)); n     } n   } n   cout << m - mmin <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 300010; n const int M = 1000010; n const int MOD = 1000000007; n const int INF = 2e9; n const double eps = 1e-8; n const double PI = acos(-1.0); n int fx[] = {1, -1, 0, 0}; n int fy[] = {0, 0, 1, -1}; n int gcd(int a, int b) { return b ? gcd(b, a % b) : a; } n int lcm(int x, int y) { n   x /= gcd(x, y); n   return x * y; n } n int Min(int a, int b) { return a < b ? a : b; } n int Max(int a, int b) { return a > b ? a : b; } n bool prime[M + 5], isprime[M + 5]; n vector<int> v; n int n, m, q, cs, ar[123456]; n void sieve() { n   v.push_back(2); n   prime[1] = 1; n   int limit = (int)sqrt(M); n   for (int i = 4; i <= M; i += 2) prime[i] = 1; n   for (int i = 3; i <= M; i += 2) { n     if (!prime[i]) { n       v.push_back(i); n       if (i <= limit) { n         for (int j = i * i; j <= M; j += 2 * i) prime[j] = 1; n       } n     } n   } n } n int br[123456]; n int main() { n   long long n, tot = 0; n   cin >> n; n   for (int i = 0; i < n; i++) { n     cin >> ar[i]; n     tot += ar[i]; n   } n   for (int i = 0; i < n; i++) cin >> br[i]; n   sort(br, br + n); n   long long a = br[n - 1], b = br[n - 2]; n   if (tot <= a + b) n     cout <<  YES ; n   else n     cout <<  NO ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, e = 0, st[100005], d[100005], T[100005], s[100005], Time; n struct Node { n   int v, t; n   double p; n }; n struct { n   int v, t, nx; n } edge[200005]; n vector<Node> E[100005]; n bool cmp(Node a, Node b) { return a.p < b.p; } n void add(int u, int v, int t) { n   edge[e].v = v; n   edge[e].t = t; n   edge[e].nx = st[u]; n   st[u] = e++; n } n void dfs(int u, int fa, int t) { n   s[u] = 1; n   d[u] = 2 * t; n   for (int i = st[u]; ~i; i = edge[i].nx) { n     int v = edge[i].v; n     if (v != fa) { n       Node tmp; n       t = edge[i].t; n       tmp.v = v; n       tmp.t = t; n       E[u].push_back(tmp); n       dfs(v, u, t); n       s[u] += s[v]; n       d[u] += d[v]; n     } n   } n } n void Dfs(int u) { n   int m = E[u].size(); n   for (int i = 0; i < m; i++) n     E[u][i].p = (double)d[E[u][i].v] / (double)s[E[u][i].v]; n   sort(E[u].begin(), E[u].end(), cmp); n   for (int i = 0; i < m; i++) { n     int v = E[u][i].v; n     int t = E[u][i].t; n     Time += t; n     T[v] = Time; n     Dfs(v); n     Time += t; n   } n } n int main() { n   while (~scanf( %d , &n)) { n     e = 0; n     memset(st, -1, sizeof(st)); n     int a, b, t; n     for (int i = 1; i < n; i++) { n       scanf( %d%d%d , &a, &b, &t); n       add(a, b, t); n       add(b, a, t); n     } n     dfs(1, -1, 0); n     Time = 0; n     Dfs(1); n     double ans = 0; n     for (int i = 2; i <= n; i++) ans += 1.0 * T[i]; n     printf( %.12f n , ans / 1.0 / (n - 1)); n   } n } n


#include <bits/stdc++.h> n const int INT_INF = 0x3f3f3f3f; n const int INT_M_INF = 0x7f7f7f7f; n const long long LL_INF = 0x3f3f3f3f3f3f3f3f; n const long long LL_M_INF = 0x7f7f7f7f7f7f7f7f; n const double pi = acos(-1.0); n using namespace std; n inline void debug(char ch) { n   for (int __ii = 0; __ii < 20; ++__ii) putchar(ch); n   printf(  n ); n } n const double eps = 1e-8; n inline int dcmp(double a, double b) { n   if (fabs(a - b) < eps) return 0; n   return a < b ? -1 : 1; n } n const int dr[] = {0, 0, -1, 1, -1, -1, 1, 1}; n const int dc[] = {-1, 1, 0, 0, -1, 1, -1, 1}; n const int MAXN = 4000 + 10; n const int MAXT = 10000 + 10; n char s[MAXN], t[MAXN]; n int main() { n   memset(s, 0, sizeof s); n   memset(t, 0, sizeof t); n   while (scanf( %s%s , s, t) == 2) { n     int slen = strlen(s), tlen = strlen(t), ans = 0; n     for (int i = 0; i < slen; ++i) { n       int num = 0; n       for (int j = 0; j < tlen; ++j) n         if (s[i + j] == t[j]) ++num; n       ans = max(ans, num); n     } n     for (int i = 0; i < tlen; ++i) { n       int num = 0; n       for (int j = 0; j < slen; ++j) n         if (t[i + j] == s[j]) ++num; n       ans = max(ans, num); n     } n     printf( %d n , tlen - ans); n     memset(s, 0, sizeof s); n     memset(t, 0, sizeof t); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int nump; n   int arr[7]; n   scanf( %d , &nump); n   scanf( %d , &arr[0]); n   for (int i = 1; i < 7; i++) { n     scanf( %d , &arr[i]); n     arr[i] += arr[i - 1]; n   } n   int tmp = (nump / arr[6]), idx = 0; n   if (tmp && arr[6] * tmp >= nump) n     tmp = (tmp - 1) * arr[6]; n   else n     tmp *= arr[6]; n   while (tmp + arr[idx] < nump) idx++; n   printf( %d n , idx + 1); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   string s, s1, s2; n   getline(cin, s); n   getline(cin, s1); n   getline(cin, s2); n   int substr = s.find(s1), substr1; n   int forward = 0, backward = 0; n   if (substr != -1) { n     substr1 = s.find(s2, substr + s1.length()); n     if (substr1 != -1) forward = 1; n   } n   string inverted; n   for (substr = 0; substr <= s.length() - 1; substr++) n     inverted = s[substr] + inverted; n   substr = inverted.find(s1); n   if (substr != -1) { n     substr1 = inverted.find(s2, substr + s1.length()); n     if (substr1 != -1) backward = 1; n   } n   if (forward == 1 && backward == 0) cout <<  forward  << endl; n   if (forward == 0 && backward == 1) cout <<  backward  << endl; n   if (forward == 1 && backward == 1) cout <<  both  << endl; n   if (forward == 0 && backward == 0) cout <<  fantasy  << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int Maxn = 50005; n char str[Maxn]; n int hs[26]; n int main() { n   scanf( %s , str); n   int len = strlen(str), cCnt = 0; n   int xNum = 0, sta = 0; n   bool ok = false; n   int i; n   for (i = 0; i < len; i++) { n     if (i - sta >= 26 && cCnt + xNum == 26) { n       ok = true; n       for (int j = sta; j < sta + 26; j++) { n         if (str[j] ==  ? ) { n           for (int k = 0; k < 26; k++) { n             if (hs[k] == 0) { n               hs[k] = 1; n               str[j] =  A  + k; n               break; n             } n           } n         } n       } n       break; n     } n     if (str[i] ==  ? ) n       xNum++; n     else { n       if (hs[str[i] -  A ] == 0) { n         hs[str[i] -  A ] = 1; n         cCnt++; n       } else { n         hs[str[i] -  A ]++; n         cCnt++; n         for (int j = sta; j < i; j++) { n           if (str[j] == str[i]) { n             hs[str[j] -  A ]--; n             cCnt--; n             sta = j + 1; n             break; n           } else { n             if (str[j] ==  ? ) n               xNum--; n             else { n               hs[str[j] -  A ]--; n               cCnt--; n             } n           } n         } n       } n     } n   } n   if (!ok && i - sta >= 26 && cCnt + xNum == 26) { n     ok = true; n     for (int j = sta; j < sta + 26; j++) { n       if (str[j] ==  ? ) { n         for (int k = 0; k < 26; k++) { n           if (hs[k] == 0) { n             hs[k] = 1; n             str[j] =  A  + k; n             break; n           } n         } n       } n     } n   } n   if (ok) { n     for (int i = 0; i < len; i++) { n       if (str[i] ==  ? ) str[i] =  A ; n     } n     printf( %s n , str); n   } else { n     puts( -1 ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 1e5 + 30; n int n, a[maxn]; n long long k, res; n bool check(long long x) { n   long long sum = 0; n   for (int i = 0; i < n; ++i) { n     sum += min((long long)a[i], x); n     if (sum > k) return false; n   } n   return true; n } n int main() { n   scanf( %d%I64d , &n, &k); n   long long sum = 0; n   for (int i = 0; i < n; ++i) { n     scanf( %d , &a[i]); n     sum += a[i]; n   } n   if (k > sum) { n     printf( -1 n ); n     return 0; n   } n   long long l = 0, r = sum; n   while (l <= r) { n     long long m = l + (r - l) / 2; n     if (check(m)) n       l = m + 1; n     else n       r = m - 1; n   } n   sum = 0; n   for (int i = 0; i < n; ++i) sum += min((long long)a[i], r); n   res = k - sum; n   int st = 0, flag = 1; n   for (int i = 0; i < n; ++i) { n     a[i] -= min((long long)a[i], r); n     if (a[i]) { n       if (res == 0 && flag) { n         flag = 0; n         st = i; n       } else if (res > 0) { n         --res; n         --a[i]; n       } n     } n   } n   for (int i = st; i < n; ++i) n     if (a[i]) printf( %d  , i + 1); n   for (int i = 0; i < st; ++i) n     if (a[i]) printf( %d  , i + 1); n   puts(  ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int x[3], y[3], r[3]; n double ansx, ansy; n bool bisecting_normal(double x1, double y1, double x2, double y2, double &a, n                       double &b, double &c) { n   double xm = (x1 + x2) * 0.5; n   double ym = (y1 + y2) * 0.5; n   double xl = x2 - x1; n   double yl = y2 - y1; n   if (fabs(xl) < 1.0e-10 && fabs(yl) < 1.0e-10) return false; n   a = xl; n   b = yl; n   c = -xl * xm - yl * ym; n   return true; n } n bool dividing_locus(double x1, double y1, double r1, double x2, double y2, n                     double r2, double &xa, double &ya, double &ra) { n   double a, b, c; n   if (fabs(r1 - r2) < 1.0e-10) return false; n   a = -r1 * r1; n   b = r2 * r2; n   c = a + b; n   a /= c; n   b /= c; n   xa = x1 * b + x2 * a; n   ya = y1 * b + y2 * a; n   ra = fabs(r1 * r2 / c) * sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2)); n   return true; n } n bool line2line() { n   double a, b, c, d, e, f, g; n   bisecting_normal(x[0], y[0], x[1], y[1], a, b, c); n   bisecting_normal(x[0], y[0], x[2], y[2], d, e, f); n   g = a * e - b * d; n   if (fabs(g) < 1.0e-10) return false; n   ansx = (b * f - e * c) / g; n   ansy = (d * c - a * f) / g; n   return true; n } n bool line2circle(int z) { n   int z1, z2; n   double a, b, c, d, e, s, t, u, n, xm, ym, xa1, xa2, ya1, ya2, d1, d2; n   z1 = (z + 1) % 3; n   z2 = (z + 2) % 3; n   bisecting_normal(x[z1], y[z1], x[z2], y[z2], a, b, c); n   dividing_locus(x[z], y[z], r[z], x[z1], y[z1], r[z1], s, t, u); n   n = sqrt(a * a + b * b); n   d = (a * s + b * t + c) / n; n   if (d > u) return false; n   xm = s - d * a / n; n   ym = t - d * b / n; n   e = sqrt(u * u - d * d); n   xa1 = xm + e * b / n; n   ya1 = ym - e * a / n; n   xa2 = xm - e * b / n; n   ya2 = ym + e * a / n; n   d1 = (xa1 - x[z]) * (xa1 - x[z]) + (ya1 - y[z]) * (ya1 - y[z]); n   d2 = (xa2 - x[z]) * (xa2 - x[z]) + (ya2 - y[z]) * (ya2 - y[z]); n   if (d1 < d2) { n     ansx = xa1; n     ansy = ya1; n   } else { n     ansx = xa2; n     ansy = ya2; n   } n   return true; n } n bool circle2circle() { n   double a, b, c, d, f, e, s1, t1, u1, s2, t2, u2, xm, ym, xv, yv, xa1, xa2, n       ya1, ya2, d1, d2; n   dividing_locus(x[0], y[0], r[0], x[1], y[1], r[1], s1, t1, u1); n   dividing_locus(x[0], y[0], r[0], x[2], y[2], r[2], s2, t2, u2); n   xv = t2 - t1; n   yv = s1 - s2; n   d = xv * xv + yv * yv; n   if ((u1 + u2) * (u1 + u2) < d || (u1 - u2) * (u1 - u2) > d) return false; n   a = d + u1 * u1 - u2 * u2; n   b = d - u1 * u1 + u2 * u2; n   c = a + b; n   a /= c; n   b /= c; n   xm = s1 * b + s2 * a; n   ym = t1 * b + t2 * a; n   d = sqrt(d); n   xv /= d; n   yv /= d; n   f = (d * d + u1 * u1 - u2 * u2) / (2. * d); n   e = fabs(sqrt(u1 * u1 - f * f)); n   xa1 = xm + e * xv; n   ya1 = ym + e * yv; n   xa2 = xm - e * xv; n   ya2 = ym - e * yv; n   d1 = (xa1 - x[0]) * (xa1 - x[0]) + (ya1 - y[0]) * (ya1 - y[0]); n   d2 = (xa2 - x[0]) * (xa2 - x[0]) + (ya2 - y[0]) * (ya2 - y[0]); n   if (d1 < d2) { n     ansx = xa1; n     ansy = ya1; n   } else { n     ansx = xa2; n     ansy = ya2; n   } n   return true; n } n int main() { n   bool exists; n   for (int i = 0; i < 3; i++) { n     cin >> x[i] >> y[i] >> r[i]; n   } n   if (r[0] == r[1]) { n     if (r[0] == r[2]) { n       exists = line2line(); n     } else { n       exists = line2circle(2); n     } n   } else { n     if (r[0] == r[2]) { n       exists = line2circle(1); n     } else { n       exists = circle2circle(); n     } n   } n   if (exists) { n     cout << fixed << setprecision(5) << ansx <<     << ansy << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long inf = 2000000002; n pair<long long, long long> p[100005]; n long long minl[100005], maxl[100005], minr[100005], maxr[100005]; n int main() { n   int i, j, n, a, b; n   while (~scanf( %d , &n)) { n     for (i = 0; i < n; i++) { n       scanf( %d%d , &a, &b); n       p[i] = make_pair(a - b, a + b); n     } n     sort(p, p + n); n     minl[0] = maxl[0] = p[0].second; n     for (i = 1; i < n; i++) n       minl[i] = min(minl[i - 1], p[i].second), n       maxl[i] = max(maxl[i - 1], p[i].second); n     minr[n - 1] = maxr[n - 1] = p[n - 1].second; n     for (i = n - 2; i >= 0; i--) n       minr[i] = min(minr[i + 1], p[i].second), n       maxr[i] = max(maxr[i + 1], p[i].second); n     long long l = 0, r = 4 * inf; n     while (l < r) { n       long long s = (l + r) / 2; n       bool can = 0; n       j = 0; n       for (i = 0; i < n; i++) { n         while (j < n - 1 && p[j + 1].first - p[i].first <= s) j++; n         if (i == 0 && j == n - 1) { n           can = true; n           break; n         } n         long long miny = inf, maxy = -inf; n         if (i > 0) miny = min(minl[i - 1], miny), maxy = max(maxl[i - 1], maxy); n         if (j < n - 1) n           miny = min(miny, minr[j + 1]), maxy = max(maxr[j + 1], maxy); n         if (maxy - miny <= s) can = true; n         if (can) break; n       } n       if (can) n         r = s; n       else n         l = s + 1; n     } n     double t = l * 0.5; n     printf( %.8lf n , t); n   } n   return 0; n } n


#include <bits/stdc++.h> n int map[2001][2001] = {0}; n int ans[2001] = {0}; n int n, m, k; n char ss[2001]; n void show() { n   int i, j; n   for (i = 0; i < n; i++) { n     for (j = 0; j < m; j++) { n       printf( %d , map[i][j]); n     } n     printf(  n ); n   } n } n int main() { n   int i, j, k; n   scanf( %d %d %d , &n, &m, &k); n   for (i = 0; i < n; i++) { n     scanf( %s , ss); n     for (j = 0; j < m; j++) { n       if (ss[j] ==  . ) { n         map[i][j] = 0; n       } else if (ss[j] ==  L ) { n         map[i][j] = 1; n       } else if (ss[j] ==  R ) { n         map[i][j] = 2; n       } else if (ss[j] ==  U ) { n         map[i][j] = 4; n       } else { n         map[i][j] = 8; n       } n     } n   } n   for (i = 0; i < n; i++) { n     for (j = 0; j < m; j++) { n       if (map[i][j] == 1) { n         if (i <= j) ans[j - i]++; n       } else if (map[i][j] == 2) { n         if (i < m - j) ans[i + j]++; n       } else if (map[i][j] == 4) { n         if (!(i & 1)) ans[j]++; n       } else if (map[i][j] == 8) { n       } n     } n   } n   printf( %d , ans[0]); n   for (i = 1; i < m; i++) { n     printf(  %d , ans[i]); n   } n   printf(  n ); n   return 0; n } n


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


#include <bits/stdc++.h> n using namespace std; n const long long MOD = 1e9 + 7; n const int N = 1e5 + 7, M = 1e7, OO = 0x3f3f3f3f; n int main() { n   int n, m, array1[100][100], mini, maxi = -OO, i, j; n   scanf( %d %d , &n, &m); n   for (i = 0; i < n; ++i) { n     for (j = 0; j < m; ++j) { n       scanf(  %d , &array1[i][j]); n     } n   } n   for (i = 0; i < n; ++i) { n     mini = OO; n     for (j = 0; j < m; ++j) { n       mini = min(mini, array1[i][j]); n     } n     maxi = max(maxi, mini); n   } n   printf( %d , maxi); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   ; n   long long i, j, k, l, n, m; n   long long t; n   cin >> t; n   while (t--) { n     cin >> n >> m; n     for (i = 1; i <= n; i++) { n       for (j = 1; j <= m; j++) { n         if (i == 1 && j == 1) n           cout <<  B ; n         else if (i == 1 && j >= 2) n           cout <<  W ; n         else n           cout <<  B ; n       } n       cout <<   n ; n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n vector<vector<int> > bord(312345); n vector<int> ans; n int main() { n   std::ios::sync_with_stdio(false); n   int n, a, b, k; n   cin >> n >> a >> b >> k; n   string s; n   cin >> s; n   int i, sumi = 0, counter = 0; n   for (i = 0; i < n; i++) { n     if (s[i] ==  1 ) { n       counter++; n     } else { n       bord[counter].push_back(i + 1); n     } n   } n   for (i = 0; i < counter + 1; i++) { n     sumi += bord[i].size() / b; n   } n   sumi -= a; n   sumi++; n   cout << sumi << endl; n   for (i = 0; i < counter + 1; i++) { n     int val = bord[i].size(); n     int j = b - 1; n     while (sumi) { n       if (j < val) { n         ans.push_back(bord[i][j]); n         j += b; n         sumi--; n       } else { n         break; n       } n     } n   } n   for (i = 0; i < ans.size(); i++) { n     cout << ans[i] <<    ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 1000000 + 10; n const int INF = 1e9 + 7; n const int MOD = 1e9 + 7; n const double eps = 1e-10; n const double pi = acos(-1); n int T, cases, n, m; n int main() { n   ios::sync_with_stdio(false); n   ; n   cin >> n >> m; n   int ans; n   if (n == 1 || m == 1) n     ans = n * m; n   else if (n == 2 || m == 2) { n     if (m == 2) swap(n, m); n     if (m % 4 == 1) n       ans = m / 4 * 4 + 2; n     else n       ans = (m + 3) / 4 * 4; n   } else n     ans = max((n * m) / 2, (n * m) - (n * m / 2)); n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 1610; n typedef int MAP[maxn][maxn]; n int n, m; n MAP A, B, flag, used; n int total; n int num[maxn * maxn], que[maxn * maxn * 2]; n MAP temp; n int px[] = {0, 0, 1, -1, 0}, py[] = {1, -1, 0, 0, 0}; n bool isok(int x, int y) { return x >= 1 && x <= n && y >= 1 && y <= m; } n void zoomin(MAP X) { n   for (int i = 1; i <= n; i++) n     for (int j = 1; j <= m; j++) { n       int value = 1; n       for (int x = 0; x < 5; x++) n         if (isok(i + px[x], j + py[x]) && X[i + px[x]][j + py[x]] == 0) n           value = 0; n       temp[i][j] = value; n     } n   for (int i = 1; i <= n; i++) n     for (int j = 1; j <= m; j++) X[i][j] = temp[i][j]; n } n void zoomout(MAP X) { n   for (int i = 1; i <= n; i++) n     for (int j = 1; j <= m; j++) { n       int value = 0; n       for (int x = 0; x < 5; x++) n         if (isok(i + px[x], j + py[x]) && X[i + px[x]][j + py[x]] == 1) n           value = 1; n       temp[i][j] = value; n     } n   for (int i = 1; i <= n; i++) n     for (int j = 1; j <= m; j++) X[i][j] = temp[i][j]; n } n void bfs(int x, int y, int& index) { n   index++; n   int start = 1, tail = 0, sum = 0; n   que[++tail] = x; n   que[++tail] = y; n   used[x][y] = 1; n   while (start <= tail) { n     x = que[start++]; n     y = que[start++]; n     flag[x][y] = index; n     sum++; n     for (int i = 0; i < 4; i++) n       if (isok(x + px[i], y + py[i]) && !used[x + px[i]][y + py[i]] && n           B[x + px[i]][y + py[i]] == 1) { n         used[x + px[i]][y + py[i]] = 1; n         que[++tail] = x + px[i]; n         que[++tail] = y + py[i]; n       } n   } n   if (sum < 100) index--; n } n void bfs(int x, int y) { n   int start = 1, tail = 0, sum = 0, index = 0; n   que[++tail] = x; n   que[++tail] = y; n   used[x][y] = 1; n   while (start <= tail) { n     x = que[start++]; n     y = que[start++]; n     sum++; n     for (int i = 0; i < 4; i++) n       if (isok(x + px[i], y + py[i]) && !used[x + px[i]][y + py[i]] && n           A[x + px[i]][y + py[i]] == 1) { n         if (!flag[x + px[i]][y + py[i]]) { n           used[x + px[i]][y + py[i]] = 1; n           que[++tail] = x + px[i]; n           que[++tail] = y + py[i]; n         } else { n           index = flag[x + px[i]][y + py[i]]; n         } n       } n   } n   if (sum > 2) num[index]++; n } n int main() { n   scanf( %d%d , &n, &m); n   for (int i = 1; i <= n; i++) n     for (int j = 1; j <= m; j++) scanf( %d , &A[i][j]); n   for (int i = 1; i <= n; i++) n     for (int j = 1; j <= m; j++) B[i][j] = A[i][j]; n   for (int i = 1; i <= 3; i++) zoomin(B); n   for (int i = 1; i <= 4; i++) zoomout(B); n   for (int i = 1; i <= n; i++) n     for (int j = 1; j <= m; j++) n       if (!used[i][j] && B[i][j]) bfs(i, j, total); n   memset(used, 0, sizeof(used)); n   for (int i = 1; i <= n; i++) n     for (int j = 1; j <= m; j++) n       if (!used[i][j] && A[i][j] && !flag[i][j]) bfs(i, j); n   sort(num + 1, num + 1 + total); n   cout << total << endl; n   for (int i = 1; i <= total; i++) cout << num[i] <<    ; n   return 0; n } n


#include <bits/stdc++.h> n int main(void) { n   int n, sum = 0; n   scanf( %d , &n); n   getchar(); n   char str[n]; n   gets(str); n   for (int i = 0; i < n; i++) { n     if (str[i] ==  + ) n       sum++; n     else if (str[i] ==  -  && sum != 0) n       sum--; n   } n   printf( %d n , sum); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int t; n   cin >> t; n   while (t--) { n     int n, m; n     cin >> n >> m; n     int a[n], s = 0; n     for (int i = 0; i < n; i++) { n       cin >> a[i]; n       s += a[i]; n     } n     if (s >= m) n       cout << m << endl; n     else n       cout << s << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 2e5 + 10, inf = 0x3f3f3f3f; n int sa[N]; n int rk[N]; n int tmp[N]; n int lcp[N]; n char s[N], t[N]; n int n, k; n bool cmp(int i, int j) { n   if (rk[i] != rk[j]) n     return rk[i] < rk[j]; n   else { n     int ri = i + k <= n ? rk[i + k] : -1; n     int rj = j + k <= n ? rk[j + k] : -1; n     return ri < rj; n   } n } n void build(char *s, int *sa) { n   n = strlen(s); n   for (int i = 0; i <= n; i++) { n     sa[i] = i; n     rk[i] = i < n ? s[i] : -1; n   } n   for (k = 1; k <= n; k *= 2) { n     sort(sa, sa + n + 1, cmp); n     tmp[sa[0]] = 0; n     for (int i = 1; i <= n; i++) { n       tmp[sa[i]] = tmp[sa[i - 1]] + (cmp(sa[i - 1], sa[i]) ? 1 : 0); n     } n     for (int i = 0; i <= n; i++) { n       rk[i] = tmp[i]; n     } n   } n } n void LCP(char *s, int *sa, int *lcp) { n   n = strlen(s); n   for (int i = 0; i <= n; i++) rk[sa[i]] = i; n   int h = 0; n   lcp[0] = 0; n   for (int i = 0; i < n; i++) { n     int j = sa[rk[i] - 1]; n     for (h ? h-- : 0; j + h < n && i + h < n && s[j + h] == s[i + h]; h++) n       ; n     lcp[rk[i] - 1] = h; n   } n } n int st[N], pos[N]; n int main() { n   int T; n   scanf( %d , &T); n   while (T--) { n     scanf( %s , s); n     int n = strlen(s); n     build(s, sa); n     LCP(s, sa, lcp); n     for (int i = 0; i < n; i++) { n     } n     int now = 0; n     lcp[n] = 0; n     long long ans = 0; n     for (int i = 1; i <= n; i++) { n       int newpos = i, len = lcp[i]; n       while (now > 0 && st[now] > len) { n         long long v1 = st[now] - max(st[now - 1], len); n         long long v2 = i - pos[now] + 1; n         ans += v1 * v2 * v2; n         newpos = pos[now--]; n       } n       st[++now] = len; n       pos[now] = newpos; n     } n     for (int i = 0; i < n; i++) { n       ans += n - i - max(lcp[rk[i] - 1], lcp[rk[i]]); n     } n     printf( %lld n , ans); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAX_N = 2000; n int n, m, k, t, a[MAX_N + 1], b[MAX_N + 1], p[MAX_N + 1]; n vector<int> x; n vector<int> y; n int main() { n   int i, j, ans = 0; n   scanf( %d , &n); n   for (i = 1; i <= n; i++) scanf( %d , a + i); n   for (i = 1; i <= n; i++) scanf( %d , b + i), p[b[i]] = i; n   for (i = 1; i <= n; i++) { n     for (k = i;; k++) n       if (p[a[k]] == i) break; n     while (k != i) { n       for (j = i; j <= k; j++) n         if (p[a[j]] >= k) { n           x.push_back(k); n           y.push_back(j); n           ans += k - j; n           swap(a[j], a[k]); n           k = j; n         } n     } n   } n   printf( %d n%d n , ans, x.size()); n   for (i = 0; i < x.size(); i++) printf( %d %d n , x[i], y[i]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long a, b; n   cin >> a >> b; n   cout << fixed << setprecision(7) << a * pow(double(1.000000011), b) << endl; n } n


#include <bits/stdc++.h> n using namespace std; n const int inf = 1000000000; n long long mod = 1000000007LL; n long long mod2 = 998244353LL; n int n, m, k; n bool a[1000005]; n int main() { n   cin >> n >> m >> k; n   memset(a, false, sizeof(a)); n   for (int i = 1; i <= n; ++i) { n     int ai; n     scanf( %d , &ai); n     a[ai] = true; n   } n   int ans = 0; n   deque<int> dq; n   for (int i = 1; i <= 1000001; ++i) { n     if (a[i]) { n       dq.push_back(i); n     } n     while (!dq.empty() && i - dq.front() >= m) { n       dq.pop_front(); n     } n     if (i >= m) { n       int det = dq.size() - (k - 1); n       if (det > 0) { n         for (int j = 1; j <= det; ++j) { n           dq.pop_back(); n           ans++; n         } n       } n     } n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int cnt[5005][5005]; n int len; n char st[5005]; n bool palin[5005][5005]; n void updateFreq(int r, int c) { n   int cc; n   while (r <= len) { n     cc = c; n     while (cc <= len) { n       cnt[r][cc]++; n       cc += (cc & (-cc)); n     } n     r += (r & (-r)); n   } n } n int readFreq(int r, int c) { n   int ret = 0; n   int cc; n   while (r > 0) { n     cc = c; n     while (cc > 0) { n       ret += cnt[r][cc]; n       cc -= (cc & (-cc)); n     } n     r -= (r & (-r)); n   } n   return ret; n } n int main() { n   scanf( %s , st + 1); n   len = strlen(st + 1); n   int q; n   scanf( %d , &q); n   for (int d = 0, lets_stop_here = (int)len + 1; d < lets_stop_here; d++) { n     for (int i = 1, lets_stop_here = (int)len; i <= lets_stop_here; i++) { n       int j = i + d; n       if (j > len) break; n       if (i == j) n         palin[i][j] = true; n       else if (j - i == 1) n         palin[i][j] = st[i] == st[j] ? true : false; n       else if (palin[i + 1][j - 1]) n         palin[i][j] = st[i] == st[j] ? true : false; n       else n         palin[i][j] = false; n       if (palin[i][j]) updateFreq(i, j); n     } n   } n   while (q--) { n     int l, r; n     scanf( %d %d , &l, &r); n     int ans = readFreq(r, r) - readFreq(r, l - 1) - readFreq(l - 1, r) + n               readFreq(l - 1, l - 1); n     printf( %d n , ans); n   } n   return 0; n } n


#include <bits/stdc++.h> n const int maxn = 1e5 + 51; n const int mod1 = 1e9 + 7; n const int mod2 = 1e9 + 9; n std::vector<std::pair<int, int>> g[maxn]; n int dfn[maxn], idx = 0; n int f[maxn], fw[maxn]; n int xr; n std::vector<std::vector<int>> V; n void dfs(int u, int fa) { n   dfn[u] = ++idx; n   for (auto x : g[u]) { n     int v = x.first, w = x.second; n     if (v == fa) continue; n     if (!dfn[v]) { n       f[v] = u, fw[v] = w; n       dfs(v, u); n     } else if (dfn[v] > dfn[u]) { n       std::vector<int> vec; n       int cur = w; n       for (int now = v; now ^ u; now = f[now]) cur ^= fw[now]; n       vec.push_back(cur ^ w); n       for (int now = v; now ^ u; now = f[now]) vec.push_back(cur ^ fw[now]); n       xr ^= cur; n       V.push_back(vec); n     } n   } n } n int add(int x, int y, const int& mod) { n   x += y; n   return (x >= mod) ? x - mod : x; n } n int dec(int x, int y, const int& mod) { n   x -= y; n   return (x < 0) ? x + mod : x; n } n int n, m; n int main() { n   std::ios::sync_with_stdio(false); n   std::cin.tie(nullptr); n   std::cout.tie(nullptr); n   std::cin >> n >> m; n   for (int i = 1; i <= m; i++) { n     int u, v, w; n     std::cin >> u >> v >> w; n     g[u].push_back({v, w}); n     g[v].push_back({u, w}); n     xr ^= w; n   } n   dfs(1, 0); n   static int a[1 << 17 | 3], b[1 << 17 | 3]; n   auto fwt = [&](int* f, int lim, const int& inv, const int& mod) { n     for (int len = 1; len < lim; len <<= 1) { n       for (int i = 0; i < lim; i += len << 1) { n         for (int j = 0; j < len; j++) { n           int x = f[i + j], y = f[i + j + len]; n           f[i + j] = add(x, y, mod); n           f[i + j + len] = dec(x, y, mod); n           if (inv > 1) n             f[i + j] = 1ll * f[i + j] * inv % mod, n                   f[i + j + len] = 1ll * f[i + j + len] * inv % mod; n         } n       } n     } n   }; n   a[xr] = 1, b[xr] = 1; n   fwt(a, 1 << 17, 1, mod1); n   fwt(b, 1 << 17, 1, mod2); n   static int c[1 << 17 | 3], d[1 << 17 | 3]; n   for (auto v : V) { n     memset(c, 0, sizeof(c)); n     memset(d, 0, sizeof(d)); n     for (auto x : v) c[x]++, d[x]++; n     fwt(c, 1 << 17, 1, mod1); n     fwt(d, 1 << 17, 1, mod2); n     for (int i = 0; i < (1 << 17); i++) { n       a[i] = 1ll * a[i] * c[i] % mod1; n       b[i] = 1ll * b[i] * d[i] % mod2; n     } n   } n   fwt(a, 1 << 17, (mod1 + 1) >> 1, mod1); n   fwt(b, 1 << 17, (mod2 + 1) >> 1, mod2); n   for (int i = 0; i < (1 << 17); i++) n     if (a[i] || b[i]) { n       std::cout << i <<     << a[i] <<   n ; n       return 0; n     } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e5 + 9; n const long long mod = 998244353; n long long dp[N][1 << 7]; n int s[N][1 << 7], st[N], Log[1 << 7]; n struct node { n   int v, id; n } a[N]; n bool cmp(node a, node b) { return a.v > b.v; } n void upd(long long &a, long long b) { n   if (a < b) a = b; n } n int main() { n   int n, p, k; n   for (int i = (1); i < (N); ++i) st[i] = st[i >> 1] + (i & 1); n   for (int i = (0); i < (7); ++i) Log[1 << i] = i; n   scanf( %d%d%d , &n, &p, &k); n   for (int i = (1); i < (n + 1); ++i) { n     scanf( %d , &a[i].v); n     a[i].id = i; n   } n   sort(a + 1, a + 1 + n, cmp); n   int op = (1 << p) - 1; n   for (int i = (1); i < (n + 1); ++i) n     for (int j = (0); j < (p); ++j) scanf( %d , &s[i][j]); n   for (int i = (1); i < (p + k + 1); ++i) { n     for (int mask = (0); mask < (1 << p); ++mask) { n       if (st[mask] > i - 1) continue; n       upd(dp[i - st[mask]][mask], dp[i - st[mask] - 1][mask] + a[i].v); n       int pos = op ^ mask; n       while (pos) { n         upd(dp[i - st[mask] - 1][mask | ((pos) & (-pos))], n             dp[i - st[mask] - 1][mask] + s[a[i].id][Log[((pos) & (-pos))]]); n         pos ^= ((pos) & (-pos)); n       } n     } n   } n   for (int i = (p + k + 1); i < (n + 1); ++i) { n     for (int mask = (1 << p) - 1; mask >= (0); --mask) { n       int pos = mask; n       while (pos) { n         upd(dp[k][mask], n             dp[k][mask ^ ((pos) & (-pos))] + s[a[i].id][Log[((pos) & (-pos))]]); n         pos ^= ((pos) & (-pos)); n       } n     } n   } n   printf( %lld , dp[k][op]); n } n


#include <bits/stdc++.h> n using namespace std; n long long ans[100010]; n long long ans2[100010]; n int main() { n   int n, p; n   int l, r; n   while (cin >> n >> p) { n     for (int i = 0; i < n; i++) { n       scanf( %d%d , &l, &r); n       ans[i] = r / p - l / p; n       ans2[i] = r - l + 1; n       if (l % p == 0) ans[i]++; n     } n     ans[n] = ans[0]; n     ans2[n] = ans2[0]; n     double anss = 0; n     for (int i = 0; i < n; i++) n       anss += ((ans[i] * (ans2[i + 1] - ans[i + 1]) + n                 (ans2[i] - ans[i]) * ans[i + 1] + ans[i] * ans[i + 1]) * n                1.0) / n               (ans2[i] * ans2[i + 1] * 1.0) * 2000; n     printf( %.10llf , anss); n   } n   return 0; n } n


#include <bits/stdc++.h> n #pragma warning(disable : 4996) n using namespace std; n int IT_MAX = 1 << 17; n const long long MOD = 5000000; n const int INF = 1034567891; n const long long LL_INF = 1234567890123456789ll; n const double PI = 3.141592653589793238; n const double ERR = 1E-11; n int in[200050]; n int lr[200050][2]; n int v[200050]; n int main() { n   int N, T, K, i, j; n   scanf( %d %d %d , &N, &T, &K); n   for (i = 1; i <= T; i++) scanf( %d , &in[i]); n   in[0] = 1; n   for (i = 1; i < T; i++) { n     lr[i][1] = in[i] - 1; n     lr[i][0] = max(0, in[i] - in[i + 1]); n   } n   lr[T][0] = lr[T][1] = in[T]; n   int mn = 0, mx = 0; n   for (i = 1; i <= T; i++) mn += lr[i][0], mx += lr[i][1]; n   if (K < mn || mx < K) return !printf( -1 n ); n   int t = K - mn; n   for (i = 1; i <= T; i++) { n     int t2 = min(t, lr[i][1] - lr[i][0]); n     t -= t2; n     v[i] = in[i] - (lr[i][0] + t2); n   } n   v[0] = 1; n   printf( %d n , N); n   int pst = 1; n   for (i = 1; i <= T; i++) { n     int st = pst + in[i - 1]; n     for (j = 0; j < v[i - 1]; j++) printf( %d %d n , pst + j, st + j); n     for (j = v[i - 1]; j < in[i]; j++) printf( %d %d n , pst, st + j); n     pst = st; n   } n   return 0; n } n


#include<bits/stdc++.h> n #define LL long long n #define dl double n #define P pair<int,int> n using namespace std; n  n const int N = 2e5 + 10; n  n int n; n struct ghb{ n   int a,b; n   bool operator < (const ghb &o)const{ n     return min(a,b) < min(o.a,o.b); n   } n }f[N]; n LL ans; n int mx; n  n int main(){ n   // freopen( in.txt , r ,stdin); n   // freopen( o.txt , w ,stdout); n   std::ios::sync_with_stdio(false); n   std::cin.tie(nullptr); n   cin >> n; n   for(int i = 1;i <= n;i++)cin >> f[i].a; n   for(int i = 1;i <= n;i++)cin >> f[i].b; n   for(int i = 1;i <= n;i++)ans += abs(f[i].a - f[i].b); n   sort(f + 1,f + n + 1); n   int mx_1 = 0,mx_2 = 0; n   for(int i = 1;i <= n;i++){ n     if(f[i].a > f[i].b){ n       if(mx_1)mx = max(mx,min(mx_1,f[i].a) - f[i].b); n       mx_2 = max(mx_2,f[i].a); n     } n     else { n       if(mx_2)mx = max(mx,min(mx_2,f[i].b) - f[i].a); n       mx_1 = max(mx_1,f[i].b); n     } n   } n   cout << ans - 2 * mx << endl; n   return 0; n } n /**/


#include <bits/stdc++.h> n using namespace std; n long long GCD(long long a, long long b) { return (b == 0) ? a : GCD(b, a % b); } n bool isPrime(int x) { n   for (int i = 2; i <= sqrt(x); i++) n     if (x % i == 0) return 0; n   return 1; n } n bool C[26]; n int main() { n   int n, a; n   cin >> n >> a; n   vector<int> v(n); n   for (int i = 0; i < n; i++) { n     scanf( %d , &v[i]); n   } n   int ans = 0; n   if (a - 1 < n - a) { n     reverse(v.begin(), v.end()); n     a = n - a + 1; n   } n   a--; n   for (int i = 0; i < a + 1; i++) { n     if (v[i] == 1 && ((2 * a - i) >= n || i == a)) n       ans++; n     else if (v[i] == 1 && v[2 * a - i] == 1) n       ans += 2; n   } n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long a, b; n long long c, d; n int main() { n   while (cin >> a >> b) { n     cin >> c >> d; n     for (int i = 0; i <= 1000; ++i) { n       for (int j = 0; j <= 1000; ++j) { n         if ((b + a * i) == (d + c * j)) { n           cout << b + a * i << endl; n           return 0; n         } n       } n     } n     cout <<  -1  << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int t; n   cin >> t; n   while (t--) { n     int mid, i, j; n     string a; n     cin >> a; n     if ((a.length()) % 2) { n       cout <<  NO n ; n       continue; n     } n     mid = (a.length()) / 2; n     j = mid; n     i = 0; n     while (i < mid) { n       if (a[i] == a[j]) { n         i++; n         j++; n       } else { n         cout <<  NO n ; n         break; n       } n     } n     if (i == mid) { n       cout <<  YES n ; n     } n   } n } n


#include <bits/stdc++.h> n const int N = 100100; n const long long mod = 1e9 + 7; n using namespace std; n int n, Xa, Ya, Xb, Yb; n int Smallest(int a, int b, int c, int d) { n   int min_ = a; n   if (min_ > b) min_ = b; n   if (min_ > c) min_ = c; n   if (min_ > d) min_ = d; n   return min_; n } n int main() { n   ios_base::sync_with_stdio(0); n   cin >> n >> Xa >> Ya >> Xb >> Yb; n   if ((Xa == Xb && (Xa == 0 || Xa == n)) || (Ya == Yb && (Ya == 0 || Ya == n))) n     cout << abs(Xa - Xb) + abs(Ya - Yb) <<   n ; n   else n     cout << Smallest(abs(0 - Xa) + abs(0 - Ya) + abs(0 - Xb) + abs(0 - Yb), n                      abs(0 - Xa) + abs(n - Ya) + abs(0 - Xb) + abs(n - Yb), n                      abs(n - Xa) + abs(0 - Ya) + abs(n - Xb) + abs(0 - Yb), n                      abs(n - Xa) + abs(n - Ya) + abs(n - Xb) + abs(n - Yb)) n          <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, k; n   scanf( %d %d , &n, &k); n   int a[500005]; n   map<int, vector<int>> m; n   set<int> s; n   for (int i = 0; i < n; i++) s.insert(i); n   for (int i = 0; i < n; i++) { n     scanf( %d , &a[i]); n     m[a[i]].push_back(i); n   } n   int llast = n + k; n   int last = n; n   int cnt = 0; n   for (auto it : m) { n     if (!cnt && it.second[0] < last - k) { n       auto it = s.lower_bound(llast - k); n       if (it != s.end()) last = *it; n       cnt = 1; n     } n     if (it.second[0] < last - k) { n       printf( NO n ); n       return 0; n     } n     for (auto it2 : it.second) { n       s.erase(it2); n     } n     llast = last; n     last = min(it.second[0], last); n   } n   printf( YES n ); n } n


#include <bits/stdc++.h> n using namespace std; n map<int, int> glosy; n map<pair<int, int>, int> parki; n int main() { n   int n, m; n   scanf( %d%d , &n, &m); n   for (int i = (0); i < (n); i++) { n     int a, b; n     scanf( %d%d , &a, &b); n     if (a > b) swap(a, b); n     if (glosy.find(a) != glosy.end()) n       glosy[a]++; n     else n       glosy.insert(make_pair(a, 1)); n     if (glosy.find(b) != glosy.end()) n       glosy[b]++; n     else n       glosy.insert(make_pair(b, 1)); n     if (parki.find(make_pair(a, b)) != parki.end()) n       parki[make_pair(a, b)]++; n     else n       parki.insert(make_pair(make_pair(a, b), 1)); n   } n   if (m == 0) { n     cout << (n * 1LL * (n - 1LL)) / 2LL << endl; n     return 0; n   } n   long long int res = 0LL; n   vector<int> v; n   for (map<int, int>::iterator it = glosy.begin(); it != glosy.end(); it++) n     v.push_back(it->second); n   int jeszcze = n - v.size(); n   sort(v.begin(), v.end()); n   int k = v.size(); n   for (int i = (0); i < (k); i++) { n     vector<int>::iterator it2 = lower_bound(v.begin(), v.end(), m - v[i]); n     int ile = (v.end() - it2); n     if (v[i] + v[i] >= m) ile--; n     if (v[i] >= m) ile += 2 * jeszcze; n     res += ile * 1LL; n   } n   res = res / 2; n   for (map<pair<int, int>, int>::iterator it = parki.begin(); it != parki.end(); n        it++) { n     pair<int, int> para = it->first; n     int a = para.first; n     int b = para.second; n     int x = glosy[a] + glosy[b] - it->second; n     if (x < m && ((glosy[a] + glosy[b]) >= m)) res--; n   } n   cout << res << endl; n   return 0; n } n


#include <bits/stdc++.h> n const int LG = 21; n const int N = 500005; n const long long MOD = 1e9 + 7; n const long long INF = 1e9; n const long long INFLL = 1e18; n using namespace std; n void dfs(int v, int paint, vector<int> &col, n          vector<vector<pair<int, int> > > &graph, vector<int> &need, int &cnt0, n          int &cnt1, int &fl) { n   col[v] = paint; n   if (paint == -1) n     cnt0++; n   else n     cnt1++; n   if (need[v] != 0) { n     if (paint == need[v]) n       fl = 1; n     else n       fl = -1; n   } n   for (auto p : graph[v]) { n     if (col[p.first] == 0) { n       dfs(p.first, p.second * paint, col, graph, need, cnt0, cnt1, fl); n     } n   } n } n int root[N]; n vector<int> suns[N]; n int cntsame[N]; n int sametoroot[N]; n int fl[N]; n int ans; n int Root(int x) { return root[x] == x ? x : Root(root[x]); } n void complete_data(int v, int FL) { n   int x = Root(v); n   if (fl[x] != 0) return; n   FL *= sametoroot[v]; n   int took_before = min(cntsame[x], (int)suns[x].size() - cntsame[x]); n   ans -= took_before; n   fl[x] = FL; n   if (FL == 1) n     ans += cntsame[x]; n   else n     ans += suns[x].size() - cntsame[x]; n } n void Merge(int v, int u, int w) { n   int x = Root(v), y = Root(u); n   if (x == y) return; n   if (suns[x].size() > suns[y].size()) { n     swap(v, u); n     swap(x, y); n   } n   w *= sametoroot[v]; n   w *= sametoroot[u]; n   if (fl[x] == 0 && fl[y] == 0) { n     ans -= min(cntsame[x], (int)suns[x].size() - cntsame[x]); n     ans -= min(cntsame[y], (int)suns[y].size() - cntsame[y]); n   } n   if (fl[x] == 0 && fl[y] != 0) { n     complete_data(x, fl[y] * w); n   } n   if (fl[x] != 0 && fl[y] == 0) { n     complete_data(y, fl[x] * w); n   } n   for (auto u : suns[x]) { n     suns[y].push_back(u); n     sametoroot[u] *= w; n     if (sametoroot[u] == 1) cntsame[y]++; n   } n   if (fl[x] == 0 && fl[y] == 0) { n     ans += min(cntsame[y], (int)suns[y].size() - cntsame[y]); n   } n   root[x] = y; n } n signed main() { n   srand(time(NULL)); n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   int n, k; n   cin >> n >> k; n   string s; n   cin >> s; n   vector<vector<int> > contains(n); n   for (int i = 0; i < k; i++) { n     int c; n     cin >> c; n     for (int j = 0; j < c; j++) { n       int x = 0; n       cin >> x; n       x--; n       contains[x].push_back(i); n     } n   } n   vector<vector<pair<int, int> > > graph(k); n   vector<int> need(k, 0); n   for (int i = 0; i < k; i++) root[i] = i; n   for (int i = 0; i < k; i++) suns[i].push_back(i); n   for (int i = 0; i < k; i++) cntsame[i] = 1; n   for (int i = 0; i < k; i++) sametoroot[i] = 1; n   for (int i = 0; i < n; i++) { n     if (contains[i].size() == 1) { n       if (s[i] ==  1 ) { n         need[contains[i][0]] = -1; n         complete_data(contains[i][0], -1); n       } else { n         need[contains[i][0]] = 1; n         complete_data(contains[i][0], 1); n       } n     } n     if (contains[i].size() == 2) { n       if (s[i] ==  0 ) { n         graph[contains[i][0]].push_back({contains[i][1], -1}); n         graph[contains[i][1]].push_back({contains[i][0], -1}); n         Merge(contains[i][0], contains[i][1], -1); n       } else { n         graph[contains[i][0]].push_back({contains[i][1], 1}); n         graph[contains[i][1]].push_back({contains[i][0], 1}); n         Merge(contains[i][0], contains[i][1], 1); n       } n     } n     cout << ans << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 1e5 + 10; n vector<int> v; n set<pair<int, int> > st; n int bfs(int x) { n   queue<int> q; n   q.push(x); n   int cnt = 0; n   while (!q.empty()) { n     int u = q.front(); n     q.pop(); n     cnt++; n     int len = v.size(); n     for (int i = 0; i < len; i++, len = v.size()) { n       int p = v[i]; n       if (st.find(make_pair(u, p)) != st.end()) n         continue; n       else n         swap(v[i], v.back()), v.pop_back(), q.push(p), i--; n     } n   } n   return cnt; n } n int main() { n   int n, m; n   scanf( %d%d , &n, &m); n   for (int i = 0; i < m; i++) { n     int u, v; n     scanf( %d%d , &u, &v); n     st.insert(make_pair(u, v)); n     st.insert(make_pair(v, u)); n   } n   for (int i = 1; i <= n; i++) v.push_back(i); n   int ans = 0; n   while (!v.empty()) { n     int x = v.back(); n     v.pop_back(); n     bfs(x); n     ans++; n   } n   printf( %d n , ans - 1); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n bool essentiallyEqual(float a, float b) { n   return fabs(a - b) <= ((fabs(a) > fabs(b) ? fabs(b) : fabs(a)) * 1e-6); n } n struct TVector { n   long long x, y; n   TVector operator+(TVector q) const { return {x + q.x, y + q.y}; } n   TVector operator-(TVector q) const { return {x - q.x, y - q.y}; } n   TVector operator*(int t) const { return {x * t, y * t}; } n   long long operator*(TVector q) const { return {x * q.x + y * q.y}; } n   long long operator^(TVector q) const { return {x * q.y - q.x * y}; } n   long double operator~() const { return {hypot(x, y)}; } n   long double disPointLine(TVector p, TVector q) { n     TVector r = *this; n     if (((r - p) * (q - p) >= 0) && ((r - q) * (p - q) >= 0)) { n       return abs((q - p) ^ (r - p)) / ~(p - q); n     } else { n       return min(~(r - p), ~(r - q)); n     } n   } n   bool onLine(TVector p, TVector q) { n     TVector r = *this; n     long double dis = r.disPointLine(p, q); n     return essentiallyEqual(dis, 0); n   } n   long double sqrdDist(TVector p) { n     return (x - p.x) * (x - p.x) + (y - p.y) * (y - p.y); n   } n   int orientation(TVector b, TVector c) { n     TVector a = *this; n     int val = (c - a) ^ (b - a); n     return val; n   } n   int orientationV2(TVector b, TVector c) { n     TVector a = *this; n     int val = (c - a) ^ (b - a); n     if (val == 0) return 0; n     return (val > 0) ? 1 : 2; n   } n   void imprimePonto() { printf( %lld %lld , x, y); } n }; n vector<TVector> stormtroopers; n vector<int> isShot; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   int n, x0, y0; n   cin >> n >> x0 >> y0; n   TVector gun; n   gun.x = x0; n   gun.y = y0; n   for (int i = 0; i < n; ++i) { n     int x, y; n     cin >> x >> y; n     stormtroopers.push_back({x, y}); n     isShot.push_back(0); n   } n   int tiros = 0; n   for (int i = 0; i < n; ++i) { n     if (!isShot[i]) { n       tiros++; n       for (int j = i + 1; j < n; ++j) { n         if (!isShot[j]) { n           if (!gun.orientationV2(stormtroopers[i], stormtroopers[j])) n             isShot[j] = 1; n         } n       } n     } n   } n   cout << tiros << endl; n   return 0; n } n


#include <vector> n #include <iostream> n #include <algorithm> n #include <string> n #include <cmath> n #include <map> n #define ll long long n using namespace std; n  n  n int main() n { n     ll t; n     cin >> t; n     while (t--) { n         ll n, m; n         cin >> n >> m; n         string s; n         vector<ll> a(n, 0); n         vector<ll> b(n, 0); n         vector<ll> c(m, 0); n         vector<ll> result(m, 0); n         vector<vector<ll>> bucket(n + 1, vector<ll>(0)); n  n         for (auto& e : a)cin >> e; n         for (auto& e : b)cin >> e; n         for (auto& e : c)cin >> e; n  n         for (ll i = 0; i < n; i++) n         { n             if (b[i] != a[i]) { n                 bucket[b[i]].push_back(i); n             } n         } n  n         ll lastC = -1; n         if (bucket[c[m - 1]].size() > 0) { n             lastC = bucket[c[m - 1]].back(); n             bucket[c[m - 1]].pop_back(); n         } n         else { n             for (ll i = 0; i < n; i++) n             { n                 if (b[i] == c[m - 1]) { n                     lastC = i; n                     break; n                 } n             } n         } n  n         if (lastC == -1) { n             cout <<  NO  << endl; n         } n         else { n             result[m - 1] = lastC; n             for (ll i = 0; i < m - 1; i++) n             { n                 if (bucket[c[i]].size() == 0) { n                     result[i] = lastC; n                 } n                 else { n                     result[i] = bucket[c[i]].back(); n                     bucket[c[i]].pop_back(); n                 } n             } n  n             auto res = true; n             for (ll i = 0; i < n + 1; i++) n             { n                 if (bucket[i].size() > 0) { n                     res = false; n                 } n             } n  n             if (res == false) { n                 cout <<  NO  << endl; n             } n             else { n                 cout <<  YES  << endl; n                 for (ll i = 0; i < m; i++) n                 { n                     cout << result[i] + 1; n                     if (i < m - 1) { n                         cout <<    ; n                     } n                     else { n                         cout << endl; n                     } n                 } n             } n         } n     } n     return 0; n }


#include <bits/stdc++.h> n using namespace std; n const int nmax = 1010; n const int inf = 1000000007; n int n; n pair<int, int> a[nmax]; n int main() { n   ios_base::sync_with_stdio(false); n   cin >> n; n   for (int i = 1; i <= n; ++i) cin >> a[i].first >> a[i].second; n   long double ans = 1e15; n   a[n + 1] = a[1]; n   a[n + 2] = a[2]; n   for (int i = 1; i <= n; ++i) { n     pair<int, int> a1 = a[i], a2 = a[i + 1], a3 = a[i + 2]; n     long double A = a3.second - a1.second, B = a1.first - a3.first, n                 C = -A * a3.first - a3.second * B; n     long double d = n         abs(A * a2.first + B * a2.second + C) / (sqrt((A * A + B * B))); n     if (ans > d) ans = d; n   } n   cout << fixed << setprecision(15) << ans / (long double)2.0 <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long maxn = 1e6 + 666; n long long saveori[maxn]; n long long save[maxn]; n long long getcnt(long long x) { n   long long cnt = 0; n   while (x) { n     if (x & 1) cnt++; n     x = x >> 1; n   } n   return cnt; n } n long long cnt[maxn][2]; n long long pre[maxn]; n signed main() { n   long long n; n   cin >> n; n   for (long long i = 1; i <= n; i++) { n     scanf( %lld , &saveori[i]); n     save[i] = getcnt(saveori[i]); n   } n   long long sum = 0; n   for (long long i = 1; i <= n; i++) { n     cnt[i][0] = cnt[i - 1][0]; n     cnt[i][1] = cnt[i - 1][1]; n     cnt[i][sum % 2]++; n     sum += save[i]; n     pre[i] = sum; n   } n   long long ans = 0; n   for (long long i = n; i >= 1; i--) { n     long long maxval = -1; n     sum = 0; n     for (long long j = i; j >= i - 64 && j > 0; j--) { n       sum += save[j]; n       maxval = max(maxval, save[j]); n       if (maxval * 2 <= sum && sum % 2 == 0) { n         ans++; n       } n     } n     if (i - 65 >= 1) { n       ans += cnt[i - 65][pre[i] % 2]; n     } n   } n   cout << ans << endl; n } n


#include <bits/stdc++.h> n using namespace std; n #define int long long int n void next(char &x){ n     if(x<= b ){ n         x++; n     }else{ n         x= a ; n     } n } n void solve() { n     int n,k; n     cin>>n>>k; n     char cur= a ; n     for(int i=0;i<n;i++){ n         cout<<cur; n         next(cur); n     } n     cout<<endl; n } n signed main() { n     ios::sync_with_stdio(false); n     cin.tie(nullptr); n     cout.tie(nullptr); n     int t; n     cin >> t; n     while (t--){ n         solve(); n     } n }


#include <bits/stdc++.h> n #pragma GCC optimize( Ofast ) n #pragma GCC target( avx,avx2,fma ) n #pragma GCC optimization( unroll-loops ) n const int mod = 1e9 + 7; n using ll = int64_t; n using namespace std; n template <int MOD_> n struct modnum { n   static constexpr int MOD = MOD_; n   static_assert(MOD_ > 0,  MOD must be positive ); n   int v; n  n  private: n   using ll = long long; n   static int minv(int a, int m) { n     a %= m; n     assert(a); n     return a == 1 ? 1 : int(m - ll(minv(m, a)) * ll(m) / a); n   } n  n  public: n   modnum() : v(0) {} n   modnum(ll v_) : v(int(v_ % MOD)) { n     if (v < 0) v += MOD; n   } n   explicit operator int() const { return v; } n   friend std::ostream& operator<<(std::ostream& out, const modnum& n) { n     return out << int(n); n   } n   friend std::istream& operator>>(std::istream& in, modnum& n) { n     ll v_; n     in >> v_; n     n = modnum(v_); n     return in; n   } n   friend bool operator==(const modnum& a, const modnum& b) { n     return a.v == b.v; n   } n   friend bool operator!=(const modnum& a, const modnum& b) { n     return a.v != b.v; n   } n   modnum inv() const { n     modnum res; n     res.v = minv(v, MOD); n     return res; n   } n   friend modnum inv(const modnum& m) { return m.inv(); } n   modnum neg() const { n     modnum res; n     res.v = v ? MOD - v : 0; n     return res; n   } n   friend modnum neg(const modnum& m) { return m.neg(); } n   modnum operator-() const { return neg(); } n   modnum operator+() const { return modnum(*this); } n   modnum& operator++() { n     v++; n     if (v == MOD) v = 0; n     return *this; n   } n   modnum& operator--() { n     if (v == 0) v = MOD; n     v--; n     return *this; n   } n   modnum& operator+=(const modnum& o) { n     v += o.v; n     if (v >= MOD) v -= MOD; n     return *this; n   } n   modnum& operator-=(const modnum& o) { n     v -= o.v; n     if (v < 0) v += MOD; n     return *this; n   } n   modnum& operator*=(const modnum& o) { n     v = int(ll(v) * ll(o.v) % MOD); n     return *this; n   } n   modnum& operator/=(const modnum& o) { return *this *= o.inv(); } n   friend modnum operator++(modnum& a, int) { n     modnum r = a; n     ++a; n     return r; n   } n   friend modnum operator--(modnum& a, int) { n     modnum r = a; n     --a; n     return r; n   } n   friend modnum operator+(const modnum& a, const modnum& b) { n     return modnum(a) += b; n   } n   friend modnum operator-(const modnum& a, const modnum& b) { n     return modnum(a) -= b; n   } n   friend modnum operator*(const modnum& a, const modnum& b) { n     return modnum(a) *= b; n   } n   friend modnum operator/(const modnum& a, const modnum& b) { n     return modnum(a) /= b; n   } n }; n using mi = modnum<mod>; n void solve() { n   int n, m; n   cin >> n; n   vector<vector<int>> adj(n); n   for (int i = 0; i < n - 1; i++) { n     int a, b; n     cin >> a >> b; n     a--, b--; n     adj[a].push_back(b); n     adj[b].push_back(a); n   } n   cin >> m; n   vector<mi> fac(m); n   for (int i = 0; i < m; i++) { n     cin >> fac[i]; n   } n   auto comp = [&](mi X, mi Y) -> bool { return X.v < Y.v; }; n   sort(fac.begin(), fac.end(), comp); n   if (int(fac.size()) < n - 1) { n     reverse(fac.begin(), fac.end()); n     while (int(fac.size()) < n - 1) { n       if (int(fac.size()) < n - 1) { n         fac.push_back(mi(1)); n       } n     } n     reverse(fac.begin(), fac.end()); n   } n   if (int(fac.size()) > n - 1) { n     mi cur = 1; n     while (int(fac.size()) >= n - 1) { n       cur *= fac.back(); n       fac.pop_back(); n     } n     fac.push_back(cur); n   } n   vector<ll> sz(n, 0), num; n   function<void(int, int)> dfs = [&](int node, int par) -> void { n     sz[node] = 1; n     for (auto i : adj[node]) { n       if (i != par) { n         dfs(i, node); n         sz[node] += sz[i]; n         num.push_back((ll)sz[i] * (n - sz[i])); n       } n     } n   }; n   mi answer = 0; n   dfs(0, 0); n   sort(num.begin(), num.end()); n   assert(int(num.size()) >= n - 1); n   assert(int(fac.size()) == n - 1); n   for (int i = 0; i < n - 1; i++) { n     mi cur = (fac[i] * num[i]); n     answer += cur; n   } n   cout << answer <<   n ; n } n int main() { n   std::ios_base::sync_with_stdio(false); n   std::cin.tie(nullptr); n   int t(1); n   cin >> t; n   while (t--) { n     solve(); n   } n } n


#include <bits/stdc++.h> n using namespace std; n int n, a[105], z, x; n int main() { n   scanf( %d , &n); n   for (int i = 0; i < n; i++) { n     scanf( %d , a + i); n     if (a[i] % 2) x++; n     z += a[i]; n   } n   if (z % 2) n     printf( %d , x); n   else n     printf( %d , n - x); n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 1000005, mod = 1000000007; n vector<int> pri; n bool vis[80]; n int pos[80], a[maxn], n; n long long w[80]; n long long dp[1 << 21], dp2[1 << 21], f[maxn]; n void prework() { n   for (int i = 2; i < (71); ++i) { n     if (!vis[i]) { n       pri.push_back(i); n       pos[i] = (int)pri.size() - 1; n       for (int j = i; j <= 70; j += i) vis[j] = 1; n     } n   } n   f[0] = 1; n   for (int i = 1; i < (maxn); ++i) f[i] = 2 * f[i - 1] % mod; n } n void init() { n   scanf( %d , &n); n   for (int i = 0; i < (n); ++i) { n     scanf( %d , &a[i]); n     w[a[i]]++; n   } n } n int gao(int n) { n   int tmp[30] = {0}; n   for (int i = 2; i <= n; ++i) { n     while (n % i == 0) { n       n /= i; n       tmp[pos[i]] ^= 1; n     } n   } n   if (n > 1) tmp[pos[n]] ^= 1; n   int res = 0; n   for (int i = 0; i < (21); ++i) n     if (tmp[i]) res |= (1 << i); n   return res; n } n inline int get_nx(int st1, int st2) { return st1 ^ st2; } n void solve() { n   dp[0] = 1; n   for (int i = 2; i < (71); ++i) { n     if (!w[i]) continue; n     int st = gao(i); n     for (int pre = 0; pre < (1 << 19); ++pre) { n       int nx_st = get_nx(st, pre); n       dp2[nx_st] += dp[pre] * f[w[i] - 1] % mod; n       dp2[pre] += dp[pre] * f[w[i] - 1] % mod; n       dp2[nx_st] %= mod; n       dp2[pre] %= mod; n     } n     for (int k = 0; k < (1 << 19); ++k) dp[k] = dp2[k], dp2[k] = 0; n   } n   long long ans = dp[0] - 1; n   if (w[1]) ans = (f[w[1]]) * ans % mod + f[w[1]] - 1; n   cout << ans % mod << endl; n } n int main() { n   prework(); n   init(); n   solve(); n } n


#include <bits/stdc++.h> n using namespace std; n const int mod = 1000000007; n ; n const int maxn = 410; n unsigned long long powmod(unsigned long long n, unsigned long long k) { n   unsigned long long ret = 1; n   while (k) { n     if (k & 1) ret = ret * n % mod; n     n = n * n % mod; n     k >>= 1; n   } n   return ret; n } n int nxt[maxn][26]; n int dp[maxn][maxn]; n void upd(int& n, int k) { n   if (n < 0 || n > k) n = k; n } n bool gao(const string& s, const string& t1, const string& t2) { n   memset(dp, -1, sizeof(dp)); n   for (int i = 0; i <= t1.size(); i++) n     for (int j = 0; j <= t2.size(); j++) n       if (dp[i][j] >= 0 || i + j == 0) { n         int p = dp[i][j] + 1; n         if (i < t1.size()) { n           int n = nxt[p][t1[i] -  a ]; n           if (n >= 0) upd(dp[i + 1][j], n); n         } n         if (j < t2.size()) { n           int n = nxt[p][t2[j] -  a ]; n           if (n >= 0) upd(dp[i][j + 1], n); n         } n       } n   return dp[t1.size()][t2.size()] >= 0; n } n char str[maxn]; n char tt[maxn]; n int main() { n   int t; n   cin >> t; n   while (t--) { n     cin >> str >> tt; n     memset(nxt, -1, sizeof(nxt)); n     for (int i = strlen(str) - 1; i >= 0; i--) { n       memcpy(nxt[i], nxt[i + 1], sizeof(nxt[i])); n       nxt[i][str[i] -  a ] = i; n     } n     bool yes = false; n     if (strlen(tt) == 1) n       yes = (nxt[0][tt[0] -  a ] >= 0); n     else { n       for (int i = 0; tt[i + 1]; i++) { n         char tmp = tt[i + 1]; n         tt[i + 1] = 0; n         string t1 = tt; n         tt[i + 1] = tmp; n         string t2 = tt + i + 1; n         if (gao(str, t1, t2)) { n           yes = true; n           break; n         } n       } n     } n     if (yes) n       puts( YES ); n     else n       puts( NO ); n   } n } n


#include <bits/stdc++.h> n using namespace std; n mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); n long long Query(long long L, long long R) { n   cout << L <<     << R << endl; n   fflush(stdout); n   string s; n   cin >> s; n   if (L == R && s ==  Yes ) exit(0); n   return s ==  Yes ; n } n int32_t main() { n   long long n, k; n   cin >> n >> k; n   long long L = 1, R = n; n   while (true) { n     if (R - L + 1 <= 4 * k + 7) { n       long long M = L + rng() % (R - L + 1); n       if (Query(M, M)) { n         return 0; n       } else { n         L = max(1LL, L - k); n         R = min(n, R + k); n       } n     } else { n       long long M = L + ((R - L) >> 1); n       if (Query(L, M)) { n         L = max(1LL, L - k); n         R = min(n, M + k); n       } else { n         L = max(1LL, M - k); n         R = min(n, R + k); n       } n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n const long long MOD = (1e9 + 7); n const long long INF = (1e9 + 123456789); n const long long INFL = (INF * INF); n inline long long addm(long long a, long long b, long long m = MOD) { n   return ((a + b) % m); n } n inline long long subm(long long a, long long b, long long m = MOD) { n   return (((a - b) % m + m) % m); n } n inline long long mulm(long long a, long long b, long long m = MOD) { n   return ((a * b) % m); n } n long long powm(long long a, long long b, long long m = MOD) { n   long long ret = (!b) ? 1 : powm(a, b / 2, m); n   return (!b) ? 1 : mulm(mulm(ret, ret, m), b % 2 ? a : 1, m); n } n long long gcd(long long a, long long b) { return (!b) ? a : gcd(b, a % b); } n long long lcm(long long a, long long b) { return a / gcd(a, b) * b; } n long long inv(long long x, long long m = MOD) { return powm(x, m - 2, m); } n const int N = 1000500; n int a[N], lft[N], nxt[N]; n vector<int> nodes, g[N]; n struct SegmentTree { n   int f, t; n   SegmentTree *lc, *rc; n   bool has_child, has_lazy = false; n   int upd = 0; n   int vmax = 0; n   SegmentTree(int l, int r) { n     if (l == r) { n       f = t = l; n       lc = rc = NULL; n       has_child = false; n     } else { n       f = l; n       t = r; n       int mid = (l + r) / 2; n       lc = new SegmentTree(l, mid); n       rc = new SegmentTree(mid + 1, r); n       has_child = true; n     } n   } n   void change(int v) { n     vmax += v; n     upd += v; n     has_lazy = true; n   } n   void push() { n     if (!has_lazy) return; n     lc->change(upd); n     rc->change(upd); n     upd = 0; n     has_lazy = false; n   } n   void update(int l, int r, int x) { n     if (r < f || l > t) return; n     if (l <= f && t <= r) { n       change(x); n       return; n     } n     push(); n     lc->update(l, r, x); n     rc->update(l, r, x); n     vmax = max(lc->vmax, rc->vmax); n   } n   int query(int l, int r) { n     if (r < f || l > t) return 0; n     if (l <= f && t <= r) return vmax; n     return vmax = max(lc->query(l, r), rc->query(l, r)); n   } n }; n void solve() { n   int n, k; n   cin >> n >> k; n   for (int i = 0; i < n; i++) cin >> a[i]; n   stack<int> st; n   for (int i = n - 1; i >= 0; i--) { n     while (!st.empty() && a[st.top()] <= a[i]) st.pop(); n     if (!st.empty()) n       nxt[i] = st.top(); n     else n       nxt[i] = n; n     st.push(i); n   } n   for (int i = 0; i < n; i++) g[nxt[i]].push_back(i); n   function<void(int)> post = [&](int v) { n     int l = (int)(nodes).size(); n     for (int u : g[v]) post(u); n     lft[(int)(nodes).size()] = l; n     nodes.push_back(v); n   }; n   post(n); n   SegmentTree root(0, n); n   for (int i = 0; i < k; i++) root.update(lft[i], i, 1); n   vector<int> ans; n   ans.push_back(root.query(0, n)); n   for (int i = k; i < n; i++) { n     root.update(lft[i - k], i - k, -1); n     root.update(lft[i], i, 1); n     ans.push_back(root.query(0, n)); n   } n   for (int i = 0; i < (int)(ans).size(); i++) { n     if (i) cout <<    ; n     cout << ans[i]; n   } n   cout <<   n ; n } n int main(int argc, char *argv[]) { n   cin.sync_with_stdio(0); n   cin.tie(NULL); n   solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long N = 2e5 + 100; n long long a[N]; n pair<long long, long long> ask(long long l, long long r) { n   cout <<  ?   << l <<     << r << endl; n   long long first; n   cin >> first; n   if (first == -1) exit(0); n   long long f; n   cin >> f; n   return {first, f}; n } n void solve(long long l, long long r) { n   if (l > r) return; n   long long first, f; n   tie(first, f) = ask(l, r); n   long long l1 = r + 1 - f, r1 = l - 1 + f; n   if (l1 <= r1) { n     for (long long i = l1; i <= r1; i++) a[i] = first; n     solve(l, l1 - 1); n     solve(r1 + 1, r); n   } else { n     long long m = (l + r) / 2; n     solve(l, m); n     solve(m + 1, r); n   } n } n signed main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   long long n; n   cin >> n; n   solve(1, n); n   cout <<  !  ; n   for (long long i = 1; i <= n; i++) cout << a[i] <<    ; n   cout << endl; n } n


#include <bits/stdc++.h> n #pragma GCC optimize( Ofast,unroll-loops,fast-math ) n using namespace std; n const int MAXN = 102, K = 900; n int n, m, k, u, v, x, y, t, a, b; n int A[MAXN]; n short F1[MAXN][MAXN][2 * MAXN]; n short F2[MAXN][MAXN][2 * MAXN]; n short F3[MAXN][MAXN][2 * MAXN]; n double dp[2][MAXN][MAXN]; n int C(int x) { return x * (x + 1) / 2; } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   cin >> n >> k; n   k = min(k, K); n   for (int i = 0; i < n; i++) cin >> A[i]; n   for (int l = 0; l < n; l++) n     for (int r = l; r < n; r++) { n       for (int i = 0; i < n; i++) n         for (int j = i + 1; j < n; j++) { n           bool ii = (l <= i && i <= r); n           bool jj = (l <= j && j <= r); n           if (ii && !jj) F1[i][j][l + r]++; n           if (!ii && jj) F2[i][j][l + r]++; n           if (ii && jj) F3[i][j][l + r]++; n         } n     } n   int prev = 0, curr = 1; n   for (int i = 0; i < n; i++) n     for (int j = 0; j < i; j++) dp[0][i][j] = 1; n   for (int x = 1; x <= k; x++) { n     for (int i = 0; i < n; i++) n       for (int j = i + 1; j < n; j++) { n         dp[curr][i][j] = (C(i) + C(j - i - 1) + C(n - j - 1)) * dp[prev][i][j]; n         for (int cent = i; cent < n + i; cent++) n           dp[curr][i][j] += F1[i][j][cent] * dp[prev][cent - i][j]; n         for (int cent = j; cent < n + j; cent++) n           dp[curr][i][j] += F2[i][j][cent] * dp[prev][i][cent - j]; n         for (int cent = j; cent < n + i; cent++) n           dp[curr][i][j] += F3[i][j][cent] * dp[prev][cent - i][cent - j]; n         dp[curr][i][j] /= C(n); n         dp[curr][j][i] = 1. - dp[curr][i][j]; n       } n     swap(prev, curr); n   } n   double ans = 0; n   for (int i = 0; i < n; i++) n     for (int j = 0; j < n; j++) n       if (A[i] < A[j]) ans += dp[prev][i][j]; n   cout << setprecision(12) << fixed << ans <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   cin >> n; n   int tab[n]; n   long long s = 0; n   for (int i = 0; i < n; i++) { n     cin >> tab[i]; n     if (i == tab[i]) { n       s++; n       tab[i] = -1; n     } n   } n   bool bo = false; n   for (int i = 0; i < n; i++) { n     if (tab[i] != -1) { n       if (tab[tab[i]] == i) { n         s += 2; n         bo = false; n         break; n       } n       bo = true; n     } n   } n   if (bo) s++; n   cout << s; n } n