#include <bits/stdc++.h> n using namespace std; n const int INF = 1000000007; n const long long MOD = 1000000007; n void solve_test() { n   int n; n   cin >> n; n   vector<int> arr(n); n   vector<int> ct(101); n   for (int i = 0; i < n; i++) { n     cin >> arr[i]; n     ct[arr[i]]++; n   } n   int max_val = -1; n   int max_freq = -1; n   for (int i = 1; i <= 100; i++) { n     if (ct[i] > max_freq) { n       max_freq = ct[i]; n       max_val = i; n     } n   } n   unordered_map<int, int> um; n   int ans = 0; n   for (int i = 1; i <= 100; i++) { n     if (max_val == i) continue; n     um.clear(); n     int st = 0; n     um[0] = -1; n     for (int j = 0; j < n; j++) { n       if (arr[j] == i) { n         st++; n       } else if (arr[j] == max_val) { n         st--; n       } n       if (um.find(st) == um.end()) { n         um[st] = j; n       } else { n         ans = max(ans, j - um[st]); n       } n     } n   } n   cout << ans; n } n signed main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   int tests; n   tests = 1; n   while (tests--) { n     solve_test(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int N, T; n char str[15], temp[15]; n int main() { n   int i, j, k; n   scanf( %s%s , str, temp); n   i = strlen(str); n   sort(str, str + i); n   if (str[0] ==  0 ) { n     do { n       if (str[0] !=  0 ) break; n     } while (next_permutation(str, str + i)); n   } n   if (strcmp(str, temp) == 0) n     printf( OK n ); n   else n     printf( WRONG_ANSWER n ); n   scanf(   ); n } n


#include <bits/stdc++.h> n using namespace std; n void init() {} n const int N = (int)1e6 + 34; n const long long INF = (long long)1e9 + 34; n int n, m; n int a[555][555]; n int c[555], r[555]; n int main() { n   init(); n   int mxr = -N, mnr = N, mxc = -N, mnc = N; n   cin >> n >> m; n   int sum = 0; n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < m; j++) { n       cin >> a[i][j]; n       sum += a[i][j]; n     } n   } n   if (n == 1) { n     for (int j = 0; j < m; j++) { n       c[j] = a[0][j]; n     } n     r[0] = 0; n   } n   if (m == 1) { n     for (int i = 0; i < n; i++) { n       r[i] = a[i][0]; n     } n     c[0] = 0; n   } n   if (max(n, m) > 1) { n     c[0] = 0; n     for (int i = 0; i < n; i++) r[i] = a[i][0]; n     for (int j = 1; j < m; j++) n       for (int i = 0; i < n; i++) { n         c[j] = a[i][j] - r[i]; n       } n   } n   for (int i = 0; i < n; i++) n     for (int j = 0; j < m; j++) { n       if (a[i][j] != r[i] + c[j]) { n         cout <<  -1 n ; n         return 0; n       } n     } n   for (int i = 0; i < n; i++) { n     mnr = min(mnr, r[i]); n     mxr = max(mxr, r[i]); n   } n   for (int j = 0; j < m; j++) { n     mxc = max(mxc, c[j]); n     mnc = min(mnc, c[j]); n   } n   if (mnr + mnc < 0) { n     cout <<  -1 n ; n     return 0; n   } n   int dr = 0; n   int dc = 0; n   if (mnr < 0) { n     dc = mnr; n     dr = -dc; n   } else if (mnc < 0) { n     dr = mnc; n     dc = -dr; n   } n   while (n > m && mnr + dr > 0) { n     dr--; n     dc++; n   } n   while (n < m && mnc + dc > 0) { n     dr++; n     dc--; n   } n   int num = 0; n   for (int i = 0; i < n; i++) num += r[i] + dr; n   for (int j = 0; j < m; j++) num += c[j] + dc; n   cout << num <<   n ; n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < r[i] + dr; j++) cout <<  row   << i + 1 <<   n ; n   } n   for (int j = 0; j < m; j++) { n     for (int i = 0; i < c[j] + dc; i++) cout <<  col   << j + 1 <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, m, v; n   scanf( %d%d%d , &n, &m, &v); n   int spec = 1; n   if (m < n - 1 || m > (n - 1) * (n - 2) / 2 + 1) { n     printf( -1 n ); n     return 0; n   } n   if (spec == v) spec++; n   printf( %d %d n , v, spec); n   m--; n   for (int i = 1; i <= n; i++) { n     if (i == spec) continue; n     for (int j = i + 1; j <= n; j++) { n       if (j == spec) continue; n       printf( %d %d n , i, j); n       m--; n       if (m == 0) return 0; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n string str[1005]; n int tim[1005][1005]; n int n, m, t; n int fx[] = {-1, +1, 0, 0}; n int fy[] = {0, 0, -1, +1}; n vector<pair<int, int> > cur; n bool okay(int x, int y) { return (x >= 0 && x < n && y >= 0 && y < m); } n void dfs(int x, int y, int curr) { n   tim[x][y] = curr; n   cur.push_back(make_pair(x, y)); n   for (int k = 0; k < 4; k++) { n     int tx = x + fx[k]; n     int ty = y + fy[k]; n     if (okay(tx, ty) == false || tim[tx][ty] != -1 || str[tx][ty] != str[x][y]) n       continue; n     dfs(tx, ty, curr); n   } n } n char alt(char x) { n   if (x ==  0 ) n     return  1 ; n   else n     return  0 ; n } n char query(int x, int y, long long curr) { n   if (tim[x][y] == -1) return str[x][y]; n   long long diff = curr - 1LL * tim[x][y]; n   if (diff % 2LL == 1LL) n     return alt(str[x][y]); n   else n     return str[x][y]; n } n int main() { n   scanf( %d %d %d , &n, &m, &t); n   for (int i = 0; i < n; i++) { n     cin >> str[i]; n   } n   memset(tim, -1, sizeof tim); n   vector<pair<int, int> > last; n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < m; j++) last.push_back(make_pair(i, j)); n   } n   for (int curTim = 0; curTim <= n * m; curTim++) { n     for (int i = 0; i < last.size(); i++) { n       int x = last[i].first, y = last[i].second; n       char col = query(x, y, curTim); n       for (int k = 0; k < 4; k++) { n         int tx = x + fx[k]; n         int ty = y + fy[k]; n         if (okay(tx, ty) == false || tim[tx][ty] != -1 || str[tx][ty] != col) n           continue; n         dfs(tx, ty, curTim); n       } n     } n     last = cur; n     cur.clear(); n     if (last.empty()) break; n   } n   while (t--) { n     int x, y; n     long long q; n     scanf( %d %d %lld , &x, &y, &q); n     x--, y--; n     char ans = query(x, y, q); n     cout << ans << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int MOD = 1e9 + 7; n const int maxn = 100005; n int a[3], d; n void Solve() { n   cin >> a[0] >> a[1] >> a[2] >> d; n   sort(a, a + 3); n   long long res = 0; n   if (a[2] < a[1] + d) res += a[1] + d - a[2]; n   if (a[0] > a[1] - d) res += a[0] - a[1] + d; n   cout << res; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   if (fopen(   n              .inp , n              r )) { n     freopen( n            n          .inp , n          r , stdin); n     freopen( n            n          .out , n          w , stdout); n   } n   int test_case = 1; n   while (test_case--) Solve(); n   return 0; n } n


#include <bits/stdc++.h> n struct circle { n   double r, x, y; n   circle(void) : r(0), x(0), y(0) {} n   bool contains(const circle& o) const { n     double dx, dy; n     dx = o.x - x; n     dy = o.y - y; n     return (dx * dx + dy * dy <= r * r) && o.r < r; n   } n }; n circle fences[1024]; n circle control[1024]; n int depth[1024]; n int parent[11][1024]; n int control_parent[1024]; n int V, n; n int find_parent(const circle& c, const int& ignore) { n   int best_j, best, j; n   best_j = V; n   best = -INT_MAX; n   for (j = 0; j < V; ++j) { n     if (j != ignore && fences[j].contains(c)) { n       if (depth[j] > best) { n         best = depth[j]; n         best_j = j; n       } n     } n   } n   return best_j; n } n void build_tree(void) { n   int i, j; n   for (i = 0; i < V; ++i) { n     depth[i] = 1; n     for (j = 0; j < V; ++j) { n       if (i != j && fences[j].contains(fences[i])) { n         ++depth[i]; n       } n     } n   } n   for (i = 0; i < V; ++i) { n     parent[0][i] = find_parent(fences[i], i); n   } n   for (i = 1; i <= n; ++i) { n     control_parent[i] = find_parent(control[i], -1); n   } n } n void build_lca(void) { n   int lg2, i; n   parent[0][V] = V; n   for (lg2 = 1; lg2 < 11; ++lg2) { n     for (i = 0; i < V; ++i) { n       parent[lg2][i] = parent[lg2 - 1][parent[lg2 - 1][i]]; n     } n   } n } n int get_lca(int a, int b) { n   int step, lg2; n   if (depth[a] < depth[b]) { n     std::swap(a, b); n   } n   for (lg2 = 10, step = 1024; lg2 >= 0; --lg2, step >>= 1) { n     if (depth[a] - step >= depth[b]) { n       a = parent[lg2][a]; n     } n   } n   if (a == b) { n     return a; n   } n   for (lg2 = 10, step = 1024; lg2 >= 0; --lg2, step >>= 1) { n     if (parent[lg2][a] != parent[lg2][b]) { n       a = parent[lg2][a]; n       b = parent[lg2][b]; n     } n   } n   return parent[0][a]; n } n int main(void) { n   int i, q; n   int control_1, control_2, control_3; n   std::cin.tie(0); n   std::ios_base::sync_with_stdio(0); n   std::cin >> n >> V >> q; n   for (i = 1; i <= n; ++i) { n     std::cin >> control[i].x >> control[i].y; n   } n   for (i = 0; i < V; ++i) { n     std::cin >> fences[i].r >> fences[i].x >> fences[i].y; n   } n   build_tree(); n   build_lca(); n   for (i = 0; i < q; ++i) { n     std::cin >> control_1 >> control_2; n     control_1 = control_parent[control_1]; n     control_2 = control_parent[control_2]; n     control_3 = get_lca(control_1, control_2); n     std::cout << (depth[control_1] + depth[control_2] - 2 * depth[control_3]) n               <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long mod = 1e9 + 7; n int logb(int base, int x) { return (log(x) / log(base)); } n int occurs(vector<long long> v, int n) { n   auto it = lower_bound(((v)).begin(), ((v)).end(), (n)); n   auto it1 = upper_bound(((v)).begin(), ((v)).end(), (n)); n   int x = it1 - it; n   return x; n } n long long gcd(long long a, long long b) { n   if (b == 0) return a; n   return gcd(b, a % b); n } n long long powm(int a, int b) { n   long long res = 1; n   while (b) { n     if (b & 1) res = (res * 1LL * a) % mod; n     b >>= 1; n     a = (a * 1LL * a) % mod; n   } n   return res; n } n long long nCr(long long n, long long k) { n   int fact[n]; n   long long res = (fact[n] * powm(fact[k], mod - 2)) % mod; n   return (res * powm(fact[n - k], mod - 2)) % mod; n } n const int N = 1e5 + 5, inf = 1e9; n vector<int> g[N]; n int n, m, d; n int mxin[N], mxout[N], marked[N]; n void dfs_down(int v, int p) { n   mxin[v] = -inf; n   if (marked[v]) mxin[v] = 0; n   for (auto u : g[v]) { n     if (u == p) continue; n     dfs_down(u, v); n     mxin[v] = max(mxin[v], mxin[u] + 1); n   } n } n void dfs_up(int v, int p) { n   int max1 = -1 * inf, max2 = -1 * inf; n   for (auto u : g[v]) { n     if (u == p) continue; n     if (mxin[u] >= max1) n       max2 = max1, max1 = mxin[u]; n     else if (mxin[u] > max2) n       max2 = mxin[u]; n   } n   for (auto u : g[v]) { n     if (u == p) continue; n     int used = (mxin[u] == max1) ? max2 : max1; n     mxout[u] = max(1 + mxout[v], 2 + used); n     if (mxout[u] < 0 && marked[u]) mxout[u] = 0; n     dfs_up(u, v); n   } n } n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cin >> n >> m >> d; n   int x, y; n   for (int(i) = 0; (i) < (m); ++(i)) { n     cin >> x; n     marked[x] = 1; n   } n   for (int(i) = 0; (i) < (n - 1); ++(i)) { n     cin >> x >> y; n     g[x].push_back(y); n     g[y].push_back(x); n   } n   dfs_down(1, 0); n   mxout[1] = (marked[1] ? 0 : -inf); n   dfs_up(1, 0); n   int ans = 0; n   for (int i = 1; i <= n; i++) ans += (mxin[i] <= d && mxout[i] <= d); n   cout << ans <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, V, A[64], B[64]; n bool canCook(double x) { n   double tot = 0.0; n   for (int i = 0; i < n; i++) n     if (A[i] * x > B[i]) n       return 0; n     else n       tot += A[i] * x; n   return tot <= V; n } n double totalAmount(double x) { n   double tot = 0.0; n   for (int i = 0; i < n; i++) tot += A[i] * x; n   return tot; n } n int main() { n   scanf( %d%d , &n, &V); n   for (int i = 0; i < n; i++) scanf( %d , &A[i]); n   for (int i = 0; i < n; i++) scanf( %d , &B[i]); n   double lo = 0.0, hi = double(2 * V), mid; n   while (abs(hi - lo) >= 1e-6) { n     mid = (hi + lo) / 2; n     if (canCook(mid)) n       lo = mid; n     else n       hi = mid; n   } n   printf( %.6f n , totalAmount(lo)); n } n


