#include <bits/stdc++.h> n using namespace std; n int const N = 3000 + 20; n int n, dp[N], mx1, mx2, ans; n vector<pair<int, bool>> adj[N]; n void dfs1(int v, int p = -1) { n   for (auto x : adj[v]) { n     int u = x.first, f = x.second; n     if (u != p) { n       dfs1(u, v); n       dp[v] += dp[u] + (!f); n     } n   } n   return; n } n void dfs2(int v, int p = -1) { n   for (auto x : adj[v]) { n     int u = x.first, f = x.second; n     if (u != p) { n       dp[u] = dp[v]; n       f ? dp[u]++ : dp[u]--; n       dfs2(u, v); n     } n   } n   return; n } n pair<int, int> dfs3(int v, int p = -1) { n   int m1 = 0, m2 = 0; n   for (auto x : adj[v]) { n     int u = x.first, f = x.second; n     if (u != p) { n       m2 = max(m2, dfs3(u, v).first + (f ? -1 : 1)); n       if (m2 > m1) swap(m1, m2); n     } n   } n   return {m1, m2}; n } n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   cin >> n; n   ans = n - 1; n   for (int i = 0; i < n - 1; i++) { n     int x, y; n     cin >> x >> y; n     x--, y--; n     adj[x].push_back({y, 1}); n     adj[y].push_back({x, 0}); n   } n   dfs1(0); n   dfs2(0); n   for (int i = 0; i < n; i++) { n     pair<int, int> cur = dfs3(i); n     mx1 = cur.first, mx2 = cur.second; n     ans = min(ans, dp[i] - mx1 - mx2); n   } n   return cout << ans, 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long int n, c; n vector<long long int> a, b; n long long int pos(long long int l) { n   long long int d1 = 0; n   for (long long int i = 0; i < n; i++) { n     long long int x = (a[i] * l) / b[i]; n     x++; n     d1 += x; n   } n   return d1; n } n pair<long long int, long long int> fun(long long int l, long long int r) { n   while (r - l > 2) { n     long long int md1 = l + (r - l) / 3; n     long long int md2 = r - (r - l) / 3; n     long long int d1 = 0, d2 = 0; n     d1 = pos(md1); n     d2 = pos(md2); n     if (d2 < c) n       l = md2; n     else if (d1 > c) n       r = md1; n     else if (d1 == c && d2 == c) { n       pair<long long int, long long int> p1 = fun(l, md1); n       pair<long long int, long long int> p2 = fun(md2, r); n       pair<long long int, long long int> p = make_pair(p1.first, p2.second); n       return p; n     } else if (d2 == c) { n       pair<long long int, long long int> p1 = fun(md1, md2); n       pair<long long int, long long int> p2 = fun(md2, r); n       pair<long long int, long long int> p = make_pair(p1.first, p2.second); n       return p; n     } else if (d1 == c) { n       pair<long long int, long long int> p1 = fun(l, md1); n       pair<long long int, long long int> p2 = fun(md1, md2); n       pair<long long int, long long int> p = make_pair(p1.first, p2.second); n       return p; n     } else { n       l = md1; n       r = md2; n     } n   } n   if (r - l > 2) n     return {l, r}; n   else { n     long long int in = 0; n     for (long long int i = l; i <= r; i++) { n       if (pos(i) == c) { n         l = i; n         in++; n         break; n       } n     } n     for (long long int i = r; i >= l; i--) { n       if (pos(i) == c) { n         r = i; n         break; n       } n     } n     if (in > 0) n       return {l, r}; n     else n       return {-1, -1}; n   } n } n int32_t main() { n   cin >> n >> c; n   a.resize(n); n   b.resize(n); n   long long int l = 0; n   for (long long int i = 0; i < n; i++) { n     cin >> a[i] >> b[i]; n     if (a[i] == 0) continue; n     long long int x = b[i] / a[i]; n     if (b[i] % a[i] != 0) x++; n     l = max(x, l); n   } n   l = 1; n   long long int r = 1e9 + 1; n   pair<long long int, long long int> ans = fun(l, r); n   l = ans.first; n   r = ans.second; n   if (l == -1) n     cout << 0; n   else n     cout << r - l + 1; n   return 0; n } n


#include <bits/stdc++.h> n #pragma GCC optimize( O3 ) n using namespace std; n template <class c> n struct rge { n   c b, e; n }; n template <class c> n rge<c> range(c i, c j) { n   return rge<c>{i, j}; n } n template <class c> n auto dud(c* x) -> decltype(cerr << *x, 0); n template <class c> n char dud(...); n struct debug { n   template <class c> n   debug& operator<<(const c&) { n     return *this; n   } n }; n void mini(int& a, int b) { a = min(a, b); } n void maxi(int& a, int b) { a = max(a, b); } n int main() { n   int n; n   scanf( %d , &n); n   vector<int> x(n), y(n); n   int big_x = INT_MIN; n   int small_x = INT_MAX; n   int big_y = INT_MIN; n   int small_y = INT_MAX; n   for (int i = 0; i < n; ++i) { n     scanf( %d%d , &x[i], &y[i]); n     mini(small_x, x[i]); n     mini(small_y, y[i]); n     maxi(big_x, x[i]); n     maxi(big_y, y[i]); n   } n   int best = 0; n   for (int i = 0; i < n; ++i) { n     maxi(best, big_x - x[i] + big_y - y[i]); n     maxi(best, x[i] - small_x + big_y - y[i]); n     maxi(best, x[i] - small_x + y[i] - small_y); n     maxi(best, big_x - x[i] + y[i] - small_y); n   } n   printf( %d  , 2 * best); n   for (int i = 4; i <= n; ++i) { n     printf( %d  , 2 * (big_x - small_x + big_y - small_y)); n   } n   puts(  ); n } n


#include <bits/stdc++.h> n using namespace std; n void fastio() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n } n long long fre[1000000]; n int32_t main() { n   fastio(); n   long long n; n   cin >> n; n   long long a[n]; n   for (long long i = 0; i < n; i++) cin >> a[i]; n   sort(a, a + n); n   for (long long i = 0; i < n; i++) { n     for (long long j = i + 1; j < n; j++) { n       fre[a[i] + a[j]]++; n     } n   } n   long long ans = 0; n   for (long long i = 0; i < 1000000; i++) { n     ans = max(ans, fre[i]); n   } n   cout << ans; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long A, B; n   int N; n   long long l, r, t, tt, M; n   cin >> A >> B >> N; n   for (int i = 0; i < N; i++) { n     scanf( %I64d%I64d%I64d , &l, &t, &M); n     long long st = l, ed = l + 1000000; n     long long H = A + (l - 1) * B; n     long long D = B, C = t / B; n     if (t < H) { n       puts( -1 ); n       continue; n     } n     while (ed - st > 1) { n       long long md = (st + ed) >> 1; n       long long E = A + (md - 1) * B; n       long long L = md - l + 1; n       long long tot = (H + E) * L / 2; n       long long tt = max(E, (tot - 1 + M) / M); n       if (t >= tt) n         st = md; n       else n         ed = md; n     } n     long long E = A + (ed - 1) * B; n     long long L = ed - l + 1; n     long long tot = (H + E) * L / 2; n     long long tt = max(E, (tot - 1 + M) / M); n     if (t >= tt) st++; n     cout << st << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 1010; n long long t, n, arr[MAXN], h, sub[MAXN], cnt, add[MAXN]; n long long find(int x) { n   long long l = 0, r = cnt; n   while (l < r) { n     long long mid = (l + r) >> 1; n     if (sub[mid] >= x) n       r = mid; n     else n       l = mid + 1; n   } n   if (sub[l] < x) return cnt + 1; n   return l; n } n bool check(long long x) { n   long long res = n; n   int idx = lower_bound(sub + 1, sub + cnt + 1, x - 1) - sub; n   res += add[idx - 1] + (x - 1) * (n - idx + 1); n   if (res >= h) return true; n   return false; n } n int main() { n   scanf( %d , &t); n   while (t--) { n     cnt = 0; n     memset(sub, 0, sizeof(sub)); n     memset(add, 0, sizeof(add)); n     scanf( %d%lld , &n, &h); n     for (int i = 1; i <= n; ++i) { n       scanf( %d , &arr[i]); n       if (i > 1) sub[++cnt] = arr[i] - arr[i - 1] - 1; n     } n     sub[++cnt] = 1e18; n     sort(sub + 1, sub + cnt + 1); n     for (int i = 1; i <= cnt; ++i) add[i] = add[i - 1] + sub[i]; n     long long l = 1, r = h; n     while (l < r) { n       long long mid = (l + r) >> 1; n       if (check(mid)) n         r = mid; n       else n         l = mid + 1; n     } n     printf( %lld n , l); n   } n } n


#include <bits/stdc++.h> n using namespace std; n long long dp[100050][205]; n int redpac[100050][4]; n int e[100050]; n bool cmp(int x, int y) { return redpac[x][0] < redpac[y][0]; } n int main() { n   int n, m, k, cur = 0; n   long long ans = -1; n   priority_queue<pair<int, pair<int, int> > > availablepackets; n   scanf( %d%d%d , &n, &m, &k); n   for (int i = 0; i < k; i++) { n     scanf( %d%d%d%d , &redpac[i][0], &redpac[i][1], &redpac[i][2], n           &redpac[i][3]); n     e[i] = i; n   } n   sort(e, e + k, cmp); n   memset(dp, -1, sizeof(dp)); n   dp[1][0] = 0; n   for (int i = 1; i <= n; i++) { n     if (availablepackets.size() > 0) { n       while (availablepackets.top().second.second < i) { n         availablepackets.pop(); n         if (availablepackets.size() == 0) break; n       } n     } n     while (cur < k && redpac[e[cur]][0] <= i) { n       availablepackets.push(make_pair( n           redpac[e[cur]][3], make_pair(redpac[e[cur]][2], redpac[e[cur]][1]))); n       cur++; n     } n     for (int j = 0; j < m + 1; j++) { n       if (dp[i][j] == -1) continue; n       if (j < m && (dp[i + 1][j + 1] > dp[i][j] || dp[i + 1][j + 1] == -1)) { n         dp[i + 1][j + 1] = dp[i][j]; n       } n       if (availablepackets.size() > 0) { n         if (dp[availablepackets.top().second.first + 1][j] > n                 dp[i][j] + availablepackets.top().first || n             dp[availablepackets.top().second.first + 1][j] == -1) { n           dp[availablepackets.top().second.first + 1][j] = n               dp[i][j] + availablepackets.top().first; n         } n       } else { n         if (dp[i + 1][j] == -1 || dp[i + 1][j] > dp[i][j]) { n           dp[i + 1][j] = dp[i][j]; n         } n       } n     } n   } n   for (int i = 0; i < m + 1; i++) { n     if (dp[n + 1][i] != -1 && (ans > dp[n + 1][i] || ans == -1)) { n       ans = dp[n + 1][i]; n     } n   } n   printf( %lld , ans); n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   cin >> n; n   vector<int> v(n); n   for (int i = 0; i < n; i++) { n     cin >> v[i]; n   } n   sort(v.begin(), v.end()); n   long long sum = 0; n   for (int i = 0; i < n / 2; i++) { n     long long p_sum = v[i] + v[n - i - 1]; n     sum += (p_sum * p_sum); n   } n   cout << sum << endl; n } n


#include <bits/stdc++.h> n using namespace std; n vector<int> r; n vector<int> l; n int n, ans, a[550000]; n int main() { n   cin >> n; n   for (int i = 1; i <= n; i++) cin >> a[i]; n   a[0] = a[1]; n   a[n + 1] = a[n]; n   for (int i = 1; i <= n; i++) { n     if (a[i] == a[i + 1] && a[i] != a[i - 1]) r.push_back(i); n     if (a[i] != a[i + 1] && a[i] == a[i - 1]) l.push_back(i); n   } n   for (int i = 0; i < l.size(); i++) ans = max(ans, r[i] - l[i]); n   cout << ans / 2 << endl; n   for (int i = 0; i < l.size(); i++) n     if (a[l[i]] == a[r[i]]) n       for (int j = l[i]; j <= r[i]; j++) a[j] = a[l[i]]; n     else { n       for (int j = l[i]; j <= (r[i] + l[i]) / 2; j++) a[j] = a[l[i]]; n       for (int j = (r[i] + l[i]) / 2 + 1; j <= r[i]; j++) a[j] = a[r[i]]; n     } n   for (int i = 1; i <= n; i++) cout << a[i] <<    ; n } n


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


