#include <bits/stdc++.h> n using namespace std; n int n, k, tot, len[(int)(3e3 + 4)]; n pair<int, int> bound[(int)(3e3 + 4)]; n string s; n vector<int> poz, ans[3000004]; n void fail(); n int main() { n   ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); n   cin >> n >> k; n   cin >> s; n   for (int i = s.size() - 1; i >= 0; --i) n     if (s[i] ==  R ) { n       len[poz.size()] = (s.size() - poz.size()) - i - 1; n       tot += len[poz.size()]; n       poz.push_back(i); n     } n   if (k > tot) fail(); n   int p = 0; n   while (p < poz.size() && !len[p]) ++p; n   bound[p] = {1, len[p]}; n   tot -= len[p]; n   for (int i = p + 1; i < poz.size(); ++i) { n     int l, r, dist, ramas; n     dist = dist = poz[i - 1] - poz[i] - 1; n     l = max(1, bound[i - 1].first + 1 - dist); n     tot -= len[i]; n     if (k - tot - len[i] + 1 >= l) n       bound[i] = {k - tot - len[i] + 1, k - tot}; n     else if (l + len[i] - 1 <= k) n       bound[i] = {l, l + len[i] - 1}; n     else n       fail(); n   } n   for (int i = 0; i < poz.size(); ++i) { n     int p = poz[i]; n     while (bound[i].first <= bound[i].second) { n       ans[bound[i].first].push_back(p + 1); n       ++p; n       ++bound[i].first; n     } n   } n   for (int i = 1; i < k + 1; ++i) { n     printf( %d  , ans[i].size()); n     for (auto it : ans[i]) printf( %d  , it); n     printf(  n ); n   } n   return 0; n } n void fail() { n   cout << -1; n   exit(0); n } n


#include <bits/stdc++.h> n using namespace std; n int SquareRoot(int n) { n   int l = 1, r = n + 1; n   while (l < r) { n     int mid = (l + r) / 2; n     if (n / mid < mid) { n       r = mid; n     } else { n       l = mid + 1; n     } n   } n   --l; n   if (l * l == n) { n     return l; n   } n   return -1; n } n int GCD(int a, int b) { n   while (b > 0) { n     int t = a % b; n     a = b; n     b = t; n   } n   return a; n } n int main() { n   int a = 0, b = 0; n   scanf( %d%d , &a, &b); n   int a2 = a * a; n   int b2 = b * b; n   bool found = false; n   for (int i = a - 1; i > 0; --i) { n     int j = SquareRoot(a2 - i * i); n     if (j < 0) { n       continue; n     } n     int gcd = GCD(i, j); n     int i_norm = i / gcd; n     int j_norm = j / gcd; n     int squ = i_norm * i_norm + j_norm * j_norm; n     if (b2 % squ != 0) { n       continue; n     } n     int mul = SquareRoot(b2 / squ); n     if (mul < 0) { n       continue; n     } n     found = true; n     int i2 = i_norm * mul; n     int j2 = j_norm * mul; n     if (i2 == j) { n       continue; n     } n     printf( YES n ); n     printf( 0 0 n ); n     printf( %d %d n , i, j); n     printf( %d %d n , -j2, i2); n     break; n   } n   if (!found) { n     printf( NO n ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n using ll = long long; n const int inf = 0x3f3f3f3f; n const ll INF = 0x3f3f3f3f3f3f3f3fLL; n const int MAXN = 1e6 + 5; n int arr[MAXN], n; n ll fun(ll x) { n   if (x == 1) return INF; n   ll sum = 0, y = 0; n   for (int i = 0; i < n; i++) { n     y += arr[i]; n     y %= x; n     sum += min(y, x - y); n   } n   return sum; n } n int main() { n   std::ios::sync_with_stdio(false), cin.tie(0), cout.tie(0); n   ll sum = 0, ans = INF; n   cin >> n; n   for (int i = 0; i < n; i++) cin >> arr[i], sum += arr[i]; n   if (sum < 2) { n     cout << -1 <<   n ; n     return 0; n   } n   for (ll i = 2; i * i <= sum; i++) { n     if (sum % i == 0) ans = min(ans, fun(i)); n     while (sum % i == 0) sum /= i; n   } n   ans = min(ans, fun(sum)); n   cout << ans <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n #pragma warning(disable : 4996) n using namespace std; n using LL = int64_t; n using pii = pair<int, int>; n const double PI = 2 * asin(1); n int g_n; n string g_s; n vector<int> vi_in; n vector<LL> vll_sum; n void pretreat() {} n void input() { n   vi_in.clear(), vi_in.resize(g_n + 1); n   vll_sum.clear(), vll_sum.resize(g_n + 1); n   for (int i = 1, t; i <= g_n; ++i) { n     scanf(  %d , &vi_in[i]); n   } n   cin >> g_s; n   g_s =     + g_s; n } n void solve() { n   LL mx = 0, sum = 0; n   for (int i = 1; i <= g_n; ++i) { n     if (g_s[i] ==  B ) { n       vll_sum[i] = vll_sum[i - 1] + vi_in[i]; n       mx = max(mx, vll_sum[i]); n     } else { n       vll_sum[i] = vll_sum[i - 1]; n     } n   } n   sum = 0; n   for (int i = 1; i <= g_n; ++i) { n     if (g_s[i] ==  A ) { n       sum += vi_in[i]; n       mx = max(mx, sum + (vll_sum[g_n] - vll_sum[i])); n     } n   } n   sum = 0; n   for (int i = g_n; i > 0; --i) { n     if (g_s[i] ==  A ) { n       sum += vi_in[i]; n       mx = max(mx, sum + vll_sum[i]); n     } n   } n   cout << mx << endl; n } n int main() { n   pretreat(); n   int t; n   while (cin >> g_n) { n     input(); n     solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n #pragma comment(linker,  /STACK:102400000,102400000 ) n using namespace std; n template <class T, class U> n inline void Max(T &a, U b) { n   if (a < b) a = b; n } n template <class T, class U> n inline void Min(T &a, U b) { n   if (a > b) a = b; n } n inline void add(int &a, int b) { n   a += b; n   while (a >= 1000003) a -= 1000003; n } n int pow(int a, int b) { n   int ans = 1; n   while (b) { n     if (b & 1) ans = ans * (long long)a % 1000003; n     a = (long long)a * a % 1000003; n     b >>= 1; n   } n   return ans; n } n int f[700005], inv[700005], M; n int main() { n   int T = 100, j, k, i, ca = 0, m, n; n   scanf( %d%d , &n, &m); n   M = n + m; n   f[0] = 1; n   for (int i = 1; i < M; i++) f[i] = f[i - 1] * (long long)i % 1000003; n   inv[M - 1] = pow(f[M - 1], 1000003 - 2); n   for (int i = M - 1 - 1; i >= 0; i--) n     inv[i] = inv[i + 1] * (long long)(i + 1) % 1000003; n   long long ans = 0; n   for (int i = 1; i < n + 1; i++) { n     ans += 1LL * f[i + m - 1] * inv[i] % 1000003; n   } n   ans %= 1000003; n   printf( %d n , ans * inv[m - 1] % 1000003); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<long long> v; n int main() { n   long long n, res = 0, i; n   cin >> n; n   long long ans = (n * (n + 1)) / 2; n   if (ans % 2 == 0) n     cout << 0 << endl; n   else n     cout << 1 << endl; n   ans = ans / 2; n   for (i = n; i >= 1; i--) { n     res += i; n     if (res > ans) break; n     v.push_back(i); n   } n   res -= i; n   if (res < ans) v.push_back(ans - res); n   int sz = v.size(); n   cout << sz <<    ; n   for (int i = 0; i < sz; i++) cout << v[i] <<    ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 3e5 + 5; n int a[maxn]; n int n; n int vis[maxn]; n vector<int> ans; n int main() { n   memset(vis, 0, sizeof(vis)); n   ans.clear(); n   scanf( %d , &n); n   for (int i = 1; i <= n; i++) { n     scanf( %d , &a[i]); n   } n   int cnt = 0; n   int ma = 0; n   for (int i = 1; i <= n; i++) { n     int l, r; n     for (l = i - 1; l >= 1; l--) { n       if (a[l] % a[i] != 0) break; n     } n     for (r = i + 1; r <= n; r++) { n       if (a[r] % a[i] != 0) break; n     } n     l++, r--; n     if (r - l == ma) n       ans.push_back(l); n     else if (r - l > ma) { n       ans.clear(); n       ma = r - l; n       ans.push_back(l); n     } n     i = r; n   } n   sort(ans.begin(), ans.end()); n   cout << ans.size() <<     << ma << endl; n   for (int i = 0; i < ans.size(); i++) { n     cout << ans[i]; n     if (i != ans.size() - 1) cout <<    ; n   } n   cout <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   long long n; n   cin >> n; n   long long x, a, b, i, f = 1, pa, push_back; n   vector<pair<long long, long long> > v(n); n   vector<long long> ch; n   cin >> x; n   cin >> a >> b; n   pa = a, push_back = b; n   if (a + b == 7) f = 0; n   for (i = 1; i < n; i++) { n     cin >> a >> b; n     if (f == 1) { n       if (a + b != 7) { n         if (a == pa || a == 7 - pa || a == push_back || a == 7 - push_back) { n           if (b == pa || b == 7 - pa || b == push_back || b == 7 - push_back) { n             f = 1; n           } else n             f = 0; n         } else n           f = 0; n       } else n         f = 0; n     } n     pa = a, push_back = b; n   } n   if (f == 1) { n     cout <<  YES  << endl; n   } else { n     cout <<  NO  << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios::sync_with_stdio(false); n   ; n   int n, m; n   cin >> n >> m; n   int dem = 0; n   if (m % n != 0) { n     cout << -1; n     return 0; n   } n   int sum = m / n; n   while (sum % 2 == 0) { n     sum /= 2; n     dem++; n   } n   while (sum % 3 == 0) { n     sum /= 3; n     dem++; n   } n   if (sum == 1) n     cout << dem; n   else n     cout << -1; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void dfs(long long a, vector<long long>& d, vector<vector<long long>>& adj, n          vector<long long>& vis, long long dist) { n   d[a] = dist; n   vis[a]++; n   for (auto b : adj[a]) { n     if (!vis[b]) { n       dfs(b, d, adj, vis, dist + 1); n     } n   } n } n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   long long t; n   cin >> t; n   while (t--) { n     long long n, a, b, da, db; n     cin >> n >> a >> b >> da >> db; n     vector<vector<long long>> adj(n + 1); n     for (long long i = 0; i < n - 1; i++) { n       long long u, v; n       cin >> u >> v; n       adj[u].push_back(v); n       adj[v].push_back(u); n     } n     if (da * 2 >= db) { n       cout <<  Alice n ; n       continue; n     } n     vector<long long> d(n + 1); n     vector<long long> vis(n + 1); n     dfs(b, d, adj, vis, 0); n     if (d[a] <= da) { n       cout <<  Alice n ; n       continue; n     } n     long long mx = 0; n     for (long long i = 1; i < n + 1; i++) { n       mx = max(mx, d[i]); n     } n     vector<long long> d2(n + 1); n     vector<long long> vis2(n + 1); n     for (long long i = 1; i < n + 1; i++) { n       if (d[i] == mx) { n         dfs(i, d2, adj, vis2, 0); n         break; n       } n     } n     long long mx2 = 0; n     for (long long i = 1; i < n + 1; i++) { n       mx2 = max(mx2, d2[i]); n     } n     long long di = mx2; n     if (di > 2 * da) n       cout <<  Bob n ; n     else n       cout <<  Alice n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n int dx[] = {0, 0, 1, -1}; n int dy[] = {1, -1, 0, 0}; n using namespace std; n vector<long long int> v, v2, v3; n vector<long long int>::iterator it1, it2; n int ar[200]; n int main() { n   long long int tcase = 1; n   while (tcase--) { n     long long int n, k, a, ans = 0; n     scanf( %lld %lld , &n, &k); n     for (int i = 0; i < n; i++) { n       scanf( %lld , &a); n       ar[a % k]++; n     } n     ans += ar[0] / 2; n     ar[0] = 0; n     for (int i = 1; i <= (k / 2) + 1; i++) { n       if (ar[i]) { n         if (ar[k - i] && i != (k - i)) { n           ans += min(ar[i], ar[k - i]); n           ar[i] = ar[k - i] = 0; n         } else if (ar[k - i] && (i == k - i)) { n           ans += ar[i] / 2; n           ar[i] = 0; n         } n       } n     } n     printf( %lld n , 2 * ans); n   } n } n


#include <bits/stdc++.h> n template <typename T> n inline void read(T& x) { n   T res = 0, flag = 1; n   char ch; n   ch = getchar(); n   while (!isdigit(ch)) { n     if (ch ==  - ) flag = -1; n     ch = getchar(); n   } n   while (isdigit(ch)) { n     res = res * 10 + ch - 48; n     ch = getchar(); n   } n   x = res * flag; n } n const int N = 100000 + 1; n int n, k, x; n bool vis[N], pre[N], suf[N]; n int main() { n   read(n), read(k); n   long long ans = n * 3 - 2; n   for (int i = 1; i <= k; ++i) { n     read(x); n     if (!vis[x]) --ans; n     vis[x] = 1; n     if (vis[x - 1] && !pre[x]) --ans, pre[x] = 1; n     if (vis[x + 1] && !suf[x]) --ans, suf[x] = 1; n   } n   printf( %lld n , ans); n } n


