#include <bits/stdc++.h> n using namespace std; n int is[1010]; n int main() { n   int n; n   scanf( %d , &n); n   int x = 0; n   for (int i = (0); i < (n); ++i) n     for (int j = (0); j < (n); ++j) { n       int v; n       scanf( %d , &v); n       if ((i == j) && (v == 1)) x ^= 1; n     } n   int q; n   scanf( %d , &q); n   for (int i = (0); i < (q); ++i) { n     int t; n     scanf( %d , &t); n     if (t < 3) { n       x ^= 1; n       int v; n       scanf( %d , &v); n     } else n       printf( %d , x); n   } n   printf(  n ); n } n


#include <bits/stdc++.h> n using namespace std; n const long long MAX = 840; n long long arr[9], W, ans; n long long dp[7890]; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cin >> W; n   for (int i = 1; i < 9; ++i) { n     cin >> arr[i]; n   } n   if (W > 840) { n     for (int i = 1; i < 9; ++i) { n       long long lef = max(arr[i] - 840 / i, (long long)0); n       long long g = (W - 840 - ans) / i; n       g = min(g, lef); n       if (g < 0) continue; n       ans += g * i; n       arr[i] -= g; n     } n   } n   dp[0] = 1; n   for (int i = 1; i < 9; ++i) { n     for (int j = 0; j < min(arr[i], (long long)840 / i); ++j) { n       for (int k = 6789; k >= i; --k) { n         if (dp[k - i]) dp[k] = 1; n       } n     } n   } n   for (int k = 6789;; --k) { n     if (dp[k] && ans + k <= W) { n       cout << ans + k <<   n ; n       return 0; n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, k; n   cin >> n >> k; n   int a[n * n]; n   for (int i = 1; i <= n * n; ++i) a[i - 1] = i; n   if (k == 1) { n     long long sum = 0; n     for (int i = 0; i < n; ++i) { n       sum += n * i + 1; n     } n     cout << sum << endl; n     for (int i = 0; i < n; ++i) { n       for (int j = 0; j < n - 1; ++j) { n         printf( %d  , a[i * n + j]); n       } n       printf( %d n , a[i * n + n - 1]); n     } n     return 0; n   } n   vector<int> v[n]; n   k--; n   for (int i = 0; i < k * n; ++i) { n     v[i % k].push_back(i + 1); n   } n   for (int i = k * n; i < n * n; ++i) { n     v[(i - k * n) % (n - k) + k].push_back(i + 1); n   } n   long long sum = 0; n   for (int i = 0; i < n; ++i) { n     sum += v[k][i]; n   } n   cout << sum << endl; n   for (int i = 0; i < n; ++i) { n     for (int j = 0; j < n - 1; ++j) { n       printf( %d  , v[j][i]); n     } n     printf( %d n , v[n - 1][i]); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int f[5010][5010], ans[5010][5010]; n int n, q; n int main() { n   scanf( %d , &n); n   for (int i = 1; i <= n; i++) { n     scanf( %d , &f[i][i]); n     ans[i][i] = f[i][i]; n   } n   for (int i = 1; i < n; i++) n     for (int j = 1; j <= n - i + 1; j++) n       f[j][j + i] = f[j][j + i - 1] ^ f[j + 1][j + i], n                ans[j][j + i] = n                    max(f[j][j + i], max(ans[j][j + i - 1], ans[j + 1][j + i])); n   scanf( %d , &q); n   for (int i = 1; i <= q; i++) { n     int l, r; n     scanf( %d%d , &l, &r); n     printf( %d n , ans[l][r]); n   } n } n


#include <bits/stdc++.h> n using namespace std; n int a[1003][1003]; n int cost[43][1003][1003]; n vector<vector<pair<int, int>>> COL(43); n int n, m, k; n int di1[4] = {0, 1, 0, -1}; n int di2[4] = {1, 0, -1, 0}; n void bfs(int col) { n   vector<bool> vis(43, 0); n   queue<pair<int, int>> q; n   for (int i = 0; i < COL[col].size(); i++) { n     cost[col][COL[col][i].first][COL[col][i].second] = 0; n     q.push({COL[col][i].first, COL[col][i].second}); n   } n   while (!q.empty()) { n     int x = q.front().first; n     int y = q.front().second; n     q.pop(); n     if (!vis[a[x][y]]) { n       vis[a[x][y]] = 1; n       for (int i = 0; i < COL[a[x][y]].size(); i++) { n         int nx = COL[a[x][y]][i].first; n         int ny = COL[a[x][y]][i].second; n         if (cost[col][nx][ny] == -1) { n           cost[col][nx][ny] = 1 + cost[col][x][y]; n           q.push({nx, ny}); n         } n       } n     } n     for (int i = 0; i < 4; i++) { n       int nx = x + di1[i]; n       int ny = y + di2[i]; n       if (nx >= 1 && nx <= n && ny >= 1 && ny <= m && cost[col][nx][ny] == -1) { n         cost[col][nx][ny] = 1 + cost[col][x][y]; n         q.push({nx, ny}); n       } n     } n   } n } n int main() { n   cin >> n >> m >> k; n   for (int i = 1; i <= n; i++) { n     for (int j = 1; j <= m; j++) { n       cin >> a[i][j]; n       COL[a[i][j]].push_back({i, j}); n     } n   } n   memset(cost, -1, sizeof(cost)); n   for (int i = 1; i <= k; i++) { n     bfs(i); n   } n   int q; n   cin >> q; n   while (q--) { n     int sx, sy, ex, ey; n     cin >> sx >> sy >> ex >> ey; n     int ans = abs(ex - sx) + abs(ey - sy); n     for (int i = 1; i <= k; i++) { n       ans = min(ans, 1 + cost[i][sx][sy] + cost[i][ex][ey]); n     } n     cout << ans <<   n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n long long i, i1, j, k, k1, t, n, m, res, flag[10], a, b, mt[110][110], s; n char c; n vector<array<long long, 6>> sl; n vector<array<long long, 3>> cl; n void mv(long long a, long long b, long long c, long long d, long long e, n         long long f) { n   sl.push_back({a, b, c, d, e, f}); n   mt[a][b] ^= 1; n   mt[c][d] ^= 1; n   mt[e][f] ^= 1; n } n void cl4(vector<long long> st) { n   long long i, l, x, s = 0; n   for (i = 1; i <= 4; i++) s += st[i]; n   if (s == 0) return; n   if (s == 3) { n     l = 0; n     cl.push_back({0, 0, 0}); n     x = cl.size(); n     x--; n     for (i = 1; i <= 4; i++) { n       if (st[i] == 1) { n         st[i] ^= 1; n         cl[x][l] = i; n         l++; n       } n     } n   } n   if (s == 2) { n     flag[1] = 0; n     l = 0; n     cl.push_back({0, 0, 0}); n     x = cl.size(); n     x--; n     for (i = 1; i <= 4; i++) { n       if (st[i] == 0 || (st[i] == 1 && flag[1] == 0)) { n         st[i] ^= 1; n         cl[x][l] = i; n         l++; n         if (st[i] == 0) flag[1] = 1; n       } n     } n     cl4(st); n   } n   if (s == 1) { n     flag[0] = 0; n     l = 0; n     cl.push_back({0, 0, 0}); n     x = cl.size(); n     x--; n     for (i = 1; i <= 4; i++) { n       if (st[i] == 1 || (st[i] == 0 && flag[0] < 2)) { n         st[i] ^= 1; n         cl[x][l] = i; n         l++; n         if (st[i] == 1) flag[0]++; n       } n     } n     cl4(st); n   } n   if (s == 4) { n     flag[1] = 0; n     l = 0; n     cl.push_back({0, 0, 0}); n     x = cl.size(); n     x--; n     for (i = 1; i <= 4; i++) { n       if (st[i] == 0 || (st[i] == 1 && flag[1] < 3)) { n         st[i] ^= 1; n         cl[x][l] = i; n         l++; n         if (st[i] == 0) flag[1]++; n       } n     } n     cl4(st); n   } n } n void init(vector<long long> st) { n   cl.clear(); n   cl4(st); n } n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cin >> t; n   while (t--) { n     cin >> n >> m; n     sl.clear(); n     for (i = 1; i <= n; i++) { n       for (j = 1; j <= m; j++) { n         cin >> c; n         if (c ==  0 ) n           mt[i][j] = 0; n         else n           mt[i][j] = 1; n       } n     } n     for (i = 1; i <= n - 2; i++) { n       for (j = 1; j <= m - 1; j++) { n         if (mt[i][j] == 1 && mt[i][j + 1] == 1) { n           mv(i, j, i, j + 1, i + 1, j); n         } else if (mt[i][j] == 1 && mt[i][j + 1] == 0) { n           mv(i, j, i + 1, j, i + 1, j + 1); n         } else if (mt[i][j] == 0 && mt[i][j + 1] == 1) { n           mv(i, j + 1, i + 1, j, i + 1, j + 1); n         } n       } n     } n     if (m % 2) { n       if (mt[n - 1][m] == 1 && mt[n][m] == 1) { n         mv(n - 1, m, n, m - 1, n, m); n       } else if (mt[n - 1][m] == 1 && mt[n][m] == 0) { n         mv(n - 1, m - 1, n - 1, m, n, m - 1); n       } else if (mt[n - 1][m] == 0 && mt[n][m] == 1) { n         mv(n - 1, m - 1, n, m - 1, n, m); n       } n     } n     for (j = 1; j <= m - 1; j += 2) { n       vector<long long> st; n       vector<array<long long, 2>> fn; n       st.push_back(0); n       st.push_back(mt[n - 1][j]); n       st.push_back(mt[n - 1][j + 1]); n       st.push_back(mt[n][j]); n       st.push_back(mt[n][j + 1]); n       init(st); n       for (auto u : cl) { n         fn.clear(); n         for (i = 0; i < 3; i++) { n           if (u[i] == 1) n             fn.push_back({n - 1, j}); n           else if (u[i] == 2) n             fn.push_back({n - 1, j + 1}); n           else if (u[i] == 3) n             fn.push_back({n, j}); n           else if (u[i] == 4) n             fn.push_back({n, j + 1}); n         } n         sl.push_back({0, 0, 0, 0, 0, 0}); n         for (i = 0; i < 3; i++) { n           s = sl.size(); n           s--; n           sl[s][2 * i] = fn[i][0]; n           sl[s][2 * i + 1] = fn[i][1]; n         } n       } n     } n     s = sl.size(); n     cout << s <<   n ; n     for (auto u : sl) { n       for (i = 0; i < 6; i++) cout << u[i] <<    ; n       cout <<   n ; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n; n int sz0[400000]; n vector<int> g[400000]; n vector<int> c0[400000]; n int centroid; n int sz[400000]; n vector<int> cs[400000]; n int ans[400000]; n void dfs0(int x) { n   sz0[x] = 1; n   for (int y : g[x]) { n     if (sz0[y] == 0) { n       c0[x].push_back(y); n       dfs0(y); n       sz0[x] += sz0[y]; n     } n   } n } n int findc(int x) { n   for (int y : c0[x]) { n     if (sz0[y] > n / 2) { n       return findc(y); n     } n   } n   return x; n } n void dfs1(int x) { n   sz[x] = 1; n   for (int y : g[x]) { n     if (sz[y] == 0) { n       cs[x].push_back(y); n       dfs1(y); n       sz[x] += sz[y]; n     } n   } n } n bool cmp(int a, int b) { return sz[a] > sz[b]; } n void dfs2(int x, int s, int m) { n   if (s - m <= n / 2) ans[x] = 1; n   for (int y : cs[x]) { n     int ss = s + sz[x] - sz[y]; n     dfs2(y, ss, m); n   } n } n int main() { n   scanf( %d , &n); n   for (int i = 0; i < n - 1; i++) { n     int a, b; n     scanf( %d%d , &a, &b); n     a--; n     b--; n     g[a].push_back(b); n     g[b].push_back(a); n   } n   dfs0(0); n   centroid = findc(0); n   dfs1(centroid); n   ans[centroid] = 1; n   if (cs[centroid].size() == 1) { n     assert(n <= 2); n     ans[0] = ans[1] = 1; n   } else { n     vector<int>& c = cs[centroid]; n     sort(c.begin(), c.end(), cmp); n     dfs2(c[0], sz[centroid] - sz[c[0]], sz[c[1]]); n     for (int i = 1; i < c.size(); i++) n       dfs2(c[i], sz[centroid] - sz[c[i]], sz[c[0]]); n     if (sz[c[0]] * 2 == n) n       for (int i = 0; i < n; i++) ans[i] = 1; n   } n   for (int i = 0; i < n; i++) printf( %d  , ans[i]); n   putchar(  n ); n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, k; n   cin >> n >> k; n   int a = min(k, 26) + 97; n   vector<char> b; n   for (int i = 97; i < a; i++) { n     b.push_back((char)i); n   } n   string key =   ; n   for (int i = 0; i < b.size(); i++) key += b[i]; n   int c = n - k, j = 0; n   while (true) { n     key += b[j]; n     j++; n     if (j > b.size() - 1) j = 0; n     c--; n     if (c <= 0) break; n   } n   if (key.size() > n) { n     key.erase(key.size() - 1, 1); n   } n   cout << key; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n string itoa(int x) { n   string s =   ; n   char c; n   if (x < 0) x *= -1, s +=  - ; n   while (x) { n     c = x % 10 + 48; n     s += c; n     x /= 10; n   } n   if (s[0] ==  - ) n     reverse(s.begin() + 1, s.end()); n   else n     reverse(s.begin(), s.end()); n   if (s ==   ) s +=  0 ; n   return s; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   priority_queue<int, vector<int>, greater<int> > pq; n   int n; n   cin >> n; n   string s, t, c; n   int x; n   vector<string> ans; n   while (n--) { n     cin >> s; n     if (s !=  removeMin ) { n       cin >> x; n       if (s ==  insert ) { n         pq.push(x); n         t =  insert  , c = itoa(x), t += c; n         ans.push_back(t); n       } else { n         while (!pq.empty() && pq.top() < x) { n           t =  removeMin ; n           ans.push_back(t); n           pq.pop(); n         } n         if (pq.empty() || pq.top() > x) n           t =  insert  , c = itoa(x), t += c, ans.push_back(t), pq.push(x); n         t =  getMin  , c = itoa(x), t += c; n         ans.push_back(t); n       } n     } else { n       if (pq.empty()) t =  insert 0 , ans.push_back(t), pq.push(0); n       t =  removeMin , ans.push_back(t), pq.pop(); n     } n   } n   cout << ans.size() <<   n ; n   for (int i = 0; i < ans.size(); i++) cout << ans[i] <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n int getValue(vector<int> &ai, int n, int sum) { n   int val = 0; n   int mval = ai[ai.size() - 1]; n   for (int i = 1; i <= mval; i++) { n     if (n * i >= sum) { n       val = i; n       break; n     } n   } n   return val; n } n int main() { n   int q; n   scanf( %d , &q); n   for (int i = 0; i < q; i++) { n     int n; n     vector<int> ai; n     scanf( %d , &n); n     int sum = 0; n     for (int j = 0; j < n; j++) { n       int temp; n       scanf( %d , &temp); n       ai.push_back(temp); n       sum += temp; n     } n     sort(ai.begin(), ai.end()); n     int ret = getValue(ai, n, sum); n     printf( %d n , ret); n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1000 * 100 + 3; n int n, a[N]; n void input() { n   cin >> n; n   for (int i = 0; i + 1 < n; i++) cin >> a[i]; n } n void solve() { n   sort(a, a + n - 1); n   for (int i = 0; i + 1 < n; i++) n     if (a[i] != i + 1) cout << i + 1 << endl, exit(0); n   cout << n << endl; n } n int main() { n   ios::sync_with_stdio(0), cin.tie(0), cout.tie(0); n   input(), solve(); n   return 0; n } n


