#include <bits/stdc++.h> n using namespace std; n const int MAXN = 1e5 * 2 + 10; n int n, len, h; n struct node { n   int flag, mx, l, r; n } t[MAXN << 2]; n int a[MAXN], b[MAXN]; n int L[MAXN]; n void push_down(int k) { n   if (t[k].flag) { n     t[k << 1].flag += t[k].flag; n     t[k << 1 | 1].flag += t[k].flag; n     t[k << 1].mx += t[k].flag; n     t[k << 1 | 1].mx += t[k].flag; n     t[k].flag = 0; n   } n } n void push_up(int k) { t[k].mx = min(t[k << 1].mx, t[k << 1 | 1].mx); } n void build(int l, int r, int k) { n   t[k].l = l; n   t[k].r = r; n   t[k].flag = 0; n   if (t[k].l == t[k].r) { n     t[k].mx = -t[k].l; n     return; n   } n   int mid = t[k].l + t[k].r >> 1; n   build(l, mid, k << 1); n   build(mid + 1, r, k << 1 | 1); n   push_up(k); n } n void update(int l, int r, int val, int k) { n   if (l <= t[k].l && t[k].r <= r) { n     t[k].mx += val; n     t[k].flag += val; n     return; n   } n   push_down(k); n   int mid = t[k].l + t[k].r >> 1; n   if (l <= mid) update(l, r, val, k << 1); n   if (r > mid) update(l, r, val, k << 1 | 1); n   push_up(k); n } n int main() { n   scanf( %d%d%d , &n, &len, &h); n   for (int i = 1; i <= len; i++) scanf( %d , b + i); n   sort(b + 1, b + len + 1); n   build(1, len, 1); n   for (int i = 1; i <= n; i++) { n     scanf( %d , a + i); n     int ll = 1, rr = len, ans = -1; n     while (ll <= rr) { n       int mid = ll + rr >> 1; n       if (b[mid] + a[i] >= h) { n         ans = mid; n         rr = mid - 1; n       } else n         ll = mid + 1; n     } n     L[i] = ans; n   } n   int res = 0; n   for (int i = 1; i <= n; i++) { n     if (L[i] != -1) update(L[i], len, 1, 1); n     if (i >= len) { n       if (t[1].mx >= 0) res++; n       if (L[i - len + 1] != -1) update(L[i - len + 1], len, -1, 1); n     } n   } n   printf( %d n , res); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int cnt[1000000 + 1]; n const int mod = 1e9 + 7; n vector<int> v; n void solve() { n   int n; n   cin >> n; n   int a[n]; n   for (int i = 0; i < n; i++) cin >> a[i]; n   sort(a, a + n); n   int mx = a[n - 1]; n   deque<int> dq; n   int i = n - 1; n   for (i = n - 1; i >= 0; i--) { n     if (a[i] == mx) { n       dq.push_back(a[i]); n     } else n       break; n   } n   bool turn = true; n   for (i; i >= 0; i--) { n     if (turn == true) { n       dq.push_back(a[i]); n       turn = false; n     } else { n       dq.push_front(a[i]); n       turn = true; n     } n   } n   for (auto i : dq) cout << i <<    ; n   cout << endl; n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   ; n   int t; n   t = 1; n   while (t--) solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int INF = 0x3f3f3f3f; n const long long INFF = 0x3f3f3f3f3f3f3f3fll; n const long long M = 1e9 + 7; n const long long maxn = 1e5 + 7; n const double eps = 0.00000001; n long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; } n template <typename T> n inline T abs(T a) { n   return a > 0 ? a : -a; n } n int cnt[6]; n int a[6], b[6]; n int i, j, k; n int sum; n int num(int m, int n) { n   if (m * 2 > n) return 1; n   if (m * 4 > n) return 2; n   if (m * 8 > n) return 3; n   if (m * 16 > n) return 4; n   if (m * 32 > n) return 5; n   return 6; n } n int n, m; n bool judge(int x) { n   int sum = 0, i; n   for (i = 1; i <= 5; i++) { n     if (a[i] == 0 || a[i] > b[i]) n       sum += num(cnt[i], n + x) * (a[i] - b[i]); n     else n       sum += num(cnt[i] + x, n + x) * (a[i] - b[i]); n   } n   return sum > 0; n } n int main() { n   scanf( %d , &n); n   for (i = 1; i <= 5; i++) { n     scanf( %d , &k); n     if (k != -1) n       cnt[i]++, a[i] = 250 - k; n     else n       a[i] = 0; n   } n   for (i = 1; i <= 5; i++) { n     scanf( %d , &k); n     if (k != -1) n       cnt[i]++, b[i] = 250 - k; n     else n       b[i] = 0; n   } n   for (j = 0; j < n - 2; j++) n     for (i = 1; i <= 5; i++) { n       scanf( %d , &k); n       if (k != -1) cnt[i]++; n     } n   for (i = 0; i <= 10000; i++) n     if (judge(i)) { n       printf( %d , i); n       return 0; n     } n   puts( -1 ); n } n