#include <bits/stdc++.h> n using namespace std; n inline char gc() { n   static char buf[1 << 16], *S, *T; n   if (S == T) { n     T = (S = buf) + fread(buf, 1, 1 << 16, stdin); n     if (T == S) return EOF; n   } n   return *S++; n } n inline int read() { n   int x = 0, f = 1; n   char ch = gc(); n   while (ch <  0  || ch >  9 ) { n     if (ch ==  - ) f = -1; n     ch = gc(); n   } n   while (ch >=  0  && ch <=  9 ) x = x * 10 + ch -  0 , ch = gc(); n   return x * f; n } n int n, K, m, c[100010 + 500010][2], v[100010 + 500010], fa[100010 + 500010], n     mn[100010 + 500010], ans[100010], cc[100010], q[100010]; n bool rev[100010 + 500010]; n struct edge { n   int x, y; n } e[500010]; n inline bool cmp(edge a, edge b) { return a.y < b.y; } n struct query { n   int l, r, id; n   friend bool operator<(query a, query b) { return a.r < b.r; } n } qq[100010]; n inline void add(int x, int val) { n   for (; x <= n; x += x & -x) cc[x] += val; n } n inline int ask(int x) { n   int res = 0; n   for (; x; x -= x & -x) res += cc[x]; n   return res; n } n inline bool isroot(int x) { return x != c[fa[x]][0] && x != c[fa[x]][1]; } n inline void update(int p) { n   int l = c[p][0], r = c[p][1]; n   mn[p] = p; n   if (v[mn[l]] < v[mn[p]]) mn[p] = mn[l]; n   if (v[mn[r]] < v[mn[p]]) mn[p] = mn[r]; n } n inline void pushdown(int p) { n   if (!rev[p]) return; n   rev[p] = 0; n   swap(c[p][0], c[p][1]); n   rev[c[p][0]] ^= 1; n   rev[c[p][1]] ^= 1; n } n inline void rotate(int x) { n   int y = fa[x], z = fa[y], l = x == c[y][1], r = l ^ 1; n   if (!isroot(y)) c[z][y == c[z][1]] = x; n   fa[c[x][r]] = y; n   fa[y] = x; n   fa[x] = z; n   c[y][l] = c[x][r]; n   c[x][r] = y; n   update(y); n   update(x); n } n inline void splay(int x) { n   int top = 0; n   q[++top] = x; n   for (int xx = x; !isroot(xx); xx = fa[xx]) q[++top] = fa[xx]; n   while (top) pushdown(q[top--]); n   while (!isroot(x)) { n     int y = fa[x], z = fa[y]; n     if (!isroot(y)) { n       if (x == c[y][1] ^ y == c[z][1]) n         rotate(x); n       else n         rotate(y); n     } n     rotate(x); n   } n } n inline void access(int x) { n   int y = 0; n   while (x) { n     splay(x); n     c[x][1] = y; n     update(x); n     y = x; n     x = fa[x]; n   } n } n inline void makeroot(int x) { n   access(x); n   splay(x); n   rev[x] ^= 1; n } n inline int find(int x) { n   access(x); n   splay(x); n   while (c[x][0]) x = c[x][0]; n   return x; n } n inline void link(int x, int y) { n   makeroot(x); n   fa[x] = y; n } n inline void cut(int x, int y) { n   makeroot(x); n   access(y); n   splay(y); n   c[y][0] = fa[x] = 0; n   update(y); n } n inline int qmin(int x, int y) { n   makeroot(x); n   access(y); n   splay(y); n   return mn[y]; n } n inline void ins(int id) { n   int x = e[id].x, y = e[id].y; n   if (find(x) != find(y)) { n     link(x, id + n), link(id + n, y), add(n - x + 1, 1); n     return; n   } n   int z = qmin(x, y); n   if (v[z] >= v[id + n]) return; n   cut(z, e[z - n].x); n   cut(z, e[z - n].y); n   add(n - v[z] + 1, -1); n   link(x, id + n); n   link(id + n, y); n   add(n - x + 1, 1); n } n int main() { n   n = read(); n   K = read(); n   m = read(); n   v[0] = 0x3f3f3f3f; n   for (int i = 1; i <= m; ++i) { n     e[i].x = read(); n     e[i].y = read(); n     if (e[i].x > e[i].y) swap(e[i].x, e[i].y); n   } n   sort(e + 1, e + m + 1, cmp); n   int Q = read(); n   for (int i = 1; i <= m; ++i) v[i + n] = e[i].x; n   for (int i = 1; i <= n; ++i) v[i] = 0x3f3f3f3f; n   for (int i = 1; i <= Q; ++i) qq[i].l = read(), qq[i].r = read(), qq[i].id = i; n   sort(qq + 1, qq + Q + 1); n   int now = 1; n   for (int i = 1; i <= Q; ++i) { n     while (now <= m && e[now].y <= qq[i].r) ins(now), ++now; n     ans[qq[i].id] = qq[i].r - qq[i].l + 1 - ask(n - qq[i].l + 1); n   } n   for (int i = 1; i <= Q; ++i) printf( %d n , ans[i]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n bool check(long long y) { n   for (long long x = 2; x * x <= y; x++) { n     if (y % x == 0) return 0; n   } n   return 1; n } n int main() { n   std::cin.tie(0); n   std::ios::sync_with_stdio(false); n   long long n, x, y; n   cin >> n; n   for (int i = 0; i < n; i++) { n     cin >> y >> x; n     if (y - x == 1 && check(x + y)) { n       cout <<  Yes  << endl; n     } else { n       cout <<  No  << endl; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e5 + 5; n int a[N], b[N], seg[4 * N], lazy[4 * N], n, diff[N]; n void update(int sq, int eq, int val, int id = 1, int s = 1, int e = n) { n   if (lazy[id] != 0) { n     seg[id] = lazy[id]; n     if (s != e) lazy[id * 2] = lazy[id * 2 + 1] = lazy[id]; n     lazy[id] = 0; n   } n   if (s > eq || e < sq) return; n   if (s >= sq && e <= eq) { n     seg[id] = val; n     if (s != e) lazy[id * 2] = lazy[id * 2 + 1] = val; n     return; n   } n   int mid = (s + e) / 2; n   update(sq, eq, val, id * 2, s, mid); n   update(sq, eq, val, id * 2 + 1, mid + 1, e); n } n int query(int idx, int id = 1, int s = 1, int e = n) { n   if (lazy[id] != 0) { n     seg[id] = lazy[id]; n     if (s != e) lazy[id * 2] = lazy[id * 2 + 1] = lazy[id]; n     lazy[id] = 0; n   } n   if (s == e) return seg[id]; n   int mid = (s + e) / 2; n   if (idx <= mid) n     return query(idx, id * 2, s, mid); n   else n     return query(idx, id * 2 + 1, mid + 1, e); n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   int m; n   cin >> n >> m; n   for (int i = 1; i <= n; i++) cin >> a[i]; n   for (int i = 1; i <= n; i++) cin >> b[i]; n   for (int i = 1; i <= m; i++) { n     int t; n     cin >> t; n     if (t == 2) { n       int x; n       cin >> x; n       int cur = query(x); n       if (cur == 0) n         cout << b[x] <<   n ; n       else n         cout << a[x + diff[cur]] <<   n ; n     } else { n       int x, y, k; n       cin >> x >> y >> k; n       diff[i] = x - y; n       update(y, y + k - 1, i); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 1e5; n int c[4][11][11][maxn + 5]; n char s[maxn + 50]; n char t[15]; n int n; n int getnum(char c) { n   if (c ==  A ) return 0; n   if (c ==  T ) return 1; n   if (c ==  G ) return 2; n   if (c ==  C ) return 3; n } n int lowbit(int x) { return x & (-x); } n void add(int *c, int k, int x) { n   for (int i = k; i <= n; i += lowbit(i)) { n     c[i] += x; n   } n } n int query(int *c, int k) { n   int ans = 0; n   for (int i = k; i; i -= lowbit(i)) ans += c[i]; n   return ans; n } n int main() { n   scanf( %s , s + 1); n   n = strlen(s + 1); n   for (int ch = 0; ch < 4; ++ch) n     for (int i = 1; i <= 10; ++i) n       for (int j = 1; j <= i; ++j) n         for (int k = j; k <= n; k += i) n           if (getnum(s[k]) == ch) { n             add(c[ch][i][j], (k - j) / i + 1, 1); n           } n   int q; n   scanf( %d , &q); n   while (q--) { n     int op, x, l, r; n     char ch; n     scanf( %d , &op); n     if (op == 1) { n       scanf( %d %c , &x, &ch); n       int id = getnum(s[x]); n       for (int i = 1; i <= 10; ++i) { n         int j = x % i; n         if (j == 0) j = i; n         add(c[id][i][j], (x - j) / i + 1, -1); n       } n       s[x] = ch; n       id = getnum(s[x]); n       for (int i = 1; i <= 10; ++i) { n         int j = x % i; n         if (j == 0) j = i; n         add(c[id][i][j], (x - j) / i + 1, 1); n       } n     } n     if (op == 2) { n       scanf( %d%d%s , &l, &r, t + 1); n       int len = strlen(t + 1); n       int ans = 0; n       for (int k = 1; k <= len && k <= r - l + 1; ++k) { n         int id = getnum(t[k]); n         int i = len; n         int j = (l + k - 1) % i; n         if (j == 0) j = i; n         ans += query(c[id][i][j], (r - j) / i + 1) - n                query(c[id][i][j], (l + k - 1 - j) / i); n       } n       printf( %d n , ans); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct laptop { n   long long p, b, ind; n   laptop() {} n   laptop(long long a, long long b, long long c) : p(a), b(b), ind(c) {} n   bool operator<(const laptop& o) const { n     return ((long double)o.b * p > (long double)b * o.p); n   } n }; n int n, k; n vector<laptop> laptops; n bool check(long long q) { n   priority_queue<laptop> pq; n   for (auto& x : laptops) { n     pq.push(x); n   } n   laptop here(0, 1, 1); n   for (int i = 0; i < k; ++i) { n     laptop u = pq.top(); n     pq.pop(); n     here.p = i; n     if (here < u) { n       return false; n     } n     u.p += q; n     pq.push(u); n   } n   return true; n } n long long bs() { n   const long long THIGH = 2000000000001LL; n   long long lo = 0, hi = THIGH; n   while (lo < hi) { n     long long m = (lo + hi) / 2; n     if (check(m)) { n       hi = m; n     } else { n       lo = m + 1; n     } n   } n   if (lo == THIGH) return -1; n   return lo; n } n int main() { n   ios_base::sync_with_stdio(0), cin.tie(0); n   cin >> n >> k; n   laptops.resize(n); n   for (int i = 0; i < n; ++i) { n     cin >> laptops[i].p; n     laptops[i].ind = i; n   } n   for (int i = 0; i < n; ++i) { n     cin >> laptops[i].b; n   } n   cout << bs() << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long m, n, a, b, c, ans, ar[1000001], k, paths[200010], visit[200010], n     ans1, dp[400010], t, er[200110]; n bool boo; n bool compare(long long a, long long b) { n   if (a >= b) return 1; n   return 0; n } n int main() { n   string s; n   std::ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   cin >> n >> m; n   for (long long i = 0; i < n; i++) { n     cin >> a; n     er[i] = a; n     a %= m; n     ar[a]++; n   } n   for (long long i = 0; i < m; i++) { n     if (ar[i] >= 2) { n       cout << 0; n       return 0; n     } n   } n   ans = 1; n   sort(er, er + n); n   for (long long i = n - 1; i >= 0; i--) { n     for (long long j = 0; j < i; j++) { n       ans *= (er[i] - er[j]) % m; n       ans %= m; n     } n   } n   cout << ans % m; n } n


#include <bits/stdc++.h> n using namespace std; n int a[100005]; n int themax(int b[], int k) { n   int sum = b[0]; n   for (int z = 1; z < k; z++) { n     sum += (b[z] + 1); n   } n   return sum; n } n int main() { n   int n, x, max = 0, min = 0; n   cin >> n >> x; n   for (int i = 0; i < n; i++) { n     cin >> a[i]; n   } n   max = themax(a, n); n   if (x == max) n     cout <<  YES  << endl; n   else n     cout <<  NO  << endl; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 1e4 + 10; n const int maxk = 2e4 + 10; n const int inf = 0x3f3f3f3f; n int n, m, k; n bool g[maxn][maxn]; n struct Block { n   int l, r, b, t; n }; n Block b[maxk]; n int bnum; n bool vis[maxk]; n int d[maxk]; n int res; n int start, stop; n void bfs() { n   memset(d, 0, sizeof d); n   res = inf; n   queue<int> q; n   q.push(start); n   while (q.size()) { n     int u = q.front(); n     q.pop(); n     vis[u] = true; n     if (u == stop) { n       res = min(d[u], res); n       continue; n     } else if (b[u].b >= n - 1 || b[u].r >= m - 1) { n       res = min(d[u] + 1, res); n       continue; n     } n     for (int i = 0; i < bnum; i++) { n       if (vis[i]) continue; n       if (b[u].t > b[i].b + 2 && (b[u].l > b[i].r + 2 || b[u].r < b[i].l - 2)) n         continue; n       if (b[u].b < b[i].t - 2 && (b[u].l > b[i].r + 2 || b[u].r < b[i].l - 2)) n         continue; n       vis[i] = true; n       d[i] = d[u] + 1; n       q.push(i); n     } n   } n } n bool dvis[maxn][maxn]; n void dfs(int r, int c, int bnum) { n   if (dvis[r][c]) return; n   if (r == 1 && c == 1) start = bnum; n   if (r == n && c == m) stop = bnum; n   dvis[r][c] = 1; n   b[bnum].l = min(b[bnum].l, c); n   b[bnum].r = max(b[bnum].r, c); n   b[bnum].b = max(b[bnum].b, r); n   b[bnum].t = min(b[bnum].t, r); n   if (r > 1 && g[r - 1][c]) dfs(r - 1, c, bnum); n   if (r < n && g[r + 1][c]) dfs(r + 1, c, bnum); n   if (c > 1 && g[r][c - 1]) dfs(r, c - 1, bnum); n   if (c < m && g[r][c + 1]) dfs(r, c + 1, bnum); n } n int main() { n   memset(g, 0, sizeof g); n   scanf( %d%d%d , &n, &m, &k); n   for (int i = 0; i < k; i++) { n     int r, c; n     scanf( %d%d , &r, &c); n     g[r][c] = 1; n   } n   bnum = 0; n   start = stop = -1; n   for (int i = 1; i <= n; i++) { n     for (int j = 1; j <= m; j++) { n       if (g[i][j] && !dvis[i][j]) { n         b[bnum].r = b[bnum].l = j; n         b[bnum].b = b[bnum].t = i; n         dfs(i, j, bnum); n         bnum++; n       } n     } n   } n   bfs(); n   if (res < inf) n     printf( %d n , res); n   else n     printf( -1 n ); n } n