#include <bits/stdc++.h> n #pragma GCC optimize( O2 ) n using namespace std; n inline long long input() { n   long long n; n   cin >> n; n   return n; n } n string base2(long long n) { n   string a =   ; n   while (n >= 2) { n     a += (char)(n % 2 +  0 ); n     n /= 2; n   } n   a += (char)(n +  0 ); n   reverse((a).begin(), (a).end()); n   return a; n } n const int MAXN = 1e6 + 10; n const int MOD = 1e9 + 7; n const int MOD2 = 998244353; n const long long INF = 8e18; n long long pw(long long a, long long b, long long mod) { n   return (!b ? 1 n              : (b & 1 ? (a * pw(a * a % mod, b / 2, mod)) % mod n                       : pw(a * a % mod, b / 2, mod))); n } n int n, m, mark[MAXN], king[MAXN], par[MAXN], sz[MAXN]; n vector<pair<int, int> > G[MAXN]; n vector<int> M; n long long dis[MAXN]; n pair<long long, pair<long long, long long> > E[MAXN]; n vector<pair<long long, pair<long long, long long> > > E2; n int GET(int x) { return ((x == par[x]) ? x : par[x] = GET(par[x])); } n bool unify(int x, int y) { n   int v = GET(x), u = GET(y); n   if (v == u) return 0; n   if (sz[u] > sz[v]) { n     swap(u, v); n   } n   par[u] = v; n   sz[v] += sz[u]; n   return 1; n } n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   ; n   cin >> n >> m; n   for (int i = 1; i <= n; i++) dis[i] = 1e18, sz[i] = 1, par[i] = i; n   for (int i = 1; i <= m; i++) { n     int a, b, c; n     cin >> a >> b >> c; n     G[a].push_back(make_pair(b, c)); n     G[b].push_back(make_pair(a, c)); n     E[i] = make_pair(c, make_pair(a, b)); n   } n   set<pair<long long, long long> > st; n   int k; n   cin >> k; n   for (int i = 1; i <= k; i++) { n     int a; n     cin >> a; n     M.push_back(a); n     king[a] = a; n     st.insert(make_pair(0, a)); n     dis[a] = 0; n   } n   while (!st.empty()) { n     pair<long long, long long> cu = *st.begin(); n     st.erase(st.begin()); n     int v = cu.second; n     if (mark[v]) { n       continue; n     } n     mark[v] = 1; n     for (auto y : G[v]) { n       long long u = y.first, cost = y.second; n       if (dis[u] > dis[v] + cost) { n         king[u] = king[v]; n         dis[u] = dis[v] + cost; n         st.insert(make_pair(dis[u], u)); n       } n     } n   } n   for (int i = 1; i <= m; i++) { n     long long v = E[i].second.first, u = E[i].second.second, cost = E[i].first; n     if (king[u] != king[v]) { n       E2.push_back( n           make_pair(dis[v] + dis[u] + cost, make_pair(king[u], king[v]))); n     } n   } n   sort((E2).begin(), (E2).end()); n   long long MST = 0; n   for (auto x : E2) { n     long long u = x.second.first, v = x.second.second, cost = x.first; n     if (unify(v, u)) { n       MST += cost; n     } n   } n   set<long long> second; n   for (auto x : M) { n     second.insert(GET(x)); n   } n   if ((int)second.size() > 1) { n     return cout << 1e18, 0; n   } n   cout << MST + dis[1]; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m, p[50]; n int ask(int second) { n   cout << second <<   n ; n   fflush(stdout); n   cin >> second; n   return second; n } n int main() { n   cin >> m >> n; n   for (int i = 0; i < n; i++) { n     p[i] = ask(1); n     if (p[i] == 0) { n       cout << 1 <<   n ; n       exit(0); n     } n   } n   int l = 1, r = m, t = 0; n   while (l <= r) { n     int m = (l + r) / 2; n     int tmp = ask(m) * p[t]; n     if (tmp == 0) { n       cout << tmp <<   n ; n       exit(0); n     } n     if (tmp == -1) n       r = m - 1; n     else n       l = m + 1; n     t = (t + 1) % n; n   } n } n


#include <bits/stdc++.h> n using namespace std; n string s; n long long int dp[510][501]; n long long int solve(long long int l, long long int r) { n   if (dp[l][r] != -1) { n     return dp[l][r]; n   } n   if (l > r) return 0; n   long long int a = 1 + solve(l + 1, r); n   for (long long int i = l + 1; i <= r; i++) { n     if (s[i] != s[l]) continue; n     a = min(a, solve(l + 1, i - 1) + solve(i, r)); n   } n   dp[l][r] = a; n   return a; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   long long int n, i; n   cin >> n; n   cin >> s; n   memset(dp, -1, sizeof dp); n   cout << solve(0, s.size() - 1); n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   int t; n   t = 1; n   while (t--) { n     long long n, a[1001]; n     cin >> n; n     for (int i = 0; i <= n - 1; i++) { n       cin >> a[i]; n     } n     sort(a, a + n); n     if (a[0] == 1) n       cout << -1; n     else n       cout << 1; n   } n   cerr <<  Time taken :   << (float)clock() / CLOCKS_PER_SEC <<   secs  << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const double pi = 3.141592653589793; n const int INF = 2000000000; n const int mod = 1000000007; n const int N = 100000000; n const int base = 10; n vector<int> read(string s) { n   vector<int> a; n   for (int i = (int)s.length(); i > 0; i -= 1) n     if (i < 1) n       a.push_back(atoi(s.substr(0, i).c_str())); n     else n       a.push_back(atoi(s.substr(i - 1, 1).c_str())); n   return a; n } n vector<int> add(vector<int> a, vector<int> b) { n   int carry = 0; n   for (size_t i = 0; i < max(a.size(), b.size()) || carry; ++i) { n     if (i == a.size()) a.push_back(0); n     a[i] += carry + (i < b.size() ? b[i] : 0); n     carry = a[i] >= base; n     if (carry) a[i] -= base; n   } n   return a; n } n vector<int> mul(vector<int> a, int b) { n   int carry = 0; n   for (size_t i = 0; i < a.size() || carry; ++i) { n     if (i == a.size()) a.push_back(0); n     long long cur = carry + a[i] * 1ll * b; n     a[i] = int(cur % base); n     carry = int(cur / base); n   } n   while (a.size() > 1 && a.back() == 0) a.pop_back(); n   return a; n } n vector<int> c; n vector<int> a[100005], b[100005]; n vector<bool> f; n vector<bool> f1; n int main() { n   vector<int> queue; n   int n, x, y, m; n   cin >> n >> m; n   for (int(i) = (0); (i) < (n); (i)++) { n     cin >> x; n     c.push_back(x); n   } n   for (int(i) = (0); (i) < (m); (i)++) { n     cin >> x >> y; n     x--; n     y--; n     a[x].push_back(y); n     b[y].push_back(x); n   } n   for (int(i) = (0); (i) < (c.size()); (i)++) n     if (c[i] == 1) { n       queue.push_back(i); n       f.push_back(true); n     } else { n       f.push_back(false); n     } n   for (int(i) = (0); (i) < (queue.size()); (i)++) { n     for (int(j) = (0); (j) < (a[queue[i]].size()); (j)++) { n       if (f[a[queue[i]][j]]) continue; n       queue.push_back(a[queue[i]][j]); n       f[a[queue[i]][j]] = true; n     } n   } n   queue.clear(); n   for (int(i) = (0); (i) < (c.size()); (i)++) n     if (c[i] == 2) { n       queue.push_back(i); n       f1.push_back(true); n     } else { n       f1.push_back(false); n     } n   for (int(i) = (0); (i) < (queue.size()); (i)++) { n     for (int(j) = (0); (j) < (b[queue[i]].size()); (j)++) { n       if (f1[b[queue[i]][j]]) continue; n       f1[b[queue[i]][j]] = true; n       if (c[b[queue[i]][j]] == 1) continue; n       queue.push_back(b[queue[i]][j]); n     } n   } n   for (int(i) = (0); (i) < (n); (i)++) n     if (f[i] && f1[i]) { n       cout << 1 << endl; n     } else { n       cout << 0 << endl; n     } n   return 0; n } n


#include <bits/stdc++.h> n #pragma GCC optimize( Ofast ) n #pragma GCC target(   n      sse,sse2,sse3,ssse3,sse4,sse4.1,sse4.2,popcnt,abm,mmx,avx,tune=native ) n using namespace std; n const int MOD = 1000000007; n const int UNDEF = -1; n const int INF = 1 << 30; n template <typename T> n inline bool chkmax(T &aa, T bb) { n   return aa < bb ? aa = bb, true : false; n } n template <typename T> n inline bool chkmin(T &aa, T bb) { n   return aa > bb ? aa = bb, true : false; n } n int rint() { n   int x; n   scanf( %d , &x); n   return x; n } n long long rlong() { n   long long x; n   scanf( %lld , &x); n   return x; n } n const int mn = (5e5) + 4; n long long c[mn]; n map<long long, vector<pair<int, int> > > sig2e; n map<int, vector<int> > h; n set<int> seen; n int cnt = 0; n void dfs(int x) { n   seen.insert(x); n   for (auto &y : h[x]) { n     if (seen.find(y) == seen.end()) { n       --cnt; n       dfs(y); n     } n   } n } n int ptwo[mn]; n int main() { n   int n = rint(), m = rint(), k = rint(); n   ptwo[0] = 1; n   for (int i = 1; i < mn; i++) { n     ptwo[i] = ptwo[i - 1] * 2; n     if (ptwo[i] >= MOD) ptwo[i] -= MOD; n   } n   for (int x = 1; x <= n; x++) c[x] = rlong(); n   for (int i = 0; i < m; i++) { n     int x = rint(), y = rint(); n     long long sig = c[x] ^ c[y]; n     sig2e[sig].push_back(make_pair(x, y)); n   } n   int rem = (ptwo[k] - (int)sig2e.size() + MOD) % MOD; n   int ans = (rem * (long long)ptwo[n]) % MOD; n   for (auto &sigve : sig2e) { n     auto &ve = sigve.second; n     h.clear(); n     seen.clear(); n     for (auto &e : ve) { n       int x = e.first, y = e.second; n       h[x].push_back(y); n       h[y].push_back(x); n     } n     cnt = n; n     for (auto &e : ve) { n       int x = e.first, y = e.second; n       if (seen.find(x) == seen.end()) dfs(x); n       if (seen.find(y) == seen.end()) dfs(y); n     } n     ans += ptwo[cnt]; n     if (ans >= MOD) ans -= MOD; n   } n   printf( %d n , ans); n } n


#include <bits/stdc++.h> n const long long mod = 1000000007; n const long long Inf = 1e9; n using namespace std; n long long a, b, d, k, t; n double f(long long x) { n   double res = 0; n   res = (double)x * (double)a + n         (double)(max(x / k - (x % k == 0 ? 1 : 0), (long long)0)) * (double)t + n         (double)(d - x) * (double)b; n   return res; n } n int main() { n   scanf( %I64d%I64d%I64d%I64d%I64d , &d, &k, &a, &b, &t); n   long long l = 1, r = d; n   double ans = 1e18; n   while (r - l > 2000000) { n     long long x1 = l + (r - l) / 3; n     long long x2 = r - (r - l) / 3; n     if (f(x1) > f(x2)) n       l = x1; n     else if (f(x1) < f(x2)) n       r = x2; n     else { n       l = x1; n       r = x2; n     } n   } n   for (long long i = l; i <= r; ++i) { n     ans = min(ans, f(i)); n   } n   printf( %0.lf , ans); n   return 0; n } n