#include <bits/stdc++.h> n using namespace std; n int max(int a, int b, int c, int d) { n   if (a >= b && a >= c && a >= d) return a; n   if (b >= a && b >= c && b >= d) return b; n   if (c >= a && c >= b && c >= d) return c; n   if (d >= a && d >= c && d >= b) return d; n   return d; n } n int main() { n   int x1, x2, x3, x4; n   cin >> x1 >> x2 >> x3 >> x4; n   int mx = max(x1, x2, x3, x4); n   if (mx == x1) { n     cout << mx - x2 <<     << mx - x3 <<     << mx - x4; n   } else { n     if (mx == x2) { n       cout << mx - x1 <<     << mx - x3 <<     << mx - x4; n     } else { n       if (mx == x3) { n         cout << mx - x2 <<     << mx - x1 <<     << mx - x4; n       } else { n         if (mx == x4) { n           cout << mx - x2 <<     << mx - x3 <<     << mx - x1; n         } n       } n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n int t, n, m, cur; n int nxt[200200][26], link[200200], dp[200200], cnt[200200], ord[200200], n     occur[200200]; n char s[200200]; n void extend(int c) { n   int x = cur, y = cur = ++m; n   dp[y] = dp[x] + 1; n   occur[y] = 1; n   while (x && !nxt[x][c]) nxt[x][c] = y, x = link[x]; n   if (!x) n     link[y] = 1; n   else { n     int z = nxt[x][c]; n     if (dp[z] == dp[x] + 1) n       link[y] = z; n     else { n       int u = ++m; n       memcpy(nxt[u], nxt[z], sizeof nxt[u]); n       link[u] = link[z], link[z] = link[y] = u; n       dp[u] = dp[x] + 1; n       while (x && nxt[x][c] == z) nxt[x][c] = u, x = link[x]; n     } n   } n } n void build(char *s) { n   memset(nxt, 0, sizeof nxt), memset(link, 0, sizeof link), n       memset(dp, 0, sizeof dp); n   n = strlen(s), m = 0; n   cur = ++m; n   for (int i = 0; i < n; i++) extend(s[i] -  a ); n   memset(cnt, 0, sizeof cnt); n   for (int i = 1; i <= m; i++) cnt[dp[i]]++; n   for (int i = 1; i <= n; i++) cnt[i] += cnt[i - 1]; n   for (int i = m; i >= 1; i--) ord[cnt[dp[i]]--] = i; n } n int main() { n   scanf( %d , &t); n   while (t--) { n     scanf( %s , s); n     memset(occur, 0, sizeof occur); n     build(s); n     for (int i = m; i >= 1; i--) occur[link[ord[i]]] += occur[ord[i]]; n     long long ans = 0; n     for (int i = 1; i <= m; i++) n       ans += 1ll * occur[i] * occur[i] * (dp[i] - dp[link[i]]); n     printf( %I64d n , ans); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxN = 1e4; n long long F[maxN + 1]; n void fact() { n   F[0] = F[1] = 1; n   for (int i = 2; i <= maxN; i++) F[i] = (1LL * F[i - 1] * i) % 998244353; n } n long long power(long long a, long long b) { n   long long res = 1; n   while (b) { n     if (b & 1) { n       res = (1LL * res * a) % 998244353, b--; n     } else { n       a = (1LL * a * a) % 998244353, b >>= 1; n     } n   } n   return res; n } n long long C(long long n, long long r) { n   if (r > n) return 0; n   long long res = F[n]; n   res = (1LL * res * power(F[r], 998244353 - 2)) % 998244353; n   res = (1LL * res * power(F[n - r], 998244353 - 2)) % 998244353; n   return res; n } n void solve() { n   long long n, k; n   cin >> n >> k; n   string second; n   cin >> second; n   if (!k) { n     cout <<  1 n ; n     return; n   } n   long long ans = 1, l = 0, r = 0, cnt = 0; n   while (r < n) { n     if (second[r] ==  1 ) { n       if (cnt == k) { n         (ans += (C(r - l, k) - 1 + 998244353) % 998244353) %= 998244353; n         while (l < n && second[l] !=  1 ) l++; n         l++, cnt--; n         ans = (ans - C(r - l, k - 1) + 1 + 998244353) % 998244353; n       } n       cnt++; n     } n     r++; n   } n   if (cnt == k) (ans += (C(r - l, k) - 1 + 998244353) % 998244353) %= 998244353; n   cout << ans <<   n ; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0), cout.tie(0), fact(); n   solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<int> v[250001]; n int a, ans[250001], n, i, q, kol; n bool pole[250001]; n void dfs(int nom) { n   int i; n   kol++; n   pole[nom] = true; n   for (i = 0; i < v[nom].size(); i++) n     if (!pole[v[nom][i]]) dfs(v[nom][i]); n   ans[nom] = kol; n } n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cin >> q; n   while (q--) { n     cin >> n; n     for (i = 1; i <= n; i++) { n       v[i].clear(); n       pole[i] = false; n     } n     for (i = 1; i <= n; i++) { n       cin >> a; n       v[i].push_back(a); n     } n     for (i = 1; i <= n; i++) n       if (!pole[i]) { n         kol = 0; n         dfs(i); n       } n     for (i = 1; i <= n - 1; i++) cout << ans[i] <<    ; n     cout << ans[n] <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int inf = 1e9 + 7; n const long long INF = 1e18L + 7; n template <class n, class second> n ostream &operator<<(ostream &p, pair<n, second> x) { n   return p <<  <  << x.first <<  ,   << x.second <<  > ; n } n template <class n> n auto operator<<(ostream &p, n y) -> n     typename enable_if<!is_same<n, string>::value, n                        decltype(y.begin(), p)>::type { n   int o = 0; n   p <<  { ; n   for (auto c : y) { n     if (o++) p <<  ,  ; n     p << c; n   } n   return p <<  } ; n } n void dor() { cerr <<   n ; } n template <class n, class... second> n void dor(n p, second... y) { n   cerr << p <<    ; n   dor(y...); n } n template <class n, class second> n void mini(n &p, second y) { n   if (p > y) p = y; n } n template <class n, class second> n void maxi(n &p, second y) { n   if (p < y) p = y; n } n const int mod = 998244353; n const int N = 507; n int n, m; n int tab[N]; n int dp[N][N][3]; n int vis[N][N][3]; n int pie[N]; n int dru[N]; n void solve(int a, int b, int t) { n   if (vis[a][b][t]) return; n   vis[a][b][t] = 1; n   if (a > b) { n     if (t == 2) n       dp[a][b][t] = 1; n     else n       dp[a][b][t] = 0; n     return; n   } n   int col = inf; n   int ind = -1; n   if (t == 2) { n     for (int i = a; i <= b; ++i) { n       if (col > tab[i]) { n         col = tab[i]; n         ind = i; n       } n     } n     solve(a, ind, 1); n     solve(ind, b, 0); n     dp[a][b][t] = (long long)dp[a][ind][1] * dp[ind][b][0] % mod; n   } else if (t == 0) { n     for (int i = a; i <= b; ++i) { n       solve(a + 1, i, 2); n       solve(i + 1, b, 2); n       dp[a][b][t] += (long long)dp[a + 1][i][2] * dp[i + 1][b][2] % mod; n       if (dp[a][b][t] >= mod) dp[a][b][t] -= mod; n     } n   } else if (t == 1) { n     for (int i = a; i <= b; ++i) { n       solve(a, i - 1, 2); n       solve(i, b - 1, 2); n       dp[a][b][t] += (long long)dp[a][i - 1][2] * dp[i][b - 1][2] % mod; n       if (dp[a][b][t] >= mod) dp[a][b][t] -= mod; n     } n   } n } n int32_t main() { n   ios_base::sync_with_stdio(0); n   cin.tie(NULL); n   cin >> n >> m; n   for (int i = 1; i <= n; ++i) { n     cin >> tab[i]; n   } n   solve(1, n, 2); n   cout << dp[1][n][2] <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n int n, a[3005], b[3005], df[3005]; n long long F[3005][3005]; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   cin >> n; n   for (int i = 1; i <= n; ++i) { n     cin >> a[i]; n     a[i] = a[i] - i; n     b[i] = a[i]; n   } n   sort(b + 1, b + n + 1); n   int m = 0; n   b[0] = b[1] - 1; n   for (int i = 1; i <= n; ++i) { n     if (b[i] > b[i - 1]) df[++m] = b[i]; n   } n   for (int i = 1; i <= n; ++i) n     for (int j = 0; j <= m; ++j) { n       F[i][j] = 1e18; n     } n   for (int i = 1; i <= n; ++i) { n     for (int j = 1; j <= m; ++j) { n       F[i][j] = min(F[i][j - 1], F[i - 1][j] + abs(a[i] - df[j])); n     } n   } n   long long rs = 1e18; n   for (int i = 1; i <= m; ++i) rs = min(rs, F[n][i]); n   cout << rs; n   return 0; n } n


#include <bits/stdc++.h> n #pragma GCC optimize( -O2 ) n using namespace std; n const int LIM = 2e5 + 5, MOD = 1e9 + 7; n int t, n, m, k, x, y; n template <typename... T> n void read(T &...args) { n   ((cin >> args), ...); n } n template <typename... T> n void print(T &&...args) { n   ((cout << args <<    ), ...); n } n int main() { n   int n; n   read(n); n   long long int arr[n]; n   for (int i = 0; i < n; i++) read(arr[i]); n   sort(arr, arr + n); n   long long int val = pow(arr[n - 1], 1.0 / (n - 1)); n   long long int a1 = 0, a2 = 0; n   for (int i = 0; i < n; i++) { n     a1 += abs(pow(val, i) - arr[i]); n   } n   val++; n   for (int i = 0; i < n; i++) { n     a2 += abs(pow(val, i) - arr[i]); n   } n   long long int curr = pow(val, n - 2), curr2 = pow(val, n - 1); n   if (curr2 / curr == val) n     cout << min(a1, a2) << endl; n   else n     cout << a1 << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n typedef struct frac { n   long long p, q; n } frac; n const double eps = 1e-8; n int n; n int nb[100100]; n vector<int> G[100100]; n bool col[100100]; n int cur[100100]; n int onstk[100100]; n inline bool operator<(frac a, frac b) { return a.p * b.q < b.p * a.q; } n inline bool operator==(frac a, frac b) { return a.p * b.q == b.p * a.q; } n inline bool operator>(frac a, frac b) { return a.p * b.q > b.p * a.q; } n inline bool operator>=(frac a, double b) { n   return ((double)a.p / (double)a.q) >= b; n } n inline bool operator!=(frac a, frac b) { return a.p * b.q != b.p * a.q; } n inline frac newfrac(long long a, long long b) { n   frac x; n   x.p = a; n   x.q = b; n   return x; n } n inline bool valid(double z, bool pr) { n   priority_queue<pair<frac, int>, vector<pair<frac, int> >, n                  greater<pair<frac, int> > > n       S; n   for (int i = (1); i <= (n); i++) { n     cur[i] = 0; n     onstk[i] = false; n   } n   for (int x = (1); x <= (n); x++) { n     if (col[x]) { n       for (int j = (0); j < (G[x].size()); j++) { n         int y = G[x][j]; n         if (col[y]) { n           cur[x]++; n         } n       } n       S.push(make_pair(newfrac(cur[x], nb[x]), x)); n       onstk[x] = true; n     } n   } n   bool ok = false; n   while (!S.empty()) { n     if (!onstk[S.top().second]) { n       S.pop(); n       continue; n     } n     if (S.top().first >= z) { n       ok = true; n       break; n     } else { n       int x = S.top().second; n       S.pop(); n       onstk[x] = false; n       for (int j = (0); j < (G[x].size()); j++) { n         int y = G[x][j]; n         if (onstk[y]) { n           cur[y]--; n           S.push(make_pair(newfrac(cur[y], nb[y]), y)); n         } n       } n     } n   } n   if (pr) { n     int cnt = 0; n     vector<int> V; n     for (int i = (1); i <= (n); i++) { n       if (onstk[i]) { n         cnt++; n         V.push_back(i); n       } n     } n     printf( %d n , cnt); n     for (int i = (0); i < (V.size()); i++) { n       printf( %d , V[i]); n       if (i < V.size() - 1) { n         printf(   ); n       } n     } n     printf(  n ); n   } n   return ok; n } n int main() { n   int m, k, x, y; n   scanf( %d%d%d , &n, &m, &k); n   for (int i = (1); i <= (n); i++) { n     nb[i] = 0; n     col[i] = true; n   } n   for (int i = (1); i <= (k); i++) { n     scanf( %d , &x); n     col[x] = false; n   } n   for (int i = (1); i <= (m); i++) { n     scanf( %d%d , &x, &y); n     nb[x]++; n     nb[y]++; n     if ((col[y]) and (col[x])) { n       G[x].push_back(y); n       G[y].push_back(x); n     } n   } n   double l = 0.0, r = 1.0; n   while ((r - l) > eps) { n     double mid = (l + r) / 2; n     if (valid(mid, false)) { n       l = mid; n     } else { n       r = mid; n     } n   } n   col[0] = valid(l, true); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int n = 100002; n char h[n], h1[n]; n int a[n]; n bool check(int e, int r, int v1, int c1) { n   int c, v, c2, v2; n   while (e < r) { n     for (c = 0; c < 10; c++) n       for (v = 0; v < 10; v++) n         if (c + v == v1 + h[r]) { n           if ((c == 0) && (e == 0)) continue; n           goto st; n         } n     if ((h[r] == 9) && (v1 == 10)) { n       c = 9; n       v = 0; n       goto st; n     } n     return 0; n   st: n     h[r - 1] -= (c + v) / 10; n     if (h[r - 1] < 0) { n       for (c2 = r - 2; (c2 >= 0) && (h[c2 + 1] < 0); c2--) { n         h[c2 + 1] += 10; n         h[c2]--; n       } n       if (c2 < e - 1) return 0; n     } n     a[e] = c; n     a[r] = v; n     c += v; n     v = v1 + h[e] - c; n     if ((v != 0) && (v != 1)) return 0; n     v1 = v * 10; n     c1 = c / 10; n     if (e == r - 1) { n       if (v == 1) return 0; n       return 1; n     } n     e++; n     r--; n   } n   if (e > r) { n     if ((v1 + c1 == 11) || (v1 + c1 == 0)) return 1; n     return 0; n   } n   for (c = 0; c < 10; c++) n     if (c + c == v1 + h[e]) break; n   if (c == 10) return 0; n   a[e] = c; n   return 1; n } n int main() { n   int q, w, e, r, t; n   scanf( %s , h1); n   q = strlen(h1); n   for (w = 0; w < q; w++) h1[w] -= 48; n   for (w = 0; w < q; w++) h[w] = h1[w]; n   if (check(0, q - 1, 0, 0)) { n     for (w = 0; w < q; w++) printf( %d , a[w]); n     return 0; n   } n   for (w = 0; w < q; w++) h[w] = h1[w]; n   if ((h[0] == 1) && (check(1, q - 1, 10, 0))) { n     for (w = 1; w < q; w++) printf( %d , a[w]); n     return 0; n   } n   printf( 0 ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e5 + 10; n int A, B, C, D; n struct Point { n   long long x, y; n   Point(long long x, long long y) : x(x), y(y) {} n }; n bool operator<(const Point& a, const Point& b) { n   return a.x < b.x || (a.x == b.x && a.y > b.y); n } n int main() { n   int n; n   scanf( %d , &n); n   scanf( %d/%d %d/%d , &A, &B, &C, &D); n   vector<Point> P; n   for (int i = 0; i < n; ++i) { n     int u, v; n     scanf( %d%d , &u, &v); n     long long x = (long long)B * v - (long long)A * u; n     long long y = (long long)C * u - (long long)D * v; n     if (x > 0 && y > 0) { n       P.emplace_back(Point(x, y)); n     } n   } n   sort(P.begin(), P.end()); n   vector<long long> len; n   for (auto& it : P) { n     int idx = lower_bound(len.begin(), len.end(), it.y) - len.begin(); n     if (idx == len.size()) { n       len.emplace_back(it.y); n     } else { n       len[idx] = it.y; n     } n   } n   printf( %d n , len.size()); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int dp[5000][5000]; n int main() { n   char a[5000], b[5000]; n   int ti, td, tr, te; n   scanf( %d %d %d %d , &ti, &td, &tr, &te); n   scanf( %s %s , a, b); n   int n = strlen(a); n   int m = strlen(b); n   dp[0][0] = 0; n   for (int i = 1; i <= n; i++) dp[i][0] = dp[i - 1][0] + td; n   for (int i = 1; i <= m; i++) dp[0][i] = dp[0][i - 1] + ti; n   int da[26]; n   int db; n   memset(da, 0, sizeof(da)); n   int ii, jj, d; n   for (int i = 1; i <= n; i++) { n     db = 0; n     for (int j = 1; j <= m; j++) { n       d = (a[i - 1] == b[j - 1]) ? 0 : tr; n       ii = da[b[j - 1] -  a ]; n       jj = db; n       db = (a[i - 1] == b[j - 1]) ? j : db; n       dp[i][j] = n           min(dp[i][j - 1] + ti, min(dp[i - 1][j] + td, dp[i - 1][j - 1] + d)); n       if (ii > 0 && jj > 0) n         dp[i][j] = min(dp[i][j], dp[ii - 1][jj - 1] + (i - ii - 1) * td + n                                      (j - jj - 1) * ti + te); n     } n     da[a[i - 1] -  a ] = i; n   } n   printf( %d n , dp[n][m]); n } n


