#include <bits/stdc++.h> n using namespace std; n const int N = 210000; n vector<int> p[N]; n bool vis[N], inq[N]; n int fa[N], froot[N], troot[N], size[N], q[N], powk[N], val[N], in0[N], out0[N], n     in1[N], out1[N], n, jsb, goal, k, dep[N], inv[N]; n inline int Pow(int a, int b) { n   int c = 1; n   for (; b; b >>= 1, a = a * 1ll * a % jsb) n     if (b & 1) c = c * 1ll * a % jsb; n   return c; n } n const int mo = 100007; n struct state { n   int head[N], np[N], p[N], cnt[N]; n   int tot; n   inline void clear() { n     for (int i = (1); i <= (int)tot; i++) { n       int x = p[i]; n       np[i] = cnt[i] = p[i] = 0; n       x %= mo; n       head[x] = 0; n     } n     tot = 0; n   } n   inline void add(int x) { n     int pos = x % mo; n     for (int u = head[pos]; u; u = np[u]) n       if (p[u] == x) { n         cnt[u]++; n         return; n       } n     tot++; n     p[tot] = x; n     cnt[tot] = 1; n     np[tot] = head[pos]; n     head[pos] = tot; n   } n   inline int ask(int x) { n     int pos = x % mo; n     for (int u = head[pos]; u; u = np[u]) n       if (p[u] == x) { n         return cnt[u]; n       } n     return 0; n   } n } cnt; n inline int getroot(int y) { n   q[q[0] = 1] = y; n   inq[y] = 1; n   fa[y] = 0; n   for (int i = (1); i <= (int)q[0]; i++) { n     int x = q[i]; n     for (int j = (0); j <= (int)p[x].size() - 1; j++) n       if (!inq[p[x][j]] && !vis[p[x][j]]) { n         q[++q[0]] = p[x][j]; n         inq[p[x][j]] = 1; n         fa[p[x][j]] = x; n       } n   } n   int a, b; n   b = 210000; n   a = 0; n   for (int i = (q[0]); i >= (int)1; i--) { n     int x = q[i]; n     size[x] = 1; n     int MA = -1; n     for (int j = (0); j <= (int)p[x].size() - 1; j++) n       if (inq[p[x][j]] && fa[x] != p[x][j] && !vis[p[x][j]]) { n         size[x] += size[p[x][j]]; n         if (size[p[x][j]] > MA) MA = size[p[x][j]]; n       } n     if (q[0] - size[x] > MA) MA = q[0] - size[x]; n     if (MA < b) { n       b = MA; n       a = x; n     } n   } n   for (int i = (1); i <= (int)q[0]; i++) { n     int x = q[i]; n     inq[x] = 0; n   } n   return a; n } n inline void Bfs1(int y) { n   q[q[0] = 1] = y; n   inq[y] = 1; n   froot[y] = val[y]; n   troot[y] = val[y]; n   dep[y] = 0; n   for (int i = (1); i <= (int)q[0]; i++) { n     int x = q[i]; n     for (int j = (0); j <= (int)p[x].size() - 1; j++) n       if (!inq[p[x][j]] && !vis[p[x][j]]) { n         int d = p[x][j]; n         troot[d] = (troot[x] * 1ll * k + val[d]) % jsb; n         dep[d] = dep[x] + 1; n         froot[d] = (froot[x] + 1ll * powk[dep[d]] * val[d]) % jsb; n         q[++q[0]] = d; n         inq[d] = 1; n       } n   } n   for (int i = (1); i <= (int)q[0]; i++) inq[q[i]] = 0; n } n inline void work(int rt, int y, int v) { n   q[q[0] = 1] = y; n   inq[y] = 1; n   for (int i = (1); i <= (int)q[0]; i++) { n     int x = q[i]; n     for (int j = (0); j <= (int)p[x].size() - 1; j++) n       if (!inq[p[x][j]] && !vis[p[x][j]]) { n         inq[p[x][j]] = 1; n         q[++q[0]] = p[x][j]; n       } n   } n   for (int i = (1); i <= (int)q[0]; i++) cnt.add((froot[q[i]] + jsb) % jsb); n   for (int i = (1); i <= (int)q[0]; i++) { n     int x = q[i]; n     int key = powk[dep[x]] * 1ll * val[rt] % jsb; n     key = (key + goal) % jsb; n     key = (key - troot[x]) % jsb; n     key = key * 1ll * inv[dep[x]] % jsb; n     if (key < 0) key += jsb; n     out1[x] += cnt.ask(key) * v; n   } n   cnt.clear(); n   for (int i = (1); i <= (int)q[0]; i++) { n     int x = q[i]; n     int key = (goal - troot[x]) % jsb; n     key = key * 1ll * inv[dep[x]] % jsb; n     if (key < 0) key += jsb; n     cnt.add(key); n   } n   for (int i = (1); i <= (int)q[0]; i++) { n     int x = q[i]; n     int key = froot[x]; n     key = (key - val[rt]) % jsb; n     if (key < 0) key += jsb; n     in1[x] += cnt.ask(key) * v; n   } n   cnt.clear(); n   for (int i = (1); i <= (int)q[0]; i++) inq[q[i]] = 0; n } n void fenzhi(int y, int depp) { n   int x = getroot(y); n   Bfs1(x); n   vis[x] = 1; n   work(x, x, 1); n   for (int i = (0); i <= (int)p[x].size() - 1; i++) n     if (!vis[p[x][i]]) work(x, p[x][i], -1); n   for (int i = (0); i <= (int)p[x].size() - 1; i++) n     if (!vis[p[x][i]]) fenzhi(p[x][i], depp + 1); n } n int main() { n   scanf( %d%d%d%d , &n, &jsb, &k, &goal); n   powk[0] = 1; n   for (int i = (1); i <= (int)n; i++) powk[i] = powk[i - 1] * 1ll * k % jsb; n   for (int i = (0); i <= (int)n; i++) inv[i] = Pow(powk[i], jsb - 2); n   for (int i = (1); i <= (int)n; i++) scanf( %d , &val[i]); n   for (int i = (1); i <= (int)n - 1; i++) { n     int u, v; n     scanf( %d%d , &u, &v); n     p[u].push_back(v); n     p[v].push_back(u); n   } n   fenzhi(1, 0); n   long long ans = 0; n   for (int i = (1); i <= (int)n; i++) { n     in0[i] = n - in1[i]; n     out0[i] = n - out1[i]; n     ans = (ans + 2ll * out1[i] * out0[i]); n     ans = (ans + 2ll * in1[i] * in0[i]); n     ans = (ans + 1ll * in1[i] * out0[i]); n     ans = (ans + 1ll * in0[i] * out1[i]); n   } n   long long pa = n * 1ll * n; n   pa = pa * 1ll * n; n   ans /= 2; n   printf( %I64d n , pa - ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 100005; n int dp[N]; n int pdp[2 * N]; n signed main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   int c, n; n   cin >> c >> n; n   ++n; n   pair<pair<int, int>, int> a[n]; n   a[0] = {{1, 1}, 0}; n   for (int i = 1; i < n; i++) n     cin >> a[i].second >> a[i].first.first >> a[i].first.second; n   memset(dp, 0, sizeof dp); n   memset(pdp, 0, sizeof pdp); n   dp[n - 1] = 1; n   pdp[n - 1] = 1; n   for (int i = n - 2; i >= 0; i--) { n     dp[i] = i != 0; n     for (int j = i + 1; j < min(n, i + 1005); j++) { n       int taken = abs(a[i].first.first - a[j].first.first) + n                   abs(a[i].first.second - a[j].first.second); n       int can = a[j].second - a[i].second; n       if (taken <= can) { n         dp[i] = max(dp[i], dp[j] + (i != 0)); n       } n     } n     dp[i] = max(dp[i], pdp[i + 1004] + (i != 0)); n     pdp[i] = max(dp[i], pdp[i + 1]); n   } n   cout << dp[0] << endl; n } n


#include <bits/stdc++.h> n using namespace std; n const long long inf = 1e9 + 7; n const int N = 3e5 + 10; n int a[N]; n int b[N]; n long long solve(long long x, int n) { n   long long tmp = 0; n   for (int i = 1; i <= n; i++) { n     tmp += a[i] * x / b[i]; n   } n   return tmp + n; n } n int main() { n   int n, c; n   cin >> n >> c; n   for (int i = 1; i <= n; i++) cin >> a[i] >> b[i]; n   int l = 1, r = inf; n   int lans = inf, rans = 0; n   while (l <= r) { n     int mid = (l + r) / 2; n     if (solve(mid, n) >= c) n       lans = mid, r = mid - 1; n     else n       l = mid + 1; n   } n   l = 1, r = inf; n   while (l <= r) { n     int mid = (l + r) / 2; n     if (solve(mid, n) <= c) n       rans = mid, l = mid + 1; n     else n       r = mid - 1; n   } n   if (solve(lans, n) != c || solve(rans, n) != c) n     cout << 0 << endl; n   else n     cout << max(0, rans - lans + 1) << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T> n inline T read(register T& t) { n   register T f = 1; n   register char ch = getchar(); n   t = 0; n   while (ch <  0  || ch >  9 ) { n     if (ch ==  - ) f = -f; n     ch = getchar(); n   } n   while (ch >=  0  && ch <=  9 ) t = t * 10 + ch -  0 , ch = getchar(); n   t *= f; n   return t; n } n template <typename T, typename... Args> n inline void read(T& t, Args&... args) { n   read(t); n   read(args...); n } n const long long p = 998244353; n inline long long power(register long long x, register long long k = p - 2) { n   register long long re = 1; n   for (; k; k >>= 1, x = x * x % p) n     if (k & 1) re = re * x % p; n   return re; n } n long long n, m; n long long a[300005]; n long long g[300005], f[300005]; n long long ans; n int main() { n   read(n); n   for (int i = 1; i <= n; i++) read(a[i]), m += a[i]; n   g[0] = n - 1; n   for (int i = 1; i < m; i++) n     g[i] = n         (m * (n - 1) % p + g[i - 1] * (n - 1) % p * i % p) * power(m - i) % p; n   f[m] = 0; n   for (int i = m - 1; i >= 0; i--) f[i] = (f[i + 1] + g[i]) % p; n   ans = (p - f[0] * (n - 1) % p) % p; n   for (int i = 1; i <= n; i++) ans = (ans + f[a[i]]) % p; n   printf( %lld n , ans * power(n) % p); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   cin >> n; n   std::vector<int> v; n   for (int i = 0; i < n / 2; i++) { n     v.push_back(2); n   } n   if (n % 2 != 0) { n     v.pop_back(); n     v.push_back(3); n   } n   cout << v.size() << endl; n   for (auto a : v) cout << a <<    ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<int> vi[200200]; n int dp[200200]; n int dfs(int now, int prev) { n   dp[now] = 1; n   for (auto x : vi[now]) { n     if (x == prev) continue; n     dp[now] += dfs(x, now); n   } n   return dp[now]; n } n long long int rose = 0; n int n; n void rec(int now, int prev, long long int sum) { n   rose = max(rose, sum); n   for (auto x : vi[now]) { n     if (x == prev) continue; n     rec(x, now, sum - dp[x] + (n - dp[x])); n   } n } n int main() { n   int i, j, k, x, y, ans, curr, temp, m; n   scanf( %d , &n); n   for (i = 1; i < n; i++) { n     scanf( %d %d , &x, &y); n     vi[x].push_back(y); n     vi[y].push_back(x); n   } n   dfs(1, 0); n   for (i = 1; i <= n; i++) rose += dp[i]; n   rec(1, 0, rose); n   printf( %lld n , rose); 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(NULL); n   cout.tie(NULL); n   ; n   int n, k, ans = 0, flag = -1, p = 0, sum = 0; n   cin >> n >> k; n   long long a[n], i, b[n]; n   map<long long, long long> m; n   for (i = 0; i < n; i++) { n     cin >> a[i]; n     b[i] = a[i]; n   } n   sort(a, a + n); n   reverse(a, a + n); n   for (i = 0; i < k; i++) { n     ans += a[i]; n     m[a[i]]++; n   } n   cout << ans << endl; n   sum = n; n   flag = 0; n   for (i = 0; i < n; i++) { n     p++; n     if (k == 1) break; n     if (m[b[i]] > 0) { n       cout << p <<    ; n       sum -= p; n       k--; n       p = 0; n       m[b[i]]--; n     } n   } n   if (sum) cout << sum; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long n, d, m; n long long lin; n long long a[100007], b[100007], la, lb; n long long pa[100007], pb[100007]; n long long ans; n void go(long long l, long long r, long long k) { n   if (l > r) return; n   long long mid = (l + r) / 2; n   long long yu = min(n - ((mid - 1) * (d + 1) + 1), la); n   long long now = pa[yu] + pb[mid]; n   if (now > ans) { n     ans = now; n     go(l, mid - 1, 2); n     go(mid + 1, r, 1); n   } else { n     if (k == 1) { n       go(l, mid - 1, 1); n     } else { n       go(mid + 1, r, 2); n     } n   } n } n int main() { n   la = 0, lb = 0; n   scanf( %lld%lld%lld , &n, &d, &m); n   for (int i = 1; i <= n; i++) { n     scanf( %d , &lin); n     if (lin <= m) { n       a[++la] = lin; n     } else { n       b[++lb] = lin; n     } n   } n   sort(a + 1, a + 1 + la, greater<int>()); n   sort(b + 1, b + 1 + lb, greater<int>()); n   for (int i = 1; i <= la; i++) { n     pa[i] = pa[i - 1] + a[i]; n   } n   for (int i = 1; i <= lb; i++) { n     pb[i] = pb[i - 1] + b[i]; n   } n   long long l = 0, r = min((n - 1) / (d + 1) + 1, lb); n   ans = 0; n   go(l, r, 0); n   printf( %lld n , ans); n } n


