#include <bits/stdc++.h> n using namespace std; n const int INF = 0x3f3f3f3f; n const long long LINF = 0x3f3f3f3f3f3f3f3fll; n const long double DINF = 1e18; n const long double pi = acos(-1.0); n const long double eps = 1e-9; n bool eq(long double a, long double b) { return abs(a - b) <= eps; } n struct pt { n   long double x, y; n   pt() {} n   pt(long double x_, long double y_) : x(x_), y(y_) {} n   bool operator<(const pt p) const { n     if (!eq(x, p.x)) return x < p.x; n     if (!eq(y, p.y)) return y < p.y; n     return 0; n   } n   bool operator==(const pt p) const { return eq(x, p.x) and eq(y, p.y); } n   pt operator+(const pt p) const { return pt(x + p.x, y + p.y); } n   pt operator-(const pt p) const { return pt(x - p.x, y - p.y); } n   pt operator*(const long double c) const { return pt(x * c, y * c); } n   pt operator/(const long double c) const { return pt(x / c, y / c); } n   long double operator*(const pt p) const { return x * p.x + y * p.y; } n   long double operator^(const pt p) const { return x * p.y - y * p.x; } n   friend istream& operator>>(istream& in, pt& p) { return in >> p.x >> p.y; } n }; n struct line { n   pt p, q; n   line() {} n   line(pt p_, pt q_) : p(p_), q(q_) {} n   friend istream& operator>>(istream& in, line& r) { return in >> r.p >> r.q; } n }; n long double dist(pt p, pt q) { return hypot(p.y - q.y, p.x - q.x); } n long double dist2(pt p, pt q) { n   return ((p.x - q.x) * (p.x - q.x)) + ((p.y - q.y) * (p.y - q.y)); n } n long double norm(pt v) { return dist(pt(0, 0), v); } n long double angle(pt v) { n   long double ang = atan2(v.y, v.x); n   if (ang < 0) ang += 2 * pi; n   return ang; n } n long double sarea(pt p, pt q, pt r) { return ((q - p) ^ (r - q)) / 2; } n bool col(pt p, pt q, pt r) { return eq(sarea(p, q, r), 0); } n int paral(pt u, pt v) { n   if (!eq(u ^ v, 0)) return 0; n   if ((u.x > eps) == (v.x > eps) and (u.y > eps) == (v.y > eps)) return 1; n   return -1; n } n bool ccw(pt p, pt q, pt r) { return sarea(p, q, r) > eps; } n pt rotate(pt p, long double th) { n   return pt(p.x * cos(th) - p.y * sin(th), p.x * sin(th) + p.y * cos(th)); n } n pt rotate90(pt p) { return pt(-p.y, p.x); } n bool isvert(line r) { return eq(r.p.x, r.q.x); } n long double getm(line r) { n   if (isvert(r)) return DINF; n   return (r.p.y - r.q.y) / (r.p.x - r.q.x); n } n long double getn(line r) { n   if (isvert(r)) return DINF; n   return r.p.y - getm(r) * r.p.x; n } n bool paraline(line r, line second) { n   return paral(r.p - r.q, second.p - second.q); n } n bool isinseg(pt p, line r) { n   if (p == r.p or p == r.q) return 1; n   return paral(p - r.p, p - r.q) == -1; n } n long double get_t(pt v, line r) { return (r.p ^ r.q) / ((r.p - r.q) ^ v); } n pt proj(pt p, line r) { n   if (r.p == r.q) return r.p; n   r.q = r.q - r.p; n   p = p - r.p; n   pt proj = r.q * ((p * r.q) / (r.q * r.q)); n   return proj + r.p; n } n pt inter(line r, line second) { n   if (paraline(r, second)) return pt(DINF, DINF); n   if (isvert(r)) return pt(r.p.x, getm(second) * r.p.x + getn(second)); n   if (isvert(second)) return pt(second.p.x, getm(r) * second.p.x + getn(r)); n   long double x = (getn(second) - getn(r)) / (getm(r) - getm(second)); n   return pt(x, getm(r) * x + getn(r)); n } n bool interseg(line r, line second) { n   if (isinseg(r.p, second) or isinseg(r.q, second) or isinseg(second.p, r) or n       isinseg(second.q, r)) n     return 1; n   return ccw(r.p, r.q, second.p) != ccw(r.p, r.q, second.q) and n          ccw(second.p, second.q, r.p) != ccw(second.p, second.q, r.q); n } n long double disttoline(pt p, line r) { return dist(p, proj(p, r)); } n long double disttoseg(pt p, line r) { n   if (isinseg(proj(p, r), r)) return disttoline(p, r); n   return min(dist(p, r.p), dist(p, r.q)); n } n long double distseg(line a, line b) { n   if (interseg(a, b)) return 0; n   long double ret = DINF; n   ret = min(ret, disttoseg(a.p, b)); n   ret = min(ret, disttoseg(a.q, b)); n   ret = min(ret, disttoseg(b.p, a)); n   ret = min(ret, disttoseg(b.q, a)); n   return ret; n } n bool operator<(const line& a, const line& b) { n   if (a.p == b.p) return ccw(a.p, a.q, b.q); n   if (!eq(a.p.x, a.q.x) and (eq(b.p.x, b.q.x) or a.p.x + eps < b.p.x)) n     return ccw(a.p, a.q, b.p); n   return ccw(a.p, b.q, b.p); n } n bool has_intersection(vector<line> v) { n   auto intersects = [&](pair<line, int> a, pair<line, int> b) { n     return interseg(a.first, b.first); n   }; n   vector<pair<pt, pair<int, int>>> w; n   for (int i = 0; i < v.size(); i++) { n     if (v[i].q < v[i].p) swap(v[i].p, v[i].q); n     w.push_back({v[i].p, {0, i}}); n     w.push_back({v[i].q, {1, i}}); n   } n   sort(w.begin(), w.end()); n   set<pair<line, int>> se; n   for (auto i : w) { n     line at = v[i.second.second]; n     if (i.second.first == 0) { n       auto nxt = se.lower_bound({at, i.second.second}); n       if (nxt != se.end() and intersects(*nxt, {at, i.second.second})) return 1; n       if (nxt != se.begin() and intersects(*(--nxt), {at, i.second.second})) n         return 1; n       se.insert({at, i.second.second}); n     } else { n       auto nxt = se.upper_bound({at, i.second.second}), cur = nxt, prev = --cur; n       if (nxt != se.end() and prev != se.begin() and n           intersects(*nxt, *(--prev))) n         return 1; n       se.erase(cur); n     } n   } n   return 0; n } n int n; n vector<pair<pt, pt>> pts; n vector<int> S; n long double com = 0.218750000; n bool has = false; n bool show(long double m) { n   bool ret = false; n   vector<line> v; n   for (int i = 0; i < n; i++) { n     pt p = pts[i].first; n     pt q = pts[i].second; n     long double mul = m * S[i] / norm(q); n     q = q * mul; n     q = q + p; n     v.push_back(line(p, q)); n   } n   ret = has_intersection(v); n   has |= ret; n   return ret; n } n long double bb(long double l, long double r) { n   if (eq(l, r)) return l; n   long double m = (l + r) / 2.0; n   if (show(m)) n     r = m; n   else n     l = m; n   return bb(l, r); n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cin >> n; n   for (int i = 0; i < n; i++) { n     pt a, b; n     cin >> a >> b; n     int second; n     cin >> second; n     S.push_back(second); n     pts.push_back({a, b}); n   } n   cout << setprecision(9) << fixed; n   long double ans = bb((long double)0, (long double)1e13); n   if (!has) n     cout <<  No show :(  <<   n ; n   else n     cout << ans <<   n ; n   exit(0); n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 2 * 1e5 + 10; n char str[maxn]; n int ans[maxn]; n int main() { n   int n, a, b, k; n   while (scanf( %d%d%d%d , &n, &a, &b, &k) != EOF) { n     scanf( %s , str + 1); n     int m = 0, cnt = 0; n     for (int i = 1; i <= n; ++i) { n       if (str[i] ==  0 ) { n         cnt++; n         if (cnt == b) { n           ans[m++] = i; n           cnt = 0; n         } n       } n       if (str[i] ==  1 ) cnt = 0; n     } n     printf( %d n , m + 1 - a); n     printf( %d , ans[0]); n     for (int i = 1; i <= m - a; ++i) printf(  %d , ans[i]); n     printf(  n ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long int power(long long int x, long long int y, long long int p) { n   long long int res = 1; n   x = x % p; n   if (x == 0) return 0; n   while (y > 0) { n     if (y & 1) res = (res * x) % p; n     y = y >> 1; n     x = (x * x) % p; n   } n   return res; n } n bool sortinrev(const pair<int, int>& a, const pair<int, int>& b) { n   if (a.first == b.first) return b.second > a.second; n   return a.first > b.first; n } n void run(vector<int> v) { n   for (int i = 0; i < v.size(); i++) cout << v[i] <<    ; n   cout << endl; n } n int modInverse(long long int a, long long int m) { n   long long int m0 = m; n   long long int y = 0, x = 1; n   if (m == 1) return 0; n   while (a > 1) { n     long long int q = a / m; n     long long int t = m; n     m = a % m, a = t; n     t = y; n     y = x - q * y; n     x = t; n   } n   if (x < 0) x += m0; n   return x; n } n void numberOfNodes(int n, vector<long long int> adj[], n                    vector<long long int>& count1, int s, int e) { n   vector<long long int>::iterator u; n   count1[s] = 1; n   for (u = adj[s].begin(); u != adj[s].end(); u++) { n     if (*u == e) continue; n     numberOfNodes(n, adj, count1, *u, s); n     count1[s] += count1[*u]; n   } n } n void solve() { n   int n; n   cin >> n; n   long long int a[n], b[n]; n   int vis[n]; n   long long int ans = 0; n   vector<vector<int>> v, total; n   memset(vis, 0, sizeof(vis)); n   for (int i = 0; i < n; i++) cin >> a[i]; n   for (int i = 0; i < n; i++) cin >> b[i]; n   for (int i = 0; i < n; i++) { n     int count = 0; n     if (!vis[i]) { n       for (int j = 0; j < n; j++) { n         if (j != i) { n           if (a[i] == a[j]) { n             if (count == 0) ans += b[i]; n             vis[j] = 1; n             vis[i] = 1; n             ans += b[j]; n             count++; n           } n         } n       } n     } n   } n   for (int i = 0; i < n; i++) { n     vector<int> vi(61); n     long long int m = a[i]; n     int count = 0; n     vector<int> ind; n     while (m > 0) { n       if (m % 2 == 1) { n         ind.push_back(count); n       } n       m = m / 2; n       count++; n     } n     for (int j = 0; j < ind.size(); j++) vi[ind[j]] = 1; n     total.push_back(vi); n     if (vis[i]) v.push_back(vi); n   } n   int count1, k; n   for (int i = 0; i < n; i++) { n     if (!vis[i]) { n       for (int j = 0; j < v.size(); j++) { n         for (k = 0; k < 60; k++) { n           if (total[i][k] == 1) { n             if (v[j][k] == 0) break; n           } n         } n         if (k == 60) { n           ans += b[i]; n           vis[i] = 1; n           v.push_back(total[i]); n           break; n         } n       } n     } n   } n   cout << ans << endl; n } n int main() { n   int t; n   t = 1; n   while (t--) { n     solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long N = 1e5 + 5; n long long n, k, dp[N], sum, pos, s1, s2, deep[N], mndp[N], ans, ss, ss1; n vector<pair<long long, long long> > g[N]; n void dfs(long long x, long long p) { n   dp[x] = 0; n   for (long long i = 0; i < g[x].size(); ++i) { n     long long y = g[x][i].first; n     if (y == p) continue; n     dfs(y, x); n     dp[x] = max(dp[x], dp[y] + g[x][i].second); n   } n } n void get_ans(long long x, long long p, long long s) { n   if (max(dp[x], s) <= sum) { n     sum = max(dp[x], s); n     pos = x; n   } n   vector<long long> a(g[x].size(), 0), b(g[x].size(), 0); n   for (long long i = 0; i < g[x].size(); ++i) { n     long long y = g[x][i].first; n     if (y != p) n       a[i] = b[i] = dp[y] + g[x][i].second; n     else n       a[i] = b[i] = 0; n     if (i > 0) a[i] = max(a[i], a[i - 1]); n   } n   for (long long i = (long long)b.size() - 2; i >= 0; --i) { n     b[i] = max(b[i], b[i + 1]); n   } n   for (long long i = 0; i < g[x].size(); ++i) { n     long long y = g[x][i].first; n     if (y == p) continue; n     long long mx = 0; n     if (i > 0) mx = max(mx, a[i - 1]); n     if (i + 1 < b.size()) mx = max(mx, b[i + 1]); n     get_ans(y, x, max(s, mx) + g[x][i].second); n   } n } n void dfs1(long long x, long long p, long long s) { n   deep[x] = deep[p] + 1; n   mndp[deep[x]] = min(mndp[deep[x]], max(s, dp[x])); n   ans = min(ans, max(mndp[deep[x]], ss1)); n   vector<long long> a(g[x].size(), 0), b(g[x].size(), 0); n   for (long long i = 0; i < g[x].size(); ++i) { n     long long y = g[x][i].first; n     if (y != p) n       a[i] = b[i] = dp[y] + g[x][i].second; n     else n       a[i] = b[i] = 0; n     if (i > 0) a[i] = max(a[i], a[i - 1]); n   } n   for (long long i = (long long)b.size() - 2; i >= 0; --i) { n     b[i] = max(b[i], b[i + 1]); n   } n   for (long long i = 0; i < g[x].size(); ++i) { n     long long y = g[x][i].first; n     if (y == p) continue; n     long long mx = 0; n     if (i > 0) mx = max(mx, a[i - 1]); n     if (i + 1 < b.size()) mx = max(mx, b[i + 1]); n     dfs1(y, x, max(mx, s)); n   } n } n void dfs2(long long x, long long p, long long s) { n   deep[x] = deep[p] + 1; n   ans = min(ans, max(max(s, dp[x]), ss1)); n   if (k - deep[x] - 1 > 0) { n     ans = min(ans, max(mndp[k - deep[x] - 1], max(s, dp[x]))); n   } n   vector<long long> a(g[x].size(), 0), b(g[x].size(), 0); n   for (long long i = 0; i < g[x].size(); ++i) { n     long long y = g[x][i].first; n     if (y != p) n       a[i] = b[i] = dp[y] + g[x][i].second; n     else n       a[i] = b[i] = 0; n     if (i > 0) a[i] = max(a[i], a[i - 1]); n   } n   for (long long i = (long long)b.size() - 2; i >= 0; --i) { n     b[i] = max(b[i], b[i + 1]); n   } n   for (long long i = 0; i < g[x].size(); ++i) { n     long long y = g[x][i].first; n     if (y == p) continue; n     long long mx = 0; n     if (i > 0) mx = max(mx, a[i - 1]); n     if (i + 1 < b.size()) mx = max(mx, b[i + 1]); n     dfs2(y, x, max(mx, s)); n   } n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   cin >> n >> k; n   for (long long i = 0; i <= n; ++i) { n     mndp[i] = 1e18; n   } n   if (n == 1) return cout << 0, 0; n   if (n == 2) { n     long long x, y, z; n     cin >> x >> y >> z; n     if (k == 2) z = 0; n     cout << z; n     return 0; n   } n   for (long long i = 1; i < n; ++i) { n     long long x, y, z; n     cin >> x >> y >> z; n     g[x].push_back(make_pair(y, z)); n     g[y].push_back(make_pair(x, z)); n   } n   dfs(1, 0); n   sum = 1e18; n   pos = 0; n   get_ans(1, 0, 0); n   if (k == 1) return cout << sum, 0; n   dfs(pos, 0); n   vector<pair<long long, long long> > a(g[pos].size()); n   for (long long i = 0; i < g[pos].size(); ++i) { n     a[i] = make_pair(dp[g[pos][i].first] + g[pos][i].second, g[pos][i].first); n   } n   sort(a.begin(), a.end()); n   reverse(a.begin(), a.end()); n   long long s = 0; n   if (a.size() > 2) n     ss = a[2].first; n   else n     ss = 0; n   s1 = a[0].second; n   s2 = a[1].second; n   mndp[0] = a[0].first; n   deep[pos] = 0; n   ans = sum; n   ss1 = a[1].first; n   dfs1(s1, pos, ss); n   for (int i = 1; i <= n; ++i) { n     mndp[i] = min(mndp[i], mndp[i - 1]); n   } n   ss1 = a[0].first; n   dfs2(s2, pos, ss); n   cout << ans; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   int a; n   int count = 0; n   cin >> n; n   for (int i = 1; i <= n; i++) { n     cin >> a; n     if (a == 1) count += 1; n   } n   if (n != 1) { n     if (count == (n - 1)) n       cout <<  YES  << endl; n     else n       cout <<  NO  << endl; n   } else { n     if (count == 1) n       cout <<  YES  << endl; n     else n       cout <<  NO  << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long int binExp(long long int a, long long int power, n                      long long int m = 1000000007) { n   long long int res = 1; n   while (power) { n     if (power & 1) res = (res * a) % m; n     a = (a * a) % m; n     power >>= 1; n   } n   return res; n } n int n, k; n int arr[100][100]; n int cost[100][100]; n void solve() { n   int q; n   cin >> n >> k; n   int c = (k + 1) / 2; n   for (int i = 1; i <= k; i++) { n     for (int j = 1; j <= k; j++) { n       arr[i][j] = arr[i][j - 1] + 1; n       cost[i][j] = abs(i - c) + abs(j - c) + cost[i][j - 1]; n     } n   } n   while (n--) { n     int ele; n     cin >> ele; n     int x = -1, l = -1, r = -1; n     int mn = 1000000000; n     for (int row = 1; row <= k; row++) { n       for (int _l = 1, _r = ele; _r <= k; _l++, _r++) { n         if (arr[row][_r] - arr[row][_l - 1] == ele) { n           if (mn > cost[row][_r] - cost[row][_l - 1]) { n             mn = cost[row][_r] - cost[row][_l - 1]; n             l = _l; n             r = _r; n             x = row; n           } n         } n       } n     } n     if (mn == 1000000000) { n       cout << -1 <<   n ; n     } else { n       cout << x <<     << l <<     << r <<   n ; n       for (int i = l; i <= r; i++) arr[x][i] -= i - l + 1; n       for (int i = r + 1; i <= k; i++) arr[x][i] -= ele; n     } n   } n } n int main(int argc, char const *argv[]) { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); 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 ucln(int k, int l) { n   int t; n   while (l > 0) { n     t = l; n     l = k % l; n     k = t; n   } n   return (k); n } n int main() { n   int x, y, m, n, a, b; n   cin >> n >> m >> x >> y >> a >> b; n   int u = ucln(a, b); n   a /= u; n   b /= u; n   int na, nb; n   if ((n / a) < (m / b)) { n     na = a * (n / a); n     nb = b * (n / a); n   } else { n     na = a * (m / b); n     nb = b * (m / b); n   } n   int x1, x2, y1, y2; n   x2 = x + na / 2; n   x1 = x2 - na; n   if (x1 < 0) { n     x2 -= x1; n     x1 = 0; n   } n   if (x2 > n) { n     x1 -= (x2 - n); n     x2 = n; n   } n   y2 = y + nb / 2; n   y1 = y2 - nb; n   if (y1 < 0) { n     y2 -= y1; n     y1 = 0; n   } n   if (y2 > m) { n     y1 -= (y2 - m); n     y2 = m; n   } n   cout << x1 <<     << y1 <<     << x2 <<     << y2; n   return 0; n } n