#include <bits/stdc++.h> n static int solve(); n using ll = long long; n int main(int argc, char* argv[]) { n   int tc = 1; n   scanf( %d , &tc); n   while (tc--) solve(); n   return 0; n } n static constexpr int MOD = 998244353; n static int solve() { n   int n, x, y; n   scanf( %d %d %d , &n, &x, &y); n   int sum = x + y; n   int min_pos, max_pos; n   if (sum == n + n) { n     min_pos = n; n     max_pos = n; n   } else if (sum <= n) { n     min_pos = 1; n     max_pos = sum - 1; n   } else { n     min_pos = sum - n + 1; n     max_pos = n; n   } n   printf( %d %d n , min_pos, max_pos); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 100005; n int power(int x, int y, int mod) { n   int ret = 1; n   for (; y; y >>= 1) { n     if (y & 1) ret = 1LL * ret * x % mod; n     x = 1LL * x * x % mod; n   } n   return ret; n } n int n, m; n char s[N], t[N]; n vector<int> primes = { n     3,   5,   7,   11,  13,  17,  19,  23,  29,  31,  37,  41,  43,  47,  53, n     59,  61,  67,  71,  73,  79,  83,  89,  97,  101, 103, 107, 109, 113, 127, n     131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199, n     211, 223, 227, 229, 233, 239, 241, 251, 257, 263, 269, 271}; n int P[60][300]; n bool isPrime(int n) { n   for (int i = 2; i * i <= n; ++i) n     if (n % i == 0) return false; n   return true; n } n int main() { n   scanf( %d %d , &n, &m); n   scanf( %s , s); n   scanf( %s , t); n   for (int i = 0; i < primes.size(); ++i) { n     int p = primes[i]; n     for (int j = 0; j < n; ++j) { n       P[i][j % p] += s[j] - t[j]; n     } n   } n   for (int p = m;; ++p) { n     if (!isPrime(p)) continue; n     for (int i = 0; i < primes.size(); ++i) { n       int d = primes[i]; n       if (d >= p) break; n       if ((p - 1) % d > 0) continue; n       int r = -1; n       do { n         r = rand() % (p - 1) + 1; n       } while ((r = power(r, (p - 1) / d, p)) == 1); n       for (int k = 0, rr = 1; k < d; ++k) { n         rr = 1LL * rr * r % p; n         int sum = 0, mul = 1; n         for (int j = 0; j < d; ++j) { n           sum = (1LL * P[i][j] * mul + sum) % p; n           mul = (1LL * mul * rr) % p; n         } n         if (sum == 0) return 0 * printf( %d %d n , p, rr); n       } n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long n, m, l, r, p, span, t, lwr, upr; n long pr[200000]; n vector<long> vec[200000]; n long calc[200000]; n long long res, nmoves[200000]; n long long check[200000]; n long long temp; n long long answ; n long long gcd(long long a, long long b) { n   while (a && b) a > b ? a %= b : b %= a; n   return a + b; n } n long long solve1(long long x, long long lim) { n   long long res = 0; n   for (int mask = 0; mask < (1 << (vec[x].size())); mask++) { n     temp = 1; n     for (int j = 0; j < vec[x].size(); j++) n       if (mask & (1 << j)) temp *= vec[x][j]; n     if (calc[mask] % 2) n       res -= lim / temp; n     else n       res += lim / temp; n   } n   return res; n } n long long solve(long long x, long long lim) { n   long long res = 0; n   long long ts = 0; n   long long q, qsum; n   for (int mask = 0; mask < (1 << (vec[x].size())); mask++) { n     temp = 1; n     for (int j = 0; j < vec[x].size(); j++) n       if (mask & (1 << j)) temp *= vec[x][j]; n     q = lim / temp; n     qsum = q * (q + 1) / 2; n     qsum *= temp; n     if (calc[mask] % 2) n       res -= qsum; n     else n       res += qsum; n   } n   return res; n } n long long gett(long long g, long long x, long long lim) { n   if (lim <= 0) return 0; n   long long tsum = (g + 1) * solve1(x, lim); n   tsum -= solve(x, lim); n   return tsum; n } n int main() { n   ios_base::sync_with_stdio(0); n   pr[1] = 1; n   for (int i = 2; i <= 100000; i++) n     if (pr[i] == 0) n       for (int j = i * 2; j <= 100000; j += i) { n         pr[j] = 1; n         vec[j].push_back(i); n       } n   for (int i = 2; i <= 100000; i++) n     if (pr[i] == 0) vec[i].push_back(i); n   calc[0] = 0; n   for (int i = 1; i <= (1 << 15); i++) { n     t = i; n     for (int j = 0; j < 15; j++) n       if (t & (1 << j)) ++calc[i]; n   } n   long long a, b; n   cin >> n >> m; n   cin >> l >> r >> p; n   l *= l; n   r *= r; n   for (long long span = 2; span <= 100000 && span <= n; span++) { n     if (span * span > r) continue; n     t = l - span * span; n     if (t <= 0) n       lwr = 0; n     else n       lwr = sqrt(t - 0.5) + 1; n     t = r - span * span; n     upr = sqrt(t + 0.5); n     if (upr >= span) upr = span - 1; n     if (upr > m) upr = m; n     if (upr < lwr) continue; n     res = (gett(m, span, upr) - gett(m, span, lwr - 1)) * (n - span + 1) * 2; n     answ = answ + res; n     answ %= p; n   } n   swap(n, m); n   for (long long span = 2; span <= 100000 && span <= n; span++) { n     if (span * span > r) continue; n     t = l - span * span; n     if (t <= 0) n       lwr = 0; n     else n       lwr = sqrt(t - 0.5) + 1; n     t = r - span * span; n     upr = sqrt(t + 0.5); n     if (upr >= span) upr = span - 1; n     if (upr > m) upr = m; n     if (upr < lwr) continue; n     res = (gett(m, span, upr) - gett(m, span, lwr - 1)) * (n - span + 1) * 2; n     answ = answ + res; n     answ %= p; n   } n   if (1 >= l && 1 <= r) answ += n * (m + 1), answ += m * (n + 1); n   if (2 >= l && 2 <= r) answ += 2 * n * m; n   cout << answ % p << endl; n   cin.get(); n   cin.get(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   cin >> n; n   string p( / ), t; n   while (n--) { n     string foo; n     cin >> foo; n     if (foo ==  pwd ) { n       cout << p << endl; n     } else { n       string dir; n       cin >> dir; n       dir +=  / ; n       for (int i = 0; i < dir.length(); i++) { n         t += dir[i]; n         if (dir[i] ==  / ) { n           if (t ==  / ) n             p = t; n           else if (t ==  ../ ) { n             for (int j = p.length() - 1; j > 0 && p[j - 1] !=  / ; j--) { n               p.resize(j - 1); n             } n           } else n             p += t; n           t =   ; n         } n       } n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count()); n mt19937 rnf(2106); n const int N = 300005; n int n; n int a[N]; n vector<int> v[N]; n int ans[N]; n void solv() { n   scanf( %d , &n); n   for (int i = 1; i <= n; ++i) scanf( %d , &a[i]); n   for (int i = 1; i <= n; ++i) v[i].clear(); n   for (int i = 1; i <= n; ++i) { n     v[a[i]].push_back(i); n   } n   for (int i = 1; i <= n; ++i) ans[i] = N; n   for (int i = 1; i <= n; ++i) { n     if (v[i].empty()) continue; n     int maxu = v[i][0] - 1; n     for (int j = 0; j < ((int)(v[i]).size()) - 1; ++j) n       maxu = max(maxu, v[i][j + 1] - v[i][j] - 1); n     maxu = max(maxu, n - v[i].back()); n     ans[maxu + 1] = min(ans[maxu + 1], i); n   } n   for (int i = 2; i <= n; ++i) ans[i] = min(ans[i], ans[i - 1]); n   for (int i = 1; i <= n; ++i) { n     if (ans[i] == N) n       printf( -1  ); n     else n       printf( %d  , ans[i]); n   } n   printf(  n ); n } n int main() { n   int tt; n   scanf( %d , &tt); n   while (tt--) solv(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long inf = 1000000000000000000LL; n const double EPS = 1e-9; n const long long mod = 1000000007ll; n const int maxn = 500500; n int longestP[1000500]; n char pat[1000500]; n void KMP() { n   for (int i = 1, k = 0; pat[i]; i++) { n     while (k > 0 && pat[i] != pat[k]) k = longestP[k - 1]; n     if (pat[i] == pat[k]) n       longestP[i] = ++k; n     else n       longestP[i] = k; n   } n } n int main() { n   scanf( %s , pat); n   int n = strlen(pat); n   KMP(); n   if (longestP[n - 1] == 0) { n     puts( Just a legend ); n     return 0; n   } n   for (int i = 0; i + 1 < n; i++) n     if (longestP[n - 1] == longestP[i]) { n       for (int j = 0; j < longestP[i]; j++) putchar(pat[j]); n       return 0; n     } n   if (longestP[longestP[n - 1] - 1] == 0) { n     puts( Just a legend ); n     return 0; n   } n   for (int j = 0; j < longestP[longestP[n - 1] - 1]; j++) putchar(pat[j]); n } n


#include <bits/stdc++.h> n using namespace std; n int n, ans = 0; n int a[1000 + 10], pre[9][1000 + 10], kr[9][1000 + 10], cnt[10]; n int f[1000 + 10][1 << 8]; n int g[9] = {0, 1, 2, 4, 8, 16, 32, 64, 128}; n int read() { n   int x = 0, f = 1; n   char ch = getchar(); n   while (!isdigit(ch)) { n     if (ch ==  - ) f = -1; n     ch = getchar(); n   } n   while (isdigit(ch)) x = x * 10 + ch -  0 , ch = getchar(); n   return x * f; n } n int find(int t, int p) { n   int l = 1, r = n, tmp; n   while (l <= r) { n     int mid = l + r >> 1; n     pre[t][mid] > p || pre[t][mid] == 0 ? r = mid - 1 n                                         : (l = mid + 1, tmp = mid); n   } n   return tmp; n } n bool check(int st) { n   memset(f, -1, sizeof(f)); n   int tmp = -1; n   for (int i = 1; i <= n; i++) { n     f[i][0] = 0; n     for (int j = 0; j < (1 << 8); j++) { n       if (j != (1 << 8) - 1) { n         for (int k = 1; k <= 8; k++) n           if (!(j & g[k]) && f[i][j] != -1) { n             int p = pre[k][i - (a[i] == k)]; n             if (st == 0) tmp = max(tmp, f[i][j]); n             if (kr[k][p + st] != 0) n               f[kr[k][p + st]][j | g[k]] = n                   max(f[kr[k][p + st]][j | g[k]], f[i][j] + st); n             if (kr[k][p + st + 1] != 0) n               f[kr[k][p + st + 1]][j | g[k]] = n                   max(f[kr[k][p + st + 1]][j | g[k]], f[i][j] + st + 1); n           } n       } else if (f[i][j] != -1) n         tmp = max(tmp, f[i][j]); n     } n   } n   if (tmp != -1) { n     ans = max(ans, tmp); n     return true; n   } else n     return false; n } n int main() { n   n = read(); n   for (int i = 1; i <= n; i++) { n     a[i] = read(); n     for (int j = 1; j <= 8; j++) pre[j][i] = pre[j][i - 1] + (a[i] == j); n     kr[a[i]][++cnt[a[i]]] = i; n   } n   int l = 0, r = n; n   while (l <= r) { n     int mid = l + r >> 1; n     check(mid) ? l = mid + 1 : r = mid - 1; n   } n   printf( %d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int a[25] = {2,  3,  5,  7,  11, 13, 17, 19, 23, 29, n              31, 37, 41, 43, 47, 4,  9,  25, 49}; n int main() { n   string s; n   int num = 0; n   for (int i = 0; i < 19; i++) { n     printf( %d n , a[i]); n     fflush(stdout); n     cin >> s; n     if (s ==  yes ) num++; n   } n   if (num <= 1) n     printf( prime n ); n   else n     printf( composite n ); n   fflush(stdout); n } n


