#include <bits/stdc++.h> n using namespace std; n const long long N = 1110; n const long long inf = 1000000007998244353ll; n struct S { n   long long l, r; n } a[N * N]; n long long si[N][N]; n long long tot; n long long n, m, k; n string s; n long long lcp[N][N]; n long long dp[N][N]; n long long suffix_sum[N]; n long long ri[N]; n inline long long add(long long a, long long b) { n   return a + b > inf ? inf : a + b; n } n long long cnt(long long S) { n   for (long long i = 0; i < n; i++) { n     ri[i] = i; n     while (ri[i] != n && si[i][ri[i]] <= S) { n       ri[i]++; n     } n   } n   for (long long i = 0; i < n; i++) { n     dp[i][1] = si[i][n - 1] > si[a[S].l][a[S].r] ? 1 : 0; n   } n   for (long long j = 2; j <= m; j++) { n     suffix_sum[n - 1] = dp[n - 1][j - 1]; n     for (long long i = n - 2; i >= 0; i--) { n       suffix_sum[i] = add(dp[i][j - 1], suffix_sum[i + 1]); n     } n     for (long long i = 0; i < n; i++) { n       dp[i][j] = ri[i] == n ? 0 : suffix_sum[ri[i] + 1]; n     } n   } n   return dp[0][m]; n } n bool cmp(S i, S j) { n   long long p = i.l + lcp[i.l][j.l], q = j.l + lcp[i.l][j.l]; n   if (p > i.r || q > j.r) { n     return i.r - i.l < j.r - j.l; n   } n   return s[p] < s[q]; n } n signed main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   tot = 0; n   cin >> n >> m >> k >> s; n   for (long long i = 0; i < n; i++) { n     for (long long j = i; j < n; j++) { n       a[tot].l = i; n       a[tot].r = j; n       tot++; n     } n   } n   for (long long i = n - 1; i >= 0; i--) { n     for (long long j = n - 1; j >= 0; j--) { n       lcp[i][j] = s[i] == s[j] ? lcp[i + 1][j + 1] + 1 : 0; n     } n   } n   sort(a, a + tot, cmp); n   for (long long i = 0; i < tot; i++) { n     si[a[i].l][a[i].r] = i; n   } n   long long l = 0, r = tot - 1; n   while (l < r) { n     long long midd = (r + l) / 2; n     if (cnt(midd) >= k) { n       l = midd + 1; n     } else { n       r = midd; n     } n   } n   for (long long i = a[r].l; i <= a[r].r; i++) { n     cout << s[i]; n   } n   return 0; n } n


#include <bits/stdc++.h> n char ch; n inline void read(int &x) { n   x = 0; n   ch = getchar(); n   while (ch <= 32) ch = getchar(); n   while (ch > 32) { n     x = x * 10 + ch - 48; n     ch = getchar(); n   }; n }; n const int dx[8] = {0, 1, 0, -1, -1, -1, 1, 1}; n const int dy[8] = {1, 0, -1, 0, -1, 1, -1, 1}; n const int D_cnt = 8; n const int K1 = 3, K2 = 6; n int n, m; n bool a[1605][1605]; n bool b[1605][1605]; n char dist[1605][1605]; n int l1[1605 * 1605], l2[1605 * 1605]; n int s, t; n inline void push(int x, int y) { n   ++t; n   l1[t] = x; n   l2[t] = y; n }; n inline void bfs1() { n   s = t = 0; n   int i, j; n   for (i = 1; i <= n; i++) { n     for (j = 1; j <= m; j++) { n       if (!b[i][j]) { n         push(i, j); n         dist[i][j] = 0; n       }; n     }; n   }; n   while (s < t) { n     int tx, ty; n     ++s; n     tx = l1[s]; n     ty = l2[s]; n     if (dist[tx][ty] == K1) continue; n     for (i = 0; i < D_cnt; i++) { n       int nx = tx + dx[i], ny = ty + dy[i]; n       if (nx < 1 || nx > n || ny < 1 || ny > m) continue; n       if (!b[nx][ny]) continue; n       b[nx][ny] = 0; n       dist[nx][ny] = dist[tx][ty] + 1; n       push(nx, ny); n     }; n   }; n }; n inline void bfs2() { n   s = t = 0; n   int i, j; n   for (i = 1; i <= n; i++) { n     for (j = 1; j <= m; j++) { n       if (b[i][j]) { n         push(i, j); n         dist[i][j] = 0; n       }; n     }; n   }; n   while (s < t) { n     int tx, ty; n     ++s; n     tx = l1[s]; n     ty = l2[s]; n     if (dist[tx][ty] == K2) continue; n     for (i = 0; i < D_cnt; i++) { n       int nx = tx + dx[i], ny = ty + dy[i]; n       if (nx < 1 || nx > n || ny < 1 || ny > m) continue; n       if (b[nx][ny]) continue; n       b[nx][ny] = 1; n       dist[nx][ny] = dist[tx][ty] + 1; n       push(nx, ny); n     }; n   }; n }; n int _cnt; n int visit[1605][1605]; n int ll1[1605 * 1605], ll2[1605 * 1605]; n inline void fill(int x0, int y0) { n   s = t = 0; n   bool _b = b[x0][y0]; n   visit[x0][y0] = ++_cnt; n   push(x0, y0); n   while (s < t) { n     int tx, ty; n     ++s; n     tx = l1[s]; n     ty = l2[s]; n     int i; n     for (i = 0; i < D_cnt; i++) { n       int nx = tx + dx[i], ny = ty + dy[i]; n       if (nx < 1 || nx > n || ny < 1 || ny > m) continue; n       if (visit[nx][ny] == _cnt) continue; n       if (!a[nx][ny] || b[nx][ny] != _b) continue; n       visit[nx][ny] = _cnt; n       push(nx, ny); n     }; n   }; n }; n int ans[10000]; n int cnt; n inline void check(int x0, int y0) { n   s = t = 0; n   push(x0, y0); n   visit[x0][y0] = ++_cnt; n   while (s < t) { n     int tx, ty; n     ++s; n     tx = l1[s]; n     ty = l2[s]; n     int i; n     for (i = 0; i < D_cnt; i++) { n       int nx = tx + dx[i], ny = ty + dy[i]; n       if (nx < 1 || nx > n || ny < 1 || ny > m) continue; n       if (visit[nx][ny] == _cnt) continue; n       if (!a[nx][ny]) continue; n       visit[nx][ny] = _cnt; n       push(nx, ny); n     }; n   }; n   memcpy(ll1, l1, (t + 1) * sizeof(int)); n   memcpy(ll2, l2, (t + 1) * sizeof(int)); n   int tmp_cnt = _cnt; n   int tmp = 0; n   int i; n   int T = t; n   for (i = 1; i <= T; i++) { n     int tx = ll1[i], ty = ll2[i]; n     if (visit[tx][ty] == tmp_cnt) { n       ++tmp; n       fill(tx, ty); n     }; n   }; n   ans[++cnt] = tmp - 1; n }; n int main() { n   read(n); n   read(m); n   int i, j; n   for (i = 1; i <= n; i++) { n     for (j = 1; j <= m; j++) { n       int x; n       read(x); n       a[i][j] = x; n     }; n   }; n   memcpy(b, a, sizeof(a)); n   bfs1(); n   bfs2(); n   for (i = 1; i <= n; i++) { n     for (j = 1; j <= m; j++) { n       if (a[i][j] && !visit[i][j]) { n         check(i, j); n       }; n     }; n   }; n   std::sort(ans + 1, ans + cnt + 1); n   printf( %d n , cnt); n   for (i = 1; i <= cnt; i++) { n     printf( %d  , ans[i]); n   }; n } n


#include <bits/stdc++.h> n using namespace std; n using ll = long long; n bool cmp(pair<ll, ll> a, pair<ll, ll> b) { return a.second > b.second; } n bool cmp2(vector<ll> a, vector<ll> b) { return a.size() > b.size(); } n int main() { n   ll n, m; n   cin >> n >> m; n   vector<pair<ll, ll>> vp(n); n   for (ll i = 0; i < n; i++) { n     ll s, r; n     cin >> s >> r; n     s--; n     vp[i] = make_pair(s, r); n   } n   sort(vp.begin(), vp.end(), cmp); n   vector<vector<ll>> g(m, vector<ll>(1)); n   ll mxsz = 0; n   for (ll i = 0; i < n; i++) { n     g[vp[i].first].push_back(vp[i].second + g[vp[i].first].back()); n     mxsz = max(mxsz, (ll)g[vp[i].first].size()); n   } n   sort(g.begin(), g.end(), cmp2); n   ll ans = 0; n   for (ll i = 0; i < mxsz; i++) { n     ll now = 0; n     vector<ll> hoge; n     for (ll j = 0; j < m; j++) { n       if (i < g[j].size()) { n         if (g[j][i] > 0) now += g[j][i]; n       } n       if (g[j].size() < i) break; n     } n     ans = max(ans, now); n   } n   cout << max(0LL, ans) << endl; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxN = 2000; n int main() { n   ios_base::sync_with_stdio(0); n   int n; n   cin >> n; n   for (int i = 1; i <= n; i++) { n     long long l, r; n     cin >> l >> r; n     int ans = 0; n     long long mn = r; n     long long cur = 0; n     int cnt = 0; n     for (int i = 60; i >= 0; i--) { n       long long tmp = r; n       if (r & (1ll << i)) { n         if (cur + (1ll << i) - 1 >= l) { n           if (ans < cnt + i) { n             mn = cur + (1ll << i) - 1; n             ans = cnt + i; n           } n         } n         cnt++; n         cur += (1ll << i); n       } n     } n     if (cnt > ans) { n       ans = cnt; n       mn = r; n     } n     cout << mn <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1 << 18; n int uf[N]; n int cost[N]; n vector<pair<int, int>> h_uf; n vector<pair<int, int>> h_cost; n pair<int, int> query[N]; n int ans[N]; n bool sample; n struct edge { n   int u, v, w; n   edge(int u, int v, int w) : u(u), v(v), w(w) {} n }; n vector<edge> seg[N * 2]; n int find(int u) { n   while (uf[u] >= 0) { n     u = uf[u]; n   } n   return u; n } n int find_cost(int u) { n   int res = 0; n   while (uf[u] >= 0) { n     res ^= cost[u]; n     u = uf[u]; n   } n   return res; n } n bool unite(int u, int v, int w) { n   int pu = find(u); n   int pv = find(v); n   if (pu == pv) return false; n   w ^= find_cost(u); n   w ^= find_cost(v); n   u = pu; n   v = pv; n   if (-uf[u] < -uf[v]) swap(u, v); n   h_uf.emplace_back(u, uf[u]); n   h_uf.emplace_back(v, uf[v]); n   h_cost.emplace_back(v, cost[v]); n   uf[u] += uf[v]; n   uf[v] = u; n   cost[v] = w; n   return true; n } n void undo() { n   auto p = h_uf.back(); n   h_uf.pop_back(); n   auto q = h_uf.back(); n   h_uf.pop_back(); n   auto r = h_cost.back(); n   h_cost.pop_back(); n   uf[p.first] = p.second; n   uf[q.first] = q.second; n   cost[r.first] = r.second; n } n int find_cost(int u, int v) { return find_cost(u) ^ find_cost(v); } n void init_uf(int n) { n   for (int i = 0; i < n; i++) { n     uf[i] = -1; n   } n } n void insert(int l, int r, edge e, int k = 1, int ll = 0, int rr = N) { n   if (rr <= l || r <= ll) return; n   if (l <= ll && rr <= r) { n     seg[k].emplace_back(e); n     return; n   } n   insert(l, r, e, k * 2 + 0, ll, ll + rr >> 1); n   insert(l, r, e, k * 2 + 1, ll + rr >> 1, rr); n } n vector<int> normalize(vector<int> a) { n   int r = 0; n   for (int j = 29; j >= 0; j--) { n     int p = -1; n     for (int i = r; i < a.size(); i++) { n       if (a[i] >> j & 1) { n         p = i; n         break; n       } n     } n     if (p == -1) continue; n     swap(a[r], a[p]); n     for (int i = r + 1; i < a.size(); i++) { n       if (a[i] >> j & 1) { n         a[i] ^= a[r]; n       } n     } n     r++; n   } n   a.resize(r); n   return a; n } n int find_min(int x, vector<int> &a) { n   for (int y : a) x = min(x, x ^ y); n   return x; n } n void dfs(vector<int> a, int k = 1, int l = 0, int r = N) { n   int cnt = 0; n   for (edge e : seg[k]) { n     if (unite(e.u, e.v, e.w)) { n       cnt++; n     } else { n       a.push_back(e.w ^ find_cost(e.u, e.v)); n     } n   } n   a = normalize(a); n   if (r - l == 1) { n     if (query[l].second != 0) { n       ans[l] = find_min(find_cost(query[l].first, query[l].second), a); n     } n     return; n   } n   dfs(a, k * 2 + 0, l, l + r >> 1); n   dfs(a, k * 2 + 1, l + r >> 1, r); n   while (cnt--) undo(); n } n int input() { n   int n; n   scanf( %d , &n); n   return n; n } n int main() { n   int n, m; n   cin >> n >> m; n   init_uf(n); n   map<pair<int, int>, pair<int, int>> mp; n   for (int i = 0; i < m; i++) { n     int u = input() - 1; n     int v = input() - 1; n     int w = input(); n     mp[{u, v}] = {w, 0}; n   } n   int q = input(); n   for (int i = 1; i <= q; i++) { n     int type = input(); n     int u = input() - 1; n     int v = input() - 1; n     if (type == 1) { n       int w = input(); n       mp[{u, v}] = {w, i}; n     } else if (type == 2) { n       auto it = mp.find({u, v}); n       insert(it->second.second, i, edge(u, v, it->second.first)); n       mp.erase(it); n     } else { n       query[i] = {u, v}; n     } n   } n   for (auto p : mp) { n     int u = p.first.first; n     int v = p.first.second; n     int w = p.second.first; n     int s = p.second.second; n     insert(s, q + 1, edge(u, v, w)); n   } n   sample = n <= 10; n   dfs({}); n   for (int i = 1; i <= q; i++) { n     if (query[i].second != 0) { n       printf( %d n , ans[i]); n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int Maxn = 200010; n const int inf = 2147483647; n const double pi = acos(-1.0); n int read() { n   int x = 0, f = 1; n   char ch = getchar(); n   while (ch <  0  || ch >  9 ) { n     if (ch ==  - ) f = -1; n     ch = getchar(); n   } n   while (ch >=  0  && ch <=  9 ) x = x * 10 + ch -  0 , ch = getchar(); n   return x * f; n } n int n, pre[Maxn], nxt[Maxn], ed; n long long a[Maxn], sum[Maxn]; n void ins(int l, int m, int r) { n   pre[m] = l, nxt[m] = r; n   if (l != -1) nxt[l] = m; n   if (r != -1) pre[r] = m; n } n int main() { n   n = read(); n   sum[0] = 0; n   for (int i = 1; i <= n; i++) a[i] = read(), sum[i] = sum[i - 1] + a[i]; n   int ans = 0; n   pre[1] = nxt[1] = -1; n   ed = 1; n   for (int i = 2; i <= n; i++) { n     int t = ed, st; n     bool flag = false; n     while (t != -1) { n       if (!flag && a[t] + sum[t] < sum[i] + a[i]) { n         flag = true; n         if (nxt[t] == -1) ed = i; n         ins(t, i, nxt[t]); n       } n       if (a[t] + sum[t] >= sum[i - 1] - a[i]) { n         if ((a[i] ^ a[t]) == sum[i - 1] - sum[t] && i - t >= 2) ans++; n       } else n         break; n       st = t; n       t = pre[t]; n     } n     if (!flag) ins(-1, i, st); n   } n   printf( %d , ans); n } n