#include <bits/stdc++.h> n const int MOD = 1e9 + 7; n inline int addmod(int a, int b) { return (a + b) % MOD; } n const int MAXN = 105; n const int MAXM = 10005; n int cnt[MAXN]; n int n, a[MAXN]; n int f[MAXN][MAXM]; n int com[MAXN][MAXN]; n int main() { n   for (int i = 0; i < MAXN; i++) { n     com[i][0] = 1; n     for (int j = 1; j <= i; j++) { n       com[i][j] = addmod(com[i - 1][j], com[i - 1][j - 1]); n     } n   } n   scanf( %d , &n); n   f[0][0] = 1; n   for (int i = 0; i < n; i++) { n     scanf( %d , &a[i]); n     cnt[a[i]]++; n     for (int x = 100; x >= 1; x--) { n       for (int y = a[i]; y <= 10000; y++) n         f[x][y] = addmod(f[x][y], f[x - 1][y - a[i]]); n     } n   } n   int tot = 0; n   for (int i = 0; i < MAXN; i++) n     if (cnt[i] > 0) tot++; n   int ans = 0; n   for (int i = 0; i <= 100; i++) { n     int sum = 0; n     for (int j = 1; j <= cnt[i]; j++) { n       sum += i; n       if (f[j][sum] == com[cnt[i]][j]) { n         ans = std::max(ans, j); n         if (j == cnt[i] && tot == 2) ans = n; n       } n     } n   } n   printf( %d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int t; n   cin >> t; n   while (t--) { n     int n; n     cin >> n; n     string s; n     cin >> s; n     string temp = s; n     sort(s.begin(), s.end()); n     int count = 0; n     for (int i = 0; i < s.size(); i++) n       if (s[i] != temp[i]) count++; n     cout << count <<   n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int a[5000]; n int b[5000]; n int main() { n   int n, m; n   cin >> n >> m; n   int x; n   for (int i = 0; i < n; i++) cin >> a[i]; n   for (int i = 0; i < m; i++) cin >> b[i]; n   sort(a, a + n); n   sort(b, b + m); n   int cura = 0; n   for (int i = 0; i < m && cura < n; i++) n     if (b[i] >= a[cura]) cura++; n   cout << n - cura << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 23, MXN = N * N * 4; n struct Edge { n   int to, flow; n }; n struct Dinic { n   int s, t, P[MXN], D[MXN], qu[MXN]; n   vector<int> Adj[MXN]; n   vector<Edge> E; n   inline void Add(int v, int u, int w) { n     Adj[v].push_back(E.size()); n     E.push_back({u, w}); n     Adj[u].push_back(E.size()); n     E.push_back({v, 0}); n   } n   inline bool BFS() { n     int l = 0, r = 0; n     memset(D, -1, sizeof(D)); n     D[s] = 0; n     qu[r++] = s; n     while (r - l) { n       int v = qu[l++]; n       for (int &id : Adj[v]) n         if (E[id].flow && D[E[id].to] == -1) n           D[E[id].to] = D[v] + 1, qu[r++] = E[id].to; n     } n     return (D[t] != -1); n   } n   int DFS(int v, int flow) { n     if (v == t) return flow; n     int Flow = 0; n     for (int i = P[v]; i < Adj[v].size(); i++) { n       int id = Adj[v][i]; n       if (E[id].flow && D[E[id].to] == D[v] + 1) { n         int sub = DFS(E[id].to, min(flow, E[id].flow)); n         if (!sub) { n           swap(Adj[v][i], Adj[v][P[v]]); n           P[v]++; n           continue; n         } n         E[id].flow -= sub; n         E[id ^ 1].flow += sub; n         flow -= sub; n         Flow += sub; n         if (!flow) return Flow; n       } else n         swap(Adj[v][i], Adj[v][P[v]]), P[v]++; n     } n     return Flow; n   } n   inline int MaxFlow(int _s, int _t) { n     int Flow = 0; n     s = _s; n     t = _t; n     while (BFS()) { n       memset(P, 0, sizeof(P)); n       Flow += DFS(s, INT_MAX); n     } n     return Flow; n   } n }; n int n, m, D[N][N], gg[] = {-1, 1, 0, 0}; n int fl, ml, X[MXN], Y[MXN], T[MXN]; n pair<int, int> qu[MXN]; n char S[N][N]; n vector<vector<long long> > E; n inline bool Solve(int md) { n   Dinic G; n   int s = ml + fl + n * m * 2, t = s + 1; n   for (int i = 0; i < ml; i++) G.Add(s, i, 1); n   for (int i = 0; i < fl; i++) G.Add(i + ml, t, 1); n   for (int i = 0; i < n; i++) n     for (int j = 0; j < m; j++) n       G.Add(ml + fl + i + j * n, ml + fl + i + j * n + n * m, 1); n   for (int i = 0; i <= md; i++) { n     int id = E[i][1], cell = n * E[i][3] + E[i][2] + ml + fl; n     if (id < ml) n       G.Add(id, cell, 1); n     else n       G.Add(cell + n * m, id, 1); n   } n   return (G.MaxFlow(s, t) == ml); n } n int main() { n   scanf( %d%d%d%d , &n, &m, &ml, &fl); n   if (abs(ml - fl) != 1) return !printf( -1 ); n   for (int i = 0; i < n; i++) scanf( %s , &S[i]); n   if (ml < fl) n     scanf( %d%d%d , &X[ml], &Y[ml], &T[ml]); n   else n     scanf( %d%d%d , &X[ml + fl], &Y[ml + fl], &T[ml + fl]); n   for (int i = 0; i < ml; i++) scanf( %d%d%d , &X[i], &Y[i], &T[i]); n   if (ml < fl) ml++; n   for (int i = ml; i < ml + fl; i++) scanf( %d%d%d , &X[i], &Y[i], &T[i]); n   if (fl < ml) fl++; n   for (int i = 0; i < ml + fl; i++) { n     int l = 0, r = 0; n     memset(D, -1, sizeof(D)); n     X[i]--; n     Y[i]--; n     D[X[i]][Y[i]] = 0; n     qu[r++] = {X[i], Y[i]}; n     while (r - l) { n       int a = qu[l].first, b = qu[l++].second; n       E.push_back({1LL * D[a][b] * T[i], i, a, b}); n       for (int h = 0; h < 4; h++) { n         int ta = a + gg[h], tb = b + gg[(h + 2) % 4]; n         if (ta < 0 || tb < 0 || ta >= n || tb >= m) continue; n         if (S[ta][tb] ==  #  || D[ta][tb] != -1) continue; n         D[ta][tb] = D[a][b] + 1; n         qu[r++] = {ta, tb}; n       } n     } n   } n   sort(E.begin(), E.end()); n   int le = -1, ri = E.size(), md; n   while (ri - le > 1) { n     md = (le + ri) >> 1; n     if (Solve(md)) n       ri = md; n     else n       le = md; n   } n   if (ri < E.size()) return !printf( %lld , E[ri][0]); n   return !printf( -1 ); n } n


