#include <bits/stdc++.h> n using namespace std; n int w, h; n char g[100][100], d[100][100]; n bool use[100][100]; n int dx[] = {-1, 0, 1, 0}; n int dy[] = {0, -1, 0, 1}; n bool is_valid(int y, int x) { return 0 <= y && y < h && 0 <= x && x < w; } n bool check() { n   for (int i = 0; i < h; i++) { n     for (int j = 0; j < w; j++) { n       if (g[i][j] ==  B  && !use[i][j]) return true; n     } n   } n   return false; n } n void dfs(int y, int x, int dir, bool can) { n   if (!is_valid(y, x)) return; n   if (g[y][x] ==  W ) return; n   use[y][x] = true; n   if (!can) n     dfs(y + dy[dir], x + dx[dir], dir, can); n   else { n     dfs(y + dy[dir], x + dx[dir], dir, can); n     for (int i = 0; i < 4; i++) { n       if (i == dir) continue; n       dfs(y + dy[i], x + dx[i], i, false); n     } n   } n   return; n } n int main() { n   cin >> h >> w; n   for (int i = 0; i < h; i++) { n     for (int j = 0; j < w; j++) { n       cin >> g[i][j]; n       d[i][j] = g[i][j]; n     } n   } n   for (int i = 0; i < h; i++) { n     for (int j = 0; j < w; j++) { n       memset(use, 0, sizeof(use)); n       if (g[i][j] ==  W ) continue; n       for (int k = 0; k < 4; k++) { n         dfs(i, j, k, true); n       } n       if (check()) { n         cout <<  NO  << endl; n         return 0; n       } n     } n   } n   cout <<  YES  << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n constexpr int MAX_N = 5000; n constexpr int INF = numeric_limits<int>::max(); n int N; n int H[MAX_N + 1]; n int DP[MAX_N + 1][MAX_N + 1]; n void print(int* ptr, int n) { n   printf( [  ); n   while (n > 0) { n     printf( %d  , *ptr); n     ++ptr; n     --n; n   } n   printf( ] n ); n } n int main() { n   cin >> N; n   if (N < 2) { n     cout << 0 << endl; n     return 0; n   } n   for (int i = 0; i < N; i++) { n     cin >> H[i]; n   } n   DP[0][0] = H[0]; n   for (int i = 1; i < N; i++) { n     if (DP[0][i - 1] == -1) { n       DP[0][i] = -1; n     } else { n       DP[0][i] = (H[i] >= H[i - 1]) ? H[i] : -1; n     } n   } n   if (DP[0][N - 1] != -1) { n     cout << 0 << endl; n     return 0; n   } n   for (int n = 1; n < N; n++) { n     DP[n][0] = H[0]; n     for (int i = 1; i < N; i++) { n       DP[n][i] = INF; n       if (DP[n - 1][i] != -1) { n         DP[n][i] = min(DP[n][i], DP[n - 1][i]); n       } n       if (DP[n][i - 1] != -1 && DP[n][i - 1] <= H[i]) { n         DP[n][i] = min(DP[n][i], H[i]); n       } n       if (DP[n - 1][i - 1] != -1) { n         DP[n][i] = min(DP[n][i], H[i] + DP[n - 1][i - 1]); n       } n       int S = H[i]; n       for (int m = n - 1; m >= 0; m--) { n         int j = i - (n - m); n         S += H[j]; n         if (j < 0 || (j > 0 && DP[m][j - 1] == -1)) break; n         if ((j == 0) || S >= DP[m][j - 1]) { n           DP[n][i] = min(DP[n][i], S); n           break; n         } n       } n     } n     for (int j = 1; j < N; j++) { n       DP[n][j] = (DP[n][j] == INF) ? -1 : DP[n][j]; n     } n     if (DP[n][N - 1] != -1) { n       cout << n << endl; n       return 0; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n #pragma GCC optimize( O3 ) n const double PI = acos(-1.0); n using namespace std; n const double EPS = 1e-9; n const int N = 5e5 + 9; n int n, mem[3009][2 * 3009], nxt[3009]; n pair<int, int> a[3009]; n bool com(pair<int, int> a, pair<int, int> b) { n   if (a.first < b.first) n     return 1; n   else if (a.first == b.first) { n     if (a.second > b.second) n       return 1; n     else n       return 0; n   } else n     return 0; n } n int dp(int l, int mx) { n   if (l > n || a[l].first > mx) return 0; n   int& ret = mem[l][mx]; n   if (~ret) return ret; n   ret = dp(l + 1, mx); n   if (a[l].second <= mx) n     ret = max(ret, 1 + dp(l + 1, a[l].second) + dp(nxt[l], mx)); n   return ret; n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   int t; n   cin >> t; n   while (t--) { n     cin >> n; n     map<int, int> mp; n     for (int i = 1; i <= n; i++) { n       cin >> a[i].first >> a[i].second; n       mp[a[i].first]; n       mp[a[i].second]; n     } n     int id = 0; n     for (auto& p : mp) p.second = ++id; n     for (int i = 1; i <= n; i++) { n       a[i].first = mp[a[i].first]; n       a[i].second = mp[a[i].second]; n     } n     sort(a + 1, a + 1 + n, com); n     for (int i = 1; i <= n; i++) { n       nxt[i] = n + 1; n       for (int j = i + 1; j <= n; j++) { n         if (a[j].first > a[i].second) { n           nxt[i] = j; n           break; n         } n       } n     } n     for (int i = 0; i <= n + 2; i++) n       for (int j = 0; j <= n * 2 + 9; j++) mem[i][j] = -1; n     cout << dp(1, n * 2 + 2) << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int dis[10001][1001]; n int pos[10001]; n int g, r, n, m; n void bfs() { n   deque<pair<int, pair<int, int> > > fila; n   fila.push_front({0, {0, 0}}); n   while (!fila.empty()) { n     int v = fila.front().first; n     int s = fila.front().second.first; n     int t = fila.front().second.second; n     fila.pop_front(); n     if (dis[v][s] != -1) continue; n     dis[v][s] = t; n     if (v) { n       if (s < g and s + (pos[v] - pos[v - 1]) <= g and n           dis[v - 1][s + (pos[v] - pos[v - 1])] == -1) { n         fila.push_front({v - 1, {s + (pos[v] - pos[v - 1]), t}}); n       } n     } n     if (v < m - 1) { n       if (s < g and s + (pos[v + 1] - pos[v]) <= g and n           dis[v + 1][s + (pos[v + 1] - pos[v])] == -1) { n         fila.push_front({v + 1, {s + (pos[v + 1] - pos[v]), t}}); n       } n     } n     if (s == g and dis[v][0] == -1) { n       fila.push_back({v, {0, t + 1}}); n     } n   } n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   memset(dis, -1, sizeof(dis)); n   cin >> n >> m; n   int l[m]; n   for (int i = 0; i < m; i++) cin >> l[i]; n   sort(l, l + m); n   for (int i = 0; i < m; i++) pos[i] = l[i]; n   cin >> g >> r; n   bfs(); n   int res = -1; n   for (int i = 0; i < m - 1; i++) { n     if (dis[i][0] == -1) continue; n     if ((pos[m - 1] - pos[i]) > g) continue; n     int atual = dis[i][0] * (g + r) + (pos[m - 1] - pos[i]); n     if (res == -1) n       res = atual; n     else n       res = min(res, atual); n   } n   cout << res << endl; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 8e4 + 10; n const long long mod = 998244353, modg = 3, modi = 332748118; n int a[maxn], b[maxn]; n int pow_mod(int a, int b) { n   int ans = 1; n   while (b) { n     if (b & 1) ans = (long long)ans * a % mod; n     a = (long long)a * a % mod, b >>= 1; n   } n   return ans; n } n int add(int a, int b) { n   a += b; n   if (a > mod) return a - mod; n   return a; n } n int sub(int a, int b) { n   a -= b; n   if (a < 0) return a + mod; n   return a; n } n class nubmer { n  public: n   int rev[maxn], len, pw; n   long long wt[2][maxn]; n   void init_0() { n     int len = 1; n     while (len <= maxn / 2) len <<= 1; n     for (int mid = 1; mid < len; mid <<= 1) { n       long long wn1 = pow_mod(modg, (mod - 1) / (mid << 1)); n       long long wn2 = pow_mod(modi, (mod - 1) / (mid << 1)); n       wt[0][mid] = wt[1][mid] = 1; n       long long wt1 = wn1, wt2 = wn2; n       for (int j = 1; j <= mid; ++j) { n         wt[0][mid + j] = wt1; n         wt1 = wt1 * wn1 % mod; n         wt[1][mid + j] = wt2; n         wt2 = wt2 * wn2 % mod; n       } n     } n   } n   void init(int n) { n     len = 1; n     pw = 0; n     while (len <= n) len <<= 1, pw++; n     for (int i = 0; i <= len - 1; ++i) n       rev[i] = (rev[i >> 1] >> 1) | ((i & 1) << (pw - 1)); n   } n   void transform(int *a, int flag) { n     int f = flag == 1 ? 0 : 1; n     for (int i = 0; i <= len - 1; ++i) n       if (i < rev[i]) swap(a[i], a[rev[i]]); n     for (int mid = 1; mid < len; mid <<= 1) { n       for (int r = mid << 1, j = 0; j < len; j += r) { n         long long *p = wt[f] + mid; n         int *p1 = a + j, *p2 = a + j + mid; n         for (int k = 0; k < mid; ++k, ++p, ++p1, ++p2) { n           int x = *p1, y = (*p) * (*p2) % mod; n           *p1 = add(x, y), *p2 = sub(x, y); n         } n       } n     } n     if (flag == -1) { n       long long inv = pow_mod(len, mod - 2); n       for (int i = 0; i <= len; i++) { n         a[i] = a[i] * inv % mod; n         if (a[i] < 0) a[i] += mod; n       } n     } n   } n } ntt; n int n, k; n int s1[maxn], s2[maxn]; n vector<int> solve(int l, int r) { n   if (l == r) { n     return vector<int>{1, s2[l]}; n   } n   int mid = (l + r) >> 1; n   vector<int> x = solve(l, mid), y = solve(mid + 1, r); n   ntt.init(x.size() + y.size()); n   for (int i = 0; i <= ntt.len + 5; i++) a[i] = 0, b[i] = 0; n   for (int i = 0; i < x.size(); i++) a[i] = x[i]; n   for (int i = 0; i < y.size(); i++) b[i] = y[i]; n   ntt.transform(a, 1); n   ntt.transform(b, 1); n   for (int i = 0; i < ntt.len; i++) a[i] = (long long)a[i] * b[i] % mod; n   ntt.transform(a, -1); n   int len = x.size() + y.size(); n   int mi = min(ntt.len, min(k + 1, len)); n   vector<int> ans(mi); n   for (int i = 0; i < mi; i++) { n     ans[i] = a[i]; n   } n   return ans; n } n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   ntt.init_0(); n   cin >> n >> k; n   for (int i = 1; i <= n; i++) { n     cin >> s1[i]; n   } n   int T; n   cin >> T; n   vector<int> tmp; n   while (T--) { n     int flag; n     cin >> flag; n     if (flag == 1) { n       int q, i, d; n       cin >> q >> i >> d; n       for (int j = 1; j <= n; j++) s2[j] = sub(q, s1[j]); n       s2[i] = sub(q, d); n       tmp = solve(1, n); n       cout << tmp[k] <<   n ; n     } else { n       int q, l, r, d; n       cin >> q >> l >> r >> d; n       for (int j = 1; j <= n; j++) { n         if (j >= l && j <= r) n           s2[j] = sub(sub(q, s1[j]), d); n         else n           s2[j] = sub(q, s1[j]); n       } n       tmp = solve(1, n); n       cout << tmp[k] <<   n ; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n #pragma comment(linker,  /STACK:256000000 ) n using namespace std; n inline void in(int &x) { scanf( %d , &x); } n inline void in(int &x, int &y) { scanf( %d %d , &x, &y); } n inline void in(int &x, int &y, int &z) { scanf( %d %d , &x, &y, &z); } n inline void out(int x) { printf( %d  , x); } n inline void outln(int x) { printf( %d n , x); } n inline void outln(int x, int y) { printf( %d %d n , x, y); } n inline int _abs(int x) { return x > 0 ? x : -x; } n const int SIZE = (int)1e7 + 10; n int n, m; n int a[SIZE], b[SIZE], minp[SIZE]; n int pa[SIZE], pb[SIZE]; n void precalc() { n   for (int i = 2; i * i < SIZE; ++i) { n     if (!minp[i]) { n       for (int j = i * i; j < SIZE; j += i) { n         if (!minp[j]) minp[j] = i; n       } n     } n   } n   for (int i = 2; i < SIZE; ++i) n     if (!minp[i]) minp[i] = i; n } n void factor(int *mas, int val) { n   for (; val != 1; val /= minp[val]) { n     ++mas[minp[val]]; n   } n } n void output(int *a, int *pa, int size) { n   for (int i = 0; i < (int)(size); i++) { n     int val = a[i], res = 1; n     for (; val != 1; val /= minp[val]) { n       if (pa[minp[val]]) n         --pa[minp[val]]; n       else n         res *= minp[val]; n     } n     out(res); n   } n } n int main() { n   precalc(); n   in(n, m); n   for (int i = 0; i < (int)(n); i++) { n     in(a[i]); n     factor(pa, a[i]); n   } n   for (int i = 0; i < (int)(m); i++) { n     in(b[i]); n     factor(pb, b[i]); n   } n   for (int i = 2; i < SIZE; ++i) { n     pa[i] = pb[i] = min(pa[i], pb[i]); n   } n   outln(n, m); n   output(a, pa, n); n   printf(  n ); n   output(b, pb, m); n   return 0; n } n


