#include <bits/stdc++.h> n using namespace std; n vector<int> bomb[5005]; n int n, m, st_time[5005], sol[5005], dist_min[5005]; n int main() { n   cin >> n >> m; n   for (int i = 1; i <= n; ++i) dist_min[i] = 2 * 5005; n   int pos, val; n   for (int i = 1; i <= m; ++i) { n     cin >> pos >> val; n     bomb[pos].push_back(val); n     int comp = 0; n     if (val >= pos) n       comp = val - pos; n     else n       comp = n - (pos - val); n     if (val != pos) dist_min[pos] = min(dist_min[pos], comp); n   } n   for (int i = 1; i <= n; ++i) { n     int blen = bomb[i].size(); n     if (blen) n       st_time[i] = n * (blen - 1) + dist_min[i]; n     else n       st_time[i] = 0; n   } n   for (int i = 1; i <= n; ++i) { n     for (int j = 1; j <= n; ++j) n       if (!bomb[j].empty()) { n         if (j < i) n           sol[i] = max(sol[i], n - (i - j) + st_time[j]); n         else n           sol[i] = max(sol[i], j - i + st_time[j]); n       } n     cout << sol[i] <<    ; n   } n   return 0; n } n


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


#include <bits/stdc++.h> n using namespace std; n long long n, x, y, num[100005], res, a[100005], b[100005]; n bool vis[100005]; n vector<int> v[100005]; n void dfs(long long now, long long pre) { n   for (long long i = 0; i < v[now].size(); i++) { n     int j = v[now][i]; n     if (j != pre) { n       dfs(j, now); n       a[now] = max(a[now], a[j]); n       b[now] = max(b[now], b[j]); n     } n   } n   num[now] += a[now]; n   num[now] -= b[now]; n   if (num[now] < 0) n     a[now] += -num[now]; n   else n     b[now] += num[now]; n } n int main() { n   cin >> n; n   for (long long i = 0; i < n - 1; i++) { n     cin >> x >> y; n     v[x].push_back(y); n     v[y].push_back(x); n   } n   for (long long i = 1; i <= n; i++) cin >> num[i]; n   dfs(1, -1); n   res = a[1] + b[1]; n   cout << res <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n #pragma GCC target( sse4,avx ) n const int p = 1000000007; n int mul32(int x, int y) { return int(int64_t(x) * y % p); } n int add32(int x, int y) { n   int res = x + y; n   if (res >= p) res -= p; n   return res; n } n int sub32(int x, int y) { n   int res = x - y; n   if (res < 0) res += p; n   return res; n } n void run(std::istream &in, std::ostream &out) { n   int n; n   in >> n; n   std::vector<int> xs(n); n   for (int i = 0; i < n; i++) { n     in >> xs[i]; n   } n   std::sort(xs.begin(), xs.end()); n   std::vector<int> pow2(n + 1); n   pow2[0] = 1; n   for (int i = 1; i <= n; i++) { n     pow2[i] = (pow2[i - 1] * 2) % p; n   } n   int res = 0; n   for (int i = 0; i < n; i++) { n     res = add32(res, mul32(xs[i], pow2[i])); n     res = sub32(res, mul32(xs[i], pow2[n - i - 1])); n   } n   out << res << std::endl; n } n int main() { n   std::cin.sync_with_stdio(false); n   std::cin.tie(nullptr); n   run(std::cin, std::cout); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long N = 1e5 + 10; n long long n, d, b, a[N], f[N]; n int main() { n   long long i, j, k, t = 0, ans; n   scanf( %I64d%I64d%I64d , &n, &d, &b); n   for (i = 1; i <= n; i++) scanf( %I64d , &a[i]), f[i] = f[i - 1] + a[i]; n   for (i = 1; i <= n / 2; i++) n     if (f[min(i * d + i, n)] >= (t + 1) * b) t++; n   ans = t; n   t = 0; n   for (i = 1; i <= n / 2; i++) swap(a[i], a[n - i + 1]); n   f[0] = 0; n   for (i = 1; i <= n; i++) f[i] = f[i - 1] + a[i]; n   for (i = 1; i <= n / 2; i++) n     if (f[min(i * d + i, n)] >= (t + 1) * b) t++; n   printf( %I64d , n / 2 - min(ans, t)); n } n


#include <bits/stdc++.h> n using namespace std; n int Get() { n   char c; n   while (c = getchar(), c <  0  || c >  9 ) n     ; n   int X = 0; n   while (c >=  0  && c <=  9 ) { n     X = X * 10 + c - 48; n     c = getchar(); n   } n   return X; n } n const int inf = 1000000000; n int GCD(int a, int b) { return b ? GCD(b, a % b) : a; } n int Solve(int a, int b, int c) { n   return a ? ((c - b * Solve(b % a, a, c % a)) / a) : 0; n } n int GetMin(int L, int R, int a, int b, int c) { n   if (L > R) return -1; n   int K = GCD(a, b); n   if (c % K) return -1; n   a /= K; n   b /= K; n   c /= K; n   int X = Solve(a, b, c); n   if (X < L) X += ((L - X - 1) / b + 1) * b; n   if (X > L) X -= (X - L) / b * b; n   return (X <= R) ? X : -1; n } n int GetMax(int L, int R, int a, int b, int c) { n   if (L > R) return -1; n   int K = GCD(a, b); n   if (c % K) return -1; n   a /= K; n   b /= K; n   c /= K; n   int X = Solve(a, b, c); n   if (X > R) X -= ((X - R - 1) / b + 1) * b; n   if (X < R) X += (R - X) / b * b; n   return (X >= L) ? X : -1; n } n int main() { n   int N = Get(), S = Get(), a = 0, b = 0, c = 0; n   while (N--) { n     int X = Get(); n     if (X == 3) a++; n     if (X == 4) b++; n     if (X == 5) c++; n   } n   int Ans = inf, Ans_x, Ans_y; n   for (int y = 0; b * y <= S; y++) { n     int B = b * y, _S = S - B, Max = min(y, (_S - c * y) / a), x; n     x = GetMin((_S - B - 1) / a + 1, min(Max, B / a), a, c, _S); n     if (x != -1 && 2 * B - _S < Ans) { n       Ans = 2 * B - _S; n       Ans_x = x; n       Ans_y = y; n     } n     x = GetMax(0, min(Max, min(B, _S - B) / a), a, c, _S); n     if (x != -1 && _S - 2 * a * x < Ans) { n       Ans = _S - 2 * a * x; n       Ans_x = x; n       Ans_y = y; n     } n     x = GetMin((max(B, _S - B) - 1) / a + 1, Max, a, c, _S); n     if (x != -1 && 2 * a * x - _S < Ans) { n       Ans = 2 * a * x - _S; n       Ans_x = x; n       Ans_y = y; n     } n     x = GetMin((B - 1) / a + 1, min(Max, (_S - B) / a), a, c, _S); n     if (x != -1 && _S - 2 * B < Ans) { n       Ans = _S - 2 * B; n       Ans_x = x; n       Ans_y = y; n     } n   } n   if (Ans == inf) n     printf( -1 n ); n   else n     printf( %d %d %d n , Ans_x, Ans_y, (S - a * Ans_x - b * Ans_y) / c); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long grundy[1234]; n int main() { n   long long idx = 1, i, j; n   for (i = 1; i <= 15; i++) { n     for (j = 1; j <= (i + 1); j++) { n       grundy[idx] = i; n       idx++; n     } n   } n   long long n; n   cin >> n; n   long long ans = 0; n   while (n--) { n     long long va; n     cin >> va; n     ans = ans ^ grundy[va]; n   } n   cout << ((ans == 0) ?  YES  :  NO ); n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int a, b, c, d, e; n   scanf( %d%d%d%d%d , &a, &b, &c, &d, &e); n   if ((a + b + c + d + e) % 5 == 0 && a + b + c + d + e >= 5) n     printf( %d n , (a + b + c + d + e) / 5); n   else n     printf( -1 n ); n } n


#include <bits/stdc++.h> n using namespace std; n long long n, a[100005], b[100005], fa[100005], s[100005], q[100005], l, n     p = -2147483647; n inline long long find(long long x) { n   if (x == fa[x]) return x; n   return fa[x] = find(fa[x]); n } n inline long long M(long long u, long long v) { return u > v ? u : v; } n int main() { n   scanf( %lld , &n); n   for (long long i = 1; i <= n; i++) scanf( %lld , &a[i]); n   for (long long i = 1; i <= n; i++) scanf( %lld , &b[i]); n   for (long long i = n; i >= 1; i--) { n     long long k = b[i]; n     if (!fa[k]) fa[k] = k, s[k] = a[k]; n     long long fk = find(k); n     if (fa[k - 1] && k - 1 > 0) { n       long long fl = find(k - 1); n       if (fk != fl) s[k] += s[fl], s[fl] = 0, fa[fl] = k; n     } n     if (fa[k + 1] && k + 1 <= n) { n       long long fr = find(k + 1); n       if (fk != fr) s[k] += s[fr], s[fr] = 0, fa[fr] = k; n     } n     p = M(p, s[k]), q[++l] = p; n   } n   for (long long i = n - 1; i >= 0; i--) printf( %lld n , q[i]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int h, w, i, j, mod = 1e9 + 7, r, c, ans = 1, a[1001][1001]; n int main() { n   cin.tie(NULL); n   ios_base::sync_with_stdio(0); n   cin >> h >> w; n   for (i = 0; i < h; i++) { n     cin >> r; n     for (j = 0; j < r; j++) a[i][j] = 2; n     a[i][r] = 1; n   } n   for (i = 0; i < w; i++) { n     cin >> c; n     for (j = 0; j < c; j++) n       if (a[j][i] == 1) { n         cout << 0 << endl; n         return 0; n       } else n         a[j][i] = 2; n     if (a[c][i] == 2) { n       cout << 0 << endl; n       return 0; n     } n     a[c][i] = 1; n   } n   for (i = 0; i < h; i++) n     for (j = 0; j < w; j++) n       if (!a[i][j]) ans = (ans * 2) % mod; n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<int> v[200010], circle; n int fa[200010], dep[200010], a[200010], b[200010], n, rt, vs, vt, h1, h2; n bool vis[200010], bo[200010]; n inline int rd() { n   int x = 0; n   char ch = getchar(); n   for (; ch <  0  || ch >  9 ; ch = getchar()) n     ; n   for (; ch >=  0  && ch <=  9 ; ch = getchar()) x = x * 10 + ch -  0 ; n   return x; n } n inline void dfs(int x) { n   for (int i = 0, siz = v[x].size(); i < siz; i++) { n     int t = v[x][i]; n     if (t == fa[x]) continue; n     dep[t] = dep[x] + 1; n     fa[t] = x; n     dfs(t); n   } n } n inline bool gao() { n   int p = 0, q = 0; n   bool flag = true; n   for (int i = 1; i <= n; i++) n     if (a[i] != b[i] && dep[i] > dep[p]) p = i; n   if (!p) return true; n   for (int hh = p; a[hh] != b[hh]; hh = fa[hh]) n     bo[hh] = true, circle.push_back(hh); n   for (int i = 1; i <= n; i++) n     if (a[i] != b[i] && !bo[i] && dep[i] > dep[q]) q = i; n   if (q) { n     reverse(circle.begin(), circle.end()); n     flag = false; n     for (int hh = q; a[hh] != b[hh]; hh = fa[hh]) n       bo[hh] = true, circle.push_back(hh); n   } else n     q = fa[circle.back()]; n   for (int i = 1; i <= n; i++) n     if (a[i] != b[i] && !bo[i]) return false; n   int h1 = p, h2 = q; n   for (; h1 != h2; h1 = fa[h1]) n     if (dep[h1] < dep[h2]) swap(h1, h2); n   rt = h1; n   if (circle.size() != dep[p] + dep[q] - dep[rt] * 2) return false; n   vs = p; n   vt = q; n   if (flag) reverse(circle.begin(), circle.end()); n   return true; n } n int main() { n   n = rd(); n   for (int i = 1; i <= n; i++) n     if (!(a[i] = rd())) h1 = i; n   for (int i = 1; i <= n; i++) n     if (!(b[i] = rd())) h2 = i; n   for (int i = 1; i < n; i++) { n     int x = rd(), y = rd(); n     v[x].push_back(y); n     v[y].push_back(x); n   } n   dep[0] = -1; n   dfs(h2); n   for (int hh = h1; hh != h2; hh = fa[hh]) swap(a[hh], a[fa[hh]]); n   long long ans = dep[h1]; n   if (!gao()) { n     puts( -1 ); n     return 0; n   } n   if (!circle.size()) { n     printf( 0 %d n , ans); n     return 0; n   } n   for (int i = h1; i; i = fa[i]) vis[i] = true; n   if (vis[circle.back()] && !vis[circle[0]]) n     reverse(circle.begin(), circle.end()); n   int gap = 0, siz = circle.size(); n   for (int i = 0; i < siz; i++) n     if (b[circle[i]] == a[circle[0]]) gap = i; n   for (int i = 0; i < siz; i++) n     if (b[circle[(i + gap) % siz]] != a[circle[i]]) { n       puts( -1 ); n       return 0; n     } n   if (vis[circle[0]] || vis[circle.back()]) { n     int now = siz; n     for (int i = 0; i < siz; i++) n       if (!vis[circle[i]]) { n         now = i; n         break; n       } n     ans = ans - now + n           min((long long)gap * (siz + 1) + now, n               abs((long long)(siz - gap) * (siz + 1) - now)); n   } else n     ans = (ans + (long long)min(gap, siz - gap) * (siz + 1)); n   for (; !vis[rt]; rt = fa[rt]) ans += 2; n   if (vs > vt) swap(vs, vt); n   printf( %d %d %I64d n , vs, vt, ans); n   return 0; n } n


