#include <bits/stdc++.h> n using namespace std; n const double PI = acos(-1.0); n const double EPS = 1e-7; n const long long BIG_PRIME7 = 1000000007; n const long long BIG_PRIME9 = 1000000009; n int gcd(int x, int y) { return y ? gcd(y, x % y) : x; } n char ch =  a ; n int j, n, m, L, i; n int N, C; n int x, y, z, k; n string f, s; n int a[101], b[101]; n int main() { n   cin >> x; n   for (i = 0; i < x; i++) { n     cin >> k; n     if (k & 1) z++; n   } n   if (!(z & 1)) n     cout << (x - z); n   else n     cout << z; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 111111; n int last[3 * N], pre[3 * N], v[3 * N], cnt[3 * N], cur = 0; n inline void add(int a, int b) { n   pre[cur] = last[a]; n   v[cur] = b; n   last[a] = cur; n   cur++; n } n set<int>* who[N]; n inline set<int>* merge(set<int>*& a, set<int>*& b) { n   if (a->size() < b->size()) { n     return merge(b, a); n   } else { n     for (set<int>::iterator it = b->begin(); it != b->end(); it++) { n       if (a->find(-*it) != a->end()) { n         a->erase(-*it); n       } else { n         a->insert(*it); n       } n     } n     b->clear(); n     return a; n   } n } n int used[N]; n void dfs(int u) { n   used[u] = true; n   for (int i = last[u]; i >= 0; i = pre[i]) n     if (!used[v[i]]) { n       dfs(v[i]); n       cnt[i ^ 1] = cnt[i] = who[v[i]]->size(); n       who[u] = merge(who[u], who[v[i]]); n     } n } n set<int>* mem[N]; n int main() { n   memset(last, -1, sizeof(last)); n   int n; n   scanf( %d , &n); n   for (int i = (0); i <= ((n - 1) - 1); i++) { n     int u, v; n     scanf( %d%d , &u, &v); n     u--; n     v--; n     add(u, v); n     add(v, u); n   } n   for (int i = (0); i <= ((n)-1); i++) mem[i] = who[i] = new set<int>(); n   int k; n   scanf( %d , &k); n   for (int i = (1); i <= ((k)); i++) { n     int u, v; n     scanf( %d%d , &u, &v); n     u--; n     v--; n     if (u != v) { n       who[u]->insert(i); n       who[v]->insert(-i); n     } n   } n   dfs(0); n   for (int i = 0; i < cur; i += 2) { n     if (i) printf(   ); n     printf( %d , cnt[i]); n   } n   puts(  ); n   for (int i = (0); i <= ((n)-1); i++) delete (mem[i]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MOD = 1e9 + 7; n const int INF = 1e9 + 7; n const int base = 1e9; n const int MAX = 1e5; n const double EPS = 1e-9; n const double PI = acos(-1.); n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   int n, *a, *res, j = 0; n   cin >> n; n   a = new int[n]; n   res = new int[n]; n   set<int> cnt; n   for (int i = 0; i < n; i++) { n     cin >> a[i]; n   } n   for (int i = n - 1; i >= 0; i--) { n     if (!cnt.count(a[i])) { n       cnt.insert(a[i]); n       res[j] = a[i]; n       j++; n     } n   } n   cout << j << endl; n   for (int i = j - 1; i >= 0; i--) { n     cout << res[i] <<    ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct cell { n   bool up, down, right, left; n   int degree; n   cell() { up = down = left = right = degree = 0; } n   void reset() { up = down = left = right = degree = 0; } n }; n struct qcell { n   int x, y, degree; n   qcell() { n     x = y = -1; n     degree = 0; n   } n   qcell(int xx, int yy, int deg) { n     x = xx; n     y = yy; n     degree = deg; n   } n }; n char pitch[2001][2001]; n cell info[2001][2001]; n int n, m; n int empty_cells = 0; n bool update(int x, int y) { n   int degree = 0; n   if (x >= 0 && x < n && y >= 0 && y < m) { n     degree = info[x][y].degree; n     if (pitch[x][y] !=  . ) n       info[x][y].reset(); n     else { n       info[x][y].up = (x - 1 >= 0 && pitch[x - 1][y] ==  . ); n       info[x][y].down = (x + 1 < n && pitch[x + 1][y] ==  . ); n       info[x][y].left = (y - 1 >= 0 && pitch[x][y - 1] ==  . ); n       info[x][y].right = (y + 1 < m && pitch[x][y + 1] ==  . ); n       info[x][y].degree = (info[x][y].up + info[x][y].down + info[x][y].left + n                            info[x][y].right); n     } n   } n   return degree != info[x][y].degree; n } n void do_push(int x, int y, queue<qcell> &q) { n   if (x >= 0 && x < n && y >= 0 && y < m) n     if (pitch[x][y] ==  . ) n       if (info[x][y].degree == 1) q.push(qcell(x, y, 1)); n } n void occupy(int x, int y, queue<qcell> &q) { n   int xx = x, yy = y; n   char dir =  U ; n   if (info[x][y].down) dir =  D ; n   if (info[x][y].left) dir =  L ; n   if (info[x][y].right) dir =  R ; n   if (dir ==  U ) { n     pitch[x][y] =  v ; n     pitch[x - 1][y] =  ^ ; n     xx = x - 1; n   } else if (dir ==  D ) { n     pitch[x][y] =  ^ ; n     pitch[x + 1][y] =  v ; n     xx = x + 1; n   } else if (dir ==  L ) { n     pitch[x][y] =  > ; n     pitch[x][y - 1] =  < ; n     yy = y - 1; n   } else { n     pitch[x][y] =  < ; n     pitch[x][y + 1] =  > ; n     yy = y + 1; n   } n   update(x, y); n   update(xx, yy); n   if (do_push) { n     if (update(x - 1, y)) do_push(x - 1, y, q); n     if (update(x + 1, y)) do_push(x + 1, y, q); n     if (update(x, y - 1)) do_push(x, y - 1, q); n     if (update(x, y + 1)) do_push(x, y + 1, q); n     if (update(xx - 1, yy)) do_push(xx - 1, yy, q); n     if (update(xx + 1, yy)) do_push(xx + 1, yy, q); n     if (update(xx, yy - 1)) do_push(xx, yy - 1, q); n     if (update(xx, yy + 1)) do_push(xx, yy + 1, q); n   } n   empty_cells -= 2; n } n int main() { n   cin >> n >> m; n   for (int i = 0; i < n; i++) n     for (int j = 0; j < m; j++) { n       cin >> pitch[i][j]; n       if (pitch[i][j] ==  . ) empty_cells++; n     } n   queue<qcell> q; n   for (int i = 0; i < n; i++) n     for (int j = 0; j < m; j++) { n       update(i, j); n       if (info[i][j].degree == 1) q.push(qcell(i, j, info[i][j].degree)); n     } n   bool unique_sol = 1; n   while (1) { n     if (q.empty()) break; n     qcell f = q.front(); n     q.pop(); n     if (info[f.x][f.y].degree == 0) { n       if (pitch[f.x][f.y] ==  . ) { n         unique_sol = false; n         break; n       } n     } else n       occupy(f.x, f.y, q); n   } n   if (unique_sol) n     if (empty_cells != 0) unique_sol = 0; n   if (!unique_sol) n     cout <<  Not unique  << endl; n   else n     for (int i = 0; i < n; i++) { n       for (int j = 0; j < m; j++) cout << pitch[i][j]; n       cout << endl; n     } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int INF = 0x3f3f3f3f; n int main() { n   ios::sync_with_stdio(false); n   int t; n   cin >> t; n   while (t--) { n     string s, t; n     cin >> s >> t; n     vector<int> each[26]; n     int n, m; n     n = s.length(); n     m = t.length(); n     for (int i = 0; i < n; ++i) { n       each[s[i] -  a ].push_back(i); n     } n     int las = INF, res = 0; n     for (int i = 0; i < m; ++i) { n       if (upper_bound(each[t[i] -  a ].begin(), each[t[i] -  a ].end(), las) != n           each[t[i] -  a ].end()) { n         las = n             *upper_bound(each[t[i] -  a ].begin(), each[t[i] -  a ].end(), las); n       } else { n         if (each[t[i] -  a ].empty()) { n           res = -1; n           break; n         } else { n           res++; n           las = each[t[i] -  a ][0]; n         } n       } n     } n     cout << res << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, h, a, road_width = 0; n   cin >> n >> h; n   for (int i = 0; i < n; i++) { n     cin >> a; n     if (a > h) n       road_width += 2; n     else n       road_width++; n   } n   cout << road_width <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int abs(int a, int b) { n   if (a > b) n     return a - b; n   else n     return b - a; n } n int main() { n   int n, c, i, b, count = 0, index1 = 1, index2 = 1, flag = 0; n   cin >> n >> c; n   char *s; n   s = new char[n + 2]; n   for (i = 1; i <= n; i++) { n     cin >> s[i]; n   } n   int *d = new int[n + 1]; n   for (i = 1; i <= n / 2; i++) { n     b = abs((int)s[i], (int)s[n - i + 1]); n     if (b < 13) n       d[i] = d[n - i + 1] = b; n     else n       d[i] = d[n - i + 1] = 26 - b; n   } n   if (c > n / 2) c = 1 + n - c; n   for (i = 1; i <= n / 2; i++) { n     if (d[i] > 0) { n       index1 = i; n       break; n     } n   } n   for (i = n / 2; i >= 1; i--) { n     if (d[i] > 0) { n       index2 = i; n       flag = 1; n       break; n     } n   } n   if (c <= index1) { n     count = index2 - c; n   } else if (c <= index2) { n     if (c - index1 > index2 - c) n       count = 2 * (index2 - c) + c - index1; n     else n       count = 2 * (c - index1) + index2 - c; n   } else n     count = c - index1; n   for (i = index1; i <= index2; i++) { n     count += d[i]; n   } n   if (flag == 1) n     cout << count; n   else n     cout << 0; n   return 0; n } n