#include <bits/stdc++.h> n using namespace std; n map<long long, int> mp; n long long n, k, i, v[500005], val[500005], x, sum[500005], fr[500005], n     frec[500005], fr3[500005], nr, sum2[500005], bucket, val1, m, st, dr, s, n     stcur, drcur, sol[500005], fr2[500005], val2, b[500005]; n struct wow { n   long long st, dr, poz; n } a[500005]; n long long cautbin(long long x) { n   long long st = 0, dr = n, mij, sol = 0; n   while (st <= dr) { n     mij = (st + dr) / 2; n     if (sum2[mij] <= x) { n       if (sum2[mij] == x) { n         sol = mij; n       } n       st = mij + 1; n     } else { n       dr = mij - 1; n     } n   } n   return sol; n } n bool compare(wow a, wow b) { n   if (a.st / bucket != b.st / bucket) { n     return a.st < b.st; n   } n   return a.dr < b.dr; n } n int main() { n   cin >> n >> k; n   for (i = 1; i <= n; i++) { n     cin >> x; n     if (x == 1) { n       val[i] = 1; n     } else { n       val[i] = -1; n     } n   } n   for (i = 1; i <= n; i++) { n     cin >> x; n     v[i] = x * val[i]; n   } n   for (i = 1; i <= n; i++) { n     sum[i] = sum[i - 1] + v[i]; n   } n   for (i = 0; i <= n; i++) { n     mp[sum[i] - k] = mp[sum[i]] = mp[sum[i] + k] = 0; n   } n   nr = 0; n   for (auto &a : mp) { n     a.second = ++nr; n   } n   for (i = 0; i <= n; i++) { n     fr[i] = mp[sum[i] - k]; n     fr2[i] = mp[sum[i]]; n     fr3[i] = mp[sum[i] + k]; n   } n   cin >> m; n   for (i = 1; i <= m; i++) { n     cin >> a[i].st >> a[i].dr; n     a[i].st--; n     a[i].poz = i; n   } n   bucket = 300; n   sort(a + 1, a + m + 1, compare); n   st = 0; n   dr = -1; n   s = 0; n   for (i = 1; i <= m; i++) { n     stcur = a[i].st; n     drcur = a[i].dr; n     while (st > stcur) { n       st--; n       s = s + frec[fr3[st]]; n       frec[fr2[st]]++; n     } n     while (dr < drcur) { n       dr++; n       s = s + frec[fr[dr]]; n       frec[fr2[dr]]++; n     } n     while (st < stcur) { n       frec[fr2[st]]--; n       s = s - frec[fr3[st]]; n       st++; n     } n     while (dr > drcur) { n       frec[fr2[dr]]--; n       s = s - frec[fr[dr]]; n       dr--; n     } n     sol[a[i].poz] = s; n   } n   for (i = 1; i <= m; i++) { n     cout << sol[i] <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long n, a[200005], sum, cnt, ans; n int main() { n   cin >> n; n   for (int i = 1; i <= n; i++) { n     scanf( %lld , &a[i]); n     sum += a[i]; n   } n   for (int i = 1; i <= n; i += 2) cnt += a[i]; n   ans = cnt; n   for (int i = 1; i < n; i++) { n     cnt = sum - cnt + a[i]; n     ans = max(ans, cnt); n   } n   printf( %lld n , ans); n } n


#include <bits/stdc++.h> n using namespace std; n long long m; n long long p(long long x) { n   long long kmx = 0; n   while ((kmx * kmx * kmx) <= x) kmx++; n   kmx--; n   long long ret = 0; n   for (long long i = 2; i <= kmx; i++) ret += x / (i * i * i); n   return ret; n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   cin >> m; n   long long ans = 0; n   long long lo = 1, hi = 1e16; n   while (lo <= hi) { n     long long mi = (lo + hi) / 2; n     if (p(mi) >= m) { n       ans = mi; n       hi = mi - 1; n     } else n       lo = mi + 1; n   } n   if (p(ans) != m) ans = -1; n   cout << ans <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxx = 200010; n unsigned long long t, n; n unsigned long long ans = 0, total = 0, sum = 0; n unsigned long long a[maxx], b[maxx], bj[maxx]; n int main() { n   scanf( %lld , &t); n   while (t--) { n     scanf( %lld , &n); n     ans = 0, total = 0, sum = 1; n     while (n >= sum) { n       unsigned long long opt = sum; n       total++; n       sum *= 2; n       ans += total + (n - opt) / sum * total; n     } n     printf( %lld n , ans); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 3e5 + 5; n const long long inf = 1e18 + 9; n int n, m, k, q; n vector<pair<int, int> > g[maxn]; n pair<long long, pair<int, int> > edges[maxn]; n pair<int, int> query[maxn]; n struct data { n   int id; n   long long val; n   bool operator<(const data& other) const { return val > other.val; } n }; n long long d[maxn]; n int lab[maxn]; n int low[maxn], high[maxn]; n vector<int> vec[maxn]; n void read_input() { n   cin >> n >> m >> k >> q; n   for (int i = 1; i <= m; ++i) { n     int u, v, w; n     cin >> u >> v >> w; n     edges[i] = make_pair(w, pair<int, int>(u, v)); n     g[u].push_back(pair<int, int>(v, w)); n     g[v].push_back(pair<int, int>(u, w)); n   } n   for (int i = 1; i <= q; ++i) { n     int u, v; n     cin >> u >> v; n     query[i] = pair<int, int>(u, v); n   } n } n void dijkstra() { n   priority_queue<data> pq; n   fill(d + 1, d + n + 1, inf); n   for (int i = 1; i <= k; ++i) { n     d[i] = 0; n     pq.push({i, 0}); n   } n   while (!pq.empty()) { n     data tp = pq.top(); n     pq.pop(); n     if (tp.val != d[tp.id]) continue; n     int u = tp.id; n     for (auto& to : g[u]) { n       int v = to.first, w = to.second; n       if (d[v] > d[u] + w) { n         d[v] = d[u] + w; n         pq.push({v, d[v]}); n       } n     } n   } n } n int find_set(int u) { return lab[u] < 0 ? u : lab[u] = find_set(lab[u]); } n void union_sets(int u, int v) { n   if (lab[u] < lab[v]) swap(u, v); n   lab[v] += lab[u]; n   lab[u] = v; n } n void solve() { n   dijkstra(); n   for (int i = 1; i <= m; ++i) { n     int u = edges[i].second.first, v = edges[i].second.second; n     edges[i].first += d[u] + d[v]; n   } n   sort(edges + 1, edges + m + 1); n   for (int i = 1; i <= q; ++i) { n     low[i] = 1; n     high[i] = m; n   } n   while (true) { n     bool stop = true; n     for (int i = 1; i <= q; ++i) n       if (low[i] <= high[i]) { n         stop = false; n         int mid = (low[i] + high[i]) / 2; n         vec[mid].push_back(i); n       } n     if (stop) break; n     fill(lab + 1, lab + n + 1, -1); n     for (int i = 1; i <= m; ++i) { n       int u = edges[i].second.first, v = edges[i].second.second; n       u = find_set(u); n       v = find_set(v); n       if (u != v) union_sets(u, v); n       while ((int)vec[i].size()) { n         int id = vec[i].back(); n         vec[i].pop_back(); n         if (find_set(query[id].first) == find_set(query[id].second)) n           high[id] = i - 1; n         else n           low[id] = i + 1; n       } n     } n   } n   for (int i = 1; i <= q; ++i) cout << edges[low[i]].first <<   n ; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(nullptr); n   read_input(); n   solve(); n } n


#include <bits/stdc++.h> n using namespace std; n bool in_range(long long l, long long v, long long r) { n   return (l <= v && v <= r); n } n int main() { n   long long x; n   cin >> x; n   x = ((-x) % 360 + 360) % 360; n   int ans = 0; n   if (x >= 225 && x < 315) n     ans = 1; n   else if (x >= 135 && x < 225) n     ans = 2; n   else if (x > 45 && x < 135) n     ans = 3; n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n inline pair<int, int> best(pair<int, int> a, pair<int, int> b) { n   if ((a.first > b.first) || ((a.first == b.first) && (a.second < b.second))) n     return a; n   return b; n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   int n, m; n   string s; n   cin >> n; n   cin >> s; n   cin >> m; n   vector<vector<int> > ka(n, vector<int>(2)); n   vector<vector<int> > kb(n, vector<int>(2)); n   vector<int> kv(n); n   vector<int> preva(n, -1); n   if (s[0] ==  a ) { n     preva[0] = 0; n     ka[0][0] = 1; n   } n   if (s[0] ==  b ) kb[0][0] = 1; n   if (s[0] ==  ? ) kv[0] = 1, preva[0] = 0; n   for (int i = 1; i < n; i++) { n     ka[i][0] = ka[i - 1][0]; n     ka[i][1] = ka[i - 1][1]; n     if (s[i] ==  a ) ka[i][i % 2]++; n     kb[i][0] = kb[i - 1][0]; n     kb[i][1] = kb[i - 1][1]; n     if (s[i] ==  b ) kb[i][i % 2]++; n     if ((s[i] ==  a ) || (s[i] ==  ? )) n       preva[i] = i; n     else n       preva[i] = preva[i - 1]; n     kv[i] = kv[i - 1]; n     if (s[i] ==  ? ) kv[i]++; n   } n   vector<vector<pair<int, int> > > dp(n, vector<pair<int, int> >(2)); n   if (((s[0] ==  a ) || (s[0] ==  ? )) && (m <= n) && (kb[m - 1][0] == 0) && n       (ka[m - 1][1] == 0)) n     dp[0][1] = {1, kv[m - 1]}; n   for (int i = 1; i < n; i++) { n     dp[i][0] = best(dp[i - 1][0], dp[i - 1][1]); n     if (i + m - 1 >= n) continue; n     int bada = ka[i + m - 1][(i % 2) ^ 1] - ka[i - 1][(i % 2) ^ 1]; n     int badb = kb[i + m - 1][i % 2] - kb[i - 1][i % 2]; n     if ((bada != 0) || (badb != 0)) continue; n     if (i < m) { n       dp[i][1] = {1, kv[i + m - 1] - kv[i - 1]}; n     } else { n       int p = preva[i - m]; n       if (p < 0) { n         dp[i][1] = {1, kv[i + m - 1] - kv[i - 1]}; n       } else { n         dp[i][1] = best(dp[p][0], dp[p][1]); n         dp[i][1].first++; n         dp[i][1].second += kv[i + m - 1] - kv[i - 1]; n       } n     } n   } n   pair<int, int> rez = best(dp[n - 1][0], dp[n - 1][1]); n   cout << rez.second <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n class Solution { n  public: n   void solve(std::istream& in, std::ostream& out) { n     long long n, k, b, c, a, d; n     in >> n >> k >> b >> c; n     b = min(b, 5 * c); n     vector<pair<long long, long long>> x(n); n     for (int i = 0; i < n; i++) { n       in >> a; n       x[i] = make_pair(a, 0); n     } n     for (int i = 0; i < n; i++) { n       for (int j = 1; j < 5; j++) { n         x.push_back(make_pair(x[i].first + j, -j)); n       } n     } n     n = x.size(); n     vector<vector<pair<long long, long long>>> y(5), z(5); n     for (int i = 0; i < n; i++) { n       a = x[i].first % 5; n       if (a < 0) { n         a = a + 5; n       } n       y[a].push_back(x[i]); n     } n     for (int i = 0; i < 5; i++) { n       sort(begin(y[i]), end(y[i])); n       int t = y[i].size(); n       long long h = y[i].back().first; n       z[i] = vector<pair<long long, long long>>(t); n       for (int j = 0; j < t; j++) { n         z[i][j] = n             make_pair(-((h - y[i][j].first) / 5 * b - y[i][j].second * c), j); n       } n       sort(begin(z[i]), end(z[i])); n     } n     long long m = 100000000000000000; n     for (int i = 0; i < 5; i++) { n       int t = y[i].size(); n       priority_queue<pair<int, int>> q; n       long long p = 0; n       int l = t - 1, r = t - 1; n       long long h; n       while (l >= 0) { n         if (r < 0) break; n         d = y[i][r].first; n         if (q.size()) { n           a = q.top().first; n           p -= (h - a) / 5 * b * q.size(); n           h = a; n         } n         while (q.size() && d < q.top().first) { n           p += q.top().second * c; n           q.pop(); n           if (q.size()) { n             a = q.top().first; n             p -= (h - a) / 5 * b * q.size(); n             h = a; n           } else n             p = 0; n         } n         if (q.size()) { n           a = q.top().first; n           p += (d - a) / 5 * b * q.size(); n         } n         while (l >= 0 && q.size() < k) { n           a = z[i][l].second; n           if (y[i][a].first > d) { n             l--; n             continue; n           } n           p -= y[i][a].second * c; n           p += (d - y[i][a].first) / 5 * b; n           q.push(y[i][a]); n           l--; n         } n         if (q.size() == k) m = min(m, p); n         h = d; n         r--; n         while (r >= 0 && y[i][r].first == y[i][r + 1].first) r--; n       } n     } n     out << m; n   } n }; n void solve(std::istream& in, std::ostream& out) { n   out << std::setprecision(12); n   Solution solution; n   solution.solve(in, out); n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   istream& in = cin; n   ostream& out = cout; n   solve(in, out); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, a[100000], s = 0, nz = 0; n   cin >> n; n   for (int i = 0; i < n; i++) cin >> a[i]; n   sort(a, a + n); n   for (int i = 0; i < n; i++) { n     if (a[i] == a[i + 1] && a[i] != 0) s++; n     if (a[i] == 0) nz++; n   } n   cout << n - s - nz << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n string now, st; n vector<int> num[26]; n int main(void) { n   int n, k; n   cin >> n >> k; n   cin >> st; n   for (int i = 0; i < (k); i++) { n     num[st[i] -  a ].push_back(i); n   } n   for (int Case = 0; Case < (n); Case++) { n     cin >> now; n     long long res = 0; n     int len = now.size(); n     for (int i = 0; i < (len); i++) { n       if (i >= k || st[i] != now[i]) { n         int alpha = now[i] -  a ; n         if (num[alpha].empty()) { n           res += len; n           continue; n         } n         int left = 0, right = num[alpha].size() - 1, mid; n         while (left < right) { n           mid = (left + right + 1) >> 1; n           if (num[alpha][mid] <= i) n             left = mid; n           else n             right = mid - 1; n         } n         int tmp = abs(i - num[alpha][left]); n         if (left + 1 < num[alpha].size()) n           tmp = min(tmp, abs(i - num[alpha][left + 1])); n         res += tmp; n       } n     } n     cout << res << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int mn = 500005; n int n, fp, ln; n pair<double, double> inp[mn]; n bool f(double mid) { n   double ma = -(int)1e9; n   for (int i = fp; i < ln + 1; i++) { n     double pos = inp[i].first + mid * inp[i].second; n     if (inp[i].second < 0) { n       if (pos <= ma) return 1; n     } else n       ma = max(ma, pos); n   } n   return 0; n } n int main() { n   n = ({ n     int t; n     scanf( %d , &t); n     t; n   }); n   for (int i = 0; i < n; i++) n     inp[i].first = ({ n       int t; n       scanf( %d , &t); n       t; n     }), n     inp[i].second = ({ n       int t; n       scanf( %d , &t); n       t; n     }); n   fp = ln = -1; n   for (int i = n - 1; i >= 0; i--) n     if (inp[i].second > 0) fp = i; n   for (int i = 0; i < n; i++) n     if (inp[i].second < 0) ln = i; n   if (fp == -1 || ln == -1 || fp > ln) { n     printf( -1 n ); n     return 0; n   } n   double lo = 0, hi = double(1e9) + 5, mid = (lo + hi) / 2; n   for (int times = 0; times < 100; times++) { n     if (f(mid)) n       hi = mid; n     else n       lo = mid; n     mid = (lo + hi) / 2; n   } n   printf( %.15lf n , mid); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n using ll = long long; n using pii = pair<int, int>; n using pll = pair<ll, ll>; n using ld = long double; n const int mod = 1e9 + 7; n const ll inf = 2e9; n const ll INF = 2e18; n const ld EPS = 1e-7; n const int N = 1e5 + 55; n ll sx[N], sy[N]; n void solve() { n   int n, m; n   cin >> n >> m; n   vector<vector<int>> a(n, vector<int>(m)); n   for (int i = 0; i < n; ++i) { n     for (int j = 0; j < m; ++j) { n       cin >> a[i][j]; n       sx[i] += a[i][j]; n       sy[j] += a[i][j]; n     } n   } n   ll mx = 0; n   vector<ll> v; n   for (int i = 0; i < n; ++i) { n     v.clear(); n     for (int j = 0; j < m; ++j) { n       v.push_back(sy[j] - a[i][j]); n       sort(v.rbegin(), v.rend()); n       while (v.size() > 3) { n         v.pop_back(); n       } n     } n     ll cur = sx[i]; n     for (ll a : v) { n       cur += a; n     } n     mx = max(mx, cur); n   } n   for (int j = 0; j < m; ++j) { n     v.clear(); n     for (int i = 0; i < n; ++i) { n       v.push_back(sx[i] - a[i][j]); n       sort(v.rbegin(), v.rend()); n       while (v.size() > 3) { n         v.pop_back(); n       } n     } n     ll cur = sy[j]; n     for (ll a : v) { n       cur += a; n     } n     mx = max(mx, cur); n   } n   if (n > m) { n     for (int i = 0; i < m; ++i) { n       for (int k = i + 1; k < m; ++k) { n         v.clear(); n         for (int j = 0; j < n; ++j) { n           v.push_back(sx[j] - a[j][i] - a[j][k]); n           sort(v.rbegin(), v.rend()); n           while (v.size() > 2) { n             v.pop_back(); n           } n         } n         ll cur = sy[i] + sy[k]; n         for (ll a : v) { n           cur += a; n         } n         mx = max(mx, cur); n       } n     } n   } else { n     for (int i = 0; i < n; ++i) { n       for (int k = i + 1; k < n; ++k) { n         v.clear(); n         for (int j = 0; j < m; ++j) { n           v.push_back(sy[j] - a[i][j] - a[k][j]); n           sort(v.rbegin(), v.rend()); n           while (v.size() > 2) { n             v.pop_back(); n           } n         } n         ll cur = sx[i] + sx[k]; n         for (ll a : v) { n           cur += a; n         } n         mx = max(mx, cur); n       } n     } n   } n   sort(sx, sx + n); n   sort(sy, sy + m); n   for (ll i = n - 1, sum = 0; i >= max(0, n - 4); --i) { n     sum += sx[i]; n     mx = max(mx, sum); n   } n   for (ll i = m - 1, sum = 0; i >= max(0, m - 4); --i) { n     sum += sy[i]; n     mx = max(mx, sum); n   } n   cout << mx; n } n int main() { n   ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); n   ; n   int tt = 1; n   while (tt--) { n     solve(); n     cout <<   n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e6, M = 1e9 + 7; n int gcd(int a, int b) { n   if (!b) return a; n   return gcd(b, a % b); n } n int n, x[N], y[N]; n map<pair<long double, long double>, int> mp; n int t(int i, int j) { n   long double dX = x[i] - x[j]; n   long double dY = y[i] - y[j]; n   long double g = gcd(dX, dY); n   dX /= g, dY /= g; n   vector<int> r; n   for (int k = 0; k < n; k++) { n     if (k == i || k == j) continue; n     long double tmpx = x[i] - x[k]; n     long double tmpy = y[i] - y[k]; n     long double g = gcd(tmpx, tmpy); n     tmpx /= g, tmpy /= g; n     if (tmpx == dX && tmpy == dY) n       continue; n     else n       r.push_back(k); n   } n   if (r.size() < 2) return true; n   long double x2 = x[r[0]] - x[r[1]]; n   long double y2 = y[r[0]] - y[r[1]]; n   g = gcd(x2, y2); n   if (g) x2 /= g, y2 /= g; n   for (int i = 1; i < r.size(); i++) { n     long double tmpx = x[r[0]] - x[r[i]]; n     long double tmpy = y[r[0]] - y[r[i]]; n     long double g = gcd(tmpx, tmpy); n     if (g) tmpx /= g, tmpy /= g; n     if (x2 != tmpx || y2 != tmpy) return false; n   } n   return true; n } n int main() { n   scanf( %d , &n); n   int cnt = 0; n   for (int i = 0; i < n; i++) scanf( %d%d , &x[i], &y[i]); n   for (int i = 1; i < n; i++) { n     long double dX = x[i] - x[0]; n     long double dY = y[i] - y[0]; n     long double g = gcd(dX, dY); n     dX /= g, dY /= g; n     if (!mp[{dX, dY}]) mp[{dX, dY}] = i, cnt++; n     if (cnt == 2) break; n   } n   if (cnt < 2) { n     return puts( YES ), 0; n   } else { n     vector<int> v; n     for (auto it = mp.begin(); it != mp.end(); it++) { n       v.push_back(it->second); n       if (t(0, it->second)) return puts( YES ); n     } n     if (t(v[0], v[1])) return puts( YES ); n   } n   puts( NO ); n } n