#include <bits/stdc++.h> n #pragma GCC optimize( Ofast ) n using namespace std; n using i64 = long long; n using i32 = int; n using f64 = long double; n template <class Key> n using vec = vector<Key>; n template <class Key> n using heap = priority_queue<Key>; n template <class Key> n using hash_set = unordered_set<Key>; n template <class Key, class Value> n using hash_map = unordered_map<Key, Value>; n using str = string; n template <size_t Size1> n using a64 = array<i64, Size1>; n template <size_t Size1, size_t Size2> n using aa64 = array<a64<Size2>, Size1>; n template <size_t Size1, size_t Size2, size_t Size3> n using aaa64 = array<aa64<Size2, Size3>, Size1>; n using p64 = pair<i64, i64>; n using v64 = vec<i64>; n using vv64 = vec<v64>; n using s64 = set<i64>; n using m64 = map<i64, i64>; n mt19937 gen(998244353); n const i64 inf = numeric_limits<i64>::max() / 2; n template <typename T> n inline i64 sz(const T& a) { n   return (i64)(a.size()); n } n inline void fast() { ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); } n template <typename T> n inline bool check_min(T& a, T b) { n   if (a > b) return a = b, 1; n   return 0; n } n template <typename T> n inline bool check_max(T& a, T b) { n   if (a < b) return a = b, 1; n   return 0; n } n struct Problem { n   Problem(bool multitest = 0) { n     read(); n     if (!multitest) solve(); n   } n   void solve(), read(); n }; n signed main() { n   fast(); n   Problem(); n   return 0; n } n i64 n, m; n v64 a, b; n vv64 c; n i64 dist(i64 from, i64 to) { return (to - from + n) % n; } n void Problem::solve() { n   c.resize(n); n   for (i64 i = 0; i < m; i++) { n     c[a[i]].push_back(dist(a[i], b[i])); n   } n   for (i64 i = 0; i < sz(c); i++) { n     sort((c[i]).begin(), (c[i]).end()); n   } n   for (i64 i = 0; i < n; i++) { n     i64 ans = 0; n     for (i64 j = 0; j < n; j++) { n       if (c[j].empty()) continue; n       i64 cur = dist(i, j) + (sz(c[j]) - 1) * n + c[j][0]; n       check_max(ans, cur); n     } n     cout << ans <<    ; n   } n } n void Problem::read() { n   cin >> n >> m; n   a.resize(m), b.resize(m); n   for (i64 i = 0; i < m; i++) { n     cin >> a[i] >> b[i]; n     --a[i], --b[i]; n   } n } n


#include <bits/stdc++.h> n using std::max; n int n, m, c, x[150000 + 1], a, b, i; n long long p[150000 + 1], ans = 0; n struct node { n   long long sum, mmax, lmax, rmax; n   void update(const node &l, const node &r) { n     sum = l.sum + r.sum; n     lmax = max(l.lmax, l.sum + r.lmax); n     rmax = max(l.rmax + r.sum, r.rmax); n     mmax = max(l.mmax, max(r.mmax, l.rmax + r.lmax)); n   } n } tree[(150000 << 2) + 1]; n void build(int x, int l, int r) { n   if (l == r) { n     tree[x].sum = tree[x].mmax = tree[x].lmax = tree[x].rmax = p[l]; n     return; n   } n   int mid = (l + r) >> 1; n   build(x << 1, l, mid); n   build((x << 1) + 1, mid + 1, r); n   tree[x].update(tree[x << 1], tree[(x << 1) + 1]); n } n node find(int x, int l, int r, int s, int t) { n   if (s == l && t == r) return tree[x]; n   int mid = (l + r) >> 1; n   if (t <= mid) return find(x << 1, l, mid, s, t); n   if (s > mid) return find((x << 1) + 1, mid + 1, r, s, t); n   node ret; n   ret.update(find(x << 1, l, mid, s, mid), n              find((x << 1) + 1, mid + 1, r, mid + 1, t)); n   return ret; n } n int main() { n   scanf( %d%d%d , &n, &m, &c); n   for (i = 1; i <= n; ++i) scanf( %d , x + i); n   for (i = 1; i < n; ++i) scanf( %d , p + i); n   for (i = 1; i < n; ++i) p[i] = 50 * (x[i + 1] - x[i]) - p[i] * c; n   --n; n   build(1, 1, n); n   for (i = 1; i <= m; ++i) { n     scanf( %d%d , &a, &b); n     ans += max(0LL, find(1, 1, n, a, b - 1).mmax); n   } n   printf( %0.9llf n , (double)ans / 100); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <class T> n bool isPrime(T x) { n   if (x <= 1) return false; n   T i; n   for (i = 2; i * i <= x; i++) n     if (x % i == 0) return false; n   return true; n } n template <class T> n class Prime { n  public: n   vector<T> z; n   Prime() { n     z.resize(1e5 + 7); n     for (int i = (0); i < (((int)z.size())); i++) z[i] = 1; n     z[0] = 0; n     z[1] = 0; n     T j; n     for (int i = (2); i < (((int)z.size())); i++) { n       if (z[i]) { n         j = i + i; n         while (j < ((int)z.size())) { n           z[j] = 0; n           j += i; n         } n       } n     } n   } n }; n template <class T> n double dist(T x1, T y1, T x2, T y2) { n   return sqrt(1. * (x2 - x1) * (x2 - x1) + 1. * (y2 - y1) * (y2 - y1)); n } n template <class T> n class Point { n  public: n   T x, y; n   Point() {} n   Point(T a, T b) : x(a), y(b) {} n   bool operator==(const Point& tmp) const { return (x == tmp.x && y == tmp.y); } n   Point operator-(const Point& tmp) const { n     return Point<T>(x - tmp.x, y - tmp.y); n   } n }; n char toLowerCase(char x) { return (x + 32); } n char toUpperCase(char x) { return (x - 32); } n bool isUpperCase(char x) { return (65 <= x && x <= 90) ? 1 : 0; } n bool isLowerCase(char x) { return (97 <= x && x <= 122) ? 1 : 0; } n bool isAlpha(char x) { return (isUpperCase(x) || isLowerCase(x)) ? 1 : 0; } n bool isDigit(char x) { return ( 0  <= x && x <=  9 ) ? 1 : 0; } n template <class T> n T toDec(string s) { n   stringstream is(s); n   T res; n   is >> res; n   return res; n } n template <class T> n string toStr(T n) { n   string s; n   stringstream is; n   is << n; n   is >> s; n   return s; n } n template <class T> n void checkmin(T& a, T b) { n   if (a > b) a = b; n } n template <class T> n void checkmax(T& a, T b) { n   if (a < b) a = b; n } n vector<vector<int> > g, ans, ng; n vector<int> visited, parent; n bool bfs(int s, int t) { n   visited = vector<int>(s + 5, 0); n   parent = vector<int>(s + 5, -1); n   queue<int> q; n   q.push(s); n   visited[s] = 1; n   int u; n   while (!q.empty()) { n     u = q.front(); n     q.pop(); n     if (u == t) return true; n     for (int v = 0; v < s + 5; v++) { n       if (g[u][v] > 0 && !visited[v]) { n         q.push(v); n         parent[v] = u; n         visited[v] = 1; n       } n     } n   } n   return false; n } n int getMaxFlow(int n) { n   int max_flow = 0; n   int path_min = 1e9; n   int u, v; n   while (bfs(n, n + 1)) { n     for (v = n + 1; v != n; v = parent[v]) { n       u = parent[v]; n       path_min = min(path_min, g[u][v]); n     } n     for (v = n + 1; v != n; v = parent[v]) { n       u = parent[v]; n       g[u][v] -= path_min; n       g[v][u] += path_min; n     } n     max_flow += path_min; n   } n   return max_flow; n } n int main() { n   int n; n   cin >> n; n   g = vector<vector<int> >(n + 5, vector<int>(n + 5, 0)); n   vector<int> a(n + 5, 0); n   for (int i = 0; i < n; i++) { n     cin >> a[i]; n     if (a[i] % 2 == 0) n       g[n][i] = 2; n     else n       g[i][n + 1] = 2; n   } n   for (int i = 0; i < n; i++) { n     if (a[i] % 2 == 0) { n       for (int j = 0; j < n; j++) { n         if (isPrime<int>(a[i] + a[j])) { n           g[i][j] = 1; n         } n       } n     } n   } n   if (getMaxFlow(n) != n) n     cout <<  Impossible ; n   else { n     ng = vector<vector<int> >(n + 5); n     for (int i = 0; i < n; i++) { n       if (a[i] % 2) { n         for (int j = 0; j < n; j++) { n           if (g[i][j]) { n             ng[i].push_back(j); n             ng[j].push_back(i); n           } n         } n       } n     } n     visited = vector<int>(n + 5); n     int node, prev, tmp; n     for (int i = 0; i < n; i++) { n       if (visited[i]) continue; n       visited[i] = 1; n       prev = i; n       node = ng[i][0]; n       vector<int> res; n       res.push_back(i); n       while (node != i) { n         visited[node] = 1; n         res.push_back(node); n         tmp = node; n         node = (prev != ng[node][0] ? ng[node][0] : ng[node][1]); n         prev = tmp; n       } n       ans.push_back(res); n     } n     cout << ((int)ans.size()) <<   n ; n     for (int i = 0; i < ((int)ans.size()); i++) { n       cout << ((int)ans[i].size()) <<    ; n       for (int j = 0; j < ((int)ans[i].size()); j++) { n         if (j) cout <<    ; n         cout << ans[i][j] + 1; n       } n       cout <<   n ; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <class T> n T sq(T x) { n   return x * x; n } n const long long inf = 2147383647; n const double pi = 2 * acos(0.0); n const double eps = 1e-9; n const long long maxint = 2147483647; n const long long minint = -2147483648; n char grid[450][450]; n int row, col, mx; n int preRight[450][450], preDiag1[450][450], preDiag2[450][450], n     preDown[450][450]; n void precalRight() { n   int i, j; n   for (i = 0; i < row; i++) { n     for (j = col - 1; j >= 0; j--) { n       if (grid[i][j] ==  1 ) n         preRight[i][j] = 0; n       else { n         preRight[i][j] = 1; n         if (j < col - 1) preRight[i][j] += preRight[i][j + 1]; n       } n     } n   } n } n void precalDiag1() { n   int i, j; n   for (i = row - 1; i >= 0; i--) { n     for (j = col - 1; j >= 0; j--) { n       if (grid[i][j] ==  1 ) n         preDiag1[i][j] = 0; n       else { n         preDiag1[i][j] = 1; n         if (i < row - 1 && j < col - 1) n           preDiag1[i][j] += preDiag1[i + 1][j + 1]; n       } n     } n   } n } n void precalDiag2() { n   int i, j; n   for (i = 0; i < row; i++) { n     for (j = col - 1; j >= 0; j--) { n       if (grid[i][j] ==  1 ) n         preDiag2[i][j] = 0; n       else { n         preDiag2[i][j] = 1; n         if (i > 0 && j < col - 1) preDiag2[i][j] += preDiag2[i - 1][j + 1]; n       } n     } n   } n } n void precalDown() { n   int i, j; n   for (i = row; i >= 0; i--) { n     for (j = col - 1; j >= 0; j--) { n       if (grid[i][j] ==  1 ) n         preDown[i][j] = 0; n       else { n         preDown[i][j] = 1; n         if (i < row - 1) preDown[i][j] += preDown[i + 1][j]; n       } n     } n   } n } n void precal() { n   precalRight(); n   precalDiag1(); n   precalDiag2(); n   precalDown(); n } n long long triangle(int r, int c) { n   long long res = 0; n   int i; n   for (i = 1; i <= mx + 1; i++) { n     if (preRight[r][c] - 1 >= i && preDown[r][c] - 1 >= i && n         preDiag2[r + i][c] - 1 >= i) n       res++; n     if (preRight[r][c] - 1 >= i && preDown[r][c + i] - 1 >= i && n         preDiag1[r][c] - 1 >= i) n       res++; n     if (preDown[r][c] - 1 >= i && preRight[r + i][c] - 1 >= i && n         preDiag1[r][c] - 1 >= i) n       res++; n     if (r + i < row && preDiag2[r + i][c] - 1 >= i && n         preRight[r + i][c] - 1 >= i && preDown[r][c + i] - 1 >= i) n       res++; n     if (i % 2 == 0) { n       int j = i / 2; n       if (preRight[r][c] - 1 >= i && preDiag1[r][c] - 1 >= j && n           preDiag2[r + j][c + j] - 1 >= j) n         res++; n       if (preDiag1[r][c] - 1 >= j && preDown[r][c] - 1 >= i && n           preDiag2[r + i][c] - 1 >= j) n         res++; n       if (r + j < row && preRight[r + j][c] - 1 >= i && n           preDiag2[r + j][c] - 1 >= j && preDiag1[r][c + j] - 1 >= j) n         res++; n       if (r + j < row && preDiag2[r + j][c] - 1 >= j && n           preDiag1[r + j][c] - 1 >= j && preDown[r][c + j] - 1 >= i) n         res++; n     } n   } n   return res; n } n int main() { n   scanf( %d %d , &row, &col); n   mx = max(row, col); n   int i, j, k; n   for (i = 0; i < row; i++) scanf( %s , grid[i]); n   precal(); n   long long res = 0; n   for (i = 0; i < row; i++) { n     for (j = 0; j < col; j++) { n       res += triangle(i, j); n     } n   } n   printf( %lld n , res); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n  n #define ll long long n #define pi pair<int, int> n #define pll pair<long long, long long> n #define vi vector<int> n #define vll vector<long long> n #define vpi vector<pair<int, int>> n #define vvi vector<vector<int>> n #define mii map<int, int> n #define mmii multimap<int, int> n #define umii unordered_map<int, int> n #define umsii unordered_multimap<int, int> n #define si set<int> n #define msi multiset<int> n #define usi unordered_set<int> n #define umsi unordered_multiset<int> n #define sc set<char> n  n #define f(i, s, e) for (long long int i = s; i < e; i++) n #define cf(i, s, e) for (long long int i = s; i <= e; i++) n #define rf(i, e, s) for (long long int i = e - 1; i >= s; i--) n #define pb push_back n #define mp make_pair n #define eb emplace_back n #define F first n #define S second n #define all(x) x.begin(), x.end() n #define rall(x) x.rbegin(), x.rend() n  n template <class T> n void print_v(vector<T> &v) { n     cout <<  [ ; n     for (auto x : v) cout << x <<  ,  ; n     cout <<   b b] n ; n } n  n template <class T> n long long int sum_v(vector<T> &v) { n     long long int ans = 0; n     for (auto a : v) ans += a; n     return ans; n } n  n #define MOD 1000000007 n #define PI 3.1415926535897932384626433832795 n ll min(ll a, int b) { n     if (a < b) return a; n     return b; n } n ll min(int a, ll b) { n     if (a < b) return a; n     return b; n } n ll max(ll a, int b) { n     if (a > b) return a; n     return b; n } n ll max(int a, ll b) { n     if (a > b) return a; n     return b; n } n ll gcd(ll a, ll b) { n     if (b == 0) return a; n     return gcd(b, a % b); n } n ll lcm(ll a, ll b) { return a / gcd(a, b) * b; } n string to_upper(string a) { n     for (int i = 0; i < (int)a.size(); ++i) n         if (a[i] >=  a  && a[i] <=  z ) a[i] -=  a  -  A ; n     return a; n } n string to_lower(string a) { n     for (int i = 0; i < (int)a.size(); ++i) n         if (a[i] >=  A  && a[i] <=  Z ) a[i] +=  a  -  A ; n     return a; n } n bool prime(ll a) { n     if (a == 1) return 0; n     for (int i = 2; i <= round(sqrt(a)); ++i) n         if (a % i == 0) return 0; n     return 1; n } n void yes() { cout <<  YES n ; } n void no() { cout <<  NO n ; } n  n typedef long int int32; n typedef unsigned long int uint32; n typedef long long int int64; n typedef unsigned long long int uint64; n  n void dfs(vvi &graph, vi &ans, si &visited, int node) { n     if (ans[node] == -1 || ans[node] == 2) return; n     if (visited.count(node)) { n         vi stack; n         vi tempvisited(graph.size()); n         stack.push_back(node); n         while (!stack.empty()) { n             int val = stack.back(); n             stack.pop_back(); n             ans[val] = -1; n             tempvisited[val] = 1; n             for (int i : graph[val]) { n                 if (!tempvisited[i]) stack.push_back(i); n             } n         } n         return; n     } n     visited.insert(node); n     if (ans[node] != -1) ans[node] = min(ans[node] + 1, 2); n     for (int i : graph[node]) { n         dfs(graph, ans, visited, i); n     } n     visited.erase(node); n } n  n void solve() { n     int n, m; n     cin >> n >> m; n     vvi graph(n + 1); n     vi ans(n + 1); n     si visited; n     f(i, 0, m) { n         int a, b; n         cin >> a >> b; n         graph[a].push_back(b); n     } n     dfs(graph, ans, visited, 1); n     cf(i, 1, n) cout << ans[i] <<    ; n     cout <<   n ; n } n  n int main() { n     ios_base::sync_with_stdio(false); n     cin.tie(NULL); n     int t; n     cin >> t; n     while (t--) { n         solve(); n     } n } n


