#include <bits/stdc++.h> n using namespace std; n using ll = long long; n using ld = long double; n using pii = pair<int, int>; n constexpr int MAXN = 1003; n constexpr int MAXC = 10004; n int n; n ll w, b, x; n int c[MAXN]; n ll cost[MAXN]; n ll dp[MAXN][MAXC]; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   cin >> n >> w >> b >> x; n   for (int i = 0; i < n; ++i) cin >> c[i]; n   for (int i = 0; i < n; ++i) cin >> cost[i]; n   memset(dp, -1, sizeof(dp)); n   dp[0][0] = w; n   for (int i = 0; i < n; ++i) { n     for (int j = 0; j < MAXC and dp[i][j] > -1; ++j) { n       for (ll k = 0; k <= c[i] and dp[i][j] - cost[i] * k >= 0; ++k) { n         dp[i + 1][j + k] = max( n             dp[i + 1][j + k], min(w + (j + k) * b, dp[i][j] + x - cost[i] * k)); n       } n     } n   } n   int ans = 0; n   for (int j = 0; j < MAXC; ++j) { n     if (dp[n][j] != -1) ans = max(ans, j); n   } n   cout << ans <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int t, n, k; n   int nums[1005]; n   cin >> t; n   while (t--) { n     cin >> n >> k; n     for (int i = 0; i < n; i++) cin >> nums[i]; n     sort(nums, nums + n); n     int sum = 0; n     for (int i = 1; i < n; i++) sum += (k - nums[i]) / nums[0]; n     cout << sum <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m, u, v, num_col = 1; n vector<int> adjList[5001]; n vector<int> edges[5001]; n bitset<5001> vis; n bitset<5001> act; n bitset<5001> col; n void dfs(int nd) { n   vis[nd] = 1; n   act[nd] = 1; n   for (int k = 0; k < adjList[nd].size(); k++) { n     int nd_b = adjList[nd][k]; n     int edge_num = edges[nd][k]; n     if (!vis[nd_b]) n       dfs(nd_b); n     else if (act[nd_b]) { n       num_col = 2; n       col[edge_num] = 1; n     } n   } n   act[nd] = 0; n } n int main() { n   cin >> n >> m; n   for (int i = 0; i < m; i++) { n     cin >> u >> v; n     u--; n     v--; n     adjList[u].push_back(v); n     edges[u].push_back(i); n   } n   for (int i = 0; i < n; i++) { n     if (!vis[i]) dfs(i); n   } n   cout << num_col << endl; n   for (int i = 0; i < m; i++) { n     cout << (((int)col[i]) + 1) << (i < m - 1 ?     :   ); n   } n   cout << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n char s[100005]; n int main() { n   int n; n   while (cin >> n) { n     scanf( %s , s); n     int ct = 1, n = strlen(s); n     bool ok = false; n     int cant = 0, ans = 0; n     for (int i = 1; i < n; i++) { n       if (s[i] == s[i - 1]) n         ct++; n       else { n         if (ct >= 3) ok = true; n         if (ct >= 2) cant++; n         ans++; n         ct = 1; n       } n     } n     ans++; n     if (ct >= 3) ok = true; n     if (ct >= 2) cant++; n     if (ok) n       ans += 2; n     else { n       if (cant >= 2) n         ans += 2; n       else if (cant == 1) n         ans++; n     } n     cout << ans << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   int width; n   int height; n   cin >> height; n   cin >> width; n   char art[height][width]; n   for (int i = 0; i < height; i++) { n     for (int k = 0; k < width; k++) { n       cin >> art[i][k]; n     } n   } n   bool found_content = false; n   int max_width = width - 1; n   int min_width = 0; n   int max_height = height - 1; n   int min_height = 0; n   int collumn = 0; n   while (!found_content) { n     for (int i = 0; i < height && !found_content; i++) { n       if (art[i][collumn] ==  * ) { n         min_width = collumn; n         found_content = true; n       } n     } n     collumn++; n   } n   found_content = false; n   collumn = width - 1; n   while (!found_content) { n     for (int i = 0; i < height && !found_content; i++) { n       if (art[i][collumn] ==  * ) { n         max_width = collumn; n         found_content = true; n       } n     } n     collumn--; n   } n   found_content = false; n   int row = 0; n   while (!found_content) { n     for (int i = 0; i < width && !found_content; i++) { n       if (art[row][i] ==  * ) { n         min_height = row; n         found_content = true; n       } n     } n     row++; n   } n   found_content = false; n   row = height - 1; n   while (!found_content) { n     for (int i = 0; i < width && !found_content; i++) { n       if (art[row][i] ==  * ) { n         max_height = row; n         found_content = true; n       } n     } n     row--; n   } n   for (int r = min_height; r <= max_height; r++) { n     for (int c = min_width; c <= max_width; c++) { n       cout << art[r][c]; n     } n     cout <<   n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   int n, m = 0; n   cin >> n; n   while (n--) { n     int x, y; n     cin >> x >> y; n     m = max(m, x + y); n   } n   cout << m; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n using ll = signed long long int; n const int MX = 7000 + 10, INF = 3e5, A = 100; n ll res; n pair<ll, ll> ab[MX]; n int n; n map<ll, pair<int, ll>> m; n vector<ll> v; n bool ad; n int main() { n   ios_base::sync_with_stdio(false); n   cout.tie(nullptr); n   cin.tie(nullptr); n   v.reserve(MX); n   cin >> n; n   for (int i = 0; i < n; i++) { n     cin >> ab[i].first; n   } n   for (int i = 0; i < n; i++) { n     cin >> ab[i].second; n   } n   for (int i = 0; i < n; i++) { n     auto& p = m[ab[i].first]; n     p.first++; n     p.second += ab[i].second; n   } n   for (auto it = m.rbegin(); it != m.rend(); it++) { n     auto& p = *it; n     ad = false; n     if (p.second.first >= 2) { n       res += p.second.second; n       v.push_back(p.first); n     } else { n       for (auto i : v) { n         if ((p.first & i) == p.first) { n           res += p.second.second; n           break; n         } n       } n     } n   } n   cout << res; n } n


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


#include <bits/stdc++.h> n using namespace std; n long long h[100000]; n long long x[100000]; n int g[31] = {0, 1, 2, 1, 4, 3, 2, 1, 5, 6,  2, 1, 8, 7,  5, n              9, 8, 7, 3, 4, 7, 4, 2, 1, 10, 9, 3, 6, 11, 12}; n bool mark[100000]; n map<int, int> gg; n int grundy(int mask) { n   int nmask; n   int used[40]; n   memset(used, 0, sizeof(used)); n   for (int i = 1; i < 30; i++) { n     if (mask & (1 << i)) { n       nmask = mask; n       for (int k = i; k < 30; k += i) n         if (mask & 1 << k) nmask -= (1 << k); n       if (gg.count(nmask)) n         used[gg[nmask]] = 1; n       else { n         gg[nmask] = grundy(nmask); n         used[gg[nmask]] = 1; n       } n     } n   } n   for (int i = 0; i < 30; i++) { n     if (!used[i]) return i; n   } n } n int main() { n   int n, c1, s, r, m; n   cin >> n; n   h[1] = 1; n   s = n - (int)sqrt(n); n   c1 = s; n   for (int i = 2; i * i <= n; i++) { n     if (!mark[i]) { n       int j; n       int p = n; n       for (j = 0; p > 0; j++) { n         p /= i; n       } n       h[i] = j - 1; n     } n     long long k = i * i; n     while (k * k < n) { n       mark[k] = 1; n       k *= i; n     } n     c1 -= (h[i] + 1) / 2; n   } n   if (c1 % 2 == 0) n     r = 0; n   else n     r = 1; n   for (int i = 1; i * i <= n; i++) n     if (!mark[i]) r ^= g[h[i]]; n   if (r == 0) n     cout <<  Petya  << endl; n   else n     cout <<  Vasya  << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long N = 2e5 + 5; n const long long inf = 199999999999999999; n const long long mod = 1e9 + 7; n void read() { n   freopen( lifeguards.in ,  r , stdin); n   freopen( lifeguards.out ,  w , stdout); n } n void solve() { n   int coloured[N] = {}, pp[N] = {}, mx = 0, mx1 = 0, attribute = 0, cur = 0; n   long long n; n   cin >> n; n   vector<long long> v(n); n   for (auto &i : v) { n     cin >> i; n     if (i == cur) attribute = 1; n     cur = i; n   } n   if (v[0] == v[n - 1]) attribute = 1; n   if (is_sorted(v.begin(), v.end()) && v[0] == v[n - 1]) { n     cout <<  1 n ; n     for (int i = 0; i < n; ++i) { n       cout <<  1  ; n     } n     return; n   } n   if (n & 1 ^ 1) { n     cout <<  2 n ; n     for (int i = 0; i < n; ++i) { n       if (i & 1) n         cout <<  1  ; n       else n         cout <<  2  ; n     } n     return; n   } else { n     if (attribute) { n       cout <<  2 n ; n       long long op = 1, borely = 1; n       cur = 2; n       for (int i = 0; i < n; ++i) { n         if (v[i] == v[i - 1] && op) { n           if (cur == 2) n             cout <<  2  , cur = 2; n           else n             cout <<  1  , cur = 1; n           op ^= 1; n           continue; n         } n         if (cur == 2) n           cout <<  1  , cur = 1; n         else n           cout <<  2  , cur = 2; n       } n     } else { n       cout <<  3 n ; n       for (int i = 0; i < n - 1; ++i) { n         if (i & 1) n           cout <<  1  ; n         else n           cout <<  2  ; n       } n       cout << 3; n     } n   } n } n int32_t main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   long long T = 1; n   cin >> T; n   while (T--) { n     solve(); n     cout <<   n ; n   } n   cerr <<   nTime elapsed:  << 1.0 * clock() / CLOCKS_PER_SEC <<   s. n ; n } n


#include <bits/stdc++.h> n using namespace std; n int A[10], B[10]; n bool Check(int j) { n   memset(B, 0, sizeof(B)); n   while (j) { n     B[j % 10]++; n     j /= 10; n   } n   for (int i = 0; i <= 9; i++) n     if (A[i] && B[i]) return true; n   return false; n } n int main() { n   int res = 0, x, _x; n   cin >> x; n   _x = x; n   while (_x) { n     A[_x % 10]++; n     _x /= 10; n   } n   double s = sqrt(x); n   for (int i = 1; i <= s; i++) { n     if (x % i == 0) { n       if (Check(i)) res++; n       if (i * i != x && Check(x / i)) res++; n     } n   } n   cout << res << endl; n } n