#include <bits/stdc++.h> n int any_table[11][11]; n struct DammedPascalNUmber { n   int tb[13][13]; n   DammedPascalNUmber() { n     int i, j; n     for (i = 0; i <= 12; i++) n       for (j = 0; j <= i; j++) { n         int &r = tb[i][j]; n         if (j == 0 || i == 0) n           r = 1; n         else n           r = tb[i - 1][j - 1] + tb[i - 1][j]; n       } n   } n   int count(int n, int m) { return tb[n][m]; } n } pas; n int any(int dig, int amo) { n   if (dig < amo) return 0; n   if (amo < 0) return 0; n   if (dig == 0) return 1; n   int &r = any_table[dig][amo]; n   int i; n   if (r >= 0) return r; n   r = 1; n   for (i = 0; i < amo; i++) r *= 2; n   for (; i < dig; i++) r *= 8; n   r *= pas.count(dig, amo); n   return r; n } n int check(int m, int n) { n   int i, j, t, r = 0; n   for (i = 1; i <= m; i++) { n     char str[15]; n     t = 0; n     sprintf(str,  %d , i); n     for (j = 0; str[j]; j++) n       if (str[j] ==  4  || str[j] ==  7 ) t++; n     if (t == n) r++; n   } n   return r; n } n int a[13]; n int dfs(int party, int quta) { n   int r = 0, i; n   if (party == 6) { n     return 1; n   } n   for (i = quta; i >= 0; i--) n     if (a[i] > 0) { n       int t = a[i]; n       a[i]--; n       r = (1LL * r + 1LL * t * 1LL * dfs(party + 1, quta - i) + n            1LL * 1000000007) % n           1000000007; n       a[i]++; n     } n   return r; n } n void test(int n, int m) { printf( any(%d,%d)=%d n , n, m, any(n, m)); } n void test2(int m, int n) { n   printf( (%d,%d)=%d a[%d]=%d n , m, n, check(m, n), n, a[n]); n } n int main() { n   memset(any_table, -1, sizeof(any_table)); n   int m, s, i, j, k, sn, lucked; n   while (~scanf( %d , &m)) { n     memset(a, 0, sizeof(a)); n     char str[15]; n     sprintf(str,  %d , m); n     for (sn = 0; str[sn]; sn++) n       ; n     for (j = sn; j > 0; j--) { n       a[j] = 0; n       lucked = 0; n       for (i = 0; i < sn; i++) { n         int p = 0, q = 0; n         k = str[i] -  0  - 1; n         for (; k >= 0; k--) n           if (k == 4 || k == 7) n             q++; n           else n             p++; n         if (p) a[j] += p * any(sn - i - 1, j - lucked); n         if (q) a[j] += q * any(sn - i - 1, j - lucked - 1); n         if (str[i] ==  4  || str[i] ==  7 ) { n           lucked++; n         } n         if (i == sn - 1 && lucked == j) a[j]++; n       } n     } n     a[0] = m; n     for (i = 1; i <= sn; i++) a[0] -= a[i]; n     int ans = 0; n     for (i = 1; i <= sn; i++) n       if (a[i]) { n         ans = (1LL * ans + 1LL * a[i] * dfs(0, i - 1) + 1LL * 1000000007) % n               1000000007; n       } n     printf( %d n , ans); n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 1e5 + 10; n const double pi = acos(-1); n const int inf = 1 << 30; n const int mod = 1e9 + 9; n struct point { n   int x, flag; n   bool operator<(const point &a) const { return flag > a.flag; } n } s, q; n bool check(long long a[], int n, long long h, int x) { n   priority_queue<int, vector<int>, less<int> > qwe; n   for (int i = 1; i <= min(n, x); i++) qwe.push(a[i]); n   int f = 1, pos; n   while (!qwe.empty()) { n     pos = qwe.top(); n     qwe.pop(); n     if (f) h -= pos; n     f ^= 1; n   } n   if (h >= 0ll) n     return true; n   else n     return false; n } n int main() { n   int n; n   long long h, a[maxn]; n   scanf( %d %lld , &n, &h); n   for (int i = 1; i <= n; i++) scanf( %lld , &a[i]); n   int l = 0, r = n, ans = 0; n   while (l <= r) { n     int mid = (l + r) >> 1; n     if (check(a, n, h, mid)) n       l = mid + 1, ans = mid; n     else n       r = mid - 1; n   } n   printf( %d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n const int64_t mod = 998244353; n int gcd(int64_t a, int64_t b) { n   while (a != 0 && b != 0) { n     if (a >= b) n       a = a % b; n     else n       b = b % a; n   } n   return a + b; n } n struct Fraction { n   int64_t numerator; n   int64_t devider; n   Fraction(int64_t n = 0, int64_t d = 1) : numerator(n), devider(d){}; n   const Fraction& operator+(const Fraction& rv) { n     if (rv.devider == devider) { n       numerator += rv.numerator; n       numerator %= mod; n       return *this; n     } else { n       numerator *= rv.devider; n       numerator %= mod; n       numerator += rv.numerator * devider; n       numerator %= mod; n       devider *= rv.devider; n       devider %= mod; n       return *this; n     } n   } n   const Fraction& operator*(const Fraction& rv) { n     numerator *= rv.numerator; n     devider *= rv.devider; n     numerator %= mod; n     devider %= mod; n     return *this; n   } n }; n struct Item { n   Item(Fraction f, int64_t s, int64_t v) : possibility(f), sum(s), val(v){}; n   Fraction possibility; n   int64_t sum; n   int64_t val; n }; n void extended_euclid(int64_t a, int64_t b, int64_t& x, int64_t& y, int64_t& d) { n   int64_t q, r, x1, x2, y1, y2; n   if (b == 0) { n     d = a, x = 1, y = 0; n     return; n   } n   x2 = 1, x1 = 0, y2 = 0, y1 = 1; n   while (b > 0) { n     q = a / b, r = a - q * b; n     x = x2 - q * x1, y = y2 - q * y1; n     a = b, b = r; n     x2 = x1, x1 = x, y2 = y1, y1 = y; n   } n   d = a, x = x2, y = y2; n } n int main() { n   int64_t n, m; n   std::cin >> n >> m; n   std::vector<std::pair<int64_t, bool>> data; n   int64_t posWeight = 0, negWeight = 0; n   for (int64_t i = 0; i < n; ++i) { n     int64_t a; n     std::cin >> a; n     if (a == 0) n       data.push_back(std::make_pair(-1, false)); n     else n       data.push_back(std::make_pair(-1, true)); n   } n   for (auto& i : data) { n     int64_t w; n     std::cin >> w; n     i.first = w; n     if (i.second) n       posWeight += w; n     else n       negWeight += w; n   } n   std::vector<Item> posVec{ n       Item(Fraction(1, 1), posWeight + negWeight, posWeight)}, n       negVec{Item(Fraction(1, 1), posWeight + negWeight, negWeight)}; n   for (int64_t i = 0; i < m; ++i) { n     std::vector<Item> newPos, newNeg; n     for (auto j : posVec) { n       if (!newPos.size()) { n         Fraction f(j.val, j.sum); n         f = f * j.possibility; n         newPos.push_back(Item(f, j.sum + 1, j.val + 1)); n       } else { n         Fraction f(j.val, j.sum); n         f = f * j.possibility; n         newPos.rbegin()->possibility = newPos.rbegin()->possibility + f; n       } n       if (j.sum > j.val) { n         Fraction f(j.sum - j.val, j.sum); n         f = f * j.possibility; n         newPos.push_back(Item(f, j.sum - 1, j.val)); n       } n     } n     for (auto j : negVec) { n       if (!newNeg.size() && j.val > 1) { n         Fraction f(j.val, j.sum); n         f = f * j.possibility; n         newNeg.push_back(Item(f, j.sum - 1, j.val - 1)); n       } else if (j.val > 1) { n         Fraction f(j.val, j.sum); n         f = f * j.possibility; n         newNeg.rbegin()->possibility = newNeg.rbegin()->possibility + f; n       } n       Fraction f(j.sum - j.val, j.sum); n       f = f * j.possibility; n       newNeg.push_back(Item(f, j.sum + 1, j.val)); n     } n     posVec = newPos; n     negVec = newNeg; n   } n   Fraction pos, neg; n   for (auto i : posVec) pos = pos + Fraction(i.val, 1) * i.possibility; n   for (auto i : negVec) neg = neg + Fraction(i.val, 1) * i.possibility; n   for (auto i : data) { n     Fraction res; n     if (i.second) n       res = Fraction(i.first, posWeight) * pos; n     else n       res = Fraction(i.first, negWeight) * neg; n     int64_t g = gcd(res.numerator, res.devider); n     res.numerator /= g; n     res.devider /= g; n     int64_t d, x, y; n     extended_euclid(res.devider, mod, x, y, d); n     std::cout << (((x % mod + mod) % mod) * res.numerator) % mod << std::endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 100005; n int n, m, x, y, f[N], fa[N], ch[N][2]; n char op[5]; n long long a[N], siz[N], ssiz[N], ans[N], sans[N], sqsiz[N], all[N]; n bool isroot(int u) { return ch[fa[u]][0] != u && ch[fa[u]][1] != u; } n int which(int u) { return u == ch[fa[u]][1]; } n void pushup(int o) { n   int lc = ch[o][0], rc = ch[o][1]; n   siz[o] = siz[lc] + siz[rc] + ssiz[o]; n   all[o] = all[lc] + all[rc] + a[o] * ssiz[o]; n   ans[o] = ans[lc] + ans[rc] + sans[o]; n   ans[o] += (ssiz[o] * ssiz[o] - sqsiz[o]) * a[o]; n   ans[o] += 2 * siz[rc] * ssiz[o] * a[o]; n   ans[o] += 2 * all[lc] * (siz[o] - siz[lc]); n } n void rotate(int x) { n   int y = fa[x], z = fa[y], md = which(x); n   if (!isroot(y)) { n     ch[z][which(y)] = x; n   } n   fa[x] = z; n   ch[y][md] = ch[x][!md]; n   fa[ch[y][md]] = y; n   ch[x][!md] = y; n   fa[y] = x; n   pushup(y); n   pushup(x); n } n void splay(int u) { n   while (!isroot(u)) { n     if (!isroot(fa[u])) { n       rotate(which(u) == which(fa[u]) ? fa[u] : u); n     } n     rotate(u); n   } n } n void access(int u) { n   for (int v = 0; u; v = u, u = fa[u]) { n     splay(u); n     int rc = ch[u][1]; n     ssiz[u] += siz[rc]; n     sans[u] += ans[rc]; n     sqsiz[u] += siz[rc] * siz[rc]; n     ch[u][1] = v; n     ssiz[u] -= siz[v]; n     sans[u] -= ans[v]; n     sqsiz[u] -= siz[v] * siz[v]; n     pushup(u); n   } n } n void link(int u, int v) { n   access(v); n   splay(v); n   access(u); n   splay(u); n   fa[u] = v; n   ssiz[v] += siz[u]; n   sans[v] += ans[u]; n   sqsiz[v] += siz[u] * siz[u]; n   pushup(v); n } n void cut(int u, int v) { n   access(u); n   access(v); n   splay(v); n   splay(u); n   fa[u] = 0; n   ssiz[v] -= siz[u]; n   sans[v] -= ans[u]; n   sqsiz[v] -= siz[u] * siz[u]; n   pushup(v); n } n bool check(int u, int v) { n   access(v); n   splay(v); n   splay(u); n   if (isroot(v)) { n     return false; n   } n   return true; n } n int main() { n   scanf( %d , &n); n   for (int i = 2; i <= n; i++) { n     scanf( %d , &f[i]); n   } n   for (int i = 1; i <= n; i++) { n     scanf( %lld , &a[i]); n     ans[i] = all[i] = a[i]; n   } n   for (int i = 1; i <= n; i++) { n     siz[i] = 1; n     ssiz[i] = 1; n   } n   for (int i = 2; i <= n; i++) { n     link(i, f[i]); n   } n   access(1); n   splay(1); n   printf( %.10lf n , 1.0 * ans[1] / n / n); n   scanf( %d , &m); n   while (m--) { n     scanf( %s%d%d , op, &x, &y); n     if (op[0] ==  P ) { n       if (check(x, y)) { n         swap(x, y); n       } n       cut(x, f[x]); n       f[x] = y; n       link(x, f[x]); n       access(1); n       splay(1); n       printf( %.10lf n , 1.0 * ans[1] / n / n); n     } else { n       access(x); n       splay(x); n       a[x] = y; n       pushup(x); n       printf( %.10lf n , 1.0 * ans[x] / n / n); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   long long x, y, n, m, t, q, a, b, count = 0; n   cin >> n >> m; n   long long arr[n]; n   for (x = 0; x < n; x++) cin >> arr[x]; n   a = 0; n   for (x = 0; x < n; x++) { n     a = a + arr[x]; n     cout << a / m <<    ; n     a = a % m; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int qread() { n   char c; n   int s = 0, f = 1; n   while ((c = getchar()) <  0  || c >  9 ) (c ==  - ) && (f = -1); n   do s = s * 10 + c -  0 ; n   while ((c = getchar()) >=  0  && c <=  9 ); n   return s * f; n } n int n, m; n const int mod = 1e9 + 7; n int a[100011], pos[100011], bin[100011], inv[100011], sum[100011], n     lp = 0, val[100011], vv[100011], svv[100011]; n struct Ques { n   int l, r, id; n   bool operator<(const Ques &b) const { return r < b.r; } n } q[100011]; n int ans[100011]; n int WWW(long long v) { return v > 2000000001 ? 2000000001 : v; } n int main() { n   n = qread(); n   m = qread(); n   bin[0] = inv[0] = 1; n   for (int i = 1; i <= n; i++) n     a[i] = qread(), bin[i] = (bin[i - 1] << 1) % mod, n     inv[i] = 1ll * inv[i - 1] * ((mod + 1) >> 1) % mod, n     sum[i] = ((sum[i - 1] + 1ll * a[i] * bin[i]) % mod + mod) % mod; n   for (int i = 1; i <= m; i++) { n     q[i].l = qread(); n     q[q[i].id = i].r = qread(); n   } n   sort(q + 1, q + 1 + m); n   pos[lp = 1] = 1; n   int j = 1; n   val[1] = a[1] * 2; n   vv[1] = svv[1] = ((a[1] * 2) % mod + mod) % mod; n   while (j <= m && q[j].r == 1) ans[q[j].id] = (a[1] + mod) % mod, j++; n   for (int i = 2; i <= n; i++) { n     if (a[i] <= 0) n       pos[++lp] = i, val[lp] = 2 * a[i], n       vv[lp] = (a[i] * 2ll % mod + mod) % mod, n       svv[lp] = (svv[lp - 1] + vv[lp]) % mod; n     else { n       int cur = n           WWW(0ll + val[lp] + bin[pos[lp] - pos[lp - 1] + 1] * 1ll * a[i]); n       int cvv = (vv[lp] + bin[pos[lp] - pos[lp - 1] + 1] * 1ll * a[i]) % mod; n       lp--; n       while (lp && cur > 0) { n         cur = WWW(0ll + val[lp] + n                   (pos[lp] - pos[lp - 1] > 31 ? 2000000001 n                                               : bin[pos[lp] - pos[lp - 1]]) * n                       1ll * cur); n         cvv = (vv[lp] + bin[pos[lp] - pos[lp - 1]] * 1ll * cvv) % mod; n         lp--; n       } n       val[++lp] = cur; n       pos[lp] = i; n       vv[lp] = cvv; n       svv[lp] = (svv[lp - 1] + vv[lp]) % mod; n     } n     while (j <= m && q[j].r == i) { n       if (q[j].l == q[j].r) { n         ans[q[j].id] = (a[i] + mod) % mod; n         j++; n         continue; n       } n       int l = q[j].l + 1, Ans = (a[q[j].l] + mod) % mod; n       int L = 1, R = lp; n       while (L < R) { n         int mid = (L + R) >> 1; n         if (pos[mid] >= l) n           R = mid; n         else n           L = mid + 1; n       } n       Ans = (Ans + svv[lp] - svv[L]) % mod; n       Ans = (Ans + mod) % mod; n       Ans = (Ans + (sum[pos[L]] - sum[l - 1] + mod) * 1ll * inv[l - 1]) % mod; n       ans[q[j].id] = Ans; n       j++; n     } n   } n   for (int i = 1; i <= m; i++) printf( %d n , ans[i]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long mod = 1000000007; n const long long inf = 1000000000000000000; n int main() { n   long long t; n   cin >> t; n   while (t--) { n     long long n; n     cin >> n; n     vector<pair<long long, long long> > p(n); n     for (auto& x : p) cin >> x.first >> x.second; n     sort(p.begin(), p.end()); n     pair<long long, long long> now = {0, 0}; n     bool f = 1; n     string ans =   ; n     for (int i = 0; i < n; i++) { n       if (p[i].first >= now.first && p[i].second >= now.second) { n         long long r = p[i].first - now.first, u = p[i].second - now.second; n         while (r--) ans +=  R ; n         while (u--) ans +=  U ; n         now = p[i]; n       } else { n         f = 0; n         break; n       } n     } n     if (f) n       cout <<  YES n  << ans << endl; n     else n       cout <<  NO n ; n   } n   return 0; n } n


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