#include <bits/stdc++.h> n using namespace std; n long long n, a, b; n int main() { n   cin >> n; n   a = (n + 2) * (n + 1) / 2 * n / 3; n   b = (n + 4) * (n + 3) / 2 * (n + 2) / 3 * (n + 1) / 4 * n / 5; n   cout << a * b; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e6 + 5; n const int MOD = 1e9 + 7; n vector<pair<long long int, long long int> > vp; n map<long long int, long long int> mp, mpp; n vector<long long int> v, v1, v2; n long long int dp[N], visit[N]; n template <typename Arg1> n void __f(const char* name, Arg1&& arg1) { n   cerr << name <<   :   << arg1 << std::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   cerr.write(names, comma - names) <<   :   << arg1 <<     ; n   __f(comma + 1, args...); n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(NULL); n   cout.tie(NULL); n   long long int n, m, val, k, d, mi = 1e9, mx(0); n   long long int sum(0), ct(0), p(0), q(0); n   cin >> n >> m; n   if (n <= 2 * m) n     cout << 0 <<    ; n   else n     cout << n - 2 * m <<    ; n   long long int low = 0, high = n; n   while (low <= high) { n     long long int mid = (low + high) >> 1; n     if (((mid * (mid - 1) / 2)) >= m) { n       high = mid - 1; n     } else { n       low = mid + 1; n     } n   } n   cout << n - (high + 1); n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   scanf( %d , &(n)); n   static long long a[int(5e3 + 5)]; n   set<int> s; n   for (int i = 1, __R = n; i <= __R; i++) { n     scanf( %lld , &(a[i])); n     s.insert(a[i]); n   } n   static long long b[int(5e3 + 5)], k = 0; n   for (long long x : s) b[++k] = x; n   static long long dp[2][int(5e3 + 5)]; n   int lst = 0, cur = 1; n   dp[lst][1] = abs(a[1] - b[1]); n   for (int j = 2, __R = k; j <= __R; j++) n     dp[lst][j] = min(dp[lst][j - 1], abs(a[1] - b[j])); n   for (int i = 2, __R = n; i <= __R; i++) { n     dp[cur][1] = dp[lst][1] + abs(a[i] - b[1]); n     for (int j = 2, __R = k; j <= __R; j++) n       dp[cur][j] = min(dp[cur][j - 1], dp[lst][j] + abs(a[i] - b[j])); n     swap(lst, cur); n   } n   cout << dp[lst][k] << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int count1[100005], count2[100005], count3[100005]; n bool can_be_palin(int *a, int size) { n   int i, no_of_odd = 0; n   for (i = 0; i < size; i++) no_of_odd += a[i] % 2; n   return (no_of_odd <= 1); n } n bool is_palin(int *a, int l, int r) { n   for (int i = l; i <= (l + r) / 2; i++) n     if (a[i] != a[r + l - i]) return false; n   return true; n } n bool func(int *a, int n, int end, int l, int r) { n   int i; n   fill(count1, count1 + n + 1, 0); n   fill(count2, count2 + n + 1, 0); n   if (end - l + 1 >= r - end) { n     for (i = l; i <= end; i++) count1[a[i]]++; n     for (i = r; i > end; i--) count2[a[i]]++; n     for (i = 1; i <= n; i++) { n       if (count2[i] > count1[i]) return false; n       count1[i] -= count2[i]; n     } n     return can_be_palin(count1, n + 1); n   } else { n     for (i = l; i <= end; i++) count1[a[i]]++; n     for (i = r; i >= r - end + l; i--) count2[a[i]]++; n     for (i = 1; i <= n; i++) { n       if (count2[i] > count1[i]) return false; n       count1[i] -= count2[i]; n     } n     return can_be_palin(count1, n + 1) && is_palin(a, end + 1, r - end + l - 1); n   } n } n bool func2(int *a, int n, int end, int l, int r) { n   int i; n   fill(count1, count1 + n + 1, 0); n   fill(count2, count2 + n + 1, 0); n   if (r - end + 1 >= end - l) { n     for (i = r; i >= end; i--) count1[a[i]]++; n     for (i = l; i < end; i++) count2[a[i]]++; n     for (i = 1; i <= n; i++) { n       if (count2[i] > count1[i]) return false; n       count1[i] -= count2[i]; n     } n     return can_be_palin(count1, n + 1); n   } else { n     for (i = r; i >= end; i--) count1[a[i]]++; n     for (i = l; i <= l + r - end; i++) count2[a[i]]++; n     for (i = 1; i <= n; i++) { n       if (count2[i] > count1[i]) return false; n       count1[i] -= count2[i]; n     } n     return can_be_palin(count1, n + 1) && is_palin(a, r - end + l + 1, end - 1); n   } n } n int main() { n   int n, i; n   scanf( %d , &n); n   long long ans = 0; n   int a[n]; n   for (i = 0; i < n; i++) scanf( %d , &a[i]); n   for (i = 0; i < n / 2 && a[i] == a[n - 1 - i]; i++) n     ; n   if (i == n / 2) { n     printf( %I64d , (long long)n * (n + 1) / 2); n     return 0; n   } n   int l = i, r = n - 1 - i, left_prev = l, right_prev = r, end = (l + r) / 2; n   int prev_pos = -1; n   while (left_prev < end - 1 && end < right_prev - 1) { n     bool flag = true; n     flag = func(a, n, end, l, r); n     int mid1 = (end + left_prev) / 2; n     int mid2 = (end + right_prev) / 2; n     if (flag == false) { n       left_prev = end; n       end = mid2; n     } else { n       right_prev = end; n       prev_pos = end; n       end = mid1; n     } n   } n   for (int k = left_prev; k < r && k < right_prev; k++) n     if (func(a, n, k, l, r) == true) n       prev_pos = (prev_pos == -1) ? k : min(prev_pos, k); n   if (prev_pos != -1) ans += (long long)(l + 1) * (r - prev_pos); n   right_prev = r, left_prev = l, end = (l + r) / 2; n   prev_pos = -1; n   while (end < right_prev - 1 && left_prev < end - 1) { n     bool flag = true; n     flag = func2(a, n, end, l, r); n     int mid1 = (end + right_prev) / 2; n     int mid2 = (end + left_prev) / 2; n     if (flag == false) { n       right_prev = end; n       end = mid2; n     } else { n       left_prev = end; n       prev_pos = end; n       end = mid1; n     } n   } n   for (int k = right_prev; k > l && k > left_prev; k--) n     if (func2(a, n, k, l, r) == true) n       prev_pos = (prev_pos == -1) ? k : max(prev_pos, k); n   if (prev_pos != -1) ans += (long long)(n - r) * (prev_pos - l); n   fill(count1, count1 + n + 1, 0); n   for (i = l; i <= r; i++) count1[a[i]]++; n   if (can_be_palin(count1, n + 1) == true) ans += (long long)(l + 1) * (n - r); n   printf( %I64d , ans); n } n