#include <bits/stdc++.h> n using namespace std; n const long long INF = 1e9 + 5, NMAX = 2e5 + 5, MOD = 998244353, N = 1e9 + 5, n                 LOG = 25; n const long double EPS = 1e-8, pi = 3.14159265358979323846; n vector<long long> res; n bool first(long long k, vector<long long> &v) { n   long long index = k; n   res.clear(); n   vector<long long> ost; n   for (long long i = 0; i < k && index < v.size(); ++i, ++index) { n     while (index < v.size() && v[index] <= v[i]) ost.push_back(v[index++]); n     if (index == v.size()) return false; n     res.push_back(v[index]); n     res.push_back(v[i]); n   } n   while (index < v.size() && v[index] <= res.back()) ost.push_back(v[index++]); n   if (index == v.size()) return false; n   while (index < v.size()) res.push_back(v[index++]); n   for (long long o : ost) res.push_back(o); n   return true; n } n signed solve() { n   long long n; n   cin >> n; n   vector<long long> v(n); n   for (long long i = 0; i < n; ++i) cin >> v[i]; n   if (n < 3) { n     cout <<  0 n ; n     for (long long x : v) cout << x <<    ; n     return 0; n   } n   sort(v.begin(), v.end()); n   long long l = 0, r = n - 2; n   while (l + 1 < r) { n     long long m = (l + r) / 2; n     if (first(m, v)) { n       l = m; n     } else { n       r = m - 1; n     } n   } n   if (first(r, v)) { n     cout << r <<   n ; n     for (long long x : res) cout << x <<    ; n   } else { n     first(l, v); n     cout << l <<   n ; n     for (long long x : res) cout << x <<    ; n   } n } n signed main() { n   { n     ios_base::sync_with_stdio(0); n     cin.tie(0); n     cout.tie(0); n   } n   long long t = 1; n   while (t-- > 0) { n     solve(); n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long n, m, s, a, b, c, d, sum, ss; n   cin >> n >> m >> s; n   sum = 0; n   for (a = 1; a <= n && a <= s; a += 2) n     for (b = 1; b <= m && a * b <= s; b += 2) { n       if (a * b == s) { n         sum += n             (n - a + 1) * (m - b + 1) * (((a + 1) / 2) * ((b + 1) / 2) * 2 - 1); n         continue; n       } n       ss = s - a * b; n       for (c = 1; c <= n; c += 2) { n         if (c < a) { n           if (ss % c != 0) continue; n           d = ss / c + b; n           if (d > m || d % 2 != 1) continue; n           sum += (n - a + 1) * (m - d + 1); n         } else if (c > a) { n           if (ss % (c - a) != 0) continue; n           d = ss / (c - a); n           if (d >= b || d % 2 != 1) continue; n           sum += (n - c + 1) * (m - b + 1); n         } n       } n     } n   cout << sum << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n; n string s; n vector<int> v; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   cin >> n >> s; n   s =  *  + s; n   for (int i = 1; i * i <= n; i++) n     if (n % i == 0) { n       v.push_back(i); n       if (i != n / i) v.push_back(n / i); n     } n   sort(v.begin(), v.end()); n   for (int x : v) reverse(s.begin() + 1, s.begin() + 1 + x); n   s.erase(0, 1); n   cout << s; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long MOD = 1000000007; n bool used[123456]; n bool cmp(const vector<int>& a, const vector<int>& b) { n   return a.size() < b.size(); n } n int main() { n   int n; n   cin >> n; n   vector<int> p(n + 1); n   for (int i = 1; i <= n; ++i) cin >> p[i]; n   vector<vector<int> > circle; n   for (int i = 1; i <= n; ++i) { n     if (!used[i]) { n       used[i] = 1; n       int curi = i; n       circle.push_back(vector<int>(1, i)); n       while (!used[p[curi]]) { n         curi = p[curi]; n         circle.back().push_back(curi); n         used[curi] = true; n       } n     } n   } n   bool ok = false; n   for (int i = 0; i < circle.size(); ++i) n     ok |= (circle[i].size() == 2 || circle[i].size() == 1); n   if (!ok) { n     cout <<  NO ; n     return 0; n   } n   sort(circle.begin(), circle.end(), cmp); n   if (circle[0].size() == 1) { n     cout <<  YES n ; n     for (int i = 1; i < circle.size(); ++i) n       for (int j = 0; j < circle[i].size(); ++j) n         cout << circle[0][0] <<     << circle[i][j] <<   n ; n     return 0; n   } n   for (int i = 0; i < circle.size(); ++i) { n     if (circle[i].size() % 2 != 0) { n       cout <<  NO ; n       return 0; n     } n   } n   cout <<  YES n ; n   cout << circle[0][1] <<     << circle[0][0] <<   n ; n   for (int i = 1; i < circle.size(); ++i) n     for (int j = 0; j < circle[i].size(); ++j) n       cout << circle[0][j % 2] <<     << circle[i][j] <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n using Vi = vector<int>; n using i64 = long long; n using ll = long long; n using Pi = pair<int, int>; n mt19937 mrand(time(0)); n int rnd(int x) { return mrand() % x; } n const int N = 500005; n const int M = 40005; n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   int T; n   cin >> T; n   while (T--) { n     int n; n     cin >> n; n     Vi a(n); n     for (int i = 0; i < n; ++i) cin >> a[i]; n     int TOP = 30; n     Vi bit(TOP); n     for (int i = 0; i < n; ++i) n       for (int j = 0; j < TOP; ++j) n         if (a[i] >> j & 1) ++bit[j]; n     Vi ans(n + 1); n     for (int i = 0; i < TOP; ++i) n       for (int j = 1; j < n + 1; ++j) n         if (bit[i] % j == 0) ++ans[j]; n     for (int i = 1; i < n + 1; ++i) n       if (ans[i] == TOP) cout << i <<    ; n     cout << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxN = 100 * 1000 + 5; n struct Node { n   int s, e, maxx; n }; n struct Segment { n   Node node[4 * maxN]; n   void build(int s, int e, int v = 1) { n     node[v].s = s; n     node[v].e = e; n     node[v].maxx = -1; n     if (e - s <= 1) return; n     int mid = (e + s) / 2; n     build(s, mid, v * 2); n     build(mid, e, v * 2 + 1); n   } n   void change(int s, int e, int x, int v = 1) { n     if (e <= node[v].s || node[v].e <= s) return; n     if (s <= node[v].s && node[v].e <= e) { n       node[v].maxx = max(x, node[v].maxx); n       return; n     } n     int l = v * 2, r = v * 2 + 1; n     change(s, e, x, l); n     change(s, e, x, r); n     node[v].maxx = max(node[l].maxx, node[r].maxx); n   } n   int get(int s, int e, int v = 1) { n     if (e <= s || e <= node[v].s || node[v].e <= s) return -1; n     if (s <= node[v].s && node[v].e <= e) return node[v].maxx; n     int l = v * 2, r = v * 2 + 1; n     return max(get(s, e, l), get(s, e, r)); n   } n }; n struct BIT { n   vector<int> val; n   void init(int sz) { val.resize(sz); } n   void add(int ind, int x) { n     ind++; n     for (int i = ind; i < ((int)(val).size()); i += i & (-i)) val[i] += x; n   } n   int get(int ind) { n     ind++; n     int res = 0; n     for (int i = ind; i > 0; i -= i & (-i)) res += val[i]; n     return res; n   } n   int getsum(int l, int r) { n     if (r < l) return 0; n     return get(r) - get(l - 1); n   } n }; n pair<int, int> p[maxN], que[maxN]; n pair<int, int> r[maxN]; n vector<int> a; n vector<int> lst[maxN]; n BIT baz; n int n, k; n int cnt[maxN], ans[maxN], pnt[maxN]; n int low(int x) { return lower_bound(a.begin(), a.end(), x) - a.begin(); } n int up(int x) { return upper_bound(a.begin(), a.end(), x) - a.begin(); } n Segment rmq; n int main() { n   ios_base::sync_with_stdio(false); n   cin >> n >> k; n   baz.init(n + 1); n   rmq.build(0, n + 1); n   a.resize(n); n   for (int i = 0; i < n; i++) n     cin >> r[i].first, r[i].second = i, p[i].second = r[i].first; n   for (int i = 0; i < n; i++) cin >> a[i], p[i].first = a[i]; n   sort(a.begin(), a.end()); n   a.resize(unique(a.begin(), a.end()) - a.begin()); n   sort(r, r + n); n   for (int i = 0; i < n; i++) pnt[r[i].second] = i; n   for (int i = 0; i < n;) { n     int ind = i; n     while (ind < n && (ind == i || r[ind].first == r[ind - 1].first)) { n       int x = p[r[ind].second].first; n       baz.add(low(x), 1); n       ind++; n     } n     ind = i; n     while (ind < n && (ind == i || r[ind].first == r[ind - 1].first)) { n       int x = p[r[ind].second].first; n       cnt[ind] = baz.getsum(low(x - k), up(x + k) - 1); n       ind++; n     } n     i = ind; n   } n   int Q; n   cin >> Q; n   for (int q = 0; q < Q; q++) { n     int x, y; n     cin >> x >> y; n     x--; n     y--; n     if (p[x].second > p[y].second) swap(x, y); n     que[q] = make_pair(x, y); n     lst[pnt[y]].push_back(q); n   } n   for (int i = n - 1; i >= 0;) { n     int ind = i; n     while (ind >= 0 && (ind == i || r[ind].first == r[ind + 1].first)) { n       int x = low(p[r[ind].second].first); n       rmq.change(x, x + 1, cnt[ind]); n       ind--; n     } n     ind = i; n     while (ind >= 0 && (ind == i || r[ind].first == r[ind + 1].first)) { n       for (int j = 0; j < ((int)(lst[ind]).size()); j++) { n         int u = lst[ind][j]; n         int x = p[que[u].first].first, y = p[que[u].second].first; n         if (x > y) swap(x, y); n         int L = low(y - k), R = up(x + k); n         ans[u] = rmq.get(L, R); n       } n       ind--; n     } n     i = ind; n   } n   for (int i = 0; i < Q; i++) cout << ans[i] << endl; n   return 0; n } n