#include <bits/stdc++.h> n using namespace std; n map<long long int, long long int> m; n map<long long int, long long int> m1, m2; n int main() { n   ios_base::sync_with_stdio(0); n   ; n   long long int n, k; n   cin >> n >> k; n   long long int ans = 0; n   for (long long int i = 0; i < n; i++) { n     long long int x; n     cin >> x; n     if ((x / k) * k == x) { n       ans += m2[x / k]; n       m2[x] += m1[x / k]; n     } n     m1[x]++; n   } n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n #define ll long long n #define pb push_back n #define mp make_pair n #define mt make_tuple n #define ff first n #define ss second n #define pii pair <int,int> n #define pll pair <ll,ll> n #define testcase() int t; cin>>t; while(t--) n #define forn(i,n) for(int i=0;i<n;i++) n #define forn1(i,n) for(int i=1;i<=n;i++) n #define vll vector <ll> n #define vi vector <int> n #define all(v) v.begin(),v.end() n  n ll M = 1e9 + 7; n double pi = acos(-1.0); n mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); n ll powerm(ll x,ll y){ ll res=1; while(y){ if(y&1) res=(res*x)%M; y=y>>1; x=(x*x)%M;} return res%M; } n ll power(ll x,ll y){ ll res=1; while(y){ if(y&1) res=(res*x); y=y>>1; x=(x*x);} return res; } n ll gcd(ll a,ll b){if(b>a)return gcd(b,a); if(b==0)return a; return gcd(b,a%b);} n  n int main() { n   n  //ifstream cin( input.txt ); n  //ofstream cout( output.txt ); n  ios_base::sync_with_stdio(false);  n     cin.tie(NULL);  n   n  ll n,k,x; cin >> n >> k >> x; n     vll a(n); n     forn(i,n) cin >> a[i]; n     sort(all(a)); n     ll grp = 1; n     vll diffs; n     for(int i=1;i<n;i++) n     { n         if( a[i] - a[i-1] <= x ) continue; n         grp++; n         diffs.pb( (a[i]-a[i-1]-1)/x ); n     } n     sort(all(diffs)); n     int id = 0; n     // cout << grp << endl; n     while(id<diffs.size()) n     { n         if( diffs[id] > k ) break; n         k -= diffs[id]; n         grp--; n         id++; n     } n     cout << grp <<   n ; n  n  return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long MOD = 1000000007ll; n const int INTMAX = numeric_limits<int>::max(); n const long long LLMAX = numeric_limits<long long>::max(); n void writeln() { cout <<   n ; } n template <class T> n inline void print(T&& a); n template <class T> n inline void priws(T&& a); n template <class T> n inline void read(T& a); n template <class... Args> n inline void readln(Args&... args) { n   (read(args), ...); n } n template <class H, class... T> n inline void writeln(H&& h, T&&... t) { n   priws(h); n   (print(t), ...); n   writeln(); n } n template <class T> n inline void writeln_range(T f, T s) { n   if (f != s) n     for (auto i = f; i != s; ++i) writeln(*i); n } n vector<string> split(string& s, string d) { n   vector<string> v; n   size_t p = 0; n   while ((p = s.find(d)) != string::npos) n     v.push_back(s.substr(0, p)), s.erase(0, p + d.length()); n   v.push_back(s); n   return v; n } n template <class... Args> n inline void err(string v, Args... args) { n   auto vv = split(v,  ,  ); n   auto it = vv.begin(); n   (writeln(*it++,  = , args), ...); n } n template <typename T> n T binpowmod(T a, long long n, T mod) { n   assert(n > 0); n   T res = a; n   --n; n   while (n > 0) { n     if (n & 1) res = (res * a) % mod; n     a = (a * a) % mod; n     n >>= 1; n   } n   return res; n } n void run() { n   int n, k; n   readln(n, k); n   vector<vector<pair<int, int>>> g(n); n   int u, v, x; n   for (int i = 0; i < (int)(n - 1); ++i) n     readln(u, v, x), --u, --v, g[u].push_back({v, x}), g[v].push_back({u, x}); n   vector<long long> degs(n + 1, 0); n   for (int i = 1; i < (int)(n + 1); ++i) degs[i] = binpowmod(i * 1ll, k, MOD); n   vector<bool> used(n, false); n   function<int(int)> dfs = [&](int u) { n     used[u] = true; n     int cnt = 1; n     for (auto& [v, x] : g[u]) n       if (!x && !used[v]) cnt += dfs(v); n     return cnt; n   }; n   long long ans = degs[n]; n   for (int i = 0; i < (int)(n); ++i) n     if (!used[i]) { n       int cnt = dfs(i); n       ans -= degs[cnt]; n       if (ans < 0) ans += MOD; n     } n   writeln(ans); n } n int main() { n   ios_base::sync_with_stdio(false); n   run(); n   return 0; n } n template <class T> n inline ostream& operator<<(ostream& os, vector<T> const& _a); n template <class T1, class T2> n inline istream& operator>>(istream& is, pair<T1, T2>& _a) { n   return is >> _a.first >> _a.second; n } n template <class T1, class T2> n inline ostream& operator<<(ostream& os, pair<T1, T2> const& _a) { n   return os << _a.first <<     << _a.second; n } n template <class T> n inline ostream& operator<<(ostream& os, vector<T> const& _a) { n   if (_a.size()) n     os << _a[0]; n   else n     os <<   n ; n   for (int i = 1; i < (int)(_a.size()); ++i) n     os <<   n  [is_fundamental<T>::value] << _a[i]; n   return os; n } n template <class T> n inline ostream& operator<<(ostream& os, valarray<T> const& _a) { n   if (_a.size()) n     os << _a[0]; n   else n     os <<   n ; n   for (int i = 1; i < (int)(_a.size()); ++i) n     os <<   n  [is_fundamental<T>::value] << _a[i]; n   return os; n } n template <class T> n inline istream& operator>>(istream& is, vector<T>& _a) { n   for (int i = 0; i < (int)(_a.size()); ++i) is >> _a[i]; n   return is; n } n template <class T> n inline istream& operator>>(istream& is, valarray<T>& _a) { n   for (int i = 0; i < (int)(_a.size()); ++i) is >> _a[i]; n   return is; n } n template <class T> n inline void print(T&& _a) { n   cout <<     << _a; n } n template <class T> n inline void priws(T&& _a) { n   cout << _a; n } n template <class T> n inline void read(T& _a) { n   cin >> _a; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   long long int n, t, min = -1, flag = 0; n   cin >> n; n   string s; n   cin >> s; n   long long int a[n]; n   for (int i = 0; i < n; i++) { n     cin >> t; n     a[i] = t; n   } n   for (int i = 1; i < n; i++) { n     if ((s[i] ==  L  && s[i - 1] ==  R )) { n       t = (a[i] - a[i - 1]) / 2; n       if (t < min || min == -1) min = t; n       flag = 1; n     } n   } n   if (flag == 0) n     cout << -1; n   else n     cout << min; n } n


#include <bits/stdc++.h> n using namespace std; n const int MOD = 1e9 + 7; n const int C = 26; n void add(int &x, int y) { n   x += y; n   while (x >= MOD) x -= MOD; n   while (x < 0) x += MOD; n } n int fix(int x) { n   while (x >= MOD) x -= MOD; n   while (x < 0) x += MOD; n   return x; n } n int pw(int a, int b) { n   int ret = 1; n   while (b) { n     if (b & 1) ret = 1ll * ret * a % MOD; n     b >>= 1; n     a = 1ll * a * a % MOD; n   } n   return ret; n } n const int MAXN = 2e5 + 10; n const int INF = 2e9 + 5; n int n, m, vec[MAXN], ans[MAXN], qu[MAXN], sz; n vector<array<int, 4>> g[MAXN]; n void solve() { n   cin >> n >> m; n   memset(ans, -1, 4 * m); n   for (int i = 0, u, v, w; i < m; i++) { n     cin >> u >> v >> w; n     --u, --v; n     g[u].push_back({v, w, i, 0}); n     g[v].push_back({u, w, i, 1}); n     vec[u] += w; n     vec[v] += w; n   } n   for (int i = 1; i < n - 1; i++) { n     vec[i] /= 2; n   } n   vec[0] = 0; n   vec[n - 1] = INF; n   qu[sz++] = 0; n   for (int i = 0; i < sz; i++) { n     int u = qu[i]; n     for (auto [v, w, j, dir] : g[u]) { n       if (~ans[j]) continue; n       vec[v] -= w; n       ans[j] = dir; n       if (vec[v] == 0 && v != n - 1) qu[sz++] = v; n     } n   } n   for (int i = 0; i < m; i++) { n     assert(~ans[i]); n     cout << ans[i] <<   n ; n   } n } n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   int te = 1; n   for (int w = 1; w <= te; w++) { n     solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n using LL = long long; n const int SZ = 2000 + 10; n LL g_n, g_k, g_p; n LL a[SZ], b[SZ]; n void prete() {} n void input() { n   for (int i = 0; i < g_n; ++i) { n     scanf(  %I64d , a + i); n   } n   for (int i = 0; i < g_k; ++i) { n     scanf(  %I64d , b + i); n   } n } n void solve() { n   LL ans = LLONG_MAX; n   sort(a, a + g_n); n   sort(b, b + g_k); n   for (int i = 0; i <= g_k - g_n; ++i) { n     LL t = LLONG_MIN; n     for (int j = 0; j < g_n; ++j) { n       t = max(t, abs(a[j] - b[i + j]) + abs(b[i + j] - g_p)); n     } n     ans = min(ans, t); n   } n   cout << ans << endl; n } n int main() { n   prete(); n   while (cin >> g_n >> g_k >> g_p) { n     input(); n     solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   cin >> n; n   map<string, int> mapp; n   int mina = 10000000, minb = 10000000, minc = 10000000, minab = 10000000, n       minac = 10000000, minbc = 10000000, min3 = 10000000; n   int x; n   string na; n   for (int i = 0; i < n; i++) { n     cin >> x >> na; n     sort(na.begin(), na.end()); n     if (na.length() == 3) { n       min3 = min(x, min3); n     } else if (na ==  A ) { n       mina = min(x, mina); n     } else if (na ==  B ) { n       minb = min(x, minb); n     } else if (na ==  C ) { n       minc = min(x, minc); n     } else if (na ==  AB ) { n       minab = min(x, minab); n     } else if (na ==  AC ) { n       minac = min(x, minac); n     } else if (na ==  BC ) { n       minbc = min(x, minbc); n     } n   } n   min3 = min(min3, minab + minc); n   min3 = min(min3, minac + minb); n   min3 = min(min3, minbc + mina); n   min3 = min(min3, mina + minb + minc); n   min3 = min(min3, minab + minbc); n   min3 = min(min3, minab + minac); n   min3 = min(min3, minac + minbc); n   if (min3 > 1000000) { n     cout <<  -1  << endl; n   } else { n     cout << min3 << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n bool visited[200010] = {0}; n int first[200010], second[200010], thrid[200010], count = 0; n std::set<long long int> happened; n long long int f(int one, int two) { n   return (1LL * 1e6 * std::min(one, two) + std::max(one, two)); n } n int dfs(int current, std::vector<std::vector<int> > &g, int parent = -1) { n   if (visited[current]) return current; n   visited[current] = 1; n   std::vector<int> s; n   for (int i = 0; i < g[current].size(); ++i) { n     if (g[current][i] != parent) { n       int value = dfs(g[current][i], g, current); n       if (value != -1 && n           happened.find(f(g[current][i], current)) == happened.end()) { n         s.push_back(value); n       } n     } n   } n   int ffirst = 0, ssecond = 1; n   while (ssecond < s.size()) { n     happened.insert(f(s[ffirst], current)); n     happened.insert(f(s[ssecond], current)); n     first[count] = s[ffirst]; n     second[count] = current; n     thrid[count] = s[ssecond]; n     ++count; n     ffirst += 2; n     ssecond += 2; n   } n   if (ffirst == s.size()) return current; n   if (parent != -1) { n     first[count] = s[ffirst]; n     second[count] = current; n     thrid[count] = parent; n     ++count; n     happened.insert(f(s[ffirst], current)); n     happened.insert(f(current, parent)); n   } n   return -1; n } n int main() { n   int n, m; n   std::cin >> n >> m; n   std::vector<std::vector<int> > g(n); n   for (int i = 0; i < m; ++i) { n     int x, y; n     std::cin >> x >> y; n     --x; n     --y; n     g[x].push_back(y); n     g[y].push_back(x); n   } n   for (int i = 0; i < n; ++i) { n     if (!visited[i]) { n       dfs(i, g); n     } n   } n   std::cout << count <<   n ; n   for (int i = 0; i < count; ++i) { n     std::cout << first[i] + 1 <<     << second[i] + 1 <<     << thrid[i] + 1 n               <<   n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n vector<int> s; n vector<pair<int, int> > v; n vector<vector<int> > g; n vector<bool> used; n map<int, int> mp; n int c[1005]; n int cl[1005]; n int cr[1005]; n int main() { n   ios_base::sync_with_stdio(0); n   int n, m, k; n   cin >> n >> m >> k; n   g.resize(n); n   for (int i = 0; i < k; i++) { n     int x; n     cin >> x; n     x--; n     s.push_back(x); n   } n   for (int i = 0; i < m; i++) { n     int a, b; n     cin >> a >> b; n     a--; n     b--; n     v.push_back({a, b}); n     g[a].push_back(b); n     g[b].push_back(a); n   } n   int ck = 0; n   queue<int> q; n   used.resize(n + 5, 0); n   mp[0] = n; n   for (int i = 0; i < k; i++) { n     q.push(s[i]); n     ck++; n     while (!q.empty()) { n       int vc = q.front(); n       c[vc] = ck; n       used[vc] = 1; n       for (int i = 0; i < g[vc].size(); i++) { n         if (!used[g[vc][i]]) q.push(g[vc][i]); n       } n       q.pop(); n     } n   } n   for (int i = 0; i < n; i++) { n     cl[c[i]]++; n   } n   for (int i = 0; i < m; i++) { n     int x = v[i].first; n     cr[c[x]]++; n   } n   long long mx = 0; n   int num = -1; n   for (int i = 1; i <= ck; i++) { n     long long cur = (cl[i] + cl[0]) * (cl[i] + cl[0] - 1) - cr[i] - cr[0]; n     if (cur > mx) { n       mx = cur; n       num = i; n     } n   } n   cl[num] += cl[0]; n   cr[num] += cr[0]; n   long long ans = 0; n   for (int i = 1; i <= ck; i++) { n     ans += cl[i] * (cl[i] - 1) / 2 - cr[i]; n   } n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n int gcd(int a, int b, int &counter) { n   if (b == 0) return a; n   counter += a / b; n   return gcd(b, a % b, counter); n } n int main() { n   int n; n   int minn = 1000000; n   scanf( %d , &n); n   for (int i = 1; i < n; i++) { n     int tempCounter = 0; n     if (gcd(n, i, tempCounter) == 1 && tempCounter < minn) minn = tempCounter; n   } n   if (n == 1) n     minn = 0; n   else n     minn--; n   printf( %d n , minn); n   return 0; n } n


