#include <bits/stdc++.h> n using namespace std; n using namespace std; n bool onSegment(pair<int, int> p, pair<int, int> q, pair<int, int> r) { n   if (q.first <= max(p.first, r.first) && q.first >= min(p.first, r.first) && n       q.second <= max(p.second, r.second) && n       q.second >= min(p.second, r.second)) n     return true; n   return false; n } n int orientation(pair<int, int> p, pair<int, int> q, pair<int, int> r) { n   int val = (q.second - p.second) * (r.first - q.first) - n             (q.first - p.first) * (r.second - q.second); n   if (val == 0) return 0; n   return (val > 0) ? 1 : 2; n } n bool intersect(pair<int, int> p1, pair<int, int> q1, pair<int, int> p2, n                pair<int, int> q2) { n   int o1 = orientation(p1, q1, p2); n   int o2 = orientation(p1, q1, q2); n   int o3 = orientation(p2, q2, p1); n   int o4 = orientation(p2, q2, q1); n   if (o1 != o2 && o3 != o4) return true; n   if (o1 == 0 && onSegment(p1, p2, q1)) return true; n   if (o2 == 0 && onSegment(p1, q2, q1)) return true; n   if (o3 == 0 && onSegment(p2, p1, q2)) return true; n   if (o4 == 0 && onSegment(p2, q1, q2)) return true; n   return false; n } n float area(int x1, int y1, int x2, int y2, int x3, int y3) { n   return abs((x1 * (y2 - y3) + x2 * (y3 - y1) + x3 * (y1 - y2)) / 2.0); n } n bool check(int x1, int y1, int x2, int y2, int x3, int y3, int x4, int y4, n            int x, int y) { n   float A = area(x1, y1, x2, y2, x3, y3) + area(x1, y1, x4, y4, x3, y3); n   float A1 = area(x, y, x1, y1, x2, y2); n   float A2 = area(x, y, x2, y2, x3, y3); n   float A3 = area(x, y, x3, y3, x4, y4); n   float A4 = area(x, y, x1, y1, x4, y4); n   return (A == A1 + A2 + A3 + A4); n } n int main() { n   int a, b, c = 0, c1 = 0; n   vector<pair<int, int> > v1, v2; n   for (int i = 0; i < 4; i++) { n     cin >> a >> b; n     v1.push_back(pair<int, int>(a, b)); n   } n   for (int i = 0; i < 4; i++) { n     cin >> a >> b; n     v2.push_back(pair<int, int>(a, b)); n   } n   for (int i = 0; i < 4; i++) { n     if (check(v1[0].first, v1[0].second, v1[1].first, v1[1].second, v1[2].first, n               v1[2].second, v1[3].first, v1[3].second, v2[i].first, n               v2[i].second)) n       c++; n   } n   for (int i = 0; i < 4; i++) { n     if (check(v2[0].first, v2[0].second, v2[1].first, v2[1].second, v2[2].first, n               v2[2].second, v2[3].first, v2[3].second, v1[i].first, n               v1[i].second)) n       c1++; n   } n   if (c == 4 || c1 == 4) { n     cout <<  YES ; n   } else { n     c = 0; n     for (int i = 0; i < 4; i++) { n       for (int j = 0; j < 4; j++) { n         if (intersect(v1[i], v1[(i + 1) % 4], v2[j], v2[(j + 1) % 4])) c++; n       } n     } n     if (c != 0) n       cout <<  YES ; n     else n       cout <<  NO ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 100010; n const int maxm = 100010; n struct Arc { n   int to, next; n } arcs[maxm]; n int head[maxn], ne, out[maxn], ans[maxn]; n inline void add_arc(int u, int v); n priority_queue<int> Q; n int main() { n   int n, m, i, u, v, k; n   while (scanf( %d%d , &n, &m) > 0) { n     memset(head, -1, sizeof(int) * (n + 1)); n     ne = 0; n     memset(out, 0, sizeof(int) * (n + 1)); n     for (i = 0; i < m; i++) { n       scanf( %d%d , &u, &v); n       add_arc(v, u); n       out[u]++; n     } n     while (!Q.empty()) Q.pop(); n     for (i = 1; i <= n; i++) { n       if (out[i] == 0) Q.push(i); n     } n     for (k = n; k >= 1; k--) { n       u = Q.top(); n       Q.pop(); n       ans[u] = k; n       for (i = head[u]; i != -1; i = arcs[i].next) { n         v = arcs[i].to; n         if (--out[v] == 0) Q.push(v); n       } n     } n     for (i = 1; i <= n; i++) printf( %d  , ans[i]); n     printf(  n ); n   } n   return 0; n } n inline void add_arc(int u, int v) { n   arcs[ne].to = v; n   arcs[ne].next = head[u]; n   head[u] = ne++; n   return; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int z[6] = {1, 0, 0, 1, 2, 2}; n   int r[6] = {2, 2, 1, 0, 0, 1}; n   int l[6] = {0, 1, 2, 2, 1, 0}; n   long long int m, n, num; n   while (cin >> m >> n) { n     num = m % 6; n     if (z[num] == n) { n       cout << 1 << endl; n     } else if (r[num] == n) { n       cout << 2 << endl; n     } else { n       cout << 0 << endl; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MaxN = 1005; n const int oo = 1000000000; n const long long Base = 1000000007; n typedef int Tarr[MaxN]; n long long x, y, a, b, n, m, xx, yy, r1, r2, r3, r4; n void Ok(int Val) { n   int xx = Val * a, yy = Val * b; n   if (xx % 2 == 0) { n     r1 = max((long long)0, min(n - xx, x - xx / 2)); n   } else n     r1 = max((long long)0, min(n - xx, x - xx / 2 - 1)); n   if (yy % 2 == 0) { n     r2 = max((long long)0, min(m - yy, y - yy / 2)); n   } else n     r2 = max((long long)0, min(m - yy, y - yy / 2 - 1)); n   r3 = r1 + xx; n   r4 = r2 + yy; n } n int main() { n   cin >> n >> m >> x >> y >> a >> b; n   int aa = a, bb = b, tmp; n   do { n     tmp = aa % bb; n     aa = bb; n     bb = tmp; n   } while (bb != 0); n   a /= aa; n   b /= aa; n   Ok(min((n) / a, (m) / b)); n   cout << r1 <<     << r2 <<     << r3 <<     << r4; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 100 * 1000 + 13; n bool flagBlack = false, flagWhite = false; n int a[N]; n int n; n int lacesB[N]; n int lacesW[N]; n inline void read() { n   cin >> n; n   for (int i = 0; i < n; i++) { n     cin >> a[i]; n     if (a[i] == 0) { n       flagBlack = true; n     } else { n       flagWhite = true; n     } n   } n } n inline void solve() { n   if (!flagBlack || !flagWhite) { n     cout <<  YES  << endl; n     return; n   } n   int mincnt = 100 * 1000 + 13; n   int maxcnt = -100 * 1000 + 13; n   int cnt = 0; n   for (int i = 0; i < n; i++) { n     if (i > 0) { n       if (a[i] != a[i - 1]) { n         if (cnt > maxcnt) { n           maxcnt = cnt; n         } n         if (cnt < mincnt) { n           mincnt = cnt; n         } n         cnt = 0; n       } n     } n     cnt++; n   } n   if (cnt > maxcnt) { n     maxcnt = cnt; n   } n   if (cnt < mincnt) { n     mincnt = cnt; n   } n   if (mincnt == maxcnt) { n     cout <<  YES  << endl; n   } else { n     cout <<  NO  << endl; n   } n } n int main() { n   read(); n   solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long INF = 1e18; n const long long MAXN = 1e2 + 20; n long long n, w, ans[MAXN]; n pair<long long, long long> a[MAXN]; n long long sa(long long n, long long k) { n   if (n % k) return n / k + 1; n   return n / k; n } n void fail() { n   cout << -1; n   exit(0); n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   cin >> n >> w; n   long long kol = 0; n   for (long long i = 0; i < n; i++) { n     cin >> a[i].first; n     a[i].second = i; n     kol += sa(a[i].first, 2); n   } n   if (kol > w) fail(); n   sort(a, a + n); n   for (long long i = 0; i < n - 1; i++) { n     ans[a[i].second] = sa(a[i].first, 2); n     w -= ans[a[i].second]; n   } n   if (w > a[n - 1].first) { n     w -= a[n - 1].first; n     ans[a[n - 1].second] = a[n - 1].first; n     for (long long i = n - 2; i >= 0; i--) { n       if (w > a[i].first - ans[a[i].second]) { n         w -= a[i].first - ans[a[i].second]; n         ans[a[i].second] = a[i].first; n       } else { n         ans[a[i].second] += w; n         break; n       } n     } n   } else { n     ans[a[n - 1].second] = w; n   } n   for (long long i = 0; i < n; i++) cout << ans[i] <<    ; n } n


#include <bits/stdc++.h> n using namespace std; n int n, P, a[150005], lazy_tag[600005], m; n int read() { n   char x = getchar(); n   int ans = 0; n   while (!isdigit(x)) x = getchar(); n   while (isdigit(x)) ans = ans * 10 + x -  0 , x = getchar(); n   return ans; n } n struct node { n   vector<pair<int, int> > V; n   void clear() { V.clear(); } n   int size() { return (int)V.size(); } n   void push_back(pair<int, int> x) { V.push_back(x); } n   node operator+(node x) { n     if (V.empty()) return x; n     node ans = *this; n     for (int i = 0; i < x.size(); i++) { n       bool flag = 0; n       for (int j = 0; j < ans.V.size(); j++) n         if (ans.V[j].first == x.V[i].first) { n           flag = 1, ans.V[j].second += x.V[i].second; n           break; n         } n       if (flag) continue; n       ans.V.push_back(x.V[i]); n       if (ans.V.size() <= P) continue; n       int minn = 1e9; n       for (int j = 0; j < ans.V.size(); j++) minn = min(minn, ans.V[j].second); n       vector<pair<int, int> > tmp; n       for (int j = 0; j < ans.V.size(); j++) n         if (ans.V[j].second - minn) n           tmp.push_back(make_pair(ans.V[j].first, ans.V[j].second - minn)); n       ans.V = tmp; n     } n     return ans; n   } n } tree[600005]; n int ls(int p) { return p << 1; } n int rs(int p) { return p << 1 | 1; } n void pushup(int p) { tree[p] = tree[ls(p)] + tree[rs(p)]; } n void build(int l, int r, int p) { n   if (l == r) { n     tree[p].push_back(make_pair(a[l], 1)); n     return; n   } n   int mid = (l + r) >> 1; n   build(l, mid, ls(p)); n   build(mid + 1, r, rs(p)); n   pushup(p); n } n void pushdown(int p, int l, int r) { n   if (!lazy_tag[p]) return; n   lazy_tag[ls(p)] = lazy_tag[rs(p)] = lazy_tag[p]; n   tree[ls(p)].clear(), tree[rs(p)].clear(); n   int mid = (l + r) >> 1; n   tree[ls(p)].push_back(make_pair(lazy_tag[p], mid - l + 1)); n   tree[rs(p)].push_back(make_pair(lazy_tag[p], r - mid)); n   lazy_tag[p] = 0; n } n void update(int l, int r, int ll, int rr, int p, int val) { n   if (ll <= l && r <= rr) { n     tree[p].clear(); n     lazy_tag[p] = val; n     tree[p].push_back(make_pair(val, r - l + 1)); n     return; n   } n   int mid = (l + r) >> 1; n   pushdown(p, l, r); n   if (ll <= mid) update(l, mid, ll, rr, ls(p), val); n   if (rr > mid) update(mid + 1, r, ll, rr, rs(p), val); n   pushup(p); n } n node query(int l, int r, int ll, int rr, int p) { n   if (ll <= l && r <= rr) return tree[p]; n   node ans; n   int mid = (l + r) >> 1; n   pushdown(p, l, r); n   if (ll <= mid) ans = ans + query(l, mid, ll, rr, ls(p)); n   if (rr > mid) ans = ans + query(mid + 1, r, ll, rr, rs(p)); n   return ans; n } n int main() { n   n = read(), m = read(), P = read(); n   P = 100 / P; n   for (int i = 1; i <= n; i++) a[i] = read(); n   build(1, n, 1); n   for (int i = 1; i <= m; i++) { n     int opt; n     opt = read(); n     if (opt == 1) { n       int l, r, id; n       l = read(), r = read(), id = read(); n       update(1, n, l, r, 1, id); n     } else { n       int l, r; n       l = read(), r = read(); n       node ans = query(1, n, l, r, 1); n       printf( %d , (int)ans.size()); n       for (int i = 0; i < ans.size(); i++) printf(  %d , ans.V[i]); n       puts(  ); n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n typedef vector<long long int> vi; n typedef vector<vi> vvi; n typedef vector<pair<long long int, long long int> > vii; n typedef vector<vii> vvii; n typedef vector<bool> vb; n const long long int dx4[] = {1, 0, -1, 0}, dy4[] = {0, -1, 0, 1}; n const long long int dx8[] = {0, 0, 1, 1, 1, -1, -1, -1}, n                     dy8[] = {1, -1, 1, 0, -1, 1, -0, -1}; n long double dist(long long int a1, long long int b1, long long int a2, n                  long long int b2) { n   return sqrt(((a2 - a1) * (a2 - a1)) + ((b2 - b1) * (b2 - b1))); n } n signed main() { n   long double r, x1, y1, x2, y2, rap, xap, yap, d, cosine, sine; n   cin >> r >> x1 >> y1 >> x2 >> y2; n   d = dist(x1, y1, x2, y2); n   if (d > r) { n     rap = r; n     xap = x1; n     yap = y1; n   } else { n     rap = (r + d) / (long double)(2); n     if (d == 0) { n       xap = x1 + r / (long double)(2); n       yap = y1; n     } else { n       cosine = (x1 - x2) / d; n       sine = (y1 - y2) / d; n       xap = x2 + (rap * cosine); n       yap = y2 + (rap * sine); n     } n   } n   cout << setprecision(100) << xap <<     << yap <<     << rap << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   pair<long long, long long> a[1001]; n   a[0].second = 1; n   for (long long i = 1; i < 1001; i++) { n     a[i].first = i; n     if (i < 10) n       a[i].second = 1; n     else if (i < 100) n       a[i].second = 2; n     else if (i < 1000) n       a[i].second = 3; n   } n   long long n; n   cin >> n; n   long long count = 1; n   long long i = 1; n   while (count < n) { n     count += a[i].second; n     i++; n   } n   if (count == n) { n     long long temp = i; n     long long ans; n     while (temp != 0) { n       ans = temp % 10; n       temp = temp / 10; n     } n     cout << ans << endl; n   } else if (count > n) { n     i--; n     long long temp = a[i].first; n     long long flag = count - n - 1; n     while (flag > 0) { n       temp = temp / 10; n       flag--; n     } n     cout << temp % 10 << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int t; n   cin >> t; n   string s[t]; n   for (int i = 0; i < t; i++) { n     cin >> s[i]; n   } n   map<string, int> m; n   for (int j = 0; j < t; j++) { n     for (int l = 1; l <= 9; l++) { n       for (int i = 0; i < 10 - l; i++) { n         string p = s[j].substr(i, l); n         if (m.find(p) != m.end()) { n           if (m[p] != j) { n             m[p] = -1; n           } n         } else { n           m[p] = j; n         } n       } n     } n   } n   string ans[t]; n   for (int i = 0; i < t; i++) { n     ans[i] =  a ; n   } n   for (auto it = m.begin(); it != m.end(); it++) { n     int x = it->second; n     string y = it->first; n     if (x == -1) { n       continue; n     } n     if (ans[x] ==  a  || ans[x].size() > y.size()) { n       ans[x] = y; n     } n   } n   for (int i = 0; i < t; i++) { n     cout << ans[i] << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n long long ceil(long long a, long long b) { n   long long res = a / b; n   if (a % b != 0) res++; n   return res; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(nullptr); n   int n, m, q; n   cin >> n >> m >> q; n   vector<int> a(n), b(m); n   long long total = 0; n   for (int i = 0; i < n; i++) { n     cin >> a[i]; n     if (i % 2 == 0) { n       total += a[i]; n     } else { n       total -= a[i]; n     } n   } n   for (int i = 0; i < m; i++) { n     cin >> b[i]; n   } n   set<long long> pos_sum; n   long long sum_even = 0, sum_odd = 0; n   for (int i = 0; i < n; i++) { n     if (i % 2 == 0) { n       sum_even += b[i]; n     } else { n       sum_odd += b[i]; n     } n   } n   for (int j = 0; j < m - n + 1; j++) { n     if (j != 0) { n       if ((j - 1) % 2 == 0) { n         sum_even -= b[j - 1]; n       } else { n         sum_odd -= b[j - 1]; n       } n       if ((j + n - 1) % 2 == 0) { n         sum_even += b[j + n - 1]; n       } else { n         sum_odd += b[j + n - 1]; n       } n     } n     if (j % 2 == 0) { n       pos_sum.insert(-sum_odd + sum_even); n     } else { n       pos_sum.insert(-sum_even + sum_odd); n     } n   } n   auto find = [&]() { n     auto ub = pos_sum.upper_bound(total); n     long long res = LONG_LONG_MAX; n     if (ub != pos_sum.end()) { n       res = min(res, abs(total - *ub)); n     } n     if (ub != pos_sum.begin()) { n       ub--; n       res = min(res, abs(total - *ub)); n     } n     return res; n   }; n   cout << find() <<   n ; n   while (q--) { n     int l, r, x; n     cin >> l >> r >> x; n     l--, r--; n     long long len = r - l + 1; n     if (l % 2 == 0) { n       total += ceil(len, 2) * x; n       total -= (len / 2) * x; n     } else { n       total -= ceil(len, 2) * x; n       total += (len / 2) * x; n     } n     cout << find() <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   string k; n   cin >> k; n   int st = 0; n   while (st < (int)k.length() && k[st] ==  a  && 1) { n     st++; n   } n   if (st == (int)k.length()) { n     k[k.length() - 1] =  z ; n     cout << k << endl; n     return 0; n   } n   int se = st + 1; n   while (se < (int)k.length() && k[se] !=  a  && 1) { n     se++; n   } n   se--; n   for (int i = st; i <= se; i++) { n     k[i]--; n   } n   cout << k << endl; n } n