#include <bits/stdc++.h> n static const int INF = 0x3f3f3f3f; n static const long long INFL = 0x3f3f3f3f3f3f3f3fLL; n static const long double epsilon = 1e-15; n static const long double pi = acos((long double)-1); n using namespace std; n inline void init_io() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n } n const int MAX_N = 100001; n int arr[2 * MAX_N]; n void rarr(int n) { n   for (int(i) = (0); (i) < (n); i++) cin >> arr[i]; n } n int main() { n   init_io(); n   int t; n   cin >> t; n   while (t--) { n     long long n, k; n     cin >> n >> k; n     long long ans = 0; n     while (n) { n       if (n % k) { n         ans += n % k; n         n = n - n % k; n       } else { n         ans++; n         n /= k; n       } n     } n     cout << ans <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 5e5 + 10; n pair<long long, long long> p[maxn]; n long long ans[maxn]; n int main() { n   int n, num = 0, cnt = 0; n   long long l, r; n   scanf( %d , &n); n   for (int i = 1; i <= n; i++) { n     scanf( %lld%lld , &l, &r); n     p[++num].first = l; n     p[num].second = 1; n     p[++num].first = r + 1; n     p[num].second = -1; n   } n   sort(p + 1, p + 1 + num); n   for (int i = 1; i <= num; i++) { n     ans[cnt] += p[i].first - p[i - 1].first; n     cnt += p[i].second; n   } n   for (int i = 1; i <= n; i++) printf( %lld%c , ans[i], i == n ?   n  :    ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m, f, vis[101][101][101]; n char a[1001][1001]; n int valid(int x, int y) { return (x >= 1 && x <= n && y >= 1 && y <= m); } n int occupies(int x, int y, int turn) { n   y = y + (turn - 1) * 2; n   if (valid(x, y) && a[x][y] >=  A  && a[x][y] <=  Z ) return 1; n   return 0; n } n void dfs(int curx, int cury, int turn) { n   if (cury == m) { n     f = 1; n     return; n   } n   if (vis[curx][cury][turn]) return; n   if (occupies(curx, cury, turn)) return; n   vis[curx][cury][turn] = 1; n   if (!occupies(curx, cury + 1, turn)) { n     if (valid(curx, cury + 1) && !occupies(curx, cury + 1, turn)) n       dfs(curx, cury + 1, turn + 1); n     if (valid(curx - 1, cury + 1) && !occupies(curx - 1, cury + 1, turn)) n       dfs(curx - 1, cury + 1, turn + 1); n     if (valid(curx + 1, cury + 1) && !occupies(curx + 1, cury + 1, turn)) n       dfs(curx + 1, cury + 1, turn + 1); n   } n } n int main() { n   ios::sync_with_stdio(false); n   ; n   cin.tie(0); n   ; n   int i, j, t, sx, sy, temp; n   string st; n   cin >> t; n   while (t--) { n     memset(vis, 0, sizeof vis); n     cin >> m >> temp; n     n = 3; n     for (i = 1; i <= n; i++) { n       cin >> st; n       st =     + st; n       for (j = 1; j <= m; j++) { n         a[i][j] = st[j]; n         if (a[i][j] ==  s ) { n           sx = i; n           sy = j; n         } n       } n     } n     f = 0; n     dfs(sx, sy, 1); n     if (f) n       cout <<  YES  << endl; n     else n       cout <<  NO  << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, k, a, b, q; n int Size; n int getMid(int s, int e) { return s + (e - s) / 2; } n int getSumUtil(int *st, int ss, int se, int qs, int qe, int si) { n   if (qs <= ss && qe >= se) return st[si]; n   if (se < qs || ss > qe) return 0; n   int mid = getMid(ss, se); n   return getSumUtil(st, ss, mid, qs, qe, 2 * si + 1) + n          getSumUtil(st, mid + 1, se, qs, qe, 2 * si + 2); n } n void updateValueUtilhigh_prod(int *st, int ss, int se, int i, int diff, n                               int si) { n   if (i < ss || i > se) return; n   if (ss == se) { n     st[si] = min(st[si] + diff, a); n     return; n   } n   int mid = getMid(ss, se); n   updateValueUtilhigh_prod(st, ss, mid, i, diff, 2 * si + 1); n   updateValueUtilhigh_prod(st, mid + 1, se, i, diff, 2 * si + 2); n   st[si] = st[2 * si + 1] + st[2 * si + 2]; n } n void updateValueUtillow_prod(int *st, int ss, int se, int i, int diff, int si) { n   if (i < ss || i > se) return; n   if (ss == se) { n     st[si] = min(st[si] + diff, b); n     return; n   } n   int mid = getMid(ss, se); n   updateValueUtillow_prod(st, ss, mid, i, diff, 2 * si + 1); n   updateValueUtillow_prod(st, mid + 1, se, i, diff, 2 * si + 2); n   st[si] = st[2 * si + 1] + st[2 * si + 2]; n } n void updateValue(int arr[], int *st, int n, int i, int new_val, int flag) { n   if (i < 0 || i > n - 1) { n     return; n   } n   if (flag == 1) n     updateValueUtillow_prod(st, 0, n - 1, i, new_val, 0); n   else n     updateValueUtilhigh_prod(st, 0, n - 1, i, new_val, 0); n } n int getSum(int *st, int n, int qs, int qe) { n   if (qs < 0 || qe > n - 1 || qs > qe) { n     return 0; n   } n   return getSumUtil(st, 0, n - 1, qs, qe, 0); n } n int constructSTUtil(int arr[], int ss, int se, int *st, int si) { n   if (ss == se) { n     st[si] = arr[ss]; n     return arr[ss]; n   } n   int mid = getMid(ss, se); n   st[si] = constructSTUtil(arr, ss, mid, st, si * 2 + 1) + n            constructSTUtil(arr, mid + 1, se, st, si * 2 + 2); n   return st[si]; n } n int *constructST(int arr[], int n) { n   int x = (int)(ceil(log2(n))); n   int max_size = 2 * (int)pow(2, x) - 1; n   Size = max_size; n   int *st = new int[max_size]; n   constructSTUtil(arr, 0, n - 1, st, 0); n   return st; n } n int main() { n   cin >> n >> k >> a >> b >> q; n   int arr[n]; n   memset(arr, 0, sizeof(arr)); n   int *low_prod = constructST(arr, n); n   int *high_prod = constructST(arr, n); n   for (int i = 0; i < Size; i++) { n     low_prod[i] = 0; n     high_prod[i] = 0; n   } n   while (q--) { n     int ch; n     cin >> ch; n     if (ch == 1) { n       int pos, data; n       cin >> pos >> data; n       updateValue(arr, low_prod, n, pos - 1, data, 1); n       updateValue(arr, high_prod, n, pos - 1, data, 2); n     } else { n       int pos, pos2; n       cin >> pos; n       pos--; n       if (pos == 0 || pos + k - 1 == n - 1) { n         if (pos == 0) { n           int ans = getSum(high_prod, n, k, n - 1); n           cout << ans << endl; n         } else { n           int ans = getSum(low_prod, n, 0, pos - 1); n           cout << ans << endl; n         } n       } else n         cout << getSum(low_prod, n, 0, pos - 1) + n                     getSum(high_prod, n, pos + k, n - 1) n              << endl; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n; n double p[3005][305], f[305][3005], d[305]; n inline int read() { n   int re = 0; n   char ch = getchar(); n   while (ch < 48 || ch > 57) ch = getchar(); n   while (ch > 47 && ch < 58) { n     re = (re << 3) + (re << 1) + (ch ^ 48); n     ch = getchar(); n   } n   return re; n } n inline void update(const int x) { n   static double g[3005]; n   memcpy(g, f[x], sizeof(double) * (n + 1)); n   f[x][0] = 0; n   for (int i = 1; i <= n; ++i) n     f[x][i] = f[x][i - 1] * (1 - p[i][x]) + g[i - 1] * p[i][x]; n   d[x] -= f[x][n]; n } n int main() { n   int m, t; n   double w = 0; n   n = read(); n   m = read(); n   for (int i = 1; i <= n; ++i) n     for (int j = 0; j < m; ++j) p[i][j] = (double)read() / 1e3; n   for (int i = 0; i < m; ++i) { n     f[i][0] = 1; n     for (int j = 1; j <= n; ++j) f[i][j] = f[i][j - 1] * (1 - p[j][i]); n     d[i] = 1 - f[i][n]; n   } n   for (int i = 0; i < n; ++i) { n     t = 0; n     for (int j = 1; j < m; ++j) n       if (d[t] < d[j]) t = j; n     w += d[t]; n     update(t); n   } n   printf( %.12lf , w); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 15000000, M = 5005; n int dp[N], n, k, a[M]; n set<int> s[23]; n int main() { n   scanf( %d%d , &n, &k); n   for (int i = 1; i <= n; i++) scanf( %d , a + i); n   s[0].insert(0); n   for (int i = 1; i <= k; i++) n     for (int j = 1; j <= n; j++) s[i].insert(i * a[j]); n   int q, x, ans; n   for (scanf( %d , &q); q--;) { n     scanf( %d , &x); n     ans = -1; n     for (int t = 1; t <= k && ans < 0; t++) n       for (int i = 1; i <= n && ans < 0; i++) { n         for (int j = 1; j <= t; j++) n           if (s[t - j].find(x - j * a[i]) != s[t - j].end()) { n             ans = t; n             break; n           } n       } n     printf( %d n , ans); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int a[60], i, x, n, k; n   cin >> n >> k; n   for (i = 1; i <= n; i++) cin >> a[i]; n   sort(a + 1, a + 1 + n); n   if ((n + 1 - k) >= 1) n     cout << a[n + 1 - k] <<     << a[n + 1 - k]; n   else n     cout <<  -1 ; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   float n, k, y, s, per; n   cin >> n >> k >> y; n   per = (n * y) / 100; n   s = per - (int)per; n   if (s != 0) per = (int)per + 1; n   if ((per - k) > 0) { n     cout << per - k; n   } else { n     cout <<  0 ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n long long a[200000 + 1]; n int main() { n   long long t; n   cin >> t; n   while (t--) { n     long long n, k; n     cin >> n >> k; n     for (int i = 1; i <= n; i++) scanf( %lld , &a[i]); n     sort(a + 1, a + n + 1); n     long long mpos = 0, ppos = 0; n     for (int i = 1; i <= n; i++) { n       if (a[n - i + 1] > 0) ppos = n - i + 1; n       if (a[i] < 0) mpos = i; n     } n     long long left = a[1], right = a[n]; n     if (left < 0) left *= -1; n     long long sum = 0; n     if (ppos != 0) { n       long long s = ppos - 1; n       if ((n - ppos + 1) % k != 0) { n         s += (n - ppos + 1) % k; n         sum += a[s] * 2; n       } n       for (int i = s + k; i <= n; i += k) sum += a[i] * 2; n     } n     if (mpos != 0) { n       long long s = mpos + 1; n       if ((mpos) % k != 0) { n         s -= (mpos % k); n         sum -= a[s] * 2; n       } n       for (long long i = s - k; i >= 1; i -= k) sum -= a[i] * 2; n     } n     if (ppos != 0 and mpos == 0) n       sum -= a[n]; n     else if (ppos == 0 and mpos != 0) n       sum += a[1]; n     else { n       if (left < right) n         sum -= a[n]; n       else n         sum += a[1]; n     } n     printf( %lld n , sum); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e5 + 100; n struct node { n   int x, y; n } f[2 * N]; n int n, pos, vt[N], a[N], b[N], dd[N]; n vector<pair<int, int> > vec; n bool check(int x) { n   if (x == 0) return 0; n   while (x) { n     if (x % 10 != 7 && x % 10 != 4) return 0; n     x /= 10; n   } n   return 1; n } n int cnt = 0; n int main() { n   scanf( %d , &n); n   pos = -1; n   for (int i = 1; i <= n; ++i) scanf( %d , &a[i]), vec.push_back({a[i], i}); n   bool ok = 0; n   for (int i = 1; i < n; ++i) { n     if (a[i + 1] < a[i]) { n       ok = 1; n       break; n     } n   } n   if (!ok) { n     printf( 0 ); n     return 0; n   } n   sort(vec.begin(), vec.end()); n   for (int i = 1; i <= n; ++i) { n     if (check(a[i])) pos = i; n     a[i] = b[i] = lower_bound(vec.begin(), vec.end(), pair<int, int>(a[i], i)) - n                   vec.begin(); n     vt[a[i]] = i; n   } n   if (pos == -1) { n     printf( -1 ); n     return 0; n   } n   int x; n   dd[b[pos]] = 1; n   sort(b + 1, b + n + 1); n   for (int i = 1; i <= n; ++i) { n     if (dd[b[i]]) continue; n     x = vt[b[i]]; n     if (x == i) continue; n     if (i != pos) { n       vt[a[pos]] = i; n       vt[a[i]] = pos; n       swap(a[i], a[pos]); n       f[++cnt] = {pos, i}; n       pos = i; n     } n     if (x != pos) { n       vt[a[pos]] = x; n       vt[a[x]] = pos; n       swap(a[x], a[pos]); n       f[++cnt] = {pos, x}; n       pos = x; n     } n   } n   printf( %d n , cnt); n   for (int i = 1; i <= cnt; ++i) { n     printf( %d %d n , f[i].x, f[i].y); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int A[100010], D[100010], n; n set<int> edge[100010]; n vector<int> ans; n void bfs() { n   int i, u; n   memset(A, 0, sizeof(A)); n   queue<int> que; n   ans.clear(); n   for (i = 1; i <= n; i++) { n     if (D[i] == A[i]) { n       que.push(i); n     } n   } n   set<int>::iterator it; n   while (!que.empty()) { n     u = que.front(); n     que.pop(); n     if (A[u] != D[u]) continue; n     A[u]++; n     ans.push_back(u); n     for (it = edge[u].begin(); it != edge[u].end(); it++) { n       i = *it; n       A[i]++; n       if (D[i] == A[i]) { n         que.push(i); n       } n     } n   } n   printf( %d n , ans.size()); n   for (i = 0; i + 1 < ans.size(); i++) printf( %d  , ans[i]); n   if (ans.size()) printf( %d n , ans[ans.size() - 1]); n } n int main() { n   int m, i, a, b; n   while (scanf( %d%d , &n, &m) != EOF) { n     for (i = 1; i <= n; i++) edge[i].clear(); n     for (i = 1; i <= m; i++) { n       scanf( %d%d , &a, &b); n       edge[a].insert(b); n       edge[b].insert(a); n     } n     for (i = 1; i <= n; i++) scanf( %d , &D[i]); n     bfs(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const double eps = 1e-9; n const int INF = 1e9; n using ll = long long; n using ull = unsigned long long; n using vi = vector<int>; n using vl = vector<ll>; n using vb = vector<bool>; n using vc = vector<char>; n using vii = vector<vi>; n using vll = vector<vl>; n using pii = pair<int, int>; n using pll = pair<ll, ll>; n using vpii = vector<pii>; n using vpll = vector<pll>; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   int n; n   cin >> n; n   vl v(n); n   ll sum = 0; n   for (int i = (0); i < (n); i++) { n     cin >> v[i]; n     sum += v[i]; n   } n   ll m = *max_element(v.begin(), v.end()); n   sum -= m; n   if (sum > m) n     cout <<  0 n ; n   else n     cout << m - sum + 1 << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <class A, class B> n inline bool mina(A &first, B second) { n   return (first > second) ? (first = second, 1) : 0; n } n template <class A, class B> n inline bool maxa(A &first, B second) { n   return (first < second) ? (first = second, 1) : 0; n } n int mod_count[(105)]; n void mult(long long c[(105)][(105)], long long a[(105)][(105)], n           long long b[(105)][(105)], int D, int mod) { n   for (int(i) = 0; (i) < (D); ++(i)) { n     for (int(j) = 0; (j) < (D); ++(j)) { n       c[i][j] = 0; n       for (int(k) = 0; (k) < (D); ++(k)) { n         c[i][j] += a[i][k] * b[k][j]; n         c[i][j] %= mod; n       } n     } n   } n } n long long temp[(105)][(105)]; n long long res[(105)][(105)]; n int main() { n   int N, B, K, X; n   int a; n   scanf( %d %d %d %d , &N, &B, &K, &X); n   for (int(i) = 0; (i) < (N); ++(i)) { n     scanf( %d , &a); n     mod_count[a % X]++; n   } n   long long mat[(105)][(105)]; n   memset(mat, 0, sizeof(mat)); n   for (int i = 0; i < X; i++) { n     for (int j = 0; j < X; j++) { n       for (int k = 0; k < X; k++) { n         if ((j * 10 + k) % X == i) { n           mat[i][j] += mod_count[k]; n         } n       } n     } n   } n   B--; n   memset(res, 0, sizeof(res)); n   for (int(i) = 0; (i) < (X); ++(i)) res[i][i] = 1; n   while (B) { n     if (B & 1) { n       mult(temp, res, mat, X, 1000000007); n       memcpy(res, temp, sizeof(temp)); n     } n     mult(temp, mat, mat, X, 1000000007); n     memcpy(mat, temp, sizeof(temp)); n     B >>= 1; n   } n   long long ans = 0; n   for (int(i) = 0; (i) < (X); ++(i)) { n     ans += res[K][i] * mod_count[i]; n     ans %= 1000000007; n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int N, M, P[100005], R = 1, a, b; n int findSet(int x) { n   if (P[x] == x) n     return x; n   else n     return (P[x] = findSet(P[x])); n } n void merge(int a, int b) { P[findSet(a)] = findSet(b); } n int main() { n   scanf( %d%d , &N, &M); n   for (int i = 1; i <= N; ++i) P[i] = i; n   for (int i = 0; i < M; ++i) { n     scanf( %d%d , &a, &b); n     if (findSet(a) != findSet(b)) n       merge(a, b); n     else { n       R += R; n       if (R >= 1000000009) R -= 1000000009; n     } n     if (R > 0) n       printf( %d n , R - 1); n     else n       printf( 1000000008 n ); n   } n } n