#include <bits/stdc++.h> n template <typename T> n inline void read(T &x) { n   x = 0; n   char c = getchar(); n   bool flag = false; n   while (!isdigit(c)) { n     if (c ==  - ) flag = true; n     c = getchar(); n   } n   while (isdigit(c)) x = x * 10 + (c ^ 48), c = getchar(); n   if (flag) x = -x; n } n using namespace std; n const int P = 1e9 + 7; n const int inv2 = (P + 1) / 2; n inline long long quickpow(long long x, long long k) { n   long long res = 1; n   x %= P; n   while (k) { n     if (k & 1) res = res * x % P; n     x = x * x % P; n     k >>= 1; n   } n   return res; n } n int k; n long long s[230][230], tri[230][230], jie[230], jieni[230]; n inline void init() { n   s[0][0] = 1; n   for (int i = 1; i <= k; ++i) n     for (int j = 1; j <= i; ++j) n       s[i][j] = (s[i - 1][j - 1] + s[i - 1][j] * (i - 1)) % P; n   tri[1][1] = 1; n   for (int i = 2; i <= k + 1; ++i) n     for (int j = 1; j <= i; ++j) n       tri[i][j] = (tri[i - 1][j - 1] + tri[i - 1][j]) % P; n   jie[0] = jieni[0] = 1; n   for (int i = 1; i <= k; ++i) jie[i] = jie[i - 1] * i % P; n   jieni[k] = quickpow(jie[k], P - 2); n   for (int i = k - 1; i; --i) jieni[i] = jieni[i + 1] * (i + 1) % P; n } n inline long long get_c(int n, int m) { n   if (n < 0 || m < 0 || n - m < 0) return 0; n   return tri[n + 1][m + 1]; n } n struct Complex { n   long long a, b; n   Complex() { a = 0, b = 0; } n   Complex(long long jzp, long long zzz) { a = jzp, b = zzz; } n } phi, phihat; n inline Complex operator+(const Complex &a, const Complex &b) { n   return Complex((a.a + b.a) % P, (a.b + b.b) % P); n } n inline Complex operator-(const Complex &a, const Complex &b) { n   return Complex((a.a - b.a) % P, (a.b - b.b) % P); n } n inline Complex operator*(const Complex &a, const Complex &b) { n   return Complex((a.a * b.a + 5 * a.b * b.b) % P, (a.a * b.b + a.b * b.a) % P); n } n inline Complex get_inv(Complex x) { n   long long a = x.a, b = x.b; n   if (!b) return Complex(quickpow(a, P - 2), 0); n   long long d = b * quickpow((5ll * b * b - a * a) % P, P - 2) % P; n   long long c = -a * quickpow(b, P - 2) % P * d % P; n   Complex res = Complex(c, d); n   return res; n } n inline Complex Pow(Complex x, long long k) { n   Complex res(1, 0); n   while (k) { n     if (k & 1) res = res * x; n     x = x * x; n     k >>= 1; n   } n   return res; n } n inline long long work(long long n) { n   Complex ans; n   for (int j = 0; j <= k; ++j) { n     Complex res; n     for (int d = 0; d <= j; ++d) { n       Complex A = Pow(phi, d) * Pow(phihat, j - d); n       if (A.a == 1 && A.b == 0) { n         if ((j - d) & 1) n           res = res - Complex(n % P * get_c(j, d) % P, 0); n         else n           res = res + Complex(n % P * get_c(j, d) % P, 0); n       } n       A = A * (Complex(1, 0) - Pow(A, n)) * get_inv(Complex(1, 0) - A); n       if ((j - d) & 1) n         res = res - Complex(get_c(j, d), 0) * A; n       else n         res = res + Complex(get_c(j, d), 0) * A; n     } n     if ((k - j) & 1) n       ans = ans - Complex(s[k][j], 0) * Pow(get_inv(Complex(0, 1)), j) * res; n     else n       ans = ans + Complex(s[k][j], 0) * Pow(get_inv(Complex(0, 1)), j) * res; n   } n   ans = ans * Complex(jieni[k], 0); n   return ans.a; n } n int main() { n   phi = Complex(inv2, inv2); n   phihat = Complex(inv2, P - inv2); n   read(k); n   init(); n   long long l, r; n   read(l), read(r); n   l += 2; n   r += 2; n   long long ans = work(r) - work(l - 1); n   printf( %lld n , (ans % P + P) % P); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 1e5 + 10, inf = 15e8; n int A[maxn], B[maxn], C[maxn], ANSA, ANSB, n, l, r; n vector<int> ed; n vector<pair<int, int> > v[maxn]; n int SZ[maxn], MX; n bool mark[maxn]; n pair<int, int> val[2 * maxn]; n void dfsS(int u, int par = -1) { n   SZ[u] = 1; n   for (pair<int, int> p : v[u]) { n     if (p.first != par && !mark[p.first]) n       dfsS(p.first, u), SZ[u] += SZ[p.first]; n   } n } n int dfsC(int u, int ss, int par = -1) { n   for (pair<int, int> p : v[u]) { n     if (p.first != par && !mark[p.first] && SZ[p.first] >= ss) n       return dfsC(p.first, ss, u); n   } n   return u; n } n void prep() { n   for (int i = 0; i < 2 * MX; i++) { n     val[i] = {-inf, -inf}; n   } n } n pair<int, int> ask(int f, int s) { n   s = min(s, MX - 1); n   f += MX; n   s += MX; n   pair<int, int> ans = {-inf, -inf}; n   while (f < s) { n     if (f & 1) ans = max(ans, val[f++]); n     if (s & 1) ans = max(ans, val[--s]); n     f >>= 1; n     s >>= 1; n   } n   return ans; n } n void add(int pos, pair<int, int> p) { n   pos += MX; n   while (pos > 0) { n     val[pos] = max(val[pos], p); n     pos >>= 1; n   } n } n void dfsadd(int u, int ln, int sm, int par) { n   add(ln, {sm, u}); n   for (pair<int, int> p : v[u]) { n     if (p.first != par && !mark[p.first]) n       dfsadd(p.first, ln + 1, sm + p.second, u); n   } n } n void dfsask(int u, int ln, int sm, int par) { n   pair<int, int> X = ask(max(0, l - ln), max(0, r - ln + 1)); n   if (X.first + sm >= 0) ANSA = X.second, ANSB = u; n   for (pair<int, int> p : v[u]) { n     if (p.first != par && !mark[p.first]) n       dfsask(p.first, ln + 1, sm + p.second, u); n   } n } n void solve(int u) { n   dfsS(u); n   MX = SZ[u] + 5; n   prep(); n   u = dfsC(u, SZ[u] / 2); n   add(0, {0, u}); n   mark[u] = 1; n   for (pair<int, int> p : v[u]) { n     if (!mark[p.first]) n       dfsask(p.first, 1, p.second, u), dfsadd(p.first, 1, p.second, u); n   } n   for (pair<int, int> p : v[u]) { n     if (!mark[p.first]) solve(p.first); n   } n } n bool okay(int num) { n   memset(mark, 0, sizeof mark); n   for (int i = 1; i <= n; i++) { n     v[i].clear(); n   } n   for (int i = 0; i < n - 1; i++) { n     v[A[i]].push_back({B[i], C[i] >= ed[num] ? 1 : -1}); n     v[B[i]].push_back({A[i], C[i] >= ed[num] ? 1 : -1}); n   } n   ANSA = ANSB = -1; n   solve(1); n   return ANSA != -1; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cin >> n >> l >> r; n   for (int i = 0; i < n - 1; i++) { n     cin >> A[i] >> B[i] >> C[i]; n     ed.push_back(C[i]); n   } n   sort(ed.begin(), ed.end()); n   int l = 0, r = n - 2, ansa = -1, ansb = -1; n   while (l <= r) { n     int mid = (l + r) / 2; n     if (okay(mid)) n       ansa = ANSA, ansb = ANSB, l = mid + 1; n     else n       r = mid - 1; n   } n   cout << ansa <<     << ansb << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int INF = 1000000007; n int main() { n   int a, b; n   cin >> a >> b; n   long long S = 0; n   for (int e = 1; e < b; e++) { n     long long s1 = ((long long)a * (a + 1) / 2) % INF; n     long long s2 = (s1 * b) % INF; n     s2 = (s2 + a) % INF; n     S = (S + ((s2 * e) % INF)) % INF; n   } n   cout << S; n   return 0; n } n


#include<bits/stdc++.h> n using namespace std; n int main(){ n     int t; n     cin>>t; n     while(t--){ n         int n; n         cin>>n; n         int a[n]; n         int mini; n         int c=0; n         for(int i=0;i<n;i++){ n             cin>>a[i]; n         } n         // for(int i=0;i<n;i++){ n         //     if(a[i]<mini){ n         //         mini=a[i]; n         //     } n        mini=*min_element(a,a+n); n          n         for(int i=0;i<n;i++){ n             if(a[i]!=mini ){ n                 c++; n             } n         } n         cout << c << endl; n     } n     n }


#include <bits/stdc++.h> n  n using namespace std; n #define int long long n bool local=false; n int z1; n int n; n vector <int> a; n mt19937 rnd; n int med(int x,int y,int z) n { n     vector <int> h={x,y,z}; n     sort(h.begin(),h.end()); n     return h[1]; n } n int query(int u,int v,int w) n { n     cout<< ?  <<u+1<<   <<v+1<<   <<w+1<<endl; n     if(!local) n     { n         int ans; n         cin>>ans; n         return ans; n     } n     else n     { n         return med(abs(a[u]-a[v]),abs(a[v]-a[w]),abs(a[w]-a[u])); n     } n } n bool check(int x) n { n     //cout<<n<<  n  <<endl; n     if(x==z1) return false; n     int sum=0; n     for(int i=0;i<18;++i) n     { n         int u=rnd()%n; n         int v=rnd()%n; n         if(u==v || u==z1 || u==x || v==z1 || v==x) {i--;continue;} n         int res1=query(u,v,z1);int res2=query(u,v,x); n         if(res1==(res2+1) || res1==res2) n         { n             ++sum; n         } n     } n     //cout<<  trfe  <<endl; n     return (sum==18); n } n int32_t main() n { n     ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0); n     int t; n     cin>>t; n     while(t--) n     { n         cin>>n; n         a.clear(); n         rnd.seed(4824948+rnd()); n         if(local) n         { n             for(int i=0;i<n;++i) a.push_back(i+1); n             shuffle(a.begin(),a.end(),default_random_engine(rand()+rnd()+6432)); n             if(a[0]>a[1]) n             { n                 for(int i=0;i<n;++i) a[i]=(n+1-a[i]); n             } n         } n         int ans1=1e9; n         vector <int> z; n         for(int i=0;i<200;++i) n         { n             int u=rnd()%n;int v=rnd()%n;int w=rnd()%n; n             if(u!=v && v!=w && w!=u) n             { n                 int r=query(u,v,w); n                 if(r<ans1) n                 { n                     ans1=r; n                     z={u,v,w}; n                 } n             } n         } n         vector <pair<int,int> > v; n         for(int x=0;x<n;++x) n         { n             if(x!=z[1] && x!=z[2]) n             { n                 int res1=query(x,z[1],z[2]); n                 v.push_back({res1,x}); n             } n         } n         sort(v.begin(),v.end());reverse(v.begin(),v.end()); n         int pos1=v[0].second; n         z1=pos1;int z2=(-1); n         int pos2=v[1].second; n         int pos3=v[2].second; n         int pos4=v[3].second; n         //cout<<z[0]<<   <<z[1]<<   <<z[2]<<endl; n         //cout<<pos1<<   <<pos2<<   <<pos3<<   <<pos4<<endl; n         { n             if(check(pos2)) {z2=pos2;} n             else if(check(pos3)) {z2=pos3;} n             else if(check(pos4)) {z2=pos4;} n             else if(check(z[1])) {z2=z[1];} n             else if(check(z[2])) {z2=z[2];} n         } n         if(z2==(-1)) assert(false); n         vector <int> ans(n); n         ans[z1]=1; n         ans[z2]=2; n         for(int i=0;i<n;++i) n         { n             if(i!=z1 && i!=z2) n             { n                 ans[i]=query(z1,z2,i)+2; n             } n         } n         if(ans[0]>ans[1]) for(int i=0;i<n;++i) ans[i]=(n+1-ans[i]); n         if(local) {for(int i=0;i<n;++i) cout<<a[i]<<   ; n         cout<<endl;} n         cout<< !  ; n         for(int i=0;i<n;++i) cout<<ans[i]<<   ; n         cout<<endl; n         int res; n         cin>>res; n         if(res==(-1)) n         { n             exit(0); n         } n     } n     return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int max_n = 20222, mod = 998244353; n int n, k, q, A[max_n], a[max_n]; n int dp[max_n]; n int solve() { n   int q1 = 0, q2 = 1; n   memset(dp, 0, sizeof(dp)); n   dp[0] = 1; n   for (int i = 0; i < n; ++i) { n     for (int j = min(i + 1, k); j >= max(1, k - (n - i - 1)); --j) { n       dp[j] = (dp[j] + 1LL * dp[j - 1] * a[i]) % mod; n     } n   } n   return dp[k]; n } n int main() { n   scanf( %d%d , &n, &k); n   for (int i = 0; i < n; ++i) { n     scanf( %d , &A[i]); n   } n   scanf( %d , &q); n   while (q--) { n     copy(A, A + n, a); n     int tp, x; n     scanf( %d%d , &tp, &x); n     if (tp == 1) { n       int pos, d; n       scanf( %d%d , &pos, &d); n       --pos; n       a[pos] = d; n     } else { n       int l, r, d; n       scanf( %d%d%d , &l, &r, &d); n       --l; n       for (int i = l; i < r; ++i) { n         a[i] += d; n       } n     } n     for (int i = 0; i < n; ++i) { n       a[i] = ((x - a[i]) % mod + mod) % mod; n     } n     printf( %d n , solve()); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct TP {}; n int ar[5][5]; n int ans = 0; n int nums[5]; n bool used[10]; n void cal(int pos) { n   if (pos == 5) { n     int tmp = 0; n     for (int x = 0; x < 5; x++) { n       for (int y = x; y < 4; y += 2) { n         tmp += ar[nums[y]][nums[y + 1]]; n         tmp += ar[nums[y + 1]][nums[y]]; n       } n     } n     ans = max(ans, tmp); n     return; n   } n   for (int i = 0; i < 5; i++) { n     if (!used[i]) { n       nums[pos] = i; n       used[i] = 1; n       cal(pos + 1); n       used[i] = 0; n     } n   } n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   for (int i = 0; i < 5; i++) n     for (int j = 0; j < 5; j++) scanf( %d , &ar[i][j]); n   cal(0); n   printf( %d , ans); n   printf(  n ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main(int argc, char const *argv[]) { n   int x1, y1, x2, y2, res = 0; n   scanf( %d%d%d%d , &x1, &y1, &x2, &y2); n   int n; n   scanf( %d , &n); n   for (int i = 0; i < (int)(n); ++i) { n     long long a, b, c; n     scanf( %lld%lld%lld , &a, &b, &c); n     long long t1 = a * x1 + b * y1 + c; n     long long t2 = a * x2 + b * y2 + c; n     t1 = t1 > 0, t2 = t2 > 0; n     res += t1 != t2; n   } n   printf( %d n , res); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int rup(int x) { n   if ((x % 7) == 0) { n     return x / 7; n   } n   return (x / 7) + 1; n } n int main() { n   int n; n   cin >> n; n   for (int i = 0; i < n; i += 1) { n     int x; n     cin >> x; n     cout << rup(x) <<   n ; n   } 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   int n, d; n   cin >> n >> d; n   int a[n], sum = 0; n   for (int i = 0; i < n; i++) cin >> a[i], sum += a[i]; n   if (sum + 10 * (n - 1) <= d) n     cout << (d - sum) / 5; n   else n     cout << -1; n } n