#include <bits/stdc++.h> n using namespace std; n void solve() { n   int n, x, y; n   cin >> n >> x >> y; n   vector<int> nums(n); n   vector<int> a(n); n   vector<vector<int>> freq(n + 1); n   vector<vector<int>> ind(n + 1 + 1); n   vector<bool> mis(n); n   for (int i = 0; i < n; i++) { n     cin >> nums[i]; n     mis[i] = false; n     ind[nums[i]].push_back(i); n   } n   for (int i = 1; i <= n + 1; i++) { n     freq[ind[i].size()].push_back(i); n   } n   int f = n; n   for (int k = 0; k < x; k++) { n     while (freq[f].empty()) { n       f--; n     } n     int col = freq[f].back(); n     int idx = ind[col].back(); n     a[idx] = col; n     ind[col].pop_back(); n     freq[f].pop_back(); n     freq[f - 1].push_back(col); n   } n   while (f > 0 && freq[f].empty()) f--; n   if (2 * f > 2 * n - x - y) { n     cout <<  NO n ; n     return; n   } n   vector<int> ve; n   for (int i = 1; i <= f; i++) { n     for (int color : freq[i]) { n       ve.insert(ve.end(), ind[color].begin(), ind[color].end()); n     } n   } n   int mismatch = n - y; n   auto makemismatch = [&](int i) { n     a[i] = freq[0][0]; n     mismatch--; n     mis[i] = true; n   }; n   for (int i = 0; i < n - x; i++) { n     a[ve[i]] = nums[ve[(i + (n - x) / 2) % (n - x)]]; n     if (a[ve[i]] == nums[ve[i]]) { n       makemismatch(ve[i]); n     } n   } n   for (int i = 0; mismatch > 0; i++) { n     if (!mis[ve[i]]) makemismatch(ve[i]); n   } n   cout <<  YES n ; n   for (int i = 0; i < n; i++) { n     cout << a[i] <<    ; n   } n   cout <<   n ; n } n int main() { n   int t; n   cin >> t; n   while (t--) { n     solve(); n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   int n; n   cin >> n; n   n--; n   int a[n]; n   for (int i = 0; i < n; ++i) { n     cin >> a[i]; n     if (a[i] == 0) --i; n   } n   int principi; n   int b[n]; n   for (int i = 0; i < n; ++i) { n     cin >> b[i]; n     if (b[i] == 0) --i; n     if (b[i] == a[0]) principi = i; n   } n   bool bo = true; n   for (int i = 1; bo and i < n; ++i) { n     if (a[i] != b[(principi + i) % n]) bo = false; n   } n   if (bo) n     cout <<  YES  << endl; n   else n     cout <<  NO  << endl; n } n