#include <bits/stdc++.h> n const int N = 1000005; n std::vector<int> e[N]; n int a[N], n, m, x, y, r[N], c[N], id[N], cnt, cnt2, s[N], vis[N]; n void dfs(int x) { n   if (vis[x]) return; n   vis[x] = 1; n   for (auto u : e[x]) { n     dfs(u); n     a[x] |= a[u]; n   } n } n int main() { n   scanf( %d%d , &n, &m); n   for (int i = 1; i <= m; i++) { n     scanf( %d%d , &x, &y); n     e[x].push_back(y); n     r[y]++; n     c[x]++; n   } n   for (int i = 1; i <= n; i++) n     if (!c[i]) a[i] = 1 << cnt, id[i] = cnt++; n   for (int i = 1; i <= n; i++) n     if (!r[i]) { n       dfs(i); n       s[cnt2++] = a[i]; n     } n   for (int i = 1; i < (1 << cnt) - 1; i++) { n     int t = 0, S = 0, t2 = 0; n     for (int j = 0; j < cnt; j++) n       if (i & (1 << j)) t++, S |= s[j]; n     for (int j = 0; j < cnt; j++) n       if (S & (1 << j)) t2++; n     if (t2 <= t) { n       puts( NO ); n       return 0; n     } n   } n   puts( YES ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long A, B, n; n   cin >> A >> B >> n; n   long long i, j; n   if (A == 0) { n     if (B == 0) { n       cout << 0; n     } else n       cout <<  No solution ; n     return 0; n   } n   if (B % A) { n     cout <<  No solution ; n     return 0; n   } n   long long rez = B / A; n   if (rez == 1) { n     cout << 1; n     return 0; n   } n   if (B == 0) { n     cout << 0; n     return 0; n   } n   for (long long i = -1002; i <= 1002; i++) { n     long long cur = i; n     for (j = 2; j <= n; j++) { n       cur *= i; n       if (abs(cur) > 1000000000) break; n     } n     if (cur == rez) { n       cout << i; n       return 0; n     } n   } n   cout <<  No solution ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void geth(int in, int& out) { n   cout << in << endl; n   cin >> out; n   if (out != 1 && out != -1) { n     exit(0); n   } n } n int main() { n   ios_base::sync_with_stdio(false); n   int n, mx; n   cin >> mx >> n; n   std::vector<int> pp(n); n   for (auto&& x : pp) { n     geth(1, x); n   } n   int l = 2; n   int r = mx; n   int tr = 0; n   while (l < r) { n     int mid = (l + r) / 2, res; n     geth(mid, res); n     res *= pp[tr]; n     if (res > 0) { n       l = mid + 1; n     } else if (res < 0) { n       r = mid - 1; n     } n     tr = (tr + 1) % n; n   } n   geth(l, r); n } n


#include <bits/stdc++.h> n using namespace std; n map<string, int> sz = { n     { S , 0}, { M , 1}, { L , 2}, { XL , 3}, { XXL , 4}, n }; n string st[5] = { S ,  M ,  L ,  XL ,  XXL }; n int a[5]; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(nullptr); n   cout.tie(nullptr); n   cerr.tie(nullptr); n   for (int i = 0; i < 5; i++) cin >> a[i]; n   int n; n   cin >> n; n   while (n--) { n     string s; n     cin >> s; n     int x = sz[s]; n     int bi = -1, bv = 123123123; n     for (int i = 0; i < 5; i++) { n       if (!a[i]) continue; n       int v = 2 * abs(i - x) + (i < x); n       if (v < bv) { n         bv = v; n         bi = i; n       } n     } n     a[bi]--; n     cout << st[bi] <<   n ; n   } n } n