#include <bits/stdc++.h> n int pn, pr[2000050]; n bool np[2000050] = {0}; n int n; n int ln, ls[2000050]; n inline void gen() { n   pn = 0; n   for (int i = 2; i < 2000050; i++) { n     if (np[i]) continue; n     pr[pn++] = i; n     if (i > 2000) continue; n     for (int j = i * i; j < 2000050; j += i) np[j] = 1; n   } n } n inline int solve() { n   int i, j, p, c = 0; n   if (n == 2) return 0; n   std::sort(ls, ls + ln); n   if (ln && ls[0] == 1) return 1; n   j = 0; n   for (i = 0; i < pn && pr[i] < n; i++) { n     p = pr[i]; n     while (j < ln && ls[j] < p) j++; n     if (j >= ln || ls[j] != p) return -1; n     c++; n   } n   return c; n } n int main(void) { n   gen(); n   while (scanf( %d %d , &ln, &n) == 2) { n     for (int i = 0; i < ln; i++) scanf( %d , ls + i); n     printf( %d n , solve()); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long power(long long a, long long b) { n   if (b == 0) return 1; n   if (b == 1) return a; n   if (b % 2 == 1) return (power(a, b - 1) * a); n   long long q = power(a, b / 2); n   return (q * q); n } n void solve() { n   long long n; n   cin >> n; n   for (long long i = 0; i < n; i++) { n     for (long long j = 0; j < n; j++) { n       if (i == j or i == j + 1 or i == j - 1) n         cout << 1 <<    ; n       else n         cout << 0 <<    ; n     } n     cout << endl; n   } n } n int32_t main() { n   ios_base::sync_with_stdio(false); n   cout.tie(NULL); n   cin.tie(NULL); n   long long t = 1; n   cin >> t; n   while (t--) { n     solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int rev(int a) { n   int ans = 0; n   int shift = 0; n   while (shift <= 7) { n     ans |= (a & 1 << shift ? 1 : 0) << (7 - shift); n     shift++; n   } n   return ans; n } n int main() { n   string a; n   int prev = 0; n   getline(cin, a); n   for (int i = 0; i < a.length(); i++) { n     int val = rev(a[i]); n     int res = prev - val; n     if (res > 0) n       cout << ((res) % 256) << endl; n     else n       cout << (256 + res) % 256 << endl; n     prev = val; n   } n   return 0; n } n


#include<cstdio> n #include<iostream> n #include<algorithm> n int main() n { n  int n; n  char a[100000]; n  bool x = 1; n  while (~scanf( %d , &n)) { n   while (n--) { n    x = 1; n    scanf( %s , a); n    for (int i = 0; a[i] !=   0 ; i++) { n     if (x) { n      if (a[i] !=  a ) n       a[i] =  a ; n      else n       a[i] =  b ; n      x = 0; n     } n     else { n      if (a[i] !=  z ) n       a[i] =  z ; n      else n       a[i] =  z  - 1; n      x = 1; n     } n    } n    printf( %s n , a); n   } n  } n   return 0; n      n } n


#include <bits/stdc++.h> n using namespace std; n typedef struct trie { n   char c; n   bool win, lose; n   struct trie *alphabet[26]; n } trie; n void addString(trie *T, char *s); n trie *initTrie(char c); n void dfs(trie *T); n int main() { n   int i, n, k; n   char s[100005]; n   trie *T = initTrie(  0 ); n   scanf( %d%d , &n, &k); n   for (i = 0; i < n; i++) { n     scanf( %s , s); n     addString(T, s); n   } n   dfs(T); n   if (!(T->win)) n     printf( Second n ); n   else if (T->win && T->lose) n     printf( First n ); n   else if (T->win && !(T->lose)) { n     if (k % 2) n       printf( First n ); n     else n       printf( Second n ); n   } n   return 0; n } n void dfs(trie *T) { n   int i, flag = 1; n   for (i = 0; i < 26; i++) { n     if (T->alphabet[i]) { n       flag = 0; n       dfs(T->alphabet[i]); n       T->win |= !(T->alphabet[i]->win); n       T->lose |= !(T->alphabet[i]->lose); n     } n   } n   if (flag) { n     T->win = false; n     T->lose = true; n   } n } n trie *initTrie(char c) { n   int i; n   trie *T = (trie *)malloc(sizeof(trie)); n   T->win = T->lose = false; n   T->c = c; n   for (i = 0; i < 26; i++) T->alphabet[i] = NULL; n   return T; n } n void addString(trie *T, char *s) { n   int len = strlen(s), i; n   for (i = 0; i < len; i++) { n     if (T->alphabet[s[i] -  a ] == NULL) n       T->alphabet[s[i] -  a ] = initTrie(s[i]); n     T = T->alphabet[s[i] -  a ]; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   string str; n   cin >> str; n   if (str[0] >  4  && str[0] !=  9 ) str[0] = (char)( 9  - str[0] +  0 ); n   for (int x = 1; x < (int)str.length(); x++) n     if (str[x] >  4 ) str[x] = (char)( 9  - str[x] +  0 ); n   cout << str << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e5 + 10; n int dp[N][3][2]; n string a; n int n; n int solve(int x, int state, int start) { n   if (x >= n) return 0; n   if (dp[x][state][start] != -1) return dp[x][state][start]; n   int res = 0; n   if (state == 0) { n     if (start == a[x]) n       res = max(solve(x + 1, 0, 1 - start) + 1, solve(x + 1, 1, start)); n     else n       res = max(solve(x + 1, 1, 1 - start) + 1, solve(x + 1, 0, start)); n   } n   if (state == 1) { n     if (start == a[x]) n       res = max(solve(x + 1, 2, 1 - start) + 1, solve(x + 1, 1, start)); n     else n       res = max(solve(x + 1, 2, start), solve(x + 1, 1, 1 - start) + 1); n   } n   if (state == 2) { n     if (start == a[x]) n       res = solve(x + 1, 2, 1 - start) + 1; n     else n       res = solve(x + 1, 2, start); n   } n   dp[x][state][start] = res; n   return res; n } n int main() { n   std::ios::sync_with_stdio(false); n   ; n   cin >> n; n   memset((dp), (-1), sizeof(dp)); n   cin >> a; n   for (int i = 0; i < n; i++) a[i] -=  0 ; n   cout << solve(0, 0, a[0]) <<   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 = new int[n]; n   for (int i = 0; i < n; i++) { n     cin >> a[i]; n   } n   for (int i = 0; i < n; i++) { n     if (i > 0 && i < n - 1) { n       if (abs(a[i] - a[i + 1]) < abs(a[i] - a[i - 1])) n         cout << abs(a[i] - a[i + 1]); n       else n         cout << abs(a[i] - a[i - 1]); n       if (abs(a[i] - a[n - 1]) < abs(a[i] - a[0])) n         cout <<     << abs(a[i] - a[0]) << endl; n       else n         cout <<     << abs(a[i] - a[n - 1]) << endl; n     } else { n       if (i == 0) { n         cout << abs(a[i] - a[i + 1]); n         cout <<     << abs(a[i] - a[n - 1]) << endl; n       } else if (i == n - 1) { n         cout << abs(a[i] - a[i - 1]); n         cout <<     << abs(a[0] - a[n - 1]) << endl; n       } n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 5e4 + 5; n int main() { n   int k, n, s, p; n   cin >> k >> n >> s >> p; n   int ans = k * (n % s == 0 ? n / s : n / s + 1); n   if (ans % p == 0) n     cout << ans / p << endl; n   else n     cout << ans / p + 1 << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 100005; n const int inf = ~0u >> 2; n int numa[N], numb[N], pos[N]; n struct node { n   int val, id; n   node(int _val, int _id) { val = _val, id = _id; } n   bool operator<(const node& A) const { return val > A.val; } n }; n int now[N], unuse[N]; n priority_queue<node> A, B; n int __ans[N]; n int main() { n   int n; n   cin >> n; n   for (int i = 0; i < n; i++) { n     scanf( %d , &numa[i]); n     pos[numa[i]] = i; n   } n   for (int i = 0; i < n; i++) { n     scanf( %d , &numb[i]); n     int val = i - pos[numb[i]]; n     if (val < 0) n       B.push(node(abs(val), i)); n     else n       A.push(node(abs(val), i)); n     now[i] = val; n   } n   int tmp = 0, len = 0; n   for (int k = n - 1; k >= 0; k--) { n     int pa = A.empty() ? inf : A.top().val + tmp; n     int pb = B.empty() ? inf : B.top().val - tmp; n     __ans[len++] = min(pa, pb); n     while (pb == 0) { n       int id = B.top().id; n       A.push(node(-tmp, id)); n       B.pop(); n       if (B.empty()) break; n       pb = B.top().val - tmp; n     } n     unuse[k] = 1; n     int np = pos[numb[k]]; n     now[k] = -np - tmp - 1; n     while (!A.empty() && (A.top().val + tmp != abs(now[A.top().id] + tmp) && n                           unuse[A.top().id])) { n       unuse[A.top().id] = 0; n       A.pop(); n     } n     tmp++; n     if (np == 0) n       A.push(node(-tmp, k)); n     else n       B.push(node(np + tmp, k)); n   } n   cout << __ans[0] << endl; n   for (int i = len - 1; i; i--) printf( %d n , __ans[i]); n } n


#pragma GCC optimize( Ofast ) n #include<bits/stdc++.h> n using namespace std; n  n #define int long long int n #define double long double n  n #define all(x) begin(x), end(x) n #define Done(x) cout<<x<<endl; return; n #define endl   n  n  n void solve(){ n  int q; cin>>q; n  for(int i=0;i<q;i++){ n   int u; cin>>u; n   int v; cin>>v; n   if(v<u){ n    cout<< NO <<endl; n   }else if(v==u){ n    cout<< YES <<endl; n   }else{ n    vector<int>cnt(31); n    for(int i=0;i<30;i++){ n     if(u&(1<<i)){ n      cnt[i]++; n     } n    } n    bool can=true; n    for(int i=0;i<30;i++){ n     int val=(1<<i); n     if(v&val){ n      if(cnt[i]==0){ n       can=false; n       break; n      }else{ n       cnt[i]-=1; n       cnt[i+1]+=cnt[i]; n      } n     }else{ n      cnt[i+1]+=cnt[i]; n     } n    } n    if(can){ n     cout<< YES <<endl; n    }else{ n     cout<< NO <<endl; n    } n   } n  } n } n  n int32_t main(){ n  // Remove for Interactive Problems n  ios_base::sync_with_stdio(false); n  cin.tie(0); cout.tie(0); n  n  solve(); n   n  cerr<< Time :  <<1000*((double)clock())/(double)CLOCKS_PER_SEC<< ms n ; n  return 0; n }


