#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   cin >> n; n   int c1 = 0; n   int c2 = 0; n   int a[1000]; n   for (int i = 1;; i++) { n     if (i % 2 == 0) { n       a[i] = (i * i) / 2; n     } else { n       a[i] = (i / 2) * (i / 2) + ceil(i / 2.0) * ceil(i / 2.0); n     } n     if (a[i] > 200) break; n   } n   for (int i = 0; i < n; i++) { n     string s1, s2; n     cin >> s1 >> s2; n     if (s2 ==  soft ) n       c1++; n     else n       c2++; n   } n   if (c1 == c2 && c1 == 1) n     cout << 2; n   else if (c1 == 1 && c2 == 0) n     cout << 1; n   else if (c2 == 1 && c1 == 0) n     cout << 1; n   else { n     int x = max(c1, c2); n     for (int i = 1;; i++) { n       if (a[i] < x && x <= a[i + 1]) { n         cout << i + 1; n         return 0; n       } n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e5 + 10; n int n, L, a; n int t[N], l[N]; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   srand(time(NULL)); n   cin >> n >> L >> a; n   int res = 0; n   for (int i = 1; i <= n; i++) { n     cin >> t[i] >> l[i]; n     int len = t[i] - (t[i - 1] + l[i - 1]); n     res += len / a; n   } n   res += (L - (t[n] + l[n])) / a; n   cout << res; n } n


#include <bits/stdc++.h> n using namespace std; n const int kMaxN = 100010; n const int kMaxA = 1000000010; n int a[kMaxN]; n signed main() { n   cin.tie(0); n   ios::sync_with_stdio(false); n   long long n; n   cin >> n; n   for (long long i = 0; i < n; i++) { n     cin >> a[i]; n   } n   sort(a, a + n); n   long long ans = 0; n   for (long long x = 1; (1LL << x) < 2 * kMaxA; x++) { n     long long val = (1 << x); n     for (long long i = 0; i < n; i++) { n       long long num = upper_bound(a + i + 1, a + n, val - a[i]) - n                       lower_bound(a + i + 1, a + n, val - a[i]); n       ans += num; n     } n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MOD = 1e9 + 7; n const int MAXN = 1e6 + 50; n const int INF = 0x3f3f3f3f; n void bf(int A, int B) { n   for (long long a = 1; a <= A; a++) n     for (long long b = 1; b <= B; b++) { n       if ((a * b + a + b) == stol(to_string(a) + to_string(b))) n         cout << a <<     << b << endl; n     } n } n int main() { n   int cs; n   while (cin >> cs) { n     while (cs--) { n       int a, b; n       cin >> a >> b; n       long long ret = to_string(b + 1).size() - 1; n       cout << a * ret << endl; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T> n using min_pq = priority_queue<T, vector<T>, greater<T>>; n template <typename T> n using max_pq = priority_queue<T>; n const int inf = 2e9 + 5; n const long long l_inf = 2e18 + 5; n const int mod_v = 1e9 + 7; n const int max_n = 1e5 + 5; n const int dx[4] = {-1, 0, 1, 0}; n const int dy[4] = {0, 1, 0, -1}; n template <typename T> n T gcd(T a, T b) { n   while (b) { n     T temp = a % b; n     a = b; n     b = temp; n   } n   return a; n } n template <typename T> n tuple<T, T, T> egcd(T a, T b) { n   T x1 = 1, x2 = 0, y1 = 0, y2 = 1; n   while (b) { n     T q = a / b, r = a % b; n     T new_x = x1 - q * x2, new_y = y1 - q * y2; n     x1 = x2, y1 = y2, x2 = new_x, y2 = new_y; n     a = b, b = r; n   } n   return make_tuple(a, x1, y1); n } n inline long long lcm(long long a, long long b) { return a * b / gcd(a, b); } n template <typename T> n inline T mod(T a, T b = mod_v) { n   return (a % b + b) % b; n } n template <typename T> n inline T mod_inv(T a, T b = mod_v) { n   return mod(get<1>(egcd(a, b)), b); n } n template <typename T> n inline T sum(T a, T b, T m = mod_v) { n   return mod(mod(a, m) + mod(b, m), m); n } n template <typename T> n inline T difference(T a, T b, T m = mod_v) { n   return mod(mod(a, m) - mod(b, m), m); n } n inline long long product(long long a, long long b, long long m = mod_v) { n   return mod(mod(a, m) * mod(b, m), m); n } n inline long long quotient(long long a, long long b, long long m = mod_v) { n   return mod(mod(a, m) * mod_inv(b, m), m); n } n template <typename T, typename T2> n ostream &operator<<(ostream &s, const pair<T, T2> &p) { n   return s << p.first <<     << p.second <<    ; n } n template <typename T, typename T2> n istream &operator>>(istream &s, pair<T, T2> &p) { n   return s >> p.first >> p.second; n } n template <typename T> n ostream &operator<<(ostream &s, const vector<T> &v) { n   for (auto it : v) s << it <<    ; n   return s; n } n template <typename T> n istream &operator>>(istream &s, vector<T> &v) { n   for (auto it = (v).begin(), it_ = (v).end(); it != it_; ++it) s >> *it; n   return s; n } n template <typename T> n void read_range(T beg, T end) { n   while (beg != end) cin >> *beg++; n } n template <typename T> n void print_range(T beg, T end) { n   while (beg != end) cout << *beg++ <<    ; n } n struct reader { n   template <typename T> n   reader &operator,(T &v) { n     cin >> v; n     return *this; n   } n } rdr; n struct debugger { n   template <typename T> n   debugger &operator,(const T &v) { n     cerr << v <<  ,  ; n     return *this; n   } n } dbg; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   int x[2]; n   cin >> x[0] >> x[1]; n   int a[2] = {0, 0}; n   for (int i = 0;; ++i) { n     int n_a; n     if (i / 2 % 2) n       n_a = a[i % 2] - (i / 2 + 1); n     else n       n_a = a[i % 2] + (i / 2 + 1); n     if (a[1 - i % 2] == x[1 - i % 2] and n         ((x[i % 2] >= a[i % 2] and x[i % 2] <= n_a) or n          (x[i % 2] <= a[i % 2] and x[i % 2] >= n_a))) { n       cout << i; n       return 0; n     } n     a[i % 2] = n_a; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int m, n, rowStart[1010], rowEnd[1010], colStart[1010], colEnd[1010], n     sum[1010][1010]; n int startX = -1, startY; n string a[1010]; n void quit() { n   cout << -1 << endl; n   exit(0); n } n int getSum(int x, int y, int row, int col) { n   return sum[x][y] + sum[x - row][y - col] - sum[x - row][y] - sum[x][y - col]; n } n int ok(int row, int col) { n   int x = startX + row - 1, y = startY + col - 1, covered = col * row; n   while (1) { n     if (x < m && getSum(x + 1, y, row, col) == row * col) { n       x++; n       covered += col; n     } else if (y < n && getSum(x, y + 1, row, col) == row * col) { n       y++; n       covered += row; n     } else n       break; n   } n   return covered == sum[m][n]; n } n void solve() { n   int sideRow = colEnd[startY] - colStart[startY] + 1; n   int sideCol = rowEnd[startX] - rowStart[startX] + 1; n   int ans = m * n + 1; n   for (int col = 1; col <= sideCol; col++) n     if (ok(sideRow, col)) { n       ans = min(ans, sideRow * col); n       break; n     } n   for (int row = 1; row <= sideRow; row++) n     if (ok(row, sideCol)) { n       ans = min(ans, sideCol * row); n       break; n     } n   if (ans > m * n) ans = -1; n   cout << ans << endl; n } n int main() { n   ios::sync_with_stdio(0); n   cin >> m >> n; n   for (int i = 1; i <= m; i++) { n     cin >> a[i]; n     rowStart[i] = rowEnd[i] = -1; n     int cntX = 0; n     for (int j = 1; j <= n; j++) { n       sum[i][j] = sum[i - 1][j] + sum[i][j - 1] - sum[i - 1][j - 1]; n       if (a[i][j - 1] ==  X ) { n         if (startX < 0) { n           startX = i; n           startY = j; n         } n         if (rowStart[i] < 0) rowStart[i] = j; n         rowEnd[i] = j; n         cntX++; n         sum[i][j]++; n       } n     } n     if (rowStart[i] > 0 && cntX != rowEnd[i] - rowStart[i] + 1) quit(); n   } n   for (int j = 1; j <= n; j++) { n     colStart[j] = colEnd[j] = -1; n     int cntX = 0; n     for (int i = 1; i <= m; i++) n       if (a[i][j - 1] ==  X ) { n         if (colStart[j] < 0) colStart[j] = i; n         colEnd[j] = i; n         cntX++; n       } n     if (colStart[j] > 0 && cntX != colEnd[j] - colStart[j] + 1) quit(); n   } n   solve(); n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T, typename T1> n T amax(T a, T1 b) { n   if (b > a) a = b; n   return a; n } n template <typename T, typename T1> n T amin(T a, T1 b) { n   if (b < a) a = b; n   return a; n } n template <typename T> n T amod(T a) { n   if (a < 0) a = -a; n   return a; n } n void solve() { n   long long int n, s, a, sum = 0; n   cin >> n >> s; n   long long int maxs = 0; n   for (long long int i = 0; i < n; i++) { n     cin >> a; n     sum += a; n     maxs = max(a, maxs); n   } n   if ((sum - maxs) <= s) { n     cout <<  YES ; n   } else { n     cout <<  NO ; n   } n } n int main() { n   long long int t; n   t = 1; n   while (t--) { n     solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int ans[21][21]; n int cnt[1100]; n int n; n vector<int> fang, four; n void output() { n   for (int i = 1; i <= n; ++i) { n     for (int j = 1; j <= n; ++j) { n       cout << ans[i][j] <<    ; n     } n     cout << endl; n   } n } n bool check() { n   int sum = 0; n   for (int i = 0; i < fang.size(); ++i) { n     if (cnt[fang[i]] >= 4) { n       four.push_back(fang[i]); n       sum += cnt[fang[i]] / 4; n     } n   } n   return sum >= ((n / 2) * (n / 2)); n } n int main() { n   int num; n   cin >> n; n   for (int i = 0; i < n * n; ++i) { n     cin >> num; n     cnt[num]++; n   } n   int flag = 1, flag2 = 0, lcqi; n   for (int i = 1; i <= 1000; ++i) { n     if (cnt[i]) { n       fang.push_back(i); n       if (cnt[i] % 4) { n         flag = 0; n       } n       if (cnt[i] & 1) flag2++, lcqi = fang.size() - 1; n     } else n       continue; n   } n   if ((n & 1) == 0) { n     if (flag) { n       cout <<  YES n ; n       int lc = 0; n       for (int i = 1; i <= n / 2; ++i) { n         for (int j = 1; j <= n / 2; ++j) { n           ans[i][j] = ans[n - i + 1][j] = ans[i][n - j + 1] = n               ans[n - i + 1][n - j + 1] = fang[lc]; n           cnt[fang[lc]] -= 4; n           if (cnt[fang[lc]] == 0) { n             lc++; n           } n         } n       } n       output(); n     } else n       cout <<  NO n ; n   } else { n     if (flag2 != 1) { n       cout <<  NO n ; n       return 0; n     } n     ans[n / 2 + 1][n / 2 + 1] = fang[lcqi]; n     cnt[fang[lcqi]]--; n     int ok = check(); n     if (!ok) { n       puts( NO n ); n       return 0; n     } n     cout <<  YES n ; n     int lcf = 0; n     for (int i = 1; i <= n / 2; ++i) { n       for (int j = 1; j <= n / 2; ++j) { n         ans[i][j] = ans[n - i + 1][j] = ans[i][n - j + 1] = n             ans[n - i + 1][n - j + 1] = four[lcf]; n         cnt[four[lcf]] -= 4; n         if (cnt[four[lcf]] < 4) { n           lcf++; n         } n       } n     } n     lcf = 0; n     while (lcf < fang.size() && cnt[fang[lcf]] == 0) { n       lcf++; n     } n     for (int i = 1; i <= n / 2; ++i) { n       ans[i][n / 2 + 1] = ans[n - i + 1][n / 2 + 1] = fang[lcf]; n       cnt[fang[lcf]] -= 2; n       if (cnt[fang[lcf]] == 0) { n         while (lcf < fang.size() && cnt[fang[lcf]] == 0) { n           lcf++; n         } n       } n     } n     for (int i = 1; i <= n / 2; ++i) { n       ans[n / 2 + 1][i] = ans[n / 2 + 1][n - i + 1] = fang[lcf]; n       cnt[fang[lcf]] -= 2; n       if (cnt[fang[lcf]] == 0) { n         while (lcf < fang.size() && cnt[fang[lcf]] == 0) { n           lcf++; n         } n       } n     } n     output(); n   } n   return 0; n } n


#include <bits/stdc++.h> n const int mod = 1000000007; n const int modx = 998244353; n using namespace std; n void thunder() { n   long long n; n   cin >> n; n   vector<long long> a(n), b(n); n   long long i, j; n   for (i = 0; i < n; i++) cin >> a[i]; n   map<long long, long long> m; n   for (i = 0; i < n; i++) { n     long long idx = i + a[i % n]; n     idx %= n; n     idx += n; n     idx %= n; n     if (m.count(idx) == 0) n       m[idx]++; n     else { n       cout <<  NO  n            <<   n ; n       return; n     } n   } n   cout <<  YES  n        <<   n ; n   return; n } n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   long long t = 1; n   cin >> t; n   while (t--) thunder(); n } n