#include <bits/stdc++.h> n using namespace std; n const long long INF = 1 << 28; n const long long LINF = 1ll << 61; n inline long long getnum() { n   register long long r = 0; n   register bool ng = 0; n   register char c; n   c = getchar(); n   while (c !=  -  && (c <  0  || c >  9 )) c = getchar(); n   if (c ==  - ) ng = 1, c = getchar(); n   while (c !=     && c !=   n ) r = r * 10 + c -  0 , c = getchar(); n   if (ng) r = -r; n   return r; n } n template <class T> n inline void putnum(T x) { n   if (x < 0) putchar( - ), x = -x; n   register short a[20] = {}, sz = 0; n   while (x > 0) a[sz++] = x % 10, x /= 10; n   if (sz == 0) putchar( 0 ); n   for (int i = sz - 1; i >= 0; i--) putchar( 0  + a[i]); n } n inline void putsp() { putchar(   ); } n inline void putendl() { putchar(  n ); } n inline char mygetchar() { n   register char c = getchar(); n   while (c ==     || c ==   n ) c = getchar(); n   return c; n } n int n, k; n int a[511]; n bool dp[511][511]; n vector<int> v; n int main() { n   n = getnum(), k = getnum(); n   for (int i = 1; i <= n; i++) a[i] = getnum(); n   dp[0][0] = 1; n   for (int i = 1; i <= n; i++) { n     for (int t1 = 500; t1 >= 0; t1--) { n       for (int t2 = 500; t2 >= 0; t2--) { n         if (t1 + a[i] <= 500) dp[t1 + a[i]][t2] |= dp[t1][t2]; n         if (t2 + a[i] <= 500) dp[t1][t2 + a[i]] |= dp[t1][t2]; n       } n     } n   } n   for (int i = 0; i <= k; i++) n     if (dp[i][k - i]) v.push_back(i); n   putnum(v.size()), putendl(); n   for (int i = 0; i < v.size(); i++) putnum(v[i]), putsp(); n   return 0; n } n


#include <bits/stdc++.h> n const double pi = 3.1415926535; n using namespace std; n int main() { n   double n, r; n   cin >> n >> r; n   r = sin(pi / n) * r / (1 - sin(pi / n)); n   cout << fixed << setprecision(7) << r; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n class SegmentTree { n  private: n   int n; n   vector<int> st; n   int left(int p) { return (p << 1); } n   int right(int p) { return (p << 1) + 1; } n   int parent(int p) { return (p >> 1); } n   void build(int p, int L, int R) { n     if (L == R) n       st[p] = A[L]; n     else { n       build(left(p), L, (L + R) / 2); n       build(right(p), (L + R) / 2 + 1, R); n       int p1 = st[left(p)], p2 = st[right(p)]; n       st[p] = p1 + p2; n     } n   } n   int rsq(int p, int L, int R, int i, int j) { n     if (i > R || j < L) return 0; n     if (i <= L && R <= j) return st[p]; n     int p1 = rsq(left(p), L, (L + R) / 2, i, j); n     int p2 = rsq(right(p), (L + R) / 2 + 1, R, i, j); n     return p1 + p2; n   } n  n  public: n   vector<int> A; n   SegmentTree() {} n   SegmentTree(const vector<int> &v) { n     A = v; n     n = (int)A.size(); n     st.assign(4 * n, 0); n     build(1, 0, n - 1); n   } n   int query(int i, int j) { return rsq(1, 0, n - 1, i, j); } n   void update(int i, int j, int a) { n     A[i] = a; n     int L = 0, R = n - 1, p = 1; n     while (L < R) { n       int mid = (L + R) / 2; n       if (i <= mid) { n         R = mid; n         p = left(p); n       } else { n         L = mid + 1; n         p = right(p); n       } n     } n     st[p] = a; n     while (p = parent(p)) { n       int p1 = st[left(p)], p2 = st[right(p)]; n       st[p] = p1 + p2; n     } n   } n }; n int a[123456]; n int L[123456]; n int R[123456]; n vector<int> isOn(123456, 1); n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   int n; n   cin >> n; n   for (int i = 0; i < n; i++) cin >> a[i]; n   SegmentTree st(isOn); n   vector<int> v; n   for (int i = 1; i < n; i++) { n     L[i] = i - 1; n     R[i] = i + 1; n     if (a[i] < a[i - 1]) v.push_back(i); n   } n   int cnt = 0; n   while (v.size() > 0) { n     for (int ID : v) { n       st.update(ID, ID, 0); n     } n     vector<int> _new; n     for (int ID : v) { n       int isOk1 = st.query(R[ID], R[ID]); n       int isOk2 = st.query(L[ID], L[ID]); n       if (isOk1 && isOk2) { n         R[L[ID]] = R[ID]; n         L[R[ID]] = L[ID]; n         if (R[ID] < n && a[R[ID]] < a[L[ID]]) { n           _new.push_back(R[ID]); n         } n         continue; n       } n       if (R[ID] < n) { n         int I = R[ID]; n         if (isOk1) { n           int lo = 0, hi = I - 1, mid; n           while (hi - lo > 5) { n             mid = (lo + hi) / 2; n             int foo = st.query(mid, I - 1); n             if (foo > 1) { n               lo = mid + 1; n             } else if (foo == 0) { n               hi = mid - 1; n             } else { n               assert(foo == 1); n               lo = mid; n             } n           } n           int idx = 0; n           for (int i = hi; i >= lo; i--) { n             if (st.query(i, I - 1) == 1) { n               idx = i; n               break; n             } n           } n           L[I] = idx; n           if (a[idx] > a[I]) _new.push_back(I); n         } n       } n       int I = L[ID]; n       if (isOk2) { n         int lo = I + 1, hi = n, mid; n         while (hi - lo > 5) { n           mid = (lo + hi) / 2; n           int foo = st.query(I + 1, mid); n           if (foo > 1) { n             hi = mid - 1; n           } else if (foo == 0) { n             lo = mid + 1; n           } else { n             assert(foo == 1); n             hi = mid; n           } n         } n         int idx = n; n         for (int i = lo; i <= hi; i++) { n           if (st.query(I + 1, i) == 1) { n             idx = i; n             break; n           } n         } n         R[I] = idx; n         if (idx < n && a[idx] < a[I]) _new.push_back(idx); n       } n     } n     v.clear(); n     v = _new; n     assert(is_sorted(v.begin(), v.end())); n     auto it = unique(v.begin(), v.end()); n     v.resize(distance(v.begin(), it)); n     cnt++; n   } n   cout << cnt <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, m; n   long long res = 0; n   cin >> n >> m; n   int v[n]; n   for (int i = 0; i < n; i++) { n     cin >> v[i]; n   } n   for (int i = 0; i < m; i++) { n     int x, y; n     cin >> x >> y; n     res += min(v[x - 1], v[y - 1]); n   } n   cout << res << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void solve() { n   int n; n   cin >> n; n   vector<int> a(n), b(n); n   pair<int, int> p; n   vector<pair<int, int> > v; n   for (int i = 0; i < n; i++) { n     cin >> a[i]; n   } n   iota(b.begin(), b.end(), 0); n   sort(b.begin(), b.end(), [&](int x, int y) { n     if (a[x] != a[y]) { n       return a[x] < a[y]; n     } n     return x < y; n   }); n   for (int i = n - 1; i >= 0; i--) { n     int it = 0; n     for (auto j = b.begin(); j != b.end(); j++) { n       if (*j == i) { n         b.erase(j); n         break; n       } n       it++; n     } n     for (int j = it; j < b.size(); j++) { n       p.first = b[j]; n       p.second = i; n       v.emplace_back(p); n     } n   } n   cout << v.size() <<   n ; n   for (auto i : v) { n     cout << i.first + 1 <<     << i.second + 1 <<   n ; n   } n } n int main() { n   solve(); n   return 0; n } n


#include  bits/stdc++.h  n using namespace std; n  n #define ll long long n #define Rip_classic_loki_for_glorious_purpose ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL) n  n #pragma GCC target ( avx2 ) n #pragma GCC optimization ( O3 ) n #pragma GCC optimization ( unroll-loops ) n  n // need for speed n  n int main() n { n     Rip_classic_loki_for_glorious_purpose;   n     int t; n     cin >> t; n  n     while(t--){ n  n      int n,k,check; n      cin >> n >> k; n  n   int w = 0; n   for(int i = 0;i < n;i++){ n    cout << (i ^ w) << endl; n    cin >> check; n    if(check == 1){ n     break; n    } n    w = i; n   } n     } n     return 0; n } n


#include <bits/stdc++.h> n using namespace std; n bool compare(vector<long long> &A, vector<long long> &B) { n   if (A[0] - A[1] > B[0] - B[1]) return true; n   return false; n } n int main() { n   long long(n), (a), (b); n   scanf( %lld%lld%lld , &(n), &(a), &(b)); n   ; n   vector<vector<long long>> A, B; n   long long sum = 0; n   for (long long i = 0; i < (n); i++) { n     long long(a), (b); n     scanf( %lld%lld , &(a), &(b)); n     ; n     if (a > b) n       B.push_back(vector<long long>{a, b}); n     else n       A.push_back(vector<long long>{a, b}); n     sum += b; n   } n   if (b == 0) { n     printf( %lld n , sum); n     return 0; n   } n   long long val = 1; n   while (a--) val *= 2; n   sort((B).begin(), (B).end(), compare); n   while (B.size() > b) { n     A.push_back(B[B.size() - 1]); n     B.pop_back(); n   } n   long long store = 0; n   for (long long i = 0; i < (B.size()); i++) { n     sum += B[i][0] - B[i][1]; n     if (B.size() == b && i == b - 1) store = B[i][1] - B[i][0]; n   } n   long long ans = sum; n   for (long long i = 0; i < (B.size()); i++) { n     long long temp = sum; n     temp -= B[i][0]; n     temp += B[i][0] * val; n     ans = max(ans, temp); n   } n   for (long long i = 0; i < (A.size()); i++) { n     long long temp = sum + store; n     temp -= A[i][1]; n     temp += A[i][0] * val; n     ans = max(ans, temp); n   } n   printf( %lld n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using std::list; n const int N = 128; n struct car { n   int coord, length, req; n }; n list<car> cars; n int InsertCar(int length, int req, int b, int f) { n   list<car>::iterator i = cars.begin(), j = ++cars.begin(); n   int from, to; n   while (j != cars.end()) { n     from = i->coord + i->length + b; n     to = j->coord - f; n     if (to - from >= length) { n       cars.insert(j, (car){from, length, req}); n       return from; n     } n     ++i, ++j; n   } n   return -1; n } n void DeleteCar(int req) { n   list<car>::iterator i = cars.begin(); n   while (i != cars.end()) { n     if (i->req == req) { n       cars.erase(i); n       break; n     } n     ++i; n   } n } n int main() { n   int l, b, f, n; n   scanf( %d%d%d%d , &l, &b, &f, &n); n   cars.push_back((car){-b, 0, -1}); n   cars.push_back((car){l + f, 0, -1}); n   int type, req; n   for (int i = 0; i < n; ++i) { n     scanf( %d%d , &type, &req); n     if (type == 1) n       printf( %d n , InsertCar(req, i, b, f)); n     else n       DeleteCar(req - 1); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 1e6 + 5; n const int INF = 0x3f3f3f3f; n const long long mod = 1e9 + 7; n int n, k; n int a[MAXN]; n stack<pair<int, int> > s; n int l[MAXN], r[MAXN]; n long long cal(int l, int r) { n   if (l > r || r - l + 1 - k < 0) return 0; n   int m = (r - l + 1 - (k - 1)) / (k - 1); n   long long res = 1LL * (m + 1) * (r - l - k + 2) % mod - n                   1LL * (1 + m) * m / 2 % mod * (k - 1) % mod; n   return res; n } n int main(int argc, char* argv[]) { n   scanf( %d %d , &n, &k); n   for (int i = 1; i <= n; i++) scanf( %d , a + i); n   for (int i = 1; i <= n; i++) { n     if (i == 1) n       s.push(make_pair(a[i], l[i] = 1)); n     else { n       l[i] = i; n       while (!s.empty() && s.top().first < a[i]) { n         l[i] = s.top().second; n         s.pop(); n       } n       s.push(make_pair(a[i], l[i])); n     } n   } n   while (!s.empty()) s.pop(); n   for (int i = n; i >= 1; i--) { n     if (i == n) n       s.push(make_pair(a[i], r[i] = n)); n     else { n       r[i] = i; n       while (!s.empty() && s.top().first <= a[i]) { n         r[i] = s.top().second; n         s.pop(); n       } n       s.push(make_pair(a[i], r[i])); n     } n   } n   long long ans = 0; n   for (int i = 1; i <= n; i++) { n     long long res = cal(l[i], r[i]) - cal(l[i], i - 1) - cal(i + 1, r[i]); n     res = res % mod * a[i] % mod; n     ans = (ans + res) % mod; n   } n   if (ans < 0) ans += mod; n   printf( %lld n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 200000 + 10; n int n; n long long x[N], y[N]; n int main() { n   scanf( %d , &n); n   for (int i = 1; i <= n; i++) { n     scanf( %lld%lld , &x[i], &y[i]); n   } n   while (1) { n     vector<int> r; n     for (int i = 1; i <= n; i++) { n       if ((x[i] + y[i]) % 2 == 0) r.push_back(i); n     } n     if (r.size() == 0 || r.size() == n) { n       for (int i = 1; i <= n; i++) { n         long long tx = x[i]; n         long long ty = y[i]; n         x[i] = tx - ty; n         y[i] = tx + ty; n       } n       if (x[1] % 2 != 0) { n         for (int i = 1; i <= n; i++) { n           x[i]++, y[i]++; n           x[i] /= 2, y[i] /= 2; n         } n       } n       while (1) { n         bool ok = 1; n         for (int i = 1; i <= n; i++) { n           if (x[i] % 2 || y[i] % 2) ok = 0; n         } n         if (ok) { n           for (int i = 1; i <= n; i++) { n             x[i] /= 2, y[i] /= 2; n           } n         } else { n           break; n         } n       } n       continue; n     } n     printf( %d n , r.size()); n     for (auto x : r) { n       printf( %d  , x); n     } n     return 0; n   } n } n