#include <bits/stdc++.h> n using namespace std; n int arr[92]; n int main() { n   int n; n   cin >> n; n   for (int i = 0; i < n; i++) cin >> arr[i]; n   if (n == 1) { n     if (arr[0] == 15) n       cout <<  DOWN ; n     else if (arr[0] == 0) n       cout <<  UP ; n     else n       cout << -1; n   } else { n     if (arr[n - 1] > arr[n - 2]) { n       if (arr[n - 1] == 15) n         cout <<  DOWN ; n       else n         cout <<  UP ; n     } else if (arr[n - 1] < arr[n - 2]) { n       if (arr[n - 1] == 0) n         cout <<  UP ; n       else n         cout <<  DOWN ; n     } else n       cout << -1; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int Maxn = 3000; n int r, c, n; n int k; n struct Node { n   int x, y; n   friend bool operator<(Node a, Node b) { n     if (a.y == b.y) { n       return a.x < b.x; n     } n     return a.y < b.y; n   } n } a[Maxn + 5], b[Maxn + 5]; n int len; n vector<int> g[Maxn + 5]; n int lst[Maxn + 5], nxt[Maxn + 5]; n long long tmp; n int cnt[Maxn + 5]; n void calc(int x, int k) { n   tmp -= b[x].y * cnt[x]; n   tmp += b[lst[x]].y * cnt[x]; n   if (lst[x] && k > 1) { n     calc(lst[x], k - 1); n   } n   if (lst[x]) { n     cnt[lst[x]] += cnt[x]; n   } n   cnt[x] = 0; n } n int main() { n   scanf( %d%d%d%d , &r, &c, &n, &k); n   for (int i = 1; i <= n; i++) { n     scanf( %d%d , &a[i].x, &a[i].y); n   } n   sort(a + 1, a + 1 + n); n   long long ans = 0; n   for (int left = 1, right; left <= r; left++) { n     right = r; n     for (int i = 1; i <= r; i++) { n       g[i].clear(); n     } n     len = 0; n     for (int i = 1; i <= n; i++) { n       if (a[i].x >= left) { n         b[++len] = a[i]; n         lst[len] = len - 1; n         nxt[len] = len + 1; n         cnt[len] = 0; n         g[b[len].x].push_back(len); n       } n     } n     tmp = 0; n     for (int i = 1, j = 0; i <= c; i++) { n       while (j + 1 <= len && b[j + 1].y <= i) { n         j++; n       } n       if (j - k + 1 > 0) { n         cnt[j - k + 1]++; n         tmp += b[j - k + 1].y; n       } n     } n     ans += tmp; n     while (right > left) { n       for (int i = 0; i < (int)g[right].size(); i++) { n         int u = g[right][i]; n         calc(u, k); n         nxt[lst[u]] = nxt[u]; n         lst[nxt[u]] = lst[u]; n       } n       ans += tmp; n       right--; n     } n   } n   printf( %lld n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long int n, m; n long long int arr[200005]; n map<long long int, long long int> x; n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   ; n   cin >> n >> m; n   for (int i = 0; i < n; ++i) cin >> arr[i]; n   long long int l = 0, sum = 0, rem = 0, ksum = 0; n   vector<long long int> ans; n   while (l < n) { n     int cnt = 0; n     rem = 0; n     sum = ksum; n     if (sum + arr[l] > m) { n       auto it = x.rbegin(); n       while (sum + arr[l] > m && it != x.rend()) { n         pair<long long int, long long int> curr = *(it); n         if ((long long int)ceil((sum + arr[l] - m) * 1.0 / curr.first) > n             curr.second) { n           rem += curr.second; n           sum -= (curr.first * curr.second); n           it++; n         } else { n           rem += (long long int)ceil((sum + arr[l] - m) * 1.0 / curr.first); n           sum -= (curr.first) * n                  ((long long int)ceil((sum + arr[l] - m) * 1.0 / curr.first)); n         } n       } n     } n     ksum += arr[l]; n     if (x.find(arr[l]) != x.end()) { n       pair<long long int, long long int> curr = *(x.find(arr[l])); n       x.erase(arr[l]); n       x.insert(make_pair(curr.first, curr.second + 1)); n     } else n       x.insert(make_pair(arr[l], 1)); n     ans.push_back(rem); n     l++; n   } n   for (auto it : ans) cout << it <<    ; n   cout << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int M = 100005; n long long dp[M][2]; n vector<int> G[M]; n int color[M]; n void dfs(int x) { n   long long ones, zeros; n   ones = color[x]; n   zeros = 1 - color[x]; n   int u; n   for (int i = 0; i < G[x].size(); i++) { n     u = G[x][i]; n     dfs(u); n     ones = (ones * dp[u][1] + ones * dp[u][0] + zeros * dp[u][1]) % 1000000007; n     zeros = (zeros * dp[u][1] + zeros * dp[u][0]) % 1000000007; n   } n   dp[x][1] = ones; n   dp[x][0] = zeros; n } n int main() { n   int n; n   scanf( %d , &n); n   int x; n   for (int i = 0; i < n - 1; i++) { n     scanf( %d , &x); n     G[x].push_back(i + 1); n   } n   for (int i = 0; i < n; i++) { n     scanf( %d , &color[i]); n   } n   dfs(0); n   printf( %d n , dp[0][1]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int INF = 0x3f3f3f3f; n const long long LINF = 0x3f3f3f3f3f3f3f3fLL; n const double EPS = 1e-8; n const int MOD = 1000000007; n const int dy[] = {1, 0, -1, 0}, dx[] = {0, -1, 0, 1}; n int main() { n   cin.tie(0); n   ios::sync_with_stdio(false); n   int h, w; n   cin >> h >> w; n   vector<int> y{1}, x{1}; n   int ue = 1, shita = h; n   while (ue < shita) { n     for (int ue_x = 1, shita_x = w; ue_x <= w && shita_x >= 0; --shita_x) { n       y.emplace_back(shita); n       x.emplace_back(shita_x); n       ++ue_x; n       if (ue_x <= w) { n         y.emplace_back(ue); n         x.emplace_back(ue_x); n       } n     } n     ++ue; n     --shita; n     if (ue <= shita) { n       y.emplace_back(ue); n       x.emplace_back(1); n     } n   } n   if (ue == shita) { n     int hidari = 1, migi = w; n     while (hidari < migi) { n       y.emplace_back(ue); n       x.emplace_back(migi); n       ++hidari; n       if (hidari < migi) { n         y.emplace_back(ue); n         x.emplace_back(hidari); n       } n       --migi; n     } n   } n   set<pair<int, int> > st; n   for (int i = (1); i < (y.size()); ++i) n     st.emplace(y[i] - y[i - 1], x[i] - x[i - 1]); n   if (st.size() < h * w - 1) assert(false); n   for (int i = (0); i < (y.size()); ++i) cout << y[i] <<     << x[i] <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long oo = 1000000000000000000ll; n const int N = 2020, M = 100100; n int i, j, k, n, m, ch, ff, En, s, t, x, y, v, nls; n int h[N], p[N], d[N], z[N], diss[N], dist[N]; n long long c[N], Ls[N], Sum[N][N], Num[N][N], f[N][N][2]; n struct edge { n   int s, n, v; n } E[M << 1]; n struct cc { n   long long x; n   int y; n } QA[N][2], QB[N][2]; n void R(int &x) { n   ff = x = 0; n   ch = getchar(); n   while (ch <  0  ||  9  < ch) { n     if (ch ==  - ) ff = 1; n     ch = getchar(); n   } n   while ( 0  <= ch && ch <=  9 ) x = x * 10 + ch -  0 , ch = getchar(); n   if (ff) x = -x; n } n void E_add(int x, int y, int v) { n   E[++En].s = y; n   E[En].v = v; n   E[En].n = h[x]; n   h[x] = En; n   E[++En].s = x; n   E[En].v = v; n   E[En].n = h[y]; n   h[y] = En; n } n int find(long long x) { n   int l = 1, r = n, mid; n   while (l < r) { n     mid = (l + r) >> 1; n     if (Ls[mid] == x) return mid; n     if (Ls[mid] < x) n       l = mid + 1; n     else n       r = mid - 1; n   } n   return l; n } n void spfa(int *dis, int x) { n   int i, j, k; n   memset(c, 60, sizeof c); n   memset(z, 0, sizeof z); n   i = 0; n   j = 1; n   d[1] = x; n   c[x] = 0; n   z[x] = 1; n   while (i != j) { n     if (++i > n) i = 1; n     int u = d[i]; n     for (k = h[u]; k; k = E[k].n) n       if (c[u] + E[k].v < c[E[k].s]) { n         c[E[k].s] = c[u] + E[k].v; n         if (!z[E[k].s]) { n           z[E[k].s] = 1; n           if (++j > n) j = 1; n           d[j] = E[k].s; n         } n       } n     z[u] = 0; n   } n   nls = 0; n   for (i = 1; i <= n; i++) Ls[i] = c[i]; n   sort(Ls + 1, Ls + n + 1); n   nls = 0; n   Ls[n + 1] = -1; n   for (i = 1; i <= n; i++) n     if (Ls[i] != Ls[i + 1]) Ls[++nls] = Ls[i]; n   for (i = 1; i <= n; i++) dis[i] = find(c[i]); n } n void Up(cc *a, cc x) { n   if (x.x < a[0].x) { n     if (a[0].y < x.y) a[1] = a[0]; n     a[0] = x; n     if (a[1].y >= a[0].y) a[1].x = 0, a[1].y = 0; n   } else { n     if (x.y < a[0].y && x.x < a[1].x) a[1] = x; n   } n } n int main() { n   memset(f, 190, sizeof f); n   R(n); n   R(m); n   R(s); n   R(t); n   for (i = 1; i <= n; i++) R(p[i]); n   for (i = 1; i <= m; i++) { n     R(x); n     R(y); n     R(v); n     E_add(x, y, v); n   } n   spfa(diss, s); n   spfa(dist, t); n   for (i = 1; i <= n; i++) { n     Sum[diss[i]][dist[i]] += p[i]; n     Num[diss[i]][dist[i]]++; n   } n   for (i = n; i >= 0; i--) n     for (j = n; j >= 0; j--) { n       Sum[i][j] += Sum[i + 1][j] + Sum[i][j + 1] - Sum[i + 1][j + 1]; n       Num[i][j] += Num[i + 1][j] + Num[i][j + 1] - Num[i + 1][j + 1]; n     } n   for (i = 0; i <= n; i++) n     for (j = 0; j <= n; j++) n       if (!Num[i][j]) f[i][j][0] = f[i][j][1] = 0; n   for (i = n; i >= 1; i--) n     for (j = n; j >= 1; j--) { n       long long t; n       if (QB[j][0].y >= Num[i][j]) n         t = QB[j][1].x; n       else n         t = QB[j][0].x; n       f[i][j][0] = Sum[i][j] - t; n       if (QA[i][0].y >= Num[i][j]) n         t = QA[i][1].x; n       else n         t = QA[i][0].x; n       f[i][j][1] = Sum[i][j] - t; n       cc tt; n       tt.x = f[i][j][0]; n       tt.y = Num[i][j]; n       Up(QA[i], tt); n       tt.x = f[i][j][1]; n       tt.y = Num[i][j]; n       Up(QB[j], tt); n     } n   long long A = f[1][1][0], B = Sum[1][1] - A; n   if (A > B) n     puts( Break a heart ); n   else { n     if (A == B) n       puts( Flowers ); n     else n       puts( Cry ); n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int MOD = 1000000007; n int n; n long long power[300009], arr[300009]; n long long len, l, r, ans, add; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(NULL); n   cin >> n; n   for (int i = 0; i < n; i++) { n     cin >> arr[i]; n   } n   power[0] = 1; n   for (int i = 1; i <= n; i++) { n     power[i] = power[i - 1] * 2; n     power[i] += MOD; n     power[i] = power[i] % MOD; n   } n   sort(arr, arr + n); n   ans = 0; n   for (int i = 1; i < n; i++) { n     l = i; n     r = n - i; n     len = arr[i] - arr[i - 1]; n     add = ((power[l] - 1 + MOD) * (power[r] - 1 + MOD)) % MOD * len; n     add = add % MOD; n     ans += add; n     ans += MOD; n     ans = ans % MOD; n   } n   cout << ans <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n int n, deg[100100], t[100100], legs[100100]; n vector<int> adj[100100]; n void dfs(int x, int px) { n   t[x] = 1; n   for (int i = 0; i < adj[x].size(); i++) { n     int u = adj[x][i]; n     if (u == px) continue; n     if (deg[u] <= 2) dfs(u, x); n   } n } n int main() { n   cin >> n; n   for (int i = 1; i <= n - 1; i++) { n     int x, y; n     cin >> x >> y; n     adj[x].push_back(y); n     adj[y].push_back(x); n     deg[x]++; n     deg[y]++; n   } n   for (int i = 1; i <= n; i++) n     if (deg[i] == 1) dfs(i, 0); n   for (int i = 1; i <= n; i++) { n     int x = adj[i].size(); n     for (int j = 0; j < x; j++) { n       int v = adj[i][j]; n       if (t[v]) legs[i]++; n     } n   } n   bool fl = false; n   for (int i = 1; i <= n; i++) { n     if (t[i]) continue; n     int cou = 0; n     for (int j = 0; j < adj[i].size(); j++) { n       int v = adj[i][j]; n       if (!t[v] && deg[v] - min(legs[v], 2) >= 2) cou++; n       if (cou > 2) { n         fl = true; n         break; n       } n     } n   } n   if (fl) n     puts( No ); n   else n     puts( Yes ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<int> edge[100005]; n int tp[100005], dst[100005], fa[100005], d[100005], son[100005]; n int n, m, S = 1, E, mx, t = 1; n queue<int> Q; n void DFS(int &a, int p, int f, int d) { n   fa[p] = f, son[p] = 1; n   if (mx < d) { n     mx = d; n     a = p; n   } n   for (int i = 0; i < edge[p].size(); i++) { n     int to = edge[p][i]; n     if (to == f) continue; n     DFS(a, to, p, d + 1); n     son[p] = max(son[p], son[to] + 1); n   } n } n int BFS(int s, int used) { n   int dep = 0; n   d[s] = 0, tp[s] = t; n   Q.push(s); n   while (!Q.empty()) { n     int cur = Q.front(); n     Q.pop(); n     for (int i = 0; i < edge[cur].size(); i++) { n       int to = edge[cur][i]; n       if (to == used || to == fa[cur]) continue; n       Q.push(to); n       d[to] = d[cur] + 1; n       tp[to] = t; n       dep = max(d[to], dep); n     } n   } n   return dep; n } n struct Segment_Tree { n   struct node { n     int L, R, mx1, mx2; n   } tree[100005 << 2]; n   void up(int p) { n     tree[p].mx1 = max(tree[p << 1].mx1, tree[p << 1 | 1].mx1); n     tree[p].mx2 = max(tree[p << 1].mx2, tree[p << 1 | 1].mx2); n   } n   void build(int L, int R, int p) { n     tree[p].L = L, tree[p].R = R; n     if (L == R) { n       tree[p].mx1 = dst[L] - L; n       tree[p].mx2 = dst[L] + L; n       return; n     } n     int mid = L + R >> 1; n     build(L, mid, p << 1); n     build(mid + 1, R, p << 1 | 1); n     up(p); n   } n   int query1(int L, int R, int p) { n     if (L > R) return -1000000000; n     if (tree[p].L == L && tree[p].R == R) return tree[p].mx1; n     int mid = tree[p].L + tree[p].R >> 1; n     if (R <= mid) n       return query1(L, R, p << 1); n     else if (L > mid) n       return query1(L, R, p << 1 | 1); n     else n       return max(query1(L, mid, p << 1), query1(mid + 1, R, p << 1 | 1)); n   } n   int query2(int L, int R, int p) { n     if (L > R) return -1000000000; n     if (tree[p].L == L && tree[p].R == R) return tree[p].mx2; n     int mid = tree[p].L + tree[p].R >> 1; n     if (R <= mid) n       return query2(L, R, p << 1); n     else if (L > mid) n       return query2(L, R, p << 1 | 1); n     else n       return max(query2(L, mid, p << 1), query2(mid + 1, R, p << 1 | 1)); n   } n } T; n bool check(int mid, int a, int b, int l, int r) { n   if (abs(mid - l) + d[a] <= abs(r - mid) + d[b]) return true; n   return false; n } n int find(int a, int b, int l, int r) { n   int L = 1, R = t, res = 0; n   while (L <= R) { n     int mid = L + R >> 1; n     if (check(mid, a, b, l, r)) { n       res = mid; n       L = mid + 1; n     } else n       R = mid - 1; n   } n   return res; n } n int main() { n   scanf( %d , &n); n   for (int i = 1; i < n; i++) { n     int a, b; n     scanf( %d%d , &a, &b); n     edge[a].push_back(b); n     edge[b].push_back(a); n   } n   DFS(S, S, 0, 1); n   E = S, mx = 0; n   memset(fa, 0, sizeof(fa)); n   memset(son, 0, sizeof(son)); n   DFS(E, E, 0, 1); n   for (int i = S; true; t++) { n     for (int j = 0; j < edge[i].size(); j++) { n       int to = edge[i][j]; n       if (son[to] != son[i] - 1) continue; n       dst[t] = BFS(i, to); n       i = to; n       break; n     } n     if (son[i] == 1) { n       t++; n       dst[t] = BFS(i, 0); n       break; n     } n   } n   T.build(1, t, 1); n   scanf( %d , &m); n   for (int i = 1; i <= m; i++) { n     int a, b; n     scanf( %d%d , &a, &b); n     int l = tp[a], r = tp[b]; n     if (l > r) swap(l, r), swap(a, b); n     int mid = find(a, b, l, r); n     int ans = 0; n     if (l != r) { n       if (mid <= l) { n         ans = max(ans, T.query1(1, min(l - 1, mid), 1) + l + d[a]); n         ans = max(ans, T.query1(mid + 1, l - 1, 1) + r + d[b]); n         ans = max(ans, T.query1(l + 1, r - 1, 1) + r + d[b]); n         ans = max(ans, T.query2(r + 1, t, 1) - r + d[b]); n       } else if (mid >= r) { n         ans = max(ans, T.query1(1, l - 1, 1) + l + d[a]); n         ans = max(ans, T.query2(l + 1, r - 1, 1) - l + d[a]); n         ans = max(ans, T.query2(r + 1, mid, 1) - l + d[a]); n         ans = max(ans, T.query2(mid + 1, t, 1) - r + d[b]); n       } else { n         ans = max(ans, T.query1(1, l - 1, 1) + l + d[a]); n         ans = max(ans, T.query2(l + 1, mid, 1) - l + d[a]); n         ans = max(ans, T.query1(mid + 1, r - 1, 1) + r + d[b]); n         ans = max(ans, T.query2(r + 1, t, 1) - r + d[b]); n       } n     } else { n       ans = max(ans, T.query2(l + 1, t, 1) - l + min(d[a], d[b])); n       ans = max(ans, T.query1(1, l - 1, 1) + l + min(d[b], d[a])); n     } n     printf( %d n , ans); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const double inf = 1e10; n inline int cmp(double x, double y = 0, double tol = 1e-7) { n   return (x <= y + tol) ? (x + tol < y) ? -1 : 0 : 1; n } n struct ball { n   double x, v, m; n   int id; n   ball(double x = 0.0, double v = 0.0, double m = 0.0) : x(x), v(v), m(m) {} n   bool operator<(const ball &q) const { return cmp(x, q.x) < 0; } n }; n bool comp(ball a, ball b) { return a.id < b.id; } n ball p[100]; n int n, T; n inline double calcula(double m1, double m2, double v1, double v2) { n   return ((m1 - m2) * v1 + 2 * m2 * v2) / (m1 + m2); n } n void solve() { n   double t = 0.0; n   while (cmp(t, T) < 0) { n     double time = inf; n     for (int i = 0; i < n - 1; i++) { n       int j = i + 1; n       if (cmp(p[i].v - p[j].v) == 0) continue; n       double tmp = (p[j].x - p[i].x) / (p[i].v - p[j].v); n       if (cmp(tmp) <= 0) continue; n       time = min(time, tmp); n     } n     if (cmp(time, inf) == 0 || cmp(T, t + time) <= 0) break; n     for (int i = 0; i < n; i++) p[i].x = p[i].x + time * p[i].v; n     for (int i = 0; i < n - 1; i++) { n       int id1 = i, id2 = i + 1; n       if (cmp(p[id1].x, p[id2].x) != 0) continue; n       double aux = p[id1].v; n       p[id1].v = calcula(p[id1].m, p[id2].m, p[id1].v, p[id2].v); n       p[id2].v = calcula(p[id2].m, p[id1].m, p[id2].v, aux); n     } n     t += time; n   } n   for (int i = 0; i < n; i++) p[i].x = p[i].x + (T - t) * p[i].v; n } n int main() { n   cin >> n >> T; n   for (int i = 0; i < n; i++) { n     scanf( %lf %lf %lf , &p[i].x, &p[i].v, &p[i].m); n     p[i].id = i; n   } n   sort(p, p + n); n   solve(); n   sort(p, p + n, comp); n   for (int i = 0; i < n; i++) printf( %.9lf n , p[i].x); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n char buf[1 << 15], *fs, *ft; n inline char getc() { n   return (fs == ft && n           (ft = (fs = buf) + fread(buf, 1, 1 << 15, stdin), fs == ft)) n              ? 0 n              : *fs++; n } n inline int read() { n   int x = 0, f = 1; n   char ch = getc(); n   while (ch <  0  || ch >  9 ) { n     if (ch ==  - ) f = -1; n     ch = getc(); n   } n   while (ch >=  0  && ch <=  9 ) { n     x = x * 10 + ch -  0 ; n     ch = getc(); n   } n   return x * f; n } n const int MAXN = 200010; n int n, len; n long long ans, sz[MAXN][2]; n int f[MAXN][2]; n inline int getfather(int x, int y) { n   return f[x][y] == x ? x : f[x][y] = getfather(f[x][y], y); n } n inline void merge(int x, int y, int z) { n   int xx = getfather(x, z); n   int yy = getfather(y, z); n   sz[yy][z] += sz[xx][z]; n   f[xx][z] = yy; n } n int main() { n   n = read(); n   for (int i = 1; i <= n; ++i) f[i][0] = f[i][1] = i, sz[i][0] = sz[i][1] = 1; n   for (int i = 1; i < n; ++i) { n     int x, y; n     x = read(); n     y = read(); n     merge(x, y, read()); n   } n   for (int i = 1; i <= n; ++i) { n     int w1 = getfather(i, 0); n     int w2 = getfather(i, 1); n     if (w1 == i) ans += sz[w1][0] * (sz[w1][0] - 1); n     if (w2 == i) ans += sz[w2][1] * (sz[w2][1] - 1); n     ans += (sz[w1][0] - 1) * (sz[w2][1] - 1); n   } n   printf( %lld n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAX = 5 + 2e5; n int in[MAX], k = 1; n long long s[MAX], sorted[MAX], tos[MAX << 1], BIT[MAX << 1]; n map<long long, int> mp; n void add(int i, int val) { n   while (i > 0) { n     BIT[i] += val; n     i -= (i & (-i)); n   } n } n long long at_index(int i) { n   long long sum = 0; n   while (i <= k) { n     sum += BIT[i]; n     i += (i & (-i)); n   } n   return sum; n } n int main() { n   ios::sync_with_stdio(0); n   long long x = 0, t, ans = 0; n   int n, i; n   cin >> n >> t; n   for (i = 0; i < n; ++i) cin >> in[i]; n   for (i = 0; i < n; ++i) { n     x += in[i]; n     s[i] = x; n     sorted[i] = x; n     tos[i] = x; n   } n   int N = 1 + 2 * n; n   for (i = 0; i < n; ++i) tos[n + i] = t + tos[i]; n   tos[n + i] = t; n   sort(sorted, sorted + n); n   sort(tos, tos + N); n   for (i = 0; i < N; ++i) { n     if (mp.find(tos[i]) == mp.end()) { n       mp[tos[i]] = k; n       ++k; n     } n   } n   for (i = 0; i < n; ++i) { n     x = upper_bound(sorted, sorted + n, t - 1) - sorted; n     ans += (x - at_index(mp[t])); n     t += in[i]; n     add(k, 1); n     add(mp[s[i]], -1); n   } n   cout << ans; n } n