#include <bits/stdc++.h> n using namespace std; n int n, m; n int used[1100][1100]; n vector<pair<int, int> > solve_l_r(int sx, int sy, int fx, int fy) { n   vector<pair<int, int> > ret; n   int len = abs(sx - fx) + 1; n   if ((fy - sy + 1) % 2 == 1) { n     int curx = sx, cury = sy; n     for (int i = 1; i <= fy - sy + 1; i++) { n       for (int j = 1; j < len; j++) { n         ret.push_back(make_pair(curx, cury)); n         if (i % 2 == 0) n           curx--; n         else n           curx++; n       } n       ret.push_back(make_pair(curx, cury)); n       cury++; n     } n   } else { n     int curx = sx, cury = sy; n     for (int i = 1; i <= fy - sy - 1; i++) { n       for (int j = 1; j < len; j++) { n         ret.push_back(make_pair(curx, cury)); n         if (i % 2 == 0) n           curx--; n         else n           curx++; n       } n       ret.push_back(make_pair(curx, cury)); n       cury++; n     } n     for (int i = 1; i <= len - 1; i++) { n       ret.push_back(make_pair(curx, cury)); n       if (i % 2 == 0) n         cury--; n       else n         cury++; n       ret.push_back(make_pair(curx, cury)); n       curx++; n     } n     if (curx == fx && cury == fy) n       ret.push_back(make_pair(curx, cury)); n     else n       ret.push_back(make_pair(curx, cury)), ret.push_back(make_pair(fx, fy)); n   } n   return ret; n } n vector<pair<int, int> > solve_r_l(int sx, int sy, int fx, int fy) { n   vector<pair<int, int> > ret; n   int len = abs(sx - fx) + 1; n   if ((sy - fy + 1) % 2 == 1) { n     int curx = sx, cury = sy; n     for (int i = 1; i <= sy - fy + 1; i++) { n       for (int j = 1; j < len; j++) { n         ret.push_back(make_pair(curx, cury)); n         if (i % 2 == 0) n           curx--; n         else n           curx++; n       } n       ret.push_back(make_pair(curx, cury)); n       cury--; n     } n   } else { n     int curx = sx, cury = sy; n     for (int i = 1; i <= sy - fy - 1; i++) { n       for (int j = 1; j < len; j++) { n         ret.push_back(make_pair(curx, cury)); n         if (i % 2 == 0) n           curx--; n         else n           curx++; n       } n       ret.push_back(make_pair(curx, cury)); n       cury--; n     } n     for (int i = 1; i <= len - 1; i++) { n       ret.push_back(make_pair(curx, cury)); n       if (i % 2 == 0) n         cury++; n       else n         cury--; n       ret.push_back(make_pair(curx, cury)); n       curx++; n     } n     if (curx == fx && cury == fy) n       ret.push_back(make_pair(curx, cury)); n     else n       ret.push_back(make_pair(curx, cury)), ret.push_back(make_pair(fx, fy)); n   } n   return ret; n } n vector<pair<int, int> > simple_l(int sx, int sy, int fx, int fy, int len) { n   vector<pair<int, int> > ret; n   if ((sx - fx + 1) % 2 == 0) { n     int curx = sx, cury = sy; n     for (int i = 1; i <= sx - fx + 1; i++) { n       for (int j = 1; j < len; j++) { n         ret.push_back(make_pair(curx, cury)); n         if (i % 2 == 0) n           cury++; n         else n           cury--; n       } n       ret.push_back(make_pair(curx, cury)); n       curx--; n     } n   } else { n     int curx = sx, cury = sy; n     for (int i = 1; i <= sx - fx - 1; i++) { n       for (int j = 1; j < len; j++) { n         ret.push_back(make_pair(curx, cury)); n         if (i % 2 == 0) n           cury++; n         else n           cury--; n       } n       ret.push_back(make_pair(curx, cury)); n       curx--; n     } n     for (int i = 1; i <= len - 1; i++) { n       ret.push_back(make_pair(curx, cury)); n       if (i % 2 == 0) n         curx++; n       else n         curx--; n       ret.push_back(make_pair(curx, cury)); n       cury++; n     } n     if (curx == fx && cury == fy) n       ret.push_back(make_pair(curx, cury)); n     else n       ret.push_back(make_pair(curx, cury)), ret.push_back(make_pair(fx, fy)); n   } n   return ret; n } n vector<pair<int, int> > simple_r(int sx, int sy, int fx, int fy, int len) { n   vector<pair<int, int> > ret; n   if ((sx - fx + 1) % 2 == 0) { n     int curx = sx, cury = sy; n     for (int i = 1; i <= sx - fx + 1; i++) { n       for (int j = 1; j < len; j++) { n         ret.push_back(make_pair(curx, cury)); n         if (i % 2 == 0) n           cury--; n         else n           cury++; n       } n       ret.push_back(make_pair(curx, cury)); n       curx--; n     } n   } else { n     int curx = sx, cury = sy; n     for (int i = 1; i <= sx - fx - 1; i++) { n       for (int j = 1; j < len; j++) { n         ret.push_back(make_pair(curx, cury)); n         if (i % 2 == 0) n           cury--; n         else n           cury++; n       } n       ret.push_back(make_pair(curx, cury)); n       curx--; n     } n     for (int i = 1; i <= len - 1; i++) { n       ret.push_back(make_pair(curx, cury)); n       if (i % 2 == 0) n         curx++; n       else n         curx--; n       ret.push_back(make_pair(curx, cury)); n       cury--; n     } n     if (curx == fx && cury == fy) n       ret.push_back(make_pair(curx, cury)); n     else n       ret.push_back(make_pair(curx, cury)), ret.push_back(make_pair(fx, fy)); n   } n   return ret; n } n vector<pair<int, int> > solve2(int sx, int sy, int fx, int fy) { n   vector<pair<int, int> > ret = simple_r(sy, sx, 1, sx, n - sx + 1); n   for (int i = 0; i < ret.size(); i++) swap(ret[i].first, ret[i].second); n   int curx = ret.back().first, cury = ret.back().second; n   curx--; n   vector<pair<int, int> > tek = solve_r_l(fx, fy, curx, cury); n   reverse(tek.begin(), tek.end()); n   for (int i = 0; i < tek.size(); i++) ret.push_back(tek[i]); n   return ret; n } n vector<pair<int, int> > solve(int sx, int sy, int fx, int fy) { n   vector<pair<int, int> > ret = solve2(sx, sy, 1, sy); n   int curx = 1, cury = sy + 1; n   for (int i = 1; i < fx; i++) { n     if (i % 2 == 1) { n       for (int j = sy + 1; j < m; j++) n         ret.push_back(make_pair(curx, cury)), cury++; n       ret.push_back(make_pair(curx, cury)); n       curx++; n     } else { n       for (int j = sy + 1; j < m; j++) n         ret.push_back(make_pair(curx, cury)), cury--; n       ret.push_back(make_pair(curx, cury)); n       curx++; n     } n   } n   int tp = -1; n   if (cury == sy + 1) { n     vector<pair<int, int> > tk = solve_l_r(curx, cury, n, fy - 1); n     for (int i = 0; i < tk.size(); i++) ret.push_back(tk[i]); n     tp = 1; n   } else { n     vector<pair<int, int> > tk = solve_r_l(curx, cury, n, fy + 1); n     for (int i = 0; i < tk.size(); i++) ret.push_back(tk[i]); n     tp = 0; n   } n   curx = n, cury = fy; n   if (tp) { n     vector<pair<int, int> > tk = simple_r(curx, cury, fx, fy, m - fy + 1); n     for (int i = 0; i < tk.size(); i++) ret.push_back(tk[i]); n   } else { n     vector<pair<int, int> > tk = simple_l(curx, cury, fx, fy, fy - sy); n     for (int i = 0; i < tk.size(); i++) ret.push_back(tk[i]); n   } n   return ret; n } n int fnd_ans(int n, int m, int sx, int st, int fx, int fy) { n   int col1 = ((sx + st + 1) % 2); n   int col2 = ((fx + fy + 1) % 2); n   int cntb = (n * m + 1) / 2; n   int cntw = (n * m) / 2; n   if (col1 == 1 && col2 == 1) return cntb * 2 - 1; n   if (col1 == 1 && col2 == 0) return cntw * 2; n   if (col1 == 0 && col2 == 1) return cntw * 2; n   if (col1 == 0 && col2 == 0) return 2 * cntw - 1; n } n int is_in(int x, int y) { return (1 <= x && x <= n && y >= 1 && y <= m); } n int check(vector<pair<int, int> > res, int sx, int sy, int fx, int fy) { n   for (int i = 1; i <= n; i++) n     for (int j = 1; j <= m; j++) used[i][j] = 0; n   if (!(sx == res[0].first && sy == res[0].second)) return 0; n   if (!(fx == res.back().first && fy == res.back().second)) return 0; n   for (int i = 0; i < res.size(); i++) n     if (!is_in(res[i].first, res[i].second)) return 0; n   for (int i = 1; i < res.size(); i++) { n     int sum = abs(res[i].first - res[i - 1].first) + n               abs(res[i].second - res[i - 1].second); n     if (sum != 1) return 0; n   } n   return 1; n } n vector<pair<int, int> > find_res(int x1, int y1, int x2, int y2, int ok) { n   vector<pair<int, int> > res; n   res = solve(x1, y1, x2, y2); n   if (res.size() == ok && check(res, x1, y1, x2, y2)) return res; n   swap(x1, x2), swap(y1, y2); n   res = solve(x1, y1, x2, y2); n   reverse(res.begin(), res.end()); n   swap(x1, x2), swap(y1, y2); n   if (res.size() == ok && check(res, x1, y1, x2, y2)) return res; n   swap(x1, y1), swap(x2, y2), swap(n, m); n   res = solve(x1, y1, x2, y2); n   for (int i = 0; i < res.size(); i++) swap(res[i].first, res[i].second); n   swap(x1, y1), swap(x2, y2), swap(n, m); n   if (res.size() == ok && check(res, x1, y1, x2, y2)) return res; n   swap(x1, y1), swap(x2, y2), swap(n, m), swap(x1, x2), swap(y1, y2); n   res = solve(x1, y1, x2, y2); n   for (int i = 0; i < res.size(); i++) swap(res[i].first, res[i].second); n   reverse(res.begin(), res.end()); n   swap(x1, y1), swap(x2, y2), swap(n, m), swap(x1, x2), swap(y1, y2); n   if (res.size() == ok && check(res, x1, y1, x2, y2)) return res; n   x1 = n + 1 - x1, x2 = n + 1 - x2; n   res = solve(x1, y1, x2, y2); n   for (int i = 0; i < res.size(); i++) res[i].first = n + 1 - res[i].first; n   x1 = n + 1 - x1, x2 = n + 1 - x2; n   if (res.size() == ok && check(res, x1, y1, x2, y2)) return res; n   x1 = n + 1 - x1, x2 = n + 1 - x2; n   swap(x1, x2), swap(y1, y2); n   res = solve(x1, y1, x2, y2); n   for (int i = 0; i < res.size(); i++) res[i].first = n + 1 - res[i].first; n   swap(x1, x2), swap(y1, y2); n   reverse(res.begin(), res.end()); n   x1 = n + 1 - x1, x2 = n + 1 - x2; n   if (res.size() == ok && check(res, x1, y1, x2, y2)) return res; n   y1 = m + 1 - y1, y2 = m + 1 - y2; n   res = solve(x1, y1, x2, y2); n   for (int i = 0; i < res.size(); i++) res[i].second = m + 1 - res[i].second; n   y1 = m + 1 - y1, y2 = m + 1 - y2; n   if (res.size() == ok && check(res, x1, y1, x2, y2)) return res; n   y1 = m + 1 - y1, y2 = m + 1 - y2; n   swap(x1, x2), swap(y1, y2); n   res = solve(x1, y1, x2, y2); n   for (int i = 0; i < res.size(); i++) res[i].second = m + 1 - res[i].second; n   y1 = m + 1 - y1, y2 = m + 1 - y2; n   reverse(res.begin(), res.end()); n   swap(x1, x2), swap(y1, y2); n   if (res.size() == ok && check(res, x1, y1, x2, y2)) return res; n   y1 = m + 1 - y1, y2 = m + 1 - y2; n   x1 = n + 1 - x1, x2 = n + 1 - x2; n   res = solve(x1, y1, x2, y2); n   for (int i = 0; i < res.size(); i++) n     res[i].second = m + 1 - res[i].second, res[i].first = n + 1 - res[i].first; n   y1 = m + 1 - y1, y2 = m + 1 - y2; n   x1 = n + 1 - x1, x2 = n + 1 - x2; n   if (res.size() == ok && check(res, x1, y1, x2, y2)) return res; n   y1 = m + 1 - y1, y2 = m + 1 - y2; n   x1 = n + 1 - x1, x2 = n + 1 - x2; n   swap(x1, x2), swap(y1, y2); n   res = solve(x1, y1, x2, y2); n   for (int i = 0; i < res.size(); i++) n     res[i].second = m + 1 - res[i].second, res[i].first = n + 1 - res[i].first; n   y1 = m + 1 - y1, y2 = m + 1 - y2; n   x1 = n + 1 - x1, x2 = n + 1 - x2; n   swap(x1, x2), swap(y1, y2); n   reverse(res.begin(), res.end()); n   if (res.size() == ok && check(res, x1, y1, x2, y2)) return res; n   swap(x1, y1), swap(x2, y2), swap(n, m); n   x1 = n + 1 - x1, x2 = n + 1 - x2; n   res = solve(x1, y1, x2, y2); n   for (int i = 0; i < res.size(); i++) n     res[i].first = n + 1 - res[i].first, swap(res[i].first, res[i].second); n   ; n   x1 = n + 1 - x1, x2 = n + 1 - x2; n   swap(x1, y1), swap(x2, y2), swap(n, m); n   if (res.size() == ok && check(res, x1, y1, x2, y2)) return res; n   swap(x1, y1), swap(x2, y2), swap(n, m); n   x1 = n + 1 - x1, x2 = n + 1 - x2; n   swap(x1, x2), swap(y1, y2); n   res = solve(x1, y1, x2, y2); n   for (int i = 0; i < res.size(); i++) n     res[i].first = n + 1 - res[i].first, swap(res[i].first, res[i].second); n   ; n   swap(x1, x2), swap(y1, y2); n   x1 = n + 1 - x1, x2 = n + 1 - x2; n   swap(x1, y1), swap(x2, y2), swap(n, m); n   reverse(res.begin(), res.end()); n   if (res.size() == ok && check(res, x1, y1, x2, y2)) return res; n   swap(x1, y1), swap(x2, y2), swap(n, m); n   y1 = m + 1 - y1, y2 = m + 1 - y2; n   res = solve(x1, y1, x2, y2); n   for (int i = 0; i < res.size(); i++) n     res[i].second = m + 1 - res[i].second, swap(res[i].first, res[i].second); n   ; n   y1 = m + 1 - y1, y2 = m + 1 - y2; n   swap(x1, y1), swap(x2, y2), swap(n, m); n   if (res.size() == ok && check(res, x1, y1, x2, y2)) return res; n   swap(x1, y1), swap(x2, y2), swap(n, m); n   y1 = m + 1 - y1, y2 = m + 1 - y2; n   swap(x1, x2), swap(y1, y2); n   res = solve(x1, y1, x2, y2); n   for (int i = 0; i < res.size(); i++) n     res[i].second = m + 1 - res[i].second, swap(res[i].first, res[i].second); n   ; n   y1 = m + 1 - y1, y2 = m + 1 - y2; n   swap(x1, x2), swap(y1, y2); n   swap(x1, y1), swap(x2, y2), swap(n, m); n   reverse(res.begin(), res.end()); n   if (res.size() == ok && check(res, x1, y1, x2, y2)) return res; n   swap(x1, y1), swap(x2, y2), swap(n, m); n   y1 = m + 1 - y1, y2 = m + 1 - y2; n   x1 = n + 1 - x1, x2 = n + 1 - x2; n   res = solve(x1, y1, x2, y2); n   for (int i = 0; i < res.size(); i++) n     res[i].second = m + 1 - res[i].second, res[i].first = n + 1 - res[i].first, n     swap(res[i].first, res[i].second); n   ; n   y1 = m + 1 - y1, y2 = m + 1 - y2; n   x1 = n + 1 - x1, x2 = n + 1 - x2; n   swap(x1, y1), swap(x2, y2), swap(n, m); n   if (res.size() == ok && check(res, x1, y1, x2, y2)) return res; n   swap(x1, y1), swap(x2, y2), swap(n, m); n   y1 = m + 1 - y1, y2 = m + 1 - y2; n   x1 = n + 1 - x1, x2 = n + 1 - x2; n   swap(x1, x2), swap(y1, y2); n   res = solve(x1, y1, x2, y2); n   for (int i = 0; i < res.size(); i++) n     res[i].second = m + 1 - res[i].second, res[i].first = n + 1 - res[i].first, n     swap(res[i].first, res[i].second); n   ; n   y1 = m + 1 - y1, y2 = m + 1 - y2; n   x1 = n + 1 - x1, x2 = n + 1 - x2; n   swap(x1, x2), swap(y1, y2); n   swap(x1, y1), swap(x2, y2), swap(n, m); n   reverse(res.begin(), res.end()); n   if (res.size() == ok && check(res, x1, y1, x2, y2)) return res; n   res.clear(); n   return res; n } n int main() { n   scanf( %d%d , &n, &m); n   int x1, y1, x2, y2; n   scanf( %d%d%d%d , &x1, &y1, &x2, &y2); n   vector<pair<int, int> > res = n       find_res(x1, y1, x2, y2, fnd_ans(n, m, x1, y1, x2, y2)); n   printf( %d n , res.size()); n   for (int i = 0; i < res.size(); i++) { n     printf( %d %d n , res[i].first, res[i].second); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int INF = 1E9 + 7; n template <class C> n void mini(C& a4, C b4) { n   a4 = min(a4, b4); n } n template <class C> n void maxi(C& a4, C b4) { n   a4 = max(a4, b4); n } n int n, k, a[57], dp[507][57][57], small, big, res; n bool is; n int dw[57][57]; n int main() { n   for (int n = 0; n < (57); ++n) { n     dw[n][0] = 1; n     for (int k = (1); k <= (n); ++k) n       dw[n][k] = (dw[n - 1][k - 1] + dw[n - 1][k]) % INF; n   } n   ios_base::sync_with_stdio(false); n   cout << setprecision(15) << fixed; n   cin >> n >> k; n   for (int i = 0; i < (n); ++i) cin >> a[i]; n   k /= 50; n   for (int i = 0; i < (n); ++i) (a[i] == 100 ? big : small)++; n   dp[0][0][0] = 1; n   for (int f = 0; f < (500); ++f) { n     for (int s = 0; s < (small + 1); ++s) n       for (int b = 0; b < (big + 1); ++b) n         if (dp[f][s][b]) { n           if (s == small && b == big) { n             cout << f << endl << dp[f][s][b] << endl; n             return 0; n           } n           int sogr = (f & 1 ? s : small - s); n           int bogr = (f & 1 ? b : big - b); n           for (int ss = 0; ss < (sogr + 1); ++ss) n             for (int bb = 0; bb < (bogr + 1); ++bb) n               if (ss + bb > 0 && ss + 2 * bb <= k) { n                 int news = s + ss * (f & 1 ? -1 : 1); n                 int newb = b + bb * (f & 1 ? -1 : 1); n                 long long ile = dp[f][s][b]; n                 ile *= dw[sogr][ss]; n                 ile %= INF; n                 ile *= dw[bogr][bb]; n                 ile %= INF; n                 dp[f + 1][news][newb] += ile; n                 dp[f + 1][news][newb] %= INF; n               } n         } n   } n   cout << -1 << endl << 0 << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long N = 2e5 + 7; n long long l[N], r[N]; n long long a[N]; n bool used[N]; n signed main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   long long n; n   cin >> n; n   string second; n   cin >> second; n   for (long long i = 0; i < n; ++i) cin >> a[i]; n   set<pair<long long, long long> > ms; n   for (long long i = 0; i < n; ++i) { n     l[i] = i - 1; n     r[i] = i + 1; n   } n   r[n] = n; n   for (long long i = 0; i < n - 1; ++i) { n     if (second[i] != second[i + 1]) { n       ms.insert(make_pair(abs(a[i] - a[i + 1]), i)); n     } n   } n   vector<pair<long long, long long> > ans; n   while (ms.size()) { n     long long i = ms.begin()->second; n     ms.erase(ms.begin()); n     if (used[i] || used[r[i]]) continue; n     ans.push_back(make_pair(i, r[i])); n     used[i] = used[r[i]] = 1; n     long long tl = l[i]; n     long long tr = r[r[i]]; n     ms.erase(make_pair(abs(a[tl] - a[i]), tl)); n     ms.erase(make_pair(abs(a[tr] - a[r[i]]), r[i])); n     if (tl != -1) r[tl] = tr; n     if (tr != n) l[tr] = tl; n     if (tl != -1 && tr != n && second[tl] != second[tr]) { n       ms.insert(make_pair(abs(a[tl] - a[tr]), tl)); n     } n   } n   cout << ans.size() <<   n ; n   for (auto e : ans) cout << e.first + 1 <<     << e.second + 1 <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long int n, t, c, i, x, suma, resultado; n   cin >> n >> t >> c; n   suma = 0; n   resultado = 0; n   for (i = 0; i < n; i++) { n     cin >> x; n     if (x > t) n       suma = 0; n     else n       suma++; n     if (suma >= c) resultado++; n   } n   cout << resultado; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   unsigned long long n, i, j, sum, k, temp; n   scanf( %llu , &n); n   unsigned long long a[n]; n   for (i = 0; i < n; i++) { n     scanf( %llu , &a[i]); n   } n   sum = 0; n   sort(a, a + n); n   k = n / 2; n   for (i = 0; i < k; i++) { n     temp = (a[i] + a[n - 1 - i]); n     sum += (temp * temp); n   } n   printf( %llu n , sum); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 100000, LOG = 20; n vector<vector<int>> g(N), p(N, vector<int>(LOG)); n vector<int> d(N), tin(N), tout(N); n int f = 1; n void dfs(int u, int pr, int depth) { n   p[u][0] = pr; n   tin[u] = f++; n   for (int i = 1; i < LOG; i++) { n     p[u][i] = p[p[u][i - 1]][i - 1]; n   } n   d[u] = depth; n   for (auto v : g[u]) { n     if (v != pr) { n       dfs(v, u, depth + 1); n     } n   } n   tout[u] = f++; n } n bool is_ancestor(int u, int v) { n   return tin[u] <= tin[v] && tout[u] >= tout[v]; n } n int lca(int u, int v) { n   if (is_ancestor(u, v)) { n     return u; n   } n   if (is_ancestor(v, u)) { n     return v; n   } n   for (int i = LOG - 1; i >= 0; i--) { n     if (!is_ancestor(p[u][i], v)) { n       u = p[u][i]; n     } n   } n   return p[u][0]; n } n int dist(int u, int v) { n   int a = lca(u, v); n   return d[u] + d[v] - 2 * d[a]; n } n int main() { n   int n; n   scanf( %d , &n); n   for (int i = 0; i < n - 1; i++) { n     int u, v; n     scanf( %d %d , &u, &v); n     g[--u].push_back(--v); n     g[v].push_back(u); n   } n   dfs(0, 0, 0); n   int q; n   scanf( %d , &q); n   for (int i = 0; i < q; i++) { n     int a, b, x, y, k; n     scanf( %d %d %d %d %d , &x, &y, &a, &b, &k); n     --a, --b, --x, --y; n     int distance = dist(a, b); n     if (distance <= k && distance % 2 == k % 2) { n       puts( yes ); n       continue; n     } n     distance = dist(a, x) + 1; n     distance += dist(b, y); n     if (distance <= k && distance % 2 == k % 2) { n       puts( yes ); n       continue; n     } n     distance = dist(a, y) + 1; n     distance += dist(b, x); n     if (distance <= k && distance % 2 == k % 2) { n       puts( yes ); n       continue; n     } n     puts( no ); n   } n } n