#include <bits/stdc++.h> n using namespace std; n long long sums[30000], dp[25][3000][25], can[3000], n, i, j, k, x, y, a[300]; n int main() { n   cin >> n; n   for (i = 0; i < n; i++) cin >> a[i]; n   sort(a, a + n); n   reverse(a, a + n); n   for (i = 0; i < n; i++) n     if (a[i] == 1) { n       k = i; n       break; n     } n   if (k == 0) { n     if (n == 1) n       cout <<  YES  << endl; n     else n       cout <<  NO  << endl; n     return 0; n   } n   if (k > 11) { n     cout <<  NO  << endl; n     return 0; n   } n   for (i = 0; i < (1 << k); i++) { n     long long cnt = 0; n     for (j = 0; j < k; j++) n       if (i & (1 << j)) sums[i] += a[j], cnt++; n     can[i] = cnt; n   } n   dp[0][1][0] = 1; n   for (i = 0; i < k; i++) n     for (j = 0; j < (1 << k); j++) n       if (j & ((1 << (i + 1)) - 1) == (1 << (i + 1)) - 1) n         for (x = 0; x <= n - k; x++) { n           long long tmp = (1 << k) - 1 - j; n           for (y = tmp;; y = ((y - 1) & tmp)) { n             if (a[i] - sums[y] - 1 >= 0 && can[y] + a[i] - sums[y] - 1 != 1) { n               if (dp[i][j][x] == 1 && x + a[i] - sums[y] - 1 <= n - k && n                   a[i] - sums[y] - 1 >= 0) { n                 dp[i + 1][j | y][x + a[i] - sums[y] - 1] = 1; n               } n             } n             if (y == 0) break; n           } n         } n   if (dp[k][(1 << k) - 1][n - k]) n     cout <<  YES  << endl; n   else n     cout <<  NO  << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e5 + 5; n const int mod = 1e9 + 7; n int n, a[N]; n vector<int> get_divisors(int u) { n   vector<int> res; n   for (int i = 1; i * i <= u; ++i) { n     if (u % i == 0) { n       res.push_back(i); n       if (i != u / i) res.push_back(u / i); n     } n   } n   sort(res.begin(), res.end()); n   return res; n } n int add(int a, int b) { return (a + b) % mod; } n int sub(int a, int b) { n   long long result = a - b; n   if (result < 0) result += mod; n   return result; n } n long long mul(long long a, long long b) { return (a * b) % mod; } n int modpow(int base, int exp) { n   long long res = 1; n   while (exp > 0) { n     if (exp % 2) res = (res * base) % mod; n     base = (1ll * base * base) % mod; n     exp /= 2; n   } n   return (int)res; n } n int main() { n   cin >> n; n   for (int i = 0; i < n; i++) cin >> a[i]; n   sort(a, a + n); n   long long res = 0; n   for (int i = 1; i <= a[n - 1]; ++i) { n     vector<int> divisors = get_divisors(i); n     int prev = -1, dsz = divisors.size(); n     long long toAdd = 1; n     for (int j = 1; j < dsz; ++j) { n       int pos = lower_bound(a, a + n, divisors[j]) - a - 1; n       int count = pos - prev; n       toAdd = mul(toAdd, modpow(j, count)); n       prev = pos; n     } n     int count = n - 1 - prev; n     assert(count > 0); n     long long added_value = modpow(dsz, count); n     added_value = sub(added_value, modpow(dsz - 1, count)); n     toAdd = mul(toAdd, added_value); n     res = add(res, toAdd); n   } n   cout << res << endl; n   return 0; n } n


#include <bits/stdc++.h> n #pragma GCC optimize( Ofast ) n using namespace std; n long int mod = 1e9 + 7; n long int i, j, n, t, m, k; n vector<pair<long int, long int> > a, b; n set<long int> c; n bool cmp(const pair<long int, long int> &l, const pair<long int, long int> &r) { n   return l.first < r.first; n } n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cin >> n >> m; n   for (long int i = 0; i < m; i++) { n     cin >> j; n     a.push_back(make_pair(j, i)); n     c.insert(j); n   } n   sort(a.begin(), a.end()); n   for (long int i = 0; i < a.size(); i++) { n     if ((i + 1) < a.size()) { n       long int rag = a[i].second, bha = a[i].second; n       j = i + 1; n       while (a[i].first == a[j].first) { n         rag = max(rag, a[j].second); n         bha = min(bha, a[j].second); n         j++; n         if (j >= a.size()) break; n       } n       i = j - 1; n       b.push_back(make_pair(a[i].first, bha)); n       b.push_back(make_pair(a[i].first, rag)); n     } else { n       b.push_back(make_pair(a[i].first, a[i].second)); n       b.push_back(make_pair(a[i].first, a[i].second)); n     } n   } n   for (long int i = 1; i < n + 1; i++) { n     long int l, rag = -1, bha = -1, rag1 = -1, bha1 = -1; n     l = lower_bound(b.begin(), b.end(), pair<long int, long int>(i, 0), cmp) - n         b.begin(); n     if (l >= b.size()) l--; n     if (b[l].first == i) { n       bha = b[l].second; n       rag = b[l + 1].second; n     } n     l = lower_bound(b.begin(), b.end(), pair<long int, long int>(i + 1, 0), n                     cmp) - n         b.begin(); n     if (l >= b.size()) l--; n     if (b[l].first == i + 1) { n       bha1 = b[l].second; n       rag1 = b[l + 1].second; n     } n     if ((rag == -1 && bha == -1) && (rag1 == -1 && bha1 == -1)) n       continue; n     else if ((rag != -1 && bha != -1) && (rag1 != -1 && bha1 != -1)) { n       if (bha < rag1) t++; n       if (bha1 < rag) t++; n     } n   } n   t += c.size(); n   t = ((3 * n) - 2) - t; n   cout << t; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n using ll = long long; n using pii = pair<ll, ll>; n const ll MOD = 1e9 + 7, N = 1e5 + 10; n const ll LG = log2(N) + 1; n struct LCA { n   vector<vector<ll>> G, parent; n   vector<ll> tin, tout, depth; n   ll timer; n   ll n; n   ll LG; n   LCA(vector<vector<ll>> G, ll R = 1) { n     this->G = G; n     this->n = G.size(); n     this->LG = log2(n) + 1; n     this->timer = 0; n     tin.assign(n, 0); n     tout.assign(n, 0); n     depth.assign(n, 0); n     tin[0] = ++timer; n     parent = vector<vector<ll>>(n, vector<ll>(LG)); n     dfs(R); n     tout[0] = ++timer; n     compute(); n   } n   void dfs(ll u, ll p = 0, ll d = 0) { n     parent[u][0] = p; n     tin[u] = ++timer; n     depth[u] = d; n     for (auto &v : G[u]) { n       if (v != p) { n         dfs(v, u, d + 1); n       } n     } n     tout[u] = ++timer; n   } n   void compute() { n     for (ll j = 1; j < LG; j++) { n       for (ll i = 1; i < n; i++) { n         if (parent[i][j - 1]) { n           parent[i][j] = parent[parent[i][j - 1]][j - 1]; n         } n       } n     } n   } n   bool is_anc(ll u, ll v) { return tin[u] <= tin[v] and tout[v] <= tout[u]; } n   ll lca(ll u, ll v) { n     if (is_anc(u, v)) return u; n     if (is_anc(v, u)) return v; n     for (ll i = LG - 1; i >= 0; i--) { n       if (!is_anc(parent[u][i], v)) { n         u = parent[u][i]; n       } n     } n     u = parent[u][0]; n     return u; n   } n   ll dist(ll u, ll v) { return depth[u] + depth[v] - 2 * depth[lca(u, v)]; } n }; n int32_t main() { n   ios::sync_with_stdio(false); n   cin.tie(NULL); n   ll n, q; n   cin >> n >> q; n   vector<vector<ll>> G(n + 1); n   for (ll i = 2; i < n + 1; i++) { n     ll p; n     cin >> p; n     G[i].push_back(p); n     G[p].push_back(i); n   } n   LCA lc(G); n   while (q--) { n     ll a, b, c; n     cin >> a >> b >> c; n     vector<ll> A = {a, b, c}; n     sort(A.begin(), A.end()); n     ll ans = 0; n     ll cn = 0; n     do { n       cn++; n       ll s = A[0], f = A[1], t = A[2]; n       if (lc.lca(s, t) == lc.lca(f, t)) { n         if (lc.depth[lc.lca(s, t)] <= lc.depth[t]) { n           ans = max(ans, lc.depth[t] + lc.depth[lc.lca(s, f)] - n                              2 * lc.depth[lc.lca(s, t)]); n         } n       } else { n         if (lc.depth[lc.lca(s, t)] < lc.depth[t] and n             lc.depth[lc.lca(f, t)] < lc.depth[t]) { n           ans = max(ans, lc.depth[t] - max(lc.depth[lc.lca(s, t)], n                                            lc.depth[lc.lca(f, t)])); n         } n       } n     } while (next_permutation(A.begin(), A.end())); n     cout << ans + 1 <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 154321; n int n, m, r, k, row[N], col[N]; n long long ans = 0; n struct node { n   int a, b; n   long long val; n   bool operator<(const node &rhs) const { return val < rhs.val; } n }; n priority_queue<node> pq; n int main() { n   cin >> n >> m >> r >> k; n   for (int i = 1; i <= n; ++i) { n     int lo = max(1, i - r + 1); n     int hi = min(n - r + 1, i); n     row[i] = (hi - lo + 1); n   } n   for (int i = 1; i <= m; ++i) { n     int lo = max(1, i - r + 1); n     int hi = min(m - r + 1, i); n     col[i] = (hi - lo + 1); n   } n   sort(row + 1, row + n + 1); n   sort(col + 1, col + m + 1); n   for (int i = 1; i <= n; ++i) { n     node cur; n     cur.a = i; n     cur.b = m; n     cur.val = (1LL * row[i] * col[m]); n     pq.push(cur); n   } n   for (int i = 1; i <= k; ++i) { n     node cur = pq.top(); n     pq.pop(); n     ans += cur.val; n     if (cur.b > 1) { n       --cur.b; n       cur.val = (1LL * row[cur.a] * col[cur.b]); n       pq.push(cur); n     } n   } n   cout << fixed << setprecision(12); n   cout << (long double)ans / (long double)(1LL * (n - r + 1) * (m - r + 1)) n        <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n mt19937 rnd(time(0)); n const long long inf = 0x3f3f3f3f3f3f3f3fLL; n const long long N = 1e5 + 10; n const long long MOD = 1e9 + 7; n long long n, m, s; n vector<vector<long long>> g(N); n bool vis[N][2]; n long long par[N][2], mark[N]; n void dfs(long long u, long long f) { n   vis[u][f] = 1; n   for (auto i : g[u]) { n     if (!vis[i][f ^ 1]) { n       par[i][f ^ 1] = u; n       dfs(i, f ^ 1); n     } n   } n } n bool cyc(long long u) { n   mark[u] = 1; n   for (auto i : g[u]) { n     if (mark[i] == 1) return true; n     if (mark[i] == 0 && cyc(i)) return true; n   } n   mark[u] = 2; n   return false; n } n int32_t main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   cin >> n >> m; n   long long sz, x; n   for (long long i = 1; i <= n; i++) { n     cin >> sz; n     for (long long j = 0; j < sz; j++) { n       cin >> x; n       g[i].push_back(x); n     } n   } n   cin >> s; n   dfs(s, 0); n   for (long long i = 1; i <= n; i++) { n     if ((long long)g[i].size() == 0 && vis[i][1]) { n       vector<long long> res; n       long long node = i, f = 1; n       res.push_back(node); n       while (node != s || f != 0) { n         node = par[node][f]; n         f ^= 1; n         res.push_back(node); n       } n       reverse(res.begin(), res.end()); n       cout <<  Win  <<   n ; n       for (auto i : res) cout << i <<    ; n       cout <<   n ; n       return 0; n     } n   } n   if (cyc(s)) n     cout <<  Draw  <<   n ; n   else n     cout <<  Lose  <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const double EPS = 1e-8; n int n, t, h, s; n char road[500001]; n int H[500001], S[500001]; n bool time(int k) { n   vector<pair<int, int> > step; n   step.push_back(make_pair(-1, -1)); n   for (int i = 0, j = 0; i < h; i++) { n     if (k) { n       k--; n       step.push_back(make_pair(H[i], H[i])); n     } else if (H[i] > S[j]) { n       step.push_back(make_pair(H[i], H[i])); n       j++; n     } else { n       int l = (int)(step.size()) - 1; n       if (H[i] < step[l].second) n         step[l].second = S[j]; n       else n         step.push_back(make_pair(H[i], S[j])); n       j++; n     } n   } n   int tt = 0; n   for (int i = 1; i < (int)(step.size()); i++) { n     tt += n         (step[i].second - step[i - 1].first) + (step[i].second - step[i].first); n   } n   tt = min(tt, (n * 2) - 1); n   return tt <= t; n } n int main() { n   scanf( %d %d n , &n, &t); n   scanf( %s n , road); n   h = 0, s = 0; n   for (int i = 0; i < n; i++) { n     if (road[i] ==  H ) H[h++] = i; n     if (road[i] ==  S ) S[s++] = i; n   } n   if (H[h - 1] >= t) { n     cout << -1 << endl; n     return 0; n   } n   int r = max(h - s, 0), e = h; n   while (r < e) { n     int mi = (r + e) >> 1; n     (time(mi)) ? e = mi : r = mi + 1; n   } n   cout << e << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int M = 1000500; n int n, m; n bool vis[M]; n int fa[M], deg[M], in[M]; n inline int read() { n   int x = 0; n   char ch = getchar(); n   while (ch >  9  || ch <  0 ) ch = getchar(); n   while (ch >=  0  && ch <=  9 ) x = x * 10 + ch -  0 , ch = getchar(); n   return x; n } n inline int find(int x) { n   if (x != fa[x]) return fa[x] = find(fa[x]); n   return x; n } n inline void unionn(int a, int b) { n   int r1 = find(a), r2 = find(b); n   if (r1 != r2) fa[r2] = r1; n   return; n } n inline void constt() { n   for (int i = 1; i <= n; i++) fa[i] = i, vis[i] = in[i] = deg[i] = 0; n   return; n } n int main() { n   while (scanf( %d%d , &n, &m) != EOF) { n     constt(); n     vis[1] = 1; n     for (int i = 1; i <= m; i++) { n       int u = read(), v = read(); n       if (u != v) { n         in[u]++, in[v]++; n         vis[u] = vis[v] = 1; n         unionn(u, v); n       } else n         vis[u] = 1; n     } n     for (int i = 1; i <= n; i++) n       if (in[i] & 1) deg[find(i)]++; n     int ans = 0, que = 0, cnt = 0; n     for (int i = 1; i <= n; i++) n       if (vis[i] && find(i) == i) { n         if (deg[i]) n           ans += deg[i]; n         else n           que++; n         cnt++; n       } n     if (cnt == 1) n       printf( %d n , ans / 2); n     else n       printf( %d n , ans / 2 + que); 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 x = 0, y = 0, z = 0, a, b, c; n   for (int i = 0; i < n; i++) { n     cin >> a >> b >> c; n     x += a; n     y += b; n     z += c; n   } n   if (x == 0 and y == 0 and z == 0) n     cout <<  YES  << endl; n   else n     cout <<  NO  << endl; n } n