#include <bits/stdc++.h> n using namespace std; n int32_t main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   ; n   long long n; n   cin >> n; n   vector<long long> dp(n + 1, 0); n   long long s_odd = 0, s_even = 0; n   for (long long i = 1; i <= n; i++) { n     dp[i] = 1; n     if (i & 1) { n       dp[i] += s_even; n       s_odd += dp[i]; n     } else { n       dp[i] += s_odd; n       s_even += dp[i]; n     } n     s_even %= 1000000007; n     s_odd %= 1000000007; n   } n   s_even += s_odd; n   s_even %= 1000000007; n   cout << s_even; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   char conten[101]; n   int count1 = 0, count2 = 0, i; n   cin >> conten; n   int s = strlen(conten); n   for (i = 0; i <= s; i++) { n     if (conten[i] >= 65 && conten[i] <= 90) { n       count1++; n     } n     if (conten[i] >= 97 && conten[i] <= 122) { n       count2++; n     } n   } n   if (count1 > count2) { n     for (i = 0; i <= s; i++) { n       if (conten[i] >= 97 && conten[i] <= 122) { n         conten[i] -= 32; n       } n     } n   } n   if (count1 <= count2) { n     for (i = 0; i <= s; i++) { n       if (conten[i] >= 65 && conten[i] <= 90) { n         conten[i] += 32; n       } n     } n   } n   cout << conten; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int v[1000005]; n int main() { n   int n, m, i, first, second, lastdif = -0x3f3f3f3f; n   cin >> n; n   for (i = 1; i <= n; ++i) cin >> v[i]; n   for (i = 2; i <= n; ++i) { n     if (v[i] - v[i - 1] != lastdif && lastdif != -0x3f3f3f3f) { n       cout << v[n]; n       return 0; n       ; n     } n     lastdif = v[i] - v[i - 1]; n   } n   cout << v[n] + lastdif; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, m; n   cin >> n >> m; n   string s; n   cin >> s; n   int now = 0; n   for (int i = 0; i < n - 1; ++i) n     if (s[i] ==  .  && s[i + 1] ==  . ) ++now; n   while (m--) { n     int pos; n     char ch; n     cin >> pos >> ch; n     --pos; n     if (ch ==  . ) { n       if (s[pos] ==  . ) { n         cout << now << endl; n         continue; n       } n       if (pos < n - 1 && s[pos + 1] ==  . ) ++now; n       if (pos && s[pos - 1] ==  . ) ++now; n       s[pos] = ch; n     } else { n       if (s[pos] !=  . ) { n         cout << now << endl; n         continue; n       } n       if (pos < n - 1 && s[pos + 1] ==  . ) --now; n       if (pos && s[pos - 1] ==  . ) --now; n       s[pos] = ch; n     } n     cout << now << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e5 + 5, INF = 1e9; n bool check(int n, string s) { n   for (int i = 1; i <= n; i++) n     if (s[i] ==  0 ) return false; n   cout << 1 <<     << n - 1 <<     << 2 <<     << n << endl; n   return true; n } n void sol(int n, string s) { n   for (int i = 1; i <= n; i++) n     if (s[i] ==  0 ) { n       if (i - 1 >= n / 2) n         cout << 1 <<     << i <<     << 1 <<     << i - 1 << endl; n       else n         cout << i <<     << n <<     << i + 1 <<     << n << endl; n       return; n     } n } n int main() { n   cin.tie(0); n   ios::sync_with_stdio(false); n   int t; n   cin >> t; n   while (t--) { n     int n; n     string s; n     cin >> n >> s; n     s =  #  + s; n     if (!check(n, s)) sol(n, s); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 111111; n pair<char, string> ev[N]; n int n; n int solve(char le) { n   set<char> st; n   for (int i = ( a ); i <= ( z ); i++) { n     st.insert(i); n   } n   int need = 0, has = 0; n   bool found = false; n   for (int i = (1); i <= (n); i++) { n     if (ev[i].first ==  . ) { n       for (auto c : ev[i].second) { n         st.erase(c); n       } n     } else if (ev[i].first ==  ! ) { n       has++; n       set<char> keep; n       for (auto c : ev[i].second) { n         keep.insert(c); n       } n       for (int c = ( a ); c <= ( z ); c++) { n         if (keep.find(c) == keep.end()) { n           st.erase(c); n         } n       } n     } else if (i != n && ev[i].first ==  ? ) { n       char gu = ev[i].second[0]; n       if (gu == le) { n         return -1; n       } else { n         st.erase(gu); n       } n       has++; n     } else if (!found && i == n && ev[i].first ==  ? ) { n       return 0; n     } n     if (st.find(le) == st.end()) return -1; n     if (!found && (int)(st).size() == 1 && *(st.begin()) == le) { n       found = true; n       need = has; n     } n   } n   if (!found) return -1; n   return has - need; n } n signed main() { n   ios_base::sync_with_stdio(false); n   cin >> n; n   for (int i = (1); i <= (n); i++) { n     cin >> ev[i].first >> ev[i].second; n   } n   int ans = -1; n   for (int c = ( a ); c <= ( z ); c++) { n     ans = max(ans, solve(c)); n   } n   cout << ans <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const double PI = acos(-1.0); n const int N = 200010; n const bool QUERY_MAX = true; n const double INF = 1e30; n struct Line { n   long long a, b, val; n   double xLeft; n   bool is_query; n   Line() {} n   Line(long long _a, long long _b) { n     a = _a; n     b = _b; n     val = 0; n     xLeft = -INF; n     is_query = false; n   } n   long long value(long long x) { return a * x + b; } n   bool operator<(const Line l) const { n     if (l.is_query) { n       return QUERY_MAX ? (xLeft < l.val) : (l.val < xLeft); n     } n     return a < l.a; n   } n   bool areParallel(const Line l) const { return a == l.a; } n   double intersect(const Line l) const { n     if (!areParallel(l)) return (l.b - b) / (1.0 * (a - l.a)); n     return INF; n   } n   void printLine() const { printf( a = %lld  t b = %lld n , a, b); } n }; n set<Line> hull; n bool has_prev(set<Line>::iterator it) { return it != hull.begin(); } n bool has_next(set<Line>::iterator it) { n   return (it != hull.end()) and (++it != hull.end()); n } n bool irrelevant(set<Line>::iterator it) { n   if (!has_prev(it) or !has_next(it)) { n     return false; n   } n   set<Line>::iterator prev = it, next = it; n   prev--; n   next++; n   return QUERY_MAX ? prev->intersect(*next) <= prev->intersect(*it) n                    : next->intersect(*prev) <= next->intersect(*it); n } n set<Line>::iterator update_left_border(set<Line>::iterator it) { n   if ((QUERY_MAX and !has_prev(it)) or (!QUERY_MAX and !has_next(it))) n     return it; n   set<Line>::iterator it2 = it; n   double val = it->intersect(QUERY_MAX ? *--it2 : *++it2); n   Line l(*it); n   l.xLeft = val; n   hull.erase(it++); n   return hull.insert(it, l); n } n void addLine(long long a, long long b) { n   Line l(a, b); n   set<Line>::iterator it = hull.lower_bound(l); n   if (it != hull.end() and it->areParallel(l)) { n     if ((QUERY_MAX and it->b < b) or (!QUERY_MAX and it->b > b)) n       hull.erase(it++); n     else n       return; n   } n   it = hull.insert(it, l); n   if (irrelevant(it)) { n     hull.erase(it); n     return; n   } n   while (has_prev(it) and irrelevant(--it)) hull.erase(it++); n   while (has_next(it) and irrelevant(++it)) hull.erase(it--); n   it = update_left_border(it); n   if (has_prev(it)) { n     update_left_border(--it); n   } n   if (has_next(++it)) { n     update_left_border(++it); n   } n } n long long best(long long x) { n   Line q(0, 0); n   q.val = x; n   q.is_query = true; n   set<Line>::iterator it = hull.lower_bound(q); n   if (QUERY_MAX) --it; n   return it->a * x + it->b; n } n long long v[N], sum[N], resp; n int main(void) { n   int n; n   scanf( %d , &n); n   for (int i = 0; i < n; i++) { n     scanf( %lld , v + i); n     sum[i + 1] = sum[i] + v[i]; n     resp += v[i] * (i + 1); n   } n   long long ans = resp; n   addLine(n, -sum[n]); n   for (int i = n - 1; i >= 1; i--) { n     long long x = v[i - 1]; n     long long value = best(x) + resp + sum[i] - v[i - 1] * i; n     addLine(i, -sum[i]); n     ans = max(ans, value); n   } n   hull.clear(); n   addLine(1, 0); n   for (int i = 2; i <= n; i++) { n     long long x = v[i - 1]; n     long long value = best(x) + resp + sum[i - 1] - v[i - 1] * i; n     addLine(i, -sum[i - 1]); n     ans = max(ans, value); n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n int main(void) { n   int n; n   scanf( %d , &n); n   int a = 0, b = 1023; n   for (int i = 1; i <= n; ++i) { n     char ch[2]; n     int x; n     scanf( %s%d , ch, &x); n     if (ch[0] ==  & ) { n       a &= x; n       b &= x; n     } else if (ch[0] ==  | ) { n       a |= x; n       b |= x; n     } else if (ch[0] ==  ^ ) { n       a ^= x; n       b ^= x; n     } n   } n   int ans1 = 0, ans2 = 0; n   for (int T = 0; T < 10; T++) { n     int flag1 = (b & (1 << T)); n     int flag2 = (a & (1 << T)); n     if (flag1 && flag2) { n       ans1 |= (1 << T); n     } else if (!flag1 && flag2) { n       ans2 ^= (1 << T); n     } else if (!flag1 && !flag2) { n       ans1 |= (1 << T); n       ans2 ^= (1 << T); n     } n   } n   printf( 2 n| %d n^ %d n , ans1, ans2); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n string s[] = {  ,         January ,   February ,  March ,   April , n                May ,      June ,      July ,      August ,  September , n                October ,  November ,  December }; n int main() { n   string t; n   int k; n   cin >> t >> k; n   int x; n   for (int i = 1; i <= 12; ++i) { n     if (s[i] == t) { n       x = i; n       break; n     } n   } n   k %= 12; n   x += k; n   cout << s[x % 12 ? x % 12 : 12] << endl; n } n