#include <bits/stdc++.h> n using namespace std; n vector<tuple<int, int, int, int> > ask; n int mat[105][105]; n int main() { n   int n, m, q, op, r, c, x, i, j; n   scanf( %d%d%d , &n, &m, &q); n   for (i = 0; i < q; i++) { n     scanf( %d , &op); n     if (op == 1 || op == 2) { n       scanf( %d , &x); n       ask.emplace_back(op, x, 0, 0); n     } else { n       scanf( %d%d%d , &r, &c, &x); n       ask.emplace_back(op, r, c, x); n     } n   } n   for (i = ask.size() - 1; i >= 0; i--) { n     tie(op, r, c, x) = ask[i]; n     if (op == 1) n       for (j = m - 1; j >= 1; j--) swap(mat[r][j + 1], mat[r][j]); n     else if (op == 2) n       for (j = n - 1; j >= 1; j--) swap(mat[j + 1][r], mat[j][r]); n     else n       mat[r][c] = x; n   } n   for (i = 1; i <= n; i++) { n     for (j = 1; j <= m; j++) printf( %d  , mat[i][j]); n     printf(  n ); n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   cin >> n; n   int arr[n]; n   for (int i = 1; i <= n; i++) cin >> arr[i]; n   int ans = 0, ct0 = 0, ct1; n   for (int i = 1; i <= n; i++) { n     if (arr[i] == 1) { n       ct1 = 0; n       for (int j = i; j <= n; j++) { n         if (arr[j] == 1) { n           ct1 += 1; n         } n       } n       ans = max(ans, ct0 + ct1); n     } else { n       ct0 += 1; n       ans = max(ans, ct0); n     } n   } n   cout << ans << endl; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 1e5 + 10; n int n, ch, x, y; n vector<int> path[maxn]; n vector<int> ans; n int main() { n   scanf( %d , &n); n   for (int i = 1; i < n; i++) { n     scanf( %d%d , &x, &y); n     path[x].push_back(y); n     path[y].push_back(x); n   } n   ch = 0; n   for (int i = 1; i <= n; i++) n     if (path[i].size() > 2) { n       if (ch == 0) n         ch = i; n       else { n         puts( No ); n         return 0; n       } n     } n   if (ch == 0) { n     puts( Yes ); n     puts( 1 ); n     x = y = 0; n     for (int i = 1; i <= n; i++) n       if (path[i].size() == 1) { n         if (x == 0) n           x = i; n         else n           y = i; n       } n     printf( %d %d n , x, y); n   } else { n     puts( Yes ); n     ans.clear(); n     for (int i = 1; i <= n; i++) n       if (path[i].size() == 1) ans.push_back(i); n     printf( %d n , ans.size()); n     for (int i = 0; i < ans.size(); i++) printf( %d %d n , ch, ans[i]); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n using ll = long long; n int const N = 200 * 1000 + 16; n int const M = 1000 * 1000 * 1000 + 7; n int n; n int a[320]; n int f[616]; n int ncr[616][616]; n int dp[616][616]; n struct dsu { n   int fa[500]; n   dsu(int n) { iota(fa, fa + n + 10, 0); } n   int find(int x) { return fa[x] == x ? x : fa[x] = find(fa[x]); } n   void unite(int x, int y) { fa[find(x)] = find(y); } n }; n int main() { n   cin.tie(0); n   cin.sync_with_stdio(0); n   cin >> n; n   for (int i = 0; i < n; ++i) cin >> a[i]; n   dsu u(n); n   for (int i = 0; i < n; ++i) n     for (int j = i + 1; j < n; ++j) { n       ll x = 1ll * a[i] * a[j]; n       ll s = sqrt(x) + 0.5; n       if (s * s == x) u.unite(i, j); n     } n   memset(a, 0, sizeof a); n   for (int i = 0; i < n; ++i) a[u.find(i)]++; n   sort(a, a + n, greater<int>()); n   int m = find(a, a + n, 0) - a; n   for (int i = 0; i < 616; ++i) ncr[i][0] = ncr[i][i] = 1; n   for (int i = 1; i < 616; ++i) n     for (int j = 1; j < i; ++j) n       ncr[i][j] = (ncr[i - 1][j] + ncr[i - 1][j - 1]) % M; n   f[0] = 1; n   for (int i = 1; i < 616; ++i) f[i] = 1ll * i * f[i - 1] % M; n   dp[0][a[0] - 1] = 1; n   int k = a[0]; n   for (int i = 1; i < m; ++i) { n     int len = a[i]; n     for (int p = 0; p < k; ++p) { n       int x = dp[i - 1][p]; n       if (not x) continue; n       for (int g = 1; g <= len; ++g) { n         for (int u = min({len, p, g}); u >= 0; --u) { n           ll y = 1ll * ncr[k - p + 1][g - u] * ncr[p][u] % M; n           y *= ncr[len - 1][g - 1]; n           y %= M; n           dp[i][p - u + len - g] += x * y % M; n           dp[i][p - u + len - g] %= M; n         } n       } n     } n     k += len; n   } n   int ans = dp[m - 1][0]; n   for (int i = 0; i < m; ++i) ans = 1ll * ans * f[a[i]] % M; n   cout << ans; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 111; n int a[N], b[N]; n int main() { n   cin.tie(0)->sync_with_stdio(0); n   int t; n   cin >> t; n   while (t--) { n     queue<int> que; n     int n; n     cin >> n; n     string S; n     cin >> S; n     S =     + S; n     for (int i = 1; i <= n; i++) { n       if (S[i] ==  R ) { n         a[i] = 1; n         b[i] = 1; n         que.push(i); n       } else if (S[i] ==  B ) { n         a[i] = 2; n         b[i] = 2; n         que.push(i); n       } else { n         a[i] = 3; n         b[i] = 3; n       } n     } n     if (que.empty()) { n       for (int i = 1; i <= n; i++) { n         if (i % 2 == 1) n           cout <<  R ; n         else n           cout <<  B ; n       } n       cout <<   n ; n       continue; n     } n     while (!que.empty()) { n       int x = que.front(); n       que.pop(); n       int left = x - 1; n       int right = x + 1; n       while (b[left] == 3 && left >= 1) { n         if (b[left + 1] == 1) { n           b[left] = 2; n           left--; n         } else { n           b[left] = 1; n           left--; n         } n       } n       while (b[right] == 3 && right <= n) { n         if (b[right - 1] == 1) { n           b[right] = 2; n           right++; n         } else { n           b[right] = 1; n           right++; n         } n       } n     } n     for (int i = 1; i <= n; i++) { n       if (b[i] == 1) n         cout <<  R ; n       else n         cout <<  B ; n     } n     cout <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long solve(int n) { n   if (n < 5) return n * (n - 1) / 2; n   long long ans = 0; n   int x = 0; n   for (int i = 0; i <= 8; i++) { n     int xx = 5 * pow(10, i); n     if (n < xx) { n       if (n < 2 * xx / 10) { n         if (n != 2 * xx / 10 - 1) n           return n - xx / 10 + 1; n         else n           return n - xx / 10; n       } n       x = i; n       break; n     } n     if (i == 8) { n       if (n < 1000000000) { n         if (n != 999999999) n           return n - 500000000 + 1; n         else n           return n - 500000000; n       } n       x = i + 1; n       break; n     } n   } n   int p = pow(10, x); n   ans += (n / p) * (n / p) * (p / 2); n   ans += (n % p) * (n / p); n   if (n % p >= p / 2) ans += (n % p) - p / 2 + 1; n   if (n % p == p - 1) ans--; n   return ans; n } n int main() { n   int n; n   cin >> n; n   long long ans = solve(n); n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 180 + 10; n const int oo = 1000000000; n int opt[maxn][maxn * 2]; n int c[maxn][maxn * 2], cc[maxn][maxn * 2]; n int e[maxn][maxn]; n int n, kk, d[maxn]; n int father[maxn]; n int res[maxn]; n void dp(int u) { n   for (int v = 1; v <= n; v++) n     if (e[u][v] && v == father[u]) e[u][v] = 0; n   for (int v = 1; v <= n; v++) n     if (e[u][v]) { n       father[v] = u; n       dp(v); n     } n   for (int i = 1; i <= n; i++) { n     int w = i - n, o = 0; n     for (int v = 1; v <= n; v++) n       if (e[u][v]) { n         int o2 = opt[v][i - 1]; n         for (int j = n; j <= n * 2; j++) o2 = min(o2, opt[v][j]); n         o += o2; n         o = min(o, oo); n       } n     if (w == 0) n       o += kk; n     else n       o += d[-w]; n     opt[u][i] = o; n   } n   int best[maxn]; n   for (int i = n + 1; i < n * 2; i++) { n     for (int v = 1; v <= n; v++) n       if (e[u][v]) { n         best[v] = oo; n         for (int j = i - 1; j <= n * 2; j++) best[v] = min(best[v], opt[v][j]); n         best[v] = min(best[v], opt[v][n + n - i - 1]); n       } n     for (int v = 1; v <= n; v++) n       if (e[u][v]) { n         int o = opt[v][i - 1]; n         for (int w = 1; w <= n; w++) n           if (w != v && e[u][w]) { n             o += best[w]; n             o = min(o, oo); n           } n         o += d[i - n]; n         if (o < opt[u][i]) { n           opt[u][i] = o; n           cc[u][i] = v; n         } n       } n   } n } n void dfs(int u, int w, int p) { n   if (w == n) p = u; n   if (w <= n) { n     res[u] = p; n     for (int v = 1; v <= n; v++) n       if (e[u][v]) { n         int o = w - 1; n         for (int j = n; j <= n * 2; j++) n           if (opt[v][j] < opt[v][o]) o = j; n         dfs(v, o, p); n       } n   } else { n     int o = cc[u][w]; n     dfs(o, w - 1, p); n     res[u] = res[o]; n     for (int v = 1; v <= n; v++) n       if (v != o && e[u][v]) { n         int _o = n + n - w - 1; n         for (int j = w - 1; j <= n * 2; j++) n           if (opt[v][j] < opt[v][_o]) _o = j; n         dfs(v, _o, res[u]); n       } n   } n } n int main() { n   scanf( %d%d , &n, &kk); n   for (int i = 1; i < n; i++) scanf( %d , &d[i]); n   for (int i = 1, u, v; i < n; i++) { n     scanf( %d%d , &u, &v); n     e[u][v] = e[v][u] = 1; n   } n   for (int i = 1; i <= n; i++) n     for (int j = 0; j <= n * 2 + 1; j++) opt[i][j] = oo; n   dp(1); n   int best = n; n   for (int i = n; i <= n * 2; i++) n     if (opt[1][i] < opt[1][best]) best = i; n   dfs(1, best, 0); n   printf( %d n , opt[1][best]); n   for (int i = 1; i <= n; i++) printf( %d%c , res[i], i == n ?   n  :    ); n } n


#include <bits/stdc++.h> n using namespace std; n int f(int x, int p2, int p) { return 2 * sqrt(p2 * p) + x + 1; } n int main() { n   printf( 302 n ); n   printf( 0 1000000 n ); n   int a = f(0, 1000000, 300); n   printf( %d 300 n , a); n   int kol = 2; n   for (int i = 299; i >= 1; --i) { n     printf( %d %d n , f(a, i + 1, i), i); n     a = f(a, i + 1, i); n     kol++; n   } n   printf( %d 1000000 n , f(a, 1000000, 1)); n   kol++; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n namespace Debug { n void __print(signed x) { cout << x; } n void __print(long x) { cout << x; } n void __print(long long x) { cout << x; } n void __print(unsigned x) { cout << x; } n void __print(unsigned long x) { cout << x; } n void __print(unsigned long long x) { cout << x; } n void __print(float x) { cout << x; } n void __print(double x) { cout << x; } n void __print(long double x) { cout << x; } n void __print(char x) { cout <<      << x <<     ; } n void __print(const char *x) { cout <<     << x <<    ; } n void __print(const string &x) { cout <<     << x <<    ; } n void __print(bool x) { cout << (x ?  true  :  false ); } n template <typename T, typename V> n void __print(const pair<T, V> &x) { n   cout <<  { ; n   __print(x.first); n   cout <<  , ; n   __print(x.second); n   cout <<  } ; n } n template <typename T> n void __print(const T &x) { n   long long f = 0; n   cout <<  { ; n   for (auto &i : x) cout << (f++ ?  ,  :   ), __print(i); n   cout <<  } ; n } n void _print() { cout <<  ]  << endl; } n template <typename T, typename... V> n void _print(T t, V... v) { n   __print(t); n   if (sizeof...(v)) cout <<  ,  ; n   _print(v...); n } n }  // namespace Debug n using namespace Debug; n const bool DEBUG = true; n long long calc_score(long long idx, long long n, long long k, n                      vector<long long> &arr, long long time_left) { n   long long score = 0ll; n   for (long long i = 0; i < k; i++) { n     for (long long j = idx; j <= n; j++) { n       if (time_left < arr[i]) return score; n       ++score; n       time_left -= arr[i]; n     } n   } n   return score; n } n void process() { n   long long n, k, M; n   cin >> n >> k >> M; n   vector<long long> arr(k); n   for (long long i = 0; i < k; i++) cin >> arr[i]; n   long long res = 0ll, sum = accumulate(arr.begin() + 0, arr.begin() + k, 0ll); n   sort(arr.begin() + 0, arr.begin() + k); n   for (long long i = 0; i <= n; i++) { n     if (sum * i > M) continue; n     long long score = i * (k + 1); n     long long time_left = M - (sum * i); n     score += calc_score(i + 1, n, k, arr, time_left); n     res = max(res, score); n   } n   cout << res <<   n ; n } n signed main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   ; n   long long t = 1; n   while (t--) process(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int arr[100005]; n int main() { n   int n, j = 1; n   scanf( %d , &n); n   int ara[n]; n   for (int i = 0; i < n; i++) { n     scanf( %d , &ara[i]); n     if (arr[ara[i]] != 0 || ara[i] > n) ara[i] = -1; n     arr[ara[i]]++; n   } n   for (int i = 0; i < n; i++) { n     if (ara[i] == -1) { n       while (arr[j] != 0) j++; n       ara[i] = j; n       arr[j] = 1; n     } n   } n   for (int i = 0; i < n; i++) printf( %d  , ara[i]); n } n