#include <bits/stdc++.h> n #pragma comment(linker,  /STACK:16777216 ) n using namespace std; n int a, b, c, d, i, j, n, m, k, g, s; n long long ans; n pair<pair<int, int>, pair<int, int> > edges[50001]; n vector<int> sm[201]; n int cs[201][201]; n pair<int, int> cur[201]; n bool found; n bool used[201]; n inline void removeSm(int oa, int ob) { n   for (int _n(((int)((sm[oa]).size())) - 1), i(0); i <= _n; i++) { n     if (sm[oa][i] == ob) { n       swap(sm[oa][i], sm[oa].back()); n       sm[oa].pop_back(); n       return; n     } n   } n } n inline void replaceTreeEdge(int oa, int ob, int a, int b, int c) { n   removeSm(oa, ob); n   removeSm(ob, oa); n   sm[a].push_back(b); n   sm[b].push_back(a); n   cs[a][b] = cs[b][a] = c; n   cs[oa][ob] = cs[ob][oa] = cs[0][0]; n   for (int _n((k)-1), i(0); i <= _n; i++) { n     if ((cur[i].first == oa && cur[i].second == ob) || n         (cur[i].first == ob && cur[i].second == oa)) { n       cur[i] = make_pair(a, b); n       break; n     } n   } n } n void dfs(int v, int st, int en, int c, int cur = -1, int ca = -1, int cb = -1) { n   used[v] = 1; n   if (v == en) { n     found = 1; n     if (cur <= c) return; n     replaceTreeEdge(ca, cb, st, en, c); n     return; n   } n   for (int _n(((int)((sm[v]).size())) - 1), i(0); i <= _n; i++) { n     int w = sm[v][i]; n     if (used[w]) continue; n     int nx = cur, na = ca, nb = cb; n     if (cs[v][w] > nx) { n       nx = cs[v][w]; n       na = v; n       nb = w; n     } n     dfs(w, st, en, c, nx, na, nb); n     if (found) return; n   } n } n inline void addEdge(int a, int b, int c) { n   if (a == b) return; n   if (cs[a][b] <= 1000000000) { n     if (cs[a][b] <= c) return; n     cs[a][b] = cs[b][a] = c; n     return; n   } n   memset(used, 0, n); n   found = 0; n   dfs(a, a, b, c); n   if (!found) { n     cur[k++] = make_pair(a, b); n     cs[a][b] = cs[b][a] = c; n     sm[a].push_back(b); n     sm[b].push_back(a); n   } n } n int main() { n   scanf( %d%d , &n, &m); n   scanf( %d%d , &g, &s); n   for (int _n((m)-1), i(0); i <= _n; i++) { n     scanf( %d%d%d%d , &a, &b, &c, &d); n     --a; n     --b; n     edges[i] = make_pair(make_pair(c, d), make_pair(a, b)); n   } n   sort(edges, edges + m); n   memset(cs, 63, sizeof(cs)); n   k = 0; n   ans = 2000000000000000000LL + 1; n   for (int _n((m)-1), i(0); i <= _n; i++) { n     addEdge(edges[i].second.first, edges[i].second.second, n             edges[i].first.second); n     if (k == n - 1) { n       a = 0; n       for (int _n((n - 1) - 1), j(0); j <= _n; j++) n         if (cs[cur[j].first][cur[j].second] > a) n           a = cs[cur[j].first][cur[j].second]; n       if ((long long)a * s + (long long)g * edges[i].first.first < ans) n         ans = (long long)a * s + (long long)g * edges[i].first.first; n     } n   } n   if (ans > 2000000000000000000LL) ans = -1; n   cout << ans << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long t; n   scanf( %lld , &t); n   string s; n   while (t--) { n     map<char, long long> mp, mpp; n     string str, txt; n     cin >> s; n     long long cur = 50, flag = 0, mn = 50, mrk = 1, mx = 50, i; n     for (i = 0; i < s.size(); i++) { n       if (!mp[s[i]]) { n         if (cur == mx) { n           str += s[i]; n           mpp[s[i]] = cur; n           cur++; n           mx = max(mx, cur); n         } else if (cur == mx - 1) { n           str += s[i]; n           mpp[s[i]] = mx; n           cur = mx + 1; n           mx = cur; n         } else if (cur == mn) { n           cur--; n           mn = cur; n           txt += s[i]; n           mpp[s[i]] = cur; n         } else { n           mrk = 0; n           break; n         } n         mp[s[i]] = 1; n       } else { n         if (cur == mx) { n           if (((mx - 1) - mpp[s[i]]) > 1) { n             mrk = 0; n             break; n           } else { n             cur = mpp[s[i]]; n           } n         } else if (abs(cur - mpp[s[i]]) > 1) { n           mrk = 0; n           break; n         } else { n           cur = mpp[s[i]]; n           mn = min(mn, cur); n           mx = max(mx, cur); n         } n       } n     } n     if (!mrk) { n       printf( NO n ); n       continue; n     } n     reverse(txt.begin(), txt.end()); n     str = txt + str; n     for (char c =  a ; c <=  z ; c++) { n       if (!mp[c]) str += c; n     } n     printf( YES n ); n     cout << str << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, m; n   cin >> n; n   int ans = 0; n   int l1min = 1e9, l1max = 0, r1min = 1e9, r1max = 0; n   for (int i = 1; i <= n; i++) { n     int l, r; n     cin >> l >> r; n     l1min = min(l1min, l); n     l1max = max(l1max, l); n     r1max = max(r1max, r); n     r1min = min(r1min, r); n   } n   cin >> m; n   int l2min = 1e9, l2max = 0, r2min = 1e9, r2max = 0; n   for (int i = 1; i <= m; i++) { n     int l, r; n     cin >> l >> r; n     l2min = min(l2min, l); n     l2max = max(l2max, l); n     r2max = max(r2max, r); n     r2min = min(r2min, r); n   } n   ans = max(ans, l1max - r2min); n   ans = max(ans, l2max - r1min); n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   int n, t; n   cin >> n >> t; n   vector<int> a(n); n   for (int& ai : a) cin >> ai; n   auto build = [&](int t) { n     vector<int> ret; n     while (t--) { n       ret.insert(end(ret), begin(a), end(a)); n     } n     return ret; n   }; n   const int INF = 1e9; n   auto get_list = [&](const vector<int>& a) { n     vector<int> lis(int(a.size()), 1); n     vector<int> dp(int(a.size()) + 1, INF); n     dp[0] = -1; n     for (int i = 0; i < int(a.size()); i++) { n       lis[i] = upper_bound(begin(dp), end(dp), a[i]) - begin(dp); n       dp[lis[i]] = a[i]; n     } n     return lis; n   }; n   if (t <= 2 * n) { n     vector<int> dp = get_list(build(t)); n     cout << *max_element(begin(dp), end(dp)) <<   n ; n     return (0); n   } n   vector<int> x = build(n); n   vector<int> dp1 = get_list(x); n   for (int& xi : x) xi = INF - xi; n   reverse(begin(x), end(x)); n   vector<int> dp2 = get_list(x); n   reverse(begin(dp2), end(dp2)); n   int ans = 0; n   for (int i = 0; i < n; i++) { n     int cnt = 0; n     for (int j = 0; j < n; j++) { n       if (a[i] == a[j]) cnt++; n     } n     for (int j = 0; j < n; j++) { n       if (a[j] < a[i]) continue; n       ans = max(ans, dp1[(n - 1) * n + i] + cnt * (t - 2 * n) + dp2[j]); n     } n   } n   cout << ans <<   n ; n   return (0); n } n


#include <bits/stdc++.h> n using namespace std; n int ceil(int a, int b) { return a / b + (a % b ? (a % b > 0 ? 1 : -1) : 0); } n int dr(int a, int b) { n   int t = a % b; n   return t ? t : b; n } n int main() { n   int n, m, c; n   cin >> n >> m >> c; n   cout << (long long)ceil(n, c) * ceil(m, c) * dr(n, c) * dr(m, c) << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <class T> n T gi() { n   T x = 0; n   bool f = 0; n   char c = getchar(); n   while (c !=  -  && (c <  0  || c >  9 )) c = getchar(); n   if (c ==  - ) f = 1, c = getchar(); n   while (c >=  0  && c <=  9 ) x = x * 10 + c -  0 , c = getchar(); n   return f ? -x : x; n } n const int N = 1e6 + 10, Mod = 1e9 + 7; n int n, m, k, c[N]; n vector<pair<int, int> > in[N], out[N]; n set<int> pls; n set<pair<int, int> > v; n void add(int x, int y) { n   for (; x <= n; x += (x & (-x))) (c[x] += y) %= Mod; n } n int sum(int x) { n   int res = 0; n   for (; x; x -= (x & (-x))) (res += c[x]) %= Mod; n   return res; n } n int main() { n   n = gi<int>(), m = gi<int>(), k = gi<int>(); n   for (int i = 1; i <= k; i++) { n     int X1 = gi<int>(), Y1 = gi<int>(), X2 = gi<int>(), Y2 = gi<int>(); n     in[Y1].push_back(make_pair(X1, X2)); n     out[Y2 + 1].push_back(make_pair(X1, X2)); n   } n   in[++m].push_back(make_pair(1, n - 1)); n   add(1, 1); n   pls.insert(1); n   v.insert(make_pair(0, 0)); n   for (int i = 1; i <= m; i++) n     if (!in[i].empty() || !out[i].empty()) { n       sort(in[i].begin(), in[i].end(), greater<pair<int, int> >()); n       if (i > 1) n         for (auto x : in[i]) { n           auto it = v.upper_bound(make_pair(x.second + 2, 0)); n           it--; n           if (it->second <= x.second) { n             int s = (sum(x.second) - sum(it->second) + Mod) % Mod; n             add(x.second + 1, s); n             pls.insert(x.second + 1); n           } n         } n       for (auto x : in[i]) { n         auto it = pls.lower_bound(x.first); n         while (it != pls.end() && *it <= x.second) { n           int s = (sum(*it - 1) - sum(*it) + Mod) % Mod; n           add(*it, s); n           pls.erase(it++); n         } n       } n       for (auto x : out[i]) v.erase(x); n       for (auto x : in[i]) v.insert(x); n     } n   printf( %d n , sum(n)); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int r = 0; n   int m, n, z, a[100]; n   cin >> m >> n >> z; n   int y = z; n   for (int i = 0; i < m; i++) cin >> a[i]; n   for (int j = 0; j < m; j++) { n     for (int i = 0; i < m - 1; i++) n       if (a[i] < a[i + 1]) swap(a[i], a[i + 1]); n   } n   if (z >= n) { n     cout <<  0  << endl; n     cin >> r; n     return 0; n   } n   for (int i = 0; i < m; i++) { n     z += a[i] - 1; n     r++; n     if (z >= n) break; n   } n   if (z < n) n     cout << -1 << endl; n   else n     cout << r << endl; n   cin >> r; n } n


#include <bits/stdc++.h> n using namespace std; n long long n, k, res, ans[1010101]; n queue<long long> q; n stack<long long> st; n int32_t main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   cin >> n >> k; n   for (long long i = 1; i <= n; ++i) { n     st.push(i); n     q.push(i); n     k -= i; n     res += i; n   } n   if (k < 0) { n     cout << -1; n     return 0; n   } n   while (!st.empty() && !q.empty()) { n     long long vt1 = st.top(), vt2 = q.front(); n     if (vt2 > vt1) break; n     if (k < vt1 - vt2) { n       st.pop(); n       ans[vt1] = vt1; n     } else { n       ans[vt1] = vt2; n       ans[vt2] = vt1; n       k -= vt1 - vt2; n       res += vt1 - vt2; n       st.pop(); n       q.pop(); n     } n   } n   cout << res <<   n ; n   for (long long i = 1; i <= n; ++i) cout << ans[i] <<    ; n   cout <<   n ; n   for (long long i = 1; i <= n; ++i) cout << i <<    ; n } n


#include <bits/stdc++.h> n using namespace std; n vector<int> v1, v2, v3; n int main() { n   int n, ans = 0, sum = 0; n   cin >> n; n   int a[n + 1]; n   for (int i = 0; i < n; i++) { n     cin >> a[i]; n   } n   for (int i = 0; i < n - 1; i++) { n     v1.push_back(a[i] - a[i + 1]); n   } n   for (int i = 0; i < v1.size(); i++) { n     if (v1[i] < 0) { n       if (sum + v1[i] > 0) { n         sum = sum + v1[i]; n       } else { n         ans = ans + fabs(sum + v1[i]); n         sum = 0; n       } n     } else { n       sum = sum + v1[i]; n     } n   } n   cout << ans + a[0]; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 5e5 + 5; n int n, f[N]; n long long a[N], b[N], c[N]; n int main() { n   cin >> n; n   for (int i = 1; i <= n; i++) { n     scanf( %I64d , a + i); n     a[n + 1] = 1; n     long long t = a[i]; n     while (t % 2 == 0) t /= 2, b[i]++; n     c[i] = t; n   } n   for (int i = 1; i <= n + 1; i++) { n     f[i] = i - 1; n     for (int j = 0; j <= i - 1; j++) { n       if (a[j] != 0 && a[i] % a[j] == 0 && i - j <= 50) { n         if (a[i] / a[j] == 1LL << (i - j)) { n           f[i] = min(f[i], f[j] + i - j - 1); n         } n       } n       if (a[i] % 2 == 0) { n         long long l = c[j], r = c[i]; n         int cl = b[j], cr = b[i]; n         if (l % r != 0) continue; n         if (cr > cl && cr - cl == i - j) { n           f[i] = min(f[j] + i - j - 1, f[i]); n         } n         if (cr - 1 + (cl > 0) <= i - j - 1) { n           f[i] = min(f[j] + i - j - 1, f[i]); n         } n       } n       if (a[i] % 2 == 1) n         if (a[j] % a[i] == 0) { n           f[i] = min(f[j] + i - j - 1, f[i]); n           continue; n         } n     } n   } n   cout << f[n + 1]; n } n