#include <bits/stdc++.h> n using namespace std; n using LL = long long; n using PII = pair<int, int>; n int main() { n   int n; n   scanf( %d , &n); n   LL ansx, ansy; n   ansx = ansy = 0; n   n *= 2; n   for (int i = 1; i <= n; i++) { n     int x, y; n     scanf( %d%d , &x, &y); n     ansx += x; n     ansy += y; n   } n   n /= 2; n   printf( %lld %lld n , ansx / n, ansy / n); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n using ll = long long; n ll dp[2][5005]; n int main() { n   cin.tie(0); n   cin.sync_with_stdio(0); n   int n, k, x; n   cin >> n >> k >> x; n   if (n / k > x) return cout << -1, 0; n   vector<int> a(n); n   for (auto &x : a) cin >> x; n   x = min(x, n); n   for (int i = 0; i < k; ++i) dp[1][i] = a[i]; n   for (int z = 2; z <= x; ++z) { n     deque<ll> d; n     int c = (z & 1); n     int p = 1 - c; n     int m = min(n, z * k); n     memset(dp[c], 0, sizeof dp[c]); n     for (int i = 0; i < m; ++i) { n       while (d.size() && i - d.front() > k) d.pop_front(); n       if (i >= z - 1) dp[c][i] = a[i] + dp[p][d.front()]; n       while (d.size() && dp[p][d.back()] <= dp[p][i]) d.pop_back(); n       d.emplace_back(i); n     } n   } n   ll ans = 0; n   for (int i = n - 1; i >= 0; --i) { n     if (n - i <= k) { n       ans = max(ans, dp[x & 1][i]); n     } else { n       break; n     } n   } n   cout << ans; n } n


#include <bits/stdc++.h> n using namespace std; n int n; n int l[10], r[10]; n double Prob(int id, int a, int b) { n   int ini = max(a, l[id]); n   int fim = min(b, r[id]); n   if (b < l[id] || a > r[id]) return 0.0; n   double ret = (fim - ini + 1) * 1.0 / (double)(r[id] - l[id] + 1); n   return ret; n } n int main() { n   double ret = 0; n   scanf( %d , &n); n   for (int i = 0; i < n; i++) { n     scanf( %d %d , &l[i], &r[i]); n   } n   int ini = l[0], fim = r[0]; n   for (int i = 0; i < n; i++) { n     ini = min(l[i], ini); n     fim = max(r[i], fim); n   } n   for (int val = ini; val <= fim; val++) { n     double prob = 0; n     for (int i = 0; i < n; i++) { n       double aux3 = Prob(i, val + 1, r[i]); n       double aux = 1; n       for (int j = 0; j < n; j++) { n         if (i == j) continue; n         aux *= Prob(j, l[j], val - 1); n       } n       double aux2 = 1; n       for (int j = 0; j < n; j++) { n         if (i == j) continue; n         aux2 *= Prob(j, l[j], val); n       } n       aux3 *= (aux2 - aux); n       prob += aux3; n     } n     double p2, aux1, aux2, aux3; n     p2 = 0; n     for (int i = 0; i < n; i++) { n       aux1 = Prob(i, val, val); n       for (int j = 0; j < n; j++) { n         if (i == j) continue; n         aux1 *= Prob(j, l[j], val - 1); n       } n       p2 += aux1; n     } n     aux1 = p2; n     aux2 = 1; n     for (int i = 0; i < n; i++) aux2 *= Prob(i, l[i], val - 1); n     aux3 = 1; n     for (int i = 0; i < n; i++) aux3 *= Prob(i, l[i], val); n     prob += aux3 - aux2 - aux1; n     ret += prob * val; n   } n   printf( %.10lf n , ret); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, ar[5005], bit[5005]; n void add(int i) { n   while (i > 0) { n     bit[i]++; n     i -= (i & (-i)); n   } n } n int ask(int i) { n   int tmp = 0; n   while (i <= n) { n     tmp += bit[i]; n     i += (i & (-i)); n   } n   return tmp; n } n int main() { n   scanf( %d , &n); n   for (int i = 1; i <= n; i++) { n     scanf( %d , &ar[i]); n     ar[i]++; n   } n   long long old = 0, best = 0, pair = 0; n   for (int i = 1; i <= n; i++) { n     memset(bit, 0, sizeof(bit)); n     for (int j = i + 1; j <= n; j++) { n       if (ar[i] < ar[j]) continue; n       old++; n       int dec = 1 + 2 * ask(ar[j]); n       if (dec > best) n         best = dec, pair = 1; n       else if (dec == best) n         pair++; n       add(ar[j]); n     } n   } n   printf( %I64d %I64d n , old - best, pair); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   int n, i; n   long long a, b; n   cin >> n; n   i = a = 0, b = 1000000001; n   int arr[n]; n   while (i < n) { n     cin >> arr[i]; n     i++; n   } n   i = n - 1; n   while (i >= 0) { n     if (arr[i] < b) { n       a += arr[i]; n       b = arr[i]; n       if (b == 1) break; n     } else if (arr[i] >= b) { n       b--; n       a += b; n       if (b == 1) break; n     } n     i--; n   } n   cout << a << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const double esp = 1e-5; n int dis[30][30], du[30], id[30][30]; n double p[30], a[500][500]; n double solve(int i, int j, int x, int y) { n   double ans = 0; n   if (i == x && j == y) n     ans = p[i] * p[j]; n   else if (i == x && j != y) n     ans = p[i] * (1 - p[j]) / du[j] * dis[j][y]; n   else if (i != x && j == y) n     ans = p[j] * (1 - p[i]) / du[i] * dis[i][x]; n   else if (i != x && j != y) n     ans = (1 - p[i]) * (1 - p[j]) / du[i] / du[j] * dis[i][x] * dis[j][y]; n   return ans; n } n int main() { n   int n, m, st1, st2; n   scanf( %d%d%d%d , &n, &m, &st1, &st2); n   for (int i = 1; i <= m; i++) { n     int u, v; n     scanf( %d%d , &u, &v); n     du[u]++; n     du[v]++; n     dis[u][v] = dis[v][u] = 1; n   } n   for (int i = 1; i <= n; i++) scanf( %lf , &p[i]); n   if (st1 == st2) { n     for (int i = 1; i <= n; i++) { n       if (i == st1) n         printf( %.6f%c , 1.0, i == n ?   n  :    ); n       else n         printf( %.6f%c , 0.0, i == n ?   n  :    ); n     } n     return 0; n   } n   int cnt = 0; n   for (int i = 1; i <= n; i++) n     for (int j = 1; j <= n; j++) n       if (i != j) id[i][j] = ++cnt; n   int len = n * n - n; n   for (int i = 1; i <= n; i++) { n     for (int j = 1; j <= n; j++) { n       if (i == j) continue; n       int u = id[i][j]; n       a[u][u] = -1; n       for (int x = 1; x <= n; x++) { n         for (int y = 1; y <= n; y++) { n           if (x == y) { n             a[u][len + x] -= solve(i, j, x, y); n             continue; n           } n           int v = id[x][y]; n           a[u][v] += solve(i, j, x, y); n         } n       } n     } n   } n   for (int i = 1; i <= len; i++) { n     if (fabs(a[i][i]) < esp) { n       int pos = -1; n       for (int j = i + 1; j <= len; j++) n         if (fabs(a[j][i]) >= esp) { n           pos = j; n           break; n         } n       swap(a[i], a[pos]); n     } n     for (int j = i + 1; j <= len; j++) { n       double f = a[j][i] / a[i][i]; n       for (int k = i; k <= n * n; k++) a[j][k] -= f * a[i][k]; n     } n   } n   for (int i = len; i >= 1; i--) { n     double f = a[i][i]; n     for (int j = len + 1; j <= n * n; j++) a[i][j] /= f; n     a[i][i] = 1; n     for (int j = i - 1; j >= 1; j--) { n       f = a[j][i]; n       for (int k = len + 1; k <= n * n; k++) a[j][k] -= f * a[i][k]; n     } n   } n   for (int i = 1; i <= n; i++) n     printf( %.6f%c , a[id[st1][st2]][len + i], i == n ?   n  :    ); n   return 0; n } n


#include <bits/stdc++.h> n int main() { n   long int n, i, j, k, a, b, c, sum, ht; n   scanf( %ld , &n); n   i = 1; n   sum = 1; n   ht = 0; n   while (n > 0) { n     n = n - sum; n     sum = sum + i + 1; n     i++; n     if (n >= 0) { n       ht++; n     } n   } n   printf( %ld n , ht); n } n


#include <bits/stdc++.h> n using namespace std; n int n, m; n int ansx, ansy; n int a[300005][10]; n int b[300005]; n int check(long long x) { n   memset(b, 0, sizeof(b)); n   for (int i = 1; i <= n; i++) { n     int p = 0; n     for (int j = 1; j <= m; j++) { n       if (a[i][j] >= x) p += (1 << (j - 1)); n     } n     b[p] = i; n   } n   int k = (1 << m); n   int c = (1 << m) - 1; n   for (int i = 0; i <= k; i++) { n     for (int j = 0; j <= k; j++) { n       if (b[i] && b[j] && ((i | j) == c)) { n         ansx = b[i]; n         ansy = b[j]; n         return 1; n       } n     } n   } n   return 0; n } n int main() { n   scanf( %d %d , &n, &m); n   for (int i = 1; i <= n; i++) { n     for (int j = 1; j <= m; j++) { n       scanf( %d , &a[i][j]); n     } n   } n   long long l = 0, r = 1e9; n   while (l <= r) { n     long long mid = (l + r) / 2; n     if (check(mid)) { n       l = mid + 1; n     } else { n       r = mid - 1; n     } n   } n   printf( %d %d n , ansx, ansy); n } n