#include <bits/stdc++.h> n #pragma GCC optimize( Ofast ) n #pragma GCC optimization( unroll-loops ) n std::pair<int, int> DR[] = {{-1, 0}, {0, 1},   {1, 0}, {0, -1}, n                             {-1, 1}, {-1, -1}, {1, 1}, {1, -1}}; n using namespace std; n template <class c> n struct rge { n   c b, e; n }; n template <class c> n rge<c> range(c i, c j) { n   return rge<c>{i, j}; n } n template <class c> n auto dud(c* x) -> decltype(cerr << *x, 0); n template <class c> n char dud(...); n struct debug { n   ~debug() { cerr << endl; } n   template <class c> n   typename enable_if<sizeof dud<c>(0) != 1, debug&>::type operator<<(c i) { n     cerr << boolalpha << i; n     return *this; n   } n   template <class c> n   typename enable_if<sizeof dud<c>(0) == 1, debug&>::type operator<<(c i) { n     return *this << range(begin(i), end(i)); n   } n   template <class c, class b> n   debug& operator<<(pair<b, c> d) { n     return *this <<  (  << d.first <<  ,   << d.second <<  ) ; n   } n   template <class c> n   debug& operator<<(rge<c> d) { n     *this <<  [ ; n     for (auto it = d.b; it != d.e; ++it) *this <<  ,   + 2 * (it == d.b) << *it; n     return *this <<  ] ; n   } n }; n long long gcd(long long a, long long b) { n   if (b) return gcd(b, a % b); n   return a; n } n mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); n long long n, m, a, b, g[3005 * 3005], val[3005][3005], mn[3005][3005], x, y, z, n     ans; n int32_t main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cerr.tie(0); n   cout.tie(0); n   cin >> n >> m >> a >> b; n   cin >> g[0] >> x >> y >> z; n   for (long long i = 1; i < n * m; i++) { n     g[i] = (g[i - 1] * x + y) % z; n   } n   for (long long i = 0; i < n; i++) { n     for (long long j = 0; j < m; j++) { n       val[i][j] = g[i * m + j]; n     } n   } n   for (long long j = 0; j < m; j++) { n     deque<long long> dq; n     for (long long i = 0; i < a; i++) { n       while ((long long)((dq).size()) && val[dq.back()][j] >= val[i][j]) n         dq.pop_back(); n       dq.push_back(i); n     } n     mn[0][j] = val[dq.front()][j]; n     for (long long i = 1; i <= n - a; i++) { n       if (dq.front() == i - 1) dq.pop_front(); n       while ((long long)((dq).size()) && val[dq.back()][j] >= val[i + a - 1][j]) n         dq.pop_back(); n       dq.push_back(i + a - 1); n       mn[i][j] = val[dq.front()][j]; n     } n   } n   for (long long i = 0; i <= n - a; i++) { n     deque<long long> dq; n     for (long long j = 0; j < b; j++) { n       while ((long long)((dq).size()) && mn[i][dq.back()] >= mn[i][j]) n         dq.pop_back(); n       dq.push_back(j); n     } n     ans += mn[i][dq.front()]; n     for (long long j = 1; j <= m - b; j++) { n       if (dq.front() == j - 1) dq.pop_front(); n       while ((long long)((dq).size()) && mn[i][dq.back()] >= mn[i][j + b - 1]) n         dq.pop_back(); n       dq.push_back(j + b - 1); n       ans += mn[i][dq.front()]; n     } n   } n   return cout << ans, 0; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); n inline int rand(int x, int y) { n   ++y; n   return (rng() % (y - x)) + x; n } n string to_string(char c) { n   string second(1, c); n   return second; n } n string to_string(bool b) { return (b ?  true  :  false ); } n template <typename A, typename B> n string to_string(pair<A, B> p) { n   return  (  + to_string(p.first) +  ,   + to_string(p.second) +  ) ; n } n template <typename A> n string to_string(A v) { n   bool first = true; n   string res =  { ; n   for (const auto &x : v) { n     if (!first) { n       res +=  ,  ; n     } n     first = false; n     res += to_string(x); n   } n   res +=  } ; n   return res; n } n void degug_out() { cerr << endl; } n template <typename Head, typename... Tail> n void degug_out(Head H, Tail... T) { n   cerr <<     << to_string(H); n   degug_out(T...); n } n inline int gcd(int a, int b) { n   if (a > b) swap(a, b); n   if (a == 0) return b; n   return gcd(b % a, a); n } n int n; n int read() { n   bool neg = false; n   int res = 0; n   char c = getchar(); n   while (true) { n     if (c ==  - ) break; n     if ( 0  <= c && c <=  9 ) break; n     c = getchar(); n   } n   if (c ==  - ) n     neg = true; n   else n     res = c -  0 ; n   while (true) { n     c = getchar(); n     if (c <  0  || c >  9 ) break; n     res = (res << 1) + (res << 3) + (c -  0 ); n   } n   if (neg) return -res; n   return res; n } n const int sz = 670; n int store[sz + 2][sz + 2]; n int A[(500006)]; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   n = read(); n   for (int i = (0); i <= (int)(n - 1); ++i) { n     int op, x, y; n     op = read(), x = read(), y = read(); n     if (op == 1) { n       for (int i = (1); i <= (int)(sz); ++i) { n         store[i][x % i] += y; n       } n       A[x] += y; n     } else { n       if (x > sz) { n         int ans = 0; n         for (int i = y; i <= 500000; i += x) ans += A[i]; n         cout << ans <<   n ; n       } else { n         cout << store[x][y] <<   n ; n       } n     } n   } n } n


#include <bits/stdc++.h> n int f[100005]; n int g[100005]; n int h[1000005]; n int v[100005]; n int main() { n   int n, m = 0; n   scanf( %d , &n); n   for (int i = 1; i <= n; i++) { n     scanf( %d , &f[i]); n   } n   for (int i = 1; i <= n; i++) { n     if (v[i] == 0) { n       if (f[i] == i) { n         m++; n         h[m] = i; n         v[i] = m; n         g[i] = m; n       } else { n         if (v[f[i]] == 0) { n           if (f[f[i]] == f[i]) { n             m++; n             h[m] = f[i]; n             v[f[i]] = m; n             g[f[i]] = m; n             g[i] = m; n           } else { n             puts( -1 ); n             return 0; n           } n         } else { n           g[i] = v[f[i]]; n         } n       } n     } n   } n   printf( %d n , m); n   for (int i = 1; i <= n; i++) { n     printf( %d  , g[i]); n   } n   puts(  ); n   for (int i = 1; i <= m; i++) { n     printf( %d  , h[i]); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 100005; n int a[MAXN], b[MAXN], c[MAXN]; n int tb[MAXN]; n int d, n; n long long x; n long long getNextX() { n   x = (x * 37 + 10007) % 1000000007; n   return x; n } n void initAB() { n   for (int i = 0; i < n; i = i + 1) { n     a[i] = i + 1; n   } n   for (int i = 0; i < n; i = i + 1) { n     swap(a[i], a[getNextX() % (i + 1)]); n   } n   for (int i = 0; i < n; i = i + 1) { n     if (i < d) n       b[i] = 1; n     else n       b[i] = 0; n   } n   for (int i = 0; i < n; i = i + 1) { n     swap(b[i], b[getNextX() % (i + 1)]); n   } n } n struct node { n   int p, num; n   bool operator<(const node &t) const { return num > t.num; } n }; n node nd[MAXN]; n int main() { n   while (cin >> n >> d >> x) { n     initAB(); n     int cnt = 10000000; n     for (int i = 0; i < n; i++) { n       nd[i].num = a[i]; n       nd[i].p = i; n     } n     sort(nd, nd + n); n     int l = 0; n     for (int i = 0; i < n; i++) { n       if (b[i] == 1) { n         tb[l++] = i; n       } n     } n     memset(c, -1, sizeof(c)); n     for (int i = 0; i < n; i++) { n       int p = nd[i].p; n       for (int j = 0; j < l && cnt > 0; j++) { n         if (p + tb[j] < n && c[p + tb[j]] == -1) { n           c[p + tb[j]] = a[p]; n         } n         cnt--; n       } n     } n     for (int i = 0; i < n; i++) { n       if (c[i] == -1) { n         c[i] = 0; n         for (int j = 0; j < l && i - tb[j] >= 0; j++) { n           c[i] = max(c[i], a[i - tb[j]] * b[tb[j]]); n         } n       } n       printf( %d n , c[i]); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long int t; n   cin >> t; n   while (t--) { n     long long int n; n     cin >> n; n     string s; n     cin >> s; n     long long int i = 0; n     string ans = s; n     string w; n     string temp; n     long long int k = 1; n     while (i != n) { n       if ((n - i) % 2 == 0) { n         w = s.substr(i, n - i) + s.substr(0, i); n       } else { n         temp = s.substr(0, i); n         reverse(temp.begin(), temp.end()); n         w = s.substr(i, n - i) + temp; n       } n       if (w.compare(ans) < 0) { n         ans = w; n         k = i + 1; n       } n       i += 1; n     } n     cout << ans << endl; n     cout << k << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n const double PI = acos(-1.0); n const double EPS = 1e-9; n using namespace std; n template <typename F, typename S> n ostream &operator<<(ostream &os, const pair<F, S> &p) { n   return os <<  (  << p.first <<  ,   << p.second <<  ) ; n } n template <typename T> n ostream &operator<<(ostream &os, const vector<T> &v) { n   os <<  { ; n   typename vector<T>::const_iterator it; n   for (it = v.begin(); it != v.end(); it++) { n     if (it != v.begin()) os <<  ,  ; n     os << *it; n   } n   return os <<  } ; n } n template <typename T> n ostream &operator<<(ostream &os, const set<T> &v) { n   os <<  [ ; n   typename set<T>::const_iterator it; n   for (it = v.begin(); it != v.end(); it++) { n     if (it != v.begin()) os <<  ,  ; n     os << *it; n   } n   return os <<  ] ; n } n template <typename F, typename S> n ostream &operator<<(ostream &os, const map<F, S> &v) { n   os <<  [ ; n   typename map<F, S>::const_iterator it; n   for (it = v.begin(); it != v.end(); it++) { n     if (it != v.begin()) os <<  ,  ; n     os << it->first <<   =   << it->second; n   } n   return os <<  ] ; n } n template <class T> n void deb_array(T *arr, int length) { n   for (int i = 0; i < length; i++) { n     cout << arr[i] <<    ; n   } n   cout <<   n ; n } n const int maxn = 500; n const double INF = 1e18 + 1; n double p[maxn][maxn], dp[maxn][maxn], prob[maxn][maxn]; n int n, m; n int main() { n   ios_base::sync_with_stdio(0); n   cin >> n; n   m = 1 << n; n   for (int i = 1; i <= m; i++) { n     for (int j = 1; j <= m; j++) { n       cin >> p[i][j]; n       p[i][j] /= 100; n     } n   } n   for (int i = 0; i <= m; i++) { n     for (int j = 0; j <= m; j++) { n       prob[i][j] = 1.0; n     } n   } n   for (int i = 1; i <= n; i++) { n     int k = (1 << i); n     int bonus = k >> 1; n     for (int j = 0; j <= m; j += k) { n       int b = j; n       int e = j + k; n       int m = (b + e) / 2; n       for (int c = j + 1; c <= m; c++) { n         prob[i][c] = 0; n         for (int d = m + 1; d <= e; d++) { n           prob[i][c] = prob[i][c] + prob[i - 1][c] * prob[i - 1][d] * p[c][d]; n         } n       } n       for (int c = m + 1; c <= e; c++) { n         prob[i][c] = 0; n         for (int d = j + 1; d <= m; d++) { n           prob[i][c] = prob[i][c] + prob[i - 1][c] * prob[i - 1][d] * p[c][d]; n         } n       } n       for (int c = m + 1; c <= e; c++) { n         if (prob[i][c] == 0 || (dp[i - 1][c] == 0 && i != 1)) { n           continue; n         } n         for (int d = j + 1; d <= m; d++) { n           dp[i][c] = n               max(dp[i][c], dp[i - 1][c] + dp[i - 1][d] + prob[i][c] * bonus); n         } n       } n       for (int c = j + 1; c <= m; c++) { n         if (prob[i][c] == 0 || (dp[i - 1][c] == 0 && i != 1)) { n           continue; n         } n         for (int d = m + 1; d <= e; d++) { n           dp[i][c] = n               max(dp[i][c], dp[i - 1][c] + dp[i - 1][d] + prob[i][c] * bonus); n         } n       } n     } n   } n   cout << fixed << setprecision(12) << *max_element(dp[n], dp[n] + m + 1) n        <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n const int INF = 200000000; n using namespace std; n int timer, lg, n; n vector<vector<int> > g; n vector<vector<int> > dp(200005); n int time_in[200005], time_out[200005], parent[200005], a[200005]; n void DFS(int, int); n bool issubtree(int, int); n int lca(int, int); n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   cin >> n; n   g.resize(n + 1); n   lg = log2(n + 1); n   for (int k = 0; k <= n; k++) dp[k].resize(lg + 1); n   for (int k = 2; k <= n; k++) { n     int x; n     cin >> x; n     a[k] = x; n     g[x].push_back(k); n     g[k].push_back(x); n   } n   int u = 1; n   int v = 2; n   int dist = 1; n   DFS(1, 0); n   cout << 1 <<    ; n   for (int k = 3; k <= n; k++) { n     int lca1 = lca(u, k); n     int lca2 = lca(v, k); n     if (lca1 > lca2) { n       if (lca1 >= dist) { n         v = k; n         dist = lca1; n       } n     } else { n       if (lca2 >= dist) { n         u = k; n         dist = lca2; n       } n     } n     cout << dist <<    ; n   } n   return 0; n } n void DFS(int v, int p) { n   parent[v] = p; n   dp[v][0] = p; n   for (int k = 1; k <= lg; k++) dp[v][k] = dp[dp[v][k - 1]][k - 1]; n   time_in[v] = timer++; n   for (int k = 0; k < g[v].size(); k++) { n     int to = g[v][k]; n     if (to == p) continue; n     DFS(to, v); n   } n   time_out[v] = timer++; n } n bool issubtree(int a, int b) { n   if (time_in[a] <= time_in[b] && time_out[b] <= time_out[a]) return 1; n   return 0; n } n int lca(int a, int b) { n   if (issubtree(a, b)) { n     int cnt = 0; n     for (int k = lg; k >= 0; k--) { n       if (dp[b][k] == 0) continue; n       if (issubtree(dp[b][k], a)) continue; n       b = dp[b][k]; n       cnt += pow(2, k); n     } n     return cnt + 1; n   } n   if (issubtree(b, a)) { n     int cnt = 0; n     for (int k = lg; k >= 0; k--) { n       if (dp[a][k] == 0) continue; n       if (issubtree(dp[a][k], b)) continue; n       a = dp[a][k]; n       cnt += pow(2, k); n     } n     return cnt + 1; n   } n   int cnt = 0; n   int sav = a; n   for (int k = lg; k >= 0; k--) { n     if (dp[a][k] == 0) continue; n     if (issubtree(dp[a][k], b)) continue; n     a = dp[a][k]; n     cnt += pow(2, k); n   } n   a = sav; n   for (int k = lg; k >= 0; k--) { n     if (dp[b][k] == 0) continue; n     if (issubtree(dp[b][k], a)) continue; n     b = dp[b][k]; n     cnt += pow(2, k); n   } n   return cnt + 2; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int a; n   string b; n   cin >> a >> b; n   while (a++) { n     int i = a; n     string comp =   ; n     while (i > 0) { n       int c = i % 10; n       if (c == 4 || c == 7) { n         comp = to_string(c) + comp; n       } n       i /= 10; n     } n     if (comp == b) { n       cout << a; n       return 0; n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n int h1, h2; n int a, b; n int ans = 0; n int main() { n   scanf( %d%d , &h1, &h2); n   scanf( %d%d , &a, &b); n   h1 += a * 8; n   if (h1 >= h2) n     printf( 0 n ); n   else if (h1 < h2 && b >= a) n     printf( -1 n ); n   else if (h1 < h2 && b < a) { n     int num1 = (h2 - h1) % ((a - b) * 12); n     if (num1 == 0) n       printf( %d n , (h2 - h1) / ((a - b) * 12)); n     else n       printf( %d n , (h2 - h1) / ((a - b) * 12) + 1); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n #pragma warning(disable : 4996) n class BIT { n  public: n   vector<int> bit; n   int size_ = 0; n   void init(int sz) { n     size_ = sz + 2; n     bit.resize(size_ + 2, 0); n   } n   void add(int pos, int x) { n     pos++; n     while (pos <= size_) { n       bit[pos] += x; n       pos += (pos & -pos); n     } n   } n   int sum(int pos) { n     int s = 0; n     pos++; n     while (pos >= 1) { n       s += bit[pos]; n       pos -= (pos & -pos); n     } n     return s; n   } n   int searchs(int pos) { n     int cl = 0, cr = size_ + 1, cm, minx = (1 << 30); n     for (int i = 0; i < 22; i++) { n       cm = ((cl + cr) >> 1); n       int v = sum(cm); n       if (v >= pos) { n         minx = min(minx, cm); n         cr = cm; n       } else { n         cl = cm; n       } n     } n     return minx; n   } n }; n int N, A[1 << 18]; n int Q, K[1 << 18], P[1 << 18], ans[1 << 18]; n vector<pair<int, int>> rem; n vector<int> G[1 << 18]; n BIT Z; n int main() { n   scanf( %d , &N); n   for (int i = 1; i <= N; i++) scanf( %d , &A[i]); n   scanf( %d , &Q); n   for (int i = 1; i <= Q; i++) scanf( %d%d , &K[i], &P[i]); n   for (int i = 1; i <= N; i++) rem.push_back(make_pair(-A[i], i)); n   for (int i = 1; i <= Q; i++) G[K[i]].push_back(i); n   sort(rem.begin(), rem.end()); n   Z.init(N + 2); n   for (int i = 1; i <= N; i++) { n     Z.add(rem[i - 1].second, 1); n     for (int j = 0; j < G[i].size(); j++) { n       int idx = G[i][j]; n       int val = Z.searchs(P[idx]); n       ans[idx] = A[val]; n     } n   } n   for (int i = 1; i <= Q; i++) printf( %d n , ans[i]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e5 + 10; n inline long long rd() { n   long long x = 0, w = 1; n   char ch = 0; n   while (ch <  0  || ch >  9 ) { n     if (ch ==  - ) w = -1; n     ch = getchar(); n   } n   while (ch >=  0  && ch <=  9 ) { n     x = (x << 3) + (x << 1) + (ch ^ 48); n     ch = getchar(); n   } n   return x * w; n } n int to[N << 2], nt[N << 2], hd[N << 1], dg[N << 1], tot = 1; n void add(int x, int y) { n   ++tot, to[tot] = y, nt[tot] = hd[x], hd[x] = tot, ++dg[x]; n   ++tot, to[tot] = x, nt[tot] = hd[y], hd[y] = tot, ++dg[y]; n } n bool cs[N << 2], ban[N << 2], v[N << 1]; n int n, m, a[N][2], b[N << 1]; n void dfs(int x) { n   v[x] = 1; n   for (int &i = hd[x]; i; i = nt[i]) { n     if (ban[i]) continue; n     int y = to[i]; n     ban[i] = ban[i ^ 1] = 1, cs[i] = 1; n     dfs(y); n   } n } n int main() { n   n = rd(); n   for (int i = 1; i <= n; ++i) n     a[i][0] = b[i * 2 - 1] = rd(), a[i][1] = b[i * 2] = rd() + 1; n   sort(b + 1, b + n + n + 1), m = unique(b + 1, b + n + n + 1) - b - 1; n   for (int i = 1; i <= n; ++i) { n     a[i][0] = lower_bound(b + 1, b + m + 1, a[i][0]) - b; n     a[i][1] = lower_bound(b + 1, b + m + 1, a[i][1]) - b; n     add(a[i][0], a[i][1]); n   } n   for (int i = 1, la = 0; i <= m; ++i) n     if (dg[i] & 1) { n       if (!la) n         la = i; n       else n         add(la, i), la = 0; n     } n   for (int i = 1; i <= m; ++i) n     if (!v[i]) dfs(i); n   for (int i = 1; i <= n; ++i) printf( %d  , cs[i << 1]); n   return 0; n } n