#include <bits/stdc++.h> n using namespace std; n const int maxn = 400003, INF = 0x3f3f3f3f; n int n, Q, sz[maxn], fa[maxn], son[maxn], dfn[maxn], cntdfn, rig[maxn], n     top[maxn], dep[maxn], z[maxn]; n vector<int> g[maxn]; n struct T { n   int mx, sum; n   T operator+(T x) const { return (T){max(x.mx, x.sum + mx), sum + x.sum}; } n } t[maxn]; n void init1(int u) { n   sz[u] = 1; n   int mxsz = -1; n   for (int v : g[u]) { n     dep[v] = dep[u] + 1; n     init1(v); n     if (sz[v] > mxsz) mxsz = sz[v], son[u] = v; n     sz[u] += sz[v]; n   } n } n void init2(int u, int tp) { n   top[u] = tp; n   dfn[u] = ++cntdfn; n   if (son[u]) init2(son[u], tp); n   for (int v : g[u]) { n     if (v == son[u]) continue; n     init2(v, v); n   } n   rig[u] = cntdfn; n } n void solve(int p, int len) { t[p] = (T){-1, -len}, z[p] = 1; } n void pushdown(int p, int l, int r) { n   if (z[p]) { n     int mid = (l + r) >> 1; n     solve(p << 1, mid - l + 1), solve(p << 1 | 1, r - mid); n     z[p] = 0; n   } n } n void add(int p, int l, int r, int pos, int k) { n   if (l == r) { n     t[p].sum += k, t[p].mx += k; n     return; n   } n   pushdown(p, l, r); n   int mid = (l + r) >> 1; n   if (pos <= mid) n     add(p << 1, l, mid, pos, k); n   else n     add(p << 1 | 1, mid + 1, r, pos, k); n   t[p] = t[p << 1] + t[p << 1 | 1]; n } n void cover(int p, int l, int r, int seg_l, int seg_r) { n   if (seg_l <= l && r <= seg_r) { n     solve(p, r - l + 1); n     return; n   } n   pushdown(p, l, r); n   int mid = (l + r) >> 1; n   if (seg_l <= mid) cover(p << 1, l, mid, seg_l, seg_r); n   if (seg_r > mid) cover(p << 1 | 1, mid + 1, r, seg_l, seg_r); n   t[p] = t[p << 1] + t[p << 1 | 1]; n } n T query(int p, int l, int r, int seg_l, int seg_r) { n   if (seg_l <= l && r <= seg_r) return t[p]; n   pushdown(p, l, r); n   int mid = (l + r) >> 1; n   if (seg_l <= mid && seg_r > mid) n     return query(p << 1, l, mid, seg_l, seg_r) + n            query(p << 1 | 1, mid + 1, r, seg_l, seg_r); n   else if (seg_l <= mid) n     return query(p << 1, l, mid, seg_l, seg_r); n   else n     return query(p << 1 | 1, mid + 1, r, seg_l, seg_r); n } n T query(int x) { n   T ret = (T){-INF, 0}; n   while (x) { n     ret = query(1, 1, n, dfn[top[x]], dfn[x]) + ret; n     x = fa[top[x]]; n   } n   return ret; n } n int main() { n   scanf( %d%d , &n, &Q); n   for (int i = 2; i <= n; i++) scanf( %d , fa + i), g[fa[i]].push_back(i); n   init1(1), init2(1, 1), cover(1, 1, n, 1, n); n   while (Q--) { n     int mo, x; n     scanf( %d%d , &mo, &x); n     if (mo == 1) { n       add(1, 1, n, dfn[x], 1); n     } n     if (mo == 2) { n       cover(1, 1, n, dfn[x], rig[x]); n       int tmp = max(query(x).mx + 1, 0); n       add(1, 1, n, dfn[x], -tmp); n     } n     if (mo == 3) { n       puts(query(x).mx >= 0 ?  black  :  white ); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m, x, y, fail; n int G[305][305], v[305], u[305], l[305], z[305]; n long long int A, B, C, a[305], b[305], d[305]; n int rx[305 * 305 * 2], ry[305 * 305 * 2], rc[305 * 305 * 2], cnt; n void dfs(int x) { n   v[x] = 1; n   A += a[x]; n   B += b[x]; n   for (int i = (0); i < (n); i++) n     if (!v[i] && G[x][i]) G[x][i] = G[i][x] = 2, dfs(i); n   l[m++] = x; n } n void add(int x, int y, long long int c) { n   rx[cnt] = x + 1, ry[cnt] = y + 1, rc[cnt++] = (int)c; n } n void pull(int x, long long int d) { n   z[x] = 1; n   for (int i = (0); i < (n); i++) n     if (G[x][i] == 2 && !u[i] && d && !z[i]) { n       if (a[i] >= d) { n         a[i] -= d; n         a[x] += d; n         add(i, x, d); n         d = 0; n       } else { n         pull(i, d - a[i]); n         long long int t = min(d, a[i]); n         a[x] += t; n         a[i] -= t; n         d -= t; n         add(i, x, t); n       } n     } n } n void push(int x, long long int d) { n   z[x] = 1; n   for (int i = (0); i < (n); i++) n     if (G[x][i] == 2 && !u[i] && d && !z[i]) { n       if (C - a[i] >= d) { n         a[x] -= d; n         a[i] += d; n         add(x, i, d); n         d = 0; n       } else { n         push(i, d - (C - a[i])); n         long long int t = min(d, C - a[i]); n         a[i] += t; n         a[x] -= t; n         d -= t; n         add(x, i, t); n       } n     } n } n int main() { n   scanf( %d%I64d%d , &n, &C, &m); n   for (int i = (0); i < (n); i++) scanf( %I64d , &a[i]); n   for (int i = (0); i < (n); i++) scanf( %I64d , &b[i]); n   while (m--) { n     scanf( %d%d , &x, &y), x--, y--; n     G[x][y] = G[y][x] = 1; n   } n   for (int i = (0); i < (n); i++) n     if (!v[i]) { n       A = B = 0; n       m = 0; n       dfs(i); n       if (A != B) { n         fail = 1; n         break; n       } n       for (int j = (0); j < (m); j++) { n         int r = l[j]; n         memset(z, 0, sizeof(z)); n         if (a[r] > b[r]) push(r, a[r] - b[r]); n         if (a[r] < b[r]) pull(r, b[r] - a[r]); n         u[r] = 1; n       } n     } n   if (fail) n     puts( NO ); n   else { n     printf( %d n , cnt); n     for (int i = (0); i < (cnt); i++) printf( %d %d %d n , rx[i], ry[i], rc[i]); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long int mod = 1e9 + 7; n mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); n const long long int N = 1e5 + 5; n long long int a[N], b[N], n, d, x; n long long int getNextX() { n   x = (x * 37 + 10007) % 1000000007; n   return x; n } n void initAB() { n   for (long long int i = 0; i < n; i++) { n     a[i] = i + 1; n   } n   for (long long int i = 0; i < n; i++) { n     swap(a[i], a[getNextX() % (i + 1)]); n   } n   for (long long int i = 0; i < n; i++) { n     if (i < d) n       b[i] = 1; n     else n       b[i] = 0; n   } n   for (long long int i = 0; i < n; i++) { n     swap(b[i], b[getNextX() % (i + 1)]); n   } n } n long long int pos[N]; n vector<long long int> bb; n long long int c[N]; n void solve() { n   cin >> n >> d >> x; n   initAB(); n   for (long long int i = 0; i <= n - 1; i++) { n     if (b[i]) bb.push_back(i); n   } n   for (long long int i = 0; i <= n - 1; i++) { n     pos[a[i]] = i; n   } n   if (bb.size() <= 500) { n     for (long long int i = 0; i <= n - 1; i++) { n       for (long long int j : bb) { n         if (j > i) break; n         c[i] = max(c[i], a[i - j]); n       } n     } n   } else { n     for (long long int i = 0; i <= n - 1; i++) n       for (long long int j = n; j >= 1; j--) { n         long long int p = pos[j]; n         if (p <= i && b[i - p]) { n           c[i] = j; n           break; n         } n       } n   } n   for (long long int i = 0; i <= n - 1; i++) cout << c[i] <<    ; n } n signed main() { n   ios ::sync_with_stdio(0), cin.tie(0), cout.tie(0); n   clock_t clk = clock(); n   long long int t; n   t = 1; n   while (t--) { n     solve(); n   } n   cerr << (double)(clock() - clk) / CLOCKS_PER_SEC; n   return 0; n } n


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


#include <bits/stdc++.h> n char b[20]; n using namespace std; n int main() { n   char a[14]; n   for (int i = 1; i <= 2; i++) { n     scanf( %s , a); n     if (strcmp(a,  monday ) == 0) b[i] = 1; n     if (strcmp(a,  tuesday ) == 0) b[i] = 2; n     if (strcmp(a,  wednesday ) == 0) b[i] = 3; n     if (strcmp(a,  thursday ) == 0) b[i] = 4; n     if (strcmp(a,  friday ) == 0) b[i] = 5; n     if (strcmp(a,  saturday ) == 0) b[i] = 6; n     if (strcmp(a,  sunday ) == 0) b[i] = 7; n   } n   int flag = 1; n   for (int j = 3; j <= 5; j++) { n     if (j * 7 - b[1] + b[2] == 31 || j * 7 - b[1] + b[2] == 30 || n         j * 7 - b[1] + b[2] == 28) { n       printf( YES n ); n       flag = 0; n     } n   } n   while (flag) { n     printf( NO n ); n     flag = 0; n   } n } n


#include <bits/stdc++.h> n using namespace std; n long long powmod(long long a, long long b) { n   long long res = 1; n   while (b > 0) { n     if (b & 1) res = res * a % 2; n     a = a * a % 2; n     b >>= 1; n   } n   return res; n } n int main() { n   ios_base::sync_with_stdio(false); n   long long b, k, sum = 0; n   cin >> b >> k; n   long long a[k]; n   for (long long i = 1; i <= k; i++) { n     cin >> a[i]; n     long long p = powmod(b, k - i); n     sum += a[i] * p; n   } n   if (sum % 2) n     cout <<  odd ; n   else n     cout <<  even ; n } n


#include <bits/stdc++.h> n using namespace std; n inline void read(int &x) { n   char ch; n   bool flag = false; n   for (ch = getchar(); !isdigit(ch); ch = getchar()) n     if (ch ==  - ) flag = true; n   for (x = 0; isdigit(ch); x = x * 10 + ch -  0 , ch = getchar()) n     ; n   x = flag ? -x : x; n } n inline void write(int x) { n   static const int maxlen = 100; n   static char s[maxlen]; n   if (x < 0) { n     putchar( - ); n     x = -x; n   } n   if (!x) { n     putchar( 0 ); n     return; n   } n   int len = 0; n   for (; x; x /= 10) s[len++] = x % 10 +  0 ; n   for (int i = len - 1; i >= 0; --i) putchar(s[i]); n } n int n, m; n int pre[11000], now[110], son[11000], v[11000], tot; n int f[110][110][30]; n char s[100]; n void build(int a, int b, int c) { n   pre[++tot] = now[a]; n   now[a] = tot; n   son[tot] = b; n   v[tot] = c; n } n int dfs(int x, int y, int MAX) { n   if (f[x][y][MAX] != -1) return f[x][y][MAX]; n   set<int> S; n   for (int p = now[x]; p; p = pre[p]) n     if (v[p] >= MAX) S.insert(dfs(y, son[p], v[p])); n   int q = -1; n   for (auto t : S) { n     if (t != q + 1) break; n     q = t; n   } n   return f[x][y][MAX] = q + 1; n } n int main() { n   read(n); n   read(m); n   for (int i = 1; i <= m; i++) { n     int a, b; n     read(a); n     read(b); n     scanf( %s , s); n     build(a, b, s[0] -  a  + 1); n   } n   memset(f, -1, sizeof(f)); n   for (int i = 1; i <= n; i++) n     for (int j = 1; j <= n; j++) dfs(i, j, 0); n   for (int i = 1; i <= n; i++) { n     for (int j = 1; j <= n; j++) n       if (f[i][j][0]) n         printf( A ); n       else n         printf( B ); n     puts(  ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long i, j, n, cnt = 0, flag = 0, k = 0; n   string str; n   cin >> n >> str; n   for (i = 0; i < n; i++) { n     if (str[i] ==  ( ) { n       cnt++; n       if (cnt == 2) j = i; n     } else n       cnt--; n     if ((cnt == -2) && (!k)) k = i; n     if (cnt < -2) break; n     if (cnt < 0) flag = 1; n   } n   if ((!cnt) || (flag && cnt == 2) || (i != n) || ((cnt != 2) && (cnt != -2))) { n     cout <<  0 n ; n     return 0; n   } n   if (cnt == 2) { n     cnt = 0; n     for (i = j; i < n; i++) { n       if (str[i] ==  ( ) cnt++; n     } n   } else { n     cnt = 0; n     for (i = 0; i < k; i++) { n       if (str[i] ==  ( ) n         cnt++; n       else n         cnt--; n       if (cnt == -1) break; n     } n     cnt = 0; n     k = i; n     for (i = 0; i <= k; i++) { n       if (str[i] ==  ) ) cnt++; n     } n   } n   cout << cnt << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   cin >> n; n   pair<int, int> p[n]; n   int t = 0; n   for (int i = 0; i < n; i++) { n     cin >> p[i].first >> p[i].second; n   } n   sort(p, p + n); n   int start = 0, l = 0, r = 0, s = 0; n   for (int i = 0; i < 2 * n; i++) { n     if (i < n && p[i].first > 0) { n       start = 1; n     } n     if (start) { n       l = i - 1; n       r = i; n       while (1) { n         if (l < 0 && r == n) break; n         if (l < 0) { n           s += p[r].second; n           break; n         } else if (r == n) { n           s += p[l].second; n           break; n         } n         s += p[l--].second + p[r++].second; n       } n       return cout << s, 0; n     } n   } n   cout << p[n - 1].second << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n; n int a[100100]; n int dig[11]; n int ans[11]; n int main() { n   int sum = 0; n   bool pos = false; n   cin >> n; n   for (int i = 0; i < n; i++) { n     cin >> a[i]; n     dig[a[i]]++; n     sum += a[i]; n   } n   if (dig[0] == 0) { n     cout << -1; n     return 0; n   } n   if (sum % 3 == 0) pos = true; n   if (sum % 3 == 1) { n     if (dig[1] > 0) { n       dig[1] -= 1; n       pos = true; n     } else if (dig[4] > 0) { n       pos = true; n       dig[4] -= 1; n     } else if (dig[7] > 0) { n       dig[7] -= 1; n       pos = true; n     } else { n       int a = 10, ai, aj; n       for (int i = 0; i <= 9; i++) n         for (int j = 0; j <= 9; j++) n           if (i != j) { n             if ((i + j) % 3 == 1 && dig[i] > 0 && dig[j] > 0 && max(i, j) < a) { n               a = max(i, j); n               ai = i; n               aj = j; n               pos = true; n             } n           } else { n             if ((i + j) % 3 == 1 && dig[i] > 1 && max(i, j) < a) { n               a = max(i, j); n               ai = i; n               aj = j; n               pos = true; n             } n           } n       dig[ai] -= 1; n       dig[aj] -= 1; n     } n   } else if (sum % 3 == 2) { n     if (dig[2] > 0) { n       dig[2] -= 1; n       pos = true; n     } else if (dig[5] > 0) { n       dig[5] -= 1; n       pos = true; n     } else if (dig[8] > 0) { n       dig[8] -= 1; n       pos = true; n     } else { n       int a = 10, ai, aj; n       for (int i = 0; i <= 9; i++) n         for (int j = 0; j <= 9; j++) n           if (i != j) { n             if ((i + j) % 3 == 2 && dig[i] > 0 && dig[j] > 0 && max(i, j) < a) { n               a = max(i, j); n               ai = i; n               aj = j; n               pos = true; n             } n           } else { n             if ((i + j) % 3 == 2 && dig[i] > 1 && max(i, j) < a) { n               a = max(i, j); n               ai = i; n               aj = j; n               pos = true; n             } n           } n       dig[ai] -= 1; n       dig[aj] -= 1; n     } n   } n   if (pos) { n     bool shown = false; n     for (int i = 9; i > 0; i--) n       for (int j = 0; j < dig[i]; j++) { n         cout << i; n         shown = true; n       } n     if (shown) n       for (int j = 0; j < dig[0]; j++) cout << 0; n     else n       cout << 0; n   } else n     cout << 0; n } n