#include <bits/stdc++.h> n using namespace std; n int mm[110][110]; n int main() { n   int n, ss = 0, md; n   scanf( %d , &n); n   for (int i = 0; i < n; i++) n     for (int j = 0; j < n; j++) scanf( %d , &mm[i][j]); n   if (n % 2 == 0) n     md = n / 2; n   else n     md = (n / 2) + 1; n   md--; n   for (int i = 0; i < n; i++) n     for (int j = 0; j < n; j++) { n       if (i == j) ss += mm[i][j]; n       if (i + j == n - 1) ss += mm[i][j]; n     } n   for (int qq = 0; qq < n; qq++) ss += mm[md][qq]; n   for (int qq = 0; qq < n; qq++) ss += mm[qq][md]; n   printf( %d n , ss - mm[md][md] * 3); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n namespace WorkSpace { n const double pi = acos(-1.0); n int N; n double r, v; n bool Check(double mid, double l) { n   double len = 2 * pi * r; n   double ang = mid / len * 2 * pi; n   double A = r - r * (cos(ang)); n   double B = r * sin(ang); n   return sqrt(A * A + B * B) + mid > l; n } n void Deal(double s, double t) { n   double l = 0, r = t - s; n   double mid; n   int Cnt = 0; n   while (Cnt <= 40) { n     ++Cnt; n     mid = (l + r) / 2; n     if (Check(mid, t - s)) n       r = mid; n     else n       l = mid; n   } n   printf( %.10lf n , l / v); n } n void Main() { n   scanf( %d%lf%lf , &N, &r, &v); n   while (N--) { n     double s, t; n     scanf( %lf%lf , &s, &t); n     Deal(s, t); n   } n   return; n } n }  // namespace WorkSpace n int main() { WorkSpace ::Main(); } n


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


#include <bits/stdc++.h> n using namespace std; n const long long N = 1e3 + 9; n string s; n long long t, h, ans = 1e10; n long long n; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cin >> n; n   cin >> s; n   for (int i = 0; i < (n); i++) { n     if (s[i] ==  T ) n       t++; n     else n       h++; n   } n   for (int i = 0; i < (n); i++) { n     long long cur = i, res = 0; n     for (int j = 0; j < (t); j++) { n       if (s[cur] !=  T ) res++; n       cur = (cur + 1) % n; n     } n     ans = min(ans, res); n   } n   cout << ans << endl; n } n


#include <bits/stdc++.h> n using namespace std; n inline long long read() { n   long long x = 0, flag = 1; n   char ch = 0; n   while (!isdigit(ch)) { n     ch = getchar(); n     if (ch ==  - ) flag = -1; n   } n   while (isdigit(ch)) { n     x = (x << 3) + (x << 1) + ch -  0 ; n     ch = getchar(); n   } n   return x * flag; n } n const long long mo = 1e9 + 7; n long long x, y, fac[110000]; n long long exgcd(long long a, long long b) { n   if (!b) { n     x = 1; n     y = 0; n     return a; n   } n   long long d = exgcd(b, a % b); n   long long t = x; n   x = y; n   y = t - (a / b) * y; n   return d; n } n long long f(long long o) { n   exgcd(o, mo); n   return (x % mo + mo) % mo; n } n long long c(long long a, long long b) { n   long long up = fac[a]; n   long long down = (fac[b] * fac[a - b]) % mo; n   return (up * f(down)) % mo; n } n long long a[110000]; n bool vis[110000]; n int main() { n   long long n, k = 0, t = 0, i, x, ans = 0; n   n = read(); n   for (i = 1, fac[0] = 1; i <= n; i++) { n     fac[i] = (fac[i - 1] * i) % mo; n     a[i] = read(); n     if (a[i] != -1) { n       vis[a[i]] = 1; n       if (a[i] == i) { n         printf( 0 ); n         return 0; n       } n     } n   } n   for (i = 1; i <= n; i++) k += (a[i] == -1 && !vis[i]), t += (a[i] == -1); n   for (i = 0; i <= k; i++) { n     x = (c(k, i) * fac[t - i]) % mo; n     if (i & 1) n       ans = ((ans - x) % mo + mo) % mo; n     else n       ans = (ans + x) % mo; n   } n   printf( %lld , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int t; n   cin >> t; n   while (t--) { n     long long int n; n     cin >> n; n     if (n == 1) { n       cout << 0 <<   n ; n       continue; n     } n     long long int ans = 0; n     while (n != 1) { n       ans++; n       if (n % 2 == 0) { n         n = n / 2; n       } else if (n % 3 == 0) { n         n = 2 * n / 3; n       } else if (n % 5 == 0) { n         n = 4 * n / 5; n       } else { n         ans = -1; n         break; n       } n     } n     cout << ans <<   n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int inf = 1e9 + 7; n const long long llinf = 1e18 + 7; n const double eps = 1e-15; n const int mod = 1e9 + 7; n int main() { n   ios::sync_with_stdio(false); n   int n, a[100001], k; n   cin >> n >> k; n   for (int i = 1; i <= n; i++) cin >> a[i]; n   int i = n; n   while (i > 0 && a[n] == a[i]) i--; n   if (i >= k) n     cout << -1; n   else n     cout << i; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n; n int a[200000 + 5]; n vector<int> g[200000 + 5]; n int cnt = 0; n void dfs(int x, int fa) { n   cnt++; n   if (cnt > n) return; n   printf( %d  , x); n   for (int i = 0; i < g[x].size(); i++) { n     int y = g[x][i]; n     if (y == fa) continue; n     dfs(y, x); n   } n } n int main() { n   scanf( %d , &n); n   int xx, yy; n   for (int i = 1; i <= n; i++) { n     int x, y; n     scanf( %d , &x), scanf( %d , &y); n     if (i == 1) xx = x, yy = y; n     g[y].push_back(x), g[x].push_back(y); n   } n   if (g[1][0] != xx && g[1][0] != yy) swap(g[1][0], g[1][1]); n   dfs(1, 0); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long int gcd(long long int a, long long int b) { n   if (a == 0) n     return b; n   else n     return gcd(b % a, a); n } n long long int lcm(long long int a, long long int b) { n   return (a * b) / gcd(a, b); n } n long long int fact(long long int n) { n   if (n == 0) n     return 1; n   else n     return n * fact(n - 1); n } n void solve(long long int t) { n   long long int n, k; n   cin >> n >> k; n   long long int x = n / 2; n   long long int rem = k - (x - 1); n   if (n == 1) { n     if (k == 0) { n       cout <<  1 ; n       return; n     } else { n       cout <<  -1 ; n       return; n     } n   } else if (n == 2) { n     if (k == 0) { n       cout <<  -1 ; n     } else { n       cout << k <<     << k * 2; n     } n     return; n   } n   if (rem <= 0) { n     cout <<  -1 ; n     return; n   } n   cout << rem <<     << rem * 2 <<    ; n   long long int c = 1; n   for (long long int i = 1; i < x; i++) { n     while (c == rem || c + 1 == rem || c == rem * 2 || c + 1 == rem * 2) { n       c += 1; n     } n     cout << c <<     << c + 1 <<    ; n     c += 2; n     while (c == rem || c + 1 == rem || c == rem * 2 || c + 1 == rem * 2) { n       c += 1; n     } n   } n   if (n % 2 != 0) cout << c; n } n int main() { n   cin.tie(0); n   ios::sync_with_stdio(0); n   cout.tie(0); n   long long int T = 1; n   for (long long int t = 1; t <= T; t++) { n     solve(t); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int a[100000]; n int b[100000]; n bool judge(long long x, int n, long long k) { n   for (int i = 0; i < n; ++i) { n     k -= max(0LL, a[i] * x - b[i]); n     if (k < 0) { n       return false; n     } n   } n   return true; n } n int main(void) { n   int n, k; n   assert(scanf( %d%d , &n, &k) == 2); n   for (int i = 0; i < n; ++i) { n     assert(scanf( %d , &a[i]) == 1); n   } n   for (int i = 0; i < n; ++i) { n     assert(scanf( %d , &b[i]) == 1); n   } n   long long l = 0, r = 2000000001; n   while (r - l > 1) { n     long long m = (l + r) / 2; n     if (judge(m, n, k)) { n       l = m; n     } else { n       r = m; n     } n   } n   printf( %I64d n , l); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n inline long long mod(long long n, long long m) { n   long long ret = n % m; n   if (ret < 0) ret += m; n   return ret; n } n long long gcd(long long a, long long b) { n   return (b == 0LL ? a : gcd(b, a % b)); n } n long long exp(long long a, long long b, long long m) { n   if (b == 0LL) return 1LL; n   if (b == 1LL) return mod(a, m); n   long long k = mod(exp(a, b / 2, m), m); n   if (b & 1LL) { n     return mod(a * mod(k * k, m), m); n   } else n     return mod(k * k, m); n } n const int N = 100100; n int mat[N][2][2]; n void solve(vector<set<pair<int, int>>>& vs) { n   if ((int)vs[0].size() == 1) return; n   vector<int> big(4, -1e9); n   set<pair<int, int>>::iterator it[4]; n   for (int j = 0; j < 4; j++) { n     it[j] = vs[j].begin(); n   } n   int here = -1; n   int op = 0; n   for (; it[0] != vs[0].end();) { n     op++; n     for (int j = 0; j < 4; j++) { n       if (j >= 2) { n         big[j] = max(big[j], -mat[it[j]->second][0][j & 1]); n         it[j]++; n       } else { n         big[j] = max(big[j], mat[it[j]->second][1][j & 1]); n         it[j]++; n       } n     } n     if (it[0] == vs[0].end()) break; n     for (int j = 0; j < 4; j++) { n       if (j >= 2) { n         if (it[j]->first >= big[j]) { n           here = j; n           break; n         } n       } else { n         if (big[j] <= it[j]->first) { n           here = j; n           break; n         } n       } n     } n     if (here != -1) break; n   } n   if (here == -1) { n     cout <<  NO  n          <<   n ; n     exit(0); n   } n   vector<set<pair<int, int>>> novo(4, set<pair<int, int>>()); n   set<pair<int, int>>::iterator it2 = vs[here].begin(); n   vector<int> ids; n   while (it2 != it[here]) { n     int id = it2->second; n     it2++; n     for (int j = 0; j < 4; j++) { n       if (j < 2) { n         novo[j].insert(pair<int, int>(mat[id][j >> 1][j & 1], id)); n         vs[j].erase(pair<int, int>(mat[id][j >> 1][j & 1], id)); n       } else { n         novo[j].insert(pair<int, int>(-mat[id][j >> 1][j & 1], id)); n         vs[j].erase(pair<int, int>(-mat[id][j >> 1][j & 1], id)); n       } n     } n   } n   solve(novo); n   solve(vs); n } n int32_t main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   ; n   int n; n   cin >> n; n   for (int i = 1; i <= n; i++) { n     for (int j = 0; j < 2; j++) { n       for (int k = 0; k < 2; k++) { n         cin >> mat[i][j][k]; n       } n     } n   } n   vector<set<pair<int, int>>> vs(4, set<pair<int, int>>()); n   for (int j = 0; j < 4; j++) { n     for (int i = 1; i <= n; i++) { n       if (j < 2) n         vs[j].insert(pair<int, int>(mat[i][j >> 1][j & 1], i)); n       else n         vs[j].insert(pair<int, int>(-mat[i][j >> 1][j & 1], i)); n     } n   } n   solve(vs); n   cout <<  YES  n        <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   ; n   long long int t; n   t = 1; n   while (t--) { n     long long int n, i, j, k; n     cin >> n; n     map<long long int, long long int> m; n     vector<long long int> a, b; n     for (i = 0; i < n; i++) { n       cin >> j; n       a.push_back(j); n     } n     for (i = 0; i < n; i++) { n       cin >> j; n       b.push_back(j); n     } n     for (i = 0; i < n; i++) { n       m[a[i]] = b[i]; n     } n     for (i = 1; i < n + 1; i++) cout << m[i] <<    ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxi = 3e6; n vector<int> ans; n int a[maxi]; n int cnt[maxi]; n int c[maxi]; n void solve() { n   int n; n   scanf( %d , &n); n   for (int i = 1; i <= n; i++) { n     c[i] = 0; n     cnt[i] = 0; n   } n   for (int i = 1; i <= n; i++) { n     int x; n     scanf( %d , &x); n     c[x]++; n   } n   for (int i = 1; i <= n; i++) n     if (c[i]) cnt[c[i]]++; n   for (int i = 1; i <= n; i++) cnt[i] += cnt[i - 1]; n   int ans = n; n   for (int i = 2; i <= n; i++) { n     int rez = 0; n     int ima = 0; n     for (int j = 1; j <= n / i + 1; j++) { n       int first = max((j - 1) * i + 1, (i - 1) * j); n       int sc = min(n, i * j); n       if (first > sc) continue; n       rez += j * (cnt[sc] - cnt[first - 1]); n       ima += cnt[sc] - cnt[first - 1]; n     } n     if (ima == cnt[n]) ans = min(ans, rez); n   } n   printf( %d n , ans); n } n int main() { n   int t; n   cin >> t; n   while (t--) solve(); n } n