#include <bits/stdc++.h> n using namespace std; n const char IO_MODE = 3; n inline long long ReadInt() { n   long long x = 0, s = 1; n   char c = getchar(); n   while (c <  0  || c >  9 ) { n     if (c ==  - ) s = -1; n     c = getchar(); n   } n   while (c >=  0  && c <=  9 ) { n     x = x * 10 + c -  0 ; n     c = getchar(); n   } n   return s * x; n } n inline void WriteInt(long long x) { n   char c[20]; n   if (!x) { n     putchar( 0 ); n     return; n   } n   if (x < 0) putchar( - ), x = -x; n   int i = 0; n   while (x > 0) c[++i] = x % 10, x /= 10; n   while (i) putchar(c[i--] + 48); n } n template <typename T> n inline void out(T x) { n   if (IO_MODE & 1) n     WriteInt(x); n   else if (typeid(x) == typeid(int)) n     printf( %i , x); n   else n     printf( %lld , (long long)x); n } n template <typename T, typename... Args> n inline void out(T x, Args... args) { n   out(x); n   putc(   , stdout); n   out(args...); n } n template <typename T> n inline void in(T &x) { n   if (IO_MODE & 2) n     x = ReadInt(); n   else if (typeid(x) == typeid(int)) n     scanf( %i , &x); n   else if (typeid(x) == typeid(long long)) n     scanf( %lld , &x); n } n template <typename T, typename... Args> n inline void in(T &x, Args &...args) { n   in(x); n   in(args...); n } n int tc; 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 vector<char *> tokenizer(const char *args) { n   char *token = new char[111]; n   strcpy(token, args); n   token = strtok(token,  ,  ); n   vector<char *> v({token}); n   while (token = strtok(NULL,  ,  )) v.push_back(token); n   return reverse(v.begin(), v.end()), v; n } n void debugg(vector<char *> args) { cerr <<   b b  ; } n template <typename Head, typename... Tail> n void debugg(vector<char *> args, Head H, Tail... T) { n   debug() <<   [  << args.back() <<  :   << H <<  ]  ; n   args.pop_back(); n   debugg(args, T...); n } n long long n, A[100200]; n long long check(long long x) { n   if (!x) return 0; n   for (long long i = 0; i < n; i++) n     if (A[i] % x > A[i] / x) return 0; n   return 1; n } n int32_t main() { n   in(n); n   for (long long i = 0; i < n; i++) in(A[i]); n   sort(A, A + n); n   long long x = A[0]; n   long long mx = 1; n   for (long long i = 1; i * i <= x; i++) { n     long long m = x / i; n     if (x % i == 0) { n       if (check(i - 1)) mx = max(mx, i - 1); n       if (check(m - 1)) mx = max(mx, m - 1); n       if (check(i)) mx = max(mx, i); n       if (check(m)) mx = max(mx, m); n     } else { n       if (check(i)) mx = max(mx, i); n       if (check(m)) mx = max(mx, m); n     } n   } n   long long ans = 0; n   for (long long i = 0; i < n; i++) ans += (A[i] + mx) / (mx + 1); n   cout << ans << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int i, t; n double a, b; n int main() { n   cin >> t; n   for (i = 0; i < t; i++) { n     cin >> a >> b; n     if (b == 0) { n       cout << 1 << endl; n       continue; n     } n     b *= 4; n     if (a > b) { n       cout << 1.0 - b / (4.0 * a) << endl; n       continue; n     } n     cout << (a / 2.0 + b) / (2.0 * b) << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long t, n, k; n   cin >> t; n   while (t--) { n     cin >> n; n     int arr[n]; n     int c1 = 0; n     for (int i = 0; i < n; i++) { n       cin >> arr[i]; n       if (arr[i] == 1) c1++; n     } n     if (n - c1 >= c1) { n       cout << n - c1 << endl; n       for (int i = 0; i < n - c1; i++) { n         cout <<  0  ; n       } n       cout << endl; n     } else { n       if (c1 % 2 == 0) { n         cout << c1 << endl; n         for (int i = 0; i < c1; i++) { n           cout <<  1  ; n         } n         cout << endl; n       } else { n         cout << c1 - 1 << endl; n         for (int i = 0; i < c1 - 1; i++) { n           cout <<  1  ; n         } n         cout << endl; n       } n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e5 + 7; n long long a[N], n, seg[20][N]; n void build(int xl = 0, int xr = n, int ind = 0) { n   if (xr - xl == 1) { n     seg[ind][xl] = a[xr] - a[xl]; n     return; n   } n   int xm = (xl + xr) / 2; n   build(xl, xm, ind + 1); n   build(xm, xr, ind + 1); n   for (int i = xl; i < xm; i++) seg[ind][i] = a[xm] - a[i]; n   for (int i = xm; i < xr; i++) seg[ind][i] = a[i + 1] - a[xm]; n   sort(&seg[ind][xl], &seg[ind][xm]); n   sort(&seg[ind][xm], &seg[ind][xr]); n } n inline long long GreaterThan(long long x, int xl = 0, int xr = n, int k = 1, n                              int ind = 0) { n   if (xr - xl == 1) return (seg[ind][xl] > x); n   int xm = (xl + xr) / 2; n   long long GreaterChild = GreaterThan(x, xl, xm, 2 * k, ind + 1) + n                            GreaterThan(x, xm, xr, 2 * k + 1, ind + 1); n   long long GreaterMid = 0; n   int j = xr; n   for (int i = xl; i < xm; i++) { n     while (j > xm && seg[ind][j - 1] + seg[ind][i] > x) j--; n     GreaterMid += xr - j; n   } n   return GreaterChild + GreaterMid; n } n int main() { n   ios::sync_with_stdio(0); n   long long k; n   scanf( %lld %lld , &n, &k); n   for (int i = 0; i < n; i++) scanf( %lld , &a[i + 1]); n   for (int i = 1; i < n; i++) a[i + 1] += a[i]; n   build(); n   long long l = -1e14 - 7, r = 1e14 + 7; n   while (r - l > 1) { n     long long m = (l + r) / 2; n     long long ans = GreaterThan(m); n     if (ans >= k) n       l = m; n     else n       r = m; n   } n   printf( %lld n , r); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 500 + 10; n int R, C; n char pic[maxn][maxn]; n bool vis[maxn][maxn]; n int sr, sc, er, ec; n const int dr[] = {-1, 0, 1, 0}; n const int dc[] = {0, 1, 0, -1}; n bool ok(int r, int c) { return r >= 1 && r <= R && c >= 1 && c <= C; } n struct Node { n   int r, c; n   Node(int r = 0, int c = 0) : r(r), c(c) {} n }; n bool bfs() { n   memset(vis, 0, sizeof(vis)); n   queue<Node> Q; n   Q.push(Node(sr, sc)); n   while (!Q.empty()) { n     Node now = Q.front(); n     Q.pop(); n     int r = now.r, c = now.c; n     pic[r][c] =  X ; n     for (int i = 0; i < 4; ++i) { n       int tr = r + dr[i]; n       int tc = c + dc[i]; n       if (tr == er && tc == ec) { n         if (pic[tr][tc] ==  X ) return true; n         for (int i = 0; i < 4; ++i) { n           int ttr = tr + dr[i], ttc = tc + dc[i]; n           if (ok(ttr, ttc) && pic[ttr][ttc] ==  . ) return true; n         } n       } n       if (ok(tr, tc) && !vis[tr][tc] && pic[tr][tc] ==  . ) { n         vis[tr][tc] = 1; n         Q.push(Node(tr, tc)); n       } n     } n   } n   return false; n } n int main() { n   scanf( %d %d , &R, &C); n   for (int i = 1; i <= R; ++i) scanf( %s , pic[i] + 1); n   scanf( %d %d %d %d , &sr, &sc, &er, &ec); n   if (bfs()) n     puts( YES ); n   else n     puts( NO ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n double majk; n double dp[8005][1005]; n int visdp[8005][1005]; n double solve(int n, int k) { n   if (n == 0 && k == 0) return 1; n   if (n == 0) return 0; n   double sol = 0; n   if (visdp[n][k]) return dp[n][k]; n   if (k) { n     sol += solve(n - 1, k - 1) * ((double)k / majk); n   } n   sol += solve(n - 1, k) * ((double)(majk - k) / majk); n   visdp[n][k] = 1; n   return dp[n][k] = sol; n } n int main() { n   int k, q; n   cin >> k >> q; n   majk = k; n   solve(8000, k); n   while (q--) { n     double x; n     double e = 1e-7; n     cin >> x; n     double lhs = (x - e) / 2000.0; n     for (int i = k; i <= 8000; i++) { n       if (solve(i, k) > lhs) { n         cout << i <<   n ; n         break; n       } n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n long long maxm = 0; n long long nmbr = INT_MAX; n map<long long, bool> vstd; n class graph { n  public: n   map<long long, list<pair<long long, long long>>> adj; n   void input(long long n) { n     long long x, y, z; n     for (long long i = 1; i < n; i++) { n       cin >> x >> y >> z; n       adj[x].push_back(make_pair(y, z)); n       adj[y].push_back(make_pair(x, z)); n     } n   } n   void dfs(long long a, long long sum) { n     vstd[a] = true; n     for (auto j : adj[a]) { n       long long nghbr = j.first; n       long long wt = j.second; n       if (!vstd[nghbr]) { n         dfs(nghbr, sum + wt); n       } n     } n     maxm = max(maxm, sum); n   } n }; n int main() { n   graph g; n   long long n; n   cin >> n; n   g.input(n); n   g.dfs(0, 0); n   cout << maxm; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n signed main() { n   cin.sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   ; n   long long t; n   cin >> t; n   while (t--) { n     long long n, s; n     cin >> n >> s; n     long long half = n / 2; n     half++; n     cout << s / half << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const double PI = acos(double(-1)); n const double INF = 1e10; n const double EPS = 1e-8; n inline double sqr(double x) { return x * x; } n struct PT { n   double x, y; n   PT() {} n   PT(double x, double y) : x(x), y(y) {} n   void in() { n     double _x, _y; n     scanf( %lf%lf , &_x, &_y); n     x = _x, y = _y; n   } n }; n bool operator<(const PT &p1, const PT &p2) { n   if (fabs(p1.x - p2.x) > EPS) return p1.x < p2.x; n   return p1.y + EPS < p2.y; n } n bool operator==(const PT &p1, const PT &p2) { n   return fabs(p1.x - p2.x) < EPS && fabs(p1.y - p2.y) < EPS; n } n PT operator+(PT p1, PT p2) { return PT(p1.x + p2.x, p1.y + p2.y); } n PT operator-(PT p1, PT p2) { return PT(p1.x - p2.x, p1.y - p2.y); } n PT operator*(PT p, double c) { return PT(p.x * c, p.y * c); } n PT operator/(PT p, double c) { return PT(p.x / c, p.y / c); } n double dis(PT p) { return sqrt(sqr(p.x) + sqr(p.y)); } n double dis2(PT p) { return sqr(p.x) + sqr(p.y); } n PT rot(PT p, double t) { n   return PT(p.x * cos(t) - p.y * sin(t), p.x * sin(t) + p.y * cos(t)); n } n PT polar(PT p, double r, double t) { n   return PT(p.x + r * cos(t), p.y + r * sin(t)); n } n double dis2(PT p1, PT p2) { return sqr(p1.x - p2.x) + sqr(p1.y - p2.y); } n double dis(PT p1, PT p2) { return sqrt(dis2(p1, p2)); } n double vect(PT p1, PT p2) { return p1.x * p2.y - p2.x * p1.y; } n double scal(PT p1, PT p2) { return p1.x * p2.x + p1.y * p2.y; } n double getAngle(PT p1, PT p2) { return atan2(p2.y - p1.y, p2.x - p1.x); } n double vect(PT p, PT p1, PT p2) { return vect(p1 - p, p2 - p); } n double scal(PT p, PT p1, PT p2) { return scal(p1 - p, p2 - p); } n double getAngle(PT p, PT p1, PT p2) { n   return getAngle(p, p2) - getAngle(p, p1); n } n double getAngle180(PT p, PT p1, PT p2) { n   return acos(scal(p, p1, p2) / dis(p, p1) / dis(p, p2)); n } n double disToLine(PT p, PT p1, PT p2) { n   return fabs(vect(p, p1, p2)) / dis(p1, p2); n } n double disToSeg(PT p, PT p1, PT p2) { n   if (scal(p1, p, p2) < 0) return dis(p, p1); n   if (scal(p2, p, p1) < 0) return dis(p, p2); n   return disToLine(p, p1, p2); n } n bool onLine(PT p, PT p1, PT p2) { return fabs(vect(p1 - p, p2 - p)) < EPS; } n bool onSeg(PT p, PT p1, PT p2) { n   if (!onLine(p, p1, p2)) return 0; n   return (p1.x - p.x) * (p2.x - p.x) < EPS && (p1.y - p.y) * (p2.y - p.y) < EPS; n } n bool cross(PT p1, PT p2, PT p3, PT p4) { n   return vect(p1, p2, p3) * vect(p1, p2, p4) < -EPS && n          vect(p3, p4, p1) * vect(p3, p4, p2) < -EPS; n } n bool common(PT p1, PT p2, PT p3, PT p4) { n   if (max(p1.x, p2.x) + EPS < min(p3.x, p4.x) || n       max(p3.x, p4.x) + EPS < min(p1.x, p2.x)) n     return 0; n   if (max(p1.y, p2.y) + EPS < min(p3.y, p4.y) || n       max(p3.y, p4.y) + EPS < min(p1.y, p2.y)) n     return 0; n   return vect(p1, p2, p3) * vect(p1, p2, p4) < EPS && n          vect(p3, p4, p1) * vect(p3, p4, p2) < EPS; n } n PT projection(PT a, PT b, PT p) { n   return a + (a - b) * (scal(p - a, a - b) / dis2(a - b)); n } n int posLineLine(PT p1, PT p2, PT p3, PT p4, PT &p) { n   double s1 = vect(p1, p2, p3), s2 = vect(p1, p2, p4); n   if (fabs(s1 - s2) < EPS) { n     if (fabs(s1) < EPS) return 2; n     return 0; n   } n   p = p3 + (p4 - p3) * s1 / (s1 - s2); n   return 1; n } n int posCirLine(PT p, double r, PT p1, PT p2, PT *q) { n   double a, b, c, d2, d; n   a = dis2(p1, p2); n   b = scal(p1, p, p2); n   c = dis2(p, p1) - sqr(r); n   d2 = sqr(b) - a * c; n   if (d2 < -EPS) return 0; n   d = sqrt(fabs(d2)); n   q[0] = p1 + (p2 - p1) * (b + d) / a; n   if (d2 < EPS) return 1; n   q[1] = p1 + (p2 - p1) * (b - d) / a; n   return 2; n } n int posCirCir(PT p1, double r1, PT p2, double r2, PT *q) { n   double dx, dy, d, e, f2, f; n   dx = p2.x - p1.x, dy = p2.y - p1.y; n   d = dis(p1, p2); n   e = (sqr(r1) - sqr(r2) + sqr(d)) / 2 / d; n   f2 = sqr(r1) - sqr(e); n   if (f2 < -EPS) return 0; n   f = sqrt(fabs(f2)); n   q[0] = PT(p1.x + (e * dx - f * dy) / d, p1.y + (e * dy + f * dx) / d); n   if (f2 < EPS) return 1; n   q[1] = PT(p1.x + (e * dx + f * dy) / d, p1.y + (e * dy - f * dx) / d); n   return 2; n } n double calcArea(PT *p, int n) { n   double rlt = 0; n   for (int i = 0; i < n; i++) rlt += vect(p[i], p[(i + 1) % n]); n   return rlt / 2; n } n int inPolygon(PT p, PT *q, int m) { n   PT p1, p2; n   int k = 0; n   for (int i = 0; i < m; i++) { n     p1 = q[i], p2 = q[(i + 1) % m]; n     if (onSeg(p, p1, p2)) return 2; n     if (p1.y > p2.y) swap(p1, p2); n     if (p1.y < p.y + EPS && p.y + EPS < p2.y && vect(p, p1, p2) > 0) k++; n   } n   return k % 2; n } n inline int sgn(double a) { return a > EPS ? 1 : a < -EPS ? -1 : 0; } n bool inCPolygon(PT *p, int n, PT q) { n   bool ccw = vect(p[1] - p[0], p[2] - p[1]) > EPS; n   int below = sgn(vect(q - p[0], p[n - 1] - p[0])); n   if (ccw && below < 0) return 0; n   if (!ccw && below > 0) return 0; n   int lo = 0, hi = n - 1; n   while (hi - lo > 1) { n     int mid = (hi + lo) >> 1; n     int s1 = sgn(vect(p[mid] - p[lo], q - p[lo])); n     int s2 = sgn(vect(q - p[hi], p[mid] - p[hi])); n     bool f1 = ccw ? (s1 >= 0) : (s1 <= 0); n     bool f2 = ccw ? (s2 >= 0) : (s2 <= 0); n     if (f1 && f2) return 1; n     if (!f1 && !f2) return 0; n     if (f1) n       lo = mid; n     else n       hi = mid; n   } n   return 0; n } n bool contact_convex(PT *p, int n, PT P, int &L, int &R) { n   if (inCPolygon(p, n, P)) return 0; n   for (int T = 0; T < 2; T++) { n     auto check = [&](int fir, int sec) { n       return vect(P, p[sec], p[fir]) > EPS; n     }; n     bool up = false; n     double cr = 0; n     if (n >= 2) { n       cr = vect(P, p[0], p[1]); n     } n     if (fabs(cr) < EPS && n >= 3) { n       cr = vect(P, p[0], p[2]); n     } n     up = (cr > EPS); n     int bd[] = {1, n - 1}; n     int ans = 0; n     while (bd[0] + 6 <= bd[1]) { n       int h[2]; n       for (int hh = 0; hh < 2; hh++) { n         h[hh] = (bd[0] + bd[1] + bd[hh]) / 3; n       } n       if (!check(h[up ^ T], 0) ^ T) { n         bd[up ^ T] = h[up ^ T]; n       } else { n         int gr = check(h[0], h[1]); n         bd[gr ^ T] = h[gr ^ T]; n       } n     } n     for (int i = bd[0]; i <= bd[1]; i++) { n       if (check(i, ans) ^ T) ans = i; n     } n     T ? R = ans : L = ans; n   } n   return 1; n } n int posLineSegEx(PT p1, PT p2, PT p3, PT p4, PT &p) { n   double s1 = vect(p1, p2, p3), s2 = vect(p1, p2, p4); n   p = p3 + (p4 - p3) * s1 / (s1 - s2); n   if (s1 * s2 > -EPS) return 0; n   return 1; n } n int cutPolygon(PT *p, int n, PT p1, PT p2, PT *q) { n   int m = 0; n   for (int i = 0; i < n; i++) { n     if (vect(p[i], p1, p2) > -EPS) q[m++] = p[i]; n     if (posLineSegEx(p1, p2, p[i], p[(i + 1) % n], q[m])) m++; n   } n   return m; n } n int cutConvex(PT *p, int n, PT p1, PT p2, PT *q) { n   assert(dis(p1, p2) > EPS); n   p1 = p1 + (p1 - p2) * 1e8; n   int L, R; n   assert(contact_convex(p, n, p1, L, R)); n   if (vect(p[R], p1, p2) > -EPS) { n     for (int i = 0; i < n; i++) q[i] = p[i]; n     return n; n   } n   if (vect(p[L], p1, p2) < -EPS) return 0; n   int st = L, en = R; n   if (en < st) en += n; n   while (st < en) { n     int md = (st + en) / 2; n     if (vect(p[md % n], p1, p2) > EPS) n       st = md + 1; n     else n       en = md; n   } n   int T = st % n; n   st = R, en = L; n   if (en < st) en += n; n   while (st < en) { n     int md = (st + en) / 2; n     if (vect(p[md % n], p1, p2) < EPS) n       st = md + 1; n     else n       en = md; n   } n   int F = st % n; n   int qn = 0; n   for (int i = F; i != T; i = (i + 1) % n) q[qn++] = p[i]; n   posLineSegEx(p1, p2, p[(T - 1 + n) % n], p[T], q[qn]), qn++; n   posLineSegEx(p1, p2, p[(F - 1 + n) % n], p[F], q[qn]); n   if (dis(q[qn], q[qn - 1]) > EPS) qn++; n   return qn; n } n double disToPolygon(PT p, PT *q, int m) { n   if (inPolygon(p, q, m)) return 0; n   double rlt = INF; n   for (int i = 0; i < m; i++) rlt = min(rlt, disToSeg(p, q[i], q[(i + 1) % m])); n   return rlt; n } n double dis(PT *p, int n, PT *q, int m) { n   double rlt; n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < m; j++) { n       if (common(p[i], p[(i + 1) % n], q[j], q[(j + 1) % m])) return 0; n     } n   } n   if (inPolygon(p[0], q, m) || inPolygon(q[0], p, n)) return 0; n   rlt = INF; n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < m; j++) { n       rlt = min(rlt, disToSeg(p[i], q[j], q[(j + 1) % m])); n       rlt = min(rlt, disToSeg(q[j], p[i], p[(i + 1) % n])); n     } n   } n   return rlt; n } n int convex_hull(PT *p, int n, PT *q) { n   sort(p, p + n); n   n = unique(p, p + n) - p; n   if (n == 1) { n     q[0] = p[0]; n     return 1; n   } n   int m = 0; n   for (int i = 0; i < n; i++) { n     while (m > 1 && vect(q[m - 2], q[m - 1], p[i]) < EPS) m--; n     q[m++] = p[i]; n   } n   int k = m; n   for (int i = n - 2; i >= 0; i--) { n     while (m > k && vect(q[m - 2], q[m - 1], p[i]) < EPS) m--; n     q[m++] = p[i]; n   } n   return --m; n } n PT innercenter(PT a, PT b, PT c) { n   double A = dis(b - c); n   double B = dis(c - a); n   double C = dis(a - b); n   return PT((A * a.x + B * b.x + C * c.x) / (A + B + C), n             (A * a.y + B * b.y + C * c.y) / (A + B + C)); n } n PT outercenter(PT a, PT b, PT c) { n   double c1 = (scal(a, a) - scal(b, b)) / 2, c2 = (scal(a, a) - scal(c, c)) / 2; n   double x0 = (c1 * (a.y - c.y) - c2 * (a.y - b.y)) / vect(a - b, a - c); n   double y0 = (c1 * (a.x - c.x) - c2 * (a.x - b.x)) / vect(a - c, a - b); n   return PT(x0, y0); n } n const int N = 1100000; n double v[N]; n PT P[N], Q[N], R[] = {PT(0, 0), PT(1e5, 0), PT(1e5, 1e5), PT(0, 1e5)}, T[22], n                S[22]; n int dx[] = {0, 1, 0, -1}; n int dy[] = {-1, 0, 1, 0}; n void print(PT pt) { printf( %d %d n , (int)round(pt.x), (int)round(pt.y)); } n int main() { n   int n; n   scanf( %d , &n); n   for (int i = 0; i < n; i++) P[i].in(), scanf( %lf , &v[i]); n   int qn = 0; n   for (int i = 0; i < n; i++) { n     if (v[i] < EPS) { n       Q[qn++] = P[i]; n       continue; n     } n     int m = 4; n     for (int j = 0; j < m; j++) T[j] = P[i] + PT(v[i] * dx[j], v[i] * dy[j]); n     for (int j = 0; j < 4; j++) { n       m = cutPolygon(T, m, R[j], R[(j + 1) % 4], S); n       for (int k = 0; k < m; k++) T[k] = S[k]; n     } n     for (int j = 0; j < m; j++) Q[qn++] = T[j]; n   } n   qn = convex_hull(Q, qn, P); n   P[qn] = P[0], P[qn + 1] = P[1]; n   double ans = 0; n   int cur; n   for (int i = 0; i < qn; i++) { n     double ang = getAngle180(P[i], P[i + 1], P[i + 2]); n     double r = dis(P[i + 1], P[i + 2]) / sin(ang); n     if (ans < r) { n       ans = r; n       cur = i; n     } n   } n   for (int i = cur; i < cur + 3; i++) print(P[i]); n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 100001; n const long long mod = 1e9 + 7; n int main() { n   int N, M; n   scanf( %d %d , &N, &M); n   int arr[M][2], nums[N]; n   for (int i = 0; i < N; i++) { n     scanf( %d , &nums[i]); n   } n   for (int i = 0; i < M; i++) { n     scanf( %d %d , &arr[i][0], &arr[i][1]); n     arr[i][0]--; n     arr[i][1]--; n   } n   int a = 0, b = 0; n   int res, ans = 0; n   for (int i = 0; i < N; i++) { n     for (int j = 0; j < N; j++) { n       res = 0; n       for (int k = 0; k < M; k++) { n         if (arr[k][0] <= i && arr[k][1] >= i && n             !(arr[k][0] <= j && arr[k][1] >= j)) { n           res++; n         } n       } n       if (res + nums[j] - nums[i] > ans) { n         a = i; n         b = j; n       } n       ans = max(ans, res + nums[j] - nums[i]); n     } n   } n   printf( %d n , ans); n   vector<int> ans2; n   for (int k = 0; k < M; k++) { n     if (arr[k][0] <= a && arr[k][1] >= a && n         !(arr[k][0] <= b && arr[k][1] >= b)) { n       ans2.push_back(k + 1); n     } n   } n   printf( %d n , ans2.size()); n   for (int i : ans2) { n     printf( %d  , i); n   } n } n