#include <bits/stdc++.h> n using namespace std; n const int N = 2e5 + 65, mod = 1e9 + 7; n vector<int> g[N]; n int w[N]; n int col[N], pr[N]; n int sz[N][2]; n long long ans = 0; n void dfs(int v, int par = -1, int c = 0) { n   pr[v] = par; n   ++sz[v][c]; n   col[v] = c; n   for (int u : g[v]) n     if (u != par) { n       dfs(u, v, c ^ 1); n       sz[v][0] += sz[u][0]; n       sz[v][1] += sz[u][1]; n     } n } n int main() { n   int n; n   scanf( %d , &n); n   for (int i = 0; i < n; ++i) scanf( %d , w + i); n   for (int i = 1; i < n; ++i) { n     int u, v; n     scanf( %d %d , &u, &v); n     --u, --v; n     g[u].push_back(v); n     g[v].push_back(u); n   } n   ans = 0; n   dfs(0); n   for (int v = 0; v < n; ++v) { n     vector<pair<int, int> > vs; n     for (int u : g[v]) n       if (u != pr[v]) vs.emplace_back(sz[u][0], sz[u][1]); n     if (pr[v] != -1) vs.emplace_back(sz[0][0] - sz[v][0], sz[0][1] - sz[v][1]); n     long long cnt = n; n     if (col[v]) { n       for (pair<int, int> e : vs) { n         cnt = (cnt + 1LL * e.second * (n - e.first - e.second)) % mod; n         cnt = (cnt - 1LL * e.first * (n - e.first - e.second)) % mod; n       } n     } else { n       for (pair<int, int> e : vs) { n         cnt = (cnt - 1LL * e.second * (n - e.first - e.second)) % mod; n         cnt = (cnt + 1LL * e.first * (n - e.first - e.second)) % mod; n       } n     } n     cnt %= mod; n     if (cnt < 0) cnt += mod; n     ans = (ans + cnt * w[v]) % mod; n   } n   ans %= mod; n   if (ans < 0) ans += mod; n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int inf = 1 << 28; n const double INF = 1e12, EPS = 1e-9; n int n, m, k; n bool star[600][600]; n int sum[600][600]; n char in[600][600]; n void run() { n   cin >> n >> m >> k; n   for (int i = 0; i < n; i++) cin >> in[i]; n   for (int i = 0; i < n; i++) n     for (int j = 0; j < m; j++) { n       if (in[i][j] ==  1  && in[i + 1][j] ==  1  && in[i + 2][j] ==  1  && j && n           in[i + 1][j - 1] ==  1  && in[i + 1][j + 1] ==  1 ) n         star[i][j] = 1; n     } n   long long ans = 0; n   for (int i = 0; i < n; i++) n     for (int j = 0; j < m; j++) n       sum[i + 1][j + 1] = n           star[i][j] + sum[i][j + 1] + sum[i + 1][j] - sum[i][j]; n   for (int a = 0; a < n; a++) n     for (int b = a + 3; b <= n; b++) { n       int l = 0, r = 2, cnt = 0; n       for (; l < m; l++) { n         while (r <= m && cnt < k) { n           r++; n           cnt += sum[b - 2][r - 1] - sum[a][r - 1] - n                  (sum[b - 2][r - 2] - sum[a][r - 2]); n         } n         if (cnt < k || r > m) break; n         ans += m - r + 1; n         cnt -= sum[b - 2][l + 2] - sum[a][l + 2] - n                (sum[b - 2][l + 1] - sum[a][l + 1]); n       } n     } n   cout << ans << endl; n } n int main() { run(); } n


#include <bits/stdc++.h> n using namespace std; n void readi(int &x) { n   int v = 0, f = 1; n   char c = getchar(); n   while (!isdigit(c) && c !=  - ) c = getchar(); n   if (c ==  - ) n     f = -1; n   else n     v = v * 10 + c -  0 ; n   while (isdigit(c = getchar())) v = v * 10 + c -  0 ; n   x = v * f; n } n void readll(long long &x) { n   long long v = 0ll, f = 1ll; n   char c = getchar(); n   while (!isdigit(c) && c !=  - ) c = getchar(); n   if (c ==  - ) n     f = -1; n   else n     v = v * 10 + c -  0 ; n   while (isdigit(c = getchar())) v = v * 10 + c -  0 ; n   x = v * f; n } n void readc(char &x) { n   char c; n   while ((c = getchar()) ==    ) n     ; n   x = c; n } n void writes(string s) { puts(s.c_str()); } n void writeln() { writes(  ); } n void writei(int x) { n   if (!x) putchar( 0 ); n   char a[25]; n   int top = 0; n   while (x) { n     a[++top] = (x % 10) +  0 ; n     x /= 10; n   } n   while (top) { n     putchar(a[top]); n     top--; n   } n } n void writell(long long x) { n   if (!x) putchar( 0 ); n   char a[25]; n   int top = 0; n   while (x) { n     a[++top] = (x % 10) +  0 ; n     x /= 10; n   } n   while (top) { n     putchar(a[top]); n     top--; n   } n } n inline long long inc(int &x) { return ++x; } n inline long long inc(long long &x) { return ++x; } n inline long long inc(int &x, long long y) { return x += y; } n inline long long inc(long long &x, long long y) { return x += y; } n inline double inc(double &x, double y) { return x += y; } n inline long long dec(int &x) { return --x; } n inline long long dec(long long &x) { return --x; } n inline long long dec(int &x, long long y) { return x -= y; } n inline long long dec(long long &x, long long y) { return x -= y; } n inline double dec(double &x, double y) { return x -= y; } n inline long long mul(int &x) { return x = ((long long)x) * x; } n inline long long mul(long long &x) { return x = x * x; } n inline long long mul(int &x, long long y) { return x *= y; } n inline long long mul(long long &x, long long y) { return x *= y; } n inline double mul(double &x, double y) { return x *= y; } n inline long long divi(const int &x) { n   long long ans, l, r, mid; n   ans = 0; n   l = 0; n   r = 0x3fffffff; n   while (l < r) { n     mid = (l + r) / 2; n     if (mid * mid <= x) { n       ans = mid; n       l = mid + 1; n     } else n       r = mid; n   } n   return ans; n } n inline long long divi(const long long &x) { n   long long ans, l, r, mid; n   ans = 0; n   l = 0; n   r = 0x3fffffff; n   while (l < r) { n     mid = (l + r) / 2; n     if (mid * mid <= x) { n       ans = mid; n       l = mid + 1; n     } else n       r = mid; n   } n   return ans; n } n inline long long divi(int &x, long long y) { return x /= y; } n inline long long divi(long long &x, long long y) { return x /= y; } n inline double divi(double &x, double y) { return x /= y; } n inline long long mod(int &x, long long y) { return x %= y; } n inline long long mod(long long &x, long long y) { return x %= y; } n int n, m, i, j, ct[100005], x, y, vis[100005]; n vector<int> e[100005]; n struct vt { n   int id, deg; n } a[100005]; n bool cmp(vt x, vt y) { return x.deg > y.deg; } n bool dfs(int x) { n   if (vis[x] == 1) return 1; n   if (vis[x] == 2) return 0; n   vis[x] = 1; n   for (__typeof((e[x]).begin()) it = (e[x]).begin(); it != (e[x]).end(); it++) n     if (dfs(*it)) return 1; n   vis[x] = 2; n   return 0; n } n bool check(int x) { n   memset(vis, 0, sizeof(vis)); n   vis[x] = 2; n   int i; n   for (i = 1; i <= n; i++) { n     if (!vis[i] && dfs(i)) { n       for (i = 1; i <= n; i++) n         if (vis[i] != 1) ct[i] = 1; n       return 0; n     } n   } n   return 1; n } n int main() { n   double st = clock(); n   ios_base::sync_with_stdio(false); n   ; n   cin >> n >> m; n   if ((1) <= ((m))) n     for (((i)) = (1); ((i)) <= ((m)); ((i))++) { n       cin >> x >> y; n       e[x].push_back(y); n     } n   if ((1) <= ((n))) n     for (((i)) = (1); ((i)) <= ((n)); ((i))++) { n       a[i].id = i; n       a[i].deg = e[i].size(); n     } n   stable_sort(a + 1, a + n + 1, cmp); n   if ((1) <= ((n))) n     for (((i)) = (1); ((i)) <= ((n)); ((i))++) { n       if ((clock() - st) / CLOCKS_PER_SEC > 0.9) break; n       x = a[i].id; n       if (ct[x] || !check(x)) continue; n       cout << x; n       return 0; n     } n   cout <<  -1 ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long mxn = 1003, inf = 1e18 + 18; n int n, m, lcp[mxn][mxn], rk[mxn][mxn], ri[mxn], tot; n long long k, f[mxn]; n string s; n struct S { n   int l, r; n } a[mxn * mxn]; n inline bool cmp(S a, S b) { n   int p = a.l + lcp[a.l][b.l], q = b.l + lcp[a.l][b.l]; n   if (p > a.r or q > b.r) return a.r - a.l < b.r - b.l; n   return s[p] < s[q]; n } n inline long long add(long long a, long long b) { n   return a + b > inf ? inf : a + b; n } n inline long long calc(int md) { n   for (int i = 1; i <= n; ++i) { n     ri[i] = i; n     for (; ri[i] <= n and rk[i][ri[i]] < md;) ++ri[i]; n   } n   memset(f, 0, sizeof(f)), f[n + 1] = 1; n   for (int i = 1; i <= m; ++i) { n     for (int j = n; j; --j) f[j] = add(f[j], f[j + 1]); n     for (int j = 1; j <= n; ++j) f[j] = ri[j] <= n ? f[ri[j] + 1] : 0; n     f[n + 1] = 0; n   } n   return f[1]; n } n int main() { n   cin >> n >> m >> k >> s, s =     + s; n   for (int i = n; i; --i) n     for (int j = n; j; --j) n       if (s[i] == s[j]) lcp[i][j] = lcp[i + 1][j + 1] + 1; n   for (int i = 1; i <= n; ++i) n     for (int j = i; j <= n; ++j) a[++tot] = (S){i, j}; n   sort(a + 1, a + tot + 1, cmp); n   for (int i = 1; i <= tot; ++i) rk[a[i].l][a[i].r] = i; n   int l = 1, r = tot, md; n   for (; l <= r;) { n     md = l + r >> 1; n     if (calc(md) >= k) n       l = md + 1; n     else n       r = md - 1; n   } n   for (int i = a[r].l; i <= a[r].r; ++i) putchar(s[i]); n } n


#include <algorithm> n #include <cmath> n #include <cstdio> n #include <cstring> n #include <iostream> n #include <map> n #include <set> n #include <unordered_map> n  n using namespace std; n typedef long long LL; n const int N = 3e5 + 10, mod = 998244353; n string g[N]; n int dp[N]; n  n int fastPow(int a, int k) { n     int res = 1; n     while (k) { n         if (k & 1) res = (LL)res * a % mod; n         k >>= 1; n         a = (LL)a * a % mod; n     } n     return res; n } n  n int main() { n     ios::sync_with_stdio(false); n     int n, m, sum = 0, res = 0; n     n = 300000; n     for (int i = 2; i <= n; i++) { n         if (i & 1) sum = (sum + 1) % mod; n         dp[i] = ((LL)dp[i - 1] * 2 % mod + sum + !(i & 1)) % mod; n         sum = (sum << 1) % mod; n     } n     sum = 0; n     cin >> n >> m; n     for (int i = 0; i < n; i++) { n         cin >> g[i]; n         for (auto c : g[i]) n             if (c ==  o ) sum++; n     } n     for (int i = 0; i < n; i++) { n         int cnt = 0, len = 0; n         while (cnt <= m) { n             if (cnt == m || g[i][cnt] ==  * ) { n                 if (len) { n                     res = (res + (LL)dp[len] * n                                      fastPow(2, sum - len) % mod) % n                           mod; n                     len = 0; n                 } n             } else { n                 len++; n             } n             cnt++; n         } n     } n     for (int i = 0; i < m; i++) { n         int cnt = 0, len = 0; n         while (cnt <= n) { n             if (cnt == n || g[cnt][i] ==  * ) { n                 if (len) { n                     res = (res + (LL)dp[len] * n                                      fastPow(2, sum - len) % mod) % n                           mod; n                     len = 0; n                 } n             } else { n                 len++; n             } n             cnt++; n         } n     } n     cout << res << endl; n     return 0; n }


#include <bits/stdc++.h> n using namespace std; n const int maxn = 100 + 10; n char s[maxn]; n int f(char ch) { n   if (ch ==  A ) return 1; n   if (ch ==  B ) return 2; n   if (ch ==  C ) return 4; n   return 0; n } n int main() { n   scanf( %s , s + 1); n   int l = strlen(s + 1); n   for (int i = 1; i <= l - 2; ++i) { n     int t = f(s[i]) | f(s[i + 1]) | f(s[i + 2]); n     if (t == 7) { n       cout <<  Yes  << endl; n       return 0; n     } n   } n   cout <<  No ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<int> tree[500001]; n bool intree[500001]; n vector<int> tmp(0); n bool isleaf(int node) { n   if (tree[node].size() == 1 && intree[(tree[node])[0]]) return true; n   return false; n } n void DFS(int lvl, int node) { n   intree[node] = true; n   if (isleaf(node)) { n     tmp.push_back(lvl); n   } n   for (int i = 0; i < tree[node].size(); i++) { n     if (!intree[(tree[node])[i]]) { n       DFS(lvl + 1, (tree[node])[i]); n     } n   } n } n int main() { n   ios_base::sync_with_stdio(false); n   int N; n   cin >> N; n   for (int i = 1; i <= N; i++) intree[i] = false; n   int x, y; n   for (int i = 0; i < N - 1; i++) { n     cin >> x >> y; n     tree[x].push_back(y); n     tree[y].push_back(x); n   } n   intree[1] = true; n   int ans = 0, c; n   for (int i = 0; i < (tree[1]).size(); i++) { n     tmp.clear(); n     DFS(0, (tree[1])[i]); n     sort(tmp.begin(), tmp.end()); n     c = tmp.size(); n     for (int j = 0; j < c; j++) { n       ans = max(ans, tmp[j] + c - j); n     } n   } n   cout << ans << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(false); n   int n; n   cin >> n; n   vector<string> v(n); n   for (int i = 0; i < n; i++) cin >> v[i]; n   for (int i = 1; i < n - 1; i++) { n     for (int j = 1; j < n - 1; j++) { n       if (v[i][j] ==  #  and v[i + 1][j] ==  #  and v[i][j + 1] ==  #  and n           v[i][j - 1] ==  #  and v[i - 1][j] ==  # ) { n         v[i][j] =  a ; n         v[i + 1][j] =  a ; n         v[i][j + 1] =  a ; n         v[i][j - 1] =  a ; n         v[i - 1][j] =  a ; n       } n     } n   } n   int c = 0; n   for (int i = 0; i < n; i++) n     for (int j = 0; j < n; j++) n       if (v[i][j] ==  # ) { n         c = 1; n         break; n       } n   if (c) n     cout <<  NO  << endl; n   else n     cout <<  YES  << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long N = 1e5 + 5; n int32_t main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   ; n   long long n; n   cin >> n; n   long long con = -1; n   long long a[n + 5]; n   for (long long i = 1; i <= n; i++) { n     cin >> a[i]; n   } n   long long i = 1; n   for (i = 1; i < n; i++) { n     if (a[i + 1] == a[i]) { n       con = i; n       break; n     } n   } n   if (con == -1) { n     for (con = 1; con < n; con++) { n       if (a[con] > a[con + 1]) { n         break; n       } n     } n   } n   for (i = 1; i < con; i++) { n     if (a[i + 1] < a[i]) { n       cout <<  NO  n            <<   n ; n       return 0; n     } n   } n   while (a[con] == a[con + 1]) { n     con++; n   } n   for (i = con; i < n; i++) { n     if (a[i + 1] >= a[i]) { n       cout <<  NO  n            <<   n ; n       return 0; n     } n   } n   cout <<  YES  n        <<   n ; n } n