#include <bits/stdc++.h> n int main() { n   int d1, d2, d3, sum = 0; n   scanf( %d%d%d , &d1, &d2, &d3); n   if (d1 >= d2) { n     sum += d2; n   } else { n     sum += d1; n   } n   if (d1 + d2 >= d3) { n     sum += d3; n   } else { n     sum += d1 + d2; n   } n   if ((d1 >= d2) && (d1 <= d2 + d3)) { n     sum += d1; n   } else if ((d1 >= d2) && (d1 > d2 + d3)) { n     sum += d2 + d3; n   } else if ((d2 >= d1) && (d2 <= d1 + d3)) { n     sum += d2; n   } else { n     sum += d1 + d3; n   } n   printf( %d , sum); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 2000 * 100 + 10, mod = 1000000007; n long long a[maxn]; n int main() { n   ios::sync_with_stdio(false), cin.tie(0), cout.tie(0); n   int t; n   t = 1; n   while (t--) { n     long long n; n     cin >> n; n     for (int i = 0; i < n; i++) { n       cin >> a[i]; n     } n     cout <<  9??>>??0  << endl; n     for (int i = 0; i < 9; i++) cout << i <<  ??<>  << i + 1 << endl; n     cout <<  ??<>1  << endl; n     for (int i = 0; i <= 9; i++) cout <<  ?  << i <<  >>  << i <<  ?  << endl; n     for (int i = 0; i < 9; i++) cout << i <<  ?<>  << i + 1 << endl; n     cout <<  9?>>??0  << endl; n     cout <<  >>?  << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 1e6 + 3; n const int INF = 1e9; n const int MOD = 1e9 + 7; n int main() { n   long long n; n   long long ans = 1; n   cin >> n; n   long long p = 2; n   while (n > 1 && p * p <= n) { n     if (n % p) n       p++; n     else { n       n /= p; n       if (ans % p) ans *= p; n     } n   } n   if (ans % n) ans *= n; n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<int> r1, r2, cr; n int main() { n   int n, prev = 0; n   cin >> n; n   r1.push_back(0); n   r2.push_back(0); n   for (int i = 0; i < n - 1; i++) { n     int w; n     cin >> w; n     w += prev; n     prev = w; n     r1.push_back(w); n   } n   prev = 0; n   for (int i = 0; i < n - 1; i++) { n     int w; n     cin >> w; n     w += prev; n     prev = w; n     r2.push_back(w); n   } n   for (int i = 0; i < n; i++) { n     int w; n     cin >> w; n     cr.push_back(w); n   } n   int minl = cr[0] + r2[n - 1], pos = 0; n   for (int i = 0; i < n; i++) { n     int cal = cr[i] + r1[i] + (r2[n - 1] - r2[i]); n     if (cal < minl) { n       pos = i; n       minl = cal; n     } n   } n   int minl2 = 100000; n   for (int i = 0; i < n; i++) { n     if (i == pos) { n       continue; n     } n     int cal = cr[i] + r1[i] + (r2[n - 1] - r2[i]); n     if (cal < minl2) { n       minl2 = cal; n     } n   } n   cout << minl + minl2; n } n