#include <bits/stdc++.h> n using namespace std; n set<int> s; n map<long long, int> m1, m2; n int main() { n   int n, n2, k; n   cin >> n >> n2 >> k; n   for (int i = 0; i < n; i++) { n     int q; n     cin >> q; n     m1[q]++; n     s.insert(q); n   } n   for (int i = 0; i < n2; i++) { n     int q; n     cin >> q; n     m2[q]++; n     s.insert(q); n   } n   vector<int> difs; n   while (!s.empty()) { n     int q = *s.begin(); n     difs.push_back(m1[q] - m2[q]); n     s.erase(s.begin()); n   } n   int sum = 0; n   for (int i = difs.size() - 1; i >= 0; i--) { n     sum += difs[i]; n     if (sum > 0) return 0 * bool(cout <<  YES  << endl); n   } n   cout <<  NO  << endl; n } n


#include <bits/stdc++.h> n using namespace std; n const long long MAXN = 2e5 + 10; n long long n, T, fac[MAXN], a[MAXN], inv[MAXN], b[MAXN], ans, mod = 1e9 + 7; n long long read() { n   long long sss = 0, fff = 1; n   char ccc; n   ccc = getchar(); n   while (ccc <  0  || ccc >  9 ) { n     if (ccc ==  - ) fff = -1; n     ccc = getchar(); n   } n   while (ccc >=  0  && ccc <=  9 ) n     sss = (sss << 1) + (sss << 3) + (ccc ^  0 ), ccc = getchar(); n   return sss * fff; n } n long long ksm(long long base, long long k) { n   long long sum = 1; n   while (k) { n     if (k & 1) sum = sum * base % mod; n     base = base * base % mod; n     k >>= 1; n   } n   return sum; n } n long long C(long long n, long long m) { n   if (n < m) return 0; n   return fac[n] * inv[n - m] % mod * inv[m] % mod; n } n int main() { n   n = read(); n   T = read(); n   for (long long i = 1; i <= n; i++) a[i] = read(); n   b[n] = ksm(2, n); n   b[n] = ksm(b[n], mod - 2); n   for (long long i = n - 1; i >= 0; i--) b[i] = b[i + 1] * 2 % mod; n   fac[0] = 1; n   for (int i = 1; i <= n; i++) fac[i] = fac[i - 1] * i % mod; n   inv[n] = ksm(fac[n], mod - 2); n   for (int i = n - 1; i >= 0; i--) inv[i] = inv[i + 1] * (i + 1) % mod; n   long long now = 0; n   bool flag = true; n   long long TT, sum = 0, CCC; n   while (now <= n && T >= a[now + 1]) { n     now++, T -= a[now]; n     if (now <= T) n       ans = (ans + 1) % mod; n     else { n       if (flag) { n         TT = T; n         CCC = now; n         for (long long i = 0; i <= TT; i++) sum = (sum + C(now, i)) % mod; n       } else n         sum = (sum * 2 % mod - C(now - 1, TT) + mod) % mod; n       flag = false; n       while (TT > T) { n         sum = (sum - C(now, TT) + mod) % mod; n         TT--; n       } n       ans = (ans + sum * b[now] % mod) % mod; n     } n   } n   printf( %lld n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, m, i, flag, ans, index, j; n   int a[4000], b[4000]; n   cin >> n >> m; n   for (i = 0; i < n; i++) cin >> a[i]; n   for (i = 0; i < m; i++) cin >> b[i]; n   ans = 0; n   for (i = n; i >= 0; i--) { n     flag = 0; n     for (j = m; j >= 0; j--) { n       if (a[i] <= b[j]) { n         flag = 1; n         b[j] = 0; n         break; n       } n     } n     if (flag == 0) ans = ans + 1; n   } n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios::sync_with_stdio(false); n   int n, k; n   cin >> n >> k; n   string s; n   cin >> s; n   string ss = s; n   int iter = 0; n   int total = 0; n   while (true) { n     int cnt = 0; n     for (int i = 1; i < n; i++) n       if (s[i - 1] ==  R  && s[i] ==  L ) { n         s[i - 1] =  L ; n         s[i] =  R ; n         cnt++; n         i++; n       } n     if (cnt == 0) break; n     iter++; n     total += cnt; n   } n   if (k < iter || k > total) { n     cout << -1 << endl; n     return 0; n   } n   s = ss; n   int slow = k - iter; n   while (true) { n     int cnt = 0; n     vector<int> res; n     for (int i = 1; i < n; i++) n       if (s[i - 1] ==  R  && s[i] ==  L ) { n         s[i - 1] =  L ; n         s[i] =  R ; n         cnt++; n         if (slow > 0) { n           slow--; n           cout <<  1   << i <<   n ; n         } else { n           res.push_back(i); n         } n         i++; n       } n     if (cnt == 0) break; n     if (res.size() > 0) { n       cout << res.size(); n       for (int i = 0; i < res.size(); i++) cout <<     << res[i]; n       cout <<   n ; n     } else { n       slow++; n     } n   } n   cout.flush(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int N; n   cin >> N; n   int v[N]; n   for (int i = 0; i < N; i++) { n     cin >> v[i]; n   } n   vector<int> quadr; n   for (int i = 0; 1; i++) { n     if (i > sqrt(INT_MAX)) { n       break; n     } n     quadr.push_back(i * i); n   } n   int dist[N]; n   vector<int> minDiffs; n   int contQuadr = 0; n   for (int i = 0; i < N; i++) { n     int curr = v[i]; n     int index = upper_bound(quadr.begin(), quadr.end(), curr) - quadr.begin(); n     int l = abs(curr - quadr[index]); n     int r = l; n     if (index - 1 >= 0) r = abs(curr - quadr[index - 1]); n     int minDiff = min(l, r); n     if (minDiff == 0) contQuadr++; n     minDiffs.push_back(minDiff); n   } n   if (contQuadr == N / 2) { n     cout << 0; n     return 0; n   } n   if (contQuadr < N / 2) { n     int diff = N / 2 - contQuadr; n     sort(minDiffs.begin(), minDiffs.end()); n     long long int res = 0; n     int i = 0; n     int cont = 0; n     while (diff > 0 && i < N) { n       if (minDiffs[i] == 0) { n         i++; n         continue; n       } n       res += minDiffs[i]; n       i++; n       diff--; n     } n     cout << res; n     return 0; n   } n   if (contQuadr > N / 2) { n     long long int diff = contQuadr - N / 2; n     long long int res = 0; n     int costs[N]; n     memset(costs, 0, N * sizeof(int)); n     for (int i = 0; i < N; i++) { n       if (v[i] == 0) { n         costs[i] = 2; n         continue; n       } n       if (minDiffs[i] == 0) { n         costs[i] = 1; n       } n     } n     sort(costs, costs + N); n     for (int i = 0; i < N && diff > 0; i++) { n       if (costs[i] == 0) continue; n       res += costs[i]; n       diff--; n     } n     cout << res; n     return 0; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 5e5 + 5; n char a[N], b[N]; n int main() { n   int n, k; n   cin >> n >> k; n   scanf( %s %s , a + 1, b + 1); n   long long ans = 0, t = 1; n   for (int i = 1; i <= n; ++i) { n     t = t * 2; n     if (b[i] ==  a ) t--; n     if (a[i] ==  b ) t--; n     if (t > k) t = k + 1; n     ans += (t > k ? k : t); n   } n   printf( %lld n , ans); n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T> n class FenwickTree { n  private: n   vector<T> bit; n   int M; n  n  public: n   FenwickTree(int M) : bit(vector<T>(M + 1, 0)), M(M) {} n   int sum(int i) { n     if (!i) return 0; n     return bit[i] + sum(i - (i & -i)); n   } n   void add(int i, T x) { n     if (i > M) return; n     bit[i] += x; n     add(i + (i & -i), x); n   } n }; n int main() { n   ios::sync_with_stdio(false); n   int N; n   cin >> N; n   vector<int> X1(N), X2(N), Y1(N), Y2(N); n   map<int, int> Yc; n   set<int> Ys; n   vector<int> YI; n   map<int, vector<int>> Ha, He; n   map<int, vector<pair<int, int>>> V, H; n   set<int> cand; n   YI.push_back(-1 << 30); n   for (int i = 0; i < N; ++i) { n     int x1, x2, y1, y2; n     cin >> x1 >> y1 >> x2 >> y2; n     if (x2 < x1) swap(x1, x2); n     if (y2 < y1) swap(y1, y2); n     YI.push_back(y1 - 1); n     YI.push_back(y1); n     YI.push_back(y1 + 1); n     YI.push_back(y2 - 1); n     YI.push_back(y2); n     YI.push_back(y2 + 1); n     X1[i] = x1, X2[i] = x2, Y1[i] = y1, Y2[i] = y2; n   } n   sort((YI.begin()), (YI.end())); n   YI.erase(unique(YI.begin(), YI.end()), YI.end()); n   for (int i = 0; i < N; ++i) { n     Y1[i] = lower_bound((YI.begin()), (YI.end()), Y1[i]) - YI.begin(); n     Y2[i] = lower_bound((YI.begin()), (YI.end()), Y2[i]) - YI.begin(); n     if (X1[i] == X2[i]) { n       V[X1[i]].push_back({Y1[i], Y2[i]}); n       cand.insert(X1[i]); n     } else { n       Ha[X1[i]].push_back(Y1[i]); n       He[X2[i]].push_back(Y1[i]); n       H[Y1[i]].push_back({X1[i], X2[i]}); n       cand.insert(X1[i]); n       cand.insert(X2[i]); n     } n   } n   FenwickTree<signed long long> bt(YI.size()); n   signed long long ret = 0; n   for (auto cx : cand) { n     for (auto y : Ha[cx]) n       if (Yc[y]++ == 0) bt.add(y, 1); n     if (V.count(cx)) { n       vector<pair<int, int>>& T = V[cx]; n       sort((T.begin()), (T.end())); n       int ma = -1 << 30; n       for (auto r : T) { n         r.first = max(r.first, ma + 1); n         if (r.first > r.second) continue; n         ret += YI[r.second] - YI[r.first] + 1; n         ret -= bt.sum(r.second) - bt.sum(r.first - 1); n         ma = r.second; n       } n     } n     for (auto y : He[cx]) n       if (--Yc[y] == 0) bt.add(y, -1); n   } n   for (auto hy : H) { n     vector<pair<int, int>>& T = hy.second; n     sort((T.begin()), (T.end())); n     int ma = -1 << 30; n     for (auto r : T) { n       r.first = max(r.first, ma + 1); n       if (r.first > r.second) continue; n       ret += r.second - r.first + 1; n       ma = r.second; n     } n   } n   cout << ret << endl; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 1e5 + 777; n vector<int> G[maxn]; n map<int, int> savesiz[maxn]; n int siz[maxn]; n vector<int> ans; n int n; n void dfs(int s, int fa) { n   siz[s] = 1; n   int cnt = 0; n   for (int d : G[s]) { n     if (d == fa) continue; n     dfs(d, s); n     savesiz[s][siz[d]]++; n     cnt++; n     siz[s] += siz[d]; n   } n   if (n - siz[s] > 0) { n     savesiz[s][n - siz[s]]++; n     cnt++; n   } n   if (savesiz[s].size() == 1 && cnt >= 2) ans.push_back(s); n } n vector<int> aans; n map<int, int> have[maxn]; n void func(int s) { n   int nowsiz = 1; n   int nowid = G[s][0]; n   int fa = s; n   while (G[nowid].size() == 2) { n     nowsiz++; n     if (G[nowid][0] == fa) { n       fa = nowid; n       nowid = G[nowid][1]; n     } else { n       fa = nowid; n       nowid = G[nowid][0]; n     } n   } n   if (have[nowid].count(nowsiz)) return; n   if (have[nowid].size() >= 2) return; n   have[nowid][nowsiz] = 1; n   if ((savesiz[nowid][nowsiz] == 1 && savesiz[nowid].size() == 2) || n       savesiz[nowid].size() == 1) { n     aans.push_back(s); n   } n } n set<int> se[maxn]; n int vis[maxn]; n bool ck(int s) { n   for (int i = 0; i <= n; i++) { n     se[i].clear(); n     vis[i] = 0; n   } n   queue<pair<int, int> > q; n   vis[s] = 1; n   q.push(make_pair(s, 0)); n   while (!q.empty()) { n     int nowid = q.front().first; n     int nowdis = q.front().second; n     q.pop(); n     se[nowdis].insert(G[nowid].size()); n     if (se[nowdis].size() >= 2) return false; n     for (int d : G[nowid]) { n       if (vis[d]) continue; n       vis[d] = 1; n       q.push(make_pair(d, nowdis + 1)); n     } n   } n   return true; n } n void solve() { n   scanf( %d , &n); n   if (n == 1) { n     cout << 1 << endl; n     return; n   } n   for (int i = 1; i < n; i++) { n     int a, b; n     scanf( %d %d , &a, &b); n     G[a].push_back(b); n     G[b].push_back(a); n   } n   dfs(1, 1); n   for (int iid : ans) { n     if (ck(iid)) { n       cout << iid << endl; n       return; n     } n   } n   for (int i = 1; i <= n; i++) { n     if (G[i].size() == 1) { n       func(i); n     } n   } n   for (int iid : aans) { n     if (ck(iid)) { n       cout << iid << endl; n       return; n     } n   } n   cout << -1 << endl; n } n signed main() { solve(); } n