#include <bits/stdc++.h> n using namespace std; n struct node { n   bool lose, win; n   node *child[30]; n   node() { n     lose = true; n     win = false; n     for (int i = 0; i < 30; i++) child[i] = NULL; n   } n   void add(queue<char> q) { n     if (q.empty()) return; n     queue<char> p = q; n     char c = q.front(); n     q.pop(); n     int t = c -  a ; n     if (child[t] == NULL) { n       child[t] = new node(); n       lose = win = false; n     } n     child[t]->add(q); n   } n   void dfs() { n     bool p = true; n     for (int i = 0; i < 30; i++) n       if (child[i] != NULL) p = false; n     if (p) return; n     for (int i = 0; i < 30; i++) n       if (child[i] != NULL) { n         child[i]->dfs(); n         if (child[i]->win == false) win = true; n         if (child[i]->lose == false) lose = true; n       } n   } n }; n node *root; n int main() { n   root = new node(); n   int n, k; n   cin >> n >> k; n   for (int i = 0; i < n; i++) { n     string s; n     cin >> s; n     queue<char> q; n     for (int j = 0; j < s.length(); j++) q.push(s[j]); n     root->add(q); n   } n   root->dfs(); n   if (root->lose && root->win) n     cout <<  First  << endl; n   else if (!root->lose && !root->win) n     cout <<  Second  << endl; n   else if (root->lose) n     cout <<  Second  << endl; n   else if (k % 2) n     cout <<  First  << endl; n   else n     cout <<  Second  << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <class T> n void smin(T& a, T val) { n   if (a > val) a = val; n } n template <class T> n void smax(T& a, T val) { n   if (a < val) a = val; n } n template <typename T> n inline std::ostream& operator<<(std::ostream& os, const std::vector<T>& v) { n   bool first = true; n   os <<  [ ; n   for (auto i : v) { n     if (!first) os <<  ,  ; n     os << i; n     first = false; n   } n   return os <<  ] ; n } n const int MAX = 100010; n int n, u, e[MAX]; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   cin >> n >> u; n   cout << fixed << setprecision(20); n   for (int i = 0; i < n; ++i) { n     cin >> e[i]; n   } n   int l = 0, r = 0, tot = 0; n   long double ans1 = -1.0; n   while (l < n) { n     tot = 0; n     while (r < n and (e[r] - e[l]) <= u) { n       ++r; n     } n     if ((r - l) > 2) { n       int t = r - 1; n       ans1 = max(ans1, n                  (long double)(1. * (e[t] - e[l + 1])) / (1. * (e[t] - e[l]))); n     } n     ++l; n   } n   cout << ans1; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int a[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; n vector<int> parse(string s) { n   int i, dd, mm, yy; n   for (int i = 0; i < (int)(s.length()); i++) n     if (s[i] ==  . ) s[i] =    ; n   istringstream ss(s); n   ss >> dd >> mm >> yy; n   vector<int> ans; n   ans.push_back(dd); n   ans.push_back(mm); n   ans.push_back(yy); n   return ans; n } n bool legal(vector<int> v) { n   int d = v[0], m = v[1], y = v[2]; n   if (y % 4 == 0 && m == 2 && d == 29) return true; n   if (m <= 12 && d <= a[m - 1]) return true; n   return false; n } n bool func(vector<int> contest, vector<int> birth) { n   sort(birth.begin(), birth.end()); n   do { n     if (!legal(birth)) continue; n     if (birth[2] < contest[2] - 18) return true; n     if (birth[2] == contest[2] - 18) { n       if (birth[1] < contest[1]) return true; n       if (birth[1] == contest[1] && birth[0] <= contest[0]) return true; n     } n   } while (next_permutation(birth.begin(), birth.end())); n   return false; n } n int main(void) { n   string contest, birth; n   cin >> contest >> birth; n   bool ans = func(parse(contest), parse(birth)); n   cout << (ans ?  YES  :  NO ) << endl; n   return 0; n } n


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