#include <bits/stdc++.h> n using ll = long long; n using namespace std; n int main() { n   ios::sync_with_stdio(false); n   int N, K; n   cin >> N >> K; n   ll total = N + 1; n   total += N; n   total += min(2 * (K - 1) + (N - K), 2 * (N - K) + K - 1); n   cout << total <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int k, n[20], a[20][5010], g[32768], f[32768], hsh[301781], ps[301781], n     ans1[20], ans2[20], vis[20], TMS; n pair<int, int> md[32768]; n long long sm[20]; n int get(int x) { n   int y = (x + 1000000001) % 301781; n   while (hsh[y] && hsh[y] ^ x + 1000000001) y = (y + 1) % 301781; n   return y; n } n void dg(int x, int s1, int s2) { n   if (x == k) { n     f[s2 + s1] |= f[s1] * g[s2]; n     return; n   } n   dg(x + 1, s1, s2); n   dg(x + 1, s1, s2 + (1 << x)); n   dg(x + 1, s1 + (1 << x), s2); n } n int main() { n   scanf( %d , &k); n   long long S = 0; n   for (int i = 0; i < k; ++i) { n     scanf( %d , n + i); n     for (int j = 0; j < n[i]; ++j) { n       int x; n       scanf( %d , &x); n       a[i][j] = x; n       sm[i] += x; n       S += x; n       int w = get(x); n       ps[w] = i; n       hsh[w] = x + 1000000001; n     } n   } n   if (S % k != 0) { n     printf( No ); n     return 0; n   } n   S /= k; n   for (int i = 0; i < k; ++i) n     for (int j = 0; j < n[i]; ++j) { n       int st = 1 << i, nw = i; n       long long c = S - (sm[i] - a[i][j]); n       int P = 1; n       ++TMS; n       for (;;) { n         vis[nw] = TMS; n         if (abs(c) > 1000000000) { n           P = 0; n           break; n         } n         int w = get(c); n         if (!hsh[w]) { n           P = 0; n           break; n         } n         st |= 1 << ps[w]; n         if (ps[w] == i) { n           P = c == a[i][j]; n           break; n         } n         c = S - (sm[ps[w]] - c); n         nw = ps[w]; n         if (vis[nw] == TMS) { n           P = 0; n           break; n         } n       } n       if (P) { n         md[st] = make_pair(i, j); n         g[st] = 1; n       } n     } n   f[0] = 1; n   dg(0, 0, 0); n   if (f[(1 << k) - 1]) { n     printf( Yes n ); n     int ST = (1 << k) - 1; n     while (ST) { n       for (int _ = ST; _; _ = (_ - 1) & ST) n         if (g[_] * f[ST - _]) { n           int i = md[_].first, j = md[_].second, nw = i; n           long long c = S - (sm[i] - a[i][j]), ls = a[i][j]; n           for (;;) { n             int w = get(c); n             ans1[ps[w]] = c; n             ans2[ps[w]] = nw; n             if (ps[w] == i) break; n             ls = c; n             nw = ps[w]; n             c = S - (sm[ps[w]] - c); n           } n           ST -= _; n           break; n         } n     } n     for (int i = 0; i < k; ++i) printf( %d %d n , ans1[i], ans2[i] + 1); n   } else { n     printf( No ); n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   long long int test = 1; n   for (; test > 0; --test) { n     long long int n, a, b, c, d, c1 = 1, c2, c3, c4; n     cin >> n >> a >> b >> c >> d; n     c2 = c1 + b - c; n     c3 = c2 + a - d; n     c4 = c3 + c - b; n     long long int m = 1 - min(min(c1, c2), min(c3, c4)), n                   m1 = max(max(c1, c2), max(c3, c4)); n     if (m1 + abs(m) > n) n       cout << 0 << endl; n     else n       cout << (n - (m1 + abs(m)) + 1) * n << endl; n   } n   return (0); n } n


#include <bits/stdc++.h> n using namespace std; n const long long int MAX = 1000000007; n const long long int MAXN = 100010; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   long long int t; n   cin >> t; n   while (t--) { n     long long int n, k; n     cin >> n >> k; n     if (n % 2 == 0 && k % 2 == 1) n       cout <<  NO  << endl; n     else if (n % 2 == 1 && k % 2 == 0) n       cout <<  NO  << endl; n     else { n       if (k * k <= n) n         cout <<  YES  << endl; n       else n         cout <<  NO  << endl; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int d[250003]; n int main() { n   int i, j, k, n, m, t, a[501][501], b[501][501]; n   cin >> t; n   while (t--) { n     cin >> n >> m; n     for (i = 0; i < n; i++) n       for (j = 0; j < m; j++) cin >> a[i][j], d[a[i][j]] = i; n     for (i = 0; i < m; i++) n       for (j = 0; j < n; j++) cin >> b[i][j]; n     for (i = 0; i < n; i++) { n       for (j = 0; j < m; j++) { n         cout << a[d[b[0][i]]][j] <<    ; n       } n       cout << endl; n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n vector<pair<long long int, pair<long long int, long long int> > > P; n int level[1007]; n double ans = 0; n bool contains(pair<long long int, pair<long long int, long long int> > A, n               pair<long long int, pair<long long int, long long int> > B) { n   return (A.second.first - B.second.first) * (A.second.first - B.second.first) + n              (A.second.second - B.second.second) * n                  (A.second.second - B.second.second) < n          (A.first) * (A.first); n } n int main() { n   long long int i, j, k, l, m, n, x, y, z, a, b, r; n   scanf( %lld , &n); n   for (i = 0; i < n; i++) { n     scanf( %lld , &x); n     scanf( %lld , &y); n     scanf( %lld , &r); n     P.push_back(make_pair(r, make_pair(x, y))); n   } n   sort(P.begin(), P.end()); n   for (i = n - 1; i >= 0; i--) { n     for (j = n - 1; j > i; j--) { n       if (contains(P[j], P[i])) { n         level[i] = max(level[i], level[j] + 1); n       } n     } n     if (level[i] == 0) level[i] = 1; n   } n   for (i = 0; i < n; i++) { n     if (level[i] <= 2) n       ans += (P[i].first * P[i].first); n     else { n       if (level[i] % 2 == 1) n         ans -= (P[i].first * P[i].first); n       else n         ans += (P[i].first * P[i].first); n     } n   } n   printf( %.10lf n , ans * 3.14159265358979); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n inline long long power(long long a, long long b) { n   long long x = 1; n   a = a % 1000000007ULL; n   while (b) { n     if (b & 1) x = (x * a) % 1000000007ULL; n     a = (a * a) % 1000000007ULL; n     b >>= 1; n   } n   return x; n } n inline long long inv(long long a) { return power(a, 1000000007ULL - 2); } n long long gcd(long long a, long long b) { return a ? gcd(b % a, a) : b; } n const int N = 2e5 + 5; n vector<int> adj[N]; n int n, dis[N]; n bool vis[N]; n int cnt[N], p[N]; n int tdis[N]; n vector<int> d[N]; n void dfs(int u, int dd, int par) { n   vis[u] = true; n   if (dd < 3) n     tdis[u] = dd; n   else n     tdis[u] = N; n   dis[u] = dd; n   d[dd].push_back(u); n   p[u] = par; n   for (auto v : adj[u]) { n     if (!vis[v]) dfs(v, dd + 1, u); n   } n } n int main() { n   ios_base::sync_with_stdio(false); n   cin >> n; n   int u, v; n   for (int i = 1; i < n; i++) { n     cin >> u >> v; n     adj[u].push_back(v); n     adj[v].push_back(u); n   } n   dfs(1, 0, 0); n   int maxx = 0; n   for (int i = 0; i < n; i++) { n     if (d[i].size() == 0) break; n     maxx = i; n   } n   int ans = 0; n   for (int i = maxx; i > 1; i--) { n     for (auto v : d[i]) { n       if (dis[v] < 2 || tdis[v] == 2) continue; n       ans++; n       tdis[p[v]] = 2; n       for (auto j : adj[p[v]]) tdis[j] = 2; n     } n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int mod = 998244353; n long long a[300001]; n long long n, m, k; n int b[300001]; n void solve() { n   cin >> n; n   for (int i = 0; i < n; ++i) { n     int x; n     cin >> x; n     a[x]++; n     b[a[x]] += a[x]; n     k = (b[a[x]] >= i && b[a[x]] != n) ? b[a[x]] : k; n   } n   cout << k + 1; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   int t = 1; n   while (t--) { n     solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   char str[2020]; n   long l; n   cin >> l; n   cin >> str; n   int i, j, hs = 0, ts = 0; n   for (i = 0; i < l; i++) { n     if (str[i] ==  H ) n       hs++; n     else n       ts++; n     str[l + i] = str[i]; n   } n   int res = 1000000000; n   int t; n   for (i = 0; i < l; i++) { n     t = 0; n     for (j = 0; j < hs; j++) { n       if (str[i + j] ==  T ) t++; n     } n     if (t < res) res = t; n     t = 0; n     for (j = 0; j < ts; j++) { n       if (str[i + j] ==  H ) t++; n     } n     if (t < res) res = t; n   } n   cout << res; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void mohammed() { n   ios::sync_with_stdio(0); n   ios_base::sync_with_stdio(0); n   cin.tie(0), cout.tie(0); n } n int main() { n   mohammed(); n   int n, x, y; n   cin >> n; n   map<int, int> mp; n   map<int, int>::iterator it; n   for (int i = 0; i < n; i++) { n     cin >> x >> y; n     mp[x]++; n     mp[y + 1]--; n   } n   x = 0; n   for (it = mp.begin(); it != mp.end(); it++) { n     it->second += x; n     if (it->second > 2) { n       cout <<  NO ; n       return 0; n     } n     x = it->second; n   } n   cout <<  YES ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const double pi = acos(-1.0); n template <typename T, typename U> n inline void amin(T &x, U y) { n   if (y < x) x = y; n } n template <typename T, typename U> n inline void amax(T &x, U y) { n   if (x < y) x = y; n } n template <class L, class R> n ostream &operator<<(ostream &os, pair<L, R> P) { n   return os <<  (  << P.first <<  ,  << P.second <<  ) n ; n } n template <class T> n ostream &operator<<(ostream &os, set<T> V) { n   os <<  [ ; n   for (auto vv : V) os << vv <<  ,  ; n   return os <<   b b] n ; n } n template <class T> n ostream &operator<<(ostream &os, vector<T> V) { n   os <<  [ ; n   for (auto vv : V) os << vv <<  ,  ; n   return os <<   b b] n ; n } n template <class K, class X> n ostream &operator<<(ostream &os, map<K, X> V) { n   os <<  [ ; n   for (auto vv : V) os << vv <<  ,  ; n   return os <<   b b] n ; n } n long int ceildiv(long int n, long int f) { n   if (n % f) { n     return n / f + 1; n   } else n     return n / f; n } n string checkTicket(vector<long> first, vector<long> second) { n   int n = first.size(); n   if (first[0] < second[0]) { n     for (int i = 0; i < n; i += 1) { n       if (first[i] >= second[i]) { n         return  NO n ; n       } n     } n   } else if (first[0] > second[0]) { n     for (int i = 0; i < n; i += 1) { n       if (first[i] <= second[i]) { n         return  NO n ; n       } n     } n   } else { n     return  NO n ; n   } n   return  YES n ; n } n int main() { n   long int n; n   cin >> n; n   string s; n   cin >> s; n   vector<long> first, second; n   for (int i = 0; i < n; i++) { n     first.push_back(s[i] -  0 ); n   } n   for (int i = n; i < 2 * n; i++) { n     second.push_back(s[i] -  0 ); n   } n   sort(first.begin(), first.end()); n   sort(second.begin(), second.end()); n   cerr << first << second; n   cout << checkTicket(first, second); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 200005; n int t[N]; n int p[N]; n int p2[N]; n int main() { n   std::ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   int n; n   long long sum = 0; n   cin >> n; n   for (int i = 0; i <= n; i++) { n     cin >> t[i]; n     sum += t[i]; n   } n   bool amb = 0; n   for (int i = 1; i <= n; i++) { n     if (t[i] > 1 && t[i - 1] > 1) amb = 1; n   } n   if (!amb) { n     cout <<  perfect ; n     return 0; n   } n   cout <<  ambiguous  <<   n ; n   long long par = 1, k = 2; n   p[1] = 0; n   p2[1] = 0; n   for (int i = 1; i <= n; i++) { n     long long temp = k; n     for (int j = 0; j < t[i]; j++) { n       p[k] = par; n       p2[k] = par; n       k++; n     } n     par = temp; n   } n   k = 2; n   for (int i = 1; i <= n; i++) { n     if (t[i] > 1 && t[i - 1] > 1) { n       p2[k]++; n       break; n     } else n       k += t[i]; n   } n   for (int i = 1; i <= sum; i++) cout << p[i] <<    ; n   cout <<   n ; n   for (int i = 1; i <= sum; i++) cout << p2[i] <<    ; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   cin >> n; n   long long sum1 = 0, sum2 = 0; n   long temp; n   for (int i = 0; i < n; i++) { n     cin >> temp; n     sum1 += temp; n   } n   for (int i = 0; i < n - 1; i++) { n     cin >> temp; n     sum2 += temp; n   } n   cout << sum1 - sum2 <<   n ; n   for (int i = 0; i < n - 2; i++) { n     cin >> temp; n     sum2 -= temp; n   } n   cout << sum2; n   return (0); n } n