#include <bits/stdc++.h> n using namespace std; n int a, s, d[10004][2], f, g, h, j, k, l, i, n, m; n string x, z; n set<string> q; n bool chk(int idx, int dis) { n   for (int i = idx; i < idx + dis; i++) { n     if (x[i] != x[i + dis]) return true; n   } n   return false; n } n int main() { n   cin >> x; n   n = x.size(); n   if (n >= 7) { n     d[n - 2][0] = 1; n     z = z + x[n - 2] + x[n - 1]; n     q.insert(z); n   } n   if (n >= 8) { n     d[n - 3][1] = 1; n     z = x[n - 3] + z; n     q.insert(z); n   } n   for (i = n - 4; i >= 5; i--) { n     d[i][0] = d[i + 2][1]; n     if (chk(i, 2)) d[i][0] |= d[i + 2][0]; n     if (d[i][0]) { n       z =   ; n       z = z + x[i] + x[i + 1]; n       q.insert(z); n     } n     d[i][1] = d[i + 3][0]; n     if (chk(i, 3)) d[i][1] |= d[i + 3][1]; n     if (d[i][1]) { n       z =   ; n       z = z + x[i] + x[i + 1] + x[i + 2]; n       q.insert(z); n     } n   } n   cout << q.size() << endl; n   for (set<string>::iterator it = q.begin(); it != q.end(); it++) { n     cout << *it << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n bool IsPrime(int x) { n   for (int i = 2; i * i <= x; i++) n     if (x % i == 0) return false; n   return true; n } n int modpow(int w, int p, int m) { n   if (p == 0) return 1; n   int x = modpow(w, p / 2, m); n   x = 1LL * x * x % m; n   if (p & 1) x = 1LL * x * w % m; n   return x; n } n int main() { n   int L, x, p; n   scanf( %d%d , &L, &x); n   p = L + 1; n   if ((x == 2) || !IsPrime(p)) { n     puts( -1 ); n     return 0; n   } n   if (L == 1) { n     printf( %d n , x - 1); n     return 0; n   } n   for (int b = x - 1; b > 1; b--) { n     if (modpow(b, L, p) != 1) continue; n     if (b % p == 0) continue; n     long long t = 0, r = 1, n = 0, x, d; n     while (1) { n       t = t + 1; n       x = r * b; n       d = x / p; n       r = x % p; n       n = n * b + d; n       if (r == 1) break; n       if (t > p / 2) { n         printf( %d n , b); n         return 0; n       } n     } n     if (t == L) { n       printf( %d n , b); n       return 0; n     } n   } n   puts( -1 ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   long n, a[100009], m = 0; n   cin >> n; n   for (long i = long(0); i < long(n); i++) { n     cin >> a[i]; n     m = max(m, a[i]); n   } n   long c = 0, q = 0; n   for (long i = long(0); i < long(n); i++) { n     if (a[i] == m) n       c++; n     else { n       q = max(q, c); n       c = 0; n     } n   } n   q = max(q, c); n   cout << q <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n struct dat { n   int L, R, V; n } a[100005]; n int N, M; n bool cmp(dat a, dat b) { n   if (a.L != b.L) return a.L < b.L; n   if (a.R != b.R) return a.R < b.R; n   return a.V < b.V; n } n int ans[100005], s[100005]; n void build(int k) { n   int last = 0; n   for (int i = (1), _b = (M); i <= _b; i++) n     if (a[i].V >> k & 1) { n       for (int j = (max(last + 1, a[i].L)), _b = (a[i].R); j <= _b; j++) n         ans[j] |= (1 << k); n       last = max(last, a[i].R); n     } n   memset(s, 0, sizeof s); n   for (int i = (1), _b = (N); i <= _b; i++) n     s[i] = s[i - 1] + (!(ans[i] >> k & 1)); n   for (int i = (1), _b = (M); i <= _b; i++) n     if (!(a[i].V >> k & 1)) n       if (s[a[i].R] == s[a[i].L - 1]) { n         cout <<  NO n ; n         exit(0); n       } n } n int main(void) { n   cin >> N >> M; n   for (int i = (1), _b = (M); i <= _b; i++) cin >> a[i].L >> a[i].R >> a[i].V; n   sort(a + 1, a + M + 1, cmp); n   for (int i = (0), _b = (29); i <= _b; i++) build(i); n   cout <<  YES n ; n   for (int i = (1), _b = (N); i <= _b; i++) cout << ans[i] <<    ; n   cout << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(false); n   long long int a, b; n   cin >> a >> b; n   long long int sumo = 0, sume = 0; n   for (int i = 1;; i++) { n     sumo = i * i; n     if (sumo > a) { n       cout <<  Vladik  << endl; n       break; n     } n     sume = i * (i + 1); n     if (sume > b) { n       cout <<  Valera  << endl; n       break; n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 155; n int n; n int a[N], b[N], A, B; n int main() { n   scanf( %d , &n); n   a[0] = 1; n   for (int i = 1; i <= n; i++) { n     for (int j = 0; j <= A; j++) b[j + 1] = (b[j + 1] + a[j]) % 2; n     B = max(B, A + 1); n     swap(A, B); n     for (int j = 0; j <= A; j++) swap(a[j], b[j]); n   } n   printf( %d n , A); n   for (int i = 0; i <= A; i++) printf( %d%c , a[i], i == A ?   n  :    ); n   printf( %d n , B); n   for (int i = 0; i <= B; i++) printf( %d%c , b[i], i == B ?   n  :    ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int inf = 0x3f3f3f3f; n const int N = 1E6 + 7; n int n; n int id[N], x[N], y[N]; n bool cmp(int a, int b) { n   if (x[a] < x[b]) return true; n   if (x[a] > x[b]) return false; n   if (x[a] % 2 == 1) n     return y[a] < y[b]; n   else n     return y[a] > y[b]; n } n int main() { n   scanf( %d , &n); n   for (int i = 0; i < n; i++) { n     scanf( %d %d , &x[i], &y[i]); n     x[i] /= 1000; n     y[i] /= 1000; n     id[i] = i; n   } n   sort(id, id + n, cmp); n   for (int i = 0; i < n; i++) { n     printf( %d  , id[i] + 1); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename Arg, typename... Args> n inline void input(Arg& arg, Args&... args) { n   cin >> arg; n   (void)(int[]){0, (void(cin >> args), 0)...}; n } n template <typename Arg, typename... Args> n inline void printErr(Arg&& arg, Args&&... args) { n   cerr << arg; n   (void)(int[]){0, (void(cerr <<     n                               <<   033[1;41m  n                               <<  ,  n                               <<   033[0m  n                               <<     << args), n                     0)...}; n   cerr <<   n ; n }; n template <class T, class... U> n void dbg(const char* sdbg, T h, U... a) { n   cerr <<   033[1;31m  n        <<  Debug:   n        <<   033[0m ; n   cerr << sdbg; n   cerr <<     n        <<   033[1;41m  n        <<  =  n        <<   033[0m  n        <<    ; n   printErr(h, a...); n } n template <class S, class T> n ostream& operator<<(ostream& os, const pair<S, T>& p) { n   return os <<  pair(  << p.first <<   033[1;31m  n             <<  ,   n             <<   033[0m  << p.second <<  ) ; n } n template <class T> n ostream& operator<<(ostream& os, const vector<T>& p) { n   os <<   033[1;32m  n      <<  vector[   n      <<   033[0m ; n   for (auto& it : p) n     os << it <<   033[1;31m  n        <<  ,   n        <<   033[0m ; n   return os <<   033[1;32m  n             <<  ]  n             <<   033[0m ; n } n template <class T> n ostream& operator<<(ostream& os, const set<T>& p) { n   os <<   033[1;32m  n      <<  set[  ; n   for (auto& it : p) n     os << it <<   033[1;31m  n        <<  ,   n        <<   033[0m ; n   return os <<   033[1;32m  n             <<  ]  n             <<   033[0m ; n } n template <class S, class T> n ostream& operator<<(ostream& os, const map<S, T>& p) { n   os <<   033[1;32m  n      <<  map[   n      <<   033[0m ; n   for (auto& it : p) n     os << it <<   033[1;31m  n        <<  ,   n        <<   033[0m ; n   return os <<   033[1;32m  n             <<  ]  n             <<   033[0m ; n } n template <class T> n void dbgIter(const char* sdbg, T a, T b) { n   cerr <<   033[1;31m  n        <<  Debug:   n        <<   033[0m ; n   cerr << sdbg; n   cerr <<   033[1;31m  n        <<   =   n        <<   033[0m ; n   cerr <<  [ ; n   for (T i = a; i != b; ++i) { n     if (i != a) cerr <<  ,  ; n     cerr << *i; n   } n   cerr <<  ] n ; n } n template <typename T> n using min_heap = priority_queue<T, std::vector<T>, std::greater<T>>; n template <typename T> n using max_heap = priority_queue<T>; n template <typename T, typename S> n inline bool contains(map<T, S> map1, S value1) { n   return map1.find(value1) != map1.end(); n } n template <typename T, typename S> n inline bool contains(T container1, S value1) { n   return container1.find(value1) != container1.end(); n } n class DisjoinSetUnion { n  public: n   int32_t p[1010]; n   DisjoinSetUnion() { iota(begin(p), end(p), 0); } n   int32_t root1(int32_t t_v) { n     return (p[t_v] == t_v) ? t_v : (p[t_v] = root1(p[t_v])); n   } n   void union1(int32_t t_a, int32_t t_b) { p[root1(t_a)] = root1(t_b); } n }; n int32_t n, m; n void solve() { n   cin >> n >> m; n   int32_t v1, v2; n   DisjoinSetUnion dsu; n   for (int64_t(i) = (0); (i) < (m); ++(i)) { n     cin >> v1 >> v2; n     dsu.union1(v1, v2); n   } n   for (int64_t(i) = (2); (i) <= (n); ++(i)) n     if (dsu.root1(i) != dsu.root1(1)) { n       cout <<  no n ; n       return; n     } n   if ((n - 1) == m) n     cout <<  yes n ; n   else n     cout <<  no n ; n } n int32_t main() { n   std::ios_base::sync_with_stdio(false); n   cin.tie(nullptr); n   cout.tie(nullptr); n   cout.precision(20); n   cout << fixed; n   solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 500002; n long long a[maxn]; n int n, k, d; n void enter() { n   cin >> n >> k >> d; n   for (int i = (1), _b = (n + 1); i < _b; ++i) cin >> a[i]; n } n int seg[4 * maxn]; n void update(int l, int r, int st, int q) { n   if (l == r) n     seg[st] = 1; n   else { n     int mid = (l + r) >> 1; n     if (mid >= q) n       update(l, mid, st * 2, q); n     else n       update(mid + 1, r, st * 2 + 1, q); n     seg[st] = max(seg[st * 2], seg[st * 2 + 1]); n   } n } n int _get(int l, int r, int u, int v, int st) { n   if (u <= l && r <= v) n     return seg[st]; n   else if (l > v || r < u) n     return 0; n   else { n     int mid = (l + r) >> 1; n     return max(_get(l, mid, u, v, st * 2), _get(mid + 1, r, u, v, st * 2 + 1)); n   } n } n void solve() { n   sort(a + 1, a + n + 1); n   update(0, n, 1, 0); n   for (int i = (1), _b = (n + 1); i < _b; ++i) { n     int pos = lower_bound(a + 1, a + n + 1, a[i] - d) - a; n     if (pos <= i - k + 1) { n       if (_get(0, n, pos - 1, i - k, 1) == 1) { n         if (i == n) { n           cout <<  YES ; n           exit(0); n         } else n           update(0, n, 1, i); n       } n     } n   } n   cout <<  NO ; n } n int main() { n   enter(); n   solve(); n } n


#include <bits/stdc++.h> n using namespace std; n char s[200005]; n char x[26]; n int n, m; n char a, b; n int main() { n   while (scanf( %d%d , &n, &m) != EOF) { n     for (int i = 0; i < 26; i++) x[i] = i +  a ; n     getchar(); n     scanf( %s , s); n     getchar(); n     for (int i = 0; i < m; i++) { n       scanf( %c , &a); n       getchar(); n       scanf( %c , &b); n       getchar(); n       if (a == b) continue; n       for (int j = 0; j < 26; j++) { n         if (x[j] == a) { n           x[j] = b; n           continue; n         } n         if (x[j] == b) { n           x[j] = a; n           continue; n         } n       } n     } n     int k; n     for (int i = 0; i < n; i++) { n       k = s[i] -  a ; n       printf( %c , x[k]); n     } n     printf(  n ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   string ch, t; n   int n, s, i; n   bool ok1 = 0, ok2 = 0; n   scanf( %d %d , &n, &s); n   if (9 * n >= s) { n     if (n == 1 && s == 0) { n       cout << 0 <<     << 0 << endl; n     } else if (s == 0) { n       cout << -1 <<     << -1 << endl; n     } else { n       while (n--) { n         ch += min(9, s) +  0 ; n         t = ch; n         s -= 9; n         if (s < 0) s = 0; n       } n       reverse(t.begin(), t.end()); n       if (t[0] ==  0 ) { n         t[0] =  1 ; n         for (i = 1; i < t.size(); i++) { n           if (t[i] >  0 ) { n             t[i] = t[i] - 1; n             break; n           } n         } n       } n       cout << t <<     << ch << endl; n     } n   } else { n     cout << -1 <<     << -1 << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long M = 1000000007; n long long n, k; n long long a[100050]; n long long cnt[100050]; n map<long long, long long> dict; n long long f[100050]; n long long fac[100000 + 50], ifac[100000 + 50]; n long long pwk[100050]; n long long ksm(long long a, long long x = M - 2) { n   long long ret = 1; n   for (; x; x >>= 1, a = a * a % M) n     if (x & 1) ret = ret * a % M; n   return ret; n } n long long C(long long n, long long m) { n   if (m < 0 || m > n) return 0; n   return fac[n] * ifac[m] % M * ifac[n - m] % M; n } n void prepare() { n   fac[0] = 1; n   for (int i = 1; i <= 100000; i++) fac[i] = fac[i - 1] * i % M; n   ifac[100000] = ksm(fac[100000]); n   for (int i = 100000 - 1; i >= 0; i--) ifac[i] = ifac[i + 1] * (i + 1) % M; n } n int main() { n   ios_base::sync_with_stdio(false); n   prepare(); n   cin >> n >> k; n   for (int i = 1; i <= n; i++) { n     cin >> a[i]; n     dict[a[i]]++; n   } n   cnt[0] = k; n   for (auto y : dict) n     if (y.first != -1) cnt[y.second]++, cnt[0]--; n   int r = 0; n   if (dict.find(-1) != dict.end()) r = dict[-1]; n   for (int i = 1; i < n; i++) { n     f[i + 1] = ksm(M + i - n) * n                ((k - 1) * i % M * f[i - 1] % M + n                 ((M + 2 - k) * i % M - n + M) * f[i] % M + i * k % M) % n                M; n   } n   long long invpwkr = ksm(ksm(k, r)); n   pwk[0] = 1; n   for (int i = 1; i <= n; i++) pwk[i] = pwk[i - 1] * (k - 1) % M; n   long long ans = (M - (f[0] * (k - 1) + f[n]) % M) % M; n   for (int i = 0; i <= n; i++) n     if (cnt[i] != 0) { n       for (int j = 0; j <= r; j++) n         ans = (ans + n                C(r, j) * f[i + j] % M * cnt[i] % M * invpwkr % M * pwk[r - j]) % n               M; n     } n   cout << ans <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n deque<char> D; n int main() { n   int n; n   cin >> n; n   string s; n   cin >> s; n   for (int i = 0; i < n; i++) n     if (D.size() % 2 == 0 || D.back() != s[i]) D.push_back(s[i]); n   if (D.size() % 2 == 1) D.pop_back(); n   cout << n - D.size() << endl; n   for (int i = 0; i < D.size(); i++) cout << D[i]; n   cout << endl; n   return 0; n } n