#include <bits/stdc++.h> n int n; n int main() { n   int i; n   scanf( %d , &n); n   for (i = 20; i >= 0; i--) { n     if (n & (1 << i)) printf( %d  , i + 1); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 100005; n struct all { n   int t, num; n   char x, type; n } a[N]; n bool cmp(all a, all b) { return a.t < b.t; } n int redhome[N], redaway[N]; n int yellhome[N], yellaway[N]; n string home, away; n int n; n int main() { n   cin >> home >> away; n   cin >> n; n   for (int i = 1; i <= n; ++i) cin >> a[i].t >> a[i].x >> a[i].num >> a[i].type; n   sort(a + 1, a + n + 1, &cmp); n   for (int i = 1; i <= n; ++i) { n     if (a[i].x ==  h ) { n       if (a[i].type ==  r ) { n         if (redhome[a[i].num] == 0) { n           redhome[a[i].num] = a[i].t; n           cout << home <<     << a[i].num <<     << a[i].t << endl; n         } n       } else { n         if (yellhome[a[i].num] == 0) n           yellhome[a[i].num] = a[i].t; n         else if (redhome[a[i].num] == 0) { n           redhome[a[i].num] = a[i].t; n           cout << home <<     << a[i].num <<     << a[i].t << endl; n         } n       } n     } else { n       if (a[i].type ==  r ) { n         if (redaway[a[i].num] == 0) { n           redaway[a[i].num] = a[i].t; n           cout << away <<     << a[i].num <<     << a[i].t << endl; n         } n       } else { n         if (yellaway[a[i].num] == 0) n           yellaway[a[i].num] = a[i].t; n         else if (redaway[a[i].num] == 0) { n           redaway[a[i].num] = a[i].t; n           cout << away <<     << a[i].num <<     << a[i].t << endl; n         } n       } n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, p; n   cin >> n >> p; n   string s; n   cin >> s; n   bool f = 0; n   int i = s.size() - 1; n   if (i > 0) { n     while (s[i] -  a  == p - 1) { n       s[i] =  a ; n       i--; n     } n     s[i]++; n   } n   if (s.size() == 2) i = s.size() - 1; n   while (!f) { n     if (i > 1 && s[i] != s[i - 1] && s[i] != s[i - 2]) n       if (i == s.size() - 1) { n         f = 1; n         break; n       } else n         i++; n     else if (i > 1 && (s[i] == s[i - 1] || s[i] == s[i - 2])) { n       if (s[i] -  a  < p - 1) n         s[i]++; n       else { n         s[i] =  a ; n         i--; n         while (s[i] -  a  == p - 1 && i >= 0) { n           s[i] =  a ; n           i--; n         } n         if (i < 0) { n           f = 0; n           break; n         } n         if (i != 0) s[i]++; n       } n     } else if (i == 1 && s[i] != s[i - 1]) n       if (i == s.size() - 1) { n         f = 1; n         break; n       } else n         i++; n     else if (i == 1 && s[i] == s[i - 1]) n       if (s[i] -  a  < p - 1) n         s[i]++; n       else { n         s[i] =  a ; n         i--; n         if (s[i] -  a  == p - 1) { n           f = 0; n           break; n         } n       } n     else if (i == 0) { n       if (s[i] -  a  < p - 1) { n         s[i]++; n         i++; n       } else { n         f = 0; n         break; n       } n       if (s.size() == 1) { n         f = 1; n         break; n       } n     } n   } n   if (f != 0) n     cout << s << endl; n   else n     cout <<  NO  << endl; n } n


#include <bits/stdc++.h> n #pragma GCC optimize(2) n using namespace std; n void qread(int &x) { n   int neg = 1; n   x = 0; n   char c = getchar(); n   while (c <  0  || c >  9 ) { n     if (c ==  - ) neg = -1; n     c = getchar(); n   } n   while (c >=  0  && c <=  9 ) x = 10 * x + c -  0 , c = getchar(); n   x *= neg; n } n const int maxn = 100005; n int n, l, r, k, ans, ansu, ansv; n vector<pair<int, int> > edge[maxn]; n vector<int> num; n int oriw[maxn], w[maxn], dep[maxn], lvl[maxn], son[maxn]; n int pos[maxn], dis[maxn]; n pair<int, int> val[maxn]; n namespace Segtree { n int sz; n pair<int, int> t[maxn * 4]; n void init(int _sz) { n   for (sz = 1; sz < _sz; sz <<= 1) n     ; n   for (int(i) = 0; (i) < sz << 1; i++) t[i] = make_pair(-1e9, -1e9); n } n void update(int v, pair<int, int> val) { n   v += sz - 1; n   t[v] = val; n   for (v >>= 1; v; v >>= 1) t[v] = max(t[v << 1], t[v << 1 | 1]); n } n pair<int, int> query(int l, int r) { n   pair<int, int> res = make_pair(-1e9, -1e9); n   for (l += sz - 1, r += sz - 1; l <= r; l >>= 1, r >>= 1) { n     if (l & 1) res = max(res, t[l]), l++; n     if ((r + 1) & 1) res = max(res, t[r]), r--; n   } n   return res; n } n }  // namespace Segtree n void prework1(int x, int p, int d) { n   dep[x] = lvl[x] = d; n   for (int(i) = 0; (i) < edge[x].size(); i++) { n     pair<int, int> y = edge[x][i]; n     if (y.first == p) continue; n     prework1(y.first, x, d + 1); n     if (!son[x] || lvl[y.first] > lvl[son[x]]) son[x] = y.first; n     lvl[x] = max(lvl[x], lvl[y.first]); n   } n } n void prework2(int x, int p) { n   pos[x] = ++k; n   if (son[x]) prework2(son[x], x); n   for (int(i) = 0; (i) < edge[x].size(); i++) { n     pair<int, int> y = edge[x][i]; n     if (y.first == p || y.first == son[x]) continue; n     prework2(y.first, x); n   } n } n void work(int x, int p, int cur) { n   val[pos[x]] = make_pair(cur, x); n   dis[x] = cur; n   Segtree::update(pos[x], make_pair(cur, x)); n   for (int(i) = 0; (i) < edge[x].size(); i++) { n     int y = edge[x][i].first; n     if (y == son[x]) work(y, x, cur + w[edge[x][i].second]); n   } n   for (int(i) = 0; (i) < edge[x].size(); i++) { n     int y = edge[x][i].first; n     if (y == p || y == son[x]) continue; n     work(y, x, cur + w[edge[x][i].second]); n     int tmp = lvl[y] - dep[y]; n     for (int(d) = 0; (d) < tmp + 1; d++) { n       int ql = max(l - d - 1, 0), qr = min(r - d - 1, lvl[x] - dep[x]); n       pair<int, int> val2 = Segtree::query(pos[x] + ql, pos[x] + qr); n       if (val[pos[y] + d].first + val2.first - 2 * dis[x] > ans) { n         ans = val[pos[y] + d].first + val2.first - 2 * dis[x]; n         ansu = val[pos[y] + d].second; n         ansv = val2.second; n       } n     } n     for (int(d) = 0; (d) < tmp + 1; d++) { n       val[pos[x] + d + 1] = max(val[pos[x] + d + 1], val[pos[y] + d]); n       Segtree::update(pos[x] + d + 1, val[pos[x] + d + 1]); n     } n   } n   pair<int, int> val2 = n       Segtree::query(pos[x] + l, pos[x] + min(r, lvl[x] - dep[x])); n   if (val2.first - dis[x] > ans) { n     ans = val2.first - dis[x]; n     ansu = x; n     ansv = val2.second; n   } n } n bool check(int x) { n   for (int(i) = 1; (i) <= n - 1; i++) { n     if (oriw[i] < x) n       w[i] = -1; n     else n       w[i] = 1; n   } n   Segtree::init(n); n   for (int(i) = 1; (i) <= n; i++) val[i] = make_pair(-1e9, -1e9); n   ans = -1e9; n   work(1, 0, 0); n   return ans >= 0; n } n int main() { n   scanf( %d%d%d , &n, &l, &r); n   for (int(i) = 1; (i) <= n - 1; i++) { n     int u, v; n     scanf( %d%d%d , &u, &v, &oriw[i]); n     edge[u].push_back(make_pair(v, i)); n     edge[v].push_back(make_pair(u, i)); n     num.push_back(oriw[i]); n   } n   prework1(1, 0, 0); n   prework2(1, 0); n   sort(num.begin(), num.end()); n   num.resize(unique(num.begin(), num.end()) - num.begin()); n   int l = 0, r = num.size(); n   while (r - l > 1) { n     int mid = (l + r) >> 1; n     if (check(num[mid])) n       l = mid; n     else n       r = mid; n   } n   check(num[l]); n   printf( %d %d n , ansu, ansv); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n using namespace std::chrono; n struct custom_hash { n   size_t operator()(uint64_t x) const { n     static const uint64_t FIXED_RANDOM = n         chrono::steady_clock::now().time_since_epoch().count(); n     return x + FIXED_RANDOM; n   } n }; n void solve() { n   long long n; n   cin >> n; n   for (long long i = 2; i * i <= n; i++) { n     if (n % i == 0) { n       cout << n / i <<     << n - (n / i) <<   n ; n       return; n     } n   } n   cout << 1 <<     << n - 1 <<   n ; n } n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   int t; n   cin >> t; n   while (t--) solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long MAXN = 200 + 17, inf = 2e18 + 17; n long long a[MAXN], b[MAXN], n, m, k, t[MAXN][MAXN], np; n void fillA() { n   for (int i = 1; i <= m; i++) n     for (int j = 0; i - j > 0 && j < n; j++) a[i] = min(t[j + 1][i - j], a[i]); n   for (int i = 1; i <= n; i++) n     for (int j = 0; i + j <= n && j < m; j++) n       a[i + m - 1] = min(a[i + m - 1], t[i + j][m - j]); n } n void fillBackT() { n   for (int i = 1; i <= m; i++) n     for (int j = 0; i - j > 0 && j < n; j++) t[j + 1][i - j] = a[i]; n   for (int i = 1; i <= n; i++) n     for (int j = 0; i + j <= n && j < m; j++) t[i + j][m - j] = a[i + m - 1]; n } n long long count(int x) { n   a[x] = 0; n   long long dp[MAXN][MAXN] = {}; n   if (a[1] == 1) n     dp[1][1] = 0; n   else n     dp[1][1] = 1; n   for (int i = 2; i <= np; i++) n     for (int j = 0; j <= np; j++) { n       if (a[i] == 1) n         dp[i][j] = dp[i - 1][j + 1]; n       else if (a[i] == 0) n         dp[i][j] = (j > 0 ? dp[i - 1][j - 1] : 0); n       else n         dp[i][j] = n             min(inf, (dp[i - 1][j + 1] + (j > 0 ? dp[i - 1][j - 1] : 0))); n     } n   return dp[np][0]; n } n int main() { n   ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0); n   cin >> n >> m >> k; n   for (int i = 1; i <= n; i++) n     for (int j = 1; j <= m; j++) cin >> t[i][j]; n   fill(a, a + MAXN, inf); n   fillA(); n   np = n + m - 1; n   iota(b + 1, b + np + 1, 1); n   sort(b + 1, b + np + 1, [](int i, int j) { return a[i] < a[j]; }); n   for (int i = 1; i <= np; i++) { n     long long tmp = count(b[i]); n     if (tmp >= k) n       a[b[i]] = 0; n     else { n       a[b[i]] = 1; n       k -= tmp; n     } n   } n   fillBackT(); n   for (int i = 1; i <= n; i++) { n     for (int j = 1; j <= m; j++) cout << (t[i][j] ?  )  :  ( ); n     cout << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int high = 2e5 + 6; n long long cnt[200005], dif[200005]; n int main() { n   long long n, m, k, i, j, ans, l, r, d, sum = 0, q; n   scanf( %lld%lld%lld , &n, &k, &q); n   for (i = 0; i < n; i++) { n     scanf( %lld%lld , &l, &r); n     dif[l]++; n     dif[r + 1]--; n   } n   for (i = 1; i <= high; i++) { n     dif[i] += dif[i - 1]; n   } n   for (i = 1; i <= high; i++) { n     if (dif[i] >= k) { n       cnt[i] = 1; n     } n   } n   for (i = 1; i <= high; i++) { n     cnt[i] += cnt[i - 1]; n   } n   while (q--) { n     scanf( %lld%lld , &l, &r); n     printf( %lld n , cnt[r] - cnt[l - 1]); n   } n } n