#include <bits/stdc++.h> n inline int getInt() { n   int s; n   scanf( %d , &s); n   return s; n } n using namespace std; n struct Node { n   long long highest; n   int mxL; n   int mxR; n   int idxL; n   int idxR; n   Node(long long h, long long l, long long r, int il, int ir) n       : highest(h), mxL(l), mxR(r), idxL(il), idxR(ir) {} n }; n template <typename Data, typename Func> n class SegmentTree { n  public: n   int n; n   std::vector<Data> data; n   Func f; n   Data d; n   Data query(int a, int b, int k, int l, int r) const { n     if (r <= a || b <= l) return d; n     if (a <= l && r <= b) n       return data[k]; n     else { n       const Data v1 = query(a, b, k * 2 + 1, l, (l + r) / 2); n       const Data v2 = query(a, b, k * 2 + 2, (l + r) / 2, r); n       return f(v1, v2); n     } n   } n   SegmentTree(int nn, Func f, Data d) : f(f), d(d) { n     n = 1; n     while (n < nn) n *= 2; n     data = std::vector<Data>(2 * n, d); n   } n   void update(int k, const Data &a) { n     k += n - 1; n     data[k] = a; n     while (k > 0) { n       k = (k - 1) / 2; n       data[k] = f(data[k * 2 + 1], data[k * 2 + 2]); n     } n   } n   Data query(int a, int b) const { return query(a, b, 0, 0, n); } n }; n template <typename Data, typename Func> n SegmentTree<Data, Func> segtree(int numberOfElements, Data defaultValue, n                                 Func mergeFunc) { n   return SegmentTree<Data, Func>(numberOfElements, mergeFunc, defaultValue); n } n int main() { n   const int n = getInt(); n   const int m = getInt(); n   vector<long long> d(n); n   vector<long long> h(n); n   for (int i = 0; i < (int)(n); i++) d[i] = getInt(); n   for (int i = 0; i < (int)(n); i++) h[i] = getInt(); n   vector<long long> ps(2 * n + 1); n   for (int i = 0; i < (int)(2 * n); i++) ps[i + 1] = ps[i] + d[i % n]; n   const Node df(-1, 0, 0, 0, 0); n   auto calc = [&](int l, int r) { n     if (l == r) return 0ll; n     return ps[r] - ps[l] + 2 * (h[l % n] + h[r % n]); n   }; n   auto st = segtree(2 * n, df, [&](const Node &lhs, const Node &rhs) { n     if (lhs.highest == -1) return rhs; n     if (rhs.highest == -1) return lhs; n     const int idxL = lhs.idxL; n     const int idxR = rhs.idxR; n     const long long la = calc(idxL, lhs.mxL); n     const long long lb = calc(idxL, rhs.mxL); n     const long long lc = calc(idxL, rhs.idxL); n     const long long ml = max(max(la, lb), lc); n     const int mxL = lc > la && lc > lb ? rhs.idxL : la > lb ? lhs.mxL : rhs.mxL; n     const long long ra = calc(lhs.mxR, idxR); n     const long long rb = calc(rhs.mxR, idxR); n     const long long rc = calc(lhs.idxR, idxR); n     const long long mr = max(max(ra, rb), rc); n     const int mxR = rc > ra && rc > rb ? lhs.idxR : rb > ra ? rhs.mxR : lhs.mxR; n     const long long t = n         max(max(calc(lhs.mxR, rhs.mxL), calc(lhs.mxR, rhs.idxL)), n             max(calc(lhs.idxR, rhs.mxL), calc(lhs.idxR, rhs.idxL))); n     const long long highest = max( n         t, n         max(max(max(lhs.highest, rhs.highest), max(ml, mr)), calc(idxL, idxR))); n     return Node(highest, mxL, mxR, idxL, idxR); n   }); n   for (int i = 0; i < (int)(2 * n); i++) st.update(i, Node(0, i, i, i, i)); n   for (int i = 0; i < (int)(m); i++) { n     const int a = getInt() - 1; n     const int b = getInt() - 1; n     const int aa = b + 1; n     const int bb = a <= b ? a + n - 1 : a - 1; n     const Node ans = st.query(aa, bb + 1); n     cout << ans.highest << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int a[3222], b[32222]; n long long dp[3333][3333]; n int main() { n   int n; n   cin >> n; n   for (int i = 1; i <= n; i++) { n     int x; n     cin >> a[i]; n     a[i] -= i; n     b[i] = a[i]; n   } n   sort(b + 1, b + n + 1); n   for (int i = 1; i <= n; i++) { n     dp[i][1] = dp[i - 1][1] + abs(a[i] - b[1]); n     for (int j = 2; j <= n; j++) n       dp[i][j] = min(dp[i][j - 1], dp[i - 1][j] + abs(a[i] - b[j])); n   } n   cout << dp[n][n] << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int T, n, cnt; n struct poi { n   double x, y, alp; n   poi() {} n   poi(double x, double y) : x(x), y(y) { alp = atan2(y, x); } n   friend bool operator<(const poi &r1, const poi &r2) { n     return r1.alp < r2.alp; n   } n   friend poi operator+(const poi &r1, const poi &r2) { n     return poi(r1.x + r2.x, r1.y + r2.y); n   } n   friend poi operator-(const poi &r1, const poi &r2) { n     return poi(r1.x - r2.x, r1.y - r2.y); n   } n   friend double operator^(const poi &r1, const poi &r2) { n     return r1.x * r2.y - r2.x * r1.y; n   } n   friend double operator*(const poi &r1, const poi &r2) { n     return r1.x * r2.x + r1.y * r2.y; n   } n   friend poi operator*(double v, const poi &r1) { n     return poi(r1.x * v, r1.y * v); n   } n } a[210000 << 1], p, w[210000 << 2]; n struct line { n   poi p, v; n   line() {} n   line(poi p, poi v) : p(p), v(v) {} n } b[210000 << 2], q[210000 << 2]; n void init() { n   cnt = 0; n   double t = 1e6; n   poi p1(t, t), p2(-t, t), p3(-t, -t), p4(t, -t); n   b[++cnt] = line(p1, p2 - p1); n   b[++cnt] = line(p2, p3 - p2); n   b[++cnt] = line(p3, p4 - p3); n   b[++cnt] = line(p4, p1 - p4); n } n int insert(poi p1, poi p2) { n   double t = (p2 - p1) ^ (p - p1); n   if (fabs(t) < 1e-12) return 0; n   if (t > 0) n     b[++cnt] = line(p1, p2 - p1); n   else n     b[++cnt] = line(p2, p1 - p2); n   return 1; n } n int onleft(poi p1, line l1) { return (l1.v ^ (p1 - l1.p)) > 0; } n int cmp(const line &l1, const line &l2) { n   if (fabs(l1.v.alp - l2.v.alp) < 1e-12) return onleft(l1.p, l2); n   return l1.v.alp < l2.v.alp; n } n poi intersect(line l1, line l2) { n   poi u = l1.p - l2.p; n   double tmp = (u ^ l2.v) / (l2.v ^ l1.v); n   return l1.p + tmp * l1.v; n } n double HPI() { n   int h, r; n   sort(b + 1, b + 1 + cnt, cmp); n   q[h = r = 1] = b[1]; n   for (int i = 2; i <= cnt; i++) { n     if (fabs(b[i].v.alp - b[i - 1].v.alp) < 1e-12) continue; n     while (h < r && !onleft(w[r], b[i])) r--; n     while (h < r && !onleft(w[h + 1], b[i])) h++; n     q[++r] = b[i]; n     w[r] = intersect(q[r], q[r - 1]); n   } n   while (h < r && !onleft(w[r], q[h])) r--; n   w[h] = intersect(q[h], q[r]); n   if (h + 1 >= r) return 0; n   w[r + 1] = w[h]; n   double ret = 0; n   for (int i = h; i <= r; i++) ret += w[i] ^ w[i + 1]; n   return fabs(ret); n } n int main() { n   scanf( %d , &T); n   for (; T--;) { n     init(); n     scanf( %d , &n); n     n--; n     scanf( %lf%lf , &p.x, &p.y); n     for (int i = 1; i <= n; i++) { n       scanf( %lf%lf , &a[i].x, &a[i].y); n       a[i] = a[i] - p; n     } n     sort(a + 1, a + 1 + n); n     int flag = 0; n     for (int i = 1; i <= n; i++) { n       a[i + n] = a[i]; n       insert(a[i] + p, a[i + 1] + p); n       if (fabs(a[i].alp - a[i + 1].alp) < 1e-12) flag = 1; n     } n     for (int i = 1, j = 2; i <= n; i++) { n       for (; j < i + n && ((a[j] - a[i]) ^ a[i]) < 1e-12; j++) n         ; n       if (i != j - 1 && !insert(a[i] + p, a[j - 1] + p)) flag = 1; n     } n     if (flag) { n       puts( 0 ); n       continue; n     } n     printf( %.8lf n , HPI() / 2.0); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <class X> n void temfunc(const X a) { n   for (auto it = a.begin(); it != a.end(); it++) cout << *it <<    ; n   cout <<   n ; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   ; n   int t, l, r; n   cin >> t; n   while (t--) { n     cin >> l >> r; n     if (l == r) n       cout << 0 <<   n ; n     else { n       int temp; n       if (r & 1) { n         int temp = r / 2; n         if (temp >= l) n           cout << temp <<   n ; n         else { n           cout << r % l <<   n ; n         } n       } else { n         temp = r / 2; n         if (temp >= l) n           cout << temp - 1 <<   n ; n         else n           cout << r % l <<   n ; n       } n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, k; n   string s; n   cin >> n >> k; n   cin >> s; n   int j = 0; n   for (int i = 0; i < n - 1; i++) { n     if (s.substr(0, i + 1) == s.substr(n - i - 1)) { n       j = i + 1; n     } n   } n   string a = s.substr(j); n   cout << s; n   for (int i = 1; i < k; i++) { n     cout << a; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   cin >> n; n   int ns = 0; n   bool ok = true; n   for (int i = 0; i < n; i++) { n     int val; n     cin >> val; n     string type; n     cin >> type; n     if (type ==  South ) { n       if (ns + val > 20000) n         ok &= false; n       else n         ns += val; n     } else if (type ==  North ) { n       if (ns - val < 0) n         ok &= false; n       else n         ns -= val; n     } else { n       if (ns == 20000 || ns == 0) ok &= false; n     } n   } n   ok &= ns == 0; n   cout << (ok ?  YES n  :  NO n ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 2e5 + 10; n const int mod = 1e9 + 7; n int n, q; n long long a[N], w[N], c[N << 1][2]; n inline void add(int x, long long v, int id) { n   if (!id) n     for (; x <= n; x += x & -x) c[x][id] += v; n   else n     for (; x <= n; x += x & -x) c[x][id] = (c[x][id] + v + mod) % mod; n } n inline long long ask(int x, int id) { n   long long ret = 0; n   if (!id) n     for (; x; x -= x & -x) ret += c[x][id]; n   else n     for (; x; x -= x & -x) ret = (ret + c[x][id] + mod) % mod; n   return ret; n } n inline long long gets(int x, int y, int id) { n   if (!id) n     return y >= x ? ask(y, id) - ask(x - 1, id) : 0; n   else n     return (ask(y, id) - ask(x - 1, id) + mod) % mod; n } n inline int bsearch(int x, int y) { n   int l = x, r = y, mid; n   while (l < r) { n     mid = (l + r) / 2; n     if (gets(x, mid, 0) >= gets(mid + 1, y, 0)) n       r = mid; n     else n       l = mid + 1; n   } n   return r; n } n int main() { n   scanf( %d%d , &n, &q); n   for (register int i = 1; i <= n; i++) scanf( %lld , &a[i]); n   for (register int i = 1; i <= n; i++) n     scanf( %lld , &w[i]), add(i, w[i], 0), add(i, w[i] * (a[i] - i), 1); n   while (q--) { n     int x, y; n     scanf( %d%d , &x, &y); n     if (x < 0) { n       x = -x; n       add(x, -w[x], 0); n       add(x, -w[x] * (a[x] - x), 1); n       w[x] = y; n       add(x, w[x], 0); n       add(x, w[x] * (a[x] - x), 1); n     } else { n       if (x == y) { n         puts( 0 ); n         continue; n       } n       long long pos = bsearch(x, y), ans = 0; n       ans = (ans + (gets(x, pos, 0) % mod) * (int)abs(a[pos] - pos) % mod - n              gets(x, pos, 1) + mod) % n             mod; n       ans = (ans - (gets(pos, y, 0) % mod) * (int)abs(a[pos] - pos) % mod + n              gets(pos, y, 1) + mod) % n             mod; n       printf( %lld n , ans); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 2e5 + 5; n int n, k, a[N]; n int main() { n   scanf( %d%d , &n, &k); n   for (int i = 1; i <= n; i++) scanf( %d , &a[i]); n   sort(a + 1, a + 1 + n); n   int p = n / 2 + 1, t = n / 2 + 1; n   for (int i = n / 2 + 2; i <= n; i++) n     if (1ll * (a[i] - a[i - 1]) * (i - t) > k) n       break; n     else { n       k -= 1ll * (a[i] - a[i - 1]) * (i - t); n       p = i; n     } n   printf( %d n , a[p] + k / (p - t + 1)); n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 5, maxm = 100010; n int n, m, h[maxm][maxn], v[maxm][maxn]; n long long f[maxm][1 << maxn]; n int main() { n   scanf( %d%d , &n, &m); n   for (int i = 0; i < m - 1; i++) n     for (int j = 0; j < n; j++) scanf( %d , &h[i][j]); n   for (int i = 0; i < m; i++) n     for (int j = 0; j < n; j++) scanf( %d , &v[i][j]); n   for (int i = 1; i < 1 << n; i++) f[0][i] = 1LL << 60LL; n   f[0][0] = 0; n   for (int i = 1; i < m; i++) n     for (int opt = 0; opt < 1 << n; opt++) { n       f[i][opt] = 1LL << 60LL; n       long long s = 0; n       for (int j = 0; j < n; j++) n         s += ((opt & (1 << ((j + 1) % n))) > 0 != (opt & (1 << j)) > 0) * n              v[i][j]; n       for (int opt0 = 0; opt0 < 1 << n; opt0++) { n         long long ss = 0; n         for (int j = 0; j < n; j++) n           ss += ((opt & (1 << j)) != (opt0 & (1 << j))) * h[i - 1][j]; n         f[i][opt] = min(f[i][opt], f[i - 1][opt0] + s + ss); n       } n     } n   printf( %I64d n , f[m - 1][(1 << n) - 1]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void dfs(int node, int par, int gp, int p, vector<int> adj[], vector<int> &src, n          vector<int> &dest, vector<int> &ans) { n   if (gp % 2 != 0) { n     src[node - 1] = !src[node - 1]; n   } n   if (src[node - 1] != dest[node - 1]) { n     ans.push_back(node); n     int temp = p; n     p = gp + 1; n     gp = temp; n   } else { n     int temp = p; n     p = gp; n     gp = temp; n   } n   for (int i = 0; i < adj[node].size(); i++) { n     if (adj[node][i] != par) { n       dfs(adj[node][i], node, gp, p, adj, src, dest, ans); n     } n   } n } n int main() { n   int n; n   cin >> n; n   vector<int> adj[n + 1]; n   for (int i = 0; i < n - 1; i++) { n     int u, v; n     cin >> u >> v; n     adj[u].push_back(v); n     adj[v].push_back(u); n   } n   vector<int> src(n); n   vector<int> dest(n); n   for (int i = 0; i < n; i++) cin >> src[i]; n   for (int i = 0; i < n; i++) cin >> dest[i]; n   vector<int> ans; n   dfs(1, -1, 0, 0, adj, src, dest, ans); n   cout << ans.size() << endl; n   for (int i = 0; i < ans.size(); i++) cout << ans[i] << endl; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 200010; n int n, m; n int a[105][105]; n int ans, row[105], col[105]; n void caluRow() { n   for (int i = 1; i <= n; i++) { n     int mi = 501; n     for (int j = 1; j <= m; j++) mi = min(mi, a[i][j]); n     if (mi > 0) { n       ans += mi; n       row[i] += mi; n       for (int j = 1; j <= m; j++) a[i][j] -= mi; n     } n   } n } n void caluCol() { n   for (int i = 1; i <= m; i++) { n     int mi = 501; n     for (int j = 1; j <= n; j++) mi = min(mi, a[j][i]); n     if (mi > 0) { n       ans += mi; n       col[i] += mi; n       for (int j = 1; j <= n; j++) a[j][i] -= mi; n     } 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++) scanf( %d , &a[i][j]); n   if (m > n) { n     caluRow(); n     caluCol(); n   } else { n     caluCol(); n     caluRow(); n   } n   for (int i = 1; i <= n; i++) n     for (int j = 1; j <= m; j++) n       if (a[i][j]) { n         printf( -1 ); n         return 0; n       } n   printf( %d n , ans); n   for (int i = 1; i <= n; i++) n     while (row[i]--) printf( row %d n , i); n   for (int i = 1; i <= m; i++) n     while (col[i]--) printf( col %d n , i); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long a[4 * 100005]; n int main() { n   int n; n   cin >> n; n   for (int i = 0; i < n; i++) scanf( %lld , &a[i]); n   sort(a, a + n); n   int i = 0, j = n - 1; n   long long ans = 0; n   while (i < j) { n     ans += (a[i] + a[j]) * (a[i] + a[j]); n     i++; n     j--; n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<int> g[5005]; n int h[5005][5005], siz[5005]; n int f[5005][5005], fa; n int n, m, a[5005], b[5005], k; n inline int min(const int x, const int y) { return x > y ? y : x; } n void dfs(int x) { n   int to; n   siz[x] = 1; n   h[x][1] = b[x]; n   f[x][0] = 0, f[x][1] = a[x]; n   for (int i = g[x].size() - 1; i >= 0; i--) { n     to = g[x][i]; n     dfs(to); n     for (int j = siz[x]; j >= 0; j--) { n       for (int u = 1; u <= siz[to]; u++) { n         if (f[to][u] > k && h[to][u] > k) break; n         if (f[to][u] <= k) { n           h[x][j + u] = min(h[x][j + u], f[to][u] + h[x][j]); n           f[x][j + u] = min(f[x][j + u], f[to][u] + f[x][j]); n         } n         if (h[to][u] <= k) h[x][j + u] = min(h[x][j + u], h[to][u] + h[x][j]); n       } n     } n     siz[x] += siz[to]; n   } n } n int main() { n   memset(h, 0x3f, sizeof(h)); n   memset(f, 0x3f, sizeof(f)); n   scanf( %d%d , &n, &k); n   for (int i = 1; i <= n; i++) { n     scanf( %d%d , a + i, b + i); n     b[i] = a[i] - b[i]; n     if (i > 1) { n       scanf( %d , &fa); n       g[fa].push_back(i); n     } n   } n   dfs(1); n   for (int i = n; i >= 0; i--) n     if (h[1][i] <= k || f[1][i] <= k) { n       printf( %d n , i); n       return 0; n     } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct Result { n   int u, v, w, z; n }; n int N, M; n vector<vector<int>> edges, distances; n vector<vector<pair<int, int>>> D, R; n vector<int> enqueued; n int best = -1; n Result result; n int scan() { n   auto c = getchar(); n   while (c <  0  || c >  9 ) c = getchar(); n   int n; n   for (n = 0;  0  <= c && c <=  9 ; c = getchar()) n = n * 10 + c -  0 ; n   return n; n } n void bfs(int start) { n   queue<pair<int, int>> q; n   q.push({start, 0}); n   enqueued[start] = start; n   while (!q.empty()) { n     auto &p = q.front(); n     q.pop(); n     D[start].push_back(p); n     R[p.first].push_back({start, p.second}); n     distances[start][p.first] = p.second; n     for (auto &v : edges[p.first]) { n       if (enqueued[v] != start) { n         q.push({v, p.second + 1}); n         enqueued[v] = start; n       } n     } n   } n } n inline bool cmp(const pair<int, int> &a, const pair<int, int> &b) { n   return a.second > b.second; n } n pair<int, int> find(const vector<pair<int, int>> &X, int a, int b, int c) { n   for (auto &x : X) { n     if (x.first != a && x.first != b && x.first != c) return x; n   } n   return {-1, -1}; n } n void process() { n   for (auto i = 0; i < N; i++) { n     for (auto j = 0; j < N; j++) { n       if (i == j || distances[i][j] == 0) continue; n       do { n         auto k = find(R[i], i, j, -1); n         if (k.first == -1) break; n         auto l = find(D[j], i, j, k.first); n         if (l.first == -1) break; n         auto value = k.second + l.second + distances[i][j]; n         if (value > best) { n           best = value; n           result = {k.first, i, j, l.first}; n         } n       } while (false); n       do { n         auto l = find(D[j], i, j, -1); n         if (l.first == -1) break; n         auto k = find(R[i], i, j, l.first); n         if (k.first == -1) break; n         auto value = k.second + l.second + distances[i][j]; n         if (value > best) { n           best = value; n           result = {k.first, i, j, l.first}; n         } n       } while (false); n     } n   } n } n int main() { n   N = scan(); n   M = scan(); n   edges.resize(N); n   distances.resize(N); n   D.resize(N); n   R.resize(N); n   enqueued.resize(N); n   fill(enqueued.begin(), enqueued.end(), -1); n   for (auto i = 0; i < M; i++) { n     auto x = scan() - 1; n     auto y = scan() - 1; n     edges[x].push_back(y); n   } n   for (auto i = 0; i < N; i++) { n     vector<int> &v = edges[i]; n     sort(v.begin(), v.end()); n     v.resize(distance(v.begin(), unique(v.begin(), v.end()))); n     distances[i].resize(N); n   } n   for (auto i = 0; i < N; i++) bfs(i); n   for (auto i = 0; i < N; i++) { n     sort(D[i].begin(), D[i].end(), cmp); n     sort(R[i].begin(), R[i].end(), cmp); n   } n   process(); n   printf( %d %d %d %d n , result.u + 1, result.v + 1, result.w + 1, n          result.z + 1); n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 200005; n int n, T, a, b; n int diff[N]; n pair<int, int> prob[N]; n void solve() { n   scanf( %d %d %d %d , &n, &T, &a, &b); n   int allA = 0, allB = 0; n   for (int i = 0; i < n; ++i) { n     scanf( %d , &diff[i]); n     if (diff[i]) n       ++allB; n     else n       ++allA; n   } n   for (int i = 0; i < n; ++i) { n     scanf( %d , &prob[i].first); n     prob[i].second = i; n   } n   if (1LL * allA * a + 1LL * allB * b <= T) { n     printf( %d n , n); n     return; n   } n   sort(prob, prob + n); n   int ans = 0; n   int mustA = 0, mustB = 0; n   for (int i = 0; i < n;) { n     int dead = prob[i].first; n     long long taken = 1LL * mustA * a + 1LL * mustB * b; n     if (taken < dead) { n       int tmp = mustA + mustB; n       int left = dead - taken - 1; n       int moreA = min(left / a, allA - mustA); n       tmp += moreA; n       left -= moreA * a; n       int moreB = min(left / b, allB - mustB); n       tmp += moreB; n       left -= moreB * b; n       ans = max(ans, tmp); n     } n     int j = i; n     while (j < n && prob[j].first == dead) { n       if (diff[prob[j].second]) n         ++mustB; n       else n         ++mustA; n       ++j; n     } n     i = j; n   } n   printf( %d n , ans); n } n int main() { n   int t; n   scanf( %d , &t); n   while (t--) solve(); n   return 0; n } n


