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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


#include <bits/stdc++.h>   using namespace std;   namespace Fread {   const int MAXN = 1 << 20;   char buffer[MAXN], *S, *T;   inline char getchar() {     if (S == T) {       T = (S = buffer) + fread(buffer, 1, MAXN, stdin);       if (S == T) return EOF;     }     return *S++;   }   }  // namespace Fread   inline int read() {     int f = 1, x = 0;     char ch = Fread::getchar();     while (!isdigit(ch)) {       if (ch ==  - ) f = -1;       ch = Fread::getchar();     }     while (isdigit(ch)) {       x = x * 10 + ch -  0 ;       ch = Fread::getchar();     }     return x * f;   }   inline long long readll() {     long long f = 1, x = 0;     char ch = Fread::getchar();     while (!isdigit(ch)) {       if (ch ==  - ) f = -1;       ch = Fread::getchar();     }     while (isdigit(ch)) {       x = x * 10 + ch -  0 ;       ch = Fread::getchar();     }     return x * f;   }   const int MAXN = 1e6 + 10, N = 1e6 + 1, INF = 0x3f3f3f3f;   int n, q, a[MAXN];   struct SegmentTree {     int val[MAXN << 2], cnt[MAXN << 2], tag[MAXN << 2];     void push_down(int p) {       if (tag[p]) {         val[p << 1] += tag[p];         tag[p << 1] += tag[p];         val[p << 1 | 1] += tag[p];         tag[p << 1 | 1] += tag[p];         tag[p] = 0;       }     }     void push_up(int p) {       if (val[p << 1] == val[p << 1 | 1]) {         val[p] = val[p << 1];         cnt[p] = cnt[p << 1] + cnt[p << 1 | 1];       } else if (val[p << 1] < val[p << 1 | 1]) {         val[p] = val[p << 1];         cnt[p] = cnt[p << 1];       } else {         val[p] = val[p << 1 | 1];         cnt[p] = cnt[p << 1 | 1];       }     }     void modify1(int p, int l, int r, int ql, int qr, int x) {       if (ql <= l && qr >= r) {         tag[p] += x;         val[p] += x;         return;       }       push_down(p);       int mid = (l + r) >> 1;       if (ql <= mid) modify1(p << 1, l, mid, ql, qr, x);       if (qr > mid) modify1(p << 1 | 1, mid + 1, r, ql, qr, x);       push_up(p);     }     void modify2(int p, int l, int r, int pos, int x) {       if (l == r) {         cnt[p] += x;         assert(cnt[p] < 2);         return;       }       push_down(p);       int mid = (l + r) >> 1;       if (pos <= mid)         modify2(p << 1, l, mid, pos, x);       else         modify2(p << 1 | 1, mid + 1, r, pos, x);       push_up(p);     }     int query(int p, int l, int r, int ql, int qr) {       if (ql <= l && qr >= r) return val[p] == 1 ? cnt[p] : 0;       int mid = (l + r) >> 1, res = 0;       if (ql <= mid) res = query(p << 1, l, mid, ql, qr);       if (qr > mid) res += query(p << 1 | 1, mid + 1, r, ql, qr);       return res;     }     SegmentTree() {}   } T;   int main() {     n = read();     q = read();     for (int i = 1; i <= n; ++i) a[i] = read();     a[0] = N;     a[n + 1] = 0;     for (int i = 0; i <= n; ++i) {       if (a[i] != a[i + 1])         T.modify1(1, 0, N, min(a[i], a[i + 1]) + 1, max(a[i], a[i + 1]), 1);       if (i) T.modify2(1, 0, N, a[i], 1);     }     while (q--) {       int i = read(), x = read();       T.modify2(1, 0, N, a[i], -1);       if (a[i] != a[i - 1]) {         T.modify1(1, 0, N, min(a[i], a[i - 1]) + 1, max(a[i], a[i - 1]), -1);       }       if (a[i] != a[i + 1]) {         T.modify1(1, 0, N, min(a[i], a[i + 1]) + 1, max(a[i], a[i + 1]), -1);       }       a[i] = x;       if (a[i] != a[i - 1]) {         T.modify1(1, 0, N, min(a[i], a[i - 1]) + 1, max(a[i], a[i - 1]), 1);       }       if (a[i] != a[i + 1]) {         T.modify1(1, 0, N, min(a[i], a[i + 1]) + 1, max(a[i], a[i + 1]), 1);       }       T.modify2(1, 0, N, a[i], 1);       printf( %d n , T.query(1, 0, N, 1, N - 1));     }     return 0;   }


#include <bits/stdc++.h>   const int N = 100010;   const int inf = 0x3f3f3f3f;   using namespace std;   int a[N], b[N], c[N], d[N];   int main() {     int n, m, k;     scanf( %d%d%d , &n, &m, &k);     for (int i = 0; i < m; i++) scanf( %d%d%d , a + i, b + i, c + i);     long long ret = 0;     for (int i = 0; i < k; i++) {       scanf( %d , d + i);       for (int j = 0; j < m; j++) {         if (a[j] <= d[i] && d[i] <= b[j]) ret += c[j] + d[i] - a[j];       }     }     cout << ret << endl;     return 0;   }