#include <bits/stdc++.h> n const long long int MOD = 1000000007ll; n const int mxsz = 200005; n using namespace std; n int main() { n   std::ios_base::sync_with_stdio(false); n   long long int n, m, a; n   cin >> n >> m >> a; n   int ar1[n + 1], ar2[m + 1]; n   for (int i = 1; i <= n; i++) { n     cin >> ar1[i]; n   } n   for (int i = 1; i <= m; i++) { n     cin >> ar2[i]; n   } n   sort(ar1 + 1, ar1 + n + 1); n   sort(ar2 + 1, ar2 + m + 1); n   vector<long long int> b, p; n   for (int i = 1; i <= min(n, m); i++) { n     b.push_back(ar1[n - i + 1]); n     p.push_back(ar2[i]); n   } n   int l = 0, r = min(n, m); n   int ans = 0; n   while (l <= r) { n     int mid = (l + r) / 2; n     long long int tmp = a; n     for (int i = 0; i < mid; i++) { n       tmp -= (long long int)max(0LL, p[mid - i - 1] - b[i]); n       if (tmp < 0) { n         break; n       } n     } n     if (tmp >= 0) { n       l = mid + 1; n       ans = mid; n     } else { n       r = mid - 1; n     } n   } n   cout << ans <<    ; n   long long int tmp = 0; n   for (int i = 0; i < ans; i++) { n     tmp += (long long int)p[i]; n   } n   cout << max(0LL, tmp - a); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int x[1000], y[1000], k, n, m; n bool chck(int xx) { n   vector<int> v; n   v.push_back(1); n   v.push_back(m); n   for (int i = 0; i < k; ++i) { n     v.push_back(max(1, x[i] - xx)); n     v.push_back(max(1, x[i] - xx - 1)); n     v.push_back(min(m, x[i] + xx)); n     v.push_back(min(m, x[i] + xx + 1)); n   } n   sort(v.begin(), v.end()); n   int mn = 1e9, mx = -1, tmp, nm, c; n   bool l; n   vector<pair<int, int> > f; n   for (int i = 0; i < v.size(); ++i) { n     tmp = v[i]; n     f.clear(); n     for (int j = 0; j < k; ++j) { n       if (x[j] - xx <= tmp && x[j] + xx >= tmp) { n         f.push_back(make_pair(max(1, y[j] - xx), 0)); n         f.push_back(make_pair(min(n, y[j] + xx), 1)); n       } n     } n     sort(f.begin(), f.end()); n     c = 0; n     nm = 0; n     l = 0; n     for (int j = 0; j < f.size(); ++j) { n       if (nm == 0) { n         if (f[j].first != c + 1) { n           break; n         } n       } n       c = f[j].first; n       if (f[j].second == 0) n         ++nm; n       else n         --nm; n       if (c == n) { n         l = 1; n         break; n       } n     } n     if (!l) { n       mn = min(mn, tmp); n       mx = max(mx, tmp); n     } n   } n   mx = min(mx, m); n   mn = max(1, mn); n   if (mx - mn + 1 > xx + xx + 1) { n     return 0; n   } n   v.clear(); n   v.push_back(1); n   v.push_back(n); n   for (int i = 0; i < k; ++i) { n     v.push_back(max(1, y[i] - xx)); n     v.push_back(max(1, y[i] - xx - 1)); n     v.push_back(min(n, y[i] + xx)); n     v.push_back(min(n, y[i] + xx + 1)); n   } n   sort(v.begin(), v.end()); n   mn = 1e9, mx = -1; n   for (int i = 0; i < v.size(); ++i) { n     tmp = v[i]; n     f.clear(); n     for (int j = 0; j < k; ++j) { n       if (y[j] - xx <= tmp && y[j] + xx >= tmp) { n         f.push_back(make_pair(max(1, x[j] - xx), 0)); n         f.push_back(make_pair(min(m, x[j] + xx), 1)); n       } n     } n     sort(f.begin(), f.end()); n     c = 0; n     nm = 0; n     l = 0; n     for (int j = 0; j < f.size(); ++j) { n       if (nm == 0) { n         if (f[j].first != c + 1) { n           break; n         } n       } n       c = f[j].first; n       if (f[j].second == 0) n         ++nm; n       else n         --nm; n       if (c == m) { n         l = 1; n         break; n       } n     } n     if (!l) { n       mn = min(mn, tmp); n       mx = max(mx, tmp); n     } n   } n   mx = min(mx, n); n   mn = max(1, mn); n   if (mx - mn + 1 > xx + xx + 1) { n     return 0; n   } n   return 1; n } n int main() { n   while (cin >> m >> n >> k) { n     for (int i = 0; i < k; ++i) { n       scanf( %d%d , &x[i], &y[i]); n     } n     int st = 0, nd = 1e9, md; n     while (st < nd) { n       md = (st + nd) / 2; n       if (chck(md)) { n         nd = md; n       } else { n         st = md + 1; n       } n     } n     printf( %d n , nd); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int cansel_sync = (ios::sync_with_stdio(0), cin.tie(0), 0); n const int N = 200010; n const int inf = ~0u >> 2; n const long long INF = ~0ull >> 2; n long long read() { n   long long x; n   if (scanf( %lld , &x) == -1) exit(0); n   return x; n } n const double pi = acos(-1.0); n double readf() { n   double x; n   if (scanf( %lf , &x) == -1) exit(0); n   return x; n } n template <typename T> n T sqr(const T &x) { n   return x * x; n } n int mod = (1 ? 1000000007 : 998244353); n long long mul(long long a, long long b, long long m = mod) { return a * b % m; } n long long qpow(long long a, long long b, long long m = mod) { n   long long ans = 1; n   for (; b; a = mul(a, a, m), b >>= 1) n     if (b & 1) ans = mul(ans, a, m); n   return ans; n } n long long getinv(long long v, long long m = mod) { return qpow(v, m - 2, m); } n long long n, x, t, l, r, sum; n void solve() { n   cin >> n >> x; n   sum = 0; n   l = inf, r = -inf; n   for (long long i = (0), _ = (n); i < _; i++) { n     cin >> t; n     if (t % x != 0) l = min(l, i), r = max(r, i); n     sum += t % x; n   } n   if (sum % x != 0) { n     cout << n << endl; n     return; n   } n   if (l != inf) { n     cout << max(n - l - 1, r) << endl; n     return; n   } n   cout << -1 << endl; n } n signed main() { n   long long T; n   cin >> T; n   while (T--) { n     solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1E5; n vector<int> vf, vz; n int main() { n   ios_base::sync_with_stdio(false); n   int n, k, p; n   cin >> n >> k >> p; n   for (int i = 0; i < n; i++) { n     int x; n     cin >> x; n     if (x % 2 == 0) n       vz.push_back(x); n     else n       vf.push_back(x); n   } n   int tf = vf.size(), tz = vz.size(); n   if (tf < (k - p) || p > tz + (tf - (k - p)) / 2 || (k - p) % 2 != tf % 2) n     cout <<  NO ; n   else { n     if ((tf - (k - p)) % 2 == 1) { n       cout <<  NO ; n     } n     if ((tf - (k - p)) % 2 == 0) { n       cout <<  YES  << endl; n       if (p == 0) { n         for (int i = 0; i < k - 1; i++) cout <<  1   << vf[i] << endl; n         cout << tf - (k - 1) + tz; n         for (int i = k - 1; i < tf; i++) cout <<     << vf[i]; n         for (int i = 0; i < tz; i++) cout <<     << vz[i]; n         cout << endl; n       } else if (tz >= p) { n         for (int i = 0; i < p - 1; i++) cout <<  1   << vz[i] << endl; n         cout << tz - p + 1 + tf - (k - p); n         for (int i = p - 1; i < tz; i++) cout <<     << vz[i]; n         for (int i = 0; i < tf - (k - p); i++) cout <<     << vf[i]; n         cout << endl; n         for (int i = tf - (k - p); i < tf; i++) cout <<  1   << vf[i] << endl; n       } else { n         for (int i = 0; i < tz; i++) cout <<  1   << vz[i] << endl; n         for (int i = 0; i < (p - tz) - 1; i++) n           cout <<  2   << vf[i * 2] <<     << vf[i * 2 + 1] << endl; n         cout << tf - (p - tz - 1) * 2 - (k - p); n         for (int i = (p - tz - 1) * 2; i < tf - (k - p); i++) n           cout <<     << vf[i]; n         cout << endl; n         for (int i = tf - (k - p); i < tf; i++) cout <<  1   << vf[i] << endl; n       } n     } n   } n } n


#include <bits/stdc++.h> n int dx[] = {-1, -1, -1, 0, 1, 1, 1, 0}; n int dy[] = {-1, 0, 1, 1, 1, 0, -1, -1}; n using namespace std; n long long power(long long a, long long p, long long M) { n   long long r = 1; n   a = a % M; n   while (p > 0) { n     if (p % 2) r = (r * a) % M; n     a = (a * a) % M; n     p /= 2; n   } n   return r; n } n long long gcd(long long a, long long b) { n   if (b == 0) return a; n   return gcd(b, a % b); n } n long long n, m; n set<long long> e[100005]; n stack<long long> cycle, temp; n long long vis[505]; n bool found; n void findCycle(long long x) { n   temp.push(x); n   vis[x] = 1; n   for (auto y : e[x]) { n     if (vis[y] == 0) n       findCycle(y); n     else if (vis[y] == 1) { n       if (found) return; n       cycle = temp; n       cycle.push(y); n       found = 1; n       return; n     } n   } n   if (!temp.empty()) temp.pop(); n   vis[x] = 2; n } n void fc(long long x) { n   vis[x] = 1; n   for (auto y : e[x]) { n     if (vis[y] == 0) n       fc(y); n     else if (vis[y] == 1) { n       found = 1; n       return; n     } n   } n   vis[x] = 2; n } n void solve() { n   cin >> n >> m; n   for (long long i = 0; i < m; i++) { n     long long a, b; n     scanf( %lld%lld , &a, &b); n     e[a].insert(b); n   } n   for (long long i = 1; i < n + 1; i++) { n     if (!vis[i]) { n       findCycle(i); n       if (found) break; n     } n   } n   if (!found) { n     cout <<  YES n ; n     return; n   } n   vector<long long> c; n   for (long long i = 0; i < 505; i++) vis[i] = 0; n   while (!cycle.empty()) { n     c.push_back(cycle.top()); n     if (vis[cycle.top()]) { n       break; n     } n     vis[cycle.top()] = 1; n     cycle.pop(); n   } n   reverse(c.begin(), c.end()); n   for (long long i = 0; i < c.size() - 1; i++) { n     found = 0; n     e[c[i]].erase(c[i + 1]); n     for (long long j = 0; j < 505; j++) vis[j] = 0; n     for (long long j = 1; j < n + 1; j++) { n       if (!vis[j]) fc(j); n       if (found) { n         break; n       } n     } n     if (!found) { n       cout <<  YES n ; n       return; n     } n     e[c[i]].insert(c[i + 1]); n   } n   cout <<  NO n ; n } n int main() { n   solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long A, B, N; n long long L, T, M; n bool ok(long long R) { n   long long mx = A + (R - 1) * B; n   long long mn = A + (L - 1) * B; n   long long D = R - L; n   if (mx > T) return false; n   return (mn * (D + 1) + B * D * (D + 1) / 2) <= T * M; n } n int main(void) { n   cin >> A >> B >> N; n   while (N--) { n     cin >> L >> T >> M; n     long long R = L; n     long long mn = A + (L - 1) * B; n     if (mn > T) { n       R = -1; n     } else { n       long long XR = 1LL << 22; n       while (XR > R) { n         long long m = (R + XR) / 2; n         if (!ok(m)) n           XR = m; n         else n           R = m + 1; n       } n       R -= 1; n     } n     cout << R <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int INF = 1e9 + 7, mod = 1e9 + 9, pw = 31; n const double eps = 1e-11; n long long binpow(long long a, long long b) { n   long long rec = 1; n   while (b) { n     if (b % 2 == 1) { n       rec *= a; n       rec %= INF; n     } n     a *= a; n     a %= INF; n     b /= 2; n   } n   return rec; n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(NULL); n   int n; n   cin >> n; n   long long x = binpow(3, n * 3) - binpow(7, n); n   if (x < 0) x += INF; n   cout << x; n   return 0; n } n