#include <bits/stdc++.h> n #pragma comment(linker,  /STACK:102400000,102400000 ) n using namespace std; n vector<int> G[20000]; n int fa[20000], c[20000], ans = 0; n void dfs(int x, int f, int col, int ori) { n   if (col != ori) ans++; n   for (int i = 0; i < G[x].size(); i++) { n     int u = G[x][i]; n     if (u == f) continue; n     dfs(u, x, c[u], col); n   } n } n int main() { n   std::ios::sync_with_stdio(0), cin.tie(0); n   int n, x; n   cin >> n; n   for (int i = 1; i <= n; i++) G[i].clear(); n   for (int i = 1; i < n; i++) { n     cin >> x; n     G[i + 1].push_back(x); n     G[x].push_back(i + 1); n   } n   for (int i = 1; i <= n; i++) cin >> c[i]; n   dfs(1, -1, c[1], 0); n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n string s; n map<int, int> dp; n int k; n int n; n int dp1[1001][1001][2]; n int go(int t) { n   if (t == 1) return 0; n   int cnt = 0; n   while (t) { n     cnt += (t % 2); n     t /= 2; n   } n   return go(cnt) + 1; n } n int solve(int i, int pre, int q) { n   if (i >= n) { n     return dp[pre] == k; n   } n   if (dp1[i][pre][q] != -1) return dp1[i][pre][q]; n   if (q) { n     return dp1[i][pre][1] = n                ((solve(i + 1, pre + 1, 1) % (long long)1000000007) + n                 (solve(i + 1, pre, 1) % (long long)1000000007)) % n                (long long)1000000007; n   } n   if (s[i] ==  1 ) { n     return dp1[i][pre][0] = n                ((solve(i + 1, pre + 1, 0) % (long long)1000000007) + n                 (solve(i + 1, pre, 1) % (long long)1000000007)) % n                (long long)1000000007; n   } n   return dp1[i][pre][0] = solve(i + 1, pre, 0) % (long long)1000000007; n } n int main() { n   ios::sync_with_stdio(0), cin.tie(0), cout.tie(0); n   cin >> s >> k; n   n = s.size(); n   memset(dp1, -1, sizeof(dp1)); n   dp[1] = 1; n   dp[0] = 0; n   for (int i = 2; i <= 1000; i++) { n     dp[i] = go(i) + 1; n   } n   cout << solve(0, 0, 0) - (k == 1 ? 1 : 0); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long n, a[1000005]; n std::vector<pair<double, pair<long long, long long> > > v; n signed main() { n   scanf( %lld , &n); n   for (long long i = 1; i <= n; i++) { n     scanf( %lld , &a[i]); n   } n   for (long long i = 1; i <= n; i++) { n     v.push_back(make_pair(1.0 * a[i], make_pair(i, i))); n     while (v.size() > 1 && v[v.size() - 1].first <= v[v.size() - 2].first) { n       double val1 = v[v.size() - 1].first; n       double val2 = v[v.size() - 2].first; n       pair<long long, long long> interval1 = v[v.size() - 1].second; n       pair<long long, long long> interval2 = v[v.size() - 2].second; n       v.pop_back(); n       v.pop_back(); n       v.push_back(make_pair((val1 * (interval1.second - interval1.first + 1) + n                              val2 * (interval2.second - interval2.first + 1)) / n                                 (interval1.second - interval2.first + 1), n                             make_pair(interval2.first, interval1.second))); n     } n   } n   for (long long i = 0; i < v.size(); i++) { n     pair<long long, long long> interval = v[i].second; n     for (long long j = interval.first; j <= interval.second; j++) { n       printf( %.10lf n , v[i].first); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n map<char, int> mp; n char t[27]; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   long long n, mx = 0, ans = 0, f, f1; n   string ch; n   cin >> n; n   for (int i = 0; i < n; i++) { n     cin >> ch; n     t[ch[0] -  a ]++; n   } n   for (int i = 0; i < 27; i++) { n     mx = t[i]; n     f = mx / 2; n     if (mx % 2 == 0) n       f1 = mx / 2; n     else n       f1 = mx / 2 + 1; n     ans += (f1 * (f1 - 1) / 2) + (f * (f - 1) / 2); n   } n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 3000 + 5; n struct node { n   int val, id; n } a[N]; n int n; n int dp[N * N], tree[N], ranks[N]; n inline int lowbit(int x) { return x & -x; } n inline int query(int x) { n   int sum = 0; n   while (x != 0) { n     sum += tree[x]; n     x -= lowbit(x); n   } n   return sum; n } n inline void update(int x, int v) { n   while (x <= n) { n     tree[x] += v; n     x += lowbit(x); n   } n   return; n } n bool cmp(node a, node b) { n   if (a.val == b.val) return a.id < b.id; n   return a.val < b.val; n } n signed main() { n   cin >> n; n   for (int i = 1; i <= n; i++) { n     cin >> a[i].val; n     a[i].id = i; n   } n   sort(a + 1, a + n + 1, cmp); n   for (int i = 1; i <= n; i++) { n     ranks[a[i].id] = i; n   } n   int ans = 0; n   for (int i = 1; i <= n; i++) { n     ans += (i - 1) - query(ranks[i]); n     update(ranks[i], 1); n   } n   printf( %.6lf , (double)(2 * ans - (ans & 1))); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long int n, a, b, k1, k2, m1 = 0, m2 = 0; n   string s; n   cin >> n >> a >> b >> s; n   a--; n   b--; n   cout << abs(s[a] - s[b]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int N, M; n vector<vector<char>> v; n bool Mastervis[50][50]; n bool vis[50][50]; n int dx[] = {0, 0, 1, -1}; n int dy[] = {1, -1, 0, 0}; n bool isCyc(int r, int c, int pr, int pc) { n   if (vis[r][c]) return true; n   vis[r][c] = true; n   Mastervis[r][c] = true; n   int nr, nc; n   for (int k = 0; k < 4; k++) { n     nr = r + dy[k]; n     nc = c + dx[k]; n     if (nr >= 0 && nr < N && nc >= 0 && nc < M && !(nr == pr && nc == pc)) { n       if (v[r][c] == v[nr][nc]) { n         if (isCyc(nr, nc, r, c)) return true; n       } n     } n   } n   return false; n } n int main() { n   string s; n   cin >> N >> M; n   v.resize(N); n   memset(Mastervis, false, 50 * 50 * sizeof(bool)); n   for (int i = 0; i < N; i++) { n     cin >> s; n     for (int j = 0; j < M; j++) { n       v[i].push_back(s[j]); n     } n   } n   for (int i = 0; i < N; i++) { n     for (int j = 0; j < M; j++) { n       if (!Mastervis[i][j]) { n         memset(vis, false, 50 * 50 * sizeof(bool)); n         if (isCyc(i, j, -2, -2)) { n           cout <<  Yes ; n           return 0; n         } n       } n     } n   } n   cout <<  No ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long t; n   cin >> t; n   while (t--) { n     long long N; n     cin >> N; n     vector<long long> a(N); n     for (auto &x : a) cin >> x; n     ; n     long long l = -1; n     for (int i = 0; i < N - 1; i++) { n       if (abs(a[i + 1] - a[i]) >= 2) { n         l = i; n         break; n       } n     } n     if (l == -1) n       cout <<  No  << endl; n     else n       cout <<  Yes n  << l + 1 <<     << l + 2 << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 1 << 21; n int n, m; n char s[maxn]; n char cmd[maxn]; n struct lucky { n   int c4, c7, c47, c74; n   lucky() { c4 = 0, c7 = 0, c47 = 0, c74 = 0; } n   void merge(lucky a, lucky b) { n     c4 = a.c4 + b.c4; n     c7 = a.c7 + b.c7; n     c47 = max(a.c47 + b.c7, a.c4 + b.c47); n     c74 = max(a.c74 + b.c4, a.c7 + b.c74); n   } n   void reset(char c) { n     if (c ==  7 ) n       c7 = 1; n     else n       c4 = 1; n     c47 = 1; n     c74 = 1; n   } n   void chng() { n     swap(c4, c7); n     swap(c47, c74); n   } n   void recieve(bool bb) { n     if (bb) chng(); n   } n }; n lucky seg[maxn]; n bool lazy[maxn]; n lucky build(int rt, int lp, int rp) { n   if (lp + 1 == rp) { n     seg[rt].reset(s[lp]); n   } else { n     int mid = (lp + rp) / 2; n     seg[rt].merge(build(rt * 2, lp, mid), build(rt * 2 + 1, mid, rp)); n   } n   return seg[rt]; n } n int qa, qb; n lucky update(int rt, int lp, int rp) { n   if (lp >= qb || rp <= qa) return seg[rt]; n   if (qa <= lp && rp <= qb) { n     lazy[rt] ^= true; n     seg[rt].chng(); n   } else { n     int mid = (lp + rp) / 2; n     seg[rt].merge(update(rt * 2, lp, mid), update(rt * 2 + 1, mid, rp)); n     seg[rt].recieve(lazy[rt]); n   } n   return seg[rt]; n } n int main() { n   scanf( %d%d%s , &n, &m, s); n   build(1, 0, n); n   for (int i = 0; i < m; i++) { n     scanf( %s , cmd); n     if (cmd[0] ==  c ) { n       printf( %d n , seg[1].c47); n     } else { n       scanf( %d%d , &qa, &qb); n       qa--; n       update(1, 0, n); n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n int n; n char a[5005][5005]; n bool vst[5005]; n bool dfs(int s, int k) { n   if (vst[k]) return false; n   vst[k] = true; n   for (int i = 0; i < n; ++i) { n     if (a[k][i] ==  1 ) { n       if (a[i][s] ==  1 ) n         return printf( %d %d %d n , s + 1, k + 1, i + 1), true; n       if (dfs(k, i)) return true; n     } n   } n   return false; n } n int main() { n   scanf( %d , &n); n   for (int i = 0; i < n; ++i) scanf( %s , a[i]); n   int i; n   for (i = 0; i < n; ++i) { n     if (dfs(i, i)) break; n   } n   if (i == n) puts( -1 ); n } n


#include <bits/stdc++.h> n using namespace std; n const int mod = 1000000007; n const int maxn = 100010; n const int INF = ~0U >> 1; n const double eps = 1e-9; n void getint(int &ret) { n   char ch; n   while (ch = getchar(), ch <  0  || ch >  9 ) n     ; n   ret = ch -  0 ; n   while (ch = getchar(), ch >=  0  && ch <=  9 ) ret = ret * 10 + ch -  0 ; n } n const int maxN = 210; n const int maxK = 42; n int nxt[maxN * 2], first[maxN], to[maxN * 2], vis[maxN], tot; n void add_edge(int x, int y) { n   nxt[++tot] = first[x]; n   first[x] = tot; n   to[tot] = y; n } n int dp[maxN][maxK * 2]; n int f[maxK * 2][maxK * 2], g[maxK * 2][maxK * 2]; n vector<int> son[maxN]; n int n, k; n void dfs(int x) { n   vis[x] = 1; n   for (int i = first[x]; i; i = nxt[i]) n     if (!vis[to[i]]) { n       son[x].push_back(to[i]); n       dfs(to[i]); n     } n   if (son[x].empty()) { n     dp[x][1 + k] = 1; n     dp[x][-k + k] = 1; n     return; n   } n   memset(f, 0, sizeof(f)); n   for (int i = -k; i <= k; i++) { n     f[i + k][i + k] = (f[i + k][i + k] + dp[son[x][0]][i + k]) % mod; n   } n   for (int kt = 1; kt < son[x].size(); kt++) { n     int Son = son[x][kt]; n     memset(g, 0, sizeof(g)); n     for (int Min = -k; Min <= k; Min++) n       for (int Max = Min; Max <= k; Max++) { n         if (f[Min + k][Max + k]) { n           for (int L = -k; L <= k; L++) { n             long long tmp = dp[Son][L + k]; n             g[min(Min, L) + k][max(Max, L) + k] = n                 (g[min(Min, L) + k][max(Max, L) + k] + n                  1ll * f[Min + k][Max + k] * tmp % mod) % n                 mod; n           } n         } n       } n     for (int Min = -k; Min <= k; Min++) n       for (int Max = Min; Max <= k; Max++) n         f[Min + k][Max + k] = g[Min + k][Max + k]; n   } n   for (int Min = -k; Min <= k; Min++) n     for (int Max = Min; Max <= k; Max++) { n       if (Min < k) { n         if (Max + Min + 1 <= 0) n           dp[x][Min + 1 + k] = (dp[x][Min + 1 + k] + f[Min + k][Max + k]) % mod; n         else if (Max < k) n           dp[x][Max + 1 + k] = (dp[x][Max + 1 + k] + f[Min + k][Max + k]) % mod; n       } n       dp[x][-k + k] = (dp[x][-k + k] + f[Min + k][Max + k]) % mod; n     } n } n int main() { n   cin >> n >> k; n   if (k == 0) { n     puts( 1 ); n     return 0; n   } n   for (int i = 1; i < n; i++) { n     int x, y; n     scanf( %d%d , &x, &y); n     add_edge(x, y); n     add_edge(y, x); n   } n   int ans = 0; n   dfs(1); n   for (int i = -k; i <= 0; i++) ans = (ans + dp[1][i + k]) % mod; n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long bin(long long x, long long n) { n   long long res = 1; n   x %= 1000000007; n   while (n) { n     if (n & 1) res = (res * x) % 1000000007; n     x = (x * x) % 1000000007; n     n >>= 1; n   } n   return res; n } n const long long MXN = 2e5 + 1; n vector<long long> adj[MXN]; n long long n; n long long a[MXN], val[MXN], dp[MXN][2]; n void dfs(long long u, long long p) { n   vector<long long> chk0, chk1; n   vector<pair<long long, long long> > in; n   for (auto i : adj[u]) { n     if (i == p) continue; n     dfs(i, u); n     in.push_back({dp[i][0] - dp[i][1], i}); n   } n   sort(in.begin(), in.end()); n   chk1.push_back(0); n   for (long long i = 0; i < in.size(); i++) { n     chk1.push_back(dp[in[i].second][1]); n     chk0.push_back(dp[in[i].second][0]); n   } n   long long y = in.size(); n   chk0.push_back(0); n   for (long long i = chk0.size(); i >= 2; i--) { n     chk0[i - 2] += chk0[i - 1]; n   } n   for (long long i = 1; i < chk1.size(); i++) { n     chk1[i] += chk1[i - 1]; n   } n   long long odd = 0, eve = 0; n   for (long long i = 0; i <= y; i += 2) { n     eve = max(eve, chk1[i] + chk0[i]); n   } n   for (long long i = 1; i <= y; i += 2) { n     odd = max(odd, chk1[i] + chk0[i]); n   } n   dp[u][0] = eve; n   dp[u][1] = max(val[u] + eve, odd); n } n void solve() { n   cin >> n; n   for (long long i = 1; i <= n; i++) { n     long long x, y; n     cin >> x >> y; n     if (x != -1) adj[i].push_back(x), adj[x].push_back(i); n     val[i] = y; n   } n   dfs(1, -1); n   cout << dp[1][1] << endl; n } n signed main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   long long t = 1; n   for (long long i = 1; i <= t; i++) { n     solve(); n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   cin >> n; n   int a; n   vector<int> v; n   for (int i = 0; i < n; ++i) { n     cin >> a; n     v.emplace_back(a); n   } n   int S = 0; n   int D = 0; n   int i = v.begin() - v.begin(); n   int j = v.end() - v.begin() - 1; n   int chance = 0; n   for (int ii = i, jj = j; jj >= ii; chance++) { n     int score = max(v[ii], v[jj]); n     if (chance % 2 == 0) { n       S += score; n     } else n       D += score; n     if (v[ii] > v[jj]) { n       ii++; n     } else n       jj--; n   } n   cout << S <<     << D; n } n