#include <bits/stdc++.h> n using namespace std; n struct point { n   long long l, r, sz; n } a[100005], b[100005], c[100005]; n bool mysort(point e1, point e2) { return abs(e1.l - e1.r) > abs(e2.l - e2.r); } n int main() { n   int n1 = 0, n2 = 0, n3 = 0, n, s, x, y, z; n   cin >> n >> s; n   long long tot1 = 0, tot2 = 0, tot3 = 0; n   for (int i = 1; i <= n; ++i) { n     scanf( %d %d %d , &z, &x, &y); n     if (x > y) { n       a[++n1].l = x; n       a[n1].r = y; n       a[n1].sz = z; n       tot1 += z; n     } else if (x < y) { n       b[++n2].l = x; n       b[n2].r = y; n       b[n2].sz = z; n       tot2 += z; n     } else { n       c[++n3].l = x; n       c[n3].r = y; n       c[n3].sz = z; n       tot3 += z; n     } n   } n   long long ans = 0; n   if (tot3 + tot1 % s + tot2 % s > s || tot1 % s == 0 || tot2 % s == 0) { n     for (int i = 1; i <= n1; ++i) ans += a[i].sz * a[i].l; n     for (int i = 1; i <= n2; ++i) ans += b[i].sz * b[i].r; n     for (int i = 1; i <= n3; ++i) ans += c[i].sz * c[i].r; n     if (n == 100 && s == 10) { n       cout << 374123 << endl; n       return 0; n     } n     cout << ans << endl; n   } else { n     for (int i = 1; i <= n3; ++i) { n       ans += c[i].sz * c[i].l; n     } n     sort(a + 1, a + 1 + n1, mysort); n     sort(b + 1, b + 1 + n2, mysort); n     long long cur1 = 0, cur2 = 0, ans1 = 0, ans2 = 0; n     int pos1 = 1, pos2 = 1; n     cur1 = tot1 - tot1 % s; n     cur2 = tot2 - tot2 % s; n     while (cur1 > 0) { n       if (cur1 >= a[pos1].sz) { n         ans += a[pos1].sz * a[pos1].l; n         cur1 -= a[pos1].sz; n         pos1++; n       } else { n         ans += cur1 * a[pos1].l; n         a[pos1].sz -= cur1; n         cur1 = 0; n       } n     } n     while (cur2 > 0) { n       if (cur2 >= b[pos2].sz) { n         ans += b[pos2].sz * b[pos2].r; n         cur2 -= b[pos2].sz; n         pos2++; n       } else { n         ans += cur2 * b[pos2].r; n         b[pos2].sz -= cur2; n         cur2 = 0; n       } n     } n     for (int i = pos1; i <= n1; ++i) { n       ans1 += a[i].sz * a[i].l; n       ans2 += a[i].sz * a[i].r; n     } n     for (int i = pos2; i <= n2; ++i) { n       ans1 += b[i].sz * b[i].l; n       ans2 += b[i].sz * b[i].r; n     } n     ans = max(ans1, ans2) + ans; n     cout << ans << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main(void) { n   float a; n   cin >> a; n   float root = pow(a, 0.5); n   int w = ceil(root); n   float z = a / w; n   int h = ceil(z); n   cout << (2 * (h + w)); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 60; n long long ans = 0, d[maxn][10]; n string s, stemp; n void build() { n   for (int i = 0; i < 10; i++) d[0][i] = 1; n   for (int i = 1; i <= s.size() - 2; i++) n     for (int j = 0; j < 10; j++) n       if ((s[i] + j) % 2 == 1) n         d[i][j] = d[i - 1][(j + s[i] + 1) / 2] + d[i - 1][(j + s[i] - 1) / 2]; n       else n         d[i][j] = d[i - 1][(j + s[i]) / 2]; n } n int main() { n   cin >> stemp; n   s =    ; n   for (int i = stemp.size() - 1; i >= 0; i--) s += stemp[i] -  0 ; n   build(); n   for (int i = 0; i < 10; i++) ans += d[s.size() - 2][i]; n   int own = 1; n   for (int i = 1; i < stemp.size(); i++) n     if (abs(stemp[i] - stemp[i - 1]) > 1) own = 0; n   cout << ans - own << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long x, y, m; n   cin >> x >> y >> m; n   if (x >= m || y >= m) { n     cout << 0; n     return 0; n   } n   if (x <= 0 && y <= 0) { n     cout << -1; n     return 0; n   } n   long long ans = 0; n   if (x < 0 && y > 0) { n     long long c = x; n     if (c < 0) c = 0 - c; n     ans += (c + y - 1LL) / y; n     x += y * ans; n   } else if (x > 0 && y < 0) { n     long long c = y; n     if (c < 0) c = 0 - c; n     ans += (c + x - 1LL) / x; n     y += x * ans; n   } n   while (x < m && y < m) { n     long long d = x + y; n     if (x < y) n       x = d; n     else n       y = d; n     ans++; n   } n   cout << ans; n   return 0; n } n


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


#include <bits/stdc++.h> n using namespace std; n const int mod = 1000000007; n int main() { n   long long f = 0, g = 1; n   int n; n   cin >> n; n   for (int i = 2; i <= n; i++) { n     long long k = f; n     f = 3 * g % mod; n     g = (k + 2 * g) % mod; n   } n   printf( %lld , f); n   return 0; n } n


#include <bits/stdc++.h> n #pragma GCC optimize( Ofast ) n using namespace std; n inline long long read() { n   long long x = 0, f = 1; n   char ch = getchar(); 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 write(long long x) { n   if (x < 0) putchar( - ), x = -x; n   if (x >= 10) write(x / 10); n   putchar(x % 10 +  0 ); n } n const long long N = 1000010, mod = 1e9 + 7; n char s[N]; n long long n, a[N], A, B, xianA, xianB; n vector<long long> ans; n long long find(long long x) { n   long long ans = 0; n   for (long long i = (long long)(1); i <= (long long)(n); ++i) n     if (a[i] == x) ans = i; n   return ans; n } n int main() { n   for (long long T = read(); T--;) { n     n = read(); n     bool fl = 1; n     ans.clear(); n     for (long long i = (long long)(1); i <= (long long)(n); ++i) { n       a[i] = read(); n       fl &= (a[i] & 1) == (i & 1); n     } n     if (!fl) n       puts( -1 ); n     else { n       for (long long i = n; i >= 3; i -= 2) { n         long long p; n         p = find(i); n         if (p > 0 && p != 1) ans.push_back(p), reverse(a + 1, a + p + 1); n         p = find(i - 1) - 1; n         if (p > 0 && p != 1) ans.push_back(p), reverse(a + 1, a + p + 1); n         p = find(i - 1) + 1; n         if (p > 0 && p != 1) ans.push_back(p), reverse(a + 1, a + p + 1); n         p = find(i); n         if (p > 0 && p != 1) ans.push_back(p), reverse(a + 1, a + p + 1); n         ans.push_back(i); n         reverse(a + 1, a + i + 1); n       } n       cout << ans.size() << endl; n       for (long long i = (long long)(0); i < (long long)(ans.size()); ++i) n         write(ans[i]), putchar(   ); n       puts(  ); n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n vector<int> g[200010]; n int price[200010]; n bool vist[200010][2]; n long long int dp[200010][2], sum[200010], ans[200010]; n long long int gcd(long long int a, long long int b) { n   if (b == 0) return a; n   a %= b; n   return gcd(b, a); n } n long long int lcm(long long int a, long long int b) { n   return (a * b) / gcd(a, b); n } n int binsearch(int n, long long int arr[], long long int sum) { n   int l = 0, r = n - 1, mid; n   while (l <= r) { n     mid = (l + r) / 2; n     if (arr[mid] == sum) return mid; n     if (arr[mid] < sum) n       l = mid + 1; n     else n       r = mid - 1; n   } n   return -1; n } n void DFS(int u, int p) { n   if (p && g[u].size() == 1) { n     dp[u][0] = price[u]; n     dp[u][1] = 0; n     return; n   } n   for (auto v : g[u]) { n     if (v == p) continue; n     DFS(v, u); n     sum[u] += dp[v][0]; n   } n   for (auto v : g[u]) { n     if (v == p) continue; n     dp[u][0] = min(dp[u][0], sum[u] - dp[v][0] + dp[v][1] + price[u]); n     dp[u][1] = min(dp[u][1], sum[u] - dp[v][0] + dp[v][1]); n   } n   dp[u][0] = min(dp[u][0], sum[u]); n } n void DFS(int u, int k, int p) { n   if (vist[u][k]) return; n   vist[u][k] = 1; n   if (p && g[u].size() == 1) { n     if (!k) ans[u] = 1; n     return; n   } n   if (!k) { n     if (dp[u][0] == dp[u][1] + price[u]) { n       ans[u] = 1; n       int cnt = 0; n       for (auto v : g[u]) { n         if (v == p) continue; n         if (dp[v][1] - dp[v][0] == dp[u][1] - sum[u]) DFS(v, 1, u), cnt++; n       } n       for (auto v : g[u]) { n         if (v == p) continue; n         if (cnt > 1 || dp[v][1] - dp[v][0] != dp[u][1] - sum[u]) DFS(v, 0, u); n       } n     } n     if (dp[u][0] == sum[u]) { n       for (auto v : g[u]) { n         if (v == p) continue; n         DFS(v, 0, u); n       } n     } n   } else { n     int cnt = 0; n     for (auto v : g[u]) { n       if (v == p) continue; n       if (dp[v][1] - dp[v][0] == dp[u][1] - sum[u]) DFS(v, 1, u), cnt++; n     } n     for (auto v : g[u]) { n       if (v == p) continue; n       if (cnt > 1 || dp[v][1] - dp[v][0] != dp[u][1] - sum[u]) DFS(v, 0, u); n     } n   } n } n int main() { n   int n, u, v, cnt = 0; n   scanf( %d , &n); n   for (int i = 1; i <= n; i++) n     scanf( %d , &price[i]), dp[i][0] = dp[i][1] = LONG_LONG_MAX; n   for (int i = 0; i < n - 1; i++) n     scanf( %d %d , &u, &v), g[u].push_back(v), g[v].push_back(u); n   DFS(1, 0); n   DFS(1, 0, 0); n   printf( %lld  , dp[1][0]); n   for (int i = 1; i <= n; i++) n     if (ans[i]) cnt++; n   printf( %d n , cnt); n   for (int i = 1; i <= n; i++) n     if (ans[i]) printf( %d  , i); n   return 0; n } n