#include <bits/stdc++.h> n using namespace std; n const double pi = acos(-1.0); n const double eps = 1e-11; n const int INFINITE = 0x3f3f3f3f; n template <class T> n inline void checkmin(T &a, T b) { n   if (b < a) a = b; n } n template <class T> n inline void checkmax(T &a, T b) { n   if (b > a) a = b; n } n template <class T> n inline T sqr(T x) { n   return x * x; n } n template <class T> n inline T lowbit(T n) { n   return (n ^ (n - 1)) & n; n } n template <class T> n inline int countbit(T n) { n   return (n == 0) ? 0 : (1 + countbit(n & (n - 1))); n } n typedef vector<int> VI; n typedef vector<VI> VII; n typedef vector<string> VS; n const long long MAXP = 1000000LL; n long long add(string a, string b, string c) { n   if (a.size() == 0 || b.size() == 0 || c.size() == 0) return -1LL * INFINITE; n   if ((a.size() > 1 && a[0] ==  0 ) || (b.size() > 1 && b[0] ==  0 ) || n       (c.size() > 1 && c[0] ==  0 )) n     return -1LL * INFINITE; n   if (a.size() > 7 || b.size() > 7 || c.size() > 7) return -1LL * INFINITE; n   long long aa = 0; n   for (int i = (int)(0); i < (int)(a.size()); i++) aa = aa * 10 + (a[i] -  0 ); n   if (aa > MAXP) return -1LL * INFINITE; n   long long bb = 0; n   for (int i = (int)(0); i < (int)(b.size()); i++) bb = bb * 10 + (b[i] -  0 ); n   if (bb > MAXP) return -1LL * INFINITE; n   long long cc = 0; n   for (int i = (int)(0); i < (int)(c.size()); i++) n     cc = cc * 10LL + (c[i] -  0 ); n   if (cc > MAXP) return -1LL * INFINITE; n   return (aa + bb + cc); n } n int main() { n   ios::sync_with_stdio(false); n   string s; n   cin >> s; n   int l = (int)s.size(); n   long long maxm = -1LL * INFINITE; n   for (int i = (int)(0); i < (int)(l - 2); i++) n     for (int j = (int)(i + 1); j < (int)(l - 1); j++) { n       string a = s.substr(0, i + 1); n       string b = s.substr(i + 1, j - i); n       string c = s.substr(j + 1); n       checkmax(maxm, add(a, b, c)); n     } n   if (maxm == -1LL * INFINITE) n     cout <<  -1 n ; n   else n     cout << maxm << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct Kid { n   int v, d; n   long long p; n   int id; n }; n void Transform(vector<Kid>& q) { n   int idx = -1; n   for (int i = 0; i < q.size(); ++i) { n     if (q[i].p < 0) { n       idx = i; n       break; n     } n   } n   if (idx == -1) return; n   for (int i = idx + 1; i < q.size(); ++i) { n     q[i].p -= q[idx].d; n   } n   q.erase(q.begin() + idx); n   Transform(q); n } n int main() { n   int n; n   cin >> n; n   vector<Kid> q(n); n   int kid = 0; n   for (Kid& k : q) { n     cin >> k.v >> k.d >> k.p; n     k.id = kid++; n   } n   vector<int> ans; n   while (!q.empty()) { n     Kid k = q[0]; n     q.erase(q.begin()); n     assert(k.p >= 0); n     ans.push_back(k.id); n     for (int i = 0; i < min((int)q.size(), k.v); ++i) { n       q[i].p -= (k.v - i); n     } n     Transform(q); n   } n   cout << ans.size() << endl; n   for (int id : ans) { n     cout << id + 1 <<    ; n   } n   cout << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int M = 100000 + 10; n struct NODE { n   long long int index; n   long long int a; n   bool operator<(const NODE& x) const { return a < x.a; } n } node[M]; n struct SUR { n   long long int index; n   long long int a; n   bool operator<(const SUR& x) const { return index < x.index; } n } sur[M]; n int main() { n   long long int n, k; n   cin >> n >> k; n   long long int bk = k; n   long long int sum = 0; n   for (int i = 0; i < n; i++) { n     cin >> node[i].a; n     node[i].index = i + 1; n     sum += node[i].a; n   } n   sort(node, node + n); n   if (sum < bk) { n     cout <<  -1  << endl; n   } else { n     if (sum > bk) { n       long long int p1 = 0; n       vector<long long int> psum; n       vector<long long int> id; n       long long int pmin = 0; n       while (p1 < n) { n         long long int p2 = p1; n         while (p2 < n && node[p2].a == node[p1].a) { n           p2++; n         } n         p2--; n         long long int value = (node[p1].a - pmin) * (n - 1 - p1 + 1); n         psum.push_back(value); n         id.push_back(p1); n         pmin = node[p1].a; n         p2++; n         p1 = p2; n       } n       long long int tsum = 0; n       int index = -1; n       for (int i = 0; i < psum.size(); i++) { n         if (tsum + psum[i] <= k) { n           tsum += psum[i]; n         } else { n           index = i; n           break; n         } n       } n       long long int res = k - tsum; n       res = res % (n - id[index]); n       int istart = 0; n       for (int i = id[index]; i < n; i++) { n         sur[istart].a = node[i].a; n         sur[istart++].index = node[i].index; n       } n       sort(sur, sur + istart); n       long long int lastmin = (id[index] - 1 >= 0 ? node[id[index] - 1].a : 0); n       long long int div = (k - tsum) / (n - id[index]); n       for (int i = 0; i < istart; i++) { n         if (i < res) { n           sur[i].a = sur[i].a - 1 - lastmin - div; n         } else { n           sur[i].a = sur[i].a - lastmin - div; n         } n       } n       vector<int> ans; n       for (int i = 0; i < istart; i++) { n         if (sur[(i + res) % istart].a > 0) { n           ans.push_back(sur[(i + res) % istart].index); n         } n       } n       for (int i = 0; i < ans.size(); i++) { n         cout << ans[i]; n         if (i < ans.size() - 1) { n           cout <<    ; n         } else { n           cout << endl; n         } n       } n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long MOD = 1e9 + 7; n struct Fib { n   long long a, b; n   Fib operator+(const Fib& o) const { n     return {(a + o.a) % MOD, (b + o.b) % MOD}; n   } n }; n struct FibOp { n   long long a, b, c, d; n   FibOp operator()(const FibOp& o) const { n     FibOp res = {a * o.a + b * o.c, a * o.b + b * o.d, c * o.a + d * o.c, n                  c * o.b + d * o.d}; n     res.a %= MOD; n     ; n     res.b %= MOD; n     ; n     res.c %= MOD; n     ; n     res.d %= MOD; n     ; n     return res; n   } n   Fib operator()(const Fib& f) const { n     Fib res = {a * f.a + b * f.b, c * f.a + d * f.b}; n     res.a %= MOD; n     ; n     res.b %= MOD; n     ; n     return res; n   } n }; n const long long K = 32; n FibOp pwr[K]; n FibOp negpwr[K]; n Fib zero = {0, 1}; n void setup() { n   pwr[0] = {1, 1, 1, 0}; n   negpwr[0] = {0, 1, 1, MOD - 1}; n   for (int i = 1; i < K; ++i) { n     pwr[i] = pwr[i - 1](pwr[i - 1]); n     negpwr[i] = negpwr[i - 1](negpwr[i - 1]); n   } n } n Fib FibMult(Fib f, long long cnt, FibOp p[K]) { n   for (int r = K - 1; r >= 0; --r) { n     if (cnt & (1LL << r)) { n       f = p[r](f); n     } n   } n   return f; n } n Fib FibMult(Fib f, long long cnt) { n   return (cnt >= 0 ? FibMult(f, cnt, pwr) : FibMult(f, -cnt, negpwr)); n } n struct Node { n   long long lo[2] = {0, 0}; n   long long hi[2] = {0, 0}; n   long long add[2] = {0, 0}; n   long long sz = 1; n   Fib f; n   void apply(long long a[2]) { n     for (int i = 0; i < 2; ++i) { n       add[i] += a[i]; n       lo[i] += a[i]; n       hi[i] += a[i]; n     } n     f = FibMult(f, a[0] + a[1]); n   } n }; n struct Segtree { n   vector<Node> t; n   int n, h; n   Segtree(int sz) { n     h = 32 - __builtin_clz(sz); n     n = 1 << h; n     t = vector<Node>(n << 1); n     for (int i = n - 1; i > 0; --i) { n       t[i].sz = t[i * 2].sz + t[i * 2 + 1].sz; n     } n   } n   void push(int x) { n     if (!t[x].add[0] && !t[x].add[1]) { n       return; n     } n     assert(x < n); n     t[x << 1].apply(t[x].add); n     t[x << 1 | 1].apply(t[x].add); n     t[x].add[0] = 0; n     t[x].add[1] = 0; n   } n   void pull(int x) { n     for (int i = 0; i < 2; ++i) { n       t[x].lo[i] = min(t[x * 2].lo[i], t[x * 2 + 1].lo[i]); n       t[x].hi[i] = max(t[x * 2].hi[i], t[x * 2 + 1].hi[i]); n     } n     t[x].f = t[x << 1].f + t[x << 1 | 1].f; n     t[x].apply(t[x].add); n   } n   void set_init(int x, int k, int va) { n     x += n; n     t[x].lo[k] = t[x].hi[k] = va; n     t[x].f = FibMult(zero, t[x].lo[0] + t[x].lo[1]); n     for (x /= 2; x; x /= 2) { n       pull(x); n     } n   } n   long long query(int l, int r, int x, int lv, int rv) { n     if (r <= lv || rv <= l) { n       return 0; n     } n     if (l <= lv && rv <= r) { n       return t[x].f.a; n     } n     push(x); n     int mv = (lv + rv) / 2; n     long long ans = 0; n     ans += query(l, r, x * 2, lv, mv); n     ans += query(l, r, x * 2 + 1, mv, rv); n     return ans % MOD; n   } n   long long query(int l, int r) { return query(l, r, 1, 0, n); } n   void add(int l, int r, int k, long long va, int x, int lv, int rv) { n     if (r <= lv || rv <= l) { n       return; n     } n     if (l <= lv && rv <= r) { n       long long a[2] = {0, 0}; n       a[k] = va; n       t[x].apply(a); n       return; n     } n     push(x); n     int mv = (lv + rv) / 2; n     add(l, r, k, va, x * 2, lv, mv); n     add(l, r, k, va, x * 2 + 1, mv, rv); n     pull(x); n   } n   void add(int l, int r, int k, long long va) { add(l, r, k, va, 1, 0, n); } n   void minupdate(int l, int r, int k, long long va, int x, int lv, int rv) { n     if (r <= lv || rv <= l) { n       return; n     } n     if (t[x].hi[k] <= va) { n       return; n     } n     if (l <= lv && rv <= r && t[x].lo[k] == t[x].hi[k]) { n       long long a[2] = {0, 0}; n       a[k] = va - t[x].lo[k]; n       t[x].apply(a); n       return; n     } n     push(x); n     int mv = (lv + rv) / 2; n     minupdate(l, r, k, va, x * 2, lv, mv); n     minupdate(l, r, k, va, x * 2 + 1, mv, rv); n     pull(x); n   } n   void minupdate(int l, int r, int k, long long va) { n     return minupdate(l, r, k, va, 1, 0, n); n   } n   void maxupdate(int l, int r, int k, long long va, int x, int lv, int rv) { n     if (r <= lv || rv <= l) { n       return; n     } n     if (t[x].lo[k] >= va) { n       return; n     } n     if (l <= lv && rv <= r && t[x].lo[k] == t[x].hi[k]) { n       long long a[2] = {0, 0}; n       a[k] = va - t[x].lo[k]; n       t[x].apply(a); n       return; n     } n     push(x); n     int mv = (lv + rv) / 2; n     maxupdate(l, r, k, va, x * 2, lv, mv); n     maxupdate(l, r, k, va, x * 2 + 1, mv, rv); n     pull(x); n   } n   void maxupdate(int l, int r, int k, long long va) { n     return maxupdate(l, r, k, va, 1, 0, n); n   } n }; n int main() { n   ios_base::sync_with_stdio(0), cin.tie(0); n   setup(); n   int n, q; n   cin >> n >> q; n   Segtree st(n + 10); n   for (int i = 1; i <= n; ++i) { n     int x; n     cin >> x; n     st.set_init(i, 0, x); n   } n   for (int i = 1; i <= n; ++i) { n     int x; n     cin >> x; n     st.set_init(i, 1, x); n   } n   for (int _i = 0; _i < q; ++_i) { n     int type; n     cin >> type; n     if (type == 1) { n       int k, l, r, x; n       cin >> k >> l >> r >> x; n       --k; n       st.minupdate(l, r + 1, k, x); n     } else if (type == 2) { n       int k, l, r, x; n       cin >> k >> l >> r >> x; n       --k; n       st.maxupdate(l, r + 1, k, x); n     } else if (type == 3) { n       int k, l, r, x; n       cin >> k >> l >> r >> x; n       --k; n       st.add(l, r + 1, k, x); n     } else if (type == 4) { n       int l, r; n       cin >> l >> r; n       cout << st.query(l, r + 1) <<   n ; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n double p[100010]; n double E[100010]; n int main() { n   int n; n   cin >> n; n   p[0] = 0; n   E[0] = 0; n   double ans = 0; n   for (int i = 1; i <= n; ++i) { n     cin >> p[i]; n     E[i] = (1.0 - p[i - 1]) * p[i] + 2 * p[i] * p[i - 1] + p[i] * E[i - 1]; n     ans += E[i]; n   } n   cout << setprecision(10) << ans << endl; n } n


#include <bits/stdc++.h> n constexpr int N = 1.5e5, M = 1 << 18; n int n, q; n int parent[N], dep[N]; n char ch[N]; n int down[N], h[N], deg[N]; n int mx[N][26], edge[N][26], sum[N]; n int e[N][2]; n int bad; n void add(int u, int c, int v) { n   u = down[u]; n   edge[u][c] += v; n   for (u = parent[u]; u != -1; u = parent[u]) { n     bad -= sum[u] > h[u]; n     sum[u] -= mx[u][c]; n     mx[u][c] = 0; n     for (int i = 0; i < deg[u]; ++i) n       mx[u][c] = std::max(mx[u][c], mx[e[u][i]][c] + edge[e[u][i]][c]); n     sum[u] += mx[u][c]; n     bad += sum[u] > h[u]; n   } n } n int main() { n   std::ios::sync_with_stdio(false); n   std::cin.tie(nullptr); n   std::cin >> n >> q; n   parent[0] = -1; n   for (int i = 1; i < n; ++i) { n     std::cin >> parent[i] >> ch[i]; n     --parent[i]; n     e[parent[i]][deg[parent[i]]++] = i; n   } n   for (int i = n - 1; i > 0; --i) h[parent[i]] = h[i] + 1; n   for (int i = 1; i < n; ++i) { n     if (h[parent[i]] != h[i] + 1) { n       while (q--) std::cout <<  Fou n ; n       return 0; n     } n     if (parent[i] != 0 && deg[parent[i]] == 1) parent[i] = parent[parent[i]]; n   } n   for (int i = n - 1; i > 0; --i) down[i] = deg[i] == 1 ? down[e[i][0]] : i; n   for (int i = 0; i < n; ++i) deg[i] = 0; n   for (int i = 1; i < n; ++i) n     if (down[i] == i) e[parent[i]][deg[parent[i]]++] = i; n   for (int i = 1; i < n; ++i) n     if (ch[i] !=  ? ) add(i, ch[i] -  a , 1); n   while (q--) { n     int v; n     char c; n     std::cin >> v >> c; n     --v; n     if (ch[v] !=  ? ) add(v, ch[v] -  a , -1); n     if (c !=  ? ) add(v, c -  a , 1); n     ch[v] = c; n     if (bad == 0) { n       int ans = 0; n       for (int i = 0; i < 26; ++i) ans += (i + 1) * (h[0] - sum[0] + mx[0][i]); n       std::cout <<  Shi   << ans <<   n ; n     } else { n       std::cout <<  Fou n ; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, m, k; n   int x[5001], y[5001], z[5001]; n   scanf( %d , &n); n   double mx = -10; n   for (int i = 0; i < n; i++) { n     scanf( %d , x + i); n     mx = max(mx, x[i] * 1.0); n   } n   scanf( %d , &m); n   for (int i = 0; i < m; i++) scanf( %d , y + i); n   scanf( %d , &k); n   for (int i = 0; i < k; i++) scanf( %d , z + i); n   double A, B; n   scanf( %lf %lf , &A, &B); n   double mn = 2e9 * 1.0; n   for (int j = 0; j < m; j++) { n     for (int l = 0; l < k; l++) { n       mn = min((A * z[l] * 1.0) / (B * y[j] * 1.0), mn); n     } n   } n   printf( %.10f n , mx * 1.0 / sqrt(1.0 + mn)); n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e5 + 5; n int k, n, m, x, ans; n int a[N]; n string second, r; n map<string, int> vis1, vis2; n int main() { n   scanf( %d , &n); n   for (int i = 0; i < n; i++) { n     char e[50]; n     scanf( %s , e); n     r = second = e; n     while (second.find( kh ) != -1) { n       x = second.find( kh ); n       second.erase(second.begin() + x); n     } n     r = second; n     while (second.find( oo ) != -1) { n       x = second.find( oo ); n       second[x] =  u ; n       second.erase(second.begin() + x + 1); n     } n     while (r.find( u ) != -1) { n       x = r.find( u ); n       r[x] =  o ; n       r.insert(r.begin() + x,  o ); n     } n     vis1[second] = 1; n     vis2[r] = 1; n   } n   printf( %d n , min(vis1.size(), vis2.size())); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n #pragma GCC optimize  trapv  n void solve() { n   int64_t n, x, y; n   cin >> n >> x >> y; n   vector<pair<int64_t, int64_t> > a; n   for (uint64_t i = 0; i < n; i++) { n     int u, v; n     cin >> u >> v; n     a.push_back(make_pair(u, v)); n   } n   sort(a.begin(), a.end()); n   multiset<int64_t, greater<int64_t> > curr; n   int64_t res = 0; n   for (uint64_t i = 0; i < a.size(); i++) { n     auto it = curr.upper_bound(a[i].first); n     if (it != curr.end()) { n       if (((a[i].second - *it) * y) < (x + y * (a[i].second - a[i].first))) { n         res += (a[i].second - *it) * y; n         res %= 1000000007; n         curr.erase(it); n       } else { n         res += x + y * (a[i].second - a[i].first); n         res %= 1000000007; n       } n       curr.insert(a[i].second); n     } else { n       res += x + y * (a[i].second - a[i].first); n       res %= 1000000007; n       curr.insert(a[i].second); n     } n   } n   cout << res << endl; n   return; n } n int main() { n   std::ios::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   int t = 1; n   while (t--) { n     solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int x1, y1, x2, y2; n   cin >> x1 >> y1 >> x2 >> y2; n   int a = abs(x1 - x2); n   int b = abs(y1 - y2); n   int u = min(a, b); n   cout << u + abs(a - b); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   cin >> n; n   int arr[n][n]; n   for (int i = 0; i < n; i++) { n     string s; n     cin >> s; n     for (int j = 0; j < n; j++) { n       arr[j][i] = s[j]; n     } n   } n   int cnt = 0; n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < n; j++) { n       int c = 0; n       if (i != 0) { n         if (arr[j][i - 1] ==  o ) { n           c++; n         } n       } n       if (i != n - 1) { n         if (arr[j][i + 1] ==  o ) { n           c++; n         } n       } n       if (j != 0) { n         if (arr[j - 1][i] ==  o ) { n           c++; n         } n       } n       if (j != n - 1) { n         if (arr[j + 1][i] ==  o ) { n           c++; n         } n       } n       if (c % 2 == 0) { n         cnt++; n       } n     } n   } n   if (cnt == n * n) { n     cout <<  YES ; n   } else { n     cout <<  NO ; n   } n   return 0; n } n