#include <bits/stdc++.h> n const long long INF64 = 1e18; n using namespace std; n const long long N = 2e5 + 6; n using namespace std; n vector<pair<long long, long long>> pf; n int main() { n   long long n, ans = INF64; n   cin >> n; n   vector<long long> cost(n, 0); n   vector<string> s; n   for (int i = 0; i < n; i++) cin >> cost[i]; n   for (int i = 0; i < n; i++) { n     string st; n     cin >> st; n     s.push_back(st); n   } n   vector<vector<long long>> dp(n, vector<long long>(2, INF64)); n   dp[n - 1][0] = 0; n   dp[n - 1][1] = cost[n - 1]; n   for (int i = n - 2; i >= 0; i--) { n     string currStr(s[i]), currrevStr(s[i]); n     string prevStr(s[i + 1]), prevrevStr(s[i + 1]); n     reverse(currrevStr.begin(), currrevStr.end()); n     reverse(prevrevStr.begin(), prevrevStr.end()); n     if (currStr.compare(prevStr) <= 0) dp[i][0] = min(dp[i][0], dp[i + 1][0]); n     if (currStr.compare(prevrevStr) <= 0) n       dp[i][0] = min(dp[i][0], dp[i + 1][1]); n     if (currrevStr.compare(prevStr) <= 0) n       dp[i][1] = min(dp[i][1], dp[i + 1][0] + cost[i]); n     if (currrevStr.compare(prevrevStr) <= 0) n       dp[i][1] = min(dp[i][1], dp[i + 1][1] + cost[i]); n   } n   ans = min(ans, min(dp[0][0], dp[0][1])); n   if (ans == INF64) n     cout << -1; n   else n     cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long a[100005], n, c, mn[100005], dp[200005], sum; n int main() { n   ios_base::sync_with_stdio(0); n   cin >> n >> c; n   for (int i = 0; i < n; sum += a[i++]) cin >> a[i]; n   deque<long long> v; n   for (int i = n; i--;) { n     while (v.size() && v.back() >= i + c) v.pop_back(); n     while (v.size() && a[i] <= a[v.front()]) v.pop_front(); n     v.push_front(i); n     if (i <= n - c) dp[i] = max(dp[i + 1], a[v.back()] + dp[i + c]); n   } n   cout << sum - dp[0]; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T1, typename T2> n inline T1 max(T1 a, T2 b) { n   return a < b ? b : a; n } n template <typename T1, typename T2> n inline T1 min(T1 a, T2 b) { n   return a < b ? a : b; n } n const char lf =   n ; n namespace ae86 { n const int bufl = 1 << 15; n char buf[bufl], *s = buf, *t = buf; n inline int fetch() { n   if (s == t) { n     t = (s = buf) + fread(buf, 1, bufl, stdin); n     if (s == t) return EOF; n   } n   return *s++; n } n inline int ty() { n   int a = 0; n   int b = 1, c = fetch(); n   while (!isdigit(c)) b ^= c ==  - , c = fetch(); n   while (isdigit(c)) a = a * 10 + c - 48, c = fetch(); n   return b ? a : -a; n } n }  // namespace ae86 n using ae86::ty; n const int _ = 100007, __ = _ << 1; n const long long linf = 0x3f3f3f3f3f3f3f3f; n int to[__], ne[__], he[_] = {0}, ecnt = 1; n inline void adde(int a, int b) { n   to[++ecnt] = b, ne[ecnt] = he[a], he[a] = ecnt; n } n inline void addde(int a, int b) { adde(a, b), adde(b, a); } n int n, m, qn; n int fa[_] = {0}, dep[_] = {0}, sz[_] = {0}, mxch[_] = {0}; n void dfs(int x, int ff) { n   fa[x] = ff, dep[x] = dep[ff] + 1, sz[x] = 1, mxch[x] = 0; n   for (int i = he[x]; i; i = ne[i]) { n     int b = to[i]; n     if (b == ff) continue; n     dfs(b, x), sz[x] += sz[b]; n     if (sz[mxch[x]] < sz[b]) mxch[x] = b; n   } n } n int dfn[_] = {0}, rdfn[_] = {0}, ndf[_] = {0}, top[_] = {0}, dcnt = 0; n void dfs2(int x, int tp) { n   dfn[x] = ++dcnt, rdfn[dcnt] = x, top[x] = tp; n   if (mxch[x]) dfs2(mxch[x], tp); n   for (int i = he[x]; i; i = ne[i]) { n     int b = to[i]; n     if (b == fa[x] || b == mxch[x]) continue; n     dfs2(b, b); n   } n   ndf[x] = dcnt; n } n vector<int> hav[_]; n struct cxk { n   int x; n   long long v; n   cxk(int x_ = 0, long long v_ = linf) { x = x_, v = v_; } n   friend bool operator<(cxk a, cxk b) { n     if (a.v != b.v) return a.v < b.v; n     return a.x < b.x; n   } n }; n const int _t = _ << 2; n cxk t[_t]; n long long tadd[_t]; n void up(int x) { t[x] = min(t[(x << 1)], t[(x << 1 | 1)]); } n void putadd(int x, long long d) { t[x].v += d, tadd[x] += d; } n void dwn(int x) { n   if (tadd[x]) n     putadd((x << 1), tadd[x]), putadd((x << 1 | 1), tadd[x]), tadd[x] = 0; n } n void plant(int x, int l, int r) { n   tadd[x] = 0; n   if (l == r) { n     t[x] = hav[l].empty() ? cxk() : cxk(l, hav[l].back()); n     return; n   } n   plant((x << 1), l, ((l + r) >> 1)), n       plant((x << 1 | 1), ((l + r) >> 1) + 1, r), up(x); n } n void change(int x, int l, int r, int tl, int tr, long long dlt) { n   if (tl <= l && r <= tr) { n     putadd(x, dlt); n     return; n   } n   dwn(x); n   if (tl <= ((l + r) >> 1)) change((x << 1), l, ((l + r) >> 1), tl, tr, dlt); n   if (tr > ((l + r) >> 1)) n     change((x << 1 | 1), ((l + r) >> 1) + 1, r, tl, tr, dlt); n   up(x); n } n void dating(int x, int l, int r, int tar) { n   if (l == r) { n     if (!hav[l].empty()) hav[l].pop_back(); n     t[x] = hav[l].empty() ? cxk() : cxk(l, hav[l].back() + tadd[x]); n     return; n   } n   dwn(x); n   if (tar <= ((l + r) >> 1)) n     dating((x << 1), l, ((l + r) >> 1), tar); n   else n     dating((x << 1 | 1), ((l + r) >> 1) + 1, r, tar); n   up(x); n } n cxk finder(int x, int l, int r, int tl, int tr) { n   if (tl <= l && r <= tr) return t[x]; n   dwn(x); n   if (tr <= ((l + r) >> 1)) return finder((x << 1), l, ((l + r) >> 1), tl, tr); n   if (tl > ((l + r) >> 1)) n     return finder((x << 1 | 1), ((l + r) >> 1) + 1, r, tl, tr); n   return min(finder((x << 1), l, ((l + r) >> 1), tl, tr), n              finder((x << 1 | 1), ((l + r) >> 1) + 1, r, tl, tr)); n } n cxk founder(int a, int b) { n   cxk ans; n   while (top[a] != top[b]) { n     if (dep[top[a]] < dep[top[b]]) swap(a, b); n     ans = min(ans, finder(1, 1, n, dfn[top[a]], dfn[a])), a = fa[top[a]]; n   } n   if (dep[a] > dep[b]) swap(a, b); n   return min(ans, finder(1, 1, n, dfn[a], dfn[b])); n } n int main() { n   ios::sync_with_stdio(0), cout.tie(nullptr); n   n = ty(), m = ty(), qn = ty(); n   for (int i = 1, a, b; i < n; i++) a = ty(), b = ty(), addde(a, b); n   dfs(1, 0), dfs2(1, 1); n   for (int i = 1; i <= m; i++) hav[dfn[ty()]].emplace_back(i); n   for (int i = 1; i <= n; i++) n     sort(hav[i].begin(), hav[i].end(), greater<int>()); n   plant(1, 1, n); n   for (int qq = 1; qq <= qn; qq++) { n     int op = ty(); n     if (op == 1) { n       int a = ty(), b = ty(), lim = ty(); n       vector<int> ans; n       while (lim--) { n         int ret = founder(a, b).x; n         if (ret <= 0) break; n         ans.emplace_back(hav[ret].back()), dating(1, 1, n, ret); n       } n       cout << ans.size(); n       for (auto i : ans) cout <<     << i; n       cout << lf; n     } n     if (op == 2) { n       int a = ty(), b = ty(); n       change(1, 1, n, dfn[a], ndf[a], b); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAX = 1010; n int n; n int a[MAX], c[MAX]; n void init() { cin >> n; } n void process() { n   memset(a, 63, sizeof(a)); n   vector<pair<int, int> > ques; n   ques.push_back(make_pair(1, n)); n   while (!ques.empty()) { n     vector<pair<int, int> > newQues; n     for (int i = 0; i < ques.size(); i++) { n       if (ques[i].first != ques[i].second) { n         int l = ques[i].first, r = ques[i].second, mid = (l + r) / 2; n         newQues.push_back(make_pair(l, mid)); n         newQues.push_back(make_pair(mid + 1, r)); n       } n     } n     vector<int> b; n     for (int i = 0; i < newQues.size(); i += 2) { n       for (int j = newQues[i].first; j <= newQues[i].second; j++) n         b.push_back(j); n     } n     if (b.size() > 0) { n       cout << b.size() <<   n ; n       for (int i = 0; i < b.size(); i++) cout << b[i] <<    ; n       cout <<   n ; n       fflush(stdout); n       for (int i = 1; i <= n; i++) cin >> c[i]; n       for (int i = 1; i <= n; i++) { n         bool check = true; n         for (int j = 0; j < b.size(); j++) { n           if (b[j] == i) { n             check = false; n             break; n           } n         } n         if (check) a[i] = min(a[i], c[i]); n       } n     } n     b.resize(0); n     for (int i = 1; i < newQues.size(); i += 2) { n       for (int j = newQues[i].first; j <= newQues[i].second; j++) n         b.push_back(j); n     } n     if (b.size() > 0) { n       cout << b.size() <<   n ; n       for (int i = 0; i < b.size(); i++) cout << b[i] <<    ; n       cout <<   n ; n       fflush(stdout); n       for (int i = 1; i <= n; i++) cin >> c[i]; n       for (int i = 1; i <= n; i++) { n         bool check = true; n         for (int j = 0; j < b.size(); j++) { n           if (b[j] == i) { n             check = false; n             break; n           } n         } n         if (check) a[i] = min(a[i], c[i]); n       } n     } n     ques = newQues; n   } n   cout << -1 <<   n ; n   for (int i = 1; i <= n; i++) cout << a[i] <<    ; n   cout <<   n ; n   fflush(stdout); n } n int main() { n   ios_base::sync_with_stdio(false); n   init(); n   process(); n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 300; n long long orign[MAXN], str[2 * MAXN]; n int radius[2 * MAXN]; n int n, m; n int mp[MAXN][MAXN]; n int num[MAXN][26]; n long long hashv[MAXN]; n long long base[30]; n int siz[MAXN]; n bool check(int x, int y) { n   for (int i = 0; i < 26; i++) { n     if (num[x][i] != num[y][i]) return false; n   } n   return true; n } n void Manacher(int len, int mark = 0) { n   for (int i = 0; i < len; i++) { n     str[2 * i + 1] = orign[i]; n     str[2 * i] = mark; n   } n   str[2 * len] = mark; n   len = 2 * len + 1; n   int max_right = 0, pos = 0; n   for (int i = 0; i < len; i++) { n     if (i < max_right) n       radius[i] = min(radius[2 * pos - i], max_right - i); n     else n       radius[i] = 1; n     while (i - radius[i] >= 0 && i + radius[i] < len && n            str[i - radius[i]] > -1 && str[i + radius[i]] > -1 && n            check(str[i - radius[i]], str[i + radius[i]])) { n       radius[i]++; n     } n     if (i + radius[i] >= max_right) { n       max_right = i + radius[i]; n       pos = i; n     } n   } n } n int main() { n   std::ios::sync_with_stdio(false); n   cin.tie(0); n   cin >> n >> m; n   base[0] = 1; n   for (int i = 1; i <= 26; i++) base[i] = base[i - 1] * 2; n   for (int i = 1; i <= n; i++) { n     for (int j = 1; j <= m; j++) { n       char ch; n       cin >> ch; n       mp[i][j] = ch -  a ; n     } n   } n   long long ans = 0; n   for (int l = 1; l <= m; l++) { n     memset(num, 0, sizeof(num)); n     memset(hashv, 0, sizeof(hashv)); n     memset(siz, 0, sizeof(siz)); n     for (int r = l; r <= m; r++) { n       for (int i = 1; i <= n; i++) { n         int id = mp[i][r]; n         num[i][id]++; n         hashv[i] += base[id]; n         if (num[i][id] & 1) n           siz[i]++; n         else n           siz[i]--; n         if (siz[i] > 1) n           orign[i - 1] = -i; n         else n           orign[i - 1] = i; n       } n       Manacher(n); n       for (int i = 0; i < 2 * n + 1; i++) { n         if (str[i] > -1) ans += radius[i] / 2; n       } n     } n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 10000010; n int p[N], num, last[N]; n bool flag[N]; n int cnt[N], n, m; n void make_prime() { n   for (int i = 2; i < N; ++i) n     if (!flag[i]) { n       p[num++] = i; n       last[i] = i; n       for (int j = i + i; j < N; j += i) flag[j] = true, last[j] = i; n     } n } n int main() { n   make_prime(); n   scanf( %d , &n); n   for (int i = 0, x; i < n; ++i) { n     scanf( %d , &x); n     int s = -1; n     while (x != 1) { n       if (last[x] != s) cnt[last[x]]++; n       s = last[x]; n       x /= last[x]; n     } n   } n   for (int i = 1; i < N; ++i) cnt[i] += cnt[i - 1]; n   scanf( %d , &m); n   for (int i = 0, x, y; i < m; ++i) { n     scanf( %d %d , &x, &y); n     if (x >= N) n       puts( 0 ); n     else { n       y = min(y, N - 1); n       printf( %d n , cnt[y] - cnt[x - 1]); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int INF = 0x3f3f3f3f; n const int N = 2005; n const int M = 730; n int Head[M * 10], Next[M * 100], vet[M * 100]; n int a[N], b[N], d[M * 10], vis[15], p[M][5], dis[M], Dis[M], P[10], len[M], n     U[M][12], D[M][12]; n int n, cnt, edgenum; n pair<int, int> c[M]; n queue<int> q; n inline int F(int x, int y) { return (x - 1) * cnt + y; } n inline void addedge(int u, int v) { n   edgenum++; n   vet[edgenum] = v; n   Next[edgenum] = Head[u]; n   Head[u] = edgenum; n } n int main() { n   scanf( %d , &n); n   int ans = INF; n   for (int i = 1; i <= n; i++) scanf( %d%d , &a[i], &b[i]); n   len[cnt = 1] = 0; n   for (int i = 1; i <= 9; i++) { n     len[++cnt] = 1; n     p[cnt][1] = i; n     for (int j = i; j <= 9; j++) { n       len[++cnt] = 2; n       p[cnt][1] = i, p[cnt][2] = j; n       for (int k = j; k <= 9; k++) { n         len[++cnt] = 3; n         p[cnt][1] = i, p[cnt][2] = j, p[cnt][3] = k; n         for (int t = k; t <= 9; t++) n           len[++cnt] = 4, p[cnt][1] = i, p[cnt][2] = j, p[cnt][3] = k, n           p[cnt][4] = t; n       } n     } n   } n   for (int i = 2; i <= cnt; i++) { n     memset(vis, 0, sizeof(vis)); n     for (int j = 1; j <= len[i]; j++) vis[p[i][j]] = 1; n     for (int j = 1; j <= 9; j++) { n       if (!vis[j]) continue; n       int res = j, cur = 0; n       for (int k = 1; k <= len[i]; k++) n         if (p[i][k] == res) n           res = -1; n         else n           P[++cur] = p[i][k]; n       for (int k = 1; k <= cnt; k++) { n         if (len[k] != cur) continue; n         bool flag = 1; n         for (int t = 1; t <= cur; t++) flag &= (P[t] == p[k][t]); n         if (flag) { n           D[i][j] = k; n           U[k][j] = i; n           break; n         } n       } n     } n   } n   memset(dis, 0x3f, sizeof(dis)); n   dis[1] = a[1] - 1; n   for (int j = 1; j < 9; j++) n     for (int k = 1; k <= cnt; k++) n       addedge(F(j, k), F(j + 1, k)), addedge(F(j + 1, k), F(j, k)); n   for (int j = 1; j <= 9; j++) n     for (int k = 1; k <= cnt; k++) n       if (D[k][j]) addedge(F(j, k), F(j, D[k][j])); n   for (int i = 1; i <= n; i++) { n     memset(Dis, 0x3f, sizeof(Dis)); n     for (int j = 1; j <= cnt; j++) n       if (U[j][b[i]]) Dis[U[j][b[i]]] = min(Dis[U[j][b[i]]], dis[j] + 1); n     for (int j = 1; j <= cnt; j++) dis[j] = Dis[j]; n     memset(d, 0x3f, sizeof(d)); n     for (int j = 1; j <= cnt; j++) n       d[F(a[i], j)] = dis[j], c[j] = make_pair(dis[j], j); n     sort(c + 1, c + 1 + cnt); n     for (int j = 1; j <= cnt; j++) n       if (c[j].first != INF) q.push(F(a[i], c[j].second)); n     while (!q.empty()) { n       int u = q.front(); n       q.pop(); n       for (int e = Head[u]; e; e = Next[e]) { n         int v = vet[e]; n         if (d[v] > d[u] + 1) d[v] = d[u] + 1, q.push(v); n       } n     } n     if (i == n) n       for (int j = 1; j <= 9; j++) ans = min(ans, d[F(j, 1)]); n     else n       for (int j = 1; j <= cnt; j++) dis[j] = d[F(a[i + 1], j)]; n   } n   printf( %d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 3003 * 6006; n const int qwq = 303030; n const int inf = 0x3f3f3f3f; n int n, m, k; n int f[N], fa[N], st[123], cnt; n int ans; n int ff[] = {1, 1, 1, 0, -1, -1, -1, 0}; n int gg[] = {1, 0, -1, -1, -1, 0, 1, 1}; n inline int read() { n   int sum = 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     sum = sum * 10 + c -  0 ; n     c = getchar(); n   } n   return sum * f; n } n inline int dian(int x, int y) { n   if (y == 0) y = 2 * m; n   if (y == 2 * m + 1) y = 1; n   return (x - 1) * (m << 1) + y; n } n int find(int u) { n   while (fa[u] != u) u = fa[u]; n   return u; n } n int Splay(int u) { return fa[u] == u ? u : fa[u] = Splay(fa[u]); } n int main() { n   int x, y; n   n = read(); n   m = read(); n   k = read(); n   if (m == 1) { n     cout << 0; n     return 0; n   } n   for (int i = 1; i <= n * m * 2; i++) fa[i] = i; n   while (k--) { n     cnt = 0; n     x = read(); n     y = read(); n     for (int i = 0; i <= 8; i++) { n       if (x + ff[i] < 1 || x + ff[i] > n) continue; n       int u = dian(x + ff[i], y + gg[i]); n       if (!f[u]) continue; n       int aa = find(u); n       int bb = find(dian(x, y)); n       if (aa == bb) continue; n       st[++cnt] = bb; n       fa[bb] = aa; n     } n     y += m; n     for (int i = 0; i < 8; i++) { n       if (x + ff[i] < 1 || x + ff[i] > n) continue; n       int u = dian(x + ff[i], y + gg[i]); n       if (!f[u]) continue; n       int aa = find(u); n       int bb = find(dian(x, y)); n       if (aa == bb) continue; n       st[++cnt] = bb; n       fa[bb] = aa; n     } n     y -= m; n     if (find(dian(x, y)) != find(dian(x, y + m))) { n       ans++; n       for (int i = 0; i < 8; i++) { n         if (x + ff[i] < 1 || x + ff[i] > n) continue; n         Splay(dian(x + ff[i], y + gg[i])); n       } n       y += m; n       for (int i = 0; i < 8; i++) { n         if (x + ff[i] < 1 || x + ff[i] > n) continue; n         Splay(dian(x + ff[i], y + gg[i])); n       } n       y -= m; n       f[dian(x, y)] = f[dian(x, y + m)] = 1; n     } else { n       for (int i = 1; i <= cnt; i++) fa[st[i]] = st[i]; n     } n   } n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, sum; n int main() { n   cin >> n; n   while (n != 0) { n     if (n % 8 == 1) sum++; n     n /= 8; n   } n   cout << sum; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int INF = 1 << 29; n const int MOD = 1000000007; n bool isPowerOfTwo(long long int x) { return x && (!(x & (x - 1))); } n void fastio() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n } n const int dx[] = {1, 0, -1, 0, 1, 1, -1, -1}; n const int dy[] = {0, -1, 0, 1, 1, -1, -1, 1}; n int main() { n   fastio(); n   int t = 1; n   while (t--) { n     int n; n     cin >> n; n     string s; n     cin >> s; n     int ans = 0; n     for (int i = 0; i < (s.length()); i++) { n       int x = int(s[i]) - 48; n       if (x % 2 == 0) ans += (i + 1); n     } n     cout << ans <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n map<long long, int> mp; n int n; n int main() { n   scanf( %d , &n); n   int t; n   long long s = 0; n   for (int i = 1; i <= n; i++) { n     scanf( %d , &t); n     s += t; n     mp[s]++; n   } n   int ans = 0; n   map<long long, int>::iterator it; n   for (it = mp.begin(); it != mp.end(); it++) { n     ans = max(ans, it->second); n   } n   printf( %d n , n - ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<int> edge[100005]; n int v[100005]; n bool cycle = false; n int mx = -1, last; n void dfs(int u, int dis = 0) { n   v[u] = 1; n   if (dis > mx) { n     mx = dis; n     last = u; n   } n   for (int i = 0; i <= (int)((int)edge[u].size() - 1); i++) { n     int first = edge[u][i]; n     if (!v[first]) dfs(first, dis + 1); n   } n } n int main() { n   int T, n, m, a, b; n   scanf( %d , &n); n   scanf( %d , &m); n   for (int i = 1; i <= (int)(m); i++) { n     scanf( %d%d , &a, &b); n     edge[a].push_back(b); n     edge[b].push_back(a); n   } n   dfs(1); n   memset(v, 0, sizeof(v)); n   mx = -1; n   dfs(last); n   printf( %d , mx); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int INF = 1 << 29; n const double EPS = 1e-10; n vector<int> G[50010]; n vector<int> chil[50010]; n int cnt[50010][510]; n void dfs(int pos, int par) { n   for (int i = 0; i < (int)G[pos].size(); i++) { n     if (G[pos][i] != par) { n       chil[pos].push_back(G[pos][i]); n       dfs(G[pos][i], pos); n     } n   } n } n void init(int n) { n   memset(cnt, 0, sizeof(cnt)); n   dfs(0, -1); n } n void calc(int v, int k, long long &sum) { n   for (int i = 0; i < (int)chil[v].size(); i++) { n     int c = chil[v][i]; n     calc(c, k, sum); n     for (int j = 0; j < k; j++) { n       sum += (long long)cnt[c][j] * cnt[v][k - j - 1]; n     } n     for (int j = 0; j < k; j++) { n       cnt[v][j + 1] += cnt[c][j]; n     } n   } n   cnt[v][0] = 1; n   sum += (long long)cnt[v][0] * cnt[v][k]; n } n int main() { n   int n, k, a, b; n   cin >> n >> k; n   for (int i = 0; i < n - 1; i++) { n     cin >> a >> b; n     a--, b--; n     G[a].push_back(b); n     G[b].push_back(a); n   } n   init(n); n   long long res = 0; n   calc(0, k, res); n   cout << res << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct P { n   int x, y; n   bool operator<(const P &p) { n     if (x != p.x) n       return x < p.x; n     else n       return y < p.y; n   } n }; n int main() { n   int T; n   cin >> T; n   for (int t = 0; t < T; ++t) { n     int n; n     cin >> n; n     int bsz = n / 3; n     vector<int> buf(bsz); n     for (int i = 0; i < n / 3; ++i) { n       cout <<  ?   << 3 * i + 1 <<     << 3 * i + 2 <<     << 3 * i + 3 << endl; n       cout.flush(); n       cin >> buf[i]; n       if (buf[i] == -1) { n         break; n       } n     } n     int ind0, ind1; n     for (int i = 0; i < bsz; ++i) { n       int indl = i; n       int indr = (i + 1) % bsz; n       if (buf[indl] != buf[indr]) { n         vector<int> tbuf(6); n         tbuf[0] = buf[indl]; n         for (int j = 1; j <= 5; ++j) { n           int v1 = (3 * indl + j) % n + 1; n           int v2 = (3 * indl + j + 1) % n + 1; n           int v3 = (3 * indl + j + 2) % n + 1; n           cout <<  ?   << v1 <<     << v2 <<     << v3 << endl; n           cout.flush(); n           cin >> tbuf[j]; n           if (tbuf[j] == -1) { n             break; n           } n           if (tbuf[j - 1] != tbuf[j]) { n             if (tbuf[j - 1] == 0) { n               ind0 = (3 * indl + j - 1) % n + 1; n               ind1 = v3; n             } else { n               ind1 = (3 * indl + j - 1) % n + 1; n               ind0 = v3; n             } n             break; n           } n         } n         break; n       } n     } n     vector<int> mapping(n); n     mapping[ind1 - 1] = 1; n     int tmp; n     for (int i = 0; i < bsz; ++i) { n       int v1 = 3 * i + 1; n       int v2 = 3 * i + 2; n       int v3 = 3 * i + 3; n       if (buf[i] == 0) { n         if (v1 == ind1) { n           cout <<  ?   << ind1 <<     << ind0 <<     << v2 << endl; n         } else if (v2 == ind1) { n           cout <<  ?   << ind1 <<     << v1 <<     << ind0 << endl; n         } else { n           cout <<  ?   << ind1 <<     << v1 <<     << v2 << endl; n         } n         cout.flush(); n         cin >> tmp; n         if (tmp == 0) { n           mapping[v1 - 1] = 0; n           mapping[v2 - 1] = 0; n           if (v3 == ind0) { n             tmp = 0; n           } else if (v3 == ind1) { n             tmp = 1; n           } else { n             cout <<  ?   << ind0 <<     << ind1 <<     << v3 << endl; n             cout.flush(); n             cin >> tmp; n           } n           mapping[v3 - 1] = tmp; n         } else { n           mapping[v3 - 1] = 0; n           if (ind0 == v1) { n             tmp = 0; n           } else if (ind1 == v1) { n             tmp = 1; n           } else { n             cout <<  ?   << ind0 <<     << ind1 <<     << v1 << endl; n             cout.flush(); n             cin >> tmp; n           } n           mapping[v1 - 1] = tmp; n           mapping[v2 - 1] = 1 - tmp; n         } n       } else { n         if (v1 == ind0) { n           cout <<  ?   << ind0 <<     << ind1 <<     << v2 << endl; n         } else if (v2 == ind0) { n           cout <<  ?   << ind0 <<     << v1 <<     << ind1 << endl; n         } else { n           cout <<  ?   << ind0 <<     << v1 <<     << v2 << endl; n         } n         cout.flush(); n         cin >> tmp; n         if (tmp == 1) { n           mapping[v1 - 1] = 1; n           mapping[v2 - 1] = 1; n           if (v3 == ind0) { n             tmp = 0; n           } else if (v3 == ind1) { n             tmp = 1; n           } else { n             cout <<  ?   << ind0 <<     << ind1 <<     << v3 << endl; n             cout.flush(); n             cin >> tmp; n           } n           mapping[v3 - 1] = tmp; n         } else { n           mapping[v3 - 1] = 1; n           if (v1 == ind0) { n             tmp = 0; n           } else if (v1 == ind1) { n             tmp = 1; n           } else { n             cout <<  ?   << ind0 <<     << ind1 <<     << v1 << endl; n             cout.flush(); n             cin >> tmp; n           } n           mapping[v1 - 1] = tmp; n           mapping[v2 - 1] = 1 - tmp; n         } n       } n     } n     vector<int> fake; n     for (int i = 0; i < n; ++i) { n       if (mapping[i] == 0) { n         fake.push_back(i + 1); n       } n     } n     cout <<  !   << fake.size(); n     for (int i = 0; i < fake.size(); ++i) { n       cout <<     << fake[i]; n     } n     cout << endl; n     cout.flush(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, k, a, b, c, d; n   scanf( %d%d%d%d%d%d , &n, &k, &a, &b, &c, &d); n   if (n <= 4 || k < n + 1) { n     puts( -1 ); n     return 0; n   } n   printf( %d %d , a, c); n   for (int i = 1; i <= n; i++) { n     if (i != a && i != b && i != c && i != d) printf(  %d , i); n   } n   printf(  %d %d n , d, b); n   printf( %d %d , c, a); n   for (int i = 1; i <= n; i++) { n     if (i != a && i != b && i != c && i != d) printf(  %d , i); n   } n   printf(  %d %d n , b, d); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int mod = 1e9 + 7; n const int N = 2e5 + 10; n const long long inf = 1e18; n const int maxm = 1005; n vector<vector<char>> matrix(maxm, vector<char>(maxm)); n struct event { n   int x, y, size; n }; n int dx[] = {0, 0, 1, -1}; n int dy[] = {-1, 1, 0, 0}; n class even { n  public: n   int a, b, c; n }; n class evenl { n  public: n   int a, b, c, ind; n }; n struct myComp { n   constexpr bool operator()(evenl f, evenl s) const noexcept { n     int z = f.b - f.a; n     int z2 = s.b - s.a; n     z -= f.c; n     z2 -= s.c; n     return z < z2; n   } n }; n long long poww(long long a, long long b, long long m) { n   if (b == 0) return 1ll; n   if (b % 2 == 1) return a * poww(a, b - 1, m) % m; n   long long rs = poww(a, b / 2, m); n   rs = rs * rs % m; n   return rs; n } n long long fact[N]; n long long ncr(long long n, long long r) { n   long long num = fact[n]; n   long long deno = fact[n - r] * fact[r] % mod; n   deno = poww(deno, mod - 2, mod); n   num = num * deno; n   num = num % mod; n   return num; n } n long long convert(long long a, long long b) { n   string s; n   while (a > 0) { n     long long p = a % b; n     s += p +  0 ; n     a = a / b; n   } n   reverse(s.begin(), s.end()); n   long long res = stoi(s); n   return res; n } n long long make(long long a, long long b, long long c) { n   while (a < b) a *= c; n   return a % b; n } n long long frog_jump(long long pos, long long n) { n   while (pos % 2 == 0) { n     pos = pos + (n - pos / 2); n   } n   return (pos + 1) / 2; n } n void solve() { n   long long n, q; n   cin >> n >> q; n   for (int i = 0; i < q; i++) { n     long long x; n     cin >> x; n     if (x % 2 != 0) { n       cout << (x + 1) / 2 <<   n ; n       continue; n     } n     long long z = frog_jump(x, n); n     cout << z <<   n ; n   } n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   solve(); n } n