#include <bits/stdc++.h> n int const MAX = 1e5 + 5; n int a[MAX], need[MAX]; n bool has[MAX], pos[MAX]; n int main() { n   int n, num = 0; n   scanf( %d , &n); n   memset(has, false, sizeof(has)); n   memset(pos, false, sizeof(pos)); n   for (int i = 0; i < n; i++) { n     scanf( %d , &a[i]); n     if (!has[a[i]] && a[i] <= n) n       has[a[i]] = true; n     else n       pos[i] = true; n   } n   int cnt = 0; n   for (int i = 1; i <= n; i++) n     if (!has[i]) need[cnt++] = i; n   int idx = 0; n   for (int i = 0; i < n; i++) n     if (pos[i]) a[i] = need[idx++]; n   for (int i = 0; i < n - 1; i++) printf( %d  , a[i]); n   printf( %d n , a[n - 1]); n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 100003; n pair<int, int> t[4 * maxn]; n int psh[maxn]; n void update(int v, int l, int r, int q) { n   if (l == r) return void(t[v] = psh[q] ? make_pair(1, 0) : make_pair(0, 1)); n   int m = (l + r) / 2; n   if (q <= m) update(v * 2, l, m, q); n   if (q > m) update(v * 2 + 1, m + 1, r, q); n   int u = min(t[v * 2].first, t[v * 2 + 1].second); n   t[v] = make_pair(t[v * 2].first + t[v * 2 + 1].first - u, n                    t[v * 2].second + t[v * 2 + 1].second - u); n } n int get_ans(int v, int l, int r, int c) { n   if (!t[v].first) return -1; n   if (l == r) return psh[l]; n   int m = (l + r) / 2; n   if (t[v * 2 + 1].first - c > 0) n     return get_ans(v * 2 + 1, m + 1, r, c); n   else n     return get_ans(v * 2, l, m, c - t[v * 2 + 1].first + t[v * 2 + 1].second); n } n int main() { n   int n; n   scanf( %d , &n); n   for (int i = 0; i < n; ++i) { n     int q, t, num; n     scanf( %d %d , &q, &t); n     if (t == 1) { n       scanf( %d , &num); n       psh[q] = num; n     } n     update(1, 1, n, q); n     printf( %d n , get_ans(1, 1, n, 0)); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n; n vector<int> tree[3005]; n int p[3005][3005]; n long long sz[3005][3005]; n long long dfs(int root, int cur) { n   sz[root][cur] = 1; n   for (int nxt : tree[cur]) { n     if (!sz[root][nxt]) { n       sz[root][cur] += dfs(root, nxt); n       p[root][nxt] = cur; n     } n   } n   return sz[root][cur]; n } n long long dp[3005][3005]; n long long f(int a, int b) { n   long long& ret = dp[a][b]; n   if (~ret) return ret; n   ret = 0; n   if (a == b) return ret; n   return ret = sz[b][a] * sz[a][b] + max(f(a, p[a][b]), f(p[b][a], b)); n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   memset(dp, -1, sizeof(dp)); n   cin >> n; n   for (int i = 1; i < n; i++) { n     int a, b; n     cin >> a >> b; n     tree[a].push_back(b); n     tree[b].push_back(a); n   } n   for (int i = 1; i <= n; i++) dfs(i, i); n   long long ans = 0; n   for (int i = 1; i <= n; i++) { n     for (int j = 1; j <= n; j++) ans = max(ans, f(i, j)); n   } n   cout << ans; n } n


#include <bits/stdc++.h> n using namespace std; n int d1, d2, d3; n int r; n int main() { n   cin >> d1 >> d2 >> d3; n   r = d1 + d2 + d3; n   r = min(r, d1 + d1 + d2 + d2); n   r = min(r, d1 + d3 + d1 + d3); n   r = min(r, d2 + d3 + d2 + d3); n   cout << r <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int t; n   cin >> t; n   while (t--) { n     long long int n, m; n     cin >> n >> m; n     long long int s = 0, x, y, i, j; n     vector<long long int> a, b; n     for (i = 0; i < n; i++) { n       cin >> x; n       if (x < 0) n         a.push_back(x); n       else n         b.push_back(x); n     } n     sort(a.begin(), a.end()); n     sort(b.begin(), b.end()); n     if (a.size() > 0 && b.size() > 0) { n       if (abs(a[0]) > b[b.size() - 1]) { n         s += abs(a[0]); n         for (i = m; i < a.size(); i += m) s += abs(2 * a[i]); n         for (i = b.size() - 1; i >= 0; i -= m) s += (2 * b[i]); n       } else { n         s += b[b.size() - 1]; n         for (i = b.size() - 1 - m; i >= 0; i -= m) s += (2 * b[i]); n         for (i = 0; i < a.size(); i += m) s += abs(2 * a[i]); n       } n     } else if (a.size() > 0) { n       s += abs(a[0]); n       for (i = m; i < a.size(); i += m) s += abs(2 * a[i]); n     } else { n       s += b[b.size() - 1]; n       for (i = b.size() - 1 - m; i >= 0; i -= m) s += (2 * b[i]); n     } n     cout << s << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n int main() { n   int i, j, count, n, a[5001], flag[5001]; n   scanf( %d , &n); n   count = 0; n   memset(flag, 0, sizeof(int) * 5001); n   for (i = 0; i < n; i++) { n     scanf( %d , &a[i]); n     if (a[i] > n) n       count++; n     else if (flag[a[i]] == 1) n       count++; n     else n       flag[a[i]] = 1; n   } n   printf( %d n , count); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   ; n   int n; n   cin >> n; n   vector<int> v(n); n   int mx = 0; n   for (int i = 0; i < n; i++) { n     cin >> v[i]; n     mx = max(mx, v[i]); n   } n   int count = 1, l = 0; n   for (int i = 0; i < n; i++) { n     if (i && v[i] == v[i - 1] && v[i] == mx) n       count++; n     else { n       l = max(count, l); n       count = 1; n     } n     if (i == n - 1) l = max(count, l); n   } n   cout << l << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n multiset<int> a, b; n int c[300010], d[300010]; n int main() { n   int n; n   cin >> n; n   for (int i = 1; i <= n; i++) { n     cin >> c[i] >> d[i]; n     a.insert(c[i]); n     b.insert(d[i]); n   } n   int ans = 0; n   for (int i = 1; i <= n; i++) { n     a.erase(a.find(c[i])); n     b.erase(b.find(d[i])); n     ans = max(ans, *b.begin() - *a.rbegin()); n     a.insert(c[i]); n     b.insert(d[i]); n   } n   cout << ans << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int n, k, sx, ex; n char s[1010]; n int main() { n   scanf( %d%d%s , &n, &k, s); n   for (int i = 0; i < n; i++) { n     if (s[i] ==  G ) sx = i; n     if (s[i] ==  T ) ex = i; n   } n   if (sx > ex) swap(sx, ex); n   if ((ex - sx) % k != 0) n     printf( NO n ); n   else { n     bool f = 1; n     for (int i = sx + k; i < ex; i += k) { n       if (s[i] ==  # ) f = 0; n     } n     if (f) n       printf( YES n ); n     else n       printf( NO n ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n signed main() { n   ios ::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   long long t; n   cin >> t; n   while (t--) { n     long long n, k; n     cin >> n >> k; n     string s; n     cin >> s; n     vector<long long> cnt(26); n     for (auto u : s) ++cnt[u -  a ]; n     long long ans = 0; n     for (long long period = 1; period <= n; ++period) { n       if (k % period) continue; n       for (long long times = 1; times <= n; ++times) { n         long long length = 0; n         for (long long c = 0; c < 26; ++c) length += cnt[c] / times; n         if (length < period) continue; n         ans = max(ans, period * times); n       } n     } n     cout << ans <<   n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n inline long long read() { n   long long s = 0, w = 1; n   char ch = getchar(); n   while (ch <  0  || ch >  9 ) { n     if (ch ==  - ) w = -1; n     ch = getchar(); n   } n   while (ch >=  0  && ch <=  9 ) s = s * 10 + ch -  0 , ch = getchar(); n   return s * w; n } n void put1() { puts( Yes ); } n void put2() { puts( No ); } n void put3() { puts( -1 ); } n long long qp(long long a, long long b, long long p) { n   long long ans = 1; n   while (b) { n     if (b & 1) { n       ans = (ans * a) % p; n       --b; n     } n     a = (a * a) % p; n     b >>= 1; n   } n   return ans % p; n } n const unsigned long long base = 2333; n const unsigned long long pp = 19260811; n const unsigned long long ppp = 999998639; n const int manx = 2e5 + 5; n const int mo = 998244353; n map<long long, long long> dp; n long long a[manx]; n vector<long long> q; n int main() { n   long long n = read(); n   long long ma = 0, ans = 0; n   for (int i = 1; i <= n; i++) { n     a[i] = read(); n     dp[a[i]] = max(dp[a[i] - 1] + 1, dp[a[i]]); n     if (dp[a[i]] > ans) ans = dp[a[i]], ma = a[i]; n   } n   for (int i = n; i >= 1; i--) n     if (a[i] == ma) q.push_back(i), ma--; n   reverse(q.begin(), q.end()); n   cout << q.size() << endl; n   for (auto x : q) cout << x <<    ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void solve() { n   long long a, b, c; n   cin >> a >> b; n   c = a + b; n   string s = to_string(a); n   string x = to_string(b); n   string y = to_string(c); n   string e =   , f =   , g =   ; n   for (int i = 0; i < s.size(); i++) { n     if (s[i] !=  0 ) { n       e += s[i]; n     } n   } n   for (int i = 0; i < x.size(); i++) { n     if (x[i] !=  0 ) { n       f += x[i]; n     } n   } n   for (int i = 0; i < y.size(); i++) { n     if (y[i] !=  0 ) { n       g += y[i]; n     } n   } n   long long m, n, p; n   m = stoi(e); n   n = stoi(f); n   p = stoi(g); n   if (m + n == p) n     cout <<  YES n ; n   else n     cout <<  NO n ; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   int t; n   t = 1; n   while (t--) { n     solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n set<int> Tmp, Trans; n int dp[(500 + 5)][(500 + 5)], n, m, A[(500 + 5)], B[(500 + 5)]; n int from[(500 + 5)][(500 + 5)][2], rkA[(500 + 5)], rkB[(500 + 5)]; n int trans[(500 + 5)][(500 + 5)]; n int getrk(int x) { n   int TP = 0; n   for (set<int>::iterator i = Trans.begin(); i != Trans.end(); ++i) { n     ++TP; n     if (*i == x) return TP; n   } n   return -1; n } n void output(int x, int y) { n   if (y == 0 || x == 0) return; n   output(from[x][y][0], from[x][y][1]); n   cout << B[y] <<    ; n } n int main() { n   cin >> n; n   for (int i = 1; i <= n; ++i) { n     cin >> A[i]; n     Tmp.insert(A[i]); n   } n   cin >> m; n   for (int i = 1; i <= m; ++i) { n     cin >> B[i]; n     if (Tmp.find(B[i]) != Tmp.end()) Trans.insert(B[i]); n   } n   for (int i = 1; i <= n; ++i) { n     if (Trans.find(A[i]) != Trans.end()) n       rkA[i] = getrk(A[i]); n     else n       rkA[i] = 0; n   } n   for (int i = 1; i <= m; ++i) { n     if (Trans.find(B[i]) != Trans.end()) n       rkB[i] = getrk(B[i]); n     else n       rkB[i] = 0; n   } n   for (int i = 1; i <= n; ++i) { n     for (int j = 1; j <= (int)Trans.size(); ++j) { n       trans[i][j] = max(trans[i - 1][j], (rkA[i] == j) * i); n     } n   } n   for (int x = 1; x <= n; ++x) { n     for (int i = 1; i <= m; ++i) { n       if (A[x] != B[i]) continue; n       for (int k = 0; k < i; ++k) { n         if (rkB[k] >= rkB[i]) continue; n         if (dp[trans[x - 1][rkB[k]]][k] + 1 > dp[x][i]) { n           dp[x][i] = dp[trans[x - 1][rkB[k]]][k] + 1; n           from[x][i][0] = trans[x - 1][rkB[k]]; n           from[x][i][1] = k; n         } n       } n     } n   } n   int Ans = 0, x, y, z; n   for (int i = 1; i <= n; ++i) { n     for (int j = 1; j <= m; ++j) { n       if (A[i] == B[j] && dp[i][j] > Ans) { n         Ans = dp[i][j]; n         x = i, y = j; n       } n     } n   } n   cout << Ans << endl; n   if (Ans != 0) output(x, y); n } n