#include <bits/stdc++.h> n using namespace std; n struct edge { n   int x, y, z, id; n } e[200005]; n vector<int> to[200005]; n int fa[200005], f[200005][18], deep[200005], g[200005][18], id[200005], n     ans[200005]; n bool flag[200005]; n int find(int x) { return fa[x] == x ? x : fa[x] = find(fa[x]); } n void dfs(int x, int fa) { n   f[x][0] = fa, deep[x] = deep[fa] + 1; n   for (auto tar : to[x]) n     if (tar != fa) dfs(tar, x); n } n pair<int, int> lca(int x, int y) { n   int res = 0; n   if (deep[x] < deep[y]) swap(x, y); n   for (int k = 17; k >= 0; k--) n     if (deep[f[x][k]] >= deep[y]) res = max(res, g[x][k]), x = f[x][k]; n   if (x == y) return {x, res}; n   for (int k = 17; k >= 0; k--) n     if (f[x][k] != f[y][k]) n       res = max(res, max(g[x][k], g[y][k])), x = f[x][k], y = f[y][k]; n   res = max(res, max(g[x][0], g[y][0])); n   return {f[x][0], res}; n } n void merge(int from, int to, int v) { n   from = find(from); n   while (deep[from] > deep[to]) { n     ans[id[from]] = v; n     fa[from] = find(f[from][0]); n     from = find(from); n   } n } n int main() { n   int n, m; n   scanf( %d%d , &n, &m); n   for (int i = 1; i <= m; i++) n     scanf( %d%d%d , &e[i].x, &e[i].y, &e[i].z), e[i].id = i; n   sort(e + 1, e + m + 1, [&](edge a, edge b) { return a.z < b.z; }); n   for (int i = 1; i <= n; i++) fa[i] = i; n   for (int i = 1; i <= m; i++) { n     int fx = find(e[i].x), fy = find(e[i].y); n     if (fx != fy) n       to[e[i].x].push_back(e[i].y), to[e[i].y].push_back(e[i].x), fa[fx] = fy; n     else n       flag[i] = 1; n   } n   dfs(1, 0); n   for (int i = 1; i <= m; i++) n     if (!flag[i]) n       g[deep[e[i].x] < deep[e[i].y] ? e[i].y : e[i].x] n        [0] = e[i].z, n           id[deep[e[i].x] < deep[e[i].y] ? e[i].y : e[i].x] = e[i].id; n   for (int k = 1; k <= 17; k++) n     for (int i = 1; i <= n; i++) n       f[i][k] = f[f[i][k - 1]][k - 1], n       g[i][k] = max(g[i][k - 1], g[f[i][k - 1]][k - 1]); n   for (int i = 1; i <= n; i++) fa[i] = i; n   for (int i = 1; i <= m; i++) n     if (flag[i]) { n       auto g = lca(e[i].x, e[i].y); n       ans[e[i].id] = g.second; n       merge(e[i].x, g.first, e[i].z), merge(e[i].y, g.first, e[i].z); n     } n   for (int i = 1; i <= m; i++) printf( %d  , ans[i] ? ans[i] - 1 : -1); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct edge { n   int to, next; n } e[400005]; n int n, k, num, head[200005], a[200005], f[200005], g[200005], h[200005], n     g_node[200005], ans, maxa, size[200005]; n bool ok[200005]; n inline void insert(int u, int v) { n   e[++num].to = v; n   e[num].next = head[u]; n   head[u] = num; n   e[++num].to = u; n   e[num].next = head[v]; n   head[v] = num; n } n void dfs(int now, int fa) { n   size[now] = 1; n   int s = 0, maxc = 0, secc = 0, id = 0; n   for (int i = head[now]; i; i = e[i].next) n     if (e[i].to != fa) { n       dfs(e[i].to, now); n       size[now] += size[e[i].to]; n       if (ok[e[i].to]) { n         int ch = f[e[i].to] + g[e[i].to] + 1; n         if (ch == size[e[i].to]) n           s += ch; n         else if (ch > maxc) n           secc = maxc, maxc = ch, id = e[i].to; n         else if (ch > secc) n           secc = ch; n       } n     } n   if (ok[now]) { n     f[now] = s; n     g[now] = maxc; n     h[now] = secc; n     g_node[now] = id; n   } n } n void work(int now, int fa, int out) { n   if (ok[now]) { n     if (out == n - size[now]) n       f[now] += out; n     else { n       if (out > g[now]) n         h[now] = g[now], g[now] = out, g_node[now] = fa; n       else if (out > h[now]) n         h[now] = out; n     } n     num = max(num, f[now] + g[now] + 1); n     if (num >= k) return; n   } n   for (int i = head[now]; i; i = e[i].next) n     if (e[i].to != fa) { n       if (!ok[now]) n         work(e[i].to, now, 0); n       else { n         if (ok[e[i].to] && f[e[i].to] + g[e[i].to] + 1 == size[e[i].to]) n           work(e[i].to, now, f[now] - size[e[i].to] + g[now] + 1); n         else if (e[i].to == g_node[now]) n           work(e[i].to, now, f[now] + h[now] + 1); n         else n           work(e[i].to, now, f[now] + g[now] + 1); n       } n     } n } n inline bool check(int x) { n   for (int i = 1; i <= n; i++) ok[i] = a[i] >= x; n   memset(f, 0, sizeof(f)); n   memset(g, 0, sizeof(g)); n   memset(h, 0, sizeof(h)); n   dfs(1, 0); n   num = 0; n   work(1, 0, 0); n   return num >= k; n } n int main() { n   scanf( %d%d , &n, &k); n   for (int i = 1; i <= n; i++) { n     scanf( %d , &a[i]); n     maxa = max(maxa, a[i]); n   } n   for (int i = 1; i < n; i++) { n     int u, v; n     scanf( %d%d , &u, &v); n     insert(u, v); n   } n   int l = 1, r = maxa; n   while (l <= r) { n     int mid = (l + r) >> 1; n     if (check(mid)) n       ans = mid, l = mid + 1; n     else n       r = mid - 1; n   } n   printf( %d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const double Pi = acos(-1.0); n void DEBUG(string name, int* begin, int* end) { n   printf( DEBUG: %s = { , name.c_str()); n   for (; begin != end; begin++) { n     printf( %5d, , *begin); n   } n   printf( %c} n , (char)8); n } n void DEBUG(string name, long long* begin, long long* end) { n   printf( DEBUG: %s = { , name.c_str()); n   for (; begin != end; begin++) { n     printf( %5lld, , *begin); n   } n   printf( %c} n , (char)8); n } n void DEBUG(string name, double* begin, double* end) { n   printf( DEBUG: %s = { , name.c_str()); n   for (; begin != end; begin++) { n     printf( %5lf, , *begin); n   } n   printf( %c} n , (char)8); n } n void DEBUG(string name, vector<int> nums) { n   printf( DEBUG: %s = { , name.c_str()); n   for (int i : nums) { n     printf( %5d, , i); n   } n   printf( %c} n , (char)8); n } n void DEBUG(string name, set<int> nums) { n   printf( DEBUG: %s = { , name.c_str()); n   for (int i : nums) { n     printf( %5d, , i); n   } n   printf( %c} n , (char)8); n } n void DEBUG(string name, vector<long long> nums) { n   printf( DEBUG: %s = { , name.c_str()); n   for (int i : nums) { n     printf( %5lld, , i); n   } n   printf( %c} n , (char)8); n } n void DEBUG(string name, set<long long> nums) { n   printf( DEBUG: %s = { , name.c_str()); n   for (int i : nums) { n     printf( %5lld, , i); n   } n   printf( %c} n , (char)8); n } n void DEBUG(string name, map<int, int> nums) { n   printf( DEBUG: %s = { , name.c_str()); n   for (auto i : nums) { n     printf( (%d -> %d), , i.first, i.second); n   } n   printf( %c} n , (char)8); n } n void DEBUG(string name, queue<int> nums) { n   if (nums.size() == 0) { n     printf( DEBUG: %s = [] n , name.c_str()); n     return; n   } n   printf( DEBUG: %s = [ , name.c_str()); n   while (!nums.empty()) { n     printf( %5d, , nums.front()); n     nums.pop(); n   } n   printf( %c] n , (char)8); n } n void DEBUG(string name, int num) { n   printf( DEBUG: %s = %d n , name.c_str(), num); n } n void DEBUG(string name, long long num) { n   printf( DEBUG: %s = %lld n , name.c_str(), num); n } n void DEBUG(string name, double num) { n   printf( DEBUG: %s = %lf n , name.c_str(), num); n } n int cross(int a1, int b1, int a2, int b2) { return (a1 * b2 - a2 * b1); } n int ccw(pair<int, int> a, pair<int, int> b, pair<int, int> c) { n   return cross(b.first - a.first, b.second - a.second, c.first - a.first, n                c.second - a.second); n } n void hi() { printf( hi n ); } n long long n; n long long times[100010]; n long long t[100010], k[100010]; n vector<pair<int, int> > adj[100010]; n bool visited[100010]; n void dfs(int node) { n   k[node] = 1; n   for (pair<int, int> i : adj[node]) { n     if (!visited[i.first]) { n       visited[i.first] = true; n       dfs(i.first); n       k[node] += k[i.first]; n       t[node] += t[i.first] + 2 * i.second; n     } n   } n } n bool comp(pair<int, int> a, pair<int, int> b) { n   return (t[a.first] + 2 * a.second) * (k[b.first]) < n          (t[b.first] + 2 * b.second) * (k[a.first]); n } n long long curr_time; n void dfs2(int node) { n   times[node] = curr_time; n   for (pair<int, int> i : adj[node]) { n     if (!visited[i.first]) { n       visited[i.first] = true; n       curr_time += i.second; n       dfs2(i.first); n       curr_time += i.second; n     } n   } n } n int main() { n   cin >> n; n   for (int i = 0; i < n - 1; i++) { n     int a, b, c; n     cin >> a >> b >> c; n     a--; n     b--; n     adj[a].push_back(pair<int, int>(b, c)); n     adj[b].push_back(pair<int, int>(a, c)); n   } n   visited[0] = true; n   dfs(0); n   for (int i = 0; i < n; i++) { n     sort(adj[i].begin(), adj[i].end(), comp); n   } n   memset(visited, 0, sizeof(visited)); n   visited[0] = true; n   dfs2(0); n   double sum = 0; n   for (int i = 0; i < n; i++) { n     sum += times[i]; n   } n   printf( %.41lf , sum / (n - 1)); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e6 + 6; n int n, k; n int Pow10[10], F[10]; n long long f[N]; n long long cal(int n) { n   int t = 0; n   long long s = 0; n   while (n > 0) { n     int x = n % 10; n     if (x % 3 == 0) s += 1LL * F[t] * x / 3; n     n /= 10; n     t++; n   } n   return s; n } n int main() { n   ios ::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   if (fopen( main.in ,  r )) freopen( main.in ,  r , stdin); n   cin >> k; n   Pow10[0] = 1; n   for (int i = 0; i < 6; i++) { n     cin >> F[i]; n     if (i) Pow10[i] = Pow10[i - 1] * 10; n   } n   int _, n; n   cin >> _ >> n; n   for (int i = 0; i <= n; i++) f[i] = -1e15; n   f[0] = 0; n   for (int t = 0; t < 6; t++) { n     int s = 0, p = 1; n     while (s + p <= 3 * (k - 1)) { n       s += p; n       for (int i = n; i >= 3LL * p * Pow10[t]; i--) n         f[i] = max(f[i], f[i - 3 * p * Pow10[t]] + 1LL * p * F[t]); n       p *= 2; n     } n     s = 3 * (k - 1) - s; n     for (int i = n; i >= 3LL * s * Pow10[t]; i--) n       f[i] = max(f[i], f[i - 3 * s * Pow10[t]] + 1LL * s * F[t]); n   } n   long long res = 0; n   for (int i = 0; i <= n; i++) { n     res = max(res, cal(n - i) + f[i]); n   } n   cout << res <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n int a, b, c; n int t, x = 0; n int ans = 0; n void find(int x) { n   for (t = x; t <= c + x; t++) n     if (b * x + (b - a) * (t - x) < 0) { n       x++; n       ans++; n       find(x); n       break; n     } n } n int main() { n   scanf( %d%d%d , &a, &b, &c); n   find(x); n   printf( %d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   cin >> n; n   vector<int> a(n); n   for (int i = 0; i < n; ++i) { n     cin >> a[i]; n   } n   sort(a.begin(), a.end()); n   bool ok = true; n   int cur = -1; n   int q = 0; n   for (int i = 0; i < n; ++i) { n     if (q == 0) { n       if (cur != -1 && a[i] != a[i - 1]) { n         ok = false; n         break; n       } n       q = 1; n       cur = a[i]; n     } else { n       if (cur == a[i]) { n         ++q; n       } else { n         if (cur == a[i] - 1) { n           --q; n         } else { n           ok = false; n           break; n         } n       } n     } n   } n   ok &= q == 0; n   if (ok) { n     cout <<  YES n ; n   } else { n     cout <<  NO n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int x; n   cin >> x; n   x = abs(x); n   int pos = 0; n   int i; n   for (i = 1; pos < x; i++) pos += i; n   int diff = pos - x; n   if (diff % 2 == 0) n     cout << (i - 1) << endl; n   else { n     if (i % 2 != 0) n       cout << i << endl; n     else n       cout << i + 1 << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int n; n int node[101], h[101], perm[101], fav[101]; n int getP(int c) { n   int p = node[c]; n   if (p < 0) return c; n   while (node[p] > 0) p = node[p]; n   return node[c] = p; n } n void join(int a, int b) { n   if (0) printf( join %d %d n , a, b); n   int pa = getP(a), pb = getP(b); n   if (pa == pb) { n     return; n   } n   if (h[pa] < h[pb]) { n     node[pa] = pb; n     h[pb] += h[pa]; n   } else { n     node[pb] = pa; n     h[pa] += h[pb]; n   } n } n void getNum() { n   int i; n   for (i = 0; i <= n; i++) { n     node[i] = -1; n     h[i] = 1; n   } n   for (i = 1; i <= n; i++) { n     if (i + fav[i] <= n) join(i, i + fav[i]); n     if (i - fav[i] > 0) join(i, i - fav[i]); n   } n   for (i = 1; i <= n; i++) n     if (getP(i) != getP(perm[i])) { n       if (0) printf( %d != %d n , i, perm[i]); n       printf( NO ); n       return; n     } n   printf( YES ); n   return; n } n int main() { n   int a, b, c; n   scanf( %d , &n); n   for (int i = 1; i <= n; i++) { n     scanf( %d , &perm[i]); n   } n   for (int i = 1; i <= n; i++) scanf( %d , &fav[i]); n   getNum(); 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 k = 0; n     string a, b, c; n     cin >> a >> b >> c; n     for (int i = 0; i < a.size(); i++) { n       if (a[i] == c[i] || b[i] == c[i]) n         k++; n       else { n         k = -1; n         break; n       } n     } n     if (k == a.size()) n       cout <<  YES  << endl; n     else n       cout <<  NO  << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e5 + 100, mod = 998244353, inf = 2e9; n int n, p[N], v[N], x[N], cnt, ans; n int qpow(int x, int y) { n   int ret = 1; n   while (y) { n     if (y & 1) ret = 1ll * ret * x % mod; n     x = 1ll * x * x % mod, y >>= 1; n   } n   return ret; n } n struct node { n   int x, y, p, q, tp; n   bool operator<(const node &tp) const { n     return 1ll * p * tp.q < 1ll * q * tp.p; n   } n } res[N * 3]; n struct matrix { n   int s[2][2]; n   void init(int o) { n     if (o == 1) n       for (int i = 0; i < 2; ++i) n         for (int j = 0; j < 2; ++j) s[i][j] = (i == j); n     else { n       for (int i = 0; i < 2; ++i) n         for (int j = 0; j < 2; ++j) s[i][j] = 0; n       s[0][0] = 1; n     } n   } n   matrix operator*(matrix b) { n     matrix ret; n     for (int i = 0; i < 2; ++i) n       for (int j = 0; j < 2; ++j) { n         ret.s[i][j] = 0; n         for (int k = 0; k < 2; ++k) n           ret.s[i][j] = (ret.s[i][j] + 1ll * s[i][k] * b.s[k][j]) % mod; n       } n     return ret; n   } n } trans[N], I, H; n struct SegmentTree { n   int L[N << 2], R[N << 2]; n   matrix F[N << 2]; n   void build(int k, int l, int r) { n     L[k] = l, R[k] = r; n     if (l == r) { n       F[k] = trans[l]; n       return; n     } n     int mid = l + r >> 1; n     build(k << 1, l, mid); n     build(k << 1 | 1, mid + 1, r); n     F[k] = F[k << 1] * F[k << 1 | 1]; n   } n   matrix Query(int k, int l, int r) { n     if (r < l) return I; n     if (l <= L[k] && R[k] <= r) { n       return F[k]; n     } n     int mid = L[k] + R[k] >> 1; n     matrix ret = I; n     if (l <= mid) ret = Query(k << 1, l, r); n     if (r > mid) ret = ret * Query(k << 1 | 1, l, r); n     return ret; n   } n   void Modify(int k, int x) { n     if (L[k] == R[k]) { n       F[k] = trans[x]; n       return; n     } n     int mid = L[k] + R[k] >> 1; n     if (x <= mid) n       Modify(k << 1, x); n     else n       Modify(k << 1 | 1, x); n     F[k] = F[k << 1] * F[k << 1 | 1]; n   } n } Tr; n int main() { n   scanf( %d , &n); n   for (int i = 1, c = qpow(100, mod - 2); i <= n; ++i) n     scanf( %d%d%d , &x[i], &v[i], &p[i]), p[i] = mod + 1 - 1ll * c * p[i] % mod, n                                           p[i] %= mod; n   if (n == 1) { n     puts( 0 ); n     return 0; n   } n   for (int i = 2; i <= n; ++i) { n     if (v[i] > v[i - 1]) n       res[++cnt] = (node){i - 1, i, x[i] - x[i - 1], v[i] - v[i - 1], 0}; n     res[++cnt] = (node){i - 1, i, x[i] - x[i - 1], v[i] + v[i - 1], 1}; n     if (v[i - 1] > v[i]) n       res[++cnt] = (node){i - 1, i, x[i] - x[i - 1], v[i - 1] - v[i], 2}; n   } n   sort(res + 1, res + cnt + 1); n   I.init(1); n   H.init(2); n   for (int i = 1; i <= n; ++i) n     trans[i].s[0][0] = trans[i].s[1][0] = p[i], n     trans[i].s[0][1] = trans[i].s[1][1] = (mod + 1 - p[i]) % mod; n   Tr.build(1, 1, n); n   for (int i = 1; i <= cnt; ++i) { n     int x = res[i].x, y = res[i].y; n     matrix pre = Tr.Query(1, 1, x), suf = Tr.Query(1, y + 1, n), Y; n     Y.init(2); n     Y.s[0][0] = 0; n     if (res[i].tp == 0) Y.s[0][0] = p[y]; n     if (res[i].tp == 1) Y.s[1][0] = p[y]; n     if (res[i].tp == 2) Y.s[1][1] = (1 + mod - p[y]) % mod; n     matrix tmp = pre * Y * suf; n     ans = (ans + 1ll * (tmp.s[0][1] + tmp.s[0][0]) * res[i].p % mod * n                      qpow(res[i].q, mod - 2) % mod) % n           mod; n     if (res[i].tp == 0) trans[y].s[0][0] = 0; n     if (res[i].tp == 1) trans[y].s[1][0] = 0; n     if (res[i].tp == 2) trans[y].s[1][1] = 0; n     Tr.Modify(1, y); n   } n   printf( %d , ans); n   return 0; n } n


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


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   cin >> n; n   cout << (n - 1) * 2 + 1 <<     << 2 << endl; n   cout << 1 <<     << 2 << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   cin >> n; n   vector<int> vec(n); n   for (int i = 0; i < n; ++i) cin >> vec[i]; n   int sum = accumulate(vec.begin(), vec.end(), 0); n   if ((sum / 100) % 2 != 0) { n     cout <<  NO n ; n     return 0; n   } n   sum /= 2; n   int num_one = count(vec.begin(), vec.end(), 100); n   int num_two = count(vec.begin(), vec.end(), 200); n   int curr = 0; n   while (curr <= sum && num_two > 0) { n     curr += 200; n     num_two--; n   } n   if (curr == sum) { n     cout <<  YES n ; n     return 0; n   } n   if (curr > sum) curr -= 200; n   while (curr != sum && num_one > 0) { n     curr += 100; n     num_one--; n   } n   if (curr == sum) n     cout <<  YES n ; n   else n     cout <<  NO n ; n } n