#include <bits/stdc++.h> n #pragma comment(linker,  /STACK:16777216 ) n using namespace std; n const double eps = 1e-9; n const double pi = acos(-1.0); n const int maxn = (int)1e4 + 10; n int a[maxn], b[maxn]; n int main() { n   int n, c; n   cin >> n >> c; n   if (c < n) { n     cout << 0 << endl; n     return 0; n   } n   c -= n; n   for (int i = 0; i < n; i++) { n     cin >> a[i] >> b[i]; n   } n   int m = 0; n   for (int i = 0; i < n; i++) n     if (a[i] != 0) { n       a[m] = a[i]; n       b[m] = b[i]; n       m++; n     } n   if (m == 0) { n     if (c == 0) { n       cout << -1 << endl; n     } else { n       cout << 0 << endl; n     } n     return 0; n   } n   long long least = 0; n   for (int i = 0; i < m; i++) { n     least += a[i] / b[i]; n   } n   if (least > c) { n     cout << 0 << endl; n     return 0; n   } n   long long lf = 0; n   long long rg = (long long)1e18; n   while (lf < rg - 1) { n     long long mm = (lf + rg) >> 1; n     double sum = 0.0; n     for (int i = 0; i < m; i++) { n       sum += floor(a[i] * 1.0 / b[i] * mm + eps); n       if (sum >= c) break; n     } n     if (sum >= c) { n       rg = mm; n     } else { n       lf = mm; n     } n   } n   long long sum = 0; n   for (int i = 0; i < m; i++) { n     sum += (long long)(a[i] * 1.0 / b[i] * rg + eps); n   } n   if (sum > c) { n     cout << 0 << endl; n     return 0; n   } n   int res = (int)1e9 + 10; n   for (int i = 0; i < m; i++) { n     int ost = a[i] % b[i] * 1LL * (rg % b[i]) % b[i]; n     res = min(res, 1 + (b[i] - ost) / a[i] - ((b[i] - ost) % a[i] == 0)); n   } n   cout << res << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = (int)15e4 + 10, M = 310; n int n, m, d; n long long a[N], b[N], t[N], tmp[N], dp[2][N]; n inline void f(int j) { n   long long k = 1ll * (t[j] - t[j - 1]) * d; n   int r = j % 2; n   deque<int> deq; n   for (int i = 0; i < n; i++) { n     while (deq.size() && dp[!r][deq.back()] <= dp[!r][i]) deq.pop_back(); n     deq.push_back(i); n     while (deq.front() < i - k) deq.pop_front(); n     tmp[i] = dp[!r][deq.front()]; n   } n } n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cin >> n >> m >> d; n   for (int i = 0; i < m; i++) { n     cin >> a[i] >> b[i] >> t[i]; n     a[i]--; n   } n   for (int i = 0; i < n; i++) dp[0][i] = b[0] - abs(a[0] - i); n   for (int j = 1; j < m; j++) { n     int r = j % 2; n     f(j); n     for (int i = 0; i < n; i++) dp[r][i] = tmp[i]; n     reverse(dp[!r], dp[!r] + n); n     f(j); n     for (int i = 0; i < n; i++) dp[r][i] = max(dp[r][i], tmp[n - 1 - i]); n     for (int i = 0; i < n; i++) dp[r][i] += b[j] - abs(a[j] - i); n   } n   m--; n   cout << *max_element(dp[m % 2], dp[m % 2] + n); n   return 0; n } n


#include <bits/stdc++.h> n #pragma GCC optimize( Ofast ,  unroll-loops ,  omit-frame-pointer ,  inline ) n #pragma GCC optimize(3) n using namespace std; n const long long INF = 1e17; n const long long maxn = 1e5 + 700; n const int mod = 998244353; n const int up = 1e9; n template <typename T> n inline void read(T &a) { n   char c = getchar(); n   T x = 0, f = 1; n   while (!isdigit(c)) { n     if (c ==  - ) f = -1; n     c = getchar(); n   } n   while (isdigit(c)) { n     x = (x << 1) + (x << 3) + c -  0 ; n     c = getchar(); n   } n   a = f * x; n } n long long n, m, p; n int num[maxn]; n struct node { n   int tag, w, l, r; n } t[maxn * 40]; n int root[maxn * 40]; n int cnt = 0; n void Modify(int &now, int pre, int l, int r, int x, int y, int w) { n   t[now = ++cnt] = t[pre]; n   if (x <= l && y >= r) { n     t[now].tag += w; n     t[now].w += w; n     return; n   } n   int mid = (l + r) / 2; n   if (x <= mid) Modify(t[now].l, t[pre].l, l, mid, x, y, w); n   if (y > mid) Modify(t[now].r, t[pre].r, mid + 1, r, x, y, w); n   int ls = t[now].l, rs = t[now].r; n   t[now].w = min(t[ls].w, t[rs].w) + t[now].tag; n } n int Query(int now, int l, int r, int id, int tag, int k) { n   if (l == r) return l; n   int mid = (l + r) / 2; n   if (mid < id) return Query(t[now].r, mid + 1, r, id, tag + t[now].tag, k); n   int ls = t[now].l, rs = t[now].r; n   return t[rs].w + tag + t[now].tag > k n              ? Query(ls, l, mid, id, tag + t[now].tag, k) n              : Query(rs, mid + 1, r, id, tag + t[now].tag, k); n } n int work(int x) { n   int s = 1, ans = 0; n   while (s <= n) { n     s = Query(root[s], 1, n, s, 0, x) + 1; n     ans++; n   } n   return ans; n } n int last[maxn]; n int main() { n   read(n); n   for (int i = 1; i <= n; i++) read(num[i]); n   for (int i = n; i >= 1; i--) { n     int up = last[num[i]] ? last[num[i]] - 1 : n; n     Modify(root[i], root[i + 1], 1, n, i, up, 1); n     last[num[i]] = i; n   } n   for (int i = 1; i <= n; i++) printf( %d  , work(i)); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int a[1000000], ans[1000000]; n int n, k, p, x, y, sum = 0; n int main() { n   cin >> n >> k >> p >> x >> y; n   for (int i = 1; i <= k; i++) { n     cin >> a[i]; n     sum += a[i]; n   } n   if (sum + n - k > x) { n     cout << -1; n     return 0; n   } n   for (int i = k + 1; i <= n; i++) { n     a[i] = y; n     sum += y; n   } n   int id = k + 1; n   while (sum > x) { n     if (a[id] == 1) { n       id++; n       continue; n     } n     a[id]--; n     sum--; n   } n   int c = 0; n   for (int i = k + 1; i <= n; i++) ans[c++] = a[i]; n   sort(a + 1, a + n + 1); n   if (a[(n + 1) / 2] < y) n     cout << -1; n   else { n     for (int i = 0; i < c; i++) cout << ans[i] <<    ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int N; n inline bool check(int x) { n   long long t = (long long)x * (long long)(x - 1) / 2; n   if (x & 1) n     t++; n   else n     t += x / 2; n   return t <= N; n } n int main() { n   int M; n   scanf( %d%d , &N, &M); n   vector<int> A; n   for (int i = 0; i < M; i++) { n     int x; n     scanf( %*d%d , &x); n     A.push_back(x); n   } n   sort(A.begin(), A.end(), greater<int>()); n   int lb = 1, rb = M; n   while (lb < rb) { n     int mid = (lb + rb + 1) >> 1; n     if (check(mid)) n       lb = mid; n     else n       rb = mid - 1; n   } n   long long ans = 0; n   for (int i = 0; i < lb; i++) ans += A[i]; n   printf( %lld n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int x; n   cin >> x; n   string names[x * 2]; n   for (int i = 0; i < x * 2; i++) cin >> names[i]; n   for (int i = 0; i < x * 2; i++) n     if (names[i] ==  rat ) { n       cout << names[i - 1]; n       cout << endl; n     } n   for (int i = 0; i < x * 2; i++) n     if (names[i] ==  woman  || names[i] ==  child ) { n       cout << names[i - 1]; n       cout << endl; n     } n   for (int i = 0; i < x * 2; i++) n     if (names[i] ==  man ) { n       cout << names[i - 1]; n       cout << endl; n     } n   for (int i = 0; i < x * 2; i++) n     if (names[i] ==  captain ) { n       cout << names[i - 1]; n       cout << endl; n     } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m; n struct EDGE { n   int u, v, w; n } p[100000 + 5]; n struct Edge { n   int v, w, next; n } edge[100000 + 5]; n int head[100000 + 5], cnt = 0; n void init() { n   cnt = 0; n   memset(head, -1, sizeof head); n } n void add_edge(int u, int v, int w) { n   edge[cnt].v = v; n   edge[cnt].w = w; n   edge[cnt].next = head[u]; n   head[u] = cnt++; n } n int vis[100000 + 5]; n int rd[100000 + 5]; n bool check(int up) { n   memset(rd, 0, sizeof rd); n   memset(vis, 0, sizeof vis); n   queue<int> que; n   for (int i = 1; i <= m; i++) n     if (p[i].w > up) rd[p[i].v]++; n   int tot = 0; n   while (1) { n     for (int u = 1; u <= n; u++) n       if (!vis[u] && rd[u] == 0) que.push(u); n     if (que.empty()) break; n     while (!que.empty()) { n       int u = que.front(); n       que.pop(); n       vis[u] = ++tot; n       for (int i = head[u]; i != -1; i = edge[i].next) n         if (!vis[edge[i].v] && edge[i].w > up) rd[edge[i].v]--; n     } n   } n   for (int u = 1; u <= n; u++) n     if (!vis[u]) return 0; n   return 1; n } n int main() { n   if (1 && fopen( in.txt ,  r )) { n     freopen( in.txt ,  r , stdin); n     freopen( out.txt ,  w , stdout); n   } n   init(); n   scanf( %d%d , &n, &m); n   int l = 0, r = 0, mid; n   for (int i = 1; i <= m; i++) { n     scanf( %d%d%d , &p[i].u, &p[i].v, &p[i].w); n     add_edge(p[i].u, p[i].v, p[i].w); n     r = max(r, p[i].w); n   } n   while (l < r) { n     mid = (l + r) >> 1; n     if (check(mid)) n       r = mid; n     else n       l = mid + 1; n   } n   int ans = l; n   vector<int> ansque; n   check(ans); n   for (int i = 1; i <= m; i++) { n     int u = p[i].u, v = p[i].v; n     if (p[i].w <= ans && vis[u] > vis[v]) ansque.push_back(i); n   } n   sort(ansque.begin(), ansque.end()); n   printf( %d %d n , ans, ansque.size()); n   for (int i = 0; i < ansque.size(); i++) n     printf( %d%c , ansque[i], i == ansque.size() - 1 ?   n  :    ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, i, a[150]; n   scanf( %d , &n); n   int sum = 0; n   for (i = 0; i < n; i++) { n     scanf( %d , &a[i]); n     sum += a[i]; n   } n   int ans = 0; n   for (i = 0; i < n; i++) (sum % 2) ^ ((a[i] + 1) % 2) ? ans++ : ans = ans; n   printf( %d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   string a, b; n   int mini = INT_MAX; n   cin >> a >> b; n   int az = a.size(); n   for (int i = -2000; i <= az; i++) { n     int ans = b.size(); n     for (int j = 0; j < b.size(); j++) { n       ans -= (i + j >= 0 && i + j < a.size() && a[i + j] == b[j]); n     } n     mini = min(mini, ans); n   } n   cout << mini; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n typedef struct line { n   int k; n   int b; n   long double x; n } line; n bool com(line l1, line l2) { n   if (l1.x < l2.x) n     return true; n   else n     return false; n } n int main() { n   long long k, b, n, t; n   cin >> k >> b >> n >> t; n   long long x = 1; n   int t1 = 0; n   while (x < t) { n     x = k * x + b; n     t1++; n   } n   if (x != t) t1--; n   if (t1 < n) { n     cout << n - t1; n   } else n     cout << 0; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 4e5 + 10; n struct Edge { n   int to, nxt; n } e[N << 1]; n int h[N], idx; n void Ins(int a, int b) { n   e[++idx].to = b; n   e[idx].nxt = h[a]; n   h[a] = idx; n } n int bas, f[N], g[N], to[N], n, siz[N], Mx[N], mk[N], sec[N]; n void dfs(int u, int fa) { n   siz[u] = 1; n   for (int i = h[u]; i; i = e[i].nxt) { n     int v = e[i].to; n     if (v == fa) continue; n     dfs(v, u); n     siz[u] += siz[v]; n     if (Mx[u] < siz[v]) Mx[u] = siz[v], to[u] = v; n     if (siz[v] <= bas) { n       if (g[u] < siz[v]) { n         sec[u] = g[u]; n         g[u] = siz[v]; n         mk[u] = v; n       } else if (sec[u] < siz[v]) { n         sec[u] = siz[v]; n       } n     } else { n       if (g[v] > g[u]) { n         sec[u] = g[u]; n         g[u] = g[v]; n         mk[u] = v; n       } else if (g[v] > sec[u]) n         sec[u] = g[v]; n     } n   } n   if (Mx[u] < n - siz[u]) { n     Mx[u] = n - siz[u]; n     to[u] = fa; n   } n } n void dfs2(int u, int fa) { n   if (Mx[u] <= bas) n     f[u] = 1; n   else if (to[u] != fa) n     f[u] = (Mx[u] - g[to[u]]) <= bas; n   else { n     if (mk[fa] == u) { n       f[u] = (Mx[u] - sec[fa]) <= bas; n     } else n       f[u] = (Mx[u] - g[fa]) <= bas; n   } n   if (mk[fa] == u) { n     if (sec[fa] > g[u]) { n       sec[u] = g[u]; n       g[u] = sec[fa]; n       mk[u] = fa; n     } else if (sec[fa] > sec[u]) { n       sec[u] = sec[fa]; n     } n     int w = n - siz[u]; n     if (w <= bas) { n       if (w > g[u]) { n         sec[u] = g[u]; n         g[u] = w; n         mk[u] = fa; n       } else if (w > sec[u]) { n         sec[u] = w; n       } n     } n   } else { n     if (g[fa] > g[u]) { n       sec[u] = g[u]; n       g[u] = g[fa]; n       mk[u] = fa; n     } else if (g[fa] > sec[u]) n       sec[u] = g[fa]; n     int w = n - siz[u]; n     if (w <= bas) { n       if (w > g[u]) { n         sec[u] = g[u]; n         g[u] = w; n         mk[u] = fa; n       } else if (w > sec[u]) { n         sec[u] = w; n       } n     } n   } n   for (int i = h[u]; i; i = e[i].nxt) { n     int v = e[i].to; n     if (v == fa) continue; n     dfs2(v, u); n   } n } n int main() { n   scanf( %d , &n); n   bas = n >> 1; n   for (int i = 1; i < n; i++) { n     int a, b; n     scanf( %d%d , &a, &b); n     Ins(a, b); n     Ins(b, a); n   } n   dfs(1, 0); n   dfs2(1, 0); n   for (int i = 1; i <= n; i++) printf( %d  , f[i]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int INF = 1000000000; n int main() { n   int n; n   cin >> n >> ws; n   char s[1001]; n   scanf( %s , &s); n   int h = 0; n   for (int i = 0; i < n; i++) { n     if (s[i] ==  H ) h++; n   } n   int _min = INF; n   for (int i = 0; i < n; i++) { n     int ans = 0; n     for (int j = 0; j < h; j++) { n       if (s[(i + j) % n] ==  T ) ans++; n     } n     _min = min(ans, _min); n   } n   cout << _min; n   return 0; n } n