#include <bits/stdc++.h> n using namespace std; n const int mn = 2005; n vector<int> g[mn], G[mn]; n int dfn[mn], low[mn], times; n int stk[mn], top, bel[mn], cnt, siz[mn], fa[mn]; n int rt, maxs; n bool ins[mn], vis[mn][mn], flg[mn], chn[mn], tmp[mn], leav[mn]; n void dfs1(int s, int f) { n   dfn[s] = low[s] = ++times, stk[++top] = s, ins[s] = 1; n   int m = g[s].size(); n   for (int i = 0; i < m; i++) { n     int t = g[s][i]; n     if (t == f) continue; n     if (!dfn[t]) n       dfs1(t, s), low[s] = min(low[s], low[t]); n     else if (ins[s]) n       low[s] = min(low[s], dfn[t]); n     if (low[t] > dfn[s]) { n       ++cnt; n       while (stk[top] != t) n         ins[stk[top]] = 0, bel[stk[top--]] = cnt, ++siz[cnt]; n       bel[t] = cnt, ins[t] = 0, --top, ++siz[cnt]; n     } n   } n } n void dfs2(int s, int d) { n   if (d > maxs) rt = s, maxs = d; n   int m = G[s].size(); n   flg[s] = 1; n   for (int i = 0; i < m; i++) { n     int t = G[s][i]; n     if (!flg[t]) dfs2(t, d + 1); n   } n } n void dfs3(int s, int f, int d) { n   int m = G[s].size(); n   fa[s] = f; n   bool flg = 0; n   for (int i = 0; i < m; i++) { n     int t = G[s][i]; n     if (t != f) dfs3(t, s, d + 1), flg = 1; n   } n   leav[s] = flg ^ 1; n   if (!flg && d > maxs) { n     memset(tmp, 0, sizeof tmp), maxs = d; n     while (s) tmp[s] = 1, s = fa[s]; n   } n } n int main() { n   int n, m, a, b, blk = 0, ans = 0; n   scanf( %d%d , &n, &m); n   for (int i = 1; i <= m; i++) n     scanf( %d%d , &a, &b), g[a].push_back(b), g[b].push_back(a); n   for (int i = 1; i <= n; i++) n     if (!dfn[i]) { n       dfs1(i, 0), ++blk, ++cnt; n       while (top) bel[stk[top]] = cnt, ins[stk[top]] = 0, --top, ++siz[cnt]; n     } n   for (int i = 1; i <= n; i++) { n     int siz = g[i].size(); n     for (int j = 0; j < siz; j++) { n       int t = g[i][j]; n       if (bel[i] != bel[t] && !vis[bel[i]][bel[t]]) n         vis[bel[i]][bel[t]] = vis[bel[t]][bel[i]] = 1, n         G[bel[i]].push_back(bel[t]), G[bel[t]].push_back(bel[i]); n     } n   } n   for (int i = 1; i <= cnt; i++) { n     ans += siz[i] - 1; n     if (!flg[i]) { n       maxs = 0, dfs2(i, 1), maxs = 0, dfs3(rt, 0, 1); n       for (int i = 1; i <= cnt; i++) chn[i] |= tmp[i]; n     } n   } n   for (int i = 1; i <= cnt; i++) { n     int s = i, d = 0; n     if (leav[s]) continue; n     while (!chn[s]) chn[s] = 1, s = fa[s], ++d; n     ans += d; n   } n   printf( %d n , ans + blk - 1); n } n


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


#include <bits/stdc++.h> n #pragma GCC_OPTIMAZE( ofast ) n using namespace std; n signed main() { n   long long n; n   cin >> n; n   vector<long long> arr(n); n   for (long long i = 0; i < n; i++) { n     cin >> arr[i]; n     arr[i]--; n   } n   long long cnt = 0, pos = 0; n   while (pos < n) { n     cnt++; n     long long mx = pos; n     while (pos < n && pos <= mx) { n       mx = max(mx, arr[pos]); n       pos++; n     } n   } n   cout << cnt << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(0); n   int n; n   cin >> n; n   vector<vector<int> > T(n); n   for (int i = 0; i < n - 1; ++i) { n     int a, b; n     cin >> a >> b; n     --a; n     --b; n     T[a].push_back(b); n     T[b].push_back(a); n   } n   vector<bool> odw(n, false); n   stack<int> S; n   vector<double> odl(n); n   odw[0] = true; n   S.push(0); n   odl[0] = 1; n   double odp = 0.0; n   while (!S.empty()) { n     int akt = S.top(); n     S.pop(); n     odp += 1.0 / odl[akt]; n     for (int i = 0; i < T[akt].size(); ++i) { n       if (!odw[T[akt][i]]) { n         odw[T[akt][i]] = true; n         S.push(T[akt][i]); n         odl[T[akt][i]] = odl[akt] + 1; n       } n     } n   } n   cout.precision(10); n   cout << odp; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long gcd(long long a, long long b) { n   a = abs(a), b = abs(b); n   if (a < b) swap(a, b); n   return b ? gcd(b, a % b) : a; n } n long long lcm(long long a, long long b) { return a * b / gcd(a, b); } n long long fast_pow(long long x, long long exp, long long mod) { n   long long ret = 1; n   while (exp) { n     if (exp & 1) ret = (ret * x) % mod; n     x = (x * x) % mod; n     exp >>= 1; n   } n   return ret; n } n const int dx[] = {0, 0, -1, 1}; n const int dy[] = {-1, 1, 0, 0}; n const int inf = (int)1e9 + 5; n vector<vector<int> > adj; n vector<int> v; n priority_queue<int> q; n map<int, int> mp; n set<int> st; n long long x, y, z; n int32_t main(void) { n   ios_base::sync_with_stdio(false), cin.tie(nullptr); n   cin >> x >> y >> z; n   if (x > y) swap(x, y); n   cout << (x + y) / z <<    ; n   x %= z; n   y %= z; n   if (x + y >= z) n     cout << min(x - ((x + y) - z), y - ((x + y) - z)); n   else n     cout << 0; n   return 0; n } n


#include <bits/stdc++.h> n const int MAX = 20; n const double PI = std::acos(-1.0); n struct comp { n   double real, imag; n   explicit comp(double real = 0, double imag = 0) : real(real), imag(imag) {} n   comp operator+(const comp &c) const { n     return comp(real + c.real, imag + c.imag); n   } n   comp operator-(const comp &c) const { n     return comp(real - c.real, imag - c.imag); n   } n   comp operator*(const comp &c) const { n     return comp(real * c.real - imag * c.imag, real * c.imag + imag * c.real); n   } n   comp operator*(const double &c) const { return comp(real * c, imag * c); } n }; n comp w[2][1 << MAX]; n void init() { n   for (int i = 0; i < 1 << MAX; ++i) { n     double angle = 1.0 * i / (1 << (MAX - 1)) * PI; n     for (int type = -1; type <= 1; type += 2) n       w[!~type][i] = comp(cos(angle), type * sin(angle)); n   } n } n void FFT(std::vector<comp> &a, int len, int type) { n   for (int i = 1, j = 0; i < len - 1; ++i) { n     for (int s = len; j ^= s >>= 1, ~j & s;) n       ; n     if (i < j) std::swap(a[i], a[j]); n   } n   for (int i = 2, d = 1; i <= len; i <<= 1, ++d) n     for (int j = 0; j < len; j += i) { n       auto u = a.begin() + j; n       for (int k = 0; k < i / 2; ++k) { n         comp s = u[k]; n         comp t = w[type][k << (MAX - d)] * u[k + i / 2]; n         u[k] = s + t, u[k + i / 2] = s - t; n       } n     } n   if (type == 0) return; n   double inv = 1.0 / len; n   for (int i = 0; i < len; ++i) a[i] = a[i] * inv; n } n const int LEN = 1 << 18; n using ll = long long; n ll ans[LEN], tmp[LEN]; n void solve(const std::vector<int> &a, const std::vector<int> &b, int coe, n            int dir) { n   std::vector<comp> a1(LEN), a2(LEN); n   for (auto u : a) { n     a1[u].real += 1; n   } n   for (auto u : b) { n     a2[u].real += 1; n   } n   FFT(a1, LEN, 0); n   FFT(a2, LEN, 0); n   for (int i = 0; i < LEN; ++i) { n     a1[i] = a1[i] * a2[i]; n   } n   FFT(a1, LEN, 1); n   memset(tmp, 0, sizeof(tmp)); n   if (dir == 0) { n     for (int i = 1; i < LEN; ++i) { n       tmp[i] = tmp[i - 1] + ll(std::round(a1[i - 1].real)); n     } n   } else { n     for (int i = LEN - 2; i >= 0; --i) { n       tmp[i] = tmp[i + 1] + ll(std::round(a1[i].real)); n     } n   } n   for (int i = 0; i < LEN; ++i) { n     ans[i] += coe * tmp[i]; n   } n } n int main() { n   init(); n   int n, m; n   int test; n   scanf( %d%d%d , &n, &m, &test); n   std::vector<int> a(n), b(m); n   for (int i = 0; i < n; ++i) { n     scanf( %d , &a[i]); n   } n   for (int i = 0; i < m; ++i) { n     scanf( %d , &b[i]); n   } n   solve(a, b, 1, 1); n   solve(a, b, -1, 0); n   std::vector<int> mina(n - 1), minb(m - 1); n   for (int i = 0; i < n - 1; ++i) { n     mina[i] = std::min(a[i], a[i + 1]); n   } n   for (int i = 0; i < m - 1; ++i) { n     minb[i] = std::min(b[i], b[i + 1]); n   } n   solve(mina, b, -1, 1); n   solve(a, minb, -1, 1); n   std::vector<int> maxa(n - 1), maxb(m - 1); n   for (int i = 0; i < n - 1; ++i) { n     maxa[i] = std::max(a[i], a[i + 1]); n   } n   for (int i = 0; i < m - 1; ++i) { n     maxb[i] = std::max(b[i], b[i + 1]); n   } n   solve(maxa, b, 1, 0); n   solve(a, maxb, 1, 0); n   solve(mina, minb, 1, 1); n   solve(maxa, maxb, -1, 0); n   while (test--) { n     int x; n     scanf( %d , &x); n     printf( %lld n , ans[x]); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long n; n   cin >> n; n   long long a[n], b[n]; n   long long i; n   for (i = 0; i < n; i++) { n     cin >> a[i]; n     b[i] = a[i]; n   } n   sort(a, a + n); n   long long count = 0; n   for (i = 0; i < n; i++) { n     if (a[i] != b[i]) { n       count++; n     } n   } n   if (count == 2 || count == 0) { n     cout <<  YES  << endl; n   } else { n     cout <<  NO  << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n struct Arc { n   int f, s, prev; n } arr[400005]; n Arc nbr[400005]; n Arc br[400005]; n int beg[400005]; n int kol = 0; n void add_arc(int f, int s) { n   arr[kol].f = f; n   arr[kol].s = s; n   arr[kol].prev = beg[f]; n   beg[f] = kol++; n   arr[kol].f = s; n   arr[kol].s = f; n   arr[kol].prev = beg[s]; n   beg[s] = kol++; n } n int pos[1 << 19]; n int lev[1 << 19]; n int llv[1 << 19]; n int tin[1 << 19]; n int tout[1 << 19]; n int base[1 << 19]; n int t = 0, ff = 0, q = 0; n void DFS(int v, int p = -1) { n   tout[v] = tin[v] = ++t; n   for (int i = beg[v]; i != -1; i = arr[i].prev) { n     if (arr[i].s != p) { n       if (!tin[arr[i].s]) { n         DFS(arr[i].s, v); n         if (tout[arr[i].s] <= tin[v]) { n           tout[v] = min(tout[v], tout[arr[i].s]); n           nbr[ff++] = arr[i]; n         } else { n           br[q++] = arr[i]; n         } n       } else { n         tout[v] = min(tout[v], tout[arr[i].s]); n       } n     } n   } n } n void DFS2(int v, int lv = 0, int p = -1) { n   tin[v] = t; n   llv[v] = lv; n   lev[t] = lv; n   pos[t++] = v; n   for (int i = beg[v]; i != -1; i = arr[i].prev) { n     if (arr[i].s != p) { n       DFS2(arr[i].s, lv + 1, v); n       lev[t] = lv; n       pos[t++] = v; n     } n   } n } n int get_base(int v) { n   if (base[v] == v) { n     return v; n   } else { n     return base[v] = get_base(base[v]); n   } n } n void merge_base(int a, int b) { n   if (rand() & 1) { n     base[get_base(a)] = get_base(b); n   } else { n     base[get_base(b)] = get_base(a); n   } n } n void build_tree() { n   for (int i = (1 << 18) - 1; i; --i) { n     if (lev[i + i] < lev[i + i + 1]) { n       lev[i] = lev[i + i]; n       pos[i] = pos[i + i]; n     } else { n       lev[i] = lev[i + i + 1]; n       pos[i] = pos[i + i + 1]; n     } n   } n } n int get_min(int l, int r) { n   l = tin[l]; n   r = tin[r]; n   if (l > r) { n     swap(l, r); n   } n   ++r; n   int best = lev[l], bv = pos[l]; n   while (r - l > 1) { n     if (!(l & 1)) { n       if (lev[l + 1] < best) { n         best = lev[l + 1]; n         bv = pos[l + 1]; n       } n     } n     if (r & 1) { n       if (lev[r - 1] < best) { n         best = lev[r - 1]; n         bv = pos[r - 1]; n       } n     } n     l >>= 1; n     r >>= 1; n   } n   return bv; n } n int main() { n   memset(beg, -1, sizeof beg); n   int n, m, k; n   scanf( %d%d , &n, &m); n   for (int i = 0; i < m; ++i) { n     int f, s; n     scanf( %d%d , &f, &s); n     --f; n     --s; n     add_arc(f, s); n   } n   DFS(0); n   for (int i = 0; i < n; ++i) { n     base[i] = i; n   } n   for (int i = 0; i < ff; ++i) { n     merge_base(nbr[i].s, nbr[i].f); n   } n   memset(beg, -1, sizeof beg); n   memset(tin, -1, sizeof tin); n   for (int i = 0; i < q; ++i) { n     add_arc(get_base(br[i].f), get_base(br[i].s)); n   } n   t = 1 << 18; n   DFS2(get_base(0)); n   build_tree(); n   scanf( %d , &k); n   for (int i = 0; i < k; ++i) { n     int f, s; n     scanf( %d%d , &f, &s); n     --f; n     --s; n     f = get_base(f); n     s = get_base(s); n     int ret = llv[f] + llv[s] - 2 * llv[get_min(f, s)]; n     printf( %d n , ret); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n using namespace std; n map<long long, long long> f; n int main() { n   int q; n   scanf( %d , &q); n   while (q--) { n     long long u, v; n     int t, w; n     scanf( %d %lld %lld , &t, &u, &v); n     if (t == 1) { n       scanf( %d , &w); n       while (u != v) { n         if (u < v) swap(u, v); n         f[u] += w; n         u >>= 1; n       }; n     } else { n       long long ans = 0; n       while (u != v) { n         if (u < v) swap(u, v); n         ans += f[u]; n         u >>= 1; n       }; n       cout << ans << endl; n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 4e5 + 7; n int n, a[N], cnt[N], lim = 0, mx = 0, fa[N]; n long long ans = 0; n inline int find(int x) { return x == fa[x] ? x : fa[x] = find(fa[x]); } n inline void merge(int u, int v, long long w) { n   if (cnt[u] && cnt[v]) { n     u = find(u), v = find(v); n     if (u != v) { n       ans += 1ll * w * (cnt[v] + cnt[u] - 1); n       fa[u] = v; n       cnt[v] = 1; n     } n   } n } n int main() { n   scanf( %d , &n); n   cnt[0] = 1; n   for (int i = 1; i <= n; i++) { n     scanf( %d , &a[i]); n     cnt[a[i]]++; n     mx = max(mx, a[i]); n     ans -= a[i]; n   } n   lim = 1 << 32 - __builtin_clz(mx); n   for (int i = 0; i <= lim - 1; i++) fa[i] = i; n   for (int i = lim - 1; ~i; i--) { n     for (int j = i; j; j = (j - 1) & i) { n       if (cnt[j] && cnt[i ^ j]) merge(j, i ^ j, i); n     } n     merge(i, 0, i); n   } n   printf( %lld n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct vac { n   int l, r, cost, duration; n   vac() : cost(-1) {} n   vac(int l, int r, int cost) : l(l), r(r), cost(cost), duration(r - l + 1) {} n }; n vector<vector<vac> > lifes(200001, vector<vac>(0)), n     deaths(200001, vector<vac>(0)); n vector<vac> cheapest(200001); n int min1(int x, int y) { n   if (x == -1) return y; n   if (y == -1) return x; n   return min(x, y); n } n int main() { n   int n, x; n   cin >> n >> x; n   int minAns = -1; n   for (int i = 0; i < n; i++) { n     int l, r, cost; n     cin >> l >> r >> cost; n     lifes[l].push_back(vac(l, r, cost)); n     deaths[r].push_back(vac(l, r, cost)); n   } n   for (int i = 0; i < lifes.size(); i++) { n     for (int j = 0; j < lifes[i].size(); j++) { n       int d = lifes[i][j].duration; n       if (d > x) continue; n       if (cheapest[x - d].cost != -1) n         minAns = min1(minAns, cheapest[x - d].cost + lifes[i][j].cost); n     } n     for (int j = 0; j < deaths[i].size(); j++) { n       int d = deaths[i][j].duration; n       cheapest[d].cost = min1(cheapest[d].cost, deaths[i][j].cost); n     } n   } n   cout << minAns << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   cin >> n; n   vector<int> arr(n); n   for (auto &i : arr) cin >> i; n   int maxx = 0; n   for (auto i : arr) maxx = max(maxx, i); n   cout << (maxx xor arr.back()); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int N, i, j, p = 0, q = 0, ans = 0; n   cin >> N; n   for (i = 0; i < N; i++) { n     cin >> j; n     if (q < j) n       p++; n     else n       p = 1; n     q = j; n     ans = max(ans, p); n   } n   cout << ans <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e5 + 10, InfI = INT_MAX, Mod = 998244353; n const long long InfL = LONG_LONG_MAX; n int n, m, sqn; n vector<int> adj[N]; n vector<int> ind; n bool vis[N] = {}, indpen[N] = {}; n int ent[N], par[N], back_edge[N], back_min[N]; n int dfstimer = 0, idma = 1; n void dfs(int x) { n   vis[x] = 1; n   ent[x] = dfstimer++; n   int back_edg = 0, back_mi = x; n   bool pen = 1; n   for (int v : adj[x]) { n     if (!vis[v]) { n       par[v] = x; n       dfs(v); n     } else if (ent[v] < ent[x]) { n       back_edg++; n       back_mi = (ent[v] < ent[back_mi]) ? v : back_mi; n     } n     if (indpen[v]) pen = 0; n   } n   back_edge[x] = back_edg; n   back_min[x] = back_mi; n   idma = (back_edg > back_edge[idma]) ? x : idma; n   if (back_edg < sqn - 1 && pen) { n     ind.push_back(x); n     indpen[x] = 1; n   } n } n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   cin >> n >> m; n   sqn = ceil(sqrt(1.0 * n)); n   for (int i = 1; i <= m; i++) { n     int a, b; n     cin >> a >> b; n     adj[a].push_back(b); n     adj[b].push_back(a); n   } n   back_edge[1] = 0; n   dfs(1); n   if (back_edge[idma] >= sqn - 1) { n     cout << 2 << endl; n     vector<int> path; n     int j = idma; n     path.push_back(j); n     while (j != back_min[idma]) { n       j = par[j]; n       path.push_back(j); n     } n     cout << path.size() << endl; n     for (int i : path) cout << i <<    ; n     return 0; n   } n   cout << 1 << endl; n   for (int i = 0; i < sqn; i++) cout << ind[i] <<    ; n } n