#include <bits/stdc++.h> n using namespace std; n long long inf = 2147483647; n const long long ll = 9223372036854775807, ninf = 1000000000; n const double eps = 1e-6; n const long long nll = 1000000000000000000; n class CircleOrder { n  public: n   string firstOrder(string s) {} n }; n long long C[1005][1005] = {}; n long long dp[1005] = {}; n const int MOD = 998244353; n int main() { n   for (int i = 0; i < 1005; i++) { n     C[i][0] = C[i][i] = 1; n     for (int j = 1; j < i; j++) { n       C[i][j] = C[i - 1][j] + C[i - 1][j - 1]; n       C[i][j] %= MOD; n     } n   } n   int a; n   while (cin >> a) { n     int A[1005] = {}; n     for (int i = 0; i < a; i++) cin >> A[i]; n     dp[a] = 1; n     for (int i = a - 1; i >= 0; i--) { n       if (A[i] <= 0) continue; n       for (int j = i + A[i] + 1; j <= a; j++) { n         dp[i] += (dp[j]) * (C[j - i - 1][A[i]]); n         dp[i] %= MOD; n       } n     } n     long long ans = 0; n     for (int i = 0; i < a; i++) { n       ans += dp[i]; n       ans %= MOD; n     } n     cout << ans <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int mod = 1000000007; n inline int add(int a, int b); n inline int mult(int a, int b); n inline int sub(int a, int b); n inline int divv(int a, int b); n int p1[300500]; n int p2[300500]; n int pow1 = 37; n int pow2 = 41; n int mod1 = 1000000007; n int mod2 = 1000000009; n int32_t main() { n   std::iostream::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   cout << fixed << setprecision(10); n   string s; n   cin >> s; n   int half = s.size() / 2; n   set<char> st; n   for (int i = 0; i < half; i++) st.insert(s[i]); n   if (st.size() <= 1) { n     cout <<  Impossible n ; n     return 0; n   } n   for (int i = 1; i < s.size(); i++) { n     string s1 = s.substr(0, i); n     string s2 = s.substr(i, s.size()); n     string t = s2 + s1; n     string rt = t; n     reverse(rt.begin(), rt.end()); n     if (t != s && t == rt) { n       cout <<  1 n ; n       return 0; n     } n   } n   cout <<  2 n ; n   return 0; n } n int add(int a, int b) { n   int result = a + b; n   if (result >= mod) result -= mod; n   return result; n } n int sub(int a, int b) { n   int result = a - b; n   if (result < 0) result += mod; n   return result; n } n int mult(int a, int b) { return (a * 1ll * b) % mod; } n int bin(int a, int n) { n   int res = 1; n   while (n) { n     if (n & 1) res = mult(res, a); n     a = mult(a, a); n     n /= 2; n   } n   return res; n } n int divv(int a, int b) { return mult(a, bin(b, mod - 2)); } n