#include <bits/stdc++.h> n using namespace std; n const long long inf = 1e9 + 5; n long long power(long long x, long long y) { n   long long res = 1; n   while (y > 0) { n     if (y & 1) res = res * x; n     y = y >> 1; n     x = x * x; n   } n   return res; n } n int32_t main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   ; n   long long t; n   cin >> t; n   while (t--) { n     long long n, k; n     cin >> n >> k; n     long long a[n]; n     for (long long i = 0; i < n; i++) cin >> a[i]; n     set<long long> s; n     bool f = 1; n     if (k == 1) { n       cout <<  YES  <<   n ; n       continue; n     } n     for (long long i = 0; i < n; i++) { n       if (a[i] == 0) continue; n       long long temp = a[i]; n       while (temp > 0) { n         for (long long j = 0; j < 1e5; j++) { n           if (temp < power(k, j)) { n             temp -= power(k, j - 1); n             if (s.count(j - 1)) { n               f = 0; n               break; n             } else { n               s.insert(j - 1); n             } n             break; n           } n         } n       } n       if (f == 0 || temp != 0) { n         f = 0; n         break; n       } n     } n     cout << (f == 0 ?  NO  :  YES ) <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long inf = 1e9 + 7; n const long long INF = 1LL << 60; n const long long mod = 1e9 + 7; n const long double eps = 1e-8; n const long double pi = acos(-1.0); n template <class T> n inline bool chmax(T& a, T b) { n   if (a < b) { n     a = b; n     return 1; n   } n   return 0; n } n template <class T> n inline bool chmin(T& a, T b) { n   if (a > b) { n     a = b; n     return 1; n   } n   return 0; n } n long long f(long long l, long long p) { n   long long w = l / p; n   long long p2 = l % p, p1 = p - p2; n   return p1 * w * w + p2 * (w + 1) * (w + 1); n } n void solve() { n   long long n, k; n   cin >> n >> k; n   vector<long long> a(n); n   long long ans = 0; n   priority_queue<pair<long long, pair<long long, long long>>> pque; n   for (long long i = 0; i < n; i++) { n     cin >> a[i]; n     ans += a[i] * a[i]; n     pque.push({f(a[i], 1) - f(a[i], 2), {a[i], 2}}); n   } n   for (long long i = 0; i < k - n; i++) { n     auto x = pque.top(); n     pque.pop(); n     ans -= x.first; n     long long l = x.second.first, p = x.second.second; n     pque.push({f(l, p) - f(l, p + 1), {l, p + 1}}); n   } n   cout << ans << endl; n } n signed main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   solve(); n   return 0; n } n


#include <stdio.h> n #include <stdlib.h> n #define N 200000 n #define Q 100000 n #define N_ ((N + Q * 2) * (LG + 1)) n #define LG 20 n #define B (1 << LG) n int ll[1 + N_], rr[1 + N_], sz[1 + N_], msk0[1 + N_], msk1[1 + N_], lz[1 + N_], _ = 1; n void put(int u, int lg, int x) {int tmp, x0, x1;if (u == 0)return;if ((x & 1 << lg - 1) != 0)tmp = ll[u], ll[u] = rr[u], rr[u] = tmp; n  x0 = msk0[u] & ~x | msk1[u] & x, x1 = msk1[u] & ~x | msk0[u] & x;msk0[u] = x0, msk1[u] = x1;lz[u] ^= x;} n void pus(int u, int lg) {if (lz[u])put(ll[u], lg - 1, lz[u]), put(rr[u], lg - 1, lz[u]), lz[u] = 0;} n void pul(int u) {int l = ll[u], r = rr[u];sz[u] = sz[l] + sz[r], msk0[u] = msk0[l] | msk0[r], msk1[u] = msk1[l] | msk1[r];} n int add(int u, int lg, int a) {if (u == 0)u = _++;if (lg == 0)sz[u] = 1, msk0[u] = a ^ B - 1, msk1[u] = a; n  else {if ((a & 1 << lg - 1) == 0)ll[u] = add(ll[u], lg - 1, a);else rr[u] = add(rr[u], lg - 1, a);pul(u);}return u;} n void split(int *u, int *v, int lg, int l, int r, int ql, int qr) { n  int m; n  n  if (qr <= l || r <= ql || *u == 0) { n   *v = 0; n   return; n  } n  if (ql <= l && r <= qr) { n   *v = *u, *u = 0; n   return; n  } n  pus(*u, lg), *v = _++; n  m = (l + r) / 2; n  split(&ll[*u], &ll[*v], lg - 1, l, m, ql, qr), split(&rr[*u], &rr[*v], lg - 1, m, r, ql, qr); n  pul(*u), pul(*v); n } n  n void merge(int *u, int *v, int lg) {if (*u == 0) {*u = *v;return;}if (*v == 0 || lg == 0)return;pus(*u, lg), pus(*v, lg);merge(&ll[*u], &ll[*v], lg - 1), merge(&rr[*u], &rr[*v], lg - 1);pul(*u);} n void update_or(int u, int lg, int x) { n  if (u == 0) n   return; n  if ((x & msk0[u] & msk1[u]) == 0) { n   put(u, lg, x & msk0[u]); n   return; n  } n  pus(u, lg); n  if ((x & 1 << lg - 1) != 0) n   put(ll[u], lg - 1, 1 << lg - 1), merge(&rr[u], &ll[u], lg - 1), ll[u] = 0; n  update_or(ll[u], lg - 1, x), update_or(rr[u], lg - 1, x); n  pul(u); n } n  n int query(int u, int lg, int l, int r, int ql, int qr) { n  int m; n  n  if (qr <= l || r <= ql || u == 0) n   return 0; n  if (ql <= l && r <= qr) n   return sz[u]; n  pus(u, lg); n  m = (l + r) / 2; n  return query(ll[u], lg - 1, l, m, ql, qr) + query(rr[u], lg - 1, m, r, ql, qr); n } n  n int main() { n  int n, q, i, u; n  n  scanf( %d%d , &n, &q); n  u = 0; n  for (i = 0; i < n; i++) { n   int a; n  n   scanf( %d , &a); n   u = add(u, LG, a); n  } n  while (q--) { n   int type, l, r; n  n   scanf( %d%d%d , &type, &l, &r), r++; n   if (type != 4) { n    int x, v; n  n    scanf( %d , &x); n    split(&u, &v, LG, 0, B, l, r); n    if (type == 1) n     put(v, LG, B - 1), update_or(v, LG, x ^ B - 1), put(v, LG, B - 1); n    else if (type == 2) n     update_or(v, LG, x); n    else n     put(v, LG, x); n    merge(&u, &v, LG); n   } else n    printf( %d n , query(u, LG, 0, B, l, r)); n  } n  return 0; n }


#include <bits/stdc++.h> n using namespace std; n long long mod = 1e9 + 7; n long long fpo(long long x, long long y); n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   ; n   int t; n   cin >> t; n   while (t--) { n     int n; n     char s[1000]; n     cin >> n >> s; n     if (n > 2) { n       cout <<  YES  n            <<   n ; n       cout << 2 <<   n ; n       cout << s[0] <<    ; n       for (int i = 1; i < n; i++) { n         cout << s[i]; n       } n     } else { n       if (s[0] < s[1]) { n         cout <<  YES  n              <<   n ; n         cout << 2 <<   n ; n         cout << s[0] <<     << s[1]; n       } else n         cout <<  NO  n              <<    ; n     } n     cout <<    n          <<   n ; n   } n   return 0; n } n long long fpo(long long x, long long y) { n   if (y == 0) n     return 1; n   else { n     long long tem = fpo(x, y / 2); n     if (y % 2 == 0) n       return (tem * tem) % mod; n     else n       return (((tem * tem) % mod) * x) % mod; n   } 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, f = 0, g = 0; n     cin >> n; n     long long int a[n + 1]; n     vector<long long int> v; n     for (long long int i = 0; i < n; i++) { n       cin >> a[i]; n       if (a[i] == 1 and g == 0) n         f++; n       else n         g++; n     } n     if (g == 0) { n       if (f % 2) n         cout <<  First n ; n       else n         cout <<  Second n ; n     } else { n       if (f % 2) n         cout <<  Second n ; n       else n         cout <<  First n ; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   long long a[3], d, ans = 0; n   cin >> a[0] >> a[1] >> a[2] >> d; n   sort(a, a + 3); n   if (a[1] - a[0] < d) ans += (d - (a[1] - a[0])); n   if (a[2] - a[1] < d) ans += (d - (a[2] - a[1])); n   cout << ans <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n int Ask(string s) { n   cout << s <<   n ; n   cout.flush(); n   int diff; n   cin >> diff; n   if (diff == 0) exit(0); n   return diff; n } n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   if (fopen( input.txt ,  r )) { n     freopen( input.txt ,  r , stdin); n     freopen( output.txt ,  w , stdout); n   } n   int n = Ask( b ) + 1; n   if (n > 300) { n     string s =   ; n     for (int i = 1; i < (int)n; ++i) s +=  a ; n     Ask(s); n   } n   string s =   ; n   for (int i = 1; i <= (int)n; ++i) s +=  b ; n   int cur = Ask(s); n   if (cur == n) { n     s =   ; n     for (int i = 1; i < (int)n; ++i) s +=  a ; n     int ok = Ask(s); n     assert(ok == 0); n     return 0; n   } n   string ans = s; n   for (int pos = 0; pos < (int)n; ++pos) { n     s = ans; n     s[pos] =  a ; n     int tmp = Ask(s); n     if (tmp < cur) ans[pos] =  a , cur = tmp; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m, a, b; n int tmp[33][33]; n int source[33][33]; n int target[33][33]; n pair<int, int> prv[33][33]; n bool block[33][33]; n vector<pair<int, int> > R; n int dx[] = {-1, -1, -1, 0, 0, 1, 1, 1}; n int dy[] = {-1, 0, 1, -1, 1, -1, 0, 1}; n void save() { n   for (int i = (0); i < (n); ++i) n     for (int j = (0); j < (m); ++j) tmp[i][j] = source[i][j]; n } n void load() { n   R.clear(); n   for (int i = (0); i < (n); ++i) n     for (int j = (0); j < (m); ++j) source[i][j] = tmp[i][j]; n } n void check() { n   for (int i = (0); i < (n); ++i) n     for (int j = (0); j < (m); ++j) n       if (source[i][j] != target[i][j]) return; n   printf( %d n , (int)R.size() - 1); n   for (pair<int, int> r : R) printf( %d %d n , r.first + 1, r.second + 1); n   exit(0); n } n void make(int first, int second) { n   swap(source[R.back().first][R.back().second], source[first][second]); n   R.push_back(pair<int, int>(first, second)); n   a = first; n   b = second; n } n bool in(int first, int second) { n   return 0 <= first && first < n && 0 <= second && second < m && n          !block[first][second]; n } n void path(int first, int second) { n   for (int i = (0); i < (n); ++i) n     for (int j = (0); j < (m); ++j) prv[i][j] = pair<int, int>(-1, -1); n   queue<pair<int, int> > Q; n   Q.push(pair<int, int>(a, b)); n   pair<int, int> c = pair<int, int>(first, second); n   while (!Q.empty()) { n     first = Q.front().first; n     second = Q.front().second; n     Q.pop(); n     for (int d = (0); d < (8); ++d) { n       int nx = first + dx[d]; n       int ny = second + dy[d]; n       if (in(nx, ny) && prv[nx][ny] == pair<int, int>(-1, -1)) { n         prv[nx][ny] = pair<int, int>(first, second); n         Q.push(pair<int, int>(nx, ny)); n       } n     } n   } n   stack<pair<int, int> > S; n   while (c != pair<int, int>(a, b)) { n     S.push(c); n     c = prv[c.first][c.second]; n   } n   while (!S.empty()) { n     make(S.top().first, S.top().second); n     S.pop(); n   } n } n void setp(int i, int j) { n   path(i, j); n   pair<int, int> s = pair<int, int>(i, j); n   block[i][j] = 1; n   for (int p = (0); p < (n); ++p) n     for (int q = (0); q < (m); ++q) n       if (!block[p][q] && source[p][q] == target[i][j]) n         s = pair<int, int>(p, q); n   block[i][j] = 0; n   if (s == pair<int, int>(i, j)) { n     printf( -1 n ); n     exit(0); n   } n   for (;;) { n     path(s.first, s.second); n     s = R[R.size() - 2]; n     if (pair<int, int>(i, j) == s) break; n     block[s.first][s.second] = 1; n     path(i, j); n     block[s.first][s.second] = 0; n   } n   block[i][j] = 1; n } n int main() { n   scanf( %d %d , &n, &m); n   for (int i = (0); i < (n); ++i) n     for (int j = (0); j < (m); ++j) scanf( %d , &source[i][j]); n   for (int i = (0); i < (n); ++i) n     for (int j = (0); j < (m); ++j) scanf( %d , &target[i][j]); n   save(); n   if (n == 1 || m == 1) { n     for (int s = (0); s < (max(n, m)); ++s) { n       for (int d = -1; d <= 1; d += 2) { n         load(); n         R.push_back(pair<int, int>(n == 1 ? 0 : s, n == 1 ? s : 0)); n         for (int i = s + d; - 1 < i && i < max(n, m); i += d) { n           make(n == 1 ? 0 : i, n == 1 ? i : 0); n           check(); n         } n       } n     } n     printf( -1 n ); n     return 0; n   } n   for (int i = (0); i < (n); ++i) n     for (int j = (0); j < (m); ++j) n       if (source[i][j] == target[n - 1][m - 1]) a = i, b = j; n   R.push_back(pair<int, int>(a, b)); n   for (int i = (0); i < (n - 2); ++i) n     for (int j = (0); j < (m); ++j) setp(i, j); n   for (int j = (0); j < (m - 1); ++j) { n     setp(n - 2, j); n     setp(n - 1, j); n   } n   check(); n   if (a == n - 1) n     make(a - 1, b); n   else n     make(a + 1, b); n   check(); n   printf( -1 n ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct elem { n   int le, num, ri; n   elem() {} n   elem(int inle, int innum, int inri) { n     le = inle; n     num = innum; n     ri = inri; n   } n }; n elem operator+(elem e1, elem e2) { n   return elem(e1.le, e1.num + e2.num + (e1.ri and e2.le ? 1 : 0), e2.ri); n } n vector<elem> genera(int n) { n   vector<elem> v; n   for (int i = 0; i + i <= n; i++) v.push_back(elem(0, i, 0)); n   for (int i = 0; i + i + 1 <= n; i++) { n     v.push_back(elem(0, i, 1)); n     v.push_back(elem(1, i, 0)); n   } n   for (int i = 0; i + i + 2 <= n; i++) v.push_back(elem(1, i, 1)); n   return v; n } n bool puede(elem e1, elem e2, int k, int x) { n   for (int i = 3; i <= k; i++) { n     elem nexte1 = e2; n     elem nexte2 = e1 + e2; n     e1 = nexte1; n     e2 = nexte2; n     if (e2.num > x) return false; n   } n   return e2.num == x; n } n void escribe(elem e, int n) { n   if (e.le) cout <<  C ; n   for (int i = 0; i < e.num; i++) cout <<  AC ; n   for (int i = e.le + e.ri + 2 * e.num; i < n; i++) cout <<  B ; n   if (e.ri) cout <<  A ; n   cout << endl; n } n int main() { n   int k, x, n, m; n   cin >> k >> x >> n >> m; n   vector<elem> v1 = genera(n); n   vector<elem> v2 = genera(m); n   for (int i = 0; i < int(v1.size()); i++) { n     for (int j = 0; j < int(v2.size()); j++) { n       if (puede(v1[i], v2[j], k, x)) { n         escribe(v1[i], n); n         escribe(v2[j], m); n         exit(0); n       } n     } n   } n   cout <<  Happy new year!  << endl; n } n