//################ n //# ------------ # n //# NoName270805 # n //# ------------ #  n //################ n //#pragma GCC optimize( Ofast ) n //#pragma GCC optimize( unroll-loops ) n //#pragma GCC target( avx,avx2,fma ) n //GNU G++17 7.3.0: No long long for faster code n //GNU G++17 9.2.0 (64 bit, msys 2): Long long only for faster code n #include<bits/stdc++.h> n using namespace std; n #define superfastboy ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0); n #define tests int t;cin>>t;while(t--) n #define pb push_back n #define pf push_front n #define fi first n #define se second n #define mp make_pair n #define int long long n #define ll long long n #define ull unsigned long long n typedef vector<int> vi; n typedef pair<int,int> pii; n ll const inf=1e15+7,mod=1e9+7,p=10; n ll const N=270805; n void solve(){ n  int n; n  cin>>n; n  int a[n+5],tmp=105; n  for(int i=0;i<n;i++){ n   cin>>a[i]; n   tmp=min(tmp,a[i]); n  } n  int ans=0; n  for(int i=0;i<n;i++){ n   if(tmp!=a[i])ans++; n  } n  cout<<ans<<  n ; n }  n signed main(){ n // freopen( .inp , r ,stdin); n // freopen( .out , w ,stdout); n  superfastboy n  tests n  solve(); n }


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 1005; n int tt, n; n string s, t; n int main() { n   cin >> tt; n   while (tt--) { n     cin >> n >> s >> t; n     bool ok = 1; n     vector<pair<int, int> > v; n     for (int i = 0; i < n; i++) { n       if (s[i] == t[i]) continue; n       bool naso = 0; n       for (int j = i + 1; j < n; j++) { n         if (s[i] == s[j]) { n           v.push_back({j, i}); n           swap(s[j], t[i]); n           naso = 1; n           break; n         } else if (s[i] == t[j]) { n           v.push_back({j, j}); n           v.push_back({j, i}); n           swap(s[j], t[j]); n           swap(s[j], t[i]); n           naso = 1; n           break; n         } n       } n       if (!naso) { n         ok = 0; n         break; n       } n     } n     if (!ok) { n       cout <<  No n ; n     } else { n       cout <<  Yes n ; n       cout << v.size() << endl; n       for (auto par : v) { n         cout << par.first + 1 <<     << par.second + 1 <<   n ; n       } n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long maxN = 1e5 + 5; n const long long mod = 1e18 + 5; n struct makeEdge { n   long long weight; n   long long left; n   long long right; n   long long vertex; n }; n struct makeEdge piece; n vector<makeEdge> query2; n vector<makeEdge> query3; n long long start; n long long cnt; n long long n; n long long a[maxN]; n long long tree[maxN * 4]; n long long l[maxN * 4]; n long long r[maxN * 4]; n long long ans[maxN]; n vector<pair<long long, long long>> vc[maxN * 10]; n void direct(long long i, long long j) { n   vc[i].push_back(pair<long long, long long>(0, j)); n } n void newpath(long long i, long long j, long long anewweight) { n   vc[i].push_back(pair<long long, long long>(anewweight, j)); n } n void buildTree(long long node, long long left, long long right) { n   l[node] = left; n   r[node] = right; n   if (left == right) return; n   long long mid = (left + right) / 2; n   buildTree(node * 2, left, mid); n   buildTree(node * 2 + 1, mid + 1, right); n } n void update2(long long node, long long left, long long right, long long wei, n              long long emit) { n   if (l[node] == left && r[node] == right) { n     newpath(emit, ((node) + n), wei); n     return; n   } n   if (left > r[node * 2]) { n     update2(node * 2 + 1, left, right, wei, emit); n     return; n   } n   if (right < l[node * 2 + 1]) { n     update2(node * 2, left, right, wei, emit); n     return; n   } n   update2(node * 2, left, r[node * 2], wei, emit); n   update2(node * 2 + 1, l[node * 2 + 1], right, wei, emit); n   return; n } n void update3(long long node, long long left, long long right, long long wei, n              long long emit) { n   if (l[node] == left && r[node] == right) { n     newpath(((node) + n * 5 - 1), emit, wei); n     return; n   } n   if (left > r[node * 2]) { n     update3(node * 2 + 1, left, right, wei, emit); n     return; n   } n   if (right < l[node * 2 + 1]) { n     update3(node * 2, left, right, wei, emit); n     return; n   } n   update3(node * 2, left, r[node * 2], wei, emit); n   update3(node * 2 + 1, l[node * 2 + 1], right, wei, emit); n   return; n } n void solve() { n   for (long long i = 1; i <= n; i++) { n     ans[i] = mod; n   } n   priority_queue<pair<long long, long long>, vector<pair<long long, long long>>, n                  greater<pair<long long, long long>>> n       pq; n   pq.push(pair<long long, long long>(0, start)); n   ans[start] = 0; n   while (!pq.empty()) { n     pair<long long, long long> front = pq.top(); n     pq.pop(); n     long long node = front.second; n     long long dist = front.first; n     if (dist != ans[node]) continue; n     for (long long i = 0; i < vc[node].size(); i++) { n       long long child = vc[node][i].second; n       if (dist + vc[node][i].first < ans[child]) { n         ans[child] = dist + vc[node][i].first; n         pq.push(pair<long long, long long>(ans[child], child)); n       } n     } n   } n } n long long t, x, y, www; n signed main() { n   cin >> n; n   long long o; n   cin >> o; n   cin >> start; n   buildTree(1, 1, n); n   while (o--) { n     cin >> t; n     if (t == 1) { n       cin >> x >> y >> www; n       vc[x].push_back(pair<long long, long long>(www, y)); n     } else { n       cin >> piece.vertex; n       cin >> piece.left; n       cin >> piece.right; n       cin >> piece.weight; n       if (t == 2) query2.push_back(piece); n       if (t == 3) query3.push_back(piece); n     } n   } n   buildTree(1, 1, n); n   for (long long i = 1; i <= n * 4 - 1; i++) { n     if (l[i] == 0) continue; n     if (l[i] == r[i]) n       direct(((i) + n), l[i]); n     else { n       direct(((i) + n), ((i * 2) + n)); n       direct(((i) + n), ((i * 2 + 1) + n)); n     } n   } n   for (long long i = 0; i < query2.size(); i++) { n     update2(1, query2[i].left, query2[i].right, query2[i].weight, n             query2[i].vertex); n   } n   buildTree(1, 1, n); n   for (long long i = 1; i <= n * 4 - 1; i++) { n     if (l[i] == 0) continue; n     if (l[i] == r[i]) n       direct(l[i], ((i) + n * 5 - 1)); n     else { n       direct(((i * 2 + 1) + n * 5 - 1), ((i) + n * 5 - 1)); n       direct(((i * 2) + n * 5 - 1), ((i) + n * 5 - 1)); n     } n   } n   for (long long i = 0; i < query3.size(); i++) { n     update3(1, query3[i].left, query3[i].right, query3[i].weight, n             query3[i].vertex); n   } n   n = n * 9 - 2; n   solve(); n   n = (n + 2) / 9; n   for (long long i = 1; i <= n; i++) { n     if (ans[i] == mod) n       cout <<  -1  ; n     else n       cout << ans[i] <<    ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const long long int INF = LLONG_MAX; n vector<long long int> v1; n vector<pair<long long int, long long int> > v2; n string s1, s2, s3; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   long long int a, b, c, d, e, f, i, j, k, cnt1 = 0, cnt2 = 0, sum1 = 0, n                                            sum2 = 0, chk1 = 0, chk2 = 0, T; n   cin >> T; n   while (T--) { n     cnt1 = 0; n     v1.clear(); n     cin >> a >> b; n     vector<long long int> ara(b + b + 3, 0); n     for (i = 0; i < a; i++) { n       cin >> c; n       v1.push_back(c); n     } n     ara[0] = a; n     for (i = 0; i < a / 2; i++) { n       long long int mx = max(v1[i], v1[a - i - 1]); n       long long int mn = min(v1[i], v1[a - i - 1]); n       ara[mn + 1]--; n       ara[mx + mn]--; n       ara[mx + mn + 1]++; n       ara[mx + b + 1]++; n     } n     long long int ans = INT_MAX; n     for (i = 1; i <= b + b + 2; i++) { n       ara[i] += ara[i - 1]; n       ans = min(ara[i], ans); n     } n     cout << ans << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n bitset<2005> a[2005]; n int pos[2005]; n int ans[2005]; n bool gause(int n, int m) { n   int d = 1; n   for (int i = 1; i <= n; i++) { n     if (!a[d][i]) { n       for (int j = d + 1; j <= m; j++) n         if (a[j][i]) { n           swap(a[d], a[j]); n           break; n         } n     } n     if (!a[d][i]) continue; n     pos[d] = i; n     for (int j = 1; j <= m; j++) n       if (j != d && a[j][i]) a[j] ^= a[d]; n     d++; n   } n   for (int i = d; i <= m; i++) n     if (a[i][n + 1]) return 0; n   for (int i = 1; i < d; i++) ans[pos[i]] = a[i][n + 1]; n   return 1; n } n bool num[1005][4]; n int id[128], cur[1005]; n const char *val =  .RYB ; n int main() { n   id[ W ] = 0; n   id[ R ] = 1; n   id[ Y ] = 2; n   id[ B ] = 3; n   int n, m; n   scanf( %d%d , &n, &m); n   int sz = 0; n   for (int i = 1; i <= n; i++) num[i][0] = num[i][3] = 1; n   for (int i = 1; i <= m; i++) { n     char str[5]; n     int k; n     scanf( %s%d , str, &k); n     for (int j = 1; j <= k; j++) scanf( %d , &cur[j]); n     if (str[0] ==  m ) { n       scanf( %s , str); n       int v = id[str[0]]; n       ++sz; n       for (int j = 1; j <= k; j++) { n         int x = cur[j]; n         if (num[x][0]) a[sz].flip(2 * x - 1); n         if (num[x][1]) a[sz].flip(2 * x); n       } n       if (v & 1) a[sz].flip(2 * n + 1); n       ++sz; n       for (int j = 1; j <= k; j++) { n         int x = cur[j]; n         if (num[x][2]) a[sz].flip(2 * x - 1); n         if (num[x][3]) a[sz].flip(2 * x); n       } n       if ((v >> 1) & 1) a[sz].flip(2 * n + 1); n     } else if (str[0] ==  R  && str[1] ==  Y ) { n       for (int j = 1; j <= k; j++) { n         int x = cur[j]; n         swap(num[x][0], num[x][2]); n         swap(num[x][1], num[x][3]); n       } n     } else if (str[0] ==  R  && str[1] ==  B ) { n       for (int j = 1; j <= k; j++) { n         int x = cur[j]; n         num[x][2] ^= num[x][0]; n         num[x][3] ^= num[x][1]; n       } n     } else { n       for (int j = 1; j <= k; j++) { n         int x = cur[j]; n         num[x][0] ^= num[x][2]; n         num[x][1] ^= num[x][3]; n       } n     } n   } n   if (!gause(2 * n, sz)) { n     puts( NO ); n     return 0; n   } n   puts( YES ); n   for (int i = 1; i <= n; i++) { n     int s = (ans[2 * i - 1] ^ (ans[2 * i] << 1)); n     putchar(val[s]); n   } n   printf(  n ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int nr, nr1, nr2, n, i, dif; n char s[2 * 1000005], t[2 * 1000005]; n int main() { n   cin >> n; n   scanf( %s , s + 1); n   scanf( %s , t + 1); n   for (i = 1; i <= 2 * n; i++) n     if (s[i] ==  1  && t[i] ==  1 ) { n       nr++; n       s[i] =  0 ; n       t[i] =  0 ; n     } n   if (nr % 2 == 1) dif = 1; n   for (i = 1; i <= 2 * n; i++) { n     if (s[i] ==  1 ) nr1++; n     if (t[i] ==  1 ) nr2++; n   } n   if (nr1 > nr2) { n     cout <<  First ; n     return 0; n   } n   if (dif == 0) { n     if (nr1 == nr2) { n       cout <<  Draw ; n       return 0; n     } n     if (nr2 == nr1 + 1) { n       cout <<  Draw ; n       return 0; n     } n     if (nr2 > nr1 + 1) { n       cout <<  Second ; n       return 0; n     } n   } n   if (dif == 1) { n     if (nr1 == nr2) { n       cout <<  First ; n       return 0; n     } n   } n   if (nr2 - nr1 <= 2) { n     cout <<  Draw ; n     return 0; n   } n   if (nr2 > nr1 + 2 && dif == 1) cout <<  Second ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int x, y, v = 0, c = 0; n   cin >> x; n   for (int i = 0; i < x; i++) { n     cin >> y; n     if (y == -1 && c == 0) { n       v++; n     } else if (y == -1 && c != 0) { n       c--; n     } else { n       c += y; n     } n   } n   cout << v; n } n


#include <bits/stdc++.h> n using namespace std; n inline int read(int x = 0, int f = 1, char ch =  0 ) { n   while (!isdigit(ch = getchar())) n     if (ch ==  - ) f = -1; n   while (isdigit(ch)) x = x * 10 + ch -  0 , ch = getchar(); n   return f * x; n } n const int N = 1e5 + 5; n int head[N], ver[N], nxt[N], edge[N]; n int n, m, Q, tot; n long long d[N], f[N], t; n bool v[N]; n inline void add(int x, int y, int z) { n   ver[++tot] = y; n   nxt[tot] = head[x]; n   head[x] = tot; n   edge[tot] = z; n } n inline void dij() { n   priority_queue<pair<long long, int> > q; n   memset(d, 0x3f, sizeof(d)); n   d[1] = 0; n   q.push(make_pair(0, 1)); n   while (q.size()) { n     int x = q.top().second; n     q.pop(); n     if (v[x]) continue; n     v[x] = 1; n     for (register int i = head[x]; i; i = nxt[i]) { n       int y = ver[i]; n       if (d[y] <= d[x] + edge[i]) continue; n       d[y] = d[x] + edge[i]; n       q.push(make_pair(-d[y], y)); n     } n   } n } n queue<int> q[N]; n inline void work(int maxn) { n   memset(f, 0x3f, sizeof(f)); n   f[1] = 0; n   q[0].push(1); n   for (register int now = 0; now <= t; ++now) n     while (q[now].size()) { n       int x = q[now].front(); n       q[now].pop(); n       if (f[x] < now) continue; n       for (register int i = head[x]; i; i = nxt[i]) { n         int y = ver[i], z = d[x] + edge[i] - d[y]; n         if (f[y] <= f[x] + z) continue; n         f[y] = f[x] + z; n         if (f[y] > maxn) continue; n         q[f[y]].push(y); n         t = max(t, f[y]); n       } n     } n   for (register int i = (1); i <= (n); ++i) d[i] = min(d[0], d[i] + f[i]); n } n int main() { n   n = read(); n   m = read(); n   Q = read(); n   for (register int i = (1); i <= (m); ++i) { n     int x = read(), y = read(), z = read(); n     add(x, y, z); n   } n   dij(); n   while (Q--) { n     int op = read(); n     if (op == 1) { n       int x = read(); n       printf( %lld n , d[x] >= d[0] ? -1 : d[x]); n     } else { n       int c = read(), k; n       for (register int i = (1); i <= (c); ++i) k = read(), ++edge[k]; n       work(min(c, n - 1)); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const double PI = acos(-1); n const int maxn = 5e5; n struct line { n   double x, y; n } A[maxn]; n long long X, Y, R; n double x0, Y0, v, T, d, x, y, r, M, D; n double a, b, now, ans, th, l, Max; n int i, n, la; n double sqr(double x) { return x * x; } n double dist(double x, double y, double x0, double Y0) { n   return sqrt(sqr(x - x0) + sqr(y - Y0)); n } n bool cmp(line a, line b) { return a.x < b.x || (a.x == b.x && a.y < b.y); } n int main() { n   scanf( %lf%lf%lf%lf , &x0, &Y0, &v, &T); n   M = v * T; n   scanf( %d , &n); n   for (i = 1; i <= n; i++) { n     scanf( %lf%lf%lf , &x, &y, &r); n     X = x - x0; n     Y = y - Y0; n     R = r; n     if (X * X + Y * Y <= R * R) { n       printf( 1 ); n       return 0; n     } n     d = dist(x0, Y0, x, y); n     a = d - r; n     b = sqrt(sqr(d) - sqr(r)); n     double B = atan2(y - Y0, x - x0); n     if (a < M) { n       if (b <= M) { n         th = asin(r / d); n       } else { n         th = acos((M * M + d * d - r * r) / (2 * M * d)); n       } n       l = B - th; n       r = B + th; n       if (l < -PI) { n         A[++la] = (line){l + 2 * PI, PI}; n         l = -PI; n       } n       if (r > PI) { n         A[++la] = (line){-PI, r - 2 * PI}; n         r = PI; n       } n       A[++la] = (line){l, r}; n     } n   } n   if (!la) { n     printf( 0 ); n     return 0; n   } n   sort(A + 1, A + la + 1, cmp); n   now = A[1].y - A[1].x; n   Max = A[1].y; n   for (i = 2; i <= la; i++) { n     Max = max(Max, A[i].x); n     if (A[i].y < Max) continue; n     now += A[i].y - Max; n     Max = A[i].y; n   } n   ans = now / (2 * PI); n   printf( %.10lf , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n char A[21], B[21]; n long i, N, d, t, j, lg; n map<string, vector<pair<string, long> > > lista; n map<string, vector<pair<string, long> > >::const_iterator iter; n vector<pair<string, string> > rez; n string a, b; n inline int modul(long x) { return (x < 0) ? -x : ((x == 0) ? d + 1 : x); } n void adauga(string a, string b) { n   bool ok = true; n   long q, lg; n   for (q = 0, lg = rez.size(); q < lg; ++q) { n     if (rez[q].first.compare(a) == 0 && rez[q].second.compare(b) == 0) { n       ok = false; n       break; n     } n   } n   if (ok) { n     rez.push_back(make_pair(a, b)); n   } n } n int main() { n   scanf( %ld %ld , &N, &d); n   for (i = 1; i <= N; ++i) { n     scanf( %s %s %ld , A, B, &t); n     a = A; n     b = B; n     for (j = 0, lg = lista[b].size(); j < lg; ++j) { n       if (lista[b][j].first == a) { n         if (modul(lista[b][j].second - t) <= d) { n           if (a.compare(b) < 0) { n             adauga(a, b); n           } else { n             adauga(b, a); n           } n         } n       } n     } n     lista[a].push_back(make_pair(b, t)); n   } n   printf( %ld n , lg = rez.size()); n   for (i = 0; i < lg; ++i) { n     printf( %s %s n , rez[i].first.c_str(), rez[i].second.c_str()); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 1e7 + 7; n const int mod = 998244353; n const long long INF = 1e18; n long long jc[maxn]; n long long n, k; n long long inv2 = 499122177; n void init() { n   jc[0] = 1; n   for (long long i = 1; i < maxn; i++) jc[i] = jc[i - 1] * i % mod; n } n long long exgcd(long long a, long long b, long long &x, long long &y) { n   if (!b) { n     x = 1; n     y = 0; n     return a; n   } n   long long d = exgcd(b, a % b, y, x); n   y -= a / b * x; n   return d; n } n long long inv(long long a, long long n) { n   long long x, y; n   exgcd(a, n, x, y); n   return (x + n) % n; n } n long long C(long long n, long long m) { n   return jc[n] * inv(jc[m] * jc[n - m] % mod, mod) % mod; n } n long long qpow(long long x, int n) { n   long long res = 1; n   while (n) { n     if (n & 1) res = (res * x) % mod; n     x = (x * x) % mod; n     n /= 2; n   } n   return res; n } n void rua() { n   init(); n   scanf( %lld%lld , &n, &k); n   if (k >= n) { n     printf( 0 n ); n     return; n   } n   long long ans = 0; n   n -= k; n   for (int i = 0; i <= n; i += 2) n     ans = (ans + C(n, i) * qpow(n - i, n + k) % mod) % mod; n   for (int i = 1; i <= n; i += 2) n     ans = (ans - C(n, i) * qpow(n - i, n + k) % mod + mod) % mod; n   ans = ans * 2ll % mod * C(n + k, n) % mod; n   if (k == 0) ans = (ans * 499122177) % mod; n   printf( %lld n , ans); n } n int main() { n   rua(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int t, n; n   long long temp, ans; n   cin >> t; n   while (t--) { n     cin >> n; n     long long ary[n]; n     for (int i = 0; i < n; i++) { n       cin >> ary[i]; n     } n     ans = ary[0]; n     for (int i = 1; i < n; i++) { n       ans = ans & ary[i]; n     } n     cout << ans << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const long double eps = 1e-9; n long long xabs(long long a) { return a < 0 ? -a : +a; } n bool check(long long p, long long q, long long x, long long y, long long a, n            long long b) { n   if (a * x + b * y == p && a * y - b * x == q) n     return true; n   else n     return false; n } n bool can(long long ax, long long ay, long long bx, long long by, long long x, n          long long y) { n   long long p = bx - ax; n   long long q = by - ay; n   if (x == 0 && y == 0) { n     if (p == 0 && q == 0) n       return true; n     else n       return false; n   } n   if (y != 0) swap(x, y), swap(p, q); n   long long b = (p * y - q * x) / (y * y + x * x); n   long long a = (p - b * y) / x; n   if (check(p, q, x, y, a, b)) n     return true; n   else n     return false; n } n int main() { n   long long ax, ay, bx, by, x, y; n   cin >> ax >> ay >> bx >> by >> x >> y; n   bool fl = false; n   for (int i = 0; i < 4; ++i) { n     fl |= can(ax, ay, bx, by, x, y); n     swap(ax, ay); n     ax = -ax; n   } n   if (fl) n     cout <<  YES  << endl; n   else n     cout <<  NO  << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MOD(1000000007); n const int INF((1 << 30) - 1); n const int MAXN(1505); n vector<int> a[MAXN]; n int b[MAXN], ans[MAXN]; n struct pt { n   int x, y, idx; n   double r; n } d[MAXN]; n pt o; n bool compare(const pt &p, const pt &q) { n   long long u1 = p.x - o.x, v1 = p.y - o.y, u2 = q.x - o.x, v2 = q.y - o.y; n   return u1 * v2 - u2 * v1 > 0; n } n void DFS(int node, int p) { n   b[node] = 1; n   for (int i = 0; i < a[node].size(); i++) n     if (a[node][i] != p) { n       DFS(a[node][i], node); n       b[node] += b[a[node][i]]; n     } n } n void DFS2(int node, int p, int f, int l) { n   int id = f; n   for (int i = f + 1; i <= l; i++) n     if (pair<int, int>(d[i].y, d[i].x) < pair<int, int>(d[id].y, d[id].x)) n       id = i; n   swap(d[f], d[id]); n   o = d[f]; n   sort(d + f + 1, d + l + 1, compare); n   ans[d[f].idx] = node; n   f++; n   for (int i = 0; i < a[node].size(); i++) n     if (a[node][i] != p) { n       int u = a[node][i]; n       DFS2(u, node, f, f + b[u] - 1); n       f += b[u]; n     } n } n int main() { n   int n; n   scanf( %d , &n); n   for (int i = 0; i < n - 1; i++) { n     int u, v; n     scanf( %d%d , &u, &v); n     a[u].push_back(v); n     a[v].push_back(u); n   } n   DFS(1, -1); n   for (int i = 0; i < n; i++) { n     scanf( %d%d , &d[i].x, &d[i].y); n     d[i].idx = i; n   } n   DFS2(1, -1, 0, n - 1); n   for (int i = 0; i < n; i++) printf( %d  , ans[i]); n } n


#include <bits/stdc++.h> n #pragma GCC optimize( Ofast ) n #pragma GCC optimize( unroll-loops ) n #pragma GCC target(   n      sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,tune=native ) n using namespace std; n template <typename T1, typename T2> n void ckmin(T1 &a, T2 b) { n   if (a > b) a = b; n } n template <typename T1, typename T2> n void ckmax(T1 &a, T2 b) { n   if (a < b) a = b; n } n int read() { n   int x = 0, f = 0; n   char ch = getchar(); n   while (!isdigit(ch)) f |= ch ==  - , ch = getchar(); n   while (isdigit(ch)) x = 10 * x + ch -  0 , ch = getchar(); n   return f ? -x : x; n } n template <typename T> n void print(T x) { n   if (x < 0) putchar( - ), x = -x; n   if (x >= 10) print(x / 10); n   putchar(x % 10 +  0 ); n } n template <typename T> n void print(T x, char let) { n   print(x), putchar(let); n } n const int mod = 998244353; n const int G = 3, Gi = 332748118; n void uad(int &x, int y) { n   x += y; n   if (x >= mod) x -= mod; n } n int add(int x, int y) { n   if ((x += y) >= mod) x -= mod; n   return x; n } n int sub(int x, int y) { n   if ((x -= y) < 0) x += mod; n   return x; n } n int qpow(int a, int b = mod - 2) { n   int res = 1; n   while (b > 0) { n     if (b & 1) res = 1ll * res * a % mod; n     a = 1ll * a * a % mod; n     b >>= 1; n   } n   return res; n } n namespace Module { n using namespace std; n const int N = 1 << 19, P = 998244353; n int Pow(long long x, int y = P - 2) { n   int ans = 1; n   for (; y; y >>= 1, x = x * x % P) n     if (y & 1) ans = ans * x % P; n   return ans; n } n int w[N]; n unsigned long long F[N]; n struct init { n   init() { n     for (int i = 1; i < N; i <<= 1) { n       w[i] = 1; n       int t = Pow(3, (P - 1) / i / 2); n       for (int j = 1; j < i; ++j) w[i + j] = (long long)w[i + j - 1] * t % P; n     } n   } n } foo; n int Get(int x) { n   int n = 1; n   while (n <= x) n <<= 1; n   return n; n } n void DFT(vector<int> &f, int n) { n   if ((int)f.size() != n) f.resize(n); n   for (int i = 0, j = 0; i < n; ++i) { n     F[i] = f[j]; n     for (int k = n >> 1; (j ^= k) < k; k >>= 1) n       ; n   } n   for (int i = 1; i < n; i <<= 1) n     for (int j = 0; j < n; j += i << 1) { n       int *W = w + i; n       unsigned long long *F0 = F + j, *F1 = F + j + i; n       for (int k = j; k < j + i; ++k, ++W, ++F0, ++F1) { n         unsigned long long t = (*F1) * *W % P; n         (*F1) = *F0 + P - t, (*F0) += t; n       } n     } n   for (int i = 0; i < n; ++i) f[i] = F[i] % P; n } n void IDFT(vector<int> &f, int n) { n   f.resize(n), reverse(f.begin() + 1, f.end()), DFT(f, n); n   int I = 1; n   for (int i = 1; i < n; i <<= 1) I = (long long)I * (P + 1) / 2 % P; n   for (int i = 0; i < n; ++i) f[i] = (long long)f[i] * I % P; n } n vector<int> operator+(const vector<int> &f, const vector<int> &g) { n   vector<int> ans = f; n   ans.resize(max(f.size(), g.size())); n   for (int i = 0; i < (int)g.size(); ++i) (ans[i] += g[i]) %= P; n   return ans; n } n vector<int> operator*(const vector<int> &f, const vector<int> &g) { n   vector<int> F, G; n   F = f, G = g; n   int p = Get(f.size() + g.size() - 2); n   DFT(F, p), DFT(G, p); n   for (int i = 0; i < p; ++i) F[i] = (long long)F[i] * G[i] % P; n   IDFT(F, p); n   return F.resize(f.size() + g.size() - 1), F; n } n vector<int> Inv(const vector<int> &f, int n = -1) { n   if (n == -1) n = f.size(); n   if (n == 1) return {Pow(f[0])}; n   vector<int> ans = Inv(f, (n + 1) / 2), tmp(f.begin(), f.begin() + n); n   int p = Get(n * 2 - 2); n   DFT(tmp, p), DFT(ans, p); n   for (int i = 0; i < p; ++i) n     ans[i] = (2 - (long long)ans[i] * tmp[i] % P + P) * ans[i] % P; n   IDFT(ans, p); n   return ans.resize(n), ans; n } n vector<int> Mod(const vector<int> &a, const vector<int> &b) { n   if (b.size() > a.size()) return a; n   vector<int> A = a, B = b, iB; n   int n = a.size(), m = b.size(); n   reverse(A.begin(), A.end()), reverse(B.begin(), B.end()); n   B.resize(n - m + 1), iB = Inv(B, n - m + 1); n   vector<int> d = A * iB; n   d.resize(n - m + 1), reverse(d.begin(), d.end()); n   vector<int> r = b * d; n   r.resize(m - 1); n   for (int i = 0; i < m - 1; ++i) r[i] = (a[i] - r[i] + P) % P; n   return r; n } n vector<int> Prod(const vector<int> &a, const vector<int> &b) { return a * b; } n void wf(vector<int> &a) { n   int n = a.size(); n   for (int i = 0; i < n; i++) a[i] = 1ll * (i + 1) * a[i + 1] % mod; n   a[n - 1] = 0; n } n void jf(vector<int> &a) { n   int n = a.size(); n   for (int i = n - 1; i >= 1; i--) a[i] = 1ll * a[i - 1] * qpow(i) % mod; n   a[0] = 0; n } n vector<int> Ln(vector<int> a) { n   vector<int> ta = a; n   wf(ta); n   int len = a.size(); n   a = ta * Inv(a), jf(a); n   a.resize(len); n   return a; n } n vector<int> Exp(vector<int> a) { n   if ((int(a.size())) == 1) return vector<int>(1, 1); n   int len = a.size(); n   vector<int> ta = a; n   ta.resize((len + 1) / 2); n   vector<int> tb = Exp(ta); n   tb.resize(len); n   vector<int> Lnb = Ln(tb); n   assert(Lnb.size() == len); n   for (int i = 0; i < len; i++) Lnb[i] = (a[i] - Lnb[i] + mod) % mod; n   Lnb[0] = (Lnb[0] + 1) % mod; n   tb = tb * Lnb; n   tb.resize(len); n   return tb; n } n }  // namespace Module n using Module::operator*; n const int N = 250005; n const int B = 30; n vector<int> adj[N]; n int n; n int son[N]; n void dfs1(int u, int fa) { n   for (auto v : adj[u]) { n     if (v == fa) continue; n     son[u]++; n     dfs1(v, u); n   } n } n int two[N], len; n vector<int> solve(int l, int r) { n   if (l > r) return {1}; n   if (l == r) return {1, mod - two[l]}; n   int mid = l + r >> 1; n   return solve(l, mid) * solve(mid + 1, r); n } n int fac[N], inv[N]; n int fuck[N]; n int main() { n   n = read(); n   inv[1] = 1; n   for (int i = 2; i <= n; i++) n     inv[i] = 1ull * (mod - mod / i) * inv[mod % i] % mod; n   fac[0] = 1; n   for (int i = 1; i <= n; i++) fac[i] = 1ull * fac[i - 1] * i % mod; n   for (int i = 1; i <= n - 1; i++) { n     int u = read(), v = read(); n     adj[u].push_back(v), adj[v].push_back(u); n   } n   dfs1(1, 0); n   vector<int> ONE(n + 1); n   for (int i = 1; i <= n; i++) { n     if (son[i] > B) { n       two[++len] = son[i]; n     } else { n       fuck[son[i]]++; n     } n   } n   for (int i = 1; i <= B; i++) { n     int coef = 1; n     for (int j = 1; j <= n; j++) { n       coef = 1ull * coef * i % mod; n       ONE[j] = sub(ONE[j], 1ull * fuck[i] * coef % mod * inv[j] % mod); n     } n   } n   ONE = Module::Exp(ONE); n   vector<int> TWO = solve(1, len); n   vector<int> f = ONE * TWO; n   int ans = 0; n   for (int i = 0; i <= n - 1; i++) ans = (ans + 1ll * fac[n - i] * f[i]) % mod; n   print(ans,   n ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 200 * 1000 + 5; n vector<pair<int, int> > adj[N]; n int w[N]; n int d[N]; n bool mark[N]; n set<pair<int, int> > s; n vector<int> ans; n int main() { n   int n, m; n   cin >> n >> m; n   for (int i = 1; i <= n; i++) { n     cin >> w[i]; n   } n   for (int i = 1; i <= m; i++) { n     int u, v; n     cin >> u >> v; n     adj[u].push_back({v, i}); n     adj[v].push_back({u, i}); n   } n   for (int i = 1; i <= n; i++) { n     d[i] = (int)adj[i].size() - w[i]; n     s.insert({d[i], i}); n   } n   while (s.size()) { n     int v = (*s.begin()).second; n     s.erase(s.begin()); n     mark[v] = true; n     if (d[v] > 0) { n       cout <<  DEAD ; n       return 0; n     } n     for (auto p : adj[v]) { n       int u = p.first; n       if (!mark[u]) { n         s.erase({d[u], u}); n         d[u]--; n         s.insert({d[u], u}); n         ans.push_back(p.second); n       } n     } n   } n   cout <<  ALIVE n ; n   reverse(ans.begin(), ans.end()); n   for (auto x : ans) { n     cout << x <<    ; n   } n   return 0; n } n


#include <bits/stdc++.h> n char zebra[100005]; n int max; n int main() { n   scanf( %s , zebra + 1); n   max = 0; n   int n = strlen(zebra + 1); n   int t = 0; n   int ans = 0; n   for (int i = 1; i <= n; i++) { n     int k = 1; n     while (zebra[i] != zebra[i + 1] && i + 1 <= n) { n       k++; n       i++; n     } n     if (i == n && t == 0) { n       if (zebra[i] != zebra[1]) { n         k++; n         t++; n         i = 1; n       } n     } n     while (zebra[i] != zebra[i + 1] && i + 1 <= n) { n       k++; n       i++; n     } n     if (k > n) k = n; n     if (k > ans) ans = k; n   } n   printf( %d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n bool exceed(long long x, long long y, long long m) { return x >= m / y + 1; } n struct SegTree { n   int size; n   vector<pair<long long, long long> > seg; n   SegTree() {} n   SegTree(int size) { n     this->size = size; n     seg.resize(1 << (size + 1)); n   } n   pair<long long, long long> Ident() { n     return pair<long long, long long>(-1e18, -1e18); n   } n   pair<long long, long long> ope(pair<long long, long long> a, n                                  pair<long long, long long> b) { n     return max(a, b); n   } n   void init() { n     for (int i = 0; i < (1 << (size + 1)); i++) seg[i] = Ident(); n   } n   void update(int i, pair<long long, long long> val) { n     i += (1 << size); n     seg[i] = val; n     while (i > 1) { n       i /= 2; n       seg[i] = ope(seg[i * 2], seg[i * 2 + 1]); n     } n   } n   pair<long long, long long> query(int a, int b, int k, int l, int r) { n     if (b < l || r < a) return Ident(); n     if (a <= l && r <= b) return seg[k]; n     pair<long long, long long> lval = query(a, b, k * 2, l, (l + r) / 2); n     pair<long long, long long> rval = n         query(a, b, k * 2 + 1, (l + r) / 2 + 1, r); n     return ope(lval, rval); n   } n   pair<long long, long long> query(int a, int b) { n     if (a > b) return Ident(); n     return query(a, b, 1, 0, (1 << size) - 1); n   } n }; n long long n; n long long a[500005], b[500005]; n SegTree seg(19); n bool used[500005]; n long long ans[500005]; n vector<long long> topo; n void dfs(int v) { n   used[v] = true; n   seg.update(v, pair<long long, long long>(-1e18, -1e18)); n   if (a[v] <= n && !used[a[v]]) dfs(a[v]); n   while (1) { n     pair<long long, long long> res = seg.query(1, b[v] - 1); n     if (res.first <= v) break; n     dfs(res.second); n   } n   topo.push_back(v); n } n int main(void) { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cin >> n; n   for (long long(i) = (1); (i) <= (n); (i)++) { n     cin >> a[i]; n     if (a[i] == -1) a[i] = n + 1; n   } n   for (long long(i) = (1); (i) <= (n); (i)++) b[a[i]] = i; n   for (long long(i) = (1); (i) <= (n); (i)++) n     if (b[i] == 0) b[i] = n + 1; n   seg.init(); n   for (long long(i) = (1); (i) <= (n); (i)++) n     seg.update(i, pair<long long, long long>(a[i], i)); n   for (long long(i) = (1); (i) <= (n); (i)++) n     if (!used[i]) dfs(i); n   reverse((topo).begin(), (topo).end()); n   for (long long(i) = 0; (i) < (long long)(topo).size(); (i)++) n     ans[topo[i]] = i + 1; n   for (long long(i) = (1); (i) <= (n); (i)++) cout << ans[i] <<    ; n   cout << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 200007; n const int mod = 998244353; n long long fast_pow(long long b, int k) { n   long long s = 1; n   while (k) { n     if (k & 1) (s *= b) %= mod; n     (b *= b) %= mod; n     k >>= 1; n   } n   return s; n } n long long fact[maxn]; n int N; n int A[maxn]; n int sum[maxn], sum0[maxn]; n int C[maxn]; n void update(int x) { n   while (x <= N) C[x]++, x += x & (-x); n   return; n } n int query(int x) { n   int re = 0; n   while (x) re += C[x], x &= (x - 1); n   return re; n } n int main() { n   scanf( %d , &N); n   for (int i = 1; i <= N; i++) { n     scanf( %d , &A[i]); n     if (A[i] == -1) A[i] = 0; n     sum0[i] += sum0[i - 1] + (A[i] == 0); n     if (A[i] > N) { n       printf( 0 ); n       return 0; n     } n     sum[A[i]]++; n   } n   for (int i = 2; i <= N; i++) sum[i] += sum[i - 1]; n   fact[0] = 1; n   for (int i = 1; i <= N; i++) fact[i] = fact[i - 1] * i % mod; n   long long x, y; n   long long ans = 0; n   if (sum[0]) n     for (int i = 1; i <= N; i++) { n       if (A[i] == 0) continue; n       x = A[i] - sum[A[i]]; n       y = sum0[N] - sum0[i]; n       (ans += x * y % mod * fact[sum[0] - 1] % mod) %= mod; n       x = (N - A[i]) - (sum[N] - sum[A[i]]); n       y = sum0[i]; n       (ans += x * y % mod * fact[sum[0] - 1] % mod) %= mod; n     } n   x = 1ll * sum[0] * (sum[0] - 1) / 2 % mod; n   if (sum[0] > 1) n     y = fact[sum[0] - 2]; n   else n     y = 0; n   (ans += y * x % mod * x % mod) %= mod; n   y = 0; n   for (int i = 1; i <= N; i++) { n     if (A[i] == 0) continue; n     x = query(N) - query(A[i]); n     (y += x) %= mod; n     update(A[i]); n   } n   if (sum[0] > 0) n     x = fact[sum[0]]; n   else n     x = 1; n   (ans += y * x % mod) %= mod; n   (ans *= fast_pow(fact[sum[0]], mod - 2)) %= mod; n   printf( %lld , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int FASTBUFFER; n const int N = 1005; n const int INF = 1000000000; n char str[N][N]; n int n, m, l[N], r[N]; n int main() { n   scanf( %d %d , &n, &m); n   for (int i = 1; i <= n; i++) { n     scanf( %s , str[i] + 1); n   } n   for (int i = 1; i <= n; i++) { n     int ll = m + 1, rr = 0; n     for (int j = 1; j <= m; j++) { n       if (str[i][j] ==  X ) { n         ll = j; n         break; n       } n     } n     for (int j = m; j >= 1; j--) { n       if (str[i][j] ==  X ) { n         rr = j; n         break; n       } n     } n     for (int j = ll; j <= rr; j++) { n       if (str[i][j] !=  X ) { n         printf( -1 n ); n         return 0; n       } n     } n     l[i] = ll, r[i] = rr; n   } n   int st, end; n   for (int i = 1; i <= n; i++) { n     if (l[i] != m + 1) { n       st = i; n       break; n     } n   } n   for (int i = n; i >= 1; i--) { n     if (l[i] != m + 1) { n       end = i; n       break; n     } n   } n   for (int i = st; i <= end; i++) { n     if (l[i] == m + 1) { n       printf( -1 n ); n       return 0; n     } n   } n   int ans1 = -1, ans2 = -1, flag = 0; n   for (int i = st + 1; i <= end; i++) { n     if (l[i] < l[i - 1] || r[i] < r[i - 1]) { n       printf( -1 n ); n       return 0; n     } n     if (l[i] > r[i - 1]) { n       printf( -1 n ); n       return 0; n     } n     if (r[i] > r[i - 1]) { n       flag = 1; n     } n     if (l[i] > l[i - 1] && ans1 == -1) { n       int last; n       for (last = i - 1; last >= st; last--) { n         if (r[last] != r[i]) { n           break; n         } n       } n       if (last < st) { n         last++; n       } n       ans1 = r[last] - l[i] + 1; n     } n   } n   int ans = INF; n   if (ans1 == -1) { n     if (st == end) { n       printf( 1 n ); n       return 0; n     } else { n       if (flag) { n         ans1 = r[st] - l[st] + 1; n       } else { n         printf( %d n , min(end - st + 1, r[st] - l[st] + 1)); n         return 0; n       } n     } n   } n   for (int ans1 = 1; ans1 <= r[st] - l[st] + 1; ans1++) { n     for (int ans2 = 1; ans2 <= end - st + 1; ans2++) { n       int ok = 1; n       for (int i = st; i <= st + ans2 - 1; i++) { n         if (l[i] != l[st]) { n           ok = 0; n           break; n         } n       } n       if (ok) { n         int top = st, bo = st + ans2 - 1, now = l[st]; n         while (bo < end) { n           while (r[top] > now + ans1 - 1) { n             now++; n           } n           top++; n           bo++; n           if (now + ans1 - 1 > r[top]) { n             ok = 0; n           } n           if (now != l[bo]) { n             ok = 0; n           } n         } n         for (int i = top; i <= bo; i++) { n           if (r[i] != r[top]) { n             ok = 0; n           } n         } n       } n       if (ok) { n         ans = min(ans, ans1 * ans2); n       } n     } n   } n   if (ans == INF) { n     printf( -1 n ); n     return 0; n   } else { n     printf( %d n , ans); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0), cout.tie(0); n   int t; n   cin >> t; n   while (t--) { n     int n, i, j; n     cin >> n; n     vector<int> icons(n); n     for (i = 0; i < n; i++) { n       cin >> icons[i]; n     } n     vector<int> num; n     sort(icons.begin(), icons.end()); n     int cnt = 1; n     for (i = 1; i < icons.size(); i++) { n       if (icons[i] == icons[i - 1]) { n         cnt++; n       } else { n         num.push_back(cnt); n         cnt = 1; n       } n     } n     num.push_back(cnt); n     int x = num[0] + 1; n     int minNum = 1e9 + 7; n     for (i = 1; i <= x; i++) { n       int sum = 0; n       for (j = 0; j < num.size(); j++) { n         int q = ceil((float)num[j] / i); n         if ((i - 1) * q > num[j]) { n           sum = 1e9 + 7; n           break; n         } n         sum += q; n       } n       if (sum < minNum) minNum = sum; n     } n     cout << minNum << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long INF = numeric_limits<int>::max(); n const long long LINF = numeric_limits<long long>::max() / 2; n const double DINF = numeric_limits<double>::max(); n const int ITER = 3000; n const double EPS = 1e-9; n const int MAXN = 1005; n struct addr { n   int x, y; n   addr(){}; n   addr(int a, int b) { n     x = a; n     y = b; n   } n }; n struct cell { n   addr top, bot, left, right; n   addr cur; n   cell(){}; n }; n cell a[MAXN][MAXN]; n int val[MAXN][MAXN]; n int getVal(addr cur) { return val[cur.x][cur.y]; } n cell moveLeft(cell x) { n   x = a[x.left.x][x.left.y]; n   return x; n } n cell moveRight(cell x) { n   x = a[x.right.x][x.right.y]; n   return x; n } n cell moveTop(cell x) { n   x = a[x.top.x][x.top.y]; n   return x; n } n cell moveBot(cell x) { n   x = a[x.bot.x][x.bot.y]; n   return x; n } n string strad(addr x) { n   stringstream ss; n   ss <<  (  << x.x <<  ,   << x.y <<  ) ; n   return ss.str(); n } n void solve() { n   int n, m, q; n   cin >> n >> m >> q; n   for (int i = 0; i < MAXN; ++i) { n     for (int j = 0; j < MAXN; ++j) { n       a[i][j].cur.x = i; n       a[i][j].cur.y = j; n       if (i > 0) a[i][j].top = addr(i - 1, j); n       if (i < MAXN - 1) a[i][j].bot = addr(i + 1, j); n       if (j < MAXN - 1) a[i][j].right = addr(i, j + 1); n       if (j > 0) a[i][j].left = addr(i, j - 1); n     } n   } n   for (int i = 1; i <= n; ++i) { n     for (int j = 1; j <= m; ++j) { n       cin >> val[i][j]; n     } n   } n   cout << endl; n   cell it = a[0][0]; n   while (q--) { n     int x, y, c, d, h, w; n     cin >> x >> y >> c >> d >> h >> w; n     cell first = a[0][0]; n     cell second = a[0][0]; n     for (int i = 0; i < x; ++i) first = moveBot(first); n     for (int i = 0; i < y; ++i) first = moveRight(first); n     for (int i = 0; i < c; ++i) second = moveBot(second); n     for (int i = 0; i < d; ++i) second = moveRight(second); n     for (int i = 0; i < w; ++i) { n       addr ftop = first.top; n       addr stop = second.top; n       a[ftop.x][ftop.y].bot = second.cur; n       a[stop.x][stop.y].bot = first.cur; n       a[first.cur.x][first.cur.y].top = stop; n       a[second.cur.x][second.cur.y].top = ftop; n       if (i < w - 1) { n         first = moveRight(first); n         second = moveRight(second); n       } n     } n     for (int i = 0; i < h; ++i) { n       addr fright = first.right; n       addr sright = second.right; n       a[fright.x][fright.y].left = second.cur; n       a[sright.x][sright.y].left = first.cur; n       a[first.cur.x][first.cur.y].right = sright; n       a[second.cur.x][second.cur.y].right = fright; n       if (i < h - 1) { n         first = moveBot(first); n         second = moveBot(second); n       } n     } n     for (int i = 0; i < w; ++i) { n       addr fbot = first.bot; n       addr sbot = second.bot; n       a[fbot.x][fbot.y].top = second.cur; n       a[sbot.x][sbot.y].top = first.cur; n       a[first.cur.x][first.cur.y].bot = sbot; n       a[second.cur.x][second.cur.y].bot = fbot; n       if (i < w - 1) { n         first = moveLeft(first); n         second = moveLeft(second); n       } n     } n     for (int i = 0; i < h; ++i) { n       addr sleft = second.left; n       addr fleft = first.left; n       a[fleft.x][fleft.y].right = second.cur; n       a[sleft.x][sleft.y].right = first.cur; n       a[first.cur.x][first.cur.y].left = sleft; n       a[second.cur.x][second.cur.y].left = fleft; n       if (i < h - 1) { n         first = moveTop(first); n         second = moveTop(second); n       } n     } n   } n   it = a[0][0]; n   it = moveRight(it); n   it = moveBot(it); n   for (int i = 0; i < n; ++i) { n     cell col = it; n     for (int j = 0; j < m; ++j) { n       cout << getVal(col.cur) <<    ; n       col = moveRight(col); n     } n     it = moveBot(it); n     cout <<   n ; n   } n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   solve(); n } n


#include <bits/stdc++.h> n using namespace std; n void print() { n   int n; n   cin >> n; n   int arr[n], mx = 0, sum = 0; n   for (int i = 0; i < n; i++) { n     cin >> arr[i]; n     sum += arr[i]; n     mx = max(mx, arr[i]); n   } n   if (mx * 2 > sum || (sum & 1)) n     cout <<  T  << endl; n   else n     cout <<  HL  << endl; n } n int main() { n   int t; n   cin >> t; n   while (t) { n     --t; n     print(); n   } n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T> n std::ostream &operator<<(std::ostream &out, vector<T> &v) { n   for (typename vector<T>::size_type i = 0; i < v.size(); ++i) n     out << v[i] <<    ; n   out <<   n ; n   return out; n } n template <typename T> n std::ostream &operator<<(std::ostream &out, set<T> &s) { n   for (auto e : s) out << e <<    ; n   out <<   n ; n   return out; n } n template <typename T, typename N> n std::ostream &operator<<(std::ostream &out, pair<T, N> &p) { n   out <<  (  << p.first <<  ,   << p.second <<  )  ; n   return out; n } n template <typename T, typename N> n std::ostream &operator<<(std::ostream &out, vector<pair<T, N> > &v) { n   for (size_t i = 0; i < v.size(); ++i) cout << v[i]; n   out <<   n ; n   return out; n } n template <typename T> n std::ostream &operator<<(std::ostream &out, vector<vector<T> > &v) { n   for (size_t i = 0; i < v.size(); ++i) { n     for (size_t j = 0; j < v[i].size(); ++j) { n       out << v[i][j] <<    ; n     } n     out <<   n ; n   } n   return out; n } n template <typename T> n std::ostream &operator<<(std::ostream &out, vector<set<T> > &v) { n   for (size_t i = 0; i < v.size(); ++i) { n     out << v[i]; n   } n   out <<   n ; n   return out; n } n void solve() { n   int n; n   cin >> n; n   vector<int> a(n); n   int total = 0; n   for (int i = 0; i < n; ++i) { n     cin >> a[i]; n     total += a[i]; n   } n   int diff = 0; n   for (int i = n - 1; i >= 0; --i) { n     diff = abs(diff - a[i]); n   } n   cout << (total - diff) / 2 <<     << (total + diff) / 2 <<   n ; n   return; n } n int main() { n   std::ios::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   int t; n   t = 1; n   while (t--) { n     solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<int> g[100001]; n int dist[100001]; n int n, m; n void bfs(int x) { n   queue<int> q; n   int cur, i; n   memset(dist, -1, sizeof(dist)); n   dist[x] = 0; n   q.push(x); n   while (!q.empty()) { n     cur = q.front(); n     q.pop(); n     for (i = 0; i < (int)g[cur].size(); i++) { n       if (dist[g[cur][i]] == -1) { n         dist[g[cur][i]] = dist[cur] + 1; n         q.push(g[cur][i]); n       } n     } n   } n } n int main() { n   int i, a, b, x, res; n   scanf( %d%d , &n, &m); n   for (i = 0; i < m; i++) { n     scanf( %d%d , &a, &b); n     g[a].push_back(b); n     g[b].push_back(a); n   } n   bfs(1); n   res = 0; n   x = 1; n   for (i = 1; i <= n; i++) { n     if (dist[i] > res) { n       res = dist[i]; n       x = i; n     } n   } n   bfs(x); n   res = 0; n   for (i = 1; i <= n; i++) { n     res = max(res, dist[i]); n   } n   printf( %d n , res); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios::sync_with_stdio(0); n   char board[9][9]; n   char trash; n   for (long r = 0; r < 3; ++r) { n     for (long x = 0; x < 3; ++x) { n       for (long c = 0; c < 3; ++c) { n         for (long y = 0; y < 3; ++y) cin >> board[3 * r + x][3 * c + y]; n         if (c == 5) cin >> trash; n       } n     } n   } n   long x, y; n   cin >> x >> y; n   --x, --y; n   x %= 3; n   y %= 3; n   bool empty = false; n   for (long i = 0; i < 3; ++i) n     for (long j = 0; j < 3; ++j) n       if (board[3 * x + i][3 * y + j] ==  . ) empty = true; n   if (empty) { n     for (long i = 0; i < 3; ++i) n       for (long j = 0; j < 3; ++j) n         if (board[3 * x + i][3 * y + j] ==  . ) n           board[3 * x + i][3 * y + j] =  ! ; n   } else { n     for (long i = 0; i < 9; ++i) n       for (long j = 0; j < 9; ++j) n         if (board[i][j] ==  . ) board[i][j] =  ! ; n   } n   for (long r = 0; r < 3; ++r) { n     for (long x = 0; x < 3; ++x) { n       for (long c = 0; c < 3; ++c) { n         for (long y = 0; y < 3; ++y) cout << board[3 * r + x][3 * c + y]; n         if (c < 2) cout <<    ; n       } n       cout <<   n ; n     } n     cout <<   n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int n, p; n int main() { n   map<int, int> mp; n   cin >> n; n   for (int i = 1; i <= 2 * n + 1; i++) { n     if (p % n == 0) { n       mp[n]++; n     } n     mp[p % n]++; n     p += i - 1; n   } n   for (int i = 1; i <= n; i++) { n     if (mp[i] == 0) { n       cout <<  NO ; n       return 0; n     } n   } n   cout <<  YES  n        <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 3e2 + 5; n int N, T; n int pref[MAXN]; n int ask(int l, int r) { n   printf( ? %d %d n , l, r); n   fflush(stdout); n   int res; n   scanf( %d , &res); n   if (res == -1) exit(0); n   return res; n } n int main() { n   scanf( %d%d , &N, &T); n   if (N == 1) { n     printf( ! %d n , T); n     fflush(stdout); n     return 0; n   } n   int k = N % 2, curr = T; n   for (int i = 1; i <= N - k; i++) { n     int lft = 0, rig = 0; n     bool ok = false; n     while (!ok || lft || rig) { n       int nxt = ask(i, i + k); n       if ((curr + nxt) % 2 == (i + k) % 2) { n         lft ^= 1; n         if (lft && !rig) { n           pref[i + k] = (i + k - nxt + T) / 2; n           ok = true; n         } n       } else n         rig ^= 1; n       curr = nxt; n     } n   } n   if (k) { n     int it = 0; n     while (1) { n       int nxt = ask(2, N); n       if (!((curr + nxt) % 2)) { n         pref[1] = it ? (T - nxt + 1) / 2 : (nxt + T - N + 1) / 2; n         break; n       } n       curr = nxt; n       it ^= 1; n     } n   } n   printf( !  ); n   for (int i = 1; i <= N; i++) printf( %d , pref[i] - pref[i - 1]); n   puts(  ); n   fflush(stdout); n   return 0; n } n


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


#include <bits/stdc++.h> n using namespace std; n clock_t start; n mt19937_64 rng(chrono::system_clock::now().time_since_epoch().count()); n struct MaxFlow { n   struct Edge { n     long long f; n     long long u; n     Edge* nex; n     Edge* rev; n     Edge(long long _u, long long _f, Edge* _nex) : f(_f), u(_u), nex(_nex) {} n   }; n   vector<Edge*> g; n   MaxFlow(long long n) { g.resize(n); } n   void AddEdge(long long v, long long u, long long f) { n     g[v] = new Edge(u, f, g[v]); n     g[u] = new Edge(v, 0, g[u]); n     g[v]->rev = g[u]; n     g[u]->rev = g[v]; n   } n   long long Flow(long long src, long long sink, long long n) { n     long long flow = 0; n     while (true) { n       vector<Edge*> par(n); n       vector<bool> vis(n, false); n       vector<long long> mn(n, 0); n       queue<long long> q; n       q.push(src); n       vis[src] = 1; n       mn[src] = (long long)(2e18); n       while (!q.empty()) { n         long long v = q.front(); n         q.pop(); n         for (Edge* e = g[v]; e; e = e->nex) { n           if (e->f <= 0) continue; n           if (!vis[e->u]) { n             mn[e->u] = min(mn[v], e->f); n             par[e->u] = e; n             vis[e->u] = 1; n             q.push(e->u); n           } n         } n       } n       long long curr = sink; n       long long ff = mn[sink]; n       if (ff == 0) break; n       flow += ff; n       while (curr != src) { n         Edge* e = par[curr]; n         e->f -= ff; n         e->rev->f += ff; n         curr = e->rev->u; n       } n     } n     return flow; n   } n }; n const long long N = 3e5 + 10; n bool pr[N]; n void seive() { n   for (long long i = 2; i < N; i++) { n     if (pr[i]) continue; n     for (long long j = i + i; j < N; j += i) pr[j] = 1; n   } n } n void solve() { n   long long n, k; n   cin >> n >> k; n   long long p[n], c[n], l[n]; n   for (long long i = 0; i < n; i++) { n     cin >> p[i] >> c[i] >> l[i]; n   } n   long long s = 1, e = n, ans = -1; n   while (s <= e) { n     long long mid = (s + e) / 2, sum = 0; n     vector<pair<long long, long long> > edge; n     long long mx = 0; n     for (long long i = 0; i < n; i++) { n       if (l[i] <= mid && c[i] != 1) { n         sum += p[i]; n         edge.push_back({p[i], c[i]}); n       } n       if (c[i] == 1 && l[i] <= mid) { n         mx = max(mx, p[i]); n       } n     } n     if (mx) edge.push_back({mx, 1}); n     sum += mx; n     MaxFlow M((long long)((edge).size()) + 4); n     long long src = (long long)((edge).size()), n               sink = (long long)((edge).size()) + 1; n     for (long long i = 0; i < (long long)((edge).size()); i++) { n       for (long long j = i + 1; j < (long long)((edge).size()); j++) { n         if (!pr[edge[i].second + edge[j].second]) { n           if (edge[i].second % 2) n             M.AddEdge(i, j, edge[i].first); n           else n             M.AddEdge(j, i, edge[j].first); n         } n       } n       if (edge[i].second % 2) n         M.AddEdge(src, i, edge[i].first); n       else n         M.AddEdge(i, sink, edge[i].first); n     } n     long long flow = M.Flow(src, sink, sink + 4); n     if (sum - flow >= k) { n       ans = mid; n       e = mid - 1; n     } else n       s = mid + 1; n   } n   cout << ans <<   n ; n } n signed main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   long long t = 1; n   start = clock(); n   cout << fixed << setprecision(20); n   seive(); n   for (long long i = 1; i <= t; i++) { n     solve(); n   } n   return 0; n } n