#include <bits/stdc++.h> n using namespace std; n int getInt() { n   int ret; n   scanf( %d , &ret); n   return ret; n } n int main() { n   int n = getInt(); n   vector<pair<int, int> > pr(n); n   for (int i = 0; i < (int)(n); i++) { n     pr[i].first = getInt(); n     pr[i].second = getInt(); n   } n   sort(pr.begin(), pr.end()); n   int ans = 0; n   int mxp = 0; n   int mx = 0; n   for (int i = 0; i < (int)(pr.size()); i++) { n     if (i != 0) { n       if (pr[i].first != pr[i - 1].first) { n         mx = mxp; n       } n     } n     if (mx > pr[i].second) ans++; n     mxp = max(mxp, pr[i].second); n   } n   printf( %d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int f[105][45], head[105], la[45]; n int n, k, i, j, ans, u, v, cnt; n struct ff { n   int to, nxt; n } e[2 * 105]; n void add(int u, int v) { n   e[++cnt] = (ff){v, head[u]}; n   head[u] = cnt; n } n void dfs(int now, int fa) { n   if (e[head[now]].nxt == 0 && fa) { n     f[now][0] = f[now][k + 1] = 1; n     return; n   } n   bool flag = 1; n   for (int i = head[now]; i; i = e[i].nxt) { n     int v = e[i].to; n     if (v == fa) continue; n     dfs(v, now); n     if (flag) { n       for (int j = 0; j <= 2 * k; j++) f[now][j + 1] = f[v][j]; n       flag = 0; n     } else { n       for (int j = 0; j <= 2 * k + 1; j++) la[j] = f[now][j], f[now][j] = 0; n       for (int j = 0; j <= 2 * k; j++) n         for (int h = 1; h <= 2 * k + 1; h++) n           if (j + h <= 2 * k) { n             int Min = min(j + 1, h); n             f[now][Min] = n                 (f[now][Min] + 1ll * la[h] * f[v][j] % 1000000007) % 1000000007; n           } else { n             int Max = max(j + 1, h); n             f[now][Max] = n                 (f[now][Max] + 1ll * la[h] * f[v][j] % 1000000007) % 1000000007; n           } n     } n   } n   for (int i = 1; i <= 2 * k + 1; i++) n     f[now][0] = (f[now][0] + f[now][i]) % 1000000007; n } n int main() { n   scanf( %d%d , &n, &k); n   for (i = 1; i < n; i++) { n     scanf( %d%d , &u, &v); n     add(u, v), add(v, u); n   } n   dfs(1, 0); n   for (i = 0; i <= k; i++) ans = (ans + f[1][i]) % 1000000007; n   if (ans == 0) ans = 1; n   printf( %d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct node { n   long long siz, fa; n   bool vis; n } a[1001]; n string s; n long long n, cnt[1001][1001]; n vector<long long> e[1001]; n vector<vector<long long> > t[1001]; n vector<pair<long long, long long> > ans; n void dfs(long long x) { n   a[x].siz = a[x].vis = 1; n   for (long long v : e[x]) n     if (v != a[x].fa) { n       if (a[v].vis) cout <<  -1 , exit(0); n       a[v].fa = x, dfs(v), a[x].siz += a[v].siz; n     } n } n vector<vector<long long> > tmp; n void check(long long u, long long fa) { n   tmp.back().push_back(u); n   for (long long v : e[u]) n     if (v != fa) check(v, u); n } n inline void chec() { n   for (long long u = 1; u <= n; u++) { n     tmp.clear(); n     for (long long v : e[u]) { n       tmp.push_back(vector<long long>()); n       check(v, u); n       sort(tmp.back().begin(), tmp.back().end()); n     } n     sort(tmp.begin(), tmp.end()); n     if (t[u] != tmp) cout <<  -1 n , exit(0); n   } n } n inline long long get_num() { n   char temp = s.back(); n   s.pop_back(); n   long long ans = 0; n   while (temp >=  0  && temp <=  9 ) { n     ans = ans * 10 + temp -  0 ; n     if (s.empty()) break; n     temp = s.back(), s.pop_back(); n   } n   return ans; n } n int main() { n   std::cin.tie(0); n   std::ios::sync_with_stdio(false); n   cin >> n; n   for (long long i = 1; i <= n; i++) { n     cin >> s, reverse(s.begin(), s.end()); n     long long d, tot = 0; n     while (tot < n - 1) { n       t[i].push_back(vector<long long>()); n       d = get_num(), tot += d; n       for (long long j = 1; j <= d; j++) n         t[i].back().push_back(get_num()), cnt[i][t[i].back().back()] = d; n       sort(t[i].back().begin(), t[i].back().end()); n     } n     sort(t[i].begin(), t[i].end()); n   } n   for (long long u = 1; u <= n; u++) n     for (long long v = u + 1; v <= n; v++) n       if (cnt[u][v] + cnt[v][u] == n) n         e[u].push_back(v), e[v].push_back(u), ans.push_back(make_pair(u, v)); n   if ((long long)ans.size() != (n - 1)) cout <<  -1 , exit(0); n   dfs(1); n   if (a[1].siz != n) cout <<  -1 , exit(0); n   chec(); n   cout << ans.size() <<   n ; n   for (auto t : ans) cout << t.first <<     << t.second <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e6 + 1e3 + 7; n int n, Q, m; n int p[N]; n int a[N], b[N]; n struct Query { n   int t, x; n } q[N]; n int nd, fa[N], del[N]; n int find(int x) { return x == fa[x] ? x : fa[x] = find(fa[x]); } n int rt[N]; n vector<int> g[N]; n int vis[N]; n int st[N], ed[N], dc, rev[N]; n void dfs(int x) { n   if (vis[x]) return; n   st[x] = ++dc; n   vis[x] = 1; n   rev[st[x]] = x; n   for (auto v : g[x]) dfs(v); n   ed[x] = dc; n } n int cnt; n struct T { n   int ls, rs, l, r; n   pair<int, int> mx; n } t[N * 2 + 1]; n void update(int x) { t[x].mx = max(t[t[x].ls].mx, t[t[x].rs].mx); } n int build(int l, int r) { n   int x = ++cnt; n   t[x].l = l, t[x].r = r; n   if (l == r) { n     t[x].mx = make_pair(p[rev[l]], l); n     return x; n   } n   int mid = (l + r) >> 1; n   t[x].ls = build(l, mid); n   t[x].rs = build(mid + 1, r); n   update(x); n   return x; n } n void change(int x, int p, int v) { n   if (t[x].l == t[x].r) { n     t[x].mx.first = v; n     return; n   } n   int mid = (t[x].l + t[x].r) >> 1; n   if (p <= mid) n     change(t[x].ls, p, v); n   else n     change(t[x].rs, p, v); n   update(x); n } n pair<int, int> query(int x, int l, int r) { n   if (l <= t[x].l && t[x].r <= r) return t[x].mx; n   int mid = (t[x].l + t[x].r) >> 1; n   pair<int, int> mx = make_pair(0, 0); n   if (l <= mid) mx = max(mx, query(t[x].ls, l, r)); n   if (r > mid) mx = max(mx, query(t[x].rs, l, r)); n   return mx; n } n int main() { n   scanf( %d%d%d , &n, &m, &Q); n   for (int i = 1; i <= n; i++) scanf( %d , &p[i]), fa[i] = i; n   for (int i = 1; i <= m; i++) scanf( %d%d , &a[i], &b[i]); n   for (int i = 1; i <= Q; i++) { n     scanf( %d%d , &q[i].t, &q[i].x); n     if (q[i].t == 2) del[q[i].x] = 1; n   } n   nd = n; n   for (int i = 1; i <= m; i++) n     if (!del[i]) { n       int fx = find(a[i]), fy = find(b[i]); n       if (fx == fy) continue; n       ++nd; n       g[nd].push_back(fx); n       g[nd].push_back(fy); n       fa[fx] = fa[fy] = fa[nd] = nd; n     } n   for (int i = Q; i >= 1; i--) { n     if (q[i].t == 2) { n       int fx = find(a[q[i].x]), fy = find(b[q[i].x]); n       if (fx == fy) continue; n       ++nd; n       g[nd].push_back(fx); n       g[nd].push_back(fy); n       fa[fx] = fa[fy] = fa[nd] = nd; n     } else n       rt[i] = find(q[i].x); n   } n   for (int i = nd; i >= 1; i--) dfs(i); n   build(1, nd); n   for (int i = 1; i <= Q; i++) { n     if (q[i].t == 1) { n       pair<int, int> ans = query(1, st[rt[i]], ed[rt[i]]); n       printf( %d n , ans.first); n       if (ans.second) change(1, ans.second, 0); n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n long long a[10010]; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   long long n, m, k; n   cin >> n >> m >> k; n   if (n % 2 == 0) { n     cout << 0; n     return 0; n   } n   if (m < ((n + 1) / 2)) { n     cout << 0; n     return 0; n   } n   long long can = m / ((n + 1) / 2) * k; n   long long mn = 1e9; n   for (long long i = 1; i <= n; ++i) { n     cin >> a[i]; n     if (i & 1) mn = min(mn, a[i]); n   } n   cout << min(mn, can) << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 2000005; n int at[MAXN]; n long long int n, x, y; n int main() { n   cin >> n >> x >> y; n   int a; n   long long int f, g; n   f = y; n   g = x; n   for (int i = 0; i < x + y; i++) { n     if (f < g) { n       at[i] = 1; n       f += y; n     } else if (g < f) { n       at[i] = 2; n       g += x; n     } else { n       at[i] = at[++i] = 3; n       g += x; n       f += y; n     } n   } n   for (int i = 0; i < n; i++) { n     cin >> a; n     a--; n     a %= x + y; n     if (at[a] == 2) n       cout <<  Vova ; n     else if (at[a] == 1) n       cout <<  Vanya ; n     else n       cout <<  Both ; n     cout << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename Arg1> n void __f(const char* name, Arg1&& arg1) { n   cerr << name <<   :   << arg1 << std::endl; n } n template <typename Arg1, typename... Args> n void __f(const char* names, Arg1&& arg1, Args&&... args) { n   const char* comma = strchr(names + 1,  , ); n   cerr.write(names, comma - names) <<   :   << arg1 <<   |  ; n   __f(comma + 1, args...); n } n long long ex(long long a, long long b) { n   long long ans = 1; n   while (b) { n     if (b & 1) ans = (ans * a) % 998244353; n     a = (a * a) % 998244353, b /= 2; n   } n   return ans; n } n inline long long add(long long a, long long b) { n   a = a % 998244353 + b % 998244353; n   if (a > 998244353) a %= 998244353; n   return a; n } n inline long long sub(long long a, long long b) { n   a = a % 998244353 - b % 998244353; n   if (a < 0) a += 998244353; n   return a; n } n inline long long mul(long long a, long long b) { n   a %= 998244353, b %= 998244353; n   return (a * b) % 998244353; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   ; n   long long n, q; n   cin >> n >> q; n   long long a[n + 2], b[n + 2], c[n + 2]; n   for (long long i = 1; i < n + 1; ++i) n     cin >> a[i], a[i] = mul(100, ex(a[i], 998244353 - 2)); n   b[0] = 0, c[n + 1] = 1; n   for (long long i = n; i >= 1; --i) c[i] = mul(c[i + 1], a[i]); n   for (long long i = 1; i < n + 1; ++i) b[i] = add(b[i - 1], c[i]); n   auto get = [&](long long l, long long r) { n     long long x = sub(b[r], b[l - 1]); n     x = mul(x, ex(c[r + 1], 998244353 - 2)); n     return x; n   }; n   long long ans = get(1, n), x; n   set<long long> s; n   s.insert(1); n   for (long long i = 0; i < q; ++i) { n     long long u; n     cin >> u; n     if (s.count(u)) { n       auto it = s.find(u); n       auto it1 = s.upper_bound(u); n       if (it1 == s.end()) n         x = n; n       else n         x = *it1 - 1; n       auto it2 = s.lower_bound(u); n       it2--; n       ans = add(ans, get(*it2, x)); n       ans = sub(ans, add(get(*it2, *it - 1), get(*it, x))); n       s.erase(u); n     } else { n       s.insert(u); n       auto it = s.find(u); n       auto it1 = s.upper_bound(u); n       if (it1 == s.end()) n         x = n; n       else n         x = *it1 - 1; n       auto it2 = s.find(u); n       it2--; n       ans = sub(ans, get(*it2, x)); n       ans = add(ans, add(get(*it2, *it - 1), get(*it, x))); n     } n     cout << ans <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, root = -1, nextnum = 3; n vector<vector<int>> edges[100005]; n int order[100005]; n int main() { n   cin >> n; n   for (int i = 1; i <= n - 1; i++) { n     int a, b; n     cin >> a >> b; n     edges[a].push_back({b, i}); n     edges[b].push_back({a, i}); n   } n   for (int i = 1; i <= n; i++) { n     if (edges[i].size() > 2) { n       root = i; n       break; n     } n   } n   if (root == -1) { n     for (int i = 0; i <= n - 2; i++) { n       cout << i <<   n ; n     } n   } else { n     for (int i = 1; i <= n - 1; i++) { n       order[i] = -1; n     } n     order[edges[root][0][1]] = 0; n     order[edges[root][1][1]] = 1; n     order[edges[root][2][1]] = 2; n     for (int i = 1; i <= n; i++) { n       for (int j = 0; j < edges[i].size(); j++) { n         if (order[edges[i][j][1]] == -1) { n           order[edges[i][j][1]] = nextnum; n           nextnum++; n         } n       } n     } n     for (int i = 1; i <= n - 1; i++) { n       cout << order[i] <<   n ; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int a[5000]; n int main() { n   int n, k, l, r, s, sk; n   cin >> n >> k >> l >> r >> s >> sk; n   int d = s - sk; n   for (int i = 1; i <= k; i++) { n     a[i] = sk / k; n     if (sk % k != 0) { n       a[i]++; n       sk--; n     } n   } n   if (k != n) { n     for (int i = k + 1; i <= n; i++) { n       a[i] = d / (n - k); n       if (d % (n - k) != 0) { n         a[i]++; n         d--; n       } n     } n   } n   for (int i = 1; i <= n; i++) cout << a[i] <<    ; n   cout << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n signed main(int argc, char const *argv[]) { n   int t; n   cin >> t; n   while (t--) { n     int n; n     string s, k; n     cin >> n; n     cin >> s >> k; n     int ans = 0; n     string prev =  01 ; n     for (int i = 0; i < n; i++) { n       if ((s[i] ==  0  && k[i] ==  1 ) || (s[i] ==  1  && k[i] ==  0 )) { n         prev =  01 ; n         ans += 2; n       } else if (s[i] ==  0  && k[i] ==  0 ) { n         ans++; n         if (prev ==  11 ) { n           ans++; n           prev =  01 ; n         } else n           prev =  00 ; n       } else { n         if (prev ==  00 ) { n           ans++; n           prev =  01 ; n         } else n           prev =  11 ; n       } n     } n     cout << ans <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m; n pair<int, int> p[100005], ans[100005]; n int tot, start[100005], remain, now; n int f[100005]; n priority_queue<pair<int, int> > Q; n int main() { n   cin >> n >> m; n   for (int i = 1; i <= m; i++) { n     scanf( %d%d , &p[i].first, &p[i].second); n     if (p[i].second == 0) p[i].second = -1; n     p[i].second = -p[i].second; n     p[i].second *= i; n   } n   sort(p + 1, p + m + 1); n   for (int i = 1; i <= n; i++) f[i] = i, start[i] = i + 1; n   remain = 1; n   now = 1; n   for (int i = 1; i <= m; i++) { n     if (p[i].second > 0) { n       if (Q.empty()) { n         return puts( -1 ), 0; n       } else { n         int x = Q.top().first, y = Q.top().second; n         Q.pop(); n         if (-x <= remain) { n           ans[p[i].second] = make_pair(-x, y); n           if (-x != n) Q.push(make_pair(x - 1, y)); n         } else { n           return puts( -1 ); n         } n       } n     } else { n       remain++; n       if (remain <= n) { n         Q.push(make_pair(-remain - 1, remain)); n         ans[-p[i].second] = make_pair(1, remain); n       } else { n         return puts( -1 ), 0; n       } n     } n   } n   for (int i = 1; i <= m; i++) printf( %d %d n , ans[i].first, ans[i].second); n } n