#include <bits/stdc++.h> n using namespace std; n const int mod = 1e9 + 7; n const int N = 3e3 + 4; n long long dp[N]; n int main() { n   int n; n   scanf( %d , &n); n   vector<pair<int, int>> v(n); n   for (int i = 0; i < n; i++) n     scanf( %d , &v[i].first), scanf( %d , &v[i].second); n   sort(v.begin(), v.end()); n   dp[n - 1] = v[n - 1].second; n   for (int i = n - 2; i >= 0; i--) { n     dp[i] = v[i].second + dp[i + 1]; n     long long sum = 0; n     for (int j = i; j < n; j++) { n       sum += (v[j].first - v[i].first); n       dp[i] = min(dp[i], sum + dp[j + 1] + v[i].second); n     } n   } n   cout << dp[0]; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n signed main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   long long n; n   cin >> n; n   long long a[n]; n   for (long long i = 0; i < n; i++) cin >> a[i]; n   sort(a, a + n); n   if (a[n - 1] >= a[n - 2] + a[n - 3]) { n     cout <<  NO n ; n     return 0; n   } n   cout <<  YES n ; n   for (long long i = 0; i < n; i += 2) cout << a[i] <<    ; n   for (long long i = n - n % 2 - 1; i >= 0; i -= 2) cout << a[i] <<    ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long int k, d, t; n   cin >> k >> d >> t; n   long long int k1, k2; n   k1 = k; n   k2 = (k / d) * d + (int)(k % d != 0) * d - k; n   t *= 2; n   long long int bal = 2 * k1 + k2; n   long long int n = t / bal; n   double time = n * (k1 + k2); n   double extra = 1 - ((double)n / (double)t) * (double)bal; n   double work_done_by_k1 = 2 * (double)k1 / (double)t; n   double work_done_by_k2 = (double)k2 / (double)t; n   if (extra > work_done_by_k1) { n     time += (k1 + (double)(extra - work_done_by_k1) * (double)t); n   } else { n     time += (double)extra * (double)t / 2; n   } n   printf( %f n , time); 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 a, b, d; n     cin >> a >> b >> d; n     if (d < a) n       cout << d << endl; n     else { n       cout << b + d - (b % d) << endl; n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n int x1, yy1, r1, R1; n int x2, y2, r2, R2; n int dis; n inline bool inside(int r, int R) { n   double dist = sqrt((double)dis); n   if (dist <= double(R - r)) return true; n   return false; n } n inline bool outside(int r, int R) { n   double dist = sqrt((double)dis); n   if (dist >= double(R + r)) return true; n   return false; n } n int main() { n   scanf( %d%d%d%d , &x1, &yy1, &r1, &R1); n   scanf( %d%d%d%d , &x2, &y2, &r2, &R2); n   dis = (x1 - x2) * (x1 - x2) + (yy1 - y2) * (yy1 - y2); n   if (R1 > R2) { n     swap(x1, x2); n     swap(yy1, y2); n     swap(r1, r2); n     swap(R1, R2); n   } n   int ans = 0; n   if (inside(r1, r2) || outside(r1, R2)) ans++; n   if (inside(R1, r2) || outside(R1, R2)) ans++; n   if (inside(R1, r2) || inside(r2, r1) || outside(r2, R1)) ans++; n   if (inside(R1, R2) || outside(R1, R2)) ans++; n   printf( %d , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int T, n, cnta[4], cntb[4], dy[2][2], ans[4040], k; n char a[4040], b[4040]; n int main() { n   dy[0][0] = 0, dy[0][1] = 1, dy[1][0] = 2, dy[1][1] = 3; n   scanf( %d , &T); n   while (T--) { n     scanf( %s %s , a + 1, b + 1); n     n = strlen(a + 1); n     k = 0; n     for (int i = 1; i <= n; ++i) a[i] -=  0 , b[i] -=  0 ; n     cnta[0] = cnta[1] = cnta[2] = cnta[3] = 0; n     for (int i = 2; i <= n; i += 2) ++cnta[dy[a[i - 1]][a[i]]]; n     cntb[0] = cntb[1] = cntb[2] = cntb[3] = 0; n     for (int i = 2; i <= n; i += 2) ++cntb[dy[b[i - 1]][b[i]]]; n     if (cnta[0] != cntb[0] || cnta[3] != cntb[3]) { n       printf( -1 n ); n       continue; n     } n     if (abs(cnta[1] - cnta[2]) >= abs(cntb[1] - cntb[2])) { n       if (cnta[1] != cntb[2]) { n         int now = 0; n         for (int i = 2; i <= n; i += 2) { n           if (dy[a[i - 1]][a[i]] == 1) ++now; n           if (dy[a[i - 1]][a[i]] == 2) --now; n           if (cnta[1] - cnta[2] - 2 * now == cntb[2] - cntb[1]) { n             reverse(a + 1, a + i + 1); n             ans[++k] = i; n             break; n           } n         } n       } n       for (int i = 2; i <= n; i += 2) { n         int p, c = dy[b[n - i + 2]][b[n - i + 1]]; n         for (int j = i; j <= n; j += 2) n           if (dy[a[j - 1]][a[j]] == c) { n             p = j; n             break; n           } n         reverse(a + 1, a + 1 + p - 2), ans[++k] = p - 2; n         reverse(a + 1, a + 1 + p), ans[++k] = p; n       } n       int cnt = 0; n       for (int i = 1; i <= k; ++i) n         if (ans[i]) { n           if (cnt && ans[i] == ans[cnt]) n             --cnt; n           else n             ans[++cnt] = ans[i]; n         } n       printf( %d n , cnt); n       for (int i = 1; i <= cnt; ++i) printf( %d  , ans[i]); n       printf(  n ); n     } else { n       int now = 0; n       for (int i = 2; i <= n; i += 2) { n         if (dy[b[i - 1]][b[i]] == 1) --now; n         if (dy[b[i - 1]][b[i]] == 2) ++now; n         if (cnta[1] - cnta[2] == cntb[2] - cntb[1] - 2 * now) { n           reverse(b + 1, b + i + 1); n           ans[++k] = i; n           break; n         } n       } n       for (int i = 2; i <= n; i += 2) { n         int p, c = dy[a[n - i + 2]][a[n - i + 1]]; n         for (int j = i; j <= n; j += 2) n           if (dy[b[j - 1]][b[j]] == c) { n             p = j; n             break; n           } n         reverse(b + 1, b + 1 + p - 2), ans[++k] = p - 2; n         reverse(b + 1, b + 1 + p), ans[++k] = p; n       } n       reverse(ans + 1, ans + 1 + k); n       int cnt = 0; n       for (int i = 1; i <= k; ++i) n         if (ans[i]) { n           if (cnt && ans[i] == ans[cnt]) n             --cnt; n           else n             ans[++cnt] = ans[i]; n         } n       printf( %d n , cnt); n       for (int i = 1; i <= cnt; ++i) printf( %d  , ans[i]); n       printf(  n ); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const double pi = acos(-1); n vector<int> v; n int main() { n   ios_base::sync_with_stdio(0); n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   ; n   int n, x; n   bool tt = 0; n   cin >> n; n   for (int i = 0; i < n; ++i) { n     cin >> x; n     v.push_back(x); n   } n   for (int i = 1; i < n; ++i) { n     if (v[i] <= v[i - 1]) n       tt = true; n     else if (tt && v[i] > v[i - 1]) n       return cout <<  NO n , 0; n   } n   reverse(v.begin(), v.end()); n   tt = 0; n   for (int i = 1; i < n; ++i) { n     if (v[i] <= v[i - 1]) n       tt = true; n     else if (tt && v[i] > v[i - 1]) n       return cout <<  NO n , 0; n   } n   cout <<  YES n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 1e6; n const int inf = 1e6; n int n, m; n int a[maxn + 5], b[maxn + 5]; n int tag[maxn << 2], mx[maxn << 2]; n void seta(int p, int d) { n   tag[p] += d; n   mx[p] += d; n } n void push(int p) { n   seta(p + p, tag[p]); n   seta(p + p + 1, tag[p]); n   tag[p] = 0; n } n void modify(int p, int l, int r, int ql, int qr, int d) { n   if (l == ql && r == qr) { n     seta(p, d); n     return; n   } n   push(p); n   int mid = (l + r) >> 1; n   if (qr <= mid) n     modify(p + p, l, mid, ql, qr, d); n   else if (mid < ql) n     modify(p + p + 1, mid + 1, r, ql, qr, d); n   else n     modify(p + p, l, mid, ql, mid, d), n         modify(p + p + 1, mid + 1, r, mid + 1, qr, d); n   mx[p] = max(mx[p + p], mx[p + p + 1]); n } n int query(int p, int l, int r) { n   if (l == r) return l; n   push(p); n   int mid = (l + r) >> 1; n   if (mx[p + p + 1] > 0) n     return query(p + p + 1, mid + 1, r); n   else n     return query(p + p, l, mid); n } n int main() { n   scanf( %d %d , &n, &m); n   for (int i = 1; i <= n; i++) { n     scanf( %d , &a[i]); n     modify(1, 1, inf, 1, a[i], 1); n   } n   for (int i = 1; i <= m; i++) { n     scanf( %d , &b[i]); n     modify(1, 1, inf, 1, b[i], -1); n   } n   int q; n   scanf( %d , &q); n   while (q--) { n     int op, i, x; n     scanf( %d %d %d , &op, &i, &x); n     if (op == 1) { n       modify(1, 1, inf, 1, a[i], -1); n       a[i] = x; n       modify(1, 1, inf, 1, a[i], 1); n     } else { n       modify(1, 1, inf, 1, b[i], 1); n       b[i] = x; n       modify(1, 1, inf, 1, b[i], -1); n     } n     if (mx[1] <= 0) n       printf( %d n , -1); n     else n       printf( %d n , query(1, 1, inf)); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 100 + 5; n struct node { n   long long x, y; n   int id; n   bool operator<(const node &ope) const { return y < ope.y; } n } a[maxn]; n bool vis[maxn][maxn]; n long long dp[maxn][maxn]; n bool vis2[maxn]; n inline void init() { n   memset(dp, -1, sizeof(dp)); n   memset(vis, false, sizeof(vis)); n   memset(vis2, false, sizeof(vis2)); n } n void solve(int n, int k) { n   init(); n   dp[0][0] = 0; n   sort(a + 1, a + 1 + n); n   for (int i = 1; i <= n; ++i) { n     int t = ((i) < (k) ? (i) : (k)); n     for (int j = 0; j <= t; ++j) { n       if (dp[i - 1][j] >= 0) dp[i][j] = dp[i - 1][j] + (k - 1) * 1ll * a[i].y; n       if (j > 0 && dp[i - 1][j - 1] >= 0) { n         if (dp[i][j] >= dp[i - 1][j - 1] + a[i].x + (j - 1) * 1ll * a[i].y) n           continue; n         dp[i][j] = dp[i - 1][j - 1] + a[i].x + (j - 1) * 1ll * a[i].y; n         vis[i][j] = true; n       } n     } n   } n } n void print(int n, int k) { n   printf( %d n , k + (n - k) * 2); n   for (int i = n, j = k; i > 0 && j >= 0; --i) { n     if (vis[i][j]) vis2[i] = true, --j; n   } n   int t = 0; n   for (int i = 1; i <= n; ++i) { n     if (!vis2[i]) continue; n     if (++t == k) { n       t = a[i].id; n       break; n     } n     printf( %d  , a[i].id); n   } n   for (int i = 1; i <= n; ++i) { n     if (!vis2[i]) printf( %d %d  , a[i].id, -a[i].id); n   } n   printf( %d n , t); n } n int main() { n   int T; n   scanf( %d , &T); n   while (T--) { n     int n, k; n     scanf( %d%d , &n, &k); n     for (int i = 1; i <= n; ++i) { n       scanf( %lld%lld , &a[i].x, &a[i].y); n       a[i].id = i; n     } n     solve(n, k); n     print(n, k); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int t, n, m, a[105], S; n int main() { n   cin >> t; n   while (t--) { n     cin >> n >> m; n     S = 0; n     for (int i = 1; i <= n; ++i) { n       cin >> a[i]; n       S = S + a[i]; n     } n     if (S == m) n       cout <<  YES ; n     else n       cout <<  NO ; n     cout << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count()); n struct custom_hash { n   static uint64_t splitmix64(uint64_t x) { n     x += 0x9e3779b97f4a7c15; n     x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9; n     x = (x ^ (x >> 27)) * 0x94d049bb133111eb; n     return x ^ (x >> 31); n   } n   size_t operator()(uint64_t x) const { n     static const uint64_t FIXED_RANDOM = n         chrono::steady_clock::now().time_since_epoch().count(); n     return splitmix64(x + FIXED_RANDOM); n   } n }; n void _print() { cout <<  ] n ; } n template <typename T, typename... V> n void _print(T t, V... v) { n   cout << t; n   if (sizeof...(v)) cout <<  ,  ; n   _print(v...); n } n using ll = long long int; n using ld = long double; n template <typename T, typename V> n using P = pair<T, V>; n template <typename T> n using V = vector<T>; n constexpr ll MOD = 1e9 + 7; n constexpr ll INF = 0x3f3f3f3f; n constexpr ld PI = 3.1415926535897932384626; n constexpr ld EPS = 1e-9; n constexpr ll MAX = 1e5 + 5; n void solve() { n   ll n; n   cin >> n; n   V<ll> a(n + 1); n   for (ll i = (ll)(1); i < (ll)(n + 1); i++) cin >> a[i]; n   if (a[1]) { n     cout << n + 1 <<    ; n     for (ll i = (ll)(1); i < (ll)(n + 1); i++) cout << i <<    n [i == n]; n     return; n   } else if (!a[n]) { n     for (ll i = (ll)(1); i < (ll)(n + 1); i++) cout << i <<    ; n     cout << n + 1 <<   n ; n     return; n   } n   for (ll i = (ll)(1); i < (ll)(n); i++) n     if (!a[i] && a[i + 1]) { n       for (ll j = (ll)(1); j < (ll)(i + 1); j++) cout << j <<    ; n       cout << n + 1 <<    ; n       for (ll j = (ll)(i + 1); j < (ll)(n + 1); j++) cout << j <<    n [j == n]; n       return; n     } n   cout <<  -1 n ; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(nullptr); n   ll t = 1; n   cin >> t; n   for (ll i = (ll)(1); i < (ll)(t + 1); i++) solve(); n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 2e5 + 5; n struct line { n   int sx, sy, ex, ey; n   friend bool operator<(const line &A, const line &B) { return A.sx < B.sx; } n }; n map<char, line> mp; n int n, m; n char s[2005][2005], p[2005][2005], mxx; n void init() { n   for (char x =  a ; x <=  z ; ++x) { n     mp[x].sx = 0, mp[x].sy = 0; n     mp[x].ex = 0, mp[x].ey = 0; n   } n   for (int i = 1; i <= n; ++i) { n     for (int j = 1; j <= m; ++j) { n       p[i][j] =  . ; n     } n   } n } n bool check() { n   for (char x =  a ; x <= mxx; ++x) { n     if (mp[x].sx != mp[x].ex and mp[x].sy != mp[x].ey) { n       return false; n     } n   } n   for (int i = 1; i <= n; ++i) { n     for (int j = 1; j <= m; ++j) { n       if (s[i][j] != p[i][j]) return false; n     } n   } n   return true; n } n void slove() { n   cin >> n >> m; n   mxx = 0; n   init(); n   for (int i = 1; i <= n; ++i) { n     for (int j = 1; j <= m; ++j) { n       cin >> s[i][j]; n       mxx = max(mxx, s[i][j]); n       if (mp[s[i][j]].sx == 0) { n         mp[s[i][j]].sx = i; n         mp[s[i][j]].sy = j; n         mp[s[i][j]].ex = i; n         mp[s[i][j]].ey = j; n       } else { n         mp[s[i][j]].ex = i; n         mp[s[i][j]].ey = j; n       } n     } n   } n   for (char x =  a ; x <= mxx; ++x) { n     if (mp[x].sx == 0) { n       mp[x] = mp[mxx]; n     } n     for (int xx = mp[x].sx; xx <= mp[x].ex; ++xx) { n       for (int yy = mp[x].sy; yy <= mp[x].ey; ++yy) { n         p[xx][yy] = x; n       } n     } n   } n   if (!check()) { n     cout <<  NO  << endl; n     return; n   } n   cout <<  YES  << endl; n   cout << max(0, mxx -  a  + 1) << endl; n   for (char x =  a ; x <= mxx; ++x) { n     cout << mp[x].sx <<     << mp[x].sy <<    ; n     cout << mp[x].ex <<     << mp[x].ey << endl; n   } n   return; n } n signed main() { n   ios ::sync_with_stdio(false); n   int T; n   cin >> T; n   while (T--) { n     slove(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct disjoint_set { n   vector<int> p; n   disjoint_set(int n) : p(n, -1) {} n   bool share(int a, int b) { return root(a) == root(b); } n   int sz(int u) { return -p[root(u)]; } n   int root(int u) { return p[u] < 0 ? u : p[u] = root(p[u]); } n   bool merge(int u, int v) { n     u = root(u), v = root(v); n     if (u == v) return false; n     p[u] += p[v], p[v] = u; n     return true; n   } n }; n int main() { n   cin.tie(0)->sync_with_stdio(0); n   int n; n   string s; n   cin >> n >> s; n   vector<array<int, 2>> dp(n + 1); n   vector<int> reach(n); n   for (auto i = n - 1; i >= 0; --i) { n     if (s[i] !=  1 ) { n       dp[i][0] = dp[i + 1][0] + 1; n     } n     if (s[i] !=  0 ) { n       dp[i][1] = dp[i + 1][1] + 1; n     } n     reach[i] = max(dp[i][0], dp[i][1]); n   } n   disjoint_set dsu(n + 1); n   for (auto l = 1; l <= n; ++l) { n     int res = 0; n     for (auto i = 0; i < n;) { n       if (reach[i] < l) { n         dsu.merge(i + 1, i); n         i = dsu.root(i); n       } else { n         res += reach[i] / l; n         i += reach[i] / l * l; n       } n     } n     cout << res <<    ; n   } n   cout <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n char s[1001][7]; n int n, a[30], p, m, ch, j, mn, mx, val, o; n long long sum; n struct chestie { n   int c, fr, ok, nr; n } v[30]; n int pow(int N, int E); n bool comp(chestie X, chestie Y); n int main() { n   cin >> n; n   for (int i = 1; i <= n; i++) { n     cin.get(); n     cin >> s[i]; n     m = strlen(s[i]); n     for (int j = 0; j < m; j++) { n       ch = s[i][j] -  a ; n       if (ch > mx) mx = ch; n       if (v[ch].fr == 0) o++; n       v[ch].fr += (pow(10, (m - j - 1))); n       v[ch].c = ch; n       if (j == 0 && m >= 1) v[ch].ok = 1; n     } n   } n   sort(v + 0, v + mx + 1, comp); n   val = 1; n   j = 1; n   for (int i = 0; i < o; i++) { n     if (v[i].ok || j == 0) { n       v[i].nr = val; n       sum += (v[i].fr * val); n       val++; n     } else if (j) n       v[i].nr = 0, j = 0, a[v[i].c] = 0; n   } n   cout << sum; n   return 0; n } n int pow(int N, int E) { n   int R = 1; n   while (E) R = R * N, E--; n   return R; n } n bool comp(chestie X, chestie Y) { n   if (X.fr > Y.fr) return 1; n   if (X.fr < Y.fr) return 0; n   if (X.c < Y.c) return 1; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long read() { n   long long f = 1, x = 0; n   char s = getchar(); n   while (s <  0  || s >  9 ) { n     if (s ==  - ) f = -f; n     s = getchar(); n   } n   while (s >=  0  && s <=  9 ) { n     x = x * 10 + (s -  0 ); n     s = getchar(); n   } n   return f * x; n } n int n, m, i, j, s, o, k; n vector<int> g[500005]; n int cnt; n vector<int> bu[500005]; n int bl[500005], rk[500005]; n int vv[500005]; n int d[500005], dpd[500005]; n void dfs0(int x, int ff) { n   d[x] = d[ff] + 1; n   dpd[x] = 0x3f3f3f3f; n   for (int i = 0; i < (int)g[x].size(); i++) { n     int y = g[x][i]; n     if (y != ff) { n       if (!d[y]) { n         dfs0(y, x); n         dpd[x] = min(dpd[x], dpd[y]); n       } else { n         dpd[x] = min(dpd[x], d[y]); n       } n     } n   } n   if (dpd[x] > d[x]) n     vv[x] = -1; n   else n     vv[x] = 1; n   return; n } n void dfs00(int x, int ff) { n   if (dpd[x] == d[x]) { n     bu[++cnt].push_back(x); n     rk[x] = 0; n     bl[x] = cnt; n   } else if (dpd[x] < d[x]) { n     bl[x] = bl[ff]; n     bu[bl[x]].push_back(x); n     rk[x] = (int)bu[bl[x]].size() - 1; n   } n   for (int i = 0; i < (int)g[x].size(); i++) { n     int y = g[x][i]; n     if (y != ff && d[y] == d[x] + 1) { n       dfs00(y, x); n     } n   } n   return; n } n int dp1[500005], dp2[500005], dp0[500005]; n vector<int> sm[500005]; n void dfs(int x, int ff, int op) { n   if (!op) { n     dp1[x] = 0; n     int nw = 0; n     for (int i = 0; i < (int)g[x].size(); i++) { n       int y = g[x][i]; n       sm[x].push_back(nw); n       if (y != ff) { n         dfs(y, x, vv[y] == 1 ? 1 : 0); n         dp1[x] = max(dp1[x], dp1[y] + 1); n         nw = max(nw, dp1[y] + 1); n       } n     } n     return; n   } n   int id = bl[x], le = (int)bu[id].size(); n   int md = rk[x], ll = (md + le - (le / 2)) % le; n   for (int ii = 0; ii < le; ii++) { n     int t = bu[id][ii], nw = 0; n     for (int i = 0; i < (int)g[t].size(); i++) { n       int y = g[t][i]; n       sm[t].push_back(nw); n       if (y != ff && bl[y] != id) { n         dfs(y, t, vv[y] == 1 ? 1 : 0); n         dp0[t] = max(dp0[t], dp1[y] + 1); n         nw = max(nw, dp1[y] + 1); n       } n     } n     dp1[t] = dp0[t]; n   } n   for (int ii = (ll + 1) % le; ii != (md + 1) % le; ii = (ii + 1) % le) { n     int pr = bu[id][(ii + le - 1) % le], t = bu[id][ii]; n     dp1[t] = max(dp1[t], dp1[pr] + 1); n   } n   for (int ii = (ll + le - 1) % le; ii != (md + le - 1) % le; n        ii = (ii + le - 1) % le) { n     int pr = bu[id][(ii + 1) % le], t = bu[id][ii]; n     if (ii != (ll + le - 1) % le) { n       dp1[t] = max(dp1[t], dp1[pr] + 1); n     } n   } n   return; n } n int as[500005]; n int q[500005], hd, tl; n int ds(int x, int y, int le) { return (y + le - x) % le; } n void dfs2(int x, int ff, int op, int fdp) { n   if (!op) { n     dp2[x] = fdp + 1; n     as[x] = max(dp2[x], dp1[x]); n     int nw = 0; n     for (int i = (int)g[x].size() - 1; i >= 0; i--) { n       int y = g[x][i]; n       if (y != ff) { n         dfs2(y, x, vv[y] == 1 ? 1 : 0, max(dp2[x], max(sm[x][i], nw))); n         nw = max(nw, dp1[y] + 1); n       } n     } n     return; n   } n   dp0[x] = max(dp0[x], fdp + 1); n   int id = bl[x], le = (int)bu[id].size(); n   hd = 1; n   tl = 0; n   int ll2 = (le - (le / 2)) % le; n   for (int i = ll2; i != 0; i = (i + 1) % le) { n     int t = bu[id][i]; n     while (tl >= hd && dp0[bu[id][q[tl]]] + ds(q[tl], i, le) < dp0[t]) tl--; n     q[++tl] = i; n   } n   for (int i = 0; i < le; i++, ll2 = (ll2 + 1) % le) { n     int t = bu[id][i]; n     if (tl >= hd) dp2[t] = max(dp2[t], dp0[bu[id][q[hd]]] + ds(q[hd], i, le)); n     while (tl >= hd && dp0[bu[id][q[tl]]] + ds(q[tl], i, le) < dp0[t]) tl--; n     q[++tl] = i; n     if (tl >= hd && q[hd] == ll2) hd++; n   } n   ll2 = (le - 1 + le / 2) % le; n   hd = 1; n   tl = 0; n   for (int i = ll2; i != le - 1; i = (i + le - 1) % le) { n     int t = bu[id][i]; n     while (tl >= hd && dp0[bu[id][q[tl]]] + ds(i, q[tl], le) < dp0[t]) tl--; n     q[++tl] = i; n   } n   for (int i = le - 1; i >= 0; i--, ll2 = (ll2 + le - 1) % le) { n     int t = bu[id][i]; n     if (tl >= hd) dp2[t] = max(dp2[t], dp0[bu[id][q[hd]]] + ds(i, q[hd], le)); n     while (tl >= hd && dp0[bu[id][q[tl]]] + ds(i, q[tl], le) < dp0[t]) tl--; n     q[++tl] = i; n     if (tl >= hd && q[hd] == ll2) hd++; n   } n   for (int ii = 0; ii < le; ii++) { n     int t = bu[id][ii], nw = 0; n     as[t] = max(dp2[t], dp0[t]); n     for (int i = (int)g[t].size() - 1; i >= 0; i--) { n       int y = g[t][i], dpp = dp2[t]; n       if (t == x) dpp = max(dpp, fdp + 1); n       if (y != ff && bl[y] != id) { n         dfs2(y, t, vv[y] == 1 ? 1 : 0, max(dpp, max(nw, sm[t][i]))); n         nw = max(nw, dp1[y] + 1); n       } n     } n   } n   return; n } n int main() { n   n = read(); n   m = read(); n   for (int i = 1; i <= m; i++) { n     s = read(); n     o = read(); n     g[s].push_back(o); n     g[o].push_back(s); n   } n   dfs0(1, 0); n   cnt = 0; n   dfs00(1, 0); n   dfs(1, 0, vv[1] == 1 ? 1 : 0); n   dfs2(1, 0, vv[1] == 1 ? 1 : 0, -1); n   for (int i = 1; i <= n; i++) { n     printf( %d  , as[i]); n   } n   putchar(  n ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int N; n long long gMin = 1e12, gMax = -1e12, rMin = 1e12, rMax = -1e12, bMin = 1e12, n           bMax = -1e12, lasty = -1e12, ret = 0; n vector<long long> notGreen[2]; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout << fixed << setprecision(18); n   cin >> N; n   for (int i = 0; i <= N; i++) { n     long long x = 1e12; n     char c =  G ; n     if (i < N) cin >> x >> c; n     if (c ==  G ) { n       if (i < N) { n         gMin = min(gMin, x); n         gMax = max(gMax, x); n       } n       long long add1 = x - lasty, add2 = add1 * 2; n       if (add1 >= 1e10) add1 = 0; n       for (int j = 0; j < 2; j++) { n         if (notGreen[j].size() == 0) continue; n         add1 += x - lasty; n         long long maxi = n             max(notGreen[j].front() - lasty, x - notGreen[j].back()); n         for (int k = 0; k < notGreen[j].size() - 1; k++) n           maxi = max(maxi, notGreen[j][k + 1] - notGreen[j][k]); n         add1 -= maxi; n         notGreen[j] = vector<long long>(); n       } n       ret += min(add1, add2); n       lasty = x; n     } else if (c ==  R ) { n       notGreen[0].push_back(x); n       rMin = min(rMin, x); n       rMax = max(rMax, x); n     } else { n       notGreen[1].push_back(x); n       bMin = min(bMin, x); n       bMax = max(bMax, x); n     } n   } n   if (gMin > gMax) ret = max(0ll, rMax - rMin) + max(0ll, bMax - bMin); n   cout << ret <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct node { n   int x, y; n }; n int n, m, d[5005]; n node a[5005]; n bool cmp(const node &x, const node &y) { n   return x.x == y.x ? x.y < y.y : x.x < y.x; n } n int main() { n   scanf( %d , &n); n   for (int i = 1; i <= n; i++) scanf( %d%d , &a[i].x, &a[i].y); n   sort(a + 1, a + 1 + n, cmp); n   memset(d, 0, sizeof(d)); n   int ans = min(a[1].x, a[1].y); n   for (int i = 2; i <= n; i++) n     if (ans > a[i].y) n       ans = a[i].x; n     else n       ans = a[i].y; n   printf( %d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void __print(int x) { cerr << x; } n void __print(long x) { cerr << x; } n void __print(long long x) { cerr << x; } n void __print(unsigned x) { cerr << x; } n void __print(unsigned long x) { cerr << x; } n void __print(unsigned long long x) { cerr << x; } n void __print(float x) { cerr << x; } n void __print(double x) { cerr << x; } n void __print(long double x) { cerr << x; } n void __print(char x) { cerr <<      << x <<     ; } n void __print(const char *x) { cerr <<     << x <<    ; } n void __print(const string &x) { cerr <<     << x <<    ; } n void __print(bool x) { cerr << (x ?  true  :  false ); } n template <typename T, typename V> n void __print(const pair<T, V> &x) { n   cerr <<  { ; n   __print(x.first); n   cerr <<  , ; n   __print(x.second); n   cerr <<  } ; n } n template <typename T> n void __print(const T &x) { n   int f = 0; n   cerr <<  { ; n   for (auto &i : x) cerr << (f++ ?  ,  :   ), __print(i); n   cerr <<  } ; n } n void _print() { cerr <<  ] n ; } n template <typename T, typename... V> n void _print(T t, V... v) { n   __print(t); n   if (sizeof...(v)) cerr <<  ,  ; n   _print(v...); n } n template <typename T> n void Print(T a[], T n) { n   for (int i = 0; i < n; i++) { n     cout << a[i] <<    ; n   } n   cout << endl; n } n void solve() { n   int n; n   cin >> n; n   string s, t; n   cin >> s >> t; n   if (s == t) { n     cout << 0 <<   n ; n     return; n   } n   int same1 = 0, diff1 = 0, same = 0; n   for (int i = 0; i < n; i++) { n     if (s[i] == t[i]) { n       same++; n       if (s[i] ==  1 ) same1++; n     } else { n       if (s[i] ==  1 ) diff1++; n     } n   } n   int diff = n - same; n   if (same == n) { n     cout << 0 <<   n ; n     return; n   } n   int ans = INT_MAX; n   if (same1 - 1 == same - same1) { n     ans = min(ans, same); n   } n   if (diff1 == diff - diff1) { n     ans = min(ans, diff); n   } n   if (ans != INT_MAX) n     cout << ans <<   n ; n   else n     cout << -1 <<   n ; n } n void solve1() { n   int n; n   cin >> n; n   string s, t; n   cin >> s >> t; n   if (s == t) { n     cout << 0 <<   n ; n     return; n   } n   int same1 = 0, diff1 = 0, same = 0; n   for (int i = 0; i < n; i++) { n     if (s[i] == t[i]) { n       same++; n       if (s[i] ==  1 ) same1++; n     } else { n       if (s[i] ==  1 ) diff1++; n     } n   } n   int diff = n - same; n   int ans = INT_MAX; n   if (diff % 2 == 0) { n     int one = (diff + 1) / 2; n     if (diff1 == one) { n       ans = min(ans, diff); n     } n   } n   if (same % 2 != 0) { n     int one = (same + 1) / 2; n     if (same1 == one) { n       ans = min(ans, same); n     } n   } n   if (ans == INT_MAX) n     cout << -1 <<   n ; n   else n     cout << ans <<   n ; n } n signed main() { n   ios_base::sync_with_stdio(false), cin.tie(NULL); n   int t; n   cin >> t; n   while (t--) solve1(); n } n