#include <bits/stdc++.h> n using namespace std; n vector<long long> Edge[100010]; n long long ar[100010]; n bool Visit[100010 + 7]; n bool InStk[100010 + 7]; n long long Low[100010 + 7], I; n long long Ind[100010 + 7]; n stack<long long> Stk; n long long ans1, ans2; n void SCC(long long u) { n   Visit[u] = true; n   InStk[u] = true; n   Ind[u] = ++I; n   Low[u] = I; n   Stk.push(u); n   for (int i = 0; i < Edge[u].size(); i++) { n     long long v = Edge[u][i]; n     if (!Visit[v]) { n       SCC(v); n       Low[u] = min(Low[u], Low[v]); n     } else if (InStk[v]) { n       Low[u] = min(Low[u], Ind[v]); n     } n   } n   if (Low[u] != Ind[u]) return; n   long long mn = LONG_LONG_MAX, num = 0; n   while (Stk.top() != u) { n     long long v = Stk.top(); n     if (mn > ar[v]) { n       mn = ar[v]; n       num = 1; n     } else if (mn == ar[v]) { n       num++; n     } n     Stk.pop(); n     InStk[v] = false; n   } n   long long v = Stk.top(); n   if (mn > ar[v]) { n     mn = ar[v]; n     num = 1; n   } else if (mn == ar[v]) { n     num++; n   } n   ans2 *= num; n   ans2 %= 1000000007; n   ans1 += mn; n   Stk.pop(); n   InStk[u] = false; n } n int main() { n   int n, m; n   while (cin >> n) { n     for (int i = 0; i < 100010; ++i) Edge[i].clear(); n     for (int i = 0; i < n; ++i) scanf( %I64d , &ar[i + 1]); n     cin >> m; n     long long u, v; n     for (int i = 0; i < m; ++i) { n       scanf( %I64d %I64d , &u, &v); n       Edge[u].push_back(v); n     } n     memset(Visit, 0, sizeof(Visit)); n     memset(InStk, 0, sizeof(InStk)); n     memset(Low, 0, sizeof(Low)); n     memset(Ind, 0, sizeof(Ind)); n     ans1 = 0; n     ans2 = 1; n     for (int i = 1; i <= n; i++) { n       if (Visit[i]) continue; n       SCC(i); n     } n     cout << ans1 <<     << ans2 << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<int> arr; n int maxNum = -1; n int n; n bool vis[1000000 + 50]; n int main() { n   memset(vis, false, sizeof(vis)); n   scanf( %d , &(n)); n   for (int i = 0; i < n; i++) { n     int tmp; n     scanf( %d , &(tmp)); n     if (!vis[tmp]) { n       arr.push_back(tmp); n       vis[tmp] = true; n       if (tmp > maxNum) { n         maxNum = tmp; n       } n     } n   } n   n = arr.size(); n   if (arr.size() == 1) { n     printf( 0 n ); n     return 0; n   } n   sort(arr.begin(), arr.end()); n   int ans = 0; n   for (int i = 0; i < n; i++) { n     int num = arr[i]; n     int t = 2; n     while (1) { n       int pos = lower_bound(arr.begin(), arr.end(), num * t) - arr.begin() - 1; n       int tmp = arr[pos] % num; n       if (tmp > ans) { n         ans = tmp; n       } n       if (pos == n - 1) { n         break; n       } n       t++; n     } n   } n   printf( %d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e6; n int n, a[N * 2], b[N * 2]; n pair<int, int> r[N]; n int main() { n   int n, q; n   scanf( %d%d , &n, &q); n   for (int i = 0; i < n; i++) { n     scanf( %d , &a[i]); n     a[n + i] = a[i]; n   } n   while (q--) { n     long long sum; n     scanf( %lld , &sum); n     int ans = 1e9, high = -1; n     long long w = 0; n     for (int i = 0; i < n; i++) { n       for (; high + 1 < n * 2 && w + a[high + 1] <= sum;) { n         w += a[++high]; n       } n       w -= a[i]; n       b[i] = high + 1; n     } n     for (int i = n - 1; i >= 0; i--) { n       if (b[i] >= n) { n         r[i] = {b[i], 1}; n       } else { n         r[i] = {r[b[i]].first, r[b[i]].second + 1}; n       } n       if (r[i].first - n >= i) { n         ans = min(ans, r[i].second); n       } n     } n     printf( %d n , ans); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n ofstream fo( test.out ); n ifstream fi( test.inp ); n int n, m, k, dem; n int x[200005], y[200005]; n int main() { n   long long a; n   cin >> a; n   for (long long i = 2; i * i <= a; i++) { n     while (a % (i * i) == 0) a /= i; n   } n   cout << a; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 205; n const int maxm = 4005; n int n, m, p, s, t, ss, tt; n namespace Flow { n int tot, S, T, V; n int head[maxn], nxt[maxm * 2], to[maxm * 2], cap[maxm * 2], cost[maxm * 2]; n int dist[maxn], parv[maxn], pare[maxn]; n bool in[maxn]; n queue<int> q; n void init() { n   tot = 0; n   memset(head, -1, sizeof(head)); n } n void addedge(int u, int v, int c, int co) { n   nxt[tot] = head[u]; n   to[tot] = v; n   cap[tot] = c; n   cost[tot] = co; n   head[u] = tot++; n   nxt[tot] = head[v]; n   to[tot] = u; n   cap[tot] = 0; n   cost[tot] = -co; n   head[v] = tot++; n } n int flow() { n   for (int i = 0; i < (V); i++) in[i] = false, dist[i] = int(1e9); n   dist[S] = 0; n   in[S] = true; n   q.push(S); n   while (!q.empty()) { n     int x = q.front(); n     q.pop(); n     in[x] = false; n     for (int i = head[x]; ~i; i = nxt[i]) { n       if (cap[i] && dist[to[i]] > dist[x] + cost[i]) { n         dist[to[i]] = dist[x] + cost[i]; n         parv[to[i]] = x; n         pare[to[i]] = i; n         if (!in[to[i]]) { n           in[to[i]] = true; n           q.push(to[i]); n         } n       } n     } n   } n   if (dist[T] == int(1e9)) return -int(1e9); n   int res = 0; n   for (int i = T; i != S; i = parv[i]) { n     res += cost[pare[i]]; n     cap[pare[i]]--; n     cap[pare[i] ^ 1]++; n   } n   return res; n } n }  // namespace Flow n int main() { n   scanf( %d %d , &n, &m); n   s = 1, t = n; n   Flow::init(); n   ss = 0; n   tt = n + 1; n   Flow::S = n + 2; n   Flow::T = Flow::S + 1; n   Flow::V = Flow::T + 1; n   Flow::addedge(Flow::S, tt, int(1e9), 0); n   Flow::addedge(t, Flow::T, int(1e9), 0); n   Flow::addedge(ss, s, int(1e9), 0); n   Flow::addedge(Flow::S, s, int(1e9), 0); n   for (int i = 1; i <= (m); i++) { n     int u, v, d, c; n     scanf( %d%d%d , &u, &v, &d); n     Flow::addedge(u, v, 1, d); n   } n   for (int i = 1; i <= (n); i++) Flow::addedge(i, tt, int(1e9), 0); n   vector<int> data; n   for (;;) { n     int f = Flow::flow(); n     if (f == -int(1e9)) break; n     data.push_back(f); n   } n   int q; n   scanf( %d , &q); n   while (q--) { n     int p; n     scanf( %d , &p); n     long long s = 0; n     int c = 0; n     double ans = 1e18; n     for (int i = 0; i < data.size(); i++) { n       s += data[i]; n       c++; n       double cur = double(s + p) / double(c); n       if (cur < ans) n         ans = cur; n       else n         break; n     } n     printf( %.10lf n , ans); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int LEN = 100000; n struct fastio { n   int it, len; n   char s[LEN + 5]; n   fastio() { it = len = 0; } n   char get() { n     if (it < len) return s[it++]; n     it = 0, len = fread(s, 1, LEN, stdin); n     return len ? s[it++] : EOF; n   } n   bool notend() { n     char c; n     for (c = get(); c ==     || c ==   n  || c ==   r ; c = get()) n       ; n     if (it) it--; n     return c != EOF; n   } n   void put(char c) { n     if (it == LEN) fwrite(s, 1, LEN, stdout), it = 0; n     s[it++] = c; n   } n   void flush() { fwrite(s, 1, it, stdout); } n } buff, bufo; n inline int getint() { n   char c; n   int res = 0, sig = 1; n   for (c = buff.get(); c <  0  || c >  9 ; c = buff.get()) n     if (c ==  - ) sig = -1; n   for (; c >=  0  && c <=  9 ; c = buff.get()) res = res * 10 + (c -  0 ); n   return sig * res; n } n inline long long getll() { n   char c; n   long long res = 0, sig = 1; n   for (c = buff.get(); c <  0  || c >  9 ; c = buff.get()) n     if (c ==  - ) sig = -1; n   for (; c >=  0  && c <=  9 ; c = buff.get()) res = res * 10 + (c -  0 ); n   return sig * res; n } n inline void putint(int x, char suf) { n   if (!x) n     bufo.put( 0 ); n   else { n     if (x < 0) bufo.put( - ), x = -x; n     int k = 0; n     char s[15]; n     while (x) { n       s[++k] = x % 10 +  0 ; n       x /= 10; n     } n     for (; k; k--) bufo.put(s[k]); n   } n   bufo.put(suf); n } n inline void putll(long long x, char suf) { n   if (!x) n     bufo.put( 0 ); n   else { n     if (x < 0) bufo.put( - ), x = -x; n     int k = 0; n     char s[25]; n     while (x) { n       s[++k] = x % 10 +  0 ; n       x /= 10; n     } n     for (; k; k--) bufo.put(s[k]); n   } n   bufo.put(suf); n } n inline char get_char() { n   char c; n   for (c = buff.get(); c ==     || c ==   n  || c ==   r ; c = buff.get()) n     ; n   return c; n } n int a[200005]; n void solve() { n   int n = getint(); n   for (int i = 1; i <= n; i++) a[i] = getint(); n   long long ans = 0; n   for (int i = 1; i < n; i++) n     if (a[i] > a[i + 1]) ans += a[i] - a[i + 1]; n   putll(ans,   n ); n } n int main() { n   int T = getint(); n   while (T--) solve(); n   bufo.flush(); n   return 0; n } n


#include <bits/stdc++.h> n int main() { n   int a, b, c; n   scanf( %d%d%d , &a, &b, &c); n   int x = 30; n   while (a > 0 || b > 0 || c > 0) { n     switch (x % 3) { n       case 0: n         a -= 2; n         if (a < 0) a = 0; n         break; n       case 1: n         b -= 2; n         if (b < 0) b = 0; n         break; n       case 2: n         c -= 2; n         if (c < 0) c = 0; n         break; n     } n     x++; n   } n   printf( %d n , x - 1); n } n


#include <bits/stdc++.h> n using namespace std; n const int mod = 998244353; n int n, k, limit, f[1010][1 << 2][2010], sta[1 << 2], ans; n int main() { n   scanf( %d%d , &n, &k); n   limit = (1 << 2) - 1; n   sta[0] = 1; n   sta[1] = 2; n   sta[2] = 2; n   sta[3] = 1; n   for (int i = 0; i <= 3; i++) f[1][i][sta[i]] = 1; n   for (int i = 1; i < n; i++) n     for (int j = 0; j <= limit; j++) n       for (int l = 1; l <= k; l++) n         if (f[i][j][l]) { n           for (int p = 0; p <= limit; p++) { n             int temp = sta[p]; n             if (((p >> 0 & 1) == (j >> 0 & 1))) temp--; n             if ((p >> 1 & 1) == (j >> 1 & 1)) temp--; n             temp = max(0, temp); n             f[i + 1][p][l + temp] = (f[i + 1][p][l + temp] + f[i][j][l]) % mod; n           } n         } n   for (int i = 0; i <= limit; i++) ans = (ans + f[n][i][k]) % mod; n   printf( %d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n  n using namespace std; n const int N=3e5+10; n typedef long long ll; n int pos[N],a[N],q,n; n void  solve() n { n  cin>>n>>q; n  for(int i=1;i<=n;i++) cin>>a[i]; n  for(int i=n;i;i--)pos[a[i]]=i; n  while(q--) n  { n   int x; n   cin>>x; n   cout<<pos[x]<<   ; n   int t=pos[x]; n   pos[x]=1; n   for(int i=1;i<=50;i++) n   if(i!=x&&pos[i]<t) n   pos[i]++; n  } n   n } n int main() n { n  int t=1; n  while(t--) n  { n   solve(); n  } n  //system( pause ); n  return 0; n }


#include <iostream> n #include <algorithm> n  n constexpr int N = 100005; n  n int n; n struct node { n     long long a, b; n } a[N]; n  n int main() { n     std::ios::sync_with_stdio(false), std::cin.tie(nullptr); n     std::cin >> n; n     long long ans = 0; n     for (int i = 1; i <= n; ++i) std::cin >> a[i].a >> a[i].b, ans += a[i].b; n     std::sort(a + 1, a + n + 1, [](node a, node b) { return a.a < b.a; }); n     int now = 1; n     long long max = a[1].a + a[1].b; n     while (now <= n) { n         int x = now + 1; n         while (x <= n && a[x].a + a[x].b <= max) ++x; n         if (x > n) break; n         ans += std::max(a[x].a - max, 0ll); n         max = a[x].a + a[x].b; n         now = x; n     } n     std::cout << ans <<   n ; n     return 0; n } n