#include <bits/stdc++.h> n #pragma GCC optimize( O3 ) n using namespace std; n template <typename T> n inline void umax(T &a, T b) { n   a = max(a, b); n } n template <typename T> n inline void umin(T &a, T b) { n   a = min(a, b); n } n const long long N = 1e5 + 10; n const long long INF = 1e16 + 10; n const pair<long long, long long> NINF = make_pair(INF, INF); n const long double EPS = 1e-6; n const long long II = 1e9; n const long long MOD = 1e9 + 7; n long long dp[1 << 22]; n long long cnt[50][50]; n int32_t main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   long long n, m; n   cin >> n >> m; n   string second; n   cin >> second; n   for (long long i = 0; i < (n - 1); i++) { n     cnt[second[i] -  a ][second[i + 1] -  a ]++; n     cnt[second[i + 1] -  a ][second[i] -  a ]++; n   } n   for (long long i = 0; i < (1 << m); i++) dp[i] = INF; n   for (long long i = 0; i < (m); i++) dp[1 << i] = 0; n   for (long long msk = 0; msk < (1 << m); msk++) { n     long long cur = dp[msk]; n     for (long long i = 0; i < (m); i++) n       if (msk & (1 << i)) n         for (long long j = 0; j < (m); j++) n           if ((msk & (1 << j)) == 0) cur += cnt[i][j]; n     for (long long j = 0; j < (m); j++) n       if ((msk & (1 << j)) == 0) umin(dp[msk | (1 << j)], cur); n   } n   cout << dp[(1 << m) - 1]; n } n


#include <bits/stdc++.h> n using namespace std; n bool myfunction(int i, int j) { return (i < j); } n istream &in = cin; n int n; n bool adj[1501][1501]; n int vtx[1501]; n vector<bool> chk; n vector<pair<pair<int, int>, int> > points; n vector<pair<int, int> > res; n int dfs(int node) { n   int cnt = 0; n   chk[node] = true; n   for (int i = 1; i <= n; ++i) n     if (!chk[i] && adj[node][i]) cnt += dfs(i); n   vtx[node] = cnt + 1; n   return vtx[node]; n } n void go(int root, vector<pair<pair<int, int>, int> > ps) { n   int min_y = INT_MAX, min_i = -1; n   for (int i = 1; i <= ps.size() - 1; ++i) n     if (ps[i].first.second < min_y) { n       min_y = ps[i].first.second; n       min_i = i; n     } n   res.push_back(make_pair(ps[min_i].second, root)); n   vector<pair<long double, int> > tps(1); n   for (int i = 1; i <= ps.size() - 1; ++i) n     if (i != min_i) n       tps.push_back( n           make_pair(atan2l(ps[i].first.first - ps[min_i].first.first, n                            ps[i].first.second - ps[min_i].first.second), n                     i)); n   sort(tps.begin() + 1, tps.end()); n   int s = 1; n   for (int i = 1; i <= n; ++i) n     if (adj[root][i]) { n       vector<pair<pair<int, int>, int> > nps(1); n       while (nps.size() < vtx[i] + 1) { n         nps.push_back(ps[tps[s].second]); n         ++s; n       } n       adj[i][root] = false; n       go(i, nps); n     } n } n int main() { n   in >> n; n   for (int i = (0); i < (n + 1); ++i) n     for (int j = (0); j < (n + 1); ++j) adj[i][j] = false; n   points.resize(n + 1); n   chk.resize(n + 1, false); n   memset(vtx, 0, sizeof(vtx)); n   for (int i = (0); i < (n - 1); ++i) { n     int a, b; n     in >> a >> b; n     adj[a][b] = adj[b][a] = true; n   } n   for (int i = (0); i < (n); ++i) { n     int x, y; n     in >> x >> y; n     points[i + 1] = make_pair(make_pair(x, y), i + 1); n   } n   dfs(1); n   sort(points.begin() + 1, points.end()); n   for (int i = (0); i < (n + 1); ++i) chk[i] = false; n   go(1, points); n   sort(res.begin(), res.end()); n   for (int i = 0; i < res.size(); ++i) cout << res[i].second <<    ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 5e3 + 500; n const double eps = 1e-8; n int sgn(double x) { n   if (fabs(x) < eps) return 0; n   if (x < 0) n     return -1; n   else n     return 1; n } n struct Point { n   double x, y; n   Point() {} n   Point(double _x, double _y) : x(_x), y(_y) {} n   void input() { scanf( %lf%lf , &x, &y); } n   void output() { printf( %.0lf %.0lf n , x, y); } n   Point operator-(const Point &b) const { return Point(x - b.x, y - b.y); } n   bool operator==(const Point &b) const { n     return sgn(x - b.x) == 0 && sgn(y - b.y) == 0; n   } n   bool operator<(const Point &b) const { n     return sgn(x - b.x) == 0 ? sgn(y - b.y) < 0 : x < b.x; n   } n   double operator^(const Point &b) const { return x * b.y - y * b.x; } n   double operator*(const Point &b) const { return x * b.x + y * b.y; } n   double distance(Point b) { return hypot(x - b.x, y - b.y); } n }; n struct polygon { n   int n; n   Point p[maxn], p1, p2, p3; n   void input(int _n) { n     n = _n; n     for (int i = 0; i < n; ++i) p[i].input(); n   } n   struct cmp { n     Point p; n     cmp(const Point &p0) { p = p0; } n     bool operator()(const Point &aa, const Point &bb) { n       Point a = aa, b = bb; n       int d = sgn((a - p) ^ (b - p)); n       if (d == 0) return sgn(a.distance(p) - b.distance(p)) < 0; n       return d > 0; n     } n   }; n   void norm() { n     Point mi = p[0]; n     for (int i = 1; i < n; ++i) mi = min(mi, p[i]); n     sort(p, p + n, cmp(mi)); n   } n   void Graham(polygon &convex) { n     norm(); n     int &top = convex.n; n     top = 0; n     if (n == 1) { n       top = 1; n       convex.p[0] = p[0]; n       return; n     } n     if (n == 2) { n       top = 2; n       convex.p[0] = p[0]; n       convex.p[1] = p[1]; n       if (convex.p[0] == convex.p[1]) top--; n       return; n     } n     convex.p[0] = p[0]; n     convex.p[1] = p[1]; n     top = 2; n     for (int i = 2; i < n; ++i) { n       while (top > 1 && sgn((convex.p[top - 1] - convex.p[top - 2]) ^ n                             (p[i] - convex.p[top - 2])) <= 0) n         top--; n       convex.p[top++] = p[i]; n     } n     if (convex.n == 2 && (convex.p[0] == convex.p[1])) convex.n--; n   } n   void rotating_calipers() { n     double ans = 0; n     for (int i = 0; i < n; ++i) { n       int k = i + 1; n       for (int j = i + 1; j < n; ++j) { n         while (sgn(fabs((p[j] - p[i]) ^ (p[(k + 1) % n] - p[i])) - n                    fabs((p[j] - p[i]) ^ (p[k] - p[i]))) > 0) n           k = (k + 1) % n; n         if (ans < 0.5 * fabs((p[j] - p[i]) ^ (p[k] - p[i]))) { n           p1 = p[i]; n           p2 = p[j]; n           p3 = p[k]; n           ans = 0.5 * fabs((p[j] - p[i]) ^ (p[k] - p[i])); n         } n       } n     } n   } n } A, B; n int n; n long long s; n int main() { n   scanf( %d%lld , &n, &s); n   A.input(n); n   A.Graham(B); n   B.rotating_calipers(); n   printf( %.0lf %.0lf n , B.p1.x + B.p2.x - B.p3.x, B.p1.y + B.p2.y - B.p3.y); n   printf( %.0lf %.0lf n , B.p1.x + B.p3.x - B.p2.x, B.p1.y + B.p3.y - B.p2.y); n   printf( %.0lf %.0lf n , B.p3.x + B.p2.x - B.p1.x, B.p3.y + B.p2.y - B.p1.y); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int A[(1 << 17)]; n int A0[(1 << 17)]; n int A1[(1 << 17)]; n int C[(1 << 17)]; n int RMQ[2 << 20]; n int I[32]; n void Add(int a, int b) { n   int i; n   for (i = (0); i < (20); ++i) RMQ[I[i] + (a >> i)] += b; n } n int Sum(int a) { n   int res = 0; n   int i; n   for (i = (0); i < (20); ++i) { n     if (a & 1) res += RMQ[I[i] + a - 1]; n     a >>= 1; n   } n   return res; n } n int main() { n   int N, M; n   scanf( %d%d , &N, &M); n   vector<int> v; n   int i; n   for (i = (0); i < (M); ++i) { n     scanf( %d , &A[i]); n     if (A[i] == 0) n       scanf( %d%d , &A0[i], &A1[i]); n     else n       v.push_back(A[i]); n   } n   sort((v).begin(), (v).end()); n   v.resize(unique((v).begin(), (v).end()) - v.begin()); n   for (i = (0); i < (M); ++i) n     if (A[i] != 0) n       A[i] = int(lower_bound((v).begin(), (v).end(), A[i]) - v.begin()) + 1; n   set<pair<int, int> > bl, lb; n   bl.insert(pair<int, int>(-1, -N)); n   lb.insert(pair<int, int>(-N, -1)); n   v.clear(); n   memset(C, -1, sizeof(C)); n   for (i = (0); i < (M); ++i) n     if (A[i] != 0) n       if (C[A[i]] == -1) { n         pair<int, int> t = *lb.begin(); n         lb.erase(lb.begin()); n         bl.erase(pair<int, int>(t.second, t.first)); n         int l = -t.second; n         int r = -t.second - t.first; n         int m = (l + r) / 2; n         bl.insert(pair<int, int>(-l, l - m)); n         lb.insert(pair<int, int>(l - m, -l)); n         bl.insert(pair<int, int>(-(m + 1), (m + 1) - r)); n         lb.insert(pair<int, int>((m + 1) - r, -(m + 1))); n         A0[i] = m; n         C[A[i]] = m; n         v.push_back(m); n       } else { n         pair<int, int> t(-C[A[i]], 0); n         set<pair<int, int> >::iterator it0 = bl.lower_bound(t); n         set<pair<int, int> >::iterator it1 = it0; n         --it1; n         pair<int, int> r = *it1; n         pair<int, int> l = *it0; n         bl.erase(l); n         bl.erase(r); n         lb.erase(pair<int, int>(l.second, l.first)); n         lb.erase(pair<int, int>(r.second, r.first)); n         l.second += r.second - 1; n         bl.insert(l); n         lb.insert(pair<int, int>(l.second, l.first)); n         A0[i] = -C[A[i]]; n         C[A[i]] = -1; n       } n   for (i = (0); i < (M); ++i) n     if (A[i] == 0) { n       ++A1[i]; n       v.push_back(A0[i]); n       v.push_back(A1[i]); n     } n   sort((v).begin(), (v).end()); n   v.resize(unique((v).begin(), (v).end()) - v.begin()); n   memset(RMQ, 0, sizeof(RMQ)); n   I[0] = 0; n   for (i = (1); i < (20); ++i) I[i] = I[i - 1] + (1 << (21 - i)); n   for (i = (0); i < (M); ++i) n     if (A[i] == 0) { n       A0[i] = lower_bound((v).begin(), (v).end(), A0[i]) - v.begin(); n       A1[i] = lower_bound((v).begin(), (v).end(), A1[i]) - v.begin(); n       printf( %d n , Sum(A1[i]) - Sum(A0[i])); n     } else if (A0[i] < 0) { n       A0[i] = lower_bound((v).begin(), (v).end(), -A0[i]) - v.begin(); n       Add(A0[i], -1); n     } else { n       A0[i] = lower_bound((v).begin(), (v).end(), A0[i]) - v.begin(); n       Add(A0[i], 1); n     } n   return 0; n }; n