#include <bits/stdc++.h> n using namespace std; n const int N = (1 << 20) + 15; n long long dp[N]; n pair<int, int> id[100]; n int bit[100], c[100], st[N]; n int main() { n   int u, a, mi, n, m, i, j, b, x, p = 0, idd; n   long long k, temp; n   long long ret = 4e18; n   scanf( %d%d%d , &n, &m, &b); n   for (i = 0; i < n; ++i) { n     scanf( %d%d%d , &c[i], &id[i].first, &mi); n     a = 0; n     for (j = 0; j < mi; ++j) scanf( %d , &u), a |= 1 << (u - 1); n     bit[i] = a; n     id[i].second = i; n   } n   memset(dp, -1, sizeof(dp)); n   sort(id, id + n); n   dp[0] = 0; n   ++p; n   for (i = 0; i < n; ++i) { n     k = id[i].first, idd = id[i].second; n     if (dp[bit[idd]] != -1 && dp[bit[idd]] <= c[idd]) continue; n     x = p; n     for (j = 0; j < p; ++j) { n       u = st[j] | bit[idd]; n       if (dp[u] == -1 && u != (1 << m) - 1) st[x++] = u; n       if (dp[u] == -1 || dp[u] > dp[st[j]] + c[idd]) dp[u] = dp[st[j]] + c[idd]; n     } n     if (dp[(1 << m) - 1] != -1) ret = min(ret, k * b + dp[(1 << m) - 1]); n     p = x; n   } n   if (ret == 4e18) ret = -1; n   cout << ret << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int cnt[1000100], a[1000100]; n int main() { n   int n, k, ans; n   scanf( %d , &n); n   scanf( %d , &k); n   for (long long i = 0; i < n; i++) scanf( %d , &a[i]); n   int l = 0, r = 0, al, ar; n   set<int> x; n   cnt[a[0]]++; n   x.insert(a[0]); n   ans = 1; n   al = 0, ar = 0; n   while (l < n && r < n) { n     r++; n     x.insert(a[r]); n     cnt[a[r]]++; n     while ((long long)x.size() > k && l <= r) { n       cnt[a[l]]--; n       if (cnt[a[l]] == 0) x.erase(a[l]); n       l++; n     } n     r = max(l, r); n     if (r >= n) break; n     if (r - l + 1 > ans) { n       al = l; n       ar = r; n       ans = r - l + 1; n     } n   } n   printf( %d %d n , al + 1, ar + 1); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int M = 105; n int l[M], r[M], t[M], c[M], res[M], p[M]; n int main() { n   int n, m; n   cin >> n >> m; n   for (int i = 0; i < m; ++i) { n     cin >> l[i] >> r[i] >> t[i] >> c[i]; n   } n   for (int i = 1; i <= n; ++i) p[i] = INT_MAX; n   for (int i = 0; i < m; ++i) n     for (int j = l[i]; j <= r[i]; ++j) n       if (t[i] < p[j]) p[j] = t[i], res[j] = i; n   int sum = 0; n   for (int i = 1; i <= n; ++i) n     if (p[i] != INT_MAX) sum += c[res[i]]; n   cout << sum << endl; n } n


#include <bits/stdc++.h> n using namespace std; n const long double EPS = 1e-9, pi = acos(-1); n const long long N = 1e5 + 5; n long long r[N], h[N], n, F[N], dp[N]; n bool cmp(pair<long long, long long> a, pair<long long, long long> b) { n   if (a.first == b.first) { n     return a.second < b.second; n   } else n     return a.first < b.first; n } n void add(long long d, long long id) { n   while (id <= n) { n     F[id] = max(F[id], d); n     id += (id & -id); n   } n } n long long get(long long i) { n   long long res = 0; n   while (i) { n     res = max(res, F[i]); n     i -= (i & -i); n   } n   return res; n } n signed main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cin >> n; n   for (long long i = 1; i <= n; ++i) cin >> r[i] >> h[i]; n   vector<pair<long long, long long> > A; n   A.clear(); n   for (long long i = 1; i <= n; ++i) { n     A.push_back({r[i] * r[i] * h[i], i}); n   } n   sort(A.begin(), A.end(), cmp); n   long long res = 0, id = 0, prev = A[0].first; n   for (long long i = 0; i < n; ++i) { n     if (prev != A[i].first) { n       while (id < i) { n         add(dp[id], A[id].second); n         id++; n       } n     } n     dp[i] = get(A[i].second) + A[i].first; n     res = max(res, dp[i]); n     prev = A[i].first; n   } n   cout << fixed << setprecision(9) << pi * res << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int t; n   cin >> t; n   string str; n   for (int i = 0; i < t; i++) { n     cin >> str; n     long long int res = 0; n     long long int arr[str.size()]; n     if (str[0] ==  + ) { n       arr[0] = 1; n     } else { n       arr[0] = -1; n     } n     for (int j = 1; j < str.size(); j++) { n       if (str[j] ==  + ) { n         arr[j] = arr[j - 1] + 1; n       } else { n         arr[j] = arr[j - 1] - 1; n       } n     } n     int x = -1; n     for (int j = 0; j < str.size(); j++) { n       if (arr[j] == x) { n         res = res + j + 1; n         x--; n       } n     } n     res = res + str.size(); n     cout << res << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n template <typename T> n void MACRO_VAR_Scan(T& t) { n   std::cin >> t; n } n template <typename First, typename... Rest> n void MACRO_VAR_Scan(First& first, Rest&... rest) { n   std::cin >> first; n   MACRO_VAR_Scan(rest...); n } n template <typename T> n void MACRO_VEC_ROW_Init(int n, T& t) { n   t.resize(n); n } n template <typename First, typename... Rest> n void MACRO_VEC_ROW_Init(int n, First& first, Rest&... rest) { n   first.resize(n); n   MACRO_VEC_ROW_Init(n, rest...); n } n template <typename T> n void MACRO_VEC_ROW_Scan(int p, T& t) { n   std::cin >> t[p]; n } n template <typename First, typename... Rest> n void MACRO_VEC_ROW_Scan(int p, First& first, Rest&... rest) { n   std::cin >> first[p]; n   MACRO_VEC_ROW_Scan(p, rest...); n } n template <typename T> n inline T CHMAX(T& a, const T b) { n   return a = (a < b) ? b : a; n } n template <typename T> n inline T CHMIN(T& a, const T b) { n   return a = (a > b) ? b : a; n } n void CHECKTIME(std::function<void()> f) { n   auto start = std::chrono::system_clock::now(); n   f(); n   auto end = std::chrono::system_clock::now(); n   auto res = std::chrono::duration_cast<std::chrono::nanoseconds>((end - start)) n                  .count(); n   std::cerr <<  [Time:  << res <<  ns  (  << res / (1.0e9) <<  s)] n ; n } n template <class T> n std::vector<std::vector<T>> VV(int n, int m, T init = T()) { n   return std::vector<std::vector<T>>(n, std::vector<T>(m, init)); n } n template <typename S, typename T> n std::ostream& operator<<(std::ostream& os, std::pair<S, T> p) { n   os <<  (  << p.first <<  ,   << p.second <<  ) ; n   return os; n } n using ll = long long; n using ull = unsigned long long; n using ld = long double; n using PAIR = std::pair<ll, ll>; n using PAIRLL = std::pair<ll, ll>; n constexpr ll INFINT = 1 << 30; n constexpr ll INFINT_LIM = (1LL << 31) - 1; n constexpr ll INFLL = 1LL << 60; n constexpr ll INFLL_LIM = (1LL << 62) - 1 + (1LL << 62); n constexpr double EPS = 1e-9; n constexpr ll MOD = 1000000007; n constexpr double PI = 3.141592653589793238462643383279; n template <class T, size_t N> n void FILL(T (&a)[N], const T& val) { n   for (auto& x : a) x = val; n } n template <class ARY, size_t N, size_t M, class T> n void FILL(ARY (&a)[N][M], const T& val) { n   for (auto& b : a) FILL(b, val); n } n template <class T> n void FILL(std::vector<T>& a, const T& val) { n   for (auto& x : a) x = val; n } n template <class ARY, class T> n void FILL(std::vector<std::vector<ARY>>& a, const T& val) { n   for (auto& b : a) FILL(b, val); n } n namespace FFT { n const double pi = std::acos(-1); n std::vector<std::complex<double>> tmp; n size_t sz = 1; n std::vector<std::complex<double>> fft(std::vector<std::complex<double>> a, n                                       bool inv = false) { n   size_t mask = sz - 1; n   size_t p = 0; n   for (size_t i = sz >> 1; i >= 1; i >>= 1) { n     auto& cur = (p & 1) ? tmp : a; n     auto& nex = (p & 1) ? a : tmp; n     std::complex<double> e = std::polar(1., 2 * pi * i * (inv ? -1 : 1) / sz); n     std::complex<double> w = 1; n     for (size_t j = 0; j < sz; j += i) { n       for (size_t k = 0; k < i; ++k) { n         nex[j + k] = n             cur[((j << 1) & mask) + k] + w * cur[(((j << 1) + i) & mask) + k]; n       } n       w *= e; n     } n     ++p; n   } n   if (p & 1) std::swap(a, tmp); n   if (inv) n     for (size_t i = 0; i < sz; ++i) a[i] /= sz; n   return a; n } n std::vector<ll> mul(std::vector<ll> a, std::vector<ll> b) { n   size_t m = a.size() + b.size() - 1; n   sz = 1; n   while (m > sz) sz <<= 1; n   tmp.resize(sz); n   std::vector<std::complex<double>> A(sz), B(sz); n   for (size_t i = 0; i < a.size(); ++i) A[i].real(a[i]); n   for (size_t i = 0; i < b.size(); ++i) B[i].real(b[i]); n   A = fft(A); n   B = fft(B); n   for (size_t i = 0; i < sz; ++i) A[i] *= B[i]; n   A = fft(A, true); n   a.resize(m); n   for (size_t i = 0; i < m; ++i) a[i] = std::round(A[i].real()); n   return a; n } n };  // namespace FFT n signed main() { n   std::ios::sync_with_stdio(false); n   std::cin.tie(0); n   ; n   ll n, x; n   MACRO_VAR_Scan(n, x); n   ; n   std::vector<ll> a(n); n   for (auto& i : a) std::cin >> i; n   ; n   for (ll i = 0; i < ll(n); ++i) a[i] = a[i] < x; n   std::vector<ll> b; n   { n     ll cnt = 0; n     for (ll i = 0; i < ll(n + 1); ++i) { n       if (i == n || a[i]) { n         b.emplace_back(cnt); n         cnt = 0; n       } else { n         ++cnt; n       } n     } n   } n   ll m = b.size(); n   { n     ll ans = 0; n     for (ll i = 0; i < ll(m); ++i) { n       ans += (ll)b[i] * (b[i] + 1) / 2; n     } n     std::cout << (ans); n     ; n   } n   { n     for (ll i = 0; i < ll(m); ++i) ++b[i]; n     auto bb(b); n     std::reverse((bb).begin(), (bb).end()); n     auto c = FFT::mul(b, bb); n     std::vector<ll> ans(n, 0); n     ll p = 0; n     for (ll i = ll(m - 1) - 1; i >= 0; --i) { n       ans[p++] = c[i]; n     } n     for (ll i = 0; i < ll(n); ++i) { n       std::cout <<    ; n       std::cout << (ans[i]); n       ; n     } n     std::cout <<   n ; n     ; n   } n   return 0; n } n


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