#include <bits/stdc++.h> n int n, m, k, flag, ans, cnt, p, q, w[1000005], f[1000005], g[1000005], n     s[1000005]; n int main() { n   scanf( %d%d%d , &n, &m, &k); n   w[0] = 1; n   for (int i = 1; i <= n; i++) w[i] = w[i - 1] * 2 % 1000000007; n   for (int i = 1; i <= m; i++) { n     int x, y; n     scanf( %d%d , &x, &y); n     if (x > y) std::swap(x, y); n     if (x + 1 == y) n       f[x] = 1; n     else if (x + k + 1 == y) n       g[x] = 1, ++cnt; n     else n       flag = 1; n   } n   if (flag) { n     puts( 0 ); n     return 0; n   } n   if (cnt == 0) { n     for (int i = 1; i <= n; i++) { n       if (i + k + 1 > n) continue; n       int up = (i + k + k + 1 > n ? n - k - 1 : i + k); n       int can = up - i + 1; n       ans = (ans + w[can - 1]) % 1000000007; n     } n     ++ans; n     printf( %d n , ans); n     return 0; n   } n   for (int i = 1; i <= n; i++) n     if (g[i]) q = i; n   for (int i = n; i >= 1; i--) n     if (g[i]) p = i; n   if (p + k + 1 <= q) { n     puts( 0 ); n     return 0; n   } n   for (int i = 1; i <= n; i++) s[i] = s[i - 1] + g[i]; n   for (int i = 1; i <= n; i++) { n     if (g[i] || i + k + 1 > n) continue; n     if (i >= p + k + 1 || i + k + 1 <= q) continue; n     int cp = p, cq = q; n     if (i < p) p = i; n     if (i > q) q = i; n     int up = (p + k + k + 1 > n ? n - k - 1 : p + k); n     int can = up - i + 1 - (s[up] - s[i - 1]); n     ans = (ans + w[can - 1]) % 1000000007; n     q = cq, p = cp; n   } n   ++ans; n   printf( %d n , ans); n } n


#include <bits/stdc++.h> n using namespace std; n const int mod = 998244353; n const int inf = 1 << 30; n const int maxn = 100000 + 5; n int tot, dfn[maxn << 1], ord[maxn << 1]; n namespace hld { n const int maxn = ::maxn << 1; n struct edge { n   int to, nxt; n } g[maxn]; n int head[maxn], ecnt = 0; n void add(int x, int y) { n   g[++ecnt] = {y, head[x]}; n   head[x] = ecnt; n } n int siz[maxn], dep[maxn], fa[maxn], son[maxn], top[maxn]; n void dfs(int p, int d, int old) { n   dfn[p] = ++tot; n   ord[tot] = p; n   dep[p] = d; n   fa[p] = old; n   siz[p] = 1; n   int m = -1; n   for (int i = head[p]; i; i = g[i].nxt) { n     int v = g[i].to; n     if (v == fa[p]) continue; n     dfs(v, d + 1, p); n     siz[p] += siz[v]; n     if (siz[v] > m) son[p] = v, m = siz[v]; n   } n } n void dfs(int p, int tp) { n   top[p] = tp; n   if (!son[p]) return; n   dfs(son[p], tp); n   for (int i = head[p]; i; i = g[i].nxt) { n     int v = g[i].to; n     if (v == fa[p] || v == son[p]) continue; n     dfs(v, v); n   } n } n void build(int rt = 1) { n   memset(son, 0, sizeof(son)); n   dfs(rt, rt, 0); n   dfs(rt, rt); n } n int qlca(int x, int y) { n   while (top[x] != top[y]) { n     if (dep[top[x]] < dep[top[y]]) swap(x, y); n     x = fa[top[x]]; n   } n   if (dep[x] > dep[y]) swap(x, y); n   return x; n } n }  // namespace hld n vector<int> edge[maxn << 1]; n struct sam { n   int sz, last, cnt[maxn << 1]; n   int len[maxn << 1], link[maxn << 1], ch[maxn << 1][26]; n   void clear() { n     for (int i = 1; i <= sz; i++) { n       len[i] = link[i] = cnt[i] = 0; n       memset(ch[i], 0, sizeof(ch[i])); n     } n     sz = last = 1; n   } n   sam() { clear(); } n   int insert(int c) { n     int cur = ++sz, p = last; n     len[cur] = len[last] + 1; n     cnt[cur] = 1; n     for (; p && !ch[p][c]; p = link[p]) ch[p][c] = cur; n     if (!p) n       link[cur] = 1; n     else { n       int q = ch[p][c]; n       if (len[p] + 1 == len[q]) n         link[cur] = q; n       else { n         int nq = ++sz; n         len[nq] = len[p] + 1; n         link[nq] = link[q]; n         link[q] = link[cur] = nq; n         memcpy(ch[nq], ch[q], sizeof ch[q]); n         for (; ch[p][c] == q; p = link[p]) ch[p][c] = nq; n       } n     } n     return last = cur; n   } n   void build(int tp) { n     if (tp == 0) { n       for (int i = 1; i <= sz; i++) { n         len[i]++; n         edge[link[i]].push_back(i); n       } n     } else { n       for (int i = 1; i <= sz; i++) { n         len[i]++; n         hld::add(link[i], i); n       } n       hld::build(); n     } n   } n } ta, tb; n int n; n long long ans; n char s[maxn]; n int prepos[maxn], sufpos[maxn]; n set<int> bag[maxn << 1]; n long long val[maxn << 1]; n void add(int u, int p) { n   auto it = bag[u].lower_bound(p); n   int node = -1, d = hld::dep[ord[p]]; n   if (it != bag[u].end()) { n     node = hld::qlca(ord[p], ord[*it]); n   } n   if (it != bag[u].begin()) { n     it--; n     int y = hld::qlca(ord[p], ord[*it]); n     if (node == -1 || abs(hld::dep[node] - d) > abs(hld::dep[y] - d)) node = y; n   } n   val[u] += tb.len[ord[p]]; n   if (node != -1) val[u] -= tb.len[node]; n   bag[u].insert(p); n } n void dfs(int u) { n   for (int v : edge[u]) { n     dfs(v); n     if (bag[v].size() > bag[u].size()) { n       swap(bag[u], bag[v]); n       val[u] = val[v]; n     } n     for (auto& x : bag[v]) add(u, x); n   } n   ans += (val[u] + 1) * (ta.len[u] - ta.len[ta.link[u]]); n   ; n } n int main() { n   scanf( %s , s + 1); n   n = strlen(s + 1); n   prepos[0] = sufpos[n + 1] = 1; n   for (int i = 1; i <= n; i++) { n     prepos[i] = ta.insert(s[i] -  a ); n   } n   for (int i = n; i >= 1; i--) { n     sufpos[i] = tb.insert(s[i] -  a ); n   } n   ta.build(0); n   tb.build(1); n   for (int i = 0; i < n; i++) { n     add(prepos[i], dfn[sufpos[i + 2]]); n   } n   dfs(1); n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int sum[5]; n int n; n int main() { n   cin >> n; n   int res = 0; n   for (int i = 1; i <= n; ++i) { n     int t; n     scanf( %d , &t); n     if (t) sum[t]++; n   } n   int tmp = min(sum[1], sum[2]); n   sum[3] += tmp; n   res += tmp; n   sum[2] -= tmp; n   sum[1] -= tmp; n   if (sum[1]) { n     int t = sum[1] / 3; n     res += t * 2; n     sum[1] -= t * 3; n     sum[3] += t; n     if (sum[1]) { n       if (sum[3] >= sum[1]) n         res += sum[1]; n       else { n         if (sum[1] == 2) { n           if (sum[4]) n             res += 2; n           else { n             printf( -1 n ); n             return 0; n           } n         } else { n           if (sum[3]) n             res += 1; n           else if (sum[4] >= 2) n             res += 2; n           else { n             printf( -1 n ); n             return 0; n           } n         } n       } n     } n   } else if (sum[2]) { n     int t = sum[2] / 3; n     res += t * 2; n     sum[2] -= t * 3; n     sum[3] += t * 2; n     if (sum[2]) { n       if (sum[4] >= sum[2]) n         res += sum[2]; n       else { n         if (sum[2] == 2) n           res += 2; n         else { n           if (sum[3] >= 2) n             res += 2; n           else { n             printf( -1 n ); n             return 0; n           } n         } n       } n     } n   } n   cout << res << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long ans, heavy; n string s; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   cin >> s; n   for (int i = 0; i <= (int)s.size() - 5; i++) { n     if (s.substr(i, 5) ==  heavy ) { n       heavy++; n     } else if (s.substr(i, 5) ==  metal ) { n       ans += heavy; n     } n   } n   cout << ans <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int K, N; n long double XX, YY, P[101], PP, arr[101][101], obj[101][2], EPS = 1e-9; n double calk(int n, int k) { n   if (arr[n][k] != -1) return arr[n][k]; n   double retAns = 0; n   if (n == 1 && k == 1) n     arr[n][k] = P[1]; n   else if (n == 1 && k == 0) n     arr[n][k] = 1 - P[1]; n   else if (n == 0 || k > n || k < 0) n     arr[n][k] = 0; n   else { n     double v1 = calk(n - 1, k - 1) * P[n]; n     double v2 = calk(n - 1, k) * (1 - P[n]); n     arr[n][k] = calk(n - 1, k - 1) * P[n] + calk(n - 1, k) * (1 - P[n]); n   } n   return arr[n][k]; n } n double calcDist(double x, double y) { n   return (x - XX) * (x - XX) + (y - YY) * (y - YY); n } n double calk2(double r) { n   double retAns = 0; n   for (int i = 0; i <= N; i++) { n     for (int j = 0; j <= N; j++) arr[i][j] = -1; n     if (i) { n       double t = calcDist(obj[i][0], obj[i][1]); n       if (r * r > EPS && t > r * r) n         P[i] = exp(1.0 - t / (r * r)); n       else n         P[i] = 1.0; n     } n   } n   int g; n   for (int k = K; k <= N; k++) retAns += calk(N, k); n   return retAns; n } n double binS(double l, double r) { n   if (fabs(l - r) < EPS) return l; n   double mid = l + (r - l) / 2.0; n   if (calk2(mid) > PP) n     return binS(l, mid); n   else n     return binS(mid, r); n } n int main() { n   cin >> N >> K; n   double tp; n   cin >> tp; n   tp /= 1000; n   tp = 1.0 - tp; n   PP = tp; n   cin >> XX >> YY; n   for (int i = 1; i <= N; i++) cin >> obj[i][0] >> obj[i][1]; n   cout.setf(ios::fixed); n   cout.precision(10); n   cout << binS(0, 10000); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const double pi = acos(-1.0); n const int N = 5000000; n const double eps = 1e-6; n int n, l, m, k, a[N], rev[N], res[N]; n bool used[N]; n complex<double> b[N], tmp[N]; n pair<int, int> p[N]; n vector<int> ans; n void fft(bool invert) { n   for (int i = 0; i < l; i++) tmp[rev[i]] = b[i]; n   for (int i = 0; i < l; i++) b[i] = tmp[i]; n   for (int r = 1; r <= k; r++) { n     for (int i = 0; i < l; i += (1 << r)) { n       for (int j = i; j < i + (1 << (r - 1)); j++) { n         double ang; n         if (!invert) n           ang = 2.0 * (j - i) * pi / (1 << r); n         else n           ang = -2.0 * (j - i) * pi / (1 << r); n         complex<double> w(cos(ang), sin(ang)); n         complex<double> t1 = b[j], t2 = b[j + (1 << (r - 1))]; n         b[j] = t1 + w * t2; n         b[j + (1 << (r - 1))] = t1 - w * t2; n       } n     } n   } n   if (invert) { n     for (int i = 0; i < l; i++) b[i] /= l; n   } n } n int main() { n   scanf( %d%d , &n, &m); n   memset(used, false, sizeof(used)); n   for (int i = 0; i < n; i++) scanf( %d , &a[i]); n   for (int i = 0; i < n; i++) used[a[i]] = true; n   m++; n   l = 1; n   k = 0; n   for (int i = 1; i <= 20; i++) n     if (l < m) { n       l *= 2; n       k++; n     } else n       break; n   l *= 2; n   k++; n   for (int i = 0; i < n; i++) b[a[i]] = complex<double>(1, 0); n   for (int i = 0; i < l; i++) { n     int mask = 0; n     for (int j = 0; j < k; j++) n       if (i & (1 << j)) mask |= 1 << (k - j - 1); n     rev[i] = mask; n   } n   fft(0); n   for (int i = 0; i < l; i++) b[i] *= b[i]; n   fft(1); n   for (int i = 0; i < l; i++) res[i] = b[i].real() + 0.5; n   for (int i = 1; i < m; i++) { n     if ((!res[i]) && (used[i])) n       ans.push_back(i); n     else if ((res[i]) && (!used[i])) { n       cout <<  NO ; n       return 0; n     } n   } n   cout <<  YES  << endl; n   printf( %d n , ans.size()); n   for (int i = 0; i < ans.size(); i++) printf( %d  , ans[i]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const double pi = acos(-1.0); n const double eps = 1e-10; n const int MOD = 998244353; n const int INF = 0x3f3f3f3f; n const int maxn = 1e3 + 10; n const int maxm = 2e5 + 10; n char s[maxn]; n int len, arr[maxn]; n pair<int, long long> dq[maxn]; n int main() { n   scanf( %d%s , &len, s + 1); n   for (int i = 1; i <= len; ++i) s[len + i] = s[i]; n   int ans = 0; n   pair<int, long long> pos; n   for (int i = 1; i <= len; ++i) n     for (int j = i; j <= len; ++j) { n       int cnt = 0; n       swap(s[i], s[j]); n       swap(s[i + len], s[j + len]); n       int top = 500, bk = 499; n       for (int k = 1; k <= len * 2; ++k) { n         if (s[k] ==  ( ) n           arr[k] = ++cnt; n         else n           arr[k] = --cnt; n       } n       for (int k = 1; k <= len; ++k) { n         while (bk >= top && dq[bk].first > arr[k]) --bk; n         dq[++bk] = {arr[k], k}; n       } n       int pre = 0, res = 0; n       for (int k = 1; k <= len; ++k) { n         while (dq[top].second < k) ++top; n         while (bk >= top && dq[bk].first >= arr[k + len - 1]) --bk; n         dq[++bk] = {arr[k + len - 1], k + len - 1}; n         if (dq[top].first - pre >= 0 && arr[k + len - 1] - pre == 0) ++res; n         pre = arr[k]; n       } n       if (ans < res) { n         ans = res; n         pos = {i, j}; n       } n       swap(s[i], s[j]); n       swap(s[i + len], s[j + len]); n     } n   printf( %d n , ans); n   if (pos.first == 0) pos = {1, 1}; n   printf( %d %d n , pos.first, pos.second); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long ans = 0; n int a[200005], n, len = 1 << 18; n int mx1[1 << 18], mx2[1 << 18], fa[200005], w1[200005], w2[200005]; n int find(int x) { return fa[x] == x ? x : fa[x] = find(fa[x]); } n void add(int x, int w) { n   if (find(w) != find(mx1[x]) && a[w] > a[mx1[x]]) n     mx2[x] = mx1[x], mx1[x] = w; n   else if (find(w) != find(mx1[x]) && a[w] > a[mx2[x]]) n     mx2[x] = w; n } n int query(int x) { n   int t = (len - 1) ^ a[x]; n   if (mx1[t] && find(mx1[t]) != find(x)) return mx1[t]; n   if (mx2[t] && find(mx2[t]) != find(x)) return mx2[t]; n   return 0; n } n int merge(int x, int y) { n   if (y == 0) return 0; n   y = find(y); n   if (x == y) return 0; n   fa[x] = y; n   return 1; n } n int main() { n   scanf( %d , &n); n   for (int i = 1; i <= n; ++i) scanf( %d , &a[i]), ans -= a[i]; n   ++n; n   a[0] = -1; n   for (int i = 1; i <= n; ++i) fa[i] = i; n   while (1) { n     for (int i = 0; i < len; ++i) mx1[i] = mx2[i] = 0; n     for (int i = 1; i <= n; ++i) add(a[i], i); n     for (int i = 0; i < 18; ++i) n       for (int j = 0; j < len; ++j) n         if (j & (1 << i)) { n           add(j, mx1[j ^ (1 << i)]); n           add(j, mx2[j ^ (1 << i)]); n         } n     for (int i = 1; i <= n; ++i) w1[i] = -1; n     for (int i = 1; i <= n; ++i) { n       int t = query(i), x = find(i); n       if (t != 0 && a[i] + a[t] > w1[x]) w1[x] = a[i] + a[t], w2[x] = t; n     } n     int f = 0; n     for (int i = 1; i <= n; ++i) n       if (find(i) == i && merge(i, w2[i])) ans += w1[i], f = 1; n     if (!f) break; n   } n   printf( %lld , ans); n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   cin >> n; n   long arr[n]; n   for (int i = 0; i < n; i++) cin >> arr[i]; n   int start = 0, ends = n - 1; n   while (start != ends) { n     if (arr[ends] < arr[start]) { n       reverse(arr + start, arr + ends + 1); n       break; n     } else if (ends == n && arr[ends] > arr[ends - 1]) { n       ends--; n       start++; n     } else if (arr[ends] < arr[ends + 1] && arr[ends] < arr[ends - 1]) { n       if (arr[ends] > arr[start]) { n         start++; n         ends++; n       } n     } else if (arr[ends] > arr[ends + 1] && arr[ends] < arr[ends - 1]) { n       ends++; n       start++; n     } n     ends--; n   } n   for (int i = 1; i < n; i++) { n     if (arr[i] < arr[i - 1]) { n       cout <<  no ; n       return 0; n     } n   } n   cout <<  yes  << endl n        << min(ends + 1, start + 1) <<     << max(ends + 1, start + 1); n   return 0; n } n