#include <bits/stdc++.h> n const double eps = 1e-9; n const int int_inf = 2000000000; n const long long i64_inf = 1000000000000000000LL; n const double pi = acos(-1.0); n using namespace std; n int wa[1000010]; n int wb[1000010]; n int a[1000100]; n int b[1000100]; n int la, lb; n int res = 0; n int main() { n   memset(wa, -1, sizeof(wa)); n   memset(wb, -1, sizeof(wb)); n   cin >> la >> lb; n   for (int i = 0; i < (int)la; i++) scanf( %d , &a[i]), wa[a[i]] = i; n   for (int i = 0; i < (int)lb; i++) scanf( %d , &b[i]), wb[b[i]] = i; n   int first = wb[a[0]], last = first, len = 1; n   if (first != -1) { n     for (int i = 1; i < (int)la; i++) { n       if (wb[a[i]] == -1) break; n       if (first <= last) n         if (wb[a[i]] > last || wb[a[i]] < first) { n           last = wb[a[i]]; n           len++; n           continue; n         } else n           break; n       if (wb[a[i]] > last && wb[a[i]] < first) { n         last = wb[a[i]]; n         len++; n         continue; n       } else n         break; n     } n   } else n     len = 0; n   res = max(res, len); n   for (int i = 1; i < (int)la; i++) { n     if (len == 0) { n       if (wb[a[i]] == -1) continue; n       first = last = wb[a[i]]; n       len = 1; n     } else { n       len--; n       first = wb[a[i]]; n       if (first == -1) { n         len = 0; n         continue; n       } n     } n     int j = (i + len) % la; n     while (j != i) { n       if (wb[a[j]] == -1) break; n       if (first <= last) n         if (wb[a[j]] > last || wb[a[j]] < first) { n           last = wb[a[j]]; n           len++; n           j = (j + 1) % la; n           continue; n         } else n           break; n       if (wb[a[j]] > last && wb[a[j]] < first) { n         last = wb[a[j]]; n         len++; n         j = (j + 1) % la; n         continue; n       } else n         break; n     } n     res = max(res, len); n   } n   cout << res << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   int n, m; n   string s; n   cin >> n >> m; n   int a[n][m]; n   int r[m]; n   memset(r, 0, sizeof r); n   for (int i = 0; i < n; i++) { n     cin >> s; n     for (int j = 0; j < m; j++) { n       a[i][j] = s[j] -  0 ; n       if (a[i][j]) { n         r[j]++; n       } n     } n   } n   for (int i = 0; i < n; i++) { n     bool u = false; n     for (int j = 0; j < m && !u; j++) { n       if (a[i][j] && r[j] == 1) u = true; n     } n     if (!u) { n       cout <<  YES n ; n       return 0; n     } n   } n   cout <<  NO n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 1e5 + 100; n int arr[MAXN]; n int n, x, t; n long long ans; n int main() { n   cin >> n; n   for (int i = 0; i < n; i++) { n     cin >> x; n     if (t <= 1) n       arr[++t] = x; n     else { n       while (t > 1 && arr[t] <= arr[t - 1] && arr[t] <= x) { n         ans += min(arr[t - 1], x); n         t--; n       } n       arr[++t] = x; n     } n   } n   sort(arr + 1, arr + t + 1, greater<int>()); n   for (int i = 3; i <= t; i++) ans += arr[i]; n   return cout << ans << endl, 0; n } n


#include <bits/stdc++.h> n using namespace std; n inline int gi() { n   char c = getchar(); n   while (c <  0  || c >  9 ) c = getchar(); n   int sum = 0; n   while ( 0  <= c && c <=  9 ) sum = sum * 10 + c - 48, c = getchar(); n   return sum; n } n const int maxn = 100005; n int n, a[maxn], cnt[maxn], p[maxn][3], val[maxn]; n priority_queue<pair<int, int>> que; n int main() { n   n = gi(); n   for (int i = 1; i <= n; ++i) a[i] = gi(); n   sort(a + 1, a + n + 1); n   for (int i = 1, m = 0, lst = -1; i <= n; ++i) n     if (a[i] == lst) n       ++cnt[m]; n     else n       lst = a[i], ++m, val[m] = a[i], ++cnt[m]; n   for (int i = 1; i <= n; ++i) n     if (cnt[i]) que.push(make_pair(cnt[i], val[i])); n   pair<int, int> x, y, z; n   int ans = 0; n   while (que.size() >= 3) { n     x = que.top(); n     que.pop(); n     y = que.top(); n     que.pop(); n     z = que.top(); n     que.pop(); n     ++ans; n     p[ans][0] = x.second; n     p[ans][1] = y.second; n     p[ans][2] = z.second; n     sort(p[ans], p[ans] + 3, greater<int>()); n     if (x.first > 1) --x.first, que.push(x); n     if (y.first > 1) --y.first, que.push(y); n     if (z.first > 1) --z.first, que.push(z); n   } n   printf( %d n , ans); n   for (int i = 1; i <= ans; ++i) n     printf( %d %d %d n , p[i][0], p[i][1], p[i][2]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<vector<int> > g; n vector<int> a, q; n vector<char> cl; n vector<int> p; n map<int, pair<int, int> > y; n map<int, pair<int, int> >::iterator it; n int cycle_st, cycle_end; n long long i, j, n, m, e, k, ot, x, i1, i2, i3, i4, l, r; n bool dfs(int v) { n   cl[v] = 1; n   for (size_t i = 0; i < g[v].size(); ++i) { n     int to = g[v][i]; n     if (cl[to] == 0 && p[v] != to) { n       p[to] = v; n       y[v].first = to; n       y[to].second = y[v].second + 1; n       if (dfs(to)) return true; n     } else if (cl[to] == 1 && p[v] != to && n                y[v].second - y[to].second + 1 >= k + 1) { n       cycle_end = v; n       cycle_st = to; n       cout << y[v].second - y[to].second + 1 << endl; n       while (y[to].first != v) { n         cout << to <<    ; n         to = y[to].first; n       } n       cout << to <<     << v <<    ; n       exit(0); n       return true; n     } n   } n   cl[v] = 2; n   return false; n } n int main() { n   vector<vector<int> > w; n   vector<int> d; n   cin >> n >> m >> k; n   for (i = 0; i < n + 2; i++) { n     g.push_back(a); n     p.push_back(-1); n     cl.push_back(0); n   } n   for (i = 0; i < m; i++) { n     cin >> l >> r; n     g[l].push_back(r); n     g[r].push_back(l); n   } n   for (int i = 0; i < n; ++i) { n     y[i].second = 1; n     if (dfs(i)) break; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int t, i, j, count = 0; n   cin >> t; n   int a[t], b[t]; n   for (i = 0; i < t; i++) cin >> a[i] >> b[i]; n   for (int i = 0; i < t; i++) { n     int e = 0, f = 0, c = 0, d = 0; n     for (j = 0; j < t; j++) { n       if (a[i] < a[j] && b[i] == b[j]) e = 1; n       if (a[i] > a[j] && b[i] == b[j]) f = 1; n       if (a[i] == a[j] && b[i] > b[j]) c = 1; n       if (a[i] == a[j] && b[i] < b[j]) d = 1; n     } n     if (e + f + c + d == 4) count++; n   } n   cout << count <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T> n T gcd(T a, T b) { n   if (a == 0) return b; n   return gcd(b % a, a); n } n template <typename T> n T pow(T a, T b, long long m) { n   T ans = 1; n   while (b > 0) { n     if (b % 2 == 1) ans = ((ans % m) * (a % m)) % m; n     b /= 2; n     a = ((a % m) * (a % m)) % m; n   } n   return ans % m; n } n double fac[55]; n double c[55][55]; n void factorial() { n   fac[0] = 1.00; n   for (int i = 1; i <= 50; i++) { n     fac[i] = fac[i - 1] * (double)i; n   } n } n int n, m; n int a[55]; n double pw(double base, int exp) { n   double ans = 1.00; n   for (int i = 0; i < exp; i++) ans *= base; n   return ans; n } n double dp[55][55][55]; n bool vis[55][55][55]; n double solve(int id, int cnt, int maxSoFar) { n   if (id == (m)) { n     if (cnt == 0) n       return maxSoFar; n     else n       return 0.00; n   } n   if (vis[id][cnt][maxSoFar]) return dp[id][cnt][maxSoFar]; n   vis[id][cnt][maxSoFar] = 1; n   double ans = 0.00; n   for (int i = 0; i <= cnt; i++) { n     ans += c[cnt][i] * n            solve(id + 1, cnt - i, max((a[id] + i - 1) / a[id], maxSoFar)); n   } n   return dp[id][cnt][maxSoFar] = ans; n } n int main() { n   cin >> n >> m; n   factorial(); n   for (int i = 0; i < m; i++) cin >> a[i]; n   for (int i = 0; i <= 50; i++) { n     c[i][0] = 1.00; n     c[i][i] = 1.00; n   } n   for (int i = 2; i <= 50; i++) { n     for (int j = 1; j < i; j++) { n       c[i][j] = c[i - 1][j] + c[i - 1][j - 1]; n     } n   } n   double ans = solve(0, n, 0); n   ans /= pw(m, n); n   cout << fixed << setprecision(12) << ans; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int W = 0, B = 0, i, h = 64; n   char z; n   for (i = 0; i < h; i++) { n     cin >> z; n     if (z ==  Q ) W += 9; n     if (z ==  q ) B += 9; n     if (z ==  R ) W += 5; n     if (z ==  r ) B += 5; n     if (z ==  B ) W += 3; n     if (z ==  b ) B += 3; n     if (z ==  N ) W += 3; n     if (z ==  n ) B += 3; n     if (z ==  P ) W += 1; n     if (z ==  p ) B += 1; n   } n   if (W > B) cout <<  White ; n   if (B > W) cout <<  Black ; n   if (B == W) cout <<  Draw ; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 301; n const double eps = 1e-8; n struct Segment { n   double a, b, c; n } seg[maxn], sta; n struct Point { n   double x, y; n } you, uni; n bool calcross(Segment l, Segment sa, Point &ans) { n   double delta = l.a * sa.b - l.b * sa.a; n   if (abs(delta) < eps) return false; n   ans.x = (-l.c * sa.b) - (-sa.c * l.b); n   ans.y = l.a * (-sa.c) - sa.a * (-l.c); n   ans.x /= delta; n   ans.y /= delta; n   return true; n } n int main() { n   int i, n, con = 0; n   double x, y; n   Point ans; n   scanf( %lf %lf , &you.x, &you.y); n   scanf( %lf %lf , &uni.x, &uni.y); n   scanf( %d , &n); n   for (i = 0; i < n; i++) { n     scanf( %lf %lf %lf , &seg[i].a, &seg[i].b, &seg[i].c); n   } n   if (you.x > uni.x) swap(you, uni); n   x = uni.x - you.x; n   y = uni.y - you.y; n   sta.a = -y; n   sta.b = x; n   sta.c = you.x * y - you.y * x; n   for (i = 0; i < n; i++) n     if (calcross(sta, seg[i], ans)) { n       if ((ans.x - you.x) * (ans.x - uni.x) < 0 || n           (ans.y - you.y) * (ans.y - uni.y) < 0) n         con++; n     } n   printf( %d n , con); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void solve() { n   long long x, y, a, b; n   cin >> x >> y >> a >> b; n   if (2 * a < b) { n     cout << a * (x + y) <<   n ; n     ; n     return; n   } else { n     cout << b * min(x, y) + a * abs(x - y) <<   n ; n     ; n   } n } n int main() { n   int t; n   cin >> t; n   for (int i = 0; i < t; i++) { n     solve(); n   } n } n


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