#include <bits/stdc++.h> n using namespace std; n struct Point { n   long long x, y; n }; n const int N = 1000010; n int n; n long long a[N]; n Point st[N]; n int tp; n bool cmp(long long x1, long long y1, long long x2, long long y2) { n   return x2 * y1 <= x1 * y2; n } n int main() { n   scanf( %d , &n); n   for (int i = 1; i <= n; i++) { n     scanf( %lld , &a[i]); n     a[i] += a[i - 1]; n     while (tp && !cmp(st[tp].x - st[tp - 1].x, st[tp].y - st[tp - 1].y, n                       i - st[tp].x, a[i] - st[tp].y)) n       tp--; n     st[++tp] = {i, a[i]}; n   } n   for (int i = 1; i <= tp; i++) { n     for (int j = st[i - 1].x + 1; j <= st[i].x; j++) { n       printf( %.9lf n , n              (double)(st[i].y - st[i - 1].y) / (st[i].x - st[i - 1].x)); n     } n   } n   getchar(); n   getchar(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int path[26][26]; n char s[100001], t[100001]; n void Floyd_Warshall(int n) { n   int i, j, k; n   for (k = 0; k < n; k++) { n     for (i = 0; i < n; i++) { n       for (j = 0; j < n; j++) { n         if (path[i][k] + path[k][j] < path[i][j]) { n           path[i][j] = path[i][k] + path[k][j]; n         } n       } n     } n   } n } n int main() { n   char A[10], B[10]; n   int n; n   int i, j; n   int w; n   int len; n   int ans; n   int minval; n   int minnum; n   bool flag; n   while (scanf( %s%s%d , s, t, &n) != EOF) { n     for (i = 0; i < 26; i++) { n       for (j = 0; j < 26; j++) { n         if (i != j) { n           path[i][j] = 1000000000; n         } n       } n     } n     for (i = 0; i < n; i++) { n       scanf( %s%s%d , A, B, &w); n       path[A[0] -  a ][B[0] -  a ] = min(path[A[0] -  a ][B[0] -  a ], w); n     } n     if (strlen(s) != strlen(t)) { n       printf( -1 n ); n       continue; n     } n     Floyd_Warshall(26); n     len = strlen(s); n     ans = 0; n     flag = true; n     for (i = 0; i < len; i++) { n       if (s[i] != t[i]) { n         minval = 1000000000; n         for (j = 0; j < 26; j++) { n           if (path[s[i] -  a ][j] + path[t[i] -  a ][j] < minval) { n             minval = path[s[i] -  a ][j] + path[t[i] -  a ][j]; n             minnum = j; n           } n         } n         if (minval == 1000000000) { n           flag = false; n           break; n         } n         ans += minval; n         s[i] = minnum +  a ; n       } n     } n     if (flag) { n       printf( %d n%s n , ans, s); n     } else { n       printf( -1 n ); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, a[1005][1005], q, opr, xi; n int row_sum[1005], col_sum[1005], total_row_add, total_col_add; n int main() { n   scanf( %d , &n); n   for (int i = 0; i < n; i++) n     for (int j = 0; j < n; j++) scanf( %d , &a[i][j]); n   for (int i = 0; i < n; i++) n     for (int j = 0; j < n; j++) n       if (i != j) { n         row_sum[i] = (row_sum[i] + a[i][j]) % 2; n         col_sum[j] = (col_sum[j] + a[i][j]) % 2; n       } n   int ans = 0; n   for (int i = 0; i < n; i++) n     for (int j = 0; j < n; j++) { n       if (i == j) n         ans = (ans + a[i][j] * a[i][j]) % 2; n       else n         ans = (ans + a[i][j] * a[j][i]) % 2; n     } n   scanf( %d , &q); n   while (q--) { n     scanf( %d , &opr); n     if (opr == 1) { n       scanf( %d , &xi); n       xi--; n       ans ^= 1; n       row_sum[xi] += n - 1; n       row_sum[xi] %= 2; n       ans = (ans + 2 * (col_sum[xi] + total_col_add)) % 2; n       col_sum[xi] = (col_sum[xi] + 1) % 2; n       total_col_add = (total_col_add + 1) % 2; n     } else if (opr == 2) { n       scanf( %d , &xi); n       xi--; n       ans ^= 1; n       col_sum[xi] += n - 1; n       col_sum[xi] %= 2; n       ans = (ans + 2 * (row_sum[xi] + total_row_add)) % 2; n       row_sum[xi] = (row_sum[xi] + 1) % 2; n       total_row_add = (total_row_add + 1) % 2; n     } else { n       printf( %d , ans); n     } n   } n   printf(  n ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   cin >> n; n   vector<int> a(n); n   map<int, vector<int>> indices; n   for (int i = 0; i < n; i++) cin >> a[i], indices[a[i]].push_back(i); n   map<int, int> dp; n   int mxn = 0, ans = -1; n   for (int i = n - 1; i >= 0; i--) { n     dp[a[i]] = 1 + dp[a[i] + 1]; n     if (dp[a[i]] > mxn) mxn = dp[a[i]], ans = a[i]; n   } n   cout << mxn <<   n ; n   int prevI = -1, currA = ans; n   while (mxn) { n     int currI = n         lower_bound(indices[currA].begin(), indices[currA].end(), prevI) - n         indices[currA].begin(); n     cout << indices[currA][currI] + 1 <<    ; n     prevI = indices[currA][currI], currA++, mxn--; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 610; n int n; n int anc[MAXN], depth[MAXN]; n bool mark[MAXN]; n vector<int> adj[MAXN]; n vector<int> partitions[2]; n map<vector<int>, int> ans; n queue<int> q; n int qtdEdges(vector<int> a) { n   if (a.empty()) return 0; n   sort(a.begin(), a.end()); n   if (ans[a] == 0) { n     cout <<  ?   << (int)a.size() <<    ; n     for (int i = 0; i < (int)a.size(); i++) cout << a[i] <<    ; n     cout << endl; n     cin >> ans[a]; n     ans[a]++; n   } n   return ans[a] - 1; n } n int qtdCrossEdges(vector<int> A, vector<int> B) { n   vector<int> merge; n   for (int i = 0; i < (int)A.size(); i++) merge.push_back(A[i]); n   for (int i = 0; i < (int)B.size(); i++) merge.push_back(B[i]); n   return qtdEdges(merge) - qtdEdges(A) - qtdEdges(B); n } n void add(int node, int p) { n   q.push(node); n   mark[node] = true; n   anc[node] = p; n   depth[node] = depth[p] + 1; n   partitions[depth[node] % 2].push_back(node); n } n void findEdges(int root, vector<int> nodes, int qtd) { n   if (qtd == 0) return; n   if (nodes.empty()) return; n   if ((int)nodes.size() == 1) { n     add(nodes.front(), root); n     return; n   } n   int sz = (int)nodes.size(); n   int m = sz / 2; n   vector<int> leftNodes, rightNodes; n   for (int i = 0; i < m; i++) leftNodes.push_back(nodes[i]); n   for (int i = m; i < (int)nodes.size(); i++) rightNodes.push_back(nodes[i]); n   findEdges(root, leftNodes, qtdCrossEdges(vector<int>(1, root), leftNodes)); n   findEdges(root, rightNodes, qtdCrossEdges(vector<int>(1, root), rightNodes)); n } n int findEndpoint(vector<int> L, vector<int> R) { n   if ((int)L.size() == 1) return L.front(); n   int sz = (int)L.size(); n   int m = sz / 2; n   vector<int> leftNodes, rightNodes; n   for (int i = 0; i < m; i++) leftNodes.push_back(L[i]); n   for (int i = m; i < (int)L.size(); i++) rightNodes.push_back(L[i]); n   if (qtdCrossEdges(leftNodes, R) != 0) return findEndpoint(leftNodes, R); n   return findEndpoint(rightNodes, R); n } n void findBadEdge(vector<int> nodes, int& A, int& B) { n   if (nodes.empty()) return; n   int sz = (int)nodes.size(); n   int m = sz / 2; n   vector<int> leftNodes, rightNodes; n   for (int i = 0; i < m; i++) leftNodes.push_back(nodes[i]); n   for (int i = m; i < (int)nodes.size(); i++) rightNodes.push_back(nodes[i]); n   if (qtdEdges(leftNodes) != 0) { n     findBadEdge(leftNodes, A, B); n     return; n   } n   if (qtdEdges(rightNodes) != 0) { n     findBadEdge(rightNodes, A, B); n     return; n   } n   A = findEndpoint(leftNodes, rightNodes); n   B = findEndpoint(rightNodes, vector<int>(1, A)); n } n int main() { n   cin >> n; n   for (int i = 1; i <= n; i++) { n     vector<int> aux(1, i); n     ans[aux] = 1; n   } n   add(1, 0); n   while (!q.empty()) { n     vector<int> remain; n     for (int i = 1; i <= n; i++) n       if (!mark[i]) remain.push_back(i); n     int cur = q.front(); n     q.pop(); n     findEdges(cur, remain, qtdCrossEdges(vector<int>(1, cur), remain)); n   } n   int bad = -1; n   for (int i = 0; i < 2; i++) n     if (qtdEdges(partitions[i]) != 0) bad = i; n   if (bad == -1) { n     cout <<  Y   << (int)partitions[0].size() << endl; n     for (int i = 0; i < (int)partitions[0].size(); i++) n       cout << partitions[0][i] <<    ; n     cout << endl; n     return 0; n   } n   int A, B; n   findBadEdge(partitions[bad], A, B); n   if (depth[A] > depth[B]) swap(A, B); n   vector<int> pathA, pathB; n   while (depth[A] != depth[B]) pathB.push_back(B), B = anc[B]; n   while (A != B) { n     pathA.push_back(A); n     A = anc[A]; n     pathB.push_back(B); n     B = anc[B]; n   } n   pathA.push_back(A); n   cout <<  N   << (int)pathA.size() + (int)pathB.size() << endl; n   for (int i = 0; i < (int)pathA.size(); i++) cout << pathA[i] <<    ; n   for (int i = (int)pathB.size() - 1; i >= 0; i--) cout << pathB[i] <<    ; n   cout << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int gcd(int a, int b) { n   if (b == 0) n     return a; n   else n     return gcd(b, a % b); n } n int main() { n   int a, b, n, krok = 0; n   cin >> a >> b >> n; n   bool podm = false; n   int g; n   while (n > 0) { n     if (krok % 2 == 0) { n       g = gcd(a, n); n       n -= g; n       podm = true; n     } else { n       g = gcd(b, n); n       n -= g; n       podm = false; n     } n     krok++; n   } n   if (podm) n     cout <<  0  << endl; n   else n     cout <<  1  << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int cnt = 0; n   int a; n   string s; n   cin >> s; n   for (int i = 0; i < s.length(); i++) { n     if (s[i] ==  4  || s[i] ==  7 ) { n       cnt++; n     } n   } n   if (cnt == 4 || cnt == 7) { n     cout <<  YES ; n   } else { n     cout <<  NO ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int minFact[1000000 + 5]; n vector<int> prime; n int rec(int N, int K) { n   int ans = 0; n   if (N / K <= 10000) { n     ans = 0; n     for (int i = 1; i <= N / K; ++i) { n       ans += (minFact[i] >= K); n     } n   } else { n     ans = N / K; n     for (int i = 0; i < prime.size() and prime[i] < K; ++i) { n       ans -= rec(N / K, prime[i]); n     } n   } n   return ans; n } n int main() { n   minFact[1] = 2 * 1000000007; n   for (int i = 2; i <= 100000; ++i) { n     if (minFact[i] == 0) { n       prime.push_back(i); n       for (int j = i; j <= 100000; j += i) { n         if (minFact[j] == 0) minFact[j] = i; n       } n     } n   } n   int L, R, K; n   cin >> L >> R >> K; n   for (int i = 2; i * i <= K; ++i) { n     if (K % i == 0) { n       cout << 0; n       return 0; n     } n   } n   cout << rec(R, K) - rec(L - 1, K); n } n