#include <bits/stdc++.h> n using namespace std; n struct rec { n   long long w, h, c; n } a[200010] = {0}; n int n; n long long ans; n vector<long long> p, sum; n double ABS_dec(double x1, double x2) { n   if (x1 < x2) return x2 - x1; n   return x1 - x2; n } n void quick_sort(struct rec a[], long h, long e) { n   long i = h, j = e; n   struct rec val; n   val = a[(h + e) / 2]; n   a[(h + e) / 2] = a[h]; n   do { n     while (i < j && ((val.w < a[j].w) || (val.w == a[j].w && val.h < a[j].h))) n       j--; n     if (i < j) a[i++] = a[j]; n     while (i < j && ((val.w > a[i].w) || (val.w == a[i].w && val.h > a[i].h))) n       i++; n     if (i < j) a[j--] = a[i]; n   } while (i < j); n   a[i] = val; n   if (i - 1 > h) quick_sort(a, h, i - 1); n   if (i + 1 < e) quick_sort(a, i + 1, e); n } n long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; } n void DFS(int i, long long k, long long gh) { n   if (i < p.size()) { n     int j; n     DFS(i + 1, k, gh); n     for (j = 1; j <= sum[i]; j++) { n       k *= p[i]; n       DFS(i + 1, k, gh); n     } n   } else { n     k = a[1].c / k; n     if (gh % k == 0) ans++; n   } n } n bool Is_valid() { n   long long i, j, q; n   long long gw, gh, k; n   double tmp; n   gw = gh = a[1].c; n   ans = 0; n   for (i = 2; i <= n; i++) { n     if (a[i].w != a[i - 1].w) n       break; n     else n       gw = gcd(gw, a[i].c); n   } n   if (i <= n) { n     q = i - 1; n     if (n % q) return false; n     i = 1; n     while (i + q <= n) { n       if (a[i].w == a[i + q].w || a[i].h != a[i + q].h) return false; n       gh = gcd(gh, a[i + q].c); n       for (j = i + 1; j < i + q; j++) { n         if (a[j + q].w != a[j + q - 1].w) return false; n         if (a[j + q].h != a[j].h) return false; n         if (ABS_dec((double)a[j].c / a[j - 1].c, n                     (double)a[j + q].c / a[j + q - 1].c) > 0.0000001) n           return false; n       } n       i += q; n     } n   } n   k = gw; n   for (i = 2; i <= sqrt(k); i++) n     if (k % i == 0) { n       p.push_back(i); n       sum.push_back(0); n       while (k % i == 0) k /= i, sum[sum.size() - 1]++; n     } n   if (k > 1) p.push_back(k), sum.push_back(1); n   DFS(0, 1, gh); n   return true; n } n int main() { n   int i, j, k; n   bool isYES = true; n   scanf( %d , &n); n   for (i = 1; i <= n; i++) { n     scanf( %I64d %I64d %I64d , &a[i].w, &a[i].h, &a[i].c); n   } n   quick_sort(a, 1, n); n   Is_valid(); n   printf( %I64d , ans); n } n


#include <bits/stdc++.h> n using namespace std; n int n, a, b, t, k, x[(int)1e6], i; n int main() { n   cin >> n; n   for (i = 0; i < n; i++) cin >> x[i]; n   sort(x, x + n), n = unique(x, x + n) - x; n   cin >> a >> b; n   while (a > b) { n     k = a - 1; n     for (i = 0; i < n; i++) n       if (a - a % x[i] < b) n         x[i] = x[n - 1], n--, i--; n       else n         k = min(k, a - a % x[i]); n     a = k, t++; n   } n   cout << t; n } n


#include <bits/stdc++.h> n using namespace std; n const int BASE = 70; n long long dataf[BASE][BASE * 2][2]; n int main() { n   long long s; n   scanf( %lld , &s); n   long long ans = 0; n   long long(*f)[BASE * 2][2] = dataf + 1; n   for (int le = 0;; le++) { n     long long t1 = (1LL << (le + 1)); n     if (t1 - 1 > s) break; n     for (int ri = 0;; ri++) { n       long long t2 = (1LL << (ri + 1)); n       if (t1 + t2 - 3 > s) break; n       long long v = s / (t1 + t2 - 3); n       long long tg = s - (v - 1) * (t1 + t2 - 3) + 1; n       int maxbase = max(le + 1, ri + 1); n       int maxc = le + ri; n       for (int sumc = 0; sumc <= maxc; sumc++) n         f[-1][sumc][0] = f[-1][sumc][1] = 0; n       f[-1][0][0] = 1; n       for (int dsumc = 0; dsumc <= maxc; dsumc++) { n         long long tg2 = tg + dsumc + 2; n         if (tg2 & 1) continue; n         tg2 >>= 1; n         tg2 -= (1LL << (le)) + (1LL << (ri)); n         if (tg2 < 0) continue; n         for (int i = -1; i < maxbase; i++) { n           for (int sumc = 0; sumc <= maxc; sumc++) n             f[i + 1][sumc][0] = f[i + 1][sumc][1] = 0; n           for (int sumc = 0; sumc <= maxc; sumc++) n             for (int nho = 0; nho < 2; nho++) { n               if (f[i][sumc][nho] == 0) continue; n               int tgbit = (((tg2) >> (i + 1)) & 1); n               for (int b1 = 0; b1 < 2; b1++) { n                 if (b1 == 1 && i + 1 >= le) continue; n                 if (i + 1 == le - 1 && b1 != 0) continue; n                 for (int b2 = 0; b2 < 2; b2++) { n                   if (b2 == 1 && i + 1 >= ri) continue; n                   if (i + 1 == ri - 1 && b2 != 1) continue; n                   int kq = (b1 + b2 + nho); n                   if ((kq & 1) != tgbit) continue; n                   if (sumc + b1 + b2 > maxc) continue; n                   kq >>= 1; n                   f[i + 1][sumc + b1 + b2][kq] += f[i][sumc][nho]; n                 } n               } n             } n         } n         ans += f[maxbase][dsumc][0]; n       } n     } n   } n   printf( %lld , ans); n } n


#include <bits/stdc++.h> n using namespace std; n vector<vector<long long>> v; n map<int, pair<int, int>> mp; n int main() { n   int k; n   scanf( %i , &k); n   v.resize(k + 1); n   for (int i = 1; i <= k; i++) { n     int x; n     scanf( %i , &x); n     v[i].push_back(0); n     for (int j = 1; j <= x; ++j) { n       int y; n       scanf( %i , &y); n       v[i][0] += y; n       v[i].push_back(y); n     } n   } n   for (int i = 1; i <= k; ++i) { n     for (int j = 1; j < v[i].size(); ++j) { n       long long x = v[i][0] - v[i][j]; n       if (mp[x].first && mp[x].first != i) { n         cout <<  YES  << endl n              << mp[x].first <<     << mp[x].second << endl n              << i <<     << j; n         return 0; n       } else { n         mp[x] = make_pair(i, j); n       } n     } n   } n   printf( NO ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 105; n int main() { n   int n; n   int as[maxn], bs[maxn], cs[maxn]; n   scanf( %d , &n); n   for (int i = 0; i < n; i++) { n     scanf( %d%d%d , &as[i], &bs[i], &cs[i]); n     as[i]--; n     bs[i]--; n   } n   int costA = 0; n   int costB = 0; n   int curEdge = 0; n   int curTown = as[curEdge]; n   for (int i = 0; i < n; i++) { n     int newEdge = -1; n     int newTown = -1; n     for (int j = 0; j < n; j++) { n       if (j == curEdge) continue; n       if (as[j] == curTown) { n         newEdge = j; n         newTown = bs[j]; n         costA += cs[j]; n         break; n       } n       if (bs[j] == curTown) { n         newEdge = j; n         newTown = as[j]; n         costB += cs[j]; n         break; n       } n     } n     curEdge = newEdge; n     curTown = newTown; n   } n   printf( %d n , min(costA, costB)); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 3e5 + 7, INF = 1e9 + 7, LIM = 1e6; n int it[4 * LIM + 7] = {}, lz[4 * LIM + 7] = {}, n, m, a[MAXN], b[MAXN], q; n void lp(int g) { n   if (lz[g]) { n     it[g * 2] += lz[g], it[g * 2 + 1] += lz[g]; n     lz[g * 2] += lz[g], lz[g * 2 + 1] += lz[g]; n     lz[g] = 0; n   } n } n void update(int g, int l, int r, int c, int delta) { n   if (l > c) return; n   if (r <= c) return lz[g] += delta, it[g] += delta, void(); n   lp(g); n   int mid = (l + r) >> 1; n   update(g * 2, l, mid, c, delta), update(g * 2 + 1, mid + 1, r, c, delta); n   it[g] = min(it[g * 2], it[g * 2 + 1]); n   return; n } n int query(int g, int l, int r, int d) { n   if (r < d) return INF; n   if (d <= l) return it[g]; n   lp(g); n   int mid = (l + r) >> 1; n   return min(query(g * 2, l, mid, d), query(g * 2 + 1, mid + 1, r, d)); n } n int main() { n   ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); n   cin >> n >> m; n   for (int i = 1; i <= n; ++i) cin >> a[i]; n   for (int i = 1; i <= m; ++i) cin >> b[i]; n   for (int i = 1; i <= n; ++i) update(1, 1, LIM, a[i], -1); n   for (int i = 1; i <= m; ++i) update(1, 1, LIM, b[i], 1); n   cin >> q; n   while (q--) { n     int tp, pos, x; n     cin >> tp >> pos >> x; n     int pre = a[pos], delta = -1; n     if (tp == 2) pre = b[pos], delta = 1; n     update(1, 1, LIM, pre, -delta); n     update(1, 1, LIM, x, delta); n     if (tp == 1) n       a[pos] = x; n     else n       b[pos] = x; n     int l = 1, r = LIM, res = -1; n     while (l <= r) { n       int mid = (l + r) >> 1; n       if (query(1, 1, LIM, mid) < 0) n         res = mid, l = mid + 1; n       else n         r = mid - 1; n     } n     cout << res <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n signed long long tonum(string s) { n   stringstream in(s); n   signed long long x; n   in >> x; n   return x; n } n string tostr(signed long long n) { n   stringstream in; n   in << n; n   string x; n   in >> x; n   return x; n } n signed long long gcd(signed long long a, signed long long b) { n   while (1) { n     a = a % b; n     if (a == 0) return b; n     b = b % a; n     if (b == 0) return a; n   } n } n int main() { n   int N; n   string s; n   cin >> N; n   cin >> s; n   int len = (int)s.size(); n   map<char, int> used; n   int marks = 0; n   for (int i = 0; i < (int)(s.size()); ++i) { n     if (s[i] ==  ?  && s[len - 1 - i] ==  ? ) { n       marks++; n       continue; n     } n     if (s[i] ==  ? ) { n       s[i] = s[len - 1 - i]; n       continue; n     } n     if (s[len - 1 - i] ==  ? ) { n       s[len - 1 - i] = s[i]; n       continue; n     } n     if (s[i] != s[len - 1 - i]) { n       cout <<  IMPOSSIBLE  << endl; n       return 0; n     } n   } n   int mid = (s.size() % 2 == 0) ? (s.size() / 2 - 1) : (s.size() / 2); n   char curr = N - 1 +  a ; n   for (int i = 0; i < (int)(s.size()); ++i) used[s[i]] = 1; n   for (int i = mid; i >= 0; --i) { n     if (s[i] ==  ? ) { n       while (used[curr] == 1) curr--; n       if (curr <  a ) break; n       s[i] = s[len - 1 - i] = curr; n       used[curr] = 1; n     } n   } n   for (int i = 0; i < (int)(s.size()); ++i) n     if (s[i] ==  ? ) s[i] = s[len - 1 - i] =  a ; n   for (int i = 0; i < (int)(s.size()); ++i) used[s[i]] = 1; n   int req = 0, use = 0; n   for (int i = 0; i < (int)(N); ++i) n     if (used[char(i +  a )] == 1) req++; n   for (int i = 0; i < (int)(26); ++i) n     if (used[char(i +  a )] == 1) use++; n   if (req != N || req != use) { n     cout <<  IMPOSSIBLE  << endl; n     return 0; n   } n   cout << s << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   double a, b, c; n   cin >> a >> b >> c; n   if (c == 0 && a != b) { n     cout <<  NO ; n     return 0; n   } n   if (a == b) { n     cout <<  YES ; n     return 0; n   } n   if (a > b && c > 0) { n     cout <<  NO ; n     return 0; n   } n   if (a < b && c < 0) { n     cout <<  NO ; n     return 0; n   } n   double temp = (b - a) / c; n   if (floor(temp) == ceil(temp)) { n     cout <<  YES ; n     return 0; n   } else { n     cout <<  NO ; n     return 0; n   } n } n