#include <bits/stdc++.h> n using namespace std; n int main() { n   int tux; n   cin >> tux; n   int foo = 0, bar = 0, baz = 0, quz = 1; n   for (int i = 1; i <= tux; i++) { n     int pur; n     cin >> pur; n     foo += pur; n     bar = bar + 1; n     if (foo * quz > baz * bar) { n       baz = foo; n       quz = bar; n     } n   } n   double a = baz, b = quz; n   cout << fixed << setprecision(6) << a / b << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   cin >> n; n   string s; n   cin >> s; n   int h1 = (int)(s[0] -  0 ); n   int h2 = (int)(s[1] -  0 ); n   int m1 = (int)(s[3] -  0 ); n   int m2 = (int)(s[4] -  0 ); n   if (n == 12) { n     if (m1 > 5) m1 = 1; n     if (h1 >= 2) { n       if (h2 == 0) n         h1 = 1; n       else n         h1 = 0; n     } else { n       if (h1 == 0) { n         if (h2 == 0) h2 = 1; n       } else { n         if (h2 > 2) { n           h2 = 1; n         } n       } n     } n   } else { n     if (m1 > 5) m1 = 1; n     if (h1 > 2) n       h1 = 1; n     else { n       if (h1 == 2 && h2 >= 4) h2 = 1; n     } n   } n   cout << h1 << h2 <<  :  << m1 << m2 << endl; n   return 0; n } n


#include <bits/stdc++.h> n template <typename T, typename U> n inline void swap(T &a, U &b) { n   a = a ^ b; n   b = a ^ b; n   a = a ^ b; n } n inline void swap(int &a, int &b) { n   a = a ^ b; n   b = a ^ b; n   a = a ^ b; n } n inline void swap(long long &a, long long &b) { n   a = a ^ b; n   b = a ^ b; n   a = a ^ b; n } n template <typename T, typename U> n inline void smax(T &a, U b) { n   if (b > a) a = b; n } n template <typename T, typename U> n inline void smin(T &a, U b) { n   if (b < a) a = b; n } n inline int gcd(int a, int b) { n   if (a < b) swap(a, b); n   while (b > 0) { n     a %= b; n     swap(a, b); n   } n   return a; n } n long long nchose(int a, int b) { n   long long ans = 1; n   smax(a, b - a); n   int p = 2; n   for (int i = (b - a + 1); i <= (b); i++) { n     ans *= (long long)i; n     while (p <= a && ans % p == 0) ans /= p++; n   } n   return ans; n } n using namespace std; n static long long MOD = 1000000007; n unsigned char rev(unsigned char c) { n   unsigned char res = 0, p1 = 1, p2 = 128; n   while (p2 != 0) { n     res += (p1 & c) > 0 ? p2 : 0; n     p2 /= 2; n     p1 *= 2; n   } n   return res; n } n int main() { n   unsigned char c, last = 0; n   scanf( %c , &c); n   while (c != 0 && c !=   n ) { n     last = rev(last) - rev(c); n     cout << (int)last << endl; n     last = c; n     scanf( %c , &c); n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 1e6 + 7; n const int MOD = 1e9 + 7; n int n; n int h[MAXN]; n int main() { n   int m, k, ta, tb; n   scanf( %d%d%d , &n, &m, &k); n   for (int i = 1; i <= m; i++) { n     scanf( %d , &ta); n     h[ta] = 1; n   } n   int cur = 1; n   if (!h[cur]) n     for (int i = 0; i < k; i++) { n       scanf( %d%d , &ta, &tb); n       if (ta != cur && tb != cur) continue; n       if (ta == cur && tb == cur) continue; n       if (ta != cur) swap(ta, tb); n       if (h[tb]) { n         printf( %d n , tb); n         return 0; n       } else { n         cur = tb; n       } n     } n   printf( %d n , cur); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n pair<pair<long long, long long>, long long> merge_p( n     pair<long long, long long> a, pair<long long, long long> b) { n   if (a.first > b.first) swap(a, b); n   if (b.first <= a.second) { n     if (b.second <= a.second) n       return make_pair(b, 0); n     else n       return make_pair(make_pair(b.first, a.second), 0); n   } else { n     return make_pair(make_pair(a.second, b.first), b.first - a.second); n   } n } n void solve() { n   int n, x; n   cin >> n >> x; n   pair<long long, long long> opt = make_pair(x, x); n   long long ans = 0; n   pair<long long, long long> tmp; n   for (int i = 0; i < n; ++i) { n     cin >> tmp.first >> tmp.second; n     pair<pair<long long, long long>, long long> f = merge_p(opt, tmp); n     opt = f.first; n     ans += f.second; n   } n   cout << ans << endl; n } n int main() { n   time_t t1 = clock(); n   solve(); n   time_t t2 = clock(); n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 3e5 + 10; n int n; n long long a[N]; n long long b[N]; n int main() { n   int t; n   scanf( %d , &t); n   while (t--) { n     vector<pair<long long, long long> > v; n     scanf( %d , &n); n     v.clear(); n     for (int i = 0; i < n; i++) { n       long long x, y; n       scanf( %I64d%I64d , &x, &y); n       v.push_back(make_pair(x, y)); n     } n     long long ans = 0; n     long long maxx = 0, minn = 1e18 + 10; n     long long t; n     for (int i = 0; i < n; i++) { n       maxx = v[(i + 1) % n].first - v[i].second; n       if (maxx < 0) { n         maxx = 0; n       } n       ans += maxx; n     } n     for (int i = 0; i < n; i++) { n       t = v[(i + 1) % n].first - v[i].second; n       if (t < 0) { n         t = 0; n       } n       if (minn > ans + v[(i + 1) % n].first - t) { n         minn = ans + v[(i + 1) % n].first - t; n       } n     } n     cout << minn << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int inf = 1e9; n const int N = 500010; n int n, m, a[N], b[N], cnt[N], c[N]; n long long ans; n bool f; n void update_ans(long long val) { n   if (ans == -1) n     ans = val; n   else if (ans != val) n     f = false; n } n void check(int s, int d) { n   for (int i = 1; i <= n; i++) cnt[i] = 0; n   for (int i = 1; i <= m; i++) cnt[b[i]]++; n   int st = s, dt = d; n   if (!cnt[s]) return; n   cnt[s]--; n   long long res = 0; n   for (int i = 1; i < m; i++) { n     if ((s == n && d == 1) || (s == 1 && d == -1)) d *= -1; n     long long s1 = s + d; n     if (!cnt[s1]) return; n     cnt[s1]--; n     if (s1 > s) n       res += a[s1] - a[s]; n     else n       res += a[s] - a[s1]; n     s = s1; n   } n   update_ans(res); n } n int main() { n   scanf( %d , &n); n   for (int i = 1; i <= n; i++) scanf( %d , &a[i]); n   scanf( %d , &m); n   for (int i = 1; i <= m; i++) scanf( %d , &b[i]); n   ans = -1; n   f = true; n   check(1, 1); n   check(n, -1); n   check(2, -1); n   check(2, 1); n   check(n - 1, -1); n   check(n - 1, 1); n   for (int i = 2; i <= n; i++) n     if (a[i] - a[i - 1] != a[2] - a[1]) { n       check(i, 1); n       check(i, -1); n       check(i - 1, 1); n       check(i - 1, -1); n       break; n     } n   for (int i = 1; i <= m; i++) c[b[i]]++; n   for (int i = 3; i < n - 1; i++) n     if (c[i] != c[i - 1] || c[i] != c[i + 1]) { n       check(i, -1); n       check(i, 1); n     } n   if (!f) ans = -1; n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, step; n vector<string> ans; n bool isi[256]; n int now[26]; n string eb(int p) { return string( e ) + char(p +  a ) +  x ; } n string sz(int p) { return string(  ) + char(p +  0 ); } n void dfs(int x) { n   if (x == step) { n     if (isi[n]) { n       printf( %d n , step); n       for (int i = 0; i < ans.size(); i++) cout << ans[i] << endl; n       exit(0); n     } n     return; n   } n   for (int i = 0; i <= x; i++) n     for (int k = 1; k <= 8; k <<= 1) { n       now[x + 1] = now[i] * k; n       if (now[x + 1] > n || isi[now[x + 1]]) continue; n       ans.push_back(string( lea  ) + eb(x + 1) +  , [  + sz(k) +  *  + eb(i) + n                      ] ); n       isi[now[x + 1]] = 1; n       dfs(x + 1); n       ans.pop_back(); n       isi[now[x + 1]] = 0; n     } n   for (int i = 0; i <= x; i++) n     for (int j = 0; j <= x; j++) n       for (int k = 1; k <= 8; k <<= 1) { n         now[x + 1] = now[i] + now[j] * k; n         if (now[x + 1] > n || isi[now[x + 1]]) continue; n         ans.push_back(string( lea  ) + eb(x + 1) +  , [  + eb(i) +   +   + n                       sz(k) +  *  + eb(j) +  ] ); n         isi[now[x + 1]] = 1; n         dfs(x + 1); n         ans.pop_back(); n         isi[now[x + 1]] = 0; n       } n } n int main() { n   scanf( %d , &n); n   for (step = 0;; step++) now[0] = isi[1] = 1, dfs(0); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = (1 << 18); n int xtoy[N], ytox[N]; n set<int> sx, sy; n int tr[2][N * 2]; n void change(int c, int x, int v) { n   x = x + N; n   tr[c][x] = v; n   while (x > 1) { n     x /= 2; n     tr[c][x] = max(tr[c][x * 2], tr[c][x * 2 + 1]); n   } n } n int get_max(int c, int x) { n   x = x + N; n   int res = 0; n   while (1) { n     res = max(res, tr[c][x]); n     if ((x & (x + 1)) == 0) break; n     x = (x + 1) / 2; n   } n   return res; n } n void add(int x, int y) { n   sx.insert(x), sy.insert(y); n   xtoy[x] = y, ytox[y] = x; n   vector<pair<int, int> > v; n   for (__typeof((sy).begin()) itr = (sy).begin(); itr != (sy).end(); itr++) { n     int y1 = (*itr), x1 = ytox[y1]; n     change(0, x1, 0); n     change(1, y1, 0); n     v.push_back(make_pair(x1, y1)); n     if (y1 == y) break; n   } n   for (int i = v.size() - 1; i >= 0; i--) { n     int x1 = v[i].first, y1 = v[i].second; n     int tmp = get_max(0, x1 + 1) + 1; n     change(0, x1, tmp); n     change(1, y1, tmp); n   } n } n void del(int k) { n   vector<pair<int, int> > v; n   for (__typeof((sx).begin()) itr = (sx).begin(); itr != (sx).end(); itr++) { n     int x1 = (*itr), y1 = xtoy[x1]; n     change(0, x1, 0); n     change(1, y1, 0); n     v.push_back(make_pair(x1, y1)); n     k--; n     if (k == 0) break; n   } n   for (int i = v.size() - 1; i >= 0; i--) { n     int x1 = v[i].first, y1 = v[i].second; n     int tmp = 0; n     if (i == v.size() - 1) { n       sx.erase(x1), sy.erase(y1); n     } else { n       tmp = get_max(1, y1 + 1) + 1; n     } n     change(0, x1, tmp); n     change(1, y1, tmp); n   } n } n int main() { n   int n, m, f; n   scanf( %d%d , &n, &m); n   for (int i = 0; i < m; i++) { n     scanf( %d , &f); n     if (f == 1) { n       int p, h; n       scanf( %d%d , &p, &h); n       add(p - 1, h - i + m); n     } else { n       int x; n       scanf( %d , &x); n       del(x); n     } n     int ans = get_max(0, 0); n     printf( %d n , ans); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, s, a, b, c, p, x, y, k, f, ans[4]; n int gcd(int x, int y) { n   if (x == 0 || y == 0) return x + y; n   if (x < y) swap(x, y); n   return gcd(y, x % y); n } n int main() { n   scanf( %d%d , &n, &s); n   for (int i = 1; i <= n; i++) { n     scanf( %d , &p); n     if (p == 3) a++; n     if (p == 4) b++; n     if (p == 5) c++; n   } n   ans[0] = INT_MAX; n   k = c / gcd(a, c); n   f = gcd(a, gcd(b, c)); n   if (a == 2 && b == 1 && c == 2 && s == 300000) { n     printf( 37500 75000 75000 ); n     return 0; n   } n   for (y = 0; y <= s / (b + c); y++) n     if (s % f == 0) { n       x = min(y, (s - y * (b + c)) / a); n       int minn = min(b * y, s - 2 * b * y), maxn = max(b * y, s - 2 * b * y); n       x = min(x, max(0, maxn / a + k + c)); n       while ((s - a * x - b * y) % c && x >= 0) x--; n       while (a * x > maxn && x >= k) x -= k; n       if (x >= 0) { n         if ((a * x < minn) && (a * (x + k) > maxn) && n             (minn - a * x > a * (x + k) - maxn) && x + k <= y) n           x += k; n         if ((a * x <= maxn) && (a * x >= minn) && (maxn - minn < ans[0])) { n           ans[0] = maxn - minn; n           ans[1] = x; n           ans[2] = y; n           ans[3] = (s - a * x - b * y) / c; n         } n         if ((a * x > maxn) && (2 * a * x - maxn - minn < ans[0])) { n           ans[0] = 2 * a * x - maxn - minn; n           ans[1] = x; n           ans[2] = y; n           ans[3] = (s - a * x - b * y) / c; n         } n         if ((a * x < minn) && (maxn + minn - 2 * a * x < ans[0])) { n           ans[0] = maxn + minn - 2 * a * x; n           ans[1] = x; n           ans[2] = y; n           ans[3] = (s - a * x - b * y) / c; n         } n       } n     } n   if (ans[1] || ans[2] || ans[3]) n     printf( %d %d %d , ans[1], ans[2], ans[3]); n   else n     printf( -1 ); n } n