#include <bits/stdc++.h> n using namespace std; n int n, m; n char grid[55][55]; n char vis[55][55]; n int di[] = {0, 1, -1, 0}; n int dj[] = {1, 0, 0, -1}; n bool dfs(int pi, int pj, char c, int li = -1, int lj = -1) { n   if (vis[pi][pj]) return true; n   vis[pi][pj] = true; n   for (int d = 0; d < 4; d++) { n     int ni = pi + di[d]; n     int nj = pj + dj[d]; n     if (ni == li && nj == lj) continue; n     if (ni < 0 || ni >= n || nj < 0 || nj >= m) continue; n     if (grid[ni][nj] != c) continue; n     if (dfs(ni, nj, c, pi, pj)) return true; n   } n   return false; n } n int main() { n   cin >> n >> m; n   for (int i = 0; i < n; i++) { n     cin >> grid[i]; n   } n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < m; j++) { n       if (vis[i][j] == false) { n         if (dfs(i, j, grid[i][j])) { n           cout <<  Yes  << endl; n           return 0; n         } n       } n     } n   } n   cout <<  No  << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n bool gt(long double a, long double b) { return a > b + 1e-12; } n long long a, b, x; n int main() { n   int n; n   cin >> n; n   while (n--) { n     string s; n     cin >> s; n     if (s ==  UR  || s ==  DL ) n       a++; n     else if (s ==  UL  || s ==  DR ) n       b++; n     else n       x++; n   } n   cout << (x + 1 + a) * (x + 1 + b) << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long a, b; n   cin >> a >> b; n   long i = 1; n   while (a >= 0 && b >= 0) { n     a -= i; n     if (a < 0) { n       cout <<  Vladik n ; n       return 0; n     } n     b -= i + 1; n     if (b < 0) { n       cout <<  Valera n ; n       return 0; n     } n     i += 2; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, a, b, c; n char str[2000000 + 5]; n vector<int> G[10000 + 5]; n vector<int> per[10000 + 5]; n int siz[10000 + 5], dfn[10000 + 5], low[10000 + 5], dcnt; n int blo[10000 + 5], bgcd[10000 + 5], blocnt; n int stk[10000 + 5], t; n int num[10000 + 5], mx[10000 + 5], mn[10000 + 5]; n bool instk[10000 + 5]; n int fact[10000 + 5], inv[10000 + 5]; n int PowMod(int x, int y) { n   int ret = 1; n   while (y) { n     if (y & 1) ret = 1LL * ret * x % 1000000007; n     x = 1LL * x * x % 1000000007; n     y >>= 1; n   } n   return ret; n } n int gcd(int x, int y) { n   if (y == 0) return x; n   return gcd(y, x % y); n } n void InPut() { n   scanf( %d %d %d , &n, &a, &b); n   for (int i = 1; i <= n; i++) { n     scanf( %s , str + 1); n     for (int j = 1; j <= n; j++) n       if (str[j] ==  1 ) G[i].push_back(j); n   } n   for (int i = 1; i <= n; i++) { n     scanf( %d , &siz[i]); n     per[i].resize(siz[i] + 1); n     scanf( %s , str); n     for (int j = 0; j < siz[i]; j++) n       if (str[j] ==  1 ) n         per[i][j] = 1; n       else if (str[j] ==  0 ) n         per[i][j] = 0; n   } n } n void Tarjan(int u, int fa) { n   dfn[u] = low[u] = ++dcnt; n   instk[u] = true; n   stk[t++] = u; n   for (int i = 0; i < (int)G[u].size(); i++) { n     int v = G[u][i]; n     if (dfn[v] == 0) { n       Tarjan(v, u); n       low[u] = min(low[u], low[v]); n     } else if (instk[v] == true) n       low[u] = min(low[u], dfn[v]); n   } n   if (low[u] == dfn[u]) { n     int fro = 0; n     blocnt++; n     do { n       fro = stk[--t]; n       instk[fro] = false; n       blo[fro] = blocnt; n       bgcd[blocnt] = gcd(bgcd[blocnt], siz[fro]); n     } while (fro != u); n   } n } n void Part1() { n   for (int i = 1; i <= n; i++) n     if (dfn[i] == 0) Tarjan(i, -1); n   for (int i = 1; i <= blocnt; i++) per[i + n].resize(bgcd[i] + 1); n   for (int i = 1; i <= n; i++) n     for (int j = 0; j < siz[i]; j++) n       per[blo[i] + n][j % bgcd[blo[i]]] |= per[i][j]; n   int nw; n   for (int i = blocnt; i > 1; i--) { n     nw = gcd(bgcd[i], bgcd[i - 1]); n     for (int j = 0; j < bgcd[i]; j++) n       if (per[i + n][j]) { n         per[i - 1 + n][j % nw] |= 1; n         num[i]++; n       } n   } n   for (int j = 0; j < bgcd[1]; j++) num[1] += per[1 + n][j]; n   for (int i = 1; i <= n; i++) { n     mx[i] = mn[i] = 0; n     mx[i] = 1LL * num[blo[i]] * siz[i] / bgcd[blo[i]]; n     for (int j = 0; j < siz[i]; j++) mn[i] += per[i][j]; n   } n } n void Prepare() { n   fact[0] = 1; n   for (int i = 1; i <= 10000; i++) fact[i] = 1LL * fact[i - 1] * i % 1000000007; n   inv[10000] = PowMod(fact[10000], 1000000007 - 2); n   for (int i = 10000 - 1; i >= 0; i--) n     inv[i] = 1LL * inv[i + 1] * (1LL * i + 1LL) % 1000000007; n } n inline int C(int x, int y) { n   return 1LL * fact[x] * inv[y] % 1000000007 * inv[x - y] % 1000000007; n } n int Part2() { n   Prepare(); n   int cnt1 = 0, cnt2 = 0, ret = 0; n   for (int i = 1; i <= n; i++) { n     cnt1 = cnt2 = 0; n     for (int j = 1; j <= n; j++) { n       if (i != j && mn[j] > mx[i]) cnt1++; n       if (i != j && mn[j] <= mx[i] && n           (mx[j] > mx[i] || (mx[j] == mx[i] && j < i))) n         cnt2++; n     } n     if (cnt1 > a - 1) continue; n     for (int j = min(b, min(a - 1 - cnt1, cnt2)); j >= 0 && j + 1 + cnt1 >= b; n          j--) n       ret = (1LL * ret + 1LL * C(cnt2, j) * C(cnt1, b - j - 1) % 1000000007) % n             1000000007; n   } n   return ret; n } n int main() { n   InPut(); n   Part1(); n   int ans = Part2(); n   printf( %d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n; n int s[100005], num[100005]; n bool cmp(int a, int b) { return a < b; } n int main() { n   while (scanf( %d , &n) != EOF) { n     int i; n     for (i = 0; i < n; i++) scanf( %d , &s[i]); n     sort(s, s + n, cmp); n     bool flag = true; n     for (i = 1; i < n - 1; i++) { n       if (s[i] == s[i - 1]) continue; n       if (s[i] != s[i - 1] + 1) flag = false; n       if (s[i] == s[i - 1] + 1 && s[i] == s[i + 1] - 1) flag = false; n     } n     if (s[n - 1] != s[n - 2] && s[n - 1] != s[n - 2] + 1) flag = false; n     if (!flag) { n       printf( NO n ); n       continue; n     } n     int ma = 0; n     memset(num, 0, sizeof(num)); n     for (i = 0; i < n; i++) { n       num[s[i] - s[0] + 1]++; n       ma = max(s[i] - s[0] + 1, ma); n     } n     num[1]--; n     num[ma]--; n     for (i = 2; i < ma; i++) num[i] -= 2; n     for (i = 1; flag && i < ma; i++) { n       if (num[i] > 0 && num[i + 1] < num[i]) flag = false; n       num[i + 1] -= num[i]; n       num[i] = 0; n     } n     if (num[ma] > 0) flag = false; n     if (!flag) n       printf( NO n ); n     else n       printf( YES n ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 5010; n const int INF = 1e5; n static int N, val[MAXN], dp[MAXN][MAXN][2]; n int main() { n   scanf( %d , &N); n   for (int i = 1; i <= N; i++) scanf( %d , &val[i]); n   val[0] = -INF; n   memset(dp, 0x3f, sizeof dp); n   dp[0][0][0] = dp[1][0][0] = dp[1][1][1] = 0; n   for (int i = 2; i <= N; i++) { n     dp[i][0][0] = 0; n     for (int j = 1; j <= i; j++) { n       int sub0 = max(0, val[i] - val[i - 1] + 1); n       int sub1 = max(0, val[i - 1] - val[i] + 1); n       int sub2 = max(0, max(val[i - 1] - val[i], val[i - 1] - val[i - 2]) + 1); n       dp[i][j][0] = min(dp[i - 1][j][0], dp[i - 1][j][1] + sub0); n       dp[i][j][1] = min(dp[i - 2][j - 1][0] + sub1, dp[i - 2][j - 1][1] + sub2); n     } n   } n   for (int i = 1; i <= (N + 1) / 2; i++) n     printf( %d  , min(dp[N][i][0], dp[N][i][1])); n   puts(  ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long x, sum = 0; n   string s; n   cin >> x; n   while (x--) { n     cin >> s; n     if (s[0] ==  T ) sum += 4; n     if (s[0] ==  C ) sum += 6; n     if (s[0] ==  O ) sum += 8; n     if (s[0] ==  D ) sum += 12; n     if (s[0] ==  I ) sum += 20; n   } n   cout << sum; n } n


#include <bits/stdc++.h> n using namespace std; n string Levelcheck(vector<vector<int> > vec) { n   int n; n   n = vec.size(); n   if (vec[0][0] < vec[0][1]) { n     return  NO ; n   } n   for (int i = 1; i < n; i++) { n     if (vec[i][0] < vec[i][1]) { n       return  NO ; n     } n     if (vec[i][0] < vec[i - 1][0]) { n       return  NO ; n     } n     if (vec[i][1] < vec[i - 1][1]) { n       return  NO ; n     } n     if ((vec[i][0] - vec[i - 1][0]) < (vec[i][1] - vec[i - 1][1])) { n       return  NO ; n     } n   } n   return  YES ; n } n int main() { n   int t; n   cin >> t; n   vector<vector<int> > vec; n   vector<int> vec1; n   vector<string> vec2; n   for (int i = 0; i < t; i++) { n     int n; n     cin >> n; n     for (int j = 0; j < n; j++) { n       int a; n       int b; n       cin >> a >> b; n       vec1.push_back(a); n       vec1.push_back(b); n       vec.push_back(vec1); n       vec1.clear(); n     } n     string result = Levelcheck(vec); n     vec2.push_back(result); n     vec.clear(); n   } n   for (int i = 0; i < t; i++) { n     cout << vec2[i] << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long int n{0}; n   cin >> n; n   bool flag = false; n   bool flag1 = false; n   long long int i{0}, x{0}, rem{0}; n   for (i = 1; i <= 1000000; i++) { n     if (n >= 4 * i && n <= 7 * i) { n       x = 4 * i; n       rem = n - x; n       if ((rem % 3) != 0) { n         x = 0; n         rem = 0; n       } else if (rem % 3 == 0) { n         break; n       } n     } n   } n   if (x == 0 && rem == 0) i = 0; n   if (x == 0) { n     cout <<  -1  << endl; n   } else { n     long long int div = rem / 3; n     for (long long int j = 1; j <= i - div; j++) { n       cout <<  4 ; n     } n     for (long long int i = 1; i <= div; i++) { n       cout <<  7 ; n     } n     cout << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long int r, c, ans = 0; n   cin >> r >> c; n   vector<vector<long long int> > A(r + 2, vector<long long int>(c + 2)); n   for (long long int i = 1; i <= r; i++) n     for (long long int j = 1; j <= c; j++) cin >> A[i][j]; n   vector<vector<long long int> > left(A), right(A), up(A), down(A); n   for (long long int i = 1; i <= r; i++) n     for (long long int j = 1; j <= c; j++) left[i][j] += left[i][j - 1]; n   for (long long int i = 1; i <= r; i++) n     for (long long int j = c; j >= 1; j--) right[i][j] += right[i][j + 1]; n   for (long long int j = 1; j <= c; j++) n     for (long long int i = 1; i <= r; i++) up[i][j] += up[i - 1][j]; n   for (long long int j = 1; j <= c; j++) n     for (long long int i = r; i >= 1; i--) down[i][j] += down[i + 1][j]; n   for (long long int i = 1; i <= r; i++) n     for (long long int j = 1; j <= c; j++) n       if (A[i][j] == 0) n         ans += (left[i][j - 1] != 0) + (right[i][j + 1] != 0) + n                (up[i - 1][j] != 0) + (down[i + 1][j] != 0); n   cout << ans; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   string s; n   cin >> s; n   int k = s.size(); n   for (string::iterator i = s.begin() + 1; i != s.end(); i++) n     if (*i ==  1 ) { n       k++; n       break; n     } n   printf( %d n , k / 2); n   return 0; n } n


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


#include <bits/stdc++.h> n using namespace std; n const long long N = 1e3 + 100, inf = 1e15; n long long dis[N][N][6], clr[6][6], ans = inf; n queue<pair<int, int> > q[3]; n char a[N][N]; n bool mark[N][N][6]; n pair<int, int> k; n int ki, kj, n, m, x, y; n int xx[] = {0, 1, 0, -1}, yy[] = {1, 0, -1, 0}; n bool is_vallid(int x, int y) { n   if (x >= 0 && x < n && y >= 0 && y < m) return 1; n   return 0; n } n void init() { n   for (int i = 0; i < 3; i++) n     for (int j = 0; j < 3; j++) clr[i][j] = inf; n   for (int i = 0; i < N; i++) n     for (int j = 0; j < N; j++) n       for (int c = 0; c < 4; c++) dis[i][j][c] = inf; n } n void bfs(int b) { n   while (!q[b].empty()) { n     k = q[b].front(); n     ki = k.first, kj = k.second; n     if (a[ki][kj] -  1  != b && a[ki][kj] !=  #  && a[ki][kj] !=  . ) n       clr[b][a[ki][kj] -  1 ] = min(dis[ki][kj][b], clr[b][a[ki][kj] -  1 ]); n     for (int i = 0; i < 4; i++) { n       x = ki + xx[i], y = kj + yy[i]; n       if (is_vallid(x, y) && a[x][y] !=  #  && !mark[x][y][b]) { n         q[b].push({x, y}); n         dis[x][y][b] = dis[ki][kj][b] + 1; n         mark[x][y][b] = 1; n       } n     } n     q[b].pop(); n   } n } n int main() { n   init(); n   cin >> n >> m; n   for (int i = 0; i < n; i++) n     for (int j = 0; j < m; j++) { n       cin >> a[i][j]; n       if (a[i][j] !=  .  && a[i][j] !=  # ) { n         q[a[i][j] -  1 ].push({i, j}); n         mark[i][j][a[i][j] -  1 ] = 1; n         dis[i][j][a[i][j] -  1 ] = 0; n       } n     } n   for (int i = 0; i < 3; i++) bfs(i); n   ans = min(min(clr[0][1] + clr[0][2], clr[1][0] + clr[1][2]), n             clr[2][0] + clr[2][1]); n   for (int i = 0; i < n; i++) n     for (int j = 0; j < m; j++) n       ans = min(ans, dis[i][j][0] + dis[i][j][1] + dis[i][j][2]); n   if (ans >= inf) { n     cout << -1; n     return 0; n   } n   cout << ans - 2; n } n


#include <bits/stdc++.h> n #pragma GCC optimize(3) n using namespace std; n const long long llINF = 9223372036854775807; n const int INF = 2147483647; n const int maxn = 1e3 + 7; n const int maxm = 1e5 + 7; n const int mod = 1e9 + 7; n int n, m; n int c[maxn], a[maxn]; n int main(int argc, char const *argv[]) { n   scanf( %d%d , &n, &m); n   for (int i = 1; i <= n; i++) scanf( %d , &c[i]); n   for (int i = 1; i <= m; i++) scanf( %d , &a[i]); n   int p = 1, cnt = 0; n   for (int i = 1; i <= n; i++) { n     if (a[p] >= c[i]) { n       cnt++; n       p++; n     } n   } n   printf( %d n , cnt); n   return 0; n } n


#include <bits/stdc++.h> n const int N = 100000 + 10; n long long A[N], B[N], L[N]; n int n, m, a, b; n std::set<std::pair<long long, int> > dic; n double ans = 1e100; n int ansx, ansy; n double sqr(long long x) { return (double)x * x; } n void Cal(int idx) { n   double dis; n   long long x = B[idx] * a / b; n   std::set<std::pair<long long, int> >::iterator u; n   u = dic.lower_bound(std::pair<long long, int>(x, 0)); n   dis = L[idx]; n   dis += sqrt(sqr(u->first) + sqr(a)); n   dis += sqrt(sqr(u->first - B[idx]) + sqr(b - a)); n   if (dis < ans) { n     ans = dis; n     ansx = u->second; n     ansy = idx; n   } n   if (u != dic.begin()) { n     --u; n     dis = L[idx]; n     dis += sqrt(sqr(u->first) + sqr(a)); n     dis += sqrt(sqr(u->first - B[idx]) + sqr(b - a)); n     if (dis < ans) { n       ans = dis; n       ansx = u->second; n       ansy = idx; n     } n   } n   u = dic.upper_bound(std::pair<long long, int>(x, 0)); n   if (u == dic.end()) return; n   dis = L[idx]; n   dis += sqrt(sqr(u->first) + sqr(a)); n   dis += sqrt(sqr(u->first - B[idx]) + sqr(b - a)); n   if (dis < ans) { n     ans = dis; n     ansx = u->second; n     ansy = idx; n   } n   if (++u != dic.end()) { n     dis = L[idx]; n     dis += sqrt(sqr(u->first) + sqr(a)); n     dis += sqrt(sqr(u->first - B[idx]) + sqr(b - a)); n     if (dis < ans) { n       ans = dis; n       ansx = u->second; n       ansy = idx; n     } n   } n } n int main() { n   scanf( %d%d%d%d , &n, &m, &a, &b); n   for (int i = 0; i < n; ++i) { n     scanf( %I64d , &A[i]); n     dic.insert(std::pair<long long, int>(A[i], i + 1)); n   } n   for (int i = 0; i < m; ++i) scanf( %I64d , &B[i]); n   for (int i = 0; i < m; ++i) scanf( %I64d , &L[i]); n   for (int i = 0; i < m; ++i) Cal(i); n   printf( %d %d n , ansx, ansy + 1); n   return 0; n } n


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