#include<bits/stdc++.h> n using namespace std; n long long t,n;long long lowbit(long long x){return x&-x;} n int main(){cin>>t;while(t--){cin>>n;if(!(n-lowbit(n)))puts( NO );else puts( YES );}}


#include <bits/stdc++.h> n using namespace std; n const signed long long Infinity = 1000000100; n const long double Epsilon = 1e-9; n template <typename T, typename U> n ostream& operator<<(ostream& os, const pair<T, U>& p) { n   return os <<  (  << p.first <<  ,  << p.second <<  ) ; n } n template <typename T> n ostream& operator<<(ostream& os, const vector<T>& V) { n   os <<  [ ; n   for (int(i) = (0); (i) < (int(V.size())); (i)++) n     os << V[i] << ((i == int(V.size()) - 1) ?    :  , ); n   return os <<  ] ; n } n template <typename T> n ostream& operator<<(ostream& os, const set<T>& S) { n   os <<  ( ; n   for (__typeof(S.begin()) i = S.begin(); i != S.end(); ++i) n     os << *i << (*i == *S.rbegin() ?    :  , ); n   return os <<  ) ; n } n template <typename T, typename U> n ostream& operator<<(ostream& os, const map<T, U>& M) { n   os <<  { ; n   for (__typeof(M.begin()) i = M.begin(); i != M.end(); ++i) n     os << *i << (*i.first == M.rbegin()->first ?    :  , ); n   return os <<  } ; n } n int Rep[555][555]; n int REP[555 * 555]; n int Siz[555 * 555]; n int Find(int a) { n   if (REP[a] != a) REP[a] = Find(REP[a]); n   return REP[a]; n } n void Union(int a, int b) { n   a = Find(a); n   b = Find(b); n   if (a == b) return; n   REP[a] = b; n   Siz[b] += Siz[a]; n } n char T[555][555]; n int dx[] = {1, -1, 0, 0}; n int dy[] = {0, 0, 1, -1}; n void U(int a, int b) { n   if (T[a][b] ==  X ) return; n   for (int(i) = (0); (i) <= (3); (i)++) { n     if (T[a + dx[i]][b + dy[i]] ==  X ) continue; n     Union(Rep[a][b], Rep[a + dx[i]][b + dy[i]]); n     Rep[a + dx[i]][b + dy[i]] = Rep[a][b] = Find(Rep[a][b]); n   } n } n int state[555 * 555]; n int sum; n int ans; n void add(int a) { n   if (state[a] == 0) sum += Siz[a]; n   state[a]++; n } n void rem(int a) { n   state[a]--; n   if (state[a] == 0) sum -= Siz[a]; n } n int n; n void a(int a, int b) { n   if (T[a][b] ==  X  and a != 0 and b != 0 and a != n + 1 and b != n + 1) sum++; n } n void r(int a, int b) { n   if (T[a][b] ==  X  and a != 0 and b != 0 and a != n + 1 and b != n + 1) sum--; n } n int main() { n   int k; n   scanf( %d%d , &n, &k); n   for (int(i) = (1); (i) <= (n); (i)++) n     for (int(j) = (1); (j) <= (n); (j)++) { n       scanf(  %c , &T[i][j]); n       Rep[i][j] = i + (j - 1) * n; n       REP[i + (j - 1) * n] = i + (j - 1) * n; n       Siz[i + (j - 1) * n] = T[i][j] !=  X ; n     } n   for (int(i) = (0); (i) <= (n + 1); (i)++) n     T[i][0] = T[0][i] = T[i][n + 1] = T[n + 1][i] =  X ; n   for (int(i) = (1); (i) <= (n); (i)++) n     for (int(j) = (1); (j) <= (n); (j)++) U(i, j); n   for (int(i) = (0); (i) <= (n); (i)++) { n     if (i + k + 1 > n + 1) break; n     for (int(j) = (0); (j) <= (555 * 555 - 1); (j)++) state[j] = 0; n     sum = 0; n     for (int(j) = (0); (j) <= (n + 1); (j)++) { n       for (int(l) = (1); (l) <= (k); (l)++) add(Find(Rep[i + l][j])); n       if (j > 0) add(Find(Rep[i][j - 1])); n       if (j > 0) add(Find(Rep[i + k + 1][j - 1])); n       if (j > 0) n         for (int(l) = (1); (l) <= (k); (l)++) a(i + l, j - 1); n       if (j - k - 2 >= 0) n         for (int(l) = (1); (l) <= (k); (l)++) rem(Find(Rep[i + l][j - k - 2])); n       if (j - k - 1 >= 0) rem(Find(Rep[i][j - k - 1])); n       if (j - k - 1 >= 0) rem(Find(Rep[i + k + 1][j - k - 1])); n       if (j - k - 1 >= 0) n         for (int(l) = (1); (l) <= (k); (l)++) r(i + l, j - k - 1); n       ans = max(ans, sum); n     } n   } n   printf( %d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n inline char gc() { n   static const int L = 23333; n   static char sxd[L], *sss = sxd, *ttt = sxd; n   if (sss == ttt) { n     ttt = (sss = sxd) + fread(sxd, 1, L, stdin); n     if (sss == ttt) { n       return EOF; n     } n   } n   return *sss++; n } n template <class T> n inline bool read(T& x) { n   x = 0; n   char c = gc(); n   bool flg = false; n   for (; !isdigit(c); c = gc()) { n     if (c ==  - ) { n       flg = true; n     } else if (c == EOF) { n       return false; n     } n   } n   for (; isdigit(c); c = gc()) { n     x = (x * 10) + (c ^ 48); n   } n   if (flg) { n     x = -x; n   } n   return true; n } n template <class T> n inline void write(T x) { n   if (x < 0) { n     x = -x; n     putchar( - ); n   } n   if (x > 9) { n     write(x / 10); n     x %= 10; n   } n   putchar(x | 48); n } n template <class T> n inline void writeln(T x) { n   write(x); n   puts(  ); n } n template <class T> n inline void writesp(T x) { n   write(x); n   putchar(   ); n } n using namespace std; n const int maxn = 100005; n int gcd(int a, int b) { n   while (b) { n     int t = a % b; n     a = b; n     b = t; n   } n   return a; n } n int n; n struct Edge { n   int to, nxt; n } e[maxn << 1]; n int first[maxn]; n int du[maxn]; n int dubk[maxn]; n int cnt; n void add_edge(int u, int v) { n   du[u]++, du[v]++; n   e[++cnt].nxt = first[u]; n   first[u] = cnt; n   e[cnt].to = v; n   e[++cnt].nxt = first[v]; n   first[v] = cnt; n   e[cnt].to = u; n } n int final_ans[maxn]; n void init() { n   cnt = 0; n   for (int i = 1; i <= n; ++i) { n     first[i] = du[i] = final_ans[i] = 0; n   } n } n bool np[maxn]; n int factor[maxn]; n int prime[maxn]; n void oula(const int n = 100000) { n   np[1] = true; n   int cnt = 0; n   for (int i = 2; i <= n; ++i) { n     if (!np[i]) { n       prime[++cnt] = i; n       factor[i] = i; n     } n     for (int j = 1; j <= cnt && (long long)i * prime[j] <= n; ++j) { n       np[i * prime[j]] = true; n       factor[i * prime[j]] = prime[j]; n       if (!(i % prime[j])) { n         break; n       } n     } n   } n } n bool vis[maxn]; n int nn[maxn]; n int bfs(int xx) { n   for (int i = 1; i <= n; ++i) { n     vis[i] = false; n     nn[i] = du[i] = dubk[i]; n   } n   queue<int> q; n   while (!q.empty()) { n     q.pop(); n   } n   for (int i = 1; i <= n; ++i) { n     if (du[i] <= 1) { n       q.push(i); n       vis[i] = true; n     } n   } n   int ans = n - 1; n   while (!q.empty()) { n     int now = q.front(); n     q.pop(); n     bool flg = false; n     if (du[now]) { n       int tmpans = gcd(ans, nn[now]); n       if (tmpans % xx) { n         nn[now]--; n         ans = gcd(ans, nn[now]); n       } else { n         ans = tmpans; n         flg = true; n       } n     } else { n       ans = gcd(ans, nn[now]); n     } n     if (ans % xx) { n       return 1; n     } n     for (int i = first[now]; i; i = e[i].nxt) { n       int to = e[i].to; n       if (!vis[to]) { n         du[to]--; n         if (flg) { n           nn[to]--; n         } n         if (du[to] <= 1) { n           q.push(to); n           vis[to] = true; n         } n       } n     } n   } n   return ans; n } n const int mod = 998244353; n int ksm(int a, int b) { n   int ans = 1; n   for (; b; b >>= 1, a = (long long)a * a % mod) { n     if (b & 1) { n       ans = (long long)ans * a % mod; n     } n   } n   return ans; n } n void solve() { n   read(n); n   init(); n   for (int i = 1; i < n; ++i) { n     int u, v; n     read(u), read(v); n     add_edge(u, v); n   } n   for (int i = 1; i <= n; ++i) { n     dubk[i] = du[i]; n   } n   int tt = n - 1; n   int tmpans = 0; n   while (tt > 1) { n     int pme = factor[tt]; n     while (!(tt % pme)) { n       tt /= pme; n     } n     int nowans = bfs(pme); n     if (nowans > 1 && !final_ans[nowans]) { n       final_ans[nowans] = 1; n       tmpans++; n     } n   } n   final_ans[1] = ksm(2, n - 1) - tmpans; n   if (final_ans[1] < 0) { n     final_ans[1] += mod; n   } n   for (int i = 1; i <= n; ++i) { n     writesp(final_ans[i]); n   } n   puts(  ); n } n int main() { n   oula(); n   int T; n   read(T); n   while (T--) { n     solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n const long long M = 1e9 + 7; n const double pi = 3.14159265358979323846; n using namespace std; n long long ans = 1; n vector<long long> adj[2005]; n long long vis[200005] = {0}; n void dfs(long long x) { n   vis[x] = 1; n   for (long long i = 0; i < adj[x].size(); i++) { n     if (vis[adj[x][i]] == 0) dfs(adj[x][i]); n   } n } n bool prime(long long n) { n   if (n <= 2) n     return false; n   else { n     for (long long i = 2; i <= sqrt(n); i++) { n       if (n % i == 0) { n         return false; n       } n     } n     return true; n   } n } n signed main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   long long t = 1; n   while (t--) { n     long long n; n     cin >> n; n     for (long long i = 0; i < n; i++) { n       if (i % 2 == 0) { n         for (long long j = 0; j < n; j++) { n           if (j % 2 == 0) n             cout <<  W ; n           else n             cout <<  B ; n         } n         cout <<   n ; n       } else { n         for (long long j = 0; j < n; j++) { n           if (j % 2 == 0) n             cout <<  B ; n           else n             cout <<  W ; n         } n         cout <<   n ; n       } n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, cnt0 = 3; n int g, r, b; n bool markb = true, markg = true, markr = true; n char id[ Z ][ Z ]; n vector<char> st, m; n int tt[500]; n int main() { n   scanf( %d n , &n); n   for (int i = 1; i <= n; ++i) { n     char ch; n     scanf( %c , &ch); n     if (ch ==  B ) { n       ++b; n       cnt0 -= markb; n       markb = false; n     } n     if (ch ==  G ) { n       ++g; n       cnt0 -= markg; n       markg = false; n     } n     if (ch ==  R ) { n       ++r; n       cnt0 -= markr; n       markr = false; n     } n   } n   if (b >= 1 && g >= 1 && r >= 1) { n     printf( BGR ); n     return 0; n   } n   if (cnt0 == 2) { n     if (b) { n       printf( B ); n       return 0; n     } n     if (g) { n       printf( G ); n       return 0; n     } n     if (r) { n       printf( R ); n       return 0; n     } n   } n   if (g == 0) { n     if (r >= 2 && b >= 2) { n       printf( BGR ); n       return 0; n     } n   } n   if (r == 0) { n     if (g >= 2 && b >= 2) { n       printf( BGR ); n       return 0; n     } n   } n   if (b == 0) { n     if (g >= 2 && r >= 2) { n       printf( BGR ); n       return 0; n     } n   } n   markb = markg = markr = false; n   id[ G ][ G ] =  G ; n   id[ R ][ R ] =  R ; n   id[ B ][ B ] =  B ; n   id[ G ][ R ] = id[ R ][ G ] =  B ; n   id[ B ][ R ] = id[ R ][ B ] =  G ; n   id[ G ][ B ] = id[ B ][ G ] =  R ; n   if (b >= 1) m.push_back( B ); n   if (b >= 2) m.push_back( B ); n   if (g >= 1) m.push_back( G ); n   if (g >= 2) m.push_back( G ); n   if (r >= 1) m.push_back( R ); n   if (r >= 2) m.push_back( R ); n   for (int i = 1; i <= m.size(); ++i) tt[i] = i; n   do { n     st.clear(); n     for (int i = 1; i <= m.size(); ++i) st.push_back(m[tt[i] - 1]); n     for (int i = 0; i < st.size() - 1;) { n       st.push_back(id[st[i]][st[i + 1]]); n       i += 2; n     } n     if (st.back() ==  B ) markb = true; n     if (st.back() ==  G ) markg = true; n     if (st.back() ==  R ) markr = true; n   } while (next_permutation(tt + 1, tt + m.size() + 1)); n   if (markb) printf( B ); n   if (markg) printf( G ); n   if (markr) printf( R ); n   return 0; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, m; n   long int x, y; n   cin >> n >> m >> x >> y; n   long int* a = new long int[n]; n   long int* b = new long int[m]; n   for (int i = 0; i < n; i++) cin >> a[i]; n   for (int i = 0; i < m; i++) cin >> b[i]; n   int count = -1, indi = 0, indj = 0; n   long int** res = new long int*[min(m, n)]; n   while ((indi < n) && (indj < m)) n     if (b[indj] < a[indi] - x) n       ++indj; n     else if (b[indj] > a[indi] + y) n       ++indi; n     else { n       res[(++count)] = new long int[2]; n       res[count][0] = indi + 1; n       res[count][1] = indj + 1; n       ++indi; n       ++indj; n     } n   cout << count + 1 << endl; n   for (int i = 0; i < count + 1; i++) n     cout << res[i][0] <<     << res[i][1] << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int r, b; n   cin >> r >> b; n   cout << max(r, b) - 1 <<     << min(r, b) <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n double dp[1100][1100]; n int n, m, x, y; n int main() { n   int i, j, T; n   scanf( %d%d%d%d , &n, &m, &x, &y); n   if (m == 1) { n     printf( %d n , (n - x) * 2); n     return 0; n   } n   for (i = n - 1; i >= x; --i) n     for (T = 1; T <= 60; ++T) n       for (j = 1; j <= m; ++j) { n         if (j == 1) n           dp[i][j] = 1 + (dp[i + 1][j] + dp[i][j] + dp[i][j + 1]) / 3; n         else if (j == m) n           dp[i][j] = 1 + (dp[i + 1][j] + dp[i][j] + dp[i][j - 1]) / 3; n         else n           dp[i][j] = n               1 + (dp[i][j] + dp[i + 1][j] + dp[i][j - 1] + dp[i][j + 1]) / 4; n       } n   printf( %.9lf n , dp[x][y]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, in = 2; n long long suma, bit[1000000]; n double x, y, w, ab[100003]; n pair<double, double> ar[100004]; n map<double, int> busca; n void updt(int p) { n   for (int i = p; i <= n; i += (i & -i)) bit[i]++; n } n int query(int p) { n   int s = 0; n   for (int i = p; i > 0; i -= (i & -i)) s += bit[i]; n   return s; n } n bool op(pair<double, double> f, pair<double, double> g) { n   if (f.first != g.first) return f.first < g.first; n   return f.second > g.second; n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cin >> n >> w; n   for (int i = 0; i < n; i++) { n     cin >> x >> y; n     ar[i] = make_pair(-(x / (y - w)), -(x / (y + w))); n     ab[i] = ar[i].second; n   } n   sort(ar, ar + n, op); n   sort(ab, ab + n); n   busca[ab[0]] = 1; n   for (int i = 1; i < n; i++) n     if (ab[i] != ab[i - 1]) busca[ab[i]] = in++; n   for (int i = 0; i < n; i++) { n     in = busca[ar[i].second]; n     suma += query(n - in + 1); n     updt(n - in + 1); n   } n   cout << suma; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 305; n vector<pair<int, int> > e; n vector<int> b; n int a[MAXN]; n bool mark[MAXN], x[MAXN][MAXN]; n int n, k, m, cnt; n void add(int u, int v) { n   if (x[u][v]) return; n   cnt++; n   e.push_back(pair<int, int>(u, v)); n   x[u][v] = x[v][u] = true; n } n void ghi() { n   for (int i = 0; i < m; i++) printf( %d %d n , e[i].first, e[i].second); n } n int main() { n   if (0) { n     freopen( input.txt ,  r , stdin); n     freopen( output.txt ,  w , stdout); n   }; n   scanf( %d%d%d , &n, &m, &k); n   for (int i = 1; i <= k; i++) scanf( %d , &a[i]), mark[a[i]] = true; n   int nn = n - k; n   if (k == n) { n     puts( -1 ); n     return 0; n   } n   for (int i = 1; i <= n; i++) n     if (!mark[i]) b.push_back(i); n   add(a[1], b[0]); n   if (cnt == m) { n     ghi(); n     return 0; n   } n   for (int i = 0; i < nn - 1; i++) { n     add(b[i], b[i + 1]); n     if (cnt == m) { n       ghi(); n       return 0; n     } n   } n   add(b[nn - 1], a[2]); n   for (int i = 2; i < k; i++) { n     add(a[i], a[i + 1]); n     if (cnt == m) { n       ghi(); n       return 0; n     } n   } n   for (int i = 0; i < nn - 1; i++) n     for (int j = i + 1; j < nn; j++) { n       add(b[i], b[j]); n       if (cnt == m) { n         ghi(); n         return 0; n       } n     } n   for (int i = 2; i < k; i++) n     for (int j = i + 1; j <= k; j++) { n       add(a[i], a[j]); n       if (cnt == m) { n         ghi(); n         return 0; n       } n     } n   for (int i = 0; i < nn; i++) n     for (int j = 2; j <= k; j++) { n       add(b[i], a[j]); n       if (cnt == m) { n         ghi(); n         return 0; n       } n     } n   for (int i = 1; i < nn; i++) { n     add(a[1], b[i]); n     if (cnt == m) { n       ghi(); n       return 0; n     } n   } n   puts( -1 ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int COUNT = 600; n int main() { n   ios::sync_with_stdio(false); n   cin.tie(nullptr); n   int N; n   cin >> N; n   string S; n   cin >> S; n   vector<int> on_count(COUNT, 0); n   for (int i = 0; i < N; i++) { n     int a, b; n     cin >> a >> b; n     int goal = b; n     int status = S[i] -  0 ; n     for (int t = 0; t < COUNT; t++) { n       if (t == goal) { n         status = 1 - status; n         goal += a; n       } n       on_count[t] += status; n     } n   } n   cout << *max_element(on_count.begin(), on_count.end()) <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n double weight; n struct edge { n   int u, v, used, id, w, flag; n   bool operator<(const edge &b) const { n     return w + weight * flag < b.w + weight * b.flag; n   } n } e[200001]; n const double eps = 1e-10; n int n, m, k, p[10001]; n int find(int x) { return p[x] == x ? x : p[x] = find(p[x]); } n int count(double x, int final = 0) { n   weight = x; n   for (int i = 0; i < m; i++) e[i].used = 0; n   for (int i = 1; i <= n; i++) p[i] = i; n   sort(e, e + m); n   int ans = 0; n   for (int i = 0; i < m; i++) { n     int x = find(e[i].u), y = find(e[i].v); n     if (x != y && !(final && ans == k && e[i].flag)) { n       p[x] = y; n       e[i].used = 1; n       if (e[i].flag) ans++; n     } n   } n   return ans; n } n int main() { n   scanf( %d%d%d , &n, &m, &k); n   for (int i = 0; i < m; i++) n     scanf( %d%d%d , &e[i].u, &e[i].v, &e[i].w), n         e[i].id = i + 1, e[i].flag = (e[i].u == 1 || e[i].v == 1); n   double l = -1e5, r = 1e5 + .3; n   while (l + eps < r) { n     double m = (l + r) / 2; n     int res = count(m); n     if (res < k) n       r = m; n     else n       l = m; n     if (res == k) break; n   } n   count(l, 1); n   int cnt = 0, cntk = 0; n   for (int i = 0; i < m; i++) cnt += e[i].used, cntk += e[i].used * e[i].flag; n   if (cnt != n - 1 || cntk != k) { n     puts( -1 ); n     return 0; n   } n   printf( %d n , n - 1); n   for (int i = 0; i < m; i++) n     if (e[i].used) printf( %d  , e[i].id); n   puts(  ); n } n