#include <bits/stdc++.h> n int main() { n   int n, l; n   std::cin >> n >> l; n   int *num = new int[n]; n   int i; n   for (i = 0; i < l; i++) { n     num[i] = i + 97; n   } n   while (i < n) { n     num[i] = num[i - l]; n     i++; n   } n   for (int i = 0; i < n; i++) std::cout << (char)(num[i]); n } n


#include <bits/stdc++.h> n using namespace std; n struct comp { n   double re, im; n   comp(double x = 0, double y = 0) : re(x), im(y) {} n   comp operator+(const comp& a) { return comp(re + a.re, im + a.im); } n   comp operator-(const comp& a) { return comp(re - a.re, im - a.im); } n   comp operator*(const comp& a) { n     return comp(re * a.re - im * a.im, re * a.im + im * a.re); n   } n }; n const double pi = acos(-1); n void fft(vector<comp>& x) { n   int n = x.size(); n   for (int i = 1, k = 0; i < n; ++i) { n     for (int bit = n >> 1; (k ^= bit) < bit; bit >>= 1) { n     } n     if (i < k) { n       swap(x[i], x[k]); n     } n   } n   for (int k = 1; k < n; k <<= 1) { n     comp wlen(cos(pi / k), sin(pi / k)); n     for (int i = 0; i < n; i += (k << 1)) { n       comp w(1); n       for (int j = 0; j < k; ++j) { n         comp tmp = w * x[i + j + k]; n         x[i + j + k] = x[i + j] - tmp; n         x[i + j] = x[i + j] + tmp; n         w = w * wlen; n       } n     } n   } n } n template <class T> n vector<T> mul(vector<T>& a, vector<T>& b) { n   int n = max(a.size(), b.size()); n   int lg = 31 - __builtin_clz(n); n   n = 1 << (lg + 1 + (n != (1 << lg))); n   vector<comp> fa(n), fb(n); n   for (int i = 0; i < a.size(); ++i) { n     fa[i] = a[i]; n   } n   for (int i = 0; i < b.size(); ++i) { n     fb[i] = b[i]; n   } n   fft(fa); n   if (a == b) { n     fb = fa; n   } else { n     fft(fb); n   } n   for (int i = 0; i < n; ++i) { n     fa[i] = fa[i] * fb[i]; n   } n   fft(fa); n   reverse(fa.begin() + 1, fa.end()); n   vector<T> c(n); n   for (int i = 0; i < n; ++i) { n     c[i] = (T)(fa[i].re / n + (fa[i].re > 0 ? 0.5 : -0.5)); n   } n   return c; n } n const int N = 5e5 + 5; n bool ok[N]; n int ans[N]; n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0), cout.tie(0); n   int n, x, y; n   cin >> n >> x >> y; n   ++n; n   vector<int> a(x + 1); n   for (int i = 0; i < n; ++i) { n     int z; n     cin >> z; n     a[z] = 1; n   } n   auto b = a; n   reverse(b.begin(), b.end()); n   auto c = mul(a, b); n   for (int i = 1; i <= x; ++i) n     if (c[i + x]) ok[i + y] = true; n   for (int i = 1; i < N; ++i) { n     if (!ans[i]) ans[i] = -1; n     if (!ok[i]) continue; n     for (int j = i; j < N; j += i) ans[j] = 2 * i; n   } n   int q; n   cin >> q; n   while (q--) { n     int z; n     cin >> z; n     cout << ans[z / 2] <<    ; n   } n   return 0; n } n


#include <bits/stdc++.h> n #pragma GCC optimize( Ofast ) n using namespace std; n const long double eps = 1e-7; n const int inf = 1000000010; n const long long INF = 10000000000010LL; n const int mod = 1000000007; n const int MAXN = 100010; n int n, m, u, v, x, y, t, a, b, ans, node, cnt; n bool ok[MAXN]; n bool mark[MAXN]; n vector<int> G[MAXN]; n vector<int> out; n void dfs(int node, int par) { n   mark[node] = true; n   cnt -= ok[node]; n   ok[node] ^= 1; n   for (int v : G[node]) { n     if (mark[v]) continue; n     out.push_back(v); n     dfs(v, node); n     out.push_back(node); n     ok[node] ^= 1; n   } n   if (ok[node] && par) { n     out.push_back(par); n     out.push_back(node); n     ok[par] ^= 1; n   } n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   cin >> n >> m; n   while (m--) { n     cin >> u >> v; n     G[u].push_back(v); n     G[v].push_back(u); n   } n   for (int i = 1; i <= n; i++) { n     cin >> ok[i]; n     if (ok[i]) cnt++; n     if (!node && ok[i]) node = i; n   } n   if (!node) return cout << 0 <<   n , 0; n   ; n   dfs(node, 0); n   if (cnt) return cout << -1 <<   n , 0; n   ; n   if (!ok[node]) n     cout << out.size() + 1 << endl << node <<    ; n   else n     cout << out.size() << endl; n   for (int i : out) cout << i <<    ; n   cout << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, m; n   cin >> n >> m; n   string a[1000], b[1000]; n   for (int i = 0; i < n; i++) { n     cin >> a[i] >> b[i]; n   } n   for (int i = 0; i < m; i++) { n     string cmd, ip, tip; n     cin >> cmd >> ip; n     for (int i = 0; i < ip.size() - 1; i++) tip += ip[i]; n     for (int i = 0; i < n; i++) { n       if (tip.compare(b[i]) == 0) n         cout << cmd <<     << ip <<   #  << a[i] <<   n ; n     } n   } n } n


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


#include <bits/stdc++.h> n using namespace std; n int n, m, i, j; n const double eps = 1e-9; n struct nd { n   double x, y; n }; n nd operator-(nd x, nd y) { return (nd){x.x - y.x, x.y - y.y}; } n bool cmp(nd x, nd y) { n   if (x.x == y.x) return x.y < y.y; n   return x.x < y.x; n } n double dis(nd x, nd y) { return abs(x.x - y.x) + abs(x.y - y.y); } n double dis2(nd x, nd y) { n   return sqrt((x.x - y.x) * (x.x - y.x) + (x.y - y.y) * (x.y - y.y)); n } n double crs(nd x, nd y) { return x.x * y.y - x.y * y.x; } n vector<nd> all, cov; n nd stk[2000005]; n int tp; n void update1(nd x) { n   if (0 <= x.x && x.x <= 1e5 && 0 <= x.y && x.y <= 1e5) all.push_back(x); n } n void update2(nd p, double r, nd x) { n   if (dis(p, x) <= r) all.push_back(x); n } n int main() { n   scanf( %d , &n); n   for (i = 1; i <= n; i++) { n     double x, y, r; n     scanf( %lf%lf%lf , &x, &y, &r); n     if (x + r <= 1e5) n       update1((nd){x + r, y}); n     else { n       update1((nd){1e5, y - (r - (1e5 - x))}); n       update1((nd){1e5, y + (r - (1e5 - x))}); n     } n     if (x - r >= 0) n       update1((nd){x - r, y}); n     else { n       update1((nd){0, y - (r - x)}); n       update1((nd){0, y + (r - x)}); n     } n     if (y + r <= 1e5) n       update1((nd){x, y + r}); n     else { n       update1((nd){x - (r - (1e5 - y)), 1e5}); n       update1((nd){x + (r - (1e5 - y)), 1e5}); n     } n     if (y - r >= 0) n       update1((nd){x, y - r}); n     else { n       update1((nd){x - (r - y), 0}); n       update1((nd){x + (r - y), 0}); n     } n     update2((nd){x, y}, r, (nd){0, 0}); n     update2((nd){x, y}, r, (nd){0, 1e5}); n     update2((nd){x, y}, r, (nd){1e5, 0}); n     update2((nd){x, y}, r, (nd){1e5, 1e5}); n   } n   sort(all.begin(), all.end(), cmp); n   stk[tp = 1] = all[0]; n   for (i = 1; i < all.size(); i++) { n     if (all[i].x == all[i - 1].x && all[i].y == all[i - 1].y) continue; n     while (tp > 1 && crs(stk[tp] - stk[tp - 1], all[i] - stk[tp]) > -eps) tp--; n     stk[++tp] = all[i]; n   } n   for (i = 1; i < tp; i++) cov.push_back(stk[i]); n   reverse(all.begin(), all.end()); n   stk[tp = 1] = all[0]; n   for (i = 1; i < all.size(); i++) { n     if (all[i].x == all[i - 1].x && all[i].y == all[i - 1].y) continue; n     while (tp > 1 && crs(stk[tp] - stk[tp - 1], all[i] - stk[tp]) > -eps) tp--; n     stk[++tp] = all[i]; n   } n   for (i = 1; i < tp; i++) cov.push_back(stk[i]); n   double ans = -1e18; n   int res = 0; n   for (i = 0; i < cov.size(); i++) { n     int j = (i + 1) % cov.size(); n     int k = (i + 2) % cov.size(); n     double tmp = dis2(cov[i], cov[j]) * dis2(cov[i], cov[k]) * n                  dis2(cov[j], cov[k]) / 4 / n                  abs(crs(cov[j] - cov[i], cov[k] - cov[i]) / 2); n     if (tmp > ans) { n       ans = tmp; n       res = i; n     } n   } n   printf( %.0f %.0f n , cov[res].x, cov[res].y); n   printf( %.0f %.0f n , cov[(res + 1) % cov.size()].x, n          cov[(res + 1) % cov.size()].y); n   printf( %.0f %.0f n , cov[(res + 2) % cov.size()].x, n          cov[(res + 2) % cov.size()].y); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<pair<long long, long long> > ps, is, psum(100001), isum(100001); n long long m, n, d; n class cmp { n  public: n   bool operator()(pair<long long, long long> &a, n                   pair<long long, long long> &b) { n     return a.first > b.first || (a.first == b.first && a.second < b.second); n   } n }; n long long bsearch(long long w, long long lo, long long hi) { n   long long mid = lo + (hi - lo) / 2; n   if (lo > hi) return -1; n   if (lo == hi) { n     return (isum[lo].second <= w) ? lo : -1; n   } n   if (isum[mid].second > w) return bsearch(w, lo, mid); n   long long ret = bsearch(w, mid + 1, hi); n   return ret == -1 ? mid : ret; n } n int main() { n   long long i, j, t1, t2; n   cin >> n >> m >> d; n   for (i = 0; i < n; i++) { n     cin >> t1 >> t2; n     ps.push_back(pair<long long, long long>(t1, t2)); n   } n   for (i = 0; i < m; i++) { n     cin >> t1 >> t2; n     is.push_back(pair<long long, long long>(t1, t2)); n   } n   sort(ps.begin(), ps.end(), cmp()); n   sort(is.begin(), is.end(), cmp()); n   psum[0] = pair<long long, long long>(0, 0), n   isum[0] = pair<long long, long long>(0, 0); n   for (i = 1; i <= n; i++) { n     j = i - 1; n     psum[i] = pair<long long, long long>(ps[j].first + psum[i - 1].first, n                                          ps[j].second + psum[i - 1].second); n   } n   for (i = 1; i <= m; i++) { n     j = i - 1; n     isum[i] = pair<long long, long long>(is[j].first + isum[i - 1].first, n                                          is[j].second + isum[i - 1].second); n   } n   long long ans = 0; n   for (i = 1; i <= n; i++) { n     pair<long long, long long> tp = psum[i]; n     long long ind = bsearch(d - tp.second, 1, m); n     if (ind == -1) break; n     ans = max(ans, tp.first + isum[ind].first); n   } n   cout << ans << endl; n } n