#include <bits/stdc++.h> n using namespace std; n int n; n int x[1024], y[1024], c[1024]; n int pts[1024]; n int chull[1024], tp; n int chull_size; n int ans_l[1024], ans_r[1024], ans_len = 0; n void add_edge(int u, int v) { n   ans_l[ans_len] = u; n   ans_r[ans_len] = v; n   ans_len++; n } n void print_ans() { n   printf( %d n , ans_len); n   for (int i = 0; i < ans_len; i++) printf( %d %d n , ans_l[i], ans_r[i]); n } n bool ccw(int i, int j, int k) { n   return (x[i] * y[j] + x[j] * y[k] + x[k] * y[i] - x[j] * y[i] - x[k] * y[j] - n           x[i] * y[k]) > 0; n } n int pivot; n bool comp_by_pivot(int i, int j) { n   assert(i != pivot); n   assert(j != pivot); n   return ccw(pivot, i, j); n } n void run(int i, int j, int k, const vector<int> &inner, bool sep) { n   if (inner.size() == 0) { n     if (sep) { n       ; n     } else { n       add_edge(j, k); n     } n   } else { n     int pivot = -1; n     for (int p : inner) { n       if (pivot == -1 || ccw(j, p, pivot)) pivot = p; n     } n     vector<int> j_inner, k_inner; n     for (int p : inner) n       if (p != pivot) { n         if (ccw(pivot, i, p)) n           j_inner.push_back(p); n         else n           k_inner.push_back(p); n       } n     if (c[pivot] == c[i]) { n       run(j, pivot, i, j_inner, false); n       run(k, i, pivot, k_inner, true); n       if (!sep) add_edge(j, k); n     } else { n       run(i, j, pivot, j_inner, false); n       run(i, pivot, k, k_inner, sep); n     } n   } n } n void run(int i, int j, int k, bool sep) { n   assert(i != j && j != k && k != i); n   assert(ccw(i, j, k)); n   vector<int> inner; n   for (int l = 0; l < n; l++) n     if (l != i && l != j && l != k && ccw(i, j, l) && ccw(j, k, l) && n         ccw(k, i, l)) { n       inner.push_back(l); n     } n   run(i, j, k, inner, sep); n } n int main() { n   scanf( %d , &n); n   for (int i = 0; i < n; i++) { n     scanf( %d %d %d , &x[i], &y[i], &c[i]); n   } n   if (n == 1) { n     printf( 0 n ); n   } else if (n == 2) { n     if (c[0] == c[1]) { n       printf( 1 n ); n       printf( 0 1 n ); n     } else { n       printf( 0 n ); n     } n   } else { n     pivot = 0; n     for (int i = 1; i < n; i++) n       if (x[i] < x[pivot] || (x[i] == x[pivot] && y[i] < y[pivot])) pivot = i; n     for (int i = 0; i < n - 1; i++) pts[i] = (i < pivot ? i : i + 1); n     sort(pts, pts + n - 1, comp_by_pivot); n     chull[0] = pivot; n     tp = 0; n     for (int i = 0; i < n - 1; i++) { n       int cur = pts[i]; n       while (tp >= 1 && (ccw(chull[tp - 1], chull[tp], cur) == false)) tp--; n       chull[++tp] = cur; n     } n     chull_size = tp + 1; n     int num_change = 0; n     for (int i = 0; i < chull_size; i++) n       if (c[chull[i]] != c[chull[(i + 1) % chull_size]]) num_change++; n     if (num_change == 0) { n       int c_chull = c[chull[0]]; n       int center_pivot = -1; n       for (int i = 0; i < n; i++) n         if (c[i] != c_chull) { n           center_pivot = i; n           break; n         } n       if (center_pivot == -1) { n         printf( %d n , n - 1); n         for (int i = 0; i < n - 1; i++) printf( %d %d n , pivot, pts[i]); n       } else { n         for (int i = 0; i < chull_size - 1; i++) { n           run(center_pivot, chull[i], chull[i + 1], false); n         } n         run(center_pivot, chull[chull_size - 1], chull[0], true); n         print_ans(); n       } n     } else if (num_change == 2) { n       int p1 = -1; n       for (int i = 1; i < chull_size; i++) n         if (c[chull[i - 1]] != c[chull[i]]) { n           p1 = i; n           break; n         } n       assert(p1 >= 1 && p1 < chull_size); n       rotate(chull, chull + p1, chull + chull_size); n       int p2 = -1; n       for (int i = 1; i < chull_size; i++) n         if (c[chull[i - 1]] != c[chull[i]]) { n           p2 = i; n           break; n         } n       for (int i = 0; i < p2 - 1; i++) { n         run(chull[p2], chull[i], chull[i + 1], false); n       } n       for (int i = p2; i < chull_size - 1; i++) { n         run(chull[0], chull[i], chull[i + 1], false); n       } n       print_ans(); n     } else { n       assert(num_change % 2 != 1); n       printf( Impossible n ); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m; n vector<int> p, h, st; n vector<vector<int>> adj; n int gst(int curr, int prev) { n   int res = p[curr]; n   for (int next : adj[curr]) { n     if (next != prev) { n       res += gst(next, curr); n     } n   } n   return st[curr] = res; n } n int dfs(int curr, int prev, int& res) { n   int g = st[curr] + h[curr]; n   if (g % 2) res = 0; n   int gv = g / 2; n   if (gv < 0 || gv > st[curr]) res = 0; n   int cgv = 0; n   for (int next : adj[curr]) { n     if (next != prev) { n       cgv += dfs(next, curr, res); n     } n   } n   if (cgv > gv) res = 0; n   return gv; n } n string solve() { n   cin >> n >> m; n   adj.clear(); n   adj.resize(n + 1); n   p.assign(n + 1, 0); n   h.assign(n + 1, 0); n   st.assign(n + 1, 0); n   for (int i = 1; i <= n; i++) cin >> p[i]; n   for (int i = 1; i <= n; i++) cin >> h[i]; n   for (int i = 0; i < n - 1; i++) { n     int u, v; n     cin >> u >> v; n     adj[u].push_back(v); n     adj[v].push_back(u); n   } n   gst(1, 0); n   int res = 1; n   dfs(1, 0, res); n   if (!res) return  NO ; n   return  YES ; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   int t; n   cin >> t; n   while (t--) { n     cout << solve() <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const double pi = 3.14159265; n const long long mod = 998244353; n const long long N = 1e6 + 5; n const long long st = 1e2; n const int P0 = 437; n const int P = 1003; n int main() { n   cin.sync_with_stdio(); n   cin.tie(0); n   cout.tie(0); n   int q; n   cin >> q; n   while (q--) { n     int n; n     cin >> n; n     vector<int> a(n); n     for (int i = 0; i < (n); i++) cin >> a[i]; n     sort(a.begin(), a.end()); n     (a.resize(unique(a.begin(), a.end()) - a.begin())); n     cout << ((int)(a).size()) <<   n ; n   } n } n


#include<bits/stdc++.h> n using namespace std; n  n int main(){ n  int t; n  cin>>t; n  while(t--){ n   int n; n   cin>>n; n   int o=0,e=0; n   int a; n   for(int i=0;i<2*n;i++){ n    cin>>a; n    if(a%2==0) n     e++; n    else n     o++; n   } n  n   if(o == e) n    cout<< YES <<endl; n   else n    cout<< NO <<endl; n  } n  return 0; n }


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