#include <bits/stdc++.h> n using namespace std; n const long long mod = 1000000007; n const int INF = 213456789; n const double eps = 1e-7; n int flag1 = 2, flag2 = 2, n, m; n template <class T> n T gcd(T a, T b) { n   if (!b) return a; n   return gcd(b, a % b); n } n int a[110][110]; n int dy[] = {1, -1, 0, 0}; n int dx[] = {0, 0, -1, 1}; n int prex[110][110]; n int prey[110][110]; n int cost[110][110]; n int dd[110][110]; n bool over; n int edx, edy; n void print(int nx, int ny) { n   if (nx == edx && ny == edy) return; n   print(prex[nx][ny], prey[nx][ny]); n   if (flag1 && dd[nx][ny] == 0) printf( L n ); n   if (!flag1 && dd[nx][ny] == 0) printf( R n ); n   if (flag1 && dd[nx][ny] == 1) printf( R n ); n   if (!flag1 && dd[nx][ny] == 1) printf( L n ); n   if (flag2 && dd[nx][ny] == 2) printf( D n ); n   if (!flag2 && dd[nx][ny] == 2) printf( U n ); n   if (flag2 && dd[nx][ny] == 3) printf( U n ); n   if (!flag2 && dd[nx][ny] == 3) printf( D n ); n   fflush(stdout); n   int l, r; n   scanf( %d%d , &l, &r); n } n void bfs(int x, int y) { n   for (int i = 1; i <= n; i++) { n     for (int j = 1; j <= m; j++) { n       cost[i][j] = INF; n     } n   } n   edx = x, edy = y; n   cost[x][y] = 0; n   queue<pair<int, int> > q; n   q.push(make_pair(x, y)); n   while (!q.empty()) { n     pair<int, int> tmp = q.front(); n     q.pop(); n     for (int i = 0; i < 4; i++) { n       int nx = dx[i] + tmp.first, ny = dy[i] + tmp.second; n       if (nx > 0 && nx <= n && ny > 0 && ny <= m && cost[nx][ny] == INF && n           a[nx][ny] != 3) { n         if (a[nx][ny] == 2) { n           dd[nx][ny] = i; n           prex[nx][ny] = tmp.first; n           prey[nx][ny] = tmp.second; n           cost[nx][ny] = cost[tmp.first][tmp.second] + 1; n           print(nx, ny); n           over = 1; n           return; n         } n         dd[nx][ny] = i; n         prex[nx][ny] = tmp.first; n         prey[nx][ny] = tmp.second; n         cost[nx][ny] = cost[tmp.first][tmp.second] + 1; n         q.push(make_pair(nx, ny)); n       } n     } n   } n } n void check1() { n   int i = 1; n   while (a[i][2] == 3) { n     if (flag2) n       printf( U n ); n     else n       printf( D n ); n     fflush(stdout); n     int l, r; n     scanf( %d%d , &l, &r); n     if (a[l][r] == 2) { n       over = 1; n       return; n     } n     i++; n   } n   printf( L n ); n   fflush(stdout); n   int l, r; n   scanf( %d%d , &l, &r); n   if (a[l][r] == 2) { n     over = 1; n     return; n   } n   if (l == i && r == 2) { n     flag1 = 1; n     printf( R n ); n     fflush(stdout); n     scanf( %d%d , &l, &r); n     if (a[l][r] == 2) { n       over = 1; n       return; n     } n     bfs(l, r); n   } else { n     flag1 = 0; n     bfs(l, r); n   } n } n void check2() { n   int i = 1; n   while (a[2][i] == 3) { n     if (flag1) n       printf( L n ); n     else n       printf( R n ); n     fflush(stdout); n     int l, r; n     scanf( %d%d , &l, &r); n     if (a[l][r] == 2) { n       over = 1; n       return; n     } n     i++; n   } n   printf( U n ); n   fflush(stdout); n   int l, r; n   scanf( %d%d , &l, &r); n   if (a[l][r] == 2) { n     over = 1; n     return; n   } n   if (l == 2 && r == i) { n     flag2 = 1; n     printf( D n ); n     fflush(stdout); n     scanf( %d%d , &l, &r); n     if (a[l][r] == 2) { n       over = 1; n       return; n     } n     bfs(l, r); n   } else { n     flag2 = 0; n     bfs(l, r); n   } 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       char c; n       cin >> c; n       if (c ==  . ) n         a[i][j] = 1; n       else if (c ==  F ) n         a[i][j] = 2; n       else if (c ==  * ) n         a[i][j] = 3; n     } n   } n   if (a[1][2] == 1 || a[1][2] == 2) { n     printf( L n ); n     fflush(stdout); n     int l, r; n     scanf( %d%d , &l, &r); n     if (a[l][r] == 2) return 0; n     if (l == 1 && r == 1) { n       flag1 = 0; n     } else { n       flag1 = 1; n       printf( R n ); n       fflush(stdout); n       scanf( %d%d , &l, &r); n       if (a[l][r] == 2) return 0; n     } n   } n   if (a[2][1] == 1 || a[2][1] == 2) { n     printf( U n ); n     fflush(stdout); n     int l, r; n     scanf( %d%d , &l, &r); n     if (a[l][r] == 2) return 0; n     if (l == 1 && r == 1) { n       flag2 = 0; n     } else { n       flag2 = 1; n       printf( D n ); n       fflush(stdout); n       scanf( %d%d , &l, &r); n       if (a[l][r] == 2) return 0; n     } n   } n   if (flag1 == 2) check1(); n   if (over) return 0; n   if (flag2 == 2) check2(); n   if (over) return 0; n   if (flag1 != 2 && flag2 != 2) bfs(1, 1); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n; n queue<int> q; n vector<int> v[1999]; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cin >> n; n   for (int i = 2; i <= n; i++) { n     int a; n     cin >> a; n     v[a].push_back(i); n   } n   q.push(1); n   int ok = 1; n   while (!q.empty()) { n     int cou = 0, sep = 0; n     int k = q.front(); n     q.pop(); n     for (auto x : v[k]) { n       sep = 1; n       if (v[x].empty()) { n         cou++; n       } n       q.push(x); n     } n     if (sep) { n       if (cou < 3) { n         ok = 0; n         break; n       } n     } n   } n   if (ok) { n     cout <<  YES ; n   } else { n     cout <<  NO ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int ans = -1; n map<pair<int, int>, bool> visited, mp; n void bfs(int x1, int y1, int x2, int y2) { n   queue<pair<pair<int, int>, int>> q; n   q.push({{x1, y1}, 0}); n   while (!q.empty()) { n     auto z = q.front(); n     q.pop(); n     visited[z.first] = 1; n     if (z.first.first == x2 && z.first.second == y2) { n       ans = z.second; n       break; n     } n     int x = z.first.first, y = z.first.second; n     if (x - 1 >= 1 && !visited[{x - 1, y}] && mp[{x - 1, y}]) { n       visited[{x - 1, y}] = true; n       q.push({{x - 1, y}, z.second + 1}); n     } n     if (x + 1 <= 1e9 && !visited[{x + 1, y}] && mp[{x + 1, y}]) { n       visited[{x + 1, y}] = true; n       q.push({{x + 1, y}, z.second + 1}); n     } n     if (y - 1 >= 1 && !visited[{x, y - 1}] && mp[{x, y - 1}]) { n       visited[{x, y - 1}] = true; n       q.push({{x, y - 1}, z.second + 1}); n     } n     if (y + 1 <= 1e9 && !visited[{x, y + 1}] && mp[{x, y + 1}]) { n       visited[{x, y + 1}] = true; n       q.push({{x, y + 1}, z.second + 1}); n     } n     if (x + 1 <= 1e9 && y + 1 <= 1e9 && !visited[{x + 1, y + 1}] && n         mp[{x + 1, y + 1}]) { n       visited[{x + 1, y + 1}] = true; n       q.push({{x + 1, y + 1}, z.second + 1}); n     } n     if (x + 1 <= 1e9 && y - 1 >= 1 && !visited[{x + 1, y - 1}] && n         mp[{x + 1, y - 1}]) { n       visited[{x + 1, y - 1}] = true; n       q.push({{x + 1, y - 1}, z.second + 1}); n     } n     if (x - 1 >= 1 && y + 1 <= 1e9 && !visited[{x - 1, y + 1}] && n         mp[{x - 1, y + 1}]) { n       visited[{x - 1, y + 1}] = true; n       q.push({{x - 1, y + 1}, z.second + 1}); n     } n     if (x - 1 >= 1 && y - 1 >= 1 && !visited[{x - 1, y - 1}] && n         mp[{x - 1, y - 1}]) { n       visited[{x - 1, y - 1}] = true; n       q.push({{x - 1, y - 1}, z.second + 1}); n     } n   } n } n int main() { n   ; n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   ; n   int x1, y1, x2, y2; n   cin >> x1 >> y1 >> x2 >> y2; n   int n; n   cin >> n; n   set<pair<int, pair<int, int>>> s; n   for (int i = 0; i < n; i++) { n     int r, a, b; n     cin >> r >> a >> b; n     s.insert({r, {a, b}}); n   } n   for (auto it : s) { n     for (int i = it.second.first; i <= it.second.second; i++) { n       mp[{it.first, i}] = true; n     } n   } n   mp[{x1, y1}] = true; n   mp[{x2, y2}] = true; n   bfs(x1, y1, x2, y2); n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void Solve() { n   long long n; n   cin >> n; n   vector<int> arr(n); n   for (int i = 0; i < n; i++) cin >> arr[i]; n   reverse(arr.begin(), arr.end()); n   for (int i = 0; i < n; i++) cout << arr[i] <<    ; n   cout <<   n ; n } n int main() { n   long long t; n   cin >> t; n   while (t--) Solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n inline char gc() { n   static char buf[1 << 16], *S, *T; n   if (S == T) { n     T = (S = buf) + fread(buf, 1, 1 << 16, stdin); n     if (T == S) return EOF; n   } n   return *S++; n } n inline int read() { n   int x = 0, f = 1; n   char ch = gc(); n   while (ch <  0  || ch >  9 ) { n     if (ch ==  - ) f = -1; n     ch = gc(); n   } n   while (ch >=  0  && ch <=  9 ) x = x * 10 + ch -  0 , ch = gc(); n   return x * f; n } n int n, w, mxb, mxa, bel[100010], nn, cnt[350], q[350][350], qh[350], qt[350]; n long long d[100010]; n struct data { n   int a, b; n   friend bool operator<(data a, data b) { return a.b < b.b; } n } A[100010]; n inline double slope(int i, int j) { return (d[i] - d[j]) * 1.0 / (j - i); } n inline void gao(int i, int x) { n   while (qh[i] < qt[i] && slope(q[i][qh[i]], q[i][qh[i] + 1]) <= x) ++qh[i]; n } n inline void rebuild(int id, int l, int r) { n   qh[id] = 1; n   qt[id] = 0; n   for (int i = l; i <= r; ++i) { n     while (qh[id] < qt[id] && n            slope(q[id][qt[id]], i) < slope(q[id][qt[id] - 1], q[id][qt[id]])) n       --qt[id]; n     q[id][++qt[id]] = i; n   } n   gao(id, 0); n } n inline void add(int x) { n   if (!x) return; n   for (int i = 1; i < bel[x]; ++i) gao(i, ++cnt[i]); n   int l = (bel[x] - 1) * nn + 1, r = min(bel[x] * nn, mxa), id = bel[x]; n   for (int i = l; i <= r; ++i) d[i] += (long long)cnt[id] * i; n   for (int i = l; i <= x; ++i) d[i] += i; n   cnt[id] = 0; n   rebuild(id, l, r); n } n int main() { n   n = read(); n   w = read(); n   for (int i = 1; i <= n; ++i) n     A[i].a = read(), A[i].b = read(), mxb = max(mxb, A[i].b), n     mxa = max(mxa, A[i].a); n   sort(A + 1, A + n + 1); n   int now = 1; n   nn = sqrt(mxa); n   for (int i = 1; i <= mxa; ++i) bel[i] = (i - 1) / nn + 1; n   int tot = bel[mxa]; n   for (int i = 1; i <= tot; ++i) qh[i] = 1, q[i][++qt[i]] = min(i * nn, mxa); n   int flag = 0; n   for (int c = 0; c <= mxb + 1; ++c) { n     while (now <= n && A[now].b < c) add(A[now].a), ++now; n     long long ans = 0; n     int ans1 = 0; n     for (int i = 1; i <= tot; ++i) { n       int x = q[i][qh[i]]; n       if (d[x] + (long long)x * cnt[i] > ans) n         ans = d[x] + (long long)x * cnt[i], ans1 = x; n     } n     printf( %I64d %d n , ans + (long long)w * c * (n - now + 1), ans1); n     if (c == 1) flag = ans + (long long)w * c * (n - now + 1); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int a[2000][2000], k, p[2000], Index; n void add(int x, int y) { a[x][y] = a[y][x] = 1; } n int main() { n   scanf( %d , &k); n   int maxbit = 0; n   for (int i = (1); i <= (30); ++i) n     if ((1 << i) & k) maxbit = i; n   Index = 2; n   int cur = ++Index; n   add(1, cur); n   for (int i = (1); i <= (maxbit); ++i) { n     add(cur, Index + 1); n     add(cur, Index + 2); n     add(Index + 1, Index + 3); n     add(Index + 2, Index + 3); n     cur = Index += 3; n     p[maxbit - i] = cur; n   } n   add(cur, 2); n   add(1, ++Index); n   cur = Index; n   for (int i = (maxbit - 1); i >= (0); --i) { n     add(cur, ++Index), cur = Index; n     if ((1 << i) & k) add(cur, p[i]); n     add(cur, ++Index), cur = Index; n   } n   printf( %d n , Index); n   for (int i = (1); i <= (Index); ++i) { n     for (int j = (1); j <= (Index); ++j) n       if (a[i][j]) n         printf( Y ); n       else n         printf( N ); n     puts(  ); n   } n   assert(Index <= 150); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m; n const int MAXN = 200100; n int win = 0, ti = 0; n int vis[MAXN][2] = {0}; n int vs[MAXN] = {0}; n int path[MAXN], cnt; n vector<int> g[MAXN]; n bool dfsloop(int u) { n   vs[u] = 1; n   bool f = false; n   for (int i = 0; i < g[u].size(); i++) { n     int v = g[u][i]; n     if (vs[v] == 1) n       f = true; n     else if (vs[v] == 0) n       f = dfsloop(v); n     if (f) return true; n   } n   vs[u] = 2; n   return false; n } n void dfs(int u, int ly) { n   vis[u][ly % 2] = 1; n   int sz = g[u].size(); n   cnt = ly; n   path[ly] = u; n   if (!sz) { n     if (ly % 2 == 0) { n       win = 1; n       return; n     } else n       ; n   } n   for (int i = 0; i < sz; i++) { n     int v = g[u][i]; n     if (vis[v][(ly + 1) % 2] == 0) { n       dfs(v, ly + 1); n       if (win) return; n     } n   } n } n int main() { n   int n, m; n   scanf( %d%d , &n, &m); n   for (int i = 1; i <= n; i++) { n     int ci; n     scanf( %d , &ci); n     while (ci--) { n       int v; n       scanf( %d , &v); n       g[i].push_back(v); n     } n   } n   int s; n   scanf( %d , &s); n   dfs(s, 1); n   if (win) { n     printf( Win n ); n     for (int i = 1; i <= cnt; i++) printf( %d  , path[i]); n   } else { n     bool res = dfsloop(s); n     if (res) n       printf( Draw ); n     else n       printf( Lose ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n set<int> s; n int t, n, a[200020], cnt, d[200020], e[200020]; n int main() { n   for (scanf( %d , &t); t--; s.clear(), cnt = 0) { n     scanf( %d , &n); n     for (int i = 0; i < n; i++) d[i] = e[i] = 0; n     for (int i = 0; i < n; i++) scanf( %d , a + i); n     for (int i = 0; i < n; i++) { n       if (s.count(a[i])) break; n       s.insert(a[i]); n       while (s.count(cnt + 1)) cnt++; n       if (s.size() == cnt) d[i] = 1; n     } n     s.clear(), cnt = 0; n     for (int i = n - 1; i >= 0; i--) { n       if (s.count(a[i])) break; n       s.insert(a[i]); n       while (s.count(cnt + 1)) cnt++; n       if (s.size() == cnt) e[i] = 1; n     } n     vector<int> v; n     for (int i = 0; i < n - 1; i++) { n       if (d[i] && e[i + 1]) v.push_back(i + 1); n     } n     printf( %d n , v.size()); n     for (int x : v) printf( %d %d n , x, n - x); n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int MAX = 2e5 + 10; n const double eps = 1e-6; n const double mod = 1e9 + 7; n struct num { n   int v, i; n } b[MAX]; n bool cmp(const num &x, const num &y) { return x.v > y.v; } n bool cmp1(const num &x, const num &y) { return x.i < y.i; } n int main() { n   int m, a[MAX]; n   cin >> m; n   for (int i = 1; i <= m; i++) cin >> a[i]; n   for (int i = 1; i <= m; i++) cin >> b[i].v, b[i].i = i; n   sort(a + 1, a + m + 1); n   sort(b + 1, b + m + 1, cmp); n   for (int i = 1; i <= m; i++) b[i].v = a[i]; n   sort(b + 1, b + m + 1, cmp1); n   for (int i = 1; i <= m; i++) cout << b[i].v <<    ; n   putchar(  n ); n } n