#include <bits/stdc++.h> n using namespace std; n signed main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   long long n; n   cin >> n; n   long long arr[n], ans[n]; n   for (long long i = 0; i < n; i++) arr[i] = 1; n   ans[n - 1] = 1; n   for (long long i = 2; i <= n; i++) { n     arr[n - 1] = i; n     cout <<  ?  ; n     for (long long i = 0; i < n; i++) cout << arr[i] <<    ; n     cout <<   n ; n     cout << flush; n     long long k; n     cin >> k; n     if (!k) { n       ans[n - 1] = n - i + 2; n       break; n     } n   } n   long long i = 1; n   for (long long i = 0; i < n; i++) arr[i] = ans[n - 1]; n   while (i <= n) { n     if (i == ans[n - 1]) { n       i++; n       continue; n     } n     arr[n - 1] = i; n     cout <<  ?  ; n     for (long long i = 0; i < n; i++) cout << arr[i] <<    ; n     cout <<   n ; n     cout << flush; n     long long k; n     cin >> k; n     ans[k - 1] = i; n     i++; n   } n   cout <<  !  ; n   for (long long i = 0; i < n; i++) cout << ans[i] <<    ; n   cout <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n const int INF = 0x3f3f3f3f; n const long long LINF = 0x3f3f3f3f3f3f3f3f; n const long double pi = acos(-1.0); n const int N = 2e5 + 100; n int tr[4 * N]; n int inicial[N]; n int v[4 * N]; n int n; n void build(int node, int b, int e) { n   if (b == e) n     tr[node] = b; n   else { n     int mid = (b + e) / 2; n     build(2 * node, b, mid); n     build(2 * node + 1, mid + 1, e); n     if (v[tr[2 * node]] > v[tr[2 * node + 1]]) { n       tr[node] = tr[2 * node + 1]; n     } else { n       tr[node] = tr[2 * node]; n     } n   } n } n void update(int node, int b, int e, int i, int val) { n   if (i < b or i > e) return; n   if (b == e) { n     if (v[b] > val) v[b] = val + 1; n     return; n   } n   int mid = (b + e) / 2; n   update(2 * node, b, mid, i, val); n   update(2 * node + 1, mid + 1, e, i, val); n   if (v[tr[2 * node]] > v[tr[2 * node + 1]]) { n     tr[node] = tr[2 * node + 1]; n   } else { n     tr[node] = tr[2 * node]; n   } n } n int query(int node, int b, int e, int i, int j) { n   if (i > e or j < b) return -1; n   if (i <= b and j >= e) return tr[node]; n   int mid = (b + e) / 2; n   int p1 = query(2 * node, b, mid, i, j); n   int p2 = query(2 * node + 1, mid + 1, e, i, j); n   if (p1 == -1) return p2; n   if (p2 == -1) return p1; n   if (v[p1] < v[p2]) return p1; n   return p2; n } n void conserta(int x) { n   if (x - 1 >= 1) { n     update(1, 1, n, x - 1, v[x]); n   } n   if (x + 1 <= n) { n     update(1, 1, n, x + 1, v[x]); n   } n } n void solve(int b, int e) { n   if (b > e) return; n   int x = query(1, 1, n, b, e); n   conserta(x); n   solve(b, x - 1); n   solve(x + 1, e); n } n int main() { n   scanf( %d , &n); n   for (int i = 1; i <= n; i++) { n     int x, y; n     scanf( %d%d , &x, &y); n     inicial[i] = x; n     v[i] = x + y; n   } n   build(1, 1, n); n   solve(1, n); n   long long resp = 0; n   bool pau = false; n   for (int i = 1; i <= n; i++) { n     resp += v[i] - inicial[i]; n     if (inicial[i] > v[i]) pau = true; n   } n   if (pau) { n     printf( %d n , -1); n   } else { n     cout << resp << endl; n     for (int i = 1; i < n; i++) printf( %d  , v[i]); n     printf( %d n , v[n]); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m, q, w[15]; n char s[15]; n int cnt[5005]; n int sum[5000][105]; n int tot2[15]; n int main() { n   scanf( %d%d%d , &n, &m, &q); n   tot2[0] = 1; n   for (int i = 1; i <= 14; i++) tot2[i] = tot2[i - 1] * 2; n   for (int i = 1; i <= n; i++) scanf( %d , &w[i]); n   for (int i = 1; i <= m; i++) { n     scanf( %s , s); n     int len = strlen(s); n     int x = 0; n     for (int i = 0; i < len; i++) x = x * 2 + s[i] -  0 ; n     cnt[x]++; n   } n   for (int i = 0; i < tot2[n]; i++) { n     for (int j = 0; j < tot2[n]; j++) { n       int x = i ^ j; n       int tot = 0; n       for (int k = 0; k < n; k++) tot += (!((x >> k) & 1)) * w[n - k]; n       if (tot <= 100) sum[i][tot] += cnt[j]; n     } n   } n   for (int i = 0; i < tot2[n]; i++) { n     for (int j = 1; j <= 100; j++) { n       sum[i][j] += sum[i][j - 1]; n     } n   } n   while (q--) { n     int x = 0; n     char Q[15]; n     int val; n     scanf( %s%d , Q, &val); n     for (int i = 0; i < n; i++) x = x * 2 + Q[i] -  0 ; n     printf( %d n , sum[x][val]); n   } n } n


#include <bits/stdc++.h> n using namespace std; n inline void rd(int &x) { n   x = 0; n   char o, f = 1; n   while (o = getchar(), o < 48) n     if (o == 45) f = -f; n   do x = (x << 3) + (x << 1) + (o ^ 48); n   while (o = getchar(), o > 47); n   x *= f; n } n int head[100005], to[100005 << 1], nxt[100005 << 1], tot; n inline void add(int a, int b) { n   to[++tot] = b; n   nxt[tot] = head[a]; n   head[a] = tot; n } n int n, m, q, a, b, c, tp, ans[100005]; n int id[100005], reid[100005], dfsid, sz[100005], son[100005], fa[100005], n     top[100005], deep[100005]; n void dfs(int x, int f) { n   fa[x] = f; n   deep[x] = deep[f] + 1; n   sz[x] = 1; n   for (int i = head[x]; i; i = nxt[i]) { n     int y = to[i]; n     if (y == fa[x]) continue; n     dfs(y, x); n     sz[x] += sz[y]; n     if (sz[y] > sz[son[x]]) son[x] = y; n   } n } n void chain(int x, int f) { n   top[x] = f; n   reid[id[x] = ++dfsid] = x; n   if (son[x]) chain(son[x], f); n   for (int i = head[x]; i; i = nxt[i]) { n     int y = to[i]; n     if (y == fa[x] || y == son[x]) continue; n     chain(y, y); n   } n } n vector<int> area[100005]; n int st[100005 << 2][22]; n void build(int p = 1, int l = 1, int r = n) { n   if (l == r) { n     for (int i = 0, tp = 0; i < area[reid[l]].size() && tp < 10; i++, tp++) n       st[p][++st[p][0]] = area[reid[l]][i]; n     return; n   } n   int mid = l + r >> 1, x = 0, y = 0; n   build(p << 1, l, mid); n   build(p << 1 | 1, mid + 1, r); n   while (st[p][0] < 10 && x < st[p << 1][0] && y < st[p << 1 | 1][0]) { n     if (st[p << 1][x + 1] < st[p << 1 | 1][y + 1]) n       st[p][++st[p][0]] = st[p << 1][++x]; n     else n       st[p][++st[p][0]] = st[p << 1 | 1][++y]; n   } n   while (st[p][0] < 10 && x < st[p << 1][0]) n     st[p][++st[p][0]] = st[p << 1][++x]; n   while (st[p][0] < 10 && y < st[p << 1 | 1][0]) n     st[p][++st[p][0]] = st[p << 1 | 1][++y]; n } n void query(int a, int b, int p = 1, int l = 1, int r = n) { n   if (l > b || r < a) return; n   if (l >= a && r <= b) { n     for (int i = 1; i <= min(st[p][0], c); i++) ans[++ans[0]] = st[p][i]; n     return; n   } n   int mid = l + r >> 1; n   query(a, b, p << 1, l, mid); n   query(a, b, p << 1 | 1, mid + 1, r); n } n int main() { n   rd(n), rd(m), rd(q); n   for (int i = 1; i < n; i++) { n     rd(a), rd(b); n     add(a, b); n     add(b, a); n   } n   dfs(1, 0); n   chain(1, 1); n   for (int i = 1; i <= m; i++) { n     rd(a); n     area[a].push_back(i); n   } n   for (int i = 1; i <= n; i++) sort(area[i].begin(), area[i].end()); n   build(); n   while (q--) { n     ans[0] = 0; n     rd(a), rd(b), rd(c); n     while (top[a] != top[b]) { n       if (deep[top[a]] < deep[top[b]]) swap(a, b); n       query(id[top[a]], id[a]); n       a = fa[top[a]]; n     } n     if (deep[a] > deep[b]) swap(a, b); n     query(id[a], id[b]); n     sort(ans + 1, ans + 1 + ans[0]); n     printf( %d  , min(c, ans[0])); n     for (int i = 1; i <= min(c, ans[0]); i++) printf( %d  , ans[i]); n     puts(  ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long MOD = 1e9; n const int SIZE = 1e6 + 5; n const long long INF = 1LL << 55; n const double eps = 1e-10; n int d[1000009]; n int main() { n   int(n); n   scanf( %d , &n); n   if (n % 2) { n     int h = n / 2; n     for (int i = 0; i < (h); ++i) { n       d[i] = d[n - 2 - i] = 2 * (i + 1); n     } n     for (int i = 0; i < (h + 1); ++i) { n       d[i + n - 1] = d[2 * n - 2 - i] = 2 * i + 1; n     } n     d[2 * n - 1] = n; n   } else { n     int h = n / 2; n     for (int i = 0; i < (h); ++i) { n       d[i] = d[n - 1 - i] = 2 * i + 1; n     } n     for (int i = 0; i < (h); ++i) { n       d[i + n] = d[2 * n - 2 - i] = (2 * (i + 1)); n     } n     d[2 * n - 1] = n; n   } n   for (int i = 0; i < (2 * n); ++i) printf( %d  , d[i]); n } n