#include <bits/stdc++.h> n double aa[12]; n int sign[12]; n int compare(const void *a, const void *b) { n   int i = *(int *)a; n   int j = *(int *)b; n   if (sign[i] != sign[j]) return sign[j] - sign[i]; n   if (sign[i] == 0) return i - j; n   return aa[i] == aa[j] ? i - j : aa[i] > aa[j] ? -sign[i] : sign[i]; n } n void power(int i, double x, double y, double z) { n   if (x == 1) n     sign[i] = 0; n   else if (x > 1) { n     sign[i] = 1; n     aa[i] = z * log(y) + log(log(x)); n   } else { n     sign[i] = -1; n     aa[i] = z * log(y) + log(-log(x)); n   } n } n void power_(int i, double x, double yz) { n   if (x == 1) n     sign[i] = 0; n   else if (x > 1) { n     sign[i] = 1; n     aa[i] = log(yz) + log(log(x)); n   } else { n     sign[i] = -1; n     aa[i] = log(yz) + log(-log(x)); n   } n } n int main() { n   static char ss[12][16] = { x^y^z ,  x^z^y ,  (x^y)^z ,  (x^z)^y , n                              y^x^z ,  y^z^x ,  (y^x)^z ,  (y^z)^x , n                              z^x^y ,  z^y^x ,  (z^x)^y ,  (z^y)^x }; n   static int ii[12]; n   double x, y, z; n   int i; n   scanf( %lf%lf%lf , &x, &y, &z); n   power(0, x, y, z); n   power(1, x, z, y); n   power_(2, x, y * z); n   power_(3, x, y * z); n   power(4, y, x, z); n   power(5, y, z, x); n   power_(6, y, x * z); n   power_(7, y, x * z); n   power(8, z, x, y); n   power(9, z, y, x); n   power_(10, z, x * y); n   power_(11, z, x * y); n   for (i = 0; i < 12; i++) ii[i] = i; n   qsort(ii, 12, sizeof *ii, compare); n   printf( %s n , ss[ii[0]]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(nullptr); n   int n, m; n   cin >> n >> m; n   vector<int> a(n); n   for (int i = 0; i < n; ++i) { n     cin >> a[i]; n   } n   vector<int> rightInc(n), leftInc(n); n   int end = 0, cur_count = 1; n   for (int i = 0; i < n; ++i) { n     if (end < i) { n       end = i, cur_count = 1; n     } n     while (end + 1 < n && a[end + 1] >= a[end]) { n       end++, cur_count++; n     } n     rightInc[i] = end - i + 1, cur_count--; n   } n   reverse(a.begin(), a.end()); n   end = 0, cur_count = 1; n   for (int i = 0; i < n; ++i) { n     if (end < i) { n       end = i, cur_count = 1; n     } n     while (end + 1 < n && a[end + 1] >= a[end]) { n       end++, cur_count++; n     } n     leftInc[i] = end - i + 1, cur_count--; n   } n   reverse(leftInc.begin(), leftInc.end()); n   while (m--) { n     int l, r; n     cin >> l >> r; n     --l, --r; n     if (rightInc[l] + leftInc[r] >= r - l + 1) n       cout <<  Yes n ; n     else n       cout <<  No n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 3e6 + 7; n const int INF = 0x7fffffff; n int n, m; n struct DATA { n   int strid; n   int index; n }; n vector<DATA> vec; n string str[maxn]; n char ans[maxn]; n bool cmp(DATA a, DATA b) { return a.index < b.index; } n int main() { n   while (cin >> n) { n     memset(ans, 0, sizeof(ans)); n     vec.clear(); n     for (int i = 0; i < n; i++) { n       int num; n       cin >> str[i]; n       scanf( %d , &num); n       for (int j = 0; j < num; j++) { n         int k; n         scanf( %d , &k); n         k--; n         DATA t; n         t.strid = i; n         t.index = k; n         vec.push_back(t); n       } n     } n     int last = -1; n     sort(vec.begin(), vec.end(), cmp); n     for (int i = 0; i < vec.size(); i++) { n       for (int j = max(last + 1, vec[i].index); n            j < vec[i].index + str[vec[i].strid].size(); j++) { n         ans[j] = str[vec[i].strid][j - vec[i].index]; n         last = max(last, j); n       } n     } n     for (int i = 0; i <= last; i++) { n       if (ans[i] != 0) n         printf( %c , ans[i]); n       else { n         printf( a ); n       } n     } n     puts(  ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   scanf( %d , &n); n   long long int arr[100]; n   for (int i = 0; i < n; i++) cin >> arr[i]; n   long long int cnt[5]; n   long long int p[5]; n   for (int i = 0; i < 5; i++) cin >> p[i]; n   long long int min = 1000000009; n   for (int i = 0; i < 5; i++) n     if (p[i] < min) min = p[i]; n   for (int i = 0; i < 5; i++) cnt[i] = 0; n   long long int ps = 0; n   for (int i = 0; i < n; i++) { n     ps = ps + arr[i]; n     while (ps >= min) { n       long long int pp; n       for (int j = 4; j >= 0; j--) { n         if (ps < min) break; n         pp = ps / p[j]; n         ps = ps - pp * p[j]; n         cnt[j] += pp; n       } n     } n   } n   for (int i = 0; i < 5; i++) cout << cnt[i] <<    ; n   cout <<   n  << ps << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long t, n, m, cap, lis[200500], high, low; n long long f(long long x) { n   long long res = 0, cnt = 0, buf = 0; n   for (int i = (0); i <= ((int)(n)-1); i++) n     if (lis[i] <= x) { n       if (!cnt) { n         res += buf; n         buf = 0; n       } n       cnt++; n       buf += lis[i]; n       res += lis[i]; n       if (cnt == m) { n         cnt = 0; n       } n     } n   return res; n } n long long g(long long x) { n   long long res = 0, task = 0, cnt = 0, buf = 0; n   for (int i = (0); i <= ((int)(n)-1); i++) n     if (lis[i] <= x) { n       if (!cnt) { n         res += buf; n         buf = 0; n       } n       if (res + lis[i] > cap) break; n       res += lis[i]; n       cnt++; n       buf += lis[i]; n       task++; n       if (cnt == m) { n         cnt = 0; n       } n     } n   return task; n } n int main() { n   ios::sync_with_stdio(0); n   cin >> t; n   while (t--) { n     cin >> n >> m >> cap; n     for (int i = (0); i <= ((int)(n)-1); i++) cin >> lis[i]; n     high = cap, low = 1; n     while (high > low) { n       long long chs = (high + low) / 2; n       if (f(chs) > cap) n         high = chs; n       else n         low = chs + 1; n     } n     long long gh = g(high), gh1 = g(high - 1); n     if (gh > gh1 || high == 1) n       cout << gh <<     << high << endl; n     else n       cout << gh1 <<     << high - 1 << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n long long int dp[2010][2010]; n long long int a[2010]; n long long int b[2010]; n int main() { n   int n; n   cin >> n; n   for (int i = 1; i <= n; i++) { n     cin >> a[i] >> b[i]; n   } n   for (int i = 0; i <= 2000; i++) { n     for (int j = 0; j <= 2000; j++) { n       dp[i][j] = 10000000000000000; n     } n   } n   dp[0][0] = 0; n   for (int i = 1; i <= n; i++) { n     long long int o = a[i] + 1; n     for (int j = 0; j <= n; j++) { n       o = a[i] + 1; n       if (j == 0) { n         dp[i][j] = 0; n       } n       if (o >= j) { n         o = j; n       } n       if (j >= o) { n         dp[i][j] = min(b[i] + dp[i - 1][j - (o)], dp[i][j]); n       } n       dp[i][j] = min(dp[i][j], dp[i - 1][j]); n     } n   } n   cout << dp[n][n] << endl; n } n