#include <bits/stdc++.h> n int main() { n   unsigned long long n; n   unsigned long long answer; n   scanf( %I64u , &n); n   if (n < 3) { n     printf( %lu , n); n     return 0; n   } n   if (n % 2 == 1) { n     answer = (n) * (n - 1) * (n - 2); n   } else if (n % 3 != 0) { n     answer = n * (n - 1) * (n - 3); n   } else { n     answer = (n - 1) * (n - 2) * (n - 3); n   } n   printf( %I64u n , answer); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int Int() { n   int x; n   scanf( %d , &x); n   return x; n } n long long Long() { n   long long x; n   scanf( %lld , &x); n   return x; n } n void err(istream_iterator<string> it) { cout << endl; } n template <typename T, typename... Args> n void err(istream_iterator<string> it, T a, Args... args) { n   cerr << *it <<   =   << a <<    ; n   err(++it, args...); n } n const int N = (int)2e5 + 5; n const int maxN = (int)1e6 + 6; n const long long Mod = (long long)1e9 + 7; n const int inf = (int)2e9; n const long long Inf = (long long)1e18; n long long dp[20][5][5][11][11]; n int n, k; n long long call(int pos, int prior, int last, int hump, int uhump) { n   if (pos == n) { n     return hump == k and uhump == k - 1; n   } n   long long &ret = dp[pos][prior][last][hump][uhump]; n   if (~ret) return ret; n   ret = 0; n   for (int i = 1; i <= 4; ++i) { n     if (i == last) continue; n     int f = hump; n     int first = uhump; n     if (prior and last) { n       f += (prior < last and last > i); n       first += (prior > last and last < i); n     } n     ret += call(pos + 1, last, i, f, first); n   } n   return ret; n } n int main() { n   int test = 1, tc = 0; n   while (test--) { n     n = Int(), k = Int(); n     memset(dp, -1, sizeof dp); n     long long res = 0; n     for (int i = 1; i <= 4; ++i) { n       res += call(1, 0, i, 0, 0); n     } n     cout << res <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long int n, k; n int a[200005]; n int main() { n   int t; n   scanf( %d , &t); n   while (t--) { n     scanf( %d%d , &n, &k); n     int pos = 0, mn = 1000000000; n     for (int i = 1; i <= n; i++) { n       scanf( %d , &a[i]); n       if (i > k && a[i] - a[i - k] < mn) { n         mn = a[i] - a[i - k]; n         pos = i; n       } n     } n     printf( %d n , (a[pos] + a[pos - k]) / 2); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int x; n   cin >> x; n   for (int a = 1; a <= x; ++a) n     for (int b = 1; b <= x; ++b) n       if (a % b == 0 && a * b > x && a / b < x) { n         cout << a <<     << b << endl; n         return 0; n       } n   puts( -1 ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long n, k, l, r, sz, i, len, q, p, mid, anspos, ans[1000001]; n long long mxl, mxr; n long long pos[1000001], t[1000001], pr[100001]; n struct Pair { n   long long pos; n   long long x; n   long long d; n }; n Pair a[100001], b[100001]; n bool cmp(Pair p1, Pair p2) { return p1.x < p2.x; } n bool cmd(Pair p1, Pair p2) { return p1.pos < p2.pos; } n long long binl(long long L, long long R, long long height) { n   long long otv = -1; n   while (L <= R) { n     mid = L + R; n     mid /= 2; n     if (b[mid].x > height) n       R = mid - 1; n     else n       otv = mid, L = mid + 1; n   } n   return otv; n } n long long binr(long long L, long long R, long long height) { n   long long otv = -1; n   while (L <= R) { n     mid = L + R; n     mid /= 2; n     if (b[mid].x < height) n       L = mid + 1; n     else n       otv = mid, R = mid - 1; n   } n   return otv; n } n long long tree(long long L, long long R) { n   long long nn = 0; n   while (L <= R) { n     if (L % 2 == 1) nn = max(nn, t[L]); n     if (R % 2 == 0) nn = max(nn, t[R]); n     L = (L + 1) / 2; n     R = (R - 1) / 2; n   } n   return nn; n } n void update(long long x) { n   x /= 2; n   while (x) { n     t[x] = max(t[x * 2], t[x * 2 + 1]); n     x /= 2; n   } n } n int main() { n   cin >> n >> k; n   sz = 1; n   while (sz < n) sz *= 2; n   for (i = 1; i <= n; i++) { n     cin >> a[i].x; n     a[i].pos = i; n     b[i] = a[i]; n   } n   sort(b + 1, b + n + 1, cmp); n   for (int i = 1; i <= n; i++) pos[b[i].pos] = i, pr[i] = b[i].pos; n   for (int i = 1; i <= n; i++) { n     int l = binl(1, pos[i] - 1, a[i].x - k); n     int r = binr(pos[i] + 1, n, a[i].x + k); n     int mxl = 0, mxr = 0; n     if (l > 0) mxl = tree(sz, l + sz - 1); n     if (r > 0) mxr = tree(r + sz - 1, n + sz - 1); n     t[pos[i] + sz - 1] = max(mxl, mxr) + 1; n     update(pos[i] + sz - 1); n     a[pr[pos[i]]].d = t[pos[i] + sz - 1]; n   } n   sort(b + 1, b + n + 1, cmd); n   cout << t[1] << endl; n   for (int i = 1; i <= n; i++) { n     if (a[i].d == t[1]) { n       p = i; n       break; n     } n   } n   ans[++q] = p; n   for (int j = p; j >= 1; j--) { n     if (a[j].d == a[p].d - 1 && abs(a[j].x - a[p].x) >= k) { n       ans[++q] = j; n       p = j; n     } n   } n   for (int i = q; i >= 1; i--) cout << ans[i] <<    ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int helper(long long int edges, long long int nodes) { n   if (edges == nodes - 1) return nodes - 1; n   edges -= nodes - 1; n   return nodes - 1 - ceil((1.0 + sqrt(1.0 + 8.0 * edges)) / 2.0); n } n int main() { n   ios::sync_with_stdio(false); n   ; n   cin.tie(0); n   ; n   long long int i, j, q, n, lo, mid, hi; n   cin >> q; n   while (q--) { n     cin >> n; n     lo = n - 1; n     hi = (n * (n - 1)) / 2 + 1; n     while (hi - lo > 1) { n       mid = (lo + hi) / 2; n       if (helper(mid, n) >= (mid + 1) / 2) n         lo = mid; n       else n         hi = mid; n     } n     cout << lo <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n priority_queue<pair<long long, long long>, vector<pair<long long, long long>>, n                less<pair<long long, long long>>> n     q; n priority_queue<long long, vector<long long>, greater<long long>> p; n long long n, k, t, b, temp, res; n int main() { n   cin >> n >> k; n   for (int i = 0; i < n; i++) { n     cin >> t >> b; n     q.push({b, t}); n   } n   for (int i = 0; i < n; i++) { n     pair<long long, long long> tt = q.top(); n     if (p.size() < k) { n       p.push(tt.second); n       temp += tt.second; n     } else { n       if (tt.second > p.top()) { n         temp += (tt.second - p.top()); n         p.pop(); n         p.push(tt.second); n       } n     } n     q.pop(); n     res = max(res, tt.first * temp); n   } n   cout << res << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   cin >> n; n   string s; n   cin >> s; n   int a[n]; n   for (int i = 0; i < n; i++) cin >> a[i]; n   int ans = 1e9; n   for (int i = 0; i < n - 1; i++) n     if (s[i] ==  R  && s[i + 1] ==  L ) ans = min(ans, (a[i + 1] - a[i]) / 2); n   if (ans == 1e9) n     cout << -1 << endl; n   else n     cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long linf = 1001002003004005006ll; n const int inf = 1001001001; n const int mod = 1000000007; n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   int t; n   cin >> t; n   while (t--) { n     int n; n     cin >> n; n     string s; n     cin >> s; n     string ans1, ans2; n     bool elf = false; n     for (int i = 0; i < (n); ++i) { n       if (elf) { n         if (s[i] ==  2 ) ans1 +=  0 , ans2 +=  2 ; n         if (s[i] ==  0 ) ans1 +=  0 , ans2 +=  0 ; n         if (s[i] ==  1 ) ans1 +=  0 , ans2 +=  1 ; n       } else { n         if (s[i] ==  2 ) ans1 +=  1 , ans2 +=  1 ; n         if (s[i] ==  0 ) ans1 +=  0 , ans2 +=  0 ; n         if (s[i] ==  1 ) { n           ans1 +=  1 , ans2 +=  0 ; n           elf = true; n         } n       } n     } n     cout << ans1 << endl << ans2 << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n inline long long rd() { n   register long long x = 0; n   register bool f = 0; n   register char c = getchar(); n   while (!isdigit(c)) { n     if (c ==  - ) f = 1; n     c = getchar(); n   } n   while (isdigit(c)) { n     x = x * 10 + (c ^ 48); n     c = getchar(); n   } n   return f ? -x : x; n } n int n, a[200010], bit[31]; n inline int insert(int x) { n   for (register int i = 30; ~i; --i) { n     if (x & (1 << i)) { n       if (!bit[i]) { n         bit[i] = x; n         return 1; n       } n       x ^= bit[i]; n     } n   } n   return 0; n } n int main() { n   n = rd(); n   for (register int i = 1; i <= n; ++i) a[i] = rd() ^ a[i - 1]; n   if (!a[n]) { n     puts( -1 ); n     return 0; n   } n   int ans = insert(a[n]); n   for (register int i = n - 1; i; --i) ans += insert(a[i]); n   printf( %d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n pair<int, int> Castle[100010]; n int Index[100010]; n int main() { n   int N; n   scanf( %d , &N); n   int Temp; n   for (int i = 1; i <= N; i++) { n     scanf( %d , &Temp); n     Castle[i] = make_pair(Temp, i); n   } n   sort(Castle + 1, Castle + N + 1); n   int Max = 1; n   int Ans = 0; n   for (int i = 1; i <= N; i++) Index[Castle[i].second] = i; n   for (int i = 1; i <= N; i++) { n     Max = max(Max, Index[i]); n     if (Max == i) Ans++; n   } n   printf( %d n , Ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int read() { n   int num = 0; n   bool f = 0; n   char ch = getchar(); n   while (ch <  0  || ch >  9 ) { n     f = (ch ==  - ); n     ch = getchar(); n   } n   while (ch >=  0  && ch <=  9 ) { n     num = (num << 1) + (num << 3) + ch -  0 ; n     ch = getchar(); n   } n   return f ? -num : num; n } n int main() { n   int num[111]; n   int n = read(), cnt = 0, t = 0, ans = 0; n   memset(num, 0, sizeof(num)); n   while (n) { n     num[cnt++] = n % 2; n     n >>= 1; n   } n   swap(num[0], num[4]); n   swap(num[2], num[3]); n   for (int i = 0; i <= 5; i++) n     if (num[i]) ans += pow(2, i); n   printf( %d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n #pragma GCC optimize( Ofast ) n #pragma GCC target( sse,sse2,sse3,sse3,sse4,popcnt,abm,mmx ) n using namespace std; n const int MX = 1e9 + 1; n const int Imp = -1; n const int N = 1e6 + 5; n const int M = 1e6 + 5; n const long long INF = 1e18 + 1; n int n, k; n string s; n set<char> st; n int main() { n   ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); n   string s; n   cin >> s; n   int k; n   cin >> k; n   int n = s.size(); n   if (n < k) { n     cout <<  impossible ; n   } else { n     for (int i = 0; i < n; ++i) st.insert(s[i]); n     int x = st.size(); n     if (x > k) n       cout << 0; n     else n       cout << abs(x - k); n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, k, x; n   vector<int> a; n   cin >> n >> k; n   vector<int> w(k); n   for (int i = 0; i < n; i++) { n     cin >> x; n     a.push_back(x); n   } n   for (int i = 0; i < k; i++) { n     for (int j = i; j < n; j += k) { n       w[i] += a[j]; n     } n   } n   int menor = w[0]; n   int z = 0; n   for (int i = 0; i < k; i++) n     if (menor > w[i]) { n       menor = w[i]; n       z = i; n     } n   cout << z + 1 << endl; n } n