#include <bits/stdc++.h> n using namespace std; n const int N = 5e5 + 100; n template <class nmsl> n inline void read(nmsl &x) { n   x = 0; n   char ch = getchar(), w = 0; n   while (!isdigit(ch)) w = (ch ==  - ), ch = getchar(); n   while (isdigit(ch)) x = (x << 1) + (x << 3) + (ch ^ 48), ch = getchar(); n   x = w ? -x : x; n } n long long n, f[N], size[N], root[N], hd[N], bianshu, ans; n struct abc { n   int nxt, to; n } qwq[N * 2]; n inline void addedg(int lai, int qu) { n   qwq[++bianshu].nxt = hd[lai]; n   qwq[bianshu].to = qu; n   hd[lai] = bianshu; n } n inline void first_dp(int u, int father) { n   size[u] = 1; n   for (register int i = hd[u]; i; i = qwq[i].nxt) { n     int v = qwq[i].to; n     if (v == father) continue; n     first_dp(v, u); n     size[u] += size[v]; n     f[u] += f[v]; n   } n   f[u] += size[u]; n   return; n } n inline void dp(int u, int father) { n   f[u] = 0; n   for (register int i = hd[u]; i; i = qwq[i].nxt) { n     int v = qwq[i].to; n     f[u] += f[v]; n   } n   long long tot = f[u] + n; n   ans = max(ans, tot); n   for (register int i = hd[u]; i; i = qwq[i].nxt) { n     int v = qwq[i].to; n     if (v == father) continue; n     size[u] = n - size[v]; n     f[u] = tot - f[v] - size[v]; n     dp(v, u); n   } n } n int main() { n   read(n); n   for (register int i = 1, qa, qb; i < n; i++) n     read(qa), read(qb), addedg(qa, qb), addedg(qb, qa); n   first_dp(1, 0); n   ans = f[1]; n   dp(1, 0); n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int M = 3000; n int n, m; n pair<pair<int, int>, int> v[M]; n map<pair<pair<int, int>, int>, vector<pair<pair<int, int>, int> > > Q; n int gcd(int a, int b) { return !b ? a : gcd(b, a % b); } n pair<pair<int, int>, int> get(int x1, int y1, int x2, int y2) { n   int x = x2 - x1, y = y2 - y1; n   if (x == 0) n     y = 1; n   else if (y == 0) n     x = 1; n   else { n     int d = gcd(x, y); n     x /= d, y /= d; n   } n   if (x < 0) x = -x, y = -y; n   return make_pair(make_pair(x, y), y1 * x - x1 * y); n } n int sign(double x) { n   if (fabs(x) < 1E-6) return 0; n   return (x > 0) ? 1 : -1; n } n double dist(int x1, int y1, int x2, int y2) { n   return sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2)); n } n bool cmp(pair<pair<int, int>, int> a, pair<pair<int, int>, int> b) { n   if (a.first != b.first) return a.first < b.first; n   return a.second > b.second; n } n int main() { n   Q.clear(); n   scanf( %d%d , &n, &m); n   for (int i = 0; i < (n); i++) { n     int x1, y1, x2, y2; n     scanf( %d%d%d%d , &x1, &y1, &x2, &y2); n     x1 *= 2, y1 *= 2, x2 *= 2, y2 *= 2; n     if (make_pair(x1, y1) > make_pair(x2, y2)) { n       swap(x1, x2); n       swap(y1, y2); n     } n     pair<pair<int, int>, int> tmp = get(x1, y1, x2, y2); n     Q[tmp].push_back(make_pair(make_pair(x1, y1), 1)); n     Q[tmp].push_back(make_pair(make_pair(x2, y2), -1)); n   } n   for (int i = 0; i < (m); i++) { n     scanf( %d%d%d , &v[i].first.first, &v[i].first.second, &v[i].second); n     for (int j = 0; j < (i); j++) { n       if (v[i].second != v[j].second) continue; n       int x1 = v[i].first.first, y1 = v[i].first.second, r = v[i].second; n       int x2 = v[j].first.first, y2 = v[j].first.second; n       if (sign(2 * r - dist(x1, y1, x2, y2)) >= 0) continue; n       int x0 = x1 + x2, y0 = y1 + y2; n       int x = x2 * 2 - x0, y = y2 * 2 - y0; n       pair<pair<int, int>, int> tmp = get(x0, y0, x0 - y, y0 + x); n       if (Q.find(tmp) == Q.end()) continue; n       Q[tmp].push_back(make_pair(make_pair(x0, y0), 0)); n     } n   } n   int ret = 0; n   for (__typeof(Q.begin()) it = Q.begin(); it != Q.end(); it++) { n     sort(it->second.begin(), it->second.end(), cmp); n     int now = 0; n     for (__typeof(it->second.begin()) t = it->second.begin(); n          t != it->second.end(); t++) { n       if (t->second == 0) ret += now; n       now += t->second; n     } n   } n   printf( %d n , ret); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int mpow(int base, int exp); n void solve() { n   long double n; n   cin >> n; n   long double sum = 0; n   while (n) { n     long double d; n     d = (1) * 1.0 / n; n     sum += d; n     n--; n   } n   cout << sum <<   n ; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   ; n   solve(); n   return 0; n } n int mpow(int base, int exp) { n   base %= 1000000007; n   int result = 1; n   while (exp > 0) { n     if (exp & 1) result = ((long long int)result * base) % 1000000007; n     base = ((long long int)base * base) % 1000000007; n     exp >>= 1; n   } n   return result; n } n


#include <bits/stdc++.h> n using namespace std; n long long unsigned int countDivisors(long long unsigned int n) { n   long long unsigned int cnt = 0; n   for (long long unsigned int i = 1; i <= sqrt(n); i++) { n     if (n % i == 0) { n       if (n / i == i) n         ++cnt; n       else n         cnt += 2; n     } n   } n   return cnt; n } n int Search(string pat, string txt) { n   int n = txt.size(); n   int m = pat.size(); n   for (int i = 0; i <= n - m; i++) { n     int j; n     for (j = 0; j < m; j++) { n       if (txt[i + j] != pat[j]) { n         break; n       } n     } n     if (j == m) { n       return i; n     } n   } n   return -1; n } n int findFirst(const std::vector<long long int> &array, int key, int low, n               int high) { n   int ans = -1; n   while (low <= high) { n     int mid = low + (high - low) / 2; n     if (key == array[mid]) { n       ans = mid; n       high = mid - 1; n     } else if (key > array[mid]) n       low = mid + 1; n     else n       high = mid - 1; n   } n   return ans; n } n int findLast(const std::vector<long long int> &array, int key, int low, n              int high) { n   int ans = -1; n   while (low <= high) { n     int mid = low + (high - low) / 2; n     if (key == array[mid]) { n       ans = mid; n       low = mid + 1; n     } else if (key > array[mid]) n       low = mid + 1; n     else n       high = mid - 1; n   } n   return ans; n } n int d, x, y; n void extendedEuclid(int a, int b) { n   if (b == 0) { n     d = 1; n     x = 1; n     y = 0; n   } else { n     extendedEuclid(b, a % b); n     int temp = x; n     x = y; n     y = temp - (a / b) * y; n   } n } n int main() { n   std::ios::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   ; n   int t; n   t = 1; n   for (int tc = 0; tc < t; tc++) { n     int n; n     cin >> n; n     vector<int> arr(n), brr(n, 0), v; n     for (int i1 = 0; i1 < n; i1++) cin >> arr[i1]; n     int pos = 0; n     for (int i = 0; i < n; i++) { n       int indx = n - arr[i]; n       brr[indx] = 1; n       while (brr[pos] == 1 && pos < n) { n         cout << (n - pos) <<    ; n         pos++; n       } n       cout << endl; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long mp[1005][1005]; n long long n, a, b; n void output() { n   for (long long i = 1; i <= n; i++) { n     for (long long j = 1; j <= n; j++) { n       if (i == j) n         printf( 0 ); n       else n         printf( %lld , a < b ? !mp[i][j] : mp[i][j]); n     } n     printf(  n ); n   } n } n int main() { n   scanf( %lld%lld%lld , &n, &a, &b); n   long long root = 2; n   if ((a != 1 && b != 1)) { n     printf( NO n ); n     return 0; n   } n   if (n == 2 && a == 1 && b == 1) { n     printf( NO n ); n     return 0; n   } n   if (n == 3 && a == 1 && b == 1) { n     printf( NO n ); n     return 0; n   } n   printf( YES n ); n   if (a == 1 && b == 1) n     for (long long i = 1; i < n; i++) mp[i][i + 1] = mp[i + 1][i] = true; n   else n     for (long long i = 0; i < n - max(a, b); i++, root++) n       mp[1][root] = mp[root][1] = true; n   output(); n } n


#include <bits/stdc++.h> n using namespace std; n long long power(long long a, long long b) { n   long long ans = 1; n   while (b > 0) { n     if (b % 2 != 0) { n       ans = (ans * a) % 1000000007; n     } n     a = ((a % 1000000007) * (a % 1000000007)) % 1000000007; n     b >>= 1; n   } n   return ans; n } n void sieve(int n) { n   bool prime[n + 1]; n   memset(prime, true, sizeof(prime)); n   for (int p = 2; p * p <= n; p++) { n     if (prime[p]) { n       for (int i = p * p; i <= n; i += p) prime[i] = false; n     } n   } n   for (int p = 2; p <= n; p++) { n     if (prime[p]) cout << p <<    ; n   } n } n int main() { n   std::ios::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   ; n   int t = 1; n   cin >> t; n   while (t--) { n     int n, k; n     cin >> n >> k; n     vector<vector<int>> v; n     for (int i = 0; i < k; i++) { n       int c; n       cin >> c; n       vector<int> temp; n       for (int j = 0; j < c; j++) { n         int z; n         cin >> z; n         temp.push_back(z); n       } n       v.push_back(temp); n     } n     int l = 1, h = n; n     int ma; n     cout <<  ?  n          <<     << n <<    ; n     for (int i = 1; i <= n; i++) cout << i <<    ; n     cout << endl; n     cin >> ma; n     for (int i = 0; i < 10; i++) { n       int mid = (h + l) / 2; n       int temp; n       cout <<  ?  n            <<     << mid - l + 1 <<    ; n       for (int kha = l; kha <= mid; kha++) cout << kha <<    ; n       cout << endl; n       cin >> temp; n       if (temp == ma) n         h = mid; n       else n         l = mid; n     } n     int ind = h; n     bool flag = false; n     vector<int> ans; n     for (int i = 0; i < v.size(); i++) { n       sort(v[i].begin(), v[i].end()); n       for (int j = 0; j < v[i].size(); j++) { n         if (v[i][j] == ind) { n           flag = true; n           cout <<  ?  n                <<    ; n           vector<int> loa; n           for (int lo = 1; lo <= n; lo++) { n             if (binary_search(v[i].begin(), v[i].end(), lo) == false) { n               loa.push_back(lo); n             } n           } n           cout << loa.size() <<    ; n           for (auto lo : loa) cout << lo <<    ; n           cout << endl; n           int kha; n           cin >> kha; n           ans.push_back(kha); n         } n       } n       if (flag == false) { n         ans.push_back(ma); n       } else { n         flag = false; n       } n     } n     cout <<  !  n          <<    ; n     for (auto i : ans) cout << i <<    ; n     cout << endl; n     string sa; n     cin >> sa; n     if (sa ==  Correct ) { n       continue; n     } else { n       return 0; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long INF = 1e18; n const int maxn = 2e6 + 10; n int a[maxn], cnt[maxn]; n long long sum[maxn]; n int main(void) { n   int n, x, y; n   scanf( %d %d %d , &n, &x, &y); n   int z = x / y; n   for (int i = 1; i <= n; i++) n     scanf( %d , a + i), cnt[a[i]]++, sum[a[i]] += a[i]; n   for (int i = 1; i <= 2000000; i++) cnt[i] += cnt[i - 1], sum[i] += sum[i - 1]; n   long long ans = INF; n   for (int i = 2; i <= 1000000; i++) { n     long long tmp = 0; n     int zz = min(z, i - 1); n     for (int j = 1; (j - 1) * i < 1000000; j++) { n       if (i - zz - 2 >= 0) n         tmp += (long long)(cnt[j * i - zz - 1] - cnt[j * i - i]) * x; n       if (zz) n         tmp += ((long long)i * j * (cnt[j * i - 1] - cnt[j * i - zz - 1]) - n                 (sum[j * i - 1] - sum[j * i - zz - 1])) * n                y; n     } n     ans = min(ans, tmp); n   } n   printf( %I64d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int md = 1e9 + 7; n int n; n char s[111]; n int f[111][3333]; n int main() { n   int t; n   f[0][0] = 1; n   for (int i = 1; i <= 100; ++i) { n     for (int j = 0; j <= 3000; ++j) { n       for (int c = 0; c <= min(j, 25); ++c) { n         f[i][j] += f[i - 1][j - c]; n         f[i][j] %= md; n       } n     } n   } n   scanf( %d , &t); n   while (t--) { n     scanf( %s , s); n     int sum = 0; n     for (int i = 0; i < strlen(s); ++i) sum += s[i] -  a ; n     printf( %d n , (f[strlen(s)][sum] + md - 1) % md); n   } n   return 0; n } n


#include <bits/stdc++.h> n #pragma GCC optimize ( O3 ) n #pragma GCC target ( sse4 ) n   n using namespace std; n   n typedef long long ll; n typedef long double ld; n typedef complex<ld> cd; n   n typedef pair<int, int> pi; n typedef pair<ll,ll> pl; n typedef pair<ld,ld> pd; n   n typedef vector<int> vi; n typedef vector<ld> vd; n typedef vector<ll> vl; n typedef vector<pi> vpi; n typedef vector<pl> vpl; n typedef vector<cd> vcd; n  n template<class T> using pq = priority_queue<T>; n template<class T> using pqg = priority_queue<T, vector<T>, greater<T>>; n   n #define FOR(i, a, b) for (int i=a; i<(b); i++) n #define F0R(i, a) for (int i=0; i<(a); i++) n #define FORd(i,a,b) for (int i = (b)-1; i >= a; i--) n #define F0Rd(i,a) for (int i = (a)-1; i >= 0; i--) n #define trav(a,x) for (auto& a : x) n #define uid(a, b) uniform_int_distribution<int>(a, b)(rng) n   n #define sz(x) (int)(x).size() n #define mp make_pair n #define pb push_back n #define f first n #define s second n #define lb lower_bound n #define ub upper_bound n #define all(x) x.begin(), x.end() n #define ins insert n  n template<class T> bool ckmin(T& a, const T& b) { return b < a ? a = b, 1 : 0; } n template<class T> bool ckmax(T& a, const T& b) { return a < b ? a = b, 1 : 0; } n   n mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); n  n void __print(int x) {cerr << x;} n void __print(long x) {cerr << x;} n void __print(long long x) {cerr << x;} n void __print(unsigned x) {cerr << x;} n void __print(unsigned long x) {cerr << x;} n void __print(unsigned long long x) {cerr << x;} n void __print(float x) {cerr << x;} n void __print(double x) {cerr << x;} n void __print(long double x) {cerr << x;} n void __print(char x) {cerr <<      << x <<     ;} n void __print(const char *x) {cerr <<     << x <<    ;} n void __print(const string &x) {cerr <<     << x <<    ;} n void __print(bool x) {cerr << (x ?  true  :  false );} n  n template<typename T, typename V> n void __print(const pair<T, V> &x) {cerr <<  { ; __print(x.first); cerr <<  ,  ; __print(x.second); cerr <<  } ;} n template<typename T> n void __print(const T &x) {int f = 0; cerr <<  { ; for (auto &i: x) cerr << (f++ ?  ,   :   ), __print(i); cerr <<  } ;} n void _print() {cerr <<  ] n ;} n template <typename T, typename... V> n void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr <<  ,  ; _print(v...);} n #ifdef DEBUG n #define dbg(x...) cerr <<   [91m <<__func__<< : <<__LINE__<<  [  << #x <<  ] = [ ; _print(x); cerr <<   [39m  << endl; n #else n #define dbg(x...) n #endif n  n  n const int MOD = 1000000007; n const char nl =   n ; n const int MX = 200001;  n  n int dist[MX]; n int A[MX]; n set<pi> rem; n pqg<pi> q; n  n void add(int p, int pre, bool ty) { n     auto it1 = rem.lb({p, -1}); n     if (it1 != rem.end()) { n         int v = it1->s; n         int d = max(pre, abs(A[v]-p)); n         if (ty) { n             d = pre + abs(A[v]-p); n         } n         if (d < dist[v]) { n             dist[v] = d; n             q.push({d, v}); n         } n     } n     if (it1 != rem.begin()) { n         it1--; n         int v = it1->s; n         int d = max(pre, abs(A[v]-p)); n         if (ty) { n             d = pre + abs(A[v]-p); n         } n         if (d < dist[v]) { n             dist[v] = d; n             q.push({d, v}); n         } n     } n } n  n void push(int p, int K) { n     while (rem.lb({p, -1}) != rem.end() && rem.lb({p, -1})->f <= p+K) { n         int x = rem.lb({p, -1})->s; n         rem.erase({A[x], x}); n         q.push({K, x}); n         dist[x] = K; n     } n     auto it = rem.lb({p, -1}); n     while (it != rem.begin()) { n         it--; n         if (it->f < p-K) break; n         int x = it->s; n         rem.erase({A[x], x}); n         q.push({K, x}); n         dist[x] = K; n         it = rem.lb({p, -1}); n     } n } n  n  n void solve() { n     int N, Q; cin >> N >> Q; n     F0R(i, N) dist[i] = 100000000; n     int S; cin >> S; S--; n     int D; cin >> D; n     F0R(i, N) { n         cin >> A[i]; n         if (i != S) rem.ins({A[i], i}); n     } n     dist[S] = 0; n     add(A[S]+D, 0, true); n     add(A[S]-D, 0, true); n     bool vis[MX]; F0R(i, N) vis[i] = false; n     while (!q.empty()) { n         int v = q.top().s; n         int d = q.top().f; n         q.pop(); n         if (vis[v]) continue; n         vis[v] = true; n         //dbg(v, d); n         rem.erase({A[v], v}); n         add(A[v], d, true); n         push(A[v]+D, d); n         push(A[v]-D, d); n         add(A[v]+D, 0, true); n         add(A[v]-D, 0, true); n     } n     /*F0R(i, N) { n         cout << dist[i] <<    ; n     } n     cout << nl;*/ n  n     while(Q--) { n         int X, V; cin >> X >> V; X--; n         cout << (dist[X]<=V? Yes : No ) << nl; n     } n } n   n int main() { n     cin.tie(0)->sync_with_stdio(0);  n     cin.exceptions(cin.failbit); n  n     int T = 1; n //    cin >> T; n     while(T--) { n         solve(); n     } n  n  return 0; n } n  n  n