#include <bits/stdc++.h> n using namespace std; n const int NMAX = 1000001; n bool vis[NMAX]; n int p[NMAX]; n int go(int u) { n   vis[u] = true; n   if (vis[p[u]]) return 1; n   return 1 + go(p[u]); n } n int sizes[NMAX]; n int cap[NMAX]; n bitset<NMAX> knp; n int main() { n   vector<int> v; n   int n, k; n   scanf( %d%d , &n, &k); n   for (int i = 1; i <= n; i++) scanf( %d , p + i); n   for (int i = 1; i <= n; i++) n     if (!vis[i]) v.push_back(go(i)); n   int one = 0, two = 0; n   for (int c : v) { n     if (c % 2 == 0) { n       two += c / 2; n     } else { n       one++; n       two += c / 2; n     } n   } n   int s = 0; n   if (one + two <= k) n     s = n; n   else { n     if (k <= two) n       s = 2 * k; n     else n       s = 2 * two + (k - two); n   } n   for (int c : v) sizes[c]++; n   const int T = 100; n   knp[0] = true; n   for (int i = 1; i <= T; i++) n     if (sizes[i]) { n       memset(cap, -1, sizeof(cap)); n       for (int j = 0; j <= k; j++) n         if (knp[j]) cap[j] = 0; n       for (int j = i; j <= k; j++) n         if (cap[j] < 0 && cap[j - i] >= 0) cap[j] = cap[j - i] + 1; n       for (int j = i; j <= k; j++) n         if (cap[j] > 0 && cap[j] <= sizes[i]) knp[j] = true; n     } n   for (int i = T + 1; i <= k; i++) n     for (int j = 0; j < sizes[i]; j++) { n       knp |= (knp << i); n     } n   printf( %d %d , knp[k] ? k : k + 1, s); n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 205; n const int MAXM = 55; n struct Point { n   int x, y; n   double k; n   Point(int _x = 0, int _y = 0) { x = _x, y = _y; } n   Point operator-(const Point &b) const { return Point(x - b.x, y - b.y); } n   bool operator<(const Point &b) const { return k < b.k; } n   long long operator*(const Point &b) const { n     return 1LL * x * b.y - 1LL * y * b.x; n   } n } a[MAXN], p[MAXN]; n int n, m; n long long ans, mx[MAXM], f[MAXN][MAXN][MAXM]; n vector<int> adj[MAXN], rev[MAXN]; n queue<int> q[MAXN]; n bool Onleft(Point a, Point b, Point c) { return (b - a) * (c - b) > 0; } n void Add(int x, int y) { n   while (!q[x].empty() && Onleft(p[q[x].front()], p[x], p[y])) n     Add(q[x].front(), y), q[x].pop(); n   rev[x].push_back(y); n   adj[y].push_back(x); n   q[y].push(x); n } n void Init(int n) { n   for (int i = 1; i <= n; i++) { n     adj[i].clear(), rev[i].clear(); n     while (!q[i].empty()) q[i].pop(); n   } n   for (int i = 1; i < n; i++) Add(i, i + 1); n } n void Dp(int n) { n   memset(f, 0, sizeof(f)); n   for (int i = n; i; i--) { n     for (int j = 0; j < m; j++) mx[j] = 0; n     reverse(adj[i].begin(), adj[i].end()); n     int cur = rev[i].size() - 1; n     for (auto j : adj[i]) { n       f[j][i][1] = p[j] * p[i]; n       for (int k = 2; k <= m; k++) n         if (mx[k - 1]) f[j][i][k] = mx[k - 1] + p[j] * p[i]; n       for (; ~cur && Onleft(p[j], p[i], p[rev[i][cur]]); cur--) n         for (int k = 1; k < m; k++) n           if (mx[k] < f[i][rev[i][cur]][k]) n             mx[k] = f[i][rev[i][cur]][k], f[j][i][k + 1] = mx[k] + p[j] * p[i]; n     } n   } n   for (int i = 1; i <= n; i++) n     for (auto j : rev[i]) ans = max(ans, f[i][j][m - 2]); n } n void Solve(int idx) { n   int cnt = 0; n   for (int i = 1; i <= n; i++) n     if (i ^ idx) n       if (a[i].x > a[idx].x || (a[i].x == a[idx].x && a[i].y > a[idx].y)) n         p[++cnt] = a[i] - a[idx], p[cnt].k = atan2(p[cnt].y, p[cnt].x); n   sort(p + 1, p + cnt + 1); n   Init(cnt); n   Dp(cnt); n } n int main() { n   scanf( %d%d , &n, &m); n   for (int i = 1; i <= n; i++) scanf( %d%d , &a[i].x, &a[i].y); n   for (int i = 1; i <= n; i++) Solve(i); n   cout << ans / 2 <<  .  << (ans & 1 ?  50  :  00 ) << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int INF = 1000 * 1000 * 1000; n vector<vector<pair<int, int>>> adj; n int mem[80][80][80][80]; n int dp(int cur, int closestLeft, int closestRight, int remainingMoves) { n   if (remainingMoves == 0) return 0; n   if (mem[cur][closestLeft][closestRight][remainingMoves] != -1) { n     return mem[cur][closestLeft][closestRight][remainingMoves]; n   } n   int res = INF; n   for (auto& nxt : adj[cur]) n     if (nxt.first != cur) { n       if (nxt.first >= closestLeft && nxt.first <= closestRight) { n         int newClosestLeft = closestLeft; n         if (nxt.first > cur) newClosestLeft = cur + 1; n         int newClosestRight = closestRight; n         if (nxt.first < cur) newClosestRight = cur - 1; n         res = min(res, nxt.second + dp(nxt.first, newClosestLeft, n                                        newClosestRight, remainingMoves - 1)); n       } n     } n   return mem[cur][closestLeft][closestRight][remainingMoves] = res; n } n int main() { n   int n, k, m; n   cin >> n >> k >> m; n   memset(mem, -1, sizeof(mem)); n   adj.resize(n); n   for (int i = 0; i < m; i++) { n     int u; n     cin >> u; n     u--; n     int v; n     cin >> v; n     v--; n     int c; n     cin >> c; n     adj[u].push_back(make_pair(v, c)); n   } n   int res = INF; n   for (int start = 0; start < n; start++) { n     int temp = dp(start, 0, n - 1, k - 1); n     res = min(res, temp); n   } n   if (res >= INF) n     cout << -1 << endl; n   else n     cout << res << endl; n } n


#include <bits/stdc++.h> n using namespace std; n void DFS(long long n, long long p, long long d, n          vector<pair<long long, long long>> &D, vector<long long> &Pa, n          vector<vector<long long>> &G) { n   for (long long nn : G[n]) { n     if (nn == p) continue; n     D.emplace_back(d + 1, nn); n     Pa[nn] = n; n     DFS(nn, n, d + 1, D, Pa, G); n   } n } n void solve() { n   long long n, m; n   cin >> n >> m; n   vector<long long> P(n), H(n); n   for (int i = 0; i < n; i++) cin >> P[i]; n   for (int i = 0; i < n; i++) cin >> H[i]; n   vector<vector<long long>> G(n, vector<long long>()); n   vector<long long> Eg(n, 0), Eb(n, 0); n   vector<long long> Pa(n, 0); n   vector<pair<long long, long long>> D; n   for (int i = 0; i < n - 1; i++) { n     long long u, v; n     cin >> u >> v; n     u--; n     v--; n     G[u].push_back(v); n     G[v].push_back(u); n   } n   DFS(0, -1, 0, D, Pa, G); n   sort(D.begin(), D.end()); n   reverse(D.begin(), D.end()); n   D.emplace_back(0, 0); n   for (const auto &p : D) { n     long long node = p.second; n     Eb[node] += P[node]; n     long long h = H[node]; n     if ((Eg[node] - Eb[node] > h) || n         (abs(Eg[node] - Eb[node]) % 2 != abs(h) % 2) || n         (Eg[node] + Eb[node] < abs(h))) { n       cout <<  NO n ; n       return; n     } n     long long x = h - (Eg[node] - Eb[node]); n     long long Ag = Eg[node] + x / 2; n     long long Ab = Eb[node] - x / 2; n     if (Ab < 0) { n       cout <<  NO n ; n       return; n     } n     if (node == 0 && Ag + Ab != m) { n       cout <<  NO n ; n       return; n     } n     Eg[Pa[node]] += Ag; n     Eb[Pa[node]] += Ab; n   } n   cout <<  YES n ; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   int t; n   cin >> t; n   for (int i = 0; i < t; i++) solve(); n } n