#include <bits/stdc++.h> n using namespace std; n const long long N = 5000700; n template <typename T> n pair<T, T> operator-(pair<T, T> a, pair<T, T> b) { n   return make_pair(a.first - b.first, a.second - b.second); n } n template <typename T> n pair<T, T> operator+(pair<T, T> a, pair<T, T> b) { n   return make_pair(a.first + b.first, a.second + b.second); n } n long long ans[N]; n long long qx[N]; n long long qy[N]; n vector<long long> qrs; n bool to_left(const pair<long long, long long> &a, n              const pair<long long, long long> &b) { n   return a.second * b.first > a.first * b.second; n } n bool to_left_equal(const pair<long long, long long> &a, n                    const pair<long long, long long> &b) { n   return a.second * b.first >= a.first * b.second; n } n bool in_triangle(const pair<long long, long long> &x, n                  const pair<long long, long long> &a, n                  const pair<long long, long long> &b, n                  const pair<long long, long long> &c) { n   bool b1 = to_left_equal(x - a, b - a), b2 = to_left_equal(x - b, c - b), n        b3 = to_left_equal(x - c, a - c), b4 = to_left_equal(x - a, c - a), n        b5 = to_left_equal(x - b, a - b), b6 = to_left_equal(x - c, b - c); n   return ((b1 && b2 && b3) || (b4 && b5 && b6)); n } n long long quadrant(const pair<long long, long long> &a) { n   if (a.second < 0) { n     if (a.first < 0) n       return 0; n     else n       return 1; n   } else { n     if (a.first > 0) n       return 2; n     else n       return 3; n   } n } n bool f(const long long &a, const long long &b) { n   if (qx[a] == qx[b] && qy[a] == qy[b]) return false; n   if (qx[a] == 0 && qy[a] == 0) return true; n   if (qx[b] == 0 && qy[b] == 0) return false; n   long long n1 = quadrant(make_pair(qx[a], qy[a])), n             n2 = quadrant(make_pair(qx[b], qy[b])); n   if (n1 != n2) return n1 < n2; n   return to_left(make_pair(qx[b], qy[b]), make_pair(qx[a], qy[a])); n } n bool f2(const pair<long long, long long> &a, n         const pair<long long, long long> &b) { n   if (a == b) return false; n   if (a.first == 0 && a.second == 0) return true; n   if (b.first == 0 && b.second == 0) return false; n   long long n1 = quadrant(a), n2 = quadrant(b); n   if (n1 != n2) return n1 < n2; n   return to_left(b, a); n } n vector<pair<long long, long long>> w[5]; n vector<pair<long long, long long>> points; n vector<pair<long long, long long>> allPoints; n vector<pair<long long, long long>> half[2]; n void sum(vector<pair<long long, long long>> w1, n          vector<pair<long long, long long>> w2, long long id) { n   points.clear(); n   allPoints.clear(); n   half[0].clear(); n   half[1].clear(); n   pair<long long, long long> mini1 = w1[0], mini2 = w2[0]; n   long long mini1it, mini2it; n   for (long long i = 0; i < w1.size(); i++) { n     if (w1[i].second > mini1.second || n         (w1[i].second == mini1.second && w1[i].first < mini1.first)) { n       mini1it = i; n       mini1 = w1[i]; n     } n   } n   for (long long i = 0; i < w2.size(); i++) { n     if (w2[i].second > mini2.second || n         (w2[i].second == mini2.second && w2[i].first < mini2.first)) { n       mini2it = i; n       mini2 = w2[i]; n     } n   } n   for (long long i = 0; i + 1 < w1.size(); i++) { n     allPoints.push_back(w1[i + 1] - w1[i]); n   } n   allPoints.push_back(w1[0] - w1[w1.size() - 1]); n   for (long long i = 0; i + 1 < w2.size(); i++) { n     allPoints.push_back(w2[i + 1] - w2[i]); n   } n   allPoints.push_back(w2[0] - w2[w2.size() - 1]); n   sort(allPoints.begin(), allPoints.end(), f2); n   pair<long long, long long> cp = mini1 + mini2; n   for (auto it : allPoints) { n     cp = cp + it; n     points.push_back(cp); n   } n   sort(points.begin(), points.end()); n   vector<pair<long long, long long>> half[2]; n   for (long long i = 0; i < 2; i++) { n     for (auto it : points) { n       while (half[i].size() >= 2 && n              to_left_equal(it - half[i][half[i].size() - 2], n                            half[i].back() - half[i][half[i].size() - 2])) n         half[i].pop_back(); n       half[i].push_back(it); n     } n     reverse(points.begin(), points.end()); n   } n   for (auto x : half[0]) { n     w[id].push_back(x); n   } n   for (long long i = 1; i + 1 < half[1].size(); i++) n     w[id].push_back(half[1][i]); n   reverse(w[id].begin(), w[id].end()); n } n void solve() { n   for (long long i = 0; i < 3; i++) { n     long long n; n     cin >> n; n     for (long long j = 0; j < n; j++) { n       long long x, y; n       cin >> x >> y; n       w[i].push_back(make_pair(x, y)); n     } n   } n   sum(w[0], w[1], 3); n   sum(w[2], w[3], 4); n   auto mini = w[4][0]; n   for (auto it : w[4]) { n     if (it.second < mini.second || n         (it.second == mini.second && it.first < mini.first)) n       mini = it; n   } n   for (auto &it : w[4]) it = it - mini; n   sort(w[4].begin(), w[4].end(), f2); n   long long q; n   cin >> q; n   for (long long i = 0; i < q; i++) { n     cin >> qx[i] >> qy[i]; n     qx[i] = 3 * qx[i] - mini.first; n     qy[i] = 3 * qy[i] - mini.second; n     qrs.push_back(i); n   } n   long long ptr = 1; n   sort(qrs.begin(), qrs.end(), f); n   for (long long i = 0; i < q; i++) { n     long long query = qrs[i]; n     if (qy[query] < 0) { n       continue; n     } n     if (qy[query] == 0 && qx[query] == 0) { n       ans[query] = 1; n       continue; n     } n     while (ptr + 1 < w[4].size() && n            to_left(make_pair(qx[query], qy[query]), w[4][ptr + 1])) { n       ptr++; n     } n     if (ptr + 1 < w[4].size() && n         in_triangle(make_pair(qx[query], qy[query]), make_pair(0, 0), w[4][ptr], n                     w[4][ptr + 1])) n       ans[query] = 1; n   } n   for (long long i = 0; i < q; i++) { n     if (ans[i]) { n       cout <<  YES n ; n     } else { n       cout <<  NO n ; n     } n   } n } n int32_t main() { n   long long t = 1; n   while (t--) solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int32_t main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   long long k; n   cin >> k; n   long long res[4][4]; n   for (long long i = 0; i < 4; ++i) { n     for (long long j = 0; j < 4; ++j) { n       if (i == 1 && j == 2) { n         res[i][j] = (1 << 17); n       } else if (i == 2 && j == 1) { n         res[i][j] = (1 << 17); n       } else if ((i + j) == 3) { n         res[i][j] = k; n       } else if (i == 3 && j == 3) { n         res[i][j] = k; n       } else { n         res[i][j] = (k + (1 << 17)); n       } n     } n   } n   cout << 4 <<     << 4 << endl; n   for (long long i = 0; i < 4; ++i) { n     for (long long j = 0; j < 4; ++j) { n       cout << res[i][j] <<    ; n     } n     cout << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long gcd(long long a, long long b) { n   long long tmp; n   while (b) { n     a %= b; n     tmp = a; n     a = b; n     b = tmp; n   } n   return a; n } n int main() { n   int n; n   cin >> n; n   vector<int> a(n + 1, 0); n   for (int i = 0; i < n; i++) { n     cin >> a[i + 1]; n   } n   sort(a.begin(), a.end()); n   long long sum1 = 0, sum2 = 0; n   vector<long long> sum(n + 1, 0); n   for (int i = 1; i <= n; i++) { n     sum[i] += sum[i - 1] + (long long)i * (a[i] - a[i - 1]); n     sum1 += sum[i]; n     sum2 += a[i]; n   } n   long long num = 2 * sum1 - sum2; n   long long den = n; n   long long g = gcd(num, den); n   cout << num / g <<     << den / g << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, root, m; n int a[100100]; n int h[100100]; n vector<int> g[100100]; n int arr[200100]; n int N, tin[100100], tout[100100]; n vector<pair<int, int> > t[200100 * 4]; n void dfs(int v, int par = -1, int len = 0) { n   h[v] = len; n   tin[v] = ++N; n   arr[N] = v; n   for (int to : g[v]) { n     if (to != par) { n       dfs(to, v, len + 1); n     } n   } n   tout[v] = ++N; n   arr[N] = v; n } n void build(int v, int l, int r) { n   if (l == r) { n     int node = arr[l]; n     t[v].push_back(make_pair(h[node], a[node])); n     return; n   } n   int mid = (l + r) >> 1; n   build(v + v, l, mid), build(v + v + 1, mid + 1, r); n   merge((t[v + v]).begin(), (t[v + v]).end(), (t[v + v + 1]).begin(), n         (t[v + v + 1]).end(), back_inserter(t[v])); n   for (int i = 1; i < ((int)(t[v]).size()); ++i) { n     t[v][i].second = min(t[v][i - 1].second, t[v][i].second); n   } n } n int get(int v, int l, int r, int L, int R, int k) { n   if (r < L || R < l) return 2e9; n   if (L <= l && r <= R) { n     int lf = 0, rg = ((int)(t[v]).size()) - 1, i = -1; n     while (lf <= rg) { n       int avg = (lf + rg) >> 1; n       if (t[v][avg].first <= k) n         i = avg, lf = avg + 1; n       else n         rg = avg - 1; n     } n     return i == -1 ? 2e9 : t[v][i].second; n   } n   int mid = (l + r) >> 1; n   return min(get(v + v, l, mid, L, R, k), get(v + v + 1, mid + 1, r, L, R, k)); n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0), cout.tie(0); n   cin >> n >> root; n   for (int i = 1; i <= n; ++i) cin >> a[i]; n   for (int i = 1; i < n; ++i) { n     int x, y; n     cin >> x >> y; n     g[x].push_back(y); n     g[y].push_back(x); n   } n   dfs(root); n   build(1, 1, N); n   cin >> m; n   int ans = 0; n   for (int i = 1; i <= m; ++i) { n     int p, q; n     cin >> p >> q; n     int x = (p + ans) % n + 1; n     int k = (q + ans) % n; n     ans = get(1, 1, N, tin[x], tout[x], h[x] + k); n     cout << ans <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct node { n   node *next[26]; n   node() { n     for (int i = 0; i < 26; i++) next[i] = NULL; n   } n }; n void insert(node *root, string &str) { n   node *current = root; n   for (char ch : str) { n     if (current->next[ch -  a ] == NULL) { n       current->next[ch -  a ] = new node(); n     } n     current = current->next[ch -  a ]; n   } n } n int canLose(node *root) { n   int was = 1, leaf = 0; n   for (int i = 0; i < 26; i++) { n     if (root->next[i]) { n       was = min(was, 1 ^ canLose(root->next[i])); n       leaf = 1; n     } n   } n   return was & leaf; n } n int canWin(node *root) { n   int was = 0; n   for (int i = 0; i < 26; i++) { n     if (root->next[i]) { n       was = max(was, 1 ^ canWin(root->next[i])); n     } n   } n   return was; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   node *root = new node(); n   int n, k; n   cin >> n >> k; n   for (int i = 1; i <= n; i++) { n     string str; n     cin >> str; n     insert(root, str); n   } n   if (canWin(root) && !canLose(root)) { n     cout <<  First  << endl; n   } else if (canWin(root)) { n     if (k & 1) { n       cout <<  First  << endl; n     } else { n       cout <<  Second  << endl; n     } n   } else { n     cout <<  Second  << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long a[100500]; n vector<long long> pfx[100500], vtx[100500]; n long long proc(long long x, long long sum) { n   int n = vtx[x].size(); n   int mid = n / 2; n   long long res = sum; n   for (int i = mid, _n = n; i < _n; i++) res += vtx[x][i] - vtx[x][mid]; n   for (int i = 0, _n = mid; i < _n; i++) res += vtx[x][mid] - vtx[x][i]; n   for (int i = 0, _n = n; i < _n; i++) res -= abs(x - vtx[x][i]); n   return res; n } n int main() { n   int n, m; n   long long sum = 0LL; n   scanf( %d %d , &n, &m); n   for (int i = 0, _n = m; i < _n; i++) scanf( %I64d , a + i); n   for (int i = 1, _n = m; i < _n; i++) { n     if (a[i] == a[i - 1]) continue; n     vtx[a[i]].push_back(a[i - 1]); n     vtx[a[i - 1]].push_back(a[i]); n     sum += abs(a[i] - a[i - 1]); n   } n   for (int i = 1, _n = n + 1; i < _n; i++) sort(vtx[i].begin(), vtx[i].end()); n   long long ans = sum; n   for (int i = 1, _n = n + 1; i < _n; i++) ans = min(ans, proc(i, sum)); n   printf( %I64d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   string s; n   cin >> s; n   long long l = s.length(); n   long long ans = 0; n   for (long long i = l - 1; i >= 1; i--) { n     ans = ans + pow(2, i); n   } n   for (long long i = 0; i < l; i++) { n     if (s[i] ==  7 ) { n       ans = ans + pow(2, l - i - 1); n     } n   } n   cout << ans + 1 <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n class Timer { n   clock_t start; n   string name; n  n  public: n   Timer() { n     name =   ; n     start = clock(); n   } n   Timer(string s) { n     name = s; n     start = clock(); n   } n   ~Timer() { n     fprintf(stderr,  %s: %.3gs n , name.c_str(), n             1.0 * (clock() - start) / CLOCKS_PER_SEC); n   } n }; n const double EPS = 1e-9; n const long double PI = acos(-1.0L); n template <typename dtype> n inline dtype sq(dtype a) { n   return a * a; n } n template <typename dtype1, typename dtype2> n inline pair<dtype1, dtype2> mp(dtype1 a, dtype2 b) { n   return make_pair(a, b); n } n template <typename dtype1, typename dtype2> n inline dtype1 safeMod(dtype1 a, dtype2 m) { n   return (a % m + m) % m; n } n template <typename dtype1, typename dtype2> n inline bool isEq(dtype1 a, dtype2 b) { n   return abs(a - b) < EPS; n } n template <typename dtype1, typename dtype2, typename dtype3> n inline bool isEq(dtype1 a, dtype2 b, dtype3 eps) { n   return abs(a - b) < eps; n } n template <typename dtype> n inline dtype toRad(dtype deg) { n   return deg * PI / 180.0; n } n template <typename dtype> n inline dtype toDeg(dtype rad) { n   return rad * 180.0 / PI; n } n template <typename dtype> n inline bool isKthBitOn(dtype n, int k) { n   assert(n <= numeric_limits<dtype>::max()); n   assert(k <= numeric_limits<dtype>::digits); n   dtype ONE = 1; n   return bool((n & (ONE << k))); n } n template <typename dtype> n inline void setKthBit(dtype& n, int k) { n   assert(n <= numeric_limits<dtype>::max()); n   assert(k <= numeric_limits<dtype>::digits); n   dtype ONE = 1; n   n = (n | (ONE << k)); n } n const int oo = 0x3f3f3f3f; n const int MAX = 200010; n const int MOD = 1000000007; n const int precision = 10; n int main(int argc, char* argv[]) { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   long long n, s, sum = 0; n   long long ara[MAX]; n   cin >> n >> s; n   long long mini = INT_MAX; n   for (int i = 0; i < n; i++) { n     cin >> ara[i]; n     sum += ara[i]; n     mini = min(mini, ara[i]); n   } n   if (sum < s) n     cout << -1; n   else { n     sum = 0; n     for (int i = 0; i < n; i++) { n       sum += ara[i] - mini; n     } n     s -= sum; n     s = max(s, 0LL); n     mini -= s / n + (s % n != 0); n     cout << mini << endl; 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(NULL); n   int t; n   cin >> t; n   while (t--) { n     int n, m; n     cin >> n >> m; n     vector<int> g[n]; n     for (int i = 0; i < m; i++) { n       int u, v; n       cin >> u >> v; n       u--, v--; n       g[v].push_back(u); n     } n     int l[n]; n     for (int i = 0; i < n; i++) l[i] = 0; n     vector<int> a; n     for (int i = 0; i < n; i++) { n       for (auto j : g[i]) l[i] = max(l[i], l[j] + 1); n       if (l[i] == 2) { n         l[i] = -1; n         a.push_back(i); n       } n     } n     cout << a.size() <<   n ; n     for (auto i : a) cout << i + 1 <<    ; n     cout <<   n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int Maxn = 10005; n const int Inf = 1000000000; n int n; n int a[Maxn]; n set<pair<int, int> > S; n long long res; n int main() { n   scanf( %d , &n); n   for (int i = 0; i < n; i++) scanf( %d , &a[i]); n   for (int i = 0; i < n; i++) S.insert(pair<int, int>(i - a[i], i)); n   for (int i = 0; i < n; i++) { n     int add = 0, j = 0; n     set<pair<int, int> >::iterator it = S.begin(); n     while (j < n - 1) { n       int nxt = j; n       while (it != S.end() && j >= it->first - i) { n         nxt = max(nxt, it->second - i); n         it++; n       } n       j = nxt; n       add++; n     } n     res += add; n     S.erase(pair<int, int>(-a[i] + i, i)); n     S.insert(pair<int, int>(n - 1 - a[i] + i + 1, n - 1 + i + 1)); n   } n   printf( %I64d n , res); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long n; n long long arr[1005]; n long long dp[1005][3 * 10000]; n long long add(long long a, long long b) { n   return ((a % 1000000007) + (b % 1000000007)) % 1000000007; n } n long long f(long long idx, long long sum) { n   if (idx == n + 1) { n     return 0; n   } n   if (dp[idx][sum] != -1) return dp[idx][sum]; n   long long res1 = sum + arr[idx]; n   long long res2 = sum - arr[idx]; n   long long val = 0; n   if (res1 == 10001 || res2 == 10001) val++; n   long long res = add(f(1 + idx, res1), f(1 + idx, res2)); n   res = add(res, val); n   return dp[idx][sum] = res; n } n int main() { n   scanf( %lld , &n); n   for (long long i = 1; i <= n; i++) scanf( %lld , &arr[i]); n   memset(dp, -1, sizeof dp); n   long long ans = 0; n   for (int i = 1; i <= n; i++) { n     ans = add(ans, f(i, 10001)); n   } n   cout << ans; n } n