#include <bits/stdc++.h> n using namespace std; n long long k, b, n, t; n int main() { n   ios_base ::sync_with_stdio(0); n   cin.tie(0); n   cin >> k >> b >> n >> t; n   long long num = 1; n   long long step = n; n   while (t >= num) { n     num = num * k + b; n     step--; n   } n   if (step < 0) { n     cout << 0; n     return 0; n   } n   cout << step + 1; n   return 0; n } n


#include <bits/stdc++.h> n const long double pi = 3.14159265359; n const long long N = 1e9 + 7; n using namespace std; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   long long n, m, arr[200005]; n   cin >> n >> m; n   long long cntne = 0, cntpo = 0; n   for (int i = 0; i < n; i++) { n     cin >> arr[i]; n     if (arr[i] == -1) n       cntne++; n     else n       cntpo++; n   } n   vector<long long> v; n   for (int i = 0; i < m; i++) { n     long long l, r; n     cin >> l >> r; n     r++; n     long long ans = abs(l - r); n     if ((ans % 2 == 0) && (cntne >= ans / 2) && (cntpo >= ans / 2)) n       v.push_back(1); n     else n       v.push_back(0); n   } n   for (int i = 0; i < ((long long)((v).size())); i++) cout << v[i] << endl; n   return 0; n } n


#include <iostream> n #include <vector> n #include <algorithm> n #include <string> n  n #define ll long long n  n using namespace std; n  n int main() { n     ll qua; n     cin >> qua; n     for (int i = 0; i < qua; ++i) { n         ll n; n         cin >> n; n         vector<int> vec(n); n         for (int j = 0; j < n; ++j) { n             cin >> vec[j]; n         } n         cout << 0 <<    ; n         for (int j = 1; j < n; ++j) { n             ll r = (vec[j] | vec[j - 1]) - vec[j]; n             cout << r <<    ; n             vec[j] += r; n         } n         cout <<   n ; n     } n     return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MaxN = 100005; n const int MaxM = MaxN * 2; n const int oo = 0x3F3F3F3F; n int ax[MaxM], K; n int ret[MaxN]; n set<pair<int, int> > tr[MaxM]; n struct event { n   int l, r, v, tp; n   event() {} n   event(int l, int r, int v, int tp) : l(l), r(r), v(v), tp(tp) {} n   bool operator<(const event& e) const { n     if (r != e.r) n       return r > e.r; n     else n       return tp < e.tp; n   } n } e[MaxM]; n int getId(int v) { return lower_bound(ax, ax + K, v) - ax + 1; } n void add(int k, pair<int, int> v) { n   while (k <= K) { n     tr[k].insert(v); n     k += k & -k; n   } n } n int read(int k, int v) { n   pair<int, int> ret = make_pair(oo, -1); n   while (k) { n     set<pair<int, int> >::iterator it = tr[k].lower_bound(make_pair(v, 0)); n     if (it != tr[k].end()) ret = min(ret, *it); n     k -= k & -k; n   } n   return ret.second; n } n int main() { n   int N, M, en; n   int l, r, t; n   scanf( %d%d , &N, &M); n   K = en = 0; n   for (int i = 0; i < N; i++) { n     scanf( %d%d%d , &l, &r, &t); n     e[en++] = event(l, r, t, -i - 1); n     ax[K++] = l; n   } n   for (int i = 0; i < M; i++) { n     scanf( %d%d%d , &l, &r, &t); n     e[en++] = event(l, r, t, i); n     ax[K++] = l; n   } n   sort(ax, ax + K); n   K = unique(ax, ax + K) - ax; n   sort(e, e + en); n   for (int i = 0; i < en; i++) { n     if (e[i].tp < 0) { n       add(getId(e[i].l), make_pair(e[i].v, -e[i].tp)); n     } else { n       ret[e[i].tp] = read(getId(e[i].l), e[i].v); n     } n   } n   for (int i = 0; i < M; i++) { n     printf( %d  , ret[i]); n   } n   puts(  ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T, typename U> n std::istream& operator>>(std::istream& i, pair<T, U>& p) { n   i >> p.first >> p.second; n   return i; n } n template <typename T> n std::istream& operator>>(std::istream& i, vector<T>& t) { n   for (auto& v : t) { n     i >> v; n   } n   return i; n } n template <typename T, typename U> n std::ostream& operator<<(std::ostream& o, const pair<T, U>& p) { n   o << p.first <<     << p.second; n   return o; n } n template <typename T> n std::ostream& operator<<(std::ostream& o, const vector<T>& t) { n   if (t.empty()) o <<   n ; n   for (size_t i = 0; i < t.size(); ++i) { n     o << t[i] <<    n [i == t.size() - 1]; n   } n   return o; n } n template <typename T> n using minheap = priority_queue<T, vector<T>, greater<T>>; n template <typename T> n using maxheap = priority_queue<T, vector<T>, less<T>>; n unsigned int logceil(long long first) { n   return first ? 8 * sizeof(long long) - __builtin_clzll(first) : 0; n } n namespace std { n template <typename T, typename U> n struct hash<pair<T, U>> { n   hash<T> t; n   hash<U> u; n   size_t operator()(const pair<T, U>& p) const { n     return t(p.first) ^ (u(p.second) << 7); n   } n }; n }  // namespace std n template <typename T, typename F> n T bsh(T l, T h, const F& f) { n   T r = -1, m; n   while (l <= h) { n     m = (l + h) / 2; n     if (f(m)) { n       l = m + 1; n       r = m; n     } else { n       h = m - 1; n     } n   } n   return r; n } n template <typename F> n double bshd(double l, double h, const F& f, double p = 1e-9) { n   unsigned int r = 3 + (unsigned int)log2((h - l) / p); n   while (r--) { n     double m = (l + h) / 2; n     if (f(m)) { n       l = m; n     } else { n       h = m; n     } n   } n   return (l + h) / 2; n } n template <typename T, typename F> n T bsl(T l, T h, const F& f) { n   T r = -1, m; n   while (l <= h) { n     m = (l + h) / 2; n     if (f(m)) { n       h = m - 1; n       r = m; n     } else { n       l = m + 1; n     } n   } n   return r; n } n template <typename F> n double bsld(double l, double h, const F& f) { n   unsigned int r = 200; n   while (r--) { n     double m = (l + h) / 2; n     if (f(m)) { n       h = m; n     } else { n       l = m; n     } n   } n   return (l + h) / 2; n } n template <typename T> n T gcd(T a, T b) { n   if (a < b) swap(a, b); n   return b ? gcd(b, a % b) : a; n } n template <typename T> n class vector2 : public vector<vector<T>> { n  public: n   vector2() {} n   vector2(size_t a, size_t b, T t = T()) n       : vector<vector<T>>(a, vector<T>(b, t)) {} n }; n template <typename T> n class vector3 : public vector<vector2<T>> { n  public: n   vector3() {} n   vector3(size_t a, size_t b, size_t c, T t = T()) n       : vector<vector2<T>>(a, vector2<T>(b, c, t)) {} n }; n template <typename T> n class vector4 : public vector<vector3<T>> { n  public: n   vector4() {} n   vector4(size_t a, size_t b, size_t c, size_t d, T t = T()) n       : vector<vector3<T>>(a, vector3<T>(b, c, d, t)) {} n }; n template <typename T> n class vector5 : public vector<vector4<T>> { n  public: n   vector5() {} n   vector5(size_t a, size_t b, size_t c, size_t d, size_t e, T t = T()) n       : vector<vector4<T>>(a, vector4<T>(b, c, d, e, t)) {} n }; n class GOmkarAndPies { n  public: n   vector<std::pair<int, int>> D; n   int N, M, K; n   void fix(int p) { n     for (int i = 0; i < K; ++i) { n       int s = p ^ 1 << i; n       if (D[s].first > D[p].first + 1) { n         D[s] = {D[p].first + 1, D[p].second}; n         fix(s); n       } n     } n   }; n   void solve(istream& cin, ostream& cout) { n     cin >> N >> M >> K; n     string S, T; n     cin >> S >> T; n     vector<std::pair<int, int>> P(N); n     cin >> P; n     for (std::pair<int, int>& p : P) { n       p.first--; n       p.second--; n     } n     vector<int> X(K); n     iota(X.begin(), X.end(), 0); n     vector<vector<int>> W{X}; n     for (int i = 0; i < N; ++i) { n       swap(X[P[i].first], X[P[i].second]); n       vector<int> I(K); n       for (int j = 0; j < K; ++j) I[X[j]] = j; n       W.push_back(X); n     } n     auto apply = [&](int first, string S) { n       int p = 0; n       for (int i = 0; i < K; ++i) { n         p |= (S[i] -  0 ) << W[first][i]; n       } n       return p; n     }; n     D.assign(1 << K, {K + 1, -1}); n     pair<int, std::pair<int, int>> ans{K + 1, {-1, -1}}; n     for (int i = N - M; i >= 0; --i) { n       int TX = apply(i + M, T); n       D[TX] = {0, i + M}; n       fix(TX); n       int SX = apply(i, S); n       ans = min(ans, {D[SX].first, {i + 1, D[SX].second}}); n     } n     cout << K - ans.first <<   n  << ans.second <<   n ; n   } n }; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(nullptr); n   cout.tie(nullptr); n   GOmkarAndPies solver; n   std::istream& in(std::cin); n   std::ostream& out(std::cout); n   solver.solve(in, out); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   string s; n   cin >> s; n   cout << (regex_match(s, regex( (1|14|144)* )) ?  YES  :  NO ); n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 3e3 + 5; n int dw[N], up[N], E[N][2], Mn; n vector<pair<int, int> > g[N]; n void dfs_down(int v, int p) { n   dw[v] = 0; n   for (pair<int, int> u : g[v]) n     if (u.first != p) dfs_down(u.first, v), dw[v] += dw[u.first] + u.second; n } n void dfs_up(int v, int p, int f = -1) { n   if (f != -1) { n     up[v] = up[p] + dw[p] - dw[v] + f - (f == 0); n   } else n     up[v] = 0; n   Mn = min(Mn, dw[v] + up[v]); n   for (pair<int, int> u : g[v]) n     if (u.first != p) dfs_up(u.first, v, u.second ^ 1); n } n int main() { n   ios::sync_with_stdio(0), cin.tie(0), cout.tie(0); n   int n; n   cin >> n; n   if (n == 1) return cout <<  0 n , 0; n   for (int i = 0; i < n - 1; i++) { n     cin >> E[i][0] >> E[i][1]; n     g[E[i][0]].push_back({E[i][1], 0}); n     g[E[i][1]].push_back({E[i][0], 1}); n   } n   int ans = n; n   for (int i = 0; i < n - 1; i++) { n     Mn = n, dfs_down(E[i][0], E[i][1]), dfs_up(E[i][0], E[i][1]); n     int cur = Mn; n     Mn = n, dfs_down(E[i][1], E[i][0]), dfs_up(E[i][1], E[i][0]); n     cur += Mn; n     ans = min(ans, cur); n   } n   cout << ans <<   n ; n } n


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