#include <bits/stdc++.h> n using namespace std; n int m1[200010] = {0}, m2[200010] = {0}; n map<int, int> pp; n bool d[200010]; n int e[100000]; n int main() { n   int n; n   cin >> n; n   long long x; n   int p = 2; n   int tt = 0; n   for (int i = 2; i <= 200005; i++) { n     m1[i] = m2[i] = 1000000000; n   } n   for (int i = 1; i <= n; i++) { n     cin >> x; n     for (int uu = 2; uu * uu <= x; uu++) n       if (x % uu == 0) { n         int k = 0; n         while (x % uu == 0) { n           x = x / uu; n           k++; n         } n         if (k != 0) { n           if (k < m1[uu]) { n             m2[uu] = m1[uu]; n             m1[uu] = k; n           } else if (k < m2[uu]) { n             m2[uu] = k; n           } n           pp[uu]++; n         } n       } n     if (x > 1) { n       pp[x]++; n       if (1 < m1[x]) { n         m2[x] = m1[x]; n         m1[x] = 1; n       } else if (1 < m2[x]) { n         m2[x] = 1; n       } n     } n   } n   long long sum = 1; n   int uu = 1; n   while (uu < 200000) { n     uu++; n     if (pp[uu] < n - 1) continue; n     if (pp[uu] == n - 1) { n       sum = sum * pow(uu, m1[uu]); n     } else n       sum = sum * pow(uu, m2[uu]); n   } n   cout << sum << endl; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 2410; n int n, l, r, a[N]; n vector<int> p; n tuple<int, int, int> tmp[2]; n vector<tuple<int, int, int>> ans; n int main() { n   ios::sync_with_stdio(0), cin.tie(0), cout.tie(0); n   cin >> n; n   for (int i = 1, x; i <= 3 * n; i++) cin >> x, a[x] = 1; n   for (int i = 1; i <= 6 * n; i++) p.push_back(i); n   for (int i = 1; i <= 2 * n; i++) { n     l = 0, r = p.size() - 1; n     while (l < r && a[p[l]] == a[p[l + 1]] && a[p[l]] == a[p[l + 2]]) n       tmp[a[p[l]]] = {p[l], p[l + 1], p[l + 2]}, l += 3; n     while (l < r && a[p[r]] == a[p[r - 1]] && a[p[r]] == a[p[r - 2]]) n       tmp[a[p[r]]] = {p[r - 2], p[r - 1], p[r]}, r -= 3; n     if (l < r) { n       for (int j = l; j + 2 <= r; j++) n         if ((i & 1) == a[p[j]] && (i & 1) == a[p[j + 1]] && n             (i & 1) == a[p[j + 2]]) { n           ans.push_back({p[j], p[j + 1], p[j + 2]}); n           goto next; n         } n     } n     ans.push_back(tmp[i & 1]); n   next: n     p.erase(find(p.begin(), p.end(), get<0>(ans.back()))); n     p.erase(find(p.begin(), p.end(), get<1>(ans.back()))); n     p.erase(find(p.begin(), p.end(), get<2>(ans.back()))); n   } n   for (const auto &it : ans) n     cout << get<0>(it) <<     << get<1>(it) <<     << get<2>(it) << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int N, M, D; n const int NMax = 100005; n vector<int> G[NMax]; n int Arb[4 * NMax], Lazy[4 * NMax]; n int Point[NMax], Left[NMax], Right[NMax]; n bool V[NMax]; n int Level[NMax], cnt; n const int INF = -10000000; n int Max[NMax], Max2[NMax]; n void Read() { n   scanf( %d%d%d , &N, &M, &D); n   for (int i = 1; i <= M; i++) { n     scanf( %d , &Point[i]); n     V[Point[i]] = 1; n   } n   for (int i = 1; i < N; i++) { n     int x, y; n     scanf( %d%d , &x, &y); n     G[x].push_back(y); n     G[y].push_back(x); n   } n } n void DFS(int node, int father) { n   Left[node] = ++cnt; n   Level[node] = Level[father] + 1; n   for (int i = 0; i < G[node].size(); i++) { n     int neighb = G[node][i]; n     if (neighb == father) continue; n     DFS(neighb, node); n   } n   Right[node] = cnt; n } n void Update(int K, int L, int R, int x, int y, int val) { n   if (Lazy[K] != INF) { n     Arb[K] = max(Arb[K], Lazy[K]); n     if (L != R) { n       Lazy[K * 2] = max(Lazy[2 * K], Lazy[K]); n       Lazy[K * 2 + 1] = max(Lazy[K], Lazy[2 * K + 1]); n     } n     Lazy[K] = INF; n   } n   if (L > R || L > y || R < x) return; n   if (L >= x && R <= y) { n     Arb[K] = max(Arb[K], val); n     if (L != R) { n       Lazy[K * 2] = max(Lazy[K * 2], val); n       Lazy[K * 2 + 1] = max(Lazy[K * 2 + 1], val); n     } n     return; n   } n   Update(K * 2, L, (L + R) / 2, x, y, val); n   Update(K * 2 + 1, (L + R) / 2 + 1, R, x, y, val); n   Arb[K] = max(Arb[K * 2], Arb[K * 2 + 1]); n } n int Query(int K, int L, int R, int x, int y) { n   if (Lazy[K] != INF) { n     Arb[K] = max(Arb[K], Lazy[K]); n     if (L != R) { n       Lazy[K * 2] = max(Lazy[2 * K], Lazy[K]); n       Lazy[K * 2 + 1] = max(Lazy[K], Lazy[2 * K + 1]); n     } n     Lazy[K] = INF; n   } n   if (L > R || L > y || R < x) return INF; n   if (L >= x && R <= y) return Arb[K]; n   int a = Query(K * 2, L, (L + R) / 2, x, y); n   int b = Query(K * 2 + 1, (L + R) / 2 + 1, R, x, y); n   return max(a, b); n } n void Upd(int node, int val) { n   if (Max[node] < val) { n     Max2[node] = Max[node]; n     Max[node] = val; n   } else if (Max2[node] < val) n     Max2[node] = val; n } n void DFS2(int node, int father) { n   for (int i = 0; i < G[node].size(); i++) { n     int neighb = G[node][i]; n     if (neighb == father) continue; n     DFS2(neighb, node); n     Upd(node, Max[neighb]); n   } n   if (V[node] == 1) Upd(node, Level[node]); n   for (int i = 0; i < G[node].size(); i++) { n     int neighb = G[node][i]; n     if (neighb == father) continue; n     if (Max[node] == Max[neighb]) { n       Update(1, 1, N, Left[neighb], Right[neighb], n              Max2[node] - 2 * Level[node]); n     } else { n       Update(1, 1, N, Left[neighb], Right[neighb], Max[node] - 2 * Level[node]); n     } n   } n   Update(1, 1, N, Left[node], Left[node], Max[node] - 2 * Level[node]); n } n void Solve() { n   int cnt = 0; n   for (int i = 1; i <= N; i++) { n     int x = Query(1, 1, N, Left[i], Left[i]); n     if (x + Level[i] > D) ++cnt; n   } n   printf( %d n , N - cnt); n } n int main() { n   Read(); n   for (int i = 1; i <= 4 * N; i++) Arb[i] = Lazy[i] = INF; n   for (int i = 1; i <= N; i++) Max[i] = Max2[i] = INF; n   DFS(1, 0); n   DFS2(1, 0); n   Solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int p[200005], q[200005]; n int fa[200005], top[200005], sz[200005], dep[200005]; n vector<int> E[200005]; n void dfs1(int root); n void dfs2(int root, int toproot); n int lca(int node1, int node2); n int dis(int node1, int node2); n pair<int, int> merge(int node1, int node2, int node3); n pair<int, int> merge(pair<int, int> path1, pair<int, int> path2); n void modify(int index, int lboundary, int rboundary, int position, n             pair<int, int> path); n int query(int index, int lboundary, int rboundary, pair<int, int> path); n pair<int, int> tr[4 * 200005]; n int main() { n   int n; n   scanf( %d , &n); n   for (int i = 1; i <= n; i++) scanf( %d , p + i), q[p[i]] = i; n   for (int i = 2; i <= n; i++) scanf( %d , fa + i); n   for (int i = 2; i <= n; i++) { n     E[i].push_back(fa[i]); n     E[fa[i]].push_back(i); n   } n   dfs1(1); n   dfs2(1, 1); n   int k; n   scanf( %d , &k); n   for (int i = 1; i <= n; i++) { n     modify(1, 0, n - 1, p[i], make_pair(q[p[i]], q[p[i]])); n   } n   while (k--) { n     int t, u, v; n     scanf( %d , &t); n     if (t == 1) { n       scanf( %d %d , &u, &v); n       swap(p[u], p[v]); n       swap(q[p[u]], q[p[v]]); n       modify(1, 0, n - 1, p[u], make_pair(q[p[u]], q[p[u]])); n       modify(1, 0, n - 1, p[v], make_pair(q[p[v]], q[p[v]])); n     } else if (t == 2) { n       if (tr[1].first) n         cout << n << endl; n       else n         cout << query(1, 0, n - 1, make_pair(q[0], q[0])) << endl; n     } n   } n } n void dfs1(int r) { n   sz[r] = 1; n   for (int u : E[r]) n     if (u != fa[r]) { n       dfs1(u); n       sz[r] += sz[u]; n     } n } n void dfs2(int r, int t) { n   top[r] = t; n   dep[r] = dep[fa[r]] + 1; n   int tid = -1, msz = 0; n   for (int u : E[r]) n     if (u != fa[r] && sz[u] > msz) { n       msz = sz[u]; n       tid = u; n     } n   if (tid == -1) return; n   dfs2(tid, t); n   for (int u : E[r]) n     if (u != fa[r] && u != tid) dfs2(u, u); n } n int lca(int a, int b) { n   while (top[a] != top[b]) { n     if (dep[top[a]] < dep[top[b]]) swap(a, b); n     a = fa[top[a]]; n   } n   return dep[a] < dep[b] ? a : b; n } n int dis(int a, int b) { return dep[a] + dep[b] - 2 * dep[lca(a, b)]; } n pair<int, int> merge(int a, int b, int c) { n   int ab = dis(a, b), ac = dis(a, c), bc = dis(b, c); n   if (ab == bc + ac) n     return make_pair(a, b); n   else if (ac == ab + bc) n     return make_pair(a, c); n   else if (bc == ab + ac) n     return make_pair(b, c); n   else n     return make_pair(0, 0); n } n pair<int, int> merge(pair<int, int> a, pair<int, int> b) { n   if (!a.first || !b.first) return make_pair(0, 0); n   pair<int, int> tmp = merge(a.first, a.second, b.first); n   if (tmp.first) tmp = merge(tmp.first, tmp.second, b.second); n   return tmp; n } n void modify(int o, int l, int r, int p, pair<int, int> v) { n   if (l == r) { n     tr[o] = v; n     return; n   } n   int mid = (l + r) >> 1; n   if (p <= mid) n     modify((o << 1), l, mid, p, v); n   else n     modify((o << 1 | 1), mid + 1, r, p, v); n   tr[o] = merge(tr[(o << 1)], tr[(o << 1 | 1)]); n } n int query(int o, int l, int r, pair<int, int> p) { n   if (l == r) return l; n   int mid = (l + r) >> 1; n   pair<int, int> q = merge(p, tr[(o << 1)]); n   if (!q.first) n     return query((o << 1), l, mid, p); n   else n     return query((o << 1 | 1), mid + 1, r, q); n } n


#include <bits/stdc++.h> n using namespace std; n string s; n bool atted, dotted, goned; n int cntu, cnth = -1, cntr = -1, cntd = -1; n int main() { n   cin >> s; n   for (int i = 0; i < ((int)(s).size()); i++) { n     if (!atted && s[i] ==  @ ) n       atted = 1; n     else if (atted && !goned && s[i] ==  .  && s[i - 1] !=  .  && n              s[i - 1] !=  @  && cntd <= 16) n       dotted = 1, cntd = -1; n     else if (atted && !goned && s[i] ==  / ) n       goned = 1; n     else if ((!atted || goned) && s[i] ==  . ) n       return puts( NO ), 0; n     else if (!isalnum(s[i]) && s[i] !=  _ ) n       return puts( NO ), 0; n     if (!atted) n       cntu++; n     else if (!goned) n       cnth++, cntd++; n     else n       cntr++; n   } n   if (cntd == 0) return puts( NO ), 0; n   if (cntu < 1 || cntu > 16 || cnth < 1 || cnth > 32 || goned && cntr < 1 || n       cntr > 32) n     return puts( NO ), 0; n   return puts( YES ), 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m; n long long dd[200000], h[200000]; n int a, b; n int aaa[100000], bbb[100000]; n long long d[200000]; n long long mmin[200000][18], mmax[200000][18]; n long long xxx(int x) { return h[x] - d[x]; } n long long yyy(int y) { return h[y] + d[y]; } n long long xval(int aa, int bb) { n   int llog = 0; n   while ((bb + 1 - aa) >= (1 << llog)) { n     llog++; n   } n   llog--; n   if (xxx(mmax[aa][llog]) >= xxx(mmax[bb + 1 - (1 << llog)][llog])) { n     return mmax[aa][llog]; n   } else { n     return mmax[bb + 1 - (1 << llog)][llog]; n   } n } n long long yval(int aa, int bb) { n   int llog = 0; n   while ((bb + 1 - aa) >= (1 << llog)) { n     llog++; n   } n   llog--; n   if (yyy(mmin[aa][llog]) >= yyy(mmin[bb + 1 - (1 << llog)][llog])) { n     return mmin[aa][llog]; n   } else { n     return mmin[bb + 1 - (1 << llog)][llog]; n   } n } n int main() { n   ios::sync_with_stdio(0); n   cin >> n >> m; n   for (int i = 0; i < n; i++) { n     cin >> dd[i]; n   } n   for (int i = 0; i < n; i++) { n     cin >> h[i]; n     h[i] *= 2; n   } n   d[0] = dd[n - 1]; n   for (int i = 0; i < n - 1; i++) { n     d[i + 1] = d[i] + dd[i]; n   } n   for (int i = 0; i < n; i++) { n     d[i + n] = d[i] + d[n - 1]; n     h[i + n] = h[i]; n   } n   n *= 2; n   for (int i = 0; i < n; i++) { n     mmax[i][0] = i; n     mmin[i][0] = i; n   } n   for (int j = 1; (1 << j) <= n; j++) { n     for (int i = 0; i < n; i++) { n       mmin[i][j] = mmin[i][j - 1]; n       if ((i + (1 << (j - 1)) < n) && n           (yyy(mmin[i + (1 << (j - 1))][j - 1]) > yyy(mmin[i][j - 1]))) n         mmin[i][j] = mmin[i + (1 << (j - 1))][j - 1]; n       mmax[i][j] = mmax[i][j - 1]; n       if ((i + (1 << (j - 1)) < n) && n           (xxx(mmax[i + (1 << (j - 1))][j - 1]) > xxx(mmax[i][j - 1]))) n         mmax[i][j] = mmax[i + (1 << (j - 1))][j - 1]; n     } n   } n   for (int i = 0; i < m; i++) { n     cin >> aaa[i] >> bbb[i]; n   } n   for (int i = 0; i < m; i++) { n     b = aaa[i]; n     a = bbb[i]; n     a--; n     b--; n     a = (a + 1) % (n / 2); n     b = (b + n - 1) % (n / 2); n     if (a > b) { n       b += n / 2; n     } n     long long r1 = -(1ll << 60); n     int xx = xval(a, b); n     int yy; n     if (xx < b) { n       yy = yval(xx + 1, b); n       r1 = yyy(yy) + xxx(xx); n     } n     yy = yval(a, b); n     long long r2 = -(1ll << 60); n     if (yy > a) { n       xx = xval(a, yy - 1); n       r2 = yyy(yy) + xxx(xx); n     } n     cout << max(r1, r2) << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 100005; n int main() { n   int n, k, m, t, i, c[N]; n   scanf( %d%d , &n, &k); n   int ans = 0; n   while (k--) { n     scanf( %d , &m); n     for (i = 0; i < m; i++) { n       scanf( %d , &c[i]); n     } n     if (c[0] == 1) { n       for (i = 0; i < m; i++) { n         if (c[i] != i + 1) break; n       } n       ans += m - i; n       t = i; n     } else if (c[0] != 1) n       ans += m - 1; n   } n   ans += n - t; n   printf( %d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long res, n, k, d = 0, f, s, moy, mx; n long long t[300003]; n bool b = true; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cin >> n >> k; n   for (long long i = 0; i < n; i++) cin >> t[i]; n   reverse(t, t + n); n   f = k - 1; n   while (d <= f) { n     b = true; n     moy = (d + f) / 2; n     mx = k - 1; n     for (long long i = 0; i < n; i++) { n       if (t[i] <= mx) n         mx = min(mx, t[i] + moy); n       else if ((t[i] + moy) >= k) n         mx = min(mx, t[i] + moy - k); n       else { n         b = false; n         break; n       } n     } n     if (b) { n       res = moy; n       f = moy - 1; n     } else n       d = moy + 1; n   } n   cout << res; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int inf = 0x3f3f3f3f; n const int maxn = 200000 + 10; n int n, m, k, nv; n vector<pair<int, pair<int, int> > > edge; n vector<pair<int, int> > e[maxn]; n int f[maxn]; n bool vis[maxn]; n long long dis[maxn]; n vector<long long> q; n void print(int a[], int n, int st = 0) { n   for (int i = 0; i < n; i++) { n     if (i != 0) printf(   ); n     printf( %d , a[i + st]); n   } n   printf(  n ); n } n void init() { n   scanf( %d%d%d , &n, &m, &k); n   for (int i = 0; i < m; i++) { n     int u, v, w; n     scanf( %d%d%d , &u, &v, &w); n     edge.push_back(make_pair(w, make_pair(min(u, v), max(u, v)))); n   } n } n void doit() { n   sort(edge.begin(), edge.end()); n   memset(f, -1, sizeof(f)); n   nv = 0; n   for (int i = 0; i < min(k, m); i++) { n     int u = edge[i].second.first, v = edge[i].second.second, w = edge[i].first; n     if (f[u] == -1) f[u] = nv++; n     u = f[u]; n     if (f[v] == -1) f[v] = nv++; n     v = f[v]; n     e[u].push_back(make_pair(v, w)); n     e[v].push_back(make_pair(u, w)); n   } n   for (int st = 0; st < nv; st++) { n     for (int i = 0; i < nv; i++) vis[i] = false; n     vis[st] = true; n     for (int i = 0; i < nv; i++) dis[i] = -1; n     dis[st] = 0; n     int cur = st; n     while (1) { n       for (int i = 0; i < e[cur].size(); i++) { n         int v = e[cur][i].first, w = e[cur][i].second; n         if (!vis[v] && (dis[v] == -1 || dis[v] > dis[cur] + w)) { n           dis[v] = dis[cur] + w; n         } n       } n       long long mn = -1; n       for (int i = 0; i < nv; i++) { n         if (!vis[i] && (dis[i] != -1 && (mn == -1 || dis[i] < mn))) { n           mn = dis[i]; n           cur = i; n         } n       } n       if (vis[cur]) break; n       vis[cur] = true; n     } n     for (int i = st + 1; i < nv; i++) { n       if (vis[i]) q.push_back(dis[i]); n     } n   } n   sort(q.begin(), q.end()); n   printf( %lld n , q[k - 1]); n } n int main() { n   init(); n   doit(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <class T> n inline T sqr(const T& x) { n   return x * x; n } n template <class T> n inline void updMin(T& a, const T& b) { n   if (b < a) a = b; n } n template <class T> n inline void updMax(T& a, const T& b) { n   if (b > a) a = b; n } n vector<string> a; n int cross(int i, int j) { n   int neq = 0; n   for (int t = 0; t < 6; t++) { n     if (a[i][t] != a[j][t]) neq++; n   } n   return (neq + 1) / 2; n } n int main() { n   int n; n   cin >> n; n   a.resize(n); n   for (int i = 0; i < n; i++) { n     cin >> a[i]; n   } n   if (n == 1) { n     cout << 6 <<   n ; n   } else { n     int ans = 6; n     for (int i = 0; i < n; i++) { n       for (int j = i + 1; j < n; j++) { n         ans = min(ans, cross(i, j) - 1); n       } n     } n     cout << ans <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int t, n, c0, c1, h, cc0 = 0, cc1 = 0; n   cin >> t; n   cout << endl; n   for (int i = 0; i < t; i++) { n     cin >> n >> c0 >> c1 >> h; n     cout << endl; n     string ali; n     cin >> ali; n     bitset<1001> ss(ali); n     cc1 = ss.count(); n     cc0 = n - cc1; n     int min_d = (cc1 * c1) + (cc0 * c0); n     if (c0 < c1) { n       int maliyet = cc1 * c0 + cc0 * c0 + cc1 * h; n       min_d = min(min_d, maliyet); n     } else { n       int maliyet = cc1 * c1 + cc0 * c1 + cc0 * h; n       min_d = min(min_d, maliyet); n     } n     cout << endl << min_d << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n   n typedef long double ld; n const long long inf = 1100000000000000000; n #define maxn 200050 n #define mod 1000000007 //998244353  n #define int long long  n #define PB push_back n #define F first n #define S second n #define vi vector<int> n #define rep(i,a,b)  for( int i = a; i < b; i++ ) n #define rep1(i,a,b) for( int i = a; i <= b; i++) n #define repd(i,a,b) for( int i = a; i >= b; --i) n #define sz(x) (int)((x).size())   n #define all(c) (c).begin(),(c).end() n #define allr(v) v.rbegin(),v.rend() n #define mem(a,x) memset(a,x,sizeof(a)) n #define dbg(a) cout << #a <<   is   << (a); cout <<   n ; n template <class T> void PR(T a[], int n, int x = 0) { for ( int i = x; i < n; i++ ) cout << a[i] <<    ; cout<<   n  ; } n template <class T> void PR(vector <T> v) { for ( T i : v ) cout << i <<     ; cout <<   n ; } n template <class T> void PR(set <T> s) { for ( T i : s ) cout << i <<     ; cout <<   n ; } n template <class T> void PR(multiset <T> s) { for ( T i : s ) cout << i <<     ; cout <<   n  ; } n const long double PI=3.141592653589793238462643383279502884197169399375105820974944; n //... n void solve(){ n  int n, k; cin>>n>>k; n  string s =   ; n  rep(i, 0, k){ n   rep(j, i, k){ n    s += (j+ a ); n    if(j != k-1) n     s += (i+ a ); n   } n  } n  int m = sz(s); n  rep(i, 0, n) n   cout<<s[i%m]; n } n  n signed main(){ n     ios_base::sync_with_stdio(false), cin.tie(nullptr); n  int T=1;// cin>>T;  n     while(T--){ n         solve();cout<<  n ; n     } n     return 0; n }