#include <bits/stdc++.h> n using namespace std; n template <class T> n inline bool fs(T &x) { n   int c = getchar(); n   int sgn = 1; n   while (~c && c <  0  || c >  9 ) { n     if (c ==  - ) sgn = -1; n     c = getchar(); n   } n   for (x = 0; ~c &&  0  <= c && c <=  9 ; c = getchar()) x = x * 10 + c -  0 ; n   x *= sgn; n   return ~c; n } n int main() { n   long long int n; n   fs(n); n   if (n % 4 == 0) n     cout <<  4  << endl; n   else n     cout <<  0  << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int win(char a, char b) { n   if (a ==  8 ) { n     if (b ==  8 ) return 0; n     return (b ==  [  ? 1 : -1); n   } n   if (a ==  [ ) { n     if (b ==  [ ) return 0; n     return (b ==  (  ? 1 : -1); n   } n   if (a ==  ( ) { n     if (b ==  ( ) return 0; n     return (b ==  8  ? 1 : -1); n   } n } n int main() { n   string s1, s2; n   getline(cin, s1); n   getline(cin, s2); n   int t = s1.length(); n   --t; n   int wn = 0; n   for (int i = 0; i < t; i += 2) { n     wn += win(s1[i], s2[i]); n   } n   if (wn > 0) n     cout <<  TEAM 1 WINS n ; n   else if (wn < 0) n     cout <<  TEAM 2 WINS n ; n   else n     cout <<  TIE n ; n } n


#include <bits/stdc++.h> n using namespace std; n int n, k, G, T; n string s; n string solve() { n   for (int i = 0; i < s.size(); i++) n     if (s[i] ==  G ) G = i; n   for (int i = 0; i < s.size(); i++) n     if (s[i] ==  T ) T = i; n   if (G > T) swap(G, T); n   int i = 0; n   for (i = G; i < T; i += k) { n     if (s[i] ==  # ) return  NO ; n   } n   return i == T ?  YES  :  NO ; n } n int main() { n   cin >> n >> k; n   cin >> s; n   cout << solve() << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios::sync_with_stdio(false); n   cin.tie(nullptr); n   int n, a, b; n   cin >> n >> a >> b; n   vector<int> c(n); n   for (int i = 0; i < n; ++i) { n     cin >> c[i]; n   } n   int i = 0, j = n - 1; n   int ans = 0; n   while (i < j) { n     if (c[i] == c[j] && c[i] != 2) { n       ++i; n       --j; n     } else if (c[i] != c[j] && c[i] != 2 && c[j] != 2) { n       cout << -1; n       return 0; n     } else if (c[i] == 2 && c[j] != 2) { n       if (c[j] == 0) n         ans += a; n       else if (c[j] == 1) n         ans += b; n       ++i; n       --j; n     } else if (c[j] == 2 && c[i] != 2) { n       if (c[i] == 0) n         ans += a; n       else if (c[i] == 1) n         ans += b; n       ++i; n       --j; n     } else if (c[i] == c[j] && c[i] == 2) { n       ans += 2 * min(a, b); n       ++i; n       --j; n     } n   } n   if (n % 2 && c[i] == 2) { n     ans += min(a, b); n   } n   cout << ans; n } n


#include <bits/stdc++.h> n using namespace std; n string smallest(string str) { n   int n = str.length(); n   if (n & 1) return str; n   int mid = n / 2; n   string s = smallest(str.substr(0, n / 2)); n   string t = smallest(str.substr(n / 2, n / 2)); n   if (s > t) swap(s, t); n   str = s + t; n   return str; n } n int main() { n   string s1, s2; n   cin >> s1 >> s2; n   puts(smallest(s1) == smallest(s2) ?  YES  :  NO ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 300100; n priority_queue<pair<long long, int>, vector<pair<long long, int> >, n                greater<pair<long long, int> > > n     vec[N]; n vector<int> g[N]; n long long tag[N]; n int fa[N], fat[N], sz[N]; n int find(int x) { return fa[x] == x ? x : fa[x] = find(fa[x]); } n void dfs(int x) { n   for (int i : g[x]) n     if (i != fat[x]) { n       fat[i] = x; n       sz[x]++; n       dfs(i); n     } n } n int n, m; n void U(int x, int y) { n   fa[y] = x; n   if (vec[x].size() < vec[y].size()) { n     swap(vec[x], vec[y]); n     swap(tag[x], tag[y]); n   } n   --sz[x]; n   sz[x] += sz[y]; n   while (!vec[y].empty()) { n     auto i = vec[y].top(); n     vec[y].pop(); n     i.first += tag[y] - tag[x]; n     vec[x].push(i); n   } n } n int q[N], cnt; n long long ans; n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   cin >> n >> m; n   for (int i = 1; i <= n; i++) fa[i] = i; n   for (int i = 1; i < n; ++i) { n     int x, y; n     cin >> x >> y; n     g[x].push_back(y); n     g[y].push_back(x); n   } n   dfs(1); n   for (int i = 1; i <= m; i++) { n     int x, y, z; n     cin >> x >> y >> z; n     if (x == y) continue; n     vec[x].emplace(z, y); n   } n   for (int i = 2; i <= n; i++) n     if (!sz[i]) q[++cnt] = i; n   for (; cnt;) { n     int u = find(q[cnt--]); n     if (vec[u].empty()) { n       cout << -1 << endl; n       return 0; n     } n     int v = vec[u].top().second, w = vec[u].top().first + tag[u]; n     ans += w; n     assert(w >= 0); n     tag[u] = -vec[u].top().first; n     vec[u].pop(); n     v = find(v); n     while (u != v) { n       int t = find(fat[u]); n       U(t, u); n       u = find(u); n     } n     if (u != 1 && !sz[u]) q[++cnt] = u; n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int dx[4] = {-1, 1, 0, 0}; n int dy[4] = {0, 0, -1, 1}; n int sx, sy; n char m[1005][1005]; n int vis[1005][1005][4]; n int flag; n int n, k; n void bfs(int x, int y, int l, int c) { n   if (x < 0 || x >= n || y < 0 || y >= k) return; n   if (m[x][y] ==  * ) return; n   if (vis[x][y][l] <= c) return; n   if (c > 2) return; n   vis[x][y][l] = c; n   if (m[x][y] ==  T ) { n     flag = 1; n     return; n   } n   for (int i = 0; i < 4; i++) { n     int xx = x + dx[i]; n     int yy = y + dy[i]; n     if (l == i) n       bfs(xx, yy, l, c); n     else n       bfs(xx, yy, i, c + 1); n   } n   return; n } n int main() { n   scanf( %d%d , &n, &k); n   for (int i = 0; i < n; i++) { n     scanf( %s , m[i]); n     for (int j = 0; j < k; j++) { n       if (m[i][j] ==  S ) { n         sx = i; n         sy = j; n       } n     } n   } n   flag = 0; n   memset(vis, 0x3f3f3f, sizeof(vis)); n   for (int i = 0; i < 4; i++) { n     int xx, yy; n     xx = sx + dx[i]; n     yy = sy + dy[i]; n     bfs(xx, yy, i, 0); n   } n   if (flag) n     printf( YES n ); n   else n     printf( NO n ); n } n


#include <bits/stdc++.h> n using namespace std; n const int L = 1000 + 10; n int b[L]; n int w, m, l = 0; n int main() { n   cin >> w >> m; n   if (w > 3) n     while (m) { n       int q = m % w; n       m /= w; n       if (q > 1 && q < w - 1) { n         cout <<  NO  << endl; n         return 0; n       } n       if (q == w - 1) m++; n     } n   cout <<  YES  << endl; n   return 0; n } n