#include <bits/stdc++.h> n using namespace std; n const int N = 2e5 + 10; n int y[N], pos[N]; n int main() { n   int n, m, t = 0, ans = 0, cnt = 0; n   scanf( %d %d , &n, &m); n   for (int i = 1; i <= n; i++) scanf( %d , &y[i]); n   for (int i = 1; i <= m; i++) { n     int y1, y2, x; n     scanf( %d %d %d , &y1, &y2, &x); n     if (y1 == 1) { n       pos[++t] = y2; n     } n     if (y2 == 1e9) { n       ans++; n     } n   } n   sort(y + 1, y + 1 + n); n   sort(pos + 1, pos + 1 + t); n   int i = 1, j = 1; n   cnt = t, ans += n; n   while (i <= n) { n     while (j <= t) { n       if (pos[j] < y[i]) { n         j++, cnt--; n       } else n         break; n     } n     ans = min(ans, cnt + i - 1); n     i++; n   } n   cout << ans; n } n


#include <bits/stdc++.h> n using namespace std; n inline int read() { n   int x = 0, f = 1; n   char c = getchar(); n   while (c <  0  || c >  9 ) { n     if (c ==  - ) f = -1; n     c = getchar(); n   } n   while (c >=  0  && c <=  9 ) n     x = (x << 3) + (x << 1) + (c ^  0 ), c = getchar(); n   return x * f; n } n const int N = 505, mod = 1e9 + 7; n inline int inc(int x, int y) { n   x += y - mod; n   return x + (x >> 31 & mod); n } n inline int mul(int x, int y) { return 1ll * x * y % mod; } n inline void Inc(int &x, int y) { x = inc(x, y); } n int n, a[N], f[N][N]; n int main() { n   n = read(); n   for (int i = 1; i <= n; i++) a[i] = read(), f[i][i] = 1; n   for (int i = 1; i <= n + 1; i++) f[i][i - 1] = 1; n   for (int len = 2; len < n; len++) n     for (int l = 1; l <= n - len + 1; l++) { n       int r = l + len - 1; n       f[l][r] = f[l + 1][r]; n       for (int k = l + 1; k <= r; k++) n         if (a[k] > a[l]) Inc(f[l][r], mul(f[l + 1][k - 1], f[k][r])); n     } n   printf( %d n , f[2][n]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 2001; n char a[MAXN][MAXN]; n int n, v[MAXN], r[MAXN], c[MAXN]; n int gao() { n   int ret = 0; n   memset(r, 0, sizeof(int) * n); n   memset(c, 0, sizeof(int) * n); n   for (int i = 0; i < n - 1; ++i) { n     for (int j = n - 1; j > i; --j) { n       if (a[i][j] ^ r[i] ^ c[j]) { n         ++ret; n         r[i] ^= 1; n         c[j] ^= 1; n       } n     } n   } n   for (int i = 0; i < n; ++i) v[i] ^= (r[i] ^ c[i]); n   memset(r, 0, sizeof(int) * n); n   memset(c, 0, sizeof(int) * n); n   for (int i = n - 1; i > 0; --i) { n     for (int j = 0; j < i; ++j) { n       if (a[i][j] ^ r[i] ^ c[j]) { n         ++ret; n         r[i] ^= 1; n         c[j] ^= 1; n       } n     } n   } n   for (int i = 0; i < n; ++i) v[i] ^= (r[i] ^ c[i]); n   for (int i = 0; i < n; ++i) ret += v[i]; n   return ret; n } n int main() { n   while (scanf( %d , &n) != EOF) { n     memset(v, 0, sizeof(int) * n); n     for (int i = 0; i < n; ++i) { n       scanf( %s , a[i]); n       for (int j = 0; j < n; ++j) a[i][j] -=  0 ; n       v[i] ^= (int)a[i][i]; n     } n     printf( %d n , gao()); n   } n   return 0; n } n


#include<bits/stdc++.h> n using namespace std; n typedef double db; n typedef uint32_t u32; n typedef uint64_t u64; n typedef pair<int, int> pii; n typedef pair<int, int> point; n #define fi first n #define se second n #define pb(x) push_back(x) n #define SZ(x) ((int) (x).size()) n #define all(x) (x).begin(), (x).end() n #define rep(i, l, r) for(int i = l; i < r; i++) n typedef long long ll; n const int mod = 998244353; n  n  n  n int main() { n #ifdef local n  freopen( in.txt ,  r , stdin); n #endif n  ios::sync_with_stdio(false); n  cin.tie(0), cout.tie(0); n  const int mod = 1e9 + 7; n  int n; n  cin >> n; n  vector<int> a(n); n  ll tot = 0; n  for(auto &e : a) { n   cin >> e; n   tot += e; n  } n  if(tot % n) { n   cout <<  0 n ; n   return 0; n  } n  vector<int> fac(n + 1, 1); n  vector<int> ifac(n + 1, 1); n  for(int i = 2; i <= n; i++) { n   fac[i] = (ll) fac[i - 1] * i % mod; n   ifac[i] = (ll) ifac[mod % i] * (mod - mod / i) % mod; n  } n  for(int i = 2; i <= n; i++) { n   ifac[i] = (ll) ifac[i] * ifac[i - 1] % mod; n  } n  auto C = [&](int n, int m) { n   if(n < m || m < 0) return 0ll; n   return (ll) fac[n] * ifac[m] % mod * ifac[n - m] % mod; n  }; n  ll avg = tot / n; n  vector<int> l, r; n  int eq = 0; n  for(auto &e : a) { n   if(e > avg) { n    r.push_back(e); n   } else if(e < avg) { n    l.push_back(e); n   } else { n    eq++; n   } n  } n  int ans = 1; n  if(l.size() > 1 && r.size() > 1) { n   ans = 2; n  } n  if(l.size() == 1) { n   r.push_back(l[0]); n   l.clear(); n  } n  if(r.size() == 1) { n   l.push_back(r[0]); n   r.clear(); n  } n  sort(all(l)); n  sort(all(r)); n  int a1 = fac[l.size()]; n  int a2 = fac[r.size()]; n  for(int i = 0, j = 0; i < (int) l.size(); i = j) { n   while(j < (int) l.size() && l[j] == l[i]) { n    j++; n   } n   a1 = (ll) a1 * ifac[j - i] % mod; n  } n  for(int i = 0, j = 0; i < (int) r.size(); i = j) { n   while(j < (int) r.size() && r[j] == r[i]) { n    j++; n   } n   a2 = (ll) a2 * ifac[j - i] % mod; n  } n  int a3 = C(n, eq); n  ans = (ll) ans * a1 % mod; n  ans = (ll) ans * a2 % mod; n  ans = (ll) ans * a3 % mod; n  cout << ans <<   n ; n  return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main(void) { n   int n; n   cin >> n; n   set<int> a, b; n   vector<pair<int, int> > ans(n); n   for (int i = 0; i < n; ++i) { n     cin >> ans[i].first >> ans[i].second; n     --ans[i].first; n     --ans[i].second; n     if (ans[i].first == -1) a.insert(i); n     if (ans[i].second == -1) b.insert(i); n   } n   while (a.size() > 1) { n     int node = *a.begin(); n     a.erase(a.begin()); n     int last = node; n     while (ans[last].second != -1) last = ans[last].second; n     auto nxt = b.begin(); n     if (*nxt == last) ++nxt; n     ans[*nxt].second = node; n     ans[node].first = *nxt; n     b.erase(nxt); n   } n   for (int i = 0; i < n; ++i) n     cout << ans[i].first + 1 <<     << ans[i].second + 1 <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   cin >> n; n   int a[n], sm = 0; n   for (int i = 0; i < n; i++) { n     cin >> a[i]; n     sm += a[i]; n   } n   vector<int> ind, r; n   int need = sm / 2; n   if (a[0] > need) { n     cout << 1 << endl; n     cout << 1; n     return 0; n   } n   for (int i = 1; i < n; i++) { n     if ((2 * a[i]) <= a[0]) { n       ind.push_back(i + 1); n       r.push_back(a[i]); n     } n   } n   bool ok; n   int u = a[0]; n   for (auto x : r) { n     u += x; n   } n   if (u > need) { n     ok = true; n   } else { n     ok = false; n   } n   if (ok) { n     cout << ind.size() + 1 << endl; n     cout << 1 <<    ; n     for (auto x : ind) { n       cout << x <<    ; n     } n   } else { n     cout << 0; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int nbVoyages, coutSimple, coutLiaison, maxCartes, coutCarte; n map<pair<string, string>, int> routes; n int main() { n   ios_base::sync_with_stdio(false); n   cin >> nbVoyages >> coutSimple >> coutLiaison >> maxCartes >> coutCarte; n   long long total = 0; n   string derFin =   ; n   for (int iVoyage = 0; iVoyage < nbVoyages; iVoyage++) { n     string a, b; n     cin >> a >> b; n     string der = b, pre = a; n     if (b < a) swap(a, b); n     if (routes.find({a, b}) == routes.end()) routes[{a, b}] = 0; n     if (derFin == pre) { n       routes[{a, b}] += coutLiaison; n       total += coutLiaison; n     } else { n       routes[{a, b}] += coutSimple; n       total += coutSimple; n     } n     derFin = der; n   } n   vector<int> couts; n   for (pair<pair<string, string>, int> route : routes) { n     couts.push_back(route.second); n   } n   sort(couts.begin(), couts.end()); n   for (int iFois = 0; iFois < maxCartes; iFois++) { n     if (!couts.empty() && couts.back() > coutCarte) { n       total += coutCarte - couts.back(); n       couts.pop_back(); n     } n   } n   cout << total << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int inf = 0x3f3f3f3f; n const double eps = 1e-6; n const int mod = 1000000007; n int modpow(int x, int y, int md) { n   if (y == 0) return 1; n   int ret = modpow(x, y >> 1, md); n   ret = (long long)ret * ret % md; n   if (y & 1) ret = (long long)ret * x % md; n   return ret; n } n int n; n int sum[5555]; n vector<int> g[5555]; n set<pair<int, int> > ans; n void dfs(int x, int par = 0) { n   sum[x] = 1; n   for (auto to : g[x]) n     if (to != par) dfs(to, x); n   for (auto to : g[x]) n     if (to != par) sum[x] += sum[to]; n   vector<bool> ok(5555, false); n   ok[0] = true; n   for (auto to : g[x]) n     if (to != par) { n       for (int i = 5005; i >= 0; i--) n         if (ok[i] && i + sum[to] <= 5005) ok[i + sum[to]] = true; n     } n   for (int i = 1; i <= 5005; i++) n     if (ok[i] && n - i - 1 >= 1) n       ans.insert(make_pair(i, n - i - 1)), ans.insert(make_pair(n - i - 1, i)); n } n int main() { n   cin >> n; n   for (int i = 1; i < n; i++) { n     int a, b; n     cin >> a >> b; n     g[a].push_back(b); n     g[b].push_back(a); n   } n   dfs(1); n   cout << ans.size() << endl; n   for (auto p : ans) cout << p.first <<     << p.second << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n string s[3]; n int N, M, p[3][1000005], a[3][1000005], b[1000005]; n void genPrefix(int pat) { n   int i, k; n   p[pat][0] = 0; n   k = 0; n   i = 1; n   while (i < M) { n     if (s[pat][i] == s[pat][k]) { n       k++; n       p[pat][i] = k; n       i++; n     } else { n       if (k != 0) n         k = p[pat][k - 1]; n       else { n         p[pat][i] = 0; n         i++; n       } n     } n   } n } n void KMPSearch(int txt, int pat) { n   int i, k; n   genPrefix(pat); n   i = k = 0; n   while (i < N) { n     if (s[txt][i] == s[pat][k]) { n       i++; n       k++; n       a[txt][i - k] = i - 1; n     } n     if (k == M) { n       k = p[pat][k - 1]; n     } else if (i < N && s[txt][i] != s[pat][k]) { n       if (k != 0) { n         if (s[txt][i - 1] == s[pat][k]) n           a[txt][i - k] = max(i - 1, a[txt][k - 1]); n         k = p[pat][k - 1]; n         if (k > 0 && s[txt][i - 1] == s[pat][k]) n           a[txt][i - k] = max(i - 1, a[txt][k - 1]); n       } else n         i++; n     } n   } n } n int bs(int low, int high, int v) { n   int mid, x = -1; n   while (low <= high) { n     mid = (low + high) >> 1; n     if (b[mid] >= v) { n       x = mid; n       high = mid - 1; n     } else n       low = mid + 1; n   } n   return x; n } n pair<int, int> solve() { n   s[2] = s[0]; n   reverse(s[2].begin(), s[2].end()); n   memset(a, -1, sizeof(a)); n   KMPSearch(1, 2); n   KMPSearch(0, 1); n   b[0] = a[1][0]; n   int i, j = N, len, k, x; n   int ansx = -1, ansy = -1; n   for (i = 1; i <= N - 1; i++) b[i] = max(b[i - 1], a[1][i]); n   for (i = 0; i <= N - 2; i++) { n     j--; n     if (s[0][i] != s[1][j]) break; n     if (b[0] >= j - 1) { n       ansx = i; n       ansy = i + 1; n     } n     if (a[0][i + 1] <= 0 || i + 1 == N - 1) continue; n     len = min(N - 2, a[0][i + 1]) - i; n     k = bs(0, len, j - 1); n     if (k >= 0 && ansx < i) { n       ansx = i; n       ansy = i + 1 + k; n     } n   } n   return make_pair(ansx, ansy); n } n int main() { n   std::ios_base::sync_with_stdio(0); n   int cases, caseno = 0, n, i, j, k, cnt, m; n   getline(cin, s[0]); n   getline(cin, s[1]); n   N = s[0].size(); n   M = s[1].size(); n   if (N != M) { n     cout <<  -1 -1  <<   n ; n     return 0; n   } n   pair<int, int> ans = solve(); n   cout << ans.first <<     << ans.second <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, x; n   scanf( %d%d , &n, &x); n   if (n == 1) { n     printf( YES n ); n     printf( %d n , x); n   } else if (n == 2) { n     if (x == 0) n       printf( NO n ); n     else { n       printf( YES n ); n       printf( 0 %d n , x); n     } n   } else { n     printf( YES n ); n     int tmp = 0; n     int a = (1 << 17); n     int b = (1 << 18); n     for (int i = 0; i < n - 3; i++) { n       printf( %d  , i); n       tmp ^= i; n     } n     printf( %d %d %d n , a, b, a ^ b ^ tmp ^ x); n   } n   return 0; n } n