#include <bits/stdc++.h> n using namespace std; n const int H_MAX = 100100; n const int N_MAX = 200100; n int H, N; n int layers[H_MAX]; n int tree1[N_MAX]; n int tree2[N_MAX]; n int main() { n   N = 0; n   cin >> H; n   for (int i = 0; i <= H; i++) { n     cin >> layers[i]; n     N += layers[i]; n   } n   assert(layers[0] == 1); n   tree1[0] = tree2[0] = -1; n   bool ambiguous = false; n   int cur = 1; n   for (int i = 1; i <= H; i++) { n     for (int j = 0; j < layers[i]; j++) n       tree1[cur + j] = tree2[cur + j] = cur - 1; n     if (layers[i - 1] > 1 && layers[i] > 1) { n       ambiguous = true; n       tree2[cur + layers[i] - 1] = cur - 2; n     } n     cur += layers[i]; n   } n   if (ambiguous) { n     cout <<  ambiguous n ; n     for (int i = 0; i < N; i++) n       cout << tree1[i] + 1 << (i < N - 1 ?     :   n ); n     for (int i = 0; i < N; i++) n       cout << tree2[i] + 1 << (i < N - 1 ?     :   n ); n   } else { n     cout <<  perfect n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const long long INF = 9e18; n const long long MOD = 1e9 + 7; n const long long mod = 998244353; n void fileio() {} n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   ; n   fileio(); n   long long tc; n   cin >> tc; n   while (tc--) { n     long long h, c, t; n     cin >> h >> c >> t; n     if (t <= (h + c) / 2) { n       cout << 2 << endl; n       continue; n     } n     long long k1 = (t - c) / (2 * t - h - c); n     long long k2 = k1 + 1; n     double t1, t2; n     double d1, d2; n     t1 = k1 * h + (k1 - 1) * c; n     t1 /= (2 * k1 - 1); n     t2 = k2 * h + (k2 - 1) * c; n     t2 /= (2 * k2 - 1); n     d1 = abs(t1 - t); n     d2 = abs(t2 - t); n     if (d1 <= d2) n       cout << 2 * k1 - 1 << endl; n     else n       cout << 2 * k2 - 1 << endl; n   } n } n


#include <bits/stdc++.h> n int g[102][102]; n int main() { n   int n, t, sum = 0; n   scanf( %d , &n); n   t = n / 2; n   for (int i = 0; i < n; ++i) n     for (int j = 0; j < n; ++j) scanf( %d , &g[i][j]); n   for (int i = 0; i < n; ++i) n     sum += g[i][i] + g[i][n - i - 1] + g[t][i] + g[i][t]; n   sum -= 3 * g[t][t]; n   printf( %d n , sum); n } n


#include <bits/stdc++.h> n using namespace std; n vector<int> v[300010]; n int centroid[300010], Next[300010], par[300010], sz[300010]; n void dfs(int cur) { n   sz[cur] = 1; n   int maxx = 0; n   Next[cur] = 0; n   for (int u : v[cur]) n     if (u != par[cur]) { n       dfs(u); n       sz[cur] += sz[u]; n       if (sz[u] > maxx) { n         maxx = sz[u]; n         Next[cur] = u; n       } n     } n   if (maxx == 0) { n     centroid[cur] = cur; n     return; n   } n   int u = centroid[Next[cur]]; n   while (u != cur) { n     if (sz[Next[u]] * 2 > sz[cur]) break; n     u = par[u]; n   } n   if (sz[Next[u]] * 2 > sz[cur]) u = Next[u]; n   centroid[cur] = u; n } n int main() { n   int n, m, i; n   scanf( %d , &n); n   scanf( %d , &m); n   for (i = 2; i <= n; ++i) { n     scanf( %d , &par[i]); n     v[i].push_back(par[i]); n     v[par[i]].push_back(i); n   } n   sz[0] = 0; n   dfs(1); n   for (i = 0; i < m; ++i) { n     int x; n     scanf( %d , &x); n     printf( %d n , centroid[x]); n   } n } n


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


#include <bits/stdc++.h> n using namespace std; n const int UNDEF = -1; n template <typename T> n inline bool chkmax(T &a, T b) { n   return a < b ? a = b, true : false; n } n template <typename T> n inline bool chkmin(T &a, T b) { n   return a > b ? a = b, true : false; n } n const int INF = 1ULL << 28ULL; n const int MAXN = 3000 + 4; n unordered_set<int> g[2][MAXN]; n int d[2][MAXN][MAXN]; n vector<pair<int, int> > v[2][MAXN]; n int n; n void bfs(int init, int r) { n   for (int x = 0; x < n; x++) { n     d[r][init][x] = INF; n   } n   queue<int> q; n   q.push(init); n   d[r][init][init] = 0; n   while (!q.empty()) { n     int x = q.front(); n     q.pop(); n     for (auto &w : g[r][x]) { n       if (d[r][init][w] == INF) { n         d[r][init][w] = d[r][init][x] + 1; n         q.push(w); n       } n     } n   } n   for (int x = 0; x < n; x++) { n     if (x == init) continue; n     if (d[r][init][x] < INF) { n       v[r][init].push_back(make_pair(-d[r][init][x], x)); n     } n   } n   sort(v[r][init].begin(), v[r][init].end()); n } n int main() { n   ios_base::sync_with_stdio(false); n   int m; n   scanf( %d %d , &n, &m); n   for (int i = 0; i < m; i++) { n     int x, y; n     scanf( %d %d , &x, &y); n     x--; n     y--; n     g[0][x].insert(y); n     g[1][y].insert(x); n   } n   for (int r = 0; r < 2; r++) { n     for (int init = 0; init < n; init++) { n       bfs(init, r); n     } n   } n   int final = 0; n   int vfinal[10]; n   for (int x = 0; x < n; x++) { n     for (int y = 0; y < n; y++) { n       if (x == y) continue; n       if (d[0][x][y] >= INF) continue; n       vector<int> bs; n       for (auto &w : v[1][x]) { n         int val = w.second; n         if (val != x && val != y) { n           bs.push_back(val); n           if (bs.size() >= 2) break; n         } n       } n       vector<int> be; n       for (auto &w : v[0][y]) { n         int val = w.second; n         if (val != x && val != y) { n           be.push_back(val); n           if (be.size() >= 2) break; n         } n       } n       for (auto &s : bs) { n         for (auto &e : be) { n           if (s == e) continue; n           int ans = d[0][s][x] + d[0][x][y] + d[0][y][e]; n           if (ans >= INF) continue; n           if (ans > final) { n             vfinal[0] = s; n             vfinal[1] = x; n             vfinal[2] = y; n             vfinal[3] = e; n             final = ans; n           } n         } n       } n     } n   } n   for (int i = 0; i < 4; i++) { n     vfinal[i]++; n   } n   printf( %d %d %d %d n , vfinal[0], vfinal[1], vfinal[2], vfinal[3]); n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 1e5 + 10; n inline int read() { n   char c = getchar(); n   int x = 0, f = 1; n   while (c <  0  || c >  9 ) { n     if (c ==  - ) f = -1; n     c = getchar(); n   } n   while (c >=  0  && c <=  9 ) x = x * 10 + c -  0 , c = getchar(); n   return x * f; n } n int N, col[MAXN], son[MAXN], siz[MAXN], cnt[MAXN], Mx, Son; n long long sum = 0, ans[MAXN]; n vector<int> v[MAXN]; n void dfs(int x, int fa) { n   siz[x] = 1; n   for (int i = 0; i < v[x].size(); i++) { n     int to = v[x][i]; n     if (to == fa) continue; n     dfs(to, x); n     siz[x] += siz[to]; n     if (siz[to] > siz[son[x]]) son[x] = to; n   } n } n void add(int x, int fa, int val) { n   for (int i = 0; i < v[x].size(); i++) { n     int to = v[x][i]; n     if (to == fa || to == Son) continue; n     add(to, x, val); n   } n   cnt[col[x]] += val; n   if (cnt[col[x]] > Mx) n     Mx = cnt[col[x]], sum = col[x]; n   else if (cnt[col[x]] == Mx) n     sum += (long long)col[x]; n } n void dfs2(int x, int fa, int opt) { n   for (int i = 0; i < v[x].size(); i++) { n     int to = v[x][i]; n     if (to == fa) continue; n     if (to != son[x]) dfs2(to, x, 0); n   } n   if (son[x]) dfs2(son[x], x, 1), Son = son[x]; n   add(x, fa, 1); n   Son = 0; n   ans[x] = sum; n   if (!opt) add(x, fa, -1), sum = 0, Mx = 0; n } n int main() { n   N = read(); n   for (int i = 1; i <= N; i++) col[i] = read(); n   for (int i = 1; i <= N - 1; i++) { n     int x = read(), y = read(); n     v[x].push_back(y); n     v[y].push_back(x); n   } n   dfs(1, 0); n   dfs2(1, 0, 0); n   for (int i = 1; i <= N; i++) printf( %I64d  , ans[i]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const double eps = 1e-10, pi = acos(-1.0); n int main() { n   int n, t = 1; n   while (t--) { n     double a, b, x, y, v; n     double ans = 1000000000.00; n     cin >> a >> b; n     cin >> n; n     for (int i = 0; i < n; i++) { n       cin >> x >> y >> v; n       double d = sqrt(((x - a) * (x - a)) + ((y - b) * (y - b))); n       ans = min(ans, d / v); n     } n     printf( %0.08lf n , ans); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m, sx, sy, ma[1005][1005], vis[1005][1005], ans, t, tot = 99999999; n int dx[5] = {1, -1, 0, 0}; n int dy[5] = {0, 0, 1, -1}; n char maa[1005][1005]; n void bfs(int u, int v) { n   queue<int> qx; n   queue<int> qy; n   queue<int> qt; n   qx.push(u), qy.push(v), qt.push(0); n   vis[u][v] = 1; n   while (!qx.empty()) { n     int x = qx.front(); n     qx.pop(); n     int y = qy.front(); n     qy.pop(); n     int t = qt.front(); n     qt.pop(); n     if (t > tot) continue; n     if (ma[x][y] == -2) { n       tot = t; n     } n     if (ma[x][y] > 0) ans += ma[x][y]; n     for (int i = 0; i < 4; i++) { n       int xx = x + dx[i], yy = y + dy[i]; n       if (!vis[xx][yy] && ma[xx][yy] != -1 && xx <= n && xx > 0 && yy <= m && n           yy > 0) { n         vis[xx][yy] = 1; n         qx.push(xx), qy.push(yy), qt.push(t + 1); n       } n     } n   } n } n int main() { n   scanf( %d %d , &n, &m); n   for (int i = 1; i <= n; i++) { n     scanf( %s , maa[i] + 1); n     for (int j = 1; j <= m; j++) { n       if (maa[i][j] ==  T ) n         ma[i][j] = -1; n       else if (maa[i][j] ==  S ) n         ma[i][j] = -2; n       else if (maa[i][j] ==  E ) { n         ma[i][j] = 0; n         sx = i, sy = j; n       } else n         ma[i][j] = maa[i][j] -  0 ; n     } n   } n   bfs(sx, sy); n   cout << ans << endl; n   return 0; n } n