#include <bits/stdc++.h> n const long long int MOD = 1e9 + 7; n const long long int INF = 1011111111; n const long long int LLINF = 1000111000111000111LL; n const long double EPS = 1e-10; n const long double PI = 3.14159265358979323; n using namespace std; n long long int power(int x, int n) { n   if (n == 0) n     return 1; n   else if (n % 2 == 0) n     return power((x * x), n / 2); n   else n     return x * power((x * x), (n - 1) / 2); n } n long long int dp[5005][5005]; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   long long int n, m, i, j, k; n   cin >> n >> k; n   vector<long long int> v(n); n   for (i = 0; i < n; i++) cin >> v[i]; n   sort(v.begin(), v.end()); n   vector<pair<pair<long long int, long long int>, long long int> > check; n   vector<long long int>::iterator it; n   for (i = 0; i < n; i++) { n     long long int idx; n     it = upper_bound(v.begin(), v.end(), v[i] + 5); n     if (it == v.end()) n       idx = n - 1; n     else { n       idx = it - v.begin(); n       idx--; n     } n     check.push_back({{i, idx}, idx - i + 1}); n   } n   long long int ans = 0; n   for (i = 0; i < n; i++) { n     for (j = 0; j <= k; j++) { n       dp[i + 1][j] = max(dp[i + 1][j], dp[i][j]); n       if (j + 1 <= k) n         dp[i + check[i].second][j + 1] = n             max(dp[i + check[i].second][j + 1], dp[i][j] + check[i].second); n     } n   } n   cout << dp[n][k]; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 2e5 + 5, oo = 0x3f3f3f3f, mod = 1e9 + 7; n int a[N], n, tree[N], srt[N], ans[N]; n void update(int a, int val) { n   while (a <= n) { n     tree[a] = max(val, tree[a]); n     a += a & -a; n   } n } n int query(int a) { n   int mx = 0; n   while (a > 0) { n     mx = max(tree[a], mx); n     a -= a & -a; n   } n   return mx; n } n int main() { n   scanf( %d , &n); n   for (int i = 1; i <= n; i++) { n     scanf( %d , a + i); n     srt[i] = a[i]; n   } n   sort(srt + 1, srt + n + 1); n   for (int i = n; i >= 1; i--) { n     int idx = lower_bound(srt + 1, srt + n + 1, a[i]) - srt; n     update(idx, i); n     ans[i] = query(idx - 1); n     if (ans[i] == 0) n       ans[i]--; n     else n       ans[i] -= i + 1; n   } n   for (int i = 1; i <= n; i++) printf( %d  , ans[i]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 2e5 + 9; n int n, maxx; n struct p { n   int lei, num, x; n } a[maxn]; n int da[maxn], b[maxn], vis[maxn]; n int main() { n   cin >> n; n   for (int i = 1; i <= n; i++) cin >> b[i]; n   int w; n   cin >> w; n   for (int i = 1; i <= w; i++) { n     cin >> a[i].lei; n     if (a[i].lei == 1) { n       cin >> a[i].num >> a[i].x; n       vis[a[i].num] = i; n       b[a[i].num] = a[i].x; n     } else { n       cin >> a[i].x; n       maxx = max(maxx, a[i].x); n     } n   } n   for (int i = w; i >= 1; i--) { n     if (a[i].lei == 1) n       da[i] = da[i + 1]; n     else n       da[i] = max(da[i + 1], a[i].x); n   } n   for (int i = 1; i <= n; i++) { n     if (vis[i] == 0) n       cout << max(b[i], maxx) <<    ; n     else { n       int xu = vis[i]; n       cout << max(b[i], da[xu]) <<    ; n     } n   } n } n


#include <bits/stdc++.h> n const int INF = 0x3f3f3f3f; n const int N = 579; n const long long P = 1000000007; n int n, b[N]; n long long d[N][N]; n long long solve(int l, int r) { n   if (l == r) return 1; n   static bool used[N][N]; n   static long long ans[N][N]; n   if (used[l][r]) return ans[l][r]; n   used[l][r] = true; n   ++l; n   ++r; n   d[l][0] = 1; n   for (int i = 0; i < r - l; ++i) { n     if (d[l][i + 1] != -1) continue; n     int c = l + i; n     d[l][i + 1] = 0; n     for (int j = 0; j <= i; ++j) n       if (b[j + l] < b[c + 1]) (d[l][i + 1] += d[l][j] * solve(j + l, c)) %= P; n   } n   long long res = 0; n   for (int j = 0; j <= r - l - 1; ++j) n     (res += d[l][j] * solve(j + l, r - 1)) %= P; n   --l; n   --r; n   return ans[l][r] = res; n } n int main() { n   memset(d, -1, sizeof(d)); n   std::cin >> n; n   for (int i = 0; i < n; ++i) std::cin >> b[i]; n   if (b[0] == 1) n     std::cout << solve(0, n - 1) <<   n ; n   else n     std::cout << 0 <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int a[4]; n   for (int i = 0; i < 4; i++) cin >> a[i]; n   int sumtotal = a[0] + a[1] + a[2] + a[3]; n   if (sumtotal % 2 != 0) { n     cout <<  NO ; n     return 0; n   } n   for (int i = 0; i < 4; i++) { n     if (a[i] == sumtotal - a[i]) { n       cout <<  YES ; n       return 0; n     } else n       for (int j = 0; j < 4; j++) { n         if (i != j) n           if ((a[i] + a[j]) == (sumtotal - a[i] - a[j])) { n             cout <<  YES ; n             return 0; n           } n       } n   } n   cout <<  NO ; n } n