#include <bits/stdc++.h> n struct V { n   int a, b; n   int wybor; n }; n std::vector<int> v[507]; n V prev[507][507][2]; n int odl[507][507][2]; n bool odw[507][507][2]; n std::queue<V> Q; n int wynik[1000007][2]; n int main() { n   int N, M; n   scanf( %d%d , &N, &M); n   int a, b; n   for (int i = 0; i < M; i++) { n     scanf( %d%d , &a, &b); n     v[a].push_back(b); n     v[b].push_back(a); n   } n   Q.push({1, N, 0}); n   odw[1][N][0] = true; n   while (!Q.empty()) { n     V ter = Q.front(); n     Q.pop(); n     if (ter.a == N && ter.b == 1 && ter.wybor == 0) break; n     if (ter.a == ter.b && ter.wybor == 0) continue; n     a = ter.a, b = ter.b; n     if (ter.wybor == 0) { n       for (auto it : v[a]) { n         if (!odw[it][b][1]) { n           odw[it][b][1] = true; n           odl[it][b][1] = odl[ter.a][ter.b][ter.wybor] + 1; n           prev[it][b][1] = ter; n           Q.push({it, b, 1}); n         } n       } n     } else { n       for (auto it : v[b]) { n         if (!odw[a][it][0]) { n           odw[a][it][0] = true; n           odl[a][it][0] = odl[ter.a][ter.b][ter.wybor] + 1; n           prev[a][it][0] = ter; n           Q.push({a, it, 0}); n         } n       } n     } n   } n   if (odl[N][1][0] == 0) { n     printf( -1 ); n     return 0; n   } n   int wyn = odl[N][1][0] / 2; n   auto ter = V{N, 1, 0}; n   wynik[wyn][1] = 1; n   while (ter.a != 0) { n     wynik[odl[ter.a][ter.b][ter.wybor] / 2][ter.wybor] = n         ter.wybor ? ter.b : ter.a; n     ter = prev[ter.a][ter.b][ter.wybor]; n   } n   printf( %d n , wyn); n   for (int i = 0; i <= wyn; i++) printf( %d  , wynik[i][0]); n   printf(  n ); n   for (int i = 0; i <= wyn; i++) printf( %d  , wynik[i][1]); n   printf(  n ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n #pragma GCC target( avx2 ) n #pragma GCC optimization( O3 ) n #pragma GCC optimization( unroll-loops ) n const long long INFll = 1e18; n const int INFint = 1e9; n const long long MOD = 1e9 + 7; n void needForSpeed() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n } n bool isPrime(long long a) { n   if (a < 2) return 0; n   for (long long i = 2; i <= sqrt(a); i++) n     if (a % i == 0) return 0; n   return 1; n } n long long binpow(long long base, long long pow, long long mod = INFll) { n   if (pow == 0) n     return 1; n   else if (pow % 2) n     return base * binpow(base, pow - 1, mod) % mod; n   else { n     long long p = binpow(base, pow / 2LL, mod); n     return p * p % mod; n   } n } n template <class T> n void PR_VEC(const vector<T> &vec) { n   cout <<  size(  << vec.size() <<  ):[   ; n   if (vec.size() == 0) { n     cout <<  !EMPTY!  ] n ; n     return; n   } n   cout << vec[0] <<    ; n   for (int i = 1; i < vec.size(); ++i) cout <<   ,    << vec[i] <<    ; n   cout <<   ] n ; n } n int solve() { n   long long n, m, ans; n   cin >> n >> m; n   string s; n   cin >> s; n   ans = n * (m - 1LL); n   for (int i = 1; i < n; ++i) { n     if (s[i] != s[i - 1]) ans += n * (m - 1LL); n   } n   int len = 1; n   for (int i = 1; i < n; ++i) { n     if (len == 1) { n       if (s[i] != s[i - 1]) { n         len = 2; n       } else { n         continue; n       } n     } else { n       if (s[i] != s[i - 2]) { n         ans -= (len * (len - 1LL)) / 2LL; n         if (s[i] == s[i - 1]) n           len = 1; n         else n           len = 2; n       } else { n         ++len; n       } n     } n   } n   ans -= (len * (len - 1LL)) / 2LL; n   cout << ans; n   return 0; n } n int main() { n   needForSpeed(); n   int tests = 1; n   while (tests--) { n     solve(); n   } n   return 0; n } n


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


#include <bits/stdc++.h> n #pragma GCC optimize( O3 ) n using namespace std; n int add(int a, int b) { n   long long x = a + b; n   if (x >= 998244353) x -= 998244353; n   if (x < 0) x += 998244353; n   return x; n } n long long mul(long long a, long long b) { return (a * b) % 998244353; } n long long pw(long long a, long long b) { n   long long ans = 1; n   while (b) { n     if (b & 1) ans = (ans * a) % 998244353; n     a = (a * a) % 998244353; n     b >>= 1; n   } n   return ans; n } n int n; n long long dp[2][1000002]; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cin >> n; n   dp[1][0] = 1; n   dp[0][0] = 1; n   for (int i = 1; i <= n; ++i) { n     int x = (i - 1); n     if (x % 2 == 0) { n       int par = (1 + x / 2) % 2; n       dp[par][i] = mul(dp[!par][x / 2], dp[0][x / 2]); n     } else { n       for (int j = x / 2; j <= 1 + x / 2; j++) { n         int par = (1 + j) % 2; n         dp[par][i] = mul(dp[!par][j], dp[0][i - j - 1]); n       } n     } n   } n   cout << (dp[1][n] + dp[0][n]) % 998244353; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 100 + 2; n int main() { n   int n, m; n   cin >> n >> m; n   int ind = 0; n   int x[3], y[3]; n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < m; j++) { n       char s; n       cin >> s; n       if (s ==  * ) { n         x[ind] = i; n         y[ind] = j; n         ind++; n       } n     } n   } n   int a, b; n   if (x[0] == x[1]) { n     a = x[2]; n   } else if (x[0] == x[2]) { n     a = x[1]; n   } else { n     a = x[0]; n   } n   if (y[0] == y[1]) { n     b = y[2]; n   } else if (y[0] == y[2]) { n     b = y[1]; n   } else { n     b = y[0]; n   } n   cout << a + 1 <<     << b + 1; n } n


#include <bits/stdc++.h> n using namespace std; n string s; n int main() { n   long long n, x, y, br = 0, k = 0; n   cin >> n >> x >> y; n   cin >> s; n   for (long i = 0; i < n; i++) { n     if (s[i] ==  0 ) { n       if (i == 0) { n         br++; n       } else if (s[i - 1] !=  0 ) { n         br++; n       } n     } n   } n   long long sol = min(br * y, (br - 1) * x + y); n   if (sol < 0) { n     sol = br * y; n   } n   cout << sol << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m, a, b, v[2100][2100]; n int k, d[2100][2100], x, y; n bool solve(int x, int y, int k) { n   int l = max(1, x - k), r = min(x + k, n), t; n   for (int i = l; i <= r; i++) { n     t = k - abs(i - x); n     if (y - t > 0 && !v[i][y - t]) { n       a = i; n       b = y - t; n       return 1; n     } n     if (y + t <= m && !v[i][y + t]) { n       a = i; n       b = y + t; n       return 1; n     } n   } n   return 0; n } n int main() { n   cin >> n >> m >> k; n   for (int i = 0; i < k; i++) { n     cin >> x >> y; n     for (int j = -2; j <= 2; j++) n       for (int k = -2; k <= 2; k++) { n         if (x + j < 1 || x + j > n || y + k < 1 || y + k > m) continue; n         d[x][y] = max(d[x][y], d[x + j][y + k] - abs(j) - abs(k)); n       } n     while (!solve(x, y, d[x][y])) d[x][y]++; n     v[a][b] = 1; n     cout << a <<     << b << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n inline int read() { n   int x = 0, f = 1; n   char c = getchar(); n   while (c <  0  || c >  9 ) { n     if (c ==  - ) f = -1; n     c = getchar(); n   } n   while (c >=  0  && c <=  9 ) { n     x = (x << 1) + (x << 3) + c -  0 ; n     c = getchar(); n   } n   return x * f; n } n const int maxn = 305; n int n, a[maxn][maxn]; n int dp[maxn << 1][maxn][maxn]; n int main() { n   n = read(); n   for (int i = 1; i <= n; i++) n     for (int j = 1; j <= n; j++) a[i][j] = read(); n   memset(dp, 0x8f, sizeof(dp)); n   dp[0][1][1] = a[1][1]; n   for (int i = 1; i <= 2 * n - 2; i++) n     for (int j = max(1, i + 2 - n); j <= min(i + 1, n); j++) n       for (int k = max(1, i + 2 - n); k <= min(i + 1, n); k++) { n         dp[i][j][k] = max(max(dp[i - 1][j - 1][k - 1], dp[i - 1][j - 1][k]), n                           max(dp[i - 1][j][k - 1], dp[i - 1][j][k])); n         dp[i][j][k] += a[j][i - j + 2]; n         if (j != k) dp[i][j][k] += a[k][i - k + 2]; n       } n   printf( %d n , dp[2 * n - 2][n][n]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0); n   long long int num, ans = INT_MAX, temp, multi1 = INT_MAX, multi2 = INT_MAX; n   cin >> num; n   for (long long int i = 1; i <= sqrt(num); i++) { n     if (num % i == 0) { n       temp = num / i; n       if (abs(i - temp) < ans) ans = abs(i - temp), multi1 = i, multi2 = temp; n     } n   } n   cout << multi1 <<     << multi2; n   return 0; n } n