#include <bits/stdc++.h> n using namespace std; n const long long md = 998244353; n int main() { n   long long n; n   cin >> n; n   vector<long long> a(n), b(n); n   for (int i = 0; i < n; i++) { n     cin >> a[i]; n     a[i] *= (n - i) * (i + 1); n   } n   for (int i = 0; i < n; i++) cin >> b[i]; n   sort(a.begin(), a.end()); n   sort(b.rbegin(), b.rend()); n   long long ans = 0; n   for (int i = 0; i < n; i++) (ans += (a[i] % md * b[i] % md) % md) %= md; n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 100005; n int a[N], c[N]; n int ans[N]; n struct qq { n   int x, y, id; n } s[N]; n int n, Q; n bool cmp(qq x, qq y) { return x.y < y.y; } n int q[N]; n int st, ed, now; n double Y(int x) { return a[x] * x - c[x]; } n double X(int x) { return -a[x]; } n double get_k(int x, int y) { n   return (double)(Y(x) - Y(y)) / (double)(X(x) - X(y)); n } n int calc(int xx, int x, int y) { return c[y] - c[xx] + a[xx] * (x - y + xx); } n void solve() { n   st = 1; n   ed = 0; n   now = 1; n   for (int u = 1; u <= n; u++) { n     while (st <= ed && a[q[ed]] >= a[u]) ed--; n     while (st < ed && get_k(q[ed - 1], q[ed]) < get_k(q[ed], u)) ed--; n     q[++ed] = u; n     while (now <= Q && s[now].y == u) { n       int l = st, r = ed, lalal; n       while (l <= r) { n         int mid = (l + r) >> 1; n         if (mid == ed || calc(q[mid], s[now].x, s[now].y) < n                              calc(q[mid + 1], s[now].x, s[now].y)) { n           lalal = mid; n           r = mid - 1; n         } else n           l = mid + 1; n       } n       ans[s[now].id] = calc(q[lalal], s[now].x, s[now].y); n       now++; n     } n   } n } n int read() { n   char ch = getchar(); n   int x = 0; n   while (ch <  0  || ch >  9 ) ch = getchar(); n   while (ch >=  0  && ch <=  9 ) { n     x = x * 10 + ch -  0 ; n     ch = getchar(); n   } n   return x; n } n int main() { n   n = read(); n   for (int u = 1; u <= n; u++) { n     a[u] = read(); n     c[u] = c[u - 1] + a[u]; n   } n   Q = read(); n   for (int u = 1; u <= Q; u++) { n     s[u].x = read(); n     s[u].y = read(); n     s[u].id = u; n   } n   sort(s + 1, s + 1 + Q, cmp); n   solve(); n   for (int u = 1; u <= Q; u++) printf( %d n , ans[u]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long MOD = 1000000007; n signed main() { n   cin.tie(0); n   ios::sync_with_stdio(false); n   long long N, a, b; n   cin >> N >> a >> b; n   vector<long long> A; n   A.push_back(a); n   A.push_back(a); n   A.push_back(a); n   A.push_back(a); n   A.push_back(b); n   A.push_back(b); n   sort(A.begin(), A.end()); n   long long res = 100000; n   long long k = 0; n   long long tres; n   do { n     k = 0; n     tres = 1; n     for (long long i = 0; i < A.size(); i++) { n       k += A[i]; n       if (k > N) { n         k = A[i]; n         tres++; n       } n     } n     res = min(res, tres); n   } while (next_permutation(A.begin(), A.end())); n   cout << res << endl; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 100005, S = 100; n int L[N], R[N], bel[N], to[N]; n long long a[N], b[N], taga[N], tagb[N]; n long double slope(int x, int y) { n   if (a[x] == a[y]) { n     if (b[x] == b[y]) return 1; n     return b[x] < b[y] ? 1e18 : -1e18; n   } n   return (long double)(b[y] - b[x]) / (a[y] - a[x]); n } n vector<int> s[N / S + 5]; n int pos[N / S + 5]; n void build(int x) { n   for (int i = L[x]; i <= R[x]; i++) b[to[i]] += a[to[i]] * taga[x] + tagb[x]; n   taga[x] = tagb[x] = 0; n   s[x].clear(); n   int top = 0; n   for (int i = L[x]; i <= R[x]; i++) { n     while (top > 1 && n            slope(s[x][top - 2], s[x][top - 1]) <= slope(s[x][top - 1], to[i])) n       --top, s[x].pop_back(); n     ++top, s[x].push_back(to[i]); n   } n   pos[x] = 0; n } n inline long long calc(int x) { n   return a[x] * taga[bel[x]] + b[x] + tagb[bel[x]]; n } n long long querymax(int x) { n   while (pos[x] < s[x].size() - 1) { n     if (calc(s[x][pos[x]]) > calc(s[x][pos[x] + 1])) break; n     pos[x]++; n   } n   return calc(s[x][pos[x]]); n } n bool cmp(const int& x, const int& y) { return a[x] < a[y]; } n int main() { n   int n; n   scanf( %d , &n); n   for (int i = 1; i <= n; i++) scanf( %lld , &a[i]), b[i] = a[i], to[i] = i; n   for (int i = 1; i <= n; i++) bel[i] = (i - 1) / S + 1; n   for (int i = 1; i <= bel[n]; i++) n     L[i] = S * (i - 1) + 1, R[i] = min(S * i, n), n     sort(to + L[i], to + R[i] + 1, cmp); n   for (int i = 1; i <= bel[n]; i++) build(i); n   long long ans = 0; n   for (int i = 1; i <= n; i++) { n     long long mx = -1ll << 60ll; n     int p; n     for (int j = 1; j <= bel[n]; j++) n       if (querymax(j) > mx) mx = querymax(j), p = s[j][pos[j]]; n     if (mx > 0 && p != 0) n       ans += mx; n     else n       break; n     b[p] = -1ll << 60ll; n     for (int j = 1; j < bel[p]; j++) tagb[j] += a[p]; n     for (int j = bel[p] + 1; j <= bel[n]; j++) taga[j]++; n     for (int j = L[bel[p]]; j < p; j++) b[j] += a[p]; n     for (int j = p + 1; j <= R[bel[p]]; j++) b[j] += a[j]; n     build(bel[p]); n   } n   printf( %lld , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long MOD = 1e9 + 7; n const long long MOD2 = 998244353; n const long long MOD3 = 1812447359; n const long long INF = 1ll << 62; n const double PI = 2 * asin(1); n void yes() { printf( yes n ); } n void no() { printf( no n ); } n void Yes() { printf( Yes n ); } n void No() { printf( No n ); } n void YES() { printf( YES n ); } n void NO() { printf( NO n ); } n struct SegmentTree { n   vector<int> Tree[21]; n   void Make() { n     for (int i = 0; i < 21; i++) { n       Tree[i].resize(1 << i); n     } n   } n   void Add(int num, int val) { n     for (int i = 20; i >= 0; i--) { n       Tree[i][num] += val; n       num /= 2; n     } n   } n   int Search(int num) { n     int now = 0, sum = 0; n     for (int i = 1; i < 21; i++) { n       now *= 2; n       if (sum + Tree[i][now] < num) { n         sum += Tree[i][now]; n         now++; n       } n     } n     return now; n   } n }; n int main() { n   int N, Q; n   scanf( %d%d , &N, &Q); n   SegmentTree T; n   T.Make(); n   for (int i = 0; i < N; i++) { n     int A; n     scanf( %d , &A); n     T.Add(A, 1); n   } n   for (int i = 0; i < Q; i++) { n     int K; n     scanf( %d , &K); n     if (K > 0) { n       T.Add(K, 1); n     } else { n       int num = T.Search(-1 * K); n       T.Add(num, -1); n     } n   } n   for (int i = 0; i <= 1e6; i++) { n     if (T.Tree[20][i] > 0) { n       cout << i << endl; n       return 0; n     } n   } n   cout << 0 << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 2e5 + 10; n int n, m, f[N]; n bool vis[2][N]; n vector<int> edges[2][N]; n void dfs0(int x) { n   if (vis[0][x]) return; n   vis[0][x] = 1; n   for (int to : edges[0][x]) { n     if (f[to] != 1) dfs0(to); n   } n } n void dfs1(int x) { n   if (vis[1][x]) return; n   vis[1][x] = 1; n   if (f[x] == 1) return; n   for (int to : edges[1][x]) { n     dfs1(to); n   } n } n void solve() { n   cin >> n >> m; n   for (int i = 1; i <= n; ++i) { n     cin >> f[i]; n   } n   for (int i = 1; i <= m; ++i) { n     int u, v; n     cin >> u >> v; n     edges[0][u].push_back(v); n     edges[1][v].push_back(u); n   } n   for (int i = 1; i <= n; ++i) { n     if (f[i] == 1) { n       dfs0(i); n     } n     if (f[i] == 2) { n       dfs1(i); n     } n   } n   for (int i = 1; i <= n; ++i) { n     cout << (vis[0][i] && vis[1][i]) <<    ; n   } n } n int main() { n   ios::sync_with_stdio(0); n   int T = 1; n   while (T--) { n     solve(); n   } n } n


#include <bits/stdc++.h> n using namespace std; n #pragma GCC optimize( O3 ) n template <class c> n struct rge { n   c b, e; n }; n template <class c> n rge<c> range(c i, c j) { n   return rge<c>{i, j}; n } n template <class c> n auto dud(c* x) -> decltype(cerr << *x, 0); n template <class c> n char dud(...); n struct debug { n   template <class c> n   debug& operator<<(const c&) { n     return *this; n   } n }; n int main(); n void file() {} n void time() {} n const int INF = 1e9 + 99; n using ll = long long int; n using pii = pair<int, int>; n using vec = vector<int>; n int main() { n   ios_base::sync_with_stdio(0); n   ; n   ll n; n   cin >> n; n   vector<ll> v(n); n   for (ll& x : v) { n     cin >> x; n   } n   sort(v.begin(), v.end()); n   ll i = 0, j = n - 1; n   ll x = 0, y = 0; n   bool ok = false; n   while (i <= j) { n     if (ok) { n       x += v[i]; n       i++; n     } else { n       y += v[j]; n       j--; n     } n     ok ^= 1; n   } n   ll ans = x * x + y * y; n   cout << ans << endl; n   return 0; n } n