#include <bits/stdc++.h> n using ll = long long; n using ld = long double; n using ull = unsigned long long; n using namespace std; n const int N = 10; n int T, n, a[N]; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(nullptr); n   cin >> T; n   while (T--) { n     cin >> n; n     for (int i = (0); i <= (n - 1); ++i) cin >> a[i]; n     sort(a, a + n); n     bool ok = 0; n     for (int i = (0); i <= (n - 1); ++i) { n       if (a[i] == 0 || (i > 0 && a[i] == a[i - 1])) ok = 1; n     } n     for (int i = (0); i <= (n - 1); ++i) { n       for (int j = (0); j <= (n - 1); ++j) n         if (a[j] == -a[i]) { n           ok = 1; n           break; n         } n       for (int x = (0); x <= ((1 << n) - 1); ++x) n         if (!(x >> i & 1)) { n           for (int mask = x;; mask = (mask - 1) & x) { n             int sum = 0; n             for (int j = (0); j <= (n - 1); ++j) n               if (x >> j & 1) { n                 if (mask >> j & 1) n                   sum += a[j]; n                 else n                   sum -= a[j]; n               } n             if (sum == a[i]) { n               ok = 1; n               break; n             } n             if (mask == 0) break; n           } n         } n     } n     cout << (ok ?  YES n  :  NO n ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int nmax = 5e5 + 42; n int n, m; n struct edges { n   int u, v, w; n   int id_original; n }; n edges inp[nmax], all[nmax]; n bool cmp(edges a, edges b) { return a.w < b.w; } n int parent[nmax]; n int root(int node) { n   if (parent[node] == node) return node; n   parent[node] = root(parent[node]); n   return parent[node]; n } n int max_cost = 0; n int group[nmax], id = 0; n bool can_be[nmax]; n bool cmp2(int edge_1, int edge_2) { return group[edge_1] < group[edge_2]; } n int sz, test[nmax]; n map<pair<int, int>, vector<int> > types; n int main() { n   scanf( %i%i , &n, &m); n   for (int i = 1; i <= m; i++) { n     scanf( %i%i%i , &inp[i].u, &inp[i].v, &inp[i].w); n     inp[i].id_original = i; n     all[i] = inp[i]; n   } n   sort(all + 1, all + m + 1, cmp); n   for (int i = 1; i <= n; i++) parent[i] = i; n   for (int i = 1; i <= m; i++) { n     int j = i; n     while (j <= m && all[i].w == all[j].w) j++; n     j--; n     max_cost = all[i].w; n     types = {}; n     for (int k = i; k <= j; k++) { n       if (root(all[k].u) != root(all[k].v)) { n         can_be[all[k].id_original] = 1; n         pair<int, int> work = {root(all[k].u), root(all[k].v)}; n         if (work.first > work.second) swap(work.first, work.second); n         types[work].push_back(all[k].id_original); n       } n     } n     for (auto w : types) { n       id++; n       for (auto k : w.second) group[k] = id; n     } n     for (int k = i; k <= j; k++) parent[root(all[k].u)] = root(all[k].v); n     i = j; n   } n   for (int i = 1; i <= n; i++) parent[i] = i; n   int q; n   scanf( %i , &q); n   for (int i = 1; i <= q; i++) { n     scanf( %i , &sz); n     for (int j = 1; j <= sz; j++) scanf( %i , &test[j]); n     bool can = 1; n     for (int j = 1; j <= sz; j++) n       if (can_be[test[j]] == 0) { n         can = 0; n         break; n       } n     sort(test + 1, test + sz + 1, cmp2); n     for (int j = 2; j <= sz; j++) n       if (group[test[j - 1]] == group[test[j]]) can = 0; n     for (int j = 1; j <= sz; j++) n       if (root(inp[test[j]].u) == root(inp[test[j]].v)) n         can = 0; n       else n         parent[root(inp[test[j]].u)] = root(inp[test[j]].v); n     for (int j = 1; j <= sz; j++) n       parent[inp[test[j]].u] = inp[test[j]].u, n       parent[inp[test[j]].v] = inp[test[j]].v; n     if (can) n       printf( YES n ); n     else n       printf( NO n ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n inline int read() { n   int x = 0, f = 1; n   char c = getchar(); n   while (!isdigit(c)) { n     if (c ==  - ) f = -1; n     c = getchar(); n   } n   while (isdigit(c)) { n     x = (x << 1) + (x << 3) + c - 48; n     c = getchar(); n   } n   return x * f; n } n const int N = 510; n const int mod = 998244353; n int n, cnt1, cnt2, cnt3, cnt4; n int s[N][N], A[N][N], C[N][N]; n int a[N], b[N]; n int f[N], g[N], G[N]; n int tmp[N], ans[N]; n int vis[N << 1], nxt[N]; n int ru[N]; n void dfs(int u, int fa) { n   vis[u] = 1; n   if (nxt[u]) { n     if (!vis[nxt[u]]) n       dfs(nxt[u], fa); n     else n       cnt3++; n   } else { n     if (fa > n && u > n) n       cnt4++; n     else if (fa <= n && u > n) n       cnt1++; n     else if (fa > n && u <= n) n       cnt2++; n   } n } n void init() { n   s[0][0] = C[0][0] = A[0][0] = 1; n   for (int i = 1; i <= n; i++) { n     C[i][0] = A[i][0] = 1; n     for (int j = 1; j <= i; j++) { n       C[i][j] = (C[i - 1][j] + C[i - 1][j - 1]) % mod; n       A[i][j] = (A[i - 1][j] + 1ll * j * A[i - 1][j - 1] % mod) % mod; n       s[i][j] = (1ll * (i - 1) * s[i - 1][j] % mod + s[i - 1][j - 1]) % mod; n     } n   } n } n void getans(int n, int *f) { n   for (int i = 0; i <= n; i++) { n     for (int j = i; j <= n; j++) { n       f[i] = (f[i] + n               1ll * C[n][j] * s[j][i] % mod * A[n - j + cnt4][n - j] % mod) % n              mod; n     } n   } n   memset(G, 0, sizeof G); n   for (int i = 0; i <= n; i++) { n     for (int j = i, fz = 1; j <= n; j++, fz = -fz) n       G[i] = (G[i] + 1ll * C[j][i] * f[j] % mod * fz + mod) % mod; n   } n   memcpy(f, G, sizeof(G)); n } n int main() { n   n = read(); n   for (int i = 1; i <= n + n; i++) vis[i] = 1; n   for (int i = 1; i <= n; i++) a[i] = read(); n   for (int i = 1; i <= n; i++) b[i] = read(); n   for (int i = 1; i <= n; i++) { n     int x = a[i] ? a[i] : i + n; n     int y = b[i] ? b[i] : i + n; n     vis[x] = vis[y] = 0; n     if (x <= n || y <= n) nxt[x] = y, ru[y]++; n   } n   for (int i = 1; i <= n + n; i++) n     if (!vis[i] && !ru[i]) dfs(i, i); n   for (int i = 1; i <= n + n; i++) n     if (!vis[i]) dfs(i, i); n   init(); n   getans(cnt1, f); n   getans(cnt2, g); n   for (int i = 0; i <= n; i++) { n     for (int j = 0; j <= i; j++) { n       tmp[i] = (tmp[i] + 1ll * f[j] * g[i - j] % mod) % mod; n     } n   } n   for (int i = 0; i <= n; i++) { n     for (int j = 0; j <= i; j++) { n       ans[i] = (ans[i] + 1ll * tmp[j] * s[cnt4][i - j] % mod) % mod; n     } n     ans[i] = 1ll * ans[i] * A[cnt4][cnt4] % mod; n   } n   for (int i = 0; i < n; i++) n     printf( %d  , (n - i - cnt3 < 0) ? 0 : ans[n - i - cnt3]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int INF = numeric_limits<int>::max(); n const long long LLINF = numeric_limits<long long>::max(); n const unsigned long long ULLINF = numeric_limits<unsigned long long>::max(); n const double PI = acos(-1.0); n vector<int> ans; n void solve(int n, int p, int maxN) { n   if (!p) return; n   int mx = n - (p - 1); n   int cur = min(maxN, mx); n   ans.push_back(cur); n   solve(n - cur, p - 1, maxN); n } n vector<char> type; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   char c; n   type.push_back(1); n   int p = 1, m = 0; n   cin >> c; n   while (c !=  = ) { n     if (c ==  + ) { n       p++; n       type.push_back( + ); n     } else if (c ==  - ) { n       type.push_back( - ); n       m++; n     } n     cin >> c; n   } n   int n; n   cin >> n; n   if (!p) { n     cout <<  Impossible ; n     return 0; n   } n   if (!m) { n     if (p > n) { n       cout <<  Impossible ; n       return 0; n     } n     cout <<  Possible n ; n     solve(n, p, n); n     cout << ans.back() <<    ; n     ans.pop_back(); n     while (ans.size()) { n       cout <<  +   << ans.back() <<    ; n       ans.pop_back(); n     } n     cout <<  =   << n; n     return 0; n   } n   vector<int> positive, negative; n   bool found = 0; n   for (int pos = n; pos <= n * p; pos++) { n     int neg = pos - n; n     if (neg / m > 0 && n * m >= neg) { n       if (p > pos) continue; n       solve(pos, p, n); n       positive = ans; n       solve(neg, m, n); n       negative = ans; n       found = 1; n       break; n     } n   } n   if (!found) { n     cout <<  Impossible ; n     return 0; n   } n   cout <<  Possible n ; n   cout << positive.back() <<    ; n   positive.pop_back(); n   for (int i = 1; i < type.size(); i++) { n     if (type[i] ==  + ) { n       cout <<  +   << positive.back() <<    ; n       positive.pop_back(); n     } else { n       cout <<  -   << negative.back() <<    ; n       negative.pop_back(); n     } n   } n   cout <<  =   << n; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n using namespace std::chrono; n inline long long lrand() { n   long long x = 0; n   for (int i = 0; i < (4); i++) { n     x <<= 16; n     x ^= rand(); n   } n   return x; n } n string fname(string s, vector<long long> v) { n   string x =  C:/leet/  + s; n   for (auto& k : (v)) x +=  _  + to_string(k); n   x +=  .txt ; n   return x; n } n long long now() { n   return duration_cast<milliseconds>(system_clock::now().time_since_epoch()) n       .count(); n } n template <typename T> n void W(vector<T>& v) { n   for (int i = 0; i < (v.size()); i++) cout << v[i] <<    ; n } n int main(int argc, char** argv) { n   cin.sync_with_stdio(false); n   long long n; n   scanf( %lld , &n); n   vector<long long> a(100007, 0), x(100007, 0); n   for (int i = (1); i <= (n); i++) scanf( %lld , &a[i]); n   for (int i = (1); i <= (n); i++) { n     x[i] = x[i - 1]; n     x[i] = max(x[i], a[i] + 1); n   } n   for (int i = n - 1; i >= 1; i--) { n     x[i] = max(x[i], x[i + 1] - 1); n   } n   long long sum = 0; n   for (int i = 1; i <= n; i++) { n     sum += x[i] - a[i] - 1; n   } n   printf( %lld n , sum); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void write(int num) { n   if (num >= 10) write(num / 10); n   putchar(num % 10 +  0 ); n } n string s; n vector<int> l, r; n int id[100001]; n vector<int> v[100001]; n vector<int> ll, lr, rl, rr; n int main() { n   cin >> s; n   int cnt = 0; n   for (int i = 0; i < s.size(); i++) switch (s[i]) { n       case  L : { n         if (r.size()) n           id[i] = *(id + *(r.end() - 1)), r.pop_back(); n         else n           id[i] = ++cnt; n         l.push_back(i); n         break; n       } n       case  R : { n         if (l.size()) n           id[i] = *(id + *(l.end() - 1)), l.pop_back(); n         else n           id[i] = ++cnt; n         r.push_back(i); n         break; n       } n     } n   for (int i = 0; i < s.size(); i++) v[id[i]].push_back(i); n   for (int i = 1; i <= cnt; i++) { n     if (s[v[i][0]] ==  L  && s[*(v[i].end() - 1)] ==  L ) ll.push_back(i); n     if (s[v[i][0]] ==  L  && s[*(v[i].end() - 1)] ==  R ) lr.push_back(i); n     if (s[v[i][0]] ==  R  && s[*(v[i].end() - 1)] ==  L ) rl.push_back(i); n     if (s[v[i][0]] ==  R  && s[*(v[i].end() - 1)] ==  R ) rr.push_back(i); n   } n   write(cnt - 1), putchar(  n ); n   while (ll.size() && rr.size()) { n     int x = *(ll.end() - 1), y = *(rr.end() - 1); n     for (int i = 0; i < v[y].size(); i++) v[x].push_back(v[y][i]); n     lr.push_back(x), ll.pop_back(), rr.pop_back(); n   } n   while (ll.size() && lr.size()) { n     int x = *(lr.end() - 1), y = *(ll.end() - 1); n     for (int i = 0; i < v[y].size(); i++) v[x].push_back(v[y][i]); n     *(ll.end() - 1) = x, lr.pop_back(); n   } n   while (ll.size() && rl.size()) { n     int x = *(ll.end() - 1), y = *(rl.end() - 1); n     for (int i = 0; i < v[y].size(); i++) v[x].push_back(v[y][i]); n     rl.pop_back(); n   } n   while (rr.size() && rl.size()) { n     int x = *(rl.end() - 1), y = *(rr.end() - 1); n     for (int i = 0; i < v[y].size(); i++) v[x].push_back(v[y][i]); n     *(rr.end() - 1) = x, rl.pop_back(); n   } n   while (rr.size() && lr.size()) { n     int x = *(rr.end() - 1), y = *(lr.end() - 1); n     for (int i = 0; i < v[y].size(); i++) v[x].push_back(v[y][i]); n     lr.pop_back(); n   } n   if (ll.size() || rr.size()) { n     int x; n     if (ll.size()) n       x = ll[0]; n     else n       x = rr[0]; n     for (int i = 0; i < v[x].size(); i++) write(v[x][i] + 1), putchar(   ); n     putchar(  n ); n     return 0; n   } n   if (lr.size()) { n     int x = lr[0]; n     while (lr.size() >= 2) { n       int y = *(lr.end() - 1); n       for (int i = 0; i < v[y].size(); i++) v[x].push_back(v[y][i]); n       lr.pop_back(); n     } n   } n   if (rl.size()) { n     int x = rl[0]; n     while (rl.size() >= 2) { n       int y = *(rl.end() - 1); n       for (int i = 0; i < v[y].size(); i++) v[x].push_back(v[y][i]); n       rl.pop_back(); n     } n   } n   if (!(lr.size() && rl.size())) { n     int x; n     if (lr.size()) n       x = lr[0]; n     else n       x = rl[0]; n     for (int i = 0; i < v[x].size(); i++) write(v[x][i] + 1), putchar(   ); n     putchar(  n ); n     return 0; n   } n   int x = lr[0], y = rl[0]; n   if (*(v[x].end() - 1) > *(v[y].end() - 1)) swap(x, y); n   v[x].push_back(*(v[y].end() - 1)), v[y].pop_back(); n   for (int i = 0; i < v[x].size(); i++) write(v[x][i] + 1), putchar(   ); n   for (int i = 0; i < v[y].size(); i++) write(v[y][i] + 1), putchar(   ); n   putchar(  n ); n } n