#include <bits/stdc++.h> n using namespace std; n const double eps = 1e-9; n double sqrDist(double x1, double y1, double x2, double y2) { n   return (x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2); n } n double dist(double x1, double y1, double x2, double y2) { n   return sqrt(sqrDist(x1, y1, x2, y2)); n } n const double PI = acos(-1.0); n int main() { n   int x0, y0, v, t; n   scanf( %d%d%d%d , &x0, &y0, &v, &t); n   double r0 = 1.0 * v * t; n   int n; n   scanf( %d , &n); n   vector<pair<double, int> > a; n   for (int i = 0; i < n; i++) { n     int x, y, r; n     scanf( %d%d%d , &x, &y, &r); n     double d = sqrDist(x, y, x0, y0); n     if (d < 1.0 * r * r + eps) { n       printf( %.11f , 1.0); n       return 0; n     } n     d = sqrt(d); n     if (r + r0 < d - eps) { n       continue; n     } n     double angL, angR, ang; n     double angM = atan2(y - y0, x - x0); n     if (angM < 0) { n       angM += 2 * PI; n     } n     double tLen = sqrt(d * d - 1.0 * r * r); n     if (tLen < r0 + eps) { n       ang = asin(r / d); n     } else { n       ang = acos((d * d + r0 * r0 - 1.0 * r * r) / (2 * d * r0)); n     } n     angL = angM - ang; n     angR = angM + ang; n     if (angL < 0) { n       a.push_back(make_pair(angL + 2 * PI, 1)); n       a.push_back(make_pair(2 * PI, -1)); n       a.push_back(make_pair(0.0, 1)); n       a.push_back(make_pair(angR, -1)); n     } else if (angR > 2 * PI) { n       a.push_back(make_pair(angL, 1)); n       a.push_back(make_pair(2 * PI, -1)); n       a.push_back(make_pair(0.0, 1)); n       a.push_back(make_pair(angR - 2 * PI, -1)); n     } else { n       a.push_back(make_pair(angL, 1)); n       a.push_back(make_pair(angR, -1)); n     } n   } n   sort(a.begin(), a.end()); n   double last = 0; n   int c = 0; n   double ans = 0; n   for (auto& p : a) { n     if (c > 0) { n       ans += p.first - last; n     } n     c += p.second; n     last = p.first; n   } n   ans /= 2 * PI; n   printf( %.11f , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<int> r1, r2; n vector<bool> pd; n vector<int> out; n vector<pair<int, int>> mp; n int le(int i, int cur) { n   if (cur == mp[i].first) return mp[i].second; n   return mp[i].first; n } n bool mak(int i, int p) { n   if (mp[i].first == -1) n     mp[i].first = p; n   else if (mp[i].second == -1) n     mp[i].second = p; n   else n     return true; n   return false; n } n class proc { n   struct Dt { n     int pos; n     bool fl; n   }; n   int p; n   int c; n   vector<Dt> out_c; n  n  public: n   proc(int i) { n     p = i; n     c = 0; n     re(); n     if (c > out_c.size() / 2) { n       for (auto& i : out_c) n         if (!i.fl) out.push_back(i.pos); n     } else n       for (auto& i : out_c) n         if (i.fl) out.push_back(i.pos); n   } n  n  private: n   void re() { n     if (pd[p]) return; n     pd[p] = 1; n     if (r1[p] == r2[p]) return; n     int x = le(r1[p], p); n     if (r1[p] == r1[x]) { n       ++c; n       swap(r1[x], r2[x]); n       out_c.push_back({x, 1}); n     } else n       out_c.push_back({x, 0}); n     p = x; n     re(); n   } n }; n int main() { n   ios_base::sync_with_stdio(false); n   int t; n   cin >> t; n   while (t--) { n     int n; n     cin >> n; n     r1.resize(n); n     r2.resize(n); n     mp = vector<pair<int, int>>(n, {-1, -1}); n     pd = vector<bool>(n, 0); n     out = vector<int>(0); n     for (int i = 0; i < n; ++i) { n       cin >> r1[i]; n       --r1[i]; n     } n     for (int i = 0; i < n; ++i) { n       cin >> r2[i]; n       --r2[i]; n     } n     for (int i = 0; i < n; ++i) n       if (mak(r1[i], i) || mak(r2[i], i)) goto badcase; n     for (int i = 0; i < n; ++i) proc lp(i); n     cout << out.size() << endl; n     for (auto i : out) cout << i + 1 <<    ; n     cout << endl; n     continue; n   badcase: n     cout << -1 << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n vector<long long> readvec(long long length, long long off = 0); n template <typename T> n string ts(T v); n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   long long n; n   long long z; n   cin >> n >> z; n   vector<long long> v = readvec(n); n   sort(v.begin(), v.end()); n   long long left = 0; n   long long right = v.size() / 2; n   long long ans = 0; n   while (true) { n     if (left >= v.size() / 2 || right >= v.size()) break; n     if (v[right] - v[left] >= z) { n       ans++; n       left++; n       right++; n     } else { n       right++; n     } n   } n   cout << ans << endl; n } n template <typename T> n string ts(T v) { n   stringstream ss; n   ss <<  [ ; n   for (auto it = v.begin(); it != v.end(); it++) { n     if (it != v.begin()) ss <<  ,  ; n     ss << *it; n   } n   ss <<  ] ; n   return ss.str(); n } n vector<long long> readvec(long long length, long long off) { n   vector<long long> v; n   for (long long i = 0; i < length; i++) { n     long long x; n     cin >> x; n     v.push_back(x + off); n   } n   return v; n } n