#include <bits/stdc++.h> n using namespace std; n const int NMAX = 1000000; n vector<int> graf[1 + NMAX]; n int minim[1 + NMAX]; n int minimLant = INT_MAX; n bool vizitat[1 + NMAX]; n void dfs(int z) { n   for (int i = 0; i < graf[z].size(); i++) { n     if (!vizitat[graf[z][i]]) { n       vizitat[graf[z][i]] = true; n       minim[graf[z][i]] = min(minim[z], graf[z][i]); n       dfs(graf[z][i]); n     } n   } n } n int main() { n   int n, q; n   ios_base::sync_with_stdio(false); n   cin.tie(nullptr); n   cin >> n >> q; n   for (int i = 1; i <= n - 1; i++) { n     int x, y; n     cin >> x >> y; n     graf[x].emplace_back(y); n     graf[y].emplace_back(x); n   } n   for (int i = 1; i <= n; i++) { n     minim[i] = INT_MAX; n   } n   int t, z; n   cin >> t >> z; n   z = z % n + 1; n   minim[z] = z; n   vizitat[z] = true; n   dfs(z); n   int last = 0; n   for (int i = 2; i <= q; i++) { n     cin >> t >> z; n     z = (z + last) % n + 1; n     if (t == 1) { n       minimLant = min(minimLant, minim[z]); n     } else { n       last = min(minimLant, minim[z]); n       cout << last <<   n ; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, q, A[1005], B[1005], x, y; n int main() { n   scanf( %d %d , &n, &q); n   for (int i = 1; i <= q; i++) { n     scanf( %d %d , &x, &y); n     A[x] = 1; n     B[y] = 1; n   } n   int cev = 0; n   for (int i = 2; i < n; i++) { n     cev += !A[i] + !B[i]; n   } n   if (n % 2 == 1 && A[n / 2 + 1] == 0 && B[n / 2 + 1] == 0) cev--; n   printf( %d n , cev); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long int n, m, p; n long long int a[2105][2105]; n long long int sz[2105 * 2105]; n long long int fa[2105 * 2105]; n pair<long long int, pair<long long int, long long int> > arr[2105 * 2105]; n const int mv[4][2] = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}}; n long long int find(long long int x) { n   return fa[x] == x ? x : fa[x] = find(fa[x]); n } n void merge(long long int a, long long int b) { n   a = find(a); n   b = find(b); n   if (a != b) { n     sz[a] += sz[b]; n     sz[b] = 0; n     fa[b] = a; n   } n } n long long int out[2105][2105]; n long long int height, freq; n void dfs(long long int x, long long int y) { n   if (x < 0 || x >= n || y < 0 || y >= m) return; n   if (out[x][y]) return; n   if (a[x][y] < height) return; n   if (!freq) return; n   out[x][y] = height; n   freq--; n   dfs(x + 1, y); n   dfs(x - 1, y); n   dfs(x, y + 1); n   dfs(x, y - 1); n   return; n } n int main() { n   ios_base::sync_with_stdio(0); n   long long int i, j, k; n   cin >> n >> m >> p; n   for (i = 0; i < (n); ++i) n     for (j = 0; j < (m); ++j) { n       cin >> a[i][j]; n       arr[i * m + j] = pair<long long int, pair<long long int, long long int> >( n           a[i][j], pair<long long int, long long int>(i, j)); n     } n   for (i = 0; i < (n * m); ++i) { n     fa[i] = i; n     sz[i] = 1; n   } n   sort(arr, arr + n * m); n   reverse(arr, arr + n * m); n   long long int ans = -1; n   i = 0; n   while (i < n * m) { n     j = i; n     while (j < n * m && arr[j].first == arr[i].first) { n       long long int x0, x, y0, y; n       x0 = arr[j].second.first; n       y0 = arr[j].second.second; n       for (k = 0; k < (4); ++k) { n         x = x0 + mv[k][0]; n         y = y0 + mv[k][1]; n         if (x >= 0 && x < n && y >= 0 && y < m) n           if (a[x][y] >= arr[j].first) merge(x * m + y, x0 * m + y0); n       } n       j++; n     } n     if (arr[i].first && (p % arr[i].first) == 0) n       for (k = i; k < j; ++k) { n         long long int x, y; n         x = arr[k].second.first; n         y = arr[k].second.second; n         if (sz[find(x * m + y)] >= p / arr[i].first) { n           ans = x * m + y; n           break; n         } n       } n     if (ans != -1) break; n     i = j; n   } n   if (ans == -1) { n     cout <<  NO n ; n     return 0; n   } n   cout <<  YES n ; n   height = a[ans / m][ans % m]; n   freq = p / height; n   dfs(ans / m, ans % m); n   for (i = 0; i < (n); ++i) { n     for (j = 0; j < (m); ++j) cout << out[i][j] <<    ; n     cout << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<long long int> ans; n vector<long long int> allD; n unordered_map<long long int, int> comp; n const int MM = 1e4; n const int LIM = 1e5; n vector<long long int> gr[MM]; n void solve(long long int N, long long int K) { n   if (K == 0 || N == 0) { n     ans.push_back(N); n     return; n   } n   for (long long int u : gr[N]) { n     solve(u, K - 1); n     if (ans.size() == LIM) { n       return; n     } n   } n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   long long int N, K; n   cin >> N >> K; n   for (long long int i = 1; i * i <= N; i++) { n     if (N % i == 0) { n       allD.push_back(i); n       if (i * i != N) { n         allD.push_back(N / i); n       } n     } n   } n   sort(allD.begin(), allD.end()); n   for (int i = 0; i < allD.size(); i++) { n     for (int j = 0; j <= i; j++) { n       if (allD[i] % allD[j] == 0) { n         gr[i].push_back(j); n       } n     } n   } n   K = min(K, (long long int)LIM); n   solve(allD.size() - 1, K); n   for (auto u : ans) { n     cout << allD[u] <<    ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n  n typedef long long ll; n typedef pair<int, int> pii; n typedef pair<ll, ll> pll; n  n const int MAXN = 2e5; n const int MAXM = 5e6; n  n int N; n pii A[MAXN+10]; n  n pii B[MAXM+10], C[MAXM+10]; n  n int main() n { n  scanf( %d , &N); n  for(int i=1; i<=N; i++) scanf( %d , &A[i].first), A[i].second=i; n  sort(A+1, A+N+1); n  n  N=min(N, 10000); n  for(int i=1; i<=N; i++) n  { n   for(int j=i-1; j>=1; j--) n   { n    int t=A[i].first-A[j].first; n    if(B[t]==pii(0, 0)) n    { n     B[t]={j, i}; n    } n   } n  } n  for(int i=N; i>=1; i--) n  { n   for(int j=i+1; j<=N; j++) n   { n    int t=A[j].first-A[i].first; n    if(C[t]==pii(0, 0)) n    { n     C[t]={i, j}; n    } n   } n  } n  n  for(int i=0; i<=MAXM; i++) n  { n   if(B[i]==pii(0, 0)) continue; n   if(C[i]==pii(0, 0)) continue; n   if(B[i].second<C[i].first) n   { n    printf( YES n ); n    printf( %d %d %d %d n , A[B[i].first].second, A[C[i].second].second, A[B[i].second].second, A[C[i].first].second); n    return 0; n   } n  } n  printf( NO n ); n }


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, s; n   cin >> n; n   n += 1; n   int h[n]; n   for (int i = 1; i < n; i++) cin >> h[i]; n   h[0] = 0; n   int energy = 0; n   int max_energy = 0; n   for (int i = 0; i < (n); ++i) { n     if (i == 0) continue; n     energy += h[i - 1] - h[i]; n     max_energy = min(max_energy, energy); n   } n   cout << -max_energy << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <class T> n T min(T a, T b, T c) { n   return min(a, min(b, c)); n } n template <class T> n T min(T a, T b, T c, T d) { n   return min(a, min(b, min(c, d))); n } n template <class T> n T max(T a, T b, T c) { n   return max(a, max(b, c)); n } n template <class T> n T max(T a, T b, T c, T d) { n   return max(a, max(b, max(c, d))); n } n bool cmp(int a, int b) { return a > b; } n long long GCD(long long a, long long b) { return (a % b) ? GCD(b, a % b) : b; } n const string namePro =  tmp ; n int main() { n   int n; n   scanf( %d , &n); n   int cnt = 0; n   int ans = 0; n   stack<int> stk; n   for (int i = (1); i <= (2 * n); ++i) { n     string s; n     cin >> s; n     if (s ==  add ) { n       int x; n       scanf( %d , &x); n       stk.push(x); n     } else { n       ++cnt; n       if (stk.size() && stk.top() == cnt) n         stk.pop(); n       else if (stk.empty()) n         continue; n       else { n         while (stk.size()) stk.pop(); n         ++ans; n       } n     } n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAX_N = 41; n const int MASK = 1 << 21; n long long sz[MAX_N], par[MAX_N], po2[MAX_N], vis[MAX_N]; n long long dp[MASK]; n vector<int> adj[MAX_N]; n int rt(int v) { return v ^ par[v] ? par[v] = rt(par[v]) : v; } n void join(int u, int v) { n   if ((u = rt(u)) != (v = rt(v))) { n     par[v] = u, sz[u] += sz[v]; n   } n } n int main() { n   int n, m; n   cin >> n >> m; n   if (m < 3) { n     cout << 0 << endl; n     return 0; n   } n   po2[0] = 1; n   for (int i = 0; i < n; i++) par[i] = i, sz[i] = 1; n   for (int i = 1; i < n + 1; i++) po2[i] = po2[i - 1] * 2ll; n   long long ans = po2[n]; n   for (int i = 0; i < m; i++) { n     int a, b; n     cin >> a >> b; n     a--, b--; n     join(a, b); n     adj[a].push_back(b); n     adj[b].push_back(a); n   } n   set<long long> s; n   long long num = 0; n   for (int i = 0; i < n; i++) { n     s.insert(rt(i)); n     if (sz[rt(i)] == 1) num++; n   } n   ans += po2[num + 1] - po2[s.size()]; n   bool imposs = false; n   for (int i = 0; i < n; i++) { n     if (vis[i]) continue; n     queue<pair<int, int> > q; n     q.push({i, 1}); n     while (q.size()) { n       pair<int, int> curr = q.front(); n       q.pop(); n       if (vis[curr.first]) { n         if (vis[curr.first] != curr.second) { n           imposs = true; n           break; n         } else n           continue; n       } n       vis[curr.first] = curr.second; n       for (int next : adj[curr.first]) q.push({next, curr.second ^ 3}); n     } n     if (imposs) break; n   } n   if (!imposs) ans += po2[s.size()]; n   num = 0; n   for (int mask = 0; mask < 1 << ((n + 1) / 2); mask++) { n     bool nowork = false; n     long long new_mask = (1 << (n / 2)) - 1; n     for (int k = 0; k < (n + 1) / 2; k++) { n       if (!(mask & (1 << k))) continue; n       for (int nxt : adj[2 * k]) { n         if (nxt & 1) n           new_mask &= (1 << (n / 2)) - (1 << (nxt / 2)) - 1; n         else n           nowork = nowork || (mask & (1 << (nxt / 2))); n       } n     } n     if (!nowork) dp[new_mask]++; n   } n   for (int i = 0; i < n / 2; i++) n     for (int mask = 0; mask < 1 << (n / 2); mask++) n       if (mask != (mask | (1 << i))) dp[mask] += dp[mask | (1 << i)]; n   for (int mask = 0; mask < 1 << (n / 2); mask++) { n     bool nowork = false; n     for (int k = 0; k < n / 2; k++) { n       if (!(mask & (1 << k))) continue; n       for (long long nxt : adj[2 * k + 1]) { n         if (nxt & 1 && mask & (1 << (nxt / 2))) nowork = true; n       } n     } n     if (!nowork) num += dp[mask]; n   } n   ans -= 2ll * num; n   cout << ans << endl; n } n


#include <bits/stdc++.h> n using namespace std; n set<pair<long long int, long long int> > s; n pair<long long int, long long int> p; n long long int d[300000], x, n, k, l = 0, di = 0; n vector<long long int> v[300000]; n int main() { n   ios::sync_with_stdio(false), cin.tie(0), cout.tie(0); n   cin >> n >> k; n   for (int i = 1; i <= n; i++) { n     cin >> x; n     d[i] = x; n     v[x].push_back(i); n     di = max(x, di); n   } n   if (v[0].size() > 1 || v[1].size() > k) { n     cout << -1; n     return 0; n   } n   for (int i = 0; i <= di; i++) n     if (v[i].size() == 0) { n       cout << -1; n       return 0; n     } n   for (int i = 0; i < v[1].size(); i++) s.insert({v[0][0], v[1][i]}); n   for (int i = 2; i <= di; i++) { n     if (v[i - 1].size() * (k - 1) < v[i].size()) { n       cout << -1; n       return 0; n     } n     x = 1; n     l = 0; n     for (int j = 0; j < v[i].size(); j++) { n       if (x > k - 1) { n         l++; n         x = 1; n       } n       s.insert({v[i - 1][l], v[i][j]}); n       x++; n     } n   } n   cout << s.size() << endl; n   while (!s.empty()) { n     p = *s.begin(); n     cout << p.first <<     << p.second <<   n ; n     s.erase(s.begin()); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m, s, tot; n int a[5005], b[5005], c[5005], last[5005]; n vector<int> edge[5005]; n int cmp(int i, int j) { return c[i] > c[j]; } n long long dp[5005][5005]; n int main() { n   scanf( %d%d%d , &n, &m, &s); n   int temp = s; n   for (int i = 1; i <= n; i++) { n     scanf( %d%d%d , &a[i], &b[i], &c[i]); n     if (temp < a[i]) { n       return printf( -1 n ), 0; n     } n     temp += b[i]; n     last[i] = i; n   } n   for (int i = 1; i <= m; i++) { n     int x, y; n     scanf( %d%d , &x, &y); n     last[y] = max(last[y], x); n   } n   for (int i = 1; i <= n; i++) edge[last[i]].push_back(i); n   for (int i = 1; i <= n; i++) { n     sort(edge[i].begin(), edge[i].end(), cmp); n   } n   memset(dp, -1, sizeof(dp)); n   dp[0][s] = 0; n   for (int i = 1; i <= n; i++) { n     for (int j = 0; j <= 5000; j++) n       if (j - b[i] >= a[i]) { n         dp[i][j] = dp[i - 1][j - b[i]]; n       } n     int len = edge[i].size(); n     for (int j = 1; j <= 5000; j++) n       if (dp[i][j] != -1) { n         long long res = 0; n         for (int k = 0; k < len; k++) n           if (j - k - 1 >= 0) { n             res += c[edge[i][k]]; n             dp[i][j - k - 1] = max(dp[i][j] + res, dp[i][j - k - 1]); n           } n       } n   } n   long long ans = 0; n   for (int i = 0; i <= 5000; i++) { n     ans = max(ans, dp[n][i]); n   } n   printf( %lld n , ans); n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e5 + 5; n const double eps = 1e-9; n double mi[N], ma[N]; n double pre1[N], pre2[N]; n double ans1[N], ans2[N]; n int main() { n   int n; n   scanf( %d , &n); n   for (int i = (1); i <= (n); i++) cin >> ma[i]; n   for (int i = (1); i <= (n); i++) cin >> mi[i]; n   for (int i = (1); i <= (n); i++) pre1[i] = pre1[i - 1] + ma[i]; n   for (int i = n; i >= 1; i--) pre2[i] = pre2[i + 1] + mi[i]; n   for (int i = (1); i <= (n); i++) { n     double t1 = pre2[i + 1] - pre1[i] - 1.0; n     double t3 = t1 * t1 - 4.0 * pre1[i]; n     if (t3 < eps) t3 = 0.0; n     t3 = sqrt(t3); n     ans1[i] = 0.5 * (-t1 + t3); n     ans2[i] = 0.5 * (-t1 - t3); n   } n   for (int i = (1); i <= (n); i++) printf( %.9lf  , ans1[i] - ans1[i - 1]); n   puts(  ); n   for (int i = (1); i <= (n); i++) printf( %.9lf  , ans2[i] - ans2[i - 1]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, x, y; n   cin >> n >> x >> y; n   n = n / 2; n   if ((n == x || n == x - 1) && (n == y || n == y - 1)) n     cout <<  NO  << endl; n   else n     cout <<  YES  << endl; n } n


#include <bits/stdc++.h> n int n; n int main() { n   scanf( %d , &n); n   printf( %d n , n % 5 % 3 + 1); n } n


#include <bits/stdc++.h> n using namespace std; n const long long mod = 1e9 + 7; n void solve() { n   long long n; n   cin >> n; n   if (n < 6) n     cout <<  -1  << endl; n   else { n     cout <<  1 2 n2 3 n2 4 n1 5 n1 6 n ; n     for (long long i = 7; i <= n; i++) { n       cout << 2 <<     << i << endl; n     } n   } n   cout <<  1 2 n ; n   for (long long i = 3; i <= n; i++) { n     cout << 2 <<     << i << endl; n   } n } n signed main() { n   long long t = 1; n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   while (t--) { n     solve(); n   } n } n


#include <bits/stdc++.h> n using namespace std; n long long pot[35]; n int numbits(long long n) { n   int conta = 0; n   for (int i = 0; i <= 33; i++) { n     if (n & pot[i]) conta++; n   } n   return conta; n } n int main() { n   pot[0] = 1; n   for (int i = 1; i <= 33; i++) pot[i] = 2 * pot[i - 1]; n   long long n, p; n   scanf( %lld %lld , &n, &p); n   if (p == 0) n     printf( %d n , numbits(n)); n   else if (p < 0) { n     int k = 0; n     bool teste = true; n     while (teste) { n       if (numbits(n - k * p) <= k) { n         printf( %d n , k); n         teste = false; n       } else n         k++; n     } n   } else { n     int k = 0; n     bool funfa = false; n     while (k <= (n / (p + 1)) && funfa == false) { n       if (numbits(n - k * p) <= k) { n         printf( %d n , k); n         funfa = true; n       } n       k++; n     } n     if (!funfa) printf( -1 n ); n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   string s; n   cin >> s; n   int a[6]; n   for (int i = 0; i < 6; i++) { n     a[i] = s[i] - 48; n   } n   int sum1 = a[0] + a[1] + a[2]; n   int sum2 = a[3] + a[4] + a[5]; n   int difference = abs(sum1 - sum2); n   int flag = 0; n   if (sum1 > sum2) flag = 3; n   for (int i = flag; i <= flag + 2; i++) { n     a[i] = 9 - a[i]; n   } n   int count = 0; n   int sum = 0; n   if (difference == 0) { n     cout << 0 << endl; n     return 0; n   } n   while (1) { n     int maxi = 0; n     for (int i = 0; i < 6; i++) { n       if (a[maxi] < a[i]) maxi = i; n     } n     if (a[maxi] == -1) return 0; n     count += 1; n     sum += a[maxi]; n     if (sum >= difference) { n       cout << count << endl; n       return 0; n     } n     a[maxi] = -1; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   string s; n   cin >> s; n   int low = 0, up = 0; n   for (int i = 0; i < s.size(); i++) { n     if (s[i] < 93) { n       up++; n     } else { n       low++; n     } n   } n   if (low >= up) { n     for (int i = 0; i < s.size(); i++) { n       s[i] = tolower(s[i]); n     } n   } else { n     for (int i = 0; i < s.size(); i++) { n       s[i] = toupper(s[i]); n     } n   } n   cout << s; n } n


#include <bits/stdc++.h> n using namespace std; n int p[500005], q[500005], c[500005], f[500005], siz[500005], fx[500005], n     sizx[500005], cln[500005 * 2], ans[500005], qu[500005], id[500005]; n struct edge { n   int u, v, w, nxt, id; n } e[500005]; n vector<int> a[500005]; n inline int find(int); n void swap(int& a, int& b) { n   int t = a; n   a = b; n   b = t; n } n bool cmpp(int x, int y) { return e[x].w < e[y].w; } n bool cmpq(int x, int y) { return c[x] < c[y]; } n inline int read() { n   int x = 0; n   char c = getchar(); n   while (c <  0  || c >  9 ) c = getchar(); n   while (c >=  0  && c <=  9 ) { n     x = (x << 3) + (x << 1) + (c ^ 48); n     c = getchar(); n   } n   return x; n } n int main() { n   int i, j, n, m, u, v, nm, t, Q, tmp, top = 0, pos = 1, z; n   n = read(); n   m = read(); n   for (i = 1; i <= m; ++i) { n     e[i].u = read(); n     e[i].v = read(); n     e[i].w = read(); n   } n   t = read(); n   for (i = 1; i <= t; ++i) { n     nm = read(); n     for (j = 1; j <= nm; ++j) qu[j] = read(); n     sort(qu + 1, qu + nm + 1, cmpp); n     for (j = 1; j <= nm; ++j) { n       if (e[qu[j - 1]].w ^ e[qu[j]].w) { n         if (top) a[top][0] = Q; n         Q = 0; n         id[++top] = i; n         c[top] = e[qu[j]].w; n         a[top].push_back(0); n       } n       ++Q; n       a[top].push_back(qu[j]); n     } n   } n   a[top][0] = Q; n   Q = 0; n   for (i = 1; i <= max(top, max(n, m)); ++i) p[i] = q[i] = f[i] = fx[i] = i; n   for (i = 1; i <= n; ++i) siz[i] = sizx[i] = 1; n   sort(p + 1, p + m + 1, cmpp); n   sort(q + 1, q + top + 1, cmpq); n   for (i = 1; i <= top; ++i) { n     Q = q[i]; n     tmp = 0; n     while (pos <= m && e[p[pos]].w < c[Q]) { n       u = find(e[p[pos]].u); n       v = find(e[p[pos]].v); n       ++pos; n       if (u == v) continue; n       if (siz[u] < siz[v]) swap(u, v); n       cln[++tmp] = v; n       f[v] = u; n       siz[u] += siz[v]; n     } n     for (j = 1; j <= tmp; ++j) { n       fx[cln[j]] = f[cln[j]]; n       sizx[cln[j]] = siz[cln[j]]; n     } n     nm = a[Q][0]; n     tmp = 0; n     for (j = 1; j <= nm; ++j) { n       z = a[Q][j]; n       u = find(e[z].u); n       v = find(e[z].v); n       if (u == v) { n         ans[id[Q]] = 1; n         break; n       } n       if (siz[u] < siz[v]) swap(u, v); n       cln[++tmp] = v; n       f[v] = u; n       siz[u] += siz[v]; n     } n     for (j = 1; j <= tmp; ++j) { n       f[cln[j]] = fx[cln[j]]; n       siz[cln[j]] = sizx[cln[j]]; n     } n   } n   for (i = 1; i <= t; ++i) puts(ans[i] ?  NO  :  YES ); n   return 0; n } n inline int find(int u) { n   while (u != f[u]) u = f[u]; n   return u; n } n


#include <bits/stdc++.h> n using namespace std; n const int desll[4][2] = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}}; n const long long mod = 1e9 + 7; n const int maxn = 1e6 + 7; n const int maxm = 1e8 + 7; n const double eps = 1e-4; n int m, n, ar[maxn]; n char ch[maxn]; n int armid[maxn]; n struct node { n   int val, num; n   char c; n   node *pre, *l, *r; n   void clear() { l = r = NULL; } n } no[maxn]; n int ma, ans = 0; n int newCal(node* u) { n   if (u->c ==  N ) n     return 1 - (u->l->val); n   else if (u->c ==  A ) n     return u->l->val & u->r->val; n   else if (u->c ==  X ) n     return u->l->val ^ u->r->val; n   else if (u->c ==  O ) n     return u->l->val | u->r->val; n } n void dfs(node* u, int x) { n   int now = u->val; n   u->val = x; n   if (u->pre == NULL || now == x) { n     ans = no[1].val; n   } else { n     if (armid[u->num] != -1) { n       ans = armid[u->num]; n     } else { n       dfs(u->pre, newCal(u->pre)); n       armid[u->num] = ans; n     } n   } n   u->val = now; n } n void init(node* u) { n   if (u->c ==  I ) return; n   init(u->l); n   if (u->r != NULL) init(u->r); n   if (u->c ==  N ) n     u->val = 1 - (u->l->val); n   else if (u->c ==  A ) n     u->val = u->l->val & u->r->val; n   else if (u->c ==  X ) n     u->val = u->l->val ^ u->r->val; n   else if (u->c ==  O ) n     u->val = u->l->val | u->r->val; n } n int main() { n   scanf( %d , &n); n   int x, y; n   no[1].pre = NULL; n   for (int i = 0; i <= n; i++) no[i].clear(); n   for (int i = 1; i <= n; i++) { n     scanf( %s , ch); n     scanf( %d , &x); n     if (ch[0] ==  A  || ch[0] ==  O  || ch[0] ==  X ) { n       scanf( %d , &y); n       no[i].l = &no[x]; n       no[i].r = &no[y]; n       no[x].pre = &no[i]; n       no[y].pre = &no[i]; n       no[i].c = ch[0]; n     } else if (ch[0] ==  N ) { n       no[i].l = &no[x]; n       no[x].pre = &no[i]; n       no[i].c = ch[0]; n     } else { n       no[i].val = x; n       no[i].c = ch[0]; n     } n     no[i].num = i; n   } n   memset(armid, -1, sizeof(armid)); n   init(&no[1]); n   int len = 0; n   for (int i = 1; i <= n; i++) { n     ma = 0; n     if (no[i].c ==  I ) { n       dfs(&no[i], 1 - no[i].val); n       ar[len++] = ans; n     } n   } n   for (int i = 0; i < len; i++) { n     printf( %d , ar[i]); n   } n   printf(  n ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main(int argc, const char* argv[]) { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   int c, m, t, r, i, W[301], tot, j; n   map<int, bool> used; n   map<int, bool> ends; n   map<int, bool> G; n   cin >> m >> t >> r; n   for (i = 0; i < m; i++) { n     cin >> W[i]; n     G[W[i]] = true; n   } n   if (r > t) { n     cout <<  -1 n ; n     return 0; n   } n   c = 0; n   tot = 0; n   for (i = 0; i <= W[m - 1]; i++) { n     if (G[i] == true) { n       if (c < r) { n         for (j = i - 1; j >= i - t && c < r; j--) { n           if (used[j] == false) { n             used[j] = true; n             ends[j + t] = true; n             c++; n             tot++; n           } n         } n       } n       if (c < r) { n         cout <<  -1 n ; n         return 0; n       } n     } n     if (ends[i] == true) c--; n   } n   cout << tot <<   n ; n   return 0; n } n


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


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   int n; n   cin >> n; n   cout <<  0 0  << endl; n   int l = 1, r = (int)1e9, mid = l + (r - l) / 2; n   string s; n   cin >> s; n   int first; n   if (s[0] ==  b ) { n     first = 0; n   } else { n     first = 1; n   } n   for (int i = 1; i < n; i++) { n     cout << mid <<     << mid << endl; n     cin >> s; n     if ((s[0] ==  w ) == first) { n       l = mid + 1; n     } else { n       r = mid; n     } n     mid = l + (r - l) / 2; n   } n   if (l >= r && mid != (int)1e9) { n     cout << mid - 1 <<     << mid <<     << mid <<     << mid - 1 << endl; n   } else { n     cout << mid <<     << 0 <<     << mid <<     << 1 << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAX = 3e5 + 5; n pair<long double, int> x[MAX]; n int main() { n   ios::sync_with_stdio(false); n   int n, id, k; n   cin >> n; n   for (int i = 0; i < (int)(n); i++) { n     cin >> x[i].first; n     x[i].second = i + 1; n   } n   cin >> k; n   sort(x, x + n); n   long double acum1 = 0, acum2 = 0, ans = 0; n   for (long long i = 0; i < k; i++) { n     acum1 += x[i].first; n     acum2 += (x[i].first * ((long double)(i + 1))); n     ans = 2 * acum2 - ((long double)(k + 1)) * acum1; n     id = 0; n   } n   for (long long i = 0; i + k < n; i++) { n     acum2 = (acum2 - acum1 + ((long double)k) * x[i + k].first); n     acum1 = (acum1 - x[i].first + x[i + k].first); n     if (ans > 2 * acum2 - ((long double)(k + 1)) * acum1) { n       id = i + 1, ans = 2 * acum2 - ((long double)(k + 1)) * acum1; n     } n   } n   for (int i = 0; i < (int)(k); i++) { n     if (i) cout <<    ; n     cout << x[i + id].second; n   } n   cout << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n inline long long getnum() { n   char c = getchar(); n   long long num, sign = 1; n   for (; c <  0  || c >  9 ; c = getchar()) n     if (c ==  - ) sign = -1; n   for (num = 0; c >=  0  && c <=  9 ;) { n     c -=  0 ; n     num = num * 10 + c; n     c = getchar(); n   } n   return num * sign; n } n long long A[3004], C[3004], B[1004]; n vector<long long> V[3004], W[3004]; n priority_queue<int, vector<int>, greater<int>> PQ; n int main() { n   int n = getnum(), m = getnum(); n   for (int i = 1; i <= n; i++) { n     A[i] = getnum(); n     B[A[i]]++; n     C[i] = getnum(); n     V[A[i]].push_back(C[i]); n     W[A[i]].push_back(C[i]); n   } n   for (int i = 1; i <= m; i++) { n     sort(V[i].begin(), V[i].end()); n     sort(W[i].begin(), W[i].end()); n     for (int j = 1; j < V[i].size(); j++) V[i][j] += V[i][j - 1]; n   } n   long long ans = LLONG_MAX; n   int l = 1, r = n; n   for (int mid = 1; mid <= n; mid++) { n     int can = B[1]; n     long long sum = 0; n     for (int i = 2; i <= m; i++) { n       for (int j = 0, rem = W[i].size(); j < W[i].size(); j++) { n         if (rem >= mid) n           sum += W[i][j], can++, rem--; n         else n           PQ.push(W[i][j]); n       } n     } n     while (can < mid) { n       if (PQ.size() == 0) break; n       int x = PQ.top(); n       PQ.pop(); n       can++; n       sum += x; n     } n     while (PQ.size()) PQ.pop(); n     if (can >= mid) { n       ans = min(ans, sum); n     } n   } n   cout << ans << endl; n } n


#include <bits/stdc++.h> n using namespace std; n void FREOPEN() {} n inline double Time() { return (clock() * 1.0) / CLOCKS_PER_SEC; } n const int N = (int)200500, inf = (int)2e9, MOD = (int)1e9 + 7, alph = 26, C = 9; n char CH[N]; n const long long INF = (long long)1e18; n const int dx[] = {1, -1, 0, 0, -1, 1, -1, 1}; n const int dy[] = {0, 0, 1, -1, -1, 1, 1, -1}; n pair<int, int> t[C][N * 4]; n int non[N], a[N]; n void build(int w, int v, int l, int r) { n   t[w][v] = {inf, inf}; n   if (l == r) return; n   int mid = (l + r) / 2; n   build(w, v + v, l, mid); n   build(w, v + v + 1, mid + 1, r); n } n pair<int, int> comb(pair<int, int> f, pair<int, int> s) { n   pair<int, int> ret = f; n   if (ret.first > s.first) { n     ret.second = ret.first; n     ret.first = s.first; n     if (ret.second > s.second) ret.second = s.second; n   } else if (ret.second > s.first) n     ret.second = s.first; n   return ret; n } n void upd(int w, int v, int l, int r, int p, int x) { n   if (l == r) { n     if (x == 1) n       t[w][v] = {a[l], inf}; n     else n       t[w][v] = {inf, inf}; n     return; n   } n   int mid = (l + r) / 2; n   if (p <= mid) n     upd(w, v + v, l, mid, p, x); n   else n     upd(w, v + v + 1, mid + 1, r, p, x); n   t[w][v] = comb(t[w][v + v], t[w][v + v + 1]); n } n pair<int, int> get(int w, int v, int l, int r, int L, int R) { n   if (L > R || l > R || r < L) return {inf, inf}; n   if (L <= l && R >= r) return t[w][v]; n   int mid = (l + r) / 2; n   return comb(get(w, v + v, l, mid, L, R), get(w, v + v + 1, mid + 1, r, L, R)); n } n int calc(int l, int r, int n) { n   int ret = inf; n   for (int k = 0; k < C; k++) { n     pair<int, int> now = get(k, 1, 1, n, l, r); n     if (now.first != inf && now.second != inf) n       ret = min(ret, now.first + now.second); n   } n   if (ret == inf) ret = -1; n   return ret; n } n int main() { n   FREOPEN(); n   int n, m; n   scanf( %d %d , &n, &m); n   for (int k = 0; k < C; k++) build(k, 1, 1, n); n   for (int i = 1; i <= n; i++) { n     scanf( %d  , &a[i]); n     int x = a[i]; n     for (int k = 0; k < C && x; k++) { n       if (x % 10 > 0) { n         non[i] |= (1 << k); n       } n       x /= 10; n     } n     for (int k = 0; k < C; k++) { n       if (non[i] & (1 << k)) upd(k, 1, 1, n, i, 1); n     } n   } n   for (int i = 1; i <= m; i++) { n     int ty; n     scanf( %d  , &ty); n     if (ty == 1) { n       int p, x; n       scanf( %d %d , &p, &x); n       for (int k = 0; k < C; k++) { n         if (non[p] & (1 << k)) upd(k, 1, 1, n, p, 0); n       } n       a[p] = x; n       non[p] = 0; n       for (int k = 0; k < C && x; k++) { n         if (x % 10 > 0) { n           non[p] |= (1 << k); n         } n         x /= 10; n       } n       for (int k = 0; k < C; k++) { n         if (non[p] & (1 << k)) upd(k, 1, 1, n, p, 1); n       } n     } else { n       int lef, rig; n       scanf( %d %d , &lef, &rig); n       printf( %d n , calc(lef, rig, n)); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m, k; n vector<vector<int> > Dist, Alist; n vector<int> Distance, GivenPath; n int mi, ma; n bool isThere(int u, vector<int>& alist) { n   for (auto e : alist) { n     if (e == u) return true; n   } n   return false; n } n int main() { n   cin >> n >> m; n   vector<int> Alist[n]; n   vector<int> Alist2[n]; n   vector<int> Dist[n + 5]; n   int Distance[n]; n   memset(Distance, -1, sizeof(Distance)); n   for (int i = 0 - (0 > m); i != m - (0 > m); i += 1 - 2 * (0 > m)) { n     int u, v; n     cin >> u >> v, u--, v--; n     Alist[v].push_back(u); n     Alist2[u].push_back(v); n   } n   cin >> k; n   GivenPath.assign(k, 0); n   for (int i = 0 - (0 > k); i != k - (0 > k); i += 1 - 2 * (0 > k)) n     cin >> GivenPath[i], GivenPath[i]--; n   mi = ma = 0; n   Distance[GivenPath.back()] = 0; n   Dist[0].push_back(GivenPath.back()); n   queue<int> q; n   q.push(GivenPath.back()); n   while (!q.empty()) { n     int u = q.front(); n     q.pop(); n     for (auto& e : Alist[u]) { n       if (Distance[e] == -1) { n         Distance[e] = Distance[u] + 1; n         Dist[Distance[e]].push_back(e); n         q.push(e); n       } n     } n   } n   for (int i = 0 - (0 > k - 1); i != k - 1 - (0 > k - 1); n        i += 1 - 2 * (0 > k - 1)) { n     int dist = Distance[GivenPath[i]] - 1; n     if (dist != Distance[GivenPath[i + 1]]) { n       mi++, ma++; n       continue; n     } n     for (auto e : Alist2[GivenPath[i]]) { n       if (e != GivenPath[i + 1] && Distance[e] == dist) { n         ma++; n         break; n       } n     } n   } n   cout << mi <<     << ma <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n inline long long bip(long long x, long long n, long long mod) { n   long long res = 1; n   while (n) { n     if (n & 1) { n       res = (res * x) % mod; n     } n     x = (x * x) % mod; n     n >>= 1; n   } n   return res; n } n const int maxn = 1e9; n const long long inf = 1e18; n const int mod = 1e9 + 7; n const int N = 1e3 + 10; n int n, m, a[1010], ind; n long long C[10][10]; n void Cnk() { n   for (int i = 1; i < 10; i++) { n     C[i][0] = C[i][i] = 1; n     for (int j = 1; j < i; j++) { n       C[i][j] = C[i - 1][j - 1] + C[i - 1][j]; n     } n   } n } n void solve() { n   cin >> n >> m >> ind; n   for (int i = 1; i <= m; i++) cin >> a[i]; n   n--, a[ind]--; n   int sum = 0; n   for (int i = 1; i <= m; i++) sum += a[i]; n   if (sum < n) { n     cout << -1; n     return; n   } n   if (a[ind] == 0) { n     cout << 0.00000; n     return; n   } n   int k = sum - a[ind], l = sum, x = l - n, y = k - n; n   if (y < 0) { n     cout << 1.00000000; n     return; n   } n   long double up = 1, down = 1; n   for (int i = k - n + 1; i <= l - n; i++) up *= (i + 0.0); n   for (int i = k + 1; i <= l; i++) down *= (i + 0.0); n   cout << 1.0000000000 - up / down; n } n int main() { n   ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); n   int tt = 1; n   cout << fixed << setprecision(9); n   while (tt--) { n     solve(); n     cout <<   n ; n   } n } n


#include <bits/stdc++.h> n const int Inf = (int)(1e9); n const int K = 1000 + 1; n const int N = 60 + 1; n int w[N][N][N]; n int n, m, r; n int ans[K][N][N]; n void prepare() { n   for (int i = 0; i < K; ++i) n     for (int j = 0; j < N; ++j) n       for (int k = 0; k < N; ++k) ans[i][j][k] = Inf; n   for (int i = 0; i < m; ++i) { n     for (int k = 0; k < n; ++k) n       for (int ii = 0; ii < n; ++ii) n         for (int jj = 0; jj < n; ++jj) n           w[i][ii][jj] = std::min(w[i][ii][jj], w[i][ii][k] + w[i][k][jj]); n     for (int ii = 0; ii < n; ++ii) n       for (int jj = 0; jj < n; ++jj) n         ans[0][ii][jj] = std::min(ans[0][ii][jj], w[i][ii][jj]); n   } n   for (int i = 1; i < K; ++i) { n     for (int ii = 0; ii < n; ++ii) n       for (int jj = 0; jj < n; ++jj) ans[i][ii][jj] = ans[i - 1][ii][jj]; n     for (int k = 0; k < n; ++k) n       for (int ii = 0; ii < n; ++ii) n         for (int jj = 0; jj < n; ++jj) n           ans[i][ii][jj] = n               std::min(ans[i][ii][jj], ans[i - 1][ii][k] + ans[0][k][jj]); n   } n } n int main() { n   scanf( %d%d%d , &n, &m, &r); n   for (int i = 0; i < m; ++i) n     for (int j = 0; j < n; ++j) n       for (int k = 0; k < n; ++k) scanf( %d , &w[i][j][k]); n   prepare(); n   int si, ti, ki; n   while (r-- > 0) { n     scanf( %d%d%d , &si, &ti, &ki); n     --si; n     --ti; n     printf( %d n , ans[ki][si][ti]); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); n const long long mod = 998244353; n int LIMIT; n long long modpow(long long b, long long e) { n   long long ans = 1; n   for (; e; b = b * b % mod, e /= 2) n     if (e & 1) ans = ans * b % mod; n   return ans; n } n const long long root = 62; n inline void ntt(vector<long long>& a, vector<long long>& rt, n                 vector<long long>& rev, int n) { n   for (int i = 0; i < (n); ++i) n     if (i < rev[i]) swap(a[i], a[rev[i]]); n   for (int k = 1; k < n; k *= 2) n     for (int i = 0; i < n; i += 2 * k) n       for (int j = 0; j < (k); ++j) { n         long long z = rt[j + k] * a[i + j + k] % mod, &ai = a[i + j]; n         a[i + j + k] = (z > ai ? ai - z + mod : ai - z); n         ai += (ai + z >= mod ? z - mod : z); n       } n } n vector<long long> conv(const vector<long long>& a, const vector<long long>& b) { n   if (a.empty() || b.empty()) return {}; n   int s = (int)(a).size() + (int)(b).size() - 1, B = 32 - __builtin_clz(s), n       n = 1 << B; n   vector<long long> L(a), R(b), out(n), rt(n, 1), rev(n); n   L.resize(n), R.resize(n); n   for (int i = 0; i < (n); ++i) rev[i] = (rev[i / 2] | (i & 1) << B) / 2; n   long long curL = mod / 2, inv = modpow(n, mod - 2); n   for (int k = 2; k < n; k *= 2) { n     long long z[] = {1, modpow(root, curL /= 2)}; n     for (int i = k; i < (2 * k); ++i) rt[i] = rt[i / 2] * z[i & 1] % mod; n   } n   ntt(L, rt, rev, n); n   ntt(R, rt, rev, n); n   for (int i = 0; i < (n); ++i) n     out[-i & (n - 1)] = L[i] * R[i] % mod * inv % mod; n   ntt(out, rt, rev, n); n   return {out.begin(), out.begin() + s}; n } n void read(int& n) { n   n = 0; n   char c = getchar(); n   while (c >=  0  && c <=  9 ) { n     n = n * 10 + c -  0 ; n     c = getchar(); n   } n } n vector<long long> t; n vector<long long> res[3000005]; n vector<long long> s[3000005]; n int a[200005]; n int cnt[3000005]; n priority_queue<pair<int, int> > pq; n int main() { n   int n; n   read(n); n   LIMIT = n / 2 + 1; n   for (int i = 1; i <= n; i++) { n     read(a[i]); n     cnt[a[i]]++; n     s[a[i]].push_back(1); n   } n   for (int i = 1; i <= n; i++) { n     if (cnt[a[i]]) { n       s[a[i]].push_back(1); n       pq.push({-cnt[a[i]], a[i]}); n       cnt[a[i]] = 0; n     } n   } n   while (!pq.empty()) { n     int v = pq.top().second; n     pq.pop(); n     if (pq.empty()) { n       cout << s[v][n / 2]; n       return 0; n     } n     int u = pq.top().second; n     pq.pop(); n     s[v] = conv(s[v], s[u]); n     while (s[v].size() > LIMIT) s[v].pop_back(); n     pq.push({-s[v].size(), v}); n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 2e5; n bool vis[N]; n int n, m, ans; n vector<int> top; n vector<pair<int, int> > adj[N]; n void dfs(int u) { n   vis[u] = 1; n   for (int i = 0; i < adj[u].size(); i++) { n     int v = adj[u][i].first; n     if (!vis[v]) dfs(v); n   } n   top.push_back(u); n } n int main() { n   cin >> n >> m; n   for (int i = 1; i <= m; i++) { n     int u, v; n     cin >> u >> v; n     adj[u].push_back({v, i}); n   } n   for (int i = 1; i <= n; i++) n     if (!vis[i]) dfs(i); n   for (int i = n - 1; i > 0; i--) { n     int u = top[i], v = top[i - 1], j = 0; n     for (j = 0; j < adj[u].size(); j++) { n       if (adj[u][j].first == v) { n         ans = max(adj[u][j].second, ans); n         break; n       } n     } n     if (j == adj[u].size()) { n       cout <<  -1 n ; n       return 0; n     } n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long num, length, devisor = 1, rest; n   long long i; n   string str; n   cin >> str; n   length = str.length(); n   num = atoi(str.c_str()); n   for (i = 1; i <= length - 1; i++) { n     devisor *= 10; n   } n   rest = num % devisor; n   cout << devisor - rest << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct st { n   long long kici, san; n   long long kici2, san2; n }; n long long v[200010]; n st T[200010 * 4]; n long long t, x, y, q; n long long n, k, S, O; n void upd(long long pos, long long x, long long l, long long r, long long node) { n   if (l == r) { n     v[pos] += x; n     if (v[pos] <= O) n       T[node].kici += x; n     else if (v[pos] - x <= O) n       T[node].san++, T[node].kici -= v[pos] - x; n     if (v[pos] <= S) n       T[node].kici2 += x; n     else if (v[pos] - x <= S) n       T[node].san2++, T[node].kici2 -= v[pos] - x; n     return; n   } n   if (pos <= (l + r) / 2) n     upd(pos, x, l, (l + r) / 2, node * 2); n   else n     upd(pos, x, (l + r) / 2 + 1, r, node * 2 + 1); n   T[node].san = T[node * 2].san + T[node * 2 + 1].san; n   T[node].san2 = T[node * 2].san2 + T[node * 2 + 1].san2; n   T[node].kici = T[node * 2].kici + T[node * 2 + 1].kici; n   T[node].kici2 = T[node * 2].kici2 + T[node * 2 + 1].kici2; n } n long long tap(long long a, long long b, long long x, long long l, long long r, n               long long node) { n   if (l > b || r < a) return 0; n   if (l >= a && r <= b) { n     if (x == 1) n       return T[node].kici + T[node].san * O; n     else n       return T[node].kici2 + T[node].san2 * S; n   } n   return tap(a, b, x, l, (l + r) / 2, node * 2) + n          tap(a, b, x, (l + r) / 2 + 1, r, node * 2 + 1); n } n int main() { n   scanf( %lld%lld%lld%lld%lld , &n, &k, &S, &O, &q); n   while (q--) { n     scanf( %lld%lld , &t, &x); n     if (t == 1) { n       scanf( %lld , &y); n       upd(x, y, 1, n, 1); n     } else n       printf( %lld n , tap(1, x - 1, 1, 1, n, 1) + tap(x + k, n, 2, 1, n, 1)); n   } n } n


#include <bits/stdc++.h> n using namespace std; n vector<int> p; n vector<int> cor; n int total; n void dfs_visit(vector<vector<int>>& adj, int i) { n   cor[i] = 1; n   for (int j : adj[i]) { n     if (cor[j] == 1) { n       total++; n     } n     if (cor[j] == 0) { n       p[j] = i; n       dfs_visit(adj, j); n     } n   } n   cor[i] = 2; n } n int dfs(vector<vector<int>>& adj, int n) { n   int ans = 10; n   for (int i = 1; i <= n; i++) { n     total = 0; n     p.assign(n + 1, 1); n     cor.assign(n + 1, 0); n     total = 0; n     int j = i; n     if (cor[j] == 0) { n       dfs_visit(adj, j); n     } n     for (int j = 1; j <= n; j++) { n       if (cor[j] == 0) { n         dfs_visit(adj, j); n       } n     } n     ans = min(ans, total); n   } n   return ans; n } n int main() { n   int n, m, a, b; n   cin >> n >> m; n   vector<int> vetor; n   vetor.assign(n + 1, 0); n   vector<vector<int>> adj; n   vector<int> lista; n   lista.clear(); n   adj.assign(n + 1, lista); n   for (int i = 1; i <= m; i++) { n     cin >> a >> b; n     adj[a].push_back(b); n   } n   int ans = dfs(adj, n); n   if (ans <= 1) n     cout <<  YES n ; n   else n     cout <<  NO n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int sy, a, b, n, l[200010], r[200010], sum[200010], m, x, y; n double xl, xr, ans; n int getmid(double xi) { n   int L = 1, R = n + 1, ret, mid; n   while (L < R) { n     mid = (L + R) >> 1; n     if (l[mid] <= xi) n       ret = mid, L = mid + 1; n     else n       R = mid; n   } n   return ret; n } n double getlenth(double xi) { n   if (xi <= l[1]) return 0; n   int pos = getmid(xi); n   return min(xi, (double)r[pos]) - l[pos] + sum[pos - 1]; n } n int main() { n   scanf( %d%d%d , &sy, &a, &b); n   scanf( %d , &n); n   sum[0] = 0; n   for (int i = 1; i <= n; ++i) { n     scanf( %d%d , &l[i], &r[i]); n     sum[i] = sum[i - 1] + r[i] - l[i]; n   } n   scanf( %d , &m); n   while (m--) { n     scanf( %d%d , &x, &y); n     xl = -1.0 * (x - a) / (y - sy) * y + x; n     xr = -1.0 * (x - b) / (y - sy) * y + x; n     ans = (getlenth(xr) - getlenth(xl)) / (xr - xl) * (b - a); n     printf( %.10lf n , ans); n   } n   return 0; n } n