#include <bits/stdc++.h> n using namespace std; n using ll = long long; n int main() { n   int T; n   cin >> T; n   while (T--) { n     int n; n     cin >> n; n     cout << n / 2 <<   n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const long long MOD = 1000000007; n const long long INF = 1000000000; n template <class T> n inline void read(T &x) { n   char c; n   int flag = 1; n   while ((c = getchar()) <  0  || c >  9 ) n     if (c ==  - ) flag *= -1; n   x = c -  0 ; n   while ((c = getchar()) >=  0  && c <=  9 ) x = x * 10 + c -  0 ; n   x *= flag; n   return; n } n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   int n, q; n   cin >> q; n   map<long long, long long> dp; n   while (q--) { n     long long op, u, v; n     cin >> op >> u >> v; n     if (op == 1) { n       long long w; n       cin >> w; n       while (u ^ v) { n         if (u < v) swap(u, v); n         dp[u] += w, u /= 2; n       } n     } else { n       long long ans = 0; n       while (u ^ v) { n         if (u < v) swap(u, v); n         ans += dp[u], u /= 2; n       } n       cout << ans << endl; n     } n   } n } n


#include <bits/stdc++.h> n int main() { n   int x, y, a, b, cnt = 0; n   scanf( %d%d%d%d , &x, &y, &a, &b); n   for (int i = a; i <= x; i++) n     for (int j = b; j <= y && j < i; j++) cnt++; n   printf( %d n , cnt); n   for (int i = a; i <= x; i++) n     for (int j = b; j <= y && j < i; j++) printf( %d %d n , i, j); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <class T> n bool ckmin(T& a, const T& b) { n   return b < a ? a = b, 1 : 0; n } n template <class T> n bool ckmax(T& a, const T& b) { n   return a < b ? a = b, 1 : 0; n } n mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); n const long double pii = 3.14159265359; n const int MOD = 1000000007; n const char nl =   n ; n const int MX = 200001; n int N; n bool calc(int T, vector<int>& A, vector<int>& B) { n   long long sum = 0; n   for (int x = 0; x < (N); x++) { n     if (A[x] > T) { n       sum += B[x]; n       if (sum > T) { n         return false; n       } n     } n   } n   return true; n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   int Test_case; n   cin >> Test_case; n   while (Test_case-- != 0) { n     cin >> N; n     vector<int> A(N), B(N); n     for (auto& i : A) cin >> i; n     for (auto& i : B) cin >> i; n     int ans = INT_MAX; n     int l = 1, r = 1000000000; n     while (l <= r) { n       int m = l + (r - l) / 2; n       if (calc(m, A, B)) { n         ckmin(ans, m); n         r = m - 1; n       } else n         l = m + 1; n     } n     cout << ans << nl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int const maxn = 1001; n int n, m, cnt, H[maxn], father[maxn], d[maxn], Dep[maxn], dist[maxn], n     dist2[maxn], middle_point[maxn], length[maxn]; n int max_dep, max_dist; n struct Edge { n   int next, to; n } E[maxn << 1]; n inline void add(int a, int b) { n   E[++cnt] = (Edge){H[a], b}; n   H[a] = cnt; n } n inline int find(int x) { n   if (father[x] != x) father[x] = find(father[x]); n   return father[x]; n } n inline void merge(int x, int y) { n   int r1 = find(x), r2 = find(y); n   if (r1 != r2) father[r1] = r2; n } n void getdep(int x, int fa) { n   Dep[x] = Dep[fa] + 1; n   for (int i = H[x]; i; i = E[i].next) n     if (E[i].to != fa) getdep(E[i].to, x); n } n void getdist(int x, int fa) { n   dist[x] = dist[fa] + 1; n   for (int i = H[x]; i; i = E[i].next) n     if (E[i].to != fa) getdist(E[i].to, x); n } n void getdist2(int x, int fa) { n   dist2[x] = dist2[fa] + 1; n   for (int i = H[x]; i; i = E[i].next) n     if (E[i].to != fa) getdist2(E[i].to, x); n } n inline void work(int x) { n   getdep(x, 0); n   int max_dep = 0; n   for (int i = 1; i <= n; i++) n     if (father[i] == x && Dep[i] > Dep[max_dep]) max_dep = i; n   getdist(max_dep, 0); n   int max_dist = 0; n   for (int i = 1; i <= n; i++) n     if (father[i] == x && dist[i] > dist[max_dist]) max_dist = i; n   getdist2(max_dist, 0); n   middle_point[x] = max_dep; n   for (int i = 1; i <= n; i++) n     if (father[i] == x && dist[i] + dist2[i] == dist[max_dist] && n         min(dist[i], dist2[i]) > n             min(dist[middle_point[x]], dist2[middle_point[x]])) n       middle_point[x] = i; n   length[x] = dist[max_dist]; n } n int main() { n   Dep[0] = dist[0] = dist2[0] = -1; n   for (int i = 1; i <= n; i++) middle_point[i] = i; n   scanf( %d%d , &n, &m); n   for (int i = 1; i <= n; i++) father[i] = i; n   for (int i = 1; i <= m; i++) { n     int x, y; n     scanf( %d%d , &x, &y); n     add(x, y), add(y, x); n     merge(x, y); n   } n   for (int i = 1; i <= n; i++) father[i] = find(father[i]); n   for (int i = 1; i <= n; i++) n     if (father[i] == i) work(i); n   int first = 0; n   for (int i = 1; i <= n; i++) n     if (father[i] == i && length[i] >= length[first]) first = i; n   int ans = length[first]; n   for (int i = 1; i <= n; i++) { n     if (father[i] != i) continue; n     if (i == first) continue; n     if (ans == 1 && length[i] == 1) n       ans += 2; n     else if (abs(ans - length[i]) <= 1) n       ans++; n     else if ((ans & 1) && (length[i] & 1) && abs(ans - length[i] == 2)) n       ans++; n   } n   printf( %d n , ans); n   for (int i = 1; i <= n; i++) n     if (father[i] == i && i != first) n       printf( %d %d n , middle_point[i], middle_point[first]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long intersect(long long l1, long long r1, long long l2, long long r2) { n   long long l = max(l1, l2); n   long long r = min(r1, r2); n   return max(r - l, 0LL); n } n int gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); } n int main() { n   long long n, m; n   double w; n   cin >> n >> w >> m; n   if (m > n * 2) { n     cout <<  NO n ; n     return 0; n   } n   if (n >= m) { n     cout <<  YES n ; n     for (int i = 0, j = 0; j < m;) { n       while ((i + 1) * m <= j * n) i++; n       if (i < n) { n         long long z = intersect(i * m, (i + 1) * m, j * n, (j + 1) * n); n         if (z > 0) printf( %d %f  , i + 1, w * z / m); n       } n       if ((i + 1) * m <= (j + 1) * n) n         i++; n       else { n         cout <<   n ; n         j++; n       } n     } n     return 0; n   } n   int d = gcd(n, m); n   n /= d, m /= d; n   if (m != n + 1) { n     cout <<  NO n ; n     return 0; n   } n   cout <<  YES n ; n   for (int h = 0; h < d; h++) n     for (int j = 0; j < m; j++) { n       if (j > 0) printf( %lld %f  , h * n + j, w * j / m); n       if (j < m - 1) printf( %lld %f  , h * n + j + 1, w * (m - 1 - j) / m); n       printf(  n ); n     } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   pair<int, int> k[3]; n   char cell[9][9]; n   int n; n   cin >> n; n   char c; n   int z = 0; n   while (n) { n     for (int i = 1; i < 9; i++) { n       for (int j = 1; j < 9; j++) { n         cin >> c; n         if (c ==  K ) { n           k[z].first = i; n           k[z].second = j; n           z++; n         } n       } n     } n     if (abs(k[0].first - k[1].first) == 4 || n         abs(k[0].first - k[1].first) == 0) { n       if (abs(k[0].second - k[1].second) == 4 || n           abs(k[0].second - k[1].second) == 0) { n         cout <<  YES n ; n         z = 0; n         n--; n         continue; n       } n     } n     cout <<  NO n ; n     z = 0; n     n--; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, i, j, m, k, l, h, g, ye, a, b, rez; n int p[300005], f[300005], ct[300005]; n int v[300005], nv; n bool comp(int x, int y) { return f[x] < f[y]; } n int main() { n   cin >> n >> m; n   for (i = 1; i <= n; i++) cin >> p[i], f[p[i]] = i; n   ye = p[n]; n   for (i = 1; i <= m; i++) { n     cin >> a >> b; n     if (f[a] < f[b]) ct[a]++; n     if (b == p[n]) v[++nv] = a; n   } n   sort(v + 1, v + nv + 1, comp); n   for (i = nv; i >= 1; i--) { n     if (ct[v[i]] >= (n - f[v[i]]) - rez) rez++; n   } n   cout << rez <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n long long arr[200000 + 10] = {0}; n long long dp[200000 + 10] = {0}; n int main() { n   int t; n   cin >> t; n   while (t--) { n     long long n, k, p; n     cin >> n >> p >> k; n     for (int i = 1; i <= n; i++) { n       cin >> arr[i]; n     } n     sort(arr + 1, arr + n + 1); n     long long sum = 0; n     for (int i = 1; i <= n; i++) { n       if (i < k) { n         sum += arr[i]; n         dp[i] = sum; n         continue; n       } n       sum = arr[i] + dp[i - k]; n       dp[i] = sum; n     } n     long long ans = 0; n     for (int i = 1; i <= n; i++) { n       if (dp[i] <= p) { n         ans = i; n       } n     } n     cout << ans << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios::sync_with_stdio(false); n   int q; n   cin >> q; n   string s; n   int n, k; n   string r =   , g =   , b =   ; n   vector<string> v; n   for (int i = 0; i < 1500; i++) { n     r +=  RGB ; n     b +=  BRG ; n     g +=  GBR ; n   } n   for (int m = 0; m < q; m++) { n     v.clear(); n     cin >> n >> k; n     cin >> s; n     string d; n     for (int i = 0; i < s.length(); i++) { n       d = s.substr(i, k); n       if (d.length() == k) v.push_back(d); n     } n     long long minn = 1e9; n     for (vector<string>::iterator it = v.begin(); it != v.end(); it++) { n       int cntr = 0; n       int cntb = 0; n       int cntg = 0; n       string s1 = (*it); n       for (int i = 0; i < s1.length(); i++) { n         if (s1[i] != r[i]) { n           cntr++; n         } n         if (s1[i] != b[i]) { n           cntb++; n         } n         if (s1[i] != g[i]) { n           cntg++; n         } n       } n       int mio = min(cntr, min(cntb, cntg)); n       if (mio < minn) minn = mio; n     } n     cout << minn << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n #pragma GCC optimize(2) n using namespace std; n const int INF = 1e9 + 7; n const int mod = 1e9 + 9; n const int maxn = 3e5 + 7; n const int lim = 800667; n int tmp; n void dfs(int a, int b) { n   if (b == 0) { n     tmp = INF; n     return; n   } else if (b == 1) { n     tmp += a - 1; n     return; n   } n   tmp += a / b; n   dfs(b, a % b); n } n int main() { n   int n; n   scanf( %d , &n); n   int res = n - 1; n   for (int i = 1; i < n; ++i) { n     tmp = 0; n     dfs(n, i); n     res = min(res, tmp); n   } n   printf( %d n , res); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 5e5 + 7; n const int INF = 1e9 + 7; n const int MOD = 1e9 + 7; n struct as { n   int id, x; n } a[maxn]; n int vis[maxn]; n bool cmp(as a, as b) { return a.x < b.x; } n vector<int> g[maxn]; n vector<int> ans; n set<int> tmp; n bool check(as a) { n   int i, j; n   for (i = 0; i < g[a.id].size(); i++) { n     if (vis[g[a.id][i]] == a.x) return false; n     if (vis[g[a.id][i]]) tmp.insert(vis[g[a.id][i]]); n   } n   if (tmp.size() != a.x - 1) return false; n   tmp.clear(); n   vis[a.id] = a.x; n   return true; n } n int main() { n   int i, j, n, m, t, z; n   scanf( %d%d , &n, &m); n   for (i = 1; i <= m; i++) { n     int x, y; n     scanf( %d%d , &x, &y); n     g[x].push_back(y); n     g[y].push_back(x); n   } n   for (i = 1; i <= n; i++) { n     scanf( %d , &a[i].x); n     a[i].id = i; n   } n   sort(a + 1, a + 1 + n, cmp); n   for (i = 1; i <= n; i++) { n     if (!check(a[i])) break; n     ans.push_back(a[i].id); n   } n   if (i <= n) { n     printf( -1 n ); n   } else { n     for (i = 0; i < n; i++) { n       printf( %d%c , ans[i], i == n - 1 ?   n  :    ); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 5010; n char s[maxn]; n int dp[maxn][maxn]; n int lnk[maxn][26]; n int n; n const int mod = (int)1e9 + 7; n int go(int pos, int x) { n   if (dp[pos][x] != -1) { n     return dp[pos][x]; n   } n   if (pos == n) { n     return dp[pos][x] = 1; n   } n   dp[pos][x] = go(pos + 1, x); n   for (int i = 0; i < 26; i++) { n     if (i != s[x] -  a  && lnk[x][i] != n) { n       dp[pos][x] += go(pos + 1, lnk[x][i]); n       if (dp[pos][x] >= mod) { n         dp[pos][x] -= mod; n       } n     } n   } n   return dp[pos][x]; n } n int now[maxn]; n int main() { n   scanf( %d , &n); n   scanf( %s , s); n   for (int i = 0; i < 26; i++) { n     lnk[n][i] = n; n   } n   for (int i = 0; i < n; i++) { n     dp[n][i] = 1; n   } n   for (int i = n - 1; i >= 0; i--) { n     for (int j = 0; j < 26; j++) { n       lnk[i][j] = lnk[i + 1][j]; n     } n     int c = s[i] -  a ; n     int f = lnk[i][c]; n     if (f != n) { n       for (int j = 0; j < n; j++) { n         now[j] -= dp[j + 1][f]; n         if (now[j] < 0) { n           now[j] += mod; n         } n       } n     } n     lnk[i][c] = i; n     for (int j = n - 1; j >= 0; j--) { n       dp[j][i] = now[j] + dp[j + 1][i]; n       if (dp[j][i] >= mod) { n         dp[j][i] -= mod; n       } n     } n     for (int j = 0; j < n; j++) { n       now[j] += dp[j + 1][i]; n       if (now[j] >= mod) { n         now[j] -= mod; n       } n     } n   } n   cout << dp[0][0] << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 200035; n long long a[N]; n long long b[N]; n long long c[N]; n const long long INF = 1e17; n bool can[N]; n int main() { n   ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); n   int n, k; n   cin >> n >> k; n   ++n; n   for (int i = 0; i < n; ++i) cin >> a[i], c[i] = a[i]; n   for (int i = 0; i < N; ++i) { n     if (a[i] % 2 == 0) { n       a[i + 1] += a[i] / 2; n       a[i] = 0; n     } n   } n   int ans = 0; n   b[N - 1] = -a[N - 1]; n   can[N - 1] = 1; n   for (int i = N - 2; i >= 0; --i) { n     b[i] = (b[i + 1] * 2) - a[i]; n     can[i] = 1; n     if (abs(b[i]) > INF) break; n   } n   for (int i = 0; i < n; ++i) { n     if (can[i]) { n       b[i] += c[i]; n       ans += abs(b[i]) <= k && (i != n - 1 or b[i]); n     } n     if (a[i]) break; n   } n   cout << ans; n } n