#include <bits/stdc++.h> n using namespace std; n int arr[2505][2505], sum[2505][2505], n, m; n int get_sum(int x1, int y1, int x2, int y2) { n   x2 = min(x2, n); n   y2 = min(y2, m); n   assert(x1 <= x2); n   assert(y1 <= y2); n   return sum[x2][y2] - sum[x2][y1 - 1] - sum[x1 - 1][y2] + sum[x1 - 1][y1 - 1]; n } n int main() { n   ios::sync_with_stdio(false); n   ; n   cin.tie(0); n   ; n   int i, j, k, ans = 100000000, ones, zeroes; n   string s; n   cin >> n >> m; n   for (i = 1; i <= n; i++) { n     cin >> s; n     s =     + s; n     for (j = 1; j <= m; j++) arr[i][j] = s[j] -  0 ; n   } n   for (i = 1; i <= n; i++) { n     for (j = 1; j <= m; j++) n       sum[i][j] = sum[i - 1][j] + sum[i][j - 1] - sum[i - 1][j - 1] + arr[i][j]; n   } n   for (k = 2; k <= max(n, m); k++) { n     int got = 0; n     for (i = 1; i <= n; i = i + k) { n       for (j = 1; j <= m; j = j + k) { n         ones = get_sum(i, j, i + k - 1, j + k - 1); n         zeroes = k * k - ones; n         got += min(ones, zeroes); n       } n     } n     ans = min(ans, got); n   } n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MOD = 1e9 + 7; n int main() { n   int n; n   string s, ss; n   cin >> s >> n; n   vector<pair<int, string> > v(n + 1); n   v[0] = make_pair(0, s); n   char next; n   while (cin.get(next)) n     if (next ==   n ) break; n   for (int i = 1; i <= n; ++i) { n     getline(cin, ss); n     if (ss.length() > 3) n       v[i] = make_pair(ss[0] -  0 , ss.substr(3)); n     else n       v[i] = make_pair(ss[0] -  0 ,   ); n   } n   long long val[10], len[10]; n   for (int i = 0; i < 10; ++i) val[i] = i, len[i] = 10; n   for (int i = n; i >= 0; --i) { n     int cu = v[i].first; n     string s = v[i].second; n     if (s.empty()) { n       val[cu] = 0; n       len[cu] = 1; n       continue; n     } n     long long pow = 1; n     long long va = 0; n     for (int j = s.length() - 1; j >= 0; --j) { n       va += ((val[(s[j] -  0 )]) * pow) % MOD; n       va %= MOD; n       pow = (pow * len[(s[j] -  0 )]) % MOD; n     } n     len[cu] = pow; n     val[cu] = va; n   } n   cout << val[0] << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = (int)2e5 + 5; n int n, a[maxn], vis[2 * maxn], mx[maxn]; n long long ans; n vector<int> slist[maxn]; n int gcd(int a, int b) { return b ? gcd(b, a % b) : a; } n void doit(int d) { n   for (int i = 0; i < d; i++) mx[i] = 0; n   for (int i = 0; i < n; i++) vis[i] = 0; n   for (int i = 0; i < n; i++) mx[i % d] = max(mx[i % d], a[i]); n   for (int i = 0; i < n; i++) n     if (a[i] == mx[i % d]) vis[i] = 1; n   for (int i = n; i < 2 * n; i++) vis[i] = vis[i - n]; n   int lst = -1; n   for (int i = 2 * n - 1; i >= 0; i--) { n     if (vis[i] && lst == -1) n       lst = i; n     else if (!vis[i]) n       lst = -1; n     if (lst == -1 || i >= n) continue; n     int len = lst - i + 1; n     int idx = n         upper_bound(slist[d].begin(), slist[d].end(), len) - slist[d].begin(); n     ans += idx; n   } n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.clear(); n   cin >> n; n   for (int i = 0; i < n; i++) cin >> a[i]; n   for (int i = 1; i < n; i++) slist[gcd(i, n)].push_back(i); n   for (int i = 1; i * i <= n; i++) { n     if (n % i) continue; n     doit(i); n     if (n / i != i) doit(n / i); n   } n   cout << ans; n } n


#include <bits/stdc++.h> n using namespace std; n vector<int> G[200005]; n bool V[200006] = {0}; n int S[200006] = {0}; n int n; n long long d = 0, mx = 0; n void dfs(int p) { n   V[p] = 1; n   d++; n   int u = 0; n   for (int i = 0; i < G[p].size(); i++) { n     int cp = G[p][i]; n     if (!V[cp]) { n       dfs(cp); n       u++; n     } n   } n   if (!u) mx = max(mx, d); n   d--; n } n int main() { n   cin >> n; n   for (int i = 1; i <= n; i++) { n     int x; n     cin >> x; n     S[i] = x; n     if (x == -1) continue; n     G[x].push_back(i); n     G[i].push_back(x); n   } n   for (int i = 1; i <= n; i++) { n     if (!V[i] && S[i] == -1) { n       d = 0; n       dfs(i); n     } n   } n   cout << mx; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long int a[1000000]; n struct cr { n   int l; n   int r; n   int d; n } op[1000000]; n long long int kt[1000000]; n long long int ktt[1000000]; n long long int fiop[1000000]; n long long int vp[1000000]; n long long int vpp[1000000]; n long long int mba[1000000]; n int main() { n   int n, m, k, b, c, i, j; n   long long int uv; n   cin >> n >> m >> k; n   for (i = 1; i <= n; i++) { n     scanf( %d , &a[i]); n   } n   for (i = 1; i <= m; i++) { n     cin >> op[i].l >> op[i].r >> op[i].d; n   } n   for (i = 0; i < k; i++) { n     cin >> b >> c; n     kt[b]++; n     ktt[c]--; n   } n   b = 0; n   for (i = 1; i <= m; i++) { n     b += kt[i]; n     fiop[i] += b; n     b += ktt[i]; n   } n   uv = 0; n   for (i = 1; i <= m; i++) { n     uv = (op[i].d * fiop[i]); n     vp[op[i].l] += uv; n     vpp[op[i].r] -= uv; n   } n   uv = 0; n   for (i = 0; i <= 100010; i++) { n     uv += vp[i]; n     mba[i] += uv; n     uv += vpp[i]; n   } n   for (i = 1; i <= n; i++) { n     a[i] += mba[i]; n     cout << a[i] <<    ; n   } n   cout << endl; n } n


#include <bits/stdc++.h> n using namespace std; n using ll = long long; n using vi = vector<int>; n using db = double; n const int mxn = 100006; n int n, m; n int a[mxn]; n vi e[mxn]; n bool v[mxn]; n vi V; n void dfsv(int x, int p) { n   v[x] = true; n   V.push_back(x); n   for (auto i : e[x]) n     if (i != p && !v[i]) dfsv(i, x); n } n bool v1[mxn]; n void dfs1(int x, int p) { n   v1[x] = true; n   for (auto i : e[x]) n     if (i != p) n       if (v1[i]) n         throw i; n       else n         dfs1(i, x); n } n bool v2[mxn]; n bool dfs2(int x, int p, int o) { n   v2[x] = true; n   for (auto i : e[x]) n     if (i != p) n       if (i == o) { n         a[x] = 1; n         return true; n       } else if (!v2[i]) { n         if (dfs2(i, x, o)) { n           a[x] = 1; n           return true; n         } n       } n   return false; n } n vector<vi> c; n void dfs3(int x, int p) { n   c.back().push_back(x); n   for (auto i : e[x]) n     if (i != p) dfs3(i, x); n } n void solve(int x) { n   V = {}; n   dfsv(x, 0); n   int o = 0; n   try { n     dfs1(x, 0); n   } catch (int x) { n     o = x; n   } n   if (o) { n     dfs2(o, 0, o); n   } else { n     int leaf = 0; n     for (auto i : V) n       if (e[i].size() == 1) ++leaf; n     if (leaf <= 2) return; n     if (leaf >= 4) { n       for (auto i : V) n         if (e[i].size() == 1) n           a[i] = 1; n         else n           a[i] = 2; n     } else { n       int r = 0; n       for (auto i : V) n         if (e[i].size() == 3) r = i; n       assert(r); n       c = {}; n       for (auto i : e[r]) { n         c.push_back({r}); n         dfs3(i, r); n       } n       vi k; n       for (auto i : c) k.push_back(min(6, (int)i.size())); n       if (k[0] * k[1] + k[0] * k[2] + k[1] * k[2] <= k[0] * k[1] * k[2]) { n         const int s = 60; n         a[r] = s; n         for (int i = 0; i < 3; ++i) n           for (int j = 0; j < k[i]; ++j) a[c[i][j]] = s / k[i] * (k[i] - j); n       } n     } n   } n } n int main() { n   int t; n   cin >> t; n   while (t--) { n     scanf( %d%d , &n, &m); n     for (int i = 1; i <= n; ++i) e[i] = {}; n     for (int i = 0; i < m; ++i) { n       int x, y; n       scanf( %d%d , &x, &y); n       e[x].push_back(y); n       e[y].push_back(x); n     } n     fill(v, v + n + 1, false); n     fill(v1, v1 + n + 1, false); n     fill(v2, v2 + n + 1, false); n     fill(a, a + n + 1, 0); n     for (int i = 1; i <= n; ++i) n       if (!v[i]) solve(i); n     if (count(a + 1, a + n + 1, 0) == n) { n       puts( NO ); n     } else { n       puts( YES ); n       for (int i = 1; i <= n; ++i) printf( %d  , a[i]); n       puts(  ); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   cin >> n; n   string rat[100], cw[100], man[100], c; n   int a = 0, b = 0, d = 0; n   for (int i = 0; i < n; i++) { n     string name, s; n     cin >> name >> s; n     if (s ==  captain ) n       c = name; n     else if (s ==  woman  || s ==  child ) { n       cw[a] = name; n       a++; n     } else if (s ==  rat ) { n       rat[b] = name; n       b++; n     } else if (s ==  man ) { n       man[d] = name; n       d++; n     } else if (s ==  captain ) n       c = name; n   } n   for (int i = 0; i <= b; i++) { n     if (i == b) n       cout << rat[i]; n     else n       cout << rat[i] << endl; n   } n   for (int i = 0; i <= a; i++) { n     if (i == a) n       cout << cw[i]; n     else n       cout << cw[i] << endl; n   } n   for (int i = 0; i <= d; i++) { n     if (i == d) n       cout << man[i]; n     else n       cout << man[i] << endl; n   } n   cout << c << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m, k; n int p[2010] = {0}; n int cnt = 0; n int sum[2010]; n int dp[2010]; n int a[200010]; n int main() { n   scanf( %d%d%d , &n, &m, &k); n   for (int i = 1; i <= n; ++i) scanf( %d , &a[i]); n   sort(a + 1, a + n + 1); n   n = min(n, k); n   for (int i = 1, x, y; i <= m; ++i) { n     scanf( %d%d , &x, &y); n     if (x > k) continue; n     p[x] = max(p[x], y); n   } n   sum[0] = 0; n   for (int i = 1; i <= n; ++i) sum[i] = sum[i - 1] + a[i]; n   memset(dp, 0x3f3f3f3f, sizeof dp); n   dp[0] = 0; n   for (int i = 1; i <= k; ++i) { n     for (int j = 0; j < i; ++j) { n       dp[i] = min(dp[i], dp[j] + sum[i] - sum[j + p[i - j]]); n     } n   } n   printf( %d n , dp[k]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, min = INT_MAX, count = 0; n   int arr[1000001]; n   int j; n   cin >> n; n   for (j = 0; j < n; j++) cin >> arr[j]; n   sort(arr, arr + n); n   int i, diff; n   for (i = 0; i < n - 1; i++) { n     j = i + 1; n     diff = arr[j] - arr[i]; n     if (diff < 0) diff = -diff; n     if (diff < min) { n       min = diff; n       count = 1; n     } else if (diff == min) n       count++; n   } n   cout << min <<     << count; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MOD = 1e9 + 7; n using namespace std; n int num[1001]; n long long n; n long long check(long long k) { n   long long p = 0, v = 0; n   long long t = n; n   while (t) { n     if (t >= k) { n       p += k; n       t -= k; n     } else { n       p += t; n       t = 0; n     } n     v += t / 10; n     t -= t / 10; n   } n   if (v <= p) n     return 1; n   else n     return 0; n } n int main() { n   cin >> n; n   long long l = 1; n   long long r = 1e18; n   long long ans = 0; n   while (l <= r) { n     long long mid = (l + r) / 2; n     if (check(mid)) { n       ans = mid; n       r = mid - 1; n     } else { n       l = mid + 1; n     } n   } n   cout << ans << endl; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 100005; n const int mod = 1e9 + 7; n int n, m, d, k, x, ans, pos[N], a[30]; n bool f[1 << 20]; n int cal(int x) { n   int res = 0; n   for (int i = (0); i < (m); i++) n     if (x & (1 << i)) res++; n   return res; n } n int main() { n   scanf( %d%d%d , &n, &m, &d); n   for (int i = (0); i < (m); i++) { n     scanf( %d , &k); n     for (int j = (0); j < (k); j++) { n       scanf( %d , &x); n       pos[x] = i; n     } n   } n   for (int i = (1); i < (d + 1); i++) a[pos[i]]++; n   for (int i = (d); i < (n + 1); i++) { n     if (i > d) a[pos[i]]++, a[pos[i - d]]--; n     ; n     x = 0; n     for (int j = (0); j < (m); j++) n       if (!a[j]) x += (1 << j); n     f[x] = 1; n   } n   ans = m; n   for (int i = (1 << m) - 1; i >= 1; i--) { n     if (f[i]) { n       for (int j = (0); j < (m); j++) n         if ((1 << j) & i) { n           f[i - (1 << j)] = 1; n         } n     } else n       ans = min(ans, cal(i)); n   } n   printf( %d , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e5 + 7; n const int MOD = 1e9 + 7; n int add(int a, int b) { return a + b >= MOD ? a + b - MOD : a + b; } n int mul(int a, int b) { return 1ll * a * b % MOD; } n int n, x[N], y[N]; n vector<int> g[N]; n map<int, int> px, py; n int far[N], cnt, cir; n void dfs(int u, int fa) { n   if (!px.count(x[u])) cnt++; n   px[x[u]] = u; n   if (!py.count(y[u])) cnt++; n   py[y[u]] = u; n   for (auto v : g[u]) { n     if (v == fa) continue; n     if (~far[v]) { n       cir = 1; n       continue; n     } n     far[v] = u; n     dfs(v, u); n   } n } n int pw2[N * 2]; n void solve() { n   for (int i = (0); i < (n); ++i) g[i].clear(); n   px.clear(); n   py.clear(); n   for (int i = (0); i < (n); ++i) { n     int &x = ::x[i], &y = ::y[i]; n     scanf( %d%d , &x, &y); n     if (px.count(x)) { n       int j = px[x]; n       g[i].push_back(j); n       g[j].push_back(i); n     } n     px[x] = i; n     if (py.count(y)) { n       int j = py[y]; n       g[i].push_back(j); n       g[j].push_back(i); n     } n     py[y] = i; n   } n   int ans = 1; n   fill_n(far, n, -1); n   pw2[0] = 1; n   for (int i = (1); i < (2 * n + 1); ++i) pw2[i] = add(pw2[i - 1], pw2[i - 1]); n   for (int i = (0); i < (n); ++i) n     if (!~far[i]) { n       px.clear(); n       py.clear(); n       far[i] = cnt = cir = 0; n       dfs(i, -1); n       ans = mul(ans, pw2[cnt] - !cir); n     } n   printf( %d n , ans); n } n int main() { n   while (~scanf( %d , &n)) solve(); n   return 0; n } n