#include <bits/stdc++.h> n using namespace std; n long long n, m; n long long getans(long long n) { n   if (n == 1) return 3 % m; n   long long k = getans(n / 2); n   k *= k; n   if (n % 2) k *= 3; n   return k % m; n } n int main() { n   while (~scanf( %I64d%I64d , &n, &m)) { n     long long ans = getans(n) - 1; n     if (ans == -1) ans = m - 1; n     printf( %I64d n , ans); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long a[100005]; n deque<long long> q; n long long mi[100005]; n void add_q(long long ae) { n   while (!q.empty() && q.back() > ae) q.pop_back(); n   q.push_back(ae); n } n void pop_q(long long re) { n   if (!q.empty() && q.front() == re) q.pop_front(); n } n void solve() { n   long long n, c, sum = 0; n   cin >> n >> c; n   for (int i = 0; i < n; i++) { n     cin >> a[i]; n     sum += a[i]; n   } n   for (int i = 0; i < c; i++) { n     add_q(a[i]); n   } n   mi[0] = q.front(); n   for (int i = c; i < n; i++) { n     pop_q(a[i - c]); n     add_q(a[i]); n     mi[i - c + 1] = q.front(); n   } n   long long maxc = 0; n   long long res = 0; n   for (int i = 0; i < c; i++) res = max(res, mi[i]); n   for (int i = c; i < n; i++) { n     maxc = max(maxc, mi[i - c]); n     mi[i] += maxc; n     res = max(res, mi[i]); n   } n   cout << sum - res; n } n int main() { n   solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long double PI = 3.141592653589793238462643383; n int vector_init(vector<vector<long long int> > &x, int r, int c) { n   x.resize(r); n   for (int i = 0; i < (r); ++i) x[i].resize(c); n } n int abso(int a) { n   if (a > 0) return a; n   return -a; n } n void print(vector<long long> &ans) { n   for (int i = 0; i < ans.size(); i++) cout << ans[i] <<    ; n   cout <<   n ; n } n int equal(vector<int> &a, vector<int> &b) { n   for (int i = 0; i < a.size(); i++) { n     if (a[i] == b[i] && a[i] == 1) return 1; n   } n   return 0; n } n void print1(vector<vector<long long> > &edge) { n   for (int i = 0; i < edge.size(); i++) print(edge[i]); n } n long long maxx(long long a, long long b) { n   if (a > b) return a; n   return b; n } n long long minn(long long a, long long b) { return a + b - maxx(a, b); } n long long Ceil(long long a, long long b) { n   if (a % b == 0) return a / b; n   return a / b + 1; n } n long long find(vector<long long int> &a, vector<long long int> &c, long long t, n                long long cunt) { n   for (int i = 0; i < (a.size()); ++i) { n     if (a[i] == t) cunt--; n     if (cunt == 0) return c[i]; n   } n } n long long power(long long k, long long m) { n   long long ans = 1; n   for (int i = 0; i < (k); ++i) { n     ans = (ans * 2) % m; n   } n   return ans; n } n int main() { n   cin.sync_with_stdio(0); n   cin.tie(0); n   cin.exceptions(cin.failbit); n   map<long long, long long> start; n   map<long long, long long> end; n   long long n; n   cin >> n; n   vector<long long int> a(n, 0); n   for (int i = 0; i < (n); ++i) { n     cin >> a[i]; n     if (start[a[i]] == 0) start[a[i]] = i + 1; n     end[a[i]] = i + 1; n   } n   long long t = end[a[0]] - 1; n   long long k = 0; n   long long cunt = 0; n   while (k < n) { n     if (k == t + 1) { n       cunt++; n       t = end[a[k]] - 1; n     } else { n       t = maxx(t, end[a[k]] - 1); n     } n     k++; n   } n   cout << power(cunt, 998244353); n } n


#include <bits/stdc++.h> n const int MAX = 1 << 17; n char s[MAX]; n long long rje[2], dp[2][2][2][MAX]; n int main() { n   int i, l, tmpdulj, j, k, ii; n   scanf( %s , s + 1); n   l = strlen(s + 1); n   for (i = 1; i <= l; i++) { n     dp[i % 2][1][s[i] -  a ][i] = 1; n     rje[1]++; n     for (j = 0; j < 2; j++) n       for (k = 0; k < 2; k++) n         for (ii = 0; ii < 2; ii++) { n           tmpdulj = k; n           if (s[i] -  a  != ii) tmpdulj = (tmpdulj + 1) % 2; n           dp[j][tmpdulj][s[i] -  a ][i] += dp[j][k][ii][i - 1]; n           if (tmpdulj % 2) rje[(i - j + 1) % 2] += dp[j][k][ii][i - 1]; n         } n   } n   printf( %I64d %I64d n , rje[0], rje[1]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int inf = 1000000000; n bool cmp(pair<int, int> a, pair<int, int> b) { return a.second < b.second; } n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   int n; n   cin >> n; n   int a[n]; n   for (int i = 0; i < n; i++) cin >> a[i]; n   sort(a, a + n, greater<int>()); n   int ans[n]; n   ans[0] = a[0]; n   ans[1] = a[1]; n   for (int i = 2; i < n; i++) { n     if (i == 2) { n       ans[n - 1] = a[i]; n     } else { n       ans[i - 1] = a[i]; n     } n   } n   bool ok = true; n   if (a[0] >= a[1] + a[2]) { n     ok = false; n   } n   if (ok) { n     cout <<  YES n ; n     for (int i = 0; i < n; i++) cout << ans[i] <<    ; n   } else { n     cout <<  NO n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int n, m; n vector<vector<char>> grid; n pair<int, int> start, endloc; n int memo[1000][1000][4]; n bool dfs(pair<int, int> loc, int turns, int dir) { n   if (loc == endloc) return true; n   memo[loc.first][loc.second][dir] = turns; n   vector<pair<int, int>> next; n   next.push_back({loc.first - 1, loc.second}); n   next.push_back({loc.first + 1, loc.second}); n   next.push_back({loc.first, loc.second - 1}); n   next.push_back({loc.first, loc.second + 1}); n   for (int i = 0; i < 4; i++) { n     pair<int, int> nextLoc = next[i]; n     if (i != dir && turns == 0) continue; n     if (nextLoc.first < 0 || nextLoc.first >= n) continue; n     if (nextLoc.second < 0 || nextLoc.second >= m) continue; n     if (grid[nextLoc.first][nextLoc.second] ==  * ) continue; n     int turnDiff = 0; n     if (i != dir) turnDiff = 1; n     if (memo[nextLoc.first][nextLoc.second][i] >= turns - turnDiff) continue; n     if (dfs(nextLoc, turns - turnDiff, i)) return true; n   } n   return false; n } n int main() { n   ios::sync_with_stdio(0); n   cin >> n >> m; n   memset(memo, -1, sizeof memo); n   grid = vector<vector<char>>(n, vector<char>(m,    )); n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < m; j++) { n       char curr; n       cin >> curr; n       if (curr ==  S ) { n         start = {i, j}; n       } else if (curr ==  T ) { n         endloc = {i, j}; n       } n       grid[i][j] = curr; n     } n   } n   for (int i = 0; i < 4; i++) { n     if (dfs(start, 2, i)) { n       cout <<  YES  << endl; n       return 0; n     } n   } n   cout <<  NO  << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n  n int main() { n     int t; n     cin >> t; n     while (t--) { n         int s; n         cin >> s; n         int k, ans = 0; n  n         for (k = 1; s > 0 && s >= k; k += 2) n             s -= k, ++ans; n         for (k = min(s, k - 1); s > 0 && k >= 1; k--) { n             ans += s / k; n             s -= (s / k) * k; n         } n  n         cout << ans <<   n ; n     } n     return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 100100; n int N; n long long K; n int ar[MAXN]; n int main() { n   cin >> N >> K; n   for (int i = 0; i < N; i++) cin >> ar[i]; n   --K; n   sort(ar, ar + N); n   for (int i = 0; i < N;) { n     int cur = i; n     int cnt = 0; n     while (i < N && ar[i] == ar[cur]) { n       ++i; n       ++cnt; n     } n     if (K < (long long)cnt * N) { n       cout << ar[cur] <<     << ar[(int)(K / cnt)] << endl; n       break; n     } n     K -= (long long)cnt * N; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void fileIO() {} n bool sortbysecdesc(const pair<long long, long long> &a, n                    const pair<long long, long long> &b) { n   return a.second > b.second; n } n int32_t main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   fileIO(); n   long long T; n   T = 1; n   while (T--) { n     long long n; n     cin >> n; n     vector<long long> a(n); n     for (long long &x : a) { n       cin >> x; n     } n     long long moves = 0; n     long long curr = 0; n     for (long long x : a) { n       moves += abs(x - curr); n       curr = x; n     } n     cout << moves <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int Maxn = 300000; n long long n, x[Maxn], w[Maxn], Index[Maxn], nx[Maxn]; n set<pair<long long, long long> > poi, xx; n set<pair<long long, long long> > u; n void input() { n   cin >> n; n   for (int i = 0; i < n; i++) { n     cin >> x[i] >> w[i]; n     xx.insert(make_pair(x[i], i)); n     poi.insert(make_pair(x[i] - w[i], i)); n   } n } n void solve() { n   long long k = 0; n   for (auto c : poi) { n     Index[c.second] = k; n     k++; n   } n   for (auto c : xx) { n     long long ind = c.second; n     poi.erase(make_pair(x[ind] - w[ind], ind)); n     long long sum = x[ind] + w[ind]; n     auto limIt = poi.lower_bound(make_pair(sum, 0)); n     if (limIt != poi.end()) { n       auto lim = *limIt; n       nx[Index[ind]] = Index[lim.second]; n       u.insert(make_pair(Index[lim.second], Index[ind])); n     } else n       nx[Index[ind]] = -1; n   } n   int ans = 1, last = 0; n   while (((int)u.size())) { n     auto c = (*u.begin()).first; n     ans++; n     for (; last < c; last++) n       if (nx[last] >= 0) u.erase(make_pair(nx[last], last)); n   } n   cout << ans << endl; n } n int main() { n   ios::sync_with_stdio(0); n   cin.tie(); n   input(); n   solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int t, a, b, ans; n   scanf( %d , &t); n   while (t--) { n     ans = 0; n     scanf( %d %d , &a, &b); n     while (a > 0 && b > 0) { n       if (a >= b) { n         ans += (a / b); n         a = a % b; n       } else if (b > a) { n         ans += (b / a); n         b = b % a; n       } n     } n     printf( %d n , ans); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int n = 3; n long long r, g, b; n void reset() { n   if (r >= g) swap(r, g); n   if (r >= b) swap(r, b); n   if (g >= b) swap(g, b); n } n int main() { n   while (cin >> r >> g >> b) { n     reset(); n     if ((r + g) * 2 <= b) n       cout << r + g << endl; n     else n       cout << (r + g + b) / 3 << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n using Long = long long; n int main() { n   int t; n   cin >> t; n   while (t--) { n     int d; n     cin >> d; n     if (d * d < 4 * d) { n       puts( N ); n     } else { n       double dis = sqrt(d * d - 4 * d); n       double x = (dis + d) / 2, y = (d - dis) / 2; n       printf( Y %0.10lf %0.10lf n , x, y); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long K, A, B, V; n   cin >> K >> A >> B >> V; n   long long nuts = A; n   long long divisors = B; n   long long capacity = V; n   long long maxSections = K; n   int boxes = 0; n   while (nuts > 0) { n     boxes++; n     int sections = min(divisors + 1, maxSections); n     nuts -= sections * capacity; n     divisors -= sections - 1; n   } n   cout << boxes; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int N, ans[(int)20 + 7][(int)20 + 7], a[500]; n set<pair<int, int> > st; n map<int, int> mp; n int main() { n   ios::sync_with_stdio(false); n   cin >> N; n   for (int i = 0; i < N * N; i++) { n     cin >> a[i]; n     mp[a[i]]++; n   } n   int idx = -1; n   map<int, int>::iterator itt = mp.begin(); n   while (itt != mp.end()) { n     st.insert({itt->second, itt->first}); n     itt++; n   } n   memset(ans, -1, sizeof ans); n   for (int i = 0; i < N; i++) { n     for (int j = 0; j < N; j++) { n       if (ans[i][j] != -1) continue; n       if (!st.size()) return cout <<  NO , 0; n       if (N % 2 && i == N / 2 || j == N / 2) { n         continue; n       } n       set<pair<int, int> >::iterator it = st.lower_bound({4, -1}); n       ; n       int c = (*it).first, n = (*it).second; n       if (it == st.end()) return cout <<  NO , 0; n       st.erase(it); n       c -= 4; n       if (c < 0) return cout <<  NO , 0; n       ans[i][j] = n; n       ans[i][N - j - 1] = n; n       ans[N - i - 1][j] = n; n       ans[N - i - 1][N - j - 1] = n; n       if (c) st.insert({c, n}); n     } n   } n   for (int i = 0; i < N; i++) { n     for (int j = 0; j < N; j++) { n       if (ans[i][j] != -1) continue; n       if (N % 2 && i == N / 2 && j == N / 2) continue; n       if (N % 2 && i == N / 2) { n         set<pair<int, int> >::iterator it = st.lower_bound({2, -1}); n         if (it == st.end()) return cout <<  NO , 0; n         st.erase(it); n         int c = (*it).first, n = (*it).second; n         c -= 2; n         if (c < 0) return cout <<  NO , 0; n         ans[i][j] = n; n         ans[i][N - j - 1] = n; n         if (c) st.insert({c, n}); n       } else if (N % 2 && j == N / 2) { n         set<pair<int, int> >::iterator it = st.lower_bound({2, -1}); n         ; n         if (it == st.end()) return cout <<  NO , 0; n         st.erase(it); n         int c = (*it).first, n = (*it).second; n         c -= 2; n         if (c < 0) return cout <<  NO , 0; n         ans[i][j] = n; n         ans[N - i - 1][j] = n; n         if (c) st.insert({c, n}); n       } n     } n   } n   if (N % 2) { n     ans[N / 2][N / 2] = (*st.begin()).second; n   } n   cout <<  YES  << endl; n   for (int i = 0; i < N; i++, cout << endl) n     for (int j = 0; j < N; j++) cout << ans[i][j] <<    ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long M = 1e9 + 7; n void flashSpeed() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n } n const long long N = 2e5 + 5; n long long fact[N]; n double Round(double var) { n   float value = (long long)(var * 100 + .5); n   return (float)value / 100; n } n long long ceils(long long x, long long y) { return x / y + ((x % y) != 0); } n long long Gcd(long long a, long long b) { n   if (b > a) { n     return Gcd(b, a); n   } n   if (b == 0) n     return a; n   else n     return Gcd(b, a % b); n } n long long lcm(long long a, long long b) { return a / Gcd(a, b) * b; } n bool isPal(string s) { n   for (long long i = 0; i < (long long)s.size() / 2; i++) { n     if (s[i] != s[(long long)s.size() - 1 - i]) return false; n   } n   return true; n } n long long Sumdigits(long long a) { n   long long total = 0; n   while (a) { n     total += a % 10; n     a /= 10; n   } n   return total; n } n bool isPerfectSquare(long long n) { n   for (long long i = 1; i * i <= n; i++) { n     if ((n % i == 0) && (n / i == i)) { n       return true; n     } n   } n   return false; n } n bool isPowerOfTwo(long long n) { return (ceil(log2(n)) == floor(log2(n))); } n void lexosmaintest(string s, string c) { n   string t1 = s; n   sort(t1.begin(), t1.end()); n   long long index = -1; n   for (long long i = 0; i < s.length(); i++) { n     if (s[i] != t1[i]) { n       index = i; n       break; n     } n   } n   long long j; n   for (long long i = 0; i < s.length(); i++) { n     if (s[i] == t1[index]) j = i; n   } n   swap(s[index], s[j]); n } n const long long NN = 1e4 + 5; n long long primes[NN]; n vector<long long> pr; n void sieve() { n   for (long long i = 2; i < NN; i++) { n     if (primes[i] == 0) { n       pr.push_back(i); n       for (long long j = i * i; j < NN; j += i) { n         primes[j] = 1; n       } n     } n     primes[i] ^= 1; n   } n } n const long long mod2 = 998244353; n long long powerr(long long x, long long y, long long p = mod2) { n   long long res = 1; n   x = x % p; n   if (x == 0) return 0; n   while (y > 0) { n     if (y & 1) res = (res * x) % p; n     y = y >> 1; n     x = (x * x) % p; n   } n   return res; n } n long long iAmUseless(long long n) {} n const double PI = acos(-1); n long long lefty[N]; n long long righty[N]; n void chaloTryKrteHai() { n   long long a, b, c; n   cin >> a >> b >> c; n   long long m; n   cin >> m; n   long long second = a + b + c; n   if (m > second - 3LL) { n     cout <<  NO  << endl; n   } else { n     long long maxi = max({a, b, c}); n     second -= maxi; n     if (maxi - 1 > second) { n       if (m < (maxi - second) - 1) { n         cout <<  NO  << endl; n       } else { n         cout <<  YES  << endl; n       } n     } else { n       cout <<  YES  << endl; n     } n   } n } n int32_t main() { n   flashSpeed(); n   sieve(); n   long long t = 1; n   cin >> t; n   while (t--) { n     chaloTryKrteHai(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long MOD = 1e9 + 7; n int32_t main() { n   std::ios::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   ; n   long long t; n   t = 1; n   cin >> t; n   for (long long tc = 0; tc < t; tc++) { n     string str; n     cin >> str; n     long long cnt_ab = 0, cnt_ba = 0; n     for (long long i = 0; i + 1 < (long long)str.size(); i++) { n       cnt_ab += (str[i] ==  a  && str[i + 1] ==  b ); n       cnt_ba += (str[i] ==  b  && str[i + 1] ==  a ); n     } n     if (cnt_ab == cnt_ba) { n       cout << str <<   n ; n       continue; n     } n     if (cnt_ab > cnt_ba) { n       for (long long i = 0; i + 1 < (long long)str.size(); i++) { n         if (str[i] ==  a ) { n           str[i] =  b ; n           break; n         } n       } n     } else { n       for (long long i = 0; i + 1 < (long long)str.size(); i++) { n         if (str[i] ==  b ) { n           str[i] =  a ; n           break; n         } n       } n     } n     cout << str <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long MAXN = 9223372036854775807, mod = 998244353; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   long long T = 1; n   for (long long t = 0; t < T; t++) { n     long long n, s1, s2, p1, p2; n     cin >> n >> s1 >> s2 >> p1 >> p2; n     long long time1 = s1 * n + 2 * p1; n     long long time2 = s2 * n + 2 * p2; n     if (time1 < time2) n       cout <<  First ; n     else if (time2 < time1) n       cout <<  Second ; n     else n       cout <<  Friendship ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int N; n int A[100005]; n int main(int argc, char *argv[]) { n   scanf( %d , &N); n   for (int i = (1), _n = (N); i <= _n; i++) scanf( %d , &A[i]); n   int hi = -1; n   for (int i = (1), _n = (N); i <= _n; i++) hi = max(hi, A[i]); n   bool done[100005] = {false}; n   int ans = 1; n   for (int i = (1), _n = (N); i <= _n; i++) n     if (A[i] == hi && done[i] == false) { n       int tans = 0; n       for (int j = (i), _n = (N); j <= _n; j++) n         if (A[j] == hi) n           done[j] = true, tans++; n         else n           break; n       ans = max(ans, tans); n     } n   printf( %d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(nullptr); n   cout.tie(nullptr); n   int B, K; n   cin >> B >> K; n   vector<int> a(K); n   for (int i = 0; i < K; ++i) cin >> a[i]; n   int odd = 0; n   bool flag = (B % 2 ? 1 : 0); n   for (int i = 0; i < K - 1; ++i) { n     odd += (a[i] % 2 && flag); n   } n   odd += (a.back() % 2); n   cout << (odd % 2 ?  odd  :  even ) <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n const double PI = acos(-1); n using namespace std; n const int mod = 1e9 + 7; n const long long oo = 0x3f3f3f3f; n const int nn = 1e2 + 15; n vector<pair<int, int>> gr[nn], shifts; n map<char, int> vis; n long long get_distance(pair<int, int> a, pair<int, int> b) { n   return ((a.first - b.first) * 1LL * (a.first - b.first) + n           (a.second - b.second) * (a.second - b.second)); n } n int main() { n   ios_base::sync_with_stdio(false); n   cout.tie(NULL); n   cin.tie(NULL); n   ; n   int n, m, x; n   cin >> n >> m >> x; n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < m; j++) { n       char c; n       cin >> c; n       if (c ==  S ) n         shifts.push_back(make_pair(i, j)); n       else n         gr[c -  a ].push_back(make_pair(i, j)); n     } n   } n   int len; n   cin >> len; n   string s; n   cin >> s; n   int res = 0; n   for (int i = 0; i < len; i++) { n     if (isupper(s[i])) { n       s[i] = tolower(s[i]); n       if (!((int)((gr[s[i] -  a ]).size())) || !((int)((shifts).size()))) n         return cout << -1, 0; n       else { n         if (vis[s[i]]) { n           if (vis[s[i]] > x * x) res++; n           continue; n         } n         int mn = oo; n         for (int j = 0; j < ((int)((shifts).size())); j++) { n           for (int k = 0; k < ((int)((gr[s[i] -  a ]).size())); k++) { n             int dist = get_distance(shifts[j], gr[s[i] -  a ][k]); n             mn = min(mn, dist); n           } n         } n         if (mn > x * x) res++; n         vis[s[i]] = mn; n       } n     } else { n       if (!((int)((gr[s[i] -  a ]).size()))) return cout << -1, 0; n     } n   } n   cout << res; n } n