#include<bits/stdc++.h> n using namespace std; n  n const int N = 3e5 + 9; n const long long inf = 2e18; n  n int a[N], c[N], n, m; n long long s1[N], s2[N]; n  n long long f(int mid) { n   for (int i = mid; i <= n; i++) { n     if (a[i] < i - mid + 1) return 0; n   } n   return 1; n } n int32_t main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   int t; cin >> t; n   while (t--) { n     cin >> n >> m; n     for (int i = 1; i <= n; i++) { n       cin >> a[i]; n     } n     for (int i = 1; i <= m; i++) { n       cin >> c[i]; n       s2[i] = s2[i - 1] + c[i]; n     } n     sort(a + 1, a + n + 1); n     for (int i = 1; i <= n; i++) { n       s1[i] = s1[i - 1] + c[a[i]]; n     } n     int l = 1, r = n; int z = n + 1; n     while (l <= r) { n       int mid = l + r >> 1; n       if (f(mid)) { n         z = mid; r = mid - 1; n       } n       else { n         l = mid + 1; n       } n     } n     long long ans = inf; n     for (int i = z; i <= n + 1; i++) { n       ans = min(ans, s1[i - 1] + s2[n - i + 1]); n     } n     cout << ans <<   n ; n   } n   return 0; n }


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   cin >> n; n   while (n--) { n     unsigned long long a, b, c; n     cin >> a >> b >> c; n     unsigned long long d; n     d = (a * a + (b + c) * (b + c)); n     d = sqrt(d) - 1; n     cout << d <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(NULL); n   cout.tie(NULL); n   ; n   unsigned long long x, y; n   cin >> x >> y; n   if (x < y) { n     return cout << -1, 0; n   } n   if ((x - y) & 1) { n     return cout << -1, 0; n   } n   long long z = (x - y) / 2; n   if (y & z) { n     cout << -1; n     return 0; n   } n   long long a = z; n   long long b = x - z; n   if (a > b) swap(a, b); n   cout << a <<     << b <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n int N, type[100005]; n int Tm, in[100005], out[100005]; n int par[100005][20]; n bool comp[100005][20][2]; n vector<int> G[100005]; n void dfs(int nd) { n   in[nd] = ++Tm; n   for (int i = 0; i < G[nd].size(); i++) { n     int ch = G[nd][i]; n     if (ch == par[nd][0]) continue; n     comp[ch][0][type[ch]] = 1; n     dfs(ch); n   } n   out[nd] = ++Tm; n } n int LCA(int u, int v) { n   if ((in[u] <= in[v] && out[u] >= out[v])) return u; n   for (int i = 20 - 1; i >= 0; i--) n     if (!(in[par[u][i]] <= in[v] && out[par[u][i]] >= out[v])) u = par[u][i]; n   return par[u][0]; n } n bool get(int u, int lca, int fl) { n   if ((in[u] <= in[lca] && out[u] >= out[lca])) return false; n   bool x = true; n   for (int i = 20 - 1; i >= 0; i--) n     if (!(in[par[u][i]] <= in[lca] && out[par[u][i]] >= out[lca])) { n       x = x && comp[u][i][fl]; n       u = par[u][i]; n     } n   x = x && comp[u][0][fl]; n   return x; n } n int main() { n   scanf( %d , &N); n   for (int i = 1; i <= N; i++) { n     scanf( %d , &par[i][0]); n     scanf( %d , &type[i]); n     if (par[i][0] != -1) G[par[i][0]].push_back(i); n   } n   for (int i = 1; i <= N; i++) n     if (par[i][0] == -1) { n       par[i][0] = i; n       dfs(i); n     } n   for (int i = 1; i < 20; i++) n     for (int j = 1; j <= N; j++) { n       par[j][i] = par[par[j][i - 1]][i - 1]; n       for (int fl = 0; fl < 2; fl++) n         comp[j][i][fl] = comp[j][i - 1][fl] && comp[par[j][i - 1]][i - 1][fl]; n     } n   int Q; n   scanf( %d , &Q); n   for (int i = 0; i < Q; i++) { n     int tp, u, v; n     scanf( %d , &tp); n     scanf( %d , &u); n     scanf( %d , &v); n     int lca = LCA(u, v); n     if (v == lca) { n       puts( NO ); n     } else { n       if (tp == 1) { n         if (u == lca && get(v, lca, 0)) n           puts( YES ); n         else n           puts( NO ); n       } else { n         if (u == lca && get(v, lca, 1)) n           puts( YES ); n         else if (u != lca && get(u, lca, 0) && get(v, lca, 1)) n           puts( YES ); n         else n           puts( NO ); n       } n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n int x[200000], y[200000]; n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   int n, s[2]; n   cin >> n >> s[0] >> s[1]; n   for (int i = 0; i < n; i++) cin >> x[i] >> y[i]; n   int quadrant[5] = { n       0, n   }; n   int line[5] = { n       0, n   }; n   for (int i = 0; i < n; i++) { n     if (x[i] > s[0] && y[i] > s[1]) quadrant[1]++; n     if (x[i] < s[0] && y[i] > s[1]) quadrant[2]++; n     if (x[i] < s[0] && y[i] < s[1]) quadrant[3]++; n     if (x[i] > s[0] && y[i] < s[1]) quadrant[4]++; n     if (x[i] > s[0] && y[i] == s[1]) line[1]++; n     if (x[i] == s[0] && y[i] > s[1]) line[2]++; n     if (x[i] < s[0] && y[i] == s[1]) line[3]++; n     if (x[i] == s[0] && y[i] < s[1]) line[4]++; n   } n   int dir[5] = { n       0, n   }; n   dir[1] = quadrant[1] + quadrant[4] + line[1]; n   dir[2] = quadrant[1] + quadrant[2] + line[2]; n   dir[3] = quadrant[2] + quadrant[3] + line[3]; n   dir[4] = quadrant[3] + quadrant[4] + line[4]; n   int maxdir, maxval = 0; n   for (int i = 1; i <= 4; i++) { n     if (maxval < dir[i]) { n       maxdir = i; n       maxval = dir[i]; n     } n   } n   cout << maxval <<   n ; n   if (maxdir == 1) n     cout << s[0] + 1 <<     << s[1] <<   n ; n   else if (maxdir == 2) n     cout << s[0] <<     << s[1] + 1 <<   n ; n   else if (maxdir == 3) n     cout << s[0] - 1 <<     << s[1] <<   n ; n   else n     cout << s[0] <<     << s[1] - 1 <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n; n vector<int> Left; n vector<int> Right; n map<pair<int, int>, int> excep; n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(); n   cin >> n; n   for (int i = 0; i < n; i++) { n     int l, r; n     cin >> l >> r; n     Left.push_back(l); n     Right.push_back(r); n     excep[{l, r}] = 1; n   } n   sort(Left.begin(), Left.end(), greater<int>()); n   sort(Right.begin(), Right.end()); n   if (excep[{Left[0], Right[0]}] == 1) n     cout << max(0, Right[1] - Left[1]); n   else n     cout << max(0, max(Right[0] - Left[1], Right[1] - Left[0])); n } n


#include <bits/stdc++.h> n using namespace std; n long long int n, k; n vector<long long int> od, ev; n long long int dps[200005], dpd[200005]; n void buildDp() { n   dps[0] = 0LL; n   dps[1] = k - 1; n   dpd[0] = 1LL; n   dpd[1] = k - 2; n   for (int i = 2; i < 200005; i++) { n     dps[i] = ((dps[i - 2] * (k - 1)) % 998244353LL + n               ((dps[i - 1] * (k - 2)) % 998244353LL)) % n              998244353LL; n     dpd[i] = ((dpd[i - 2] * (k - 1)) % 998244353LL + n               ((dpd[i - 1] * (k - 2)) % 998244353LL)) % n              998244353LL; n   } n } n vector<pair<long long int, int>> p; n long long int solve(vector<long long int> v) { n   p.clear(); n   long long int ans = 1LL; n   int vz = (int)v.size(); n   if (vz == 0) return 1LL; n   for (int i = 0; i < vz; i++) n     if (v[i] != -1) p.push_back(make_pair(v[i], i)); n   int pz = (int)p.size(); n   if (pz > 0) { n     long long int nf = p[0].second; n     long long int nb = vz - 1 - p[pz - 1].second; n     for (int i = 0; i < nf; i++) ans = (ans * (k - 1)) % 998244353LL; n     for (int i = 0; i < nb; i++) ans = (ans * (k - 1)) % 998244353LL; n     if (pz >= 2) { n       for (int i = 0; i < pz - 1; i++) { n         int gap = p[i + 1].second - p[i].second - 1; n         if (p[i].first == p[i + 1].first) n           ans = (ans * dps[gap]) % 998244353LL; n         else n           ans = (ans * dpd[gap]) % 998244353LL; n       } n     } n   } else { n     ans = k; n     for (int i = 0; i < vz - 1; i++) ans = (ans * (k - 1)) % 998244353LL; n   } n   return ans; n } n int main() { n   ios_base::sync_with_stdio(0); n   ; n   cin >> n >> k; n   for (int i = 0; i < n; i++) { n     long long int temp; n     cin >> temp; n     if (i & 1LL) n       od.push_back(temp); n     else n       ev.push_back(temp); n   } n   buildDp(); n   cout << (solve(ev) * solve(od)) % 998244353LL; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n #pragma GCC diagnostic ignored  -Wmissing-declarations  n inline int safe_mul(const int x, const int y) n     __attribute__((warn_unused_result)); n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   int n, m; n   cin >> n >> m; n   vector<int> a(n); n   for (int& x : a) cin >> x; n   int const inf = 200001; n   vector<vector<int>> te(inf + 2); n   for (int i = 0; i < m; ++i) { n     int d, t; n     cin >> d >> t; n     te[d].push_back(t - 1); n   } n   int l = 0; n   int r = inf; n   while (r - l > 1) { n     int mid = (l + r) >> 1; n     auto tmpa = a; n     int val = mid; n     int sum = 0; n     for (int i = mid; i >= 1; --i) { n       for (int x : te[i]) { n         int cur = min(val, i); n         int need = min(tmpa[x], cur); n         cur -= need; n         val = min(val, cur); n         tmpa[x] -= need; n         sum += need; n       } n     } n     if (accumulate(begin(tmpa), end(tmpa), 0LL) * 2 <= mid - sum) n       r = mid; n     else n       l = mid; n   } n   if (r == inf) { n     int mid = inf - 1; n     auto tmpa = a; n     int val = mid; n     int64_t sum = 0; n     for (int i = mid; i >= 1; --i) { n       for (int x : te[i]) { n         int cur = min(val, i); n         int need = min(tmpa[x], cur); n         cur -= need; n         val = min(val, cur); n         tmpa[x] -= need; n         sum += need; n       } n     } n     sum += accumulate(begin(tmpa), end(tmpa), 0LL) * 2; n     cout << sum << endl; n   } else { n     cout << r << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 2 * 200000; n const int MAXM = 199999 * 2 * 2 + 200000 * 2; n int n, m, a, b; n string s[2]; n int dsu[MAXN]; n int to[MAXM], head[MAXN], nxt[MAXM], E; n int p[MAXN][19]; n int d[MAXN]; n int tin[MAXN], tout[MAXN], timer; n inline int root(int x) { return ~dsu[x] ? dsu[x] = root(dsu[x]) : x; } n inline void merge(int a, int b) { dsu[root(a)] = root(b); } n inline void addEdge(int a, int b) { n   to[E] = b; n   nxt[E] = head[a]; n   head[a] = E++; n   to[E] = a; n   nxt[E] = head[b]; n   head[b] = E++; n   merge(a, b); n } n void dfs(int v, int par, int dist = 0) { n   tin[v] = ++timer; n   p[v][0] = par; n   d[v] = dist; n   for (int i = 1; i < 19; ++i) p[v][i] = p[p[v][i - 1]][i - 1]; n   for (int i = head[v]; ~i; i = nxt[i]) { n     if (to[i] != par) { n       dfs(to[i], v, dist + 1); n     } n   } n   tout[v] = ++timer; n } n bool ancestor(int a, int b) { return tin[a] <= tin[b] && tout[a] >= tout[b]; } n int lca(int a, int b) { n   if (ancestor(a, b)) return a; n   if (ancestor(b, a)) return b; n   for (int i = 18; i > -1; --i) n     if (!ancestor(p[a][i], b)) a = p[a][i]; n   return p[a][0]; n } n int Distance(int a, int b) { n   int c = lca(a, b); n   return d[a] + d[b] - 2 * d[c]; n } n int dist(int a, int b) { n   if (root(a) != root(b)) return -1; n   int d1, d2; n   d1 = Distance(a, b); n   if (b < n) n     d2 = Distance(a, b + n); n   else n     d2 = Distance(a, b - n); n   return min(d1, d2 + 1); n } n int main() { n   ios_base::sync_with_stdio(false); n   memset(dsu, 0xff, sizeof dsu); n   memset(head, 0xff, sizeof head); n   cin >> n >> m; n   cin >> s[0] >> s[1]; n   for (int i = 0; i < n; ++i) { n     if (s[0][i] ==  . ) { n       if (s[1][i] ==  .  && n           (i + 1 == n || max(s[0][i + 1], s[1][i + 1]) ==  X )) n         addEdge(i, n + i); n       if (i + 1 != n && s[0][i + 1] ==  . ) addEdge(i, i + 1); n     } n     if (s[1][i] ==  .  && i + 1 != n && s[1][i + 1] ==  . ) n       addEdge(n + i, n + i + 1); n   } n   for (int i = 0; i < 2 * n; ++i) n     if (root(i) == i) dfs(i, i); n   while (m-- > 0) { n     cin >> a >> b; n     --a, --b; n     if (a % n > b % n) swap(a, b); n     cout << dist(a, b) <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T> n inline void Read(T &x) { n   char c = getchar(); n   bool f = false; n   for (x = 0; !isdigit(c); c = getchar()) { n     if (c ==  - ) { n       f = true; n     } n   } n   for (; isdigit(c); c = getchar()) { n     x = x * 10 + c -  0 ; n   } n   if (f) { n     x = -x; n   } n } n template <typename T> n inline bool CheckMax(T &a, const T &b) { n   return a < b ? a = b, true : false; n } n template <typename T> n inline bool CheckMin(T &a, const T &b) { n   return a > b ? a = b, true : false; n } n const int N = 205; n unsigned int ans, mod, c[N][N], f[N][N], s[N][N]; n int n, m; n int main() { n   Read(n), Read(m), Read(mod); n   for (int i = 0; i <= n; ++i) { n     c[i][0] = 1; n     for (int j = 1; j <= i; ++j) { n       c[i][j] = (c[i - 1][j] + c[i - 1][j - 1]) % mod; n     } n   } n   f[1][0] = 1; n   for (int i = 0; i <= m; ++i) { n     s[1][i] = 1; n   } n   for (int i = 2; i <= n; ++i) { n     for (int j = 1; j < i; ++j) { n       for (int k = 0; k < m; ++k) { n         f[i][k + 1] = (1LL * s[j][m - 1] * c[i - 2][j - 1] % mod * f[i - j][k] + n                        f[i][k + 1]) % n                       mod; n       } n     } n     for (int j = 1; j <= m; ++j) { n       s[i][j] = (s[i][j - 1] + f[i][j]) % mod; n     } n   } n   for (int i = 0; i < n; ++i) { n     for (int j = 0; j <= m; ++j) { n       for (int k = 0; j + k <= m; ++k) { n         if (k != 1) { n           ans = (1LL * f[i + 1][j] * f[n - i][k] + ans) % mod; n         } n       } n     } n   } n   ans = 2LL * n * (n - 1) * ans % mod; n   printf( %u n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int g[5][5] = {0}, tes; n   for (int x = 1; x < 4; x++) n     for (int y = 1; y < 4; y++) { n       cin >> tes; n       if (tes % 2 == 0) n         continue; n       else { n         g[x][y + 1]++; n         g[x][y - 1]++; n         g[x + 1][y]++; n         g[x - 1][y]++; n         g[x][y]++; n       } n     } n   for (int x = 1; x < 4; x++) { n     for (int y = 1; y < 4; y++) { n       cout << ((g[x][y] + 1) % 2); n     } n     cout << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int const xn = 1e4 + 105, xm = 105; n int n, m, cnt, hd[xn], ct, to[xn], nxt[xn], w[xn], s1, s2, f; n long long dis[2][xn], inf = 1e17; n bool vis[xn]; n struct E { n   int u, v, bh, l, r, w; n } ed[xm]; n struct N { n   long long d; n   int id; n   N(long long d = 0, int i = 0) : d(d), id(i) {} n   bool operator<(const N &y) const { return d > y.d; } n }; n priority_queue<N> q; n int rd() { n   int ret = 0, f = 1; n   char ch = getchar(); n   while (ch <  0  || ch >  9 ) { n     if (ch ==  - ) f = 0; n     ch = getchar(); n   } n   while (ch >=  0  && ch <=  9 ) ret = ret * 10 + ch -  0 , ch = getchar(); n   return f ? ret : -ret; n } n void add(int x, int y, int z) { n   to[++ct] = y; n   nxt[ct] = hd[x]; n   hd[x] = ct; n   w[ct] = z; n } n void dij(int t) { n   for (int i = 1; i <= n; i++) dis[t][i] = inf; n   for (int i = 1; i <= n; i++) vis[i] = 0; n   int st = (t ? s2 : s1); n   dis[t][st] = 0; n   q.push(N(0, st)); n   while (q.size()) { n     int x = q.top().id; n     q.pop(); n     if (vis[x]) continue; n     vis[x] = 1; n     for (int i = hd[x], u; i; i = nxt[i]) n       if (dis[t][u = to[i]] > dis[t][x] + w[i]) { n         dis[t][u] = dis[t][x] + w[i]; n         q.push(N(dis[t][u], u)); n       } n   } n } n int main() { n   n = rd(); n   m = rd(); n   cnt = rd(); n   s1 = rd(); n   s2 = rd(); n   f = rd(); n   for (int i = 1, x, y, z; i <= m; i++) n     x = rd(), y = rd(), z = rd(), add(x, y, z); n   for (int i = 1; i <= cnt; i++) n     ed[i].u = rd(), ed[i].v = rd(), ed[i].l = rd(), ed[i].r = rd(), n     ed[i].bh = ct + 1, ed[i].w = 1, add(ed[i].u, ed[i].v, ed[i].r); n   dij(0); n   dij(1); n   while (dis[0][f] >= dis[1][f]) { n     int i; n     for (i = 1; i <= cnt; i++) n       if (dis[0][ed[i].u] <= dis[1][ed[i].u] && ed[i].w) break; n     if (i > cnt) { n       if (dis[0][f] > dis[1][f]) n         puts( LOSE ); n       else { n         puts( DRAW ); n         for (int j = 1; j <= cnt; j++) n           printf( %d  , ed[j].w ? ed[j].r : ed[j].l); n         puts(  ); n       } n       return 0; n     } n     ed[i].w = 0; n     w[ed[i].bh] = ed[i].l; n     dij(0); n     dij(1); n   } n   puts( WIN ); n   for (int j = 1; j <= cnt; j++) printf( %d  , ed[j].w ? ed[j].r : ed[j].l); n   puts(  ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct edg { n   int cost, id, parity; n   bool operator<(edg other) const { return cost > other.cost; } n }; n vector<int> g[1000011 >> 1][2]; n int from[1000011], to[1000011], l[1000011], r[1000011]; n int N, M; n bool cmp(int &a, int &b) { return l[a] > l[b]; } n int main() { n   int i; n   priority_queue<edg, vector<edg>, less<edg>> q; n   scanf( %d%d , &N, &M); n   if (N == 1) { n     printf( 0 n ); n     return 0; n   } n   for (i = 1; i <= M; ++i) { n     scanf( %d%d%d%d , &from[i * 2], &to[i * 2], &l[i * 2], &r[i * 2]); n     r[i * 2]--; n     from[i * 2 + 1] = to[i * 2]; n     to[i * 2 + 1] = from[i * 2]; n     l[i * 2 + 1] = l[i * 2]; n     r[i * 2 + 1] = r[i * 2]; n     g[from[i * 2]][0].push_back(i * 2); n     g[from[i * 2]][1].push_back(i * 2); n     g[to[i * 2]][0].push_back(i * 2 + 1); n     g[to[i * 2]][1].push_back(i * 2 + 1); n   } n   for (i = 1; i <= N; ++i) { n     sort(g[i][0].begin(), g[i][0].end(), cmp); n     sort(g[i][1].begin(), g[i][1].end(), cmp); n   } n   while (!g[1][0].empty() && l[g[1][0].back()] == 0) { n     edg e = {0, g[1][0].back(), 0}; n     q.push(e); n     g[1][0].pop_back(); n   } n   while (!q.empty()) { n     edg curr = q.top(); n     q.pop(); n     if (to[curr.id] == N) { n       printf( %d n , curr.cost + 1); n       return 0; n     } n     int mxright = r[curr.id] - ((r[curr.id] % 2) != curr.parity); n     while (!g[to[curr.id]][curr.parity ^ 1].empty() && n            l[g[to[curr.id]][curr.parity ^ 1].back()] <= mxright + 1) { n       int nxtid = g[to[curr.id]][curr.parity ^ 1].back(); n       int ncost = l[nxtid]; n       ncost += (ncost % 2 == curr.parity); n       ncost = max(ncost, curr.cost + 1); n       if (ncost <= r[nxtid]) { n         q.push(edg{ncost, nxtid, curr.parity ^ 1}); n       } n       g[to[curr.id]][curr.parity ^ 1].pop_back(); n     } n   } n   printf( -1 n ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   long long n, a, b, t; n   string str; n   cin >> n >> a >> b >> t >> str; n   str += str; n   long long len = 1, l = n, r, remain = t - 1 - (str[n] ==  w ) * b, ans = 1; n   if (remain < 0) { n     cout <<  0 n ; n     return 0; n   } n   while (len < n) { n     long long require = 2 * a + 1 + (str[l - 1] ==  w ) * b; n     if (remain < require) { n       break; n     } else { n       l--; n       len++; n       remain -= require; n     } n   } n   for (r = n + 1; r < str.size(); r++) { n     remain -= a + 1 + (str[r] ==  w ) * b; n     while (r - l + 1 > n || (l < n && remain < 0)) { n       remain += 2 * a + 1 + (str[l] ==  w ) * b; n       l++; n     } n     if (l >= n && remain < 0) break; n     ans = max(ans, r - l + 1); n   } n   len = 1, l = r = n, remain = t - 1 - (str[n] ==  w ) * b; n   while (len < n) { n     long long require = 2 * a + 1 + (str[r + 1] ==  w ) * b; n     if (remain < require) { n       break; n     } else { n       r++; n       len++; n       remain -= require; n     } n   } n   for (l = n - 1; l > 0; l--) { n     remain -= a + 1 + (str[l] ==  w ) * b; n     while (r - l + 1 > n || (r > n && remain < 0)) { n       remain += 2 * a + 1 + (str[r] ==  w ) * b; n       r--; n     } n     if (r <= n && remain < 0) break; n     ans = max(ans, r - l + 1); n   } n   cout << ans <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n const int MOD = (int)1e9 + 7; n const int INF = (int)1e9; n const long long LINF = (long long)1e18; n const long double PI = 2 * acos((long double)0); n long long gcd(long long a, long long b) { n   long long r; n   while (b) { n     r = a % b; n     a = b; n     b = r; n   } n   return a; n } n long long lcm(long long a, long long b) { return a / gcd(a, b) * b; } n long long fpow(long long n, long long k, int p = MOD) { n   long long r = 1; n   for (; k; k >>= 1) { n     if (k & 1) r = r * n % p; n     n = n * n % p; n   } n   return r; n } n template <class T> n void setmin(T& a, T val) { n   if (a > val) a = val; n } n template <class T> n void setmax(T& a, T val) { n   if (a < val) a = val; n } n void addmod(int& a, int val, int p = MOD) { n   if ((a = (a + val)) >= p) a -= p; n } n void submod(int& a, int val, int p = MOD) { n   if ((a = (a - val)) < 0) a += p; n } n int mult(int a, int b, int p = MOD) { return (long long)a * b % p; } n int inv(int a, int p = MOD) { return fpow(a, p - 2, p); } n const int maxn = 200010; n long long n, d, m; n long long x[maxn]; n long long p[maxn]; n long long st[4 * maxn]; n void build(int node, int L, int R) { n   if (L == R) { n     st[node] = p[L]; n     return; n   } n   build(node << 1, L, (L + R) >> 1); n   build((node << 1) + 1, ((L + R) >> 1) + 1, R); n   st[node] = min(st[node << 1], st[(node << 1) + 1]); n } n long long query(int node, int l, int r, int L, int R) { n   if (l > R || r < L) return LINF; n   if (l <= L && r >= R) return st[node]; n   return min(query(node << 1, l, r, L, (L + R) >> 1), n              query((node << 1) + 1, l, r, ((L + R) >> 1) + 1, R)); n } n void solve() { n   cin >> d >> n >> m; n   vector<pair<int, int> > v; n   for (int i = (1); i < (m + 1); i++) { n     cin >> x[i] >> p[i]; n     v.push_back(make_pair(x[i], p[i])); n   } n   sort((v).begin(), (v).end()); n   for (int i = (1); i < (m + 1); i++) { n     x[i] = v[i - 1].first; n     p[i] = v[i - 1].second; n   } n   x[m + 1] = d; n   build(1, 0, m + 1); n   int cur = 0; n   long long ans = 0, r = 0; n   while (cur < m + 1) { n     if (x[cur] + n < x[cur + 1]) { n       cout << -1; n       return; n     } n     int lo = cur + 1, hi = upper_bound(x, x + m + 2, x[cur] + n) - x - 1; n     while (lo < hi) { n       int mid = (lo + hi) >> 1; n       if (query(1, cur + 1, mid, 0, m + 1) > p[cur]) n         lo = mid + 1; n       else n         hi = mid; n     } n     if (query(1, cur + 1, lo, 0, m + 1) <= p[cur]) { n       ans += p[cur] * max(0LL, x[lo] - x[cur] - r); n       r = max(0LL, x[cur] + r - x[lo]); n       cur = lo; n     } else { n       ans += (n - r) * p[cur]; n       r = x[cur] + n - x[cur + 1]; n       cur++; n     } n   } n   cout << ans; n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int gcd(int a, int b) { n   if (a == 0) return b; n   if (b == 0) return a; n   if (a == b) return a; n   if (a > b) return gcd(a - b, b); n   return gcd(a, b - a); n } n int main() { n   long long int n, m; n   cin >> n >> m; n   long long int a[n + 1], b[m + 1]; n   for (long long int i = 0; i < n + 1; i++) { n     cin >> a[i]; n   } n   for (long long int i = 0; i < m + 1; i++) { n     cin >> b[i]; n   } n   if (m > n) n     cout <<  0/1 ; n   else if (n > m) { n     int sign = 1; n     if ((a[0] < 0 && b[0] > 0) || (a[0] > 0 && b[0] < 0)) sign = 0; n     if (sign == 0) n       cout <<  -Infinity ; n     else n       cout <<  Infinity ; n   } else { n     int sign = 1; n     if ((a[0] < 0 && b[0] > 0) || (a[0] > 0 && b[0] < 0)) sign = 0; n     int g = gcd(a[0], b[0]); n     a[0] /= g; n     b[0] /= g; n     if (sign == 1) { n       if (a[0] < 0) a[0] = -a[0]; n       if (b[0] < 0) b[0] = -b[0]; n       cout << a[0] <<  /  << b[0]; n     } else { n       if (a[0] < 0) a[0] = -a[0]; n       if (b[0] < 0) b[0] = -b[0]; n       cout <<  -  << a[0] <<  /  << b[0]; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct heap { n   int v, x; n } h[1000010]; n int n, m, a[500010]; n int tmp, sz, ans, tot; n int v[500010], cnt; n int nxt[500010], pre[500010], pos[500010]; n inline int read() { n   char ch = getchar(); n   int f = 1, x = 0; n   while (!(ch >=  0  && ch <=  9 )) { n     if (ch ==  - ) f = -1; n     ch = getchar(); n   } n   while (ch >=  0  && ch <=  9 ) { n     x = x * 10 + (ch -  0 ); n     ch = getchar(); n   } n   return x * f; n } n void pushup(int x) { n   while (h[x].v < h[x >> 1].v) { n     pos[h[x >> 1].x] = x; n     swap(h[x], h[x >> 1]); n     x = x >> 1; n   } n   pos[h[x].x] = x; n } n void push(int v, int x) { n   sz++; n   h[sz].v = v; n   h[sz].x = x; n   pos[x] = sz; n   pushup(sz); n } n void pushdown(int x) { n   int to; n   while ((x << 1) <= sz) { n     to = (x << 1); n     if (to < sz && h[to].v > h[to + 1].v) to++; n     if (h[x].v > h[to].v) { n       pos[h[to].x] = x; n       swap(h[to], h[x]); n       x = to; n     } else n       break; n   } n   pos[h[x].x] = x; n } n void del(int x) { n   h[x].v = 0x7fffffff; n   pushdown(x); n } n void init() { n   m = read(); n   n = read(); n   for (int i = 1; i <= n; i++) a[i] = read(); n   sort(a + 1, a + n + 1); n   for (int i = 2; i <= n; i++) { n     push(a[i] - a[i - 1], i); n     pre[i] = i - 1; n     nxt[i] = i + 1; n   } n   pre[2] = nxt[n] = -1; n } n void solve() { n   int a, b; n   heap k; n   for (int i = 1; i <= m; i++) { n     k = h[1]; n     if (pre[k.x] == -1) { n       ans += k.v; n       del(1); n       del(pos[nxt[k.x]]); n       pre[nxt[nxt[k.x]]] = -1; n     } else if (nxt[k.x] == -1) { n       ans += k.v; n       del(1); n       del(pos[pre[k.x]]); n       nxt[pre[pre[k.x]]] = -1; n     } else { n       ans += k.v; n       a = nxt[k.x]; n       b = pre[k.x]; n       pre[k.x] = pre[b]; n       nxt[pre[b]] = k.x; n       nxt[k.x] = nxt[a]; n       pre[nxt[a]] = k.x; n       h[1].v = h[pos[a]].v + h[pos[b]].v - h[1].v; n       pushdown(1); n       del(pos[a]); n       del(pos[b]); n     } n   } n   printf( %d n , ans); n } n int main() { n   init(); n   solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MOD = 1e9 + 7, N = 2e5 + 5; n int par[N], over[N]; n int getpar(int u) { n   if (par[par[u]] == par[u]) return par[u]; n   return par[u] = getpar(par[u]); n } n bool unite(int a, int b) { n   a = getpar(a); n   b = getpar(b); n   if (a == b) { n     if (over[a]) return false; n     over[a] += 1; n     return true; n   } else { n     if (over[a] && over[b]) { n       return false; n     } n     par[a] = b; n     over[b] += over[a]; n     return true; n   } n   assert(false); n } n struct Edge { n   int a, b, c; n   bool operator<(const Edge &t) const { return c > t.c; } n }; n int main() { n   iota(par, par + N, 0); n   int n, m; n   cin >> n >> m; n   vector<Edge> edges(m); n   for (int i = 0; i < m; ++i) n     scanf( %d %d %d , &edges[i].a, &edges[i].b, &edges[i].c); n   sort(edges.begin(), edges.end()); n   int ans = 0; n   for (auto cur : edges) n     if (unite(cur.a, cur.b)) ans += cur.c; n   cout << ans <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n class Solution { n   int max_free; n   vector<bool> visited; n   vector<int> parent; n   vector<set<int>> children; n   void dfs(int i) { n     visited[i] = true; n     for (int j : children[i]) { n       if (!visited[j]) { n         if (i != 0) cout << i <<     << j << endl; n         dfs(j); n       } n     } n   } n  n  public: n   void solve() { n     int n; n     cin >> n; n     vector<int> a(n); n     for (int i = 1; i < n; ++i) scanf( %d , &a[i]); n     children = vector<set<int>>(n + 1); n     parent = vector<int>(n + 1, -1); n     children[0].insert(n); n     parent[n] = 0; n     max_free = n; n     for (int i = 1; i < n; ++i) { n       if (parent[a[i]] == -1) { n         if (!children[a[i]].empty()) { n           max_free--; n           while (!children[max_free].empty()) max_free--; n         } n         if (parent[max_free] != -1) { n           int p = parent[max_free]; n           children[p].erase(max_free); n           children[p].insert(a[i]); n         } n       } else { n         max_free--; n         while (!children[max_free].empty()) max_free--; n       } n       children[a[i]].insert(max_free); n       parent[max_free] = a[i]; n     } n     cout << a[1] << endl; n     visited = vector<bool>(n + 1); n     dfs(0); n   } n }; n int main() { n   Solution solution = Solution(); n   solution.solve(); n } n


#include <bits/stdc++.h> n using namespace std; n int n, m, ha1[50][50], ha2[50][50], ans1[50][50], ans2[50][50], x = 0, flag = 0; n int main() { n   memset(ha1, 0, sizeof(ha1)); n   memset(ha2, 0, sizeof(ha2)); n   memset(ans1, 0, sizeof(ans1)); n   memset(ans2, 0, sizeof(ans2)); n   cin >> n >> m; n   for (int i = 1, xx, yy; i <= n; ++i) { n     cin >> xx >> yy; n     ha1[xx][yy] = 1; n     ha1[yy][xx] = 1; n   } n   for (int i = 1, xx, yy; i <= m; ++i) { n     cin >> xx >> yy; n     ha2[xx][yy] = 1; n     ha2[yy][xx] = 1; n   } n   for (int i = 1; i <= 9; ++i) n     for (int j = 1; j <= 9; ++j) { n       if (i == j) continue; n       for (int k = 1; k <= 9; ++k) { n         if (k == i || k == j) continue; n         if (ha1[i][j] == ha2[i][k] && ha1[i][j]) { n           ++ans1[i][j]; n           ++ans2[i][k]; n           x = i; n         } n       } n     } n   for (int i = 1; i <= 9; ++i) n     for (int j = 1; j <= 9; ++j) n       if (ans1[i][j]) { n         flag++; n         break; n       } n   if (flag == 1) { n     cout << x << endl; n     return 0; n   } n   x = 0; n   for (int i = 1; i <= 9; ++i) n     for (int j = 1; j <= 9; ++j) { n       if (i == j) continue; n       if ((ans1[i][j] && ans1[j][i]) || (ans2[i][j] && ans2[j][i])) x = -1; n     } n   cout << x << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int parent[100000], Size[100000]; n void init(int n) { n   for (int i = 0; i < n; i++) { n     parent[i] = i; n     Size[i] = 1; n   } n } n int Find(int u) { n   if (u != parent[u]) parent[u] = Find(parent[u]); n   return parent[u]; n } n void Union(int u, int v) { n   u = Find(u); n   v = Find(v); n   if (u != v) { n     if (Size[u] < Size[v]) swap(u, v); n     parent[v] = u; n     Size[u] += Size[v]; n   } n } n int qa[100000], qb[100000], big[100000], table[450][100000] = {}; n bool visited[100000] = {}, match[100000] = {}; n vector<int> bigVertices, temp[100000], appear[100000]; n vector<vector<int>> comp; n vector<pair<int, int>> edges[100000]; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   int n, m; n   cin >> n >> m; n   for (int i = 0; i < m; i++) { n     int a, b, c; n     cin >> a >> b >> c; n     a--, b--, c--; n     edges[c].emplace_back(a, b); n   } n   int q; n   cin >> q; n   for (int i = 0; i < q; i++) { n     cin >> qa[i] >> qb[i]; n     qa[i]--, qb[i]--; n   } n   init(n); n   for (int c = 0; c < m; c++) { n     for (auto e : edges[c]) Union(e.first, e.second); n     for (auto e : edges[c]) { n       if (!visited[e.first]) { n         temp[Find(e.first)].push_back(e.first); n         visited[e.first] = true; n       } n       if (!visited[e.second]) { n         temp[Find(e.second)].push_back(e.second); n         visited[e.second] = true; n       } n     } n     for (auto e : edges[c]) { n       if (!temp[e.first].empty()) { n         comp.push_back(temp[e.first]); n         temp[e.first].clear(); n       } n       if (!temp[e.second].empty()) { n         comp.push_back(temp[e.second]); n         temp[e.second].clear(); n       } n       visited[e.first] = visited[e.second] = false; n       parent[e.first] = e.first, parent[e.second] = e.second; n       Size[e.first] = Size[e.second] = 1; n     } n   } n   for (int i = 0; i < comp.size(); i++) n     for (int j = 0; j < comp[i].size(); j++) appear[comp[i][j]].push_back(i); n   for (int i = 0; i < n; i++) { n     if (appear[i].size() > 450) { n       big[i] = bigVertices.size(); n       bigVertices.push_back(i); n     } else n       big[i] = -1; n   } n   for (int i = 0; i < bigVertices.size(); i++) n     for (int c : appear[bigVertices[i]]) n       for (int j : comp[c]) table[i][j]++; n   for (int i = 0; i < q; i++) { n     int a = qa[i], b = qb[i]; n     if (big[a] == -1) swap(a, b); n     if (big[a] != -1) n       cout << table[big[a]][b] <<   n ; n     else { n       int ret = 0; n       for (int c : appear[a]) match[c] = true; n       for (int c : appear[b]) ret += match[c]; n       for (int c : appear[a]) match[c] = false; n       cout << ret <<   n ; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e6; n int lp[N + 1]; n vector<int> pr; n int i, j, p1, p2, x0, x1, x2, ans; n int main() { n   for (i = 2; i <= N; i++) { n     if (lp[i] == 0) { n       lp[i] = i; n       pr.push_back(i); n     } n     for (j = 0; j < pr.size() && pr[j] <= lp[i] && i * pr[j] <= N; j++) n       lp[i * pr[j]] = pr[j]; n   } n   cin >> x2; n   for (i = pr.size() - 1; i >= 0; i--) n     if (x2 % pr[i] == 0) break; n   if (x2 == pr[i]) { n     cout << x2 << endl; n     return 0; n   } n   p2 = pr[i]; n   ans = x2 - p2 + 1; n   for (j = 0; j < pr.size() && pr[j] <= x2; j++) { n     p1 = pr[j]; n     x1 = ((x2 - p2) / p1 + 1) * p1; n     if (x1 > x2) continue; n     if (x1 == p1) n       x0 = p1; n     else n       x0 = x1 - p1 + 1; n     if (x0 >= 3) ans = min(ans, x0); n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long M = 2100000; n const long long N = 210000; n struct node { n   long long inx; n   long long a, b; n   long long m; n   long long lt, rt; n }; n vector<node> odd_nodes[M]; n vector<node> even_nodes[M]; n bool cmp(node a, node b) { n   if (a.lt == b.lt) n     return a.rt > b.rt; n   else n     return a.lt < b.lt; n } n long long lt_ans[N], rt_ans[N]; n set<long long> op; n int main() { n   ios::sync_with_stdio(false); n   long long tt; n   cin >> tt; n   while (tt--) { n     op.clear(); n     long long m, n; n     cin >> n; n     long long a, b; n     for (long long i = 0; i < n; i++) { n       cin >> a >> b >> m; n       long long mn = max(0ll, a - m); n       long long tb = b - (m - (a - mn)); n       long long mx = max(0ll, b - m); n       long long ta = a - (m - (b - mx)); n       long long v = a + b - m; n       op.insert(v); n       odd_nodes[v].push_back({i, a, b, m, mn - tb, ta - mx}); n     } n     long long ans = 0; n     for (auto &u : op) { n       auto &p = odd_nodes[u]; n       if (p.empty()) continue; n       sort(p.begin(), p.end(), cmp); n       long long st = 0, ed = 1e9; n       for (long long i = 0; i < p.size(); i++) { n         if (p[i].lt > ed) { n           for (long long j = st; j < i; j++) { n             long long t = (p[j].a - p[j].b + p[j].m - ed) / 2; n             lt_ans[p[j].inx] = t; n             rt_ans[p[j].inx] = p[j].m - t; n           } n           ans++; n           ed = p[i].rt; n           st = i; n         } else { n           ed = min(ed, p[i].rt); n         } n       } n       ans++; n       for (long long j = st; j < p.size(); j++) { n         long long t = (p[j].a - p[j].b + p[j].m - ed) / 2; n         lt_ans[p[j].inx] = t; n         rt_ans[p[j].inx] = p[j].m - t; n       } n       p.clear(); n     } n     cout << ans << endl; n     for (long long i = 0; i < n; i++) n       cout << lt_ans[i] <<     << rt_ans[i] << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, a[1000005], r[3], cnt; n bool up, down; n set<int> s; n int main() { n   srand(75435); n   scanf( %d , &n); n   for (int i = 0; i < n; i++) scanf( %d , &a[i]); n   if (n <= 2) { n     cout << -1; n     return 0; n   } n   for (int i = 0; i < n; i++) { n     if (s.find(a[i]) == s.end()) { n       if (cnt < 3) r[cnt++] = i; n       s.insert(a[i]); n     } n   } n   if (cnt == 3) { n     if (r[0] > r[1]) swap(r[0], r[1]); n     if (r[0] > r[2]) swap(r[0], r[2]); n     if (r[1] > r[2]) swap(r[1], r[2]); n     if ((a[r[1]] > a[r[0]] && a[r[1]] > a[r[2]]) || n         (a[r[1]] < a[r[0]] && a[r[1]] < a[r[2]])) { n       cout << r[0] + 1 <<     << r[2] + 1; n       return 0; n     } n     cout << r[0] + 1 <<     << r[1] + 1 << endl; n     return 0; n   } n   for (int i = 0; i < n - 1; i++) n     if (a[i] != a[i + 1]) { n       swap(a[i], a[i + 1]); n       up = true; n       down = true; n       for (int j = 0; j < n - 1; j++) n         if (a[j] > a[j + 1]) n           up = false; n         else if (a[j] < a[j + 1]) n           down = false; n       if (!down && !up) { n         cout << i + 1 <<     << i + 2; n         return 0; n       } n       swap(a[i], a[i + 1]); n     } n   cout << -1; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int a, b; n   cin >> a >> b; n   vector<string> N(a), M(b); n   for (auto& i : N) cin >> i; n   for (auto& i : M) cin >> i; n   int q; n   cin >> q; n   while (q--) { n     int n; n     cin >> n; n     cout << N[(n - 1) % a] << M[(n - 1) % b] <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   cin >> n; n   set<int> s; n   for (int i = 0; i < n; i++) { n     int t; n     cin >> t; n     s.insert(t); n   } n   int a, b; n   cin >> a >> b; n   int res = 0; n   while (a > b) { n     int mx = 1; n     set<int> toerase; n     for (set<int>::reverse_iterator it = s.rbegin(); it != s.rend(); ++it) { n       int curr = a % *it; n       if (a - curr >= b) { n         if (curr > mx) mx = curr; n       } else { n         toerase.insert(*it); n       } n       if (mx >= *it) break; n     } n     for (set<int>::iterator it = toerase.begin(); it != toerase.end(); ++it) { n       s.erase(*it); n     } n     a -= mx; n     res++; n   } n   cout << res; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 2020, mod = 1000000007; n int n, m, t = 0, a[N] = {}, vis[N] = {}; n int *f[N] = {}, *g, *h, arr[N][N << 2] = {}; n vector<int> e[N]; n long long ans = 0; n int main() { n   cin >> n >> m; n   for (int i = 1, u, v; i <= m; ++i) { n     cin >> u >> v; n     e[u].push_back(v); n     e[v].push_back(u); n   } n   for (auto x : e[1]) n     if (!vis[x]) { n       int l = 1, now = x, last = 1, tmp; n       vis[now] = true; n       while (now != 1) { n         tmp = last; n         last = now; n         now = e[now][e[now][0] == tmp]; n         vis[now] = true; n         ++l; n       } n       a[++t] = l; n     } n   for (int i = 0; i <= t; ++i) f[i] = arr[i] + m; n   f[0][0] = 1; n   for (int i = 1; i <= t; ++i) n     for (int v = -m; v <= m; ++v) { n       if (v + a[i] <= m) f[i][v] = (f[i][v] + f[i - 1][v + a[i]]) % mod; n       if (v - a[i] >= -m) f[i][v] = (f[i][v] + f[i - 1][v - a[i]]) % mod; n       f[i][v] = (f[i][v] + f[i - 1][v]) % mod; n     } n   g = arr[t + 1] + m; n   h = arr[t + 2] + m; n   for (int i = 1; i <= t; ++i) { n     copy(f[t] - m, f[t] + m + 1, g - m); n     fill(h - m, h + m + 1, 0); n     for (int v = -m; v <= m; ++v) { n       g[v + a[i]] = (g[v + a[i]] + mod - g[v]) % mod; n       g[v + a[i] + a[i]] = (g[v + a[i] + a[i]] + mod - g[v]) % mod; n       h[v + a[i]] = g[v]; n       g[v] = 0; n     } n     for (int v = -a[i] + 2; v <= a[i] - 2; ++v) n       ans = (ans + h[v] * 2 % mod) % mod; n   } n   memset(arr, 0, sizeof(arr)); n   f[0][0] = 1; n   for (int i = 1; i <= t; ++i) n     for (int v = -m; v <= m; ++v) { n       if (v + a[i] <= m) f[i][v] = (f[i][v] + f[i - 1][v + a[i]]) % mod; n       if (v - a[i] >= -m) f[i][v] = (f[i][v] + f[i - 1][v - a[i]]) % mod; n     } n   g = arr[t + 1] + m; n   h = arr[t + 2] + m; n   for (int i = 1; i <= t; ++i) { n     copy(f[t] - m, f[t] + m + 1, g - m); n     fill(h - m, h + m + 1, 0); n     for (int v = -m; v <= m; ++v) { n       g[v + a[i] + a[i]] = (g[v + a[i] + a[i]] + mod - g[v]) % mod; n       h[v + a[i]] = g[v]; n       g[v] = 0; n     } n     ans = (ans + h[a[i] - 1] * 2ll + h[1 - a[i]] * 2ll) % mod; n   } n   ans = (ans + f[t][0]) % mod; n   cout << ans << endl; n } n


#include <bits/stdc++.h> n using namespace std; n const int INF = 2e9; n const long long LINF = 1LL << 60; n const double inf = 1e15; n long long mod = 1e9 + 7; n char READ_DATA; n int SIGNAL_INPUT; n template <typename Type> n inline Type ru(Type &v) { n   SIGNAL_INPUT = 1; n   while ((READ_DATA = getchar()) <  0  || READ_DATA >  9 ) n     if (READ_DATA ==  - ) n       SIGNAL_INPUT = -1; n     else if (READ_DATA == EOF) n       return EOF; n   v = READ_DATA -  0 ; n   while ((READ_DATA = getchar()) >=  0  && READ_DATA <=  9 ) n     v = v * 10 + READ_DATA -  0 ; n   v *= SIGNAL_INPUT; n   return v; n } n inline long long modru(long long &v) { n   long long p = 0; n   SIGNAL_INPUT = 1; n   while ((READ_DATA = getchar()) <  0  || READ_DATA >  9 ) n     if (READ_DATA ==  - ) n       SIGNAL_INPUT = -1; n     else if (READ_DATA == EOF) n       return EOF; n   p = v = READ_DATA -  0 ; n   while ((READ_DATA = getchar()) >=  0  && READ_DATA <=  9 ) { n     v = (v * 10 + READ_DATA -  0 ) % mod; n     p = (p * 10 + READ_DATA -  0 ) % (mod - 1); n   } n   v *= SIGNAL_INPUT; n   return p; n } n template <typename A, typename B> n inline char ru(A &x, B &y) { n   if (ru(x) == EOF) return EOF; n   ru(y); n   return 2; n } n template <typename A, typename B, typename C> n inline char ru(A &x, B &y, C &z) { n   if (ru(x) == EOF) return EOF; n   ru(y); n   ru(z); n   return 3; n } n template <typename A, typename B, typename C, typename D> n inline char ru(A &x, B &y, C &z, D &w) { n   if (ru(x) == EOF) return EOF; n   ru(y); n   ru(z); n   ru(w); n   return 4; n } n struct Edge { n   int u, v, next; n   long long w, cap, flow; n   Edge(int _u = 0, int _v = 0, int nxt = -1, long long _w = 1, n        long long _cap = 0) { n     u = _u; n     v = _v; n     w = _w; n     cap = _cap; n     flow = 0; n     next = nxt; n   } n   int operator<(const Edge &b) const { return w < b.w; } n }; n const int maxn = 2e6 + 3, N = 2e7, SZ = 21; n double eps = 1e-7, pi = acos(-1.0); n long long seed = 131, smod = (1LL << 32) - 267; n long long a, b, p, n; n long long sp(long long a, long long b, long long mod) { n   long long ans = 1, x = a; n   while (b) { n     if (b & 1) (ans *= x) %= mod; n     (x *= x) %= mod; n     b >>= 1; n   } n   return ans; n } n long long c[maxn]; n long long ans1, ans2; n int main() { n   cin >> a >> b >> p >> n; n   c[0] = 1; n   for (register int j = 1; j < p; ++j) c[j] = c[j - 1] * a % p; n   for (register int j = 1; j < p; ++j) { n     long long y = c[j * (p - 2) % (p - 1)] * b % p; n     long long t = (j - y + p) % p; n     if (t * (p - 1) + j > n) continue; n     long long u = ((n - j) / (p - 1) - t) / p; n     ans2 += (u + 1); n   } n   cout << ans2; n } n


#include <bits/stdc++.h> n using namespace std; n void change(char &x) { n   if (x ==  B ) n     x =  W ; n   else n     x =  B ; n } n int main() { n   string s; n   int n; n   cin >> n; n   cin >> s; n   int w = 0, b = 0; n   for (int i = 0; i < n; i++) { n     if (s[i] ==  B ) n       b++; n     else n       w++; n   } n   if (b % 2 != 0 && w % 2 != 0) { n     cout << -1; n     return 0; n   } n   string c = s; n   vector<int> vec1, vec2; n   for (int i = 0; i < n - 1; i++) { n     if (s[i] ==  B ) { n       vec1.push_back(i + 1); n       if (s[i] ==  B ) change(s[i]); n       change(s[i + 1]); n     } n   } n   if (s[n - 1] ==  W ) { n     cout << vec1.size() << endl; n     for (int i = 0; i < vec1.size(); i++) cout << vec1[i] <<    ; n     return 0; n   } n   for (int i = 0; i < n - 1; i++) { n     if (c[i] ==  W ) { n       vec2.push_back(i + 1); n       change(c[i]); n       change(c[i + 1]); n     } n   } n   if (s[n - 1] ==  B ) { n     cout << vec2.size() << endl; n     for (int i = 0; i < vec2.size(); i++) cout << vec2[i] <<    ; n     return 0; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <class T, class T2> n inline int chkmax(T &x, const T2 &y) { n   return x < y ? x = y, 1 : 0; n } n template <class T, class T2> n inline int chkmin(T &x, const T2 &y) { n   return x > y ? x = y, 1 : 0; n } n const int MAXN = (1 << 20); n int n, m; n int64_t g[MAXN], b[MAXN]; n void read() { n   cin >> n >> m; n   for (int i = 0; i < n; i++) cin >> b[i]; n   for (int i = 0; i < m; i++) cin >> g[i]; n } n void solve() { n   sort(b, b + n); n   sort(g, g + m); n   if (b[n - 1] > g[0]) { n     cout << -1 <<   n ; n     return; n   } n   int64_t answer = 0; n   for (int i = 0; i < n; i++) answer += b[i] * 1ll * m; n   for (int i = m - 1; i >= 0; i--) answer += g[i]; n   if (b[n - 1] == g[0]) n     answer -= m * 1ll * b[n - 1]; n   else { n     answer -= (m - 1) * 1ll * b[n - 1]; n     answer -= b[n - 2]; n   } n   cout << answer <<   n ; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   read(); n   solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, i; n   long long sum = 0, z = 0, s = 0, m = 0, mm = 0; n   cin >> n; n   vector<long long> a(n); n   for (i = 0; i < n; i++) cin >> a[i]; n   for (i = 0; i < (n - 1); i++) { n     if (i % 2 == 0) n       sum = sum + abs(a[i + 1] - a[i]); n     else n       sum = max(z, sum - abs(a[i + 1] - a[i])); n     m = max(m, sum); n   } n   for (i = 1; i < (n - 1); i++) { n     if (i % 2 == 1) n       s = s + abs(a[i + 1] - a[i]); n     else n       s = max(z, s - abs(a[i + 1] - a[i])); n     m = max(s, m); n   } n   cout << m; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, a[100]; n   cin >> n; n   int i, loce, loco, even = 0, odd = 0; n   for (i = 0; i < n; i++) { n     cin >> a[i]; n     if (a[i] % 2 == 0) { n       even++; n       loce = i; n     } n     if (a[i] % 2 == 1) { n       odd++; n       loco = i; n     } n   } n   if (odd > even) { n     cout << loce + 1; n   } else { n     cout << loco + 1; n   } n } n


#include <bits/stdc++.h> n using namespace std; n long long mInv[41]; n long long fact[41]; n long long factInv[41]; n map<long, long> mymap; n long long heights[5]; n long long segTree[3]; n long nums[5005]; n long g[5005]; n vector<long> primes; n vector<long> badPrimes; n long funcVal[1000006]; n long long N; n set<string> dictionary; n set<string>::iterator it; n pair<set<string>::iterator, bool> ret; n long long gcd(long long a, long long b) { n   if (b == 0) return a; n   if (a % b == 0) return b; n   return gcd(b, a % b); n } n long long getmoduloInv(long long n) { n   if (n == 1) return 1; n   if (mInv[n] > 0) return mInv[n]; n   long long m = (-1 * 1000000007) / n; n   m += 1000000007; n   m *= getmoduloInv(1000000007 % n); n   mInv[n] = (m % 1000000007); n   return mInv[n]; n } n vector<long> get_primes(unsigned long maxN) { n   char *sieve; n   sieve = new char[maxN / 8 + 1]; n   long m = (maxN / 8) + 1; n   for (long long i = 0; i < m; i++) sieve[i] = 255; n   for (unsigned long x = 2; x <= maxN; x++) n     if (sieve[x / 8] & (0x01 << (x % 8))) { n       primes.push_back(x); n       for (unsigned long j = 2 * x; j <= maxN; j += x) n         sieve[j / 8] &= ~(0x01 << (j % 8)); n     } n   delete[] sieve; n   return primes; n } n long long getPow(long long b, long long p) { n   if (b < 2 || p == 1) return b; n   if (p == 0) return 1; n   long long val = getPow(b, p / 2); n   val *= val; n   val %= 1000000007; n   if (p % 2 == 1) { n     val *= b; n     val %= 1000000007; n   } n   return val; n } n void buildSegTree(long node, long b, long e) { n   if (b == e) n     segTree[node] = b; n   else { n     long mid = (b + e) / 2; n     buildSegTree(node * 2, b, mid); n     buildSegTree(1 + node * 2, mid + 1, e); n     if (heights[segTree[node * 2]] <= heights[segTree[1 + node * 2]]) n       segTree[node] = segTree[node * 2]; n     else n       segTree[node] = segTree[1 + node * 2]; n   } n } n long long getMin(long node, long segBeg, long segEnd, long qBeg, long qEnd) { n   if (segEnd < qBeg || segBeg > qEnd) return N + 1; n   if (segBeg >= qBeg && segEnd <= qEnd) return segTree[node]; n   long mid = (segBeg + segEnd) / 2; n   long fh = getMin(node * 2, segBeg, mid, qBeg, qEnd); n   long sh = getMin(1 + node * 2, mid + 1, segEnd, qBeg, qEnd); n   if (heights[sh] < heights[fh]) return sh; n   return fh; n } n string genMinString(long long val) { n   long len = val / 9, firstDigit = val % 9; n   string s =   ; n   if (firstDigit > 0) { n     char ch =  0  + firstDigit; n     s = ch + s; n   } n   for (long i = 0; i < len; i++) s = s +  9 ; n   return s; n } n long maxBuild; n vector<long> mult(vector<long> A, vector<long> B) { n   long len1 = A.size(), len2 = B.size(); n   vector<long> C; n   for (long i = 0; i < len1; i++) { n     long carry = 0; n     for (long j = 0; j < len2; j++) { n       long pos = i + j; n       long val = carry + (A[i] * B[j]); n       carry = val / 10; n       val = val % 10; n       if (pos >= C.size()) { n         C.push_back(val); n       } else { n         C[pos] += val; n         carry = C[pos] / 10; n         C[pos] = C[pos] % 10; n       } n     } n     if (carry > 0) C.push_back(carry); n   } n   return C; n } n vector<long> getVal(long power) { n   vector<long> C; n   C.push_back(5); n   if (power == 1) return C; n   vector<long> B = getVal(power / 2); n   B = mult(B, B); n   if (power % 2 == 1) B = mult(B, C); n   return B; n } n long getZeros(long fives) { n   long ans = 0; n   while (fives > 0) { n     ans += fives; n     fives /= 5; n   } n   return ans; n } n long long getPow(long zeros) { n   long val = ((zeros * 4) / 5) - 1; n   while (getZeros(val) < zeros) val++; n   if (getZeros(val) == zeros) return val; n   return -1; n } n long long rowVal[5050], colVal[5050]; n long long rowAlter[5050], colAlter[5050]; n int main(void) { n   long long test_cases = 1, l, r, seed = 1, maxVal; n   vector<string> originalStrings; n   long long curNum; n   long long m, n, k; n   long long ans = 0; n   for (long T = 0; T < test_cases; T++) { n     cin >> n >> m >> k; n     long rv, cv, av; n     memset(rowVal, 0, sizeof(rowVal)); n     memset(colVal, 0, sizeof(colVal)); n     memset(rowAlter, -1, sizeof(rowAlter)); n     memset(colAlter, -1, sizeof(colAlter)); n     for (long i = 0; i < k; i++) { n       scanf( %ld , &av); n       scanf( %ld , &rv); n       rv--; n       scanf( %ld , &cv); n       if (av == 1) { n         rowVal[rv] = cv; n         rowAlter[rv] = i; n       } else { n         colVal[rv] = cv; n         colAlter[rv] = i; n       } n     } n     for (long i = 0; i < n; i++) { n       for (long j = 0; j < m; j++) { n         if (rowAlter[i] > colAlter[j]) n           printf( %ld  , rowVal[i]); n         else n           printf( %ld  , colVal[j]); n       } n       cout << endl; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m; n int an[333333]; n int bit[333333]; n void upd(int x, int v) { n   for (int i = x; i <= n; i += (i & -i)) { n     bit[i] += v; n   } n } n int get(int x) { n   int an = 0; n   for (int i = x; i > 0; i -= (i & -i)) an += bit[i]; n   return an; n } n int main() { n   scanf( %d%d , &n, &m); n   for (int i = 1; i <= n; ++i) upd(i, 1); n   while (m--) { n     int from, to, win; n     scanf( %d%d%d , &from, &to, &win); n     int add = get(to); n     int dec = get(from - 1); n     while (add > dec) { n       int ll = from, rr = to; n       while (ll <= rr) { n         int mid = (ll + rr) / 2; n         if (get(mid) == dec) n           ll = mid + 1; n         else n           rr = mid - 1; n       } n       an[ll] = win; n       upd(ll, -1); n       --add; n     } n     an[win] = 0; n     upd(win, 1); n   } n   for (int i = 1; i <= n; ++i) printf( %d  , an[i]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 1e4 + 5; n const int mod = 1e9 + 7; n int main() { n   ios::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   int n; n   cin >> n; n   int a[n + 5], mx = 0; n   long long sum = 0; n   for (int i = 0; i < n; i++) cin >> a[i], sum += a[i], mx = max(mx, a[i]); n   sum -= mx; n   cout << (mx - sum + 1) << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   long long k; n   cin >> n >> k; n   int arr[n]; n   for (int i = 0; i < n; i++) cin >> arr[i]; n   sort(arr, arr + n); n   int prev = arr[0], best = -1, num, left = 0; n   for (int i = 0; i < n; i++) { n     k -= (0LL + i - left) * (arr[i] - prev); n     while (k < 0) { n       k += arr[i] - arr[left++]; n     } n     if (best < i - left + 1) { n       best = i - left + 1; n       num = arr[i]; n     } n     prev = arr[i]; n   } n   cout << best <<     << num << endl; n   return 0; n } n


#include <bits/stdc++.h> n int g[1010][1010]; n int main() { n   int n, k, ans, i, j; n   while (~scanf( %d%d , &n, &k)) { n     ans = 0; n     memset(g, 0, sizeof(g)); n     for (i = 0; i < n; i++) n       for (j = i + 1; j <= i + k; j++) g[i][j % n] = 1, ans++; n     int flag = 1; n     for (i = 0; i < n; i++) n       for (j = 0; j < n; j++) n         if (g[i][j] && g[j][i]) flag = 0; n     if (flag) { n       printf( %d n , ans); n       for (i = 0; i < n; i++) n         for (j = 0; j < n; j++) n           if (g[i][j]) printf( %d %d n , i + 1, j + 1); n     } else n       puts( -1 ); n   } n   return 0; n } n


#include <bits/stdc++.h> n struct node { n   int a, b, w; n } p[200005]; n int fa[200005]; n bool d[200005]; n bool cmp(node x, node y) { return x.w > y.w; } n int find(int x) { return x == fa[x] ? x : fa[x] = find(fa[x]); } n int main() { n   int n, m, ans = 0; n   scanf( %d%d , &n, &m); n   for (int i = 1; i <= m; i++) { n     scanf( %d%d%d , &p[i].a, &p[i].b, &p[i].w); n   } n   std::sort(p + 1, p + m + 1, cmp); n   for (int i = 1; i <= n; i++) { n     fa[i] = i; n     d[i] = true; n   } n   for (int i = 1; i <= m; i++) { n     int x = find(p[i].a); n     int y = find(p[i].b); n     if (x != y && (d[x] || d[y])) { n       fa[y] = x; n       ans += p[i].w; n       d[x] = d[x] & d[y]; n     } else if (x == y && d[x]) { n       d[x] = false; n       ans += p[i].w; n     } n   } n   printf( %d n , ans); n   return 0; n } n


#include<bits/stdc++.h> n #define ll long long int n using namespace std; n  n int t; n  n int main(){ n     ios_base::sync_with_stdio(false); n     cin.tie(NULL); n     int n; n     cin >> n; n     int sum=0; n     int odd=-1; n     vector<int> a(n); n     for (int q=0;q<n;q++){ n         int x; n         cin>>x; n         a[q]=x; n         sum+=x; n         if (odd==-1 && x%2==1){ n             odd=q; n         } n     } n     if (sum%2==1){ n         cout<<0<<  n ; n     }else{ n         int s=sum/2; n         bool dp[n+1][s+1]; n         for (int i=0;i<=n;i++){ n             dp[i][0]=true; n         } n         for (int j=1;j<=s;j++){ n             dp[0][j]=false; n         } n         for (int i=1;i<=n;i++){ n             for (int j=1;j<=s;j++){ n                 if (dp[i-1][j]==true){ n                     dp[i][j]=true; n                 }else{ n                     if (a[i-1]>j){ n                         dp[i][j]=false; n                     }else{ n                         dp[i][j]=dp[i-1][j-a[i-1]]; n                     } n                 } n             } n         } n         if (dp[n][s]==false){ n             cout<<0<<  n ; n         }else{ n             if (odd!=-1){ n                 cout<<1<<  n ; n                 cout<<odd+1<<  n ; n             }else{ n                 int idx=-1; n                 int mn=33; n                 for (int i=0;i<n;i++){ n                     if (mn > __builtin_ctz(a[i])){ n                         mn=__builtin_ctz(a[i]); n                         idx=i; n                     } n                 } n                 cout<<1<<  n ; n                 cout<<idx+1<<  n ; n             } n         } n     } n      n }


#include <bits/stdc++.h> n using namespace std; n int pre[100010]; n char f[100010]; n int a[100010]; n int ans[100010]; n int main() { n   int n, m, i, c, cnt = 0; n   scanf( %d , &n); n   scanf( %d , &m); n   for (i = 0; i < n; ++i) { n     scanf( %c , &f[i]); n     scanf( %c , &f[i]); n     scanf( %d , &a[i]); n     --a[i]; n     if (f[i] ==  + ) { n       pre[a[i]]++; n       pre[a[i] + 1]--; n     } else { n       if (a[i]) { n         pre[0]++; n         pre[a[i]]--; n       } n       pre[a[i] + 1]++; n     } n   } n   if (pre[0] == m) { n     ans[0] = 1; n     cnt++; n   } else { n     ans[0] = 0; n   } n   for (i = 1; i < n; ++i) { n     pre[i] = pre[i - 1] + pre[i]; n     if (pre[i] == m) { n       ans[i] = 1; n       cnt++; n     } else { n       ans[i] = 0; n     } n   } n   if (cnt > 1) n     for (i = 0; i < n; ++i) n       if (ans[i] == 1) ans[i] = -1; n   for (i = 0; i < n; ++i) { n     if (f[i] ==  + ) { n       if (ans[a[i]] == 1) n         printf( Truth n ); n       else if (ans[a[i]] == 0) n         printf( Lie n ); n       else n         printf( Not defined n ); n     } else { n       if (ans[a[i]] == 0) n         printf( Truth n ); n       else if (ans[a[i]] == 1) n         printf( Lie n ); n       else n         printf( Not defined n ); n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n int vis[500500]; n int a[500500]; n int b[500500]; n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   int n; n   cin >> n; n   for (int i = 0; i < n; i++) cin >> a[i]; n   for (int j = 0; j < n; j++) cin >> b[j]; n   int ans = 0; n   int j = 0; n   for (int i = 0; i < n; i++) { n     if (vis[a[i]]) continue; n     while (j < n && b[j] != a[i]) { n       vis[b[j]] = 1; n       ans++; n       j++; n     } n     if (j < n && a[i] == b[j]) j++; n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long n, u, r; n long long ans = -1e18; n long long a[35], b[35], k[35], p[35]; n void calc(long long aa[]) { n   long long mx = 0; n   for (int i = 1; i <= n; i++) mx += aa[i] * k[i]; n   ans = (ans > mx ? ans : mx); n } n void dfs(long long depth, long long aa[]) { n   if (depth == u) { n     calc(aa); n     return; n   } n   long long t1[35], t2[35]; n   for (int i = 1; i <= n; i++) t1[i] = aa[i] ^ b[i]; n   if ((u - depth) % 2) n     calc(t1); n   else n     calc(aa); n   for (int i = 1; i <= n; i++) t2[i] = aa[p[i]] + r; n   dfs(depth + 1, t2); n   if (depth <= u - 2) { n     for (int i = 1; i <= n; i++) t2[i] = t1[p[i]] + r; n     dfs(depth + 2, t2); n   } n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(); n   cout.tie(); n   cin >> n >> u >> r; n   for (int i = 1; i <= n; i++) cin >> a[i]; n   for (int i = 1; i <= n; i++) cin >> b[i]; n   for (int i = 1; i <= n; i++) cin >> k[i]; n   for (int i = 1; i <= n; i++) cin >> p[i]; n   dfs(0, a); n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   int n, q; n   cin >> n >> q; n   vector<int> res(n + 1, 0); n   vector<vector<int>> adjacenyList(n + 1); n   for (int i = 1; i < n; i++) { n     int u, v; n     cin >> u >> v; n     adjacenyList[u].emplace_back(v); n     adjacenyList[v].emplace_back(u); n   } n   function<void(int, int)> DFS = [&](int u, int val) { n     res[u] = val; n     for (int v : adjacenyList[u]) { n       if (res[v]) continue; n       DFS(v, min(v, val)); n     } n   }; n   int c = 0, l = 0; n   int t, x; n   cin >> t >> x; n   x = (x + l) % n + 1; n   c = x; n   DFS(x, x); n   while (--q) { n     cin >> t >> x; n     x = (x + l) % n + 1; n     if (t & 1) { n       c = min(c, res[x]); n     } else { n       l = min(c, res[x]); n       cout << l <<   n ; n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n int N, K; n int arr[205]; n int u[100005], v[100005], f[100005]; n int lvl[205], ptr[205]; n bool vis[205]; n vector<int> graph[205]; n vector<int> cgraph[205]; n vector<vector<int>> vec; n void addedge(int a, int b, int k) { n   u[K] = a; n   v[K] = b; n   u[K ^ 1] = b; n   v[K ^ 1] = a; n   f[K] = k; n   graph[a].push_back(K); n   graph[b].push_back(K ^ 1); n   K += 2; n } n bool bfs() { n   queue<int> qu; n   qu.push(0); n   fill(lvl, lvl + N + 2, 0); n   lvl[0] = 1; n   while (qu.size()) { n     int n = qu.front(); n     qu.pop(); n     for (int e : graph[n]) { n       if (f[e] && !lvl[v[e]]) { n         lvl[v[e]] = lvl[u[e]] + 1; n         qu.push(v[e]); n       } n     } n   } n   return lvl[N + 1]; n } n int dfs(int n, int p) { n   if (n == N + 1 || !p) { n     return p; n   } n   for (int &i = ptr[n]; i < graph[n].size(); i++) { n     int e = graph[n][i]; n     if (f[e] && lvl[v[e]] > lvl[n]) { n       int k = dfs(v[e], min(f[e], p)); n       if (!k) { n         continue; n       } n       f[e] -= k; n       f[e ^ 1] += k; n       return k; n     } n   } n   return 0; n } n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   cin >> N; n   for (int i = 1; i <= N; i++) { n     cin >> arr[i]; n     if (arr[i] % 2 == 1) { n       addedge(0, i, 2); n     } else { n       addedge(i, N + 1, 2); n     } n   } n   for (int i = 1; i <= N; i++) { n     for (int j = 1; j <= N; j++) { n       if (arr[i] % 2 == 1 && arr[j] % 2 == 0) { n         bool isp = 1; n         for (int k = 2; k <= sqrt(arr[i] + arr[j]); k++) { n           if ((arr[i] + arr[j]) % k == 0) { n             isp = 0; n             break; n           } n         } n         if (isp) { n           addedge(i, j, 1); n         } n       } n     } n   } n   int ans = 0; n   while (1) { n     if (!bfs()) { n       break; n     } n     fill(ptr, ptr + 2 + N, 0); n     int p; n     while ((p = dfs(0, INT_MAX / 2))) { n       ans += p; n     } n   } n   if (ans != N) { n     cout <<  Impossible n ; n     return 0; n   } n   for (int e = 1; e < K; e += 2) { n     if (f[e] && u[e] != 0 && v[e] != 0 && u[e] != N + 1 && v[e] != N + 1) { n       cgraph[v[e]].push_back(u[e]); n       cgraph[u[e]].push_back(v[e]); n     } n   } n   for (int n = 1; n <= N; n++) { n     if (!vis[n]) { n       int c = n; n       vector<int> temp; n       while (!vis[c]) { n         temp.push_back(c); n         vis[c] = 1; n         for (int e : cgraph[c]) { n           if (!vis[e]) { n             c = e; n           } n         } n       } n       vec.push_back(temp); n     } n   } n   cout << vec.size() <<   n ; n   for (auto k : vec) { n     cout << k.size() <<    ; n     for (int n : k) { n       cout << n <<    ; n     } n     cout <<   n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int n, k; n int main() { n   scanf( %d%d , &n, &k); n   if (k >= (n * (n - 1)) / 2) n     puts( no solution ); n   else { n     for (int i = 0, t = 1; t <= n; t++) printf( %d %d n , i, t); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<vector<int> > a; n vector<int> color; n int n, m; n long long ans = 0; n void brut(int v) { n   if (v == n) { n     set<pair<int, int> > q; n     vector<bool> find(n, false); n     for (int i = 0; i < n; ++i) { n       for (int j : a[i]) { n         if (j < i) continue; n         if (color[i] == -1 && color[j] == -1) continue; n         if (color[i] == -1 || color[j] == -1) continue; n         pair<int, int> temp = {color[i], color[j]}; n         if (temp.second < temp.first) swap(temp.first, temp.second); n         if (q.find(temp) != q.end()) continue; n         find[i] = find[j] = true; n         q.insert(temp); n         break; n       } n     } n     for (int i = 0; i < n; ++i) { n       for (int j : a[i]) { n         if (j < i) continue; n         if (color[i] == -1 && color[j] == -1) continue; n         if (color[i] == -1 || color[j] == -1) continue; n         pair<int, int> temp = {color[i], color[j]}; n         if (temp.second < temp.first) swap(temp.first, temp.second); n         if (q.find(temp) != q.end()) continue; n         find[i] = find[j] = true; n         q.insert(temp); n       } n     } n     for (int i = 0; i < n; ++i) n       if (color[i] != -1 && !find[i]) return; n     ans = max(ans, (long long)q.size()); n     return; n   } n   for (int i = -1; i < 6; ++i) { n     color[v] = i; n     brut(v + 1); n   } n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cin >> n >> m; n   a.resize(n); n   for (int i = 0; i < m; ++i) { n     int x, y; n     cin >> x >> y; n     --x; n     --y; n     a[x].push_back(y); n     a[y].push_back(x); n   } n   color.resize(n); n   brut(0); n   cout << ans <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n const long long N = 2e5 + 5; n long long n, x, y, z, ans; n long long a[N], b[N], par[N], sz[N]; n long long get(long long x) { n   if (par[x] == x) return x; n   return par[x] = get(par[x]); n } n void merge(long long x, long long y) { n   long long root_a = get(x); n   long long root_b = get(y); n   if (root_a == root_b) { n     return; n   } n   if (sz[root_a] < sz[root_b]) { n     par[root_a] = par[root_b]; n     sz[root_b] += sz[root_a]; n   } else { n     par[root_b] = par[root_a]; n     sz[root_a] += sz[root_b]; n   } n } n signed main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   long long t = 1; n   while (t--) { n     long long n; n     for (long long i = 0; i < N; i++) { n       par[i] = i; n       sz[i] = 1; n     } n     cin >> n; n     for (long long i = 1; i < n + 1; i++) cin >> a[i]; n     for (long long i = 1; i < n + 1; i++) cin >> b[i]; n     for (long long i = 1; i < n + 1; i++) { n       long long left = i - b[i]; n       long long right = i + b[i]; n       long long p1 = get(i); n       if (left >= 1) { n         long long p2 = get(left); n         if (p1 != p2) merge(p1, p2); n       } n       if (right <= n) { n         long long p2 = get(right); n         if (p1 != p2) merge(p1, p2); n       } n     } n     long long f = 0; n     for (long long i = 1; i < n + 1; i++) { n       long long p1 = get(i); n       long long p2 = get(a[i]); n       if (p1 != p2) f = 1; n     } n     if (f) n       cout <<  NO n ; n     else n       cout <<  YES n ; n   } n } n


#include <bits/stdc++.h> n #pragma GCC optimize( Ofast,no-stack-protector,unroll-loops,fast-math ) n #pragma GCC target(   n      sse,sse2,sse3,ssse3,sse4.1,sse4.2,avx,avx2,popcnt,tune=native ) n using namespace std; n template <typename Tp> n inline void read(Tp &x) { n   static char c; n   static bool neg; n   x = 0, c = getchar(), neg = false; n   for (; !isdigit(c); c = getchar()) { n     if (c ==  - ) { n       neg = true; n     } n   } n   for (; isdigit(c); c = getchar()) { n     x = x * 10 + c -  0 ; n   } n   if (neg) { n     x = -x; n   } n } n const int N = 100000 + 5; n int n, w, q; n int a[N], b[N]; n int mn[N], mx[N], ans[N]; n inline void checkMax(int &x, const int &y) { n   if (x < y) { n     x = y; n   } n } n inline void checkMin(int &x, const int &y) { n   if (x > y) { n     x = y; n   } n } n int main() { n   read(n), read(w), read(q); n   for (int i = 0; i < n; ++i) { n     read(a[i]); n   } n   for (int i = 0, x; i < q; ++i) { n     read(x); n     b[i] = w - x; n   } n   fill(mn, mn + q, a[0]); n   fill(mx, mx + q, a[0]); n   for (int j = 1; j < n; ++j) { n     for (int i = 0; i < q; ++i) { n       checkMax(mx[i], a[j]); n       checkMin(mn[i], a[j]); n       if (mx[i] - mn[i] > b[i]) { n         ++ans[i]; n         mn[i] = mx[i] = a[j]; n       } n     } n   } n   for (int i = 0; i < q; ++i) { n     printf( %d n , ans[i]); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n inline int in() { n   int x; n   scanf( %d , &x); n   return x; n } n inline void priv(vector<int> a) { n   for (int i = 0; i < ((int)(a).size()); ++i) n     printf( %d%c , a[i], i == (int)(a).size() - 1 ?   n  :    ); n } n template <typename T> n istream& operator>>(istream& i, vector<T>& v) { n   for (int j = 0; j < ((int)(v).size()); ++j) i >> v[j]; n   return i; n } n template <typename T> n string join(vector<T>& v) { n   stringstream s; n   for (int i = 0; i < ((int)(v).size()); ++i) s <<     << v[i]; n   return s.str().substr(1); n } n template <typename T> n ostream& operator<<(ostream& o, vector<T>& v) { n   if ((int)(v).size()) o << join(v); n   return o; n } n template <typename T1, typename T2> n istream& operator>>(istream& i, pair<T1, T2>& v) { n   return i >> v.first >> v.second; n } n template <typename T1, typename T2> n ostream& operator<<(ostream& o, pair<T1, T2>& v) { n   return o << v.first <<  ,  << v.second; n } n const int MX = 100005, INF = 1001001001; n const long long int LINF = 1e18; n const double eps = 1e-10; n int main() { n   int n; n   scanf( %d , &n); n   vector<int> x; n   long long int sum = 0; n   for (int i = 0; i < (n); ++i) { n     int a, b, c, d; n     scanf( %d%d%d%d , &a, &b, &c, &d); n     if (a <= d && b <= c) continue; n     long long int s = a + b, t = c + d; n     if (s <= t) { n       if (a <= d) n         sum += c - b; n       else n         sum += a - d; n     } else { n       sum += a + c; n       x.push_back(s); n       x.push_back(t); n     } n   } n   sort(x.begin(), x.end()); n   reverse(x.begin(), x.end()); n   for (int i = 0; i < ((int)(x).size()); ++i) { n     if (i & 1) sum -= x[i]; n   } n   cout << sum << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long q; n   cin >> q; n   while (q--) { n     long long n, count = 0; n     cin >> n; n     long long a[100005]; n     for (int i = 0; i < n; i++) cin >> a[i]; n     if (n == 1) n       cout <<  1  << endl; n     else { n       for (int i = 0; i < n; i++) { n         for (int j = i + 1; j < n; j++) { n           if ((a[i] - a[j]) == 1 || (a[i] - a[j] == -1)) count++; n         } n       } n       if (count > 0) n         cout <<  2  << endl; n       else n         cout <<  1  << endl; n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n int n, i, j, k, s, a[50], b[50], c[50]; n long long x; n int main() { n   scanf( %d , &n); n   for (i = 1; i <= n; i++) { n     scanf( %I64d , &x); n     for (j = 0; (1ll << j) < x; j++) n       ; n     if ((1ll << j) == x) n       a[j]++; n     else n       b[j - 1]++; n   } n   for (i = a[0]; i > 0; i--) { n     k = i; n     s = 0; n     for (j = 0; j <= 40; j++) { n       k = min(k, a[j]); n       c[j] = k; n     } n     for (j = 40; j >= 0; j--) { n       s += b[j] + a[j] - c[j]; n       if (s > c[j]) break; n     } n     if (j >= 0) break; n   } n   if (i == a[0]) n     cout <<  -1 ; n   else n     for (i++; i <= a[0]; i++) cout << i <<    ; n   return 0; n } n


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


#include <bits/stdc++.h> n using namespace std; n  n #define ll long long int n #define pll pair<ll, ll> n #define vl vector<ll> n #define pb push_back n #define pp pop_back n #define f(i, a, b) for (ll i = a; i < b; ++i) n #define rev(i, a, b) for (ll i = a; i >= b; --i) n #define print(x) cout << x << endl n #define endl   n  n #define F first n #define S second n #define setbits(x) __builtin_popcountll(x) n #define zerobits(x) __builtin_ctzll(x) n #define to(n) to_string(n) n #define low(v, n) lower_bound(v.begin(), v.end(), n) - v.begin() n #define upp(v, n) upper_bound(v.begin(), v.end(), n) - v.begin() n #define mod 998244353 n #define mex 100005 n  n ll power(ll x, ll y) n { n     if (y == 0) n     { n         return 1; n     } n     if (y % 2 == 0) n     { n         return power((x % mod) * (x % mod), y / 2) % mod; n     } n     else n     { n         return ((x % mod) * power((x % mod) * (x % mod), y / 2) % mod); n     } n } n  n ll modinverse(ll a, ll m) n { n     return power(a, m - 2); n } n  n ll ncr(ll n, ll r) n { n     ll k = min(r, n - r); n     ll result = 1; n     ll divide = 1; n  n     f(i, 1, k + 1) n     { n         result = ((result % mod) * ((n + 1 - i) % mod)) % mod; n         divide = ((divide % mod) * (i % mod)) % mod; n     } n  n     result = (result * modinverse(divide, mod)) % mod; n     return result; n } n  n int main() n { n     ios_base::sync_with_stdio(0); n     cin.tie(0); n     cout.tie(0); n  n     ll t; n     cin >> t; n     while (t--) n     { n         ll n; n         cin >> n; n         string s; n         cin >> s; n  n         ll p = 0; n         ll q = 0; n         ll i = 0; n         while (i < n) n         { n             if (s[i] ==  0 ) n             { n                 p++; n                 ++i; n             } n             else if (s[i] ==  1  && i + 1 < n && s[i + 1] ==  1 ) n             { n                 q++; n                 i += 2; n             } n             else n             { n                 ++i; n             } n         } n  n         print(ncr(p + q, p)); n     } n  n     return 0; n }


#include <bits/stdc++.h> n using namespace std; n vector<pair<int, int> > v; n int N, M; n long long f(int a, int b) { n   long long res = 1; n   for (int i = 1; i <= a; i++) { n     int ii = i; n     while (b > 0 && ii % 2 == 0) { n       ii /= 2; n       b--; n     } n     res = (res * ii) % M; n   } n   return res; n } n int main() { n   scanf( %d , &N); n   for (int i = 0; i < N; i++) { n     int x; n     scanf( %d , &x); n     v.push_back(make_pair(x, i + 1)); n   } n   for (int i = 0; i < N; i++) { n     int x; n     scanf( %d , &x); n     v.push_back(make_pair(x, i + 1)); n   } n   scanf( %d , &M); n   sort(v.begin(), v.end()); n   long long res = 1; n   int cnt = 0; n   int b = 0; n   for (int i = 0; i < N * 2; i++) { n     if (i > 0 && v[i].first != v[i - 1].first) { n       res = (res * f(cnt, b)) % M; n       cnt = 1; n       b = 0; n     } else { n       if (i != 0 && v[i] == v[i - 1]) b++; n       cnt++; n     } n   } n   res = (res * f(cnt, b)) % M; n   cout << res <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n int main() { n   int a, b; n   scanf( %d  %d , &a, &b); n   int diff, same, total; n   total = a + b; n   if (a > b) { n     diff = b; n   } else { n     diff = a; n   } n   same = (total - (2 * diff)) / 2; n   printf( %d  %d n , diff, same); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 40005; n const int M = 1005; n const int Q = 1000000007; n const int P = 10000019; n int n, m, q, tot; n int v[N], w[N], Ask[N], f[N], id[N]; n int g[M], dp[N * 4]; n vector<int> T[N * 4]; n void Build(int s, int x, int y, int l, int r, int k) { n   if (l <= x && y <= r) { n     T[s].push_back(k); n     return; n   } n   int Mid = (x + y) / 2; n   int t = s + s; n   if (l <= Mid) Build(t, x, Mid, l, r, k); n   if (Mid < r) Build(t + 1, Mid + 1, y, l, r, k); n } n void Make(int s, int x, int y) { n   if (x == y) { n     dp[s] = Ask[x]; n     return; n   } n   int Mid = (x + y) / 2; n   int t = s + s; n   Make(t, x, Mid); n   Make(t + 1, Mid + 1, y); n   dp[s] = dp[t] | dp[t + 1]; n } n void Add(int dp[M], int y, int x) { n   for (int i = m; i >= x; i--) dp[i] = max(dp[i], dp[i - x] + y); n } n void print() { n   int Ans = 0; n   for (int i = m; i; i--) Ans = (1LL * P * Ans + g[i]) % Q; n   printf( %d n , Ans); n } n void Work(int s, int x, int y) { n   if (!dp[s]) return; n   int Mid = (x + y) / 2; n   int tmp[N]; n   for (int i = 0; i <= m; i++) tmp[i] = g[i]; n   for (int j = 0; j < T[s].size(); j++) Add(g, v[T[s][j]], w[T[s][j]]); n   if (x == y) { n     print(); n     for (int i = 0; i <= m; i++) g[i] = tmp[i]; n     return; n   } n   int t = s + s; n   Work(t, x, Mid); n   Work(t + 1, Mid + 1, y); n   for (int i = 0; i <= m; i++) g[i] = tmp[i]; n } n int main() { n   scanf( %d%d , &n, &m); n   for (int i = 1; i <= n; i++) { n     scanf( %d%d , &v[i], &w[i]); n     f[i] = 1; n     id[i] = i; n   } n   scanf( %d , &q); n   tot = n; n   for (int i = 1; i <= q; i++) { n     int x; n     scanf( %d , &x); n     if (x == 1) { n       tot++; n       scanf( %d%d , &v[tot], &w[tot]); n       f[tot] = 1; n       id[tot] = n + i; n     } else if (x == 2) { n       int y; n       scanf( %d , &y); n       Build(1, 1, n + q, id[y], n + i, y); n       f[y] = 0; n     } else if (x == 3) { n       Ask[n + i] = 1; n     } n   } n   for (int i = 1; i <= n; i++) n     if (f[i]) Add(g, v[i], w[i]); n   for (int i = n + 1; i <= tot; i++) n     if (f[i]) Build(1, 1, n + q, id[i], n + q, i); n   Make(1, 1, n + q); n   Work(1, 1, n + q); n   return 0; n } n