#include <bits/stdc++.h> n using namespace std; n vector<int> e[200100], g[200100]; n int n, m, q; n int low[200100], dfn[200100], sta[200100], idx, top; n bool in[200100]; n void tarjan(int u, int pre) { n   low[u] = dfn[u] = ++idx; n   sta[top++] = u; n   in[u] = true; n   for (auto v : g[u]) { n     if (v == pre) continue; n     if (!dfn[v]) { n       tarjan(v, u); n       if (low[u] > low[v]) low[u] = low[v]; n       if (low[v] >= dfn[u]) { n         ++m; n         e[u].push_back(m); n         e[m].push_back(u); n         int vn; n         do { n           vn = sta[--top]; n           e[vn].push_back(m); n           e[m].push_back(vn); n           in[vn] = 0; n         } while (vn != v); n       } n     } else if (in[v] && low[u] > dfn[v]) n       low[u] = dfn[v]; n   } n } n int fa[200100][20], dep[200100], dis[200100]; n void dfs(int u, int pre) { n   fa[u][0] = pre; n   for (int i = 1; i < 20; ++i) fa[u][i] = fa[fa[u][i - 1]][i - 1]; n   dep[u] = dep[pre] + 1; n   dis[u] = dis[pre] + (u <= n); n   for (auto v : e[u]) n     if (v != pre) dfs(v, u); n } n int LCA(int u, int v) { n   if (dep[u] > dep[v]) swap(u, v); n   int hu = dep[u], hv = dep[v]; n   int tu = u, tv = v; n   for (int det = hv - hu, i = 0; det; det >>= 1, i++) n     if (det & 1) tv = fa[tv][i]; n   if (tu == tv) return tu; n   for (int i = 19; i >= 0; --i) { n     if (fa[tu][i] == fa[tv][i]) continue; n     tu = fa[tu][i]; n     tv = fa[tv][i]; n   } n   return fa[tu][0]; n } n int main() { n   scanf( %d%d%d , &n, &m, &q); n   while (m--) { n     int u, v; n     scanf( %d%d , &u, &v); n     g[u].push_back(v); n     g[v].push_back(u); n   } n   m = n; n   tarjan(1, -1); n   dfs(1, 1); n   while (q--) { n     int u, v; n     scanf( %d%d , &u, &v); n     int lca = LCA(u, v); n     int ans = dis[u] + dis[v] - 2 * dis[lca] - (lca > n); n     printf( %d n , ans); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long int a[1000001], has[1000001]; n int main() { n   long long int n, i; n   cin >> n; n   for (i = 0; i < n; i++) { n     cin >> a[i]; n     has[a[i]] = 1; n   } n   long long int c = 0; n   for (i = 1; i <= n; i++) n     if (has[i]) c++; n   cout << n - c; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 2e5 + 100; n const long long inf = 1e18; n vector<int> g[maxn]; n bool visit[maxn]; n int cnt = 0; n void dfs(int u) { n   cnt = 0; n   queue<int> q; n   q.push(u); n   visit[u] = true; n   cnt++; n   while (!q.empty()) { n     int u = q.front(); n     q.pop(); n     for (auto v : g[u]) n       if (visit[v] == false) { n         q.push(v); n         visit[v] = true; n         cnt++; n       } n   } n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   ; n   int n, m; n   cin >> n >> m; n   vector<int> st; n   int u, v; n   memset(visit, false, sizeof visit); n   for (int i = 1; i <= m; i++) { n     cin >> u >> v; n     g[u].push_back(v); n     g[v].push_back(u); n     st.push_back(u); n     st.push_back(v); n   } n   int ans = 0; n   for (auto v : st) { n     if (visit[v] == false) { n       dfs(v); n       ans += (cnt - 1); n       cnt = 0; n     } n   } n   cout << (m - ans) << endl; n   return 0; n } n


#include <bits/stdc++.h> n #pragma comment(linker,  /STACK:200000000 ) n using namespace std; n const long long oo = 1LL << 60; n const long long kNumMoves = 4; n const long long kMoves[kNumMoves][2] = {{1, 0}, {-1, 0}, {0, -1}, {0, 1}}; n int n, v, e; n vector<int> a, b; n vector<vector<int> > conn, edges; n vector<int> ansX, ansY, ansD; n vector<int> topSort; n vector<int> seen; n void DFS_Topological(int node) { n   if (seen[node]) return; n   seen[node] = 1; n   for (int i = 0; i < int(n); ++i) { n     if (edges[node][i] && !seen[i]) { n       DFS_Topological(i); n       conn[node].push_back(i); n       conn[i].push_back(node); n     } n   } n   topSort.push_back(node); n } n int AddTransfusionStep(int src, int dest, int delta) { n   int mxMoveOut = min(a[src], delta); n   int mxMoveIn = min(v - a[dest], mxMoveOut); n   a[src] -= mxMoveIn; n   a[dest] += mxMoveIn; n   if (mxMoveIn) n     ansX.push_back(src + 1), ansY.push_back(dest + 1), ansD.push_back(mxMoveIn); n   return mxMoveIn; n } n int Transfuse_Out(int node, int dest, int delta) { n   if (!delta || seen[node] || seen[dest]) return 0; n   int step1 = AddTransfusionStep(node, dest, delta); n   if (step1 == delta) return delta; n   for (int i = 0; i < int(conn[dest].size()); ++i) { n     int newDest = conn[dest][i]; n     if (newDest == node) continue; n     if (seen[newDest]) continue; n     Transfuse_Out(dest, newDest, v); n   } n   int step2 = AddTransfusionStep(node, dest, delta - step1); n   return step1 + step2; n } n int Transfuse_In(int node, int dest, int delta) { n   if (!delta || seen[node] || seen[dest]) return 0; n   int step1 = AddTransfusionStep(dest, node, delta); n   if (step1 == delta) return delta; n   for (int i = 0; i < int(conn[dest].size()); ++i) { n     int newDest = conn[dest][i]; n     if (newDest == node) continue; n     if (seen[newDest]) continue; n     Transfuse_In(dest, newDest, v); n   } n   int step2 = AddTransfusionStep(dest, node, delta - step1); n   return step1 + step2; n } n bool Transfuse(int node) { n   int delta = a[node] - b[node]; n   if (delta > 0) { n     for (int i = 0; i < int(conn[node].size()); ++i) { n       int dest = conn[node][i]; n       if (seen[dest]) continue; n       delta -= Transfuse_Out(node, dest, delta); n     } n     return (delta == 0 && a[node] == b[node]); n   } else if (delta < 0) { n     delta = -delta; n     for (int i = 0; i < int(conn[node].size()); ++i) { n       int dest = conn[node][i]; n       if (seen[dest]) continue; n       delta -= Transfuse_In(node, dest, delta); n     } n     return (delta == 0 && a[node] == b[node]); n   } n   return true; n } n bool FindTransfusions() { n   seen.clear(); n   seen.resize(n, 0); n   for (int i = 0; i < int(n); ++i) DFS_Topological(i); n   seen.clear(); n   seen.resize(n, 0); n   for (int i = 0; i < int(n); ++i) { n     if (!Transfuse(topSort[i])) return false; n     seen[topSort[i]] = 1; n   } n   return true; n } n int main() { n   cin >> n >> v >> e; n   a.resize(n), b.resize(n); n   for (int i = 0; i < int(n); ++i) cin >> a[i]; n   for (int i = 0; i < int(n); ++i) cin >> b[i]; n   conn.resize(n); n   edges.resize(n, vector<int>(n, 0)); n   for (int i = 0; i < int(e); ++i) { n     int x, y; n     cin >> x >> y; n     --x, --y; n     edges[x][y] = edges[y][x] = 1; n   } n   if (FindTransfusions()) { n     cout << ansX.size() << endl; n     for (int i = 0; i < int(ansX.size()); ++i) n       cout << ansX[i] <<     << ansY[i] <<     << ansD[i] << endl; n   } else { n     cout <<  NO ; n   } n   int i; n   cin >> i; n   return 0; n } n