#include <bits/stdc++.h> n using namespace std; n long long solve() { n   pair<long long, long long> init, final; n   cin >> init.first >> init.second >> final.first >> final.second; n   long long n; n   cin >> n; n   string s; n   cin >> s; n   pair<long long, long long> pre[n], cur = {0, 0}; n   map<char, pair<long long, long long> > dir; n   dir[ L ] = make_pair(-1, 0); n   dir[ R ] = make_pair(1, 0); n   dir[ U ] = make_pair(0, 1); n   dir[ D ] = make_pair(0, -1); n   for (long long i = 0; i < n; ++i) n     cur.first += dir[s[i]].first, cur.second += dir[s[i]].second, pre[i] = cur; n   long long l = 0, r = 1e18; n   while (r - l > 1) { n     long long mid = (r + l) / 2; n     long long x = init.first + (mid / n) * pre[n - 1].first, n               y = init.second + (mid / n) * pre[n - 1].second; n     if (mid % n) x += (pre[mid % n - 1].first), y += (pre[mid % n - 1].second); n     if (abs(x - final.first) + abs(y - final.second) <= mid) n       r = mid; n     else n       l = mid; n   } n   if (r > 1e17) r = -1; n   cout << r <<   n ; n   return 0; n } n signed main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n inline void FAST_IO() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n } n int main() { n   FAST_IO(); n   int n, m; n   cin >> n >> m; n   pair<int, int> ac{0, 0}, bc{0, 0}; n   for (int i = 0; i < n; ++i) { n     int a; n     cin >> a; n     if (a % 2) n       ac.first++; n     else n       ac.second++; n   } n   for (int i = 0; i < m; ++i) { n     int a; n     cin >> a; n     if (a % 2) n       bc.first++; n     else n       bc.second++; n   } n   cout << min(ac.first, bc.second) + min(ac.second, bc.first) << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 210000, K = 60; n int n, k, p; n int a[N], sum[N], f[N][K]; n int main() { n   scanf( %d%d%d , &n, &k, &p); n   for (int i = 1; i <= n; i++) n     scanf( %d , &a[i]), sum[i] = (sum[i - 1] + a[i] % p) % p; n   memset(f, -0x3f, sizeof(f)); n   f[0][0] = 0; n   for (int i = 1; i <= n; i++) { n     for (int j = 1; j <= k; j++) { n       for (int o = 0; o <= p; o++) { n         f[sum[i]][j] = max(f[sum[i]][j], f[o][j - 1] + (sum[i] - o + p) % p); n       } n     } n   } n   printf( %d n , f[sum[n]][k]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 500005; n int n, m, u[MAXN], v[MAXN]; n bool cho[MAXN], sel[MAXN]; n void solve() { n   int ans = 0; n   scanf( %d%d , &n, &m); n   for (int i = 1; i <= m; i++) { n     scanf( %d%d , &u[i], &v[i]); n     if (!cho[u[i]] && !cho[v[i]]) { n       sel[i] = 1; n       ++ans; n       cho[u[i]] = cho[v[i]] = 1; n     } n   } n   if (ans >= n) { n     printf( Matching n ); n     int cnt = 0; n     for (int i = 1; i <= m && cnt < n; i++) n       if (sel[i]) { n         printf( %d  , i); n         ++cnt; n       } n     printf(  n ); n   } else { n     printf( IndSet n ); n     int cnt = 0; n     for (int i = 1; i <= 3 * n && cnt < n; i++) n       if (!cho[i]) { n         printf( %d  , i); n         ++cnt; n       } n     printf(  n ); n   } n   for (int i = 1; i <= 3 * n; i++) cho[i] = 0; n   for (int i = 1; i <= m; i++) sel[i] = 0; n } n int main() { n   int T; n   scanf( %d , &T); n   while (T--) solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long double pi = 3.1415926535897932384626433832795l; n template <typename T> n inline auto sqr(T x) -> decltype(x * x) { n   return x * x; n } n template <typename T1, typename T2> n inline bool umx(T1& a, T2 b) { n   if (a < b) { n     a = b; n     return 1; n   } n   return 0; n } n template <typename T1, typename T2> n inline bool umn(T1& a, T2 b) { n   if (b < a) { n     a = b; n     return 1; n   } n   return 0; n } n const int N = 1000100; n int p[N]; n bool use[N]; n int main() { n   std::ios::sync_with_stdio(false); n   cout.setf(ios::showpoint | ios::fixed); n   cout.precision(20); n   int n; n   cin >> n; n   for (int i = int(0); i < int(n); ++i) { n     cin >> p[i]; n     p[i]--; n   } n   int res = n; n   for (int i = int(0); i < int(n); ++i) { n     if (use[i]) continue; n     int cur = i; n     while (!use[cur]) { n       use[cur] = 1; n       cur = p[cur]; n     } n     res--; n   } n   if (((n & 1) ^ (res & 1)) == 0) { n     cout <<  Petr ; n   } else { n     cout <<  Um_nik ; n   } n   return 0; n } n


#include <bits/stdc++.h> n struct ele { n   int y, st; n } tmp; n std::vector<ele> edge[100100]; n std::vector<int> gr[100100]; n bool vi[100100]; n int co[100100], N; n int flag, sp[100100]; n void dfs(int x) { n   int c, t; n   for (int k = 0; k < edge[x].size(); k++) { n     c = edge[x][k].y; n     t = edge[x][k].st; n     if (c == 1) continue; n     if (!vi[c]) { n       vi[c] = true; n       if (t == 0) n         co[c] = 1 + co[x] % 2; n       else n         co[c] = co[x]; n       gr[N].push_back(c); n       dfs(c); n     } else { n       if ((co[x] == co[c] && t == 0) || (co[x] != co[c] && t == 1)) { n         flag = 0; n         return; n       } n     } n   } n   return; n } n int main() { n   int k, n, m, t, a, b, c, d, i; n   scanf( %d%d , &n, &m); n   flag = 1; n   memset(sp, -1, sizeof(sp)); n   for (k = 1; k <= m; k++) { n     scanf( %d%d%d , &a, &b, &t); n     edge[a].push_back(ele{b, t}); n     edge[b].push_back(ele{a, t}); n     if (a == 1) sp[b] = t + 1; n     if (b == 1) sp[a] = t + 1; n   } n   for (k = 2; k <= n; k++) { n     if (!vi[k]) { n       N++; n       gr[N].push_back(k); n       vi[k] = 1; n       co[k] = 1; n       dfs(k); n     } n   } n   long long ans = 1, MOD = 1000000007, tm, s; n   if (flag == 0) { n     printf( 0 n ); n     return 0; n   } n   for (k = 1; k <= N; k++) { n     tm = 2; n     s = -1; n     for (i = 0; i < gr[k].size(); i++) { n       c = gr[k][i]; n       if (sp[c] == -1) continue; n       if (s == -1) { n         if (sp[c] == co[c]) n           s = 0; n         else n           s = 1; n         tm = 1; n       } else { n         if (sp[c] == co[c] && s == 1) tm = 0; n         if (sp[c] != co[c] && s == 0) tm = 0; n       } n     } n     ans *= tm; n     ans %= MOD; n   } n   printf( %I64d , ans); n } n