#include <bits/stdc++.h> n using namespace std; n vector<pair<int, double>> ans[52]; n int main() { n   int n, w, m; n   cin >> n >> w >> m; n   double s = (n * w) / (m * 1.0), c = w, b = 1, k = 0; n   for (int i = 0; i < m; i++) { n     double ne = s; n     while (1) { n       if (ne + 10e-7 > c) { n         ans[i].push_back(make_pair(b, c)); n         ne -= c; n         c = w; n         b++; n         if (++k > 2) { n           cout <<  NO ; n           return 0; n         } n         k = 0; n         if (abs(ne) < 10e-7) break; n       } else { n         ans[i].push_back(make_pair(b, ne)); n         c -= ne; n         k++; n         break; n       } n     } n   } n   cout <<  YES n ; n   for (int i = 0; i < m; i++) { n     for (auto it : ans[i]) n       cout << fixed << setprecision(0) << it.first <<     << setprecision(6) n            << it.second <<    ; n     cout <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void quickly_quickly_kro() { 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   ; n   map<long long, long long> M, N, I; n   for (long long i = 0; i < n; i++) { n     if (v[i] != 0) n       M[v[i]]++; n     else n       I[i]++; n   } n   for (long long i = 1; i < n + 1; i++) { n     if (M[i] == 0) { n       N[i]++; n     } n   } n   for (auto k : I) { n     if ((N.find(k.first + 1) != N.end())) { n       auto it = N.find(k.first + 1); n       if (it != N.begin()) n         it--; n       else n         it++; n       v[k.first] = it->first; n       N.erase(it); n     } n   } n   for (long long i = 0; i < n; i++) { n     if (v[i] == 0) { n       auto it = N.begin(); n       pair<long long, long long> p = *it; n       v[i] = p.first; n       N.erase(it); n     } n   } n   for (long long i = 0; i < n; i++) cout << v[i] <<    ; n } n signed main() { n   std::ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   long long t; n   t = 1; n   while (t--) { n     quickly_quickly_kro(); n     cout <<   n ; n     ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct HashMap { n   vector<pair<int, int> > g[1000007]; n   void ins(int x, int y) { n     int id = ((long long)x * 1000000007 + y) % 1000007; n     g[id].push_back(make_pair(x, y)); n   } n   bool get(int x, int y) { n     int id = ((long long)x * 1000000007 + y) % 1000007; n     for (int i = 0; i < g[id].size(); i++) n       if (g[id][i].first == x && g[id][i].second == y) return true; n     return false; n   } n }; n int n; n HashMap mp; n vector<int> row[100011]; n vector<int> rateB; n int main() { n   scanf( %d , &n); n   for (int i = 1; i <= n; i++) { n     int x, y; n     scanf( %d%d , &x, &y); n     row[y].push_back(x); n     mp.ins(x, y); n   } n   int di = (int)sqrt(n); n   long long int ans = 0; n   for (int a = 0; a <= 100000; a++) { n     if (row[a].size() != 0) { n       sort(row[a].begin(), row[a].end()); n       if (row[a].size() <= di) { n         for (int i = 0; i < row[a].size(); i++) { n           for (int j = i + 1; j < row[a].size(); j++) { n             int x1 = row[a][i]; n             int x2 = row[a][j]; n             int dis = x2 - x1; n             if (mp.get(x1, a + dis) && mp.get(x2, a + dis)) ans++; n             if (a - dis >= 0 && row[a - dis].size() > di && n                 (mp.get(x1, a - dis) && mp.get(x2, a - dis))) n               ans++; n           } n         } n       } else n         rateB.push_back(a); n     } n   } n   for (int i = 0; i < rateB.size(); i++) { n     for (int j = i + 1; j < rateB.size(); j++) { n       int a = rateB[i]; n       int b = rateB[j]; n       int u = 0, p = 0; n       int dis = b - a; n       while (u < row[a].size() && p < row[b].size()) { n         if (row[a][u] < row[b][p]) n           u++; n         else if (row[a][u] > row[b][p]) n           p++; n         else { n           int x1 = row[a][u]; n           if (mp.get(x1 + dis, a) && mp.get(x1 + dis, b)) ans++; n           u++; n           p++; n         } n       } n     } n   } n   printf( %I64d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int inf = (int)1.01e9; n const double eps = 1e-9; n const int maxn = (int)2e5 + 10; n set<int> S; n int main() { n   int n, k, a; n   scanf( %d%d%d , &n, &k, &a); n   int cur = (n + 1) / (a + 1); n   assert(cur >= k); n   int m; n   scanf( %d , &m); n   for (int i = 0; (i) < (m); ++i) { n     int x; n     scanf( %d , &x); n     set<int>::iterator it = S.lower_bound(x); n     int left = 0; n     int right = n + 1; n     if (it != S.end()) { n       right = *it; n     } n     if (it != S.begin()) { n       --it; n       left = *it; n     } n     cur -= (right - left) / (a + 1); n     cur += (x - left) / (a + 1); n     cur += (right - x) / (a + 1); n     if (cur < k) { n       printf( %d n , i + 1); n       return 0; n     } n     S.insert(x); n   } n   printf( -1 n ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n bool has[200009]; n int left[200009]; n int main() { n   int Q; n   cin >> Q; n   for (int q = 0; q < Q; q++) { n     int N; n     cin >> N; n     vector<int> v; n     for (int i = 0; i < N; i++) { n       int a; n       cin >> a; n       v.push_back(a); n     } n     sort(v.begin(), v.end()); n     reverse(v.begin(), v.end()); n     int res = v[0]; n     vector<int> w; n     vector<int> t; n     for (int i = 1; i < v.size(); i++) n       if (v[i] != v[i - 1]) { n         if (v[0] % v[i] != 0) n           w.push_back(v[i]); n         else n           t.push_back(v[i]); n       } n     if (w.size() > 0) { n       res += w[0]; n       for (int i = 1; i < w.size(); i++) n         if (w[0] % w[i] != 0) { n           res += w[i]; n           break; n         } n     } n     if (t.size() >= 3 && t[0] + t[1] + t[2] > res) { n       if (t[0] % t[1] != 0 && t[1] % t[2] != 0 && t[0] % t[2] != 0) n         res = t[0] + t[1] + t[2]; n     } n     if (t.size() >= 4 && t[0] + t[1] + t[3] > res) { n       if (t[0] % t[1] != 0 && t[1] % t[3] != 0 && t[0] % t[3] != 0) n         res = t[0] + t[1] + t[3]; n     } n     cout << res <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct Segment { n   int l, r, x, ls, rs, fa; n } seg[2000100]; n int n, k, p[100100], cnt, pnt, b[2000100], w, ans[100100], res; n int Dfs(int &id, int l, int r) { n   id = ++cnt; n   seg[id].l = l, seg[id].r = r; n   seg[id].x = 1; n   if (r - l == 1) return 1; n   int mid = (l + r) >> 1; n   seg[id].x += Dfs(seg[id].ls, l, mid); n   seg[id].x += Dfs(seg[id].rs, mid, r); n   return seg[id].x; n } n void Vis(int x) { n   if (!x) return; n   b[x] = 1; n   Vis(seg[x].ls); n   Vis(seg[x].rs); n } n bool cmp(int x, int y) { return seg[x].l < seg[y].l; } n void Solve(int x) { n   if (!x) return; n   if (b[x]) { n     for (int i = seg[x].l; i < seg[x].r; i++) ans[i] = res++; n     return; n   } n   Solve(seg[x].rs); n   Solve(seg[x].ls); n } n int main() { n   scanf( %d%d , &n, &k); n   Dfs(w, 0, n); n   k = seg[1].x - k; n   for (int i = 1; i <= cnt; i++) n     if (!b[i]) { n       if (k >= seg[i].x - 1 && seg[i].x > 1) { n         k -= seg[i].x - 1; n         p[++pnt] = i; n         Vis(i); n         if (!k) break; n       } n     } n   if (k) n     printf( -1 ); n   else { n     res = 1; n     Solve(1); n     for (int i = n - 1; i >= 0; i--) ans[i] = ans[i] ? ans[i] : res++; n     for (int i = 0; i < n; i++) printf( %d  , ans[i]); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n string s[1100]; n int vis[1100]; n int num[100]; n int main() { n   ios::sync_with_stdio(false); n   int n, len, k; n   cin >> n >> len >> k; n   string st; n   cin >> st; n   for (int i = 0; i < st.size(); i++) { n     num[st[i] -  a ]++; n   } n   int l = 0, r = 25; n   for (int i = k + 1; i <= n; i++) { n     for (int j = 1; j <= len; j++) { n       while (!num[r] && r > l) r--; n       s[i] += (char)(r +  a ); n       num[r]--; n     } n   } n   int now = 0; n   int pos = 0; n   while (1) { n     for (int i = 1; i <= k; i++) { n       if (vis[i]) continue; n       if (pos >= 1 && i != k && (int)(s[i][pos - 1] -  a ) < now) { n         for (int j = pos + 1; j <= len; j++) { n           while (!num[r] && r > l) r--; n           s[i] += (char)(r +  a ); n           num[r]--; n         } n         vis[i] = 1; n         continue; n       } n       while (!num[l] && r > l) l++; n       s[i] += (char)(l +  a ); n       num[l]--; n       if (i == k) { n         now = l; n       } n     } n     pos++; n     if (pos == len) break; n   } n   sort(s + 1, s + 1 + n); n   for (int i = 1; i <= n; i++) cout << s[i] << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int T, n, m, nw, a[110][110], id[110][110], pos[110], p[110]; n char ch; n bool cmp(int i, int j) { return a[m][i] < a[m][j]; } n queue<int> q; n void find(int x) { n   p[x] = 0; n   int y = id[x][pos[x]]; n   pos[x]--; n   if (!p[y]) n     p[y] = x, p[x] = y; n   else if (a[y][x] > a[y][p[y]]) { n     p[p[y]] = 0, q.push(p[y]); n     p[y] = x, p[x] = y; n   } n   if (!p[x]) q.push(x); n } n void work() { n   for (int i = 1; i <= 2 * n; i++) { n     for (int j = 1; j <= n; j++) id[i][j] = (i <= n) * n + j; n     m = i; n     sort(id[i] + 1, id[i] + n + 1, cmp); n     p[i] = 0; n   } n   for (int i = 1; i <= n; i++) pos[i] = n, q.push(i); n   while (!q.empty()) { n     int u = q.front(); n     q.pop(); n     find(u); n   } n   for (int i = 1; i <= n; i++) assert(p[i]); n   for (int i = 1; i <= n; i++) n     for (int j = 1; j <= n; j++) n       assert(!(a[p[i]][i] < a[p[i]][j] && a[j][p[i]] > a[j][p[j]])); n } n void solve() { n   bool falg = 0; n   scanf( %d , &n); n   memset(a, 0, sizeof(a)), memset(id, 0, sizeof(id)); n   for (int i = 1; i <= n; i++) n     for (int j = n + 1; j <= 2 * n; j++) n       scanf( %d , &a[i][j]), a[j][i] = -a[i][j]; n   puts( B ), fflush(stdout); n   scanf(  %c%d , &ch, &nw); n   if (ch ==  I ) falg ^= 1; n   if (nw > n) falg ^= 1; n   if (falg) n     for (int i = 1; i <= 2 * n; i++) n       for (int j = 1; j <= 2 * n; j++) a[i][j] = -a[i][j]; n   work(); n   printf( %d n , p[nw]), fflush(stdout); n   while (~scanf( %d , &nw)) { n     if (nw == -1) return; n     printf( %d n , p[nw]), fflush(stdout); n   } n } n int main() { n   scanf( %d , &T); n   while (T--) solve(); n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 2e5 + 9; n vector<int> getBorder(const string str) { n   int n = str.size(); n   vector<int> border(n, -1); n   for (int i = 1, j = -1; i < n; i++) { n     while (j >= 0 && str[i] != str[j + 1]) { n       j = border[j]; n     } n     if (str[i] == str[j + 1]) j++; n     border[i] = j; n   } n   return border; n } n int matchPattern(string txt, string pat, const vector<int> border) { n   int freq = 0; n   for (int i = 0, j = -1; i < (int)txt.size(); i++) { n     while (j >= 0 && txt[i] != pat[j + 1]) { n       j = border[j]; n     } n     if (txt[i] == pat[j + 1]) { n       j++; n     } n     if (j + 1 == (int)pat.size()) { n       freq++; n       j = border[j]; n     } n   } n   return freq; n } n string cut(string s, int p, bool pref = true) { n   if (!pref) { n     reverse(s.begin(), s.end()); n   } n   while ((int)s.size() >= p) s.pop_back(); n   if (!pref) { n     reverse(s.begin(), s.end()); n   } n   return s; n } n const int ms = 210; n string pref[ms], suf[ms]; n long long trans[ms]; n int bk = 14; n long long getFreq(int n, string pat, long long k) { n   std::vector<long long> f(n + 1, 0); n   if (pat ==  0 ) f[0]++; n   if (pat ==  1 ) f[1]++; n   auto border = getBorder(pat); n   for (int i = 2; i <= n; i++) { n     long long cur = trans[i - 2]; n     if (i < bk + 4) { n       string curString = cut(suf[i - 2], (int)pat.size(), false) + n                          cut(pref[i - 1], (int)pat.size()); n       cur = matchPattern(curString, pat, border); n     } n     f[i] = f[i - 1] + f[i - 2] + cur; n     if (f[i] > k) return k + 1; n     trans[i] = cur; n   } n   return min(f[n], k + 1); n } n int main() { n   pref[0] = suf[0] =  0 ; n   pref[1] = suf[1] =  1 ; n   for (int i = 2; i < ms; i++) { n     pref[i] = cut(pref[i - 2] + pref[i - 1], ms); n     suf[i] = cut(suf[i - 2] + suf[i - 1], ms, false); n   } n   int n, m; n   long long k; n   cin >> n >> k >> m; n   if (n == 0) { n     cout <<  0 n ; n     return 0; n   } n   if (n == 1) { n     cout <<  1 n ; n     return 0; n   } n   string ans; n   k--; n   while (k >= 0 && (int)ans.size() < m) { n     if (ans.size() > 0 && n         suf[n].substr(suf[n].size() - ans.size(), ans.size()) == ans) { n       k--; n     } n     if (k < 0) break; n     ans +=  0 ; n     long long got = getFreq(n, ans, k); n     if (got <= k) { n       ans.back() =  1 ; n       k -= got; n     } n   } n   cout << ans << endl; n } n