#include <bits/stdc++.h> n using namespace std; n int n, a, b, k, cnt = 0, ans; n string s; n vector<int> arr; n int main(void) { n   ios ::sync_with_stdio(0); n   cin.tie(0); n   cin >> n >> a >> b >> k >> s; n   for (int i = 1; i <= n; ++i) { n     if (s[i - 1] ==  0 ) { n       ++cnt; n     } else { n       cnt = 0; n     } n     if (cnt == b) { n       arr.emplace_back(i); n       cnt = 0; n     } n   } n   ans = int(arr.size()) - (a - 1); n   cout << ans <<   n ; n   for (int i = 0; i <= ans - 1; ++i) { n     cout << arr[i] <<    ; n   } n   cout <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long Ins, Del, Rep, Exc; n string A; n string B; n int lenA, lenB; n long long DP[4001][4001]; n int posA[4001][26]; n int posB[4001][26]; n int main() { n   ios::sync_with_stdio(false); n   while (cin >> Ins >> Del >> Rep >> Exc) { n     cin >> A >> B; n     A =  .  + A; n     B =  .  + B; n     lenA = A.length() - 1; n     lenB = B.length() - 1; n     memset(posA, 0xff, sizeof(posA)); n     memset(posB, 0xff, sizeof(posB)); n     for (int i = 0; i < 26; i++) { n       for (int j = 1; j <= lenB; j++) n         if (B[j] == i +  a ) n           posB[j][i] = j; n         else n           posB[j][i] = posB[j - 1][i]; n       for (int j = 1; j <= lenA; j++) n         if (A[j] == i +  a ) n           posA[j][i] = j; n         else n           posA[j][i] = posA[j - 1][i]; n     } n     DP[0][0] = 0; n     for (int i = 0; i <= lenA; i++) n       for (int j = 0; j <= lenB; j++) n         if (i + j > 0) { n           DP[i][j] = 1000000000000000000LL; n           if (A[i] == B[j]) DP[i][j] = DP[i - 1][j - 1]; n           if (i >= 1 && j >= 1) n             DP[i][j] = min(DP[i][j], DP[i - 1][j - 1] + Rep); n           if (i >= 1) DP[i][j] = min(DP[i][j], DP[i - 1][j] + Del); n           if (j >= 1) DP[i][j] = min(DP[i][j], DP[i][j - 1] + Ins); n           int u = posA[i][B[j] -  a ]; n           int v = posB[j][A[i] -  a ]; n           if (u > 0 && v > 0 && u < i && v < j) n             DP[i][j] = min(DP[i][j], DP[u - 1][v - 1] + Exc + n                                          Ins * (j - v - 1) + Del * (i - u - 1)); n         } n     cout << DP[lenA][lenB] << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long int inf = 1e9 + 7; n const long long int mod = 1e9 + 9; n long long int mini(long long int a, long long int b) { n   if (a >= b) return b; n   return a; n } n long long int maxi(long long int a, long long int b) { n   if (a >= b) return a; n   return b; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(nullptr); n   cout.tie(nullptr); n   long long int n; n   cin >> n; n   map<long long int, long long int> cnt; n   long long int ans = 0; n   vector<long long int> A; n   for (long long int i = 0; i <= n - 1; i++) { n     string s; n     cin >> s; n     long long int x = 0; n     for (long long int j = 0; j <= s.length() - 1; j++) { n       long long int mask = (1LL << (s[j] -  a )); n       x ^= mask; n     } n     ans += cnt[x]; n     cnt[x]++; n     A.push_back(x); n   } n   for (long long int i = 0; i <= n - 1; i++) { n     long long int x = A[i]; n     for (long long int j = 0; j <= 25; j++) { n       long long int mask = (1LL << j); n       if (mask & x) { n         x ^= mask; n         ans += cnt[x]; n         x ^= mask; n       } n     } n   } n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m; n int s, t; n struct Edge { n   int from, to, next; n   int cap, flow; n   void get(int u, int a, int b, int c, int d) { n     from = u; n     to = a; n     next = b; n     cap = c; n     flow = d; n   } n } edge[100050 << 4]; n int tol; n int head[100050]; n int gap[100050], dep[100050], pre[100050], cur[100050]; n void init() { n   tol = 0; n   memset(head, -1, sizeof head); n } n void add_edge(int u, int v, int w, int rw = 0) { n   edge[tol].get(u, v, head[u], w, 0); n   head[u] = tol++; n   edge[tol].get(v, u, head[v], rw, 0); n   head[v] = tol++; n } n int sap(int start, int end, int N) { n   memset(gap, 0, sizeof gap); n   memset(dep, 0, sizeof dep); n   memcpy(cur, head, sizeof head); n   int u = start; n   pre[u] = -1; n   gap[0] = N; n   int ans = 0; n   while (dep[start] < N) { n     if (u == end) { n       int Min = 1e7; n       for (int i = pre[u]; i != -1; i = pre[edge[i ^ 1].to]) n         if (Min > edge[i].cap - edge[i].flow) Min = edge[i].cap - edge[i].flow; n       for (int i = pre[u]; i != -1; i = pre[edge[i ^ 1].to]) { n         edge[i].flow += Min; n         edge[i ^ 1].flow -= Min; n       } n       u = start; n       ans += Min; n       continue; n     } n     bool flag = false; n     int v; n     for (int i = cur[u]; i != -1; i = edge[i].next) { n       v = edge[i].to; n       if (edge[i].cap - edge[i].flow && dep[v] + 1 == dep[u]) { n         flag = true; n         cur[u] = pre[v] = i; n         break; n       } n     } n     if (flag) { n       u = v; n       continue; n     } n     int Min = N; n     for (int i = head[u]; i != -1; i = edge[i].next) n       if (edge[i].cap - edge[i].flow && dep[edge[i].to] < Min) { n         Min = dep[edge[i].to]; n         cur[u] = i; n       } n     gap[dep[u]]--; n     if (!gap[dep[u]]) return ans; n     dep[u] = Min + 1; n     gap[dep[u]]++; n     if (u != start) u = edge[pre[u] ^ 1].to; n   } n   return ans; n } n int a[11]; n char str[100050]; n void add(int now) { n   int len = strlen(str); n   int j; n   for (int i = 0; i < len; i = j + 1) { n     j = i; n     while (str[j] !=  ,  && str[j] != 0) j++; n     if (str[i] ==  S ) { n       add_edge(now, n + 1, 1); n     } else if (str[i] ==  M ) { n       add_edge(now, n + 2, 1); n     } else if (str[i] ==  L ) { n       add_edge(now, n + 3, 1); n     } else { n       int more = j - i; n       more += 2; n       add_edge(now, more + n, 1); n     } n   } n } n void print(int num) { n   num -= n; n   if (num == 1) { n     cout <<  S  << endl; n   } else if (num == 2) n     cout <<  M  << endl; n   else if (num == 3) n     cout <<  L  << endl; n   else if (num == 4) n     cout <<  XL  << endl; n   else if (num == 5) n     cout <<  XXL  << endl; n   else n     cout <<  XXXL  << endl; n } n int main() { n   for (int i = 1; i <= 6; i++) { n     cin >> a[i]; n   } n   cin >> n; n   int N; n   s = 0, t = n + 7, N = t + 1; n   init(); n   for (int i = 1; i <= 6; i++) { n     add_edge(n + i, t, a[i]); n   } n   for (int i = 1; i <= n; i++) { n     cin >> str; n     add_edge(s, i, 1); n     add(i); n   } n   int ans = sap(s, t, N); n   if (ans != n) { n     cout <<  NO  << endl; n     return 0; n   } n   cout <<  YES  << endl; n   for (int u = 1; u <= n; u++) { n     for (int i = head[u]; i != -1; i = edge[i].next) { n       int v = edge[i].to; n       if (v > n && v <= n + 6 && edge[i].flow) { n         print(v); n       } n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 1e3; n int N, p[MAXN + 1]; n set<int> adj[MAXN + 1]; n void dfs(int v, int padre) { n   p[v] = padre; n   for (auto x : adj[v]) { n     if (x == padre || p[x]) continue; n     p[x] = padre; n     dfs(x, v); n   } n   adj[v].clear(); n } n int main() { n   ios_base::sync_with_stdio(false); n   cin >> N; n   for (int i = 0; i < N - 1; i++) { n     int u, v; n     cin >> u >> v; n     adj[u].insert(v); n     adj[v].insert(u); n   } n   while (1) { n     int cnt = 0; n     int Max = 0, pos = 0; n     for (int i = 1; i <= N; i++) { n       if (!p[i]) { n         cnt++; n         int sz = adj[i].size(); n         if (sz > Max) { n           Max = sz; n           pos = i; n         } n       } n     } n     if (cnt == 1) { n       for (int i = 1; i <= N; i++) { n         if (!p[i]) { n           cout <<  !   << i << endl; n           break; n         } n       } n       break; n     } else if (cnt == 2) { n       int u = -1, v = -1; n       for (int i = 1; i <= N; i++) { n         if (!p[i]) { n           if (u == -1) n             u = i; n           else n             v = i; n         } n       } n       cout <<  ?   << u <<     << v << endl; n       int lca; n       cin >> lca; n       cout <<  !   << lca << endl; n       break; n     } n     int u = -1, v = -1; n     vector<int> del; n     for (auto x : adj[pos]) { n       if (u == -1) n         u = x; n       else n         v = x; n       if (u != -1 && v != -1) { n         cout <<  ?   << u <<     << v << endl; n         int lca; n         cin >> lca; n         if (lca == u || lca == v) { n           dfs(pos, lca); n           u = v = -1; n           del.clear(); n           break; n         } else { n           dfs(u, pos); n           dfs(v, pos); n           del.push_back(u); n           del.push_back(v); n           u = v = -1; n         } n       } n     } n     for (auto x : del) adj[pos].erase(x); n   } n } n


#include <bits/stdc++.h> n using namespace std; n int euclid(int m, int n) { n   int amari; n   if ((amari = m % n) == 0) { n     return n; n   } else { n     return euclid(n, amari); n   } n } n int main() { n   int N; n   int A[101]; n   cin >> N; n   for (int i = 1; i <= N; i++) { n     cin >> A[i]; n   } n   vector<int> v; n   bool used[101] = {}; n   bool flag = true; n   for (int i = 1; i <= N; i++) { n     if (!used[i]) { n       used[i] = true; n       int x = 1; n       int now = i; n       while (A[now] != i) { n         now = A[now]; n         if (used[now]) { n           flag = false; n           break; n         } n         used[now] = true; n         x++; n       } n       v.push_back(x); n     } n   } n   if (!flag) { n     cout << -1 << endl; n     return 0; n   } n   int res = 1; n   for (int i = 0; i < v.size(); i++) { n     int z; n     if (v[i] % 2 == 0) { n       z = v[i] / 2; n     } else { n       z = v[i]; n     } n     if (res > z) { n       res = res * z / euclid(res, z); n     } else { n       res = res * z / euclid(z, res); n     } n   } n   cout << res << endl; n   return 0; n } n